11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * NSA Security-Enhanced Linux (SELinux) security module 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * This file contains the SELinux hook function implementations. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Authors: Stephen Smalley, <sds@epoch.ncsc.mil> 71da177e4SLinus Torvalds * Chris Vance, <cvance@nai.com> 81da177e4SLinus Torvalds * Wayne Salamon, <wsalamon@nai.com> 91da177e4SLinus Torvalds * James Morris <jmorris@redhat.com> 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * Copyright (C) 2001,2002 Networks Associates Technology, Inc. 121da177e4SLinus Torvalds * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com> 131da177e4SLinus Torvalds * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc. 141da177e4SLinus Torvalds * <dgoeddel@trustedcs.com> 151da177e4SLinus Torvalds * 161da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 171da177e4SLinus Torvalds * it under the terms of the GNU General Public License version 2, 181da177e4SLinus Torvalds * as published by the Free Software Foundation. 191da177e4SLinus Torvalds */ 201da177e4SLinus Torvalds 211da177e4SLinus Torvalds #include <linux/config.h> 221da177e4SLinus Torvalds #include <linux/module.h> 231da177e4SLinus Torvalds #include <linux/init.h> 241da177e4SLinus Torvalds #include <linux/kernel.h> 251da177e4SLinus Torvalds #include <linux/ptrace.h> 261da177e4SLinus Torvalds #include <linux/errno.h> 271da177e4SLinus Torvalds #include <linux/sched.h> 281da177e4SLinus Torvalds #include <linux/security.h> 291da177e4SLinus Torvalds #include <linux/xattr.h> 301da177e4SLinus Torvalds #include <linux/capability.h> 311da177e4SLinus Torvalds #include <linux/unistd.h> 321da177e4SLinus Torvalds #include <linux/mm.h> 331da177e4SLinus Torvalds #include <linux/mman.h> 341da177e4SLinus Torvalds #include <linux/slab.h> 351da177e4SLinus Torvalds #include <linux/pagemap.h> 361da177e4SLinus Torvalds #include <linux/swap.h> 371da177e4SLinus Torvalds #include <linux/smp_lock.h> 381da177e4SLinus Torvalds #include <linux/spinlock.h> 391da177e4SLinus Torvalds #include <linux/syscalls.h> 401da177e4SLinus Torvalds #include <linux/file.h> 411da177e4SLinus Torvalds #include <linux/namei.h> 421da177e4SLinus Torvalds #include <linux/mount.h> 431da177e4SLinus Torvalds #include <linux/ext2_fs.h> 441da177e4SLinus Torvalds #include <linux/proc_fs.h> 451da177e4SLinus Torvalds #include <linux/kd.h> 461da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h> 471da177e4SLinus Torvalds #include <linux/netfilter_ipv6.h> 481da177e4SLinus Torvalds #include <linux/tty.h> 491da177e4SLinus Torvalds #include <net/icmp.h> 501da177e4SLinus Torvalds #include <net/ip.h> /* for sysctl_local_port_range[] */ 511da177e4SLinus Torvalds #include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */ 521da177e4SLinus Torvalds #include <asm/uaccess.h> 531da177e4SLinus Torvalds #include <asm/semaphore.h> 541da177e4SLinus Torvalds #include <asm/ioctls.h> 551da177e4SLinus Torvalds #include <linux/bitops.h> 561da177e4SLinus Torvalds #include <linux/interrupt.h> 571da177e4SLinus Torvalds #include <linux/netdevice.h> /* for network interface checks */ 581da177e4SLinus Torvalds #include <linux/netlink.h> 591da177e4SLinus Torvalds #include <linux/tcp.h> 601da177e4SLinus Torvalds #include <linux/udp.h> 611da177e4SLinus Torvalds #include <linux/quota.h> 621da177e4SLinus Torvalds #include <linux/un.h> /* for Unix socket types */ 631da177e4SLinus Torvalds #include <net/af_unix.h> /* for Unix socket types */ 641da177e4SLinus Torvalds #include <linux/parser.h> 651da177e4SLinus Torvalds #include <linux/nfs_mount.h> 661da177e4SLinus Torvalds #include <net/ipv6.h> 671da177e4SLinus Torvalds #include <linux/hugetlb.h> 681da177e4SLinus Torvalds #include <linux/personality.h> 691da177e4SLinus Torvalds #include <linux/sysctl.h> 701da177e4SLinus Torvalds #include <linux/audit.h> 716931dfc9SEric Paris #include <linux/string.h> 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds #include "avc.h" 741da177e4SLinus Torvalds #include "objsec.h" 751da177e4SLinus Torvalds #include "netif.h" 76d28d1e08STrent Jaeger #include "xfrm.h" 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds #define XATTR_SELINUX_SUFFIX "selinux" 791da177e4SLinus Torvalds #define XATTR_NAME_SELINUX XATTR_SECURITY_PREFIX XATTR_SELINUX_SUFFIX 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds extern unsigned int policydb_loaded_version; 821da177e4SLinus Torvalds extern int selinux_nlmsg_lookup(u16 sclass, u16 nlmsg_type, u32 *perm); 834e5ab4cbSJames Morris extern int selinux_compat_net; 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP 861da177e4SLinus Torvalds int selinux_enforcing = 0; 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds static int __init enforcing_setup(char *str) 891da177e4SLinus Torvalds { 901da177e4SLinus Torvalds selinux_enforcing = simple_strtol(str,NULL,0); 911da177e4SLinus Torvalds return 1; 921da177e4SLinus Torvalds } 931da177e4SLinus Torvalds __setup("enforcing=", enforcing_setup); 941da177e4SLinus Torvalds #endif 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM 971da177e4SLinus Torvalds int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE; 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds static int __init selinux_enabled_setup(char *str) 1001da177e4SLinus Torvalds { 1011da177e4SLinus Torvalds selinux_enabled = simple_strtol(str, NULL, 0); 1021da177e4SLinus Torvalds return 1; 1031da177e4SLinus Torvalds } 1041da177e4SLinus Torvalds __setup("selinux=", selinux_enabled_setup); 10530d55280SStephen Smalley #else 10630d55280SStephen Smalley int selinux_enabled = 1; 1071da177e4SLinus Torvalds #endif 1081da177e4SLinus Torvalds 1091da177e4SLinus Torvalds /* Original (dummy) security module. */ 1101da177e4SLinus Torvalds static struct security_operations *original_ops = NULL; 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds /* Minimal support for a secondary security module, 1131da177e4SLinus Torvalds just to allow the use of the dummy or capability modules. 1141da177e4SLinus Torvalds The owlsm module can alternatively be used as a secondary 1151da177e4SLinus Torvalds module as long as CONFIG_OWLSM_FD is not enabled. */ 1161da177e4SLinus Torvalds static struct security_operations *secondary_ops = NULL; 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds /* Lists of inode and superblock security structures initialized 1191da177e4SLinus Torvalds before the policy was loaded. */ 1201da177e4SLinus Torvalds static LIST_HEAD(superblock_security_head); 1211da177e4SLinus Torvalds static DEFINE_SPINLOCK(sb_security_lock); 1221da177e4SLinus Torvalds 1237cae7e26SJames Morris static kmem_cache_t *sel_inode_cache; 1247cae7e26SJames Morris 1258c8570fbSDustin Kirkland /* Return security context for a given sid or just the context 1268c8570fbSDustin Kirkland length if the buffer is null or length is 0 */ 1278c8570fbSDustin Kirkland static int selinux_getsecurity(u32 sid, void *buffer, size_t size) 1288c8570fbSDustin Kirkland { 1298c8570fbSDustin Kirkland char *context; 1308c8570fbSDustin Kirkland unsigned len; 1318c8570fbSDustin Kirkland int rc; 1328c8570fbSDustin Kirkland 1338c8570fbSDustin Kirkland rc = security_sid_to_context(sid, &context, &len); 1348c8570fbSDustin Kirkland if (rc) 1358c8570fbSDustin Kirkland return rc; 1368c8570fbSDustin Kirkland 1378c8570fbSDustin Kirkland if (!buffer || !size) 1388c8570fbSDustin Kirkland goto getsecurity_exit; 1398c8570fbSDustin Kirkland 1408c8570fbSDustin Kirkland if (size < len) { 1418c8570fbSDustin Kirkland len = -ERANGE; 1428c8570fbSDustin Kirkland goto getsecurity_exit; 1438c8570fbSDustin Kirkland } 1448c8570fbSDustin Kirkland memcpy(buffer, context, len); 1458c8570fbSDustin Kirkland 1468c8570fbSDustin Kirkland getsecurity_exit: 1478c8570fbSDustin Kirkland kfree(context); 1488c8570fbSDustin Kirkland return len; 1498c8570fbSDustin Kirkland } 1508c8570fbSDustin Kirkland 1511da177e4SLinus Torvalds /* Allocate and free functions for each kind of security blob. */ 1521da177e4SLinus Torvalds 1531da177e4SLinus Torvalds static int task_alloc_security(struct task_struct *task) 1541da177e4SLinus Torvalds { 1551da177e4SLinus Torvalds struct task_security_struct *tsec; 1561da177e4SLinus Torvalds 15789d155efSJames Morris tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL); 1581da177e4SLinus Torvalds if (!tsec) 1591da177e4SLinus Torvalds return -ENOMEM; 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds tsec->task = task; 1621da177e4SLinus Torvalds tsec->osid = tsec->sid = tsec->ptrace_sid = SECINITSID_UNLABELED; 1631da177e4SLinus Torvalds task->security = tsec; 1641da177e4SLinus Torvalds 1651da177e4SLinus Torvalds return 0; 1661da177e4SLinus Torvalds } 1671da177e4SLinus Torvalds 1681da177e4SLinus Torvalds static void task_free_security(struct task_struct *task) 1691da177e4SLinus Torvalds { 1701da177e4SLinus Torvalds struct task_security_struct *tsec = task->security; 1711da177e4SLinus Torvalds task->security = NULL; 1721da177e4SLinus Torvalds kfree(tsec); 1731da177e4SLinus Torvalds } 1741da177e4SLinus Torvalds 1751da177e4SLinus Torvalds static int inode_alloc_security(struct inode *inode) 1761da177e4SLinus Torvalds { 1771da177e4SLinus Torvalds struct task_security_struct *tsec = current->security; 1781da177e4SLinus Torvalds struct inode_security_struct *isec; 1791da177e4SLinus Torvalds 1807cae7e26SJames Morris isec = kmem_cache_alloc(sel_inode_cache, SLAB_KERNEL); 1811da177e4SLinus Torvalds if (!isec) 1821da177e4SLinus Torvalds return -ENOMEM; 1831da177e4SLinus Torvalds 1847cae7e26SJames Morris memset(isec, 0, sizeof(*isec)); 1851da177e4SLinus Torvalds init_MUTEX(&isec->sem); 1861da177e4SLinus Torvalds INIT_LIST_HEAD(&isec->list); 1871da177e4SLinus Torvalds isec->inode = inode; 1881da177e4SLinus Torvalds isec->sid = SECINITSID_UNLABELED; 1891da177e4SLinus Torvalds isec->sclass = SECCLASS_FILE; 1901da177e4SLinus Torvalds isec->task_sid = tsec->sid; 1911da177e4SLinus Torvalds inode->i_security = isec; 1921da177e4SLinus Torvalds 1931da177e4SLinus Torvalds return 0; 1941da177e4SLinus Torvalds } 1951da177e4SLinus Torvalds 1961da177e4SLinus Torvalds static void inode_free_security(struct inode *inode) 1971da177e4SLinus Torvalds { 1981da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 1991da177e4SLinus Torvalds struct superblock_security_struct *sbsec = inode->i_sb->s_security; 2001da177e4SLinus Torvalds 2011da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 2021da177e4SLinus Torvalds if (!list_empty(&isec->list)) 2031da177e4SLinus Torvalds list_del_init(&isec->list); 2041da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 2051da177e4SLinus Torvalds 2061da177e4SLinus Torvalds inode->i_security = NULL; 2077cae7e26SJames Morris kmem_cache_free(sel_inode_cache, isec); 2081da177e4SLinus Torvalds } 2091da177e4SLinus Torvalds 2101da177e4SLinus Torvalds static int file_alloc_security(struct file *file) 2111da177e4SLinus Torvalds { 2121da177e4SLinus Torvalds struct task_security_struct *tsec = current->security; 2131da177e4SLinus Torvalds struct file_security_struct *fsec; 2141da177e4SLinus Torvalds 21526d2a4beSStephen Smalley fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL); 2161da177e4SLinus Torvalds if (!fsec) 2171da177e4SLinus Torvalds return -ENOMEM; 2181da177e4SLinus Torvalds 2191da177e4SLinus Torvalds fsec->file = file; 2201da177e4SLinus Torvalds fsec->sid = tsec->sid; 2211da177e4SLinus Torvalds fsec->fown_sid = tsec->sid; 2221da177e4SLinus Torvalds file->f_security = fsec; 2231da177e4SLinus Torvalds 2241da177e4SLinus Torvalds return 0; 2251da177e4SLinus Torvalds } 2261da177e4SLinus Torvalds 2271da177e4SLinus Torvalds static void file_free_security(struct file *file) 2281da177e4SLinus Torvalds { 2291da177e4SLinus Torvalds struct file_security_struct *fsec = file->f_security; 2301da177e4SLinus Torvalds file->f_security = NULL; 2311da177e4SLinus Torvalds kfree(fsec); 2321da177e4SLinus Torvalds } 2331da177e4SLinus Torvalds 2341da177e4SLinus Torvalds static int superblock_alloc_security(struct super_block *sb) 2351da177e4SLinus Torvalds { 2361da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 2371da177e4SLinus Torvalds 23889d155efSJames Morris sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL); 2391da177e4SLinus Torvalds if (!sbsec) 2401da177e4SLinus Torvalds return -ENOMEM; 2411da177e4SLinus Torvalds 2421da177e4SLinus Torvalds init_MUTEX(&sbsec->sem); 2431da177e4SLinus Torvalds INIT_LIST_HEAD(&sbsec->list); 2441da177e4SLinus Torvalds INIT_LIST_HEAD(&sbsec->isec_head); 2451da177e4SLinus Torvalds spin_lock_init(&sbsec->isec_lock); 2461da177e4SLinus Torvalds sbsec->sb = sb; 2471da177e4SLinus Torvalds sbsec->sid = SECINITSID_UNLABELED; 2481da177e4SLinus Torvalds sbsec->def_sid = SECINITSID_FILE; 2491da177e4SLinus Torvalds sb->s_security = sbsec; 2501da177e4SLinus Torvalds 2511da177e4SLinus Torvalds return 0; 2521da177e4SLinus Torvalds } 2531da177e4SLinus Torvalds 2541da177e4SLinus Torvalds static void superblock_free_security(struct super_block *sb) 2551da177e4SLinus Torvalds { 2561da177e4SLinus Torvalds struct superblock_security_struct *sbsec = sb->s_security; 2571da177e4SLinus Torvalds 2581da177e4SLinus Torvalds spin_lock(&sb_security_lock); 2591da177e4SLinus Torvalds if (!list_empty(&sbsec->list)) 2601da177e4SLinus Torvalds list_del_init(&sbsec->list); 2611da177e4SLinus Torvalds spin_unlock(&sb_security_lock); 2621da177e4SLinus Torvalds 2631da177e4SLinus Torvalds sb->s_security = NULL; 2641da177e4SLinus Torvalds kfree(sbsec); 2651da177e4SLinus Torvalds } 2661da177e4SLinus Torvalds 2677d877f3bSAl Viro static int sk_alloc_security(struct sock *sk, int family, gfp_t priority) 2681da177e4SLinus Torvalds { 2691da177e4SLinus Torvalds struct sk_security_struct *ssec; 2701da177e4SLinus Torvalds 2711da177e4SLinus Torvalds if (family != PF_UNIX) 2721da177e4SLinus Torvalds return 0; 2731da177e4SLinus Torvalds 27489d155efSJames Morris ssec = kzalloc(sizeof(*ssec), priority); 2751da177e4SLinus Torvalds if (!ssec) 2761da177e4SLinus Torvalds return -ENOMEM; 2771da177e4SLinus Torvalds 2781da177e4SLinus Torvalds ssec->sk = sk; 2791da177e4SLinus Torvalds ssec->peer_sid = SECINITSID_UNLABELED; 2801da177e4SLinus Torvalds sk->sk_security = ssec; 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds return 0; 2831da177e4SLinus Torvalds } 2841da177e4SLinus Torvalds 2851da177e4SLinus Torvalds static void sk_free_security(struct sock *sk) 2861da177e4SLinus Torvalds { 2871da177e4SLinus Torvalds struct sk_security_struct *ssec = sk->sk_security; 2881da177e4SLinus Torvalds 2899ac49d22SStephen Smalley if (sk->sk_family != PF_UNIX) 2901da177e4SLinus Torvalds return; 2911da177e4SLinus Torvalds 2921da177e4SLinus Torvalds sk->sk_security = NULL; 2931da177e4SLinus Torvalds kfree(ssec); 2941da177e4SLinus Torvalds } 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 63213402580SJames Morris static inline int default_protocol_stream(int protocol) 63313402580SJames Morris { 63413402580SJames Morris return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP); 63513402580SJames Morris } 63613402580SJames Morris 63713402580SJames Morris static inline int default_protocol_dgram(int protocol) 63813402580SJames Morris { 63913402580SJames Morris return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP); 64013402580SJames Morris } 64113402580SJames Morris 6421da177e4SLinus Torvalds static inline u16 socket_type_to_security_class(int family, int type, int protocol) 6431da177e4SLinus Torvalds { 6441da177e4SLinus Torvalds switch (family) { 6451da177e4SLinus Torvalds case PF_UNIX: 6461da177e4SLinus Torvalds switch (type) { 6471da177e4SLinus Torvalds case SOCK_STREAM: 6481da177e4SLinus Torvalds case SOCK_SEQPACKET: 6491da177e4SLinus Torvalds return SECCLASS_UNIX_STREAM_SOCKET; 6501da177e4SLinus Torvalds case SOCK_DGRAM: 6511da177e4SLinus Torvalds return SECCLASS_UNIX_DGRAM_SOCKET; 6521da177e4SLinus Torvalds } 6531da177e4SLinus Torvalds break; 6541da177e4SLinus Torvalds case PF_INET: 6551da177e4SLinus Torvalds case PF_INET6: 6561da177e4SLinus Torvalds switch (type) { 6571da177e4SLinus Torvalds case SOCK_STREAM: 65813402580SJames Morris if (default_protocol_stream(protocol)) 6591da177e4SLinus Torvalds return SECCLASS_TCP_SOCKET; 66013402580SJames Morris else 66113402580SJames Morris return SECCLASS_RAWIP_SOCKET; 6621da177e4SLinus Torvalds case SOCK_DGRAM: 66313402580SJames Morris if (default_protocol_dgram(protocol)) 6641da177e4SLinus Torvalds return SECCLASS_UDP_SOCKET; 66513402580SJames Morris else 66613402580SJames Morris return SECCLASS_RAWIP_SOCKET; 66713402580SJames Morris default: 6681da177e4SLinus Torvalds return SECCLASS_RAWIP_SOCKET; 6691da177e4SLinus Torvalds } 6701da177e4SLinus Torvalds break; 6711da177e4SLinus Torvalds case PF_NETLINK: 6721da177e4SLinus Torvalds switch (protocol) { 6731da177e4SLinus Torvalds case NETLINK_ROUTE: 6741da177e4SLinus Torvalds return SECCLASS_NETLINK_ROUTE_SOCKET; 6751da177e4SLinus Torvalds case NETLINK_FIREWALL: 6761da177e4SLinus Torvalds return SECCLASS_NETLINK_FIREWALL_SOCKET; 677216efaaaSJames Morris case NETLINK_INET_DIAG: 6781da177e4SLinus Torvalds return SECCLASS_NETLINK_TCPDIAG_SOCKET; 6791da177e4SLinus Torvalds case NETLINK_NFLOG: 6801da177e4SLinus Torvalds return SECCLASS_NETLINK_NFLOG_SOCKET; 6811da177e4SLinus Torvalds case NETLINK_XFRM: 6821da177e4SLinus Torvalds return SECCLASS_NETLINK_XFRM_SOCKET; 6831da177e4SLinus Torvalds case NETLINK_SELINUX: 6841da177e4SLinus Torvalds return SECCLASS_NETLINK_SELINUX_SOCKET; 6851da177e4SLinus Torvalds case NETLINK_AUDIT: 6861da177e4SLinus Torvalds return SECCLASS_NETLINK_AUDIT_SOCKET; 6871da177e4SLinus Torvalds case NETLINK_IP6_FW: 6881da177e4SLinus Torvalds return SECCLASS_NETLINK_IP6FW_SOCKET; 6891da177e4SLinus Torvalds case NETLINK_DNRTMSG: 6901da177e4SLinus Torvalds return SECCLASS_NETLINK_DNRT_SOCKET; 6910c9b7942SJames Morris case NETLINK_KOBJECT_UEVENT: 6920c9b7942SJames Morris return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET; 6931da177e4SLinus Torvalds default: 6941da177e4SLinus Torvalds return SECCLASS_NETLINK_SOCKET; 6951da177e4SLinus Torvalds } 6961da177e4SLinus Torvalds case PF_PACKET: 6971da177e4SLinus Torvalds return SECCLASS_PACKET_SOCKET; 6981da177e4SLinus Torvalds case PF_KEY: 6991da177e4SLinus Torvalds return SECCLASS_KEY_SOCKET; 7003e3ff15eSChristopher J. PeBenito case PF_APPLETALK: 7013e3ff15eSChristopher J. PeBenito return SECCLASS_APPLETALK_SOCKET; 7021da177e4SLinus Torvalds } 7031da177e4SLinus Torvalds 7041da177e4SLinus Torvalds return SECCLASS_SOCKET; 7051da177e4SLinus Torvalds } 7061da177e4SLinus Torvalds 7071da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 7081da177e4SLinus Torvalds static int selinux_proc_get_sid(struct proc_dir_entry *de, 7091da177e4SLinus Torvalds u16 tclass, 7101da177e4SLinus Torvalds u32 *sid) 7111da177e4SLinus Torvalds { 7121da177e4SLinus Torvalds int buflen, rc; 7131da177e4SLinus Torvalds char *buffer, *path, *end; 7141da177e4SLinus Torvalds 7151da177e4SLinus Torvalds buffer = (char*)__get_free_page(GFP_KERNEL); 7161da177e4SLinus Torvalds if (!buffer) 7171da177e4SLinus Torvalds return -ENOMEM; 7181da177e4SLinus Torvalds 7191da177e4SLinus Torvalds buflen = PAGE_SIZE; 7201da177e4SLinus Torvalds end = buffer+buflen; 7211da177e4SLinus Torvalds *--end = '\0'; 7221da177e4SLinus Torvalds buflen--; 7231da177e4SLinus Torvalds path = end-1; 7241da177e4SLinus Torvalds *path = '/'; 7251da177e4SLinus Torvalds while (de && de != de->parent) { 7261da177e4SLinus Torvalds buflen -= de->namelen + 1; 7271da177e4SLinus Torvalds if (buflen < 0) 7281da177e4SLinus Torvalds break; 7291da177e4SLinus Torvalds end -= de->namelen; 7301da177e4SLinus Torvalds memcpy(end, de->name, de->namelen); 7311da177e4SLinus Torvalds *--end = '/'; 7321da177e4SLinus Torvalds path = end; 7331da177e4SLinus Torvalds de = de->parent; 7341da177e4SLinus Torvalds } 7351da177e4SLinus Torvalds rc = security_genfs_sid("proc", path, tclass, sid); 7361da177e4SLinus Torvalds free_page((unsigned long)buffer); 7371da177e4SLinus Torvalds return rc; 7381da177e4SLinus Torvalds } 7391da177e4SLinus Torvalds #else 7401da177e4SLinus Torvalds static int selinux_proc_get_sid(struct proc_dir_entry *de, 7411da177e4SLinus Torvalds u16 tclass, 7421da177e4SLinus Torvalds u32 *sid) 7431da177e4SLinus Torvalds { 7441da177e4SLinus Torvalds return -EINVAL; 7451da177e4SLinus Torvalds } 7461da177e4SLinus Torvalds #endif 7471da177e4SLinus Torvalds 7481da177e4SLinus Torvalds /* The inode's security attributes must be initialized before first use. */ 7491da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry) 7501da177e4SLinus Torvalds { 7511da177e4SLinus Torvalds struct superblock_security_struct *sbsec = NULL; 7521da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 7531da177e4SLinus Torvalds u32 sid; 7541da177e4SLinus Torvalds struct dentry *dentry; 7551da177e4SLinus Torvalds #define INITCONTEXTLEN 255 7561da177e4SLinus Torvalds char *context = NULL; 7571da177e4SLinus Torvalds unsigned len = 0; 7581da177e4SLinus Torvalds int rc = 0; 7591da177e4SLinus Torvalds int hold_sem = 0; 7601da177e4SLinus Torvalds 7611da177e4SLinus Torvalds if (isec->initialized) 7621da177e4SLinus Torvalds goto out; 7631da177e4SLinus Torvalds 7641da177e4SLinus Torvalds down(&isec->sem); 7651da177e4SLinus Torvalds hold_sem = 1; 7661da177e4SLinus Torvalds if (isec->initialized) 7671da177e4SLinus Torvalds goto out; 7681da177e4SLinus Torvalds 7691da177e4SLinus Torvalds sbsec = inode->i_sb->s_security; 7701da177e4SLinus Torvalds if (!sbsec->initialized) { 7711da177e4SLinus Torvalds /* Defer initialization until selinux_complete_init, 7721da177e4SLinus Torvalds after the initial policy is loaded and the security 7731da177e4SLinus Torvalds server is ready to handle calls. */ 7741da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 7751da177e4SLinus Torvalds if (list_empty(&isec->list)) 7761da177e4SLinus Torvalds list_add(&isec->list, &sbsec->isec_head); 7771da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 7781da177e4SLinus Torvalds goto out; 7791da177e4SLinus Torvalds } 7801da177e4SLinus Torvalds 7811da177e4SLinus Torvalds switch (sbsec->behavior) { 7821da177e4SLinus Torvalds case SECURITY_FS_USE_XATTR: 7831da177e4SLinus Torvalds if (!inode->i_op->getxattr) { 7841da177e4SLinus Torvalds isec->sid = sbsec->def_sid; 7851da177e4SLinus Torvalds break; 7861da177e4SLinus Torvalds } 7871da177e4SLinus Torvalds 7881da177e4SLinus Torvalds /* Need a dentry, since the xattr API requires one. 7891da177e4SLinus Torvalds Life would be simpler if we could just pass the inode. */ 7901da177e4SLinus Torvalds if (opt_dentry) { 7911da177e4SLinus Torvalds /* Called from d_instantiate or d_splice_alias. */ 7921da177e4SLinus Torvalds dentry = dget(opt_dentry); 7931da177e4SLinus Torvalds } else { 7941da177e4SLinus Torvalds /* Called from selinux_complete_init, try to find a dentry. */ 7951da177e4SLinus Torvalds dentry = d_find_alias(inode); 7961da177e4SLinus Torvalds } 7971da177e4SLinus Torvalds if (!dentry) { 7981da177e4SLinus Torvalds printk(KERN_WARNING "%s: no dentry for dev=%s " 7991da177e4SLinus Torvalds "ino=%ld\n", __FUNCTION__, inode->i_sb->s_id, 8001da177e4SLinus Torvalds inode->i_ino); 8011da177e4SLinus Torvalds goto out; 8021da177e4SLinus Torvalds } 8031da177e4SLinus Torvalds 8041da177e4SLinus Torvalds len = INITCONTEXTLEN; 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, XATTR_NAME_SELINUX, 8121da177e4SLinus Torvalds context, len); 8131da177e4SLinus Torvalds if (rc == -ERANGE) { 8141da177e4SLinus Torvalds /* Need a larger buffer. Query for the right size. */ 8151da177e4SLinus Torvalds rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX, 8161da177e4SLinus Torvalds NULL, 0); 8171da177e4SLinus Torvalds if (rc < 0) { 8181da177e4SLinus Torvalds dput(dentry); 8191da177e4SLinus Torvalds goto out; 8201da177e4SLinus Torvalds } 8211da177e4SLinus Torvalds kfree(context); 8221da177e4SLinus Torvalds len = rc; 8231da177e4SLinus Torvalds context = kmalloc(len, GFP_KERNEL); 8241da177e4SLinus Torvalds if (!context) { 8251da177e4SLinus Torvalds rc = -ENOMEM; 8261da177e4SLinus Torvalds dput(dentry); 8271da177e4SLinus Torvalds goto out; 8281da177e4SLinus Torvalds } 8291da177e4SLinus Torvalds rc = inode->i_op->getxattr(dentry, 8301da177e4SLinus Torvalds XATTR_NAME_SELINUX, 8311da177e4SLinus Torvalds context, len); 8321da177e4SLinus Torvalds } 8331da177e4SLinus Torvalds dput(dentry); 8341da177e4SLinus Torvalds if (rc < 0) { 8351da177e4SLinus Torvalds if (rc != -ENODATA) { 8361da177e4SLinus Torvalds printk(KERN_WARNING "%s: getxattr returned " 8371da177e4SLinus Torvalds "%d for dev=%s ino=%ld\n", __FUNCTION__, 8381da177e4SLinus Torvalds -rc, inode->i_sb->s_id, inode->i_ino); 8391da177e4SLinus Torvalds kfree(context); 8401da177e4SLinus Torvalds goto out; 8411da177e4SLinus Torvalds } 8421da177e4SLinus Torvalds /* Map ENODATA to the default file SID */ 8431da177e4SLinus Torvalds sid = sbsec->def_sid; 8441da177e4SLinus Torvalds rc = 0; 8451da177e4SLinus Torvalds } else { 846f5c1d5b2SJames Morris rc = security_context_to_sid_default(context, rc, &sid, 847f5c1d5b2SJames Morris sbsec->def_sid); 8481da177e4SLinus Torvalds if (rc) { 8491da177e4SLinus Torvalds printk(KERN_WARNING "%s: context_to_sid(%s) " 8501da177e4SLinus Torvalds "returned %d for dev=%s ino=%ld\n", 8511da177e4SLinus Torvalds __FUNCTION__, context, -rc, 8521da177e4SLinus Torvalds inode->i_sb->s_id, inode->i_ino); 8531da177e4SLinus Torvalds kfree(context); 8541da177e4SLinus Torvalds /* Leave with the unlabeled SID */ 8551da177e4SLinus Torvalds rc = 0; 8561da177e4SLinus Torvalds break; 8571da177e4SLinus Torvalds } 8581da177e4SLinus Torvalds } 8591da177e4SLinus Torvalds kfree(context); 8601da177e4SLinus Torvalds isec->sid = sid; 8611da177e4SLinus Torvalds break; 8621da177e4SLinus Torvalds case SECURITY_FS_USE_TASK: 8631da177e4SLinus Torvalds isec->sid = isec->task_sid; 8641da177e4SLinus Torvalds break; 8651da177e4SLinus Torvalds case SECURITY_FS_USE_TRANS: 8661da177e4SLinus Torvalds /* Default to the fs SID. */ 8671da177e4SLinus Torvalds isec->sid = sbsec->sid; 8681da177e4SLinus Torvalds 8691da177e4SLinus Torvalds /* Try to obtain a transition SID. */ 8701da177e4SLinus Torvalds isec->sclass = inode_mode_to_security_class(inode->i_mode); 8711da177e4SLinus Torvalds rc = security_transition_sid(isec->task_sid, 8721da177e4SLinus Torvalds sbsec->sid, 8731da177e4SLinus Torvalds isec->sclass, 8741da177e4SLinus Torvalds &sid); 8751da177e4SLinus Torvalds if (rc) 8761da177e4SLinus Torvalds goto out; 8771da177e4SLinus Torvalds isec->sid = sid; 8781da177e4SLinus Torvalds break; 8791da177e4SLinus Torvalds default: 8801da177e4SLinus Torvalds /* Default to the fs SID. */ 8811da177e4SLinus Torvalds isec->sid = sbsec->sid; 8821da177e4SLinus Torvalds 8831da177e4SLinus Torvalds if (sbsec->proc) { 8841da177e4SLinus Torvalds struct proc_inode *proci = PROC_I(inode); 8851da177e4SLinus Torvalds if (proci->pde) { 8861da177e4SLinus Torvalds isec->sclass = inode_mode_to_security_class(inode->i_mode); 8871da177e4SLinus Torvalds rc = selinux_proc_get_sid(proci->pde, 8881da177e4SLinus Torvalds isec->sclass, 8891da177e4SLinus Torvalds &sid); 8901da177e4SLinus Torvalds if (rc) 8911da177e4SLinus Torvalds goto out; 8921da177e4SLinus Torvalds isec->sid = sid; 8931da177e4SLinus Torvalds } 8941da177e4SLinus Torvalds } 8951da177e4SLinus Torvalds break; 8961da177e4SLinus Torvalds } 8971da177e4SLinus Torvalds 8981da177e4SLinus Torvalds isec->initialized = 1; 8991da177e4SLinus Torvalds 9001da177e4SLinus Torvalds out: 9011da177e4SLinus Torvalds if (isec->sclass == SECCLASS_FILE) 9021da177e4SLinus Torvalds isec->sclass = inode_mode_to_security_class(inode->i_mode); 9031da177e4SLinus Torvalds 9041da177e4SLinus Torvalds if (hold_sem) 9051da177e4SLinus Torvalds up(&isec->sem); 9061da177e4SLinus Torvalds return rc; 9071da177e4SLinus Torvalds } 9081da177e4SLinus Torvalds 9091da177e4SLinus Torvalds /* Convert a Linux signal to an access vector. */ 9101da177e4SLinus Torvalds static inline u32 signal_to_av(int sig) 9111da177e4SLinus Torvalds { 9121da177e4SLinus Torvalds u32 perm = 0; 9131da177e4SLinus Torvalds 9141da177e4SLinus Torvalds switch (sig) { 9151da177e4SLinus Torvalds case SIGCHLD: 9161da177e4SLinus Torvalds /* Commonly granted from child to parent. */ 9171da177e4SLinus Torvalds perm = PROCESS__SIGCHLD; 9181da177e4SLinus Torvalds break; 9191da177e4SLinus Torvalds case SIGKILL: 9201da177e4SLinus Torvalds /* Cannot be caught or ignored */ 9211da177e4SLinus Torvalds perm = PROCESS__SIGKILL; 9221da177e4SLinus Torvalds break; 9231da177e4SLinus Torvalds case SIGSTOP: 9241da177e4SLinus Torvalds /* Cannot be caught or ignored */ 9251da177e4SLinus Torvalds perm = PROCESS__SIGSTOP; 9261da177e4SLinus Torvalds break; 9271da177e4SLinus Torvalds default: 9281da177e4SLinus Torvalds /* All other signals. */ 9291da177e4SLinus Torvalds perm = PROCESS__SIGNAL; 9301da177e4SLinus Torvalds break; 9311da177e4SLinus Torvalds } 9321da177e4SLinus Torvalds 9331da177e4SLinus Torvalds return perm; 9341da177e4SLinus Torvalds } 9351da177e4SLinus Torvalds 9361da177e4SLinus Torvalds /* Check permission betweeen a pair of tasks, e.g. signal checks, 9371da177e4SLinus Torvalds fork check, ptrace check, etc. */ 9381da177e4SLinus Torvalds static int task_has_perm(struct task_struct *tsk1, 9391da177e4SLinus Torvalds struct task_struct *tsk2, 9401da177e4SLinus Torvalds u32 perms) 9411da177e4SLinus Torvalds { 9421da177e4SLinus Torvalds struct task_security_struct *tsec1, *tsec2; 9431da177e4SLinus Torvalds 9441da177e4SLinus Torvalds tsec1 = tsk1->security; 9451da177e4SLinus Torvalds tsec2 = tsk2->security; 9461da177e4SLinus Torvalds return avc_has_perm(tsec1->sid, tsec2->sid, 9471da177e4SLinus Torvalds SECCLASS_PROCESS, perms, NULL); 9481da177e4SLinus Torvalds } 9491da177e4SLinus Torvalds 9501da177e4SLinus Torvalds /* Check whether a task is allowed to use a capability. */ 9511da177e4SLinus Torvalds static int task_has_capability(struct task_struct *tsk, 9521da177e4SLinus Torvalds int cap) 9531da177e4SLinus Torvalds { 9541da177e4SLinus Torvalds struct task_security_struct *tsec; 9551da177e4SLinus Torvalds struct avc_audit_data ad; 9561da177e4SLinus Torvalds 9571da177e4SLinus Torvalds tsec = tsk->security; 9581da177e4SLinus Torvalds 9591da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,CAP); 9601da177e4SLinus Torvalds ad.tsk = tsk; 9611da177e4SLinus Torvalds ad.u.cap = cap; 9621da177e4SLinus Torvalds 9631da177e4SLinus Torvalds return avc_has_perm(tsec->sid, tsec->sid, 9641da177e4SLinus Torvalds SECCLASS_CAPABILITY, CAP_TO_MASK(cap), &ad); 9651da177e4SLinus Torvalds } 9661da177e4SLinus Torvalds 9671da177e4SLinus Torvalds /* Check whether a task is allowed to use a system operation. */ 9681da177e4SLinus Torvalds static int task_has_system(struct task_struct *tsk, 9691da177e4SLinus Torvalds u32 perms) 9701da177e4SLinus Torvalds { 9711da177e4SLinus Torvalds struct task_security_struct *tsec; 9721da177e4SLinus Torvalds 9731da177e4SLinus Torvalds tsec = tsk->security; 9741da177e4SLinus Torvalds 9751da177e4SLinus Torvalds return avc_has_perm(tsec->sid, SECINITSID_KERNEL, 9761da177e4SLinus Torvalds SECCLASS_SYSTEM, perms, NULL); 9771da177e4SLinus Torvalds } 9781da177e4SLinus Torvalds 9791da177e4SLinus Torvalds /* Check whether a task has a particular permission to an inode. 9801da177e4SLinus Torvalds The 'adp' parameter is optional and allows other audit 9811da177e4SLinus Torvalds data to be passed (e.g. the dentry). */ 9821da177e4SLinus Torvalds static int inode_has_perm(struct task_struct *tsk, 9831da177e4SLinus Torvalds struct inode *inode, 9841da177e4SLinus Torvalds u32 perms, 9851da177e4SLinus Torvalds struct avc_audit_data *adp) 9861da177e4SLinus Torvalds { 9871da177e4SLinus Torvalds struct task_security_struct *tsec; 9881da177e4SLinus Torvalds struct inode_security_struct *isec; 9891da177e4SLinus Torvalds struct avc_audit_data ad; 9901da177e4SLinus Torvalds 9911da177e4SLinus Torvalds tsec = tsk->security; 9921da177e4SLinus Torvalds isec = inode->i_security; 9931da177e4SLinus Torvalds 9941da177e4SLinus Torvalds if (!adp) { 9951da177e4SLinus Torvalds adp = &ad; 9961da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 9971da177e4SLinus Torvalds ad.u.fs.inode = inode; 9981da177e4SLinus Torvalds } 9991da177e4SLinus Torvalds 10001da177e4SLinus Torvalds return avc_has_perm(tsec->sid, isec->sid, isec->sclass, perms, adp); 10011da177e4SLinus Torvalds } 10021da177e4SLinus Torvalds 10031da177e4SLinus Torvalds /* Same as inode_has_perm, but pass explicit audit data containing 10041da177e4SLinus Torvalds the dentry to help the auditing code to more easily generate the 10051da177e4SLinus Torvalds pathname if needed. */ 10061da177e4SLinus Torvalds static inline int dentry_has_perm(struct task_struct *tsk, 10071da177e4SLinus Torvalds struct vfsmount *mnt, 10081da177e4SLinus Torvalds struct dentry *dentry, 10091da177e4SLinus Torvalds u32 av) 10101da177e4SLinus Torvalds { 10111da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 10121da177e4SLinus Torvalds struct avc_audit_data ad; 10131da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,FS); 10141da177e4SLinus Torvalds ad.u.fs.mnt = mnt; 10151da177e4SLinus Torvalds ad.u.fs.dentry = dentry; 10161da177e4SLinus Torvalds return inode_has_perm(tsk, inode, av, &ad); 10171da177e4SLinus Torvalds } 10181da177e4SLinus Torvalds 10191da177e4SLinus Torvalds /* Check whether a task can use an open file descriptor to 10201da177e4SLinus Torvalds access an inode in a given way. Check access to the 10211da177e4SLinus Torvalds descriptor itself, and then use dentry_has_perm to 10221da177e4SLinus Torvalds check a particular permission to the file. 10231da177e4SLinus Torvalds Access to the descriptor is implicitly granted if it 10241da177e4SLinus Torvalds has the same SID as the process. If av is zero, then 10251da177e4SLinus Torvalds access to the file is not checked, e.g. for cases 10261da177e4SLinus Torvalds where only the descriptor is affected like seek. */ 1027858119e1SArjan van de Ven static int file_has_perm(struct task_struct *tsk, 10281da177e4SLinus Torvalds struct file *file, 10291da177e4SLinus Torvalds u32 av) 10301da177e4SLinus Torvalds { 10311da177e4SLinus Torvalds struct task_security_struct *tsec = tsk->security; 10321da177e4SLinus Torvalds struct file_security_struct *fsec = file->f_security; 10331da177e4SLinus Torvalds struct vfsmount *mnt = file->f_vfsmnt; 10341da177e4SLinus Torvalds struct dentry *dentry = file->f_dentry; 10351da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 10361da177e4SLinus Torvalds struct avc_audit_data ad; 10371da177e4SLinus Torvalds int rc; 10381da177e4SLinus Torvalds 10391da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 10401da177e4SLinus Torvalds ad.u.fs.mnt = mnt; 10411da177e4SLinus Torvalds ad.u.fs.dentry = dentry; 10421da177e4SLinus Torvalds 10431da177e4SLinus Torvalds if (tsec->sid != fsec->sid) { 10441da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, fsec->sid, 10451da177e4SLinus Torvalds SECCLASS_FD, 10461da177e4SLinus Torvalds FD__USE, 10471da177e4SLinus Torvalds &ad); 10481da177e4SLinus Torvalds if (rc) 10491da177e4SLinus Torvalds return rc; 10501da177e4SLinus Torvalds } 10511da177e4SLinus Torvalds 10521da177e4SLinus Torvalds /* av is zero if only checking access to the descriptor. */ 10531da177e4SLinus Torvalds if (av) 10541da177e4SLinus Torvalds return inode_has_perm(tsk, inode, av, &ad); 10551da177e4SLinus Torvalds 10561da177e4SLinus Torvalds return 0; 10571da177e4SLinus Torvalds } 10581da177e4SLinus Torvalds 10591da177e4SLinus Torvalds /* Check whether a task can create a file. */ 10601da177e4SLinus Torvalds static int may_create(struct inode *dir, 10611da177e4SLinus Torvalds struct dentry *dentry, 10621da177e4SLinus Torvalds u16 tclass) 10631da177e4SLinus Torvalds { 10641da177e4SLinus Torvalds struct task_security_struct *tsec; 10651da177e4SLinus Torvalds struct inode_security_struct *dsec; 10661da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 10671da177e4SLinus Torvalds u32 newsid; 10681da177e4SLinus Torvalds struct avc_audit_data ad; 10691da177e4SLinus Torvalds int rc; 10701da177e4SLinus Torvalds 10711da177e4SLinus Torvalds tsec = current->security; 10721da177e4SLinus Torvalds dsec = dir->i_security; 10731da177e4SLinus Torvalds sbsec = dir->i_sb->s_security; 10741da177e4SLinus Torvalds 10751da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 10761da177e4SLinus Torvalds ad.u.fs.dentry = dentry; 10771da177e4SLinus Torvalds 10781da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, dsec->sid, SECCLASS_DIR, 10791da177e4SLinus Torvalds DIR__ADD_NAME | DIR__SEARCH, 10801da177e4SLinus Torvalds &ad); 10811da177e4SLinus Torvalds if (rc) 10821da177e4SLinus Torvalds return rc; 10831da177e4SLinus Torvalds 10841da177e4SLinus Torvalds if (tsec->create_sid && sbsec->behavior != SECURITY_FS_USE_MNTPOINT) { 10851da177e4SLinus Torvalds newsid = tsec->create_sid; 10861da177e4SLinus Torvalds } else { 10871da177e4SLinus Torvalds rc = security_transition_sid(tsec->sid, dsec->sid, tclass, 10881da177e4SLinus Torvalds &newsid); 10891da177e4SLinus Torvalds if (rc) 10901da177e4SLinus Torvalds return rc; 10911da177e4SLinus Torvalds } 10921da177e4SLinus Torvalds 10931da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, newsid, tclass, FILE__CREATE, &ad); 10941da177e4SLinus Torvalds if (rc) 10951da177e4SLinus Torvalds return rc; 10961da177e4SLinus Torvalds 10971da177e4SLinus Torvalds return avc_has_perm(newsid, sbsec->sid, 10981da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 10991da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, &ad); 11001da177e4SLinus Torvalds } 11011da177e4SLinus Torvalds 11021da177e4SLinus Torvalds #define MAY_LINK 0 11031da177e4SLinus Torvalds #define MAY_UNLINK 1 11041da177e4SLinus Torvalds #define MAY_RMDIR 2 11051da177e4SLinus Torvalds 11061da177e4SLinus Torvalds /* Check whether a task can link, unlink, or rmdir a file/directory. */ 11071da177e4SLinus Torvalds static int may_link(struct inode *dir, 11081da177e4SLinus Torvalds struct dentry *dentry, 11091da177e4SLinus Torvalds int kind) 11101da177e4SLinus Torvalds 11111da177e4SLinus Torvalds { 11121da177e4SLinus Torvalds struct task_security_struct *tsec; 11131da177e4SLinus Torvalds struct inode_security_struct *dsec, *isec; 11141da177e4SLinus Torvalds struct avc_audit_data ad; 11151da177e4SLinus Torvalds u32 av; 11161da177e4SLinus Torvalds int rc; 11171da177e4SLinus Torvalds 11181da177e4SLinus Torvalds tsec = current->security; 11191da177e4SLinus Torvalds dsec = dir->i_security; 11201da177e4SLinus Torvalds isec = dentry->d_inode->i_security; 11211da177e4SLinus Torvalds 11221da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 11231da177e4SLinus Torvalds ad.u.fs.dentry = dentry; 11241da177e4SLinus Torvalds 11251da177e4SLinus Torvalds av = DIR__SEARCH; 11261da177e4SLinus Torvalds av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME); 11271da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, dsec->sid, SECCLASS_DIR, av, &ad); 11281da177e4SLinus Torvalds if (rc) 11291da177e4SLinus Torvalds return rc; 11301da177e4SLinus Torvalds 11311da177e4SLinus Torvalds switch (kind) { 11321da177e4SLinus Torvalds case MAY_LINK: 11331da177e4SLinus Torvalds av = FILE__LINK; 11341da177e4SLinus Torvalds break; 11351da177e4SLinus Torvalds case MAY_UNLINK: 11361da177e4SLinus Torvalds av = FILE__UNLINK; 11371da177e4SLinus Torvalds break; 11381da177e4SLinus Torvalds case MAY_RMDIR: 11391da177e4SLinus Torvalds av = DIR__RMDIR; 11401da177e4SLinus Torvalds break; 11411da177e4SLinus Torvalds default: 11421da177e4SLinus Torvalds printk(KERN_WARNING "may_link: unrecognized kind %d\n", kind); 11431da177e4SLinus Torvalds return 0; 11441da177e4SLinus Torvalds } 11451da177e4SLinus Torvalds 11461da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, isec->sclass, av, &ad); 11471da177e4SLinus Torvalds return rc; 11481da177e4SLinus Torvalds } 11491da177e4SLinus Torvalds 11501da177e4SLinus Torvalds static inline int may_rename(struct inode *old_dir, 11511da177e4SLinus Torvalds struct dentry *old_dentry, 11521da177e4SLinus Torvalds struct inode *new_dir, 11531da177e4SLinus Torvalds struct dentry *new_dentry) 11541da177e4SLinus Torvalds { 11551da177e4SLinus Torvalds struct task_security_struct *tsec; 11561da177e4SLinus Torvalds struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec; 11571da177e4SLinus Torvalds struct avc_audit_data ad; 11581da177e4SLinus Torvalds u32 av; 11591da177e4SLinus Torvalds int old_is_dir, new_is_dir; 11601da177e4SLinus Torvalds int rc; 11611da177e4SLinus Torvalds 11621da177e4SLinus Torvalds tsec = current->security; 11631da177e4SLinus Torvalds old_dsec = old_dir->i_security; 11641da177e4SLinus Torvalds old_isec = old_dentry->d_inode->i_security; 11651da177e4SLinus Torvalds old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 11661da177e4SLinus Torvalds new_dsec = new_dir->i_security; 11671da177e4SLinus Torvalds 11681da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 11691da177e4SLinus Torvalds 11701da177e4SLinus Torvalds ad.u.fs.dentry = old_dentry; 11711da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, old_dsec->sid, SECCLASS_DIR, 11721da177e4SLinus Torvalds DIR__REMOVE_NAME | DIR__SEARCH, &ad); 11731da177e4SLinus Torvalds if (rc) 11741da177e4SLinus Torvalds return rc; 11751da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, old_isec->sid, 11761da177e4SLinus Torvalds old_isec->sclass, FILE__RENAME, &ad); 11771da177e4SLinus Torvalds if (rc) 11781da177e4SLinus Torvalds return rc; 11791da177e4SLinus Torvalds if (old_is_dir && new_dir != old_dir) { 11801da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, old_isec->sid, 11811da177e4SLinus Torvalds old_isec->sclass, DIR__REPARENT, &ad); 11821da177e4SLinus Torvalds if (rc) 11831da177e4SLinus Torvalds return rc; 11841da177e4SLinus Torvalds } 11851da177e4SLinus Torvalds 11861da177e4SLinus Torvalds ad.u.fs.dentry = new_dentry; 11871da177e4SLinus Torvalds av = DIR__ADD_NAME | DIR__SEARCH; 11881da177e4SLinus Torvalds if (new_dentry->d_inode) 11891da177e4SLinus Torvalds av |= DIR__REMOVE_NAME; 11901da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, new_dsec->sid, SECCLASS_DIR, av, &ad); 11911da177e4SLinus Torvalds if (rc) 11921da177e4SLinus Torvalds return rc; 11931da177e4SLinus Torvalds if (new_dentry->d_inode) { 11941da177e4SLinus Torvalds new_isec = new_dentry->d_inode->i_security; 11951da177e4SLinus Torvalds new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode); 11961da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, new_isec->sid, 11971da177e4SLinus Torvalds new_isec->sclass, 11981da177e4SLinus Torvalds (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad); 11991da177e4SLinus Torvalds if (rc) 12001da177e4SLinus Torvalds return rc; 12011da177e4SLinus Torvalds } 12021da177e4SLinus Torvalds 12031da177e4SLinus Torvalds return 0; 12041da177e4SLinus Torvalds } 12051da177e4SLinus Torvalds 12061da177e4SLinus Torvalds /* Check whether a task can perform a filesystem operation. */ 12071da177e4SLinus Torvalds static int superblock_has_perm(struct task_struct *tsk, 12081da177e4SLinus Torvalds struct super_block *sb, 12091da177e4SLinus Torvalds u32 perms, 12101da177e4SLinus Torvalds struct avc_audit_data *ad) 12111da177e4SLinus Torvalds { 12121da177e4SLinus Torvalds struct task_security_struct *tsec; 12131da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 12141da177e4SLinus Torvalds 12151da177e4SLinus Torvalds tsec = tsk->security; 12161da177e4SLinus Torvalds sbsec = sb->s_security; 12171da177e4SLinus Torvalds return avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 12181da177e4SLinus Torvalds perms, ad); 12191da177e4SLinus Torvalds } 12201da177e4SLinus Torvalds 12211da177e4SLinus Torvalds /* Convert a Linux mode and permission mask to an access vector. */ 12221da177e4SLinus Torvalds static inline u32 file_mask_to_av(int mode, int mask) 12231da177e4SLinus Torvalds { 12241da177e4SLinus Torvalds u32 av = 0; 12251da177e4SLinus Torvalds 12261da177e4SLinus Torvalds if ((mode & S_IFMT) != S_IFDIR) { 12271da177e4SLinus Torvalds if (mask & MAY_EXEC) 12281da177e4SLinus Torvalds av |= FILE__EXECUTE; 12291da177e4SLinus Torvalds if (mask & MAY_READ) 12301da177e4SLinus Torvalds av |= FILE__READ; 12311da177e4SLinus Torvalds 12321da177e4SLinus Torvalds if (mask & MAY_APPEND) 12331da177e4SLinus Torvalds av |= FILE__APPEND; 12341da177e4SLinus Torvalds else if (mask & MAY_WRITE) 12351da177e4SLinus Torvalds av |= FILE__WRITE; 12361da177e4SLinus Torvalds 12371da177e4SLinus Torvalds } else { 12381da177e4SLinus Torvalds if (mask & MAY_EXEC) 12391da177e4SLinus Torvalds av |= DIR__SEARCH; 12401da177e4SLinus Torvalds if (mask & MAY_WRITE) 12411da177e4SLinus Torvalds av |= DIR__WRITE; 12421da177e4SLinus Torvalds if (mask & MAY_READ) 12431da177e4SLinus Torvalds av |= DIR__READ; 12441da177e4SLinus Torvalds } 12451da177e4SLinus Torvalds 12461da177e4SLinus Torvalds return av; 12471da177e4SLinus Torvalds } 12481da177e4SLinus Torvalds 12491da177e4SLinus Torvalds /* Convert a Linux file to an access vector. */ 12501da177e4SLinus Torvalds static inline u32 file_to_av(struct file *file) 12511da177e4SLinus Torvalds { 12521da177e4SLinus Torvalds u32 av = 0; 12531da177e4SLinus Torvalds 12541da177e4SLinus Torvalds if (file->f_mode & FMODE_READ) 12551da177e4SLinus Torvalds av |= FILE__READ; 12561da177e4SLinus Torvalds if (file->f_mode & FMODE_WRITE) { 12571da177e4SLinus Torvalds if (file->f_flags & O_APPEND) 12581da177e4SLinus Torvalds av |= FILE__APPEND; 12591da177e4SLinus Torvalds else 12601da177e4SLinus Torvalds av |= FILE__WRITE; 12611da177e4SLinus Torvalds } 12621da177e4SLinus Torvalds 12631da177e4SLinus Torvalds return av; 12641da177e4SLinus Torvalds } 12651da177e4SLinus Torvalds 12661da177e4SLinus Torvalds /* Set an inode's SID to a specified value. */ 12671da177e4SLinus Torvalds static int inode_security_set_sid(struct inode *inode, u32 sid) 12681da177e4SLinus Torvalds { 12691da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 12701da177e4SLinus Torvalds struct superblock_security_struct *sbsec = inode->i_sb->s_security; 12711da177e4SLinus Torvalds 12721da177e4SLinus Torvalds if (!sbsec->initialized) { 12731da177e4SLinus Torvalds /* Defer initialization to selinux_complete_init. */ 12741da177e4SLinus Torvalds return 0; 12751da177e4SLinus Torvalds } 12761da177e4SLinus Torvalds 12771da177e4SLinus Torvalds down(&isec->sem); 12781da177e4SLinus Torvalds isec->sclass = inode_mode_to_security_class(inode->i_mode); 12791da177e4SLinus Torvalds isec->sid = sid; 12801da177e4SLinus Torvalds isec->initialized = 1; 12811da177e4SLinus Torvalds up(&isec->sem); 12821da177e4SLinus Torvalds return 0; 12831da177e4SLinus Torvalds } 12841da177e4SLinus Torvalds 12851da177e4SLinus Torvalds /* Hook functions begin here. */ 12861da177e4SLinus Torvalds 12871da177e4SLinus Torvalds static int selinux_ptrace(struct task_struct *parent, struct task_struct *child) 12881da177e4SLinus Torvalds { 12891da177e4SLinus Torvalds struct task_security_struct *psec = parent->security; 12901da177e4SLinus Torvalds struct task_security_struct *csec = child->security; 12911da177e4SLinus Torvalds int rc; 12921da177e4SLinus Torvalds 12931da177e4SLinus Torvalds rc = secondary_ops->ptrace(parent,child); 12941da177e4SLinus Torvalds if (rc) 12951da177e4SLinus Torvalds return rc; 12961da177e4SLinus Torvalds 12971da177e4SLinus Torvalds rc = task_has_perm(parent, child, PROCESS__PTRACE); 12981da177e4SLinus Torvalds /* Save the SID of the tracing process for later use in apply_creds. */ 1299341c2d80SStephen Smalley if (!(child->ptrace & PT_PTRACED) && !rc) 13001da177e4SLinus Torvalds csec->ptrace_sid = psec->sid; 13011da177e4SLinus Torvalds return rc; 13021da177e4SLinus Torvalds } 13031da177e4SLinus Torvalds 13041da177e4SLinus Torvalds static int selinux_capget(struct task_struct *target, kernel_cap_t *effective, 13051da177e4SLinus Torvalds kernel_cap_t *inheritable, kernel_cap_t *permitted) 13061da177e4SLinus Torvalds { 13071da177e4SLinus Torvalds int error; 13081da177e4SLinus Torvalds 13091da177e4SLinus Torvalds error = task_has_perm(current, target, PROCESS__GETCAP); 13101da177e4SLinus Torvalds if (error) 13111da177e4SLinus Torvalds return error; 13121da177e4SLinus Torvalds 13131da177e4SLinus Torvalds return secondary_ops->capget(target, effective, inheritable, permitted); 13141da177e4SLinus Torvalds } 13151da177e4SLinus Torvalds 13161da177e4SLinus Torvalds static int selinux_capset_check(struct task_struct *target, kernel_cap_t *effective, 13171da177e4SLinus Torvalds kernel_cap_t *inheritable, kernel_cap_t *permitted) 13181da177e4SLinus Torvalds { 13191da177e4SLinus Torvalds int error; 13201da177e4SLinus Torvalds 13211da177e4SLinus Torvalds error = secondary_ops->capset_check(target, effective, inheritable, permitted); 13221da177e4SLinus Torvalds if (error) 13231da177e4SLinus Torvalds return error; 13241da177e4SLinus Torvalds 13251da177e4SLinus Torvalds return task_has_perm(current, target, PROCESS__SETCAP); 13261da177e4SLinus Torvalds } 13271da177e4SLinus Torvalds 13281da177e4SLinus Torvalds static void selinux_capset_set(struct task_struct *target, kernel_cap_t *effective, 13291da177e4SLinus Torvalds kernel_cap_t *inheritable, kernel_cap_t *permitted) 13301da177e4SLinus Torvalds { 13311da177e4SLinus Torvalds secondary_ops->capset_set(target, effective, inheritable, permitted); 13321da177e4SLinus Torvalds } 13331da177e4SLinus Torvalds 13341da177e4SLinus Torvalds static int selinux_capable(struct task_struct *tsk, int cap) 13351da177e4SLinus Torvalds { 13361da177e4SLinus Torvalds int rc; 13371da177e4SLinus Torvalds 13381da177e4SLinus Torvalds rc = secondary_ops->capable(tsk, cap); 13391da177e4SLinus Torvalds if (rc) 13401da177e4SLinus Torvalds return rc; 13411da177e4SLinus Torvalds 13421da177e4SLinus Torvalds return task_has_capability(tsk,cap); 13431da177e4SLinus Torvalds } 13441da177e4SLinus Torvalds 13451da177e4SLinus Torvalds static int selinux_sysctl(ctl_table *table, int op) 13461da177e4SLinus Torvalds { 13471da177e4SLinus Torvalds int error = 0; 13481da177e4SLinus Torvalds u32 av; 13491da177e4SLinus Torvalds struct task_security_struct *tsec; 13501da177e4SLinus Torvalds u32 tsid; 13511da177e4SLinus Torvalds int rc; 13521da177e4SLinus Torvalds 13531da177e4SLinus Torvalds rc = secondary_ops->sysctl(table, op); 13541da177e4SLinus Torvalds if (rc) 13551da177e4SLinus Torvalds return rc; 13561da177e4SLinus Torvalds 13571da177e4SLinus Torvalds tsec = current->security; 13581da177e4SLinus Torvalds 13591da177e4SLinus Torvalds rc = selinux_proc_get_sid(table->de, (op == 001) ? 13601da177e4SLinus Torvalds SECCLASS_DIR : SECCLASS_FILE, &tsid); 13611da177e4SLinus Torvalds if (rc) { 13621da177e4SLinus Torvalds /* Default to the well-defined sysctl SID. */ 13631da177e4SLinus Torvalds tsid = SECINITSID_SYSCTL; 13641da177e4SLinus Torvalds } 13651da177e4SLinus Torvalds 13661da177e4SLinus Torvalds /* The op values are "defined" in sysctl.c, thereby creating 13671da177e4SLinus Torvalds * a bad coupling between this module and sysctl.c */ 13681da177e4SLinus Torvalds if(op == 001) { 13691da177e4SLinus Torvalds error = avc_has_perm(tsec->sid, tsid, 13701da177e4SLinus Torvalds SECCLASS_DIR, DIR__SEARCH, NULL); 13711da177e4SLinus Torvalds } else { 13721da177e4SLinus Torvalds av = 0; 13731da177e4SLinus Torvalds if (op & 004) 13741da177e4SLinus Torvalds av |= FILE__READ; 13751da177e4SLinus Torvalds if (op & 002) 13761da177e4SLinus Torvalds av |= FILE__WRITE; 13771da177e4SLinus Torvalds if (av) 13781da177e4SLinus Torvalds error = avc_has_perm(tsec->sid, tsid, 13791da177e4SLinus Torvalds SECCLASS_FILE, av, NULL); 13801da177e4SLinus Torvalds } 13811da177e4SLinus Torvalds 13821da177e4SLinus Torvalds return error; 13831da177e4SLinus Torvalds } 13841da177e4SLinus Torvalds 13851da177e4SLinus Torvalds static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb) 13861da177e4SLinus Torvalds { 13871da177e4SLinus Torvalds int rc = 0; 13881da177e4SLinus Torvalds 13891da177e4SLinus Torvalds if (!sb) 13901da177e4SLinus Torvalds return 0; 13911da177e4SLinus Torvalds 13921da177e4SLinus Torvalds switch (cmds) { 13931da177e4SLinus Torvalds case Q_SYNC: 13941da177e4SLinus Torvalds case Q_QUOTAON: 13951da177e4SLinus Torvalds case Q_QUOTAOFF: 13961da177e4SLinus Torvalds case Q_SETINFO: 13971da177e4SLinus Torvalds case Q_SETQUOTA: 13981da177e4SLinus Torvalds rc = superblock_has_perm(current, 13991da177e4SLinus Torvalds sb, 14001da177e4SLinus Torvalds FILESYSTEM__QUOTAMOD, NULL); 14011da177e4SLinus Torvalds break; 14021da177e4SLinus Torvalds case Q_GETFMT: 14031da177e4SLinus Torvalds case Q_GETINFO: 14041da177e4SLinus Torvalds case Q_GETQUOTA: 14051da177e4SLinus Torvalds rc = superblock_has_perm(current, 14061da177e4SLinus Torvalds sb, 14071da177e4SLinus Torvalds FILESYSTEM__QUOTAGET, NULL); 14081da177e4SLinus Torvalds break; 14091da177e4SLinus Torvalds default: 14101da177e4SLinus Torvalds rc = 0; /* let the kernel handle invalid cmds */ 14111da177e4SLinus Torvalds break; 14121da177e4SLinus Torvalds } 14131da177e4SLinus Torvalds return rc; 14141da177e4SLinus Torvalds } 14151da177e4SLinus Torvalds 14161da177e4SLinus Torvalds static int selinux_quota_on(struct dentry *dentry) 14171da177e4SLinus Torvalds { 14181da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__QUOTAON); 14191da177e4SLinus Torvalds } 14201da177e4SLinus Torvalds 14211da177e4SLinus Torvalds static int selinux_syslog(int type) 14221da177e4SLinus Torvalds { 14231da177e4SLinus Torvalds int rc; 14241da177e4SLinus Torvalds 14251da177e4SLinus Torvalds rc = secondary_ops->syslog(type); 14261da177e4SLinus Torvalds if (rc) 14271da177e4SLinus Torvalds return rc; 14281da177e4SLinus Torvalds 14291da177e4SLinus Torvalds switch (type) { 14301da177e4SLinus Torvalds case 3: /* Read last kernel messages */ 14311da177e4SLinus Torvalds case 10: /* Return size of the log buffer */ 14321da177e4SLinus Torvalds rc = task_has_system(current, SYSTEM__SYSLOG_READ); 14331da177e4SLinus Torvalds break; 14341da177e4SLinus Torvalds case 6: /* Disable logging to console */ 14351da177e4SLinus Torvalds case 7: /* Enable logging to console */ 14361da177e4SLinus Torvalds case 8: /* Set level of messages printed to console */ 14371da177e4SLinus Torvalds rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE); 14381da177e4SLinus Torvalds break; 14391da177e4SLinus Torvalds case 0: /* Close log */ 14401da177e4SLinus Torvalds case 1: /* Open log */ 14411da177e4SLinus Torvalds case 2: /* Read from log */ 14421da177e4SLinus Torvalds case 4: /* Read/clear last kernel messages */ 14431da177e4SLinus Torvalds case 5: /* Clear ring buffer */ 14441da177e4SLinus Torvalds default: 14451da177e4SLinus Torvalds rc = task_has_system(current, SYSTEM__SYSLOG_MOD); 14461da177e4SLinus Torvalds break; 14471da177e4SLinus Torvalds } 14481da177e4SLinus Torvalds return rc; 14491da177e4SLinus Torvalds } 14501da177e4SLinus Torvalds 14511da177e4SLinus Torvalds /* 14521da177e4SLinus Torvalds * Check that a process has enough memory to allocate a new virtual 14531da177e4SLinus Torvalds * mapping. 0 means there is enough memory for the allocation to 14541da177e4SLinus Torvalds * succeed and -ENOMEM implies there is not. 14551da177e4SLinus Torvalds * 14561da177e4SLinus Torvalds * Note that secondary_ops->capable and task_has_perm_noaudit return 0 14571da177e4SLinus Torvalds * if the capability is granted, but __vm_enough_memory requires 1 if 14581da177e4SLinus Torvalds * the capability is granted. 14591da177e4SLinus Torvalds * 14601da177e4SLinus Torvalds * Do not audit the selinux permission check, as this is applied to all 14611da177e4SLinus Torvalds * processes that allocate mappings. 14621da177e4SLinus Torvalds */ 14631da177e4SLinus Torvalds static int selinux_vm_enough_memory(long pages) 14641da177e4SLinus Torvalds { 14651da177e4SLinus Torvalds int rc, cap_sys_admin = 0; 14661da177e4SLinus Torvalds struct task_security_struct *tsec = current->security; 14671da177e4SLinus Torvalds 14681da177e4SLinus Torvalds rc = secondary_ops->capable(current, CAP_SYS_ADMIN); 14691da177e4SLinus Torvalds if (rc == 0) 14701da177e4SLinus Torvalds rc = avc_has_perm_noaudit(tsec->sid, tsec->sid, 14711da177e4SLinus Torvalds SECCLASS_CAPABILITY, 14721da177e4SLinus Torvalds CAP_TO_MASK(CAP_SYS_ADMIN), 14731da177e4SLinus Torvalds NULL); 14741da177e4SLinus Torvalds 14751da177e4SLinus Torvalds if (rc == 0) 14761da177e4SLinus Torvalds cap_sys_admin = 1; 14771da177e4SLinus Torvalds 14781da177e4SLinus Torvalds return __vm_enough_memory(pages, cap_sys_admin); 14791da177e4SLinus Torvalds } 14801da177e4SLinus Torvalds 14811da177e4SLinus Torvalds /* binprm security operations */ 14821da177e4SLinus Torvalds 14831da177e4SLinus Torvalds static int selinux_bprm_alloc_security(struct linux_binprm *bprm) 14841da177e4SLinus Torvalds { 14851da177e4SLinus Torvalds struct bprm_security_struct *bsec; 14861da177e4SLinus Torvalds 148789d155efSJames Morris bsec = kzalloc(sizeof(struct bprm_security_struct), GFP_KERNEL); 14881da177e4SLinus Torvalds if (!bsec) 14891da177e4SLinus Torvalds return -ENOMEM; 14901da177e4SLinus Torvalds 14911da177e4SLinus Torvalds bsec->bprm = bprm; 14921da177e4SLinus Torvalds bsec->sid = SECINITSID_UNLABELED; 14931da177e4SLinus Torvalds bsec->set = 0; 14941da177e4SLinus Torvalds 14951da177e4SLinus Torvalds bprm->security = bsec; 14961da177e4SLinus Torvalds return 0; 14971da177e4SLinus Torvalds } 14981da177e4SLinus Torvalds 14991da177e4SLinus Torvalds static int selinux_bprm_set_security(struct linux_binprm *bprm) 15001da177e4SLinus Torvalds { 15011da177e4SLinus Torvalds struct task_security_struct *tsec; 15021da177e4SLinus Torvalds struct inode *inode = bprm->file->f_dentry->d_inode; 15031da177e4SLinus Torvalds struct inode_security_struct *isec; 15041da177e4SLinus Torvalds struct bprm_security_struct *bsec; 15051da177e4SLinus Torvalds u32 newsid; 15061da177e4SLinus Torvalds struct avc_audit_data ad; 15071da177e4SLinus Torvalds int rc; 15081da177e4SLinus Torvalds 15091da177e4SLinus Torvalds rc = secondary_ops->bprm_set_security(bprm); 15101da177e4SLinus Torvalds if (rc) 15111da177e4SLinus Torvalds return rc; 15121da177e4SLinus Torvalds 15131da177e4SLinus Torvalds bsec = bprm->security; 15141da177e4SLinus Torvalds 15151da177e4SLinus Torvalds if (bsec->set) 15161da177e4SLinus Torvalds return 0; 15171da177e4SLinus Torvalds 15181da177e4SLinus Torvalds tsec = current->security; 15191da177e4SLinus Torvalds isec = inode->i_security; 15201da177e4SLinus Torvalds 15211da177e4SLinus Torvalds /* Default to the current task SID. */ 15221da177e4SLinus Torvalds bsec->sid = tsec->sid; 15231da177e4SLinus Torvalds 15241da177e4SLinus Torvalds /* Reset create SID on execve. */ 15251da177e4SLinus Torvalds tsec->create_sid = 0; 15261da177e4SLinus Torvalds 15271da177e4SLinus Torvalds if (tsec->exec_sid) { 15281da177e4SLinus Torvalds newsid = tsec->exec_sid; 15291da177e4SLinus Torvalds /* Reset exec SID on execve. */ 15301da177e4SLinus Torvalds tsec->exec_sid = 0; 15311da177e4SLinus Torvalds } else { 15321da177e4SLinus Torvalds /* Check for a default transition on this program. */ 15331da177e4SLinus Torvalds rc = security_transition_sid(tsec->sid, isec->sid, 15341da177e4SLinus Torvalds SECCLASS_PROCESS, &newsid); 15351da177e4SLinus Torvalds if (rc) 15361da177e4SLinus Torvalds return rc; 15371da177e4SLinus Torvalds } 15381da177e4SLinus Torvalds 15391da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 15401da177e4SLinus Torvalds ad.u.fs.mnt = bprm->file->f_vfsmnt; 15411da177e4SLinus Torvalds ad.u.fs.dentry = bprm->file->f_dentry; 15421da177e4SLinus Torvalds 15431da177e4SLinus Torvalds if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID) 15441da177e4SLinus Torvalds newsid = tsec->sid; 15451da177e4SLinus Torvalds 15461da177e4SLinus Torvalds if (tsec->sid == newsid) { 15471da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, 15481da177e4SLinus Torvalds SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad); 15491da177e4SLinus Torvalds if (rc) 15501da177e4SLinus Torvalds return rc; 15511da177e4SLinus Torvalds } else { 15521da177e4SLinus Torvalds /* Check permissions for the transition. */ 15531da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, newsid, 15541da177e4SLinus Torvalds SECCLASS_PROCESS, PROCESS__TRANSITION, &ad); 15551da177e4SLinus Torvalds if (rc) 15561da177e4SLinus Torvalds return rc; 15571da177e4SLinus Torvalds 15581da177e4SLinus Torvalds rc = avc_has_perm(newsid, isec->sid, 15591da177e4SLinus Torvalds SECCLASS_FILE, FILE__ENTRYPOINT, &ad); 15601da177e4SLinus Torvalds if (rc) 15611da177e4SLinus Torvalds return rc; 15621da177e4SLinus Torvalds 15631da177e4SLinus Torvalds /* Clear any possibly unsafe personality bits on exec: */ 15641da177e4SLinus Torvalds current->personality &= ~PER_CLEAR_ON_SETID; 15651da177e4SLinus Torvalds 15661da177e4SLinus Torvalds /* Set the security field to the new SID. */ 15671da177e4SLinus Torvalds bsec->sid = newsid; 15681da177e4SLinus Torvalds } 15691da177e4SLinus Torvalds 15701da177e4SLinus Torvalds bsec->set = 1; 15711da177e4SLinus Torvalds return 0; 15721da177e4SLinus Torvalds } 15731da177e4SLinus Torvalds 15741da177e4SLinus Torvalds static int selinux_bprm_check_security (struct linux_binprm *bprm) 15751da177e4SLinus Torvalds { 15761da177e4SLinus Torvalds return secondary_ops->bprm_check_security(bprm); 15771da177e4SLinus Torvalds } 15781da177e4SLinus Torvalds 15791da177e4SLinus Torvalds 15801da177e4SLinus Torvalds static int selinux_bprm_secureexec (struct linux_binprm *bprm) 15811da177e4SLinus Torvalds { 15821da177e4SLinus Torvalds struct task_security_struct *tsec = current->security; 15831da177e4SLinus Torvalds int atsecure = 0; 15841da177e4SLinus Torvalds 15851da177e4SLinus Torvalds if (tsec->osid != tsec->sid) { 15861da177e4SLinus Torvalds /* Enable secure mode for SIDs transitions unless 15871da177e4SLinus Torvalds the noatsecure permission is granted between 15881da177e4SLinus Torvalds the two SIDs, i.e. ahp returns 0. */ 15891da177e4SLinus Torvalds atsecure = avc_has_perm(tsec->osid, tsec->sid, 15901da177e4SLinus Torvalds SECCLASS_PROCESS, 15911da177e4SLinus Torvalds PROCESS__NOATSECURE, NULL); 15921da177e4SLinus Torvalds } 15931da177e4SLinus Torvalds 15941da177e4SLinus Torvalds return (atsecure || secondary_ops->bprm_secureexec(bprm)); 15951da177e4SLinus Torvalds } 15961da177e4SLinus Torvalds 15971da177e4SLinus Torvalds static void selinux_bprm_free_security(struct linux_binprm *bprm) 15981da177e4SLinus Torvalds { 15999a5f04bfSJesper Juhl kfree(bprm->security); 16001da177e4SLinus Torvalds bprm->security = NULL; 16011da177e4SLinus Torvalds } 16021da177e4SLinus Torvalds 16031da177e4SLinus Torvalds extern struct vfsmount *selinuxfs_mount; 16041da177e4SLinus Torvalds extern struct dentry *selinux_null; 16051da177e4SLinus Torvalds 16061da177e4SLinus Torvalds /* Derived from fs/exec.c:flush_old_files. */ 16071da177e4SLinus Torvalds static inline void flush_unauthorized_files(struct files_struct * files) 16081da177e4SLinus Torvalds { 16091da177e4SLinus Torvalds struct avc_audit_data ad; 16101da177e4SLinus Torvalds struct file *file, *devnull = NULL; 16111da177e4SLinus Torvalds struct tty_struct *tty = current->signal->tty; 1612badf1662SDipankar Sarma struct fdtable *fdt; 16131da177e4SLinus Torvalds long j = -1; 16141da177e4SLinus Torvalds 16151da177e4SLinus Torvalds if (tty) { 16161da177e4SLinus Torvalds file_list_lock(); 16172f512016SEric Dumazet file = list_entry(tty->tty_files.next, typeof(*file), f_u.fu_list); 16181da177e4SLinus Torvalds if (file) { 16191da177e4SLinus Torvalds /* Revalidate access to controlling tty. 16201da177e4SLinus Torvalds Use inode_has_perm on the tty inode directly rather 16211da177e4SLinus Torvalds than using file_has_perm, as this particular open 16221da177e4SLinus Torvalds file may belong to another process and we are only 16231da177e4SLinus Torvalds interested in the inode-based check here. */ 16241da177e4SLinus Torvalds struct inode *inode = file->f_dentry->d_inode; 16251da177e4SLinus Torvalds if (inode_has_perm(current, inode, 16261da177e4SLinus Torvalds FILE__READ | FILE__WRITE, NULL)) { 16271da177e4SLinus Torvalds /* Reset controlling tty. */ 16281da177e4SLinus Torvalds current->signal->tty = NULL; 16291da177e4SLinus Torvalds current->signal->tty_old_pgrp = 0; 16301da177e4SLinus Torvalds } 16311da177e4SLinus Torvalds } 16321da177e4SLinus Torvalds file_list_unlock(); 16331da177e4SLinus Torvalds } 16341da177e4SLinus Torvalds 16351da177e4SLinus Torvalds /* Revalidate access to inherited open files. */ 16361da177e4SLinus Torvalds 16371da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,FS); 16381da177e4SLinus Torvalds 16391da177e4SLinus Torvalds spin_lock(&files->file_lock); 16401da177e4SLinus Torvalds for (;;) { 16411da177e4SLinus Torvalds unsigned long set, i; 16421da177e4SLinus Torvalds int fd; 16431da177e4SLinus Torvalds 16441da177e4SLinus Torvalds j++; 16451da177e4SLinus Torvalds i = j * __NFDBITS; 1646badf1662SDipankar Sarma fdt = files_fdtable(files); 1647badf1662SDipankar Sarma if (i >= fdt->max_fds || i >= fdt->max_fdset) 16481da177e4SLinus Torvalds break; 1649badf1662SDipankar Sarma set = fdt->open_fds->fds_bits[j]; 16501da177e4SLinus Torvalds if (!set) 16511da177e4SLinus Torvalds continue; 16521da177e4SLinus Torvalds spin_unlock(&files->file_lock); 16531da177e4SLinus Torvalds for ( ; set ; i++,set >>= 1) { 16541da177e4SLinus Torvalds if (set & 1) { 16551da177e4SLinus Torvalds file = fget(i); 16561da177e4SLinus Torvalds if (!file) 16571da177e4SLinus Torvalds continue; 16581da177e4SLinus Torvalds if (file_has_perm(current, 16591da177e4SLinus Torvalds file, 16601da177e4SLinus Torvalds file_to_av(file))) { 16611da177e4SLinus Torvalds sys_close(i); 16621da177e4SLinus Torvalds fd = get_unused_fd(); 16631da177e4SLinus Torvalds if (fd != i) { 16641da177e4SLinus Torvalds if (fd >= 0) 16651da177e4SLinus Torvalds put_unused_fd(fd); 16661da177e4SLinus Torvalds fput(file); 16671da177e4SLinus Torvalds continue; 16681da177e4SLinus Torvalds } 16691da177e4SLinus Torvalds if (devnull) { 1670095975daSNick Piggin get_file(devnull); 16711da177e4SLinus Torvalds } else { 16721da177e4SLinus Torvalds devnull = dentry_open(dget(selinux_null), mntget(selinuxfs_mount), O_RDWR); 16731da177e4SLinus Torvalds if (!devnull) { 16741da177e4SLinus Torvalds put_unused_fd(fd); 16751da177e4SLinus Torvalds fput(file); 16761da177e4SLinus Torvalds continue; 16771da177e4SLinus Torvalds } 16781da177e4SLinus Torvalds } 16791da177e4SLinus Torvalds fd_install(fd, devnull); 16801da177e4SLinus Torvalds } 16811da177e4SLinus Torvalds fput(file); 16821da177e4SLinus Torvalds } 16831da177e4SLinus Torvalds } 16841da177e4SLinus Torvalds spin_lock(&files->file_lock); 16851da177e4SLinus Torvalds 16861da177e4SLinus Torvalds } 16871da177e4SLinus Torvalds spin_unlock(&files->file_lock); 16881da177e4SLinus Torvalds } 16891da177e4SLinus Torvalds 16901da177e4SLinus Torvalds static void selinux_bprm_apply_creds(struct linux_binprm *bprm, int unsafe) 16911da177e4SLinus Torvalds { 16921da177e4SLinus Torvalds struct task_security_struct *tsec; 16931da177e4SLinus Torvalds struct bprm_security_struct *bsec; 16941da177e4SLinus Torvalds u32 sid; 16951da177e4SLinus Torvalds int rc; 16961da177e4SLinus Torvalds 16971da177e4SLinus Torvalds secondary_ops->bprm_apply_creds(bprm, unsafe); 16981da177e4SLinus Torvalds 16991da177e4SLinus Torvalds tsec = current->security; 17001da177e4SLinus Torvalds 17011da177e4SLinus Torvalds bsec = bprm->security; 17021da177e4SLinus Torvalds sid = bsec->sid; 17031da177e4SLinus Torvalds 17041da177e4SLinus Torvalds tsec->osid = tsec->sid; 17051da177e4SLinus Torvalds bsec->unsafe = 0; 17061da177e4SLinus Torvalds if (tsec->sid != sid) { 17071da177e4SLinus Torvalds /* Check for shared state. If not ok, leave SID 17081da177e4SLinus Torvalds unchanged and kill. */ 17091da177e4SLinus Torvalds if (unsafe & LSM_UNSAFE_SHARE) { 17101da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS, 17111da177e4SLinus Torvalds PROCESS__SHARE, NULL); 17121da177e4SLinus Torvalds if (rc) { 17131da177e4SLinus Torvalds bsec->unsafe = 1; 17141da177e4SLinus Torvalds return; 17151da177e4SLinus Torvalds } 17161da177e4SLinus Torvalds } 17171da177e4SLinus Torvalds 17181da177e4SLinus Torvalds /* Check for ptracing, and update the task SID if ok. 17191da177e4SLinus Torvalds Otherwise, leave SID unchanged and kill. */ 17201da177e4SLinus Torvalds if (unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) { 17211da177e4SLinus Torvalds rc = avc_has_perm(tsec->ptrace_sid, sid, 17221da177e4SLinus Torvalds SECCLASS_PROCESS, PROCESS__PTRACE, 17231da177e4SLinus Torvalds NULL); 17241da177e4SLinus Torvalds if (rc) { 17251da177e4SLinus Torvalds bsec->unsafe = 1; 17261da177e4SLinus Torvalds return; 17271da177e4SLinus Torvalds } 17281da177e4SLinus Torvalds } 17291da177e4SLinus Torvalds tsec->sid = sid; 17301da177e4SLinus Torvalds } 17311da177e4SLinus Torvalds } 17321da177e4SLinus Torvalds 17331da177e4SLinus Torvalds /* 17341da177e4SLinus Torvalds * called after apply_creds without the task lock held 17351da177e4SLinus Torvalds */ 17361da177e4SLinus Torvalds static void selinux_bprm_post_apply_creds(struct linux_binprm *bprm) 17371da177e4SLinus Torvalds { 17381da177e4SLinus Torvalds struct task_security_struct *tsec; 17391da177e4SLinus Torvalds struct rlimit *rlim, *initrlim; 17401da177e4SLinus Torvalds struct itimerval itimer; 17411da177e4SLinus Torvalds struct bprm_security_struct *bsec; 17421da177e4SLinus Torvalds int rc, i; 17431da177e4SLinus Torvalds 17441da177e4SLinus Torvalds tsec = current->security; 17451da177e4SLinus Torvalds bsec = bprm->security; 17461da177e4SLinus Torvalds 17471da177e4SLinus Torvalds if (bsec->unsafe) { 17481da177e4SLinus Torvalds force_sig_specific(SIGKILL, current); 17491da177e4SLinus Torvalds return; 17501da177e4SLinus Torvalds } 17511da177e4SLinus Torvalds if (tsec->osid == tsec->sid) 17521da177e4SLinus Torvalds return; 17531da177e4SLinus Torvalds 17541da177e4SLinus Torvalds /* Close files for which the new task SID is not authorized. */ 17551da177e4SLinus Torvalds flush_unauthorized_files(current->files); 17561da177e4SLinus Torvalds 17571da177e4SLinus Torvalds /* Check whether the new SID can inherit signal state 17581da177e4SLinus Torvalds from the old SID. If not, clear itimers to avoid 17591da177e4SLinus Torvalds subsequent signal generation and flush and unblock 17601da177e4SLinus Torvalds signals. This must occur _after_ the task SID has 17611da177e4SLinus Torvalds been updated so that any kill done after the flush 17621da177e4SLinus Torvalds will be checked against the new SID. */ 17631da177e4SLinus Torvalds rc = avc_has_perm(tsec->osid, tsec->sid, SECCLASS_PROCESS, 17641da177e4SLinus Torvalds PROCESS__SIGINH, NULL); 17651da177e4SLinus Torvalds if (rc) { 17661da177e4SLinus Torvalds memset(&itimer, 0, sizeof itimer); 17671da177e4SLinus Torvalds for (i = 0; i < 3; i++) 17681da177e4SLinus Torvalds do_setitimer(i, &itimer, NULL); 17691da177e4SLinus Torvalds flush_signals(current); 17701da177e4SLinus Torvalds spin_lock_irq(¤t->sighand->siglock); 17711da177e4SLinus Torvalds flush_signal_handlers(current, 1); 17721da177e4SLinus Torvalds sigemptyset(¤t->blocked); 17731da177e4SLinus Torvalds recalc_sigpending(); 17741da177e4SLinus Torvalds spin_unlock_irq(¤t->sighand->siglock); 17751da177e4SLinus Torvalds } 17761da177e4SLinus Torvalds 17771da177e4SLinus Torvalds /* Check whether the new SID can inherit resource limits 17781da177e4SLinus Torvalds from the old SID. If not, reset all soft limits to 17791da177e4SLinus Torvalds the lower of the current task's hard limit and the init 17801da177e4SLinus Torvalds task's soft limit. Note that the setting of hard limits 17811da177e4SLinus Torvalds (even to lower them) can be controlled by the setrlimit 17821da177e4SLinus Torvalds check. The inclusion of the init task's soft limit into 17831da177e4SLinus Torvalds the computation is to avoid resetting soft limits higher 17841da177e4SLinus Torvalds than the default soft limit for cases where the default 17851da177e4SLinus Torvalds is lower than the hard limit, e.g. RLIMIT_CORE or 17861da177e4SLinus Torvalds RLIMIT_STACK.*/ 17871da177e4SLinus Torvalds rc = avc_has_perm(tsec->osid, tsec->sid, SECCLASS_PROCESS, 17881da177e4SLinus Torvalds PROCESS__RLIMITINH, NULL); 17891da177e4SLinus Torvalds if (rc) { 17901da177e4SLinus Torvalds for (i = 0; i < RLIM_NLIMITS; i++) { 17911da177e4SLinus Torvalds rlim = current->signal->rlim + i; 17921da177e4SLinus Torvalds initrlim = init_task.signal->rlim+i; 17931da177e4SLinus Torvalds rlim->rlim_cur = min(rlim->rlim_max,initrlim->rlim_cur); 17941da177e4SLinus Torvalds } 17951da177e4SLinus Torvalds if (current->signal->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) { 17961da177e4SLinus Torvalds /* 17971da177e4SLinus Torvalds * This will cause RLIMIT_CPU calculations 17981da177e4SLinus Torvalds * to be refigured. 17991da177e4SLinus Torvalds */ 18001da177e4SLinus Torvalds current->it_prof_expires = jiffies_to_cputime(1); 18011da177e4SLinus Torvalds } 18021da177e4SLinus Torvalds } 18031da177e4SLinus Torvalds 18041da177e4SLinus Torvalds /* Wake up the parent if it is waiting so that it can 18051da177e4SLinus Torvalds recheck wait permission to the new task SID. */ 18061da177e4SLinus Torvalds wake_up_interruptible(¤t->parent->signal->wait_chldexit); 18071da177e4SLinus Torvalds } 18081da177e4SLinus Torvalds 18091da177e4SLinus Torvalds /* superblock security operations */ 18101da177e4SLinus Torvalds 18111da177e4SLinus Torvalds static int selinux_sb_alloc_security(struct super_block *sb) 18121da177e4SLinus Torvalds { 18131da177e4SLinus Torvalds return superblock_alloc_security(sb); 18141da177e4SLinus Torvalds } 18151da177e4SLinus Torvalds 18161da177e4SLinus Torvalds static void selinux_sb_free_security(struct super_block *sb) 18171da177e4SLinus Torvalds { 18181da177e4SLinus Torvalds superblock_free_security(sb); 18191da177e4SLinus Torvalds } 18201da177e4SLinus Torvalds 18211da177e4SLinus Torvalds static inline int match_prefix(char *prefix, int plen, char *option, int olen) 18221da177e4SLinus Torvalds { 18231da177e4SLinus Torvalds if (plen > olen) 18241da177e4SLinus Torvalds return 0; 18251da177e4SLinus Torvalds 18261da177e4SLinus Torvalds return !memcmp(prefix, option, plen); 18271da177e4SLinus Torvalds } 18281da177e4SLinus Torvalds 18291da177e4SLinus Torvalds static inline int selinux_option(char *option, int len) 18301da177e4SLinus Torvalds { 18311da177e4SLinus Torvalds return (match_prefix("context=", sizeof("context=")-1, option, len) || 18321da177e4SLinus Torvalds match_prefix("fscontext=", sizeof("fscontext=")-1, option, len) || 18331da177e4SLinus Torvalds match_prefix("defcontext=", sizeof("defcontext=")-1, option, len)); 18341da177e4SLinus Torvalds } 18351da177e4SLinus Torvalds 18361da177e4SLinus Torvalds static inline void take_option(char **to, char *from, int *first, int len) 18371da177e4SLinus Torvalds { 18381da177e4SLinus Torvalds if (!*first) { 18391da177e4SLinus Torvalds **to = ','; 18401da177e4SLinus Torvalds *to += 1; 18411da177e4SLinus Torvalds } 18421da177e4SLinus Torvalds else 18431da177e4SLinus Torvalds *first = 0; 18441da177e4SLinus Torvalds memcpy(*to, from, len); 18451da177e4SLinus Torvalds *to += len; 18461da177e4SLinus Torvalds } 18471da177e4SLinus Torvalds 18481da177e4SLinus Torvalds static int selinux_sb_copy_data(struct file_system_type *type, void *orig, void *copy) 18491da177e4SLinus Torvalds { 18501da177e4SLinus Torvalds int fnosec, fsec, rc = 0; 18511da177e4SLinus Torvalds char *in_save, *in_curr, *in_end; 18521da177e4SLinus Torvalds char *sec_curr, *nosec_save, *nosec; 18531da177e4SLinus Torvalds 18541da177e4SLinus Torvalds in_curr = orig; 18551da177e4SLinus Torvalds sec_curr = copy; 18561da177e4SLinus Torvalds 18571da177e4SLinus Torvalds /* Binary mount data: just copy */ 18581da177e4SLinus Torvalds if (type->fs_flags & FS_BINARY_MOUNTDATA) { 18591da177e4SLinus Torvalds copy_page(sec_curr, in_curr); 18601da177e4SLinus Torvalds goto out; 18611da177e4SLinus Torvalds } 18621da177e4SLinus Torvalds 18631da177e4SLinus Torvalds nosec = (char *)get_zeroed_page(GFP_KERNEL); 18641da177e4SLinus Torvalds if (!nosec) { 18651da177e4SLinus Torvalds rc = -ENOMEM; 18661da177e4SLinus Torvalds goto out; 18671da177e4SLinus Torvalds } 18681da177e4SLinus Torvalds 18691da177e4SLinus Torvalds nosec_save = nosec; 18701da177e4SLinus Torvalds fnosec = fsec = 1; 18711da177e4SLinus Torvalds in_save = in_end = orig; 18721da177e4SLinus Torvalds 18731da177e4SLinus Torvalds do { 18741da177e4SLinus Torvalds if (*in_end == ',' || *in_end == '\0') { 18751da177e4SLinus Torvalds int len = in_end - in_curr; 18761da177e4SLinus Torvalds 18771da177e4SLinus Torvalds if (selinux_option(in_curr, len)) 18781da177e4SLinus Torvalds take_option(&sec_curr, in_curr, &fsec, len); 18791da177e4SLinus Torvalds else 18801da177e4SLinus Torvalds take_option(&nosec, in_curr, &fnosec, len); 18811da177e4SLinus Torvalds 18821da177e4SLinus Torvalds in_curr = in_end + 1; 18831da177e4SLinus Torvalds } 18841da177e4SLinus Torvalds } while (*in_end++); 18851da177e4SLinus Torvalds 18866931dfc9SEric Paris strcpy(in_save, nosec_save); 1887da3caa20SGerald Schaefer free_page((unsigned long)nosec_save); 18881da177e4SLinus Torvalds out: 18891da177e4SLinus Torvalds return rc; 18901da177e4SLinus Torvalds } 18911da177e4SLinus Torvalds 18921da177e4SLinus Torvalds static int selinux_sb_kern_mount(struct super_block *sb, void *data) 18931da177e4SLinus Torvalds { 18941da177e4SLinus Torvalds struct avc_audit_data ad; 18951da177e4SLinus Torvalds int rc; 18961da177e4SLinus Torvalds 18971da177e4SLinus Torvalds rc = superblock_doinit(sb, data); 18981da177e4SLinus Torvalds if (rc) 18991da177e4SLinus Torvalds return rc; 19001da177e4SLinus Torvalds 19011da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,FS); 19021da177e4SLinus Torvalds ad.u.fs.dentry = sb->s_root; 19031da177e4SLinus Torvalds return superblock_has_perm(current, sb, FILESYSTEM__MOUNT, &ad); 19041da177e4SLinus Torvalds } 19051da177e4SLinus Torvalds 1906726c3342SDavid Howells static int selinux_sb_statfs(struct dentry *dentry) 19071da177e4SLinus Torvalds { 19081da177e4SLinus Torvalds struct avc_audit_data ad; 19091da177e4SLinus Torvalds 19101da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,FS); 1911726c3342SDavid Howells ad.u.fs.dentry = dentry->d_sb->s_root; 1912726c3342SDavid Howells return superblock_has_perm(current, dentry->d_sb, FILESYSTEM__GETATTR, &ad); 19131da177e4SLinus Torvalds } 19141da177e4SLinus Torvalds 19151da177e4SLinus Torvalds static int selinux_mount(char * dev_name, 19161da177e4SLinus Torvalds struct nameidata *nd, 19171da177e4SLinus Torvalds char * type, 19181da177e4SLinus Torvalds unsigned long flags, 19191da177e4SLinus Torvalds void * data) 19201da177e4SLinus Torvalds { 19211da177e4SLinus Torvalds int rc; 19221da177e4SLinus Torvalds 19231da177e4SLinus Torvalds rc = secondary_ops->sb_mount(dev_name, nd, type, flags, data); 19241da177e4SLinus Torvalds if (rc) 19251da177e4SLinus Torvalds return rc; 19261da177e4SLinus Torvalds 19271da177e4SLinus Torvalds if (flags & MS_REMOUNT) 19281da177e4SLinus Torvalds return superblock_has_perm(current, nd->mnt->mnt_sb, 19291da177e4SLinus Torvalds FILESYSTEM__REMOUNT, NULL); 19301da177e4SLinus Torvalds else 19311da177e4SLinus Torvalds return dentry_has_perm(current, nd->mnt, nd->dentry, 19321da177e4SLinus Torvalds FILE__MOUNTON); 19331da177e4SLinus Torvalds } 19341da177e4SLinus Torvalds 19351da177e4SLinus Torvalds static int selinux_umount(struct vfsmount *mnt, int flags) 19361da177e4SLinus Torvalds { 19371da177e4SLinus Torvalds int rc; 19381da177e4SLinus Torvalds 19391da177e4SLinus Torvalds rc = secondary_ops->sb_umount(mnt, flags); 19401da177e4SLinus Torvalds if (rc) 19411da177e4SLinus Torvalds return rc; 19421da177e4SLinus Torvalds 19431da177e4SLinus Torvalds return superblock_has_perm(current,mnt->mnt_sb, 19441da177e4SLinus Torvalds FILESYSTEM__UNMOUNT,NULL); 19451da177e4SLinus Torvalds } 19461da177e4SLinus Torvalds 19471da177e4SLinus Torvalds /* inode security operations */ 19481da177e4SLinus Torvalds 19491da177e4SLinus Torvalds static int selinux_inode_alloc_security(struct inode *inode) 19501da177e4SLinus Torvalds { 19511da177e4SLinus Torvalds return inode_alloc_security(inode); 19521da177e4SLinus Torvalds } 19531da177e4SLinus Torvalds 19541da177e4SLinus Torvalds static void selinux_inode_free_security(struct inode *inode) 19551da177e4SLinus Torvalds { 19561da177e4SLinus Torvalds inode_free_security(inode); 19571da177e4SLinus Torvalds } 19581da177e4SLinus Torvalds 19595e41ff9eSStephen Smalley static int selinux_inode_init_security(struct inode *inode, struct inode *dir, 19605e41ff9eSStephen Smalley char **name, void **value, 19615e41ff9eSStephen Smalley size_t *len) 19625e41ff9eSStephen Smalley { 19635e41ff9eSStephen Smalley struct task_security_struct *tsec; 19645e41ff9eSStephen Smalley struct inode_security_struct *dsec; 19655e41ff9eSStephen Smalley struct superblock_security_struct *sbsec; 1966570bc1c2SStephen Smalley u32 newsid, clen; 19675e41ff9eSStephen Smalley int rc; 1968570bc1c2SStephen Smalley char *namep = NULL, *context; 19695e41ff9eSStephen Smalley 19705e41ff9eSStephen Smalley tsec = current->security; 19715e41ff9eSStephen Smalley dsec = dir->i_security; 19725e41ff9eSStephen Smalley sbsec = dir->i_sb->s_security; 19735e41ff9eSStephen Smalley 19745e41ff9eSStephen Smalley if (tsec->create_sid && sbsec->behavior != SECURITY_FS_USE_MNTPOINT) { 19755e41ff9eSStephen Smalley newsid = tsec->create_sid; 19765e41ff9eSStephen Smalley } else { 19775e41ff9eSStephen Smalley rc = security_transition_sid(tsec->sid, dsec->sid, 19785e41ff9eSStephen Smalley inode_mode_to_security_class(inode->i_mode), 19795e41ff9eSStephen Smalley &newsid); 19805e41ff9eSStephen Smalley if (rc) { 19815e41ff9eSStephen Smalley printk(KERN_WARNING "%s: " 19825e41ff9eSStephen Smalley "security_transition_sid failed, rc=%d (dev=%s " 19835e41ff9eSStephen Smalley "ino=%ld)\n", 19845e41ff9eSStephen Smalley __FUNCTION__, 19855e41ff9eSStephen Smalley -rc, inode->i_sb->s_id, inode->i_ino); 19865e41ff9eSStephen Smalley return rc; 19875e41ff9eSStephen Smalley } 19885e41ff9eSStephen Smalley } 19895e41ff9eSStephen Smalley 19905e41ff9eSStephen Smalley inode_security_set_sid(inode, newsid); 19915e41ff9eSStephen Smalley 19928aad3875SStephen Smalley if (!ss_initialized || sbsec->behavior == SECURITY_FS_USE_MNTPOINT) 199325a74f3bSStephen Smalley return -EOPNOTSUPP; 199425a74f3bSStephen Smalley 1995570bc1c2SStephen Smalley if (name) { 19965e41ff9eSStephen Smalley namep = kstrdup(XATTR_SELINUX_SUFFIX, GFP_KERNEL); 19975e41ff9eSStephen Smalley if (!namep) 19985e41ff9eSStephen Smalley return -ENOMEM; 19995e41ff9eSStephen Smalley *name = namep; 2000570bc1c2SStephen Smalley } 20015e41ff9eSStephen Smalley 2002570bc1c2SStephen Smalley if (value && len) { 2003570bc1c2SStephen Smalley rc = security_sid_to_context(newsid, &context, &clen); 20045e41ff9eSStephen Smalley if (rc) { 20055e41ff9eSStephen Smalley kfree(namep); 20065e41ff9eSStephen Smalley return rc; 20075e41ff9eSStephen Smalley } 20085e41ff9eSStephen Smalley *value = context; 2009570bc1c2SStephen Smalley *len = clen; 2010570bc1c2SStephen Smalley } 20115e41ff9eSStephen Smalley 20125e41ff9eSStephen Smalley return 0; 20135e41ff9eSStephen Smalley } 20145e41ff9eSStephen Smalley 20151da177e4SLinus Torvalds static int selinux_inode_create(struct inode *dir, struct dentry *dentry, int mask) 20161da177e4SLinus Torvalds { 20171da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_FILE); 20181da177e4SLinus Torvalds } 20191da177e4SLinus Torvalds 20201da177e4SLinus Torvalds static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 20211da177e4SLinus Torvalds { 20221da177e4SLinus Torvalds int rc; 20231da177e4SLinus Torvalds 20241da177e4SLinus Torvalds rc = secondary_ops->inode_link(old_dentry,dir,new_dentry); 20251da177e4SLinus Torvalds if (rc) 20261da177e4SLinus Torvalds return rc; 20271da177e4SLinus Torvalds return may_link(dir, old_dentry, MAY_LINK); 20281da177e4SLinus Torvalds } 20291da177e4SLinus Torvalds 20301da177e4SLinus Torvalds static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry) 20311da177e4SLinus Torvalds { 20321da177e4SLinus Torvalds int rc; 20331da177e4SLinus Torvalds 20341da177e4SLinus Torvalds rc = secondary_ops->inode_unlink(dir, dentry); 20351da177e4SLinus Torvalds if (rc) 20361da177e4SLinus Torvalds return rc; 20371da177e4SLinus Torvalds return may_link(dir, dentry, MAY_UNLINK); 20381da177e4SLinus Torvalds } 20391da177e4SLinus Torvalds 20401da177e4SLinus Torvalds static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name) 20411da177e4SLinus Torvalds { 20421da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_LNK_FILE); 20431da177e4SLinus Torvalds } 20441da177e4SLinus Torvalds 20451da177e4SLinus Torvalds static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, int mask) 20461da177e4SLinus Torvalds { 20471da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_DIR); 20481da177e4SLinus Torvalds } 20491da177e4SLinus Torvalds 20501da177e4SLinus Torvalds static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry) 20511da177e4SLinus Torvalds { 20521da177e4SLinus Torvalds return may_link(dir, dentry, MAY_RMDIR); 20531da177e4SLinus Torvalds } 20541da177e4SLinus Torvalds 20551da177e4SLinus Torvalds static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 20561da177e4SLinus Torvalds { 20571da177e4SLinus Torvalds int rc; 20581da177e4SLinus Torvalds 20591da177e4SLinus Torvalds rc = secondary_ops->inode_mknod(dir, dentry, mode, dev); 20601da177e4SLinus Torvalds if (rc) 20611da177e4SLinus Torvalds return rc; 20621da177e4SLinus Torvalds 20631da177e4SLinus Torvalds return may_create(dir, dentry, inode_mode_to_security_class(mode)); 20641da177e4SLinus Torvalds } 20651da177e4SLinus Torvalds 20661da177e4SLinus Torvalds static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry, 20671da177e4SLinus Torvalds struct inode *new_inode, struct dentry *new_dentry) 20681da177e4SLinus Torvalds { 20691da177e4SLinus Torvalds return may_rename(old_inode, old_dentry, new_inode, new_dentry); 20701da177e4SLinus Torvalds } 20711da177e4SLinus Torvalds 20721da177e4SLinus Torvalds static int selinux_inode_readlink(struct dentry *dentry) 20731da177e4SLinus Torvalds { 20741da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__READ); 20751da177e4SLinus Torvalds } 20761da177e4SLinus Torvalds 20771da177e4SLinus Torvalds static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata) 20781da177e4SLinus Torvalds { 20791da177e4SLinus Torvalds int rc; 20801da177e4SLinus Torvalds 20811da177e4SLinus Torvalds rc = secondary_ops->inode_follow_link(dentry,nameidata); 20821da177e4SLinus Torvalds if (rc) 20831da177e4SLinus Torvalds return rc; 20841da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__READ); 20851da177e4SLinus Torvalds } 20861da177e4SLinus Torvalds 20871da177e4SLinus Torvalds static int selinux_inode_permission(struct inode *inode, int mask, 20881da177e4SLinus Torvalds struct nameidata *nd) 20891da177e4SLinus Torvalds { 20901da177e4SLinus Torvalds int rc; 20911da177e4SLinus Torvalds 20921da177e4SLinus Torvalds rc = secondary_ops->inode_permission(inode, mask, nd); 20931da177e4SLinus Torvalds if (rc) 20941da177e4SLinus Torvalds return rc; 20951da177e4SLinus Torvalds 20961da177e4SLinus Torvalds if (!mask) { 20971da177e4SLinus Torvalds /* No permission to check. Existence test. */ 20981da177e4SLinus Torvalds return 0; 20991da177e4SLinus Torvalds } 21001da177e4SLinus Torvalds 21011da177e4SLinus Torvalds return inode_has_perm(current, inode, 21021da177e4SLinus Torvalds file_mask_to_av(inode->i_mode, mask), NULL); 21031da177e4SLinus Torvalds } 21041da177e4SLinus Torvalds 21051da177e4SLinus Torvalds static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr) 21061da177e4SLinus Torvalds { 21071da177e4SLinus Torvalds int rc; 21081da177e4SLinus Torvalds 21091da177e4SLinus Torvalds rc = secondary_ops->inode_setattr(dentry, iattr); 21101da177e4SLinus Torvalds if (rc) 21111da177e4SLinus Torvalds return rc; 21121da177e4SLinus Torvalds 21131da177e4SLinus Torvalds if (iattr->ia_valid & ATTR_FORCE) 21141da177e4SLinus Torvalds return 0; 21151da177e4SLinus Torvalds 21161da177e4SLinus Torvalds if (iattr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | 21171da177e4SLinus Torvalds ATTR_ATIME_SET | ATTR_MTIME_SET)) 21181da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__SETATTR); 21191da177e4SLinus Torvalds 21201da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__WRITE); 21211da177e4SLinus Torvalds } 21221da177e4SLinus Torvalds 21231da177e4SLinus Torvalds static int selinux_inode_getattr(struct vfsmount *mnt, struct dentry *dentry) 21241da177e4SLinus Torvalds { 21251da177e4SLinus Torvalds return dentry_has_perm(current, mnt, dentry, FILE__GETATTR); 21261da177e4SLinus Torvalds } 21271da177e4SLinus Torvalds 21281da177e4SLinus Torvalds static int selinux_inode_setxattr(struct dentry *dentry, char *name, void *value, size_t size, int flags) 21291da177e4SLinus Torvalds { 21301da177e4SLinus Torvalds struct task_security_struct *tsec = current->security; 21311da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 21321da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 21331da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 21341da177e4SLinus Torvalds struct avc_audit_data ad; 21351da177e4SLinus Torvalds u32 newsid; 21361da177e4SLinus Torvalds int rc = 0; 21371da177e4SLinus Torvalds 21381da177e4SLinus Torvalds if (strcmp(name, XATTR_NAME_SELINUX)) { 21391da177e4SLinus Torvalds if (!strncmp(name, XATTR_SECURITY_PREFIX, 21401da177e4SLinus Torvalds sizeof XATTR_SECURITY_PREFIX - 1) && 21411da177e4SLinus Torvalds !capable(CAP_SYS_ADMIN)) { 21421da177e4SLinus Torvalds /* A different attribute in the security namespace. 21431da177e4SLinus Torvalds Restrict to administrator. */ 21441da177e4SLinus Torvalds return -EPERM; 21451da177e4SLinus Torvalds } 21461da177e4SLinus Torvalds 21471da177e4SLinus Torvalds /* Not an attribute we recognize, so just check the 21481da177e4SLinus Torvalds ordinary setattr permission. */ 21491da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__SETATTR); 21501da177e4SLinus Torvalds } 21511da177e4SLinus Torvalds 21521da177e4SLinus Torvalds sbsec = inode->i_sb->s_security; 21531da177e4SLinus Torvalds if (sbsec->behavior == SECURITY_FS_USE_MNTPOINT) 21541da177e4SLinus Torvalds return -EOPNOTSUPP; 21551da177e4SLinus Torvalds 21561da177e4SLinus Torvalds if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER)) 21571da177e4SLinus Torvalds return -EPERM; 21581da177e4SLinus Torvalds 21591da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,FS); 21601da177e4SLinus Torvalds ad.u.fs.dentry = dentry; 21611da177e4SLinus Torvalds 21621da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, isec->sclass, 21631da177e4SLinus Torvalds FILE__RELABELFROM, &ad); 21641da177e4SLinus Torvalds if (rc) 21651da177e4SLinus Torvalds return rc; 21661da177e4SLinus Torvalds 21671da177e4SLinus Torvalds rc = security_context_to_sid(value, size, &newsid); 21681da177e4SLinus Torvalds if (rc) 21691da177e4SLinus Torvalds return rc; 21701da177e4SLinus Torvalds 21711da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, newsid, isec->sclass, 21721da177e4SLinus Torvalds FILE__RELABELTO, &ad); 21731da177e4SLinus Torvalds if (rc) 21741da177e4SLinus Torvalds return rc; 21751da177e4SLinus Torvalds 21761da177e4SLinus Torvalds rc = security_validate_transition(isec->sid, newsid, tsec->sid, 21771da177e4SLinus Torvalds isec->sclass); 21781da177e4SLinus Torvalds if (rc) 21791da177e4SLinus Torvalds return rc; 21801da177e4SLinus Torvalds 21811da177e4SLinus Torvalds return avc_has_perm(newsid, 21821da177e4SLinus Torvalds sbsec->sid, 21831da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 21841da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, 21851da177e4SLinus Torvalds &ad); 21861da177e4SLinus Torvalds } 21871da177e4SLinus Torvalds 21881da177e4SLinus Torvalds static void selinux_inode_post_setxattr(struct dentry *dentry, char *name, 21891da177e4SLinus Torvalds void *value, size_t size, int flags) 21901da177e4SLinus Torvalds { 21911da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 21921da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 21931da177e4SLinus Torvalds u32 newsid; 21941da177e4SLinus Torvalds int rc; 21951da177e4SLinus Torvalds 21961da177e4SLinus Torvalds if (strcmp(name, XATTR_NAME_SELINUX)) { 21971da177e4SLinus Torvalds /* Not an attribute we recognize, so nothing to do. */ 21981da177e4SLinus Torvalds return; 21991da177e4SLinus Torvalds } 22001da177e4SLinus Torvalds 22011da177e4SLinus Torvalds rc = security_context_to_sid(value, size, &newsid); 22021da177e4SLinus Torvalds if (rc) { 22031da177e4SLinus Torvalds printk(KERN_WARNING "%s: unable to obtain SID for context " 22041da177e4SLinus Torvalds "%s, rc=%d\n", __FUNCTION__, (char*)value, -rc); 22051da177e4SLinus Torvalds return; 22061da177e4SLinus Torvalds } 22071da177e4SLinus Torvalds 22081da177e4SLinus Torvalds isec->sid = newsid; 22091da177e4SLinus Torvalds return; 22101da177e4SLinus Torvalds } 22111da177e4SLinus Torvalds 22121da177e4SLinus Torvalds static int selinux_inode_getxattr (struct dentry *dentry, char *name) 22131da177e4SLinus Torvalds { 22141da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__GETATTR); 22151da177e4SLinus Torvalds } 22161da177e4SLinus Torvalds 22171da177e4SLinus Torvalds static int selinux_inode_listxattr (struct dentry *dentry) 22181da177e4SLinus Torvalds { 22191da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__GETATTR); 22201da177e4SLinus Torvalds } 22211da177e4SLinus Torvalds 22221da177e4SLinus Torvalds static int selinux_inode_removexattr (struct dentry *dentry, char *name) 22231da177e4SLinus Torvalds { 22241da177e4SLinus Torvalds if (strcmp(name, XATTR_NAME_SELINUX)) { 22251da177e4SLinus Torvalds if (!strncmp(name, XATTR_SECURITY_PREFIX, 22261da177e4SLinus Torvalds sizeof XATTR_SECURITY_PREFIX - 1) && 22271da177e4SLinus Torvalds !capable(CAP_SYS_ADMIN)) { 22281da177e4SLinus Torvalds /* A different attribute in the security namespace. 22291da177e4SLinus Torvalds Restrict to administrator. */ 22301da177e4SLinus Torvalds return -EPERM; 22311da177e4SLinus Torvalds } 22321da177e4SLinus Torvalds 22331da177e4SLinus Torvalds /* Not an attribute we recognize, so just check the 22341da177e4SLinus Torvalds ordinary setattr permission. Might want a separate 22351da177e4SLinus Torvalds permission for removexattr. */ 22361da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__SETATTR); 22371da177e4SLinus Torvalds } 22381da177e4SLinus Torvalds 22391da177e4SLinus Torvalds /* No one is allowed to remove a SELinux security label. 22401da177e4SLinus Torvalds You can change the label, but all data must be labeled. */ 22411da177e4SLinus Torvalds return -EACCES; 22421da177e4SLinus Torvalds } 22431da177e4SLinus Torvalds 22448c8570fbSDustin Kirkland static const char *selinux_inode_xattr_getsuffix(void) 22458c8570fbSDustin Kirkland { 22468c8570fbSDustin Kirkland return XATTR_SELINUX_SUFFIX; 22478c8570fbSDustin Kirkland } 22488c8570fbSDustin Kirkland 2249d381d8a9SJames Morris /* 2250d381d8a9SJames Morris * Copy the in-core inode security context value to the user. If the 2251d381d8a9SJames Morris * getxattr() prior to this succeeded, check to see if we need to 2252d381d8a9SJames Morris * canonicalize the value to be finally returned to the user. 2253d381d8a9SJames Morris * 2254d381d8a9SJames Morris * Permission check is handled by selinux_inode_getxattr hook. 2255d381d8a9SJames Morris */ 22567306a0b9SDustin Kirkland static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void *buffer, size_t size, int err) 22571da177e4SLinus Torvalds { 22581da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 22591da177e4SLinus Torvalds 22608c8570fbSDustin Kirkland if (strcmp(name, XATTR_SELINUX_SUFFIX)) 22618c8570fbSDustin Kirkland return -EOPNOTSUPP; 22621da177e4SLinus Torvalds 22638c8570fbSDustin Kirkland return selinux_getsecurity(isec->sid, buffer, size); 22641da177e4SLinus Torvalds } 22651da177e4SLinus Torvalds 22661da177e4SLinus Torvalds static int selinux_inode_setsecurity(struct inode *inode, const char *name, 22671da177e4SLinus Torvalds const void *value, size_t size, int flags) 22681da177e4SLinus Torvalds { 22691da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 22701da177e4SLinus Torvalds u32 newsid; 22711da177e4SLinus Torvalds int rc; 22721da177e4SLinus Torvalds 22731da177e4SLinus Torvalds if (strcmp(name, XATTR_SELINUX_SUFFIX)) 22741da177e4SLinus Torvalds return -EOPNOTSUPP; 22751da177e4SLinus Torvalds 22761da177e4SLinus Torvalds if (!value || !size) 22771da177e4SLinus Torvalds return -EACCES; 22781da177e4SLinus Torvalds 22791da177e4SLinus Torvalds rc = security_context_to_sid((void*)value, size, &newsid); 22801da177e4SLinus Torvalds if (rc) 22811da177e4SLinus Torvalds return rc; 22821da177e4SLinus Torvalds 22831da177e4SLinus Torvalds isec->sid = newsid; 22841da177e4SLinus Torvalds return 0; 22851da177e4SLinus Torvalds } 22861da177e4SLinus Torvalds 22871da177e4SLinus Torvalds static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size) 22881da177e4SLinus Torvalds { 22891da177e4SLinus Torvalds const int len = sizeof(XATTR_NAME_SELINUX); 22901da177e4SLinus Torvalds if (buffer && len <= buffer_size) 22911da177e4SLinus Torvalds memcpy(buffer, XATTR_NAME_SELINUX, len); 22921da177e4SLinus Torvalds return len; 22931da177e4SLinus Torvalds } 22941da177e4SLinus Torvalds 22951da177e4SLinus Torvalds /* file security operations */ 22961da177e4SLinus Torvalds 22971da177e4SLinus Torvalds static int selinux_file_permission(struct file *file, int mask) 22981da177e4SLinus Torvalds { 22991da177e4SLinus Torvalds struct inode *inode = file->f_dentry->d_inode; 23001da177e4SLinus Torvalds 23011da177e4SLinus Torvalds if (!mask) { 23021da177e4SLinus Torvalds /* No permission to check. Existence test. */ 23031da177e4SLinus Torvalds return 0; 23041da177e4SLinus Torvalds } 23051da177e4SLinus Torvalds 23061da177e4SLinus Torvalds /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */ 23071da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE)) 23081da177e4SLinus Torvalds mask |= MAY_APPEND; 23091da177e4SLinus Torvalds 23101da177e4SLinus Torvalds return file_has_perm(current, file, 23111da177e4SLinus Torvalds file_mask_to_av(inode->i_mode, mask)); 23121da177e4SLinus Torvalds } 23131da177e4SLinus Torvalds 23141da177e4SLinus Torvalds static int selinux_file_alloc_security(struct file *file) 23151da177e4SLinus Torvalds { 23161da177e4SLinus Torvalds return file_alloc_security(file); 23171da177e4SLinus Torvalds } 23181da177e4SLinus Torvalds 23191da177e4SLinus Torvalds static void selinux_file_free_security(struct file *file) 23201da177e4SLinus Torvalds { 23211da177e4SLinus Torvalds file_free_security(file); 23221da177e4SLinus Torvalds } 23231da177e4SLinus Torvalds 23241da177e4SLinus Torvalds static int selinux_file_ioctl(struct file *file, unsigned int cmd, 23251da177e4SLinus Torvalds unsigned long arg) 23261da177e4SLinus Torvalds { 23271da177e4SLinus Torvalds int error = 0; 23281da177e4SLinus Torvalds 23291da177e4SLinus Torvalds switch (cmd) { 23301da177e4SLinus Torvalds case FIONREAD: 23311da177e4SLinus Torvalds /* fall through */ 23321da177e4SLinus Torvalds case FIBMAP: 23331da177e4SLinus Torvalds /* fall through */ 23341da177e4SLinus Torvalds case FIGETBSZ: 23351da177e4SLinus Torvalds /* fall through */ 23361da177e4SLinus Torvalds case EXT2_IOC_GETFLAGS: 23371da177e4SLinus Torvalds /* fall through */ 23381da177e4SLinus Torvalds case EXT2_IOC_GETVERSION: 23391da177e4SLinus Torvalds error = file_has_perm(current, file, FILE__GETATTR); 23401da177e4SLinus Torvalds break; 23411da177e4SLinus Torvalds 23421da177e4SLinus Torvalds case EXT2_IOC_SETFLAGS: 23431da177e4SLinus Torvalds /* fall through */ 23441da177e4SLinus Torvalds case EXT2_IOC_SETVERSION: 23451da177e4SLinus Torvalds error = file_has_perm(current, file, FILE__SETATTR); 23461da177e4SLinus Torvalds break; 23471da177e4SLinus Torvalds 23481da177e4SLinus Torvalds /* sys_ioctl() checks */ 23491da177e4SLinus Torvalds case FIONBIO: 23501da177e4SLinus Torvalds /* fall through */ 23511da177e4SLinus Torvalds case FIOASYNC: 23521da177e4SLinus Torvalds error = file_has_perm(current, file, 0); 23531da177e4SLinus Torvalds break; 23541da177e4SLinus Torvalds 23551da177e4SLinus Torvalds case KDSKBENT: 23561da177e4SLinus Torvalds case KDSKBSENT: 23571da177e4SLinus Torvalds error = task_has_capability(current,CAP_SYS_TTY_CONFIG); 23581da177e4SLinus Torvalds break; 23591da177e4SLinus Torvalds 23601da177e4SLinus Torvalds /* default case assumes that the command will go 23611da177e4SLinus Torvalds * to the file's ioctl() function. 23621da177e4SLinus Torvalds */ 23631da177e4SLinus Torvalds default: 23641da177e4SLinus Torvalds error = file_has_perm(current, file, FILE__IOCTL); 23651da177e4SLinus Torvalds 23661da177e4SLinus Torvalds } 23671da177e4SLinus Torvalds return error; 23681da177e4SLinus Torvalds } 23691da177e4SLinus Torvalds 23701da177e4SLinus Torvalds static int file_map_prot_check(struct file *file, unsigned long prot, int shared) 23711da177e4SLinus Torvalds { 23721da177e4SLinus Torvalds #ifndef CONFIG_PPC32 23731da177e4SLinus Torvalds if ((prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) { 23741da177e4SLinus Torvalds /* 23751da177e4SLinus Torvalds * We are making executable an anonymous mapping or a 23761da177e4SLinus Torvalds * private file mapping that will also be writable. 23771da177e4SLinus Torvalds * This has an additional check. 23781da177e4SLinus Torvalds */ 23791da177e4SLinus Torvalds int rc = task_has_perm(current, current, PROCESS__EXECMEM); 23801da177e4SLinus Torvalds if (rc) 23811da177e4SLinus Torvalds return rc; 23821da177e4SLinus Torvalds } 23831da177e4SLinus Torvalds #endif 23841da177e4SLinus Torvalds 23851da177e4SLinus Torvalds if (file) { 23861da177e4SLinus Torvalds /* read access is always possible with a mapping */ 23871da177e4SLinus Torvalds u32 av = FILE__READ; 23881da177e4SLinus Torvalds 23891da177e4SLinus Torvalds /* write access only matters if the mapping is shared */ 23901da177e4SLinus Torvalds if (shared && (prot & PROT_WRITE)) 23911da177e4SLinus Torvalds av |= FILE__WRITE; 23921da177e4SLinus Torvalds 23931da177e4SLinus Torvalds if (prot & PROT_EXEC) 23941da177e4SLinus Torvalds av |= FILE__EXECUTE; 23951da177e4SLinus Torvalds 23961da177e4SLinus Torvalds return file_has_perm(current, file, av); 23971da177e4SLinus Torvalds } 23981da177e4SLinus Torvalds return 0; 23991da177e4SLinus Torvalds } 24001da177e4SLinus Torvalds 24011da177e4SLinus Torvalds static int selinux_file_mmap(struct file *file, unsigned long reqprot, 24021da177e4SLinus Torvalds unsigned long prot, unsigned long flags) 24031da177e4SLinus Torvalds { 24041da177e4SLinus Torvalds int rc; 24051da177e4SLinus Torvalds 24061da177e4SLinus Torvalds rc = secondary_ops->file_mmap(file, reqprot, prot, flags); 24071da177e4SLinus Torvalds if (rc) 24081da177e4SLinus Torvalds return rc; 24091da177e4SLinus Torvalds 24101da177e4SLinus Torvalds if (selinux_checkreqprot) 24111da177e4SLinus Torvalds prot = reqprot; 24121da177e4SLinus Torvalds 24131da177e4SLinus Torvalds return file_map_prot_check(file, prot, 24141da177e4SLinus Torvalds (flags & MAP_TYPE) == MAP_SHARED); 24151da177e4SLinus Torvalds } 24161da177e4SLinus Torvalds 24171da177e4SLinus Torvalds static int selinux_file_mprotect(struct vm_area_struct *vma, 24181da177e4SLinus Torvalds unsigned long reqprot, 24191da177e4SLinus Torvalds unsigned long prot) 24201da177e4SLinus Torvalds { 24211da177e4SLinus Torvalds int rc; 24221da177e4SLinus Torvalds 24231da177e4SLinus Torvalds rc = secondary_ops->file_mprotect(vma, reqprot, prot); 24241da177e4SLinus Torvalds if (rc) 24251da177e4SLinus Torvalds return rc; 24261da177e4SLinus Torvalds 24271da177e4SLinus Torvalds if (selinux_checkreqprot) 24281da177e4SLinus Torvalds prot = reqprot; 24291da177e4SLinus Torvalds 24301da177e4SLinus Torvalds #ifndef CONFIG_PPC32 2431db4c9641SStephen Smalley if ((prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) { 2432db4c9641SStephen Smalley rc = 0; 2433db4c9641SStephen Smalley if (vma->vm_start >= vma->vm_mm->start_brk && 2434db4c9641SStephen Smalley vma->vm_end <= vma->vm_mm->brk) { 2435db4c9641SStephen Smalley rc = task_has_perm(current, current, 2436db4c9641SStephen Smalley PROCESS__EXECHEAP); 2437db4c9641SStephen Smalley } else if (!vma->vm_file && 24386b992197SLorenzo Hernandez García-Hierro vma->vm_start <= vma->vm_mm->start_stack && 24396b992197SLorenzo Hernandez García-Hierro vma->vm_end >= vma->vm_mm->start_stack) { 24406b992197SLorenzo Hernandez García-Hierro rc = task_has_perm(current, current, PROCESS__EXECSTACK); 2441db4c9641SStephen Smalley } else if (vma->vm_file && vma->anon_vma) { 2442db4c9641SStephen Smalley /* 2443db4c9641SStephen Smalley * We are making executable a file mapping that has 2444db4c9641SStephen Smalley * had some COW done. Since pages might have been 2445db4c9641SStephen Smalley * written, check ability to execute the possibly 2446db4c9641SStephen Smalley * modified content. This typically should only 2447db4c9641SStephen Smalley * occur for text relocations. 2448db4c9641SStephen Smalley */ 2449db4c9641SStephen Smalley rc = file_has_perm(current, vma->vm_file, 2450db4c9641SStephen Smalley FILE__EXECMOD); 2451db4c9641SStephen Smalley } 24526b992197SLorenzo Hernandez García-Hierro if (rc) 24536b992197SLorenzo Hernandez García-Hierro return rc; 24546b992197SLorenzo Hernandez García-Hierro } 24551da177e4SLinus Torvalds #endif 24561da177e4SLinus Torvalds 24571da177e4SLinus Torvalds return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED); 24581da177e4SLinus Torvalds } 24591da177e4SLinus Torvalds 24601da177e4SLinus Torvalds static int selinux_file_lock(struct file *file, unsigned int cmd) 24611da177e4SLinus Torvalds { 24621da177e4SLinus Torvalds return file_has_perm(current, file, FILE__LOCK); 24631da177e4SLinus Torvalds } 24641da177e4SLinus Torvalds 24651da177e4SLinus Torvalds static int selinux_file_fcntl(struct file *file, unsigned int cmd, 24661da177e4SLinus Torvalds unsigned long arg) 24671da177e4SLinus Torvalds { 24681da177e4SLinus Torvalds int err = 0; 24691da177e4SLinus Torvalds 24701da177e4SLinus Torvalds switch (cmd) { 24711da177e4SLinus Torvalds case F_SETFL: 24721da177e4SLinus Torvalds if (!file->f_dentry || !file->f_dentry->d_inode) { 24731da177e4SLinus Torvalds err = -EINVAL; 24741da177e4SLinus Torvalds break; 24751da177e4SLinus Torvalds } 24761da177e4SLinus Torvalds 24771da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) { 24781da177e4SLinus Torvalds err = file_has_perm(current, file,FILE__WRITE); 24791da177e4SLinus Torvalds break; 24801da177e4SLinus Torvalds } 24811da177e4SLinus Torvalds /* fall through */ 24821da177e4SLinus Torvalds case F_SETOWN: 24831da177e4SLinus Torvalds case F_SETSIG: 24841da177e4SLinus Torvalds case F_GETFL: 24851da177e4SLinus Torvalds case F_GETOWN: 24861da177e4SLinus Torvalds case F_GETSIG: 24871da177e4SLinus Torvalds /* Just check FD__USE permission */ 24881da177e4SLinus Torvalds err = file_has_perm(current, file, 0); 24891da177e4SLinus Torvalds break; 24901da177e4SLinus Torvalds case F_GETLK: 24911da177e4SLinus Torvalds case F_SETLK: 24921da177e4SLinus Torvalds case F_SETLKW: 24931da177e4SLinus Torvalds #if BITS_PER_LONG == 32 24941da177e4SLinus Torvalds case F_GETLK64: 24951da177e4SLinus Torvalds case F_SETLK64: 24961da177e4SLinus Torvalds case F_SETLKW64: 24971da177e4SLinus Torvalds #endif 24981da177e4SLinus Torvalds if (!file->f_dentry || !file->f_dentry->d_inode) { 24991da177e4SLinus Torvalds err = -EINVAL; 25001da177e4SLinus Torvalds break; 25011da177e4SLinus Torvalds } 25021da177e4SLinus Torvalds err = file_has_perm(current, file, FILE__LOCK); 25031da177e4SLinus Torvalds break; 25041da177e4SLinus Torvalds } 25051da177e4SLinus Torvalds 25061da177e4SLinus Torvalds return err; 25071da177e4SLinus Torvalds } 25081da177e4SLinus Torvalds 25091da177e4SLinus Torvalds static int selinux_file_set_fowner(struct file *file) 25101da177e4SLinus Torvalds { 25111da177e4SLinus Torvalds struct task_security_struct *tsec; 25121da177e4SLinus Torvalds struct file_security_struct *fsec; 25131da177e4SLinus Torvalds 25141da177e4SLinus Torvalds tsec = current->security; 25151da177e4SLinus Torvalds fsec = file->f_security; 25161da177e4SLinus Torvalds fsec->fown_sid = tsec->sid; 25171da177e4SLinus Torvalds 25181da177e4SLinus Torvalds return 0; 25191da177e4SLinus Torvalds } 25201da177e4SLinus Torvalds 25211da177e4SLinus Torvalds static int selinux_file_send_sigiotask(struct task_struct *tsk, 25221da177e4SLinus Torvalds struct fown_struct *fown, int signum) 25231da177e4SLinus Torvalds { 25241da177e4SLinus Torvalds struct file *file; 25251da177e4SLinus Torvalds u32 perm; 25261da177e4SLinus Torvalds struct task_security_struct *tsec; 25271da177e4SLinus Torvalds struct file_security_struct *fsec; 25281da177e4SLinus Torvalds 25291da177e4SLinus Torvalds /* struct fown_struct is never outside the context of a struct file */ 25301da177e4SLinus Torvalds file = (struct file *)((long)fown - offsetof(struct file,f_owner)); 25311da177e4SLinus Torvalds 25321da177e4SLinus Torvalds tsec = tsk->security; 25331da177e4SLinus Torvalds fsec = file->f_security; 25341da177e4SLinus Torvalds 25351da177e4SLinus Torvalds if (!signum) 25361da177e4SLinus Torvalds perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */ 25371da177e4SLinus Torvalds else 25381da177e4SLinus Torvalds perm = signal_to_av(signum); 25391da177e4SLinus Torvalds 25401da177e4SLinus Torvalds return avc_has_perm(fsec->fown_sid, tsec->sid, 25411da177e4SLinus Torvalds SECCLASS_PROCESS, perm, NULL); 25421da177e4SLinus Torvalds } 25431da177e4SLinus Torvalds 25441da177e4SLinus Torvalds static int selinux_file_receive(struct file *file) 25451da177e4SLinus Torvalds { 25461da177e4SLinus Torvalds return file_has_perm(current, file, file_to_av(file)); 25471da177e4SLinus Torvalds } 25481da177e4SLinus Torvalds 25491da177e4SLinus Torvalds /* task security operations */ 25501da177e4SLinus Torvalds 25511da177e4SLinus Torvalds static int selinux_task_create(unsigned long clone_flags) 25521da177e4SLinus Torvalds { 25531da177e4SLinus Torvalds int rc; 25541da177e4SLinus Torvalds 25551da177e4SLinus Torvalds rc = secondary_ops->task_create(clone_flags); 25561da177e4SLinus Torvalds if (rc) 25571da177e4SLinus Torvalds return rc; 25581da177e4SLinus Torvalds 25591da177e4SLinus Torvalds return task_has_perm(current, current, PROCESS__FORK); 25601da177e4SLinus Torvalds } 25611da177e4SLinus Torvalds 25621da177e4SLinus Torvalds static int selinux_task_alloc_security(struct task_struct *tsk) 25631da177e4SLinus Torvalds { 25641da177e4SLinus Torvalds struct task_security_struct *tsec1, *tsec2; 25651da177e4SLinus Torvalds int rc; 25661da177e4SLinus Torvalds 25671da177e4SLinus Torvalds tsec1 = current->security; 25681da177e4SLinus Torvalds 25691da177e4SLinus Torvalds rc = task_alloc_security(tsk); 25701da177e4SLinus Torvalds if (rc) 25711da177e4SLinus Torvalds return rc; 25721da177e4SLinus Torvalds tsec2 = tsk->security; 25731da177e4SLinus Torvalds 25741da177e4SLinus Torvalds tsec2->osid = tsec1->osid; 25751da177e4SLinus Torvalds tsec2->sid = tsec1->sid; 25761da177e4SLinus Torvalds 25771da177e4SLinus Torvalds /* Retain the exec and create SIDs across fork */ 25781da177e4SLinus Torvalds tsec2->exec_sid = tsec1->exec_sid; 25791da177e4SLinus Torvalds tsec2->create_sid = tsec1->create_sid; 25801da177e4SLinus Torvalds 25811da177e4SLinus Torvalds /* Retain ptracer SID across fork, if any. 25821da177e4SLinus Torvalds This will be reset by the ptrace hook upon any 25831da177e4SLinus Torvalds subsequent ptrace_attach operations. */ 25841da177e4SLinus Torvalds tsec2->ptrace_sid = tsec1->ptrace_sid; 25851da177e4SLinus Torvalds 25861da177e4SLinus Torvalds return 0; 25871da177e4SLinus Torvalds } 25881da177e4SLinus Torvalds 25891da177e4SLinus Torvalds static void selinux_task_free_security(struct task_struct *tsk) 25901da177e4SLinus Torvalds { 25911da177e4SLinus Torvalds task_free_security(tsk); 25921da177e4SLinus Torvalds } 25931da177e4SLinus Torvalds 25941da177e4SLinus Torvalds static int selinux_task_setuid(uid_t id0, uid_t id1, uid_t id2, int flags) 25951da177e4SLinus Torvalds { 25961da177e4SLinus Torvalds /* Since setuid only affects the current process, and 25971da177e4SLinus Torvalds since the SELinux controls are not based on the Linux 25981da177e4SLinus Torvalds identity attributes, SELinux does not need to control 25991da177e4SLinus Torvalds this operation. However, SELinux does control the use 26001da177e4SLinus Torvalds of the CAP_SETUID and CAP_SETGID capabilities using the 26011da177e4SLinus Torvalds capable hook. */ 26021da177e4SLinus Torvalds return 0; 26031da177e4SLinus Torvalds } 26041da177e4SLinus Torvalds 26051da177e4SLinus Torvalds static int selinux_task_post_setuid(uid_t id0, uid_t id1, uid_t id2, int flags) 26061da177e4SLinus Torvalds { 26071da177e4SLinus Torvalds return secondary_ops->task_post_setuid(id0,id1,id2,flags); 26081da177e4SLinus Torvalds } 26091da177e4SLinus Torvalds 26101da177e4SLinus Torvalds static int selinux_task_setgid(gid_t id0, gid_t id1, gid_t id2, int flags) 26111da177e4SLinus Torvalds { 26121da177e4SLinus Torvalds /* See the comment for setuid above. */ 26131da177e4SLinus Torvalds return 0; 26141da177e4SLinus Torvalds } 26151da177e4SLinus Torvalds 26161da177e4SLinus Torvalds static int selinux_task_setpgid(struct task_struct *p, pid_t pgid) 26171da177e4SLinus Torvalds { 26181da177e4SLinus Torvalds return task_has_perm(current, p, PROCESS__SETPGID); 26191da177e4SLinus Torvalds } 26201da177e4SLinus Torvalds 26211da177e4SLinus Torvalds static int selinux_task_getpgid(struct task_struct *p) 26221da177e4SLinus Torvalds { 26231da177e4SLinus Torvalds return task_has_perm(current, p, PROCESS__GETPGID); 26241da177e4SLinus Torvalds } 26251da177e4SLinus Torvalds 26261da177e4SLinus Torvalds static int selinux_task_getsid(struct task_struct *p) 26271da177e4SLinus Torvalds { 26281da177e4SLinus Torvalds return task_has_perm(current, p, PROCESS__GETSESSION); 26291da177e4SLinus Torvalds } 26301da177e4SLinus Torvalds 26311da177e4SLinus Torvalds static int selinux_task_setgroups(struct group_info *group_info) 26321da177e4SLinus Torvalds { 26331da177e4SLinus Torvalds /* See the comment for setuid above. */ 26341da177e4SLinus Torvalds return 0; 26351da177e4SLinus Torvalds } 26361da177e4SLinus Torvalds 26371da177e4SLinus Torvalds static int selinux_task_setnice(struct task_struct *p, int nice) 26381da177e4SLinus Torvalds { 26391da177e4SLinus Torvalds int rc; 26401da177e4SLinus Torvalds 26411da177e4SLinus Torvalds rc = secondary_ops->task_setnice(p, nice); 26421da177e4SLinus Torvalds if (rc) 26431da177e4SLinus Torvalds return rc; 26441da177e4SLinus Torvalds 26451da177e4SLinus Torvalds return task_has_perm(current,p, PROCESS__SETSCHED); 26461da177e4SLinus Torvalds } 26471da177e4SLinus Torvalds 264803e68060SJames Morris static int selinux_task_setioprio(struct task_struct *p, int ioprio) 264903e68060SJames Morris { 265003e68060SJames Morris return task_has_perm(current, p, PROCESS__SETSCHED); 265103e68060SJames Morris } 265203e68060SJames Morris 26531da177e4SLinus Torvalds static int selinux_task_setrlimit(unsigned int resource, struct rlimit *new_rlim) 26541da177e4SLinus Torvalds { 26551da177e4SLinus Torvalds struct rlimit *old_rlim = current->signal->rlim + resource; 26561da177e4SLinus Torvalds int rc; 26571da177e4SLinus Torvalds 26581da177e4SLinus Torvalds rc = secondary_ops->task_setrlimit(resource, new_rlim); 26591da177e4SLinus Torvalds if (rc) 26601da177e4SLinus Torvalds return rc; 26611da177e4SLinus Torvalds 26621da177e4SLinus Torvalds /* Control the ability to change the hard limit (whether 26631da177e4SLinus Torvalds lowering or raising it), so that the hard limit can 26641da177e4SLinus Torvalds later be used as a safe reset point for the soft limit 26651da177e4SLinus Torvalds upon context transitions. See selinux_bprm_apply_creds. */ 26661da177e4SLinus Torvalds if (old_rlim->rlim_max != new_rlim->rlim_max) 26671da177e4SLinus Torvalds return task_has_perm(current, current, PROCESS__SETRLIMIT); 26681da177e4SLinus Torvalds 26691da177e4SLinus Torvalds return 0; 26701da177e4SLinus Torvalds } 26711da177e4SLinus Torvalds 26721da177e4SLinus Torvalds static int selinux_task_setscheduler(struct task_struct *p, int policy, struct sched_param *lp) 26731da177e4SLinus Torvalds { 26741da177e4SLinus Torvalds return task_has_perm(current, p, PROCESS__SETSCHED); 26751da177e4SLinus Torvalds } 26761da177e4SLinus Torvalds 26771da177e4SLinus Torvalds static int selinux_task_getscheduler(struct task_struct *p) 26781da177e4SLinus Torvalds { 26791da177e4SLinus Torvalds return task_has_perm(current, p, PROCESS__GETSCHED); 26801da177e4SLinus Torvalds } 26811da177e4SLinus Torvalds 2682*35601547SDavid Quigley static int selinux_task_movememory(struct task_struct *p) 2683*35601547SDavid Quigley { 2684*35601547SDavid Quigley return task_has_perm(current, p, PROCESS__SETSCHED); 2685*35601547SDavid Quigley } 2686*35601547SDavid Quigley 26871da177e4SLinus Torvalds static int selinux_task_kill(struct task_struct *p, struct siginfo *info, int sig) 26881da177e4SLinus Torvalds { 26891da177e4SLinus Torvalds u32 perm; 26901da177e4SLinus Torvalds int rc; 26911da177e4SLinus Torvalds 26921da177e4SLinus Torvalds rc = secondary_ops->task_kill(p, info, sig); 26931da177e4SLinus Torvalds if (rc) 26941da177e4SLinus Torvalds return rc; 26951da177e4SLinus Torvalds 2696621d3121SOleg Nesterov if (info != SEND_SIG_NOINFO && (is_si_special(info) || SI_FROMKERNEL(info))) 26971da177e4SLinus Torvalds return 0; 26981da177e4SLinus Torvalds 26991da177e4SLinus Torvalds if (!sig) 27001da177e4SLinus Torvalds perm = PROCESS__SIGNULL; /* null signal; existence test */ 27011da177e4SLinus Torvalds else 27021da177e4SLinus Torvalds perm = signal_to_av(sig); 27031da177e4SLinus Torvalds 27041da177e4SLinus Torvalds return task_has_perm(current, p, perm); 27051da177e4SLinus Torvalds } 27061da177e4SLinus Torvalds 27071da177e4SLinus Torvalds static int selinux_task_prctl(int option, 27081da177e4SLinus Torvalds unsigned long arg2, 27091da177e4SLinus Torvalds unsigned long arg3, 27101da177e4SLinus Torvalds unsigned long arg4, 27111da177e4SLinus Torvalds unsigned long arg5) 27121da177e4SLinus Torvalds { 27131da177e4SLinus Torvalds /* The current prctl operations do not appear to require 27141da177e4SLinus Torvalds any SELinux controls since they merely observe or modify 27151da177e4SLinus Torvalds the state of the current process. */ 27161da177e4SLinus Torvalds return 0; 27171da177e4SLinus Torvalds } 27181da177e4SLinus Torvalds 27191da177e4SLinus Torvalds static int selinux_task_wait(struct task_struct *p) 27201da177e4SLinus Torvalds { 27211da177e4SLinus Torvalds u32 perm; 27221da177e4SLinus Torvalds 27231da177e4SLinus Torvalds perm = signal_to_av(p->exit_signal); 27241da177e4SLinus Torvalds 27251da177e4SLinus Torvalds return task_has_perm(p, current, perm); 27261da177e4SLinus Torvalds } 27271da177e4SLinus Torvalds 27281da177e4SLinus Torvalds static void selinux_task_reparent_to_init(struct task_struct *p) 27291da177e4SLinus Torvalds { 27301da177e4SLinus Torvalds struct task_security_struct *tsec; 27311da177e4SLinus Torvalds 27321da177e4SLinus Torvalds secondary_ops->task_reparent_to_init(p); 27331da177e4SLinus Torvalds 27341da177e4SLinus Torvalds tsec = p->security; 27351da177e4SLinus Torvalds tsec->osid = tsec->sid; 27361da177e4SLinus Torvalds tsec->sid = SECINITSID_KERNEL; 27371da177e4SLinus Torvalds return; 27381da177e4SLinus Torvalds } 27391da177e4SLinus Torvalds 27401da177e4SLinus Torvalds static void selinux_task_to_inode(struct task_struct *p, 27411da177e4SLinus Torvalds struct inode *inode) 27421da177e4SLinus Torvalds { 27431da177e4SLinus Torvalds struct task_security_struct *tsec = p->security; 27441da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 27451da177e4SLinus Torvalds 27461da177e4SLinus Torvalds isec->sid = tsec->sid; 27471da177e4SLinus Torvalds isec->initialized = 1; 27481da177e4SLinus Torvalds return; 27491da177e4SLinus Torvalds } 27501da177e4SLinus Torvalds 27511da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 27521da177e4SLinus Torvalds static int selinux_parse_skb_ipv4(struct sk_buff *skb, struct avc_audit_data *ad) 27531da177e4SLinus Torvalds { 27541da177e4SLinus Torvalds int offset, ihlen, ret = -EINVAL; 27551da177e4SLinus Torvalds struct iphdr _iph, *ih; 27561da177e4SLinus Torvalds 27571da177e4SLinus Torvalds offset = skb->nh.raw - skb->data; 27581da177e4SLinus Torvalds ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph); 27591da177e4SLinus Torvalds if (ih == NULL) 27601da177e4SLinus Torvalds goto out; 27611da177e4SLinus Torvalds 27621da177e4SLinus Torvalds ihlen = ih->ihl * 4; 27631da177e4SLinus Torvalds if (ihlen < sizeof(_iph)) 27641da177e4SLinus Torvalds goto out; 27651da177e4SLinus Torvalds 27661da177e4SLinus Torvalds ad->u.net.v4info.saddr = ih->saddr; 27671da177e4SLinus Torvalds ad->u.net.v4info.daddr = ih->daddr; 27681da177e4SLinus Torvalds ret = 0; 27691da177e4SLinus Torvalds 27701da177e4SLinus Torvalds switch (ih->protocol) { 27711da177e4SLinus Torvalds case IPPROTO_TCP: { 27721da177e4SLinus Torvalds struct tcphdr _tcph, *th; 27731da177e4SLinus Torvalds 27741da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 27751da177e4SLinus Torvalds break; 27761da177e4SLinus Torvalds 27771da177e4SLinus Torvalds offset += ihlen; 27781da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 27791da177e4SLinus Torvalds if (th == NULL) 27801da177e4SLinus Torvalds break; 27811da177e4SLinus Torvalds 27821da177e4SLinus Torvalds ad->u.net.sport = th->source; 27831da177e4SLinus Torvalds ad->u.net.dport = th->dest; 27841da177e4SLinus Torvalds break; 27851da177e4SLinus Torvalds } 27861da177e4SLinus Torvalds 27871da177e4SLinus Torvalds case IPPROTO_UDP: { 27881da177e4SLinus Torvalds struct udphdr _udph, *uh; 27891da177e4SLinus Torvalds 27901da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 27911da177e4SLinus Torvalds break; 27921da177e4SLinus Torvalds 27931da177e4SLinus Torvalds offset += ihlen; 27941da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 27951da177e4SLinus Torvalds if (uh == NULL) 27961da177e4SLinus Torvalds break; 27971da177e4SLinus Torvalds 27981da177e4SLinus Torvalds ad->u.net.sport = uh->source; 27991da177e4SLinus Torvalds ad->u.net.dport = uh->dest; 28001da177e4SLinus Torvalds break; 28011da177e4SLinus Torvalds } 28021da177e4SLinus Torvalds 28031da177e4SLinus Torvalds default: 28041da177e4SLinus Torvalds break; 28051da177e4SLinus Torvalds } 28061da177e4SLinus Torvalds out: 28071da177e4SLinus Torvalds return ret; 28081da177e4SLinus Torvalds } 28091da177e4SLinus Torvalds 28101da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 28111da177e4SLinus Torvalds 28121da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 28131da177e4SLinus Torvalds static int selinux_parse_skb_ipv6(struct sk_buff *skb, struct avc_audit_data *ad) 28141da177e4SLinus Torvalds { 28151da177e4SLinus Torvalds u8 nexthdr; 28161da177e4SLinus Torvalds int ret = -EINVAL, offset; 28171da177e4SLinus Torvalds struct ipv6hdr _ipv6h, *ip6; 28181da177e4SLinus Torvalds 28191da177e4SLinus Torvalds offset = skb->nh.raw - skb->data; 28201da177e4SLinus Torvalds ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h); 28211da177e4SLinus Torvalds if (ip6 == NULL) 28221da177e4SLinus Torvalds goto out; 28231da177e4SLinus Torvalds 28241da177e4SLinus Torvalds ipv6_addr_copy(&ad->u.net.v6info.saddr, &ip6->saddr); 28251da177e4SLinus Torvalds ipv6_addr_copy(&ad->u.net.v6info.daddr, &ip6->daddr); 28261da177e4SLinus Torvalds ret = 0; 28271da177e4SLinus Torvalds 28281da177e4SLinus Torvalds nexthdr = ip6->nexthdr; 28291da177e4SLinus Torvalds offset += sizeof(_ipv6h); 28300d3d077cSHerbert Xu offset = ipv6_skip_exthdr(skb, offset, &nexthdr); 28311da177e4SLinus Torvalds if (offset < 0) 28321da177e4SLinus Torvalds goto out; 28331da177e4SLinus Torvalds 28341da177e4SLinus Torvalds switch (nexthdr) { 28351da177e4SLinus Torvalds case IPPROTO_TCP: { 28361da177e4SLinus Torvalds struct tcphdr _tcph, *th; 28371da177e4SLinus Torvalds 28381da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 28391da177e4SLinus Torvalds if (th == NULL) 28401da177e4SLinus Torvalds break; 28411da177e4SLinus Torvalds 28421da177e4SLinus Torvalds ad->u.net.sport = th->source; 28431da177e4SLinus Torvalds ad->u.net.dport = th->dest; 28441da177e4SLinus Torvalds break; 28451da177e4SLinus Torvalds } 28461da177e4SLinus Torvalds 28471da177e4SLinus Torvalds case IPPROTO_UDP: { 28481da177e4SLinus Torvalds struct udphdr _udph, *uh; 28491da177e4SLinus Torvalds 28501da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 28511da177e4SLinus Torvalds if (uh == NULL) 28521da177e4SLinus Torvalds break; 28531da177e4SLinus Torvalds 28541da177e4SLinus Torvalds ad->u.net.sport = uh->source; 28551da177e4SLinus Torvalds ad->u.net.dport = uh->dest; 28561da177e4SLinus Torvalds break; 28571da177e4SLinus Torvalds } 28581da177e4SLinus Torvalds 28591da177e4SLinus Torvalds /* includes fragments */ 28601da177e4SLinus Torvalds default: 28611da177e4SLinus Torvalds break; 28621da177e4SLinus Torvalds } 28631da177e4SLinus Torvalds out: 28641da177e4SLinus Torvalds return ret; 28651da177e4SLinus Torvalds } 28661da177e4SLinus Torvalds 28671da177e4SLinus Torvalds #endif /* IPV6 */ 28681da177e4SLinus Torvalds 28691da177e4SLinus Torvalds static int selinux_parse_skb(struct sk_buff *skb, struct avc_audit_data *ad, 28701da177e4SLinus Torvalds char **addrp, int *len, int src) 28711da177e4SLinus Torvalds { 28721da177e4SLinus Torvalds int ret = 0; 28731da177e4SLinus Torvalds 28741da177e4SLinus Torvalds switch (ad->u.net.family) { 28751da177e4SLinus Torvalds case PF_INET: 28761da177e4SLinus Torvalds ret = selinux_parse_skb_ipv4(skb, ad); 28771da177e4SLinus Torvalds if (ret || !addrp) 28781da177e4SLinus Torvalds break; 28791da177e4SLinus Torvalds *len = 4; 28801da177e4SLinus Torvalds *addrp = (char *)(src ? &ad->u.net.v4info.saddr : 28811da177e4SLinus Torvalds &ad->u.net.v4info.daddr); 28821da177e4SLinus Torvalds break; 28831da177e4SLinus Torvalds 28841da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 28851da177e4SLinus Torvalds case PF_INET6: 28861da177e4SLinus Torvalds ret = selinux_parse_skb_ipv6(skb, ad); 28871da177e4SLinus Torvalds if (ret || !addrp) 28881da177e4SLinus Torvalds break; 28891da177e4SLinus Torvalds *len = 16; 28901da177e4SLinus Torvalds *addrp = (char *)(src ? &ad->u.net.v6info.saddr : 28911da177e4SLinus Torvalds &ad->u.net.v6info.daddr); 28921da177e4SLinus Torvalds break; 28931da177e4SLinus Torvalds #endif /* IPV6 */ 28941da177e4SLinus Torvalds default: 28951da177e4SLinus Torvalds break; 28961da177e4SLinus Torvalds } 28971da177e4SLinus Torvalds 28981da177e4SLinus Torvalds return ret; 28991da177e4SLinus Torvalds } 29001da177e4SLinus Torvalds 29011da177e4SLinus Torvalds /* socket security operations */ 29021da177e4SLinus Torvalds static int socket_has_perm(struct task_struct *task, struct socket *sock, 29031da177e4SLinus Torvalds u32 perms) 29041da177e4SLinus Torvalds { 29051da177e4SLinus Torvalds struct inode_security_struct *isec; 29061da177e4SLinus Torvalds struct task_security_struct *tsec; 29071da177e4SLinus Torvalds struct avc_audit_data ad; 29081da177e4SLinus Torvalds int err = 0; 29091da177e4SLinus Torvalds 29101da177e4SLinus Torvalds tsec = task->security; 29111da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 29121da177e4SLinus Torvalds 29131da177e4SLinus Torvalds if (isec->sid == SECINITSID_KERNEL) 29141da177e4SLinus Torvalds goto out; 29151da177e4SLinus Torvalds 29161da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,NET); 29171da177e4SLinus Torvalds ad.u.net.sk = sock->sk; 29181da177e4SLinus Torvalds err = avc_has_perm(tsec->sid, isec->sid, isec->sclass, perms, &ad); 29191da177e4SLinus Torvalds 29201da177e4SLinus Torvalds out: 29211da177e4SLinus Torvalds return err; 29221da177e4SLinus Torvalds } 29231da177e4SLinus Torvalds 29241da177e4SLinus Torvalds static int selinux_socket_create(int family, int type, 29251da177e4SLinus Torvalds int protocol, int kern) 29261da177e4SLinus Torvalds { 29271da177e4SLinus Torvalds int err = 0; 29281da177e4SLinus Torvalds struct task_security_struct *tsec; 29291da177e4SLinus Torvalds 29301da177e4SLinus Torvalds if (kern) 29311da177e4SLinus Torvalds goto out; 29321da177e4SLinus Torvalds 29331da177e4SLinus Torvalds tsec = current->security; 29341da177e4SLinus Torvalds err = avc_has_perm(tsec->sid, tsec->sid, 29351da177e4SLinus Torvalds socket_type_to_security_class(family, type, 29361da177e4SLinus Torvalds protocol), SOCKET__CREATE, NULL); 29371da177e4SLinus Torvalds 29381da177e4SLinus Torvalds out: 29391da177e4SLinus Torvalds return err; 29401da177e4SLinus Torvalds } 29411da177e4SLinus Torvalds 29421da177e4SLinus Torvalds static void selinux_socket_post_create(struct socket *sock, int family, 29431da177e4SLinus Torvalds int type, int protocol, int kern) 29441da177e4SLinus Torvalds { 29451da177e4SLinus Torvalds struct inode_security_struct *isec; 29461da177e4SLinus Torvalds struct task_security_struct *tsec; 29471da177e4SLinus Torvalds 29481da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 29491da177e4SLinus Torvalds 29501da177e4SLinus Torvalds tsec = current->security; 29511da177e4SLinus Torvalds isec->sclass = socket_type_to_security_class(family, type, protocol); 29521da177e4SLinus Torvalds isec->sid = kern ? SECINITSID_KERNEL : tsec->sid; 29531da177e4SLinus Torvalds isec->initialized = 1; 29541da177e4SLinus Torvalds 29551da177e4SLinus Torvalds return; 29561da177e4SLinus Torvalds } 29571da177e4SLinus Torvalds 29581da177e4SLinus Torvalds /* Range of port numbers used to automatically bind. 29591da177e4SLinus Torvalds Need to determine whether we should perform a name_bind 29601da177e4SLinus Torvalds permission check between the socket and the port number. */ 29611da177e4SLinus Torvalds #define ip_local_port_range_0 sysctl_local_port_range[0] 29621da177e4SLinus Torvalds #define ip_local_port_range_1 sysctl_local_port_range[1] 29631da177e4SLinus Torvalds 29641da177e4SLinus Torvalds static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen) 29651da177e4SLinus Torvalds { 29661da177e4SLinus Torvalds u16 family; 29671da177e4SLinus Torvalds int err; 29681da177e4SLinus Torvalds 29691da177e4SLinus Torvalds err = socket_has_perm(current, sock, SOCKET__BIND); 29701da177e4SLinus Torvalds if (err) 29711da177e4SLinus Torvalds goto out; 29721da177e4SLinus Torvalds 29731da177e4SLinus Torvalds /* 29741da177e4SLinus Torvalds * If PF_INET or PF_INET6, check name_bind permission for the port. 297513402580SJames Morris * Multiple address binding for SCTP is not supported yet: we just 297613402580SJames Morris * check the first address now. 29771da177e4SLinus Torvalds */ 29781da177e4SLinus Torvalds family = sock->sk->sk_family; 29791da177e4SLinus Torvalds if (family == PF_INET || family == PF_INET6) { 29801da177e4SLinus Torvalds char *addrp; 29811da177e4SLinus Torvalds struct inode_security_struct *isec; 29821da177e4SLinus Torvalds struct task_security_struct *tsec; 29831da177e4SLinus Torvalds struct avc_audit_data ad; 29841da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 29851da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 29861da177e4SLinus Torvalds unsigned short snum; 29871da177e4SLinus Torvalds struct sock *sk = sock->sk; 29881da177e4SLinus Torvalds u32 sid, node_perm, addrlen; 29891da177e4SLinus Torvalds 29901da177e4SLinus Torvalds tsec = current->security; 29911da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 29921da177e4SLinus Torvalds 29931da177e4SLinus Torvalds if (family == PF_INET) { 29941da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 29951da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 29961da177e4SLinus Torvalds addrlen = sizeof(addr4->sin_addr.s_addr); 29971da177e4SLinus Torvalds addrp = (char *)&addr4->sin_addr.s_addr; 29981da177e4SLinus Torvalds } else { 29991da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 30001da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 30011da177e4SLinus Torvalds addrlen = sizeof(addr6->sin6_addr.s6_addr); 30021da177e4SLinus Torvalds addrp = (char *)&addr6->sin6_addr.s6_addr; 30031da177e4SLinus Torvalds } 30041da177e4SLinus Torvalds 30051da177e4SLinus Torvalds if (snum&&(snum < max(PROT_SOCK,ip_local_port_range_0) || 30061da177e4SLinus Torvalds snum > ip_local_port_range_1)) { 30071da177e4SLinus Torvalds err = security_port_sid(sk->sk_family, sk->sk_type, 30081da177e4SLinus Torvalds sk->sk_protocol, snum, &sid); 30091da177e4SLinus Torvalds if (err) 30101da177e4SLinus Torvalds goto out; 30111da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,NET); 30121da177e4SLinus Torvalds ad.u.net.sport = htons(snum); 30131da177e4SLinus Torvalds ad.u.net.family = family; 30141da177e4SLinus Torvalds err = avc_has_perm(isec->sid, sid, 30151da177e4SLinus Torvalds isec->sclass, 30161da177e4SLinus Torvalds SOCKET__NAME_BIND, &ad); 30171da177e4SLinus Torvalds if (err) 30181da177e4SLinus Torvalds goto out; 30191da177e4SLinus Torvalds } 30201da177e4SLinus Torvalds 302113402580SJames Morris switch(isec->sclass) { 302213402580SJames Morris case SECCLASS_TCP_SOCKET: 30231da177e4SLinus Torvalds node_perm = TCP_SOCKET__NODE_BIND; 30241da177e4SLinus Torvalds break; 30251da177e4SLinus Torvalds 302613402580SJames Morris case SECCLASS_UDP_SOCKET: 30271da177e4SLinus Torvalds node_perm = UDP_SOCKET__NODE_BIND; 30281da177e4SLinus Torvalds break; 30291da177e4SLinus Torvalds 30301da177e4SLinus Torvalds default: 30311da177e4SLinus Torvalds node_perm = RAWIP_SOCKET__NODE_BIND; 30321da177e4SLinus Torvalds break; 30331da177e4SLinus Torvalds } 30341da177e4SLinus Torvalds 30351da177e4SLinus Torvalds err = security_node_sid(family, addrp, addrlen, &sid); 30361da177e4SLinus Torvalds if (err) 30371da177e4SLinus Torvalds goto out; 30381da177e4SLinus Torvalds 30391da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,NET); 30401da177e4SLinus Torvalds ad.u.net.sport = htons(snum); 30411da177e4SLinus Torvalds ad.u.net.family = family; 30421da177e4SLinus Torvalds 30431da177e4SLinus Torvalds if (family == PF_INET) 30441da177e4SLinus Torvalds ad.u.net.v4info.saddr = addr4->sin_addr.s_addr; 30451da177e4SLinus Torvalds else 30461da177e4SLinus Torvalds ipv6_addr_copy(&ad.u.net.v6info.saddr, &addr6->sin6_addr); 30471da177e4SLinus Torvalds 30481da177e4SLinus Torvalds err = avc_has_perm(isec->sid, sid, 30491da177e4SLinus Torvalds isec->sclass, node_perm, &ad); 30501da177e4SLinus Torvalds if (err) 30511da177e4SLinus Torvalds goto out; 30521da177e4SLinus Torvalds } 30531da177e4SLinus Torvalds out: 30541da177e4SLinus Torvalds return err; 30551da177e4SLinus Torvalds } 30561da177e4SLinus Torvalds 30571da177e4SLinus Torvalds static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen) 30581da177e4SLinus Torvalds { 30591da177e4SLinus Torvalds struct inode_security_struct *isec; 30601da177e4SLinus Torvalds int err; 30611da177e4SLinus Torvalds 30621da177e4SLinus Torvalds err = socket_has_perm(current, sock, SOCKET__CONNECT); 30631da177e4SLinus Torvalds if (err) 30641da177e4SLinus Torvalds return err; 30651da177e4SLinus Torvalds 30661da177e4SLinus Torvalds /* 30671da177e4SLinus Torvalds * If a TCP socket, check name_connect permission for the port. 30681da177e4SLinus Torvalds */ 30691da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 30701da177e4SLinus Torvalds if (isec->sclass == SECCLASS_TCP_SOCKET) { 30711da177e4SLinus Torvalds struct sock *sk = sock->sk; 30721da177e4SLinus Torvalds struct avc_audit_data ad; 30731da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 30741da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 30751da177e4SLinus Torvalds unsigned short snum; 30761da177e4SLinus Torvalds u32 sid; 30771da177e4SLinus Torvalds 30781da177e4SLinus Torvalds if (sk->sk_family == PF_INET) { 30791da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 3080911656f8SStephen Smalley if (addrlen < sizeof(struct sockaddr_in)) 30811da177e4SLinus Torvalds return -EINVAL; 30821da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 30831da177e4SLinus Torvalds } else { 30841da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 3085911656f8SStephen Smalley if (addrlen < SIN6_LEN_RFC2133) 30861da177e4SLinus Torvalds return -EINVAL; 30871da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 30881da177e4SLinus Torvalds } 30891da177e4SLinus Torvalds 30901da177e4SLinus Torvalds err = security_port_sid(sk->sk_family, sk->sk_type, 30911da177e4SLinus Torvalds sk->sk_protocol, snum, &sid); 30921da177e4SLinus Torvalds if (err) 30931da177e4SLinus Torvalds goto out; 30941da177e4SLinus Torvalds 30951da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,NET); 30961da177e4SLinus Torvalds ad.u.net.dport = htons(snum); 30971da177e4SLinus Torvalds ad.u.net.family = sk->sk_family; 30981da177e4SLinus Torvalds err = avc_has_perm(isec->sid, sid, isec->sclass, 30991da177e4SLinus Torvalds TCP_SOCKET__NAME_CONNECT, &ad); 31001da177e4SLinus Torvalds if (err) 31011da177e4SLinus Torvalds goto out; 31021da177e4SLinus Torvalds } 31031da177e4SLinus Torvalds 31041da177e4SLinus Torvalds out: 31051da177e4SLinus Torvalds return err; 31061da177e4SLinus Torvalds } 31071da177e4SLinus Torvalds 31081da177e4SLinus Torvalds static int selinux_socket_listen(struct socket *sock, int backlog) 31091da177e4SLinus Torvalds { 31101da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__LISTEN); 31111da177e4SLinus Torvalds } 31121da177e4SLinus Torvalds 31131da177e4SLinus Torvalds static int selinux_socket_accept(struct socket *sock, struct socket *newsock) 31141da177e4SLinus Torvalds { 31151da177e4SLinus Torvalds int err; 31161da177e4SLinus Torvalds struct inode_security_struct *isec; 31171da177e4SLinus Torvalds struct inode_security_struct *newisec; 31181da177e4SLinus Torvalds 31191da177e4SLinus Torvalds err = socket_has_perm(current, sock, SOCKET__ACCEPT); 31201da177e4SLinus Torvalds if (err) 31211da177e4SLinus Torvalds return err; 31221da177e4SLinus Torvalds 31231da177e4SLinus Torvalds newisec = SOCK_INODE(newsock)->i_security; 31241da177e4SLinus Torvalds 31251da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 31261da177e4SLinus Torvalds newisec->sclass = isec->sclass; 31271da177e4SLinus Torvalds newisec->sid = isec->sid; 31281da177e4SLinus Torvalds newisec->initialized = 1; 31291da177e4SLinus Torvalds 31301da177e4SLinus Torvalds return 0; 31311da177e4SLinus Torvalds } 31321da177e4SLinus Torvalds 31331da177e4SLinus Torvalds static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg, 31341da177e4SLinus Torvalds int size) 31351da177e4SLinus Torvalds { 31361da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__WRITE); 31371da177e4SLinus Torvalds } 31381da177e4SLinus Torvalds 31391da177e4SLinus Torvalds static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg, 31401da177e4SLinus Torvalds int size, int flags) 31411da177e4SLinus Torvalds { 31421da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__READ); 31431da177e4SLinus Torvalds } 31441da177e4SLinus Torvalds 31451da177e4SLinus Torvalds static int selinux_socket_getsockname(struct socket *sock) 31461da177e4SLinus Torvalds { 31471da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__GETATTR); 31481da177e4SLinus Torvalds } 31491da177e4SLinus Torvalds 31501da177e4SLinus Torvalds static int selinux_socket_getpeername(struct socket *sock) 31511da177e4SLinus Torvalds { 31521da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__GETATTR); 31531da177e4SLinus Torvalds } 31541da177e4SLinus Torvalds 31551da177e4SLinus Torvalds static int selinux_socket_setsockopt(struct socket *sock,int level,int optname) 31561da177e4SLinus Torvalds { 31571da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__SETOPT); 31581da177e4SLinus Torvalds } 31591da177e4SLinus Torvalds 31601da177e4SLinus Torvalds static int selinux_socket_getsockopt(struct socket *sock, int level, 31611da177e4SLinus Torvalds int optname) 31621da177e4SLinus Torvalds { 31631da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__GETOPT); 31641da177e4SLinus Torvalds } 31651da177e4SLinus Torvalds 31661da177e4SLinus Torvalds static int selinux_socket_shutdown(struct socket *sock, int how) 31671da177e4SLinus Torvalds { 31681da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__SHUTDOWN); 31691da177e4SLinus Torvalds } 31701da177e4SLinus Torvalds 31711da177e4SLinus Torvalds static int selinux_socket_unix_stream_connect(struct socket *sock, 31721da177e4SLinus Torvalds struct socket *other, 31731da177e4SLinus Torvalds struct sock *newsk) 31741da177e4SLinus Torvalds { 31751da177e4SLinus Torvalds struct sk_security_struct *ssec; 31761da177e4SLinus Torvalds struct inode_security_struct *isec; 31771da177e4SLinus Torvalds struct inode_security_struct *other_isec; 31781da177e4SLinus Torvalds struct avc_audit_data ad; 31791da177e4SLinus Torvalds int err; 31801da177e4SLinus Torvalds 31811da177e4SLinus Torvalds err = secondary_ops->unix_stream_connect(sock, other, newsk); 31821da177e4SLinus Torvalds if (err) 31831da177e4SLinus Torvalds return err; 31841da177e4SLinus Torvalds 31851da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 31861da177e4SLinus Torvalds other_isec = SOCK_INODE(other)->i_security; 31871da177e4SLinus Torvalds 31881da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,NET); 31891da177e4SLinus Torvalds ad.u.net.sk = other->sk; 31901da177e4SLinus Torvalds 31911da177e4SLinus Torvalds err = avc_has_perm(isec->sid, other_isec->sid, 31921da177e4SLinus Torvalds isec->sclass, 31931da177e4SLinus Torvalds UNIX_STREAM_SOCKET__CONNECTTO, &ad); 31941da177e4SLinus Torvalds if (err) 31951da177e4SLinus Torvalds return err; 31961da177e4SLinus Torvalds 31971da177e4SLinus Torvalds /* connecting socket */ 31981da177e4SLinus Torvalds ssec = sock->sk->sk_security; 31991da177e4SLinus Torvalds ssec->peer_sid = other_isec->sid; 32001da177e4SLinus Torvalds 32011da177e4SLinus Torvalds /* server child socket */ 32021da177e4SLinus Torvalds ssec = newsk->sk_security; 32031da177e4SLinus Torvalds ssec->peer_sid = isec->sid; 32041da177e4SLinus Torvalds 32051da177e4SLinus Torvalds return 0; 32061da177e4SLinus Torvalds } 32071da177e4SLinus Torvalds 32081da177e4SLinus Torvalds static int selinux_socket_unix_may_send(struct socket *sock, 32091da177e4SLinus Torvalds struct socket *other) 32101da177e4SLinus Torvalds { 32111da177e4SLinus Torvalds struct inode_security_struct *isec; 32121da177e4SLinus Torvalds struct inode_security_struct *other_isec; 32131da177e4SLinus Torvalds struct avc_audit_data ad; 32141da177e4SLinus Torvalds int err; 32151da177e4SLinus Torvalds 32161da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 32171da177e4SLinus Torvalds other_isec = SOCK_INODE(other)->i_security; 32181da177e4SLinus Torvalds 32191da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,NET); 32201da177e4SLinus Torvalds ad.u.net.sk = other->sk; 32211da177e4SLinus Torvalds 32221da177e4SLinus Torvalds err = avc_has_perm(isec->sid, other_isec->sid, 32231da177e4SLinus Torvalds isec->sclass, SOCKET__SENDTO, &ad); 32241da177e4SLinus Torvalds if (err) 32251da177e4SLinus Torvalds return err; 32261da177e4SLinus Torvalds 32271da177e4SLinus Torvalds return 0; 32281da177e4SLinus Torvalds } 32291da177e4SLinus Torvalds 32304e5ab4cbSJames Morris static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb, 32314e5ab4cbSJames Morris struct avc_audit_data *ad, u32 sock_sid, u16 sock_class, 32324e5ab4cbSJames Morris u16 family, char *addrp, int len) 32331da177e4SLinus Torvalds { 32344e5ab4cbSJames Morris int err = 0; 32351da177e4SLinus Torvalds u32 netif_perm, node_perm, node_sid, if_sid, recv_perm = 0; 32361da177e4SLinus Torvalds 32374e5ab4cbSJames Morris if (!skb->dev) 32381da177e4SLinus Torvalds goto out; 32391da177e4SLinus Torvalds 32404e5ab4cbSJames Morris err = sel_netif_sids(skb->dev, &if_sid, NULL); 32411da177e4SLinus Torvalds if (err) 32421da177e4SLinus Torvalds goto out; 32431da177e4SLinus Torvalds 32441da177e4SLinus Torvalds switch (sock_class) { 32451da177e4SLinus Torvalds case SECCLASS_UDP_SOCKET: 32461da177e4SLinus Torvalds netif_perm = NETIF__UDP_RECV; 32471da177e4SLinus Torvalds node_perm = NODE__UDP_RECV; 32481da177e4SLinus Torvalds recv_perm = UDP_SOCKET__RECV_MSG; 32491da177e4SLinus Torvalds break; 32501da177e4SLinus Torvalds 32511da177e4SLinus Torvalds case SECCLASS_TCP_SOCKET: 32521da177e4SLinus Torvalds netif_perm = NETIF__TCP_RECV; 32531da177e4SLinus Torvalds node_perm = NODE__TCP_RECV; 32541da177e4SLinus Torvalds recv_perm = TCP_SOCKET__RECV_MSG; 32551da177e4SLinus Torvalds break; 32561da177e4SLinus Torvalds 32571da177e4SLinus Torvalds default: 32581da177e4SLinus Torvalds netif_perm = NETIF__RAWIP_RECV; 32591da177e4SLinus Torvalds node_perm = NODE__RAWIP_RECV; 32601da177e4SLinus Torvalds break; 32611da177e4SLinus Torvalds } 32621da177e4SLinus Torvalds 32634e5ab4cbSJames Morris err = avc_has_perm(sock_sid, if_sid, SECCLASS_NETIF, netif_perm, ad); 32641da177e4SLinus Torvalds if (err) 32651da177e4SLinus Torvalds goto out; 32661da177e4SLinus Torvalds 32671da177e4SLinus Torvalds err = security_node_sid(family, addrp, len, &node_sid); 32681da177e4SLinus Torvalds if (err) 32691da177e4SLinus Torvalds goto out; 32701da177e4SLinus Torvalds 32714e5ab4cbSJames Morris err = avc_has_perm(sock_sid, node_sid, SECCLASS_NODE, node_perm, ad); 32721da177e4SLinus Torvalds if (err) 32731da177e4SLinus Torvalds goto out; 32741da177e4SLinus Torvalds 32751da177e4SLinus Torvalds if (recv_perm) { 32761da177e4SLinus Torvalds u32 port_sid; 32771da177e4SLinus Torvalds 32781da177e4SLinus Torvalds err = security_port_sid(sk->sk_family, sk->sk_type, 32794e5ab4cbSJames Morris sk->sk_protocol, ntohs(ad->u.net.sport), 32801da177e4SLinus Torvalds &port_sid); 32811da177e4SLinus Torvalds if (err) 32821da177e4SLinus Torvalds goto out; 32831da177e4SLinus Torvalds 32841da177e4SLinus Torvalds err = avc_has_perm(sock_sid, port_sid, 32854e5ab4cbSJames Morris sock_class, recv_perm, ad); 32861da177e4SLinus Torvalds } 3287d28d1e08STrent Jaeger 32884e5ab4cbSJames Morris out: 32894e5ab4cbSJames Morris return err; 32904e5ab4cbSJames Morris } 3291d28d1e08STrent Jaeger 32924e5ab4cbSJames Morris static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) 32934e5ab4cbSJames Morris { 32944e5ab4cbSJames Morris u16 family; 32954e5ab4cbSJames Morris u16 sock_class = 0; 32964e5ab4cbSJames Morris char *addrp; 32974e5ab4cbSJames Morris int len, err = 0; 32984e5ab4cbSJames Morris u32 sock_sid = 0; 32994e5ab4cbSJames Morris struct socket *sock; 33004e5ab4cbSJames Morris struct avc_audit_data ad; 33014e5ab4cbSJames Morris 33024e5ab4cbSJames Morris family = sk->sk_family; 33034e5ab4cbSJames Morris if (family != PF_INET && family != PF_INET6) 33044e5ab4cbSJames Morris goto out; 33054e5ab4cbSJames Morris 33064e5ab4cbSJames Morris /* Handle mapped IPv4 packets arriving via IPv6 sockets */ 33074e5ab4cbSJames Morris if (family == PF_INET6 && skb->protocol == ntohs(ETH_P_IP)) 33084e5ab4cbSJames Morris family = PF_INET; 33094e5ab4cbSJames Morris 33104e5ab4cbSJames Morris read_lock_bh(&sk->sk_callback_lock); 33114e5ab4cbSJames Morris sock = sk->sk_socket; 33124e5ab4cbSJames Morris if (sock) { 33134e5ab4cbSJames Morris struct inode *inode; 33144e5ab4cbSJames Morris inode = SOCK_INODE(sock); 33154e5ab4cbSJames Morris if (inode) { 33164e5ab4cbSJames Morris struct inode_security_struct *isec; 33174e5ab4cbSJames Morris isec = inode->i_security; 33184e5ab4cbSJames Morris sock_sid = isec->sid; 33194e5ab4cbSJames Morris sock_class = isec->sclass; 33204e5ab4cbSJames Morris } 33214e5ab4cbSJames Morris } 33224e5ab4cbSJames Morris read_unlock_bh(&sk->sk_callback_lock); 33234e5ab4cbSJames Morris if (!sock_sid) 33244e5ab4cbSJames Morris goto out; 33254e5ab4cbSJames Morris 33264e5ab4cbSJames Morris AVC_AUDIT_DATA_INIT(&ad, NET); 33274e5ab4cbSJames Morris ad.u.net.netif = skb->dev ? skb->dev->name : "[unknown]"; 33284e5ab4cbSJames Morris ad.u.net.family = family; 33294e5ab4cbSJames Morris 33304e5ab4cbSJames Morris err = selinux_parse_skb(skb, &ad, &addrp, &len, 1); 33314e5ab4cbSJames Morris if (err) 33324e5ab4cbSJames Morris goto out; 33334e5ab4cbSJames Morris 33344e5ab4cbSJames Morris if (selinux_compat_net) 33354e5ab4cbSJames Morris err = selinux_sock_rcv_skb_compat(sk, skb, &ad, sock_sid, 33364e5ab4cbSJames Morris sock_class, family, 33374e5ab4cbSJames Morris addrp, len); 33384e5ab4cbSJames Morris else 33394e5ab4cbSJames Morris err = avc_has_perm(sock_sid, skb->secmark, SECCLASS_PACKET, 33404e5ab4cbSJames Morris PACKET__RECV, &ad); 33414e5ab4cbSJames Morris if (err) 33424e5ab4cbSJames Morris goto out; 33434e5ab4cbSJames Morris 33444e5ab4cbSJames Morris err = selinux_xfrm_sock_rcv_skb(sock_sid, skb); 33451da177e4SLinus Torvalds out: 33461da177e4SLinus Torvalds return err; 33471da177e4SLinus Torvalds } 33481da177e4SLinus Torvalds 33492c7946a7SCatherine Zhang static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval, 33501da177e4SLinus Torvalds int __user *optlen, unsigned len) 33511da177e4SLinus Torvalds { 33521da177e4SLinus Torvalds int err = 0; 33531da177e4SLinus Torvalds char *scontext; 33541da177e4SLinus Torvalds u32 scontext_len; 33551da177e4SLinus Torvalds struct sk_security_struct *ssec; 33561da177e4SLinus Torvalds struct inode_security_struct *isec; 33572c7946a7SCatherine Zhang u32 peer_sid = 0; 33581da177e4SLinus Torvalds 33591da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 33602c7946a7SCatherine Zhang 33612c7946a7SCatherine Zhang /* if UNIX_STREAM check peer_sid, if TCP check dst for labelled sa */ 33622c7946a7SCatherine Zhang if (isec->sclass == SECCLASS_UNIX_STREAM_SOCKET) { 33632c7946a7SCatherine Zhang ssec = sock->sk->sk_security; 33642c7946a7SCatherine Zhang peer_sid = ssec->peer_sid; 33652c7946a7SCatherine Zhang } 33662c7946a7SCatherine Zhang else if (isec->sclass == SECCLASS_TCP_SOCKET) { 33672c7946a7SCatherine Zhang peer_sid = selinux_socket_getpeer_stream(sock->sk); 33682c7946a7SCatherine Zhang 33692c7946a7SCatherine Zhang if (peer_sid == SECSID_NULL) { 33702c7946a7SCatherine Zhang err = -ENOPROTOOPT; 33712c7946a7SCatherine Zhang goto out; 33722c7946a7SCatherine Zhang } 33732c7946a7SCatherine Zhang } 33742c7946a7SCatherine Zhang else { 33751da177e4SLinus Torvalds err = -ENOPROTOOPT; 33761da177e4SLinus Torvalds goto out; 33771da177e4SLinus Torvalds } 33781da177e4SLinus Torvalds 33792c7946a7SCatherine Zhang err = security_sid_to_context(peer_sid, &scontext, &scontext_len); 33801da177e4SLinus Torvalds 33811da177e4SLinus Torvalds if (err) 33821da177e4SLinus Torvalds goto out; 33831da177e4SLinus Torvalds 33841da177e4SLinus Torvalds if (scontext_len > len) { 33851da177e4SLinus Torvalds err = -ERANGE; 33861da177e4SLinus Torvalds goto out_len; 33871da177e4SLinus Torvalds } 33881da177e4SLinus Torvalds 33891da177e4SLinus Torvalds if (copy_to_user(optval, scontext, scontext_len)) 33901da177e4SLinus Torvalds err = -EFAULT; 33911da177e4SLinus Torvalds 33921da177e4SLinus Torvalds out_len: 33931da177e4SLinus Torvalds if (put_user(scontext_len, optlen)) 33941da177e4SLinus Torvalds err = -EFAULT; 33951da177e4SLinus Torvalds 33961da177e4SLinus Torvalds kfree(scontext); 33971da177e4SLinus Torvalds out: 33981da177e4SLinus Torvalds return err; 33991da177e4SLinus Torvalds } 34001da177e4SLinus Torvalds 34012c7946a7SCatherine Zhang static int selinux_socket_getpeersec_dgram(struct sk_buff *skb, char **secdata, u32 *seclen) 34022c7946a7SCatherine Zhang { 34032c7946a7SCatherine Zhang int err = 0; 34042c7946a7SCatherine Zhang u32 peer_sid = selinux_socket_getpeer_dgram(skb); 34052c7946a7SCatherine Zhang 34062c7946a7SCatherine Zhang if (peer_sid == SECSID_NULL) 34072c7946a7SCatherine Zhang return -EINVAL; 34082c7946a7SCatherine Zhang 34092c7946a7SCatherine Zhang err = security_sid_to_context(peer_sid, secdata, seclen); 34102c7946a7SCatherine Zhang if (err) 34112c7946a7SCatherine Zhang return err; 34122c7946a7SCatherine Zhang 34132c7946a7SCatherine Zhang return 0; 34142c7946a7SCatherine Zhang } 34152c7946a7SCatherine Zhang 34162c7946a7SCatherine Zhang 34172c7946a7SCatherine Zhang 34187d877f3bSAl Viro static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority) 34191da177e4SLinus Torvalds { 34201da177e4SLinus Torvalds return sk_alloc_security(sk, family, priority); 34211da177e4SLinus Torvalds } 34221da177e4SLinus Torvalds 34231da177e4SLinus Torvalds static void selinux_sk_free_security(struct sock *sk) 34241da177e4SLinus Torvalds { 34251da177e4SLinus Torvalds sk_free_security(sk); 34261da177e4SLinus Torvalds } 34271da177e4SLinus Torvalds 3428d28d1e08STrent Jaeger static unsigned int selinux_sk_getsid_security(struct sock *sk, struct flowi *fl, u8 dir) 3429d28d1e08STrent Jaeger { 3430d28d1e08STrent Jaeger struct inode_security_struct *isec; 3431d28d1e08STrent Jaeger u32 sock_sid = SECINITSID_ANY_SOCKET; 3432d28d1e08STrent Jaeger 3433d28d1e08STrent Jaeger if (!sk) 3434d28d1e08STrent Jaeger return selinux_no_sk_sid(fl); 3435d28d1e08STrent Jaeger 3436d28d1e08STrent Jaeger read_lock_bh(&sk->sk_callback_lock); 3437d28d1e08STrent Jaeger isec = get_sock_isec(sk); 3438d28d1e08STrent Jaeger 3439d28d1e08STrent Jaeger if (isec) 3440d28d1e08STrent Jaeger sock_sid = isec->sid; 3441d28d1e08STrent Jaeger 3442d28d1e08STrent Jaeger read_unlock_bh(&sk->sk_callback_lock); 3443d28d1e08STrent Jaeger return sock_sid; 3444d28d1e08STrent Jaeger } 3445d28d1e08STrent Jaeger 34461da177e4SLinus Torvalds static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb) 34471da177e4SLinus Torvalds { 34481da177e4SLinus Torvalds int err = 0; 34491da177e4SLinus Torvalds u32 perm; 34501da177e4SLinus Torvalds struct nlmsghdr *nlh; 34511da177e4SLinus Torvalds struct socket *sock = sk->sk_socket; 34521da177e4SLinus Torvalds struct inode_security_struct *isec = SOCK_INODE(sock)->i_security; 34531da177e4SLinus Torvalds 34541da177e4SLinus Torvalds if (skb->len < NLMSG_SPACE(0)) { 34551da177e4SLinus Torvalds err = -EINVAL; 34561da177e4SLinus Torvalds goto out; 34571da177e4SLinus Torvalds } 34581da177e4SLinus Torvalds nlh = (struct nlmsghdr *)skb->data; 34591da177e4SLinus Torvalds 34601da177e4SLinus Torvalds err = selinux_nlmsg_lookup(isec->sclass, nlh->nlmsg_type, &perm); 34611da177e4SLinus Torvalds if (err) { 34621da177e4SLinus Torvalds if (err == -EINVAL) { 34639ad9ad38SDavid Woodhouse audit_log(current->audit_context, GFP_KERNEL, AUDIT_SELINUX_ERR, 34641da177e4SLinus Torvalds "SELinux: unrecognized netlink message" 34651da177e4SLinus Torvalds " type=%hu for sclass=%hu\n", 34661da177e4SLinus Torvalds nlh->nlmsg_type, isec->sclass); 34671da177e4SLinus Torvalds if (!selinux_enforcing) 34681da177e4SLinus Torvalds err = 0; 34691da177e4SLinus Torvalds } 34701da177e4SLinus Torvalds 34711da177e4SLinus Torvalds /* Ignore */ 34721da177e4SLinus Torvalds if (err == -ENOENT) 34731da177e4SLinus Torvalds err = 0; 34741da177e4SLinus Torvalds goto out; 34751da177e4SLinus Torvalds } 34761da177e4SLinus Torvalds 34771da177e4SLinus Torvalds err = socket_has_perm(current, sock, perm); 34781da177e4SLinus Torvalds out: 34791da177e4SLinus Torvalds return err; 34801da177e4SLinus Torvalds } 34811da177e4SLinus Torvalds 34821da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER 34831da177e4SLinus Torvalds 34844e5ab4cbSJames Morris static int selinux_ip_postroute_last_compat(struct sock *sk, struct net_device *dev, 34854e5ab4cbSJames Morris struct inode_security_struct *isec, 34864e5ab4cbSJames Morris struct avc_audit_data *ad, 34874e5ab4cbSJames Morris u16 family, char *addrp, int len) 34881da177e4SLinus Torvalds { 34894e5ab4cbSJames Morris int err; 34901da177e4SLinus Torvalds u32 netif_perm, node_perm, node_sid, if_sid, send_perm = 0; 34911da177e4SLinus Torvalds 34921da177e4SLinus Torvalds err = sel_netif_sids(dev, &if_sid, NULL); 34931da177e4SLinus Torvalds if (err) 34941da177e4SLinus Torvalds goto out; 34951da177e4SLinus Torvalds 34961da177e4SLinus Torvalds switch (isec->sclass) { 34971da177e4SLinus Torvalds case SECCLASS_UDP_SOCKET: 34981da177e4SLinus Torvalds netif_perm = NETIF__UDP_SEND; 34991da177e4SLinus Torvalds node_perm = NODE__UDP_SEND; 35001da177e4SLinus Torvalds send_perm = UDP_SOCKET__SEND_MSG; 35011da177e4SLinus Torvalds break; 35021da177e4SLinus Torvalds 35031da177e4SLinus Torvalds case SECCLASS_TCP_SOCKET: 35041da177e4SLinus Torvalds netif_perm = NETIF__TCP_SEND; 35051da177e4SLinus Torvalds node_perm = NODE__TCP_SEND; 35061da177e4SLinus Torvalds send_perm = TCP_SOCKET__SEND_MSG; 35071da177e4SLinus Torvalds break; 35081da177e4SLinus Torvalds 35091da177e4SLinus Torvalds default: 35101da177e4SLinus Torvalds netif_perm = NETIF__RAWIP_SEND; 35111da177e4SLinus Torvalds node_perm = NODE__RAWIP_SEND; 35121da177e4SLinus Torvalds break; 35131da177e4SLinus Torvalds } 35141da177e4SLinus Torvalds 35154e5ab4cbSJames Morris err = avc_has_perm(isec->sid, if_sid, SECCLASS_NETIF, netif_perm, ad); 35164e5ab4cbSJames Morris if (err) 35171da177e4SLinus Torvalds goto out; 35181da177e4SLinus Torvalds 35194e5ab4cbSJames Morris err = security_node_sid(family, addrp, len, &node_sid); 35204e5ab4cbSJames Morris if (err) 35211da177e4SLinus Torvalds goto out; 35221da177e4SLinus Torvalds 35234e5ab4cbSJames Morris err = avc_has_perm(isec->sid, node_sid, SECCLASS_NODE, node_perm, ad); 35244e5ab4cbSJames Morris if (err) 35251da177e4SLinus Torvalds goto out; 35261da177e4SLinus Torvalds 35271da177e4SLinus Torvalds if (send_perm) { 35281da177e4SLinus Torvalds u32 port_sid; 35291da177e4SLinus Torvalds 35301da177e4SLinus Torvalds err = security_port_sid(sk->sk_family, 35311da177e4SLinus Torvalds sk->sk_type, 35321da177e4SLinus Torvalds sk->sk_protocol, 35334e5ab4cbSJames Morris ntohs(ad->u.net.dport), 35344e5ab4cbSJames Morris &port_sid); 35354e5ab4cbSJames Morris if (err) 35361da177e4SLinus Torvalds goto out; 35371da177e4SLinus Torvalds 35381da177e4SLinus Torvalds err = avc_has_perm(isec->sid, port_sid, isec->sclass, 35394e5ab4cbSJames Morris send_perm, ad); 35404e5ab4cbSJames Morris } 35414e5ab4cbSJames Morris out: 35424e5ab4cbSJames Morris return err; 35431da177e4SLinus Torvalds } 35441da177e4SLinus Torvalds 35454e5ab4cbSJames Morris static unsigned int selinux_ip_postroute_last(unsigned int hooknum, 35464e5ab4cbSJames Morris struct sk_buff **pskb, 35474e5ab4cbSJames Morris const struct net_device *in, 35484e5ab4cbSJames Morris const struct net_device *out, 35494e5ab4cbSJames Morris int (*okfn)(struct sk_buff *), 35504e5ab4cbSJames Morris u16 family) 35514e5ab4cbSJames Morris { 35524e5ab4cbSJames Morris char *addrp; 35534e5ab4cbSJames Morris int len, err = 0; 35544e5ab4cbSJames Morris struct sock *sk; 35554e5ab4cbSJames Morris struct socket *sock; 35564e5ab4cbSJames Morris struct inode *inode; 35574e5ab4cbSJames Morris struct sk_buff *skb = *pskb; 35584e5ab4cbSJames Morris struct inode_security_struct *isec; 35594e5ab4cbSJames Morris struct avc_audit_data ad; 35604e5ab4cbSJames Morris struct net_device *dev = (struct net_device *)out; 35614e5ab4cbSJames Morris 35624e5ab4cbSJames Morris sk = skb->sk; 35634e5ab4cbSJames Morris if (!sk) 35644e5ab4cbSJames Morris goto out; 35654e5ab4cbSJames Morris 35664e5ab4cbSJames Morris sock = sk->sk_socket; 35674e5ab4cbSJames Morris if (!sock) 35684e5ab4cbSJames Morris goto out; 35694e5ab4cbSJames Morris 35704e5ab4cbSJames Morris inode = SOCK_INODE(sock); 35714e5ab4cbSJames Morris if (!inode) 35724e5ab4cbSJames Morris goto out; 35734e5ab4cbSJames Morris 35744e5ab4cbSJames Morris isec = inode->i_security; 35754e5ab4cbSJames Morris 35764e5ab4cbSJames Morris AVC_AUDIT_DATA_INIT(&ad, NET); 35774e5ab4cbSJames Morris ad.u.net.netif = dev->name; 35784e5ab4cbSJames Morris ad.u.net.family = family; 35794e5ab4cbSJames Morris 35804e5ab4cbSJames Morris err = selinux_parse_skb(skb, &ad, &addrp, &len, 0); 35814e5ab4cbSJames Morris if (err) 35824e5ab4cbSJames Morris goto out; 35834e5ab4cbSJames Morris 35844e5ab4cbSJames Morris if (selinux_compat_net) 35854e5ab4cbSJames Morris err = selinux_ip_postroute_last_compat(sk, dev, isec, &ad, 35864e5ab4cbSJames Morris family, addrp, len); 35874e5ab4cbSJames Morris else 35884e5ab4cbSJames Morris err = avc_has_perm(isec->sid, skb->secmark, SECCLASS_PACKET, 35894e5ab4cbSJames Morris PACKET__SEND, &ad); 35904e5ab4cbSJames Morris 35914e5ab4cbSJames Morris if (err) 3592d28d1e08STrent Jaeger goto out; 3593d28d1e08STrent Jaeger 3594d28d1e08STrent Jaeger err = selinux_xfrm_postroute_last(isec->sid, skb); 35951da177e4SLinus Torvalds out: 35964e5ab4cbSJames Morris return err ? NF_DROP : NF_ACCEPT; 35971da177e4SLinus Torvalds } 35981da177e4SLinus Torvalds 35991da177e4SLinus Torvalds static unsigned int selinux_ipv4_postroute_last(unsigned int hooknum, 36001da177e4SLinus Torvalds struct sk_buff **pskb, 36011da177e4SLinus Torvalds const struct net_device *in, 36021da177e4SLinus Torvalds const struct net_device *out, 36031da177e4SLinus Torvalds int (*okfn)(struct sk_buff *)) 36041da177e4SLinus Torvalds { 36051da177e4SLinus Torvalds return selinux_ip_postroute_last(hooknum, pskb, in, out, okfn, PF_INET); 36061da177e4SLinus Torvalds } 36071da177e4SLinus Torvalds 36081da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 36091da177e4SLinus Torvalds 36101da177e4SLinus Torvalds static unsigned int selinux_ipv6_postroute_last(unsigned int hooknum, 36111da177e4SLinus Torvalds struct sk_buff **pskb, 36121da177e4SLinus Torvalds const struct net_device *in, 36131da177e4SLinus Torvalds const struct net_device *out, 36141da177e4SLinus Torvalds int (*okfn)(struct sk_buff *)) 36151da177e4SLinus Torvalds { 36161da177e4SLinus Torvalds return selinux_ip_postroute_last(hooknum, pskb, in, out, okfn, PF_INET6); 36171da177e4SLinus Torvalds } 36181da177e4SLinus Torvalds 36191da177e4SLinus Torvalds #endif /* IPV6 */ 36201da177e4SLinus Torvalds 36211da177e4SLinus Torvalds #endif /* CONFIG_NETFILTER */ 36221da177e4SLinus Torvalds 36231da177e4SLinus Torvalds static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb) 36241da177e4SLinus Torvalds { 36251da177e4SLinus Torvalds struct task_security_struct *tsec; 36261da177e4SLinus Torvalds struct av_decision avd; 36271da177e4SLinus Torvalds int err; 36281da177e4SLinus Torvalds 36291da177e4SLinus Torvalds err = secondary_ops->netlink_send(sk, skb); 36301da177e4SLinus Torvalds if (err) 36311da177e4SLinus Torvalds return err; 36321da177e4SLinus Torvalds 36331da177e4SLinus Torvalds tsec = current->security; 36341da177e4SLinus Torvalds 36351da177e4SLinus Torvalds avd.allowed = 0; 36361da177e4SLinus Torvalds avc_has_perm_noaudit(tsec->sid, tsec->sid, 36371da177e4SLinus Torvalds SECCLASS_CAPABILITY, ~0, &avd); 36381da177e4SLinus Torvalds cap_mask(NETLINK_CB(skb).eff_cap, avd.allowed); 36391da177e4SLinus Torvalds 36401da177e4SLinus Torvalds if (policydb_loaded_version >= POLICYDB_VERSION_NLCLASS) 36411da177e4SLinus Torvalds err = selinux_nlmsg_perm(sk, skb); 36421da177e4SLinus Torvalds 36431da177e4SLinus Torvalds return err; 36441da177e4SLinus Torvalds } 36451da177e4SLinus Torvalds 36461da177e4SLinus Torvalds static int selinux_netlink_recv(struct sk_buff *skb) 36471da177e4SLinus Torvalds { 36481da177e4SLinus Torvalds if (!cap_raised(NETLINK_CB(skb).eff_cap, CAP_NET_ADMIN)) 36491da177e4SLinus Torvalds return -EPERM; 36501da177e4SLinus Torvalds return 0; 36511da177e4SLinus Torvalds } 36521da177e4SLinus Torvalds 36531da177e4SLinus Torvalds static int ipc_alloc_security(struct task_struct *task, 36541da177e4SLinus Torvalds struct kern_ipc_perm *perm, 36551da177e4SLinus Torvalds u16 sclass) 36561da177e4SLinus Torvalds { 36571da177e4SLinus Torvalds struct task_security_struct *tsec = task->security; 36581da177e4SLinus Torvalds struct ipc_security_struct *isec; 36591da177e4SLinus Torvalds 366089d155efSJames Morris isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL); 36611da177e4SLinus Torvalds if (!isec) 36621da177e4SLinus Torvalds return -ENOMEM; 36631da177e4SLinus Torvalds 36641da177e4SLinus Torvalds isec->sclass = sclass; 36651da177e4SLinus Torvalds isec->ipc_perm = perm; 36661da177e4SLinus Torvalds isec->sid = tsec->sid; 36671da177e4SLinus Torvalds perm->security = isec; 36681da177e4SLinus Torvalds 36691da177e4SLinus Torvalds return 0; 36701da177e4SLinus Torvalds } 36711da177e4SLinus Torvalds 36721da177e4SLinus Torvalds static void ipc_free_security(struct kern_ipc_perm *perm) 36731da177e4SLinus Torvalds { 36741da177e4SLinus Torvalds struct ipc_security_struct *isec = perm->security; 36751da177e4SLinus Torvalds perm->security = NULL; 36761da177e4SLinus Torvalds kfree(isec); 36771da177e4SLinus Torvalds } 36781da177e4SLinus Torvalds 36791da177e4SLinus Torvalds static int msg_msg_alloc_security(struct msg_msg *msg) 36801da177e4SLinus Torvalds { 36811da177e4SLinus Torvalds struct msg_security_struct *msec; 36821da177e4SLinus Torvalds 368389d155efSJames Morris msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL); 36841da177e4SLinus Torvalds if (!msec) 36851da177e4SLinus Torvalds return -ENOMEM; 36861da177e4SLinus Torvalds 36871da177e4SLinus Torvalds msec->msg = msg; 36881da177e4SLinus Torvalds msec->sid = SECINITSID_UNLABELED; 36891da177e4SLinus Torvalds msg->security = msec; 36901da177e4SLinus Torvalds 36911da177e4SLinus Torvalds return 0; 36921da177e4SLinus Torvalds } 36931da177e4SLinus Torvalds 36941da177e4SLinus Torvalds static void msg_msg_free_security(struct msg_msg *msg) 36951da177e4SLinus Torvalds { 36961da177e4SLinus Torvalds struct msg_security_struct *msec = msg->security; 36971da177e4SLinus Torvalds 36981da177e4SLinus Torvalds msg->security = NULL; 36991da177e4SLinus Torvalds kfree(msec); 37001da177e4SLinus Torvalds } 37011da177e4SLinus Torvalds 37021da177e4SLinus Torvalds static int ipc_has_perm(struct kern_ipc_perm *ipc_perms, 37036af963f1SStephen Smalley u32 perms) 37041da177e4SLinus Torvalds { 37051da177e4SLinus Torvalds struct task_security_struct *tsec; 37061da177e4SLinus Torvalds struct ipc_security_struct *isec; 37071da177e4SLinus Torvalds struct avc_audit_data ad; 37081da177e4SLinus Torvalds 37091da177e4SLinus Torvalds tsec = current->security; 37101da177e4SLinus Torvalds isec = ipc_perms->security; 37111da177e4SLinus Torvalds 37121da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 37131da177e4SLinus Torvalds ad.u.ipc_id = ipc_perms->key; 37141da177e4SLinus Torvalds 37156af963f1SStephen Smalley return avc_has_perm(tsec->sid, isec->sid, isec->sclass, perms, &ad); 37161da177e4SLinus Torvalds } 37171da177e4SLinus Torvalds 37181da177e4SLinus Torvalds static int selinux_msg_msg_alloc_security(struct msg_msg *msg) 37191da177e4SLinus Torvalds { 37201da177e4SLinus Torvalds return msg_msg_alloc_security(msg); 37211da177e4SLinus Torvalds } 37221da177e4SLinus Torvalds 37231da177e4SLinus Torvalds static void selinux_msg_msg_free_security(struct msg_msg *msg) 37241da177e4SLinus Torvalds { 37251da177e4SLinus Torvalds msg_msg_free_security(msg); 37261da177e4SLinus Torvalds } 37271da177e4SLinus Torvalds 37281da177e4SLinus Torvalds /* message queue security operations */ 37291da177e4SLinus Torvalds static int selinux_msg_queue_alloc_security(struct msg_queue *msq) 37301da177e4SLinus Torvalds { 37311da177e4SLinus Torvalds struct task_security_struct *tsec; 37321da177e4SLinus Torvalds struct ipc_security_struct *isec; 37331da177e4SLinus Torvalds struct avc_audit_data ad; 37341da177e4SLinus Torvalds int rc; 37351da177e4SLinus Torvalds 37361da177e4SLinus Torvalds rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ); 37371da177e4SLinus Torvalds if (rc) 37381da177e4SLinus Torvalds return rc; 37391da177e4SLinus Torvalds 37401da177e4SLinus Torvalds tsec = current->security; 37411da177e4SLinus Torvalds isec = msq->q_perm.security; 37421da177e4SLinus Torvalds 37431da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 37441da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 37451da177e4SLinus Torvalds 37461da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_MSGQ, 37471da177e4SLinus Torvalds MSGQ__CREATE, &ad); 37481da177e4SLinus Torvalds if (rc) { 37491da177e4SLinus Torvalds ipc_free_security(&msq->q_perm); 37501da177e4SLinus Torvalds return rc; 37511da177e4SLinus Torvalds } 37521da177e4SLinus Torvalds return 0; 37531da177e4SLinus Torvalds } 37541da177e4SLinus Torvalds 37551da177e4SLinus Torvalds static void selinux_msg_queue_free_security(struct msg_queue *msq) 37561da177e4SLinus Torvalds { 37571da177e4SLinus Torvalds ipc_free_security(&msq->q_perm); 37581da177e4SLinus Torvalds } 37591da177e4SLinus Torvalds 37601da177e4SLinus Torvalds static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg) 37611da177e4SLinus Torvalds { 37621da177e4SLinus Torvalds struct task_security_struct *tsec; 37631da177e4SLinus Torvalds struct ipc_security_struct *isec; 37641da177e4SLinus Torvalds struct avc_audit_data ad; 37651da177e4SLinus Torvalds 37661da177e4SLinus Torvalds tsec = current->security; 37671da177e4SLinus Torvalds isec = msq->q_perm.security; 37681da177e4SLinus Torvalds 37691da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 37701da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 37711da177e4SLinus Torvalds 37721da177e4SLinus Torvalds return avc_has_perm(tsec->sid, isec->sid, SECCLASS_MSGQ, 37731da177e4SLinus Torvalds MSGQ__ASSOCIATE, &ad); 37741da177e4SLinus Torvalds } 37751da177e4SLinus Torvalds 37761da177e4SLinus Torvalds static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd) 37771da177e4SLinus Torvalds { 37781da177e4SLinus Torvalds int err; 37791da177e4SLinus Torvalds int perms; 37801da177e4SLinus Torvalds 37811da177e4SLinus Torvalds switch(cmd) { 37821da177e4SLinus Torvalds case IPC_INFO: 37831da177e4SLinus Torvalds case MSG_INFO: 37841da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 37851da177e4SLinus Torvalds return task_has_system(current, SYSTEM__IPC_INFO); 37861da177e4SLinus Torvalds case IPC_STAT: 37871da177e4SLinus Torvalds case MSG_STAT: 37881da177e4SLinus Torvalds perms = MSGQ__GETATTR | MSGQ__ASSOCIATE; 37891da177e4SLinus Torvalds break; 37901da177e4SLinus Torvalds case IPC_SET: 37911da177e4SLinus Torvalds perms = MSGQ__SETATTR; 37921da177e4SLinus Torvalds break; 37931da177e4SLinus Torvalds case IPC_RMID: 37941da177e4SLinus Torvalds perms = MSGQ__DESTROY; 37951da177e4SLinus Torvalds break; 37961da177e4SLinus Torvalds default: 37971da177e4SLinus Torvalds return 0; 37981da177e4SLinus Torvalds } 37991da177e4SLinus Torvalds 38006af963f1SStephen Smalley err = ipc_has_perm(&msq->q_perm, perms); 38011da177e4SLinus Torvalds return err; 38021da177e4SLinus Torvalds } 38031da177e4SLinus Torvalds 38041da177e4SLinus Torvalds static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg) 38051da177e4SLinus Torvalds { 38061da177e4SLinus Torvalds struct task_security_struct *tsec; 38071da177e4SLinus Torvalds struct ipc_security_struct *isec; 38081da177e4SLinus Torvalds struct msg_security_struct *msec; 38091da177e4SLinus Torvalds struct avc_audit_data ad; 38101da177e4SLinus Torvalds int rc; 38111da177e4SLinus Torvalds 38121da177e4SLinus Torvalds tsec = current->security; 38131da177e4SLinus Torvalds isec = msq->q_perm.security; 38141da177e4SLinus Torvalds msec = msg->security; 38151da177e4SLinus Torvalds 38161da177e4SLinus Torvalds /* 38171da177e4SLinus Torvalds * First time through, need to assign label to the message 38181da177e4SLinus Torvalds */ 38191da177e4SLinus Torvalds if (msec->sid == SECINITSID_UNLABELED) { 38201da177e4SLinus Torvalds /* 38211da177e4SLinus Torvalds * Compute new sid based on current process and 38221da177e4SLinus Torvalds * message queue this message will be stored in 38231da177e4SLinus Torvalds */ 38241da177e4SLinus Torvalds rc = security_transition_sid(tsec->sid, 38251da177e4SLinus Torvalds isec->sid, 38261da177e4SLinus Torvalds SECCLASS_MSG, 38271da177e4SLinus Torvalds &msec->sid); 38281da177e4SLinus Torvalds if (rc) 38291da177e4SLinus Torvalds return rc; 38301da177e4SLinus Torvalds } 38311da177e4SLinus Torvalds 38321da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 38331da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 38341da177e4SLinus Torvalds 38351da177e4SLinus Torvalds /* Can this process write to the queue? */ 38361da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_MSGQ, 38371da177e4SLinus Torvalds MSGQ__WRITE, &ad); 38381da177e4SLinus Torvalds if (!rc) 38391da177e4SLinus Torvalds /* Can this process send the message */ 38401da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, msec->sid, 38411da177e4SLinus Torvalds SECCLASS_MSG, MSG__SEND, &ad); 38421da177e4SLinus Torvalds if (!rc) 38431da177e4SLinus Torvalds /* Can the message be put in the queue? */ 38441da177e4SLinus Torvalds rc = avc_has_perm(msec->sid, isec->sid, 38451da177e4SLinus Torvalds SECCLASS_MSGQ, MSGQ__ENQUEUE, &ad); 38461da177e4SLinus Torvalds 38471da177e4SLinus Torvalds return rc; 38481da177e4SLinus Torvalds } 38491da177e4SLinus Torvalds 38501da177e4SLinus Torvalds static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg, 38511da177e4SLinus Torvalds struct task_struct *target, 38521da177e4SLinus Torvalds long type, int mode) 38531da177e4SLinus Torvalds { 38541da177e4SLinus Torvalds struct task_security_struct *tsec; 38551da177e4SLinus Torvalds struct ipc_security_struct *isec; 38561da177e4SLinus Torvalds struct msg_security_struct *msec; 38571da177e4SLinus Torvalds struct avc_audit_data ad; 38581da177e4SLinus Torvalds int rc; 38591da177e4SLinus Torvalds 38601da177e4SLinus Torvalds tsec = target->security; 38611da177e4SLinus Torvalds isec = msq->q_perm.security; 38621da177e4SLinus Torvalds msec = msg->security; 38631da177e4SLinus Torvalds 38641da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 38651da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 38661da177e4SLinus Torvalds 38671da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, 38681da177e4SLinus Torvalds SECCLASS_MSGQ, MSGQ__READ, &ad); 38691da177e4SLinus Torvalds if (!rc) 38701da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, msec->sid, 38711da177e4SLinus Torvalds SECCLASS_MSG, MSG__RECEIVE, &ad); 38721da177e4SLinus Torvalds return rc; 38731da177e4SLinus Torvalds } 38741da177e4SLinus Torvalds 38751da177e4SLinus Torvalds /* Shared Memory security operations */ 38761da177e4SLinus Torvalds static int selinux_shm_alloc_security(struct shmid_kernel *shp) 38771da177e4SLinus Torvalds { 38781da177e4SLinus Torvalds struct task_security_struct *tsec; 38791da177e4SLinus Torvalds struct ipc_security_struct *isec; 38801da177e4SLinus Torvalds struct avc_audit_data ad; 38811da177e4SLinus Torvalds int rc; 38821da177e4SLinus Torvalds 38831da177e4SLinus Torvalds rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM); 38841da177e4SLinus Torvalds if (rc) 38851da177e4SLinus Torvalds return rc; 38861da177e4SLinus Torvalds 38871da177e4SLinus Torvalds tsec = current->security; 38881da177e4SLinus Torvalds isec = shp->shm_perm.security; 38891da177e4SLinus Torvalds 38901da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 38911da177e4SLinus Torvalds ad.u.ipc_id = shp->shm_perm.key; 38921da177e4SLinus Torvalds 38931da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_SHM, 38941da177e4SLinus Torvalds SHM__CREATE, &ad); 38951da177e4SLinus Torvalds if (rc) { 38961da177e4SLinus Torvalds ipc_free_security(&shp->shm_perm); 38971da177e4SLinus Torvalds return rc; 38981da177e4SLinus Torvalds } 38991da177e4SLinus Torvalds return 0; 39001da177e4SLinus Torvalds } 39011da177e4SLinus Torvalds 39021da177e4SLinus Torvalds static void selinux_shm_free_security(struct shmid_kernel *shp) 39031da177e4SLinus Torvalds { 39041da177e4SLinus Torvalds ipc_free_security(&shp->shm_perm); 39051da177e4SLinus Torvalds } 39061da177e4SLinus Torvalds 39071da177e4SLinus Torvalds static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg) 39081da177e4SLinus Torvalds { 39091da177e4SLinus Torvalds struct task_security_struct *tsec; 39101da177e4SLinus Torvalds struct ipc_security_struct *isec; 39111da177e4SLinus Torvalds struct avc_audit_data ad; 39121da177e4SLinus Torvalds 39131da177e4SLinus Torvalds tsec = current->security; 39141da177e4SLinus Torvalds isec = shp->shm_perm.security; 39151da177e4SLinus Torvalds 39161da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 39171da177e4SLinus Torvalds ad.u.ipc_id = shp->shm_perm.key; 39181da177e4SLinus Torvalds 39191da177e4SLinus Torvalds return avc_has_perm(tsec->sid, isec->sid, SECCLASS_SHM, 39201da177e4SLinus Torvalds SHM__ASSOCIATE, &ad); 39211da177e4SLinus Torvalds } 39221da177e4SLinus Torvalds 39231da177e4SLinus Torvalds /* Note, at this point, shp is locked down */ 39241da177e4SLinus Torvalds static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd) 39251da177e4SLinus Torvalds { 39261da177e4SLinus Torvalds int perms; 39271da177e4SLinus Torvalds int err; 39281da177e4SLinus Torvalds 39291da177e4SLinus Torvalds switch(cmd) { 39301da177e4SLinus Torvalds case IPC_INFO: 39311da177e4SLinus Torvalds case SHM_INFO: 39321da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 39331da177e4SLinus Torvalds return task_has_system(current, SYSTEM__IPC_INFO); 39341da177e4SLinus Torvalds case IPC_STAT: 39351da177e4SLinus Torvalds case SHM_STAT: 39361da177e4SLinus Torvalds perms = SHM__GETATTR | SHM__ASSOCIATE; 39371da177e4SLinus Torvalds break; 39381da177e4SLinus Torvalds case IPC_SET: 39391da177e4SLinus Torvalds perms = SHM__SETATTR; 39401da177e4SLinus Torvalds break; 39411da177e4SLinus Torvalds case SHM_LOCK: 39421da177e4SLinus Torvalds case SHM_UNLOCK: 39431da177e4SLinus Torvalds perms = SHM__LOCK; 39441da177e4SLinus Torvalds break; 39451da177e4SLinus Torvalds case IPC_RMID: 39461da177e4SLinus Torvalds perms = SHM__DESTROY; 39471da177e4SLinus Torvalds break; 39481da177e4SLinus Torvalds default: 39491da177e4SLinus Torvalds return 0; 39501da177e4SLinus Torvalds } 39511da177e4SLinus Torvalds 39526af963f1SStephen Smalley err = ipc_has_perm(&shp->shm_perm, perms); 39531da177e4SLinus Torvalds return err; 39541da177e4SLinus Torvalds } 39551da177e4SLinus Torvalds 39561da177e4SLinus Torvalds static int selinux_shm_shmat(struct shmid_kernel *shp, 39571da177e4SLinus Torvalds char __user *shmaddr, int shmflg) 39581da177e4SLinus Torvalds { 39591da177e4SLinus Torvalds u32 perms; 39601da177e4SLinus Torvalds int rc; 39611da177e4SLinus Torvalds 39621da177e4SLinus Torvalds rc = secondary_ops->shm_shmat(shp, shmaddr, shmflg); 39631da177e4SLinus Torvalds if (rc) 39641da177e4SLinus Torvalds return rc; 39651da177e4SLinus Torvalds 39661da177e4SLinus Torvalds if (shmflg & SHM_RDONLY) 39671da177e4SLinus Torvalds perms = SHM__READ; 39681da177e4SLinus Torvalds else 39691da177e4SLinus Torvalds perms = SHM__READ | SHM__WRITE; 39701da177e4SLinus Torvalds 39716af963f1SStephen Smalley return ipc_has_perm(&shp->shm_perm, perms); 39721da177e4SLinus Torvalds } 39731da177e4SLinus Torvalds 39741da177e4SLinus Torvalds /* Semaphore security operations */ 39751da177e4SLinus Torvalds static int selinux_sem_alloc_security(struct sem_array *sma) 39761da177e4SLinus Torvalds { 39771da177e4SLinus Torvalds struct task_security_struct *tsec; 39781da177e4SLinus Torvalds struct ipc_security_struct *isec; 39791da177e4SLinus Torvalds struct avc_audit_data ad; 39801da177e4SLinus Torvalds int rc; 39811da177e4SLinus Torvalds 39821da177e4SLinus Torvalds rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM); 39831da177e4SLinus Torvalds if (rc) 39841da177e4SLinus Torvalds return rc; 39851da177e4SLinus Torvalds 39861da177e4SLinus Torvalds tsec = current->security; 39871da177e4SLinus Torvalds isec = sma->sem_perm.security; 39881da177e4SLinus Torvalds 39891da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 39901da177e4SLinus Torvalds ad.u.ipc_id = sma->sem_perm.key; 39911da177e4SLinus Torvalds 39921da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_SEM, 39931da177e4SLinus Torvalds SEM__CREATE, &ad); 39941da177e4SLinus Torvalds if (rc) { 39951da177e4SLinus Torvalds ipc_free_security(&sma->sem_perm); 39961da177e4SLinus Torvalds return rc; 39971da177e4SLinus Torvalds } 39981da177e4SLinus Torvalds return 0; 39991da177e4SLinus Torvalds } 40001da177e4SLinus Torvalds 40011da177e4SLinus Torvalds static void selinux_sem_free_security(struct sem_array *sma) 40021da177e4SLinus Torvalds { 40031da177e4SLinus Torvalds ipc_free_security(&sma->sem_perm); 40041da177e4SLinus Torvalds } 40051da177e4SLinus Torvalds 40061da177e4SLinus Torvalds static int selinux_sem_associate(struct sem_array *sma, int semflg) 40071da177e4SLinus Torvalds { 40081da177e4SLinus Torvalds struct task_security_struct *tsec; 40091da177e4SLinus Torvalds struct ipc_security_struct *isec; 40101da177e4SLinus Torvalds struct avc_audit_data ad; 40111da177e4SLinus Torvalds 40121da177e4SLinus Torvalds tsec = current->security; 40131da177e4SLinus Torvalds isec = sma->sem_perm.security; 40141da177e4SLinus Torvalds 40151da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 40161da177e4SLinus Torvalds ad.u.ipc_id = sma->sem_perm.key; 40171da177e4SLinus Torvalds 40181da177e4SLinus Torvalds return avc_has_perm(tsec->sid, isec->sid, SECCLASS_SEM, 40191da177e4SLinus Torvalds SEM__ASSOCIATE, &ad); 40201da177e4SLinus Torvalds } 40211da177e4SLinus Torvalds 40221da177e4SLinus Torvalds /* Note, at this point, sma is locked down */ 40231da177e4SLinus Torvalds static int selinux_sem_semctl(struct sem_array *sma, int cmd) 40241da177e4SLinus Torvalds { 40251da177e4SLinus Torvalds int err; 40261da177e4SLinus Torvalds u32 perms; 40271da177e4SLinus Torvalds 40281da177e4SLinus Torvalds switch(cmd) { 40291da177e4SLinus Torvalds case IPC_INFO: 40301da177e4SLinus Torvalds case SEM_INFO: 40311da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 40321da177e4SLinus Torvalds return task_has_system(current, SYSTEM__IPC_INFO); 40331da177e4SLinus Torvalds case GETPID: 40341da177e4SLinus Torvalds case GETNCNT: 40351da177e4SLinus Torvalds case GETZCNT: 40361da177e4SLinus Torvalds perms = SEM__GETATTR; 40371da177e4SLinus Torvalds break; 40381da177e4SLinus Torvalds case GETVAL: 40391da177e4SLinus Torvalds case GETALL: 40401da177e4SLinus Torvalds perms = SEM__READ; 40411da177e4SLinus Torvalds break; 40421da177e4SLinus Torvalds case SETVAL: 40431da177e4SLinus Torvalds case SETALL: 40441da177e4SLinus Torvalds perms = SEM__WRITE; 40451da177e4SLinus Torvalds break; 40461da177e4SLinus Torvalds case IPC_RMID: 40471da177e4SLinus Torvalds perms = SEM__DESTROY; 40481da177e4SLinus Torvalds break; 40491da177e4SLinus Torvalds case IPC_SET: 40501da177e4SLinus Torvalds perms = SEM__SETATTR; 40511da177e4SLinus Torvalds break; 40521da177e4SLinus Torvalds case IPC_STAT: 40531da177e4SLinus Torvalds case SEM_STAT: 40541da177e4SLinus Torvalds perms = SEM__GETATTR | SEM__ASSOCIATE; 40551da177e4SLinus Torvalds break; 40561da177e4SLinus Torvalds default: 40571da177e4SLinus Torvalds return 0; 40581da177e4SLinus Torvalds } 40591da177e4SLinus Torvalds 40606af963f1SStephen Smalley err = ipc_has_perm(&sma->sem_perm, perms); 40611da177e4SLinus Torvalds return err; 40621da177e4SLinus Torvalds } 40631da177e4SLinus Torvalds 40641da177e4SLinus Torvalds static int selinux_sem_semop(struct sem_array *sma, 40651da177e4SLinus Torvalds struct sembuf *sops, unsigned nsops, int alter) 40661da177e4SLinus Torvalds { 40671da177e4SLinus Torvalds u32 perms; 40681da177e4SLinus Torvalds 40691da177e4SLinus Torvalds if (alter) 40701da177e4SLinus Torvalds perms = SEM__READ | SEM__WRITE; 40711da177e4SLinus Torvalds else 40721da177e4SLinus Torvalds perms = SEM__READ; 40731da177e4SLinus Torvalds 40746af963f1SStephen Smalley return ipc_has_perm(&sma->sem_perm, perms); 40751da177e4SLinus Torvalds } 40761da177e4SLinus Torvalds 40771da177e4SLinus Torvalds static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag) 40781da177e4SLinus Torvalds { 40791da177e4SLinus Torvalds u32 av = 0; 40801da177e4SLinus Torvalds 40811da177e4SLinus Torvalds av = 0; 40821da177e4SLinus Torvalds if (flag & S_IRUGO) 40831da177e4SLinus Torvalds av |= IPC__UNIX_READ; 40841da177e4SLinus Torvalds if (flag & S_IWUGO) 40851da177e4SLinus Torvalds av |= IPC__UNIX_WRITE; 40861da177e4SLinus Torvalds 40871da177e4SLinus Torvalds if (av == 0) 40881da177e4SLinus Torvalds return 0; 40891da177e4SLinus Torvalds 40906af963f1SStephen Smalley return ipc_has_perm(ipcp, av); 40911da177e4SLinus Torvalds } 40921da177e4SLinus Torvalds 40931da177e4SLinus Torvalds /* module stacking operations */ 40941da177e4SLinus Torvalds static int selinux_register_security (const char *name, struct security_operations *ops) 40951da177e4SLinus Torvalds { 40961da177e4SLinus Torvalds if (secondary_ops != original_ops) { 40971da177e4SLinus Torvalds printk(KERN_INFO "%s: There is already a secondary security " 40981da177e4SLinus Torvalds "module registered.\n", __FUNCTION__); 40991da177e4SLinus Torvalds return -EINVAL; 41001da177e4SLinus Torvalds } 41011da177e4SLinus Torvalds 41021da177e4SLinus Torvalds secondary_ops = ops; 41031da177e4SLinus Torvalds 41041da177e4SLinus Torvalds printk(KERN_INFO "%s: Registering secondary module %s\n", 41051da177e4SLinus Torvalds __FUNCTION__, 41061da177e4SLinus Torvalds name); 41071da177e4SLinus Torvalds 41081da177e4SLinus Torvalds return 0; 41091da177e4SLinus Torvalds } 41101da177e4SLinus Torvalds 41111da177e4SLinus Torvalds static int selinux_unregister_security (const char *name, struct security_operations *ops) 41121da177e4SLinus Torvalds { 41131da177e4SLinus Torvalds if (ops != secondary_ops) { 41141da177e4SLinus Torvalds printk (KERN_INFO "%s: trying to unregister a security module " 41151da177e4SLinus Torvalds "that is not registered.\n", __FUNCTION__); 41161da177e4SLinus Torvalds return -EINVAL; 41171da177e4SLinus Torvalds } 41181da177e4SLinus Torvalds 41191da177e4SLinus Torvalds secondary_ops = original_ops; 41201da177e4SLinus Torvalds 41211da177e4SLinus Torvalds return 0; 41221da177e4SLinus Torvalds } 41231da177e4SLinus Torvalds 41241da177e4SLinus Torvalds static void selinux_d_instantiate (struct dentry *dentry, struct inode *inode) 41251da177e4SLinus Torvalds { 41261da177e4SLinus Torvalds if (inode) 41271da177e4SLinus Torvalds inode_doinit_with_dentry(inode, dentry); 41281da177e4SLinus Torvalds } 41291da177e4SLinus Torvalds 41301da177e4SLinus Torvalds static int selinux_getprocattr(struct task_struct *p, 41311da177e4SLinus Torvalds char *name, void *value, size_t size) 41321da177e4SLinus Torvalds { 41331da177e4SLinus Torvalds struct task_security_struct *tsec; 41348c8570fbSDustin Kirkland u32 sid; 41351da177e4SLinus Torvalds int error; 41361da177e4SLinus Torvalds 41371da177e4SLinus Torvalds if (current != p) { 41381da177e4SLinus Torvalds error = task_has_perm(current, p, PROCESS__GETATTR); 41391da177e4SLinus Torvalds if (error) 41401da177e4SLinus Torvalds return error; 41411da177e4SLinus Torvalds } 41421da177e4SLinus Torvalds 41431da177e4SLinus Torvalds tsec = p->security; 41441da177e4SLinus Torvalds 41451da177e4SLinus Torvalds if (!strcmp(name, "current")) 41461da177e4SLinus Torvalds sid = tsec->sid; 41471da177e4SLinus Torvalds else if (!strcmp(name, "prev")) 41481da177e4SLinus Torvalds sid = tsec->osid; 41491da177e4SLinus Torvalds else if (!strcmp(name, "exec")) 41501da177e4SLinus Torvalds sid = tsec->exec_sid; 41511da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 41521da177e4SLinus Torvalds sid = tsec->create_sid; 41531da177e4SLinus Torvalds else 41541da177e4SLinus Torvalds return -EINVAL; 41551da177e4SLinus Torvalds 41561da177e4SLinus Torvalds if (!sid) 41571da177e4SLinus Torvalds return 0; 41581da177e4SLinus Torvalds 41598c8570fbSDustin Kirkland return selinux_getsecurity(sid, value, size); 41601da177e4SLinus Torvalds } 41611da177e4SLinus Torvalds 41621da177e4SLinus Torvalds static int selinux_setprocattr(struct task_struct *p, 41631da177e4SLinus Torvalds char *name, void *value, size_t size) 41641da177e4SLinus Torvalds { 41651da177e4SLinus Torvalds struct task_security_struct *tsec; 41661da177e4SLinus Torvalds u32 sid = 0; 41671da177e4SLinus Torvalds int error; 41681da177e4SLinus Torvalds char *str = value; 41691da177e4SLinus Torvalds 41701da177e4SLinus Torvalds if (current != p) { 41711da177e4SLinus Torvalds /* SELinux only allows a process to change its own 41721da177e4SLinus Torvalds security attributes. */ 41731da177e4SLinus Torvalds return -EACCES; 41741da177e4SLinus Torvalds } 41751da177e4SLinus Torvalds 41761da177e4SLinus Torvalds /* 41771da177e4SLinus Torvalds * Basic control over ability to set these attributes at all. 41781da177e4SLinus Torvalds * current == p, but we'll pass them separately in case the 41791da177e4SLinus Torvalds * above restriction is ever removed. 41801da177e4SLinus Torvalds */ 41811da177e4SLinus Torvalds if (!strcmp(name, "exec")) 41821da177e4SLinus Torvalds error = task_has_perm(current, p, PROCESS__SETEXEC); 41831da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 41841da177e4SLinus Torvalds error = task_has_perm(current, p, PROCESS__SETFSCREATE); 41851da177e4SLinus Torvalds else if (!strcmp(name, "current")) 41861da177e4SLinus Torvalds error = task_has_perm(current, p, PROCESS__SETCURRENT); 41871da177e4SLinus Torvalds else 41881da177e4SLinus Torvalds error = -EINVAL; 41891da177e4SLinus Torvalds if (error) 41901da177e4SLinus Torvalds return error; 41911da177e4SLinus Torvalds 41921da177e4SLinus Torvalds /* Obtain a SID for the context, if one was specified. */ 41931da177e4SLinus Torvalds if (size && str[1] && str[1] != '\n') { 41941da177e4SLinus Torvalds if (str[size-1] == '\n') { 41951da177e4SLinus Torvalds str[size-1] = 0; 41961da177e4SLinus Torvalds size--; 41971da177e4SLinus Torvalds } 41981da177e4SLinus Torvalds error = security_context_to_sid(value, size, &sid); 41991da177e4SLinus Torvalds if (error) 42001da177e4SLinus Torvalds return error; 42011da177e4SLinus Torvalds } 42021da177e4SLinus Torvalds 42031da177e4SLinus Torvalds /* Permission checking based on the specified context is 42041da177e4SLinus Torvalds performed during the actual operation (execve, 42051da177e4SLinus Torvalds open/mkdir/...), when we know the full context of the 42061da177e4SLinus Torvalds operation. See selinux_bprm_set_security for the execve 42071da177e4SLinus Torvalds checks and may_create for the file creation checks. The 42081da177e4SLinus Torvalds operation will then fail if the context is not permitted. */ 42091da177e4SLinus Torvalds tsec = p->security; 42101da177e4SLinus Torvalds if (!strcmp(name, "exec")) 42111da177e4SLinus Torvalds tsec->exec_sid = sid; 42121da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 42131da177e4SLinus Torvalds tsec->create_sid = sid; 42141da177e4SLinus Torvalds else if (!strcmp(name, "current")) { 42151da177e4SLinus Torvalds struct av_decision avd; 42161da177e4SLinus Torvalds 42171da177e4SLinus Torvalds if (sid == 0) 42181da177e4SLinus Torvalds return -EINVAL; 42191da177e4SLinus Torvalds 42201da177e4SLinus Torvalds /* Only allow single threaded processes to change context */ 42211da177e4SLinus Torvalds if (atomic_read(&p->mm->mm_users) != 1) { 42221da177e4SLinus Torvalds struct task_struct *g, *t; 42231da177e4SLinus Torvalds struct mm_struct *mm = p->mm; 42241da177e4SLinus Torvalds read_lock(&tasklist_lock); 42251da177e4SLinus Torvalds do_each_thread(g, t) 42261da177e4SLinus Torvalds if (t->mm == mm && t != p) { 42271da177e4SLinus Torvalds read_unlock(&tasklist_lock); 42281da177e4SLinus Torvalds return -EPERM; 42291da177e4SLinus Torvalds } 42301da177e4SLinus Torvalds while_each_thread(g, t); 42311da177e4SLinus Torvalds read_unlock(&tasklist_lock); 42321da177e4SLinus Torvalds } 42331da177e4SLinus Torvalds 42341da177e4SLinus Torvalds /* Check permissions for the transition. */ 42351da177e4SLinus Torvalds error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS, 42361da177e4SLinus Torvalds PROCESS__DYNTRANSITION, NULL); 42371da177e4SLinus Torvalds if (error) 42381da177e4SLinus Torvalds return error; 42391da177e4SLinus Torvalds 42401da177e4SLinus Torvalds /* Check for ptracing, and update the task SID if ok. 42411da177e4SLinus Torvalds Otherwise, leave SID unchanged and fail. */ 42421da177e4SLinus Torvalds task_lock(p); 42431da177e4SLinus Torvalds if (p->ptrace & PT_PTRACED) { 42441da177e4SLinus Torvalds error = avc_has_perm_noaudit(tsec->ptrace_sid, sid, 42451da177e4SLinus Torvalds SECCLASS_PROCESS, 42461da177e4SLinus Torvalds PROCESS__PTRACE, &avd); 42471da177e4SLinus Torvalds if (!error) 42481da177e4SLinus Torvalds tsec->sid = sid; 42491da177e4SLinus Torvalds task_unlock(p); 42501da177e4SLinus Torvalds avc_audit(tsec->ptrace_sid, sid, SECCLASS_PROCESS, 42511da177e4SLinus Torvalds PROCESS__PTRACE, &avd, error, NULL); 42521da177e4SLinus Torvalds if (error) 42531da177e4SLinus Torvalds return error; 42541da177e4SLinus Torvalds } else { 42551da177e4SLinus Torvalds tsec->sid = sid; 42561da177e4SLinus Torvalds task_unlock(p); 42571da177e4SLinus Torvalds } 42581da177e4SLinus Torvalds } 42591da177e4SLinus Torvalds else 42601da177e4SLinus Torvalds return -EINVAL; 42611da177e4SLinus Torvalds 42621da177e4SLinus Torvalds return size; 42631da177e4SLinus Torvalds } 42641da177e4SLinus Torvalds 4265d720024eSMichael LeMay #ifdef CONFIG_KEYS 4266d720024eSMichael LeMay 4267d720024eSMichael LeMay static int selinux_key_alloc(struct key *k, struct task_struct *tsk) 4268d720024eSMichael LeMay { 4269d720024eSMichael LeMay struct task_security_struct *tsec = tsk->security; 4270d720024eSMichael LeMay struct key_security_struct *ksec; 4271d720024eSMichael LeMay 4272d720024eSMichael LeMay ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL); 4273d720024eSMichael LeMay if (!ksec) 4274d720024eSMichael LeMay return -ENOMEM; 4275d720024eSMichael LeMay 4276d720024eSMichael LeMay ksec->obj = k; 4277d720024eSMichael LeMay ksec->sid = tsec->sid; 4278d720024eSMichael LeMay k->security = ksec; 4279d720024eSMichael LeMay 4280d720024eSMichael LeMay return 0; 4281d720024eSMichael LeMay } 4282d720024eSMichael LeMay 4283d720024eSMichael LeMay static void selinux_key_free(struct key *k) 4284d720024eSMichael LeMay { 4285d720024eSMichael LeMay struct key_security_struct *ksec = k->security; 4286d720024eSMichael LeMay 4287d720024eSMichael LeMay k->security = NULL; 4288d720024eSMichael LeMay kfree(ksec); 4289d720024eSMichael LeMay } 4290d720024eSMichael LeMay 4291d720024eSMichael LeMay static int selinux_key_permission(key_ref_t key_ref, 4292d720024eSMichael LeMay struct task_struct *ctx, 4293d720024eSMichael LeMay key_perm_t perm) 4294d720024eSMichael LeMay { 4295d720024eSMichael LeMay struct key *key; 4296d720024eSMichael LeMay struct task_security_struct *tsec; 4297d720024eSMichael LeMay struct key_security_struct *ksec; 4298d720024eSMichael LeMay 4299d720024eSMichael LeMay key = key_ref_to_ptr(key_ref); 4300d720024eSMichael LeMay 4301d720024eSMichael LeMay tsec = ctx->security; 4302d720024eSMichael LeMay ksec = key->security; 4303d720024eSMichael LeMay 4304d720024eSMichael LeMay /* if no specific permissions are requested, we skip the 4305d720024eSMichael LeMay permission check. No serious, additional covert channels 4306d720024eSMichael LeMay appear to be created. */ 4307d720024eSMichael LeMay if (perm == 0) 4308d720024eSMichael LeMay return 0; 4309d720024eSMichael LeMay 4310d720024eSMichael LeMay return avc_has_perm(tsec->sid, ksec->sid, 4311d720024eSMichael LeMay SECCLASS_KEY, perm, NULL); 4312d720024eSMichael LeMay } 4313d720024eSMichael LeMay 4314d720024eSMichael LeMay #endif 4315d720024eSMichael LeMay 43161da177e4SLinus Torvalds static struct security_operations selinux_ops = { 43171da177e4SLinus Torvalds .ptrace = selinux_ptrace, 43181da177e4SLinus Torvalds .capget = selinux_capget, 43191da177e4SLinus Torvalds .capset_check = selinux_capset_check, 43201da177e4SLinus Torvalds .capset_set = selinux_capset_set, 43211da177e4SLinus Torvalds .sysctl = selinux_sysctl, 43221da177e4SLinus Torvalds .capable = selinux_capable, 43231da177e4SLinus Torvalds .quotactl = selinux_quotactl, 43241da177e4SLinus Torvalds .quota_on = selinux_quota_on, 43251da177e4SLinus Torvalds .syslog = selinux_syslog, 43261da177e4SLinus Torvalds .vm_enough_memory = selinux_vm_enough_memory, 43271da177e4SLinus Torvalds 43281da177e4SLinus Torvalds .netlink_send = selinux_netlink_send, 43291da177e4SLinus Torvalds .netlink_recv = selinux_netlink_recv, 43301da177e4SLinus Torvalds 43311da177e4SLinus Torvalds .bprm_alloc_security = selinux_bprm_alloc_security, 43321da177e4SLinus Torvalds .bprm_free_security = selinux_bprm_free_security, 43331da177e4SLinus Torvalds .bprm_apply_creds = selinux_bprm_apply_creds, 43341da177e4SLinus Torvalds .bprm_post_apply_creds = selinux_bprm_post_apply_creds, 43351da177e4SLinus Torvalds .bprm_set_security = selinux_bprm_set_security, 43361da177e4SLinus Torvalds .bprm_check_security = selinux_bprm_check_security, 43371da177e4SLinus Torvalds .bprm_secureexec = selinux_bprm_secureexec, 43381da177e4SLinus Torvalds 43391da177e4SLinus Torvalds .sb_alloc_security = selinux_sb_alloc_security, 43401da177e4SLinus Torvalds .sb_free_security = selinux_sb_free_security, 43411da177e4SLinus Torvalds .sb_copy_data = selinux_sb_copy_data, 43421da177e4SLinus Torvalds .sb_kern_mount = selinux_sb_kern_mount, 43431da177e4SLinus Torvalds .sb_statfs = selinux_sb_statfs, 43441da177e4SLinus Torvalds .sb_mount = selinux_mount, 43451da177e4SLinus Torvalds .sb_umount = selinux_umount, 43461da177e4SLinus Torvalds 43471da177e4SLinus Torvalds .inode_alloc_security = selinux_inode_alloc_security, 43481da177e4SLinus Torvalds .inode_free_security = selinux_inode_free_security, 43495e41ff9eSStephen Smalley .inode_init_security = selinux_inode_init_security, 43501da177e4SLinus Torvalds .inode_create = selinux_inode_create, 43511da177e4SLinus Torvalds .inode_link = selinux_inode_link, 43521da177e4SLinus Torvalds .inode_unlink = selinux_inode_unlink, 43531da177e4SLinus Torvalds .inode_symlink = selinux_inode_symlink, 43541da177e4SLinus Torvalds .inode_mkdir = selinux_inode_mkdir, 43551da177e4SLinus Torvalds .inode_rmdir = selinux_inode_rmdir, 43561da177e4SLinus Torvalds .inode_mknod = selinux_inode_mknod, 43571da177e4SLinus Torvalds .inode_rename = selinux_inode_rename, 43581da177e4SLinus Torvalds .inode_readlink = selinux_inode_readlink, 43591da177e4SLinus Torvalds .inode_follow_link = selinux_inode_follow_link, 43601da177e4SLinus Torvalds .inode_permission = selinux_inode_permission, 43611da177e4SLinus Torvalds .inode_setattr = selinux_inode_setattr, 43621da177e4SLinus Torvalds .inode_getattr = selinux_inode_getattr, 43631da177e4SLinus Torvalds .inode_setxattr = selinux_inode_setxattr, 43641da177e4SLinus Torvalds .inode_post_setxattr = selinux_inode_post_setxattr, 43651da177e4SLinus Torvalds .inode_getxattr = selinux_inode_getxattr, 43661da177e4SLinus Torvalds .inode_listxattr = selinux_inode_listxattr, 43671da177e4SLinus Torvalds .inode_removexattr = selinux_inode_removexattr, 43688c8570fbSDustin Kirkland .inode_xattr_getsuffix = selinux_inode_xattr_getsuffix, 43691da177e4SLinus Torvalds .inode_getsecurity = selinux_inode_getsecurity, 43701da177e4SLinus Torvalds .inode_setsecurity = selinux_inode_setsecurity, 43711da177e4SLinus Torvalds .inode_listsecurity = selinux_inode_listsecurity, 43721da177e4SLinus Torvalds 43731da177e4SLinus Torvalds .file_permission = selinux_file_permission, 43741da177e4SLinus Torvalds .file_alloc_security = selinux_file_alloc_security, 43751da177e4SLinus Torvalds .file_free_security = selinux_file_free_security, 43761da177e4SLinus Torvalds .file_ioctl = selinux_file_ioctl, 43771da177e4SLinus Torvalds .file_mmap = selinux_file_mmap, 43781da177e4SLinus Torvalds .file_mprotect = selinux_file_mprotect, 43791da177e4SLinus Torvalds .file_lock = selinux_file_lock, 43801da177e4SLinus Torvalds .file_fcntl = selinux_file_fcntl, 43811da177e4SLinus Torvalds .file_set_fowner = selinux_file_set_fowner, 43821da177e4SLinus Torvalds .file_send_sigiotask = selinux_file_send_sigiotask, 43831da177e4SLinus Torvalds .file_receive = selinux_file_receive, 43841da177e4SLinus Torvalds 43851da177e4SLinus Torvalds .task_create = selinux_task_create, 43861da177e4SLinus Torvalds .task_alloc_security = selinux_task_alloc_security, 43871da177e4SLinus Torvalds .task_free_security = selinux_task_free_security, 43881da177e4SLinus Torvalds .task_setuid = selinux_task_setuid, 43891da177e4SLinus Torvalds .task_post_setuid = selinux_task_post_setuid, 43901da177e4SLinus Torvalds .task_setgid = selinux_task_setgid, 43911da177e4SLinus Torvalds .task_setpgid = selinux_task_setpgid, 43921da177e4SLinus Torvalds .task_getpgid = selinux_task_getpgid, 43931da177e4SLinus Torvalds .task_getsid = selinux_task_getsid, 43941da177e4SLinus Torvalds .task_setgroups = selinux_task_setgroups, 43951da177e4SLinus Torvalds .task_setnice = selinux_task_setnice, 439603e68060SJames Morris .task_setioprio = selinux_task_setioprio, 43971da177e4SLinus Torvalds .task_setrlimit = selinux_task_setrlimit, 43981da177e4SLinus Torvalds .task_setscheduler = selinux_task_setscheduler, 43991da177e4SLinus Torvalds .task_getscheduler = selinux_task_getscheduler, 4400*35601547SDavid Quigley .task_movememory = selinux_task_movememory, 44011da177e4SLinus Torvalds .task_kill = selinux_task_kill, 44021da177e4SLinus Torvalds .task_wait = selinux_task_wait, 44031da177e4SLinus Torvalds .task_prctl = selinux_task_prctl, 44041da177e4SLinus Torvalds .task_reparent_to_init = selinux_task_reparent_to_init, 44051da177e4SLinus Torvalds .task_to_inode = selinux_task_to_inode, 44061da177e4SLinus Torvalds 44071da177e4SLinus Torvalds .ipc_permission = selinux_ipc_permission, 44081da177e4SLinus Torvalds 44091da177e4SLinus Torvalds .msg_msg_alloc_security = selinux_msg_msg_alloc_security, 44101da177e4SLinus Torvalds .msg_msg_free_security = selinux_msg_msg_free_security, 44111da177e4SLinus Torvalds 44121da177e4SLinus Torvalds .msg_queue_alloc_security = selinux_msg_queue_alloc_security, 44131da177e4SLinus Torvalds .msg_queue_free_security = selinux_msg_queue_free_security, 44141da177e4SLinus Torvalds .msg_queue_associate = selinux_msg_queue_associate, 44151da177e4SLinus Torvalds .msg_queue_msgctl = selinux_msg_queue_msgctl, 44161da177e4SLinus Torvalds .msg_queue_msgsnd = selinux_msg_queue_msgsnd, 44171da177e4SLinus Torvalds .msg_queue_msgrcv = selinux_msg_queue_msgrcv, 44181da177e4SLinus Torvalds 44191da177e4SLinus Torvalds .shm_alloc_security = selinux_shm_alloc_security, 44201da177e4SLinus Torvalds .shm_free_security = selinux_shm_free_security, 44211da177e4SLinus Torvalds .shm_associate = selinux_shm_associate, 44221da177e4SLinus Torvalds .shm_shmctl = selinux_shm_shmctl, 44231da177e4SLinus Torvalds .shm_shmat = selinux_shm_shmat, 44241da177e4SLinus Torvalds 44251da177e4SLinus Torvalds .sem_alloc_security = selinux_sem_alloc_security, 44261da177e4SLinus Torvalds .sem_free_security = selinux_sem_free_security, 44271da177e4SLinus Torvalds .sem_associate = selinux_sem_associate, 44281da177e4SLinus Torvalds .sem_semctl = selinux_sem_semctl, 44291da177e4SLinus Torvalds .sem_semop = selinux_sem_semop, 44301da177e4SLinus Torvalds 44311da177e4SLinus Torvalds .register_security = selinux_register_security, 44321da177e4SLinus Torvalds .unregister_security = selinux_unregister_security, 44331da177e4SLinus Torvalds 44341da177e4SLinus Torvalds .d_instantiate = selinux_d_instantiate, 44351da177e4SLinus Torvalds 44361da177e4SLinus Torvalds .getprocattr = selinux_getprocattr, 44371da177e4SLinus Torvalds .setprocattr = selinux_setprocattr, 44381da177e4SLinus Torvalds 44391da177e4SLinus Torvalds .unix_stream_connect = selinux_socket_unix_stream_connect, 44401da177e4SLinus Torvalds .unix_may_send = selinux_socket_unix_may_send, 44411da177e4SLinus Torvalds 44421da177e4SLinus Torvalds .socket_create = selinux_socket_create, 44431da177e4SLinus Torvalds .socket_post_create = selinux_socket_post_create, 44441da177e4SLinus Torvalds .socket_bind = selinux_socket_bind, 44451da177e4SLinus Torvalds .socket_connect = selinux_socket_connect, 44461da177e4SLinus Torvalds .socket_listen = selinux_socket_listen, 44471da177e4SLinus Torvalds .socket_accept = selinux_socket_accept, 44481da177e4SLinus Torvalds .socket_sendmsg = selinux_socket_sendmsg, 44491da177e4SLinus Torvalds .socket_recvmsg = selinux_socket_recvmsg, 44501da177e4SLinus Torvalds .socket_getsockname = selinux_socket_getsockname, 44511da177e4SLinus Torvalds .socket_getpeername = selinux_socket_getpeername, 44521da177e4SLinus Torvalds .socket_getsockopt = selinux_socket_getsockopt, 44531da177e4SLinus Torvalds .socket_setsockopt = selinux_socket_setsockopt, 44541da177e4SLinus Torvalds .socket_shutdown = selinux_socket_shutdown, 44551da177e4SLinus Torvalds .socket_sock_rcv_skb = selinux_socket_sock_rcv_skb, 44562c7946a7SCatherine Zhang .socket_getpeersec_stream = selinux_socket_getpeersec_stream, 44572c7946a7SCatherine Zhang .socket_getpeersec_dgram = selinux_socket_getpeersec_dgram, 44581da177e4SLinus Torvalds .sk_alloc_security = selinux_sk_alloc_security, 44591da177e4SLinus Torvalds .sk_free_security = selinux_sk_free_security, 4460d28d1e08STrent Jaeger .sk_getsid = selinux_sk_getsid_security, 4461d28d1e08STrent Jaeger 4462d28d1e08STrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM 4463d28d1e08STrent Jaeger .xfrm_policy_alloc_security = selinux_xfrm_policy_alloc, 4464d28d1e08STrent Jaeger .xfrm_policy_clone_security = selinux_xfrm_policy_clone, 4465d28d1e08STrent Jaeger .xfrm_policy_free_security = selinux_xfrm_policy_free, 4466c8c05a8eSCatherine Zhang .xfrm_policy_delete_security = selinux_xfrm_policy_delete, 4467d28d1e08STrent Jaeger .xfrm_state_alloc_security = selinux_xfrm_state_alloc, 4468d28d1e08STrent Jaeger .xfrm_state_free_security = selinux_xfrm_state_free, 4469c8c05a8eSCatherine Zhang .xfrm_state_delete_security = selinux_xfrm_state_delete, 4470d28d1e08STrent Jaeger .xfrm_policy_lookup = selinux_xfrm_policy_lookup, 44711da177e4SLinus Torvalds #endif 4472d720024eSMichael LeMay 4473d720024eSMichael LeMay #ifdef CONFIG_KEYS 4474d720024eSMichael LeMay .key_alloc = selinux_key_alloc, 4475d720024eSMichael LeMay .key_free = selinux_key_free, 4476d720024eSMichael LeMay .key_permission = selinux_key_permission, 4477d720024eSMichael LeMay #endif 44781da177e4SLinus Torvalds }; 44791da177e4SLinus Torvalds 44801da177e4SLinus Torvalds static __init int selinux_init(void) 44811da177e4SLinus Torvalds { 44821da177e4SLinus Torvalds struct task_security_struct *tsec; 44831da177e4SLinus Torvalds 44841da177e4SLinus Torvalds if (!selinux_enabled) { 44851da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Disabled at boot.\n"); 44861da177e4SLinus Torvalds return 0; 44871da177e4SLinus Torvalds } 44881da177e4SLinus Torvalds 44891da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Initializing.\n"); 44901da177e4SLinus Torvalds 44911da177e4SLinus Torvalds /* Set the security state for the initial task. */ 44921da177e4SLinus Torvalds if (task_alloc_security(current)) 44931da177e4SLinus Torvalds panic("SELinux: Failed to initialize initial task.\n"); 44941da177e4SLinus Torvalds tsec = current->security; 44951da177e4SLinus Torvalds tsec->osid = tsec->sid = SECINITSID_KERNEL; 44961da177e4SLinus Torvalds 44977cae7e26SJames Morris sel_inode_cache = kmem_cache_create("selinux_inode_security", 44987cae7e26SJames Morris sizeof(struct inode_security_struct), 44997cae7e26SJames Morris 0, SLAB_PANIC, NULL, NULL); 45001da177e4SLinus Torvalds avc_init(); 45011da177e4SLinus Torvalds 45021da177e4SLinus Torvalds original_ops = secondary_ops = security_ops; 45031da177e4SLinus Torvalds if (!secondary_ops) 45041da177e4SLinus Torvalds panic ("SELinux: No initial security operations\n"); 45051da177e4SLinus Torvalds if (register_security (&selinux_ops)) 45061da177e4SLinus Torvalds panic("SELinux: Unable to register with kernel.\n"); 45071da177e4SLinus Torvalds 45081da177e4SLinus Torvalds if (selinux_enforcing) { 45091da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Starting in enforcing mode\n"); 45101da177e4SLinus Torvalds } else { 45111da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Starting in permissive mode\n"); 45121da177e4SLinus Torvalds } 4513d720024eSMichael LeMay 4514d720024eSMichael LeMay #ifdef CONFIG_KEYS 4515d720024eSMichael LeMay /* Add security information to initial keyrings */ 4516d720024eSMichael LeMay security_key_alloc(&root_user_keyring, current); 4517d720024eSMichael LeMay security_key_alloc(&root_session_keyring, current); 4518d720024eSMichael LeMay #endif 4519d720024eSMichael LeMay 45201da177e4SLinus Torvalds return 0; 45211da177e4SLinus Torvalds } 45221da177e4SLinus Torvalds 45231da177e4SLinus Torvalds void selinux_complete_init(void) 45241da177e4SLinus Torvalds { 45251da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Completing initialization.\n"); 45261da177e4SLinus Torvalds 45271da177e4SLinus Torvalds /* Set up any superblocks initialized prior to the policy load. */ 45281da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Setting up existing superblocks.\n"); 4529ba0c19edSStephen Smalley spin_lock(&sb_lock); 45301da177e4SLinus Torvalds spin_lock(&sb_security_lock); 45311da177e4SLinus Torvalds next_sb: 45321da177e4SLinus Torvalds if (!list_empty(&superblock_security_head)) { 45331da177e4SLinus Torvalds struct superblock_security_struct *sbsec = 45341da177e4SLinus Torvalds list_entry(superblock_security_head.next, 45351da177e4SLinus Torvalds struct superblock_security_struct, 45361da177e4SLinus Torvalds list); 45371da177e4SLinus Torvalds struct super_block *sb = sbsec->sb; 45381da177e4SLinus Torvalds sb->s_count++; 45391da177e4SLinus Torvalds spin_unlock(&sb_security_lock); 4540ba0c19edSStephen Smalley spin_unlock(&sb_lock); 45411da177e4SLinus Torvalds down_read(&sb->s_umount); 45421da177e4SLinus Torvalds if (sb->s_root) 45431da177e4SLinus Torvalds superblock_doinit(sb, NULL); 45441da177e4SLinus Torvalds drop_super(sb); 4545ba0c19edSStephen Smalley spin_lock(&sb_lock); 45461da177e4SLinus Torvalds spin_lock(&sb_security_lock); 45471da177e4SLinus Torvalds list_del_init(&sbsec->list); 45481da177e4SLinus Torvalds goto next_sb; 45491da177e4SLinus Torvalds } 45501da177e4SLinus Torvalds spin_unlock(&sb_security_lock); 4551ba0c19edSStephen Smalley spin_unlock(&sb_lock); 45521da177e4SLinus Torvalds } 45531da177e4SLinus Torvalds 45541da177e4SLinus Torvalds /* SELinux requires early initialization in order to label 45551da177e4SLinus Torvalds all processes and objects when they are created. */ 45561da177e4SLinus Torvalds security_initcall(selinux_init); 45571da177e4SLinus Torvalds 4558c2b507fdSStephen Smalley #if defined(CONFIG_NETFILTER) 45591da177e4SLinus Torvalds 45601da177e4SLinus Torvalds static struct nf_hook_ops selinux_ipv4_op = { 45611da177e4SLinus Torvalds .hook = selinux_ipv4_postroute_last, 45621da177e4SLinus Torvalds .owner = THIS_MODULE, 45631da177e4SLinus Torvalds .pf = PF_INET, 45641da177e4SLinus Torvalds .hooknum = NF_IP_POST_ROUTING, 45651da177e4SLinus Torvalds .priority = NF_IP_PRI_SELINUX_LAST, 45661da177e4SLinus Torvalds }; 45671da177e4SLinus Torvalds 45681da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 45691da177e4SLinus Torvalds 45701da177e4SLinus Torvalds static struct nf_hook_ops selinux_ipv6_op = { 45711da177e4SLinus Torvalds .hook = selinux_ipv6_postroute_last, 45721da177e4SLinus Torvalds .owner = THIS_MODULE, 45731da177e4SLinus Torvalds .pf = PF_INET6, 45741da177e4SLinus Torvalds .hooknum = NF_IP6_POST_ROUTING, 45751da177e4SLinus Torvalds .priority = NF_IP6_PRI_SELINUX_LAST, 45761da177e4SLinus Torvalds }; 45771da177e4SLinus Torvalds 45781da177e4SLinus Torvalds #endif /* IPV6 */ 45791da177e4SLinus Torvalds 45801da177e4SLinus Torvalds static int __init selinux_nf_ip_init(void) 45811da177e4SLinus Torvalds { 45821da177e4SLinus Torvalds int err = 0; 45831da177e4SLinus Torvalds 45841da177e4SLinus Torvalds if (!selinux_enabled) 45851da177e4SLinus Torvalds goto out; 45861da177e4SLinus Torvalds 45871da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Registering netfilter hooks\n"); 45881da177e4SLinus Torvalds 45891da177e4SLinus Torvalds err = nf_register_hook(&selinux_ipv4_op); 45901da177e4SLinus Torvalds if (err) 45911da177e4SLinus Torvalds panic("SELinux: nf_register_hook for IPv4: error %d\n", err); 45921da177e4SLinus Torvalds 45931da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 45941da177e4SLinus Torvalds 45951da177e4SLinus Torvalds err = nf_register_hook(&selinux_ipv6_op); 45961da177e4SLinus Torvalds if (err) 45971da177e4SLinus Torvalds panic("SELinux: nf_register_hook for IPv6: error %d\n", err); 45981da177e4SLinus Torvalds 45991da177e4SLinus Torvalds #endif /* IPV6 */ 4600d28d1e08STrent Jaeger 46011da177e4SLinus Torvalds out: 46021da177e4SLinus Torvalds return err; 46031da177e4SLinus Torvalds } 46041da177e4SLinus Torvalds 46051da177e4SLinus Torvalds __initcall(selinux_nf_ip_init); 46061da177e4SLinus Torvalds 46071da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 46081da177e4SLinus Torvalds static void selinux_nf_ip_exit(void) 46091da177e4SLinus Torvalds { 46101da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Unregistering netfilter hooks\n"); 46111da177e4SLinus Torvalds 46121da177e4SLinus Torvalds nf_unregister_hook(&selinux_ipv4_op); 46131da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 46141da177e4SLinus Torvalds nf_unregister_hook(&selinux_ipv6_op); 46151da177e4SLinus Torvalds #endif /* IPV6 */ 46161da177e4SLinus Torvalds } 46171da177e4SLinus Torvalds #endif 46181da177e4SLinus Torvalds 4619c2b507fdSStephen Smalley #else /* CONFIG_NETFILTER */ 46201da177e4SLinus Torvalds 46211da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 46221da177e4SLinus Torvalds #define selinux_nf_ip_exit() 46231da177e4SLinus Torvalds #endif 46241da177e4SLinus Torvalds 4625c2b507fdSStephen Smalley #endif /* CONFIG_NETFILTER */ 46261da177e4SLinus Torvalds 46271da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 46281da177e4SLinus Torvalds int selinux_disable(void) 46291da177e4SLinus Torvalds { 46301da177e4SLinus Torvalds extern void exit_sel_fs(void); 46311da177e4SLinus Torvalds static int selinux_disabled = 0; 46321da177e4SLinus Torvalds 46331da177e4SLinus Torvalds if (ss_initialized) { 46341da177e4SLinus Torvalds /* Not permitted after initial policy load. */ 46351da177e4SLinus Torvalds return -EINVAL; 46361da177e4SLinus Torvalds } 46371da177e4SLinus Torvalds 46381da177e4SLinus Torvalds if (selinux_disabled) { 46391da177e4SLinus Torvalds /* Only do this once. */ 46401da177e4SLinus Torvalds return -EINVAL; 46411da177e4SLinus Torvalds } 46421da177e4SLinus Torvalds 46431da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Disabled at runtime.\n"); 46441da177e4SLinus Torvalds 46451da177e4SLinus Torvalds selinux_disabled = 1; 464630d55280SStephen Smalley selinux_enabled = 0; 46471da177e4SLinus Torvalds 46481da177e4SLinus Torvalds /* Reset security_ops to the secondary module, dummy or capability. */ 46491da177e4SLinus Torvalds security_ops = secondary_ops; 46501da177e4SLinus Torvalds 46511da177e4SLinus Torvalds /* Unregister netfilter hooks. */ 46521da177e4SLinus Torvalds selinux_nf_ip_exit(); 46531da177e4SLinus Torvalds 46541da177e4SLinus Torvalds /* Unregister selinuxfs. */ 46551da177e4SLinus Torvalds exit_sel_fs(); 46561da177e4SLinus Torvalds 46571da177e4SLinus Torvalds return 0; 46581da177e4SLinus Torvalds } 46591da177e4SLinus Torvalds #endif 46601da177e4SLinus Torvalds 46611da177e4SLinus Torvalds 4662