xref: /openbmc/linux/security/selinux/hooks.c (revision 3e3ff15e6d8ba931fa9a6c7f9fe711edc77e96e5)
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>
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or modify
171da177e4SLinus Torvalds  *	it under the terms of the GNU General Public License version 2,
181da177e4SLinus Torvalds  *      as published by the Free Software Foundation.
191da177e4SLinus Torvalds  */
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds #include <linux/config.h>
221da177e4SLinus Torvalds #include <linux/module.h>
231da177e4SLinus Torvalds #include <linux/init.h>
241da177e4SLinus Torvalds #include <linux/kernel.h>
251da177e4SLinus Torvalds #include <linux/ptrace.h>
261da177e4SLinus Torvalds #include <linux/errno.h>
271da177e4SLinus Torvalds #include <linux/sched.h>
281da177e4SLinus Torvalds #include <linux/security.h>
291da177e4SLinus Torvalds #include <linux/xattr.h>
301da177e4SLinus Torvalds #include <linux/capability.h>
311da177e4SLinus Torvalds #include <linux/unistd.h>
321da177e4SLinus Torvalds #include <linux/mm.h>
331da177e4SLinus Torvalds #include <linux/mman.h>
341da177e4SLinus Torvalds #include <linux/slab.h>
351da177e4SLinus Torvalds #include <linux/pagemap.h>
361da177e4SLinus Torvalds #include <linux/swap.h>
371da177e4SLinus Torvalds #include <linux/smp_lock.h>
381da177e4SLinus Torvalds #include <linux/spinlock.h>
391da177e4SLinus Torvalds #include <linux/syscalls.h>
401da177e4SLinus Torvalds #include <linux/file.h>
411da177e4SLinus Torvalds #include <linux/namei.h>
421da177e4SLinus Torvalds #include <linux/mount.h>
431da177e4SLinus Torvalds #include <linux/ext2_fs.h>
441da177e4SLinus Torvalds #include <linux/proc_fs.h>
451da177e4SLinus Torvalds #include <linux/kd.h>
461da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h>
471da177e4SLinus Torvalds #include <linux/netfilter_ipv6.h>
481da177e4SLinus Torvalds #include <linux/tty.h>
491da177e4SLinus Torvalds #include <net/icmp.h>
501da177e4SLinus Torvalds #include <net/ip.h>		/* for sysctl_local_port_range[] */
511da177e4SLinus Torvalds #include <net/tcp.h>		/* struct or_callable used in sock_rcv_skb */
521da177e4SLinus Torvalds #include <asm/uaccess.h>
531da177e4SLinus Torvalds #include <asm/semaphore.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>
611da177e4SLinus Torvalds #include <linux/quota.h>
621da177e4SLinus Torvalds #include <linux/un.h>		/* for Unix socket types */
631da177e4SLinus Torvalds #include <net/af_unix.h>	/* for Unix socket types */
641da177e4SLinus Torvalds #include <linux/parser.h>
651da177e4SLinus Torvalds #include <linux/nfs_mount.h>
661da177e4SLinus Torvalds #include <net/ipv6.h>
671da177e4SLinus Torvalds #include <linux/hugetlb.h>
681da177e4SLinus Torvalds #include <linux/personality.h>
691da177e4SLinus Torvalds #include <linux/sysctl.h>
701da177e4SLinus Torvalds #include <linux/audit.h>
716931dfc9SEric Paris #include <linux/string.h>
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds #include "avc.h"
741da177e4SLinus Torvalds #include "objsec.h"
751da177e4SLinus Torvalds #include "netif.h"
76d28d1e08STrent Jaeger #include "xfrm.h"
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds #define XATTR_SELINUX_SUFFIX "selinux"
791da177e4SLinus Torvalds #define XATTR_NAME_SELINUX XATTR_SECURITY_PREFIX XATTR_SELINUX_SUFFIX
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds extern unsigned int policydb_loaded_version;
821da177e4SLinus Torvalds extern int selinux_nlmsg_lookup(u16 sclass, u16 nlmsg_type, u32 *perm);
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
851da177e4SLinus Torvalds int selinux_enforcing = 0;
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds static int __init enforcing_setup(char *str)
881da177e4SLinus Torvalds {
891da177e4SLinus Torvalds 	selinux_enforcing = simple_strtol(str,NULL,0);
901da177e4SLinus Torvalds 	return 1;
911da177e4SLinus Torvalds }
921da177e4SLinus Torvalds __setup("enforcing=", enforcing_setup);
931da177e4SLinus Torvalds #endif
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
961da177e4SLinus Torvalds int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE;
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds static int __init selinux_enabled_setup(char *str)
991da177e4SLinus Torvalds {
1001da177e4SLinus Torvalds 	selinux_enabled = simple_strtol(str, NULL, 0);
1011da177e4SLinus Torvalds 	return 1;
1021da177e4SLinus Torvalds }
1031da177e4SLinus Torvalds __setup("selinux=", selinux_enabled_setup);
10430d55280SStephen Smalley #else
10530d55280SStephen Smalley int selinux_enabled = 1;
1061da177e4SLinus Torvalds #endif
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds /* Original (dummy) security module. */
1091da177e4SLinus Torvalds static struct security_operations *original_ops = NULL;
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds /* Minimal support for a secondary security module,
1121da177e4SLinus Torvalds    just to allow the use of the dummy or capability modules.
1131da177e4SLinus Torvalds    The owlsm module can alternatively be used as a secondary
1141da177e4SLinus Torvalds    module as long as CONFIG_OWLSM_FD is not enabled. */
1151da177e4SLinus Torvalds static struct security_operations *secondary_ops = NULL;
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds /* Lists of inode and superblock security structures initialized
1181da177e4SLinus Torvalds    before the policy was loaded. */
1191da177e4SLinus Torvalds static LIST_HEAD(superblock_security_head);
1201da177e4SLinus Torvalds static DEFINE_SPINLOCK(sb_security_lock);
1211da177e4SLinus Torvalds 
1227cae7e26SJames Morris static kmem_cache_t *sel_inode_cache;
1237cae7e26SJames Morris 
1248c8570fbSDustin Kirkland /* Return security context for a given sid or just the context
1258c8570fbSDustin Kirkland    length if the buffer is null or length is 0 */
1268c8570fbSDustin Kirkland static int selinux_getsecurity(u32 sid, void *buffer, size_t size)
1278c8570fbSDustin Kirkland {
1288c8570fbSDustin Kirkland 	char *context;
1298c8570fbSDustin Kirkland 	unsigned len;
1308c8570fbSDustin Kirkland 	int rc;
1318c8570fbSDustin Kirkland 
1328c8570fbSDustin Kirkland 	rc = security_sid_to_context(sid, &context, &len);
1338c8570fbSDustin Kirkland 	if (rc)
1348c8570fbSDustin Kirkland 		return rc;
1358c8570fbSDustin Kirkland 
1368c8570fbSDustin Kirkland 	if (!buffer || !size)
1378c8570fbSDustin Kirkland 		goto getsecurity_exit;
1388c8570fbSDustin Kirkland 
1398c8570fbSDustin Kirkland 	if (size < len) {
1408c8570fbSDustin Kirkland 		len = -ERANGE;
1418c8570fbSDustin Kirkland 		goto getsecurity_exit;
1428c8570fbSDustin Kirkland 	}
1438c8570fbSDustin Kirkland 	memcpy(buffer, context, len);
1448c8570fbSDustin Kirkland 
1458c8570fbSDustin Kirkland getsecurity_exit:
1468c8570fbSDustin Kirkland 	kfree(context);
1478c8570fbSDustin Kirkland 	return len;
1488c8570fbSDustin Kirkland }
1498c8570fbSDustin Kirkland 
1501da177e4SLinus Torvalds /* Allocate and free functions for each kind of security blob. */
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds static int task_alloc_security(struct task_struct *task)
1531da177e4SLinus Torvalds {
1541da177e4SLinus Torvalds 	struct task_security_struct *tsec;
1551da177e4SLinus Torvalds 
15689d155efSJames Morris 	tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL);
1571da177e4SLinus Torvalds 	if (!tsec)
1581da177e4SLinus Torvalds 		return -ENOMEM;
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds 	tsec->task = task;
1611da177e4SLinus Torvalds 	tsec->osid = tsec->sid = tsec->ptrace_sid = SECINITSID_UNLABELED;
1621da177e4SLinus Torvalds 	task->security = tsec;
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	return 0;
1651da177e4SLinus Torvalds }
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds static void task_free_security(struct task_struct *task)
1681da177e4SLinus Torvalds {
1691da177e4SLinus Torvalds 	struct task_security_struct *tsec = task->security;
1701da177e4SLinus Torvalds 	task->security = NULL;
1711da177e4SLinus Torvalds 	kfree(tsec);
1721da177e4SLinus Torvalds }
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds static int inode_alloc_security(struct inode *inode)
1751da177e4SLinus Torvalds {
1761da177e4SLinus Torvalds 	struct task_security_struct *tsec = current->security;
1771da177e4SLinus Torvalds 	struct inode_security_struct *isec;
1781da177e4SLinus Torvalds 
1797cae7e26SJames Morris 	isec = kmem_cache_alloc(sel_inode_cache, SLAB_KERNEL);
1801da177e4SLinus Torvalds 	if (!isec)
1811da177e4SLinus Torvalds 		return -ENOMEM;
1821da177e4SLinus Torvalds 
1837cae7e26SJames Morris 	memset(isec, 0, sizeof(*isec));
1841da177e4SLinus Torvalds 	init_MUTEX(&isec->sem);
1851da177e4SLinus Torvalds 	INIT_LIST_HEAD(&isec->list);
1861da177e4SLinus Torvalds 	isec->inode = inode;
1871da177e4SLinus Torvalds 	isec->sid = SECINITSID_UNLABELED;
1881da177e4SLinus Torvalds 	isec->sclass = SECCLASS_FILE;
1891da177e4SLinus Torvalds 	isec->task_sid = tsec->sid;
1901da177e4SLinus Torvalds 	inode->i_security = isec;
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	return 0;
1931da177e4SLinus Torvalds }
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds static void inode_free_security(struct inode *inode)
1961da177e4SLinus Torvalds {
1971da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
1981da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = inode->i_sb->s_security;
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds 	spin_lock(&sbsec->isec_lock);
2011da177e4SLinus Torvalds 	if (!list_empty(&isec->list))
2021da177e4SLinus Torvalds 		list_del_init(&isec->list);
2031da177e4SLinus Torvalds 	spin_unlock(&sbsec->isec_lock);
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds 	inode->i_security = NULL;
2067cae7e26SJames Morris 	kmem_cache_free(sel_inode_cache, isec);
2071da177e4SLinus Torvalds }
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds static int file_alloc_security(struct file *file)
2101da177e4SLinus Torvalds {
2111da177e4SLinus Torvalds 	struct task_security_struct *tsec = current->security;
2121da177e4SLinus Torvalds 	struct file_security_struct *fsec;
2131da177e4SLinus Torvalds 
21426d2a4beSStephen Smalley 	fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL);
2151da177e4SLinus Torvalds 	if (!fsec)
2161da177e4SLinus Torvalds 		return -ENOMEM;
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds 	fsec->file = file;
2191da177e4SLinus Torvalds 	fsec->sid = tsec->sid;
2201da177e4SLinus Torvalds 	fsec->fown_sid = tsec->sid;
2211da177e4SLinus Torvalds 	file->f_security = fsec;
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds 	return 0;
2241da177e4SLinus Torvalds }
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds static void file_free_security(struct file *file)
2271da177e4SLinus Torvalds {
2281da177e4SLinus Torvalds 	struct file_security_struct *fsec = file->f_security;
2291da177e4SLinus Torvalds 	file->f_security = NULL;
2301da177e4SLinus Torvalds 	kfree(fsec);
2311da177e4SLinus Torvalds }
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds static int superblock_alloc_security(struct super_block *sb)
2341da177e4SLinus Torvalds {
2351da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
2361da177e4SLinus Torvalds 
23789d155efSJames Morris 	sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
2381da177e4SLinus Torvalds 	if (!sbsec)
2391da177e4SLinus Torvalds 		return -ENOMEM;
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	init_MUTEX(&sbsec->sem);
2421da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sbsec->list);
2431da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sbsec->isec_head);
2441da177e4SLinus Torvalds 	spin_lock_init(&sbsec->isec_lock);
2451da177e4SLinus Torvalds 	sbsec->sb = sb;
2461da177e4SLinus Torvalds 	sbsec->sid = SECINITSID_UNLABELED;
2471da177e4SLinus Torvalds 	sbsec->def_sid = SECINITSID_FILE;
2481da177e4SLinus Torvalds 	sb->s_security = sbsec;
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds 	return 0;
2511da177e4SLinus Torvalds }
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds static void superblock_free_security(struct super_block *sb)
2541da177e4SLinus Torvalds {
2551da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = sb->s_security;
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds 	spin_lock(&sb_security_lock);
2581da177e4SLinus Torvalds 	if (!list_empty(&sbsec->list))
2591da177e4SLinus Torvalds 		list_del_init(&sbsec->list);
2601da177e4SLinus Torvalds 	spin_unlock(&sb_security_lock);
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 	sb->s_security = NULL;
2631da177e4SLinus Torvalds 	kfree(sbsec);
2641da177e4SLinus Torvalds }
2651da177e4SLinus Torvalds 
2667d877f3bSAl Viro static int sk_alloc_security(struct sock *sk, int family, gfp_t priority)
2671da177e4SLinus Torvalds {
2681da177e4SLinus Torvalds 	struct sk_security_struct *ssec;
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 	if (family != PF_UNIX)
2711da177e4SLinus Torvalds 		return 0;
2721da177e4SLinus Torvalds 
27389d155efSJames Morris 	ssec = kzalloc(sizeof(*ssec), priority);
2741da177e4SLinus Torvalds 	if (!ssec)
2751da177e4SLinus Torvalds 		return -ENOMEM;
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 	ssec->sk = sk;
2781da177e4SLinus Torvalds 	ssec->peer_sid = SECINITSID_UNLABELED;
2791da177e4SLinus Torvalds 	sk->sk_security = ssec;
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 	return 0;
2821da177e4SLinus Torvalds }
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds static void sk_free_security(struct sock *sk)
2851da177e4SLinus Torvalds {
2861da177e4SLinus Torvalds 	struct sk_security_struct *ssec = sk->sk_security;
2871da177e4SLinus Torvalds 
2889ac49d22SStephen Smalley 	if (sk->sk_family != PF_UNIX)
2891da177e4SLinus Torvalds 		return;
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	sk->sk_security = NULL;
2921da177e4SLinus Torvalds 	kfree(ssec);
2931da177e4SLinus Torvalds }
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds /* The security server must be initialized before
2961da177e4SLinus Torvalds    any labeling or access decisions can be provided. */
2971da177e4SLinus Torvalds extern int ss_initialized;
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds /* The file system's label must be initialized prior to use. */
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds static char *labeling_behaviors[6] = {
3021da177e4SLinus Torvalds 	"uses xattr",
3031da177e4SLinus Torvalds 	"uses transition SIDs",
3041da177e4SLinus Torvalds 	"uses task SIDs",
3051da177e4SLinus Torvalds 	"uses genfs_contexts",
3061da177e4SLinus Torvalds 	"not configured for labeling",
3071da177e4SLinus Torvalds 	"uses mountpoint labeling",
3081da177e4SLinus Torvalds };
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds static inline int inode_doinit(struct inode *inode)
3131da177e4SLinus Torvalds {
3141da177e4SLinus Torvalds 	return inode_doinit_with_dentry(inode, NULL);
3151da177e4SLinus Torvalds }
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds enum {
3181da177e4SLinus Torvalds 	Opt_context = 1,
3191da177e4SLinus Torvalds 	Opt_fscontext = 2,
3201da177e4SLinus Torvalds 	Opt_defcontext = 4,
3211da177e4SLinus Torvalds };
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds static match_table_t tokens = {
3241da177e4SLinus Torvalds 	{Opt_context, "context=%s"},
3251da177e4SLinus Torvalds 	{Opt_fscontext, "fscontext=%s"},
3261da177e4SLinus Torvalds 	{Opt_defcontext, "defcontext=%s"},
3271da177e4SLinus Torvalds };
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds #define SEL_MOUNT_FAIL_MSG "SELinux:  duplicate or incompatible mount options\n"
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds static int try_context_mount(struct super_block *sb, void *data)
3321da177e4SLinus Torvalds {
3331da177e4SLinus Torvalds 	char *context = NULL, *defcontext = NULL;
3341da177e4SLinus Torvalds 	const char *name;
3351da177e4SLinus Torvalds 	u32 sid;
3361da177e4SLinus Torvalds 	int alloc = 0, rc = 0, seen = 0;
3371da177e4SLinus Torvalds 	struct task_security_struct *tsec = current->security;
3381da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = sb->s_security;
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 	if (!data)
3411da177e4SLinus Torvalds 		goto out;
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	name = sb->s_type->name;
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 	if (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA) {
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds 		/* NFS we understand. */
3481da177e4SLinus Torvalds 		if (!strcmp(name, "nfs")) {
3491da177e4SLinus Torvalds 			struct nfs_mount_data *d = data;
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 			if (d->version <  NFS_MOUNT_VERSION)
3521da177e4SLinus Torvalds 				goto out;
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds 			if (d->context[0]) {
3551da177e4SLinus Torvalds 				context = d->context;
3561da177e4SLinus Torvalds 				seen |= Opt_context;
3571da177e4SLinus Torvalds 			}
3581da177e4SLinus Torvalds 		} else
3591da177e4SLinus Torvalds 			goto out;
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 	} else {
3621da177e4SLinus Torvalds 		/* Standard string-based options. */
3631da177e4SLinus Torvalds 		char *p, *options = data;
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds 		while ((p = strsep(&options, ",")) != NULL) {
3661da177e4SLinus Torvalds 			int token;
3671da177e4SLinus Torvalds 			substring_t args[MAX_OPT_ARGS];
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 			if (!*p)
3701da177e4SLinus Torvalds 				continue;
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds 			token = match_token(p, tokens, args);
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 			switch (token) {
3751da177e4SLinus Torvalds 			case Opt_context:
3761da177e4SLinus Torvalds 				if (seen) {
3771da177e4SLinus Torvalds 					rc = -EINVAL;
3781da177e4SLinus Torvalds 					printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
3791da177e4SLinus Torvalds 					goto out_free;
3801da177e4SLinus Torvalds 				}
3811da177e4SLinus Torvalds 				context = match_strdup(&args[0]);
3821da177e4SLinus Torvalds 				if (!context) {
3831da177e4SLinus Torvalds 					rc = -ENOMEM;
3841da177e4SLinus Torvalds 					goto out_free;
3851da177e4SLinus Torvalds 				}
3861da177e4SLinus Torvalds 				if (!alloc)
3871da177e4SLinus Torvalds 					alloc = 1;
3881da177e4SLinus Torvalds 				seen |= Opt_context;
3891da177e4SLinus Torvalds 				break;
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 			case Opt_fscontext:
3921da177e4SLinus Torvalds 				if (seen & (Opt_context|Opt_fscontext)) {
3931da177e4SLinus Torvalds 					rc = -EINVAL;
3941da177e4SLinus Torvalds 					printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
3951da177e4SLinus Torvalds 					goto out_free;
3961da177e4SLinus Torvalds 				}
3971da177e4SLinus Torvalds 				context = match_strdup(&args[0]);
3981da177e4SLinus Torvalds 				if (!context) {
3991da177e4SLinus Torvalds 					rc = -ENOMEM;
4001da177e4SLinus Torvalds 					goto out_free;
4011da177e4SLinus Torvalds 				}
4021da177e4SLinus Torvalds 				if (!alloc)
4031da177e4SLinus Torvalds 					alloc = 1;
4041da177e4SLinus Torvalds 				seen |= Opt_fscontext;
4051da177e4SLinus Torvalds 				break;
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds 			case Opt_defcontext:
4081da177e4SLinus Torvalds 				if (sbsec->behavior != SECURITY_FS_USE_XATTR) {
4091da177e4SLinus Torvalds 					rc = -EINVAL;
4101da177e4SLinus Torvalds 					printk(KERN_WARNING "SELinux:  "
4111da177e4SLinus Torvalds 					       "defcontext option is invalid "
4121da177e4SLinus Torvalds 					       "for this filesystem type\n");
4131da177e4SLinus Torvalds 					goto out_free;
4141da177e4SLinus Torvalds 				}
4151da177e4SLinus Torvalds 				if (seen & (Opt_context|Opt_defcontext)) {
4161da177e4SLinus Torvalds 					rc = -EINVAL;
4171da177e4SLinus Torvalds 					printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
4181da177e4SLinus Torvalds 					goto out_free;
4191da177e4SLinus Torvalds 				}
4201da177e4SLinus Torvalds 				defcontext = match_strdup(&args[0]);
4211da177e4SLinus Torvalds 				if (!defcontext) {
4221da177e4SLinus Torvalds 					rc = -ENOMEM;
4231da177e4SLinus Torvalds 					goto out_free;
4241da177e4SLinus Torvalds 				}
4251da177e4SLinus Torvalds 				if (!alloc)
4261da177e4SLinus Torvalds 					alloc = 1;
4271da177e4SLinus Torvalds 				seen |= Opt_defcontext;
4281da177e4SLinus Torvalds 				break;
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 			default:
4311da177e4SLinus Torvalds 				rc = -EINVAL;
4321da177e4SLinus Torvalds 				printk(KERN_WARNING "SELinux:  unknown mount "
4331da177e4SLinus Torvalds 				       "option\n");
4341da177e4SLinus Torvalds 				goto out_free;
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 			}
4371da177e4SLinus Torvalds 		}
4381da177e4SLinus Torvalds 	}
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 	if (!seen)
4411da177e4SLinus Torvalds 		goto out;
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds 	if (context) {
4441da177e4SLinus Torvalds 		rc = security_context_to_sid(context, strlen(context), &sid);
4451da177e4SLinus Torvalds 		if (rc) {
4461da177e4SLinus Torvalds 			printk(KERN_WARNING "SELinux: security_context_to_sid"
4471da177e4SLinus Torvalds 			       "(%s) failed for (dev %s, type %s) errno=%d\n",
4481da177e4SLinus Torvalds 			       context, sb->s_id, name, rc);
4491da177e4SLinus Torvalds 			goto out_free;
4501da177e4SLinus Torvalds 		}
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds 		rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
4531da177e4SLinus Torvalds 		                  FILESYSTEM__RELABELFROM, NULL);
4541da177e4SLinus Torvalds 		if (rc)
4551da177e4SLinus Torvalds 			goto out_free;
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 		rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM,
4581da177e4SLinus Torvalds 		                  FILESYSTEM__RELABELTO, NULL);
4591da177e4SLinus Torvalds 		if (rc)
4601da177e4SLinus Torvalds 			goto out_free;
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds 		sbsec->sid = sid;
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds 		if (seen & Opt_context)
4651da177e4SLinus Torvalds 			sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
4661da177e4SLinus Torvalds 	}
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds 	if (defcontext) {
4691da177e4SLinus Torvalds 		rc = security_context_to_sid(defcontext, strlen(defcontext), &sid);
4701da177e4SLinus Torvalds 		if (rc) {
4711da177e4SLinus Torvalds 			printk(KERN_WARNING "SELinux: security_context_to_sid"
4721da177e4SLinus Torvalds 			       "(%s) failed for (dev %s, type %s) errno=%d\n",
4731da177e4SLinus Torvalds 			       defcontext, sb->s_id, name, rc);
4741da177e4SLinus Torvalds 			goto out_free;
4751da177e4SLinus Torvalds 		}
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 		if (sid == sbsec->def_sid)
4781da177e4SLinus Torvalds 			goto out_free;
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds 		rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
4811da177e4SLinus Torvalds 				  FILESYSTEM__RELABELFROM, NULL);
4821da177e4SLinus Torvalds 		if (rc)
4831da177e4SLinus Torvalds 			goto out_free;
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds 		rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM,
4861da177e4SLinus Torvalds 				  FILESYSTEM__ASSOCIATE, NULL);
4871da177e4SLinus Torvalds 		if (rc)
4881da177e4SLinus Torvalds 			goto out_free;
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds 		sbsec->def_sid = sid;
4911da177e4SLinus Torvalds 	}
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds out_free:
4941da177e4SLinus Torvalds 	if (alloc) {
4951da177e4SLinus Torvalds 		kfree(context);
4961da177e4SLinus Torvalds 		kfree(defcontext);
4971da177e4SLinus Torvalds 	}
4981da177e4SLinus Torvalds out:
4991da177e4SLinus Torvalds 	return rc;
5001da177e4SLinus Torvalds }
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds static int superblock_doinit(struct super_block *sb, void *data)
5031da177e4SLinus Torvalds {
5041da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = sb->s_security;
5051da177e4SLinus Torvalds 	struct dentry *root = sb->s_root;
5061da177e4SLinus Torvalds 	struct inode *inode = root->d_inode;
5071da177e4SLinus Torvalds 	int rc = 0;
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds 	down(&sbsec->sem);
5101da177e4SLinus Torvalds 	if (sbsec->initialized)
5111da177e4SLinus Torvalds 		goto out;
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds 	if (!ss_initialized) {
5141da177e4SLinus Torvalds 		/* Defer initialization until selinux_complete_init,
5151da177e4SLinus Torvalds 		   after the initial policy is loaded and the security
5161da177e4SLinus Torvalds 		   server is ready to handle calls. */
5171da177e4SLinus Torvalds 		spin_lock(&sb_security_lock);
5181da177e4SLinus Torvalds 		if (list_empty(&sbsec->list))
5191da177e4SLinus Torvalds 			list_add(&sbsec->list, &superblock_security_head);
5201da177e4SLinus Torvalds 		spin_unlock(&sb_security_lock);
5211da177e4SLinus Torvalds 		goto out;
5221da177e4SLinus Torvalds 	}
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds 	/* Determine the labeling behavior to use for this filesystem type. */
5251da177e4SLinus Torvalds 	rc = security_fs_use(sb->s_type->name, &sbsec->behavior, &sbsec->sid);
5261da177e4SLinus Torvalds 	if (rc) {
5271da177e4SLinus Torvalds 		printk(KERN_WARNING "%s:  security_fs_use(%s) returned %d\n",
5281da177e4SLinus Torvalds 		       __FUNCTION__, sb->s_type->name, rc);
5291da177e4SLinus Torvalds 		goto out;
5301da177e4SLinus Torvalds 	}
5311da177e4SLinus Torvalds 
5321da177e4SLinus Torvalds 	rc = try_context_mount(sb, data);
5331da177e4SLinus Torvalds 	if (rc)
5341da177e4SLinus Torvalds 		goto out;
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds 	if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
5371da177e4SLinus Torvalds 		/* Make sure that the xattr handler exists and that no
5381da177e4SLinus Torvalds 		   error other than -ENODATA is returned by getxattr on
5391da177e4SLinus Torvalds 		   the root directory.  -ENODATA is ok, as this may be
5401da177e4SLinus Torvalds 		   the first boot of the SELinux kernel before we have
5411da177e4SLinus Torvalds 		   assigned xattr values to the filesystem. */
5421da177e4SLinus Torvalds 		if (!inode->i_op->getxattr) {
5431da177e4SLinus Torvalds 			printk(KERN_WARNING "SELinux: (dev %s, type %s) has no "
5441da177e4SLinus Torvalds 			       "xattr support\n", sb->s_id, sb->s_type->name);
5451da177e4SLinus Torvalds 			rc = -EOPNOTSUPP;
5461da177e4SLinus Torvalds 			goto out;
5471da177e4SLinus Torvalds 		}
5481da177e4SLinus Torvalds 		rc = inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0);
5491da177e4SLinus Torvalds 		if (rc < 0 && rc != -ENODATA) {
5501da177e4SLinus Torvalds 			if (rc == -EOPNOTSUPP)
5511da177e4SLinus Torvalds 				printk(KERN_WARNING "SELinux: (dev %s, type "
5521da177e4SLinus Torvalds 				       "%s) has no security xattr handler\n",
5531da177e4SLinus Torvalds 				       sb->s_id, sb->s_type->name);
5541da177e4SLinus Torvalds 			else
5551da177e4SLinus Torvalds 				printk(KERN_WARNING "SELinux: (dev %s, type "
5561da177e4SLinus Torvalds 				       "%s) getxattr errno %d\n", sb->s_id,
5571da177e4SLinus Torvalds 				       sb->s_type->name, -rc);
5581da177e4SLinus Torvalds 			goto out;
5591da177e4SLinus Torvalds 		}
5601da177e4SLinus Torvalds 	}
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds 	if (strcmp(sb->s_type->name, "proc") == 0)
5631da177e4SLinus Torvalds 		sbsec->proc = 1;
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 	sbsec->initialized = 1;
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds 	if (sbsec->behavior > ARRAY_SIZE(labeling_behaviors)) {
5681da177e4SLinus Torvalds 		printk(KERN_INFO "SELinux: initialized (dev %s, type %s), unknown behavior\n",
5691da177e4SLinus Torvalds 		       sb->s_id, sb->s_type->name);
5701da177e4SLinus Torvalds 	}
5711da177e4SLinus Torvalds 	else {
5721da177e4SLinus Torvalds 		printk(KERN_INFO "SELinux: initialized (dev %s, type %s), %s\n",
5731da177e4SLinus Torvalds 		       sb->s_id, sb->s_type->name,
5741da177e4SLinus Torvalds 		       labeling_behaviors[sbsec->behavior-1]);
5751da177e4SLinus Torvalds 	}
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds 	/* Initialize the root inode. */
5781da177e4SLinus Torvalds 	rc = inode_doinit_with_dentry(sb->s_root->d_inode, sb->s_root);
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds 	/* Initialize any other inodes associated with the superblock, e.g.
5811da177e4SLinus Torvalds 	   inodes created prior to initial policy load or inodes created
5821da177e4SLinus Torvalds 	   during get_sb by a pseudo filesystem that directly
5831da177e4SLinus Torvalds 	   populates itself. */
5841da177e4SLinus Torvalds 	spin_lock(&sbsec->isec_lock);
5851da177e4SLinus Torvalds next_inode:
5861da177e4SLinus Torvalds 	if (!list_empty(&sbsec->isec_head)) {
5871da177e4SLinus Torvalds 		struct inode_security_struct *isec =
5881da177e4SLinus Torvalds 				list_entry(sbsec->isec_head.next,
5891da177e4SLinus Torvalds 				           struct inode_security_struct, list);
5901da177e4SLinus Torvalds 		struct inode *inode = isec->inode;
5911da177e4SLinus Torvalds 		spin_unlock(&sbsec->isec_lock);
5921da177e4SLinus Torvalds 		inode = igrab(inode);
5931da177e4SLinus Torvalds 		if (inode) {
5941da177e4SLinus Torvalds 			if (!IS_PRIVATE (inode))
5951da177e4SLinus Torvalds 				inode_doinit(inode);
5961da177e4SLinus Torvalds 			iput(inode);
5971da177e4SLinus Torvalds 		}
5981da177e4SLinus Torvalds 		spin_lock(&sbsec->isec_lock);
5991da177e4SLinus Torvalds 		list_del_init(&isec->list);
6001da177e4SLinus Torvalds 		goto next_inode;
6011da177e4SLinus Torvalds 	}
6021da177e4SLinus Torvalds 	spin_unlock(&sbsec->isec_lock);
6031da177e4SLinus Torvalds out:
6041da177e4SLinus Torvalds 	up(&sbsec->sem);
6051da177e4SLinus Torvalds 	return rc;
6061da177e4SLinus Torvalds }
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds static inline u16 inode_mode_to_security_class(umode_t mode)
6091da177e4SLinus Torvalds {
6101da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
6111da177e4SLinus Torvalds 	case S_IFSOCK:
6121da177e4SLinus Torvalds 		return SECCLASS_SOCK_FILE;
6131da177e4SLinus Torvalds 	case S_IFLNK:
6141da177e4SLinus Torvalds 		return SECCLASS_LNK_FILE;
6151da177e4SLinus Torvalds 	case S_IFREG:
6161da177e4SLinus Torvalds 		return SECCLASS_FILE;
6171da177e4SLinus Torvalds 	case S_IFBLK:
6181da177e4SLinus Torvalds 		return SECCLASS_BLK_FILE;
6191da177e4SLinus Torvalds 	case S_IFDIR:
6201da177e4SLinus Torvalds 		return SECCLASS_DIR;
6211da177e4SLinus Torvalds 	case S_IFCHR:
6221da177e4SLinus Torvalds 		return SECCLASS_CHR_FILE;
6231da177e4SLinus Torvalds 	case S_IFIFO:
6241da177e4SLinus Torvalds 		return SECCLASS_FIFO_FILE;
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds 	}
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds 	return SECCLASS_FILE;
6291da177e4SLinus Torvalds }
6301da177e4SLinus Torvalds 
63113402580SJames Morris static inline int default_protocol_stream(int protocol)
63213402580SJames Morris {
63313402580SJames Morris 	return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
63413402580SJames Morris }
63513402580SJames Morris 
63613402580SJames Morris static inline int default_protocol_dgram(int protocol)
63713402580SJames Morris {
63813402580SJames Morris 	return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
63913402580SJames Morris }
64013402580SJames Morris 
6411da177e4SLinus Torvalds static inline u16 socket_type_to_security_class(int family, int type, int protocol)
6421da177e4SLinus Torvalds {
6431da177e4SLinus Torvalds 	switch (family) {
6441da177e4SLinus Torvalds 	case PF_UNIX:
6451da177e4SLinus Torvalds 		switch (type) {
6461da177e4SLinus Torvalds 		case SOCK_STREAM:
6471da177e4SLinus Torvalds 		case SOCK_SEQPACKET:
6481da177e4SLinus Torvalds 			return SECCLASS_UNIX_STREAM_SOCKET;
6491da177e4SLinus Torvalds 		case SOCK_DGRAM:
6501da177e4SLinus Torvalds 			return SECCLASS_UNIX_DGRAM_SOCKET;
6511da177e4SLinus Torvalds 		}
6521da177e4SLinus Torvalds 		break;
6531da177e4SLinus Torvalds 	case PF_INET:
6541da177e4SLinus Torvalds 	case PF_INET6:
6551da177e4SLinus Torvalds 		switch (type) {
6561da177e4SLinus Torvalds 		case SOCK_STREAM:
65713402580SJames Morris 			if (default_protocol_stream(protocol))
6581da177e4SLinus Torvalds 				return SECCLASS_TCP_SOCKET;
65913402580SJames Morris 			else
66013402580SJames Morris 				return SECCLASS_RAWIP_SOCKET;
6611da177e4SLinus Torvalds 		case SOCK_DGRAM:
66213402580SJames Morris 			if (default_protocol_dgram(protocol))
6631da177e4SLinus Torvalds 				return SECCLASS_UDP_SOCKET;
66413402580SJames Morris 			else
66513402580SJames Morris 				return SECCLASS_RAWIP_SOCKET;
66613402580SJames Morris 		default:
6671da177e4SLinus Torvalds 			return SECCLASS_RAWIP_SOCKET;
6681da177e4SLinus Torvalds 		}
6691da177e4SLinus Torvalds 		break;
6701da177e4SLinus Torvalds 	case PF_NETLINK:
6711da177e4SLinus Torvalds 		switch (protocol) {
6721da177e4SLinus Torvalds 		case NETLINK_ROUTE:
6731da177e4SLinus Torvalds 			return SECCLASS_NETLINK_ROUTE_SOCKET;
6741da177e4SLinus Torvalds 		case NETLINK_FIREWALL:
6751da177e4SLinus Torvalds 			return SECCLASS_NETLINK_FIREWALL_SOCKET;
676216efaaaSJames Morris 		case NETLINK_INET_DIAG:
6771da177e4SLinus Torvalds 			return SECCLASS_NETLINK_TCPDIAG_SOCKET;
6781da177e4SLinus Torvalds 		case NETLINK_NFLOG:
6791da177e4SLinus Torvalds 			return SECCLASS_NETLINK_NFLOG_SOCKET;
6801da177e4SLinus Torvalds 		case NETLINK_XFRM:
6811da177e4SLinus Torvalds 			return SECCLASS_NETLINK_XFRM_SOCKET;
6821da177e4SLinus Torvalds 		case NETLINK_SELINUX:
6831da177e4SLinus Torvalds 			return SECCLASS_NETLINK_SELINUX_SOCKET;
6841da177e4SLinus Torvalds 		case NETLINK_AUDIT:
6851da177e4SLinus Torvalds 			return SECCLASS_NETLINK_AUDIT_SOCKET;
6861da177e4SLinus Torvalds 		case NETLINK_IP6_FW:
6871da177e4SLinus Torvalds 			return SECCLASS_NETLINK_IP6FW_SOCKET;
6881da177e4SLinus Torvalds 		case NETLINK_DNRTMSG:
6891da177e4SLinus Torvalds 			return SECCLASS_NETLINK_DNRT_SOCKET;
6900c9b7942SJames Morris 		case NETLINK_KOBJECT_UEVENT:
6910c9b7942SJames Morris 			return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
6921da177e4SLinus Torvalds 		default:
6931da177e4SLinus Torvalds 			return SECCLASS_NETLINK_SOCKET;
6941da177e4SLinus Torvalds 		}
6951da177e4SLinus Torvalds 	case PF_PACKET:
6961da177e4SLinus Torvalds 		return SECCLASS_PACKET_SOCKET;
6971da177e4SLinus Torvalds 	case PF_KEY:
6981da177e4SLinus Torvalds 		return SECCLASS_KEY_SOCKET;
699*3e3ff15eSChristopher J. PeBenito 	case PF_APPLETALK:
700*3e3ff15eSChristopher J. PeBenito 		return SECCLASS_APPLETALK_SOCKET;
7011da177e4SLinus Torvalds 	}
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds 	return SECCLASS_SOCKET;
7041da177e4SLinus Torvalds }
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
7071da177e4SLinus Torvalds static int selinux_proc_get_sid(struct proc_dir_entry *de,
7081da177e4SLinus Torvalds 				u16 tclass,
7091da177e4SLinus Torvalds 				u32 *sid)
7101da177e4SLinus Torvalds {
7111da177e4SLinus Torvalds 	int buflen, rc;
7121da177e4SLinus Torvalds 	char *buffer, *path, *end;
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds 	buffer = (char*)__get_free_page(GFP_KERNEL);
7151da177e4SLinus Torvalds 	if (!buffer)
7161da177e4SLinus Torvalds 		return -ENOMEM;
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds 	buflen = PAGE_SIZE;
7191da177e4SLinus Torvalds 	end = buffer+buflen;
7201da177e4SLinus Torvalds 	*--end = '\0';
7211da177e4SLinus Torvalds 	buflen--;
7221da177e4SLinus Torvalds 	path = end-1;
7231da177e4SLinus Torvalds 	*path = '/';
7241da177e4SLinus Torvalds 	while (de && de != de->parent) {
7251da177e4SLinus Torvalds 		buflen -= de->namelen + 1;
7261da177e4SLinus Torvalds 		if (buflen < 0)
7271da177e4SLinus Torvalds 			break;
7281da177e4SLinus Torvalds 		end -= de->namelen;
7291da177e4SLinus Torvalds 		memcpy(end, de->name, de->namelen);
7301da177e4SLinus Torvalds 		*--end = '/';
7311da177e4SLinus Torvalds 		path = end;
7321da177e4SLinus Torvalds 		de = de->parent;
7331da177e4SLinus Torvalds 	}
7341da177e4SLinus Torvalds 	rc = security_genfs_sid("proc", path, tclass, sid);
7351da177e4SLinus Torvalds 	free_page((unsigned long)buffer);
7361da177e4SLinus Torvalds 	return rc;
7371da177e4SLinus Torvalds }
7381da177e4SLinus Torvalds #else
7391da177e4SLinus Torvalds static int selinux_proc_get_sid(struct proc_dir_entry *de,
7401da177e4SLinus Torvalds 				u16 tclass,
7411da177e4SLinus Torvalds 				u32 *sid)
7421da177e4SLinus Torvalds {
7431da177e4SLinus Torvalds 	return -EINVAL;
7441da177e4SLinus Torvalds }
7451da177e4SLinus Torvalds #endif
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds /* The inode's security attributes must be initialized before first use. */
7481da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
7491da177e4SLinus Torvalds {
7501da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = NULL;
7511da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
7521da177e4SLinus Torvalds 	u32 sid;
7531da177e4SLinus Torvalds 	struct dentry *dentry;
7541da177e4SLinus Torvalds #define INITCONTEXTLEN 255
7551da177e4SLinus Torvalds 	char *context = NULL;
7561da177e4SLinus Torvalds 	unsigned len = 0;
7571da177e4SLinus Torvalds 	int rc = 0;
7581da177e4SLinus Torvalds 	int hold_sem = 0;
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 	if (isec->initialized)
7611da177e4SLinus Torvalds 		goto out;
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds 	down(&isec->sem);
7641da177e4SLinus Torvalds 	hold_sem = 1;
7651da177e4SLinus Torvalds 	if (isec->initialized)
7661da177e4SLinus Torvalds 		goto out;
7671da177e4SLinus Torvalds 
7681da177e4SLinus Torvalds 	sbsec = inode->i_sb->s_security;
7691da177e4SLinus Torvalds 	if (!sbsec->initialized) {
7701da177e4SLinus Torvalds 		/* Defer initialization until selinux_complete_init,
7711da177e4SLinus Torvalds 		   after the initial policy is loaded and the security
7721da177e4SLinus Torvalds 		   server is ready to handle calls. */
7731da177e4SLinus Torvalds 		spin_lock(&sbsec->isec_lock);
7741da177e4SLinus Torvalds 		if (list_empty(&isec->list))
7751da177e4SLinus Torvalds 			list_add(&isec->list, &sbsec->isec_head);
7761da177e4SLinus Torvalds 		spin_unlock(&sbsec->isec_lock);
7771da177e4SLinus Torvalds 		goto out;
7781da177e4SLinus Torvalds 	}
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds 	switch (sbsec->behavior) {
7811da177e4SLinus Torvalds 	case SECURITY_FS_USE_XATTR:
7821da177e4SLinus Torvalds 		if (!inode->i_op->getxattr) {
7831da177e4SLinus Torvalds 			isec->sid = sbsec->def_sid;
7841da177e4SLinus Torvalds 			break;
7851da177e4SLinus Torvalds 		}
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds 		/* Need a dentry, since the xattr API requires one.
7881da177e4SLinus Torvalds 		   Life would be simpler if we could just pass the inode. */
7891da177e4SLinus Torvalds 		if (opt_dentry) {
7901da177e4SLinus Torvalds 			/* Called from d_instantiate or d_splice_alias. */
7911da177e4SLinus Torvalds 			dentry = dget(opt_dentry);
7921da177e4SLinus Torvalds 		} else {
7931da177e4SLinus Torvalds 			/* Called from selinux_complete_init, try to find a dentry. */
7941da177e4SLinus Torvalds 			dentry = d_find_alias(inode);
7951da177e4SLinus Torvalds 		}
7961da177e4SLinus Torvalds 		if (!dentry) {
7971da177e4SLinus Torvalds 			printk(KERN_WARNING "%s:  no dentry for dev=%s "
7981da177e4SLinus Torvalds 			       "ino=%ld\n", __FUNCTION__, inode->i_sb->s_id,
7991da177e4SLinus Torvalds 			       inode->i_ino);
8001da177e4SLinus Torvalds 			goto out;
8011da177e4SLinus Torvalds 		}
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds 		len = INITCONTEXTLEN;
8041da177e4SLinus Torvalds 		context = kmalloc(len, GFP_KERNEL);
8051da177e4SLinus Torvalds 		if (!context) {
8061da177e4SLinus Torvalds 			rc = -ENOMEM;
8071da177e4SLinus Torvalds 			dput(dentry);
8081da177e4SLinus Torvalds 			goto out;
8091da177e4SLinus Torvalds 		}
8101da177e4SLinus Torvalds 		rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
8111da177e4SLinus Torvalds 					   context, len);
8121da177e4SLinus Torvalds 		if (rc == -ERANGE) {
8131da177e4SLinus Torvalds 			/* Need a larger buffer.  Query for the right size. */
8141da177e4SLinus Torvalds 			rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
8151da177e4SLinus Torvalds 						   NULL, 0);
8161da177e4SLinus Torvalds 			if (rc < 0) {
8171da177e4SLinus Torvalds 				dput(dentry);
8181da177e4SLinus Torvalds 				goto out;
8191da177e4SLinus Torvalds 			}
8201da177e4SLinus Torvalds 			kfree(context);
8211da177e4SLinus Torvalds 			len = rc;
8221da177e4SLinus Torvalds 			context = kmalloc(len, GFP_KERNEL);
8231da177e4SLinus Torvalds 			if (!context) {
8241da177e4SLinus Torvalds 				rc = -ENOMEM;
8251da177e4SLinus Torvalds 				dput(dentry);
8261da177e4SLinus Torvalds 				goto out;
8271da177e4SLinus Torvalds 			}
8281da177e4SLinus Torvalds 			rc = inode->i_op->getxattr(dentry,
8291da177e4SLinus Torvalds 						   XATTR_NAME_SELINUX,
8301da177e4SLinus Torvalds 						   context, len);
8311da177e4SLinus Torvalds 		}
8321da177e4SLinus Torvalds 		dput(dentry);
8331da177e4SLinus Torvalds 		if (rc < 0) {
8341da177e4SLinus Torvalds 			if (rc != -ENODATA) {
8351da177e4SLinus Torvalds 				printk(KERN_WARNING "%s:  getxattr returned "
8361da177e4SLinus Torvalds 				       "%d for dev=%s ino=%ld\n", __FUNCTION__,
8371da177e4SLinus Torvalds 				       -rc, inode->i_sb->s_id, inode->i_ino);
8381da177e4SLinus Torvalds 				kfree(context);
8391da177e4SLinus Torvalds 				goto out;
8401da177e4SLinus Torvalds 			}
8411da177e4SLinus Torvalds 			/* Map ENODATA to the default file SID */
8421da177e4SLinus Torvalds 			sid = sbsec->def_sid;
8431da177e4SLinus Torvalds 			rc = 0;
8441da177e4SLinus Torvalds 		} else {
845f5c1d5b2SJames Morris 			rc = security_context_to_sid_default(context, rc, &sid,
846f5c1d5b2SJames Morris 			                                     sbsec->def_sid);
8471da177e4SLinus Torvalds 			if (rc) {
8481da177e4SLinus Torvalds 				printk(KERN_WARNING "%s:  context_to_sid(%s) "
8491da177e4SLinus Torvalds 				       "returned %d for dev=%s ino=%ld\n",
8501da177e4SLinus Torvalds 				       __FUNCTION__, context, -rc,
8511da177e4SLinus Torvalds 				       inode->i_sb->s_id, inode->i_ino);
8521da177e4SLinus Torvalds 				kfree(context);
8531da177e4SLinus Torvalds 				/* Leave with the unlabeled SID */
8541da177e4SLinus Torvalds 				rc = 0;
8551da177e4SLinus Torvalds 				break;
8561da177e4SLinus Torvalds 			}
8571da177e4SLinus Torvalds 		}
8581da177e4SLinus Torvalds 		kfree(context);
8591da177e4SLinus Torvalds 		isec->sid = sid;
8601da177e4SLinus Torvalds 		break;
8611da177e4SLinus Torvalds 	case SECURITY_FS_USE_TASK:
8621da177e4SLinus Torvalds 		isec->sid = isec->task_sid;
8631da177e4SLinus Torvalds 		break;
8641da177e4SLinus Torvalds 	case SECURITY_FS_USE_TRANS:
8651da177e4SLinus Torvalds 		/* Default to the fs SID. */
8661da177e4SLinus Torvalds 		isec->sid = sbsec->sid;
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds 		/* Try to obtain a transition SID. */
8691da177e4SLinus Torvalds 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
8701da177e4SLinus Torvalds 		rc = security_transition_sid(isec->task_sid,
8711da177e4SLinus Torvalds 					     sbsec->sid,
8721da177e4SLinus Torvalds 					     isec->sclass,
8731da177e4SLinus Torvalds 					     &sid);
8741da177e4SLinus Torvalds 		if (rc)
8751da177e4SLinus Torvalds 			goto out;
8761da177e4SLinus Torvalds 		isec->sid = sid;
8771da177e4SLinus Torvalds 		break;
8781da177e4SLinus Torvalds 	default:
8791da177e4SLinus Torvalds 		/* Default to the fs SID. */
8801da177e4SLinus Torvalds 		isec->sid = sbsec->sid;
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 		if (sbsec->proc) {
8831da177e4SLinus Torvalds 			struct proc_inode *proci = PROC_I(inode);
8841da177e4SLinus Torvalds 			if (proci->pde) {
8851da177e4SLinus Torvalds 				isec->sclass = inode_mode_to_security_class(inode->i_mode);
8861da177e4SLinus Torvalds 				rc = selinux_proc_get_sid(proci->pde,
8871da177e4SLinus Torvalds 							  isec->sclass,
8881da177e4SLinus Torvalds 							  &sid);
8891da177e4SLinus Torvalds 				if (rc)
8901da177e4SLinus Torvalds 					goto out;
8911da177e4SLinus Torvalds 				isec->sid = sid;
8921da177e4SLinus Torvalds 			}
8931da177e4SLinus Torvalds 		}
8941da177e4SLinus Torvalds 		break;
8951da177e4SLinus Torvalds 	}
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds 	isec->initialized = 1;
8981da177e4SLinus Torvalds 
8991da177e4SLinus Torvalds out:
9001da177e4SLinus Torvalds 	if (isec->sclass == SECCLASS_FILE)
9011da177e4SLinus Torvalds 		isec->sclass = inode_mode_to_security_class(inode->i_mode);
9021da177e4SLinus Torvalds 
9031da177e4SLinus Torvalds 	if (hold_sem)
9041da177e4SLinus Torvalds 		up(&isec->sem);
9051da177e4SLinus Torvalds 	return rc;
9061da177e4SLinus Torvalds }
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds /* Convert a Linux signal to an access vector. */
9091da177e4SLinus Torvalds static inline u32 signal_to_av(int sig)
9101da177e4SLinus Torvalds {
9111da177e4SLinus Torvalds 	u32 perm = 0;
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds 	switch (sig) {
9141da177e4SLinus Torvalds 	case SIGCHLD:
9151da177e4SLinus Torvalds 		/* Commonly granted from child to parent. */
9161da177e4SLinus Torvalds 		perm = PROCESS__SIGCHLD;
9171da177e4SLinus Torvalds 		break;
9181da177e4SLinus Torvalds 	case SIGKILL:
9191da177e4SLinus Torvalds 		/* Cannot be caught or ignored */
9201da177e4SLinus Torvalds 		perm = PROCESS__SIGKILL;
9211da177e4SLinus Torvalds 		break;
9221da177e4SLinus Torvalds 	case SIGSTOP:
9231da177e4SLinus Torvalds 		/* Cannot be caught or ignored */
9241da177e4SLinus Torvalds 		perm = PROCESS__SIGSTOP;
9251da177e4SLinus Torvalds 		break;
9261da177e4SLinus Torvalds 	default:
9271da177e4SLinus Torvalds 		/* All other signals. */
9281da177e4SLinus Torvalds 		perm = PROCESS__SIGNAL;
9291da177e4SLinus Torvalds 		break;
9301da177e4SLinus Torvalds 	}
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds 	return perm;
9331da177e4SLinus Torvalds }
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds /* Check permission betweeen a pair of tasks, e.g. signal checks,
9361da177e4SLinus Torvalds    fork check, ptrace check, etc. */
9371da177e4SLinus Torvalds static int task_has_perm(struct task_struct *tsk1,
9381da177e4SLinus Torvalds 			 struct task_struct *tsk2,
9391da177e4SLinus Torvalds 			 u32 perms)
9401da177e4SLinus Torvalds {
9411da177e4SLinus Torvalds 	struct task_security_struct *tsec1, *tsec2;
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds 	tsec1 = tsk1->security;
9441da177e4SLinus Torvalds 	tsec2 = tsk2->security;
9451da177e4SLinus Torvalds 	return avc_has_perm(tsec1->sid, tsec2->sid,
9461da177e4SLinus Torvalds 			    SECCLASS_PROCESS, perms, NULL);
9471da177e4SLinus Torvalds }
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds /* Check whether a task is allowed to use a capability. */
9501da177e4SLinus Torvalds static int task_has_capability(struct task_struct *tsk,
9511da177e4SLinus Torvalds 			       int cap)
9521da177e4SLinus Torvalds {
9531da177e4SLinus Torvalds 	struct task_security_struct *tsec;
9541da177e4SLinus Torvalds 	struct avc_audit_data ad;
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds 	tsec = tsk->security;
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad,CAP);
9591da177e4SLinus Torvalds 	ad.tsk = tsk;
9601da177e4SLinus Torvalds 	ad.u.cap = cap;
9611da177e4SLinus Torvalds 
9621da177e4SLinus Torvalds 	return avc_has_perm(tsec->sid, tsec->sid,
9631da177e4SLinus Torvalds 			    SECCLASS_CAPABILITY, CAP_TO_MASK(cap), &ad);
9641da177e4SLinus Torvalds }
9651da177e4SLinus Torvalds 
9661da177e4SLinus Torvalds /* Check whether a task is allowed to use a system operation. */
9671da177e4SLinus Torvalds static int task_has_system(struct task_struct *tsk,
9681da177e4SLinus Torvalds 			   u32 perms)
9691da177e4SLinus Torvalds {
9701da177e4SLinus Torvalds 	struct task_security_struct *tsec;
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds 	tsec = tsk->security;
9731da177e4SLinus Torvalds 
9741da177e4SLinus Torvalds 	return avc_has_perm(tsec->sid, SECINITSID_KERNEL,
9751da177e4SLinus Torvalds 			    SECCLASS_SYSTEM, perms, NULL);
9761da177e4SLinus Torvalds }
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds /* Check whether a task has a particular permission to an inode.
9791da177e4SLinus Torvalds    The 'adp' parameter is optional and allows other audit
9801da177e4SLinus Torvalds    data to be passed (e.g. the dentry). */
9811da177e4SLinus Torvalds static int inode_has_perm(struct task_struct *tsk,
9821da177e4SLinus Torvalds 			  struct inode *inode,
9831da177e4SLinus Torvalds 			  u32 perms,
9841da177e4SLinus Torvalds 			  struct avc_audit_data *adp)
9851da177e4SLinus Torvalds {
9861da177e4SLinus Torvalds 	struct task_security_struct *tsec;
9871da177e4SLinus Torvalds 	struct inode_security_struct *isec;
9881da177e4SLinus Torvalds 	struct avc_audit_data ad;
9891da177e4SLinus Torvalds 
9901da177e4SLinus Torvalds 	tsec = tsk->security;
9911da177e4SLinus Torvalds 	isec = inode->i_security;
9921da177e4SLinus Torvalds 
9931da177e4SLinus Torvalds 	if (!adp) {
9941da177e4SLinus Torvalds 		adp = &ad;
9951da177e4SLinus Torvalds 		AVC_AUDIT_DATA_INIT(&ad, FS);
9961da177e4SLinus Torvalds 		ad.u.fs.inode = inode;
9971da177e4SLinus Torvalds 	}
9981da177e4SLinus Torvalds 
9991da177e4SLinus Torvalds 	return avc_has_perm(tsec->sid, isec->sid, isec->sclass, perms, adp);
10001da177e4SLinus Torvalds }
10011da177e4SLinus Torvalds 
10021da177e4SLinus Torvalds /* Same as inode_has_perm, but pass explicit audit data containing
10031da177e4SLinus Torvalds    the dentry to help the auditing code to more easily generate the
10041da177e4SLinus Torvalds    pathname if needed. */
10051da177e4SLinus Torvalds static inline int dentry_has_perm(struct task_struct *tsk,
10061da177e4SLinus Torvalds 				  struct vfsmount *mnt,
10071da177e4SLinus Torvalds 				  struct dentry *dentry,
10081da177e4SLinus Torvalds 				  u32 av)
10091da177e4SLinus Torvalds {
10101da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
10111da177e4SLinus Torvalds 	struct avc_audit_data ad;
10121da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad,FS);
10131da177e4SLinus Torvalds 	ad.u.fs.mnt = mnt;
10141da177e4SLinus Torvalds 	ad.u.fs.dentry = dentry;
10151da177e4SLinus Torvalds 	return inode_has_perm(tsk, inode, av, &ad);
10161da177e4SLinus Torvalds }
10171da177e4SLinus Torvalds 
10181da177e4SLinus Torvalds /* Check whether a task can use an open file descriptor to
10191da177e4SLinus Torvalds    access an inode in a given way.  Check access to the
10201da177e4SLinus Torvalds    descriptor itself, and then use dentry_has_perm to
10211da177e4SLinus Torvalds    check a particular permission to the file.
10221da177e4SLinus Torvalds    Access to the descriptor is implicitly granted if it
10231da177e4SLinus Torvalds    has the same SID as the process.  If av is zero, then
10241da177e4SLinus Torvalds    access to the file is not checked, e.g. for cases
10251da177e4SLinus Torvalds    where only the descriptor is affected like seek. */
1026858119e1SArjan van de Ven static int file_has_perm(struct task_struct *tsk,
10271da177e4SLinus Torvalds 				struct file *file,
10281da177e4SLinus Torvalds 				u32 av)
10291da177e4SLinus Torvalds {
10301da177e4SLinus Torvalds 	struct task_security_struct *tsec = tsk->security;
10311da177e4SLinus Torvalds 	struct file_security_struct *fsec = file->f_security;
10321da177e4SLinus Torvalds 	struct vfsmount *mnt = file->f_vfsmnt;
10331da177e4SLinus Torvalds 	struct dentry *dentry = file->f_dentry;
10341da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
10351da177e4SLinus Torvalds 	struct avc_audit_data ad;
10361da177e4SLinus Torvalds 	int rc;
10371da177e4SLinus Torvalds 
10381da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, FS);
10391da177e4SLinus Torvalds 	ad.u.fs.mnt = mnt;
10401da177e4SLinus Torvalds 	ad.u.fs.dentry = dentry;
10411da177e4SLinus Torvalds 
10421da177e4SLinus Torvalds 	if (tsec->sid != fsec->sid) {
10431da177e4SLinus Torvalds 		rc = avc_has_perm(tsec->sid, fsec->sid,
10441da177e4SLinus Torvalds 				  SECCLASS_FD,
10451da177e4SLinus Torvalds 				  FD__USE,
10461da177e4SLinus Torvalds 				  &ad);
10471da177e4SLinus Torvalds 		if (rc)
10481da177e4SLinus Torvalds 			return rc;
10491da177e4SLinus Torvalds 	}
10501da177e4SLinus Torvalds 
10511da177e4SLinus Torvalds 	/* av is zero if only checking access to the descriptor. */
10521da177e4SLinus Torvalds 	if (av)
10531da177e4SLinus Torvalds 		return inode_has_perm(tsk, inode, av, &ad);
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds 	return 0;
10561da177e4SLinus Torvalds }
10571da177e4SLinus Torvalds 
10581da177e4SLinus Torvalds /* Check whether a task can create a file. */
10591da177e4SLinus Torvalds static int may_create(struct inode *dir,
10601da177e4SLinus Torvalds 		      struct dentry *dentry,
10611da177e4SLinus Torvalds 		      u16 tclass)
10621da177e4SLinus Torvalds {
10631da177e4SLinus Torvalds 	struct task_security_struct *tsec;
10641da177e4SLinus Torvalds 	struct inode_security_struct *dsec;
10651da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
10661da177e4SLinus Torvalds 	u32 newsid;
10671da177e4SLinus Torvalds 	struct avc_audit_data ad;
10681da177e4SLinus Torvalds 	int rc;
10691da177e4SLinus Torvalds 
10701da177e4SLinus Torvalds 	tsec = current->security;
10711da177e4SLinus Torvalds 	dsec = dir->i_security;
10721da177e4SLinus Torvalds 	sbsec = dir->i_sb->s_security;
10731da177e4SLinus Torvalds 
10741da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, FS);
10751da177e4SLinus Torvalds 	ad.u.fs.dentry = dentry;
10761da177e4SLinus Torvalds 
10771da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, dsec->sid, SECCLASS_DIR,
10781da177e4SLinus Torvalds 			  DIR__ADD_NAME | DIR__SEARCH,
10791da177e4SLinus Torvalds 			  &ad);
10801da177e4SLinus Torvalds 	if (rc)
10811da177e4SLinus Torvalds 		return rc;
10821da177e4SLinus Torvalds 
10831da177e4SLinus Torvalds 	if (tsec->create_sid && sbsec->behavior != SECURITY_FS_USE_MNTPOINT) {
10841da177e4SLinus Torvalds 		newsid = tsec->create_sid;
10851da177e4SLinus Torvalds 	} else {
10861da177e4SLinus Torvalds 		rc = security_transition_sid(tsec->sid, dsec->sid, tclass,
10871da177e4SLinus Torvalds 					     &newsid);
10881da177e4SLinus Torvalds 		if (rc)
10891da177e4SLinus Torvalds 			return rc;
10901da177e4SLinus Torvalds 	}
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, newsid, tclass, FILE__CREATE, &ad);
10931da177e4SLinus Torvalds 	if (rc)
10941da177e4SLinus Torvalds 		return rc;
10951da177e4SLinus Torvalds 
10961da177e4SLinus Torvalds 	return avc_has_perm(newsid, sbsec->sid,
10971da177e4SLinus Torvalds 			    SECCLASS_FILESYSTEM,
10981da177e4SLinus Torvalds 			    FILESYSTEM__ASSOCIATE, &ad);
10991da177e4SLinus Torvalds }
11001da177e4SLinus Torvalds 
11011da177e4SLinus Torvalds #define MAY_LINK   0
11021da177e4SLinus Torvalds #define MAY_UNLINK 1
11031da177e4SLinus Torvalds #define MAY_RMDIR  2
11041da177e4SLinus Torvalds 
11051da177e4SLinus Torvalds /* Check whether a task can link, unlink, or rmdir a file/directory. */
11061da177e4SLinus Torvalds static int may_link(struct inode *dir,
11071da177e4SLinus Torvalds 		    struct dentry *dentry,
11081da177e4SLinus Torvalds 		    int kind)
11091da177e4SLinus Torvalds 
11101da177e4SLinus Torvalds {
11111da177e4SLinus Torvalds 	struct task_security_struct *tsec;
11121da177e4SLinus Torvalds 	struct inode_security_struct *dsec, *isec;
11131da177e4SLinus Torvalds 	struct avc_audit_data ad;
11141da177e4SLinus Torvalds 	u32 av;
11151da177e4SLinus Torvalds 	int rc;
11161da177e4SLinus Torvalds 
11171da177e4SLinus Torvalds 	tsec = current->security;
11181da177e4SLinus Torvalds 	dsec = dir->i_security;
11191da177e4SLinus Torvalds 	isec = dentry->d_inode->i_security;
11201da177e4SLinus Torvalds 
11211da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, FS);
11221da177e4SLinus Torvalds 	ad.u.fs.dentry = dentry;
11231da177e4SLinus Torvalds 
11241da177e4SLinus Torvalds 	av = DIR__SEARCH;
11251da177e4SLinus Torvalds 	av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
11261da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, dsec->sid, SECCLASS_DIR, av, &ad);
11271da177e4SLinus Torvalds 	if (rc)
11281da177e4SLinus Torvalds 		return rc;
11291da177e4SLinus Torvalds 
11301da177e4SLinus Torvalds 	switch (kind) {
11311da177e4SLinus Torvalds 	case MAY_LINK:
11321da177e4SLinus Torvalds 		av = FILE__LINK;
11331da177e4SLinus Torvalds 		break;
11341da177e4SLinus Torvalds 	case MAY_UNLINK:
11351da177e4SLinus Torvalds 		av = FILE__UNLINK;
11361da177e4SLinus Torvalds 		break;
11371da177e4SLinus Torvalds 	case MAY_RMDIR:
11381da177e4SLinus Torvalds 		av = DIR__RMDIR;
11391da177e4SLinus Torvalds 		break;
11401da177e4SLinus Torvalds 	default:
11411da177e4SLinus Torvalds 		printk(KERN_WARNING "may_link:  unrecognized kind %d\n", kind);
11421da177e4SLinus Torvalds 		return 0;
11431da177e4SLinus Torvalds 	}
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, isec->sid, isec->sclass, av, &ad);
11461da177e4SLinus Torvalds 	return rc;
11471da177e4SLinus Torvalds }
11481da177e4SLinus Torvalds 
11491da177e4SLinus Torvalds static inline int may_rename(struct inode *old_dir,
11501da177e4SLinus Torvalds 			     struct dentry *old_dentry,
11511da177e4SLinus Torvalds 			     struct inode *new_dir,
11521da177e4SLinus Torvalds 			     struct dentry *new_dentry)
11531da177e4SLinus Torvalds {
11541da177e4SLinus Torvalds 	struct task_security_struct *tsec;
11551da177e4SLinus Torvalds 	struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
11561da177e4SLinus Torvalds 	struct avc_audit_data ad;
11571da177e4SLinus Torvalds 	u32 av;
11581da177e4SLinus Torvalds 	int old_is_dir, new_is_dir;
11591da177e4SLinus Torvalds 	int rc;
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds 	tsec = current->security;
11621da177e4SLinus Torvalds 	old_dsec = old_dir->i_security;
11631da177e4SLinus Torvalds 	old_isec = old_dentry->d_inode->i_security;
11641da177e4SLinus Torvalds 	old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
11651da177e4SLinus Torvalds 	new_dsec = new_dir->i_security;
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, FS);
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds 	ad.u.fs.dentry = old_dentry;
11701da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, old_dsec->sid, SECCLASS_DIR,
11711da177e4SLinus Torvalds 			  DIR__REMOVE_NAME | DIR__SEARCH, &ad);
11721da177e4SLinus Torvalds 	if (rc)
11731da177e4SLinus Torvalds 		return rc;
11741da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, old_isec->sid,
11751da177e4SLinus Torvalds 			  old_isec->sclass, FILE__RENAME, &ad);
11761da177e4SLinus Torvalds 	if (rc)
11771da177e4SLinus Torvalds 		return rc;
11781da177e4SLinus Torvalds 	if (old_is_dir && new_dir != old_dir) {
11791da177e4SLinus Torvalds 		rc = avc_has_perm(tsec->sid, old_isec->sid,
11801da177e4SLinus Torvalds 				  old_isec->sclass, DIR__REPARENT, &ad);
11811da177e4SLinus Torvalds 		if (rc)
11821da177e4SLinus Torvalds 			return rc;
11831da177e4SLinus Torvalds 	}
11841da177e4SLinus Torvalds 
11851da177e4SLinus Torvalds 	ad.u.fs.dentry = new_dentry;
11861da177e4SLinus Torvalds 	av = DIR__ADD_NAME | DIR__SEARCH;
11871da177e4SLinus Torvalds 	if (new_dentry->d_inode)
11881da177e4SLinus Torvalds 		av |= DIR__REMOVE_NAME;
11891da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
11901da177e4SLinus Torvalds 	if (rc)
11911da177e4SLinus Torvalds 		return rc;
11921da177e4SLinus Torvalds 	if (new_dentry->d_inode) {
11931da177e4SLinus Torvalds 		new_isec = new_dentry->d_inode->i_security;
11941da177e4SLinus Torvalds 		new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode);
11951da177e4SLinus Torvalds 		rc = avc_has_perm(tsec->sid, new_isec->sid,
11961da177e4SLinus Torvalds 				  new_isec->sclass,
11971da177e4SLinus Torvalds 				  (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
11981da177e4SLinus Torvalds 		if (rc)
11991da177e4SLinus Torvalds 			return rc;
12001da177e4SLinus Torvalds 	}
12011da177e4SLinus Torvalds 
12021da177e4SLinus Torvalds 	return 0;
12031da177e4SLinus Torvalds }
12041da177e4SLinus Torvalds 
12051da177e4SLinus Torvalds /* Check whether a task can perform a filesystem operation. */
12061da177e4SLinus Torvalds static int superblock_has_perm(struct task_struct *tsk,
12071da177e4SLinus Torvalds 			       struct super_block *sb,
12081da177e4SLinus Torvalds 			       u32 perms,
12091da177e4SLinus Torvalds 			       struct avc_audit_data *ad)
12101da177e4SLinus Torvalds {
12111da177e4SLinus Torvalds 	struct task_security_struct *tsec;
12121da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
12131da177e4SLinus Torvalds 
12141da177e4SLinus Torvalds 	tsec = tsk->security;
12151da177e4SLinus Torvalds 	sbsec = sb->s_security;
12161da177e4SLinus Torvalds 	return avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
12171da177e4SLinus Torvalds 			    perms, ad);
12181da177e4SLinus Torvalds }
12191da177e4SLinus Torvalds 
12201da177e4SLinus Torvalds /* Convert a Linux mode and permission mask to an access vector. */
12211da177e4SLinus Torvalds static inline u32 file_mask_to_av(int mode, int mask)
12221da177e4SLinus Torvalds {
12231da177e4SLinus Torvalds 	u32 av = 0;
12241da177e4SLinus Torvalds 
12251da177e4SLinus Torvalds 	if ((mode & S_IFMT) != S_IFDIR) {
12261da177e4SLinus Torvalds 		if (mask & MAY_EXEC)
12271da177e4SLinus Torvalds 			av |= FILE__EXECUTE;
12281da177e4SLinus Torvalds 		if (mask & MAY_READ)
12291da177e4SLinus Torvalds 			av |= FILE__READ;
12301da177e4SLinus Torvalds 
12311da177e4SLinus Torvalds 		if (mask & MAY_APPEND)
12321da177e4SLinus Torvalds 			av |= FILE__APPEND;
12331da177e4SLinus Torvalds 		else if (mask & MAY_WRITE)
12341da177e4SLinus Torvalds 			av |= FILE__WRITE;
12351da177e4SLinus Torvalds 
12361da177e4SLinus Torvalds 	} else {
12371da177e4SLinus Torvalds 		if (mask & MAY_EXEC)
12381da177e4SLinus Torvalds 			av |= DIR__SEARCH;
12391da177e4SLinus Torvalds 		if (mask & MAY_WRITE)
12401da177e4SLinus Torvalds 			av |= DIR__WRITE;
12411da177e4SLinus Torvalds 		if (mask & MAY_READ)
12421da177e4SLinus Torvalds 			av |= DIR__READ;
12431da177e4SLinus Torvalds 	}
12441da177e4SLinus Torvalds 
12451da177e4SLinus Torvalds 	return av;
12461da177e4SLinus Torvalds }
12471da177e4SLinus Torvalds 
12481da177e4SLinus Torvalds /* Convert a Linux file to an access vector. */
12491da177e4SLinus Torvalds static inline u32 file_to_av(struct file *file)
12501da177e4SLinus Torvalds {
12511da177e4SLinus Torvalds 	u32 av = 0;
12521da177e4SLinus Torvalds 
12531da177e4SLinus Torvalds 	if (file->f_mode & FMODE_READ)
12541da177e4SLinus Torvalds 		av |= FILE__READ;
12551da177e4SLinus Torvalds 	if (file->f_mode & FMODE_WRITE) {
12561da177e4SLinus Torvalds 		if (file->f_flags & O_APPEND)
12571da177e4SLinus Torvalds 			av |= FILE__APPEND;
12581da177e4SLinus Torvalds 		else
12591da177e4SLinus Torvalds 			av |= FILE__WRITE;
12601da177e4SLinus Torvalds 	}
12611da177e4SLinus Torvalds 
12621da177e4SLinus Torvalds 	return av;
12631da177e4SLinus Torvalds }
12641da177e4SLinus Torvalds 
12651da177e4SLinus Torvalds /* Set an inode's SID to a specified value. */
12661da177e4SLinus Torvalds static int inode_security_set_sid(struct inode *inode, u32 sid)
12671da177e4SLinus Torvalds {
12681da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
12691da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec = inode->i_sb->s_security;
12701da177e4SLinus Torvalds 
12711da177e4SLinus Torvalds 	if (!sbsec->initialized) {
12721da177e4SLinus Torvalds 		/* Defer initialization to selinux_complete_init. */
12731da177e4SLinus Torvalds 		return 0;
12741da177e4SLinus Torvalds 	}
12751da177e4SLinus Torvalds 
12761da177e4SLinus Torvalds 	down(&isec->sem);
12771da177e4SLinus Torvalds 	isec->sclass = inode_mode_to_security_class(inode->i_mode);
12781da177e4SLinus Torvalds 	isec->sid = sid;
12791da177e4SLinus Torvalds 	isec->initialized = 1;
12801da177e4SLinus Torvalds 	up(&isec->sem);
12811da177e4SLinus Torvalds 	return 0;
12821da177e4SLinus Torvalds }
12831da177e4SLinus Torvalds 
12841da177e4SLinus Torvalds /* Hook functions begin here. */
12851da177e4SLinus Torvalds 
12861da177e4SLinus Torvalds static int selinux_ptrace(struct task_struct *parent, struct task_struct *child)
12871da177e4SLinus Torvalds {
12881da177e4SLinus Torvalds 	struct task_security_struct *psec = parent->security;
12891da177e4SLinus Torvalds 	struct task_security_struct *csec = child->security;
12901da177e4SLinus Torvalds 	int rc;
12911da177e4SLinus Torvalds 
12921da177e4SLinus Torvalds 	rc = secondary_ops->ptrace(parent,child);
12931da177e4SLinus Torvalds 	if (rc)
12941da177e4SLinus Torvalds 		return rc;
12951da177e4SLinus Torvalds 
12961da177e4SLinus Torvalds 	rc = task_has_perm(parent, child, PROCESS__PTRACE);
12971da177e4SLinus Torvalds 	/* Save the SID of the tracing process for later use in apply_creds. */
1298341c2d80SStephen Smalley 	if (!(child->ptrace & PT_PTRACED) && !rc)
12991da177e4SLinus Torvalds 		csec->ptrace_sid = psec->sid;
13001da177e4SLinus Torvalds 	return rc;
13011da177e4SLinus Torvalds }
13021da177e4SLinus Torvalds 
13031da177e4SLinus Torvalds static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
13041da177e4SLinus Torvalds                           kernel_cap_t *inheritable, kernel_cap_t *permitted)
13051da177e4SLinus Torvalds {
13061da177e4SLinus Torvalds 	int error;
13071da177e4SLinus Torvalds 
13081da177e4SLinus Torvalds 	error = task_has_perm(current, target, PROCESS__GETCAP);
13091da177e4SLinus Torvalds 	if (error)
13101da177e4SLinus Torvalds 		return error;
13111da177e4SLinus Torvalds 
13121da177e4SLinus Torvalds 	return secondary_ops->capget(target, effective, inheritable, permitted);
13131da177e4SLinus Torvalds }
13141da177e4SLinus Torvalds 
13151da177e4SLinus Torvalds static int selinux_capset_check(struct task_struct *target, kernel_cap_t *effective,
13161da177e4SLinus Torvalds                                 kernel_cap_t *inheritable, kernel_cap_t *permitted)
13171da177e4SLinus Torvalds {
13181da177e4SLinus Torvalds 	int error;
13191da177e4SLinus Torvalds 
13201da177e4SLinus Torvalds 	error = secondary_ops->capset_check(target, effective, inheritable, permitted);
13211da177e4SLinus Torvalds 	if (error)
13221da177e4SLinus Torvalds 		return error;
13231da177e4SLinus Torvalds 
13241da177e4SLinus Torvalds 	return task_has_perm(current, target, PROCESS__SETCAP);
13251da177e4SLinus Torvalds }
13261da177e4SLinus Torvalds 
13271da177e4SLinus Torvalds static void selinux_capset_set(struct task_struct *target, kernel_cap_t *effective,
13281da177e4SLinus Torvalds                                kernel_cap_t *inheritable, kernel_cap_t *permitted)
13291da177e4SLinus Torvalds {
13301da177e4SLinus Torvalds 	secondary_ops->capset_set(target, effective, inheritable, permitted);
13311da177e4SLinus Torvalds }
13321da177e4SLinus Torvalds 
13331da177e4SLinus Torvalds static int selinux_capable(struct task_struct *tsk, int cap)
13341da177e4SLinus Torvalds {
13351da177e4SLinus Torvalds 	int rc;
13361da177e4SLinus Torvalds 
13371da177e4SLinus Torvalds 	rc = secondary_ops->capable(tsk, cap);
13381da177e4SLinus Torvalds 	if (rc)
13391da177e4SLinus Torvalds 		return rc;
13401da177e4SLinus Torvalds 
13411da177e4SLinus Torvalds 	return task_has_capability(tsk,cap);
13421da177e4SLinus Torvalds }
13431da177e4SLinus Torvalds 
13441da177e4SLinus Torvalds static int selinux_sysctl(ctl_table *table, int op)
13451da177e4SLinus Torvalds {
13461da177e4SLinus Torvalds 	int error = 0;
13471da177e4SLinus Torvalds 	u32 av;
13481da177e4SLinus Torvalds 	struct task_security_struct *tsec;
13491da177e4SLinus Torvalds 	u32 tsid;
13501da177e4SLinus Torvalds 	int rc;
13511da177e4SLinus Torvalds 
13521da177e4SLinus Torvalds 	rc = secondary_ops->sysctl(table, op);
13531da177e4SLinus Torvalds 	if (rc)
13541da177e4SLinus Torvalds 		return rc;
13551da177e4SLinus Torvalds 
13561da177e4SLinus Torvalds 	tsec = current->security;
13571da177e4SLinus Torvalds 
13581da177e4SLinus Torvalds 	rc = selinux_proc_get_sid(table->de, (op == 001) ?
13591da177e4SLinus Torvalds 	                          SECCLASS_DIR : SECCLASS_FILE, &tsid);
13601da177e4SLinus Torvalds 	if (rc) {
13611da177e4SLinus Torvalds 		/* Default to the well-defined sysctl SID. */
13621da177e4SLinus Torvalds 		tsid = SECINITSID_SYSCTL;
13631da177e4SLinus Torvalds 	}
13641da177e4SLinus Torvalds 
13651da177e4SLinus Torvalds 	/* The op values are "defined" in sysctl.c, thereby creating
13661da177e4SLinus Torvalds 	 * a bad coupling between this module and sysctl.c */
13671da177e4SLinus Torvalds 	if(op == 001) {
13681da177e4SLinus Torvalds 		error = avc_has_perm(tsec->sid, tsid,
13691da177e4SLinus Torvalds 				     SECCLASS_DIR, DIR__SEARCH, NULL);
13701da177e4SLinus Torvalds 	} else {
13711da177e4SLinus Torvalds 		av = 0;
13721da177e4SLinus Torvalds 		if (op & 004)
13731da177e4SLinus Torvalds 			av |= FILE__READ;
13741da177e4SLinus Torvalds 		if (op & 002)
13751da177e4SLinus Torvalds 			av |= FILE__WRITE;
13761da177e4SLinus Torvalds 		if (av)
13771da177e4SLinus Torvalds 			error = avc_has_perm(tsec->sid, tsid,
13781da177e4SLinus Torvalds 					     SECCLASS_FILE, av, NULL);
13791da177e4SLinus Torvalds         }
13801da177e4SLinus Torvalds 
13811da177e4SLinus Torvalds 	return error;
13821da177e4SLinus Torvalds }
13831da177e4SLinus Torvalds 
13841da177e4SLinus Torvalds static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
13851da177e4SLinus Torvalds {
13861da177e4SLinus Torvalds 	int rc = 0;
13871da177e4SLinus Torvalds 
13881da177e4SLinus Torvalds 	if (!sb)
13891da177e4SLinus Torvalds 		return 0;
13901da177e4SLinus Torvalds 
13911da177e4SLinus Torvalds 	switch (cmds) {
13921da177e4SLinus Torvalds 		case Q_SYNC:
13931da177e4SLinus Torvalds 		case Q_QUOTAON:
13941da177e4SLinus Torvalds 		case Q_QUOTAOFF:
13951da177e4SLinus Torvalds 	        case Q_SETINFO:
13961da177e4SLinus Torvalds 		case Q_SETQUOTA:
13971da177e4SLinus Torvalds 			rc = superblock_has_perm(current,
13981da177e4SLinus Torvalds 						 sb,
13991da177e4SLinus Torvalds 						 FILESYSTEM__QUOTAMOD, NULL);
14001da177e4SLinus Torvalds 			break;
14011da177e4SLinus Torvalds 	        case Q_GETFMT:
14021da177e4SLinus Torvalds 	        case Q_GETINFO:
14031da177e4SLinus Torvalds 		case Q_GETQUOTA:
14041da177e4SLinus Torvalds 			rc = superblock_has_perm(current,
14051da177e4SLinus Torvalds 						 sb,
14061da177e4SLinus Torvalds 						 FILESYSTEM__QUOTAGET, NULL);
14071da177e4SLinus Torvalds 			break;
14081da177e4SLinus Torvalds 		default:
14091da177e4SLinus Torvalds 			rc = 0;  /* let the kernel handle invalid cmds */
14101da177e4SLinus Torvalds 			break;
14111da177e4SLinus Torvalds 	}
14121da177e4SLinus Torvalds 	return rc;
14131da177e4SLinus Torvalds }
14141da177e4SLinus Torvalds 
14151da177e4SLinus Torvalds static int selinux_quota_on(struct dentry *dentry)
14161da177e4SLinus Torvalds {
14171da177e4SLinus Torvalds 	return dentry_has_perm(current, NULL, dentry, FILE__QUOTAON);
14181da177e4SLinus Torvalds }
14191da177e4SLinus Torvalds 
14201da177e4SLinus Torvalds static int selinux_syslog(int type)
14211da177e4SLinus Torvalds {
14221da177e4SLinus Torvalds 	int rc;
14231da177e4SLinus Torvalds 
14241da177e4SLinus Torvalds 	rc = secondary_ops->syslog(type);
14251da177e4SLinus Torvalds 	if (rc)
14261da177e4SLinus Torvalds 		return rc;
14271da177e4SLinus Torvalds 
14281da177e4SLinus Torvalds 	switch (type) {
14291da177e4SLinus Torvalds 		case 3:         /* Read last kernel messages */
14301da177e4SLinus Torvalds 		case 10:        /* Return size of the log buffer */
14311da177e4SLinus Torvalds 			rc = task_has_system(current, SYSTEM__SYSLOG_READ);
14321da177e4SLinus Torvalds 			break;
14331da177e4SLinus Torvalds 		case 6:         /* Disable logging to console */
14341da177e4SLinus Torvalds 		case 7:         /* Enable logging to console */
14351da177e4SLinus Torvalds 		case 8:		/* Set level of messages printed to console */
14361da177e4SLinus Torvalds 			rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE);
14371da177e4SLinus Torvalds 			break;
14381da177e4SLinus Torvalds 		case 0:         /* Close log */
14391da177e4SLinus Torvalds 		case 1:         /* Open log */
14401da177e4SLinus Torvalds 		case 2:         /* Read from log */
14411da177e4SLinus Torvalds 		case 4:         /* Read/clear last kernel messages */
14421da177e4SLinus Torvalds 		case 5:         /* Clear ring buffer */
14431da177e4SLinus Torvalds 		default:
14441da177e4SLinus Torvalds 			rc = task_has_system(current, SYSTEM__SYSLOG_MOD);
14451da177e4SLinus Torvalds 			break;
14461da177e4SLinus Torvalds 	}
14471da177e4SLinus Torvalds 	return rc;
14481da177e4SLinus Torvalds }
14491da177e4SLinus Torvalds 
14501da177e4SLinus Torvalds /*
14511da177e4SLinus Torvalds  * Check that a process has enough memory to allocate a new virtual
14521da177e4SLinus Torvalds  * mapping. 0 means there is enough memory for the allocation to
14531da177e4SLinus Torvalds  * succeed and -ENOMEM implies there is not.
14541da177e4SLinus Torvalds  *
14551da177e4SLinus Torvalds  * Note that secondary_ops->capable and task_has_perm_noaudit return 0
14561da177e4SLinus Torvalds  * if the capability is granted, but __vm_enough_memory requires 1 if
14571da177e4SLinus Torvalds  * the capability is granted.
14581da177e4SLinus Torvalds  *
14591da177e4SLinus Torvalds  * Do not audit the selinux permission check, as this is applied to all
14601da177e4SLinus Torvalds  * processes that allocate mappings.
14611da177e4SLinus Torvalds  */
14621da177e4SLinus Torvalds static int selinux_vm_enough_memory(long pages)
14631da177e4SLinus Torvalds {
14641da177e4SLinus Torvalds 	int rc, cap_sys_admin = 0;
14651da177e4SLinus Torvalds 	struct task_security_struct *tsec = current->security;
14661da177e4SLinus Torvalds 
14671da177e4SLinus Torvalds 	rc = secondary_ops->capable(current, CAP_SYS_ADMIN);
14681da177e4SLinus Torvalds 	if (rc == 0)
14691da177e4SLinus Torvalds 		rc = avc_has_perm_noaudit(tsec->sid, tsec->sid,
14701da177e4SLinus Torvalds 					SECCLASS_CAPABILITY,
14711da177e4SLinus Torvalds 					CAP_TO_MASK(CAP_SYS_ADMIN),
14721da177e4SLinus Torvalds 					NULL);
14731da177e4SLinus Torvalds 
14741da177e4SLinus Torvalds 	if (rc == 0)
14751da177e4SLinus Torvalds 		cap_sys_admin = 1;
14761da177e4SLinus Torvalds 
14771da177e4SLinus Torvalds 	return __vm_enough_memory(pages, cap_sys_admin);
14781da177e4SLinus Torvalds }
14791da177e4SLinus Torvalds 
14801da177e4SLinus Torvalds /* binprm security operations */
14811da177e4SLinus Torvalds 
14821da177e4SLinus Torvalds static int selinux_bprm_alloc_security(struct linux_binprm *bprm)
14831da177e4SLinus Torvalds {
14841da177e4SLinus Torvalds 	struct bprm_security_struct *bsec;
14851da177e4SLinus Torvalds 
148689d155efSJames Morris 	bsec = kzalloc(sizeof(struct bprm_security_struct), GFP_KERNEL);
14871da177e4SLinus Torvalds 	if (!bsec)
14881da177e4SLinus Torvalds 		return -ENOMEM;
14891da177e4SLinus Torvalds 
14901da177e4SLinus Torvalds 	bsec->bprm = bprm;
14911da177e4SLinus Torvalds 	bsec->sid = SECINITSID_UNLABELED;
14921da177e4SLinus Torvalds 	bsec->set = 0;
14931da177e4SLinus Torvalds 
14941da177e4SLinus Torvalds 	bprm->security = bsec;
14951da177e4SLinus Torvalds 	return 0;
14961da177e4SLinus Torvalds }
14971da177e4SLinus Torvalds 
14981da177e4SLinus Torvalds static int selinux_bprm_set_security(struct linux_binprm *bprm)
14991da177e4SLinus Torvalds {
15001da177e4SLinus Torvalds 	struct task_security_struct *tsec;
15011da177e4SLinus Torvalds 	struct inode *inode = bprm->file->f_dentry->d_inode;
15021da177e4SLinus Torvalds 	struct inode_security_struct *isec;
15031da177e4SLinus Torvalds 	struct bprm_security_struct *bsec;
15041da177e4SLinus Torvalds 	u32 newsid;
15051da177e4SLinus Torvalds 	struct avc_audit_data ad;
15061da177e4SLinus Torvalds 	int rc;
15071da177e4SLinus Torvalds 
15081da177e4SLinus Torvalds 	rc = secondary_ops->bprm_set_security(bprm);
15091da177e4SLinus Torvalds 	if (rc)
15101da177e4SLinus Torvalds 		return rc;
15111da177e4SLinus Torvalds 
15121da177e4SLinus Torvalds 	bsec = bprm->security;
15131da177e4SLinus Torvalds 
15141da177e4SLinus Torvalds 	if (bsec->set)
15151da177e4SLinus Torvalds 		return 0;
15161da177e4SLinus Torvalds 
15171da177e4SLinus Torvalds 	tsec = current->security;
15181da177e4SLinus Torvalds 	isec = inode->i_security;
15191da177e4SLinus Torvalds 
15201da177e4SLinus Torvalds 	/* Default to the current task SID. */
15211da177e4SLinus Torvalds 	bsec->sid = tsec->sid;
15221da177e4SLinus Torvalds 
15231da177e4SLinus Torvalds 	/* Reset create SID on execve. */
15241da177e4SLinus Torvalds 	tsec->create_sid = 0;
15251da177e4SLinus Torvalds 
15261da177e4SLinus Torvalds 	if (tsec->exec_sid) {
15271da177e4SLinus Torvalds 		newsid = tsec->exec_sid;
15281da177e4SLinus Torvalds 		/* Reset exec SID on execve. */
15291da177e4SLinus Torvalds 		tsec->exec_sid = 0;
15301da177e4SLinus Torvalds 	} else {
15311da177e4SLinus Torvalds 		/* Check for a default transition on this program. */
15321da177e4SLinus Torvalds 		rc = security_transition_sid(tsec->sid, isec->sid,
15331da177e4SLinus Torvalds 		                             SECCLASS_PROCESS, &newsid);
15341da177e4SLinus Torvalds 		if (rc)
15351da177e4SLinus Torvalds 			return rc;
15361da177e4SLinus Torvalds 	}
15371da177e4SLinus Torvalds 
15381da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, FS);
15391da177e4SLinus Torvalds 	ad.u.fs.mnt = bprm->file->f_vfsmnt;
15401da177e4SLinus Torvalds 	ad.u.fs.dentry = bprm->file->f_dentry;
15411da177e4SLinus Torvalds 
15421da177e4SLinus Torvalds 	if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)
15431da177e4SLinus Torvalds 		newsid = tsec->sid;
15441da177e4SLinus Torvalds 
15451da177e4SLinus Torvalds         if (tsec->sid == newsid) {
15461da177e4SLinus Torvalds 		rc = avc_has_perm(tsec->sid, isec->sid,
15471da177e4SLinus Torvalds 				  SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
15481da177e4SLinus Torvalds 		if (rc)
15491da177e4SLinus Torvalds 			return rc;
15501da177e4SLinus Torvalds 	} else {
15511da177e4SLinus Torvalds 		/* Check permissions for the transition. */
15521da177e4SLinus Torvalds 		rc = avc_has_perm(tsec->sid, newsid,
15531da177e4SLinus Torvalds 				  SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
15541da177e4SLinus Torvalds 		if (rc)
15551da177e4SLinus Torvalds 			return rc;
15561da177e4SLinus Torvalds 
15571da177e4SLinus Torvalds 		rc = avc_has_perm(newsid, isec->sid,
15581da177e4SLinus Torvalds 				  SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
15591da177e4SLinus Torvalds 		if (rc)
15601da177e4SLinus Torvalds 			return rc;
15611da177e4SLinus Torvalds 
15621da177e4SLinus Torvalds 		/* Clear any possibly unsafe personality bits on exec: */
15631da177e4SLinus Torvalds 		current->personality &= ~PER_CLEAR_ON_SETID;
15641da177e4SLinus Torvalds 
15651da177e4SLinus Torvalds 		/* Set the security field to the new SID. */
15661da177e4SLinus Torvalds 		bsec->sid = newsid;
15671da177e4SLinus Torvalds 	}
15681da177e4SLinus Torvalds 
15691da177e4SLinus Torvalds 	bsec->set = 1;
15701da177e4SLinus Torvalds 	return 0;
15711da177e4SLinus Torvalds }
15721da177e4SLinus Torvalds 
15731da177e4SLinus Torvalds static int selinux_bprm_check_security (struct linux_binprm *bprm)
15741da177e4SLinus Torvalds {
15751da177e4SLinus Torvalds 	return secondary_ops->bprm_check_security(bprm);
15761da177e4SLinus Torvalds }
15771da177e4SLinus Torvalds 
15781da177e4SLinus Torvalds 
15791da177e4SLinus Torvalds static int selinux_bprm_secureexec (struct linux_binprm *bprm)
15801da177e4SLinus Torvalds {
15811da177e4SLinus Torvalds 	struct task_security_struct *tsec = current->security;
15821da177e4SLinus Torvalds 	int atsecure = 0;
15831da177e4SLinus Torvalds 
15841da177e4SLinus Torvalds 	if (tsec->osid != tsec->sid) {
15851da177e4SLinus Torvalds 		/* Enable secure mode for SIDs transitions unless
15861da177e4SLinus Torvalds 		   the noatsecure permission is granted between
15871da177e4SLinus Torvalds 		   the two SIDs, i.e. ahp returns 0. */
15881da177e4SLinus Torvalds 		atsecure = avc_has_perm(tsec->osid, tsec->sid,
15891da177e4SLinus Torvalds 					 SECCLASS_PROCESS,
15901da177e4SLinus Torvalds 					 PROCESS__NOATSECURE, NULL);
15911da177e4SLinus Torvalds 	}
15921da177e4SLinus Torvalds 
15931da177e4SLinus Torvalds 	return (atsecure || secondary_ops->bprm_secureexec(bprm));
15941da177e4SLinus Torvalds }
15951da177e4SLinus Torvalds 
15961da177e4SLinus Torvalds static void selinux_bprm_free_security(struct linux_binprm *bprm)
15971da177e4SLinus Torvalds {
15989a5f04bfSJesper Juhl 	kfree(bprm->security);
15991da177e4SLinus Torvalds 	bprm->security = NULL;
16001da177e4SLinus Torvalds }
16011da177e4SLinus Torvalds 
16021da177e4SLinus Torvalds extern struct vfsmount *selinuxfs_mount;
16031da177e4SLinus Torvalds extern struct dentry *selinux_null;
16041da177e4SLinus Torvalds 
16051da177e4SLinus Torvalds /* Derived from fs/exec.c:flush_old_files. */
16061da177e4SLinus Torvalds static inline void flush_unauthorized_files(struct files_struct * files)
16071da177e4SLinus Torvalds {
16081da177e4SLinus Torvalds 	struct avc_audit_data ad;
16091da177e4SLinus Torvalds 	struct file *file, *devnull = NULL;
16101da177e4SLinus Torvalds 	struct tty_struct *tty = current->signal->tty;
1611badf1662SDipankar Sarma 	struct fdtable *fdt;
16121da177e4SLinus Torvalds 	long j = -1;
16131da177e4SLinus Torvalds 
16141da177e4SLinus Torvalds 	if (tty) {
16151da177e4SLinus Torvalds 		file_list_lock();
16162f512016SEric Dumazet 		file = list_entry(tty->tty_files.next, typeof(*file), f_u.fu_list);
16171da177e4SLinus Torvalds 		if (file) {
16181da177e4SLinus Torvalds 			/* Revalidate access to controlling tty.
16191da177e4SLinus Torvalds 			   Use inode_has_perm on the tty inode directly rather
16201da177e4SLinus Torvalds 			   than using file_has_perm, as this particular open
16211da177e4SLinus Torvalds 			   file may belong to another process and we are only
16221da177e4SLinus Torvalds 			   interested in the inode-based check here. */
16231da177e4SLinus Torvalds 			struct inode *inode = file->f_dentry->d_inode;
16241da177e4SLinus Torvalds 			if (inode_has_perm(current, inode,
16251da177e4SLinus Torvalds 					   FILE__READ | FILE__WRITE, NULL)) {
16261da177e4SLinus Torvalds 				/* Reset controlling tty. */
16271da177e4SLinus Torvalds 				current->signal->tty = NULL;
16281da177e4SLinus Torvalds 				current->signal->tty_old_pgrp = 0;
16291da177e4SLinus Torvalds 			}
16301da177e4SLinus Torvalds 		}
16311da177e4SLinus Torvalds 		file_list_unlock();
16321da177e4SLinus Torvalds 	}
16331da177e4SLinus Torvalds 
16341da177e4SLinus Torvalds 	/* Revalidate access to inherited open files. */
16351da177e4SLinus Torvalds 
16361da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad,FS);
16371da177e4SLinus Torvalds 
16381da177e4SLinus Torvalds 	spin_lock(&files->file_lock);
16391da177e4SLinus Torvalds 	for (;;) {
16401da177e4SLinus Torvalds 		unsigned long set, i;
16411da177e4SLinus Torvalds 		int fd;
16421da177e4SLinus Torvalds 
16431da177e4SLinus Torvalds 		j++;
16441da177e4SLinus Torvalds 		i = j * __NFDBITS;
1645badf1662SDipankar Sarma 		fdt = files_fdtable(files);
1646badf1662SDipankar Sarma 		if (i >= fdt->max_fds || i >= fdt->max_fdset)
16471da177e4SLinus Torvalds 			break;
1648badf1662SDipankar Sarma 		set = fdt->open_fds->fds_bits[j];
16491da177e4SLinus Torvalds 		if (!set)
16501da177e4SLinus Torvalds 			continue;
16511da177e4SLinus Torvalds 		spin_unlock(&files->file_lock);
16521da177e4SLinus Torvalds 		for ( ; set ; i++,set >>= 1) {
16531da177e4SLinus Torvalds 			if (set & 1) {
16541da177e4SLinus Torvalds 				file = fget(i);
16551da177e4SLinus Torvalds 				if (!file)
16561da177e4SLinus Torvalds 					continue;
16571da177e4SLinus Torvalds 				if (file_has_perm(current,
16581da177e4SLinus Torvalds 						  file,
16591da177e4SLinus Torvalds 						  file_to_av(file))) {
16601da177e4SLinus Torvalds 					sys_close(i);
16611da177e4SLinus Torvalds 					fd = get_unused_fd();
16621da177e4SLinus Torvalds 					if (fd != i) {
16631da177e4SLinus Torvalds 						if (fd >= 0)
16641da177e4SLinus Torvalds 							put_unused_fd(fd);
16651da177e4SLinus Torvalds 						fput(file);
16661da177e4SLinus Torvalds 						continue;
16671da177e4SLinus Torvalds 					}
16681da177e4SLinus Torvalds 					if (devnull) {
1669095975daSNick Piggin 						get_file(devnull);
16701da177e4SLinus Torvalds 					} else {
16711da177e4SLinus Torvalds 						devnull = dentry_open(dget(selinux_null), mntget(selinuxfs_mount), O_RDWR);
16721da177e4SLinus Torvalds 						if (!devnull) {
16731da177e4SLinus Torvalds 							put_unused_fd(fd);
16741da177e4SLinus Torvalds 							fput(file);
16751da177e4SLinus Torvalds 							continue;
16761da177e4SLinus Torvalds 						}
16771da177e4SLinus Torvalds 					}
16781da177e4SLinus Torvalds 					fd_install(fd, devnull);
16791da177e4SLinus Torvalds 				}
16801da177e4SLinus Torvalds 				fput(file);
16811da177e4SLinus Torvalds 			}
16821da177e4SLinus Torvalds 		}
16831da177e4SLinus Torvalds 		spin_lock(&files->file_lock);
16841da177e4SLinus Torvalds 
16851da177e4SLinus Torvalds 	}
16861da177e4SLinus Torvalds 	spin_unlock(&files->file_lock);
16871da177e4SLinus Torvalds }
16881da177e4SLinus Torvalds 
16891da177e4SLinus Torvalds static void selinux_bprm_apply_creds(struct linux_binprm *bprm, int unsafe)
16901da177e4SLinus Torvalds {
16911da177e4SLinus Torvalds 	struct task_security_struct *tsec;
16921da177e4SLinus Torvalds 	struct bprm_security_struct *bsec;
16931da177e4SLinus Torvalds 	u32 sid;
16941da177e4SLinus Torvalds 	int rc;
16951da177e4SLinus Torvalds 
16961da177e4SLinus Torvalds 	secondary_ops->bprm_apply_creds(bprm, unsafe);
16971da177e4SLinus Torvalds 
16981da177e4SLinus Torvalds 	tsec = current->security;
16991da177e4SLinus Torvalds 
17001da177e4SLinus Torvalds 	bsec = bprm->security;
17011da177e4SLinus Torvalds 	sid = bsec->sid;
17021da177e4SLinus Torvalds 
17031da177e4SLinus Torvalds 	tsec->osid = tsec->sid;
17041da177e4SLinus Torvalds 	bsec->unsafe = 0;
17051da177e4SLinus Torvalds 	if (tsec->sid != sid) {
17061da177e4SLinus Torvalds 		/* Check for shared state.  If not ok, leave SID
17071da177e4SLinus Torvalds 		   unchanged and kill. */
17081da177e4SLinus Torvalds 		if (unsafe & LSM_UNSAFE_SHARE) {
17091da177e4SLinus Torvalds 			rc = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS,
17101da177e4SLinus Torvalds 					PROCESS__SHARE, NULL);
17111da177e4SLinus Torvalds 			if (rc) {
17121da177e4SLinus Torvalds 				bsec->unsafe = 1;
17131da177e4SLinus Torvalds 				return;
17141da177e4SLinus Torvalds 			}
17151da177e4SLinus Torvalds 		}
17161da177e4SLinus Torvalds 
17171da177e4SLinus Torvalds 		/* Check for ptracing, and update the task SID if ok.
17181da177e4SLinus Torvalds 		   Otherwise, leave SID unchanged and kill. */
17191da177e4SLinus Torvalds 		if (unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
17201da177e4SLinus Torvalds 			rc = avc_has_perm(tsec->ptrace_sid, sid,
17211da177e4SLinus Torvalds 					  SECCLASS_PROCESS, PROCESS__PTRACE,
17221da177e4SLinus Torvalds 					  NULL);
17231da177e4SLinus Torvalds 			if (rc) {
17241da177e4SLinus Torvalds 				bsec->unsafe = 1;
17251da177e4SLinus Torvalds 				return;
17261da177e4SLinus Torvalds 			}
17271da177e4SLinus Torvalds 		}
17281da177e4SLinus Torvalds 		tsec->sid = sid;
17291da177e4SLinus Torvalds 	}
17301da177e4SLinus Torvalds }
17311da177e4SLinus Torvalds 
17321da177e4SLinus Torvalds /*
17331da177e4SLinus Torvalds  * called after apply_creds without the task lock held
17341da177e4SLinus Torvalds  */
17351da177e4SLinus Torvalds static void selinux_bprm_post_apply_creds(struct linux_binprm *bprm)
17361da177e4SLinus Torvalds {
17371da177e4SLinus Torvalds 	struct task_security_struct *tsec;
17381da177e4SLinus Torvalds 	struct rlimit *rlim, *initrlim;
17391da177e4SLinus Torvalds 	struct itimerval itimer;
17401da177e4SLinus Torvalds 	struct bprm_security_struct *bsec;
17411da177e4SLinus Torvalds 	int rc, i;
17421da177e4SLinus Torvalds 
17431da177e4SLinus Torvalds 	tsec = current->security;
17441da177e4SLinus Torvalds 	bsec = bprm->security;
17451da177e4SLinus Torvalds 
17461da177e4SLinus Torvalds 	if (bsec->unsafe) {
17471da177e4SLinus Torvalds 		force_sig_specific(SIGKILL, current);
17481da177e4SLinus Torvalds 		return;
17491da177e4SLinus Torvalds 	}
17501da177e4SLinus Torvalds 	if (tsec->osid == tsec->sid)
17511da177e4SLinus Torvalds 		return;
17521da177e4SLinus Torvalds 
17531da177e4SLinus Torvalds 	/* Close files for which the new task SID is not authorized. */
17541da177e4SLinus Torvalds 	flush_unauthorized_files(current->files);
17551da177e4SLinus Torvalds 
17561da177e4SLinus Torvalds 	/* Check whether the new SID can inherit signal state
17571da177e4SLinus Torvalds 	   from the old SID.  If not, clear itimers to avoid
17581da177e4SLinus Torvalds 	   subsequent signal generation and flush and unblock
17591da177e4SLinus Torvalds 	   signals. This must occur _after_ the task SID has
17601da177e4SLinus Torvalds 	  been updated so that any kill done after the flush
17611da177e4SLinus Torvalds 	  will be checked against the new SID. */
17621da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->osid, tsec->sid, SECCLASS_PROCESS,
17631da177e4SLinus Torvalds 			  PROCESS__SIGINH, NULL);
17641da177e4SLinus Torvalds 	if (rc) {
17651da177e4SLinus Torvalds 		memset(&itimer, 0, sizeof itimer);
17661da177e4SLinus Torvalds 		for (i = 0; i < 3; i++)
17671da177e4SLinus Torvalds 			do_setitimer(i, &itimer, NULL);
17681da177e4SLinus Torvalds 		flush_signals(current);
17691da177e4SLinus Torvalds 		spin_lock_irq(&current->sighand->siglock);
17701da177e4SLinus Torvalds 		flush_signal_handlers(current, 1);
17711da177e4SLinus Torvalds 		sigemptyset(&current->blocked);
17721da177e4SLinus Torvalds 		recalc_sigpending();
17731da177e4SLinus Torvalds 		spin_unlock_irq(&current->sighand->siglock);
17741da177e4SLinus Torvalds 	}
17751da177e4SLinus Torvalds 
17761da177e4SLinus Torvalds 	/* Check whether the new SID can inherit resource limits
17771da177e4SLinus Torvalds 	   from the old SID.  If not, reset all soft limits to
17781da177e4SLinus Torvalds 	   the lower of the current task's hard limit and the init
17791da177e4SLinus Torvalds 	   task's soft limit.  Note that the setting of hard limits
17801da177e4SLinus Torvalds 	   (even to lower them) can be controlled by the setrlimit
17811da177e4SLinus Torvalds 	   check. The inclusion of the init task's soft limit into
17821da177e4SLinus Torvalds 	   the computation is to avoid resetting soft limits higher
17831da177e4SLinus Torvalds 	   than the default soft limit for cases where the default
17841da177e4SLinus Torvalds 	   is lower than the hard limit, e.g. RLIMIT_CORE or
17851da177e4SLinus Torvalds 	   RLIMIT_STACK.*/
17861da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->osid, tsec->sid, SECCLASS_PROCESS,
17871da177e4SLinus Torvalds 			  PROCESS__RLIMITINH, NULL);
17881da177e4SLinus Torvalds 	if (rc) {
17891da177e4SLinus Torvalds 		for (i = 0; i < RLIM_NLIMITS; i++) {
17901da177e4SLinus Torvalds 			rlim = current->signal->rlim + i;
17911da177e4SLinus Torvalds 			initrlim = init_task.signal->rlim+i;
17921da177e4SLinus Torvalds 			rlim->rlim_cur = min(rlim->rlim_max,initrlim->rlim_cur);
17931da177e4SLinus Torvalds 		}
17941da177e4SLinus Torvalds 		if (current->signal->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) {
17951da177e4SLinus Torvalds 			/*
17961da177e4SLinus Torvalds 			 * This will cause RLIMIT_CPU calculations
17971da177e4SLinus Torvalds 			 * to be refigured.
17981da177e4SLinus Torvalds 			 */
17991da177e4SLinus Torvalds 			current->it_prof_expires = jiffies_to_cputime(1);
18001da177e4SLinus Torvalds 		}
18011da177e4SLinus Torvalds 	}
18021da177e4SLinus Torvalds 
18031da177e4SLinus Torvalds 	/* Wake up the parent if it is waiting so that it can
18041da177e4SLinus Torvalds 	   recheck wait permission to the new task SID. */
18051da177e4SLinus Torvalds 	wake_up_interruptible(&current->parent->signal->wait_chldexit);
18061da177e4SLinus Torvalds }
18071da177e4SLinus Torvalds 
18081da177e4SLinus Torvalds /* superblock security operations */
18091da177e4SLinus Torvalds 
18101da177e4SLinus Torvalds static int selinux_sb_alloc_security(struct super_block *sb)
18111da177e4SLinus Torvalds {
18121da177e4SLinus Torvalds 	return superblock_alloc_security(sb);
18131da177e4SLinus Torvalds }
18141da177e4SLinus Torvalds 
18151da177e4SLinus Torvalds static void selinux_sb_free_security(struct super_block *sb)
18161da177e4SLinus Torvalds {
18171da177e4SLinus Torvalds 	superblock_free_security(sb);
18181da177e4SLinus Torvalds }
18191da177e4SLinus Torvalds 
18201da177e4SLinus Torvalds static inline int match_prefix(char *prefix, int plen, char *option, int olen)
18211da177e4SLinus Torvalds {
18221da177e4SLinus Torvalds 	if (plen > olen)
18231da177e4SLinus Torvalds 		return 0;
18241da177e4SLinus Torvalds 
18251da177e4SLinus Torvalds 	return !memcmp(prefix, option, plen);
18261da177e4SLinus Torvalds }
18271da177e4SLinus Torvalds 
18281da177e4SLinus Torvalds static inline int selinux_option(char *option, int len)
18291da177e4SLinus Torvalds {
18301da177e4SLinus Torvalds 	return (match_prefix("context=", sizeof("context=")-1, option, len) ||
18311da177e4SLinus Torvalds 	        match_prefix("fscontext=", sizeof("fscontext=")-1, option, len) ||
18321da177e4SLinus Torvalds 	        match_prefix("defcontext=", sizeof("defcontext=")-1, option, len));
18331da177e4SLinus Torvalds }
18341da177e4SLinus Torvalds 
18351da177e4SLinus Torvalds static inline void take_option(char **to, char *from, int *first, int len)
18361da177e4SLinus Torvalds {
18371da177e4SLinus Torvalds 	if (!*first) {
18381da177e4SLinus Torvalds 		**to = ',';
18391da177e4SLinus Torvalds 		*to += 1;
18401da177e4SLinus Torvalds 	}
18411da177e4SLinus Torvalds 	else
18421da177e4SLinus Torvalds 		*first = 0;
18431da177e4SLinus Torvalds 	memcpy(*to, from, len);
18441da177e4SLinus Torvalds 	*to += len;
18451da177e4SLinus Torvalds }
18461da177e4SLinus Torvalds 
18471da177e4SLinus Torvalds static int selinux_sb_copy_data(struct file_system_type *type, void *orig, void *copy)
18481da177e4SLinus Torvalds {
18491da177e4SLinus Torvalds 	int fnosec, fsec, rc = 0;
18501da177e4SLinus Torvalds 	char *in_save, *in_curr, *in_end;
18511da177e4SLinus Torvalds 	char *sec_curr, *nosec_save, *nosec;
18521da177e4SLinus Torvalds 
18531da177e4SLinus Torvalds 	in_curr = orig;
18541da177e4SLinus Torvalds 	sec_curr = copy;
18551da177e4SLinus Torvalds 
18561da177e4SLinus Torvalds 	/* Binary mount data: just copy */
18571da177e4SLinus Torvalds 	if (type->fs_flags & FS_BINARY_MOUNTDATA) {
18581da177e4SLinus Torvalds 		copy_page(sec_curr, in_curr);
18591da177e4SLinus Torvalds 		goto out;
18601da177e4SLinus Torvalds 	}
18611da177e4SLinus Torvalds 
18621da177e4SLinus Torvalds 	nosec = (char *)get_zeroed_page(GFP_KERNEL);
18631da177e4SLinus Torvalds 	if (!nosec) {
18641da177e4SLinus Torvalds 		rc = -ENOMEM;
18651da177e4SLinus Torvalds 		goto out;
18661da177e4SLinus Torvalds 	}
18671da177e4SLinus Torvalds 
18681da177e4SLinus Torvalds 	nosec_save = nosec;
18691da177e4SLinus Torvalds 	fnosec = fsec = 1;
18701da177e4SLinus Torvalds 	in_save = in_end = orig;
18711da177e4SLinus Torvalds 
18721da177e4SLinus Torvalds 	do {
18731da177e4SLinus Torvalds 		if (*in_end == ',' || *in_end == '\0') {
18741da177e4SLinus Torvalds 			int len = in_end - in_curr;
18751da177e4SLinus Torvalds 
18761da177e4SLinus Torvalds 			if (selinux_option(in_curr, len))
18771da177e4SLinus Torvalds 				take_option(&sec_curr, in_curr, &fsec, len);
18781da177e4SLinus Torvalds 			else
18791da177e4SLinus Torvalds 				take_option(&nosec, in_curr, &fnosec, len);
18801da177e4SLinus Torvalds 
18811da177e4SLinus Torvalds 			in_curr = in_end + 1;
18821da177e4SLinus Torvalds 		}
18831da177e4SLinus Torvalds 	} while (*in_end++);
18841da177e4SLinus Torvalds 
18856931dfc9SEric Paris 	strcpy(in_save, nosec_save);
1886da3caa20SGerald Schaefer 	free_page((unsigned long)nosec_save);
18871da177e4SLinus Torvalds out:
18881da177e4SLinus Torvalds 	return rc;
18891da177e4SLinus Torvalds }
18901da177e4SLinus Torvalds 
18911da177e4SLinus Torvalds static int selinux_sb_kern_mount(struct super_block *sb, void *data)
18921da177e4SLinus Torvalds {
18931da177e4SLinus Torvalds 	struct avc_audit_data ad;
18941da177e4SLinus Torvalds 	int rc;
18951da177e4SLinus Torvalds 
18961da177e4SLinus Torvalds 	rc = superblock_doinit(sb, data);
18971da177e4SLinus Torvalds 	if (rc)
18981da177e4SLinus Torvalds 		return rc;
18991da177e4SLinus Torvalds 
19001da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad,FS);
19011da177e4SLinus Torvalds 	ad.u.fs.dentry = sb->s_root;
19021da177e4SLinus Torvalds 	return superblock_has_perm(current, sb, FILESYSTEM__MOUNT, &ad);
19031da177e4SLinus Torvalds }
19041da177e4SLinus Torvalds 
19051da177e4SLinus Torvalds static int selinux_sb_statfs(struct super_block *sb)
19061da177e4SLinus Torvalds {
19071da177e4SLinus Torvalds 	struct avc_audit_data ad;
19081da177e4SLinus Torvalds 
19091da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad,FS);
19101da177e4SLinus Torvalds 	ad.u.fs.dentry = sb->s_root;
19111da177e4SLinus Torvalds 	return superblock_has_perm(current, sb, FILESYSTEM__GETATTR, &ad);
19121da177e4SLinus Torvalds }
19131da177e4SLinus Torvalds 
19141da177e4SLinus Torvalds static int selinux_mount(char * dev_name,
19151da177e4SLinus Torvalds                          struct nameidata *nd,
19161da177e4SLinus Torvalds                          char * type,
19171da177e4SLinus Torvalds                          unsigned long flags,
19181da177e4SLinus Torvalds                          void * data)
19191da177e4SLinus Torvalds {
19201da177e4SLinus Torvalds 	int rc;
19211da177e4SLinus Torvalds 
19221da177e4SLinus Torvalds 	rc = secondary_ops->sb_mount(dev_name, nd, type, flags, data);
19231da177e4SLinus Torvalds 	if (rc)
19241da177e4SLinus Torvalds 		return rc;
19251da177e4SLinus Torvalds 
19261da177e4SLinus Torvalds 	if (flags & MS_REMOUNT)
19271da177e4SLinus Torvalds 		return superblock_has_perm(current, nd->mnt->mnt_sb,
19281da177e4SLinus Torvalds 		                           FILESYSTEM__REMOUNT, NULL);
19291da177e4SLinus Torvalds 	else
19301da177e4SLinus Torvalds 		return dentry_has_perm(current, nd->mnt, nd->dentry,
19311da177e4SLinus Torvalds 		                       FILE__MOUNTON);
19321da177e4SLinus Torvalds }
19331da177e4SLinus Torvalds 
19341da177e4SLinus Torvalds static int selinux_umount(struct vfsmount *mnt, int flags)
19351da177e4SLinus Torvalds {
19361da177e4SLinus Torvalds 	int rc;
19371da177e4SLinus Torvalds 
19381da177e4SLinus Torvalds 	rc = secondary_ops->sb_umount(mnt, flags);
19391da177e4SLinus Torvalds 	if (rc)
19401da177e4SLinus Torvalds 		return rc;
19411da177e4SLinus Torvalds 
19421da177e4SLinus Torvalds 	return superblock_has_perm(current,mnt->mnt_sb,
19431da177e4SLinus Torvalds 	                           FILESYSTEM__UNMOUNT,NULL);
19441da177e4SLinus Torvalds }
19451da177e4SLinus Torvalds 
19461da177e4SLinus Torvalds /* inode security operations */
19471da177e4SLinus Torvalds 
19481da177e4SLinus Torvalds static int selinux_inode_alloc_security(struct inode *inode)
19491da177e4SLinus Torvalds {
19501da177e4SLinus Torvalds 	return inode_alloc_security(inode);
19511da177e4SLinus Torvalds }
19521da177e4SLinus Torvalds 
19531da177e4SLinus Torvalds static void selinux_inode_free_security(struct inode *inode)
19541da177e4SLinus Torvalds {
19551da177e4SLinus Torvalds 	inode_free_security(inode);
19561da177e4SLinus Torvalds }
19571da177e4SLinus Torvalds 
19585e41ff9eSStephen Smalley static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
19595e41ff9eSStephen Smalley 				       char **name, void **value,
19605e41ff9eSStephen Smalley 				       size_t *len)
19615e41ff9eSStephen Smalley {
19625e41ff9eSStephen Smalley 	struct task_security_struct *tsec;
19635e41ff9eSStephen Smalley 	struct inode_security_struct *dsec;
19645e41ff9eSStephen Smalley 	struct superblock_security_struct *sbsec;
1965570bc1c2SStephen Smalley 	u32 newsid, clen;
19665e41ff9eSStephen Smalley 	int rc;
1967570bc1c2SStephen Smalley 	char *namep = NULL, *context;
19685e41ff9eSStephen Smalley 
19695e41ff9eSStephen Smalley 	tsec = current->security;
19705e41ff9eSStephen Smalley 	dsec = dir->i_security;
19715e41ff9eSStephen Smalley 	sbsec = dir->i_sb->s_security;
19725e41ff9eSStephen Smalley 
19735e41ff9eSStephen Smalley 	if (tsec->create_sid && sbsec->behavior != SECURITY_FS_USE_MNTPOINT) {
19745e41ff9eSStephen Smalley 		newsid = tsec->create_sid;
19755e41ff9eSStephen Smalley 	} else {
19765e41ff9eSStephen Smalley 		rc = security_transition_sid(tsec->sid, dsec->sid,
19775e41ff9eSStephen Smalley 					     inode_mode_to_security_class(inode->i_mode),
19785e41ff9eSStephen Smalley 					     &newsid);
19795e41ff9eSStephen Smalley 		if (rc) {
19805e41ff9eSStephen Smalley 			printk(KERN_WARNING "%s:  "
19815e41ff9eSStephen Smalley 			       "security_transition_sid failed, rc=%d (dev=%s "
19825e41ff9eSStephen Smalley 			       "ino=%ld)\n",
19835e41ff9eSStephen Smalley 			       __FUNCTION__,
19845e41ff9eSStephen Smalley 			       -rc, inode->i_sb->s_id, inode->i_ino);
19855e41ff9eSStephen Smalley 			return rc;
19865e41ff9eSStephen Smalley 		}
19875e41ff9eSStephen Smalley 	}
19885e41ff9eSStephen Smalley 
19895e41ff9eSStephen Smalley 	inode_security_set_sid(inode, newsid);
19905e41ff9eSStephen Smalley 
19918aad3875SStephen Smalley 	if (!ss_initialized || sbsec->behavior == SECURITY_FS_USE_MNTPOINT)
199225a74f3bSStephen Smalley 		return -EOPNOTSUPP;
199325a74f3bSStephen Smalley 
1994570bc1c2SStephen Smalley 	if (name) {
19955e41ff9eSStephen Smalley 		namep = kstrdup(XATTR_SELINUX_SUFFIX, GFP_KERNEL);
19965e41ff9eSStephen Smalley 		if (!namep)
19975e41ff9eSStephen Smalley 			return -ENOMEM;
19985e41ff9eSStephen Smalley 		*name = namep;
1999570bc1c2SStephen Smalley 	}
20005e41ff9eSStephen Smalley 
2001570bc1c2SStephen Smalley 	if (value && len) {
2002570bc1c2SStephen Smalley 		rc = security_sid_to_context(newsid, &context, &clen);
20035e41ff9eSStephen Smalley 		if (rc) {
20045e41ff9eSStephen Smalley 			kfree(namep);
20055e41ff9eSStephen Smalley 			return rc;
20065e41ff9eSStephen Smalley 		}
20075e41ff9eSStephen Smalley 		*value = context;
2008570bc1c2SStephen Smalley 		*len = clen;
2009570bc1c2SStephen Smalley 	}
20105e41ff9eSStephen Smalley 
20115e41ff9eSStephen Smalley 	return 0;
20125e41ff9eSStephen Smalley }
20135e41ff9eSStephen Smalley 
20141da177e4SLinus Torvalds static int selinux_inode_create(struct inode *dir, struct dentry *dentry, int mask)
20151da177e4SLinus Torvalds {
20161da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_FILE);
20171da177e4SLinus Torvalds }
20181da177e4SLinus Torvalds 
20191da177e4SLinus Torvalds static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
20201da177e4SLinus Torvalds {
20211da177e4SLinus Torvalds 	int rc;
20221da177e4SLinus Torvalds 
20231da177e4SLinus Torvalds 	rc = secondary_ops->inode_link(old_dentry,dir,new_dentry);
20241da177e4SLinus Torvalds 	if (rc)
20251da177e4SLinus Torvalds 		return rc;
20261da177e4SLinus Torvalds 	return may_link(dir, old_dentry, MAY_LINK);
20271da177e4SLinus Torvalds }
20281da177e4SLinus Torvalds 
20291da177e4SLinus Torvalds static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
20301da177e4SLinus Torvalds {
20311da177e4SLinus Torvalds 	int rc;
20321da177e4SLinus Torvalds 
20331da177e4SLinus Torvalds 	rc = secondary_ops->inode_unlink(dir, dentry);
20341da177e4SLinus Torvalds 	if (rc)
20351da177e4SLinus Torvalds 		return rc;
20361da177e4SLinus Torvalds 	return may_link(dir, dentry, MAY_UNLINK);
20371da177e4SLinus Torvalds }
20381da177e4SLinus Torvalds 
20391da177e4SLinus Torvalds static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
20401da177e4SLinus Torvalds {
20411da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_LNK_FILE);
20421da177e4SLinus Torvalds }
20431da177e4SLinus Torvalds 
20441da177e4SLinus Torvalds static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, int mask)
20451da177e4SLinus Torvalds {
20461da177e4SLinus Torvalds 	return may_create(dir, dentry, SECCLASS_DIR);
20471da177e4SLinus Torvalds }
20481da177e4SLinus Torvalds 
20491da177e4SLinus Torvalds static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
20501da177e4SLinus Torvalds {
20511da177e4SLinus Torvalds 	return may_link(dir, dentry, MAY_RMDIR);
20521da177e4SLinus Torvalds }
20531da177e4SLinus Torvalds 
20541da177e4SLinus Torvalds static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
20551da177e4SLinus Torvalds {
20561da177e4SLinus Torvalds 	int rc;
20571da177e4SLinus Torvalds 
20581da177e4SLinus Torvalds 	rc = secondary_ops->inode_mknod(dir, dentry, mode, dev);
20591da177e4SLinus Torvalds 	if (rc)
20601da177e4SLinus Torvalds 		return rc;
20611da177e4SLinus Torvalds 
20621da177e4SLinus Torvalds 	return may_create(dir, dentry, inode_mode_to_security_class(mode));
20631da177e4SLinus Torvalds }
20641da177e4SLinus Torvalds 
20651da177e4SLinus Torvalds static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
20661da177e4SLinus Torvalds                                 struct inode *new_inode, struct dentry *new_dentry)
20671da177e4SLinus Torvalds {
20681da177e4SLinus Torvalds 	return may_rename(old_inode, old_dentry, new_inode, new_dentry);
20691da177e4SLinus Torvalds }
20701da177e4SLinus Torvalds 
20711da177e4SLinus Torvalds static int selinux_inode_readlink(struct dentry *dentry)
20721da177e4SLinus Torvalds {
20731da177e4SLinus Torvalds 	return dentry_has_perm(current, NULL, dentry, FILE__READ);
20741da177e4SLinus Torvalds }
20751da177e4SLinus Torvalds 
20761da177e4SLinus Torvalds static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata)
20771da177e4SLinus Torvalds {
20781da177e4SLinus Torvalds 	int rc;
20791da177e4SLinus Torvalds 
20801da177e4SLinus Torvalds 	rc = secondary_ops->inode_follow_link(dentry,nameidata);
20811da177e4SLinus Torvalds 	if (rc)
20821da177e4SLinus Torvalds 		return rc;
20831da177e4SLinus Torvalds 	return dentry_has_perm(current, NULL, dentry, FILE__READ);
20841da177e4SLinus Torvalds }
20851da177e4SLinus Torvalds 
20861da177e4SLinus Torvalds static int selinux_inode_permission(struct inode *inode, int mask,
20871da177e4SLinus Torvalds 				    struct nameidata *nd)
20881da177e4SLinus Torvalds {
20891da177e4SLinus Torvalds 	int rc;
20901da177e4SLinus Torvalds 
20911da177e4SLinus Torvalds 	rc = secondary_ops->inode_permission(inode, mask, nd);
20921da177e4SLinus Torvalds 	if (rc)
20931da177e4SLinus Torvalds 		return rc;
20941da177e4SLinus Torvalds 
20951da177e4SLinus Torvalds 	if (!mask) {
20961da177e4SLinus Torvalds 		/* No permission to check.  Existence test. */
20971da177e4SLinus Torvalds 		return 0;
20981da177e4SLinus Torvalds 	}
20991da177e4SLinus Torvalds 
21001da177e4SLinus Torvalds 	return inode_has_perm(current, inode,
21011da177e4SLinus Torvalds 			       file_mask_to_av(inode->i_mode, mask), NULL);
21021da177e4SLinus Torvalds }
21031da177e4SLinus Torvalds 
21041da177e4SLinus Torvalds static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
21051da177e4SLinus Torvalds {
21061da177e4SLinus Torvalds 	int rc;
21071da177e4SLinus Torvalds 
21081da177e4SLinus Torvalds 	rc = secondary_ops->inode_setattr(dentry, iattr);
21091da177e4SLinus Torvalds 	if (rc)
21101da177e4SLinus Torvalds 		return rc;
21111da177e4SLinus Torvalds 
21121da177e4SLinus Torvalds 	if (iattr->ia_valid & ATTR_FORCE)
21131da177e4SLinus Torvalds 		return 0;
21141da177e4SLinus Torvalds 
21151da177e4SLinus Torvalds 	if (iattr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
21161da177e4SLinus Torvalds 			       ATTR_ATIME_SET | ATTR_MTIME_SET))
21171da177e4SLinus Torvalds 		return dentry_has_perm(current, NULL, dentry, FILE__SETATTR);
21181da177e4SLinus Torvalds 
21191da177e4SLinus Torvalds 	return dentry_has_perm(current, NULL, dentry, FILE__WRITE);
21201da177e4SLinus Torvalds }
21211da177e4SLinus Torvalds 
21221da177e4SLinus Torvalds static int selinux_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
21231da177e4SLinus Torvalds {
21241da177e4SLinus Torvalds 	return dentry_has_perm(current, mnt, dentry, FILE__GETATTR);
21251da177e4SLinus Torvalds }
21261da177e4SLinus Torvalds 
21271da177e4SLinus Torvalds static int selinux_inode_setxattr(struct dentry *dentry, char *name, void *value, size_t size, int flags)
21281da177e4SLinus Torvalds {
21291da177e4SLinus Torvalds 	struct task_security_struct *tsec = current->security;
21301da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
21311da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
21321da177e4SLinus Torvalds 	struct superblock_security_struct *sbsec;
21331da177e4SLinus Torvalds 	struct avc_audit_data ad;
21341da177e4SLinus Torvalds 	u32 newsid;
21351da177e4SLinus Torvalds 	int rc = 0;
21361da177e4SLinus Torvalds 
21371da177e4SLinus Torvalds 	if (strcmp(name, XATTR_NAME_SELINUX)) {
21381da177e4SLinus Torvalds 		if (!strncmp(name, XATTR_SECURITY_PREFIX,
21391da177e4SLinus Torvalds 			     sizeof XATTR_SECURITY_PREFIX - 1) &&
21401da177e4SLinus Torvalds 		    !capable(CAP_SYS_ADMIN)) {
21411da177e4SLinus Torvalds 			/* A different attribute in the security namespace.
21421da177e4SLinus Torvalds 			   Restrict to administrator. */
21431da177e4SLinus Torvalds 			return -EPERM;
21441da177e4SLinus Torvalds 		}
21451da177e4SLinus Torvalds 
21461da177e4SLinus Torvalds 		/* Not an attribute we recognize, so just check the
21471da177e4SLinus Torvalds 		   ordinary setattr permission. */
21481da177e4SLinus Torvalds 		return dentry_has_perm(current, NULL, dentry, FILE__SETATTR);
21491da177e4SLinus Torvalds 	}
21501da177e4SLinus Torvalds 
21511da177e4SLinus Torvalds 	sbsec = inode->i_sb->s_security;
21521da177e4SLinus Torvalds 	if (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)
21531da177e4SLinus Torvalds 		return -EOPNOTSUPP;
21541da177e4SLinus Torvalds 
21551da177e4SLinus Torvalds 	if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
21561da177e4SLinus Torvalds 		return -EPERM;
21571da177e4SLinus Torvalds 
21581da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad,FS);
21591da177e4SLinus Torvalds 	ad.u.fs.dentry = dentry;
21601da177e4SLinus Torvalds 
21611da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, isec->sid, isec->sclass,
21621da177e4SLinus Torvalds 			  FILE__RELABELFROM, &ad);
21631da177e4SLinus Torvalds 	if (rc)
21641da177e4SLinus Torvalds 		return rc;
21651da177e4SLinus Torvalds 
21661da177e4SLinus Torvalds 	rc = security_context_to_sid(value, size, &newsid);
21671da177e4SLinus Torvalds 	if (rc)
21681da177e4SLinus Torvalds 		return rc;
21691da177e4SLinus Torvalds 
21701da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, newsid, isec->sclass,
21711da177e4SLinus Torvalds 			  FILE__RELABELTO, &ad);
21721da177e4SLinus Torvalds 	if (rc)
21731da177e4SLinus Torvalds 		return rc;
21741da177e4SLinus Torvalds 
21751da177e4SLinus Torvalds 	rc = security_validate_transition(isec->sid, newsid, tsec->sid,
21761da177e4SLinus Torvalds 	                                  isec->sclass);
21771da177e4SLinus Torvalds 	if (rc)
21781da177e4SLinus Torvalds 		return rc;
21791da177e4SLinus Torvalds 
21801da177e4SLinus Torvalds 	return avc_has_perm(newsid,
21811da177e4SLinus Torvalds 			    sbsec->sid,
21821da177e4SLinus Torvalds 			    SECCLASS_FILESYSTEM,
21831da177e4SLinus Torvalds 			    FILESYSTEM__ASSOCIATE,
21841da177e4SLinus Torvalds 			    &ad);
21851da177e4SLinus Torvalds }
21861da177e4SLinus Torvalds 
21871da177e4SLinus Torvalds static void selinux_inode_post_setxattr(struct dentry *dentry, char *name,
21881da177e4SLinus Torvalds                                         void *value, size_t size, int flags)
21891da177e4SLinus Torvalds {
21901da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
21911da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
21921da177e4SLinus Torvalds 	u32 newsid;
21931da177e4SLinus Torvalds 	int rc;
21941da177e4SLinus Torvalds 
21951da177e4SLinus Torvalds 	if (strcmp(name, XATTR_NAME_SELINUX)) {
21961da177e4SLinus Torvalds 		/* Not an attribute we recognize, so nothing to do. */
21971da177e4SLinus Torvalds 		return;
21981da177e4SLinus Torvalds 	}
21991da177e4SLinus Torvalds 
22001da177e4SLinus Torvalds 	rc = security_context_to_sid(value, size, &newsid);
22011da177e4SLinus Torvalds 	if (rc) {
22021da177e4SLinus Torvalds 		printk(KERN_WARNING "%s:  unable to obtain SID for context "
22031da177e4SLinus Torvalds 		       "%s, rc=%d\n", __FUNCTION__, (char*)value, -rc);
22041da177e4SLinus Torvalds 		return;
22051da177e4SLinus Torvalds 	}
22061da177e4SLinus Torvalds 
22071da177e4SLinus Torvalds 	isec->sid = newsid;
22081da177e4SLinus Torvalds 	return;
22091da177e4SLinus Torvalds }
22101da177e4SLinus Torvalds 
22111da177e4SLinus Torvalds static int selinux_inode_getxattr (struct dentry *dentry, char *name)
22121da177e4SLinus Torvalds {
22131da177e4SLinus Torvalds 	return dentry_has_perm(current, NULL, dentry, FILE__GETATTR);
22141da177e4SLinus Torvalds }
22151da177e4SLinus Torvalds 
22161da177e4SLinus Torvalds static int selinux_inode_listxattr (struct dentry *dentry)
22171da177e4SLinus Torvalds {
22181da177e4SLinus Torvalds 	return dentry_has_perm(current, NULL, dentry, FILE__GETATTR);
22191da177e4SLinus Torvalds }
22201da177e4SLinus Torvalds 
22211da177e4SLinus Torvalds static int selinux_inode_removexattr (struct dentry *dentry, char *name)
22221da177e4SLinus Torvalds {
22231da177e4SLinus Torvalds 	if (strcmp(name, XATTR_NAME_SELINUX)) {
22241da177e4SLinus Torvalds 		if (!strncmp(name, XATTR_SECURITY_PREFIX,
22251da177e4SLinus Torvalds 			     sizeof XATTR_SECURITY_PREFIX - 1) &&
22261da177e4SLinus Torvalds 		    !capable(CAP_SYS_ADMIN)) {
22271da177e4SLinus Torvalds 			/* A different attribute in the security namespace.
22281da177e4SLinus Torvalds 			   Restrict to administrator. */
22291da177e4SLinus Torvalds 			return -EPERM;
22301da177e4SLinus Torvalds 		}
22311da177e4SLinus Torvalds 
22321da177e4SLinus Torvalds 		/* Not an attribute we recognize, so just check the
22331da177e4SLinus Torvalds 		   ordinary setattr permission. Might want a separate
22341da177e4SLinus Torvalds 		   permission for removexattr. */
22351da177e4SLinus Torvalds 		return dentry_has_perm(current, NULL, dentry, FILE__SETATTR);
22361da177e4SLinus Torvalds 	}
22371da177e4SLinus Torvalds 
22381da177e4SLinus Torvalds 	/* No one is allowed to remove a SELinux security label.
22391da177e4SLinus Torvalds 	   You can change the label, but all data must be labeled. */
22401da177e4SLinus Torvalds 	return -EACCES;
22411da177e4SLinus Torvalds }
22421da177e4SLinus Torvalds 
22438c8570fbSDustin Kirkland static const char *selinux_inode_xattr_getsuffix(void)
22448c8570fbSDustin Kirkland {
22458c8570fbSDustin Kirkland       return XATTR_SELINUX_SUFFIX;
22468c8570fbSDustin Kirkland }
22478c8570fbSDustin Kirkland 
2248d381d8a9SJames Morris /*
2249d381d8a9SJames Morris  * Copy the in-core inode security context value to the user.  If the
2250d381d8a9SJames Morris  * getxattr() prior to this succeeded, check to see if we need to
2251d381d8a9SJames Morris  * canonicalize the value to be finally returned to the user.
2252d381d8a9SJames Morris  *
2253d381d8a9SJames Morris  * Permission check is handled by selinux_inode_getxattr hook.
2254d381d8a9SJames Morris  */
22557306a0b9SDustin Kirkland static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void *buffer, size_t size, int err)
22561da177e4SLinus Torvalds {
22571da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
22581da177e4SLinus Torvalds 
22598c8570fbSDustin Kirkland 	if (strcmp(name, XATTR_SELINUX_SUFFIX))
22608c8570fbSDustin Kirkland 		return -EOPNOTSUPP;
22611da177e4SLinus Torvalds 
22628c8570fbSDustin Kirkland 	return selinux_getsecurity(isec->sid, buffer, size);
22631da177e4SLinus Torvalds }
22641da177e4SLinus Torvalds 
22651da177e4SLinus Torvalds static int selinux_inode_setsecurity(struct inode *inode, const char *name,
22661da177e4SLinus Torvalds                                      const void *value, size_t size, int flags)
22671da177e4SLinus Torvalds {
22681da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
22691da177e4SLinus Torvalds 	u32 newsid;
22701da177e4SLinus Torvalds 	int rc;
22711da177e4SLinus Torvalds 
22721da177e4SLinus Torvalds 	if (strcmp(name, XATTR_SELINUX_SUFFIX))
22731da177e4SLinus Torvalds 		return -EOPNOTSUPP;
22741da177e4SLinus Torvalds 
22751da177e4SLinus Torvalds 	if (!value || !size)
22761da177e4SLinus Torvalds 		return -EACCES;
22771da177e4SLinus Torvalds 
22781da177e4SLinus Torvalds 	rc = security_context_to_sid((void*)value, size, &newsid);
22791da177e4SLinus Torvalds 	if (rc)
22801da177e4SLinus Torvalds 		return rc;
22811da177e4SLinus Torvalds 
22821da177e4SLinus Torvalds 	isec->sid = newsid;
22831da177e4SLinus Torvalds 	return 0;
22841da177e4SLinus Torvalds }
22851da177e4SLinus Torvalds 
22861da177e4SLinus Torvalds static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
22871da177e4SLinus Torvalds {
22881da177e4SLinus Torvalds 	const int len = sizeof(XATTR_NAME_SELINUX);
22891da177e4SLinus Torvalds 	if (buffer && len <= buffer_size)
22901da177e4SLinus Torvalds 		memcpy(buffer, XATTR_NAME_SELINUX, len);
22911da177e4SLinus Torvalds 	return len;
22921da177e4SLinus Torvalds }
22931da177e4SLinus Torvalds 
22941da177e4SLinus Torvalds /* file security operations */
22951da177e4SLinus Torvalds 
22961da177e4SLinus Torvalds static int selinux_file_permission(struct file *file, int mask)
22971da177e4SLinus Torvalds {
22981da177e4SLinus Torvalds 	struct inode *inode = file->f_dentry->d_inode;
22991da177e4SLinus Torvalds 
23001da177e4SLinus Torvalds 	if (!mask) {
23011da177e4SLinus Torvalds 		/* No permission to check.  Existence test. */
23021da177e4SLinus Torvalds 		return 0;
23031da177e4SLinus Torvalds 	}
23041da177e4SLinus Torvalds 
23051da177e4SLinus Torvalds 	/* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
23061da177e4SLinus Torvalds 	if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
23071da177e4SLinus Torvalds 		mask |= MAY_APPEND;
23081da177e4SLinus Torvalds 
23091da177e4SLinus Torvalds 	return file_has_perm(current, file,
23101da177e4SLinus Torvalds 			     file_mask_to_av(inode->i_mode, mask));
23111da177e4SLinus Torvalds }
23121da177e4SLinus Torvalds 
23131da177e4SLinus Torvalds static int selinux_file_alloc_security(struct file *file)
23141da177e4SLinus Torvalds {
23151da177e4SLinus Torvalds 	return file_alloc_security(file);
23161da177e4SLinus Torvalds }
23171da177e4SLinus Torvalds 
23181da177e4SLinus Torvalds static void selinux_file_free_security(struct file *file)
23191da177e4SLinus Torvalds {
23201da177e4SLinus Torvalds 	file_free_security(file);
23211da177e4SLinus Torvalds }
23221da177e4SLinus Torvalds 
23231da177e4SLinus Torvalds static int selinux_file_ioctl(struct file *file, unsigned int cmd,
23241da177e4SLinus Torvalds 			      unsigned long arg)
23251da177e4SLinus Torvalds {
23261da177e4SLinus Torvalds 	int error = 0;
23271da177e4SLinus Torvalds 
23281da177e4SLinus Torvalds 	switch (cmd) {
23291da177e4SLinus Torvalds 		case FIONREAD:
23301da177e4SLinus Torvalds 		/* fall through */
23311da177e4SLinus Torvalds 		case FIBMAP:
23321da177e4SLinus Torvalds 		/* fall through */
23331da177e4SLinus Torvalds 		case FIGETBSZ:
23341da177e4SLinus Torvalds 		/* fall through */
23351da177e4SLinus Torvalds 		case EXT2_IOC_GETFLAGS:
23361da177e4SLinus Torvalds 		/* fall through */
23371da177e4SLinus Torvalds 		case EXT2_IOC_GETVERSION:
23381da177e4SLinus Torvalds 			error = file_has_perm(current, file, FILE__GETATTR);
23391da177e4SLinus Torvalds 			break;
23401da177e4SLinus Torvalds 
23411da177e4SLinus Torvalds 		case EXT2_IOC_SETFLAGS:
23421da177e4SLinus Torvalds 		/* fall through */
23431da177e4SLinus Torvalds 		case EXT2_IOC_SETVERSION:
23441da177e4SLinus Torvalds 			error = file_has_perm(current, file, FILE__SETATTR);
23451da177e4SLinus Torvalds 			break;
23461da177e4SLinus Torvalds 
23471da177e4SLinus Torvalds 		/* sys_ioctl() checks */
23481da177e4SLinus Torvalds 		case FIONBIO:
23491da177e4SLinus Torvalds 		/* fall through */
23501da177e4SLinus Torvalds 		case FIOASYNC:
23511da177e4SLinus Torvalds 			error = file_has_perm(current, file, 0);
23521da177e4SLinus Torvalds 			break;
23531da177e4SLinus Torvalds 
23541da177e4SLinus Torvalds 	        case KDSKBENT:
23551da177e4SLinus Torvalds 	        case KDSKBSENT:
23561da177e4SLinus Torvalds 			error = task_has_capability(current,CAP_SYS_TTY_CONFIG);
23571da177e4SLinus Torvalds 			break;
23581da177e4SLinus Torvalds 
23591da177e4SLinus Torvalds 		/* default case assumes that the command will go
23601da177e4SLinus Torvalds 		 * to the file's ioctl() function.
23611da177e4SLinus Torvalds 		 */
23621da177e4SLinus Torvalds 		default:
23631da177e4SLinus Torvalds 			error = file_has_perm(current, file, FILE__IOCTL);
23641da177e4SLinus Torvalds 
23651da177e4SLinus Torvalds 	}
23661da177e4SLinus Torvalds 	return error;
23671da177e4SLinus Torvalds }
23681da177e4SLinus Torvalds 
23691da177e4SLinus Torvalds static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
23701da177e4SLinus Torvalds {
23711da177e4SLinus Torvalds #ifndef CONFIG_PPC32
23721da177e4SLinus Torvalds 	if ((prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) {
23731da177e4SLinus Torvalds 		/*
23741da177e4SLinus Torvalds 		 * We are making executable an anonymous mapping or a
23751da177e4SLinus Torvalds 		 * private file mapping that will also be writable.
23761da177e4SLinus Torvalds 		 * This has an additional check.
23771da177e4SLinus Torvalds 		 */
23781da177e4SLinus Torvalds 		int rc = task_has_perm(current, current, PROCESS__EXECMEM);
23791da177e4SLinus Torvalds 		if (rc)
23801da177e4SLinus Torvalds 			return rc;
23811da177e4SLinus Torvalds 	}
23821da177e4SLinus Torvalds #endif
23831da177e4SLinus Torvalds 
23841da177e4SLinus Torvalds 	if (file) {
23851da177e4SLinus Torvalds 		/* read access is always possible with a mapping */
23861da177e4SLinus Torvalds 		u32 av = FILE__READ;
23871da177e4SLinus Torvalds 
23881da177e4SLinus Torvalds 		/* write access only matters if the mapping is shared */
23891da177e4SLinus Torvalds 		if (shared && (prot & PROT_WRITE))
23901da177e4SLinus Torvalds 			av |= FILE__WRITE;
23911da177e4SLinus Torvalds 
23921da177e4SLinus Torvalds 		if (prot & PROT_EXEC)
23931da177e4SLinus Torvalds 			av |= FILE__EXECUTE;
23941da177e4SLinus Torvalds 
23951da177e4SLinus Torvalds 		return file_has_perm(current, file, av);
23961da177e4SLinus Torvalds 	}
23971da177e4SLinus Torvalds 	return 0;
23981da177e4SLinus Torvalds }
23991da177e4SLinus Torvalds 
24001da177e4SLinus Torvalds static int selinux_file_mmap(struct file *file, unsigned long reqprot,
24011da177e4SLinus Torvalds 			     unsigned long prot, unsigned long flags)
24021da177e4SLinus Torvalds {
24031da177e4SLinus Torvalds 	int rc;
24041da177e4SLinus Torvalds 
24051da177e4SLinus Torvalds 	rc = secondary_ops->file_mmap(file, reqprot, prot, flags);
24061da177e4SLinus Torvalds 	if (rc)
24071da177e4SLinus Torvalds 		return rc;
24081da177e4SLinus Torvalds 
24091da177e4SLinus Torvalds 	if (selinux_checkreqprot)
24101da177e4SLinus Torvalds 		prot = reqprot;
24111da177e4SLinus Torvalds 
24121da177e4SLinus Torvalds 	return file_map_prot_check(file, prot,
24131da177e4SLinus Torvalds 				   (flags & MAP_TYPE) == MAP_SHARED);
24141da177e4SLinus Torvalds }
24151da177e4SLinus Torvalds 
24161da177e4SLinus Torvalds static int selinux_file_mprotect(struct vm_area_struct *vma,
24171da177e4SLinus Torvalds 				 unsigned long reqprot,
24181da177e4SLinus Torvalds 				 unsigned long prot)
24191da177e4SLinus Torvalds {
24201da177e4SLinus Torvalds 	int rc;
24211da177e4SLinus Torvalds 
24221da177e4SLinus Torvalds 	rc = secondary_ops->file_mprotect(vma, reqprot, prot);
24231da177e4SLinus Torvalds 	if (rc)
24241da177e4SLinus Torvalds 		return rc;
24251da177e4SLinus Torvalds 
24261da177e4SLinus Torvalds 	if (selinux_checkreqprot)
24271da177e4SLinus Torvalds 		prot = reqprot;
24281da177e4SLinus Torvalds 
24291da177e4SLinus Torvalds #ifndef CONFIG_PPC32
2430db4c9641SStephen Smalley 	if ((prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
2431db4c9641SStephen Smalley 		rc = 0;
2432db4c9641SStephen Smalley 		if (vma->vm_start >= vma->vm_mm->start_brk &&
2433db4c9641SStephen Smalley 		    vma->vm_end <= vma->vm_mm->brk) {
2434db4c9641SStephen Smalley 			rc = task_has_perm(current, current,
2435db4c9641SStephen Smalley 					   PROCESS__EXECHEAP);
2436db4c9641SStephen Smalley 		} else if (!vma->vm_file &&
24376b992197SLorenzo Hernandez García-Hierro 			   vma->vm_start <= vma->vm_mm->start_stack &&
24386b992197SLorenzo Hernandez García-Hierro 			   vma->vm_end >= vma->vm_mm->start_stack) {
24396b992197SLorenzo Hernandez García-Hierro 			rc = task_has_perm(current, current, PROCESS__EXECSTACK);
2440db4c9641SStephen Smalley 		} else if (vma->vm_file && vma->anon_vma) {
2441db4c9641SStephen Smalley 			/*
2442db4c9641SStephen Smalley 			 * We are making executable a file mapping that has
2443db4c9641SStephen Smalley 			 * had some COW done. Since pages might have been
2444db4c9641SStephen Smalley 			 * written, check ability to execute the possibly
2445db4c9641SStephen Smalley 			 * modified content.  This typically should only
2446db4c9641SStephen Smalley 			 * occur for text relocations.
2447db4c9641SStephen Smalley 			 */
2448db4c9641SStephen Smalley 			rc = file_has_perm(current, vma->vm_file,
2449db4c9641SStephen Smalley 					   FILE__EXECMOD);
2450db4c9641SStephen Smalley 		}
24516b992197SLorenzo Hernandez García-Hierro 		if (rc)
24526b992197SLorenzo Hernandez García-Hierro 			return rc;
24536b992197SLorenzo Hernandez García-Hierro 	}
24541da177e4SLinus Torvalds #endif
24551da177e4SLinus Torvalds 
24561da177e4SLinus Torvalds 	return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
24571da177e4SLinus Torvalds }
24581da177e4SLinus Torvalds 
24591da177e4SLinus Torvalds static int selinux_file_lock(struct file *file, unsigned int cmd)
24601da177e4SLinus Torvalds {
24611da177e4SLinus Torvalds 	return file_has_perm(current, file, FILE__LOCK);
24621da177e4SLinus Torvalds }
24631da177e4SLinus Torvalds 
24641da177e4SLinus Torvalds static int selinux_file_fcntl(struct file *file, unsigned int cmd,
24651da177e4SLinus Torvalds 			      unsigned long arg)
24661da177e4SLinus Torvalds {
24671da177e4SLinus Torvalds 	int err = 0;
24681da177e4SLinus Torvalds 
24691da177e4SLinus Torvalds 	switch (cmd) {
24701da177e4SLinus Torvalds 	        case F_SETFL:
24711da177e4SLinus Torvalds 			if (!file->f_dentry || !file->f_dentry->d_inode) {
24721da177e4SLinus Torvalds 				err = -EINVAL;
24731da177e4SLinus Torvalds 				break;
24741da177e4SLinus Torvalds 			}
24751da177e4SLinus Torvalds 
24761da177e4SLinus Torvalds 			if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
24771da177e4SLinus Torvalds 				err = file_has_perm(current, file,FILE__WRITE);
24781da177e4SLinus Torvalds 				break;
24791da177e4SLinus Torvalds 			}
24801da177e4SLinus Torvalds 			/* fall through */
24811da177e4SLinus Torvalds 	        case F_SETOWN:
24821da177e4SLinus Torvalds 	        case F_SETSIG:
24831da177e4SLinus Torvalds 	        case F_GETFL:
24841da177e4SLinus Torvalds 	        case F_GETOWN:
24851da177e4SLinus Torvalds 	        case F_GETSIG:
24861da177e4SLinus Torvalds 			/* Just check FD__USE permission */
24871da177e4SLinus Torvalds 			err = file_has_perm(current, file, 0);
24881da177e4SLinus Torvalds 			break;
24891da177e4SLinus Torvalds 		case F_GETLK:
24901da177e4SLinus Torvalds 		case F_SETLK:
24911da177e4SLinus Torvalds 	        case F_SETLKW:
24921da177e4SLinus Torvalds #if BITS_PER_LONG == 32
24931da177e4SLinus Torvalds 	        case F_GETLK64:
24941da177e4SLinus Torvalds 		case F_SETLK64:
24951da177e4SLinus Torvalds 	        case F_SETLKW64:
24961da177e4SLinus Torvalds #endif
24971da177e4SLinus Torvalds 			if (!file->f_dentry || !file->f_dentry->d_inode) {
24981da177e4SLinus Torvalds 				err = -EINVAL;
24991da177e4SLinus Torvalds 				break;
25001da177e4SLinus Torvalds 			}
25011da177e4SLinus Torvalds 			err = file_has_perm(current, file, FILE__LOCK);
25021da177e4SLinus Torvalds 			break;
25031da177e4SLinus Torvalds 	}
25041da177e4SLinus Torvalds 
25051da177e4SLinus Torvalds 	return err;
25061da177e4SLinus Torvalds }
25071da177e4SLinus Torvalds 
25081da177e4SLinus Torvalds static int selinux_file_set_fowner(struct file *file)
25091da177e4SLinus Torvalds {
25101da177e4SLinus Torvalds 	struct task_security_struct *tsec;
25111da177e4SLinus Torvalds 	struct file_security_struct *fsec;
25121da177e4SLinus Torvalds 
25131da177e4SLinus Torvalds 	tsec = current->security;
25141da177e4SLinus Torvalds 	fsec = file->f_security;
25151da177e4SLinus Torvalds 	fsec->fown_sid = tsec->sid;
25161da177e4SLinus Torvalds 
25171da177e4SLinus Torvalds 	return 0;
25181da177e4SLinus Torvalds }
25191da177e4SLinus Torvalds 
25201da177e4SLinus Torvalds static int selinux_file_send_sigiotask(struct task_struct *tsk,
25211da177e4SLinus Torvalds 				       struct fown_struct *fown, int signum)
25221da177e4SLinus Torvalds {
25231da177e4SLinus Torvalds         struct file *file;
25241da177e4SLinus Torvalds 	u32 perm;
25251da177e4SLinus Torvalds 	struct task_security_struct *tsec;
25261da177e4SLinus Torvalds 	struct file_security_struct *fsec;
25271da177e4SLinus Torvalds 
25281da177e4SLinus Torvalds 	/* struct fown_struct is never outside the context of a struct file */
25291da177e4SLinus Torvalds         file = (struct file *)((long)fown - offsetof(struct file,f_owner));
25301da177e4SLinus Torvalds 
25311da177e4SLinus Torvalds 	tsec = tsk->security;
25321da177e4SLinus Torvalds 	fsec = file->f_security;
25331da177e4SLinus Torvalds 
25341da177e4SLinus Torvalds 	if (!signum)
25351da177e4SLinus Torvalds 		perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
25361da177e4SLinus Torvalds 	else
25371da177e4SLinus Torvalds 		perm = signal_to_av(signum);
25381da177e4SLinus Torvalds 
25391da177e4SLinus Torvalds 	return avc_has_perm(fsec->fown_sid, tsec->sid,
25401da177e4SLinus Torvalds 			    SECCLASS_PROCESS, perm, NULL);
25411da177e4SLinus Torvalds }
25421da177e4SLinus Torvalds 
25431da177e4SLinus Torvalds static int selinux_file_receive(struct file *file)
25441da177e4SLinus Torvalds {
25451da177e4SLinus Torvalds 	return file_has_perm(current, file, file_to_av(file));
25461da177e4SLinus Torvalds }
25471da177e4SLinus Torvalds 
25481da177e4SLinus Torvalds /* task security operations */
25491da177e4SLinus Torvalds 
25501da177e4SLinus Torvalds static int selinux_task_create(unsigned long clone_flags)
25511da177e4SLinus Torvalds {
25521da177e4SLinus Torvalds 	int rc;
25531da177e4SLinus Torvalds 
25541da177e4SLinus Torvalds 	rc = secondary_ops->task_create(clone_flags);
25551da177e4SLinus Torvalds 	if (rc)
25561da177e4SLinus Torvalds 		return rc;
25571da177e4SLinus Torvalds 
25581da177e4SLinus Torvalds 	return task_has_perm(current, current, PROCESS__FORK);
25591da177e4SLinus Torvalds }
25601da177e4SLinus Torvalds 
25611da177e4SLinus Torvalds static int selinux_task_alloc_security(struct task_struct *tsk)
25621da177e4SLinus Torvalds {
25631da177e4SLinus Torvalds 	struct task_security_struct *tsec1, *tsec2;
25641da177e4SLinus Torvalds 	int rc;
25651da177e4SLinus Torvalds 
25661da177e4SLinus Torvalds 	tsec1 = current->security;
25671da177e4SLinus Torvalds 
25681da177e4SLinus Torvalds 	rc = task_alloc_security(tsk);
25691da177e4SLinus Torvalds 	if (rc)
25701da177e4SLinus Torvalds 		return rc;
25711da177e4SLinus Torvalds 	tsec2 = tsk->security;
25721da177e4SLinus Torvalds 
25731da177e4SLinus Torvalds 	tsec2->osid = tsec1->osid;
25741da177e4SLinus Torvalds 	tsec2->sid = tsec1->sid;
25751da177e4SLinus Torvalds 
25761da177e4SLinus Torvalds 	/* Retain the exec and create SIDs across fork */
25771da177e4SLinus Torvalds 	tsec2->exec_sid = tsec1->exec_sid;
25781da177e4SLinus Torvalds 	tsec2->create_sid = tsec1->create_sid;
25791da177e4SLinus Torvalds 
25801da177e4SLinus Torvalds 	/* Retain ptracer SID across fork, if any.
25811da177e4SLinus Torvalds 	   This will be reset by the ptrace hook upon any
25821da177e4SLinus Torvalds 	   subsequent ptrace_attach operations. */
25831da177e4SLinus Torvalds 	tsec2->ptrace_sid = tsec1->ptrace_sid;
25841da177e4SLinus Torvalds 
25851da177e4SLinus Torvalds 	return 0;
25861da177e4SLinus Torvalds }
25871da177e4SLinus Torvalds 
25881da177e4SLinus Torvalds static void selinux_task_free_security(struct task_struct *tsk)
25891da177e4SLinus Torvalds {
25901da177e4SLinus Torvalds 	task_free_security(tsk);
25911da177e4SLinus Torvalds }
25921da177e4SLinus Torvalds 
25931da177e4SLinus Torvalds static int selinux_task_setuid(uid_t id0, uid_t id1, uid_t id2, int flags)
25941da177e4SLinus Torvalds {
25951da177e4SLinus Torvalds 	/* Since setuid only affects the current process, and
25961da177e4SLinus Torvalds 	   since the SELinux controls are not based on the Linux
25971da177e4SLinus Torvalds 	   identity attributes, SELinux does not need to control
25981da177e4SLinus Torvalds 	   this operation.  However, SELinux does control the use
25991da177e4SLinus Torvalds 	   of the CAP_SETUID and CAP_SETGID capabilities using the
26001da177e4SLinus Torvalds 	   capable hook. */
26011da177e4SLinus Torvalds 	return 0;
26021da177e4SLinus Torvalds }
26031da177e4SLinus Torvalds 
26041da177e4SLinus Torvalds static int selinux_task_post_setuid(uid_t id0, uid_t id1, uid_t id2, int flags)
26051da177e4SLinus Torvalds {
26061da177e4SLinus Torvalds 	return secondary_ops->task_post_setuid(id0,id1,id2,flags);
26071da177e4SLinus Torvalds }
26081da177e4SLinus Torvalds 
26091da177e4SLinus Torvalds static int selinux_task_setgid(gid_t id0, gid_t id1, gid_t id2, int flags)
26101da177e4SLinus Torvalds {
26111da177e4SLinus Torvalds 	/* See the comment for setuid above. */
26121da177e4SLinus Torvalds 	return 0;
26131da177e4SLinus Torvalds }
26141da177e4SLinus Torvalds 
26151da177e4SLinus Torvalds static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
26161da177e4SLinus Torvalds {
26171da177e4SLinus Torvalds 	return task_has_perm(current, p, PROCESS__SETPGID);
26181da177e4SLinus Torvalds }
26191da177e4SLinus Torvalds 
26201da177e4SLinus Torvalds static int selinux_task_getpgid(struct task_struct *p)
26211da177e4SLinus Torvalds {
26221da177e4SLinus Torvalds 	return task_has_perm(current, p, PROCESS__GETPGID);
26231da177e4SLinus Torvalds }
26241da177e4SLinus Torvalds 
26251da177e4SLinus Torvalds static int selinux_task_getsid(struct task_struct *p)
26261da177e4SLinus Torvalds {
26271da177e4SLinus Torvalds 	return task_has_perm(current, p, PROCESS__GETSESSION);
26281da177e4SLinus Torvalds }
26291da177e4SLinus Torvalds 
26301da177e4SLinus Torvalds static int selinux_task_setgroups(struct group_info *group_info)
26311da177e4SLinus Torvalds {
26321da177e4SLinus Torvalds 	/* See the comment for setuid above. */
26331da177e4SLinus Torvalds 	return 0;
26341da177e4SLinus Torvalds }
26351da177e4SLinus Torvalds 
26361da177e4SLinus Torvalds static int selinux_task_setnice(struct task_struct *p, int nice)
26371da177e4SLinus Torvalds {
26381da177e4SLinus Torvalds 	int rc;
26391da177e4SLinus Torvalds 
26401da177e4SLinus Torvalds 	rc = secondary_ops->task_setnice(p, nice);
26411da177e4SLinus Torvalds 	if (rc)
26421da177e4SLinus Torvalds 		return rc;
26431da177e4SLinus Torvalds 
26441da177e4SLinus Torvalds 	return task_has_perm(current,p, PROCESS__SETSCHED);
26451da177e4SLinus Torvalds }
26461da177e4SLinus Torvalds 
26471da177e4SLinus Torvalds static int selinux_task_setrlimit(unsigned int resource, struct rlimit *new_rlim)
26481da177e4SLinus Torvalds {
26491da177e4SLinus Torvalds 	struct rlimit *old_rlim = current->signal->rlim + resource;
26501da177e4SLinus Torvalds 	int rc;
26511da177e4SLinus Torvalds 
26521da177e4SLinus Torvalds 	rc = secondary_ops->task_setrlimit(resource, new_rlim);
26531da177e4SLinus Torvalds 	if (rc)
26541da177e4SLinus Torvalds 		return rc;
26551da177e4SLinus Torvalds 
26561da177e4SLinus Torvalds 	/* Control the ability to change the hard limit (whether
26571da177e4SLinus Torvalds 	   lowering or raising it), so that the hard limit can
26581da177e4SLinus Torvalds 	   later be used as a safe reset point for the soft limit
26591da177e4SLinus Torvalds 	   upon context transitions. See selinux_bprm_apply_creds. */
26601da177e4SLinus Torvalds 	if (old_rlim->rlim_max != new_rlim->rlim_max)
26611da177e4SLinus Torvalds 		return task_has_perm(current, current, PROCESS__SETRLIMIT);
26621da177e4SLinus Torvalds 
26631da177e4SLinus Torvalds 	return 0;
26641da177e4SLinus Torvalds }
26651da177e4SLinus Torvalds 
26661da177e4SLinus Torvalds static int selinux_task_setscheduler(struct task_struct *p, int policy, struct sched_param *lp)
26671da177e4SLinus Torvalds {
26681da177e4SLinus Torvalds 	return task_has_perm(current, p, PROCESS__SETSCHED);
26691da177e4SLinus Torvalds }
26701da177e4SLinus Torvalds 
26711da177e4SLinus Torvalds static int selinux_task_getscheduler(struct task_struct *p)
26721da177e4SLinus Torvalds {
26731da177e4SLinus Torvalds 	return task_has_perm(current, p, PROCESS__GETSCHED);
26741da177e4SLinus Torvalds }
26751da177e4SLinus Torvalds 
26761da177e4SLinus Torvalds static int selinux_task_kill(struct task_struct *p, struct siginfo *info, int sig)
26771da177e4SLinus Torvalds {
26781da177e4SLinus Torvalds 	u32 perm;
26791da177e4SLinus Torvalds 	int rc;
26801da177e4SLinus Torvalds 
26811da177e4SLinus Torvalds 	rc = secondary_ops->task_kill(p, info, sig);
26821da177e4SLinus Torvalds 	if (rc)
26831da177e4SLinus Torvalds 		return rc;
26841da177e4SLinus Torvalds 
2685621d3121SOleg Nesterov 	if (info != SEND_SIG_NOINFO && (is_si_special(info) || SI_FROMKERNEL(info)))
26861da177e4SLinus Torvalds 		return 0;
26871da177e4SLinus Torvalds 
26881da177e4SLinus Torvalds 	if (!sig)
26891da177e4SLinus Torvalds 		perm = PROCESS__SIGNULL; /* null signal; existence test */
26901da177e4SLinus Torvalds 	else
26911da177e4SLinus Torvalds 		perm = signal_to_av(sig);
26921da177e4SLinus Torvalds 
26931da177e4SLinus Torvalds 	return task_has_perm(current, p, perm);
26941da177e4SLinus Torvalds }
26951da177e4SLinus Torvalds 
26961da177e4SLinus Torvalds static int selinux_task_prctl(int option,
26971da177e4SLinus Torvalds 			      unsigned long arg2,
26981da177e4SLinus Torvalds 			      unsigned long arg3,
26991da177e4SLinus Torvalds 			      unsigned long arg4,
27001da177e4SLinus Torvalds 			      unsigned long arg5)
27011da177e4SLinus Torvalds {
27021da177e4SLinus Torvalds 	/* The current prctl operations do not appear to require
27031da177e4SLinus Torvalds 	   any SELinux controls since they merely observe or modify
27041da177e4SLinus Torvalds 	   the state of the current process. */
27051da177e4SLinus Torvalds 	return 0;
27061da177e4SLinus Torvalds }
27071da177e4SLinus Torvalds 
27081da177e4SLinus Torvalds static int selinux_task_wait(struct task_struct *p)
27091da177e4SLinus Torvalds {
27101da177e4SLinus Torvalds 	u32 perm;
27111da177e4SLinus Torvalds 
27121da177e4SLinus Torvalds 	perm = signal_to_av(p->exit_signal);
27131da177e4SLinus Torvalds 
27141da177e4SLinus Torvalds 	return task_has_perm(p, current, perm);
27151da177e4SLinus Torvalds }
27161da177e4SLinus Torvalds 
27171da177e4SLinus Torvalds static void selinux_task_reparent_to_init(struct task_struct *p)
27181da177e4SLinus Torvalds {
27191da177e4SLinus Torvalds   	struct task_security_struct *tsec;
27201da177e4SLinus Torvalds 
27211da177e4SLinus Torvalds 	secondary_ops->task_reparent_to_init(p);
27221da177e4SLinus Torvalds 
27231da177e4SLinus Torvalds 	tsec = p->security;
27241da177e4SLinus Torvalds 	tsec->osid = tsec->sid;
27251da177e4SLinus Torvalds 	tsec->sid = SECINITSID_KERNEL;
27261da177e4SLinus Torvalds 	return;
27271da177e4SLinus Torvalds }
27281da177e4SLinus Torvalds 
27291da177e4SLinus Torvalds static void selinux_task_to_inode(struct task_struct *p,
27301da177e4SLinus Torvalds 				  struct inode *inode)
27311da177e4SLinus Torvalds {
27321da177e4SLinus Torvalds 	struct task_security_struct *tsec = p->security;
27331da177e4SLinus Torvalds 	struct inode_security_struct *isec = inode->i_security;
27341da177e4SLinus Torvalds 
27351da177e4SLinus Torvalds 	isec->sid = tsec->sid;
27361da177e4SLinus Torvalds 	isec->initialized = 1;
27371da177e4SLinus Torvalds 	return;
27381da177e4SLinus Torvalds }
27391da177e4SLinus Torvalds 
27401da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */
27411da177e4SLinus Torvalds static int selinux_parse_skb_ipv4(struct sk_buff *skb, struct avc_audit_data *ad)
27421da177e4SLinus Torvalds {
27431da177e4SLinus Torvalds 	int offset, ihlen, ret = -EINVAL;
27441da177e4SLinus Torvalds 	struct iphdr _iph, *ih;
27451da177e4SLinus Torvalds 
27461da177e4SLinus Torvalds 	offset = skb->nh.raw - skb->data;
27471da177e4SLinus Torvalds 	ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
27481da177e4SLinus Torvalds 	if (ih == NULL)
27491da177e4SLinus Torvalds 		goto out;
27501da177e4SLinus Torvalds 
27511da177e4SLinus Torvalds 	ihlen = ih->ihl * 4;
27521da177e4SLinus Torvalds 	if (ihlen < sizeof(_iph))
27531da177e4SLinus Torvalds 		goto out;
27541da177e4SLinus Torvalds 
27551da177e4SLinus Torvalds 	ad->u.net.v4info.saddr = ih->saddr;
27561da177e4SLinus Torvalds 	ad->u.net.v4info.daddr = ih->daddr;
27571da177e4SLinus Torvalds 	ret = 0;
27581da177e4SLinus Torvalds 
27591da177e4SLinus Torvalds 	switch (ih->protocol) {
27601da177e4SLinus Torvalds         case IPPROTO_TCP: {
27611da177e4SLinus Torvalds         	struct tcphdr _tcph, *th;
27621da177e4SLinus Torvalds 
27631da177e4SLinus Torvalds         	if (ntohs(ih->frag_off) & IP_OFFSET)
27641da177e4SLinus Torvalds         		break;
27651da177e4SLinus Torvalds 
27661da177e4SLinus Torvalds 		offset += ihlen;
27671da177e4SLinus Torvalds 		th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
27681da177e4SLinus Torvalds 		if (th == NULL)
27691da177e4SLinus Torvalds 			break;
27701da177e4SLinus Torvalds 
27711da177e4SLinus Torvalds 		ad->u.net.sport = th->source;
27721da177e4SLinus Torvalds 		ad->u.net.dport = th->dest;
27731da177e4SLinus Torvalds 		break;
27741da177e4SLinus Torvalds         }
27751da177e4SLinus Torvalds 
27761da177e4SLinus Torvalds         case IPPROTO_UDP: {
27771da177e4SLinus Torvalds         	struct udphdr _udph, *uh;
27781da177e4SLinus Torvalds 
27791da177e4SLinus Torvalds         	if (ntohs(ih->frag_off) & IP_OFFSET)
27801da177e4SLinus Torvalds         		break;
27811da177e4SLinus Torvalds 
27821da177e4SLinus Torvalds 		offset += ihlen;
27831da177e4SLinus Torvalds         	uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
27841da177e4SLinus Torvalds 		if (uh == NULL)
27851da177e4SLinus Torvalds 			break;
27861da177e4SLinus Torvalds 
27871da177e4SLinus Torvalds         	ad->u.net.sport = uh->source;
27881da177e4SLinus Torvalds         	ad->u.net.dport = uh->dest;
27891da177e4SLinus Torvalds         	break;
27901da177e4SLinus Torvalds         }
27911da177e4SLinus Torvalds 
27921da177e4SLinus Torvalds         default:
27931da177e4SLinus Torvalds         	break;
27941da177e4SLinus Torvalds         }
27951da177e4SLinus Torvalds out:
27961da177e4SLinus Torvalds 	return ret;
27971da177e4SLinus Torvalds }
27981da177e4SLinus Torvalds 
27991da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
28001da177e4SLinus Torvalds 
28011da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */
28021da177e4SLinus Torvalds static int selinux_parse_skb_ipv6(struct sk_buff *skb, struct avc_audit_data *ad)
28031da177e4SLinus Torvalds {
28041da177e4SLinus Torvalds 	u8 nexthdr;
28051da177e4SLinus Torvalds 	int ret = -EINVAL, offset;
28061da177e4SLinus Torvalds 	struct ipv6hdr _ipv6h, *ip6;
28071da177e4SLinus Torvalds 
28081da177e4SLinus Torvalds 	offset = skb->nh.raw - skb->data;
28091da177e4SLinus Torvalds 	ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
28101da177e4SLinus Torvalds 	if (ip6 == NULL)
28111da177e4SLinus Torvalds 		goto out;
28121da177e4SLinus Torvalds 
28131da177e4SLinus Torvalds 	ipv6_addr_copy(&ad->u.net.v6info.saddr, &ip6->saddr);
28141da177e4SLinus Torvalds 	ipv6_addr_copy(&ad->u.net.v6info.daddr, &ip6->daddr);
28151da177e4SLinus Torvalds 	ret = 0;
28161da177e4SLinus Torvalds 
28171da177e4SLinus Torvalds 	nexthdr = ip6->nexthdr;
28181da177e4SLinus Torvalds 	offset += sizeof(_ipv6h);
28190d3d077cSHerbert Xu 	offset = ipv6_skip_exthdr(skb, offset, &nexthdr);
28201da177e4SLinus Torvalds 	if (offset < 0)
28211da177e4SLinus Torvalds 		goto out;
28221da177e4SLinus Torvalds 
28231da177e4SLinus Torvalds 	switch (nexthdr) {
28241da177e4SLinus Torvalds 	case IPPROTO_TCP: {
28251da177e4SLinus Torvalds         	struct tcphdr _tcph, *th;
28261da177e4SLinus Torvalds 
28271da177e4SLinus Torvalds 		th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
28281da177e4SLinus Torvalds 		if (th == NULL)
28291da177e4SLinus Torvalds 			break;
28301da177e4SLinus Torvalds 
28311da177e4SLinus Torvalds 		ad->u.net.sport = th->source;
28321da177e4SLinus Torvalds 		ad->u.net.dport = th->dest;
28331da177e4SLinus Torvalds 		break;
28341da177e4SLinus Torvalds 	}
28351da177e4SLinus Torvalds 
28361da177e4SLinus Torvalds 	case IPPROTO_UDP: {
28371da177e4SLinus Torvalds 		struct udphdr _udph, *uh;
28381da177e4SLinus Torvalds 
28391da177e4SLinus Torvalds 		uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
28401da177e4SLinus Torvalds 		if (uh == NULL)
28411da177e4SLinus Torvalds 			break;
28421da177e4SLinus Torvalds 
28431da177e4SLinus Torvalds 		ad->u.net.sport = uh->source;
28441da177e4SLinus Torvalds 		ad->u.net.dport = uh->dest;
28451da177e4SLinus Torvalds 		break;
28461da177e4SLinus Torvalds 	}
28471da177e4SLinus Torvalds 
28481da177e4SLinus Torvalds 	/* includes fragments */
28491da177e4SLinus Torvalds 	default:
28501da177e4SLinus Torvalds 		break;
28511da177e4SLinus Torvalds 	}
28521da177e4SLinus Torvalds out:
28531da177e4SLinus Torvalds 	return ret;
28541da177e4SLinus Torvalds }
28551da177e4SLinus Torvalds 
28561da177e4SLinus Torvalds #endif /* IPV6 */
28571da177e4SLinus Torvalds 
28581da177e4SLinus Torvalds static int selinux_parse_skb(struct sk_buff *skb, struct avc_audit_data *ad,
28591da177e4SLinus Torvalds 			     char **addrp, int *len, int src)
28601da177e4SLinus Torvalds {
28611da177e4SLinus Torvalds 	int ret = 0;
28621da177e4SLinus Torvalds 
28631da177e4SLinus Torvalds 	switch (ad->u.net.family) {
28641da177e4SLinus Torvalds 	case PF_INET:
28651da177e4SLinus Torvalds 		ret = selinux_parse_skb_ipv4(skb, ad);
28661da177e4SLinus Torvalds 		if (ret || !addrp)
28671da177e4SLinus Torvalds 			break;
28681da177e4SLinus Torvalds 		*len = 4;
28691da177e4SLinus Torvalds 		*addrp = (char *)(src ? &ad->u.net.v4info.saddr :
28701da177e4SLinus Torvalds 					&ad->u.net.v4info.daddr);
28711da177e4SLinus Torvalds 		break;
28721da177e4SLinus Torvalds 
28731da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
28741da177e4SLinus Torvalds 	case PF_INET6:
28751da177e4SLinus Torvalds 		ret = selinux_parse_skb_ipv6(skb, ad);
28761da177e4SLinus Torvalds 		if (ret || !addrp)
28771da177e4SLinus Torvalds 			break;
28781da177e4SLinus Torvalds 		*len = 16;
28791da177e4SLinus Torvalds 		*addrp = (char *)(src ? &ad->u.net.v6info.saddr :
28801da177e4SLinus Torvalds 					&ad->u.net.v6info.daddr);
28811da177e4SLinus Torvalds 		break;
28821da177e4SLinus Torvalds #endif	/* IPV6 */
28831da177e4SLinus Torvalds 	default:
28841da177e4SLinus Torvalds 		break;
28851da177e4SLinus Torvalds 	}
28861da177e4SLinus Torvalds 
28871da177e4SLinus Torvalds 	return ret;
28881da177e4SLinus Torvalds }
28891da177e4SLinus Torvalds 
28901da177e4SLinus Torvalds /* socket security operations */
28911da177e4SLinus Torvalds static int socket_has_perm(struct task_struct *task, struct socket *sock,
28921da177e4SLinus Torvalds 			   u32 perms)
28931da177e4SLinus Torvalds {
28941da177e4SLinus Torvalds 	struct inode_security_struct *isec;
28951da177e4SLinus Torvalds 	struct task_security_struct *tsec;
28961da177e4SLinus Torvalds 	struct avc_audit_data ad;
28971da177e4SLinus Torvalds 	int err = 0;
28981da177e4SLinus Torvalds 
28991da177e4SLinus Torvalds 	tsec = task->security;
29001da177e4SLinus Torvalds 	isec = SOCK_INODE(sock)->i_security;
29011da177e4SLinus Torvalds 
29021da177e4SLinus Torvalds 	if (isec->sid == SECINITSID_KERNEL)
29031da177e4SLinus Torvalds 		goto out;
29041da177e4SLinus Torvalds 
29051da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad,NET);
29061da177e4SLinus Torvalds 	ad.u.net.sk = sock->sk;
29071da177e4SLinus Torvalds 	err = avc_has_perm(tsec->sid, isec->sid, isec->sclass, perms, &ad);
29081da177e4SLinus Torvalds 
29091da177e4SLinus Torvalds out:
29101da177e4SLinus Torvalds 	return err;
29111da177e4SLinus Torvalds }
29121da177e4SLinus Torvalds 
29131da177e4SLinus Torvalds static int selinux_socket_create(int family, int type,
29141da177e4SLinus Torvalds 				 int protocol, int kern)
29151da177e4SLinus Torvalds {
29161da177e4SLinus Torvalds 	int err = 0;
29171da177e4SLinus Torvalds 	struct task_security_struct *tsec;
29181da177e4SLinus Torvalds 
29191da177e4SLinus Torvalds 	if (kern)
29201da177e4SLinus Torvalds 		goto out;
29211da177e4SLinus Torvalds 
29221da177e4SLinus Torvalds 	tsec = current->security;
29231da177e4SLinus Torvalds 	err = avc_has_perm(tsec->sid, tsec->sid,
29241da177e4SLinus Torvalds 			   socket_type_to_security_class(family, type,
29251da177e4SLinus Torvalds 			   protocol), SOCKET__CREATE, NULL);
29261da177e4SLinus Torvalds 
29271da177e4SLinus Torvalds out:
29281da177e4SLinus Torvalds 	return err;
29291da177e4SLinus Torvalds }
29301da177e4SLinus Torvalds 
29311da177e4SLinus Torvalds static void selinux_socket_post_create(struct socket *sock, int family,
29321da177e4SLinus Torvalds 				       int type, int protocol, int kern)
29331da177e4SLinus Torvalds {
29341da177e4SLinus Torvalds 	struct inode_security_struct *isec;
29351da177e4SLinus Torvalds 	struct task_security_struct *tsec;
29361da177e4SLinus Torvalds 
29371da177e4SLinus Torvalds 	isec = SOCK_INODE(sock)->i_security;
29381da177e4SLinus Torvalds 
29391da177e4SLinus Torvalds 	tsec = current->security;
29401da177e4SLinus Torvalds 	isec->sclass = socket_type_to_security_class(family, type, protocol);
29411da177e4SLinus Torvalds 	isec->sid = kern ? SECINITSID_KERNEL : tsec->sid;
29421da177e4SLinus Torvalds 	isec->initialized = 1;
29431da177e4SLinus Torvalds 
29441da177e4SLinus Torvalds 	return;
29451da177e4SLinus Torvalds }
29461da177e4SLinus Torvalds 
29471da177e4SLinus Torvalds /* Range of port numbers used to automatically bind.
29481da177e4SLinus Torvalds    Need to determine whether we should perform a name_bind
29491da177e4SLinus Torvalds    permission check between the socket and the port number. */
29501da177e4SLinus Torvalds #define ip_local_port_range_0 sysctl_local_port_range[0]
29511da177e4SLinus Torvalds #define ip_local_port_range_1 sysctl_local_port_range[1]
29521da177e4SLinus Torvalds 
29531da177e4SLinus Torvalds static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
29541da177e4SLinus Torvalds {
29551da177e4SLinus Torvalds 	u16 family;
29561da177e4SLinus Torvalds 	int err;
29571da177e4SLinus Torvalds 
29581da177e4SLinus Torvalds 	err = socket_has_perm(current, sock, SOCKET__BIND);
29591da177e4SLinus Torvalds 	if (err)
29601da177e4SLinus Torvalds 		goto out;
29611da177e4SLinus Torvalds 
29621da177e4SLinus Torvalds 	/*
29631da177e4SLinus Torvalds 	 * If PF_INET or PF_INET6, check name_bind permission for the port.
296413402580SJames Morris 	 * Multiple address binding for SCTP is not supported yet: we just
296513402580SJames Morris 	 * check the first address now.
29661da177e4SLinus Torvalds 	 */
29671da177e4SLinus Torvalds 	family = sock->sk->sk_family;
29681da177e4SLinus Torvalds 	if (family == PF_INET || family == PF_INET6) {
29691da177e4SLinus Torvalds 		char *addrp;
29701da177e4SLinus Torvalds 		struct inode_security_struct *isec;
29711da177e4SLinus Torvalds 		struct task_security_struct *tsec;
29721da177e4SLinus Torvalds 		struct avc_audit_data ad;
29731da177e4SLinus Torvalds 		struct sockaddr_in *addr4 = NULL;
29741da177e4SLinus Torvalds 		struct sockaddr_in6 *addr6 = NULL;
29751da177e4SLinus Torvalds 		unsigned short snum;
29761da177e4SLinus Torvalds 		struct sock *sk = sock->sk;
29771da177e4SLinus Torvalds 		u32 sid, node_perm, addrlen;
29781da177e4SLinus Torvalds 
29791da177e4SLinus Torvalds 		tsec = current->security;
29801da177e4SLinus Torvalds 		isec = SOCK_INODE(sock)->i_security;
29811da177e4SLinus Torvalds 
29821da177e4SLinus Torvalds 		if (family == PF_INET) {
29831da177e4SLinus Torvalds 			addr4 = (struct sockaddr_in *)address;
29841da177e4SLinus Torvalds 			snum = ntohs(addr4->sin_port);
29851da177e4SLinus Torvalds 			addrlen = sizeof(addr4->sin_addr.s_addr);
29861da177e4SLinus Torvalds 			addrp = (char *)&addr4->sin_addr.s_addr;
29871da177e4SLinus Torvalds 		} else {
29881da177e4SLinus Torvalds 			addr6 = (struct sockaddr_in6 *)address;
29891da177e4SLinus Torvalds 			snum = ntohs(addr6->sin6_port);
29901da177e4SLinus Torvalds 			addrlen = sizeof(addr6->sin6_addr.s6_addr);
29911da177e4SLinus Torvalds 			addrp = (char *)&addr6->sin6_addr.s6_addr;
29921da177e4SLinus Torvalds 		}
29931da177e4SLinus Torvalds 
29941da177e4SLinus Torvalds 		if (snum&&(snum < max(PROT_SOCK,ip_local_port_range_0) ||
29951da177e4SLinus Torvalds 			   snum > ip_local_port_range_1)) {
29961da177e4SLinus Torvalds 			err = security_port_sid(sk->sk_family, sk->sk_type,
29971da177e4SLinus Torvalds 						sk->sk_protocol, snum, &sid);
29981da177e4SLinus Torvalds 			if (err)
29991da177e4SLinus Torvalds 				goto out;
30001da177e4SLinus Torvalds 			AVC_AUDIT_DATA_INIT(&ad,NET);
30011da177e4SLinus Torvalds 			ad.u.net.sport = htons(snum);
30021da177e4SLinus Torvalds 			ad.u.net.family = family;
30031da177e4SLinus Torvalds 			err = avc_has_perm(isec->sid, sid,
30041da177e4SLinus Torvalds 					   isec->sclass,
30051da177e4SLinus Torvalds 					   SOCKET__NAME_BIND, &ad);
30061da177e4SLinus Torvalds 			if (err)
30071da177e4SLinus Torvalds 				goto out;
30081da177e4SLinus Torvalds 		}
30091da177e4SLinus Torvalds 
301013402580SJames Morris 		switch(isec->sclass) {
301113402580SJames Morris 		case SECCLASS_TCP_SOCKET:
30121da177e4SLinus Torvalds 			node_perm = TCP_SOCKET__NODE_BIND;
30131da177e4SLinus Torvalds 			break;
30141da177e4SLinus Torvalds 
301513402580SJames Morris 		case SECCLASS_UDP_SOCKET:
30161da177e4SLinus Torvalds 			node_perm = UDP_SOCKET__NODE_BIND;
30171da177e4SLinus Torvalds 			break;
30181da177e4SLinus Torvalds 
30191da177e4SLinus Torvalds 		default:
30201da177e4SLinus Torvalds 			node_perm = RAWIP_SOCKET__NODE_BIND;
30211da177e4SLinus Torvalds 			break;
30221da177e4SLinus Torvalds 		}
30231da177e4SLinus Torvalds 
30241da177e4SLinus Torvalds 		err = security_node_sid(family, addrp, addrlen, &sid);
30251da177e4SLinus Torvalds 		if (err)
30261da177e4SLinus Torvalds 			goto out;
30271da177e4SLinus Torvalds 
30281da177e4SLinus Torvalds 		AVC_AUDIT_DATA_INIT(&ad,NET);
30291da177e4SLinus Torvalds 		ad.u.net.sport = htons(snum);
30301da177e4SLinus Torvalds 		ad.u.net.family = family;
30311da177e4SLinus Torvalds 
30321da177e4SLinus Torvalds 		if (family == PF_INET)
30331da177e4SLinus Torvalds 			ad.u.net.v4info.saddr = addr4->sin_addr.s_addr;
30341da177e4SLinus Torvalds 		else
30351da177e4SLinus Torvalds 			ipv6_addr_copy(&ad.u.net.v6info.saddr, &addr6->sin6_addr);
30361da177e4SLinus Torvalds 
30371da177e4SLinus Torvalds 		err = avc_has_perm(isec->sid, sid,
30381da177e4SLinus Torvalds 		                   isec->sclass, node_perm, &ad);
30391da177e4SLinus Torvalds 		if (err)
30401da177e4SLinus Torvalds 			goto out;
30411da177e4SLinus Torvalds 	}
30421da177e4SLinus Torvalds out:
30431da177e4SLinus Torvalds 	return err;
30441da177e4SLinus Torvalds }
30451da177e4SLinus Torvalds 
30461da177e4SLinus Torvalds static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
30471da177e4SLinus Torvalds {
30481da177e4SLinus Torvalds 	struct inode_security_struct *isec;
30491da177e4SLinus Torvalds 	int err;
30501da177e4SLinus Torvalds 
30511da177e4SLinus Torvalds 	err = socket_has_perm(current, sock, SOCKET__CONNECT);
30521da177e4SLinus Torvalds 	if (err)
30531da177e4SLinus Torvalds 		return err;
30541da177e4SLinus Torvalds 
30551da177e4SLinus Torvalds 	/*
30561da177e4SLinus Torvalds 	 * If a TCP socket, check name_connect permission for the port.
30571da177e4SLinus Torvalds 	 */
30581da177e4SLinus Torvalds 	isec = SOCK_INODE(sock)->i_security;
30591da177e4SLinus Torvalds 	if (isec->sclass == SECCLASS_TCP_SOCKET) {
30601da177e4SLinus Torvalds 		struct sock *sk = sock->sk;
30611da177e4SLinus Torvalds 		struct avc_audit_data ad;
30621da177e4SLinus Torvalds 		struct sockaddr_in *addr4 = NULL;
30631da177e4SLinus Torvalds 		struct sockaddr_in6 *addr6 = NULL;
30641da177e4SLinus Torvalds 		unsigned short snum;
30651da177e4SLinus Torvalds 		u32 sid;
30661da177e4SLinus Torvalds 
30671da177e4SLinus Torvalds 		if (sk->sk_family == PF_INET) {
30681da177e4SLinus Torvalds 			addr4 = (struct sockaddr_in *)address;
3069911656f8SStephen Smalley 			if (addrlen < sizeof(struct sockaddr_in))
30701da177e4SLinus Torvalds 				return -EINVAL;
30711da177e4SLinus Torvalds 			snum = ntohs(addr4->sin_port);
30721da177e4SLinus Torvalds 		} else {
30731da177e4SLinus Torvalds 			addr6 = (struct sockaddr_in6 *)address;
3074911656f8SStephen Smalley 			if (addrlen < SIN6_LEN_RFC2133)
30751da177e4SLinus Torvalds 				return -EINVAL;
30761da177e4SLinus Torvalds 			snum = ntohs(addr6->sin6_port);
30771da177e4SLinus Torvalds 		}
30781da177e4SLinus Torvalds 
30791da177e4SLinus Torvalds 		err = security_port_sid(sk->sk_family, sk->sk_type,
30801da177e4SLinus Torvalds 					sk->sk_protocol, snum, &sid);
30811da177e4SLinus Torvalds 		if (err)
30821da177e4SLinus Torvalds 			goto out;
30831da177e4SLinus Torvalds 
30841da177e4SLinus Torvalds 		AVC_AUDIT_DATA_INIT(&ad,NET);
30851da177e4SLinus Torvalds 		ad.u.net.dport = htons(snum);
30861da177e4SLinus Torvalds 		ad.u.net.family = sk->sk_family;
30871da177e4SLinus Torvalds 		err = avc_has_perm(isec->sid, sid, isec->sclass,
30881da177e4SLinus Torvalds 				   TCP_SOCKET__NAME_CONNECT, &ad);
30891da177e4SLinus Torvalds 		if (err)
30901da177e4SLinus Torvalds 			goto out;
30911da177e4SLinus Torvalds 	}
30921da177e4SLinus Torvalds 
30931da177e4SLinus Torvalds out:
30941da177e4SLinus Torvalds 	return err;
30951da177e4SLinus Torvalds }
30961da177e4SLinus Torvalds 
30971da177e4SLinus Torvalds static int selinux_socket_listen(struct socket *sock, int backlog)
30981da177e4SLinus Torvalds {
30991da177e4SLinus Torvalds 	return socket_has_perm(current, sock, SOCKET__LISTEN);
31001da177e4SLinus Torvalds }
31011da177e4SLinus Torvalds 
31021da177e4SLinus Torvalds static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
31031da177e4SLinus Torvalds {
31041da177e4SLinus Torvalds 	int err;
31051da177e4SLinus Torvalds 	struct inode_security_struct *isec;
31061da177e4SLinus Torvalds 	struct inode_security_struct *newisec;
31071da177e4SLinus Torvalds 
31081da177e4SLinus Torvalds 	err = socket_has_perm(current, sock, SOCKET__ACCEPT);
31091da177e4SLinus Torvalds 	if (err)
31101da177e4SLinus Torvalds 		return err;
31111da177e4SLinus Torvalds 
31121da177e4SLinus Torvalds 	newisec = SOCK_INODE(newsock)->i_security;
31131da177e4SLinus Torvalds 
31141da177e4SLinus Torvalds 	isec = SOCK_INODE(sock)->i_security;
31151da177e4SLinus Torvalds 	newisec->sclass = isec->sclass;
31161da177e4SLinus Torvalds 	newisec->sid = isec->sid;
31171da177e4SLinus Torvalds 	newisec->initialized = 1;
31181da177e4SLinus Torvalds 
31191da177e4SLinus Torvalds 	return 0;
31201da177e4SLinus Torvalds }
31211da177e4SLinus Torvalds 
31221da177e4SLinus Torvalds static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
31231da177e4SLinus Torvalds  				  int size)
31241da177e4SLinus Torvalds {
31251da177e4SLinus Torvalds 	return socket_has_perm(current, sock, SOCKET__WRITE);
31261da177e4SLinus Torvalds }
31271da177e4SLinus Torvalds 
31281da177e4SLinus Torvalds static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
31291da177e4SLinus Torvalds 				  int size, int flags)
31301da177e4SLinus Torvalds {
31311da177e4SLinus Torvalds 	return socket_has_perm(current, sock, SOCKET__READ);
31321da177e4SLinus Torvalds }
31331da177e4SLinus Torvalds 
31341da177e4SLinus Torvalds static int selinux_socket_getsockname(struct socket *sock)
31351da177e4SLinus Torvalds {
31361da177e4SLinus Torvalds 	return socket_has_perm(current, sock, SOCKET__GETATTR);
31371da177e4SLinus Torvalds }
31381da177e4SLinus Torvalds 
31391da177e4SLinus Torvalds static int selinux_socket_getpeername(struct socket *sock)
31401da177e4SLinus Torvalds {
31411da177e4SLinus Torvalds 	return socket_has_perm(current, sock, SOCKET__GETATTR);
31421da177e4SLinus Torvalds }
31431da177e4SLinus Torvalds 
31441da177e4SLinus Torvalds static int selinux_socket_setsockopt(struct socket *sock,int level,int optname)
31451da177e4SLinus Torvalds {
31461da177e4SLinus Torvalds 	return socket_has_perm(current, sock, SOCKET__SETOPT);
31471da177e4SLinus Torvalds }
31481da177e4SLinus Torvalds 
31491da177e4SLinus Torvalds static int selinux_socket_getsockopt(struct socket *sock, int level,
31501da177e4SLinus Torvalds 				     int optname)
31511da177e4SLinus Torvalds {
31521da177e4SLinus Torvalds 	return socket_has_perm(current, sock, SOCKET__GETOPT);
31531da177e4SLinus Torvalds }
31541da177e4SLinus Torvalds 
31551da177e4SLinus Torvalds static int selinux_socket_shutdown(struct socket *sock, int how)
31561da177e4SLinus Torvalds {
31571da177e4SLinus Torvalds 	return socket_has_perm(current, sock, SOCKET__SHUTDOWN);
31581da177e4SLinus Torvalds }
31591da177e4SLinus Torvalds 
31601da177e4SLinus Torvalds static int selinux_socket_unix_stream_connect(struct socket *sock,
31611da177e4SLinus Torvalds 					      struct socket *other,
31621da177e4SLinus Torvalds 					      struct sock *newsk)
31631da177e4SLinus Torvalds {
31641da177e4SLinus Torvalds 	struct sk_security_struct *ssec;
31651da177e4SLinus Torvalds 	struct inode_security_struct *isec;
31661da177e4SLinus Torvalds 	struct inode_security_struct *other_isec;
31671da177e4SLinus Torvalds 	struct avc_audit_data ad;
31681da177e4SLinus Torvalds 	int err;
31691da177e4SLinus Torvalds 
31701da177e4SLinus Torvalds 	err = secondary_ops->unix_stream_connect(sock, other, newsk);
31711da177e4SLinus Torvalds 	if (err)
31721da177e4SLinus Torvalds 		return err;
31731da177e4SLinus Torvalds 
31741da177e4SLinus Torvalds 	isec = SOCK_INODE(sock)->i_security;
31751da177e4SLinus Torvalds 	other_isec = SOCK_INODE(other)->i_security;
31761da177e4SLinus Torvalds 
31771da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad,NET);
31781da177e4SLinus Torvalds 	ad.u.net.sk = other->sk;
31791da177e4SLinus Torvalds 
31801da177e4SLinus Torvalds 	err = avc_has_perm(isec->sid, other_isec->sid,
31811da177e4SLinus Torvalds 			   isec->sclass,
31821da177e4SLinus Torvalds 			   UNIX_STREAM_SOCKET__CONNECTTO, &ad);
31831da177e4SLinus Torvalds 	if (err)
31841da177e4SLinus Torvalds 		return err;
31851da177e4SLinus Torvalds 
31861da177e4SLinus Torvalds 	/* connecting socket */
31871da177e4SLinus Torvalds 	ssec = sock->sk->sk_security;
31881da177e4SLinus Torvalds 	ssec->peer_sid = other_isec->sid;
31891da177e4SLinus Torvalds 
31901da177e4SLinus Torvalds 	/* server child socket */
31911da177e4SLinus Torvalds 	ssec = newsk->sk_security;
31921da177e4SLinus Torvalds 	ssec->peer_sid = isec->sid;
31931da177e4SLinus Torvalds 
31941da177e4SLinus Torvalds 	return 0;
31951da177e4SLinus Torvalds }
31961da177e4SLinus Torvalds 
31971da177e4SLinus Torvalds static int selinux_socket_unix_may_send(struct socket *sock,
31981da177e4SLinus Torvalds 					struct socket *other)
31991da177e4SLinus Torvalds {
32001da177e4SLinus Torvalds 	struct inode_security_struct *isec;
32011da177e4SLinus Torvalds 	struct inode_security_struct *other_isec;
32021da177e4SLinus Torvalds 	struct avc_audit_data ad;
32031da177e4SLinus Torvalds 	int err;
32041da177e4SLinus Torvalds 
32051da177e4SLinus Torvalds 	isec = SOCK_INODE(sock)->i_security;
32061da177e4SLinus Torvalds 	other_isec = SOCK_INODE(other)->i_security;
32071da177e4SLinus Torvalds 
32081da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad,NET);
32091da177e4SLinus Torvalds 	ad.u.net.sk = other->sk;
32101da177e4SLinus Torvalds 
32111da177e4SLinus Torvalds 	err = avc_has_perm(isec->sid, other_isec->sid,
32121da177e4SLinus Torvalds 			   isec->sclass, SOCKET__SENDTO, &ad);
32131da177e4SLinus Torvalds 	if (err)
32141da177e4SLinus Torvalds 		return err;
32151da177e4SLinus Torvalds 
32161da177e4SLinus Torvalds 	return 0;
32171da177e4SLinus Torvalds }
32181da177e4SLinus Torvalds 
32191da177e4SLinus Torvalds static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
32201da177e4SLinus Torvalds {
32211da177e4SLinus Torvalds 	u16 family;
32221da177e4SLinus Torvalds 	char *addrp;
32231da177e4SLinus Torvalds 	int len, err = 0;
32241da177e4SLinus Torvalds 	u32 netif_perm, node_perm, node_sid, if_sid, recv_perm = 0;
32251da177e4SLinus Torvalds 	u32 sock_sid = 0;
32261da177e4SLinus Torvalds 	u16 sock_class = 0;
32271da177e4SLinus Torvalds 	struct socket *sock;
32281da177e4SLinus Torvalds 	struct net_device *dev;
32291da177e4SLinus Torvalds 	struct avc_audit_data ad;
32301da177e4SLinus Torvalds 
32311da177e4SLinus Torvalds 	family = sk->sk_family;
32321da177e4SLinus Torvalds 	if (family != PF_INET && family != PF_INET6)
32331da177e4SLinus Torvalds 		goto out;
32341da177e4SLinus Torvalds 
32351da177e4SLinus Torvalds 	/* Handle mapped IPv4 packets arriving via IPv6 sockets */
3236d4e9dc63SAlexey Dobriyan 	if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
32371da177e4SLinus Torvalds 		family = PF_INET;
32381da177e4SLinus Torvalds 
32391da177e4SLinus Torvalds  	read_lock_bh(&sk->sk_callback_lock);
32401da177e4SLinus Torvalds  	sock = sk->sk_socket;
32411da177e4SLinus Torvalds  	if (sock) {
32421da177e4SLinus Torvalds  		struct inode *inode;
32431da177e4SLinus Torvalds  		inode = SOCK_INODE(sock);
32441da177e4SLinus Torvalds  		if (inode) {
32451da177e4SLinus Torvalds  			struct inode_security_struct *isec;
32461da177e4SLinus Torvalds  			isec = inode->i_security;
32471da177e4SLinus Torvalds  			sock_sid = isec->sid;
32481da177e4SLinus Torvalds  			sock_class = isec->sclass;
32491da177e4SLinus Torvalds  		}
32501da177e4SLinus Torvalds  	}
32511da177e4SLinus Torvalds  	read_unlock_bh(&sk->sk_callback_lock);
32521da177e4SLinus Torvalds  	if (!sock_sid)
32531da177e4SLinus Torvalds   		goto out;
32541da177e4SLinus Torvalds 
32551da177e4SLinus Torvalds 	dev = skb->dev;
32561da177e4SLinus Torvalds 	if (!dev)
32571da177e4SLinus Torvalds 		goto out;
32581da177e4SLinus Torvalds 
32591da177e4SLinus Torvalds 	err = sel_netif_sids(dev, &if_sid, NULL);
32601da177e4SLinus Torvalds 	if (err)
32611da177e4SLinus Torvalds 		goto out;
32621da177e4SLinus Torvalds 
32631da177e4SLinus Torvalds 	switch (sock_class) {
32641da177e4SLinus Torvalds 	case SECCLASS_UDP_SOCKET:
32651da177e4SLinus Torvalds 		netif_perm = NETIF__UDP_RECV;
32661da177e4SLinus Torvalds 		node_perm = NODE__UDP_RECV;
32671da177e4SLinus Torvalds 		recv_perm = UDP_SOCKET__RECV_MSG;
32681da177e4SLinus Torvalds 		break;
32691da177e4SLinus Torvalds 
32701da177e4SLinus Torvalds 	case SECCLASS_TCP_SOCKET:
32711da177e4SLinus Torvalds 		netif_perm = NETIF__TCP_RECV;
32721da177e4SLinus Torvalds 		node_perm = NODE__TCP_RECV;
32731da177e4SLinus Torvalds 		recv_perm = TCP_SOCKET__RECV_MSG;
32741da177e4SLinus Torvalds 		break;
32751da177e4SLinus Torvalds 
32761da177e4SLinus Torvalds 	default:
32771da177e4SLinus Torvalds 		netif_perm = NETIF__RAWIP_RECV;
32781da177e4SLinus Torvalds 		node_perm = NODE__RAWIP_RECV;
32791da177e4SLinus Torvalds 		break;
32801da177e4SLinus Torvalds 	}
32811da177e4SLinus Torvalds 
32821da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, NET);
32831da177e4SLinus Torvalds 	ad.u.net.netif = dev->name;
32841da177e4SLinus Torvalds 	ad.u.net.family = family;
32851da177e4SLinus Torvalds 
32861da177e4SLinus Torvalds 	err = selinux_parse_skb(skb, &ad, &addrp, &len, 1);
32871da177e4SLinus Torvalds 	if (err)
32881da177e4SLinus Torvalds 		goto out;
32891da177e4SLinus Torvalds 
32901da177e4SLinus Torvalds 	err = avc_has_perm(sock_sid, if_sid, SECCLASS_NETIF, netif_perm, &ad);
32911da177e4SLinus Torvalds 	if (err)
32921da177e4SLinus Torvalds 		goto out;
32931da177e4SLinus Torvalds 
32941da177e4SLinus Torvalds 	/* Fixme: this lookup is inefficient */
32951da177e4SLinus Torvalds 	err = security_node_sid(family, addrp, len, &node_sid);
32961da177e4SLinus Torvalds 	if (err)
32971da177e4SLinus Torvalds 		goto out;
32981da177e4SLinus Torvalds 
32991da177e4SLinus Torvalds 	err = avc_has_perm(sock_sid, node_sid, SECCLASS_NODE, node_perm, &ad);
33001da177e4SLinus Torvalds 	if (err)
33011da177e4SLinus Torvalds 		goto out;
33021da177e4SLinus Torvalds 
33031da177e4SLinus Torvalds 	if (recv_perm) {
33041da177e4SLinus Torvalds 		u32 port_sid;
33051da177e4SLinus Torvalds 
33061da177e4SLinus Torvalds 		/* Fixme: make this more efficient */
33071da177e4SLinus Torvalds 		err = security_port_sid(sk->sk_family, sk->sk_type,
33081da177e4SLinus Torvalds 		                        sk->sk_protocol, ntohs(ad.u.net.sport),
33091da177e4SLinus Torvalds 		                        &port_sid);
33101da177e4SLinus Torvalds 		if (err)
33111da177e4SLinus Torvalds 			goto out;
33121da177e4SLinus Torvalds 
33131da177e4SLinus Torvalds 		err = avc_has_perm(sock_sid, port_sid,
33141da177e4SLinus Torvalds 				   sock_class, recv_perm, &ad);
33151da177e4SLinus Torvalds 	}
3316d28d1e08STrent Jaeger 
3317d28d1e08STrent Jaeger 	if (!err)
3318d28d1e08STrent Jaeger 		err = selinux_xfrm_sock_rcv_skb(sock_sid, skb);
3319d28d1e08STrent Jaeger 
33201da177e4SLinus Torvalds out:
33211da177e4SLinus Torvalds 	return err;
33221da177e4SLinus Torvalds }
33231da177e4SLinus Torvalds 
33242c7946a7SCatherine Zhang static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
33251da177e4SLinus Torvalds 					    int __user *optlen, unsigned len)
33261da177e4SLinus Torvalds {
33271da177e4SLinus Torvalds 	int err = 0;
33281da177e4SLinus Torvalds 	char *scontext;
33291da177e4SLinus Torvalds 	u32 scontext_len;
33301da177e4SLinus Torvalds 	struct sk_security_struct *ssec;
33311da177e4SLinus Torvalds 	struct inode_security_struct *isec;
33322c7946a7SCatherine Zhang 	u32 peer_sid = 0;
33331da177e4SLinus Torvalds 
33341da177e4SLinus Torvalds 	isec = SOCK_INODE(sock)->i_security;
33352c7946a7SCatherine Zhang 
33362c7946a7SCatherine Zhang 	/* if UNIX_STREAM check peer_sid, if TCP check dst for labelled sa */
33372c7946a7SCatherine Zhang 	if (isec->sclass == SECCLASS_UNIX_STREAM_SOCKET) {
33382c7946a7SCatherine Zhang 		ssec = sock->sk->sk_security;
33392c7946a7SCatherine Zhang 		peer_sid = ssec->peer_sid;
33402c7946a7SCatherine Zhang 	}
33412c7946a7SCatherine Zhang 	else if (isec->sclass == SECCLASS_TCP_SOCKET) {
33422c7946a7SCatherine Zhang 		peer_sid = selinux_socket_getpeer_stream(sock->sk);
33432c7946a7SCatherine Zhang 
33442c7946a7SCatherine Zhang 		if (peer_sid == SECSID_NULL) {
33452c7946a7SCatherine Zhang 			err = -ENOPROTOOPT;
33462c7946a7SCatherine Zhang 			goto out;
33472c7946a7SCatherine Zhang 		}
33482c7946a7SCatherine Zhang 	}
33492c7946a7SCatherine Zhang 	else {
33501da177e4SLinus Torvalds 		err = -ENOPROTOOPT;
33511da177e4SLinus Torvalds 		goto out;
33521da177e4SLinus Torvalds 	}
33531da177e4SLinus Torvalds 
33542c7946a7SCatherine Zhang 	err = security_sid_to_context(peer_sid, &scontext, &scontext_len);
33551da177e4SLinus Torvalds 
33561da177e4SLinus Torvalds 	if (err)
33571da177e4SLinus Torvalds 		goto out;
33581da177e4SLinus Torvalds 
33591da177e4SLinus Torvalds 	if (scontext_len > len) {
33601da177e4SLinus Torvalds 		err = -ERANGE;
33611da177e4SLinus Torvalds 		goto out_len;
33621da177e4SLinus Torvalds 	}
33631da177e4SLinus Torvalds 
33641da177e4SLinus Torvalds 	if (copy_to_user(optval, scontext, scontext_len))
33651da177e4SLinus Torvalds 		err = -EFAULT;
33661da177e4SLinus Torvalds 
33671da177e4SLinus Torvalds out_len:
33681da177e4SLinus Torvalds 	if (put_user(scontext_len, optlen))
33691da177e4SLinus Torvalds 		err = -EFAULT;
33701da177e4SLinus Torvalds 
33711da177e4SLinus Torvalds 	kfree(scontext);
33721da177e4SLinus Torvalds out:
33731da177e4SLinus Torvalds 	return err;
33741da177e4SLinus Torvalds }
33751da177e4SLinus Torvalds 
33762c7946a7SCatherine Zhang static int selinux_socket_getpeersec_dgram(struct sk_buff *skb, char **secdata, u32 *seclen)
33772c7946a7SCatherine Zhang {
33782c7946a7SCatherine Zhang 	int err = 0;
33792c7946a7SCatherine Zhang 	u32 peer_sid = selinux_socket_getpeer_dgram(skb);
33802c7946a7SCatherine Zhang 
33812c7946a7SCatherine Zhang 	if (peer_sid == SECSID_NULL)
33822c7946a7SCatherine Zhang 		return -EINVAL;
33832c7946a7SCatherine Zhang 
33842c7946a7SCatherine Zhang 	err = security_sid_to_context(peer_sid, secdata, seclen);
33852c7946a7SCatherine Zhang 	if (err)
33862c7946a7SCatherine Zhang 		return err;
33872c7946a7SCatherine Zhang 
33882c7946a7SCatherine Zhang 	return 0;
33892c7946a7SCatherine Zhang }
33902c7946a7SCatherine Zhang 
33912c7946a7SCatherine Zhang 
33922c7946a7SCatherine Zhang 
33937d877f3bSAl Viro static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
33941da177e4SLinus Torvalds {
33951da177e4SLinus Torvalds 	return sk_alloc_security(sk, family, priority);
33961da177e4SLinus Torvalds }
33971da177e4SLinus Torvalds 
33981da177e4SLinus Torvalds static void selinux_sk_free_security(struct sock *sk)
33991da177e4SLinus Torvalds {
34001da177e4SLinus Torvalds 	sk_free_security(sk);
34011da177e4SLinus Torvalds }
34021da177e4SLinus Torvalds 
3403d28d1e08STrent Jaeger static unsigned int selinux_sk_getsid_security(struct sock *sk, struct flowi *fl, u8 dir)
3404d28d1e08STrent Jaeger {
3405d28d1e08STrent Jaeger 	struct inode_security_struct *isec;
3406d28d1e08STrent Jaeger 	u32 sock_sid = SECINITSID_ANY_SOCKET;
3407d28d1e08STrent Jaeger 
3408d28d1e08STrent Jaeger 	if (!sk)
3409d28d1e08STrent Jaeger 		return selinux_no_sk_sid(fl);
3410d28d1e08STrent Jaeger 
3411d28d1e08STrent Jaeger 	read_lock_bh(&sk->sk_callback_lock);
3412d28d1e08STrent Jaeger 	isec = get_sock_isec(sk);
3413d28d1e08STrent Jaeger 
3414d28d1e08STrent Jaeger 	if (isec)
3415d28d1e08STrent Jaeger 		sock_sid = isec->sid;
3416d28d1e08STrent Jaeger 
3417d28d1e08STrent Jaeger 	read_unlock_bh(&sk->sk_callback_lock);
3418d28d1e08STrent Jaeger 	return sock_sid;
3419d28d1e08STrent Jaeger }
3420d28d1e08STrent Jaeger 
34211da177e4SLinus Torvalds static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
34221da177e4SLinus Torvalds {
34231da177e4SLinus Torvalds 	int err = 0;
34241da177e4SLinus Torvalds 	u32 perm;
34251da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
34261da177e4SLinus Torvalds 	struct socket *sock = sk->sk_socket;
34271da177e4SLinus Torvalds 	struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
34281da177e4SLinus Torvalds 
34291da177e4SLinus Torvalds 	if (skb->len < NLMSG_SPACE(0)) {
34301da177e4SLinus Torvalds 		err = -EINVAL;
34311da177e4SLinus Torvalds 		goto out;
34321da177e4SLinus Torvalds 	}
34331da177e4SLinus Torvalds 	nlh = (struct nlmsghdr *)skb->data;
34341da177e4SLinus Torvalds 
34351da177e4SLinus Torvalds 	err = selinux_nlmsg_lookup(isec->sclass, nlh->nlmsg_type, &perm);
34361da177e4SLinus Torvalds 	if (err) {
34371da177e4SLinus Torvalds 		if (err == -EINVAL) {
34389ad9ad38SDavid Woodhouse 			audit_log(current->audit_context, GFP_KERNEL, AUDIT_SELINUX_ERR,
34391da177e4SLinus Torvalds 				  "SELinux:  unrecognized netlink message"
34401da177e4SLinus Torvalds 				  " type=%hu for sclass=%hu\n",
34411da177e4SLinus Torvalds 				  nlh->nlmsg_type, isec->sclass);
34421da177e4SLinus Torvalds 			if (!selinux_enforcing)
34431da177e4SLinus Torvalds 				err = 0;
34441da177e4SLinus Torvalds 		}
34451da177e4SLinus Torvalds 
34461da177e4SLinus Torvalds 		/* Ignore */
34471da177e4SLinus Torvalds 		if (err == -ENOENT)
34481da177e4SLinus Torvalds 			err = 0;
34491da177e4SLinus Torvalds 		goto out;
34501da177e4SLinus Torvalds 	}
34511da177e4SLinus Torvalds 
34521da177e4SLinus Torvalds 	err = socket_has_perm(current, sock, perm);
34531da177e4SLinus Torvalds out:
34541da177e4SLinus Torvalds 	return err;
34551da177e4SLinus Torvalds }
34561da177e4SLinus Torvalds 
34571da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER
34581da177e4SLinus Torvalds 
34591da177e4SLinus Torvalds static unsigned int selinux_ip_postroute_last(unsigned int hooknum,
34601da177e4SLinus Torvalds                                               struct sk_buff **pskb,
34611da177e4SLinus Torvalds                                               const struct net_device *in,
34621da177e4SLinus Torvalds                                               const struct net_device *out,
34631da177e4SLinus Torvalds                                               int (*okfn)(struct sk_buff *),
34641da177e4SLinus Torvalds                                               u16 family)
34651da177e4SLinus Torvalds {
34661da177e4SLinus Torvalds 	char *addrp;
34671da177e4SLinus Torvalds 	int len, err = NF_ACCEPT;
34681da177e4SLinus Torvalds 	u32 netif_perm, node_perm, node_sid, if_sid, send_perm = 0;
34691da177e4SLinus Torvalds 	struct sock *sk;
34701da177e4SLinus Torvalds 	struct socket *sock;
34711da177e4SLinus Torvalds 	struct inode *inode;
34721da177e4SLinus Torvalds 	struct sk_buff *skb = *pskb;
34731da177e4SLinus Torvalds 	struct inode_security_struct *isec;
34741da177e4SLinus Torvalds 	struct avc_audit_data ad;
34751da177e4SLinus Torvalds 	struct net_device *dev = (struct net_device *)out;
34761da177e4SLinus Torvalds 
34771da177e4SLinus Torvalds 	sk = skb->sk;
34781da177e4SLinus Torvalds 	if (!sk)
34791da177e4SLinus Torvalds 		goto out;
34801da177e4SLinus Torvalds 
34811da177e4SLinus Torvalds 	sock = sk->sk_socket;
34821da177e4SLinus Torvalds 	if (!sock)
34831da177e4SLinus Torvalds 		goto out;
34841da177e4SLinus Torvalds 
34851da177e4SLinus Torvalds 	inode = SOCK_INODE(sock);
34861da177e4SLinus Torvalds 	if (!inode)
34871da177e4SLinus Torvalds 		goto out;
34881da177e4SLinus Torvalds 
34891da177e4SLinus Torvalds 	err = sel_netif_sids(dev, &if_sid, NULL);
34901da177e4SLinus Torvalds 	if (err)
34911da177e4SLinus Torvalds 		goto out;
34921da177e4SLinus Torvalds 
34931da177e4SLinus Torvalds 	isec = inode->i_security;
34941da177e4SLinus Torvalds 
34951da177e4SLinus Torvalds 	switch (isec->sclass) {
34961da177e4SLinus Torvalds 	case SECCLASS_UDP_SOCKET:
34971da177e4SLinus Torvalds 		netif_perm = NETIF__UDP_SEND;
34981da177e4SLinus Torvalds 		node_perm = NODE__UDP_SEND;
34991da177e4SLinus Torvalds 		send_perm = UDP_SOCKET__SEND_MSG;
35001da177e4SLinus Torvalds 		break;
35011da177e4SLinus Torvalds 
35021da177e4SLinus Torvalds 	case SECCLASS_TCP_SOCKET:
35031da177e4SLinus Torvalds 		netif_perm = NETIF__TCP_SEND;
35041da177e4SLinus Torvalds 		node_perm = NODE__TCP_SEND;
35051da177e4SLinus Torvalds 		send_perm = TCP_SOCKET__SEND_MSG;
35061da177e4SLinus Torvalds 		break;
35071da177e4SLinus Torvalds 
35081da177e4SLinus Torvalds 	default:
35091da177e4SLinus Torvalds 		netif_perm = NETIF__RAWIP_SEND;
35101da177e4SLinus Torvalds 		node_perm = NODE__RAWIP_SEND;
35111da177e4SLinus Torvalds 		break;
35121da177e4SLinus Torvalds 	}
35131da177e4SLinus Torvalds 
35141da177e4SLinus Torvalds 
35151da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, NET);
35161da177e4SLinus Torvalds 	ad.u.net.netif = dev->name;
35171da177e4SLinus Torvalds 	ad.u.net.family = family;
35181da177e4SLinus Torvalds 
35191da177e4SLinus Torvalds 	err = selinux_parse_skb(skb, &ad, &addrp,
35201da177e4SLinus Torvalds 				&len, 0) ? NF_DROP : NF_ACCEPT;
35211da177e4SLinus Torvalds 	if (err != NF_ACCEPT)
35221da177e4SLinus Torvalds 		goto out;
35231da177e4SLinus Torvalds 
35241da177e4SLinus Torvalds 	err = avc_has_perm(isec->sid, if_sid, SECCLASS_NETIF,
35251da177e4SLinus Torvalds 	                   netif_perm, &ad) ? NF_DROP : NF_ACCEPT;
35261da177e4SLinus Torvalds 	if (err != NF_ACCEPT)
35271da177e4SLinus Torvalds 		goto out;
35281da177e4SLinus Torvalds 
35291da177e4SLinus Torvalds 	/* Fixme: this lookup is inefficient */
35301da177e4SLinus Torvalds 	err = security_node_sid(family, addrp, len,
35311da177e4SLinus Torvalds 				&node_sid) ? NF_DROP : NF_ACCEPT;
35321da177e4SLinus Torvalds 	if (err != NF_ACCEPT)
35331da177e4SLinus Torvalds 		goto out;
35341da177e4SLinus Torvalds 
35351da177e4SLinus Torvalds 	err = avc_has_perm(isec->sid, node_sid, SECCLASS_NODE,
35361da177e4SLinus Torvalds 	                   node_perm, &ad) ? NF_DROP : NF_ACCEPT;
35371da177e4SLinus Torvalds 	if (err != NF_ACCEPT)
35381da177e4SLinus Torvalds 		goto out;
35391da177e4SLinus Torvalds 
35401da177e4SLinus Torvalds 	if (send_perm) {
35411da177e4SLinus Torvalds 		u32 port_sid;
35421da177e4SLinus Torvalds 
35431da177e4SLinus Torvalds 		/* Fixme: make this more efficient */
35441da177e4SLinus Torvalds 		err = security_port_sid(sk->sk_family,
35451da177e4SLinus Torvalds 		                        sk->sk_type,
35461da177e4SLinus Torvalds 		                        sk->sk_protocol,
35471da177e4SLinus Torvalds 		                        ntohs(ad.u.net.dport),
35481da177e4SLinus Torvalds 		                        &port_sid) ? NF_DROP : NF_ACCEPT;
35491da177e4SLinus Torvalds 		if (err != NF_ACCEPT)
35501da177e4SLinus Torvalds 			goto out;
35511da177e4SLinus Torvalds 
35521da177e4SLinus Torvalds 		err = avc_has_perm(isec->sid, port_sid, isec->sclass,
35531da177e4SLinus Torvalds 		                   send_perm, &ad) ? NF_DROP : NF_ACCEPT;
35541da177e4SLinus Torvalds 	}
35551da177e4SLinus Torvalds 
3556d28d1e08STrent Jaeger 	if (err != NF_ACCEPT)
3557d28d1e08STrent Jaeger 		goto out;
3558d28d1e08STrent Jaeger 
3559d28d1e08STrent Jaeger 	err = selinux_xfrm_postroute_last(isec->sid, skb);
3560d28d1e08STrent Jaeger 
35611da177e4SLinus Torvalds out:
35621da177e4SLinus Torvalds 	return err;
35631da177e4SLinus Torvalds }
35641da177e4SLinus Torvalds 
35651da177e4SLinus Torvalds static unsigned int selinux_ipv4_postroute_last(unsigned int hooknum,
35661da177e4SLinus Torvalds 						struct sk_buff **pskb,
35671da177e4SLinus Torvalds 						const struct net_device *in,
35681da177e4SLinus Torvalds 						const struct net_device *out,
35691da177e4SLinus Torvalds 						int (*okfn)(struct sk_buff *))
35701da177e4SLinus Torvalds {
35711da177e4SLinus Torvalds 	return selinux_ip_postroute_last(hooknum, pskb, in, out, okfn, PF_INET);
35721da177e4SLinus Torvalds }
35731da177e4SLinus Torvalds 
35741da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
35751da177e4SLinus Torvalds 
35761da177e4SLinus Torvalds static unsigned int selinux_ipv6_postroute_last(unsigned int hooknum,
35771da177e4SLinus Torvalds 						struct sk_buff **pskb,
35781da177e4SLinus Torvalds 						const struct net_device *in,
35791da177e4SLinus Torvalds 						const struct net_device *out,
35801da177e4SLinus Torvalds 						int (*okfn)(struct sk_buff *))
35811da177e4SLinus Torvalds {
35821da177e4SLinus Torvalds 	return selinux_ip_postroute_last(hooknum, pskb, in, out, okfn, PF_INET6);
35831da177e4SLinus Torvalds }
35841da177e4SLinus Torvalds 
35851da177e4SLinus Torvalds #endif	/* IPV6 */
35861da177e4SLinus Torvalds 
35871da177e4SLinus Torvalds #endif	/* CONFIG_NETFILTER */
35881da177e4SLinus Torvalds 
35891da177e4SLinus Torvalds static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
35901da177e4SLinus Torvalds {
35911da177e4SLinus Torvalds 	struct task_security_struct *tsec;
35921da177e4SLinus Torvalds 	struct av_decision avd;
35931da177e4SLinus Torvalds 	int err;
35941da177e4SLinus Torvalds 
35951da177e4SLinus Torvalds 	err = secondary_ops->netlink_send(sk, skb);
35961da177e4SLinus Torvalds 	if (err)
35971da177e4SLinus Torvalds 		return err;
35981da177e4SLinus Torvalds 
35991da177e4SLinus Torvalds 	tsec = current->security;
36001da177e4SLinus Torvalds 
36011da177e4SLinus Torvalds 	avd.allowed = 0;
36021da177e4SLinus Torvalds 	avc_has_perm_noaudit(tsec->sid, tsec->sid,
36031da177e4SLinus Torvalds 				SECCLASS_CAPABILITY, ~0, &avd);
36041da177e4SLinus Torvalds 	cap_mask(NETLINK_CB(skb).eff_cap, avd.allowed);
36051da177e4SLinus Torvalds 
36061da177e4SLinus Torvalds 	if (policydb_loaded_version >= POLICYDB_VERSION_NLCLASS)
36071da177e4SLinus Torvalds 		err = selinux_nlmsg_perm(sk, skb);
36081da177e4SLinus Torvalds 
36091da177e4SLinus Torvalds 	return err;
36101da177e4SLinus Torvalds }
36111da177e4SLinus Torvalds 
36121da177e4SLinus Torvalds static int selinux_netlink_recv(struct sk_buff *skb)
36131da177e4SLinus Torvalds {
36141da177e4SLinus Torvalds 	if (!cap_raised(NETLINK_CB(skb).eff_cap, CAP_NET_ADMIN))
36151da177e4SLinus Torvalds 		return -EPERM;
36161da177e4SLinus Torvalds 	return 0;
36171da177e4SLinus Torvalds }
36181da177e4SLinus Torvalds 
36191da177e4SLinus Torvalds static int ipc_alloc_security(struct task_struct *task,
36201da177e4SLinus Torvalds 			      struct kern_ipc_perm *perm,
36211da177e4SLinus Torvalds 			      u16 sclass)
36221da177e4SLinus Torvalds {
36231da177e4SLinus Torvalds 	struct task_security_struct *tsec = task->security;
36241da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
36251da177e4SLinus Torvalds 
362689d155efSJames Morris 	isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
36271da177e4SLinus Torvalds 	if (!isec)
36281da177e4SLinus Torvalds 		return -ENOMEM;
36291da177e4SLinus Torvalds 
36301da177e4SLinus Torvalds 	isec->sclass = sclass;
36311da177e4SLinus Torvalds 	isec->ipc_perm = perm;
36321da177e4SLinus Torvalds 	isec->sid = tsec->sid;
36331da177e4SLinus Torvalds 	perm->security = isec;
36341da177e4SLinus Torvalds 
36351da177e4SLinus Torvalds 	return 0;
36361da177e4SLinus Torvalds }
36371da177e4SLinus Torvalds 
36381da177e4SLinus Torvalds static void ipc_free_security(struct kern_ipc_perm *perm)
36391da177e4SLinus Torvalds {
36401da177e4SLinus Torvalds 	struct ipc_security_struct *isec = perm->security;
36411da177e4SLinus Torvalds 	perm->security = NULL;
36421da177e4SLinus Torvalds 	kfree(isec);
36431da177e4SLinus Torvalds }
36441da177e4SLinus Torvalds 
36451da177e4SLinus Torvalds static int msg_msg_alloc_security(struct msg_msg *msg)
36461da177e4SLinus Torvalds {
36471da177e4SLinus Torvalds 	struct msg_security_struct *msec;
36481da177e4SLinus Torvalds 
364989d155efSJames Morris 	msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
36501da177e4SLinus Torvalds 	if (!msec)
36511da177e4SLinus Torvalds 		return -ENOMEM;
36521da177e4SLinus Torvalds 
36531da177e4SLinus Torvalds 	msec->msg = msg;
36541da177e4SLinus Torvalds 	msec->sid = SECINITSID_UNLABELED;
36551da177e4SLinus Torvalds 	msg->security = msec;
36561da177e4SLinus Torvalds 
36571da177e4SLinus Torvalds 	return 0;
36581da177e4SLinus Torvalds }
36591da177e4SLinus Torvalds 
36601da177e4SLinus Torvalds static void msg_msg_free_security(struct msg_msg *msg)
36611da177e4SLinus Torvalds {
36621da177e4SLinus Torvalds 	struct msg_security_struct *msec = msg->security;
36631da177e4SLinus Torvalds 
36641da177e4SLinus Torvalds 	msg->security = NULL;
36651da177e4SLinus Torvalds 	kfree(msec);
36661da177e4SLinus Torvalds }
36671da177e4SLinus Torvalds 
36681da177e4SLinus Torvalds static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
36696af963f1SStephen Smalley 			u32 perms)
36701da177e4SLinus Torvalds {
36711da177e4SLinus Torvalds 	struct task_security_struct *tsec;
36721da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
36731da177e4SLinus Torvalds 	struct avc_audit_data ad;
36741da177e4SLinus Torvalds 
36751da177e4SLinus Torvalds 	tsec = current->security;
36761da177e4SLinus Torvalds 	isec = ipc_perms->security;
36771da177e4SLinus Torvalds 
36781da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, IPC);
36791da177e4SLinus Torvalds 	ad.u.ipc_id = ipc_perms->key;
36801da177e4SLinus Torvalds 
36816af963f1SStephen Smalley 	return avc_has_perm(tsec->sid, isec->sid, isec->sclass, perms, &ad);
36821da177e4SLinus Torvalds }
36831da177e4SLinus Torvalds 
36841da177e4SLinus Torvalds static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
36851da177e4SLinus Torvalds {
36861da177e4SLinus Torvalds 	return msg_msg_alloc_security(msg);
36871da177e4SLinus Torvalds }
36881da177e4SLinus Torvalds 
36891da177e4SLinus Torvalds static void selinux_msg_msg_free_security(struct msg_msg *msg)
36901da177e4SLinus Torvalds {
36911da177e4SLinus Torvalds 	msg_msg_free_security(msg);
36921da177e4SLinus Torvalds }
36931da177e4SLinus Torvalds 
36941da177e4SLinus Torvalds /* message queue security operations */
36951da177e4SLinus Torvalds static int selinux_msg_queue_alloc_security(struct msg_queue *msq)
36961da177e4SLinus Torvalds {
36971da177e4SLinus Torvalds 	struct task_security_struct *tsec;
36981da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
36991da177e4SLinus Torvalds 	struct avc_audit_data ad;
37001da177e4SLinus Torvalds 	int rc;
37011da177e4SLinus Torvalds 
37021da177e4SLinus Torvalds 	rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ);
37031da177e4SLinus Torvalds 	if (rc)
37041da177e4SLinus Torvalds 		return rc;
37051da177e4SLinus Torvalds 
37061da177e4SLinus Torvalds 	tsec = current->security;
37071da177e4SLinus Torvalds 	isec = msq->q_perm.security;
37081da177e4SLinus Torvalds 
37091da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, IPC);
37101da177e4SLinus Torvalds  	ad.u.ipc_id = msq->q_perm.key;
37111da177e4SLinus Torvalds 
37121da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_MSGQ,
37131da177e4SLinus Torvalds 			  MSGQ__CREATE, &ad);
37141da177e4SLinus Torvalds 	if (rc) {
37151da177e4SLinus Torvalds 		ipc_free_security(&msq->q_perm);
37161da177e4SLinus Torvalds 		return rc;
37171da177e4SLinus Torvalds 	}
37181da177e4SLinus Torvalds 	return 0;
37191da177e4SLinus Torvalds }
37201da177e4SLinus Torvalds 
37211da177e4SLinus Torvalds static void selinux_msg_queue_free_security(struct msg_queue *msq)
37221da177e4SLinus Torvalds {
37231da177e4SLinus Torvalds 	ipc_free_security(&msq->q_perm);
37241da177e4SLinus Torvalds }
37251da177e4SLinus Torvalds 
37261da177e4SLinus Torvalds static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg)
37271da177e4SLinus Torvalds {
37281da177e4SLinus Torvalds 	struct task_security_struct *tsec;
37291da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
37301da177e4SLinus Torvalds 	struct avc_audit_data ad;
37311da177e4SLinus Torvalds 
37321da177e4SLinus Torvalds 	tsec = current->security;
37331da177e4SLinus Torvalds 	isec = msq->q_perm.security;
37341da177e4SLinus Torvalds 
37351da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, IPC);
37361da177e4SLinus Torvalds 	ad.u.ipc_id = msq->q_perm.key;
37371da177e4SLinus Torvalds 
37381da177e4SLinus Torvalds 	return avc_has_perm(tsec->sid, isec->sid, SECCLASS_MSGQ,
37391da177e4SLinus Torvalds 			    MSGQ__ASSOCIATE, &ad);
37401da177e4SLinus Torvalds }
37411da177e4SLinus Torvalds 
37421da177e4SLinus Torvalds static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd)
37431da177e4SLinus Torvalds {
37441da177e4SLinus Torvalds 	int err;
37451da177e4SLinus Torvalds 	int perms;
37461da177e4SLinus Torvalds 
37471da177e4SLinus Torvalds 	switch(cmd) {
37481da177e4SLinus Torvalds 	case IPC_INFO:
37491da177e4SLinus Torvalds 	case MSG_INFO:
37501da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
37511da177e4SLinus Torvalds 		return task_has_system(current, SYSTEM__IPC_INFO);
37521da177e4SLinus Torvalds 	case IPC_STAT:
37531da177e4SLinus Torvalds 	case MSG_STAT:
37541da177e4SLinus Torvalds 		perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
37551da177e4SLinus Torvalds 		break;
37561da177e4SLinus Torvalds 	case IPC_SET:
37571da177e4SLinus Torvalds 		perms = MSGQ__SETATTR;
37581da177e4SLinus Torvalds 		break;
37591da177e4SLinus Torvalds 	case IPC_RMID:
37601da177e4SLinus Torvalds 		perms = MSGQ__DESTROY;
37611da177e4SLinus Torvalds 		break;
37621da177e4SLinus Torvalds 	default:
37631da177e4SLinus Torvalds 		return 0;
37641da177e4SLinus Torvalds 	}
37651da177e4SLinus Torvalds 
37666af963f1SStephen Smalley 	err = ipc_has_perm(&msq->q_perm, perms);
37671da177e4SLinus Torvalds 	return err;
37681da177e4SLinus Torvalds }
37691da177e4SLinus Torvalds 
37701da177e4SLinus Torvalds static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg)
37711da177e4SLinus Torvalds {
37721da177e4SLinus Torvalds 	struct task_security_struct *tsec;
37731da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
37741da177e4SLinus Torvalds 	struct msg_security_struct *msec;
37751da177e4SLinus Torvalds 	struct avc_audit_data ad;
37761da177e4SLinus Torvalds 	int rc;
37771da177e4SLinus Torvalds 
37781da177e4SLinus Torvalds 	tsec = current->security;
37791da177e4SLinus Torvalds 	isec = msq->q_perm.security;
37801da177e4SLinus Torvalds 	msec = msg->security;
37811da177e4SLinus Torvalds 
37821da177e4SLinus Torvalds 	/*
37831da177e4SLinus Torvalds 	 * First time through, need to assign label to the message
37841da177e4SLinus Torvalds 	 */
37851da177e4SLinus Torvalds 	if (msec->sid == SECINITSID_UNLABELED) {
37861da177e4SLinus Torvalds 		/*
37871da177e4SLinus Torvalds 		 * Compute new sid based on current process and
37881da177e4SLinus Torvalds 		 * message queue this message will be stored in
37891da177e4SLinus Torvalds 		 */
37901da177e4SLinus Torvalds 		rc = security_transition_sid(tsec->sid,
37911da177e4SLinus Torvalds 					     isec->sid,
37921da177e4SLinus Torvalds 					     SECCLASS_MSG,
37931da177e4SLinus Torvalds 					     &msec->sid);
37941da177e4SLinus Torvalds 		if (rc)
37951da177e4SLinus Torvalds 			return rc;
37961da177e4SLinus Torvalds 	}
37971da177e4SLinus Torvalds 
37981da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, IPC);
37991da177e4SLinus Torvalds 	ad.u.ipc_id = msq->q_perm.key;
38001da177e4SLinus Torvalds 
38011da177e4SLinus Torvalds 	/* Can this process write to the queue? */
38021da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_MSGQ,
38031da177e4SLinus Torvalds 			  MSGQ__WRITE, &ad);
38041da177e4SLinus Torvalds 	if (!rc)
38051da177e4SLinus Torvalds 		/* Can this process send the message */
38061da177e4SLinus Torvalds 		rc = avc_has_perm(tsec->sid, msec->sid,
38071da177e4SLinus Torvalds 				  SECCLASS_MSG, MSG__SEND, &ad);
38081da177e4SLinus Torvalds 	if (!rc)
38091da177e4SLinus Torvalds 		/* Can the message be put in the queue? */
38101da177e4SLinus Torvalds 		rc = avc_has_perm(msec->sid, isec->sid,
38111da177e4SLinus Torvalds 				  SECCLASS_MSGQ, MSGQ__ENQUEUE, &ad);
38121da177e4SLinus Torvalds 
38131da177e4SLinus Torvalds 	return rc;
38141da177e4SLinus Torvalds }
38151da177e4SLinus Torvalds 
38161da177e4SLinus Torvalds static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
38171da177e4SLinus Torvalds 				    struct task_struct *target,
38181da177e4SLinus Torvalds 				    long type, int mode)
38191da177e4SLinus Torvalds {
38201da177e4SLinus Torvalds 	struct task_security_struct *tsec;
38211da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
38221da177e4SLinus Torvalds 	struct msg_security_struct *msec;
38231da177e4SLinus Torvalds 	struct avc_audit_data ad;
38241da177e4SLinus Torvalds 	int rc;
38251da177e4SLinus Torvalds 
38261da177e4SLinus Torvalds 	tsec = target->security;
38271da177e4SLinus Torvalds 	isec = msq->q_perm.security;
38281da177e4SLinus Torvalds 	msec = msg->security;
38291da177e4SLinus Torvalds 
38301da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, IPC);
38311da177e4SLinus Torvalds  	ad.u.ipc_id = msq->q_perm.key;
38321da177e4SLinus Torvalds 
38331da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, isec->sid,
38341da177e4SLinus Torvalds 			  SECCLASS_MSGQ, MSGQ__READ, &ad);
38351da177e4SLinus Torvalds 	if (!rc)
38361da177e4SLinus Torvalds 		rc = avc_has_perm(tsec->sid, msec->sid,
38371da177e4SLinus Torvalds 				  SECCLASS_MSG, MSG__RECEIVE, &ad);
38381da177e4SLinus Torvalds 	return rc;
38391da177e4SLinus Torvalds }
38401da177e4SLinus Torvalds 
38411da177e4SLinus Torvalds /* Shared Memory security operations */
38421da177e4SLinus Torvalds static int selinux_shm_alloc_security(struct shmid_kernel *shp)
38431da177e4SLinus Torvalds {
38441da177e4SLinus Torvalds 	struct task_security_struct *tsec;
38451da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
38461da177e4SLinus Torvalds 	struct avc_audit_data ad;
38471da177e4SLinus Torvalds 	int rc;
38481da177e4SLinus Torvalds 
38491da177e4SLinus Torvalds 	rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM);
38501da177e4SLinus Torvalds 	if (rc)
38511da177e4SLinus Torvalds 		return rc;
38521da177e4SLinus Torvalds 
38531da177e4SLinus Torvalds 	tsec = current->security;
38541da177e4SLinus Torvalds 	isec = shp->shm_perm.security;
38551da177e4SLinus Torvalds 
38561da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, IPC);
38571da177e4SLinus Torvalds  	ad.u.ipc_id = shp->shm_perm.key;
38581da177e4SLinus Torvalds 
38591da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_SHM,
38601da177e4SLinus Torvalds 			  SHM__CREATE, &ad);
38611da177e4SLinus Torvalds 	if (rc) {
38621da177e4SLinus Torvalds 		ipc_free_security(&shp->shm_perm);
38631da177e4SLinus Torvalds 		return rc;
38641da177e4SLinus Torvalds 	}
38651da177e4SLinus Torvalds 	return 0;
38661da177e4SLinus Torvalds }
38671da177e4SLinus Torvalds 
38681da177e4SLinus Torvalds static void selinux_shm_free_security(struct shmid_kernel *shp)
38691da177e4SLinus Torvalds {
38701da177e4SLinus Torvalds 	ipc_free_security(&shp->shm_perm);
38711da177e4SLinus Torvalds }
38721da177e4SLinus Torvalds 
38731da177e4SLinus Torvalds static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg)
38741da177e4SLinus Torvalds {
38751da177e4SLinus Torvalds 	struct task_security_struct *tsec;
38761da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
38771da177e4SLinus Torvalds 	struct avc_audit_data ad;
38781da177e4SLinus Torvalds 
38791da177e4SLinus Torvalds 	tsec = current->security;
38801da177e4SLinus Torvalds 	isec = shp->shm_perm.security;
38811da177e4SLinus Torvalds 
38821da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, IPC);
38831da177e4SLinus Torvalds 	ad.u.ipc_id = shp->shm_perm.key;
38841da177e4SLinus Torvalds 
38851da177e4SLinus Torvalds 	return avc_has_perm(tsec->sid, isec->sid, SECCLASS_SHM,
38861da177e4SLinus Torvalds 			    SHM__ASSOCIATE, &ad);
38871da177e4SLinus Torvalds }
38881da177e4SLinus Torvalds 
38891da177e4SLinus Torvalds /* Note, at this point, shp is locked down */
38901da177e4SLinus Torvalds static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd)
38911da177e4SLinus Torvalds {
38921da177e4SLinus Torvalds 	int perms;
38931da177e4SLinus Torvalds 	int err;
38941da177e4SLinus Torvalds 
38951da177e4SLinus Torvalds 	switch(cmd) {
38961da177e4SLinus Torvalds 	case IPC_INFO:
38971da177e4SLinus Torvalds 	case SHM_INFO:
38981da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
38991da177e4SLinus Torvalds 		return task_has_system(current, SYSTEM__IPC_INFO);
39001da177e4SLinus Torvalds 	case IPC_STAT:
39011da177e4SLinus Torvalds 	case SHM_STAT:
39021da177e4SLinus Torvalds 		perms = SHM__GETATTR | SHM__ASSOCIATE;
39031da177e4SLinus Torvalds 		break;
39041da177e4SLinus Torvalds 	case IPC_SET:
39051da177e4SLinus Torvalds 		perms = SHM__SETATTR;
39061da177e4SLinus Torvalds 		break;
39071da177e4SLinus Torvalds 	case SHM_LOCK:
39081da177e4SLinus Torvalds 	case SHM_UNLOCK:
39091da177e4SLinus Torvalds 		perms = SHM__LOCK;
39101da177e4SLinus Torvalds 		break;
39111da177e4SLinus Torvalds 	case IPC_RMID:
39121da177e4SLinus Torvalds 		perms = SHM__DESTROY;
39131da177e4SLinus Torvalds 		break;
39141da177e4SLinus Torvalds 	default:
39151da177e4SLinus Torvalds 		return 0;
39161da177e4SLinus Torvalds 	}
39171da177e4SLinus Torvalds 
39186af963f1SStephen Smalley 	err = ipc_has_perm(&shp->shm_perm, perms);
39191da177e4SLinus Torvalds 	return err;
39201da177e4SLinus Torvalds }
39211da177e4SLinus Torvalds 
39221da177e4SLinus Torvalds static int selinux_shm_shmat(struct shmid_kernel *shp,
39231da177e4SLinus Torvalds 			     char __user *shmaddr, int shmflg)
39241da177e4SLinus Torvalds {
39251da177e4SLinus Torvalds 	u32 perms;
39261da177e4SLinus Torvalds 	int rc;
39271da177e4SLinus Torvalds 
39281da177e4SLinus Torvalds 	rc = secondary_ops->shm_shmat(shp, shmaddr, shmflg);
39291da177e4SLinus Torvalds 	if (rc)
39301da177e4SLinus Torvalds 		return rc;
39311da177e4SLinus Torvalds 
39321da177e4SLinus Torvalds 	if (shmflg & SHM_RDONLY)
39331da177e4SLinus Torvalds 		perms = SHM__READ;
39341da177e4SLinus Torvalds 	else
39351da177e4SLinus Torvalds 		perms = SHM__READ | SHM__WRITE;
39361da177e4SLinus Torvalds 
39376af963f1SStephen Smalley 	return ipc_has_perm(&shp->shm_perm, perms);
39381da177e4SLinus Torvalds }
39391da177e4SLinus Torvalds 
39401da177e4SLinus Torvalds /* Semaphore security operations */
39411da177e4SLinus Torvalds static int selinux_sem_alloc_security(struct sem_array *sma)
39421da177e4SLinus Torvalds {
39431da177e4SLinus Torvalds 	struct task_security_struct *tsec;
39441da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
39451da177e4SLinus Torvalds 	struct avc_audit_data ad;
39461da177e4SLinus Torvalds 	int rc;
39471da177e4SLinus Torvalds 
39481da177e4SLinus Torvalds 	rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM);
39491da177e4SLinus Torvalds 	if (rc)
39501da177e4SLinus Torvalds 		return rc;
39511da177e4SLinus Torvalds 
39521da177e4SLinus Torvalds 	tsec = current->security;
39531da177e4SLinus Torvalds 	isec = sma->sem_perm.security;
39541da177e4SLinus Torvalds 
39551da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, IPC);
39561da177e4SLinus Torvalds  	ad.u.ipc_id = sma->sem_perm.key;
39571da177e4SLinus Torvalds 
39581da177e4SLinus Torvalds 	rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_SEM,
39591da177e4SLinus Torvalds 			  SEM__CREATE, &ad);
39601da177e4SLinus Torvalds 	if (rc) {
39611da177e4SLinus Torvalds 		ipc_free_security(&sma->sem_perm);
39621da177e4SLinus Torvalds 		return rc;
39631da177e4SLinus Torvalds 	}
39641da177e4SLinus Torvalds 	return 0;
39651da177e4SLinus Torvalds }
39661da177e4SLinus Torvalds 
39671da177e4SLinus Torvalds static void selinux_sem_free_security(struct sem_array *sma)
39681da177e4SLinus Torvalds {
39691da177e4SLinus Torvalds 	ipc_free_security(&sma->sem_perm);
39701da177e4SLinus Torvalds }
39711da177e4SLinus Torvalds 
39721da177e4SLinus Torvalds static int selinux_sem_associate(struct sem_array *sma, int semflg)
39731da177e4SLinus Torvalds {
39741da177e4SLinus Torvalds 	struct task_security_struct *tsec;
39751da177e4SLinus Torvalds 	struct ipc_security_struct *isec;
39761da177e4SLinus Torvalds 	struct avc_audit_data ad;
39771da177e4SLinus Torvalds 
39781da177e4SLinus Torvalds 	tsec = current->security;
39791da177e4SLinus Torvalds 	isec = sma->sem_perm.security;
39801da177e4SLinus Torvalds 
39811da177e4SLinus Torvalds 	AVC_AUDIT_DATA_INIT(&ad, IPC);
39821da177e4SLinus Torvalds 	ad.u.ipc_id = sma->sem_perm.key;
39831da177e4SLinus Torvalds 
39841da177e4SLinus Torvalds 	return avc_has_perm(tsec->sid, isec->sid, SECCLASS_SEM,
39851da177e4SLinus Torvalds 			    SEM__ASSOCIATE, &ad);
39861da177e4SLinus Torvalds }
39871da177e4SLinus Torvalds 
39881da177e4SLinus Torvalds /* Note, at this point, sma is locked down */
39891da177e4SLinus Torvalds static int selinux_sem_semctl(struct sem_array *sma, int cmd)
39901da177e4SLinus Torvalds {
39911da177e4SLinus Torvalds 	int err;
39921da177e4SLinus Torvalds 	u32 perms;
39931da177e4SLinus Torvalds 
39941da177e4SLinus Torvalds 	switch(cmd) {
39951da177e4SLinus Torvalds 	case IPC_INFO:
39961da177e4SLinus Torvalds 	case SEM_INFO:
39971da177e4SLinus Torvalds 		/* No specific object, just general system-wide information. */
39981da177e4SLinus Torvalds 		return task_has_system(current, SYSTEM__IPC_INFO);
39991da177e4SLinus Torvalds 	case GETPID:
40001da177e4SLinus Torvalds 	case GETNCNT:
40011da177e4SLinus Torvalds 	case GETZCNT:
40021da177e4SLinus Torvalds 		perms = SEM__GETATTR;
40031da177e4SLinus Torvalds 		break;
40041da177e4SLinus Torvalds 	case GETVAL:
40051da177e4SLinus Torvalds 	case GETALL:
40061da177e4SLinus Torvalds 		perms = SEM__READ;
40071da177e4SLinus Torvalds 		break;
40081da177e4SLinus Torvalds 	case SETVAL:
40091da177e4SLinus Torvalds 	case SETALL:
40101da177e4SLinus Torvalds 		perms = SEM__WRITE;
40111da177e4SLinus Torvalds 		break;
40121da177e4SLinus Torvalds 	case IPC_RMID:
40131da177e4SLinus Torvalds 		perms = SEM__DESTROY;
40141da177e4SLinus Torvalds 		break;
40151da177e4SLinus Torvalds 	case IPC_SET:
40161da177e4SLinus Torvalds 		perms = SEM__SETATTR;
40171da177e4SLinus Torvalds 		break;
40181da177e4SLinus Torvalds 	case IPC_STAT:
40191da177e4SLinus Torvalds 	case SEM_STAT:
40201da177e4SLinus Torvalds 		perms = SEM__GETATTR | SEM__ASSOCIATE;
40211da177e4SLinus Torvalds 		break;
40221da177e4SLinus Torvalds 	default:
40231da177e4SLinus Torvalds 		return 0;
40241da177e4SLinus Torvalds 	}
40251da177e4SLinus Torvalds 
40266af963f1SStephen Smalley 	err = ipc_has_perm(&sma->sem_perm, perms);
40271da177e4SLinus Torvalds 	return err;
40281da177e4SLinus Torvalds }
40291da177e4SLinus Torvalds 
40301da177e4SLinus Torvalds static int selinux_sem_semop(struct sem_array *sma,
40311da177e4SLinus Torvalds 			     struct sembuf *sops, unsigned nsops, int alter)
40321da177e4SLinus Torvalds {
40331da177e4SLinus Torvalds 	u32 perms;
40341da177e4SLinus Torvalds 
40351da177e4SLinus Torvalds 	if (alter)
40361da177e4SLinus Torvalds 		perms = SEM__READ | SEM__WRITE;
40371da177e4SLinus Torvalds 	else
40381da177e4SLinus Torvalds 		perms = SEM__READ;
40391da177e4SLinus Torvalds 
40406af963f1SStephen Smalley 	return ipc_has_perm(&sma->sem_perm, perms);
40411da177e4SLinus Torvalds }
40421da177e4SLinus Torvalds 
40431da177e4SLinus Torvalds static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
40441da177e4SLinus Torvalds {
40451da177e4SLinus Torvalds 	u32 av = 0;
40461da177e4SLinus Torvalds 
40471da177e4SLinus Torvalds 	av = 0;
40481da177e4SLinus Torvalds 	if (flag & S_IRUGO)
40491da177e4SLinus Torvalds 		av |= IPC__UNIX_READ;
40501da177e4SLinus Torvalds 	if (flag & S_IWUGO)
40511da177e4SLinus Torvalds 		av |= IPC__UNIX_WRITE;
40521da177e4SLinus Torvalds 
40531da177e4SLinus Torvalds 	if (av == 0)
40541da177e4SLinus Torvalds 		return 0;
40551da177e4SLinus Torvalds 
40566af963f1SStephen Smalley 	return ipc_has_perm(ipcp, av);
40571da177e4SLinus Torvalds }
40581da177e4SLinus Torvalds 
40591da177e4SLinus Torvalds /* module stacking operations */
40601da177e4SLinus Torvalds static int selinux_register_security (const char *name, struct security_operations *ops)
40611da177e4SLinus Torvalds {
40621da177e4SLinus Torvalds 	if (secondary_ops != original_ops) {
40631da177e4SLinus Torvalds 		printk(KERN_INFO "%s:  There is already a secondary security "
40641da177e4SLinus Torvalds 		       "module registered.\n", __FUNCTION__);
40651da177e4SLinus Torvalds 		return -EINVAL;
40661da177e4SLinus Torvalds  	}
40671da177e4SLinus Torvalds 
40681da177e4SLinus Torvalds 	secondary_ops = ops;
40691da177e4SLinus Torvalds 
40701da177e4SLinus Torvalds 	printk(KERN_INFO "%s:  Registering secondary module %s\n",
40711da177e4SLinus Torvalds 	       __FUNCTION__,
40721da177e4SLinus Torvalds 	       name);
40731da177e4SLinus Torvalds 
40741da177e4SLinus Torvalds 	return 0;
40751da177e4SLinus Torvalds }
40761da177e4SLinus Torvalds 
40771da177e4SLinus Torvalds static int selinux_unregister_security (const char *name, struct security_operations *ops)
40781da177e4SLinus Torvalds {
40791da177e4SLinus Torvalds 	if (ops != secondary_ops) {
40801da177e4SLinus Torvalds 		printk (KERN_INFO "%s:  trying to unregister a security module "
40811da177e4SLinus Torvalds 		        "that is not registered.\n", __FUNCTION__);
40821da177e4SLinus Torvalds 		return -EINVAL;
40831da177e4SLinus Torvalds 	}
40841da177e4SLinus Torvalds 
40851da177e4SLinus Torvalds 	secondary_ops = original_ops;
40861da177e4SLinus Torvalds 
40871da177e4SLinus Torvalds 	return 0;
40881da177e4SLinus Torvalds }
40891da177e4SLinus Torvalds 
40901da177e4SLinus Torvalds static void selinux_d_instantiate (struct dentry *dentry, struct inode *inode)
40911da177e4SLinus Torvalds {
40921da177e4SLinus Torvalds 	if (inode)
40931da177e4SLinus Torvalds 		inode_doinit_with_dentry(inode, dentry);
40941da177e4SLinus Torvalds }
40951da177e4SLinus Torvalds 
40961da177e4SLinus Torvalds static int selinux_getprocattr(struct task_struct *p,
40971da177e4SLinus Torvalds 			       char *name, void *value, size_t size)
40981da177e4SLinus Torvalds {
40991da177e4SLinus Torvalds 	struct task_security_struct *tsec;
41008c8570fbSDustin Kirkland 	u32 sid;
41011da177e4SLinus Torvalds 	int error;
41021da177e4SLinus Torvalds 
41031da177e4SLinus Torvalds 	if (current != p) {
41041da177e4SLinus Torvalds 		error = task_has_perm(current, p, PROCESS__GETATTR);
41051da177e4SLinus Torvalds 		if (error)
41061da177e4SLinus Torvalds 			return error;
41071da177e4SLinus Torvalds 	}
41081da177e4SLinus Torvalds 
41091da177e4SLinus Torvalds 	tsec = p->security;
41101da177e4SLinus Torvalds 
41111da177e4SLinus Torvalds 	if (!strcmp(name, "current"))
41121da177e4SLinus Torvalds 		sid = tsec->sid;
41131da177e4SLinus Torvalds 	else if (!strcmp(name, "prev"))
41141da177e4SLinus Torvalds 		sid = tsec->osid;
41151da177e4SLinus Torvalds 	else if (!strcmp(name, "exec"))
41161da177e4SLinus Torvalds 		sid = tsec->exec_sid;
41171da177e4SLinus Torvalds 	else if (!strcmp(name, "fscreate"))
41181da177e4SLinus Torvalds 		sid = tsec->create_sid;
41191da177e4SLinus Torvalds 	else
41201da177e4SLinus Torvalds 		return -EINVAL;
41211da177e4SLinus Torvalds 
41221da177e4SLinus Torvalds 	if (!sid)
41231da177e4SLinus Torvalds 		return 0;
41241da177e4SLinus Torvalds 
41258c8570fbSDustin Kirkland 	return selinux_getsecurity(sid, value, size);
41261da177e4SLinus Torvalds }
41271da177e4SLinus Torvalds 
41281da177e4SLinus Torvalds static int selinux_setprocattr(struct task_struct *p,
41291da177e4SLinus Torvalds 			       char *name, void *value, size_t size)
41301da177e4SLinus Torvalds {
41311da177e4SLinus Torvalds 	struct task_security_struct *tsec;
41321da177e4SLinus Torvalds 	u32 sid = 0;
41331da177e4SLinus Torvalds 	int error;
41341da177e4SLinus Torvalds 	char *str = value;
41351da177e4SLinus Torvalds 
41361da177e4SLinus Torvalds 	if (current != p) {
41371da177e4SLinus Torvalds 		/* SELinux only allows a process to change its own
41381da177e4SLinus Torvalds 		   security attributes. */
41391da177e4SLinus Torvalds 		return -EACCES;
41401da177e4SLinus Torvalds 	}
41411da177e4SLinus Torvalds 
41421da177e4SLinus Torvalds 	/*
41431da177e4SLinus Torvalds 	 * Basic control over ability to set these attributes at all.
41441da177e4SLinus Torvalds 	 * current == p, but we'll pass them separately in case the
41451da177e4SLinus Torvalds 	 * above restriction is ever removed.
41461da177e4SLinus Torvalds 	 */
41471da177e4SLinus Torvalds 	if (!strcmp(name, "exec"))
41481da177e4SLinus Torvalds 		error = task_has_perm(current, p, PROCESS__SETEXEC);
41491da177e4SLinus Torvalds 	else if (!strcmp(name, "fscreate"))
41501da177e4SLinus Torvalds 		error = task_has_perm(current, p, PROCESS__SETFSCREATE);
41511da177e4SLinus Torvalds 	else if (!strcmp(name, "current"))
41521da177e4SLinus Torvalds 		error = task_has_perm(current, p, PROCESS__SETCURRENT);
41531da177e4SLinus Torvalds 	else
41541da177e4SLinus Torvalds 		error = -EINVAL;
41551da177e4SLinus Torvalds 	if (error)
41561da177e4SLinus Torvalds 		return error;
41571da177e4SLinus Torvalds 
41581da177e4SLinus Torvalds 	/* Obtain a SID for the context, if one was specified. */
41591da177e4SLinus Torvalds 	if (size && str[1] && str[1] != '\n') {
41601da177e4SLinus Torvalds 		if (str[size-1] == '\n') {
41611da177e4SLinus Torvalds 			str[size-1] = 0;
41621da177e4SLinus Torvalds 			size--;
41631da177e4SLinus Torvalds 		}
41641da177e4SLinus Torvalds 		error = security_context_to_sid(value, size, &sid);
41651da177e4SLinus Torvalds 		if (error)
41661da177e4SLinus Torvalds 			return error;
41671da177e4SLinus Torvalds 	}
41681da177e4SLinus Torvalds 
41691da177e4SLinus Torvalds 	/* Permission checking based on the specified context is
41701da177e4SLinus Torvalds 	   performed during the actual operation (execve,
41711da177e4SLinus Torvalds 	   open/mkdir/...), when we know the full context of the
41721da177e4SLinus Torvalds 	   operation.  See selinux_bprm_set_security for the execve
41731da177e4SLinus Torvalds 	   checks and may_create for the file creation checks. The
41741da177e4SLinus Torvalds 	   operation will then fail if the context is not permitted. */
41751da177e4SLinus Torvalds 	tsec = p->security;
41761da177e4SLinus Torvalds 	if (!strcmp(name, "exec"))
41771da177e4SLinus Torvalds 		tsec->exec_sid = sid;
41781da177e4SLinus Torvalds 	else if (!strcmp(name, "fscreate"))
41791da177e4SLinus Torvalds 		tsec->create_sid = sid;
41801da177e4SLinus Torvalds 	else if (!strcmp(name, "current")) {
41811da177e4SLinus Torvalds 		struct av_decision avd;
41821da177e4SLinus Torvalds 
41831da177e4SLinus Torvalds 		if (sid == 0)
41841da177e4SLinus Torvalds 			return -EINVAL;
41851da177e4SLinus Torvalds 
41861da177e4SLinus Torvalds 		/* Only allow single threaded processes to change context */
41871da177e4SLinus Torvalds 		if (atomic_read(&p->mm->mm_users) != 1) {
41881da177e4SLinus Torvalds 			struct task_struct *g, *t;
41891da177e4SLinus Torvalds 			struct mm_struct *mm = p->mm;
41901da177e4SLinus Torvalds 			read_lock(&tasklist_lock);
41911da177e4SLinus Torvalds 			do_each_thread(g, t)
41921da177e4SLinus Torvalds 				if (t->mm == mm && t != p) {
41931da177e4SLinus Torvalds 					read_unlock(&tasklist_lock);
41941da177e4SLinus Torvalds 					return -EPERM;
41951da177e4SLinus Torvalds 				}
41961da177e4SLinus Torvalds 			while_each_thread(g, t);
41971da177e4SLinus Torvalds 			read_unlock(&tasklist_lock);
41981da177e4SLinus Torvalds                 }
41991da177e4SLinus Torvalds 
42001da177e4SLinus Torvalds 		/* Check permissions for the transition. */
42011da177e4SLinus Torvalds 		error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS,
42021da177e4SLinus Torvalds 		                     PROCESS__DYNTRANSITION, NULL);
42031da177e4SLinus Torvalds 		if (error)
42041da177e4SLinus Torvalds 			return error;
42051da177e4SLinus Torvalds 
42061da177e4SLinus Torvalds 		/* Check for ptracing, and update the task SID if ok.
42071da177e4SLinus Torvalds 		   Otherwise, leave SID unchanged and fail. */
42081da177e4SLinus Torvalds 		task_lock(p);
42091da177e4SLinus Torvalds 		if (p->ptrace & PT_PTRACED) {
42101da177e4SLinus Torvalds 			error = avc_has_perm_noaudit(tsec->ptrace_sid, sid,
42111da177e4SLinus Torvalds 						     SECCLASS_PROCESS,
42121da177e4SLinus Torvalds 						     PROCESS__PTRACE, &avd);
42131da177e4SLinus Torvalds 			if (!error)
42141da177e4SLinus Torvalds 				tsec->sid = sid;
42151da177e4SLinus Torvalds 			task_unlock(p);
42161da177e4SLinus Torvalds 			avc_audit(tsec->ptrace_sid, sid, SECCLASS_PROCESS,
42171da177e4SLinus Torvalds 				  PROCESS__PTRACE, &avd, error, NULL);
42181da177e4SLinus Torvalds 			if (error)
42191da177e4SLinus Torvalds 				return error;
42201da177e4SLinus Torvalds 		} else {
42211da177e4SLinus Torvalds 			tsec->sid = sid;
42221da177e4SLinus Torvalds 			task_unlock(p);
42231da177e4SLinus Torvalds 		}
42241da177e4SLinus Torvalds 	}
42251da177e4SLinus Torvalds 	else
42261da177e4SLinus Torvalds 		return -EINVAL;
42271da177e4SLinus Torvalds 
42281da177e4SLinus Torvalds 	return size;
42291da177e4SLinus Torvalds }
42301da177e4SLinus Torvalds 
42311da177e4SLinus Torvalds static struct security_operations selinux_ops = {
42321da177e4SLinus Torvalds 	.ptrace =			selinux_ptrace,
42331da177e4SLinus Torvalds 	.capget =			selinux_capget,
42341da177e4SLinus Torvalds 	.capset_check =			selinux_capset_check,
42351da177e4SLinus Torvalds 	.capset_set =			selinux_capset_set,
42361da177e4SLinus Torvalds 	.sysctl =			selinux_sysctl,
42371da177e4SLinus Torvalds 	.capable =			selinux_capable,
42381da177e4SLinus Torvalds 	.quotactl =			selinux_quotactl,
42391da177e4SLinus Torvalds 	.quota_on =			selinux_quota_on,
42401da177e4SLinus Torvalds 	.syslog =			selinux_syslog,
42411da177e4SLinus Torvalds 	.vm_enough_memory =		selinux_vm_enough_memory,
42421da177e4SLinus Torvalds 
42431da177e4SLinus Torvalds 	.netlink_send =			selinux_netlink_send,
42441da177e4SLinus Torvalds         .netlink_recv =			selinux_netlink_recv,
42451da177e4SLinus Torvalds 
42461da177e4SLinus Torvalds 	.bprm_alloc_security =		selinux_bprm_alloc_security,
42471da177e4SLinus Torvalds 	.bprm_free_security =		selinux_bprm_free_security,
42481da177e4SLinus Torvalds 	.bprm_apply_creds =		selinux_bprm_apply_creds,
42491da177e4SLinus Torvalds 	.bprm_post_apply_creds =	selinux_bprm_post_apply_creds,
42501da177e4SLinus Torvalds 	.bprm_set_security =		selinux_bprm_set_security,
42511da177e4SLinus Torvalds 	.bprm_check_security =		selinux_bprm_check_security,
42521da177e4SLinus Torvalds 	.bprm_secureexec =		selinux_bprm_secureexec,
42531da177e4SLinus Torvalds 
42541da177e4SLinus Torvalds 	.sb_alloc_security =		selinux_sb_alloc_security,
42551da177e4SLinus Torvalds 	.sb_free_security =		selinux_sb_free_security,
42561da177e4SLinus Torvalds 	.sb_copy_data =			selinux_sb_copy_data,
42571da177e4SLinus Torvalds 	.sb_kern_mount =	        selinux_sb_kern_mount,
42581da177e4SLinus Torvalds 	.sb_statfs =			selinux_sb_statfs,
42591da177e4SLinus Torvalds 	.sb_mount =			selinux_mount,
42601da177e4SLinus Torvalds 	.sb_umount =			selinux_umount,
42611da177e4SLinus Torvalds 
42621da177e4SLinus Torvalds 	.inode_alloc_security =		selinux_inode_alloc_security,
42631da177e4SLinus Torvalds 	.inode_free_security =		selinux_inode_free_security,
42645e41ff9eSStephen Smalley 	.inode_init_security =		selinux_inode_init_security,
42651da177e4SLinus Torvalds 	.inode_create =			selinux_inode_create,
42661da177e4SLinus Torvalds 	.inode_link =			selinux_inode_link,
42671da177e4SLinus Torvalds 	.inode_unlink =			selinux_inode_unlink,
42681da177e4SLinus Torvalds 	.inode_symlink =		selinux_inode_symlink,
42691da177e4SLinus Torvalds 	.inode_mkdir =			selinux_inode_mkdir,
42701da177e4SLinus Torvalds 	.inode_rmdir =			selinux_inode_rmdir,
42711da177e4SLinus Torvalds 	.inode_mknod =			selinux_inode_mknod,
42721da177e4SLinus Torvalds 	.inode_rename =			selinux_inode_rename,
42731da177e4SLinus Torvalds 	.inode_readlink =		selinux_inode_readlink,
42741da177e4SLinus Torvalds 	.inode_follow_link =		selinux_inode_follow_link,
42751da177e4SLinus Torvalds 	.inode_permission =		selinux_inode_permission,
42761da177e4SLinus Torvalds 	.inode_setattr =		selinux_inode_setattr,
42771da177e4SLinus Torvalds 	.inode_getattr =		selinux_inode_getattr,
42781da177e4SLinus Torvalds 	.inode_setxattr =		selinux_inode_setxattr,
42791da177e4SLinus Torvalds 	.inode_post_setxattr =		selinux_inode_post_setxattr,
42801da177e4SLinus Torvalds 	.inode_getxattr =		selinux_inode_getxattr,
42811da177e4SLinus Torvalds 	.inode_listxattr =		selinux_inode_listxattr,
42821da177e4SLinus Torvalds 	.inode_removexattr =		selinux_inode_removexattr,
42838c8570fbSDustin Kirkland 	.inode_xattr_getsuffix =        selinux_inode_xattr_getsuffix,
42841da177e4SLinus Torvalds 	.inode_getsecurity =            selinux_inode_getsecurity,
42851da177e4SLinus Torvalds 	.inode_setsecurity =            selinux_inode_setsecurity,
42861da177e4SLinus Torvalds 	.inode_listsecurity =           selinux_inode_listsecurity,
42871da177e4SLinus Torvalds 
42881da177e4SLinus Torvalds 	.file_permission =		selinux_file_permission,
42891da177e4SLinus Torvalds 	.file_alloc_security =		selinux_file_alloc_security,
42901da177e4SLinus Torvalds 	.file_free_security =		selinux_file_free_security,
42911da177e4SLinus Torvalds 	.file_ioctl =			selinux_file_ioctl,
42921da177e4SLinus Torvalds 	.file_mmap =			selinux_file_mmap,
42931da177e4SLinus Torvalds 	.file_mprotect =		selinux_file_mprotect,
42941da177e4SLinus Torvalds 	.file_lock =			selinux_file_lock,
42951da177e4SLinus Torvalds 	.file_fcntl =			selinux_file_fcntl,
42961da177e4SLinus Torvalds 	.file_set_fowner =		selinux_file_set_fowner,
42971da177e4SLinus Torvalds 	.file_send_sigiotask =		selinux_file_send_sigiotask,
42981da177e4SLinus Torvalds 	.file_receive =			selinux_file_receive,
42991da177e4SLinus Torvalds 
43001da177e4SLinus Torvalds 	.task_create =			selinux_task_create,
43011da177e4SLinus Torvalds 	.task_alloc_security =		selinux_task_alloc_security,
43021da177e4SLinus Torvalds 	.task_free_security =		selinux_task_free_security,
43031da177e4SLinus Torvalds 	.task_setuid =			selinux_task_setuid,
43041da177e4SLinus Torvalds 	.task_post_setuid =		selinux_task_post_setuid,
43051da177e4SLinus Torvalds 	.task_setgid =			selinux_task_setgid,
43061da177e4SLinus Torvalds 	.task_setpgid =			selinux_task_setpgid,
43071da177e4SLinus Torvalds 	.task_getpgid =			selinux_task_getpgid,
43081da177e4SLinus Torvalds 	.task_getsid =		        selinux_task_getsid,
43091da177e4SLinus Torvalds 	.task_setgroups =		selinux_task_setgroups,
43101da177e4SLinus Torvalds 	.task_setnice =			selinux_task_setnice,
43111da177e4SLinus Torvalds 	.task_setrlimit =		selinux_task_setrlimit,
43121da177e4SLinus Torvalds 	.task_setscheduler =		selinux_task_setscheduler,
43131da177e4SLinus Torvalds 	.task_getscheduler =		selinux_task_getscheduler,
43141da177e4SLinus Torvalds 	.task_kill =			selinux_task_kill,
43151da177e4SLinus Torvalds 	.task_wait =			selinux_task_wait,
43161da177e4SLinus Torvalds 	.task_prctl =			selinux_task_prctl,
43171da177e4SLinus Torvalds 	.task_reparent_to_init =	selinux_task_reparent_to_init,
43181da177e4SLinus Torvalds 	.task_to_inode =                selinux_task_to_inode,
43191da177e4SLinus Torvalds 
43201da177e4SLinus Torvalds 	.ipc_permission =		selinux_ipc_permission,
43211da177e4SLinus Torvalds 
43221da177e4SLinus Torvalds 	.msg_msg_alloc_security =	selinux_msg_msg_alloc_security,
43231da177e4SLinus Torvalds 	.msg_msg_free_security =	selinux_msg_msg_free_security,
43241da177e4SLinus Torvalds 
43251da177e4SLinus Torvalds 	.msg_queue_alloc_security =	selinux_msg_queue_alloc_security,
43261da177e4SLinus Torvalds 	.msg_queue_free_security =	selinux_msg_queue_free_security,
43271da177e4SLinus Torvalds 	.msg_queue_associate =		selinux_msg_queue_associate,
43281da177e4SLinus Torvalds 	.msg_queue_msgctl =		selinux_msg_queue_msgctl,
43291da177e4SLinus Torvalds 	.msg_queue_msgsnd =		selinux_msg_queue_msgsnd,
43301da177e4SLinus Torvalds 	.msg_queue_msgrcv =		selinux_msg_queue_msgrcv,
43311da177e4SLinus Torvalds 
43321da177e4SLinus Torvalds 	.shm_alloc_security =		selinux_shm_alloc_security,
43331da177e4SLinus Torvalds 	.shm_free_security =		selinux_shm_free_security,
43341da177e4SLinus Torvalds 	.shm_associate =		selinux_shm_associate,
43351da177e4SLinus Torvalds 	.shm_shmctl =			selinux_shm_shmctl,
43361da177e4SLinus Torvalds 	.shm_shmat =			selinux_shm_shmat,
43371da177e4SLinus Torvalds 
43381da177e4SLinus Torvalds 	.sem_alloc_security = 		selinux_sem_alloc_security,
43391da177e4SLinus Torvalds 	.sem_free_security =  		selinux_sem_free_security,
43401da177e4SLinus Torvalds 	.sem_associate =		selinux_sem_associate,
43411da177e4SLinus Torvalds 	.sem_semctl =			selinux_sem_semctl,
43421da177e4SLinus Torvalds 	.sem_semop =			selinux_sem_semop,
43431da177e4SLinus Torvalds 
43441da177e4SLinus Torvalds 	.register_security =		selinux_register_security,
43451da177e4SLinus Torvalds 	.unregister_security =		selinux_unregister_security,
43461da177e4SLinus Torvalds 
43471da177e4SLinus Torvalds 	.d_instantiate =                selinux_d_instantiate,
43481da177e4SLinus Torvalds 
43491da177e4SLinus Torvalds 	.getprocattr =                  selinux_getprocattr,
43501da177e4SLinus Torvalds 	.setprocattr =                  selinux_setprocattr,
43511da177e4SLinus Torvalds 
43521da177e4SLinus Torvalds         .unix_stream_connect =		selinux_socket_unix_stream_connect,
43531da177e4SLinus Torvalds 	.unix_may_send =		selinux_socket_unix_may_send,
43541da177e4SLinus Torvalds 
43551da177e4SLinus Torvalds 	.socket_create =		selinux_socket_create,
43561da177e4SLinus Torvalds 	.socket_post_create =		selinux_socket_post_create,
43571da177e4SLinus Torvalds 	.socket_bind =			selinux_socket_bind,
43581da177e4SLinus Torvalds 	.socket_connect =		selinux_socket_connect,
43591da177e4SLinus Torvalds 	.socket_listen =		selinux_socket_listen,
43601da177e4SLinus Torvalds 	.socket_accept =		selinux_socket_accept,
43611da177e4SLinus Torvalds 	.socket_sendmsg =		selinux_socket_sendmsg,
43621da177e4SLinus Torvalds 	.socket_recvmsg =		selinux_socket_recvmsg,
43631da177e4SLinus Torvalds 	.socket_getsockname =		selinux_socket_getsockname,
43641da177e4SLinus Torvalds 	.socket_getpeername =		selinux_socket_getpeername,
43651da177e4SLinus Torvalds 	.socket_getsockopt =		selinux_socket_getsockopt,
43661da177e4SLinus Torvalds 	.socket_setsockopt =		selinux_socket_setsockopt,
43671da177e4SLinus Torvalds 	.socket_shutdown =		selinux_socket_shutdown,
43681da177e4SLinus Torvalds 	.socket_sock_rcv_skb =		selinux_socket_sock_rcv_skb,
43692c7946a7SCatherine Zhang 	.socket_getpeersec_stream =	selinux_socket_getpeersec_stream,
43702c7946a7SCatherine Zhang 	.socket_getpeersec_dgram =	selinux_socket_getpeersec_dgram,
43711da177e4SLinus Torvalds 	.sk_alloc_security =		selinux_sk_alloc_security,
43721da177e4SLinus Torvalds 	.sk_free_security =		selinux_sk_free_security,
4373d28d1e08STrent Jaeger 	.sk_getsid = 			selinux_sk_getsid_security,
4374d28d1e08STrent Jaeger 
4375d28d1e08STrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM
4376d28d1e08STrent Jaeger 	.xfrm_policy_alloc_security =	selinux_xfrm_policy_alloc,
4377d28d1e08STrent Jaeger 	.xfrm_policy_clone_security =	selinux_xfrm_policy_clone,
4378d28d1e08STrent Jaeger 	.xfrm_policy_free_security =	selinux_xfrm_policy_free,
4379c8c05a8eSCatherine Zhang 	.xfrm_policy_delete_security =	selinux_xfrm_policy_delete,
4380d28d1e08STrent Jaeger 	.xfrm_state_alloc_security =	selinux_xfrm_state_alloc,
4381d28d1e08STrent Jaeger 	.xfrm_state_free_security =	selinux_xfrm_state_free,
4382c8c05a8eSCatherine Zhang 	.xfrm_state_delete_security =	selinux_xfrm_state_delete,
4383d28d1e08STrent Jaeger 	.xfrm_policy_lookup = 		selinux_xfrm_policy_lookup,
43841da177e4SLinus Torvalds #endif
43851da177e4SLinus Torvalds };
43861da177e4SLinus Torvalds 
43871da177e4SLinus Torvalds static __init int selinux_init(void)
43881da177e4SLinus Torvalds {
43891da177e4SLinus Torvalds 	struct task_security_struct *tsec;
43901da177e4SLinus Torvalds 
43911da177e4SLinus Torvalds 	if (!selinux_enabled) {
43921da177e4SLinus Torvalds 		printk(KERN_INFO "SELinux:  Disabled at boot.\n");
43931da177e4SLinus Torvalds 		return 0;
43941da177e4SLinus Torvalds 	}
43951da177e4SLinus Torvalds 
43961da177e4SLinus Torvalds 	printk(KERN_INFO "SELinux:  Initializing.\n");
43971da177e4SLinus Torvalds 
43981da177e4SLinus Torvalds 	/* Set the security state for the initial task. */
43991da177e4SLinus Torvalds 	if (task_alloc_security(current))
44001da177e4SLinus Torvalds 		panic("SELinux:  Failed to initialize initial task.\n");
44011da177e4SLinus Torvalds 	tsec = current->security;
44021da177e4SLinus Torvalds 	tsec->osid = tsec->sid = SECINITSID_KERNEL;
44031da177e4SLinus Torvalds 
44047cae7e26SJames Morris 	sel_inode_cache = kmem_cache_create("selinux_inode_security",
44057cae7e26SJames Morris 					    sizeof(struct inode_security_struct),
44067cae7e26SJames Morris 					    0, SLAB_PANIC, NULL, NULL);
44071da177e4SLinus Torvalds 	avc_init();
44081da177e4SLinus Torvalds 
44091da177e4SLinus Torvalds 	original_ops = secondary_ops = security_ops;
44101da177e4SLinus Torvalds 	if (!secondary_ops)
44111da177e4SLinus Torvalds 		panic ("SELinux: No initial security operations\n");
44121da177e4SLinus Torvalds 	if (register_security (&selinux_ops))
44131da177e4SLinus Torvalds 		panic("SELinux: Unable to register with kernel.\n");
44141da177e4SLinus Torvalds 
44151da177e4SLinus Torvalds 	if (selinux_enforcing) {
44161da177e4SLinus Torvalds 		printk(KERN_INFO "SELinux:  Starting in enforcing mode\n");
44171da177e4SLinus Torvalds 	} else {
44181da177e4SLinus Torvalds 		printk(KERN_INFO "SELinux:  Starting in permissive mode\n");
44191da177e4SLinus Torvalds 	}
44201da177e4SLinus Torvalds 	return 0;
44211da177e4SLinus Torvalds }
44221da177e4SLinus Torvalds 
44231da177e4SLinus Torvalds void selinux_complete_init(void)
44241da177e4SLinus Torvalds {
44251da177e4SLinus Torvalds 	printk(KERN_INFO "SELinux:  Completing initialization.\n");
44261da177e4SLinus Torvalds 
44271da177e4SLinus Torvalds 	/* Set up any superblocks initialized prior to the policy load. */
44281da177e4SLinus Torvalds 	printk(KERN_INFO "SELinux:  Setting up existing superblocks.\n");
4429ba0c19edSStephen Smalley 	spin_lock(&sb_lock);
44301da177e4SLinus Torvalds 	spin_lock(&sb_security_lock);
44311da177e4SLinus Torvalds next_sb:
44321da177e4SLinus Torvalds 	if (!list_empty(&superblock_security_head)) {
44331da177e4SLinus Torvalds 		struct superblock_security_struct *sbsec =
44341da177e4SLinus Torvalds 				list_entry(superblock_security_head.next,
44351da177e4SLinus Torvalds 				           struct superblock_security_struct,
44361da177e4SLinus Torvalds 				           list);
44371da177e4SLinus Torvalds 		struct super_block *sb = sbsec->sb;
44381da177e4SLinus Torvalds 		sb->s_count++;
44391da177e4SLinus Torvalds 		spin_unlock(&sb_security_lock);
4440ba0c19edSStephen Smalley 		spin_unlock(&sb_lock);
44411da177e4SLinus Torvalds 		down_read(&sb->s_umount);
44421da177e4SLinus Torvalds 		if (sb->s_root)
44431da177e4SLinus Torvalds 			superblock_doinit(sb, NULL);
44441da177e4SLinus Torvalds 		drop_super(sb);
4445ba0c19edSStephen Smalley 		spin_lock(&sb_lock);
44461da177e4SLinus Torvalds 		spin_lock(&sb_security_lock);
44471da177e4SLinus Torvalds 		list_del_init(&sbsec->list);
44481da177e4SLinus Torvalds 		goto next_sb;
44491da177e4SLinus Torvalds 	}
44501da177e4SLinus Torvalds 	spin_unlock(&sb_security_lock);
4451ba0c19edSStephen Smalley 	spin_unlock(&sb_lock);
44521da177e4SLinus Torvalds }
44531da177e4SLinus Torvalds 
44541da177e4SLinus Torvalds /* SELinux requires early initialization in order to label
44551da177e4SLinus Torvalds    all processes and objects when they are created. */
44561da177e4SLinus Torvalds security_initcall(selinux_init);
44571da177e4SLinus Torvalds 
4458c2b507fdSStephen Smalley #if defined(CONFIG_NETFILTER)
44591da177e4SLinus Torvalds 
44601da177e4SLinus Torvalds static struct nf_hook_ops selinux_ipv4_op = {
44611da177e4SLinus Torvalds 	.hook =		selinux_ipv4_postroute_last,
44621da177e4SLinus Torvalds 	.owner =	THIS_MODULE,
44631da177e4SLinus Torvalds 	.pf =		PF_INET,
44641da177e4SLinus Torvalds 	.hooknum =	NF_IP_POST_ROUTING,
44651da177e4SLinus Torvalds 	.priority =	NF_IP_PRI_SELINUX_LAST,
44661da177e4SLinus Torvalds };
44671da177e4SLinus Torvalds 
44681da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
44691da177e4SLinus Torvalds 
44701da177e4SLinus Torvalds static struct nf_hook_ops selinux_ipv6_op = {
44711da177e4SLinus Torvalds 	.hook =		selinux_ipv6_postroute_last,
44721da177e4SLinus Torvalds 	.owner =	THIS_MODULE,
44731da177e4SLinus Torvalds 	.pf =		PF_INET6,
44741da177e4SLinus Torvalds 	.hooknum =	NF_IP6_POST_ROUTING,
44751da177e4SLinus Torvalds 	.priority =	NF_IP6_PRI_SELINUX_LAST,
44761da177e4SLinus Torvalds };
44771da177e4SLinus Torvalds 
44781da177e4SLinus Torvalds #endif	/* IPV6 */
44791da177e4SLinus Torvalds 
44801da177e4SLinus Torvalds static int __init selinux_nf_ip_init(void)
44811da177e4SLinus Torvalds {
44821da177e4SLinus Torvalds 	int err = 0;
44831da177e4SLinus Torvalds 
44841da177e4SLinus Torvalds 	if (!selinux_enabled)
44851da177e4SLinus Torvalds 		goto out;
44861da177e4SLinus Torvalds 
44871da177e4SLinus Torvalds 	printk(KERN_INFO "SELinux:  Registering netfilter hooks\n");
44881da177e4SLinus Torvalds 
44891da177e4SLinus Torvalds 	err = nf_register_hook(&selinux_ipv4_op);
44901da177e4SLinus Torvalds 	if (err)
44911da177e4SLinus Torvalds 		panic("SELinux: nf_register_hook for IPv4: error %d\n", err);
44921da177e4SLinus Torvalds 
44931da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
44941da177e4SLinus Torvalds 
44951da177e4SLinus Torvalds 	err = nf_register_hook(&selinux_ipv6_op);
44961da177e4SLinus Torvalds 	if (err)
44971da177e4SLinus Torvalds 		panic("SELinux: nf_register_hook for IPv6: error %d\n", err);
44981da177e4SLinus Torvalds 
44991da177e4SLinus Torvalds #endif	/* IPV6 */
4500d28d1e08STrent Jaeger 
45011da177e4SLinus Torvalds out:
45021da177e4SLinus Torvalds 	return err;
45031da177e4SLinus Torvalds }
45041da177e4SLinus Torvalds 
45051da177e4SLinus Torvalds __initcall(selinux_nf_ip_init);
45061da177e4SLinus Torvalds 
45071da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
45081da177e4SLinus Torvalds static void selinux_nf_ip_exit(void)
45091da177e4SLinus Torvalds {
45101da177e4SLinus Torvalds 	printk(KERN_INFO "SELinux:  Unregistering netfilter hooks\n");
45111da177e4SLinus Torvalds 
45121da177e4SLinus Torvalds 	nf_unregister_hook(&selinux_ipv4_op);
45131da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
45141da177e4SLinus Torvalds 	nf_unregister_hook(&selinux_ipv6_op);
45151da177e4SLinus Torvalds #endif	/* IPV6 */
45161da177e4SLinus Torvalds }
45171da177e4SLinus Torvalds #endif
45181da177e4SLinus Torvalds 
4519c2b507fdSStephen Smalley #else /* CONFIG_NETFILTER */
45201da177e4SLinus Torvalds 
45211da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
45221da177e4SLinus Torvalds #define selinux_nf_ip_exit()
45231da177e4SLinus Torvalds #endif
45241da177e4SLinus Torvalds 
4525c2b507fdSStephen Smalley #endif /* CONFIG_NETFILTER */
45261da177e4SLinus Torvalds 
45271da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
45281da177e4SLinus Torvalds int selinux_disable(void)
45291da177e4SLinus Torvalds {
45301da177e4SLinus Torvalds 	extern void exit_sel_fs(void);
45311da177e4SLinus Torvalds 	static int selinux_disabled = 0;
45321da177e4SLinus Torvalds 
45331da177e4SLinus Torvalds 	if (ss_initialized) {
45341da177e4SLinus Torvalds 		/* Not permitted after initial policy load. */
45351da177e4SLinus Torvalds 		return -EINVAL;
45361da177e4SLinus Torvalds 	}
45371da177e4SLinus Torvalds 
45381da177e4SLinus Torvalds 	if (selinux_disabled) {
45391da177e4SLinus Torvalds 		/* Only do this once. */
45401da177e4SLinus Torvalds 		return -EINVAL;
45411da177e4SLinus Torvalds 	}
45421da177e4SLinus Torvalds 
45431da177e4SLinus Torvalds 	printk(KERN_INFO "SELinux:  Disabled at runtime.\n");
45441da177e4SLinus Torvalds 
45451da177e4SLinus Torvalds 	selinux_disabled = 1;
454630d55280SStephen Smalley 	selinux_enabled = 0;
45471da177e4SLinus Torvalds 
45481da177e4SLinus Torvalds 	/* Reset security_ops to the secondary module, dummy or capability. */
45491da177e4SLinus Torvalds 	security_ops = secondary_ops;
45501da177e4SLinus Torvalds 
45511da177e4SLinus Torvalds 	/* Unregister netfilter hooks. */
45521da177e4SLinus Torvalds 	selinux_nf_ip_exit();
45531da177e4SLinus Torvalds 
45541da177e4SLinus Torvalds 	/* Unregister selinuxfs. */
45551da177e4SLinus Torvalds 	exit_sel_fs();
45561da177e4SLinus Torvalds 
45571da177e4SLinus Torvalds 	return 0;
45581da177e4SLinus Torvalds }
45591da177e4SLinus Torvalds #endif
45601da177e4SLinus Torvalds 
45611da177e4SLinus Torvalds 
4562