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> 15effad8dfSPaul Moore * Copyright (C) 2006, 2007 Hewlett-Packard Development Company, L.P. 16effad8dfSPaul Moore * Paul Moore <paul.moore@hp.com> 17788e7dd4SYuichi Nakamura * Copyright (C) 2007 Hitachi Software Engineering Co., Ltd. 18788e7dd4SYuichi Nakamura * Yuichi Nakamura <ynakam@hitachisoft.jp> 191da177e4SLinus Torvalds * 201da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 211da177e4SLinus Torvalds * it under the terms of the GNU General Public License version 2, 221da177e4SLinus Torvalds * as published by the Free Software Foundation. 231da177e4SLinus Torvalds */ 241da177e4SLinus Torvalds 251da177e4SLinus Torvalds #include <linux/init.h> 261da177e4SLinus Torvalds #include <linux/kernel.h> 271da177e4SLinus Torvalds #include <linux/ptrace.h> 281da177e4SLinus Torvalds #include <linux/errno.h> 291da177e4SLinus Torvalds #include <linux/sched.h> 301da177e4SLinus Torvalds #include <linux/security.h> 311da177e4SLinus Torvalds #include <linux/xattr.h> 321da177e4SLinus Torvalds #include <linux/capability.h> 331da177e4SLinus Torvalds #include <linux/unistd.h> 341da177e4SLinus Torvalds #include <linux/mm.h> 351da177e4SLinus Torvalds #include <linux/mman.h> 361da177e4SLinus Torvalds #include <linux/slab.h> 371da177e4SLinus Torvalds #include <linux/pagemap.h> 381da177e4SLinus Torvalds #include <linux/swap.h> 391da177e4SLinus Torvalds #include <linux/spinlock.h> 401da177e4SLinus Torvalds #include <linux/syscalls.h> 411da177e4SLinus Torvalds #include <linux/file.h> 421da177e4SLinus Torvalds #include <linux/namei.h> 431da177e4SLinus Torvalds #include <linux/mount.h> 441da177e4SLinus Torvalds #include <linux/ext2_fs.h> 451da177e4SLinus Torvalds #include <linux/proc_fs.h> 461da177e4SLinus Torvalds #include <linux/kd.h> 471da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h> 481da177e4SLinus Torvalds #include <linux/netfilter_ipv6.h> 491da177e4SLinus Torvalds #include <linux/tty.h> 501da177e4SLinus Torvalds #include <net/icmp.h> 51227b60f5SStephen Hemminger #include <net/ip.h> /* for local_port_range[] */ 521da177e4SLinus Torvalds #include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */ 53220deb96SPaul Moore #include <net/net_namespace.h> 54d621d35eSPaul Moore #include <net/netlabel.h> 551da177e4SLinus Torvalds #include <asm/uaccess.h> 561da177e4SLinus Torvalds #include <asm/ioctls.h> 57d621d35eSPaul Moore #include <asm/atomic.h> 581da177e4SLinus Torvalds #include <linux/bitops.h> 591da177e4SLinus Torvalds #include <linux/interrupt.h> 601da177e4SLinus Torvalds #include <linux/netdevice.h> /* for network interface checks */ 611da177e4SLinus Torvalds #include <linux/netlink.h> 621da177e4SLinus Torvalds #include <linux/tcp.h> 631da177e4SLinus Torvalds #include <linux/udp.h> 642ee92d46SJames Morris #include <linux/dccp.h> 651da177e4SLinus Torvalds #include <linux/quota.h> 661da177e4SLinus Torvalds #include <linux/un.h> /* for Unix socket types */ 671da177e4SLinus Torvalds #include <net/af_unix.h> /* for Unix socket types */ 681da177e4SLinus Torvalds #include <linux/parser.h> 691da177e4SLinus Torvalds #include <linux/nfs_mount.h> 701da177e4SLinus Torvalds #include <net/ipv6.h> 711da177e4SLinus Torvalds #include <linux/hugetlb.h> 721da177e4SLinus Torvalds #include <linux/personality.h> 731da177e4SLinus Torvalds #include <linux/sysctl.h> 741da177e4SLinus Torvalds #include <linux/audit.h> 756931dfc9SEric Paris #include <linux/string.h> 76877ce7c1SCatherine Zhang #include <linux/selinux.h> 7723970741SEric Paris #include <linux/mutex.h> 781da177e4SLinus Torvalds 791da177e4SLinus Torvalds #include "avc.h" 801da177e4SLinus Torvalds #include "objsec.h" 811da177e4SLinus Torvalds #include "netif.h" 82224dfbd8SPaul Moore #include "netnode.h" 833e112172SPaul Moore #include "netport.h" 84d28d1e08STrent Jaeger #include "xfrm.h" 85c60475bfSPaul Moore #include "netlabel.h" 869d57a7f9SAhmed S. Darwish #include "audit.h" 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds #define XATTR_SELINUX_SUFFIX "selinux" 891da177e4SLinus Torvalds #define XATTR_NAME_SELINUX XATTR_SECURITY_PREFIX XATTR_SELINUX_SUFFIX 901da177e4SLinus Torvalds 91c9180a57SEric Paris #define NUM_SEL_MNT_OPTS 4 92c9180a57SEric Paris 931da177e4SLinus Torvalds extern unsigned int policydb_loaded_version; 941da177e4SLinus Torvalds extern int selinux_nlmsg_lookup(u16 sclass, u16 nlmsg_type, u32 *perm); 954e5ab4cbSJames Morris extern int selinux_compat_net; 9620510f2fSJames Morris extern struct security_operations *security_ops; 971da177e4SLinus Torvalds 98d621d35eSPaul Moore /* SECMARK reference count */ 99d621d35eSPaul Moore atomic_t selinux_secmark_refcount = ATOMIC_INIT(0); 100d621d35eSPaul Moore 1011da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP 102828dfe1dSEric Paris int selinux_enforcing; 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds static int __init enforcing_setup(char *str) 1051da177e4SLinus Torvalds { 1061da177e4SLinus Torvalds selinux_enforcing = simple_strtol(str, NULL, 0); 1071da177e4SLinus Torvalds return 1; 1081da177e4SLinus Torvalds } 1091da177e4SLinus Torvalds __setup("enforcing=", enforcing_setup); 1101da177e4SLinus Torvalds #endif 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM 1131da177e4SLinus Torvalds int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE; 1141da177e4SLinus Torvalds 1151da177e4SLinus Torvalds static int __init selinux_enabled_setup(char *str) 1161da177e4SLinus Torvalds { 1171da177e4SLinus Torvalds selinux_enabled = simple_strtol(str, NULL, 0); 1181da177e4SLinus Torvalds return 1; 1191da177e4SLinus Torvalds } 1201da177e4SLinus Torvalds __setup("selinux=", selinux_enabled_setup); 12130d55280SStephen Smalley #else 12230d55280SStephen Smalley int selinux_enabled = 1; 1231da177e4SLinus Torvalds #endif 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds /* Original (dummy) security module. */ 126828dfe1dSEric Paris static struct security_operations *original_ops; 1271da177e4SLinus Torvalds 1281da177e4SLinus Torvalds /* Minimal support for a secondary security module, 1291da177e4SLinus Torvalds just to allow the use of the dummy or capability modules. 1301da177e4SLinus Torvalds The owlsm module can alternatively be used as a secondary 1311da177e4SLinus Torvalds module as long as CONFIG_OWLSM_FD is not enabled. */ 132828dfe1dSEric Paris static struct security_operations *secondary_ops; 1331da177e4SLinus Torvalds 1341da177e4SLinus Torvalds /* Lists of inode and superblock security structures initialized 1351da177e4SLinus Torvalds before the policy was loaded. */ 1361da177e4SLinus Torvalds static LIST_HEAD(superblock_security_head); 1371da177e4SLinus Torvalds static DEFINE_SPINLOCK(sb_security_lock); 1381da177e4SLinus Torvalds 139e18b890bSChristoph Lameter static struct kmem_cache *sel_inode_cache; 1407cae7e26SJames Morris 141d621d35eSPaul Moore /** 142d621d35eSPaul Moore * selinux_secmark_enabled - Check to see if SECMARK is currently enabled 143d621d35eSPaul Moore * 144d621d35eSPaul Moore * Description: 145d621d35eSPaul Moore * This function checks the SECMARK reference counter to see if any SECMARK 146d621d35eSPaul Moore * targets are currently configured, if the reference counter is greater than 147d621d35eSPaul Moore * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is 148d621d35eSPaul Moore * enabled, false (0) if SECMARK is disabled. 149d621d35eSPaul Moore * 150d621d35eSPaul Moore */ 151d621d35eSPaul Moore static int selinux_secmark_enabled(void) 152d621d35eSPaul Moore { 153d621d35eSPaul Moore return (atomic_read(&selinux_secmark_refcount) > 0); 154d621d35eSPaul Moore } 155d621d35eSPaul Moore 1561da177e4SLinus Torvalds /* Allocate and free functions for each kind of security blob. */ 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds static int task_alloc_security(struct task_struct *task) 1591da177e4SLinus Torvalds { 1601da177e4SLinus Torvalds struct task_security_struct *tsec; 1611da177e4SLinus Torvalds 16289d155efSJames Morris tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL); 1631da177e4SLinus Torvalds if (!tsec) 1641da177e4SLinus Torvalds return -ENOMEM; 1651da177e4SLinus Torvalds 1660356357cSRoland McGrath tsec->osid = tsec->sid = SECINITSID_UNLABELED; 1671da177e4SLinus Torvalds task->security = tsec; 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds return 0; 1701da177e4SLinus Torvalds } 1711da177e4SLinus Torvalds 1721da177e4SLinus Torvalds static void task_free_security(struct task_struct *task) 1731da177e4SLinus Torvalds { 1741da177e4SLinus Torvalds struct task_security_struct *tsec = task->security; 1751da177e4SLinus Torvalds task->security = NULL; 1761da177e4SLinus Torvalds kfree(tsec); 1771da177e4SLinus Torvalds } 1781da177e4SLinus Torvalds 1791da177e4SLinus Torvalds static int inode_alloc_security(struct inode *inode) 1801da177e4SLinus Torvalds { 1811da177e4SLinus Torvalds struct task_security_struct *tsec = current->security; 1821da177e4SLinus Torvalds struct inode_security_struct *isec; 1831da177e4SLinus Torvalds 184a02fe132SJosef Bacik isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS); 1851da177e4SLinus Torvalds if (!isec) 1861da177e4SLinus Torvalds return -ENOMEM; 1871da177e4SLinus Torvalds 18823970741SEric Paris mutex_init(&isec->lock); 1891da177e4SLinus Torvalds INIT_LIST_HEAD(&isec->list); 1901da177e4SLinus Torvalds isec->inode = inode; 1911da177e4SLinus Torvalds isec->sid = SECINITSID_UNLABELED; 1921da177e4SLinus Torvalds isec->sclass = SECCLASS_FILE; 1931da177e4SLinus Torvalds isec->task_sid = tsec->sid; 1941da177e4SLinus Torvalds inode->i_security = isec; 1951da177e4SLinus Torvalds 1961da177e4SLinus Torvalds return 0; 1971da177e4SLinus Torvalds } 1981da177e4SLinus Torvalds 1991da177e4SLinus Torvalds static void inode_free_security(struct inode *inode) 2001da177e4SLinus Torvalds { 2011da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 2021da177e4SLinus Torvalds struct superblock_security_struct *sbsec = inode->i_sb->s_security; 2031da177e4SLinus Torvalds 2041da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 2051da177e4SLinus Torvalds if (!list_empty(&isec->list)) 2061da177e4SLinus Torvalds list_del_init(&isec->list); 2071da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 2081da177e4SLinus Torvalds 2091da177e4SLinus Torvalds inode->i_security = NULL; 2107cae7e26SJames Morris kmem_cache_free(sel_inode_cache, isec); 2111da177e4SLinus Torvalds } 2121da177e4SLinus Torvalds 2131da177e4SLinus Torvalds static int file_alloc_security(struct file *file) 2141da177e4SLinus Torvalds { 2151da177e4SLinus Torvalds struct task_security_struct *tsec = current->security; 2161da177e4SLinus Torvalds struct file_security_struct *fsec; 2171da177e4SLinus Torvalds 21826d2a4beSStephen Smalley fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL); 2191da177e4SLinus Torvalds if (!fsec) 2201da177e4SLinus Torvalds return -ENOMEM; 2211da177e4SLinus Torvalds 2221da177e4SLinus Torvalds fsec->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->peer_sid = SECINITSID_UNLABELED; 279892c141eSVenkat Yekkirala ssec->sid = SECINITSID_UNLABELED; 2801da177e4SLinus Torvalds sk->sk_security = ssec; 2811da177e4SLinus Torvalds 282f74af6e8SPaul Moore selinux_netlbl_sk_security_reset(ssec, family); 28399f59ed0SPaul Moore 2841da177e4SLinus Torvalds return 0; 2851da177e4SLinus Torvalds } 2861da177e4SLinus Torvalds 2871da177e4SLinus Torvalds static void sk_free_security(struct sock *sk) 2881da177e4SLinus Torvalds { 2891da177e4SLinus Torvalds struct sk_security_struct *ssec = sk->sk_security; 2901da177e4SLinus Torvalds 2911da177e4SLinus Torvalds sk->sk_security = NULL; 2921da177e4SLinus Torvalds kfree(ssec); 2931da177e4SLinus Torvalds } 2941da177e4SLinus Torvalds 2951da177e4SLinus Torvalds /* The security server must be initialized before 2961da177e4SLinus Torvalds any labeling or access decisions can be provided. */ 2971da177e4SLinus Torvalds extern int ss_initialized; 2981da177e4SLinus Torvalds 2991da177e4SLinus Torvalds /* The file system's label must be initialized prior to use. */ 3001da177e4SLinus Torvalds 3011da177e4SLinus Torvalds static char *labeling_behaviors[6] = { 3021da177e4SLinus Torvalds "uses xattr", 3031da177e4SLinus Torvalds "uses transition SIDs", 3041da177e4SLinus Torvalds "uses task SIDs", 3051da177e4SLinus Torvalds "uses genfs_contexts", 3061da177e4SLinus Torvalds "not configured for labeling", 3071da177e4SLinus Torvalds "uses mountpoint labeling", 3081da177e4SLinus Torvalds }; 3091da177e4SLinus Torvalds 3101da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry); 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds static inline int inode_doinit(struct inode *inode) 3131da177e4SLinus Torvalds { 3141da177e4SLinus Torvalds return inode_doinit_with_dentry(inode, NULL); 3151da177e4SLinus Torvalds } 3161da177e4SLinus Torvalds 3171da177e4SLinus Torvalds enum { 31831e87930SEric Paris Opt_error = -1, 3191da177e4SLinus Torvalds Opt_context = 1, 3201da177e4SLinus Torvalds Opt_fscontext = 2, 321c9180a57SEric Paris Opt_defcontext = 3, 322c9180a57SEric Paris Opt_rootcontext = 4, 3231da177e4SLinus Torvalds }; 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds static match_table_t tokens = { 326832cbd9aSEric Paris {Opt_context, CONTEXT_STR "%s"}, 327832cbd9aSEric Paris {Opt_fscontext, FSCONTEXT_STR "%s"}, 328832cbd9aSEric Paris {Opt_defcontext, DEFCONTEXT_STR "%s"}, 329832cbd9aSEric Paris {Opt_rootcontext, ROOTCONTEXT_STR "%s"}, 33031e87930SEric Paris {Opt_error, NULL}, 3311da177e4SLinus Torvalds }; 3321da177e4SLinus Torvalds 3331da177e4SLinus Torvalds #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n" 3341da177e4SLinus Torvalds 335c312feb2SEric Paris static int may_context_mount_sb_relabel(u32 sid, 336c312feb2SEric Paris struct superblock_security_struct *sbsec, 337c312feb2SEric Paris struct task_security_struct *tsec) 338c312feb2SEric Paris { 339c312feb2SEric Paris int rc; 340c312feb2SEric Paris 341c312feb2SEric Paris rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 342c312feb2SEric Paris FILESYSTEM__RELABELFROM, NULL); 343c312feb2SEric Paris if (rc) 344c312feb2SEric Paris return rc; 345c312feb2SEric Paris 346c312feb2SEric Paris rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM, 347c312feb2SEric Paris FILESYSTEM__RELABELTO, NULL); 348c312feb2SEric Paris return rc; 349c312feb2SEric Paris } 350c312feb2SEric Paris 3510808925eSEric Paris static int may_context_mount_inode_relabel(u32 sid, 3520808925eSEric Paris struct superblock_security_struct *sbsec, 3530808925eSEric Paris struct task_security_struct *tsec) 3540808925eSEric Paris { 3550808925eSEric Paris int rc; 3560808925eSEric Paris rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 3570808925eSEric Paris FILESYSTEM__RELABELFROM, NULL); 3580808925eSEric Paris if (rc) 3590808925eSEric Paris return rc; 3600808925eSEric Paris 3610808925eSEric Paris rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, 3620808925eSEric Paris FILESYSTEM__ASSOCIATE, NULL); 3630808925eSEric Paris return rc; 3640808925eSEric Paris } 3650808925eSEric Paris 366c9180a57SEric Paris static int sb_finish_set_opts(struct super_block *sb) 3671da177e4SLinus Torvalds { 3681da177e4SLinus Torvalds struct superblock_security_struct *sbsec = sb->s_security; 3691da177e4SLinus Torvalds struct dentry *root = sb->s_root; 370c9180a57SEric Paris struct inode *root_inode = root->d_inode; 3711da177e4SLinus Torvalds int rc = 0; 3721da177e4SLinus Torvalds 3731da177e4SLinus Torvalds if (sbsec->behavior == SECURITY_FS_USE_XATTR) { 3741da177e4SLinus Torvalds /* Make sure that the xattr handler exists and that no 3751da177e4SLinus Torvalds error other than -ENODATA is returned by getxattr on 3761da177e4SLinus Torvalds the root directory. -ENODATA is ok, as this may be 3771da177e4SLinus Torvalds the first boot of the SELinux kernel before we have 3781da177e4SLinus Torvalds assigned xattr values to the filesystem. */ 379c9180a57SEric Paris if (!root_inode->i_op->getxattr) { 3801da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: (dev %s, type %s) has no " 3811da177e4SLinus Torvalds "xattr support\n", sb->s_id, sb->s_type->name); 3821da177e4SLinus Torvalds rc = -EOPNOTSUPP; 3831da177e4SLinus Torvalds goto out; 3841da177e4SLinus Torvalds } 385c9180a57SEric Paris rc = root_inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0); 3861da177e4SLinus Torvalds if (rc < 0 && rc != -ENODATA) { 3871da177e4SLinus Torvalds if (rc == -EOPNOTSUPP) 3881da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: (dev %s, type " 3891da177e4SLinus Torvalds "%s) has no security xattr handler\n", 3901da177e4SLinus Torvalds sb->s_id, sb->s_type->name); 3911da177e4SLinus Torvalds else 3921da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: (dev %s, type " 3931da177e4SLinus Torvalds "%s) getxattr errno %d\n", sb->s_id, 3941da177e4SLinus Torvalds sb->s_type->name, -rc); 3951da177e4SLinus Torvalds goto out; 3961da177e4SLinus Torvalds } 3971da177e4SLinus Torvalds } 3981da177e4SLinus Torvalds 3991da177e4SLinus Torvalds sbsec->initialized = 1; 4001da177e4SLinus Torvalds 401c9180a57SEric Paris if (sbsec->behavior > ARRAY_SIZE(labeling_behaviors)) 402fadcdb45SEric Paris printk(KERN_ERR "SELinux: initialized (dev %s, type %s), unknown behavior\n", 4031da177e4SLinus Torvalds sb->s_id, sb->s_type->name); 404c9180a57SEric Paris else 405fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: initialized (dev %s, type %s), %s\n", 4061da177e4SLinus Torvalds sb->s_id, sb->s_type->name, 4071da177e4SLinus Torvalds labeling_behaviors[sbsec->behavior-1]); 4081da177e4SLinus Torvalds 4091da177e4SLinus Torvalds /* Initialize the root inode. */ 410c9180a57SEric Paris rc = inode_doinit_with_dentry(root_inode, root); 4111da177e4SLinus Torvalds 4121da177e4SLinus Torvalds /* Initialize any other inodes associated with the superblock, e.g. 4131da177e4SLinus Torvalds inodes created prior to initial policy load or inodes created 4141da177e4SLinus Torvalds during get_sb by a pseudo filesystem that directly 4151da177e4SLinus Torvalds populates itself. */ 4161da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 4171da177e4SLinus Torvalds next_inode: 4181da177e4SLinus Torvalds if (!list_empty(&sbsec->isec_head)) { 4191da177e4SLinus Torvalds struct inode_security_struct *isec = 4201da177e4SLinus Torvalds list_entry(sbsec->isec_head.next, 4211da177e4SLinus Torvalds struct inode_security_struct, list); 4221da177e4SLinus Torvalds struct inode *inode = isec->inode; 4231da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 4241da177e4SLinus Torvalds inode = igrab(inode); 4251da177e4SLinus Torvalds if (inode) { 4261da177e4SLinus Torvalds if (!IS_PRIVATE(inode)) 4271da177e4SLinus Torvalds inode_doinit(inode); 4281da177e4SLinus Torvalds iput(inode); 4291da177e4SLinus Torvalds } 4301da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 4311da177e4SLinus Torvalds list_del_init(&isec->list); 4321da177e4SLinus Torvalds goto next_inode; 4331da177e4SLinus Torvalds } 4341da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 4351da177e4SLinus Torvalds out: 436c9180a57SEric Paris return rc; 437c9180a57SEric Paris } 438c9180a57SEric Paris 439c9180a57SEric Paris /* 440c9180a57SEric Paris * This function should allow an FS to ask what it's mount security 441c9180a57SEric Paris * options were so it can use those later for submounts, displaying 442c9180a57SEric Paris * mount options, or whatever. 443c9180a57SEric Paris */ 444c9180a57SEric Paris static int selinux_get_mnt_opts(const struct super_block *sb, 445e0007529SEric Paris struct security_mnt_opts *opts) 446c9180a57SEric Paris { 447c9180a57SEric Paris int rc = 0, i; 448c9180a57SEric Paris struct superblock_security_struct *sbsec = sb->s_security; 449c9180a57SEric Paris char *context = NULL; 450c9180a57SEric Paris u32 len; 451c9180a57SEric Paris char tmp; 452c9180a57SEric Paris 453e0007529SEric Paris security_init_mnt_opts(opts); 454c9180a57SEric Paris 455c9180a57SEric Paris if (!sbsec->initialized) 456c9180a57SEric Paris return -EINVAL; 457c9180a57SEric Paris 458c9180a57SEric Paris if (!ss_initialized) 459c9180a57SEric Paris return -EINVAL; 460c9180a57SEric Paris 461c9180a57SEric Paris /* 462c9180a57SEric Paris * if we ever use sbsec flags for anything other than tracking mount 463c9180a57SEric Paris * settings this is going to need a mask 464c9180a57SEric Paris */ 465c9180a57SEric Paris tmp = sbsec->flags; 466c9180a57SEric Paris /* count the number of mount options for this sb */ 467c9180a57SEric Paris for (i = 0; i < 8; i++) { 468c9180a57SEric Paris if (tmp & 0x01) 469e0007529SEric Paris opts->num_mnt_opts++; 470c9180a57SEric Paris tmp >>= 1; 471c9180a57SEric Paris } 472c9180a57SEric Paris 473e0007529SEric Paris opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC); 474e0007529SEric Paris if (!opts->mnt_opts) { 475c9180a57SEric Paris rc = -ENOMEM; 476c9180a57SEric Paris goto out_free; 477c9180a57SEric Paris } 478c9180a57SEric Paris 479e0007529SEric Paris opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC); 480e0007529SEric Paris if (!opts->mnt_opts_flags) { 481c9180a57SEric Paris rc = -ENOMEM; 482c9180a57SEric Paris goto out_free; 483c9180a57SEric Paris } 484c9180a57SEric Paris 485c9180a57SEric Paris i = 0; 486c9180a57SEric Paris if (sbsec->flags & FSCONTEXT_MNT) { 487c9180a57SEric Paris rc = security_sid_to_context(sbsec->sid, &context, &len); 488c9180a57SEric Paris if (rc) 489c9180a57SEric Paris goto out_free; 490e0007529SEric Paris opts->mnt_opts[i] = context; 491e0007529SEric Paris opts->mnt_opts_flags[i++] = FSCONTEXT_MNT; 492c9180a57SEric Paris } 493c9180a57SEric Paris if (sbsec->flags & CONTEXT_MNT) { 494c9180a57SEric Paris rc = security_sid_to_context(sbsec->mntpoint_sid, &context, &len); 495c9180a57SEric Paris if (rc) 496c9180a57SEric Paris goto out_free; 497e0007529SEric Paris opts->mnt_opts[i] = context; 498e0007529SEric Paris opts->mnt_opts_flags[i++] = CONTEXT_MNT; 499c9180a57SEric Paris } 500c9180a57SEric Paris if (sbsec->flags & DEFCONTEXT_MNT) { 501c9180a57SEric Paris rc = security_sid_to_context(sbsec->def_sid, &context, &len); 502c9180a57SEric Paris if (rc) 503c9180a57SEric Paris goto out_free; 504e0007529SEric Paris opts->mnt_opts[i] = context; 505e0007529SEric Paris opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT; 506c9180a57SEric Paris } 507c9180a57SEric Paris if (sbsec->flags & ROOTCONTEXT_MNT) { 508c9180a57SEric Paris struct inode *root = sbsec->sb->s_root->d_inode; 509c9180a57SEric Paris struct inode_security_struct *isec = root->i_security; 510c9180a57SEric Paris 511c9180a57SEric Paris rc = security_sid_to_context(isec->sid, &context, &len); 512c9180a57SEric Paris if (rc) 513c9180a57SEric Paris goto out_free; 514e0007529SEric Paris opts->mnt_opts[i] = context; 515e0007529SEric Paris opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT; 516c9180a57SEric Paris } 517c9180a57SEric Paris 518e0007529SEric Paris BUG_ON(i != opts->num_mnt_opts); 519c9180a57SEric Paris 520c9180a57SEric Paris return 0; 521c9180a57SEric Paris 522c9180a57SEric Paris out_free: 523e0007529SEric Paris security_free_mnt_opts(opts); 524c9180a57SEric Paris return rc; 525c9180a57SEric Paris } 526c9180a57SEric Paris 527c9180a57SEric Paris static int bad_option(struct superblock_security_struct *sbsec, char flag, 528c9180a57SEric Paris u32 old_sid, u32 new_sid) 529c9180a57SEric Paris { 530c9180a57SEric Paris /* check if the old mount command had the same options */ 531c9180a57SEric Paris if (sbsec->initialized) 532c9180a57SEric Paris if (!(sbsec->flags & flag) || 533c9180a57SEric Paris (old_sid != new_sid)) 534c9180a57SEric Paris return 1; 535c9180a57SEric Paris 536c9180a57SEric Paris /* check if we were passed the same options twice, 537c9180a57SEric Paris * aka someone passed context=a,context=b 538c9180a57SEric Paris */ 539c9180a57SEric Paris if (!sbsec->initialized) 540c9180a57SEric Paris if (sbsec->flags & flag) 541c9180a57SEric Paris return 1; 542c9180a57SEric Paris return 0; 543c9180a57SEric Paris } 544e0007529SEric Paris 545c9180a57SEric Paris /* 546c9180a57SEric Paris * Allow filesystems with binary mount data to explicitly set mount point 547c9180a57SEric Paris * labeling information. 548c9180a57SEric Paris */ 549e0007529SEric Paris static int selinux_set_mnt_opts(struct super_block *sb, 550e0007529SEric Paris struct security_mnt_opts *opts) 551c9180a57SEric Paris { 552c9180a57SEric Paris int rc = 0, i; 553c9180a57SEric Paris struct task_security_struct *tsec = current->security; 554c9180a57SEric Paris struct superblock_security_struct *sbsec = sb->s_security; 555c9180a57SEric Paris const char *name = sb->s_type->name; 556c9180a57SEric Paris struct inode *inode = sbsec->sb->s_root->d_inode; 557c9180a57SEric Paris struct inode_security_struct *root_isec = inode->i_security; 558c9180a57SEric Paris u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0; 559c9180a57SEric Paris u32 defcontext_sid = 0; 560e0007529SEric Paris char **mount_options = opts->mnt_opts; 561e0007529SEric Paris int *flags = opts->mnt_opts_flags; 562e0007529SEric Paris int num_opts = opts->num_mnt_opts; 563c9180a57SEric Paris 564c9180a57SEric Paris mutex_lock(&sbsec->lock); 565c9180a57SEric Paris 566c9180a57SEric Paris if (!ss_initialized) { 567c9180a57SEric Paris if (!num_opts) { 568c9180a57SEric Paris /* Defer initialization until selinux_complete_init, 569c9180a57SEric Paris after the initial policy is loaded and the security 570c9180a57SEric Paris server is ready to handle calls. */ 571c9180a57SEric Paris spin_lock(&sb_security_lock); 572c9180a57SEric Paris if (list_empty(&sbsec->list)) 573c9180a57SEric Paris list_add(&sbsec->list, &superblock_security_head); 574c9180a57SEric Paris spin_unlock(&sb_security_lock); 575c9180a57SEric Paris goto out; 576c9180a57SEric Paris } 577c9180a57SEric Paris rc = -EINVAL; 578744ba35eSEric Paris printk(KERN_WARNING "SELinux: Unable to set superblock options " 579744ba35eSEric Paris "before the security server is initialized\n"); 580c9180a57SEric Paris goto out; 581c9180a57SEric Paris } 582c9180a57SEric Paris 583c9180a57SEric Paris /* 584e0007529SEric Paris * Binary mount data FS will come through this function twice. Once 585e0007529SEric Paris * from an explicit call and once from the generic calls from the vfs. 586e0007529SEric Paris * Since the generic VFS calls will not contain any security mount data 587e0007529SEric Paris * we need to skip the double mount verification. 588e0007529SEric Paris * 589e0007529SEric Paris * This does open a hole in which we will not notice if the first 590e0007529SEric Paris * mount using this sb set explict options and a second mount using 591e0007529SEric Paris * this sb does not set any security options. (The first options 592e0007529SEric Paris * will be used for both mounts) 593e0007529SEric Paris */ 594e0007529SEric Paris if (sbsec->initialized && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA) 595e0007529SEric Paris && (num_opts == 0)) 596e0007529SEric Paris goto out; 597e0007529SEric Paris 598e0007529SEric Paris /* 599c9180a57SEric Paris * parse the mount options, check if they are valid sids. 600c9180a57SEric Paris * also check if someone is trying to mount the same sb more 601c9180a57SEric Paris * than once with different security options. 602c9180a57SEric Paris */ 603c9180a57SEric Paris for (i = 0; i < num_opts; i++) { 604c9180a57SEric Paris u32 sid; 605c9180a57SEric Paris rc = security_context_to_sid(mount_options[i], 606c9180a57SEric Paris strlen(mount_options[i]), &sid); 607c9180a57SEric Paris if (rc) { 608c9180a57SEric Paris printk(KERN_WARNING "SELinux: security_context_to_sid" 609c9180a57SEric Paris "(%s) failed for (dev %s, type %s) errno=%d\n", 610c9180a57SEric Paris mount_options[i], sb->s_id, name, rc); 611c9180a57SEric Paris goto out; 612c9180a57SEric Paris } 613c9180a57SEric Paris switch (flags[i]) { 614c9180a57SEric Paris case FSCONTEXT_MNT: 615c9180a57SEric Paris fscontext_sid = sid; 616c9180a57SEric Paris 617c9180a57SEric Paris if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, 618c9180a57SEric Paris fscontext_sid)) 619c9180a57SEric Paris goto out_double_mount; 620c9180a57SEric Paris 621c9180a57SEric Paris sbsec->flags |= FSCONTEXT_MNT; 622c9180a57SEric Paris break; 623c9180a57SEric Paris case CONTEXT_MNT: 624c9180a57SEric Paris context_sid = sid; 625c9180a57SEric Paris 626c9180a57SEric Paris if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, 627c9180a57SEric Paris context_sid)) 628c9180a57SEric Paris goto out_double_mount; 629c9180a57SEric Paris 630c9180a57SEric Paris sbsec->flags |= CONTEXT_MNT; 631c9180a57SEric Paris break; 632c9180a57SEric Paris case ROOTCONTEXT_MNT: 633c9180a57SEric Paris rootcontext_sid = sid; 634c9180a57SEric Paris 635c9180a57SEric Paris if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, 636c9180a57SEric Paris rootcontext_sid)) 637c9180a57SEric Paris goto out_double_mount; 638c9180a57SEric Paris 639c9180a57SEric Paris sbsec->flags |= ROOTCONTEXT_MNT; 640c9180a57SEric Paris 641c9180a57SEric Paris break; 642c9180a57SEric Paris case DEFCONTEXT_MNT: 643c9180a57SEric Paris defcontext_sid = sid; 644c9180a57SEric Paris 645c9180a57SEric Paris if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, 646c9180a57SEric Paris defcontext_sid)) 647c9180a57SEric Paris goto out_double_mount; 648c9180a57SEric Paris 649c9180a57SEric Paris sbsec->flags |= DEFCONTEXT_MNT; 650c9180a57SEric Paris 651c9180a57SEric Paris break; 652c9180a57SEric Paris default: 653c9180a57SEric Paris rc = -EINVAL; 654c9180a57SEric Paris goto out; 655c9180a57SEric Paris } 656c9180a57SEric Paris } 657c9180a57SEric Paris 658c9180a57SEric Paris if (sbsec->initialized) { 659c9180a57SEric Paris /* previously mounted with options, but not on this attempt? */ 660c9180a57SEric Paris if (sbsec->flags && !num_opts) 661c9180a57SEric Paris goto out_double_mount; 662c9180a57SEric Paris rc = 0; 663c9180a57SEric Paris goto out; 664c9180a57SEric Paris } 665c9180a57SEric Paris 666c9180a57SEric Paris if (strcmp(sb->s_type->name, "proc") == 0) 667c9180a57SEric Paris sbsec->proc = 1; 668c9180a57SEric Paris 669c9180a57SEric Paris /* Determine the labeling behavior to use for this filesystem type. */ 670c9180a57SEric Paris rc = security_fs_use(sb->s_type->name, &sbsec->behavior, &sbsec->sid); 671c9180a57SEric Paris if (rc) { 672c9180a57SEric Paris printk(KERN_WARNING "%s: security_fs_use(%s) returned %d\n", 673dd6f953aSHarvey Harrison __func__, sb->s_type->name, rc); 674c9180a57SEric Paris goto out; 675c9180a57SEric Paris } 676c9180a57SEric Paris 677c9180a57SEric Paris /* sets the context of the superblock for the fs being mounted. */ 678c9180a57SEric Paris if (fscontext_sid) { 679c9180a57SEric Paris 680c9180a57SEric Paris rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, tsec); 681c9180a57SEric Paris if (rc) 682c9180a57SEric Paris goto out; 683c9180a57SEric Paris 684c9180a57SEric Paris sbsec->sid = fscontext_sid; 685c9180a57SEric Paris } 686c9180a57SEric Paris 687c9180a57SEric Paris /* 688c9180a57SEric Paris * Switch to using mount point labeling behavior. 689c9180a57SEric Paris * sets the label used on all file below the mountpoint, and will set 690c9180a57SEric Paris * the superblock context if not already set. 691c9180a57SEric Paris */ 692c9180a57SEric Paris if (context_sid) { 693c9180a57SEric Paris if (!fscontext_sid) { 694c9180a57SEric Paris rc = may_context_mount_sb_relabel(context_sid, sbsec, tsec); 695c9180a57SEric Paris if (rc) 696c9180a57SEric Paris goto out; 697c9180a57SEric Paris sbsec->sid = context_sid; 698c9180a57SEric Paris } else { 699c9180a57SEric Paris rc = may_context_mount_inode_relabel(context_sid, sbsec, tsec); 700c9180a57SEric Paris if (rc) 701c9180a57SEric Paris goto out; 702c9180a57SEric Paris } 703c9180a57SEric Paris if (!rootcontext_sid) 704c9180a57SEric Paris rootcontext_sid = context_sid; 705c9180a57SEric Paris 706c9180a57SEric Paris sbsec->mntpoint_sid = context_sid; 707c9180a57SEric Paris sbsec->behavior = SECURITY_FS_USE_MNTPOINT; 708c9180a57SEric Paris } 709c9180a57SEric Paris 710c9180a57SEric Paris if (rootcontext_sid) { 711c9180a57SEric Paris rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec, tsec); 712c9180a57SEric Paris if (rc) 713c9180a57SEric Paris goto out; 714c9180a57SEric Paris 715c9180a57SEric Paris root_isec->sid = rootcontext_sid; 716c9180a57SEric Paris root_isec->initialized = 1; 717c9180a57SEric Paris } 718c9180a57SEric Paris 719c9180a57SEric Paris if (defcontext_sid) { 720c9180a57SEric Paris if (sbsec->behavior != SECURITY_FS_USE_XATTR) { 721c9180a57SEric Paris rc = -EINVAL; 722c9180a57SEric Paris printk(KERN_WARNING "SELinux: defcontext option is " 723c9180a57SEric Paris "invalid for this filesystem type\n"); 724c9180a57SEric Paris goto out; 725c9180a57SEric Paris } 726c9180a57SEric Paris 727c9180a57SEric Paris if (defcontext_sid != sbsec->def_sid) { 728c9180a57SEric Paris rc = may_context_mount_inode_relabel(defcontext_sid, 729c9180a57SEric Paris sbsec, tsec); 730c9180a57SEric Paris if (rc) 731c9180a57SEric Paris goto out; 732c9180a57SEric Paris } 733c9180a57SEric Paris 734c9180a57SEric Paris sbsec->def_sid = defcontext_sid; 735c9180a57SEric Paris } 736c9180a57SEric Paris 737c9180a57SEric Paris rc = sb_finish_set_opts(sb); 738c9180a57SEric Paris out: 739bc7e982bSEric Paris mutex_unlock(&sbsec->lock); 7401da177e4SLinus Torvalds return rc; 741c9180a57SEric Paris out_double_mount: 742c9180a57SEric Paris rc = -EINVAL; 743c9180a57SEric Paris printk(KERN_WARNING "SELinux: mount invalid. Same superblock, different " 744c9180a57SEric Paris "security settings for (dev %s, type %s)\n", sb->s_id, name); 745c9180a57SEric Paris goto out; 746c9180a57SEric Paris } 747c9180a57SEric Paris 748c9180a57SEric Paris static void selinux_sb_clone_mnt_opts(const struct super_block *oldsb, 749c9180a57SEric Paris struct super_block *newsb) 750c9180a57SEric Paris { 751c9180a57SEric Paris const struct superblock_security_struct *oldsbsec = oldsb->s_security; 752c9180a57SEric Paris struct superblock_security_struct *newsbsec = newsb->s_security; 753c9180a57SEric Paris 754c9180a57SEric Paris int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT); 755c9180a57SEric Paris int set_context = (oldsbsec->flags & CONTEXT_MNT); 756c9180a57SEric Paris int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT); 757c9180a57SEric Paris 7580f5e6420SEric Paris /* 7590f5e6420SEric Paris * if the parent was able to be mounted it clearly had no special lsm 7600f5e6420SEric Paris * mount options. thus we can safely put this sb on the list and deal 7610f5e6420SEric Paris * with it later 7620f5e6420SEric Paris */ 7630f5e6420SEric Paris if (!ss_initialized) { 7640f5e6420SEric Paris spin_lock(&sb_security_lock); 7650f5e6420SEric Paris if (list_empty(&newsbsec->list)) 7660f5e6420SEric Paris list_add(&newsbsec->list, &superblock_security_head); 7670f5e6420SEric Paris spin_unlock(&sb_security_lock); 7680f5e6420SEric Paris return; 7690f5e6420SEric Paris } 770c9180a57SEric Paris 771c9180a57SEric Paris /* how can we clone if the old one wasn't set up?? */ 772c9180a57SEric Paris BUG_ON(!oldsbsec->initialized); 773c9180a57SEric Paris 7745a552617SEric Paris /* if fs is reusing a sb, just let its options stand... */ 7755a552617SEric Paris if (newsbsec->initialized) 7765a552617SEric Paris return; 7775a552617SEric Paris 778c9180a57SEric Paris mutex_lock(&newsbsec->lock); 779c9180a57SEric Paris 780c9180a57SEric Paris newsbsec->flags = oldsbsec->flags; 781c9180a57SEric Paris 782c9180a57SEric Paris newsbsec->sid = oldsbsec->sid; 783c9180a57SEric Paris newsbsec->def_sid = oldsbsec->def_sid; 784c9180a57SEric Paris newsbsec->behavior = oldsbsec->behavior; 785c9180a57SEric Paris 786c9180a57SEric Paris if (set_context) { 787c9180a57SEric Paris u32 sid = oldsbsec->mntpoint_sid; 788c9180a57SEric Paris 789c9180a57SEric Paris if (!set_fscontext) 790c9180a57SEric Paris newsbsec->sid = sid; 791c9180a57SEric Paris if (!set_rootcontext) { 792c9180a57SEric Paris struct inode *newinode = newsb->s_root->d_inode; 793c9180a57SEric Paris struct inode_security_struct *newisec = newinode->i_security; 794c9180a57SEric Paris newisec->sid = sid; 795c9180a57SEric Paris } 796c9180a57SEric Paris newsbsec->mntpoint_sid = sid; 797c9180a57SEric Paris } 798c9180a57SEric Paris if (set_rootcontext) { 799c9180a57SEric Paris const struct inode *oldinode = oldsb->s_root->d_inode; 800c9180a57SEric Paris const struct inode_security_struct *oldisec = oldinode->i_security; 801c9180a57SEric Paris struct inode *newinode = newsb->s_root->d_inode; 802c9180a57SEric Paris struct inode_security_struct *newisec = newinode->i_security; 803c9180a57SEric Paris 804c9180a57SEric Paris newisec->sid = oldisec->sid; 805c9180a57SEric Paris } 806c9180a57SEric Paris 807c9180a57SEric Paris sb_finish_set_opts(newsb); 808c9180a57SEric Paris mutex_unlock(&newsbsec->lock); 809c9180a57SEric Paris } 810c9180a57SEric Paris 8112e1479d9SAdrian Bunk static int selinux_parse_opts_str(char *options, 8122e1479d9SAdrian Bunk struct security_mnt_opts *opts) 813c9180a57SEric Paris { 814e0007529SEric Paris char *p; 815c9180a57SEric Paris char *context = NULL, *defcontext = NULL; 816c9180a57SEric Paris char *fscontext = NULL, *rootcontext = NULL; 817e0007529SEric Paris int rc, num_mnt_opts = 0; 818c9180a57SEric Paris 819e0007529SEric Paris opts->num_mnt_opts = 0; 820c9180a57SEric Paris 821c9180a57SEric Paris /* Standard string-based options. */ 822c9180a57SEric Paris while ((p = strsep(&options, "|")) != NULL) { 823c9180a57SEric Paris int token; 824c9180a57SEric Paris substring_t args[MAX_OPT_ARGS]; 825c9180a57SEric Paris 826c9180a57SEric Paris if (!*p) 827c9180a57SEric Paris continue; 828c9180a57SEric Paris 829c9180a57SEric Paris token = match_token(p, tokens, args); 830c9180a57SEric Paris 831c9180a57SEric Paris switch (token) { 832c9180a57SEric Paris case Opt_context: 833c9180a57SEric Paris if (context || defcontext) { 834c9180a57SEric Paris rc = -EINVAL; 835c9180a57SEric Paris printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 836c9180a57SEric Paris goto out_err; 837c9180a57SEric Paris } 838c9180a57SEric Paris context = match_strdup(&args[0]); 839c9180a57SEric Paris if (!context) { 840c9180a57SEric Paris rc = -ENOMEM; 841c9180a57SEric Paris goto out_err; 842c9180a57SEric Paris } 843c9180a57SEric Paris break; 844c9180a57SEric Paris 845c9180a57SEric Paris case Opt_fscontext: 846c9180a57SEric Paris if (fscontext) { 847c9180a57SEric Paris rc = -EINVAL; 848c9180a57SEric Paris printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 849c9180a57SEric Paris goto out_err; 850c9180a57SEric Paris } 851c9180a57SEric Paris fscontext = match_strdup(&args[0]); 852c9180a57SEric Paris if (!fscontext) { 853c9180a57SEric Paris rc = -ENOMEM; 854c9180a57SEric Paris goto out_err; 855c9180a57SEric Paris } 856c9180a57SEric Paris break; 857c9180a57SEric Paris 858c9180a57SEric Paris case Opt_rootcontext: 859c9180a57SEric Paris if (rootcontext) { 860c9180a57SEric Paris rc = -EINVAL; 861c9180a57SEric Paris printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 862c9180a57SEric Paris goto out_err; 863c9180a57SEric Paris } 864c9180a57SEric Paris rootcontext = match_strdup(&args[0]); 865c9180a57SEric Paris if (!rootcontext) { 866c9180a57SEric Paris rc = -ENOMEM; 867c9180a57SEric Paris goto out_err; 868c9180a57SEric Paris } 869c9180a57SEric Paris break; 870c9180a57SEric Paris 871c9180a57SEric Paris case Opt_defcontext: 872c9180a57SEric Paris if (context || defcontext) { 873c9180a57SEric Paris rc = -EINVAL; 874c9180a57SEric Paris printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 875c9180a57SEric Paris goto out_err; 876c9180a57SEric Paris } 877c9180a57SEric Paris defcontext = match_strdup(&args[0]); 878c9180a57SEric Paris if (!defcontext) { 879c9180a57SEric Paris rc = -ENOMEM; 880c9180a57SEric Paris goto out_err; 881c9180a57SEric Paris } 882c9180a57SEric Paris break; 883c9180a57SEric Paris 884c9180a57SEric Paris default: 885c9180a57SEric Paris rc = -EINVAL; 886c9180a57SEric Paris printk(KERN_WARNING "SELinux: unknown mount option\n"); 887c9180a57SEric Paris goto out_err; 888c9180a57SEric Paris 889c9180a57SEric Paris } 890c9180a57SEric Paris } 891c9180a57SEric Paris 892e0007529SEric Paris rc = -ENOMEM; 893e0007529SEric Paris opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_ATOMIC); 894e0007529SEric Paris if (!opts->mnt_opts) 895e0007529SEric Paris goto out_err; 896e0007529SEric Paris 897e0007529SEric Paris opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), GFP_ATOMIC); 898e0007529SEric Paris if (!opts->mnt_opts_flags) { 899e0007529SEric Paris kfree(opts->mnt_opts); 900e0007529SEric Paris goto out_err; 901c9180a57SEric Paris } 902c9180a57SEric Paris 903e0007529SEric Paris if (fscontext) { 904e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = fscontext; 905e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT; 906e0007529SEric Paris } 907e0007529SEric Paris if (context) { 908e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = context; 909e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT; 910e0007529SEric Paris } 911e0007529SEric Paris if (rootcontext) { 912e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = rootcontext; 913e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT; 914e0007529SEric Paris } 915e0007529SEric Paris if (defcontext) { 916e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = defcontext; 917e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT; 918e0007529SEric Paris } 919e0007529SEric Paris 920e0007529SEric Paris opts->num_mnt_opts = num_mnt_opts; 921e0007529SEric Paris return 0; 922e0007529SEric Paris 923c9180a57SEric Paris out_err: 924c9180a57SEric Paris kfree(context); 925c9180a57SEric Paris kfree(defcontext); 926c9180a57SEric Paris kfree(fscontext); 927c9180a57SEric Paris kfree(rootcontext); 928c9180a57SEric Paris return rc; 9291da177e4SLinus Torvalds } 930e0007529SEric Paris /* 931e0007529SEric Paris * string mount options parsing and call set the sbsec 932e0007529SEric Paris */ 933e0007529SEric Paris static int superblock_doinit(struct super_block *sb, void *data) 934e0007529SEric Paris { 935e0007529SEric Paris int rc = 0; 936e0007529SEric Paris char *options = data; 937e0007529SEric Paris struct security_mnt_opts opts; 938e0007529SEric Paris 939e0007529SEric Paris security_init_mnt_opts(&opts); 940e0007529SEric Paris 941e0007529SEric Paris if (!data) 942e0007529SEric Paris goto out; 943e0007529SEric Paris 944e0007529SEric Paris BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA); 945e0007529SEric Paris 946e0007529SEric Paris rc = selinux_parse_opts_str(options, &opts); 947e0007529SEric Paris if (rc) 948e0007529SEric Paris goto out_err; 949e0007529SEric Paris 950e0007529SEric Paris out: 951e0007529SEric Paris rc = selinux_set_mnt_opts(sb, &opts); 952e0007529SEric Paris 953e0007529SEric Paris out_err: 954e0007529SEric Paris security_free_mnt_opts(&opts); 955e0007529SEric Paris return rc; 956e0007529SEric Paris } 9571da177e4SLinus Torvalds 9581da177e4SLinus Torvalds static inline u16 inode_mode_to_security_class(umode_t mode) 9591da177e4SLinus Torvalds { 9601da177e4SLinus Torvalds switch (mode & S_IFMT) { 9611da177e4SLinus Torvalds case S_IFSOCK: 9621da177e4SLinus Torvalds return SECCLASS_SOCK_FILE; 9631da177e4SLinus Torvalds case S_IFLNK: 9641da177e4SLinus Torvalds return SECCLASS_LNK_FILE; 9651da177e4SLinus Torvalds case S_IFREG: 9661da177e4SLinus Torvalds return SECCLASS_FILE; 9671da177e4SLinus Torvalds case S_IFBLK: 9681da177e4SLinus Torvalds return SECCLASS_BLK_FILE; 9691da177e4SLinus Torvalds case S_IFDIR: 9701da177e4SLinus Torvalds return SECCLASS_DIR; 9711da177e4SLinus Torvalds case S_IFCHR: 9721da177e4SLinus Torvalds return SECCLASS_CHR_FILE; 9731da177e4SLinus Torvalds case S_IFIFO: 9741da177e4SLinus Torvalds return SECCLASS_FIFO_FILE; 9751da177e4SLinus Torvalds 9761da177e4SLinus Torvalds } 9771da177e4SLinus Torvalds 9781da177e4SLinus Torvalds return SECCLASS_FILE; 9791da177e4SLinus Torvalds } 9801da177e4SLinus Torvalds 98113402580SJames Morris static inline int default_protocol_stream(int protocol) 98213402580SJames Morris { 98313402580SJames Morris return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP); 98413402580SJames Morris } 98513402580SJames Morris 98613402580SJames Morris static inline int default_protocol_dgram(int protocol) 98713402580SJames Morris { 98813402580SJames Morris return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP); 98913402580SJames Morris } 99013402580SJames Morris 9911da177e4SLinus Torvalds static inline u16 socket_type_to_security_class(int family, int type, int protocol) 9921da177e4SLinus Torvalds { 9931da177e4SLinus Torvalds switch (family) { 9941da177e4SLinus Torvalds case PF_UNIX: 9951da177e4SLinus Torvalds switch (type) { 9961da177e4SLinus Torvalds case SOCK_STREAM: 9971da177e4SLinus Torvalds case SOCK_SEQPACKET: 9981da177e4SLinus Torvalds return SECCLASS_UNIX_STREAM_SOCKET; 9991da177e4SLinus Torvalds case SOCK_DGRAM: 10001da177e4SLinus Torvalds return SECCLASS_UNIX_DGRAM_SOCKET; 10011da177e4SLinus Torvalds } 10021da177e4SLinus Torvalds break; 10031da177e4SLinus Torvalds case PF_INET: 10041da177e4SLinus Torvalds case PF_INET6: 10051da177e4SLinus Torvalds switch (type) { 10061da177e4SLinus Torvalds case SOCK_STREAM: 100713402580SJames Morris if (default_protocol_stream(protocol)) 10081da177e4SLinus Torvalds return SECCLASS_TCP_SOCKET; 100913402580SJames Morris else 101013402580SJames Morris return SECCLASS_RAWIP_SOCKET; 10111da177e4SLinus Torvalds case SOCK_DGRAM: 101213402580SJames Morris if (default_protocol_dgram(protocol)) 10131da177e4SLinus Torvalds return SECCLASS_UDP_SOCKET; 101413402580SJames Morris else 101513402580SJames Morris return SECCLASS_RAWIP_SOCKET; 10162ee92d46SJames Morris case SOCK_DCCP: 10172ee92d46SJames Morris return SECCLASS_DCCP_SOCKET; 101813402580SJames Morris default: 10191da177e4SLinus Torvalds return SECCLASS_RAWIP_SOCKET; 10201da177e4SLinus Torvalds } 10211da177e4SLinus Torvalds break; 10221da177e4SLinus Torvalds case PF_NETLINK: 10231da177e4SLinus Torvalds switch (protocol) { 10241da177e4SLinus Torvalds case NETLINK_ROUTE: 10251da177e4SLinus Torvalds return SECCLASS_NETLINK_ROUTE_SOCKET; 10261da177e4SLinus Torvalds case NETLINK_FIREWALL: 10271da177e4SLinus Torvalds return SECCLASS_NETLINK_FIREWALL_SOCKET; 1028216efaaaSJames Morris case NETLINK_INET_DIAG: 10291da177e4SLinus Torvalds return SECCLASS_NETLINK_TCPDIAG_SOCKET; 10301da177e4SLinus Torvalds case NETLINK_NFLOG: 10311da177e4SLinus Torvalds return SECCLASS_NETLINK_NFLOG_SOCKET; 10321da177e4SLinus Torvalds case NETLINK_XFRM: 10331da177e4SLinus Torvalds return SECCLASS_NETLINK_XFRM_SOCKET; 10341da177e4SLinus Torvalds case NETLINK_SELINUX: 10351da177e4SLinus Torvalds return SECCLASS_NETLINK_SELINUX_SOCKET; 10361da177e4SLinus Torvalds case NETLINK_AUDIT: 10371da177e4SLinus Torvalds return SECCLASS_NETLINK_AUDIT_SOCKET; 10381da177e4SLinus Torvalds case NETLINK_IP6_FW: 10391da177e4SLinus Torvalds return SECCLASS_NETLINK_IP6FW_SOCKET; 10401da177e4SLinus Torvalds case NETLINK_DNRTMSG: 10411da177e4SLinus Torvalds return SECCLASS_NETLINK_DNRT_SOCKET; 10420c9b7942SJames Morris case NETLINK_KOBJECT_UEVENT: 10430c9b7942SJames Morris return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET; 10441da177e4SLinus Torvalds default: 10451da177e4SLinus Torvalds return SECCLASS_NETLINK_SOCKET; 10461da177e4SLinus Torvalds } 10471da177e4SLinus Torvalds case PF_PACKET: 10481da177e4SLinus Torvalds return SECCLASS_PACKET_SOCKET; 10491da177e4SLinus Torvalds case PF_KEY: 10501da177e4SLinus Torvalds return SECCLASS_KEY_SOCKET; 10513e3ff15eSChristopher J. PeBenito case PF_APPLETALK: 10523e3ff15eSChristopher J. PeBenito return SECCLASS_APPLETALK_SOCKET; 10531da177e4SLinus Torvalds } 10541da177e4SLinus Torvalds 10551da177e4SLinus Torvalds return SECCLASS_SOCKET; 10561da177e4SLinus Torvalds } 10571da177e4SLinus Torvalds 10581da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 10591da177e4SLinus Torvalds static int selinux_proc_get_sid(struct proc_dir_entry *de, 10601da177e4SLinus Torvalds u16 tclass, 10611da177e4SLinus Torvalds u32 *sid) 10621da177e4SLinus Torvalds { 10631da177e4SLinus Torvalds int buflen, rc; 10641da177e4SLinus Torvalds char *buffer, *path, *end; 10651da177e4SLinus Torvalds 10661da177e4SLinus Torvalds buffer = (char *)__get_free_page(GFP_KERNEL); 10671da177e4SLinus Torvalds if (!buffer) 10681da177e4SLinus Torvalds return -ENOMEM; 10691da177e4SLinus Torvalds 10701da177e4SLinus Torvalds buflen = PAGE_SIZE; 10711da177e4SLinus Torvalds end = buffer+buflen; 10721da177e4SLinus Torvalds *--end = '\0'; 10731da177e4SLinus Torvalds buflen--; 10741da177e4SLinus Torvalds path = end-1; 10751da177e4SLinus Torvalds *path = '/'; 10761da177e4SLinus Torvalds while (de && de != de->parent) { 10771da177e4SLinus Torvalds buflen -= de->namelen + 1; 10781da177e4SLinus Torvalds if (buflen < 0) 10791da177e4SLinus Torvalds break; 10801da177e4SLinus Torvalds end -= de->namelen; 10811da177e4SLinus Torvalds memcpy(end, de->name, de->namelen); 10821da177e4SLinus Torvalds *--end = '/'; 10831da177e4SLinus Torvalds path = end; 10841da177e4SLinus Torvalds de = de->parent; 10851da177e4SLinus Torvalds } 10861da177e4SLinus Torvalds rc = security_genfs_sid("proc", path, tclass, sid); 10871da177e4SLinus Torvalds free_page((unsigned long)buffer); 10881da177e4SLinus Torvalds return rc; 10891da177e4SLinus Torvalds } 10901da177e4SLinus Torvalds #else 10911da177e4SLinus Torvalds static int selinux_proc_get_sid(struct proc_dir_entry *de, 10921da177e4SLinus Torvalds u16 tclass, 10931da177e4SLinus Torvalds u32 *sid) 10941da177e4SLinus Torvalds { 10951da177e4SLinus Torvalds return -EINVAL; 10961da177e4SLinus Torvalds } 10971da177e4SLinus Torvalds #endif 10981da177e4SLinus Torvalds 10991da177e4SLinus Torvalds /* The inode's security attributes must be initialized before first use. */ 11001da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry) 11011da177e4SLinus Torvalds { 11021da177e4SLinus Torvalds struct superblock_security_struct *sbsec = NULL; 11031da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 11041da177e4SLinus Torvalds u32 sid; 11051da177e4SLinus Torvalds struct dentry *dentry; 11061da177e4SLinus Torvalds #define INITCONTEXTLEN 255 11071da177e4SLinus Torvalds char *context = NULL; 11081da177e4SLinus Torvalds unsigned len = 0; 11091da177e4SLinus Torvalds int rc = 0; 11101da177e4SLinus Torvalds 11111da177e4SLinus Torvalds if (isec->initialized) 11121da177e4SLinus Torvalds goto out; 11131da177e4SLinus Torvalds 111423970741SEric Paris mutex_lock(&isec->lock); 11151da177e4SLinus Torvalds if (isec->initialized) 111623970741SEric Paris goto out_unlock; 11171da177e4SLinus Torvalds 11181da177e4SLinus Torvalds sbsec = inode->i_sb->s_security; 11191da177e4SLinus Torvalds if (!sbsec->initialized) { 11201da177e4SLinus Torvalds /* Defer initialization until selinux_complete_init, 11211da177e4SLinus Torvalds after the initial policy is loaded and the security 11221da177e4SLinus Torvalds server is ready to handle calls. */ 11231da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 11241da177e4SLinus Torvalds if (list_empty(&isec->list)) 11251da177e4SLinus Torvalds list_add(&isec->list, &sbsec->isec_head); 11261da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 112723970741SEric Paris goto out_unlock; 11281da177e4SLinus Torvalds } 11291da177e4SLinus Torvalds 11301da177e4SLinus Torvalds switch (sbsec->behavior) { 11311da177e4SLinus Torvalds case SECURITY_FS_USE_XATTR: 11321da177e4SLinus Torvalds if (!inode->i_op->getxattr) { 11331da177e4SLinus Torvalds isec->sid = sbsec->def_sid; 11341da177e4SLinus Torvalds break; 11351da177e4SLinus Torvalds } 11361da177e4SLinus Torvalds 11371da177e4SLinus Torvalds /* Need a dentry, since the xattr API requires one. 11381da177e4SLinus Torvalds Life would be simpler if we could just pass the inode. */ 11391da177e4SLinus Torvalds if (opt_dentry) { 11401da177e4SLinus Torvalds /* Called from d_instantiate or d_splice_alias. */ 11411da177e4SLinus Torvalds dentry = dget(opt_dentry); 11421da177e4SLinus Torvalds } else { 11431da177e4SLinus Torvalds /* Called from selinux_complete_init, try to find a dentry. */ 11441da177e4SLinus Torvalds dentry = d_find_alias(inode); 11451da177e4SLinus Torvalds } 11461da177e4SLinus Torvalds if (!dentry) { 1147744ba35eSEric Paris printk(KERN_WARNING "SELinux: %s: no dentry for dev=%s " 1148dd6f953aSHarvey Harrison "ino=%ld\n", __func__, inode->i_sb->s_id, 11491da177e4SLinus Torvalds inode->i_ino); 115023970741SEric Paris goto out_unlock; 11511da177e4SLinus Torvalds } 11521da177e4SLinus Torvalds 11531da177e4SLinus Torvalds len = INITCONTEXTLEN; 1154869ab514SStephen Smalley context = kmalloc(len, GFP_NOFS); 11551da177e4SLinus Torvalds if (!context) { 11561da177e4SLinus Torvalds rc = -ENOMEM; 11571da177e4SLinus Torvalds dput(dentry); 115823970741SEric Paris goto out_unlock; 11591da177e4SLinus Torvalds } 11601da177e4SLinus Torvalds rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX, 11611da177e4SLinus Torvalds context, len); 11621da177e4SLinus Torvalds if (rc == -ERANGE) { 11631da177e4SLinus Torvalds /* Need a larger buffer. Query for the right size. */ 11641da177e4SLinus Torvalds rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX, 11651da177e4SLinus Torvalds NULL, 0); 11661da177e4SLinus Torvalds if (rc < 0) { 11671da177e4SLinus Torvalds dput(dentry); 116823970741SEric Paris goto out_unlock; 11691da177e4SLinus Torvalds } 11701da177e4SLinus Torvalds kfree(context); 11711da177e4SLinus Torvalds len = rc; 1172869ab514SStephen Smalley context = kmalloc(len, GFP_NOFS); 11731da177e4SLinus Torvalds if (!context) { 11741da177e4SLinus Torvalds rc = -ENOMEM; 11751da177e4SLinus Torvalds dput(dentry); 117623970741SEric Paris goto out_unlock; 11771da177e4SLinus Torvalds } 11781da177e4SLinus Torvalds rc = inode->i_op->getxattr(dentry, 11791da177e4SLinus Torvalds XATTR_NAME_SELINUX, 11801da177e4SLinus Torvalds context, len); 11811da177e4SLinus Torvalds } 11821da177e4SLinus Torvalds dput(dentry); 11831da177e4SLinus Torvalds if (rc < 0) { 11841da177e4SLinus Torvalds if (rc != -ENODATA) { 1185744ba35eSEric Paris printk(KERN_WARNING "SELinux: %s: getxattr returned " 1186dd6f953aSHarvey Harrison "%d for dev=%s ino=%ld\n", __func__, 11871da177e4SLinus Torvalds -rc, inode->i_sb->s_id, inode->i_ino); 11881da177e4SLinus Torvalds kfree(context); 118923970741SEric Paris goto out_unlock; 11901da177e4SLinus Torvalds } 11911da177e4SLinus Torvalds /* Map ENODATA to the default file SID */ 11921da177e4SLinus Torvalds sid = sbsec->def_sid; 11931da177e4SLinus Torvalds rc = 0; 11941da177e4SLinus Torvalds } else { 1195f5c1d5b2SJames Morris rc = security_context_to_sid_default(context, rc, &sid, 1196869ab514SStephen Smalley sbsec->def_sid, 1197869ab514SStephen Smalley GFP_NOFS); 11981da177e4SLinus Torvalds if (rc) { 1199744ba35eSEric Paris printk(KERN_WARNING "SELinux: %s: context_to_sid(%s) " 12001da177e4SLinus Torvalds "returned %d for dev=%s ino=%ld\n", 1201dd6f953aSHarvey Harrison __func__, context, -rc, 12021da177e4SLinus Torvalds inode->i_sb->s_id, inode->i_ino); 12031da177e4SLinus Torvalds kfree(context); 12041da177e4SLinus Torvalds /* Leave with the unlabeled SID */ 12051da177e4SLinus Torvalds rc = 0; 12061da177e4SLinus Torvalds break; 12071da177e4SLinus Torvalds } 12081da177e4SLinus Torvalds } 12091da177e4SLinus Torvalds kfree(context); 12101da177e4SLinus Torvalds isec->sid = sid; 12111da177e4SLinus Torvalds break; 12121da177e4SLinus Torvalds case SECURITY_FS_USE_TASK: 12131da177e4SLinus Torvalds isec->sid = isec->task_sid; 12141da177e4SLinus Torvalds break; 12151da177e4SLinus Torvalds case SECURITY_FS_USE_TRANS: 12161da177e4SLinus Torvalds /* Default to the fs SID. */ 12171da177e4SLinus Torvalds isec->sid = sbsec->sid; 12181da177e4SLinus Torvalds 12191da177e4SLinus Torvalds /* Try to obtain a transition SID. */ 12201da177e4SLinus Torvalds isec->sclass = inode_mode_to_security_class(inode->i_mode); 12211da177e4SLinus Torvalds rc = security_transition_sid(isec->task_sid, 12221da177e4SLinus Torvalds sbsec->sid, 12231da177e4SLinus Torvalds isec->sclass, 12241da177e4SLinus Torvalds &sid); 12251da177e4SLinus Torvalds if (rc) 122623970741SEric Paris goto out_unlock; 12271da177e4SLinus Torvalds isec->sid = sid; 12281da177e4SLinus Torvalds break; 1229c312feb2SEric Paris case SECURITY_FS_USE_MNTPOINT: 1230c312feb2SEric Paris isec->sid = sbsec->mntpoint_sid; 1231c312feb2SEric Paris break; 12321da177e4SLinus Torvalds default: 1233c312feb2SEric Paris /* Default to the fs superblock SID. */ 12341da177e4SLinus Torvalds isec->sid = sbsec->sid; 12351da177e4SLinus Torvalds 12361da177e4SLinus Torvalds if (sbsec->proc) { 12371da177e4SLinus Torvalds struct proc_inode *proci = PROC_I(inode); 12381da177e4SLinus Torvalds if (proci->pde) { 12391da177e4SLinus Torvalds isec->sclass = inode_mode_to_security_class(inode->i_mode); 12401da177e4SLinus Torvalds rc = selinux_proc_get_sid(proci->pde, 12411da177e4SLinus Torvalds isec->sclass, 12421da177e4SLinus Torvalds &sid); 12431da177e4SLinus Torvalds if (rc) 124423970741SEric Paris goto out_unlock; 12451da177e4SLinus Torvalds isec->sid = sid; 12461da177e4SLinus Torvalds } 12471da177e4SLinus Torvalds } 12481da177e4SLinus Torvalds break; 12491da177e4SLinus Torvalds } 12501da177e4SLinus Torvalds 12511da177e4SLinus Torvalds isec->initialized = 1; 12521da177e4SLinus Torvalds 125323970741SEric Paris out_unlock: 125423970741SEric Paris mutex_unlock(&isec->lock); 12551da177e4SLinus Torvalds out: 12561da177e4SLinus Torvalds if (isec->sclass == SECCLASS_FILE) 12571da177e4SLinus Torvalds isec->sclass = inode_mode_to_security_class(inode->i_mode); 12581da177e4SLinus Torvalds return rc; 12591da177e4SLinus Torvalds } 12601da177e4SLinus Torvalds 12611da177e4SLinus Torvalds /* Convert a Linux signal to an access vector. */ 12621da177e4SLinus Torvalds static inline u32 signal_to_av(int sig) 12631da177e4SLinus Torvalds { 12641da177e4SLinus Torvalds u32 perm = 0; 12651da177e4SLinus Torvalds 12661da177e4SLinus Torvalds switch (sig) { 12671da177e4SLinus Torvalds case SIGCHLD: 12681da177e4SLinus Torvalds /* Commonly granted from child to parent. */ 12691da177e4SLinus Torvalds perm = PROCESS__SIGCHLD; 12701da177e4SLinus Torvalds break; 12711da177e4SLinus Torvalds case SIGKILL: 12721da177e4SLinus Torvalds /* Cannot be caught or ignored */ 12731da177e4SLinus Torvalds perm = PROCESS__SIGKILL; 12741da177e4SLinus Torvalds break; 12751da177e4SLinus Torvalds case SIGSTOP: 12761da177e4SLinus Torvalds /* Cannot be caught or ignored */ 12771da177e4SLinus Torvalds perm = PROCESS__SIGSTOP; 12781da177e4SLinus Torvalds break; 12791da177e4SLinus Torvalds default: 12801da177e4SLinus Torvalds /* All other signals. */ 12811da177e4SLinus Torvalds perm = PROCESS__SIGNAL; 12821da177e4SLinus Torvalds break; 12831da177e4SLinus Torvalds } 12841da177e4SLinus Torvalds 12851da177e4SLinus Torvalds return perm; 12861da177e4SLinus Torvalds } 12871da177e4SLinus Torvalds 12881da177e4SLinus Torvalds /* Check permission betweeen a pair of tasks, e.g. signal checks, 12891da177e4SLinus Torvalds fork check, ptrace check, etc. */ 12901da177e4SLinus Torvalds static int task_has_perm(struct task_struct *tsk1, 12911da177e4SLinus Torvalds struct task_struct *tsk2, 12921da177e4SLinus Torvalds u32 perms) 12931da177e4SLinus Torvalds { 12941da177e4SLinus Torvalds struct task_security_struct *tsec1, *tsec2; 12951da177e4SLinus Torvalds 12961da177e4SLinus Torvalds tsec1 = tsk1->security; 12971da177e4SLinus Torvalds tsec2 = tsk2->security; 12981da177e4SLinus Torvalds return avc_has_perm(tsec1->sid, tsec2->sid, 12991da177e4SLinus Torvalds SECCLASS_PROCESS, perms, NULL); 13001da177e4SLinus Torvalds } 13011da177e4SLinus Torvalds 1302b68e418cSStephen Smalley #if CAP_LAST_CAP > 63 1303b68e418cSStephen Smalley #error Fix SELinux to handle capabilities > 63. 1304b68e418cSStephen Smalley #endif 1305b68e418cSStephen Smalley 13061da177e4SLinus Torvalds /* Check whether a task is allowed to use a capability. */ 13071da177e4SLinus Torvalds static int task_has_capability(struct task_struct *tsk, 13081da177e4SLinus Torvalds int cap) 13091da177e4SLinus Torvalds { 13101da177e4SLinus Torvalds struct task_security_struct *tsec; 13111da177e4SLinus Torvalds struct avc_audit_data ad; 1312b68e418cSStephen Smalley u16 sclass; 1313b68e418cSStephen Smalley u32 av = CAP_TO_MASK(cap); 13141da177e4SLinus Torvalds 13151da177e4SLinus Torvalds tsec = tsk->security; 13161da177e4SLinus Torvalds 13171da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, CAP); 13181da177e4SLinus Torvalds ad.tsk = tsk; 13191da177e4SLinus Torvalds ad.u.cap = cap; 13201da177e4SLinus Torvalds 1321b68e418cSStephen Smalley switch (CAP_TO_INDEX(cap)) { 1322b68e418cSStephen Smalley case 0: 1323b68e418cSStephen Smalley sclass = SECCLASS_CAPABILITY; 1324b68e418cSStephen Smalley break; 1325b68e418cSStephen Smalley case 1: 1326b68e418cSStephen Smalley sclass = SECCLASS_CAPABILITY2; 1327b68e418cSStephen Smalley break; 1328b68e418cSStephen Smalley default: 1329b68e418cSStephen Smalley printk(KERN_ERR 1330b68e418cSStephen Smalley "SELinux: out of range capability %d\n", cap); 1331b68e418cSStephen Smalley BUG(); 1332b68e418cSStephen Smalley } 1333b68e418cSStephen Smalley return avc_has_perm(tsec->sid, tsec->sid, sclass, av, &ad); 13341da177e4SLinus Torvalds } 13351da177e4SLinus Torvalds 13361da177e4SLinus Torvalds /* Check whether a task is allowed to use a system operation. */ 13371da177e4SLinus Torvalds static int task_has_system(struct task_struct *tsk, 13381da177e4SLinus Torvalds u32 perms) 13391da177e4SLinus Torvalds { 13401da177e4SLinus Torvalds struct task_security_struct *tsec; 13411da177e4SLinus Torvalds 13421da177e4SLinus Torvalds tsec = tsk->security; 13431da177e4SLinus Torvalds 13441da177e4SLinus Torvalds return avc_has_perm(tsec->sid, SECINITSID_KERNEL, 13451da177e4SLinus Torvalds SECCLASS_SYSTEM, perms, NULL); 13461da177e4SLinus Torvalds } 13471da177e4SLinus Torvalds 13481da177e4SLinus Torvalds /* Check whether a task has a particular permission to an inode. 13491da177e4SLinus Torvalds The 'adp' parameter is optional and allows other audit 13501da177e4SLinus Torvalds data to be passed (e.g. the dentry). */ 13511da177e4SLinus Torvalds static int inode_has_perm(struct task_struct *tsk, 13521da177e4SLinus Torvalds struct inode *inode, 13531da177e4SLinus Torvalds u32 perms, 13541da177e4SLinus Torvalds struct avc_audit_data *adp) 13551da177e4SLinus Torvalds { 13561da177e4SLinus Torvalds struct task_security_struct *tsec; 13571da177e4SLinus Torvalds struct inode_security_struct *isec; 13581da177e4SLinus Torvalds struct avc_audit_data ad; 13591da177e4SLinus Torvalds 1360bbaca6c2SStephen Smalley if (unlikely(IS_PRIVATE(inode))) 1361bbaca6c2SStephen Smalley return 0; 1362bbaca6c2SStephen Smalley 13631da177e4SLinus Torvalds tsec = tsk->security; 13641da177e4SLinus Torvalds isec = inode->i_security; 13651da177e4SLinus Torvalds 13661da177e4SLinus Torvalds if (!adp) { 13671da177e4SLinus Torvalds adp = &ad; 13681da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 13691da177e4SLinus Torvalds ad.u.fs.inode = inode; 13701da177e4SLinus Torvalds } 13711da177e4SLinus Torvalds 13721da177e4SLinus Torvalds return avc_has_perm(tsec->sid, isec->sid, isec->sclass, perms, adp); 13731da177e4SLinus Torvalds } 13741da177e4SLinus Torvalds 13751da177e4SLinus Torvalds /* Same as inode_has_perm, but pass explicit audit data containing 13761da177e4SLinus Torvalds the dentry to help the auditing code to more easily generate the 13771da177e4SLinus Torvalds pathname if needed. */ 13781da177e4SLinus Torvalds static inline int dentry_has_perm(struct task_struct *tsk, 13791da177e4SLinus Torvalds struct vfsmount *mnt, 13801da177e4SLinus Torvalds struct dentry *dentry, 13811da177e4SLinus Torvalds u32 av) 13821da177e4SLinus Torvalds { 13831da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 13841da177e4SLinus Torvalds struct avc_audit_data ad; 13851da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 138644707fdfSJan Blunck ad.u.fs.path.mnt = mnt; 138744707fdfSJan Blunck ad.u.fs.path.dentry = dentry; 13881da177e4SLinus Torvalds return inode_has_perm(tsk, inode, av, &ad); 13891da177e4SLinus Torvalds } 13901da177e4SLinus Torvalds 13911da177e4SLinus Torvalds /* Check whether a task can use an open file descriptor to 13921da177e4SLinus Torvalds access an inode in a given way. Check access to the 13931da177e4SLinus Torvalds descriptor itself, and then use dentry_has_perm to 13941da177e4SLinus Torvalds check a particular permission to the file. 13951da177e4SLinus Torvalds Access to the descriptor is implicitly granted if it 13961da177e4SLinus Torvalds has the same SID as the process. If av is zero, then 13971da177e4SLinus Torvalds access to the file is not checked, e.g. for cases 13981da177e4SLinus Torvalds where only the descriptor is affected like seek. */ 1399858119e1SArjan van de Ven static int file_has_perm(struct task_struct *tsk, 14001da177e4SLinus Torvalds struct file *file, 14011da177e4SLinus Torvalds u32 av) 14021da177e4SLinus Torvalds { 14031da177e4SLinus Torvalds struct task_security_struct *tsec = tsk->security; 14041da177e4SLinus Torvalds struct file_security_struct *fsec = file->f_security; 140544707fdfSJan Blunck struct inode *inode = file->f_path.dentry->d_inode; 14061da177e4SLinus Torvalds struct avc_audit_data ad; 14071da177e4SLinus Torvalds int rc; 14081da177e4SLinus Torvalds 14091da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 141044707fdfSJan Blunck ad.u.fs.path = file->f_path; 14111da177e4SLinus Torvalds 14121da177e4SLinus Torvalds if (tsec->sid != fsec->sid) { 14131da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, fsec->sid, 14141da177e4SLinus Torvalds SECCLASS_FD, 14151da177e4SLinus Torvalds FD__USE, 14161da177e4SLinus Torvalds &ad); 14171da177e4SLinus Torvalds if (rc) 14181da177e4SLinus Torvalds return rc; 14191da177e4SLinus Torvalds } 14201da177e4SLinus Torvalds 14211da177e4SLinus Torvalds /* av is zero if only checking access to the descriptor. */ 14221da177e4SLinus Torvalds if (av) 14231da177e4SLinus Torvalds return inode_has_perm(tsk, inode, av, &ad); 14241da177e4SLinus Torvalds 14251da177e4SLinus Torvalds return 0; 14261da177e4SLinus Torvalds } 14271da177e4SLinus Torvalds 14281da177e4SLinus Torvalds /* Check whether a task can create a file. */ 14291da177e4SLinus Torvalds static int may_create(struct inode *dir, 14301da177e4SLinus Torvalds struct dentry *dentry, 14311da177e4SLinus Torvalds u16 tclass) 14321da177e4SLinus Torvalds { 14331da177e4SLinus Torvalds struct task_security_struct *tsec; 14341da177e4SLinus Torvalds struct inode_security_struct *dsec; 14351da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 14361da177e4SLinus Torvalds u32 newsid; 14371da177e4SLinus Torvalds struct avc_audit_data ad; 14381da177e4SLinus Torvalds int rc; 14391da177e4SLinus Torvalds 14401da177e4SLinus Torvalds tsec = current->security; 14411da177e4SLinus Torvalds dsec = dir->i_security; 14421da177e4SLinus Torvalds sbsec = dir->i_sb->s_security; 14431da177e4SLinus Torvalds 14441da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 144544707fdfSJan Blunck ad.u.fs.path.dentry = dentry; 14461da177e4SLinus Torvalds 14471da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, dsec->sid, SECCLASS_DIR, 14481da177e4SLinus Torvalds DIR__ADD_NAME | DIR__SEARCH, 14491da177e4SLinus Torvalds &ad); 14501da177e4SLinus Torvalds if (rc) 14511da177e4SLinus Torvalds return rc; 14521da177e4SLinus Torvalds 14531da177e4SLinus Torvalds if (tsec->create_sid && sbsec->behavior != SECURITY_FS_USE_MNTPOINT) { 14541da177e4SLinus Torvalds newsid = tsec->create_sid; 14551da177e4SLinus Torvalds } else { 14561da177e4SLinus Torvalds rc = security_transition_sid(tsec->sid, dsec->sid, tclass, 14571da177e4SLinus Torvalds &newsid); 14581da177e4SLinus Torvalds if (rc) 14591da177e4SLinus Torvalds return rc; 14601da177e4SLinus Torvalds } 14611da177e4SLinus Torvalds 14621da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, newsid, tclass, FILE__CREATE, &ad); 14631da177e4SLinus Torvalds if (rc) 14641da177e4SLinus Torvalds return rc; 14651da177e4SLinus Torvalds 14661da177e4SLinus Torvalds return avc_has_perm(newsid, sbsec->sid, 14671da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 14681da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, &ad); 14691da177e4SLinus Torvalds } 14701da177e4SLinus Torvalds 14714eb582cfSMichael LeMay /* Check whether a task can create a key. */ 14724eb582cfSMichael LeMay static int may_create_key(u32 ksid, 14734eb582cfSMichael LeMay struct task_struct *ctx) 14744eb582cfSMichael LeMay { 14754eb582cfSMichael LeMay struct task_security_struct *tsec; 14764eb582cfSMichael LeMay 14774eb582cfSMichael LeMay tsec = ctx->security; 14784eb582cfSMichael LeMay 14794eb582cfSMichael LeMay return avc_has_perm(tsec->sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL); 14804eb582cfSMichael LeMay } 14814eb582cfSMichael LeMay 14821da177e4SLinus Torvalds #define MAY_LINK 0 14831da177e4SLinus Torvalds #define MAY_UNLINK 1 14841da177e4SLinus Torvalds #define MAY_RMDIR 2 14851da177e4SLinus Torvalds 14861da177e4SLinus Torvalds /* Check whether a task can link, unlink, or rmdir a file/directory. */ 14871da177e4SLinus Torvalds static int may_link(struct inode *dir, 14881da177e4SLinus Torvalds struct dentry *dentry, 14891da177e4SLinus Torvalds int kind) 14901da177e4SLinus Torvalds 14911da177e4SLinus Torvalds { 14921da177e4SLinus Torvalds struct task_security_struct *tsec; 14931da177e4SLinus Torvalds struct inode_security_struct *dsec, *isec; 14941da177e4SLinus Torvalds struct avc_audit_data ad; 14951da177e4SLinus Torvalds u32 av; 14961da177e4SLinus Torvalds int rc; 14971da177e4SLinus Torvalds 14981da177e4SLinus Torvalds tsec = current->security; 14991da177e4SLinus Torvalds dsec = dir->i_security; 15001da177e4SLinus Torvalds isec = dentry->d_inode->i_security; 15011da177e4SLinus Torvalds 15021da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 150344707fdfSJan Blunck ad.u.fs.path.dentry = dentry; 15041da177e4SLinus Torvalds 15051da177e4SLinus Torvalds av = DIR__SEARCH; 15061da177e4SLinus Torvalds av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME); 15071da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, dsec->sid, SECCLASS_DIR, av, &ad); 15081da177e4SLinus Torvalds if (rc) 15091da177e4SLinus Torvalds return rc; 15101da177e4SLinus Torvalds 15111da177e4SLinus Torvalds switch (kind) { 15121da177e4SLinus Torvalds case MAY_LINK: 15131da177e4SLinus Torvalds av = FILE__LINK; 15141da177e4SLinus Torvalds break; 15151da177e4SLinus Torvalds case MAY_UNLINK: 15161da177e4SLinus Torvalds av = FILE__UNLINK; 15171da177e4SLinus Torvalds break; 15181da177e4SLinus Torvalds case MAY_RMDIR: 15191da177e4SLinus Torvalds av = DIR__RMDIR; 15201da177e4SLinus Torvalds break; 15211da177e4SLinus Torvalds default: 1522744ba35eSEric Paris printk(KERN_WARNING "SELinux: %s: unrecognized kind %d\n", 1523744ba35eSEric Paris __func__, kind); 15241da177e4SLinus Torvalds return 0; 15251da177e4SLinus Torvalds } 15261da177e4SLinus Torvalds 15271da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, isec->sclass, av, &ad); 15281da177e4SLinus Torvalds return rc; 15291da177e4SLinus Torvalds } 15301da177e4SLinus Torvalds 15311da177e4SLinus Torvalds static inline int may_rename(struct inode *old_dir, 15321da177e4SLinus Torvalds struct dentry *old_dentry, 15331da177e4SLinus Torvalds struct inode *new_dir, 15341da177e4SLinus Torvalds struct dentry *new_dentry) 15351da177e4SLinus Torvalds { 15361da177e4SLinus Torvalds struct task_security_struct *tsec; 15371da177e4SLinus Torvalds struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec; 15381da177e4SLinus Torvalds struct avc_audit_data ad; 15391da177e4SLinus Torvalds u32 av; 15401da177e4SLinus Torvalds int old_is_dir, new_is_dir; 15411da177e4SLinus Torvalds int rc; 15421da177e4SLinus Torvalds 15431da177e4SLinus Torvalds tsec = current->security; 15441da177e4SLinus Torvalds old_dsec = old_dir->i_security; 15451da177e4SLinus Torvalds old_isec = old_dentry->d_inode->i_security; 15461da177e4SLinus Torvalds old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 15471da177e4SLinus Torvalds new_dsec = new_dir->i_security; 15481da177e4SLinus Torvalds 15491da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 15501da177e4SLinus Torvalds 155144707fdfSJan Blunck ad.u.fs.path.dentry = old_dentry; 15521da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, old_dsec->sid, SECCLASS_DIR, 15531da177e4SLinus Torvalds DIR__REMOVE_NAME | DIR__SEARCH, &ad); 15541da177e4SLinus Torvalds if (rc) 15551da177e4SLinus Torvalds return rc; 15561da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, old_isec->sid, 15571da177e4SLinus Torvalds old_isec->sclass, FILE__RENAME, &ad); 15581da177e4SLinus Torvalds if (rc) 15591da177e4SLinus Torvalds return rc; 15601da177e4SLinus Torvalds if (old_is_dir && new_dir != old_dir) { 15611da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, old_isec->sid, 15621da177e4SLinus Torvalds old_isec->sclass, DIR__REPARENT, &ad); 15631da177e4SLinus Torvalds if (rc) 15641da177e4SLinus Torvalds return rc; 15651da177e4SLinus Torvalds } 15661da177e4SLinus Torvalds 156744707fdfSJan Blunck ad.u.fs.path.dentry = new_dentry; 15681da177e4SLinus Torvalds av = DIR__ADD_NAME | DIR__SEARCH; 15691da177e4SLinus Torvalds if (new_dentry->d_inode) 15701da177e4SLinus Torvalds av |= DIR__REMOVE_NAME; 15711da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, new_dsec->sid, SECCLASS_DIR, av, &ad); 15721da177e4SLinus Torvalds if (rc) 15731da177e4SLinus Torvalds return rc; 15741da177e4SLinus Torvalds if (new_dentry->d_inode) { 15751da177e4SLinus Torvalds new_isec = new_dentry->d_inode->i_security; 15761da177e4SLinus Torvalds new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode); 15771da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, new_isec->sid, 15781da177e4SLinus Torvalds new_isec->sclass, 15791da177e4SLinus Torvalds (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad); 15801da177e4SLinus Torvalds if (rc) 15811da177e4SLinus Torvalds return rc; 15821da177e4SLinus Torvalds } 15831da177e4SLinus Torvalds 15841da177e4SLinus Torvalds return 0; 15851da177e4SLinus Torvalds } 15861da177e4SLinus Torvalds 15871da177e4SLinus Torvalds /* Check whether a task can perform a filesystem operation. */ 15881da177e4SLinus Torvalds static int superblock_has_perm(struct task_struct *tsk, 15891da177e4SLinus Torvalds struct super_block *sb, 15901da177e4SLinus Torvalds u32 perms, 15911da177e4SLinus Torvalds struct avc_audit_data *ad) 15921da177e4SLinus Torvalds { 15931da177e4SLinus Torvalds struct task_security_struct *tsec; 15941da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 15951da177e4SLinus Torvalds 15961da177e4SLinus Torvalds tsec = tsk->security; 15971da177e4SLinus Torvalds sbsec = sb->s_security; 15981da177e4SLinus Torvalds return avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 15991da177e4SLinus Torvalds perms, ad); 16001da177e4SLinus Torvalds } 16011da177e4SLinus Torvalds 16021da177e4SLinus Torvalds /* Convert a Linux mode and permission mask to an access vector. */ 16031da177e4SLinus Torvalds static inline u32 file_mask_to_av(int mode, int mask) 16041da177e4SLinus Torvalds { 16051da177e4SLinus Torvalds u32 av = 0; 16061da177e4SLinus Torvalds 16071da177e4SLinus Torvalds if ((mode & S_IFMT) != S_IFDIR) { 16081da177e4SLinus Torvalds if (mask & MAY_EXEC) 16091da177e4SLinus Torvalds av |= FILE__EXECUTE; 16101da177e4SLinus Torvalds if (mask & MAY_READ) 16111da177e4SLinus Torvalds av |= FILE__READ; 16121da177e4SLinus Torvalds 16131da177e4SLinus Torvalds if (mask & MAY_APPEND) 16141da177e4SLinus Torvalds av |= FILE__APPEND; 16151da177e4SLinus Torvalds else if (mask & MAY_WRITE) 16161da177e4SLinus Torvalds av |= FILE__WRITE; 16171da177e4SLinus Torvalds 16181da177e4SLinus Torvalds } else { 16191da177e4SLinus Torvalds if (mask & MAY_EXEC) 16201da177e4SLinus Torvalds av |= DIR__SEARCH; 16211da177e4SLinus Torvalds if (mask & MAY_WRITE) 16221da177e4SLinus Torvalds av |= DIR__WRITE; 16231da177e4SLinus Torvalds if (mask & MAY_READ) 16241da177e4SLinus Torvalds av |= DIR__READ; 16251da177e4SLinus Torvalds } 16261da177e4SLinus Torvalds 16271da177e4SLinus Torvalds return av; 16281da177e4SLinus Torvalds } 16291da177e4SLinus Torvalds 1630b0c636b9SEric Paris /* 1631b0c636b9SEric Paris * Convert a file mask to an access vector and include the correct open 1632b0c636b9SEric Paris * open permission. 1633b0c636b9SEric Paris */ 1634b0c636b9SEric Paris static inline u32 open_file_mask_to_av(int mode, int mask) 1635b0c636b9SEric Paris { 1636b0c636b9SEric Paris u32 av = file_mask_to_av(mode, mask); 1637b0c636b9SEric Paris 1638b0c636b9SEric Paris if (selinux_policycap_openperm) { 1639b0c636b9SEric Paris /* 1640b0c636b9SEric Paris * lnk files and socks do not really have an 'open' 1641b0c636b9SEric Paris */ 1642b0c636b9SEric Paris if (S_ISREG(mode)) 1643b0c636b9SEric Paris av |= FILE__OPEN; 1644b0c636b9SEric Paris else if (S_ISCHR(mode)) 1645b0c636b9SEric Paris av |= CHR_FILE__OPEN; 1646b0c636b9SEric Paris else if (S_ISBLK(mode)) 1647b0c636b9SEric Paris av |= BLK_FILE__OPEN; 1648b0c636b9SEric Paris else if (S_ISFIFO(mode)) 1649b0c636b9SEric Paris av |= FIFO_FILE__OPEN; 1650b0c636b9SEric Paris else if (S_ISDIR(mode)) 1651b0c636b9SEric Paris av |= DIR__OPEN; 1652b0c636b9SEric Paris else 1653744ba35eSEric Paris printk(KERN_ERR "SELinux: WARNING: inside %s with " 1654744ba35eSEric Paris "unknown mode:%x\n", __func__, mode); 1655b0c636b9SEric Paris } 1656b0c636b9SEric Paris return av; 1657b0c636b9SEric Paris } 1658b0c636b9SEric Paris 16591da177e4SLinus Torvalds /* Convert a Linux file to an access vector. */ 16601da177e4SLinus Torvalds static inline u32 file_to_av(struct file *file) 16611da177e4SLinus Torvalds { 16621da177e4SLinus Torvalds u32 av = 0; 16631da177e4SLinus Torvalds 16641da177e4SLinus Torvalds if (file->f_mode & FMODE_READ) 16651da177e4SLinus Torvalds av |= FILE__READ; 16661da177e4SLinus Torvalds if (file->f_mode & FMODE_WRITE) { 16671da177e4SLinus Torvalds if (file->f_flags & O_APPEND) 16681da177e4SLinus Torvalds av |= FILE__APPEND; 16691da177e4SLinus Torvalds else 16701da177e4SLinus Torvalds av |= FILE__WRITE; 16711da177e4SLinus Torvalds } 16720794c66dSStephen Smalley if (!av) { 16730794c66dSStephen Smalley /* 16740794c66dSStephen Smalley * Special file opened with flags 3 for ioctl-only use. 16750794c66dSStephen Smalley */ 16760794c66dSStephen Smalley av = FILE__IOCTL; 16770794c66dSStephen Smalley } 16781da177e4SLinus Torvalds 16791da177e4SLinus Torvalds return av; 16801da177e4SLinus Torvalds } 16811da177e4SLinus Torvalds 16821da177e4SLinus Torvalds /* Hook functions begin here. */ 16831da177e4SLinus Torvalds 16841da177e4SLinus Torvalds static int selinux_ptrace(struct task_struct *parent, struct task_struct *child) 16851da177e4SLinus Torvalds { 16861da177e4SLinus Torvalds int rc; 16871da177e4SLinus Torvalds 16881da177e4SLinus Torvalds rc = secondary_ops->ptrace(parent, child); 16891da177e4SLinus Torvalds if (rc) 16901da177e4SLinus Torvalds return rc; 16911da177e4SLinus Torvalds 16920356357cSRoland McGrath return task_has_perm(parent, child, PROCESS__PTRACE); 16931da177e4SLinus Torvalds } 16941da177e4SLinus Torvalds 16951da177e4SLinus Torvalds static int selinux_capget(struct task_struct *target, kernel_cap_t *effective, 16961da177e4SLinus Torvalds kernel_cap_t *inheritable, kernel_cap_t *permitted) 16971da177e4SLinus Torvalds { 16981da177e4SLinus Torvalds int error; 16991da177e4SLinus Torvalds 17001da177e4SLinus Torvalds error = task_has_perm(current, target, PROCESS__GETCAP); 17011da177e4SLinus Torvalds if (error) 17021da177e4SLinus Torvalds return error; 17031da177e4SLinus Torvalds 17041da177e4SLinus Torvalds return secondary_ops->capget(target, effective, inheritable, permitted); 17051da177e4SLinus Torvalds } 17061da177e4SLinus Torvalds 17071da177e4SLinus Torvalds static int selinux_capset_check(struct task_struct *target, kernel_cap_t *effective, 17081da177e4SLinus Torvalds kernel_cap_t *inheritable, kernel_cap_t *permitted) 17091da177e4SLinus Torvalds { 17101da177e4SLinus Torvalds int error; 17111da177e4SLinus Torvalds 17121da177e4SLinus Torvalds error = secondary_ops->capset_check(target, effective, inheritable, permitted); 17131da177e4SLinus Torvalds if (error) 17141da177e4SLinus Torvalds return error; 17151da177e4SLinus Torvalds 17161da177e4SLinus Torvalds return task_has_perm(current, target, PROCESS__SETCAP); 17171da177e4SLinus Torvalds } 17181da177e4SLinus Torvalds 17191da177e4SLinus Torvalds static void selinux_capset_set(struct task_struct *target, kernel_cap_t *effective, 17201da177e4SLinus Torvalds kernel_cap_t *inheritable, kernel_cap_t *permitted) 17211da177e4SLinus Torvalds { 17221da177e4SLinus Torvalds secondary_ops->capset_set(target, effective, inheritable, permitted); 17231da177e4SLinus Torvalds } 17241da177e4SLinus Torvalds 17251da177e4SLinus Torvalds static int selinux_capable(struct task_struct *tsk, int cap) 17261da177e4SLinus Torvalds { 17271da177e4SLinus Torvalds int rc; 17281da177e4SLinus Torvalds 17291da177e4SLinus Torvalds rc = secondary_ops->capable(tsk, cap); 17301da177e4SLinus Torvalds if (rc) 17311da177e4SLinus Torvalds return rc; 17321da177e4SLinus Torvalds 17331da177e4SLinus Torvalds return task_has_capability(tsk, cap); 17341da177e4SLinus Torvalds } 17351da177e4SLinus Torvalds 17363fbfa981SEric W. Biederman static int selinux_sysctl_get_sid(ctl_table *table, u16 tclass, u32 *sid) 17373fbfa981SEric W. Biederman { 17383fbfa981SEric W. Biederman int buflen, rc; 17393fbfa981SEric W. Biederman char *buffer, *path, *end; 17403fbfa981SEric W. Biederman 17413fbfa981SEric W. Biederman rc = -ENOMEM; 17423fbfa981SEric W. Biederman buffer = (char *)__get_free_page(GFP_KERNEL); 17433fbfa981SEric W. Biederman if (!buffer) 17443fbfa981SEric W. Biederman goto out; 17453fbfa981SEric W. Biederman 17463fbfa981SEric W. Biederman buflen = PAGE_SIZE; 17473fbfa981SEric W. Biederman end = buffer+buflen; 17483fbfa981SEric W. Biederman *--end = '\0'; 17493fbfa981SEric W. Biederman buflen--; 17503fbfa981SEric W. Biederman path = end-1; 17513fbfa981SEric W. Biederman *path = '/'; 17523fbfa981SEric W. Biederman while (table) { 17533fbfa981SEric W. Biederman const char *name = table->procname; 17543fbfa981SEric W. Biederman size_t namelen = strlen(name); 17553fbfa981SEric W. Biederman buflen -= namelen + 1; 17563fbfa981SEric W. Biederman if (buflen < 0) 17573fbfa981SEric W. Biederman goto out_free; 17583fbfa981SEric W. Biederman end -= namelen; 17593fbfa981SEric W. Biederman memcpy(end, name, namelen); 17603fbfa981SEric W. Biederman *--end = '/'; 17613fbfa981SEric W. Biederman path = end; 17623fbfa981SEric W. Biederman table = table->parent; 17633fbfa981SEric W. Biederman } 1764b599fdfdSEric W. Biederman buflen -= 4; 1765b599fdfdSEric W. Biederman if (buflen < 0) 1766b599fdfdSEric W. Biederman goto out_free; 1767b599fdfdSEric W. Biederman end -= 4; 1768b599fdfdSEric W. Biederman memcpy(end, "/sys", 4); 1769b599fdfdSEric W. Biederman path = end; 17703fbfa981SEric W. Biederman rc = security_genfs_sid("proc", path, tclass, sid); 17713fbfa981SEric W. Biederman out_free: 17723fbfa981SEric W. Biederman free_page((unsigned long)buffer); 17733fbfa981SEric W. Biederman out: 17743fbfa981SEric W. Biederman return rc; 17753fbfa981SEric W. Biederman } 17763fbfa981SEric W. Biederman 17771da177e4SLinus Torvalds static int selinux_sysctl(ctl_table *table, int op) 17781da177e4SLinus Torvalds { 17791da177e4SLinus Torvalds int error = 0; 17801da177e4SLinus Torvalds u32 av; 17811da177e4SLinus Torvalds struct task_security_struct *tsec; 17821da177e4SLinus Torvalds u32 tsid; 17831da177e4SLinus Torvalds int rc; 17841da177e4SLinus Torvalds 17851da177e4SLinus Torvalds rc = secondary_ops->sysctl(table, op); 17861da177e4SLinus Torvalds if (rc) 17871da177e4SLinus Torvalds return rc; 17881da177e4SLinus Torvalds 17891da177e4SLinus Torvalds tsec = current->security; 17901da177e4SLinus Torvalds 17913fbfa981SEric W. Biederman rc = selinux_sysctl_get_sid(table, (op == 0001) ? 17921da177e4SLinus Torvalds SECCLASS_DIR : SECCLASS_FILE, &tsid); 17931da177e4SLinus Torvalds if (rc) { 17941da177e4SLinus Torvalds /* Default to the well-defined sysctl SID. */ 17951da177e4SLinus Torvalds tsid = SECINITSID_SYSCTL; 17961da177e4SLinus Torvalds } 17971da177e4SLinus Torvalds 17981da177e4SLinus Torvalds /* The op values are "defined" in sysctl.c, thereby creating 17991da177e4SLinus Torvalds * a bad coupling between this module and sysctl.c */ 18001da177e4SLinus Torvalds if (op == 001) { 18011da177e4SLinus Torvalds error = avc_has_perm(tsec->sid, tsid, 18021da177e4SLinus Torvalds SECCLASS_DIR, DIR__SEARCH, NULL); 18031da177e4SLinus Torvalds } else { 18041da177e4SLinus Torvalds av = 0; 18051da177e4SLinus Torvalds if (op & 004) 18061da177e4SLinus Torvalds av |= FILE__READ; 18071da177e4SLinus Torvalds if (op & 002) 18081da177e4SLinus Torvalds av |= FILE__WRITE; 18091da177e4SLinus Torvalds if (av) 18101da177e4SLinus Torvalds error = avc_has_perm(tsec->sid, tsid, 18111da177e4SLinus Torvalds SECCLASS_FILE, av, NULL); 18121da177e4SLinus Torvalds } 18131da177e4SLinus Torvalds 18141da177e4SLinus Torvalds return error; 18151da177e4SLinus Torvalds } 18161da177e4SLinus Torvalds 18171da177e4SLinus Torvalds static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb) 18181da177e4SLinus Torvalds { 18191da177e4SLinus Torvalds int rc = 0; 18201da177e4SLinus Torvalds 18211da177e4SLinus Torvalds if (!sb) 18221da177e4SLinus Torvalds return 0; 18231da177e4SLinus Torvalds 18241da177e4SLinus Torvalds switch (cmds) { 18251da177e4SLinus Torvalds case Q_SYNC: 18261da177e4SLinus Torvalds case Q_QUOTAON: 18271da177e4SLinus Torvalds case Q_QUOTAOFF: 18281da177e4SLinus Torvalds case Q_SETINFO: 18291da177e4SLinus Torvalds case Q_SETQUOTA: 1830828dfe1dSEric Paris rc = superblock_has_perm(current, sb, FILESYSTEM__QUOTAMOD, 1831828dfe1dSEric Paris NULL); 18321da177e4SLinus Torvalds break; 18331da177e4SLinus Torvalds case Q_GETFMT: 18341da177e4SLinus Torvalds case Q_GETINFO: 18351da177e4SLinus Torvalds case Q_GETQUOTA: 1836828dfe1dSEric Paris rc = superblock_has_perm(current, sb, FILESYSTEM__QUOTAGET, 1837828dfe1dSEric Paris NULL); 18381da177e4SLinus Torvalds break; 18391da177e4SLinus Torvalds default: 18401da177e4SLinus Torvalds rc = 0; /* let the kernel handle invalid cmds */ 18411da177e4SLinus Torvalds break; 18421da177e4SLinus Torvalds } 18431da177e4SLinus Torvalds return rc; 18441da177e4SLinus Torvalds } 18451da177e4SLinus Torvalds 18461da177e4SLinus Torvalds static int selinux_quota_on(struct dentry *dentry) 18471da177e4SLinus Torvalds { 18481da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__QUOTAON); 18491da177e4SLinus Torvalds } 18501da177e4SLinus Torvalds 18511da177e4SLinus Torvalds static int selinux_syslog(int type) 18521da177e4SLinus Torvalds { 18531da177e4SLinus Torvalds int rc; 18541da177e4SLinus Torvalds 18551da177e4SLinus Torvalds rc = secondary_ops->syslog(type); 18561da177e4SLinus Torvalds if (rc) 18571da177e4SLinus Torvalds return rc; 18581da177e4SLinus Torvalds 18591da177e4SLinus Torvalds switch (type) { 18601da177e4SLinus Torvalds case 3: /* Read last kernel messages */ 18611da177e4SLinus Torvalds case 10: /* Return size of the log buffer */ 18621da177e4SLinus Torvalds rc = task_has_system(current, SYSTEM__SYSLOG_READ); 18631da177e4SLinus Torvalds break; 18641da177e4SLinus Torvalds case 6: /* Disable logging to console */ 18651da177e4SLinus Torvalds case 7: /* Enable logging to console */ 18661da177e4SLinus Torvalds case 8: /* Set level of messages printed to console */ 18671da177e4SLinus Torvalds rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE); 18681da177e4SLinus Torvalds break; 18691da177e4SLinus Torvalds case 0: /* Close log */ 18701da177e4SLinus Torvalds case 1: /* Open log */ 18711da177e4SLinus Torvalds case 2: /* Read from log */ 18721da177e4SLinus Torvalds case 4: /* Read/clear last kernel messages */ 18731da177e4SLinus Torvalds case 5: /* Clear ring buffer */ 18741da177e4SLinus Torvalds default: 18751da177e4SLinus Torvalds rc = task_has_system(current, SYSTEM__SYSLOG_MOD); 18761da177e4SLinus Torvalds break; 18771da177e4SLinus Torvalds } 18781da177e4SLinus Torvalds return rc; 18791da177e4SLinus Torvalds } 18801da177e4SLinus Torvalds 18811da177e4SLinus Torvalds /* 18821da177e4SLinus Torvalds * Check that a process has enough memory to allocate a new virtual 18831da177e4SLinus Torvalds * mapping. 0 means there is enough memory for the allocation to 18841da177e4SLinus Torvalds * succeed and -ENOMEM implies there is not. 18851da177e4SLinus Torvalds * 18861da177e4SLinus Torvalds * Note that secondary_ops->capable and task_has_perm_noaudit return 0 18871da177e4SLinus Torvalds * if the capability is granted, but __vm_enough_memory requires 1 if 18881da177e4SLinus Torvalds * the capability is granted. 18891da177e4SLinus Torvalds * 18901da177e4SLinus Torvalds * Do not audit the selinux permission check, as this is applied to all 18911da177e4SLinus Torvalds * processes that allocate mappings. 18921da177e4SLinus Torvalds */ 189334b4e4aaSAlan Cox static int selinux_vm_enough_memory(struct mm_struct *mm, long pages) 18941da177e4SLinus Torvalds { 18951da177e4SLinus Torvalds int rc, cap_sys_admin = 0; 18961da177e4SLinus Torvalds struct task_security_struct *tsec = current->security; 18971da177e4SLinus Torvalds 18981da177e4SLinus Torvalds rc = secondary_ops->capable(current, CAP_SYS_ADMIN); 18991da177e4SLinus Torvalds if (rc == 0) 19001da177e4SLinus Torvalds rc = avc_has_perm_noaudit(tsec->sid, tsec->sid, 19011da177e4SLinus Torvalds SECCLASS_CAPABILITY, 19021da177e4SLinus Torvalds CAP_TO_MASK(CAP_SYS_ADMIN), 19032c3c05dbSStephen Smalley 0, 19041da177e4SLinus Torvalds NULL); 19051da177e4SLinus Torvalds 19061da177e4SLinus Torvalds if (rc == 0) 19071da177e4SLinus Torvalds cap_sys_admin = 1; 19081da177e4SLinus Torvalds 190934b4e4aaSAlan Cox return __vm_enough_memory(mm, pages, cap_sys_admin); 19101da177e4SLinus Torvalds } 19111da177e4SLinus Torvalds 19120356357cSRoland McGrath /** 19130356357cSRoland McGrath * task_tracer_task - return the task that is tracing the given task 19140356357cSRoland McGrath * @task: task to consider 19150356357cSRoland McGrath * 19160356357cSRoland McGrath * Returns NULL if noone is tracing @task, or the &struct task_struct 19170356357cSRoland McGrath * pointer to its tracer. 19180356357cSRoland McGrath * 19190356357cSRoland McGrath * Must be called under rcu_read_lock(). 19200356357cSRoland McGrath */ 19210356357cSRoland McGrath static struct task_struct *task_tracer_task(struct task_struct *task) 19220356357cSRoland McGrath { 19230356357cSRoland McGrath if (task->ptrace & PT_PTRACED) 19240356357cSRoland McGrath return rcu_dereference(task->parent); 19250356357cSRoland McGrath return NULL; 19260356357cSRoland McGrath } 19270356357cSRoland McGrath 19281da177e4SLinus Torvalds /* binprm security operations */ 19291da177e4SLinus Torvalds 19301da177e4SLinus Torvalds static int selinux_bprm_alloc_security(struct linux_binprm *bprm) 19311da177e4SLinus Torvalds { 19321da177e4SLinus Torvalds struct bprm_security_struct *bsec; 19331da177e4SLinus Torvalds 193489d155efSJames Morris bsec = kzalloc(sizeof(struct bprm_security_struct), GFP_KERNEL); 19351da177e4SLinus Torvalds if (!bsec) 19361da177e4SLinus Torvalds return -ENOMEM; 19371da177e4SLinus Torvalds 19381da177e4SLinus Torvalds bsec->sid = SECINITSID_UNLABELED; 19391da177e4SLinus Torvalds bsec->set = 0; 19401da177e4SLinus Torvalds 19411da177e4SLinus Torvalds bprm->security = bsec; 19421da177e4SLinus Torvalds return 0; 19431da177e4SLinus Torvalds } 19441da177e4SLinus Torvalds 19451da177e4SLinus Torvalds static int selinux_bprm_set_security(struct linux_binprm *bprm) 19461da177e4SLinus Torvalds { 19471da177e4SLinus Torvalds struct task_security_struct *tsec; 19483d5ff529SJosef Sipek struct inode *inode = bprm->file->f_path.dentry->d_inode; 19491da177e4SLinus Torvalds struct inode_security_struct *isec; 19501da177e4SLinus Torvalds struct bprm_security_struct *bsec; 19511da177e4SLinus Torvalds u32 newsid; 19521da177e4SLinus Torvalds struct avc_audit_data ad; 19531da177e4SLinus Torvalds int rc; 19541da177e4SLinus Torvalds 19551da177e4SLinus Torvalds rc = secondary_ops->bprm_set_security(bprm); 19561da177e4SLinus Torvalds if (rc) 19571da177e4SLinus Torvalds return rc; 19581da177e4SLinus Torvalds 19591da177e4SLinus Torvalds bsec = bprm->security; 19601da177e4SLinus Torvalds 19611da177e4SLinus Torvalds if (bsec->set) 19621da177e4SLinus Torvalds return 0; 19631da177e4SLinus Torvalds 19641da177e4SLinus Torvalds tsec = current->security; 19651da177e4SLinus Torvalds isec = inode->i_security; 19661da177e4SLinus Torvalds 19671da177e4SLinus Torvalds /* Default to the current task SID. */ 19681da177e4SLinus Torvalds bsec->sid = tsec->sid; 19691da177e4SLinus Torvalds 197028eba5bfSMichael LeMay /* Reset fs, key, and sock SIDs on execve. */ 19711da177e4SLinus Torvalds tsec->create_sid = 0; 197228eba5bfSMichael LeMay tsec->keycreate_sid = 0; 197342c3e03eSEric Paris tsec->sockcreate_sid = 0; 19741da177e4SLinus Torvalds 19751da177e4SLinus Torvalds if (tsec->exec_sid) { 19761da177e4SLinus Torvalds newsid = tsec->exec_sid; 19771da177e4SLinus Torvalds /* Reset exec SID on execve. */ 19781da177e4SLinus Torvalds tsec->exec_sid = 0; 19791da177e4SLinus Torvalds } else { 19801da177e4SLinus Torvalds /* Check for a default transition on this program. */ 19811da177e4SLinus Torvalds rc = security_transition_sid(tsec->sid, isec->sid, 19821da177e4SLinus Torvalds SECCLASS_PROCESS, &newsid); 19831da177e4SLinus Torvalds if (rc) 19841da177e4SLinus Torvalds return rc; 19851da177e4SLinus Torvalds } 19861da177e4SLinus Torvalds 19871da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 198844707fdfSJan Blunck ad.u.fs.path = bprm->file->f_path; 19891da177e4SLinus Torvalds 19903d5ff529SJosef Sipek if (bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) 19911da177e4SLinus Torvalds newsid = tsec->sid; 19921da177e4SLinus Torvalds 19931da177e4SLinus Torvalds if (tsec->sid == newsid) { 19941da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, 19951da177e4SLinus Torvalds SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad); 19961da177e4SLinus Torvalds if (rc) 19971da177e4SLinus Torvalds return rc; 19981da177e4SLinus Torvalds } else { 19991da177e4SLinus Torvalds /* Check permissions for the transition. */ 20001da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, newsid, 20011da177e4SLinus Torvalds SECCLASS_PROCESS, PROCESS__TRANSITION, &ad); 20021da177e4SLinus Torvalds if (rc) 20031da177e4SLinus Torvalds return rc; 20041da177e4SLinus Torvalds 20051da177e4SLinus Torvalds rc = avc_has_perm(newsid, isec->sid, 20061da177e4SLinus Torvalds SECCLASS_FILE, FILE__ENTRYPOINT, &ad); 20071da177e4SLinus Torvalds if (rc) 20081da177e4SLinus Torvalds return rc; 20091da177e4SLinus Torvalds 20101da177e4SLinus Torvalds /* Clear any possibly unsafe personality bits on exec: */ 20111da177e4SLinus Torvalds current->personality &= ~PER_CLEAR_ON_SETID; 20121da177e4SLinus Torvalds 20131da177e4SLinus Torvalds /* Set the security field to the new SID. */ 20141da177e4SLinus Torvalds bsec->sid = newsid; 20151da177e4SLinus Torvalds } 20161da177e4SLinus Torvalds 20171da177e4SLinus Torvalds bsec->set = 1; 20181da177e4SLinus Torvalds return 0; 20191da177e4SLinus Torvalds } 20201da177e4SLinus Torvalds 20211da177e4SLinus Torvalds static int selinux_bprm_check_security(struct linux_binprm *bprm) 20221da177e4SLinus Torvalds { 20231da177e4SLinus Torvalds return secondary_ops->bprm_check_security(bprm); 20241da177e4SLinus Torvalds } 20251da177e4SLinus Torvalds 20261da177e4SLinus Torvalds 20271da177e4SLinus Torvalds static int selinux_bprm_secureexec(struct linux_binprm *bprm) 20281da177e4SLinus Torvalds { 20291da177e4SLinus Torvalds struct task_security_struct *tsec = current->security; 20301da177e4SLinus Torvalds int atsecure = 0; 20311da177e4SLinus Torvalds 20321da177e4SLinus Torvalds if (tsec->osid != tsec->sid) { 20331da177e4SLinus Torvalds /* Enable secure mode for SIDs transitions unless 20341da177e4SLinus Torvalds the noatsecure permission is granted between 20351da177e4SLinus Torvalds the two SIDs, i.e. ahp returns 0. */ 20361da177e4SLinus Torvalds atsecure = avc_has_perm(tsec->osid, tsec->sid, 20371da177e4SLinus Torvalds SECCLASS_PROCESS, 20381da177e4SLinus Torvalds PROCESS__NOATSECURE, NULL); 20391da177e4SLinus Torvalds } 20401da177e4SLinus Torvalds 20411da177e4SLinus Torvalds return (atsecure || secondary_ops->bprm_secureexec(bprm)); 20421da177e4SLinus Torvalds } 20431da177e4SLinus Torvalds 20441da177e4SLinus Torvalds static void selinux_bprm_free_security(struct linux_binprm *bprm) 20451da177e4SLinus Torvalds { 20469a5f04bfSJesper Juhl kfree(bprm->security); 20471da177e4SLinus Torvalds bprm->security = NULL; 20481da177e4SLinus Torvalds } 20491da177e4SLinus Torvalds 20501da177e4SLinus Torvalds extern struct vfsmount *selinuxfs_mount; 20511da177e4SLinus Torvalds extern struct dentry *selinux_null; 20521da177e4SLinus Torvalds 20531da177e4SLinus Torvalds /* Derived from fs/exec.c:flush_old_files. */ 20541da177e4SLinus Torvalds static inline void flush_unauthorized_files(struct files_struct *files) 20551da177e4SLinus Torvalds { 20561da177e4SLinus Torvalds struct avc_audit_data ad; 20571da177e4SLinus Torvalds struct file *file, *devnull = NULL; 2058b20c8122SStephen Smalley struct tty_struct *tty; 2059badf1662SDipankar Sarma struct fdtable *fdt; 20601da177e4SLinus Torvalds long j = -1; 206124ec839cSPeter Zijlstra int drop_tty = 0; 20621da177e4SLinus Torvalds 2063b20c8122SStephen Smalley mutex_lock(&tty_mutex); 206424ec839cSPeter Zijlstra tty = get_current_tty(); 20651da177e4SLinus Torvalds if (tty) { 20661da177e4SLinus Torvalds file_list_lock(); 20672f512016SEric Dumazet file = list_entry(tty->tty_files.next, typeof(*file), f_u.fu_list); 20681da177e4SLinus Torvalds if (file) { 20691da177e4SLinus Torvalds /* Revalidate access to controlling tty. 20701da177e4SLinus Torvalds Use inode_has_perm on the tty inode directly rather 20711da177e4SLinus Torvalds than using file_has_perm, as this particular open 20721da177e4SLinus Torvalds file may belong to another process and we are only 20731da177e4SLinus Torvalds interested in the inode-based check here. */ 20743d5ff529SJosef Sipek struct inode *inode = file->f_path.dentry->d_inode; 20751da177e4SLinus Torvalds if (inode_has_perm(current, inode, 20761da177e4SLinus Torvalds FILE__READ | FILE__WRITE, NULL)) { 207724ec839cSPeter Zijlstra drop_tty = 1; 20781da177e4SLinus Torvalds } 20791da177e4SLinus Torvalds } 20801da177e4SLinus Torvalds file_list_unlock(); 20811da177e4SLinus Torvalds } 2082b20c8122SStephen Smalley mutex_unlock(&tty_mutex); 208398a27ba4SEric W. Biederman /* Reset controlling tty. */ 208498a27ba4SEric W. Biederman if (drop_tty) 208598a27ba4SEric W. Biederman no_tty(); 20861da177e4SLinus Torvalds 20871da177e4SLinus Torvalds /* Revalidate access to inherited open files. */ 20881da177e4SLinus Torvalds 20891da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 20901da177e4SLinus Torvalds 20911da177e4SLinus Torvalds spin_lock(&files->file_lock); 20921da177e4SLinus Torvalds for (;;) { 20931da177e4SLinus Torvalds unsigned long set, i; 20941da177e4SLinus Torvalds int fd; 20951da177e4SLinus Torvalds 20961da177e4SLinus Torvalds j++; 20971da177e4SLinus Torvalds i = j * __NFDBITS; 2098badf1662SDipankar Sarma fdt = files_fdtable(files); 2099bbea9f69SVadim Lobanov if (i >= fdt->max_fds) 21001da177e4SLinus Torvalds break; 2101badf1662SDipankar Sarma set = fdt->open_fds->fds_bits[j]; 21021da177e4SLinus Torvalds if (!set) 21031da177e4SLinus Torvalds continue; 21041da177e4SLinus Torvalds spin_unlock(&files->file_lock); 21051da177e4SLinus Torvalds for ( ; set ; i++, set >>= 1) { 21061da177e4SLinus Torvalds if (set & 1) { 21071da177e4SLinus Torvalds file = fget(i); 21081da177e4SLinus Torvalds if (!file) 21091da177e4SLinus Torvalds continue; 21101da177e4SLinus Torvalds if (file_has_perm(current, 21111da177e4SLinus Torvalds file, 21121da177e4SLinus Torvalds file_to_av(file))) { 21131da177e4SLinus Torvalds sys_close(i); 21141da177e4SLinus Torvalds fd = get_unused_fd(); 21151da177e4SLinus Torvalds if (fd != i) { 21161da177e4SLinus Torvalds if (fd >= 0) 21171da177e4SLinus Torvalds put_unused_fd(fd); 21181da177e4SLinus Torvalds fput(file); 21191da177e4SLinus Torvalds continue; 21201da177e4SLinus Torvalds } 21211da177e4SLinus Torvalds if (devnull) { 2122095975daSNick Piggin get_file(devnull); 21231da177e4SLinus Torvalds } else { 21241da177e4SLinus Torvalds devnull = dentry_open(dget(selinux_null), mntget(selinuxfs_mount), O_RDWR); 2125fc5d81e6SAkinobu Mita if (IS_ERR(devnull)) { 2126fc5d81e6SAkinobu Mita devnull = NULL; 21271da177e4SLinus Torvalds put_unused_fd(fd); 21281da177e4SLinus Torvalds fput(file); 21291da177e4SLinus Torvalds continue; 21301da177e4SLinus Torvalds } 21311da177e4SLinus Torvalds } 21321da177e4SLinus Torvalds fd_install(fd, devnull); 21331da177e4SLinus Torvalds } 21341da177e4SLinus Torvalds fput(file); 21351da177e4SLinus Torvalds } 21361da177e4SLinus Torvalds } 21371da177e4SLinus Torvalds spin_lock(&files->file_lock); 21381da177e4SLinus Torvalds 21391da177e4SLinus Torvalds } 21401da177e4SLinus Torvalds spin_unlock(&files->file_lock); 21411da177e4SLinus Torvalds } 21421da177e4SLinus Torvalds 21431da177e4SLinus Torvalds static void selinux_bprm_apply_creds(struct linux_binprm *bprm, int unsafe) 21441da177e4SLinus Torvalds { 21451da177e4SLinus Torvalds struct task_security_struct *tsec; 21461da177e4SLinus Torvalds struct bprm_security_struct *bsec; 21471da177e4SLinus Torvalds u32 sid; 21481da177e4SLinus Torvalds int rc; 21491da177e4SLinus Torvalds 21501da177e4SLinus Torvalds secondary_ops->bprm_apply_creds(bprm, unsafe); 21511da177e4SLinus Torvalds 21521da177e4SLinus Torvalds tsec = current->security; 21531da177e4SLinus Torvalds 21541da177e4SLinus Torvalds bsec = bprm->security; 21551da177e4SLinus Torvalds sid = bsec->sid; 21561da177e4SLinus Torvalds 21571da177e4SLinus Torvalds tsec->osid = tsec->sid; 21581da177e4SLinus Torvalds bsec->unsafe = 0; 21591da177e4SLinus Torvalds if (tsec->sid != sid) { 21601da177e4SLinus Torvalds /* Check for shared state. If not ok, leave SID 21611da177e4SLinus Torvalds unchanged and kill. */ 21621da177e4SLinus Torvalds if (unsafe & LSM_UNSAFE_SHARE) { 21631da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS, 21641da177e4SLinus Torvalds PROCESS__SHARE, NULL); 21651da177e4SLinus Torvalds if (rc) { 21661da177e4SLinus Torvalds bsec->unsafe = 1; 21671da177e4SLinus Torvalds return; 21681da177e4SLinus Torvalds } 21691da177e4SLinus Torvalds } 21701da177e4SLinus Torvalds 21711da177e4SLinus Torvalds /* Check for ptracing, and update the task SID if ok. 21721da177e4SLinus Torvalds Otherwise, leave SID unchanged and kill. */ 21731da177e4SLinus Torvalds if (unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) { 21740356357cSRoland McGrath struct task_struct *tracer; 21750356357cSRoland McGrath struct task_security_struct *sec; 21760356357cSRoland McGrath u32 ptsid = 0; 21770356357cSRoland McGrath 21780356357cSRoland McGrath rcu_read_lock(); 21790356357cSRoland McGrath tracer = task_tracer_task(current); 21800356357cSRoland McGrath if (likely(tracer != NULL)) { 21810356357cSRoland McGrath sec = tracer->security; 21820356357cSRoland McGrath ptsid = sec->sid; 21830356357cSRoland McGrath } 21840356357cSRoland McGrath rcu_read_unlock(); 21850356357cSRoland McGrath 21860356357cSRoland McGrath if (ptsid != 0) { 21870356357cSRoland McGrath rc = avc_has_perm(ptsid, sid, SECCLASS_PROCESS, 21880356357cSRoland McGrath PROCESS__PTRACE, NULL); 21891da177e4SLinus Torvalds if (rc) { 21901da177e4SLinus Torvalds bsec->unsafe = 1; 21911da177e4SLinus Torvalds return; 21921da177e4SLinus Torvalds } 21931da177e4SLinus Torvalds } 21940356357cSRoland McGrath } 21951da177e4SLinus Torvalds tsec->sid = sid; 21961da177e4SLinus Torvalds } 21971da177e4SLinus Torvalds } 21981da177e4SLinus Torvalds 21991da177e4SLinus Torvalds /* 22001da177e4SLinus Torvalds * called after apply_creds without the task lock held 22011da177e4SLinus Torvalds */ 22021da177e4SLinus Torvalds static void selinux_bprm_post_apply_creds(struct linux_binprm *bprm) 22031da177e4SLinus Torvalds { 22041da177e4SLinus Torvalds struct task_security_struct *tsec; 22051da177e4SLinus Torvalds struct rlimit *rlim, *initrlim; 22061da177e4SLinus Torvalds struct itimerval itimer; 22071da177e4SLinus Torvalds struct bprm_security_struct *bsec; 22081da177e4SLinus Torvalds int rc, i; 22091da177e4SLinus Torvalds 22101da177e4SLinus Torvalds tsec = current->security; 22111da177e4SLinus Torvalds bsec = bprm->security; 22121da177e4SLinus Torvalds 22131da177e4SLinus Torvalds if (bsec->unsafe) { 22141da177e4SLinus Torvalds force_sig_specific(SIGKILL, current); 22151da177e4SLinus Torvalds return; 22161da177e4SLinus Torvalds } 22171da177e4SLinus Torvalds if (tsec->osid == tsec->sid) 22181da177e4SLinus Torvalds return; 22191da177e4SLinus Torvalds 22201da177e4SLinus Torvalds /* Close files for which the new task SID is not authorized. */ 22211da177e4SLinus Torvalds flush_unauthorized_files(current->files); 22221da177e4SLinus Torvalds 22231da177e4SLinus Torvalds /* Check whether the new SID can inherit signal state 22241da177e4SLinus Torvalds from the old SID. If not, clear itimers to avoid 22251da177e4SLinus Torvalds subsequent signal generation and flush and unblock 22261da177e4SLinus Torvalds signals. This must occur _after_ the task SID has 22271da177e4SLinus Torvalds been updated so that any kill done after the flush 22281da177e4SLinus Torvalds will be checked against the new SID. */ 22291da177e4SLinus Torvalds rc = avc_has_perm(tsec->osid, tsec->sid, SECCLASS_PROCESS, 22301da177e4SLinus Torvalds PROCESS__SIGINH, NULL); 22311da177e4SLinus Torvalds if (rc) { 22321da177e4SLinus Torvalds memset(&itimer, 0, sizeof itimer); 22331da177e4SLinus Torvalds for (i = 0; i < 3; i++) 22341da177e4SLinus Torvalds do_setitimer(i, &itimer, NULL); 22351da177e4SLinus Torvalds flush_signals(current); 22361da177e4SLinus Torvalds spin_lock_irq(¤t->sighand->siglock); 22371da177e4SLinus Torvalds flush_signal_handlers(current, 1); 22381da177e4SLinus Torvalds sigemptyset(¤t->blocked); 22391da177e4SLinus Torvalds recalc_sigpending(); 22401da177e4SLinus Torvalds spin_unlock_irq(¤t->sighand->siglock); 22411da177e4SLinus Torvalds } 22421da177e4SLinus Torvalds 22434ac212adSStephen Smalley /* Always clear parent death signal on SID transitions. */ 22444ac212adSStephen Smalley current->pdeath_signal = 0; 22454ac212adSStephen Smalley 22461da177e4SLinus Torvalds /* Check whether the new SID can inherit resource limits 22471da177e4SLinus Torvalds from the old SID. If not, reset all soft limits to 22481da177e4SLinus Torvalds the lower of the current task's hard limit and the init 22491da177e4SLinus Torvalds task's soft limit. Note that the setting of hard limits 22501da177e4SLinus Torvalds (even to lower them) can be controlled by the setrlimit 22511da177e4SLinus Torvalds check. The inclusion of the init task's soft limit into 22521da177e4SLinus Torvalds the computation is to avoid resetting soft limits higher 22531da177e4SLinus Torvalds than the default soft limit for cases where the default 22541da177e4SLinus Torvalds is lower than the hard limit, e.g. RLIMIT_CORE or 22551da177e4SLinus Torvalds RLIMIT_STACK.*/ 22561da177e4SLinus Torvalds rc = avc_has_perm(tsec->osid, tsec->sid, SECCLASS_PROCESS, 22571da177e4SLinus Torvalds PROCESS__RLIMITINH, NULL); 22581da177e4SLinus Torvalds if (rc) { 22591da177e4SLinus Torvalds for (i = 0; i < RLIM_NLIMITS; i++) { 22601da177e4SLinus Torvalds rlim = current->signal->rlim + i; 22611da177e4SLinus Torvalds initrlim = init_task.signal->rlim+i; 22621da177e4SLinus Torvalds rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur); 22631da177e4SLinus Torvalds } 22641da177e4SLinus Torvalds if (current->signal->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) { 22651da177e4SLinus Torvalds /* 22661da177e4SLinus Torvalds * This will cause RLIMIT_CPU calculations 22671da177e4SLinus Torvalds * to be refigured. 22681da177e4SLinus Torvalds */ 22691da177e4SLinus Torvalds current->it_prof_expires = jiffies_to_cputime(1); 22701da177e4SLinus Torvalds } 22711da177e4SLinus Torvalds } 22721da177e4SLinus Torvalds 22731da177e4SLinus Torvalds /* Wake up the parent if it is waiting so that it can 22741da177e4SLinus Torvalds recheck wait permission to the new task SID. */ 22751da177e4SLinus Torvalds wake_up_interruptible(¤t->parent->signal->wait_chldexit); 22761da177e4SLinus Torvalds } 22771da177e4SLinus Torvalds 22781da177e4SLinus Torvalds /* superblock security operations */ 22791da177e4SLinus Torvalds 22801da177e4SLinus Torvalds static int selinux_sb_alloc_security(struct super_block *sb) 22811da177e4SLinus Torvalds { 22821da177e4SLinus Torvalds return superblock_alloc_security(sb); 22831da177e4SLinus Torvalds } 22841da177e4SLinus Torvalds 22851da177e4SLinus Torvalds static void selinux_sb_free_security(struct super_block *sb) 22861da177e4SLinus Torvalds { 22871da177e4SLinus Torvalds superblock_free_security(sb); 22881da177e4SLinus Torvalds } 22891da177e4SLinus Torvalds 22901da177e4SLinus Torvalds static inline int match_prefix(char *prefix, int plen, char *option, int olen) 22911da177e4SLinus Torvalds { 22921da177e4SLinus Torvalds if (plen > olen) 22931da177e4SLinus Torvalds return 0; 22941da177e4SLinus Torvalds 22951da177e4SLinus Torvalds return !memcmp(prefix, option, plen); 22961da177e4SLinus Torvalds } 22971da177e4SLinus Torvalds 22981da177e4SLinus Torvalds static inline int selinux_option(char *option, int len) 22991da177e4SLinus Torvalds { 2300832cbd9aSEric Paris return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) || 2301832cbd9aSEric Paris match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) || 2302832cbd9aSEric Paris match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) || 2303832cbd9aSEric Paris match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len)); 23041da177e4SLinus Torvalds } 23051da177e4SLinus Torvalds 23061da177e4SLinus Torvalds static inline void take_option(char **to, char *from, int *first, int len) 23071da177e4SLinus Torvalds { 23081da177e4SLinus Torvalds if (!*first) { 23091da177e4SLinus Torvalds **to = ','; 23101da177e4SLinus Torvalds *to += 1; 23113528a953SCory Olmo } else 23121da177e4SLinus Torvalds *first = 0; 23131da177e4SLinus Torvalds memcpy(*to, from, len); 23141da177e4SLinus Torvalds *to += len; 23151da177e4SLinus Torvalds } 23161da177e4SLinus Torvalds 23173528a953SCory Olmo static inline void take_selinux_option(char **to, char *from, int *first, 23183528a953SCory Olmo int len) 23193528a953SCory Olmo { 23203528a953SCory Olmo int current_size = 0; 23213528a953SCory Olmo 23223528a953SCory Olmo if (!*first) { 23233528a953SCory Olmo **to = '|'; 23243528a953SCory Olmo *to += 1; 2325828dfe1dSEric Paris } else 23263528a953SCory Olmo *first = 0; 23273528a953SCory Olmo 23283528a953SCory Olmo while (current_size < len) { 23293528a953SCory Olmo if (*from != '"') { 23303528a953SCory Olmo **to = *from; 23313528a953SCory Olmo *to += 1; 23323528a953SCory Olmo } 23333528a953SCory Olmo from += 1; 23343528a953SCory Olmo current_size += 1; 23353528a953SCory Olmo } 23363528a953SCory Olmo } 23373528a953SCory Olmo 2338e0007529SEric Paris static int selinux_sb_copy_data(char *orig, char *copy) 23391da177e4SLinus Torvalds { 23401da177e4SLinus Torvalds int fnosec, fsec, rc = 0; 23411da177e4SLinus Torvalds char *in_save, *in_curr, *in_end; 23421da177e4SLinus Torvalds char *sec_curr, *nosec_save, *nosec; 23433528a953SCory Olmo int open_quote = 0; 23441da177e4SLinus Torvalds 23451da177e4SLinus Torvalds in_curr = orig; 23461da177e4SLinus Torvalds sec_curr = copy; 23471da177e4SLinus Torvalds 23481da177e4SLinus Torvalds nosec = (char *)get_zeroed_page(GFP_KERNEL); 23491da177e4SLinus Torvalds if (!nosec) { 23501da177e4SLinus Torvalds rc = -ENOMEM; 23511da177e4SLinus Torvalds goto out; 23521da177e4SLinus Torvalds } 23531da177e4SLinus Torvalds 23541da177e4SLinus Torvalds nosec_save = nosec; 23551da177e4SLinus Torvalds fnosec = fsec = 1; 23561da177e4SLinus Torvalds in_save = in_end = orig; 23571da177e4SLinus Torvalds 23581da177e4SLinus Torvalds do { 23593528a953SCory Olmo if (*in_end == '"') 23603528a953SCory Olmo open_quote = !open_quote; 23613528a953SCory Olmo if ((*in_end == ',' && open_quote == 0) || 23623528a953SCory Olmo *in_end == '\0') { 23631da177e4SLinus Torvalds int len = in_end - in_curr; 23641da177e4SLinus Torvalds 23651da177e4SLinus Torvalds if (selinux_option(in_curr, len)) 23663528a953SCory Olmo take_selinux_option(&sec_curr, in_curr, &fsec, len); 23671da177e4SLinus Torvalds else 23681da177e4SLinus Torvalds take_option(&nosec, in_curr, &fnosec, len); 23691da177e4SLinus Torvalds 23701da177e4SLinus Torvalds in_curr = in_end + 1; 23711da177e4SLinus Torvalds } 23721da177e4SLinus Torvalds } while (*in_end++); 23731da177e4SLinus Torvalds 23746931dfc9SEric Paris strcpy(in_save, nosec_save); 2375da3caa20SGerald Schaefer free_page((unsigned long)nosec_save); 23761da177e4SLinus Torvalds out: 23771da177e4SLinus Torvalds return rc; 23781da177e4SLinus Torvalds } 23791da177e4SLinus Torvalds 23801da177e4SLinus Torvalds static int selinux_sb_kern_mount(struct super_block *sb, void *data) 23811da177e4SLinus Torvalds { 23821da177e4SLinus Torvalds struct avc_audit_data ad; 23831da177e4SLinus Torvalds int rc; 23841da177e4SLinus Torvalds 23851da177e4SLinus Torvalds rc = superblock_doinit(sb, data); 23861da177e4SLinus Torvalds if (rc) 23871da177e4SLinus Torvalds return rc; 23881da177e4SLinus Torvalds 23891da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 239044707fdfSJan Blunck ad.u.fs.path.dentry = sb->s_root; 23911da177e4SLinus Torvalds return superblock_has_perm(current, sb, FILESYSTEM__MOUNT, &ad); 23921da177e4SLinus Torvalds } 23931da177e4SLinus Torvalds 2394726c3342SDavid Howells static int selinux_sb_statfs(struct dentry *dentry) 23951da177e4SLinus Torvalds { 23961da177e4SLinus Torvalds struct avc_audit_data ad; 23971da177e4SLinus Torvalds 23981da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 239944707fdfSJan Blunck ad.u.fs.path.dentry = dentry->d_sb->s_root; 2400726c3342SDavid Howells return superblock_has_perm(current, dentry->d_sb, FILESYSTEM__GETATTR, &ad); 24011da177e4SLinus Torvalds } 24021da177e4SLinus Torvalds 24031da177e4SLinus Torvalds static int selinux_mount(char *dev_name, 2404b5266eb4SAl Viro struct path *path, 24051da177e4SLinus Torvalds char *type, 24061da177e4SLinus Torvalds unsigned long flags, 24071da177e4SLinus Torvalds void *data) 24081da177e4SLinus Torvalds { 24091da177e4SLinus Torvalds int rc; 24101da177e4SLinus Torvalds 2411b5266eb4SAl Viro rc = secondary_ops->sb_mount(dev_name, path, type, flags, data); 24121da177e4SLinus Torvalds if (rc) 24131da177e4SLinus Torvalds return rc; 24141da177e4SLinus Torvalds 24151da177e4SLinus Torvalds if (flags & MS_REMOUNT) 2416b5266eb4SAl Viro return superblock_has_perm(current, path->mnt->mnt_sb, 24171da177e4SLinus Torvalds FILESYSTEM__REMOUNT, NULL); 24181da177e4SLinus Torvalds else 2419b5266eb4SAl Viro return dentry_has_perm(current, path->mnt, path->dentry, 24201da177e4SLinus Torvalds FILE__MOUNTON); 24211da177e4SLinus Torvalds } 24221da177e4SLinus Torvalds 24231da177e4SLinus Torvalds static int selinux_umount(struct vfsmount *mnt, int flags) 24241da177e4SLinus Torvalds { 24251da177e4SLinus Torvalds int rc; 24261da177e4SLinus Torvalds 24271da177e4SLinus Torvalds rc = secondary_ops->sb_umount(mnt, flags); 24281da177e4SLinus Torvalds if (rc) 24291da177e4SLinus Torvalds return rc; 24301da177e4SLinus Torvalds 24311da177e4SLinus Torvalds return superblock_has_perm(current, mnt->mnt_sb, 24321da177e4SLinus Torvalds FILESYSTEM__UNMOUNT, NULL); 24331da177e4SLinus Torvalds } 24341da177e4SLinus Torvalds 24351da177e4SLinus Torvalds /* inode security operations */ 24361da177e4SLinus Torvalds 24371da177e4SLinus Torvalds static int selinux_inode_alloc_security(struct inode *inode) 24381da177e4SLinus Torvalds { 24391da177e4SLinus Torvalds return inode_alloc_security(inode); 24401da177e4SLinus Torvalds } 24411da177e4SLinus Torvalds 24421da177e4SLinus Torvalds static void selinux_inode_free_security(struct inode *inode) 24431da177e4SLinus Torvalds { 24441da177e4SLinus Torvalds inode_free_security(inode); 24451da177e4SLinus Torvalds } 24461da177e4SLinus Torvalds 24475e41ff9eSStephen Smalley static int selinux_inode_init_security(struct inode *inode, struct inode *dir, 24485e41ff9eSStephen Smalley char **name, void **value, 24495e41ff9eSStephen Smalley size_t *len) 24505e41ff9eSStephen Smalley { 24515e41ff9eSStephen Smalley struct task_security_struct *tsec; 24525e41ff9eSStephen Smalley struct inode_security_struct *dsec; 24535e41ff9eSStephen Smalley struct superblock_security_struct *sbsec; 2454570bc1c2SStephen Smalley u32 newsid, clen; 24555e41ff9eSStephen Smalley int rc; 2456570bc1c2SStephen Smalley char *namep = NULL, *context; 24575e41ff9eSStephen Smalley 24585e41ff9eSStephen Smalley tsec = current->security; 24595e41ff9eSStephen Smalley dsec = dir->i_security; 24605e41ff9eSStephen Smalley sbsec = dir->i_sb->s_security; 24615e41ff9eSStephen Smalley 24625e41ff9eSStephen Smalley if (tsec->create_sid && sbsec->behavior != SECURITY_FS_USE_MNTPOINT) { 24635e41ff9eSStephen Smalley newsid = tsec->create_sid; 24645e41ff9eSStephen Smalley } else { 24655e41ff9eSStephen Smalley rc = security_transition_sid(tsec->sid, dsec->sid, 24665e41ff9eSStephen Smalley inode_mode_to_security_class(inode->i_mode), 24675e41ff9eSStephen Smalley &newsid); 24685e41ff9eSStephen Smalley if (rc) { 24695e41ff9eSStephen Smalley printk(KERN_WARNING "%s: " 24705e41ff9eSStephen Smalley "security_transition_sid failed, rc=%d (dev=%s " 24715e41ff9eSStephen Smalley "ino=%ld)\n", 2472dd6f953aSHarvey Harrison __func__, 24735e41ff9eSStephen Smalley -rc, inode->i_sb->s_id, inode->i_ino); 24745e41ff9eSStephen Smalley return rc; 24755e41ff9eSStephen Smalley } 24765e41ff9eSStephen Smalley } 24775e41ff9eSStephen Smalley 2478296fddf7SEric Paris /* Possibly defer initialization to selinux_complete_init. */ 2479296fddf7SEric Paris if (sbsec->initialized) { 2480296fddf7SEric Paris struct inode_security_struct *isec = inode->i_security; 2481296fddf7SEric Paris isec->sclass = inode_mode_to_security_class(inode->i_mode); 2482296fddf7SEric Paris isec->sid = newsid; 2483296fddf7SEric Paris isec->initialized = 1; 2484296fddf7SEric Paris } 24855e41ff9eSStephen Smalley 24868aad3875SStephen Smalley if (!ss_initialized || sbsec->behavior == SECURITY_FS_USE_MNTPOINT) 248725a74f3bSStephen Smalley return -EOPNOTSUPP; 248825a74f3bSStephen Smalley 2489570bc1c2SStephen Smalley if (name) { 2490a02fe132SJosef Bacik namep = kstrdup(XATTR_SELINUX_SUFFIX, GFP_NOFS); 24915e41ff9eSStephen Smalley if (!namep) 24925e41ff9eSStephen Smalley return -ENOMEM; 24935e41ff9eSStephen Smalley *name = namep; 2494570bc1c2SStephen Smalley } 24955e41ff9eSStephen Smalley 2496570bc1c2SStephen Smalley if (value && len) { 2497570bc1c2SStephen Smalley rc = security_sid_to_context(newsid, &context, &clen); 24985e41ff9eSStephen Smalley if (rc) { 24995e41ff9eSStephen Smalley kfree(namep); 25005e41ff9eSStephen Smalley return rc; 25015e41ff9eSStephen Smalley } 25025e41ff9eSStephen Smalley *value = context; 2503570bc1c2SStephen Smalley *len = clen; 2504570bc1c2SStephen Smalley } 25055e41ff9eSStephen Smalley 25065e41ff9eSStephen Smalley return 0; 25075e41ff9eSStephen Smalley } 25085e41ff9eSStephen Smalley 25091da177e4SLinus Torvalds static int selinux_inode_create(struct inode *dir, struct dentry *dentry, int mask) 25101da177e4SLinus Torvalds { 25111da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_FILE); 25121da177e4SLinus Torvalds } 25131da177e4SLinus Torvalds 25141da177e4SLinus Torvalds static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 25151da177e4SLinus Torvalds { 25161da177e4SLinus Torvalds int rc; 25171da177e4SLinus Torvalds 25181da177e4SLinus Torvalds rc = secondary_ops->inode_link(old_dentry, dir, new_dentry); 25191da177e4SLinus Torvalds if (rc) 25201da177e4SLinus Torvalds return rc; 25211da177e4SLinus Torvalds return may_link(dir, old_dentry, MAY_LINK); 25221da177e4SLinus Torvalds } 25231da177e4SLinus Torvalds 25241da177e4SLinus Torvalds static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry) 25251da177e4SLinus Torvalds { 25261da177e4SLinus Torvalds int rc; 25271da177e4SLinus Torvalds 25281da177e4SLinus Torvalds rc = secondary_ops->inode_unlink(dir, dentry); 25291da177e4SLinus Torvalds if (rc) 25301da177e4SLinus Torvalds return rc; 25311da177e4SLinus Torvalds return may_link(dir, dentry, MAY_UNLINK); 25321da177e4SLinus Torvalds } 25331da177e4SLinus Torvalds 25341da177e4SLinus Torvalds static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name) 25351da177e4SLinus Torvalds { 25361da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_LNK_FILE); 25371da177e4SLinus Torvalds } 25381da177e4SLinus Torvalds 25391da177e4SLinus Torvalds static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, int mask) 25401da177e4SLinus Torvalds { 25411da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_DIR); 25421da177e4SLinus Torvalds } 25431da177e4SLinus Torvalds 25441da177e4SLinus Torvalds static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry) 25451da177e4SLinus Torvalds { 25461da177e4SLinus Torvalds return may_link(dir, dentry, MAY_RMDIR); 25471da177e4SLinus Torvalds } 25481da177e4SLinus Torvalds 25491da177e4SLinus Torvalds static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 25501da177e4SLinus Torvalds { 25511da177e4SLinus Torvalds int rc; 25521da177e4SLinus Torvalds 25531da177e4SLinus Torvalds rc = secondary_ops->inode_mknod(dir, dentry, mode, dev); 25541da177e4SLinus Torvalds if (rc) 25551da177e4SLinus Torvalds return rc; 25561da177e4SLinus Torvalds 25571da177e4SLinus Torvalds return may_create(dir, dentry, inode_mode_to_security_class(mode)); 25581da177e4SLinus Torvalds } 25591da177e4SLinus Torvalds 25601da177e4SLinus Torvalds static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry, 25611da177e4SLinus Torvalds struct inode *new_inode, struct dentry *new_dentry) 25621da177e4SLinus Torvalds { 25631da177e4SLinus Torvalds return may_rename(old_inode, old_dentry, new_inode, new_dentry); 25641da177e4SLinus Torvalds } 25651da177e4SLinus Torvalds 25661da177e4SLinus Torvalds static int selinux_inode_readlink(struct dentry *dentry) 25671da177e4SLinus Torvalds { 25681da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__READ); 25691da177e4SLinus Torvalds } 25701da177e4SLinus Torvalds 25711da177e4SLinus Torvalds static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata) 25721da177e4SLinus Torvalds { 25731da177e4SLinus Torvalds int rc; 25741da177e4SLinus Torvalds 25751da177e4SLinus Torvalds rc = secondary_ops->inode_follow_link(dentry, nameidata); 25761da177e4SLinus Torvalds if (rc) 25771da177e4SLinus Torvalds return rc; 25781da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__READ); 25791da177e4SLinus Torvalds } 25801da177e4SLinus Torvalds 25811da177e4SLinus Torvalds static int selinux_inode_permission(struct inode *inode, int mask, 25821da177e4SLinus Torvalds struct nameidata *nd) 25831da177e4SLinus Torvalds { 25841da177e4SLinus Torvalds int rc; 25851da177e4SLinus Torvalds 25861da177e4SLinus Torvalds rc = secondary_ops->inode_permission(inode, mask, nd); 25871da177e4SLinus Torvalds if (rc) 25881da177e4SLinus Torvalds return rc; 25891da177e4SLinus Torvalds 25901da177e4SLinus Torvalds if (!mask) { 25911da177e4SLinus Torvalds /* No permission to check. Existence test. */ 25921da177e4SLinus Torvalds return 0; 25931da177e4SLinus Torvalds } 25941da177e4SLinus Torvalds 25951da177e4SLinus Torvalds return inode_has_perm(current, inode, 2596b0c636b9SEric Paris open_file_mask_to_av(inode->i_mode, mask), NULL); 25971da177e4SLinus Torvalds } 25981da177e4SLinus Torvalds 25991da177e4SLinus Torvalds static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr) 26001da177e4SLinus Torvalds { 26011da177e4SLinus Torvalds int rc; 26021da177e4SLinus Torvalds 26031da177e4SLinus Torvalds rc = secondary_ops->inode_setattr(dentry, iattr); 26041da177e4SLinus Torvalds if (rc) 26051da177e4SLinus Torvalds return rc; 26061da177e4SLinus Torvalds 26071da177e4SLinus Torvalds if (iattr->ia_valid & ATTR_FORCE) 26081da177e4SLinus Torvalds return 0; 26091da177e4SLinus Torvalds 26101da177e4SLinus Torvalds if (iattr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | 26111da177e4SLinus Torvalds ATTR_ATIME_SET | ATTR_MTIME_SET)) 26121da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__SETATTR); 26131da177e4SLinus Torvalds 26141da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__WRITE); 26151da177e4SLinus Torvalds } 26161da177e4SLinus Torvalds 26171da177e4SLinus Torvalds static int selinux_inode_getattr(struct vfsmount *mnt, struct dentry *dentry) 26181da177e4SLinus Torvalds { 26191da177e4SLinus Torvalds return dentry_has_perm(current, mnt, dentry, FILE__GETATTR); 26201da177e4SLinus Torvalds } 26211da177e4SLinus Torvalds 2622b5376771SSerge E. Hallyn static int selinux_inode_setotherxattr(struct dentry *dentry, char *name) 2623b5376771SSerge E. Hallyn { 2624b5376771SSerge E. Hallyn if (!strncmp(name, XATTR_SECURITY_PREFIX, 2625b5376771SSerge E. Hallyn sizeof XATTR_SECURITY_PREFIX - 1)) { 2626b5376771SSerge E. Hallyn if (!strcmp(name, XATTR_NAME_CAPS)) { 2627b5376771SSerge E. Hallyn if (!capable(CAP_SETFCAP)) 2628b5376771SSerge E. Hallyn return -EPERM; 2629b5376771SSerge E. Hallyn } else if (!capable(CAP_SYS_ADMIN)) { 2630b5376771SSerge E. Hallyn /* A different attribute in the security namespace. 2631b5376771SSerge E. Hallyn Restrict to administrator. */ 2632b5376771SSerge E. Hallyn return -EPERM; 2633b5376771SSerge E. Hallyn } 2634b5376771SSerge E. Hallyn } 2635b5376771SSerge E. Hallyn 2636b5376771SSerge E. Hallyn /* Not an attribute we recognize, so just check the 2637b5376771SSerge E. Hallyn ordinary setattr permission. */ 2638b5376771SSerge E. Hallyn return dentry_has_perm(current, NULL, dentry, FILE__SETATTR); 2639b5376771SSerge E. Hallyn } 2640b5376771SSerge E. Hallyn 26411da177e4SLinus Torvalds static int selinux_inode_setxattr(struct dentry *dentry, char *name, void *value, size_t size, int flags) 26421da177e4SLinus Torvalds { 26431da177e4SLinus Torvalds struct task_security_struct *tsec = current->security; 26441da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 26451da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 26461da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 26471da177e4SLinus Torvalds struct avc_audit_data ad; 26481da177e4SLinus Torvalds u32 newsid; 26491da177e4SLinus Torvalds int rc = 0; 26501da177e4SLinus Torvalds 2651b5376771SSerge E. Hallyn if (strcmp(name, XATTR_NAME_SELINUX)) 2652b5376771SSerge E. Hallyn return selinux_inode_setotherxattr(dentry, name); 26531da177e4SLinus Torvalds 26541da177e4SLinus Torvalds sbsec = inode->i_sb->s_security; 26551da177e4SLinus Torvalds if (sbsec->behavior == SECURITY_FS_USE_MNTPOINT) 26561da177e4SLinus Torvalds return -EOPNOTSUPP; 26571da177e4SLinus Torvalds 26583bd858abSSatyam Sharma if (!is_owner_or_cap(inode)) 26591da177e4SLinus Torvalds return -EPERM; 26601da177e4SLinus Torvalds 26611da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 266244707fdfSJan Blunck ad.u.fs.path.dentry = dentry; 26631da177e4SLinus Torvalds 26641da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, isec->sclass, 26651da177e4SLinus Torvalds FILE__RELABELFROM, &ad); 26661da177e4SLinus Torvalds if (rc) 26671da177e4SLinus Torvalds return rc; 26681da177e4SLinus Torvalds 26691da177e4SLinus Torvalds rc = security_context_to_sid(value, size, &newsid); 26701da177e4SLinus Torvalds if (rc) 26711da177e4SLinus Torvalds return rc; 26721da177e4SLinus Torvalds 26731da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, newsid, isec->sclass, 26741da177e4SLinus Torvalds FILE__RELABELTO, &ad); 26751da177e4SLinus Torvalds if (rc) 26761da177e4SLinus Torvalds return rc; 26771da177e4SLinus Torvalds 26781da177e4SLinus Torvalds rc = security_validate_transition(isec->sid, newsid, tsec->sid, 26791da177e4SLinus Torvalds isec->sclass); 26801da177e4SLinus Torvalds if (rc) 26811da177e4SLinus Torvalds return rc; 26821da177e4SLinus Torvalds 26831da177e4SLinus Torvalds return avc_has_perm(newsid, 26841da177e4SLinus Torvalds sbsec->sid, 26851da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 26861da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, 26871da177e4SLinus Torvalds &ad); 26881da177e4SLinus Torvalds } 26891da177e4SLinus Torvalds 26901da177e4SLinus Torvalds static void selinux_inode_post_setxattr(struct dentry *dentry, char *name, 26911da177e4SLinus Torvalds void *value, size_t size, int flags) 26921da177e4SLinus Torvalds { 26931da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 26941da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 26951da177e4SLinus Torvalds u32 newsid; 26961da177e4SLinus Torvalds int rc; 26971da177e4SLinus Torvalds 26981da177e4SLinus Torvalds if (strcmp(name, XATTR_NAME_SELINUX)) { 26991da177e4SLinus Torvalds /* Not an attribute we recognize, so nothing to do. */ 27001da177e4SLinus Torvalds return; 27011da177e4SLinus Torvalds } 27021da177e4SLinus Torvalds 27031da177e4SLinus Torvalds rc = security_context_to_sid(value, size, &newsid); 27041da177e4SLinus Torvalds if (rc) { 27051da177e4SLinus Torvalds printk(KERN_WARNING "%s: unable to obtain SID for context " 2706dd6f953aSHarvey Harrison "%s, rc=%d\n", __func__, (char *)value, -rc); 27071da177e4SLinus Torvalds return; 27081da177e4SLinus Torvalds } 27091da177e4SLinus Torvalds 27101da177e4SLinus Torvalds isec->sid = newsid; 27111da177e4SLinus Torvalds return; 27121da177e4SLinus Torvalds } 27131da177e4SLinus Torvalds 27141da177e4SLinus Torvalds static int selinux_inode_getxattr(struct dentry *dentry, char *name) 27151da177e4SLinus Torvalds { 27161da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__GETATTR); 27171da177e4SLinus Torvalds } 27181da177e4SLinus Torvalds 27191da177e4SLinus Torvalds static int selinux_inode_listxattr(struct dentry *dentry) 27201da177e4SLinus Torvalds { 27211da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__GETATTR); 27221da177e4SLinus Torvalds } 27231da177e4SLinus Torvalds 27241da177e4SLinus Torvalds static int selinux_inode_removexattr(struct dentry *dentry, char *name) 27251da177e4SLinus Torvalds { 2726b5376771SSerge E. Hallyn if (strcmp(name, XATTR_NAME_SELINUX)) 2727b5376771SSerge E. Hallyn return selinux_inode_setotherxattr(dentry, name); 27281da177e4SLinus Torvalds 27291da177e4SLinus Torvalds /* No one is allowed to remove a SELinux security label. 27301da177e4SLinus Torvalds You can change the label, but all data must be labeled. */ 27311da177e4SLinus Torvalds return -EACCES; 27321da177e4SLinus Torvalds } 27331da177e4SLinus Torvalds 2734d381d8a9SJames Morris /* 2735d381d8a9SJames Morris * Copy the in-core inode security context value to the user. If the 2736d381d8a9SJames Morris * getxattr() prior to this succeeded, check to see if we need to 2737d381d8a9SJames Morris * canonicalize the value to be finally returned to the user. 2738d381d8a9SJames Morris * 2739d381d8a9SJames Morris * Permission check is handled by selinux_inode_getxattr hook. 2740d381d8a9SJames Morris */ 274142492594SDavid P. Quigley static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void **buffer, bool alloc) 27421da177e4SLinus Torvalds { 274342492594SDavid P. Quigley u32 size; 274442492594SDavid P. Quigley int error; 274542492594SDavid P. Quigley char *context = NULL; 27461da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 27471da177e4SLinus Torvalds 27488c8570fbSDustin Kirkland if (strcmp(name, XATTR_SELINUX_SUFFIX)) 27498c8570fbSDustin Kirkland return -EOPNOTSUPP; 27501da177e4SLinus Torvalds 275142492594SDavid P. Quigley error = security_sid_to_context(isec->sid, &context, &size); 275242492594SDavid P. Quigley if (error) 275342492594SDavid P. Quigley return error; 275442492594SDavid P. Quigley error = size; 275542492594SDavid P. Quigley if (alloc) { 275642492594SDavid P. Quigley *buffer = context; 275742492594SDavid P. Quigley goto out_nofree; 275842492594SDavid P. Quigley } 275942492594SDavid P. Quigley kfree(context); 276042492594SDavid P. Quigley out_nofree: 276142492594SDavid P. Quigley return error; 27621da177e4SLinus Torvalds } 27631da177e4SLinus Torvalds 27641da177e4SLinus Torvalds static int selinux_inode_setsecurity(struct inode *inode, const char *name, 27651da177e4SLinus Torvalds const void *value, size_t size, int flags) 27661da177e4SLinus Torvalds { 27671da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 27681da177e4SLinus Torvalds u32 newsid; 27691da177e4SLinus Torvalds int rc; 27701da177e4SLinus Torvalds 27711da177e4SLinus Torvalds if (strcmp(name, XATTR_SELINUX_SUFFIX)) 27721da177e4SLinus Torvalds return -EOPNOTSUPP; 27731da177e4SLinus Torvalds 27741da177e4SLinus Torvalds if (!value || !size) 27751da177e4SLinus Torvalds return -EACCES; 27761da177e4SLinus Torvalds 27771da177e4SLinus Torvalds rc = security_context_to_sid((void *)value, size, &newsid); 27781da177e4SLinus Torvalds if (rc) 27791da177e4SLinus Torvalds return rc; 27801da177e4SLinus Torvalds 27811da177e4SLinus Torvalds isec->sid = newsid; 27821da177e4SLinus Torvalds return 0; 27831da177e4SLinus Torvalds } 27841da177e4SLinus Torvalds 27851da177e4SLinus Torvalds static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size) 27861da177e4SLinus Torvalds { 27871da177e4SLinus Torvalds const int len = sizeof(XATTR_NAME_SELINUX); 27881da177e4SLinus Torvalds if (buffer && len <= buffer_size) 27891da177e4SLinus Torvalds memcpy(buffer, XATTR_NAME_SELINUX, len); 27901da177e4SLinus Torvalds return len; 27911da177e4SLinus Torvalds } 27921da177e4SLinus Torvalds 2793b5376771SSerge E. Hallyn static int selinux_inode_need_killpriv(struct dentry *dentry) 2794b5376771SSerge E. Hallyn { 2795b5376771SSerge E. Hallyn return secondary_ops->inode_need_killpriv(dentry); 2796b5376771SSerge E. Hallyn } 2797b5376771SSerge E. Hallyn 2798b5376771SSerge E. Hallyn static int selinux_inode_killpriv(struct dentry *dentry) 2799b5376771SSerge E. Hallyn { 2800b5376771SSerge E. Hallyn return secondary_ops->inode_killpriv(dentry); 2801b5376771SSerge E. Hallyn } 2802b5376771SSerge E. Hallyn 2803713a04aeSAhmed S. Darwish static void selinux_inode_getsecid(const struct inode *inode, u32 *secid) 2804713a04aeSAhmed S. Darwish { 2805713a04aeSAhmed S. Darwish struct inode_security_struct *isec = inode->i_security; 2806713a04aeSAhmed S. Darwish *secid = isec->sid; 2807713a04aeSAhmed S. Darwish } 2808713a04aeSAhmed S. Darwish 28091da177e4SLinus Torvalds /* file security operations */ 28101da177e4SLinus Torvalds 2811788e7dd4SYuichi Nakamura static int selinux_revalidate_file_permission(struct file *file, int mask) 28121da177e4SLinus Torvalds { 28137420ed23SVenkat Yekkirala int rc; 28143d5ff529SJosef Sipek struct inode *inode = file->f_path.dentry->d_inode; 28151da177e4SLinus Torvalds 28161da177e4SLinus Torvalds if (!mask) { 28171da177e4SLinus Torvalds /* No permission to check. Existence test. */ 28181da177e4SLinus Torvalds return 0; 28191da177e4SLinus Torvalds } 28201da177e4SLinus Torvalds 28211da177e4SLinus Torvalds /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */ 28221da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE)) 28231da177e4SLinus Torvalds mask |= MAY_APPEND; 28241da177e4SLinus Torvalds 28257420ed23SVenkat Yekkirala rc = file_has_perm(current, file, 28261da177e4SLinus Torvalds file_mask_to_av(inode->i_mode, mask)); 28277420ed23SVenkat Yekkirala if (rc) 28287420ed23SVenkat Yekkirala return rc; 28297420ed23SVenkat Yekkirala 28307420ed23SVenkat Yekkirala return selinux_netlbl_inode_permission(inode, mask); 28311da177e4SLinus Torvalds } 28321da177e4SLinus Torvalds 2833788e7dd4SYuichi Nakamura static int selinux_file_permission(struct file *file, int mask) 2834788e7dd4SYuichi Nakamura { 2835788e7dd4SYuichi Nakamura struct inode *inode = file->f_path.dentry->d_inode; 2836788e7dd4SYuichi Nakamura struct task_security_struct *tsec = current->security; 2837788e7dd4SYuichi Nakamura struct file_security_struct *fsec = file->f_security; 2838788e7dd4SYuichi Nakamura struct inode_security_struct *isec = inode->i_security; 2839788e7dd4SYuichi Nakamura 2840788e7dd4SYuichi Nakamura if (!mask) { 2841788e7dd4SYuichi Nakamura /* No permission to check. Existence test. */ 2842788e7dd4SYuichi Nakamura return 0; 2843788e7dd4SYuichi Nakamura } 2844788e7dd4SYuichi Nakamura 2845788e7dd4SYuichi Nakamura if (tsec->sid == fsec->sid && fsec->isid == isec->sid 2846788e7dd4SYuichi Nakamura && fsec->pseqno == avc_policy_seqno()) 2847788e7dd4SYuichi Nakamura return selinux_netlbl_inode_permission(inode, mask); 2848788e7dd4SYuichi Nakamura 2849788e7dd4SYuichi Nakamura return selinux_revalidate_file_permission(file, mask); 2850788e7dd4SYuichi Nakamura } 2851788e7dd4SYuichi Nakamura 28521da177e4SLinus Torvalds static int selinux_file_alloc_security(struct file *file) 28531da177e4SLinus Torvalds { 28541da177e4SLinus Torvalds return file_alloc_security(file); 28551da177e4SLinus Torvalds } 28561da177e4SLinus Torvalds 28571da177e4SLinus Torvalds static void selinux_file_free_security(struct file *file) 28581da177e4SLinus Torvalds { 28591da177e4SLinus Torvalds file_free_security(file); 28601da177e4SLinus Torvalds } 28611da177e4SLinus Torvalds 28621da177e4SLinus Torvalds static int selinux_file_ioctl(struct file *file, unsigned int cmd, 28631da177e4SLinus Torvalds unsigned long arg) 28641da177e4SLinus Torvalds { 28651da177e4SLinus Torvalds int error = 0; 28661da177e4SLinus Torvalds 28671da177e4SLinus Torvalds switch (cmd) { 28681da177e4SLinus Torvalds case FIONREAD: 28691da177e4SLinus Torvalds /* fall through */ 28701da177e4SLinus Torvalds case FIBMAP: 28711da177e4SLinus Torvalds /* fall through */ 28721da177e4SLinus Torvalds case FIGETBSZ: 28731da177e4SLinus Torvalds /* fall through */ 28741da177e4SLinus Torvalds case EXT2_IOC_GETFLAGS: 28751da177e4SLinus Torvalds /* fall through */ 28761da177e4SLinus Torvalds case EXT2_IOC_GETVERSION: 28771da177e4SLinus Torvalds error = file_has_perm(current, file, FILE__GETATTR); 28781da177e4SLinus Torvalds break; 28791da177e4SLinus Torvalds 28801da177e4SLinus Torvalds case EXT2_IOC_SETFLAGS: 28811da177e4SLinus Torvalds /* fall through */ 28821da177e4SLinus Torvalds case EXT2_IOC_SETVERSION: 28831da177e4SLinus Torvalds error = file_has_perm(current, file, FILE__SETATTR); 28841da177e4SLinus Torvalds break; 28851da177e4SLinus Torvalds 28861da177e4SLinus Torvalds /* sys_ioctl() checks */ 28871da177e4SLinus Torvalds case FIONBIO: 28881da177e4SLinus Torvalds /* fall through */ 28891da177e4SLinus Torvalds case FIOASYNC: 28901da177e4SLinus Torvalds error = file_has_perm(current, file, 0); 28911da177e4SLinus Torvalds break; 28921da177e4SLinus Torvalds 28931da177e4SLinus Torvalds case KDSKBENT: 28941da177e4SLinus Torvalds case KDSKBSENT: 28951da177e4SLinus Torvalds error = task_has_capability(current, CAP_SYS_TTY_CONFIG); 28961da177e4SLinus Torvalds break; 28971da177e4SLinus Torvalds 28981da177e4SLinus Torvalds /* default case assumes that the command will go 28991da177e4SLinus Torvalds * to the file's ioctl() function. 29001da177e4SLinus Torvalds */ 29011da177e4SLinus Torvalds default: 29021da177e4SLinus Torvalds error = file_has_perm(current, file, FILE__IOCTL); 29031da177e4SLinus Torvalds } 29041da177e4SLinus Torvalds return error; 29051da177e4SLinus Torvalds } 29061da177e4SLinus Torvalds 29071da177e4SLinus Torvalds static int file_map_prot_check(struct file *file, unsigned long prot, int shared) 29081da177e4SLinus Torvalds { 29091da177e4SLinus Torvalds #ifndef CONFIG_PPC32 29101da177e4SLinus Torvalds if ((prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) { 29111da177e4SLinus Torvalds /* 29121da177e4SLinus Torvalds * We are making executable an anonymous mapping or a 29131da177e4SLinus Torvalds * private file mapping that will also be writable. 29141da177e4SLinus Torvalds * This has an additional check. 29151da177e4SLinus Torvalds */ 29161da177e4SLinus Torvalds int rc = task_has_perm(current, current, PROCESS__EXECMEM); 29171da177e4SLinus Torvalds if (rc) 29181da177e4SLinus Torvalds return rc; 29191da177e4SLinus Torvalds } 29201da177e4SLinus Torvalds #endif 29211da177e4SLinus Torvalds 29221da177e4SLinus Torvalds if (file) { 29231da177e4SLinus Torvalds /* read access is always possible with a mapping */ 29241da177e4SLinus Torvalds u32 av = FILE__READ; 29251da177e4SLinus Torvalds 29261da177e4SLinus Torvalds /* write access only matters if the mapping is shared */ 29271da177e4SLinus Torvalds if (shared && (prot & PROT_WRITE)) 29281da177e4SLinus Torvalds av |= FILE__WRITE; 29291da177e4SLinus Torvalds 29301da177e4SLinus Torvalds if (prot & PROT_EXEC) 29311da177e4SLinus Torvalds av |= FILE__EXECUTE; 29321da177e4SLinus Torvalds 29331da177e4SLinus Torvalds return file_has_perm(current, file, av); 29341da177e4SLinus Torvalds } 29351da177e4SLinus Torvalds return 0; 29361da177e4SLinus Torvalds } 29371da177e4SLinus Torvalds 29381da177e4SLinus Torvalds static int selinux_file_mmap(struct file *file, unsigned long reqprot, 2939ed032189SEric Paris unsigned long prot, unsigned long flags, 2940ed032189SEric Paris unsigned long addr, unsigned long addr_only) 29411da177e4SLinus Torvalds { 2942ed032189SEric Paris int rc = 0; 2943ed032189SEric Paris u32 sid = ((struct task_security_struct *)(current->security))->sid; 29441da177e4SLinus Torvalds 2945ed032189SEric Paris if (addr < mmap_min_addr) 2946ed032189SEric Paris rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT, 2947ed032189SEric Paris MEMPROTECT__MMAP_ZERO, NULL); 2948ed032189SEric Paris if (rc || addr_only) 29491da177e4SLinus Torvalds return rc; 29501da177e4SLinus Torvalds 29511da177e4SLinus Torvalds if (selinux_checkreqprot) 29521da177e4SLinus Torvalds prot = reqprot; 29531da177e4SLinus Torvalds 29541da177e4SLinus Torvalds return file_map_prot_check(file, prot, 29551da177e4SLinus Torvalds (flags & MAP_TYPE) == MAP_SHARED); 29561da177e4SLinus Torvalds } 29571da177e4SLinus Torvalds 29581da177e4SLinus Torvalds static int selinux_file_mprotect(struct vm_area_struct *vma, 29591da177e4SLinus Torvalds unsigned long reqprot, 29601da177e4SLinus Torvalds unsigned long prot) 29611da177e4SLinus Torvalds { 29621da177e4SLinus Torvalds int rc; 29631da177e4SLinus Torvalds 29641da177e4SLinus Torvalds rc = secondary_ops->file_mprotect(vma, reqprot, prot); 29651da177e4SLinus Torvalds if (rc) 29661da177e4SLinus Torvalds return rc; 29671da177e4SLinus Torvalds 29681da177e4SLinus Torvalds if (selinux_checkreqprot) 29691da177e4SLinus Torvalds prot = reqprot; 29701da177e4SLinus Torvalds 29711da177e4SLinus Torvalds #ifndef CONFIG_PPC32 2972db4c9641SStephen Smalley if ((prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) { 2973db4c9641SStephen Smalley rc = 0; 2974db4c9641SStephen Smalley if (vma->vm_start >= vma->vm_mm->start_brk && 2975db4c9641SStephen Smalley vma->vm_end <= vma->vm_mm->brk) { 2976db4c9641SStephen Smalley rc = task_has_perm(current, current, 2977db4c9641SStephen Smalley PROCESS__EXECHEAP); 2978db4c9641SStephen Smalley } else if (!vma->vm_file && 29796b992197SLorenzo Hernandez García-Hierro vma->vm_start <= vma->vm_mm->start_stack && 29806b992197SLorenzo Hernandez García-Hierro vma->vm_end >= vma->vm_mm->start_stack) { 29816b992197SLorenzo Hernandez García-Hierro rc = task_has_perm(current, current, PROCESS__EXECSTACK); 2982db4c9641SStephen Smalley } else if (vma->vm_file && vma->anon_vma) { 2983db4c9641SStephen Smalley /* 2984db4c9641SStephen Smalley * We are making executable a file mapping that has 2985db4c9641SStephen Smalley * had some COW done. Since pages might have been 2986db4c9641SStephen Smalley * written, check ability to execute the possibly 2987db4c9641SStephen Smalley * modified content. This typically should only 2988db4c9641SStephen Smalley * occur for text relocations. 2989db4c9641SStephen Smalley */ 2990db4c9641SStephen Smalley rc = file_has_perm(current, vma->vm_file, 2991db4c9641SStephen Smalley FILE__EXECMOD); 2992db4c9641SStephen Smalley } 29936b992197SLorenzo Hernandez García-Hierro if (rc) 29946b992197SLorenzo Hernandez García-Hierro return rc; 29956b992197SLorenzo Hernandez García-Hierro } 29961da177e4SLinus Torvalds #endif 29971da177e4SLinus Torvalds 29981da177e4SLinus Torvalds return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED); 29991da177e4SLinus Torvalds } 30001da177e4SLinus Torvalds 30011da177e4SLinus Torvalds static int selinux_file_lock(struct file *file, unsigned int cmd) 30021da177e4SLinus Torvalds { 30031da177e4SLinus Torvalds return file_has_perm(current, file, FILE__LOCK); 30041da177e4SLinus Torvalds } 30051da177e4SLinus Torvalds 30061da177e4SLinus Torvalds static int selinux_file_fcntl(struct file *file, unsigned int cmd, 30071da177e4SLinus Torvalds unsigned long arg) 30081da177e4SLinus Torvalds { 30091da177e4SLinus Torvalds int err = 0; 30101da177e4SLinus Torvalds 30111da177e4SLinus Torvalds switch (cmd) { 30121da177e4SLinus Torvalds case F_SETFL: 30133d5ff529SJosef Sipek if (!file->f_path.dentry || !file->f_path.dentry->d_inode) { 30141da177e4SLinus Torvalds err = -EINVAL; 30151da177e4SLinus Torvalds break; 30161da177e4SLinus Torvalds } 30171da177e4SLinus Torvalds 30181da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) { 30191da177e4SLinus Torvalds err = file_has_perm(current, file, FILE__WRITE); 30201da177e4SLinus Torvalds break; 30211da177e4SLinus Torvalds } 30221da177e4SLinus Torvalds /* fall through */ 30231da177e4SLinus Torvalds case F_SETOWN: 30241da177e4SLinus Torvalds case F_SETSIG: 30251da177e4SLinus Torvalds case F_GETFL: 30261da177e4SLinus Torvalds case F_GETOWN: 30271da177e4SLinus Torvalds case F_GETSIG: 30281da177e4SLinus Torvalds /* Just check FD__USE permission */ 30291da177e4SLinus Torvalds err = file_has_perm(current, file, 0); 30301da177e4SLinus Torvalds break; 30311da177e4SLinus Torvalds case F_GETLK: 30321da177e4SLinus Torvalds case F_SETLK: 30331da177e4SLinus Torvalds case F_SETLKW: 30341da177e4SLinus Torvalds #if BITS_PER_LONG == 32 30351da177e4SLinus Torvalds case F_GETLK64: 30361da177e4SLinus Torvalds case F_SETLK64: 30371da177e4SLinus Torvalds case F_SETLKW64: 30381da177e4SLinus Torvalds #endif 30393d5ff529SJosef Sipek if (!file->f_path.dentry || !file->f_path.dentry->d_inode) { 30401da177e4SLinus Torvalds err = -EINVAL; 30411da177e4SLinus Torvalds break; 30421da177e4SLinus Torvalds } 30431da177e4SLinus Torvalds err = file_has_perm(current, file, FILE__LOCK); 30441da177e4SLinus Torvalds break; 30451da177e4SLinus Torvalds } 30461da177e4SLinus Torvalds 30471da177e4SLinus Torvalds return err; 30481da177e4SLinus Torvalds } 30491da177e4SLinus Torvalds 30501da177e4SLinus Torvalds static int selinux_file_set_fowner(struct file *file) 30511da177e4SLinus Torvalds { 30521da177e4SLinus Torvalds struct task_security_struct *tsec; 30531da177e4SLinus Torvalds struct file_security_struct *fsec; 30541da177e4SLinus Torvalds 30551da177e4SLinus Torvalds tsec = current->security; 30561da177e4SLinus Torvalds fsec = file->f_security; 30571da177e4SLinus Torvalds fsec->fown_sid = tsec->sid; 30581da177e4SLinus Torvalds 30591da177e4SLinus Torvalds return 0; 30601da177e4SLinus Torvalds } 30611da177e4SLinus Torvalds 30621da177e4SLinus Torvalds static int selinux_file_send_sigiotask(struct task_struct *tsk, 30631da177e4SLinus Torvalds struct fown_struct *fown, int signum) 30641da177e4SLinus Torvalds { 30651da177e4SLinus Torvalds struct file *file; 30661da177e4SLinus Torvalds u32 perm; 30671da177e4SLinus Torvalds struct task_security_struct *tsec; 30681da177e4SLinus Torvalds struct file_security_struct *fsec; 30691da177e4SLinus Torvalds 30701da177e4SLinus Torvalds /* struct fown_struct is never outside the context of a struct file */ 3071b385a144SRobert P. J. Day file = container_of(fown, struct file, f_owner); 30721da177e4SLinus Torvalds 30731da177e4SLinus Torvalds tsec = tsk->security; 30741da177e4SLinus Torvalds fsec = file->f_security; 30751da177e4SLinus Torvalds 30761da177e4SLinus Torvalds if (!signum) 30771da177e4SLinus Torvalds perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */ 30781da177e4SLinus Torvalds else 30791da177e4SLinus Torvalds perm = signal_to_av(signum); 30801da177e4SLinus Torvalds 30811da177e4SLinus Torvalds return avc_has_perm(fsec->fown_sid, tsec->sid, 30821da177e4SLinus Torvalds SECCLASS_PROCESS, perm, NULL); 30831da177e4SLinus Torvalds } 30841da177e4SLinus Torvalds 30851da177e4SLinus Torvalds static int selinux_file_receive(struct file *file) 30861da177e4SLinus Torvalds { 30871da177e4SLinus Torvalds return file_has_perm(current, file, file_to_av(file)); 30881da177e4SLinus Torvalds } 30891da177e4SLinus Torvalds 3090788e7dd4SYuichi Nakamura static int selinux_dentry_open(struct file *file) 3091788e7dd4SYuichi Nakamura { 3092788e7dd4SYuichi Nakamura struct file_security_struct *fsec; 3093788e7dd4SYuichi Nakamura struct inode *inode; 3094788e7dd4SYuichi Nakamura struct inode_security_struct *isec; 3095788e7dd4SYuichi Nakamura inode = file->f_path.dentry->d_inode; 3096788e7dd4SYuichi Nakamura fsec = file->f_security; 3097788e7dd4SYuichi Nakamura isec = inode->i_security; 3098788e7dd4SYuichi Nakamura /* 3099788e7dd4SYuichi Nakamura * Save inode label and policy sequence number 3100788e7dd4SYuichi Nakamura * at open-time so that selinux_file_permission 3101788e7dd4SYuichi Nakamura * can determine whether revalidation is necessary. 3102788e7dd4SYuichi Nakamura * Task label is already saved in the file security 3103788e7dd4SYuichi Nakamura * struct as its SID. 3104788e7dd4SYuichi Nakamura */ 3105788e7dd4SYuichi Nakamura fsec->isid = isec->sid; 3106788e7dd4SYuichi Nakamura fsec->pseqno = avc_policy_seqno(); 3107788e7dd4SYuichi Nakamura /* 3108788e7dd4SYuichi Nakamura * Since the inode label or policy seqno may have changed 3109788e7dd4SYuichi Nakamura * between the selinux_inode_permission check and the saving 3110788e7dd4SYuichi Nakamura * of state above, recheck that access is still permitted. 3111788e7dd4SYuichi Nakamura * Otherwise, access might never be revalidated against the 3112788e7dd4SYuichi Nakamura * new inode label or new policy. 3113788e7dd4SYuichi Nakamura * This check is not redundant - do not remove. 3114788e7dd4SYuichi Nakamura */ 3115788e7dd4SYuichi Nakamura return inode_has_perm(current, inode, file_to_av(file), NULL); 3116788e7dd4SYuichi Nakamura } 3117788e7dd4SYuichi Nakamura 31181da177e4SLinus Torvalds /* task security operations */ 31191da177e4SLinus Torvalds 31201da177e4SLinus Torvalds static int selinux_task_create(unsigned long clone_flags) 31211da177e4SLinus Torvalds { 31221da177e4SLinus Torvalds int rc; 31231da177e4SLinus Torvalds 31241da177e4SLinus Torvalds rc = secondary_ops->task_create(clone_flags); 31251da177e4SLinus Torvalds if (rc) 31261da177e4SLinus Torvalds return rc; 31271da177e4SLinus Torvalds 31281da177e4SLinus Torvalds return task_has_perm(current, current, PROCESS__FORK); 31291da177e4SLinus Torvalds } 31301da177e4SLinus Torvalds 31311da177e4SLinus Torvalds static int selinux_task_alloc_security(struct task_struct *tsk) 31321da177e4SLinus Torvalds { 31331da177e4SLinus Torvalds struct task_security_struct *tsec1, *tsec2; 31341da177e4SLinus Torvalds int rc; 31351da177e4SLinus Torvalds 31361da177e4SLinus Torvalds tsec1 = current->security; 31371da177e4SLinus Torvalds 31381da177e4SLinus Torvalds rc = task_alloc_security(tsk); 31391da177e4SLinus Torvalds if (rc) 31401da177e4SLinus Torvalds return rc; 31411da177e4SLinus Torvalds tsec2 = tsk->security; 31421da177e4SLinus Torvalds 31431da177e4SLinus Torvalds tsec2->osid = tsec1->osid; 31441da177e4SLinus Torvalds tsec2->sid = tsec1->sid; 31451da177e4SLinus Torvalds 314628eba5bfSMichael LeMay /* Retain the exec, fs, key, and sock SIDs across fork */ 31471da177e4SLinus Torvalds tsec2->exec_sid = tsec1->exec_sid; 31481da177e4SLinus Torvalds tsec2->create_sid = tsec1->create_sid; 314928eba5bfSMichael LeMay tsec2->keycreate_sid = tsec1->keycreate_sid; 315042c3e03eSEric Paris tsec2->sockcreate_sid = tsec1->sockcreate_sid; 31511da177e4SLinus Torvalds 31521da177e4SLinus Torvalds return 0; 31531da177e4SLinus Torvalds } 31541da177e4SLinus Torvalds 31551da177e4SLinus Torvalds static void selinux_task_free_security(struct task_struct *tsk) 31561da177e4SLinus Torvalds { 31571da177e4SLinus Torvalds task_free_security(tsk); 31581da177e4SLinus Torvalds } 31591da177e4SLinus Torvalds 31601da177e4SLinus Torvalds static int selinux_task_setuid(uid_t id0, uid_t id1, uid_t id2, int flags) 31611da177e4SLinus Torvalds { 31621da177e4SLinus Torvalds /* Since setuid only affects the current process, and 31631da177e4SLinus Torvalds since the SELinux controls are not based on the Linux 31641da177e4SLinus Torvalds identity attributes, SELinux does not need to control 31651da177e4SLinus Torvalds this operation. However, SELinux does control the use 31661da177e4SLinus Torvalds of the CAP_SETUID and CAP_SETGID capabilities using the 31671da177e4SLinus Torvalds capable hook. */ 31681da177e4SLinus Torvalds return 0; 31691da177e4SLinus Torvalds } 31701da177e4SLinus Torvalds 31711da177e4SLinus Torvalds static int selinux_task_post_setuid(uid_t id0, uid_t id1, uid_t id2, int flags) 31721da177e4SLinus Torvalds { 31731da177e4SLinus Torvalds return secondary_ops->task_post_setuid(id0, id1, id2, flags); 31741da177e4SLinus Torvalds } 31751da177e4SLinus Torvalds 31761da177e4SLinus Torvalds static int selinux_task_setgid(gid_t id0, gid_t id1, gid_t id2, int flags) 31771da177e4SLinus Torvalds { 31781da177e4SLinus Torvalds /* See the comment for setuid above. */ 31791da177e4SLinus Torvalds return 0; 31801da177e4SLinus Torvalds } 31811da177e4SLinus Torvalds 31821da177e4SLinus Torvalds static int selinux_task_setpgid(struct task_struct *p, pid_t pgid) 31831da177e4SLinus Torvalds { 31841da177e4SLinus Torvalds return task_has_perm(current, p, PROCESS__SETPGID); 31851da177e4SLinus Torvalds } 31861da177e4SLinus Torvalds 31871da177e4SLinus Torvalds static int selinux_task_getpgid(struct task_struct *p) 31881da177e4SLinus Torvalds { 31891da177e4SLinus Torvalds return task_has_perm(current, p, PROCESS__GETPGID); 31901da177e4SLinus Torvalds } 31911da177e4SLinus Torvalds 31921da177e4SLinus Torvalds static int selinux_task_getsid(struct task_struct *p) 31931da177e4SLinus Torvalds { 31941da177e4SLinus Torvalds return task_has_perm(current, p, PROCESS__GETSESSION); 31951da177e4SLinus Torvalds } 31961da177e4SLinus Torvalds 3197f9008e4cSDavid Quigley static void selinux_task_getsecid(struct task_struct *p, u32 *secid) 3198f9008e4cSDavid Quigley { 3199713a04aeSAhmed S. Darwish struct task_security_struct *tsec = p->security; 3200713a04aeSAhmed S. Darwish *secid = tsec->sid; 3201f9008e4cSDavid Quigley } 3202f9008e4cSDavid Quigley 32031da177e4SLinus Torvalds static int selinux_task_setgroups(struct group_info *group_info) 32041da177e4SLinus Torvalds { 32051da177e4SLinus Torvalds /* See the comment for setuid above. */ 32061da177e4SLinus Torvalds return 0; 32071da177e4SLinus Torvalds } 32081da177e4SLinus Torvalds 32091da177e4SLinus Torvalds static int selinux_task_setnice(struct task_struct *p, int nice) 32101da177e4SLinus Torvalds { 32111da177e4SLinus Torvalds int rc; 32121da177e4SLinus Torvalds 32131da177e4SLinus Torvalds rc = secondary_ops->task_setnice(p, nice); 32141da177e4SLinus Torvalds if (rc) 32151da177e4SLinus Torvalds return rc; 32161da177e4SLinus Torvalds 32171da177e4SLinus Torvalds return task_has_perm(current, p, PROCESS__SETSCHED); 32181da177e4SLinus Torvalds } 32191da177e4SLinus Torvalds 322003e68060SJames Morris static int selinux_task_setioprio(struct task_struct *p, int ioprio) 322103e68060SJames Morris { 3222b5376771SSerge E. Hallyn int rc; 3223b5376771SSerge E. Hallyn 3224b5376771SSerge E. Hallyn rc = secondary_ops->task_setioprio(p, ioprio); 3225b5376771SSerge E. Hallyn if (rc) 3226b5376771SSerge E. Hallyn return rc; 3227b5376771SSerge E. Hallyn 322803e68060SJames Morris return task_has_perm(current, p, PROCESS__SETSCHED); 322903e68060SJames Morris } 323003e68060SJames Morris 3231a1836a42SDavid Quigley static int selinux_task_getioprio(struct task_struct *p) 3232a1836a42SDavid Quigley { 3233a1836a42SDavid Quigley return task_has_perm(current, p, PROCESS__GETSCHED); 3234a1836a42SDavid Quigley } 3235a1836a42SDavid Quigley 32361da177e4SLinus Torvalds static int selinux_task_setrlimit(unsigned int resource, struct rlimit *new_rlim) 32371da177e4SLinus Torvalds { 32381da177e4SLinus Torvalds struct rlimit *old_rlim = current->signal->rlim + resource; 32391da177e4SLinus Torvalds int rc; 32401da177e4SLinus Torvalds 32411da177e4SLinus Torvalds rc = secondary_ops->task_setrlimit(resource, new_rlim); 32421da177e4SLinus Torvalds if (rc) 32431da177e4SLinus Torvalds return rc; 32441da177e4SLinus Torvalds 32451da177e4SLinus Torvalds /* Control the ability to change the hard limit (whether 32461da177e4SLinus Torvalds lowering or raising it), so that the hard limit can 32471da177e4SLinus Torvalds later be used as a safe reset point for the soft limit 32481da177e4SLinus Torvalds upon context transitions. See selinux_bprm_apply_creds. */ 32491da177e4SLinus Torvalds if (old_rlim->rlim_max != new_rlim->rlim_max) 32501da177e4SLinus Torvalds return task_has_perm(current, current, PROCESS__SETRLIMIT); 32511da177e4SLinus Torvalds 32521da177e4SLinus Torvalds return 0; 32531da177e4SLinus Torvalds } 32541da177e4SLinus Torvalds 32551da177e4SLinus Torvalds static int selinux_task_setscheduler(struct task_struct *p, int policy, struct sched_param *lp) 32561da177e4SLinus Torvalds { 3257b5376771SSerge E. Hallyn int rc; 3258b5376771SSerge E. Hallyn 3259b5376771SSerge E. Hallyn rc = secondary_ops->task_setscheduler(p, policy, lp); 3260b5376771SSerge E. Hallyn if (rc) 3261b5376771SSerge E. Hallyn return rc; 3262b5376771SSerge E. Hallyn 32631da177e4SLinus Torvalds return task_has_perm(current, p, PROCESS__SETSCHED); 32641da177e4SLinus Torvalds } 32651da177e4SLinus Torvalds 32661da177e4SLinus Torvalds static int selinux_task_getscheduler(struct task_struct *p) 32671da177e4SLinus Torvalds { 32681da177e4SLinus Torvalds return task_has_perm(current, p, PROCESS__GETSCHED); 32691da177e4SLinus Torvalds } 32701da177e4SLinus Torvalds 327135601547SDavid Quigley static int selinux_task_movememory(struct task_struct *p) 327235601547SDavid Quigley { 327335601547SDavid Quigley return task_has_perm(current, p, PROCESS__SETSCHED); 327435601547SDavid Quigley } 327535601547SDavid Quigley 3276f9008e4cSDavid Quigley static int selinux_task_kill(struct task_struct *p, struct siginfo *info, 3277f9008e4cSDavid Quigley int sig, u32 secid) 32781da177e4SLinus Torvalds { 32791da177e4SLinus Torvalds u32 perm; 32801da177e4SLinus Torvalds int rc; 3281f9008e4cSDavid Quigley struct task_security_struct *tsec; 32821da177e4SLinus Torvalds 3283f9008e4cSDavid Quigley rc = secondary_ops->task_kill(p, info, sig, secid); 32841da177e4SLinus Torvalds if (rc) 32851da177e4SLinus Torvalds return rc; 32861da177e4SLinus Torvalds 3287621d3121SOleg Nesterov if (info != SEND_SIG_NOINFO && (is_si_special(info) || SI_FROMKERNEL(info))) 32881da177e4SLinus Torvalds return 0; 32891da177e4SLinus Torvalds 32901da177e4SLinus Torvalds if (!sig) 32911da177e4SLinus Torvalds perm = PROCESS__SIGNULL; /* null signal; existence test */ 32921da177e4SLinus Torvalds else 32931da177e4SLinus Torvalds perm = signal_to_av(sig); 3294f9008e4cSDavid Quigley tsec = p->security; 3295f9008e4cSDavid Quigley if (secid) 3296f9008e4cSDavid Quigley rc = avc_has_perm(secid, tsec->sid, SECCLASS_PROCESS, perm, NULL); 3297f9008e4cSDavid Quigley else 3298f9008e4cSDavid Quigley rc = task_has_perm(current, p, perm); 3299f9008e4cSDavid Quigley return rc; 33001da177e4SLinus Torvalds } 33011da177e4SLinus Torvalds 33021da177e4SLinus Torvalds static int selinux_task_prctl(int option, 33031da177e4SLinus Torvalds unsigned long arg2, 33041da177e4SLinus Torvalds unsigned long arg3, 33051da177e4SLinus Torvalds unsigned long arg4, 3306*3898b1b4SAndrew G. Morgan unsigned long arg5, 3307*3898b1b4SAndrew G. Morgan long *rc_p) 33081da177e4SLinus Torvalds { 33091da177e4SLinus Torvalds /* The current prctl operations do not appear to require 33101da177e4SLinus Torvalds any SELinux controls since they merely observe or modify 33111da177e4SLinus Torvalds the state of the current process. */ 3312*3898b1b4SAndrew G. Morgan return secondary_ops->task_prctl(option, arg2, arg3, arg4, arg5, rc_p); 33131da177e4SLinus Torvalds } 33141da177e4SLinus Torvalds 33151da177e4SLinus Torvalds static int selinux_task_wait(struct task_struct *p) 33161da177e4SLinus Torvalds { 33178a535140SEric Paris return task_has_perm(p, current, PROCESS__SIGCHLD); 33181da177e4SLinus Torvalds } 33191da177e4SLinus Torvalds 33201da177e4SLinus Torvalds static void selinux_task_reparent_to_init(struct task_struct *p) 33211da177e4SLinus Torvalds { 33221da177e4SLinus Torvalds struct task_security_struct *tsec; 33231da177e4SLinus Torvalds 33241da177e4SLinus Torvalds secondary_ops->task_reparent_to_init(p); 33251da177e4SLinus Torvalds 33261da177e4SLinus Torvalds tsec = p->security; 33271da177e4SLinus Torvalds tsec->osid = tsec->sid; 33281da177e4SLinus Torvalds tsec->sid = SECINITSID_KERNEL; 33291da177e4SLinus Torvalds return; 33301da177e4SLinus Torvalds } 33311da177e4SLinus Torvalds 33321da177e4SLinus Torvalds static void selinux_task_to_inode(struct task_struct *p, 33331da177e4SLinus Torvalds struct inode *inode) 33341da177e4SLinus Torvalds { 33351da177e4SLinus Torvalds struct task_security_struct *tsec = p->security; 33361da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 33371da177e4SLinus Torvalds 33381da177e4SLinus Torvalds isec->sid = tsec->sid; 33391da177e4SLinus Torvalds isec->initialized = 1; 33401da177e4SLinus Torvalds return; 33411da177e4SLinus Torvalds } 33421da177e4SLinus Torvalds 33431da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 334467f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv4(struct sk_buff *skb, 334567f83cbfSVenkat Yekkirala struct avc_audit_data *ad, u8 *proto) 33461da177e4SLinus Torvalds { 33471da177e4SLinus Torvalds int offset, ihlen, ret = -EINVAL; 33481da177e4SLinus Torvalds struct iphdr _iph, *ih; 33491da177e4SLinus Torvalds 3350bbe735e4SArnaldo Carvalho de Melo offset = skb_network_offset(skb); 33511da177e4SLinus Torvalds ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph); 33521da177e4SLinus Torvalds if (ih == NULL) 33531da177e4SLinus Torvalds goto out; 33541da177e4SLinus Torvalds 33551da177e4SLinus Torvalds ihlen = ih->ihl * 4; 33561da177e4SLinus Torvalds if (ihlen < sizeof(_iph)) 33571da177e4SLinus Torvalds goto out; 33581da177e4SLinus Torvalds 33591da177e4SLinus Torvalds ad->u.net.v4info.saddr = ih->saddr; 33601da177e4SLinus Torvalds ad->u.net.v4info.daddr = ih->daddr; 33611da177e4SLinus Torvalds ret = 0; 33621da177e4SLinus Torvalds 336367f83cbfSVenkat Yekkirala if (proto) 336467f83cbfSVenkat Yekkirala *proto = ih->protocol; 336567f83cbfSVenkat Yekkirala 33661da177e4SLinus Torvalds switch (ih->protocol) { 33671da177e4SLinus Torvalds case IPPROTO_TCP: { 33681da177e4SLinus Torvalds struct tcphdr _tcph, *th; 33691da177e4SLinus Torvalds 33701da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 33711da177e4SLinus Torvalds break; 33721da177e4SLinus Torvalds 33731da177e4SLinus Torvalds offset += ihlen; 33741da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 33751da177e4SLinus Torvalds if (th == NULL) 33761da177e4SLinus Torvalds break; 33771da177e4SLinus Torvalds 33781da177e4SLinus Torvalds ad->u.net.sport = th->source; 33791da177e4SLinus Torvalds ad->u.net.dport = th->dest; 33801da177e4SLinus Torvalds break; 33811da177e4SLinus Torvalds } 33821da177e4SLinus Torvalds 33831da177e4SLinus Torvalds case IPPROTO_UDP: { 33841da177e4SLinus Torvalds struct udphdr _udph, *uh; 33851da177e4SLinus Torvalds 33861da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 33871da177e4SLinus Torvalds break; 33881da177e4SLinus Torvalds 33891da177e4SLinus Torvalds offset += ihlen; 33901da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 33911da177e4SLinus Torvalds if (uh == NULL) 33921da177e4SLinus Torvalds break; 33931da177e4SLinus Torvalds 33941da177e4SLinus Torvalds ad->u.net.sport = uh->source; 33951da177e4SLinus Torvalds ad->u.net.dport = uh->dest; 33961da177e4SLinus Torvalds break; 33971da177e4SLinus Torvalds } 33981da177e4SLinus Torvalds 33992ee92d46SJames Morris case IPPROTO_DCCP: { 34002ee92d46SJames Morris struct dccp_hdr _dccph, *dh; 34012ee92d46SJames Morris 34022ee92d46SJames Morris if (ntohs(ih->frag_off) & IP_OFFSET) 34032ee92d46SJames Morris break; 34042ee92d46SJames Morris 34052ee92d46SJames Morris offset += ihlen; 34062ee92d46SJames Morris dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); 34072ee92d46SJames Morris if (dh == NULL) 34082ee92d46SJames Morris break; 34092ee92d46SJames Morris 34102ee92d46SJames Morris ad->u.net.sport = dh->dccph_sport; 34112ee92d46SJames Morris ad->u.net.dport = dh->dccph_dport; 34122ee92d46SJames Morris break; 34132ee92d46SJames Morris } 34142ee92d46SJames Morris 34151da177e4SLinus Torvalds default: 34161da177e4SLinus Torvalds break; 34171da177e4SLinus Torvalds } 34181da177e4SLinus Torvalds out: 34191da177e4SLinus Torvalds return ret; 34201da177e4SLinus Torvalds } 34211da177e4SLinus Torvalds 34221da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 34231da177e4SLinus Torvalds 34241da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 342567f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv6(struct sk_buff *skb, 342667f83cbfSVenkat Yekkirala struct avc_audit_data *ad, u8 *proto) 34271da177e4SLinus Torvalds { 34281da177e4SLinus Torvalds u8 nexthdr; 34291da177e4SLinus Torvalds int ret = -EINVAL, offset; 34301da177e4SLinus Torvalds struct ipv6hdr _ipv6h, *ip6; 34311da177e4SLinus Torvalds 3432bbe735e4SArnaldo Carvalho de Melo offset = skb_network_offset(skb); 34331da177e4SLinus Torvalds ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h); 34341da177e4SLinus Torvalds if (ip6 == NULL) 34351da177e4SLinus Torvalds goto out; 34361da177e4SLinus Torvalds 34371da177e4SLinus Torvalds ipv6_addr_copy(&ad->u.net.v6info.saddr, &ip6->saddr); 34381da177e4SLinus Torvalds ipv6_addr_copy(&ad->u.net.v6info.daddr, &ip6->daddr); 34391da177e4SLinus Torvalds ret = 0; 34401da177e4SLinus Torvalds 34411da177e4SLinus Torvalds nexthdr = ip6->nexthdr; 34421da177e4SLinus Torvalds offset += sizeof(_ipv6h); 34430d3d077cSHerbert Xu offset = ipv6_skip_exthdr(skb, offset, &nexthdr); 34441da177e4SLinus Torvalds if (offset < 0) 34451da177e4SLinus Torvalds goto out; 34461da177e4SLinus Torvalds 344767f83cbfSVenkat Yekkirala if (proto) 344867f83cbfSVenkat Yekkirala *proto = nexthdr; 344967f83cbfSVenkat Yekkirala 34501da177e4SLinus Torvalds switch (nexthdr) { 34511da177e4SLinus Torvalds case IPPROTO_TCP: { 34521da177e4SLinus Torvalds struct tcphdr _tcph, *th; 34531da177e4SLinus Torvalds 34541da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 34551da177e4SLinus Torvalds if (th == NULL) 34561da177e4SLinus Torvalds break; 34571da177e4SLinus Torvalds 34581da177e4SLinus Torvalds ad->u.net.sport = th->source; 34591da177e4SLinus Torvalds ad->u.net.dport = th->dest; 34601da177e4SLinus Torvalds break; 34611da177e4SLinus Torvalds } 34621da177e4SLinus Torvalds 34631da177e4SLinus Torvalds case IPPROTO_UDP: { 34641da177e4SLinus Torvalds struct udphdr _udph, *uh; 34651da177e4SLinus Torvalds 34661da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 34671da177e4SLinus Torvalds if (uh == NULL) 34681da177e4SLinus Torvalds break; 34691da177e4SLinus Torvalds 34701da177e4SLinus Torvalds ad->u.net.sport = uh->source; 34711da177e4SLinus Torvalds ad->u.net.dport = uh->dest; 34721da177e4SLinus Torvalds break; 34731da177e4SLinus Torvalds } 34741da177e4SLinus Torvalds 34752ee92d46SJames Morris case IPPROTO_DCCP: { 34762ee92d46SJames Morris struct dccp_hdr _dccph, *dh; 34772ee92d46SJames Morris 34782ee92d46SJames Morris dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); 34792ee92d46SJames Morris if (dh == NULL) 34802ee92d46SJames Morris break; 34812ee92d46SJames Morris 34822ee92d46SJames Morris ad->u.net.sport = dh->dccph_sport; 34832ee92d46SJames Morris ad->u.net.dport = dh->dccph_dport; 34842ee92d46SJames Morris break; 34852ee92d46SJames Morris } 34862ee92d46SJames Morris 34871da177e4SLinus Torvalds /* includes fragments */ 34881da177e4SLinus Torvalds default: 34891da177e4SLinus Torvalds break; 34901da177e4SLinus Torvalds } 34911da177e4SLinus Torvalds out: 34921da177e4SLinus Torvalds return ret; 34931da177e4SLinus Torvalds } 34941da177e4SLinus Torvalds 34951da177e4SLinus Torvalds #endif /* IPV6 */ 34961da177e4SLinus Torvalds 34971da177e4SLinus Torvalds static int selinux_parse_skb(struct sk_buff *skb, struct avc_audit_data *ad, 3498224dfbd8SPaul Moore char **addrp, int src, u8 *proto) 34991da177e4SLinus Torvalds { 35001da177e4SLinus Torvalds int ret = 0; 35011da177e4SLinus Torvalds 35021da177e4SLinus Torvalds switch (ad->u.net.family) { 35031da177e4SLinus Torvalds case PF_INET: 350467f83cbfSVenkat Yekkirala ret = selinux_parse_skb_ipv4(skb, ad, proto); 35051da177e4SLinus Torvalds if (ret || !addrp) 35061da177e4SLinus Torvalds break; 35071da177e4SLinus Torvalds *addrp = (char *)(src ? &ad->u.net.v4info.saddr : 35081da177e4SLinus Torvalds &ad->u.net.v4info.daddr); 35091da177e4SLinus Torvalds break; 35101da177e4SLinus Torvalds 35111da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 35121da177e4SLinus Torvalds case PF_INET6: 351367f83cbfSVenkat Yekkirala ret = selinux_parse_skb_ipv6(skb, ad, proto); 35141da177e4SLinus Torvalds if (ret || !addrp) 35151da177e4SLinus Torvalds break; 35161da177e4SLinus Torvalds *addrp = (char *)(src ? &ad->u.net.v6info.saddr : 35171da177e4SLinus Torvalds &ad->u.net.v6info.daddr); 35181da177e4SLinus Torvalds break; 35191da177e4SLinus Torvalds #endif /* IPV6 */ 35201da177e4SLinus Torvalds default: 35211da177e4SLinus Torvalds break; 35221da177e4SLinus Torvalds } 35231da177e4SLinus Torvalds 352471f1cb05SPaul Moore if (unlikely(ret)) 352571f1cb05SPaul Moore printk(KERN_WARNING 352671f1cb05SPaul Moore "SELinux: failure in selinux_parse_skb()," 352771f1cb05SPaul Moore " unable to parse packet\n"); 352871f1cb05SPaul Moore 35291da177e4SLinus Torvalds return ret; 35301da177e4SLinus Torvalds } 35311da177e4SLinus Torvalds 35324f6a993fSPaul Moore /** 3533220deb96SPaul Moore * selinux_skb_peerlbl_sid - Determine the peer label of a packet 35344f6a993fSPaul Moore * @skb: the packet 353575e22910SPaul Moore * @family: protocol family 3536220deb96SPaul Moore * @sid: the packet's peer label SID 35374f6a993fSPaul Moore * 35384f6a993fSPaul Moore * Description: 3539220deb96SPaul Moore * Check the various different forms of network peer labeling and determine 3540220deb96SPaul Moore * the peer label/SID for the packet; most of the magic actually occurs in 3541220deb96SPaul Moore * the security server function security_net_peersid_cmp(). The function 3542220deb96SPaul Moore * returns zero if the value in @sid is valid (although it may be SECSID_NULL) 3543220deb96SPaul Moore * or -EACCES if @sid is invalid due to inconsistencies with the different 3544220deb96SPaul Moore * peer labels. 35454f6a993fSPaul Moore * 35464f6a993fSPaul Moore */ 3547220deb96SPaul Moore static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid) 35484f6a993fSPaul Moore { 354971f1cb05SPaul Moore int err; 35504f6a993fSPaul Moore u32 xfrm_sid; 35514f6a993fSPaul Moore u32 nlbl_sid; 3552220deb96SPaul Moore u32 nlbl_type; 35534f6a993fSPaul Moore 35544f6a993fSPaul Moore selinux_skb_xfrm_sid(skb, &xfrm_sid); 35555dbe1eb0SPaul Moore selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid); 3556220deb96SPaul Moore 355771f1cb05SPaul Moore err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid); 355871f1cb05SPaul Moore if (unlikely(err)) { 355971f1cb05SPaul Moore printk(KERN_WARNING 356071f1cb05SPaul Moore "SELinux: failure in selinux_skb_peerlbl_sid()," 356171f1cb05SPaul Moore " unable to determine packet's peer label\n"); 3562220deb96SPaul Moore return -EACCES; 356371f1cb05SPaul Moore } 3564220deb96SPaul Moore 3565220deb96SPaul Moore return 0; 35664f6a993fSPaul Moore } 35674f6a993fSPaul Moore 35681da177e4SLinus Torvalds /* socket security operations */ 35691da177e4SLinus Torvalds static int socket_has_perm(struct task_struct *task, struct socket *sock, 35701da177e4SLinus Torvalds u32 perms) 35711da177e4SLinus Torvalds { 35721da177e4SLinus Torvalds struct inode_security_struct *isec; 35731da177e4SLinus Torvalds struct task_security_struct *tsec; 35741da177e4SLinus Torvalds struct avc_audit_data ad; 35751da177e4SLinus Torvalds int err = 0; 35761da177e4SLinus Torvalds 35771da177e4SLinus Torvalds tsec = task->security; 35781da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 35791da177e4SLinus Torvalds 35801da177e4SLinus Torvalds if (isec->sid == SECINITSID_KERNEL) 35811da177e4SLinus Torvalds goto out; 35821da177e4SLinus Torvalds 35831da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, NET); 35841da177e4SLinus Torvalds ad.u.net.sk = sock->sk; 35851da177e4SLinus Torvalds err = avc_has_perm(tsec->sid, isec->sid, isec->sclass, perms, &ad); 35861da177e4SLinus Torvalds 35871da177e4SLinus Torvalds out: 35881da177e4SLinus Torvalds return err; 35891da177e4SLinus Torvalds } 35901da177e4SLinus Torvalds 35911da177e4SLinus Torvalds static int selinux_socket_create(int family, int type, 35921da177e4SLinus Torvalds int protocol, int kern) 35931da177e4SLinus Torvalds { 35941da177e4SLinus Torvalds int err = 0; 35951da177e4SLinus Torvalds struct task_security_struct *tsec; 359642c3e03eSEric Paris u32 newsid; 35971da177e4SLinus Torvalds 35981da177e4SLinus Torvalds if (kern) 35991da177e4SLinus Torvalds goto out; 36001da177e4SLinus Torvalds 36011da177e4SLinus Torvalds tsec = current->security; 360242c3e03eSEric Paris newsid = tsec->sockcreate_sid ? : tsec->sid; 360342c3e03eSEric Paris err = avc_has_perm(tsec->sid, newsid, 36041da177e4SLinus Torvalds socket_type_to_security_class(family, type, 36051da177e4SLinus Torvalds protocol), SOCKET__CREATE, NULL); 36061da177e4SLinus Torvalds 36071da177e4SLinus Torvalds out: 36081da177e4SLinus Torvalds return err; 36091da177e4SLinus Torvalds } 36101da177e4SLinus Torvalds 36117420ed23SVenkat Yekkirala static int selinux_socket_post_create(struct socket *sock, int family, 36121da177e4SLinus Torvalds int type, int protocol, int kern) 36131da177e4SLinus Torvalds { 36147420ed23SVenkat Yekkirala int err = 0; 36151da177e4SLinus Torvalds struct inode_security_struct *isec; 36161da177e4SLinus Torvalds struct task_security_struct *tsec; 3617892c141eSVenkat Yekkirala struct sk_security_struct *sksec; 361842c3e03eSEric Paris u32 newsid; 36191da177e4SLinus Torvalds 36201da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 36211da177e4SLinus Torvalds 36221da177e4SLinus Torvalds tsec = current->security; 362342c3e03eSEric Paris newsid = tsec->sockcreate_sid ? : tsec->sid; 36241da177e4SLinus Torvalds isec->sclass = socket_type_to_security_class(family, type, protocol); 362542c3e03eSEric Paris isec->sid = kern ? SECINITSID_KERNEL : newsid; 36261da177e4SLinus Torvalds isec->initialized = 1; 36271da177e4SLinus Torvalds 3628892c141eSVenkat Yekkirala if (sock->sk) { 3629892c141eSVenkat Yekkirala sksec = sock->sk->sk_security; 3630892c141eSVenkat Yekkirala sksec->sid = isec->sid; 3631220deb96SPaul Moore sksec->sclass = isec->sclass; 36329f2ad665SPaul Moore err = selinux_netlbl_socket_post_create(sock); 3633892c141eSVenkat Yekkirala } 3634892c141eSVenkat Yekkirala 36357420ed23SVenkat Yekkirala return err; 36361da177e4SLinus Torvalds } 36371da177e4SLinus Torvalds 36381da177e4SLinus Torvalds /* Range of port numbers used to automatically bind. 36391da177e4SLinus Torvalds Need to determine whether we should perform a name_bind 36401da177e4SLinus Torvalds permission check between the socket and the port number. */ 36411da177e4SLinus Torvalds 36421da177e4SLinus Torvalds static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen) 36431da177e4SLinus Torvalds { 36441da177e4SLinus Torvalds u16 family; 36451da177e4SLinus Torvalds int err; 36461da177e4SLinus Torvalds 36471da177e4SLinus Torvalds err = socket_has_perm(current, sock, SOCKET__BIND); 36481da177e4SLinus Torvalds if (err) 36491da177e4SLinus Torvalds goto out; 36501da177e4SLinus Torvalds 36511da177e4SLinus Torvalds /* 36521da177e4SLinus Torvalds * If PF_INET or PF_INET6, check name_bind permission for the port. 365313402580SJames Morris * Multiple address binding for SCTP is not supported yet: we just 365413402580SJames Morris * check the first address now. 36551da177e4SLinus Torvalds */ 36561da177e4SLinus Torvalds family = sock->sk->sk_family; 36571da177e4SLinus Torvalds if (family == PF_INET || family == PF_INET6) { 36581da177e4SLinus Torvalds char *addrp; 36591da177e4SLinus Torvalds struct inode_security_struct *isec; 36601da177e4SLinus Torvalds struct task_security_struct *tsec; 36611da177e4SLinus Torvalds struct avc_audit_data ad; 36621da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 36631da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 36641da177e4SLinus Torvalds unsigned short snum; 36651da177e4SLinus Torvalds struct sock *sk = sock->sk; 36661da177e4SLinus Torvalds u32 sid, node_perm, addrlen; 36671da177e4SLinus Torvalds 36681da177e4SLinus Torvalds tsec = current->security; 36691da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 36701da177e4SLinus Torvalds 36711da177e4SLinus Torvalds if (family == PF_INET) { 36721da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 36731da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 36741da177e4SLinus Torvalds addrlen = sizeof(addr4->sin_addr.s_addr); 36751da177e4SLinus Torvalds addrp = (char *)&addr4->sin_addr.s_addr; 36761da177e4SLinus Torvalds } else { 36771da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 36781da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 36791da177e4SLinus Torvalds addrlen = sizeof(addr6->sin6_addr.s6_addr); 36801da177e4SLinus Torvalds addrp = (char *)&addr6->sin6_addr.s6_addr; 36811da177e4SLinus Torvalds } 36821da177e4SLinus Torvalds 3683227b60f5SStephen Hemminger if (snum) { 3684227b60f5SStephen Hemminger int low, high; 3685227b60f5SStephen Hemminger 3686227b60f5SStephen Hemminger inet_get_local_port_range(&low, &high); 3687227b60f5SStephen Hemminger 3688227b60f5SStephen Hemminger if (snum < max(PROT_SOCK, low) || snum > high) { 36893e112172SPaul Moore err = sel_netport_sid(sk->sk_protocol, 36903e112172SPaul Moore snum, &sid); 36911da177e4SLinus Torvalds if (err) 36921da177e4SLinus Torvalds goto out; 36931da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, NET); 36941da177e4SLinus Torvalds ad.u.net.sport = htons(snum); 36951da177e4SLinus Torvalds ad.u.net.family = family; 36961da177e4SLinus Torvalds err = avc_has_perm(isec->sid, sid, 36971da177e4SLinus Torvalds isec->sclass, 36981da177e4SLinus Torvalds SOCKET__NAME_BIND, &ad); 36991da177e4SLinus Torvalds if (err) 37001da177e4SLinus Torvalds goto out; 37011da177e4SLinus Torvalds } 3702227b60f5SStephen Hemminger } 37031da177e4SLinus Torvalds 370413402580SJames Morris switch (isec->sclass) { 370513402580SJames Morris case SECCLASS_TCP_SOCKET: 37061da177e4SLinus Torvalds node_perm = TCP_SOCKET__NODE_BIND; 37071da177e4SLinus Torvalds break; 37081da177e4SLinus Torvalds 370913402580SJames Morris case SECCLASS_UDP_SOCKET: 37101da177e4SLinus Torvalds node_perm = UDP_SOCKET__NODE_BIND; 37111da177e4SLinus Torvalds break; 37121da177e4SLinus Torvalds 37132ee92d46SJames Morris case SECCLASS_DCCP_SOCKET: 37142ee92d46SJames Morris node_perm = DCCP_SOCKET__NODE_BIND; 37152ee92d46SJames Morris break; 37162ee92d46SJames Morris 37171da177e4SLinus Torvalds default: 37181da177e4SLinus Torvalds node_perm = RAWIP_SOCKET__NODE_BIND; 37191da177e4SLinus Torvalds break; 37201da177e4SLinus Torvalds } 37211da177e4SLinus Torvalds 3722224dfbd8SPaul Moore err = sel_netnode_sid(addrp, family, &sid); 37231da177e4SLinus Torvalds if (err) 37241da177e4SLinus Torvalds goto out; 37251da177e4SLinus Torvalds 37261da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, NET); 37271da177e4SLinus Torvalds ad.u.net.sport = htons(snum); 37281da177e4SLinus Torvalds ad.u.net.family = family; 37291da177e4SLinus Torvalds 37301da177e4SLinus Torvalds if (family == PF_INET) 37311da177e4SLinus Torvalds ad.u.net.v4info.saddr = addr4->sin_addr.s_addr; 37321da177e4SLinus Torvalds else 37331da177e4SLinus Torvalds ipv6_addr_copy(&ad.u.net.v6info.saddr, &addr6->sin6_addr); 37341da177e4SLinus Torvalds 37351da177e4SLinus Torvalds err = avc_has_perm(isec->sid, sid, 37361da177e4SLinus Torvalds isec->sclass, node_perm, &ad); 37371da177e4SLinus Torvalds if (err) 37381da177e4SLinus Torvalds goto out; 37391da177e4SLinus Torvalds } 37401da177e4SLinus Torvalds out: 37411da177e4SLinus Torvalds return err; 37421da177e4SLinus Torvalds } 37431da177e4SLinus Torvalds 37441da177e4SLinus Torvalds static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen) 37451da177e4SLinus Torvalds { 37461da177e4SLinus Torvalds struct inode_security_struct *isec; 37471da177e4SLinus Torvalds int err; 37481da177e4SLinus Torvalds 37491da177e4SLinus Torvalds err = socket_has_perm(current, sock, SOCKET__CONNECT); 37501da177e4SLinus Torvalds if (err) 37511da177e4SLinus Torvalds return err; 37521da177e4SLinus Torvalds 37531da177e4SLinus Torvalds /* 37542ee92d46SJames Morris * If a TCP or DCCP socket, check name_connect permission for the port. 37551da177e4SLinus Torvalds */ 37561da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 37572ee92d46SJames Morris if (isec->sclass == SECCLASS_TCP_SOCKET || 37582ee92d46SJames Morris isec->sclass == SECCLASS_DCCP_SOCKET) { 37591da177e4SLinus Torvalds struct sock *sk = sock->sk; 37601da177e4SLinus Torvalds struct avc_audit_data ad; 37611da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 37621da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 37631da177e4SLinus Torvalds unsigned short snum; 37642ee92d46SJames Morris u32 sid, perm; 37651da177e4SLinus Torvalds 37661da177e4SLinus Torvalds if (sk->sk_family == PF_INET) { 37671da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 3768911656f8SStephen Smalley if (addrlen < sizeof(struct sockaddr_in)) 37691da177e4SLinus Torvalds return -EINVAL; 37701da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 37711da177e4SLinus Torvalds } else { 37721da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 3773911656f8SStephen Smalley if (addrlen < SIN6_LEN_RFC2133) 37741da177e4SLinus Torvalds return -EINVAL; 37751da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 37761da177e4SLinus Torvalds } 37771da177e4SLinus Torvalds 37783e112172SPaul Moore err = sel_netport_sid(sk->sk_protocol, snum, &sid); 37791da177e4SLinus Torvalds if (err) 37801da177e4SLinus Torvalds goto out; 37811da177e4SLinus Torvalds 37822ee92d46SJames Morris perm = (isec->sclass == SECCLASS_TCP_SOCKET) ? 37832ee92d46SJames Morris TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT; 37842ee92d46SJames Morris 37851da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, NET); 37861da177e4SLinus Torvalds ad.u.net.dport = htons(snum); 37871da177e4SLinus Torvalds ad.u.net.family = sk->sk_family; 37882ee92d46SJames Morris err = avc_has_perm(isec->sid, sid, isec->sclass, perm, &ad); 37891da177e4SLinus Torvalds if (err) 37901da177e4SLinus Torvalds goto out; 37911da177e4SLinus Torvalds } 37921da177e4SLinus Torvalds 37931da177e4SLinus Torvalds out: 37941da177e4SLinus Torvalds return err; 37951da177e4SLinus Torvalds } 37961da177e4SLinus Torvalds 37971da177e4SLinus Torvalds static int selinux_socket_listen(struct socket *sock, int backlog) 37981da177e4SLinus Torvalds { 37991da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__LISTEN); 38001da177e4SLinus Torvalds } 38011da177e4SLinus Torvalds 38021da177e4SLinus Torvalds static int selinux_socket_accept(struct socket *sock, struct socket *newsock) 38031da177e4SLinus Torvalds { 38041da177e4SLinus Torvalds int err; 38051da177e4SLinus Torvalds struct inode_security_struct *isec; 38061da177e4SLinus Torvalds struct inode_security_struct *newisec; 38071da177e4SLinus Torvalds 38081da177e4SLinus Torvalds err = socket_has_perm(current, sock, SOCKET__ACCEPT); 38091da177e4SLinus Torvalds if (err) 38101da177e4SLinus Torvalds return err; 38111da177e4SLinus Torvalds 38121da177e4SLinus Torvalds newisec = SOCK_INODE(newsock)->i_security; 38131da177e4SLinus Torvalds 38141da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 38151da177e4SLinus Torvalds newisec->sclass = isec->sclass; 38161da177e4SLinus Torvalds newisec->sid = isec->sid; 38171da177e4SLinus Torvalds newisec->initialized = 1; 38181da177e4SLinus Torvalds 38191da177e4SLinus Torvalds return 0; 38201da177e4SLinus Torvalds } 38211da177e4SLinus Torvalds 38221da177e4SLinus Torvalds static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg, 38231da177e4SLinus Torvalds int size) 38241da177e4SLinus Torvalds { 38257420ed23SVenkat Yekkirala int rc; 38267420ed23SVenkat Yekkirala 38277420ed23SVenkat Yekkirala rc = socket_has_perm(current, sock, SOCKET__WRITE); 38287420ed23SVenkat Yekkirala if (rc) 38297420ed23SVenkat Yekkirala return rc; 38307420ed23SVenkat Yekkirala 38317420ed23SVenkat Yekkirala return selinux_netlbl_inode_permission(SOCK_INODE(sock), MAY_WRITE); 38321da177e4SLinus Torvalds } 38331da177e4SLinus Torvalds 38341da177e4SLinus Torvalds static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg, 38351da177e4SLinus Torvalds int size, int flags) 38361da177e4SLinus Torvalds { 38371da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__READ); 38381da177e4SLinus Torvalds } 38391da177e4SLinus Torvalds 38401da177e4SLinus Torvalds static int selinux_socket_getsockname(struct socket *sock) 38411da177e4SLinus Torvalds { 38421da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__GETATTR); 38431da177e4SLinus Torvalds } 38441da177e4SLinus Torvalds 38451da177e4SLinus Torvalds static int selinux_socket_getpeername(struct socket *sock) 38461da177e4SLinus Torvalds { 38471da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__GETATTR); 38481da177e4SLinus Torvalds } 38491da177e4SLinus Torvalds 38501da177e4SLinus Torvalds static int selinux_socket_setsockopt(struct socket *sock, int level, int optname) 38511da177e4SLinus Torvalds { 3852f8687afeSPaul Moore int err; 3853f8687afeSPaul Moore 3854f8687afeSPaul Moore err = socket_has_perm(current, sock, SOCKET__SETOPT); 3855f8687afeSPaul Moore if (err) 3856f8687afeSPaul Moore return err; 3857f8687afeSPaul Moore 3858f8687afeSPaul Moore return selinux_netlbl_socket_setsockopt(sock, level, optname); 38591da177e4SLinus Torvalds } 38601da177e4SLinus Torvalds 38611da177e4SLinus Torvalds static int selinux_socket_getsockopt(struct socket *sock, int level, 38621da177e4SLinus Torvalds int optname) 38631da177e4SLinus Torvalds { 38641da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__GETOPT); 38651da177e4SLinus Torvalds } 38661da177e4SLinus Torvalds 38671da177e4SLinus Torvalds static int selinux_socket_shutdown(struct socket *sock, int how) 38681da177e4SLinus Torvalds { 38691da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__SHUTDOWN); 38701da177e4SLinus Torvalds } 38711da177e4SLinus Torvalds 38721da177e4SLinus Torvalds static int selinux_socket_unix_stream_connect(struct socket *sock, 38731da177e4SLinus Torvalds struct socket *other, 38741da177e4SLinus Torvalds struct sock *newsk) 38751da177e4SLinus Torvalds { 38761da177e4SLinus Torvalds struct sk_security_struct *ssec; 38771da177e4SLinus Torvalds struct inode_security_struct *isec; 38781da177e4SLinus Torvalds struct inode_security_struct *other_isec; 38791da177e4SLinus Torvalds struct avc_audit_data ad; 38801da177e4SLinus Torvalds int err; 38811da177e4SLinus Torvalds 38821da177e4SLinus Torvalds err = secondary_ops->unix_stream_connect(sock, other, newsk); 38831da177e4SLinus Torvalds if (err) 38841da177e4SLinus Torvalds return err; 38851da177e4SLinus Torvalds 38861da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 38871da177e4SLinus Torvalds other_isec = SOCK_INODE(other)->i_security; 38881da177e4SLinus Torvalds 38891da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, NET); 38901da177e4SLinus Torvalds ad.u.net.sk = other->sk; 38911da177e4SLinus Torvalds 38921da177e4SLinus Torvalds err = avc_has_perm(isec->sid, other_isec->sid, 38931da177e4SLinus Torvalds isec->sclass, 38941da177e4SLinus Torvalds UNIX_STREAM_SOCKET__CONNECTTO, &ad); 38951da177e4SLinus Torvalds if (err) 38961da177e4SLinus Torvalds return err; 38971da177e4SLinus Torvalds 38981da177e4SLinus Torvalds /* connecting socket */ 38991da177e4SLinus Torvalds ssec = sock->sk->sk_security; 39001da177e4SLinus Torvalds ssec->peer_sid = other_isec->sid; 39011da177e4SLinus Torvalds 39021da177e4SLinus Torvalds /* server child socket */ 39031da177e4SLinus Torvalds ssec = newsk->sk_security; 39041da177e4SLinus Torvalds ssec->peer_sid = isec->sid; 39054237c75cSVenkat Yekkirala err = security_sid_mls_copy(other_isec->sid, ssec->peer_sid, &ssec->sid); 39061da177e4SLinus Torvalds 39074237c75cSVenkat Yekkirala return err; 39081da177e4SLinus Torvalds } 39091da177e4SLinus Torvalds 39101da177e4SLinus Torvalds static int selinux_socket_unix_may_send(struct socket *sock, 39111da177e4SLinus Torvalds struct socket *other) 39121da177e4SLinus Torvalds { 39131da177e4SLinus Torvalds struct inode_security_struct *isec; 39141da177e4SLinus Torvalds struct inode_security_struct *other_isec; 39151da177e4SLinus Torvalds struct avc_audit_data ad; 39161da177e4SLinus Torvalds int err; 39171da177e4SLinus Torvalds 39181da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 39191da177e4SLinus Torvalds other_isec = SOCK_INODE(other)->i_security; 39201da177e4SLinus Torvalds 39211da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, NET); 39221da177e4SLinus Torvalds ad.u.net.sk = other->sk; 39231da177e4SLinus Torvalds 39241da177e4SLinus Torvalds err = avc_has_perm(isec->sid, other_isec->sid, 39251da177e4SLinus Torvalds isec->sclass, SOCKET__SENDTO, &ad); 39261da177e4SLinus Torvalds if (err) 39271da177e4SLinus Torvalds return err; 39281da177e4SLinus Torvalds 39291da177e4SLinus Torvalds return 0; 39301da177e4SLinus Torvalds } 39311da177e4SLinus Torvalds 3932effad8dfSPaul Moore static int selinux_inet_sys_rcv_skb(int ifindex, char *addrp, u16 family, 3933effad8dfSPaul Moore u32 peer_sid, 3934effad8dfSPaul Moore struct avc_audit_data *ad) 3935effad8dfSPaul Moore { 3936effad8dfSPaul Moore int err; 3937effad8dfSPaul Moore u32 if_sid; 3938effad8dfSPaul Moore u32 node_sid; 3939effad8dfSPaul Moore 3940effad8dfSPaul Moore err = sel_netif_sid(ifindex, &if_sid); 3941effad8dfSPaul Moore if (err) 3942effad8dfSPaul Moore return err; 3943effad8dfSPaul Moore err = avc_has_perm(peer_sid, if_sid, 3944effad8dfSPaul Moore SECCLASS_NETIF, NETIF__INGRESS, ad); 3945effad8dfSPaul Moore if (err) 3946effad8dfSPaul Moore return err; 3947effad8dfSPaul Moore 3948effad8dfSPaul Moore err = sel_netnode_sid(addrp, family, &node_sid); 3949effad8dfSPaul Moore if (err) 3950effad8dfSPaul Moore return err; 3951effad8dfSPaul Moore return avc_has_perm(peer_sid, node_sid, 3952effad8dfSPaul Moore SECCLASS_NODE, NODE__RECVFROM, ad); 3953effad8dfSPaul Moore } 3954effad8dfSPaul Moore 3955220deb96SPaul Moore static int selinux_sock_rcv_skb_iptables_compat(struct sock *sk, 3956220deb96SPaul Moore struct sk_buff *skb, 3957224dfbd8SPaul Moore struct avc_audit_data *ad, 3958220deb96SPaul Moore u16 family, 3959220deb96SPaul Moore char *addrp) 39601da177e4SLinus Torvalds { 3961220deb96SPaul Moore int err; 3962220deb96SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 3963220deb96SPaul Moore u16 sk_class; 3964220deb96SPaul Moore u32 netif_perm, node_perm, recv_perm; 3965220deb96SPaul Moore u32 port_sid, node_sid, if_sid, sk_sid; 39664237c75cSVenkat Yekkirala 3967220deb96SPaul Moore sk_sid = sksec->sid; 3968220deb96SPaul Moore sk_class = sksec->sclass; 39691da177e4SLinus Torvalds 3970220deb96SPaul Moore switch (sk_class) { 39711da177e4SLinus Torvalds case SECCLASS_UDP_SOCKET: 39721da177e4SLinus Torvalds netif_perm = NETIF__UDP_RECV; 39731da177e4SLinus Torvalds node_perm = NODE__UDP_RECV; 39741da177e4SLinus Torvalds recv_perm = UDP_SOCKET__RECV_MSG; 39751da177e4SLinus Torvalds break; 39761da177e4SLinus Torvalds case SECCLASS_TCP_SOCKET: 39771da177e4SLinus Torvalds netif_perm = NETIF__TCP_RECV; 39781da177e4SLinus Torvalds node_perm = NODE__TCP_RECV; 39791da177e4SLinus Torvalds recv_perm = TCP_SOCKET__RECV_MSG; 39801da177e4SLinus Torvalds break; 39812ee92d46SJames Morris case SECCLASS_DCCP_SOCKET: 39822ee92d46SJames Morris netif_perm = NETIF__DCCP_RECV; 39832ee92d46SJames Morris node_perm = NODE__DCCP_RECV; 39842ee92d46SJames Morris recv_perm = DCCP_SOCKET__RECV_MSG; 39852ee92d46SJames Morris break; 39861da177e4SLinus Torvalds default: 39871da177e4SLinus Torvalds netif_perm = NETIF__RAWIP_RECV; 39881da177e4SLinus Torvalds node_perm = NODE__RAWIP_RECV; 3989220deb96SPaul Moore recv_perm = 0; 39901da177e4SLinus Torvalds break; 39911da177e4SLinus Torvalds } 39921da177e4SLinus Torvalds 3993220deb96SPaul Moore err = sel_netif_sid(skb->iif, &if_sid); 39941da177e4SLinus Torvalds if (err) 3995220deb96SPaul Moore return err; 3996220deb96SPaul Moore err = avc_has_perm(sk_sid, if_sid, SECCLASS_NETIF, netif_perm, ad); 3997220deb96SPaul Moore if (err) 3998220deb96SPaul Moore return err; 39991da177e4SLinus Torvalds 4000224dfbd8SPaul Moore err = sel_netnode_sid(addrp, family, &node_sid); 40011da177e4SLinus Torvalds if (err) 4002220deb96SPaul Moore return err; 4003220deb96SPaul Moore err = avc_has_perm(sk_sid, node_sid, SECCLASS_NODE, node_perm, ad); 40041da177e4SLinus Torvalds if (err) 4005220deb96SPaul Moore return err; 40061da177e4SLinus Torvalds 4007220deb96SPaul Moore if (!recv_perm) 4008220deb96SPaul Moore return 0; 40093e112172SPaul Moore err = sel_netport_sid(sk->sk_protocol, 40103e112172SPaul Moore ntohs(ad->u.net.sport), &port_sid); 401171f1cb05SPaul Moore if (unlikely(err)) { 401271f1cb05SPaul Moore printk(KERN_WARNING 401371f1cb05SPaul Moore "SELinux: failure in" 401471f1cb05SPaul Moore " selinux_sock_rcv_skb_iptables_compat()," 401571f1cb05SPaul Moore " network port label not found\n"); 4016220deb96SPaul Moore return err; 401771f1cb05SPaul Moore } 4018220deb96SPaul Moore return avc_has_perm(sk_sid, port_sid, sk_class, recv_perm, ad); 40191da177e4SLinus Torvalds } 4020d28d1e08STrent Jaeger 4021220deb96SPaul Moore static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb, 4022220deb96SPaul Moore struct avc_audit_data *ad, 4023220deb96SPaul Moore u16 family, char *addrp) 4024220deb96SPaul Moore { 4025220deb96SPaul Moore int err; 4026220deb96SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 4027220deb96SPaul Moore u32 peer_sid; 4028220deb96SPaul Moore u32 sk_sid = sksec->sid; 4029220deb96SPaul Moore 4030220deb96SPaul Moore if (selinux_compat_net) 4031220deb96SPaul Moore err = selinux_sock_rcv_skb_iptables_compat(sk, skb, ad, 4032220deb96SPaul Moore family, addrp); 4033220deb96SPaul Moore else 4034220deb96SPaul Moore err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET, 4035220deb96SPaul Moore PACKET__RECV, ad); 4036220deb96SPaul Moore if (err) 4037220deb96SPaul Moore return err; 4038220deb96SPaul Moore 4039220deb96SPaul Moore if (selinux_policycap_netpeer) { 4040220deb96SPaul Moore err = selinux_skb_peerlbl_sid(skb, family, &peer_sid); 4041220deb96SPaul Moore if (err) 4042220deb96SPaul Moore return err; 4043220deb96SPaul Moore err = avc_has_perm(sk_sid, peer_sid, 4044220deb96SPaul Moore SECCLASS_PEER, PEER__RECV, ad); 4045220deb96SPaul Moore } else { 4046220deb96SPaul Moore err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, ad); 4047220deb96SPaul Moore if (err) 4048220deb96SPaul Moore return err; 4049220deb96SPaul Moore err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, ad); 4050220deb96SPaul Moore } 4051220deb96SPaul Moore 40524e5ab4cbSJames Morris return err; 40534e5ab4cbSJames Morris } 4054d28d1e08STrent Jaeger 40554e5ab4cbSJames Morris static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) 40564e5ab4cbSJames Morris { 4057220deb96SPaul Moore int err; 40584237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 4059220deb96SPaul Moore u16 family = sk->sk_family; 4060220deb96SPaul Moore u32 sk_sid = sksec->sid; 4061220deb96SPaul Moore struct avc_audit_data ad; 4062220deb96SPaul Moore char *addrp; 40634e5ab4cbSJames Morris 40644e5ab4cbSJames Morris if (family != PF_INET && family != PF_INET6) 4065220deb96SPaul Moore return 0; 40664e5ab4cbSJames Morris 40674e5ab4cbSJames Morris /* Handle mapped IPv4 packets arriving via IPv6 sockets */ 406887fcd70dSAl Viro if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 40694e5ab4cbSJames Morris family = PF_INET; 40704e5ab4cbSJames Morris 40714e5ab4cbSJames Morris AVC_AUDIT_DATA_INIT(&ad, NET); 4072da5645a2SPaul Moore ad.u.net.netif = skb->iif; 40734e5ab4cbSJames Morris ad.u.net.family = family; 4074224dfbd8SPaul Moore err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); 40754e5ab4cbSJames Morris if (err) 4076220deb96SPaul Moore return err; 40774e5ab4cbSJames Morris 4078220deb96SPaul Moore /* If any sort of compatibility mode is enabled then handoff processing 4079220deb96SPaul Moore * to the selinux_sock_rcv_skb_compat() function to deal with the 4080220deb96SPaul Moore * special handling. We do this in an attempt to keep this function 4081220deb96SPaul Moore * as fast and as clean as possible. */ 4082220deb96SPaul Moore if (selinux_compat_net || !selinux_policycap_netpeer) 4083220deb96SPaul Moore return selinux_sock_rcv_skb_compat(sk, skb, &ad, 4084220deb96SPaul Moore family, addrp); 4085220deb96SPaul Moore 4086d621d35eSPaul Moore if (netlbl_enabled() || selinux_xfrm_enabled()) { 4087d621d35eSPaul Moore u32 peer_sid; 4088220deb96SPaul Moore 4089220deb96SPaul Moore err = selinux_skb_peerlbl_sid(skb, family, &peer_sid); 4090220deb96SPaul Moore if (err) 4091220deb96SPaul Moore return err; 4092effad8dfSPaul Moore err = selinux_inet_sys_rcv_skb(skb->iif, addrp, family, 4093effad8dfSPaul Moore peer_sid, &ad); 4094effad8dfSPaul Moore if (err) 4095effad8dfSPaul Moore return err; 4096d621d35eSPaul Moore err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER, 4097d621d35eSPaul Moore PEER__RECV, &ad); 4098d621d35eSPaul Moore } 4099d621d35eSPaul Moore 4100effad8dfSPaul Moore if (selinux_secmark_enabled()) { 4101effad8dfSPaul Moore err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET, 4102effad8dfSPaul Moore PACKET__RECV, &ad); 4103effad8dfSPaul Moore if (err) 4104effad8dfSPaul Moore return err; 4105effad8dfSPaul Moore } 4106effad8dfSPaul Moore 4107d621d35eSPaul Moore return err; 41081da177e4SLinus Torvalds } 41091da177e4SLinus Torvalds 41102c7946a7SCatherine Zhang static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval, 41111da177e4SLinus Torvalds int __user *optlen, unsigned len) 41121da177e4SLinus Torvalds { 41131da177e4SLinus Torvalds int err = 0; 41141da177e4SLinus Torvalds char *scontext; 41151da177e4SLinus Torvalds u32 scontext_len; 41161da177e4SLinus Torvalds struct sk_security_struct *ssec; 41171da177e4SLinus Torvalds struct inode_security_struct *isec; 41183de4bab5SPaul Moore u32 peer_sid = SECSID_NULL; 41191da177e4SLinus Torvalds 41201da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 41212c7946a7SCatherine Zhang 41223de4bab5SPaul Moore if (isec->sclass == SECCLASS_UNIX_STREAM_SOCKET || 41233de4bab5SPaul Moore isec->sclass == SECCLASS_TCP_SOCKET) { 41246b877699SVenkat Yekkirala ssec = sock->sk->sk_security; 41256b877699SVenkat Yekkirala peer_sid = ssec->peer_sid; 41266b877699SVenkat Yekkirala } 41272c7946a7SCatherine Zhang if (peer_sid == SECSID_NULL) { 41282c7946a7SCatherine Zhang err = -ENOPROTOOPT; 41292c7946a7SCatherine Zhang goto out; 41302c7946a7SCatherine Zhang } 41311da177e4SLinus Torvalds 41322c7946a7SCatherine Zhang err = security_sid_to_context(peer_sid, &scontext, &scontext_len); 41331da177e4SLinus Torvalds 41341da177e4SLinus Torvalds if (err) 41351da177e4SLinus Torvalds goto out; 41361da177e4SLinus Torvalds 41371da177e4SLinus Torvalds if (scontext_len > len) { 41381da177e4SLinus Torvalds err = -ERANGE; 41391da177e4SLinus Torvalds goto out_len; 41401da177e4SLinus Torvalds } 41411da177e4SLinus Torvalds 41421da177e4SLinus Torvalds if (copy_to_user(optval, scontext, scontext_len)) 41431da177e4SLinus Torvalds err = -EFAULT; 41441da177e4SLinus Torvalds 41451da177e4SLinus Torvalds out_len: 41461da177e4SLinus Torvalds if (put_user(scontext_len, optlen)) 41471da177e4SLinus Torvalds err = -EFAULT; 41481da177e4SLinus Torvalds 41491da177e4SLinus Torvalds kfree(scontext); 41501da177e4SLinus Torvalds out: 41511da177e4SLinus Torvalds return err; 41521da177e4SLinus Torvalds } 41531da177e4SLinus Torvalds 4154dc49c1f9SCatherine Zhang static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid) 41552c7946a7SCatherine Zhang { 4156dc49c1f9SCatherine Zhang u32 peer_secid = SECSID_NULL; 415775e22910SPaul Moore u16 family; 4158877ce7c1SCatherine Zhang 415975e22910SPaul Moore if (sock) 416075e22910SPaul Moore family = sock->sk->sk_family; 416175e22910SPaul Moore else if (skb && skb->sk) 416275e22910SPaul Moore family = skb->sk->sk_family; 416375e22910SPaul Moore else 416475e22910SPaul Moore goto out; 416575e22910SPaul Moore 416675e22910SPaul Moore if (sock && family == PF_UNIX) 4167713a04aeSAhmed S. Darwish selinux_inode_getsecid(SOCK_INODE(sock), &peer_secid); 41683de4bab5SPaul Moore else if (skb) 4169220deb96SPaul Moore selinux_skb_peerlbl_sid(skb, family, &peer_secid); 41702c7946a7SCatherine Zhang 417175e22910SPaul Moore out: 4172dc49c1f9SCatherine Zhang *secid = peer_secid; 417375e22910SPaul Moore if (peer_secid == SECSID_NULL) 417475e22910SPaul Moore return -EINVAL; 417575e22910SPaul Moore return 0; 41762c7946a7SCatherine Zhang } 41772c7946a7SCatherine Zhang 41787d877f3bSAl Viro static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority) 41791da177e4SLinus Torvalds { 41801da177e4SLinus Torvalds return sk_alloc_security(sk, family, priority); 41811da177e4SLinus Torvalds } 41821da177e4SLinus Torvalds 41831da177e4SLinus Torvalds static void selinux_sk_free_security(struct sock *sk) 41841da177e4SLinus Torvalds { 41851da177e4SLinus Torvalds sk_free_security(sk); 41861da177e4SLinus Torvalds } 41871da177e4SLinus Torvalds 4188892c141eSVenkat Yekkirala static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk) 4189892c141eSVenkat Yekkirala { 4190892c141eSVenkat Yekkirala struct sk_security_struct *ssec = sk->sk_security; 4191892c141eSVenkat Yekkirala struct sk_security_struct *newssec = newsk->sk_security; 4192892c141eSVenkat Yekkirala 4193892c141eSVenkat Yekkirala newssec->sid = ssec->sid; 4194892c141eSVenkat Yekkirala newssec->peer_sid = ssec->peer_sid; 4195220deb96SPaul Moore newssec->sclass = ssec->sclass; 419699f59ed0SPaul Moore 4197f74af6e8SPaul Moore selinux_netlbl_sk_security_reset(newssec, newsk->sk_family); 4198892c141eSVenkat Yekkirala } 4199892c141eSVenkat Yekkirala 4200beb8d13bSVenkat Yekkirala static void selinux_sk_getsecid(struct sock *sk, u32 *secid) 4201d28d1e08STrent Jaeger { 4202d28d1e08STrent Jaeger if (!sk) 4203beb8d13bSVenkat Yekkirala *secid = SECINITSID_ANY_SOCKET; 4204892c141eSVenkat Yekkirala else { 4205892c141eSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 4206d28d1e08STrent Jaeger 4207beb8d13bSVenkat Yekkirala *secid = sksec->sid; 4208892c141eSVenkat Yekkirala } 4209d28d1e08STrent Jaeger } 4210d28d1e08STrent Jaeger 42119a673e56SAdrian Bunk static void selinux_sock_graft(struct sock *sk, struct socket *parent) 42124237c75cSVenkat Yekkirala { 42134237c75cSVenkat Yekkirala struct inode_security_struct *isec = SOCK_INODE(parent)->i_security; 42144237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 42154237c75cSVenkat Yekkirala 42162148ccc4SDavid Woodhouse if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 || 42172148ccc4SDavid Woodhouse sk->sk_family == PF_UNIX) 42184237c75cSVenkat Yekkirala isec->sid = sksec->sid; 4219220deb96SPaul Moore sksec->sclass = isec->sclass; 42207420ed23SVenkat Yekkirala 42217420ed23SVenkat Yekkirala selinux_netlbl_sock_graft(sk, parent); 42224237c75cSVenkat Yekkirala } 42234237c75cSVenkat Yekkirala 42249a673e56SAdrian Bunk static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb, 42254237c75cSVenkat Yekkirala struct request_sock *req) 42264237c75cSVenkat Yekkirala { 42274237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 42284237c75cSVenkat Yekkirala int err; 42297420ed23SVenkat Yekkirala u32 newsid; 42304237c75cSVenkat Yekkirala u32 peersid; 42314237c75cSVenkat Yekkirala 4232220deb96SPaul Moore err = selinux_skb_peerlbl_sid(skb, sk->sk_family, &peersid); 4233220deb96SPaul Moore if (err) 4234220deb96SPaul Moore return err; 4235a51c64f1SVenkat Yekkirala if (peersid == SECSID_NULL) { 4236a51c64f1SVenkat Yekkirala req->secid = sksec->sid; 42373de4bab5SPaul Moore req->peer_secid = SECSID_NULL; 4238a51c64f1SVenkat Yekkirala return 0; 4239a51c64f1SVenkat Yekkirala } 4240a51c64f1SVenkat Yekkirala 42414237c75cSVenkat Yekkirala err = security_sid_mls_copy(sksec->sid, peersid, &newsid); 42424237c75cSVenkat Yekkirala if (err) 42434237c75cSVenkat Yekkirala return err; 42444237c75cSVenkat Yekkirala 42454237c75cSVenkat Yekkirala req->secid = newsid; 42466b877699SVenkat Yekkirala req->peer_secid = peersid; 42474237c75cSVenkat Yekkirala return 0; 42484237c75cSVenkat Yekkirala } 42494237c75cSVenkat Yekkirala 42509a673e56SAdrian Bunk static void selinux_inet_csk_clone(struct sock *newsk, 42519a673e56SAdrian Bunk const struct request_sock *req) 42524237c75cSVenkat Yekkirala { 42534237c75cSVenkat Yekkirala struct sk_security_struct *newsksec = newsk->sk_security; 42544237c75cSVenkat Yekkirala 42554237c75cSVenkat Yekkirala newsksec->sid = req->secid; 42566b877699SVenkat Yekkirala newsksec->peer_sid = req->peer_secid; 42574237c75cSVenkat Yekkirala /* NOTE: Ideally, we should also get the isec->sid for the 42584237c75cSVenkat Yekkirala new socket in sync, but we don't have the isec available yet. 42594237c75cSVenkat Yekkirala So we will wait until sock_graft to do it, by which 42604237c75cSVenkat Yekkirala time it will have been created and available. */ 426199f59ed0SPaul Moore 42629f2ad665SPaul Moore /* We don't need to take any sort of lock here as we are the only 42639f2ad665SPaul Moore * thread with access to newsksec */ 42649f2ad665SPaul Moore selinux_netlbl_sk_security_reset(newsksec, req->rsk_ops->family); 42654237c75cSVenkat Yekkirala } 42664237c75cSVenkat Yekkirala 42676b877699SVenkat Yekkirala static void selinux_inet_conn_established(struct sock *sk, 42686b877699SVenkat Yekkirala struct sk_buff *skb) 42696b877699SVenkat Yekkirala { 42706b877699SVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 42716b877699SVenkat Yekkirala 4272220deb96SPaul Moore selinux_skb_peerlbl_sid(skb, sk->sk_family, &sksec->peer_sid); 42736b877699SVenkat Yekkirala } 42746b877699SVenkat Yekkirala 42759a673e56SAdrian Bunk static void selinux_req_classify_flow(const struct request_sock *req, 42769a673e56SAdrian Bunk struct flowi *fl) 42774237c75cSVenkat Yekkirala { 42784237c75cSVenkat Yekkirala fl->secid = req->secid; 42794237c75cSVenkat Yekkirala } 42804237c75cSVenkat Yekkirala 42811da177e4SLinus Torvalds static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb) 42821da177e4SLinus Torvalds { 42831da177e4SLinus Torvalds int err = 0; 42841da177e4SLinus Torvalds u32 perm; 42851da177e4SLinus Torvalds struct nlmsghdr *nlh; 42861da177e4SLinus Torvalds struct socket *sock = sk->sk_socket; 42871da177e4SLinus Torvalds struct inode_security_struct *isec = SOCK_INODE(sock)->i_security; 42881da177e4SLinus Torvalds 42891da177e4SLinus Torvalds if (skb->len < NLMSG_SPACE(0)) { 42901da177e4SLinus Torvalds err = -EINVAL; 42911da177e4SLinus Torvalds goto out; 42921da177e4SLinus Torvalds } 4293b529ccf2SArnaldo Carvalho de Melo nlh = nlmsg_hdr(skb); 42941da177e4SLinus Torvalds 42951da177e4SLinus Torvalds err = selinux_nlmsg_lookup(isec->sclass, nlh->nlmsg_type, &perm); 42961da177e4SLinus Torvalds if (err) { 42971da177e4SLinus Torvalds if (err == -EINVAL) { 42989ad9ad38SDavid Woodhouse audit_log(current->audit_context, GFP_KERNEL, AUDIT_SELINUX_ERR, 42991da177e4SLinus Torvalds "SELinux: unrecognized netlink message" 43001da177e4SLinus Torvalds " type=%hu for sclass=%hu\n", 43011da177e4SLinus Torvalds nlh->nlmsg_type, isec->sclass); 43021da177e4SLinus Torvalds if (!selinux_enforcing) 43031da177e4SLinus Torvalds err = 0; 43041da177e4SLinus Torvalds } 43051da177e4SLinus Torvalds 43061da177e4SLinus Torvalds /* Ignore */ 43071da177e4SLinus Torvalds if (err == -ENOENT) 43081da177e4SLinus Torvalds err = 0; 43091da177e4SLinus Torvalds goto out; 43101da177e4SLinus Torvalds } 43111da177e4SLinus Torvalds 43121da177e4SLinus Torvalds err = socket_has_perm(current, sock, perm); 43131da177e4SLinus Torvalds out: 43141da177e4SLinus Torvalds return err; 43151da177e4SLinus Torvalds } 43161da177e4SLinus Torvalds 43171da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER 43181da177e4SLinus Torvalds 4319effad8dfSPaul Moore static unsigned int selinux_ip_forward(struct sk_buff *skb, int ifindex, 4320effad8dfSPaul Moore u16 family) 43211da177e4SLinus Torvalds { 4322effad8dfSPaul Moore char *addrp; 4323effad8dfSPaul Moore u32 peer_sid; 4324effad8dfSPaul Moore struct avc_audit_data ad; 4325effad8dfSPaul Moore u8 secmark_active; 4326effad8dfSPaul Moore u8 peerlbl_active; 43274237c75cSVenkat Yekkirala 4328effad8dfSPaul Moore if (!selinux_policycap_netpeer) 4329effad8dfSPaul Moore return NF_ACCEPT; 43304237c75cSVenkat Yekkirala 4331effad8dfSPaul Moore secmark_active = selinux_secmark_enabled(); 4332effad8dfSPaul Moore peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled(); 4333effad8dfSPaul Moore if (!secmark_active && !peerlbl_active) 4334effad8dfSPaul Moore return NF_ACCEPT; 43354237c75cSVenkat Yekkirala 4336effad8dfSPaul Moore AVC_AUDIT_DATA_INIT(&ad, NET); 4337effad8dfSPaul Moore ad.u.net.netif = ifindex; 4338effad8dfSPaul Moore ad.u.net.family = family; 4339effad8dfSPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0) 4340effad8dfSPaul Moore return NF_DROP; 43411da177e4SLinus Torvalds 4342effad8dfSPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0) 4343effad8dfSPaul Moore return NF_DROP; 43441da177e4SLinus Torvalds 4345effad8dfSPaul Moore if (peerlbl_active) 4346effad8dfSPaul Moore if (selinux_inet_sys_rcv_skb(ifindex, addrp, family, 4347effad8dfSPaul Moore peer_sid, &ad) != 0) 4348effad8dfSPaul Moore return NF_DROP; 4349effad8dfSPaul Moore 4350effad8dfSPaul Moore if (secmark_active) 4351effad8dfSPaul Moore if (avc_has_perm(peer_sid, skb->secmark, 4352effad8dfSPaul Moore SECCLASS_PACKET, PACKET__FORWARD_IN, &ad)) 4353effad8dfSPaul Moore return NF_DROP; 4354effad8dfSPaul Moore 4355effad8dfSPaul Moore return NF_ACCEPT; 4356effad8dfSPaul Moore } 4357effad8dfSPaul Moore 4358effad8dfSPaul Moore static unsigned int selinux_ipv4_forward(unsigned int hooknum, 4359effad8dfSPaul Moore struct sk_buff *skb, 4360effad8dfSPaul Moore const struct net_device *in, 4361effad8dfSPaul Moore const struct net_device *out, 4362effad8dfSPaul Moore int (*okfn)(struct sk_buff *)) 4363effad8dfSPaul Moore { 4364effad8dfSPaul Moore return selinux_ip_forward(skb, in->ifindex, PF_INET); 4365effad8dfSPaul Moore } 4366effad8dfSPaul Moore 4367effad8dfSPaul Moore #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 4368effad8dfSPaul Moore static unsigned int selinux_ipv6_forward(unsigned int hooknum, 4369effad8dfSPaul Moore struct sk_buff *skb, 4370effad8dfSPaul Moore const struct net_device *in, 4371effad8dfSPaul Moore const struct net_device *out, 4372effad8dfSPaul Moore int (*okfn)(struct sk_buff *)) 4373effad8dfSPaul Moore { 4374effad8dfSPaul Moore return selinux_ip_forward(skb, in->ifindex, PF_INET6); 4375effad8dfSPaul Moore } 4376effad8dfSPaul Moore #endif /* IPV6 */ 4377effad8dfSPaul Moore 4378effad8dfSPaul Moore static int selinux_ip_postroute_iptables_compat(struct sock *sk, 4379effad8dfSPaul Moore int ifindex, 4380effad8dfSPaul Moore struct avc_audit_data *ad, 4381effad8dfSPaul Moore u16 family, char *addrp) 4382effad8dfSPaul Moore { 4383effad8dfSPaul Moore int err; 4384effad8dfSPaul Moore struct sk_security_struct *sksec = sk->sk_security; 4385effad8dfSPaul Moore u16 sk_class; 4386effad8dfSPaul Moore u32 netif_perm, node_perm, send_perm; 4387effad8dfSPaul Moore u32 port_sid, node_sid, if_sid, sk_sid; 4388effad8dfSPaul Moore 4389effad8dfSPaul Moore sk_sid = sksec->sid; 4390effad8dfSPaul Moore sk_class = sksec->sclass; 4391effad8dfSPaul Moore 4392effad8dfSPaul Moore switch (sk_class) { 43931da177e4SLinus Torvalds case SECCLASS_UDP_SOCKET: 43941da177e4SLinus Torvalds netif_perm = NETIF__UDP_SEND; 43951da177e4SLinus Torvalds node_perm = NODE__UDP_SEND; 43961da177e4SLinus Torvalds send_perm = UDP_SOCKET__SEND_MSG; 43971da177e4SLinus Torvalds break; 43981da177e4SLinus Torvalds case SECCLASS_TCP_SOCKET: 43991da177e4SLinus Torvalds netif_perm = NETIF__TCP_SEND; 44001da177e4SLinus Torvalds node_perm = NODE__TCP_SEND; 44011da177e4SLinus Torvalds send_perm = TCP_SOCKET__SEND_MSG; 44021da177e4SLinus Torvalds break; 44032ee92d46SJames Morris case SECCLASS_DCCP_SOCKET: 44042ee92d46SJames Morris netif_perm = NETIF__DCCP_SEND; 44052ee92d46SJames Morris node_perm = NODE__DCCP_SEND; 44062ee92d46SJames Morris send_perm = DCCP_SOCKET__SEND_MSG; 44072ee92d46SJames Morris break; 44081da177e4SLinus Torvalds default: 44091da177e4SLinus Torvalds netif_perm = NETIF__RAWIP_SEND; 44101da177e4SLinus Torvalds node_perm = NODE__RAWIP_SEND; 4411effad8dfSPaul Moore send_perm = 0; 44121da177e4SLinus Torvalds break; 44131da177e4SLinus Torvalds } 44141da177e4SLinus Torvalds 4415effad8dfSPaul Moore err = sel_netif_sid(ifindex, &if_sid); 44164e5ab4cbSJames Morris if (err) 4417effad8dfSPaul Moore return err; 4418effad8dfSPaul Moore err = avc_has_perm(sk_sid, if_sid, SECCLASS_NETIF, netif_perm, ad); 4419effad8dfSPaul Moore return err; 44201da177e4SLinus Torvalds 4421224dfbd8SPaul Moore err = sel_netnode_sid(addrp, family, &node_sid); 44224e5ab4cbSJames Morris if (err) 4423effad8dfSPaul Moore return err; 4424effad8dfSPaul Moore err = avc_has_perm(sk_sid, node_sid, SECCLASS_NODE, node_perm, ad); 44254e5ab4cbSJames Morris if (err) 4426effad8dfSPaul Moore return err; 44271da177e4SLinus Torvalds 4428effad8dfSPaul Moore if (send_perm != 0) 4429effad8dfSPaul Moore return 0; 44301da177e4SLinus Torvalds 44313e112172SPaul Moore err = sel_netport_sid(sk->sk_protocol, 44323e112172SPaul Moore ntohs(ad->u.net.dport), &port_sid); 443371f1cb05SPaul Moore if (unlikely(err)) { 443471f1cb05SPaul Moore printk(KERN_WARNING 443571f1cb05SPaul Moore "SELinux: failure in" 443671f1cb05SPaul Moore " selinux_ip_postroute_iptables_compat()," 443771f1cb05SPaul Moore " network port label not found\n"); 44384e5ab4cbSJames Morris return err; 443971f1cb05SPaul Moore } 4440effad8dfSPaul Moore return avc_has_perm(sk_sid, port_sid, sk_class, send_perm, ad); 44411da177e4SLinus Torvalds } 44421da177e4SLinus Torvalds 4443effad8dfSPaul Moore static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb, 4444effad8dfSPaul Moore int ifindex, 4445effad8dfSPaul Moore struct avc_audit_data *ad, 4446effad8dfSPaul Moore u16 family, 4447effad8dfSPaul Moore char *addrp, 4448effad8dfSPaul Moore u8 proto) 44494e5ab4cbSJames Morris { 4450effad8dfSPaul Moore struct sock *sk = skb->sk; 44514237c75cSVenkat Yekkirala struct sk_security_struct *sksec; 44524e5ab4cbSJames Morris 4453effad8dfSPaul Moore if (sk == NULL) 4454effad8dfSPaul Moore return NF_ACCEPT; 44554237c75cSVenkat Yekkirala sksec = sk->sk_security; 44564e5ab4cbSJames Morris 4457effad8dfSPaul Moore if (selinux_compat_net) { 4458effad8dfSPaul Moore if (selinux_ip_postroute_iptables_compat(skb->sk, ifindex, 4459effad8dfSPaul Moore ad, family, addrp)) 4460effad8dfSPaul Moore return NF_DROP; 4461effad8dfSPaul Moore } else { 4462effad8dfSPaul Moore if (avc_has_perm(sksec->sid, skb->secmark, 4463effad8dfSPaul Moore SECCLASS_PACKET, PACKET__SEND, ad)) 4464effad8dfSPaul Moore return NF_DROP; 44651da177e4SLinus Torvalds } 44661da177e4SLinus Torvalds 4467effad8dfSPaul Moore if (selinux_policycap_netpeer) 4468effad8dfSPaul Moore if (selinux_xfrm_postroute_last(sksec->sid, skb, ad, proto)) 4469effad8dfSPaul Moore return NF_DROP; 4470effad8dfSPaul Moore 4471effad8dfSPaul Moore return NF_ACCEPT; 4472effad8dfSPaul Moore } 4473effad8dfSPaul Moore 4474effad8dfSPaul Moore static unsigned int selinux_ip_postroute(struct sk_buff *skb, int ifindex, 4475effad8dfSPaul Moore u16 family) 4476effad8dfSPaul Moore { 4477effad8dfSPaul Moore u32 secmark_perm; 4478effad8dfSPaul Moore u32 peer_sid; 4479effad8dfSPaul Moore struct sock *sk; 4480effad8dfSPaul Moore struct avc_audit_data ad; 4481effad8dfSPaul Moore char *addrp; 4482effad8dfSPaul Moore u8 proto; 4483effad8dfSPaul Moore u8 secmark_active; 4484effad8dfSPaul Moore u8 peerlbl_active; 4485effad8dfSPaul Moore 4486effad8dfSPaul Moore AVC_AUDIT_DATA_INIT(&ad, NET); 4487effad8dfSPaul Moore ad.u.net.netif = ifindex; 4488effad8dfSPaul Moore ad.u.net.family = family; 4489effad8dfSPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto)) 4490effad8dfSPaul Moore return NF_DROP; 4491effad8dfSPaul Moore 4492effad8dfSPaul Moore /* If any sort of compatibility mode is enabled then handoff processing 4493effad8dfSPaul Moore * to the selinux_ip_postroute_compat() function to deal with the 4494effad8dfSPaul Moore * special handling. We do this in an attempt to keep this function 4495effad8dfSPaul Moore * as fast and as clean as possible. */ 4496effad8dfSPaul Moore if (selinux_compat_net || !selinux_policycap_netpeer) 4497effad8dfSPaul Moore return selinux_ip_postroute_compat(skb, ifindex, &ad, 4498effad8dfSPaul Moore family, addrp, proto); 4499effad8dfSPaul Moore 4500effad8dfSPaul Moore /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec 4501effad8dfSPaul Moore * packet transformation so allow the packet to pass without any checks 4502effad8dfSPaul Moore * since we'll have another chance to perform access control checks 4503effad8dfSPaul Moore * when the packet is on it's final way out. 4504effad8dfSPaul Moore * NOTE: there appear to be some IPv6 multicast cases where skb->dst 4505effad8dfSPaul Moore * is NULL, in this case go ahead and apply access control. */ 4506effad8dfSPaul Moore if (skb->dst != NULL && skb->dst->xfrm != NULL) 4507effad8dfSPaul Moore return NF_ACCEPT; 4508effad8dfSPaul Moore 4509effad8dfSPaul Moore secmark_active = selinux_secmark_enabled(); 4510effad8dfSPaul Moore peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled(); 4511effad8dfSPaul Moore if (!secmark_active && !peerlbl_active) 4512effad8dfSPaul Moore return NF_ACCEPT; 4513effad8dfSPaul Moore 4514effad8dfSPaul Moore /* if the packet is locally generated (skb->sk != NULL) then use the 4515effad8dfSPaul Moore * socket's label as the peer label, otherwise the packet is being 4516effad8dfSPaul Moore * forwarded through this system and we need to fetch the peer label 4517effad8dfSPaul Moore * directly from the packet */ 4518effad8dfSPaul Moore sk = skb->sk; 4519effad8dfSPaul Moore if (sk) { 4520effad8dfSPaul Moore struct sk_security_struct *sksec = sk->sk_security; 4521effad8dfSPaul Moore peer_sid = sksec->sid; 4522effad8dfSPaul Moore secmark_perm = PACKET__SEND; 4523effad8dfSPaul Moore } else { 4524effad8dfSPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &peer_sid)) 4525effad8dfSPaul Moore return NF_DROP; 4526effad8dfSPaul Moore secmark_perm = PACKET__FORWARD_OUT; 4527effad8dfSPaul Moore } 4528effad8dfSPaul Moore 4529effad8dfSPaul Moore if (secmark_active) 4530effad8dfSPaul Moore if (avc_has_perm(peer_sid, skb->secmark, 4531effad8dfSPaul Moore SECCLASS_PACKET, secmark_perm, &ad)) 4532effad8dfSPaul Moore return NF_DROP; 4533effad8dfSPaul Moore 4534effad8dfSPaul Moore if (peerlbl_active) { 4535effad8dfSPaul Moore u32 if_sid; 4536effad8dfSPaul Moore u32 node_sid; 4537effad8dfSPaul Moore 4538effad8dfSPaul Moore if (sel_netif_sid(ifindex, &if_sid)) 4539effad8dfSPaul Moore return NF_DROP; 4540effad8dfSPaul Moore if (avc_has_perm(peer_sid, if_sid, 4541effad8dfSPaul Moore SECCLASS_NETIF, NETIF__EGRESS, &ad)) 4542effad8dfSPaul Moore return NF_DROP; 4543effad8dfSPaul Moore 4544effad8dfSPaul Moore if (sel_netnode_sid(addrp, family, &node_sid)) 4545effad8dfSPaul Moore return NF_DROP; 4546effad8dfSPaul Moore if (avc_has_perm(peer_sid, node_sid, 4547effad8dfSPaul Moore SECCLASS_NODE, NODE__SENDTO, &ad)) 4548effad8dfSPaul Moore return NF_DROP; 4549effad8dfSPaul Moore } 4550effad8dfSPaul Moore 4551effad8dfSPaul Moore return NF_ACCEPT; 4552effad8dfSPaul Moore } 4553effad8dfSPaul Moore 4554effad8dfSPaul Moore static unsigned int selinux_ipv4_postroute(unsigned int hooknum, 4555a224be76SDavid S. Miller struct sk_buff *skb, 45561da177e4SLinus Torvalds const struct net_device *in, 45571da177e4SLinus Torvalds const struct net_device *out, 45581da177e4SLinus Torvalds int (*okfn)(struct sk_buff *)) 45591da177e4SLinus Torvalds { 4560effad8dfSPaul Moore return selinux_ip_postroute(skb, out->ifindex, PF_INET); 45611da177e4SLinus Torvalds } 45621da177e4SLinus Torvalds 45631da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 4564effad8dfSPaul Moore static unsigned int selinux_ipv6_postroute(unsigned int hooknum, 4565a224be76SDavid S. Miller struct sk_buff *skb, 45661da177e4SLinus Torvalds const struct net_device *in, 45671da177e4SLinus Torvalds const struct net_device *out, 45681da177e4SLinus Torvalds int (*okfn)(struct sk_buff *)) 45691da177e4SLinus Torvalds { 4570effad8dfSPaul Moore return selinux_ip_postroute(skb, out->ifindex, PF_INET6); 45711da177e4SLinus Torvalds } 45721da177e4SLinus Torvalds #endif /* IPV6 */ 45731da177e4SLinus Torvalds 45741da177e4SLinus Torvalds #endif /* CONFIG_NETFILTER */ 45751da177e4SLinus Torvalds 45761da177e4SLinus Torvalds static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb) 45771da177e4SLinus Torvalds { 45781da177e4SLinus Torvalds int err; 45791da177e4SLinus Torvalds 45801da177e4SLinus Torvalds err = secondary_ops->netlink_send(sk, skb); 45811da177e4SLinus Torvalds if (err) 45821da177e4SLinus Torvalds return err; 45831da177e4SLinus Torvalds 45841da177e4SLinus Torvalds if (policydb_loaded_version >= POLICYDB_VERSION_NLCLASS) 45851da177e4SLinus Torvalds err = selinux_nlmsg_perm(sk, skb); 45861da177e4SLinus Torvalds 45871da177e4SLinus Torvalds return err; 45881da177e4SLinus Torvalds } 45891da177e4SLinus Torvalds 4590c7bdb545SDarrel Goeddel static int selinux_netlink_recv(struct sk_buff *skb, int capability) 45911da177e4SLinus Torvalds { 4592c7bdb545SDarrel Goeddel int err; 4593c7bdb545SDarrel Goeddel struct avc_audit_data ad; 4594c7bdb545SDarrel Goeddel 4595c7bdb545SDarrel Goeddel err = secondary_ops->netlink_recv(skb, capability); 4596c7bdb545SDarrel Goeddel if (err) 4597c7bdb545SDarrel Goeddel return err; 4598c7bdb545SDarrel Goeddel 4599c7bdb545SDarrel Goeddel AVC_AUDIT_DATA_INIT(&ad, CAP); 4600c7bdb545SDarrel Goeddel ad.u.cap = capability; 4601c7bdb545SDarrel Goeddel 4602c7bdb545SDarrel Goeddel return avc_has_perm(NETLINK_CB(skb).sid, NETLINK_CB(skb).sid, 4603c7bdb545SDarrel Goeddel SECCLASS_CAPABILITY, CAP_TO_MASK(capability), &ad); 46041da177e4SLinus Torvalds } 46051da177e4SLinus Torvalds 46061da177e4SLinus Torvalds static int ipc_alloc_security(struct task_struct *task, 46071da177e4SLinus Torvalds struct kern_ipc_perm *perm, 46081da177e4SLinus Torvalds u16 sclass) 46091da177e4SLinus Torvalds { 46101da177e4SLinus Torvalds struct task_security_struct *tsec = task->security; 46111da177e4SLinus Torvalds struct ipc_security_struct *isec; 46121da177e4SLinus Torvalds 461389d155efSJames Morris isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL); 46141da177e4SLinus Torvalds if (!isec) 46151da177e4SLinus Torvalds return -ENOMEM; 46161da177e4SLinus Torvalds 46171da177e4SLinus Torvalds isec->sclass = sclass; 46181da177e4SLinus Torvalds isec->sid = tsec->sid; 46191da177e4SLinus Torvalds perm->security = isec; 46201da177e4SLinus Torvalds 46211da177e4SLinus Torvalds return 0; 46221da177e4SLinus Torvalds } 46231da177e4SLinus Torvalds 46241da177e4SLinus Torvalds static void ipc_free_security(struct kern_ipc_perm *perm) 46251da177e4SLinus Torvalds { 46261da177e4SLinus Torvalds struct ipc_security_struct *isec = perm->security; 46271da177e4SLinus Torvalds perm->security = NULL; 46281da177e4SLinus Torvalds kfree(isec); 46291da177e4SLinus Torvalds } 46301da177e4SLinus Torvalds 46311da177e4SLinus Torvalds static int msg_msg_alloc_security(struct msg_msg *msg) 46321da177e4SLinus Torvalds { 46331da177e4SLinus Torvalds struct msg_security_struct *msec; 46341da177e4SLinus Torvalds 463589d155efSJames Morris msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL); 46361da177e4SLinus Torvalds if (!msec) 46371da177e4SLinus Torvalds return -ENOMEM; 46381da177e4SLinus Torvalds 46391da177e4SLinus Torvalds msec->sid = SECINITSID_UNLABELED; 46401da177e4SLinus Torvalds msg->security = msec; 46411da177e4SLinus Torvalds 46421da177e4SLinus Torvalds return 0; 46431da177e4SLinus Torvalds } 46441da177e4SLinus Torvalds 46451da177e4SLinus Torvalds static void msg_msg_free_security(struct msg_msg *msg) 46461da177e4SLinus Torvalds { 46471da177e4SLinus Torvalds struct msg_security_struct *msec = msg->security; 46481da177e4SLinus Torvalds 46491da177e4SLinus Torvalds msg->security = NULL; 46501da177e4SLinus Torvalds kfree(msec); 46511da177e4SLinus Torvalds } 46521da177e4SLinus Torvalds 46531da177e4SLinus Torvalds static int ipc_has_perm(struct kern_ipc_perm *ipc_perms, 46546af963f1SStephen Smalley u32 perms) 46551da177e4SLinus Torvalds { 46561da177e4SLinus Torvalds struct task_security_struct *tsec; 46571da177e4SLinus Torvalds struct ipc_security_struct *isec; 46581da177e4SLinus Torvalds struct avc_audit_data ad; 46591da177e4SLinus Torvalds 46601da177e4SLinus Torvalds tsec = current->security; 46611da177e4SLinus Torvalds isec = ipc_perms->security; 46621da177e4SLinus Torvalds 46631da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 46641da177e4SLinus Torvalds ad.u.ipc_id = ipc_perms->key; 46651da177e4SLinus Torvalds 46666af963f1SStephen Smalley return avc_has_perm(tsec->sid, isec->sid, isec->sclass, perms, &ad); 46671da177e4SLinus Torvalds } 46681da177e4SLinus Torvalds 46691da177e4SLinus Torvalds static int selinux_msg_msg_alloc_security(struct msg_msg *msg) 46701da177e4SLinus Torvalds { 46711da177e4SLinus Torvalds return msg_msg_alloc_security(msg); 46721da177e4SLinus Torvalds } 46731da177e4SLinus Torvalds 46741da177e4SLinus Torvalds static void selinux_msg_msg_free_security(struct msg_msg *msg) 46751da177e4SLinus Torvalds { 46761da177e4SLinus Torvalds msg_msg_free_security(msg); 46771da177e4SLinus Torvalds } 46781da177e4SLinus Torvalds 46791da177e4SLinus Torvalds /* message queue security operations */ 46801da177e4SLinus Torvalds static int selinux_msg_queue_alloc_security(struct msg_queue *msq) 46811da177e4SLinus Torvalds { 46821da177e4SLinus Torvalds struct task_security_struct *tsec; 46831da177e4SLinus Torvalds struct ipc_security_struct *isec; 46841da177e4SLinus Torvalds struct avc_audit_data ad; 46851da177e4SLinus Torvalds int rc; 46861da177e4SLinus Torvalds 46871da177e4SLinus Torvalds rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ); 46881da177e4SLinus Torvalds if (rc) 46891da177e4SLinus Torvalds return rc; 46901da177e4SLinus Torvalds 46911da177e4SLinus Torvalds tsec = current->security; 46921da177e4SLinus Torvalds isec = msq->q_perm.security; 46931da177e4SLinus Torvalds 46941da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 46951da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 46961da177e4SLinus Torvalds 46971da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_MSGQ, 46981da177e4SLinus Torvalds MSGQ__CREATE, &ad); 46991da177e4SLinus Torvalds if (rc) { 47001da177e4SLinus Torvalds ipc_free_security(&msq->q_perm); 47011da177e4SLinus Torvalds return rc; 47021da177e4SLinus Torvalds } 47031da177e4SLinus Torvalds return 0; 47041da177e4SLinus Torvalds } 47051da177e4SLinus Torvalds 47061da177e4SLinus Torvalds static void selinux_msg_queue_free_security(struct msg_queue *msq) 47071da177e4SLinus Torvalds { 47081da177e4SLinus Torvalds ipc_free_security(&msq->q_perm); 47091da177e4SLinus Torvalds } 47101da177e4SLinus Torvalds 47111da177e4SLinus Torvalds static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg) 47121da177e4SLinus Torvalds { 47131da177e4SLinus Torvalds struct task_security_struct *tsec; 47141da177e4SLinus Torvalds struct ipc_security_struct *isec; 47151da177e4SLinus Torvalds struct avc_audit_data ad; 47161da177e4SLinus Torvalds 47171da177e4SLinus Torvalds tsec = current->security; 47181da177e4SLinus Torvalds isec = msq->q_perm.security; 47191da177e4SLinus Torvalds 47201da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 47211da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 47221da177e4SLinus Torvalds 47231da177e4SLinus Torvalds return avc_has_perm(tsec->sid, isec->sid, SECCLASS_MSGQ, 47241da177e4SLinus Torvalds MSGQ__ASSOCIATE, &ad); 47251da177e4SLinus Torvalds } 47261da177e4SLinus Torvalds 47271da177e4SLinus Torvalds static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd) 47281da177e4SLinus Torvalds { 47291da177e4SLinus Torvalds int err; 47301da177e4SLinus Torvalds int perms; 47311da177e4SLinus Torvalds 47321da177e4SLinus Torvalds switch (cmd) { 47331da177e4SLinus Torvalds case IPC_INFO: 47341da177e4SLinus Torvalds case MSG_INFO: 47351da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 47361da177e4SLinus Torvalds return task_has_system(current, SYSTEM__IPC_INFO); 47371da177e4SLinus Torvalds case IPC_STAT: 47381da177e4SLinus Torvalds case MSG_STAT: 47391da177e4SLinus Torvalds perms = MSGQ__GETATTR | MSGQ__ASSOCIATE; 47401da177e4SLinus Torvalds break; 47411da177e4SLinus Torvalds case IPC_SET: 47421da177e4SLinus Torvalds perms = MSGQ__SETATTR; 47431da177e4SLinus Torvalds break; 47441da177e4SLinus Torvalds case IPC_RMID: 47451da177e4SLinus Torvalds perms = MSGQ__DESTROY; 47461da177e4SLinus Torvalds break; 47471da177e4SLinus Torvalds default: 47481da177e4SLinus Torvalds return 0; 47491da177e4SLinus Torvalds } 47501da177e4SLinus Torvalds 47516af963f1SStephen Smalley err = ipc_has_perm(&msq->q_perm, perms); 47521da177e4SLinus Torvalds return err; 47531da177e4SLinus Torvalds } 47541da177e4SLinus Torvalds 47551da177e4SLinus Torvalds static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg) 47561da177e4SLinus Torvalds { 47571da177e4SLinus Torvalds struct task_security_struct *tsec; 47581da177e4SLinus Torvalds struct ipc_security_struct *isec; 47591da177e4SLinus Torvalds struct msg_security_struct *msec; 47601da177e4SLinus Torvalds struct avc_audit_data ad; 47611da177e4SLinus Torvalds int rc; 47621da177e4SLinus Torvalds 47631da177e4SLinus Torvalds tsec = current->security; 47641da177e4SLinus Torvalds isec = msq->q_perm.security; 47651da177e4SLinus Torvalds msec = msg->security; 47661da177e4SLinus Torvalds 47671da177e4SLinus Torvalds /* 47681da177e4SLinus Torvalds * First time through, need to assign label to the message 47691da177e4SLinus Torvalds */ 47701da177e4SLinus Torvalds if (msec->sid == SECINITSID_UNLABELED) { 47711da177e4SLinus Torvalds /* 47721da177e4SLinus Torvalds * Compute new sid based on current process and 47731da177e4SLinus Torvalds * message queue this message will be stored in 47741da177e4SLinus Torvalds */ 47751da177e4SLinus Torvalds rc = security_transition_sid(tsec->sid, 47761da177e4SLinus Torvalds isec->sid, 47771da177e4SLinus Torvalds SECCLASS_MSG, 47781da177e4SLinus Torvalds &msec->sid); 47791da177e4SLinus Torvalds if (rc) 47801da177e4SLinus Torvalds return rc; 47811da177e4SLinus Torvalds } 47821da177e4SLinus Torvalds 47831da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 47841da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 47851da177e4SLinus Torvalds 47861da177e4SLinus Torvalds /* Can this process write to the queue? */ 47871da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_MSGQ, 47881da177e4SLinus Torvalds MSGQ__WRITE, &ad); 47891da177e4SLinus Torvalds if (!rc) 47901da177e4SLinus Torvalds /* Can this process send the message */ 47911da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, msec->sid, 47921da177e4SLinus Torvalds SECCLASS_MSG, MSG__SEND, &ad); 47931da177e4SLinus Torvalds if (!rc) 47941da177e4SLinus Torvalds /* Can the message be put in the queue? */ 47951da177e4SLinus Torvalds rc = avc_has_perm(msec->sid, isec->sid, 47961da177e4SLinus Torvalds SECCLASS_MSGQ, MSGQ__ENQUEUE, &ad); 47971da177e4SLinus Torvalds 47981da177e4SLinus Torvalds return rc; 47991da177e4SLinus Torvalds } 48001da177e4SLinus Torvalds 48011da177e4SLinus Torvalds static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg, 48021da177e4SLinus Torvalds struct task_struct *target, 48031da177e4SLinus Torvalds long type, int mode) 48041da177e4SLinus Torvalds { 48051da177e4SLinus Torvalds struct task_security_struct *tsec; 48061da177e4SLinus Torvalds struct ipc_security_struct *isec; 48071da177e4SLinus Torvalds struct msg_security_struct *msec; 48081da177e4SLinus Torvalds struct avc_audit_data ad; 48091da177e4SLinus Torvalds int rc; 48101da177e4SLinus Torvalds 48111da177e4SLinus Torvalds tsec = target->security; 48121da177e4SLinus Torvalds isec = msq->q_perm.security; 48131da177e4SLinus Torvalds msec = msg->security; 48141da177e4SLinus Torvalds 48151da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 48161da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 48171da177e4SLinus Torvalds 48181da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, 48191da177e4SLinus Torvalds SECCLASS_MSGQ, MSGQ__READ, &ad); 48201da177e4SLinus Torvalds if (!rc) 48211da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, msec->sid, 48221da177e4SLinus Torvalds SECCLASS_MSG, MSG__RECEIVE, &ad); 48231da177e4SLinus Torvalds return rc; 48241da177e4SLinus Torvalds } 48251da177e4SLinus Torvalds 48261da177e4SLinus Torvalds /* Shared Memory security operations */ 48271da177e4SLinus Torvalds static int selinux_shm_alloc_security(struct shmid_kernel *shp) 48281da177e4SLinus Torvalds { 48291da177e4SLinus Torvalds struct task_security_struct *tsec; 48301da177e4SLinus Torvalds struct ipc_security_struct *isec; 48311da177e4SLinus Torvalds struct avc_audit_data ad; 48321da177e4SLinus Torvalds int rc; 48331da177e4SLinus Torvalds 48341da177e4SLinus Torvalds rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM); 48351da177e4SLinus Torvalds if (rc) 48361da177e4SLinus Torvalds return rc; 48371da177e4SLinus Torvalds 48381da177e4SLinus Torvalds tsec = current->security; 48391da177e4SLinus Torvalds isec = shp->shm_perm.security; 48401da177e4SLinus Torvalds 48411da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 48421da177e4SLinus Torvalds ad.u.ipc_id = shp->shm_perm.key; 48431da177e4SLinus Torvalds 48441da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_SHM, 48451da177e4SLinus Torvalds SHM__CREATE, &ad); 48461da177e4SLinus Torvalds if (rc) { 48471da177e4SLinus Torvalds ipc_free_security(&shp->shm_perm); 48481da177e4SLinus Torvalds return rc; 48491da177e4SLinus Torvalds } 48501da177e4SLinus Torvalds return 0; 48511da177e4SLinus Torvalds } 48521da177e4SLinus Torvalds 48531da177e4SLinus Torvalds static void selinux_shm_free_security(struct shmid_kernel *shp) 48541da177e4SLinus Torvalds { 48551da177e4SLinus Torvalds ipc_free_security(&shp->shm_perm); 48561da177e4SLinus Torvalds } 48571da177e4SLinus Torvalds 48581da177e4SLinus Torvalds static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg) 48591da177e4SLinus Torvalds { 48601da177e4SLinus Torvalds struct task_security_struct *tsec; 48611da177e4SLinus Torvalds struct ipc_security_struct *isec; 48621da177e4SLinus Torvalds struct avc_audit_data ad; 48631da177e4SLinus Torvalds 48641da177e4SLinus Torvalds tsec = current->security; 48651da177e4SLinus Torvalds isec = shp->shm_perm.security; 48661da177e4SLinus Torvalds 48671da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 48681da177e4SLinus Torvalds ad.u.ipc_id = shp->shm_perm.key; 48691da177e4SLinus Torvalds 48701da177e4SLinus Torvalds return avc_has_perm(tsec->sid, isec->sid, SECCLASS_SHM, 48711da177e4SLinus Torvalds SHM__ASSOCIATE, &ad); 48721da177e4SLinus Torvalds } 48731da177e4SLinus Torvalds 48741da177e4SLinus Torvalds /* Note, at this point, shp is locked down */ 48751da177e4SLinus Torvalds static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd) 48761da177e4SLinus Torvalds { 48771da177e4SLinus Torvalds int perms; 48781da177e4SLinus Torvalds int err; 48791da177e4SLinus Torvalds 48801da177e4SLinus Torvalds switch (cmd) { 48811da177e4SLinus Torvalds case IPC_INFO: 48821da177e4SLinus Torvalds case SHM_INFO: 48831da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 48841da177e4SLinus Torvalds return task_has_system(current, SYSTEM__IPC_INFO); 48851da177e4SLinus Torvalds case IPC_STAT: 48861da177e4SLinus Torvalds case SHM_STAT: 48871da177e4SLinus Torvalds perms = SHM__GETATTR | SHM__ASSOCIATE; 48881da177e4SLinus Torvalds break; 48891da177e4SLinus Torvalds case IPC_SET: 48901da177e4SLinus Torvalds perms = SHM__SETATTR; 48911da177e4SLinus Torvalds break; 48921da177e4SLinus Torvalds case SHM_LOCK: 48931da177e4SLinus Torvalds case SHM_UNLOCK: 48941da177e4SLinus Torvalds perms = SHM__LOCK; 48951da177e4SLinus Torvalds break; 48961da177e4SLinus Torvalds case IPC_RMID: 48971da177e4SLinus Torvalds perms = SHM__DESTROY; 48981da177e4SLinus Torvalds break; 48991da177e4SLinus Torvalds default: 49001da177e4SLinus Torvalds return 0; 49011da177e4SLinus Torvalds } 49021da177e4SLinus Torvalds 49036af963f1SStephen Smalley err = ipc_has_perm(&shp->shm_perm, perms); 49041da177e4SLinus Torvalds return err; 49051da177e4SLinus Torvalds } 49061da177e4SLinus Torvalds 49071da177e4SLinus Torvalds static int selinux_shm_shmat(struct shmid_kernel *shp, 49081da177e4SLinus Torvalds char __user *shmaddr, int shmflg) 49091da177e4SLinus Torvalds { 49101da177e4SLinus Torvalds u32 perms; 49111da177e4SLinus Torvalds int rc; 49121da177e4SLinus Torvalds 49131da177e4SLinus Torvalds rc = secondary_ops->shm_shmat(shp, shmaddr, shmflg); 49141da177e4SLinus Torvalds if (rc) 49151da177e4SLinus Torvalds return rc; 49161da177e4SLinus Torvalds 49171da177e4SLinus Torvalds if (shmflg & SHM_RDONLY) 49181da177e4SLinus Torvalds perms = SHM__READ; 49191da177e4SLinus Torvalds else 49201da177e4SLinus Torvalds perms = SHM__READ | SHM__WRITE; 49211da177e4SLinus Torvalds 49226af963f1SStephen Smalley return ipc_has_perm(&shp->shm_perm, perms); 49231da177e4SLinus Torvalds } 49241da177e4SLinus Torvalds 49251da177e4SLinus Torvalds /* Semaphore security operations */ 49261da177e4SLinus Torvalds static int selinux_sem_alloc_security(struct sem_array *sma) 49271da177e4SLinus Torvalds { 49281da177e4SLinus Torvalds struct task_security_struct *tsec; 49291da177e4SLinus Torvalds struct ipc_security_struct *isec; 49301da177e4SLinus Torvalds struct avc_audit_data ad; 49311da177e4SLinus Torvalds int rc; 49321da177e4SLinus Torvalds 49331da177e4SLinus Torvalds rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM); 49341da177e4SLinus Torvalds if (rc) 49351da177e4SLinus Torvalds return rc; 49361da177e4SLinus Torvalds 49371da177e4SLinus Torvalds tsec = current->security; 49381da177e4SLinus Torvalds isec = sma->sem_perm.security; 49391da177e4SLinus Torvalds 49401da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 49411da177e4SLinus Torvalds ad.u.ipc_id = sma->sem_perm.key; 49421da177e4SLinus Torvalds 49431da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_SEM, 49441da177e4SLinus Torvalds SEM__CREATE, &ad); 49451da177e4SLinus Torvalds if (rc) { 49461da177e4SLinus Torvalds ipc_free_security(&sma->sem_perm); 49471da177e4SLinus Torvalds return rc; 49481da177e4SLinus Torvalds } 49491da177e4SLinus Torvalds return 0; 49501da177e4SLinus Torvalds } 49511da177e4SLinus Torvalds 49521da177e4SLinus Torvalds static void selinux_sem_free_security(struct sem_array *sma) 49531da177e4SLinus Torvalds { 49541da177e4SLinus Torvalds ipc_free_security(&sma->sem_perm); 49551da177e4SLinus Torvalds } 49561da177e4SLinus Torvalds 49571da177e4SLinus Torvalds static int selinux_sem_associate(struct sem_array *sma, int semflg) 49581da177e4SLinus Torvalds { 49591da177e4SLinus Torvalds struct task_security_struct *tsec; 49601da177e4SLinus Torvalds struct ipc_security_struct *isec; 49611da177e4SLinus Torvalds struct avc_audit_data ad; 49621da177e4SLinus Torvalds 49631da177e4SLinus Torvalds tsec = current->security; 49641da177e4SLinus Torvalds isec = sma->sem_perm.security; 49651da177e4SLinus Torvalds 49661da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 49671da177e4SLinus Torvalds ad.u.ipc_id = sma->sem_perm.key; 49681da177e4SLinus Torvalds 49691da177e4SLinus Torvalds return avc_has_perm(tsec->sid, isec->sid, SECCLASS_SEM, 49701da177e4SLinus Torvalds SEM__ASSOCIATE, &ad); 49711da177e4SLinus Torvalds } 49721da177e4SLinus Torvalds 49731da177e4SLinus Torvalds /* Note, at this point, sma is locked down */ 49741da177e4SLinus Torvalds static int selinux_sem_semctl(struct sem_array *sma, int cmd) 49751da177e4SLinus Torvalds { 49761da177e4SLinus Torvalds int err; 49771da177e4SLinus Torvalds u32 perms; 49781da177e4SLinus Torvalds 49791da177e4SLinus Torvalds switch (cmd) { 49801da177e4SLinus Torvalds case IPC_INFO: 49811da177e4SLinus Torvalds case SEM_INFO: 49821da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 49831da177e4SLinus Torvalds return task_has_system(current, SYSTEM__IPC_INFO); 49841da177e4SLinus Torvalds case GETPID: 49851da177e4SLinus Torvalds case GETNCNT: 49861da177e4SLinus Torvalds case GETZCNT: 49871da177e4SLinus Torvalds perms = SEM__GETATTR; 49881da177e4SLinus Torvalds break; 49891da177e4SLinus Torvalds case GETVAL: 49901da177e4SLinus Torvalds case GETALL: 49911da177e4SLinus Torvalds perms = SEM__READ; 49921da177e4SLinus Torvalds break; 49931da177e4SLinus Torvalds case SETVAL: 49941da177e4SLinus Torvalds case SETALL: 49951da177e4SLinus Torvalds perms = SEM__WRITE; 49961da177e4SLinus Torvalds break; 49971da177e4SLinus Torvalds case IPC_RMID: 49981da177e4SLinus Torvalds perms = SEM__DESTROY; 49991da177e4SLinus Torvalds break; 50001da177e4SLinus Torvalds case IPC_SET: 50011da177e4SLinus Torvalds perms = SEM__SETATTR; 50021da177e4SLinus Torvalds break; 50031da177e4SLinus Torvalds case IPC_STAT: 50041da177e4SLinus Torvalds case SEM_STAT: 50051da177e4SLinus Torvalds perms = SEM__GETATTR | SEM__ASSOCIATE; 50061da177e4SLinus Torvalds break; 50071da177e4SLinus Torvalds default: 50081da177e4SLinus Torvalds return 0; 50091da177e4SLinus Torvalds } 50101da177e4SLinus Torvalds 50116af963f1SStephen Smalley err = ipc_has_perm(&sma->sem_perm, perms); 50121da177e4SLinus Torvalds return err; 50131da177e4SLinus Torvalds } 50141da177e4SLinus Torvalds 50151da177e4SLinus Torvalds static int selinux_sem_semop(struct sem_array *sma, 50161da177e4SLinus Torvalds struct sembuf *sops, unsigned nsops, int alter) 50171da177e4SLinus Torvalds { 50181da177e4SLinus Torvalds u32 perms; 50191da177e4SLinus Torvalds 50201da177e4SLinus Torvalds if (alter) 50211da177e4SLinus Torvalds perms = SEM__READ | SEM__WRITE; 50221da177e4SLinus Torvalds else 50231da177e4SLinus Torvalds perms = SEM__READ; 50241da177e4SLinus Torvalds 50256af963f1SStephen Smalley return ipc_has_perm(&sma->sem_perm, perms); 50261da177e4SLinus Torvalds } 50271da177e4SLinus Torvalds 50281da177e4SLinus Torvalds static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag) 50291da177e4SLinus Torvalds { 50301da177e4SLinus Torvalds u32 av = 0; 50311da177e4SLinus Torvalds 50321da177e4SLinus Torvalds av = 0; 50331da177e4SLinus Torvalds if (flag & S_IRUGO) 50341da177e4SLinus Torvalds av |= IPC__UNIX_READ; 50351da177e4SLinus Torvalds if (flag & S_IWUGO) 50361da177e4SLinus Torvalds av |= IPC__UNIX_WRITE; 50371da177e4SLinus Torvalds 50381da177e4SLinus Torvalds if (av == 0) 50391da177e4SLinus Torvalds return 0; 50401da177e4SLinus Torvalds 50416af963f1SStephen Smalley return ipc_has_perm(ipcp, av); 50421da177e4SLinus Torvalds } 50431da177e4SLinus Torvalds 5044713a04aeSAhmed S. Darwish static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid) 5045713a04aeSAhmed S. Darwish { 5046713a04aeSAhmed S. Darwish struct ipc_security_struct *isec = ipcp->security; 5047713a04aeSAhmed S. Darwish *secid = isec->sid; 5048713a04aeSAhmed S. Darwish } 5049713a04aeSAhmed S. Darwish 50501da177e4SLinus Torvalds /* module stacking operations */ 50511da177e4SLinus Torvalds static int selinux_register_security(const char *name, struct security_operations *ops) 50521da177e4SLinus Torvalds { 50531da177e4SLinus Torvalds if (secondary_ops != original_ops) { 5054fadcdb45SEric Paris printk(KERN_ERR "%s: There is already a secondary security " 5055dd6f953aSHarvey Harrison "module registered.\n", __func__); 50561da177e4SLinus Torvalds return -EINVAL; 50571da177e4SLinus Torvalds } 50581da177e4SLinus Torvalds 50591da177e4SLinus Torvalds secondary_ops = ops; 50601da177e4SLinus Torvalds 50611da177e4SLinus Torvalds printk(KERN_INFO "%s: Registering secondary module %s\n", 5062dd6f953aSHarvey Harrison __func__, 50631da177e4SLinus Torvalds name); 50641da177e4SLinus Torvalds 50651da177e4SLinus Torvalds return 0; 50661da177e4SLinus Torvalds } 50671da177e4SLinus Torvalds 50681da177e4SLinus Torvalds static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode) 50691da177e4SLinus Torvalds { 50701da177e4SLinus Torvalds if (inode) 50711da177e4SLinus Torvalds inode_doinit_with_dentry(inode, dentry); 50721da177e4SLinus Torvalds } 50731da177e4SLinus Torvalds 50741da177e4SLinus Torvalds static int selinux_getprocattr(struct task_struct *p, 507504ff9708SAl Viro char *name, char **value) 50761da177e4SLinus Torvalds { 50771da177e4SLinus Torvalds struct task_security_struct *tsec; 50788c8570fbSDustin Kirkland u32 sid; 50791da177e4SLinus Torvalds int error; 508004ff9708SAl Viro unsigned len; 50811da177e4SLinus Torvalds 50821da177e4SLinus Torvalds if (current != p) { 50831da177e4SLinus Torvalds error = task_has_perm(current, p, PROCESS__GETATTR); 50841da177e4SLinus Torvalds if (error) 50851da177e4SLinus Torvalds return error; 50861da177e4SLinus Torvalds } 50871da177e4SLinus Torvalds 50881da177e4SLinus Torvalds tsec = p->security; 50891da177e4SLinus Torvalds 50901da177e4SLinus Torvalds if (!strcmp(name, "current")) 50911da177e4SLinus Torvalds sid = tsec->sid; 50921da177e4SLinus Torvalds else if (!strcmp(name, "prev")) 50931da177e4SLinus Torvalds sid = tsec->osid; 50941da177e4SLinus Torvalds else if (!strcmp(name, "exec")) 50951da177e4SLinus Torvalds sid = tsec->exec_sid; 50961da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 50971da177e4SLinus Torvalds sid = tsec->create_sid; 50984eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) 50994eb582cfSMichael LeMay sid = tsec->keycreate_sid; 510042c3e03eSEric Paris else if (!strcmp(name, "sockcreate")) 510142c3e03eSEric Paris sid = tsec->sockcreate_sid; 51021da177e4SLinus Torvalds else 51031da177e4SLinus Torvalds return -EINVAL; 51041da177e4SLinus Torvalds 51051da177e4SLinus Torvalds if (!sid) 51061da177e4SLinus Torvalds return 0; 51071da177e4SLinus Torvalds 510804ff9708SAl Viro error = security_sid_to_context(sid, value, &len); 510904ff9708SAl Viro if (error) 511004ff9708SAl Viro return error; 511104ff9708SAl Viro return len; 51121da177e4SLinus Torvalds } 51131da177e4SLinus Torvalds 51141da177e4SLinus Torvalds static int selinux_setprocattr(struct task_struct *p, 51151da177e4SLinus Torvalds char *name, void *value, size_t size) 51161da177e4SLinus Torvalds { 51171da177e4SLinus Torvalds struct task_security_struct *tsec; 51180356357cSRoland McGrath struct task_struct *tracer; 51191da177e4SLinus Torvalds u32 sid = 0; 51201da177e4SLinus Torvalds int error; 51211da177e4SLinus Torvalds char *str = value; 51221da177e4SLinus Torvalds 51231da177e4SLinus Torvalds if (current != p) { 51241da177e4SLinus Torvalds /* SELinux only allows a process to change its own 51251da177e4SLinus Torvalds security attributes. */ 51261da177e4SLinus Torvalds return -EACCES; 51271da177e4SLinus Torvalds } 51281da177e4SLinus Torvalds 51291da177e4SLinus Torvalds /* 51301da177e4SLinus Torvalds * Basic control over ability to set these attributes at all. 51311da177e4SLinus Torvalds * current == p, but we'll pass them separately in case the 51321da177e4SLinus Torvalds * above restriction is ever removed. 51331da177e4SLinus Torvalds */ 51341da177e4SLinus Torvalds if (!strcmp(name, "exec")) 51351da177e4SLinus Torvalds error = task_has_perm(current, p, PROCESS__SETEXEC); 51361da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 51371da177e4SLinus Torvalds error = task_has_perm(current, p, PROCESS__SETFSCREATE); 51384eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) 51394eb582cfSMichael LeMay error = task_has_perm(current, p, PROCESS__SETKEYCREATE); 514042c3e03eSEric Paris else if (!strcmp(name, "sockcreate")) 514142c3e03eSEric Paris error = task_has_perm(current, p, PROCESS__SETSOCKCREATE); 51421da177e4SLinus Torvalds else if (!strcmp(name, "current")) 51431da177e4SLinus Torvalds error = task_has_perm(current, p, PROCESS__SETCURRENT); 51441da177e4SLinus Torvalds else 51451da177e4SLinus Torvalds error = -EINVAL; 51461da177e4SLinus Torvalds if (error) 51471da177e4SLinus Torvalds return error; 51481da177e4SLinus Torvalds 51491da177e4SLinus Torvalds /* Obtain a SID for the context, if one was specified. */ 51501da177e4SLinus Torvalds if (size && str[1] && str[1] != '\n') { 51511da177e4SLinus Torvalds if (str[size-1] == '\n') { 51521da177e4SLinus Torvalds str[size-1] = 0; 51531da177e4SLinus Torvalds size--; 51541da177e4SLinus Torvalds } 51551da177e4SLinus Torvalds error = security_context_to_sid(value, size, &sid); 51561da177e4SLinus Torvalds if (error) 51571da177e4SLinus Torvalds return error; 51581da177e4SLinus Torvalds } 51591da177e4SLinus Torvalds 51601da177e4SLinus Torvalds /* Permission checking based on the specified context is 51611da177e4SLinus Torvalds performed during the actual operation (execve, 51621da177e4SLinus Torvalds open/mkdir/...), when we know the full context of the 51631da177e4SLinus Torvalds operation. See selinux_bprm_set_security for the execve 51641da177e4SLinus Torvalds checks and may_create for the file creation checks. The 51651da177e4SLinus Torvalds operation will then fail if the context is not permitted. */ 51661da177e4SLinus Torvalds tsec = p->security; 51671da177e4SLinus Torvalds if (!strcmp(name, "exec")) 51681da177e4SLinus Torvalds tsec->exec_sid = sid; 51691da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 51701da177e4SLinus Torvalds tsec->create_sid = sid; 51714eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) { 51724eb582cfSMichael LeMay error = may_create_key(sid, p); 51734eb582cfSMichael LeMay if (error) 51744eb582cfSMichael LeMay return error; 51754eb582cfSMichael LeMay tsec->keycreate_sid = sid; 517642c3e03eSEric Paris } else if (!strcmp(name, "sockcreate")) 517742c3e03eSEric Paris tsec->sockcreate_sid = sid; 517842c3e03eSEric Paris else if (!strcmp(name, "current")) { 51791da177e4SLinus Torvalds struct av_decision avd; 51801da177e4SLinus Torvalds 51811da177e4SLinus Torvalds if (sid == 0) 51821da177e4SLinus Torvalds return -EINVAL; 51831da177e4SLinus Torvalds 51841da177e4SLinus Torvalds /* Only allow single threaded processes to change context */ 51851da177e4SLinus Torvalds if (atomic_read(&p->mm->mm_users) != 1) { 51861da177e4SLinus Torvalds struct task_struct *g, *t; 51871da177e4SLinus Torvalds struct mm_struct *mm = p->mm; 51881da177e4SLinus Torvalds read_lock(&tasklist_lock); 51891da177e4SLinus Torvalds do_each_thread(g, t) 51901da177e4SLinus Torvalds if (t->mm == mm && t != p) { 51911da177e4SLinus Torvalds read_unlock(&tasklist_lock); 51921da177e4SLinus Torvalds return -EPERM; 51931da177e4SLinus Torvalds } 51941da177e4SLinus Torvalds while_each_thread(g, t); 51951da177e4SLinus Torvalds read_unlock(&tasklist_lock); 51961da177e4SLinus Torvalds } 51971da177e4SLinus Torvalds 51981da177e4SLinus Torvalds /* Check permissions for the transition. */ 51991da177e4SLinus Torvalds error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS, 52001da177e4SLinus Torvalds PROCESS__DYNTRANSITION, NULL); 52011da177e4SLinus Torvalds if (error) 52021da177e4SLinus Torvalds return error; 52031da177e4SLinus Torvalds 52041da177e4SLinus Torvalds /* Check for ptracing, and update the task SID if ok. 52051da177e4SLinus Torvalds Otherwise, leave SID unchanged and fail. */ 52061da177e4SLinus Torvalds task_lock(p); 52070356357cSRoland McGrath rcu_read_lock(); 52080356357cSRoland McGrath tracer = task_tracer_task(p); 52090356357cSRoland McGrath if (tracer != NULL) { 52100356357cSRoland McGrath struct task_security_struct *ptsec = tracer->security; 52110356357cSRoland McGrath u32 ptsid = ptsec->sid; 52120356357cSRoland McGrath rcu_read_unlock(); 52130356357cSRoland McGrath error = avc_has_perm_noaudit(ptsid, sid, 52141da177e4SLinus Torvalds SECCLASS_PROCESS, 52152c3c05dbSStephen Smalley PROCESS__PTRACE, 0, &avd); 52161da177e4SLinus Torvalds if (!error) 52171da177e4SLinus Torvalds tsec->sid = sid; 52181da177e4SLinus Torvalds task_unlock(p); 52190356357cSRoland McGrath avc_audit(ptsid, sid, SECCLASS_PROCESS, 52201da177e4SLinus Torvalds PROCESS__PTRACE, &avd, error, NULL); 52211da177e4SLinus Torvalds if (error) 52221da177e4SLinus Torvalds return error; 52231da177e4SLinus Torvalds } else { 52240356357cSRoland McGrath rcu_read_unlock(); 52251da177e4SLinus Torvalds tsec->sid = sid; 52261da177e4SLinus Torvalds task_unlock(p); 52271da177e4SLinus Torvalds } 5228828dfe1dSEric Paris } else 52291da177e4SLinus Torvalds return -EINVAL; 52301da177e4SLinus Torvalds 52311da177e4SLinus Torvalds return size; 52321da177e4SLinus Torvalds } 52331da177e4SLinus Torvalds 5234dc49c1f9SCatherine Zhang static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) 5235dc49c1f9SCatherine Zhang { 5236dc49c1f9SCatherine Zhang return security_sid_to_context(secid, secdata, seclen); 5237dc49c1f9SCatherine Zhang } 5238dc49c1f9SCatherine Zhang 523963cb3449SDavid Howells static int selinux_secctx_to_secid(char *secdata, u32 seclen, u32 *secid) 524063cb3449SDavid Howells { 524163cb3449SDavid Howells return security_context_to_sid(secdata, seclen, secid); 524263cb3449SDavid Howells } 524363cb3449SDavid Howells 5244dc49c1f9SCatherine Zhang static void selinux_release_secctx(char *secdata, u32 seclen) 5245dc49c1f9SCatherine Zhang { 5246dc49c1f9SCatherine Zhang kfree(secdata); 5247dc49c1f9SCatherine Zhang } 5248dc49c1f9SCatherine Zhang 5249d720024eSMichael LeMay #ifdef CONFIG_KEYS 5250d720024eSMichael LeMay 52517e047ef5SDavid Howells static int selinux_key_alloc(struct key *k, struct task_struct *tsk, 52527e047ef5SDavid Howells unsigned long flags) 5253d720024eSMichael LeMay { 5254d720024eSMichael LeMay struct task_security_struct *tsec = tsk->security; 5255d720024eSMichael LeMay struct key_security_struct *ksec; 5256d720024eSMichael LeMay 5257d720024eSMichael LeMay ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL); 5258d720024eSMichael LeMay if (!ksec) 5259d720024eSMichael LeMay return -ENOMEM; 5260d720024eSMichael LeMay 52614eb582cfSMichael LeMay if (tsec->keycreate_sid) 52624eb582cfSMichael LeMay ksec->sid = tsec->keycreate_sid; 52634eb582cfSMichael LeMay else 5264d720024eSMichael LeMay ksec->sid = tsec->sid; 5265d720024eSMichael LeMay k->security = ksec; 5266d720024eSMichael LeMay 5267d720024eSMichael LeMay return 0; 5268d720024eSMichael LeMay } 5269d720024eSMichael LeMay 5270d720024eSMichael LeMay static void selinux_key_free(struct key *k) 5271d720024eSMichael LeMay { 5272d720024eSMichael LeMay struct key_security_struct *ksec = k->security; 5273d720024eSMichael LeMay 5274d720024eSMichael LeMay k->security = NULL; 5275d720024eSMichael LeMay kfree(ksec); 5276d720024eSMichael LeMay } 5277d720024eSMichael LeMay 5278d720024eSMichael LeMay static int selinux_key_permission(key_ref_t key_ref, 5279d720024eSMichael LeMay struct task_struct *ctx, 5280d720024eSMichael LeMay key_perm_t perm) 5281d720024eSMichael LeMay { 5282d720024eSMichael LeMay struct key *key; 5283d720024eSMichael LeMay struct task_security_struct *tsec; 5284d720024eSMichael LeMay struct key_security_struct *ksec; 5285d720024eSMichael LeMay 5286d720024eSMichael LeMay key = key_ref_to_ptr(key_ref); 5287d720024eSMichael LeMay 5288d720024eSMichael LeMay tsec = ctx->security; 5289d720024eSMichael LeMay ksec = key->security; 5290d720024eSMichael LeMay 5291d720024eSMichael LeMay /* if no specific permissions are requested, we skip the 5292d720024eSMichael LeMay permission check. No serious, additional covert channels 5293d720024eSMichael LeMay appear to be created. */ 5294d720024eSMichael LeMay if (perm == 0) 5295d720024eSMichael LeMay return 0; 5296d720024eSMichael LeMay 5297d720024eSMichael LeMay return avc_has_perm(tsec->sid, ksec->sid, 5298d720024eSMichael LeMay SECCLASS_KEY, perm, NULL); 5299d720024eSMichael LeMay } 5300d720024eSMichael LeMay 5301d720024eSMichael LeMay #endif 5302d720024eSMichael LeMay 53031da177e4SLinus Torvalds static struct security_operations selinux_ops = { 5304076c54c5SAhmed S. Darwish .name = "selinux", 5305076c54c5SAhmed S. Darwish 53061da177e4SLinus Torvalds .ptrace = selinux_ptrace, 53071da177e4SLinus Torvalds .capget = selinux_capget, 53081da177e4SLinus Torvalds .capset_check = selinux_capset_check, 53091da177e4SLinus Torvalds .capset_set = selinux_capset_set, 53101da177e4SLinus Torvalds .sysctl = selinux_sysctl, 53111da177e4SLinus Torvalds .capable = selinux_capable, 53121da177e4SLinus Torvalds .quotactl = selinux_quotactl, 53131da177e4SLinus Torvalds .quota_on = selinux_quota_on, 53141da177e4SLinus Torvalds .syslog = selinux_syslog, 53151da177e4SLinus Torvalds .vm_enough_memory = selinux_vm_enough_memory, 53161da177e4SLinus Torvalds 53171da177e4SLinus Torvalds .netlink_send = selinux_netlink_send, 53181da177e4SLinus Torvalds .netlink_recv = selinux_netlink_recv, 53191da177e4SLinus Torvalds 53201da177e4SLinus Torvalds .bprm_alloc_security = selinux_bprm_alloc_security, 53211da177e4SLinus Torvalds .bprm_free_security = selinux_bprm_free_security, 53221da177e4SLinus Torvalds .bprm_apply_creds = selinux_bprm_apply_creds, 53231da177e4SLinus Torvalds .bprm_post_apply_creds = selinux_bprm_post_apply_creds, 53241da177e4SLinus Torvalds .bprm_set_security = selinux_bprm_set_security, 53251da177e4SLinus Torvalds .bprm_check_security = selinux_bprm_check_security, 53261da177e4SLinus Torvalds .bprm_secureexec = selinux_bprm_secureexec, 53271da177e4SLinus Torvalds 53281da177e4SLinus Torvalds .sb_alloc_security = selinux_sb_alloc_security, 53291da177e4SLinus Torvalds .sb_free_security = selinux_sb_free_security, 53301da177e4SLinus Torvalds .sb_copy_data = selinux_sb_copy_data, 53311da177e4SLinus Torvalds .sb_kern_mount = selinux_sb_kern_mount, 53321da177e4SLinus Torvalds .sb_statfs = selinux_sb_statfs, 53331da177e4SLinus Torvalds .sb_mount = selinux_mount, 53341da177e4SLinus Torvalds .sb_umount = selinux_umount, 5335c9180a57SEric Paris .sb_get_mnt_opts = selinux_get_mnt_opts, 5336c9180a57SEric Paris .sb_set_mnt_opts = selinux_set_mnt_opts, 5337c9180a57SEric Paris .sb_clone_mnt_opts = selinux_sb_clone_mnt_opts, 5338e0007529SEric Paris .sb_parse_opts_str = selinux_parse_opts_str, 5339e0007529SEric Paris 53401da177e4SLinus Torvalds 53411da177e4SLinus Torvalds .inode_alloc_security = selinux_inode_alloc_security, 53421da177e4SLinus Torvalds .inode_free_security = selinux_inode_free_security, 53435e41ff9eSStephen Smalley .inode_init_security = selinux_inode_init_security, 53441da177e4SLinus Torvalds .inode_create = selinux_inode_create, 53451da177e4SLinus Torvalds .inode_link = selinux_inode_link, 53461da177e4SLinus Torvalds .inode_unlink = selinux_inode_unlink, 53471da177e4SLinus Torvalds .inode_symlink = selinux_inode_symlink, 53481da177e4SLinus Torvalds .inode_mkdir = selinux_inode_mkdir, 53491da177e4SLinus Torvalds .inode_rmdir = selinux_inode_rmdir, 53501da177e4SLinus Torvalds .inode_mknod = selinux_inode_mknod, 53511da177e4SLinus Torvalds .inode_rename = selinux_inode_rename, 53521da177e4SLinus Torvalds .inode_readlink = selinux_inode_readlink, 53531da177e4SLinus Torvalds .inode_follow_link = selinux_inode_follow_link, 53541da177e4SLinus Torvalds .inode_permission = selinux_inode_permission, 53551da177e4SLinus Torvalds .inode_setattr = selinux_inode_setattr, 53561da177e4SLinus Torvalds .inode_getattr = selinux_inode_getattr, 53571da177e4SLinus Torvalds .inode_setxattr = selinux_inode_setxattr, 53581da177e4SLinus Torvalds .inode_post_setxattr = selinux_inode_post_setxattr, 53591da177e4SLinus Torvalds .inode_getxattr = selinux_inode_getxattr, 53601da177e4SLinus Torvalds .inode_listxattr = selinux_inode_listxattr, 53611da177e4SLinus Torvalds .inode_removexattr = selinux_inode_removexattr, 53621da177e4SLinus Torvalds .inode_getsecurity = selinux_inode_getsecurity, 53631da177e4SLinus Torvalds .inode_setsecurity = selinux_inode_setsecurity, 53641da177e4SLinus Torvalds .inode_listsecurity = selinux_inode_listsecurity, 5365b5376771SSerge E. Hallyn .inode_need_killpriv = selinux_inode_need_killpriv, 5366b5376771SSerge E. Hallyn .inode_killpriv = selinux_inode_killpriv, 5367713a04aeSAhmed S. Darwish .inode_getsecid = selinux_inode_getsecid, 53681da177e4SLinus Torvalds 53691da177e4SLinus Torvalds .file_permission = selinux_file_permission, 53701da177e4SLinus Torvalds .file_alloc_security = selinux_file_alloc_security, 53711da177e4SLinus Torvalds .file_free_security = selinux_file_free_security, 53721da177e4SLinus Torvalds .file_ioctl = selinux_file_ioctl, 53731da177e4SLinus Torvalds .file_mmap = selinux_file_mmap, 53741da177e4SLinus Torvalds .file_mprotect = selinux_file_mprotect, 53751da177e4SLinus Torvalds .file_lock = selinux_file_lock, 53761da177e4SLinus Torvalds .file_fcntl = selinux_file_fcntl, 53771da177e4SLinus Torvalds .file_set_fowner = selinux_file_set_fowner, 53781da177e4SLinus Torvalds .file_send_sigiotask = selinux_file_send_sigiotask, 53791da177e4SLinus Torvalds .file_receive = selinux_file_receive, 53801da177e4SLinus Torvalds 5381788e7dd4SYuichi Nakamura .dentry_open = selinux_dentry_open, 5382788e7dd4SYuichi Nakamura 53831da177e4SLinus Torvalds .task_create = selinux_task_create, 53841da177e4SLinus Torvalds .task_alloc_security = selinux_task_alloc_security, 53851da177e4SLinus Torvalds .task_free_security = selinux_task_free_security, 53861da177e4SLinus Torvalds .task_setuid = selinux_task_setuid, 53871da177e4SLinus Torvalds .task_post_setuid = selinux_task_post_setuid, 53881da177e4SLinus Torvalds .task_setgid = selinux_task_setgid, 53891da177e4SLinus Torvalds .task_setpgid = selinux_task_setpgid, 53901da177e4SLinus Torvalds .task_getpgid = selinux_task_getpgid, 53911da177e4SLinus Torvalds .task_getsid = selinux_task_getsid, 5392f9008e4cSDavid Quigley .task_getsecid = selinux_task_getsecid, 53931da177e4SLinus Torvalds .task_setgroups = selinux_task_setgroups, 53941da177e4SLinus Torvalds .task_setnice = selinux_task_setnice, 539503e68060SJames Morris .task_setioprio = selinux_task_setioprio, 5396a1836a42SDavid Quigley .task_getioprio = selinux_task_getioprio, 53971da177e4SLinus Torvalds .task_setrlimit = selinux_task_setrlimit, 53981da177e4SLinus Torvalds .task_setscheduler = selinux_task_setscheduler, 53991da177e4SLinus Torvalds .task_getscheduler = selinux_task_getscheduler, 540035601547SDavid Quigley .task_movememory = selinux_task_movememory, 54011da177e4SLinus Torvalds .task_kill = selinux_task_kill, 54021da177e4SLinus Torvalds .task_wait = selinux_task_wait, 54031da177e4SLinus Torvalds .task_prctl = selinux_task_prctl, 54041da177e4SLinus Torvalds .task_reparent_to_init = selinux_task_reparent_to_init, 54051da177e4SLinus Torvalds .task_to_inode = selinux_task_to_inode, 54061da177e4SLinus Torvalds 54071da177e4SLinus Torvalds .ipc_permission = selinux_ipc_permission, 5408713a04aeSAhmed S. Darwish .ipc_getsecid = selinux_ipc_getsecid, 54091da177e4SLinus Torvalds 54101da177e4SLinus Torvalds .msg_msg_alloc_security = selinux_msg_msg_alloc_security, 54111da177e4SLinus Torvalds .msg_msg_free_security = selinux_msg_msg_free_security, 54121da177e4SLinus Torvalds 54131da177e4SLinus Torvalds .msg_queue_alloc_security = selinux_msg_queue_alloc_security, 54141da177e4SLinus Torvalds .msg_queue_free_security = selinux_msg_queue_free_security, 54151da177e4SLinus Torvalds .msg_queue_associate = selinux_msg_queue_associate, 54161da177e4SLinus Torvalds .msg_queue_msgctl = selinux_msg_queue_msgctl, 54171da177e4SLinus Torvalds .msg_queue_msgsnd = selinux_msg_queue_msgsnd, 54181da177e4SLinus Torvalds .msg_queue_msgrcv = selinux_msg_queue_msgrcv, 54191da177e4SLinus Torvalds 54201da177e4SLinus Torvalds .shm_alloc_security = selinux_shm_alloc_security, 54211da177e4SLinus Torvalds .shm_free_security = selinux_shm_free_security, 54221da177e4SLinus Torvalds .shm_associate = selinux_shm_associate, 54231da177e4SLinus Torvalds .shm_shmctl = selinux_shm_shmctl, 54241da177e4SLinus Torvalds .shm_shmat = selinux_shm_shmat, 54251da177e4SLinus Torvalds 54261da177e4SLinus Torvalds .sem_alloc_security = selinux_sem_alloc_security, 54271da177e4SLinus Torvalds .sem_free_security = selinux_sem_free_security, 54281da177e4SLinus Torvalds .sem_associate = selinux_sem_associate, 54291da177e4SLinus Torvalds .sem_semctl = selinux_sem_semctl, 54301da177e4SLinus Torvalds .sem_semop = selinux_sem_semop, 54311da177e4SLinus Torvalds 54321da177e4SLinus Torvalds .register_security = selinux_register_security, 54331da177e4SLinus Torvalds 54341da177e4SLinus Torvalds .d_instantiate = selinux_d_instantiate, 54351da177e4SLinus Torvalds 54361da177e4SLinus Torvalds .getprocattr = selinux_getprocattr, 54371da177e4SLinus Torvalds .setprocattr = selinux_setprocattr, 54381da177e4SLinus Torvalds 5439dc49c1f9SCatherine Zhang .secid_to_secctx = selinux_secid_to_secctx, 544063cb3449SDavid Howells .secctx_to_secid = selinux_secctx_to_secid, 5441dc49c1f9SCatherine Zhang .release_secctx = selinux_release_secctx, 5442dc49c1f9SCatherine Zhang 54431da177e4SLinus Torvalds .unix_stream_connect = selinux_socket_unix_stream_connect, 54441da177e4SLinus Torvalds .unix_may_send = selinux_socket_unix_may_send, 54451da177e4SLinus Torvalds 54461da177e4SLinus Torvalds .socket_create = selinux_socket_create, 54471da177e4SLinus Torvalds .socket_post_create = selinux_socket_post_create, 54481da177e4SLinus Torvalds .socket_bind = selinux_socket_bind, 54491da177e4SLinus Torvalds .socket_connect = selinux_socket_connect, 54501da177e4SLinus Torvalds .socket_listen = selinux_socket_listen, 54511da177e4SLinus Torvalds .socket_accept = selinux_socket_accept, 54521da177e4SLinus Torvalds .socket_sendmsg = selinux_socket_sendmsg, 54531da177e4SLinus Torvalds .socket_recvmsg = selinux_socket_recvmsg, 54541da177e4SLinus Torvalds .socket_getsockname = selinux_socket_getsockname, 54551da177e4SLinus Torvalds .socket_getpeername = selinux_socket_getpeername, 54561da177e4SLinus Torvalds .socket_getsockopt = selinux_socket_getsockopt, 54571da177e4SLinus Torvalds .socket_setsockopt = selinux_socket_setsockopt, 54581da177e4SLinus Torvalds .socket_shutdown = selinux_socket_shutdown, 54591da177e4SLinus Torvalds .socket_sock_rcv_skb = selinux_socket_sock_rcv_skb, 54602c7946a7SCatherine Zhang .socket_getpeersec_stream = selinux_socket_getpeersec_stream, 54612c7946a7SCatherine Zhang .socket_getpeersec_dgram = selinux_socket_getpeersec_dgram, 54621da177e4SLinus Torvalds .sk_alloc_security = selinux_sk_alloc_security, 54631da177e4SLinus Torvalds .sk_free_security = selinux_sk_free_security, 5464892c141eSVenkat Yekkirala .sk_clone_security = selinux_sk_clone_security, 5465beb8d13bSVenkat Yekkirala .sk_getsecid = selinux_sk_getsecid, 54664237c75cSVenkat Yekkirala .sock_graft = selinux_sock_graft, 54674237c75cSVenkat Yekkirala .inet_conn_request = selinux_inet_conn_request, 54684237c75cSVenkat Yekkirala .inet_csk_clone = selinux_inet_csk_clone, 54696b877699SVenkat Yekkirala .inet_conn_established = selinux_inet_conn_established, 54704237c75cSVenkat Yekkirala .req_classify_flow = selinux_req_classify_flow, 5471d28d1e08STrent Jaeger 5472d28d1e08STrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM 5473d28d1e08STrent Jaeger .xfrm_policy_alloc_security = selinux_xfrm_policy_alloc, 5474d28d1e08STrent Jaeger .xfrm_policy_clone_security = selinux_xfrm_policy_clone, 5475d28d1e08STrent Jaeger .xfrm_policy_free_security = selinux_xfrm_policy_free, 5476c8c05a8eSCatherine Zhang .xfrm_policy_delete_security = selinux_xfrm_policy_delete, 5477d28d1e08STrent Jaeger .xfrm_state_alloc_security = selinux_xfrm_state_alloc, 5478d28d1e08STrent Jaeger .xfrm_state_free_security = selinux_xfrm_state_free, 5479c8c05a8eSCatherine Zhang .xfrm_state_delete_security = selinux_xfrm_state_delete, 5480d28d1e08STrent Jaeger .xfrm_policy_lookup = selinux_xfrm_policy_lookup, 5481e0d1caa7SVenkat Yekkirala .xfrm_state_pol_flow_match = selinux_xfrm_state_pol_flow_match, 5482e0d1caa7SVenkat Yekkirala .xfrm_decode_session = selinux_xfrm_decode_session, 54831da177e4SLinus Torvalds #endif 5484d720024eSMichael LeMay 5485d720024eSMichael LeMay #ifdef CONFIG_KEYS 5486d720024eSMichael LeMay .key_alloc = selinux_key_alloc, 5487d720024eSMichael LeMay .key_free = selinux_key_free, 5488d720024eSMichael LeMay .key_permission = selinux_key_permission, 5489d720024eSMichael LeMay #endif 54909d57a7f9SAhmed S. Darwish 54919d57a7f9SAhmed S. Darwish #ifdef CONFIG_AUDIT 54929d57a7f9SAhmed S. Darwish .audit_rule_init = selinux_audit_rule_init, 54939d57a7f9SAhmed S. Darwish .audit_rule_known = selinux_audit_rule_known, 54949d57a7f9SAhmed S. Darwish .audit_rule_match = selinux_audit_rule_match, 54959d57a7f9SAhmed S. Darwish .audit_rule_free = selinux_audit_rule_free, 54969d57a7f9SAhmed S. Darwish #endif 54971da177e4SLinus Torvalds }; 54981da177e4SLinus Torvalds 54991da177e4SLinus Torvalds static __init int selinux_init(void) 55001da177e4SLinus Torvalds { 55011da177e4SLinus Torvalds struct task_security_struct *tsec; 55021da177e4SLinus Torvalds 5503076c54c5SAhmed S. Darwish if (!security_module_enable(&selinux_ops)) { 5504076c54c5SAhmed S. Darwish selinux_enabled = 0; 5505076c54c5SAhmed S. Darwish return 0; 5506076c54c5SAhmed S. Darwish } 5507076c54c5SAhmed S. Darwish 55081da177e4SLinus Torvalds if (!selinux_enabled) { 55091da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Disabled at boot.\n"); 55101da177e4SLinus Torvalds return 0; 55111da177e4SLinus Torvalds } 55121da177e4SLinus Torvalds 55131da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Initializing.\n"); 55141da177e4SLinus Torvalds 55151da177e4SLinus Torvalds /* Set the security state for the initial task. */ 55161da177e4SLinus Torvalds if (task_alloc_security(current)) 55171da177e4SLinus Torvalds panic("SELinux: Failed to initialize initial task.\n"); 55181da177e4SLinus Torvalds tsec = current->security; 55191da177e4SLinus Torvalds tsec->osid = tsec->sid = SECINITSID_KERNEL; 55201da177e4SLinus Torvalds 55217cae7e26SJames Morris sel_inode_cache = kmem_cache_create("selinux_inode_security", 55227cae7e26SJames Morris sizeof(struct inode_security_struct), 552320c2df83SPaul Mundt 0, SLAB_PANIC, NULL); 55241da177e4SLinus Torvalds avc_init(); 55251da177e4SLinus Torvalds 55261da177e4SLinus Torvalds original_ops = secondary_ops = security_ops; 55271da177e4SLinus Torvalds if (!secondary_ops) 55281da177e4SLinus Torvalds panic("SELinux: No initial security operations\n"); 55291da177e4SLinus Torvalds if (register_security(&selinux_ops)) 55301da177e4SLinus Torvalds panic("SELinux: Unable to register with kernel.\n"); 55311da177e4SLinus Torvalds 5532828dfe1dSEric Paris if (selinux_enforcing) 5533fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Starting in enforcing mode\n"); 5534828dfe1dSEric Paris else 5535fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Starting in permissive mode\n"); 5536d720024eSMichael LeMay 5537d720024eSMichael LeMay #ifdef CONFIG_KEYS 5538d720024eSMichael LeMay /* Add security information to initial keyrings */ 55394eb582cfSMichael LeMay selinux_key_alloc(&root_user_keyring, current, 55407e047ef5SDavid Howells KEY_ALLOC_NOT_IN_QUOTA); 55414eb582cfSMichael LeMay selinux_key_alloc(&root_session_keyring, current, 55427e047ef5SDavid Howells KEY_ALLOC_NOT_IN_QUOTA); 5543d720024eSMichael LeMay #endif 5544d720024eSMichael LeMay 55451da177e4SLinus Torvalds return 0; 55461da177e4SLinus Torvalds } 55471da177e4SLinus Torvalds 55481da177e4SLinus Torvalds void selinux_complete_init(void) 55491da177e4SLinus Torvalds { 5550fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Completing initialization.\n"); 55511da177e4SLinus Torvalds 55521da177e4SLinus Torvalds /* Set up any superblocks initialized prior to the policy load. */ 5553fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Setting up existing superblocks.\n"); 5554ba0c19edSStephen Smalley spin_lock(&sb_lock); 55551da177e4SLinus Torvalds spin_lock(&sb_security_lock); 55561da177e4SLinus Torvalds next_sb: 55571da177e4SLinus Torvalds if (!list_empty(&superblock_security_head)) { 55581da177e4SLinus Torvalds struct superblock_security_struct *sbsec = 55591da177e4SLinus Torvalds list_entry(superblock_security_head.next, 55601da177e4SLinus Torvalds struct superblock_security_struct, 55611da177e4SLinus Torvalds list); 55621da177e4SLinus Torvalds struct super_block *sb = sbsec->sb; 55631da177e4SLinus Torvalds sb->s_count++; 55641da177e4SLinus Torvalds spin_unlock(&sb_security_lock); 5565ba0c19edSStephen Smalley spin_unlock(&sb_lock); 55661da177e4SLinus Torvalds down_read(&sb->s_umount); 55671da177e4SLinus Torvalds if (sb->s_root) 55681da177e4SLinus Torvalds superblock_doinit(sb, NULL); 55691da177e4SLinus Torvalds drop_super(sb); 5570ba0c19edSStephen Smalley spin_lock(&sb_lock); 55711da177e4SLinus Torvalds spin_lock(&sb_security_lock); 55721da177e4SLinus Torvalds list_del_init(&sbsec->list); 55731da177e4SLinus Torvalds goto next_sb; 55741da177e4SLinus Torvalds } 55751da177e4SLinus Torvalds spin_unlock(&sb_security_lock); 5576ba0c19edSStephen Smalley spin_unlock(&sb_lock); 55771da177e4SLinus Torvalds } 55781da177e4SLinus Torvalds 55791da177e4SLinus Torvalds /* SELinux requires early initialization in order to label 55801da177e4SLinus Torvalds all processes and objects when they are created. */ 55811da177e4SLinus Torvalds security_initcall(selinux_init); 55821da177e4SLinus Torvalds 5583c2b507fdSStephen Smalley #if defined(CONFIG_NETFILTER) 55841da177e4SLinus Torvalds 5585effad8dfSPaul Moore static struct nf_hook_ops selinux_ipv4_ops[] = { 5586effad8dfSPaul Moore { 5587effad8dfSPaul Moore .hook = selinux_ipv4_postroute, 55881da177e4SLinus Torvalds .owner = THIS_MODULE, 55891da177e4SLinus Torvalds .pf = PF_INET, 55906e23ae2aSPatrick McHardy .hooknum = NF_INET_POST_ROUTING, 55911da177e4SLinus Torvalds .priority = NF_IP_PRI_SELINUX_LAST, 5592effad8dfSPaul Moore }, 5593effad8dfSPaul Moore { 5594effad8dfSPaul Moore .hook = selinux_ipv4_forward, 5595effad8dfSPaul Moore .owner = THIS_MODULE, 5596effad8dfSPaul Moore .pf = PF_INET, 5597effad8dfSPaul Moore .hooknum = NF_INET_FORWARD, 5598effad8dfSPaul Moore .priority = NF_IP_PRI_SELINUX_FIRST, 5599effad8dfSPaul Moore } 56001da177e4SLinus Torvalds }; 56011da177e4SLinus Torvalds 56021da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 56031da177e4SLinus Torvalds 5604effad8dfSPaul Moore static struct nf_hook_ops selinux_ipv6_ops[] = { 5605effad8dfSPaul Moore { 5606effad8dfSPaul Moore .hook = selinux_ipv6_postroute, 56071da177e4SLinus Torvalds .owner = THIS_MODULE, 56081da177e4SLinus Torvalds .pf = PF_INET6, 56096e23ae2aSPatrick McHardy .hooknum = NF_INET_POST_ROUTING, 56101da177e4SLinus Torvalds .priority = NF_IP6_PRI_SELINUX_LAST, 5611effad8dfSPaul Moore }, 5612effad8dfSPaul Moore { 5613effad8dfSPaul Moore .hook = selinux_ipv6_forward, 5614effad8dfSPaul Moore .owner = THIS_MODULE, 5615effad8dfSPaul Moore .pf = PF_INET6, 5616effad8dfSPaul Moore .hooknum = NF_INET_FORWARD, 5617effad8dfSPaul Moore .priority = NF_IP6_PRI_SELINUX_FIRST, 5618effad8dfSPaul Moore } 56191da177e4SLinus Torvalds }; 56201da177e4SLinus Torvalds 56211da177e4SLinus Torvalds #endif /* IPV6 */ 56221da177e4SLinus Torvalds 56231da177e4SLinus Torvalds static int __init selinux_nf_ip_init(void) 56241da177e4SLinus Torvalds { 56251da177e4SLinus Torvalds int err = 0; 5626effad8dfSPaul Moore u32 iter; 56271da177e4SLinus Torvalds 56281da177e4SLinus Torvalds if (!selinux_enabled) 56291da177e4SLinus Torvalds goto out; 56301da177e4SLinus Torvalds 5631fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Registering netfilter hooks\n"); 56321da177e4SLinus Torvalds 5633effad8dfSPaul Moore for (iter = 0; iter < ARRAY_SIZE(selinux_ipv4_ops); iter++) { 5634effad8dfSPaul Moore err = nf_register_hook(&selinux_ipv4_ops[iter]); 56351da177e4SLinus Torvalds if (err) 5636effad8dfSPaul Moore panic("SELinux: nf_register_hook for IPv4: error %d\n", 5637effad8dfSPaul Moore err); 5638effad8dfSPaul Moore } 56391da177e4SLinus Torvalds 56401da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 5641effad8dfSPaul Moore for (iter = 0; iter < ARRAY_SIZE(selinux_ipv6_ops); iter++) { 5642effad8dfSPaul Moore err = nf_register_hook(&selinux_ipv6_ops[iter]); 56431da177e4SLinus Torvalds if (err) 5644effad8dfSPaul Moore panic("SELinux: nf_register_hook for IPv6: error %d\n", 5645effad8dfSPaul Moore err); 5646effad8dfSPaul Moore } 56471da177e4SLinus Torvalds #endif /* IPV6 */ 5648d28d1e08STrent Jaeger 56491da177e4SLinus Torvalds out: 56501da177e4SLinus Torvalds return err; 56511da177e4SLinus Torvalds } 56521da177e4SLinus Torvalds 56531da177e4SLinus Torvalds __initcall(selinux_nf_ip_init); 56541da177e4SLinus Torvalds 56551da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 56561da177e4SLinus Torvalds static void selinux_nf_ip_exit(void) 56571da177e4SLinus Torvalds { 5658effad8dfSPaul Moore u32 iter; 5659effad8dfSPaul Moore 5660fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Unregistering netfilter hooks\n"); 56611da177e4SLinus Torvalds 5662effad8dfSPaul Moore for (iter = 0; iter < ARRAY_SIZE(selinux_ipv4_ops); iter++) 5663effad8dfSPaul Moore nf_unregister_hook(&selinux_ipv4_ops[iter]); 56641da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 5665effad8dfSPaul Moore for (iter = 0; iter < ARRAY_SIZE(selinux_ipv6_ops); iter++) 5666effad8dfSPaul Moore nf_unregister_hook(&selinux_ipv6_ops[iter]); 56671da177e4SLinus Torvalds #endif /* IPV6 */ 56681da177e4SLinus Torvalds } 56691da177e4SLinus Torvalds #endif 56701da177e4SLinus Torvalds 5671c2b507fdSStephen Smalley #else /* CONFIG_NETFILTER */ 56721da177e4SLinus Torvalds 56731da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 56741da177e4SLinus Torvalds #define selinux_nf_ip_exit() 56751da177e4SLinus Torvalds #endif 56761da177e4SLinus Torvalds 5677c2b507fdSStephen Smalley #endif /* CONFIG_NETFILTER */ 56781da177e4SLinus Torvalds 56791da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 5680828dfe1dSEric Paris static int selinux_disabled; 5681828dfe1dSEric Paris 56821da177e4SLinus Torvalds int selinux_disable(void) 56831da177e4SLinus Torvalds { 56841da177e4SLinus Torvalds extern void exit_sel_fs(void); 56851da177e4SLinus Torvalds 56861da177e4SLinus Torvalds if (ss_initialized) { 56871da177e4SLinus Torvalds /* Not permitted after initial policy load. */ 56881da177e4SLinus Torvalds return -EINVAL; 56891da177e4SLinus Torvalds } 56901da177e4SLinus Torvalds 56911da177e4SLinus Torvalds if (selinux_disabled) { 56921da177e4SLinus Torvalds /* Only do this once. */ 56931da177e4SLinus Torvalds return -EINVAL; 56941da177e4SLinus Torvalds } 56951da177e4SLinus Torvalds 56961da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Disabled at runtime.\n"); 56971da177e4SLinus Torvalds 56981da177e4SLinus Torvalds selinux_disabled = 1; 569930d55280SStephen Smalley selinux_enabled = 0; 57001da177e4SLinus Torvalds 57011da177e4SLinus Torvalds /* Reset security_ops to the secondary module, dummy or capability. */ 57021da177e4SLinus Torvalds security_ops = secondary_ops; 57031da177e4SLinus Torvalds 57041da177e4SLinus Torvalds /* Unregister netfilter hooks. */ 57051da177e4SLinus Torvalds selinux_nf_ip_exit(); 57061da177e4SLinus Torvalds 57071da177e4SLinus Torvalds /* Unregister selinuxfs. */ 57081da177e4SLinus Torvalds exit_sel_fs(); 57091da177e4SLinus Torvalds 57101da177e4SLinus Torvalds return 0; 57111da177e4SLinus Torvalds } 57121da177e4SLinus Torvalds #endif 5713