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