11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * NSA Security-Enhanced Linux (SELinux) security module 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * This file contains the SELinux hook function implementations. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Authors: Stephen Smalley, <sds@epoch.ncsc.mil> 71da177e4SLinus Torvalds * Chris Vance, <cvance@nai.com> 81da177e4SLinus Torvalds * Wayne Salamon, <wsalamon@nai.com> 91da177e4SLinus Torvalds * James Morris <jmorris@redhat.com> 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * Copyright (C) 2001,2002 Networks Associates Technology, Inc. 121da177e4SLinus Torvalds * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com> 131da177e4SLinus Torvalds * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc. 141da177e4SLinus Torvalds * <dgoeddel@trustedcs.com> 157420ed23SVenkat Yekkirala * Copyright (C) 2006 Hewlett-Packard Development Company, L.P. 167420ed23SVenkat Yekkirala * Paul Moore, <paul.moore@hp.com> 171da177e4SLinus Torvalds * 181da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 191da177e4SLinus Torvalds * it under the terms of the GNU General Public License version 2, 201da177e4SLinus Torvalds * as published by the Free Software Foundation. 211da177e4SLinus Torvalds */ 221da177e4SLinus Torvalds 231da177e4SLinus Torvalds #include <linux/module.h> 241da177e4SLinus Torvalds #include <linux/init.h> 251da177e4SLinus Torvalds #include <linux/kernel.h> 261da177e4SLinus Torvalds #include <linux/ptrace.h> 271da177e4SLinus Torvalds #include <linux/errno.h> 281da177e4SLinus Torvalds #include <linux/sched.h> 291da177e4SLinus Torvalds #include <linux/security.h> 301da177e4SLinus Torvalds #include <linux/xattr.h> 311da177e4SLinus Torvalds #include <linux/capability.h> 321da177e4SLinus Torvalds #include <linux/unistd.h> 331da177e4SLinus Torvalds #include <linux/mm.h> 341da177e4SLinus Torvalds #include <linux/mman.h> 351da177e4SLinus Torvalds #include <linux/slab.h> 361da177e4SLinus Torvalds #include <linux/pagemap.h> 371da177e4SLinus Torvalds #include <linux/swap.h> 381da177e4SLinus Torvalds #include <linux/smp_lock.h> 391da177e4SLinus Torvalds #include <linux/spinlock.h> 401da177e4SLinus Torvalds #include <linux/syscalls.h> 411da177e4SLinus Torvalds #include <linux/file.h> 421da177e4SLinus Torvalds #include <linux/namei.h> 431da177e4SLinus Torvalds #include <linux/mount.h> 441da177e4SLinus Torvalds #include <linux/ext2_fs.h> 451da177e4SLinus Torvalds #include <linux/proc_fs.h> 461da177e4SLinus Torvalds #include <linux/kd.h> 471da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h> 481da177e4SLinus Torvalds #include <linux/netfilter_ipv6.h> 491da177e4SLinus Torvalds #include <linux/tty.h> 501da177e4SLinus Torvalds #include <net/icmp.h> 511da177e4SLinus Torvalds #include <net/ip.h> /* for sysctl_local_port_range[] */ 521da177e4SLinus Torvalds #include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */ 531da177e4SLinus Torvalds #include <asm/uaccess.h> 541da177e4SLinus Torvalds #include <asm/ioctls.h> 551da177e4SLinus Torvalds #include <linux/bitops.h> 561da177e4SLinus Torvalds #include <linux/interrupt.h> 571da177e4SLinus Torvalds #include <linux/netdevice.h> /* for network interface checks */ 581da177e4SLinus Torvalds #include <linux/netlink.h> 591da177e4SLinus Torvalds #include <linux/tcp.h> 601da177e4SLinus Torvalds #include <linux/udp.h> 612ee92d46SJames Morris #include <linux/dccp.h> 621da177e4SLinus Torvalds #include <linux/quota.h> 631da177e4SLinus Torvalds #include <linux/un.h> /* for Unix socket types */ 641da177e4SLinus Torvalds #include <net/af_unix.h> /* for Unix socket types */ 651da177e4SLinus Torvalds #include <linux/parser.h> 661da177e4SLinus Torvalds #include <linux/nfs_mount.h> 671da177e4SLinus Torvalds #include <net/ipv6.h> 681da177e4SLinus Torvalds #include <linux/hugetlb.h> 691da177e4SLinus Torvalds #include <linux/personality.h> 701da177e4SLinus Torvalds #include <linux/sysctl.h> 711da177e4SLinus Torvalds #include <linux/audit.h> 726931dfc9SEric Paris #include <linux/string.h> 73877ce7c1SCatherine Zhang #include <linux/selinux.h> 7423970741SEric Paris #include <linux/mutex.h> 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds #include "avc.h" 771da177e4SLinus Torvalds #include "objsec.h" 781da177e4SLinus Torvalds #include "netif.h" 79d28d1e08STrent Jaeger #include "xfrm.h" 807420ed23SVenkat Yekkirala #include "selinux_netlabel.h" 811da177e4SLinus Torvalds 821da177e4SLinus Torvalds #define XATTR_SELINUX_SUFFIX "selinux" 831da177e4SLinus Torvalds #define XATTR_NAME_SELINUX XATTR_SECURITY_PREFIX XATTR_SELINUX_SUFFIX 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds extern unsigned int policydb_loaded_version; 861da177e4SLinus Torvalds extern int selinux_nlmsg_lookup(u16 sclass, u16 nlmsg_type, u32 *perm); 874e5ab4cbSJames Morris extern int selinux_compat_net; 881da177e4SLinus Torvalds 891da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP 901da177e4SLinus Torvalds int selinux_enforcing = 0; 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds static int __init enforcing_setup(char *str) 931da177e4SLinus Torvalds { 941da177e4SLinus Torvalds selinux_enforcing = simple_strtol(str,NULL,0); 951da177e4SLinus Torvalds return 1; 961da177e4SLinus Torvalds } 971da177e4SLinus Torvalds __setup("enforcing=", enforcing_setup); 981da177e4SLinus Torvalds #endif 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM 1011da177e4SLinus Torvalds int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE; 1021da177e4SLinus Torvalds 1031da177e4SLinus Torvalds static int __init selinux_enabled_setup(char *str) 1041da177e4SLinus Torvalds { 1051da177e4SLinus Torvalds selinux_enabled = simple_strtol(str, NULL, 0); 1061da177e4SLinus Torvalds return 1; 1071da177e4SLinus Torvalds } 1081da177e4SLinus Torvalds __setup("selinux=", selinux_enabled_setup); 10930d55280SStephen Smalley #else 11030d55280SStephen Smalley int selinux_enabled = 1; 1111da177e4SLinus Torvalds #endif 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds /* Original (dummy) security module. */ 1141da177e4SLinus Torvalds static struct security_operations *original_ops = NULL; 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds /* Minimal support for a secondary security module, 1171da177e4SLinus Torvalds just to allow the use of the dummy or capability modules. 1181da177e4SLinus Torvalds The owlsm module can alternatively be used as a secondary 1191da177e4SLinus Torvalds module as long as CONFIG_OWLSM_FD is not enabled. */ 1201da177e4SLinus Torvalds static struct security_operations *secondary_ops = NULL; 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds /* Lists of inode and superblock security structures initialized 1231da177e4SLinus Torvalds before the policy was loaded. */ 1241da177e4SLinus Torvalds static LIST_HEAD(superblock_security_head); 1251da177e4SLinus Torvalds static DEFINE_SPINLOCK(sb_security_lock); 1261da177e4SLinus Torvalds 127e18b890bSChristoph Lameter static struct kmem_cache *sel_inode_cache; 1287cae7e26SJames Morris 1298c8570fbSDustin Kirkland /* Return security context for a given sid or just the context 1308c8570fbSDustin Kirkland length if the buffer is null or length is 0 */ 1318c8570fbSDustin Kirkland static int selinux_getsecurity(u32 sid, void *buffer, size_t size) 1328c8570fbSDustin Kirkland { 1338c8570fbSDustin Kirkland char *context; 1348c8570fbSDustin Kirkland unsigned len; 1358c8570fbSDustin Kirkland int rc; 1368c8570fbSDustin Kirkland 1378c8570fbSDustin Kirkland rc = security_sid_to_context(sid, &context, &len); 1388c8570fbSDustin Kirkland if (rc) 1398c8570fbSDustin Kirkland return rc; 1408c8570fbSDustin Kirkland 1418c8570fbSDustin Kirkland if (!buffer || !size) 1428c8570fbSDustin Kirkland goto getsecurity_exit; 1438c8570fbSDustin Kirkland 1448c8570fbSDustin Kirkland if (size < len) { 1458c8570fbSDustin Kirkland len = -ERANGE; 1468c8570fbSDustin Kirkland goto getsecurity_exit; 1478c8570fbSDustin Kirkland } 1488c8570fbSDustin Kirkland memcpy(buffer, context, len); 1498c8570fbSDustin Kirkland 1508c8570fbSDustin Kirkland getsecurity_exit: 1518c8570fbSDustin Kirkland kfree(context); 1528c8570fbSDustin Kirkland return len; 1538c8570fbSDustin Kirkland } 1548c8570fbSDustin Kirkland 1551da177e4SLinus Torvalds /* Allocate and free functions for each kind of security blob. */ 1561da177e4SLinus Torvalds 1571da177e4SLinus Torvalds static int task_alloc_security(struct task_struct *task) 1581da177e4SLinus Torvalds { 1591da177e4SLinus Torvalds struct task_security_struct *tsec; 1601da177e4SLinus Torvalds 16189d155efSJames Morris tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL); 1621da177e4SLinus Torvalds if (!tsec) 1631da177e4SLinus Torvalds return -ENOMEM; 1641da177e4SLinus Torvalds 1651da177e4SLinus Torvalds tsec->task = task; 1661da177e4SLinus Torvalds tsec->osid = tsec->sid = tsec->ptrace_sid = SECINITSID_UNLABELED; 1671da177e4SLinus Torvalds task->security = tsec; 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds return 0; 1701da177e4SLinus Torvalds } 1711da177e4SLinus Torvalds 1721da177e4SLinus Torvalds static void task_free_security(struct task_struct *task) 1731da177e4SLinus Torvalds { 1741da177e4SLinus Torvalds struct task_security_struct *tsec = task->security; 1751da177e4SLinus Torvalds task->security = NULL; 1761da177e4SLinus Torvalds kfree(tsec); 1771da177e4SLinus Torvalds } 1781da177e4SLinus Torvalds 1791da177e4SLinus Torvalds static int inode_alloc_security(struct inode *inode) 1801da177e4SLinus Torvalds { 1811da177e4SLinus Torvalds struct task_security_struct *tsec = current->security; 1821da177e4SLinus Torvalds struct inode_security_struct *isec; 1831da177e4SLinus Torvalds 184c3762229SRobert P. J. Day isec = kmem_cache_zalloc(sel_inode_cache, GFP_KERNEL); 1851da177e4SLinus Torvalds if (!isec) 1861da177e4SLinus Torvalds return -ENOMEM; 1871da177e4SLinus Torvalds 18823970741SEric Paris mutex_init(&isec->lock); 1891da177e4SLinus Torvalds INIT_LIST_HEAD(&isec->list); 1901da177e4SLinus Torvalds isec->inode = inode; 1911da177e4SLinus Torvalds isec->sid = SECINITSID_UNLABELED; 1921da177e4SLinus Torvalds isec->sclass = SECCLASS_FILE; 1931da177e4SLinus Torvalds isec->task_sid = tsec->sid; 1941da177e4SLinus Torvalds inode->i_security = isec; 1951da177e4SLinus Torvalds 1961da177e4SLinus Torvalds return 0; 1971da177e4SLinus Torvalds } 1981da177e4SLinus Torvalds 1991da177e4SLinus Torvalds static void inode_free_security(struct inode *inode) 2001da177e4SLinus Torvalds { 2011da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 2021da177e4SLinus Torvalds struct superblock_security_struct *sbsec = inode->i_sb->s_security; 2031da177e4SLinus Torvalds 2041da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 2051da177e4SLinus Torvalds if (!list_empty(&isec->list)) 2061da177e4SLinus Torvalds list_del_init(&isec->list); 2071da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 2081da177e4SLinus Torvalds 2091da177e4SLinus Torvalds inode->i_security = NULL; 2107cae7e26SJames Morris kmem_cache_free(sel_inode_cache, isec); 2111da177e4SLinus Torvalds } 2121da177e4SLinus Torvalds 2131da177e4SLinus Torvalds static int file_alloc_security(struct file *file) 2141da177e4SLinus Torvalds { 2151da177e4SLinus Torvalds struct task_security_struct *tsec = current->security; 2161da177e4SLinus Torvalds struct file_security_struct *fsec; 2171da177e4SLinus Torvalds 21826d2a4beSStephen Smalley fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL); 2191da177e4SLinus Torvalds if (!fsec) 2201da177e4SLinus Torvalds return -ENOMEM; 2211da177e4SLinus Torvalds 2221da177e4SLinus Torvalds fsec->file = file; 2231da177e4SLinus Torvalds fsec->sid = tsec->sid; 2241da177e4SLinus Torvalds fsec->fown_sid = tsec->sid; 2251da177e4SLinus Torvalds file->f_security = fsec; 2261da177e4SLinus Torvalds 2271da177e4SLinus Torvalds return 0; 2281da177e4SLinus Torvalds } 2291da177e4SLinus Torvalds 2301da177e4SLinus Torvalds static void file_free_security(struct file *file) 2311da177e4SLinus Torvalds { 2321da177e4SLinus Torvalds struct file_security_struct *fsec = file->f_security; 2331da177e4SLinus Torvalds file->f_security = NULL; 2341da177e4SLinus Torvalds kfree(fsec); 2351da177e4SLinus Torvalds } 2361da177e4SLinus Torvalds 2371da177e4SLinus Torvalds static int superblock_alloc_security(struct super_block *sb) 2381da177e4SLinus Torvalds { 2391da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 2401da177e4SLinus Torvalds 24189d155efSJames Morris sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL); 2421da177e4SLinus Torvalds if (!sbsec) 2431da177e4SLinus Torvalds return -ENOMEM; 2441da177e4SLinus Torvalds 245bc7e982bSEric Paris mutex_init(&sbsec->lock); 2461da177e4SLinus Torvalds INIT_LIST_HEAD(&sbsec->list); 2471da177e4SLinus Torvalds INIT_LIST_HEAD(&sbsec->isec_head); 2481da177e4SLinus Torvalds spin_lock_init(&sbsec->isec_lock); 2491da177e4SLinus Torvalds sbsec->sb = sb; 2501da177e4SLinus Torvalds sbsec->sid = SECINITSID_UNLABELED; 2511da177e4SLinus Torvalds sbsec->def_sid = SECINITSID_FILE; 252c312feb2SEric Paris sbsec->mntpoint_sid = SECINITSID_UNLABELED; 2531da177e4SLinus Torvalds sb->s_security = sbsec; 2541da177e4SLinus Torvalds 2551da177e4SLinus Torvalds return 0; 2561da177e4SLinus Torvalds } 2571da177e4SLinus Torvalds 2581da177e4SLinus Torvalds static void superblock_free_security(struct super_block *sb) 2591da177e4SLinus Torvalds { 2601da177e4SLinus Torvalds struct superblock_security_struct *sbsec = sb->s_security; 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds spin_lock(&sb_security_lock); 2631da177e4SLinus Torvalds if (!list_empty(&sbsec->list)) 2641da177e4SLinus Torvalds list_del_init(&sbsec->list); 2651da177e4SLinus Torvalds spin_unlock(&sb_security_lock); 2661da177e4SLinus Torvalds 2671da177e4SLinus Torvalds sb->s_security = NULL; 2681da177e4SLinus Torvalds kfree(sbsec); 2691da177e4SLinus Torvalds } 2701da177e4SLinus Torvalds 2717d877f3bSAl Viro static int sk_alloc_security(struct sock *sk, int family, gfp_t priority) 2721da177e4SLinus Torvalds { 2731da177e4SLinus Torvalds struct sk_security_struct *ssec; 2741da177e4SLinus Torvalds 27589d155efSJames Morris ssec = kzalloc(sizeof(*ssec), priority); 2761da177e4SLinus Torvalds if (!ssec) 2771da177e4SLinus Torvalds return -ENOMEM; 2781da177e4SLinus Torvalds 2791da177e4SLinus Torvalds ssec->sk = sk; 2801da177e4SLinus Torvalds ssec->peer_sid = SECINITSID_UNLABELED; 281892c141eSVenkat Yekkirala ssec->sid = SECINITSID_UNLABELED; 2821da177e4SLinus Torvalds sk->sk_security = ssec; 2831da177e4SLinus Torvalds 28499f59ed0SPaul Moore selinux_netlbl_sk_security_init(ssec, family); 28599f59ed0SPaul Moore 2861da177e4SLinus Torvalds return 0; 2871da177e4SLinus Torvalds } 2881da177e4SLinus Torvalds 2891da177e4SLinus Torvalds static void sk_free_security(struct sock *sk) 2901da177e4SLinus Torvalds { 2911da177e4SLinus Torvalds struct sk_security_struct *ssec = sk->sk_security; 2921da177e4SLinus Torvalds 2931da177e4SLinus Torvalds sk->sk_security = NULL; 2941da177e4SLinus Torvalds kfree(ssec); 2951da177e4SLinus Torvalds } 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds /* The security server must be initialized before 2981da177e4SLinus Torvalds any labeling or access decisions can be provided. */ 2991da177e4SLinus Torvalds extern int ss_initialized; 3001da177e4SLinus Torvalds 3011da177e4SLinus Torvalds /* The file system's label must be initialized prior to use. */ 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds static char *labeling_behaviors[6] = { 3041da177e4SLinus Torvalds "uses xattr", 3051da177e4SLinus Torvalds "uses transition SIDs", 3061da177e4SLinus Torvalds "uses task SIDs", 3071da177e4SLinus Torvalds "uses genfs_contexts", 3081da177e4SLinus Torvalds "not configured for labeling", 3091da177e4SLinus Torvalds "uses mountpoint labeling", 3101da177e4SLinus Torvalds }; 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry); 3131da177e4SLinus Torvalds 3141da177e4SLinus Torvalds static inline int inode_doinit(struct inode *inode) 3151da177e4SLinus Torvalds { 3161da177e4SLinus Torvalds return inode_doinit_with_dentry(inode, NULL); 3171da177e4SLinus Torvalds } 3181da177e4SLinus Torvalds 3191da177e4SLinus Torvalds enum { 3201da177e4SLinus Torvalds Opt_context = 1, 3211da177e4SLinus Torvalds Opt_fscontext = 2, 3221da177e4SLinus Torvalds Opt_defcontext = 4, 3230808925eSEric Paris Opt_rootcontext = 8, 3241da177e4SLinus Torvalds }; 3251da177e4SLinus Torvalds 3261da177e4SLinus Torvalds static match_table_t tokens = { 3271da177e4SLinus Torvalds {Opt_context, "context=%s"}, 3281da177e4SLinus Torvalds {Opt_fscontext, "fscontext=%s"}, 3291da177e4SLinus Torvalds {Opt_defcontext, "defcontext=%s"}, 3300808925eSEric Paris {Opt_rootcontext, "rootcontext=%s"}, 3311da177e4SLinus Torvalds }; 3321da177e4SLinus Torvalds 3331da177e4SLinus Torvalds #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n" 3341da177e4SLinus Torvalds 335c312feb2SEric Paris static int may_context_mount_sb_relabel(u32 sid, 336c312feb2SEric Paris struct superblock_security_struct *sbsec, 337c312feb2SEric Paris struct task_security_struct *tsec) 338c312feb2SEric Paris { 339c312feb2SEric Paris int rc; 340c312feb2SEric Paris 341c312feb2SEric Paris rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 342c312feb2SEric Paris FILESYSTEM__RELABELFROM, NULL); 343c312feb2SEric Paris if (rc) 344c312feb2SEric Paris return rc; 345c312feb2SEric Paris 346c312feb2SEric Paris rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM, 347c312feb2SEric Paris FILESYSTEM__RELABELTO, NULL); 348c312feb2SEric Paris return rc; 349c312feb2SEric Paris } 350c312feb2SEric Paris 3510808925eSEric Paris static int may_context_mount_inode_relabel(u32 sid, 3520808925eSEric Paris struct superblock_security_struct *sbsec, 3530808925eSEric Paris struct task_security_struct *tsec) 3540808925eSEric Paris { 3550808925eSEric Paris int rc; 3560808925eSEric Paris rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 3570808925eSEric Paris FILESYSTEM__RELABELFROM, NULL); 3580808925eSEric Paris if (rc) 3590808925eSEric Paris return rc; 3600808925eSEric Paris 3610808925eSEric Paris rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, 3620808925eSEric Paris FILESYSTEM__ASSOCIATE, NULL); 3630808925eSEric Paris return rc; 3640808925eSEric Paris } 3650808925eSEric Paris 3661da177e4SLinus Torvalds static int try_context_mount(struct super_block *sb, void *data) 3671da177e4SLinus Torvalds { 3681da177e4SLinus Torvalds char *context = NULL, *defcontext = NULL; 3690808925eSEric Paris char *fscontext = NULL, *rootcontext = NULL; 3701da177e4SLinus Torvalds const char *name; 3711da177e4SLinus Torvalds u32 sid; 3721da177e4SLinus Torvalds int alloc = 0, rc = 0, seen = 0; 3731da177e4SLinus Torvalds struct task_security_struct *tsec = current->security; 3741da177e4SLinus Torvalds struct superblock_security_struct *sbsec = sb->s_security; 3751da177e4SLinus Torvalds 3761da177e4SLinus Torvalds if (!data) 3771da177e4SLinus Torvalds goto out; 3781da177e4SLinus Torvalds 3791da177e4SLinus Torvalds name = sb->s_type->name; 3801da177e4SLinus Torvalds 3811da177e4SLinus Torvalds if (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA) { 3821da177e4SLinus Torvalds 3831da177e4SLinus Torvalds /* NFS we understand. */ 3841da177e4SLinus Torvalds if (!strcmp(name, "nfs")) { 3851da177e4SLinus Torvalds struct nfs_mount_data *d = data; 3861da177e4SLinus Torvalds 3871da177e4SLinus Torvalds if (d->version < NFS_MOUNT_VERSION) 3881da177e4SLinus Torvalds goto out; 3891da177e4SLinus Torvalds 3901da177e4SLinus Torvalds if (d->context[0]) { 3911da177e4SLinus Torvalds context = d->context; 3921da177e4SLinus Torvalds seen |= Opt_context; 3931da177e4SLinus Torvalds } 3941da177e4SLinus Torvalds } else 3951da177e4SLinus Torvalds goto out; 3961da177e4SLinus Torvalds 3971da177e4SLinus Torvalds } else { 3981da177e4SLinus Torvalds /* Standard string-based options. */ 3991da177e4SLinus Torvalds char *p, *options = data; 4001da177e4SLinus Torvalds 4013528a953SCory Olmo while ((p = strsep(&options, "|")) != NULL) { 4021da177e4SLinus Torvalds int token; 4031da177e4SLinus Torvalds substring_t args[MAX_OPT_ARGS]; 4041da177e4SLinus Torvalds 4051da177e4SLinus Torvalds if (!*p) 4061da177e4SLinus Torvalds continue; 4071da177e4SLinus Torvalds 4081da177e4SLinus Torvalds token = match_token(p, tokens, args); 4091da177e4SLinus Torvalds 4101da177e4SLinus Torvalds switch (token) { 4111da177e4SLinus Torvalds case Opt_context: 412c312feb2SEric Paris if (seen & (Opt_context|Opt_defcontext)) { 4131da177e4SLinus Torvalds rc = -EINVAL; 4141da177e4SLinus Torvalds printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 4151da177e4SLinus Torvalds goto out_free; 4161da177e4SLinus Torvalds } 4171da177e4SLinus Torvalds context = match_strdup(&args[0]); 4181da177e4SLinus Torvalds if (!context) { 4191da177e4SLinus Torvalds rc = -ENOMEM; 4201da177e4SLinus Torvalds goto out_free; 4211da177e4SLinus Torvalds } 4221da177e4SLinus Torvalds if (!alloc) 4231da177e4SLinus Torvalds alloc = 1; 4241da177e4SLinus Torvalds seen |= Opt_context; 4251da177e4SLinus Torvalds break; 4261da177e4SLinus Torvalds 4271da177e4SLinus Torvalds case Opt_fscontext: 428c312feb2SEric Paris if (seen & Opt_fscontext) { 4291da177e4SLinus Torvalds rc = -EINVAL; 4301da177e4SLinus Torvalds printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 4311da177e4SLinus Torvalds goto out_free; 4321da177e4SLinus Torvalds } 433c312feb2SEric Paris fscontext = match_strdup(&args[0]); 434c312feb2SEric Paris if (!fscontext) { 4351da177e4SLinus Torvalds rc = -ENOMEM; 4361da177e4SLinus Torvalds goto out_free; 4371da177e4SLinus Torvalds } 4381da177e4SLinus Torvalds if (!alloc) 4391da177e4SLinus Torvalds alloc = 1; 4401da177e4SLinus Torvalds seen |= Opt_fscontext; 4411da177e4SLinus Torvalds break; 4421da177e4SLinus Torvalds 4430808925eSEric Paris case Opt_rootcontext: 4440808925eSEric Paris if (seen & Opt_rootcontext) { 4450808925eSEric Paris rc = -EINVAL; 4460808925eSEric Paris printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 4470808925eSEric Paris goto out_free; 4480808925eSEric Paris } 4490808925eSEric Paris rootcontext = match_strdup(&args[0]); 4500808925eSEric Paris if (!rootcontext) { 4510808925eSEric Paris rc = -ENOMEM; 4520808925eSEric Paris goto out_free; 4530808925eSEric Paris } 4540808925eSEric Paris if (!alloc) 4550808925eSEric Paris alloc = 1; 4560808925eSEric Paris seen |= Opt_rootcontext; 4570808925eSEric Paris break; 4580808925eSEric Paris 4591da177e4SLinus Torvalds case Opt_defcontext: 4601da177e4SLinus Torvalds if (sbsec->behavior != SECURITY_FS_USE_XATTR) { 4611da177e4SLinus Torvalds rc = -EINVAL; 4621da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: " 4631da177e4SLinus Torvalds "defcontext option is invalid " 4641da177e4SLinus Torvalds "for this filesystem type\n"); 4651da177e4SLinus Torvalds goto out_free; 4661da177e4SLinus Torvalds } 4671da177e4SLinus Torvalds if (seen & (Opt_context|Opt_defcontext)) { 4681da177e4SLinus Torvalds rc = -EINVAL; 4691da177e4SLinus Torvalds printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 4701da177e4SLinus Torvalds goto out_free; 4711da177e4SLinus Torvalds } 4721da177e4SLinus Torvalds defcontext = match_strdup(&args[0]); 4731da177e4SLinus Torvalds if (!defcontext) { 4741da177e4SLinus Torvalds rc = -ENOMEM; 4751da177e4SLinus Torvalds goto out_free; 4761da177e4SLinus Torvalds } 4771da177e4SLinus Torvalds if (!alloc) 4781da177e4SLinus Torvalds alloc = 1; 4791da177e4SLinus Torvalds seen |= Opt_defcontext; 4801da177e4SLinus Torvalds break; 4811da177e4SLinus Torvalds 4821da177e4SLinus Torvalds default: 4831da177e4SLinus Torvalds rc = -EINVAL; 4841da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: unknown mount " 4851da177e4SLinus Torvalds "option\n"); 4861da177e4SLinus Torvalds goto out_free; 4871da177e4SLinus Torvalds 4881da177e4SLinus Torvalds } 4891da177e4SLinus Torvalds } 4901da177e4SLinus Torvalds } 4911da177e4SLinus Torvalds 4921da177e4SLinus Torvalds if (!seen) 4931da177e4SLinus Torvalds goto out; 4941da177e4SLinus Torvalds 495c312feb2SEric Paris /* sets the context of the superblock for the fs being mounted. */ 496c312feb2SEric Paris if (fscontext) { 497c312feb2SEric Paris rc = security_context_to_sid(fscontext, strlen(fscontext), &sid); 498c312feb2SEric Paris if (rc) { 499c312feb2SEric Paris printk(KERN_WARNING "SELinux: security_context_to_sid" 500c312feb2SEric Paris "(%s) failed for (dev %s, type %s) errno=%d\n", 501c312feb2SEric Paris fscontext, sb->s_id, name, rc); 502c312feb2SEric Paris goto out_free; 503c312feb2SEric Paris } 504c312feb2SEric Paris 505c312feb2SEric Paris rc = may_context_mount_sb_relabel(sid, sbsec, tsec); 506c312feb2SEric Paris if (rc) 507c312feb2SEric Paris goto out_free; 508c312feb2SEric Paris 509c312feb2SEric Paris sbsec->sid = sid; 510c312feb2SEric Paris } 511c312feb2SEric Paris 512c312feb2SEric Paris /* 513c312feb2SEric Paris * Switch to using mount point labeling behavior. 514c312feb2SEric Paris * sets the label used on all file below the mountpoint, and will set 515c312feb2SEric Paris * the superblock context if not already set. 516c312feb2SEric Paris */ 5171da177e4SLinus Torvalds if (context) { 5181da177e4SLinus Torvalds rc = security_context_to_sid(context, strlen(context), &sid); 5191da177e4SLinus Torvalds if (rc) { 5201da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: security_context_to_sid" 5211da177e4SLinus Torvalds "(%s) failed for (dev %s, type %s) errno=%d\n", 5221da177e4SLinus Torvalds context, sb->s_id, name, rc); 5231da177e4SLinus Torvalds goto out_free; 5241da177e4SLinus Torvalds } 5251da177e4SLinus Torvalds 526b04ea3ceSEric Paris if (!fscontext) { 527c312feb2SEric Paris rc = may_context_mount_sb_relabel(sid, sbsec, tsec); 5281da177e4SLinus Torvalds if (rc) 5291da177e4SLinus Torvalds goto out_free; 5301da177e4SLinus Torvalds sbsec->sid = sid; 531b04ea3ceSEric Paris } else { 532b04ea3ceSEric Paris rc = may_context_mount_inode_relabel(sid, sbsec, tsec); 533b04ea3ceSEric Paris if (rc) 534b04ea3ceSEric Paris goto out_free; 535b04ea3ceSEric Paris } 536c312feb2SEric Paris sbsec->mntpoint_sid = sid; 5371da177e4SLinus Torvalds 5381da177e4SLinus Torvalds sbsec->behavior = SECURITY_FS_USE_MNTPOINT; 5391da177e4SLinus Torvalds } 5401da177e4SLinus Torvalds 5410808925eSEric Paris if (rootcontext) { 5420808925eSEric Paris struct inode *inode = sb->s_root->d_inode; 5430808925eSEric Paris struct inode_security_struct *isec = inode->i_security; 5440808925eSEric Paris rc = security_context_to_sid(rootcontext, strlen(rootcontext), &sid); 5450808925eSEric Paris if (rc) { 5460808925eSEric Paris printk(KERN_WARNING "SELinux: security_context_to_sid" 5470808925eSEric Paris "(%s) failed for (dev %s, type %s) errno=%d\n", 5480808925eSEric Paris rootcontext, sb->s_id, name, rc); 5490808925eSEric Paris goto out_free; 5500808925eSEric Paris } 5510808925eSEric Paris 5520808925eSEric Paris rc = may_context_mount_inode_relabel(sid, sbsec, tsec); 5530808925eSEric Paris if (rc) 5540808925eSEric Paris goto out_free; 5550808925eSEric Paris 5560808925eSEric Paris isec->sid = sid; 5570808925eSEric Paris isec->initialized = 1; 5580808925eSEric Paris } 5590808925eSEric Paris 5601da177e4SLinus Torvalds if (defcontext) { 5611da177e4SLinus Torvalds rc = security_context_to_sid(defcontext, strlen(defcontext), &sid); 5621da177e4SLinus Torvalds if (rc) { 5631da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: security_context_to_sid" 5641da177e4SLinus Torvalds "(%s) failed for (dev %s, type %s) errno=%d\n", 5651da177e4SLinus Torvalds defcontext, sb->s_id, name, rc); 5661da177e4SLinus Torvalds goto out_free; 5671da177e4SLinus Torvalds } 5681da177e4SLinus Torvalds 5691da177e4SLinus Torvalds if (sid == sbsec->def_sid) 5701da177e4SLinus Torvalds goto out_free; 5711da177e4SLinus Torvalds 5720808925eSEric Paris rc = may_context_mount_inode_relabel(sid, sbsec, tsec); 5731da177e4SLinus Torvalds if (rc) 5741da177e4SLinus Torvalds goto out_free; 5751da177e4SLinus Torvalds 5761da177e4SLinus Torvalds sbsec->def_sid = sid; 5771da177e4SLinus Torvalds } 5781da177e4SLinus Torvalds 5791da177e4SLinus Torvalds out_free: 5801da177e4SLinus Torvalds if (alloc) { 5811da177e4SLinus Torvalds kfree(context); 5821da177e4SLinus Torvalds kfree(defcontext); 583c312feb2SEric Paris kfree(fscontext); 5840808925eSEric Paris kfree(rootcontext); 5851da177e4SLinus Torvalds } 5861da177e4SLinus Torvalds out: 5871da177e4SLinus Torvalds return rc; 5881da177e4SLinus Torvalds } 5891da177e4SLinus Torvalds 5901da177e4SLinus Torvalds static int superblock_doinit(struct super_block *sb, void *data) 5911da177e4SLinus Torvalds { 5921da177e4SLinus Torvalds struct superblock_security_struct *sbsec = sb->s_security; 5931da177e4SLinus Torvalds struct dentry *root = sb->s_root; 5941da177e4SLinus Torvalds struct inode *inode = root->d_inode; 5951da177e4SLinus Torvalds int rc = 0; 5961da177e4SLinus Torvalds 597bc7e982bSEric Paris mutex_lock(&sbsec->lock); 5981da177e4SLinus Torvalds if (sbsec->initialized) 5991da177e4SLinus Torvalds goto out; 6001da177e4SLinus Torvalds 6011da177e4SLinus Torvalds if (!ss_initialized) { 6021da177e4SLinus Torvalds /* Defer initialization until selinux_complete_init, 6031da177e4SLinus Torvalds after the initial policy is loaded and the security 6041da177e4SLinus Torvalds server is ready to handle calls. */ 6051da177e4SLinus Torvalds spin_lock(&sb_security_lock); 6061da177e4SLinus Torvalds if (list_empty(&sbsec->list)) 6071da177e4SLinus Torvalds list_add(&sbsec->list, &superblock_security_head); 6081da177e4SLinus Torvalds spin_unlock(&sb_security_lock); 6091da177e4SLinus Torvalds goto out; 6101da177e4SLinus Torvalds } 6111da177e4SLinus Torvalds 6121da177e4SLinus Torvalds /* Determine the labeling behavior to use for this filesystem type. */ 6131da177e4SLinus Torvalds rc = security_fs_use(sb->s_type->name, &sbsec->behavior, &sbsec->sid); 6141da177e4SLinus Torvalds if (rc) { 6151da177e4SLinus Torvalds printk(KERN_WARNING "%s: security_fs_use(%s) returned %d\n", 6161da177e4SLinus Torvalds __FUNCTION__, sb->s_type->name, rc); 6171da177e4SLinus Torvalds goto out; 6181da177e4SLinus Torvalds } 6191da177e4SLinus Torvalds 6201da177e4SLinus Torvalds rc = try_context_mount(sb, data); 6211da177e4SLinus Torvalds if (rc) 6221da177e4SLinus Torvalds goto out; 6231da177e4SLinus Torvalds 6241da177e4SLinus Torvalds if (sbsec->behavior == SECURITY_FS_USE_XATTR) { 6251da177e4SLinus Torvalds /* Make sure that the xattr handler exists and that no 6261da177e4SLinus Torvalds error other than -ENODATA is returned by getxattr on 6271da177e4SLinus Torvalds the root directory. -ENODATA is ok, as this may be 6281da177e4SLinus Torvalds the first boot of the SELinux kernel before we have 6291da177e4SLinus Torvalds assigned xattr values to the filesystem. */ 6301da177e4SLinus Torvalds if (!inode->i_op->getxattr) { 6311da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: (dev %s, type %s) has no " 6321da177e4SLinus Torvalds "xattr support\n", sb->s_id, sb->s_type->name); 6331da177e4SLinus Torvalds rc = -EOPNOTSUPP; 6341da177e4SLinus Torvalds goto out; 6351da177e4SLinus Torvalds } 6361da177e4SLinus Torvalds rc = inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0); 6371da177e4SLinus Torvalds if (rc < 0 && rc != -ENODATA) { 6381da177e4SLinus Torvalds if (rc == -EOPNOTSUPP) 6391da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: (dev %s, type " 6401da177e4SLinus Torvalds "%s) has no security xattr handler\n", 6411da177e4SLinus Torvalds sb->s_id, sb->s_type->name); 6421da177e4SLinus Torvalds else 6431da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: (dev %s, type " 6441da177e4SLinus Torvalds "%s) getxattr errno %d\n", sb->s_id, 6451da177e4SLinus Torvalds sb->s_type->name, -rc); 6461da177e4SLinus Torvalds goto out; 6471da177e4SLinus Torvalds } 6481da177e4SLinus Torvalds } 6491da177e4SLinus Torvalds 6501da177e4SLinus Torvalds if (strcmp(sb->s_type->name, "proc") == 0) 6511da177e4SLinus Torvalds sbsec->proc = 1; 6521da177e4SLinus Torvalds 6531da177e4SLinus Torvalds sbsec->initialized = 1; 6541da177e4SLinus Torvalds 6551da177e4SLinus Torvalds if (sbsec->behavior > ARRAY_SIZE(labeling_behaviors)) { 6561da177e4SLinus Torvalds printk(KERN_INFO "SELinux: initialized (dev %s, type %s), unknown behavior\n", 6571da177e4SLinus Torvalds sb->s_id, sb->s_type->name); 6581da177e4SLinus Torvalds } 6591da177e4SLinus Torvalds else { 6601da177e4SLinus Torvalds printk(KERN_INFO "SELinux: initialized (dev %s, type %s), %s\n", 6611da177e4SLinus Torvalds sb->s_id, sb->s_type->name, 6621da177e4SLinus Torvalds labeling_behaviors[sbsec->behavior-1]); 6631da177e4SLinus Torvalds } 6641da177e4SLinus Torvalds 6651da177e4SLinus Torvalds /* Initialize the root inode. */ 6661da177e4SLinus Torvalds rc = inode_doinit_with_dentry(sb->s_root->d_inode, sb->s_root); 6671da177e4SLinus Torvalds 6681da177e4SLinus Torvalds /* Initialize any other inodes associated with the superblock, e.g. 6691da177e4SLinus Torvalds inodes created prior to initial policy load or inodes created 6701da177e4SLinus Torvalds during get_sb by a pseudo filesystem that directly 6711da177e4SLinus Torvalds populates itself. */ 6721da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 6731da177e4SLinus Torvalds next_inode: 6741da177e4SLinus Torvalds if (!list_empty(&sbsec->isec_head)) { 6751da177e4SLinus Torvalds struct inode_security_struct *isec = 6761da177e4SLinus Torvalds list_entry(sbsec->isec_head.next, 6771da177e4SLinus Torvalds struct inode_security_struct, list); 6781da177e4SLinus Torvalds struct inode *inode = isec->inode; 6791da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 6801da177e4SLinus Torvalds inode = igrab(inode); 6811da177e4SLinus Torvalds if (inode) { 6821da177e4SLinus Torvalds if (!IS_PRIVATE (inode)) 6831da177e4SLinus Torvalds inode_doinit(inode); 6841da177e4SLinus Torvalds iput(inode); 6851da177e4SLinus Torvalds } 6861da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 6871da177e4SLinus Torvalds list_del_init(&isec->list); 6881da177e4SLinus Torvalds goto next_inode; 6891da177e4SLinus Torvalds } 6901da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 6911da177e4SLinus Torvalds out: 692bc7e982bSEric Paris mutex_unlock(&sbsec->lock); 6931da177e4SLinus Torvalds return rc; 6941da177e4SLinus Torvalds } 6951da177e4SLinus Torvalds 6961da177e4SLinus Torvalds static inline u16 inode_mode_to_security_class(umode_t mode) 6971da177e4SLinus Torvalds { 6981da177e4SLinus Torvalds switch (mode & S_IFMT) { 6991da177e4SLinus Torvalds case S_IFSOCK: 7001da177e4SLinus Torvalds return SECCLASS_SOCK_FILE; 7011da177e4SLinus Torvalds case S_IFLNK: 7021da177e4SLinus Torvalds return SECCLASS_LNK_FILE; 7031da177e4SLinus Torvalds case S_IFREG: 7041da177e4SLinus Torvalds return SECCLASS_FILE; 7051da177e4SLinus Torvalds case S_IFBLK: 7061da177e4SLinus Torvalds return SECCLASS_BLK_FILE; 7071da177e4SLinus Torvalds case S_IFDIR: 7081da177e4SLinus Torvalds return SECCLASS_DIR; 7091da177e4SLinus Torvalds case S_IFCHR: 7101da177e4SLinus Torvalds return SECCLASS_CHR_FILE; 7111da177e4SLinus Torvalds case S_IFIFO: 7121da177e4SLinus Torvalds return SECCLASS_FIFO_FILE; 7131da177e4SLinus Torvalds 7141da177e4SLinus Torvalds } 7151da177e4SLinus Torvalds 7161da177e4SLinus Torvalds return SECCLASS_FILE; 7171da177e4SLinus Torvalds } 7181da177e4SLinus Torvalds 71913402580SJames Morris static inline int default_protocol_stream(int protocol) 72013402580SJames Morris { 72113402580SJames Morris return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP); 72213402580SJames Morris } 72313402580SJames Morris 72413402580SJames Morris static inline int default_protocol_dgram(int protocol) 72513402580SJames Morris { 72613402580SJames Morris return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP); 72713402580SJames Morris } 72813402580SJames Morris 7291da177e4SLinus Torvalds static inline u16 socket_type_to_security_class(int family, int type, int protocol) 7301da177e4SLinus Torvalds { 7311da177e4SLinus Torvalds switch (family) { 7321da177e4SLinus Torvalds case PF_UNIX: 7331da177e4SLinus Torvalds switch (type) { 7341da177e4SLinus Torvalds case SOCK_STREAM: 7351da177e4SLinus Torvalds case SOCK_SEQPACKET: 7361da177e4SLinus Torvalds return SECCLASS_UNIX_STREAM_SOCKET; 7371da177e4SLinus Torvalds case SOCK_DGRAM: 7381da177e4SLinus Torvalds return SECCLASS_UNIX_DGRAM_SOCKET; 7391da177e4SLinus Torvalds } 7401da177e4SLinus Torvalds break; 7411da177e4SLinus Torvalds case PF_INET: 7421da177e4SLinus Torvalds case PF_INET6: 7431da177e4SLinus Torvalds switch (type) { 7441da177e4SLinus Torvalds case SOCK_STREAM: 74513402580SJames Morris if (default_protocol_stream(protocol)) 7461da177e4SLinus Torvalds return SECCLASS_TCP_SOCKET; 74713402580SJames Morris else 74813402580SJames Morris return SECCLASS_RAWIP_SOCKET; 7491da177e4SLinus Torvalds case SOCK_DGRAM: 75013402580SJames Morris if (default_protocol_dgram(protocol)) 7511da177e4SLinus Torvalds return SECCLASS_UDP_SOCKET; 75213402580SJames Morris else 75313402580SJames Morris return SECCLASS_RAWIP_SOCKET; 7542ee92d46SJames Morris case SOCK_DCCP: 7552ee92d46SJames Morris return SECCLASS_DCCP_SOCKET; 75613402580SJames Morris default: 7571da177e4SLinus Torvalds return SECCLASS_RAWIP_SOCKET; 7581da177e4SLinus Torvalds } 7591da177e4SLinus Torvalds break; 7601da177e4SLinus Torvalds case PF_NETLINK: 7611da177e4SLinus Torvalds switch (protocol) { 7621da177e4SLinus Torvalds case NETLINK_ROUTE: 7631da177e4SLinus Torvalds return SECCLASS_NETLINK_ROUTE_SOCKET; 7641da177e4SLinus Torvalds case NETLINK_FIREWALL: 7651da177e4SLinus Torvalds return SECCLASS_NETLINK_FIREWALL_SOCKET; 766216efaaaSJames Morris case NETLINK_INET_DIAG: 7671da177e4SLinus Torvalds return SECCLASS_NETLINK_TCPDIAG_SOCKET; 7681da177e4SLinus Torvalds case NETLINK_NFLOG: 7691da177e4SLinus Torvalds return SECCLASS_NETLINK_NFLOG_SOCKET; 7701da177e4SLinus Torvalds case NETLINK_XFRM: 7711da177e4SLinus Torvalds return SECCLASS_NETLINK_XFRM_SOCKET; 7721da177e4SLinus Torvalds case NETLINK_SELINUX: 7731da177e4SLinus Torvalds return SECCLASS_NETLINK_SELINUX_SOCKET; 7741da177e4SLinus Torvalds case NETLINK_AUDIT: 7751da177e4SLinus Torvalds return SECCLASS_NETLINK_AUDIT_SOCKET; 7761da177e4SLinus Torvalds case NETLINK_IP6_FW: 7771da177e4SLinus Torvalds return SECCLASS_NETLINK_IP6FW_SOCKET; 7781da177e4SLinus Torvalds case NETLINK_DNRTMSG: 7791da177e4SLinus Torvalds return SECCLASS_NETLINK_DNRT_SOCKET; 7800c9b7942SJames Morris case NETLINK_KOBJECT_UEVENT: 7810c9b7942SJames Morris return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET; 7821da177e4SLinus Torvalds default: 7831da177e4SLinus Torvalds return SECCLASS_NETLINK_SOCKET; 7841da177e4SLinus Torvalds } 7851da177e4SLinus Torvalds case PF_PACKET: 7861da177e4SLinus Torvalds return SECCLASS_PACKET_SOCKET; 7871da177e4SLinus Torvalds case PF_KEY: 7881da177e4SLinus Torvalds return SECCLASS_KEY_SOCKET; 7893e3ff15eSChristopher J. PeBenito case PF_APPLETALK: 7903e3ff15eSChristopher J. PeBenito return SECCLASS_APPLETALK_SOCKET; 7911da177e4SLinus Torvalds } 7921da177e4SLinus Torvalds 7931da177e4SLinus Torvalds return SECCLASS_SOCKET; 7941da177e4SLinus Torvalds } 7951da177e4SLinus Torvalds 7961da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 7971da177e4SLinus Torvalds static int selinux_proc_get_sid(struct proc_dir_entry *de, 7981da177e4SLinus Torvalds u16 tclass, 7991da177e4SLinus Torvalds u32 *sid) 8001da177e4SLinus Torvalds { 8011da177e4SLinus Torvalds int buflen, rc; 8021da177e4SLinus Torvalds char *buffer, *path, *end; 8031da177e4SLinus Torvalds 8041da177e4SLinus Torvalds buffer = (char*)__get_free_page(GFP_KERNEL); 8051da177e4SLinus Torvalds if (!buffer) 8061da177e4SLinus Torvalds return -ENOMEM; 8071da177e4SLinus Torvalds 8081da177e4SLinus Torvalds buflen = PAGE_SIZE; 8091da177e4SLinus Torvalds end = buffer+buflen; 8101da177e4SLinus Torvalds *--end = '\0'; 8111da177e4SLinus Torvalds buflen--; 8121da177e4SLinus Torvalds path = end-1; 8131da177e4SLinus Torvalds *path = '/'; 8141da177e4SLinus Torvalds while (de && de != de->parent) { 8151da177e4SLinus Torvalds buflen -= de->namelen + 1; 8161da177e4SLinus Torvalds if (buflen < 0) 8171da177e4SLinus Torvalds break; 8181da177e4SLinus Torvalds end -= de->namelen; 8191da177e4SLinus Torvalds memcpy(end, de->name, de->namelen); 8201da177e4SLinus Torvalds *--end = '/'; 8211da177e4SLinus Torvalds path = end; 8221da177e4SLinus Torvalds de = de->parent; 8231da177e4SLinus Torvalds } 8241da177e4SLinus Torvalds rc = security_genfs_sid("proc", path, tclass, sid); 8251da177e4SLinus Torvalds free_page((unsigned long)buffer); 8261da177e4SLinus Torvalds return rc; 8271da177e4SLinus Torvalds } 8281da177e4SLinus Torvalds #else 8291da177e4SLinus Torvalds static int selinux_proc_get_sid(struct proc_dir_entry *de, 8301da177e4SLinus Torvalds u16 tclass, 8311da177e4SLinus Torvalds u32 *sid) 8321da177e4SLinus Torvalds { 8331da177e4SLinus Torvalds return -EINVAL; 8341da177e4SLinus Torvalds } 8351da177e4SLinus Torvalds #endif 8361da177e4SLinus Torvalds 8371da177e4SLinus Torvalds /* The inode's security attributes must be initialized before first use. */ 8381da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry) 8391da177e4SLinus Torvalds { 8401da177e4SLinus Torvalds struct superblock_security_struct *sbsec = NULL; 8411da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 8421da177e4SLinus Torvalds u32 sid; 8431da177e4SLinus Torvalds struct dentry *dentry; 8441da177e4SLinus Torvalds #define INITCONTEXTLEN 255 8451da177e4SLinus Torvalds char *context = NULL; 8461da177e4SLinus Torvalds unsigned len = 0; 8471da177e4SLinus Torvalds int rc = 0; 8481da177e4SLinus Torvalds 8491da177e4SLinus Torvalds if (isec->initialized) 8501da177e4SLinus Torvalds goto out; 8511da177e4SLinus Torvalds 85223970741SEric Paris mutex_lock(&isec->lock); 8531da177e4SLinus Torvalds if (isec->initialized) 85423970741SEric Paris goto out_unlock; 8551da177e4SLinus Torvalds 8561da177e4SLinus Torvalds sbsec = inode->i_sb->s_security; 8571da177e4SLinus Torvalds if (!sbsec->initialized) { 8581da177e4SLinus Torvalds /* Defer initialization until selinux_complete_init, 8591da177e4SLinus Torvalds after the initial policy is loaded and the security 8601da177e4SLinus Torvalds server is ready to handle calls. */ 8611da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 8621da177e4SLinus Torvalds if (list_empty(&isec->list)) 8631da177e4SLinus Torvalds list_add(&isec->list, &sbsec->isec_head); 8641da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 86523970741SEric Paris goto out_unlock; 8661da177e4SLinus Torvalds } 8671da177e4SLinus Torvalds 8681da177e4SLinus Torvalds switch (sbsec->behavior) { 8691da177e4SLinus Torvalds case SECURITY_FS_USE_XATTR: 8701da177e4SLinus Torvalds if (!inode->i_op->getxattr) { 8711da177e4SLinus Torvalds isec->sid = sbsec->def_sid; 8721da177e4SLinus Torvalds break; 8731da177e4SLinus Torvalds } 8741da177e4SLinus Torvalds 8751da177e4SLinus Torvalds /* Need a dentry, since the xattr API requires one. 8761da177e4SLinus Torvalds Life would be simpler if we could just pass the inode. */ 8771da177e4SLinus Torvalds if (opt_dentry) { 8781da177e4SLinus Torvalds /* Called from d_instantiate or d_splice_alias. */ 8791da177e4SLinus Torvalds dentry = dget(opt_dentry); 8801da177e4SLinus Torvalds } else { 8811da177e4SLinus Torvalds /* Called from selinux_complete_init, try to find a dentry. */ 8821da177e4SLinus Torvalds dentry = d_find_alias(inode); 8831da177e4SLinus Torvalds } 8841da177e4SLinus Torvalds if (!dentry) { 8851da177e4SLinus Torvalds printk(KERN_WARNING "%s: no dentry for dev=%s " 8861da177e4SLinus Torvalds "ino=%ld\n", __FUNCTION__, inode->i_sb->s_id, 8871da177e4SLinus Torvalds inode->i_ino); 88823970741SEric Paris goto out_unlock; 8891da177e4SLinus Torvalds } 8901da177e4SLinus Torvalds 8911da177e4SLinus Torvalds len = INITCONTEXTLEN; 8921da177e4SLinus Torvalds context = kmalloc(len, GFP_KERNEL); 8931da177e4SLinus Torvalds if (!context) { 8941da177e4SLinus Torvalds rc = -ENOMEM; 8951da177e4SLinus Torvalds dput(dentry); 89623970741SEric Paris goto out_unlock; 8971da177e4SLinus Torvalds } 8981da177e4SLinus Torvalds rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX, 8991da177e4SLinus Torvalds context, len); 9001da177e4SLinus Torvalds if (rc == -ERANGE) { 9011da177e4SLinus Torvalds /* Need a larger buffer. Query for the right size. */ 9021da177e4SLinus Torvalds rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX, 9031da177e4SLinus Torvalds NULL, 0); 9041da177e4SLinus Torvalds if (rc < 0) { 9051da177e4SLinus Torvalds dput(dentry); 90623970741SEric Paris goto out_unlock; 9071da177e4SLinus Torvalds } 9081da177e4SLinus Torvalds kfree(context); 9091da177e4SLinus Torvalds len = rc; 9101da177e4SLinus Torvalds context = kmalloc(len, GFP_KERNEL); 9111da177e4SLinus Torvalds if (!context) { 9121da177e4SLinus Torvalds rc = -ENOMEM; 9131da177e4SLinus Torvalds dput(dentry); 91423970741SEric Paris goto out_unlock; 9151da177e4SLinus Torvalds } 9161da177e4SLinus Torvalds rc = inode->i_op->getxattr(dentry, 9171da177e4SLinus Torvalds XATTR_NAME_SELINUX, 9181da177e4SLinus Torvalds context, len); 9191da177e4SLinus Torvalds } 9201da177e4SLinus Torvalds dput(dentry); 9211da177e4SLinus Torvalds if (rc < 0) { 9221da177e4SLinus Torvalds if (rc != -ENODATA) { 9231da177e4SLinus Torvalds printk(KERN_WARNING "%s: getxattr returned " 9241da177e4SLinus Torvalds "%d for dev=%s ino=%ld\n", __FUNCTION__, 9251da177e4SLinus Torvalds -rc, inode->i_sb->s_id, inode->i_ino); 9261da177e4SLinus Torvalds kfree(context); 92723970741SEric Paris goto out_unlock; 9281da177e4SLinus Torvalds } 9291da177e4SLinus Torvalds /* Map ENODATA to the default file SID */ 9301da177e4SLinus Torvalds sid = sbsec->def_sid; 9311da177e4SLinus Torvalds rc = 0; 9321da177e4SLinus Torvalds } else { 933f5c1d5b2SJames Morris rc = security_context_to_sid_default(context, rc, &sid, 934f5c1d5b2SJames Morris sbsec->def_sid); 9351da177e4SLinus Torvalds if (rc) { 9361da177e4SLinus Torvalds printk(KERN_WARNING "%s: context_to_sid(%s) " 9371da177e4SLinus Torvalds "returned %d for dev=%s ino=%ld\n", 9381da177e4SLinus Torvalds __FUNCTION__, context, -rc, 9391da177e4SLinus Torvalds inode->i_sb->s_id, inode->i_ino); 9401da177e4SLinus Torvalds kfree(context); 9411da177e4SLinus Torvalds /* Leave with the unlabeled SID */ 9421da177e4SLinus Torvalds rc = 0; 9431da177e4SLinus Torvalds break; 9441da177e4SLinus Torvalds } 9451da177e4SLinus Torvalds } 9461da177e4SLinus Torvalds kfree(context); 9471da177e4SLinus Torvalds isec->sid = sid; 9481da177e4SLinus Torvalds break; 9491da177e4SLinus Torvalds case SECURITY_FS_USE_TASK: 9501da177e4SLinus Torvalds isec->sid = isec->task_sid; 9511da177e4SLinus Torvalds break; 9521da177e4SLinus Torvalds case SECURITY_FS_USE_TRANS: 9531da177e4SLinus Torvalds /* Default to the fs SID. */ 9541da177e4SLinus Torvalds isec->sid = sbsec->sid; 9551da177e4SLinus Torvalds 9561da177e4SLinus Torvalds /* Try to obtain a transition SID. */ 9571da177e4SLinus Torvalds isec->sclass = inode_mode_to_security_class(inode->i_mode); 9581da177e4SLinus Torvalds rc = security_transition_sid(isec->task_sid, 9591da177e4SLinus Torvalds sbsec->sid, 9601da177e4SLinus Torvalds isec->sclass, 9611da177e4SLinus Torvalds &sid); 9621da177e4SLinus Torvalds if (rc) 96323970741SEric Paris goto out_unlock; 9641da177e4SLinus Torvalds isec->sid = sid; 9651da177e4SLinus Torvalds break; 966c312feb2SEric Paris case SECURITY_FS_USE_MNTPOINT: 967c312feb2SEric Paris isec->sid = sbsec->mntpoint_sid; 968c312feb2SEric Paris break; 9691da177e4SLinus Torvalds default: 970c312feb2SEric Paris /* Default to the fs superblock SID. */ 9711da177e4SLinus Torvalds isec->sid = sbsec->sid; 9721da177e4SLinus Torvalds 9731da177e4SLinus Torvalds if (sbsec->proc) { 9741da177e4SLinus Torvalds struct proc_inode *proci = PROC_I(inode); 9751da177e4SLinus Torvalds if (proci->pde) { 9761da177e4SLinus Torvalds isec->sclass = inode_mode_to_security_class(inode->i_mode); 9771da177e4SLinus Torvalds rc = selinux_proc_get_sid(proci->pde, 9781da177e4SLinus Torvalds isec->sclass, 9791da177e4SLinus Torvalds &sid); 9801da177e4SLinus Torvalds if (rc) 98123970741SEric Paris goto out_unlock; 9821da177e4SLinus Torvalds isec->sid = sid; 9831da177e4SLinus Torvalds } 9841da177e4SLinus Torvalds } 9851da177e4SLinus Torvalds break; 9861da177e4SLinus Torvalds } 9871da177e4SLinus Torvalds 9881da177e4SLinus Torvalds isec->initialized = 1; 9891da177e4SLinus Torvalds 99023970741SEric Paris out_unlock: 99123970741SEric Paris mutex_unlock(&isec->lock); 9921da177e4SLinus Torvalds out: 9931da177e4SLinus Torvalds if (isec->sclass == SECCLASS_FILE) 9941da177e4SLinus Torvalds isec->sclass = inode_mode_to_security_class(inode->i_mode); 9951da177e4SLinus Torvalds return rc; 9961da177e4SLinus Torvalds } 9971da177e4SLinus Torvalds 9981da177e4SLinus Torvalds /* Convert a Linux signal to an access vector. */ 9991da177e4SLinus Torvalds static inline u32 signal_to_av(int sig) 10001da177e4SLinus Torvalds { 10011da177e4SLinus Torvalds u32 perm = 0; 10021da177e4SLinus Torvalds 10031da177e4SLinus Torvalds switch (sig) { 10041da177e4SLinus Torvalds case SIGCHLD: 10051da177e4SLinus Torvalds /* Commonly granted from child to parent. */ 10061da177e4SLinus Torvalds perm = PROCESS__SIGCHLD; 10071da177e4SLinus Torvalds break; 10081da177e4SLinus Torvalds case SIGKILL: 10091da177e4SLinus Torvalds /* Cannot be caught or ignored */ 10101da177e4SLinus Torvalds perm = PROCESS__SIGKILL; 10111da177e4SLinus Torvalds break; 10121da177e4SLinus Torvalds case SIGSTOP: 10131da177e4SLinus Torvalds /* Cannot be caught or ignored */ 10141da177e4SLinus Torvalds perm = PROCESS__SIGSTOP; 10151da177e4SLinus Torvalds break; 10161da177e4SLinus Torvalds default: 10171da177e4SLinus Torvalds /* All other signals. */ 10181da177e4SLinus Torvalds perm = PROCESS__SIGNAL; 10191da177e4SLinus Torvalds break; 10201da177e4SLinus Torvalds } 10211da177e4SLinus Torvalds 10221da177e4SLinus Torvalds return perm; 10231da177e4SLinus Torvalds } 10241da177e4SLinus Torvalds 10251da177e4SLinus Torvalds /* Check permission betweeen a pair of tasks, e.g. signal checks, 10261da177e4SLinus Torvalds fork check, ptrace check, etc. */ 10271da177e4SLinus Torvalds static int task_has_perm(struct task_struct *tsk1, 10281da177e4SLinus Torvalds struct task_struct *tsk2, 10291da177e4SLinus Torvalds u32 perms) 10301da177e4SLinus Torvalds { 10311da177e4SLinus Torvalds struct task_security_struct *tsec1, *tsec2; 10321da177e4SLinus Torvalds 10331da177e4SLinus Torvalds tsec1 = tsk1->security; 10341da177e4SLinus Torvalds tsec2 = tsk2->security; 10351da177e4SLinus Torvalds return avc_has_perm(tsec1->sid, tsec2->sid, 10361da177e4SLinus Torvalds SECCLASS_PROCESS, perms, NULL); 10371da177e4SLinus Torvalds } 10381da177e4SLinus Torvalds 10391da177e4SLinus Torvalds /* Check whether a task is allowed to use a capability. */ 10401da177e4SLinus Torvalds static int task_has_capability(struct task_struct *tsk, 10411da177e4SLinus Torvalds int cap) 10421da177e4SLinus Torvalds { 10431da177e4SLinus Torvalds struct task_security_struct *tsec; 10441da177e4SLinus Torvalds struct avc_audit_data ad; 10451da177e4SLinus Torvalds 10461da177e4SLinus Torvalds tsec = tsk->security; 10471da177e4SLinus Torvalds 10481da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,CAP); 10491da177e4SLinus Torvalds ad.tsk = tsk; 10501da177e4SLinus Torvalds ad.u.cap = cap; 10511da177e4SLinus Torvalds 10521da177e4SLinus Torvalds return avc_has_perm(tsec->sid, tsec->sid, 10531da177e4SLinus Torvalds SECCLASS_CAPABILITY, CAP_TO_MASK(cap), &ad); 10541da177e4SLinus Torvalds } 10551da177e4SLinus Torvalds 10561da177e4SLinus Torvalds /* Check whether a task is allowed to use a system operation. */ 10571da177e4SLinus Torvalds static int task_has_system(struct task_struct *tsk, 10581da177e4SLinus Torvalds u32 perms) 10591da177e4SLinus Torvalds { 10601da177e4SLinus Torvalds struct task_security_struct *tsec; 10611da177e4SLinus Torvalds 10621da177e4SLinus Torvalds tsec = tsk->security; 10631da177e4SLinus Torvalds 10641da177e4SLinus Torvalds return avc_has_perm(tsec->sid, SECINITSID_KERNEL, 10651da177e4SLinus Torvalds SECCLASS_SYSTEM, perms, NULL); 10661da177e4SLinus Torvalds } 10671da177e4SLinus Torvalds 10681da177e4SLinus Torvalds /* Check whether a task has a particular permission to an inode. 10691da177e4SLinus Torvalds The 'adp' parameter is optional and allows other audit 10701da177e4SLinus Torvalds data to be passed (e.g. the dentry). */ 10711da177e4SLinus Torvalds static int inode_has_perm(struct task_struct *tsk, 10721da177e4SLinus Torvalds struct inode *inode, 10731da177e4SLinus Torvalds u32 perms, 10741da177e4SLinus Torvalds struct avc_audit_data *adp) 10751da177e4SLinus Torvalds { 10761da177e4SLinus Torvalds struct task_security_struct *tsec; 10771da177e4SLinus Torvalds struct inode_security_struct *isec; 10781da177e4SLinus Torvalds struct avc_audit_data ad; 10791da177e4SLinus Torvalds 10801da177e4SLinus Torvalds tsec = tsk->security; 10811da177e4SLinus Torvalds isec = inode->i_security; 10821da177e4SLinus Torvalds 10831da177e4SLinus Torvalds if (!adp) { 10841da177e4SLinus Torvalds adp = &ad; 10851da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 10861da177e4SLinus Torvalds ad.u.fs.inode = inode; 10871da177e4SLinus Torvalds } 10881da177e4SLinus Torvalds 10891da177e4SLinus Torvalds return avc_has_perm(tsec->sid, isec->sid, isec->sclass, perms, adp); 10901da177e4SLinus Torvalds } 10911da177e4SLinus Torvalds 10921da177e4SLinus Torvalds /* Same as inode_has_perm, but pass explicit audit data containing 10931da177e4SLinus Torvalds the dentry to help the auditing code to more easily generate the 10941da177e4SLinus Torvalds pathname if needed. */ 10951da177e4SLinus Torvalds static inline int dentry_has_perm(struct task_struct *tsk, 10961da177e4SLinus Torvalds struct vfsmount *mnt, 10971da177e4SLinus Torvalds struct dentry *dentry, 10981da177e4SLinus Torvalds u32 av) 10991da177e4SLinus Torvalds { 11001da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 11011da177e4SLinus Torvalds struct avc_audit_data ad; 11021da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,FS); 11031da177e4SLinus Torvalds ad.u.fs.mnt = mnt; 11041da177e4SLinus Torvalds ad.u.fs.dentry = dentry; 11051da177e4SLinus Torvalds return inode_has_perm(tsk, inode, av, &ad); 11061da177e4SLinus Torvalds } 11071da177e4SLinus Torvalds 11081da177e4SLinus Torvalds /* Check whether a task can use an open file descriptor to 11091da177e4SLinus Torvalds access an inode in a given way. Check access to the 11101da177e4SLinus Torvalds descriptor itself, and then use dentry_has_perm to 11111da177e4SLinus Torvalds check a particular permission to the file. 11121da177e4SLinus Torvalds Access to the descriptor is implicitly granted if it 11131da177e4SLinus Torvalds has the same SID as the process. If av is zero, then 11141da177e4SLinus Torvalds access to the file is not checked, e.g. for cases 11151da177e4SLinus Torvalds where only the descriptor is affected like seek. */ 1116858119e1SArjan van de Ven static int file_has_perm(struct task_struct *tsk, 11171da177e4SLinus Torvalds struct file *file, 11181da177e4SLinus Torvalds u32 av) 11191da177e4SLinus Torvalds { 11201da177e4SLinus Torvalds struct task_security_struct *tsec = tsk->security; 11211da177e4SLinus Torvalds struct file_security_struct *fsec = file->f_security; 11223d5ff529SJosef Sipek struct vfsmount *mnt = file->f_path.mnt; 11233d5ff529SJosef Sipek struct dentry *dentry = file->f_path.dentry; 11241da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 11251da177e4SLinus Torvalds struct avc_audit_data ad; 11261da177e4SLinus Torvalds int rc; 11271da177e4SLinus Torvalds 11281da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 11291da177e4SLinus Torvalds ad.u.fs.mnt = mnt; 11301da177e4SLinus Torvalds ad.u.fs.dentry = dentry; 11311da177e4SLinus Torvalds 11321da177e4SLinus Torvalds if (tsec->sid != fsec->sid) { 11331da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, fsec->sid, 11341da177e4SLinus Torvalds SECCLASS_FD, 11351da177e4SLinus Torvalds FD__USE, 11361da177e4SLinus Torvalds &ad); 11371da177e4SLinus Torvalds if (rc) 11381da177e4SLinus Torvalds return rc; 11391da177e4SLinus Torvalds } 11401da177e4SLinus Torvalds 11411da177e4SLinus Torvalds /* av is zero if only checking access to the descriptor. */ 11421da177e4SLinus Torvalds if (av) 11431da177e4SLinus Torvalds return inode_has_perm(tsk, inode, av, &ad); 11441da177e4SLinus Torvalds 11451da177e4SLinus Torvalds return 0; 11461da177e4SLinus Torvalds } 11471da177e4SLinus Torvalds 11481da177e4SLinus Torvalds /* Check whether a task can create a file. */ 11491da177e4SLinus Torvalds static int may_create(struct inode *dir, 11501da177e4SLinus Torvalds struct dentry *dentry, 11511da177e4SLinus Torvalds u16 tclass) 11521da177e4SLinus Torvalds { 11531da177e4SLinus Torvalds struct task_security_struct *tsec; 11541da177e4SLinus Torvalds struct inode_security_struct *dsec; 11551da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 11561da177e4SLinus Torvalds u32 newsid; 11571da177e4SLinus Torvalds struct avc_audit_data ad; 11581da177e4SLinus Torvalds int rc; 11591da177e4SLinus Torvalds 11601da177e4SLinus Torvalds tsec = current->security; 11611da177e4SLinus Torvalds dsec = dir->i_security; 11621da177e4SLinus Torvalds sbsec = dir->i_sb->s_security; 11631da177e4SLinus Torvalds 11641da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 11651da177e4SLinus Torvalds ad.u.fs.dentry = dentry; 11661da177e4SLinus Torvalds 11671da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, dsec->sid, SECCLASS_DIR, 11681da177e4SLinus Torvalds DIR__ADD_NAME | DIR__SEARCH, 11691da177e4SLinus Torvalds &ad); 11701da177e4SLinus Torvalds if (rc) 11711da177e4SLinus Torvalds return rc; 11721da177e4SLinus Torvalds 11731da177e4SLinus Torvalds if (tsec->create_sid && sbsec->behavior != SECURITY_FS_USE_MNTPOINT) { 11741da177e4SLinus Torvalds newsid = tsec->create_sid; 11751da177e4SLinus Torvalds } else { 11761da177e4SLinus Torvalds rc = security_transition_sid(tsec->sid, dsec->sid, tclass, 11771da177e4SLinus Torvalds &newsid); 11781da177e4SLinus Torvalds if (rc) 11791da177e4SLinus Torvalds return rc; 11801da177e4SLinus Torvalds } 11811da177e4SLinus Torvalds 11821da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, newsid, tclass, FILE__CREATE, &ad); 11831da177e4SLinus Torvalds if (rc) 11841da177e4SLinus Torvalds return rc; 11851da177e4SLinus Torvalds 11861da177e4SLinus Torvalds return avc_has_perm(newsid, sbsec->sid, 11871da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 11881da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, &ad); 11891da177e4SLinus Torvalds } 11901da177e4SLinus Torvalds 11914eb582cfSMichael LeMay /* Check whether a task can create a key. */ 11924eb582cfSMichael LeMay static int may_create_key(u32 ksid, 11934eb582cfSMichael LeMay struct task_struct *ctx) 11944eb582cfSMichael LeMay { 11954eb582cfSMichael LeMay struct task_security_struct *tsec; 11964eb582cfSMichael LeMay 11974eb582cfSMichael LeMay tsec = ctx->security; 11984eb582cfSMichael LeMay 11994eb582cfSMichael LeMay return avc_has_perm(tsec->sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL); 12004eb582cfSMichael LeMay } 12014eb582cfSMichael LeMay 12021da177e4SLinus Torvalds #define MAY_LINK 0 12031da177e4SLinus Torvalds #define MAY_UNLINK 1 12041da177e4SLinus Torvalds #define MAY_RMDIR 2 12051da177e4SLinus Torvalds 12061da177e4SLinus Torvalds /* Check whether a task can link, unlink, or rmdir a file/directory. */ 12071da177e4SLinus Torvalds static int may_link(struct inode *dir, 12081da177e4SLinus Torvalds struct dentry *dentry, 12091da177e4SLinus Torvalds int kind) 12101da177e4SLinus Torvalds 12111da177e4SLinus Torvalds { 12121da177e4SLinus Torvalds struct task_security_struct *tsec; 12131da177e4SLinus Torvalds struct inode_security_struct *dsec, *isec; 12141da177e4SLinus Torvalds struct avc_audit_data ad; 12151da177e4SLinus Torvalds u32 av; 12161da177e4SLinus Torvalds int rc; 12171da177e4SLinus Torvalds 12181da177e4SLinus Torvalds tsec = current->security; 12191da177e4SLinus Torvalds dsec = dir->i_security; 12201da177e4SLinus Torvalds isec = dentry->d_inode->i_security; 12211da177e4SLinus Torvalds 12221da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 12231da177e4SLinus Torvalds ad.u.fs.dentry = dentry; 12241da177e4SLinus Torvalds 12251da177e4SLinus Torvalds av = DIR__SEARCH; 12261da177e4SLinus Torvalds av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME); 12271da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, dsec->sid, SECCLASS_DIR, av, &ad); 12281da177e4SLinus Torvalds if (rc) 12291da177e4SLinus Torvalds return rc; 12301da177e4SLinus Torvalds 12311da177e4SLinus Torvalds switch (kind) { 12321da177e4SLinus Torvalds case MAY_LINK: 12331da177e4SLinus Torvalds av = FILE__LINK; 12341da177e4SLinus Torvalds break; 12351da177e4SLinus Torvalds case MAY_UNLINK: 12361da177e4SLinus Torvalds av = FILE__UNLINK; 12371da177e4SLinus Torvalds break; 12381da177e4SLinus Torvalds case MAY_RMDIR: 12391da177e4SLinus Torvalds av = DIR__RMDIR; 12401da177e4SLinus Torvalds break; 12411da177e4SLinus Torvalds default: 12421da177e4SLinus Torvalds printk(KERN_WARNING "may_link: unrecognized kind %d\n", kind); 12431da177e4SLinus Torvalds return 0; 12441da177e4SLinus Torvalds } 12451da177e4SLinus Torvalds 12461da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, isec->sclass, av, &ad); 12471da177e4SLinus Torvalds return rc; 12481da177e4SLinus Torvalds } 12491da177e4SLinus Torvalds 12501da177e4SLinus Torvalds static inline int may_rename(struct inode *old_dir, 12511da177e4SLinus Torvalds struct dentry *old_dentry, 12521da177e4SLinus Torvalds struct inode *new_dir, 12531da177e4SLinus Torvalds struct dentry *new_dentry) 12541da177e4SLinus Torvalds { 12551da177e4SLinus Torvalds struct task_security_struct *tsec; 12561da177e4SLinus Torvalds struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec; 12571da177e4SLinus Torvalds struct avc_audit_data ad; 12581da177e4SLinus Torvalds u32 av; 12591da177e4SLinus Torvalds int old_is_dir, new_is_dir; 12601da177e4SLinus Torvalds int rc; 12611da177e4SLinus Torvalds 12621da177e4SLinus Torvalds tsec = current->security; 12631da177e4SLinus Torvalds old_dsec = old_dir->i_security; 12641da177e4SLinus Torvalds old_isec = old_dentry->d_inode->i_security; 12651da177e4SLinus Torvalds old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 12661da177e4SLinus Torvalds new_dsec = new_dir->i_security; 12671da177e4SLinus Torvalds 12681da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 12691da177e4SLinus Torvalds 12701da177e4SLinus Torvalds ad.u.fs.dentry = old_dentry; 12711da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, old_dsec->sid, SECCLASS_DIR, 12721da177e4SLinus Torvalds DIR__REMOVE_NAME | DIR__SEARCH, &ad); 12731da177e4SLinus Torvalds if (rc) 12741da177e4SLinus Torvalds return rc; 12751da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, old_isec->sid, 12761da177e4SLinus Torvalds old_isec->sclass, FILE__RENAME, &ad); 12771da177e4SLinus Torvalds if (rc) 12781da177e4SLinus Torvalds return rc; 12791da177e4SLinus Torvalds if (old_is_dir && new_dir != old_dir) { 12801da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, old_isec->sid, 12811da177e4SLinus Torvalds old_isec->sclass, DIR__REPARENT, &ad); 12821da177e4SLinus Torvalds if (rc) 12831da177e4SLinus Torvalds return rc; 12841da177e4SLinus Torvalds } 12851da177e4SLinus Torvalds 12861da177e4SLinus Torvalds ad.u.fs.dentry = new_dentry; 12871da177e4SLinus Torvalds av = DIR__ADD_NAME | DIR__SEARCH; 12881da177e4SLinus Torvalds if (new_dentry->d_inode) 12891da177e4SLinus Torvalds av |= DIR__REMOVE_NAME; 12901da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, new_dsec->sid, SECCLASS_DIR, av, &ad); 12911da177e4SLinus Torvalds if (rc) 12921da177e4SLinus Torvalds return rc; 12931da177e4SLinus Torvalds if (new_dentry->d_inode) { 12941da177e4SLinus Torvalds new_isec = new_dentry->d_inode->i_security; 12951da177e4SLinus Torvalds new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode); 12961da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, new_isec->sid, 12971da177e4SLinus Torvalds new_isec->sclass, 12981da177e4SLinus Torvalds (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad); 12991da177e4SLinus Torvalds if (rc) 13001da177e4SLinus Torvalds return rc; 13011da177e4SLinus Torvalds } 13021da177e4SLinus Torvalds 13031da177e4SLinus Torvalds return 0; 13041da177e4SLinus Torvalds } 13051da177e4SLinus Torvalds 13061da177e4SLinus Torvalds /* Check whether a task can perform a filesystem operation. */ 13071da177e4SLinus Torvalds static int superblock_has_perm(struct task_struct *tsk, 13081da177e4SLinus Torvalds struct super_block *sb, 13091da177e4SLinus Torvalds u32 perms, 13101da177e4SLinus Torvalds struct avc_audit_data *ad) 13111da177e4SLinus Torvalds { 13121da177e4SLinus Torvalds struct task_security_struct *tsec; 13131da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 13141da177e4SLinus Torvalds 13151da177e4SLinus Torvalds tsec = tsk->security; 13161da177e4SLinus Torvalds sbsec = sb->s_security; 13171da177e4SLinus Torvalds return avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 13181da177e4SLinus Torvalds perms, ad); 13191da177e4SLinus Torvalds } 13201da177e4SLinus Torvalds 13211da177e4SLinus Torvalds /* Convert a Linux mode and permission mask to an access vector. */ 13221da177e4SLinus Torvalds static inline u32 file_mask_to_av(int mode, int mask) 13231da177e4SLinus Torvalds { 13241da177e4SLinus Torvalds u32 av = 0; 13251da177e4SLinus Torvalds 13261da177e4SLinus Torvalds if ((mode & S_IFMT) != S_IFDIR) { 13271da177e4SLinus Torvalds if (mask & MAY_EXEC) 13281da177e4SLinus Torvalds av |= FILE__EXECUTE; 13291da177e4SLinus Torvalds if (mask & MAY_READ) 13301da177e4SLinus Torvalds av |= FILE__READ; 13311da177e4SLinus Torvalds 13321da177e4SLinus Torvalds if (mask & MAY_APPEND) 13331da177e4SLinus Torvalds av |= FILE__APPEND; 13341da177e4SLinus Torvalds else if (mask & MAY_WRITE) 13351da177e4SLinus Torvalds av |= FILE__WRITE; 13361da177e4SLinus Torvalds 13371da177e4SLinus Torvalds } else { 13381da177e4SLinus Torvalds if (mask & MAY_EXEC) 13391da177e4SLinus Torvalds av |= DIR__SEARCH; 13401da177e4SLinus Torvalds if (mask & MAY_WRITE) 13411da177e4SLinus Torvalds av |= DIR__WRITE; 13421da177e4SLinus Torvalds if (mask & MAY_READ) 13431da177e4SLinus Torvalds av |= DIR__READ; 13441da177e4SLinus Torvalds } 13451da177e4SLinus Torvalds 13461da177e4SLinus Torvalds return av; 13471da177e4SLinus Torvalds } 13481da177e4SLinus Torvalds 13491da177e4SLinus Torvalds /* Convert a Linux file to an access vector. */ 13501da177e4SLinus Torvalds static inline u32 file_to_av(struct file *file) 13511da177e4SLinus Torvalds { 13521da177e4SLinus Torvalds u32 av = 0; 13531da177e4SLinus Torvalds 13541da177e4SLinus Torvalds if (file->f_mode & FMODE_READ) 13551da177e4SLinus Torvalds av |= FILE__READ; 13561da177e4SLinus Torvalds if (file->f_mode & FMODE_WRITE) { 13571da177e4SLinus Torvalds if (file->f_flags & O_APPEND) 13581da177e4SLinus Torvalds av |= FILE__APPEND; 13591da177e4SLinus Torvalds else 13601da177e4SLinus Torvalds av |= FILE__WRITE; 13611da177e4SLinus Torvalds } 13621da177e4SLinus Torvalds 13631da177e4SLinus Torvalds return av; 13641da177e4SLinus Torvalds } 13651da177e4SLinus Torvalds 13661da177e4SLinus Torvalds /* Hook functions begin here. */ 13671da177e4SLinus Torvalds 13681da177e4SLinus Torvalds static int selinux_ptrace(struct task_struct *parent, struct task_struct *child) 13691da177e4SLinus Torvalds { 13701da177e4SLinus Torvalds struct task_security_struct *psec = parent->security; 13711da177e4SLinus Torvalds struct task_security_struct *csec = child->security; 13721da177e4SLinus Torvalds int rc; 13731da177e4SLinus Torvalds 13741da177e4SLinus Torvalds rc = secondary_ops->ptrace(parent,child); 13751da177e4SLinus Torvalds if (rc) 13761da177e4SLinus Torvalds return rc; 13771da177e4SLinus Torvalds 13781da177e4SLinus Torvalds rc = task_has_perm(parent, child, PROCESS__PTRACE); 13791da177e4SLinus Torvalds /* Save the SID of the tracing process for later use in apply_creds. */ 1380341c2d80SStephen Smalley if (!(child->ptrace & PT_PTRACED) && !rc) 13811da177e4SLinus Torvalds csec->ptrace_sid = psec->sid; 13821da177e4SLinus Torvalds return rc; 13831da177e4SLinus Torvalds } 13841da177e4SLinus Torvalds 13851da177e4SLinus Torvalds static int selinux_capget(struct task_struct *target, kernel_cap_t *effective, 13861da177e4SLinus Torvalds kernel_cap_t *inheritable, kernel_cap_t *permitted) 13871da177e4SLinus Torvalds { 13881da177e4SLinus Torvalds int error; 13891da177e4SLinus Torvalds 13901da177e4SLinus Torvalds error = task_has_perm(current, target, PROCESS__GETCAP); 13911da177e4SLinus Torvalds if (error) 13921da177e4SLinus Torvalds return error; 13931da177e4SLinus Torvalds 13941da177e4SLinus Torvalds return secondary_ops->capget(target, effective, inheritable, permitted); 13951da177e4SLinus Torvalds } 13961da177e4SLinus Torvalds 13971da177e4SLinus Torvalds static int selinux_capset_check(struct task_struct *target, kernel_cap_t *effective, 13981da177e4SLinus Torvalds kernel_cap_t *inheritable, kernel_cap_t *permitted) 13991da177e4SLinus Torvalds { 14001da177e4SLinus Torvalds int error; 14011da177e4SLinus Torvalds 14021da177e4SLinus Torvalds error = secondary_ops->capset_check(target, effective, inheritable, permitted); 14031da177e4SLinus Torvalds if (error) 14041da177e4SLinus Torvalds return error; 14051da177e4SLinus Torvalds 14061da177e4SLinus Torvalds return task_has_perm(current, target, PROCESS__SETCAP); 14071da177e4SLinus Torvalds } 14081da177e4SLinus Torvalds 14091da177e4SLinus Torvalds static void selinux_capset_set(struct task_struct *target, kernel_cap_t *effective, 14101da177e4SLinus Torvalds kernel_cap_t *inheritable, kernel_cap_t *permitted) 14111da177e4SLinus Torvalds { 14121da177e4SLinus Torvalds secondary_ops->capset_set(target, effective, inheritable, permitted); 14131da177e4SLinus Torvalds } 14141da177e4SLinus Torvalds 14151da177e4SLinus Torvalds static int selinux_capable(struct task_struct *tsk, int cap) 14161da177e4SLinus Torvalds { 14171da177e4SLinus Torvalds int rc; 14181da177e4SLinus Torvalds 14191da177e4SLinus Torvalds rc = secondary_ops->capable(tsk, cap); 14201da177e4SLinus Torvalds if (rc) 14211da177e4SLinus Torvalds return rc; 14221da177e4SLinus Torvalds 14231da177e4SLinus Torvalds return task_has_capability(tsk,cap); 14241da177e4SLinus Torvalds } 14251da177e4SLinus Torvalds 14261da177e4SLinus Torvalds static int selinux_sysctl(ctl_table *table, int op) 14271da177e4SLinus Torvalds { 14281da177e4SLinus Torvalds int error = 0; 14291da177e4SLinus Torvalds u32 av; 14301da177e4SLinus Torvalds struct task_security_struct *tsec; 14311da177e4SLinus Torvalds u32 tsid; 14321da177e4SLinus Torvalds int rc; 14331da177e4SLinus Torvalds 14341da177e4SLinus Torvalds rc = secondary_ops->sysctl(table, op); 14351da177e4SLinus Torvalds if (rc) 14361da177e4SLinus Torvalds return rc; 14371da177e4SLinus Torvalds 14381da177e4SLinus Torvalds tsec = current->security; 14391da177e4SLinus Torvalds 14401da177e4SLinus Torvalds rc = selinux_proc_get_sid(table->de, (op == 001) ? 14411da177e4SLinus Torvalds SECCLASS_DIR : SECCLASS_FILE, &tsid); 14421da177e4SLinus Torvalds if (rc) { 14431da177e4SLinus Torvalds /* Default to the well-defined sysctl SID. */ 14441da177e4SLinus Torvalds tsid = SECINITSID_SYSCTL; 14451da177e4SLinus Torvalds } 14461da177e4SLinus Torvalds 14471da177e4SLinus Torvalds /* The op values are "defined" in sysctl.c, thereby creating 14481da177e4SLinus Torvalds * a bad coupling between this module and sysctl.c */ 14491da177e4SLinus Torvalds if(op == 001) { 14501da177e4SLinus Torvalds error = avc_has_perm(tsec->sid, tsid, 14511da177e4SLinus Torvalds SECCLASS_DIR, DIR__SEARCH, NULL); 14521da177e4SLinus Torvalds } else { 14531da177e4SLinus Torvalds av = 0; 14541da177e4SLinus Torvalds if (op & 004) 14551da177e4SLinus Torvalds av |= FILE__READ; 14561da177e4SLinus Torvalds if (op & 002) 14571da177e4SLinus Torvalds av |= FILE__WRITE; 14581da177e4SLinus Torvalds if (av) 14591da177e4SLinus Torvalds error = avc_has_perm(tsec->sid, tsid, 14601da177e4SLinus Torvalds SECCLASS_FILE, av, NULL); 14611da177e4SLinus Torvalds } 14621da177e4SLinus Torvalds 14631da177e4SLinus Torvalds return error; 14641da177e4SLinus Torvalds } 14651da177e4SLinus Torvalds 14661da177e4SLinus Torvalds static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb) 14671da177e4SLinus Torvalds { 14681da177e4SLinus Torvalds int rc = 0; 14691da177e4SLinus Torvalds 14701da177e4SLinus Torvalds if (!sb) 14711da177e4SLinus Torvalds return 0; 14721da177e4SLinus Torvalds 14731da177e4SLinus Torvalds switch (cmds) { 14741da177e4SLinus Torvalds case Q_SYNC: 14751da177e4SLinus Torvalds case Q_QUOTAON: 14761da177e4SLinus Torvalds case Q_QUOTAOFF: 14771da177e4SLinus Torvalds case Q_SETINFO: 14781da177e4SLinus Torvalds case Q_SETQUOTA: 14791da177e4SLinus Torvalds rc = superblock_has_perm(current, 14801da177e4SLinus Torvalds sb, 14811da177e4SLinus Torvalds FILESYSTEM__QUOTAMOD, NULL); 14821da177e4SLinus Torvalds break; 14831da177e4SLinus Torvalds case Q_GETFMT: 14841da177e4SLinus Torvalds case Q_GETINFO: 14851da177e4SLinus Torvalds case Q_GETQUOTA: 14861da177e4SLinus Torvalds rc = superblock_has_perm(current, 14871da177e4SLinus Torvalds sb, 14881da177e4SLinus Torvalds FILESYSTEM__QUOTAGET, NULL); 14891da177e4SLinus Torvalds break; 14901da177e4SLinus Torvalds default: 14911da177e4SLinus Torvalds rc = 0; /* let the kernel handle invalid cmds */ 14921da177e4SLinus Torvalds break; 14931da177e4SLinus Torvalds } 14941da177e4SLinus Torvalds return rc; 14951da177e4SLinus Torvalds } 14961da177e4SLinus Torvalds 14971da177e4SLinus Torvalds static int selinux_quota_on(struct dentry *dentry) 14981da177e4SLinus Torvalds { 14991da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__QUOTAON); 15001da177e4SLinus Torvalds } 15011da177e4SLinus Torvalds 15021da177e4SLinus Torvalds static int selinux_syslog(int type) 15031da177e4SLinus Torvalds { 15041da177e4SLinus Torvalds int rc; 15051da177e4SLinus Torvalds 15061da177e4SLinus Torvalds rc = secondary_ops->syslog(type); 15071da177e4SLinus Torvalds if (rc) 15081da177e4SLinus Torvalds return rc; 15091da177e4SLinus Torvalds 15101da177e4SLinus Torvalds switch (type) { 15111da177e4SLinus Torvalds case 3: /* Read last kernel messages */ 15121da177e4SLinus Torvalds case 10: /* Return size of the log buffer */ 15131da177e4SLinus Torvalds rc = task_has_system(current, SYSTEM__SYSLOG_READ); 15141da177e4SLinus Torvalds break; 15151da177e4SLinus Torvalds case 6: /* Disable logging to console */ 15161da177e4SLinus Torvalds case 7: /* Enable logging to console */ 15171da177e4SLinus Torvalds case 8: /* Set level of messages printed to console */ 15181da177e4SLinus Torvalds rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE); 15191da177e4SLinus Torvalds break; 15201da177e4SLinus Torvalds case 0: /* Close log */ 15211da177e4SLinus Torvalds case 1: /* Open log */ 15221da177e4SLinus Torvalds case 2: /* Read from log */ 15231da177e4SLinus Torvalds case 4: /* Read/clear last kernel messages */ 15241da177e4SLinus Torvalds case 5: /* Clear ring buffer */ 15251da177e4SLinus Torvalds default: 15261da177e4SLinus Torvalds rc = task_has_system(current, SYSTEM__SYSLOG_MOD); 15271da177e4SLinus Torvalds break; 15281da177e4SLinus Torvalds } 15291da177e4SLinus Torvalds return rc; 15301da177e4SLinus Torvalds } 15311da177e4SLinus Torvalds 15321da177e4SLinus Torvalds /* 15331da177e4SLinus Torvalds * Check that a process has enough memory to allocate a new virtual 15341da177e4SLinus Torvalds * mapping. 0 means there is enough memory for the allocation to 15351da177e4SLinus Torvalds * succeed and -ENOMEM implies there is not. 15361da177e4SLinus Torvalds * 15371da177e4SLinus Torvalds * Note that secondary_ops->capable and task_has_perm_noaudit return 0 15381da177e4SLinus Torvalds * if the capability is granted, but __vm_enough_memory requires 1 if 15391da177e4SLinus Torvalds * the capability is granted. 15401da177e4SLinus Torvalds * 15411da177e4SLinus Torvalds * Do not audit the selinux permission check, as this is applied to all 15421da177e4SLinus Torvalds * processes that allocate mappings. 15431da177e4SLinus Torvalds */ 15441da177e4SLinus Torvalds static int selinux_vm_enough_memory(long pages) 15451da177e4SLinus Torvalds { 15461da177e4SLinus Torvalds int rc, cap_sys_admin = 0; 15471da177e4SLinus Torvalds struct task_security_struct *tsec = current->security; 15481da177e4SLinus Torvalds 15491da177e4SLinus Torvalds rc = secondary_ops->capable(current, CAP_SYS_ADMIN); 15501da177e4SLinus Torvalds if (rc == 0) 15511da177e4SLinus Torvalds rc = avc_has_perm_noaudit(tsec->sid, tsec->sid, 15521da177e4SLinus Torvalds SECCLASS_CAPABILITY, 15531da177e4SLinus Torvalds CAP_TO_MASK(CAP_SYS_ADMIN), 15541da177e4SLinus Torvalds NULL); 15551da177e4SLinus Torvalds 15561da177e4SLinus Torvalds if (rc == 0) 15571da177e4SLinus Torvalds cap_sys_admin = 1; 15581da177e4SLinus Torvalds 15591da177e4SLinus Torvalds return __vm_enough_memory(pages, cap_sys_admin); 15601da177e4SLinus Torvalds } 15611da177e4SLinus Torvalds 15621da177e4SLinus Torvalds /* binprm security operations */ 15631da177e4SLinus Torvalds 15641da177e4SLinus Torvalds static int selinux_bprm_alloc_security(struct linux_binprm *bprm) 15651da177e4SLinus Torvalds { 15661da177e4SLinus Torvalds struct bprm_security_struct *bsec; 15671da177e4SLinus Torvalds 156889d155efSJames Morris bsec = kzalloc(sizeof(struct bprm_security_struct), GFP_KERNEL); 15691da177e4SLinus Torvalds if (!bsec) 15701da177e4SLinus Torvalds return -ENOMEM; 15711da177e4SLinus Torvalds 15721da177e4SLinus Torvalds bsec->bprm = bprm; 15731da177e4SLinus Torvalds bsec->sid = SECINITSID_UNLABELED; 15741da177e4SLinus Torvalds bsec->set = 0; 15751da177e4SLinus Torvalds 15761da177e4SLinus Torvalds bprm->security = bsec; 15771da177e4SLinus Torvalds return 0; 15781da177e4SLinus Torvalds } 15791da177e4SLinus Torvalds 15801da177e4SLinus Torvalds static int selinux_bprm_set_security(struct linux_binprm *bprm) 15811da177e4SLinus Torvalds { 15821da177e4SLinus Torvalds struct task_security_struct *tsec; 15833d5ff529SJosef Sipek struct inode *inode = bprm->file->f_path.dentry->d_inode; 15841da177e4SLinus Torvalds struct inode_security_struct *isec; 15851da177e4SLinus Torvalds struct bprm_security_struct *bsec; 15861da177e4SLinus Torvalds u32 newsid; 15871da177e4SLinus Torvalds struct avc_audit_data ad; 15881da177e4SLinus Torvalds int rc; 15891da177e4SLinus Torvalds 15901da177e4SLinus Torvalds rc = secondary_ops->bprm_set_security(bprm); 15911da177e4SLinus Torvalds if (rc) 15921da177e4SLinus Torvalds return rc; 15931da177e4SLinus Torvalds 15941da177e4SLinus Torvalds bsec = bprm->security; 15951da177e4SLinus Torvalds 15961da177e4SLinus Torvalds if (bsec->set) 15971da177e4SLinus Torvalds return 0; 15981da177e4SLinus Torvalds 15991da177e4SLinus Torvalds tsec = current->security; 16001da177e4SLinus Torvalds isec = inode->i_security; 16011da177e4SLinus Torvalds 16021da177e4SLinus Torvalds /* Default to the current task SID. */ 16031da177e4SLinus Torvalds bsec->sid = tsec->sid; 16041da177e4SLinus Torvalds 160528eba5bfSMichael LeMay /* Reset fs, key, and sock SIDs on execve. */ 16061da177e4SLinus Torvalds tsec->create_sid = 0; 160728eba5bfSMichael LeMay tsec->keycreate_sid = 0; 160842c3e03eSEric Paris tsec->sockcreate_sid = 0; 16091da177e4SLinus Torvalds 16101da177e4SLinus Torvalds if (tsec->exec_sid) { 16111da177e4SLinus Torvalds newsid = tsec->exec_sid; 16121da177e4SLinus Torvalds /* Reset exec SID on execve. */ 16131da177e4SLinus Torvalds tsec->exec_sid = 0; 16141da177e4SLinus Torvalds } else { 16151da177e4SLinus Torvalds /* Check for a default transition on this program. */ 16161da177e4SLinus Torvalds rc = security_transition_sid(tsec->sid, isec->sid, 16171da177e4SLinus Torvalds SECCLASS_PROCESS, &newsid); 16181da177e4SLinus Torvalds if (rc) 16191da177e4SLinus Torvalds return rc; 16201da177e4SLinus Torvalds } 16211da177e4SLinus Torvalds 16221da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 16233d5ff529SJosef Sipek ad.u.fs.mnt = bprm->file->f_path.mnt; 16243d5ff529SJosef Sipek ad.u.fs.dentry = bprm->file->f_path.dentry; 16251da177e4SLinus Torvalds 16263d5ff529SJosef Sipek if (bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) 16271da177e4SLinus Torvalds newsid = tsec->sid; 16281da177e4SLinus Torvalds 16291da177e4SLinus Torvalds if (tsec->sid == newsid) { 16301da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, 16311da177e4SLinus Torvalds SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad); 16321da177e4SLinus Torvalds if (rc) 16331da177e4SLinus Torvalds return rc; 16341da177e4SLinus Torvalds } else { 16351da177e4SLinus Torvalds /* Check permissions for the transition. */ 16361da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, newsid, 16371da177e4SLinus Torvalds SECCLASS_PROCESS, PROCESS__TRANSITION, &ad); 16381da177e4SLinus Torvalds if (rc) 16391da177e4SLinus Torvalds return rc; 16401da177e4SLinus Torvalds 16411da177e4SLinus Torvalds rc = avc_has_perm(newsid, isec->sid, 16421da177e4SLinus Torvalds SECCLASS_FILE, FILE__ENTRYPOINT, &ad); 16431da177e4SLinus Torvalds if (rc) 16441da177e4SLinus Torvalds return rc; 16451da177e4SLinus Torvalds 16461da177e4SLinus Torvalds /* Clear any possibly unsafe personality bits on exec: */ 16471da177e4SLinus Torvalds current->personality &= ~PER_CLEAR_ON_SETID; 16481da177e4SLinus Torvalds 16491da177e4SLinus Torvalds /* Set the security field to the new SID. */ 16501da177e4SLinus Torvalds bsec->sid = newsid; 16511da177e4SLinus Torvalds } 16521da177e4SLinus Torvalds 16531da177e4SLinus Torvalds bsec->set = 1; 16541da177e4SLinus Torvalds return 0; 16551da177e4SLinus Torvalds } 16561da177e4SLinus Torvalds 16571da177e4SLinus Torvalds static int selinux_bprm_check_security (struct linux_binprm *bprm) 16581da177e4SLinus Torvalds { 16591da177e4SLinus Torvalds return secondary_ops->bprm_check_security(bprm); 16601da177e4SLinus Torvalds } 16611da177e4SLinus Torvalds 16621da177e4SLinus Torvalds 16631da177e4SLinus Torvalds static int selinux_bprm_secureexec (struct linux_binprm *bprm) 16641da177e4SLinus Torvalds { 16651da177e4SLinus Torvalds struct task_security_struct *tsec = current->security; 16661da177e4SLinus Torvalds int atsecure = 0; 16671da177e4SLinus Torvalds 16681da177e4SLinus Torvalds if (tsec->osid != tsec->sid) { 16691da177e4SLinus Torvalds /* Enable secure mode for SIDs transitions unless 16701da177e4SLinus Torvalds the noatsecure permission is granted between 16711da177e4SLinus Torvalds the two SIDs, i.e. ahp returns 0. */ 16721da177e4SLinus Torvalds atsecure = avc_has_perm(tsec->osid, tsec->sid, 16731da177e4SLinus Torvalds SECCLASS_PROCESS, 16741da177e4SLinus Torvalds PROCESS__NOATSECURE, NULL); 16751da177e4SLinus Torvalds } 16761da177e4SLinus Torvalds 16771da177e4SLinus Torvalds return (atsecure || secondary_ops->bprm_secureexec(bprm)); 16781da177e4SLinus Torvalds } 16791da177e4SLinus Torvalds 16801da177e4SLinus Torvalds static void selinux_bprm_free_security(struct linux_binprm *bprm) 16811da177e4SLinus Torvalds { 16829a5f04bfSJesper Juhl kfree(bprm->security); 16831da177e4SLinus Torvalds bprm->security = NULL; 16841da177e4SLinus Torvalds } 16851da177e4SLinus Torvalds 16861da177e4SLinus Torvalds extern struct vfsmount *selinuxfs_mount; 16871da177e4SLinus Torvalds extern struct dentry *selinux_null; 16881da177e4SLinus Torvalds 16891da177e4SLinus Torvalds /* Derived from fs/exec.c:flush_old_files. */ 16901da177e4SLinus Torvalds static inline void flush_unauthorized_files(struct files_struct * files) 16911da177e4SLinus Torvalds { 16921da177e4SLinus Torvalds struct avc_audit_data ad; 16931da177e4SLinus Torvalds struct file *file, *devnull = NULL; 1694b20c8122SStephen Smalley struct tty_struct *tty; 1695badf1662SDipankar Sarma struct fdtable *fdt; 16961da177e4SLinus Torvalds long j = -1; 169724ec839cSPeter Zijlstra int drop_tty = 0; 16981da177e4SLinus Torvalds 1699b20c8122SStephen Smalley mutex_lock(&tty_mutex); 170024ec839cSPeter Zijlstra tty = get_current_tty(); 17011da177e4SLinus Torvalds if (tty) { 17021da177e4SLinus Torvalds file_list_lock(); 17032f512016SEric Dumazet file = list_entry(tty->tty_files.next, typeof(*file), f_u.fu_list); 17041da177e4SLinus Torvalds if (file) { 17051da177e4SLinus Torvalds /* Revalidate access to controlling tty. 17061da177e4SLinus Torvalds Use inode_has_perm on the tty inode directly rather 17071da177e4SLinus Torvalds than using file_has_perm, as this particular open 17081da177e4SLinus Torvalds file may belong to another process and we are only 17091da177e4SLinus Torvalds interested in the inode-based check here. */ 17103d5ff529SJosef Sipek struct inode *inode = file->f_path.dentry->d_inode; 17111da177e4SLinus Torvalds if (inode_has_perm(current, inode, 17121da177e4SLinus Torvalds FILE__READ | FILE__WRITE, NULL)) { 171324ec839cSPeter Zijlstra drop_tty = 1; 17141da177e4SLinus Torvalds } 17151da177e4SLinus Torvalds } 17161da177e4SLinus Torvalds file_list_unlock(); 171724ec839cSPeter Zijlstra 171824ec839cSPeter Zijlstra /* Reset controlling tty. */ 171924ec839cSPeter Zijlstra if (drop_tty) 172024ec839cSPeter Zijlstra proc_set_tty(current, NULL); 17211da177e4SLinus Torvalds } 1722b20c8122SStephen Smalley mutex_unlock(&tty_mutex); 17231da177e4SLinus Torvalds 17241da177e4SLinus Torvalds /* Revalidate access to inherited open files. */ 17251da177e4SLinus Torvalds 17261da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,FS); 17271da177e4SLinus Torvalds 17281da177e4SLinus Torvalds spin_lock(&files->file_lock); 17291da177e4SLinus Torvalds for (;;) { 17301da177e4SLinus Torvalds unsigned long set, i; 17311da177e4SLinus Torvalds int fd; 17321da177e4SLinus Torvalds 17331da177e4SLinus Torvalds j++; 17341da177e4SLinus Torvalds i = j * __NFDBITS; 1735badf1662SDipankar Sarma fdt = files_fdtable(files); 1736bbea9f69SVadim Lobanov if (i >= fdt->max_fds) 17371da177e4SLinus Torvalds break; 1738badf1662SDipankar Sarma set = fdt->open_fds->fds_bits[j]; 17391da177e4SLinus Torvalds if (!set) 17401da177e4SLinus Torvalds continue; 17411da177e4SLinus Torvalds spin_unlock(&files->file_lock); 17421da177e4SLinus Torvalds for ( ; set ; i++,set >>= 1) { 17431da177e4SLinus Torvalds if (set & 1) { 17441da177e4SLinus Torvalds file = fget(i); 17451da177e4SLinus Torvalds if (!file) 17461da177e4SLinus Torvalds continue; 17471da177e4SLinus Torvalds if (file_has_perm(current, 17481da177e4SLinus Torvalds file, 17491da177e4SLinus Torvalds file_to_av(file))) { 17501da177e4SLinus Torvalds sys_close(i); 17511da177e4SLinus Torvalds fd = get_unused_fd(); 17521da177e4SLinus Torvalds if (fd != i) { 17531da177e4SLinus Torvalds if (fd >= 0) 17541da177e4SLinus Torvalds put_unused_fd(fd); 17551da177e4SLinus Torvalds fput(file); 17561da177e4SLinus Torvalds continue; 17571da177e4SLinus Torvalds } 17581da177e4SLinus Torvalds if (devnull) { 1759095975daSNick Piggin get_file(devnull); 17601da177e4SLinus Torvalds } else { 17611da177e4SLinus Torvalds devnull = dentry_open(dget(selinux_null), mntget(selinuxfs_mount), O_RDWR); 1762fc5d81e6SAkinobu Mita if (IS_ERR(devnull)) { 1763fc5d81e6SAkinobu Mita devnull = NULL; 17641da177e4SLinus Torvalds put_unused_fd(fd); 17651da177e4SLinus Torvalds fput(file); 17661da177e4SLinus Torvalds continue; 17671da177e4SLinus Torvalds } 17681da177e4SLinus Torvalds } 17691da177e4SLinus Torvalds fd_install(fd, devnull); 17701da177e4SLinus Torvalds } 17711da177e4SLinus Torvalds fput(file); 17721da177e4SLinus Torvalds } 17731da177e4SLinus Torvalds } 17741da177e4SLinus Torvalds spin_lock(&files->file_lock); 17751da177e4SLinus Torvalds 17761da177e4SLinus Torvalds } 17771da177e4SLinus Torvalds spin_unlock(&files->file_lock); 17781da177e4SLinus Torvalds } 17791da177e4SLinus Torvalds 17801da177e4SLinus Torvalds static void selinux_bprm_apply_creds(struct linux_binprm *bprm, int unsafe) 17811da177e4SLinus Torvalds { 17821da177e4SLinus Torvalds struct task_security_struct *tsec; 17831da177e4SLinus Torvalds struct bprm_security_struct *bsec; 17841da177e4SLinus Torvalds u32 sid; 17851da177e4SLinus Torvalds int rc; 17861da177e4SLinus Torvalds 17871da177e4SLinus Torvalds secondary_ops->bprm_apply_creds(bprm, unsafe); 17881da177e4SLinus Torvalds 17891da177e4SLinus Torvalds tsec = current->security; 17901da177e4SLinus Torvalds 17911da177e4SLinus Torvalds bsec = bprm->security; 17921da177e4SLinus Torvalds sid = bsec->sid; 17931da177e4SLinus Torvalds 17941da177e4SLinus Torvalds tsec->osid = tsec->sid; 17951da177e4SLinus Torvalds bsec->unsafe = 0; 17961da177e4SLinus Torvalds if (tsec->sid != sid) { 17971da177e4SLinus Torvalds /* Check for shared state. If not ok, leave SID 17981da177e4SLinus Torvalds unchanged and kill. */ 17991da177e4SLinus Torvalds if (unsafe & LSM_UNSAFE_SHARE) { 18001da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS, 18011da177e4SLinus Torvalds PROCESS__SHARE, NULL); 18021da177e4SLinus Torvalds if (rc) { 18031da177e4SLinus Torvalds bsec->unsafe = 1; 18041da177e4SLinus Torvalds return; 18051da177e4SLinus Torvalds } 18061da177e4SLinus Torvalds } 18071da177e4SLinus Torvalds 18081da177e4SLinus Torvalds /* Check for ptracing, and update the task SID if ok. 18091da177e4SLinus Torvalds Otherwise, leave SID unchanged and kill. */ 18101da177e4SLinus Torvalds if (unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) { 18111da177e4SLinus Torvalds rc = avc_has_perm(tsec->ptrace_sid, sid, 18121da177e4SLinus Torvalds SECCLASS_PROCESS, PROCESS__PTRACE, 18131da177e4SLinus Torvalds NULL); 18141da177e4SLinus Torvalds if (rc) { 18151da177e4SLinus Torvalds bsec->unsafe = 1; 18161da177e4SLinus Torvalds return; 18171da177e4SLinus Torvalds } 18181da177e4SLinus Torvalds } 18191da177e4SLinus Torvalds tsec->sid = sid; 18201da177e4SLinus Torvalds } 18211da177e4SLinus Torvalds } 18221da177e4SLinus Torvalds 18231da177e4SLinus Torvalds /* 18241da177e4SLinus Torvalds * called after apply_creds without the task lock held 18251da177e4SLinus Torvalds */ 18261da177e4SLinus Torvalds static void selinux_bprm_post_apply_creds(struct linux_binprm *bprm) 18271da177e4SLinus Torvalds { 18281da177e4SLinus Torvalds struct task_security_struct *tsec; 18291da177e4SLinus Torvalds struct rlimit *rlim, *initrlim; 18301da177e4SLinus Torvalds struct itimerval itimer; 18311da177e4SLinus Torvalds struct bprm_security_struct *bsec; 18321da177e4SLinus Torvalds int rc, i; 18331da177e4SLinus Torvalds 18341da177e4SLinus Torvalds tsec = current->security; 18351da177e4SLinus Torvalds bsec = bprm->security; 18361da177e4SLinus Torvalds 18371da177e4SLinus Torvalds if (bsec->unsafe) { 18381da177e4SLinus Torvalds force_sig_specific(SIGKILL, current); 18391da177e4SLinus Torvalds return; 18401da177e4SLinus Torvalds } 18411da177e4SLinus Torvalds if (tsec->osid == tsec->sid) 18421da177e4SLinus Torvalds return; 18431da177e4SLinus Torvalds 18441da177e4SLinus Torvalds /* Close files for which the new task SID is not authorized. */ 18451da177e4SLinus Torvalds flush_unauthorized_files(current->files); 18461da177e4SLinus Torvalds 18471da177e4SLinus Torvalds /* Check whether the new SID can inherit signal state 18481da177e4SLinus Torvalds from the old SID. If not, clear itimers to avoid 18491da177e4SLinus Torvalds subsequent signal generation and flush and unblock 18501da177e4SLinus Torvalds signals. This must occur _after_ the task SID has 18511da177e4SLinus Torvalds been updated so that any kill done after the flush 18521da177e4SLinus Torvalds will be checked against the new SID. */ 18531da177e4SLinus Torvalds rc = avc_has_perm(tsec->osid, tsec->sid, SECCLASS_PROCESS, 18541da177e4SLinus Torvalds PROCESS__SIGINH, NULL); 18551da177e4SLinus Torvalds if (rc) { 18561da177e4SLinus Torvalds memset(&itimer, 0, sizeof itimer); 18571da177e4SLinus Torvalds for (i = 0; i < 3; i++) 18581da177e4SLinus Torvalds do_setitimer(i, &itimer, NULL); 18591da177e4SLinus Torvalds flush_signals(current); 18601da177e4SLinus Torvalds spin_lock_irq(¤t->sighand->siglock); 18611da177e4SLinus Torvalds flush_signal_handlers(current, 1); 18621da177e4SLinus Torvalds sigemptyset(¤t->blocked); 18631da177e4SLinus Torvalds recalc_sigpending(); 18641da177e4SLinus Torvalds spin_unlock_irq(¤t->sighand->siglock); 18651da177e4SLinus Torvalds } 18661da177e4SLinus Torvalds 18671da177e4SLinus Torvalds /* Check whether the new SID can inherit resource limits 18681da177e4SLinus Torvalds from the old SID. If not, reset all soft limits to 18691da177e4SLinus Torvalds the lower of the current task's hard limit and the init 18701da177e4SLinus Torvalds task's soft limit. Note that the setting of hard limits 18711da177e4SLinus Torvalds (even to lower them) can be controlled by the setrlimit 18721da177e4SLinus Torvalds check. The inclusion of the init task's soft limit into 18731da177e4SLinus Torvalds the computation is to avoid resetting soft limits higher 18741da177e4SLinus Torvalds than the default soft limit for cases where the default 18751da177e4SLinus Torvalds is lower than the hard limit, e.g. RLIMIT_CORE or 18761da177e4SLinus Torvalds RLIMIT_STACK.*/ 18771da177e4SLinus Torvalds rc = avc_has_perm(tsec->osid, tsec->sid, SECCLASS_PROCESS, 18781da177e4SLinus Torvalds PROCESS__RLIMITINH, NULL); 18791da177e4SLinus Torvalds if (rc) { 18801da177e4SLinus Torvalds for (i = 0; i < RLIM_NLIMITS; i++) { 18811da177e4SLinus Torvalds rlim = current->signal->rlim + i; 18821da177e4SLinus Torvalds initrlim = init_task.signal->rlim+i; 18831da177e4SLinus Torvalds rlim->rlim_cur = min(rlim->rlim_max,initrlim->rlim_cur); 18841da177e4SLinus Torvalds } 18851da177e4SLinus Torvalds if (current->signal->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) { 18861da177e4SLinus Torvalds /* 18871da177e4SLinus Torvalds * This will cause RLIMIT_CPU calculations 18881da177e4SLinus Torvalds * to be refigured. 18891da177e4SLinus Torvalds */ 18901da177e4SLinus Torvalds current->it_prof_expires = jiffies_to_cputime(1); 18911da177e4SLinus Torvalds } 18921da177e4SLinus Torvalds } 18931da177e4SLinus Torvalds 18941da177e4SLinus Torvalds /* Wake up the parent if it is waiting so that it can 18951da177e4SLinus Torvalds recheck wait permission to the new task SID. */ 18961da177e4SLinus Torvalds wake_up_interruptible(¤t->parent->signal->wait_chldexit); 18971da177e4SLinus Torvalds } 18981da177e4SLinus Torvalds 18991da177e4SLinus Torvalds /* superblock security operations */ 19001da177e4SLinus Torvalds 19011da177e4SLinus Torvalds static int selinux_sb_alloc_security(struct super_block *sb) 19021da177e4SLinus Torvalds { 19031da177e4SLinus Torvalds return superblock_alloc_security(sb); 19041da177e4SLinus Torvalds } 19051da177e4SLinus Torvalds 19061da177e4SLinus Torvalds static void selinux_sb_free_security(struct super_block *sb) 19071da177e4SLinus Torvalds { 19081da177e4SLinus Torvalds superblock_free_security(sb); 19091da177e4SLinus Torvalds } 19101da177e4SLinus Torvalds 19111da177e4SLinus Torvalds static inline int match_prefix(char *prefix, int plen, char *option, int olen) 19121da177e4SLinus Torvalds { 19131da177e4SLinus Torvalds if (plen > olen) 19141da177e4SLinus Torvalds return 0; 19151da177e4SLinus Torvalds 19161da177e4SLinus Torvalds return !memcmp(prefix, option, plen); 19171da177e4SLinus Torvalds } 19181da177e4SLinus Torvalds 19191da177e4SLinus Torvalds static inline int selinux_option(char *option, int len) 19201da177e4SLinus Torvalds { 19211da177e4SLinus Torvalds return (match_prefix("context=", sizeof("context=")-1, option, len) || 19221da177e4SLinus Torvalds match_prefix("fscontext=", sizeof("fscontext=")-1, option, len) || 19230808925eSEric Paris match_prefix("defcontext=", sizeof("defcontext=")-1, option, len) || 19240808925eSEric Paris match_prefix("rootcontext=", sizeof("rootcontext=")-1, option, len)); 19251da177e4SLinus Torvalds } 19261da177e4SLinus Torvalds 19271da177e4SLinus Torvalds static inline void take_option(char **to, char *from, int *first, int len) 19281da177e4SLinus Torvalds { 19291da177e4SLinus Torvalds if (!*first) { 19301da177e4SLinus Torvalds **to = ','; 19311da177e4SLinus Torvalds *to += 1; 19323528a953SCory Olmo } else 19331da177e4SLinus Torvalds *first = 0; 19341da177e4SLinus Torvalds memcpy(*to, from, len); 19351da177e4SLinus Torvalds *to += len; 19361da177e4SLinus Torvalds } 19371da177e4SLinus Torvalds 19383528a953SCory Olmo static inline void take_selinux_option(char **to, char *from, int *first, 19393528a953SCory Olmo int len) 19403528a953SCory Olmo { 19413528a953SCory Olmo int current_size = 0; 19423528a953SCory Olmo 19433528a953SCory Olmo if (!*first) { 19443528a953SCory Olmo **to = '|'; 19453528a953SCory Olmo *to += 1; 19463528a953SCory Olmo } 19473528a953SCory Olmo else 19483528a953SCory Olmo *first = 0; 19493528a953SCory Olmo 19503528a953SCory Olmo while (current_size < len) { 19513528a953SCory Olmo if (*from != '"') { 19523528a953SCory Olmo **to = *from; 19533528a953SCory Olmo *to += 1; 19543528a953SCory Olmo } 19553528a953SCory Olmo from += 1; 19563528a953SCory Olmo current_size += 1; 19573528a953SCory Olmo } 19583528a953SCory Olmo } 19593528a953SCory Olmo 19601da177e4SLinus Torvalds static int selinux_sb_copy_data(struct file_system_type *type, void *orig, void *copy) 19611da177e4SLinus Torvalds { 19621da177e4SLinus Torvalds int fnosec, fsec, rc = 0; 19631da177e4SLinus Torvalds char *in_save, *in_curr, *in_end; 19641da177e4SLinus Torvalds char *sec_curr, *nosec_save, *nosec; 19653528a953SCory Olmo int open_quote = 0; 19661da177e4SLinus Torvalds 19671da177e4SLinus Torvalds in_curr = orig; 19681da177e4SLinus Torvalds sec_curr = copy; 19691da177e4SLinus Torvalds 19701da177e4SLinus Torvalds /* Binary mount data: just copy */ 19711da177e4SLinus Torvalds if (type->fs_flags & FS_BINARY_MOUNTDATA) { 19721da177e4SLinus Torvalds copy_page(sec_curr, in_curr); 19731da177e4SLinus Torvalds goto out; 19741da177e4SLinus Torvalds } 19751da177e4SLinus Torvalds 19761da177e4SLinus Torvalds nosec = (char *)get_zeroed_page(GFP_KERNEL); 19771da177e4SLinus Torvalds if (!nosec) { 19781da177e4SLinus Torvalds rc = -ENOMEM; 19791da177e4SLinus Torvalds goto out; 19801da177e4SLinus Torvalds } 19811da177e4SLinus Torvalds 19821da177e4SLinus Torvalds nosec_save = nosec; 19831da177e4SLinus Torvalds fnosec = fsec = 1; 19841da177e4SLinus Torvalds in_save = in_end = orig; 19851da177e4SLinus Torvalds 19861da177e4SLinus Torvalds do { 19873528a953SCory Olmo if (*in_end == '"') 19883528a953SCory Olmo open_quote = !open_quote; 19893528a953SCory Olmo if ((*in_end == ',' && open_quote == 0) || 19903528a953SCory Olmo *in_end == '\0') { 19911da177e4SLinus Torvalds int len = in_end - in_curr; 19921da177e4SLinus Torvalds 19931da177e4SLinus Torvalds if (selinux_option(in_curr, len)) 19943528a953SCory Olmo take_selinux_option(&sec_curr, in_curr, &fsec, len); 19951da177e4SLinus Torvalds else 19961da177e4SLinus Torvalds take_option(&nosec, in_curr, &fnosec, len); 19971da177e4SLinus Torvalds 19981da177e4SLinus Torvalds in_curr = in_end + 1; 19991da177e4SLinus Torvalds } 20001da177e4SLinus Torvalds } while (*in_end++); 20011da177e4SLinus Torvalds 20026931dfc9SEric Paris strcpy(in_save, nosec_save); 2003da3caa20SGerald Schaefer free_page((unsigned long)nosec_save); 20041da177e4SLinus Torvalds out: 20051da177e4SLinus Torvalds return rc; 20061da177e4SLinus Torvalds } 20071da177e4SLinus Torvalds 20081da177e4SLinus Torvalds static int selinux_sb_kern_mount(struct super_block *sb, void *data) 20091da177e4SLinus Torvalds { 20101da177e4SLinus Torvalds struct avc_audit_data ad; 20111da177e4SLinus Torvalds int rc; 20121da177e4SLinus Torvalds 20131da177e4SLinus Torvalds rc = superblock_doinit(sb, data); 20141da177e4SLinus Torvalds if (rc) 20151da177e4SLinus Torvalds return rc; 20161da177e4SLinus Torvalds 20171da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,FS); 20181da177e4SLinus Torvalds ad.u.fs.dentry = sb->s_root; 20191da177e4SLinus Torvalds return superblock_has_perm(current, sb, FILESYSTEM__MOUNT, &ad); 20201da177e4SLinus Torvalds } 20211da177e4SLinus Torvalds 2022726c3342SDavid Howells static int selinux_sb_statfs(struct dentry *dentry) 20231da177e4SLinus Torvalds { 20241da177e4SLinus Torvalds struct avc_audit_data ad; 20251da177e4SLinus Torvalds 20261da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,FS); 2027726c3342SDavid Howells ad.u.fs.dentry = dentry->d_sb->s_root; 2028726c3342SDavid Howells return superblock_has_perm(current, dentry->d_sb, FILESYSTEM__GETATTR, &ad); 20291da177e4SLinus Torvalds } 20301da177e4SLinus Torvalds 20311da177e4SLinus Torvalds static int selinux_mount(char * dev_name, 20321da177e4SLinus Torvalds struct nameidata *nd, 20331da177e4SLinus Torvalds char * type, 20341da177e4SLinus Torvalds unsigned long flags, 20351da177e4SLinus Torvalds void * data) 20361da177e4SLinus Torvalds { 20371da177e4SLinus Torvalds int rc; 20381da177e4SLinus Torvalds 20391da177e4SLinus Torvalds rc = secondary_ops->sb_mount(dev_name, nd, type, flags, data); 20401da177e4SLinus Torvalds if (rc) 20411da177e4SLinus Torvalds return rc; 20421da177e4SLinus Torvalds 20431da177e4SLinus Torvalds if (flags & MS_REMOUNT) 20441da177e4SLinus Torvalds return superblock_has_perm(current, nd->mnt->mnt_sb, 20451da177e4SLinus Torvalds FILESYSTEM__REMOUNT, NULL); 20461da177e4SLinus Torvalds else 20471da177e4SLinus Torvalds return dentry_has_perm(current, nd->mnt, nd->dentry, 20481da177e4SLinus Torvalds FILE__MOUNTON); 20491da177e4SLinus Torvalds } 20501da177e4SLinus Torvalds 20511da177e4SLinus Torvalds static int selinux_umount(struct vfsmount *mnt, int flags) 20521da177e4SLinus Torvalds { 20531da177e4SLinus Torvalds int rc; 20541da177e4SLinus Torvalds 20551da177e4SLinus Torvalds rc = secondary_ops->sb_umount(mnt, flags); 20561da177e4SLinus Torvalds if (rc) 20571da177e4SLinus Torvalds return rc; 20581da177e4SLinus Torvalds 20591da177e4SLinus Torvalds return superblock_has_perm(current,mnt->mnt_sb, 20601da177e4SLinus Torvalds FILESYSTEM__UNMOUNT,NULL); 20611da177e4SLinus Torvalds } 20621da177e4SLinus Torvalds 20631da177e4SLinus Torvalds /* inode security operations */ 20641da177e4SLinus Torvalds 20651da177e4SLinus Torvalds static int selinux_inode_alloc_security(struct inode *inode) 20661da177e4SLinus Torvalds { 20671da177e4SLinus Torvalds return inode_alloc_security(inode); 20681da177e4SLinus Torvalds } 20691da177e4SLinus Torvalds 20701da177e4SLinus Torvalds static void selinux_inode_free_security(struct inode *inode) 20711da177e4SLinus Torvalds { 20721da177e4SLinus Torvalds inode_free_security(inode); 20731da177e4SLinus Torvalds } 20741da177e4SLinus Torvalds 20755e41ff9eSStephen Smalley static int selinux_inode_init_security(struct inode *inode, struct inode *dir, 20765e41ff9eSStephen Smalley char **name, void **value, 20775e41ff9eSStephen Smalley size_t *len) 20785e41ff9eSStephen Smalley { 20795e41ff9eSStephen Smalley struct task_security_struct *tsec; 20805e41ff9eSStephen Smalley struct inode_security_struct *dsec; 20815e41ff9eSStephen Smalley struct superblock_security_struct *sbsec; 2082570bc1c2SStephen Smalley u32 newsid, clen; 20835e41ff9eSStephen Smalley int rc; 2084570bc1c2SStephen Smalley char *namep = NULL, *context; 20855e41ff9eSStephen Smalley 20865e41ff9eSStephen Smalley tsec = current->security; 20875e41ff9eSStephen Smalley dsec = dir->i_security; 20885e41ff9eSStephen Smalley sbsec = dir->i_sb->s_security; 20895e41ff9eSStephen Smalley 20905e41ff9eSStephen Smalley if (tsec->create_sid && sbsec->behavior != SECURITY_FS_USE_MNTPOINT) { 20915e41ff9eSStephen Smalley newsid = tsec->create_sid; 20925e41ff9eSStephen Smalley } else { 20935e41ff9eSStephen Smalley rc = security_transition_sid(tsec->sid, dsec->sid, 20945e41ff9eSStephen Smalley inode_mode_to_security_class(inode->i_mode), 20955e41ff9eSStephen Smalley &newsid); 20965e41ff9eSStephen Smalley if (rc) { 20975e41ff9eSStephen Smalley printk(KERN_WARNING "%s: " 20985e41ff9eSStephen Smalley "security_transition_sid failed, rc=%d (dev=%s " 20995e41ff9eSStephen Smalley "ino=%ld)\n", 21005e41ff9eSStephen Smalley __FUNCTION__, 21015e41ff9eSStephen Smalley -rc, inode->i_sb->s_id, inode->i_ino); 21025e41ff9eSStephen Smalley return rc; 21035e41ff9eSStephen Smalley } 21045e41ff9eSStephen Smalley } 21055e41ff9eSStephen Smalley 2106296fddf7SEric Paris /* Possibly defer initialization to selinux_complete_init. */ 2107296fddf7SEric Paris if (sbsec->initialized) { 2108296fddf7SEric Paris struct inode_security_struct *isec = inode->i_security; 2109296fddf7SEric Paris isec->sclass = inode_mode_to_security_class(inode->i_mode); 2110296fddf7SEric Paris isec->sid = newsid; 2111296fddf7SEric Paris isec->initialized = 1; 2112296fddf7SEric Paris } 21135e41ff9eSStephen Smalley 21148aad3875SStephen Smalley if (!ss_initialized || sbsec->behavior == SECURITY_FS_USE_MNTPOINT) 211525a74f3bSStephen Smalley return -EOPNOTSUPP; 211625a74f3bSStephen Smalley 2117570bc1c2SStephen Smalley if (name) { 21185e41ff9eSStephen Smalley namep = kstrdup(XATTR_SELINUX_SUFFIX, GFP_KERNEL); 21195e41ff9eSStephen Smalley if (!namep) 21205e41ff9eSStephen Smalley return -ENOMEM; 21215e41ff9eSStephen Smalley *name = namep; 2122570bc1c2SStephen Smalley } 21235e41ff9eSStephen Smalley 2124570bc1c2SStephen Smalley if (value && len) { 2125570bc1c2SStephen Smalley rc = security_sid_to_context(newsid, &context, &clen); 21265e41ff9eSStephen Smalley if (rc) { 21275e41ff9eSStephen Smalley kfree(namep); 21285e41ff9eSStephen Smalley return rc; 21295e41ff9eSStephen Smalley } 21305e41ff9eSStephen Smalley *value = context; 2131570bc1c2SStephen Smalley *len = clen; 2132570bc1c2SStephen Smalley } 21335e41ff9eSStephen Smalley 21345e41ff9eSStephen Smalley return 0; 21355e41ff9eSStephen Smalley } 21365e41ff9eSStephen Smalley 21371da177e4SLinus Torvalds static int selinux_inode_create(struct inode *dir, struct dentry *dentry, int mask) 21381da177e4SLinus Torvalds { 21391da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_FILE); 21401da177e4SLinus Torvalds } 21411da177e4SLinus Torvalds 21421da177e4SLinus Torvalds static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 21431da177e4SLinus Torvalds { 21441da177e4SLinus Torvalds int rc; 21451da177e4SLinus Torvalds 21461da177e4SLinus Torvalds rc = secondary_ops->inode_link(old_dentry,dir,new_dentry); 21471da177e4SLinus Torvalds if (rc) 21481da177e4SLinus Torvalds return rc; 21491da177e4SLinus Torvalds return may_link(dir, old_dentry, MAY_LINK); 21501da177e4SLinus Torvalds } 21511da177e4SLinus Torvalds 21521da177e4SLinus Torvalds static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry) 21531da177e4SLinus Torvalds { 21541da177e4SLinus Torvalds int rc; 21551da177e4SLinus Torvalds 21561da177e4SLinus Torvalds rc = secondary_ops->inode_unlink(dir, dentry); 21571da177e4SLinus Torvalds if (rc) 21581da177e4SLinus Torvalds return rc; 21591da177e4SLinus Torvalds return may_link(dir, dentry, MAY_UNLINK); 21601da177e4SLinus Torvalds } 21611da177e4SLinus Torvalds 21621da177e4SLinus Torvalds static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name) 21631da177e4SLinus Torvalds { 21641da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_LNK_FILE); 21651da177e4SLinus Torvalds } 21661da177e4SLinus Torvalds 21671da177e4SLinus Torvalds static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, int mask) 21681da177e4SLinus Torvalds { 21691da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_DIR); 21701da177e4SLinus Torvalds } 21711da177e4SLinus Torvalds 21721da177e4SLinus Torvalds static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry) 21731da177e4SLinus Torvalds { 21741da177e4SLinus Torvalds return may_link(dir, dentry, MAY_RMDIR); 21751da177e4SLinus Torvalds } 21761da177e4SLinus Torvalds 21771da177e4SLinus Torvalds static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 21781da177e4SLinus Torvalds { 21791da177e4SLinus Torvalds int rc; 21801da177e4SLinus Torvalds 21811da177e4SLinus Torvalds rc = secondary_ops->inode_mknod(dir, dentry, mode, dev); 21821da177e4SLinus Torvalds if (rc) 21831da177e4SLinus Torvalds return rc; 21841da177e4SLinus Torvalds 21851da177e4SLinus Torvalds return may_create(dir, dentry, inode_mode_to_security_class(mode)); 21861da177e4SLinus Torvalds } 21871da177e4SLinus Torvalds 21881da177e4SLinus Torvalds static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry, 21891da177e4SLinus Torvalds struct inode *new_inode, struct dentry *new_dentry) 21901da177e4SLinus Torvalds { 21911da177e4SLinus Torvalds return may_rename(old_inode, old_dentry, new_inode, new_dentry); 21921da177e4SLinus Torvalds } 21931da177e4SLinus Torvalds 21941da177e4SLinus Torvalds static int selinux_inode_readlink(struct dentry *dentry) 21951da177e4SLinus Torvalds { 21961da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__READ); 21971da177e4SLinus Torvalds } 21981da177e4SLinus Torvalds 21991da177e4SLinus Torvalds static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata) 22001da177e4SLinus Torvalds { 22011da177e4SLinus Torvalds int rc; 22021da177e4SLinus Torvalds 22031da177e4SLinus Torvalds rc = secondary_ops->inode_follow_link(dentry,nameidata); 22041da177e4SLinus Torvalds if (rc) 22051da177e4SLinus Torvalds return rc; 22061da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__READ); 22071da177e4SLinus Torvalds } 22081da177e4SLinus Torvalds 22091da177e4SLinus Torvalds static int selinux_inode_permission(struct inode *inode, int mask, 22101da177e4SLinus Torvalds struct nameidata *nd) 22111da177e4SLinus Torvalds { 22121da177e4SLinus Torvalds int rc; 22131da177e4SLinus Torvalds 22141da177e4SLinus Torvalds rc = secondary_ops->inode_permission(inode, mask, nd); 22151da177e4SLinus Torvalds if (rc) 22161da177e4SLinus Torvalds return rc; 22171da177e4SLinus Torvalds 22181da177e4SLinus Torvalds if (!mask) { 22191da177e4SLinus Torvalds /* No permission to check. Existence test. */ 22201da177e4SLinus Torvalds return 0; 22211da177e4SLinus Torvalds } 22221da177e4SLinus Torvalds 22231da177e4SLinus Torvalds return inode_has_perm(current, inode, 22241da177e4SLinus Torvalds file_mask_to_av(inode->i_mode, mask), NULL); 22251da177e4SLinus Torvalds } 22261da177e4SLinus Torvalds 22271da177e4SLinus Torvalds static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr) 22281da177e4SLinus Torvalds { 22291da177e4SLinus Torvalds int rc; 22301da177e4SLinus Torvalds 22311da177e4SLinus Torvalds rc = secondary_ops->inode_setattr(dentry, iattr); 22321da177e4SLinus Torvalds if (rc) 22331da177e4SLinus Torvalds return rc; 22341da177e4SLinus Torvalds 22351da177e4SLinus Torvalds if (iattr->ia_valid & ATTR_FORCE) 22361da177e4SLinus Torvalds return 0; 22371da177e4SLinus Torvalds 22381da177e4SLinus Torvalds if (iattr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | 22391da177e4SLinus Torvalds ATTR_ATIME_SET | ATTR_MTIME_SET)) 22401da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__SETATTR); 22411da177e4SLinus Torvalds 22421da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__WRITE); 22431da177e4SLinus Torvalds } 22441da177e4SLinus Torvalds 22451da177e4SLinus Torvalds static int selinux_inode_getattr(struct vfsmount *mnt, struct dentry *dentry) 22461da177e4SLinus Torvalds { 22471da177e4SLinus Torvalds return dentry_has_perm(current, mnt, dentry, FILE__GETATTR); 22481da177e4SLinus Torvalds } 22491da177e4SLinus Torvalds 22501da177e4SLinus Torvalds static int selinux_inode_setxattr(struct dentry *dentry, char *name, void *value, size_t size, int flags) 22511da177e4SLinus Torvalds { 22521da177e4SLinus Torvalds struct task_security_struct *tsec = current->security; 22531da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 22541da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 22551da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 22561da177e4SLinus Torvalds struct avc_audit_data ad; 22571da177e4SLinus Torvalds u32 newsid; 22581da177e4SLinus Torvalds int rc = 0; 22591da177e4SLinus Torvalds 22601da177e4SLinus Torvalds if (strcmp(name, XATTR_NAME_SELINUX)) { 22611da177e4SLinus Torvalds if (!strncmp(name, XATTR_SECURITY_PREFIX, 22621da177e4SLinus Torvalds sizeof XATTR_SECURITY_PREFIX - 1) && 22631da177e4SLinus Torvalds !capable(CAP_SYS_ADMIN)) { 22641da177e4SLinus Torvalds /* A different attribute in the security namespace. 22651da177e4SLinus Torvalds Restrict to administrator. */ 22661da177e4SLinus Torvalds return -EPERM; 22671da177e4SLinus Torvalds } 22681da177e4SLinus Torvalds 22691da177e4SLinus Torvalds /* Not an attribute we recognize, so just check the 22701da177e4SLinus Torvalds ordinary setattr permission. */ 22711da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__SETATTR); 22721da177e4SLinus Torvalds } 22731da177e4SLinus Torvalds 22741da177e4SLinus Torvalds sbsec = inode->i_sb->s_security; 22751da177e4SLinus Torvalds if (sbsec->behavior == SECURITY_FS_USE_MNTPOINT) 22761da177e4SLinus Torvalds return -EOPNOTSUPP; 22771da177e4SLinus Torvalds 22781da177e4SLinus Torvalds if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER)) 22791da177e4SLinus Torvalds return -EPERM; 22801da177e4SLinus Torvalds 22811da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,FS); 22821da177e4SLinus Torvalds ad.u.fs.dentry = dentry; 22831da177e4SLinus Torvalds 22841da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, isec->sclass, 22851da177e4SLinus Torvalds FILE__RELABELFROM, &ad); 22861da177e4SLinus Torvalds if (rc) 22871da177e4SLinus Torvalds return rc; 22881da177e4SLinus Torvalds 22891da177e4SLinus Torvalds rc = security_context_to_sid(value, size, &newsid); 22901da177e4SLinus Torvalds if (rc) 22911da177e4SLinus Torvalds return rc; 22921da177e4SLinus Torvalds 22931da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, newsid, isec->sclass, 22941da177e4SLinus Torvalds FILE__RELABELTO, &ad); 22951da177e4SLinus Torvalds if (rc) 22961da177e4SLinus Torvalds return rc; 22971da177e4SLinus Torvalds 22981da177e4SLinus Torvalds rc = security_validate_transition(isec->sid, newsid, tsec->sid, 22991da177e4SLinus Torvalds isec->sclass); 23001da177e4SLinus Torvalds if (rc) 23011da177e4SLinus Torvalds return rc; 23021da177e4SLinus Torvalds 23031da177e4SLinus Torvalds return avc_has_perm(newsid, 23041da177e4SLinus Torvalds sbsec->sid, 23051da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 23061da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, 23071da177e4SLinus Torvalds &ad); 23081da177e4SLinus Torvalds } 23091da177e4SLinus Torvalds 23101da177e4SLinus Torvalds static void selinux_inode_post_setxattr(struct dentry *dentry, char *name, 23111da177e4SLinus Torvalds void *value, size_t size, int flags) 23121da177e4SLinus Torvalds { 23131da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 23141da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 23151da177e4SLinus Torvalds u32 newsid; 23161da177e4SLinus Torvalds int rc; 23171da177e4SLinus Torvalds 23181da177e4SLinus Torvalds if (strcmp(name, XATTR_NAME_SELINUX)) { 23191da177e4SLinus Torvalds /* Not an attribute we recognize, so nothing to do. */ 23201da177e4SLinus Torvalds return; 23211da177e4SLinus Torvalds } 23221da177e4SLinus Torvalds 23231da177e4SLinus Torvalds rc = security_context_to_sid(value, size, &newsid); 23241da177e4SLinus Torvalds if (rc) { 23251da177e4SLinus Torvalds printk(KERN_WARNING "%s: unable to obtain SID for context " 23261da177e4SLinus Torvalds "%s, rc=%d\n", __FUNCTION__, (char*)value, -rc); 23271da177e4SLinus Torvalds return; 23281da177e4SLinus Torvalds } 23291da177e4SLinus Torvalds 23301da177e4SLinus Torvalds isec->sid = newsid; 23311da177e4SLinus Torvalds return; 23321da177e4SLinus Torvalds } 23331da177e4SLinus Torvalds 23341da177e4SLinus Torvalds static int selinux_inode_getxattr (struct dentry *dentry, char *name) 23351da177e4SLinus Torvalds { 23361da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__GETATTR); 23371da177e4SLinus Torvalds } 23381da177e4SLinus Torvalds 23391da177e4SLinus Torvalds static int selinux_inode_listxattr (struct dentry *dentry) 23401da177e4SLinus Torvalds { 23411da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__GETATTR); 23421da177e4SLinus Torvalds } 23431da177e4SLinus Torvalds 23441da177e4SLinus Torvalds static int selinux_inode_removexattr (struct dentry *dentry, char *name) 23451da177e4SLinus Torvalds { 23461da177e4SLinus Torvalds if (strcmp(name, XATTR_NAME_SELINUX)) { 23471da177e4SLinus Torvalds if (!strncmp(name, XATTR_SECURITY_PREFIX, 23481da177e4SLinus Torvalds sizeof XATTR_SECURITY_PREFIX - 1) && 23491da177e4SLinus Torvalds !capable(CAP_SYS_ADMIN)) { 23501da177e4SLinus Torvalds /* A different attribute in the security namespace. 23511da177e4SLinus Torvalds Restrict to administrator. */ 23521da177e4SLinus Torvalds return -EPERM; 23531da177e4SLinus Torvalds } 23541da177e4SLinus Torvalds 23551da177e4SLinus Torvalds /* Not an attribute we recognize, so just check the 23561da177e4SLinus Torvalds ordinary setattr permission. Might want a separate 23571da177e4SLinus Torvalds permission for removexattr. */ 23581da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__SETATTR); 23591da177e4SLinus Torvalds } 23601da177e4SLinus Torvalds 23611da177e4SLinus Torvalds /* No one is allowed to remove a SELinux security label. 23621da177e4SLinus Torvalds You can change the label, but all data must be labeled. */ 23631da177e4SLinus Torvalds return -EACCES; 23641da177e4SLinus Torvalds } 23651da177e4SLinus Torvalds 23668c8570fbSDustin Kirkland static const char *selinux_inode_xattr_getsuffix(void) 23678c8570fbSDustin Kirkland { 23688c8570fbSDustin Kirkland return XATTR_SELINUX_SUFFIX; 23698c8570fbSDustin Kirkland } 23708c8570fbSDustin Kirkland 2371d381d8a9SJames Morris /* 2372d381d8a9SJames Morris * Copy the in-core inode security context value to the user. If the 2373d381d8a9SJames Morris * getxattr() prior to this succeeded, check to see if we need to 2374d381d8a9SJames Morris * canonicalize the value to be finally returned to the user. 2375d381d8a9SJames Morris * 2376d381d8a9SJames Morris * Permission check is handled by selinux_inode_getxattr hook. 2377d381d8a9SJames Morris */ 23787306a0b9SDustin Kirkland static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void *buffer, size_t size, int err) 23791da177e4SLinus Torvalds { 23801da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 23811da177e4SLinus Torvalds 23828c8570fbSDustin Kirkland if (strcmp(name, XATTR_SELINUX_SUFFIX)) 23838c8570fbSDustin Kirkland return -EOPNOTSUPP; 23841da177e4SLinus Torvalds 23858c8570fbSDustin Kirkland return selinux_getsecurity(isec->sid, buffer, size); 23861da177e4SLinus Torvalds } 23871da177e4SLinus Torvalds 23881da177e4SLinus Torvalds static int selinux_inode_setsecurity(struct inode *inode, const char *name, 23891da177e4SLinus Torvalds const void *value, size_t size, int flags) 23901da177e4SLinus Torvalds { 23911da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 23921da177e4SLinus Torvalds u32 newsid; 23931da177e4SLinus Torvalds int rc; 23941da177e4SLinus Torvalds 23951da177e4SLinus Torvalds if (strcmp(name, XATTR_SELINUX_SUFFIX)) 23961da177e4SLinus Torvalds return -EOPNOTSUPP; 23971da177e4SLinus Torvalds 23981da177e4SLinus Torvalds if (!value || !size) 23991da177e4SLinus Torvalds return -EACCES; 24001da177e4SLinus Torvalds 24011da177e4SLinus Torvalds rc = security_context_to_sid((void*)value, size, &newsid); 24021da177e4SLinus Torvalds if (rc) 24031da177e4SLinus Torvalds return rc; 24041da177e4SLinus Torvalds 24051da177e4SLinus Torvalds isec->sid = newsid; 24061da177e4SLinus Torvalds return 0; 24071da177e4SLinus Torvalds } 24081da177e4SLinus Torvalds 24091da177e4SLinus Torvalds static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size) 24101da177e4SLinus Torvalds { 24111da177e4SLinus Torvalds const int len = sizeof(XATTR_NAME_SELINUX); 24121da177e4SLinus Torvalds if (buffer && len <= buffer_size) 24131da177e4SLinus Torvalds memcpy(buffer, XATTR_NAME_SELINUX, len); 24141da177e4SLinus Torvalds return len; 24151da177e4SLinus Torvalds } 24161da177e4SLinus Torvalds 24171da177e4SLinus Torvalds /* file security operations */ 24181da177e4SLinus Torvalds 24191da177e4SLinus Torvalds static int selinux_file_permission(struct file *file, int mask) 24201da177e4SLinus Torvalds { 24217420ed23SVenkat Yekkirala int rc; 24223d5ff529SJosef Sipek struct inode *inode = file->f_path.dentry->d_inode; 24231da177e4SLinus Torvalds 24241da177e4SLinus Torvalds if (!mask) { 24251da177e4SLinus Torvalds /* No permission to check. Existence test. */ 24261da177e4SLinus Torvalds return 0; 24271da177e4SLinus Torvalds } 24281da177e4SLinus Torvalds 24291da177e4SLinus Torvalds /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */ 24301da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE)) 24311da177e4SLinus Torvalds mask |= MAY_APPEND; 24321da177e4SLinus Torvalds 24337420ed23SVenkat Yekkirala rc = file_has_perm(current, file, 24341da177e4SLinus Torvalds file_mask_to_av(inode->i_mode, mask)); 24357420ed23SVenkat Yekkirala if (rc) 24367420ed23SVenkat Yekkirala return rc; 24377420ed23SVenkat Yekkirala 24387420ed23SVenkat Yekkirala return selinux_netlbl_inode_permission(inode, mask); 24391da177e4SLinus Torvalds } 24401da177e4SLinus Torvalds 24411da177e4SLinus Torvalds static int selinux_file_alloc_security(struct file *file) 24421da177e4SLinus Torvalds { 24431da177e4SLinus Torvalds return file_alloc_security(file); 24441da177e4SLinus Torvalds } 24451da177e4SLinus Torvalds 24461da177e4SLinus Torvalds static void selinux_file_free_security(struct file *file) 24471da177e4SLinus Torvalds { 24481da177e4SLinus Torvalds file_free_security(file); 24491da177e4SLinus Torvalds } 24501da177e4SLinus Torvalds 24511da177e4SLinus Torvalds static int selinux_file_ioctl(struct file *file, unsigned int cmd, 24521da177e4SLinus Torvalds unsigned long arg) 24531da177e4SLinus Torvalds { 24541da177e4SLinus Torvalds int error = 0; 24551da177e4SLinus Torvalds 24561da177e4SLinus Torvalds switch (cmd) { 24571da177e4SLinus Torvalds case FIONREAD: 24581da177e4SLinus Torvalds /* fall through */ 24591da177e4SLinus Torvalds case FIBMAP: 24601da177e4SLinus Torvalds /* fall through */ 24611da177e4SLinus Torvalds case FIGETBSZ: 24621da177e4SLinus Torvalds /* fall through */ 24631da177e4SLinus Torvalds case EXT2_IOC_GETFLAGS: 24641da177e4SLinus Torvalds /* fall through */ 24651da177e4SLinus Torvalds case EXT2_IOC_GETVERSION: 24661da177e4SLinus Torvalds error = file_has_perm(current, file, FILE__GETATTR); 24671da177e4SLinus Torvalds break; 24681da177e4SLinus Torvalds 24691da177e4SLinus Torvalds case EXT2_IOC_SETFLAGS: 24701da177e4SLinus Torvalds /* fall through */ 24711da177e4SLinus Torvalds case EXT2_IOC_SETVERSION: 24721da177e4SLinus Torvalds error = file_has_perm(current, file, FILE__SETATTR); 24731da177e4SLinus Torvalds break; 24741da177e4SLinus Torvalds 24751da177e4SLinus Torvalds /* sys_ioctl() checks */ 24761da177e4SLinus Torvalds case FIONBIO: 24771da177e4SLinus Torvalds /* fall through */ 24781da177e4SLinus Torvalds case FIOASYNC: 24791da177e4SLinus Torvalds error = file_has_perm(current, file, 0); 24801da177e4SLinus Torvalds break; 24811da177e4SLinus Torvalds 24821da177e4SLinus Torvalds case KDSKBENT: 24831da177e4SLinus Torvalds case KDSKBSENT: 24841da177e4SLinus Torvalds error = task_has_capability(current,CAP_SYS_TTY_CONFIG); 24851da177e4SLinus Torvalds break; 24861da177e4SLinus Torvalds 24871da177e4SLinus Torvalds /* default case assumes that the command will go 24881da177e4SLinus Torvalds * to the file's ioctl() function. 24891da177e4SLinus Torvalds */ 24901da177e4SLinus Torvalds default: 24911da177e4SLinus Torvalds error = file_has_perm(current, file, FILE__IOCTL); 24921da177e4SLinus Torvalds 24931da177e4SLinus Torvalds } 24941da177e4SLinus Torvalds return error; 24951da177e4SLinus Torvalds } 24961da177e4SLinus Torvalds 24971da177e4SLinus Torvalds static int file_map_prot_check(struct file *file, unsigned long prot, int shared) 24981da177e4SLinus Torvalds { 24991da177e4SLinus Torvalds #ifndef CONFIG_PPC32 25001da177e4SLinus Torvalds if ((prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) { 25011da177e4SLinus Torvalds /* 25021da177e4SLinus Torvalds * We are making executable an anonymous mapping or a 25031da177e4SLinus Torvalds * private file mapping that will also be writable. 25041da177e4SLinus Torvalds * This has an additional check. 25051da177e4SLinus Torvalds */ 25061da177e4SLinus Torvalds int rc = task_has_perm(current, current, PROCESS__EXECMEM); 25071da177e4SLinus Torvalds if (rc) 25081da177e4SLinus Torvalds return rc; 25091da177e4SLinus Torvalds } 25101da177e4SLinus Torvalds #endif 25111da177e4SLinus Torvalds 25121da177e4SLinus Torvalds if (file) { 25131da177e4SLinus Torvalds /* read access is always possible with a mapping */ 25141da177e4SLinus Torvalds u32 av = FILE__READ; 25151da177e4SLinus Torvalds 25161da177e4SLinus Torvalds /* write access only matters if the mapping is shared */ 25171da177e4SLinus Torvalds if (shared && (prot & PROT_WRITE)) 25181da177e4SLinus Torvalds av |= FILE__WRITE; 25191da177e4SLinus Torvalds 25201da177e4SLinus Torvalds if (prot & PROT_EXEC) 25211da177e4SLinus Torvalds av |= FILE__EXECUTE; 25221da177e4SLinus Torvalds 25231da177e4SLinus Torvalds return file_has_perm(current, file, av); 25241da177e4SLinus Torvalds } 25251da177e4SLinus Torvalds return 0; 25261da177e4SLinus Torvalds } 25271da177e4SLinus Torvalds 25281da177e4SLinus Torvalds static int selinux_file_mmap(struct file *file, unsigned long reqprot, 25291da177e4SLinus Torvalds unsigned long prot, unsigned long flags) 25301da177e4SLinus Torvalds { 25311da177e4SLinus Torvalds int rc; 25321da177e4SLinus Torvalds 25331da177e4SLinus Torvalds rc = secondary_ops->file_mmap(file, reqprot, prot, flags); 25341da177e4SLinus Torvalds if (rc) 25351da177e4SLinus Torvalds return rc; 25361da177e4SLinus Torvalds 25371da177e4SLinus Torvalds if (selinux_checkreqprot) 25381da177e4SLinus Torvalds prot = reqprot; 25391da177e4SLinus Torvalds 25401da177e4SLinus Torvalds return file_map_prot_check(file, prot, 25411da177e4SLinus Torvalds (flags & MAP_TYPE) == MAP_SHARED); 25421da177e4SLinus Torvalds } 25431da177e4SLinus Torvalds 25441da177e4SLinus Torvalds static int selinux_file_mprotect(struct vm_area_struct *vma, 25451da177e4SLinus Torvalds unsigned long reqprot, 25461da177e4SLinus Torvalds unsigned long prot) 25471da177e4SLinus Torvalds { 25481da177e4SLinus Torvalds int rc; 25491da177e4SLinus Torvalds 25501da177e4SLinus Torvalds rc = secondary_ops->file_mprotect(vma, reqprot, prot); 25511da177e4SLinus Torvalds if (rc) 25521da177e4SLinus Torvalds return rc; 25531da177e4SLinus Torvalds 25541da177e4SLinus Torvalds if (selinux_checkreqprot) 25551da177e4SLinus Torvalds prot = reqprot; 25561da177e4SLinus Torvalds 25571da177e4SLinus Torvalds #ifndef CONFIG_PPC32 2558db4c9641SStephen Smalley if ((prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) { 2559db4c9641SStephen Smalley rc = 0; 2560db4c9641SStephen Smalley if (vma->vm_start >= vma->vm_mm->start_brk && 2561db4c9641SStephen Smalley vma->vm_end <= vma->vm_mm->brk) { 2562db4c9641SStephen Smalley rc = task_has_perm(current, current, 2563db4c9641SStephen Smalley PROCESS__EXECHEAP); 2564db4c9641SStephen Smalley } else if (!vma->vm_file && 25656b992197SLorenzo Hernandez García-Hierro vma->vm_start <= vma->vm_mm->start_stack && 25666b992197SLorenzo Hernandez García-Hierro vma->vm_end >= vma->vm_mm->start_stack) { 25676b992197SLorenzo Hernandez García-Hierro rc = task_has_perm(current, current, PROCESS__EXECSTACK); 2568db4c9641SStephen Smalley } else if (vma->vm_file && vma->anon_vma) { 2569db4c9641SStephen Smalley /* 2570db4c9641SStephen Smalley * We are making executable a file mapping that has 2571db4c9641SStephen Smalley * had some COW done. Since pages might have been 2572db4c9641SStephen Smalley * written, check ability to execute the possibly 2573db4c9641SStephen Smalley * modified content. This typically should only 2574db4c9641SStephen Smalley * occur for text relocations. 2575db4c9641SStephen Smalley */ 2576db4c9641SStephen Smalley rc = file_has_perm(current, vma->vm_file, 2577db4c9641SStephen Smalley FILE__EXECMOD); 2578db4c9641SStephen Smalley } 25796b992197SLorenzo Hernandez García-Hierro if (rc) 25806b992197SLorenzo Hernandez García-Hierro return rc; 25816b992197SLorenzo Hernandez García-Hierro } 25821da177e4SLinus Torvalds #endif 25831da177e4SLinus Torvalds 25841da177e4SLinus Torvalds return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED); 25851da177e4SLinus Torvalds } 25861da177e4SLinus Torvalds 25871da177e4SLinus Torvalds static int selinux_file_lock(struct file *file, unsigned int cmd) 25881da177e4SLinus Torvalds { 25891da177e4SLinus Torvalds return file_has_perm(current, file, FILE__LOCK); 25901da177e4SLinus Torvalds } 25911da177e4SLinus Torvalds 25921da177e4SLinus Torvalds static int selinux_file_fcntl(struct file *file, unsigned int cmd, 25931da177e4SLinus Torvalds unsigned long arg) 25941da177e4SLinus Torvalds { 25951da177e4SLinus Torvalds int err = 0; 25961da177e4SLinus Torvalds 25971da177e4SLinus Torvalds switch (cmd) { 25981da177e4SLinus Torvalds case F_SETFL: 25993d5ff529SJosef Sipek if (!file->f_path.dentry || !file->f_path.dentry->d_inode) { 26001da177e4SLinus Torvalds err = -EINVAL; 26011da177e4SLinus Torvalds break; 26021da177e4SLinus Torvalds } 26031da177e4SLinus Torvalds 26041da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) { 26051da177e4SLinus Torvalds err = file_has_perm(current, file,FILE__WRITE); 26061da177e4SLinus Torvalds break; 26071da177e4SLinus Torvalds } 26081da177e4SLinus Torvalds /* fall through */ 26091da177e4SLinus Torvalds case F_SETOWN: 26101da177e4SLinus Torvalds case F_SETSIG: 26111da177e4SLinus Torvalds case F_GETFL: 26121da177e4SLinus Torvalds case F_GETOWN: 26131da177e4SLinus Torvalds case F_GETSIG: 26141da177e4SLinus Torvalds /* Just check FD__USE permission */ 26151da177e4SLinus Torvalds err = file_has_perm(current, file, 0); 26161da177e4SLinus Torvalds break; 26171da177e4SLinus Torvalds case F_GETLK: 26181da177e4SLinus Torvalds case F_SETLK: 26191da177e4SLinus Torvalds case F_SETLKW: 26201da177e4SLinus Torvalds #if BITS_PER_LONG == 32 26211da177e4SLinus Torvalds case F_GETLK64: 26221da177e4SLinus Torvalds case F_SETLK64: 26231da177e4SLinus Torvalds case F_SETLKW64: 26241da177e4SLinus Torvalds #endif 26253d5ff529SJosef Sipek if (!file->f_path.dentry || !file->f_path.dentry->d_inode) { 26261da177e4SLinus Torvalds err = -EINVAL; 26271da177e4SLinus Torvalds break; 26281da177e4SLinus Torvalds } 26291da177e4SLinus Torvalds err = file_has_perm(current, file, FILE__LOCK); 26301da177e4SLinus Torvalds break; 26311da177e4SLinus Torvalds } 26321da177e4SLinus Torvalds 26331da177e4SLinus Torvalds return err; 26341da177e4SLinus Torvalds } 26351da177e4SLinus Torvalds 26361da177e4SLinus Torvalds static int selinux_file_set_fowner(struct file *file) 26371da177e4SLinus Torvalds { 26381da177e4SLinus Torvalds struct task_security_struct *tsec; 26391da177e4SLinus Torvalds struct file_security_struct *fsec; 26401da177e4SLinus Torvalds 26411da177e4SLinus Torvalds tsec = current->security; 26421da177e4SLinus Torvalds fsec = file->f_security; 26431da177e4SLinus Torvalds fsec->fown_sid = tsec->sid; 26441da177e4SLinus Torvalds 26451da177e4SLinus Torvalds return 0; 26461da177e4SLinus Torvalds } 26471da177e4SLinus Torvalds 26481da177e4SLinus Torvalds static int selinux_file_send_sigiotask(struct task_struct *tsk, 26491da177e4SLinus Torvalds struct fown_struct *fown, int signum) 26501da177e4SLinus Torvalds { 26511da177e4SLinus Torvalds struct file *file; 26521da177e4SLinus Torvalds u32 perm; 26531da177e4SLinus Torvalds struct task_security_struct *tsec; 26541da177e4SLinus Torvalds struct file_security_struct *fsec; 26551da177e4SLinus Torvalds 26561da177e4SLinus Torvalds /* struct fown_struct is never outside the context of a struct file */ 2657*b385a144SRobert P. J. Day file = container_of(fown, struct file, f_owner); 26581da177e4SLinus Torvalds 26591da177e4SLinus Torvalds tsec = tsk->security; 26601da177e4SLinus Torvalds fsec = file->f_security; 26611da177e4SLinus Torvalds 26621da177e4SLinus Torvalds if (!signum) 26631da177e4SLinus Torvalds perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */ 26641da177e4SLinus Torvalds else 26651da177e4SLinus Torvalds perm = signal_to_av(signum); 26661da177e4SLinus Torvalds 26671da177e4SLinus Torvalds return avc_has_perm(fsec->fown_sid, tsec->sid, 26681da177e4SLinus Torvalds SECCLASS_PROCESS, perm, NULL); 26691da177e4SLinus Torvalds } 26701da177e4SLinus Torvalds 26711da177e4SLinus Torvalds static int selinux_file_receive(struct file *file) 26721da177e4SLinus Torvalds { 26731da177e4SLinus Torvalds return file_has_perm(current, file, file_to_av(file)); 26741da177e4SLinus Torvalds } 26751da177e4SLinus Torvalds 26761da177e4SLinus Torvalds /* task security operations */ 26771da177e4SLinus Torvalds 26781da177e4SLinus Torvalds static int selinux_task_create(unsigned long clone_flags) 26791da177e4SLinus Torvalds { 26801da177e4SLinus Torvalds int rc; 26811da177e4SLinus Torvalds 26821da177e4SLinus Torvalds rc = secondary_ops->task_create(clone_flags); 26831da177e4SLinus Torvalds if (rc) 26841da177e4SLinus Torvalds return rc; 26851da177e4SLinus Torvalds 26861da177e4SLinus Torvalds return task_has_perm(current, current, PROCESS__FORK); 26871da177e4SLinus Torvalds } 26881da177e4SLinus Torvalds 26891da177e4SLinus Torvalds static int selinux_task_alloc_security(struct task_struct *tsk) 26901da177e4SLinus Torvalds { 26911da177e4SLinus Torvalds struct task_security_struct *tsec1, *tsec2; 26921da177e4SLinus Torvalds int rc; 26931da177e4SLinus Torvalds 26941da177e4SLinus Torvalds tsec1 = current->security; 26951da177e4SLinus Torvalds 26961da177e4SLinus Torvalds rc = task_alloc_security(tsk); 26971da177e4SLinus Torvalds if (rc) 26981da177e4SLinus Torvalds return rc; 26991da177e4SLinus Torvalds tsec2 = tsk->security; 27001da177e4SLinus Torvalds 27011da177e4SLinus Torvalds tsec2->osid = tsec1->osid; 27021da177e4SLinus Torvalds tsec2->sid = tsec1->sid; 27031da177e4SLinus Torvalds 270428eba5bfSMichael LeMay /* Retain the exec, fs, key, and sock SIDs across fork */ 27051da177e4SLinus Torvalds tsec2->exec_sid = tsec1->exec_sid; 27061da177e4SLinus Torvalds tsec2->create_sid = tsec1->create_sid; 270728eba5bfSMichael LeMay tsec2->keycreate_sid = tsec1->keycreate_sid; 270842c3e03eSEric Paris tsec2->sockcreate_sid = tsec1->sockcreate_sid; 27091da177e4SLinus Torvalds 27101da177e4SLinus Torvalds /* Retain ptracer SID across fork, if any. 27111da177e4SLinus Torvalds This will be reset by the ptrace hook upon any 27121da177e4SLinus Torvalds subsequent ptrace_attach operations. */ 27131da177e4SLinus Torvalds tsec2->ptrace_sid = tsec1->ptrace_sid; 27141da177e4SLinus Torvalds 27151da177e4SLinus Torvalds return 0; 27161da177e4SLinus Torvalds } 27171da177e4SLinus Torvalds 27181da177e4SLinus Torvalds static void selinux_task_free_security(struct task_struct *tsk) 27191da177e4SLinus Torvalds { 27201da177e4SLinus Torvalds task_free_security(tsk); 27211da177e4SLinus Torvalds } 27221da177e4SLinus Torvalds 27231da177e4SLinus Torvalds static int selinux_task_setuid(uid_t id0, uid_t id1, uid_t id2, int flags) 27241da177e4SLinus Torvalds { 27251da177e4SLinus Torvalds /* Since setuid only affects the current process, and 27261da177e4SLinus Torvalds since the SELinux controls are not based on the Linux 27271da177e4SLinus Torvalds identity attributes, SELinux does not need to control 27281da177e4SLinus Torvalds this operation. However, SELinux does control the use 27291da177e4SLinus Torvalds of the CAP_SETUID and CAP_SETGID capabilities using the 27301da177e4SLinus Torvalds capable hook. */ 27311da177e4SLinus Torvalds return 0; 27321da177e4SLinus Torvalds } 27331da177e4SLinus Torvalds 27341da177e4SLinus Torvalds static int selinux_task_post_setuid(uid_t id0, uid_t id1, uid_t id2, int flags) 27351da177e4SLinus Torvalds { 27361da177e4SLinus Torvalds return secondary_ops->task_post_setuid(id0,id1,id2,flags); 27371da177e4SLinus Torvalds } 27381da177e4SLinus Torvalds 27391da177e4SLinus Torvalds static int selinux_task_setgid(gid_t id0, gid_t id1, gid_t id2, int flags) 27401da177e4SLinus Torvalds { 27411da177e4SLinus Torvalds /* See the comment for setuid above. */ 27421da177e4SLinus Torvalds return 0; 27431da177e4SLinus Torvalds } 27441da177e4SLinus Torvalds 27451da177e4SLinus Torvalds static int selinux_task_setpgid(struct task_struct *p, pid_t pgid) 27461da177e4SLinus Torvalds { 27471da177e4SLinus Torvalds return task_has_perm(current, p, PROCESS__SETPGID); 27481da177e4SLinus Torvalds } 27491da177e4SLinus Torvalds 27501da177e4SLinus Torvalds static int selinux_task_getpgid(struct task_struct *p) 27511da177e4SLinus Torvalds { 27521da177e4SLinus Torvalds return task_has_perm(current, p, PROCESS__GETPGID); 27531da177e4SLinus Torvalds } 27541da177e4SLinus Torvalds 27551da177e4SLinus Torvalds static int selinux_task_getsid(struct task_struct *p) 27561da177e4SLinus Torvalds { 27571da177e4SLinus Torvalds return task_has_perm(current, p, PROCESS__GETSESSION); 27581da177e4SLinus Torvalds } 27591da177e4SLinus Torvalds 2760f9008e4cSDavid Quigley static void selinux_task_getsecid(struct task_struct *p, u32 *secid) 2761f9008e4cSDavid Quigley { 2762f9008e4cSDavid Quigley selinux_get_task_sid(p, secid); 2763f9008e4cSDavid Quigley } 2764f9008e4cSDavid Quigley 27651da177e4SLinus Torvalds static int selinux_task_setgroups(struct group_info *group_info) 27661da177e4SLinus Torvalds { 27671da177e4SLinus Torvalds /* See the comment for setuid above. */ 27681da177e4SLinus Torvalds return 0; 27691da177e4SLinus Torvalds } 27701da177e4SLinus Torvalds 27711da177e4SLinus Torvalds static int selinux_task_setnice(struct task_struct *p, int nice) 27721da177e4SLinus Torvalds { 27731da177e4SLinus Torvalds int rc; 27741da177e4SLinus Torvalds 27751da177e4SLinus Torvalds rc = secondary_ops->task_setnice(p, nice); 27761da177e4SLinus Torvalds if (rc) 27771da177e4SLinus Torvalds return rc; 27781da177e4SLinus Torvalds 27791da177e4SLinus Torvalds return task_has_perm(current,p, PROCESS__SETSCHED); 27801da177e4SLinus Torvalds } 27811da177e4SLinus Torvalds 278203e68060SJames Morris static int selinux_task_setioprio(struct task_struct *p, int ioprio) 278303e68060SJames Morris { 278403e68060SJames Morris return task_has_perm(current, p, PROCESS__SETSCHED); 278503e68060SJames Morris } 278603e68060SJames Morris 2787a1836a42SDavid Quigley static int selinux_task_getioprio(struct task_struct *p) 2788a1836a42SDavid Quigley { 2789a1836a42SDavid Quigley return task_has_perm(current, p, PROCESS__GETSCHED); 2790a1836a42SDavid Quigley } 2791a1836a42SDavid Quigley 27921da177e4SLinus Torvalds static int selinux_task_setrlimit(unsigned int resource, struct rlimit *new_rlim) 27931da177e4SLinus Torvalds { 27941da177e4SLinus Torvalds struct rlimit *old_rlim = current->signal->rlim + resource; 27951da177e4SLinus Torvalds int rc; 27961da177e4SLinus Torvalds 27971da177e4SLinus Torvalds rc = secondary_ops->task_setrlimit(resource, new_rlim); 27981da177e4SLinus Torvalds if (rc) 27991da177e4SLinus Torvalds return rc; 28001da177e4SLinus Torvalds 28011da177e4SLinus Torvalds /* Control the ability to change the hard limit (whether 28021da177e4SLinus Torvalds lowering or raising it), so that the hard limit can 28031da177e4SLinus Torvalds later be used as a safe reset point for the soft limit 28041da177e4SLinus Torvalds upon context transitions. See selinux_bprm_apply_creds. */ 28051da177e4SLinus Torvalds if (old_rlim->rlim_max != new_rlim->rlim_max) 28061da177e4SLinus Torvalds return task_has_perm(current, current, PROCESS__SETRLIMIT); 28071da177e4SLinus Torvalds 28081da177e4SLinus Torvalds return 0; 28091da177e4SLinus Torvalds } 28101da177e4SLinus Torvalds 28111da177e4SLinus Torvalds static int selinux_task_setscheduler(struct task_struct *p, int policy, struct sched_param *lp) 28121da177e4SLinus Torvalds { 28131da177e4SLinus Torvalds return task_has_perm(current, p, PROCESS__SETSCHED); 28141da177e4SLinus Torvalds } 28151da177e4SLinus Torvalds 28161da177e4SLinus Torvalds static int selinux_task_getscheduler(struct task_struct *p) 28171da177e4SLinus Torvalds { 28181da177e4SLinus Torvalds return task_has_perm(current, p, PROCESS__GETSCHED); 28191da177e4SLinus Torvalds } 28201da177e4SLinus Torvalds 282135601547SDavid Quigley static int selinux_task_movememory(struct task_struct *p) 282235601547SDavid Quigley { 282335601547SDavid Quigley return task_has_perm(current, p, PROCESS__SETSCHED); 282435601547SDavid Quigley } 282535601547SDavid Quigley 2826f9008e4cSDavid Quigley static int selinux_task_kill(struct task_struct *p, struct siginfo *info, 2827f9008e4cSDavid Quigley int sig, u32 secid) 28281da177e4SLinus Torvalds { 28291da177e4SLinus Torvalds u32 perm; 28301da177e4SLinus Torvalds int rc; 2831f9008e4cSDavid Quigley struct task_security_struct *tsec; 28321da177e4SLinus Torvalds 2833f9008e4cSDavid Quigley rc = secondary_ops->task_kill(p, info, sig, secid); 28341da177e4SLinus Torvalds if (rc) 28351da177e4SLinus Torvalds return rc; 28361da177e4SLinus Torvalds 2837621d3121SOleg Nesterov if (info != SEND_SIG_NOINFO && (is_si_special(info) || SI_FROMKERNEL(info))) 28381da177e4SLinus Torvalds return 0; 28391da177e4SLinus Torvalds 28401da177e4SLinus Torvalds if (!sig) 28411da177e4SLinus Torvalds perm = PROCESS__SIGNULL; /* null signal; existence test */ 28421da177e4SLinus Torvalds else 28431da177e4SLinus Torvalds perm = signal_to_av(sig); 2844f9008e4cSDavid Quigley tsec = p->security; 2845f9008e4cSDavid Quigley if (secid) 2846f9008e4cSDavid Quigley rc = avc_has_perm(secid, tsec->sid, SECCLASS_PROCESS, perm, NULL); 2847f9008e4cSDavid Quigley else 2848f9008e4cSDavid Quigley rc = task_has_perm(current, p, perm); 2849f9008e4cSDavid Quigley return rc; 28501da177e4SLinus Torvalds } 28511da177e4SLinus Torvalds 28521da177e4SLinus Torvalds static int selinux_task_prctl(int option, 28531da177e4SLinus Torvalds unsigned long arg2, 28541da177e4SLinus Torvalds unsigned long arg3, 28551da177e4SLinus Torvalds unsigned long arg4, 28561da177e4SLinus Torvalds unsigned long arg5) 28571da177e4SLinus Torvalds { 28581da177e4SLinus Torvalds /* The current prctl operations do not appear to require 28591da177e4SLinus Torvalds any SELinux controls since they merely observe or modify 28601da177e4SLinus Torvalds the state of the current process. */ 28611da177e4SLinus Torvalds return 0; 28621da177e4SLinus Torvalds } 28631da177e4SLinus Torvalds 28641da177e4SLinus Torvalds static int selinux_task_wait(struct task_struct *p) 28651da177e4SLinus Torvalds { 28661da177e4SLinus Torvalds u32 perm; 28671da177e4SLinus Torvalds 28681da177e4SLinus Torvalds perm = signal_to_av(p->exit_signal); 28691da177e4SLinus Torvalds 28701da177e4SLinus Torvalds return task_has_perm(p, current, perm); 28711da177e4SLinus Torvalds } 28721da177e4SLinus Torvalds 28731da177e4SLinus Torvalds static void selinux_task_reparent_to_init(struct task_struct *p) 28741da177e4SLinus Torvalds { 28751da177e4SLinus Torvalds struct task_security_struct *tsec; 28761da177e4SLinus Torvalds 28771da177e4SLinus Torvalds secondary_ops->task_reparent_to_init(p); 28781da177e4SLinus Torvalds 28791da177e4SLinus Torvalds tsec = p->security; 28801da177e4SLinus Torvalds tsec->osid = tsec->sid; 28811da177e4SLinus Torvalds tsec->sid = SECINITSID_KERNEL; 28821da177e4SLinus Torvalds return; 28831da177e4SLinus Torvalds } 28841da177e4SLinus Torvalds 28851da177e4SLinus Torvalds static void selinux_task_to_inode(struct task_struct *p, 28861da177e4SLinus Torvalds struct inode *inode) 28871da177e4SLinus Torvalds { 28881da177e4SLinus Torvalds struct task_security_struct *tsec = p->security; 28891da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 28901da177e4SLinus Torvalds 28911da177e4SLinus Torvalds isec->sid = tsec->sid; 28921da177e4SLinus Torvalds isec->initialized = 1; 28931da177e4SLinus Torvalds return; 28941da177e4SLinus Torvalds } 28951da177e4SLinus Torvalds 28961da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 289767f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv4(struct sk_buff *skb, 289867f83cbfSVenkat Yekkirala struct avc_audit_data *ad, u8 *proto) 28991da177e4SLinus Torvalds { 29001da177e4SLinus Torvalds int offset, ihlen, ret = -EINVAL; 29011da177e4SLinus Torvalds struct iphdr _iph, *ih; 29021da177e4SLinus Torvalds 29031da177e4SLinus Torvalds offset = skb->nh.raw - skb->data; 29041da177e4SLinus Torvalds ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph); 29051da177e4SLinus Torvalds if (ih == NULL) 29061da177e4SLinus Torvalds goto out; 29071da177e4SLinus Torvalds 29081da177e4SLinus Torvalds ihlen = ih->ihl * 4; 29091da177e4SLinus Torvalds if (ihlen < sizeof(_iph)) 29101da177e4SLinus Torvalds goto out; 29111da177e4SLinus Torvalds 29121da177e4SLinus Torvalds ad->u.net.v4info.saddr = ih->saddr; 29131da177e4SLinus Torvalds ad->u.net.v4info.daddr = ih->daddr; 29141da177e4SLinus Torvalds ret = 0; 29151da177e4SLinus Torvalds 291667f83cbfSVenkat Yekkirala if (proto) 291767f83cbfSVenkat Yekkirala *proto = ih->protocol; 291867f83cbfSVenkat Yekkirala 29191da177e4SLinus Torvalds switch (ih->protocol) { 29201da177e4SLinus Torvalds case IPPROTO_TCP: { 29211da177e4SLinus Torvalds struct tcphdr _tcph, *th; 29221da177e4SLinus Torvalds 29231da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 29241da177e4SLinus Torvalds break; 29251da177e4SLinus Torvalds 29261da177e4SLinus Torvalds offset += ihlen; 29271da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 29281da177e4SLinus Torvalds if (th == NULL) 29291da177e4SLinus Torvalds break; 29301da177e4SLinus Torvalds 29311da177e4SLinus Torvalds ad->u.net.sport = th->source; 29321da177e4SLinus Torvalds ad->u.net.dport = th->dest; 29331da177e4SLinus Torvalds break; 29341da177e4SLinus Torvalds } 29351da177e4SLinus Torvalds 29361da177e4SLinus Torvalds case IPPROTO_UDP: { 29371da177e4SLinus Torvalds struct udphdr _udph, *uh; 29381da177e4SLinus Torvalds 29391da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 29401da177e4SLinus Torvalds break; 29411da177e4SLinus Torvalds 29421da177e4SLinus Torvalds offset += ihlen; 29431da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 29441da177e4SLinus Torvalds if (uh == NULL) 29451da177e4SLinus Torvalds break; 29461da177e4SLinus Torvalds 29471da177e4SLinus Torvalds ad->u.net.sport = uh->source; 29481da177e4SLinus Torvalds ad->u.net.dport = uh->dest; 29491da177e4SLinus Torvalds break; 29501da177e4SLinus Torvalds } 29511da177e4SLinus Torvalds 29522ee92d46SJames Morris case IPPROTO_DCCP: { 29532ee92d46SJames Morris struct dccp_hdr _dccph, *dh; 29542ee92d46SJames Morris 29552ee92d46SJames Morris if (ntohs(ih->frag_off) & IP_OFFSET) 29562ee92d46SJames Morris break; 29572ee92d46SJames Morris 29582ee92d46SJames Morris offset += ihlen; 29592ee92d46SJames Morris dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); 29602ee92d46SJames Morris if (dh == NULL) 29612ee92d46SJames Morris break; 29622ee92d46SJames Morris 29632ee92d46SJames Morris ad->u.net.sport = dh->dccph_sport; 29642ee92d46SJames Morris ad->u.net.dport = dh->dccph_dport; 29652ee92d46SJames Morris break; 29662ee92d46SJames Morris } 29672ee92d46SJames Morris 29681da177e4SLinus Torvalds default: 29691da177e4SLinus Torvalds break; 29701da177e4SLinus Torvalds } 29711da177e4SLinus Torvalds out: 29721da177e4SLinus Torvalds return ret; 29731da177e4SLinus Torvalds } 29741da177e4SLinus Torvalds 29751da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 29761da177e4SLinus Torvalds 29771da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 297867f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv6(struct sk_buff *skb, 297967f83cbfSVenkat Yekkirala struct avc_audit_data *ad, u8 *proto) 29801da177e4SLinus Torvalds { 29811da177e4SLinus Torvalds u8 nexthdr; 29821da177e4SLinus Torvalds int ret = -EINVAL, offset; 29831da177e4SLinus Torvalds struct ipv6hdr _ipv6h, *ip6; 29841da177e4SLinus Torvalds 29851da177e4SLinus Torvalds offset = skb->nh.raw - skb->data; 29861da177e4SLinus Torvalds ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h); 29871da177e4SLinus Torvalds if (ip6 == NULL) 29881da177e4SLinus Torvalds goto out; 29891da177e4SLinus Torvalds 29901da177e4SLinus Torvalds ipv6_addr_copy(&ad->u.net.v6info.saddr, &ip6->saddr); 29911da177e4SLinus Torvalds ipv6_addr_copy(&ad->u.net.v6info.daddr, &ip6->daddr); 29921da177e4SLinus Torvalds ret = 0; 29931da177e4SLinus Torvalds 29941da177e4SLinus Torvalds nexthdr = ip6->nexthdr; 29951da177e4SLinus Torvalds offset += sizeof(_ipv6h); 29960d3d077cSHerbert Xu offset = ipv6_skip_exthdr(skb, offset, &nexthdr); 29971da177e4SLinus Torvalds if (offset < 0) 29981da177e4SLinus Torvalds goto out; 29991da177e4SLinus Torvalds 300067f83cbfSVenkat Yekkirala if (proto) 300167f83cbfSVenkat Yekkirala *proto = nexthdr; 300267f83cbfSVenkat Yekkirala 30031da177e4SLinus Torvalds switch (nexthdr) { 30041da177e4SLinus Torvalds case IPPROTO_TCP: { 30051da177e4SLinus Torvalds struct tcphdr _tcph, *th; 30061da177e4SLinus Torvalds 30071da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 30081da177e4SLinus Torvalds if (th == NULL) 30091da177e4SLinus Torvalds break; 30101da177e4SLinus Torvalds 30111da177e4SLinus Torvalds ad->u.net.sport = th->source; 30121da177e4SLinus Torvalds ad->u.net.dport = th->dest; 30131da177e4SLinus Torvalds break; 30141da177e4SLinus Torvalds } 30151da177e4SLinus Torvalds 30161da177e4SLinus Torvalds case IPPROTO_UDP: { 30171da177e4SLinus Torvalds struct udphdr _udph, *uh; 30181da177e4SLinus Torvalds 30191da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 30201da177e4SLinus Torvalds if (uh == NULL) 30211da177e4SLinus Torvalds break; 30221da177e4SLinus Torvalds 30231da177e4SLinus Torvalds ad->u.net.sport = uh->source; 30241da177e4SLinus Torvalds ad->u.net.dport = uh->dest; 30251da177e4SLinus Torvalds break; 30261da177e4SLinus Torvalds } 30271da177e4SLinus Torvalds 30282ee92d46SJames Morris case IPPROTO_DCCP: { 30292ee92d46SJames Morris struct dccp_hdr _dccph, *dh; 30302ee92d46SJames Morris 30312ee92d46SJames Morris dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); 30322ee92d46SJames Morris if (dh == NULL) 30332ee92d46SJames Morris break; 30342ee92d46SJames Morris 30352ee92d46SJames Morris ad->u.net.sport = dh->dccph_sport; 30362ee92d46SJames Morris ad->u.net.dport = dh->dccph_dport; 30372ee92d46SJames Morris break; 30382ee92d46SJames Morris } 30392ee92d46SJames Morris 30401da177e4SLinus Torvalds /* includes fragments */ 30411da177e4SLinus Torvalds default: 30421da177e4SLinus Torvalds break; 30431da177e4SLinus Torvalds } 30441da177e4SLinus Torvalds out: 30451da177e4SLinus Torvalds return ret; 30461da177e4SLinus Torvalds } 30471da177e4SLinus Torvalds 30481da177e4SLinus Torvalds #endif /* IPV6 */ 30491da177e4SLinus Torvalds 30501da177e4SLinus Torvalds static int selinux_parse_skb(struct sk_buff *skb, struct avc_audit_data *ad, 305167f83cbfSVenkat Yekkirala char **addrp, int *len, int src, u8 *proto) 30521da177e4SLinus Torvalds { 30531da177e4SLinus Torvalds int ret = 0; 30541da177e4SLinus Torvalds 30551da177e4SLinus Torvalds switch (ad->u.net.family) { 30561da177e4SLinus Torvalds case PF_INET: 305767f83cbfSVenkat Yekkirala ret = selinux_parse_skb_ipv4(skb, ad, proto); 30581da177e4SLinus Torvalds if (ret || !addrp) 30591da177e4SLinus Torvalds break; 30601da177e4SLinus Torvalds *len = 4; 30611da177e4SLinus Torvalds *addrp = (char *)(src ? &ad->u.net.v4info.saddr : 30621da177e4SLinus Torvalds &ad->u.net.v4info.daddr); 30631da177e4SLinus Torvalds break; 30641da177e4SLinus Torvalds 30651da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 30661da177e4SLinus Torvalds case PF_INET6: 306767f83cbfSVenkat Yekkirala ret = selinux_parse_skb_ipv6(skb, ad, proto); 30681da177e4SLinus Torvalds if (ret || !addrp) 30691da177e4SLinus Torvalds break; 30701da177e4SLinus Torvalds *len = 16; 30711da177e4SLinus Torvalds *addrp = (char *)(src ? &ad->u.net.v6info.saddr : 30721da177e4SLinus Torvalds &ad->u.net.v6info.daddr); 30731da177e4SLinus Torvalds break; 30741da177e4SLinus Torvalds #endif /* IPV6 */ 30751da177e4SLinus Torvalds default: 30761da177e4SLinus Torvalds break; 30771da177e4SLinus Torvalds } 30781da177e4SLinus Torvalds 30791da177e4SLinus Torvalds return ret; 30801da177e4SLinus Torvalds } 30811da177e4SLinus Torvalds 30821da177e4SLinus Torvalds /* socket security operations */ 30831da177e4SLinus Torvalds static int socket_has_perm(struct task_struct *task, struct socket *sock, 30841da177e4SLinus Torvalds u32 perms) 30851da177e4SLinus Torvalds { 30861da177e4SLinus Torvalds struct inode_security_struct *isec; 30871da177e4SLinus Torvalds struct task_security_struct *tsec; 30881da177e4SLinus Torvalds struct avc_audit_data ad; 30891da177e4SLinus Torvalds int err = 0; 30901da177e4SLinus Torvalds 30911da177e4SLinus Torvalds tsec = task->security; 30921da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 30931da177e4SLinus Torvalds 30941da177e4SLinus Torvalds if (isec->sid == SECINITSID_KERNEL) 30951da177e4SLinus Torvalds goto out; 30961da177e4SLinus Torvalds 30971da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,NET); 30981da177e4SLinus Torvalds ad.u.net.sk = sock->sk; 30991da177e4SLinus Torvalds err = avc_has_perm(tsec->sid, isec->sid, isec->sclass, perms, &ad); 31001da177e4SLinus Torvalds 31011da177e4SLinus Torvalds out: 31021da177e4SLinus Torvalds return err; 31031da177e4SLinus Torvalds } 31041da177e4SLinus Torvalds 31051da177e4SLinus Torvalds static int selinux_socket_create(int family, int type, 31061da177e4SLinus Torvalds int protocol, int kern) 31071da177e4SLinus Torvalds { 31081da177e4SLinus Torvalds int err = 0; 31091da177e4SLinus Torvalds struct task_security_struct *tsec; 311042c3e03eSEric Paris u32 newsid; 31111da177e4SLinus Torvalds 31121da177e4SLinus Torvalds if (kern) 31131da177e4SLinus Torvalds goto out; 31141da177e4SLinus Torvalds 31151da177e4SLinus Torvalds tsec = current->security; 311642c3e03eSEric Paris newsid = tsec->sockcreate_sid ? : tsec->sid; 311742c3e03eSEric Paris err = avc_has_perm(tsec->sid, newsid, 31181da177e4SLinus Torvalds socket_type_to_security_class(family, type, 31191da177e4SLinus Torvalds protocol), SOCKET__CREATE, NULL); 31201da177e4SLinus Torvalds 31211da177e4SLinus Torvalds out: 31221da177e4SLinus Torvalds return err; 31231da177e4SLinus Torvalds } 31241da177e4SLinus Torvalds 31257420ed23SVenkat Yekkirala static int selinux_socket_post_create(struct socket *sock, int family, 31261da177e4SLinus Torvalds int type, int protocol, int kern) 31271da177e4SLinus Torvalds { 31287420ed23SVenkat Yekkirala int err = 0; 31291da177e4SLinus Torvalds struct inode_security_struct *isec; 31301da177e4SLinus Torvalds struct task_security_struct *tsec; 3131892c141eSVenkat Yekkirala struct sk_security_struct *sksec; 313242c3e03eSEric Paris u32 newsid; 31331da177e4SLinus Torvalds 31341da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 31351da177e4SLinus Torvalds 31361da177e4SLinus Torvalds tsec = current->security; 313742c3e03eSEric Paris newsid = tsec->sockcreate_sid ? : tsec->sid; 31381da177e4SLinus Torvalds isec->sclass = socket_type_to_security_class(family, type, protocol); 313942c3e03eSEric Paris isec->sid = kern ? SECINITSID_KERNEL : newsid; 31401da177e4SLinus Torvalds isec->initialized = 1; 31411da177e4SLinus Torvalds 3142892c141eSVenkat Yekkirala if (sock->sk) { 3143892c141eSVenkat Yekkirala sksec = sock->sk->sk_security; 3144892c141eSVenkat Yekkirala sksec->sid = isec->sid; 31459f2ad665SPaul Moore err = selinux_netlbl_socket_post_create(sock); 3146892c141eSVenkat Yekkirala } 3147892c141eSVenkat Yekkirala 31487420ed23SVenkat Yekkirala return err; 31491da177e4SLinus Torvalds } 31501da177e4SLinus Torvalds 31511da177e4SLinus Torvalds /* Range of port numbers used to automatically bind. 31521da177e4SLinus Torvalds Need to determine whether we should perform a name_bind 31531da177e4SLinus Torvalds permission check between the socket and the port number. */ 31541da177e4SLinus Torvalds #define ip_local_port_range_0 sysctl_local_port_range[0] 31551da177e4SLinus Torvalds #define ip_local_port_range_1 sysctl_local_port_range[1] 31561da177e4SLinus Torvalds 31571da177e4SLinus Torvalds static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen) 31581da177e4SLinus Torvalds { 31591da177e4SLinus Torvalds u16 family; 31601da177e4SLinus Torvalds int err; 31611da177e4SLinus Torvalds 31621da177e4SLinus Torvalds err = socket_has_perm(current, sock, SOCKET__BIND); 31631da177e4SLinus Torvalds if (err) 31641da177e4SLinus Torvalds goto out; 31651da177e4SLinus Torvalds 31661da177e4SLinus Torvalds /* 31671da177e4SLinus Torvalds * If PF_INET or PF_INET6, check name_bind permission for the port. 316813402580SJames Morris * Multiple address binding for SCTP is not supported yet: we just 316913402580SJames Morris * check the first address now. 31701da177e4SLinus Torvalds */ 31711da177e4SLinus Torvalds family = sock->sk->sk_family; 31721da177e4SLinus Torvalds if (family == PF_INET || family == PF_INET6) { 31731da177e4SLinus Torvalds char *addrp; 31741da177e4SLinus Torvalds struct inode_security_struct *isec; 31751da177e4SLinus Torvalds struct task_security_struct *tsec; 31761da177e4SLinus Torvalds struct avc_audit_data ad; 31771da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 31781da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 31791da177e4SLinus Torvalds unsigned short snum; 31801da177e4SLinus Torvalds struct sock *sk = sock->sk; 31811da177e4SLinus Torvalds u32 sid, node_perm, addrlen; 31821da177e4SLinus Torvalds 31831da177e4SLinus Torvalds tsec = current->security; 31841da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 31851da177e4SLinus Torvalds 31861da177e4SLinus Torvalds if (family == PF_INET) { 31871da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 31881da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 31891da177e4SLinus Torvalds addrlen = sizeof(addr4->sin_addr.s_addr); 31901da177e4SLinus Torvalds addrp = (char *)&addr4->sin_addr.s_addr; 31911da177e4SLinus Torvalds } else { 31921da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 31931da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 31941da177e4SLinus Torvalds addrlen = sizeof(addr6->sin6_addr.s6_addr); 31951da177e4SLinus Torvalds addrp = (char *)&addr6->sin6_addr.s6_addr; 31961da177e4SLinus Torvalds } 31971da177e4SLinus Torvalds 31981da177e4SLinus Torvalds if (snum&&(snum < max(PROT_SOCK,ip_local_port_range_0) || 31991da177e4SLinus Torvalds snum > ip_local_port_range_1)) { 32001da177e4SLinus Torvalds err = security_port_sid(sk->sk_family, sk->sk_type, 32011da177e4SLinus Torvalds sk->sk_protocol, snum, &sid); 32021da177e4SLinus Torvalds if (err) 32031da177e4SLinus Torvalds goto out; 32041da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,NET); 32051da177e4SLinus Torvalds ad.u.net.sport = htons(snum); 32061da177e4SLinus Torvalds ad.u.net.family = family; 32071da177e4SLinus Torvalds err = avc_has_perm(isec->sid, sid, 32081da177e4SLinus Torvalds isec->sclass, 32091da177e4SLinus Torvalds SOCKET__NAME_BIND, &ad); 32101da177e4SLinus Torvalds if (err) 32111da177e4SLinus Torvalds goto out; 32121da177e4SLinus Torvalds } 32131da177e4SLinus Torvalds 321413402580SJames Morris switch(isec->sclass) { 321513402580SJames Morris case SECCLASS_TCP_SOCKET: 32161da177e4SLinus Torvalds node_perm = TCP_SOCKET__NODE_BIND; 32171da177e4SLinus Torvalds break; 32181da177e4SLinus Torvalds 321913402580SJames Morris case SECCLASS_UDP_SOCKET: 32201da177e4SLinus Torvalds node_perm = UDP_SOCKET__NODE_BIND; 32211da177e4SLinus Torvalds break; 32221da177e4SLinus Torvalds 32232ee92d46SJames Morris case SECCLASS_DCCP_SOCKET: 32242ee92d46SJames Morris node_perm = DCCP_SOCKET__NODE_BIND; 32252ee92d46SJames Morris break; 32262ee92d46SJames Morris 32271da177e4SLinus Torvalds default: 32281da177e4SLinus Torvalds node_perm = RAWIP_SOCKET__NODE_BIND; 32291da177e4SLinus Torvalds break; 32301da177e4SLinus Torvalds } 32311da177e4SLinus Torvalds 32321da177e4SLinus Torvalds err = security_node_sid(family, addrp, addrlen, &sid); 32331da177e4SLinus Torvalds if (err) 32341da177e4SLinus Torvalds goto out; 32351da177e4SLinus Torvalds 32361da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,NET); 32371da177e4SLinus Torvalds ad.u.net.sport = htons(snum); 32381da177e4SLinus Torvalds ad.u.net.family = family; 32391da177e4SLinus Torvalds 32401da177e4SLinus Torvalds if (family == PF_INET) 32411da177e4SLinus Torvalds ad.u.net.v4info.saddr = addr4->sin_addr.s_addr; 32421da177e4SLinus Torvalds else 32431da177e4SLinus Torvalds ipv6_addr_copy(&ad.u.net.v6info.saddr, &addr6->sin6_addr); 32441da177e4SLinus Torvalds 32451da177e4SLinus Torvalds err = avc_has_perm(isec->sid, sid, 32461da177e4SLinus Torvalds isec->sclass, node_perm, &ad); 32471da177e4SLinus Torvalds if (err) 32481da177e4SLinus Torvalds goto out; 32491da177e4SLinus Torvalds } 32501da177e4SLinus Torvalds out: 32511da177e4SLinus Torvalds return err; 32521da177e4SLinus Torvalds } 32531da177e4SLinus Torvalds 32541da177e4SLinus Torvalds static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen) 32551da177e4SLinus Torvalds { 32561da177e4SLinus Torvalds struct inode_security_struct *isec; 32571da177e4SLinus Torvalds int err; 32581da177e4SLinus Torvalds 32591da177e4SLinus Torvalds err = socket_has_perm(current, sock, SOCKET__CONNECT); 32601da177e4SLinus Torvalds if (err) 32611da177e4SLinus Torvalds return err; 32621da177e4SLinus Torvalds 32631da177e4SLinus Torvalds /* 32642ee92d46SJames Morris * If a TCP or DCCP socket, check name_connect permission for the port. 32651da177e4SLinus Torvalds */ 32661da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 32672ee92d46SJames Morris if (isec->sclass == SECCLASS_TCP_SOCKET || 32682ee92d46SJames Morris isec->sclass == SECCLASS_DCCP_SOCKET) { 32691da177e4SLinus Torvalds struct sock *sk = sock->sk; 32701da177e4SLinus Torvalds struct avc_audit_data ad; 32711da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 32721da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 32731da177e4SLinus Torvalds unsigned short snum; 32742ee92d46SJames Morris u32 sid, perm; 32751da177e4SLinus Torvalds 32761da177e4SLinus Torvalds if (sk->sk_family == PF_INET) { 32771da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 3278911656f8SStephen Smalley if (addrlen < sizeof(struct sockaddr_in)) 32791da177e4SLinus Torvalds return -EINVAL; 32801da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 32811da177e4SLinus Torvalds } else { 32821da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 3283911656f8SStephen Smalley if (addrlen < SIN6_LEN_RFC2133) 32841da177e4SLinus Torvalds return -EINVAL; 32851da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 32861da177e4SLinus Torvalds } 32871da177e4SLinus Torvalds 32881da177e4SLinus Torvalds err = security_port_sid(sk->sk_family, sk->sk_type, 32891da177e4SLinus Torvalds sk->sk_protocol, snum, &sid); 32901da177e4SLinus Torvalds if (err) 32911da177e4SLinus Torvalds goto out; 32921da177e4SLinus Torvalds 32932ee92d46SJames Morris perm = (isec->sclass == SECCLASS_TCP_SOCKET) ? 32942ee92d46SJames Morris TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT; 32952ee92d46SJames Morris 32961da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,NET); 32971da177e4SLinus Torvalds ad.u.net.dport = htons(snum); 32981da177e4SLinus Torvalds ad.u.net.family = sk->sk_family; 32992ee92d46SJames Morris err = avc_has_perm(isec->sid, sid, isec->sclass, perm, &ad); 33001da177e4SLinus Torvalds if (err) 33011da177e4SLinus Torvalds goto out; 33021da177e4SLinus Torvalds } 33031da177e4SLinus Torvalds 33041da177e4SLinus Torvalds out: 33051da177e4SLinus Torvalds return err; 33061da177e4SLinus Torvalds } 33071da177e4SLinus Torvalds 33081da177e4SLinus Torvalds static int selinux_socket_listen(struct socket *sock, int backlog) 33091da177e4SLinus Torvalds { 33101da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__LISTEN); 33111da177e4SLinus Torvalds } 33121da177e4SLinus Torvalds 33131da177e4SLinus Torvalds static int selinux_socket_accept(struct socket *sock, struct socket *newsock) 33141da177e4SLinus Torvalds { 33151da177e4SLinus Torvalds int err; 33161da177e4SLinus Torvalds struct inode_security_struct *isec; 33171da177e4SLinus Torvalds struct inode_security_struct *newisec; 33181da177e4SLinus Torvalds 33191da177e4SLinus Torvalds err = socket_has_perm(current, sock, SOCKET__ACCEPT); 33201da177e4SLinus Torvalds if (err) 33211da177e4SLinus Torvalds return err; 33221da177e4SLinus Torvalds 33231da177e4SLinus Torvalds newisec = SOCK_INODE(newsock)->i_security; 33241da177e4SLinus Torvalds 33251da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 33261da177e4SLinus Torvalds newisec->sclass = isec->sclass; 33271da177e4SLinus Torvalds newisec->sid = isec->sid; 33281da177e4SLinus Torvalds newisec->initialized = 1; 33291da177e4SLinus Torvalds 33301da177e4SLinus Torvalds return 0; 33311da177e4SLinus Torvalds } 33321da177e4SLinus Torvalds 33331da177e4SLinus Torvalds static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg, 33341da177e4SLinus Torvalds int size) 33351da177e4SLinus Torvalds { 33367420ed23SVenkat Yekkirala int rc; 33377420ed23SVenkat Yekkirala 33387420ed23SVenkat Yekkirala rc = socket_has_perm(current, sock, SOCKET__WRITE); 33397420ed23SVenkat Yekkirala if (rc) 33407420ed23SVenkat Yekkirala return rc; 33417420ed23SVenkat Yekkirala 33427420ed23SVenkat Yekkirala return selinux_netlbl_inode_permission(SOCK_INODE(sock), MAY_WRITE); 33431da177e4SLinus Torvalds } 33441da177e4SLinus Torvalds 33451da177e4SLinus Torvalds static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg, 33461da177e4SLinus Torvalds int size, int flags) 33471da177e4SLinus Torvalds { 33481da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__READ); 33491da177e4SLinus Torvalds } 33501da177e4SLinus Torvalds 33511da177e4SLinus Torvalds static int selinux_socket_getsockname(struct socket *sock) 33521da177e4SLinus Torvalds { 33531da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__GETATTR); 33541da177e4SLinus Torvalds } 33551da177e4SLinus Torvalds 33561da177e4SLinus Torvalds static int selinux_socket_getpeername(struct socket *sock) 33571da177e4SLinus Torvalds { 33581da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__GETATTR); 33591da177e4SLinus Torvalds } 33601da177e4SLinus Torvalds 33611da177e4SLinus Torvalds static int selinux_socket_setsockopt(struct socket *sock,int level,int optname) 33621da177e4SLinus Torvalds { 3363f8687afeSPaul Moore int err; 3364f8687afeSPaul Moore 3365f8687afeSPaul Moore err = socket_has_perm(current, sock, SOCKET__SETOPT); 3366f8687afeSPaul Moore if (err) 3367f8687afeSPaul Moore return err; 3368f8687afeSPaul Moore 3369f8687afeSPaul Moore return selinux_netlbl_socket_setsockopt(sock, level, optname); 33701da177e4SLinus Torvalds } 33711da177e4SLinus Torvalds 33721da177e4SLinus Torvalds static int selinux_socket_getsockopt(struct socket *sock, int level, 33731da177e4SLinus Torvalds int optname) 33741da177e4SLinus Torvalds { 33751da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__GETOPT); 33761da177e4SLinus Torvalds } 33771da177e4SLinus Torvalds 33781da177e4SLinus Torvalds static int selinux_socket_shutdown(struct socket *sock, int how) 33791da177e4SLinus Torvalds { 33801da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__SHUTDOWN); 33811da177e4SLinus Torvalds } 33821da177e4SLinus Torvalds 33831da177e4SLinus Torvalds static int selinux_socket_unix_stream_connect(struct socket *sock, 33841da177e4SLinus Torvalds struct socket *other, 33851da177e4SLinus Torvalds struct sock *newsk) 33861da177e4SLinus Torvalds { 33871da177e4SLinus Torvalds struct sk_security_struct *ssec; 33881da177e4SLinus Torvalds struct inode_security_struct *isec; 33891da177e4SLinus Torvalds struct inode_security_struct *other_isec; 33901da177e4SLinus Torvalds struct avc_audit_data ad; 33911da177e4SLinus Torvalds int err; 33921da177e4SLinus Torvalds 33931da177e4SLinus Torvalds err = secondary_ops->unix_stream_connect(sock, other, newsk); 33941da177e4SLinus Torvalds if (err) 33951da177e4SLinus Torvalds return err; 33961da177e4SLinus Torvalds 33971da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 33981da177e4SLinus Torvalds other_isec = SOCK_INODE(other)->i_security; 33991da177e4SLinus Torvalds 34001da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,NET); 34011da177e4SLinus Torvalds ad.u.net.sk = other->sk; 34021da177e4SLinus Torvalds 34031da177e4SLinus Torvalds err = avc_has_perm(isec->sid, other_isec->sid, 34041da177e4SLinus Torvalds isec->sclass, 34051da177e4SLinus Torvalds UNIX_STREAM_SOCKET__CONNECTTO, &ad); 34061da177e4SLinus Torvalds if (err) 34071da177e4SLinus Torvalds return err; 34081da177e4SLinus Torvalds 34091da177e4SLinus Torvalds /* connecting socket */ 34101da177e4SLinus Torvalds ssec = sock->sk->sk_security; 34111da177e4SLinus Torvalds ssec->peer_sid = other_isec->sid; 34121da177e4SLinus Torvalds 34131da177e4SLinus Torvalds /* server child socket */ 34141da177e4SLinus Torvalds ssec = newsk->sk_security; 34151da177e4SLinus Torvalds ssec->peer_sid = isec->sid; 34164237c75cSVenkat Yekkirala err = security_sid_mls_copy(other_isec->sid, ssec->peer_sid, &ssec->sid); 34171da177e4SLinus Torvalds 34184237c75cSVenkat Yekkirala return err; 34191da177e4SLinus Torvalds } 34201da177e4SLinus Torvalds 34211da177e4SLinus Torvalds static int selinux_socket_unix_may_send(struct socket *sock, 34221da177e4SLinus Torvalds struct socket *other) 34231da177e4SLinus Torvalds { 34241da177e4SLinus Torvalds struct inode_security_struct *isec; 34251da177e4SLinus Torvalds struct inode_security_struct *other_isec; 34261da177e4SLinus Torvalds struct avc_audit_data ad; 34271da177e4SLinus Torvalds int err; 34281da177e4SLinus Torvalds 34291da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 34301da177e4SLinus Torvalds other_isec = SOCK_INODE(other)->i_security; 34311da177e4SLinus Torvalds 34321da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,NET); 34331da177e4SLinus Torvalds ad.u.net.sk = other->sk; 34341da177e4SLinus Torvalds 34351da177e4SLinus Torvalds err = avc_has_perm(isec->sid, other_isec->sid, 34361da177e4SLinus Torvalds isec->sclass, SOCKET__SENDTO, &ad); 34371da177e4SLinus Torvalds if (err) 34381da177e4SLinus Torvalds return err; 34391da177e4SLinus Torvalds 34401da177e4SLinus Torvalds return 0; 34411da177e4SLinus Torvalds } 34421da177e4SLinus Torvalds 34434e5ab4cbSJames Morris static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb, 34444237c75cSVenkat Yekkirala struct avc_audit_data *ad, u16 family, char *addrp, int len) 34451da177e4SLinus Torvalds { 34464e5ab4cbSJames Morris int err = 0; 34471da177e4SLinus Torvalds u32 netif_perm, node_perm, node_sid, if_sid, recv_perm = 0; 34484237c75cSVenkat Yekkirala struct socket *sock; 34494237c75cSVenkat Yekkirala u16 sock_class = 0; 34504237c75cSVenkat Yekkirala u32 sock_sid = 0; 34514237c75cSVenkat Yekkirala 34524237c75cSVenkat Yekkirala read_lock_bh(&sk->sk_callback_lock); 34534237c75cSVenkat Yekkirala sock = sk->sk_socket; 34544237c75cSVenkat Yekkirala if (sock) { 34554237c75cSVenkat Yekkirala struct inode *inode; 34564237c75cSVenkat Yekkirala inode = SOCK_INODE(sock); 34574237c75cSVenkat Yekkirala if (inode) { 34584237c75cSVenkat Yekkirala struct inode_security_struct *isec; 34594237c75cSVenkat Yekkirala isec = inode->i_security; 34604237c75cSVenkat Yekkirala sock_sid = isec->sid; 34614237c75cSVenkat Yekkirala sock_class = isec->sclass; 34624237c75cSVenkat Yekkirala } 34634237c75cSVenkat Yekkirala } 34644237c75cSVenkat Yekkirala read_unlock_bh(&sk->sk_callback_lock); 34654237c75cSVenkat Yekkirala if (!sock_sid) 34664237c75cSVenkat Yekkirala goto out; 34671da177e4SLinus Torvalds 34684e5ab4cbSJames Morris if (!skb->dev) 34691da177e4SLinus Torvalds goto out; 34701da177e4SLinus Torvalds 34714e5ab4cbSJames Morris err = sel_netif_sids(skb->dev, &if_sid, NULL); 34721da177e4SLinus Torvalds if (err) 34731da177e4SLinus Torvalds goto out; 34741da177e4SLinus Torvalds 34751da177e4SLinus Torvalds switch (sock_class) { 34761da177e4SLinus Torvalds case SECCLASS_UDP_SOCKET: 34771da177e4SLinus Torvalds netif_perm = NETIF__UDP_RECV; 34781da177e4SLinus Torvalds node_perm = NODE__UDP_RECV; 34791da177e4SLinus Torvalds recv_perm = UDP_SOCKET__RECV_MSG; 34801da177e4SLinus Torvalds break; 34811da177e4SLinus Torvalds 34821da177e4SLinus Torvalds case SECCLASS_TCP_SOCKET: 34831da177e4SLinus Torvalds netif_perm = NETIF__TCP_RECV; 34841da177e4SLinus Torvalds node_perm = NODE__TCP_RECV; 34851da177e4SLinus Torvalds recv_perm = TCP_SOCKET__RECV_MSG; 34861da177e4SLinus Torvalds break; 34871da177e4SLinus Torvalds 34882ee92d46SJames Morris case SECCLASS_DCCP_SOCKET: 34892ee92d46SJames Morris netif_perm = NETIF__DCCP_RECV; 34902ee92d46SJames Morris node_perm = NODE__DCCP_RECV; 34912ee92d46SJames Morris recv_perm = DCCP_SOCKET__RECV_MSG; 34922ee92d46SJames Morris break; 34932ee92d46SJames Morris 34941da177e4SLinus Torvalds default: 34951da177e4SLinus Torvalds netif_perm = NETIF__RAWIP_RECV; 34961da177e4SLinus Torvalds node_perm = NODE__RAWIP_RECV; 34971da177e4SLinus Torvalds break; 34981da177e4SLinus Torvalds } 34991da177e4SLinus Torvalds 35004e5ab4cbSJames Morris err = avc_has_perm(sock_sid, if_sid, SECCLASS_NETIF, netif_perm, ad); 35011da177e4SLinus Torvalds if (err) 35021da177e4SLinus Torvalds goto out; 35031da177e4SLinus Torvalds 35041da177e4SLinus Torvalds err = security_node_sid(family, addrp, len, &node_sid); 35051da177e4SLinus Torvalds if (err) 35061da177e4SLinus Torvalds goto out; 35071da177e4SLinus Torvalds 35084e5ab4cbSJames Morris err = avc_has_perm(sock_sid, node_sid, SECCLASS_NODE, node_perm, ad); 35091da177e4SLinus Torvalds if (err) 35101da177e4SLinus Torvalds goto out; 35111da177e4SLinus Torvalds 35121da177e4SLinus Torvalds if (recv_perm) { 35131da177e4SLinus Torvalds u32 port_sid; 35141da177e4SLinus Torvalds 35151da177e4SLinus Torvalds err = security_port_sid(sk->sk_family, sk->sk_type, 35164e5ab4cbSJames Morris sk->sk_protocol, ntohs(ad->u.net.sport), 35171da177e4SLinus Torvalds &port_sid); 35181da177e4SLinus Torvalds if (err) 35191da177e4SLinus Torvalds goto out; 35201da177e4SLinus Torvalds 35211da177e4SLinus Torvalds err = avc_has_perm(sock_sid, port_sid, 35224e5ab4cbSJames Morris sock_class, recv_perm, ad); 35231da177e4SLinus Torvalds } 3524d28d1e08STrent Jaeger 35254e5ab4cbSJames Morris out: 35264e5ab4cbSJames Morris return err; 35274e5ab4cbSJames Morris } 3528d28d1e08STrent Jaeger 35294e5ab4cbSJames Morris static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) 35304e5ab4cbSJames Morris { 35314e5ab4cbSJames Morris u16 family; 35324e5ab4cbSJames Morris char *addrp; 35334e5ab4cbSJames Morris int len, err = 0; 35344e5ab4cbSJames Morris struct avc_audit_data ad; 35354237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 35364e5ab4cbSJames Morris 35374e5ab4cbSJames Morris family = sk->sk_family; 35384e5ab4cbSJames Morris if (family != PF_INET && family != PF_INET6) 35394e5ab4cbSJames Morris goto out; 35404e5ab4cbSJames Morris 35414e5ab4cbSJames Morris /* Handle mapped IPv4 packets arriving via IPv6 sockets */ 354287fcd70dSAl Viro if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 35434e5ab4cbSJames Morris family = PF_INET; 35444e5ab4cbSJames Morris 35454e5ab4cbSJames Morris AVC_AUDIT_DATA_INIT(&ad, NET); 35464e5ab4cbSJames Morris ad.u.net.netif = skb->dev ? skb->dev->name : "[unknown]"; 35474e5ab4cbSJames Morris ad.u.net.family = family; 35484e5ab4cbSJames Morris 354967f83cbfSVenkat Yekkirala err = selinux_parse_skb(skb, &ad, &addrp, &len, 1, NULL); 35504e5ab4cbSJames Morris if (err) 35514e5ab4cbSJames Morris goto out; 35524e5ab4cbSJames Morris 35534e5ab4cbSJames Morris if (selinux_compat_net) 35544237c75cSVenkat Yekkirala err = selinux_sock_rcv_skb_compat(sk, skb, &ad, family, 35554e5ab4cbSJames Morris addrp, len); 35564e5ab4cbSJames Morris else 35574237c75cSVenkat Yekkirala err = avc_has_perm(sksec->sid, skb->secmark, SECCLASS_PACKET, 35584e5ab4cbSJames Morris PACKET__RECV, &ad); 35594e5ab4cbSJames Morris if (err) 35604e5ab4cbSJames Morris goto out; 35614e5ab4cbSJames Morris 35627420ed23SVenkat Yekkirala err = selinux_netlbl_sock_rcv_skb(sksec, skb, &ad); 35637420ed23SVenkat Yekkirala if (err) 35647420ed23SVenkat Yekkirala goto out; 35657420ed23SVenkat Yekkirala 35664237c75cSVenkat Yekkirala err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad); 35671da177e4SLinus Torvalds out: 35681da177e4SLinus Torvalds return err; 35691da177e4SLinus Torvalds } 35701da177e4SLinus Torvalds 35712c7946a7SCatherine Zhang static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval, 35721da177e4SLinus Torvalds int __user *optlen, unsigned len) 35731da177e4SLinus Torvalds { 35741da177e4SLinus Torvalds int err = 0; 35751da177e4SLinus Torvalds char *scontext; 35761da177e4SLinus Torvalds u32 scontext_len; 35771da177e4SLinus Torvalds struct sk_security_struct *ssec; 35781da177e4SLinus Torvalds struct inode_security_struct *isec; 35793de4bab5SPaul Moore u32 peer_sid = SECSID_NULL; 35801da177e4SLinus Torvalds 35811da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 35822c7946a7SCatherine Zhang 35833de4bab5SPaul Moore if (isec->sclass == SECCLASS_UNIX_STREAM_SOCKET || 35843de4bab5SPaul Moore isec->sclass == SECCLASS_TCP_SOCKET) { 35856b877699SVenkat Yekkirala ssec = sock->sk->sk_security; 35866b877699SVenkat Yekkirala peer_sid = ssec->peer_sid; 35876b877699SVenkat Yekkirala } 35882c7946a7SCatherine Zhang if (peer_sid == SECSID_NULL) { 35892c7946a7SCatherine Zhang err = -ENOPROTOOPT; 35902c7946a7SCatherine Zhang goto out; 35912c7946a7SCatherine Zhang } 35921da177e4SLinus Torvalds 35932c7946a7SCatherine Zhang err = security_sid_to_context(peer_sid, &scontext, &scontext_len); 35941da177e4SLinus Torvalds 35951da177e4SLinus Torvalds if (err) 35961da177e4SLinus Torvalds goto out; 35971da177e4SLinus Torvalds 35981da177e4SLinus Torvalds if (scontext_len > len) { 35991da177e4SLinus Torvalds err = -ERANGE; 36001da177e4SLinus Torvalds goto out_len; 36011da177e4SLinus Torvalds } 36021da177e4SLinus Torvalds 36031da177e4SLinus Torvalds if (copy_to_user(optval, scontext, scontext_len)) 36041da177e4SLinus Torvalds err = -EFAULT; 36051da177e4SLinus Torvalds 36061da177e4SLinus Torvalds out_len: 36071da177e4SLinus Torvalds if (put_user(scontext_len, optlen)) 36081da177e4SLinus Torvalds err = -EFAULT; 36091da177e4SLinus Torvalds 36101da177e4SLinus Torvalds kfree(scontext); 36111da177e4SLinus Torvalds out: 36121da177e4SLinus Torvalds return err; 36131da177e4SLinus Torvalds } 36141da177e4SLinus Torvalds 3615dc49c1f9SCatherine Zhang static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid) 36162c7946a7SCatherine Zhang { 3617dc49c1f9SCatherine Zhang u32 peer_secid = SECSID_NULL; 36182c7946a7SCatherine Zhang int err = 0; 3619877ce7c1SCatherine Zhang 36203de4bab5SPaul Moore if (sock && sock->sk->sk_family == PF_UNIX) 3621dc49c1f9SCatherine Zhang selinux_get_inode_sid(SOCK_INODE(sock), &peer_secid); 36223de4bab5SPaul Moore else if (skb) 36233de4bab5SPaul Moore security_skb_extlbl_sid(skb, 36243de4bab5SPaul Moore SECINITSID_UNLABELED, 36253de4bab5SPaul Moore &peer_secid); 36262c7946a7SCatherine Zhang 3627dc49c1f9SCatherine Zhang if (peer_secid == SECSID_NULL) 3628dc49c1f9SCatherine Zhang err = -EINVAL; 3629dc49c1f9SCatherine Zhang *secid = peer_secid; 36302c7946a7SCatherine Zhang 36312c7946a7SCatherine Zhang return err; 36322c7946a7SCatherine Zhang } 36332c7946a7SCatherine Zhang 36347d877f3bSAl Viro static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority) 36351da177e4SLinus Torvalds { 36361da177e4SLinus Torvalds return sk_alloc_security(sk, family, priority); 36371da177e4SLinus Torvalds } 36381da177e4SLinus Torvalds 36391da177e4SLinus Torvalds static void selinux_sk_free_security(struct sock *sk) 36401da177e4SLinus Torvalds { 36411da177e4SLinus Torvalds sk_free_security(sk); 36421da177e4SLinus Torvalds } 36431da177e4SLinus Torvalds 3644892c141eSVenkat Yekkirala static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk) 3645892c141eSVenkat Yekkirala { 3646892c141eSVenkat Yekkirala struct sk_security_struct *ssec = sk->sk_security; 3647892c141eSVenkat Yekkirala struct sk_security_struct *newssec = newsk->sk_security; 3648892c141eSVenkat Yekkirala 3649892c141eSVenkat Yekkirala newssec->sid = ssec->sid; 3650892c141eSVenkat Yekkirala newssec->peer_sid = ssec->peer_sid; 365199f59ed0SPaul Moore 36529f2ad665SPaul Moore selinux_netlbl_sk_security_clone(ssec, newssec); 3653892c141eSVenkat Yekkirala } 3654892c141eSVenkat Yekkirala 3655beb8d13bSVenkat Yekkirala static void selinux_sk_getsecid(struct sock *sk, u32 *secid) 3656d28d1e08STrent Jaeger { 3657d28d1e08STrent Jaeger if (!sk) 3658beb8d13bSVenkat Yekkirala *secid = SECINITSID_ANY_SOCKET; 3659892c141eSVenkat Yekkirala else { 3660892c141eSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 3661d28d1e08STrent Jaeger 3662beb8d13bSVenkat Yekkirala *secid = sksec->sid; 3663892c141eSVenkat Yekkirala } 3664d28d1e08STrent Jaeger } 3665d28d1e08STrent Jaeger 36669a673e56SAdrian Bunk static void selinux_sock_graft(struct sock* sk, struct socket *parent) 36674237c75cSVenkat Yekkirala { 36684237c75cSVenkat Yekkirala struct inode_security_struct *isec = SOCK_INODE(parent)->i_security; 36694237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 36704237c75cSVenkat Yekkirala 36712148ccc4SDavid Woodhouse if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 || 36722148ccc4SDavid Woodhouse sk->sk_family == PF_UNIX) 36734237c75cSVenkat Yekkirala isec->sid = sksec->sid; 36747420ed23SVenkat Yekkirala 36757420ed23SVenkat Yekkirala selinux_netlbl_sock_graft(sk, parent); 36764237c75cSVenkat Yekkirala } 36774237c75cSVenkat Yekkirala 36789a673e56SAdrian Bunk static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb, 36794237c75cSVenkat Yekkirala struct request_sock *req) 36804237c75cSVenkat Yekkirala { 36814237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 36824237c75cSVenkat Yekkirala int err; 36837420ed23SVenkat Yekkirala u32 newsid; 36844237c75cSVenkat Yekkirala u32 peersid; 36854237c75cSVenkat Yekkirala 36863de4bab5SPaul Moore security_skb_extlbl_sid(skb, SECINITSID_UNLABELED, &peersid); 3687a51c64f1SVenkat Yekkirala if (peersid == SECSID_NULL) { 3688a51c64f1SVenkat Yekkirala req->secid = sksec->sid; 36893de4bab5SPaul Moore req->peer_secid = SECSID_NULL; 3690a51c64f1SVenkat Yekkirala return 0; 3691a51c64f1SVenkat Yekkirala } 3692a51c64f1SVenkat Yekkirala 36934237c75cSVenkat Yekkirala err = security_sid_mls_copy(sksec->sid, peersid, &newsid); 36944237c75cSVenkat Yekkirala if (err) 36954237c75cSVenkat Yekkirala return err; 36964237c75cSVenkat Yekkirala 36974237c75cSVenkat Yekkirala req->secid = newsid; 36986b877699SVenkat Yekkirala req->peer_secid = peersid; 36994237c75cSVenkat Yekkirala return 0; 37004237c75cSVenkat Yekkirala } 37014237c75cSVenkat Yekkirala 37029a673e56SAdrian Bunk static void selinux_inet_csk_clone(struct sock *newsk, 37039a673e56SAdrian Bunk const struct request_sock *req) 37044237c75cSVenkat Yekkirala { 37054237c75cSVenkat Yekkirala struct sk_security_struct *newsksec = newsk->sk_security; 37064237c75cSVenkat Yekkirala 37074237c75cSVenkat Yekkirala newsksec->sid = req->secid; 37086b877699SVenkat Yekkirala newsksec->peer_sid = req->peer_secid; 37094237c75cSVenkat Yekkirala /* NOTE: Ideally, we should also get the isec->sid for the 37104237c75cSVenkat Yekkirala new socket in sync, but we don't have the isec available yet. 37114237c75cSVenkat Yekkirala So we will wait until sock_graft to do it, by which 37124237c75cSVenkat Yekkirala time it will have been created and available. */ 371399f59ed0SPaul Moore 37149f2ad665SPaul Moore /* We don't need to take any sort of lock here as we are the only 37159f2ad665SPaul Moore * thread with access to newsksec */ 37169f2ad665SPaul Moore selinux_netlbl_sk_security_reset(newsksec, req->rsk_ops->family); 37174237c75cSVenkat Yekkirala } 37184237c75cSVenkat Yekkirala 37196b877699SVenkat Yekkirala static void selinux_inet_conn_established(struct sock *sk, 37206b877699SVenkat Yekkirala struct sk_buff *skb) 37216b877699SVenkat Yekkirala { 37226b877699SVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 37236b877699SVenkat Yekkirala 37243de4bab5SPaul Moore security_skb_extlbl_sid(skb, SECINITSID_UNLABELED, &sksec->peer_sid); 37256b877699SVenkat Yekkirala } 37266b877699SVenkat Yekkirala 37279a673e56SAdrian Bunk static void selinux_req_classify_flow(const struct request_sock *req, 37289a673e56SAdrian Bunk struct flowi *fl) 37294237c75cSVenkat Yekkirala { 37304237c75cSVenkat Yekkirala fl->secid = req->secid; 37314237c75cSVenkat Yekkirala } 37324237c75cSVenkat Yekkirala 37331da177e4SLinus Torvalds static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb) 37341da177e4SLinus Torvalds { 37351da177e4SLinus Torvalds int err = 0; 37361da177e4SLinus Torvalds u32 perm; 37371da177e4SLinus Torvalds struct nlmsghdr *nlh; 37381da177e4SLinus Torvalds struct socket *sock = sk->sk_socket; 37391da177e4SLinus Torvalds struct inode_security_struct *isec = SOCK_INODE(sock)->i_security; 37401da177e4SLinus Torvalds 37411da177e4SLinus Torvalds if (skb->len < NLMSG_SPACE(0)) { 37421da177e4SLinus Torvalds err = -EINVAL; 37431da177e4SLinus Torvalds goto out; 37441da177e4SLinus Torvalds } 37451da177e4SLinus Torvalds nlh = (struct nlmsghdr *)skb->data; 37461da177e4SLinus Torvalds 37471da177e4SLinus Torvalds err = selinux_nlmsg_lookup(isec->sclass, nlh->nlmsg_type, &perm); 37481da177e4SLinus Torvalds if (err) { 37491da177e4SLinus Torvalds if (err == -EINVAL) { 37509ad9ad38SDavid Woodhouse audit_log(current->audit_context, GFP_KERNEL, AUDIT_SELINUX_ERR, 37511da177e4SLinus Torvalds "SELinux: unrecognized netlink message" 37521da177e4SLinus Torvalds " type=%hu for sclass=%hu\n", 37531da177e4SLinus Torvalds nlh->nlmsg_type, isec->sclass); 37541da177e4SLinus Torvalds if (!selinux_enforcing) 37551da177e4SLinus Torvalds err = 0; 37561da177e4SLinus Torvalds } 37571da177e4SLinus Torvalds 37581da177e4SLinus Torvalds /* Ignore */ 37591da177e4SLinus Torvalds if (err == -ENOENT) 37601da177e4SLinus Torvalds err = 0; 37611da177e4SLinus Torvalds goto out; 37621da177e4SLinus Torvalds } 37631da177e4SLinus Torvalds 37641da177e4SLinus Torvalds err = socket_has_perm(current, sock, perm); 37651da177e4SLinus Torvalds out: 37661da177e4SLinus Torvalds return err; 37671da177e4SLinus Torvalds } 37681da177e4SLinus Torvalds 37691da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER 37701da177e4SLinus Torvalds 37714e5ab4cbSJames Morris static int selinux_ip_postroute_last_compat(struct sock *sk, struct net_device *dev, 37724e5ab4cbSJames Morris struct avc_audit_data *ad, 37734e5ab4cbSJames Morris u16 family, char *addrp, int len) 37741da177e4SLinus Torvalds { 37754237c75cSVenkat Yekkirala int err = 0; 37761da177e4SLinus Torvalds u32 netif_perm, node_perm, node_sid, if_sid, send_perm = 0; 37774237c75cSVenkat Yekkirala struct socket *sock; 37784237c75cSVenkat Yekkirala struct inode *inode; 37794237c75cSVenkat Yekkirala struct inode_security_struct *isec; 37804237c75cSVenkat Yekkirala 37814237c75cSVenkat Yekkirala sock = sk->sk_socket; 37824237c75cSVenkat Yekkirala if (!sock) 37834237c75cSVenkat Yekkirala goto out; 37844237c75cSVenkat Yekkirala 37854237c75cSVenkat Yekkirala inode = SOCK_INODE(sock); 37864237c75cSVenkat Yekkirala if (!inode) 37874237c75cSVenkat Yekkirala goto out; 37884237c75cSVenkat Yekkirala 37894237c75cSVenkat Yekkirala isec = inode->i_security; 37901da177e4SLinus Torvalds 37911da177e4SLinus Torvalds err = sel_netif_sids(dev, &if_sid, NULL); 37921da177e4SLinus Torvalds if (err) 37931da177e4SLinus Torvalds goto out; 37941da177e4SLinus Torvalds 37951da177e4SLinus Torvalds switch (isec->sclass) { 37961da177e4SLinus Torvalds case SECCLASS_UDP_SOCKET: 37971da177e4SLinus Torvalds netif_perm = NETIF__UDP_SEND; 37981da177e4SLinus Torvalds node_perm = NODE__UDP_SEND; 37991da177e4SLinus Torvalds send_perm = UDP_SOCKET__SEND_MSG; 38001da177e4SLinus Torvalds break; 38011da177e4SLinus Torvalds 38021da177e4SLinus Torvalds case SECCLASS_TCP_SOCKET: 38031da177e4SLinus Torvalds netif_perm = NETIF__TCP_SEND; 38041da177e4SLinus Torvalds node_perm = NODE__TCP_SEND; 38051da177e4SLinus Torvalds send_perm = TCP_SOCKET__SEND_MSG; 38061da177e4SLinus Torvalds break; 38071da177e4SLinus Torvalds 38082ee92d46SJames Morris case SECCLASS_DCCP_SOCKET: 38092ee92d46SJames Morris netif_perm = NETIF__DCCP_SEND; 38102ee92d46SJames Morris node_perm = NODE__DCCP_SEND; 38112ee92d46SJames Morris send_perm = DCCP_SOCKET__SEND_MSG; 38122ee92d46SJames Morris break; 38132ee92d46SJames Morris 38141da177e4SLinus Torvalds default: 38151da177e4SLinus Torvalds netif_perm = NETIF__RAWIP_SEND; 38161da177e4SLinus Torvalds node_perm = NODE__RAWIP_SEND; 38171da177e4SLinus Torvalds break; 38181da177e4SLinus Torvalds } 38191da177e4SLinus Torvalds 38204e5ab4cbSJames Morris err = avc_has_perm(isec->sid, if_sid, SECCLASS_NETIF, netif_perm, ad); 38214e5ab4cbSJames Morris if (err) 38221da177e4SLinus Torvalds goto out; 38231da177e4SLinus Torvalds 38244e5ab4cbSJames Morris err = security_node_sid(family, addrp, len, &node_sid); 38254e5ab4cbSJames Morris if (err) 38261da177e4SLinus Torvalds goto out; 38271da177e4SLinus Torvalds 38284e5ab4cbSJames Morris err = avc_has_perm(isec->sid, node_sid, SECCLASS_NODE, node_perm, ad); 38294e5ab4cbSJames Morris if (err) 38301da177e4SLinus Torvalds goto out; 38311da177e4SLinus Torvalds 38321da177e4SLinus Torvalds if (send_perm) { 38331da177e4SLinus Torvalds u32 port_sid; 38341da177e4SLinus Torvalds 38351da177e4SLinus Torvalds err = security_port_sid(sk->sk_family, 38361da177e4SLinus Torvalds sk->sk_type, 38371da177e4SLinus Torvalds sk->sk_protocol, 38384e5ab4cbSJames Morris ntohs(ad->u.net.dport), 38394e5ab4cbSJames Morris &port_sid); 38404e5ab4cbSJames Morris if (err) 38411da177e4SLinus Torvalds goto out; 38421da177e4SLinus Torvalds 38431da177e4SLinus Torvalds err = avc_has_perm(isec->sid, port_sid, isec->sclass, 38444e5ab4cbSJames Morris send_perm, ad); 38454e5ab4cbSJames Morris } 38464e5ab4cbSJames Morris out: 38474e5ab4cbSJames Morris return err; 38481da177e4SLinus Torvalds } 38491da177e4SLinus Torvalds 38504e5ab4cbSJames Morris static unsigned int selinux_ip_postroute_last(unsigned int hooknum, 38514e5ab4cbSJames Morris struct sk_buff **pskb, 38524e5ab4cbSJames Morris const struct net_device *in, 38534e5ab4cbSJames Morris const struct net_device *out, 38544e5ab4cbSJames Morris int (*okfn)(struct sk_buff *), 38554e5ab4cbSJames Morris u16 family) 38564e5ab4cbSJames Morris { 38574e5ab4cbSJames Morris char *addrp; 38584e5ab4cbSJames Morris int len, err = 0; 38594e5ab4cbSJames Morris struct sock *sk; 38604e5ab4cbSJames Morris struct sk_buff *skb = *pskb; 38614e5ab4cbSJames Morris struct avc_audit_data ad; 38624e5ab4cbSJames Morris struct net_device *dev = (struct net_device *)out; 38634237c75cSVenkat Yekkirala struct sk_security_struct *sksec; 386467f83cbfSVenkat Yekkirala u8 proto; 38654e5ab4cbSJames Morris 38664e5ab4cbSJames Morris sk = skb->sk; 38674e5ab4cbSJames Morris if (!sk) 38684e5ab4cbSJames Morris goto out; 38694e5ab4cbSJames Morris 38704237c75cSVenkat Yekkirala sksec = sk->sk_security; 38714e5ab4cbSJames Morris 38724e5ab4cbSJames Morris AVC_AUDIT_DATA_INIT(&ad, NET); 38734e5ab4cbSJames Morris ad.u.net.netif = dev->name; 38744e5ab4cbSJames Morris ad.u.net.family = family; 38754e5ab4cbSJames Morris 387667f83cbfSVenkat Yekkirala err = selinux_parse_skb(skb, &ad, &addrp, &len, 0, &proto); 38774e5ab4cbSJames Morris if (err) 38784e5ab4cbSJames Morris goto out; 38794e5ab4cbSJames Morris 38804e5ab4cbSJames Morris if (selinux_compat_net) 38814237c75cSVenkat Yekkirala err = selinux_ip_postroute_last_compat(sk, dev, &ad, 38824e5ab4cbSJames Morris family, addrp, len); 38834e5ab4cbSJames Morris else 38844237c75cSVenkat Yekkirala err = avc_has_perm(sksec->sid, skb->secmark, SECCLASS_PACKET, 38854e5ab4cbSJames Morris PACKET__SEND, &ad); 38864e5ab4cbSJames Morris 38874e5ab4cbSJames Morris if (err) 3888d28d1e08STrent Jaeger goto out; 3889d28d1e08STrent Jaeger 389067f83cbfSVenkat Yekkirala err = selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto); 38911da177e4SLinus Torvalds out: 38924e5ab4cbSJames Morris return err ? NF_DROP : NF_ACCEPT; 38931da177e4SLinus Torvalds } 38941da177e4SLinus Torvalds 38951da177e4SLinus Torvalds static unsigned int selinux_ipv4_postroute_last(unsigned int hooknum, 38961da177e4SLinus Torvalds struct sk_buff **pskb, 38971da177e4SLinus Torvalds const struct net_device *in, 38981da177e4SLinus Torvalds const struct net_device *out, 38991da177e4SLinus Torvalds int (*okfn)(struct sk_buff *)) 39001da177e4SLinus Torvalds { 39011da177e4SLinus Torvalds return selinux_ip_postroute_last(hooknum, pskb, in, out, okfn, PF_INET); 39021da177e4SLinus Torvalds } 39031da177e4SLinus Torvalds 39041da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 39051da177e4SLinus Torvalds 39061da177e4SLinus Torvalds static unsigned int selinux_ipv6_postroute_last(unsigned int hooknum, 39071da177e4SLinus Torvalds struct sk_buff **pskb, 39081da177e4SLinus Torvalds const struct net_device *in, 39091da177e4SLinus Torvalds const struct net_device *out, 39101da177e4SLinus Torvalds int (*okfn)(struct sk_buff *)) 39111da177e4SLinus Torvalds { 39121da177e4SLinus Torvalds return selinux_ip_postroute_last(hooknum, pskb, in, out, okfn, PF_INET6); 39131da177e4SLinus Torvalds } 39141da177e4SLinus Torvalds 39151da177e4SLinus Torvalds #endif /* IPV6 */ 39161da177e4SLinus Torvalds 39171da177e4SLinus Torvalds #endif /* CONFIG_NETFILTER */ 39181da177e4SLinus Torvalds 39191da177e4SLinus Torvalds static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb) 39201da177e4SLinus Torvalds { 39211da177e4SLinus Torvalds int err; 39221da177e4SLinus Torvalds 39231da177e4SLinus Torvalds err = secondary_ops->netlink_send(sk, skb); 39241da177e4SLinus Torvalds if (err) 39251da177e4SLinus Torvalds return err; 39261da177e4SLinus Torvalds 39271da177e4SLinus Torvalds if (policydb_loaded_version >= POLICYDB_VERSION_NLCLASS) 39281da177e4SLinus Torvalds err = selinux_nlmsg_perm(sk, skb); 39291da177e4SLinus Torvalds 39301da177e4SLinus Torvalds return err; 39311da177e4SLinus Torvalds } 39321da177e4SLinus Torvalds 3933c7bdb545SDarrel Goeddel static int selinux_netlink_recv(struct sk_buff *skb, int capability) 39341da177e4SLinus Torvalds { 3935c7bdb545SDarrel Goeddel int err; 3936c7bdb545SDarrel Goeddel struct avc_audit_data ad; 3937c7bdb545SDarrel Goeddel 3938c7bdb545SDarrel Goeddel err = secondary_ops->netlink_recv(skb, capability); 3939c7bdb545SDarrel Goeddel if (err) 3940c7bdb545SDarrel Goeddel return err; 3941c7bdb545SDarrel Goeddel 3942c7bdb545SDarrel Goeddel AVC_AUDIT_DATA_INIT(&ad, CAP); 3943c7bdb545SDarrel Goeddel ad.u.cap = capability; 3944c7bdb545SDarrel Goeddel 3945c7bdb545SDarrel Goeddel return avc_has_perm(NETLINK_CB(skb).sid, NETLINK_CB(skb).sid, 3946c7bdb545SDarrel Goeddel SECCLASS_CAPABILITY, CAP_TO_MASK(capability), &ad); 39471da177e4SLinus Torvalds } 39481da177e4SLinus Torvalds 39491da177e4SLinus Torvalds static int ipc_alloc_security(struct task_struct *task, 39501da177e4SLinus Torvalds struct kern_ipc_perm *perm, 39511da177e4SLinus Torvalds u16 sclass) 39521da177e4SLinus Torvalds { 39531da177e4SLinus Torvalds struct task_security_struct *tsec = task->security; 39541da177e4SLinus Torvalds struct ipc_security_struct *isec; 39551da177e4SLinus Torvalds 395689d155efSJames Morris isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL); 39571da177e4SLinus Torvalds if (!isec) 39581da177e4SLinus Torvalds return -ENOMEM; 39591da177e4SLinus Torvalds 39601da177e4SLinus Torvalds isec->sclass = sclass; 39611da177e4SLinus Torvalds isec->ipc_perm = perm; 39621da177e4SLinus Torvalds isec->sid = tsec->sid; 39631da177e4SLinus Torvalds perm->security = isec; 39641da177e4SLinus Torvalds 39651da177e4SLinus Torvalds return 0; 39661da177e4SLinus Torvalds } 39671da177e4SLinus Torvalds 39681da177e4SLinus Torvalds static void ipc_free_security(struct kern_ipc_perm *perm) 39691da177e4SLinus Torvalds { 39701da177e4SLinus Torvalds struct ipc_security_struct *isec = perm->security; 39711da177e4SLinus Torvalds perm->security = NULL; 39721da177e4SLinus Torvalds kfree(isec); 39731da177e4SLinus Torvalds } 39741da177e4SLinus Torvalds 39751da177e4SLinus Torvalds static int msg_msg_alloc_security(struct msg_msg *msg) 39761da177e4SLinus Torvalds { 39771da177e4SLinus Torvalds struct msg_security_struct *msec; 39781da177e4SLinus Torvalds 397989d155efSJames Morris msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL); 39801da177e4SLinus Torvalds if (!msec) 39811da177e4SLinus Torvalds return -ENOMEM; 39821da177e4SLinus Torvalds 39831da177e4SLinus Torvalds msec->msg = msg; 39841da177e4SLinus Torvalds msec->sid = SECINITSID_UNLABELED; 39851da177e4SLinus Torvalds msg->security = msec; 39861da177e4SLinus Torvalds 39871da177e4SLinus Torvalds return 0; 39881da177e4SLinus Torvalds } 39891da177e4SLinus Torvalds 39901da177e4SLinus Torvalds static void msg_msg_free_security(struct msg_msg *msg) 39911da177e4SLinus Torvalds { 39921da177e4SLinus Torvalds struct msg_security_struct *msec = msg->security; 39931da177e4SLinus Torvalds 39941da177e4SLinus Torvalds msg->security = NULL; 39951da177e4SLinus Torvalds kfree(msec); 39961da177e4SLinus Torvalds } 39971da177e4SLinus Torvalds 39981da177e4SLinus Torvalds static int ipc_has_perm(struct kern_ipc_perm *ipc_perms, 39996af963f1SStephen Smalley u32 perms) 40001da177e4SLinus Torvalds { 40011da177e4SLinus Torvalds struct task_security_struct *tsec; 40021da177e4SLinus Torvalds struct ipc_security_struct *isec; 40031da177e4SLinus Torvalds struct avc_audit_data ad; 40041da177e4SLinus Torvalds 40051da177e4SLinus Torvalds tsec = current->security; 40061da177e4SLinus Torvalds isec = ipc_perms->security; 40071da177e4SLinus Torvalds 40081da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 40091da177e4SLinus Torvalds ad.u.ipc_id = ipc_perms->key; 40101da177e4SLinus Torvalds 40116af963f1SStephen Smalley return avc_has_perm(tsec->sid, isec->sid, isec->sclass, perms, &ad); 40121da177e4SLinus Torvalds } 40131da177e4SLinus Torvalds 40141da177e4SLinus Torvalds static int selinux_msg_msg_alloc_security(struct msg_msg *msg) 40151da177e4SLinus Torvalds { 40161da177e4SLinus Torvalds return msg_msg_alloc_security(msg); 40171da177e4SLinus Torvalds } 40181da177e4SLinus Torvalds 40191da177e4SLinus Torvalds static void selinux_msg_msg_free_security(struct msg_msg *msg) 40201da177e4SLinus Torvalds { 40211da177e4SLinus Torvalds msg_msg_free_security(msg); 40221da177e4SLinus Torvalds } 40231da177e4SLinus Torvalds 40241da177e4SLinus Torvalds /* message queue security operations */ 40251da177e4SLinus Torvalds static int selinux_msg_queue_alloc_security(struct msg_queue *msq) 40261da177e4SLinus Torvalds { 40271da177e4SLinus Torvalds struct task_security_struct *tsec; 40281da177e4SLinus Torvalds struct ipc_security_struct *isec; 40291da177e4SLinus Torvalds struct avc_audit_data ad; 40301da177e4SLinus Torvalds int rc; 40311da177e4SLinus Torvalds 40321da177e4SLinus Torvalds rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ); 40331da177e4SLinus Torvalds if (rc) 40341da177e4SLinus Torvalds return rc; 40351da177e4SLinus Torvalds 40361da177e4SLinus Torvalds tsec = current->security; 40371da177e4SLinus Torvalds isec = msq->q_perm.security; 40381da177e4SLinus Torvalds 40391da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 40401da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 40411da177e4SLinus Torvalds 40421da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_MSGQ, 40431da177e4SLinus Torvalds MSGQ__CREATE, &ad); 40441da177e4SLinus Torvalds if (rc) { 40451da177e4SLinus Torvalds ipc_free_security(&msq->q_perm); 40461da177e4SLinus Torvalds return rc; 40471da177e4SLinus Torvalds } 40481da177e4SLinus Torvalds return 0; 40491da177e4SLinus Torvalds } 40501da177e4SLinus Torvalds 40511da177e4SLinus Torvalds static void selinux_msg_queue_free_security(struct msg_queue *msq) 40521da177e4SLinus Torvalds { 40531da177e4SLinus Torvalds ipc_free_security(&msq->q_perm); 40541da177e4SLinus Torvalds } 40551da177e4SLinus Torvalds 40561da177e4SLinus Torvalds static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg) 40571da177e4SLinus Torvalds { 40581da177e4SLinus Torvalds struct task_security_struct *tsec; 40591da177e4SLinus Torvalds struct ipc_security_struct *isec; 40601da177e4SLinus Torvalds struct avc_audit_data ad; 40611da177e4SLinus Torvalds 40621da177e4SLinus Torvalds tsec = current->security; 40631da177e4SLinus Torvalds isec = msq->q_perm.security; 40641da177e4SLinus Torvalds 40651da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 40661da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 40671da177e4SLinus Torvalds 40681da177e4SLinus Torvalds return avc_has_perm(tsec->sid, isec->sid, SECCLASS_MSGQ, 40691da177e4SLinus Torvalds MSGQ__ASSOCIATE, &ad); 40701da177e4SLinus Torvalds } 40711da177e4SLinus Torvalds 40721da177e4SLinus Torvalds static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd) 40731da177e4SLinus Torvalds { 40741da177e4SLinus Torvalds int err; 40751da177e4SLinus Torvalds int perms; 40761da177e4SLinus Torvalds 40771da177e4SLinus Torvalds switch(cmd) { 40781da177e4SLinus Torvalds case IPC_INFO: 40791da177e4SLinus Torvalds case MSG_INFO: 40801da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 40811da177e4SLinus Torvalds return task_has_system(current, SYSTEM__IPC_INFO); 40821da177e4SLinus Torvalds case IPC_STAT: 40831da177e4SLinus Torvalds case MSG_STAT: 40841da177e4SLinus Torvalds perms = MSGQ__GETATTR | MSGQ__ASSOCIATE; 40851da177e4SLinus Torvalds break; 40861da177e4SLinus Torvalds case IPC_SET: 40871da177e4SLinus Torvalds perms = MSGQ__SETATTR; 40881da177e4SLinus Torvalds break; 40891da177e4SLinus Torvalds case IPC_RMID: 40901da177e4SLinus Torvalds perms = MSGQ__DESTROY; 40911da177e4SLinus Torvalds break; 40921da177e4SLinus Torvalds default: 40931da177e4SLinus Torvalds return 0; 40941da177e4SLinus Torvalds } 40951da177e4SLinus Torvalds 40966af963f1SStephen Smalley err = ipc_has_perm(&msq->q_perm, perms); 40971da177e4SLinus Torvalds return err; 40981da177e4SLinus Torvalds } 40991da177e4SLinus Torvalds 41001da177e4SLinus Torvalds static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg) 41011da177e4SLinus Torvalds { 41021da177e4SLinus Torvalds struct task_security_struct *tsec; 41031da177e4SLinus Torvalds struct ipc_security_struct *isec; 41041da177e4SLinus Torvalds struct msg_security_struct *msec; 41051da177e4SLinus Torvalds struct avc_audit_data ad; 41061da177e4SLinus Torvalds int rc; 41071da177e4SLinus Torvalds 41081da177e4SLinus Torvalds tsec = current->security; 41091da177e4SLinus Torvalds isec = msq->q_perm.security; 41101da177e4SLinus Torvalds msec = msg->security; 41111da177e4SLinus Torvalds 41121da177e4SLinus Torvalds /* 41131da177e4SLinus Torvalds * First time through, need to assign label to the message 41141da177e4SLinus Torvalds */ 41151da177e4SLinus Torvalds if (msec->sid == SECINITSID_UNLABELED) { 41161da177e4SLinus Torvalds /* 41171da177e4SLinus Torvalds * Compute new sid based on current process and 41181da177e4SLinus Torvalds * message queue this message will be stored in 41191da177e4SLinus Torvalds */ 41201da177e4SLinus Torvalds rc = security_transition_sid(tsec->sid, 41211da177e4SLinus Torvalds isec->sid, 41221da177e4SLinus Torvalds SECCLASS_MSG, 41231da177e4SLinus Torvalds &msec->sid); 41241da177e4SLinus Torvalds if (rc) 41251da177e4SLinus Torvalds return rc; 41261da177e4SLinus Torvalds } 41271da177e4SLinus Torvalds 41281da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 41291da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 41301da177e4SLinus Torvalds 41311da177e4SLinus Torvalds /* Can this process write to the queue? */ 41321da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_MSGQ, 41331da177e4SLinus Torvalds MSGQ__WRITE, &ad); 41341da177e4SLinus Torvalds if (!rc) 41351da177e4SLinus Torvalds /* Can this process send the message */ 41361da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, msec->sid, 41371da177e4SLinus Torvalds SECCLASS_MSG, MSG__SEND, &ad); 41381da177e4SLinus Torvalds if (!rc) 41391da177e4SLinus Torvalds /* Can the message be put in the queue? */ 41401da177e4SLinus Torvalds rc = avc_has_perm(msec->sid, isec->sid, 41411da177e4SLinus Torvalds SECCLASS_MSGQ, MSGQ__ENQUEUE, &ad); 41421da177e4SLinus Torvalds 41431da177e4SLinus Torvalds return rc; 41441da177e4SLinus Torvalds } 41451da177e4SLinus Torvalds 41461da177e4SLinus Torvalds static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg, 41471da177e4SLinus Torvalds struct task_struct *target, 41481da177e4SLinus Torvalds long type, int mode) 41491da177e4SLinus Torvalds { 41501da177e4SLinus Torvalds struct task_security_struct *tsec; 41511da177e4SLinus Torvalds struct ipc_security_struct *isec; 41521da177e4SLinus Torvalds struct msg_security_struct *msec; 41531da177e4SLinus Torvalds struct avc_audit_data ad; 41541da177e4SLinus Torvalds int rc; 41551da177e4SLinus Torvalds 41561da177e4SLinus Torvalds tsec = target->security; 41571da177e4SLinus Torvalds isec = msq->q_perm.security; 41581da177e4SLinus Torvalds msec = msg->security; 41591da177e4SLinus Torvalds 41601da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 41611da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 41621da177e4SLinus Torvalds 41631da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, 41641da177e4SLinus Torvalds SECCLASS_MSGQ, MSGQ__READ, &ad); 41651da177e4SLinus Torvalds if (!rc) 41661da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, msec->sid, 41671da177e4SLinus Torvalds SECCLASS_MSG, MSG__RECEIVE, &ad); 41681da177e4SLinus Torvalds return rc; 41691da177e4SLinus Torvalds } 41701da177e4SLinus Torvalds 41711da177e4SLinus Torvalds /* Shared Memory security operations */ 41721da177e4SLinus Torvalds static int selinux_shm_alloc_security(struct shmid_kernel *shp) 41731da177e4SLinus Torvalds { 41741da177e4SLinus Torvalds struct task_security_struct *tsec; 41751da177e4SLinus Torvalds struct ipc_security_struct *isec; 41761da177e4SLinus Torvalds struct avc_audit_data ad; 41771da177e4SLinus Torvalds int rc; 41781da177e4SLinus Torvalds 41791da177e4SLinus Torvalds rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM); 41801da177e4SLinus Torvalds if (rc) 41811da177e4SLinus Torvalds return rc; 41821da177e4SLinus Torvalds 41831da177e4SLinus Torvalds tsec = current->security; 41841da177e4SLinus Torvalds isec = shp->shm_perm.security; 41851da177e4SLinus Torvalds 41861da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 41871da177e4SLinus Torvalds ad.u.ipc_id = shp->shm_perm.key; 41881da177e4SLinus Torvalds 41891da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_SHM, 41901da177e4SLinus Torvalds SHM__CREATE, &ad); 41911da177e4SLinus Torvalds if (rc) { 41921da177e4SLinus Torvalds ipc_free_security(&shp->shm_perm); 41931da177e4SLinus Torvalds return rc; 41941da177e4SLinus Torvalds } 41951da177e4SLinus Torvalds return 0; 41961da177e4SLinus Torvalds } 41971da177e4SLinus Torvalds 41981da177e4SLinus Torvalds static void selinux_shm_free_security(struct shmid_kernel *shp) 41991da177e4SLinus Torvalds { 42001da177e4SLinus Torvalds ipc_free_security(&shp->shm_perm); 42011da177e4SLinus Torvalds } 42021da177e4SLinus Torvalds 42031da177e4SLinus Torvalds static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg) 42041da177e4SLinus Torvalds { 42051da177e4SLinus Torvalds struct task_security_struct *tsec; 42061da177e4SLinus Torvalds struct ipc_security_struct *isec; 42071da177e4SLinus Torvalds struct avc_audit_data ad; 42081da177e4SLinus Torvalds 42091da177e4SLinus Torvalds tsec = current->security; 42101da177e4SLinus Torvalds isec = shp->shm_perm.security; 42111da177e4SLinus Torvalds 42121da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 42131da177e4SLinus Torvalds ad.u.ipc_id = shp->shm_perm.key; 42141da177e4SLinus Torvalds 42151da177e4SLinus Torvalds return avc_has_perm(tsec->sid, isec->sid, SECCLASS_SHM, 42161da177e4SLinus Torvalds SHM__ASSOCIATE, &ad); 42171da177e4SLinus Torvalds } 42181da177e4SLinus Torvalds 42191da177e4SLinus Torvalds /* Note, at this point, shp is locked down */ 42201da177e4SLinus Torvalds static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd) 42211da177e4SLinus Torvalds { 42221da177e4SLinus Torvalds int perms; 42231da177e4SLinus Torvalds int err; 42241da177e4SLinus Torvalds 42251da177e4SLinus Torvalds switch(cmd) { 42261da177e4SLinus Torvalds case IPC_INFO: 42271da177e4SLinus Torvalds case SHM_INFO: 42281da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 42291da177e4SLinus Torvalds return task_has_system(current, SYSTEM__IPC_INFO); 42301da177e4SLinus Torvalds case IPC_STAT: 42311da177e4SLinus Torvalds case SHM_STAT: 42321da177e4SLinus Torvalds perms = SHM__GETATTR | SHM__ASSOCIATE; 42331da177e4SLinus Torvalds break; 42341da177e4SLinus Torvalds case IPC_SET: 42351da177e4SLinus Torvalds perms = SHM__SETATTR; 42361da177e4SLinus Torvalds break; 42371da177e4SLinus Torvalds case SHM_LOCK: 42381da177e4SLinus Torvalds case SHM_UNLOCK: 42391da177e4SLinus Torvalds perms = SHM__LOCK; 42401da177e4SLinus Torvalds break; 42411da177e4SLinus Torvalds case IPC_RMID: 42421da177e4SLinus Torvalds perms = SHM__DESTROY; 42431da177e4SLinus Torvalds break; 42441da177e4SLinus Torvalds default: 42451da177e4SLinus Torvalds return 0; 42461da177e4SLinus Torvalds } 42471da177e4SLinus Torvalds 42486af963f1SStephen Smalley err = ipc_has_perm(&shp->shm_perm, perms); 42491da177e4SLinus Torvalds return err; 42501da177e4SLinus Torvalds } 42511da177e4SLinus Torvalds 42521da177e4SLinus Torvalds static int selinux_shm_shmat(struct shmid_kernel *shp, 42531da177e4SLinus Torvalds char __user *shmaddr, int shmflg) 42541da177e4SLinus Torvalds { 42551da177e4SLinus Torvalds u32 perms; 42561da177e4SLinus Torvalds int rc; 42571da177e4SLinus Torvalds 42581da177e4SLinus Torvalds rc = secondary_ops->shm_shmat(shp, shmaddr, shmflg); 42591da177e4SLinus Torvalds if (rc) 42601da177e4SLinus Torvalds return rc; 42611da177e4SLinus Torvalds 42621da177e4SLinus Torvalds if (shmflg & SHM_RDONLY) 42631da177e4SLinus Torvalds perms = SHM__READ; 42641da177e4SLinus Torvalds else 42651da177e4SLinus Torvalds perms = SHM__READ | SHM__WRITE; 42661da177e4SLinus Torvalds 42676af963f1SStephen Smalley return ipc_has_perm(&shp->shm_perm, perms); 42681da177e4SLinus Torvalds } 42691da177e4SLinus Torvalds 42701da177e4SLinus Torvalds /* Semaphore security operations */ 42711da177e4SLinus Torvalds static int selinux_sem_alloc_security(struct sem_array *sma) 42721da177e4SLinus Torvalds { 42731da177e4SLinus Torvalds struct task_security_struct *tsec; 42741da177e4SLinus Torvalds struct ipc_security_struct *isec; 42751da177e4SLinus Torvalds struct avc_audit_data ad; 42761da177e4SLinus Torvalds int rc; 42771da177e4SLinus Torvalds 42781da177e4SLinus Torvalds rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM); 42791da177e4SLinus Torvalds if (rc) 42801da177e4SLinus Torvalds return rc; 42811da177e4SLinus Torvalds 42821da177e4SLinus Torvalds tsec = current->security; 42831da177e4SLinus Torvalds isec = sma->sem_perm.security; 42841da177e4SLinus Torvalds 42851da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 42861da177e4SLinus Torvalds ad.u.ipc_id = sma->sem_perm.key; 42871da177e4SLinus Torvalds 42881da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_SEM, 42891da177e4SLinus Torvalds SEM__CREATE, &ad); 42901da177e4SLinus Torvalds if (rc) { 42911da177e4SLinus Torvalds ipc_free_security(&sma->sem_perm); 42921da177e4SLinus Torvalds return rc; 42931da177e4SLinus Torvalds } 42941da177e4SLinus Torvalds return 0; 42951da177e4SLinus Torvalds } 42961da177e4SLinus Torvalds 42971da177e4SLinus Torvalds static void selinux_sem_free_security(struct sem_array *sma) 42981da177e4SLinus Torvalds { 42991da177e4SLinus Torvalds ipc_free_security(&sma->sem_perm); 43001da177e4SLinus Torvalds } 43011da177e4SLinus Torvalds 43021da177e4SLinus Torvalds static int selinux_sem_associate(struct sem_array *sma, int semflg) 43031da177e4SLinus Torvalds { 43041da177e4SLinus Torvalds struct task_security_struct *tsec; 43051da177e4SLinus Torvalds struct ipc_security_struct *isec; 43061da177e4SLinus Torvalds struct avc_audit_data ad; 43071da177e4SLinus Torvalds 43081da177e4SLinus Torvalds tsec = current->security; 43091da177e4SLinus Torvalds isec = sma->sem_perm.security; 43101da177e4SLinus Torvalds 43111da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 43121da177e4SLinus Torvalds ad.u.ipc_id = sma->sem_perm.key; 43131da177e4SLinus Torvalds 43141da177e4SLinus Torvalds return avc_has_perm(tsec->sid, isec->sid, SECCLASS_SEM, 43151da177e4SLinus Torvalds SEM__ASSOCIATE, &ad); 43161da177e4SLinus Torvalds } 43171da177e4SLinus Torvalds 43181da177e4SLinus Torvalds /* Note, at this point, sma is locked down */ 43191da177e4SLinus Torvalds static int selinux_sem_semctl(struct sem_array *sma, int cmd) 43201da177e4SLinus Torvalds { 43211da177e4SLinus Torvalds int err; 43221da177e4SLinus Torvalds u32 perms; 43231da177e4SLinus Torvalds 43241da177e4SLinus Torvalds switch(cmd) { 43251da177e4SLinus Torvalds case IPC_INFO: 43261da177e4SLinus Torvalds case SEM_INFO: 43271da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 43281da177e4SLinus Torvalds return task_has_system(current, SYSTEM__IPC_INFO); 43291da177e4SLinus Torvalds case GETPID: 43301da177e4SLinus Torvalds case GETNCNT: 43311da177e4SLinus Torvalds case GETZCNT: 43321da177e4SLinus Torvalds perms = SEM__GETATTR; 43331da177e4SLinus Torvalds break; 43341da177e4SLinus Torvalds case GETVAL: 43351da177e4SLinus Torvalds case GETALL: 43361da177e4SLinus Torvalds perms = SEM__READ; 43371da177e4SLinus Torvalds break; 43381da177e4SLinus Torvalds case SETVAL: 43391da177e4SLinus Torvalds case SETALL: 43401da177e4SLinus Torvalds perms = SEM__WRITE; 43411da177e4SLinus Torvalds break; 43421da177e4SLinus Torvalds case IPC_RMID: 43431da177e4SLinus Torvalds perms = SEM__DESTROY; 43441da177e4SLinus Torvalds break; 43451da177e4SLinus Torvalds case IPC_SET: 43461da177e4SLinus Torvalds perms = SEM__SETATTR; 43471da177e4SLinus Torvalds break; 43481da177e4SLinus Torvalds case IPC_STAT: 43491da177e4SLinus Torvalds case SEM_STAT: 43501da177e4SLinus Torvalds perms = SEM__GETATTR | SEM__ASSOCIATE; 43511da177e4SLinus Torvalds break; 43521da177e4SLinus Torvalds default: 43531da177e4SLinus Torvalds return 0; 43541da177e4SLinus Torvalds } 43551da177e4SLinus Torvalds 43566af963f1SStephen Smalley err = ipc_has_perm(&sma->sem_perm, perms); 43571da177e4SLinus Torvalds return err; 43581da177e4SLinus Torvalds } 43591da177e4SLinus Torvalds 43601da177e4SLinus Torvalds static int selinux_sem_semop(struct sem_array *sma, 43611da177e4SLinus Torvalds struct sembuf *sops, unsigned nsops, int alter) 43621da177e4SLinus Torvalds { 43631da177e4SLinus Torvalds u32 perms; 43641da177e4SLinus Torvalds 43651da177e4SLinus Torvalds if (alter) 43661da177e4SLinus Torvalds perms = SEM__READ | SEM__WRITE; 43671da177e4SLinus Torvalds else 43681da177e4SLinus Torvalds perms = SEM__READ; 43691da177e4SLinus Torvalds 43706af963f1SStephen Smalley return ipc_has_perm(&sma->sem_perm, perms); 43711da177e4SLinus Torvalds } 43721da177e4SLinus Torvalds 43731da177e4SLinus Torvalds static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag) 43741da177e4SLinus Torvalds { 43751da177e4SLinus Torvalds u32 av = 0; 43761da177e4SLinus Torvalds 43771da177e4SLinus Torvalds av = 0; 43781da177e4SLinus Torvalds if (flag & S_IRUGO) 43791da177e4SLinus Torvalds av |= IPC__UNIX_READ; 43801da177e4SLinus Torvalds if (flag & S_IWUGO) 43811da177e4SLinus Torvalds av |= IPC__UNIX_WRITE; 43821da177e4SLinus Torvalds 43831da177e4SLinus Torvalds if (av == 0) 43841da177e4SLinus Torvalds return 0; 43851da177e4SLinus Torvalds 43866af963f1SStephen Smalley return ipc_has_perm(ipcp, av); 43871da177e4SLinus Torvalds } 43881da177e4SLinus Torvalds 43891da177e4SLinus Torvalds /* module stacking operations */ 43901da177e4SLinus Torvalds static int selinux_register_security (const char *name, struct security_operations *ops) 43911da177e4SLinus Torvalds { 43921da177e4SLinus Torvalds if (secondary_ops != original_ops) { 43931da177e4SLinus Torvalds printk(KERN_INFO "%s: There is already a secondary security " 43941da177e4SLinus Torvalds "module registered.\n", __FUNCTION__); 43951da177e4SLinus Torvalds return -EINVAL; 43961da177e4SLinus Torvalds } 43971da177e4SLinus Torvalds 43981da177e4SLinus Torvalds secondary_ops = ops; 43991da177e4SLinus Torvalds 44001da177e4SLinus Torvalds printk(KERN_INFO "%s: Registering secondary module %s\n", 44011da177e4SLinus Torvalds __FUNCTION__, 44021da177e4SLinus Torvalds name); 44031da177e4SLinus Torvalds 44041da177e4SLinus Torvalds return 0; 44051da177e4SLinus Torvalds } 44061da177e4SLinus Torvalds 44071da177e4SLinus Torvalds static int selinux_unregister_security (const char *name, struct security_operations *ops) 44081da177e4SLinus Torvalds { 44091da177e4SLinus Torvalds if (ops != secondary_ops) { 44101da177e4SLinus Torvalds printk (KERN_INFO "%s: trying to unregister a security module " 44111da177e4SLinus Torvalds "that is not registered.\n", __FUNCTION__); 44121da177e4SLinus Torvalds return -EINVAL; 44131da177e4SLinus Torvalds } 44141da177e4SLinus Torvalds 44151da177e4SLinus Torvalds secondary_ops = original_ops; 44161da177e4SLinus Torvalds 44171da177e4SLinus Torvalds return 0; 44181da177e4SLinus Torvalds } 44191da177e4SLinus Torvalds 44201da177e4SLinus Torvalds static void selinux_d_instantiate (struct dentry *dentry, struct inode *inode) 44211da177e4SLinus Torvalds { 44221da177e4SLinus Torvalds if (inode) 44231da177e4SLinus Torvalds inode_doinit_with_dentry(inode, dentry); 44241da177e4SLinus Torvalds } 44251da177e4SLinus Torvalds 44261da177e4SLinus Torvalds static int selinux_getprocattr(struct task_struct *p, 44271da177e4SLinus Torvalds char *name, void *value, size_t size) 44281da177e4SLinus Torvalds { 44291da177e4SLinus Torvalds struct task_security_struct *tsec; 44308c8570fbSDustin Kirkland u32 sid; 44311da177e4SLinus Torvalds int error; 44321da177e4SLinus Torvalds 44331da177e4SLinus Torvalds if (current != p) { 44341da177e4SLinus Torvalds error = task_has_perm(current, p, PROCESS__GETATTR); 44351da177e4SLinus Torvalds if (error) 44361da177e4SLinus Torvalds return error; 44371da177e4SLinus Torvalds } 44381da177e4SLinus Torvalds 44391da177e4SLinus Torvalds tsec = p->security; 44401da177e4SLinus Torvalds 44411da177e4SLinus Torvalds if (!strcmp(name, "current")) 44421da177e4SLinus Torvalds sid = tsec->sid; 44431da177e4SLinus Torvalds else if (!strcmp(name, "prev")) 44441da177e4SLinus Torvalds sid = tsec->osid; 44451da177e4SLinus Torvalds else if (!strcmp(name, "exec")) 44461da177e4SLinus Torvalds sid = tsec->exec_sid; 44471da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 44481da177e4SLinus Torvalds sid = tsec->create_sid; 44494eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) 44504eb582cfSMichael LeMay sid = tsec->keycreate_sid; 445142c3e03eSEric Paris else if (!strcmp(name, "sockcreate")) 445242c3e03eSEric Paris sid = tsec->sockcreate_sid; 44531da177e4SLinus Torvalds else 44541da177e4SLinus Torvalds return -EINVAL; 44551da177e4SLinus Torvalds 44561da177e4SLinus Torvalds if (!sid) 44571da177e4SLinus Torvalds return 0; 44581da177e4SLinus Torvalds 44598c8570fbSDustin Kirkland return selinux_getsecurity(sid, value, size); 44601da177e4SLinus Torvalds } 44611da177e4SLinus Torvalds 44621da177e4SLinus Torvalds static int selinux_setprocattr(struct task_struct *p, 44631da177e4SLinus Torvalds char *name, void *value, size_t size) 44641da177e4SLinus Torvalds { 44651da177e4SLinus Torvalds struct task_security_struct *tsec; 44661da177e4SLinus Torvalds u32 sid = 0; 44671da177e4SLinus Torvalds int error; 44681da177e4SLinus Torvalds char *str = value; 44691da177e4SLinus Torvalds 44701da177e4SLinus Torvalds if (current != p) { 44711da177e4SLinus Torvalds /* SELinux only allows a process to change its own 44721da177e4SLinus Torvalds security attributes. */ 44731da177e4SLinus Torvalds return -EACCES; 44741da177e4SLinus Torvalds } 44751da177e4SLinus Torvalds 44761da177e4SLinus Torvalds /* 44771da177e4SLinus Torvalds * Basic control over ability to set these attributes at all. 44781da177e4SLinus Torvalds * current == p, but we'll pass them separately in case the 44791da177e4SLinus Torvalds * above restriction is ever removed. 44801da177e4SLinus Torvalds */ 44811da177e4SLinus Torvalds if (!strcmp(name, "exec")) 44821da177e4SLinus Torvalds error = task_has_perm(current, p, PROCESS__SETEXEC); 44831da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 44841da177e4SLinus Torvalds error = task_has_perm(current, p, PROCESS__SETFSCREATE); 44854eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) 44864eb582cfSMichael LeMay error = task_has_perm(current, p, PROCESS__SETKEYCREATE); 448742c3e03eSEric Paris else if (!strcmp(name, "sockcreate")) 448842c3e03eSEric Paris error = task_has_perm(current, p, PROCESS__SETSOCKCREATE); 44891da177e4SLinus Torvalds else if (!strcmp(name, "current")) 44901da177e4SLinus Torvalds error = task_has_perm(current, p, PROCESS__SETCURRENT); 44911da177e4SLinus Torvalds else 44921da177e4SLinus Torvalds error = -EINVAL; 44931da177e4SLinus Torvalds if (error) 44941da177e4SLinus Torvalds return error; 44951da177e4SLinus Torvalds 44961da177e4SLinus Torvalds /* Obtain a SID for the context, if one was specified. */ 44971da177e4SLinus Torvalds if (size && str[1] && str[1] != '\n') { 44981da177e4SLinus Torvalds if (str[size-1] == '\n') { 44991da177e4SLinus Torvalds str[size-1] = 0; 45001da177e4SLinus Torvalds size--; 45011da177e4SLinus Torvalds } 45021da177e4SLinus Torvalds error = security_context_to_sid(value, size, &sid); 45031da177e4SLinus Torvalds if (error) 45041da177e4SLinus Torvalds return error; 45051da177e4SLinus Torvalds } 45061da177e4SLinus Torvalds 45071da177e4SLinus Torvalds /* Permission checking based on the specified context is 45081da177e4SLinus Torvalds performed during the actual operation (execve, 45091da177e4SLinus Torvalds open/mkdir/...), when we know the full context of the 45101da177e4SLinus Torvalds operation. See selinux_bprm_set_security for the execve 45111da177e4SLinus Torvalds checks and may_create for the file creation checks. The 45121da177e4SLinus Torvalds operation will then fail if the context is not permitted. */ 45131da177e4SLinus Torvalds tsec = p->security; 45141da177e4SLinus Torvalds if (!strcmp(name, "exec")) 45151da177e4SLinus Torvalds tsec->exec_sid = sid; 45161da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 45171da177e4SLinus Torvalds tsec->create_sid = sid; 45184eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) { 45194eb582cfSMichael LeMay error = may_create_key(sid, p); 45204eb582cfSMichael LeMay if (error) 45214eb582cfSMichael LeMay return error; 45224eb582cfSMichael LeMay tsec->keycreate_sid = sid; 452342c3e03eSEric Paris } else if (!strcmp(name, "sockcreate")) 452442c3e03eSEric Paris tsec->sockcreate_sid = sid; 452542c3e03eSEric Paris else if (!strcmp(name, "current")) { 45261da177e4SLinus Torvalds struct av_decision avd; 45271da177e4SLinus Torvalds 45281da177e4SLinus Torvalds if (sid == 0) 45291da177e4SLinus Torvalds return -EINVAL; 45301da177e4SLinus Torvalds 45311da177e4SLinus Torvalds /* Only allow single threaded processes to change context */ 45321da177e4SLinus Torvalds if (atomic_read(&p->mm->mm_users) != 1) { 45331da177e4SLinus Torvalds struct task_struct *g, *t; 45341da177e4SLinus Torvalds struct mm_struct *mm = p->mm; 45351da177e4SLinus Torvalds read_lock(&tasklist_lock); 45361da177e4SLinus Torvalds do_each_thread(g, t) 45371da177e4SLinus Torvalds if (t->mm == mm && t != p) { 45381da177e4SLinus Torvalds read_unlock(&tasklist_lock); 45391da177e4SLinus Torvalds return -EPERM; 45401da177e4SLinus Torvalds } 45411da177e4SLinus Torvalds while_each_thread(g, t); 45421da177e4SLinus Torvalds read_unlock(&tasklist_lock); 45431da177e4SLinus Torvalds } 45441da177e4SLinus Torvalds 45451da177e4SLinus Torvalds /* Check permissions for the transition. */ 45461da177e4SLinus Torvalds error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS, 45471da177e4SLinus Torvalds PROCESS__DYNTRANSITION, NULL); 45481da177e4SLinus Torvalds if (error) 45491da177e4SLinus Torvalds return error; 45501da177e4SLinus Torvalds 45511da177e4SLinus Torvalds /* Check for ptracing, and update the task SID if ok. 45521da177e4SLinus Torvalds Otherwise, leave SID unchanged and fail. */ 45531da177e4SLinus Torvalds task_lock(p); 45541da177e4SLinus Torvalds if (p->ptrace & PT_PTRACED) { 45551da177e4SLinus Torvalds error = avc_has_perm_noaudit(tsec->ptrace_sid, sid, 45561da177e4SLinus Torvalds SECCLASS_PROCESS, 45571da177e4SLinus Torvalds PROCESS__PTRACE, &avd); 45581da177e4SLinus Torvalds if (!error) 45591da177e4SLinus Torvalds tsec->sid = sid; 45601da177e4SLinus Torvalds task_unlock(p); 45611da177e4SLinus Torvalds avc_audit(tsec->ptrace_sid, sid, SECCLASS_PROCESS, 45621da177e4SLinus Torvalds PROCESS__PTRACE, &avd, error, NULL); 45631da177e4SLinus Torvalds if (error) 45641da177e4SLinus Torvalds return error; 45651da177e4SLinus Torvalds } else { 45661da177e4SLinus Torvalds tsec->sid = sid; 45671da177e4SLinus Torvalds task_unlock(p); 45681da177e4SLinus Torvalds } 45691da177e4SLinus Torvalds } 45701da177e4SLinus Torvalds else 45711da177e4SLinus Torvalds return -EINVAL; 45721da177e4SLinus Torvalds 45731da177e4SLinus Torvalds return size; 45741da177e4SLinus Torvalds } 45751da177e4SLinus Torvalds 4576dc49c1f9SCatherine Zhang static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) 4577dc49c1f9SCatherine Zhang { 4578dc49c1f9SCatherine Zhang return security_sid_to_context(secid, secdata, seclen); 4579dc49c1f9SCatherine Zhang } 4580dc49c1f9SCatherine Zhang 4581dc49c1f9SCatherine Zhang static void selinux_release_secctx(char *secdata, u32 seclen) 4582dc49c1f9SCatherine Zhang { 4583dc49c1f9SCatherine Zhang if (secdata) 4584dc49c1f9SCatherine Zhang kfree(secdata); 4585dc49c1f9SCatherine Zhang } 4586dc49c1f9SCatherine Zhang 4587d720024eSMichael LeMay #ifdef CONFIG_KEYS 4588d720024eSMichael LeMay 45897e047ef5SDavid Howells static int selinux_key_alloc(struct key *k, struct task_struct *tsk, 45907e047ef5SDavid Howells unsigned long flags) 4591d720024eSMichael LeMay { 4592d720024eSMichael LeMay struct task_security_struct *tsec = tsk->security; 4593d720024eSMichael LeMay struct key_security_struct *ksec; 4594d720024eSMichael LeMay 4595d720024eSMichael LeMay ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL); 4596d720024eSMichael LeMay if (!ksec) 4597d720024eSMichael LeMay return -ENOMEM; 4598d720024eSMichael LeMay 4599d720024eSMichael LeMay ksec->obj = k; 46004eb582cfSMichael LeMay if (tsec->keycreate_sid) 46014eb582cfSMichael LeMay ksec->sid = tsec->keycreate_sid; 46024eb582cfSMichael LeMay else 4603d720024eSMichael LeMay ksec->sid = tsec->sid; 4604d720024eSMichael LeMay k->security = ksec; 4605d720024eSMichael LeMay 4606d720024eSMichael LeMay return 0; 4607d720024eSMichael LeMay } 4608d720024eSMichael LeMay 4609d720024eSMichael LeMay static void selinux_key_free(struct key *k) 4610d720024eSMichael LeMay { 4611d720024eSMichael LeMay struct key_security_struct *ksec = k->security; 4612d720024eSMichael LeMay 4613d720024eSMichael LeMay k->security = NULL; 4614d720024eSMichael LeMay kfree(ksec); 4615d720024eSMichael LeMay } 4616d720024eSMichael LeMay 4617d720024eSMichael LeMay static int selinux_key_permission(key_ref_t key_ref, 4618d720024eSMichael LeMay struct task_struct *ctx, 4619d720024eSMichael LeMay key_perm_t perm) 4620d720024eSMichael LeMay { 4621d720024eSMichael LeMay struct key *key; 4622d720024eSMichael LeMay struct task_security_struct *tsec; 4623d720024eSMichael LeMay struct key_security_struct *ksec; 4624d720024eSMichael LeMay 4625d720024eSMichael LeMay key = key_ref_to_ptr(key_ref); 4626d720024eSMichael LeMay 4627d720024eSMichael LeMay tsec = ctx->security; 4628d720024eSMichael LeMay ksec = key->security; 4629d720024eSMichael LeMay 4630d720024eSMichael LeMay /* if no specific permissions are requested, we skip the 4631d720024eSMichael LeMay permission check. No serious, additional covert channels 4632d720024eSMichael LeMay appear to be created. */ 4633d720024eSMichael LeMay if (perm == 0) 4634d720024eSMichael LeMay return 0; 4635d720024eSMichael LeMay 4636d720024eSMichael LeMay return avc_has_perm(tsec->sid, ksec->sid, 4637d720024eSMichael LeMay SECCLASS_KEY, perm, NULL); 4638d720024eSMichael LeMay } 4639d720024eSMichael LeMay 4640d720024eSMichael LeMay #endif 4641d720024eSMichael LeMay 46421da177e4SLinus Torvalds static struct security_operations selinux_ops = { 46431da177e4SLinus Torvalds .ptrace = selinux_ptrace, 46441da177e4SLinus Torvalds .capget = selinux_capget, 46451da177e4SLinus Torvalds .capset_check = selinux_capset_check, 46461da177e4SLinus Torvalds .capset_set = selinux_capset_set, 46471da177e4SLinus Torvalds .sysctl = selinux_sysctl, 46481da177e4SLinus Torvalds .capable = selinux_capable, 46491da177e4SLinus Torvalds .quotactl = selinux_quotactl, 46501da177e4SLinus Torvalds .quota_on = selinux_quota_on, 46511da177e4SLinus Torvalds .syslog = selinux_syslog, 46521da177e4SLinus Torvalds .vm_enough_memory = selinux_vm_enough_memory, 46531da177e4SLinus Torvalds 46541da177e4SLinus Torvalds .netlink_send = selinux_netlink_send, 46551da177e4SLinus Torvalds .netlink_recv = selinux_netlink_recv, 46561da177e4SLinus Torvalds 46571da177e4SLinus Torvalds .bprm_alloc_security = selinux_bprm_alloc_security, 46581da177e4SLinus Torvalds .bprm_free_security = selinux_bprm_free_security, 46591da177e4SLinus Torvalds .bprm_apply_creds = selinux_bprm_apply_creds, 46601da177e4SLinus Torvalds .bprm_post_apply_creds = selinux_bprm_post_apply_creds, 46611da177e4SLinus Torvalds .bprm_set_security = selinux_bprm_set_security, 46621da177e4SLinus Torvalds .bprm_check_security = selinux_bprm_check_security, 46631da177e4SLinus Torvalds .bprm_secureexec = selinux_bprm_secureexec, 46641da177e4SLinus Torvalds 46651da177e4SLinus Torvalds .sb_alloc_security = selinux_sb_alloc_security, 46661da177e4SLinus Torvalds .sb_free_security = selinux_sb_free_security, 46671da177e4SLinus Torvalds .sb_copy_data = selinux_sb_copy_data, 46681da177e4SLinus Torvalds .sb_kern_mount = selinux_sb_kern_mount, 46691da177e4SLinus Torvalds .sb_statfs = selinux_sb_statfs, 46701da177e4SLinus Torvalds .sb_mount = selinux_mount, 46711da177e4SLinus Torvalds .sb_umount = selinux_umount, 46721da177e4SLinus Torvalds 46731da177e4SLinus Torvalds .inode_alloc_security = selinux_inode_alloc_security, 46741da177e4SLinus Torvalds .inode_free_security = selinux_inode_free_security, 46755e41ff9eSStephen Smalley .inode_init_security = selinux_inode_init_security, 46761da177e4SLinus Torvalds .inode_create = selinux_inode_create, 46771da177e4SLinus Torvalds .inode_link = selinux_inode_link, 46781da177e4SLinus Torvalds .inode_unlink = selinux_inode_unlink, 46791da177e4SLinus Torvalds .inode_symlink = selinux_inode_symlink, 46801da177e4SLinus Torvalds .inode_mkdir = selinux_inode_mkdir, 46811da177e4SLinus Torvalds .inode_rmdir = selinux_inode_rmdir, 46821da177e4SLinus Torvalds .inode_mknod = selinux_inode_mknod, 46831da177e4SLinus Torvalds .inode_rename = selinux_inode_rename, 46841da177e4SLinus Torvalds .inode_readlink = selinux_inode_readlink, 46851da177e4SLinus Torvalds .inode_follow_link = selinux_inode_follow_link, 46861da177e4SLinus Torvalds .inode_permission = selinux_inode_permission, 46871da177e4SLinus Torvalds .inode_setattr = selinux_inode_setattr, 46881da177e4SLinus Torvalds .inode_getattr = selinux_inode_getattr, 46891da177e4SLinus Torvalds .inode_setxattr = selinux_inode_setxattr, 46901da177e4SLinus Torvalds .inode_post_setxattr = selinux_inode_post_setxattr, 46911da177e4SLinus Torvalds .inode_getxattr = selinux_inode_getxattr, 46921da177e4SLinus Torvalds .inode_listxattr = selinux_inode_listxattr, 46931da177e4SLinus Torvalds .inode_removexattr = selinux_inode_removexattr, 46948c8570fbSDustin Kirkland .inode_xattr_getsuffix = selinux_inode_xattr_getsuffix, 46951da177e4SLinus Torvalds .inode_getsecurity = selinux_inode_getsecurity, 46961da177e4SLinus Torvalds .inode_setsecurity = selinux_inode_setsecurity, 46971da177e4SLinus Torvalds .inode_listsecurity = selinux_inode_listsecurity, 46981da177e4SLinus Torvalds 46991da177e4SLinus Torvalds .file_permission = selinux_file_permission, 47001da177e4SLinus Torvalds .file_alloc_security = selinux_file_alloc_security, 47011da177e4SLinus Torvalds .file_free_security = selinux_file_free_security, 47021da177e4SLinus Torvalds .file_ioctl = selinux_file_ioctl, 47031da177e4SLinus Torvalds .file_mmap = selinux_file_mmap, 47041da177e4SLinus Torvalds .file_mprotect = selinux_file_mprotect, 47051da177e4SLinus Torvalds .file_lock = selinux_file_lock, 47061da177e4SLinus Torvalds .file_fcntl = selinux_file_fcntl, 47071da177e4SLinus Torvalds .file_set_fowner = selinux_file_set_fowner, 47081da177e4SLinus Torvalds .file_send_sigiotask = selinux_file_send_sigiotask, 47091da177e4SLinus Torvalds .file_receive = selinux_file_receive, 47101da177e4SLinus Torvalds 47111da177e4SLinus Torvalds .task_create = selinux_task_create, 47121da177e4SLinus Torvalds .task_alloc_security = selinux_task_alloc_security, 47131da177e4SLinus Torvalds .task_free_security = selinux_task_free_security, 47141da177e4SLinus Torvalds .task_setuid = selinux_task_setuid, 47151da177e4SLinus Torvalds .task_post_setuid = selinux_task_post_setuid, 47161da177e4SLinus Torvalds .task_setgid = selinux_task_setgid, 47171da177e4SLinus Torvalds .task_setpgid = selinux_task_setpgid, 47181da177e4SLinus Torvalds .task_getpgid = selinux_task_getpgid, 47191da177e4SLinus Torvalds .task_getsid = selinux_task_getsid, 4720f9008e4cSDavid Quigley .task_getsecid = selinux_task_getsecid, 47211da177e4SLinus Torvalds .task_setgroups = selinux_task_setgroups, 47221da177e4SLinus Torvalds .task_setnice = selinux_task_setnice, 472303e68060SJames Morris .task_setioprio = selinux_task_setioprio, 4724a1836a42SDavid Quigley .task_getioprio = selinux_task_getioprio, 47251da177e4SLinus Torvalds .task_setrlimit = selinux_task_setrlimit, 47261da177e4SLinus Torvalds .task_setscheduler = selinux_task_setscheduler, 47271da177e4SLinus Torvalds .task_getscheduler = selinux_task_getscheduler, 472835601547SDavid Quigley .task_movememory = selinux_task_movememory, 47291da177e4SLinus Torvalds .task_kill = selinux_task_kill, 47301da177e4SLinus Torvalds .task_wait = selinux_task_wait, 47311da177e4SLinus Torvalds .task_prctl = selinux_task_prctl, 47321da177e4SLinus Torvalds .task_reparent_to_init = selinux_task_reparent_to_init, 47331da177e4SLinus Torvalds .task_to_inode = selinux_task_to_inode, 47341da177e4SLinus Torvalds 47351da177e4SLinus Torvalds .ipc_permission = selinux_ipc_permission, 47361da177e4SLinus Torvalds 47371da177e4SLinus Torvalds .msg_msg_alloc_security = selinux_msg_msg_alloc_security, 47381da177e4SLinus Torvalds .msg_msg_free_security = selinux_msg_msg_free_security, 47391da177e4SLinus Torvalds 47401da177e4SLinus Torvalds .msg_queue_alloc_security = selinux_msg_queue_alloc_security, 47411da177e4SLinus Torvalds .msg_queue_free_security = selinux_msg_queue_free_security, 47421da177e4SLinus Torvalds .msg_queue_associate = selinux_msg_queue_associate, 47431da177e4SLinus Torvalds .msg_queue_msgctl = selinux_msg_queue_msgctl, 47441da177e4SLinus Torvalds .msg_queue_msgsnd = selinux_msg_queue_msgsnd, 47451da177e4SLinus Torvalds .msg_queue_msgrcv = selinux_msg_queue_msgrcv, 47461da177e4SLinus Torvalds 47471da177e4SLinus Torvalds .shm_alloc_security = selinux_shm_alloc_security, 47481da177e4SLinus Torvalds .shm_free_security = selinux_shm_free_security, 47491da177e4SLinus Torvalds .shm_associate = selinux_shm_associate, 47501da177e4SLinus Torvalds .shm_shmctl = selinux_shm_shmctl, 47511da177e4SLinus Torvalds .shm_shmat = selinux_shm_shmat, 47521da177e4SLinus Torvalds 47531da177e4SLinus Torvalds .sem_alloc_security = selinux_sem_alloc_security, 47541da177e4SLinus Torvalds .sem_free_security = selinux_sem_free_security, 47551da177e4SLinus Torvalds .sem_associate = selinux_sem_associate, 47561da177e4SLinus Torvalds .sem_semctl = selinux_sem_semctl, 47571da177e4SLinus Torvalds .sem_semop = selinux_sem_semop, 47581da177e4SLinus Torvalds 47591da177e4SLinus Torvalds .register_security = selinux_register_security, 47601da177e4SLinus Torvalds .unregister_security = selinux_unregister_security, 47611da177e4SLinus Torvalds 47621da177e4SLinus Torvalds .d_instantiate = selinux_d_instantiate, 47631da177e4SLinus Torvalds 47641da177e4SLinus Torvalds .getprocattr = selinux_getprocattr, 47651da177e4SLinus Torvalds .setprocattr = selinux_setprocattr, 47661da177e4SLinus Torvalds 4767dc49c1f9SCatherine Zhang .secid_to_secctx = selinux_secid_to_secctx, 4768dc49c1f9SCatherine Zhang .release_secctx = selinux_release_secctx, 4769dc49c1f9SCatherine Zhang 47701da177e4SLinus Torvalds .unix_stream_connect = selinux_socket_unix_stream_connect, 47711da177e4SLinus Torvalds .unix_may_send = selinux_socket_unix_may_send, 47721da177e4SLinus Torvalds 47731da177e4SLinus Torvalds .socket_create = selinux_socket_create, 47741da177e4SLinus Torvalds .socket_post_create = selinux_socket_post_create, 47751da177e4SLinus Torvalds .socket_bind = selinux_socket_bind, 47761da177e4SLinus Torvalds .socket_connect = selinux_socket_connect, 47771da177e4SLinus Torvalds .socket_listen = selinux_socket_listen, 47781da177e4SLinus Torvalds .socket_accept = selinux_socket_accept, 47791da177e4SLinus Torvalds .socket_sendmsg = selinux_socket_sendmsg, 47801da177e4SLinus Torvalds .socket_recvmsg = selinux_socket_recvmsg, 47811da177e4SLinus Torvalds .socket_getsockname = selinux_socket_getsockname, 47821da177e4SLinus Torvalds .socket_getpeername = selinux_socket_getpeername, 47831da177e4SLinus Torvalds .socket_getsockopt = selinux_socket_getsockopt, 47841da177e4SLinus Torvalds .socket_setsockopt = selinux_socket_setsockopt, 47851da177e4SLinus Torvalds .socket_shutdown = selinux_socket_shutdown, 47861da177e4SLinus Torvalds .socket_sock_rcv_skb = selinux_socket_sock_rcv_skb, 47872c7946a7SCatherine Zhang .socket_getpeersec_stream = selinux_socket_getpeersec_stream, 47882c7946a7SCatherine Zhang .socket_getpeersec_dgram = selinux_socket_getpeersec_dgram, 47891da177e4SLinus Torvalds .sk_alloc_security = selinux_sk_alloc_security, 47901da177e4SLinus Torvalds .sk_free_security = selinux_sk_free_security, 4791892c141eSVenkat Yekkirala .sk_clone_security = selinux_sk_clone_security, 4792beb8d13bSVenkat Yekkirala .sk_getsecid = selinux_sk_getsecid, 47934237c75cSVenkat Yekkirala .sock_graft = selinux_sock_graft, 47944237c75cSVenkat Yekkirala .inet_conn_request = selinux_inet_conn_request, 47954237c75cSVenkat Yekkirala .inet_csk_clone = selinux_inet_csk_clone, 47966b877699SVenkat Yekkirala .inet_conn_established = selinux_inet_conn_established, 47974237c75cSVenkat Yekkirala .req_classify_flow = selinux_req_classify_flow, 4798d28d1e08STrent Jaeger 4799d28d1e08STrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM 4800d28d1e08STrent Jaeger .xfrm_policy_alloc_security = selinux_xfrm_policy_alloc, 4801d28d1e08STrent Jaeger .xfrm_policy_clone_security = selinux_xfrm_policy_clone, 4802d28d1e08STrent Jaeger .xfrm_policy_free_security = selinux_xfrm_policy_free, 4803c8c05a8eSCatherine Zhang .xfrm_policy_delete_security = selinux_xfrm_policy_delete, 4804d28d1e08STrent Jaeger .xfrm_state_alloc_security = selinux_xfrm_state_alloc, 4805d28d1e08STrent Jaeger .xfrm_state_free_security = selinux_xfrm_state_free, 4806c8c05a8eSCatherine Zhang .xfrm_state_delete_security = selinux_xfrm_state_delete, 4807d28d1e08STrent Jaeger .xfrm_policy_lookup = selinux_xfrm_policy_lookup, 4808e0d1caa7SVenkat Yekkirala .xfrm_state_pol_flow_match = selinux_xfrm_state_pol_flow_match, 4809e0d1caa7SVenkat Yekkirala .xfrm_decode_session = selinux_xfrm_decode_session, 48101da177e4SLinus Torvalds #endif 4811d720024eSMichael LeMay 4812d720024eSMichael LeMay #ifdef CONFIG_KEYS 4813d720024eSMichael LeMay .key_alloc = selinux_key_alloc, 4814d720024eSMichael LeMay .key_free = selinux_key_free, 4815d720024eSMichael LeMay .key_permission = selinux_key_permission, 4816d720024eSMichael LeMay #endif 48171da177e4SLinus Torvalds }; 48181da177e4SLinus Torvalds 48191da177e4SLinus Torvalds static __init int selinux_init(void) 48201da177e4SLinus Torvalds { 48211da177e4SLinus Torvalds struct task_security_struct *tsec; 48221da177e4SLinus Torvalds 48231da177e4SLinus Torvalds if (!selinux_enabled) { 48241da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Disabled at boot.\n"); 48251da177e4SLinus Torvalds return 0; 48261da177e4SLinus Torvalds } 48271da177e4SLinus Torvalds 48281da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Initializing.\n"); 48291da177e4SLinus Torvalds 48301da177e4SLinus Torvalds /* Set the security state for the initial task. */ 48311da177e4SLinus Torvalds if (task_alloc_security(current)) 48321da177e4SLinus Torvalds panic("SELinux: Failed to initialize initial task.\n"); 48331da177e4SLinus Torvalds tsec = current->security; 48341da177e4SLinus Torvalds tsec->osid = tsec->sid = SECINITSID_KERNEL; 48351da177e4SLinus Torvalds 48367cae7e26SJames Morris sel_inode_cache = kmem_cache_create("selinux_inode_security", 48377cae7e26SJames Morris sizeof(struct inode_security_struct), 48387cae7e26SJames Morris 0, SLAB_PANIC, NULL, NULL); 48391da177e4SLinus Torvalds avc_init(); 48401da177e4SLinus Torvalds 48411da177e4SLinus Torvalds original_ops = secondary_ops = security_ops; 48421da177e4SLinus Torvalds if (!secondary_ops) 48431da177e4SLinus Torvalds panic ("SELinux: No initial security operations\n"); 48441da177e4SLinus Torvalds if (register_security (&selinux_ops)) 48451da177e4SLinus Torvalds panic("SELinux: Unable to register with kernel.\n"); 48461da177e4SLinus Torvalds 48471da177e4SLinus Torvalds if (selinux_enforcing) { 48481da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Starting in enforcing mode\n"); 48491da177e4SLinus Torvalds } else { 48501da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Starting in permissive mode\n"); 48511da177e4SLinus Torvalds } 4852d720024eSMichael LeMay 4853d720024eSMichael LeMay #ifdef CONFIG_KEYS 4854d720024eSMichael LeMay /* Add security information to initial keyrings */ 48554eb582cfSMichael LeMay selinux_key_alloc(&root_user_keyring, current, 48567e047ef5SDavid Howells KEY_ALLOC_NOT_IN_QUOTA); 48574eb582cfSMichael LeMay selinux_key_alloc(&root_session_keyring, current, 48587e047ef5SDavid Howells KEY_ALLOC_NOT_IN_QUOTA); 4859d720024eSMichael LeMay #endif 4860d720024eSMichael LeMay 48611da177e4SLinus Torvalds return 0; 48621da177e4SLinus Torvalds } 48631da177e4SLinus Torvalds 48641da177e4SLinus Torvalds void selinux_complete_init(void) 48651da177e4SLinus Torvalds { 48661da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Completing initialization.\n"); 48671da177e4SLinus Torvalds 48681da177e4SLinus Torvalds /* Set up any superblocks initialized prior to the policy load. */ 48691da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Setting up existing superblocks.\n"); 4870ba0c19edSStephen Smalley spin_lock(&sb_lock); 48711da177e4SLinus Torvalds spin_lock(&sb_security_lock); 48721da177e4SLinus Torvalds next_sb: 48731da177e4SLinus Torvalds if (!list_empty(&superblock_security_head)) { 48741da177e4SLinus Torvalds struct superblock_security_struct *sbsec = 48751da177e4SLinus Torvalds list_entry(superblock_security_head.next, 48761da177e4SLinus Torvalds struct superblock_security_struct, 48771da177e4SLinus Torvalds list); 48781da177e4SLinus Torvalds struct super_block *sb = sbsec->sb; 48791da177e4SLinus Torvalds sb->s_count++; 48801da177e4SLinus Torvalds spin_unlock(&sb_security_lock); 4881ba0c19edSStephen Smalley spin_unlock(&sb_lock); 48821da177e4SLinus Torvalds down_read(&sb->s_umount); 48831da177e4SLinus Torvalds if (sb->s_root) 48841da177e4SLinus Torvalds superblock_doinit(sb, NULL); 48851da177e4SLinus Torvalds drop_super(sb); 4886ba0c19edSStephen Smalley spin_lock(&sb_lock); 48871da177e4SLinus Torvalds spin_lock(&sb_security_lock); 48881da177e4SLinus Torvalds list_del_init(&sbsec->list); 48891da177e4SLinus Torvalds goto next_sb; 48901da177e4SLinus Torvalds } 48911da177e4SLinus Torvalds spin_unlock(&sb_security_lock); 4892ba0c19edSStephen Smalley spin_unlock(&sb_lock); 48931da177e4SLinus Torvalds } 48941da177e4SLinus Torvalds 48951da177e4SLinus Torvalds /* SELinux requires early initialization in order to label 48961da177e4SLinus Torvalds all processes and objects when they are created. */ 48971da177e4SLinus Torvalds security_initcall(selinux_init); 48981da177e4SLinus Torvalds 4899c2b507fdSStephen Smalley #if defined(CONFIG_NETFILTER) 49001da177e4SLinus Torvalds 49011da177e4SLinus Torvalds static struct nf_hook_ops selinux_ipv4_op = { 49021da177e4SLinus Torvalds .hook = selinux_ipv4_postroute_last, 49031da177e4SLinus Torvalds .owner = THIS_MODULE, 49041da177e4SLinus Torvalds .pf = PF_INET, 49051da177e4SLinus Torvalds .hooknum = NF_IP_POST_ROUTING, 49061da177e4SLinus Torvalds .priority = NF_IP_PRI_SELINUX_LAST, 49071da177e4SLinus Torvalds }; 49081da177e4SLinus Torvalds 49091da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 49101da177e4SLinus Torvalds 49111da177e4SLinus Torvalds static struct nf_hook_ops selinux_ipv6_op = { 49121da177e4SLinus Torvalds .hook = selinux_ipv6_postroute_last, 49131da177e4SLinus Torvalds .owner = THIS_MODULE, 49141da177e4SLinus Torvalds .pf = PF_INET6, 49151da177e4SLinus Torvalds .hooknum = NF_IP6_POST_ROUTING, 49161da177e4SLinus Torvalds .priority = NF_IP6_PRI_SELINUX_LAST, 49171da177e4SLinus Torvalds }; 49181da177e4SLinus Torvalds 49191da177e4SLinus Torvalds #endif /* IPV6 */ 49201da177e4SLinus Torvalds 49211da177e4SLinus Torvalds static int __init selinux_nf_ip_init(void) 49221da177e4SLinus Torvalds { 49231da177e4SLinus Torvalds int err = 0; 49241da177e4SLinus Torvalds 49251da177e4SLinus Torvalds if (!selinux_enabled) 49261da177e4SLinus Torvalds goto out; 49271da177e4SLinus Torvalds 49281da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Registering netfilter hooks\n"); 49291da177e4SLinus Torvalds 49301da177e4SLinus Torvalds err = nf_register_hook(&selinux_ipv4_op); 49311da177e4SLinus Torvalds if (err) 49321da177e4SLinus Torvalds panic("SELinux: nf_register_hook for IPv4: error %d\n", err); 49331da177e4SLinus Torvalds 49341da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 49351da177e4SLinus Torvalds 49361da177e4SLinus Torvalds err = nf_register_hook(&selinux_ipv6_op); 49371da177e4SLinus Torvalds if (err) 49381da177e4SLinus Torvalds panic("SELinux: nf_register_hook for IPv6: error %d\n", err); 49391da177e4SLinus Torvalds 49401da177e4SLinus Torvalds #endif /* IPV6 */ 4941d28d1e08STrent Jaeger 49421da177e4SLinus Torvalds out: 49431da177e4SLinus Torvalds return err; 49441da177e4SLinus Torvalds } 49451da177e4SLinus Torvalds 49461da177e4SLinus Torvalds __initcall(selinux_nf_ip_init); 49471da177e4SLinus Torvalds 49481da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 49491da177e4SLinus Torvalds static void selinux_nf_ip_exit(void) 49501da177e4SLinus Torvalds { 49511da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Unregistering netfilter hooks\n"); 49521da177e4SLinus Torvalds 49531da177e4SLinus Torvalds nf_unregister_hook(&selinux_ipv4_op); 49541da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 49551da177e4SLinus Torvalds nf_unregister_hook(&selinux_ipv6_op); 49561da177e4SLinus Torvalds #endif /* IPV6 */ 49571da177e4SLinus Torvalds } 49581da177e4SLinus Torvalds #endif 49591da177e4SLinus Torvalds 4960c2b507fdSStephen Smalley #else /* CONFIG_NETFILTER */ 49611da177e4SLinus Torvalds 49621da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 49631da177e4SLinus Torvalds #define selinux_nf_ip_exit() 49641da177e4SLinus Torvalds #endif 49651da177e4SLinus Torvalds 4966c2b507fdSStephen Smalley #endif /* CONFIG_NETFILTER */ 49671da177e4SLinus Torvalds 49681da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 49691da177e4SLinus Torvalds int selinux_disable(void) 49701da177e4SLinus Torvalds { 49711da177e4SLinus Torvalds extern void exit_sel_fs(void); 49721da177e4SLinus Torvalds static int selinux_disabled = 0; 49731da177e4SLinus Torvalds 49741da177e4SLinus Torvalds if (ss_initialized) { 49751da177e4SLinus Torvalds /* Not permitted after initial policy load. */ 49761da177e4SLinus Torvalds return -EINVAL; 49771da177e4SLinus Torvalds } 49781da177e4SLinus Torvalds 49791da177e4SLinus Torvalds if (selinux_disabled) { 49801da177e4SLinus Torvalds /* Only do this once. */ 49811da177e4SLinus Torvalds return -EINVAL; 49821da177e4SLinus Torvalds } 49831da177e4SLinus Torvalds 49841da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Disabled at runtime.\n"); 49851da177e4SLinus Torvalds 49861da177e4SLinus Torvalds selinux_disabled = 1; 498730d55280SStephen Smalley selinux_enabled = 0; 49881da177e4SLinus Torvalds 49891da177e4SLinus Torvalds /* Reset security_ops to the secondary module, dummy or capability. */ 49901da177e4SLinus Torvalds security_ops = secondary_ops; 49911da177e4SLinus Torvalds 49921da177e4SLinus Torvalds /* Unregister netfilter hooks. */ 49931da177e4SLinus Torvalds selinux_nf_ip_exit(); 49941da177e4SLinus Torvalds 49951da177e4SLinus Torvalds /* Unregister selinuxfs. */ 49961da177e4SLinus Torvalds exit_sel_fs(); 49971da177e4SLinus Torvalds 49981da177e4SLinus Torvalds return 0; 49991da177e4SLinus Torvalds } 50001da177e4SLinus Torvalds #endif 50011da177e4SLinus Torvalds 50021da177e4SLinus Torvalds 5003