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