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/spinlock.h> 391da177e4SLinus Torvalds #include <linux/syscalls.h> 401da177e4SLinus Torvalds #include <linux/file.h> 411da177e4SLinus Torvalds #include <linux/namei.h> 421da177e4SLinus Torvalds #include <linux/mount.h> 431da177e4SLinus Torvalds #include <linux/ext2_fs.h> 441da177e4SLinus Torvalds #include <linux/proc_fs.h> 451da177e4SLinus Torvalds #include <linux/kd.h> 461da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h> 471da177e4SLinus Torvalds #include <linux/netfilter_ipv6.h> 481da177e4SLinus Torvalds #include <linux/tty.h> 491da177e4SLinus Torvalds #include <net/icmp.h> 501da177e4SLinus Torvalds #include <net/ip.h> /* for sysctl_local_port_range[] */ 511da177e4SLinus Torvalds #include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */ 521da177e4SLinus Torvalds #include <asm/uaccess.h> 531da177e4SLinus Torvalds #include <asm/ioctls.h> 541da177e4SLinus Torvalds #include <linux/bitops.h> 551da177e4SLinus Torvalds #include <linux/interrupt.h> 561da177e4SLinus Torvalds #include <linux/netdevice.h> /* for network interface checks */ 571da177e4SLinus Torvalds #include <linux/netlink.h> 581da177e4SLinus Torvalds #include <linux/tcp.h> 591da177e4SLinus Torvalds #include <linux/udp.h> 602ee92d46SJames Morris #include <linux/dccp.h> 611da177e4SLinus Torvalds #include <linux/quota.h> 621da177e4SLinus Torvalds #include <linux/un.h> /* for Unix socket types */ 631da177e4SLinus Torvalds #include <net/af_unix.h> /* for Unix socket types */ 641da177e4SLinus Torvalds #include <linux/parser.h> 651da177e4SLinus Torvalds #include <linux/nfs_mount.h> 661da177e4SLinus Torvalds #include <net/ipv6.h> 671da177e4SLinus Torvalds #include <linux/hugetlb.h> 681da177e4SLinus Torvalds #include <linux/personality.h> 691da177e4SLinus Torvalds #include <linux/sysctl.h> 701da177e4SLinus Torvalds #include <linux/audit.h> 716931dfc9SEric Paris #include <linux/string.h> 72877ce7c1SCatherine Zhang #include <linux/selinux.h> 7323970741SEric Paris #include <linux/mutex.h> 741da177e4SLinus Torvalds 751da177e4SLinus Torvalds #include "avc.h" 761da177e4SLinus Torvalds #include "objsec.h" 771da177e4SLinus Torvalds #include "netif.h" 78d28d1e08STrent Jaeger #include "xfrm.h" 79c60475bfSPaul Moore #include "netlabel.h" 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds #define XATTR_SELINUX_SUFFIX "selinux" 821da177e4SLinus Torvalds #define XATTR_NAME_SELINUX XATTR_SECURITY_PREFIX XATTR_SELINUX_SUFFIX 831da177e4SLinus Torvalds 841da177e4SLinus Torvalds extern unsigned int policydb_loaded_version; 851da177e4SLinus Torvalds extern int selinux_nlmsg_lookup(u16 sclass, u16 nlmsg_type, u32 *perm); 864e5ab4cbSJames Morris extern int selinux_compat_net; 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP 891da177e4SLinus Torvalds int selinux_enforcing = 0; 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds static int __init enforcing_setup(char *str) 921da177e4SLinus Torvalds { 931da177e4SLinus Torvalds selinux_enforcing = simple_strtol(str,NULL,0); 941da177e4SLinus Torvalds return 1; 951da177e4SLinus Torvalds } 961da177e4SLinus Torvalds __setup("enforcing=", enforcing_setup); 971da177e4SLinus Torvalds #endif 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM 1001da177e4SLinus Torvalds int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE; 1011da177e4SLinus Torvalds 1021da177e4SLinus Torvalds static int __init selinux_enabled_setup(char *str) 1031da177e4SLinus Torvalds { 1041da177e4SLinus Torvalds selinux_enabled = simple_strtol(str, NULL, 0); 1051da177e4SLinus Torvalds return 1; 1061da177e4SLinus Torvalds } 1071da177e4SLinus Torvalds __setup("selinux=", selinux_enabled_setup); 10830d55280SStephen Smalley #else 10930d55280SStephen Smalley int selinux_enabled = 1; 1101da177e4SLinus Torvalds #endif 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds /* Original (dummy) security module. */ 1131da177e4SLinus Torvalds static struct security_operations *original_ops = NULL; 1141da177e4SLinus Torvalds 1151da177e4SLinus Torvalds /* Minimal support for a secondary security module, 1161da177e4SLinus Torvalds just to allow the use of the dummy or capability modules. 1171da177e4SLinus Torvalds The owlsm module can alternatively be used as a secondary 1181da177e4SLinus Torvalds module as long as CONFIG_OWLSM_FD is not enabled. */ 1191da177e4SLinus Torvalds static struct security_operations *secondary_ops = NULL; 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds /* Lists of inode and superblock security structures initialized 1221da177e4SLinus Torvalds before the policy was loaded. */ 1231da177e4SLinus Torvalds static LIST_HEAD(superblock_security_head); 1241da177e4SLinus Torvalds static DEFINE_SPINLOCK(sb_security_lock); 1251da177e4SLinus Torvalds 126e18b890bSChristoph Lameter static struct kmem_cache *sel_inode_cache; 1277cae7e26SJames Morris 1288c8570fbSDustin Kirkland /* Return security context for a given sid or just the context 1298c8570fbSDustin Kirkland length if the buffer is null or length is 0 */ 1308c8570fbSDustin Kirkland static int selinux_getsecurity(u32 sid, void *buffer, size_t size) 1318c8570fbSDustin Kirkland { 1328c8570fbSDustin Kirkland char *context; 1338c8570fbSDustin Kirkland unsigned len; 1348c8570fbSDustin Kirkland int rc; 1358c8570fbSDustin Kirkland 1368c8570fbSDustin Kirkland rc = security_sid_to_context(sid, &context, &len); 1378c8570fbSDustin Kirkland if (rc) 1388c8570fbSDustin Kirkland return rc; 1398c8570fbSDustin Kirkland 1408c8570fbSDustin Kirkland if (!buffer || !size) 1418c8570fbSDustin Kirkland goto getsecurity_exit; 1428c8570fbSDustin Kirkland 1438c8570fbSDustin Kirkland if (size < len) { 1448c8570fbSDustin Kirkland len = -ERANGE; 1458c8570fbSDustin Kirkland goto getsecurity_exit; 1468c8570fbSDustin Kirkland } 1478c8570fbSDustin Kirkland memcpy(buffer, context, len); 1488c8570fbSDustin Kirkland 1498c8570fbSDustin Kirkland getsecurity_exit: 1508c8570fbSDustin Kirkland kfree(context); 1518c8570fbSDustin Kirkland return len; 1528c8570fbSDustin Kirkland } 1538c8570fbSDustin Kirkland 1541da177e4SLinus Torvalds /* Allocate and free functions for each kind of security blob. */ 1551da177e4SLinus Torvalds 1561da177e4SLinus Torvalds static int task_alloc_security(struct task_struct *task) 1571da177e4SLinus Torvalds { 1581da177e4SLinus Torvalds struct task_security_struct *tsec; 1591da177e4SLinus Torvalds 16089d155efSJames Morris tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL); 1611da177e4SLinus Torvalds if (!tsec) 1621da177e4SLinus Torvalds return -ENOMEM; 1631da177e4SLinus Torvalds 1641da177e4SLinus Torvalds tsec->task = task; 1651da177e4SLinus Torvalds tsec->osid = tsec->sid = tsec->ptrace_sid = SECINITSID_UNLABELED; 1661da177e4SLinus Torvalds task->security = tsec; 1671da177e4SLinus Torvalds 1681da177e4SLinus Torvalds return 0; 1691da177e4SLinus Torvalds } 1701da177e4SLinus Torvalds 1711da177e4SLinus Torvalds static void task_free_security(struct task_struct *task) 1721da177e4SLinus Torvalds { 1731da177e4SLinus Torvalds struct task_security_struct *tsec = task->security; 1741da177e4SLinus Torvalds task->security = NULL; 1751da177e4SLinus Torvalds kfree(tsec); 1761da177e4SLinus Torvalds } 1771da177e4SLinus Torvalds 1781da177e4SLinus Torvalds static int inode_alloc_security(struct inode *inode) 1791da177e4SLinus Torvalds { 1801da177e4SLinus Torvalds struct task_security_struct *tsec = current->security; 1811da177e4SLinus Torvalds struct inode_security_struct *isec; 1821da177e4SLinus Torvalds 183c3762229SRobert P. J. Day isec = kmem_cache_zalloc(sel_inode_cache, GFP_KERNEL); 1841da177e4SLinus Torvalds if (!isec) 1851da177e4SLinus Torvalds return -ENOMEM; 1861da177e4SLinus Torvalds 18723970741SEric Paris mutex_init(&isec->lock); 1881da177e4SLinus Torvalds INIT_LIST_HEAD(&isec->list); 1891da177e4SLinus Torvalds isec->inode = inode; 1901da177e4SLinus Torvalds isec->sid = SECINITSID_UNLABELED; 1911da177e4SLinus Torvalds isec->sclass = SECCLASS_FILE; 1921da177e4SLinus Torvalds isec->task_sid = tsec->sid; 1931da177e4SLinus Torvalds inode->i_security = isec; 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds return 0; 1961da177e4SLinus Torvalds } 1971da177e4SLinus Torvalds 1981da177e4SLinus Torvalds static void inode_free_security(struct inode *inode) 1991da177e4SLinus Torvalds { 2001da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 2011da177e4SLinus Torvalds struct superblock_security_struct *sbsec = inode->i_sb->s_security; 2021da177e4SLinus Torvalds 2031da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 2041da177e4SLinus Torvalds if (!list_empty(&isec->list)) 2051da177e4SLinus Torvalds list_del_init(&isec->list); 2061da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 2071da177e4SLinus Torvalds 2081da177e4SLinus Torvalds inode->i_security = NULL; 2097cae7e26SJames Morris kmem_cache_free(sel_inode_cache, isec); 2101da177e4SLinus Torvalds } 2111da177e4SLinus Torvalds 2121da177e4SLinus Torvalds static int file_alloc_security(struct file *file) 2131da177e4SLinus Torvalds { 2141da177e4SLinus Torvalds struct task_security_struct *tsec = current->security; 2151da177e4SLinus Torvalds struct file_security_struct *fsec; 2161da177e4SLinus Torvalds 21726d2a4beSStephen Smalley fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL); 2181da177e4SLinus Torvalds if (!fsec) 2191da177e4SLinus Torvalds return -ENOMEM; 2201da177e4SLinus Torvalds 2211da177e4SLinus Torvalds fsec->file = file; 2221da177e4SLinus Torvalds fsec->sid = tsec->sid; 2231da177e4SLinus Torvalds fsec->fown_sid = tsec->sid; 2241da177e4SLinus Torvalds file->f_security = fsec; 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds return 0; 2271da177e4SLinus Torvalds } 2281da177e4SLinus Torvalds 2291da177e4SLinus Torvalds static void file_free_security(struct file *file) 2301da177e4SLinus Torvalds { 2311da177e4SLinus Torvalds struct file_security_struct *fsec = file->f_security; 2321da177e4SLinus Torvalds file->f_security = NULL; 2331da177e4SLinus Torvalds kfree(fsec); 2341da177e4SLinus Torvalds } 2351da177e4SLinus Torvalds 2361da177e4SLinus Torvalds static int superblock_alloc_security(struct super_block *sb) 2371da177e4SLinus Torvalds { 2381da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 2391da177e4SLinus Torvalds 24089d155efSJames Morris sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL); 2411da177e4SLinus Torvalds if (!sbsec) 2421da177e4SLinus Torvalds return -ENOMEM; 2431da177e4SLinus Torvalds 244bc7e982bSEric Paris mutex_init(&sbsec->lock); 2451da177e4SLinus Torvalds INIT_LIST_HEAD(&sbsec->list); 2461da177e4SLinus Torvalds INIT_LIST_HEAD(&sbsec->isec_head); 2471da177e4SLinus Torvalds spin_lock_init(&sbsec->isec_lock); 2481da177e4SLinus Torvalds sbsec->sb = sb; 2491da177e4SLinus Torvalds sbsec->sid = SECINITSID_UNLABELED; 2501da177e4SLinus Torvalds sbsec->def_sid = SECINITSID_FILE; 251c312feb2SEric Paris sbsec->mntpoint_sid = SECINITSID_UNLABELED; 2521da177e4SLinus Torvalds sb->s_security = sbsec; 2531da177e4SLinus Torvalds 2541da177e4SLinus Torvalds return 0; 2551da177e4SLinus Torvalds } 2561da177e4SLinus Torvalds 2571da177e4SLinus Torvalds static void superblock_free_security(struct super_block *sb) 2581da177e4SLinus Torvalds { 2591da177e4SLinus Torvalds struct superblock_security_struct *sbsec = sb->s_security; 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds spin_lock(&sb_security_lock); 2621da177e4SLinus Torvalds if (!list_empty(&sbsec->list)) 2631da177e4SLinus Torvalds list_del_init(&sbsec->list); 2641da177e4SLinus Torvalds spin_unlock(&sb_security_lock); 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds sb->s_security = NULL; 2671da177e4SLinus Torvalds kfree(sbsec); 2681da177e4SLinus Torvalds } 2691da177e4SLinus Torvalds 2707d877f3bSAl Viro static int sk_alloc_security(struct sock *sk, int family, gfp_t priority) 2711da177e4SLinus Torvalds { 2721da177e4SLinus Torvalds struct sk_security_struct *ssec; 2731da177e4SLinus Torvalds 27489d155efSJames Morris ssec = kzalloc(sizeof(*ssec), priority); 2751da177e4SLinus Torvalds if (!ssec) 2761da177e4SLinus Torvalds return -ENOMEM; 2771da177e4SLinus Torvalds 2781da177e4SLinus Torvalds ssec->sk = sk; 2791da177e4SLinus Torvalds ssec->peer_sid = SECINITSID_UNLABELED; 280892c141eSVenkat Yekkirala ssec->sid = SECINITSID_UNLABELED; 2811da177e4SLinus Torvalds sk->sk_security = ssec; 2821da177e4SLinus Torvalds 28399f59ed0SPaul Moore selinux_netlbl_sk_security_init(ssec, family); 28499f59ed0SPaul Moore 2851da177e4SLinus Torvalds return 0; 2861da177e4SLinus Torvalds } 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds static void sk_free_security(struct sock *sk) 2891da177e4SLinus Torvalds { 2901da177e4SLinus Torvalds struct sk_security_struct *ssec = sk->sk_security; 2911da177e4SLinus Torvalds 2921da177e4SLinus Torvalds sk->sk_security = NULL; 2931da177e4SLinus Torvalds kfree(ssec); 2941da177e4SLinus Torvalds } 2951da177e4SLinus Torvalds 2961da177e4SLinus Torvalds /* The security server must be initialized before 2971da177e4SLinus Torvalds any labeling or access decisions can be provided. */ 2981da177e4SLinus Torvalds extern int ss_initialized; 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds /* The file system's label must be initialized prior to use. */ 3011da177e4SLinus Torvalds 3021da177e4SLinus Torvalds static char *labeling_behaviors[6] = { 3031da177e4SLinus Torvalds "uses xattr", 3041da177e4SLinus Torvalds "uses transition SIDs", 3051da177e4SLinus Torvalds "uses task SIDs", 3061da177e4SLinus Torvalds "uses genfs_contexts", 3071da177e4SLinus Torvalds "not configured for labeling", 3081da177e4SLinus Torvalds "uses mountpoint labeling", 3091da177e4SLinus Torvalds }; 3101da177e4SLinus Torvalds 3111da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry); 3121da177e4SLinus Torvalds 3131da177e4SLinus Torvalds static inline int inode_doinit(struct inode *inode) 3141da177e4SLinus Torvalds { 3151da177e4SLinus Torvalds return inode_doinit_with_dentry(inode, NULL); 3161da177e4SLinus Torvalds } 3171da177e4SLinus Torvalds 3181da177e4SLinus Torvalds enum { 319*31e87930SEric Paris Opt_error = -1, 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"}, 331*31e87930SEric Paris {Opt_error, NULL}, 3321da177e4SLinus Torvalds }; 3331da177e4SLinus Torvalds 3341da177e4SLinus Torvalds #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n" 3351da177e4SLinus Torvalds 336c312feb2SEric Paris static int may_context_mount_sb_relabel(u32 sid, 337c312feb2SEric Paris struct superblock_security_struct *sbsec, 338c312feb2SEric Paris struct task_security_struct *tsec) 339c312feb2SEric Paris { 340c312feb2SEric Paris int rc; 341c312feb2SEric Paris 342c312feb2SEric Paris rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 343c312feb2SEric Paris FILESYSTEM__RELABELFROM, NULL); 344c312feb2SEric Paris if (rc) 345c312feb2SEric Paris return rc; 346c312feb2SEric Paris 347c312feb2SEric Paris rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM, 348c312feb2SEric Paris FILESYSTEM__RELABELTO, NULL); 349c312feb2SEric Paris return rc; 350c312feb2SEric Paris } 351c312feb2SEric Paris 3520808925eSEric Paris static int may_context_mount_inode_relabel(u32 sid, 3530808925eSEric Paris struct superblock_security_struct *sbsec, 3540808925eSEric Paris struct task_security_struct *tsec) 3550808925eSEric Paris { 3560808925eSEric Paris int rc; 3570808925eSEric Paris rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 3580808925eSEric Paris FILESYSTEM__RELABELFROM, NULL); 3590808925eSEric Paris if (rc) 3600808925eSEric Paris return rc; 3610808925eSEric Paris 3620808925eSEric Paris rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, 3630808925eSEric Paris FILESYSTEM__ASSOCIATE, NULL); 3640808925eSEric Paris return rc; 3650808925eSEric Paris } 3660808925eSEric Paris 3671da177e4SLinus Torvalds static int try_context_mount(struct super_block *sb, void *data) 3681da177e4SLinus Torvalds { 3691da177e4SLinus Torvalds char *context = NULL, *defcontext = NULL; 3700808925eSEric Paris char *fscontext = NULL, *rootcontext = NULL; 3711da177e4SLinus Torvalds const char *name; 3721da177e4SLinus Torvalds u32 sid; 3731da177e4SLinus Torvalds int alloc = 0, rc = 0, seen = 0; 3741da177e4SLinus Torvalds struct task_security_struct *tsec = current->security; 3751da177e4SLinus Torvalds struct superblock_security_struct *sbsec = sb->s_security; 3761da177e4SLinus Torvalds 3771da177e4SLinus Torvalds if (!data) 3781da177e4SLinus Torvalds goto out; 3791da177e4SLinus Torvalds 3801da177e4SLinus Torvalds name = sb->s_type->name; 3811da177e4SLinus Torvalds 3821da177e4SLinus Torvalds if (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA) { 3831da177e4SLinus Torvalds 3841da177e4SLinus Torvalds /* NFS we understand. */ 3851da177e4SLinus Torvalds if (!strcmp(name, "nfs")) { 3861da177e4SLinus Torvalds struct nfs_mount_data *d = data; 3871da177e4SLinus Torvalds 3881da177e4SLinus Torvalds if (d->version < NFS_MOUNT_VERSION) 3891da177e4SLinus Torvalds goto out; 3901da177e4SLinus Torvalds 3911da177e4SLinus Torvalds if (d->context[0]) { 3921da177e4SLinus Torvalds context = d->context; 3931da177e4SLinus Torvalds seen |= Opt_context; 3941da177e4SLinus Torvalds } 3951da177e4SLinus Torvalds } else 3961da177e4SLinus Torvalds goto out; 3971da177e4SLinus Torvalds 3981da177e4SLinus Torvalds } else { 3991da177e4SLinus Torvalds /* Standard string-based options. */ 4001da177e4SLinus Torvalds char *p, *options = data; 4011da177e4SLinus Torvalds 4023528a953SCory Olmo while ((p = strsep(&options, "|")) != NULL) { 4031da177e4SLinus Torvalds int token; 4041da177e4SLinus Torvalds substring_t args[MAX_OPT_ARGS]; 4051da177e4SLinus Torvalds 4061da177e4SLinus Torvalds if (!*p) 4071da177e4SLinus Torvalds continue; 4081da177e4SLinus Torvalds 4091da177e4SLinus Torvalds token = match_token(p, tokens, args); 4101da177e4SLinus Torvalds 4111da177e4SLinus Torvalds switch (token) { 4121da177e4SLinus Torvalds case Opt_context: 413c312feb2SEric Paris if (seen & (Opt_context|Opt_defcontext)) { 4141da177e4SLinus Torvalds rc = -EINVAL; 4151da177e4SLinus Torvalds printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 4161da177e4SLinus Torvalds goto out_free; 4171da177e4SLinus Torvalds } 4181da177e4SLinus Torvalds context = match_strdup(&args[0]); 4191da177e4SLinus Torvalds if (!context) { 4201da177e4SLinus Torvalds rc = -ENOMEM; 4211da177e4SLinus Torvalds goto out_free; 4221da177e4SLinus Torvalds } 4231da177e4SLinus Torvalds if (!alloc) 4241da177e4SLinus Torvalds alloc = 1; 4251da177e4SLinus Torvalds seen |= Opt_context; 4261da177e4SLinus Torvalds break; 4271da177e4SLinus Torvalds 4281da177e4SLinus Torvalds case Opt_fscontext: 429c312feb2SEric Paris if (seen & Opt_fscontext) { 4301da177e4SLinus Torvalds rc = -EINVAL; 4311da177e4SLinus Torvalds printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 4321da177e4SLinus Torvalds goto out_free; 4331da177e4SLinus Torvalds } 434c312feb2SEric Paris fscontext = match_strdup(&args[0]); 435c312feb2SEric Paris if (!fscontext) { 4361da177e4SLinus Torvalds rc = -ENOMEM; 4371da177e4SLinus Torvalds goto out_free; 4381da177e4SLinus Torvalds } 4391da177e4SLinus Torvalds if (!alloc) 4401da177e4SLinus Torvalds alloc = 1; 4411da177e4SLinus Torvalds seen |= Opt_fscontext; 4421da177e4SLinus Torvalds break; 4431da177e4SLinus Torvalds 4440808925eSEric Paris case Opt_rootcontext: 4450808925eSEric Paris if (seen & Opt_rootcontext) { 4460808925eSEric Paris rc = -EINVAL; 4470808925eSEric Paris printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 4480808925eSEric Paris goto out_free; 4490808925eSEric Paris } 4500808925eSEric Paris rootcontext = match_strdup(&args[0]); 4510808925eSEric Paris if (!rootcontext) { 4520808925eSEric Paris rc = -ENOMEM; 4530808925eSEric Paris goto out_free; 4540808925eSEric Paris } 4550808925eSEric Paris if (!alloc) 4560808925eSEric Paris alloc = 1; 4570808925eSEric Paris seen |= Opt_rootcontext; 4580808925eSEric Paris break; 4590808925eSEric Paris 4601da177e4SLinus Torvalds case Opt_defcontext: 4611da177e4SLinus Torvalds if (sbsec->behavior != SECURITY_FS_USE_XATTR) { 4621da177e4SLinus Torvalds rc = -EINVAL; 4631da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: " 4641da177e4SLinus Torvalds "defcontext option is invalid " 4651da177e4SLinus Torvalds "for this filesystem type\n"); 4661da177e4SLinus Torvalds goto out_free; 4671da177e4SLinus Torvalds } 4681da177e4SLinus Torvalds if (seen & (Opt_context|Opt_defcontext)) { 4691da177e4SLinus Torvalds rc = -EINVAL; 4701da177e4SLinus Torvalds printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 4711da177e4SLinus Torvalds goto out_free; 4721da177e4SLinus Torvalds } 4731da177e4SLinus Torvalds defcontext = match_strdup(&args[0]); 4741da177e4SLinus Torvalds if (!defcontext) { 4751da177e4SLinus Torvalds rc = -ENOMEM; 4761da177e4SLinus Torvalds goto out_free; 4771da177e4SLinus Torvalds } 4781da177e4SLinus Torvalds if (!alloc) 4791da177e4SLinus Torvalds alloc = 1; 4801da177e4SLinus Torvalds seen |= Opt_defcontext; 4811da177e4SLinus Torvalds break; 4821da177e4SLinus Torvalds 4831da177e4SLinus Torvalds default: 4841da177e4SLinus Torvalds rc = -EINVAL; 4851da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: unknown mount " 4861da177e4SLinus Torvalds "option\n"); 4871da177e4SLinus Torvalds goto out_free; 4881da177e4SLinus Torvalds 4891da177e4SLinus Torvalds } 4901da177e4SLinus Torvalds } 4911da177e4SLinus Torvalds } 4921da177e4SLinus Torvalds 4931da177e4SLinus Torvalds if (!seen) 4941da177e4SLinus Torvalds goto out; 4951da177e4SLinus Torvalds 496c312feb2SEric Paris /* sets the context of the superblock for the fs being mounted. */ 497c312feb2SEric Paris if (fscontext) { 498c312feb2SEric Paris rc = security_context_to_sid(fscontext, strlen(fscontext), &sid); 499c312feb2SEric Paris if (rc) { 500c312feb2SEric Paris printk(KERN_WARNING "SELinux: security_context_to_sid" 501c312feb2SEric Paris "(%s) failed for (dev %s, type %s) errno=%d\n", 502c312feb2SEric Paris fscontext, sb->s_id, name, rc); 503c312feb2SEric Paris goto out_free; 504c312feb2SEric Paris } 505c312feb2SEric Paris 506c312feb2SEric Paris rc = may_context_mount_sb_relabel(sid, sbsec, tsec); 507c312feb2SEric Paris if (rc) 508c312feb2SEric Paris goto out_free; 509c312feb2SEric Paris 510c312feb2SEric Paris sbsec->sid = sid; 511c312feb2SEric Paris } 512c312feb2SEric Paris 513c312feb2SEric Paris /* 514c312feb2SEric Paris * Switch to using mount point labeling behavior. 515c312feb2SEric Paris * sets the label used on all file below the mountpoint, and will set 516c312feb2SEric Paris * the superblock context if not already set. 517c312feb2SEric Paris */ 5181da177e4SLinus Torvalds if (context) { 5191da177e4SLinus Torvalds rc = security_context_to_sid(context, strlen(context), &sid); 5201da177e4SLinus Torvalds if (rc) { 5211da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: security_context_to_sid" 5221da177e4SLinus Torvalds "(%s) failed for (dev %s, type %s) errno=%d\n", 5231da177e4SLinus Torvalds context, sb->s_id, name, rc); 5241da177e4SLinus Torvalds goto out_free; 5251da177e4SLinus Torvalds } 5261da177e4SLinus Torvalds 527b04ea3ceSEric Paris if (!fscontext) { 528c312feb2SEric Paris rc = may_context_mount_sb_relabel(sid, sbsec, tsec); 5291da177e4SLinus Torvalds if (rc) 5301da177e4SLinus Torvalds goto out_free; 5311da177e4SLinus Torvalds sbsec->sid = sid; 532b04ea3ceSEric Paris } else { 533b04ea3ceSEric Paris rc = may_context_mount_inode_relabel(sid, sbsec, tsec); 534b04ea3ceSEric Paris if (rc) 535b04ea3ceSEric Paris goto out_free; 536b04ea3ceSEric Paris } 537c312feb2SEric Paris sbsec->mntpoint_sid = sid; 5381da177e4SLinus Torvalds 5391da177e4SLinus Torvalds sbsec->behavior = SECURITY_FS_USE_MNTPOINT; 5401da177e4SLinus Torvalds } 5411da177e4SLinus Torvalds 5420808925eSEric Paris if (rootcontext) { 5430808925eSEric Paris struct inode *inode = sb->s_root->d_inode; 5440808925eSEric Paris struct inode_security_struct *isec = inode->i_security; 5450808925eSEric Paris rc = security_context_to_sid(rootcontext, strlen(rootcontext), &sid); 5460808925eSEric Paris if (rc) { 5470808925eSEric Paris printk(KERN_WARNING "SELinux: security_context_to_sid" 5480808925eSEric Paris "(%s) failed for (dev %s, type %s) errno=%d\n", 5490808925eSEric Paris rootcontext, sb->s_id, name, rc); 5500808925eSEric Paris goto out_free; 5510808925eSEric Paris } 5520808925eSEric Paris 5530808925eSEric Paris rc = may_context_mount_inode_relabel(sid, sbsec, tsec); 5540808925eSEric Paris if (rc) 5550808925eSEric Paris goto out_free; 5560808925eSEric Paris 5570808925eSEric Paris isec->sid = sid; 5580808925eSEric Paris isec->initialized = 1; 5590808925eSEric Paris } 5600808925eSEric Paris 5611da177e4SLinus Torvalds if (defcontext) { 5621da177e4SLinus Torvalds rc = security_context_to_sid(defcontext, strlen(defcontext), &sid); 5631da177e4SLinus Torvalds if (rc) { 5641da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: security_context_to_sid" 5651da177e4SLinus Torvalds "(%s) failed for (dev %s, type %s) errno=%d\n", 5661da177e4SLinus Torvalds defcontext, sb->s_id, name, rc); 5671da177e4SLinus Torvalds goto out_free; 5681da177e4SLinus Torvalds } 5691da177e4SLinus Torvalds 5701da177e4SLinus Torvalds if (sid == sbsec->def_sid) 5711da177e4SLinus Torvalds goto out_free; 5721da177e4SLinus Torvalds 5730808925eSEric Paris rc = may_context_mount_inode_relabel(sid, sbsec, tsec); 5741da177e4SLinus Torvalds if (rc) 5751da177e4SLinus Torvalds goto out_free; 5761da177e4SLinus Torvalds 5771da177e4SLinus Torvalds sbsec->def_sid = sid; 5781da177e4SLinus Torvalds } 5791da177e4SLinus Torvalds 5801da177e4SLinus Torvalds out_free: 5811da177e4SLinus Torvalds if (alloc) { 5821da177e4SLinus Torvalds kfree(context); 5831da177e4SLinus Torvalds kfree(defcontext); 584c312feb2SEric Paris kfree(fscontext); 5850808925eSEric Paris kfree(rootcontext); 5861da177e4SLinus Torvalds } 5871da177e4SLinus Torvalds out: 5881da177e4SLinus Torvalds return rc; 5891da177e4SLinus Torvalds } 5901da177e4SLinus Torvalds 5911da177e4SLinus Torvalds static int superblock_doinit(struct super_block *sb, void *data) 5921da177e4SLinus Torvalds { 5931da177e4SLinus Torvalds struct superblock_security_struct *sbsec = sb->s_security; 5941da177e4SLinus Torvalds struct dentry *root = sb->s_root; 5951da177e4SLinus Torvalds struct inode *inode = root->d_inode; 5961da177e4SLinus Torvalds int rc = 0; 5971da177e4SLinus Torvalds 598bc7e982bSEric Paris mutex_lock(&sbsec->lock); 5991da177e4SLinus Torvalds if (sbsec->initialized) 6001da177e4SLinus Torvalds goto out; 6011da177e4SLinus Torvalds 6021da177e4SLinus Torvalds if (!ss_initialized) { 6031da177e4SLinus Torvalds /* Defer initialization until selinux_complete_init, 6041da177e4SLinus Torvalds after the initial policy is loaded and the security 6051da177e4SLinus Torvalds server is ready to handle calls. */ 6061da177e4SLinus Torvalds spin_lock(&sb_security_lock); 6071da177e4SLinus Torvalds if (list_empty(&sbsec->list)) 6081da177e4SLinus Torvalds list_add(&sbsec->list, &superblock_security_head); 6091da177e4SLinus Torvalds spin_unlock(&sb_security_lock); 6101da177e4SLinus Torvalds goto out; 6111da177e4SLinus Torvalds } 6121da177e4SLinus Torvalds 6131da177e4SLinus Torvalds /* Determine the labeling behavior to use for this filesystem type. */ 6141da177e4SLinus Torvalds rc = security_fs_use(sb->s_type->name, &sbsec->behavior, &sbsec->sid); 6151da177e4SLinus Torvalds if (rc) { 6161da177e4SLinus Torvalds printk(KERN_WARNING "%s: security_fs_use(%s) returned %d\n", 6171da177e4SLinus Torvalds __FUNCTION__, sb->s_type->name, rc); 6181da177e4SLinus Torvalds goto out; 6191da177e4SLinus Torvalds } 6201da177e4SLinus Torvalds 6211da177e4SLinus Torvalds rc = try_context_mount(sb, data); 6221da177e4SLinus Torvalds if (rc) 6231da177e4SLinus Torvalds goto out; 6241da177e4SLinus Torvalds 6251da177e4SLinus Torvalds if (sbsec->behavior == SECURITY_FS_USE_XATTR) { 6261da177e4SLinus Torvalds /* Make sure that the xattr handler exists and that no 6271da177e4SLinus Torvalds error other than -ENODATA is returned by getxattr on 6281da177e4SLinus Torvalds the root directory. -ENODATA is ok, as this may be 6291da177e4SLinus Torvalds the first boot of the SELinux kernel before we have 6301da177e4SLinus Torvalds assigned xattr values to the filesystem. */ 6311da177e4SLinus Torvalds if (!inode->i_op->getxattr) { 6321da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: (dev %s, type %s) has no " 6331da177e4SLinus Torvalds "xattr support\n", sb->s_id, sb->s_type->name); 6341da177e4SLinus Torvalds rc = -EOPNOTSUPP; 6351da177e4SLinus Torvalds goto out; 6361da177e4SLinus Torvalds } 6371da177e4SLinus Torvalds rc = inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0); 6381da177e4SLinus Torvalds if (rc < 0 && rc != -ENODATA) { 6391da177e4SLinus Torvalds if (rc == -EOPNOTSUPP) 6401da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: (dev %s, type " 6411da177e4SLinus Torvalds "%s) has no security xattr handler\n", 6421da177e4SLinus Torvalds sb->s_id, sb->s_type->name); 6431da177e4SLinus Torvalds else 6441da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: (dev %s, type " 6451da177e4SLinus Torvalds "%s) getxattr errno %d\n", sb->s_id, 6461da177e4SLinus Torvalds sb->s_type->name, -rc); 6471da177e4SLinus Torvalds goto out; 6481da177e4SLinus Torvalds } 6491da177e4SLinus Torvalds } 6501da177e4SLinus Torvalds 6511da177e4SLinus Torvalds if (strcmp(sb->s_type->name, "proc") == 0) 6521da177e4SLinus Torvalds sbsec->proc = 1; 6531da177e4SLinus Torvalds 6541da177e4SLinus Torvalds sbsec->initialized = 1; 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds if (sbsec->behavior > ARRAY_SIZE(labeling_behaviors)) { 657fadcdb45SEric Paris printk(KERN_ERR "SELinux: initialized (dev %s, type %s), unknown behavior\n", 6581da177e4SLinus Torvalds sb->s_id, sb->s_type->name); 6591da177e4SLinus Torvalds } 6601da177e4SLinus Torvalds else { 661fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: initialized (dev %s, type %s), %s\n", 6621da177e4SLinus Torvalds sb->s_id, sb->s_type->name, 6631da177e4SLinus Torvalds labeling_behaviors[sbsec->behavior-1]); 6641da177e4SLinus Torvalds } 6651da177e4SLinus Torvalds 6661da177e4SLinus Torvalds /* Initialize the root inode. */ 6671da177e4SLinus Torvalds rc = inode_doinit_with_dentry(sb->s_root->d_inode, sb->s_root); 6681da177e4SLinus Torvalds 6691da177e4SLinus Torvalds /* Initialize any other inodes associated with the superblock, e.g. 6701da177e4SLinus Torvalds inodes created prior to initial policy load or inodes created 6711da177e4SLinus Torvalds during get_sb by a pseudo filesystem that directly 6721da177e4SLinus Torvalds populates itself. */ 6731da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 6741da177e4SLinus Torvalds next_inode: 6751da177e4SLinus Torvalds if (!list_empty(&sbsec->isec_head)) { 6761da177e4SLinus Torvalds struct inode_security_struct *isec = 6771da177e4SLinus Torvalds list_entry(sbsec->isec_head.next, 6781da177e4SLinus Torvalds struct inode_security_struct, list); 6791da177e4SLinus Torvalds struct inode *inode = isec->inode; 6801da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 6811da177e4SLinus Torvalds inode = igrab(inode); 6821da177e4SLinus Torvalds if (inode) { 6831da177e4SLinus Torvalds if (!IS_PRIVATE (inode)) 6841da177e4SLinus Torvalds inode_doinit(inode); 6851da177e4SLinus Torvalds iput(inode); 6861da177e4SLinus Torvalds } 6871da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 6881da177e4SLinus Torvalds list_del_init(&isec->list); 6891da177e4SLinus Torvalds goto next_inode; 6901da177e4SLinus Torvalds } 6911da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 6921da177e4SLinus Torvalds out: 693bc7e982bSEric Paris mutex_unlock(&sbsec->lock); 6941da177e4SLinus Torvalds return rc; 6951da177e4SLinus Torvalds } 6961da177e4SLinus Torvalds 6971da177e4SLinus Torvalds static inline u16 inode_mode_to_security_class(umode_t mode) 6981da177e4SLinus Torvalds { 6991da177e4SLinus Torvalds switch (mode & S_IFMT) { 7001da177e4SLinus Torvalds case S_IFSOCK: 7011da177e4SLinus Torvalds return SECCLASS_SOCK_FILE; 7021da177e4SLinus Torvalds case S_IFLNK: 7031da177e4SLinus Torvalds return SECCLASS_LNK_FILE; 7041da177e4SLinus Torvalds case S_IFREG: 7051da177e4SLinus Torvalds return SECCLASS_FILE; 7061da177e4SLinus Torvalds case S_IFBLK: 7071da177e4SLinus Torvalds return SECCLASS_BLK_FILE; 7081da177e4SLinus Torvalds case S_IFDIR: 7091da177e4SLinus Torvalds return SECCLASS_DIR; 7101da177e4SLinus Torvalds case S_IFCHR: 7111da177e4SLinus Torvalds return SECCLASS_CHR_FILE; 7121da177e4SLinus Torvalds case S_IFIFO: 7131da177e4SLinus Torvalds return SECCLASS_FIFO_FILE; 7141da177e4SLinus Torvalds 7151da177e4SLinus Torvalds } 7161da177e4SLinus Torvalds 7171da177e4SLinus Torvalds return SECCLASS_FILE; 7181da177e4SLinus Torvalds } 7191da177e4SLinus Torvalds 72013402580SJames Morris static inline int default_protocol_stream(int protocol) 72113402580SJames Morris { 72213402580SJames Morris return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP); 72313402580SJames Morris } 72413402580SJames Morris 72513402580SJames Morris static inline int default_protocol_dgram(int protocol) 72613402580SJames Morris { 72713402580SJames Morris return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP); 72813402580SJames Morris } 72913402580SJames Morris 7301da177e4SLinus Torvalds static inline u16 socket_type_to_security_class(int family, int type, int protocol) 7311da177e4SLinus Torvalds { 7321da177e4SLinus Torvalds switch (family) { 7331da177e4SLinus Torvalds case PF_UNIX: 7341da177e4SLinus Torvalds switch (type) { 7351da177e4SLinus Torvalds case SOCK_STREAM: 7361da177e4SLinus Torvalds case SOCK_SEQPACKET: 7371da177e4SLinus Torvalds return SECCLASS_UNIX_STREAM_SOCKET; 7381da177e4SLinus Torvalds case SOCK_DGRAM: 7391da177e4SLinus Torvalds return SECCLASS_UNIX_DGRAM_SOCKET; 7401da177e4SLinus Torvalds } 7411da177e4SLinus Torvalds break; 7421da177e4SLinus Torvalds case PF_INET: 7431da177e4SLinus Torvalds case PF_INET6: 7441da177e4SLinus Torvalds switch (type) { 7451da177e4SLinus Torvalds case SOCK_STREAM: 74613402580SJames Morris if (default_protocol_stream(protocol)) 7471da177e4SLinus Torvalds return SECCLASS_TCP_SOCKET; 74813402580SJames Morris else 74913402580SJames Morris return SECCLASS_RAWIP_SOCKET; 7501da177e4SLinus Torvalds case SOCK_DGRAM: 75113402580SJames Morris if (default_protocol_dgram(protocol)) 7521da177e4SLinus Torvalds return SECCLASS_UDP_SOCKET; 75313402580SJames Morris else 75413402580SJames Morris return SECCLASS_RAWIP_SOCKET; 7552ee92d46SJames Morris case SOCK_DCCP: 7562ee92d46SJames Morris return SECCLASS_DCCP_SOCKET; 75713402580SJames Morris default: 7581da177e4SLinus Torvalds return SECCLASS_RAWIP_SOCKET; 7591da177e4SLinus Torvalds } 7601da177e4SLinus Torvalds break; 7611da177e4SLinus Torvalds case PF_NETLINK: 7621da177e4SLinus Torvalds switch (protocol) { 7631da177e4SLinus Torvalds case NETLINK_ROUTE: 7641da177e4SLinus Torvalds return SECCLASS_NETLINK_ROUTE_SOCKET; 7651da177e4SLinus Torvalds case NETLINK_FIREWALL: 7661da177e4SLinus Torvalds return SECCLASS_NETLINK_FIREWALL_SOCKET; 767216efaaaSJames Morris case NETLINK_INET_DIAG: 7681da177e4SLinus Torvalds return SECCLASS_NETLINK_TCPDIAG_SOCKET; 7691da177e4SLinus Torvalds case NETLINK_NFLOG: 7701da177e4SLinus Torvalds return SECCLASS_NETLINK_NFLOG_SOCKET; 7711da177e4SLinus Torvalds case NETLINK_XFRM: 7721da177e4SLinus Torvalds return SECCLASS_NETLINK_XFRM_SOCKET; 7731da177e4SLinus Torvalds case NETLINK_SELINUX: 7741da177e4SLinus Torvalds return SECCLASS_NETLINK_SELINUX_SOCKET; 7751da177e4SLinus Torvalds case NETLINK_AUDIT: 7761da177e4SLinus Torvalds return SECCLASS_NETLINK_AUDIT_SOCKET; 7771da177e4SLinus Torvalds case NETLINK_IP6_FW: 7781da177e4SLinus Torvalds return SECCLASS_NETLINK_IP6FW_SOCKET; 7791da177e4SLinus Torvalds case NETLINK_DNRTMSG: 7801da177e4SLinus Torvalds return SECCLASS_NETLINK_DNRT_SOCKET; 7810c9b7942SJames Morris case NETLINK_KOBJECT_UEVENT: 7820c9b7942SJames Morris return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET; 7831da177e4SLinus Torvalds default: 7841da177e4SLinus Torvalds return SECCLASS_NETLINK_SOCKET; 7851da177e4SLinus Torvalds } 7861da177e4SLinus Torvalds case PF_PACKET: 7871da177e4SLinus Torvalds return SECCLASS_PACKET_SOCKET; 7881da177e4SLinus Torvalds case PF_KEY: 7891da177e4SLinus Torvalds return SECCLASS_KEY_SOCKET; 7903e3ff15eSChristopher J. PeBenito case PF_APPLETALK: 7913e3ff15eSChristopher J. PeBenito return SECCLASS_APPLETALK_SOCKET; 7921da177e4SLinus Torvalds } 7931da177e4SLinus Torvalds 7941da177e4SLinus Torvalds return SECCLASS_SOCKET; 7951da177e4SLinus Torvalds } 7961da177e4SLinus Torvalds 7971da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 7981da177e4SLinus Torvalds static int selinux_proc_get_sid(struct proc_dir_entry *de, 7991da177e4SLinus Torvalds u16 tclass, 8001da177e4SLinus Torvalds u32 *sid) 8011da177e4SLinus Torvalds { 8021da177e4SLinus Torvalds int buflen, rc; 8031da177e4SLinus Torvalds char *buffer, *path, *end; 8041da177e4SLinus Torvalds 8051da177e4SLinus Torvalds buffer = (char*)__get_free_page(GFP_KERNEL); 8061da177e4SLinus Torvalds if (!buffer) 8071da177e4SLinus Torvalds return -ENOMEM; 8081da177e4SLinus Torvalds 8091da177e4SLinus Torvalds buflen = PAGE_SIZE; 8101da177e4SLinus Torvalds end = buffer+buflen; 8111da177e4SLinus Torvalds *--end = '\0'; 8121da177e4SLinus Torvalds buflen--; 8131da177e4SLinus Torvalds path = end-1; 8141da177e4SLinus Torvalds *path = '/'; 8151da177e4SLinus Torvalds while (de && de != de->parent) { 8161da177e4SLinus Torvalds buflen -= de->namelen + 1; 8171da177e4SLinus Torvalds if (buflen < 0) 8181da177e4SLinus Torvalds break; 8191da177e4SLinus Torvalds end -= de->namelen; 8201da177e4SLinus Torvalds memcpy(end, de->name, de->namelen); 8211da177e4SLinus Torvalds *--end = '/'; 8221da177e4SLinus Torvalds path = end; 8231da177e4SLinus Torvalds de = de->parent; 8241da177e4SLinus Torvalds } 8251da177e4SLinus Torvalds rc = security_genfs_sid("proc", path, tclass, sid); 8261da177e4SLinus Torvalds free_page((unsigned long)buffer); 8271da177e4SLinus Torvalds return rc; 8281da177e4SLinus Torvalds } 8291da177e4SLinus Torvalds #else 8301da177e4SLinus Torvalds static int selinux_proc_get_sid(struct proc_dir_entry *de, 8311da177e4SLinus Torvalds u16 tclass, 8321da177e4SLinus Torvalds u32 *sid) 8331da177e4SLinus Torvalds { 8341da177e4SLinus Torvalds return -EINVAL; 8351da177e4SLinus Torvalds } 8361da177e4SLinus Torvalds #endif 8371da177e4SLinus Torvalds 8381da177e4SLinus Torvalds /* The inode's security attributes must be initialized before first use. */ 8391da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry) 8401da177e4SLinus Torvalds { 8411da177e4SLinus Torvalds struct superblock_security_struct *sbsec = NULL; 8421da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 8431da177e4SLinus Torvalds u32 sid; 8441da177e4SLinus Torvalds struct dentry *dentry; 8451da177e4SLinus Torvalds #define INITCONTEXTLEN 255 8461da177e4SLinus Torvalds char *context = NULL; 8471da177e4SLinus Torvalds unsigned len = 0; 8481da177e4SLinus Torvalds int rc = 0; 8491da177e4SLinus Torvalds 8501da177e4SLinus Torvalds if (isec->initialized) 8511da177e4SLinus Torvalds goto out; 8521da177e4SLinus Torvalds 85323970741SEric Paris mutex_lock(&isec->lock); 8541da177e4SLinus Torvalds if (isec->initialized) 85523970741SEric Paris goto out_unlock; 8561da177e4SLinus Torvalds 8571da177e4SLinus Torvalds sbsec = inode->i_sb->s_security; 8581da177e4SLinus Torvalds if (!sbsec->initialized) { 8591da177e4SLinus Torvalds /* Defer initialization until selinux_complete_init, 8601da177e4SLinus Torvalds after the initial policy is loaded and the security 8611da177e4SLinus Torvalds server is ready to handle calls. */ 8621da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 8631da177e4SLinus Torvalds if (list_empty(&isec->list)) 8641da177e4SLinus Torvalds list_add(&isec->list, &sbsec->isec_head); 8651da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 86623970741SEric Paris goto out_unlock; 8671da177e4SLinus Torvalds } 8681da177e4SLinus Torvalds 8691da177e4SLinus Torvalds switch (sbsec->behavior) { 8701da177e4SLinus Torvalds case SECURITY_FS_USE_XATTR: 8711da177e4SLinus Torvalds if (!inode->i_op->getxattr) { 8721da177e4SLinus Torvalds isec->sid = sbsec->def_sid; 8731da177e4SLinus Torvalds break; 8741da177e4SLinus Torvalds } 8751da177e4SLinus Torvalds 8761da177e4SLinus Torvalds /* Need a dentry, since the xattr API requires one. 8771da177e4SLinus Torvalds Life would be simpler if we could just pass the inode. */ 8781da177e4SLinus Torvalds if (opt_dentry) { 8791da177e4SLinus Torvalds /* Called from d_instantiate or d_splice_alias. */ 8801da177e4SLinus Torvalds dentry = dget(opt_dentry); 8811da177e4SLinus Torvalds } else { 8821da177e4SLinus Torvalds /* Called from selinux_complete_init, try to find a dentry. */ 8831da177e4SLinus Torvalds dentry = d_find_alias(inode); 8841da177e4SLinus Torvalds } 8851da177e4SLinus Torvalds if (!dentry) { 8861da177e4SLinus Torvalds printk(KERN_WARNING "%s: no dentry for dev=%s " 8871da177e4SLinus Torvalds "ino=%ld\n", __FUNCTION__, inode->i_sb->s_id, 8881da177e4SLinus Torvalds inode->i_ino); 88923970741SEric Paris goto out_unlock; 8901da177e4SLinus Torvalds } 8911da177e4SLinus Torvalds 8921da177e4SLinus Torvalds len = INITCONTEXTLEN; 8931da177e4SLinus Torvalds context = kmalloc(len, GFP_KERNEL); 8941da177e4SLinus Torvalds if (!context) { 8951da177e4SLinus Torvalds rc = -ENOMEM; 8961da177e4SLinus Torvalds dput(dentry); 89723970741SEric Paris goto out_unlock; 8981da177e4SLinus Torvalds } 8991da177e4SLinus Torvalds rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX, 9001da177e4SLinus Torvalds context, len); 9011da177e4SLinus Torvalds if (rc == -ERANGE) { 9021da177e4SLinus Torvalds /* Need a larger buffer. Query for the right size. */ 9031da177e4SLinus Torvalds rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX, 9041da177e4SLinus Torvalds NULL, 0); 9051da177e4SLinus Torvalds if (rc < 0) { 9061da177e4SLinus Torvalds dput(dentry); 90723970741SEric Paris goto out_unlock; 9081da177e4SLinus Torvalds } 9091da177e4SLinus Torvalds kfree(context); 9101da177e4SLinus Torvalds len = rc; 9111da177e4SLinus Torvalds context = kmalloc(len, GFP_KERNEL); 9121da177e4SLinus Torvalds if (!context) { 9131da177e4SLinus Torvalds rc = -ENOMEM; 9141da177e4SLinus Torvalds dput(dentry); 91523970741SEric Paris goto out_unlock; 9161da177e4SLinus Torvalds } 9171da177e4SLinus Torvalds rc = inode->i_op->getxattr(dentry, 9181da177e4SLinus Torvalds XATTR_NAME_SELINUX, 9191da177e4SLinus Torvalds context, len); 9201da177e4SLinus Torvalds } 9211da177e4SLinus Torvalds dput(dentry); 9221da177e4SLinus Torvalds if (rc < 0) { 9231da177e4SLinus Torvalds if (rc != -ENODATA) { 9241da177e4SLinus Torvalds printk(KERN_WARNING "%s: getxattr returned " 9251da177e4SLinus Torvalds "%d for dev=%s ino=%ld\n", __FUNCTION__, 9261da177e4SLinus Torvalds -rc, inode->i_sb->s_id, inode->i_ino); 9271da177e4SLinus Torvalds kfree(context); 92823970741SEric Paris goto out_unlock; 9291da177e4SLinus Torvalds } 9301da177e4SLinus Torvalds /* Map ENODATA to the default file SID */ 9311da177e4SLinus Torvalds sid = sbsec->def_sid; 9321da177e4SLinus Torvalds rc = 0; 9331da177e4SLinus Torvalds } else { 934f5c1d5b2SJames Morris rc = security_context_to_sid_default(context, rc, &sid, 935f5c1d5b2SJames Morris sbsec->def_sid); 9361da177e4SLinus Torvalds if (rc) { 9371da177e4SLinus Torvalds printk(KERN_WARNING "%s: context_to_sid(%s) " 9381da177e4SLinus Torvalds "returned %d for dev=%s ino=%ld\n", 9391da177e4SLinus Torvalds __FUNCTION__, context, -rc, 9401da177e4SLinus Torvalds inode->i_sb->s_id, inode->i_ino); 9411da177e4SLinus Torvalds kfree(context); 9421da177e4SLinus Torvalds /* Leave with the unlabeled SID */ 9431da177e4SLinus Torvalds rc = 0; 9441da177e4SLinus Torvalds break; 9451da177e4SLinus Torvalds } 9461da177e4SLinus Torvalds } 9471da177e4SLinus Torvalds kfree(context); 9481da177e4SLinus Torvalds isec->sid = sid; 9491da177e4SLinus Torvalds break; 9501da177e4SLinus Torvalds case SECURITY_FS_USE_TASK: 9511da177e4SLinus Torvalds isec->sid = isec->task_sid; 9521da177e4SLinus Torvalds break; 9531da177e4SLinus Torvalds case SECURITY_FS_USE_TRANS: 9541da177e4SLinus Torvalds /* Default to the fs SID. */ 9551da177e4SLinus Torvalds isec->sid = sbsec->sid; 9561da177e4SLinus Torvalds 9571da177e4SLinus Torvalds /* Try to obtain a transition SID. */ 9581da177e4SLinus Torvalds isec->sclass = inode_mode_to_security_class(inode->i_mode); 9591da177e4SLinus Torvalds rc = security_transition_sid(isec->task_sid, 9601da177e4SLinus Torvalds sbsec->sid, 9611da177e4SLinus Torvalds isec->sclass, 9621da177e4SLinus Torvalds &sid); 9631da177e4SLinus Torvalds if (rc) 96423970741SEric Paris goto out_unlock; 9651da177e4SLinus Torvalds isec->sid = sid; 9661da177e4SLinus Torvalds break; 967c312feb2SEric Paris case SECURITY_FS_USE_MNTPOINT: 968c312feb2SEric Paris isec->sid = sbsec->mntpoint_sid; 969c312feb2SEric Paris break; 9701da177e4SLinus Torvalds default: 971c312feb2SEric Paris /* Default to the fs superblock SID. */ 9721da177e4SLinus Torvalds isec->sid = sbsec->sid; 9731da177e4SLinus Torvalds 9741da177e4SLinus Torvalds if (sbsec->proc) { 9751da177e4SLinus Torvalds struct proc_inode *proci = PROC_I(inode); 9761da177e4SLinus Torvalds if (proci->pde) { 9771da177e4SLinus Torvalds isec->sclass = inode_mode_to_security_class(inode->i_mode); 9781da177e4SLinus Torvalds rc = selinux_proc_get_sid(proci->pde, 9791da177e4SLinus Torvalds isec->sclass, 9801da177e4SLinus Torvalds &sid); 9811da177e4SLinus Torvalds if (rc) 98223970741SEric Paris goto out_unlock; 9831da177e4SLinus Torvalds isec->sid = sid; 9841da177e4SLinus Torvalds } 9851da177e4SLinus Torvalds } 9861da177e4SLinus Torvalds break; 9871da177e4SLinus Torvalds } 9881da177e4SLinus Torvalds 9891da177e4SLinus Torvalds isec->initialized = 1; 9901da177e4SLinus Torvalds 99123970741SEric Paris out_unlock: 99223970741SEric Paris mutex_unlock(&isec->lock); 9931da177e4SLinus Torvalds out: 9941da177e4SLinus Torvalds if (isec->sclass == SECCLASS_FILE) 9951da177e4SLinus Torvalds isec->sclass = inode_mode_to_security_class(inode->i_mode); 9961da177e4SLinus Torvalds return rc; 9971da177e4SLinus Torvalds } 9981da177e4SLinus Torvalds 9991da177e4SLinus Torvalds /* Convert a Linux signal to an access vector. */ 10001da177e4SLinus Torvalds static inline u32 signal_to_av(int sig) 10011da177e4SLinus Torvalds { 10021da177e4SLinus Torvalds u32 perm = 0; 10031da177e4SLinus Torvalds 10041da177e4SLinus Torvalds switch (sig) { 10051da177e4SLinus Torvalds case SIGCHLD: 10061da177e4SLinus Torvalds /* Commonly granted from child to parent. */ 10071da177e4SLinus Torvalds perm = PROCESS__SIGCHLD; 10081da177e4SLinus Torvalds break; 10091da177e4SLinus Torvalds case SIGKILL: 10101da177e4SLinus Torvalds /* Cannot be caught or ignored */ 10111da177e4SLinus Torvalds perm = PROCESS__SIGKILL; 10121da177e4SLinus Torvalds break; 10131da177e4SLinus Torvalds case SIGSTOP: 10141da177e4SLinus Torvalds /* Cannot be caught or ignored */ 10151da177e4SLinus Torvalds perm = PROCESS__SIGSTOP; 10161da177e4SLinus Torvalds break; 10171da177e4SLinus Torvalds default: 10181da177e4SLinus Torvalds /* All other signals. */ 10191da177e4SLinus Torvalds perm = PROCESS__SIGNAL; 10201da177e4SLinus Torvalds break; 10211da177e4SLinus Torvalds } 10221da177e4SLinus Torvalds 10231da177e4SLinus Torvalds return perm; 10241da177e4SLinus Torvalds } 10251da177e4SLinus Torvalds 10261da177e4SLinus Torvalds /* Check permission betweeen a pair of tasks, e.g. signal checks, 10271da177e4SLinus Torvalds fork check, ptrace check, etc. */ 10281da177e4SLinus Torvalds static int task_has_perm(struct task_struct *tsk1, 10291da177e4SLinus Torvalds struct task_struct *tsk2, 10301da177e4SLinus Torvalds u32 perms) 10311da177e4SLinus Torvalds { 10321da177e4SLinus Torvalds struct task_security_struct *tsec1, *tsec2; 10331da177e4SLinus Torvalds 10341da177e4SLinus Torvalds tsec1 = tsk1->security; 10351da177e4SLinus Torvalds tsec2 = tsk2->security; 10361da177e4SLinus Torvalds return avc_has_perm(tsec1->sid, tsec2->sid, 10371da177e4SLinus Torvalds SECCLASS_PROCESS, perms, NULL); 10381da177e4SLinus Torvalds } 10391da177e4SLinus Torvalds 10401da177e4SLinus Torvalds /* Check whether a task is allowed to use a capability. */ 10411da177e4SLinus Torvalds static int task_has_capability(struct task_struct *tsk, 10421da177e4SLinus Torvalds int cap) 10431da177e4SLinus Torvalds { 10441da177e4SLinus Torvalds struct task_security_struct *tsec; 10451da177e4SLinus Torvalds struct avc_audit_data ad; 10461da177e4SLinus Torvalds 10471da177e4SLinus Torvalds tsec = tsk->security; 10481da177e4SLinus Torvalds 10491da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,CAP); 10501da177e4SLinus Torvalds ad.tsk = tsk; 10511da177e4SLinus Torvalds ad.u.cap = cap; 10521da177e4SLinus Torvalds 10531da177e4SLinus Torvalds return avc_has_perm(tsec->sid, tsec->sid, 10541da177e4SLinus Torvalds SECCLASS_CAPABILITY, CAP_TO_MASK(cap), &ad); 10551da177e4SLinus Torvalds } 10561da177e4SLinus Torvalds 10571da177e4SLinus Torvalds /* Check whether a task is allowed to use a system operation. */ 10581da177e4SLinus Torvalds static int task_has_system(struct task_struct *tsk, 10591da177e4SLinus Torvalds u32 perms) 10601da177e4SLinus Torvalds { 10611da177e4SLinus Torvalds struct task_security_struct *tsec; 10621da177e4SLinus Torvalds 10631da177e4SLinus Torvalds tsec = tsk->security; 10641da177e4SLinus Torvalds 10651da177e4SLinus Torvalds return avc_has_perm(tsec->sid, SECINITSID_KERNEL, 10661da177e4SLinus Torvalds SECCLASS_SYSTEM, perms, NULL); 10671da177e4SLinus Torvalds } 10681da177e4SLinus Torvalds 10691da177e4SLinus Torvalds /* Check whether a task has a particular permission to an inode. 10701da177e4SLinus Torvalds The 'adp' parameter is optional and allows other audit 10711da177e4SLinus Torvalds data to be passed (e.g. the dentry). */ 10721da177e4SLinus Torvalds static int inode_has_perm(struct task_struct *tsk, 10731da177e4SLinus Torvalds struct inode *inode, 10741da177e4SLinus Torvalds u32 perms, 10751da177e4SLinus Torvalds struct avc_audit_data *adp) 10761da177e4SLinus Torvalds { 10771da177e4SLinus Torvalds struct task_security_struct *tsec; 10781da177e4SLinus Torvalds struct inode_security_struct *isec; 10791da177e4SLinus Torvalds struct avc_audit_data ad; 10801da177e4SLinus Torvalds 1081bbaca6c2SStephen Smalley if (unlikely (IS_PRIVATE (inode))) 1082bbaca6c2SStephen Smalley return 0; 1083bbaca6c2SStephen Smalley 10841da177e4SLinus Torvalds tsec = tsk->security; 10851da177e4SLinus Torvalds isec = inode->i_security; 10861da177e4SLinus Torvalds 10871da177e4SLinus Torvalds if (!adp) { 10881da177e4SLinus Torvalds adp = &ad; 10891da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 10901da177e4SLinus Torvalds ad.u.fs.inode = inode; 10911da177e4SLinus Torvalds } 10921da177e4SLinus Torvalds 10931da177e4SLinus Torvalds return avc_has_perm(tsec->sid, isec->sid, isec->sclass, perms, adp); 10941da177e4SLinus Torvalds } 10951da177e4SLinus Torvalds 10961da177e4SLinus Torvalds /* Same as inode_has_perm, but pass explicit audit data containing 10971da177e4SLinus Torvalds the dentry to help the auditing code to more easily generate the 10981da177e4SLinus Torvalds pathname if needed. */ 10991da177e4SLinus Torvalds static inline int dentry_has_perm(struct task_struct *tsk, 11001da177e4SLinus Torvalds struct vfsmount *mnt, 11011da177e4SLinus Torvalds struct dentry *dentry, 11021da177e4SLinus Torvalds u32 av) 11031da177e4SLinus Torvalds { 11041da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 11051da177e4SLinus Torvalds struct avc_audit_data ad; 11061da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,FS); 11071da177e4SLinus Torvalds ad.u.fs.mnt = mnt; 11081da177e4SLinus Torvalds ad.u.fs.dentry = dentry; 11091da177e4SLinus Torvalds return inode_has_perm(tsk, inode, av, &ad); 11101da177e4SLinus Torvalds } 11111da177e4SLinus Torvalds 11121da177e4SLinus Torvalds /* Check whether a task can use an open file descriptor to 11131da177e4SLinus Torvalds access an inode in a given way. Check access to the 11141da177e4SLinus Torvalds descriptor itself, and then use dentry_has_perm to 11151da177e4SLinus Torvalds check a particular permission to the file. 11161da177e4SLinus Torvalds Access to the descriptor is implicitly granted if it 11171da177e4SLinus Torvalds has the same SID as the process. If av is zero, then 11181da177e4SLinus Torvalds access to the file is not checked, e.g. for cases 11191da177e4SLinus Torvalds where only the descriptor is affected like seek. */ 1120858119e1SArjan van de Ven static int file_has_perm(struct task_struct *tsk, 11211da177e4SLinus Torvalds struct file *file, 11221da177e4SLinus Torvalds u32 av) 11231da177e4SLinus Torvalds { 11241da177e4SLinus Torvalds struct task_security_struct *tsec = tsk->security; 11251da177e4SLinus Torvalds struct file_security_struct *fsec = file->f_security; 11263d5ff529SJosef Sipek struct vfsmount *mnt = file->f_path.mnt; 11273d5ff529SJosef Sipek struct dentry *dentry = file->f_path.dentry; 11281da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 11291da177e4SLinus Torvalds struct avc_audit_data ad; 11301da177e4SLinus Torvalds int rc; 11311da177e4SLinus Torvalds 11321da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 11331da177e4SLinus Torvalds ad.u.fs.mnt = mnt; 11341da177e4SLinus Torvalds ad.u.fs.dentry = dentry; 11351da177e4SLinus Torvalds 11361da177e4SLinus Torvalds if (tsec->sid != fsec->sid) { 11371da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, fsec->sid, 11381da177e4SLinus Torvalds SECCLASS_FD, 11391da177e4SLinus Torvalds FD__USE, 11401da177e4SLinus Torvalds &ad); 11411da177e4SLinus Torvalds if (rc) 11421da177e4SLinus Torvalds return rc; 11431da177e4SLinus Torvalds } 11441da177e4SLinus Torvalds 11451da177e4SLinus Torvalds /* av is zero if only checking access to the descriptor. */ 11461da177e4SLinus Torvalds if (av) 11471da177e4SLinus Torvalds return inode_has_perm(tsk, inode, av, &ad); 11481da177e4SLinus Torvalds 11491da177e4SLinus Torvalds return 0; 11501da177e4SLinus Torvalds } 11511da177e4SLinus Torvalds 11521da177e4SLinus Torvalds /* Check whether a task can create a file. */ 11531da177e4SLinus Torvalds static int may_create(struct inode *dir, 11541da177e4SLinus Torvalds struct dentry *dentry, 11551da177e4SLinus Torvalds u16 tclass) 11561da177e4SLinus Torvalds { 11571da177e4SLinus Torvalds struct task_security_struct *tsec; 11581da177e4SLinus Torvalds struct inode_security_struct *dsec; 11591da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 11601da177e4SLinus Torvalds u32 newsid; 11611da177e4SLinus Torvalds struct avc_audit_data ad; 11621da177e4SLinus Torvalds int rc; 11631da177e4SLinus Torvalds 11641da177e4SLinus Torvalds tsec = current->security; 11651da177e4SLinus Torvalds dsec = dir->i_security; 11661da177e4SLinus Torvalds sbsec = dir->i_sb->s_security; 11671da177e4SLinus Torvalds 11681da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 11691da177e4SLinus Torvalds ad.u.fs.dentry = dentry; 11701da177e4SLinus Torvalds 11711da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, dsec->sid, SECCLASS_DIR, 11721da177e4SLinus Torvalds DIR__ADD_NAME | DIR__SEARCH, 11731da177e4SLinus Torvalds &ad); 11741da177e4SLinus Torvalds if (rc) 11751da177e4SLinus Torvalds return rc; 11761da177e4SLinus Torvalds 11771da177e4SLinus Torvalds if (tsec->create_sid && sbsec->behavior != SECURITY_FS_USE_MNTPOINT) { 11781da177e4SLinus Torvalds newsid = tsec->create_sid; 11791da177e4SLinus Torvalds } else { 11801da177e4SLinus Torvalds rc = security_transition_sid(tsec->sid, dsec->sid, tclass, 11811da177e4SLinus Torvalds &newsid); 11821da177e4SLinus Torvalds if (rc) 11831da177e4SLinus Torvalds return rc; 11841da177e4SLinus Torvalds } 11851da177e4SLinus Torvalds 11861da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, newsid, tclass, FILE__CREATE, &ad); 11871da177e4SLinus Torvalds if (rc) 11881da177e4SLinus Torvalds return rc; 11891da177e4SLinus Torvalds 11901da177e4SLinus Torvalds return avc_has_perm(newsid, sbsec->sid, 11911da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 11921da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, &ad); 11931da177e4SLinus Torvalds } 11941da177e4SLinus Torvalds 11954eb582cfSMichael LeMay /* Check whether a task can create a key. */ 11964eb582cfSMichael LeMay static int may_create_key(u32 ksid, 11974eb582cfSMichael LeMay struct task_struct *ctx) 11984eb582cfSMichael LeMay { 11994eb582cfSMichael LeMay struct task_security_struct *tsec; 12004eb582cfSMichael LeMay 12014eb582cfSMichael LeMay tsec = ctx->security; 12024eb582cfSMichael LeMay 12034eb582cfSMichael LeMay return avc_has_perm(tsec->sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL); 12044eb582cfSMichael LeMay } 12054eb582cfSMichael LeMay 12061da177e4SLinus Torvalds #define MAY_LINK 0 12071da177e4SLinus Torvalds #define MAY_UNLINK 1 12081da177e4SLinus Torvalds #define MAY_RMDIR 2 12091da177e4SLinus Torvalds 12101da177e4SLinus Torvalds /* Check whether a task can link, unlink, or rmdir a file/directory. */ 12111da177e4SLinus Torvalds static int may_link(struct inode *dir, 12121da177e4SLinus Torvalds struct dentry *dentry, 12131da177e4SLinus Torvalds int kind) 12141da177e4SLinus Torvalds 12151da177e4SLinus Torvalds { 12161da177e4SLinus Torvalds struct task_security_struct *tsec; 12171da177e4SLinus Torvalds struct inode_security_struct *dsec, *isec; 12181da177e4SLinus Torvalds struct avc_audit_data ad; 12191da177e4SLinus Torvalds u32 av; 12201da177e4SLinus Torvalds int rc; 12211da177e4SLinus Torvalds 12221da177e4SLinus Torvalds tsec = current->security; 12231da177e4SLinus Torvalds dsec = dir->i_security; 12241da177e4SLinus Torvalds isec = dentry->d_inode->i_security; 12251da177e4SLinus Torvalds 12261da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 12271da177e4SLinus Torvalds ad.u.fs.dentry = dentry; 12281da177e4SLinus Torvalds 12291da177e4SLinus Torvalds av = DIR__SEARCH; 12301da177e4SLinus Torvalds av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME); 12311da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, dsec->sid, SECCLASS_DIR, av, &ad); 12321da177e4SLinus Torvalds if (rc) 12331da177e4SLinus Torvalds return rc; 12341da177e4SLinus Torvalds 12351da177e4SLinus Torvalds switch (kind) { 12361da177e4SLinus Torvalds case MAY_LINK: 12371da177e4SLinus Torvalds av = FILE__LINK; 12381da177e4SLinus Torvalds break; 12391da177e4SLinus Torvalds case MAY_UNLINK: 12401da177e4SLinus Torvalds av = FILE__UNLINK; 12411da177e4SLinus Torvalds break; 12421da177e4SLinus Torvalds case MAY_RMDIR: 12431da177e4SLinus Torvalds av = DIR__RMDIR; 12441da177e4SLinus Torvalds break; 12451da177e4SLinus Torvalds default: 12461da177e4SLinus Torvalds printk(KERN_WARNING "may_link: unrecognized kind %d\n", kind); 12471da177e4SLinus Torvalds return 0; 12481da177e4SLinus Torvalds } 12491da177e4SLinus Torvalds 12501da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, isec->sclass, av, &ad); 12511da177e4SLinus Torvalds return rc; 12521da177e4SLinus Torvalds } 12531da177e4SLinus Torvalds 12541da177e4SLinus Torvalds static inline int may_rename(struct inode *old_dir, 12551da177e4SLinus Torvalds struct dentry *old_dentry, 12561da177e4SLinus Torvalds struct inode *new_dir, 12571da177e4SLinus Torvalds struct dentry *new_dentry) 12581da177e4SLinus Torvalds { 12591da177e4SLinus Torvalds struct task_security_struct *tsec; 12601da177e4SLinus Torvalds struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec; 12611da177e4SLinus Torvalds struct avc_audit_data ad; 12621da177e4SLinus Torvalds u32 av; 12631da177e4SLinus Torvalds int old_is_dir, new_is_dir; 12641da177e4SLinus Torvalds int rc; 12651da177e4SLinus Torvalds 12661da177e4SLinus Torvalds tsec = current->security; 12671da177e4SLinus Torvalds old_dsec = old_dir->i_security; 12681da177e4SLinus Torvalds old_isec = old_dentry->d_inode->i_security; 12691da177e4SLinus Torvalds old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 12701da177e4SLinus Torvalds new_dsec = new_dir->i_security; 12711da177e4SLinus Torvalds 12721da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 12731da177e4SLinus Torvalds 12741da177e4SLinus Torvalds ad.u.fs.dentry = old_dentry; 12751da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, old_dsec->sid, SECCLASS_DIR, 12761da177e4SLinus Torvalds DIR__REMOVE_NAME | DIR__SEARCH, &ad); 12771da177e4SLinus Torvalds if (rc) 12781da177e4SLinus Torvalds return rc; 12791da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, old_isec->sid, 12801da177e4SLinus Torvalds old_isec->sclass, FILE__RENAME, &ad); 12811da177e4SLinus Torvalds if (rc) 12821da177e4SLinus Torvalds return rc; 12831da177e4SLinus Torvalds if (old_is_dir && new_dir != old_dir) { 12841da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, old_isec->sid, 12851da177e4SLinus Torvalds old_isec->sclass, DIR__REPARENT, &ad); 12861da177e4SLinus Torvalds if (rc) 12871da177e4SLinus Torvalds return rc; 12881da177e4SLinus Torvalds } 12891da177e4SLinus Torvalds 12901da177e4SLinus Torvalds ad.u.fs.dentry = new_dentry; 12911da177e4SLinus Torvalds av = DIR__ADD_NAME | DIR__SEARCH; 12921da177e4SLinus Torvalds if (new_dentry->d_inode) 12931da177e4SLinus Torvalds av |= DIR__REMOVE_NAME; 12941da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, new_dsec->sid, SECCLASS_DIR, av, &ad); 12951da177e4SLinus Torvalds if (rc) 12961da177e4SLinus Torvalds return rc; 12971da177e4SLinus Torvalds if (new_dentry->d_inode) { 12981da177e4SLinus Torvalds new_isec = new_dentry->d_inode->i_security; 12991da177e4SLinus Torvalds new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode); 13001da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, new_isec->sid, 13011da177e4SLinus Torvalds new_isec->sclass, 13021da177e4SLinus Torvalds (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad); 13031da177e4SLinus Torvalds if (rc) 13041da177e4SLinus Torvalds return rc; 13051da177e4SLinus Torvalds } 13061da177e4SLinus Torvalds 13071da177e4SLinus Torvalds return 0; 13081da177e4SLinus Torvalds } 13091da177e4SLinus Torvalds 13101da177e4SLinus Torvalds /* Check whether a task can perform a filesystem operation. */ 13111da177e4SLinus Torvalds static int superblock_has_perm(struct task_struct *tsk, 13121da177e4SLinus Torvalds struct super_block *sb, 13131da177e4SLinus Torvalds u32 perms, 13141da177e4SLinus Torvalds struct avc_audit_data *ad) 13151da177e4SLinus Torvalds { 13161da177e4SLinus Torvalds struct task_security_struct *tsec; 13171da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 13181da177e4SLinus Torvalds 13191da177e4SLinus Torvalds tsec = tsk->security; 13201da177e4SLinus Torvalds sbsec = sb->s_security; 13211da177e4SLinus Torvalds return avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 13221da177e4SLinus Torvalds perms, ad); 13231da177e4SLinus Torvalds } 13241da177e4SLinus Torvalds 13251da177e4SLinus Torvalds /* Convert a Linux mode and permission mask to an access vector. */ 13261da177e4SLinus Torvalds static inline u32 file_mask_to_av(int mode, int mask) 13271da177e4SLinus Torvalds { 13281da177e4SLinus Torvalds u32 av = 0; 13291da177e4SLinus Torvalds 13301da177e4SLinus Torvalds if ((mode & S_IFMT) != S_IFDIR) { 13311da177e4SLinus Torvalds if (mask & MAY_EXEC) 13321da177e4SLinus Torvalds av |= FILE__EXECUTE; 13331da177e4SLinus Torvalds if (mask & MAY_READ) 13341da177e4SLinus Torvalds av |= FILE__READ; 13351da177e4SLinus Torvalds 13361da177e4SLinus Torvalds if (mask & MAY_APPEND) 13371da177e4SLinus Torvalds av |= FILE__APPEND; 13381da177e4SLinus Torvalds else if (mask & MAY_WRITE) 13391da177e4SLinus Torvalds av |= FILE__WRITE; 13401da177e4SLinus Torvalds 13411da177e4SLinus Torvalds } else { 13421da177e4SLinus Torvalds if (mask & MAY_EXEC) 13431da177e4SLinus Torvalds av |= DIR__SEARCH; 13441da177e4SLinus Torvalds if (mask & MAY_WRITE) 13451da177e4SLinus Torvalds av |= DIR__WRITE; 13461da177e4SLinus Torvalds if (mask & MAY_READ) 13471da177e4SLinus Torvalds av |= DIR__READ; 13481da177e4SLinus Torvalds } 13491da177e4SLinus Torvalds 13501da177e4SLinus Torvalds return av; 13511da177e4SLinus Torvalds } 13521da177e4SLinus Torvalds 13531da177e4SLinus Torvalds /* Convert a Linux file to an access vector. */ 13541da177e4SLinus Torvalds static inline u32 file_to_av(struct file *file) 13551da177e4SLinus Torvalds { 13561da177e4SLinus Torvalds u32 av = 0; 13571da177e4SLinus Torvalds 13581da177e4SLinus Torvalds if (file->f_mode & FMODE_READ) 13591da177e4SLinus Torvalds av |= FILE__READ; 13601da177e4SLinus Torvalds if (file->f_mode & FMODE_WRITE) { 13611da177e4SLinus Torvalds if (file->f_flags & O_APPEND) 13621da177e4SLinus Torvalds av |= FILE__APPEND; 13631da177e4SLinus Torvalds else 13641da177e4SLinus Torvalds av |= FILE__WRITE; 13651da177e4SLinus Torvalds } 13661da177e4SLinus Torvalds 13671da177e4SLinus Torvalds return av; 13681da177e4SLinus Torvalds } 13691da177e4SLinus Torvalds 13701da177e4SLinus Torvalds /* Hook functions begin here. */ 13711da177e4SLinus Torvalds 13721da177e4SLinus Torvalds static int selinux_ptrace(struct task_struct *parent, struct task_struct *child) 13731da177e4SLinus Torvalds { 13741da177e4SLinus Torvalds struct task_security_struct *psec = parent->security; 13751da177e4SLinus Torvalds struct task_security_struct *csec = child->security; 13761da177e4SLinus Torvalds int rc; 13771da177e4SLinus Torvalds 13781da177e4SLinus Torvalds rc = secondary_ops->ptrace(parent,child); 13791da177e4SLinus Torvalds if (rc) 13801da177e4SLinus Torvalds return rc; 13811da177e4SLinus Torvalds 13821da177e4SLinus Torvalds rc = task_has_perm(parent, child, PROCESS__PTRACE); 13831da177e4SLinus Torvalds /* Save the SID of the tracing process for later use in apply_creds. */ 1384341c2d80SStephen Smalley if (!(child->ptrace & PT_PTRACED) && !rc) 13851da177e4SLinus Torvalds csec->ptrace_sid = psec->sid; 13861da177e4SLinus Torvalds return rc; 13871da177e4SLinus Torvalds } 13881da177e4SLinus Torvalds 13891da177e4SLinus Torvalds static int selinux_capget(struct task_struct *target, kernel_cap_t *effective, 13901da177e4SLinus Torvalds kernel_cap_t *inheritable, kernel_cap_t *permitted) 13911da177e4SLinus Torvalds { 13921da177e4SLinus Torvalds int error; 13931da177e4SLinus Torvalds 13941da177e4SLinus Torvalds error = task_has_perm(current, target, PROCESS__GETCAP); 13951da177e4SLinus Torvalds if (error) 13961da177e4SLinus Torvalds return error; 13971da177e4SLinus Torvalds 13981da177e4SLinus Torvalds return secondary_ops->capget(target, effective, inheritable, permitted); 13991da177e4SLinus Torvalds } 14001da177e4SLinus Torvalds 14011da177e4SLinus Torvalds static int selinux_capset_check(struct task_struct *target, kernel_cap_t *effective, 14021da177e4SLinus Torvalds kernel_cap_t *inheritable, kernel_cap_t *permitted) 14031da177e4SLinus Torvalds { 14041da177e4SLinus Torvalds int error; 14051da177e4SLinus Torvalds 14061da177e4SLinus Torvalds error = secondary_ops->capset_check(target, effective, inheritable, permitted); 14071da177e4SLinus Torvalds if (error) 14081da177e4SLinus Torvalds return error; 14091da177e4SLinus Torvalds 14101da177e4SLinus Torvalds return task_has_perm(current, target, PROCESS__SETCAP); 14111da177e4SLinus Torvalds } 14121da177e4SLinus Torvalds 14131da177e4SLinus Torvalds static void selinux_capset_set(struct task_struct *target, kernel_cap_t *effective, 14141da177e4SLinus Torvalds kernel_cap_t *inheritable, kernel_cap_t *permitted) 14151da177e4SLinus Torvalds { 14161da177e4SLinus Torvalds secondary_ops->capset_set(target, effective, inheritable, permitted); 14171da177e4SLinus Torvalds } 14181da177e4SLinus Torvalds 14191da177e4SLinus Torvalds static int selinux_capable(struct task_struct *tsk, int cap) 14201da177e4SLinus Torvalds { 14211da177e4SLinus Torvalds int rc; 14221da177e4SLinus Torvalds 14231da177e4SLinus Torvalds rc = secondary_ops->capable(tsk, cap); 14241da177e4SLinus Torvalds if (rc) 14251da177e4SLinus Torvalds return rc; 14261da177e4SLinus Torvalds 14271da177e4SLinus Torvalds return task_has_capability(tsk,cap); 14281da177e4SLinus Torvalds } 14291da177e4SLinus Torvalds 14303fbfa981SEric W. Biederman static int selinux_sysctl_get_sid(ctl_table *table, u16 tclass, u32 *sid) 14313fbfa981SEric W. Biederman { 14323fbfa981SEric W. Biederman int buflen, rc; 14333fbfa981SEric W. Biederman char *buffer, *path, *end; 14343fbfa981SEric W. Biederman 14353fbfa981SEric W. Biederman rc = -ENOMEM; 14363fbfa981SEric W. Biederman buffer = (char*)__get_free_page(GFP_KERNEL); 14373fbfa981SEric W. Biederman if (!buffer) 14383fbfa981SEric W. Biederman goto out; 14393fbfa981SEric W. Biederman 14403fbfa981SEric W. Biederman buflen = PAGE_SIZE; 14413fbfa981SEric W. Biederman end = buffer+buflen; 14423fbfa981SEric W. Biederman *--end = '\0'; 14433fbfa981SEric W. Biederman buflen--; 14443fbfa981SEric W. Biederman path = end-1; 14453fbfa981SEric W. Biederman *path = '/'; 14463fbfa981SEric W. Biederman while (table) { 14473fbfa981SEric W. Biederman const char *name = table->procname; 14483fbfa981SEric W. Biederman size_t namelen = strlen(name); 14493fbfa981SEric W. Biederman buflen -= namelen + 1; 14503fbfa981SEric W. Biederman if (buflen < 0) 14513fbfa981SEric W. Biederman goto out_free; 14523fbfa981SEric W. Biederman end -= namelen; 14533fbfa981SEric W. Biederman memcpy(end, name, namelen); 14543fbfa981SEric W. Biederman *--end = '/'; 14553fbfa981SEric W. Biederman path = end; 14563fbfa981SEric W. Biederman table = table->parent; 14573fbfa981SEric W. Biederman } 1458b599fdfdSEric W. Biederman buflen -= 4; 1459b599fdfdSEric W. Biederman if (buflen < 0) 1460b599fdfdSEric W. Biederman goto out_free; 1461b599fdfdSEric W. Biederman end -= 4; 1462b599fdfdSEric W. Biederman memcpy(end, "/sys", 4); 1463b599fdfdSEric W. Biederman path = end; 14643fbfa981SEric W. Biederman rc = security_genfs_sid("proc", path, tclass, sid); 14653fbfa981SEric W. Biederman out_free: 14663fbfa981SEric W. Biederman free_page((unsigned long)buffer); 14673fbfa981SEric W. Biederman out: 14683fbfa981SEric W. Biederman return rc; 14693fbfa981SEric W. Biederman } 14703fbfa981SEric W. Biederman 14711da177e4SLinus Torvalds static int selinux_sysctl(ctl_table *table, int op) 14721da177e4SLinus Torvalds { 14731da177e4SLinus Torvalds int error = 0; 14741da177e4SLinus Torvalds u32 av; 14751da177e4SLinus Torvalds struct task_security_struct *tsec; 14761da177e4SLinus Torvalds u32 tsid; 14771da177e4SLinus Torvalds int rc; 14781da177e4SLinus Torvalds 14791da177e4SLinus Torvalds rc = secondary_ops->sysctl(table, op); 14801da177e4SLinus Torvalds if (rc) 14811da177e4SLinus Torvalds return rc; 14821da177e4SLinus Torvalds 14831da177e4SLinus Torvalds tsec = current->security; 14841da177e4SLinus Torvalds 14853fbfa981SEric W. Biederman rc = selinux_sysctl_get_sid(table, (op == 0001) ? 14861da177e4SLinus Torvalds SECCLASS_DIR : SECCLASS_FILE, &tsid); 14871da177e4SLinus Torvalds if (rc) { 14881da177e4SLinus Torvalds /* Default to the well-defined sysctl SID. */ 14891da177e4SLinus Torvalds tsid = SECINITSID_SYSCTL; 14901da177e4SLinus Torvalds } 14911da177e4SLinus Torvalds 14921da177e4SLinus Torvalds /* The op values are "defined" in sysctl.c, thereby creating 14931da177e4SLinus Torvalds * a bad coupling between this module and sysctl.c */ 14941da177e4SLinus Torvalds if(op == 001) { 14951da177e4SLinus Torvalds error = avc_has_perm(tsec->sid, tsid, 14961da177e4SLinus Torvalds SECCLASS_DIR, DIR__SEARCH, NULL); 14971da177e4SLinus Torvalds } else { 14981da177e4SLinus Torvalds av = 0; 14991da177e4SLinus Torvalds if (op & 004) 15001da177e4SLinus Torvalds av |= FILE__READ; 15011da177e4SLinus Torvalds if (op & 002) 15021da177e4SLinus Torvalds av |= FILE__WRITE; 15031da177e4SLinus Torvalds if (av) 15041da177e4SLinus Torvalds error = avc_has_perm(tsec->sid, tsid, 15051da177e4SLinus Torvalds SECCLASS_FILE, av, NULL); 15061da177e4SLinus Torvalds } 15071da177e4SLinus Torvalds 15081da177e4SLinus Torvalds return error; 15091da177e4SLinus Torvalds } 15101da177e4SLinus Torvalds 15111da177e4SLinus Torvalds static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb) 15121da177e4SLinus Torvalds { 15131da177e4SLinus Torvalds int rc = 0; 15141da177e4SLinus Torvalds 15151da177e4SLinus Torvalds if (!sb) 15161da177e4SLinus Torvalds return 0; 15171da177e4SLinus Torvalds 15181da177e4SLinus Torvalds switch (cmds) { 15191da177e4SLinus Torvalds case Q_SYNC: 15201da177e4SLinus Torvalds case Q_QUOTAON: 15211da177e4SLinus Torvalds case Q_QUOTAOFF: 15221da177e4SLinus Torvalds case Q_SETINFO: 15231da177e4SLinus Torvalds case Q_SETQUOTA: 15241da177e4SLinus Torvalds rc = superblock_has_perm(current, 15251da177e4SLinus Torvalds sb, 15261da177e4SLinus Torvalds FILESYSTEM__QUOTAMOD, NULL); 15271da177e4SLinus Torvalds break; 15281da177e4SLinus Torvalds case Q_GETFMT: 15291da177e4SLinus Torvalds case Q_GETINFO: 15301da177e4SLinus Torvalds case Q_GETQUOTA: 15311da177e4SLinus Torvalds rc = superblock_has_perm(current, 15321da177e4SLinus Torvalds sb, 15331da177e4SLinus Torvalds FILESYSTEM__QUOTAGET, NULL); 15341da177e4SLinus Torvalds break; 15351da177e4SLinus Torvalds default: 15361da177e4SLinus Torvalds rc = 0; /* let the kernel handle invalid cmds */ 15371da177e4SLinus Torvalds break; 15381da177e4SLinus Torvalds } 15391da177e4SLinus Torvalds return rc; 15401da177e4SLinus Torvalds } 15411da177e4SLinus Torvalds 15421da177e4SLinus Torvalds static int selinux_quota_on(struct dentry *dentry) 15431da177e4SLinus Torvalds { 15441da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__QUOTAON); 15451da177e4SLinus Torvalds } 15461da177e4SLinus Torvalds 15471da177e4SLinus Torvalds static int selinux_syslog(int type) 15481da177e4SLinus Torvalds { 15491da177e4SLinus Torvalds int rc; 15501da177e4SLinus Torvalds 15511da177e4SLinus Torvalds rc = secondary_ops->syslog(type); 15521da177e4SLinus Torvalds if (rc) 15531da177e4SLinus Torvalds return rc; 15541da177e4SLinus Torvalds 15551da177e4SLinus Torvalds switch (type) { 15561da177e4SLinus Torvalds case 3: /* Read last kernel messages */ 15571da177e4SLinus Torvalds case 10: /* Return size of the log buffer */ 15581da177e4SLinus Torvalds rc = task_has_system(current, SYSTEM__SYSLOG_READ); 15591da177e4SLinus Torvalds break; 15601da177e4SLinus Torvalds case 6: /* Disable logging to console */ 15611da177e4SLinus Torvalds case 7: /* Enable logging to console */ 15621da177e4SLinus Torvalds case 8: /* Set level of messages printed to console */ 15631da177e4SLinus Torvalds rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE); 15641da177e4SLinus Torvalds break; 15651da177e4SLinus Torvalds case 0: /* Close log */ 15661da177e4SLinus Torvalds case 1: /* Open log */ 15671da177e4SLinus Torvalds case 2: /* Read from log */ 15681da177e4SLinus Torvalds case 4: /* Read/clear last kernel messages */ 15691da177e4SLinus Torvalds case 5: /* Clear ring buffer */ 15701da177e4SLinus Torvalds default: 15711da177e4SLinus Torvalds rc = task_has_system(current, SYSTEM__SYSLOG_MOD); 15721da177e4SLinus Torvalds break; 15731da177e4SLinus Torvalds } 15741da177e4SLinus Torvalds return rc; 15751da177e4SLinus Torvalds } 15761da177e4SLinus Torvalds 15771da177e4SLinus Torvalds /* 15781da177e4SLinus Torvalds * Check that a process has enough memory to allocate a new virtual 15791da177e4SLinus Torvalds * mapping. 0 means there is enough memory for the allocation to 15801da177e4SLinus Torvalds * succeed and -ENOMEM implies there is not. 15811da177e4SLinus Torvalds * 15821da177e4SLinus Torvalds * Note that secondary_ops->capable and task_has_perm_noaudit return 0 15831da177e4SLinus Torvalds * if the capability is granted, but __vm_enough_memory requires 1 if 15841da177e4SLinus Torvalds * the capability is granted. 15851da177e4SLinus Torvalds * 15861da177e4SLinus Torvalds * Do not audit the selinux permission check, as this is applied to all 15871da177e4SLinus Torvalds * processes that allocate mappings. 15881da177e4SLinus Torvalds */ 158934b4e4aaSAlan Cox static int selinux_vm_enough_memory(struct mm_struct *mm, long pages) 15901da177e4SLinus Torvalds { 15911da177e4SLinus Torvalds int rc, cap_sys_admin = 0; 15921da177e4SLinus Torvalds struct task_security_struct *tsec = current->security; 15931da177e4SLinus Torvalds 15941da177e4SLinus Torvalds rc = secondary_ops->capable(current, CAP_SYS_ADMIN); 15951da177e4SLinus Torvalds if (rc == 0) 15961da177e4SLinus Torvalds rc = avc_has_perm_noaudit(tsec->sid, tsec->sid, 15971da177e4SLinus Torvalds SECCLASS_CAPABILITY, 15981da177e4SLinus Torvalds CAP_TO_MASK(CAP_SYS_ADMIN), 15992c3c05dbSStephen Smalley 0, 16001da177e4SLinus Torvalds NULL); 16011da177e4SLinus Torvalds 16021da177e4SLinus Torvalds if (rc == 0) 16031da177e4SLinus Torvalds cap_sys_admin = 1; 16041da177e4SLinus Torvalds 160534b4e4aaSAlan Cox return __vm_enough_memory(mm, pages, cap_sys_admin); 16061da177e4SLinus Torvalds } 16071da177e4SLinus Torvalds 16081da177e4SLinus Torvalds /* binprm security operations */ 16091da177e4SLinus Torvalds 16101da177e4SLinus Torvalds static int selinux_bprm_alloc_security(struct linux_binprm *bprm) 16111da177e4SLinus Torvalds { 16121da177e4SLinus Torvalds struct bprm_security_struct *bsec; 16131da177e4SLinus Torvalds 161489d155efSJames Morris bsec = kzalloc(sizeof(struct bprm_security_struct), GFP_KERNEL); 16151da177e4SLinus Torvalds if (!bsec) 16161da177e4SLinus Torvalds return -ENOMEM; 16171da177e4SLinus Torvalds 16181da177e4SLinus Torvalds bsec->bprm = bprm; 16191da177e4SLinus Torvalds bsec->sid = SECINITSID_UNLABELED; 16201da177e4SLinus Torvalds bsec->set = 0; 16211da177e4SLinus Torvalds 16221da177e4SLinus Torvalds bprm->security = bsec; 16231da177e4SLinus Torvalds return 0; 16241da177e4SLinus Torvalds } 16251da177e4SLinus Torvalds 16261da177e4SLinus Torvalds static int selinux_bprm_set_security(struct linux_binprm *bprm) 16271da177e4SLinus Torvalds { 16281da177e4SLinus Torvalds struct task_security_struct *tsec; 16293d5ff529SJosef Sipek struct inode *inode = bprm->file->f_path.dentry->d_inode; 16301da177e4SLinus Torvalds struct inode_security_struct *isec; 16311da177e4SLinus Torvalds struct bprm_security_struct *bsec; 16321da177e4SLinus Torvalds u32 newsid; 16331da177e4SLinus Torvalds struct avc_audit_data ad; 16341da177e4SLinus Torvalds int rc; 16351da177e4SLinus Torvalds 16361da177e4SLinus Torvalds rc = secondary_ops->bprm_set_security(bprm); 16371da177e4SLinus Torvalds if (rc) 16381da177e4SLinus Torvalds return rc; 16391da177e4SLinus Torvalds 16401da177e4SLinus Torvalds bsec = bprm->security; 16411da177e4SLinus Torvalds 16421da177e4SLinus Torvalds if (bsec->set) 16431da177e4SLinus Torvalds return 0; 16441da177e4SLinus Torvalds 16451da177e4SLinus Torvalds tsec = current->security; 16461da177e4SLinus Torvalds isec = inode->i_security; 16471da177e4SLinus Torvalds 16481da177e4SLinus Torvalds /* Default to the current task SID. */ 16491da177e4SLinus Torvalds bsec->sid = tsec->sid; 16501da177e4SLinus Torvalds 165128eba5bfSMichael LeMay /* Reset fs, key, and sock SIDs on execve. */ 16521da177e4SLinus Torvalds tsec->create_sid = 0; 165328eba5bfSMichael LeMay tsec->keycreate_sid = 0; 165442c3e03eSEric Paris tsec->sockcreate_sid = 0; 16551da177e4SLinus Torvalds 16561da177e4SLinus Torvalds if (tsec->exec_sid) { 16571da177e4SLinus Torvalds newsid = tsec->exec_sid; 16581da177e4SLinus Torvalds /* Reset exec SID on execve. */ 16591da177e4SLinus Torvalds tsec->exec_sid = 0; 16601da177e4SLinus Torvalds } else { 16611da177e4SLinus Torvalds /* Check for a default transition on this program. */ 16621da177e4SLinus Torvalds rc = security_transition_sid(tsec->sid, isec->sid, 16631da177e4SLinus Torvalds SECCLASS_PROCESS, &newsid); 16641da177e4SLinus Torvalds if (rc) 16651da177e4SLinus Torvalds return rc; 16661da177e4SLinus Torvalds } 16671da177e4SLinus Torvalds 16681da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 16693d5ff529SJosef Sipek ad.u.fs.mnt = bprm->file->f_path.mnt; 16703d5ff529SJosef Sipek ad.u.fs.dentry = bprm->file->f_path.dentry; 16711da177e4SLinus Torvalds 16723d5ff529SJosef Sipek if (bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) 16731da177e4SLinus Torvalds newsid = tsec->sid; 16741da177e4SLinus Torvalds 16751da177e4SLinus Torvalds if (tsec->sid == newsid) { 16761da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, 16771da177e4SLinus Torvalds SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad); 16781da177e4SLinus Torvalds if (rc) 16791da177e4SLinus Torvalds return rc; 16801da177e4SLinus Torvalds } else { 16811da177e4SLinus Torvalds /* Check permissions for the transition. */ 16821da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, newsid, 16831da177e4SLinus Torvalds SECCLASS_PROCESS, PROCESS__TRANSITION, &ad); 16841da177e4SLinus Torvalds if (rc) 16851da177e4SLinus Torvalds return rc; 16861da177e4SLinus Torvalds 16871da177e4SLinus Torvalds rc = avc_has_perm(newsid, isec->sid, 16881da177e4SLinus Torvalds SECCLASS_FILE, FILE__ENTRYPOINT, &ad); 16891da177e4SLinus Torvalds if (rc) 16901da177e4SLinus Torvalds return rc; 16911da177e4SLinus Torvalds 16921da177e4SLinus Torvalds /* Clear any possibly unsafe personality bits on exec: */ 16931da177e4SLinus Torvalds current->personality &= ~PER_CLEAR_ON_SETID; 16941da177e4SLinus Torvalds 16951da177e4SLinus Torvalds /* Set the security field to the new SID. */ 16961da177e4SLinus Torvalds bsec->sid = newsid; 16971da177e4SLinus Torvalds } 16981da177e4SLinus Torvalds 16991da177e4SLinus Torvalds bsec->set = 1; 17001da177e4SLinus Torvalds return 0; 17011da177e4SLinus Torvalds } 17021da177e4SLinus Torvalds 17031da177e4SLinus Torvalds static int selinux_bprm_check_security (struct linux_binprm *bprm) 17041da177e4SLinus Torvalds { 17051da177e4SLinus Torvalds return secondary_ops->bprm_check_security(bprm); 17061da177e4SLinus Torvalds } 17071da177e4SLinus Torvalds 17081da177e4SLinus Torvalds 17091da177e4SLinus Torvalds static int selinux_bprm_secureexec (struct linux_binprm *bprm) 17101da177e4SLinus Torvalds { 17111da177e4SLinus Torvalds struct task_security_struct *tsec = current->security; 17121da177e4SLinus Torvalds int atsecure = 0; 17131da177e4SLinus Torvalds 17141da177e4SLinus Torvalds if (tsec->osid != tsec->sid) { 17151da177e4SLinus Torvalds /* Enable secure mode for SIDs transitions unless 17161da177e4SLinus Torvalds the noatsecure permission is granted between 17171da177e4SLinus Torvalds the two SIDs, i.e. ahp returns 0. */ 17181da177e4SLinus Torvalds atsecure = avc_has_perm(tsec->osid, tsec->sid, 17191da177e4SLinus Torvalds SECCLASS_PROCESS, 17201da177e4SLinus Torvalds PROCESS__NOATSECURE, NULL); 17211da177e4SLinus Torvalds } 17221da177e4SLinus Torvalds 17231da177e4SLinus Torvalds return (atsecure || secondary_ops->bprm_secureexec(bprm)); 17241da177e4SLinus Torvalds } 17251da177e4SLinus Torvalds 17261da177e4SLinus Torvalds static void selinux_bprm_free_security(struct linux_binprm *bprm) 17271da177e4SLinus Torvalds { 17289a5f04bfSJesper Juhl kfree(bprm->security); 17291da177e4SLinus Torvalds bprm->security = NULL; 17301da177e4SLinus Torvalds } 17311da177e4SLinus Torvalds 17321da177e4SLinus Torvalds extern struct vfsmount *selinuxfs_mount; 17331da177e4SLinus Torvalds extern struct dentry *selinux_null; 17341da177e4SLinus Torvalds 17351da177e4SLinus Torvalds /* Derived from fs/exec.c:flush_old_files. */ 17361da177e4SLinus Torvalds static inline void flush_unauthorized_files(struct files_struct * files) 17371da177e4SLinus Torvalds { 17381da177e4SLinus Torvalds struct avc_audit_data ad; 17391da177e4SLinus Torvalds struct file *file, *devnull = NULL; 1740b20c8122SStephen Smalley struct tty_struct *tty; 1741badf1662SDipankar Sarma struct fdtable *fdt; 17421da177e4SLinus Torvalds long j = -1; 174324ec839cSPeter Zijlstra int drop_tty = 0; 17441da177e4SLinus Torvalds 1745b20c8122SStephen Smalley mutex_lock(&tty_mutex); 174624ec839cSPeter Zijlstra tty = get_current_tty(); 17471da177e4SLinus Torvalds if (tty) { 17481da177e4SLinus Torvalds file_list_lock(); 17492f512016SEric Dumazet file = list_entry(tty->tty_files.next, typeof(*file), f_u.fu_list); 17501da177e4SLinus Torvalds if (file) { 17511da177e4SLinus Torvalds /* Revalidate access to controlling tty. 17521da177e4SLinus Torvalds Use inode_has_perm on the tty inode directly rather 17531da177e4SLinus Torvalds than using file_has_perm, as this particular open 17541da177e4SLinus Torvalds file may belong to another process and we are only 17551da177e4SLinus Torvalds interested in the inode-based check here. */ 17563d5ff529SJosef Sipek struct inode *inode = file->f_path.dentry->d_inode; 17571da177e4SLinus Torvalds if (inode_has_perm(current, inode, 17581da177e4SLinus Torvalds FILE__READ | FILE__WRITE, NULL)) { 175924ec839cSPeter Zijlstra drop_tty = 1; 17601da177e4SLinus Torvalds } 17611da177e4SLinus Torvalds } 17621da177e4SLinus Torvalds file_list_unlock(); 17631da177e4SLinus Torvalds } 1764b20c8122SStephen Smalley mutex_unlock(&tty_mutex); 176598a27ba4SEric W. Biederman /* Reset controlling tty. */ 176698a27ba4SEric W. Biederman if (drop_tty) 176798a27ba4SEric W. Biederman no_tty(); 17681da177e4SLinus Torvalds 17691da177e4SLinus Torvalds /* Revalidate access to inherited open files. */ 17701da177e4SLinus Torvalds 17711da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,FS); 17721da177e4SLinus Torvalds 17731da177e4SLinus Torvalds spin_lock(&files->file_lock); 17741da177e4SLinus Torvalds for (;;) { 17751da177e4SLinus Torvalds unsigned long set, i; 17761da177e4SLinus Torvalds int fd; 17771da177e4SLinus Torvalds 17781da177e4SLinus Torvalds j++; 17791da177e4SLinus Torvalds i = j * __NFDBITS; 1780badf1662SDipankar Sarma fdt = files_fdtable(files); 1781bbea9f69SVadim Lobanov if (i >= fdt->max_fds) 17821da177e4SLinus Torvalds break; 1783badf1662SDipankar Sarma set = fdt->open_fds->fds_bits[j]; 17841da177e4SLinus Torvalds if (!set) 17851da177e4SLinus Torvalds continue; 17861da177e4SLinus Torvalds spin_unlock(&files->file_lock); 17871da177e4SLinus Torvalds for ( ; set ; i++,set >>= 1) { 17881da177e4SLinus Torvalds if (set & 1) { 17891da177e4SLinus Torvalds file = fget(i); 17901da177e4SLinus Torvalds if (!file) 17911da177e4SLinus Torvalds continue; 17921da177e4SLinus Torvalds if (file_has_perm(current, 17931da177e4SLinus Torvalds file, 17941da177e4SLinus Torvalds file_to_av(file))) { 17951da177e4SLinus Torvalds sys_close(i); 17961da177e4SLinus Torvalds fd = get_unused_fd(); 17971da177e4SLinus Torvalds if (fd != i) { 17981da177e4SLinus Torvalds if (fd >= 0) 17991da177e4SLinus Torvalds put_unused_fd(fd); 18001da177e4SLinus Torvalds fput(file); 18011da177e4SLinus Torvalds continue; 18021da177e4SLinus Torvalds } 18031da177e4SLinus Torvalds if (devnull) { 1804095975daSNick Piggin get_file(devnull); 18051da177e4SLinus Torvalds } else { 18061da177e4SLinus Torvalds devnull = dentry_open(dget(selinux_null), mntget(selinuxfs_mount), O_RDWR); 1807fc5d81e6SAkinobu Mita if (IS_ERR(devnull)) { 1808fc5d81e6SAkinobu Mita devnull = NULL; 18091da177e4SLinus Torvalds put_unused_fd(fd); 18101da177e4SLinus Torvalds fput(file); 18111da177e4SLinus Torvalds continue; 18121da177e4SLinus Torvalds } 18131da177e4SLinus Torvalds } 18141da177e4SLinus Torvalds fd_install(fd, devnull); 18151da177e4SLinus Torvalds } 18161da177e4SLinus Torvalds fput(file); 18171da177e4SLinus Torvalds } 18181da177e4SLinus Torvalds } 18191da177e4SLinus Torvalds spin_lock(&files->file_lock); 18201da177e4SLinus Torvalds 18211da177e4SLinus Torvalds } 18221da177e4SLinus Torvalds spin_unlock(&files->file_lock); 18231da177e4SLinus Torvalds } 18241da177e4SLinus Torvalds 18251da177e4SLinus Torvalds static void selinux_bprm_apply_creds(struct linux_binprm *bprm, int unsafe) 18261da177e4SLinus Torvalds { 18271da177e4SLinus Torvalds struct task_security_struct *tsec; 18281da177e4SLinus Torvalds struct bprm_security_struct *bsec; 18291da177e4SLinus Torvalds u32 sid; 18301da177e4SLinus Torvalds int rc; 18311da177e4SLinus Torvalds 18321da177e4SLinus Torvalds secondary_ops->bprm_apply_creds(bprm, unsafe); 18331da177e4SLinus Torvalds 18341da177e4SLinus Torvalds tsec = current->security; 18351da177e4SLinus Torvalds 18361da177e4SLinus Torvalds bsec = bprm->security; 18371da177e4SLinus Torvalds sid = bsec->sid; 18381da177e4SLinus Torvalds 18391da177e4SLinus Torvalds tsec->osid = tsec->sid; 18401da177e4SLinus Torvalds bsec->unsafe = 0; 18411da177e4SLinus Torvalds if (tsec->sid != sid) { 18421da177e4SLinus Torvalds /* Check for shared state. If not ok, leave SID 18431da177e4SLinus Torvalds unchanged and kill. */ 18441da177e4SLinus Torvalds if (unsafe & LSM_UNSAFE_SHARE) { 18451da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS, 18461da177e4SLinus Torvalds PROCESS__SHARE, NULL); 18471da177e4SLinus Torvalds if (rc) { 18481da177e4SLinus Torvalds bsec->unsafe = 1; 18491da177e4SLinus Torvalds return; 18501da177e4SLinus Torvalds } 18511da177e4SLinus Torvalds } 18521da177e4SLinus Torvalds 18531da177e4SLinus Torvalds /* Check for ptracing, and update the task SID if ok. 18541da177e4SLinus Torvalds Otherwise, leave SID unchanged and kill. */ 18551da177e4SLinus Torvalds if (unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) { 18561da177e4SLinus Torvalds rc = avc_has_perm(tsec->ptrace_sid, sid, 18571da177e4SLinus Torvalds SECCLASS_PROCESS, PROCESS__PTRACE, 18581da177e4SLinus Torvalds NULL); 18591da177e4SLinus Torvalds if (rc) { 18601da177e4SLinus Torvalds bsec->unsafe = 1; 18611da177e4SLinus Torvalds return; 18621da177e4SLinus Torvalds } 18631da177e4SLinus Torvalds } 18641da177e4SLinus Torvalds tsec->sid = sid; 18651da177e4SLinus Torvalds } 18661da177e4SLinus Torvalds } 18671da177e4SLinus Torvalds 18681da177e4SLinus Torvalds /* 18691da177e4SLinus Torvalds * called after apply_creds without the task lock held 18701da177e4SLinus Torvalds */ 18711da177e4SLinus Torvalds static void selinux_bprm_post_apply_creds(struct linux_binprm *bprm) 18721da177e4SLinus Torvalds { 18731da177e4SLinus Torvalds struct task_security_struct *tsec; 18741da177e4SLinus Torvalds struct rlimit *rlim, *initrlim; 18751da177e4SLinus Torvalds struct itimerval itimer; 18761da177e4SLinus Torvalds struct bprm_security_struct *bsec; 18771da177e4SLinus Torvalds int rc, i; 18781da177e4SLinus Torvalds 18791da177e4SLinus Torvalds tsec = current->security; 18801da177e4SLinus Torvalds bsec = bprm->security; 18811da177e4SLinus Torvalds 18821da177e4SLinus Torvalds if (bsec->unsafe) { 18831da177e4SLinus Torvalds force_sig_specific(SIGKILL, current); 18841da177e4SLinus Torvalds return; 18851da177e4SLinus Torvalds } 18861da177e4SLinus Torvalds if (tsec->osid == tsec->sid) 18871da177e4SLinus Torvalds return; 18881da177e4SLinus Torvalds 18891da177e4SLinus Torvalds /* Close files for which the new task SID is not authorized. */ 18901da177e4SLinus Torvalds flush_unauthorized_files(current->files); 18911da177e4SLinus Torvalds 18921da177e4SLinus Torvalds /* Check whether the new SID can inherit signal state 18931da177e4SLinus Torvalds from the old SID. If not, clear itimers to avoid 18941da177e4SLinus Torvalds subsequent signal generation and flush and unblock 18951da177e4SLinus Torvalds signals. This must occur _after_ the task SID has 18961da177e4SLinus Torvalds been updated so that any kill done after the flush 18971da177e4SLinus Torvalds will be checked against the new SID. */ 18981da177e4SLinus Torvalds rc = avc_has_perm(tsec->osid, tsec->sid, SECCLASS_PROCESS, 18991da177e4SLinus Torvalds PROCESS__SIGINH, NULL); 19001da177e4SLinus Torvalds if (rc) { 19011da177e4SLinus Torvalds memset(&itimer, 0, sizeof itimer); 19021da177e4SLinus Torvalds for (i = 0; i < 3; i++) 19031da177e4SLinus Torvalds do_setitimer(i, &itimer, NULL); 19041da177e4SLinus Torvalds flush_signals(current); 19051da177e4SLinus Torvalds spin_lock_irq(¤t->sighand->siglock); 19061da177e4SLinus Torvalds flush_signal_handlers(current, 1); 19071da177e4SLinus Torvalds sigemptyset(¤t->blocked); 19081da177e4SLinus Torvalds recalc_sigpending(); 19091da177e4SLinus Torvalds spin_unlock_irq(¤t->sighand->siglock); 19101da177e4SLinus Torvalds } 19111da177e4SLinus Torvalds 19124ac212adSStephen Smalley /* Always clear parent death signal on SID transitions. */ 19134ac212adSStephen Smalley current->pdeath_signal = 0; 19144ac212adSStephen Smalley 19151da177e4SLinus Torvalds /* Check whether the new SID can inherit resource limits 19161da177e4SLinus Torvalds from the old SID. If not, reset all soft limits to 19171da177e4SLinus Torvalds the lower of the current task's hard limit and the init 19181da177e4SLinus Torvalds task's soft limit. Note that the setting of hard limits 19191da177e4SLinus Torvalds (even to lower them) can be controlled by the setrlimit 19201da177e4SLinus Torvalds check. The inclusion of the init task's soft limit into 19211da177e4SLinus Torvalds the computation is to avoid resetting soft limits higher 19221da177e4SLinus Torvalds than the default soft limit for cases where the default 19231da177e4SLinus Torvalds is lower than the hard limit, e.g. RLIMIT_CORE or 19241da177e4SLinus Torvalds RLIMIT_STACK.*/ 19251da177e4SLinus Torvalds rc = avc_has_perm(tsec->osid, tsec->sid, SECCLASS_PROCESS, 19261da177e4SLinus Torvalds PROCESS__RLIMITINH, NULL); 19271da177e4SLinus Torvalds if (rc) { 19281da177e4SLinus Torvalds for (i = 0; i < RLIM_NLIMITS; i++) { 19291da177e4SLinus Torvalds rlim = current->signal->rlim + i; 19301da177e4SLinus Torvalds initrlim = init_task.signal->rlim+i; 19311da177e4SLinus Torvalds rlim->rlim_cur = min(rlim->rlim_max,initrlim->rlim_cur); 19321da177e4SLinus Torvalds } 19331da177e4SLinus Torvalds if (current->signal->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) { 19341da177e4SLinus Torvalds /* 19351da177e4SLinus Torvalds * This will cause RLIMIT_CPU calculations 19361da177e4SLinus Torvalds * to be refigured. 19371da177e4SLinus Torvalds */ 19381da177e4SLinus Torvalds current->it_prof_expires = jiffies_to_cputime(1); 19391da177e4SLinus Torvalds } 19401da177e4SLinus Torvalds } 19411da177e4SLinus Torvalds 19421da177e4SLinus Torvalds /* Wake up the parent if it is waiting so that it can 19431da177e4SLinus Torvalds recheck wait permission to the new task SID. */ 19441da177e4SLinus Torvalds wake_up_interruptible(¤t->parent->signal->wait_chldexit); 19451da177e4SLinus Torvalds } 19461da177e4SLinus Torvalds 19471da177e4SLinus Torvalds /* superblock security operations */ 19481da177e4SLinus Torvalds 19491da177e4SLinus Torvalds static int selinux_sb_alloc_security(struct super_block *sb) 19501da177e4SLinus Torvalds { 19511da177e4SLinus Torvalds return superblock_alloc_security(sb); 19521da177e4SLinus Torvalds } 19531da177e4SLinus Torvalds 19541da177e4SLinus Torvalds static void selinux_sb_free_security(struct super_block *sb) 19551da177e4SLinus Torvalds { 19561da177e4SLinus Torvalds superblock_free_security(sb); 19571da177e4SLinus Torvalds } 19581da177e4SLinus Torvalds 19591da177e4SLinus Torvalds static inline int match_prefix(char *prefix, int plen, char *option, int olen) 19601da177e4SLinus Torvalds { 19611da177e4SLinus Torvalds if (plen > olen) 19621da177e4SLinus Torvalds return 0; 19631da177e4SLinus Torvalds 19641da177e4SLinus Torvalds return !memcmp(prefix, option, plen); 19651da177e4SLinus Torvalds } 19661da177e4SLinus Torvalds 19671da177e4SLinus Torvalds static inline int selinux_option(char *option, int len) 19681da177e4SLinus Torvalds { 19691da177e4SLinus Torvalds return (match_prefix("context=", sizeof("context=")-1, option, len) || 19701da177e4SLinus Torvalds match_prefix("fscontext=", sizeof("fscontext=")-1, option, len) || 19710808925eSEric Paris match_prefix("defcontext=", sizeof("defcontext=")-1, option, len) || 19720808925eSEric Paris match_prefix("rootcontext=", sizeof("rootcontext=")-1, option, len)); 19731da177e4SLinus Torvalds } 19741da177e4SLinus Torvalds 19751da177e4SLinus Torvalds static inline void take_option(char **to, char *from, int *first, int len) 19761da177e4SLinus Torvalds { 19771da177e4SLinus Torvalds if (!*first) { 19781da177e4SLinus Torvalds **to = ','; 19791da177e4SLinus Torvalds *to += 1; 19803528a953SCory Olmo } else 19811da177e4SLinus Torvalds *first = 0; 19821da177e4SLinus Torvalds memcpy(*to, from, len); 19831da177e4SLinus Torvalds *to += len; 19841da177e4SLinus Torvalds } 19851da177e4SLinus Torvalds 19863528a953SCory Olmo static inline void take_selinux_option(char **to, char *from, int *first, 19873528a953SCory Olmo int len) 19883528a953SCory Olmo { 19893528a953SCory Olmo int current_size = 0; 19903528a953SCory Olmo 19913528a953SCory Olmo if (!*first) { 19923528a953SCory Olmo **to = '|'; 19933528a953SCory Olmo *to += 1; 19943528a953SCory Olmo } 19953528a953SCory Olmo else 19963528a953SCory Olmo *first = 0; 19973528a953SCory Olmo 19983528a953SCory Olmo while (current_size < len) { 19993528a953SCory Olmo if (*from != '"') { 20003528a953SCory Olmo **to = *from; 20013528a953SCory Olmo *to += 1; 20023528a953SCory Olmo } 20033528a953SCory Olmo from += 1; 20043528a953SCory Olmo current_size += 1; 20053528a953SCory Olmo } 20063528a953SCory Olmo } 20073528a953SCory Olmo 20081da177e4SLinus Torvalds static int selinux_sb_copy_data(struct file_system_type *type, void *orig, void *copy) 20091da177e4SLinus Torvalds { 20101da177e4SLinus Torvalds int fnosec, fsec, rc = 0; 20111da177e4SLinus Torvalds char *in_save, *in_curr, *in_end; 20121da177e4SLinus Torvalds char *sec_curr, *nosec_save, *nosec; 20133528a953SCory Olmo int open_quote = 0; 20141da177e4SLinus Torvalds 20151da177e4SLinus Torvalds in_curr = orig; 20161da177e4SLinus Torvalds sec_curr = copy; 20171da177e4SLinus Torvalds 20181da177e4SLinus Torvalds /* Binary mount data: just copy */ 20191da177e4SLinus Torvalds if (type->fs_flags & FS_BINARY_MOUNTDATA) { 20201da177e4SLinus Torvalds copy_page(sec_curr, in_curr); 20211da177e4SLinus Torvalds goto out; 20221da177e4SLinus Torvalds } 20231da177e4SLinus Torvalds 20241da177e4SLinus Torvalds nosec = (char *)get_zeroed_page(GFP_KERNEL); 20251da177e4SLinus Torvalds if (!nosec) { 20261da177e4SLinus Torvalds rc = -ENOMEM; 20271da177e4SLinus Torvalds goto out; 20281da177e4SLinus Torvalds } 20291da177e4SLinus Torvalds 20301da177e4SLinus Torvalds nosec_save = nosec; 20311da177e4SLinus Torvalds fnosec = fsec = 1; 20321da177e4SLinus Torvalds in_save = in_end = orig; 20331da177e4SLinus Torvalds 20341da177e4SLinus Torvalds do { 20353528a953SCory Olmo if (*in_end == '"') 20363528a953SCory Olmo open_quote = !open_quote; 20373528a953SCory Olmo if ((*in_end == ',' && open_quote == 0) || 20383528a953SCory Olmo *in_end == '\0') { 20391da177e4SLinus Torvalds int len = in_end - in_curr; 20401da177e4SLinus Torvalds 20411da177e4SLinus Torvalds if (selinux_option(in_curr, len)) 20423528a953SCory Olmo take_selinux_option(&sec_curr, in_curr, &fsec, len); 20431da177e4SLinus Torvalds else 20441da177e4SLinus Torvalds take_option(&nosec, in_curr, &fnosec, len); 20451da177e4SLinus Torvalds 20461da177e4SLinus Torvalds in_curr = in_end + 1; 20471da177e4SLinus Torvalds } 20481da177e4SLinus Torvalds } while (*in_end++); 20491da177e4SLinus Torvalds 20506931dfc9SEric Paris strcpy(in_save, nosec_save); 2051da3caa20SGerald Schaefer free_page((unsigned long)nosec_save); 20521da177e4SLinus Torvalds out: 20531da177e4SLinus Torvalds return rc; 20541da177e4SLinus Torvalds } 20551da177e4SLinus Torvalds 20561da177e4SLinus Torvalds static int selinux_sb_kern_mount(struct super_block *sb, void *data) 20571da177e4SLinus Torvalds { 20581da177e4SLinus Torvalds struct avc_audit_data ad; 20591da177e4SLinus Torvalds int rc; 20601da177e4SLinus Torvalds 20611da177e4SLinus Torvalds rc = superblock_doinit(sb, data); 20621da177e4SLinus Torvalds if (rc) 20631da177e4SLinus Torvalds return rc; 20641da177e4SLinus Torvalds 20651da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,FS); 20661da177e4SLinus Torvalds ad.u.fs.dentry = sb->s_root; 20671da177e4SLinus Torvalds return superblock_has_perm(current, sb, FILESYSTEM__MOUNT, &ad); 20681da177e4SLinus Torvalds } 20691da177e4SLinus Torvalds 2070726c3342SDavid Howells static int selinux_sb_statfs(struct dentry *dentry) 20711da177e4SLinus Torvalds { 20721da177e4SLinus Torvalds struct avc_audit_data ad; 20731da177e4SLinus Torvalds 20741da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,FS); 2075726c3342SDavid Howells ad.u.fs.dentry = dentry->d_sb->s_root; 2076726c3342SDavid Howells return superblock_has_perm(current, dentry->d_sb, FILESYSTEM__GETATTR, &ad); 20771da177e4SLinus Torvalds } 20781da177e4SLinus Torvalds 20791da177e4SLinus Torvalds static int selinux_mount(char * dev_name, 20801da177e4SLinus Torvalds struct nameidata *nd, 20811da177e4SLinus Torvalds char * type, 20821da177e4SLinus Torvalds unsigned long flags, 20831da177e4SLinus Torvalds void * data) 20841da177e4SLinus Torvalds { 20851da177e4SLinus Torvalds int rc; 20861da177e4SLinus Torvalds 20871da177e4SLinus Torvalds rc = secondary_ops->sb_mount(dev_name, nd, type, flags, data); 20881da177e4SLinus Torvalds if (rc) 20891da177e4SLinus Torvalds return rc; 20901da177e4SLinus Torvalds 20911da177e4SLinus Torvalds if (flags & MS_REMOUNT) 20921da177e4SLinus Torvalds return superblock_has_perm(current, nd->mnt->mnt_sb, 20931da177e4SLinus Torvalds FILESYSTEM__REMOUNT, NULL); 20941da177e4SLinus Torvalds else 20951da177e4SLinus Torvalds return dentry_has_perm(current, nd->mnt, nd->dentry, 20961da177e4SLinus Torvalds FILE__MOUNTON); 20971da177e4SLinus Torvalds } 20981da177e4SLinus Torvalds 20991da177e4SLinus Torvalds static int selinux_umount(struct vfsmount *mnt, int flags) 21001da177e4SLinus Torvalds { 21011da177e4SLinus Torvalds int rc; 21021da177e4SLinus Torvalds 21031da177e4SLinus Torvalds rc = secondary_ops->sb_umount(mnt, flags); 21041da177e4SLinus Torvalds if (rc) 21051da177e4SLinus Torvalds return rc; 21061da177e4SLinus Torvalds 21071da177e4SLinus Torvalds return superblock_has_perm(current,mnt->mnt_sb, 21081da177e4SLinus Torvalds FILESYSTEM__UNMOUNT,NULL); 21091da177e4SLinus Torvalds } 21101da177e4SLinus Torvalds 21111da177e4SLinus Torvalds /* inode security operations */ 21121da177e4SLinus Torvalds 21131da177e4SLinus Torvalds static int selinux_inode_alloc_security(struct inode *inode) 21141da177e4SLinus Torvalds { 21151da177e4SLinus Torvalds return inode_alloc_security(inode); 21161da177e4SLinus Torvalds } 21171da177e4SLinus Torvalds 21181da177e4SLinus Torvalds static void selinux_inode_free_security(struct inode *inode) 21191da177e4SLinus Torvalds { 21201da177e4SLinus Torvalds inode_free_security(inode); 21211da177e4SLinus Torvalds } 21221da177e4SLinus Torvalds 21235e41ff9eSStephen Smalley static int selinux_inode_init_security(struct inode *inode, struct inode *dir, 21245e41ff9eSStephen Smalley char **name, void **value, 21255e41ff9eSStephen Smalley size_t *len) 21265e41ff9eSStephen Smalley { 21275e41ff9eSStephen Smalley struct task_security_struct *tsec; 21285e41ff9eSStephen Smalley struct inode_security_struct *dsec; 21295e41ff9eSStephen Smalley struct superblock_security_struct *sbsec; 2130570bc1c2SStephen Smalley u32 newsid, clen; 21315e41ff9eSStephen Smalley int rc; 2132570bc1c2SStephen Smalley char *namep = NULL, *context; 21335e41ff9eSStephen Smalley 21345e41ff9eSStephen Smalley tsec = current->security; 21355e41ff9eSStephen Smalley dsec = dir->i_security; 21365e41ff9eSStephen Smalley sbsec = dir->i_sb->s_security; 21375e41ff9eSStephen Smalley 21385e41ff9eSStephen Smalley if (tsec->create_sid && sbsec->behavior != SECURITY_FS_USE_MNTPOINT) { 21395e41ff9eSStephen Smalley newsid = tsec->create_sid; 21405e41ff9eSStephen Smalley } else { 21415e41ff9eSStephen Smalley rc = security_transition_sid(tsec->sid, dsec->sid, 21425e41ff9eSStephen Smalley inode_mode_to_security_class(inode->i_mode), 21435e41ff9eSStephen Smalley &newsid); 21445e41ff9eSStephen Smalley if (rc) { 21455e41ff9eSStephen Smalley printk(KERN_WARNING "%s: " 21465e41ff9eSStephen Smalley "security_transition_sid failed, rc=%d (dev=%s " 21475e41ff9eSStephen Smalley "ino=%ld)\n", 21485e41ff9eSStephen Smalley __FUNCTION__, 21495e41ff9eSStephen Smalley -rc, inode->i_sb->s_id, inode->i_ino); 21505e41ff9eSStephen Smalley return rc; 21515e41ff9eSStephen Smalley } 21525e41ff9eSStephen Smalley } 21535e41ff9eSStephen Smalley 2154296fddf7SEric Paris /* Possibly defer initialization to selinux_complete_init. */ 2155296fddf7SEric Paris if (sbsec->initialized) { 2156296fddf7SEric Paris struct inode_security_struct *isec = inode->i_security; 2157296fddf7SEric Paris isec->sclass = inode_mode_to_security_class(inode->i_mode); 2158296fddf7SEric Paris isec->sid = newsid; 2159296fddf7SEric Paris isec->initialized = 1; 2160296fddf7SEric Paris } 21615e41ff9eSStephen Smalley 21628aad3875SStephen Smalley if (!ss_initialized || sbsec->behavior == SECURITY_FS_USE_MNTPOINT) 216325a74f3bSStephen Smalley return -EOPNOTSUPP; 216425a74f3bSStephen Smalley 2165570bc1c2SStephen Smalley if (name) { 21665e41ff9eSStephen Smalley namep = kstrdup(XATTR_SELINUX_SUFFIX, GFP_KERNEL); 21675e41ff9eSStephen Smalley if (!namep) 21685e41ff9eSStephen Smalley return -ENOMEM; 21695e41ff9eSStephen Smalley *name = namep; 2170570bc1c2SStephen Smalley } 21715e41ff9eSStephen Smalley 2172570bc1c2SStephen Smalley if (value && len) { 2173570bc1c2SStephen Smalley rc = security_sid_to_context(newsid, &context, &clen); 21745e41ff9eSStephen Smalley if (rc) { 21755e41ff9eSStephen Smalley kfree(namep); 21765e41ff9eSStephen Smalley return rc; 21775e41ff9eSStephen Smalley } 21785e41ff9eSStephen Smalley *value = context; 2179570bc1c2SStephen Smalley *len = clen; 2180570bc1c2SStephen Smalley } 21815e41ff9eSStephen Smalley 21825e41ff9eSStephen Smalley return 0; 21835e41ff9eSStephen Smalley } 21845e41ff9eSStephen Smalley 21851da177e4SLinus Torvalds static int selinux_inode_create(struct inode *dir, struct dentry *dentry, int mask) 21861da177e4SLinus Torvalds { 21871da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_FILE); 21881da177e4SLinus Torvalds } 21891da177e4SLinus Torvalds 21901da177e4SLinus Torvalds static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 21911da177e4SLinus Torvalds { 21921da177e4SLinus Torvalds int rc; 21931da177e4SLinus Torvalds 21941da177e4SLinus Torvalds rc = secondary_ops->inode_link(old_dentry,dir,new_dentry); 21951da177e4SLinus Torvalds if (rc) 21961da177e4SLinus Torvalds return rc; 21971da177e4SLinus Torvalds return may_link(dir, old_dentry, MAY_LINK); 21981da177e4SLinus Torvalds } 21991da177e4SLinus Torvalds 22001da177e4SLinus Torvalds static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry) 22011da177e4SLinus Torvalds { 22021da177e4SLinus Torvalds int rc; 22031da177e4SLinus Torvalds 22041da177e4SLinus Torvalds rc = secondary_ops->inode_unlink(dir, dentry); 22051da177e4SLinus Torvalds if (rc) 22061da177e4SLinus Torvalds return rc; 22071da177e4SLinus Torvalds return may_link(dir, dentry, MAY_UNLINK); 22081da177e4SLinus Torvalds } 22091da177e4SLinus Torvalds 22101da177e4SLinus Torvalds static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name) 22111da177e4SLinus Torvalds { 22121da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_LNK_FILE); 22131da177e4SLinus Torvalds } 22141da177e4SLinus Torvalds 22151da177e4SLinus Torvalds static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, int mask) 22161da177e4SLinus Torvalds { 22171da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_DIR); 22181da177e4SLinus Torvalds } 22191da177e4SLinus Torvalds 22201da177e4SLinus Torvalds static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry) 22211da177e4SLinus Torvalds { 22221da177e4SLinus Torvalds return may_link(dir, dentry, MAY_RMDIR); 22231da177e4SLinus Torvalds } 22241da177e4SLinus Torvalds 22251da177e4SLinus Torvalds static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 22261da177e4SLinus Torvalds { 22271da177e4SLinus Torvalds int rc; 22281da177e4SLinus Torvalds 22291da177e4SLinus Torvalds rc = secondary_ops->inode_mknod(dir, dentry, mode, dev); 22301da177e4SLinus Torvalds if (rc) 22311da177e4SLinus Torvalds return rc; 22321da177e4SLinus Torvalds 22331da177e4SLinus Torvalds return may_create(dir, dentry, inode_mode_to_security_class(mode)); 22341da177e4SLinus Torvalds } 22351da177e4SLinus Torvalds 22361da177e4SLinus Torvalds static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry, 22371da177e4SLinus Torvalds struct inode *new_inode, struct dentry *new_dentry) 22381da177e4SLinus Torvalds { 22391da177e4SLinus Torvalds return may_rename(old_inode, old_dentry, new_inode, new_dentry); 22401da177e4SLinus Torvalds } 22411da177e4SLinus Torvalds 22421da177e4SLinus Torvalds static int selinux_inode_readlink(struct dentry *dentry) 22431da177e4SLinus Torvalds { 22441da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__READ); 22451da177e4SLinus Torvalds } 22461da177e4SLinus Torvalds 22471da177e4SLinus Torvalds static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata) 22481da177e4SLinus Torvalds { 22491da177e4SLinus Torvalds int rc; 22501da177e4SLinus Torvalds 22511da177e4SLinus Torvalds rc = secondary_ops->inode_follow_link(dentry,nameidata); 22521da177e4SLinus Torvalds if (rc) 22531da177e4SLinus Torvalds return rc; 22541da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__READ); 22551da177e4SLinus Torvalds } 22561da177e4SLinus Torvalds 22571da177e4SLinus Torvalds static int selinux_inode_permission(struct inode *inode, int mask, 22581da177e4SLinus Torvalds struct nameidata *nd) 22591da177e4SLinus Torvalds { 22601da177e4SLinus Torvalds int rc; 22611da177e4SLinus Torvalds 22621da177e4SLinus Torvalds rc = secondary_ops->inode_permission(inode, mask, nd); 22631da177e4SLinus Torvalds if (rc) 22641da177e4SLinus Torvalds return rc; 22651da177e4SLinus Torvalds 22661da177e4SLinus Torvalds if (!mask) { 22671da177e4SLinus Torvalds /* No permission to check. Existence test. */ 22681da177e4SLinus Torvalds return 0; 22691da177e4SLinus Torvalds } 22701da177e4SLinus Torvalds 22711da177e4SLinus Torvalds return inode_has_perm(current, inode, 22721da177e4SLinus Torvalds file_mask_to_av(inode->i_mode, mask), NULL); 22731da177e4SLinus Torvalds } 22741da177e4SLinus Torvalds 22751da177e4SLinus Torvalds static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr) 22761da177e4SLinus Torvalds { 22771da177e4SLinus Torvalds int rc; 22781da177e4SLinus Torvalds 22791da177e4SLinus Torvalds rc = secondary_ops->inode_setattr(dentry, iattr); 22801da177e4SLinus Torvalds if (rc) 22811da177e4SLinus Torvalds return rc; 22821da177e4SLinus Torvalds 22831da177e4SLinus Torvalds if (iattr->ia_valid & ATTR_FORCE) 22841da177e4SLinus Torvalds return 0; 22851da177e4SLinus Torvalds 22861da177e4SLinus Torvalds if (iattr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | 22871da177e4SLinus Torvalds ATTR_ATIME_SET | ATTR_MTIME_SET)) 22881da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__SETATTR); 22891da177e4SLinus Torvalds 22901da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__WRITE); 22911da177e4SLinus Torvalds } 22921da177e4SLinus Torvalds 22931da177e4SLinus Torvalds static int selinux_inode_getattr(struct vfsmount *mnt, struct dentry *dentry) 22941da177e4SLinus Torvalds { 22951da177e4SLinus Torvalds return dentry_has_perm(current, mnt, dentry, FILE__GETATTR); 22961da177e4SLinus Torvalds } 22971da177e4SLinus Torvalds 22981da177e4SLinus Torvalds static int selinux_inode_setxattr(struct dentry *dentry, char *name, void *value, size_t size, int flags) 22991da177e4SLinus Torvalds { 23001da177e4SLinus Torvalds struct task_security_struct *tsec = current->security; 23011da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 23021da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 23031da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 23041da177e4SLinus Torvalds struct avc_audit_data ad; 23051da177e4SLinus Torvalds u32 newsid; 23061da177e4SLinus Torvalds int rc = 0; 23071da177e4SLinus Torvalds 23081da177e4SLinus Torvalds if (strcmp(name, XATTR_NAME_SELINUX)) { 23091da177e4SLinus Torvalds if (!strncmp(name, XATTR_SECURITY_PREFIX, 23101da177e4SLinus Torvalds sizeof XATTR_SECURITY_PREFIX - 1) && 23111da177e4SLinus Torvalds !capable(CAP_SYS_ADMIN)) { 23121da177e4SLinus Torvalds /* A different attribute in the security namespace. 23131da177e4SLinus Torvalds Restrict to administrator. */ 23141da177e4SLinus Torvalds return -EPERM; 23151da177e4SLinus Torvalds } 23161da177e4SLinus Torvalds 23171da177e4SLinus Torvalds /* Not an attribute we recognize, so just check the 23181da177e4SLinus Torvalds ordinary setattr permission. */ 23191da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__SETATTR); 23201da177e4SLinus Torvalds } 23211da177e4SLinus Torvalds 23221da177e4SLinus Torvalds sbsec = inode->i_sb->s_security; 23231da177e4SLinus Torvalds if (sbsec->behavior == SECURITY_FS_USE_MNTPOINT) 23241da177e4SLinus Torvalds return -EOPNOTSUPP; 23251da177e4SLinus Torvalds 23263bd858abSSatyam Sharma if (!is_owner_or_cap(inode)) 23271da177e4SLinus Torvalds return -EPERM; 23281da177e4SLinus Torvalds 23291da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,FS); 23301da177e4SLinus Torvalds ad.u.fs.dentry = dentry; 23311da177e4SLinus Torvalds 23321da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, isec->sclass, 23331da177e4SLinus Torvalds FILE__RELABELFROM, &ad); 23341da177e4SLinus Torvalds if (rc) 23351da177e4SLinus Torvalds return rc; 23361da177e4SLinus Torvalds 23371da177e4SLinus Torvalds rc = security_context_to_sid(value, size, &newsid); 23381da177e4SLinus Torvalds if (rc) 23391da177e4SLinus Torvalds return rc; 23401da177e4SLinus Torvalds 23411da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, newsid, isec->sclass, 23421da177e4SLinus Torvalds FILE__RELABELTO, &ad); 23431da177e4SLinus Torvalds if (rc) 23441da177e4SLinus Torvalds return rc; 23451da177e4SLinus Torvalds 23461da177e4SLinus Torvalds rc = security_validate_transition(isec->sid, newsid, tsec->sid, 23471da177e4SLinus Torvalds isec->sclass); 23481da177e4SLinus Torvalds if (rc) 23491da177e4SLinus Torvalds return rc; 23501da177e4SLinus Torvalds 23511da177e4SLinus Torvalds return avc_has_perm(newsid, 23521da177e4SLinus Torvalds sbsec->sid, 23531da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 23541da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, 23551da177e4SLinus Torvalds &ad); 23561da177e4SLinus Torvalds } 23571da177e4SLinus Torvalds 23581da177e4SLinus Torvalds static void selinux_inode_post_setxattr(struct dentry *dentry, char *name, 23591da177e4SLinus Torvalds void *value, size_t size, int flags) 23601da177e4SLinus Torvalds { 23611da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 23621da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 23631da177e4SLinus Torvalds u32 newsid; 23641da177e4SLinus Torvalds int rc; 23651da177e4SLinus Torvalds 23661da177e4SLinus Torvalds if (strcmp(name, XATTR_NAME_SELINUX)) { 23671da177e4SLinus Torvalds /* Not an attribute we recognize, so nothing to do. */ 23681da177e4SLinus Torvalds return; 23691da177e4SLinus Torvalds } 23701da177e4SLinus Torvalds 23711da177e4SLinus Torvalds rc = security_context_to_sid(value, size, &newsid); 23721da177e4SLinus Torvalds if (rc) { 23731da177e4SLinus Torvalds printk(KERN_WARNING "%s: unable to obtain SID for context " 23741da177e4SLinus Torvalds "%s, rc=%d\n", __FUNCTION__, (char*)value, -rc); 23751da177e4SLinus Torvalds return; 23761da177e4SLinus Torvalds } 23771da177e4SLinus Torvalds 23781da177e4SLinus Torvalds isec->sid = newsid; 23791da177e4SLinus Torvalds return; 23801da177e4SLinus Torvalds } 23811da177e4SLinus Torvalds 23821da177e4SLinus Torvalds static int selinux_inode_getxattr (struct dentry *dentry, char *name) 23831da177e4SLinus Torvalds { 23841da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__GETATTR); 23851da177e4SLinus Torvalds } 23861da177e4SLinus Torvalds 23871da177e4SLinus Torvalds static int selinux_inode_listxattr (struct dentry *dentry) 23881da177e4SLinus Torvalds { 23891da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__GETATTR); 23901da177e4SLinus Torvalds } 23911da177e4SLinus Torvalds 23921da177e4SLinus Torvalds static int selinux_inode_removexattr (struct dentry *dentry, char *name) 23931da177e4SLinus Torvalds { 23941da177e4SLinus Torvalds if (strcmp(name, XATTR_NAME_SELINUX)) { 23951da177e4SLinus Torvalds if (!strncmp(name, XATTR_SECURITY_PREFIX, 23961da177e4SLinus Torvalds sizeof XATTR_SECURITY_PREFIX - 1) && 23971da177e4SLinus Torvalds !capable(CAP_SYS_ADMIN)) { 23981da177e4SLinus Torvalds /* A different attribute in the security namespace. 23991da177e4SLinus Torvalds Restrict to administrator. */ 24001da177e4SLinus Torvalds return -EPERM; 24011da177e4SLinus Torvalds } 24021da177e4SLinus Torvalds 24031da177e4SLinus Torvalds /* Not an attribute we recognize, so just check the 24041da177e4SLinus Torvalds ordinary setattr permission. Might want a separate 24051da177e4SLinus Torvalds permission for removexattr. */ 24061da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__SETATTR); 24071da177e4SLinus Torvalds } 24081da177e4SLinus Torvalds 24091da177e4SLinus Torvalds /* No one is allowed to remove a SELinux security label. 24101da177e4SLinus Torvalds You can change the label, but all data must be labeled. */ 24111da177e4SLinus Torvalds return -EACCES; 24121da177e4SLinus Torvalds } 24131da177e4SLinus Torvalds 24148c8570fbSDustin Kirkland static const char *selinux_inode_xattr_getsuffix(void) 24158c8570fbSDustin Kirkland { 24168c8570fbSDustin Kirkland return XATTR_SELINUX_SUFFIX; 24178c8570fbSDustin Kirkland } 24188c8570fbSDustin Kirkland 2419d381d8a9SJames Morris /* 2420d381d8a9SJames Morris * Copy the in-core inode security context value to the user. If the 2421d381d8a9SJames Morris * getxattr() prior to this succeeded, check to see if we need to 2422d381d8a9SJames Morris * canonicalize the value to be finally returned to the user. 2423d381d8a9SJames Morris * 2424d381d8a9SJames Morris * Permission check is handled by selinux_inode_getxattr hook. 2425d381d8a9SJames Morris */ 24267306a0b9SDustin Kirkland static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void *buffer, size_t size, int err) 24271da177e4SLinus Torvalds { 24281da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 24291da177e4SLinus Torvalds 24308c8570fbSDustin Kirkland if (strcmp(name, XATTR_SELINUX_SUFFIX)) 24318c8570fbSDustin Kirkland return -EOPNOTSUPP; 24321da177e4SLinus Torvalds 24338c8570fbSDustin Kirkland return selinux_getsecurity(isec->sid, buffer, size); 24341da177e4SLinus Torvalds } 24351da177e4SLinus Torvalds 24361da177e4SLinus Torvalds static int selinux_inode_setsecurity(struct inode *inode, const char *name, 24371da177e4SLinus Torvalds const void *value, size_t size, int flags) 24381da177e4SLinus Torvalds { 24391da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 24401da177e4SLinus Torvalds u32 newsid; 24411da177e4SLinus Torvalds int rc; 24421da177e4SLinus Torvalds 24431da177e4SLinus Torvalds if (strcmp(name, XATTR_SELINUX_SUFFIX)) 24441da177e4SLinus Torvalds return -EOPNOTSUPP; 24451da177e4SLinus Torvalds 24461da177e4SLinus Torvalds if (!value || !size) 24471da177e4SLinus Torvalds return -EACCES; 24481da177e4SLinus Torvalds 24491da177e4SLinus Torvalds rc = security_context_to_sid((void*)value, size, &newsid); 24501da177e4SLinus Torvalds if (rc) 24511da177e4SLinus Torvalds return rc; 24521da177e4SLinus Torvalds 24531da177e4SLinus Torvalds isec->sid = newsid; 24541da177e4SLinus Torvalds return 0; 24551da177e4SLinus Torvalds } 24561da177e4SLinus Torvalds 24571da177e4SLinus Torvalds static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size) 24581da177e4SLinus Torvalds { 24591da177e4SLinus Torvalds const int len = sizeof(XATTR_NAME_SELINUX); 24601da177e4SLinus Torvalds if (buffer && len <= buffer_size) 24611da177e4SLinus Torvalds memcpy(buffer, XATTR_NAME_SELINUX, len); 24621da177e4SLinus Torvalds return len; 24631da177e4SLinus Torvalds } 24641da177e4SLinus Torvalds 24651da177e4SLinus Torvalds /* file security operations */ 24661da177e4SLinus Torvalds 24671da177e4SLinus Torvalds static int selinux_file_permission(struct file *file, int mask) 24681da177e4SLinus Torvalds { 24697420ed23SVenkat Yekkirala int rc; 24703d5ff529SJosef Sipek struct inode *inode = file->f_path.dentry->d_inode; 24711da177e4SLinus Torvalds 24721da177e4SLinus Torvalds if (!mask) { 24731da177e4SLinus Torvalds /* No permission to check. Existence test. */ 24741da177e4SLinus Torvalds return 0; 24751da177e4SLinus Torvalds } 24761da177e4SLinus Torvalds 24771da177e4SLinus Torvalds /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */ 24781da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE)) 24791da177e4SLinus Torvalds mask |= MAY_APPEND; 24801da177e4SLinus Torvalds 24817420ed23SVenkat Yekkirala rc = file_has_perm(current, file, 24821da177e4SLinus Torvalds file_mask_to_av(inode->i_mode, mask)); 24837420ed23SVenkat Yekkirala if (rc) 24847420ed23SVenkat Yekkirala return rc; 24857420ed23SVenkat Yekkirala 24867420ed23SVenkat Yekkirala return selinux_netlbl_inode_permission(inode, mask); 24871da177e4SLinus Torvalds } 24881da177e4SLinus Torvalds 24891da177e4SLinus Torvalds static int selinux_file_alloc_security(struct file *file) 24901da177e4SLinus Torvalds { 24911da177e4SLinus Torvalds return file_alloc_security(file); 24921da177e4SLinus Torvalds } 24931da177e4SLinus Torvalds 24941da177e4SLinus Torvalds static void selinux_file_free_security(struct file *file) 24951da177e4SLinus Torvalds { 24961da177e4SLinus Torvalds file_free_security(file); 24971da177e4SLinus Torvalds } 24981da177e4SLinus Torvalds 24991da177e4SLinus Torvalds static int selinux_file_ioctl(struct file *file, unsigned int cmd, 25001da177e4SLinus Torvalds unsigned long arg) 25011da177e4SLinus Torvalds { 25021da177e4SLinus Torvalds int error = 0; 25031da177e4SLinus Torvalds 25041da177e4SLinus Torvalds switch (cmd) { 25051da177e4SLinus Torvalds case FIONREAD: 25061da177e4SLinus Torvalds /* fall through */ 25071da177e4SLinus Torvalds case FIBMAP: 25081da177e4SLinus Torvalds /* fall through */ 25091da177e4SLinus Torvalds case FIGETBSZ: 25101da177e4SLinus Torvalds /* fall through */ 25111da177e4SLinus Torvalds case EXT2_IOC_GETFLAGS: 25121da177e4SLinus Torvalds /* fall through */ 25131da177e4SLinus Torvalds case EXT2_IOC_GETVERSION: 25141da177e4SLinus Torvalds error = file_has_perm(current, file, FILE__GETATTR); 25151da177e4SLinus Torvalds break; 25161da177e4SLinus Torvalds 25171da177e4SLinus Torvalds case EXT2_IOC_SETFLAGS: 25181da177e4SLinus Torvalds /* fall through */ 25191da177e4SLinus Torvalds case EXT2_IOC_SETVERSION: 25201da177e4SLinus Torvalds error = file_has_perm(current, file, FILE__SETATTR); 25211da177e4SLinus Torvalds break; 25221da177e4SLinus Torvalds 25231da177e4SLinus Torvalds /* sys_ioctl() checks */ 25241da177e4SLinus Torvalds case FIONBIO: 25251da177e4SLinus Torvalds /* fall through */ 25261da177e4SLinus Torvalds case FIOASYNC: 25271da177e4SLinus Torvalds error = file_has_perm(current, file, 0); 25281da177e4SLinus Torvalds break; 25291da177e4SLinus Torvalds 25301da177e4SLinus Torvalds case KDSKBENT: 25311da177e4SLinus Torvalds case KDSKBSENT: 25321da177e4SLinus Torvalds error = task_has_capability(current,CAP_SYS_TTY_CONFIG); 25331da177e4SLinus Torvalds break; 25341da177e4SLinus Torvalds 25351da177e4SLinus Torvalds /* default case assumes that the command will go 25361da177e4SLinus Torvalds * to the file's ioctl() function. 25371da177e4SLinus Torvalds */ 25381da177e4SLinus Torvalds default: 25391da177e4SLinus Torvalds error = file_has_perm(current, file, FILE__IOCTL); 25401da177e4SLinus Torvalds 25411da177e4SLinus Torvalds } 25421da177e4SLinus Torvalds return error; 25431da177e4SLinus Torvalds } 25441da177e4SLinus Torvalds 25451da177e4SLinus Torvalds static int file_map_prot_check(struct file *file, unsigned long prot, int shared) 25461da177e4SLinus Torvalds { 25471da177e4SLinus Torvalds #ifndef CONFIG_PPC32 25481da177e4SLinus Torvalds if ((prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) { 25491da177e4SLinus Torvalds /* 25501da177e4SLinus Torvalds * We are making executable an anonymous mapping or a 25511da177e4SLinus Torvalds * private file mapping that will also be writable. 25521da177e4SLinus Torvalds * This has an additional check. 25531da177e4SLinus Torvalds */ 25541da177e4SLinus Torvalds int rc = task_has_perm(current, current, PROCESS__EXECMEM); 25551da177e4SLinus Torvalds if (rc) 25561da177e4SLinus Torvalds return rc; 25571da177e4SLinus Torvalds } 25581da177e4SLinus Torvalds #endif 25591da177e4SLinus Torvalds 25601da177e4SLinus Torvalds if (file) { 25611da177e4SLinus Torvalds /* read access is always possible with a mapping */ 25621da177e4SLinus Torvalds u32 av = FILE__READ; 25631da177e4SLinus Torvalds 25641da177e4SLinus Torvalds /* write access only matters if the mapping is shared */ 25651da177e4SLinus Torvalds if (shared && (prot & PROT_WRITE)) 25661da177e4SLinus Torvalds av |= FILE__WRITE; 25671da177e4SLinus Torvalds 25681da177e4SLinus Torvalds if (prot & PROT_EXEC) 25691da177e4SLinus Torvalds av |= FILE__EXECUTE; 25701da177e4SLinus Torvalds 25711da177e4SLinus Torvalds return file_has_perm(current, file, av); 25721da177e4SLinus Torvalds } 25731da177e4SLinus Torvalds return 0; 25741da177e4SLinus Torvalds } 25751da177e4SLinus Torvalds 25761da177e4SLinus Torvalds static int selinux_file_mmap(struct file *file, unsigned long reqprot, 2577ed032189SEric Paris unsigned long prot, unsigned long flags, 2578ed032189SEric Paris unsigned long addr, unsigned long addr_only) 25791da177e4SLinus Torvalds { 2580ed032189SEric Paris int rc = 0; 2581ed032189SEric Paris u32 sid = ((struct task_security_struct*)(current->security))->sid; 25821da177e4SLinus Torvalds 2583ed032189SEric Paris if (addr < mmap_min_addr) 2584ed032189SEric Paris rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT, 2585ed032189SEric Paris MEMPROTECT__MMAP_ZERO, NULL); 2586ed032189SEric Paris if (rc || addr_only) 25871da177e4SLinus Torvalds return rc; 25881da177e4SLinus Torvalds 25891da177e4SLinus Torvalds if (selinux_checkreqprot) 25901da177e4SLinus Torvalds prot = reqprot; 25911da177e4SLinus Torvalds 25921da177e4SLinus Torvalds return file_map_prot_check(file, prot, 25931da177e4SLinus Torvalds (flags & MAP_TYPE) == MAP_SHARED); 25941da177e4SLinus Torvalds } 25951da177e4SLinus Torvalds 25961da177e4SLinus Torvalds static int selinux_file_mprotect(struct vm_area_struct *vma, 25971da177e4SLinus Torvalds unsigned long reqprot, 25981da177e4SLinus Torvalds unsigned long prot) 25991da177e4SLinus Torvalds { 26001da177e4SLinus Torvalds int rc; 26011da177e4SLinus Torvalds 26021da177e4SLinus Torvalds rc = secondary_ops->file_mprotect(vma, reqprot, prot); 26031da177e4SLinus Torvalds if (rc) 26041da177e4SLinus Torvalds return rc; 26051da177e4SLinus Torvalds 26061da177e4SLinus Torvalds if (selinux_checkreqprot) 26071da177e4SLinus Torvalds prot = reqprot; 26081da177e4SLinus Torvalds 26091da177e4SLinus Torvalds #ifndef CONFIG_PPC32 2610db4c9641SStephen Smalley if ((prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) { 2611db4c9641SStephen Smalley rc = 0; 2612db4c9641SStephen Smalley if (vma->vm_start >= vma->vm_mm->start_brk && 2613db4c9641SStephen Smalley vma->vm_end <= vma->vm_mm->brk) { 2614db4c9641SStephen Smalley rc = task_has_perm(current, current, 2615db4c9641SStephen Smalley PROCESS__EXECHEAP); 2616db4c9641SStephen Smalley } else if (!vma->vm_file && 26176b992197SLorenzo Hernandez García-Hierro vma->vm_start <= vma->vm_mm->start_stack && 26186b992197SLorenzo Hernandez García-Hierro vma->vm_end >= vma->vm_mm->start_stack) { 26196b992197SLorenzo Hernandez García-Hierro rc = task_has_perm(current, current, PROCESS__EXECSTACK); 2620db4c9641SStephen Smalley } else if (vma->vm_file && vma->anon_vma) { 2621db4c9641SStephen Smalley /* 2622db4c9641SStephen Smalley * We are making executable a file mapping that has 2623db4c9641SStephen Smalley * had some COW done. Since pages might have been 2624db4c9641SStephen Smalley * written, check ability to execute the possibly 2625db4c9641SStephen Smalley * modified content. This typically should only 2626db4c9641SStephen Smalley * occur for text relocations. 2627db4c9641SStephen Smalley */ 2628db4c9641SStephen Smalley rc = file_has_perm(current, vma->vm_file, 2629db4c9641SStephen Smalley FILE__EXECMOD); 2630db4c9641SStephen Smalley } 26316b992197SLorenzo Hernandez García-Hierro if (rc) 26326b992197SLorenzo Hernandez García-Hierro return rc; 26336b992197SLorenzo Hernandez García-Hierro } 26341da177e4SLinus Torvalds #endif 26351da177e4SLinus Torvalds 26361da177e4SLinus Torvalds return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED); 26371da177e4SLinus Torvalds } 26381da177e4SLinus Torvalds 26391da177e4SLinus Torvalds static int selinux_file_lock(struct file *file, unsigned int cmd) 26401da177e4SLinus Torvalds { 26411da177e4SLinus Torvalds return file_has_perm(current, file, FILE__LOCK); 26421da177e4SLinus Torvalds } 26431da177e4SLinus Torvalds 26441da177e4SLinus Torvalds static int selinux_file_fcntl(struct file *file, unsigned int cmd, 26451da177e4SLinus Torvalds unsigned long arg) 26461da177e4SLinus Torvalds { 26471da177e4SLinus Torvalds int err = 0; 26481da177e4SLinus Torvalds 26491da177e4SLinus Torvalds switch (cmd) { 26501da177e4SLinus Torvalds case F_SETFL: 26513d5ff529SJosef Sipek if (!file->f_path.dentry || !file->f_path.dentry->d_inode) { 26521da177e4SLinus Torvalds err = -EINVAL; 26531da177e4SLinus Torvalds break; 26541da177e4SLinus Torvalds } 26551da177e4SLinus Torvalds 26561da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) { 26571da177e4SLinus Torvalds err = file_has_perm(current, file,FILE__WRITE); 26581da177e4SLinus Torvalds break; 26591da177e4SLinus Torvalds } 26601da177e4SLinus Torvalds /* fall through */ 26611da177e4SLinus Torvalds case F_SETOWN: 26621da177e4SLinus Torvalds case F_SETSIG: 26631da177e4SLinus Torvalds case F_GETFL: 26641da177e4SLinus Torvalds case F_GETOWN: 26651da177e4SLinus Torvalds case F_GETSIG: 26661da177e4SLinus Torvalds /* Just check FD__USE permission */ 26671da177e4SLinus Torvalds err = file_has_perm(current, file, 0); 26681da177e4SLinus Torvalds break; 26691da177e4SLinus Torvalds case F_GETLK: 26701da177e4SLinus Torvalds case F_SETLK: 26711da177e4SLinus Torvalds case F_SETLKW: 26721da177e4SLinus Torvalds #if BITS_PER_LONG == 32 26731da177e4SLinus Torvalds case F_GETLK64: 26741da177e4SLinus Torvalds case F_SETLK64: 26751da177e4SLinus Torvalds case F_SETLKW64: 26761da177e4SLinus Torvalds #endif 26773d5ff529SJosef Sipek if (!file->f_path.dentry || !file->f_path.dentry->d_inode) { 26781da177e4SLinus Torvalds err = -EINVAL; 26791da177e4SLinus Torvalds break; 26801da177e4SLinus Torvalds } 26811da177e4SLinus Torvalds err = file_has_perm(current, file, FILE__LOCK); 26821da177e4SLinus Torvalds break; 26831da177e4SLinus Torvalds } 26841da177e4SLinus Torvalds 26851da177e4SLinus Torvalds return err; 26861da177e4SLinus Torvalds } 26871da177e4SLinus Torvalds 26881da177e4SLinus Torvalds static int selinux_file_set_fowner(struct file *file) 26891da177e4SLinus Torvalds { 26901da177e4SLinus Torvalds struct task_security_struct *tsec; 26911da177e4SLinus Torvalds struct file_security_struct *fsec; 26921da177e4SLinus Torvalds 26931da177e4SLinus Torvalds tsec = current->security; 26941da177e4SLinus Torvalds fsec = file->f_security; 26951da177e4SLinus Torvalds fsec->fown_sid = tsec->sid; 26961da177e4SLinus Torvalds 26971da177e4SLinus Torvalds return 0; 26981da177e4SLinus Torvalds } 26991da177e4SLinus Torvalds 27001da177e4SLinus Torvalds static int selinux_file_send_sigiotask(struct task_struct *tsk, 27011da177e4SLinus Torvalds struct fown_struct *fown, int signum) 27021da177e4SLinus Torvalds { 27031da177e4SLinus Torvalds struct file *file; 27041da177e4SLinus Torvalds u32 perm; 27051da177e4SLinus Torvalds struct task_security_struct *tsec; 27061da177e4SLinus Torvalds struct file_security_struct *fsec; 27071da177e4SLinus Torvalds 27081da177e4SLinus Torvalds /* struct fown_struct is never outside the context of a struct file */ 2709b385a144SRobert P. J. Day file = container_of(fown, struct file, f_owner); 27101da177e4SLinus Torvalds 27111da177e4SLinus Torvalds tsec = tsk->security; 27121da177e4SLinus Torvalds fsec = file->f_security; 27131da177e4SLinus Torvalds 27141da177e4SLinus Torvalds if (!signum) 27151da177e4SLinus Torvalds perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */ 27161da177e4SLinus Torvalds else 27171da177e4SLinus Torvalds perm = signal_to_av(signum); 27181da177e4SLinus Torvalds 27191da177e4SLinus Torvalds return avc_has_perm(fsec->fown_sid, tsec->sid, 27201da177e4SLinus Torvalds SECCLASS_PROCESS, perm, NULL); 27211da177e4SLinus Torvalds } 27221da177e4SLinus Torvalds 27231da177e4SLinus Torvalds static int selinux_file_receive(struct file *file) 27241da177e4SLinus Torvalds { 27251da177e4SLinus Torvalds return file_has_perm(current, file, file_to_av(file)); 27261da177e4SLinus Torvalds } 27271da177e4SLinus Torvalds 27281da177e4SLinus Torvalds /* task security operations */ 27291da177e4SLinus Torvalds 27301da177e4SLinus Torvalds static int selinux_task_create(unsigned long clone_flags) 27311da177e4SLinus Torvalds { 27321da177e4SLinus Torvalds int rc; 27331da177e4SLinus Torvalds 27341da177e4SLinus Torvalds rc = secondary_ops->task_create(clone_flags); 27351da177e4SLinus Torvalds if (rc) 27361da177e4SLinus Torvalds return rc; 27371da177e4SLinus Torvalds 27381da177e4SLinus Torvalds return task_has_perm(current, current, PROCESS__FORK); 27391da177e4SLinus Torvalds } 27401da177e4SLinus Torvalds 27411da177e4SLinus Torvalds static int selinux_task_alloc_security(struct task_struct *tsk) 27421da177e4SLinus Torvalds { 27431da177e4SLinus Torvalds struct task_security_struct *tsec1, *tsec2; 27441da177e4SLinus Torvalds int rc; 27451da177e4SLinus Torvalds 27461da177e4SLinus Torvalds tsec1 = current->security; 27471da177e4SLinus Torvalds 27481da177e4SLinus Torvalds rc = task_alloc_security(tsk); 27491da177e4SLinus Torvalds if (rc) 27501da177e4SLinus Torvalds return rc; 27511da177e4SLinus Torvalds tsec2 = tsk->security; 27521da177e4SLinus Torvalds 27531da177e4SLinus Torvalds tsec2->osid = tsec1->osid; 27541da177e4SLinus Torvalds tsec2->sid = tsec1->sid; 27551da177e4SLinus Torvalds 275628eba5bfSMichael LeMay /* Retain the exec, fs, key, and sock SIDs across fork */ 27571da177e4SLinus Torvalds tsec2->exec_sid = tsec1->exec_sid; 27581da177e4SLinus Torvalds tsec2->create_sid = tsec1->create_sid; 275928eba5bfSMichael LeMay tsec2->keycreate_sid = tsec1->keycreate_sid; 276042c3e03eSEric Paris tsec2->sockcreate_sid = tsec1->sockcreate_sid; 27611da177e4SLinus Torvalds 27621da177e4SLinus Torvalds /* Retain ptracer SID across fork, if any. 27631da177e4SLinus Torvalds This will be reset by the ptrace hook upon any 27641da177e4SLinus Torvalds subsequent ptrace_attach operations. */ 27651da177e4SLinus Torvalds tsec2->ptrace_sid = tsec1->ptrace_sid; 27661da177e4SLinus Torvalds 27671da177e4SLinus Torvalds return 0; 27681da177e4SLinus Torvalds } 27691da177e4SLinus Torvalds 27701da177e4SLinus Torvalds static void selinux_task_free_security(struct task_struct *tsk) 27711da177e4SLinus Torvalds { 27721da177e4SLinus Torvalds task_free_security(tsk); 27731da177e4SLinus Torvalds } 27741da177e4SLinus Torvalds 27751da177e4SLinus Torvalds static int selinux_task_setuid(uid_t id0, uid_t id1, uid_t id2, int flags) 27761da177e4SLinus Torvalds { 27771da177e4SLinus Torvalds /* Since setuid only affects the current process, and 27781da177e4SLinus Torvalds since the SELinux controls are not based on the Linux 27791da177e4SLinus Torvalds identity attributes, SELinux does not need to control 27801da177e4SLinus Torvalds this operation. However, SELinux does control the use 27811da177e4SLinus Torvalds of the CAP_SETUID and CAP_SETGID capabilities using the 27821da177e4SLinus Torvalds capable hook. */ 27831da177e4SLinus Torvalds return 0; 27841da177e4SLinus Torvalds } 27851da177e4SLinus Torvalds 27861da177e4SLinus Torvalds static int selinux_task_post_setuid(uid_t id0, uid_t id1, uid_t id2, int flags) 27871da177e4SLinus Torvalds { 27881da177e4SLinus Torvalds return secondary_ops->task_post_setuid(id0,id1,id2,flags); 27891da177e4SLinus Torvalds } 27901da177e4SLinus Torvalds 27911da177e4SLinus Torvalds static int selinux_task_setgid(gid_t id0, gid_t id1, gid_t id2, int flags) 27921da177e4SLinus Torvalds { 27931da177e4SLinus Torvalds /* See the comment for setuid above. */ 27941da177e4SLinus Torvalds return 0; 27951da177e4SLinus Torvalds } 27961da177e4SLinus Torvalds 27971da177e4SLinus Torvalds static int selinux_task_setpgid(struct task_struct *p, pid_t pgid) 27981da177e4SLinus Torvalds { 27991da177e4SLinus Torvalds return task_has_perm(current, p, PROCESS__SETPGID); 28001da177e4SLinus Torvalds } 28011da177e4SLinus Torvalds 28021da177e4SLinus Torvalds static int selinux_task_getpgid(struct task_struct *p) 28031da177e4SLinus Torvalds { 28041da177e4SLinus Torvalds return task_has_perm(current, p, PROCESS__GETPGID); 28051da177e4SLinus Torvalds } 28061da177e4SLinus Torvalds 28071da177e4SLinus Torvalds static int selinux_task_getsid(struct task_struct *p) 28081da177e4SLinus Torvalds { 28091da177e4SLinus Torvalds return task_has_perm(current, p, PROCESS__GETSESSION); 28101da177e4SLinus Torvalds } 28111da177e4SLinus Torvalds 2812f9008e4cSDavid Quigley static void selinux_task_getsecid(struct task_struct *p, u32 *secid) 2813f9008e4cSDavid Quigley { 2814f9008e4cSDavid Quigley selinux_get_task_sid(p, secid); 2815f9008e4cSDavid Quigley } 2816f9008e4cSDavid Quigley 28171da177e4SLinus Torvalds static int selinux_task_setgroups(struct group_info *group_info) 28181da177e4SLinus Torvalds { 28191da177e4SLinus Torvalds /* See the comment for setuid above. */ 28201da177e4SLinus Torvalds return 0; 28211da177e4SLinus Torvalds } 28221da177e4SLinus Torvalds 28231da177e4SLinus Torvalds static int selinux_task_setnice(struct task_struct *p, int nice) 28241da177e4SLinus Torvalds { 28251da177e4SLinus Torvalds int rc; 28261da177e4SLinus Torvalds 28271da177e4SLinus Torvalds rc = secondary_ops->task_setnice(p, nice); 28281da177e4SLinus Torvalds if (rc) 28291da177e4SLinus Torvalds return rc; 28301da177e4SLinus Torvalds 28311da177e4SLinus Torvalds return task_has_perm(current,p, PROCESS__SETSCHED); 28321da177e4SLinus Torvalds } 28331da177e4SLinus Torvalds 283403e68060SJames Morris static int selinux_task_setioprio(struct task_struct *p, int ioprio) 283503e68060SJames Morris { 283603e68060SJames Morris return task_has_perm(current, p, PROCESS__SETSCHED); 283703e68060SJames Morris } 283803e68060SJames Morris 2839a1836a42SDavid Quigley static int selinux_task_getioprio(struct task_struct *p) 2840a1836a42SDavid Quigley { 2841a1836a42SDavid Quigley return task_has_perm(current, p, PROCESS__GETSCHED); 2842a1836a42SDavid Quigley } 2843a1836a42SDavid Quigley 28441da177e4SLinus Torvalds static int selinux_task_setrlimit(unsigned int resource, struct rlimit *new_rlim) 28451da177e4SLinus Torvalds { 28461da177e4SLinus Torvalds struct rlimit *old_rlim = current->signal->rlim + resource; 28471da177e4SLinus Torvalds int rc; 28481da177e4SLinus Torvalds 28491da177e4SLinus Torvalds rc = secondary_ops->task_setrlimit(resource, new_rlim); 28501da177e4SLinus Torvalds if (rc) 28511da177e4SLinus Torvalds return rc; 28521da177e4SLinus Torvalds 28531da177e4SLinus Torvalds /* Control the ability to change the hard limit (whether 28541da177e4SLinus Torvalds lowering or raising it), so that the hard limit can 28551da177e4SLinus Torvalds later be used as a safe reset point for the soft limit 28561da177e4SLinus Torvalds upon context transitions. See selinux_bprm_apply_creds. */ 28571da177e4SLinus Torvalds if (old_rlim->rlim_max != new_rlim->rlim_max) 28581da177e4SLinus Torvalds return task_has_perm(current, current, PROCESS__SETRLIMIT); 28591da177e4SLinus Torvalds 28601da177e4SLinus Torvalds return 0; 28611da177e4SLinus Torvalds } 28621da177e4SLinus Torvalds 28631da177e4SLinus Torvalds static int selinux_task_setscheduler(struct task_struct *p, int policy, struct sched_param *lp) 28641da177e4SLinus Torvalds { 28651da177e4SLinus Torvalds return task_has_perm(current, p, PROCESS__SETSCHED); 28661da177e4SLinus Torvalds } 28671da177e4SLinus Torvalds 28681da177e4SLinus Torvalds static int selinux_task_getscheduler(struct task_struct *p) 28691da177e4SLinus Torvalds { 28701da177e4SLinus Torvalds return task_has_perm(current, p, PROCESS__GETSCHED); 28711da177e4SLinus Torvalds } 28721da177e4SLinus Torvalds 287335601547SDavid Quigley static int selinux_task_movememory(struct task_struct *p) 287435601547SDavid Quigley { 287535601547SDavid Quigley return task_has_perm(current, p, PROCESS__SETSCHED); 287635601547SDavid Quigley } 287735601547SDavid Quigley 2878f9008e4cSDavid Quigley static int selinux_task_kill(struct task_struct *p, struct siginfo *info, 2879f9008e4cSDavid Quigley int sig, u32 secid) 28801da177e4SLinus Torvalds { 28811da177e4SLinus Torvalds u32 perm; 28821da177e4SLinus Torvalds int rc; 2883f9008e4cSDavid Quigley struct task_security_struct *tsec; 28841da177e4SLinus Torvalds 2885f9008e4cSDavid Quigley rc = secondary_ops->task_kill(p, info, sig, secid); 28861da177e4SLinus Torvalds if (rc) 28871da177e4SLinus Torvalds return rc; 28881da177e4SLinus Torvalds 2889621d3121SOleg Nesterov if (info != SEND_SIG_NOINFO && (is_si_special(info) || SI_FROMKERNEL(info))) 28901da177e4SLinus Torvalds return 0; 28911da177e4SLinus Torvalds 28921da177e4SLinus Torvalds if (!sig) 28931da177e4SLinus Torvalds perm = PROCESS__SIGNULL; /* null signal; existence test */ 28941da177e4SLinus Torvalds else 28951da177e4SLinus Torvalds perm = signal_to_av(sig); 2896f9008e4cSDavid Quigley tsec = p->security; 2897f9008e4cSDavid Quigley if (secid) 2898f9008e4cSDavid Quigley rc = avc_has_perm(secid, tsec->sid, SECCLASS_PROCESS, perm, NULL); 2899f9008e4cSDavid Quigley else 2900f9008e4cSDavid Quigley rc = task_has_perm(current, p, perm); 2901f9008e4cSDavid Quigley return rc; 29021da177e4SLinus Torvalds } 29031da177e4SLinus Torvalds 29041da177e4SLinus Torvalds static int selinux_task_prctl(int option, 29051da177e4SLinus Torvalds unsigned long arg2, 29061da177e4SLinus Torvalds unsigned long arg3, 29071da177e4SLinus Torvalds unsigned long arg4, 29081da177e4SLinus Torvalds unsigned long arg5) 29091da177e4SLinus Torvalds { 29101da177e4SLinus Torvalds /* The current prctl operations do not appear to require 29111da177e4SLinus Torvalds any SELinux controls since they merely observe or modify 29121da177e4SLinus Torvalds the state of the current process. */ 29131da177e4SLinus Torvalds return 0; 29141da177e4SLinus Torvalds } 29151da177e4SLinus Torvalds 29161da177e4SLinus Torvalds static int selinux_task_wait(struct task_struct *p) 29171da177e4SLinus Torvalds { 29181da177e4SLinus Torvalds u32 perm; 29191da177e4SLinus Torvalds 29201da177e4SLinus Torvalds perm = signal_to_av(p->exit_signal); 29211da177e4SLinus Torvalds 29221da177e4SLinus Torvalds return task_has_perm(p, current, perm); 29231da177e4SLinus Torvalds } 29241da177e4SLinus Torvalds 29251da177e4SLinus Torvalds static void selinux_task_reparent_to_init(struct task_struct *p) 29261da177e4SLinus Torvalds { 29271da177e4SLinus Torvalds struct task_security_struct *tsec; 29281da177e4SLinus Torvalds 29291da177e4SLinus Torvalds secondary_ops->task_reparent_to_init(p); 29301da177e4SLinus Torvalds 29311da177e4SLinus Torvalds tsec = p->security; 29321da177e4SLinus Torvalds tsec->osid = tsec->sid; 29331da177e4SLinus Torvalds tsec->sid = SECINITSID_KERNEL; 29341da177e4SLinus Torvalds return; 29351da177e4SLinus Torvalds } 29361da177e4SLinus Torvalds 29371da177e4SLinus Torvalds static void selinux_task_to_inode(struct task_struct *p, 29381da177e4SLinus Torvalds struct inode *inode) 29391da177e4SLinus Torvalds { 29401da177e4SLinus Torvalds struct task_security_struct *tsec = p->security; 29411da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 29421da177e4SLinus Torvalds 29431da177e4SLinus Torvalds isec->sid = tsec->sid; 29441da177e4SLinus Torvalds isec->initialized = 1; 29451da177e4SLinus Torvalds return; 29461da177e4SLinus Torvalds } 29471da177e4SLinus Torvalds 29481da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 294967f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv4(struct sk_buff *skb, 295067f83cbfSVenkat Yekkirala struct avc_audit_data *ad, u8 *proto) 29511da177e4SLinus Torvalds { 29521da177e4SLinus Torvalds int offset, ihlen, ret = -EINVAL; 29531da177e4SLinus Torvalds struct iphdr _iph, *ih; 29541da177e4SLinus Torvalds 2955bbe735e4SArnaldo Carvalho de Melo offset = skb_network_offset(skb); 29561da177e4SLinus Torvalds ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph); 29571da177e4SLinus Torvalds if (ih == NULL) 29581da177e4SLinus Torvalds goto out; 29591da177e4SLinus Torvalds 29601da177e4SLinus Torvalds ihlen = ih->ihl * 4; 29611da177e4SLinus Torvalds if (ihlen < sizeof(_iph)) 29621da177e4SLinus Torvalds goto out; 29631da177e4SLinus Torvalds 29641da177e4SLinus Torvalds ad->u.net.v4info.saddr = ih->saddr; 29651da177e4SLinus Torvalds ad->u.net.v4info.daddr = ih->daddr; 29661da177e4SLinus Torvalds ret = 0; 29671da177e4SLinus Torvalds 296867f83cbfSVenkat Yekkirala if (proto) 296967f83cbfSVenkat Yekkirala *proto = ih->protocol; 297067f83cbfSVenkat Yekkirala 29711da177e4SLinus Torvalds switch (ih->protocol) { 29721da177e4SLinus Torvalds case IPPROTO_TCP: { 29731da177e4SLinus Torvalds struct tcphdr _tcph, *th; 29741da177e4SLinus Torvalds 29751da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 29761da177e4SLinus Torvalds break; 29771da177e4SLinus Torvalds 29781da177e4SLinus Torvalds offset += ihlen; 29791da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 29801da177e4SLinus Torvalds if (th == NULL) 29811da177e4SLinus Torvalds break; 29821da177e4SLinus Torvalds 29831da177e4SLinus Torvalds ad->u.net.sport = th->source; 29841da177e4SLinus Torvalds ad->u.net.dport = th->dest; 29851da177e4SLinus Torvalds break; 29861da177e4SLinus Torvalds } 29871da177e4SLinus Torvalds 29881da177e4SLinus Torvalds case IPPROTO_UDP: { 29891da177e4SLinus Torvalds struct udphdr _udph, *uh; 29901da177e4SLinus Torvalds 29911da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 29921da177e4SLinus Torvalds break; 29931da177e4SLinus Torvalds 29941da177e4SLinus Torvalds offset += ihlen; 29951da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 29961da177e4SLinus Torvalds if (uh == NULL) 29971da177e4SLinus Torvalds break; 29981da177e4SLinus Torvalds 29991da177e4SLinus Torvalds ad->u.net.sport = uh->source; 30001da177e4SLinus Torvalds ad->u.net.dport = uh->dest; 30011da177e4SLinus Torvalds break; 30021da177e4SLinus Torvalds } 30031da177e4SLinus Torvalds 30042ee92d46SJames Morris case IPPROTO_DCCP: { 30052ee92d46SJames Morris struct dccp_hdr _dccph, *dh; 30062ee92d46SJames Morris 30072ee92d46SJames Morris if (ntohs(ih->frag_off) & IP_OFFSET) 30082ee92d46SJames Morris break; 30092ee92d46SJames Morris 30102ee92d46SJames Morris offset += ihlen; 30112ee92d46SJames Morris dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); 30122ee92d46SJames Morris if (dh == NULL) 30132ee92d46SJames Morris break; 30142ee92d46SJames Morris 30152ee92d46SJames Morris ad->u.net.sport = dh->dccph_sport; 30162ee92d46SJames Morris ad->u.net.dport = dh->dccph_dport; 30172ee92d46SJames Morris break; 30182ee92d46SJames Morris } 30192ee92d46SJames Morris 30201da177e4SLinus Torvalds default: 30211da177e4SLinus Torvalds break; 30221da177e4SLinus Torvalds } 30231da177e4SLinus Torvalds out: 30241da177e4SLinus Torvalds return ret; 30251da177e4SLinus Torvalds } 30261da177e4SLinus Torvalds 30271da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 30281da177e4SLinus Torvalds 30291da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 303067f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv6(struct sk_buff *skb, 303167f83cbfSVenkat Yekkirala struct avc_audit_data *ad, u8 *proto) 30321da177e4SLinus Torvalds { 30331da177e4SLinus Torvalds u8 nexthdr; 30341da177e4SLinus Torvalds int ret = -EINVAL, offset; 30351da177e4SLinus Torvalds struct ipv6hdr _ipv6h, *ip6; 30361da177e4SLinus Torvalds 3037bbe735e4SArnaldo Carvalho de Melo offset = skb_network_offset(skb); 30381da177e4SLinus Torvalds ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h); 30391da177e4SLinus Torvalds if (ip6 == NULL) 30401da177e4SLinus Torvalds goto out; 30411da177e4SLinus Torvalds 30421da177e4SLinus Torvalds ipv6_addr_copy(&ad->u.net.v6info.saddr, &ip6->saddr); 30431da177e4SLinus Torvalds ipv6_addr_copy(&ad->u.net.v6info.daddr, &ip6->daddr); 30441da177e4SLinus Torvalds ret = 0; 30451da177e4SLinus Torvalds 30461da177e4SLinus Torvalds nexthdr = ip6->nexthdr; 30471da177e4SLinus Torvalds offset += sizeof(_ipv6h); 30480d3d077cSHerbert Xu offset = ipv6_skip_exthdr(skb, offset, &nexthdr); 30491da177e4SLinus Torvalds if (offset < 0) 30501da177e4SLinus Torvalds goto out; 30511da177e4SLinus Torvalds 305267f83cbfSVenkat Yekkirala if (proto) 305367f83cbfSVenkat Yekkirala *proto = nexthdr; 305467f83cbfSVenkat Yekkirala 30551da177e4SLinus Torvalds switch (nexthdr) { 30561da177e4SLinus Torvalds case IPPROTO_TCP: { 30571da177e4SLinus Torvalds struct tcphdr _tcph, *th; 30581da177e4SLinus Torvalds 30591da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 30601da177e4SLinus Torvalds if (th == NULL) 30611da177e4SLinus Torvalds break; 30621da177e4SLinus Torvalds 30631da177e4SLinus Torvalds ad->u.net.sport = th->source; 30641da177e4SLinus Torvalds ad->u.net.dport = th->dest; 30651da177e4SLinus Torvalds break; 30661da177e4SLinus Torvalds } 30671da177e4SLinus Torvalds 30681da177e4SLinus Torvalds case IPPROTO_UDP: { 30691da177e4SLinus Torvalds struct udphdr _udph, *uh; 30701da177e4SLinus Torvalds 30711da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 30721da177e4SLinus Torvalds if (uh == NULL) 30731da177e4SLinus Torvalds break; 30741da177e4SLinus Torvalds 30751da177e4SLinus Torvalds ad->u.net.sport = uh->source; 30761da177e4SLinus Torvalds ad->u.net.dport = uh->dest; 30771da177e4SLinus Torvalds break; 30781da177e4SLinus Torvalds } 30791da177e4SLinus Torvalds 30802ee92d46SJames Morris case IPPROTO_DCCP: { 30812ee92d46SJames Morris struct dccp_hdr _dccph, *dh; 30822ee92d46SJames Morris 30832ee92d46SJames Morris dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); 30842ee92d46SJames Morris if (dh == NULL) 30852ee92d46SJames Morris break; 30862ee92d46SJames Morris 30872ee92d46SJames Morris ad->u.net.sport = dh->dccph_sport; 30882ee92d46SJames Morris ad->u.net.dport = dh->dccph_dport; 30892ee92d46SJames Morris break; 30902ee92d46SJames Morris } 30912ee92d46SJames Morris 30921da177e4SLinus Torvalds /* includes fragments */ 30931da177e4SLinus Torvalds default: 30941da177e4SLinus Torvalds break; 30951da177e4SLinus Torvalds } 30961da177e4SLinus Torvalds out: 30971da177e4SLinus Torvalds return ret; 30981da177e4SLinus Torvalds } 30991da177e4SLinus Torvalds 31001da177e4SLinus Torvalds #endif /* IPV6 */ 31011da177e4SLinus Torvalds 31021da177e4SLinus Torvalds static int selinux_parse_skb(struct sk_buff *skb, struct avc_audit_data *ad, 310367f83cbfSVenkat Yekkirala char **addrp, int *len, int src, u8 *proto) 31041da177e4SLinus Torvalds { 31051da177e4SLinus Torvalds int ret = 0; 31061da177e4SLinus Torvalds 31071da177e4SLinus Torvalds switch (ad->u.net.family) { 31081da177e4SLinus Torvalds case PF_INET: 310967f83cbfSVenkat Yekkirala ret = selinux_parse_skb_ipv4(skb, ad, proto); 31101da177e4SLinus Torvalds if (ret || !addrp) 31111da177e4SLinus Torvalds break; 31121da177e4SLinus Torvalds *len = 4; 31131da177e4SLinus Torvalds *addrp = (char *)(src ? &ad->u.net.v4info.saddr : 31141da177e4SLinus Torvalds &ad->u.net.v4info.daddr); 31151da177e4SLinus Torvalds break; 31161da177e4SLinus Torvalds 31171da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 31181da177e4SLinus Torvalds case PF_INET6: 311967f83cbfSVenkat Yekkirala ret = selinux_parse_skb_ipv6(skb, ad, proto); 31201da177e4SLinus Torvalds if (ret || !addrp) 31211da177e4SLinus Torvalds break; 31221da177e4SLinus Torvalds *len = 16; 31231da177e4SLinus Torvalds *addrp = (char *)(src ? &ad->u.net.v6info.saddr : 31241da177e4SLinus Torvalds &ad->u.net.v6info.daddr); 31251da177e4SLinus Torvalds break; 31261da177e4SLinus Torvalds #endif /* IPV6 */ 31271da177e4SLinus Torvalds default: 31281da177e4SLinus Torvalds break; 31291da177e4SLinus Torvalds } 31301da177e4SLinus Torvalds 31311da177e4SLinus Torvalds return ret; 31321da177e4SLinus Torvalds } 31331da177e4SLinus Torvalds 31344f6a993fSPaul Moore /** 31354f6a993fSPaul Moore * selinux_skb_extlbl_sid - Determine the external label of a packet 31364f6a993fSPaul Moore * @skb: the packet 31374f6a993fSPaul Moore * @sid: the packet's SID 31384f6a993fSPaul Moore * 31394f6a993fSPaul Moore * Description: 31404f6a993fSPaul Moore * Check the various different forms of external packet labeling and determine 3141f36158c4SPaul Moore * the external SID for the packet. If only one form of external labeling is 3142f36158c4SPaul Moore * present then it is used, if both labeled IPsec and NetLabel labels are 3143f36158c4SPaul Moore * present then the SELinux type information is taken from the labeled IPsec 3144f36158c4SPaul Moore * SA and the MLS sensitivity label information is taken from the NetLabel 3145f36158c4SPaul Moore * security attributes. This bit of "magic" is done in the call to 3146f36158c4SPaul Moore * selinux_netlbl_skbuff_getsid(). 31474f6a993fSPaul Moore * 31484f6a993fSPaul Moore */ 3149f36158c4SPaul Moore static void selinux_skb_extlbl_sid(struct sk_buff *skb, u32 *sid) 31504f6a993fSPaul Moore { 31514f6a993fSPaul Moore u32 xfrm_sid; 31524f6a993fSPaul Moore u32 nlbl_sid; 31534f6a993fSPaul Moore 31544f6a993fSPaul Moore selinux_skb_xfrm_sid(skb, &xfrm_sid); 31554f6a993fSPaul Moore if (selinux_netlbl_skbuff_getsid(skb, 31564f6a993fSPaul Moore (xfrm_sid == SECSID_NULL ? 3157f36158c4SPaul Moore SECINITSID_NETMSG : xfrm_sid), 31584f6a993fSPaul Moore &nlbl_sid) != 0) 31594f6a993fSPaul Moore nlbl_sid = SECSID_NULL; 31604f6a993fSPaul Moore *sid = (nlbl_sid == SECSID_NULL ? xfrm_sid : nlbl_sid); 31614f6a993fSPaul Moore } 31624f6a993fSPaul Moore 31631da177e4SLinus Torvalds /* socket security operations */ 31641da177e4SLinus Torvalds static int socket_has_perm(struct task_struct *task, struct socket *sock, 31651da177e4SLinus Torvalds u32 perms) 31661da177e4SLinus Torvalds { 31671da177e4SLinus Torvalds struct inode_security_struct *isec; 31681da177e4SLinus Torvalds struct task_security_struct *tsec; 31691da177e4SLinus Torvalds struct avc_audit_data ad; 31701da177e4SLinus Torvalds int err = 0; 31711da177e4SLinus Torvalds 31721da177e4SLinus Torvalds tsec = task->security; 31731da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 31741da177e4SLinus Torvalds 31751da177e4SLinus Torvalds if (isec->sid == SECINITSID_KERNEL) 31761da177e4SLinus Torvalds goto out; 31771da177e4SLinus Torvalds 31781da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,NET); 31791da177e4SLinus Torvalds ad.u.net.sk = sock->sk; 31801da177e4SLinus Torvalds err = avc_has_perm(tsec->sid, isec->sid, isec->sclass, perms, &ad); 31811da177e4SLinus Torvalds 31821da177e4SLinus Torvalds out: 31831da177e4SLinus Torvalds return err; 31841da177e4SLinus Torvalds } 31851da177e4SLinus Torvalds 31861da177e4SLinus Torvalds static int selinux_socket_create(int family, int type, 31871da177e4SLinus Torvalds int protocol, int kern) 31881da177e4SLinus Torvalds { 31891da177e4SLinus Torvalds int err = 0; 31901da177e4SLinus Torvalds struct task_security_struct *tsec; 319142c3e03eSEric Paris u32 newsid; 31921da177e4SLinus Torvalds 31931da177e4SLinus Torvalds if (kern) 31941da177e4SLinus Torvalds goto out; 31951da177e4SLinus Torvalds 31961da177e4SLinus Torvalds tsec = current->security; 319742c3e03eSEric Paris newsid = tsec->sockcreate_sid ? : tsec->sid; 319842c3e03eSEric Paris err = avc_has_perm(tsec->sid, newsid, 31991da177e4SLinus Torvalds socket_type_to_security_class(family, type, 32001da177e4SLinus Torvalds protocol), SOCKET__CREATE, NULL); 32011da177e4SLinus Torvalds 32021da177e4SLinus Torvalds out: 32031da177e4SLinus Torvalds return err; 32041da177e4SLinus Torvalds } 32051da177e4SLinus Torvalds 32067420ed23SVenkat Yekkirala static int selinux_socket_post_create(struct socket *sock, int family, 32071da177e4SLinus Torvalds int type, int protocol, int kern) 32081da177e4SLinus Torvalds { 32097420ed23SVenkat Yekkirala int err = 0; 32101da177e4SLinus Torvalds struct inode_security_struct *isec; 32111da177e4SLinus Torvalds struct task_security_struct *tsec; 3212892c141eSVenkat Yekkirala struct sk_security_struct *sksec; 321342c3e03eSEric Paris u32 newsid; 32141da177e4SLinus Torvalds 32151da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 32161da177e4SLinus Torvalds 32171da177e4SLinus Torvalds tsec = current->security; 321842c3e03eSEric Paris newsid = tsec->sockcreate_sid ? : tsec->sid; 32191da177e4SLinus Torvalds isec->sclass = socket_type_to_security_class(family, type, protocol); 322042c3e03eSEric Paris isec->sid = kern ? SECINITSID_KERNEL : newsid; 32211da177e4SLinus Torvalds isec->initialized = 1; 32221da177e4SLinus Torvalds 3223892c141eSVenkat Yekkirala if (sock->sk) { 3224892c141eSVenkat Yekkirala sksec = sock->sk->sk_security; 3225892c141eSVenkat Yekkirala sksec->sid = isec->sid; 32269f2ad665SPaul Moore err = selinux_netlbl_socket_post_create(sock); 3227892c141eSVenkat Yekkirala } 3228892c141eSVenkat Yekkirala 32297420ed23SVenkat Yekkirala return err; 32301da177e4SLinus Torvalds } 32311da177e4SLinus Torvalds 32321da177e4SLinus Torvalds /* Range of port numbers used to automatically bind. 32331da177e4SLinus Torvalds Need to determine whether we should perform a name_bind 32341da177e4SLinus Torvalds permission check between the socket and the port number. */ 32351da177e4SLinus Torvalds #define ip_local_port_range_0 sysctl_local_port_range[0] 32361da177e4SLinus Torvalds #define ip_local_port_range_1 sysctl_local_port_range[1] 32371da177e4SLinus Torvalds 32381da177e4SLinus Torvalds static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen) 32391da177e4SLinus Torvalds { 32401da177e4SLinus Torvalds u16 family; 32411da177e4SLinus Torvalds int err; 32421da177e4SLinus Torvalds 32431da177e4SLinus Torvalds err = socket_has_perm(current, sock, SOCKET__BIND); 32441da177e4SLinus Torvalds if (err) 32451da177e4SLinus Torvalds goto out; 32461da177e4SLinus Torvalds 32471da177e4SLinus Torvalds /* 32481da177e4SLinus Torvalds * If PF_INET or PF_INET6, check name_bind permission for the port. 324913402580SJames Morris * Multiple address binding for SCTP is not supported yet: we just 325013402580SJames Morris * check the first address now. 32511da177e4SLinus Torvalds */ 32521da177e4SLinus Torvalds family = sock->sk->sk_family; 32531da177e4SLinus Torvalds if (family == PF_INET || family == PF_INET6) { 32541da177e4SLinus Torvalds char *addrp; 32551da177e4SLinus Torvalds struct inode_security_struct *isec; 32561da177e4SLinus Torvalds struct task_security_struct *tsec; 32571da177e4SLinus Torvalds struct avc_audit_data ad; 32581da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 32591da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 32601da177e4SLinus Torvalds unsigned short snum; 32611da177e4SLinus Torvalds struct sock *sk = sock->sk; 32621da177e4SLinus Torvalds u32 sid, node_perm, addrlen; 32631da177e4SLinus Torvalds 32641da177e4SLinus Torvalds tsec = current->security; 32651da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 32661da177e4SLinus Torvalds 32671da177e4SLinus Torvalds if (family == PF_INET) { 32681da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 32691da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 32701da177e4SLinus Torvalds addrlen = sizeof(addr4->sin_addr.s_addr); 32711da177e4SLinus Torvalds addrp = (char *)&addr4->sin_addr.s_addr; 32721da177e4SLinus Torvalds } else { 32731da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 32741da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 32751da177e4SLinus Torvalds addrlen = sizeof(addr6->sin6_addr.s6_addr); 32761da177e4SLinus Torvalds addrp = (char *)&addr6->sin6_addr.s6_addr; 32771da177e4SLinus Torvalds } 32781da177e4SLinus Torvalds 32791da177e4SLinus Torvalds if (snum&&(snum < max(PROT_SOCK,ip_local_port_range_0) || 32801da177e4SLinus Torvalds snum > ip_local_port_range_1)) { 32811da177e4SLinus Torvalds err = security_port_sid(sk->sk_family, sk->sk_type, 32821da177e4SLinus Torvalds sk->sk_protocol, snum, &sid); 32831da177e4SLinus Torvalds if (err) 32841da177e4SLinus Torvalds goto out; 32851da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,NET); 32861da177e4SLinus Torvalds ad.u.net.sport = htons(snum); 32871da177e4SLinus Torvalds ad.u.net.family = family; 32881da177e4SLinus Torvalds err = avc_has_perm(isec->sid, sid, 32891da177e4SLinus Torvalds isec->sclass, 32901da177e4SLinus Torvalds SOCKET__NAME_BIND, &ad); 32911da177e4SLinus Torvalds if (err) 32921da177e4SLinus Torvalds goto out; 32931da177e4SLinus Torvalds } 32941da177e4SLinus Torvalds 329513402580SJames Morris switch(isec->sclass) { 329613402580SJames Morris case SECCLASS_TCP_SOCKET: 32971da177e4SLinus Torvalds node_perm = TCP_SOCKET__NODE_BIND; 32981da177e4SLinus Torvalds break; 32991da177e4SLinus Torvalds 330013402580SJames Morris case SECCLASS_UDP_SOCKET: 33011da177e4SLinus Torvalds node_perm = UDP_SOCKET__NODE_BIND; 33021da177e4SLinus Torvalds break; 33031da177e4SLinus Torvalds 33042ee92d46SJames Morris case SECCLASS_DCCP_SOCKET: 33052ee92d46SJames Morris node_perm = DCCP_SOCKET__NODE_BIND; 33062ee92d46SJames Morris break; 33072ee92d46SJames Morris 33081da177e4SLinus Torvalds default: 33091da177e4SLinus Torvalds node_perm = RAWIP_SOCKET__NODE_BIND; 33101da177e4SLinus Torvalds break; 33111da177e4SLinus Torvalds } 33121da177e4SLinus Torvalds 33131da177e4SLinus Torvalds err = security_node_sid(family, addrp, addrlen, &sid); 33141da177e4SLinus Torvalds if (err) 33151da177e4SLinus Torvalds goto out; 33161da177e4SLinus Torvalds 33171da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,NET); 33181da177e4SLinus Torvalds ad.u.net.sport = htons(snum); 33191da177e4SLinus Torvalds ad.u.net.family = family; 33201da177e4SLinus Torvalds 33211da177e4SLinus Torvalds if (family == PF_INET) 33221da177e4SLinus Torvalds ad.u.net.v4info.saddr = addr4->sin_addr.s_addr; 33231da177e4SLinus Torvalds else 33241da177e4SLinus Torvalds ipv6_addr_copy(&ad.u.net.v6info.saddr, &addr6->sin6_addr); 33251da177e4SLinus Torvalds 33261da177e4SLinus Torvalds err = avc_has_perm(isec->sid, sid, 33271da177e4SLinus Torvalds isec->sclass, node_perm, &ad); 33281da177e4SLinus Torvalds if (err) 33291da177e4SLinus Torvalds goto out; 33301da177e4SLinus Torvalds } 33311da177e4SLinus Torvalds out: 33321da177e4SLinus Torvalds return err; 33331da177e4SLinus Torvalds } 33341da177e4SLinus Torvalds 33351da177e4SLinus Torvalds static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen) 33361da177e4SLinus Torvalds { 33371da177e4SLinus Torvalds struct inode_security_struct *isec; 33381da177e4SLinus Torvalds int err; 33391da177e4SLinus Torvalds 33401da177e4SLinus Torvalds err = socket_has_perm(current, sock, SOCKET__CONNECT); 33411da177e4SLinus Torvalds if (err) 33421da177e4SLinus Torvalds return err; 33431da177e4SLinus Torvalds 33441da177e4SLinus Torvalds /* 33452ee92d46SJames Morris * If a TCP or DCCP socket, check name_connect permission for the port. 33461da177e4SLinus Torvalds */ 33471da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 33482ee92d46SJames Morris if (isec->sclass == SECCLASS_TCP_SOCKET || 33492ee92d46SJames Morris isec->sclass == SECCLASS_DCCP_SOCKET) { 33501da177e4SLinus Torvalds struct sock *sk = sock->sk; 33511da177e4SLinus Torvalds struct avc_audit_data ad; 33521da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 33531da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 33541da177e4SLinus Torvalds unsigned short snum; 33552ee92d46SJames Morris u32 sid, perm; 33561da177e4SLinus Torvalds 33571da177e4SLinus Torvalds if (sk->sk_family == PF_INET) { 33581da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 3359911656f8SStephen Smalley if (addrlen < sizeof(struct sockaddr_in)) 33601da177e4SLinus Torvalds return -EINVAL; 33611da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 33621da177e4SLinus Torvalds } else { 33631da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 3364911656f8SStephen Smalley if (addrlen < SIN6_LEN_RFC2133) 33651da177e4SLinus Torvalds return -EINVAL; 33661da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 33671da177e4SLinus Torvalds } 33681da177e4SLinus Torvalds 33691da177e4SLinus Torvalds err = security_port_sid(sk->sk_family, sk->sk_type, 33701da177e4SLinus Torvalds sk->sk_protocol, snum, &sid); 33711da177e4SLinus Torvalds if (err) 33721da177e4SLinus Torvalds goto out; 33731da177e4SLinus Torvalds 33742ee92d46SJames Morris perm = (isec->sclass == SECCLASS_TCP_SOCKET) ? 33752ee92d46SJames Morris TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT; 33762ee92d46SJames Morris 33771da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,NET); 33781da177e4SLinus Torvalds ad.u.net.dport = htons(snum); 33791da177e4SLinus Torvalds ad.u.net.family = sk->sk_family; 33802ee92d46SJames Morris err = avc_has_perm(isec->sid, sid, isec->sclass, perm, &ad); 33811da177e4SLinus Torvalds if (err) 33821da177e4SLinus Torvalds goto out; 33831da177e4SLinus Torvalds } 33841da177e4SLinus Torvalds 33851da177e4SLinus Torvalds out: 33861da177e4SLinus Torvalds return err; 33871da177e4SLinus Torvalds } 33881da177e4SLinus Torvalds 33891da177e4SLinus Torvalds static int selinux_socket_listen(struct socket *sock, int backlog) 33901da177e4SLinus Torvalds { 33911da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__LISTEN); 33921da177e4SLinus Torvalds } 33931da177e4SLinus Torvalds 33941da177e4SLinus Torvalds static int selinux_socket_accept(struct socket *sock, struct socket *newsock) 33951da177e4SLinus Torvalds { 33961da177e4SLinus Torvalds int err; 33971da177e4SLinus Torvalds struct inode_security_struct *isec; 33981da177e4SLinus Torvalds struct inode_security_struct *newisec; 33991da177e4SLinus Torvalds 34001da177e4SLinus Torvalds err = socket_has_perm(current, sock, SOCKET__ACCEPT); 34011da177e4SLinus Torvalds if (err) 34021da177e4SLinus Torvalds return err; 34031da177e4SLinus Torvalds 34041da177e4SLinus Torvalds newisec = SOCK_INODE(newsock)->i_security; 34051da177e4SLinus Torvalds 34061da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 34071da177e4SLinus Torvalds newisec->sclass = isec->sclass; 34081da177e4SLinus Torvalds newisec->sid = isec->sid; 34091da177e4SLinus Torvalds newisec->initialized = 1; 34101da177e4SLinus Torvalds 34111da177e4SLinus Torvalds return 0; 34121da177e4SLinus Torvalds } 34131da177e4SLinus Torvalds 34141da177e4SLinus Torvalds static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg, 34151da177e4SLinus Torvalds int size) 34161da177e4SLinus Torvalds { 34177420ed23SVenkat Yekkirala int rc; 34187420ed23SVenkat Yekkirala 34197420ed23SVenkat Yekkirala rc = socket_has_perm(current, sock, SOCKET__WRITE); 34207420ed23SVenkat Yekkirala if (rc) 34217420ed23SVenkat Yekkirala return rc; 34227420ed23SVenkat Yekkirala 34237420ed23SVenkat Yekkirala return selinux_netlbl_inode_permission(SOCK_INODE(sock), MAY_WRITE); 34241da177e4SLinus Torvalds } 34251da177e4SLinus Torvalds 34261da177e4SLinus Torvalds static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg, 34271da177e4SLinus Torvalds int size, int flags) 34281da177e4SLinus Torvalds { 34291da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__READ); 34301da177e4SLinus Torvalds } 34311da177e4SLinus Torvalds 34321da177e4SLinus Torvalds static int selinux_socket_getsockname(struct socket *sock) 34331da177e4SLinus Torvalds { 34341da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__GETATTR); 34351da177e4SLinus Torvalds } 34361da177e4SLinus Torvalds 34371da177e4SLinus Torvalds static int selinux_socket_getpeername(struct socket *sock) 34381da177e4SLinus Torvalds { 34391da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__GETATTR); 34401da177e4SLinus Torvalds } 34411da177e4SLinus Torvalds 34421da177e4SLinus Torvalds static int selinux_socket_setsockopt(struct socket *sock,int level,int optname) 34431da177e4SLinus Torvalds { 3444f8687afeSPaul Moore int err; 3445f8687afeSPaul Moore 3446f8687afeSPaul Moore err = socket_has_perm(current, sock, SOCKET__SETOPT); 3447f8687afeSPaul Moore if (err) 3448f8687afeSPaul Moore return err; 3449f8687afeSPaul Moore 3450f8687afeSPaul Moore return selinux_netlbl_socket_setsockopt(sock, level, optname); 34511da177e4SLinus Torvalds } 34521da177e4SLinus Torvalds 34531da177e4SLinus Torvalds static int selinux_socket_getsockopt(struct socket *sock, int level, 34541da177e4SLinus Torvalds int optname) 34551da177e4SLinus Torvalds { 34561da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__GETOPT); 34571da177e4SLinus Torvalds } 34581da177e4SLinus Torvalds 34591da177e4SLinus Torvalds static int selinux_socket_shutdown(struct socket *sock, int how) 34601da177e4SLinus Torvalds { 34611da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__SHUTDOWN); 34621da177e4SLinus Torvalds } 34631da177e4SLinus Torvalds 34641da177e4SLinus Torvalds static int selinux_socket_unix_stream_connect(struct socket *sock, 34651da177e4SLinus Torvalds struct socket *other, 34661da177e4SLinus Torvalds struct sock *newsk) 34671da177e4SLinus Torvalds { 34681da177e4SLinus Torvalds struct sk_security_struct *ssec; 34691da177e4SLinus Torvalds struct inode_security_struct *isec; 34701da177e4SLinus Torvalds struct inode_security_struct *other_isec; 34711da177e4SLinus Torvalds struct avc_audit_data ad; 34721da177e4SLinus Torvalds int err; 34731da177e4SLinus Torvalds 34741da177e4SLinus Torvalds err = secondary_ops->unix_stream_connect(sock, other, newsk); 34751da177e4SLinus Torvalds if (err) 34761da177e4SLinus Torvalds return err; 34771da177e4SLinus Torvalds 34781da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 34791da177e4SLinus Torvalds other_isec = SOCK_INODE(other)->i_security; 34801da177e4SLinus Torvalds 34811da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,NET); 34821da177e4SLinus Torvalds ad.u.net.sk = other->sk; 34831da177e4SLinus Torvalds 34841da177e4SLinus Torvalds err = avc_has_perm(isec->sid, other_isec->sid, 34851da177e4SLinus Torvalds isec->sclass, 34861da177e4SLinus Torvalds UNIX_STREAM_SOCKET__CONNECTTO, &ad); 34871da177e4SLinus Torvalds if (err) 34881da177e4SLinus Torvalds return err; 34891da177e4SLinus Torvalds 34901da177e4SLinus Torvalds /* connecting socket */ 34911da177e4SLinus Torvalds ssec = sock->sk->sk_security; 34921da177e4SLinus Torvalds ssec->peer_sid = other_isec->sid; 34931da177e4SLinus Torvalds 34941da177e4SLinus Torvalds /* server child socket */ 34951da177e4SLinus Torvalds ssec = newsk->sk_security; 34961da177e4SLinus Torvalds ssec->peer_sid = isec->sid; 34974237c75cSVenkat Yekkirala err = security_sid_mls_copy(other_isec->sid, ssec->peer_sid, &ssec->sid); 34981da177e4SLinus Torvalds 34994237c75cSVenkat Yekkirala return err; 35001da177e4SLinus Torvalds } 35011da177e4SLinus Torvalds 35021da177e4SLinus Torvalds static int selinux_socket_unix_may_send(struct socket *sock, 35031da177e4SLinus Torvalds struct socket *other) 35041da177e4SLinus Torvalds { 35051da177e4SLinus Torvalds struct inode_security_struct *isec; 35061da177e4SLinus Torvalds struct inode_security_struct *other_isec; 35071da177e4SLinus Torvalds struct avc_audit_data ad; 35081da177e4SLinus Torvalds int err; 35091da177e4SLinus Torvalds 35101da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 35111da177e4SLinus Torvalds other_isec = SOCK_INODE(other)->i_security; 35121da177e4SLinus Torvalds 35131da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,NET); 35141da177e4SLinus Torvalds ad.u.net.sk = other->sk; 35151da177e4SLinus Torvalds 35161da177e4SLinus Torvalds err = avc_has_perm(isec->sid, other_isec->sid, 35171da177e4SLinus Torvalds isec->sclass, SOCKET__SENDTO, &ad); 35181da177e4SLinus Torvalds if (err) 35191da177e4SLinus Torvalds return err; 35201da177e4SLinus Torvalds 35211da177e4SLinus Torvalds return 0; 35221da177e4SLinus Torvalds } 35231da177e4SLinus Torvalds 35244e5ab4cbSJames Morris static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb, 35254237c75cSVenkat Yekkirala struct avc_audit_data *ad, u16 family, char *addrp, int len) 35261da177e4SLinus Torvalds { 35274e5ab4cbSJames Morris int err = 0; 35281da177e4SLinus Torvalds u32 netif_perm, node_perm, node_sid, if_sid, recv_perm = 0; 35294237c75cSVenkat Yekkirala struct socket *sock; 35304237c75cSVenkat Yekkirala u16 sock_class = 0; 35314237c75cSVenkat Yekkirala u32 sock_sid = 0; 35324237c75cSVenkat Yekkirala 35334237c75cSVenkat Yekkirala read_lock_bh(&sk->sk_callback_lock); 35344237c75cSVenkat Yekkirala sock = sk->sk_socket; 35354237c75cSVenkat Yekkirala if (sock) { 35364237c75cSVenkat Yekkirala struct inode *inode; 35374237c75cSVenkat Yekkirala inode = SOCK_INODE(sock); 35384237c75cSVenkat Yekkirala if (inode) { 35394237c75cSVenkat Yekkirala struct inode_security_struct *isec; 35404237c75cSVenkat Yekkirala isec = inode->i_security; 35414237c75cSVenkat Yekkirala sock_sid = isec->sid; 35424237c75cSVenkat Yekkirala sock_class = isec->sclass; 35434237c75cSVenkat Yekkirala } 35444237c75cSVenkat Yekkirala } 35454237c75cSVenkat Yekkirala read_unlock_bh(&sk->sk_callback_lock); 35464237c75cSVenkat Yekkirala if (!sock_sid) 35474237c75cSVenkat Yekkirala goto out; 35481da177e4SLinus Torvalds 35494e5ab4cbSJames Morris if (!skb->dev) 35501da177e4SLinus Torvalds goto out; 35511da177e4SLinus Torvalds 35524e5ab4cbSJames Morris err = sel_netif_sids(skb->dev, &if_sid, NULL); 35531da177e4SLinus Torvalds if (err) 35541da177e4SLinus Torvalds goto out; 35551da177e4SLinus Torvalds 35561da177e4SLinus Torvalds switch (sock_class) { 35571da177e4SLinus Torvalds case SECCLASS_UDP_SOCKET: 35581da177e4SLinus Torvalds netif_perm = NETIF__UDP_RECV; 35591da177e4SLinus Torvalds node_perm = NODE__UDP_RECV; 35601da177e4SLinus Torvalds recv_perm = UDP_SOCKET__RECV_MSG; 35611da177e4SLinus Torvalds break; 35621da177e4SLinus Torvalds 35631da177e4SLinus Torvalds case SECCLASS_TCP_SOCKET: 35641da177e4SLinus Torvalds netif_perm = NETIF__TCP_RECV; 35651da177e4SLinus Torvalds node_perm = NODE__TCP_RECV; 35661da177e4SLinus Torvalds recv_perm = TCP_SOCKET__RECV_MSG; 35671da177e4SLinus Torvalds break; 35681da177e4SLinus Torvalds 35692ee92d46SJames Morris case SECCLASS_DCCP_SOCKET: 35702ee92d46SJames Morris netif_perm = NETIF__DCCP_RECV; 35712ee92d46SJames Morris node_perm = NODE__DCCP_RECV; 35722ee92d46SJames Morris recv_perm = DCCP_SOCKET__RECV_MSG; 35732ee92d46SJames Morris break; 35742ee92d46SJames Morris 35751da177e4SLinus Torvalds default: 35761da177e4SLinus Torvalds netif_perm = NETIF__RAWIP_RECV; 35771da177e4SLinus Torvalds node_perm = NODE__RAWIP_RECV; 35781da177e4SLinus Torvalds break; 35791da177e4SLinus Torvalds } 35801da177e4SLinus Torvalds 35814e5ab4cbSJames Morris err = avc_has_perm(sock_sid, if_sid, SECCLASS_NETIF, netif_perm, ad); 35821da177e4SLinus Torvalds if (err) 35831da177e4SLinus Torvalds goto out; 35841da177e4SLinus Torvalds 35851da177e4SLinus Torvalds err = security_node_sid(family, addrp, len, &node_sid); 35861da177e4SLinus Torvalds if (err) 35871da177e4SLinus Torvalds goto out; 35881da177e4SLinus Torvalds 35894e5ab4cbSJames Morris err = avc_has_perm(sock_sid, node_sid, SECCLASS_NODE, node_perm, ad); 35901da177e4SLinus Torvalds if (err) 35911da177e4SLinus Torvalds goto out; 35921da177e4SLinus Torvalds 35931da177e4SLinus Torvalds if (recv_perm) { 35941da177e4SLinus Torvalds u32 port_sid; 35951da177e4SLinus Torvalds 35961da177e4SLinus Torvalds err = security_port_sid(sk->sk_family, sk->sk_type, 35974e5ab4cbSJames Morris sk->sk_protocol, ntohs(ad->u.net.sport), 35981da177e4SLinus Torvalds &port_sid); 35991da177e4SLinus Torvalds if (err) 36001da177e4SLinus Torvalds goto out; 36011da177e4SLinus Torvalds 36021da177e4SLinus Torvalds err = avc_has_perm(sock_sid, port_sid, 36034e5ab4cbSJames Morris sock_class, recv_perm, ad); 36041da177e4SLinus Torvalds } 3605d28d1e08STrent Jaeger 36064e5ab4cbSJames Morris out: 36074e5ab4cbSJames Morris return err; 36084e5ab4cbSJames Morris } 3609d28d1e08STrent Jaeger 36104e5ab4cbSJames Morris static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) 36114e5ab4cbSJames Morris { 36124e5ab4cbSJames Morris u16 family; 36134e5ab4cbSJames Morris char *addrp; 36144e5ab4cbSJames Morris int len, err = 0; 36154e5ab4cbSJames Morris struct avc_audit_data ad; 36164237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 36174e5ab4cbSJames Morris 36184e5ab4cbSJames Morris family = sk->sk_family; 36194e5ab4cbSJames Morris if (family != PF_INET && family != PF_INET6) 36204e5ab4cbSJames Morris goto out; 36214e5ab4cbSJames Morris 36224e5ab4cbSJames Morris /* Handle mapped IPv4 packets arriving via IPv6 sockets */ 362387fcd70dSAl Viro if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 36244e5ab4cbSJames Morris family = PF_INET; 36254e5ab4cbSJames Morris 36264e5ab4cbSJames Morris AVC_AUDIT_DATA_INIT(&ad, NET); 36274e5ab4cbSJames Morris ad.u.net.netif = skb->dev ? skb->dev->name : "[unknown]"; 36284e5ab4cbSJames Morris ad.u.net.family = family; 36294e5ab4cbSJames Morris 363067f83cbfSVenkat Yekkirala err = selinux_parse_skb(skb, &ad, &addrp, &len, 1, NULL); 36314e5ab4cbSJames Morris if (err) 36324e5ab4cbSJames Morris goto out; 36334e5ab4cbSJames Morris 36344e5ab4cbSJames Morris if (selinux_compat_net) 36354237c75cSVenkat Yekkirala err = selinux_sock_rcv_skb_compat(sk, skb, &ad, family, 36364e5ab4cbSJames Morris addrp, len); 36374e5ab4cbSJames Morris else 36384237c75cSVenkat Yekkirala err = avc_has_perm(sksec->sid, skb->secmark, SECCLASS_PACKET, 36394e5ab4cbSJames Morris PACKET__RECV, &ad); 36404e5ab4cbSJames Morris if (err) 36414e5ab4cbSJames Morris goto out; 36424e5ab4cbSJames Morris 36437420ed23SVenkat Yekkirala err = selinux_netlbl_sock_rcv_skb(sksec, skb, &ad); 36447420ed23SVenkat Yekkirala if (err) 36457420ed23SVenkat Yekkirala goto out; 36467420ed23SVenkat Yekkirala 36474237c75cSVenkat Yekkirala err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad); 36481da177e4SLinus Torvalds out: 36491da177e4SLinus Torvalds return err; 36501da177e4SLinus Torvalds } 36511da177e4SLinus Torvalds 36522c7946a7SCatherine Zhang static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval, 36531da177e4SLinus Torvalds int __user *optlen, unsigned len) 36541da177e4SLinus Torvalds { 36551da177e4SLinus Torvalds int err = 0; 36561da177e4SLinus Torvalds char *scontext; 36571da177e4SLinus Torvalds u32 scontext_len; 36581da177e4SLinus Torvalds struct sk_security_struct *ssec; 36591da177e4SLinus Torvalds struct inode_security_struct *isec; 36603de4bab5SPaul Moore u32 peer_sid = SECSID_NULL; 36611da177e4SLinus Torvalds 36621da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 36632c7946a7SCatherine Zhang 36643de4bab5SPaul Moore if (isec->sclass == SECCLASS_UNIX_STREAM_SOCKET || 36653de4bab5SPaul Moore isec->sclass == SECCLASS_TCP_SOCKET) { 36666b877699SVenkat Yekkirala ssec = sock->sk->sk_security; 36676b877699SVenkat Yekkirala peer_sid = ssec->peer_sid; 36686b877699SVenkat Yekkirala } 36692c7946a7SCatherine Zhang if (peer_sid == SECSID_NULL) { 36702c7946a7SCatherine Zhang err = -ENOPROTOOPT; 36712c7946a7SCatherine Zhang goto out; 36722c7946a7SCatherine Zhang } 36731da177e4SLinus Torvalds 36742c7946a7SCatherine Zhang err = security_sid_to_context(peer_sid, &scontext, &scontext_len); 36751da177e4SLinus Torvalds 36761da177e4SLinus Torvalds if (err) 36771da177e4SLinus Torvalds goto out; 36781da177e4SLinus Torvalds 36791da177e4SLinus Torvalds if (scontext_len > len) { 36801da177e4SLinus Torvalds err = -ERANGE; 36811da177e4SLinus Torvalds goto out_len; 36821da177e4SLinus Torvalds } 36831da177e4SLinus Torvalds 36841da177e4SLinus Torvalds if (copy_to_user(optval, scontext, scontext_len)) 36851da177e4SLinus Torvalds err = -EFAULT; 36861da177e4SLinus Torvalds 36871da177e4SLinus Torvalds out_len: 36881da177e4SLinus Torvalds if (put_user(scontext_len, optlen)) 36891da177e4SLinus Torvalds err = -EFAULT; 36901da177e4SLinus Torvalds 36911da177e4SLinus Torvalds kfree(scontext); 36921da177e4SLinus Torvalds out: 36931da177e4SLinus Torvalds return err; 36941da177e4SLinus Torvalds } 36951da177e4SLinus Torvalds 3696dc49c1f9SCatherine Zhang static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid) 36972c7946a7SCatherine Zhang { 3698dc49c1f9SCatherine Zhang u32 peer_secid = SECSID_NULL; 36992c7946a7SCatherine Zhang int err = 0; 3700877ce7c1SCatherine Zhang 37013de4bab5SPaul Moore if (sock && sock->sk->sk_family == PF_UNIX) 3702dc49c1f9SCatherine Zhang selinux_get_inode_sid(SOCK_INODE(sock), &peer_secid); 37033de4bab5SPaul Moore else if (skb) 3704f36158c4SPaul Moore selinux_skb_extlbl_sid(skb, &peer_secid); 37052c7946a7SCatherine Zhang 3706dc49c1f9SCatherine Zhang if (peer_secid == SECSID_NULL) 3707dc49c1f9SCatherine Zhang err = -EINVAL; 3708dc49c1f9SCatherine Zhang *secid = peer_secid; 37092c7946a7SCatherine Zhang 37102c7946a7SCatherine Zhang return err; 37112c7946a7SCatherine Zhang } 37122c7946a7SCatherine Zhang 37137d877f3bSAl Viro static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority) 37141da177e4SLinus Torvalds { 37151da177e4SLinus Torvalds return sk_alloc_security(sk, family, priority); 37161da177e4SLinus Torvalds } 37171da177e4SLinus Torvalds 37181da177e4SLinus Torvalds static void selinux_sk_free_security(struct sock *sk) 37191da177e4SLinus Torvalds { 37201da177e4SLinus Torvalds sk_free_security(sk); 37211da177e4SLinus Torvalds } 37221da177e4SLinus Torvalds 3723892c141eSVenkat Yekkirala static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk) 3724892c141eSVenkat Yekkirala { 3725892c141eSVenkat Yekkirala struct sk_security_struct *ssec = sk->sk_security; 3726892c141eSVenkat Yekkirala struct sk_security_struct *newssec = newsk->sk_security; 3727892c141eSVenkat Yekkirala 3728892c141eSVenkat Yekkirala newssec->sid = ssec->sid; 3729892c141eSVenkat Yekkirala newssec->peer_sid = ssec->peer_sid; 373099f59ed0SPaul Moore 37319f2ad665SPaul Moore selinux_netlbl_sk_security_clone(ssec, newssec); 3732892c141eSVenkat Yekkirala } 3733892c141eSVenkat Yekkirala 3734beb8d13bSVenkat Yekkirala static void selinux_sk_getsecid(struct sock *sk, u32 *secid) 3735d28d1e08STrent Jaeger { 3736d28d1e08STrent Jaeger if (!sk) 3737beb8d13bSVenkat Yekkirala *secid = SECINITSID_ANY_SOCKET; 3738892c141eSVenkat Yekkirala else { 3739892c141eSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 3740d28d1e08STrent Jaeger 3741beb8d13bSVenkat Yekkirala *secid = sksec->sid; 3742892c141eSVenkat Yekkirala } 3743d28d1e08STrent Jaeger } 3744d28d1e08STrent Jaeger 37459a673e56SAdrian Bunk static void selinux_sock_graft(struct sock* sk, struct socket *parent) 37464237c75cSVenkat Yekkirala { 37474237c75cSVenkat Yekkirala struct inode_security_struct *isec = SOCK_INODE(parent)->i_security; 37484237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 37494237c75cSVenkat Yekkirala 37502148ccc4SDavid Woodhouse if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 || 37512148ccc4SDavid Woodhouse sk->sk_family == PF_UNIX) 37524237c75cSVenkat Yekkirala isec->sid = sksec->sid; 37537420ed23SVenkat Yekkirala 37547420ed23SVenkat Yekkirala selinux_netlbl_sock_graft(sk, parent); 37554237c75cSVenkat Yekkirala } 37564237c75cSVenkat Yekkirala 37579a673e56SAdrian Bunk static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb, 37584237c75cSVenkat Yekkirala struct request_sock *req) 37594237c75cSVenkat Yekkirala { 37604237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 37614237c75cSVenkat Yekkirala int err; 37627420ed23SVenkat Yekkirala u32 newsid; 37634237c75cSVenkat Yekkirala u32 peersid; 37644237c75cSVenkat Yekkirala 3765f36158c4SPaul Moore selinux_skb_extlbl_sid(skb, &peersid); 3766a51c64f1SVenkat Yekkirala if (peersid == SECSID_NULL) { 3767a51c64f1SVenkat Yekkirala req->secid = sksec->sid; 37683de4bab5SPaul Moore req->peer_secid = SECSID_NULL; 3769a51c64f1SVenkat Yekkirala return 0; 3770a51c64f1SVenkat Yekkirala } 3771a51c64f1SVenkat Yekkirala 37724237c75cSVenkat Yekkirala err = security_sid_mls_copy(sksec->sid, peersid, &newsid); 37734237c75cSVenkat Yekkirala if (err) 37744237c75cSVenkat Yekkirala return err; 37754237c75cSVenkat Yekkirala 37764237c75cSVenkat Yekkirala req->secid = newsid; 37776b877699SVenkat Yekkirala req->peer_secid = peersid; 37784237c75cSVenkat Yekkirala return 0; 37794237c75cSVenkat Yekkirala } 37804237c75cSVenkat Yekkirala 37819a673e56SAdrian Bunk static void selinux_inet_csk_clone(struct sock *newsk, 37829a673e56SAdrian Bunk const struct request_sock *req) 37834237c75cSVenkat Yekkirala { 37844237c75cSVenkat Yekkirala struct sk_security_struct *newsksec = newsk->sk_security; 37854237c75cSVenkat Yekkirala 37864237c75cSVenkat Yekkirala newsksec->sid = req->secid; 37876b877699SVenkat Yekkirala newsksec->peer_sid = req->peer_secid; 37884237c75cSVenkat Yekkirala /* NOTE: Ideally, we should also get the isec->sid for the 37894237c75cSVenkat Yekkirala new socket in sync, but we don't have the isec available yet. 37904237c75cSVenkat Yekkirala So we will wait until sock_graft to do it, by which 37914237c75cSVenkat Yekkirala time it will have been created and available. */ 379299f59ed0SPaul Moore 37939f2ad665SPaul Moore /* We don't need to take any sort of lock here as we are the only 37949f2ad665SPaul Moore * thread with access to newsksec */ 37959f2ad665SPaul Moore selinux_netlbl_sk_security_reset(newsksec, req->rsk_ops->family); 37964237c75cSVenkat Yekkirala } 37974237c75cSVenkat Yekkirala 37986b877699SVenkat Yekkirala static void selinux_inet_conn_established(struct sock *sk, 37996b877699SVenkat Yekkirala struct sk_buff *skb) 38006b877699SVenkat Yekkirala { 38016b877699SVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 38026b877699SVenkat Yekkirala 3803f36158c4SPaul Moore selinux_skb_extlbl_sid(skb, &sksec->peer_sid); 38046b877699SVenkat Yekkirala } 38056b877699SVenkat Yekkirala 38069a673e56SAdrian Bunk static void selinux_req_classify_flow(const struct request_sock *req, 38079a673e56SAdrian Bunk struct flowi *fl) 38084237c75cSVenkat Yekkirala { 38094237c75cSVenkat Yekkirala fl->secid = req->secid; 38104237c75cSVenkat Yekkirala } 38114237c75cSVenkat Yekkirala 38121da177e4SLinus Torvalds static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb) 38131da177e4SLinus Torvalds { 38141da177e4SLinus Torvalds int err = 0; 38151da177e4SLinus Torvalds u32 perm; 38161da177e4SLinus Torvalds struct nlmsghdr *nlh; 38171da177e4SLinus Torvalds struct socket *sock = sk->sk_socket; 38181da177e4SLinus Torvalds struct inode_security_struct *isec = SOCK_INODE(sock)->i_security; 38191da177e4SLinus Torvalds 38201da177e4SLinus Torvalds if (skb->len < NLMSG_SPACE(0)) { 38211da177e4SLinus Torvalds err = -EINVAL; 38221da177e4SLinus Torvalds goto out; 38231da177e4SLinus Torvalds } 3824b529ccf2SArnaldo Carvalho de Melo nlh = nlmsg_hdr(skb); 38251da177e4SLinus Torvalds 38261da177e4SLinus Torvalds err = selinux_nlmsg_lookup(isec->sclass, nlh->nlmsg_type, &perm); 38271da177e4SLinus Torvalds if (err) { 38281da177e4SLinus Torvalds if (err == -EINVAL) { 38299ad9ad38SDavid Woodhouse audit_log(current->audit_context, GFP_KERNEL, AUDIT_SELINUX_ERR, 38301da177e4SLinus Torvalds "SELinux: unrecognized netlink message" 38311da177e4SLinus Torvalds " type=%hu for sclass=%hu\n", 38321da177e4SLinus Torvalds nlh->nlmsg_type, isec->sclass); 38331da177e4SLinus Torvalds if (!selinux_enforcing) 38341da177e4SLinus Torvalds err = 0; 38351da177e4SLinus Torvalds } 38361da177e4SLinus Torvalds 38371da177e4SLinus Torvalds /* Ignore */ 38381da177e4SLinus Torvalds if (err == -ENOENT) 38391da177e4SLinus Torvalds err = 0; 38401da177e4SLinus Torvalds goto out; 38411da177e4SLinus Torvalds } 38421da177e4SLinus Torvalds 38431da177e4SLinus Torvalds err = socket_has_perm(current, sock, perm); 38441da177e4SLinus Torvalds out: 38451da177e4SLinus Torvalds return err; 38461da177e4SLinus Torvalds } 38471da177e4SLinus Torvalds 38481da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER 38491da177e4SLinus Torvalds 38504e5ab4cbSJames Morris static int selinux_ip_postroute_last_compat(struct sock *sk, struct net_device *dev, 38514e5ab4cbSJames Morris struct avc_audit_data *ad, 38524e5ab4cbSJames Morris u16 family, char *addrp, int len) 38531da177e4SLinus Torvalds { 38544237c75cSVenkat Yekkirala int err = 0; 38551da177e4SLinus Torvalds u32 netif_perm, node_perm, node_sid, if_sid, send_perm = 0; 38564237c75cSVenkat Yekkirala struct socket *sock; 38574237c75cSVenkat Yekkirala struct inode *inode; 38584237c75cSVenkat Yekkirala struct inode_security_struct *isec; 38594237c75cSVenkat Yekkirala 38604237c75cSVenkat Yekkirala sock = sk->sk_socket; 38614237c75cSVenkat Yekkirala if (!sock) 38624237c75cSVenkat Yekkirala goto out; 38634237c75cSVenkat Yekkirala 38644237c75cSVenkat Yekkirala inode = SOCK_INODE(sock); 38654237c75cSVenkat Yekkirala if (!inode) 38664237c75cSVenkat Yekkirala goto out; 38674237c75cSVenkat Yekkirala 38684237c75cSVenkat Yekkirala isec = inode->i_security; 38691da177e4SLinus Torvalds 38701da177e4SLinus Torvalds err = sel_netif_sids(dev, &if_sid, NULL); 38711da177e4SLinus Torvalds if (err) 38721da177e4SLinus Torvalds goto out; 38731da177e4SLinus Torvalds 38741da177e4SLinus Torvalds switch (isec->sclass) { 38751da177e4SLinus Torvalds case SECCLASS_UDP_SOCKET: 38761da177e4SLinus Torvalds netif_perm = NETIF__UDP_SEND; 38771da177e4SLinus Torvalds node_perm = NODE__UDP_SEND; 38781da177e4SLinus Torvalds send_perm = UDP_SOCKET__SEND_MSG; 38791da177e4SLinus Torvalds break; 38801da177e4SLinus Torvalds 38811da177e4SLinus Torvalds case SECCLASS_TCP_SOCKET: 38821da177e4SLinus Torvalds netif_perm = NETIF__TCP_SEND; 38831da177e4SLinus Torvalds node_perm = NODE__TCP_SEND; 38841da177e4SLinus Torvalds send_perm = TCP_SOCKET__SEND_MSG; 38851da177e4SLinus Torvalds break; 38861da177e4SLinus Torvalds 38872ee92d46SJames Morris case SECCLASS_DCCP_SOCKET: 38882ee92d46SJames Morris netif_perm = NETIF__DCCP_SEND; 38892ee92d46SJames Morris node_perm = NODE__DCCP_SEND; 38902ee92d46SJames Morris send_perm = DCCP_SOCKET__SEND_MSG; 38912ee92d46SJames Morris break; 38922ee92d46SJames Morris 38931da177e4SLinus Torvalds default: 38941da177e4SLinus Torvalds netif_perm = NETIF__RAWIP_SEND; 38951da177e4SLinus Torvalds node_perm = NODE__RAWIP_SEND; 38961da177e4SLinus Torvalds break; 38971da177e4SLinus Torvalds } 38981da177e4SLinus Torvalds 38994e5ab4cbSJames Morris err = avc_has_perm(isec->sid, if_sid, SECCLASS_NETIF, netif_perm, ad); 39004e5ab4cbSJames Morris if (err) 39011da177e4SLinus Torvalds goto out; 39021da177e4SLinus Torvalds 39034e5ab4cbSJames Morris err = security_node_sid(family, addrp, len, &node_sid); 39044e5ab4cbSJames Morris if (err) 39051da177e4SLinus Torvalds goto out; 39061da177e4SLinus Torvalds 39074e5ab4cbSJames Morris err = avc_has_perm(isec->sid, node_sid, SECCLASS_NODE, node_perm, ad); 39084e5ab4cbSJames Morris if (err) 39091da177e4SLinus Torvalds goto out; 39101da177e4SLinus Torvalds 39111da177e4SLinus Torvalds if (send_perm) { 39121da177e4SLinus Torvalds u32 port_sid; 39131da177e4SLinus Torvalds 39141da177e4SLinus Torvalds err = security_port_sid(sk->sk_family, 39151da177e4SLinus Torvalds sk->sk_type, 39161da177e4SLinus Torvalds sk->sk_protocol, 39174e5ab4cbSJames Morris ntohs(ad->u.net.dport), 39184e5ab4cbSJames Morris &port_sid); 39194e5ab4cbSJames Morris if (err) 39201da177e4SLinus Torvalds goto out; 39211da177e4SLinus Torvalds 39221da177e4SLinus Torvalds err = avc_has_perm(isec->sid, port_sid, isec->sclass, 39234e5ab4cbSJames Morris send_perm, ad); 39244e5ab4cbSJames Morris } 39254e5ab4cbSJames Morris out: 39264e5ab4cbSJames Morris return err; 39271da177e4SLinus Torvalds } 39281da177e4SLinus Torvalds 39294e5ab4cbSJames Morris static unsigned int selinux_ip_postroute_last(unsigned int hooknum, 39304e5ab4cbSJames Morris struct sk_buff **pskb, 39314e5ab4cbSJames Morris const struct net_device *in, 39324e5ab4cbSJames Morris const struct net_device *out, 39334e5ab4cbSJames Morris int (*okfn)(struct sk_buff *), 39344e5ab4cbSJames Morris u16 family) 39354e5ab4cbSJames Morris { 39364e5ab4cbSJames Morris char *addrp; 39374e5ab4cbSJames Morris int len, err = 0; 39384e5ab4cbSJames Morris struct sock *sk; 39394e5ab4cbSJames Morris struct sk_buff *skb = *pskb; 39404e5ab4cbSJames Morris struct avc_audit_data ad; 39414e5ab4cbSJames Morris struct net_device *dev = (struct net_device *)out; 39424237c75cSVenkat Yekkirala struct sk_security_struct *sksec; 394367f83cbfSVenkat Yekkirala u8 proto; 39444e5ab4cbSJames Morris 39454e5ab4cbSJames Morris sk = skb->sk; 39464e5ab4cbSJames Morris if (!sk) 39474e5ab4cbSJames Morris goto out; 39484e5ab4cbSJames Morris 39494237c75cSVenkat Yekkirala sksec = sk->sk_security; 39504e5ab4cbSJames Morris 39514e5ab4cbSJames Morris AVC_AUDIT_DATA_INIT(&ad, NET); 39524e5ab4cbSJames Morris ad.u.net.netif = dev->name; 39534e5ab4cbSJames Morris ad.u.net.family = family; 39544e5ab4cbSJames Morris 395567f83cbfSVenkat Yekkirala err = selinux_parse_skb(skb, &ad, &addrp, &len, 0, &proto); 39564e5ab4cbSJames Morris if (err) 39574e5ab4cbSJames Morris goto out; 39584e5ab4cbSJames Morris 39594e5ab4cbSJames Morris if (selinux_compat_net) 39604237c75cSVenkat Yekkirala err = selinux_ip_postroute_last_compat(sk, dev, &ad, 39614e5ab4cbSJames Morris family, addrp, len); 39624e5ab4cbSJames Morris else 39634237c75cSVenkat Yekkirala err = avc_has_perm(sksec->sid, skb->secmark, SECCLASS_PACKET, 39644e5ab4cbSJames Morris PACKET__SEND, &ad); 39654e5ab4cbSJames Morris 39664e5ab4cbSJames Morris if (err) 3967d28d1e08STrent Jaeger goto out; 3968d28d1e08STrent Jaeger 396967f83cbfSVenkat Yekkirala err = selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto); 39701da177e4SLinus Torvalds out: 39714e5ab4cbSJames Morris return err ? NF_DROP : NF_ACCEPT; 39721da177e4SLinus Torvalds } 39731da177e4SLinus Torvalds 39741da177e4SLinus Torvalds static unsigned int selinux_ipv4_postroute_last(unsigned int hooknum, 39751da177e4SLinus Torvalds struct sk_buff **pskb, 39761da177e4SLinus Torvalds const struct net_device *in, 39771da177e4SLinus Torvalds const struct net_device *out, 39781da177e4SLinus Torvalds int (*okfn)(struct sk_buff *)) 39791da177e4SLinus Torvalds { 39801da177e4SLinus Torvalds return selinux_ip_postroute_last(hooknum, pskb, in, out, okfn, PF_INET); 39811da177e4SLinus Torvalds } 39821da177e4SLinus Torvalds 39831da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 39841da177e4SLinus Torvalds 39851da177e4SLinus Torvalds static unsigned int selinux_ipv6_postroute_last(unsigned int hooknum, 39861da177e4SLinus Torvalds struct sk_buff **pskb, 39871da177e4SLinus Torvalds const struct net_device *in, 39881da177e4SLinus Torvalds const struct net_device *out, 39891da177e4SLinus Torvalds int (*okfn)(struct sk_buff *)) 39901da177e4SLinus Torvalds { 39911da177e4SLinus Torvalds return selinux_ip_postroute_last(hooknum, pskb, in, out, okfn, PF_INET6); 39921da177e4SLinus Torvalds } 39931da177e4SLinus Torvalds 39941da177e4SLinus Torvalds #endif /* IPV6 */ 39951da177e4SLinus Torvalds 39961da177e4SLinus Torvalds #endif /* CONFIG_NETFILTER */ 39971da177e4SLinus Torvalds 39981da177e4SLinus Torvalds static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb) 39991da177e4SLinus Torvalds { 40001da177e4SLinus Torvalds int err; 40011da177e4SLinus Torvalds 40021da177e4SLinus Torvalds err = secondary_ops->netlink_send(sk, skb); 40031da177e4SLinus Torvalds if (err) 40041da177e4SLinus Torvalds return err; 40051da177e4SLinus Torvalds 40061da177e4SLinus Torvalds if (policydb_loaded_version >= POLICYDB_VERSION_NLCLASS) 40071da177e4SLinus Torvalds err = selinux_nlmsg_perm(sk, skb); 40081da177e4SLinus Torvalds 40091da177e4SLinus Torvalds return err; 40101da177e4SLinus Torvalds } 40111da177e4SLinus Torvalds 4012c7bdb545SDarrel Goeddel static int selinux_netlink_recv(struct sk_buff *skb, int capability) 40131da177e4SLinus Torvalds { 4014c7bdb545SDarrel Goeddel int err; 4015c7bdb545SDarrel Goeddel struct avc_audit_data ad; 4016c7bdb545SDarrel Goeddel 4017c7bdb545SDarrel Goeddel err = secondary_ops->netlink_recv(skb, capability); 4018c7bdb545SDarrel Goeddel if (err) 4019c7bdb545SDarrel Goeddel return err; 4020c7bdb545SDarrel Goeddel 4021c7bdb545SDarrel Goeddel AVC_AUDIT_DATA_INIT(&ad, CAP); 4022c7bdb545SDarrel Goeddel ad.u.cap = capability; 4023c7bdb545SDarrel Goeddel 4024c7bdb545SDarrel Goeddel return avc_has_perm(NETLINK_CB(skb).sid, NETLINK_CB(skb).sid, 4025c7bdb545SDarrel Goeddel SECCLASS_CAPABILITY, CAP_TO_MASK(capability), &ad); 40261da177e4SLinus Torvalds } 40271da177e4SLinus Torvalds 40281da177e4SLinus Torvalds static int ipc_alloc_security(struct task_struct *task, 40291da177e4SLinus Torvalds struct kern_ipc_perm *perm, 40301da177e4SLinus Torvalds u16 sclass) 40311da177e4SLinus Torvalds { 40321da177e4SLinus Torvalds struct task_security_struct *tsec = task->security; 40331da177e4SLinus Torvalds struct ipc_security_struct *isec; 40341da177e4SLinus Torvalds 403589d155efSJames Morris isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL); 40361da177e4SLinus Torvalds if (!isec) 40371da177e4SLinus Torvalds return -ENOMEM; 40381da177e4SLinus Torvalds 40391da177e4SLinus Torvalds isec->sclass = sclass; 40401da177e4SLinus Torvalds isec->ipc_perm = perm; 40411da177e4SLinus Torvalds isec->sid = tsec->sid; 40421da177e4SLinus Torvalds perm->security = isec; 40431da177e4SLinus Torvalds 40441da177e4SLinus Torvalds return 0; 40451da177e4SLinus Torvalds } 40461da177e4SLinus Torvalds 40471da177e4SLinus Torvalds static void ipc_free_security(struct kern_ipc_perm *perm) 40481da177e4SLinus Torvalds { 40491da177e4SLinus Torvalds struct ipc_security_struct *isec = perm->security; 40501da177e4SLinus Torvalds perm->security = NULL; 40511da177e4SLinus Torvalds kfree(isec); 40521da177e4SLinus Torvalds } 40531da177e4SLinus Torvalds 40541da177e4SLinus Torvalds static int msg_msg_alloc_security(struct msg_msg *msg) 40551da177e4SLinus Torvalds { 40561da177e4SLinus Torvalds struct msg_security_struct *msec; 40571da177e4SLinus Torvalds 405889d155efSJames Morris msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL); 40591da177e4SLinus Torvalds if (!msec) 40601da177e4SLinus Torvalds return -ENOMEM; 40611da177e4SLinus Torvalds 40621da177e4SLinus Torvalds msec->msg = msg; 40631da177e4SLinus Torvalds msec->sid = SECINITSID_UNLABELED; 40641da177e4SLinus Torvalds msg->security = msec; 40651da177e4SLinus Torvalds 40661da177e4SLinus Torvalds return 0; 40671da177e4SLinus Torvalds } 40681da177e4SLinus Torvalds 40691da177e4SLinus Torvalds static void msg_msg_free_security(struct msg_msg *msg) 40701da177e4SLinus Torvalds { 40711da177e4SLinus Torvalds struct msg_security_struct *msec = msg->security; 40721da177e4SLinus Torvalds 40731da177e4SLinus Torvalds msg->security = NULL; 40741da177e4SLinus Torvalds kfree(msec); 40751da177e4SLinus Torvalds } 40761da177e4SLinus Torvalds 40771da177e4SLinus Torvalds static int ipc_has_perm(struct kern_ipc_perm *ipc_perms, 40786af963f1SStephen Smalley u32 perms) 40791da177e4SLinus Torvalds { 40801da177e4SLinus Torvalds struct task_security_struct *tsec; 40811da177e4SLinus Torvalds struct ipc_security_struct *isec; 40821da177e4SLinus Torvalds struct avc_audit_data ad; 40831da177e4SLinus Torvalds 40841da177e4SLinus Torvalds tsec = current->security; 40851da177e4SLinus Torvalds isec = ipc_perms->security; 40861da177e4SLinus Torvalds 40871da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 40881da177e4SLinus Torvalds ad.u.ipc_id = ipc_perms->key; 40891da177e4SLinus Torvalds 40906af963f1SStephen Smalley return avc_has_perm(tsec->sid, isec->sid, isec->sclass, perms, &ad); 40911da177e4SLinus Torvalds } 40921da177e4SLinus Torvalds 40931da177e4SLinus Torvalds static int selinux_msg_msg_alloc_security(struct msg_msg *msg) 40941da177e4SLinus Torvalds { 40951da177e4SLinus Torvalds return msg_msg_alloc_security(msg); 40961da177e4SLinus Torvalds } 40971da177e4SLinus Torvalds 40981da177e4SLinus Torvalds static void selinux_msg_msg_free_security(struct msg_msg *msg) 40991da177e4SLinus Torvalds { 41001da177e4SLinus Torvalds msg_msg_free_security(msg); 41011da177e4SLinus Torvalds } 41021da177e4SLinus Torvalds 41031da177e4SLinus Torvalds /* message queue security operations */ 41041da177e4SLinus Torvalds static int selinux_msg_queue_alloc_security(struct msg_queue *msq) 41051da177e4SLinus Torvalds { 41061da177e4SLinus Torvalds struct task_security_struct *tsec; 41071da177e4SLinus Torvalds struct ipc_security_struct *isec; 41081da177e4SLinus Torvalds struct avc_audit_data ad; 41091da177e4SLinus Torvalds int rc; 41101da177e4SLinus Torvalds 41111da177e4SLinus Torvalds rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ); 41121da177e4SLinus Torvalds if (rc) 41131da177e4SLinus Torvalds return rc; 41141da177e4SLinus Torvalds 41151da177e4SLinus Torvalds tsec = current->security; 41161da177e4SLinus Torvalds isec = msq->q_perm.security; 41171da177e4SLinus Torvalds 41181da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 41191da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 41201da177e4SLinus Torvalds 41211da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_MSGQ, 41221da177e4SLinus Torvalds MSGQ__CREATE, &ad); 41231da177e4SLinus Torvalds if (rc) { 41241da177e4SLinus Torvalds ipc_free_security(&msq->q_perm); 41251da177e4SLinus Torvalds return rc; 41261da177e4SLinus Torvalds } 41271da177e4SLinus Torvalds return 0; 41281da177e4SLinus Torvalds } 41291da177e4SLinus Torvalds 41301da177e4SLinus Torvalds static void selinux_msg_queue_free_security(struct msg_queue *msq) 41311da177e4SLinus Torvalds { 41321da177e4SLinus Torvalds ipc_free_security(&msq->q_perm); 41331da177e4SLinus Torvalds } 41341da177e4SLinus Torvalds 41351da177e4SLinus Torvalds static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg) 41361da177e4SLinus Torvalds { 41371da177e4SLinus Torvalds struct task_security_struct *tsec; 41381da177e4SLinus Torvalds struct ipc_security_struct *isec; 41391da177e4SLinus Torvalds struct avc_audit_data ad; 41401da177e4SLinus Torvalds 41411da177e4SLinus Torvalds tsec = current->security; 41421da177e4SLinus Torvalds isec = msq->q_perm.security; 41431da177e4SLinus Torvalds 41441da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 41451da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 41461da177e4SLinus Torvalds 41471da177e4SLinus Torvalds return avc_has_perm(tsec->sid, isec->sid, SECCLASS_MSGQ, 41481da177e4SLinus Torvalds MSGQ__ASSOCIATE, &ad); 41491da177e4SLinus Torvalds } 41501da177e4SLinus Torvalds 41511da177e4SLinus Torvalds static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd) 41521da177e4SLinus Torvalds { 41531da177e4SLinus Torvalds int err; 41541da177e4SLinus Torvalds int perms; 41551da177e4SLinus Torvalds 41561da177e4SLinus Torvalds switch(cmd) { 41571da177e4SLinus Torvalds case IPC_INFO: 41581da177e4SLinus Torvalds case MSG_INFO: 41591da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 41601da177e4SLinus Torvalds return task_has_system(current, SYSTEM__IPC_INFO); 41611da177e4SLinus Torvalds case IPC_STAT: 41621da177e4SLinus Torvalds case MSG_STAT: 41631da177e4SLinus Torvalds perms = MSGQ__GETATTR | MSGQ__ASSOCIATE; 41641da177e4SLinus Torvalds break; 41651da177e4SLinus Torvalds case IPC_SET: 41661da177e4SLinus Torvalds perms = MSGQ__SETATTR; 41671da177e4SLinus Torvalds break; 41681da177e4SLinus Torvalds case IPC_RMID: 41691da177e4SLinus Torvalds perms = MSGQ__DESTROY; 41701da177e4SLinus Torvalds break; 41711da177e4SLinus Torvalds default: 41721da177e4SLinus Torvalds return 0; 41731da177e4SLinus Torvalds } 41741da177e4SLinus Torvalds 41756af963f1SStephen Smalley err = ipc_has_perm(&msq->q_perm, perms); 41761da177e4SLinus Torvalds return err; 41771da177e4SLinus Torvalds } 41781da177e4SLinus Torvalds 41791da177e4SLinus Torvalds static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg) 41801da177e4SLinus Torvalds { 41811da177e4SLinus Torvalds struct task_security_struct *tsec; 41821da177e4SLinus Torvalds struct ipc_security_struct *isec; 41831da177e4SLinus Torvalds struct msg_security_struct *msec; 41841da177e4SLinus Torvalds struct avc_audit_data ad; 41851da177e4SLinus Torvalds int rc; 41861da177e4SLinus Torvalds 41871da177e4SLinus Torvalds tsec = current->security; 41881da177e4SLinus Torvalds isec = msq->q_perm.security; 41891da177e4SLinus Torvalds msec = msg->security; 41901da177e4SLinus Torvalds 41911da177e4SLinus Torvalds /* 41921da177e4SLinus Torvalds * First time through, need to assign label to the message 41931da177e4SLinus Torvalds */ 41941da177e4SLinus Torvalds if (msec->sid == SECINITSID_UNLABELED) { 41951da177e4SLinus Torvalds /* 41961da177e4SLinus Torvalds * Compute new sid based on current process and 41971da177e4SLinus Torvalds * message queue this message will be stored in 41981da177e4SLinus Torvalds */ 41991da177e4SLinus Torvalds rc = security_transition_sid(tsec->sid, 42001da177e4SLinus Torvalds isec->sid, 42011da177e4SLinus Torvalds SECCLASS_MSG, 42021da177e4SLinus Torvalds &msec->sid); 42031da177e4SLinus Torvalds if (rc) 42041da177e4SLinus Torvalds return rc; 42051da177e4SLinus Torvalds } 42061da177e4SLinus Torvalds 42071da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 42081da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 42091da177e4SLinus Torvalds 42101da177e4SLinus Torvalds /* Can this process write to the queue? */ 42111da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_MSGQ, 42121da177e4SLinus Torvalds MSGQ__WRITE, &ad); 42131da177e4SLinus Torvalds if (!rc) 42141da177e4SLinus Torvalds /* Can this process send the message */ 42151da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, msec->sid, 42161da177e4SLinus Torvalds SECCLASS_MSG, MSG__SEND, &ad); 42171da177e4SLinus Torvalds if (!rc) 42181da177e4SLinus Torvalds /* Can the message be put in the queue? */ 42191da177e4SLinus Torvalds rc = avc_has_perm(msec->sid, isec->sid, 42201da177e4SLinus Torvalds SECCLASS_MSGQ, MSGQ__ENQUEUE, &ad); 42211da177e4SLinus Torvalds 42221da177e4SLinus Torvalds return rc; 42231da177e4SLinus Torvalds } 42241da177e4SLinus Torvalds 42251da177e4SLinus Torvalds static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg, 42261da177e4SLinus Torvalds struct task_struct *target, 42271da177e4SLinus Torvalds long type, int mode) 42281da177e4SLinus Torvalds { 42291da177e4SLinus Torvalds struct task_security_struct *tsec; 42301da177e4SLinus Torvalds struct ipc_security_struct *isec; 42311da177e4SLinus Torvalds struct msg_security_struct *msec; 42321da177e4SLinus Torvalds struct avc_audit_data ad; 42331da177e4SLinus Torvalds int rc; 42341da177e4SLinus Torvalds 42351da177e4SLinus Torvalds tsec = target->security; 42361da177e4SLinus Torvalds isec = msq->q_perm.security; 42371da177e4SLinus Torvalds msec = msg->security; 42381da177e4SLinus Torvalds 42391da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 42401da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 42411da177e4SLinus Torvalds 42421da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, 42431da177e4SLinus Torvalds SECCLASS_MSGQ, MSGQ__READ, &ad); 42441da177e4SLinus Torvalds if (!rc) 42451da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, msec->sid, 42461da177e4SLinus Torvalds SECCLASS_MSG, MSG__RECEIVE, &ad); 42471da177e4SLinus Torvalds return rc; 42481da177e4SLinus Torvalds } 42491da177e4SLinus Torvalds 42501da177e4SLinus Torvalds /* Shared Memory security operations */ 42511da177e4SLinus Torvalds static int selinux_shm_alloc_security(struct shmid_kernel *shp) 42521da177e4SLinus Torvalds { 42531da177e4SLinus Torvalds struct task_security_struct *tsec; 42541da177e4SLinus Torvalds struct ipc_security_struct *isec; 42551da177e4SLinus Torvalds struct avc_audit_data ad; 42561da177e4SLinus Torvalds int rc; 42571da177e4SLinus Torvalds 42581da177e4SLinus Torvalds rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM); 42591da177e4SLinus Torvalds if (rc) 42601da177e4SLinus Torvalds return rc; 42611da177e4SLinus Torvalds 42621da177e4SLinus Torvalds tsec = current->security; 42631da177e4SLinus Torvalds isec = shp->shm_perm.security; 42641da177e4SLinus Torvalds 42651da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 42661da177e4SLinus Torvalds ad.u.ipc_id = shp->shm_perm.key; 42671da177e4SLinus Torvalds 42681da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_SHM, 42691da177e4SLinus Torvalds SHM__CREATE, &ad); 42701da177e4SLinus Torvalds if (rc) { 42711da177e4SLinus Torvalds ipc_free_security(&shp->shm_perm); 42721da177e4SLinus Torvalds return rc; 42731da177e4SLinus Torvalds } 42741da177e4SLinus Torvalds return 0; 42751da177e4SLinus Torvalds } 42761da177e4SLinus Torvalds 42771da177e4SLinus Torvalds static void selinux_shm_free_security(struct shmid_kernel *shp) 42781da177e4SLinus Torvalds { 42791da177e4SLinus Torvalds ipc_free_security(&shp->shm_perm); 42801da177e4SLinus Torvalds } 42811da177e4SLinus Torvalds 42821da177e4SLinus Torvalds static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg) 42831da177e4SLinus Torvalds { 42841da177e4SLinus Torvalds struct task_security_struct *tsec; 42851da177e4SLinus Torvalds struct ipc_security_struct *isec; 42861da177e4SLinus Torvalds struct avc_audit_data ad; 42871da177e4SLinus Torvalds 42881da177e4SLinus Torvalds tsec = current->security; 42891da177e4SLinus Torvalds isec = shp->shm_perm.security; 42901da177e4SLinus Torvalds 42911da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 42921da177e4SLinus Torvalds ad.u.ipc_id = shp->shm_perm.key; 42931da177e4SLinus Torvalds 42941da177e4SLinus Torvalds return avc_has_perm(tsec->sid, isec->sid, SECCLASS_SHM, 42951da177e4SLinus Torvalds SHM__ASSOCIATE, &ad); 42961da177e4SLinus Torvalds } 42971da177e4SLinus Torvalds 42981da177e4SLinus Torvalds /* Note, at this point, shp is locked down */ 42991da177e4SLinus Torvalds static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd) 43001da177e4SLinus Torvalds { 43011da177e4SLinus Torvalds int perms; 43021da177e4SLinus Torvalds int err; 43031da177e4SLinus Torvalds 43041da177e4SLinus Torvalds switch(cmd) { 43051da177e4SLinus Torvalds case IPC_INFO: 43061da177e4SLinus Torvalds case SHM_INFO: 43071da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 43081da177e4SLinus Torvalds return task_has_system(current, SYSTEM__IPC_INFO); 43091da177e4SLinus Torvalds case IPC_STAT: 43101da177e4SLinus Torvalds case SHM_STAT: 43111da177e4SLinus Torvalds perms = SHM__GETATTR | SHM__ASSOCIATE; 43121da177e4SLinus Torvalds break; 43131da177e4SLinus Torvalds case IPC_SET: 43141da177e4SLinus Torvalds perms = SHM__SETATTR; 43151da177e4SLinus Torvalds break; 43161da177e4SLinus Torvalds case SHM_LOCK: 43171da177e4SLinus Torvalds case SHM_UNLOCK: 43181da177e4SLinus Torvalds perms = SHM__LOCK; 43191da177e4SLinus Torvalds break; 43201da177e4SLinus Torvalds case IPC_RMID: 43211da177e4SLinus Torvalds perms = SHM__DESTROY; 43221da177e4SLinus Torvalds break; 43231da177e4SLinus Torvalds default: 43241da177e4SLinus Torvalds return 0; 43251da177e4SLinus Torvalds } 43261da177e4SLinus Torvalds 43276af963f1SStephen Smalley err = ipc_has_perm(&shp->shm_perm, perms); 43281da177e4SLinus Torvalds return err; 43291da177e4SLinus Torvalds } 43301da177e4SLinus Torvalds 43311da177e4SLinus Torvalds static int selinux_shm_shmat(struct shmid_kernel *shp, 43321da177e4SLinus Torvalds char __user *shmaddr, int shmflg) 43331da177e4SLinus Torvalds { 43341da177e4SLinus Torvalds u32 perms; 43351da177e4SLinus Torvalds int rc; 43361da177e4SLinus Torvalds 43371da177e4SLinus Torvalds rc = secondary_ops->shm_shmat(shp, shmaddr, shmflg); 43381da177e4SLinus Torvalds if (rc) 43391da177e4SLinus Torvalds return rc; 43401da177e4SLinus Torvalds 43411da177e4SLinus Torvalds if (shmflg & SHM_RDONLY) 43421da177e4SLinus Torvalds perms = SHM__READ; 43431da177e4SLinus Torvalds else 43441da177e4SLinus Torvalds perms = SHM__READ | SHM__WRITE; 43451da177e4SLinus Torvalds 43466af963f1SStephen Smalley return ipc_has_perm(&shp->shm_perm, perms); 43471da177e4SLinus Torvalds } 43481da177e4SLinus Torvalds 43491da177e4SLinus Torvalds /* Semaphore security operations */ 43501da177e4SLinus Torvalds static int selinux_sem_alloc_security(struct sem_array *sma) 43511da177e4SLinus Torvalds { 43521da177e4SLinus Torvalds struct task_security_struct *tsec; 43531da177e4SLinus Torvalds struct ipc_security_struct *isec; 43541da177e4SLinus Torvalds struct avc_audit_data ad; 43551da177e4SLinus Torvalds int rc; 43561da177e4SLinus Torvalds 43571da177e4SLinus Torvalds rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM); 43581da177e4SLinus Torvalds if (rc) 43591da177e4SLinus Torvalds return rc; 43601da177e4SLinus Torvalds 43611da177e4SLinus Torvalds tsec = current->security; 43621da177e4SLinus Torvalds isec = sma->sem_perm.security; 43631da177e4SLinus Torvalds 43641da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 43651da177e4SLinus Torvalds ad.u.ipc_id = sma->sem_perm.key; 43661da177e4SLinus Torvalds 43671da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_SEM, 43681da177e4SLinus Torvalds SEM__CREATE, &ad); 43691da177e4SLinus Torvalds if (rc) { 43701da177e4SLinus Torvalds ipc_free_security(&sma->sem_perm); 43711da177e4SLinus Torvalds return rc; 43721da177e4SLinus Torvalds } 43731da177e4SLinus Torvalds return 0; 43741da177e4SLinus Torvalds } 43751da177e4SLinus Torvalds 43761da177e4SLinus Torvalds static void selinux_sem_free_security(struct sem_array *sma) 43771da177e4SLinus Torvalds { 43781da177e4SLinus Torvalds ipc_free_security(&sma->sem_perm); 43791da177e4SLinus Torvalds } 43801da177e4SLinus Torvalds 43811da177e4SLinus Torvalds static int selinux_sem_associate(struct sem_array *sma, int semflg) 43821da177e4SLinus Torvalds { 43831da177e4SLinus Torvalds struct task_security_struct *tsec; 43841da177e4SLinus Torvalds struct ipc_security_struct *isec; 43851da177e4SLinus Torvalds struct avc_audit_data ad; 43861da177e4SLinus Torvalds 43871da177e4SLinus Torvalds tsec = current->security; 43881da177e4SLinus Torvalds isec = sma->sem_perm.security; 43891da177e4SLinus Torvalds 43901da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 43911da177e4SLinus Torvalds ad.u.ipc_id = sma->sem_perm.key; 43921da177e4SLinus Torvalds 43931da177e4SLinus Torvalds return avc_has_perm(tsec->sid, isec->sid, SECCLASS_SEM, 43941da177e4SLinus Torvalds SEM__ASSOCIATE, &ad); 43951da177e4SLinus Torvalds } 43961da177e4SLinus Torvalds 43971da177e4SLinus Torvalds /* Note, at this point, sma is locked down */ 43981da177e4SLinus Torvalds static int selinux_sem_semctl(struct sem_array *sma, int cmd) 43991da177e4SLinus Torvalds { 44001da177e4SLinus Torvalds int err; 44011da177e4SLinus Torvalds u32 perms; 44021da177e4SLinus Torvalds 44031da177e4SLinus Torvalds switch(cmd) { 44041da177e4SLinus Torvalds case IPC_INFO: 44051da177e4SLinus Torvalds case SEM_INFO: 44061da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 44071da177e4SLinus Torvalds return task_has_system(current, SYSTEM__IPC_INFO); 44081da177e4SLinus Torvalds case GETPID: 44091da177e4SLinus Torvalds case GETNCNT: 44101da177e4SLinus Torvalds case GETZCNT: 44111da177e4SLinus Torvalds perms = SEM__GETATTR; 44121da177e4SLinus Torvalds break; 44131da177e4SLinus Torvalds case GETVAL: 44141da177e4SLinus Torvalds case GETALL: 44151da177e4SLinus Torvalds perms = SEM__READ; 44161da177e4SLinus Torvalds break; 44171da177e4SLinus Torvalds case SETVAL: 44181da177e4SLinus Torvalds case SETALL: 44191da177e4SLinus Torvalds perms = SEM__WRITE; 44201da177e4SLinus Torvalds break; 44211da177e4SLinus Torvalds case IPC_RMID: 44221da177e4SLinus Torvalds perms = SEM__DESTROY; 44231da177e4SLinus Torvalds break; 44241da177e4SLinus Torvalds case IPC_SET: 44251da177e4SLinus Torvalds perms = SEM__SETATTR; 44261da177e4SLinus Torvalds break; 44271da177e4SLinus Torvalds case IPC_STAT: 44281da177e4SLinus Torvalds case SEM_STAT: 44291da177e4SLinus Torvalds perms = SEM__GETATTR | SEM__ASSOCIATE; 44301da177e4SLinus Torvalds break; 44311da177e4SLinus Torvalds default: 44321da177e4SLinus Torvalds return 0; 44331da177e4SLinus Torvalds } 44341da177e4SLinus Torvalds 44356af963f1SStephen Smalley err = ipc_has_perm(&sma->sem_perm, perms); 44361da177e4SLinus Torvalds return err; 44371da177e4SLinus Torvalds } 44381da177e4SLinus Torvalds 44391da177e4SLinus Torvalds static int selinux_sem_semop(struct sem_array *sma, 44401da177e4SLinus Torvalds struct sembuf *sops, unsigned nsops, int alter) 44411da177e4SLinus Torvalds { 44421da177e4SLinus Torvalds u32 perms; 44431da177e4SLinus Torvalds 44441da177e4SLinus Torvalds if (alter) 44451da177e4SLinus Torvalds perms = SEM__READ | SEM__WRITE; 44461da177e4SLinus Torvalds else 44471da177e4SLinus Torvalds perms = SEM__READ; 44481da177e4SLinus Torvalds 44496af963f1SStephen Smalley return ipc_has_perm(&sma->sem_perm, perms); 44501da177e4SLinus Torvalds } 44511da177e4SLinus Torvalds 44521da177e4SLinus Torvalds static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag) 44531da177e4SLinus Torvalds { 44541da177e4SLinus Torvalds u32 av = 0; 44551da177e4SLinus Torvalds 44561da177e4SLinus Torvalds av = 0; 44571da177e4SLinus Torvalds if (flag & S_IRUGO) 44581da177e4SLinus Torvalds av |= IPC__UNIX_READ; 44591da177e4SLinus Torvalds if (flag & S_IWUGO) 44601da177e4SLinus Torvalds av |= IPC__UNIX_WRITE; 44611da177e4SLinus Torvalds 44621da177e4SLinus Torvalds if (av == 0) 44631da177e4SLinus Torvalds return 0; 44641da177e4SLinus Torvalds 44656af963f1SStephen Smalley return ipc_has_perm(ipcp, av); 44661da177e4SLinus Torvalds } 44671da177e4SLinus Torvalds 44681da177e4SLinus Torvalds /* module stacking operations */ 44691da177e4SLinus Torvalds static int selinux_register_security (const char *name, struct security_operations *ops) 44701da177e4SLinus Torvalds { 44711da177e4SLinus Torvalds if (secondary_ops != original_ops) { 4472fadcdb45SEric Paris printk(KERN_ERR "%s: There is already a secondary security " 44731da177e4SLinus Torvalds "module registered.\n", __FUNCTION__); 44741da177e4SLinus Torvalds return -EINVAL; 44751da177e4SLinus Torvalds } 44761da177e4SLinus Torvalds 44771da177e4SLinus Torvalds secondary_ops = ops; 44781da177e4SLinus Torvalds 44791da177e4SLinus Torvalds printk(KERN_INFO "%s: Registering secondary module %s\n", 44801da177e4SLinus Torvalds __FUNCTION__, 44811da177e4SLinus Torvalds name); 44821da177e4SLinus Torvalds 44831da177e4SLinus Torvalds return 0; 44841da177e4SLinus Torvalds } 44851da177e4SLinus Torvalds 44861da177e4SLinus Torvalds static int selinux_unregister_security (const char *name, struct security_operations *ops) 44871da177e4SLinus Torvalds { 44881da177e4SLinus Torvalds if (ops != secondary_ops) { 4489fadcdb45SEric Paris printk(KERN_ERR "%s: trying to unregister a security module " 44901da177e4SLinus Torvalds "that is not registered.\n", __FUNCTION__); 44911da177e4SLinus Torvalds return -EINVAL; 44921da177e4SLinus Torvalds } 44931da177e4SLinus Torvalds 44941da177e4SLinus Torvalds secondary_ops = original_ops; 44951da177e4SLinus Torvalds 44961da177e4SLinus Torvalds return 0; 44971da177e4SLinus Torvalds } 44981da177e4SLinus Torvalds 44991da177e4SLinus Torvalds static void selinux_d_instantiate (struct dentry *dentry, struct inode *inode) 45001da177e4SLinus Torvalds { 45011da177e4SLinus Torvalds if (inode) 45021da177e4SLinus Torvalds inode_doinit_with_dentry(inode, dentry); 45031da177e4SLinus Torvalds } 45041da177e4SLinus Torvalds 45051da177e4SLinus Torvalds static int selinux_getprocattr(struct task_struct *p, 450604ff9708SAl Viro char *name, char **value) 45071da177e4SLinus Torvalds { 45081da177e4SLinus Torvalds struct task_security_struct *tsec; 45098c8570fbSDustin Kirkland u32 sid; 45101da177e4SLinus Torvalds int error; 451104ff9708SAl Viro unsigned len; 45121da177e4SLinus Torvalds 45131da177e4SLinus Torvalds if (current != p) { 45141da177e4SLinus Torvalds error = task_has_perm(current, p, PROCESS__GETATTR); 45151da177e4SLinus Torvalds if (error) 45161da177e4SLinus Torvalds return error; 45171da177e4SLinus Torvalds } 45181da177e4SLinus Torvalds 45191da177e4SLinus Torvalds tsec = p->security; 45201da177e4SLinus Torvalds 45211da177e4SLinus Torvalds if (!strcmp(name, "current")) 45221da177e4SLinus Torvalds sid = tsec->sid; 45231da177e4SLinus Torvalds else if (!strcmp(name, "prev")) 45241da177e4SLinus Torvalds sid = tsec->osid; 45251da177e4SLinus Torvalds else if (!strcmp(name, "exec")) 45261da177e4SLinus Torvalds sid = tsec->exec_sid; 45271da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 45281da177e4SLinus Torvalds sid = tsec->create_sid; 45294eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) 45304eb582cfSMichael LeMay sid = tsec->keycreate_sid; 453142c3e03eSEric Paris else if (!strcmp(name, "sockcreate")) 453242c3e03eSEric Paris sid = tsec->sockcreate_sid; 45331da177e4SLinus Torvalds else 45341da177e4SLinus Torvalds return -EINVAL; 45351da177e4SLinus Torvalds 45361da177e4SLinus Torvalds if (!sid) 45371da177e4SLinus Torvalds return 0; 45381da177e4SLinus Torvalds 453904ff9708SAl Viro error = security_sid_to_context(sid, value, &len); 454004ff9708SAl Viro if (error) 454104ff9708SAl Viro return error; 454204ff9708SAl Viro return len; 45431da177e4SLinus Torvalds } 45441da177e4SLinus Torvalds 45451da177e4SLinus Torvalds static int selinux_setprocattr(struct task_struct *p, 45461da177e4SLinus Torvalds char *name, void *value, size_t size) 45471da177e4SLinus Torvalds { 45481da177e4SLinus Torvalds struct task_security_struct *tsec; 45491da177e4SLinus Torvalds u32 sid = 0; 45501da177e4SLinus Torvalds int error; 45511da177e4SLinus Torvalds char *str = value; 45521da177e4SLinus Torvalds 45531da177e4SLinus Torvalds if (current != p) { 45541da177e4SLinus Torvalds /* SELinux only allows a process to change its own 45551da177e4SLinus Torvalds security attributes. */ 45561da177e4SLinus Torvalds return -EACCES; 45571da177e4SLinus Torvalds } 45581da177e4SLinus Torvalds 45591da177e4SLinus Torvalds /* 45601da177e4SLinus Torvalds * Basic control over ability to set these attributes at all. 45611da177e4SLinus Torvalds * current == p, but we'll pass them separately in case the 45621da177e4SLinus Torvalds * above restriction is ever removed. 45631da177e4SLinus Torvalds */ 45641da177e4SLinus Torvalds if (!strcmp(name, "exec")) 45651da177e4SLinus Torvalds error = task_has_perm(current, p, PROCESS__SETEXEC); 45661da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 45671da177e4SLinus Torvalds error = task_has_perm(current, p, PROCESS__SETFSCREATE); 45684eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) 45694eb582cfSMichael LeMay error = task_has_perm(current, p, PROCESS__SETKEYCREATE); 457042c3e03eSEric Paris else if (!strcmp(name, "sockcreate")) 457142c3e03eSEric Paris error = task_has_perm(current, p, PROCESS__SETSOCKCREATE); 45721da177e4SLinus Torvalds else if (!strcmp(name, "current")) 45731da177e4SLinus Torvalds error = task_has_perm(current, p, PROCESS__SETCURRENT); 45741da177e4SLinus Torvalds else 45751da177e4SLinus Torvalds error = -EINVAL; 45761da177e4SLinus Torvalds if (error) 45771da177e4SLinus Torvalds return error; 45781da177e4SLinus Torvalds 45791da177e4SLinus Torvalds /* Obtain a SID for the context, if one was specified. */ 45801da177e4SLinus Torvalds if (size && str[1] && str[1] != '\n') { 45811da177e4SLinus Torvalds if (str[size-1] == '\n') { 45821da177e4SLinus Torvalds str[size-1] = 0; 45831da177e4SLinus Torvalds size--; 45841da177e4SLinus Torvalds } 45851da177e4SLinus Torvalds error = security_context_to_sid(value, size, &sid); 45861da177e4SLinus Torvalds if (error) 45871da177e4SLinus Torvalds return error; 45881da177e4SLinus Torvalds } 45891da177e4SLinus Torvalds 45901da177e4SLinus Torvalds /* Permission checking based on the specified context is 45911da177e4SLinus Torvalds performed during the actual operation (execve, 45921da177e4SLinus Torvalds open/mkdir/...), when we know the full context of the 45931da177e4SLinus Torvalds operation. See selinux_bprm_set_security for the execve 45941da177e4SLinus Torvalds checks and may_create for the file creation checks. The 45951da177e4SLinus Torvalds operation will then fail if the context is not permitted. */ 45961da177e4SLinus Torvalds tsec = p->security; 45971da177e4SLinus Torvalds if (!strcmp(name, "exec")) 45981da177e4SLinus Torvalds tsec->exec_sid = sid; 45991da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 46001da177e4SLinus Torvalds tsec->create_sid = sid; 46014eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) { 46024eb582cfSMichael LeMay error = may_create_key(sid, p); 46034eb582cfSMichael LeMay if (error) 46044eb582cfSMichael LeMay return error; 46054eb582cfSMichael LeMay tsec->keycreate_sid = sid; 460642c3e03eSEric Paris } else if (!strcmp(name, "sockcreate")) 460742c3e03eSEric Paris tsec->sockcreate_sid = sid; 460842c3e03eSEric Paris else if (!strcmp(name, "current")) { 46091da177e4SLinus Torvalds struct av_decision avd; 46101da177e4SLinus Torvalds 46111da177e4SLinus Torvalds if (sid == 0) 46121da177e4SLinus Torvalds return -EINVAL; 46131da177e4SLinus Torvalds 46141da177e4SLinus Torvalds /* Only allow single threaded processes to change context */ 46151da177e4SLinus Torvalds if (atomic_read(&p->mm->mm_users) != 1) { 46161da177e4SLinus Torvalds struct task_struct *g, *t; 46171da177e4SLinus Torvalds struct mm_struct *mm = p->mm; 46181da177e4SLinus Torvalds read_lock(&tasklist_lock); 46191da177e4SLinus Torvalds do_each_thread(g, t) 46201da177e4SLinus Torvalds if (t->mm == mm && t != p) { 46211da177e4SLinus Torvalds read_unlock(&tasklist_lock); 46221da177e4SLinus Torvalds return -EPERM; 46231da177e4SLinus Torvalds } 46241da177e4SLinus Torvalds while_each_thread(g, t); 46251da177e4SLinus Torvalds read_unlock(&tasklist_lock); 46261da177e4SLinus Torvalds } 46271da177e4SLinus Torvalds 46281da177e4SLinus Torvalds /* Check permissions for the transition. */ 46291da177e4SLinus Torvalds error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS, 46301da177e4SLinus Torvalds PROCESS__DYNTRANSITION, NULL); 46311da177e4SLinus Torvalds if (error) 46321da177e4SLinus Torvalds return error; 46331da177e4SLinus Torvalds 46341da177e4SLinus Torvalds /* Check for ptracing, and update the task SID if ok. 46351da177e4SLinus Torvalds Otherwise, leave SID unchanged and fail. */ 46361da177e4SLinus Torvalds task_lock(p); 46371da177e4SLinus Torvalds if (p->ptrace & PT_PTRACED) { 46381da177e4SLinus Torvalds error = avc_has_perm_noaudit(tsec->ptrace_sid, sid, 46391da177e4SLinus Torvalds SECCLASS_PROCESS, 46402c3c05dbSStephen Smalley PROCESS__PTRACE, 0, &avd); 46411da177e4SLinus Torvalds if (!error) 46421da177e4SLinus Torvalds tsec->sid = sid; 46431da177e4SLinus Torvalds task_unlock(p); 46441da177e4SLinus Torvalds avc_audit(tsec->ptrace_sid, sid, SECCLASS_PROCESS, 46451da177e4SLinus Torvalds PROCESS__PTRACE, &avd, error, NULL); 46461da177e4SLinus Torvalds if (error) 46471da177e4SLinus Torvalds return error; 46481da177e4SLinus Torvalds } else { 46491da177e4SLinus Torvalds tsec->sid = sid; 46501da177e4SLinus Torvalds task_unlock(p); 46511da177e4SLinus Torvalds } 46521da177e4SLinus Torvalds } 46531da177e4SLinus Torvalds else 46541da177e4SLinus Torvalds return -EINVAL; 46551da177e4SLinus Torvalds 46561da177e4SLinus Torvalds return size; 46571da177e4SLinus Torvalds } 46581da177e4SLinus Torvalds 4659dc49c1f9SCatherine Zhang static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) 4660dc49c1f9SCatherine Zhang { 4661dc49c1f9SCatherine Zhang return security_sid_to_context(secid, secdata, seclen); 4662dc49c1f9SCatherine Zhang } 4663dc49c1f9SCatherine Zhang 4664dc49c1f9SCatherine Zhang static void selinux_release_secctx(char *secdata, u32 seclen) 4665dc49c1f9SCatherine Zhang { 4666dc49c1f9SCatherine Zhang kfree(secdata); 4667dc49c1f9SCatherine Zhang } 4668dc49c1f9SCatherine Zhang 4669d720024eSMichael LeMay #ifdef CONFIG_KEYS 4670d720024eSMichael LeMay 46717e047ef5SDavid Howells static int selinux_key_alloc(struct key *k, struct task_struct *tsk, 46727e047ef5SDavid Howells unsigned long flags) 4673d720024eSMichael LeMay { 4674d720024eSMichael LeMay struct task_security_struct *tsec = tsk->security; 4675d720024eSMichael LeMay struct key_security_struct *ksec; 4676d720024eSMichael LeMay 4677d720024eSMichael LeMay ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL); 4678d720024eSMichael LeMay if (!ksec) 4679d720024eSMichael LeMay return -ENOMEM; 4680d720024eSMichael LeMay 4681d720024eSMichael LeMay ksec->obj = k; 46824eb582cfSMichael LeMay if (tsec->keycreate_sid) 46834eb582cfSMichael LeMay ksec->sid = tsec->keycreate_sid; 46844eb582cfSMichael LeMay else 4685d720024eSMichael LeMay ksec->sid = tsec->sid; 4686d720024eSMichael LeMay k->security = ksec; 4687d720024eSMichael LeMay 4688d720024eSMichael LeMay return 0; 4689d720024eSMichael LeMay } 4690d720024eSMichael LeMay 4691d720024eSMichael LeMay static void selinux_key_free(struct key *k) 4692d720024eSMichael LeMay { 4693d720024eSMichael LeMay struct key_security_struct *ksec = k->security; 4694d720024eSMichael LeMay 4695d720024eSMichael LeMay k->security = NULL; 4696d720024eSMichael LeMay kfree(ksec); 4697d720024eSMichael LeMay } 4698d720024eSMichael LeMay 4699d720024eSMichael LeMay static int selinux_key_permission(key_ref_t key_ref, 4700d720024eSMichael LeMay struct task_struct *ctx, 4701d720024eSMichael LeMay key_perm_t perm) 4702d720024eSMichael LeMay { 4703d720024eSMichael LeMay struct key *key; 4704d720024eSMichael LeMay struct task_security_struct *tsec; 4705d720024eSMichael LeMay struct key_security_struct *ksec; 4706d720024eSMichael LeMay 4707d720024eSMichael LeMay key = key_ref_to_ptr(key_ref); 4708d720024eSMichael LeMay 4709d720024eSMichael LeMay tsec = ctx->security; 4710d720024eSMichael LeMay ksec = key->security; 4711d720024eSMichael LeMay 4712d720024eSMichael LeMay /* if no specific permissions are requested, we skip the 4713d720024eSMichael LeMay permission check. No serious, additional covert channels 4714d720024eSMichael LeMay appear to be created. */ 4715d720024eSMichael LeMay if (perm == 0) 4716d720024eSMichael LeMay return 0; 4717d720024eSMichael LeMay 4718d720024eSMichael LeMay return avc_has_perm(tsec->sid, ksec->sid, 4719d720024eSMichael LeMay SECCLASS_KEY, perm, NULL); 4720d720024eSMichael LeMay } 4721d720024eSMichael LeMay 4722d720024eSMichael LeMay #endif 4723d720024eSMichael LeMay 47241da177e4SLinus Torvalds static struct security_operations selinux_ops = { 47251da177e4SLinus Torvalds .ptrace = selinux_ptrace, 47261da177e4SLinus Torvalds .capget = selinux_capget, 47271da177e4SLinus Torvalds .capset_check = selinux_capset_check, 47281da177e4SLinus Torvalds .capset_set = selinux_capset_set, 47291da177e4SLinus Torvalds .sysctl = selinux_sysctl, 47301da177e4SLinus Torvalds .capable = selinux_capable, 47311da177e4SLinus Torvalds .quotactl = selinux_quotactl, 47321da177e4SLinus Torvalds .quota_on = selinux_quota_on, 47331da177e4SLinus Torvalds .syslog = selinux_syslog, 47341da177e4SLinus Torvalds .vm_enough_memory = selinux_vm_enough_memory, 47351da177e4SLinus Torvalds 47361da177e4SLinus Torvalds .netlink_send = selinux_netlink_send, 47371da177e4SLinus Torvalds .netlink_recv = selinux_netlink_recv, 47381da177e4SLinus Torvalds 47391da177e4SLinus Torvalds .bprm_alloc_security = selinux_bprm_alloc_security, 47401da177e4SLinus Torvalds .bprm_free_security = selinux_bprm_free_security, 47411da177e4SLinus Torvalds .bprm_apply_creds = selinux_bprm_apply_creds, 47421da177e4SLinus Torvalds .bprm_post_apply_creds = selinux_bprm_post_apply_creds, 47431da177e4SLinus Torvalds .bprm_set_security = selinux_bprm_set_security, 47441da177e4SLinus Torvalds .bprm_check_security = selinux_bprm_check_security, 47451da177e4SLinus Torvalds .bprm_secureexec = selinux_bprm_secureexec, 47461da177e4SLinus Torvalds 47471da177e4SLinus Torvalds .sb_alloc_security = selinux_sb_alloc_security, 47481da177e4SLinus Torvalds .sb_free_security = selinux_sb_free_security, 47491da177e4SLinus Torvalds .sb_copy_data = selinux_sb_copy_data, 47501da177e4SLinus Torvalds .sb_kern_mount = selinux_sb_kern_mount, 47511da177e4SLinus Torvalds .sb_statfs = selinux_sb_statfs, 47521da177e4SLinus Torvalds .sb_mount = selinux_mount, 47531da177e4SLinus Torvalds .sb_umount = selinux_umount, 47541da177e4SLinus Torvalds 47551da177e4SLinus Torvalds .inode_alloc_security = selinux_inode_alloc_security, 47561da177e4SLinus Torvalds .inode_free_security = selinux_inode_free_security, 47575e41ff9eSStephen Smalley .inode_init_security = selinux_inode_init_security, 47581da177e4SLinus Torvalds .inode_create = selinux_inode_create, 47591da177e4SLinus Torvalds .inode_link = selinux_inode_link, 47601da177e4SLinus Torvalds .inode_unlink = selinux_inode_unlink, 47611da177e4SLinus Torvalds .inode_symlink = selinux_inode_symlink, 47621da177e4SLinus Torvalds .inode_mkdir = selinux_inode_mkdir, 47631da177e4SLinus Torvalds .inode_rmdir = selinux_inode_rmdir, 47641da177e4SLinus Torvalds .inode_mknod = selinux_inode_mknod, 47651da177e4SLinus Torvalds .inode_rename = selinux_inode_rename, 47661da177e4SLinus Torvalds .inode_readlink = selinux_inode_readlink, 47671da177e4SLinus Torvalds .inode_follow_link = selinux_inode_follow_link, 47681da177e4SLinus Torvalds .inode_permission = selinux_inode_permission, 47691da177e4SLinus Torvalds .inode_setattr = selinux_inode_setattr, 47701da177e4SLinus Torvalds .inode_getattr = selinux_inode_getattr, 47711da177e4SLinus Torvalds .inode_setxattr = selinux_inode_setxattr, 47721da177e4SLinus Torvalds .inode_post_setxattr = selinux_inode_post_setxattr, 47731da177e4SLinus Torvalds .inode_getxattr = selinux_inode_getxattr, 47741da177e4SLinus Torvalds .inode_listxattr = selinux_inode_listxattr, 47751da177e4SLinus Torvalds .inode_removexattr = selinux_inode_removexattr, 47768c8570fbSDustin Kirkland .inode_xattr_getsuffix = selinux_inode_xattr_getsuffix, 47771da177e4SLinus Torvalds .inode_getsecurity = selinux_inode_getsecurity, 47781da177e4SLinus Torvalds .inode_setsecurity = selinux_inode_setsecurity, 47791da177e4SLinus Torvalds .inode_listsecurity = selinux_inode_listsecurity, 47801da177e4SLinus Torvalds 47811da177e4SLinus Torvalds .file_permission = selinux_file_permission, 47821da177e4SLinus Torvalds .file_alloc_security = selinux_file_alloc_security, 47831da177e4SLinus Torvalds .file_free_security = selinux_file_free_security, 47841da177e4SLinus Torvalds .file_ioctl = selinux_file_ioctl, 47851da177e4SLinus Torvalds .file_mmap = selinux_file_mmap, 47861da177e4SLinus Torvalds .file_mprotect = selinux_file_mprotect, 47871da177e4SLinus Torvalds .file_lock = selinux_file_lock, 47881da177e4SLinus Torvalds .file_fcntl = selinux_file_fcntl, 47891da177e4SLinus Torvalds .file_set_fowner = selinux_file_set_fowner, 47901da177e4SLinus Torvalds .file_send_sigiotask = selinux_file_send_sigiotask, 47911da177e4SLinus Torvalds .file_receive = selinux_file_receive, 47921da177e4SLinus Torvalds 47931da177e4SLinus Torvalds .task_create = selinux_task_create, 47941da177e4SLinus Torvalds .task_alloc_security = selinux_task_alloc_security, 47951da177e4SLinus Torvalds .task_free_security = selinux_task_free_security, 47961da177e4SLinus Torvalds .task_setuid = selinux_task_setuid, 47971da177e4SLinus Torvalds .task_post_setuid = selinux_task_post_setuid, 47981da177e4SLinus Torvalds .task_setgid = selinux_task_setgid, 47991da177e4SLinus Torvalds .task_setpgid = selinux_task_setpgid, 48001da177e4SLinus Torvalds .task_getpgid = selinux_task_getpgid, 48011da177e4SLinus Torvalds .task_getsid = selinux_task_getsid, 4802f9008e4cSDavid Quigley .task_getsecid = selinux_task_getsecid, 48031da177e4SLinus Torvalds .task_setgroups = selinux_task_setgroups, 48041da177e4SLinus Torvalds .task_setnice = selinux_task_setnice, 480503e68060SJames Morris .task_setioprio = selinux_task_setioprio, 4806a1836a42SDavid Quigley .task_getioprio = selinux_task_getioprio, 48071da177e4SLinus Torvalds .task_setrlimit = selinux_task_setrlimit, 48081da177e4SLinus Torvalds .task_setscheduler = selinux_task_setscheduler, 48091da177e4SLinus Torvalds .task_getscheduler = selinux_task_getscheduler, 481035601547SDavid Quigley .task_movememory = selinux_task_movememory, 48111da177e4SLinus Torvalds .task_kill = selinux_task_kill, 48121da177e4SLinus Torvalds .task_wait = selinux_task_wait, 48131da177e4SLinus Torvalds .task_prctl = selinux_task_prctl, 48141da177e4SLinus Torvalds .task_reparent_to_init = selinux_task_reparent_to_init, 48151da177e4SLinus Torvalds .task_to_inode = selinux_task_to_inode, 48161da177e4SLinus Torvalds 48171da177e4SLinus Torvalds .ipc_permission = selinux_ipc_permission, 48181da177e4SLinus Torvalds 48191da177e4SLinus Torvalds .msg_msg_alloc_security = selinux_msg_msg_alloc_security, 48201da177e4SLinus Torvalds .msg_msg_free_security = selinux_msg_msg_free_security, 48211da177e4SLinus Torvalds 48221da177e4SLinus Torvalds .msg_queue_alloc_security = selinux_msg_queue_alloc_security, 48231da177e4SLinus Torvalds .msg_queue_free_security = selinux_msg_queue_free_security, 48241da177e4SLinus Torvalds .msg_queue_associate = selinux_msg_queue_associate, 48251da177e4SLinus Torvalds .msg_queue_msgctl = selinux_msg_queue_msgctl, 48261da177e4SLinus Torvalds .msg_queue_msgsnd = selinux_msg_queue_msgsnd, 48271da177e4SLinus Torvalds .msg_queue_msgrcv = selinux_msg_queue_msgrcv, 48281da177e4SLinus Torvalds 48291da177e4SLinus Torvalds .shm_alloc_security = selinux_shm_alloc_security, 48301da177e4SLinus Torvalds .shm_free_security = selinux_shm_free_security, 48311da177e4SLinus Torvalds .shm_associate = selinux_shm_associate, 48321da177e4SLinus Torvalds .shm_shmctl = selinux_shm_shmctl, 48331da177e4SLinus Torvalds .shm_shmat = selinux_shm_shmat, 48341da177e4SLinus Torvalds 48351da177e4SLinus Torvalds .sem_alloc_security = selinux_sem_alloc_security, 48361da177e4SLinus Torvalds .sem_free_security = selinux_sem_free_security, 48371da177e4SLinus Torvalds .sem_associate = selinux_sem_associate, 48381da177e4SLinus Torvalds .sem_semctl = selinux_sem_semctl, 48391da177e4SLinus Torvalds .sem_semop = selinux_sem_semop, 48401da177e4SLinus Torvalds 48411da177e4SLinus Torvalds .register_security = selinux_register_security, 48421da177e4SLinus Torvalds .unregister_security = selinux_unregister_security, 48431da177e4SLinus Torvalds 48441da177e4SLinus Torvalds .d_instantiate = selinux_d_instantiate, 48451da177e4SLinus Torvalds 48461da177e4SLinus Torvalds .getprocattr = selinux_getprocattr, 48471da177e4SLinus Torvalds .setprocattr = selinux_setprocattr, 48481da177e4SLinus Torvalds 4849dc49c1f9SCatherine Zhang .secid_to_secctx = selinux_secid_to_secctx, 4850dc49c1f9SCatherine Zhang .release_secctx = selinux_release_secctx, 4851dc49c1f9SCatherine Zhang 48521da177e4SLinus Torvalds .unix_stream_connect = selinux_socket_unix_stream_connect, 48531da177e4SLinus Torvalds .unix_may_send = selinux_socket_unix_may_send, 48541da177e4SLinus Torvalds 48551da177e4SLinus Torvalds .socket_create = selinux_socket_create, 48561da177e4SLinus Torvalds .socket_post_create = selinux_socket_post_create, 48571da177e4SLinus Torvalds .socket_bind = selinux_socket_bind, 48581da177e4SLinus Torvalds .socket_connect = selinux_socket_connect, 48591da177e4SLinus Torvalds .socket_listen = selinux_socket_listen, 48601da177e4SLinus Torvalds .socket_accept = selinux_socket_accept, 48611da177e4SLinus Torvalds .socket_sendmsg = selinux_socket_sendmsg, 48621da177e4SLinus Torvalds .socket_recvmsg = selinux_socket_recvmsg, 48631da177e4SLinus Torvalds .socket_getsockname = selinux_socket_getsockname, 48641da177e4SLinus Torvalds .socket_getpeername = selinux_socket_getpeername, 48651da177e4SLinus Torvalds .socket_getsockopt = selinux_socket_getsockopt, 48661da177e4SLinus Torvalds .socket_setsockopt = selinux_socket_setsockopt, 48671da177e4SLinus Torvalds .socket_shutdown = selinux_socket_shutdown, 48681da177e4SLinus Torvalds .socket_sock_rcv_skb = selinux_socket_sock_rcv_skb, 48692c7946a7SCatherine Zhang .socket_getpeersec_stream = selinux_socket_getpeersec_stream, 48702c7946a7SCatherine Zhang .socket_getpeersec_dgram = selinux_socket_getpeersec_dgram, 48711da177e4SLinus Torvalds .sk_alloc_security = selinux_sk_alloc_security, 48721da177e4SLinus Torvalds .sk_free_security = selinux_sk_free_security, 4873892c141eSVenkat Yekkirala .sk_clone_security = selinux_sk_clone_security, 4874beb8d13bSVenkat Yekkirala .sk_getsecid = selinux_sk_getsecid, 48754237c75cSVenkat Yekkirala .sock_graft = selinux_sock_graft, 48764237c75cSVenkat Yekkirala .inet_conn_request = selinux_inet_conn_request, 48774237c75cSVenkat Yekkirala .inet_csk_clone = selinux_inet_csk_clone, 48786b877699SVenkat Yekkirala .inet_conn_established = selinux_inet_conn_established, 48794237c75cSVenkat Yekkirala .req_classify_flow = selinux_req_classify_flow, 4880d28d1e08STrent Jaeger 4881d28d1e08STrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM 4882d28d1e08STrent Jaeger .xfrm_policy_alloc_security = selinux_xfrm_policy_alloc, 4883d28d1e08STrent Jaeger .xfrm_policy_clone_security = selinux_xfrm_policy_clone, 4884d28d1e08STrent Jaeger .xfrm_policy_free_security = selinux_xfrm_policy_free, 4885c8c05a8eSCatherine Zhang .xfrm_policy_delete_security = selinux_xfrm_policy_delete, 4886d28d1e08STrent Jaeger .xfrm_state_alloc_security = selinux_xfrm_state_alloc, 4887d28d1e08STrent Jaeger .xfrm_state_free_security = selinux_xfrm_state_free, 4888c8c05a8eSCatherine Zhang .xfrm_state_delete_security = selinux_xfrm_state_delete, 4889d28d1e08STrent Jaeger .xfrm_policy_lookup = selinux_xfrm_policy_lookup, 4890e0d1caa7SVenkat Yekkirala .xfrm_state_pol_flow_match = selinux_xfrm_state_pol_flow_match, 4891e0d1caa7SVenkat Yekkirala .xfrm_decode_session = selinux_xfrm_decode_session, 48921da177e4SLinus Torvalds #endif 4893d720024eSMichael LeMay 4894d720024eSMichael LeMay #ifdef CONFIG_KEYS 4895d720024eSMichael LeMay .key_alloc = selinux_key_alloc, 4896d720024eSMichael LeMay .key_free = selinux_key_free, 4897d720024eSMichael LeMay .key_permission = selinux_key_permission, 4898d720024eSMichael LeMay #endif 48991da177e4SLinus Torvalds }; 49001da177e4SLinus Torvalds 49011da177e4SLinus Torvalds static __init int selinux_init(void) 49021da177e4SLinus Torvalds { 49031da177e4SLinus Torvalds struct task_security_struct *tsec; 49041da177e4SLinus Torvalds 49051da177e4SLinus Torvalds if (!selinux_enabled) { 49061da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Disabled at boot.\n"); 49071da177e4SLinus Torvalds return 0; 49081da177e4SLinus Torvalds } 49091da177e4SLinus Torvalds 49101da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Initializing.\n"); 49111da177e4SLinus Torvalds 49121da177e4SLinus Torvalds /* Set the security state for the initial task. */ 49131da177e4SLinus Torvalds if (task_alloc_security(current)) 49141da177e4SLinus Torvalds panic("SELinux: Failed to initialize initial task.\n"); 49151da177e4SLinus Torvalds tsec = current->security; 49161da177e4SLinus Torvalds tsec->osid = tsec->sid = SECINITSID_KERNEL; 49171da177e4SLinus Torvalds 49187cae7e26SJames Morris sel_inode_cache = kmem_cache_create("selinux_inode_security", 49197cae7e26SJames Morris sizeof(struct inode_security_struct), 492020c2df83SPaul Mundt 0, SLAB_PANIC, NULL); 49211da177e4SLinus Torvalds avc_init(); 49221da177e4SLinus Torvalds 49231da177e4SLinus Torvalds original_ops = secondary_ops = security_ops; 49241da177e4SLinus Torvalds if (!secondary_ops) 49251da177e4SLinus Torvalds panic ("SELinux: No initial security operations\n"); 49261da177e4SLinus Torvalds if (register_security (&selinux_ops)) 49271da177e4SLinus Torvalds panic("SELinux: Unable to register with kernel.\n"); 49281da177e4SLinus Torvalds 49291da177e4SLinus Torvalds if (selinux_enforcing) { 4930fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Starting in enforcing mode\n"); 49311da177e4SLinus Torvalds } else { 4932fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Starting in permissive mode\n"); 49331da177e4SLinus Torvalds } 4934d720024eSMichael LeMay 4935d720024eSMichael LeMay #ifdef CONFIG_KEYS 4936d720024eSMichael LeMay /* Add security information to initial keyrings */ 49374eb582cfSMichael LeMay selinux_key_alloc(&root_user_keyring, current, 49387e047ef5SDavid Howells KEY_ALLOC_NOT_IN_QUOTA); 49394eb582cfSMichael LeMay selinux_key_alloc(&root_session_keyring, current, 49407e047ef5SDavid Howells KEY_ALLOC_NOT_IN_QUOTA); 4941d720024eSMichael LeMay #endif 4942d720024eSMichael LeMay 49431da177e4SLinus Torvalds return 0; 49441da177e4SLinus Torvalds } 49451da177e4SLinus Torvalds 49461da177e4SLinus Torvalds void selinux_complete_init(void) 49471da177e4SLinus Torvalds { 4948fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Completing initialization.\n"); 49491da177e4SLinus Torvalds 49501da177e4SLinus Torvalds /* Set up any superblocks initialized prior to the policy load. */ 4951fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Setting up existing superblocks.\n"); 4952ba0c19edSStephen Smalley spin_lock(&sb_lock); 49531da177e4SLinus Torvalds spin_lock(&sb_security_lock); 49541da177e4SLinus Torvalds next_sb: 49551da177e4SLinus Torvalds if (!list_empty(&superblock_security_head)) { 49561da177e4SLinus Torvalds struct superblock_security_struct *sbsec = 49571da177e4SLinus Torvalds list_entry(superblock_security_head.next, 49581da177e4SLinus Torvalds struct superblock_security_struct, 49591da177e4SLinus Torvalds list); 49601da177e4SLinus Torvalds struct super_block *sb = sbsec->sb; 49611da177e4SLinus Torvalds sb->s_count++; 49621da177e4SLinus Torvalds spin_unlock(&sb_security_lock); 4963ba0c19edSStephen Smalley spin_unlock(&sb_lock); 49641da177e4SLinus Torvalds down_read(&sb->s_umount); 49651da177e4SLinus Torvalds if (sb->s_root) 49661da177e4SLinus Torvalds superblock_doinit(sb, NULL); 49671da177e4SLinus Torvalds drop_super(sb); 4968ba0c19edSStephen Smalley spin_lock(&sb_lock); 49691da177e4SLinus Torvalds spin_lock(&sb_security_lock); 49701da177e4SLinus Torvalds list_del_init(&sbsec->list); 49711da177e4SLinus Torvalds goto next_sb; 49721da177e4SLinus Torvalds } 49731da177e4SLinus Torvalds spin_unlock(&sb_security_lock); 4974ba0c19edSStephen Smalley spin_unlock(&sb_lock); 49751da177e4SLinus Torvalds } 49761da177e4SLinus Torvalds 49771da177e4SLinus Torvalds /* SELinux requires early initialization in order to label 49781da177e4SLinus Torvalds all processes and objects when they are created. */ 49791da177e4SLinus Torvalds security_initcall(selinux_init); 49801da177e4SLinus Torvalds 4981c2b507fdSStephen Smalley #if defined(CONFIG_NETFILTER) 49821da177e4SLinus Torvalds 49831da177e4SLinus Torvalds static struct nf_hook_ops selinux_ipv4_op = { 49841da177e4SLinus Torvalds .hook = selinux_ipv4_postroute_last, 49851da177e4SLinus Torvalds .owner = THIS_MODULE, 49861da177e4SLinus Torvalds .pf = PF_INET, 49871da177e4SLinus Torvalds .hooknum = NF_IP_POST_ROUTING, 49881da177e4SLinus Torvalds .priority = NF_IP_PRI_SELINUX_LAST, 49891da177e4SLinus Torvalds }; 49901da177e4SLinus Torvalds 49911da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 49921da177e4SLinus Torvalds 49931da177e4SLinus Torvalds static struct nf_hook_ops selinux_ipv6_op = { 49941da177e4SLinus Torvalds .hook = selinux_ipv6_postroute_last, 49951da177e4SLinus Torvalds .owner = THIS_MODULE, 49961da177e4SLinus Torvalds .pf = PF_INET6, 49971da177e4SLinus Torvalds .hooknum = NF_IP6_POST_ROUTING, 49981da177e4SLinus Torvalds .priority = NF_IP6_PRI_SELINUX_LAST, 49991da177e4SLinus Torvalds }; 50001da177e4SLinus Torvalds 50011da177e4SLinus Torvalds #endif /* IPV6 */ 50021da177e4SLinus Torvalds 50031da177e4SLinus Torvalds static int __init selinux_nf_ip_init(void) 50041da177e4SLinus Torvalds { 50051da177e4SLinus Torvalds int err = 0; 50061da177e4SLinus Torvalds 50071da177e4SLinus Torvalds if (!selinux_enabled) 50081da177e4SLinus Torvalds goto out; 50091da177e4SLinus Torvalds 5010fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Registering netfilter hooks\n"); 50111da177e4SLinus Torvalds 50121da177e4SLinus Torvalds err = nf_register_hook(&selinux_ipv4_op); 50131da177e4SLinus Torvalds if (err) 50141da177e4SLinus Torvalds panic("SELinux: nf_register_hook for IPv4: error %d\n", err); 50151da177e4SLinus Torvalds 50161da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 50171da177e4SLinus Torvalds 50181da177e4SLinus Torvalds err = nf_register_hook(&selinux_ipv6_op); 50191da177e4SLinus Torvalds if (err) 50201da177e4SLinus Torvalds panic("SELinux: nf_register_hook for IPv6: error %d\n", err); 50211da177e4SLinus Torvalds 50221da177e4SLinus Torvalds #endif /* IPV6 */ 5023d28d1e08STrent Jaeger 50241da177e4SLinus Torvalds out: 50251da177e4SLinus Torvalds return err; 50261da177e4SLinus Torvalds } 50271da177e4SLinus Torvalds 50281da177e4SLinus Torvalds __initcall(selinux_nf_ip_init); 50291da177e4SLinus Torvalds 50301da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 50311da177e4SLinus Torvalds static void selinux_nf_ip_exit(void) 50321da177e4SLinus Torvalds { 5033fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Unregistering netfilter hooks\n"); 50341da177e4SLinus Torvalds 50351da177e4SLinus Torvalds nf_unregister_hook(&selinux_ipv4_op); 50361da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 50371da177e4SLinus Torvalds nf_unregister_hook(&selinux_ipv6_op); 50381da177e4SLinus Torvalds #endif /* IPV6 */ 50391da177e4SLinus Torvalds } 50401da177e4SLinus Torvalds #endif 50411da177e4SLinus Torvalds 5042c2b507fdSStephen Smalley #else /* CONFIG_NETFILTER */ 50431da177e4SLinus Torvalds 50441da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 50451da177e4SLinus Torvalds #define selinux_nf_ip_exit() 50461da177e4SLinus Torvalds #endif 50471da177e4SLinus Torvalds 5048c2b507fdSStephen Smalley #endif /* CONFIG_NETFILTER */ 50491da177e4SLinus Torvalds 50501da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 50511da177e4SLinus Torvalds int selinux_disable(void) 50521da177e4SLinus Torvalds { 50531da177e4SLinus Torvalds extern void exit_sel_fs(void); 50541da177e4SLinus Torvalds static int selinux_disabled = 0; 50551da177e4SLinus Torvalds 50561da177e4SLinus Torvalds if (ss_initialized) { 50571da177e4SLinus Torvalds /* Not permitted after initial policy load. */ 50581da177e4SLinus Torvalds return -EINVAL; 50591da177e4SLinus Torvalds } 50601da177e4SLinus Torvalds 50611da177e4SLinus Torvalds if (selinux_disabled) { 50621da177e4SLinus Torvalds /* Only do this once. */ 50631da177e4SLinus Torvalds return -EINVAL; 50641da177e4SLinus Torvalds } 50651da177e4SLinus Torvalds 50661da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Disabled at runtime.\n"); 50671da177e4SLinus Torvalds 50681da177e4SLinus Torvalds selinux_disabled = 1; 506930d55280SStephen Smalley selinux_enabled = 0; 50701da177e4SLinus Torvalds 50711da177e4SLinus Torvalds /* Reset security_ops to the secondary module, dummy or capability. */ 50721da177e4SLinus Torvalds security_ops = secondary_ops; 50731da177e4SLinus Torvalds 50741da177e4SLinus Torvalds /* Unregister netfilter hooks. */ 50751da177e4SLinus Torvalds selinux_nf_ip_exit(); 50761da177e4SLinus Torvalds 50771da177e4SLinus Torvalds /* Unregister selinuxfs. */ 50781da177e4SLinus Torvalds exit_sel_fs(); 50791da177e4SLinus Torvalds 50801da177e4SLinus Torvalds return 0; 50811da177e4SLinus Torvalds } 50821da177e4SLinus Torvalds #endif 50831da177e4SLinus Torvalds 50841da177e4SLinus Torvalds 5085