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. 122069f457SEric Paris * Copyright (C) 2003-2008 Red Hat, Inc., James Morris <jmorris@redhat.com> 132069f457SEric Paris * Eric Paris <eparis@redhat.com> 141da177e4SLinus Torvalds * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc. 151da177e4SLinus Torvalds * <dgoeddel@trustedcs.com> 16ed6d76e4SPaul Moore * Copyright (C) 2006, 2007, 2009 Hewlett-Packard Development Company, L.P. 17effad8dfSPaul Moore * Paul Moore <paul.moore@hp.com> 18788e7dd4SYuichi Nakamura * Copyright (C) 2007 Hitachi Software Engineering Co., Ltd. 19788e7dd4SYuichi Nakamura * Yuichi Nakamura <ynakam@hitachisoft.jp> 201da177e4SLinus Torvalds * 211da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 221da177e4SLinus Torvalds * it under the terms of the GNU General Public License version 2, 231da177e4SLinus Torvalds * as published by the Free Software Foundation. 241da177e4SLinus Torvalds */ 251da177e4SLinus Torvalds 261da177e4SLinus Torvalds #include <linux/init.h> 270b24dcb7SEric Paris #include <linux/kd.h> 281da177e4SLinus Torvalds #include <linux/kernel.h> 290d094efeSRoland McGrath #include <linux/tracehook.h> 301da177e4SLinus Torvalds #include <linux/errno.h> 310b24dcb7SEric Paris #include <linux/ext2_fs.h> 321da177e4SLinus Torvalds #include <linux/sched.h> 331da177e4SLinus Torvalds #include <linux/security.h> 341da177e4SLinus Torvalds #include <linux/xattr.h> 351da177e4SLinus Torvalds #include <linux/capability.h> 361da177e4SLinus Torvalds #include <linux/unistd.h> 371da177e4SLinus Torvalds #include <linux/mm.h> 381da177e4SLinus Torvalds #include <linux/mman.h> 391da177e4SLinus Torvalds #include <linux/slab.h> 401da177e4SLinus Torvalds #include <linux/pagemap.h> 410b24dcb7SEric Paris #include <linux/proc_fs.h> 421da177e4SLinus Torvalds #include <linux/swap.h> 431da177e4SLinus Torvalds #include <linux/spinlock.h> 441da177e4SLinus Torvalds #include <linux/syscalls.h> 452a7dba39SEric Paris #include <linux/dcache.h> 461da177e4SLinus Torvalds #include <linux/file.h> 479f3acc31SAl Viro #include <linux/fdtable.h> 481da177e4SLinus Torvalds #include <linux/namei.h> 491da177e4SLinus Torvalds #include <linux/mount.h> 501da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h> 511da177e4SLinus Torvalds #include <linux/netfilter_ipv6.h> 521da177e4SLinus Torvalds #include <linux/tty.h> 531da177e4SLinus Torvalds #include <net/icmp.h> 54227b60f5SStephen Hemminger #include <net/ip.h> /* for local_port_range[] */ 551da177e4SLinus Torvalds #include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */ 56220deb96SPaul Moore #include <net/net_namespace.h> 57d621d35eSPaul Moore #include <net/netlabel.h> 58f5269710SEric Paris #include <linux/uaccess.h> 591da177e4SLinus Torvalds #include <asm/ioctls.h> 60d621d35eSPaul Moore #include <asm/atomic.h> 611da177e4SLinus Torvalds #include <linux/bitops.h> 621da177e4SLinus Torvalds #include <linux/interrupt.h> 631da177e4SLinus Torvalds #include <linux/netdevice.h> /* for network interface checks */ 641da177e4SLinus Torvalds #include <linux/netlink.h> 651da177e4SLinus Torvalds #include <linux/tcp.h> 661da177e4SLinus Torvalds #include <linux/udp.h> 672ee92d46SJames Morris #include <linux/dccp.h> 681da177e4SLinus Torvalds #include <linux/quota.h> 691da177e4SLinus Torvalds #include <linux/un.h> /* for Unix socket types */ 701da177e4SLinus Torvalds #include <net/af_unix.h> /* for Unix socket types */ 711da177e4SLinus Torvalds #include <linux/parser.h> 721da177e4SLinus Torvalds #include <linux/nfs_mount.h> 731da177e4SLinus Torvalds #include <net/ipv6.h> 741da177e4SLinus Torvalds #include <linux/hugetlb.h> 751da177e4SLinus Torvalds #include <linux/personality.h> 761da177e4SLinus Torvalds #include <linux/audit.h> 776931dfc9SEric Paris #include <linux/string.h> 78877ce7c1SCatherine Zhang #include <linux/selinux.h> 7923970741SEric Paris #include <linux/mutex.h> 80f06febc9SFrank Mayhar #include <linux/posix-timers.h> 8100234592SKees Cook #include <linux/syslog.h> 821da177e4SLinus Torvalds 831da177e4SLinus Torvalds #include "avc.h" 841da177e4SLinus Torvalds #include "objsec.h" 851da177e4SLinus Torvalds #include "netif.h" 86224dfbd8SPaul Moore #include "netnode.h" 873e112172SPaul Moore #include "netport.h" 88d28d1e08STrent Jaeger #include "xfrm.h" 89c60475bfSPaul Moore #include "netlabel.h" 909d57a7f9SAhmed S. Darwish #include "audit.h" 911da177e4SLinus Torvalds 9211689d47SDavid P. Quigley #define NUM_SEL_MNT_OPTS 5 93c9180a57SEric Paris 941da177e4SLinus Torvalds extern int selinux_nlmsg_lookup(u16 sclass, u16 nlmsg_type, u32 *perm); 9520510f2fSJames Morris extern struct security_operations *security_ops; 961da177e4SLinus Torvalds 97d621d35eSPaul Moore /* SECMARK reference count */ 98d621d35eSPaul Moore atomic_t selinux_secmark_refcount = ATOMIC_INIT(0); 99d621d35eSPaul Moore 1001da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP 101828dfe1dSEric Paris int selinux_enforcing; 1021da177e4SLinus Torvalds 1031da177e4SLinus Torvalds static int __init enforcing_setup(char *str) 1041da177e4SLinus Torvalds { 105f5269710SEric Paris unsigned long enforcing; 106f5269710SEric Paris if (!strict_strtoul(str, 0, &enforcing)) 107f5269710SEric Paris selinux_enforcing = enforcing ? 1 : 0; 1081da177e4SLinus Torvalds return 1; 1091da177e4SLinus Torvalds } 1101da177e4SLinus Torvalds __setup("enforcing=", enforcing_setup); 1111da177e4SLinus Torvalds #endif 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM 1141da177e4SLinus Torvalds int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE; 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds static int __init selinux_enabled_setup(char *str) 1171da177e4SLinus Torvalds { 118f5269710SEric Paris unsigned long enabled; 119f5269710SEric Paris if (!strict_strtoul(str, 0, &enabled)) 120f5269710SEric Paris selinux_enabled = enabled ? 1 : 0; 1211da177e4SLinus Torvalds return 1; 1221da177e4SLinus Torvalds } 1231da177e4SLinus Torvalds __setup("selinux=", selinux_enabled_setup); 12430d55280SStephen Smalley #else 12530d55280SStephen Smalley int selinux_enabled = 1; 1261da177e4SLinus Torvalds #endif 1271da177e4SLinus Torvalds 128e18b890bSChristoph Lameter static struct kmem_cache *sel_inode_cache; 1297cae7e26SJames Morris 130d621d35eSPaul Moore /** 131d621d35eSPaul Moore * selinux_secmark_enabled - Check to see if SECMARK is currently enabled 132d621d35eSPaul Moore * 133d621d35eSPaul Moore * Description: 134d621d35eSPaul Moore * This function checks the SECMARK reference counter to see if any SECMARK 135d621d35eSPaul Moore * targets are currently configured, if the reference counter is greater than 136d621d35eSPaul Moore * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is 137d621d35eSPaul Moore * enabled, false (0) if SECMARK is disabled. 138d621d35eSPaul Moore * 139d621d35eSPaul Moore */ 140d621d35eSPaul Moore static int selinux_secmark_enabled(void) 141d621d35eSPaul Moore { 142d621d35eSPaul Moore return (atomic_read(&selinux_secmark_refcount) > 0); 143d621d35eSPaul Moore } 144d621d35eSPaul Moore 145d84f4f99SDavid Howells /* 146d84f4f99SDavid Howells * initialise the security for the init task 147d84f4f99SDavid Howells */ 148d84f4f99SDavid Howells static void cred_init_security(void) 1491da177e4SLinus Torvalds { 1503b11a1deSDavid Howells struct cred *cred = (struct cred *) current->real_cred; 1511da177e4SLinus Torvalds struct task_security_struct *tsec; 1521da177e4SLinus Torvalds 15389d155efSJames Morris tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL); 1541da177e4SLinus Torvalds if (!tsec) 155d84f4f99SDavid Howells panic("SELinux: Failed to initialize initial task.\n"); 1561da177e4SLinus Torvalds 157d84f4f99SDavid Howells tsec->osid = tsec->sid = SECINITSID_KERNEL; 158f1752eecSDavid Howells cred->security = tsec; 1591da177e4SLinus Torvalds } 1601da177e4SLinus Torvalds 161275bb41eSDavid Howells /* 16288e67f3bSDavid Howells * get the security ID of a set of credentials 16388e67f3bSDavid Howells */ 16488e67f3bSDavid Howells static inline u32 cred_sid(const struct cred *cred) 16588e67f3bSDavid Howells { 16688e67f3bSDavid Howells const struct task_security_struct *tsec; 16788e67f3bSDavid Howells 16888e67f3bSDavid Howells tsec = cred->security; 16988e67f3bSDavid Howells return tsec->sid; 17088e67f3bSDavid Howells } 17188e67f3bSDavid Howells 17288e67f3bSDavid Howells /* 1733b11a1deSDavid Howells * get the objective security ID of a task 174275bb41eSDavid Howells */ 175275bb41eSDavid Howells static inline u32 task_sid(const struct task_struct *task) 176275bb41eSDavid Howells { 177275bb41eSDavid Howells u32 sid; 178275bb41eSDavid Howells 179275bb41eSDavid Howells rcu_read_lock(); 18088e67f3bSDavid Howells sid = cred_sid(__task_cred(task)); 181275bb41eSDavid Howells rcu_read_unlock(); 182275bb41eSDavid Howells return sid; 183275bb41eSDavid Howells } 184275bb41eSDavid Howells 185275bb41eSDavid Howells /* 1863b11a1deSDavid Howells * get the subjective security ID of the current task 187275bb41eSDavid Howells */ 188275bb41eSDavid Howells static inline u32 current_sid(void) 189275bb41eSDavid Howells { 1905fb49870SPaul Moore const struct task_security_struct *tsec = current_security(); 191275bb41eSDavid Howells 192275bb41eSDavid Howells return tsec->sid; 193275bb41eSDavid Howells } 194275bb41eSDavid Howells 19588e67f3bSDavid Howells /* Allocate and free functions for each kind of security blob. */ 19688e67f3bSDavid Howells 1971da177e4SLinus Torvalds static int inode_alloc_security(struct inode *inode) 1981da177e4SLinus Torvalds { 1991da177e4SLinus Torvalds struct inode_security_struct *isec; 200275bb41eSDavid Howells u32 sid = current_sid(); 2011da177e4SLinus Torvalds 202a02fe132SJosef Bacik isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS); 2031da177e4SLinus Torvalds if (!isec) 2041da177e4SLinus Torvalds return -ENOMEM; 2051da177e4SLinus Torvalds 20623970741SEric Paris mutex_init(&isec->lock); 2071da177e4SLinus Torvalds INIT_LIST_HEAD(&isec->list); 2081da177e4SLinus Torvalds isec->inode = inode; 2091da177e4SLinus Torvalds isec->sid = SECINITSID_UNLABELED; 2101da177e4SLinus Torvalds isec->sclass = SECCLASS_FILE; 211275bb41eSDavid Howells isec->task_sid = sid; 2121da177e4SLinus Torvalds inode->i_security = isec; 2131da177e4SLinus Torvalds 2141da177e4SLinus Torvalds return 0; 2151da177e4SLinus Torvalds } 2161da177e4SLinus Torvalds 2171da177e4SLinus Torvalds static void inode_free_security(struct inode *inode) 2181da177e4SLinus Torvalds { 2191da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 2201da177e4SLinus Torvalds struct superblock_security_struct *sbsec = inode->i_sb->s_security; 2211da177e4SLinus Torvalds 2221da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 2231da177e4SLinus Torvalds if (!list_empty(&isec->list)) 2241da177e4SLinus Torvalds list_del_init(&isec->list); 2251da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 2261da177e4SLinus Torvalds 2271da177e4SLinus Torvalds inode->i_security = NULL; 2287cae7e26SJames Morris kmem_cache_free(sel_inode_cache, isec); 2291da177e4SLinus Torvalds } 2301da177e4SLinus Torvalds 2311da177e4SLinus Torvalds static int file_alloc_security(struct file *file) 2321da177e4SLinus Torvalds { 2331da177e4SLinus Torvalds struct file_security_struct *fsec; 234275bb41eSDavid Howells u32 sid = current_sid(); 2351da177e4SLinus Torvalds 23626d2a4beSStephen Smalley fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL); 2371da177e4SLinus Torvalds if (!fsec) 2381da177e4SLinus Torvalds return -ENOMEM; 2391da177e4SLinus Torvalds 240275bb41eSDavid Howells fsec->sid = sid; 241275bb41eSDavid Howells fsec->fown_sid = sid; 2421da177e4SLinus Torvalds file->f_security = fsec; 2431da177e4SLinus Torvalds 2441da177e4SLinus Torvalds return 0; 2451da177e4SLinus Torvalds } 2461da177e4SLinus Torvalds 2471da177e4SLinus Torvalds static void file_free_security(struct file *file) 2481da177e4SLinus Torvalds { 2491da177e4SLinus Torvalds struct file_security_struct *fsec = file->f_security; 2501da177e4SLinus Torvalds file->f_security = NULL; 2511da177e4SLinus Torvalds kfree(fsec); 2521da177e4SLinus Torvalds } 2531da177e4SLinus Torvalds 2541da177e4SLinus Torvalds static int superblock_alloc_security(struct super_block *sb) 2551da177e4SLinus Torvalds { 2561da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 2571da177e4SLinus Torvalds 25889d155efSJames Morris sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL); 2591da177e4SLinus Torvalds if (!sbsec) 2601da177e4SLinus Torvalds return -ENOMEM; 2611da177e4SLinus Torvalds 262bc7e982bSEric Paris mutex_init(&sbsec->lock); 2631da177e4SLinus Torvalds INIT_LIST_HEAD(&sbsec->isec_head); 2641da177e4SLinus Torvalds spin_lock_init(&sbsec->isec_lock); 2651da177e4SLinus Torvalds sbsec->sb = sb; 2661da177e4SLinus Torvalds sbsec->sid = SECINITSID_UNLABELED; 2671da177e4SLinus Torvalds sbsec->def_sid = SECINITSID_FILE; 268c312feb2SEric Paris sbsec->mntpoint_sid = SECINITSID_UNLABELED; 2691da177e4SLinus Torvalds sb->s_security = sbsec; 2701da177e4SLinus Torvalds 2711da177e4SLinus Torvalds return 0; 2721da177e4SLinus Torvalds } 2731da177e4SLinus Torvalds 2741da177e4SLinus Torvalds static void superblock_free_security(struct super_block *sb) 2751da177e4SLinus Torvalds { 2761da177e4SLinus Torvalds struct superblock_security_struct *sbsec = sb->s_security; 2771da177e4SLinus Torvalds sb->s_security = NULL; 2781da177e4SLinus Torvalds kfree(sbsec); 2791da177e4SLinus Torvalds } 2801da177e4SLinus Torvalds 2811da177e4SLinus Torvalds /* The security server must be initialized before 2821da177e4SLinus Torvalds any labeling or access decisions can be provided. */ 2831da177e4SLinus Torvalds extern int ss_initialized; 2841da177e4SLinus Torvalds 2851da177e4SLinus Torvalds /* The file system's label must be initialized prior to use. */ 2861da177e4SLinus Torvalds 287634a539eSStephen Hemminger static const char *labeling_behaviors[6] = { 2881da177e4SLinus Torvalds "uses xattr", 2891da177e4SLinus Torvalds "uses transition SIDs", 2901da177e4SLinus Torvalds "uses task SIDs", 2911da177e4SLinus Torvalds "uses genfs_contexts", 2921da177e4SLinus Torvalds "not configured for labeling", 2931da177e4SLinus Torvalds "uses mountpoint labeling", 2941da177e4SLinus Torvalds }; 2951da177e4SLinus Torvalds 2961da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry); 2971da177e4SLinus Torvalds 2981da177e4SLinus Torvalds static inline int inode_doinit(struct inode *inode) 2991da177e4SLinus Torvalds { 3001da177e4SLinus Torvalds return inode_doinit_with_dentry(inode, NULL); 3011da177e4SLinus Torvalds } 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds enum { 30431e87930SEric Paris Opt_error = -1, 3051da177e4SLinus Torvalds Opt_context = 1, 3061da177e4SLinus Torvalds Opt_fscontext = 2, 307c9180a57SEric Paris Opt_defcontext = 3, 308c9180a57SEric Paris Opt_rootcontext = 4, 30911689d47SDavid P. Quigley Opt_labelsupport = 5, 3101da177e4SLinus Torvalds }; 3111da177e4SLinus Torvalds 312a447c093SSteven Whitehouse static const match_table_t tokens = { 313832cbd9aSEric Paris {Opt_context, CONTEXT_STR "%s"}, 314832cbd9aSEric Paris {Opt_fscontext, FSCONTEXT_STR "%s"}, 315832cbd9aSEric Paris {Opt_defcontext, DEFCONTEXT_STR "%s"}, 316832cbd9aSEric Paris {Opt_rootcontext, ROOTCONTEXT_STR "%s"}, 31711689d47SDavid P. Quigley {Opt_labelsupport, LABELSUPP_STR}, 31831e87930SEric Paris {Opt_error, NULL}, 3191da177e4SLinus Torvalds }; 3201da177e4SLinus Torvalds 3211da177e4SLinus Torvalds #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n" 3221da177e4SLinus Torvalds 323c312feb2SEric Paris static int may_context_mount_sb_relabel(u32 sid, 324c312feb2SEric Paris struct superblock_security_struct *sbsec, 325275bb41eSDavid Howells const struct cred *cred) 326c312feb2SEric Paris { 327275bb41eSDavid Howells const struct task_security_struct *tsec = cred->security; 328c312feb2SEric Paris int rc; 329c312feb2SEric Paris 330c312feb2SEric Paris rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 331c312feb2SEric Paris FILESYSTEM__RELABELFROM, NULL); 332c312feb2SEric Paris if (rc) 333c312feb2SEric Paris return rc; 334c312feb2SEric Paris 335c312feb2SEric Paris rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM, 336c312feb2SEric Paris FILESYSTEM__RELABELTO, NULL); 337c312feb2SEric Paris return rc; 338c312feb2SEric Paris } 339c312feb2SEric Paris 3400808925eSEric Paris static int may_context_mount_inode_relabel(u32 sid, 3410808925eSEric Paris struct superblock_security_struct *sbsec, 342275bb41eSDavid Howells const struct cred *cred) 3430808925eSEric Paris { 344275bb41eSDavid Howells const struct task_security_struct *tsec = cred->security; 3450808925eSEric Paris int rc; 3460808925eSEric Paris rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 3470808925eSEric Paris FILESYSTEM__RELABELFROM, NULL); 3480808925eSEric Paris if (rc) 3490808925eSEric Paris return rc; 3500808925eSEric Paris 3510808925eSEric Paris rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, 3520808925eSEric Paris FILESYSTEM__ASSOCIATE, NULL); 3530808925eSEric Paris return rc; 3540808925eSEric Paris } 3550808925eSEric Paris 356c9180a57SEric Paris static int sb_finish_set_opts(struct super_block *sb) 3571da177e4SLinus Torvalds { 3581da177e4SLinus Torvalds struct superblock_security_struct *sbsec = sb->s_security; 3591da177e4SLinus Torvalds struct dentry *root = sb->s_root; 360c9180a57SEric Paris struct inode *root_inode = root->d_inode; 3611da177e4SLinus Torvalds int rc = 0; 3621da177e4SLinus Torvalds 3631da177e4SLinus Torvalds if (sbsec->behavior == SECURITY_FS_USE_XATTR) { 3641da177e4SLinus Torvalds /* Make sure that the xattr handler exists and that no 3651da177e4SLinus Torvalds error other than -ENODATA is returned by getxattr on 3661da177e4SLinus Torvalds the root directory. -ENODATA is ok, as this may be 3671da177e4SLinus Torvalds the first boot of the SELinux kernel before we have 3681da177e4SLinus Torvalds assigned xattr values to the filesystem. */ 369c9180a57SEric Paris if (!root_inode->i_op->getxattr) { 3701da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: (dev %s, type %s) has no " 3711da177e4SLinus Torvalds "xattr support\n", sb->s_id, sb->s_type->name); 3721da177e4SLinus Torvalds rc = -EOPNOTSUPP; 3731da177e4SLinus Torvalds goto out; 3741da177e4SLinus Torvalds } 375c9180a57SEric Paris rc = root_inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0); 3761da177e4SLinus Torvalds if (rc < 0 && rc != -ENODATA) { 3771da177e4SLinus Torvalds if (rc == -EOPNOTSUPP) 3781da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: (dev %s, type " 3791da177e4SLinus Torvalds "%s) has no security xattr handler\n", 3801da177e4SLinus Torvalds sb->s_id, sb->s_type->name); 3811da177e4SLinus Torvalds else 3821da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: (dev %s, type " 3831da177e4SLinus Torvalds "%s) getxattr errno %d\n", sb->s_id, 3841da177e4SLinus Torvalds sb->s_type->name, -rc); 3851da177e4SLinus Torvalds goto out; 3861da177e4SLinus Torvalds } 3871da177e4SLinus Torvalds } 3881da177e4SLinus Torvalds 38911689d47SDavid P. Quigley sbsec->flags |= (SE_SBINITIALIZED | SE_SBLABELSUPP); 3901da177e4SLinus Torvalds 391c9180a57SEric Paris if (sbsec->behavior > ARRAY_SIZE(labeling_behaviors)) 392fadcdb45SEric Paris printk(KERN_ERR "SELinux: initialized (dev %s, type %s), unknown behavior\n", 3931da177e4SLinus Torvalds sb->s_id, sb->s_type->name); 394c9180a57SEric Paris else 395fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: initialized (dev %s, type %s), %s\n", 3961da177e4SLinus Torvalds sb->s_id, sb->s_type->name, 3971da177e4SLinus Torvalds labeling_behaviors[sbsec->behavior-1]); 3981da177e4SLinus Torvalds 39911689d47SDavid P. Quigley if (sbsec->behavior == SECURITY_FS_USE_GENFS || 40011689d47SDavid P. Quigley sbsec->behavior == SECURITY_FS_USE_MNTPOINT || 40111689d47SDavid P. Quigley sbsec->behavior == SECURITY_FS_USE_NONE || 40211689d47SDavid P. Quigley sbsec->behavior > ARRAY_SIZE(labeling_behaviors)) 40311689d47SDavid P. Quigley sbsec->flags &= ~SE_SBLABELSUPP; 40411689d47SDavid P. Quigley 405ddd29ec6SDavid P. Quigley /* Special handling for sysfs. Is genfs but also has setxattr handler*/ 406ddd29ec6SDavid P. Quigley if (strncmp(sb->s_type->name, "sysfs", sizeof("sysfs")) == 0) 407ddd29ec6SDavid P. Quigley sbsec->flags |= SE_SBLABELSUPP; 408ddd29ec6SDavid P. Quigley 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 4550d90a7ecSDavid P. Quigley if (!(sbsec->flags & SE_SBINITIALIZED)) 456c9180a57SEric Paris return -EINVAL; 457c9180a57SEric Paris 458c9180a57SEric Paris if (!ss_initialized) 459c9180a57SEric Paris return -EINVAL; 460c9180a57SEric Paris 4610d90a7ecSDavid P. Quigley tmp = sbsec->flags & SE_MNTMASK; 462c9180a57SEric Paris /* count the number of mount options for this sb */ 463c9180a57SEric Paris for (i = 0; i < 8; i++) { 464c9180a57SEric Paris if (tmp & 0x01) 465e0007529SEric Paris opts->num_mnt_opts++; 466c9180a57SEric Paris tmp >>= 1; 467c9180a57SEric Paris } 46811689d47SDavid P. Quigley /* Check if the Label support flag is set */ 46911689d47SDavid P. Quigley if (sbsec->flags & SE_SBLABELSUPP) 47011689d47SDavid P. Quigley opts->num_mnt_opts++; 471c9180a57SEric Paris 472e0007529SEric Paris opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC); 473e0007529SEric Paris if (!opts->mnt_opts) { 474c9180a57SEric Paris rc = -ENOMEM; 475c9180a57SEric Paris goto out_free; 476c9180a57SEric Paris } 477c9180a57SEric Paris 478e0007529SEric Paris opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC); 479e0007529SEric Paris if (!opts->mnt_opts_flags) { 480c9180a57SEric Paris rc = -ENOMEM; 481c9180a57SEric Paris goto out_free; 482c9180a57SEric Paris } 483c9180a57SEric Paris 484c9180a57SEric Paris i = 0; 485c9180a57SEric Paris if (sbsec->flags & FSCONTEXT_MNT) { 486c9180a57SEric Paris rc = security_sid_to_context(sbsec->sid, &context, &len); 487c9180a57SEric Paris if (rc) 488c9180a57SEric Paris goto out_free; 489e0007529SEric Paris opts->mnt_opts[i] = context; 490e0007529SEric Paris opts->mnt_opts_flags[i++] = FSCONTEXT_MNT; 491c9180a57SEric Paris } 492c9180a57SEric Paris if (sbsec->flags & CONTEXT_MNT) { 493c9180a57SEric Paris rc = security_sid_to_context(sbsec->mntpoint_sid, &context, &len); 494c9180a57SEric Paris if (rc) 495c9180a57SEric Paris goto out_free; 496e0007529SEric Paris opts->mnt_opts[i] = context; 497e0007529SEric Paris opts->mnt_opts_flags[i++] = CONTEXT_MNT; 498c9180a57SEric Paris } 499c9180a57SEric Paris if (sbsec->flags & DEFCONTEXT_MNT) { 500c9180a57SEric Paris rc = security_sid_to_context(sbsec->def_sid, &context, &len); 501c9180a57SEric Paris if (rc) 502c9180a57SEric Paris goto out_free; 503e0007529SEric Paris opts->mnt_opts[i] = context; 504e0007529SEric Paris opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT; 505c9180a57SEric Paris } 506c9180a57SEric Paris if (sbsec->flags & ROOTCONTEXT_MNT) { 507c9180a57SEric Paris struct inode *root = sbsec->sb->s_root->d_inode; 508c9180a57SEric Paris struct inode_security_struct *isec = root->i_security; 509c9180a57SEric Paris 510c9180a57SEric Paris rc = security_sid_to_context(isec->sid, &context, &len); 511c9180a57SEric Paris if (rc) 512c9180a57SEric Paris goto out_free; 513e0007529SEric Paris opts->mnt_opts[i] = context; 514e0007529SEric Paris opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT; 515c9180a57SEric Paris } 51611689d47SDavid P. Quigley if (sbsec->flags & SE_SBLABELSUPP) { 51711689d47SDavid P. Quigley opts->mnt_opts[i] = NULL; 51811689d47SDavid P. Quigley opts->mnt_opts_flags[i++] = SE_SBLABELSUPP; 51911689d47SDavid P. Quigley } 520c9180a57SEric Paris 521e0007529SEric Paris BUG_ON(i != opts->num_mnt_opts); 522c9180a57SEric Paris 523c9180a57SEric Paris return 0; 524c9180a57SEric Paris 525c9180a57SEric Paris out_free: 526e0007529SEric Paris security_free_mnt_opts(opts); 527c9180a57SEric Paris return rc; 528c9180a57SEric Paris } 529c9180a57SEric Paris 530c9180a57SEric Paris static int bad_option(struct superblock_security_struct *sbsec, char flag, 531c9180a57SEric Paris u32 old_sid, u32 new_sid) 532c9180a57SEric Paris { 5330d90a7ecSDavid P. Quigley char mnt_flags = sbsec->flags & SE_MNTMASK; 5340d90a7ecSDavid P. Quigley 535c9180a57SEric Paris /* check if the old mount command had the same options */ 5360d90a7ecSDavid P. Quigley if (sbsec->flags & SE_SBINITIALIZED) 537c9180a57SEric Paris if (!(sbsec->flags & flag) || 538c9180a57SEric Paris (old_sid != new_sid)) 539c9180a57SEric Paris return 1; 540c9180a57SEric Paris 541c9180a57SEric Paris /* check if we were passed the same options twice, 542c9180a57SEric Paris * aka someone passed context=a,context=b 543c9180a57SEric Paris */ 5440d90a7ecSDavid P. Quigley if (!(sbsec->flags & SE_SBINITIALIZED)) 5450d90a7ecSDavid P. Quigley if (mnt_flags & flag) 546c9180a57SEric Paris return 1; 547c9180a57SEric Paris return 0; 548c9180a57SEric Paris } 549e0007529SEric Paris 550c9180a57SEric Paris /* 551c9180a57SEric Paris * Allow filesystems with binary mount data to explicitly set mount point 552c9180a57SEric Paris * labeling information. 553c9180a57SEric Paris */ 554e0007529SEric Paris static int selinux_set_mnt_opts(struct super_block *sb, 555e0007529SEric Paris struct security_mnt_opts *opts) 556c9180a57SEric Paris { 557275bb41eSDavid Howells const struct cred *cred = current_cred(); 558c9180a57SEric Paris int rc = 0, i; 559c9180a57SEric Paris struct superblock_security_struct *sbsec = sb->s_security; 560c9180a57SEric Paris const char *name = sb->s_type->name; 561089be43eSJames Morris struct inode *inode = sbsec->sb->s_root->d_inode; 562089be43eSJames Morris struct inode_security_struct *root_isec = inode->i_security; 563c9180a57SEric Paris u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0; 564c9180a57SEric Paris u32 defcontext_sid = 0; 565e0007529SEric Paris char **mount_options = opts->mnt_opts; 566e0007529SEric Paris int *flags = opts->mnt_opts_flags; 567e0007529SEric Paris int num_opts = opts->num_mnt_opts; 568c9180a57SEric Paris 569c9180a57SEric Paris mutex_lock(&sbsec->lock); 570c9180a57SEric Paris 571c9180a57SEric Paris if (!ss_initialized) { 572c9180a57SEric Paris if (!num_opts) { 573c9180a57SEric Paris /* Defer initialization until selinux_complete_init, 574c9180a57SEric Paris after the initial policy is loaded and the security 575c9180a57SEric Paris server is ready to handle calls. */ 576c9180a57SEric Paris goto out; 577c9180a57SEric Paris } 578c9180a57SEric Paris rc = -EINVAL; 579744ba35eSEric Paris printk(KERN_WARNING "SELinux: Unable to set superblock options " 580744ba35eSEric Paris "before the security server is initialized\n"); 581c9180a57SEric Paris goto out; 582c9180a57SEric Paris } 583c9180a57SEric Paris 584c9180a57SEric Paris /* 585e0007529SEric Paris * Binary mount data FS will come through this function twice. Once 586e0007529SEric Paris * from an explicit call and once from the generic calls from the vfs. 587e0007529SEric Paris * Since the generic VFS calls will not contain any security mount data 588e0007529SEric Paris * we need to skip the double mount verification. 589e0007529SEric Paris * 590e0007529SEric Paris * This does open a hole in which we will not notice if the first 591e0007529SEric Paris * mount using this sb set explict options and a second mount using 592e0007529SEric Paris * this sb does not set any security options. (The first options 593e0007529SEric Paris * will be used for both mounts) 594e0007529SEric Paris */ 5950d90a7ecSDavid P. Quigley if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA) 596e0007529SEric Paris && (num_opts == 0)) 597e0007529SEric Paris goto out; 598e0007529SEric Paris 599e0007529SEric Paris /* 600c9180a57SEric Paris * parse the mount options, check if they are valid sids. 601c9180a57SEric Paris * also check if someone is trying to mount the same sb more 602c9180a57SEric Paris * than once with different security options. 603c9180a57SEric Paris */ 604c9180a57SEric Paris for (i = 0; i < num_opts; i++) { 605c9180a57SEric Paris u32 sid; 60611689d47SDavid P. Quigley 60711689d47SDavid P. Quigley if (flags[i] == SE_SBLABELSUPP) 60811689d47SDavid P. Quigley continue; 609c9180a57SEric Paris rc = security_context_to_sid(mount_options[i], 610c9180a57SEric Paris strlen(mount_options[i]), &sid); 611c9180a57SEric Paris if (rc) { 612c9180a57SEric Paris printk(KERN_WARNING "SELinux: security_context_to_sid" 613c9180a57SEric Paris "(%s) failed for (dev %s, type %s) errno=%d\n", 614c9180a57SEric Paris mount_options[i], sb->s_id, name, rc); 615c9180a57SEric Paris goto out; 616c9180a57SEric Paris } 617c9180a57SEric Paris switch (flags[i]) { 618c9180a57SEric Paris case FSCONTEXT_MNT: 619c9180a57SEric Paris fscontext_sid = sid; 620c9180a57SEric Paris 621c9180a57SEric Paris if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, 622c9180a57SEric Paris fscontext_sid)) 623c9180a57SEric Paris goto out_double_mount; 624c9180a57SEric Paris 625c9180a57SEric Paris sbsec->flags |= FSCONTEXT_MNT; 626c9180a57SEric Paris break; 627c9180a57SEric Paris case CONTEXT_MNT: 628c9180a57SEric Paris context_sid = sid; 629c9180a57SEric Paris 630c9180a57SEric Paris if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, 631c9180a57SEric Paris context_sid)) 632c9180a57SEric Paris goto out_double_mount; 633c9180a57SEric Paris 634c9180a57SEric Paris sbsec->flags |= CONTEXT_MNT; 635c9180a57SEric Paris break; 636c9180a57SEric Paris case ROOTCONTEXT_MNT: 637c9180a57SEric Paris rootcontext_sid = sid; 638c9180a57SEric Paris 639c9180a57SEric Paris if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, 640c9180a57SEric Paris rootcontext_sid)) 641c9180a57SEric Paris goto out_double_mount; 642c9180a57SEric Paris 643c9180a57SEric Paris sbsec->flags |= ROOTCONTEXT_MNT; 644c9180a57SEric Paris 645c9180a57SEric Paris break; 646c9180a57SEric Paris case DEFCONTEXT_MNT: 647c9180a57SEric Paris defcontext_sid = sid; 648c9180a57SEric Paris 649c9180a57SEric Paris if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, 650c9180a57SEric Paris defcontext_sid)) 651c9180a57SEric Paris goto out_double_mount; 652c9180a57SEric Paris 653c9180a57SEric Paris sbsec->flags |= DEFCONTEXT_MNT; 654c9180a57SEric Paris 655c9180a57SEric Paris break; 656c9180a57SEric Paris default: 657c9180a57SEric Paris rc = -EINVAL; 658c9180a57SEric Paris goto out; 659c9180a57SEric Paris } 660c9180a57SEric Paris } 661c9180a57SEric Paris 6620d90a7ecSDavid P. Quigley if (sbsec->flags & SE_SBINITIALIZED) { 663c9180a57SEric Paris /* previously mounted with options, but not on this attempt? */ 6640d90a7ecSDavid P. Quigley if ((sbsec->flags & SE_MNTMASK) && !num_opts) 665c9180a57SEric Paris goto out_double_mount; 666c9180a57SEric Paris rc = 0; 667c9180a57SEric Paris goto out; 668c9180a57SEric Paris } 669c9180a57SEric Paris 670089be43eSJames Morris if (strcmp(sb->s_type->name, "proc") == 0) 6710d90a7ecSDavid P. Quigley sbsec->flags |= SE_SBPROC; 672c9180a57SEric Paris 673c9180a57SEric Paris /* Determine the labeling behavior to use for this filesystem type. */ 6740d90a7ecSDavid P. Quigley rc = security_fs_use((sbsec->flags & SE_SBPROC) ? "proc" : sb->s_type->name, &sbsec->behavior, &sbsec->sid); 675c9180a57SEric Paris if (rc) { 676c9180a57SEric Paris printk(KERN_WARNING "%s: security_fs_use(%s) returned %d\n", 677089be43eSJames Morris __func__, sb->s_type->name, rc); 678c9180a57SEric Paris goto out; 679c9180a57SEric Paris } 680c9180a57SEric Paris 681c9180a57SEric Paris /* sets the context of the superblock for the fs being mounted. */ 682c9180a57SEric Paris if (fscontext_sid) { 683275bb41eSDavid Howells rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred); 684c9180a57SEric Paris if (rc) 685c9180a57SEric Paris goto out; 686c9180a57SEric Paris 687c9180a57SEric Paris sbsec->sid = fscontext_sid; 688c9180a57SEric Paris } 689c9180a57SEric Paris 690c9180a57SEric Paris /* 691c9180a57SEric Paris * Switch to using mount point labeling behavior. 692c9180a57SEric Paris * sets the label used on all file below the mountpoint, and will set 693c9180a57SEric Paris * the superblock context if not already set. 694c9180a57SEric Paris */ 695c9180a57SEric Paris if (context_sid) { 696c9180a57SEric Paris if (!fscontext_sid) { 697275bb41eSDavid Howells rc = may_context_mount_sb_relabel(context_sid, sbsec, 698275bb41eSDavid Howells cred); 699c9180a57SEric Paris if (rc) 700c9180a57SEric Paris goto out; 701c9180a57SEric Paris sbsec->sid = context_sid; 702c9180a57SEric Paris } else { 703275bb41eSDavid Howells rc = may_context_mount_inode_relabel(context_sid, sbsec, 704275bb41eSDavid Howells cred); 705c9180a57SEric Paris if (rc) 706c9180a57SEric Paris goto out; 707c9180a57SEric Paris } 708c9180a57SEric Paris if (!rootcontext_sid) 709c9180a57SEric Paris rootcontext_sid = context_sid; 710c9180a57SEric Paris 711c9180a57SEric Paris sbsec->mntpoint_sid = context_sid; 712c9180a57SEric Paris sbsec->behavior = SECURITY_FS_USE_MNTPOINT; 713c9180a57SEric Paris } 714c9180a57SEric Paris 715c9180a57SEric Paris if (rootcontext_sid) { 716275bb41eSDavid Howells rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec, 717275bb41eSDavid Howells cred); 718c9180a57SEric Paris if (rc) 719c9180a57SEric Paris goto out; 720c9180a57SEric Paris 721c9180a57SEric Paris root_isec->sid = rootcontext_sid; 722c9180a57SEric Paris root_isec->initialized = 1; 723c9180a57SEric Paris } 724c9180a57SEric Paris 725c9180a57SEric Paris if (defcontext_sid) { 726c9180a57SEric Paris if (sbsec->behavior != SECURITY_FS_USE_XATTR) { 727c9180a57SEric Paris rc = -EINVAL; 728c9180a57SEric Paris printk(KERN_WARNING "SELinux: defcontext option is " 729c9180a57SEric Paris "invalid for this filesystem type\n"); 730c9180a57SEric Paris goto out; 731c9180a57SEric Paris } 732c9180a57SEric Paris 733c9180a57SEric Paris if (defcontext_sid != sbsec->def_sid) { 734c9180a57SEric Paris rc = may_context_mount_inode_relabel(defcontext_sid, 735275bb41eSDavid Howells sbsec, cred); 736c9180a57SEric Paris if (rc) 737c9180a57SEric Paris goto out; 738c9180a57SEric Paris } 739c9180a57SEric Paris 740c9180a57SEric Paris sbsec->def_sid = defcontext_sid; 741c9180a57SEric Paris } 742c9180a57SEric Paris 743c9180a57SEric Paris rc = sb_finish_set_opts(sb); 744c9180a57SEric Paris out: 745bc7e982bSEric Paris mutex_unlock(&sbsec->lock); 7461da177e4SLinus Torvalds return rc; 747c9180a57SEric Paris out_double_mount: 748c9180a57SEric Paris rc = -EINVAL; 749c9180a57SEric Paris printk(KERN_WARNING "SELinux: mount invalid. Same superblock, different " 750c9180a57SEric Paris "security settings for (dev %s, type %s)\n", sb->s_id, name); 751c9180a57SEric Paris goto out; 752c9180a57SEric Paris } 753c9180a57SEric Paris 754c9180a57SEric Paris static void selinux_sb_clone_mnt_opts(const struct super_block *oldsb, 755c9180a57SEric Paris struct super_block *newsb) 756c9180a57SEric Paris { 757c9180a57SEric Paris const struct superblock_security_struct *oldsbsec = oldsb->s_security; 758c9180a57SEric Paris struct superblock_security_struct *newsbsec = newsb->s_security; 759c9180a57SEric Paris 760c9180a57SEric Paris int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT); 761c9180a57SEric Paris int set_context = (oldsbsec->flags & CONTEXT_MNT); 762c9180a57SEric Paris int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT); 763c9180a57SEric Paris 7640f5e6420SEric Paris /* 7650f5e6420SEric Paris * if the parent was able to be mounted it clearly had no special lsm 766e8c26255SAl Viro * mount options. thus we can safely deal with this superblock later 7670f5e6420SEric Paris */ 768e8c26255SAl Viro if (!ss_initialized) 7690f5e6420SEric Paris return; 770c9180a57SEric Paris 771c9180a57SEric Paris /* how can we clone if the old one wasn't set up?? */ 7720d90a7ecSDavid P. Quigley BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED)); 773c9180a57SEric Paris 7745a552617SEric Paris /* if fs is reusing a sb, just let its options stand... */ 7750d90a7ecSDavid P. Quigley if (newsbsec->flags & SE_SBINITIALIZED) 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; 88311689d47SDavid P. Quigley case Opt_labelsupport: 88411689d47SDavid P. Quigley break; 885c9180a57SEric Paris default: 886c9180a57SEric Paris rc = -EINVAL; 887c9180a57SEric Paris printk(KERN_WARNING "SELinux: unknown mount option\n"); 888c9180a57SEric Paris goto out_err; 889c9180a57SEric Paris 890c9180a57SEric Paris } 891c9180a57SEric Paris } 892c9180a57SEric Paris 893e0007529SEric Paris rc = -ENOMEM; 894e0007529SEric Paris opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_ATOMIC); 895e0007529SEric Paris if (!opts->mnt_opts) 896e0007529SEric Paris goto out_err; 897e0007529SEric Paris 898e0007529SEric Paris opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), GFP_ATOMIC); 899e0007529SEric Paris if (!opts->mnt_opts_flags) { 900e0007529SEric Paris kfree(opts->mnt_opts); 901e0007529SEric Paris goto out_err; 902c9180a57SEric Paris } 903c9180a57SEric Paris 904e0007529SEric Paris if (fscontext) { 905e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = fscontext; 906e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT; 907e0007529SEric Paris } 908e0007529SEric Paris if (context) { 909e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = context; 910e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT; 911e0007529SEric Paris } 912e0007529SEric Paris if (rootcontext) { 913e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = rootcontext; 914e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT; 915e0007529SEric Paris } 916e0007529SEric Paris if (defcontext) { 917e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = defcontext; 918e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT; 919e0007529SEric Paris } 920e0007529SEric Paris 921e0007529SEric Paris opts->num_mnt_opts = num_mnt_opts; 922e0007529SEric Paris return 0; 923e0007529SEric Paris 924c9180a57SEric Paris out_err: 925c9180a57SEric Paris kfree(context); 926c9180a57SEric Paris kfree(defcontext); 927c9180a57SEric Paris kfree(fscontext); 928c9180a57SEric Paris kfree(rootcontext); 929c9180a57SEric Paris return rc; 9301da177e4SLinus Torvalds } 931e0007529SEric Paris /* 932e0007529SEric Paris * string mount options parsing and call set the sbsec 933e0007529SEric Paris */ 934e0007529SEric Paris static int superblock_doinit(struct super_block *sb, void *data) 935e0007529SEric Paris { 936e0007529SEric Paris int rc = 0; 937e0007529SEric Paris char *options = data; 938e0007529SEric Paris struct security_mnt_opts opts; 939e0007529SEric Paris 940e0007529SEric Paris security_init_mnt_opts(&opts); 941e0007529SEric Paris 942e0007529SEric Paris if (!data) 943e0007529SEric Paris goto out; 944e0007529SEric Paris 945e0007529SEric Paris BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA); 946e0007529SEric Paris 947e0007529SEric Paris rc = selinux_parse_opts_str(options, &opts); 948e0007529SEric Paris if (rc) 949e0007529SEric Paris goto out_err; 950e0007529SEric Paris 951e0007529SEric Paris out: 952e0007529SEric Paris rc = selinux_set_mnt_opts(sb, &opts); 953e0007529SEric Paris 954e0007529SEric Paris out_err: 955e0007529SEric Paris security_free_mnt_opts(&opts); 956e0007529SEric Paris return rc; 957e0007529SEric Paris } 9581da177e4SLinus Torvalds 9593583a711SAdrian Bunk static void selinux_write_opts(struct seq_file *m, 9603583a711SAdrian Bunk struct security_mnt_opts *opts) 9612069f457SEric Paris { 9622069f457SEric Paris int i; 9632069f457SEric Paris char *prefix; 9642069f457SEric Paris 9652069f457SEric Paris for (i = 0; i < opts->num_mnt_opts; i++) { 96611689d47SDavid P. Quigley char *has_comma; 96711689d47SDavid P. Quigley 96811689d47SDavid P. Quigley if (opts->mnt_opts[i]) 96911689d47SDavid P. Quigley has_comma = strchr(opts->mnt_opts[i], ','); 97011689d47SDavid P. Quigley else 97111689d47SDavid P. Quigley has_comma = NULL; 9722069f457SEric Paris 9732069f457SEric Paris switch (opts->mnt_opts_flags[i]) { 9742069f457SEric Paris case CONTEXT_MNT: 9752069f457SEric Paris prefix = CONTEXT_STR; 9762069f457SEric Paris break; 9772069f457SEric Paris case FSCONTEXT_MNT: 9782069f457SEric Paris prefix = FSCONTEXT_STR; 9792069f457SEric Paris break; 9802069f457SEric Paris case ROOTCONTEXT_MNT: 9812069f457SEric Paris prefix = ROOTCONTEXT_STR; 9822069f457SEric Paris break; 9832069f457SEric Paris case DEFCONTEXT_MNT: 9842069f457SEric Paris prefix = DEFCONTEXT_STR; 9852069f457SEric Paris break; 98611689d47SDavid P. Quigley case SE_SBLABELSUPP: 98711689d47SDavid P. Quigley seq_putc(m, ','); 98811689d47SDavid P. Quigley seq_puts(m, LABELSUPP_STR); 98911689d47SDavid P. Quigley continue; 9902069f457SEric Paris default: 9912069f457SEric Paris BUG(); 9922069f457SEric Paris }; 9932069f457SEric Paris /* we need a comma before each option */ 9942069f457SEric Paris seq_putc(m, ','); 9952069f457SEric Paris seq_puts(m, prefix); 9962069f457SEric Paris if (has_comma) 9972069f457SEric Paris seq_putc(m, '\"'); 9982069f457SEric Paris seq_puts(m, opts->mnt_opts[i]); 9992069f457SEric Paris if (has_comma) 10002069f457SEric Paris seq_putc(m, '\"'); 10012069f457SEric Paris } 10022069f457SEric Paris } 10032069f457SEric Paris 10042069f457SEric Paris static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb) 10052069f457SEric Paris { 10062069f457SEric Paris struct security_mnt_opts opts; 10072069f457SEric Paris int rc; 10082069f457SEric Paris 10092069f457SEric Paris rc = selinux_get_mnt_opts(sb, &opts); 1010383795c2SEric Paris if (rc) { 1011383795c2SEric Paris /* before policy load we may get EINVAL, don't show anything */ 1012383795c2SEric Paris if (rc == -EINVAL) 1013383795c2SEric Paris rc = 0; 10142069f457SEric Paris return rc; 1015383795c2SEric Paris } 10162069f457SEric Paris 10172069f457SEric Paris selinux_write_opts(m, &opts); 10182069f457SEric Paris 10192069f457SEric Paris security_free_mnt_opts(&opts); 10202069f457SEric Paris 10212069f457SEric Paris return rc; 10222069f457SEric Paris } 10232069f457SEric Paris 10241da177e4SLinus Torvalds static inline u16 inode_mode_to_security_class(umode_t mode) 10251da177e4SLinus Torvalds { 10261da177e4SLinus Torvalds switch (mode & S_IFMT) { 10271da177e4SLinus Torvalds case S_IFSOCK: 10281da177e4SLinus Torvalds return SECCLASS_SOCK_FILE; 10291da177e4SLinus Torvalds case S_IFLNK: 10301da177e4SLinus Torvalds return SECCLASS_LNK_FILE; 10311da177e4SLinus Torvalds case S_IFREG: 10321da177e4SLinus Torvalds return SECCLASS_FILE; 10331da177e4SLinus Torvalds case S_IFBLK: 10341da177e4SLinus Torvalds return SECCLASS_BLK_FILE; 10351da177e4SLinus Torvalds case S_IFDIR: 10361da177e4SLinus Torvalds return SECCLASS_DIR; 10371da177e4SLinus Torvalds case S_IFCHR: 10381da177e4SLinus Torvalds return SECCLASS_CHR_FILE; 10391da177e4SLinus Torvalds case S_IFIFO: 10401da177e4SLinus Torvalds return SECCLASS_FIFO_FILE; 10411da177e4SLinus Torvalds 10421da177e4SLinus Torvalds } 10431da177e4SLinus Torvalds 10441da177e4SLinus Torvalds return SECCLASS_FILE; 10451da177e4SLinus Torvalds } 10461da177e4SLinus Torvalds 104713402580SJames Morris static inline int default_protocol_stream(int protocol) 104813402580SJames Morris { 104913402580SJames Morris return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP); 105013402580SJames Morris } 105113402580SJames Morris 105213402580SJames Morris static inline int default_protocol_dgram(int protocol) 105313402580SJames Morris { 105413402580SJames Morris return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP); 105513402580SJames Morris } 105613402580SJames Morris 10571da177e4SLinus Torvalds static inline u16 socket_type_to_security_class(int family, int type, int protocol) 10581da177e4SLinus Torvalds { 10591da177e4SLinus Torvalds switch (family) { 10601da177e4SLinus Torvalds case PF_UNIX: 10611da177e4SLinus Torvalds switch (type) { 10621da177e4SLinus Torvalds case SOCK_STREAM: 10631da177e4SLinus Torvalds case SOCK_SEQPACKET: 10641da177e4SLinus Torvalds return SECCLASS_UNIX_STREAM_SOCKET; 10651da177e4SLinus Torvalds case SOCK_DGRAM: 10661da177e4SLinus Torvalds return SECCLASS_UNIX_DGRAM_SOCKET; 10671da177e4SLinus Torvalds } 10681da177e4SLinus Torvalds break; 10691da177e4SLinus Torvalds case PF_INET: 10701da177e4SLinus Torvalds case PF_INET6: 10711da177e4SLinus Torvalds switch (type) { 10721da177e4SLinus Torvalds case SOCK_STREAM: 107313402580SJames Morris if (default_protocol_stream(protocol)) 10741da177e4SLinus Torvalds return SECCLASS_TCP_SOCKET; 107513402580SJames Morris else 107613402580SJames Morris return SECCLASS_RAWIP_SOCKET; 10771da177e4SLinus Torvalds case SOCK_DGRAM: 107813402580SJames Morris if (default_protocol_dgram(protocol)) 10791da177e4SLinus Torvalds return SECCLASS_UDP_SOCKET; 108013402580SJames Morris else 108113402580SJames Morris return SECCLASS_RAWIP_SOCKET; 10822ee92d46SJames Morris case SOCK_DCCP: 10832ee92d46SJames Morris return SECCLASS_DCCP_SOCKET; 108413402580SJames Morris default: 10851da177e4SLinus Torvalds return SECCLASS_RAWIP_SOCKET; 10861da177e4SLinus Torvalds } 10871da177e4SLinus Torvalds break; 10881da177e4SLinus Torvalds case PF_NETLINK: 10891da177e4SLinus Torvalds switch (protocol) { 10901da177e4SLinus Torvalds case NETLINK_ROUTE: 10911da177e4SLinus Torvalds return SECCLASS_NETLINK_ROUTE_SOCKET; 10921da177e4SLinus Torvalds case NETLINK_FIREWALL: 10931da177e4SLinus Torvalds return SECCLASS_NETLINK_FIREWALL_SOCKET; 1094216efaaaSJames Morris case NETLINK_INET_DIAG: 10951da177e4SLinus Torvalds return SECCLASS_NETLINK_TCPDIAG_SOCKET; 10961da177e4SLinus Torvalds case NETLINK_NFLOG: 10971da177e4SLinus Torvalds return SECCLASS_NETLINK_NFLOG_SOCKET; 10981da177e4SLinus Torvalds case NETLINK_XFRM: 10991da177e4SLinus Torvalds return SECCLASS_NETLINK_XFRM_SOCKET; 11001da177e4SLinus Torvalds case NETLINK_SELINUX: 11011da177e4SLinus Torvalds return SECCLASS_NETLINK_SELINUX_SOCKET; 11021da177e4SLinus Torvalds case NETLINK_AUDIT: 11031da177e4SLinus Torvalds return SECCLASS_NETLINK_AUDIT_SOCKET; 11041da177e4SLinus Torvalds case NETLINK_IP6_FW: 11051da177e4SLinus Torvalds return SECCLASS_NETLINK_IP6FW_SOCKET; 11061da177e4SLinus Torvalds case NETLINK_DNRTMSG: 11071da177e4SLinus Torvalds return SECCLASS_NETLINK_DNRT_SOCKET; 11080c9b7942SJames Morris case NETLINK_KOBJECT_UEVENT: 11090c9b7942SJames Morris return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET; 11101da177e4SLinus Torvalds default: 11111da177e4SLinus Torvalds return SECCLASS_NETLINK_SOCKET; 11121da177e4SLinus Torvalds } 11131da177e4SLinus Torvalds case PF_PACKET: 11141da177e4SLinus Torvalds return SECCLASS_PACKET_SOCKET; 11151da177e4SLinus Torvalds case PF_KEY: 11161da177e4SLinus Torvalds return SECCLASS_KEY_SOCKET; 11173e3ff15eSChristopher J. PeBenito case PF_APPLETALK: 11183e3ff15eSChristopher J. PeBenito return SECCLASS_APPLETALK_SOCKET; 11191da177e4SLinus Torvalds } 11201da177e4SLinus Torvalds 11211da177e4SLinus Torvalds return SECCLASS_SOCKET; 11221da177e4SLinus Torvalds } 11231da177e4SLinus Torvalds 11241da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 11258e6c9693SLucian Adrian Grijincu static int selinux_proc_get_sid(struct dentry *dentry, 11261da177e4SLinus Torvalds u16 tclass, 11271da177e4SLinus Torvalds u32 *sid) 11281da177e4SLinus Torvalds { 11298e6c9693SLucian Adrian Grijincu int rc; 11308e6c9693SLucian Adrian Grijincu char *buffer, *path; 11311da177e4SLinus Torvalds 11321da177e4SLinus Torvalds buffer = (char *)__get_free_page(GFP_KERNEL); 11331da177e4SLinus Torvalds if (!buffer) 11341da177e4SLinus Torvalds return -ENOMEM; 11351da177e4SLinus Torvalds 11368e6c9693SLucian Adrian Grijincu path = dentry_path_raw(dentry, buffer, PAGE_SIZE); 11378e6c9693SLucian Adrian Grijincu if (IS_ERR(path)) 11388e6c9693SLucian Adrian Grijincu rc = PTR_ERR(path); 11398e6c9693SLucian Adrian Grijincu else { 11408e6c9693SLucian Adrian Grijincu /* each process gets a /proc/PID/ entry. Strip off the 11418e6c9693SLucian Adrian Grijincu * PID part to get a valid selinux labeling. 11428e6c9693SLucian Adrian Grijincu * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */ 11438e6c9693SLucian Adrian Grijincu while (path[1] >= '0' && path[1] <= '9') { 11448e6c9693SLucian Adrian Grijincu path[1] = '/'; 11458e6c9693SLucian Adrian Grijincu path++; 11461da177e4SLinus Torvalds } 11471da177e4SLinus Torvalds rc = security_genfs_sid("proc", path, tclass, sid); 11488e6c9693SLucian Adrian Grijincu } 11491da177e4SLinus Torvalds free_page((unsigned long)buffer); 11501da177e4SLinus Torvalds return rc; 11511da177e4SLinus Torvalds } 11521da177e4SLinus Torvalds #else 11538e6c9693SLucian Adrian Grijincu static int selinux_proc_get_sid(struct dentry *dentry, 11541da177e4SLinus Torvalds u16 tclass, 11551da177e4SLinus Torvalds u32 *sid) 11561da177e4SLinus Torvalds { 11571da177e4SLinus Torvalds return -EINVAL; 11581da177e4SLinus Torvalds } 11591da177e4SLinus Torvalds #endif 11601da177e4SLinus Torvalds 11611da177e4SLinus Torvalds /* The inode's security attributes must be initialized before first use. */ 11621da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry) 11631da177e4SLinus Torvalds { 11641da177e4SLinus Torvalds struct superblock_security_struct *sbsec = NULL; 11651da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 11661da177e4SLinus Torvalds u32 sid; 11671da177e4SLinus Torvalds struct dentry *dentry; 11681da177e4SLinus Torvalds #define INITCONTEXTLEN 255 11691da177e4SLinus Torvalds char *context = NULL; 11701da177e4SLinus Torvalds unsigned len = 0; 11711da177e4SLinus Torvalds int rc = 0; 11721da177e4SLinus Torvalds 11731da177e4SLinus Torvalds if (isec->initialized) 11741da177e4SLinus Torvalds goto out; 11751da177e4SLinus Torvalds 117623970741SEric Paris mutex_lock(&isec->lock); 11771da177e4SLinus Torvalds if (isec->initialized) 117823970741SEric Paris goto out_unlock; 11791da177e4SLinus Torvalds 11801da177e4SLinus Torvalds sbsec = inode->i_sb->s_security; 11810d90a7ecSDavid P. Quigley if (!(sbsec->flags & SE_SBINITIALIZED)) { 11821da177e4SLinus Torvalds /* Defer initialization until selinux_complete_init, 11831da177e4SLinus Torvalds after the initial policy is loaded and the security 11841da177e4SLinus Torvalds server is ready to handle calls. */ 11851da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 11861da177e4SLinus Torvalds if (list_empty(&isec->list)) 11871da177e4SLinus Torvalds list_add(&isec->list, &sbsec->isec_head); 11881da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 118923970741SEric Paris goto out_unlock; 11901da177e4SLinus Torvalds } 11911da177e4SLinus Torvalds 11921da177e4SLinus Torvalds switch (sbsec->behavior) { 11931da177e4SLinus Torvalds case SECURITY_FS_USE_XATTR: 11941da177e4SLinus Torvalds if (!inode->i_op->getxattr) { 11951da177e4SLinus Torvalds isec->sid = sbsec->def_sid; 11961da177e4SLinus Torvalds break; 11971da177e4SLinus Torvalds } 11981da177e4SLinus Torvalds 11991da177e4SLinus Torvalds /* Need a dentry, since the xattr API requires one. 12001da177e4SLinus Torvalds Life would be simpler if we could just pass the inode. */ 12011da177e4SLinus Torvalds if (opt_dentry) { 12021da177e4SLinus Torvalds /* Called from d_instantiate or d_splice_alias. */ 12031da177e4SLinus Torvalds dentry = dget(opt_dentry); 12041da177e4SLinus Torvalds } else { 12051da177e4SLinus Torvalds /* Called from selinux_complete_init, try to find a dentry. */ 12061da177e4SLinus Torvalds dentry = d_find_alias(inode); 12071da177e4SLinus Torvalds } 12081da177e4SLinus Torvalds if (!dentry) { 1209df7f54c0SEric Paris /* 1210df7f54c0SEric Paris * this is can be hit on boot when a file is accessed 1211df7f54c0SEric Paris * before the policy is loaded. When we load policy we 1212df7f54c0SEric Paris * may find inodes that have no dentry on the 1213df7f54c0SEric Paris * sbsec->isec_head list. No reason to complain as these 1214df7f54c0SEric Paris * will get fixed up the next time we go through 1215df7f54c0SEric Paris * inode_doinit with a dentry, before these inodes could 1216df7f54c0SEric Paris * be used again by userspace. 1217df7f54c0SEric Paris */ 121823970741SEric Paris goto out_unlock; 12191da177e4SLinus Torvalds } 12201da177e4SLinus Torvalds 12211da177e4SLinus Torvalds len = INITCONTEXTLEN; 12224cb912f1SEric Paris context = kmalloc(len+1, GFP_NOFS); 12231da177e4SLinus Torvalds if (!context) { 12241da177e4SLinus Torvalds rc = -ENOMEM; 12251da177e4SLinus Torvalds dput(dentry); 122623970741SEric Paris goto out_unlock; 12271da177e4SLinus Torvalds } 12284cb912f1SEric Paris context[len] = '\0'; 12291da177e4SLinus Torvalds rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX, 12301da177e4SLinus Torvalds context, len); 12311da177e4SLinus Torvalds if (rc == -ERANGE) { 1232314dabb8SJames Morris kfree(context); 1233314dabb8SJames Morris 12341da177e4SLinus Torvalds /* Need a larger buffer. Query for the right size. */ 12351da177e4SLinus Torvalds rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX, 12361da177e4SLinus Torvalds NULL, 0); 12371da177e4SLinus Torvalds if (rc < 0) { 12381da177e4SLinus Torvalds dput(dentry); 123923970741SEric Paris goto out_unlock; 12401da177e4SLinus Torvalds } 12411da177e4SLinus Torvalds len = rc; 12424cb912f1SEric Paris context = kmalloc(len+1, GFP_NOFS); 12431da177e4SLinus Torvalds if (!context) { 12441da177e4SLinus Torvalds rc = -ENOMEM; 12451da177e4SLinus Torvalds dput(dentry); 124623970741SEric Paris goto out_unlock; 12471da177e4SLinus Torvalds } 12484cb912f1SEric Paris context[len] = '\0'; 12491da177e4SLinus Torvalds rc = inode->i_op->getxattr(dentry, 12501da177e4SLinus Torvalds XATTR_NAME_SELINUX, 12511da177e4SLinus Torvalds context, len); 12521da177e4SLinus Torvalds } 12531da177e4SLinus Torvalds dput(dentry); 12541da177e4SLinus Torvalds if (rc < 0) { 12551da177e4SLinus Torvalds if (rc != -ENODATA) { 1256744ba35eSEric Paris printk(KERN_WARNING "SELinux: %s: getxattr returned " 1257dd6f953aSHarvey Harrison "%d for dev=%s ino=%ld\n", __func__, 12581da177e4SLinus Torvalds -rc, inode->i_sb->s_id, inode->i_ino); 12591da177e4SLinus Torvalds kfree(context); 126023970741SEric Paris goto out_unlock; 12611da177e4SLinus Torvalds } 12621da177e4SLinus Torvalds /* Map ENODATA to the default file SID */ 12631da177e4SLinus Torvalds sid = sbsec->def_sid; 12641da177e4SLinus Torvalds rc = 0; 12651da177e4SLinus Torvalds } else { 1266f5c1d5b2SJames Morris rc = security_context_to_sid_default(context, rc, &sid, 1267869ab514SStephen Smalley sbsec->def_sid, 1268869ab514SStephen Smalley GFP_NOFS); 12691da177e4SLinus Torvalds if (rc) { 12704ba0a8adSEric Paris char *dev = inode->i_sb->s_id; 12714ba0a8adSEric Paris unsigned long ino = inode->i_ino; 12724ba0a8adSEric Paris 12734ba0a8adSEric Paris if (rc == -EINVAL) { 12744ba0a8adSEric Paris if (printk_ratelimit()) 12754ba0a8adSEric Paris printk(KERN_NOTICE "SELinux: inode=%lu on dev=%s was found to have an invalid " 12764ba0a8adSEric Paris "context=%s. This indicates you may need to relabel the inode or the " 12774ba0a8adSEric Paris "filesystem in question.\n", ino, dev, context); 12784ba0a8adSEric Paris } else { 1279744ba35eSEric Paris printk(KERN_WARNING "SELinux: %s: context_to_sid(%s) " 12801da177e4SLinus Torvalds "returned %d for dev=%s ino=%ld\n", 12814ba0a8adSEric Paris __func__, context, -rc, dev, ino); 12824ba0a8adSEric Paris } 12831da177e4SLinus Torvalds kfree(context); 12841da177e4SLinus Torvalds /* Leave with the unlabeled SID */ 12851da177e4SLinus Torvalds rc = 0; 12861da177e4SLinus Torvalds break; 12871da177e4SLinus Torvalds } 12881da177e4SLinus Torvalds } 12891da177e4SLinus Torvalds kfree(context); 12901da177e4SLinus Torvalds isec->sid = sid; 12911da177e4SLinus Torvalds break; 12921da177e4SLinus Torvalds case SECURITY_FS_USE_TASK: 12931da177e4SLinus Torvalds isec->sid = isec->task_sid; 12941da177e4SLinus Torvalds break; 12951da177e4SLinus Torvalds case SECURITY_FS_USE_TRANS: 12961da177e4SLinus Torvalds /* Default to the fs SID. */ 12971da177e4SLinus Torvalds isec->sid = sbsec->sid; 12981da177e4SLinus Torvalds 12991da177e4SLinus Torvalds /* Try to obtain a transition SID. */ 13001da177e4SLinus Torvalds isec->sclass = inode_mode_to_security_class(inode->i_mode); 1301652bb9b0SEric Paris rc = security_transition_sid(isec->task_sid, sbsec->sid, 1302652bb9b0SEric Paris isec->sclass, NULL, &sid); 13031da177e4SLinus Torvalds if (rc) 130423970741SEric Paris goto out_unlock; 13051da177e4SLinus Torvalds isec->sid = sid; 13061da177e4SLinus Torvalds break; 1307c312feb2SEric Paris case SECURITY_FS_USE_MNTPOINT: 1308c312feb2SEric Paris isec->sid = sbsec->mntpoint_sid; 1309c312feb2SEric Paris break; 13101da177e4SLinus Torvalds default: 1311c312feb2SEric Paris /* Default to the fs superblock SID. */ 13121da177e4SLinus Torvalds isec->sid = sbsec->sid; 13131da177e4SLinus Torvalds 13140d90a7ecSDavid P. Quigley if ((sbsec->flags & SE_SBPROC) && !S_ISLNK(inode->i_mode)) { 13158e6c9693SLucian Adrian Grijincu if (opt_dentry) { 13161da177e4SLinus Torvalds isec->sclass = inode_mode_to_security_class(inode->i_mode); 13178e6c9693SLucian Adrian Grijincu rc = selinux_proc_get_sid(opt_dentry, 13181da177e4SLinus Torvalds isec->sclass, 13191da177e4SLinus Torvalds &sid); 13201da177e4SLinus Torvalds if (rc) 132123970741SEric Paris goto out_unlock; 13221da177e4SLinus Torvalds isec->sid = sid; 13231da177e4SLinus Torvalds } 13241da177e4SLinus Torvalds } 13251da177e4SLinus Torvalds break; 13261da177e4SLinus Torvalds } 13271da177e4SLinus Torvalds 13281da177e4SLinus Torvalds isec->initialized = 1; 13291da177e4SLinus Torvalds 133023970741SEric Paris out_unlock: 133123970741SEric Paris mutex_unlock(&isec->lock); 13321da177e4SLinus Torvalds out: 13331da177e4SLinus Torvalds if (isec->sclass == SECCLASS_FILE) 13341da177e4SLinus Torvalds isec->sclass = inode_mode_to_security_class(inode->i_mode); 13351da177e4SLinus Torvalds return rc; 13361da177e4SLinus Torvalds } 13371da177e4SLinus Torvalds 13381da177e4SLinus Torvalds /* Convert a Linux signal to an access vector. */ 13391da177e4SLinus Torvalds static inline u32 signal_to_av(int sig) 13401da177e4SLinus Torvalds { 13411da177e4SLinus Torvalds u32 perm = 0; 13421da177e4SLinus Torvalds 13431da177e4SLinus Torvalds switch (sig) { 13441da177e4SLinus Torvalds case SIGCHLD: 13451da177e4SLinus Torvalds /* Commonly granted from child to parent. */ 13461da177e4SLinus Torvalds perm = PROCESS__SIGCHLD; 13471da177e4SLinus Torvalds break; 13481da177e4SLinus Torvalds case SIGKILL: 13491da177e4SLinus Torvalds /* Cannot be caught or ignored */ 13501da177e4SLinus Torvalds perm = PROCESS__SIGKILL; 13511da177e4SLinus Torvalds break; 13521da177e4SLinus Torvalds case SIGSTOP: 13531da177e4SLinus Torvalds /* Cannot be caught or ignored */ 13541da177e4SLinus Torvalds perm = PROCESS__SIGSTOP; 13551da177e4SLinus Torvalds break; 13561da177e4SLinus Torvalds default: 13571da177e4SLinus Torvalds /* All other signals. */ 13581da177e4SLinus Torvalds perm = PROCESS__SIGNAL; 13591da177e4SLinus Torvalds break; 13601da177e4SLinus Torvalds } 13611da177e4SLinus Torvalds 13621da177e4SLinus Torvalds return perm; 13631da177e4SLinus Torvalds } 13641da177e4SLinus Torvalds 1365275bb41eSDavid Howells /* 1366d84f4f99SDavid Howells * Check permission between a pair of credentials 1367d84f4f99SDavid Howells * fork check, ptrace check, etc. 1368d84f4f99SDavid Howells */ 1369d84f4f99SDavid Howells static int cred_has_perm(const struct cred *actor, 1370d84f4f99SDavid Howells const struct cred *target, 1371d84f4f99SDavid Howells u32 perms) 1372d84f4f99SDavid Howells { 1373d84f4f99SDavid Howells u32 asid = cred_sid(actor), tsid = cred_sid(target); 1374d84f4f99SDavid Howells 1375d84f4f99SDavid Howells return avc_has_perm(asid, tsid, SECCLASS_PROCESS, perms, NULL); 1376d84f4f99SDavid Howells } 1377d84f4f99SDavid Howells 1378d84f4f99SDavid Howells /* 137988e67f3bSDavid Howells * Check permission between a pair of tasks, e.g. signal checks, 1380275bb41eSDavid Howells * fork check, ptrace check, etc. 1381275bb41eSDavid Howells * tsk1 is the actor and tsk2 is the target 13823b11a1deSDavid Howells * - this uses the default subjective creds of tsk1 1383275bb41eSDavid Howells */ 1384275bb41eSDavid Howells static int task_has_perm(const struct task_struct *tsk1, 1385275bb41eSDavid Howells const struct task_struct *tsk2, 13861da177e4SLinus Torvalds u32 perms) 13871da177e4SLinus Torvalds { 1388275bb41eSDavid Howells const struct task_security_struct *__tsec1, *__tsec2; 1389275bb41eSDavid Howells u32 sid1, sid2; 13901da177e4SLinus Torvalds 1391275bb41eSDavid Howells rcu_read_lock(); 1392275bb41eSDavid Howells __tsec1 = __task_cred(tsk1)->security; sid1 = __tsec1->sid; 1393275bb41eSDavid Howells __tsec2 = __task_cred(tsk2)->security; sid2 = __tsec2->sid; 1394275bb41eSDavid Howells rcu_read_unlock(); 1395275bb41eSDavid Howells return avc_has_perm(sid1, sid2, SECCLASS_PROCESS, perms, NULL); 13961da177e4SLinus Torvalds } 13971da177e4SLinus Torvalds 13983b11a1deSDavid Howells /* 13993b11a1deSDavid Howells * Check permission between current and another task, e.g. signal checks, 14003b11a1deSDavid Howells * fork check, ptrace check, etc. 14013b11a1deSDavid Howells * current is the actor and tsk2 is the target 14023b11a1deSDavid Howells * - this uses current's subjective creds 14033b11a1deSDavid Howells */ 14043b11a1deSDavid Howells static int current_has_perm(const struct task_struct *tsk, 14053b11a1deSDavid Howells u32 perms) 14063b11a1deSDavid Howells { 14073b11a1deSDavid Howells u32 sid, tsid; 14083b11a1deSDavid Howells 14093b11a1deSDavid Howells sid = current_sid(); 14103b11a1deSDavid Howells tsid = task_sid(tsk); 14113b11a1deSDavid Howells return avc_has_perm(sid, tsid, SECCLASS_PROCESS, perms, NULL); 14123b11a1deSDavid Howells } 14133b11a1deSDavid Howells 1414b68e418cSStephen Smalley #if CAP_LAST_CAP > 63 1415b68e418cSStephen Smalley #error Fix SELinux to handle capabilities > 63. 1416b68e418cSStephen Smalley #endif 1417b68e418cSStephen Smalley 14181da177e4SLinus Torvalds /* Check whether a task is allowed to use a capability. */ 14191da177e4SLinus Torvalds static int task_has_capability(struct task_struct *tsk, 14203699c53cSDavid Howells const struct cred *cred, 142106112163SEric Paris int cap, int audit) 14221da177e4SLinus Torvalds { 14232bf49690SThomas Liu struct common_audit_data ad; 142406112163SEric Paris struct av_decision avd; 1425b68e418cSStephen Smalley u16 sclass; 14263699c53cSDavid Howells u32 sid = cred_sid(cred); 1427b68e418cSStephen Smalley u32 av = CAP_TO_MASK(cap); 142806112163SEric Paris int rc; 14291da177e4SLinus Torvalds 14302bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, CAP); 14311da177e4SLinus Torvalds ad.tsk = tsk; 14321da177e4SLinus Torvalds ad.u.cap = cap; 14331da177e4SLinus Torvalds 1434b68e418cSStephen Smalley switch (CAP_TO_INDEX(cap)) { 1435b68e418cSStephen Smalley case 0: 1436b68e418cSStephen Smalley sclass = SECCLASS_CAPABILITY; 1437b68e418cSStephen Smalley break; 1438b68e418cSStephen Smalley case 1: 1439b68e418cSStephen Smalley sclass = SECCLASS_CAPABILITY2; 1440b68e418cSStephen Smalley break; 1441b68e418cSStephen Smalley default: 1442b68e418cSStephen Smalley printk(KERN_ERR 1443b68e418cSStephen Smalley "SELinux: out of range capability %d\n", cap); 1444b68e418cSStephen Smalley BUG(); 1445b68e418cSStephen Smalley } 144606112163SEric Paris 1447275bb41eSDavid Howells rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd); 144806112163SEric Paris if (audit == SECURITY_CAP_AUDIT) 1449275bb41eSDavid Howells avc_audit(sid, sid, sclass, av, &avd, rc, &ad); 145006112163SEric Paris return rc; 14511da177e4SLinus Torvalds } 14521da177e4SLinus Torvalds 14531da177e4SLinus Torvalds /* Check whether a task is allowed to use a system operation. */ 14541da177e4SLinus Torvalds static int task_has_system(struct task_struct *tsk, 14551da177e4SLinus Torvalds u32 perms) 14561da177e4SLinus Torvalds { 1457275bb41eSDavid Howells u32 sid = task_sid(tsk); 14581da177e4SLinus Torvalds 1459275bb41eSDavid Howells return avc_has_perm(sid, SECINITSID_KERNEL, 14601da177e4SLinus Torvalds SECCLASS_SYSTEM, perms, NULL); 14611da177e4SLinus Torvalds } 14621da177e4SLinus Torvalds 14631da177e4SLinus Torvalds /* Check whether a task has a particular permission to an inode. 14641da177e4SLinus Torvalds The 'adp' parameter is optional and allows other audit 14651da177e4SLinus Torvalds data to be passed (e.g. the dentry). */ 146688e67f3bSDavid Howells static int inode_has_perm(const struct cred *cred, 14671da177e4SLinus Torvalds struct inode *inode, 14681da177e4SLinus Torvalds u32 perms, 14692bf49690SThomas Liu struct common_audit_data *adp) 14701da177e4SLinus Torvalds { 14711da177e4SLinus Torvalds struct inode_security_struct *isec; 14722bf49690SThomas Liu struct common_audit_data ad; 1473275bb41eSDavid Howells u32 sid; 14741da177e4SLinus Torvalds 1475e0e81739SDavid Howells validate_creds(cred); 1476e0e81739SDavid Howells 1477bbaca6c2SStephen Smalley if (unlikely(IS_PRIVATE(inode))) 1478bbaca6c2SStephen Smalley return 0; 1479bbaca6c2SStephen Smalley 148088e67f3bSDavid Howells sid = cred_sid(cred); 14811da177e4SLinus Torvalds isec = inode->i_security; 14821da177e4SLinus Torvalds 14831da177e4SLinus Torvalds if (!adp) { 14841da177e4SLinus Torvalds adp = &ad; 14852bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, FS); 14861da177e4SLinus Torvalds ad.u.fs.inode = inode; 14871da177e4SLinus Torvalds } 14881da177e4SLinus Torvalds 1489275bb41eSDavid Howells return avc_has_perm(sid, isec->sid, isec->sclass, perms, adp); 14901da177e4SLinus Torvalds } 14911da177e4SLinus Torvalds 14921da177e4SLinus Torvalds /* Same as inode_has_perm, but pass explicit audit data containing 14931da177e4SLinus Torvalds the dentry to help the auditing code to more easily generate the 14941da177e4SLinus Torvalds pathname if needed. */ 149588e67f3bSDavid Howells static inline int dentry_has_perm(const struct cred *cred, 14961da177e4SLinus Torvalds struct vfsmount *mnt, 14971da177e4SLinus Torvalds struct dentry *dentry, 14981da177e4SLinus Torvalds u32 av) 14991da177e4SLinus Torvalds { 15001da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 15012bf49690SThomas Liu struct common_audit_data ad; 150288e67f3bSDavid Howells 15032bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, FS); 150444707fdfSJan Blunck ad.u.fs.path.mnt = mnt; 150544707fdfSJan Blunck ad.u.fs.path.dentry = dentry; 150688e67f3bSDavid Howells return inode_has_perm(cred, inode, av, &ad); 15071da177e4SLinus Torvalds } 15081da177e4SLinus Torvalds 15091da177e4SLinus Torvalds /* Check whether a task can use an open file descriptor to 15101da177e4SLinus Torvalds access an inode in a given way. Check access to the 15111da177e4SLinus Torvalds descriptor itself, and then use dentry_has_perm to 15121da177e4SLinus Torvalds check a particular permission to the file. 15131da177e4SLinus Torvalds Access to the descriptor is implicitly granted if it 15141da177e4SLinus Torvalds has the same SID as the process. If av is zero, then 15151da177e4SLinus Torvalds access to the file is not checked, e.g. for cases 15161da177e4SLinus Torvalds where only the descriptor is affected like seek. */ 151788e67f3bSDavid Howells static int file_has_perm(const struct cred *cred, 15181da177e4SLinus Torvalds struct file *file, 15191da177e4SLinus Torvalds u32 av) 15201da177e4SLinus Torvalds { 15211da177e4SLinus Torvalds struct file_security_struct *fsec = file->f_security; 152244707fdfSJan Blunck struct inode *inode = file->f_path.dentry->d_inode; 15232bf49690SThomas Liu struct common_audit_data ad; 152488e67f3bSDavid Howells u32 sid = cred_sid(cred); 15251da177e4SLinus Torvalds int rc; 15261da177e4SLinus Torvalds 15272bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, FS); 152844707fdfSJan Blunck ad.u.fs.path = file->f_path; 15291da177e4SLinus Torvalds 1530275bb41eSDavid Howells if (sid != fsec->sid) { 1531275bb41eSDavid Howells rc = avc_has_perm(sid, fsec->sid, 15321da177e4SLinus Torvalds SECCLASS_FD, 15331da177e4SLinus Torvalds FD__USE, 15341da177e4SLinus Torvalds &ad); 15351da177e4SLinus Torvalds if (rc) 153688e67f3bSDavid Howells goto out; 15371da177e4SLinus Torvalds } 15381da177e4SLinus Torvalds 15391da177e4SLinus Torvalds /* av is zero if only checking access to the descriptor. */ 154088e67f3bSDavid Howells rc = 0; 15411da177e4SLinus Torvalds if (av) 154288e67f3bSDavid Howells rc = inode_has_perm(cred, inode, av, &ad); 15431da177e4SLinus Torvalds 154488e67f3bSDavid Howells out: 154588e67f3bSDavid Howells return rc; 15461da177e4SLinus Torvalds } 15471da177e4SLinus Torvalds 15481da177e4SLinus Torvalds /* Check whether a task can create a file. */ 15491da177e4SLinus Torvalds static int may_create(struct inode *dir, 15501da177e4SLinus Torvalds struct dentry *dentry, 15511da177e4SLinus Torvalds u16 tclass) 15521da177e4SLinus Torvalds { 15535fb49870SPaul Moore const struct task_security_struct *tsec = current_security(); 15541da177e4SLinus Torvalds struct inode_security_struct *dsec; 15551da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 1556275bb41eSDavid Howells u32 sid, newsid; 15572bf49690SThomas Liu struct common_audit_data ad; 15581da177e4SLinus Torvalds int rc; 15591da177e4SLinus Torvalds 15601da177e4SLinus Torvalds dsec = dir->i_security; 15611da177e4SLinus Torvalds sbsec = dir->i_sb->s_security; 15621da177e4SLinus Torvalds 1563275bb41eSDavid Howells sid = tsec->sid; 1564275bb41eSDavid Howells newsid = tsec->create_sid; 1565275bb41eSDavid Howells 15662bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, FS); 156744707fdfSJan Blunck ad.u.fs.path.dentry = dentry; 15681da177e4SLinus Torvalds 1569275bb41eSDavid Howells rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, 15701da177e4SLinus Torvalds DIR__ADD_NAME | DIR__SEARCH, 15711da177e4SLinus Torvalds &ad); 15721da177e4SLinus Torvalds if (rc) 15731da177e4SLinus Torvalds return rc; 15741da177e4SLinus Torvalds 1575cd89596fSDavid P. Quigley if (!newsid || !(sbsec->flags & SE_SBLABELSUPP)) { 1576652bb9b0SEric Paris rc = security_transition_sid(sid, dsec->sid, tclass, NULL, &newsid); 15771da177e4SLinus Torvalds if (rc) 15781da177e4SLinus Torvalds return rc; 15791da177e4SLinus Torvalds } 15801da177e4SLinus Torvalds 1581275bb41eSDavid Howells rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad); 15821da177e4SLinus Torvalds if (rc) 15831da177e4SLinus Torvalds return rc; 15841da177e4SLinus Torvalds 15851da177e4SLinus Torvalds return avc_has_perm(newsid, sbsec->sid, 15861da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 15871da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, &ad); 15881da177e4SLinus Torvalds } 15891da177e4SLinus Torvalds 15904eb582cfSMichael LeMay /* Check whether a task can create a key. */ 15914eb582cfSMichael LeMay static int may_create_key(u32 ksid, 15924eb582cfSMichael LeMay struct task_struct *ctx) 15934eb582cfSMichael LeMay { 1594275bb41eSDavid Howells u32 sid = task_sid(ctx); 15954eb582cfSMichael LeMay 1596275bb41eSDavid Howells return avc_has_perm(sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL); 15974eb582cfSMichael LeMay } 15984eb582cfSMichael LeMay 15991da177e4SLinus Torvalds #define MAY_LINK 0 16001da177e4SLinus Torvalds #define MAY_UNLINK 1 16011da177e4SLinus Torvalds #define MAY_RMDIR 2 16021da177e4SLinus Torvalds 16031da177e4SLinus Torvalds /* Check whether a task can link, unlink, or rmdir a file/directory. */ 16041da177e4SLinus Torvalds static int may_link(struct inode *dir, 16051da177e4SLinus Torvalds struct dentry *dentry, 16061da177e4SLinus Torvalds int kind) 16071da177e4SLinus Torvalds 16081da177e4SLinus Torvalds { 16091da177e4SLinus Torvalds struct inode_security_struct *dsec, *isec; 16102bf49690SThomas Liu struct common_audit_data ad; 1611275bb41eSDavid Howells u32 sid = current_sid(); 16121da177e4SLinus Torvalds u32 av; 16131da177e4SLinus Torvalds int rc; 16141da177e4SLinus Torvalds 16151da177e4SLinus Torvalds dsec = dir->i_security; 16161da177e4SLinus Torvalds isec = dentry->d_inode->i_security; 16171da177e4SLinus Torvalds 16182bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, FS); 161944707fdfSJan Blunck ad.u.fs.path.dentry = dentry; 16201da177e4SLinus Torvalds 16211da177e4SLinus Torvalds av = DIR__SEARCH; 16221da177e4SLinus Torvalds av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME); 1623275bb41eSDavid Howells rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad); 16241da177e4SLinus Torvalds if (rc) 16251da177e4SLinus Torvalds return rc; 16261da177e4SLinus Torvalds 16271da177e4SLinus Torvalds switch (kind) { 16281da177e4SLinus Torvalds case MAY_LINK: 16291da177e4SLinus Torvalds av = FILE__LINK; 16301da177e4SLinus Torvalds break; 16311da177e4SLinus Torvalds case MAY_UNLINK: 16321da177e4SLinus Torvalds av = FILE__UNLINK; 16331da177e4SLinus Torvalds break; 16341da177e4SLinus Torvalds case MAY_RMDIR: 16351da177e4SLinus Torvalds av = DIR__RMDIR; 16361da177e4SLinus Torvalds break; 16371da177e4SLinus Torvalds default: 1638744ba35eSEric Paris printk(KERN_WARNING "SELinux: %s: unrecognized kind %d\n", 1639744ba35eSEric Paris __func__, kind); 16401da177e4SLinus Torvalds return 0; 16411da177e4SLinus Torvalds } 16421da177e4SLinus Torvalds 1643275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad); 16441da177e4SLinus Torvalds return rc; 16451da177e4SLinus Torvalds } 16461da177e4SLinus Torvalds 16471da177e4SLinus Torvalds static inline int may_rename(struct inode *old_dir, 16481da177e4SLinus Torvalds struct dentry *old_dentry, 16491da177e4SLinus Torvalds struct inode *new_dir, 16501da177e4SLinus Torvalds struct dentry *new_dentry) 16511da177e4SLinus Torvalds { 16521da177e4SLinus Torvalds struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec; 16532bf49690SThomas Liu struct common_audit_data ad; 1654275bb41eSDavid Howells u32 sid = current_sid(); 16551da177e4SLinus Torvalds u32 av; 16561da177e4SLinus Torvalds int old_is_dir, new_is_dir; 16571da177e4SLinus Torvalds int rc; 16581da177e4SLinus Torvalds 16591da177e4SLinus Torvalds old_dsec = old_dir->i_security; 16601da177e4SLinus Torvalds old_isec = old_dentry->d_inode->i_security; 16611da177e4SLinus Torvalds old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 16621da177e4SLinus Torvalds new_dsec = new_dir->i_security; 16631da177e4SLinus Torvalds 16642bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, FS); 16651da177e4SLinus Torvalds 166644707fdfSJan Blunck ad.u.fs.path.dentry = old_dentry; 1667275bb41eSDavid Howells rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR, 16681da177e4SLinus Torvalds DIR__REMOVE_NAME | DIR__SEARCH, &ad); 16691da177e4SLinus Torvalds if (rc) 16701da177e4SLinus Torvalds return rc; 1671275bb41eSDavid Howells rc = avc_has_perm(sid, old_isec->sid, 16721da177e4SLinus Torvalds old_isec->sclass, FILE__RENAME, &ad); 16731da177e4SLinus Torvalds if (rc) 16741da177e4SLinus Torvalds return rc; 16751da177e4SLinus Torvalds if (old_is_dir && new_dir != old_dir) { 1676275bb41eSDavid Howells rc = avc_has_perm(sid, old_isec->sid, 16771da177e4SLinus Torvalds old_isec->sclass, DIR__REPARENT, &ad); 16781da177e4SLinus Torvalds if (rc) 16791da177e4SLinus Torvalds return rc; 16801da177e4SLinus Torvalds } 16811da177e4SLinus Torvalds 168244707fdfSJan Blunck ad.u.fs.path.dentry = new_dentry; 16831da177e4SLinus Torvalds av = DIR__ADD_NAME | DIR__SEARCH; 16841da177e4SLinus Torvalds if (new_dentry->d_inode) 16851da177e4SLinus Torvalds av |= DIR__REMOVE_NAME; 1686275bb41eSDavid Howells rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad); 16871da177e4SLinus Torvalds if (rc) 16881da177e4SLinus Torvalds return rc; 16891da177e4SLinus Torvalds if (new_dentry->d_inode) { 16901da177e4SLinus Torvalds new_isec = new_dentry->d_inode->i_security; 16911da177e4SLinus Torvalds new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode); 1692275bb41eSDavid Howells rc = avc_has_perm(sid, new_isec->sid, 16931da177e4SLinus Torvalds new_isec->sclass, 16941da177e4SLinus Torvalds (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad); 16951da177e4SLinus Torvalds if (rc) 16961da177e4SLinus Torvalds return rc; 16971da177e4SLinus Torvalds } 16981da177e4SLinus Torvalds 16991da177e4SLinus Torvalds return 0; 17001da177e4SLinus Torvalds } 17011da177e4SLinus Torvalds 17021da177e4SLinus Torvalds /* Check whether a task can perform a filesystem operation. */ 170388e67f3bSDavid Howells static int superblock_has_perm(const struct cred *cred, 17041da177e4SLinus Torvalds struct super_block *sb, 17051da177e4SLinus Torvalds u32 perms, 17062bf49690SThomas Liu struct common_audit_data *ad) 17071da177e4SLinus Torvalds { 17081da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 170988e67f3bSDavid Howells u32 sid = cred_sid(cred); 17101da177e4SLinus Torvalds 17111da177e4SLinus Torvalds sbsec = sb->s_security; 1712275bb41eSDavid Howells return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad); 17131da177e4SLinus Torvalds } 17141da177e4SLinus Torvalds 17151da177e4SLinus Torvalds /* Convert a Linux mode and permission mask to an access vector. */ 17161da177e4SLinus Torvalds static inline u32 file_mask_to_av(int mode, int mask) 17171da177e4SLinus Torvalds { 17181da177e4SLinus Torvalds u32 av = 0; 17191da177e4SLinus Torvalds 17201da177e4SLinus Torvalds if ((mode & S_IFMT) != S_IFDIR) { 17211da177e4SLinus Torvalds if (mask & MAY_EXEC) 17221da177e4SLinus Torvalds av |= FILE__EXECUTE; 17231da177e4SLinus Torvalds if (mask & MAY_READ) 17241da177e4SLinus Torvalds av |= FILE__READ; 17251da177e4SLinus Torvalds 17261da177e4SLinus Torvalds if (mask & MAY_APPEND) 17271da177e4SLinus Torvalds av |= FILE__APPEND; 17281da177e4SLinus Torvalds else if (mask & MAY_WRITE) 17291da177e4SLinus Torvalds av |= FILE__WRITE; 17301da177e4SLinus Torvalds 17311da177e4SLinus Torvalds } else { 17321da177e4SLinus Torvalds if (mask & MAY_EXEC) 17331da177e4SLinus Torvalds av |= DIR__SEARCH; 17341da177e4SLinus Torvalds if (mask & MAY_WRITE) 17351da177e4SLinus Torvalds av |= DIR__WRITE; 17361da177e4SLinus Torvalds if (mask & MAY_READ) 17371da177e4SLinus Torvalds av |= DIR__READ; 17381da177e4SLinus Torvalds } 17391da177e4SLinus Torvalds 17401da177e4SLinus Torvalds return av; 17411da177e4SLinus Torvalds } 17421da177e4SLinus Torvalds 17431da177e4SLinus Torvalds /* Convert a Linux file to an access vector. */ 17441da177e4SLinus Torvalds static inline u32 file_to_av(struct file *file) 17451da177e4SLinus Torvalds { 17461da177e4SLinus Torvalds u32 av = 0; 17471da177e4SLinus Torvalds 17481da177e4SLinus Torvalds if (file->f_mode & FMODE_READ) 17491da177e4SLinus Torvalds av |= FILE__READ; 17501da177e4SLinus Torvalds if (file->f_mode & FMODE_WRITE) { 17511da177e4SLinus Torvalds if (file->f_flags & O_APPEND) 17521da177e4SLinus Torvalds av |= FILE__APPEND; 17531da177e4SLinus Torvalds else 17541da177e4SLinus Torvalds av |= FILE__WRITE; 17551da177e4SLinus Torvalds } 17560794c66dSStephen Smalley if (!av) { 17570794c66dSStephen Smalley /* 17580794c66dSStephen Smalley * Special file opened with flags 3 for ioctl-only use. 17590794c66dSStephen Smalley */ 17600794c66dSStephen Smalley av = FILE__IOCTL; 17610794c66dSStephen Smalley } 17621da177e4SLinus Torvalds 17631da177e4SLinus Torvalds return av; 17641da177e4SLinus Torvalds } 17651da177e4SLinus Torvalds 17668b6a5a37SEric Paris /* 17678b6a5a37SEric Paris * Convert a file to an access vector and include the correct open 17688b6a5a37SEric Paris * open permission. 17698b6a5a37SEric Paris */ 17708b6a5a37SEric Paris static inline u32 open_file_to_av(struct file *file) 17718b6a5a37SEric Paris { 17728b6a5a37SEric Paris u32 av = file_to_av(file); 17738b6a5a37SEric Paris 177449b7b8deSEric Paris if (selinux_policycap_openperm) 17758b6a5a37SEric Paris av |= FILE__OPEN; 177649b7b8deSEric Paris 17778b6a5a37SEric Paris return av; 17788b6a5a37SEric Paris } 17798b6a5a37SEric Paris 17801da177e4SLinus Torvalds /* Hook functions begin here. */ 17811da177e4SLinus Torvalds 17829e48858fSIngo Molnar static int selinux_ptrace_access_check(struct task_struct *child, 1783006ebb40SStephen Smalley unsigned int mode) 17841da177e4SLinus Torvalds { 17851da177e4SLinus Torvalds int rc; 17861da177e4SLinus Torvalds 17879e48858fSIngo Molnar rc = cap_ptrace_access_check(child, mode); 17881da177e4SLinus Torvalds if (rc) 17891da177e4SLinus Torvalds return rc; 17901da177e4SLinus Torvalds 1791006ebb40SStephen Smalley if (mode == PTRACE_MODE_READ) { 1792275bb41eSDavid Howells u32 sid = current_sid(); 1793275bb41eSDavid Howells u32 csid = task_sid(child); 1794275bb41eSDavid Howells return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, NULL); 1795006ebb40SStephen Smalley } 1796006ebb40SStephen Smalley 17973b11a1deSDavid Howells return current_has_perm(child, PROCESS__PTRACE); 17985cd9c58fSDavid Howells } 17995cd9c58fSDavid Howells 18005cd9c58fSDavid Howells static int selinux_ptrace_traceme(struct task_struct *parent) 18015cd9c58fSDavid Howells { 18025cd9c58fSDavid Howells int rc; 18035cd9c58fSDavid Howells 1804200ac532SEric Paris rc = cap_ptrace_traceme(parent); 18055cd9c58fSDavid Howells if (rc) 18065cd9c58fSDavid Howells return rc; 18075cd9c58fSDavid Howells 18085cd9c58fSDavid Howells return task_has_perm(parent, current, PROCESS__PTRACE); 18091da177e4SLinus Torvalds } 18101da177e4SLinus Torvalds 18111da177e4SLinus Torvalds static int selinux_capget(struct task_struct *target, kernel_cap_t *effective, 18121da177e4SLinus Torvalds kernel_cap_t *inheritable, kernel_cap_t *permitted) 18131da177e4SLinus Torvalds { 18141da177e4SLinus Torvalds int error; 18151da177e4SLinus Torvalds 18163b11a1deSDavid Howells error = current_has_perm(target, PROCESS__GETCAP); 18171da177e4SLinus Torvalds if (error) 18181da177e4SLinus Torvalds return error; 18191da177e4SLinus Torvalds 1820200ac532SEric Paris return cap_capget(target, effective, inheritable, permitted); 18211da177e4SLinus Torvalds } 18221da177e4SLinus Torvalds 1823d84f4f99SDavid Howells static int selinux_capset(struct cred *new, const struct cred *old, 1824d84f4f99SDavid Howells const kernel_cap_t *effective, 182515a2460eSDavid Howells const kernel_cap_t *inheritable, 182615a2460eSDavid Howells const kernel_cap_t *permitted) 18271da177e4SLinus Torvalds { 18281da177e4SLinus Torvalds int error; 18291da177e4SLinus Torvalds 1830200ac532SEric Paris error = cap_capset(new, old, 1831d84f4f99SDavid Howells effective, inheritable, permitted); 18321da177e4SLinus Torvalds if (error) 18331da177e4SLinus Torvalds return error; 18341da177e4SLinus Torvalds 1835d84f4f99SDavid Howells return cred_has_perm(old, new, PROCESS__SETCAP); 18361da177e4SLinus Torvalds } 18371da177e4SLinus Torvalds 18385626d3e8SJames Morris /* 18395626d3e8SJames Morris * (This comment used to live with the selinux_task_setuid hook, 18405626d3e8SJames Morris * which was removed). 18415626d3e8SJames Morris * 18425626d3e8SJames Morris * Since setuid only affects the current process, and since the SELinux 18435626d3e8SJames Morris * controls are not based on the Linux identity attributes, SELinux does not 18445626d3e8SJames Morris * need to control this operation. However, SELinux does control the use of 18455626d3e8SJames Morris * the CAP_SETUID and CAP_SETGID capabilities using the capable hook. 18465626d3e8SJames Morris */ 18475626d3e8SJames Morris 18483699c53cSDavid Howells static int selinux_capable(struct task_struct *tsk, const struct cred *cred, 18493699c53cSDavid Howells int cap, int audit) 18501da177e4SLinus Torvalds { 18511da177e4SLinus Torvalds int rc; 18521da177e4SLinus Torvalds 1853200ac532SEric Paris rc = cap_capable(tsk, cred, cap, audit); 18541da177e4SLinus Torvalds if (rc) 18551da177e4SLinus Torvalds return rc; 18561da177e4SLinus Torvalds 18573699c53cSDavid Howells return task_has_capability(tsk, cred, cap, audit); 18581da177e4SLinus Torvalds } 18591da177e4SLinus Torvalds 18601da177e4SLinus Torvalds static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb) 18611da177e4SLinus Torvalds { 186288e67f3bSDavid Howells const struct cred *cred = current_cred(); 18631da177e4SLinus Torvalds int rc = 0; 18641da177e4SLinus Torvalds 18651da177e4SLinus Torvalds if (!sb) 18661da177e4SLinus Torvalds return 0; 18671da177e4SLinus Torvalds 18681da177e4SLinus Torvalds switch (cmds) { 18691da177e4SLinus Torvalds case Q_SYNC: 18701da177e4SLinus Torvalds case Q_QUOTAON: 18711da177e4SLinus Torvalds case Q_QUOTAOFF: 18721da177e4SLinus Torvalds case Q_SETINFO: 18731da177e4SLinus Torvalds case Q_SETQUOTA: 187488e67f3bSDavid Howells rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL); 18751da177e4SLinus Torvalds break; 18761da177e4SLinus Torvalds case Q_GETFMT: 18771da177e4SLinus Torvalds case Q_GETINFO: 18781da177e4SLinus Torvalds case Q_GETQUOTA: 187988e67f3bSDavid Howells rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL); 18801da177e4SLinus Torvalds break; 18811da177e4SLinus Torvalds default: 18821da177e4SLinus Torvalds rc = 0; /* let the kernel handle invalid cmds */ 18831da177e4SLinus Torvalds break; 18841da177e4SLinus Torvalds } 18851da177e4SLinus Torvalds return rc; 18861da177e4SLinus Torvalds } 18871da177e4SLinus Torvalds 18881da177e4SLinus Torvalds static int selinux_quota_on(struct dentry *dentry) 18891da177e4SLinus Torvalds { 189088e67f3bSDavid Howells const struct cred *cred = current_cred(); 189188e67f3bSDavid Howells 189288e67f3bSDavid Howells return dentry_has_perm(cred, NULL, dentry, FILE__QUOTAON); 18931da177e4SLinus Torvalds } 18941da177e4SLinus Torvalds 189512b3052cSEric Paris static int selinux_syslog(int type) 18961da177e4SLinus Torvalds { 18971da177e4SLinus Torvalds int rc; 18981da177e4SLinus Torvalds 18991da177e4SLinus Torvalds switch (type) { 1900d78ca3cdSKees Cook case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */ 1901d78ca3cdSKees Cook case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */ 19021da177e4SLinus Torvalds rc = task_has_system(current, SYSTEM__SYSLOG_READ); 19031da177e4SLinus Torvalds break; 1904d78ca3cdSKees Cook case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */ 1905d78ca3cdSKees Cook case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */ 1906d78ca3cdSKees Cook /* Set level of messages printed to console */ 1907d78ca3cdSKees Cook case SYSLOG_ACTION_CONSOLE_LEVEL: 19081da177e4SLinus Torvalds rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE); 19091da177e4SLinus Torvalds break; 1910d78ca3cdSKees Cook case SYSLOG_ACTION_CLOSE: /* Close log */ 1911d78ca3cdSKees Cook case SYSLOG_ACTION_OPEN: /* Open log */ 1912d78ca3cdSKees Cook case SYSLOG_ACTION_READ: /* Read from log */ 1913d78ca3cdSKees Cook case SYSLOG_ACTION_READ_CLEAR: /* Read/clear last kernel messages */ 1914d78ca3cdSKees Cook case SYSLOG_ACTION_CLEAR: /* Clear ring buffer */ 19151da177e4SLinus Torvalds default: 19161da177e4SLinus Torvalds rc = task_has_system(current, SYSTEM__SYSLOG_MOD); 19171da177e4SLinus Torvalds break; 19181da177e4SLinus Torvalds } 19191da177e4SLinus Torvalds return rc; 19201da177e4SLinus Torvalds } 19211da177e4SLinus Torvalds 19221da177e4SLinus Torvalds /* 19231da177e4SLinus Torvalds * Check that a process has enough memory to allocate a new virtual 19241da177e4SLinus Torvalds * mapping. 0 means there is enough memory for the allocation to 19251da177e4SLinus Torvalds * succeed and -ENOMEM implies there is not. 19261da177e4SLinus Torvalds * 19271da177e4SLinus Torvalds * Do not audit the selinux permission check, as this is applied to all 19281da177e4SLinus Torvalds * processes that allocate mappings. 19291da177e4SLinus Torvalds */ 193034b4e4aaSAlan Cox static int selinux_vm_enough_memory(struct mm_struct *mm, long pages) 19311da177e4SLinus Torvalds { 19321da177e4SLinus Torvalds int rc, cap_sys_admin = 0; 19331da177e4SLinus Torvalds 19343699c53cSDavid Howells rc = selinux_capable(current, current_cred(), CAP_SYS_ADMIN, 19353699c53cSDavid Howells SECURITY_CAP_NOAUDIT); 19361da177e4SLinus Torvalds if (rc == 0) 19371da177e4SLinus Torvalds cap_sys_admin = 1; 19381da177e4SLinus Torvalds 193934b4e4aaSAlan Cox return __vm_enough_memory(mm, pages, cap_sys_admin); 19401da177e4SLinus Torvalds } 19411da177e4SLinus Torvalds 19421da177e4SLinus Torvalds /* binprm security operations */ 19431da177e4SLinus Torvalds 1944a6f76f23SDavid Howells static int selinux_bprm_set_creds(struct linux_binprm *bprm) 19451da177e4SLinus Torvalds { 1946a6f76f23SDavid Howells const struct task_security_struct *old_tsec; 1947a6f76f23SDavid Howells struct task_security_struct *new_tsec; 19481da177e4SLinus Torvalds struct inode_security_struct *isec; 19492bf49690SThomas Liu struct common_audit_data ad; 1950a6f76f23SDavid Howells struct inode *inode = bprm->file->f_path.dentry->d_inode; 19511da177e4SLinus Torvalds int rc; 19521da177e4SLinus Torvalds 1953200ac532SEric Paris rc = cap_bprm_set_creds(bprm); 19541da177e4SLinus Torvalds if (rc) 19551da177e4SLinus Torvalds return rc; 19561da177e4SLinus Torvalds 1957a6f76f23SDavid Howells /* SELinux context only depends on initial program or script and not 1958a6f76f23SDavid Howells * the script interpreter */ 1959a6f76f23SDavid Howells if (bprm->cred_prepared) 19601da177e4SLinus Torvalds return 0; 19611da177e4SLinus Torvalds 1962a6f76f23SDavid Howells old_tsec = current_security(); 1963a6f76f23SDavid Howells new_tsec = bprm->cred->security; 19641da177e4SLinus Torvalds isec = inode->i_security; 19651da177e4SLinus Torvalds 19661da177e4SLinus Torvalds /* Default to the current task SID. */ 1967a6f76f23SDavid Howells new_tsec->sid = old_tsec->sid; 1968a6f76f23SDavid Howells new_tsec->osid = old_tsec->sid; 19691da177e4SLinus Torvalds 197028eba5bfSMichael LeMay /* Reset fs, key, and sock SIDs on execve. */ 1971a6f76f23SDavid Howells new_tsec->create_sid = 0; 1972a6f76f23SDavid Howells new_tsec->keycreate_sid = 0; 1973a6f76f23SDavid Howells new_tsec->sockcreate_sid = 0; 19741da177e4SLinus Torvalds 1975a6f76f23SDavid Howells if (old_tsec->exec_sid) { 1976a6f76f23SDavid Howells new_tsec->sid = old_tsec->exec_sid; 19771da177e4SLinus Torvalds /* Reset exec SID on execve. */ 1978a6f76f23SDavid Howells new_tsec->exec_sid = 0; 19791da177e4SLinus Torvalds } else { 19801da177e4SLinus Torvalds /* Check for a default transition on this program. */ 1981a6f76f23SDavid Howells rc = security_transition_sid(old_tsec->sid, isec->sid, 1982652bb9b0SEric Paris SECCLASS_PROCESS, NULL, 1983652bb9b0SEric Paris &new_tsec->sid); 19841da177e4SLinus Torvalds if (rc) 19851da177e4SLinus Torvalds return rc; 19861da177e4SLinus Torvalds } 19871da177e4SLinus Torvalds 19882bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, FS); 198944707fdfSJan Blunck ad.u.fs.path = bprm->file->f_path; 19901da177e4SLinus Torvalds 19913d5ff529SJosef Sipek if (bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) 1992a6f76f23SDavid Howells new_tsec->sid = old_tsec->sid; 19931da177e4SLinus Torvalds 1994a6f76f23SDavid Howells if (new_tsec->sid == old_tsec->sid) { 1995a6f76f23SDavid Howells rc = avc_has_perm(old_tsec->sid, isec->sid, 19961da177e4SLinus Torvalds SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad); 19971da177e4SLinus Torvalds if (rc) 19981da177e4SLinus Torvalds return rc; 19991da177e4SLinus Torvalds } else { 20001da177e4SLinus Torvalds /* Check permissions for the transition. */ 2001a6f76f23SDavid Howells rc = avc_has_perm(old_tsec->sid, new_tsec->sid, 20021da177e4SLinus Torvalds SECCLASS_PROCESS, PROCESS__TRANSITION, &ad); 20031da177e4SLinus Torvalds if (rc) 20041da177e4SLinus Torvalds return rc; 20051da177e4SLinus Torvalds 2006a6f76f23SDavid Howells rc = avc_has_perm(new_tsec->sid, isec->sid, 20071da177e4SLinus Torvalds SECCLASS_FILE, FILE__ENTRYPOINT, &ad); 20081da177e4SLinus Torvalds if (rc) 20091da177e4SLinus Torvalds return rc; 20101da177e4SLinus Torvalds 2011a6f76f23SDavid Howells /* Check for shared state */ 2012a6f76f23SDavid Howells if (bprm->unsafe & LSM_UNSAFE_SHARE) { 2013a6f76f23SDavid Howells rc = avc_has_perm(old_tsec->sid, new_tsec->sid, 2014a6f76f23SDavid Howells SECCLASS_PROCESS, PROCESS__SHARE, 2015a6f76f23SDavid Howells NULL); 2016a6f76f23SDavid Howells if (rc) 2017a6f76f23SDavid Howells return -EPERM; 20181da177e4SLinus Torvalds } 20191da177e4SLinus Torvalds 2020a6f76f23SDavid Howells /* Make sure that anyone attempting to ptrace over a task that 2021a6f76f23SDavid Howells * changes its SID has the appropriate permit */ 2022a6f76f23SDavid Howells if (bprm->unsafe & 2023a6f76f23SDavid Howells (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) { 2024a6f76f23SDavid Howells struct task_struct *tracer; 2025a6f76f23SDavid Howells struct task_security_struct *sec; 2026a6f76f23SDavid Howells u32 ptsid = 0; 2027a6f76f23SDavid Howells 2028a6f76f23SDavid Howells rcu_read_lock(); 2029a6f76f23SDavid Howells tracer = tracehook_tracer_task(current); 2030a6f76f23SDavid Howells if (likely(tracer != NULL)) { 2031a6f76f23SDavid Howells sec = __task_cred(tracer)->security; 2032a6f76f23SDavid Howells ptsid = sec->sid; 2033a6f76f23SDavid Howells } 2034a6f76f23SDavid Howells rcu_read_unlock(); 2035a6f76f23SDavid Howells 2036a6f76f23SDavid Howells if (ptsid != 0) { 2037a6f76f23SDavid Howells rc = avc_has_perm(ptsid, new_tsec->sid, 2038a6f76f23SDavid Howells SECCLASS_PROCESS, 2039a6f76f23SDavid Howells PROCESS__PTRACE, NULL); 2040a6f76f23SDavid Howells if (rc) 2041a6f76f23SDavid Howells return -EPERM; 2042a6f76f23SDavid Howells } 2043a6f76f23SDavid Howells } 2044a6f76f23SDavid Howells 2045a6f76f23SDavid Howells /* Clear any possibly unsafe personality bits on exec: */ 2046a6f76f23SDavid Howells bprm->per_clear |= PER_CLEAR_ON_SETID; 2047a6f76f23SDavid Howells } 2048a6f76f23SDavid Howells 20491da177e4SLinus Torvalds return 0; 20501da177e4SLinus Torvalds } 20511da177e4SLinus Torvalds 20521da177e4SLinus Torvalds static int selinux_bprm_secureexec(struct linux_binprm *bprm) 20531da177e4SLinus Torvalds { 20545fb49870SPaul Moore const struct task_security_struct *tsec = current_security(); 2055275bb41eSDavid Howells u32 sid, osid; 20561da177e4SLinus Torvalds int atsecure = 0; 20571da177e4SLinus Torvalds 2058275bb41eSDavid Howells sid = tsec->sid; 2059275bb41eSDavid Howells osid = tsec->osid; 2060275bb41eSDavid Howells 2061275bb41eSDavid Howells if (osid != sid) { 20621da177e4SLinus Torvalds /* Enable secure mode for SIDs transitions unless 20631da177e4SLinus Torvalds the noatsecure permission is granted between 20641da177e4SLinus Torvalds the two SIDs, i.e. ahp returns 0. */ 2065275bb41eSDavid Howells atsecure = avc_has_perm(osid, sid, 20661da177e4SLinus Torvalds SECCLASS_PROCESS, 20671da177e4SLinus Torvalds PROCESS__NOATSECURE, NULL); 20681da177e4SLinus Torvalds } 20691da177e4SLinus Torvalds 2070200ac532SEric Paris return (atsecure || cap_bprm_secureexec(bprm)); 20711da177e4SLinus Torvalds } 20721da177e4SLinus Torvalds 20731da177e4SLinus Torvalds extern struct vfsmount *selinuxfs_mount; 20741da177e4SLinus Torvalds extern struct dentry *selinux_null; 20751da177e4SLinus Torvalds 20761da177e4SLinus Torvalds /* Derived from fs/exec.c:flush_old_files. */ 2077745ca247SDavid Howells static inline void flush_unauthorized_files(const struct cred *cred, 2078745ca247SDavid Howells struct files_struct *files) 20791da177e4SLinus Torvalds { 20802bf49690SThomas Liu struct common_audit_data ad; 20811da177e4SLinus Torvalds struct file *file, *devnull = NULL; 2082b20c8122SStephen Smalley struct tty_struct *tty; 2083badf1662SDipankar Sarma struct fdtable *fdt; 20841da177e4SLinus Torvalds long j = -1; 208524ec839cSPeter Zijlstra int drop_tty = 0; 20861da177e4SLinus Torvalds 208724ec839cSPeter Zijlstra tty = get_current_tty(); 20881da177e4SLinus Torvalds if (tty) { 2089ee2ffa0dSNick Piggin spin_lock(&tty_files_lock); 209037dd0bd0SEric Paris if (!list_empty(&tty->tty_files)) { 2091d996b62aSNick Piggin struct tty_file_private *file_priv; 209237dd0bd0SEric Paris struct inode *inode; 209337dd0bd0SEric Paris 20941da177e4SLinus Torvalds /* Revalidate access to controlling tty. 20951da177e4SLinus Torvalds Use inode_has_perm on the tty inode directly rather 20961da177e4SLinus Torvalds than using file_has_perm, as this particular open 20971da177e4SLinus Torvalds file may belong to another process and we are only 20981da177e4SLinus Torvalds interested in the inode-based check here. */ 2099d996b62aSNick Piggin file_priv = list_first_entry(&tty->tty_files, 2100d996b62aSNick Piggin struct tty_file_private, list); 2101d996b62aSNick Piggin file = file_priv->file; 210237dd0bd0SEric Paris inode = file->f_path.dentry->d_inode; 210388e67f3bSDavid Howells if (inode_has_perm(cred, inode, 21041da177e4SLinus Torvalds FILE__READ | FILE__WRITE, NULL)) { 210524ec839cSPeter Zijlstra drop_tty = 1; 21061da177e4SLinus Torvalds } 21071da177e4SLinus Torvalds } 2108ee2ffa0dSNick Piggin spin_unlock(&tty_files_lock); 2109452a00d2SAlan Cox tty_kref_put(tty); 21101da177e4SLinus Torvalds } 211198a27ba4SEric W. Biederman /* Reset controlling tty. */ 211298a27ba4SEric W. Biederman if (drop_tty) 211398a27ba4SEric W. Biederman no_tty(); 21141da177e4SLinus Torvalds 21151da177e4SLinus Torvalds /* Revalidate access to inherited open files. */ 21161da177e4SLinus Torvalds 21172bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, FS); 21181da177e4SLinus Torvalds 21191da177e4SLinus Torvalds spin_lock(&files->file_lock); 21201da177e4SLinus Torvalds for (;;) { 21211da177e4SLinus Torvalds unsigned long set, i; 21221da177e4SLinus Torvalds int fd; 21231da177e4SLinus Torvalds 21241da177e4SLinus Torvalds j++; 21251da177e4SLinus Torvalds i = j * __NFDBITS; 2126badf1662SDipankar Sarma fdt = files_fdtable(files); 2127bbea9f69SVadim Lobanov if (i >= fdt->max_fds) 21281da177e4SLinus Torvalds break; 2129badf1662SDipankar Sarma set = fdt->open_fds->fds_bits[j]; 21301da177e4SLinus Torvalds if (!set) 21311da177e4SLinus Torvalds continue; 21321da177e4SLinus Torvalds spin_unlock(&files->file_lock); 21331da177e4SLinus Torvalds for ( ; set ; i++, set >>= 1) { 21341da177e4SLinus Torvalds if (set & 1) { 21351da177e4SLinus Torvalds file = fget(i); 21361da177e4SLinus Torvalds if (!file) 21371da177e4SLinus Torvalds continue; 213888e67f3bSDavid Howells if (file_has_perm(cred, 21391da177e4SLinus Torvalds file, 21401da177e4SLinus Torvalds file_to_av(file))) { 21411da177e4SLinus Torvalds sys_close(i); 21421da177e4SLinus Torvalds fd = get_unused_fd(); 21431da177e4SLinus Torvalds if (fd != i) { 21441da177e4SLinus Torvalds if (fd >= 0) 21451da177e4SLinus Torvalds put_unused_fd(fd); 21461da177e4SLinus Torvalds fput(file); 21471da177e4SLinus Torvalds continue; 21481da177e4SLinus Torvalds } 21491da177e4SLinus Torvalds if (devnull) { 2150095975daSNick Piggin get_file(devnull); 21511da177e4SLinus Torvalds } else { 2152745ca247SDavid Howells devnull = dentry_open( 2153745ca247SDavid Howells dget(selinux_null), 2154745ca247SDavid Howells mntget(selinuxfs_mount), 2155745ca247SDavid Howells O_RDWR, cred); 2156fc5d81e6SAkinobu Mita if (IS_ERR(devnull)) { 2157fc5d81e6SAkinobu Mita devnull = NULL; 21581da177e4SLinus Torvalds put_unused_fd(fd); 21591da177e4SLinus Torvalds fput(file); 21601da177e4SLinus Torvalds continue; 21611da177e4SLinus Torvalds } 21621da177e4SLinus Torvalds } 21631da177e4SLinus Torvalds fd_install(fd, devnull); 21641da177e4SLinus Torvalds } 21651da177e4SLinus Torvalds fput(file); 21661da177e4SLinus Torvalds } 21671da177e4SLinus Torvalds } 21681da177e4SLinus Torvalds spin_lock(&files->file_lock); 21691da177e4SLinus Torvalds 21701da177e4SLinus Torvalds } 21711da177e4SLinus Torvalds spin_unlock(&files->file_lock); 21721da177e4SLinus Torvalds } 21731da177e4SLinus Torvalds 21741da177e4SLinus Torvalds /* 2175a6f76f23SDavid Howells * Prepare a process for imminent new credential changes due to exec 21761da177e4SLinus Torvalds */ 2177a6f76f23SDavid Howells static void selinux_bprm_committing_creds(struct linux_binprm *bprm) 21781da177e4SLinus Torvalds { 2179a6f76f23SDavid Howells struct task_security_struct *new_tsec; 21801da177e4SLinus Torvalds struct rlimit *rlim, *initrlim; 21811da177e4SLinus Torvalds int rc, i; 21821da177e4SLinus Torvalds 2183a6f76f23SDavid Howells new_tsec = bprm->cred->security; 2184a6f76f23SDavid Howells if (new_tsec->sid == new_tsec->osid) 21851da177e4SLinus Torvalds return; 21861da177e4SLinus Torvalds 21871da177e4SLinus Torvalds /* Close files for which the new task SID is not authorized. */ 2188a6f76f23SDavid Howells flush_unauthorized_files(bprm->cred, current->files); 21891da177e4SLinus Torvalds 2190a6f76f23SDavid Howells /* Always clear parent death signal on SID transitions. */ 2191a6f76f23SDavid Howells current->pdeath_signal = 0; 2192a6f76f23SDavid Howells 2193a6f76f23SDavid Howells /* Check whether the new SID can inherit resource limits from the old 2194a6f76f23SDavid Howells * SID. If not, reset all soft limits to the lower of the current 2195a6f76f23SDavid Howells * task's hard limit and the init task's soft limit. 2196a6f76f23SDavid Howells * 2197a6f76f23SDavid Howells * Note that the setting of hard limits (even to lower them) can be 2198a6f76f23SDavid Howells * controlled by the setrlimit check. The inclusion of the init task's 2199a6f76f23SDavid Howells * soft limit into the computation is to avoid resetting soft limits 2200a6f76f23SDavid Howells * higher than the default soft limit for cases where the default is 2201a6f76f23SDavid Howells * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK. 2202a6f76f23SDavid Howells */ 2203a6f76f23SDavid Howells rc = avc_has_perm(new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS, 2204a6f76f23SDavid Howells PROCESS__RLIMITINH, NULL); 2205a6f76f23SDavid Howells if (rc) { 2206eb2d55a3SOleg Nesterov /* protect against do_prlimit() */ 2207eb2d55a3SOleg Nesterov task_lock(current); 2208a6f76f23SDavid Howells for (i = 0; i < RLIM_NLIMITS; i++) { 2209a6f76f23SDavid Howells rlim = current->signal->rlim + i; 2210a6f76f23SDavid Howells initrlim = init_task.signal->rlim + i; 2211a6f76f23SDavid Howells rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur); 2212a6f76f23SDavid Howells } 2213eb2d55a3SOleg Nesterov task_unlock(current); 2214eb2d55a3SOleg Nesterov update_rlimit_cpu(current, rlimit(RLIMIT_CPU)); 2215a6f76f23SDavid Howells } 2216a6f76f23SDavid Howells } 2217a6f76f23SDavid Howells 2218a6f76f23SDavid Howells /* 2219a6f76f23SDavid Howells * Clean up the process immediately after the installation of new credentials 2220a6f76f23SDavid Howells * due to exec 2221a6f76f23SDavid Howells */ 2222a6f76f23SDavid Howells static void selinux_bprm_committed_creds(struct linux_binprm *bprm) 2223a6f76f23SDavid Howells { 2224a6f76f23SDavid Howells const struct task_security_struct *tsec = current_security(); 2225a6f76f23SDavid Howells struct itimerval itimer; 2226a6f76f23SDavid Howells u32 osid, sid; 2227a6f76f23SDavid Howells int rc, i; 2228a6f76f23SDavid Howells 2229a6f76f23SDavid Howells osid = tsec->osid; 2230a6f76f23SDavid Howells sid = tsec->sid; 2231a6f76f23SDavid Howells 2232a6f76f23SDavid Howells if (sid == osid) 2233a6f76f23SDavid Howells return; 2234a6f76f23SDavid Howells 2235a6f76f23SDavid Howells /* Check whether the new SID can inherit signal state from the old SID. 2236a6f76f23SDavid Howells * If not, clear itimers to avoid subsequent signal generation and 2237a6f76f23SDavid Howells * flush and unblock signals. 2238a6f76f23SDavid Howells * 2239a6f76f23SDavid Howells * This must occur _after_ the task SID has been updated so that any 2240a6f76f23SDavid Howells * kill done after the flush will be checked against the new SID. 2241a6f76f23SDavid Howells */ 2242a6f76f23SDavid Howells rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL); 22431da177e4SLinus Torvalds if (rc) { 22441da177e4SLinus Torvalds memset(&itimer, 0, sizeof itimer); 22451da177e4SLinus Torvalds for (i = 0; i < 3; i++) 22461da177e4SLinus Torvalds do_setitimer(i, &itimer, NULL); 22471da177e4SLinus Torvalds spin_lock_irq(¤t->sighand->siglock); 22483bcac026SDavid Howells if (!(current->signal->flags & SIGNAL_GROUP_EXIT)) { 22493bcac026SDavid Howells __flush_signals(current); 22501da177e4SLinus Torvalds flush_signal_handlers(current, 1); 22511da177e4SLinus Torvalds sigemptyset(¤t->blocked); 22523bcac026SDavid Howells } 22531da177e4SLinus Torvalds spin_unlock_irq(¤t->sighand->siglock); 22541da177e4SLinus Torvalds } 22551da177e4SLinus Torvalds 2256a6f76f23SDavid Howells /* Wake up the parent if it is waiting so that it can recheck 2257a6f76f23SDavid Howells * wait permission to the new task SID. */ 2258ecd6de3cSOleg Nesterov read_lock(&tasklist_lock); 22590b7570e7SOleg Nesterov __wake_up_parent(current, current->real_parent); 2260ecd6de3cSOleg Nesterov read_unlock(&tasklist_lock); 22611da177e4SLinus Torvalds } 22621da177e4SLinus Torvalds 22631da177e4SLinus Torvalds /* superblock security operations */ 22641da177e4SLinus Torvalds 22651da177e4SLinus Torvalds static int selinux_sb_alloc_security(struct super_block *sb) 22661da177e4SLinus Torvalds { 22671da177e4SLinus Torvalds return superblock_alloc_security(sb); 22681da177e4SLinus Torvalds } 22691da177e4SLinus Torvalds 22701da177e4SLinus Torvalds static void selinux_sb_free_security(struct super_block *sb) 22711da177e4SLinus Torvalds { 22721da177e4SLinus Torvalds superblock_free_security(sb); 22731da177e4SLinus Torvalds } 22741da177e4SLinus Torvalds 22751da177e4SLinus Torvalds static inline int match_prefix(char *prefix, int plen, char *option, int olen) 22761da177e4SLinus Torvalds { 22771da177e4SLinus Torvalds if (plen > olen) 22781da177e4SLinus Torvalds return 0; 22791da177e4SLinus Torvalds 22801da177e4SLinus Torvalds return !memcmp(prefix, option, plen); 22811da177e4SLinus Torvalds } 22821da177e4SLinus Torvalds 22831da177e4SLinus Torvalds static inline int selinux_option(char *option, int len) 22841da177e4SLinus Torvalds { 2285832cbd9aSEric Paris return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) || 2286832cbd9aSEric Paris match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) || 2287832cbd9aSEric Paris match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) || 228811689d47SDavid P. Quigley match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) || 228911689d47SDavid P. Quigley match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len)); 22901da177e4SLinus Torvalds } 22911da177e4SLinus Torvalds 22921da177e4SLinus Torvalds static inline void take_option(char **to, char *from, int *first, int len) 22931da177e4SLinus Torvalds { 22941da177e4SLinus Torvalds if (!*first) { 22951da177e4SLinus Torvalds **to = ','; 22961da177e4SLinus Torvalds *to += 1; 22973528a953SCory Olmo } else 22981da177e4SLinus Torvalds *first = 0; 22991da177e4SLinus Torvalds memcpy(*to, from, len); 23001da177e4SLinus Torvalds *to += len; 23011da177e4SLinus Torvalds } 23021da177e4SLinus Torvalds 23033528a953SCory Olmo static inline void take_selinux_option(char **to, char *from, int *first, 23043528a953SCory Olmo int len) 23053528a953SCory Olmo { 23063528a953SCory Olmo int current_size = 0; 23073528a953SCory Olmo 23083528a953SCory Olmo if (!*first) { 23093528a953SCory Olmo **to = '|'; 23103528a953SCory Olmo *to += 1; 2311828dfe1dSEric Paris } else 23123528a953SCory Olmo *first = 0; 23133528a953SCory Olmo 23143528a953SCory Olmo while (current_size < len) { 23153528a953SCory Olmo if (*from != '"') { 23163528a953SCory Olmo **to = *from; 23173528a953SCory Olmo *to += 1; 23183528a953SCory Olmo } 23193528a953SCory Olmo from += 1; 23203528a953SCory Olmo current_size += 1; 23213528a953SCory Olmo } 23223528a953SCory Olmo } 23233528a953SCory Olmo 2324e0007529SEric Paris static int selinux_sb_copy_data(char *orig, char *copy) 23251da177e4SLinus Torvalds { 23261da177e4SLinus Torvalds int fnosec, fsec, rc = 0; 23271da177e4SLinus Torvalds char *in_save, *in_curr, *in_end; 23281da177e4SLinus Torvalds char *sec_curr, *nosec_save, *nosec; 23293528a953SCory Olmo int open_quote = 0; 23301da177e4SLinus Torvalds 23311da177e4SLinus Torvalds in_curr = orig; 23321da177e4SLinus Torvalds sec_curr = copy; 23331da177e4SLinus Torvalds 23341da177e4SLinus Torvalds nosec = (char *)get_zeroed_page(GFP_KERNEL); 23351da177e4SLinus Torvalds if (!nosec) { 23361da177e4SLinus Torvalds rc = -ENOMEM; 23371da177e4SLinus Torvalds goto out; 23381da177e4SLinus Torvalds } 23391da177e4SLinus Torvalds 23401da177e4SLinus Torvalds nosec_save = nosec; 23411da177e4SLinus Torvalds fnosec = fsec = 1; 23421da177e4SLinus Torvalds in_save = in_end = orig; 23431da177e4SLinus Torvalds 23441da177e4SLinus Torvalds do { 23453528a953SCory Olmo if (*in_end == '"') 23463528a953SCory Olmo open_quote = !open_quote; 23473528a953SCory Olmo if ((*in_end == ',' && open_quote == 0) || 23483528a953SCory Olmo *in_end == '\0') { 23491da177e4SLinus Torvalds int len = in_end - in_curr; 23501da177e4SLinus Torvalds 23511da177e4SLinus Torvalds if (selinux_option(in_curr, len)) 23523528a953SCory Olmo take_selinux_option(&sec_curr, in_curr, &fsec, len); 23531da177e4SLinus Torvalds else 23541da177e4SLinus Torvalds take_option(&nosec, in_curr, &fnosec, len); 23551da177e4SLinus Torvalds 23561da177e4SLinus Torvalds in_curr = in_end + 1; 23571da177e4SLinus Torvalds } 23581da177e4SLinus Torvalds } while (*in_end++); 23591da177e4SLinus Torvalds 23606931dfc9SEric Paris strcpy(in_save, nosec_save); 2361da3caa20SGerald Schaefer free_page((unsigned long)nosec_save); 23621da177e4SLinus Torvalds out: 23631da177e4SLinus Torvalds return rc; 23641da177e4SLinus Torvalds } 23651da177e4SLinus Torvalds 236612204e24SJames Morris static int selinux_sb_kern_mount(struct super_block *sb, int flags, void *data) 23671da177e4SLinus Torvalds { 236888e67f3bSDavid Howells const struct cred *cred = current_cred(); 23692bf49690SThomas Liu struct common_audit_data ad; 23701da177e4SLinus Torvalds int rc; 23711da177e4SLinus Torvalds 23721da177e4SLinus Torvalds rc = superblock_doinit(sb, data); 23731da177e4SLinus Torvalds if (rc) 23741da177e4SLinus Torvalds return rc; 23751da177e4SLinus Torvalds 237674192246SJames Morris /* Allow all mounts performed by the kernel */ 237774192246SJames Morris if (flags & MS_KERNMOUNT) 237874192246SJames Morris return 0; 237974192246SJames Morris 23802bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, FS); 238144707fdfSJan Blunck ad.u.fs.path.dentry = sb->s_root; 238288e67f3bSDavid Howells return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad); 23831da177e4SLinus Torvalds } 23841da177e4SLinus Torvalds 2385726c3342SDavid Howells static int selinux_sb_statfs(struct dentry *dentry) 23861da177e4SLinus Torvalds { 238788e67f3bSDavid Howells const struct cred *cred = current_cred(); 23882bf49690SThomas Liu struct common_audit_data ad; 23891da177e4SLinus Torvalds 23902bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, FS); 239144707fdfSJan Blunck ad.u.fs.path.dentry = dentry->d_sb->s_root; 239288e67f3bSDavid Howells return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad); 23931da177e4SLinus Torvalds } 23941da177e4SLinus Torvalds 23951da177e4SLinus Torvalds static int selinux_mount(char *dev_name, 2396b5266eb4SAl Viro struct path *path, 23971da177e4SLinus Torvalds char *type, 23981da177e4SLinus Torvalds unsigned long flags, 23991da177e4SLinus Torvalds void *data) 24001da177e4SLinus Torvalds { 240188e67f3bSDavid Howells const struct cred *cred = current_cred(); 24021da177e4SLinus Torvalds 24031da177e4SLinus Torvalds if (flags & MS_REMOUNT) 240488e67f3bSDavid Howells return superblock_has_perm(cred, path->mnt->mnt_sb, 24051da177e4SLinus Torvalds FILESYSTEM__REMOUNT, NULL); 24061da177e4SLinus Torvalds else 240788e67f3bSDavid Howells return dentry_has_perm(cred, path->mnt, path->dentry, 24081da177e4SLinus Torvalds FILE__MOUNTON); 24091da177e4SLinus Torvalds } 24101da177e4SLinus Torvalds 24111da177e4SLinus Torvalds static int selinux_umount(struct vfsmount *mnt, int flags) 24121da177e4SLinus Torvalds { 241388e67f3bSDavid Howells const struct cred *cred = current_cred(); 24141da177e4SLinus Torvalds 241588e67f3bSDavid Howells return superblock_has_perm(cred, mnt->mnt_sb, 24161da177e4SLinus Torvalds FILESYSTEM__UNMOUNT, NULL); 24171da177e4SLinus Torvalds } 24181da177e4SLinus Torvalds 24191da177e4SLinus Torvalds /* inode security operations */ 24201da177e4SLinus Torvalds 24211da177e4SLinus Torvalds static int selinux_inode_alloc_security(struct inode *inode) 24221da177e4SLinus Torvalds { 24231da177e4SLinus Torvalds return inode_alloc_security(inode); 24241da177e4SLinus Torvalds } 24251da177e4SLinus Torvalds 24261da177e4SLinus Torvalds static void selinux_inode_free_security(struct inode *inode) 24271da177e4SLinus Torvalds { 24281da177e4SLinus Torvalds inode_free_security(inode); 24291da177e4SLinus Torvalds } 24301da177e4SLinus Torvalds 24315e41ff9eSStephen Smalley static int selinux_inode_init_security(struct inode *inode, struct inode *dir, 24322a7dba39SEric Paris const struct qstr *qstr, char **name, 24332a7dba39SEric Paris void **value, size_t *len) 24345e41ff9eSStephen Smalley { 24355fb49870SPaul Moore const struct task_security_struct *tsec = current_security(); 24365e41ff9eSStephen Smalley struct inode_security_struct *dsec; 24375e41ff9eSStephen Smalley struct superblock_security_struct *sbsec; 2438275bb41eSDavid Howells u32 sid, newsid, clen; 24395e41ff9eSStephen Smalley int rc; 2440570bc1c2SStephen Smalley char *namep = NULL, *context; 24415e41ff9eSStephen Smalley 24425e41ff9eSStephen Smalley dsec = dir->i_security; 24435e41ff9eSStephen Smalley sbsec = dir->i_sb->s_security; 24445e41ff9eSStephen Smalley 2445275bb41eSDavid Howells sid = tsec->sid; 24465e41ff9eSStephen Smalley newsid = tsec->create_sid; 2447275bb41eSDavid Howells 2448415103f9SEric Paris if ((sbsec->flags & SE_SBINITIALIZED) && 2449415103f9SEric Paris (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) 2450415103f9SEric Paris newsid = sbsec->mntpoint_sid; 2451415103f9SEric Paris else if (!newsid || !(sbsec->flags & SE_SBLABELSUPP)) { 2452275bb41eSDavid Howells rc = security_transition_sid(sid, dsec->sid, 24535e41ff9eSStephen Smalley inode_mode_to_security_class(inode->i_mode), 2454652bb9b0SEric Paris qstr, &newsid); 24555e41ff9eSStephen Smalley if (rc) { 24565e41ff9eSStephen Smalley printk(KERN_WARNING "%s: " 24575e41ff9eSStephen Smalley "security_transition_sid failed, rc=%d (dev=%s " 24585e41ff9eSStephen Smalley "ino=%ld)\n", 2459dd6f953aSHarvey Harrison __func__, 24605e41ff9eSStephen Smalley -rc, inode->i_sb->s_id, inode->i_ino); 24615e41ff9eSStephen Smalley return rc; 24625e41ff9eSStephen Smalley } 24635e41ff9eSStephen Smalley } 24645e41ff9eSStephen Smalley 2465296fddf7SEric Paris /* Possibly defer initialization to selinux_complete_init. */ 24660d90a7ecSDavid P. Quigley if (sbsec->flags & SE_SBINITIALIZED) { 2467296fddf7SEric Paris struct inode_security_struct *isec = inode->i_security; 2468296fddf7SEric Paris isec->sclass = inode_mode_to_security_class(inode->i_mode); 2469296fddf7SEric Paris isec->sid = newsid; 2470296fddf7SEric Paris isec->initialized = 1; 2471296fddf7SEric Paris } 24725e41ff9eSStephen Smalley 2473cd89596fSDavid P. Quigley if (!ss_initialized || !(sbsec->flags & SE_SBLABELSUPP)) 247425a74f3bSStephen Smalley return -EOPNOTSUPP; 247525a74f3bSStephen Smalley 2476570bc1c2SStephen Smalley if (name) { 2477a02fe132SJosef Bacik namep = kstrdup(XATTR_SELINUX_SUFFIX, GFP_NOFS); 24785e41ff9eSStephen Smalley if (!namep) 24795e41ff9eSStephen Smalley return -ENOMEM; 24805e41ff9eSStephen Smalley *name = namep; 2481570bc1c2SStephen Smalley } 24825e41ff9eSStephen Smalley 2483570bc1c2SStephen Smalley if (value && len) { 248412b29f34SStephen Smalley rc = security_sid_to_context_force(newsid, &context, &clen); 24855e41ff9eSStephen Smalley if (rc) { 24865e41ff9eSStephen Smalley kfree(namep); 24875e41ff9eSStephen Smalley return rc; 24885e41ff9eSStephen Smalley } 24895e41ff9eSStephen Smalley *value = context; 2490570bc1c2SStephen Smalley *len = clen; 2491570bc1c2SStephen Smalley } 24925e41ff9eSStephen Smalley 24935e41ff9eSStephen Smalley return 0; 24945e41ff9eSStephen Smalley } 24955e41ff9eSStephen Smalley 24961da177e4SLinus Torvalds static int selinux_inode_create(struct inode *dir, struct dentry *dentry, int mask) 24971da177e4SLinus Torvalds { 24981da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_FILE); 24991da177e4SLinus Torvalds } 25001da177e4SLinus Torvalds 25011da177e4SLinus Torvalds static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 25021da177e4SLinus Torvalds { 25031da177e4SLinus Torvalds return may_link(dir, old_dentry, MAY_LINK); 25041da177e4SLinus Torvalds } 25051da177e4SLinus Torvalds 25061da177e4SLinus Torvalds static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry) 25071da177e4SLinus Torvalds { 25081da177e4SLinus Torvalds return may_link(dir, dentry, MAY_UNLINK); 25091da177e4SLinus Torvalds } 25101da177e4SLinus Torvalds 25111da177e4SLinus Torvalds static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name) 25121da177e4SLinus Torvalds { 25131da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_LNK_FILE); 25141da177e4SLinus Torvalds } 25151da177e4SLinus Torvalds 25161da177e4SLinus Torvalds static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, int mask) 25171da177e4SLinus Torvalds { 25181da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_DIR); 25191da177e4SLinus Torvalds } 25201da177e4SLinus Torvalds 25211da177e4SLinus Torvalds static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry) 25221da177e4SLinus Torvalds { 25231da177e4SLinus Torvalds return may_link(dir, dentry, MAY_RMDIR); 25241da177e4SLinus Torvalds } 25251da177e4SLinus Torvalds 25261da177e4SLinus Torvalds static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 25271da177e4SLinus Torvalds { 25281da177e4SLinus Torvalds return may_create(dir, dentry, inode_mode_to_security_class(mode)); 25291da177e4SLinus Torvalds } 25301da177e4SLinus Torvalds 25311da177e4SLinus Torvalds static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry, 25321da177e4SLinus Torvalds struct inode *new_inode, struct dentry *new_dentry) 25331da177e4SLinus Torvalds { 25341da177e4SLinus Torvalds return may_rename(old_inode, old_dentry, new_inode, new_dentry); 25351da177e4SLinus Torvalds } 25361da177e4SLinus Torvalds 25371da177e4SLinus Torvalds static int selinux_inode_readlink(struct dentry *dentry) 25381da177e4SLinus Torvalds { 253988e67f3bSDavid Howells const struct cred *cred = current_cred(); 254088e67f3bSDavid Howells 254188e67f3bSDavid Howells return dentry_has_perm(cred, NULL, dentry, FILE__READ); 25421da177e4SLinus Torvalds } 25431da177e4SLinus Torvalds 25441da177e4SLinus Torvalds static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata) 25451da177e4SLinus Torvalds { 254688e67f3bSDavid Howells const struct cred *cred = current_cred(); 25471da177e4SLinus Torvalds 254888e67f3bSDavid Howells return dentry_has_perm(cred, NULL, dentry, FILE__READ); 25491da177e4SLinus Torvalds } 25501da177e4SLinus Torvalds 2551b77b0646SAl Viro static int selinux_inode_permission(struct inode *inode, int mask) 25521da177e4SLinus Torvalds { 255388e67f3bSDavid Howells const struct cred *cred = current_cred(); 2554b782e0a6SEric Paris struct common_audit_data ad; 2555b782e0a6SEric Paris u32 perms; 2556b782e0a6SEric Paris bool from_access; 25571da177e4SLinus Torvalds 2558b782e0a6SEric Paris from_access = mask & MAY_ACCESS; 2559d09ca739SEric Paris mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND); 2560d09ca739SEric Paris 25611da177e4SLinus Torvalds /* No permission to check. Existence test. */ 2562b782e0a6SEric Paris if (!mask) 25631da177e4SLinus Torvalds return 0; 25641da177e4SLinus Torvalds 2565b782e0a6SEric Paris COMMON_AUDIT_DATA_INIT(&ad, FS); 2566b782e0a6SEric Paris ad.u.fs.inode = inode; 2567b782e0a6SEric Paris 2568b782e0a6SEric Paris if (from_access) 2569b782e0a6SEric Paris ad.selinux_audit_data.auditdeny |= FILE__AUDIT_ACCESS; 2570b782e0a6SEric Paris 2571b782e0a6SEric Paris perms = file_mask_to_av(inode->i_mode, mask); 2572b782e0a6SEric Paris 2573b782e0a6SEric Paris return inode_has_perm(cred, inode, perms, &ad); 25741da177e4SLinus Torvalds } 25751da177e4SLinus Torvalds 25761da177e4SLinus Torvalds static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr) 25771da177e4SLinus Torvalds { 257888e67f3bSDavid Howells const struct cred *cred = current_cred(); 2579bc6a6008SAmerigo Wang unsigned int ia_valid = iattr->ia_valid; 25801da177e4SLinus Torvalds 2581bc6a6008SAmerigo Wang /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */ 2582bc6a6008SAmerigo Wang if (ia_valid & ATTR_FORCE) { 2583bc6a6008SAmerigo Wang ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE | 2584bc6a6008SAmerigo Wang ATTR_FORCE); 2585bc6a6008SAmerigo Wang if (!ia_valid) 25861da177e4SLinus Torvalds return 0; 2587bc6a6008SAmerigo Wang } 25881da177e4SLinus Torvalds 2589bc6a6008SAmerigo Wang if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | 2590bc6a6008SAmerigo Wang ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET)) 259188e67f3bSDavid Howells return dentry_has_perm(cred, NULL, dentry, FILE__SETATTR); 25921da177e4SLinus Torvalds 259388e67f3bSDavid Howells return dentry_has_perm(cred, NULL, dentry, FILE__WRITE); 25941da177e4SLinus Torvalds } 25951da177e4SLinus Torvalds 25961da177e4SLinus Torvalds static int selinux_inode_getattr(struct vfsmount *mnt, struct dentry *dentry) 25971da177e4SLinus Torvalds { 259888e67f3bSDavid Howells const struct cred *cred = current_cred(); 259988e67f3bSDavid Howells 260088e67f3bSDavid Howells return dentry_has_perm(cred, mnt, dentry, FILE__GETATTR); 26011da177e4SLinus Torvalds } 26021da177e4SLinus Torvalds 26038f0cfa52SDavid Howells static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name) 2604b5376771SSerge E. Hallyn { 260588e67f3bSDavid Howells const struct cred *cred = current_cred(); 260688e67f3bSDavid Howells 2607b5376771SSerge E. Hallyn if (!strncmp(name, XATTR_SECURITY_PREFIX, 2608b5376771SSerge E. Hallyn sizeof XATTR_SECURITY_PREFIX - 1)) { 2609b5376771SSerge E. Hallyn if (!strcmp(name, XATTR_NAME_CAPS)) { 2610b5376771SSerge E. Hallyn if (!capable(CAP_SETFCAP)) 2611b5376771SSerge E. Hallyn return -EPERM; 2612b5376771SSerge E. Hallyn } else if (!capable(CAP_SYS_ADMIN)) { 2613b5376771SSerge E. Hallyn /* A different attribute in the security namespace. 2614b5376771SSerge E. Hallyn Restrict to administrator. */ 2615b5376771SSerge E. Hallyn return -EPERM; 2616b5376771SSerge E. Hallyn } 2617b5376771SSerge E. Hallyn } 2618b5376771SSerge E. Hallyn 2619b5376771SSerge E. Hallyn /* Not an attribute we recognize, so just check the 2620b5376771SSerge E. Hallyn ordinary setattr permission. */ 262188e67f3bSDavid Howells return dentry_has_perm(cred, NULL, dentry, FILE__SETATTR); 2622b5376771SSerge E. Hallyn } 2623b5376771SSerge E. Hallyn 26248f0cfa52SDavid Howells static int selinux_inode_setxattr(struct dentry *dentry, const char *name, 26258f0cfa52SDavid Howells const void *value, size_t size, int flags) 26261da177e4SLinus Torvalds { 26271da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 26281da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 26291da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 26302bf49690SThomas Liu struct common_audit_data ad; 2631275bb41eSDavid Howells u32 newsid, sid = current_sid(); 26321da177e4SLinus Torvalds int rc = 0; 26331da177e4SLinus Torvalds 2634b5376771SSerge E. Hallyn if (strcmp(name, XATTR_NAME_SELINUX)) 2635b5376771SSerge E. Hallyn return selinux_inode_setotherxattr(dentry, name); 26361da177e4SLinus Torvalds 26371da177e4SLinus Torvalds sbsec = inode->i_sb->s_security; 2638cd89596fSDavid P. Quigley if (!(sbsec->flags & SE_SBLABELSUPP)) 26391da177e4SLinus Torvalds return -EOPNOTSUPP; 26401da177e4SLinus Torvalds 26413bd858abSSatyam Sharma if (!is_owner_or_cap(inode)) 26421da177e4SLinus Torvalds return -EPERM; 26431da177e4SLinus Torvalds 26442bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, FS); 264544707fdfSJan Blunck ad.u.fs.path.dentry = dentry; 26461da177e4SLinus Torvalds 2647275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, isec->sclass, 26481da177e4SLinus Torvalds FILE__RELABELFROM, &ad); 26491da177e4SLinus Torvalds if (rc) 26501da177e4SLinus Torvalds return rc; 26511da177e4SLinus Torvalds 26521da177e4SLinus Torvalds rc = security_context_to_sid(value, size, &newsid); 265312b29f34SStephen Smalley if (rc == -EINVAL) { 265412b29f34SStephen Smalley if (!capable(CAP_MAC_ADMIN)) 265512b29f34SStephen Smalley return rc; 265612b29f34SStephen Smalley rc = security_context_to_sid_force(value, size, &newsid); 265712b29f34SStephen Smalley } 26581da177e4SLinus Torvalds if (rc) 26591da177e4SLinus Torvalds return rc; 26601da177e4SLinus Torvalds 2661275bb41eSDavid Howells rc = avc_has_perm(sid, newsid, isec->sclass, 26621da177e4SLinus Torvalds FILE__RELABELTO, &ad); 26631da177e4SLinus Torvalds if (rc) 26641da177e4SLinus Torvalds return rc; 26651da177e4SLinus Torvalds 2666275bb41eSDavid Howells rc = security_validate_transition(isec->sid, newsid, sid, 26671da177e4SLinus Torvalds isec->sclass); 26681da177e4SLinus Torvalds if (rc) 26691da177e4SLinus Torvalds return rc; 26701da177e4SLinus Torvalds 26711da177e4SLinus Torvalds return avc_has_perm(newsid, 26721da177e4SLinus Torvalds sbsec->sid, 26731da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 26741da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, 26751da177e4SLinus Torvalds &ad); 26761da177e4SLinus Torvalds } 26771da177e4SLinus Torvalds 26788f0cfa52SDavid Howells static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name, 26798f0cfa52SDavid Howells const void *value, size_t size, 26808f0cfa52SDavid Howells int flags) 26811da177e4SLinus Torvalds { 26821da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 26831da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 26841da177e4SLinus Torvalds u32 newsid; 26851da177e4SLinus Torvalds int rc; 26861da177e4SLinus Torvalds 26871da177e4SLinus Torvalds if (strcmp(name, XATTR_NAME_SELINUX)) { 26881da177e4SLinus Torvalds /* Not an attribute we recognize, so nothing to do. */ 26891da177e4SLinus Torvalds return; 26901da177e4SLinus Torvalds } 26911da177e4SLinus Torvalds 269212b29f34SStephen Smalley rc = security_context_to_sid_force(value, size, &newsid); 26931da177e4SLinus Torvalds if (rc) { 269412b29f34SStephen Smalley printk(KERN_ERR "SELinux: unable to map context to SID" 269512b29f34SStephen Smalley "for (%s, %lu), rc=%d\n", 269612b29f34SStephen Smalley inode->i_sb->s_id, inode->i_ino, -rc); 26971da177e4SLinus Torvalds return; 26981da177e4SLinus Torvalds } 26991da177e4SLinus Torvalds 27001da177e4SLinus Torvalds isec->sid = newsid; 27011da177e4SLinus Torvalds return; 27021da177e4SLinus Torvalds } 27031da177e4SLinus Torvalds 27048f0cfa52SDavid Howells static int selinux_inode_getxattr(struct dentry *dentry, const char *name) 27051da177e4SLinus Torvalds { 270688e67f3bSDavid Howells const struct cred *cred = current_cred(); 270788e67f3bSDavid Howells 270888e67f3bSDavid Howells return dentry_has_perm(cred, NULL, dentry, FILE__GETATTR); 27091da177e4SLinus Torvalds } 27101da177e4SLinus Torvalds 27111da177e4SLinus Torvalds static int selinux_inode_listxattr(struct dentry *dentry) 27121da177e4SLinus Torvalds { 271388e67f3bSDavid Howells const struct cred *cred = current_cred(); 271488e67f3bSDavid Howells 271588e67f3bSDavid Howells return dentry_has_perm(cred, NULL, dentry, FILE__GETATTR); 27161da177e4SLinus Torvalds } 27171da177e4SLinus Torvalds 27188f0cfa52SDavid Howells static int selinux_inode_removexattr(struct dentry *dentry, const char *name) 27191da177e4SLinus Torvalds { 2720b5376771SSerge E. Hallyn if (strcmp(name, XATTR_NAME_SELINUX)) 2721b5376771SSerge E. Hallyn return selinux_inode_setotherxattr(dentry, name); 27221da177e4SLinus Torvalds 27231da177e4SLinus Torvalds /* No one is allowed to remove a SELinux security label. 27241da177e4SLinus Torvalds You can change the label, but all data must be labeled. */ 27251da177e4SLinus Torvalds return -EACCES; 27261da177e4SLinus Torvalds } 27271da177e4SLinus Torvalds 2728d381d8a9SJames Morris /* 2729abc69bb6SStephen Smalley * Copy the inode security context value to the user. 2730d381d8a9SJames Morris * 2731d381d8a9SJames Morris * Permission check is handled by selinux_inode_getxattr hook. 2732d381d8a9SJames Morris */ 273342492594SDavid P. Quigley static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void **buffer, bool alloc) 27341da177e4SLinus Torvalds { 273542492594SDavid P. Quigley u32 size; 273642492594SDavid P. Quigley int error; 273742492594SDavid P. Quigley char *context = NULL; 27381da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 27391da177e4SLinus Torvalds 27408c8570fbSDustin Kirkland if (strcmp(name, XATTR_SELINUX_SUFFIX)) 27418c8570fbSDustin Kirkland return -EOPNOTSUPP; 27421da177e4SLinus Torvalds 2743abc69bb6SStephen Smalley /* 2744abc69bb6SStephen Smalley * If the caller has CAP_MAC_ADMIN, then get the raw context 2745abc69bb6SStephen Smalley * value even if it is not defined by current policy; otherwise, 2746abc69bb6SStephen Smalley * use the in-core value under current policy. 2747abc69bb6SStephen Smalley * Use the non-auditing forms of the permission checks since 2748abc69bb6SStephen Smalley * getxattr may be called by unprivileged processes commonly 2749abc69bb6SStephen Smalley * and lack of permission just means that we fall back to the 2750abc69bb6SStephen Smalley * in-core context value, not a denial. 2751abc69bb6SStephen Smalley */ 27523699c53cSDavid Howells error = selinux_capable(current, current_cred(), CAP_MAC_ADMIN, 27533699c53cSDavid Howells SECURITY_CAP_NOAUDIT); 2754abc69bb6SStephen Smalley if (!error) 2755abc69bb6SStephen Smalley error = security_sid_to_context_force(isec->sid, &context, 2756abc69bb6SStephen Smalley &size); 2757abc69bb6SStephen Smalley else 275842492594SDavid P. Quigley error = security_sid_to_context(isec->sid, &context, &size); 275942492594SDavid P. Quigley if (error) 276042492594SDavid P. Quigley return error; 276142492594SDavid P. Quigley error = size; 276242492594SDavid P. Quigley if (alloc) { 276342492594SDavid P. Quigley *buffer = context; 276442492594SDavid P. Quigley goto out_nofree; 276542492594SDavid P. Quigley } 276642492594SDavid P. Quigley kfree(context); 276742492594SDavid P. Quigley out_nofree: 276842492594SDavid P. Quigley return error; 27691da177e4SLinus Torvalds } 27701da177e4SLinus Torvalds 27711da177e4SLinus Torvalds static int selinux_inode_setsecurity(struct inode *inode, const char *name, 27721da177e4SLinus Torvalds const void *value, size_t size, int flags) 27731da177e4SLinus Torvalds { 27741da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 27751da177e4SLinus Torvalds u32 newsid; 27761da177e4SLinus Torvalds int rc; 27771da177e4SLinus Torvalds 27781da177e4SLinus Torvalds if (strcmp(name, XATTR_SELINUX_SUFFIX)) 27791da177e4SLinus Torvalds return -EOPNOTSUPP; 27801da177e4SLinus Torvalds 27811da177e4SLinus Torvalds if (!value || !size) 27821da177e4SLinus Torvalds return -EACCES; 27831da177e4SLinus Torvalds 27841da177e4SLinus Torvalds rc = security_context_to_sid((void *)value, size, &newsid); 27851da177e4SLinus Torvalds if (rc) 27861da177e4SLinus Torvalds return rc; 27871da177e4SLinus Torvalds 27881da177e4SLinus Torvalds isec->sid = newsid; 2789ddd29ec6SDavid P. Quigley isec->initialized = 1; 27901da177e4SLinus Torvalds return 0; 27911da177e4SLinus Torvalds } 27921da177e4SLinus Torvalds 27931da177e4SLinus Torvalds static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size) 27941da177e4SLinus Torvalds { 27951da177e4SLinus Torvalds const int len = sizeof(XATTR_NAME_SELINUX); 27961da177e4SLinus Torvalds if (buffer && len <= buffer_size) 27971da177e4SLinus Torvalds memcpy(buffer, XATTR_NAME_SELINUX, len); 27981da177e4SLinus Torvalds return len; 27991da177e4SLinus Torvalds } 28001da177e4SLinus Torvalds 2801713a04aeSAhmed S. Darwish static void selinux_inode_getsecid(const struct inode *inode, u32 *secid) 2802713a04aeSAhmed S. Darwish { 2803713a04aeSAhmed S. Darwish struct inode_security_struct *isec = inode->i_security; 2804713a04aeSAhmed S. Darwish *secid = isec->sid; 2805713a04aeSAhmed S. Darwish } 2806713a04aeSAhmed S. Darwish 28071da177e4SLinus Torvalds /* file security operations */ 28081da177e4SLinus Torvalds 2809788e7dd4SYuichi Nakamura static int selinux_revalidate_file_permission(struct file *file, int mask) 28101da177e4SLinus Torvalds { 281188e67f3bSDavid Howells const struct cred *cred = current_cred(); 28123d5ff529SJosef Sipek struct inode *inode = file->f_path.dentry->d_inode; 28131da177e4SLinus Torvalds 28141da177e4SLinus Torvalds /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */ 28151da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE)) 28161da177e4SLinus Torvalds mask |= MAY_APPEND; 28171da177e4SLinus Torvalds 2818389fb800SPaul Moore return file_has_perm(cred, file, 28191da177e4SLinus Torvalds file_mask_to_av(inode->i_mode, mask)); 28201da177e4SLinus Torvalds } 28211da177e4SLinus Torvalds 2822788e7dd4SYuichi Nakamura static int selinux_file_permission(struct file *file, int mask) 2823788e7dd4SYuichi Nakamura { 282420dda18bSStephen Smalley struct inode *inode = file->f_path.dentry->d_inode; 282520dda18bSStephen Smalley struct file_security_struct *fsec = file->f_security; 282620dda18bSStephen Smalley struct inode_security_struct *isec = inode->i_security; 282720dda18bSStephen Smalley u32 sid = current_sid(); 282820dda18bSStephen Smalley 2829389fb800SPaul Moore if (!mask) 2830788e7dd4SYuichi Nakamura /* No permission to check. Existence test. */ 2831788e7dd4SYuichi Nakamura return 0; 2832788e7dd4SYuichi Nakamura 283320dda18bSStephen Smalley if (sid == fsec->sid && fsec->isid == isec->sid && 283420dda18bSStephen Smalley fsec->pseqno == avc_policy_seqno()) 283520dda18bSStephen Smalley /* No change since dentry_open check. */ 283620dda18bSStephen Smalley return 0; 283720dda18bSStephen Smalley 2838788e7dd4SYuichi Nakamura return selinux_revalidate_file_permission(file, mask); 2839788e7dd4SYuichi Nakamura } 2840788e7dd4SYuichi Nakamura 28411da177e4SLinus Torvalds static int selinux_file_alloc_security(struct file *file) 28421da177e4SLinus Torvalds { 28431da177e4SLinus Torvalds return file_alloc_security(file); 28441da177e4SLinus Torvalds } 28451da177e4SLinus Torvalds 28461da177e4SLinus Torvalds static void selinux_file_free_security(struct file *file) 28471da177e4SLinus Torvalds { 28481da177e4SLinus Torvalds file_free_security(file); 28491da177e4SLinus Torvalds } 28501da177e4SLinus Torvalds 28511da177e4SLinus Torvalds static int selinux_file_ioctl(struct file *file, unsigned int cmd, 28521da177e4SLinus Torvalds unsigned long arg) 28531da177e4SLinus Torvalds { 285488e67f3bSDavid Howells const struct cred *cred = current_cred(); 28550b24dcb7SEric Paris int error = 0; 28561da177e4SLinus Torvalds 28570b24dcb7SEric Paris switch (cmd) { 28580b24dcb7SEric Paris case FIONREAD: 28590b24dcb7SEric Paris /* fall through */ 28600b24dcb7SEric Paris case FIBMAP: 28610b24dcb7SEric Paris /* fall through */ 28620b24dcb7SEric Paris case FIGETBSZ: 28630b24dcb7SEric Paris /* fall through */ 28640b24dcb7SEric Paris case EXT2_IOC_GETFLAGS: 28650b24dcb7SEric Paris /* fall through */ 28660b24dcb7SEric Paris case EXT2_IOC_GETVERSION: 28670b24dcb7SEric Paris error = file_has_perm(cred, file, FILE__GETATTR); 28680b24dcb7SEric Paris break; 28691da177e4SLinus Torvalds 28700b24dcb7SEric Paris case EXT2_IOC_SETFLAGS: 28710b24dcb7SEric Paris /* fall through */ 28720b24dcb7SEric Paris case EXT2_IOC_SETVERSION: 28730b24dcb7SEric Paris error = file_has_perm(cred, file, FILE__SETATTR); 28740b24dcb7SEric Paris break; 28750b24dcb7SEric Paris 28760b24dcb7SEric Paris /* sys_ioctl() checks */ 28770b24dcb7SEric Paris case FIONBIO: 28780b24dcb7SEric Paris /* fall through */ 28790b24dcb7SEric Paris case FIOASYNC: 28800b24dcb7SEric Paris error = file_has_perm(cred, file, 0); 28810b24dcb7SEric Paris break; 28820b24dcb7SEric Paris 28830b24dcb7SEric Paris case KDSKBENT: 28840b24dcb7SEric Paris case KDSKBSENT: 28850b24dcb7SEric Paris error = task_has_capability(current, cred, CAP_SYS_TTY_CONFIG, 28860b24dcb7SEric Paris SECURITY_CAP_AUDIT); 28870b24dcb7SEric Paris break; 28880b24dcb7SEric Paris 28890b24dcb7SEric Paris /* default case assumes that the command will go 28900b24dcb7SEric Paris * to the file's ioctl() function. 28910b24dcb7SEric Paris */ 28920b24dcb7SEric Paris default: 28930b24dcb7SEric Paris error = file_has_perm(cred, file, FILE__IOCTL); 28940b24dcb7SEric Paris } 28950b24dcb7SEric Paris return error; 28961da177e4SLinus Torvalds } 28971da177e4SLinus Torvalds 2898fcaaade1SStephen Smalley static int default_noexec; 2899fcaaade1SStephen Smalley 29001da177e4SLinus Torvalds static int file_map_prot_check(struct file *file, unsigned long prot, int shared) 29011da177e4SLinus Torvalds { 290288e67f3bSDavid Howells const struct cred *cred = current_cred(); 2903d84f4f99SDavid Howells int rc = 0; 290488e67f3bSDavid Howells 2905fcaaade1SStephen Smalley if (default_noexec && 2906fcaaade1SStephen Smalley (prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) { 29071da177e4SLinus Torvalds /* 29081da177e4SLinus Torvalds * We are making executable an anonymous mapping or a 29091da177e4SLinus Torvalds * private file mapping that will also be writable. 29101da177e4SLinus Torvalds * This has an additional check. 29111da177e4SLinus Torvalds */ 2912d84f4f99SDavid Howells rc = cred_has_perm(cred, cred, PROCESS__EXECMEM); 29131da177e4SLinus Torvalds if (rc) 2914d84f4f99SDavid Howells goto error; 29151da177e4SLinus Torvalds } 29161da177e4SLinus Torvalds 29171da177e4SLinus Torvalds if (file) { 29181da177e4SLinus Torvalds /* read access is always possible with a mapping */ 29191da177e4SLinus Torvalds u32 av = FILE__READ; 29201da177e4SLinus Torvalds 29211da177e4SLinus Torvalds /* write access only matters if the mapping is shared */ 29221da177e4SLinus Torvalds if (shared && (prot & PROT_WRITE)) 29231da177e4SLinus Torvalds av |= FILE__WRITE; 29241da177e4SLinus Torvalds 29251da177e4SLinus Torvalds if (prot & PROT_EXEC) 29261da177e4SLinus Torvalds av |= FILE__EXECUTE; 29271da177e4SLinus Torvalds 292888e67f3bSDavid Howells return file_has_perm(cred, file, av); 29291da177e4SLinus Torvalds } 2930d84f4f99SDavid Howells 2931d84f4f99SDavid Howells error: 2932d84f4f99SDavid Howells return rc; 29331da177e4SLinus Torvalds } 29341da177e4SLinus Torvalds 29351da177e4SLinus Torvalds static int selinux_file_mmap(struct file *file, unsigned long reqprot, 2936ed032189SEric Paris unsigned long prot, unsigned long flags, 2937ed032189SEric Paris unsigned long addr, unsigned long addr_only) 29381da177e4SLinus Torvalds { 2939ed032189SEric Paris int rc = 0; 2940275bb41eSDavid Howells u32 sid = current_sid(); 29411da177e4SLinus Torvalds 294284336d1aSEric Paris /* 294384336d1aSEric Paris * notice that we are intentionally putting the SELinux check before 294484336d1aSEric Paris * the secondary cap_file_mmap check. This is such a likely attempt 294584336d1aSEric Paris * at bad behaviour/exploit that we always want to get the AVC, even 294684336d1aSEric Paris * if DAC would have also denied the operation. 294784336d1aSEric Paris */ 2948a2551df7SEric Paris if (addr < CONFIG_LSM_MMAP_MIN_ADDR) { 2949ed032189SEric Paris rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT, 2950ed032189SEric Paris MEMPROTECT__MMAP_ZERO, NULL); 295184336d1aSEric Paris if (rc) 295284336d1aSEric Paris return rc; 295384336d1aSEric Paris } 295484336d1aSEric Paris 295584336d1aSEric Paris /* do DAC check on address space usage */ 295684336d1aSEric Paris rc = cap_file_mmap(file, reqprot, prot, flags, addr, addr_only); 2957ed032189SEric Paris if (rc || addr_only) 29581da177e4SLinus Torvalds return rc; 29591da177e4SLinus Torvalds 29601da177e4SLinus Torvalds if (selinux_checkreqprot) 29611da177e4SLinus Torvalds prot = reqprot; 29621da177e4SLinus Torvalds 29631da177e4SLinus Torvalds return file_map_prot_check(file, prot, 29641da177e4SLinus Torvalds (flags & MAP_TYPE) == MAP_SHARED); 29651da177e4SLinus Torvalds } 29661da177e4SLinus Torvalds 29671da177e4SLinus Torvalds static int selinux_file_mprotect(struct vm_area_struct *vma, 29681da177e4SLinus Torvalds unsigned long reqprot, 29691da177e4SLinus Torvalds unsigned long prot) 29701da177e4SLinus Torvalds { 297188e67f3bSDavid Howells const struct cred *cred = current_cred(); 29721da177e4SLinus Torvalds 29731da177e4SLinus Torvalds if (selinux_checkreqprot) 29741da177e4SLinus Torvalds prot = reqprot; 29751da177e4SLinus Torvalds 2976fcaaade1SStephen Smalley if (default_noexec && 2977fcaaade1SStephen Smalley (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) { 2978d541bbeeSJames Morris int rc = 0; 2979db4c9641SStephen Smalley if (vma->vm_start >= vma->vm_mm->start_brk && 2980db4c9641SStephen Smalley vma->vm_end <= vma->vm_mm->brk) { 2981d84f4f99SDavid Howells rc = cred_has_perm(cred, cred, PROCESS__EXECHEAP); 2982db4c9641SStephen Smalley } else if (!vma->vm_file && 29836b992197SLorenzo Hernandez García-Hierro vma->vm_start <= vma->vm_mm->start_stack && 29846b992197SLorenzo Hernandez García-Hierro vma->vm_end >= vma->vm_mm->start_stack) { 29853b11a1deSDavid Howells rc = current_has_perm(current, PROCESS__EXECSTACK); 2986db4c9641SStephen Smalley } else if (vma->vm_file && vma->anon_vma) { 2987db4c9641SStephen Smalley /* 2988db4c9641SStephen Smalley * We are making executable a file mapping that has 2989db4c9641SStephen Smalley * had some COW done. Since pages might have been 2990db4c9641SStephen Smalley * written, check ability to execute the possibly 2991db4c9641SStephen Smalley * modified content. This typically should only 2992db4c9641SStephen Smalley * occur for text relocations. 2993db4c9641SStephen Smalley */ 2994d84f4f99SDavid Howells rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD); 2995db4c9641SStephen Smalley } 29966b992197SLorenzo Hernandez García-Hierro if (rc) 29976b992197SLorenzo Hernandez García-Hierro return rc; 29986b992197SLorenzo Hernandez García-Hierro } 29991da177e4SLinus Torvalds 30001da177e4SLinus Torvalds return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED); 30011da177e4SLinus Torvalds } 30021da177e4SLinus Torvalds 30031da177e4SLinus Torvalds static int selinux_file_lock(struct file *file, unsigned int cmd) 30041da177e4SLinus Torvalds { 300588e67f3bSDavid Howells const struct cred *cred = current_cred(); 300688e67f3bSDavid Howells 300788e67f3bSDavid Howells return file_has_perm(cred, file, FILE__LOCK); 30081da177e4SLinus Torvalds } 30091da177e4SLinus Torvalds 30101da177e4SLinus Torvalds static int selinux_file_fcntl(struct file *file, unsigned int cmd, 30111da177e4SLinus Torvalds unsigned long arg) 30121da177e4SLinus Torvalds { 301388e67f3bSDavid Howells const struct cred *cred = current_cred(); 30141da177e4SLinus Torvalds int err = 0; 30151da177e4SLinus Torvalds 30161da177e4SLinus Torvalds switch (cmd) { 30171da177e4SLinus Torvalds case F_SETFL: 30183d5ff529SJosef Sipek if (!file->f_path.dentry || !file->f_path.dentry->d_inode) { 30191da177e4SLinus Torvalds err = -EINVAL; 30201da177e4SLinus Torvalds break; 30211da177e4SLinus Torvalds } 30221da177e4SLinus Torvalds 30231da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) { 302488e67f3bSDavid Howells err = file_has_perm(cred, file, FILE__WRITE); 30251da177e4SLinus Torvalds break; 30261da177e4SLinus Torvalds } 30271da177e4SLinus Torvalds /* fall through */ 30281da177e4SLinus Torvalds case F_SETOWN: 30291da177e4SLinus Torvalds case F_SETSIG: 30301da177e4SLinus Torvalds case F_GETFL: 30311da177e4SLinus Torvalds case F_GETOWN: 30321da177e4SLinus Torvalds case F_GETSIG: 30331da177e4SLinus Torvalds /* Just check FD__USE permission */ 303488e67f3bSDavid Howells err = file_has_perm(cred, file, 0); 30351da177e4SLinus Torvalds break; 30361da177e4SLinus Torvalds case F_GETLK: 30371da177e4SLinus Torvalds case F_SETLK: 30381da177e4SLinus Torvalds case F_SETLKW: 30391da177e4SLinus Torvalds #if BITS_PER_LONG == 32 30401da177e4SLinus Torvalds case F_GETLK64: 30411da177e4SLinus Torvalds case F_SETLK64: 30421da177e4SLinus Torvalds case F_SETLKW64: 30431da177e4SLinus Torvalds #endif 30443d5ff529SJosef Sipek if (!file->f_path.dentry || !file->f_path.dentry->d_inode) { 30451da177e4SLinus Torvalds err = -EINVAL; 30461da177e4SLinus Torvalds break; 30471da177e4SLinus Torvalds } 304888e67f3bSDavid Howells err = file_has_perm(cred, file, FILE__LOCK); 30491da177e4SLinus Torvalds break; 30501da177e4SLinus Torvalds } 30511da177e4SLinus Torvalds 30521da177e4SLinus Torvalds return err; 30531da177e4SLinus Torvalds } 30541da177e4SLinus Torvalds 30551da177e4SLinus Torvalds static int selinux_file_set_fowner(struct file *file) 30561da177e4SLinus Torvalds { 30571da177e4SLinus Torvalds struct file_security_struct *fsec; 30581da177e4SLinus Torvalds 30591da177e4SLinus Torvalds fsec = file->f_security; 3060275bb41eSDavid Howells fsec->fown_sid = current_sid(); 30611da177e4SLinus Torvalds 30621da177e4SLinus Torvalds return 0; 30631da177e4SLinus Torvalds } 30641da177e4SLinus Torvalds 30651da177e4SLinus Torvalds static int selinux_file_send_sigiotask(struct task_struct *tsk, 30661da177e4SLinus Torvalds struct fown_struct *fown, int signum) 30671da177e4SLinus Torvalds { 30681da177e4SLinus Torvalds struct file *file; 306965c90bcaSStephen Smalley u32 sid = task_sid(tsk); 30701da177e4SLinus Torvalds u32 perm; 30711da177e4SLinus Torvalds struct file_security_struct *fsec; 30721da177e4SLinus Torvalds 30731da177e4SLinus Torvalds /* struct fown_struct is never outside the context of a struct file */ 3074b385a144SRobert P. J. Day file = container_of(fown, struct file, f_owner); 30751da177e4SLinus Torvalds 30761da177e4SLinus Torvalds fsec = file->f_security; 30771da177e4SLinus Torvalds 30781da177e4SLinus Torvalds if (!signum) 30791da177e4SLinus Torvalds perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */ 30801da177e4SLinus Torvalds else 30811da177e4SLinus Torvalds perm = signal_to_av(signum); 30821da177e4SLinus Torvalds 3083275bb41eSDavid Howells return avc_has_perm(fsec->fown_sid, sid, 30841da177e4SLinus Torvalds SECCLASS_PROCESS, perm, NULL); 30851da177e4SLinus Torvalds } 30861da177e4SLinus Torvalds 30871da177e4SLinus Torvalds static int selinux_file_receive(struct file *file) 30881da177e4SLinus Torvalds { 308988e67f3bSDavid Howells const struct cred *cred = current_cred(); 309088e67f3bSDavid Howells 309188e67f3bSDavid Howells return file_has_perm(cred, file, file_to_av(file)); 30921da177e4SLinus Torvalds } 30931da177e4SLinus Torvalds 3094745ca247SDavid Howells static int selinux_dentry_open(struct file *file, const struct cred *cred) 3095788e7dd4SYuichi Nakamura { 3096788e7dd4SYuichi Nakamura struct file_security_struct *fsec; 3097788e7dd4SYuichi Nakamura struct inode *inode; 3098788e7dd4SYuichi Nakamura struct inode_security_struct *isec; 3099d84f4f99SDavid Howells 3100788e7dd4SYuichi Nakamura inode = file->f_path.dentry->d_inode; 3101788e7dd4SYuichi Nakamura fsec = file->f_security; 3102788e7dd4SYuichi Nakamura isec = inode->i_security; 3103788e7dd4SYuichi Nakamura /* 3104788e7dd4SYuichi Nakamura * Save inode label and policy sequence number 3105788e7dd4SYuichi Nakamura * at open-time so that selinux_file_permission 3106788e7dd4SYuichi Nakamura * can determine whether revalidation is necessary. 3107788e7dd4SYuichi Nakamura * Task label is already saved in the file security 3108788e7dd4SYuichi Nakamura * struct as its SID. 3109788e7dd4SYuichi Nakamura */ 3110788e7dd4SYuichi Nakamura fsec->isid = isec->sid; 3111788e7dd4SYuichi Nakamura fsec->pseqno = avc_policy_seqno(); 3112788e7dd4SYuichi Nakamura /* 3113788e7dd4SYuichi Nakamura * Since the inode label or policy seqno may have changed 3114788e7dd4SYuichi Nakamura * between the selinux_inode_permission check and the saving 3115788e7dd4SYuichi Nakamura * of state above, recheck that access is still permitted. 3116788e7dd4SYuichi Nakamura * Otherwise, access might never be revalidated against the 3117788e7dd4SYuichi Nakamura * new inode label or new policy. 3118788e7dd4SYuichi Nakamura * This check is not redundant - do not remove. 3119788e7dd4SYuichi Nakamura */ 312088e67f3bSDavid Howells return inode_has_perm(cred, inode, open_file_to_av(file), NULL); 3121788e7dd4SYuichi Nakamura } 3122788e7dd4SYuichi Nakamura 31231da177e4SLinus Torvalds /* task security operations */ 31241da177e4SLinus Torvalds 31251da177e4SLinus Torvalds static int selinux_task_create(unsigned long clone_flags) 31261da177e4SLinus Torvalds { 31273b11a1deSDavid Howells return current_has_perm(current, PROCESS__FORK); 31281da177e4SLinus Torvalds } 31291da177e4SLinus Torvalds 3130f1752eecSDavid Howells /* 3131ee18d64cSDavid Howells * allocate the SELinux part of blank credentials 3132ee18d64cSDavid Howells */ 3133ee18d64cSDavid Howells static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp) 3134ee18d64cSDavid Howells { 3135ee18d64cSDavid Howells struct task_security_struct *tsec; 3136ee18d64cSDavid Howells 3137ee18d64cSDavid Howells tsec = kzalloc(sizeof(struct task_security_struct), gfp); 3138ee18d64cSDavid Howells if (!tsec) 3139ee18d64cSDavid Howells return -ENOMEM; 3140ee18d64cSDavid Howells 3141ee18d64cSDavid Howells cred->security = tsec; 3142ee18d64cSDavid Howells return 0; 3143ee18d64cSDavid Howells } 3144ee18d64cSDavid Howells 3145ee18d64cSDavid Howells /* 3146f1752eecSDavid Howells * detach and free the LSM part of a set of credentials 3147f1752eecSDavid Howells */ 3148f1752eecSDavid Howells static void selinux_cred_free(struct cred *cred) 31491da177e4SLinus Torvalds { 3150f1752eecSDavid Howells struct task_security_struct *tsec = cred->security; 3151e0e81739SDavid Howells 3152e0e81739SDavid Howells BUG_ON((unsigned long) cred->security < PAGE_SIZE); 3153e0e81739SDavid Howells cred->security = (void *) 0x7UL; 3154f1752eecSDavid Howells kfree(tsec); 31551da177e4SLinus Torvalds } 31561da177e4SLinus Torvalds 3157d84f4f99SDavid Howells /* 3158d84f4f99SDavid Howells * prepare a new set of credentials for modification 3159d84f4f99SDavid Howells */ 3160d84f4f99SDavid Howells static int selinux_cred_prepare(struct cred *new, const struct cred *old, 3161d84f4f99SDavid Howells gfp_t gfp) 3162d84f4f99SDavid Howells { 3163d84f4f99SDavid Howells const struct task_security_struct *old_tsec; 3164d84f4f99SDavid Howells struct task_security_struct *tsec; 3165d84f4f99SDavid Howells 3166d84f4f99SDavid Howells old_tsec = old->security; 3167d84f4f99SDavid Howells 3168d84f4f99SDavid Howells tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp); 3169d84f4f99SDavid Howells if (!tsec) 3170d84f4f99SDavid Howells return -ENOMEM; 3171d84f4f99SDavid Howells 3172d84f4f99SDavid Howells new->security = tsec; 3173d84f4f99SDavid Howells return 0; 3174d84f4f99SDavid Howells } 3175d84f4f99SDavid Howells 3176d84f4f99SDavid Howells /* 3177ee18d64cSDavid Howells * transfer the SELinux data to a blank set of creds 3178ee18d64cSDavid Howells */ 3179ee18d64cSDavid Howells static void selinux_cred_transfer(struct cred *new, const struct cred *old) 3180ee18d64cSDavid Howells { 3181ee18d64cSDavid Howells const struct task_security_struct *old_tsec = old->security; 3182ee18d64cSDavid Howells struct task_security_struct *tsec = new->security; 3183ee18d64cSDavid Howells 3184ee18d64cSDavid Howells *tsec = *old_tsec; 3185ee18d64cSDavid Howells } 3186ee18d64cSDavid Howells 3187ee18d64cSDavid Howells /* 31883a3b7ce9SDavid Howells * set the security data for a kernel service 31893a3b7ce9SDavid Howells * - all the creation contexts are set to unlabelled 31903a3b7ce9SDavid Howells */ 31913a3b7ce9SDavid Howells static int selinux_kernel_act_as(struct cred *new, u32 secid) 31923a3b7ce9SDavid Howells { 31933a3b7ce9SDavid Howells struct task_security_struct *tsec = new->security; 31943a3b7ce9SDavid Howells u32 sid = current_sid(); 31953a3b7ce9SDavid Howells int ret; 31963a3b7ce9SDavid Howells 31973a3b7ce9SDavid Howells ret = avc_has_perm(sid, secid, 31983a3b7ce9SDavid Howells SECCLASS_KERNEL_SERVICE, 31993a3b7ce9SDavid Howells KERNEL_SERVICE__USE_AS_OVERRIDE, 32003a3b7ce9SDavid Howells NULL); 32013a3b7ce9SDavid Howells if (ret == 0) { 32023a3b7ce9SDavid Howells tsec->sid = secid; 32033a3b7ce9SDavid Howells tsec->create_sid = 0; 32043a3b7ce9SDavid Howells tsec->keycreate_sid = 0; 32053a3b7ce9SDavid Howells tsec->sockcreate_sid = 0; 32063a3b7ce9SDavid Howells } 32073a3b7ce9SDavid Howells return ret; 32083a3b7ce9SDavid Howells } 32093a3b7ce9SDavid Howells 32103a3b7ce9SDavid Howells /* 32113a3b7ce9SDavid Howells * set the file creation context in a security record to the same as the 32123a3b7ce9SDavid Howells * objective context of the specified inode 32133a3b7ce9SDavid Howells */ 32143a3b7ce9SDavid Howells static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode) 32153a3b7ce9SDavid Howells { 32163a3b7ce9SDavid Howells struct inode_security_struct *isec = inode->i_security; 32173a3b7ce9SDavid Howells struct task_security_struct *tsec = new->security; 32183a3b7ce9SDavid Howells u32 sid = current_sid(); 32193a3b7ce9SDavid Howells int ret; 32203a3b7ce9SDavid Howells 32213a3b7ce9SDavid Howells ret = avc_has_perm(sid, isec->sid, 32223a3b7ce9SDavid Howells SECCLASS_KERNEL_SERVICE, 32233a3b7ce9SDavid Howells KERNEL_SERVICE__CREATE_FILES_AS, 32243a3b7ce9SDavid Howells NULL); 32253a3b7ce9SDavid Howells 32263a3b7ce9SDavid Howells if (ret == 0) 32273a3b7ce9SDavid Howells tsec->create_sid = isec->sid; 3228ef57471aSDavid Howells return ret; 32293a3b7ce9SDavid Howells } 32303a3b7ce9SDavid Howells 3231dd8dbf2eSEric Paris static int selinux_kernel_module_request(char *kmod_name) 323225354c4fSEric Paris { 3233dd8dbf2eSEric Paris u32 sid; 3234dd8dbf2eSEric Paris struct common_audit_data ad; 3235dd8dbf2eSEric Paris 3236dd8dbf2eSEric Paris sid = task_sid(current); 3237dd8dbf2eSEric Paris 3238dd8dbf2eSEric Paris COMMON_AUDIT_DATA_INIT(&ad, KMOD); 3239dd8dbf2eSEric Paris ad.u.kmod_name = kmod_name; 3240dd8dbf2eSEric Paris 3241dd8dbf2eSEric Paris return avc_has_perm(sid, SECINITSID_KERNEL, SECCLASS_SYSTEM, 3242dd8dbf2eSEric Paris SYSTEM__MODULE_REQUEST, &ad); 324325354c4fSEric Paris } 324425354c4fSEric Paris 32451da177e4SLinus Torvalds static int selinux_task_setpgid(struct task_struct *p, pid_t pgid) 32461da177e4SLinus Torvalds { 32473b11a1deSDavid Howells return current_has_perm(p, PROCESS__SETPGID); 32481da177e4SLinus Torvalds } 32491da177e4SLinus Torvalds 32501da177e4SLinus Torvalds static int selinux_task_getpgid(struct task_struct *p) 32511da177e4SLinus Torvalds { 32523b11a1deSDavid Howells return current_has_perm(p, PROCESS__GETPGID); 32531da177e4SLinus Torvalds } 32541da177e4SLinus Torvalds 32551da177e4SLinus Torvalds static int selinux_task_getsid(struct task_struct *p) 32561da177e4SLinus Torvalds { 32573b11a1deSDavid Howells return current_has_perm(p, PROCESS__GETSESSION); 32581da177e4SLinus Torvalds } 32591da177e4SLinus Torvalds 3260f9008e4cSDavid Quigley static void selinux_task_getsecid(struct task_struct *p, u32 *secid) 3261f9008e4cSDavid Quigley { 3262275bb41eSDavid Howells *secid = task_sid(p); 3263f9008e4cSDavid Quigley } 3264f9008e4cSDavid Quigley 32651da177e4SLinus Torvalds static int selinux_task_setnice(struct task_struct *p, int nice) 32661da177e4SLinus Torvalds { 32671da177e4SLinus Torvalds int rc; 32681da177e4SLinus Torvalds 3269200ac532SEric Paris rc = cap_task_setnice(p, nice); 32701da177e4SLinus Torvalds if (rc) 32711da177e4SLinus Torvalds return rc; 32721da177e4SLinus Torvalds 32733b11a1deSDavid Howells return current_has_perm(p, PROCESS__SETSCHED); 32741da177e4SLinus Torvalds } 32751da177e4SLinus Torvalds 327603e68060SJames Morris static int selinux_task_setioprio(struct task_struct *p, int ioprio) 327703e68060SJames Morris { 3278b5376771SSerge E. Hallyn int rc; 3279b5376771SSerge E. Hallyn 3280200ac532SEric Paris rc = cap_task_setioprio(p, ioprio); 3281b5376771SSerge E. Hallyn if (rc) 3282b5376771SSerge E. Hallyn return rc; 3283b5376771SSerge E. Hallyn 32843b11a1deSDavid Howells return current_has_perm(p, PROCESS__SETSCHED); 328503e68060SJames Morris } 328603e68060SJames Morris 3287a1836a42SDavid Quigley static int selinux_task_getioprio(struct task_struct *p) 3288a1836a42SDavid Quigley { 32893b11a1deSDavid Howells return current_has_perm(p, PROCESS__GETSCHED); 3290a1836a42SDavid Quigley } 3291a1836a42SDavid Quigley 32928fd00b4dSJiri Slaby static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource, 32938fd00b4dSJiri Slaby struct rlimit *new_rlim) 32941da177e4SLinus Torvalds { 32958fd00b4dSJiri Slaby struct rlimit *old_rlim = p->signal->rlim + resource; 32961da177e4SLinus Torvalds 32971da177e4SLinus Torvalds /* Control the ability to change the hard limit (whether 32981da177e4SLinus Torvalds lowering or raising it), so that the hard limit can 32991da177e4SLinus Torvalds later be used as a safe reset point for the soft limit 3300d84f4f99SDavid Howells upon context transitions. See selinux_bprm_committing_creds. */ 33011da177e4SLinus Torvalds if (old_rlim->rlim_max != new_rlim->rlim_max) 33028fd00b4dSJiri Slaby return current_has_perm(p, PROCESS__SETRLIMIT); 33031da177e4SLinus Torvalds 33041da177e4SLinus Torvalds return 0; 33051da177e4SLinus Torvalds } 33061da177e4SLinus Torvalds 3307b0ae1981SKOSAKI Motohiro static int selinux_task_setscheduler(struct task_struct *p) 33081da177e4SLinus Torvalds { 3309b5376771SSerge E. Hallyn int rc; 3310b5376771SSerge E. Hallyn 3311b0ae1981SKOSAKI Motohiro rc = cap_task_setscheduler(p); 3312b5376771SSerge E. Hallyn if (rc) 3313b5376771SSerge E. Hallyn return rc; 3314b5376771SSerge E. Hallyn 33153b11a1deSDavid Howells return current_has_perm(p, PROCESS__SETSCHED); 33161da177e4SLinus Torvalds } 33171da177e4SLinus Torvalds 33181da177e4SLinus Torvalds static int selinux_task_getscheduler(struct task_struct *p) 33191da177e4SLinus Torvalds { 33203b11a1deSDavid Howells return current_has_perm(p, PROCESS__GETSCHED); 33211da177e4SLinus Torvalds } 33221da177e4SLinus Torvalds 332335601547SDavid Quigley static int selinux_task_movememory(struct task_struct *p) 332435601547SDavid Quigley { 33253b11a1deSDavid Howells return current_has_perm(p, PROCESS__SETSCHED); 332635601547SDavid Quigley } 332735601547SDavid Quigley 3328f9008e4cSDavid Quigley static int selinux_task_kill(struct task_struct *p, struct siginfo *info, 3329f9008e4cSDavid Quigley int sig, u32 secid) 33301da177e4SLinus Torvalds { 33311da177e4SLinus Torvalds u32 perm; 33321da177e4SLinus Torvalds int rc; 33331da177e4SLinus Torvalds 33341da177e4SLinus Torvalds if (!sig) 33351da177e4SLinus Torvalds perm = PROCESS__SIGNULL; /* null signal; existence test */ 33361da177e4SLinus Torvalds else 33371da177e4SLinus Torvalds perm = signal_to_av(sig); 3338f9008e4cSDavid Quigley if (secid) 3339275bb41eSDavid Howells rc = avc_has_perm(secid, task_sid(p), 3340275bb41eSDavid Howells SECCLASS_PROCESS, perm, NULL); 3341f9008e4cSDavid Quigley else 33423b11a1deSDavid Howells rc = current_has_perm(p, perm); 3343f9008e4cSDavid Quigley return rc; 33441da177e4SLinus Torvalds } 33451da177e4SLinus Torvalds 33461da177e4SLinus Torvalds static int selinux_task_wait(struct task_struct *p) 33471da177e4SLinus Torvalds { 33488a535140SEric Paris return task_has_perm(p, current, PROCESS__SIGCHLD); 33491da177e4SLinus Torvalds } 33501da177e4SLinus Torvalds 33511da177e4SLinus Torvalds static void selinux_task_to_inode(struct task_struct *p, 33521da177e4SLinus Torvalds struct inode *inode) 33531da177e4SLinus Torvalds { 33541da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 3355275bb41eSDavid Howells u32 sid = task_sid(p); 33561da177e4SLinus Torvalds 3357275bb41eSDavid Howells isec->sid = sid; 33581da177e4SLinus Torvalds isec->initialized = 1; 33591da177e4SLinus Torvalds } 33601da177e4SLinus Torvalds 33611da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 336267f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv4(struct sk_buff *skb, 33632bf49690SThomas Liu struct common_audit_data *ad, u8 *proto) 33641da177e4SLinus Torvalds { 33651da177e4SLinus Torvalds int offset, ihlen, ret = -EINVAL; 33661da177e4SLinus Torvalds struct iphdr _iph, *ih; 33671da177e4SLinus Torvalds 3368bbe735e4SArnaldo Carvalho de Melo offset = skb_network_offset(skb); 33691da177e4SLinus Torvalds ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph); 33701da177e4SLinus Torvalds if (ih == NULL) 33711da177e4SLinus Torvalds goto out; 33721da177e4SLinus Torvalds 33731da177e4SLinus Torvalds ihlen = ih->ihl * 4; 33741da177e4SLinus Torvalds if (ihlen < sizeof(_iph)) 33751da177e4SLinus Torvalds goto out; 33761da177e4SLinus Torvalds 33771da177e4SLinus Torvalds ad->u.net.v4info.saddr = ih->saddr; 33781da177e4SLinus Torvalds ad->u.net.v4info.daddr = ih->daddr; 33791da177e4SLinus Torvalds ret = 0; 33801da177e4SLinus Torvalds 338167f83cbfSVenkat Yekkirala if (proto) 338267f83cbfSVenkat Yekkirala *proto = ih->protocol; 338367f83cbfSVenkat Yekkirala 33841da177e4SLinus Torvalds switch (ih->protocol) { 33851da177e4SLinus Torvalds case IPPROTO_TCP: { 33861da177e4SLinus Torvalds struct tcphdr _tcph, *th; 33871da177e4SLinus Torvalds 33881da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 33891da177e4SLinus Torvalds break; 33901da177e4SLinus Torvalds 33911da177e4SLinus Torvalds offset += ihlen; 33921da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 33931da177e4SLinus Torvalds if (th == NULL) 33941da177e4SLinus Torvalds break; 33951da177e4SLinus Torvalds 33961da177e4SLinus Torvalds ad->u.net.sport = th->source; 33971da177e4SLinus Torvalds ad->u.net.dport = th->dest; 33981da177e4SLinus Torvalds break; 33991da177e4SLinus Torvalds } 34001da177e4SLinus Torvalds 34011da177e4SLinus Torvalds case IPPROTO_UDP: { 34021da177e4SLinus Torvalds struct udphdr _udph, *uh; 34031da177e4SLinus Torvalds 34041da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 34051da177e4SLinus Torvalds break; 34061da177e4SLinus Torvalds 34071da177e4SLinus Torvalds offset += ihlen; 34081da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 34091da177e4SLinus Torvalds if (uh == NULL) 34101da177e4SLinus Torvalds break; 34111da177e4SLinus Torvalds 34121da177e4SLinus Torvalds ad->u.net.sport = uh->source; 34131da177e4SLinus Torvalds ad->u.net.dport = uh->dest; 34141da177e4SLinus Torvalds break; 34151da177e4SLinus Torvalds } 34161da177e4SLinus Torvalds 34172ee92d46SJames Morris case IPPROTO_DCCP: { 34182ee92d46SJames Morris struct dccp_hdr _dccph, *dh; 34192ee92d46SJames Morris 34202ee92d46SJames Morris if (ntohs(ih->frag_off) & IP_OFFSET) 34212ee92d46SJames Morris break; 34222ee92d46SJames Morris 34232ee92d46SJames Morris offset += ihlen; 34242ee92d46SJames Morris dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); 34252ee92d46SJames Morris if (dh == NULL) 34262ee92d46SJames Morris break; 34272ee92d46SJames Morris 34282ee92d46SJames Morris ad->u.net.sport = dh->dccph_sport; 34292ee92d46SJames Morris ad->u.net.dport = dh->dccph_dport; 34302ee92d46SJames Morris break; 34312ee92d46SJames Morris } 34322ee92d46SJames Morris 34331da177e4SLinus Torvalds default: 34341da177e4SLinus Torvalds break; 34351da177e4SLinus Torvalds } 34361da177e4SLinus Torvalds out: 34371da177e4SLinus Torvalds return ret; 34381da177e4SLinus Torvalds } 34391da177e4SLinus Torvalds 34401da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 34411da177e4SLinus Torvalds 34421da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 344367f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv6(struct sk_buff *skb, 34442bf49690SThomas Liu struct common_audit_data *ad, u8 *proto) 34451da177e4SLinus Torvalds { 34461da177e4SLinus Torvalds u8 nexthdr; 34471da177e4SLinus Torvalds int ret = -EINVAL, offset; 34481da177e4SLinus Torvalds struct ipv6hdr _ipv6h, *ip6; 34491da177e4SLinus Torvalds 3450bbe735e4SArnaldo Carvalho de Melo offset = skb_network_offset(skb); 34511da177e4SLinus Torvalds ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h); 34521da177e4SLinus Torvalds if (ip6 == NULL) 34531da177e4SLinus Torvalds goto out; 34541da177e4SLinus Torvalds 34551da177e4SLinus Torvalds ipv6_addr_copy(&ad->u.net.v6info.saddr, &ip6->saddr); 34561da177e4SLinus Torvalds ipv6_addr_copy(&ad->u.net.v6info.daddr, &ip6->daddr); 34571da177e4SLinus Torvalds ret = 0; 34581da177e4SLinus Torvalds 34591da177e4SLinus Torvalds nexthdr = ip6->nexthdr; 34601da177e4SLinus Torvalds offset += sizeof(_ipv6h); 34610d3d077cSHerbert Xu offset = ipv6_skip_exthdr(skb, offset, &nexthdr); 34621da177e4SLinus Torvalds if (offset < 0) 34631da177e4SLinus Torvalds goto out; 34641da177e4SLinus Torvalds 346567f83cbfSVenkat Yekkirala if (proto) 346667f83cbfSVenkat Yekkirala *proto = nexthdr; 346767f83cbfSVenkat Yekkirala 34681da177e4SLinus Torvalds switch (nexthdr) { 34691da177e4SLinus Torvalds case IPPROTO_TCP: { 34701da177e4SLinus Torvalds struct tcphdr _tcph, *th; 34711da177e4SLinus Torvalds 34721da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 34731da177e4SLinus Torvalds if (th == NULL) 34741da177e4SLinus Torvalds break; 34751da177e4SLinus Torvalds 34761da177e4SLinus Torvalds ad->u.net.sport = th->source; 34771da177e4SLinus Torvalds ad->u.net.dport = th->dest; 34781da177e4SLinus Torvalds break; 34791da177e4SLinus Torvalds } 34801da177e4SLinus Torvalds 34811da177e4SLinus Torvalds case IPPROTO_UDP: { 34821da177e4SLinus Torvalds struct udphdr _udph, *uh; 34831da177e4SLinus Torvalds 34841da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 34851da177e4SLinus Torvalds if (uh == NULL) 34861da177e4SLinus Torvalds break; 34871da177e4SLinus Torvalds 34881da177e4SLinus Torvalds ad->u.net.sport = uh->source; 34891da177e4SLinus Torvalds ad->u.net.dport = uh->dest; 34901da177e4SLinus Torvalds break; 34911da177e4SLinus Torvalds } 34921da177e4SLinus Torvalds 34932ee92d46SJames Morris case IPPROTO_DCCP: { 34942ee92d46SJames Morris struct dccp_hdr _dccph, *dh; 34952ee92d46SJames Morris 34962ee92d46SJames Morris dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); 34972ee92d46SJames Morris if (dh == NULL) 34982ee92d46SJames Morris break; 34992ee92d46SJames Morris 35002ee92d46SJames Morris ad->u.net.sport = dh->dccph_sport; 35012ee92d46SJames Morris ad->u.net.dport = dh->dccph_dport; 35022ee92d46SJames Morris break; 35032ee92d46SJames Morris } 35042ee92d46SJames Morris 35051da177e4SLinus Torvalds /* includes fragments */ 35061da177e4SLinus Torvalds default: 35071da177e4SLinus Torvalds break; 35081da177e4SLinus Torvalds } 35091da177e4SLinus Torvalds out: 35101da177e4SLinus Torvalds return ret; 35111da177e4SLinus Torvalds } 35121da177e4SLinus Torvalds 35131da177e4SLinus Torvalds #endif /* IPV6 */ 35141da177e4SLinus Torvalds 35152bf49690SThomas Liu static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad, 3516cf9481e2SDavid Howells char **_addrp, int src, u8 *proto) 35171da177e4SLinus Torvalds { 3518cf9481e2SDavid Howells char *addrp; 3519cf9481e2SDavid Howells int ret; 35201da177e4SLinus Torvalds 35211da177e4SLinus Torvalds switch (ad->u.net.family) { 35221da177e4SLinus Torvalds case PF_INET: 352367f83cbfSVenkat Yekkirala ret = selinux_parse_skb_ipv4(skb, ad, proto); 3524cf9481e2SDavid Howells if (ret) 3525cf9481e2SDavid Howells goto parse_error; 3526cf9481e2SDavid Howells addrp = (char *)(src ? &ad->u.net.v4info.saddr : 35271da177e4SLinus Torvalds &ad->u.net.v4info.daddr); 3528cf9481e2SDavid Howells goto okay; 35291da177e4SLinus Torvalds 35301da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 35311da177e4SLinus Torvalds case PF_INET6: 353267f83cbfSVenkat Yekkirala ret = selinux_parse_skb_ipv6(skb, ad, proto); 3533cf9481e2SDavid Howells if (ret) 3534cf9481e2SDavid Howells goto parse_error; 3535cf9481e2SDavid Howells addrp = (char *)(src ? &ad->u.net.v6info.saddr : 35361da177e4SLinus Torvalds &ad->u.net.v6info.daddr); 3537cf9481e2SDavid Howells goto okay; 35381da177e4SLinus Torvalds #endif /* IPV6 */ 35391da177e4SLinus Torvalds default: 3540cf9481e2SDavid Howells addrp = NULL; 3541cf9481e2SDavid Howells goto okay; 35421da177e4SLinus Torvalds } 35431da177e4SLinus Torvalds 3544cf9481e2SDavid Howells parse_error: 354571f1cb05SPaul Moore printk(KERN_WARNING 354671f1cb05SPaul Moore "SELinux: failure in selinux_parse_skb()," 354771f1cb05SPaul Moore " unable to parse packet\n"); 35481da177e4SLinus Torvalds return ret; 3549cf9481e2SDavid Howells 3550cf9481e2SDavid Howells okay: 3551cf9481e2SDavid Howells if (_addrp) 3552cf9481e2SDavid Howells *_addrp = addrp; 3553cf9481e2SDavid Howells return 0; 35541da177e4SLinus Torvalds } 35551da177e4SLinus Torvalds 35564f6a993fSPaul Moore /** 3557220deb96SPaul Moore * selinux_skb_peerlbl_sid - Determine the peer label of a packet 35584f6a993fSPaul Moore * @skb: the packet 355975e22910SPaul Moore * @family: protocol family 3560220deb96SPaul Moore * @sid: the packet's peer label SID 35614f6a993fSPaul Moore * 35624f6a993fSPaul Moore * Description: 3563220deb96SPaul Moore * Check the various different forms of network peer labeling and determine 3564220deb96SPaul Moore * the peer label/SID for the packet; most of the magic actually occurs in 3565220deb96SPaul Moore * the security server function security_net_peersid_cmp(). The function 3566220deb96SPaul Moore * returns zero if the value in @sid is valid (although it may be SECSID_NULL) 3567220deb96SPaul Moore * or -EACCES if @sid is invalid due to inconsistencies with the different 3568220deb96SPaul Moore * peer labels. 35694f6a993fSPaul Moore * 35704f6a993fSPaul Moore */ 3571220deb96SPaul Moore static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid) 35724f6a993fSPaul Moore { 357371f1cb05SPaul Moore int err; 35744f6a993fSPaul Moore u32 xfrm_sid; 35754f6a993fSPaul Moore u32 nlbl_sid; 3576220deb96SPaul Moore u32 nlbl_type; 35774f6a993fSPaul Moore 35784f6a993fSPaul Moore selinux_skb_xfrm_sid(skb, &xfrm_sid); 35795dbe1eb0SPaul Moore selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid); 3580220deb96SPaul Moore 358171f1cb05SPaul Moore err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid); 358271f1cb05SPaul Moore if (unlikely(err)) { 358371f1cb05SPaul Moore printk(KERN_WARNING 358471f1cb05SPaul Moore "SELinux: failure in selinux_skb_peerlbl_sid()," 358571f1cb05SPaul Moore " unable to determine packet's peer label\n"); 3586220deb96SPaul Moore return -EACCES; 358771f1cb05SPaul Moore } 3588220deb96SPaul Moore 3589220deb96SPaul Moore return 0; 35904f6a993fSPaul Moore } 35914f6a993fSPaul Moore 35921da177e4SLinus Torvalds /* socket security operations */ 3593d4f2d978SPaul Moore 3594*2ad18bdfSHarry Ciao static int socket_sockcreate_sid(const struct task_security_struct *tsec, 3595*2ad18bdfSHarry Ciao u16 secclass, u32 *socksid) 3596d4f2d978SPaul Moore { 3597*2ad18bdfSHarry Ciao if (tsec->sockcreate_sid > SECSID_NULL) { 3598*2ad18bdfSHarry Ciao *socksid = tsec->sockcreate_sid; 3599*2ad18bdfSHarry Ciao return 0; 3600*2ad18bdfSHarry Ciao } 3601*2ad18bdfSHarry Ciao 3602*2ad18bdfSHarry Ciao return security_transition_sid(tsec->sid, tsec->sid, secclass, NULL, 3603*2ad18bdfSHarry Ciao socksid); 3604d4f2d978SPaul Moore } 3605d4f2d978SPaul Moore 3606253bfae6SPaul Moore static int sock_has_perm(struct task_struct *task, struct sock *sk, u32 perms) 36071da177e4SLinus Torvalds { 3608253bfae6SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 36092bf49690SThomas Liu struct common_audit_data ad; 3610253bfae6SPaul Moore u32 tsid = task_sid(task); 36111da177e4SLinus Torvalds 3612253bfae6SPaul Moore if (sksec->sid == SECINITSID_KERNEL) 3613253bfae6SPaul Moore return 0; 36141da177e4SLinus Torvalds 36152bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, NET); 3616253bfae6SPaul Moore ad.u.net.sk = sk; 36171da177e4SLinus Torvalds 3618253bfae6SPaul Moore return avc_has_perm(tsid, sksec->sid, sksec->sclass, perms, &ad); 36191da177e4SLinus Torvalds } 36201da177e4SLinus Torvalds 36211da177e4SLinus Torvalds static int selinux_socket_create(int family, int type, 36221da177e4SLinus Torvalds int protocol, int kern) 36231da177e4SLinus Torvalds { 36245fb49870SPaul Moore const struct task_security_struct *tsec = current_security(); 3625d4f2d978SPaul Moore u32 newsid; 3626275bb41eSDavid Howells u16 secclass; 3627*2ad18bdfSHarry Ciao int rc; 36281da177e4SLinus Torvalds 36291da177e4SLinus Torvalds if (kern) 3630d4f2d978SPaul Moore return 0; 36311da177e4SLinus Torvalds 3632275bb41eSDavid Howells secclass = socket_type_to_security_class(family, type, protocol); 3633*2ad18bdfSHarry Ciao rc = socket_sockcreate_sid(tsec, secclass, &newsid); 3634*2ad18bdfSHarry Ciao if (rc) 3635*2ad18bdfSHarry Ciao return rc; 3636*2ad18bdfSHarry Ciao 3637d4f2d978SPaul Moore return avc_has_perm(tsec->sid, newsid, secclass, SOCKET__CREATE, NULL); 36381da177e4SLinus Torvalds } 36391da177e4SLinus Torvalds 36407420ed23SVenkat Yekkirala static int selinux_socket_post_create(struct socket *sock, int family, 36411da177e4SLinus Torvalds int type, int protocol, int kern) 36421da177e4SLinus Torvalds { 36435fb49870SPaul Moore const struct task_security_struct *tsec = current_security(); 3644d4f2d978SPaul Moore struct inode_security_struct *isec = SOCK_INODE(sock)->i_security; 3645892c141eSVenkat Yekkirala struct sk_security_struct *sksec; 3646275bb41eSDavid Howells int err = 0; 3647275bb41eSDavid Howells 3648*2ad18bdfSHarry Ciao isec->sclass = socket_type_to_security_class(family, type, protocol); 3649*2ad18bdfSHarry Ciao 3650275bb41eSDavid Howells if (kern) 3651275bb41eSDavid Howells isec->sid = SECINITSID_KERNEL; 3652*2ad18bdfSHarry Ciao else { 3653*2ad18bdfSHarry Ciao err = socket_sockcreate_sid(tsec, isec->sclass, &(isec->sid)); 3654*2ad18bdfSHarry Ciao if (err) 3655*2ad18bdfSHarry Ciao return err; 3656*2ad18bdfSHarry Ciao } 3657275bb41eSDavid Howells 36581da177e4SLinus Torvalds isec->initialized = 1; 36591da177e4SLinus Torvalds 3660892c141eSVenkat Yekkirala if (sock->sk) { 3661892c141eSVenkat Yekkirala sksec = sock->sk->sk_security; 3662892c141eSVenkat Yekkirala sksec->sid = isec->sid; 3663220deb96SPaul Moore sksec->sclass = isec->sclass; 3664389fb800SPaul Moore err = selinux_netlbl_socket_post_create(sock->sk, family); 3665892c141eSVenkat Yekkirala } 3666892c141eSVenkat Yekkirala 36677420ed23SVenkat Yekkirala return err; 36681da177e4SLinus Torvalds } 36691da177e4SLinus Torvalds 36701da177e4SLinus Torvalds /* Range of port numbers used to automatically bind. 36711da177e4SLinus Torvalds Need to determine whether we should perform a name_bind 36721da177e4SLinus Torvalds permission check between the socket and the port number. */ 36731da177e4SLinus Torvalds 36741da177e4SLinus Torvalds static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen) 36751da177e4SLinus Torvalds { 3676253bfae6SPaul Moore struct sock *sk = sock->sk; 36771da177e4SLinus Torvalds u16 family; 36781da177e4SLinus Torvalds int err; 36791da177e4SLinus Torvalds 3680253bfae6SPaul Moore err = sock_has_perm(current, sk, SOCKET__BIND); 36811da177e4SLinus Torvalds if (err) 36821da177e4SLinus Torvalds goto out; 36831da177e4SLinus Torvalds 36841da177e4SLinus Torvalds /* 36851da177e4SLinus Torvalds * If PF_INET or PF_INET6, check name_bind permission for the port. 368613402580SJames Morris * Multiple address binding for SCTP is not supported yet: we just 368713402580SJames Morris * check the first address now. 36881da177e4SLinus Torvalds */ 3689253bfae6SPaul Moore family = sk->sk_family; 36901da177e4SLinus Torvalds if (family == PF_INET || family == PF_INET6) { 36911da177e4SLinus Torvalds char *addrp; 3692253bfae6SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 36932bf49690SThomas Liu struct common_audit_data ad; 36941da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 36951da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 36961da177e4SLinus Torvalds unsigned short snum; 3697e399f982SJames Morris u32 sid, node_perm; 36981da177e4SLinus Torvalds 36991da177e4SLinus Torvalds if (family == PF_INET) { 37001da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 37011da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 37021da177e4SLinus Torvalds addrp = (char *)&addr4->sin_addr.s_addr; 37031da177e4SLinus Torvalds } else { 37041da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 37051da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 37061da177e4SLinus Torvalds addrp = (char *)&addr6->sin6_addr.s6_addr; 37071da177e4SLinus Torvalds } 37081da177e4SLinus Torvalds 3709227b60f5SStephen Hemminger if (snum) { 3710227b60f5SStephen Hemminger int low, high; 3711227b60f5SStephen Hemminger 3712227b60f5SStephen Hemminger inet_get_local_port_range(&low, &high); 3713227b60f5SStephen Hemminger 3714227b60f5SStephen Hemminger if (snum < max(PROT_SOCK, low) || snum > high) { 37153e112172SPaul Moore err = sel_netport_sid(sk->sk_protocol, 37163e112172SPaul Moore snum, &sid); 37171da177e4SLinus Torvalds if (err) 37181da177e4SLinus Torvalds goto out; 37192bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, NET); 37201da177e4SLinus Torvalds ad.u.net.sport = htons(snum); 37211da177e4SLinus Torvalds ad.u.net.family = family; 3722253bfae6SPaul Moore err = avc_has_perm(sksec->sid, sid, 3723253bfae6SPaul Moore sksec->sclass, 37241da177e4SLinus Torvalds SOCKET__NAME_BIND, &ad); 37251da177e4SLinus Torvalds if (err) 37261da177e4SLinus Torvalds goto out; 37271da177e4SLinus Torvalds } 3728227b60f5SStephen Hemminger } 37291da177e4SLinus Torvalds 3730253bfae6SPaul Moore switch (sksec->sclass) { 373113402580SJames Morris case SECCLASS_TCP_SOCKET: 37321da177e4SLinus Torvalds node_perm = TCP_SOCKET__NODE_BIND; 37331da177e4SLinus Torvalds break; 37341da177e4SLinus Torvalds 373513402580SJames Morris case SECCLASS_UDP_SOCKET: 37361da177e4SLinus Torvalds node_perm = UDP_SOCKET__NODE_BIND; 37371da177e4SLinus Torvalds break; 37381da177e4SLinus Torvalds 37392ee92d46SJames Morris case SECCLASS_DCCP_SOCKET: 37402ee92d46SJames Morris node_perm = DCCP_SOCKET__NODE_BIND; 37412ee92d46SJames Morris break; 37422ee92d46SJames Morris 37431da177e4SLinus Torvalds default: 37441da177e4SLinus Torvalds node_perm = RAWIP_SOCKET__NODE_BIND; 37451da177e4SLinus Torvalds break; 37461da177e4SLinus Torvalds } 37471da177e4SLinus Torvalds 3748224dfbd8SPaul Moore err = sel_netnode_sid(addrp, family, &sid); 37491da177e4SLinus Torvalds if (err) 37501da177e4SLinus Torvalds goto out; 37511da177e4SLinus Torvalds 37522bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, NET); 37531da177e4SLinus Torvalds ad.u.net.sport = htons(snum); 37541da177e4SLinus Torvalds ad.u.net.family = family; 37551da177e4SLinus Torvalds 37561da177e4SLinus Torvalds if (family == PF_INET) 37571da177e4SLinus Torvalds ad.u.net.v4info.saddr = addr4->sin_addr.s_addr; 37581da177e4SLinus Torvalds else 37591da177e4SLinus Torvalds ipv6_addr_copy(&ad.u.net.v6info.saddr, &addr6->sin6_addr); 37601da177e4SLinus Torvalds 3761253bfae6SPaul Moore err = avc_has_perm(sksec->sid, sid, 3762253bfae6SPaul Moore sksec->sclass, node_perm, &ad); 37631da177e4SLinus Torvalds if (err) 37641da177e4SLinus Torvalds goto out; 37651da177e4SLinus Torvalds } 37661da177e4SLinus Torvalds out: 37671da177e4SLinus Torvalds return err; 37681da177e4SLinus Torvalds } 37691da177e4SLinus Torvalds 37701da177e4SLinus Torvalds static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen) 37711da177e4SLinus Torvalds { 3772014ab19aSPaul Moore struct sock *sk = sock->sk; 3773253bfae6SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 37741da177e4SLinus Torvalds int err; 37751da177e4SLinus Torvalds 3776253bfae6SPaul Moore err = sock_has_perm(current, sk, SOCKET__CONNECT); 37771da177e4SLinus Torvalds if (err) 37781da177e4SLinus Torvalds return err; 37791da177e4SLinus Torvalds 37801da177e4SLinus Torvalds /* 37812ee92d46SJames Morris * If a TCP or DCCP socket, check name_connect permission for the port. 37821da177e4SLinus Torvalds */ 3783253bfae6SPaul Moore if (sksec->sclass == SECCLASS_TCP_SOCKET || 3784253bfae6SPaul Moore sksec->sclass == SECCLASS_DCCP_SOCKET) { 37852bf49690SThomas Liu struct common_audit_data ad; 37861da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 37871da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 37881da177e4SLinus Torvalds unsigned short snum; 37892ee92d46SJames Morris u32 sid, perm; 37901da177e4SLinus Torvalds 37911da177e4SLinus Torvalds if (sk->sk_family == PF_INET) { 37921da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 3793911656f8SStephen Smalley if (addrlen < sizeof(struct sockaddr_in)) 37941da177e4SLinus Torvalds return -EINVAL; 37951da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 37961da177e4SLinus Torvalds } else { 37971da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 3798911656f8SStephen Smalley if (addrlen < SIN6_LEN_RFC2133) 37991da177e4SLinus Torvalds return -EINVAL; 38001da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 38011da177e4SLinus Torvalds } 38021da177e4SLinus Torvalds 38033e112172SPaul Moore err = sel_netport_sid(sk->sk_protocol, snum, &sid); 38041da177e4SLinus Torvalds if (err) 38051da177e4SLinus Torvalds goto out; 38061da177e4SLinus Torvalds 3807253bfae6SPaul Moore perm = (sksec->sclass == SECCLASS_TCP_SOCKET) ? 38082ee92d46SJames Morris TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT; 38092ee92d46SJames Morris 38102bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, NET); 38111da177e4SLinus Torvalds ad.u.net.dport = htons(snum); 38121da177e4SLinus Torvalds ad.u.net.family = sk->sk_family; 3813253bfae6SPaul Moore err = avc_has_perm(sksec->sid, sid, sksec->sclass, perm, &ad); 38141da177e4SLinus Torvalds if (err) 38151da177e4SLinus Torvalds goto out; 38161da177e4SLinus Torvalds } 38171da177e4SLinus Torvalds 3818014ab19aSPaul Moore err = selinux_netlbl_socket_connect(sk, address); 3819014ab19aSPaul Moore 38201da177e4SLinus Torvalds out: 38211da177e4SLinus Torvalds return err; 38221da177e4SLinus Torvalds } 38231da177e4SLinus Torvalds 38241da177e4SLinus Torvalds static int selinux_socket_listen(struct socket *sock, int backlog) 38251da177e4SLinus Torvalds { 3826253bfae6SPaul Moore return sock_has_perm(current, sock->sk, SOCKET__LISTEN); 38271da177e4SLinus Torvalds } 38281da177e4SLinus Torvalds 38291da177e4SLinus Torvalds static int selinux_socket_accept(struct socket *sock, struct socket *newsock) 38301da177e4SLinus Torvalds { 38311da177e4SLinus Torvalds int err; 38321da177e4SLinus Torvalds struct inode_security_struct *isec; 38331da177e4SLinus Torvalds struct inode_security_struct *newisec; 38341da177e4SLinus Torvalds 3835253bfae6SPaul Moore err = sock_has_perm(current, sock->sk, SOCKET__ACCEPT); 38361da177e4SLinus Torvalds if (err) 38371da177e4SLinus Torvalds return err; 38381da177e4SLinus Torvalds 38391da177e4SLinus Torvalds newisec = SOCK_INODE(newsock)->i_security; 38401da177e4SLinus Torvalds 38411da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 38421da177e4SLinus Torvalds newisec->sclass = isec->sclass; 38431da177e4SLinus Torvalds newisec->sid = isec->sid; 38441da177e4SLinus Torvalds newisec->initialized = 1; 38451da177e4SLinus Torvalds 38461da177e4SLinus Torvalds return 0; 38471da177e4SLinus Torvalds } 38481da177e4SLinus Torvalds 38491da177e4SLinus Torvalds static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg, 38501da177e4SLinus Torvalds int size) 38511da177e4SLinus Torvalds { 3852253bfae6SPaul Moore return sock_has_perm(current, sock->sk, SOCKET__WRITE); 38531da177e4SLinus Torvalds } 38541da177e4SLinus Torvalds 38551da177e4SLinus Torvalds static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg, 38561da177e4SLinus Torvalds int size, int flags) 38571da177e4SLinus Torvalds { 3858253bfae6SPaul Moore return sock_has_perm(current, sock->sk, SOCKET__READ); 38591da177e4SLinus Torvalds } 38601da177e4SLinus Torvalds 38611da177e4SLinus Torvalds static int selinux_socket_getsockname(struct socket *sock) 38621da177e4SLinus Torvalds { 3863253bfae6SPaul Moore return sock_has_perm(current, sock->sk, SOCKET__GETATTR); 38641da177e4SLinus Torvalds } 38651da177e4SLinus Torvalds 38661da177e4SLinus Torvalds static int selinux_socket_getpeername(struct socket *sock) 38671da177e4SLinus Torvalds { 3868253bfae6SPaul Moore return sock_has_perm(current, sock->sk, SOCKET__GETATTR); 38691da177e4SLinus Torvalds } 38701da177e4SLinus Torvalds 38711da177e4SLinus Torvalds static int selinux_socket_setsockopt(struct socket *sock, int level, int optname) 38721da177e4SLinus Torvalds { 3873f8687afeSPaul Moore int err; 3874f8687afeSPaul Moore 3875253bfae6SPaul Moore err = sock_has_perm(current, sock->sk, SOCKET__SETOPT); 3876f8687afeSPaul Moore if (err) 3877f8687afeSPaul Moore return err; 3878f8687afeSPaul Moore 3879f8687afeSPaul Moore return selinux_netlbl_socket_setsockopt(sock, level, optname); 38801da177e4SLinus Torvalds } 38811da177e4SLinus Torvalds 38821da177e4SLinus Torvalds static int selinux_socket_getsockopt(struct socket *sock, int level, 38831da177e4SLinus Torvalds int optname) 38841da177e4SLinus Torvalds { 3885253bfae6SPaul Moore return sock_has_perm(current, sock->sk, SOCKET__GETOPT); 38861da177e4SLinus Torvalds } 38871da177e4SLinus Torvalds 38881da177e4SLinus Torvalds static int selinux_socket_shutdown(struct socket *sock, int how) 38891da177e4SLinus Torvalds { 3890253bfae6SPaul Moore return sock_has_perm(current, sock->sk, SOCKET__SHUTDOWN); 38911da177e4SLinus Torvalds } 38921da177e4SLinus Torvalds 38933610cda5SDavid S. Miller static int selinux_socket_unix_stream_connect(struct sock *sock, 38943610cda5SDavid S. Miller struct sock *other, 38951da177e4SLinus Torvalds struct sock *newsk) 38961da177e4SLinus Torvalds { 38973610cda5SDavid S. Miller struct sk_security_struct *sksec_sock = sock->sk_security; 38983610cda5SDavid S. Miller struct sk_security_struct *sksec_other = other->sk_security; 38994d1e2451SPaul Moore struct sk_security_struct *sksec_new = newsk->sk_security; 39002bf49690SThomas Liu struct common_audit_data ad; 39011da177e4SLinus Torvalds int err; 39021da177e4SLinus Torvalds 39032bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, NET); 39043610cda5SDavid S. Miller ad.u.net.sk = other; 39051da177e4SLinus Torvalds 39064d1e2451SPaul Moore err = avc_has_perm(sksec_sock->sid, sksec_other->sid, 39074d1e2451SPaul Moore sksec_other->sclass, 39081da177e4SLinus Torvalds UNIX_STREAM_SOCKET__CONNECTTO, &ad); 39091da177e4SLinus Torvalds if (err) 39101da177e4SLinus Torvalds return err; 39111da177e4SLinus Torvalds 39121da177e4SLinus Torvalds /* server child socket */ 39134d1e2451SPaul Moore sksec_new->peer_sid = sksec_sock->sid; 39144d1e2451SPaul Moore err = security_sid_mls_copy(sksec_other->sid, sksec_sock->sid, 39154d1e2451SPaul Moore &sksec_new->sid); 39164d1e2451SPaul Moore if (err) 39174237c75cSVenkat Yekkirala return err; 39184d1e2451SPaul Moore 39194d1e2451SPaul Moore /* connecting socket */ 39204d1e2451SPaul Moore sksec_sock->peer_sid = sksec_new->sid; 39214d1e2451SPaul Moore 39224d1e2451SPaul Moore return 0; 39231da177e4SLinus Torvalds } 39241da177e4SLinus Torvalds 39251da177e4SLinus Torvalds static int selinux_socket_unix_may_send(struct socket *sock, 39261da177e4SLinus Torvalds struct socket *other) 39271da177e4SLinus Torvalds { 3928253bfae6SPaul Moore struct sk_security_struct *ssec = sock->sk->sk_security; 3929253bfae6SPaul Moore struct sk_security_struct *osec = other->sk->sk_security; 39302bf49690SThomas Liu struct common_audit_data ad; 39311da177e4SLinus Torvalds 39322bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, NET); 39331da177e4SLinus Torvalds ad.u.net.sk = other->sk; 39341da177e4SLinus Torvalds 3935253bfae6SPaul Moore return avc_has_perm(ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO, 3936253bfae6SPaul Moore &ad); 39371da177e4SLinus Torvalds } 39381da177e4SLinus Torvalds 3939effad8dfSPaul Moore static int selinux_inet_sys_rcv_skb(int ifindex, char *addrp, u16 family, 3940effad8dfSPaul Moore u32 peer_sid, 39412bf49690SThomas Liu struct common_audit_data *ad) 3942effad8dfSPaul Moore { 3943effad8dfSPaul Moore int err; 3944effad8dfSPaul Moore u32 if_sid; 3945effad8dfSPaul Moore u32 node_sid; 3946effad8dfSPaul Moore 3947effad8dfSPaul Moore err = sel_netif_sid(ifindex, &if_sid); 3948effad8dfSPaul Moore if (err) 3949effad8dfSPaul Moore return err; 3950effad8dfSPaul Moore err = avc_has_perm(peer_sid, if_sid, 3951effad8dfSPaul Moore SECCLASS_NETIF, NETIF__INGRESS, ad); 3952effad8dfSPaul Moore if (err) 3953effad8dfSPaul Moore return err; 3954effad8dfSPaul Moore 3955effad8dfSPaul Moore err = sel_netnode_sid(addrp, family, &node_sid); 3956effad8dfSPaul Moore if (err) 3957effad8dfSPaul Moore return err; 3958effad8dfSPaul Moore return avc_has_perm(peer_sid, node_sid, 3959effad8dfSPaul Moore SECCLASS_NODE, NODE__RECVFROM, ad); 3960effad8dfSPaul Moore } 3961effad8dfSPaul Moore 3962220deb96SPaul Moore static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb, 3963d8395c87SPaul Moore u16 family) 3964220deb96SPaul Moore { 3965277d342fSPaul Moore int err = 0; 3966220deb96SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 3967220deb96SPaul Moore u32 sk_sid = sksec->sid; 39682bf49690SThomas Liu struct common_audit_data ad; 3969d8395c87SPaul Moore char *addrp; 3970d8395c87SPaul Moore 39712bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, NET); 39728964be4aSEric Dumazet ad.u.net.netif = skb->skb_iif; 3973d8395c87SPaul Moore ad.u.net.family = family; 3974d8395c87SPaul Moore err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); 3975d8395c87SPaul Moore if (err) 3976d8395c87SPaul Moore return err; 3977220deb96SPaul Moore 397858bfbb51SPaul Moore if (selinux_secmark_enabled()) { 3979220deb96SPaul Moore err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET, 3980d8395c87SPaul Moore PACKET__RECV, &ad); 3981220deb96SPaul Moore if (err) 3982220deb96SPaul Moore return err; 398358bfbb51SPaul Moore } 3984220deb96SPaul Moore 3985d8395c87SPaul Moore err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad); 3986220deb96SPaul Moore if (err) 3987220deb96SPaul Moore return err; 3988d8395c87SPaul Moore err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad); 3989220deb96SPaul Moore 39904e5ab4cbSJames Morris return err; 39914e5ab4cbSJames Morris } 3992d28d1e08STrent Jaeger 39934e5ab4cbSJames Morris static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) 39944e5ab4cbSJames Morris { 3995220deb96SPaul Moore int err; 39964237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 3997220deb96SPaul Moore u16 family = sk->sk_family; 3998220deb96SPaul Moore u32 sk_sid = sksec->sid; 39992bf49690SThomas Liu struct common_audit_data ad; 4000220deb96SPaul Moore char *addrp; 4001d8395c87SPaul Moore u8 secmark_active; 4002d8395c87SPaul Moore u8 peerlbl_active; 40034e5ab4cbSJames Morris 40044e5ab4cbSJames Morris if (family != PF_INET && family != PF_INET6) 4005220deb96SPaul Moore return 0; 40064e5ab4cbSJames Morris 40074e5ab4cbSJames Morris /* Handle mapped IPv4 packets arriving via IPv6 sockets */ 400887fcd70dSAl Viro if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 40094e5ab4cbSJames Morris family = PF_INET; 40104e5ab4cbSJames Morris 4011d8395c87SPaul Moore /* If any sort of compatibility mode is enabled then handoff processing 4012d8395c87SPaul Moore * to the selinux_sock_rcv_skb_compat() function to deal with the 4013d8395c87SPaul Moore * special handling. We do this in an attempt to keep this function 4014d8395c87SPaul Moore * as fast and as clean as possible. */ 401558bfbb51SPaul Moore if (!selinux_policycap_netpeer) 4016d8395c87SPaul Moore return selinux_sock_rcv_skb_compat(sk, skb, family); 4017d8395c87SPaul Moore 4018d8395c87SPaul Moore secmark_active = selinux_secmark_enabled(); 4019d8395c87SPaul Moore peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled(); 4020d8395c87SPaul Moore if (!secmark_active && !peerlbl_active) 4021d8395c87SPaul Moore return 0; 4022d8395c87SPaul Moore 40232bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, NET); 40248964be4aSEric Dumazet ad.u.net.netif = skb->skb_iif; 40254e5ab4cbSJames Morris ad.u.net.family = family; 4026224dfbd8SPaul Moore err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); 40274e5ab4cbSJames Morris if (err) 4028220deb96SPaul Moore return err; 40294e5ab4cbSJames Morris 4030d8395c87SPaul Moore if (peerlbl_active) { 4031d621d35eSPaul Moore u32 peer_sid; 4032220deb96SPaul Moore 4033220deb96SPaul Moore err = selinux_skb_peerlbl_sid(skb, family, &peer_sid); 4034220deb96SPaul Moore if (err) 4035220deb96SPaul Moore return err; 40368964be4aSEric Dumazet err = selinux_inet_sys_rcv_skb(skb->skb_iif, addrp, family, 4037effad8dfSPaul Moore peer_sid, &ad); 4038dfaebe98SPaul Moore if (err) { 4039dfaebe98SPaul Moore selinux_netlbl_err(skb, err, 0); 4040effad8dfSPaul Moore return err; 4041dfaebe98SPaul Moore } 4042d621d35eSPaul Moore err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER, 4043d621d35eSPaul Moore PEER__RECV, &ad); 4044dfaebe98SPaul Moore if (err) 4045dfaebe98SPaul Moore selinux_netlbl_err(skb, err, 0); 4046d621d35eSPaul Moore } 4047d621d35eSPaul Moore 4048d8395c87SPaul Moore if (secmark_active) { 4049effad8dfSPaul Moore err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET, 4050effad8dfSPaul Moore PACKET__RECV, &ad); 4051effad8dfSPaul Moore if (err) 4052effad8dfSPaul Moore return err; 4053effad8dfSPaul Moore } 4054effad8dfSPaul Moore 4055d621d35eSPaul Moore return err; 40561da177e4SLinus Torvalds } 40571da177e4SLinus Torvalds 40582c7946a7SCatherine Zhang static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval, 40591da177e4SLinus Torvalds int __user *optlen, unsigned len) 40601da177e4SLinus Torvalds { 40611da177e4SLinus Torvalds int err = 0; 40621da177e4SLinus Torvalds char *scontext; 40631da177e4SLinus Torvalds u32 scontext_len; 4064253bfae6SPaul Moore struct sk_security_struct *sksec = sock->sk->sk_security; 40653de4bab5SPaul Moore u32 peer_sid = SECSID_NULL; 40661da177e4SLinus Torvalds 4067253bfae6SPaul Moore if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET || 4068253bfae6SPaul Moore sksec->sclass == SECCLASS_TCP_SOCKET) 4069dd3e7836SEric Paris peer_sid = sksec->peer_sid; 4070253bfae6SPaul Moore if (peer_sid == SECSID_NULL) 4071253bfae6SPaul Moore return -ENOPROTOOPT; 40721da177e4SLinus Torvalds 40732c7946a7SCatherine Zhang err = security_sid_to_context(peer_sid, &scontext, &scontext_len); 40741da177e4SLinus Torvalds if (err) 4075253bfae6SPaul Moore return err; 40761da177e4SLinus Torvalds 40771da177e4SLinus Torvalds if (scontext_len > len) { 40781da177e4SLinus Torvalds err = -ERANGE; 40791da177e4SLinus Torvalds goto out_len; 40801da177e4SLinus Torvalds } 40811da177e4SLinus Torvalds 40821da177e4SLinus Torvalds if (copy_to_user(optval, scontext, scontext_len)) 40831da177e4SLinus Torvalds err = -EFAULT; 40841da177e4SLinus Torvalds 40851da177e4SLinus Torvalds out_len: 40861da177e4SLinus Torvalds if (put_user(scontext_len, optlen)) 40871da177e4SLinus Torvalds err = -EFAULT; 40881da177e4SLinus Torvalds kfree(scontext); 40891da177e4SLinus Torvalds return err; 40901da177e4SLinus Torvalds } 40911da177e4SLinus Torvalds 4092dc49c1f9SCatherine Zhang static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid) 40932c7946a7SCatherine Zhang { 4094dc49c1f9SCatherine Zhang u32 peer_secid = SECSID_NULL; 409575e22910SPaul Moore u16 family; 4096877ce7c1SCatherine Zhang 4097aa862900SPaul Moore if (skb && skb->protocol == htons(ETH_P_IP)) 4098aa862900SPaul Moore family = PF_INET; 4099aa862900SPaul Moore else if (skb && skb->protocol == htons(ETH_P_IPV6)) 4100aa862900SPaul Moore family = PF_INET6; 4101aa862900SPaul Moore else if (sock) 410275e22910SPaul Moore family = sock->sk->sk_family; 410375e22910SPaul Moore else 410475e22910SPaul Moore goto out; 410575e22910SPaul Moore 410675e22910SPaul Moore if (sock && family == PF_UNIX) 4107713a04aeSAhmed S. Darwish selinux_inode_getsecid(SOCK_INODE(sock), &peer_secid); 41083de4bab5SPaul Moore else if (skb) 4109220deb96SPaul Moore selinux_skb_peerlbl_sid(skb, family, &peer_secid); 41102c7946a7SCatherine Zhang 411175e22910SPaul Moore out: 4112dc49c1f9SCatherine Zhang *secid = peer_secid; 411375e22910SPaul Moore if (peer_secid == SECSID_NULL) 411475e22910SPaul Moore return -EINVAL; 411575e22910SPaul Moore return 0; 41162c7946a7SCatherine Zhang } 41172c7946a7SCatherine Zhang 41187d877f3bSAl Viro static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority) 41191da177e4SLinus Torvalds { 412084914b7eSPaul Moore struct sk_security_struct *sksec; 412184914b7eSPaul Moore 412284914b7eSPaul Moore sksec = kzalloc(sizeof(*sksec), priority); 412384914b7eSPaul Moore if (!sksec) 412484914b7eSPaul Moore return -ENOMEM; 412584914b7eSPaul Moore 412684914b7eSPaul Moore sksec->peer_sid = SECINITSID_UNLABELED; 412784914b7eSPaul Moore sksec->sid = SECINITSID_UNLABELED; 412884914b7eSPaul Moore selinux_netlbl_sk_security_reset(sksec); 412984914b7eSPaul Moore sk->sk_security = sksec; 413084914b7eSPaul Moore 413184914b7eSPaul Moore return 0; 41321da177e4SLinus Torvalds } 41331da177e4SLinus Torvalds 41341da177e4SLinus Torvalds static void selinux_sk_free_security(struct sock *sk) 41351da177e4SLinus Torvalds { 413684914b7eSPaul Moore struct sk_security_struct *sksec = sk->sk_security; 413784914b7eSPaul Moore 413884914b7eSPaul Moore sk->sk_security = NULL; 413984914b7eSPaul Moore selinux_netlbl_sk_security_free(sksec); 414084914b7eSPaul Moore kfree(sksec); 41411da177e4SLinus Torvalds } 41421da177e4SLinus Torvalds 4143892c141eSVenkat Yekkirala static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk) 4144892c141eSVenkat Yekkirala { 4145dd3e7836SEric Paris struct sk_security_struct *sksec = sk->sk_security; 4146dd3e7836SEric Paris struct sk_security_struct *newsksec = newsk->sk_security; 4147892c141eSVenkat Yekkirala 4148dd3e7836SEric Paris newsksec->sid = sksec->sid; 4149dd3e7836SEric Paris newsksec->peer_sid = sksec->peer_sid; 4150dd3e7836SEric Paris newsksec->sclass = sksec->sclass; 415199f59ed0SPaul Moore 4152dd3e7836SEric Paris selinux_netlbl_sk_security_reset(newsksec); 4153892c141eSVenkat Yekkirala } 4154892c141eSVenkat Yekkirala 4155beb8d13bSVenkat Yekkirala static void selinux_sk_getsecid(struct sock *sk, u32 *secid) 4156d28d1e08STrent Jaeger { 4157d28d1e08STrent Jaeger if (!sk) 4158beb8d13bSVenkat Yekkirala *secid = SECINITSID_ANY_SOCKET; 4159892c141eSVenkat Yekkirala else { 4160892c141eSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 4161d28d1e08STrent Jaeger 4162beb8d13bSVenkat Yekkirala *secid = sksec->sid; 4163892c141eSVenkat Yekkirala } 4164d28d1e08STrent Jaeger } 4165d28d1e08STrent Jaeger 41669a673e56SAdrian Bunk static void selinux_sock_graft(struct sock *sk, struct socket *parent) 41674237c75cSVenkat Yekkirala { 41684237c75cSVenkat Yekkirala struct inode_security_struct *isec = SOCK_INODE(parent)->i_security; 41694237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 41704237c75cSVenkat Yekkirala 41712148ccc4SDavid Woodhouse if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 || 41722148ccc4SDavid Woodhouse sk->sk_family == PF_UNIX) 41734237c75cSVenkat Yekkirala isec->sid = sksec->sid; 4174220deb96SPaul Moore sksec->sclass = isec->sclass; 41754237c75cSVenkat Yekkirala } 41764237c75cSVenkat Yekkirala 41779a673e56SAdrian Bunk static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb, 41784237c75cSVenkat Yekkirala struct request_sock *req) 41794237c75cSVenkat Yekkirala { 41804237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 41814237c75cSVenkat Yekkirala int err; 4182aa862900SPaul Moore u16 family = sk->sk_family; 41837420ed23SVenkat Yekkirala u32 newsid; 41844237c75cSVenkat Yekkirala u32 peersid; 41854237c75cSVenkat Yekkirala 4186aa862900SPaul Moore /* handle mapped IPv4 packets arriving via IPv6 sockets */ 4187aa862900SPaul Moore if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 4188aa862900SPaul Moore family = PF_INET; 4189aa862900SPaul Moore 4190aa862900SPaul Moore err = selinux_skb_peerlbl_sid(skb, family, &peersid); 4191220deb96SPaul Moore if (err) 4192220deb96SPaul Moore return err; 4193a51c64f1SVenkat Yekkirala if (peersid == SECSID_NULL) { 4194a51c64f1SVenkat Yekkirala req->secid = sksec->sid; 41953de4bab5SPaul Moore req->peer_secid = SECSID_NULL; 4196389fb800SPaul Moore } else { 41974237c75cSVenkat Yekkirala err = security_sid_mls_copy(sksec->sid, peersid, &newsid); 41984237c75cSVenkat Yekkirala if (err) 41994237c75cSVenkat Yekkirala return err; 42004237c75cSVenkat Yekkirala req->secid = newsid; 42016b877699SVenkat Yekkirala req->peer_secid = peersid; 4202389fb800SPaul Moore } 4203389fb800SPaul Moore 4204389fb800SPaul Moore return selinux_netlbl_inet_conn_request(req, family); 42054237c75cSVenkat Yekkirala } 42064237c75cSVenkat Yekkirala 42079a673e56SAdrian Bunk static void selinux_inet_csk_clone(struct sock *newsk, 42089a673e56SAdrian Bunk const struct request_sock *req) 42094237c75cSVenkat Yekkirala { 42104237c75cSVenkat Yekkirala struct sk_security_struct *newsksec = newsk->sk_security; 42114237c75cSVenkat Yekkirala 42124237c75cSVenkat Yekkirala newsksec->sid = req->secid; 42136b877699SVenkat Yekkirala newsksec->peer_sid = req->peer_secid; 42144237c75cSVenkat Yekkirala /* NOTE: Ideally, we should also get the isec->sid for the 42154237c75cSVenkat Yekkirala new socket in sync, but we don't have the isec available yet. 42164237c75cSVenkat Yekkirala So we will wait until sock_graft to do it, by which 42174237c75cSVenkat Yekkirala time it will have been created and available. */ 421899f59ed0SPaul Moore 42199f2ad665SPaul Moore /* We don't need to take any sort of lock here as we are the only 42209f2ad665SPaul Moore * thread with access to newsksec */ 4221389fb800SPaul Moore selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family); 42224237c75cSVenkat Yekkirala } 42234237c75cSVenkat Yekkirala 4224014ab19aSPaul Moore static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb) 42256b877699SVenkat Yekkirala { 4226aa862900SPaul Moore u16 family = sk->sk_family; 42276b877699SVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 42286b877699SVenkat Yekkirala 4229aa862900SPaul Moore /* handle mapped IPv4 packets arriving via IPv6 sockets */ 4230aa862900SPaul Moore if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 4231aa862900SPaul Moore family = PF_INET; 4232aa862900SPaul Moore 4233aa862900SPaul Moore selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid); 42346b877699SVenkat Yekkirala } 42356b877699SVenkat Yekkirala 42362606fd1fSEric Paris static int selinux_secmark_relabel_packet(u32 sid) 42372606fd1fSEric Paris { 42382606fd1fSEric Paris const struct task_security_struct *__tsec; 42392606fd1fSEric Paris u32 tsid; 42402606fd1fSEric Paris 42412606fd1fSEric Paris __tsec = current_security(); 42422606fd1fSEric Paris tsid = __tsec->sid; 42432606fd1fSEric Paris 42442606fd1fSEric Paris return avc_has_perm(tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, NULL); 42452606fd1fSEric Paris } 42462606fd1fSEric Paris 42472606fd1fSEric Paris static void selinux_secmark_refcount_inc(void) 42482606fd1fSEric Paris { 42492606fd1fSEric Paris atomic_inc(&selinux_secmark_refcount); 42502606fd1fSEric Paris } 42512606fd1fSEric Paris 42522606fd1fSEric Paris static void selinux_secmark_refcount_dec(void) 42532606fd1fSEric Paris { 42542606fd1fSEric Paris atomic_dec(&selinux_secmark_refcount); 42552606fd1fSEric Paris } 42562606fd1fSEric Paris 42579a673e56SAdrian Bunk static void selinux_req_classify_flow(const struct request_sock *req, 42589a673e56SAdrian Bunk struct flowi *fl) 42594237c75cSVenkat Yekkirala { 42604237c75cSVenkat Yekkirala fl->secid = req->secid; 42614237c75cSVenkat Yekkirala } 42624237c75cSVenkat Yekkirala 4263ed6d76e4SPaul Moore static int selinux_tun_dev_create(void) 4264ed6d76e4SPaul Moore { 4265ed6d76e4SPaul Moore u32 sid = current_sid(); 4266ed6d76e4SPaul Moore 4267ed6d76e4SPaul Moore /* we aren't taking into account the "sockcreate" SID since the socket 4268ed6d76e4SPaul Moore * that is being created here is not a socket in the traditional sense, 4269ed6d76e4SPaul Moore * instead it is a private sock, accessible only to the kernel, and 4270ed6d76e4SPaul Moore * representing a wide range of network traffic spanning multiple 4271ed6d76e4SPaul Moore * connections unlike traditional sockets - check the TUN driver to 4272ed6d76e4SPaul Moore * get a better understanding of why this socket is special */ 4273ed6d76e4SPaul Moore 4274ed6d76e4SPaul Moore return avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE, 4275ed6d76e4SPaul Moore NULL); 4276ed6d76e4SPaul Moore } 4277ed6d76e4SPaul Moore 4278ed6d76e4SPaul Moore static void selinux_tun_dev_post_create(struct sock *sk) 4279ed6d76e4SPaul Moore { 4280ed6d76e4SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 4281ed6d76e4SPaul Moore 4282ed6d76e4SPaul Moore /* we don't currently perform any NetLabel based labeling here and it 4283ed6d76e4SPaul Moore * isn't clear that we would want to do so anyway; while we could apply 4284ed6d76e4SPaul Moore * labeling without the support of the TUN user the resulting labeled 4285ed6d76e4SPaul Moore * traffic from the other end of the connection would almost certainly 4286ed6d76e4SPaul Moore * cause confusion to the TUN user that had no idea network labeling 4287ed6d76e4SPaul Moore * protocols were being used */ 4288ed6d76e4SPaul Moore 4289ed6d76e4SPaul Moore /* see the comments in selinux_tun_dev_create() about why we don't use 4290ed6d76e4SPaul Moore * the sockcreate SID here */ 4291ed6d76e4SPaul Moore 4292ed6d76e4SPaul Moore sksec->sid = current_sid(); 4293ed6d76e4SPaul Moore sksec->sclass = SECCLASS_TUN_SOCKET; 4294ed6d76e4SPaul Moore } 4295ed6d76e4SPaul Moore 4296ed6d76e4SPaul Moore static int selinux_tun_dev_attach(struct sock *sk) 4297ed6d76e4SPaul Moore { 4298ed6d76e4SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 4299ed6d76e4SPaul Moore u32 sid = current_sid(); 4300ed6d76e4SPaul Moore int err; 4301ed6d76e4SPaul Moore 4302ed6d76e4SPaul Moore err = avc_has_perm(sid, sksec->sid, SECCLASS_TUN_SOCKET, 4303ed6d76e4SPaul Moore TUN_SOCKET__RELABELFROM, NULL); 4304ed6d76e4SPaul Moore if (err) 4305ed6d76e4SPaul Moore return err; 4306ed6d76e4SPaul Moore err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, 4307ed6d76e4SPaul Moore TUN_SOCKET__RELABELTO, NULL); 4308ed6d76e4SPaul Moore if (err) 4309ed6d76e4SPaul Moore return err; 4310ed6d76e4SPaul Moore 4311ed6d76e4SPaul Moore sksec->sid = sid; 4312ed6d76e4SPaul Moore 4313ed6d76e4SPaul Moore return 0; 4314ed6d76e4SPaul Moore } 4315ed6d76e4SPaul Moore 43161da177e4SLinus Torvalds static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb) 43171da177e4SLinus Torvalds { 43181da177e4SLinus Torvalds int err = 0; 43191da177e4SLinus Torvalds u32 perm; 43201da177e4SLinus Torvalds struct nlmsghdr *nlh; 4321253bfae6SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 43221da177e4SLinus Torvalds 43231da177e4SLinus Torvalds if (skb->len < NLMSG_SPACE(0)) { 43241da177e4SLinus Torvalds err = -EINVAL; 43251da177e4SLinus Torvalds goto out; 43261da177e4SLinus Torvalds } 4327b529ccf2SArnaldo Carvalho de Melo nlh = nlmsg_hdr(skb); 43281da177e4SLinus Torvalds 4329253bfae6SPaul Moore err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm); 43301da177e4SLinus Torvalds if (err) { 43311da177e4SLinus Torvalds if (err == -EINVAL) { 43329ad9ad38SDavid Woodhouse audit_log(current->audit_context, GFP_KERNEL, AUDIT_SELINUX_ERR, 43331da177e4SLinus Torvalds "SELinux: unrecognized netlink message" 43341da177e4SLinus Torvalds " type=%hu for sclass=%hu\n", 4335253bfae6SPaul Moore nlh->nlmsg_type, sksec->sclass); 433639c9aedeSEric Paris if (!selinux_enforcing || security_get_allow_unknown()) 43371da177e4SLinus Torvalds err = 0; 43381da177e4SLinus Torvalds } 43391da177e4SLinus Torvalds 43401da177e4SLinus Torvalds /* Ignore */ 43411da177e4SLinus Torvalds if (err == -ENOENT) 43421da177e4SLinus Torvalds err = 0; 43431da177e4SLinus Torvalds goto out; 43441da177e4SLinus Torvalds } 43451da177e4SLinus Torvalds 4346253bfae6SPaul Moore err = sock_has_perm(current, sk, perm); 43471da177e4SLinus Torvalds out: 43481da177e4SLinus Torvalds return err; 43491da177e4SLinus Torvalds } 43501da177e4SLinus Torvalds 43511da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER 43521da177e4SLinus Torvalds 4353effad8dfSPaul Moore static unsigned int selinux_ip_forward(struct sk_buff *skb, int ifindex, 4354effad8dfSPaul Moore u16 family) 43551da177e4SLinus Torvalds { 4356dfaebe98SPaul Moore int err; 4357effad8dfSPaul Moore char *addrp; 4358effad8dfSPaul Moore u32 peer_sid; 43592bf49690SThomas Liu struct common_audit_data ad; 4360effad8dfSPaul Moore u8 secmark_active; 4361948bf85cSPaul Moore u8 netlbl_active; 4362effad8dfSPaul Moore u8 peerlbl_active; 43634237c75cSVenkat Yekkirala 4364effad8dfSPaul Moore if (!selinux_policycap_netpeer) 4365effad8dfSPaul Moore return NF_ACCEPT; 43664237c75cSVenkat Yekkirala 4367effad8dfSPaul Moore secmark_active = selinux_secmark_enabled(); 4368948bf85cSPaul Moore netlbl_active = netlbl_enabled(); 4369948bf85cSPaul Moore peerlbl_active = netlbl_active || selinux_xfrm_enabled(); 4370effad8dfSPaul Moore if (!secmark_active && !peerlbl_active) 4371effad8dfSPaul Moore return NF_ACCEPT; 43724237c75cSVenkat Yekkirala 4373d8395c87SPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0) 4374d8395c87SPaul Moore return NF_DROP; 4375d8395c87SPaul Moore 43762bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, NET); 4377effad8dfSPaul Moore ad.u.net.netif = ifindex; 4378effad8dfSPaul Moore ad.u.net.family = family; 4379effad8dfSPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0) 4380effad8dfSPaul Moore return NF_DROP; 43811da177e4SLinus Torvalds 4382dfaebe98SPaul Moore if (peerlbl_active) { 4383dfaebe98SPaul Moore err = selinux_inet_sys_rcv_skb(ifindex, addrp, family, 4384dfaebe98SPaul Moore peer_sid, &ad); 4385dfaebe98SPaul Moore if (err) { 4386dfaebe98SPaul Moore selinux_netlbl_err(skb, err, 1); 4387effad8dfSPaul Moore return NF_DROP; 4388dfaebe98SPaul Moore } 4389dfaebe98SPaul Moore } 4390effad8dfSPaul Moore 4391effad8dfSPaul Moore if (secmark_active) 4392effad8dfSPaul Moore if (avc_has_perm(peer_sid, skb->secmark, 4393effad8dfSPaul Moore SECCLASS_PACKET, PACKET__FORWARD_IN, &ad)) 4394effad8dfSPaul Moore return NF_DROP; 4395effad8dfSPaul Moore 4396948bf85cSPaul Moore if (netlbl_active) 4397948bf85cSPaul Moore /* we do this in the FORWARD path and not the POST_ROUTING 4398948bf85cSPaul Moore * path because we want to make sure we apply the necessary 4399948bf85cSPaul Moore * labeling before IPsec is applied so we can leverage AH 4400948bf85cSPaul Moore * protection */ 4401948bf85cSPaul Moore if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0) 4402948bf85cSPaul Moore return NF_DROP; 4403948bf85cSPaul Moore 4404effad8dfSPaul Moore return NF_ACCEPT; 4405effad8dfSPaul Moore } 4406effad8dfSPaul Moore 4407effad8dfSPaul Moore static unsigned int selinux_ipv4_forward(unsigned int hooknum, 4408effad8dfSPaul Moore struct sk_buff *skb, 4409effad8dfSPaul Moore const struct net_device *in, 4410effad8dfSPaul Moore const struct net_device *out, 4411effad8dfSPaul Moore int (*okfn)(struct sk_buff *)) 4412effad8dfSPaul Moore { 4413effad8dfSPaul Moore return selinux_ip_forward(skb, in->ifindex, PF_INET); 4414effad8dfSPaul Moore } 4415effad8dfSPaul Moore 4416effad8dfSPaul Moore #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 4417effad8dfSPaul Moore static unsigned int selinux_ipv6_forward(unsigned int hooknum, 4418effad8dfSPaul Moore struct sk_buff *skb, 4419effad8dfSPaul Moore const struct net_device *in, 4420effad8dfSPaul Moore const struct net_device *out, 4421effad8dfSPaul Moore int (*okfn)(struct sk_buff *)) 4422effad8dfSPaul Moore { 4423effad8dfSPaul Moore return selinux_ip_forward(skb, in->ifindex, PF_INET6); 4424effad8dfSPaul Moore } 4425effad8dfSPaul Moore #endif /* IPV6 */ 4426effad8dfSPaul Moore 4427948bf85cSPaul Moore static unsigned int selinux_ip_output(struct sk_buff *skb, 4428948bf85cSPaul Moore u16 family) 4429948bf85cSPaul Moore { 4430948bf85cSPaul Moore u32 sid; 4431948bf85cSPaul Moore 4432948bf85cSPaul Moore if (!netlbl_enabled()) 4433948bf85cSPaul Moore return NF_ACCEPT; 4434948bf85cSPaul Moore 4435948bf85cSPaul Moore /* we do this in the LOCAL_OUT path and not the POST_ROUTING path 4436948bf85cSPaul Moore * because we want to make sure we apply the necessary labeling 4437948bf85cSPaul Moore * before IPsec is applied so we can leverage AH protection */ 4438948bf85cSPaul Moore if (skb->sk) { 4439948bf85cSPaul Moore struct sk_security_struct *sksec = skb->sk->sk_security; 4440948bf85cSPaul Moore sid = sksec->sid; 4441948bf85cSPaul Moore } else 4442948bf85cSPaul Moore sid = SECINITSID_KERNEL; 4443948bf85cSPaul Moore if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0) 4444948bf85cSPaul Moore return NF_DROP; 4445948bf85cSPaul Moore 4446948bf85cSPaul Moore return NF_ACCEPT; 4447948bf85cSPaul Moore } 4448948bf85cSPaul Moore 4449948bf85cSPaul Moore static unsigned int selinux_ipv4_output(unsigned int hooknum, 4450948bf85cSPaul Moore struct sk_buff *skb, 4451948bf85cSPaul Moore const struct net_device *in, 4452948bf85cSPaul Moore const struct net_device *out, 4453948bf85cSPaul Moore int (*okfn)(struct sk_buff *)) 4454948bf85cSPaul Moore { 4455948bf85cSPaul Moore return selinux_ip_output(skb, PF_INET); 4456948bf85cSPaul Moore } 4457948bf85cSPaul Moore 4458effad8dfSPaul Moore static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb, 4459effad8dfSPaul Moore int ifindex, 4460d8395c87SPaul Moore u16 family) 44614e5ab4cbSJames Morris { 4462effad8dfSPaul Moore struct sock *sk = skb->sk; 44634237c75cSVenkat Yekkirala struct sk_security_struct *sksec; 44642bf49690SThomas Liu struct common_audit_data ad; 4465d8395c87SPaul Moore char *addrp; 4466d8395c87SPaul Moore u8 proto; 44674e5ab4cbSJames Morris 4468effad8dfSPaul Moore if (sk == NULL) 4469effad8dfSPaul Moore return NF_ACCEPT; 44704237c75cSVenkat Yekkirala sksec = sk->sk_security; 44714e5ab4cbSJames Morris 44722bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, NET); 4473d8395c87SPaul Moore ad.u.net.netif = ifindex; 4474d8395c87SPaul Moore ad.u.net.family = family; 4475d8395c87SPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto)) 4476d8395c87SPaul Moore return NF_DROP; 4477d8395c87SPaul Moore 447858bfbb51SPaul Moore if (selinux_secmark_enabled()) 4479effad8dfSPaul Moore if (avc_has_perm(sksec->sid, skb->secmark, 4480d8395c87SPaul Moore SECCLASS_PACKET, PACKET__SEND, &ad)) 44812fe66ec2SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 44821da177e4SLinus Torvalds 4483d8395c87SPaul Moore if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto)) 44842fe66ec2SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 4485effad8dfSPaul Moore 4486effad8dfSPaul Moore return NF_ACCEPT; 4487effad8dfSPaul Moore } 4488effad8dfSPaul Moore 4489effad8dfSPaul Moore static unsigned int selinux_ip_postroute(struct sk_buff *skb, int ifindex, 4490effad8dfSPaul Moore u16 family) 4491effad8dfSPaul Moore { 4492effad8dfSPaul Moore u32 secmark_perm; 4493effad8dfSPaul Moore u32 peer_sid; 4494effad8dfSPaul Moore struct sock *sk; 44952bf49690SThomas Liu struct common_audit_data ad; 4496effad8dfSPaul Moore char *addrp; 4497effad8dfSPaul Moore u8 secmark_active; 4498effad8dfSPaul Moore u8 peerlbl_active; 4499effad8dfSPaul Moore 4500effad8dfSPaul Moore /* If any sort of compatibility mode is enabled then handoff processing 4501effad8dfSPaul Moore * to the selinux_ip_postroute_compat() function to deal with the 4502effad8dfSPaul Moore * special handling. We do this in an attempt to keep this function 4503effad8dfSPaul Moore * as fast and as clean as possible. */ 450458bfbb51SPaul Moore if (!selinux_policycap_netpeer) 4505d8395c87SPaul Moore return selinux_ip_postroute_compat(skb, ifindex, family); 4506def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM 4507effad8dfSPaul Moore /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec 4508effad8dfSPaul Moore * packet transformation so allow the packet to pass without any checks 4509effad8dfSPaul Moore * since we'll have another chance to perform access control checks 4510effad8dfSPaul Moore * when the packet is on it's final way out. 4511effad8dfSPaul Moore * NOTE: there appear to be some IPv6 multicast cases where skb->dst 4512effad8dfSPaul Moore * is NULL, in this case go ahead and apply access control. */ 4513adf30907SEric Dumazet if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL) 4514effad8dfSPaul Moore return NF_ACCEPT; 4515def8b4faSAlexey Dobriyan #endif 4516effad8dfSPaul Moore secmark_active = selinux_secmark_enabled(); 4517effad8dfSPaul Moore peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled(); 4518effad8dfSPaul Moore if (!secmark_active && !peerlbl_active) 4519effad8dfSPaul Moore return NF_ACCEPT; 4520effad8dfSPaul Moore 4521d8395c87SPaul Moore /* if the packet is being forwarded then get the peer label from the 4522d8395c87SPaul Moore * packet itself; otherwise check to see if it is from a local 4523d8395c87SPaul Moore * application or the kernel, if from an application get the peer label 4524d8395c87SPaul Moore * from the sending socket, otherwise use the kernel's sid */ 4525effad8dfSPaul Moore sk = skb->sk; 4526d8395c87SPaul Moore if (sk == NULL) { 45274a7ab3dcSSteffen Klassert if (skb->skb_iif) { 4528d8395c87SPaul Moore secmark_perm = PACKET__FORWARD_OUT; 4529d8395c87SPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &peer_sid)) 453004f6d70fSEric Paris return NF_DROP; 45314a7ab3dcSSteffen Klassert } else { 45324a7ab3dcSSteffen Klassert secmark_perm = PACKET__SEND; 4533d8395c87SPaul Moore peer_sid = SECINITSID_KERNEL; 45344a7ab3dcSSteffen Klassert } 4535d8395c87SPaul Moore } else { 4536effad8dfSPaul Moore struct sk_security_struct *sksec = sk->sk_security; 4537effad8dfSPaul Moore peer_sid = sksec->sid; 4538effad8dfSPaul Moore secmark_perm = PACKET__SEND; 4539effad8dfSPaul Moore } 4540effad8dfSPaul Moore 45412bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, NET); 4542d8395c87SPaul Moore ad.u.net.netif = ifindex; 4543d8395c87SPaul Moore ad.u.net.family = family; 4544d8395c87SPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL)) 454504f6d70fSEric Paris return NF_DROP; 4546d8395c87SPaul Moore 4547effad8dfSPaul Moore if (secmark_active) 4548effad8dfSPaul Moore if (avc_has_perm(peer_sid, skb->secmark, 4549effad8dfSPaul Moore SECCLASS_PACKET, secmark_perm, &ad)) 45501f1aaf82SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 4551effad8dfSPaul Moore 4552effad8dfSPaul Moore if (peerlbl_active) { 4553effad8dfSPaul Moore u32 if_sid; 4554effad8dfSPaul Moore u32 node_sid; 4555effad8dfSPaul Moore 4556effad8dfSPaul Moore if (sel_netif_sid(ifindex, &if_sid)) 455704f6d70fSEric Paris return NF_DROP; 4558effad8dfSPaul Moore if (avc_has_perm(peer_sid, if_sid, 4559effad8dfSPaul Moore SECCLASS_NETIF, NETIF__EGRESS, &ad)) 45601f1aaf82SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 4561effad8dfSPaul Moore 4562effad8dfSPaul Moore if (sel_netnode_sid(addrp, family, &node_sid)) 456304f6d70fSEric Paris return NF_DROP; 4564effad8dfSPaul Moore if (avc_has_perm(peer_sid, node_sid, 4565effad8dfSPaul Moore SECCLASS_NODE, NODE__SENDTO, &ad)) 45661f1aaf82SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 4567effad8dfSPaul Moore } 4568effad8dfSPaul Moore 4569effad8dfSPaul Moore return NF_ACCEPT; 4570effad8dfSPaul Moore } 4571effad8dfSPaul Moore 4572effad8dfSPaul Moore static unsigned int selinux_ipv4_postroute(unsigned int hooknum, 4573a224be76SDavid S. Miller struct sk_buff *skb, 45741da177e4SLinus Torvalds const struct net_device *in, 45751da177e4SLinus Torvalds const struct net_device *out, 45761da177e4SLinus Torvalds int (*okfn)(struct sk_buff *)) 45771da177e4SLinus Torvalds { 4578effad8dfSPaul Moore return selinux_ip_postroute(skb, out->ifindex, PF_INET); 45791da177e4SLinus Torvalds } 45801da177e4SLinus Torvalds 45811da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 4582effad8dfSPaul Moore static unsigned int selinux_ipv6_postroute(unsigned int hooknum, 4583a224be76SDavid S. Miller struct sk_buff *skb, 45841da177e4SLinus Torvalds const struct net_device *in, 45851da177e4SLinus Torvalds const struct net_device *out, 45861da177e4SLinus Torvalds int (*okfn)(struct sk_buff *)) 45871da177e4SLinus Torvalds { 4588effad8dfSPaul Moore return selinux_ip_postroute(skb, out->ifindex, PF_INET6); 45891da177e4SLinus Torvalds } 45901da177e4SLinus Torvalds #endif /* IPV6 */ 45911da177e4SLinus Torvalds 45921da177e4SLinus Torvalds #endif /* CONFIG_NETFILTER */ 45931da177e4SLinus Torvalds 45941da177e4SLinus Torvalds static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb) 45951da177e4SLinus Torvalds { 45961da177e4SLinus Torvalds int err; 45971da177e4SLinus Torvalds 4598200ac532SEric Paris err = cap_netlink_send(sk, skb); 45991da177e4SLinus Torvalds if (err) 46001da177e4SLinus Torvalds return err; 46011da177e4SLinus Torvalds 4602941fc5b2SStephen Smalley return selinux_nlmsg_perm(sk, skb); 46031da177e4SLinus Torvalds } 46041da177e4SLinus Torvalds 4605c7bdb545SDarrel Goeddel static int selinux_netlink_recv(struct sk_buff *skb, int capability) 46061da177e4SLinus Torvalds { 4607c7bdb545SDarrel Goeddel int err; 46082bf49690SThomas Liu struct common_audit_data ad; 4609c7bdb545SDarrel Goeddel 4610200ac532SEric Paris err = cap_netlink_recv(skb, capability); 4611c7bdb545SDarrel Goeddel if (err) 4612c7bdb545SDarrel Goeddel return err; 4613c7bdb545SDarrel Goeddel 46142bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, CAP); 4615c7bdb545SDarrel Goeddel ad.u.cap = capability; 4616c7bdb545SDarrel Goeddel 4617c7bdb545SDarrel Goeddel return avc_has_perm(NETLINK_CB(skb).sid, NETLINK_CB(skb).sid, 4618c7bdb545SDarrel Goeddel SECCLASS_CAPABILITY, CAP_TO_MASK(capability), &ad); 46191da177e4SLinus Torvalds } 46201da177e4SLinus Torvalds 46211da177e4SLinus Torvalds static int ipc_alloc_security(struct task_struct *task, 46221da177e4SLinus Torvalds struct kern_ipc_perm *perm, 46231da177e4SLinus Torvalds u16 sclass) 46241da177e4SLinus Torvalds { 46251da177e4SLinus Torvalds struct ipc_security_struct *isec; 4626275bb41eSDavid Howells u32 sid; 46271da177e4SLinus Torvalds 462889d155efSJames Morris isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL); 46291da177e4SLinus Torvalds if (!isec) 46301da177e4SLinus Torvalds return -ENOMEM; 46311da177e4SLinus Torvalds 4632275bb41eSDavid Howells sid = task_sid(task); 46331da177e4SLinus Torvalds isec->sclass = sclass; 4634275bb41eSDavid Howells isec->sid = sid; 46351da177e4SLinus Torvalds perm->security = isec; 46361da177e4SLinus Torvalds 46371da177e4SLinus Torvalds return 0; 46381da177e4SLinus Torvalds } 46391da177e4SLinus Torvalds 46401da177e4SLinus Torvalds static void ipc_free_security(struct kern_ipc_perm *perm) 46411da177e4SLinus Torvalds { 46421da177e4SLinus Torvalds struct ipc_security_struct *isec = perm->security; 46431da177e4SLinus Torvalds perm->security = NULL; 46441da177e4SLinus Torvalds kfree(isec); 46451da177e4SLinus Torvalds } 46461da177e4SLinus Torvalds 46471da177e4SLinus Torvalds static int msg_msg_alloc_security(struct msg_msg *msg) 46481da177e4SLinus Torvalds { 46491da177e4SLinus Torvalds struct msg_security_struct *msec; 46501da177e4SLinus Torvalds 465189d155efSJames Morris msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL); 46521da177e4SLinus Torvalds if (!msec) 46531da177e4SLinus Torvalds return -ENOMEM; 46541da177e4SLinus Torvalds 46551da177e4SLinus Torvalds msec->sid = SECINITSID_UNLABELED; 46561da177e4SLinus Torvalds msg->security = msec; 46571da177e4SLinus Torvalds 46581da177e4SLinus Torvalds return 0; 46591da177e4SLinus Torvalds } 46601da177e4SLinus Torvalds 46611da177e4SLinus Torvalds static void msg_msg_free_security(struct msg_msg *msg) 46621da177e4SLinus Torvalds { 46631da177e4SLinus Torvalds struct msg_security_struct *msec = msg->security; 46641da177e4SLinus Torvalds 46651da177e4SLinus Torvalds msg->security = NULL; 46661da177e4SLinus Torvalds kfree(msec); 46671da177e4SLinus Torvalds } 46681da177e4SLinus Torvalds 46691da177e4SLinus Torvalds static int ipc_has_perm(struct kern_ipc_perm *ipc_perms, 46706af963f1SStephen Smalley u32 perms) 46711da177e4SLinus Torvalds { 46721da177e4SLinus Torvalds struct ipc_security_struct *isec; 46732bf49690SThomas Liu struct common_audit_data ad; 4674275bb41eSDavid Howells u32 sid = current_sid(); 46751da177e4SLinus Torvalds 46761da177e4SLinus Torvalds isec = ipc_perms->security; 46771da177e4SLinus Torvalds 46782bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, IPC); 46791da177e4SLinus Torvalds ad.u.ipc_id = ipc_perms->key; 46801da177e4SLinus Torvalds 4681275bb41eSDavid Howells return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad); 46821da177e4SLinus Torvalds } 46831da177e4SLinus Torvalds 46841da177e4SLinus Torvalds static int selinux_msg_msg_alloc_security(struct msg_msg *msg) 46851da177e4SLinus Torvalds { 46861da177e4SLinus Torvalds return msg_msg_alloc_security(msg); 46871da177e4SLinus Torvalds } 46881da177e4SLinus Torvalds 46891da177e4SLinus Torvalds static void selinux_msg_msg_free_security(struct msg_msg *msg) 46901da177e4SLinus Torvalds { 46911da177e4SLinus Torvalds msg_msg_free_security(msg); 46921da177e4SLinus Torvalds } 46931da177e4SLinus Torvalds 46941da177e4SLinus Torvalds /* message queue security operations */ 46951da177e4SLinus Torvalds static int selinux_msg_queue_alloc_security(struct msg_queue *msq) 46961da177e4SLinus Torvalds { 46971da177e4SLinus Torvalds struct ipc_security_struct *isec; 46982bf49690SThomas Liu struct common_audit_data ad; 4699275bb41eSDavid Howells u32 sid = current_sid(); 47001da177e4SLinus Torvalds int rc; 47011da177e4SLinus Torvalds 47021da177e4SLinus Torvalds rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ); 47031da177e4SLinus Torvalds if (rc) 47041da177e4SLinus Torvalds return rc; 47051da177e4SLinus Torvalds 47061da177e4SLinus Torvalds isec = msq->q_perm.security; 47071da177e4SLinus Torvalds 47082bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, IPC); 47091da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 47101da177e4SLinus Torvalds 4711275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ, 47121da177e4SLinus Torvalds MSGQ__CREATE, &ad); 47131da177e4SLinus Torvalds if (rc) { 47141da177e4SLinus Torvalds ipc_free_security(&msq->q_perm); 47151da177e4SLinus Torvalds return rc; 47161da177e4SLinus Torvalds } 47171da177e4SLinus Torvalds return 0; 47181da177e4SLinus Torvalds } 47191da177e4SLinus Torvalds 47201da177e4SLinus Torvalds static void selinux_msg_queue_free_security(struct msg_queue *msq) 47211da177e4SLinus Torvalds { 47221da177e4SLinus Torvalds ipc_free_security(&msq->q_perm); 47231da177e4SLinus Torvalds } 47241da177e4SLinus Torvalds 47251da177e4SLinus Torvalds static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg) 47261da177e4SLinus Torvalds { 47271da177e4SLinus Torvalds struct ipc_security_struct *isec; 47282bf49690SThomas Liu struct common_audit_data ad; 4729275bb41eSDavid Howells u32 sid = current_sid(); 47301da177e4SLinus Torvalds 47311da177e4SLinus Torvalds isec = msq->q_perm.security; 47321da177e4SLinus Torvalds 47332bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, IPC); 47341da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 47351da177e4SLinus Torvalds 4736275bb41eSDavid Howells return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ, 47371da177e4SLinus Torvalds MSGQ__ASSOCIATE, &ad); 47381da177e4SLinus Torvalds } 47391da177e4SLinus Torvalds 47401da177e4SLinus Torvalds static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd) 47411da177e4SLinus Torvalds { 47421da177e4SLinus Torvalds int err; 47431da177e4SLinus Torvalds int perms; 47441da177e4SLinus Torvalds 47451da177e4SLinus Torvalds switch (cmd) { 47461da177e4SLinus Torvalds case IPC_INFO: 47471da177e4SLinus Torvalds case MSG_INFO: 47481da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 47491da177e4SLinus Torvalds return task_has_system(current, SYSTEM__IPC_INFO); 47501da177e4SLinus Torvalds case IPC_STAT: 47511da177e4SLinus Torvalds case MSG_STAT: 47521da177e4SLinus Torvalds perms = MSGQ__GETATTR | MSGQ__ASSOCIATE; 47531da177e4SLinus Torvalds break; 47541da177e4SLinus Torvalds case IPC_SET: 47551da177e4SLinus Torvalds perms = MSGQ__SETATTR; 47561da177e4SLinus Torvalds break; 47571da177e4SLinus Torvalds case IPC_RMID: 47581da177e4SLinus Torvalds perms = MSGQ__DESTROY; 47591da177e4SLinus Torvalds break; 47601da177e4SLinus Torvalds default: 47611da177e4SLinus Torvalds return 0; 47621da177e4SLinus Torvalds } 47631da177e4SLinus Torvalds 47646af963f1SStephen Smalley err = ipc_has_perm(&msq->q_perm, perms); 47651da177e4SLinus Torvalds return err; 47661da177e4SLinus Torvalds } 47671da177e4SLinus Torvalds 47681da177e4SLinus Torvalds static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg) 47691da177e4SLinus Torvalds { 47701da177e4SLinus Torvalds struct ipc_security_struct *isec; 47711da177e4SLinus Torvalds struct msg_security_struct *msec; 47722bf49690SThomas Liu struct common_audit_data ad; 4773275bb41eSDavid Howells u32 sid = current_sid(); 47741da177e4SLinus Torvalds int rc; 47751da177e4SLinus Torvalds 47761da177e4SLinus Torvalds isec = msq->q_perm.security; 47771da177e4SLinus Torvalds msec = msg->security; 47781da177e4SLinus Torvalds 47791da177e4SLinus Torvalds /* 47801da177e4SLinus Torvalds * First time through, need to assign label to the message 47811da177e4SLinus Torvalds */ 47821da177e4SLinus Torvalds if (msec->sid == SECINITSID_UNLABELED) { 47831da177e4SLinus Torvalds /* 47841da177e4SLinus Torvalds * Compute new sid based on current process and 47851da177e4SLinus Torvalds * message queue this message will be stored in 47861da177e4SLinus Torvalds */ 4787275bb41eSDavid Howells rc = security_transition_sid(sid, isec->sid, SECCLASS_MSG, 4788652bb9b0SEric Paris NULL, &msec->sid); 47891da177e4SLinus Torvalds if (rc) 47901da177e4SLinus Torvalds return rc; 47911da177e4SLinus Torvalds } 47921da177e4SLinus Torvalds 47932bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, IPC); 47941da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 47951da177e4SLinus Torvalds 47961da177e4SLinus Torvalds /* Can this process write to the queue? */ 4797275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ, 47981da177e4SLinus Torvalds MSGQ__WRITE, &ad); 47991da177e4SLinus Torvalds if (!rc) 48001da177e4SLinus Torvalds /* Can this process send the message */ 4801275bb41eSDavid Howells rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG, 4802275bb41eSDavid Howells MSG__SEND, &ad); 48031da177e4SLinus Torvalds if (!rc) 48041da177e4SLinus Torvalds /* Can the message be put in the queue? */ 4805275bb41eSDavid Howells rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ, 4806275bb41eSDavid Howells MSGQ__ENQUEUE, &ad); 48071da177e4SLinus Torvalds 48081da177e4SLinus Torvalds return rc; 48091da177e4SLinus Torvalds } 48101da177e4SLinus Torvalds 48111da177e4SLinus Torvalds static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg, 48121da177e4SLinus Torvalds struct task_struct *target, 48131da177e4SLinus Torvalds long type, int mode) 48141da177e4SLinus Torvalds { 48151da177e4SLinus Torvalds struct ipc_security_struct *isec; 48161da177e4SLinus Torvalds struct msg_security_struct *msec; 48172bf49690SThomas Liu struct common_audit_data ad; 4818275bb41eSDavid Howells u32 sid = task_sid(target); 48191da177e4SLinus Torvalds int rc; 48201da177e4SLinus Torvalds 48211da177e4SLinus Torvalds isec = msq->q_perm.security; 48221da177e4SLinus Torvalds msec = msg->security; 48231da177e4SLinus Torvalds 48242bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, IPC); 48251da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 48261da177e4SLinus Torvalds 4827275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, 48281da177e4SLinus Torvalds SECCLASS_MSGQ, MSGQ__READ, &ad); 48291da177e4SLinus Torvalds if (!rc) 4830275bb41eSDavid Howells rc = avc_has_perm(sid, msec->sid, 48311da177e4SLinus Torvalds SECCLASS_MSG, MSG__RECEIVE, &ad); 48321da177e4SLinus Torvalds return rc; 48331da177e4SLinus Torvalds } 48341da177e4SLinus Torvalds 48351da177e4SLinus Torvalds /* Shared Memory security operations */ 48361da177e4SLinus Torvalds static int selinux_shm_alloc_security(struct shmid_kernel *shp) 48371da177e4SLinus Torvalds { 48381da177e4SLinus Torvalds struct ipc_security_struct *isec; 48392bf49690SThomas Liu struct common_audit_data ad; 4840275bb41eSDavid Howells u32 sid = current_sid(); 48411da177e4SLinus Torvalds int rc; 48421da177e4SLinus Torvalds 48431da177e4SLinus Torvalds rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM); 48441da177e4SLinus Torvalds if (rc) 48451da177e4SLinus Torvalds return rc; 48461da177e4SLinus Torvalds 48471da177e4SLinus Torvalds isec = shp->shm_perm.security; 48481da177e4SLinus Torvalds 48492bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, IPC); 48501da177e4SLinus Torvalds ad.u.ipc_id = shp->shm_perm.key; 48511da177e4SLinus Torvalds 4852275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, SECCLASS_SHM, 48531da177e4SLinus Torvalds SHM__CREATE, &ad); 48541da177e4SLinus Torvalds if (rc) { 48551da177e4SLinus Torvalds ipc_free_security(&shp->shm_perm); 48561da177e4SLinus Torvalds return rc; 48571da177e4SLinus Torvalds } 48581da177e4SLinus Torvalds return 0; 48591da177e4SLinus Torvalds } 48601da177e4SLinus Torvalds 48611da177e4SLinus Torvalds static void selinux_shm_free_security(struct shmid_kernel *shp) 48621da177e4SLinus Torvalds { 48631da177e4SLinus Torvalds ipc_free_security(&shp->shm_perm); 48641da177e4SLinus Torvalds } 48651da177e4SLinus Torvalds 48661da177e4SLinus Torvalds static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg) 48671da177e4SLinus Torvalds { 48681da177e4SLinus Torvalds struct ipc_security_struct *isec; 48692bf49690SThomas Liu struct common_audit_data ad; 4870275bb41eSDavid Howells u32 sid = current_sid(); 48711da177e4SLinus Torvalds 48721da177e4SLinus Torvalds isec = shp->shm_perm.security; 48731da177e4SLinus Torvalds 48742bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, IPC); 48751da177e4SLinus Torvalds ad.u.ipc_id = shp->shm_perm.key; 48761da177e4SLinus Torvalds 4877275bb41eSDavid Howells return avc_has_perm(sid, isec->sid, SECCLASS_SHM, 48781da177e4SLinus Torvalds SHM__ASSOCIATE, &ad); 48791da177e4SLinus Torvalds } 48801da177e4SLinus Torvalds 48811da177e4SLinus Torvalds /* Note, at this point, shp is locked down */ 48821da177e4SLinus Torvalds static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd) 48831da177e4SLinus Torvalds { 48841da177e4SLinus Torvalds int perms; 48851da177e4SLinus Torvalds int err; 48861da177e4SLinus Torvalds 48871da177e4SLinus Torvalds switch (cmd) { 48881da177e4SLinus Torvalds case IPC_INFO: 48891da177e4SLinus Torvalds case SHM_INFO: 48901da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 48911da177e4SLinus Torvalds return task_has_system(current, SYSTEM__IPC_INFO); 48921da177e4SLinus Torvalds case IPC_STAT: 48931da177e4SLinus Torvalds case SHM_STAT: 48941da177e4SLinus Torvalds perms = SHM__GETATTR | SHM__ASSOCIATE; 48951da177e4SLinus Torvalds break; 48961da177e4SLinus Torvalds case IPC_SET: 48971da177e4SLinus Torvalds perms = SHM__SETATTR; 48981da177e4SLinus Torvalds break; 48991da177e4SLinus Torvalds case SHM_LOCK: 49001da177e4SLinus Torvalds case SHM_UNLOCK: 49011da177e4SLinus Torvalds perms = SHM__LOCK; 49021da177e4SLinus Torvalds break; 49031da177e4SLinus Torvalds case IPC_RMID: 49041da177e4SLinus Torvalds perms = SHM__DESTROY; 49051da177e4SLinus Torvalds break; 49061da177e4SLinus Torvalds default: 49071da177e4SLinus Torvalds return 0; 49081da177e4SLinus Torvalds } 49091da177e4SLinus Torvalds 49106af963f1SStephen Smalley err = ipc_has_perm(&shp->shm_perm, perms); 49111da177e4SLinus Torvalds return err; 49121da177e4SLinus Torvalds } 49131da177e4SLinus Torvalds 49141da177e4SLinus Torvalds static int selinux_shm_shmat(struct shmid_kernel *shp, 49151da177e4SLinus Torvalds char __user *shmaddr, int shmflg) 49161da177e4SLinus Torvalds { 49171da177e4SLinus Torvalds u32 perms; 49181da177e4SLinus Torvalds 49191da177e4SLinus Torvalds if (shmflg & SHM_RDONLY) 49201da177e4SLinus Torvalds perms = SHM__READ; 49211da177e4SLinus Torvalds else 49221da177e4SLinus Torvalds perms = SHM__READ | SHM__WRITE; 49231da177e4SLinus Torvalds 49246af963f1SStephen Smalley return ipc_has_perm(&shp->shm_perm, perms); 49251da177e4SLinus Torvalds } 49261da177e4SLinus Torvalds 49271da177e4SLinus Torvalds /* Semaphore security operations */ 49281da177e4SLinus Torvalds static int selinux_sem_alloc_security(struct sem_array *sma) 49291da177e4SLinus Torvalds { 49301da177e4SLinus Torvalds struct ipc_security_struct *isec; 49312bf49690SThomas Liu struct common_audit_data ad; 4932275bb41eSDavid Howells u32 sid = current_sid(); 49331da177e4SLinus Torvalds int rc; 49341da177e4SLinus Torvalds 49351da177e4SLinus Torvalds rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM); 49361da177e4SLinus Torvalds if (rc) 49371da177e4SLinus Torvalds return rc; 49381da177e4SLinus Torvalds 49391da177e4SLinus Torvalds isec = sma->sem_perm.security; 49401da177e4SLinus Torvalds 49412bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, IPC); 49421da177e4SLinus Torvalds ad.u.ipc_id = sma->sem_perm.key; 49431da177e4SLinus Torvalds 4944275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, SECCLASS_SEM, 49451da177e4SLinus Torvalds SEM__CREATE, &ad); 49461da177e4SLinus Torvalds if (rc) { 49471da177e4SLinus Torvalds ipc_free_security(&sma->sem_perm); 49481da177e4SLinus Torvalds return rc; 49491da177e4SLinus Torvalds } 49501da177e4SLinus Torvalds return 0; 49511da177e4SLinus Torvalds } 49521da177e4SLinus Torvalds 49531da177e4SLinus Torvalds static void selinux_sem_free_security(struct sem_array *sma) 49541da177e4SLinus Torvalds { 49551da177e4SLinus Torvalds ipc_free_security(&sma->sem_perm); 49561da177e4SLinus Torvalds } 49571da177e4SLinus Torvalds 49581da177e4SLinus Torvalds static int selinux_sem_associate(struct sem_array *sma, int semflg) 49591da177e4SLinus Torvalds { 49601da177e4SLinus Torvalds struct ipc_security_struct *isec; 49612bf49690SThomas Liu struct common_audit_data ad; 4962275bb41eSDavid Howells u32 sid = current_sid(); 49631da177e4SLinus Torvalds 49641da177e4SLinus Torvalds isec = sma->sem_perm.security; 49651da177e4SLinus Torvalds 49662bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, IPC); 49671da177e4SLinus Torvalds ad.u.ipc_id = sma->sem_perm.key; 49681da177e4SLinus Torvalds 4969275bb41eSDavid Howells return avc_has_perm(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 static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode) 50511da177e4SLinus Torvalds { 50521da177e4SLinus Torvalds if (inode) 50531da177e4SLinus Torvalds inode_doinit_with_dentry(inode, dentry); 50541da177e4SLinus Torvalds } 50551da177e4SLinus Torvalds 50561da177e4SLinus Torvalds static int selinux_getprocattr(struct task_struct *p, 505704ff9708SAl Viro char *name, char **value) 50581da177e4SLinus Torvalds { 5059275bb41eSDavid Howells const struct task_security_struct *__tsec; 50608c8570fbSDustin Kirkland u32 sid; 50611da177e4SLinus Torvalds int error; 506204ff9708SAl Viro unsigned len; 50631da177e4SLinus Torvalds 50641da177e4SLinus Torvalds if (current != p) { 50653b11a1deSDavid Howells error = current_has_perm(p, PROCESS__GETATTR); 50661da177e4SLinus Torvalds if (error) 50671da177e4SLinus Torvalds return error; 50681da177e4SLinus Torvalds } 50691da177e4SLinus Torvalds 5070275bb41eSDavid Howells rcu_read_lock(); 5071275bb41eSDavid Howells __tsec = __task_cred(p)->security; 50721da177e4SLinus Torvalds 50731da177e4SLinus Torvalds if (!strcmp(name, "current")) 5074275bb41eSDavid Howells sid = __tsec->sid; 50751da177e4SLinus Torvalds else if (!strcmp(name, "prev")) 5076275bb41eSDavid Howells sid = __tsec->osid; 50771da177e4SLinus Torvalds else if (!strcmp(name, "exec")) 5078275bb41eSDavid Howells sid = __tsec->exec_sid; 50791da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 5080275bb41eSDavid Howells sid = __tsec->create_sid; 50814eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) 5082275bb41eSDavid Howells sid = __tsec->keycreate_sid; 508342c3e03eSEric Paris else if (!strcmp(name, "sockcreate")) 5084275bb41eSDavid Howells sid = __tsec->sockcreate_sid; 50851da177e4SLinus Torvalds else 5086275bb41eSDavid Howells goto invalid; 5087275bb41eSDavid Howells rcu_read_unlock(); 50881da177e4SLinus Torvalds 50891da177e4SLinus Torvalds if (!sid) 50901da177e4SLinus Torvalds return 0; 50911da177e4SLinus Torvalds 509204ff9708SAl Viro error = security_sid_to_context(sid, value, &len); 509304ff9708SAl Viro if (error) 509404ff9708SAl Viro return error; 509504ff9708SAl Viro return len; 5096275bb41eSDavid Howells 5097275bb41eSDavid Howells invalid: 5098275bb41eSDavid Howells rcu_read_unlock(); 5099275bb41eSDavid Howells return -EINVAL; 51001da177e4SLinus Torvalds } 51011da177e4SLinus Torvalds 51021da177e4SLinus Torvalds static int selinux_setprocattr(struct task_struct *p, 51031da177e4SLinus Torvalds char *name, void *value, size_t size) 51041da177e4SLinus Torvalds { 51051da177e4SLinus Torvalds struct task_security_struct *tsec; 51060356357cSRoland McGrath struct task_struct *tracer; 5107d84f4f99SDavid Howells struct cred *new; 5108d84f4f99SDavid Howells u32 sid = 0, ptsid; 51091da177e4SLinus Torvalds int error; 51101da177e4SLinus Torvalds char *str = value; 51111da177e4SLinus Torvalds 51121da177e4SLinus Torvalds if (current != p) { 51131da177e4SLinus Torvalds /* SELinux only allows a process to change its own 51141da177e4SLinus Torvalds security attributes. */ 51151da177e4SLinus Torvalds return -EACCES; 51161da177e4SLinus Torvalds } 51171da177e4SLinus Torvalds 51181da177e4SLinus Torvalds /* 51191da177e4SLinus Torvalds * Basic control over ability to set these attributes at all. 51201da177e4SLinus Torvalds * current == p, but we'll pass them separately in case the 51211da177e4SLinus Torvalds * above restriction is ever removed. 51221da177e4SLinus Torvalds */ 51231da177e4SLinus Torvalds if (!strcmp(name, "exec")) 51243b11a1deSDavid Howells error = current_has_perm(p, PROCESS__SETEXEC); 51251da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 51263b11a1deSDavid Howells error = current_has_perm(p, PROCESS__SETFSCREATE); 51274eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) 51283b11a1deSDavid Howells error = current_has_perm(p, PROCESS__SETKEYCREATE); 512942c3e03eSEric Paris else if (!strcmp(name, "sockcreate")) 51303b11a1deSDavid Howells error = current_has_perm(p, PROCESS__SETSOCKCREATE); 51311da177e4SLinus Torvalds else if (!strcmp(name, "current")) 51323b11a1deSDavid Howells error = current_has_perm(p, PROCESS__SETCURRENT); 51331da177e4SLinus Torvalds else 51341da177e4SLinus Torvalds error = -EINVAL; 51351da177e4SLinus Torvalds if (error) 51361da177e4SLinus Torvalds return error; 51371da177e4SLinus Torvalds 51381da177e4SLinus Torvalds /* Obtain a SID for the context, if one was specified. */ 51391da177e4SLinus Torvalds if (size && str[1] && str[1] != '\n') { 51401da177e4SLinus Torvalds if (str[size-1] == '\n') { 51411da177e4SLinus Torvalds str[size-1] = 0; 51421da177e4SLinus Torvalds size--; 51431da177e4SLinus Torvalds } 51441da177e4SLinus Torvalds error = security_context_to_sid(value, size, &sid); 514512b29f34SStephen Smalley if (error == -EINVAL && !strcmp(name, "fscreate")) { 514612b29f34SStephen Smalley if (!capable(CAP_MAC_ADMIN)) 514712b29f34SStephen Smalley return error; 514812b29f34SStephen Smalley error = security_context_to_sid_force(value, size, 514912b29f34SStephen Smalley &sid); 515012b29f34SStephen Smalley } 51511da177e4SLinus Torvalds if (error) 51521da177e4SLinus Torvalds return error; 51531da177e4SLinus Torvalds } 51541da177e4SLinus Torvalds 5155d84f4f99SDavid Howells new = prepare_creds(); 5156d84f4f99SDavid Howells if (!new) 5157d84f4f99SDavid Howells return -ENOMEM; 5158d84f4f99SDavid Howells 51591da177e4SLinus Torvalds /* Permission checking based on the specified context is 51601da177e4SLinus Torvalds performed during the actual operation (execve, 51611da177e4SLinus Torvalds open/mkdir/...), when we know the full context of the 5162d84f4f99SDavid Howells operation. See selinux_bprm_set_creds for the execve 51631da177e4SLinus Torvalds checks and may_create for the file creation checks. The 51641da177e4SLinus Torvalds operation will then fail if the context is not permitted. */ 5165d84f4f99SDavid Howells tsec = new->security; 5166d84f4f99SDavid Howells if (!strcmp(name, "exec")) { 51671da177e4SLinus Torvalds tsec->exec_sid = sid; 5168d84f4f99SDavid Howells } else if (!strcmp(name, "fscreate")) { 51691da177e4SLinus Torvalds tsec->create_sid = sid; 5170d84f4f99SDavid Howells } else if (!strcmp(name, "keycreate")) { 51714eb582cfSMichael LeMay error = may_create_key(sid, p); 51724eb582cfSMichael LeMay if (error) 5173d84f4f99SDavid Howells goto abort_change; 51744eb582cfSMichael LeMay tsec->keycreate_sid = sid; 5175d84f4f99SDavid Howells } else if (!strcmp(name, "sockcreate")) { 517642c3e03eSEric Paris tsec->sockcreate_sid = sid; 5177d84f4f99SDavid Howells } else if (!strcmp(name, "current")) { 5178d84f4f99SDavid Howells error = -EINVAL; 51791da177e4SLinus Torvalds if (sid == 0) 5180d84f4f99SDavid Howells goto abort_change; 5181d9250deaSKaiGai Kohei 5182d84f4f99SDavid Howells /* Only allow single threaded processes to change context */ 5183d84f4f99SDavid Howells error = -EPERM; 51845bb459bbSOleg Nesterov if (!current_is_single_threaded()) { 5185d84f4f99SDavid Howells error = security_bounded_transition(tsec->sid, sid); 5186d84f4f99SDavid Howells if (error) 5187d84f4f99SDavid Howells goto abort_change; 51881da177e4SLinus Torvalds } 51891da177e4SLinus Torvalds 51901da177e4SLinus Torvalds /* Check permissions for the transition. */ 51911da177e4SLinus Torvalds error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS, 51921da177e4SLinus Torvalds PROCESS__DYNTRANSITION, NULL); 51931da177e4SLinus Torvalds if (error) 5194d84f4f99SDavid Howells goto abort_change; 51951da177e4SLinus Torvalds 51961da177e4SLinus Torvalds /* Check for ptracing, and update the task SID if ok. 51971da177e4SLinus Torvalds Otherwise, leave SID unchanged and fail. */ 5198d84f4f99SDavid Howells ptsid = 0; 51991da177e4SLinus Torvalds task_lock(p); 52000d094efeSRoland McGrath tracer = tracehook_tracer_task(p); 5201d84f4f99SDavid Howells if (tracer) 5202d84f4f99SDavid Howells ptsid = task_sid(tracer); 52031da177e4SLinus Torvalds task_unlock(p); 52041da177e4SLinus Torvalds 5205d84f4f99SDavid Howells if (tracer) { 5206d84f4f99SDavid Howells error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS, 5207d84f4f99SDavid Howells PROCESS__PTRACE, NULL); 5208d84f4f99SDavid Howells if (error) 5209d84f4f99SDavid Howells goto abort_change; 5210d84f4f99SDavid Howells } 5211d84f4f99SDavid Howells 5212d84f4f99SDavid Howells tsec->sid = sid; 5213d84f4f99SDavid Howells } else { 5214d84f4f99SDavid Howells error = -EINVAL; 5215d84f4f99SDavid Howells goto abort_change; 5216d84f4f99SDavid Howells } 5217d84f4f99SDavid Howells 5218d84f4f99SDavid Howells commit_creds(new); 52191da177e4SLinus Torvalds return size; 5220d84f4f99SDavid Howells 5221d84f4f99SDavid Howells abort_change: 5222d84f4f99SDavid Howells abort_creds(new); 5223d84f4f99SDavid Howells return error; 52241da177e4SLinus Torvalds } 52251da177e4SLinus Torvalds 5226dc49c1f9SCatherine Zhang static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) 5227dc49c1f9SCatherine Zhang { 5228dc49c1f9SCatherine Zhang return security_sid_to_context(secid, secdata, seclen); 5229dc49c1f9SCatherine Zhang } 5230dc49c1f9SCatherine Zhang 52317bf570dcSDavid Howells static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid) 523263cb3449SDavid Howells { 523363cb3449SDavid Howells return security_context_to_sid(secdata, seclen, secid); 523463cb3449SDavid Howells } 523563cb3449SDavid Howells 5236dc49c1f9SCatherine Zhang static void selinux_release_secctx(char *secdata, u32 seclen) 5237dc49c1f9SCatherine Zhang { 5238dc49c1f9SCatherine Zhang kfree(secdata); 5239dc49c1f9SCatherine Zhang } 5240dc49c1f9SCatherine Zhang 52411ee65e37SDavid P. Quigley /* 52421ee65e37SDavid P. Quigley * called with inode->i_mutex locked 52431ee65e37SDavid P. Quigley */ 52441ee65e37SDavid P. Quigley static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen) 52451ee65e37SDavid P. Quigley { 52461ee65e37SDavid P. Quigley return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0); 52471ee65e37SDavid P. Quigley } 52481ee65e37SDavid P. Quigley 52491ee65e37SDavid P. Quigley /* 52501ee65e37SDavid P. Quigley * called with inode->i_mutex locked 52511ee65e37SDavid P. Quigley */ 52521ee65e37SDavid P. Quigley static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen) 52531ee65e37SDavid P. Quigley { 52541ee65e37SDavid P. Quigley return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0); 52551ee65e37SDavid P. Quigley } 52561ee65e37SDavid P. Quigley 52571ee65e37SDavid P. Quigley static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen) 52581ee65e37SDavid P. Quigley { 52591ee65e37SDavid P. Quigley int len = 0; 52601ee65e37SDavid P. Quigley len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX, 52611ee65e37SDavid P. Quigley ctx, true); 52621ee65e37SDavid P. Quigley if (len < 0) 52631ee65e37SDavid P. Quigley return len; 52641ee65e37SDavid P. Quigley *ctxlen = len; 52651ee65e37SDavid P. Quigley return 0; 52661ee65e37SDavid P. Quigley } 5267d720024eSMichael LeMay #ifdef CONFIG_KEYS 5268d720024eSMichael LeMay 5269d84f4f99SDavid Howells static int selinux_key_alloc(struct key *k, const struct cred *cred, 52707e047ef5SDavid Howells unsigned long flags) 5271d720024eSMichael LeMay { 5272d84f4f99SDavid Howells const struct task_security_struct *tsec; 5273d720024eSMichael LeMay struct key_security_struct *ksec; 5274d720024eSMichael LeMay 5275d720024eSMichael LeMay ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL); 5276d720024eSMichael LeMay if (!ksec) 5277d720024eSMichael LeMay return -ENOMEM; 5278d720024eSMichael LeMay 5279d84f4f99SDavid Howells tsec = cred->security; 5280d84f4f99SDavid Howells if (tsec->keycreate_sid) 5281d84f4f99SDavid Howells ksec->sid = tsec->keycreate_sid; 52824eb582cfSMichael LeMay else 5283d84f4f99SDavid Howells ksec->sid = tsec->sid; 5284d720024eSMichael LeMay 5285275bb41eSDavid Howells k->security = ksec; 5286d720024eSMichael LeMay return 0; 5287d720024eSMichael LeMay } 5288d720024eSMichael LeMay 5289d720024eSMichael LeMay static void selinux_key_free(struct key *k) 5290d720024eSMichael LeMay { 5291d720024eSMichael LeMay struct key_security_struct *ksec = k->security; 5292d720024eSMichael LeMay 5293d720024eSMichael LeMay k->security = NULL; 5294d720024eSMichael LeMay kfree(ksec); 5295d720024eSMichael LeMay } 5296d720024eSMichael LeMay 5297d720024eSMichael LeMay static int selinux_key_permission(key_ref_t key_ref, 5298d84f4f99SDavid Howells const struct cred *cred, 5299d720024eSMichael LeMay key_perm_t perm) 5300d720024eSMichael LeMay { 5301d720024eSMichael LeMay struct key *key; 5302d720024eSMichael LeMay struct key_security_struct *ksec; 5303275bb41eSDavid Howells u32 sid; 5304d720024eSMichael LeMay 5305d720024eSMichael LeMay /* if no specific permissions are requested, we skip the 5306d720024eSMichael LeMay permission check. No serious, additional covert channels 5307d720024eSMichael LeMay appear to be created. */ 5308d720024eSMichael LeMay if (perm == 0) 5309d720024eSMichael LeMay return 0; 5310d720024eSMichael LeMay 5311d84f4f99SDavid Howells sid = cred_sid(cred); 5312275bb41eSDavid Howells 5313275bb41eSDavid Howells key = key_ref_to_ptr(key_ref); 5314275bb41eSDavid Howells ksec = key->security; 5315275bb41eSDavid Howells 5316275bb41eSDavid Howells return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL); 5317d720024eSMichael LeMay } 5318d720024eSMichael LeMay 531970a5bb72SDavid Howells static int selinux_key_getsecurity(struct key *key, char **_buffer) 532070a5bb72SDavid Howells { 532170a5bb72SDavid Howells struct key_security_struct *ksec = key->security; 532270a5bb72SDavid Howells char *context = NULL; 532370a5bb72SDavid Howells unsigned len; 532470a5bb72SDavid Howells int rc; 532570a5bb72SDavid Howells 532670a5bb72SDavid Howells rc = security_sid_to_context(ksec->sid, &context, &len); 532770a5bb72SDavid Howells if (!rc) 532870a5bb72SDavid Howells rc = len; 532970a5bb72SDavid Howells *_buffer = context; 533070a5bb72SDavid Howells return rc; 533170a5bb72SDavid Howells } 533270a5bb72SDavid Howells 5333d720024eSMichael LeMay #endif 5334d720024eSMichael LeMay 53351da177e4SLinus Torvalds static struct security_operations selinux_ops = { 5336076c54c5SAhmed S. Darwish .name = "selinux", 5337076c54c5SAhmed S. Darwish 53389e48858fSIngo Molnar .ptrace_access_check = selinux_ptrace_access_check, 53395cd9c58fSDavid Howells .ptrace_traceme = selinux_ptrace_traceme, 53401da177e4SLinus Torvalds .capget = selinux_capget, 5341d84f4f99SDavid Howells .capset = selinux_capset, 53421da177e4SLinus Torvalds .capable = selinux_capable, 53431da177e4SLinus Torvalds .quotactl = selinux_quotactl, 53441da177e4SLinus Torvalds .quota_on = selinux_quota_on, 53451da177e4SLinus Torvalds .syslog = selinux_syslog, 53461da177e4SLinus Torvalds .vm_enough_memory = selinux_vm_enough_memory, 53471da177e4SLinus Torvalds 53481da177e4SLinus Torvalds .netlink_send = selinux_netlink_send, 53491da177e4SLinus Torvalds .netlink_recv = selinux_netlink_recv, 53501da177e4SLinus Torvalds 5351a6f76f23SDavid Howells .bprm_set_creds = selinux_bprm_set_creds, 5352a6f76f23SDavid Howells .bprm_committing_creds = selinux_bprm_committing_creds, 5353a6f76f23SDavid Howells .bprm_committed_creds = selinux_bprm_committed_creds, 53541da177e4SLinus Torvalds .bprm_secureexec = selinux_bprm_secureexec, 53551da177e4SLinus Torvalds 53561da177e4SLinus Torvalds .sb_alloc_security = selinux_sb_alloc_security, 53571da177e4SLinus Torvalds .sb_free_security = selinux_sb_free_security, 53581da177e4SLinus Torvalds .sb_copy_data = selinux_sb_copy_data, 53591da177e4SLinus Torvalds .sb_kern_mount = selinux_sb_kern_mount, 53602069f457SEric Paris .sb_show_options = selinux_sb_show_options, 53611da177e4SLinus Torvalds .sb_statfs = selinux_sb_statfs, 53621da177e4SLinus Torvalds .sb_mount = selinux_mount, 53631da177e4SLinus Torvalds .sb_umount = selinux_umount, 5364c9180a57SEric Paris .sb_set_mnt_opts = selinux_set_mnt_opts, 5365c9180a57SEric Paris .sb_clone_mnt_opts = selinux_sb_clone_mnt_opts, 5366e0007529SEric Paris .sb_parse_opts_str = selinux_parse_opts_str, 5367e0007529SEric Paris 53681da177e4SLinus Torvalds 53691da177e4SLinus Torvalds .inode_alloc_security = selinux_inode_alloc_security, 53701da177e4SLinus Torvalds .inode_free_security = selinux_inode_free_security, 53715e41ff9eSStephen Smalley .inode_init_security = selinux_inode_init_security, 53721da177e4SLinus Torvalds .inode_create = selinux_inode_create, 53731da177e4SLinus Torvalds .inode_link = selinux_inode_link, 53741da177e4SLinus Torvalds .inode_unlink = selinux_inode_unlink, 53751da177e4SLinus Torvalds .inode_symlink = selinux_inode_symlink, 53761da177e4SLinus Torvalds .inode_mkdir = selinux_inode_mkdir, 53771da177e4SLinus Torvalds .inode_rmdir = selinux_inode_rmdir, 53781da177e4SLinus Torvalds .inode_mknod = selinux_inode_mknod, 53791da177e4SLinus Torvalds .inode_rename = selinux_inode_rename, 53801da177e4SLinus Torvalds .inode_readlink = selinux_inode_readlink, 53811da177e4SLinus Torvalds .inode_follow_link = selinux_inode_follow_link, 53821da177e4SLinus Torvalds .inode_permission = selinux_inode_permission, 53831da177e4SLinus Torvalds .inode_setattr = selinux_inode_setattr, 53841da177e4SLinus Torvalds .inode_getattr = selinux_inode_getattr, 53851da177e4SLinus Torvalds .inode_setxattr = selinux_inode_setxattr, 53861da177e4SLinus Torvalds .inode_post_setxattr = selinux_inode_post_setxattr, 53871da177e4SLinus Torvalds .inode_getxattr = selinux_inode_getxattr, 53881da177e4SLinus Torvalds .inode_listxattr = selinux_inode_listxattr, 53891da177e4SLinus Torvalds .inode_removexattr = selinux_inode_removexattr, 53901da177e4SLinus Torvalds .inode_getsecurity = selinux_inode_getsecurity, 53911da177e4SLinus Torvalds .inode_setsecurity = selinux_inode_setsecurity, 53921da177e4SLinus Torvalds .inode_listsecurity = selinux_inode_listsecurity, 5393713a04aeSAhmed S. Darwish .inode_getsecid = selinux_inode_getsecid, 53941da177e4SLinus Torvalds 53951da177e4SLinus Torvalds .file_permission = selinux_file_permission, 53961da177e4SLinus Torvalds .file_alloc_security = selinux_file_alloc_security, 53971da177e4SLinus Torvalds .file_free_security = selinux_file_free_security, 53981da177e4SLinus Torvalds .file_ioctl = selinux_file_ioctl, 53991da177e4SLinus Torvalds .file_mmap = selinux_file_mmap, 54001da177e4SLinus Torvalds .file_mprotect = selinux_file_mprotect, 54011da177e4SLinus Torvalds .file_lock = selinux_file_lock, 54021da177e4SLinus Torvalds .file_fcntl = selinux_file_fcntl, 54031da177e4SLinus Torvalds .file_set_fowner = selinux_file_set_fowner, 54041da177e4SLinus Torvalds .file_send_sigiotask = selinux_file_send_sigiotask, 54051da177e4SLinus Torvalds .file_receive = selinux_file_receive, 54061da177e4SLinus Torvalds 5407788e7dd4SYuichi Nakamura .dentry_open = selinux_dentry_open, 5408788e7dd4SYuichi Nakamura 54091da177e4SLinus Torvalds .task_create = selinux_task_create, 5410ee18d64cSDavid Howells .cred_alloc_blank = selinux_cred_alloc_blank, 5411f1752eecSDavid Howells .cred_free = selinux_cred_free, 5412d84f4f99SDavid Howells .cred_prepare = selinux_cred_prepare, 5413ee18d64cSDavid Howells .cred_transfer = selinux_cred_transfer, 54143a3b7ce9SDavid Howells .kernel_act_as = selinux_kernel_act_as, 54153a3b7ce9SDavid Howells .kernel_create_files_as = selinux_kernel_create_files_as, 541625354c4fSEric Paris .kernel_module_request = selinux_kernel_module_request, 54171da177e4SLinus Torvalds .task_setpgid = selinux_task_setpgid, 54181da177e4SLinus Torvalds .task_getpgid = selinux_task_getpgid, 54191da177e4SLinus Torvalds .task_getsid = selinux_task_getsid, 5420f9008e4cSDavid Quigley .task_getsecid = selinux_task_getsecid, 54211da177e4SLinus Torvalds .task_setnice = selinux_task_setnice, 542203e68060SJames Morris .task_setioprio = selinux_task_setioprio, 5423a1836a42SDavid Quigley .task_getioprio = selinux_task_getioprio, 54241da177e4SLinus Torvalds .task_setrlimit = selinux_task_setrlimit, 54251da177e4SLinus Torvalds .task_setscheduler = selinux_task_setscheduler, 54261da177e4SLinus Torvalds .task_getscheduler = selinux_task_getscheduler, 542735601547SDavid Quigley .task_movememory = selinux_task_movememory, 54281da177e4SLinus Torvalds .task_kill = selinux_task_kill, 54291da177e4SLinus Torvalds .task_wait = selinux_task_wait, 54301da177e4SLinus Torvalds .task_to_inode = selinux_task_to_inode, 54311da177e4SLinus Torvalds 54321da177e4SLinus Torvalds .ipc_permission = selinux_ipc_permission, 5433713a04aeSAhmed S. Darwish .ipc_getsecid = selinux_ipc_getsecid, 54341da177e4SLinus Torvalds 54351da177e4SLinus Torvalds .msg_msg_alloc_security = selinux_msg_msg_alloc_security, 54361da177e4SLinus Torvalds .msg_msg_free_security = selinux_msg_msg_free_security, 54371da177e4SLinus Torvalds 54381da177e4SLinus Torvalds .msg_queue_alloc_security = selinux_msg_queue_alloc_security, 54391da177e4SLinus Torvalds .msg_queue_free_security = selinux_msg_queue_free_security, 54401da177e4SLinus Torvalds .msg_queue_associate = selinux_msg_queue_associate, 54411da177e4SLinus Torvalds .msg_queue_msgctl = selinux_msg_queue_msgctl, 54421da177e4SLinus Torvalds .msg_queue_msgsnd = selinux_msg_queue_msgsnd, 54431da177e4SLinus Torvalds .msg_queue_msgrcv = selinux_msg_queue_msgrcv, 54441da177e4SLinus Torvalds 54451da177e4SLinus Torvalds .shm_alloc_security = selinux_shm_alloc_security, 54461da177e4SLinus Torvalds .shm_free_security = selinux_shm_free_security, 54471da177e4SLinus Torvalds .shm_associate = selinux_shm_associate, 54481da177e4SLinus Torvalds .shm_shmctl = selinux_shm_shmctl, 54491da177e4SLinus Torvalds .shm_shmat = selinux_shm_shmat, 54501da177e4SLinus Torvalds 54511da177e4SLinus Torvalds .sem_alloc_security = selinux_sem_alloc_security, 54521da177e4SLinus Torvalds .sem_free_security = selinux_sem_free_security, 54531da177e4SLinus Torvalds .sem_associate = selinux_sem_associate, 54541da177e4SLinus Torvalds .sem_semctl = selinux_sem_semctl, 54551da177e4SLinus Torvalds .sem_semop = selinux_sem_semop, 54561da177e4SLinus Torvalds 54571da177e4SLinus Torvalds .d_instantiate = selinux_d_instantiate, 54581da177e4SLinus Torvalds 54591da177e4SLinus Torvalds .getprocattr = selinux_getprocattr, 54601da177e4SLinus Torvalds .setprocattr = selinux_setprocattr, 54611da177e4SLinus Torvalds 5462dc49c1f9SCatherine Zhang .secid_to_secctx = selinux_secid_to_secctx, 546363cb3449SDavid Howells .secctx_to_secid = selinux_secctx_to_secid, 5464dc49c1f9SCatherine Zhang .release_secctx = selinux_release_secctx, 54651ee65e37SDavid P. Quigley .inode_notifysecctx = selinux_inode_notifysecctx, 54661ee65e37SDavid P. Quigley .inode_setsecctx = selinux_inode_setsecctx, 54671ee65e37SDavid P. Quigley .inode_getsecctx = selinux_inode_getsecctx, 5468dc49c1f9SCatherine Zhang 54691da177e4SLinus Torvalds .unix_stream_connect = selinux_socket_unix_stream_connect, 54701da177e4SLinus Torvalds .unix_may_send = selinux_socket_unix_may_send, 54711da177e4SLinus Torvalds 54721da177e4SLinus Torvalds .socket_create = selinux_socket_create, 54731da177e4SLinus Torvalds .socket_post_create = selinux_socket_post_create, 54741da177e4SLinus Torvalds .socket_bind = selinux_socket_bind, 54751da177e4SLinus Torvalds .socket_connect = selinux_socket_connect, 54761da177e4SLinus Torvalds .socket_listen = selinux_socket_listen, 54771da177e4SLinus Torvalds .socket_accept = selinux_socket_accept, 54781da177e4SLinus Torvalds .socket_sendmsg = selinux_socket_sendmsg, 54791da177e4SLinus Torvalds .socket_recvmsg = selinux_socket_recvmsg, 54801da177e4SLinus Torvalds .socket_getsockname = selinux_socket_getsockname, 54811da177e4SLinus Torvalds .socket_getpeername = selinux_socket_getpeername, 54821da177e4SLinus Torvalds .socket_getsockopt = selinux_socket_getsockopt, 54831da177e4SLinus Torvalds .socket_setsockopt = selinux_socket_setsockopt, 54841da177e4SLinus Torvalds .socket_shutdown = selinux_socket_shutdown, 54851da177e4SLinus Torvalds .socket_sock_rcv_skb = selinux_socket_sock_rcv_skb, 54862c7946a7SCatherine Zhang .socket_getpeersec_stream = selinux_socket_getpeersec_stream, 54872c7946a7SCatherine Zhang .socket_getpeersec_dgram = selinux_socket_getpeersec_dgram, 54881da177e4SLinus Torvalds .sk_alloc_security = selinux_sk_alloc_security, 54891da177e4SLinus Torvalds .sk_free_security = selinux_sk_free_security, 5490892c141eSVenkat Yekkirala .sk_clone_security = selinux_sk_clone_security, 5491beb8d13bSVenkat Yekkirala .sk_getsecid = selinux_sk_getsecid, 54924237c75cSVenkat Yekkirala .sock_graft = selinux_sock_graft, 54934237c75cSVenkat Yekkirala .inet_conn_request = selinux_inet_conn_request, 54944237c75cSVenkat Yekkirala .inet_csk_clone = selinux_inet_csk_clone, 54956b877699SVenkat Yekkirala .inet_conn_established = selinux_inet_conn_established, 54962606fd1fSEric Paris .secmark_relabel_packet = selinux_secmark_relabel_packet, 54972606fd1fSEric Paris .secmark_refcount_inc = selinux_secmark_refcount_inc, 54982606fd1fSEric Paris .secmark_refcount_dec = selinux_secmark_refcount_dec, 54994237c75cSVenkat Yekkirala .req_classify_flow = selinux_req_classify_flow, 5500ed6d76e4SPaul Moore .tun_dev_create = selinux_tun_dev_create, 5501ed6d76e4SPaul Moore .tun_dev_post_create = selinux_tun_dev_post_create, 5502ed6d76e4SPaul Moore .tun_dev_attach = selinux_tun_dev_attach, 5503d28d1e08STrent Jaeger 5504d28d1e08STrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM 5505d28d1e08STrent Jaeger .xfrm_policy_alloc_security = selinux_xfrm_policy_alloc, 5506d28d1e08STrent Jaeger .xfrm_policy_clone_security = selinux_xfrm_policy_clone, 5507d28d1e08STrent Jaeger .xfrm_policy_free_security = selinux_xfrm_policy_free, 5508c8c05a8eSCatherine Zhang .xfrm_policy_delete_security = selinux_xfrm_policy_delete, 5509d28d1e08STrent Jaeger .xfrm_state_alloc_security = selinux_xfrm_state_alloc, 5510d28d1e08STrent Jaeger .xfrm_state_free_security = selinux_xfrm_state_free, 5511c8c05a8eSCatherine Zhang .xfrm_state_delete_security = selinux_xfrm_state_delete, 5512d28d1e08STrent Jaeger .xfrm_policy_lookup = selinux_xfrm_policy_lookup, 5513e0d1caa7SVenkat Yekkirala .xfrm_state_pol_flow_match = selinux_xfrm_state_pol_flow_match, 5514e0d1caa7SVenkat Yekkirala .xfrm_decode_session = selinux_xfrm_decode_session, 55151da177e4SLinus Torvalds #endif 5516d720024eSMichael LeMay 5517d720024eSMichael LeMay #ifdef CONFIG_KEYS 5518d720024eSMichael LeMay .key_alloc = selinux_key_alloc, 5519d720024eSMichael LeMay .key_free = selinux_key_free, 5520d720024eSMichael LeMay .key_permission = selinux_key_permission, 552170a5bb72SDavid Howells .key_getsecurity = selinux_key_getsecurity, 5522d720024eSMichael LeMay #endif 55239d57a7f9SAhmed S. Darwish 55249d57a7f9SAhmed S. Darwish #ifdef CONFIG_AUDIT 55259d57a7f9SAhmed S. Darwish .audit_rule_init = selinux_audit_rule_init, 55269d57a7f9SAhmed S. Darwish .audit_rule_known = selinux_audit_rule_known, 55279d57a7f9SAhmed S. Darwish .audit_rule_match = selinux_audit_rule_match, 55289d57a7f9SAhmed S. Darwish .audit_rule_free = selinux_audit_rule_free, 55299d57a7f9SAhmed S. Darwish #endif 55301da177e4SLinus Torvalds }; 55311da177e4SLinus Torvalds 55321da177e4SLinus Torvalds static __init int selinux_init(void) 55331da177e4SLinus Torvalds { 5534076c54c5SAhmed S. Darwish if (!security_module_enable(&selinux_ops)) { 5535076c54c5SAhmed S. Darwish selinux_enabled = 0; 5536076c54c5SAhmed S. Darwish return 0; 5537076c54c5SAhmed S. Darwish } 5538076c54c5SAhmed S. Darwish 55391da177e4SLinus Torvalds if (!selinux_enabled) { 55401da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Disabled at boot.\n"); 55411da177e4SLinus Torvalds return 0; 55421da177e4SLinus Torvalds } 55431da177e4SLinus Torvalds 55441da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Initializing.\n"); 55451da177e4SLinus Torvalds 55461da177e4SLinus Torvalds /* Set the security state for the initial task. */ 5547d84f4f99SDavid Howells cred_init_security(); 55481da177e4SLinus Torvalds 5549fcaaade1SStephen Smalley default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC); 5550fcaaade1SStephen Smalley 55517cae7e26SJames Morris sel_inode_cache = kmem_cache_create("selinux_inode_security", 55527cae7e26SJames Morris sizeof(struct inode_security_struct), 555320c2df83SPaul Mundt 0, SLAB_PANIC, NULL); 55541da177e4SLinus Torvalds avc_init(); 55551da177e4SLinus Torvalds 55561da177e4SLinus Torvalds if (register_security(&selinux_ops)) 55571da177e4SLinus Torvalds panic("SELinux: Unable to register with kernel.\n"); 55581da177e4SLinus Torvalds 5559828dfe1dSEric Paris if (selinux_enforcing) 5560fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Starting in enforcing mode\n"); 5561828dfe1dSEric Paris else 5562fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Starting in permissive mode\n"); 5563d720024eSMichael LeMay 55641da177e4SLinus Torvalds return 0; 55651da177e4SLinus Torvalds } 55661da177e4SLinus Torvalds 5567e8c26255SAl Viro static void delayed_superblock_init(struct super_block *sb, void *unused) 5568e8c26255SAl Viro { 5569e8c26255SAl Viro superblock_doinit(sb, NULL); 5570e8c26255SAl Viro } 5571e8c26255SAl Viro 55721da177e4SLinus Torvalds void selinux_complete_init(void) 55731da177e4SLinus Torvalds { 5574fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Completing initialization.\n"); 55751da177e4SLinus Torvalds 55761da177e4SLinus Torvalds /* Set up any superblocks initialized prior to the policy load. */ 5577fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Setting up existing superblocks.\n"); 5578e8c26255SAl Viro iterate_supers(delayed_superblock_init, NULL); 55791da177e4SLinus Torvalds } 55801da177e4SLinus Torvalds 55811da177e4SLinus Torvalds /* SELinux requires early initialization in order to label 55821da177e4SLinus Torvalds all processes and objects when they are created. */ 55831da177e4SLinus Torvalds security_initcall(selinux_init); 55841da177e4SLinus Torvalds 5585c2b507fdSStephen Smalley #if defined(CONFIG_NETFILTER) 55861da177e4SLinus Torvalds 5587effad8dfSPaul Moore static struct nf_hook_ops selinux_ipv4_ops[] = { 5588effad8dfSPaul Moore { 5589effad8dfSPaul Moore .hook = selinux_ipv4_postroute, 55901da177e4SLinus Torvalds .owner = THIS_MODULE, 55911da177e4SLinus Torvalds .pf = PF_INET, 55926e23ae2aSPatrick McHardy .hooknum = NF_INET_POST_ROUTING, 55931da177e4SLinus Torvalds .priority = NF_IP_PRI_SELINUX_LAST, 5594effad8dfSPaul Moore }, 5595effad8dfSPaul Moore { 5596effad8dfSPaul Moore .hook = selinux_ipv4_forward, 5597effad8dfSPaul Moore .owner = THIS_MODULE, 5598effad8dfSPaul Moore .pf = PF_INET, 5599effad8dfSPaul Moore .hooknum = NF_INET_FORWARD, 5600effad8dfSPaul Moore .priority = NF_IP_PRI_SELINUX_FIRST, 5601948bf85cSPaul Moore }, 5602948bf85cSPaul Moore { 5603948bf85cSPaul Moore .hook = selinux_ipv4_output, 5604948bf85cSPaul Moore .owner = THIS_MODULE, 5605948bf85cSPaul Moore .pf = PF_INET, 5606948bf85cSPaul Moore .hooknum = NF_INET_LOCAL_OUT, 5607948bf85cSPaul Moore .priority = NF_IP_PRI_SELINUX_FIRST, 5608effad8dfSPaul Moore } 56091da177e4SLinus Torvalds }; 56101da177e4SLinus Torvalds 56111da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 56121da177e4SLinus Torvalds 5613effad8dfSPaul Moore static struct nf_hook_ops selinux_ipv6_ops[] = { 5614effad8dfSPaul Moore { 5615effad8dfSPaul Moore .hook = selinux_ipv6_postroute, 56161da177e4SLinus Torvalds .owner = THIS_MODULE, 56171da177e4SLinus Torvalds .pf = PF_INET6, 56186e23ae2aSPatrick McHardy .hooknum = NF_INET_POST_ROUTING, 56191da177e4SLinus Torvalds .priority = NF_IP6_PRI_SELINUX_LAST, 5620effad8dfSPaul Moore }, 5621effad8dfSPaul Moore { 5622effad8dfSPaul Moore .hook = selinux_ipv6_forward, 5623effad8dfSPaul Moore .owner = THIS_MODULE, 5624effad8dfSPaul Moore .pf = PF_INET6, 5625effad8dfSPaul Moore .hooknum = NF_INET_FORWARD, 5626effad8dfSPaul Moore .priority = NF_IP6_PRI_SELINUX_FIRST, 5627effad8dfSPaul Moore } 56281da177e4SLinus Torvalds }; 56291da177e4SLinus Torvalds 56301da177e4SLinus Torvalds #endif /* IPV6 */ 56311da177e4SLinus Torvalds 56321da177e4SLinus Torvalds static int __init selinux_nf_ip_init(void) 56331da177e4SLinus Torvalds { 56341da177e4SLinus Torvalds int err = 0; 56351da177e4SLinus Torvalds 56361da177e4SLinus Torvalds if (!selinux_enabled) 56371da177e4SLinus Torvalds goto out; 56381da177e4SLinus Torvalds 5639fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Registering netfilter hooks\n"); 56401da177e4SLinus Torvalds 56416c5a9d2eSAlexey Dobriyan err = nf_register_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops)); 56421da177e4SLinus Torvalds if (err) 56436c5a9d2eSAlexey Dobriyan panic("SELinux: nf_register_hooks for IPv4: error %d\n", err); 56441da177e4SLinus Torvalds 56451da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 56466c5a9d2eSAlexey Dobriyan err = nf_register_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops)); 56471da177e4SLinus Torvalds if (err) 56486c5a9d2eSAlexey Dobriyan panic("SELinux: nf_register_hooks for IPv6: error %d\n", err); 56491da177e4SLinus Torvalds #endif /* IPV6 */ 5650d28d1e08STrent Jaeger 56511da177e4SLinus Torvalds out: 56521da177e4SLinus Torvalds return err; 56531da177e4SLinus Torvalds } 56541da177e4SLinus Torvalds 56551da177e4SLinus Torvalds __initcall(selinux_nf_ip_init); 56561da177e4SLinus Torvalds 56571da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 56581da177e4SLinus Torvalds static void selinux_nf_ip_exit(void) 56591da177e4SLinus Torvalds { 5660fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Unregistering netfilter hooks\n"); 56611da177e4SLinus Torvalds 56626c5a9d2eSAlexey Dobriyan nf_unregister_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops)); 56631da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 56646c5a9d2eSAlexey Dobriyan nf_unregister_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops)); 56651da177e4SLinus Torvalds #endif /* IPV6 */ 56661da177e4SLinus Torvalds } 56671da177e4SLinus Torvalds #endif 56681da177e4SLinus Torvalds 5669c2b507fdSStephen Smalley #else /* CONFIG_NETFILTER */ 56701da177e4SLinus Torvalds 56711da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 56721da177e4SLinus Torvalds #define selinux_nf_ip_exit() 56731da177e4SLinus Torvalds #endif 56741da177e4SLinus Torvalds 5675c2b507fdSStephen Smalley #endif /* CONFIG_NETFILTER */ 56761da177e4SLinus Torvalds 56771da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 5678828dfe1dSEric Paris static int selinux_disabled; 5679828dfe1dSEric Paris 56801da177e4SLinus Torvalds int selinux_disable(void) 56811da177e4SLinus Torvalds { 56821da177e4SLinus Torvalds extern void exit_sel_fs(void); 56831da177e4SLinus Torvalds 56841da177e4SLinus Torvalds if (ss_initialized) { 56851da177e4SLinus Torvalds /* Not permitted after initial policy load. */ 56861da177e4SLinus Torvalds return -EINVAL; 56871da177e4SLinus Torvalds } 56881da177e4SLinus Torvalds 56891da177e4SLinus Torvalds if (selinux_disabled) { 56901da177e4SLinus Torvalds /* Only do this once. */ 56911da177e4SLinus Torvalds return -EINVAL; 56921da177e4SLinus Torvalds } 56931da177e4SLinus Torvalds 56941da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Disabled at runtime.\n"); 56951da177e4SLinus Torvalds 56961da177e4SLinus Torvalds selinux_disabled = 1; 569730d55280SStephen Smalley selinux_enabled = 0; 56981da177e4SLinus Torvalds 5699189b3b1cSwzt.wzt@gmail.com reset_security_ops(); 57001da177e4SLinus Torvalds 5701af8ff049SEric Paris /* Try to destroy the avc node cache */ 5702af8ff049SEric Paris avc_disable(); 5703af8ff049SEric Paris 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