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 * 67efbb60bSStephen Smalley * Authors: Stephen Smalley, <sds@tycho.nsa.gov> 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. 1782c21bfaSPaul Moore * Paul Moore <paul@paul-moore.com> 18788e7dd4SYuichi Nakamura * Copyright (C) 2007 Hitachi Software Engineering Co., Ltd. 19788e7dd4SYuichi Nakamura * Yuichi Nakamura <ynakam@hitachisoft.jp> 203a976fa6SDaniel Jurgens * Copyright (C) 2016 Mellanox Technologies 211da177e4SLinus Torvalds * 221da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 231da177e4SLinus Torvalds * it under the terms of the GNU General Public License version 2, 241da177e4SLinus Torvalds * as published by the Free Software Foundation. 251da177e4SLinus Torvalds */ 261da177e4SLinus Torvalds 271da177e4SLinus Torvalds #include <linux/init.h> 280b24dcb7SEric Paris #include <linux/kd.h> 291da177e4SLinus Torvalds #include <linux/kernel.h> 300d094efeSRoland McGrath #include <linux/tracehook.h> 311da177e4SLinus Torvalds #include <linux/errno.h> 323f07c014SIngo Molnar #include <linux/sched/signal.h> 3329930025SIngo Molnar #include <linux/sched/task.h> 343c4ed7bdSCasey Schaufler #include <linux/lsm_hooks.h> 351da177e4SLinus Torvalds #include <linux/xattr.h> 361da177e4SLinus Torvalds #include <linux/capability.h> 371da177e4SLinus Torvalds #include <linux/unistd.h> 381da177e4SLinus Torvalds #include <linux/mm.h> 391da177e4SLinus Torvalds #include <linux/mman.h> 401da177e4SLinus Torvalds #include <linux/slab.h> 411da177e4SLinus Torvalds #include <linux/pagemap.h> 420b24dcb7SEric Paris #include <linux/proc_fs.h> 431da177e4SLinus Torvalds #include <linux/swap.h> 441da177e4SLinus Torvalds #include <linux/spinlock.h> 451da177e4SLinus Torvalds #include <linux/syscalls.h> 462a7dba39SEric Paris #include <linux/dcache.h> 471da177e4SLinus Torvalds #include <linux/file.h> 489f3acc31SAl Viro #include <linux/fdtable.h> 491da177e4SLinus Torvalds #include <linux/namei.h> 501da177e4SLinus Torvalds #include <linux/mount.h> 511da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h> 521da177e4SLinus Torvalds #include <linux/netfilter_ipv6.h> 531da177e4SLinus Torvalds #include <linux/tty.h> 541da177e4SLinus Torvalds #include <net/icmp.h> 55227b60f5SStephen Hemminger #include <net/ip.h> /* for local_port_range[] */ 561da177e4SLinus Torvalds #include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */ 5747180068SPaul Moore #include <net/inet_connection_sock.h> 58220deb96SPaul Moore #include <net/net_namespace.h> 59d621d35eSPaul Moore #include <net/netlabel.h> 60f5269710SEric Paris #include <linux/uaccess.h> 611da177e4SLinus Torvalds #include <asm/ioctls.h> 6260063497SArun Sharma #include <linux/atomic.h> 631da177e4SLinus Torvalds #include <linux/bitops.h> 641da177e4SLinus Torvalds #include <linux/interrupt.h> 651da177e4SLinus Torvalds #include <linux/netdevice.h> /* for network interface checks */ 6677954983SHong zhi guo #include <net/netlink.h> 671da177e4SLinus Torvalds #include <linux/tcp.h> 681da177e4SLinus Torvalds #include <linux/udp.h> 692ee92d46SJames Morris #include <linux/dccp.h> 701da177e4SLinus Torvalds #include <linux/quota.h> 711da177e4SLinus Torvalds #include <linux/un.h> /* for Unix socket types */ 721da177e4SLinus Torvalds #include <net/af_unix.h> /* for Unix socket types */ 731da177e4SLinus Torvalds #include <linux/parser.h> 741da177e4SLinus Torvalds #include <linux/nfs_mount.h> 751da177e4SLinus Torvalds #include <net/ipv6.h> 761da177e4SLinus Torvalds #include <linux/hugetlb.h> 771da177e4SLinus Torvalds #include <linux/personality.h> 781da177e4SLinus Torvalds #include <linux/audit.h> 796931dfc9SEric Paris #include <linux/string.h> 80877ce7c1SCatherine Zhang #include <linux/selinux.h> 8123970741SEric Paris #include <linux/mutex.h> 82f06febc9SFrank Mayhar #include <linux/posix-timers.h> 8300234592SKees Cook #include <linux/syslog.h> 843486740aSSerge E. Hallyn #include <linux/user_namespace.h> 8544fc7ea0SPaul Gortmaker #include <linux/export.h> 8640401530SAl Viro #include <linux/msg.h> 8740401530SAl Viro #include <linux/shm.h> 88ec27c356SChenbo Feng #include <linux/bpf.h> 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds #include "avc.h" 911da177e4SLinus Torvalds #include "objsec.h" 921da177e4SLinus Torvalds #include "netif.h" 93224dfbd8SPaul Moore #include "netnode.h" 943e112172SPaul Moore #include "netport.h" 95409dcf31SDaniel Jurgens #include "ibpkey.h" 96d28d1e08STrent Jaeger #include "xfrm.h" 97c60475bfSPaul Moore #include "netlabel.h" 989d57a7f9SAhmed S. Darwish #include "audit.h" 997b98a585SJames Morris #include "avc_ss.h" 1001da177e4SLinus Torvalds 101d621d35eSPaul Moore /* SECMARK reference count */ 10256a4ca99SJames Morris static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0); 103d621d35eSPaul Moore 1041da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP 105828dfe1dSEric Paris int selinux_enforcing; 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds static int __init enforcing_setup(char *str) 1081da177e4SLinus Torvalds { 109f5269710SEric Paris unsigned long enforcing; 11029707b20SJingoo Han if (!kstrtoul(str, 0, &enforcing)) 111f5269710SEric Paris selinux_enforcing = enforcing ? 1 : 0; 1121da177e4SLinus Torvalds return 1; 1131da177e4SLinus Torvalds } 1141da177e4SLinus Torvalds __setup("enforcing=", enforcing_setup); 1151da177e4SLinus Torvalds #endif 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM 1181da177e4SLinus Torvalds int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE; 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds static int __init selinux_enabled_setup(char *str) 1211da177e4SLinus Torvalds { 122f5269710SEric Paris unsigned long enabled; 12329707b20SJingoo Han if (!kstrtoul(str, 0, &enabled)) 124f5269710SEric Paris selinux_enabled = enabled ? 1 : 0; 1251da177e4SLinus Torvalds return 1; 1261da177e4SLinus Torvalds } 1271da177e4SLinus Torvalds __setup("selinux=", selinux_enabled_setup); 12830d55280SStephen Smalley #else 12930d55280SStephen Smalley int selinux_enabled = 1; 1301da177e4SLinus Torvalds #endif 1311da177e4SLinus Torvalds 132e18b890bSChristoph Lameter static struct kmem_cache *sel_inode_cache; 13363205654SSangwoo static struct kmem_cache *file_security_cache; 1347cae7e26SJames Morris 135d621d35eSPaul Moore /** 136d621d35eSPaul Moore * selinux_secmark_enabled - Check to see if SECMARK is currently enabled 137d621d35eSPaul Moore * 138d621d35eSPaul Moore * Description: 139d621d35eSPaul Moore * This function checks the SECMARK reference counter to see if any SECMARK 140d621d35eSPaul Moore * targets are currently configured, if the reference counter is greater than 141d621d35eSPaul Moore * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is 1422be4d74fSChris PeBenito * enabled, false (0) if SECMARK is disabled. If the always_check_network 1432be4d74fSChris PeBenito * policy capability is enabled, SECMARK is always considered enabled. 144d621d35eSPaul Moore * 145d621d35eSPaul Moore */ 146d621d35eSPaul Moore static int selinux_secmark_enabled(void) 147d621d35eSPaul Moore { 1482be4d74fSChris PeBenito return (selinux_policycap_alwaysnetwork || atomic_read(&selinux_secmark_refcount)); 1492be4d74fSChris PeBenito } 1502be4d74fSChris PeBenito 1512be4d74fSChris PeBenito /** 1522be4d74fSChris PeBenito * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled 1532be4d74fSChris PeBenito * 1542be4d74fSChris PeBenito * Description: 1552be4d74fSChris PeBenito * This function checks if NetLabel or labeled IPSEC is enabled. Returns true 1562be4d74fSChris PeBenito * (1) if any are enabled or false (0) if neither are enabled. If the 1572be4d74fSChris PeBenito * always_check_network policy capability is enabled, peer labeling 1582be4d74fSChris PeBenito * is always considered enabled. 1592be4d74fSChris PeBenito * 1602be4d74fSChris PeBenito */ 1612be4d74fSChris PeBenito static int selinux_peerlbl_enabled(void) 1622be4d74fSChris PeBenito { 1632be4d74fSChris PeBenito return (selinux_policycap_alwaysnetwork || netlbl_enabled() || selinux_xfrm_enabled()); 164d621d35eSPaul Moore } 165d621d35eSPaul Moore 166615e51fdSPaul Moore static int selinux_netcache_avc_callback(u32 event) 167615e51fdSPaul Moore { 168615e51fdSPaul Moore if (event == AVC_CALLBACK_RESET) { 169615e51fdSPaul Moore sel_netif_flush(); 170615e51fdSPaul Moore sel_netnode_flush(); 171615e51fdSPaul Moore sel_netport_flush(); 172615e51fdSPaul Moore synchronize_net(); 173615e51fdSPaul Moore } 174615e51fdSPaul Moore return 0; 175615e51fdSPaul Moore } 176615e51fdSPaul Moore 1778f408ab6SDaniel Jurgens static int selinux_lsm_notifier_avc_callback(u32 event) 1788f408ab6SDaniel Jurgens { 179409dcf31SDaniel Jurgens if (event == AVC_CALLBACK_RESET) { 180409dcf31SDaniel Jurgens sel_ib_pkey_flush(); 1818f408ab6SDaniel Jurgens call_lsm_notifier(LSM_POLICY_CHANGE, NULL); 182409dcf31SDaniel Jurgens } 1838f408ab6SDaniel Jurgens 1848f408ab6SDaniel Jurgens return 0; 1858f408ab6SDaniel Jurgens } 1868f408ab6SDaniel Jurgens 187d84f4f99SDavid Howells /* 188d84f4f99SDavid Howells * initialise the security for the init task 189d84f4f99SDavid Howells */ 190d84f4f99SDavid Howells static void cred_init_security(void) 1911da177e4SLinus Torvalds { 1923b11a1deSDavid Howells struct cred *cred = (struct cred *) current->real_cred; 1931da177e4SLinus Torvalds struct task_security_struct *tsec; 1941da177e4SLinus Torvalds 19589d155efSJames Morris tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL); 1961da177e4SLinus Torvalds if (!tsec) 197d84f4f99SDavid Howells panic("SELinux: Failed to initialize initial task.\n"); 1981da177e4SLinus Torvalds 199d84f4f99SDavid Howells tsec->osid = tsec->sid = SECINITSID_KERNEL; 200f1752eecSDavid Howells cred->security = tsec; 2011da177e4SLinus Torvalds } 2021da177e4SLinus Torvalds 203275bb41eSDavid Howells /* 20488e67f3bSDavid Howells * get the security ID of a set of credentials 20588e67f3bSDavid Howells */ 20688e67f3bSDavid Howells static inline u32 cred_sid(const struct cred *cred) 20788e67f3bSDavid Howells { 20888e67f3bSDavid Howells const struct task_security_struct *tsec; 20988e67f3bSDavid Howells 21088e67f3bSDavid Howells tsec = cred->security; 21188e67f3bSDavid Howells return tsec->sid; 21288e67f3bSDavid Howells } 21388e67f3bSDavid Howells 21488e67f3bSDavid Howells /* 2153b11a1deSDavid Howells * get the objective security ID of a task 216275bb41eSDavid Howells */ 217275bb41eSDavid Howells static inline u32 task_sid(const struct task_struct *task) 218275bb41eSDavid Howells { 219275bb41eSDavid Howells u32 sid; 220275bb41eSDavid Howells 221275bb41eSDavid Howells rcu_read_lock(); 22288e67f3bSDavid Howells sid = cred_sid(__task_cred(task)); 223275bb41eSDavid Howells rcu_read_unlock(); 224275bb41eSDavid Howells return sid; 225275bb41eSDavid Howells } 226275bb41eSDavid Howells 22788e67f3bSDavid Howells /* Allocate and free functions for each kind of security blob. */ 22888e67f3bSDavid Howells 2291da177e4SLinus Torvalds static int inode_alloc_security(struct inode *inode) 2301da177e4SLinus Torvalds { 2311da177e4SLinus Torvalds struct inode_security_struct *isec; 232275bb41eSDavid Howells u32 sid = current_sid(); 2331da177e4SLinus Torvalds 234a02fe132SJosef Bacik isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS); 2351da177e4SLinus Torvalds if (!isec) 2361da177e4SLinus Torvalds return -ENOMEM; 2371da177e4SLinus Torvalds 2389287aed2SAndreas Gruenbacher spin_lock_init(&isec->lock); 2391da177e4SLinus Torvalds INIT_LIST_HEAD(&isec->list); 2401da177e4SLinus Torvalds isec->inode = inode; 2411da177e4SLinus Torvalds isec->sid = SECINITSID_UNLABELED; 2421da177e4SLinus Torvalds isec->sclass = SECCLASS_FILE; 243275bb41eSDavid Howells isec->task_sid = sid; 24442059112SAndreas Gruenbacher isec->initialized = LABEL_INVALID; 2451da177e4SLinus Torvalds inode->i_security = isec; 2461da177e4SLinus Torvalds 2471da177e4SLinus Torvalds return 0; 2481da177e4SLinus Torvalds } 2491da177e4SLinus Torvalds 2505d226df4SAndreas Gruenbacher static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry); 2515d226df4SAndreas Gruenbacher 2525d226df4SAndreas Gruenbacher /* 2535d226df4SAndreas Gruenbacher * Try reloading inode security labels that have been marked as invalid. The 2545d226df4SAndreas Gruenbacher * @may_sleep parameter indicates when sleeping and thus reloading labels is 25542059112SAndreas Gruenbacher * allowed; when set to false, returns -ECHILD when the label is 2565d226df4SAndreas Gruenbacher * invalid. The @opt_dentry parameter should be set to a dentry of the inode; 2575d226df4SAndreas Gruenbacher * when no dentry is available, set it to NULL instead. 2585d226df4SAndreas Gruenbacher */ 2595d226df4SAndreas Gruenbacher static int __inode_security_revalidate(struct inode *inode, 2605d226df4SAndreas Gruenbacher struct dentry *opt_dentry, 2615d226df4SAndreas Gruenbacher bool may_sleep) 2625d226df4SAndreas Gruenbacher { 2635d226df4SAndreas Gruenbacher struct inode_security_struct *isec = inode->i_security; 2645d226df4SAndreas Gruenbacher 2655d226df4SAndreas Gruenbacher might_sleep_if(may_sleep); 2665d226df4SAndreas Gruenbacher 2671ac42476SPaul Moore if (ss_initialized && isec->initialized != LABEL_INITIALIZED) { 2685d226df4SAndreas Gruenbacher if (!may_sleep) 2695d226df4SAndreas Gruenbacher return -ECHILD; 2705d226df4SAndreas Gruenbacher 2715d226df4SAndreas Gruenbacher /* 2725d226df4SAndreas Gruenbacher * Try reloading the inode security label. This will fail if 2735d226df4SAndreas Gruenbacher * @opt_dentry is NULL and no dentry for this inode can be 2745d226df4SAndreas Gruenbacher * found; in that case, continue using the old label. 2755d226df4SAndreas Gruenbacher */ 2765d226df4SAndreas Gruenbacher inode_doinit_with_dentry(inode, opt_dentry); 2775d226df4SAndreas Gruenbacher } 2785d226df4SAndreas Gruenbacher return 0; 2795d226df4SAndreas Gruenbacher } 2805d226df4SAndreas Gruenbacher 2815d226df4SAndreas Gruenbacher static struct inode_security_struct *inode_security_novalidate(struct inode *inode) 2825d226df4SAndreas Gruenbacher { 2835d226df4SAndreas Gruenbacher return inode->i_security; 2845d226df4SAndreas Gruenbacher } 2855d226df4SAndreas Gruenbacher 2865d226df4SAndreas Gruenbacher static struct inode_security_struct *inode_security_rcu(struct inode *inode, bool rcu) 2875d226df4SAndreas Gruenbacher { 2885d226df4SAndreas Gruenbacher int error; 2895d226df4SAndreas Gruenbacher 2905d226df4SAndreas Gruenbacher error = __inode_security_revalidate(inode, NULL, !rcu); 2915d226df4SAndreas Gruenbacher if (error) 2925d226df4SAndreas Gruenbacher return ERR_PTR(error); 2935d226df4SAndreas Gruenbacher return inode->i_security; 2945d226df4SAndreas Gruenbacher } 2955d226df4SAndreas Gruenbacher 29683da53c5SAndreas Gruenbacher /* 29783da53c5SAndreas Gruenbacher * Get the security label of an inode. 29883da53c5SAndreas Gruenbacher */ 29983da53c5SAndreas Gruenbacher static struct inode_security_struct *inode_security(struct inode *inode) 30083da53c5SAndreas Gruenbacher { 3015d226df4SAndreas Gruenbacher __inode_security_revalidate(inode, NULL, true); 30283da53c5SAndreas Gruenbacher return inode->i_security; 30383da53c5SAndreas Gruenbacher } 30483da53c5SAndreas Gruenbacher 3052c97165bSPaul Moore static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry) 3062c97165bSPaul Moore { 3072c97165bSPaul Moore struct inode *inode = d_backing_inode(dentry); 3082c97165bSPaul Moore 3092c97165bSPaul Moore return inode->i_security; 3102c97165bSPaul Moore } 3112c97165bSPaul Moore 31283da53c5SAndreas Gruenbacher /* 31383da53c5SAndreas Gruenbacher * Get the security label of a dentry's backing inode. 31483da53c5SAndreas Gruenbacher */ 31583da53c5SAndreas Gruenbacher static struct inode_security_struct *backing_inode_security(struct dentry *dentry) 31683da53c5SAndreas Gruenbacher { 31783da53c5SAndreas Gruenbacher struct inode *inode = d_backing_inode(dentry); 31883da53c5SAndreas Gruenbacher 3195d226df4SAndreas Gruenbacher __inode_security_revalidate(inode, dentry, true); 32083da53c5SAndreas Gruenbacher return inode->i_security; 32183da53c5SAndreas Gruenbacher } 32283da53c5SAndreas Gruenbacher 3233dc91d43SSteven Rostedt static void inode_free_rcu(struct rcu_head *head) 3243dc91d43SSteven Rostedt { 3253dc91d43SSteven Rostedt struct inode_security_struct *isec; 3263dc91d43SSteven Rostedt 3273dc91d43SSteven Rostedt isec = container_of(head, struct inode_security_struct, rcu); 3283dc91d43SSteven Rostedt kmem_cache_free(sel_inode_cache, isec); 3293dc91d43SSteven Rostedt } 3303dc91d43SSteven Rostedt 3311da177e4SLinus Torvalds static void inode_free_security(struct inode *inode) 3321da177e4SLinus Torvalds { 3331da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 3341da177e4SLinus Torvalds struct superblock_security_struct *sbsec = inode->i_sb->s_security; 3351da177e4SLinus Torvalds 3369629d04aSWaiman Long /* 3379629d04aSWaiman Long * As not all inode security structures are in a list, we check for 3389629d04aSWaiman Long * empty list outside of the lock to make sure that we won't waste 3399629d04aSWaiman Long * time taking a lock doing nothing. 3409629d04aSWaiman Long * 3419629d04aSWaiman Long * The list_del_init() function can be safely called more than once. 3429629d04aSWaiman Long * It should not be possible for this function to be called with 3439629d04aSWaiman Long * concurrent list_add(), but for better safety against future changes 3449629d04aSWaiman Long * in the code, we use list_empty_careful() here. 3459629d04aSWaiman Long */ 3469629d04aSWaiman Long if (!list_empty_careful(&isec->list)) { 3471da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 3481da177e4SLinus Torvalds list_del_init(&isec->list); 3491da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 3509629d04aSWaiman Long } 3511da177e4SLinus Torvalds 3523dc91d43SSteven Rostedt /* 3533dc91d43SSteven Rostedt * The inode may still be referenced in a path walk and 3543dc91d43SSteven Rostedt * a call to selinux_inode_permission() can be made 3553dc91d43SSteven Rostedt * after inode_free_security() is called. Ideally, the VFS 3563dc91d43SSteven Rostedt * wouldn't do this, but fixing that is a much harder 3573dc91d43SSteven Rostedt * job. For now, simply free the i_security via RCU, and 3583dc91d43SSteven Rostedt * leave the current inode->i_security pointer intact. 3593dc91d43SSteven Rostedt * The inode will be freed after the RCU grace period too. 3603dc91d43SSteven Rostedt */ 3613dc91d43SSteven Rostedt call_rcu(&isec->rcu, inode_free_rcu); 3621da177e4SLinus Torvalds } 3631da177e4SLinus Torvalds 3641da177e4SLinus Torvalds static int file_alloc_security(struct file *file) 3651da177e4SLinus Torvalds { 3661da177e4SLinus Torvalds struct file_security_struct *fsec; 367275bb41eSDavid Howells u32 sid = current_sid(); 3681da177e4SLinus Torvalds 36963205654SSangwoo fsec = kmem_cache_zalloc(file_security_cache, GFP_KERNEL); 3701da177e4SLinus Torvalds if (!fsec) 3711da177e4SLinus Torvalds return -ENOMEM; 3721da177e4SLinus Torvalds 373275bb41eSDavid Howells fsec->sid = sid; 374275bb41eSDavid Howells fsec->fown_sid = sid; 3751da177e4SLinus Torvalds file->f_security = fsec; 3761da177e4SLinus Torvalds 3771da177e4SLinus Torvalds return 0; 3781da177e4SLinus Torvalds } 3791da177e4SLinus Torvalds 3801da177e4SLinus Torvalds static void file_free_security(struct file *file) 3811da177e4SLinus Torvalds { 3821da177e4SLinus Torvalds struct file_security_struct *fsec = file->f_security; 3831da177e4SLinus Torvalds file->f_security = NULL; 38463205654SSangwoo kmem_cache_free(file_security_cache, fsec); 3851da177e4SLinus Torvalds } 3861da177e4SLinus Torvalds 3871da177e4SLinus Torvalds static int superblock_alloc_security(struct super_block *sb) 3881da177e4SLinus Torvalds { 3891da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 3901da177e4SLinus Torvalds 39189d155efSJames Morris sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL); 3921da177e4SLinus Torvalds if (!sbsec) 3931da177e4SLinus Torvalds return -ENOMEM; 3941da177e4SLinus Torvalds 395bc7e982bSEric Paris mutex_init(&sbsec->lock); 3961da177e4SLinus Torvalds INIT_LIST_HEAD(&sbsec->isec_head); 3971da177e4SLinus Torvalds spin_lock_init(&sbsec->isec_lock); 3981da177e4SLinus Torvalds sbsec->sb = sb; 3991da177e4SLinus Torvalds sbsec->sid = SECINITSID_UNLABELED; 4001da177e4SLinus Torvalds sbsec->def_sid = SECINITSID_FILE; 401c312feb2SEric Paris sbsec->mntpoint_sid = SECINITSID_UNLABELED; 4021da177e4SLinus Torvalds sb->s_security = sbsec; 4031da177e4SLinus Torvalds 4041da177e4SLinus Torvalds return 0; 4051da177e4SLinus Torvalds } 4061da177e4SLinus Torvalds 4071da177e4SLinus Torvalds static void superblock_free_security(struct super_block *sb) 4081da177e4SLinus Torvalds { 4091da177e4SLinus Torvalds struct superblock_security_struct *sbsec = sb->s_security; 4101da177e4SLinus Torvalds sb->s_security = NULL; 4111da177e4SLinus Torvalds kfree(sbsec); 4121da177e4SLinus Torvalds } 4131da177e4SLinus Torvalds 4141da177e4SLinus Torvalds static inline int inode_doinit(struct inode *inode) 4151da177e4SLinus Torvalds { 4161da177e4SLinus Torvalds return inode_doinit_with_dentry(inode, NULL); 4171da177e4SLinus Torvalds } 4181da177e4SLinus Torvalds 4191da177e4SLinus Torvalds enum { 42031e87930SEric Paris Opt_error = -1, 4211da177e4SLinus Torvalds Opt_context = 1, 4221da177e4SLinus Torvalds Opt_fscontext = 2, 423c9180a57SEric Paris Opt_defcontext = 3, 424c9180a57SEric Paris Opt_rootcontext = 4, 42511689d47SDavid P. Quigley Opt_labelsupport = 5, 426d355987fSEric Paris Opt_nextmntopt = 6, 4271da177e4SLinus Torvalds }; 4281da177e4SLinus Torvalds 429d355987fSEric Paris #define NUM_SEL_MNT_OPTS (Opt_nextmntopt - 1) 430d355987fSEric Paris 431a447c093SSteven Whitehouse static const match_table_t tokens = { 432832cbd9aSEric Paris {Opt_context, CONTEXT_STR "%s"}, 433832cbd9aSEric Paris {Opt_fscontext, FSCONTEXT_STR "%s"}, 434832cbd9aSEric Paris {Opt_defcontext, DEFCONTEXT_STR "%s"}, 435832cbd9aSEric Paris {Opt_rootcontext, ROOTCONTEXT_STR "%s"}, 43611689d47SDavid P. Quigley {Opt_labelsupport, LABELSUPP_STR}, 43731e87930SEric Paris {Opt_error, NULL}, 4381da177e4SLinus Torvalds }; 4391da177e4SLinus Torvalds 4401da177e4SLinus Torvalds #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n" 4411da177e4SLinus Torvalds 442c312feb2SEric Paris static int may_context_mount_sb_relabel(u32 sid, 443c312feb2SEric Paris struct superblock_security_struct *sbsec, 444275bb41eSDavid Howells const struct cred *cred) 445c312feb2SEric Paris { 446275bb41eSDavid Howells const struct task_security_struct *tsec = cred->security; 447c312feb2SEric Paris int rc; 448c312feb2SEric Paris 449c312feb2SEric Paris rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 450c312feb2SEric Paris FILESYSTEM__RELABELFROM, NULL); 451c312feb2SEric Paris if (rc) 452c312feb2SEric Paris return rc; 453c312feb2SEric Paris 454c312feb2SEric Paris rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM, 455c312feb2SEric Paris FILESYSTEM__RELABELTO, NULL); 456c312feb2SEric Paris return rc; 457c312feb2SEric Paris } 458c312feb2SEric Paris 4590808925eSEric Paris static int may_context_mount_inode_relabel(u32 sid, 4600808925eSEric Paris struct superblock_security_struct *sbsec, 461275bb41eSDavid Howells const struct cred *cred) 4620808925eSEric Paris { 463275bb41eSDavid Howells const struct task_security_struct *tsec = cred->security; 4640808925eSEric Paris int rc; 4650808925eSEric Paris rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 4660808925eSEric Paris FILESYSTEM__RELABELFROM, NULL); 4670808925eSEric Paris if (rc) 4680808925eSEric Paris return rc; 4690808925eSEric Paris 4700808925eSEric Paris rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, 4710808925eSEric Paris FILESYSTEM__ASSOCIATE, NULL); 4720808925eSEric Paris return rc; 4730808925eSEric Paris } 4740808925eSEric Paris 475b43e725dSEric Paris static int selinux_is_sblabel_mnt(struct super_block *sb) 476b43e725dSEric Paris { 477b43e725dSEric Paris struct superblock_security_struct *sbsec = sb->s_security; 478b43e725dSEric Paris 479d5f3a5f6SMark Salyzyn return sbsec->behavior == SECURITY_FS_USE_XATTR || 480b43e725dSEric Paris sbsec->behavior == SECURITY_FS_USE_TRANS || 481d5f3a5f6SMark Salyzyn sbsec->behavior == SECURITY_FS_USE_TASK || 4829fc2b4b4SJ. Bruce Fields sbsec->behavior == SECURITY_FS_USE_NATIVE || 483d5f3a5f6SMark Salyzyn /* Special handling. Genfs but also in-core setxattr handler */ 484d5f3a5f6SMark Salyzyn !strcmp(sb->s_type->name, "sysfs") || 485d5f3a5f6SMark Salyzyn !strcmp(sb->s_type->name, "pstore") || 486d5f3a5f6SMark Salyzyn !strcmp(sb->s_type->name, "debugfs") || 487a2c7c6fbSYongqin Liu !strcmp(sb->s_type->name, "tracefs") || 4882651225bSStephen Smalley !strcmp(sb->s_type->name, "rootfs") || 4892651225bSStephen Smalley (selinux_policycap_cgroupseclabel && 4902651225bSStephen Smalley (!strcmp(sb->s_type->name, "cgroup") || 4912651225bSStephen Smalley !strcmp(sb->s_type->name, "cgroup2"))); 492b43e725dSEric Paris } 493b43e725dSEric Paris 494c9180a57SEric Paris static int sb_finish_set_opts(struct super_block *sb) 4951da177e4SLinus Torvalds { 4961da177e4SLinus Torvalds struct superblock_security_struct *sbsec = sb->s_security; 4971da177e4SLinus Torvalds struct dentry *root = sb->s_root; 498c6f493d6SDavid Howells struct inode *root_inode = d_backing_inode(root); 4991da177e4SLinus Torvalds int rc = 0; 5001da177e4SLinus Torvalds 5011da177e4SLinus Torvalds if (sbsec->behavior == SECURITY_FS_USE_XATTR) { 5021da177e4SLinus Torvalds /* Make sure that the xattr handler exists and that no 5031da177e4SLinus Torvalds error other than -ENODATA is returned by getxattr on 5041da177e4SLinus Torvalds the root directory. -ENODATA is ok, as this may be 5051da177e4SLinus Torvalds the first boot of the SELinux kernel before we have 5061da177e4SLinus Torvalds assigned xattr values to the filesystem. */ 5075d6c3191SAndreas Gruenbacher if (!(root_inode->i_opflags & IOP_XATTR)) { 50829b1deb2SLinus Torvalds printk(KERN_WARNING "SELinux: (dev %s, type %s) has no " 50929b1deb2SLinus Torvalds "xattr support\n", sb->s_id, sb->s_type->name); 5101da177e4SLinus Torvalds rc = -EOPNOTSUPP; 5111da177e4SLinus Torvalds goto out; 5121da177e4SLinus Torvalds } 5135d6c3191SAndreas Gruenbacher 5145d6c3191SAndreas Gruenbacher rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0); 5151da177e4SLinus Torvalds if (rc < 0 && rc != -ENODATA) { 5161da177e4SLinus Torvalds if (rc == -EOPNOTSUPP) 5171da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: (dev %s, type " 51829b1deb2SLinus Torvalds "%s) has no security xattr handler\n", 51929b1deb2SLinus Torvalds sb->s_id, sb->s_type->name); 5201da177e4SLinus Torvalds else 5211da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: (dev %s, type " 52229b1deb2SLinus Torvalds "%s) getxattr errno %d\n", sb->s_id, 52329b1deb2SLinus Torvalds sb->s_type->name, -rc); 5241da177e4SLinus Torvalds goto out; 5251da177e4SLinus Torvalds } 5261da177e4SLinus Torvalds } 5271da177e4SLinus Torvalds 528eadcabc6SEric Paris sbsec->flags |= SE_SBINITIALIZED; 5290b4d3452SScott Mayhew 5300b4d3452SScott Mayhew /* 5310b4d3452SScott Mayhew * Explicitly set or clear SBLABEL_MNT. It's not sufficient to simply 5320b4d3452SScott Mayhew * leave the flag untouched because sb_clone_mnt_opts might be handing 5330b4d3452SScott Mayhew * us a superblock that needs the flag to be cleared. 5340b4d3452SScott Mayhew */ 535b43e725dSEric Paris if (selinux_is_sblabel_mnt(sb)) 53612f348b9SEric Paris sbsec->flags |= SBLABEL_MNT; 5370b4d3452SScott Mayhew else 5380b4d3452SScott Mayhew sbsec->flags &= ~SBLABEL_MNT; 539ddd29ec6SDavid P. Quigley 5401da177e4SLinus Torvalds /* Initialize the root inode. */ 541c9180a57SEric Paris rc = inode_doinit_with_dentry(root_inode, root); 5421da177e4SLinus Torvalds 5431da177e4SLinus Torvalds /* Initialize any other inodes associated with the superblock, e.g. 5441da177e4SLinus Torvalds inodes created prior to initial policy load or inodes created 5451da177e4SLinus Torvalds during get_sb by a pseudo filesystem that directly 5461da177e4SLinus Torvalds populates itself. */ 5471da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 5481da177e4SLinus Torvalds next_inode: 5491da177e4SLinus Torvalds if (!list_empty(&sbsec->isec_head)) { 5501da177e4SLinus Torvalds struct inode_security_struct *isec = 5511da177e4SLinus Torvalds list_entry(sbsec->isec_head.next, 5521da177e4SLinus Torvalds struct inode_security_struct, list); 5531da177e4SLinus Torvalds struct inode *inode = isec->inode; 554923190d3SStephen Smalley list_del_init(&isec->list); 5551da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 5561da177e4SLinus Torvalds inode = igrab(inode); 5571da177e4SLinus Torvalds if (inode) { 5581da177e4SLinus Torvalds if (!IS_PRIVATE(inode)) 5591da177e4SLinus Torvalds inode_doinit(inode); 5601da177e4SLinus Torvalds iput(inode); 5611da177e4SLinus Torvalds } 5621da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 5631da177e4SLinus Torvalds goto next_inode; 5641da177e4SLinus Torvalds } 5651da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 5661da177e4SLinus Torvalds out: 567c9180a57SEric Paris return rc; 568c9180a57SEric Paris } 569c9180a57SEric Paris 570c9180a57SEric Paris /* 571c9180a57SEric Paris * This function should allow an FS to ask what it's mount security 572c9180a57SEric Paris * options were so it can use those later for submounts, displaying 573c9180a57SEric Paris * mount options, or whatever. 574c9180a57SEric Paris */ 575c9180a57SEric Paris static int selinux_get_mnt_opts(const struct super_block *sb, 576e0007529SEric Paris struct security_mnt_opts *opts) 577c9180a57SEric Paris { 578c9180a57SEric Paris int rc = 0, i; 579c9180a57SEric Paris struct superblock_security_struct *sbsec = sb->s_security; 580c9180a57SEric Paris char *context = NULL; 581c9180a57SEric Paris u32 len; 582c9180a57SEric Paris char tmp; 583c9180a57SEric Paris 584e0007529SEric Paris security_init_mnt_opts(opts); 585c9180a57SEric Paris 5860d90a7ecSDavid P. Quigley if (!(sbsec->flags & SE_SBINITIALIZED)) 587c9180a57SEric Paris return -EINVAL; 588c9180a57SEric Paris 589c9180a57SEric Paris if (!ss_initialized) 590c9180a57SEric Paris return -EINVAL; 591c9180a57SEric Paris 592af8e50ccSEric Paris /* make sure we always check enough bits to cover the mask */ 593af8e50ccSEric Paris BUILD_BUG_ON(SE_MNTMASK >= (1 << NUM_SEL_MNT_OPTS)); 594af8e50ccSEric Paris 5950d90a7ecSDavid P. Quigley tmp = sbsec->flags & SE_MNTMASK; 596c9180a57SEric Paris /* count the number of mount options for this sb */ 597af8e50ccSEric Paris for (i = 0; i < NUM_SEL_MNT_OPTS; i++) { 598c9180a57SEric Paris if (tmp & 0x01) 599e0007529SEric Paris opts->num_mnt_opts++; 600c9180a57SEric Paris tmp >>= 1; 601c9180a57SEric Paris } 60211689d47SDavid P. Quigley /* Check if the Label support flag is set */ 6030b4bdb35SEric Paris if (sbsec->flags & SBLABEL_MNT) 60411689d47SDavid P. Quigley opts->num_mnt_opts++; 605c9180a57SEric Paris 606e0007529SEric Paris opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC); 607e0007529SEric Paris if (!opts->mnt_opts) { 608c9180a57SEric Paris rc = -ENOMEM; 609c9180a57SEric Paris goto out_free; 610c9180a57SEric Paris } 611c9180a57SEric Paris 612e0007529SEric Paris opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC); 613e0007529SEric Paris if (!opts->mnt_opts_flags) { 614c9180a57SEric Paris rc = -ENOMEM; 615c9180a57SEric Paris goto out_free; 616c9180a57SEric Paris } 617c9180a57SEric Paris 618c9180a57SEric Paris i = 0; 619c9180a57SEric Paris if (sbsec->flags & FSCONTEXT_MNT) { 620c9180a57SEric Paris rc = security_sid_to_context(sbsec->sid, &context, &len); 621c9180a57SEric Paris if (rc) 622c9180a57SEric Paris goto out_free; 623e0007529SEric Paris opts->mnt_opts[i] = context; 624e0007529SEric Paris opts->mnt_opts_flags[i++] = FSCONTEXT_MNT; 625c9180a57SEric Paris } 626c9180a57SEric Paris if (sbsec->flags & CONTEXT_MNT) { 627c9180a57SEric Paris rc = security_sid_to_context(sbsec->mntpoint_sid, &context, &len); 628c9180a57SEric Paris if (rc) 629c9180a57SEric Paris goto out_free; 630e0007529SEric Paris opts->mnt_opts[i] = context; 631e0007529SEric Paris opts->mnt_opts_flags[i++] = CONTEXT_MNT; 632c9180a57SEric Paris } 633c9180a57SEric Paris if (sbsec->flags & DEFCONTEXT_MNT) { 634c9180a57SEric Paris rc = security_sid_to_context(sbsec->def_sid, &context, &len); 635c9180a57SEric Paris if (rc) 636c9180a57SEric Paris goto out_free; 637e0007529SEric Paris opts->mnt_opts[i] = context; 638e0007529SEric Paris opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT; 639c9180a57SEric Paris } 640c9180a57SEric Paris if (sbsec->flags & ROOTCONTEXT_MNT) { 64183da53c5SAndreas Gruenbacher struct dentry *root = sbsec->sb->s_root; 64283da53c5SAndreas Gruenbacher struct inode_security_struct *isec = backing_inode_security(root); 643c9180a57SEric Paris 644c9180a57SEric Paris rc = security_sid_to_context(isec->sid, &context, &len); 645c9180a57SEric Paris if (rc) 646c9180a57SEric Paris goto out_free; 647e0007529SEric Paris opts->mnt_opts[i] = context; 648e0007529SEric Paris opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT; 649c9180a57SEric Paris } 65012f348b9SEric Paris if (sbsec->flags & SBLABEL_MNT) { 65111689d47SDavid P. Quigley opts->mnt_opts[i] = NULL; 65212f348b9SEric Paris opts->mnt_opts_flags[i++] = SBLABEL_MNT; 65311689d47SDavid P. Quigley } 654c9180a57SEric Paris 655e0007529SEric Paris BUG_ON(i != opts->num_mnt_opts); 656c9180a57SEric Paris 657c9180a57SEric Paris return 0; 658c9180a57SEric Paris 659c9180a57SEric Paris out_free: 660e0007529SEric Paris security_free_mnt_opts(opts); 661c9180a57SEric Paris return rc; 662c9180a57SEric Paris } 663c9180a57SEric Paris 664c9180a57SEric Paris static int bad_option(struct superblock_security_struct *sbsec, char flag, 665c9180a57SEric Paris u32 old_sid, u32 new_sid) 666c9180a57SEric Paris { 6670d90a7ecSDavid P. Quigley char mnt_flags = sbsec->flags & SE_MNTMASK; 6680d90a7ecSDavid P. Quigley 669c9180a57SEric Paris /* check if the old mount command had the same options */ 6700d90a7ecSDavid P. Quigley if (sbsec->flags & SE_SBINITIALIZED) 671c9180a57SEric Paris if (!(sbsec->flags & flag) || 672c9180a57SEric Paris (old_sid != new_sid)) 673c9180a57SEric Paris return 1; 674c9180a57SEric Paris 675c9180a57SEric Paris /* check if we were passed the same options twice, 676c9180a57SEric Paris * aka someone passed context=a,context=b 677c9180a57SEric Paris */ 6780d90a7ecSDavid P. Quigley if (!(sbsec->flags & SE_SBINITIALIZED)) 6790d90a7ecSDavid P. Quigley if (mnt_flags & flag) 680c9180a57SEric Paris return 1; 681c9180a57SEric Paris return 0; 682c9180a57SEric Paris } 683e0007529SEric Paris 684c9180a57SEric Paris /* 685c9180a57SEric Paris * Allow filesystems with binary mount data to explicitly set mount point 686c9180a57SEric Paris * labeling information. 687c9180a57SEric Paris */ 688e0007529SEric Paris static int selinux_set_mnt_opts(struct super_block *sb, 689649f6e77SDavid Quigley struct security_mnt_opts *opts, 690649f6e77SDavid Quigley unsigned long kern_flags, 691649f6e77SDavid Quigley unsigned long *set_kern_flags) 692c9180a57SEric Paris { 693275bb41eSDavid Howells const struct cred *cred = current_cred(); 694c9180a57SEric Paris int rc = 0, i; 695c9180a57SEric Paris struct superblock_security_struct *sbsec = sb->s_security; 69629b1deb2SLinus Torvalds const char *name = sb->s_type->name; 69783da53c5SAndreas Gruenbacher struct dentry *root = sbsec->sb->s_root; 6982c97165bSPaul Moore struct inode_security_struct *root_isec; 699c9180a57SEric Paris u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0; 700c9180a57SEric Paris u32 defcontext_sid = 0; 701e0007529SEric Paris char **mount_options = opts->mnt_opts; 702e0007529SEric Paris int *flags = opts->mnt_opts_flags; 703e0007529SEric Paris int num_opts = opts->num_mnt_opts; 704c9180a57SEric Paris 705c9180a57SEric Paris mutex_lock(&sbsec->lock); 706c9180a57SEric Paris 707c9180a57SEric Paris if (!ss_initialized) { 708c9180a57SEric Paris if (!num_opts) { 709c9180a57SEric Paris /* Defer initialization until selinux_complete_init, 710c9180a57SEric Paris after the initial policy is loaded and the security 711c9180a57SEric Paris server is ready to handle calls. */ 712c9180a57SEric Paris goto out; 713c9180a57SEric Paris } 714c9180a57SEric Paris rc = -EINVAL; 715744ba35eSEric Paris printk(KERN_WARNING "SELinux: Unable to set superblock options " 716744ba35eSEric Paris "before the security server is initialized\n"); 717c9180a57SEric Paris goto out; 718c9180a57SEric Paris } 719649f6e77SDavid Quigley if (kern_flags && !set_kern_flags) { 720649f6e77SDavid Quigley /* Specifying internal flags without providing a place to 721649f6e77SDavid Quigley * place the results is not allowed */ 722649f6e77SDavid Quigley rc = -EINVAL; 723649f6e77SDavid Quigley goto out; 724649f6e77SDavid Quigley } 725c9180a57SEric Paris 726c9180a57SEric Paris /* 727e0007529SEric Paris * Binary mount data FS will come through this function twice. Once 728e0007529SEric Paris * from an explicit call and once from the generic calls from the vfs. 729e0007529SEric Paris * Since the generic VFS calls will not contain any security mount data 730e0007529SEric Paris * we need to skip the double mount verification. 731e0007529SEric Paris * 732e0007529SEric Paris * This does open a hole in which we will not notice if the first 733e0007529SEric Paris * mount using this sb set explict options and a second mount using 734e0007529SEric Paris * this sb does not set any security options. (The first options 735e0007529SEric Paris * will be used for both mounts) 736e0007529SEric Paris */ 7370d90a7ecSDavid P. Quigley if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA) 738e0007529SEric Paris && (num_opts == 0)) 739e0007529SEric Paris goto out; 740e0007529SEric Paris 7412c97165bSPaul Moore root_isec = backing_inode_security_novalidate(root); 7422c97165bSPaul Moore 743e0007529SEric Paris /* 744c9180a57SEric Paris * parse the mount options, check if they are valid sids. 745c9180a57SEric Paris * also check if someone is trying to mount the same sb more 746c9180a57SEric Paris * than once with different security options. 747c9180a57SEric Paris */ 748c9180a57SEric Paris for (i = 0; i < num_opts; i++) { 749c9180a57SEric Paris u32 sid; 75011689d47SDavid P. Quigley 75112f348b9SEric Paris if (flags[i] == SBLABEL_MNT) 75211689d47SDavid P. Quigley continue; 75344be2f65SRasmus Villemoes rc = security_context_str_to_sid(mount_options[i], &sid, GFP_KERNEL); 754c9180a57SEric Paris if (rc) { 75544be2f65SRasmus Villemoes printk(KERN_WARNING "SELinux: security_context_str_to_sid" 75629b1deb2SLinus Torvalds "(%s) failed for (dev %s, type %s) errno=%d\n", 75729b1deb2SLinus Torvalds mount_options[i], sb->s_id, name, rc); 758c9180a57SEric Paris goto out; 759c9180a57SEric Paris } 760c9180a57SEric Paris switch (flags[i]) { 761c9180a57SEric Paris case FSCONTEXT_MNT: 762c9180a57SEric Paris fscontext_sid = sid; 763c9180a57SEric Paris 764c9180a57SEric Paris if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, 765c9180a57SEric Paris fscontext_sid)) 766c9180a57SEric Paris goto out_double_mount; 767c9180a57SEric Paris 768c9180a57SEric Paris sbsec->flags |= FSCONTEXT_MNT; 769c9180a57SEric Paris break; 770c9180a57SEric Paris case CONTEXT_MNT: 771c9180a57SEric Paris context_sid = sid; 772c9180a57SEric Paris 773c9180a57SEric Paris if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, 774c9180a57SEric Paris context_sid)) 775c9180a57SEric Paris goto out_double_mount; 776c9180a57SEric Paris 777c9180a57SEric Paris sbsec->flags |= CONTEXT_MNT; 778c9180a57SEric Paris break; 779c9180a57SEric Paris case ROOTCONTEXT_MNT: 780c9180a57SEric Paris rootcontext_sid = sid; 781c9180a57SEric Paris 782c9180a57SEric Paris if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, 783c9180a57SEric Paris rootcontext_sid)) 784c9180a57SEric Paris goto out_double_mount; 785c9180a57SEric Paris 786c9180a57SEric Paris sbsec->flags |= ROOTCONTEXT_MNT; 787c9180a57SEric Paris 788c9180a57SEric Paris break; 789c9180a57SEric Paris case DEFCONTEXT_MNT: 790c9180a57SEric Paris defcontext_sid = sid; 791c9180a57SEric Paris 792c9180a57SEric Paris if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, 793c9180a57SEric Paris defcontext_sid)) 794c9180a57SEric Paris goto out_double_mount; 795c9180a57SEric Paris 796c9180a57SEric Paris sbsec->flags |= DEFCONTEXT_MNT; 797c9180a57SEric Paris 798c9180a57SEric Paris break; 799c9180a57SEric Paris default: 800c9180a57SEric Paris rc = -EINVAL; 801c9180a57SEric Paris goto out; 802c9180a57SEric Paris } 803c9180a57SEric Paris } 804c9180a57SEric Paris 8050d90a7ecSDavid P. Quigley if (sbsec->flags & SE_SBINITIALIZED) { 806c9180a57SEric Paris /* previously mounted with options, but not on this attempt? */ 8070d90a7ecSDavid P. Quigley if ((sbsec->flags & SE_MNTMASK) && !num_opts) 808c9180a57SEric Paris goto out_double_mount; 809c9180a57SEric Paris rc = 0; 810c9180a57SEric Paris goto out; 811c9180a57SEric Paris } 812c9180a57SEric Paris 813089be43eSJames Morris if (strcmp(sb->s_type->name, "proc") == 0) 814134509d5SStephen Smalley sbsec->flags |= SE_SBPROC | SE_SBGENFS; 815134509d5SStephen Smalley 8168e014720SStephen Smalley if (!strcmp(sb->s_type->name, "debugfs") || 8176a391183SJeff Vander Stoep !strcmp(sb->s_type->name, "tracefs") || 8188e014720SStephen Smalley !strcmp(sb->s_type->name, "sysfs") || 819901ef845SAntonio Murdaca !strcmp(sb->s_type->name, "pstore") || 820901ef845SAntonio Murdaca !strcmp(sb->s_type->name, "cgroup") || 821901ef845SAntonio Murdaca !strcmp(sb->s_type->name, "cgroup2")) 822134509d5SStephen Smalley sbsec->flags |= SE_SBGENFS; 823c9180a57SEric Paris 824eb9ae686SDavid Quigley if (!sbsec->behavior) { 825eb9ae686SDavid Quigley /* 826eb9ae686SDavid Quigley * Determine the labeling behavior to use for this 827eb9ae686SDavid Quigley * filesystem type. 828eb9ae686SDavid Quigley */ 829a64c54cfSEric Paris rc = security_fs_use(sb); 830c9180a57SEric Paris if (rc) { 831eb9ae686SDavid Quigley printk(KERN_WARNING 832eb9ae686SDavid Quigley "%s: security_fs_use(%s) returned %d\n", 833089be43eSJames Morris __func__, sb->s_type->name, rc); 834c9180a57SEric Paris goto out; 835c9180a57SEric Paris } 836eb9ae686SDavid Quigley } 837aad82892SSeth Forshee 838aad82892SSeth Forshee /* 83901593d32SStephen Smalley * If this is a user namespace mount and the filesystem type is not 84001593d32SStephen Smalley * explicitly whitelisted, then no contexts are allowed on the command 84101593d32SStephen Smalley * line and security labels must be ignored. 842aad82892SSeth Forshee */ 84301593d32SStephen Smalley if (sb->s_user_ns != &init_user_ns && 84401593d32SStephen Smalley strcmp(sb->s_type->name, "tmpfs") && 84501593d32SStephen Smalley strcmp(sb->s_type->name, "ramfs") && 84601593d32SStephen Smalley strcmp(sb->s_type->name, "devpts")) { 847aad82892SSeth Forshee if (context_sid || fscontext_sid || rootcontext_sid || 848aad82892SSeth Forshee defcontext_sid) { 849aad82892SSeth Forshee rc = -EACCES; 850aad82892SSeth Forshee goto out; 851aad82892SSeth Forshee } 852aad82892SSeth Forshee if (sbsec->behavior == SECURITY_FS_USE_XATTR) { 853aad82892SSeth Forshee sbsec->behavior = SECURITY_FS_USE_MNTPOINT; 854aad82892SSeth Forshee rc = security_transition_sid(current_sid(), current_sid(), 855aad82892SSeth Forshee SECCLASS_FILE, NULL, 856aad82892SSeth Forshee &sbsec->mntpoint_sid); 857aad82892SSeth Forshee if (rc) 858aad82892SSeth Forshee goto out; 859aad82892SSeth Forshee } 860aad82892SSeth Forshee goto out_set_opts; 861aad82892SSeth Forshee } 862aad82892SSeth Forshee 863c9180a57SEric Paris /* sets the context of the superblock for the fs being mounted. */ 864c9180a57SEric Paris if (fscontext_sid) { 865275bb41eSDavid Howells rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred); 866c9180a57SEric Paris if (rc) 867c9180a57SEric Paris goto out; 868c9180a57SEric Paris 869c9180a57SEric Paris sbsec->sid = fscontext_sid; 870c9180a57SEric Paris } 871c9180a57SEric Paris 872c9180a57SEric Paris /* 873c9180a57SEric Paris * Switch to using mount point labeling behavior. 874c9180a57SEric Paris * sets the label used on all file below the mountpoint, and will set 875c9180a57SEric Paris * the superblock context if not already set. 876c9180a57SEric Paris */ 877eb9ae686SDavid Quigley if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) { 878eb9ae686SDavid Quigley sbsec->behavior = SECURITY_FS_USE_NATIVE; 879eb9ae686SDavid Quigley *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS; 880eb9ae686SDavid Quigley } 881eb9ae686SDavid Quigley 882c9180a57SEric Paris if (context_sid) { 883c9180a57SEric Paris if (!fscontext_sid) { 884275bb41eSDavid Howells rc = may_context_mount_sb_relabel(context_sid, sbsec, 885275bb41eSDavid Howells cred); 886c9180a57SEric Paris if (rc) 887c9180a57SEric Paris goto out; 888c9180a57SEric Paris sbsec->sid = context_sid; 889c9180a57SEric Paris } else { 890275bb41eSDavid Howells rc = may_context_mount_inode_relabel(context_sid, sbsec, 891275bb41eSDavid Howells cred); 892c9180a57SEric Paris if (rc) 893c9180a57SEric Paris goto out; 894c9180a57SEric Paris } 895c9180a57SEric Paris if (!rootcontext_sid) 896c9180a57SEric Paris rootcontext_sid = context_sid; 897c9180a57SEric Paris 898c9180a57SEric Paris sbsec->mntpoint_sid = context_sid; 899c9180a57SEric Paris sbsec->behavior = SECURITY_FS_USE_MNTPOINT; 900c9180a57SEric Paris } 901c9180a57SEric Paris 902c9180a57SEric Paris if (rootcontext_sid) { 903275bb41eSDavid Howells rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec, 904275bb41eSDavid Howells cred); 905c9180a57SEric Paris if (rc) 906c9180a57SEric Paris goto out; 907c9180a57SEric Paris 908c9180a57SEric Paris root_isec->sid = rootcontext_sid; 9096f3be9f5SAndreas Gruenbacher root_isec->initialized = LABEL_INITIALIZED; 910c9180a57SEric Paris } 911c9180a57SEric Paris 912c9180a57SEric Paris if (defcontext_sid) { 913eb9ae686SDavid Quigley if (sbsec->behavior != SECURITY_FS_USE_XATTR && 914eb9ae686SDavid Quigley sbsec->behavior != SECURITY_FS_USE_NATIVE) { 915c9180a57SEric Paris rc = -EINVAL; 916c9180a57SEric Paris printk(KERN_WARNING "SELinux: defcontext option is " 917c9180a57SEric Paris "invalid for this filesystem type\n"); 918c9180a57SEric Paris goto out; 919c9180a57SEric Paris } 920c9180a57SEric Paris 921c9180a57SEric Paris if (defcontext_sid != sbsec->def_sid) { 922c9180a57SEric Paris rc = may_context_mount_inode_relabel(defcontext_sid, 923275bb41eSDavid Howells sbsec, cred); 924c9180a57SEric Paris if (rc) 925c9180a57SEric Paris goto out; 926c9180a57SEric Paris } 927c9180a57SEric Paris 928c9180a57SEric Paris sbsec->def_sid = defcontext_sid; 929c9180a57SEric Paris } 930c9180a57SEric Paris 931aad82892SSeth Forshee out_set_opts: 932c9180a57SEric Paris rc = sb_finish_set_opts(sb); 933c9180a57SEric Paris out: 934bc7e982bSEric Paris mutex_unlock(&sbsec->lock); 9351da177e4SLinus Torvalds return rc; 936c9180a57SEric Paris out_double_mount: 937c9180a57SEric Paris rc = -EINVAL; 938c9180a57SEric Paris printk(KERN_WARNING "SELinux: mount invalid. Same superblock, different " 93929b1deb2SLinus Torvalds "security settings for (dev %s, type %s)\n", sb->s_id, name); 940c9180a57SEric Paris goto out; 941c9180a57SEric Paris } 942c9180a57SEric Paris 943094f7b69SJeff Layton static int selinux_cmp_sb_context(const struct super_block *oldsb, 944094f7b69SJeff Layton const struct super_block *newsb) 945094f7b69SJeff Layton { 946094f7b69SJeff Layton struct superblock_security_struct *old = oldsb->s_security; 947094f7b69SJeff Layton struct superblock_security_struct *new = newsb->s_security; 948094f7b69SJeff Layton char oldflags = old->flags & SE_MNTMASK; 949094f7b69SJeff Layton char newflags = new->flags & SE_MNTMASK; 950094f7b69SJeff Layton 951094f7b69SJeff Layton if (oldflags != newflags) 952094f7b69SJeff Layton goto mismatch; 953094f7b69SJeff Layton if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid) 954094f7b69SJeff Layton goto mismatch; 955094f7b69SJeff Layton if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid) 956094f7b69SJeff Layton goto mismatch; 957094f7b69SJeff Layton if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid) 958094f7b69SJeff Layton goto mismatch; 959094f7b69SJeff Layton if (oldflags & ROOTCONTEXT_MNT) { 96083da53c5SAndreas Gruenbacher struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root); 96183da53c5SAndreas Gruenbacher struct inode_security_struct *newroot = backing_inode_security(newsb->s_root); 962094f7b69SJeff Layton if (oldroot->sid != newroot->sid) 963094f7b69SJeff Layton goto mismatch; 964094f7b69SJeff Layton } 965094f7b69SJeff Layton return 0; 966094f7b69SJeff Layton mismatch: 967094f7b69SJeff Layton printk(KERN_WARNING "SELinux: mount invalid. Same superblock, " 968094f7b69SJeff Layton "different security settings for (dev %s, " 969094f7b69SJeff Layton "type %s)\n", newsb->s_id, newsb->s_type->name); 970094f7b69SJeff Layton return -EBUSY; 971094f7b69SJeff Layton } 972094f7b69SJeff Layton 973094f7b69SJeff Layton static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb, 9740b4d3452SScott Mayhew struct super_block *newsb, 9750b4d3452SScott Mayhew unsigned long kern_flags, 9760b4d3452SScott Mayhew unsigned long *set_kern_flags) 977c9180a57SEric Paris { 9780b4d3452SScott Mayhew int rc = 0; 979c9180a57SEric Paris const struct superblock_security_struct *oldsbsec = oldsb->s_security; 980c9180a57SEric Paris struct superblock_security_struct *newsbsec = newsb->s_security; 981c9180a57SEric Paris 982c9180a57SEric Paris int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT); 983c9180a57SEric Paris int set_context = (oldsbsec->flags & CONTEXT_MNT); 984c9180a57SEric Paris int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT); 985c9180a57SEric Paris 9860f5e6420SEric Paris /* 9870f5e6420SEric Paris * if the parent was able to be mounted it clearly had no special lsm 988e8c26255SAl Viro * mount options. thus we can safely deal with this superblock later 9890f5e6420SEric Paris */ 990e8c26255SAl Viro if (!ss_initialized) 991094f7b69SJeff Layton return 0; 992c9180a57SEric Paris 9930b4d3452SScott Mayhew /* 9940b4d3452SScott Mayhew * Specifying internal flags without providing a place to 9950b4d3452SScott Mayhew * place the results is not allowed. 9960b4d3452SScott Mayhew */ 9970b4d3452SScott Mayhew if (kern_flags && !set_kern_flags) 9980b4d3452SScott Mayhew return -EINVAL; 9990b4d3452SScott Mayhew 1000c9180a57SEric Paris /* how can we clone if the old one wasn't set up?? */ 10010d90a7ecSDavid P. Quigley BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED)); 1002c9180a57SEric Paris 1003094f7b69SJeff Layton /* if fs is reusing a sb, make sure that the contexts match */ 10040d90a7ecSDavid P. Quigley if (newsbsec->flags & SE_SBINITIALIZED) 1005094f7b69SJeff Layton return selinux_cmp_sb_context(oldsb, newsb); 10065a552617SEric Paris 1007c9180a57SEric Paris mutex_lock(&newsbsec->lock); 1008c9180a57SEric Paris 1009c9180a57SEric Paris newsbsec->flags = oldsbsec->flags; 1010c9180a57SEric Paris 1011c9180a57SEric Paris newsbsec->sid = oldsbsec->sid; 1012c9180a57SEric Paris newsbsec->def_sid = oldsbsec->def_sid; 1013c9180a57SEric Paris newsbsec->behavior = oldsbsec->behavior; 1014c9180a57SEric Paris 10150b4d3452SScott Mayhew if (newsbsec->behavior == SECURITY_FS_USE_NATIVE && 10160b4d3452SScott Mayhew !(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) { 10170b4d3452SScott Mayhew rc = security_fs_use(newsb); 10180b4d3452SScott Mayhew if (rc) 10190b4d3452SScott Mayhew goto out; 10200b4d3452SScott Mayhew } 10210b4d3452SScott Mayhew 10220b4d3452SScott Mayhew if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) { 10230b4d3452SScott Mayhew newsbsec->behavior = SECURITY_FS_USE_NATIVE; 10240b4d3452SScott Mayhew *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS; 10250b4d3452SScott Mayhew } 10260b4d3452SScott Mayhew 1027c9180a57SEric Paris if (set_context) { 1028c9180a57SEric Paris u32 sid = oldsbsec->mntpoint_sid; 1029c9180a57SEric Paris 1030c9180a57SEric Paris if (!set_fscontext) 1031c9180a57SEric Paris newsbsec->sid = sid; 1032c9180a57SEric Paris if (!set_rootcontext) { 103383da53c5SAndreas Gruenbacher struct inode_security_struct *newisec = backing_inode_security(newsb->s_root); 1034c9180a57SEric Paris newisec->sid = sid; 1035c9180a57SEric Paris } 1036c9180a57SEric Paris newsbsec->mntpoint_sid = sid; 1037c9180a57SEric Paris } 1038c9180a57SEric Paris if (set_rootcontext) { 103983da53c5SAndreas Gruenbacher const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root); 104083da53c5SAndreas Gruenbacher struct inode_security_struct *newisec = backing_inode_security(newsb->s_root); 1041c9180a57SEric Paris 1042c9180a57SEric Paris newisec->sid = oldisec->sid; 1043c9180a57SEric Paris } 1044c9180a57SEric Paris 1045c9180a57SEric Paris sb_finish_set_opts(newsb); 10460b4d3452SScott Mayhew out: 1047c9180a57SEric Paris mutex_unlock(&newsbsec->lock); 10480b4d3452SScott Mayhew return rc; 1049c9180a57SEric Paris } 1050c9180a57SEric Paris 10512e1479d9SAdrian Bunk static int selinux_parse_opts_str(char *options, 10522e1479d9SAdrian Bunk struct security_mnt_opts *opts) 1053c9180a57SEric Paris { 1054e0007529SEric Paris char *p; 1055c9180a57SEric Paris char *context = NULL, *defcontext = NULL; 1056c9180a57SEric Paris char *fscontext = NULL, *rootcontext = NULL; 1057e0007529SEric Paris int rc, num_mnt_opts = 0; 1058c9180a57SEric Paris 1059e0007529SEric Paris opts->num_mnt_opts = 0; 1060c9180a57SEric Paris 1061c9180a57SEric Paris /* Standard string-based options. */ 1062c9180a57SEric Paris while ((p = strsep(&options, "|")) != NULL) { 1063c9180a57SEric Paris int token; 1064c9180a57SEric Paris substring_t args[MAX_OPT_ARGS]; 1065c9180a57SEric Paris 1066c9180a57SEric Paris if (!*p) 1067c9180a57SEric Paris continue; 1068c9180a57SEric Paris 1069c9180a57SEric Paris token = match_token(p, tokens, args); 1070c9180a57SEric Paris 1071c9180a57SEric Paris switch (token) { 1072c9180a57SEric Paris case Opt_context: 1073c9180a57SEric Paris if (context || defcontext) { 1074c9180a57SEric Paris rc = -EINVAL; 1075c9180a57SEric Paris printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 1076c9180a57SEric Paris goto out_err; 1077c9180a57SEric Paris } 1078c9180a57SEric Paris context = match_strdup(&args[0]); 1079c9180a57SEric Paris if (!context) { 1080c9180a57SEric Paris rc = -ENOMEM; 1081c9180a57SEric Paris goto out_err; 1082c9180a57SEric Paris } 1083c9180a57SEric Paris break; 1084c9180a57SEric Paris 1085c9180a57SEric Paris case Opt_fscontext: 1086c9180a57SEric Paris if (fscontext) { 1087c9180a57SEric Paris rc = -EINVAL; 1088c9180a57SEric Paris printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 1089c9180a57SEric Paris goto out_err; 1090c9180a57SEric Paris } 1091c9180a57SEric Paris fscontext = match_strdup(&args[0]); 1092c9180a57SEric Paris if (!fscontext) { 1093c9180a57SEric Paris rc = -ENOMEM; 1094c9180a57SEric Paris goto out_err; 1095c9180a57SEric Paris } 1096c9180a57SEric Paris break; 1097c9180a57SEric Paris 1098c9180a57SEric Paris case Opt_rootcontext: 1099c9180a57SEric Paris if (rootcontext) { 1100c9180a57SEric Paris rc = -EINVAL; 1101c9180a57SEric Paris printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 1102c9180a57SEric Paris goto out_err; 1103c9180a57SEric Paris } 1104c9180a57SEric Paris rootcontext = match_strdup(&args[0]); 1105c9180a57SEric Paris if (!rootcontext) { 1106c9180a57SEric Paris rc = -ENOMEM; 1107c9180a57SEric Paris goto out_err; 1108c9180a57SEric Paris } 1109c9180a57SEric Paris break; 1110c9180a57SEric Paris 1111c9180a57SEric Paris case Opt_defcontext: 1112c9180a57SEric Paris if (context || defcontext) { 1113c9180a57SEric Paris rc = -EINVAL; 1114c9180a57SEric Paris printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 1115c9180a57SEric Paris goto out_err; 1116c9180a57SEric Paris } 1117c9180a57SEric Paris defcontext = match_strdup(&args[0]); 1118c9180a57SEric Paris if (!defcontext) { 1119c9180a57SEric Paris rc = -ENOMEM; 1120c9180a57SEric Paris goto out_err; 1121c9180a57SEric Paris } 1122c9180a57SEric Paris break; 112311689d47SDavid P. Quigley case Opt_labelsupport: 112411689d47SDavid P. Quigley break; 1125c9180a57SEric Paris default: 1126c9180a57SEric Paris rc = -EINVAL; 1127c9180a57SEric Paris printk(KERN_WARNING "SELinux: unknown mount option\n"); 1128c9180a57SEric Paris goto out_err; 1129c9180a57SEric Paris 1130c9180a57SEric Paris } 1131c9180a57SEric Paris } 1132c9180a57SEric Paris 1133e0007529SEric Paris rc = -ENOMEM; 11348931c3bdSTetsuo Handa opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_KERNEL); 1135e0007529SEric Paris if (!opts->mnt_opts) 1136e0007529SEric Paris goto out_err; 1137e0007529SEric Paris 11388931c3bdSTetsuo Handa opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), 11398931c3bdSTetsuo Handa GFP_KERNEL); 1140023f108dSPaul Moore if (!opts->mnt_opts_flags) 1141e0007529SEric Paris goto out_err; 1142c9180a57SEric Paris 1143e0007529SEric Paris if (fscontext) { 1144e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = fscontext; 1145e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT; 1146e0007529SEric Paris } 1147e0007529SEric Paris if (context) { 1148e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = context; 1149e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT; 1150e0007529SEric Paris } 1151e0007529SEric Paris if (rootcontext) { 1152e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = rootcontext; 1153e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT; 1154e0007529SEric Paris } 1155e0007529SEric Paris if (defcontext) { 1156e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = defcontext; 1157e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT; 1158e0007529SEric Paris } 1159e0007529SEric Paris 1160e0007529SEric Paris opts->num_mnt_opts = num_mnt_opts; 1161e0007529SEric Paris return 0; 1162e0007529SEric Paris 1163c9180a57SEric Paris out_err: 1164023f108dSPaul Moore security_free_mnt_opts(opts); 1165c9180a57SEric Paris kfree(context); 1166c9180a57SEric Paris kfree(defcontext); 1167c9180a57SEric Paris kfree(fscontext); 1168c9180a57SEric Paris kfree(rootcontext); 1169c9180a57SEric Paris return rc; 11701da177e4SLinus Torvalds } 1171e0007529SEric Paris /* 1172e0007529SEric Paris * string mount options parsing and call set the sbsec 1173e0007529SEric Paris */ 1174e0007529SEric Paris static int superblock_doinit(struct super_block *sb, void *data) 1175e0007529SEric Paris { 1176e0007529SEric Paris int rc = 0; 1177e0007529SEric Paris char *options = data; 1178e0007529SEric Paris struct security_mnt_opts opts; 1179e0007529SEric Paris 1180e0007529SEric Paris security_init_mnt_opts(&opts); 1181e0007529SEric Paris 1182e0007529SEric Paris if (!data) 1183e0007529SEric Paris goto out; 1184e0007529SEric Paris 1185e0007529SEric Paris BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA); 1186e0007529SEric Paris 1187e0007529SEric Paris rc = selinux_parse_opts_str(options, &opts); 1188e0007529SEric Paris if (rc) 1189e0007529SEric Paris goto out_err; 1190e0007529SEric Paris 1191e0007529SEric Paris out: 1192649f6e77SDavid Quigley rc = selinux_set_mnt_opts(sb, &opts, 0, NULL); 1193e0007529SEric Paris 1194e0007529SEric Paris out_err: 1195e0007529SEric Paris security_free_mnt_opts(&opts); 1196e0007529SEric Paris return rc; 1197e0007529SEric Paris } 11981da177e4SLinus Torvalds 11993583a711SAdrian Bunk static void selinux_write_opts(struct seq_file *m, 12003583a711SAdrian Bunk struct security_mnt_opts *opts) 12012069f457SEric Paris { 12022069f457SEric Paris int i; 12032069f457SEric Paris char *prefix; 12042069f457SEric Paris 12052069f457SEric Paris for (i = 0; i < opts->num_mnt_opts; i++) { 120611689d47SDavid P. Quigley char *has_comma; 120711689d47SDavid P. Quigley 120811689d47SDavid P. Quigley if (opts->mnt_opts[i]) 120911689d47SDavid P. Quigley has_comma = strchr(opts->mnt_opts[i], ','); 121011689d47SDavid P. Quigley else 121111689d47SDavid P. Quigley has_comma = NULL; 12122069f457SEric Paris 12132069f457SEric Paris switch (opts->mnt_opts_flags[i]) { 12142069f457SEric Paris case CONTEXT_MNT: 12152069f457SEric Paris prefix = CONTEXT_STR; 12162069f457SEric Paris break; 12172069f457SEric Paris case FSCONTEXT_MNT: 12182069f457SEric Paris prefix = FSCONTEXT_STR; 12192069f457SEric Paris break; 12202069f457SEric Paris case ROOTCONTEXT_MNT: 12212069f457SEric Paris prefix = ROOTCONTEXT_STR; 12222069f457SEric Paris break; 12232069f457SEric Paris case DEFCONTEXT_MNT: 12242069f457SEric Paris prefix = DEFCONTEXT_STR; 12252069f457SEric Paris break; 122612f348b9SEric Paris case SBLABEL_MNT: 122711689d47SDavid P. Quigley seq_putc(m, ','); 122811689d47SDavid P. Quigley seq_puts(m, LABELSUPP_STR); 122911689d47SDavid P. Quigley continue; 12302069f457SEric Paris default: 12312069f457SEric Paris BUG(); 1232a35c6c83SEric Paris return; 12332069f457SEric Paris }; 12342069f457SEric Paris /* we need a comma before each option */ 12352069f457SEric Paris seq_putc(m, ','); 12362069f457SEric Paris seq_puts(m, prefix); 12372069f457SEric Paris if (has_comma) 12382069f457SEric Paris seq_putc(m, '\"'); 1239a068acf2SKees Cook seq_escape(m, opts->mnt_opts[i], "\"\n\\"); 12402069f457SEric Paris if (has_comma) 12412069f457SEric Paris seq_putc(m, '\"'); 12422069f457SEric Paris } 12432069f457SEric Paris } 12442069f457SEric Paris 12452069f457SEric Paris static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb) 12462069f457SEric Paris { 12472069f457SEric Paris struct security_mnt_opts opts; 12482069f457SEric Paris int rc; 12492069f457SEric Paris 12502069f457SEric Paris rc = selinux_get_mnt_opts(sb, &opts); 1251383795c2SEric Paris if (rc) { 1252383795c2SEric Paris /* before policy load we may get EINVAL, don't show anything */ 1253383795c2SEric Paris if (rc == -EINVAL) 1254383795c2SEric Paris rc = 0; 12552069f457SEric Paris return rc; 1256383795c2SEric Paris } 12572069f457SEric Paris 12582069f457SEric Paris selinux_write_opts(m, &opts); 12592069f457SEric Paris 12602069f457SEric Paris security_free_mnt_opts(&opts); 12612069f457SEric Paris 12622069f457SEric Paris return rc; 12632069f457SEric Paris } 12642069f457SEric Paris 12651da177e4SLinus Torvalds static inline u16 inode_mode_to_security_class(umode_t mode) 12661da177e4SLinus Torvalds { 12671da177e4SLinus Torvalds switch (mode & S_IFMT) { 12681da177e4SLinus Torvalds case S_IFSOCK: 12691da177e4SLinus Torvalds return SECCLASS_SOCK_FILE; 12701da177e4SLinus Torvalds case S_IFLNK: 12711da177e4SLinus Torvalds return SECCLASS_LNK_FILE; 12721da177e4SLinus Torvalds case S_IFREG: 12731da177e4SLinus Torvalds return SECCLASS_FILE; 12741da177e4SLinus Torvalds case S_IFBLK: 12751da177e4SLinus Torvalds return SECCLASS_BLK_FILE; 12761da177e4SLinus Torvalds case S_IFDIR: 12771da177e4SLinus Torvalds return SECCLASS_DIR; 12781da177e4SLinus Torvalds case S_IFCHR: 12791da177e4SLinus Torvalds return SECCLASS_CHR_FILE; 12801da177e4SLinus Torvalds case S_IFIFO: 12811da177e4SLinus Torvalds return SECCLASS_FIFO_FILE; 12821da177e4SLinus Torvalds 12831da177e4SLinus Torvalds } 12841da177e4SLinus Torvalds 12851da177e4SLinus Torvalds return SECCLASS_FILE; 12861da177e4SLinus Torvalds } 12871da177e4SLinus Torvalds 128813402580SJames Morris static inline int default_protocol_stream(int protocol) 128913402580SJames Morris { 129013402580SJames Morris return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP); 129113402580SJames Morris } 129213402580SJames Morris 129313402580SJames Morris static inline int default_protocol_dgram(int protocol) 129413402580SJames Morris { 129513402580SJames Morris return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP); 129613402580SJames Morris } 129713402580SJames Morris 12981da177e4SLinus Torvalds static inline u16 socket_type_to_security_class(int family, int type, int protocol) 12991da177e4SLinus Torvalds { 1300da69a530SStephen Smalley int extsockclass = selinux_policycap_extsockclass; 1301da69a530SStephen Smalley 13021da177e4SLinus Torvalds switch (family) { 13031da177e4SLinus Torvalds case PF_UNIX: 13041da177e4SLinus Torvalds switch (type) { 13051da177e4SLinus Torvalds case SOCK_STREAM: 13061da177e4SLinus Torvalds case SOCK_SEQPACKET: 13071da177e4SLinus Torvalds return SECCLASS_UNIX_STREAM_SOCKET; 13081da177e4SLinus Torvalds case SOCK_DGRAM: 13092a764b52SLuis Ressel case SOCK_RAW: 13101da177e4SLinus Torvalds return SECCLASS_UNIX_DGRAM_SOCKET; 13111da177e4SLinus Torvalds } 13121da177e4SLinus Torvalds break; 13131da177e4SLinus Torvalds case PF_INET: 13141da177e4SLinus Torvalds case PF_INET6: 13151da177e4SLinus Torvalds switch (type) { 13161da177e4SLinus Torvalds case SOCK_STREAM: 1317da69a530SStephen Smalley case SOCK_SEQPACKET: 131813402580SJames Morris if (default_protocol_stream(protocol)) 13191da177e4SLinus Torvalds return SECCLASS_TCP_SOCKET; 1320da69a530SStephen Smalley else if (extsockclass && protocol == IPPROTO_SCTP) 1321da69a530SStephen Smalley return SECCLASS_SCTP_SOCKET; 132213402580SJames Morris else 132313402580SJames Morris return SECCLASS_RAWIP_SOCKET; 13241da177e4SLinus Torvalds case SOCK_DGRAM: 132513402580SJames Morris if (default_protocol_dgram(protocol)) 13261da177e4SLinus Torvalds return SECCLASS_UDP_SOCKET; 1327ef37979aSStephen Smalley else if (extsockclass && (protocol == IPPROTO_ICMP || 1328ef37979aSStephen Smalley protocol == IPPROTO_ICMPV6)) 1329da69a530SStephen Smalley return SECCLASS_ICMP_SOCKET; 133013402580SJames Morris else 133113402580SJames Morris return SECCLASS_RAWIP_SOCKET; 13322ee92d46SJames Morris case SOCK_DCCP: 13332ee92d46SJames Morris return SECCLASS_DCCP_SOCKET; 133413402580SJames Morris default: 13351da177e4SLinus Torvalds return SECCLASS_RAWIP_SOCKET; 13361da177e4SLinus Torvalds } 13371da177e4SLinus Torvalds break; 13381da177e4SLinus Torvalds case PF_NETLINK: 13391da177e4SLinus Torvalds switch (protocol) { 13401da177e4SLinus Torvalds case NETLINK_ROUTE: 13411da177e4SLinus Torvalds return SECCLASS_NETLINK_ROUTE_SOCKET; 13427f1fb60cSPavel Emelyanov case NETLINK_SOCK_DIAG: 13431da177e4SLinus Torvalds return SECCLASS_NETLINK_TCPDIAG_SOCKET; 13441da177e4SLinus Torvalds case NETLINK_NFLOG: 13451da177e4SLinus Torvalds return SECCLASS_NETLINK_NFLOG_SOCKET; 13461da177e4SLinus Torvalds case NETLINK_XFRM: 13471da177e4SLinus Torvalds return SECCLASS_NETLINK_XFRM_SOCKET; 13481da177e4SLinus Torvalds case NETLINK_SELINUX: 13491da177e4SLinus Torvalds return SECCLASS_NETLINK_SELINUX_SOCKET; 13506c6d2e9bSStephen Smalley case NETLINK_ISCSI: 13516c6d2e9bSStephen Smalley return SECCLASS_NETLINK_ISCSI_SOCKET; 13521da177e4SLinus Torvalds case NETLINK_AUDIT: 13531da177e4SLinus Torvalds return SECCLASS_NETLINK_AUDIT_SOCKET; 13546c6d2e9bSStephen Smalley case NETLINK_FIB_LOOKUP: 13556c6d2e9bSStephen Smalley return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET; 13566c6d2e9bSStephen Smalley case NETLINK_CONNECTOR: 13576c6d2e9bSStephen Smalley return SECCLASS_NETLINK_CONNECTOR_SOCKET; 13586c6d2e9bSStephen Smalley case NETLINK_NETFILTER: 13596c6d2e9bSStephen Smalley return SECCLASS_NETLINK_NETFILTER_SOCKET; 13601da177e4SLinus Torvalds case NETLINK_DNRTMSG: 13611da177e4SLinus Torvalds return SECCLASS_NETLINK_DNRT_SOCKET; 13620c9b7942SJames Morris case NETLINK_KOBJECT_UEVENT: 13630c9b7942SJames Morris return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET; 13646c6d2e9bSStephen Smalley case NETLINK_GENERIC: 13656c6d2e9bSStephen Smalley return SECCLASS_NETLINK_GENERIC_SOCKET; 13666c6d2e9bSStephen Smalley case NETLINK_SCSITRANSPORT: 13676c6d2e9bSStephen Smalley return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET; 13686c6d2e9bSStephen Smalley case NETLINK_RDMA: 13696c6d2e9bSStephen Smalley return SECCLASS_NETLINK_RDMA_SOCKET; 13706c6d2e9bSStephen Smalley case NETLINK_CRYPTO: 13716c6d2e9bSStephen Smalley return SECCLASS_NETLINK_CRYPTO_SOCKET; 13721da177e4SLinus Torvalds default: 13731da177e4SLinus Torvalds return SECCLASS_NETLINK_SOCKET; 13741da177e4SLinus Torvalds } 13751da177e4SLinus Torvalds case PF_PACKET: 13761da177e4SLinus Torvalds return SECCLASS_PACKET_SOCKET; 13771da177e4SLinus Torvalds case PF_KEY: 13781da177e4SLinus Torvalds return SECCLASS_KEY_SOCKET; 13793e3ff15eSChristopher J. PeBenito case PF_APPLETALK: 13803e3ff15eSChristopher J. PeBenito return SECCLASS_APPLETALK_SOCKET; 13811da177e4SLinus Torvalds } 13821da177e4SLinus Torvalds 1383da69a530SStephen Smalley if (extsockclass) { 1384da69a530SStephen Smalley switch (family) { 1385da69a530SStephen Smalley case PF_AX25: 1386da69a530SStephen Smalley return SECCLASS_AX25_SOCKET; 1387da69a530SStephen Smalley case PF_IPX: 1388da69a530SStephen Smalley return SECCLASS_IPX_SOCKET; 1389da69a530SStephen Smalley case PF_NETROM: 1390da69a530SStephen Smalley return SECCLASS_NETROM_SOCKET; 1391da69a530SStephen Smalley case PF_ATMPVC: 1392da69a530SStephen Smalley return SECCLASS_ATMPVC_SOCKET; 1393da69a530SStephen Smalley case PF_X25: 1394da69a530SStephen Smalley return SECCLASS_X25_SOCKET; 1395da69a530SStephen Smalley case PF_ROSE: 1396da69a530SStephen Smalley return SECCLASS_ROSE_SOCKET; 1397da69a530SStephen Smalley case PF_DECnet: 1398da69a530SStephen Smalley return SECCLASS_DECNET_SOCKET; 1399da69a530SStephen Smalley case PF_ATMSVC: 1400da69a530SStephen Smalley return SECCLASS_ATMSVC_SOCKET; 1401da69a530SStephen Smalley case PF_RDS: 1402da69a530SStephen Smalley return SECCLASS_RDS_SOCKET; 1403da69a530SStephen Smalley case PF_IRDA: 1404da69a530SStephen Smalley return SECCLASS_IRDA_SOCKET; 1405da69a530SStephen Smalley case PF_PPPOX: 1406da69a530SStephen Smalley return SECCLASS_PPPOX_SOCKET; 1407da69a530SStephen Smalley case PF_LLC: 1408da69a530SStephen Smalley return SECCLASS_LLC_SOCKET; 1409da69a530SStephen Smalley case PF_CAN: 1410da69a530SStephen Smalley return SECCLASS_CAN_SOCKET; 1411da69a530SStephen Smalley case PF_TIPC: 1412da69a530SStephen Smalley return SECCLASS_TIPC_SOCKET; 1413da69a530SStephen Smalley case PF_BLUETOOTH: 1414da69a530SStephen Smalley return SECCLASS_BLUETOOTH_SOCKET; 1415da69a530SStephen Smalley case PF_IUCV: 1416da69a530SStephen Smalley return SECCLASS_IUCV_SOCKET; 1417da69a530SStephen Smalley case PF_RXRPC: 1418da69a530SStephen Smalley return SECCLASS_RXRPC_SOCKET; 1419da69a530SStephen Smalley case PF_ISDN: 1420da69a530SStephen Smalley return SECCLASS_ISDN_SOCKET; 1421da69a530SStephen Smalley case PF_PHONET: 1422da69a530SStephen Smalley return SECCLASS_PHONET_SOCKET; 1423da69a530SStephen Smalley case PF_IEEE802154: 1424da69a530SStephen Smalley return SECCLASS_IEEE802154_SOCKET; 1425da69a530SStephen Smalley case PF_CAIF: 1426da69a530SStephen Smalley return SECCLASS_CAIF_SOCKET; 1427da69a530SStephen Smalley case PF_ALG: 1428da69a530SStephen Smalley return SECCLASS_ALG_SOCKET; 1429da69a530SStephen Smalley case PF_NFC: 1430da69a530SStephen Smalley return SECCLASS_NFC_SOCKET; 1431da69a530SStephen Smalley case PF_VSOCK: 1432da69a530SStephen Smalley return SECCLASS_VSOCK_SOCKET; 1433da69a530SStephen Smalley case PF_KCM: 1434da69a530SStephen Smalley return SECCLASS_KCM_SOCKET; 1435da69a530SStephen Smalley case PF_QIPCRTR: 1436da69a530SStephen Smalley return SECCLASS_QIPCRTR_SOCKET; 14373051bf36SLinus Torvalds case PF_SMC: 14383051bf36SLinus Torvalds return SECCLASS_SMC_SOCKET; 14393051bf36SLinus Torvalds #if PF_MAX > 44 1440da69a530SStephen Smalley #error New address family defined, please update this function. 1441da69a530SStephen Smalley #endif 1442da69a530SStephen Smalley } 1443da69a530SStephen Smalley } 1444da69a530SStephen Smalley 14451da177e4SLinus Torvalds return SECCLASS_SOCKET; 14461da177e4SLinus Torvalds } 14471da177e4SLinus Torvalds 1448134509d5SStephen Smalley static int selinux_genfs_get_sid(struct dentry *dentry, 14491da177e4SLinus Torvalds u16 tclass, 1450134509d5SStephen Smalley u16 flags, 14511da177e4SLinus Torvalds u32 *sid) 14521da177e4SLinus Torvalds { 14538e6c9693SLucian Adrian Grijincu int rc; 1454fc64005cSAl Viro struct super_block *sb = dentry->d_sb; 14558e6c9693SLucian Adrian Grijincu char *buffer, *path; 14561da177e4SLinus Torvalds 14571da177e4SLinus Torvalds buffer = (char *)__get_free_page(GFP_KERNEL); 14581da177e4SLinus Torvalds if (!buffer) 14591da177e4SLinus Torvalds return -ENOMEM; 14601da177e4SLinus Torvalds 14618e6c9693SLucian Adrian Grijincu path = dentry_path_raw(dentry, buffer, PAGE_SIZE); 14628e6c9693SLucian Adrian Grijincu if (IS_ERR(path)) 14638e6c9693SLucian Adrian Grijincu rc = PTR_ERR(path); 14648e6c9693SLucian Adrian Grijincu else { 1465134509d5SStephen Smalley if (flags & SE_SBPROC) { 14668e6c9693SLucian Adrian Grijincu /* each process gets a /proc/PID/ entry. Strip off the 14678e6c9693SLucian Adrian Grijincu * PID part to get a valid selinux labeling. 14688e6c9693SLucian Adrian Grijincu * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */ 14698e6c9693SLucian Adrian Grijincu while (path[1] >= '0' && path[1] <= '9') { 14708e6c9693SLucian Adrian Grijincu path[1] = '/'; 14718e6c9693SLucian Adrian Grijincu path++; 14721da177e4SLinus Torvalds } 1473134509d5SStephen Smalley } 1474134509d5SStephen Smalley rc = security_genfs_sid(sb->s_type->name, path, tclass, sid); 14758e6c9693SLucian Adrian Grijincu } 14761da177e4SLinus Torvalds free_page((unsigned long)buffer); 14771da177e4SLinus Torvalds return rc; 14781da177e4SLinus Torvalds } 14791da177e4SLinus Torvalds 14801da177e4SLinus Torvalds /* The inode's security attributes must be initialized before first use. */ 14811da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry) 14821da177e4SLinus Torvalds { 14831da177e4SLinus Torvalds struct superblock_security_struct *sbsec = NULL; 14841da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 14859287aed2SAndreas Gruenbacher u32 task_sid, sid = 0; 14869287aed2SAndreas Gruenbacher u16 sclass; 14871da177e4SLinus Torvalds struct dentry *dentry; 14881da177e4SLinus Torvalds #define INITCONTEXTLEN 255 14891da177e4SLinus Torvalds char *context = NULL; 14901da177e4SLinus Torvalds unsigned len = 0; 14911da177e4SLinus Torvalds int rc = 0; 14921da177e4SLinus Torvalds 14936f3be9f5SAndreas Gruenbacher if (isec->initialized == LABEL_INITIALIZED) 149413457d07SAndreas Gruenbacher return 0; 14951da177e4SLinus Torvalds 14969287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 14976f3be9f5SAndreas Gruenbacher if (isec->initialized == LABEL_INITIALIZED) 149823970741SEric Paris goto out_unlock; 14991da177e4SLinus Torvalds 150013457d07SAndreas Gruenbacher if (isec->sclass == SECCLASS_FILE) 150113457d07SAndreas Gruenbacher isec->sclass = inode_mode_to_security_class(inode->i_mode); 150213457d07SAndreas Gruenbacher 15031da177e4SLinus Torvalds sbsec = inode->i_sb->s_security; 15040d90a7ecSDavid P. Quigley if (!(sbsec->flags & SE_SBINITIALIZED)) { 15051da177e4SLinus Torvalds /* Defer initialization until selinux_complete_init, 15061da177e4SLinus Torvalds after the initial policy is loaded and the security 15071da177e4SLinus Torvalds server is ready to handle calls. */ 15081da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 15091da177e4SLinus Torvalds if (list_empty(&isec->list)) 15101da177e4SLinus Torvalds list_add(&isec->list, &sbsec->isec_head); 15111da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 151223970741SEric Paris goto out_unlock; 15131da177e4SLinus Torvalds } 15141da177e4SLinus Torvalds 15159287aed2SAndreas Gruenbacher sclass = isec->sclass; 15169287aed2SAndreas Gruenbacher task_sid = isec->task_sid; 15179287aed2SAndreas Gruenbacher sid = isec->sid; 15189287aed2SAndreas Gruenbacher isec->initialized = LABEL_PENDING; 15199287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 15209287aed2SAndreas Gruenbacher 15211da177e4SLinus Torvalds switch (sbsec->behavior) { 1522eb9ae686SDavid Quigley case SECURITY_FS_USE_NATIVE: 1523eb9ae686SDavid Quigley break; 15241da177e4SLinus Torvalds case SECURITY_FS_USE_XATTR: 15255d6c3191SAndreas Gruenbacher if (!(inode->i_opflags & IOP_XATTR)) { 15269287aed2SAndreas Gruenbacher sid = sbsec->def_sid; 15271da177e4SLinus Torvalds break; 15281da177e4SLinus Torvalds } 15291da177e4SLinus Torvalds /* Need a dentry, since the xattr API requires one. 15301da177e4SLinus Torvalds Life would be simpler if we could just pass the inode. */ 15311da177e4SLinus Torvalds if (opt_dentry) { 15321da177e4SLinus Torvalds /* Called from d_instantiate or d_splice_alias. */ 15331da177e4SLinus Torvalds dentry = dget(opt_dentry); 15341da177e4SLinus Torvalds } else { 15351da177e4SLinus Torvalds /* Called from selinux_complete_init, try to find a dentry. */ 15361da177e4SLinus Torvalds dentry = d_find_alias(inode); 15371da177e4SLinus Torvalds } 15381da177e4SLinus Torvalds if (!dentry) { 1539df7f54c0SEric Paris /* 1540df7f54c0SEric Paris * this is can be hit on boot when a file is accessed 1541df7f54c0SEric Paris * before the policy is loaded. When we load policy we 1542df7f54c0SEric Paris * may find inodes that have no dentry on the 1543df7f54c0SEric Paris * sbsec->isec_head list. No reason to complain as these 1544df7f54c0SEric Paris * will get fixed up the next time we go through 1545df7f54c0SEric Paris * inode_doinit with a dentry, before these inodes could 1546df7f54c0SEric Paris * be used again by userspace. 1547df7f54c0SEric Paris */ 15489287aed2SAndreas Gruenbacher goto out; 15491da177e4SLinus Torvalds } 15501da177e4SLinus Torvalds 15511da177e4SLinus Torvalds len = INITCONTEXTLEN; 15524cb912f1SEric Paris context = kmalloc(len+1, GFP_NOFS); 15531da177e4SLinus Torvalds if (!context) { 15541da177e4SLinus Torvalds rc = -ENOMEM; 15551da177e4SLinus Torvalds dput(dentry); 15569287aed2SAndreas Gruenbacher goto out; 15571da177e4SLinus Torvalds } 15584cb912f1SEric Paris context[len] = '\0'; 15595d6c3191SAndreas Gruenbacher rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len); 15601da177e4SLinus Torvalds if (rc == -ERANGE) { 1561314dabb8SJames Morris kfree(context); 1562314dabb8SJames Morris 15631da177e4SLinus Torvalds /* Need a larger buffer. Query for the right size. */ 15645d6c3191SAndreas Gruenbacher rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0); 15651da177e4SLinus Torvalds if (rc < 0) { 15661da177e4SLinus Torvalds dput(dentry); 15679287aed2SAndreas Gruenbacher goto out; 15681da177e4SLinus Torvalds } 15691da177e4SLinus Torvalds len = rc; 15704cb912f1SEric Paris context = kmalloc(len+1, GFP_NOFS); 15711da177e4SLinus Torvalds if (!context) { 15721da177e4SLinus Torvalds rc = -ENOMEM; 15731da177e4SLinus Torvalds dput(dentry); 15749287aed2SAndreas Gruenbacher goto out; 15751da177e4SLinus Torvalds } 15764cb912f1SEric Paris context[len] = '\0'; 15775d6c3191SAndreas Gruenbacher rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len); 15781da177e4SLinus Torvalds } 15791da177e4SLinus Torvalds dput(dentry); 15801da177e4SLinus Torvalds if (rc < 0) { 15811da177e4SLinus Torvalds if (rc != -ENODATA) { 1582744ba35eSEric Paris printk(KERN_WARNING "SELinux: %s: getxattr returned " 1583dd6f953aSHarvey Harrison "%d for dev=%s ino=%ld\n", __func__, 15841da177e4SLinus Torvalds -rc, inode->i_sb->s_id, inode->i_ino); 15851da177e4SLinus Torvalds kfree(context); 15869287aed2SAndreas Gruenbacher goto out; 15871da177e4SLinus Torvalds } 15881da177e4SLinus Torvalds /* Map ENODATA to the default file SID */ 15891da177e4SLinus Torvalds sid = sbsec->def_sid; 15901da177e4SLinus Torvalds rc = 0; 15911da177e4SLinus Torvalds } else { 1592f5c1d5b2SJames Morris rc = security_context_to_sid_default(context, rc, &sid, 1593869ab514SStephen Smalley sbsec->def_sid, 1594869ab514SStephen Smalley GFP_NOFS); 15951da177e4SLinus Torvalds if (rc) { 15964ba0a8adSEric Paris char *dev = inode->i_sb->s_id; 15974ba0a8adSEric Paris unsigned long ino = inode->i_ino; 15984ba0a8adSEric Paris 15994ba0a8adSEric Paris if (rc == -EINVAL) { 16004ba0a8adSEric Paris if (printk_ratelimit()) 16014ba0a8adSEric Paris printk(KERN_NOTICE "SELinux: inode=%lu on dev=%s was found to have an invalid " 16024ba0a8adSEric Paris "context=%s. This indicates you may need to relabel the inode or the " 16034ba0a8adSEric Paris "filesystem in question.\n", ino, dev, context); 16044ba0a8adSEric Paris } else { 1605744ba35eSEric Paris printk(KERN_WARNING "SELinux: %s: context_to_sid(%s) " 16061da177e4SLinus Torvalds "returned %d for dev=%s ino=%ld\n", 16074ba0a8adSEric Paris __func__, context, -rc, dev, ino); 16084ba0a8adSEric Paris } 16091da177e4SLinus Torvalds kfree(context); 16101da177e4SLinus Torvalds /* Leave with the unlabeled SID */ 16111da177e4SLinus Torvalds rc = 0; 16121da177e4SLinus Torvalds break; 16131da177e4SLinus Torvalds } 16141da177e4SLinus Torvalds } 16151da177e4SLinus Torvalds kfree(context); 16161da177e4SLinus Torvalds break; 16171da177e4SLinus Torvalds case SECURITY_FS_USE_TASK: 16189287aed2SAndreas Gruenbacher sid = task_sid; 16191da177e4SLinus Torvalds break; 16201da177e4SLinus Torvalds case SECURITY_FS_USE_TRANS: 16211da177e4SLinus Torvalds /* Default to the fs SID. */ 16229287aed2SAndreas Gruenbacher sid = sbsec->sid; 16231da177e4SLinus Torvalds 16241da177e4SLinus Torvalds /* Try to obtain a transition SID. */ 16259287aed2SAndreas Gruenbacher rc = security_transition_sid(task_sid, sid, sclass, NULL, &sid); 16261da177e4SLinus Torvalds if (rc) 16279287aed2SAndreas Gruenbacher goto out; 16281da177e4SLinus Torvalds break; 1629c312feb2SEric Paris case SECURITY_FS_USE_MNTPOINT: 16309287aed2SAndreas Gruenbacher sid = sbsec->mntpoint_sid; 1631c312feb2SEric Paris break; 16321da177e4SLinus Torvalds default: 1633c312feb2SEric Paris /* Default to the fs superblock SID. */ 16349287aed2SAndreas Gruenbacher sid = sbsec->sid; 16351da177e4SLinus Torvalds 1636134509d5SStephen Smalley if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) { 1637f64410ecSPaul Moore /* We must have a dentry to determine the label on 1638f64410ecSPaul Moore * procfs inodes */ 1639f64410ecSPaul Moore if (opt_dentry) 1640f64410ecSPaul Moore /* Called from d_instantiate or 1641f64410ecSPaul Moore * d_splice_alias. */ 1642f64410ecSPaul Moore dentry = dget(opt_dentry); 1643f64410ecSPaul Moore else 1644f64410ecSPaul Moore /* Called from selinux_complete_init, try to 1645f64410ecSPaul Moore * find a dentry. */ 1646f64410ecSPaul Moore dentry = d_find_alias(inode); 1647f64410ecSPaul Moore /* 1648f64410ecSPaul Moore * This can be hit on boot when a file is accessed 1649f64410ecSPaul Moore * before the policy is loaded. When we load policy we 1650f64410ecSPaul Moore * may find inodes that have no dentry on the 1651f64410ecSPaul Moore * sbsec->isec_head list. No reason to complain as 1652f64410ecSPaul Moore * these will get fixed up the next time we go through 1653f64410ecSPaul Moore * inode_doinit() with a dentry, before these inodes 1654f64410ecSPaul Moore * could be used again by userspace. 1655f64410ecSPaul Moore */ 1656f64410ecSPaul Moore if (!dentry) 16579287aed2SAndreas Gruenbacher goto out; 16589287aed2SAndreas Gruenbacher rc = selinux_genfs_get_sid(dentry, sclass, 1659134509d5SStephen Smalley sbsec->flags, &sid); 1660f64410ecSPaul Moore dput(dentry); 16611da177e4SLinus Torvalds if (rc) 16629287aed2SAndreas Gruenbacher goto out; 16631da177e4SLinus Torvalds } 16641da177e4SLinus Torvalds break; 16651da177e4SLinus Torvalds } 16661da177e4SLinus Torvalds 16679287aed2SAndreas Gruenbacher out: 16689287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 16699287aed2SAndreas Gruenbacher if (isec->initialized == LABEL_PENDING) { 16709287aed2SAndreas Gruenbacher if (!sid || rc) { 16719287aed2SAndreas Gruenbacher isec->initialized = LABEL_INVALID; 16729287aed2SAndreas Gruenbacher goto out_unlock; 16739287aed2SAndreas Gruenbacher } 16749287aed2SAndreas Gruenbacher 16756f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 16769287aed2SAndreas Gruenbacher isec->sid = sid; 16779287aed2SAndreas Gruenbacher } 16781da177e4SLinus Torvalds 167923970741SEric Paris out_unlock: 16809287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 16811da177e4SLinus Torvalds return rc; 16821da177e4SLinus Torvalds } 16831da177e4SLinus Torvalds 16841da177e4SLinus Torvalds /* Convert a Linux signal to an access vector. */ 16851da177e4SLinus Torvalds static inline u32 signal_to_av(int sig) 16861da177e4SLinus Torvalds { 16871da177e4SLinus Torvalds u32 perm = 0; 16881da177e4SLinus Torvalds 16891da177e4SLinus Torvalds switch (sig) { 16901da177e4SLinus Torvalds case SIGCHLD: 16911da177e4SLinus Torvalds /* Commonly granted from child to parent. */ 16921da177e4SLinus Torvalds perm = PROCESS__SIGCHLD; 16931da177e4SLinus Torvalds break; 16941da177e4SLinus Torvalds case SIGKILL: 16951da177e4SLinus Torvalds /* Cannot be caught or ignored */ 16961da177e4SLinus Torvalds perm = PROCESS__SIGKILL; 16971da177e4SLinus Torvalds break; 16981da177e4SLinus Torvalds case SIGSTOP: 16991da177e4SLinus Torvalds /* Cannot be caught or ignored */ 17001da177e4SLinus Torvalds perm = PROCESS__SIGSTOP; 17011da177e4SLinus Torvalds break; 17021da177e4SLinus Torvalds default: 17031da177e4SLinus Torvalds /* All other signals. */ 17041da177e4SLinus Torvalds perm = PROCESS__SIGNAL; 17051da177e4SLinus Torvalds break; 17061da177e4SLinus Torvalds } 17071da177e4SLinus Torvalds 17081da177e4SLinus Torvalds return perm; 17091da177e4SLinus Torvalds } 17101da177e4SLinus Torvalds 1711b68e418cSStephen Smalley #if CAP_LAST_CAP > 63 1712b68e418cSStephen Smalley #error Fix SELinux to handle capabilities > 63. 1713b68e418cSStephen Smalley #endif 1714b68e418cSStephen Smalley 17151da177e4SLinus Torvalds /* Check whether a task is allowed to use a capability. */ 17166a9de491SEric Paris static int cred_has_capability(const struct cred *cred, 17178e4ff6f2SStephen Smalley int cap, int audit, bool initns) 17181da177e4SLinus Torvalds { 17192bf49690SThomas Liu struct common_audit_data ad; 172006112163SEric Paris struct av_decision avd; 1721b68e418cSStephen Smalley u16 sclass; 17223699c53cSDavid Howells u32 sid = cred_sid(cred); 1723b68e418cSStephen Smalley u32 av = CAP_TO_MASK(cap); 172406112163SEric Paris int rc; 17251da177e4SLinus Torvalds 172650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_CAP; 17271da177e4SLinus Torvalds ad.u.cap = cap; 17281da177e4SLinus Torvalds 1729b68e418cSStephen Smalley switch (CAP_TO_INDEX(cap)) { 1730b68e418cSStephen Smalley case 0: 17318e4ff6f2SStephen Smalley sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS; 1732b68e418cSStephen Smalley break; 1733b68e418cSStephen Smalley case 1: 17348e4ff6f2SStephen Smalley sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS; 1735b68e418cSStephen Smalley break; 1736b68e418cSStephen Smalley default: 1737b68e418cSStephen Smalley printk(KERN_ERR 1738b68e418cSStephen Smalley "SELinux: out of range capability %d\n", cap); 1739b68e418cSStephen Smalley BUG(); 1740a35c6c83SEric Paris return -EINVAL; 1741b68e418cSStephen Smalley } 174206112163SEric Paris 1743275bb41eSDavid Howells rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd); 17449ade0cf4SEric Paris if (audit == SECURITY_CAP_AUDIT) { 17457b20ea25SNeilBrown int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad, 0); 17469ade0cf4SEric Paris if (rc2) 17479ade0cf4SEric Paris return rc2; 17489ade0cf4SEric Paris } 174906112163SEric Paris return rc; 17501da177e4SLinus Torvalds } 17511da177e4SLinus Torvalds 17521da177e4SLinus Torvalds /* Check whether a task has a particular permission to an inode. 17531da177e4SLinus Torvalds The 'adp' parameter is optional and allows other audit 17541da177e4SLinus Torvalds data to be passed (e.g. the dentry). */ 175588e67f3bSDavid Howells static int inode_has_perm(const struct cred *cred, 17561da177e4SLinus Torvalds struct inode *inode, 17571da177e4SLinus Torvalds u32 perms, 175819e49834SLinus Torvalds struct common_audit_data *adp) 17591da177e4SLinus Torvalds { 17601da177e4SLinus Torvalds struct inode_security_struct *isec; 1761275bb41eSDavid Howells u32 sid; 17621da177e4SLinus Torvalds 1763e0e81739SDavid Howells validate_creds(cred); 1764e0e81739SDavid Howells 1765bbaca6c2SStephen Smalley if (unlikely(IS_PRIVATE(inode))) 1766bbaca6c2SStephen Smalley return 0; 1767bbaca6c2SStephen Smalley 176888e67f3bSDavid Howells sid = cred_sid(cred); 17691da177e4SLinus Torvalds isec = inode->i_security; 17701da177e4SLinus Torvalds 177119e49834SLinus Torvalds return avc_has_perm(sid, isec->sid, isec->sclass, perms, adp); 17721da177e4SLinus Torvalds } 17731da177e4SLinus Torvalds 17741da177e4SLinus Torvalds /* Same as inode_has_perm, but pass explicit audit data containing 17751da177e4SLinus Torvalds the dentry to help the auditing code to more easily generate the 17761da177e4SLinus Torvalds pathname if needed. */ 177788e67f3bSDavid Howells static inline int dentry_has_perm(const struct cred *cred, 17781da177e4SLinus Torvalds struct dentry *dentry, 17791da177e4SLinus Torvalds u32 av) 17801da177e4SLinus Torvalds { 1781c6f493d6SDavid Howells struct inode *inode = d_backing_inode(dentry); 17822bf49690SThomas Liu struct common_audit_data ad; 178388e67f3bSDavid Howells 178450c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 17852875fa00SEric Paris ad.u.dentry = dentry; 17865d226df4SAndreas Gruenbacher __inode_security_revalidate(inode, dentry, true); 178719e49834SLinus Torvalds return inode_has_perm(cred, inode, av, &ad); 17882875fa00SEric Paris } 17892875fa00SEric Paris 17902875fa00SEric Paris /* Same as inode_has_perm, but pass explicit audit data containing 17912875fa00SEric Paris the path to help the auditing code to more easily generate the 17922875fa00SEric Paris pathname if needed. */ 17932875fa00SEric Paris static inline int path_has_perm(const struct cred *cred, 17943f7036a0SAl Viro const struct path *path, 17952875fa00SEric Paris u32 av) 17962875fa00SEric Paris { 1797c6f493d6SDavid Howells struct inode *inode = d_backing_inode(path->dentry); 17982875fa00SEric Paris struct common_audit_data ad; 17992875fa00SEric Paris 180050c205f5SEric Paris ad.type = LSM_AUDIT_DATA_PATH; 18012875fa00SEric Paris ad.u.path = *path; 18025d226df4SAndreas Gruenbacher __inode_security_revalidate(inode, path->dentry, true); 180319e49834SLinus Torvalds return inode_has_perm(cred, inode, av, &ad); 18041da177e4SLinus Torvalds } 18051da177e4SLinus Torvalds 180613f8e981SDavid Howells /* Same as path_has_perm, but uses the inode from the file struct. */ 180713f8e981SDavid Howells static inline int file_path_has_perm(const struct cred *cred, 180813f8e981SDavid Howells struct file *file, 180913f8e981SDavid Howells u32 av) 181013f8e981SDavid Howells { 181113f8e981SDavid Howells struct common_audit_data ad; 181213f8e981SDavid Howells 181343af5de7SVivek Goyal ad.type = LSM_AUDIT_DATA_FILE; 181443af5de7SVivek Goyal ad.u.file = file; 181519e49834SLinus Torvalds return inode_has_perm(cred, file_inode(file), av, &ad); 181613f8e981SDavid Howells } 181713f8e981SDavid Howells 1818f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL 1819f66e448cSChenbo Feng static int bpf_fd_pass(struct file *file, u32 sid); 1820f66e448cSChenbo Feng #endif 1821f66e448cSChenbo Feng 18221da177e4SLinus Torvalds /* Check whether a task can use an open file descriptor to 18231da177e4SLinus Torvalds access an inode in a given way. Check access to the 18241da177e4SLinus Torvalds descriptor itself, and then use dentry_has_perm to 18251da177e4SLinus Torvalds check a particular permission to the file. 18261da177e4SLinus Torvalds Access to the descriptor is implicitly granted if it 18271da177e4SLinus Torvalds has the same SID as the process. If av is zero, then 18281da177e4SLinus Torvalds access to the file is not checked, e.g. for cases 18291da177e4SLinus Torvalds where only the descriptor is affected like seek. */ 183088e67f3bSDavid Howells static int file_has_perm(const struct cred *cred, 18311da177e4SLinus Torvalds struct file *file, 18321da177e4SLinus Torvalds u32 av) 18331da177e4SLinus Torvalds { 18341da177e4SLinus Torvalds struct file_security_struct *fsec = file->f_security; 1835496ad9aaSAl Viro struct inode *inode = file_inode(file); 18362bf49690SThomas Liu struct common_audit_data ad; 183788e67f3bSDavid Howells u32 sid = cred_sid(cred); 18381da177e4SLinus Torvalds int rc; 18391da177e4SLinus Torvalds 184043af5de7SVivek Goyal ad.type = LSM_AUDIT_DATA_FILE; 184143af5de7SVivek Goyal ad.u.file = file; 18421da177e4SLinus Torvalds 1843275bb41eSDavid Howells if (sid != fsec->sid) { 1844275bb41eSDavid Howells rc = avc_has_perm(sid, fsec->sid, 18451da177e4SLinus Torvalds SECCLASS_FD, 18461da177e4SLinus Torvalds FD__USE, 18471da177e4SLinus Torvalds &ad); 18481da177e4SLinus Torvalds if (rc) 184988e67f3bSDavid Howells goto out; 18501da177e4SLinus Torvalds } 18511da177e4SLinus Torvalds 1852f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL 1853f66e448cSChenbo Feng rc = bpf_fd_pass(file, cred_sid(cred)); 1854f66e448cSChenbo Feng if (rc) 1855f66e448cSChenbo Feng return rc; 1856f66e448cSChenbo Feng #endif 1857f66e448cSChenbo Feng 18581da177e4SLinus Torvalds /* av is zero if only checking access to the descriptor. */ 185988e67f3bSDavid Howells rc = 0; 18601da177e4SLinus Torvalds if (av) 186119e49834SLinus Torvalds rc = inode_has_perm(cred, inode, av, &ad); 18621da177e4SLinus Torvalds 186388e67f3bSDavid Howells out: 186488e67f3bSDavid Howells return rc; 18651da177e4SLinus Torvalds } 18661da177e4SLinus Torvalds 1867c3c188b2SDavid Howells /* 1868c3c188b2SDavid Howells * Determine the label for an inode that might be unioned. 1869c3c188b2SDavid Howells */ 1870c957f6dfSVivek Goyal static int 1871c957f6dfSVivek Goyal selinux_determine_inode_label(const struct task_security_struct *tsec, 1872c957f6dfSVivek Goyal struct inode *dir, 1873c957f6dfSVivek Goyal const struct qstr *name, u16 tclass, 1874c3c188b2SDavid Howells u32 *_new_isid) 1875c3c188b2SDavid Howells { 1876c3c188b2SDavid Howells const struct superblock_security_struct *sbsec = dir->i_sb->s_security; 1877c3c188b2SDavid Howells 1878c3c188b2SDavid Howells if ((sbsec->flags & SE_SBINITIALIZED) && 1879c3c188b2SDavid Howells (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) { 1880c3c188b2SDavid Howells *_new_isid = sbsec->mntpoint_sid; 1881c3c188b2SDavid Howells } else if ((sbsec->flags & SBLABEL_MNT) && 1882c3c188b2SDavid Howells tsec->create_sid) { 1883c3c188b2SDavid Howells *_new_isid = tsec->create_sid; 1884c3c188b2SDavid Howells } else { 188520cdef8dSPaul Moore const struct inode_security_struct *dsec = inode_security(dir); 1886c3c188b2SDavid Howells return security_transition_sid(tsec->sid, dsec->sid, tclass, 1887c3c188b2SDavid Howells name, _new_isid); 1888c3c188b2SDavid Howells } 1889c3c188b2SDavid Howells 1890c3c188b2SDavid Howells return 0; 1891c3c188b2SDavid Howells } 1892c3c188b2SDavid Howells 18931da177e4SLinus Torvalds /* Check whether a task can create a file. */ 18941da177e4SLinus Torvalds static int may_create(struct inode *dir, 18951da177e4SLinus Torvalds struct dentry *dentry, 18961da177e4SLinus Torvalds u16 tclass) 18971da177e4SLinus Torvalds { 18985fb49870SPaul Moore const struct task_security_struct *tsec = current_security(); 18991da177e4SLinus Torvalds struct inode_security_struct *dsec; 19001da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 1901275bb41eSDavid Howells u32 sid, newsid; 19022bf49690SThomas Liu struct common_audit_data ad; 19031da177e4SLinus Torvalds int rc; 19041da177e4SLinus Torvalds 190583da53c5SAndreas Gruenbacher dsec = inode_security(dir); 19061da177e4SLinus Torvalds sbsec = dir->i_sb->s_security; 19071da177e4SLinus Torvalds 1908275bb41eSDavid Howells sid = tsec->sid; 1909275bb41eSDavid Howells 191050c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 1911a269434dSEric Paris ad.u.dentry = dentry; 19121da177e4SLinus Torvalds 1913275bb41eSDavid Howells rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, 19141da177e4SLinus Torvalds DIR__ADD_NAME | DIR__SEARCH, 19151da177e4SLinus Torvalds &ad); 19161da177e4SLinus Torvalds if (rc) 19171da177e4SLinus Torvalds return rc; 19181da177e4SLinus Torvalds 1919c957f6dfSVivek Goyal rc = selinux_determine_inode_label(current_security(), dir, 1920c957f6dfSVivek Goyal &dentry->d_name, tclass, &newsid); 19211da177e4SLinus Torvalds if (rc) 19221da177e4SLinus Torvalds return rc; 19231da177e4SLinus Torvalds 1924275bb41eSDavid Howells rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad); 19251da177e4SLinus Torvalds if (rc) 19261da177e4SLinus Torvalds return rc; 19271da177e4SLinus Torvalds 19281da177e4SLinus Torvalds return avc_has_perm(newsid, sbsec->sid, 19291da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 19301da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, &ad); 19311da177e4SLinus Torvalds } 19321da177e4SLinus Torvalds 19331da177e4SLinus Torvalds #define MAY_LINK 0 19341da177e4SLinus Torvalds #define MAY_UNLINK 1 19351da177e4SLinus Torvalds #define MAY_RMDIR 2 19361da177e4SLinus Torvalds 19371da177e4SLinus Torvalds /* Check whether a task can link, unlink, or rmdir a file/directory. */ 19381da177e4SLinus Torvalds static int may_link(struct inode *dir, 19391da177e4SLinus Torvalds struct dentry *dentry, 19401da177e4SLinus Torvalds int kind) 19411da177e4SLinus Torvalds 19421da177e4SLinus Torvalds { 19431da177e4SLinus Torvalds struct inode_security_struct *dsec, *isec; 19442bf49690SThomas Liu struct common_audit_data ad; 1945275bb41eSDavid Howells u32 sid = current_sid(); 19461da177e4SLinus Torvalds u32 av; 19471da177e4SLinus Torvalds int rc; 19481da177e4SLinus Torvalds 194983da53c5SAndreas Gruenbacher dsec = inode_security(dir); 195083da53c5SAndreas Gruenbacher isec = backing_inode_security(dentry); 19511da177e4SLinus Torvalds 195250c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 1953a269434dSEric Paris ad.u.dentry = dentry; 19541da177e4SLinus Torvalds 19551da177e4SLinus Torvalds av = DIR__SEARCH; 19561da177e4SLinus Torvalds av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME); 1957275bb41eSDavid Howells rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad); 19581da177e4SLinus Torvalds if (rc) 19591da177e4SLinus Torvalds return rc; 19601da177e4SLinus Torvalds 19611da177e4SLinus Torvalds switch (kind) { 19621da177e4SLinus Torvalds case MAY_LINK: 19631da177e4SLinus Torvalds av = FILE__LINK; 19641da177e4SLinus Torvalds break; 19651da177e4SLinus Torvalds case MAY_UNLINK: 19661da177e4SLinus Torvalds av = FILE__UNLINK; 19671da177e4SLinus Torvalds break; 19681da177e4SLinus Torvalds case MAY_RMDIR: 19691da177e4SLinus Torvalds av = DIR__RMDIR; 19701da177e4SLinus Torvalds break; 19711da177e4SLinus Torvalds default: 1972744ba35eSEric Paris printk(KERN_WARNING "SELinux: %s: unrecognized kind %d\n", 1973744ba35eSEric Paris __func__, kind); 19741da177e4SLinus Torvalds return 0; 19751da177e4SLinus Torvalds } 19761da177e4SLinus Torvalds 1977275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad); 19781da177e4SLinus Torvalds return rc; 19791da177e4SLinus Torvalds } 19801da177e4SLinus Torvalds 19811da177e4SLinus Torvalds static inline int may_rename(struct inode *old_dir, 19821da177e4SLinus Torvalds struct dentry *old_dentry, 19831da177e4SLinus Torvalds struct inode *new_dir, 19841da177e4SLinus Torvalds struct dentry *new_dentry) 19851da177e4SLinus Torvalds { 19861da177e4SLinus Torvalds struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec; 19872bf49690SThomas Liu struct common_audit_data ad; 1988275bb41eSDavid Howells u32 sid = current_sid(); 19891da177e4SLinus Torvalds u32 av; 19901da177e4SLinus Torvalds int old_is_dir, new_is_dir; 19911da177e4SLinus Torvalds int rc; 19921da177e4SLinus Torvalds 199383da53c5SAndreas Gruenbacher old_dsec = inode_security(old_dir); 199483da53c5SAndreas Gruenbacher old_isec = backing_inode_security(old_dentry); 1995e36cb0b8SDavid Howells old_is_dir = d_is_dir(old_dentry); 199683da53c5SAndreas Gruenbacher new_dsec = inode_security(new_dir); 19971da177e4SLinus Torvalds 199850c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 19991da177e4SLinus Torvalds 2000a269434dSEric Paris ad.u.dentry = old_dentry; 2001275bb41eSDavid Howells rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR, 20021da177e4SLinus Torvalds DIR__REMOVE_NAME | DIR__SEARCH, &ad); 20031da177e4SLinus Torvalds if (rc) 20041da177e4SLinus Torvalds return rc; 2005275bb41eSDavid Howells rc = avc_has_perm(sid, old_isec->sid, 20061da177e4SLinus Torvalds old_isec->sclass, FILE__RENAME, &ad); 20071da177e4SLinus Torvalds if (rc) 20081da177e4SLinus Torvalds return rc; 20091da177e4SLinus Torvalds if (old_is_dir && new_dir != old_dir) { 2010275bb41eSDavid Howells rc = avc_has_perm(sid, old_isec->sid, 20111da177e4SLinus Torvalds old_isec->sclass, DIR__REPARENT, &ad); 20121da177e4SLinus Torvalds if (rc) 20131da177e4SLinus Torvalds return rc; 20141da177e4SLinus Torvalds } 20151da177e4SLinus Torvalds 2016a269434dSEric Paris ad.u.dentry = new_dentry; 20171da177e4SLinus Torvalds av = DIR__ADD_NAME | DIR__SEARCH; 20182c616d4dSDavid Howells if (d_is_positive(new_dentry)) 20191da177e4SLinus Torvalds av |= DIR__REMOVE_NAME; 2020275bb41eSDavid Howells rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad); 20211da177e4SLinus Torvalds if (rc) 20221da177e4SLinus Torvalds return rc; 20232c616d4dSDavid Howells if (d_is_positive(new_dentry)) { 202483da53c5SAndreas Gruenbacher new_isec = backing_inode_security(new_dentry); 2025e36cb0b8SDavid Howells new_is_dir = d_is_dir(new_dentry); 2026275bb41eSDavid Howells rc = avc_has_perm(sid, new_isec->sid, 20271da177e4SLinus Torvalds new_isec->sclass, 20281da177e4SLinus Torvalds (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad); 20291da177e4SLinus Torvalds if (rc) 20301da177e4SLinus Torvalds return rc; 20311da177e4SLinus Torvalds } 20321da177e4SLinus Torvalds 20331da177e4SLinus Torvalds return 0; 20341da177e4SLinus Torvalds } 20351da177e4SLinus Torvalds 20361da177e4SLinus Torvalds /* Check whether a task can perform a filesystem operation. */ 203788e67f3bSDavid Howells static int superblock_has_perm(const struct cred *cred, 20381da177e4SLinus Torvalds struct super_block *sb, 20391da177e4SLinus Torvalds u32 perms, 20402bf49690SThomas Liu struct common_audit_data *ad) 20411da177e4SLinus Torvalds { 20421da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 204388e67f3bSDavid Howells u32 sid = cred_sid(cred); 20441da177e4SLinus Torvalds 20451da177e4SLinus Torvalds sbsec = sb->s_security; 2046275bb41eSDavid Howells return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad); 20471da177e4SLinus Torvalds } 20481da177e4SLinus Torvalds 20491da177e4SLinus Torvalds /* Convert a Linux mode and permission mask to an access vector. */ 20501da177e4SLinus Torvalds static inline u32 file_mask_to_av(int mode, int mask) 20511da177e4SLinus Torvalds { 20521da177e4SLinus Torvalds u32 av = 0; 20531da177e4SLinus Torvalds 2054dba19c60SAl Viro if (!S_ISDIR(mode)) { 20551da177e4SLinus Torvalds if (mask & MAY_EXEC) 20561da177e4SLinus Torvalds av |= FILE__EXECUTE; 20571da177e4SLinus Torvalds if (mask & MAY_READ) 20581da177e4SLinus Torvalds av |= FILE__READ; 20591da177e4SLinus Torvalds 20601da177e4SLinus Torvalds if (mask & MAY_APPEND) 20611da177e4SLinus Torvalds av |= FILE__APPEND; 20621da177e4SLinus Torvalds else if (mask & MAY_WRITE) 20631da177e4SLinus Torvalds av |= FILE__WRITE; 20641da177e4SLinus Torvalds 20651da177e4SLinus Torvalds } else { 20661da177e4SLinus Torvalds if (mask & MAY_EXEC) 20671da177e4SLinus Torvalds av |= DIR__SEARCH; 20681da177e4SLinus Torvalds if (mask & MAY_WRITE) 20691da177e4SLinus Torvalds av |= DIR__WRITE; 20701da177e4SLinus Torvalds if (mask & MAY_READ) 20711da177e4SLinus Torvalds av |= DIR__READ; 20721da177e4SLinus Torvalds } 20731da177e4SLinus Torvalds 20741da177e4SLinus Torvalds return av; 20751da177e4SLinus Torvalds } 20761da177e4SLinus Torvalds 20771da177e4SLinus Torvalds /* Convert a Linux file to an access vector. */ 20781da177e4SLinus Torvalds static inline u32 file_to_av(struct file *file) 20791da177e4SLinus Torvalds { 20801da177e4SLinus Torvalds u32 av = 0; 20811da177e4SLinus Torvalds 20821da177e4SLinus Torvalds if (file->f_mode & FMODE_READ) 20831da177e4SLinus Torvalds av |= FILE__READ; 20841da177e4SLinus Torvalds if (file->f_mode & FMODE_WRITE) { 20851da177e4SLinus Torvalds if (file->f_flags & O_APPEND) 20861da177e4SLinus Torvalds av |= FILE__APPEND; 20871da177e4SLinus Torvalds else 20881da177e4SLinus Torvalds av |= FILE__WRITE; 20891da177e4SLinus Torvalds } 20900794c66dSStephen Smalley if (!av) { 20910794c66dSStephen Smalley /* 20920794c66dSStephen Smalley * Special file opened with flags 3 for ioctl-only use. 20930794c66dSStephen Smalley */ 20940794c66dSStephen Smalley av = FILE__IOCTL; 20950794c66dSStephen Smalley } 20961da177e4SLinus Torvalds 20971da177e4SLinus Torvalds return av; 20981da177e4SLinus Torvalds } 20991da177e4SLinus Torvalds 21008b6a5a37SEric Paris /* 21018b6a5a37SEric Paris * Convert a file to an access vector and include the correct open 21028b6a5a37SEric Paris * open permission. 21038b6a5a37SEric Paris */ 21048b6a5a37SEric Paris static inline u32 open_file_to_av(struct file *file) 21058b6a5a37SEric Paris { 21068b6a5a37SEric Paris u32 av = file_to_av(file); 2107ccb54478SStephen Smalley struct inode *inode = file_inode(file); 21088b6a5a37SEric Paris 2109ccb54478SStephen Smalley if (selinux_policycap_openperm && inode->i_sb->s_magic != SOCKFS_MAGIC) 21108b6a5a37SEric Paris av |= FILE__OPEN; 211149b7b8deSEric Paris 21128b6a5a37SEric Paris return av; 21138b6a5a37SEric Paris } 21148b6a5a37SEric Paris 21151da177e4SLinus Torvalds /* Hook functions begin here. */ 21161da177e4SLinus Torvalds 211779af7307SStephen Smalley static int selinux_binder_set_context_mgr(struct task_struct *mgr) 211879af7307SStephen Smalley { 211979af7307SStephen Smalley u32 mysid = current_sid(); 212079af7307SStephen Smalley u32 mgrsid = task_sid(mgr); 212179af7307SStephen Smalley 212279af7307SStephen Smalley return avc_has_perm(mysid, mgrsid, SECCLASS_BINDER, 212379af7307SStephen Smalley BINDER__SET_CONTEXT_MGR, NULL); 212479af7307SStephen Smalley } 212579af7307SStephen Smalley 212679af7307SStephen Smalley static int selinux_binder_transaction(struct task_struct *from, 212779af7307SStephen Smalley struct task_struct *to) 212879af7307SStephen Smalley { 212979af7307SStephen Smalley u32 mysid = current_sid(); 213079af7307SStephen Smalley u32 fromsid = task_sid(from); 213179af7307SStephen Smalley u32 tosid = task_sid(to); 213279af7307SStephen Smalley int rc; 213379af7307SStephen Smalley 213479af7307SStephen Smalley if (mysid != fromsid) { 213579af7307SStephen Smalley rc = avc_has_perm(mysid, fromsid, SECCLASS_BINDER, 213679af7307SStephen Smalley BINDER__IMPERSONATE, NULL); 213779af7307SStephen Smalley if (rc) 213879af7307SStephen Smalley return rc; 213979af7307SStephen Smalley } 214079af7307SStephen Smalley 214179af7307SStephen Smalley return avc_has_perm(fromsid, tosid, SECCLASS_BINDER, BINDER__CALL, 214279af7307SStephen Smalley NULL); 214379af7307SStephen Smalley } 214479af7307SStephen Smalley 214579af7307SStephen Smalley static int selinux_binder_transfer_binder(struct task_struct *from, 214679af7307SStephen Smalley struct task_struct *to) 214779af7307SStephen Smalley { 214879af7307SStephen Smalley u32 fromsid = task_sid(from); 214979af7307SStephen Smalley u32 tosid = task_sid(to); 215079af7307SStephen Smalley 215179af7307SStephen Smalley return avc_has_perm(fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER, 215279af7307SStephen Smalley NULL); 215379af7307SStephen Smalley } 215479af7307SStephen Smalley 215579af7307SStephen Smalley static int selinux_binder_transfer_file(struct task_struct *from, 215679af7307SStephen Smalley struct task_struct *to, 215779af7307SStephen Smalley struct file *file) 215879af7307SStephen Smalley { 215979af7307SStephen Smalley u32 sid = task_sid(to); 216079af7307SStephen Smalley struct file_security_struct *fsec = file->f_security; 216183da53c5SAndreas Gruenbacher struct dentry *dentry = file->f_path.dentry; 216220cdef8dSPaul Moore struct inode_security_struct *isec; 216379af7307SStephen Smalley struct common_audit_data ad; 216479af7307SStephen Smalley int rc; 216579af7307SStephen Smalley 216679af7307SStephen Smalley ad.type = LSM_AUDIT_DATA_PATH; 216779af7307SStephen Smalley ad.u.path = file->f_path; 216879af7307SStephen Smalley 216979af7307SStephen Smalley if (sid != fsec->sid) { 217079af7307SStephen Smalley rc = avc_has_perm(sid, fsec->sid, 217179af7307SStephen Smalley SECCLASS_FD, 217279af7307SStephen Smalley FD__USE, 217379af7307SStephen Smalley &ad); 217479af7307SStephen Smalley if (rc) 217579af7307SStephen Smalley return rc; 217679af7307SStephen Smalley } 217779af7307SStephen Smalley 2178f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL 2179f66e448cSChenbo Feng rc = bpf_fd_pass(file, sid); 2180f66e448cSChenbo Feng if (rc) 2181f66e448cSChenbo Feng return rc; 2182f66e448cSChenbo Feng #endif 2183f66e448cSChenbo Feng 218483da53c5SAndreas Gruenbacher if (unlikely(IS_PRIVATE(d_backing_inode(dentry)))) 218579af7307SStephen Smalley return 0; 218679af7307SStephen Smalley 218720cdef8dSPaul Moore isec = backing_inode_security(dentry); 218879af7307SStephen Smalley return avc_has_perm(sid, isec->sid, isec->sclass, file_to_av(file), 218979af7307SStephen Smalley &ad); 219079af7307SStephen Smalley } 219179af7307SStephen Smalley 21929e48858fSIngo Molnar static int selinux_ptrace_access_check(struct task_struct *child, 2193006ebb40SStephen Smalley unsigned int mode) 21941da177e4SLinus Torvalds { 2195275bb41eSDavid Howells u32 sid = current_sid(); 2196275bb41eSDavid Howells u32 csid = task_sid(child); 2197006ebb40SStephen Smalley 2198be0554c9SStephen Smalley if (mode & PTRACE_MODE_READ) 2199be0554c9SStephen Smalley return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, NULL); 2200be0554c9SStephen Smalley 2201be0554c9SStephen Smalley return avc_has_perm(sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL); 22025cd9c58fSDavid Howells } 22035cd9c58fSDavid Howells 22045cd9c58fSDavid Howells static int selinux_ptrace_traceme(struct task_struct *parent) 22055cd9c58fSDavid Howells { 2206be0554c9SStephen Smalley return avc_has_perm(task_sid(parent), current_sid(), SECCLASS_PROCESS, 2207be0554c9SStephen Smalley PROCESS__PTRACE, NULL); 22081da177e4SLinus Torvalds } 22091da177e4SLinus Torvalds 22101da177e4SLinus Torvalds static int selinux_capget(struct task_struct *target, kernel_cap_t *effective, 22111da177e4SLinus Torvalds kernel_cap_t *inheritable, kernel_cap_t *permitted) 22121da177e4SLinus Torvalds { 2213be0554c9SStephen Smalley return avc_has_perm(current_sid(), task_sid(target), SECCLASS_PROCESS, 2214be0554c9SStephen Smalley PROCESS__GETCAP, NULL); 22151da177e4SLinus Torvalds } 22161da177e4SLinus Torvalds 2217d84f4f99SDavid Howells static int selinux_capset(struct cred *new, const struct cred *old, 2218d84f4f99SDavid Howells const kernel_cap_t *effective, 221915a2460eSDavid Howells const kernel_cap_t *inheritable, 222015a2460eSDavid Howells const kernel_cap_t *permitted) 22211da177e4SLinus Torvalds { 2222be0554c9SStephen Smalley return avc_has_perm(cred_sid(old), cred_sid(new), SECCLASS_PROCESS, 2223be0554c9SStephen Smalley PROCESS__SETCAP, NULL); 22241da177e4SLinus Torvalds } 22251da177e4SLinus Torvalds 22265626d3e8SJames Morris /* 22275626d3e8SJames Morris * (This comment used to live with the selinux_task_setuid hook, 22285626d3e8SJames Morris * which was removed). 22295626d3e8SJames Morris * 22305626d3e8SJames Morris * Since setuid only affects the current process, and since the SELinux 22315626d3e8SJames Morris * controls are not based on the Linux identity attributes, SELinux does not 22325626d3e8SJames Morris * need to control this operation. However, SELinux does control the use of 22335626d3e8SJames Morris * the CAP_SETUID and CAP_SETGID capabilities using the capable hook. 22345626d3e8SJames Morris */ 22355626d3e8SJames Morris 22366a9de491SEric Paris static int selinux_capable(const struct cred *cred, struct user_namespace *ns, 22376a9de491SEric Paris int cap, int audit) 22381da177e4SLinus Torvalds { 22398e4ff6f2SStephen Smalley return cred_has_capability(cred, cap, audit, ns == &init_user_ns); 22401da177e4SLinus Torvalds } 22411da177e4SLinus Torvalds 22421da177e4SLinus Torvalds static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb) 22431da177e4SLinus Torvalds { 224488e67f3bSDavid Howells const struct cred *cred = current_cred(); 22451da177e4SLinus Torvalds int rc = 0; 22461da177e4SLinus Torvalds 22471da177e4SLinus Torvalds if (!sb) 22481da177e4SLinus Torvalds return 0; 22491da177e4SLinus Torvalds 22501da177e4SLinus Torvalds switch (cmds) { 22511da177e4SLinus Torvalds case Q_SYNC: 22521da177e4SLinus Torvalds case Q_QUOTAON: 22531da177e4SLinus Torvalds case Q_QUOTAOFF: 22541da177e4SLinus Torvalds case Q_SETINFO: 22551da177e4SLinus Torvalds case Q_SETQUOTA: 225688e67f3bSDavid Howells rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL); 22571da177e4SLinus Torvalds break; 22581da177e4SLinus Torvalds case Q_GETFMT: 22591da177e4SLinus Torvalds case Q_GETINFO: 22601da177e4SLinus Torvalds case Q_GETQUOTA: 226188e67f3bSDavid Howells rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL); 22621da177e4SLinus Torvalds break; 22631da177e4SLinus Torvalds default: 22641da177e4SLinus Torvalds rc = 0; /* let the kernel handle invalid cmds */ 22651da177e4SLinus Torvalds break; 22661da177e4SLinus Torvalds } 22671da177e4SLinus Torvalds return rc; 22681da177e4SLinus Torvalds } 22691da177e4SLinus Torvalds 22701da177e4SLinus Torvalds static int selinux_quota_on(struct dentry *dentry) 22711da177e4SLinus Torvalds { 227288e67f3bSDavid Howells const struct cred *cred = current_cred(); 227388e67f3bSDavid Howells 22742875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__QUOTAON); 22751da177e4SLinus Torvalds } 22761da177e4SLinus Torvalds 227712b3052cSEric Paris static int selinux_syslog(int type) 22781da177e4SLinus Torvalds { 22791da177e4SLinus Torvalds switch (type) { 2280d78ca3cdSKees Cook case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */ 2281d78ca3cdSKees Cook case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */ 2282be0554c9SStephen Smalley return avc_has_perm(current_sid(), SECINITSID_KERNEL, 2283be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL); 2284d78ca3cdSKees Cook case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */ 2285d78ca3cdSKees Cook case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */ 2286d78ca3cdSKees Cook /* Set level of messages printed to console */ 2287d78ca3cdSKees Cook case SYSLOG_ACTION_CONSOLE_LEVEL: 2288be0554c9SStephen Smalley return avc_has_perm(current_sid(), SECINITSID_KERNEL, 2289be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE, 2290be0554c9SStephen Smalley NULL); 22911da177e4SLinus Torvalds } 2292be0554c9SStephen Smalley /* All other syslog types */ 2293be0554c9SStephen Smalley return avc_has_perm(current_sid(), SECINITSID_KERNEL, 2294be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL); 22951da177e4SLinus Torvalds } 22961da177e4SLinus Torvalds 22971da177e4SLinus Torvalds /* 22981da177e4SLinus Torvalds * Check that a process has enough memory to allocate a new virtual 22991da177e4SLinus Torvalds * mapping. 0 means there is enough memory for the allocation to 23001da177e4SLinus Torvalds * succeed and -ENOMEM implies there is not. 23011da177e4SLinus Torvalds * 23021da177e4SLinus Torvalds * Do not audit the selinux permission check, as this is applied to all 23031da177e4SLinus Torvalds * processes that allocate mappings. 23041da177e4SLinus Torvalds */ 230534b4e4aaSAlan Cox static int selinux_vm_enough_memory(struct mm_struct *mm, long pages) 23061da177e4SLinus Torvalds { 23071da177e4SLinus Torvalds int rc, cap_sys_admin = 0; 23081da177e4SLinus Torvalds 2309b1d9e6b0SCasey Schaufler rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN, 23108e4ff6f2SStephen Smalley SECURITY_CAP_NOAUDIT, true); 23111da177e4SLinus Torvalds if (rc == 0) 23121da177e4SLinus Torvalds cap_sys_admin = 1; 23131da177e4SLinus Torvalds 2314b1d9e6b0SCasey Schaufler return cap_sys_admin; 23151da177e4SLinus Torvalds } 23161da177e4SLinus Torvalds 23171da177e4SLinus Torvalds /* binprm security operations */ 23181da177e4SLinus Torvalds 2319be0554c9SStephen Smalley static u32 ptrace_parent_sid(void) 23200c6181cbSPaul Moore { 23210c6181cbSPaul Moore u32 sid = 0; 23220c6181cbSPaul Moore struct task_struct *tracer; 23230c6181cbSPaul Moore 23240c6181cbSPaul Moore rcu_read_lock(); 2325be0554c9SStephen Smalley tracer = ptrace_parent(current); 23260c6181cbSPaul Moore if (tracer) 23270c6181cbSPaul Moore sid = task_sid(tracer); 23280c6181cbSPaul Moore rcu_read_unlock(); 23290c6181cbSPaul Moore 23300c6181cbSPaul Moore return sid; 23310c6181cbSPaul Moore } 23320c6181cbSPaul Moore 23337b0d0b40SStephen Smalley static int check_nnp_nosuid(const struct linux_binprm *bprm, 23347b0d0b40SStephen Smalley const struct task_security_struct *old_tsec, 23357b0d0b40SStephen Smalley const struct task_security_struct *new_tsec) 23367b0d0b40SStephen Smalley { 23377b0d0b40SStephen Smalley int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS); 2338380cf5baSAndy Lutomirski int nosuid = !mnt_may_suid(bprm->file->f_path.mnt); 23397b0d0b40SStephen Smalley int rc; 2340af63f419SStephen Smalley u32 av; 23417b0d0b40SStephen Smalley 23427b0d0b40SStephen Smalley if (!nnp && !nosuid) 23437b0d0b40SStephen Smalley return 0; /* neither NNP nor nosuid */ 23447b0d0b40SStephen Smalley 23457b0d0b40SStephen Smalley if (new_tsec->sid == old_tsec->sid) 23467b0d0b40SStephen Smalley return 0; /* No change in credentials */ 23477b0d0b40SStephen Smalley 23487b0d0b40SStephen Smalley /* 2349af63f419SStephen Smalley * If the policy enables the nnp_nosuid_transition policy capability, 2350af63f419SStephen Smalley * then we permit transitions under NNP or nosuid if the 2351af63f419SStephen Smalley * policy allows the corresponding permission between 2352af63f419SStephen Smalley * the old and new contexts. 2353af63f419SStephen Smalley */ 2354af63f419SStephen Smalley if (selinux_policycap_nnp_nosuid_transition) { 2355af63f419SStephen Smalley av = 0; 2356af63f419SStephen Smalley if (nnp) 2357af63f419SStephen Smalley av |= PROCESS2__NNP_TRANSITION; 2358af63f419SStephen Smalley if (nosuid) 2359af63f419SStephen Smalley av |= PROCESS2__NOSUID_TRANSITION; 2360af63f419SStephen Smalley rc = avc_has_perm(old_tsec->sid, new_tsec->sid, 2361af63f419SStephen Smalley SECCLASS_PROCESS2, av, NULL); 2362af63f419SStephen Smalley if (!rc) 2363af63f419SStephen Smalley return 0; 2364af63f419SStephen Smalley } 2365af63f419SStephen Smalley 2366af63f419SStephen Smalley /* 2367af63f419SStephen Smalley * We also permit NNP or nosuid transitions to bounded SIDs, 2368af63f419SStephen Smalley * i.e. SIDs that are guaranteed to only be allowed a subset 2369af63f419SStephen Smalley * of the permissions of the current SID. 23707b0d0b40SStephen Smalley */ 23717b0d0b40SStephen Smalley rc = security_bounded_transition(old_tsec->sid, new_tsec->sid); 2372af63f419SStephen Smalley if (!rc) 2373af63f419SStephen Smalley return 0; 2374af63f419SStephen Smalley 23757b0d0b40SStephen Smalley /* 23767b0d0b40SStephen Smalley * On failure, preserve the errno values for NNP vs nosuid. 23777b0d0b40SStephen Smalley * NNP: Operation not permitted for caller. 23787b0d0b40SStephen Smalley * nosuid: Permission denied to file. 23797b0d0b40SStephen Smalley */ 23807b0d0b40SStephen Smalley if (nnp) 23817b0d0b40SStephen Smalley return -EPERM; 23827b0d0b40SStephen Smalley return -EACCES; 23837b0d0b40SStephen Smalley } 23847b0d0b40SStephen Smalley 2385a6f76f23SDavid Howells static int selinux_bprm_set_creds(struct linux_binprm *bprm) 23861da177e4SLinus Torvalds { 2387a6f76f23SDavid Howells const struct task_security_struct *old_tsec; 2388a6f76f23SDavid Howells struct task_security_struct *new_tsec; 23891da177e4SLinus Torvalds struct inode_security_struct *isec; 23902bf49690SThomas Liu struct common_audit_data ad; 2391496ad9aaSAl Viro struct inode *inode = file_inode(bprm->file); 23921da177e4SLinus Torvalds int rc; 23931da177e4SLinus Torvalds 2394a6f76f23SDavid Howells /* SELinux context only depends on initial program or script and not 2395a6f76f23SDavid Howells * the script interpreter */ 2396ddb4a144SKees Cook if (bprm->called_set_creds) 23971da177e4SLinus Torvalds return 0; 23981da177e4SLinus Torvalds 2399a6f76f23SDavid Howells old_tsec = current_security(); 2400a6f76f23SDavid Howells new_tsec = bprm->cred->security; 240183da53c5SAndreas Gruenbacher isec = inode_security(inode); 24021da177e4SLinus Torvalds 24031da177e4SLinus Torvalds /* Default to the current task SID. */ 2404a6f76f23SDavid Howells new_tsec->sid = old_tsec->sid; 2405a6f76f23SDavid Howells new_tsec->osid = old_tsec->sid; 24061da177e4SLinus Torvalds 240728eba5bfSMichael LeMay /* Reset fs, key, and sock SIDs on execve. */ 2408a6f76f23SDavid Howells new_tsec->create_sid = 0; 2409a6f76f23SDavid Howells new_tsec->keycreate_sid = 0; 2410a6f76f23SDavid Howells new_tsec->sockcreate_sid = 0; 24111da177e4SLinus Torvalds 2412a6f76f23SDavid Howells if (old_tsec->exec_sid) { 2413a6f76f23SDavid Howells new_tsec->sid = old_tsec->exec_sid; 24141da177e4SLinus Torvalds /* Reset exec SID on execve. */ 2415a6f76f23SDavid Howells new_tsec->exec_sid = 0; 2416259e5e6cSAndy Lutomirski 24177b0d0b40SStephen Smalley /* Fail on NNP or nosuid if not an allowed transition. */ 24187b0d0b40SStephen Smalley rc = check_nnp_nosuid(bprm, old_tsec, new_tsec); 24197b0d0b40SStephen Smalley if (rc) 24207b0d0b40SStephen Smalley return rc; 24211da177e4SLinus Torvalds } else { 24221da177e4SLinus Torvalds /* Check for a default transition on this program. */ 2423a6f76f23SDavid Howells rc = security_transition_sid(old_tsec->sid, isec->sid, 2424652bb9b0SEric Paris SECCLASS_PROCESS, NULL, 2425652bb9b0SEric Paris &new_tsec->sid); 24261da177e4SLinus Torvalds if (rc) 24271da177e4SLinus Torvalds return rc; 24287b0d0b40SStephen Smalley 24297b0d0b40SStephen Smalley /* 24307b0d0b40SStephen Smalley * Fallback to old SID on NNP or nosuid if not an allowed 24317b0d0b40SStephen Smalley * transition. 24327b0d0b40SStephen Smalley */ 24337b0d0b40SStephen Smalley rc = check_nnp_nosuid(bprm, old_tsec, new_tsec); 24347b0d0b40SStephen Smalley if (rc) 24357b0d0b40SStephen Smalley new_tsec->sid = old_tsec->sid; 24361da177e4SLinus Torvalds } 24371da177e4SLinus Torvalds 243843af5de7SVivek Goyal ad.type = LSM_AUDIT_DATA_FILE; 243943af5de7SVivek Goyal ad.u.file = bprm->file; 24401da177e4SLinus Torvalds 2441a6f76f23SDavid Howells if (new_tsec->sid == old_tsec->sid) { 2442a6f76f23SDavid Howells rc = avc_has_perm(old_tsec->sid, isec->sid, 24431da177e4SLinus Torvalds SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad); 24441da177e4SLinus Torvalds if (rc) 24451da177e4SLinus Torvalds return rc; 24461da177e4SLinus Torvalds } else { 24471da177e4SLinus Torvalds /* Check permissions for the transition. */ 2448a6f76f23SDavid Howells rc = avc_has_perm(old_tsec->sid, new_tsec->sid, 24491da177e4SLinus Torvalds SECCLASS_PROCESS, PROCESS__TRANSITION, &ad); 24501da177e4SLinus Torvalds if (rc) 24511da177e4SLinus Torvalds return rc; 24521da177e4SLinus Torvalds 2453a6f76f23SDavid Howells rc = avc_has_perm(new_tsec->sid, isec->sid, 24541da177e4SLinus Torvalds SECCLASS_FILE, FILE__ENTRYPOINT, &ad); 24551da177e4SLinus Torvalds if (rc) 24561da177e4SLinus Torvalds return rc; 24571da177e4SLinus Torvalds 2458a6f76f23SDavid Howells /* Check for shared state */ 2459a6f76f23SDavid Howells if (bprm->unsafe & LSM_UNSAFE_SHARE) { 2460a6f76f23SDavid Howells rc = avc_has_perm(old_tsec->sid, new_tsec->sid, 2461a6f76f23SDavid Howells SECCLASS_PROCESS, PROCESS__SHARE, 2462a6f76f23SDavid Howells NULL); 2463a6f76f23SDavid Howells if (rc) 2464a6f76f23SDavid Howells return -EPERM; 24651da177e4SLinus Torvalds } 24661da177e4SLinus Torvalds 2467a6f76f23SDavid Howells /* Make sure that anyone attempting to ptrace over a task that 2468a6f76f23SDavid Howells * changes its SID has the appropriate permit */ 24699227dd2aSEric W. Biederman if (bprm->unsafe & LSM_UNSAFE_PTRACE) { 2470be0554c9SStephen Smalley u32 ptsid = ptrace_parent_sid(); 2471a6f76f23SDavid Howells if (ptsid != 0) { 2472a6f76f23SDavid Howells rc = avc_has_perm(ptsid, new_tsec->sid, 2473a6f76f23SDavid Howells SECCLASS_PROCESS, 2474a6f76f23SDavid Howells PROCESS__PTRACE, NULL); 2475a6f76f23SDavid Howells if (rc) 2476a6f76f23SDavid Howells return -EPERM; 2477a6f76f23SDavid Howells } 2478a6f76f23SDavid Howells } 2479a6f76f23SDavid Howells 2480a6f76f23SDavid Howells /* Clear any possibly unsafe personality bits on exec: */ 2481a6f76f23SDavid Howells bprm->per_clear |= PER_CLEAR_ON_SETID; 2482a6f76f23SDavid Howells 24831da177e4SLinus Torvalds /* Enable secure mode for SIDs transitions unless 24841da177e4SLinus Torvalds the noatsecure permission is granted between 24851da177e4SLinus Torvalds the two SIDs, i.e. ahp returns 0. */ 248662874c3aSKees Cook rc = avc_has_perm(old_tsec->sid, new_tsec->sid, 248762874c3aSKees Cook SECCLASS_PROCESS, PROCESS__NOATSECURE, 248862874c3aSKees Cook NULL); 248962874c3aSKees Cook bprm->secureexec |= !!rc; 24901da177e4SLinus Torvalds } 24911da177e4SLinus Torvalds 249262874c3aSKees Cook return 0; 24931da177e4SLinus Torvalds } 24941da177e4SLinus Torvalds 2495c3c073f8SAl Viro static int match_file(const void *p, struct file *file, unsigned fd) 2496c3c073f8SAl Viro { 2497c3c073f8SAl Viro return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0; 2498c3c073f8SAl Viro } 2499c3c073f8SAl Viro 25001da177e4SLinus Torvalds /* Derived from fs/exec.c:flush_old_files. */ 2501745ca247SDavid Howells static inline void flush_unauthorized_files(const struct cred *cred, 2502745ca247SDavid Howells struct files_struct *files) 25031da177e4SLinus Torvalds { 25041da177e4SLinus Torvalds struct file *file, *devnull = NULL; 2505b20c8122SStephen Smalley struct tty_struct *tty; 250624ec839cSPeter Zijlstra int drop_tty = 0; 2507c3c073f8SAl Viro unsigned n; 25081da177e4SLinus Torvalds 250924ec839cSPeter Zijlstra tty = get_current_tty(); 25101da177e4SLinus Torvalds if (tty) { 25114a510969SPeter Hurley spin_lock(&tty->files_lock); 251237dd0bd0SEric Paris if (!list_empty(&tty->tty_files)) { 2513d996b62aSNick Piggin struct tty_file_private *file_priv; 251437dd0bd0SEric Paris 25151da177e4SLinus Torvalds /* Revalidate access to controlling tty. 251613f8e981SDavid Howells Use file_path_has_perm on the tty path directly 251713f8e981SDavid Howells rather than using file_has_perm, as this particular 251813f8e981SDavid Howells open file may belong to another process and we are 251913f8e981SDavid Howells only interested in the inode-based check here. */ 2520d996b62aSNick Piggin file_priv = list_first_entry(&tty->tty_files, 2521d996b62aSNick Piggin struct tty_file_private, list); 2522d996b62aSNick Piggin file = file_priv->file; 252313f8e981SDavid Howells if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE)) 252424ec839cSPeter Zijlstra drop_tty = 1; 25251da177e4SLinus Torvalds } 25264a510969SPeter Hurley spin_unlock(&tty->files_lock); 2527452a00d2SAlan Cox tty_kref_put(tty); 25281da177e4SLinus Torvalds } 252998a27ba4SEric W. Biederman /* Reset controlling tty. */ 253098a27ba4SEric W. Biederman if (drop_tty) 253198a27ba4SEric W. Biederman no_tty(); 25321da177e4SLinus Torvalds 25331da177e4SLinus Torvalds /* Revalidate access to inherited open files. */ 2534c3c073f8SAl Viro n = iterate_fd(files, 0, match_file, cred); 2535c3c073f8SAl Viro if (!n) /* none found? */ 2536c3c073f8SAl Viro return; 25371da177e4SLinus Torvalds 2538c3c073f8SAl Viro devnull = dentry_open(&selinux_null, O_RDWR, cred); 253945525b26SAl Viro if (IS_ERR(devnull)) 254045525b26SAl Viro devnull = NULL; 2541c3c073f8SAl Viro /* replace all the matching ones with this */ 2542c3c073f8SAl Viro do { 254345525b26SAl Viro replace_fd(n - 1, devnull, 0); 2544c3c073f8SAl Viro } while ((n = iterate_fd(files, n, match_file, cred)) != 0); 254545525b26SAl Viro if (devnull) 2546c3c073f8SAl Viro fput(devnull); 25471da177e4SLinus Torvalds } 25481da177e4SLinus Torvalds 25491da177e4SLinus Torvalds /* 2550a6f76f23SDavid Howells * Prepare a process for imminent new credential changes due to exec 25511da177e4SLinus Torvalds */ 2552a6f76f23SDavid Howells static void selinux_bprm_committing_creds(struct linux_binprm *bprm) 25531da177e4SLinus Torvalds { 2554a6f76f23SDavid Howells struct task_security_struct *new_tsec; 25551da177e4SLinus Torvalds struct rlimit *rlim, *initrlim; 25561da177e4SLinus Torvalds int rc, i; 25571da177e4SLinus Torvalds 2558a6f76f23SDavid Howells new_tsec = bprm->cred->security; 2559a6f76f23SDavid Howells if (new_tsec->sid == new_tsec->osid) 25601da177e4SLinus Torvalds return; 25611da177e4SLinus Torvalds 25621da177e4SLinus Torvalds /* Close files for which the new task SID is not authorized. */ 2563a6f76f23SDavid Howells flush_unauthorized_files(bprm->cred, current->files); 25641da177e4SLinus Torvalds 2565a6f76f23SDavid Howells /* Always clear parent death signal on SID transitions. */ 2566a6f76f23SDavid Howells current->pdeath_signal = 0; 2567a6f76f23SDavid Howells 2568a6f76f23SDavid Howells /* Check whether the new SID can inherit resource limits from the old 2569a6f76f23SDavid Howells * SID. If not, reset all soft limits to the lower of the current 2570a6f76f23SDavid Howells * task's hard limit and the init task's soft limit. 2571a6f76f23SDavid Howells * 2572a6f76f23SDavid Howells * Note that the setting of hard limits (even to lower them) can be 2573a6f76f23SDavid Howells * controlled by the setrlimit check. The inclusion of the init task's 2574a6f76f23SDavid Howells * soft limit into the computation is to avoid resetting soft limits 2575a6f76f23SDavid Howells * higher than the default soft limit for cases where the default is 2576a6f76f23SDavid Howells * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK. 2577a6f76f23SDavid Howells */ 2578a6f76f23SDavid Howells rc = avc_has_perm(new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS, 2579a6f76f23SDavid Howells PROCESS__RLIMITINH, NULL); 2580a6f76f23SDavid Howells if (rc) { 2581eb2d55a3SOleg Nesterov /* protect against do_prlimit() */ 2582eb2d55a3SOleg Nesterov task_lock(current); 2583a6f76f23SDavid Howells for (i = 0; i < RLIM_NLIMITS; i++) { 2584a6f76f23SDavid Howells rlim = current->signal->rlim + i; 2585a6f76f23SDavid Howells initrlim = init_task.signal->rlim + i; 2586a6f76f23SDavid Howells rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur); 2587a6f76f23SDavid Howells } 2588eb2d55a3SOleg Nesterov task_unlock(current); 2589baa73d9eSNicolas Pitre if (IS_ENABLED(CONFIG_POSIX_TIMERS)) 2590eb2d55a3SOleg Nesterov update_rlimit_cpu(current, rlimit(RLIMIT_CPU)); 2591a6f76f23SDavid Howells } 2592a6f76f23SDavid Howells } 2593a6f76f23SDavid Howells 2594a6f76f23SDavid Howells /* 2595a6f76f23SDavid Howells * Clean up the process immediately after the installation of new credentials 2596a6f76f23SDavid Howells * due to exec 2597a6f76f23SDavid Howells */ 2598a6f76f23SDavid Howells static void selinux_bprm_committed_creds(struct linux_binprm *bprm) 2599a6f76f23SDavid Howells { 2600a6f76f23SDavid Howells const struct task_security_struct *tsec = current_security(); 2601a6f76f23SDavid Howells struct itimerval itimer; 2602a6f76f23SDavid Howells u32 osid, sid; 2603a6f76f23SDavid Howells int rc, i; 2604a6f76f23SDavid Howells 2605a6f76f23SDavid Howells osid = tsec->osid; 2606a6f76f23SDavid Howells sid = tsec->sid; 2607a6f76f23SDavid Howells 2608a6f76f23SDavid Howells if (sid == osid) 2609a6f76f23SDavid Howells return; 2610a6f76f23SDavid Howells 2611a6f76f23SDavid Howells /* Check whether the new SID can inherit signal state from the old SID. 2612a6f76f23SDavid Howells * If not, clear itimers to avoid subsequent signal generation and 2613a6f76f23SDavid Howells * flush and unblock signals. 2614a6f76f23SDavid Howells * 2615a6f76f23SDavid Howells * This must occur _after_ the task SID has been updated so that any 2616a6f76f23SDavid Howells * kill done after the flush will be checked against the new SID. 2617a6f76f23SDavid Howells */ 2618a6f76f23SDavid Howells rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL); 26191da177e4SLinus Torvalds if (rc) { 2620baa73d9eSNicolas Pitre if (IS_ENABLED(CONFIG_POSIX_TIMERS)) { 26211da177e4SLinus Torvalds memset(&itimer, 0, sizeof itimer); 26221da177e4SLinus Torvalds for (i = 0; i < 3; i++) 26231da177e4SLinus Torvalds do_setitimer(i, &itimer, NULL); 2624baa73d9eSNicolas Pitre } 26251da177e4SLinus Torvalds spin_lock_irq(¤t->sighand->siglock); 26269e7c8f8cSOleg Nesterov if (!fatal_signal_pending(current)) { 26279e7c8f8cSOleg Nesterov flush_sigqueue(¤t->pending); 26289e7c8f8cSOleg Nesterov flush_sigqueue(¤t->signal->shared_pending); 26291da177e4SLinus Torvalds flush_signal_handlers(current, 1); 26301da177e4SLinus Torvalds sigemptyset(¤t->blocked); 26319e7c8f8cSOleg Nesterov recalc_sigpending(); 26323bcac026SDavid Howells } 26331da177e4SLinus Torvalds spin_unlock_irq(¤t->sighand->siglock); 26341da177e4SLinus Torvalds } 26351da177e4SLinus Torvalds 2636a6f76f23SDavid Howells /* Wake up the parent if it is waiting so that it can recheck 2637a6f76f23SDavid Howells * wait permission to the new task SID. */ 2638ecd6de3cSOleg Nesterov read_lock(&tasklist_lock); 26390b7570e7SOleg Nesterov __wake_up_parent(current, current->real_parent); 2640ecd6de3cSOleg Nesterov read_unlock(&tasklist_lock); 26411da177e4SLinus Torvalds } 26421da177e4SLinus Torvalds 26431da177e4SLinus Torvalds /* superblock security operations */ 26441da177e4SLinus Torvalds 26451da177e4SLinus Torvalds static int selinux_sb_alloc_security(struct super_block *sb) 26461da177e4SLinus Torvalds { 26471da177e4SLinus Torvalds return superblock_alloc_security(sb); 26481da177e4SLinus Torvalds } 26491da177e4SLinus Torvalds 26501da177e4SLinus Torvalds static void selinux_sb_free_security(struct super_block *sb) 26511da177e4SLinus Torvalds { 26521da177e4SLinus Torvalds superblock_free_security(sb); 26531da177e4SLinus Torvalds } 26541da177e4SLinus Torvalds 26551da177e4SLinus Torvalds static inline int match_prefix(char *prefix, int plen, char *option, int olen) 26561da177e4SLinus Torvalds { 26571da177e4SLinus Torvalds if (plen > olen) 26581da177e4SLinus Torvalds return 0; 26591da177e4SLinus Torvalds 26601da177e4SLinus Torvalds return !memcmp(prefix, option, plen); 26611da177e4SLinus Torvalds } 26621da177e4SLinus Torvalds 26631da177e4SLinus Torvalds static inline int selinux_option(char *option, int len) 26641da177e4SLinus Torvalds { 2665832cbd9aSEric Paris return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) || 2666832cbd9aSEric Paris match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) || 2667832cbd9aSEric Paris match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) || 266811689d47SDavid P. Quigley match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) || 266911689d47SDavid P. Quigley match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len)); 26701da177e4SLinus Torvalds } 26711da177e4SLinus Torvalds 26721da177e4SLinus Torvalds static inline void take_option(char **to, char *from, int *first, int len) 26731da177e4SLinus Torvalds { 26741da177e4SLinus Torvalds if (!*first) { 26751da177e4SLinus Torvalds **to = ','; 26761da177e4SLinus Torvalds *to += 1; 26773528a953SCory Olmo } else 26781da177e4SLinus Torvalds *first = 0; 26791da177e4SLinus Torvalds memcpy(*to, from, len); 26801da177e4SLinus Torvalds *to += len; 26811da177e4SLinus Torvalds } 26821da177e4SLinus Torvalds 26833528a953SCory Olmo static inline void take_selinux_option(char **to, char *from, int *first, 26843528a953SCory Olmo int len) 26853528a953SCory Olmo { 26863528a953SCory Olmo int current_size = 0; 26873528a953SCory Olmo 26883528a953SCory Olmo if (!*first) { 26893528a953SCory Olmo **to = '|'; 26903528a953SCory Olmo *to += 1; 2691828dfe1dSEric Paris } else 26923528a953SCory Olmo *first = 0; 26933528a953SCory Olmo 26943528a953SCory Olmo while (current_size < len) { 26953528a953SCory Olmo if (*from != '"') { 26963528a953SCory Olmo **to = *from; 26973528a953SCory Olmo *to += 1; 26983528a953SCory Olmo } 26993528a953SCory Olmo from += 1; 27003528a953SCory Olmo current_size += 1; 27013528a953SCory Olmo } 27023528a953SCory Olmo } 27033528a953SCory Olmo 2704e0007529SEric Paris static int selinux_sb_copy_data(char *orig, char *copy) 27051da177e4SLinus Torvalds { 27061da177e4SLinus Torvalds int fnosec, fsec, rc = 0; 27071da177e4SLinus Torvalds char *in_save, *in_curr, *in_end; 27081da177e4SLinus Torvalds char *sec_curr, *nosec_save, *nosec; 27093528a953SCory Olmo int open_quote = 0; 27101da177e4SLinus Torvalds 27111da177e4SLinus Torvalds in_curr = orig; 27121da177e4SLinus Torvalds sec_curr = copy; 27131da177e4SLinus Torvalds 27141da177e4SLinus Torvalds nosec = (char *)get_zeroed_page(GFP_KERNEL); 27151da177e4SLinus Torvalds if (!nosec) { 27161da177e4SLinus Torvalds rc = -ENOMEM; 27171da177e4SLinus Torvalds goto out; 27181da177e4SLinus Torvalds } 27191da177e4SLinus Torvalds 27201da177e4SLinus Torvalds nosec_save = nosec; 27211da177e4SLinus Torvalds fnosec = fsec = 1; 27221da177e4SLinus Torvalds in_save = in_end = orig; 27231da177e4SLinus Torvalds 27241da177e4SLinus Torvalds do { 27253528a953SCory Olmo if (*in_end == '"') 27263528a953SCory Olmo open_quote = !open_quote; 27273528a953SCory Olmo if ((*in_end == ',' && open_quote == 0) || 27283528a953SCory Olmo *in_end == '\0') { 27291da177e4SLinus Torvalds int len = in_end - in_curr; 27301da177e4SLinus Torvalds 27311da177e4SLinus Torvalds if (selinux_option(in_curr, len)) 27323528a953SCory Olmo take_selinux_option(&sec_curr, in_curr, &fsec, len); 27331da177e4SLinus Torvalds else 27341da177e4SLinus Torvalds take_option(&nosec, in_curr, &fnosec, len); 27351da177e4SLinus Torvalds 27361da177e4SLinus Torvalds in_curr = in_end + 1; 27371da177e4SLinus Torvalds } 27381da177e4SLinus Torvalds } while (*in_end++); 27391da177e4SLinus Torvalds 27406931dfc9SEric Paris strcpy(in_save, nosec_save); 2741da3caa20SGerald Schaefer free_page((unsigned long)nosec_save); 27421da177e4SLinus Torvalds out: 27431da177e4SLinus Torvalds return rc; 27441da177e4SLinus Torvalds } 27451da177e4SLinus Torvalds 2746026eb167SEric Paris static int selinux_sb_remount(struct super_block *sb, void *data) 2747026eb167SEric Paris { 2748026eb167SEric Paris int rc, i, *flags; 2749026eb167SEric Paris struct security_mnt_opts opts; 2750026eb167SEric Paris char *secdata, **mount_options; 2751026eb167SEric Paris struct superblock_security_struct *sbsec = sb->s_security; 2752026eb167SEric Paris 2753026eb167SEric Paris if (!(sbsec->flags & SE_SBINITIALIZED)) 2754026eb167SEric Paris return 0; 2755026eb167SEric Paris 2756026eb167SEric Paris if (!data) 2757026eb167SEric Paris return 0; 2758026eb167SEric Paris 2759026eb167SEric Paris if (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA) 2760026eb167SEric Paris return 0; 2761026eb167SEric Paris 2762026eb167SEric Paris security_init_mnt_opts(&opts); 2763026eb167SEric Paris secdata = alloc_secdata(); 2764026eb167SEric Paris if (!secdata) 2765026eb167SEric Paris return -ENOMEM; 2766026eb167SEric Paris rc = selinux_sb_copy_data(data, secdata); 2767026eb167SEric Paris if (rc) 2768026eb167SEric Paris goto out_free_secdata; 2769026eb167SEric Paris 2770026eb167SEric Paris rc = selinux_parse_opts_str(secdata, &opts); 2771026eb167SEric Paris if (rc) 2772026eb167SEric Paris goto out_free_secdata; 2773026eb167SEric Paris 2774026eb167SEric Paris mount_options = opts.mnt_opts; 2775026eb167SEric Paris flags = opts.mnt_opts_flags; 2776026eb167SEric Paris 2777026eb167SEric Paris for (i = 0; i < opts.num_mnt_opts; i++) { 2778026eb167SEric Paris u32 sid; 2779026eb167SEric Paris 278012f348b9SEric Paris if (flags[i] == SBLABEL_MNT) 2781026eb167SEric Paris continue; 278244be2f65SRasmus Villemoes rc = security_context_str_to_sid(mount_options[i], &sid, GFP_KERNEL); 2783026eb167SEric Paris if (rc) { 278444be2f65SRasmus Villemoes printk(KERN_WARNING "SELinux: security_context_str_to_sid" 278529b1deb2SLinus Torvalds "(%s) failed for (dev %s, type %s) errno=%d\n", 278629b1deb2SLinus Torvalds mount_options[i], sb->s_id, sb->s_type->name, rc); 2787026eb167SEric Paris goto out_free_opts; 2788026eb167SEric Paris } 2789026eb167SEric Paris rc = -EINVAL; 2790026eb167SEric Paris switch (flags[i]) { 2791026eb167SEric Paris case FSCONTEXT_MNT: 2792026eb167SEric Paris if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid)) 2793026eb167SEric Paris goto out_bad_option; 2794026eb167SEric Paris break; 2795026eb167SEric Paris case CONTEXT_MNT: 2796026eb167SEric Paris if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid)) 2797026eb167SEric Paris goto out_bad_option; 2798026eb167SEric Paris break; 2799026eb167SEric Paris case ROOTCONTEXT_MNT: { 2800026eb167SEric Paris struct inode_security_struct *root_isec; 280183da53c5SAndreas Gruenbacher root_isec = backing_inode_security(sb->s_root); 2802026eb167SEric Paris 2803026eb167SEric Paris if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid)) 2804026eb167SEric Paris goto out_bad_option; 2805026eb167SEric Paris break; 2806026eb167SEric Paris } 2807026eb167SEric Paris case DEFCONTEXT_MNT: 2808026eb167SEric Paris if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid)) 2809026eb167SEric Paris goto out_bad_option; 2810026eb167SEric Paris break; 2811026eb167SEric Paris default: 2812026eb167SEric Paris goto out_free_opts; 2813026eb167SEric Paris } 2814026eb167SEric Paris } 2815026eb167SEric Paris 2816026eb167SEric Paris rc = 0; 2817026eb167SEric Paris out_free_opts: 2818026eb167SEric Paris security_free_mnt_opts(&opts); 2819026eb167SEric Paris out_free_secdata: 2820026eb167SEric Paris free_secdata(secdata); 2821026eb167SEric Paris return rc; 2822026eb167SEric Paris out_bad_option: 2823026eb167SEric Paris printk(KERN_WARNING "SELinux: unable to change security options " 282429b1deb2SLinus Torvalds "during remount (dev %s, type=%s)\n", sb->s_id, 282529b1deb2SLinus Torvalds sb->s_type->name); 2826026eb167SEric Paris goto out_free_opts; 2827026eb167SEric Paris } 2828026eb167SEric Paris 282912204e24SJames Morris static int selinux_sb_kern_mount(struct super_block *sb, int flags, void *data) 28301da177e4SLinus Torvalds { 283188e67f3bSDavid Howells const struct cred *cred = current_cred(); 28322bf49690SThomas Liu struct common_audit_data ad; 28331da177e4SLinus Torvalds int rc; 28341da177e4SLinus Torvalds 28351da177e4SLinus Torvalds rc = superblock_doinit(sb, data); 28361da177e4SLinus Torvalds if (rc) 28371da177e4SLinus Torvalds return rc; 28381da177e4SLinus Torvalds 283974192246SJames Morris /* Allow all mounts performed by the kernel */ 284074192246SJames Morris if (flags & MS_KERNMOUNT) 284174192246SJames Morris return 0; 284274192246SJames Morris 284350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 2844a269434dSEric Paris ad.u.dentry = sb->s_root; 284588e67f3bSDavid Howells return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad); 28461da177e4SLinus Torvalds } 28471da177e4SLinus Torvalds 2848726c3342SDavid Howells static int selinux_sb_statfs(struct dentry *dentry) 28491da177e4SLinus Torvalds { 285088e67f3bSDavid Howells const struct cred *cred = current_cred(); 28512bf49690SThomas Liu struct common_audit_data ad; 28521da177e4SLinus Torvalds 285350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 2854a269434dSEric Paris ad.u.dentry = dentry->d_sb->s_root; 285588e67f3bSDavid Howells return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad); 28561da177e4SLinus Torvalds } 28571da177e4SLinus Torvalds 2858808d4e3cSAl Viro static int selinux_mount(const char *dev_name, 28598a04c43bSAl Viro const struct path *path, 2860808d4e3cSAl Viro const char *type, 28611da177e4SLinus Torvalds unsigned long flags, 28621da177e4SLinus Torvalds void *data) 28631da177e4SLinus Torvalds { 286488e67f3bSDavid Howells const struct cred *cred = current_cred(); 28651da177e4SLinus Torvalds 28661da177e4SLinus Torvalds if (flags & MS_REMOUNT) 2867d8c9584eSAl Viro return superblock_has_perm(cred, path->dentry->d_sb, 28681da177e4SLinus Torvalds FILESYSTEM__REMOUNT, NULL); 28691da177e4SLinus Torvalds else 28702875fa00SEric Paris return path_has_perm(cred, path, FILE__MOUNTON); 28711da177e4SLinus Torvalds } 28721da177e4SLinus Torvalds 28731da177e4SLinus Torvalds static int selinux_umount(struct vfsmount *mnt, int flags) 28741da177e4SLinus Torvalds { 287588e67f3bSDavid Howells const struct cred *cred = current_cred(); 28761da177e4SLinus Torvalds 287788e67f3bSDavid Howells return superblock_has_perm(cred, mnt->mnt_sb, 28781da177e4SLinus Torvalds FILESYSTEM__UNMOUNT, NULL); 28791da177e4SLinus Torvalds } 28801da177e4SLinus Torvalds 28811da177e4SLinus Torvalds /* inode security operations */ 28821da177e4SLinus Torvalds 28831da177e4SLinus Torvalds static int selinux_inode_alloc_security(struct inode *inode) 28841da177e4SLinus Torvalds { 28851da177e4SLinus Torvalds return inode_alloc_security(inode); 28861da177e4SLinus Torvalds } 28871da177e4SLinus Torvalds 28881da177e4SLinus Torvalds static void selinux_inode_free_security(struct inode *inode) 28891da177e4SLinus Torvalds { 28901da177e4SLinus Torvalds inode_free_security(inode); 28911da177e4SLinus Torvalds } 28921da177e4SLinus Torvalds 2893d47be3dfSDavid Quigley static int selinux_dentry_init_security(struct dentry *dentry, int mode, 28944f3ccd76SAl Viro const struct qstr *name, void **ctx, 2895d47be3dfSDavid Quigley u32 *ctxlen) 2896d47be3dfSDavid Quigley { 2897d47be3dfSDavid Quigley u32 newsid; 2898d47be3dfSDavid Quigley int rc; 2899d47be3dfSDavid Quigley 2900c957f6dfSVivek Goyal rc = selinux_determine_inode_label(current_security(), 2901c957f6dfSVivek Goyal d_inode(dentry->d_parent), name, 2902d47be3dfSDavid Quigley inode_mode_to_security_class(mode), 2903d47be3dfSDavid Quigley &newsid); 2904c3c188b2SDavid Howells if (rc) 2905d47be3dfSDavid Quigley return rc; 2906d47be3dfSDavid Quigley 2907d47be3dfSDavid Quigley return security_sid_to_context(newsid, (char **)ctx, ctxlen); 2908d47be3dfSDavid Quigley } 2909d47be3dfSDavid Quigley 2910a518b0a5SVivek Goyal static int selinux_dentry_create_files_as(struct dentry *dentry, int mode, 2911a518b0a5SVivek Goyal struct qstr *name, 2912a518b0a5SVivek Goyal const struct cred *old, 2913a518b0a5SVivek Goyal struct cred *new) 2914a518b0a5SVivek Goyal { 2915a518b0a5SVivek Goyal u32 newsid; 2916a518b0a5SVivek Goyal int rc; 2917a518b0a5SVivek Goyal struct task_security_struct *tsec; 2918a518b0a5SVivek Goyal 2919a518b0a5SVivek Goyal rc = selinux_determine_inode_label(old->security, 2920a518b0a5SVivek Goyal d_inode(dentry->d_parent), name, 2921a518b0a5SVivek Goyal inode_mode_to_security_class(mode), 2922a518b0a5SVivek Goyal &newsid); 2923a518b0a5SVivek Goyal if (rc) 2924a518b0a5SVivek Goyal return rc; 2925a518b0a5SVivek Goyal 2926a518b0a5SVivek Goyal tsec = new->security; 2927a518b0a5SVivek Goyal tsec->create_sid = newsid; 2928a518b0a5SVivek Goyal return 0; 2929a518b0a5SVivek Goyal } 2930a518b0a5SVivek Goyal 29315e41ff9eSStephen Smalley static int selinux_inode_init_security(struct inode *inode, struct inode *dir, 29329548906bSTetsuo Handa const struct qstr *qstr, 29339548906bSTetsuo Handa const char **name, 29342a7dba39SEric Paris void **value, size_t *len) 29355e41ff9eSStephen Smalley { 29365fb49870SPaul Moore const struct task_security_struct *tsec = current_security(); 29375e41ff9eSStephen Smalley struct superblock_security_struct *sbsec; 2938c0d4f464SCorentin LABBE u32 newsid, clen; 29395e41ff9eSStephen Smalley int rc; 29409548906bSTetsuo Handa char *context; 29415e41ff9eSStephen Smalley 29425e41ff9eSStephen Smalley sbsec = dir->i_sb->s_security; 29435e41ff9eSStephen Smalley 29445e41ff9eSStephen Smalley newsid = tsec->create_sid; 2945275bb41eSDavid Howells 2946c957f6dfSVivek Goyal rc = selinux_determine_inode_label(current_security(), 2947c3c188b2SDavid Howells dir, qstr, 29485e41ff9eSStephen Smalley inode_mode_to_security_class(inode->i_mode), 2949c3c188b2SDavid Howells &newsid); 2950c3c188b2SDavid Howells if (rc) 29515e41ff9eSStephen Smalley return rc; 29525e41ff9eSStephen Smalley 2953296fddf7SEric Paris /* Possibly defer initialization to selinux_complete_init. */ 29540d90a7ecSDavid P. Quigley if (sbsec->flags & SE_SBINITIALIZED) { 2955296fddf7SEric Paris struct inode_security_struct *isec = inode->i_security; 2956296fddf7SEric Paris isec->sclass = inode_mode_to_security_class(inode->i_mode); 2957296fddf7SEric Paris isec->sid = newsid; 29586f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 2959296fddf7SEric Paris } 29605e41ff9eSStephen Smalley 296112f348b9SEric Paris if (!ss_initialized || !(sbsec->flags & SBLABEL_MNT)) 296225a74f3bSStephen Smalley return -EOPNOTSUPP; 296325a74f3bSStephen Smalley 29649548906bSTetsuo Handa if (name) 29659548906bSTetsuo Handa *name = XATTR_SELINUX_SUFFIX; 29665e41ff9eSStephen Smalley 2967570bc1c2SStephen Smalley if (value && len) { 296812b29f34SStephen Smalley rc = security_sid_to_context_force(newsid, &context, &clen); 29699548906bSTetsuo Handa if (rc) 29705e41ff9eSStephen Smalley return rc; 29715e41ff9eSStephen Smalley *value = context; 2972570bc1c2SStephen Smalley *len = clen; 2973570bc1c2SStephen Smalley } 29745e41ff9eSStephen Smalley 29755e41ff9eSStephen Smalley return 0; 29765e41ff9eSStephen Smalley } 29775e41ff9eSStephen Smalley 29784acdaf27SAl Viro static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode) 29791da177e4SLinus Torvalds { 29801da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_FILE); 29811da177e4SLinus Torvalds } 29821da177e4SLinus Torvalds 29831da177e4SLinus Torvalds static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 29841da177e4SLinus Torvalds { 29851da177e4SLinus Torvalds return may_link(dir, old_dentry, MAY_LINK); 29861da177e4SLinus Torvalds } 29871da177e4SLinus Torvalds 29881da177e4SLinus Torvalds static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry) 29891da177e4SLinus Torvalds { 29901da177e4SLinus Torvalds return may_link(dir, dentry, MAY_UNLINK); 29911da177e4SLinus Torvalds } 29921da177e4SLinus Torvalds 29931da177e4SLinus Torvalds static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name) 29941da177e4SLinus Torvalds { 29951da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_LNK_FILE); 29961da177e4SLinus Torvalds } 29971da177e4SLinus Torvalds 299818bb1db3SAl Viro static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask) 29991da177e4SLinus Torvalds { 30001da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_DIR); 30011da177e4SLinus Torvalds } 30021da177e4SLinus Torvalds 30031da177e4SLinus Torvalds static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry) 30041da177e4SLinus Torvalds { 30051da177e4SLinus Torvalds return may_link(dir, dentry, MAY_RMDIR); 30061da177e4SLinus Torvalds } 30071da177e4SLinus Torvalds 30081a67aafbSAl Viro static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) 30091da177e4SLinus Torvalds { 30101da177e4SLinus Torvalds return may_create(dir, dentry, inode_mode_to_security_class(mode)); 30111da177e4SLinus Torvalds } 30121da177e4SLinus Torvalds 30131da177e4SLinus Torvalds static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry, 30141da177e4SLinus Torvalds struct inode *new_inode, struct dentry *new_dentry) 30151da177e4SLinus Torvalds { 30161da177e4SLinus Torvalds return may_rename(old_inode, old_dentry, new_inode, new_dentry); 30171da177e4SLinus Torvalds } 30181da177e4SLinus Torvalds 30191da177e4SLinus Torvalds static int selinux_inode_readlink(struct dentry *dentry) 30201da177e4SLinus Torvalds { 302188e67f3bSDavid Howells const struct cred *cred = current_cred(); 302288e67f3bSDavid Howells 30232875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__READ); 30241da177e4SLinus Torvalds } 30251da177e4SLinus Torvalds 3026bda0be7aSNeilBrown static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode, 3027bda0be7aSNeilBrown bool rcu) 30281da177e4SLinus Torvalds { 302988e67f3bSDavid Howells const struct cred *cred = current_cred(); 3030bda0be7aSNeilBrown struct common_audit_data ad; 3031bda0be7aSNeilBrown struct inode_security_struct *isec; 3032bda0be7aSNeilBrown u32 sid; 30331da177e4SLinus Torvalds 3034bda0be7aSNeilBrown validate_creds(cred); 3035bda0be7aSNeilBrown 3036bda0be7aSNeilBrown ad.type = LSM_AUDIT_DATA_DENTRY; 3037bda0be7aSNeilBrown ad.u.dentry = dentry; 3038bda0be7aSNeilBrown sid = cred_sid(cred); 30395d226df4SAndreas Gruenbacher isec = inode_security_rcu(inode, rcu); 30405d226df4SAndreas Gruenbacher if (IS_ERR(isec)) 30415d226df4SAndreas Gruenbacher return PTR_ERR(isec); 3042bda0be7aSNeilBrown 3043bda0be7aSNeilBrown return avc_has_perm_flags(sid, isec->sid, isec->sclass, FILE__READ, &ad, 3044bda0be7aSNeilBrown rcu ? MAY_NOT_BLOCK : 0); 30451da177e4SLinus Torvalds } 30461da177e4SLinus Torvalds 3047d4cf970dSEric Paris static noinline int audit_inode_permission(struct inode *inode, 3048d4cf970dSEric Paris u32 perms, u32 audited, u32 denied, 3049626b9740SStephen Smalley int result, 3050d4cf970dSEric Paris unsigned flags) 3051d4cf970dSEric Paris { 3052d4cf970dSEric Paris struct common_audit_data ad; 3053d4cf970dSEric Paris struct inode_security_struct *isec = inode->i_security; 3054d4cf970dSEric Paris int rc; 3055d4cf970dSEric Paris 305650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_INODE; 3057d4cf970dSEric Paris ad.u.inode = inode; 3058d4cf970dSEric Paris 3059d4cf970dSEric Paris rc = slow_avc_audit(current_sid(), isec->sid, isec->sclass, perms, 3060626b9740SStephen Smalley audited, denied, result, &ad, flags); 3061d4cf970dSEric Paris if (rc) 3062d4cf970dSEric Paris return rc; 3063d4cf970dSEric Paris return 0; 3064d4cf970dSEric Paris } 3065d4cf970dSEric Paris 3066e74f71ebSAl Viro static int selinux_inode_permission(struct inode *inode, int mask) 30671da177e4SLinus Torvalds { 306888e67f3bSDavid Howells const struct cred *cred = current_cred(); 3069b782e0a6SEric Paris u32 perms; 3070b782e0a6SEric Paris bool from_access; 3071cf1dd1daSAl Viro unsigned flags = mask & MAY_NOT_BLOCK; 30722e334057SEric Paris struct inode_security_struct *isec; 30732e334057SEric Paris u32 sid; 30742e334057SEric Paris struct av_decision avd; 30752e334057SEric Paris int rc, rc2; 30762e334057SEric Paris u32 audited, denied; 30771da177e4SLinus Torvalds 3078b782e0a6SEric Paris from_access = mask & MAY_ACCESS; 3079d09ca739SEric Paris mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND); 3080d09ca739SEric Paris 30811da177e4SLinus Torvalds /* No permission to check. Existence test. */ 3082b782e0a6SEric Paris if (!mask) 30831da177e4SLinus Torvalds return 0; 30841da177e4SLinus Torvalds 30852e334057SEric Paris validate_creds(cred); 3086b782e0a6SEric Paris 30872e334057SEric Paris if (unlikely(IS_PRIVATE(inode))) 30882e334057SEric Paris return 0; 3089b782e0a6SEric Paris 3090b782e0a6SEric Paris perms = file_mask_to_av(inode->i_mode, mask); 3091b782e0a6SEric Paris 30922e334057SEric Paris sid = cred_sid(cred); 30935d226df4SAndreas Gruenbacher isec = inode_security_rcu(inode, flags & MAY_NOT_BLOCK); 30945d226df4SAndreas Gruenbacher if (IS_ERR(isec)) 30955d226df4SAndreas Gruenbacher return PTR_ERR(isec); 30962e334057SEric Paris 30972e334057SEric Paris rc = avc_has_perm_noaudit(sid, isec->sid, isec->sclass, perms, 0, &avd); 30982e334057SEric Paris audited = avc_audit_required(perms, &avd, rc, 30992e334057SEric Paris from_access ? FILE__AUDIT_ACCESS : 0, 31002e334057SEric Paris &denied); 31012e334057SEric Paris if (likely(!audited)) 31022e334057SEric Paris return rc; 31032e334057SEric Paris 3104626b9740SStephen Smalley rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags); 31052e334057SEric Paris if (rc2) 31062e334057SEric Paris return rc2; 31072e334057SEric Paris return rc; 31081da177e4SLinus Torvalds } 31091da177e4SLinus Torvalds 31101da177e4SLinus Torvalds static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr) 31111da177e4SLinus Torvalds { 311288e67f3bSDavid Howells const struct cred *cred = current_cred(); 3113ccb54478SStephen Smalley struct inode *inode = d_backing_inode(dentry); 3114bc6a6008SAmerigo Wang unsigned int ia_valid = iattr->ia_valid; 311595dbf739SEric Paris __u32 av = FILE__WRITE; 31161da177e4SLinus Torvalds 3117bc6a6008SAmerigo Wang /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */ 3118bc6a6008SAmerigo Wang if (ia_valid & ATTR_FORCE) { 3119bc6a6008SAmerigo Wang ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE | 3120bc6a6008SAmerigo Wang ATTR_FORCE); 3121bc6a6008SAmerigo Wang if (!ia_valid) 31221da177e4SLinus Torvalds return 0; 3123bc6a6008SAmerigo Wang } 31241da177e4SLinus Torvalds 3125bc6a6008SAmerigo Wang if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | 3126bc6a6008SAmerigo Wang ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET)) 31272875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__SETATTR); 31281da177e4SLinus Torvalds 3129ccb54478SStephen Smalley if (selinux_policycap_openperm && 3130ccb54478SStephen Smalley inode->i_sb->s_magic != SOCKFS_MAGIC && 3131ccb54478SStephen Smalley (ia_valid & ATTR_SIZE) && 3132ccb54478SStephen Smalley !(ia_valid & ATTR_FILE)) 313395dbf739SEric Paris av |= FILE__OPEN; 313495dbf739SEric Paris 313595dbf739SEric Paris return dentry_has_perm(cred, dentry, av); 31361da177e4SLinus Torvalds } 31371da177e4SLinus Torvalds 31383f7036a0SAl Viro static int selinux_inode_getattr(const struct path *path) 31391da177e4SLinus Torvalds { 31403f7036a0SAl Viro return path_has_perm(current_cred(), path, FILE__GETATTR); 31411da177e4SLinus Torvalds } 31421da177e4SLinus Torvalds 3143db59000aSStephen Smalley static bool has_cap_mac_admin(bool audit) 3144db59000aSStephen Smalley { 3145db59000aSStephen Smalley const struct cred *cred = current_cred(); 3146db59000aSStephen Smalley int cap_audit = audit ? SECURITY_CAP_AUDIT : SECURITY_CAP_NOAUDIT; 3147db59000aSStephen Smalley 3148db59000aSStephen Smalley if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, cap_audit)) 3149db59000aSStephen Smalley return false; 3150db59000aSStephen Smalley if (cred_has_capability(cred, CAP_MAC_ADMIN, cap_audit, true)) 3151db59000aSStephen Smalley return false; 3152db59000aSStephen Smalley return true; 3153db59000aSStephen Smalley } 3154db59000aSStephen Smalley 31558f0cfa52SDavid Howells static int selinux_inode_setxattr(struct dentry *dentry, const char *name, 31568f0cfa52SDavid Howells const void *value, size_t size, int flags) 31571da177e4SLinus Torvalds { 3158c6f493d6SDavid Howells struct inode *inode = d_backing_inode(dentry); 315920cdef8dSPaul Moore struct inode_security_struct *isec; 31601da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 31612bf49690SThomas Liu struct common_audit_data ad; 3162275bb41eSDavid Howells u32 newsid, sid = current_sid(); 31631da177e4SLinus Torvalds int rc = 0; 31641da177e4SLinus Torvalds 31656b240306SEric W. Biederman if (strcmp(name, XATTR_NAME_SELINUX)) { 31666b240306SEric W. Biederman rc = cap_inode_setxattr(dentry, name, value, size, flags); 31676b240306SEric W. Biederman if (rc) 31686b240306SEric W. Biederman return rc; 31696b240306SEric W. Biederman 31706b240306SEric W. Biederman /* Not an attribute we recognize, so just check the 31716b240306SEric W. Biederman ordinary setattr permission. */ 31726b240306SEric W. Biederman return dentry_has_perm(current_cred(), dentry, FILE__SETATTR); 31736b240306SEric W. Biederman } 31741da177e4SLinus Torvalds 31751da177e4SLinus Torvalds sbsec = inode->i_sb->s_security; 317612f348b9SEric Paris if (!(sbsec->flags & SBLABEL_MNT)) 31771da177e4SLinus Torvalds return -EOPNOTSUPP; 31781da177e4SLinus Torvalds 31792e149670SSerge E. Hallyn if (!inode_owner_or_capable(inode)) 31801da177e4SLinus Torvalds return -EPERM; 31811da177e4SLinus Torvalds 318250c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 3183a269434dSEric Paris ad.u.dentry = dentry; 31841da177e4SLinus Torvalds 318520cdef8dSPaul Moore isec = backing_inode_security(dentry); 3186275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, isec->sclass, 31871da177e4SLinus Torvalds FILE__RELABELFROM, &ad); 31881da177e4SLinus Torvalds if (rc) 31891da177e4SLinus Torvalds return rc; 31901da177e4SLinus Torvalds 319152a4c640SNikolay Aleksandrov rc = security_context_to_sid(value, size, &newsid, GFP_KERNEL); 319212b29f34SStephen Smalley if (rc == -EINVAL) { 3193db59000aSStephen Smalley if (!has_cap_mac_admin(true)) { 3194d6ea83ecSEric Paris struct audit_buffer *ab; 3195d6ea83ecSEric Paris size_t audit_size; 3196d6ea83ecSEric Paris 3197d6ea83ecSEric Paris /* We strip a nul only if it is at the end, otherwise the 3198d6ea83ecSEric Paris * context contains a nul and we should audit that */ 3199e3fea3f7SAl Viro if (value) { 3200add24372SColin Ian King const char *str = value; 3201add24372SColin Ian King 3202d6ea83ecSEric Paris if (str[size - 1] == '\0') 3203d6ea83ecSEric Paris audit_size = size - 1; 3204d6ea83ecSEric Paris else 3205d6ea83ecSEric Paris audit_size = size; 3206e3fea3f7SAl Viro } else { 3207e3fea3f7SAl Viro audit_size = 0; 3208e3fea3f7SAl Viro } 3209d6ea83ecSEric Paris ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR); 3210d6ea83ecSEric Paris audit_log_format(ab, "op=setxattr invalid_context="); 3211d6ea83ecSEric Paris audit_log_n_untrustedstring(ab, value, audit_size); 3212d6ea83ecSEric Paris audit_log_end(ab); 3213d6ea83ecSEric Paris 321412b29f34SStephen Smalley return rc; 3215d6ea83ecSEric Paris } 321612b29f34SStephen Smalley rc = security_context_to_sid_force(value, size, &newsid); 321712b29f34SStephen Smalley } 32181da177e4SLinus Torvalds if (rc) 32191da177e4SLinus Torvalds return rc; 32201da177e4SLinus Torvalds 3221275bb41eSDavid Howells rc = avc_has_perm(sid, newsid, isec->sclass, 32221da177e4SLinus Torvalds FILE__RELABELTO, &ad); 32231da177e4SLinus Torvalds if (rc) 32241da177e4SLinus Torvalds return rc; 32251da177e4SLinus Torvalds 3226275bb41eSDavid Howells rc = security_validate_transition(isec->sid, newsid, sid, 32271da177e4SLinus Torvalds isec->sclass); 32281da177e4SLinus Torvalds if (rc) 32291da177e4SLinus Torvalds return rc; 32301da177e4SLinus Torvalds 32311da177e4SLinus Torvalds return avc_has_perm(newsid, 32321da177e4SLinus Torvalds sbsec->sid, 32331da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 32341da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, 32351da177e4SLinus Torvalds &ad); 32361da177e4SLinus Torvalds } 32371da177e4SLinus Torvalds 32388f0cfa52SDavid Howells static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name, 32398f0cfa52SDavid Howells const void *value, size_t size, 32408f0cfa52SDavid Howells int flags) 32411da177e4SLinus Torvalds { 3242c6f493d6SDavid Howells struct inode *inode = d_backing_inode(dentry); 324320cdef8dSPaul Moore struct inode_security_struct *isec; 32441da177e4SLinus Torvalds u32 newsid; 32451da177e4SLinus Torvalds int rc; 32461da177e4SLinus Torvalds 32471da177e4SLinus Torvalds if (strcmp(name, XATTR_NAME_SELINUX)) { 32481da177e4SLinus Torvalds /* Not an attribute we recognize, so nothing to do. */ 32491da177e4SLinus Torvalds return; 32501da177e4SLinus Torvalds } 32511da177e4SLinus Torvalds 325212b29f34SStephen Smalley rc = security_context_to_sid_force(value, size, &newsid); 32531da177e4SLinus Torvalds if (rc) { 325412b29f34SStephen Smalley printk(KERN_ERR "SELinux: unable to map context to SID" 325512b29f34SStephen Smalley "for (%s, %lu), rc=%d\n", 325612b29f34SStephen Smalley inode->i_sb->s_id, inode->i_ino, -rc); 32571da177e4SLinus Torvalds return; 32581da177e4SLinus Torvalds } 32591da177e4SLinus Torvalds 326020cdef8dSPaul Moore isec = backing_inode_security(dentry); 32619287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 3262aa9c2669SDavid Quigley isec->sclass = inode_mode_to_security_class(inode->i_mode); 32631da177e4SLinus Torvalds isec->sid = newsid; 32646f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 32659287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 3266aa9c2669SDavid Quigley 32671da177e4SLinus Torvalds return; 32681da177e4SLinus Torvalds } 32691da177e4SLinus Torvalds 32708f0cfa52SDavid Howells static int selinux_inode_getxattr(struct dentry *dentry, const char *name) 32711da177e4SLinus Torvalds { 327288e67f3bSDavid Howells const struct cred *cred = current_cred(); 327388e67f3bSDavid Howells 32742875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__GETATTR); 32751da177e4SLinus Torvalds } 32761da177e4SLinus Torvalds 32771da177e4SLinus Torvalds static int selinux_inode_listxattr(struct dentry *dentry) 32781da177e4SLinus Torvalds { 327988e67f3bSDavid Howells const struct cred *cred = current_cred(); 328088e67f3bSDavid Howells 32812875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__GETATTR); 32821da177e4SLinus Torvalds } 32831da177e4SLinus Torvalds 32848f0cfa52SDavid Howells static int selinux_inode_removexattr(struct dentry *dentry, const char *name) 32851da177e4SLinus Torvalds { 32866b240306SEric W. Biederman if (strcmp(name, XATTR_NAME_SELINUX)) { 32876b240306SEric W. Biederman int rc = cap_inode_removexattr(dentry, name); 32886b240306SEric W. Biederman if (rc) 32896b240306SEric W. Biederman return rc; 32906b240306SEric W. Biederman 32916b240306SEric W. Biederman /* Not an attribute we recognize, so just check the 32926b240306SEric W. Biederman ordinary setattr permission. */ 32936b240306SEric W. Biederman return dentry_has_perm(current_cred(), dentry, FILE__SETATTR); 32946b240306SEric W. Biederman } 32951da177e4SLinus Torvalds 32961da177e4SLinus Torvalds /* No one is allowed to remove a SELinux security label. 32971da177e4SLinus Torvalds You can change the label, but all data must be labeled. */ 32981da177e4SLinus Torvalds return -EACCES; 32991da177e4SLinus Torvalds } 33001da177e4SLinus Torvalds 3301d381d8a9SJames Morris /* 3302abc69bb6SStephen Smalley * Copy the inode security context value to the user. 3303d381d8a9SJames Morris * 3304d381d8a9SJames Morris * Permission check is handled by selinux_inode_getxattr hook. 3305d381d8a9SJames Morris */ 3306ea861dfdSAndreas Gruenbacher static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc) 33071da177e4SLinus Torvalds { 330842492594SDavid P. Quigley u32 size; 330942492594SDavid P. Quigley int error; 331042492594SDavid P. Quigley char *context = NULL; 331120cdef8dSPaul Moore struct inode_security_struct *isec; 33121da177e4SLinus Torvalds 33138c8570fbSDustin Kirkland if (strcmp(name, XATTR_SELINUX_SUFFIX)) 33148c8570fbSDustin Kirkland return -EOPNOTSUPP; 33151da177e4SLinus Torvalds 3316abc69bb6SStephen Smalley /* 3317abc69bb6SStephen Smalley * If the caller has CAP_MAC_ADMIN, then get the raw context 3318abc69bb6SStephen Smalley * value even if it is not defined by current policy; otherwise, 3319abc69bb6SStephen Smalley * use the in-core value under current policy. 3320abc69bb6SStephen Smalley * Use the non-auditing forms of the permission checks since 3321abc69bb6SStephen Smalley * getxattr may be called by unprivileged processes commonly 3322abc69bb6SStephen Smalley * and lack of permission just means that we fall back to the 3323abc69bb6SStephen Smalley * in-core context value, not a denial. 3324abc69bb6SStephen Smalley */ 332520cdef8dSPaul Moore isec = inode_security(inode); 3326db59000aSStephen Smalley if (has_cap_mac_admin(false)) 3327abc69bb6SStephen Smalley error = security_sid_to_context_force(isec->sid, &context, 3328abc69bb6SStephen Smalley &size); 3329abc69bb6SStephen Smalley else 333042492594SDavid P. Quigley error = security_sid_to_context(isec->sid, &context, &size); 333142492594SDavid P. Quigley if (error) 333242492594SDavid P. Quigley return error; 333342492594SDavid P. Quigley error = size; 333442492594SDavid P. Quigley if (alloc) { 333542492594SDavid P. Quigley *buffer = context; 333642492594SDavid P. Quigley goto out_nofree; 333742492594SDavid P. Quigley } 333842492594SDavid P. Quigley kfree(context); 333942492594SDavid P. Quigley out_nofree: 334042492594SDavid P. Quigley return error; 33411da177e4SLinus Torvalds } 33421da177e4SLinus Torvalds 33431da177e4SLinus Torvalds static int selinux_inode_setsecurity(struct inode *inode, const char *name, 33441da177e4SLinus Torvalds const void *value, size_t size, int flags) 33451da177e4SLinus Torvalds { 33462c97165bSPaul Moore struct inode_security_struct *isec = inode_security_novalidate(inode); 33471da177e4SLinus Torvalds u32 newsid; 33481da177e4SLinus Torvalds int rc; 33491da177e4SLinus Torvalds 33501da177e4SLinus Torvalds if (strcmp(name, XATTR_SELINUX_SUFFIX)) 33511da177e4SLinus Torvalds return -EOPNOTSUPP; 33521da177e4SLinus Torvalds 33531da177e4SLinus Torvalds if (!value || !size) 33541da177e4SLinus Torvalds return -EACCES; 33551da177e4SLinus Torvalds 335620ba96aeSRasmus Villemoes rc = security_context_to_sid(value, size, &newsid, GFP_KERNEL); 33571da177e4SLinus Torvalds if (rc) 33581da177e4SLinus Torvalds return rc; 33591da177e4SLinus Torvalds 33609287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 3361aa9c2669SDavid Quigley isec->sclass = inode_mode_to_security_class(inode->i_mode); 33621da177e4SLinus Torvalds isec->sid = newsid; 33636f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 33649287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 33651da177e4SLinus Torvalds return 0; 33661da177e4SLinus Torvalds } 33671da177e4SLinus Torvalds 33681da177e4SLinus Torvalds static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size) 33691da177e4SLinus Torvalds { 33701da177e4SLinus Torvalds const int len = sizeof(XATTR_NAME_SELINUX); 33711da177e4SLinus Torvalds if (buffer && len <= buffer_size) 33721da177e4SLinus Torvalds memcpy(buffer, XATTR_NAME_SELINUX, len); 33731da177e4SLinus Torvalds return len; 33741da177e4SLinus Torvalds } 33751da177e4SLinus Torvalds 3376d6335d77SAndreas Gruenbacher static void selinux_inode_getsecid(struct inode *inode, u32 *secid) 3377713a04aeSAhmed S. Darwish { 3378e817c2f3SAndreas Gruenbacher struct inode_security_struct *isec = inode_security_novalidate(inode); 3379713a04aeSAhmed S. Darwish *secid = isec->sid; 3380713a04aeSAhmed S. Darwish } 3381713a04aeSAhmed S. Darwish 338256909eb3SVivek Goyal static int selinux_inode_copy_up(struct dentry *src, struct cred **new) 338356909eb3SVivek Goyal { 338456909eb3SVivek Goyal u32 sid; 338556909eb3SVivek Goyal struct task_security_struct *tsec; 338656909eb3SVivek Goyal struct cred *new_creds = *new; 338756909eb3SVivek Goyal 338856909eb3SVivek Goyal if (new_creds == NULL) { 338956909eb3SVivek Goyal new_creds = prepare_creds(); 339056909eb3SVivek Goyal if (!new_creds) 339156909eb3SVivek Goyal return -ENOMEM; 339256909eb3SVivek Goyal } 339356909eb3SVivek Goyal 339456909eb3SVivek Goyal tsec = new_creds->security; 339556909eb3SVivek Goyal /* Get label from overlay inode and set it in create_sid */ 339656909eb3SVivek Goyal selinux_inode_getsecid(d_inode(src), &sid); 339756909eb3SVivek Goyal tsec->create_sid = sid; 339856909eb3SVivek Goyal *new = new_creds; 339956909eb3SVivek Goyal return 0; 340056909eb3SVivek Goyal } 340156909eb3SVivek Goyal 340219472b69SVivek Goyal static int selinux_inode_copy_up_xattr(const char *name) 340319472b69SVivek Goyal { 340419472b69SVivek Goyal /* The copy_up hook above sets the initial context on an inode, but we 340519472b69SVivek Goyal * don't then want to overwrite it by blindly copying all the lower 340619472b69SVivek Goyal * xattrs up. Instead, we have to filter out SELinux-related xattrs. 340719472b69SVivek Goyal */ 340819472b69SVivek Goyal if (strcmp(name, XATTR_NAME_SELINUX) == 0) 340919472b69SVivek Goyal return 1; /* Discard */ 341019472b69SVivek Goyal /* 341119472b69SVivek Goyal * Any other attribute apart from SELINUX is not claimed, supported 341219472b69SVivek Goyal * by selinux. 341319472b69SVivek Goyal */ 341419472b69SVivek Goyal return -EOPNOTSUPP; 341519472b69SVivek Goyal } 341619472b69SVivek Goyal 34171da177e4SLinus Torvalds /* file security operations */ 34181da177e4SLinus Torvalds 3419788e7dd4SYuichi Nakamura static int selinux_revalidate_file_permission(struct file *file, int mask) 34201da177e4SLinus Torvalds { 342188e67f3bSDavid Howells const struct cred *cred = current_cred(); 3422496ad9aaSAl Viro struct inode *inode = file_inode(file); 34231da177e4SLinus Torvalds 34241da177e4SLinus Torvalds /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */ 34251da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE)) 34261da177e4SLinus Torvalds mask |= MAY_APPEND; 34271da177e4SLinus Torvalds 3428389fb800SPaul Moore return file_has_perm(cred, file, 34291da177e4SLinus Torvalds file_mask_to_av(inode->i_mode, mask)); 34301da177e4SLinus Torvalds } 34311da177e4SLinus Torvalds 3432788e7dd4SYuichi Nakamura static int selinux_file_permission(struct file *file, int mask) 3433788e7dd4SYuichi Nakamura { 3434496ad9aaSAl Viro struct inode *inode = file_inode(file); 343520dda18bSStephen Smalley struct file_security_struct *fsec = file->f_security; 3436b197367eSAndreas Gruenbacher struct inode_security_struct *isec; 343720dda18bSStephen Smalley u32 sid = current_sid(); 343820dda18bSStephen Smalley 3439389fb800SPaul Moore if (!mask) 3440788e7dd4SYuichi Nakamura /* No permission to check. Existence test. */ 3441788e7dd4SYuichi Nakamura return 0; 3442788e7dd4SYuichi Nakamura 3443b197367eSAndreas Gruenbacher isec = inode_security(inode); 344420dda18bSStephen Smalley if (sid == fsec->sid && fsec->isid == isec->sid && 344520dda18bSStephen Smalley fsec->pseqno == avc_policy_seqno()) 344683d49856SEric Paris /* No change since file_open check. */ 344720dda18bSStephen Smalley return 0; 344820dda18bSStephen Smalley 3449788e7dd4SYuichi Nakamura return selinux_revalidate_file_permission(file, mask); 3450788e7dd4SYuichi Nakamura } 3451788e7dd4SYuichi Nakamura 34521da177e4SLinus Torvalds static int selinux_file_alloc_security(struct file *file) 34531da177e4SLinus Torvalds { 34541da177e4SLinus Torvalds return file_alloc_security(file); 34551da177e4SLinus Torvalds } 34561da177e4SLinus Torvalds 34571da177e4SLinus Torvalds static void selinux_file_free_security(struct file *file) 34581da177e4SLinus Torvalds { 34591da177e4SLinus Torvalds file_free_security(file); 34601da177e4SLinus Torvalds } 34611da177e4SLinus Torvalds 3462fa1aa143SJeff Vander Stoep /* 3463fa1aa143SJeff Vander Stoep * Check whether a task has the ioctl permission and cmd 3464fa1aa143SJeff Vander Stoep * operation to an inode. 3465fa1aa143SJeff Vander Stoep */ 34661d2a168aSGeliang Tang static int ioctl_has_perm(const struct cred *cred, struct file *file, 3467fa1aa143SJeff Vander Stoep u32 requested, u16 cmd) 3468fa1aa143SJeff Vander Stoep { 3469fa1aa143SJeff Vander Stoep struct common_audit_data ad; 3470fa1aa143SJeff Vander Stoep struct file_security_struct *fsec = file->f_security; 3471fa1aa143SJeff Vander Stoep struct inode *inode = file_inode(file); 347220cdef8dSPaul Moore struct inode_security_struct *isec; 3473fa1aa143SJeff Vander Stoep struct lsm_ioctlop_audit ioctl; 3474fa1aa143SJeff Vander Stoep u32 ssid = cred_sid(cred); 3475fa1aa143SJeff Vander Stoep int rc; 3476fa1aa143SJeff Vander Stoep u8 driver = cmd >> 8; 3477fa1aa143SJeff Vander Stoep u8 xperm = cmd & 0xff; 3478fa1aa143SJeff Vander Stoep 3479fa1aa143SJeff Vander Stoep ad.type = LSM_AUDIT_DATA_IOCTL_OP; 3480fa1aa143SJeff Vander Stoep ad.u.op = &ioctl; 3481fa1aa143SJeff Vander Stoep ad.u.op->cmd = cmd; 3482fa1aa143SJeff Vander Stoep ad.u.op->path = file->f_path; 3483fa1aa143SJeff Vander Stoep 3484fa1aa143SJeff Vander Stoep if (ssid != fsec->sid) { 3485fa1aa143SJeff Vander Stoep rc = avc_has_perm(ssid, fsec->sid, 3486fa1aa143SJeff Vander Stoep SECCLASS_FD, 3487fa1aa143SJeff Vander Stoep FD__USE, 3488fa1aa143SJeff Vander Stoep &ad); 3489fa1aa143SJeff Vander Stoep if (rc) 3490fa1aa143SJeff Vander Stoep goto out; 3491fa1aa143SJeff Vander Stoep } 3492fa1aa143SJeff Vander Stoep 3493fa1aa143SJeff Vander Stoep if (unlikely(IS_PRIVATE(inode))) 3494fa1aa143SJeff Vander Stoep return 0; 3495fa1aa143SJeff Vander Stoep 349620cdef8dSPaul Moore isec = inode_security(inode); 3497fa1aa143SJeff Vander Stoep rc = avc_has_extended_perms(ssid, isec->sid, isec->sclass, 3498fa1aa143SJeff Vander Stoep requested, driver, xperm, &ad); 3499fa1aa143SJeff Vander Stoep out: 3500fa1aa143SJeff Vander Stoep return rc; 3501fa1aa143SJeff Vander Stoep } 3502fa1aa143SJeff Vander Stoep 35031da177e4SLinus Torvalds static int selinux_file_ioctl(struct file *file, unsigned int cmd, 35041da177e4SLinus Torvalds unsigned long arg) 35051da177e4SLinus Torvalds { 350688e67f3bSDavid Howells const struct cred *cred = current_cred(); 35070b24dcb7SEric Paris int error = 0; 35081da177e4SLinus Torvalds 35090b24dcb7SEric Paris switch (cmd) { 35100b24dcb7SEric Paris case FIONREAD: 35110b24dcb7SEric Paris /* fall through */ 35120b24dcb7SEric Paris case FIBMAP: 35130b24dcb7SEric Paris /* fall through */ 35140b24dcb7SEric Paris case FIGETBSZ: 35150b24dcb7SEric Paris /* fall through */ 35162f99c369SAl Viro case FS_IOC_GETFLAGS: 35170b24dcb7SEric Paris /* fall through */ 35182f99c369SAl Viro case FS_IOC_GETVERSION: 35190b24dcb7SEric Paris error = file_has_perm(cred, file, FILE__GETATTR); 35200b24dcb7SEric Paris break; 35211da177e4SLinus Torvalds 35222f99c369SAl Viro case FS_IOC_SETFLAGS: 35230b24dcb7SEric Paris /* fall through */ 35242f99c369SAl Viro case FS_IOC_SETVERSION: 35250b24dcb7SEric Paris error = file_has_perm(cred, file, FILE__SETATTR); 35260b24dcb7SEric Paris break; 35270b24dcb7SEric Paris 35280b24dcb7SEric Paris /* sys_ioctl() checks */ 35290b24dcb7SEric Paris case FIONBIO: 35300b24dcb7SEric Paris /* fall through */ 35310b24dcb7SEric Paris case FIOASYNC: 35320b24dcb7SEric Paris error = file_has_perm(cred, file, 0); 35330b24dcb7SEric Paris break; 35340b24dcb7SEric Paris 35350b24dcb7SEric Paris case KDSKBENT: 35360b24dcb7SEric Paris case KDSKBSENT: 35376a9de491SEric Paris error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG, 35388e4ff6f2SStephen Smalley SECURITY_CAP_AUDIT, true); 35390b24dcb7SEric Paris break; 35400b24dcb7SEric Paris 35410b24dcb7SEric Paris /* default case assumes that the command will go 35420b24dcb7SEric Paris * to the file's ioctl() function. 35430b24dcb7SEric Paris */ 35440b24dcb7SEric Paris default: 3545fa1aa143SJeff Vander Stoep error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd); 35460b24dcb7SEric Paris } 35470b24dcb7SEric Paris return error; 35481da177e4SLinus Torvalds } 35491da177e4SLinus Torvalds 3550fcaaade1SStephen Smalley static int default_noexec; 3551fcaaade1SStephen Smalley 35521da177e4SLinus Torvalds static int file_map_prot_check(struct file *file, unsigned long prot, int shared) 35531da177e4SLinus Torvalds { 355488e67f3bSDavid Howells const struct cred *cred = current_cred(); 3555be0554c9SStephen Smalley u32 sid = cred_sid(cred); 3556d84f4f99SDavid Howells int rc = 0; 355788e67f3bSDavid Howells 3558fcaaade1SStephen Smalley if (default_noexec && 3559892e8cacSStephen Smalley (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) || 3560892e8cacSStephen Smalley (!shared && (prot & PROT_WRITE)))) { 35611da177e4SLinus Torvalds /* 35621da177e4SLinus Torvalds * We are making executable an anonymous mapping or a 35631da177e4SLinus Torvalds * private file mapping that will also be writable. 35641da177e4SLinus Torvalds * This has an additional check. 35651da177e4SLinus Torvalds */ 3566be0554c9SStephen Smalley rc = avc_has_perm(sid, sid, SECCLASS_PROCESS, 3567be0554c9SStephen Smalley PROCESS__EXECMEM, NULL); 35681da177e4SLinus Torvalds if (rc) 3569d84f4f99SDavid Howells goto error; 35701da177e4SLinus Torvalds } 35711da177e4SLinus Torvalds 35721da177e4SLinus Torvalds if (file) { 35731da177e4SLinus Torvalds /* read access is always possible with a mapping */ 35741da177e4SLinus Torvalds u32 av = FILE__READ; 35751da177e4SLinus Torvalds 35761da177e4SLinus Torvalds /* write access only matters if the mapping is shared */ 35771da177e4SLinus Torvalds if (shared && (prot & PROT_WRITE)) 35781da177e4SLinus Torvalds av |= FILE__WRITE; 35791da177e4SLinus Torvalds 35801da177e4SLinus Torvalds if (prot & PROT_EXEC) 35811da177e4SLinus Torvalds av |= FILE__EXECUTE; 35821da177e4SLinus Torvalds 358388e67f3bSDavid Howells return file_has_perm(cred, file, av); 35841da177e4SLinus Torvalds } 3585d84f4f99SDavid Howells 3586d84f4f99SDavid Howells error: 3587d84f4f99SDavid Howells return rc; 35881da177e4SLinus Torvalds } 35891da177e4SLinus Torvalds 3590e5467859SAl Viro static int selinux_mmap_addr(unsigned long addr) 35911da177e4SLinus Torvalds { 3592b1d9e6b0SCasey Schaufler int rc = 0; 359398883bfdSPaul Moore 359498883bfdSPaul Moore if (addr < CONFIG_LSM_MMAP_MIN_ADDR) { 359598883bfdSPaul Moore u32 sid = current_sid(); 359698883bfdSPaul Moore rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT, 359798883bfdSPaul Moore MEMPROTECT__MMAP_ZERO, NULL); 359898883bfdSPaul Moore } 359998883bfdSPaul Moore 360098883bfdSPaul Moore return rc; 3601e5467859SAl Viro } 36021da177e4SLinus Torvalds 3603e5467859SAl Viro static int selinux_mmap_file(struct file *file, unsigned long reqprot, 3604e5467859SAl Viro unsigned long prot, unsigned long flags) 3605e5467859SAl Viro { 36063ba4bf5fSStephen Smalley struct common_audit_data ad; 36073ba4bf5fSStephen Smalley int rc; 36083ba4bf5fSStephen Smalley 36093ba4bf5fSStephen Smalley if (file) { 36103ba4bf5fSStephen Smalley ad.type = LSM_AUDIT_DATA_FILE; 36113ba4bf5fSStephen Smalley ad.u.file = file; 36123ba4bf5fSStephen Smalley rc = inode_has_perm(current_cred(), file_inode(file), 36133ba4bf5fSStephen Smalley FILE__MAP, &ad); 36143ba4bf5fSStephen Smalley if (rc) 36153ba4bf5fSStephen Smalley return rc; 36163ba4bf5fSStephen Smalley } 36173ba4bf5fSStephen Smalley 36181da177e4SLinus Torvalds if (selinux_checkreqprot) 36191da177e4SLinus Torvalds prot = reqprot; 36201da177e4SLinus Torvalds 36211da177e4SLinus Torvalds return file_map_prot_check(file, prot, 36221da177e4SLinus Torvalds (flags & MAP_TYPE) == MAP_SHARED); 36231da177e4SLinus Torvalds } 36241da177e4SLinus Torvalds 36251da177e4SLinus Torvalds static int selinux_file_mprotect(struct vm_area_struct *vma, 36261da177e4SLinus Torvalds unsigned long reqprot, 36271da177e4SLinus Torvalds unsigned long prot) 36281da177e4SLinus Torvalds { 362988e67f3bSDavid Howells const struct cred *cred = current_cred(); 3630be0554c9SStephen Smalley u32 sid = cred_sid(cred); 36311da177e4SLinus Torvalds 36321da177e4SLinus Torvalds if (selinux_checkreqprot) 36331da177e4SLinus Torvalds prot = reqprot; 36341da177e4SLinus Torvalds 3635fcaaade1SStephen Smalley if (default_noexec && 3636fcaaade1SStephen Smalley (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) { 3637d541bbeeSJames Morris int rc = 0; 3638db4c9641SStephen Smalley if (vma->vm_start >= vma->vm_mm->start_brk && 3639db4c9641SStephen Smalley vma->vm_end <= vma->vm_mm->brk) { 3640be0554c9SStephen Smalley rc = avc_has_perm(sid, sid, SECCLASS_PROCESS, 3641be0554c9SStephen Smalley PROCESS__EXECHEAP, NULL); 3642db4c9641SStephen Smalley } else if (!vma->vm_file && 3643c2316dbfSStephen Smalley ((vma->vm_start <= vma->vm_mm->start_stack && 3644c2316dbfSStephen Smalley vma->vm_end >= vma->vm_mm->start_stack) || 3645d17af505SAndy Lutomirski vma_is_stack_for_current(vma))) { 3646be0554c9SStephen Smalley rc = avc_has_perm(sid, sid, SECCLASS_PROCESS, 3647be0554c9SStephen Smalley PROCESS__EXECSTACK, NULL); 3648db4c9641SStephen Smalley } else if (vma->vm_file && vma->anon_vma) { 3649db4c9641SStephen Smalley /* 3650db4c9641SStephen Smalley * We are making executable a file mapping that has 3651db4c9641SStephen Smalley * had some COW done. Since pages might have been 3652db4c9641SStephen Smalley * written, check ability to execute the possibly 3653db4c9641SStephen Smalley * modified content. This typically should only 3654db4c9641SStephen Smalley * occur for text relocations. 3655db4c9641SStephen Smalley */ 3656d84f4f99SDavid Howells rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD); 3657db4c9641SStephen Smalley } 36586b992197SLorenzo Hernandez García-Hierro if (rc) 36596b992197SLorenzo Hernandez García-Hierro return rc; 36606b992197SLorenzo Hernandez García-Hierro } 36611da177e4SLinus Torvalds 36621da177e4SLinus Torvalds return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED); 36631da177e4SLinus Torvalds } 36641da177e4SLinus Torvalds 36651da177e4SLinus Torvalds static int selinux_file_lock(struct file *file, unsigned int cmd) 36661da177e4SLinus Torvalds { 366788e67f3bSDavid Howells const struct cred *cred = current_cred(); 366888e67f3bSDavid Howells 366988e67f3bSDavid Howells return file_has_perm(cred, file, FILE__LOCK); 36701da177e4SLinus Torvalds } 36711da177e4SLinus Torvalds 36721da177e4SLinus Torvalds static int selinux_file_fcntl(struct file *file, unsigned int cmd, 36731da177e4SLinus Torvalds unsigned long arg) 36741da177e4SLinus Torvalds { 367588e67f3bSDavid Howells const struct cred *cred = current_cred(); 36761da177e4SLinus Torvalds int err = 0; 36771da177e4SLinus Torvalds 36781da177e4SLinus Torvalds switch (cmd) { 36791da177e4SLinus Torvalds case F_SETFL: 36801da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) { 368188e67f3bSDavid Howells err = file_has_perm(cred, file, FILE__WRITE); 36821da177e4SLinus Torvalds break; 36831da177e4SLinus Torvalds } 36841da177e4SLinus Torvalds /* fall through */ 36851da177e4SLinus Torvalds case F_SETOWN: 36861da177e4SLinus Torvalds case F_SETSIG: 36871da177e4SLinus Torvalds case F_GETFL: 36881da177e4SLinus Torvalds case F_GETOWN: 36891da177e4SLinus Torvalds case F_GETSIG: 36901d151c33SCyrill Gorcunov case F_GETOWNER_UIDS: 36911da177e4SLinus Torvalds /* Just check FD__USE permission */ 369288e67f3bSDavid Howells err = file_has_perm(cred, file, 0); 36931da177e4SLinus Torvalds break; 36941da177e4SLinus Torvalds case F_GETLK: 36951da177e4SLinus Torvalds case F_SETLK: 36961da177e4SLinus Torvalds case F_SETLKW: 36970d3f7a2dSJeff Layton case F_OFD_GETLK: 36980d3f7a2dSJeff Layton case F_OFD_SETLK: 36990d3f7a2dSJeff Layton case F_OFD_SETLKW: 37001da177e4SLinus Torvalds #if BITS_PER_LONG == 32 37011da177e4SLinus Torvalds case F_GETLK64: 37021da177e4SLinus Torvalds case F_SETLK64: 37031da177e4SLinus Torvalds case F_SETLKW64: 37041da177e4SLinus Torvalds #endif 370588e67f3bSDavid Howells err = file_has_perm(cred, file, FILE__LOCK); 37061da177e4SLinus Torvalds break; 37071da177e4SLinus Torvalds } 37081da177e4SLinus Torvalds 37091da177e4SLinus Torvalds return err; 37101da177e4SLinus Torvalds } 37111da177e4SLinus Torvalds 3712e0b93eddSJeff Layton static void selinux_file_set_fowner(struct file *file) 37131da177e4SLinus Torvalds { 37141da177e4SLinus Torvalds struct file_security_struct *fsec; 37151da177e4SLinus Torvalds 37161da177e4SLinus Torvalds fsec = file->f_security; 3717275bb41eSDavid Howells fsec->fown_sid = current_sid(); 37181da177e4SLinus Torvalds } 37191da177e4SLinus Torvalds 37201da177e4SLinus Torvalds static int selinux_file_send_sigiotask(struct task_struct *tsk, 37211da177e4SLinus Torvalds struct fown_struct *fown, int signum) 37221da177e4SLinus Torvalds { 37231da177e4SLinus Torvalds struct file *file; 372465c90bcaSStephen Smalley u32 sid = task_sid(tsk); 37251da177e4SLinus Torvalds u32 perm; 37261da177e4SLinus Torvalds struct file_security_struct *fsec; 37271da177e4SLinus Torvalds 37281da177e4SLinus Torvalds /* struct fown_struct is never outside the context of a struct file */ 3729b385a144SRobert P. J. Day file = container_of(fown, struct file, f_owner); 37301da177e4SLinus Torvalds 37311da177e4SLinus Torvalds fsec = file->f_security; 37321da177e4SLinus Torvalds 37331da177e4SLinus Torvalds if (!signum) 37341da177e4SLinus Torvalds perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */ 37351da177e4SLinus Torvalds else 37361da177e4SLinus Torvalds perm = signal_to_av(signum); 37371da177e4SLinus Torvalds 3738275bb41eSDavid Howells return avc_has_perm(fsec->fown_sid, sid, 37391da177e4SLinus Torvalds SECCLASS_PROCESS, perm, NULL); 37401da177e4SLinus Torvalds } 37411da177e4SLinus Torvalds 37421da177e4SLinus Torvalds static int selinux_file_receive(struct file *file) 37431da177e4SLinus Torvalds { 374488e67f3bSDavid Howells const struct cred *cred = current_cred(); 374588e67f3bSDavid Howells 374688e67f3bSDavid Howells return file_has_perm(cred, file, file_to_av(file)); 37471da177e4SLinus Torvalds } 37481da177e4SLinus Torvalds 374983d49856SEric Paris static int selinux_file_open(struct file *file, const struct cred *cred) 3750788e7dd4SYuichi Nakamura { 3751788e7dd4SYuichi Nakamura struct file_security_struct *fsec; 3752788e7dd4SYuichi Nakamura struct inode_security_struct *isec; 3753d84f4f99SDavid Howells 3754788e7dd4SYuichi Nakamura fsec = file->f_security; 375583da53c5SAndreas Gruenbacher isec = inode_security(file_inode(file)); 3756788e7dd4SYuichi Nakamura /* 3757788e7dd4SYuichi Nakamura * Save inode label and policy sequence number 3758788e7dd4SYuichi Nakamura * at open-time so that selinux_file_permission 3759788e7dd4SYuichi Nakamura * can determine whether revalidation is necessary. 3760788e7dd4SYuichi Nakamura * Task label is already saved in the file security 3761788e7dd4SYuichi Nakamura * struct as its SID. 3762788e7dd4SYuichi Nakamura */ 3763788e7dd4SYuichi Nakamura fsec->isid = isec->sid; 3764788e7dd4SYuichi Nakamura fsec->pseqno = avc_policy_seqno(); 3765788e7dd4SYuichi Nakamura /* 3766788e7dd4SYuichi Nakamura * Since the inode label or policy seqno may have changed 3767788e7dd4SYuichi Nakamura * between the selinux_inode_permission check and the saving 3768788e7dd4SYuichi Nakamura * of state above, recheck that access is still permitted. 3769788e7dd4SYuichi Nakamura * Otherwise, access might never be revalidated against the 3770788e7dd4SYuichi Nakamura * new inode label or new policy. 3771788e7dd4SYuichi Nakamura * This check is not redundant - do not remove. 3772788e7dd4SYuichi Nakamura */ 377313f8e981SDavid Howells return file_path_has_perm(cred, file, open_file_to_av(file)); 3774788e7dd4SYuichi Nakamura } 3775788e7dd4SYuichi Nakamura 37761da177e4SLinus Torvalds /* task security operations */ 37771da177e4SLinus Torvalds 3778a79be238STetsuo Handa static int selinux_task_alloc(struct task_struct *task, 3779a79be238STetsuo Handa unsigned long clone_flags) 37801da177e4SLinus Torvalds { 3781be0554c9SStephen Smalley u32 sid = current_sid(); 3782be0554c9SStephen Smalley 3783be0554c9SStephen Smalley return avc_has_perm(sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL); 37841da177e4SLinus Torvalds } 37851da177e4SLinus Torvalds 3786f1752eecSDavid Howells /* 3787ee18d64cSDavid Howells * allocate the SELinux part of blank credentials 3788ee18d64cSDavid Howells */ 3789ee18d64cSDavid Howells static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp) 3790ee18d64cSDavid Howells { 3791ee18d64cSDavid Howells struct task_security_struct *tsec; 3792ee18d64cSDavid Howells 3793ee18d64cSDavid Howells tsec = kzalloc(sizeof(struct task_security_struct), gfp); 3794ee18d64cSDavid Howells if (!tsec) 3795ee18d64cSDavid Howells return -ENOMEM; 3796ee18d64cSDavid Howells 3797ee18d64cSDavid Howells cred->security = tsec; 3798ee18d64cSDavid Howells return 0; 3799ee18d64cSDavid Howells } 3800ee18d64cSDavid Howells 3801ee18d64cSDavid Howells /* 3802f1752eecSDavid Howells * detach and free the LSM part of a set of credentials 3803f1752eecSDavid Howells */ 3804f1752eecSDavid Howells static void selinux_cred_free(struct cred *cred) 38051da177e4SLinus Torvalds { 3806f1752eecSDavid Howells struct task_security_struct *tsec = cred->security; 3807e0e81739SDavid Howells 38082edeaa34STetsuo Handa /* 38092edeaa34STetsuo Handa * cred->security == NULL if security_cred_alloc_blank() or 38102edeaa34STetsuo Handa * security_prepare_creds() returned an error. 38112edeaa34STetsuo Handa */ 38122edeaa34STetsuo Handa BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE); 3813e0e81739SDavid Howells cred->security = (void *) 0x7UL; 3814f1752eecSDavid Howells kfree(tsec); 38151da177e4SLinus Torvalds } 38161da177e4SLinus Torvalds 3817d84f4f99SDavid Howells /* 3818d84f4f99SDavid Howells * prepare a new set of credentials for modification 3819d84f4f99SDavid Howells */ 3820d84f4f99SDavid Howells static int selinux_cred_prepare(struct cred *new, const struct cred *old, 3821d84f4f99SDavid Howells gfp_t gfp) 3822d84f4f99SDavid Howells { 3823d84f4f99SDavid Howells const struct task_security_struct *old_tsec; 3824d84f4f99SDavid Howells struct task_security_struct *tsec; 3825d84f4f99SDavid Howells 3826d84f4f99SDavid Howells old_tsec = old->security; 3827d84f4f99SDavid Howells 3828d84f4f99SDavid Howells tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp); 3829d84f4f99SDavid Howells if (!tsec) 3830d84f4f99SDavid Howells return -ENOMEM; 3831d84f4f99SDavid Howells 3832d84f4f99SDavid Howells new->security = tsec; 3833d84f4f99SDavid Howells return 0; 3834d84f4f99SDavid Howells } 3835d84f4f99SDavid Howells 3836d84f4f99SDavid Howells /* 3837ee18d64cSDavid Howells * transfer the SELinux data to a blank set of creds 3838ee18d64cSDavid Howells */ 3839ee18d64cSDavid Howells static void selinux_cred_transfer(struct cred *new, const struct cred *old) 3840ee18d64cSDavid Howells { 3841ee18d64cSDavid Howells const struct task_security_struct *old_tsec = old->security; 3842ee18d64cSDavid Howells struct task_security_struct *tsec = new->security; 3843ee18d64cSDavid Howells 3844ee18d64cSDavid Howells *tsec = *old_tsec; 3845ee18d64cSDavid Howells } 3846ee18d64cSDavid Howells 3847ee18d64cSDavid Howells /* 38483a3b7ce9SDavid Howells * set the security data for a kernel service 38493a3b7ce9SDavid Howells * - all the creation contexts are set to unlabelled 38503a3b7ce9SDavid Howells */ 38513a3b7ce9SDavid Howells static int selinux_kernel_act_as(struct cred *new, u32 secid) 38523a3b7ce9SDavid Howells { 38533a3b7ce9SDavid Howells struct task_security_struct *tsec = new->security; 38543a3b7ce9SDavid Howells u32 sid = current_sid(); 38553a3b7ce9SDavid Howells int ret; 38563a3b7ce9SDavid Howells 38573a3b7ce9SDavid Howells ret = avc_has_perm(sid, secid, 38583a3b7ce9SDavid Howells SECCLASS_KERNEL_SERVICE, 38593a3b7ce9SDavid Howells KERNEL_SERVICE__USE_AS_OVERRIDE, 38603a3b7ce9SDavid Howells NULL); 38613a3b7ce9SDavid Howells if (ret == 0) { 38623a3b7ce9SDavid Howells tsec->sid = secid; 38633a3b7ce9SDavid Howells tsec->create_sid = 0; 38643a3b7ce9SDavid Howells tsec->keycreate_sid = 0; 38653a3b7ce9SDavid Howells tsec->sockcreate_sid = 0; 38663a3b7ce9SDavid Howells } 38673a3b7ce9SDavid Howells return ret; 38683a3b7ce9SDavid Howells } 38693a3b7ce9SDavid Howells 38703a3b7ce9SDavid Howells /* 38713a3b7ce9SDavid Howells * set the file creation context in a security record to the same as the 38723a3b7ce9SDavid Howells * objective context of the specified inode 38733a3b7ce9SDavid Howells */ 38743a3b7ce9SDavid Howells static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode) 38753a3b7ce9SDavid Howells { 387683da53c5SAndreas Gruenbacher struct inode_security_struct *isec = inode_security(inode); 38773a3b7ce9SDavid Howells struct task_security_struct *tsec = new->security; 38783a3b7ce9SDavid Howells u32 sid = current_sid(); 38793a3b7ce9SDavid Howells int ret; 38803a3b7ce9SDavid Howells 38813a3b7ce9SDavid Howells ret = avc_has_perm(sid, isec->sid, 38823a3b7ce9SDavid Howells SECCLASS_KERNEL_SERVICE, 38833a3b7ce9SDavid Howells KERNEL_SERVICE__CREATE_FILES_AS, 38843a3b7ce9SDavid Howells NULL); 38853a3b7ce9SDavid Howells 38863a3b7ce9SDavid Howells if (ret == 0) 38873a3b7ce9SDavid Howells tsec->create_sid = isec->sid; 3888ef57471aSDavid Howells return ret; 38893a3b7ce9SDavid Howells } 38903a3b7ce9SDavid Howells 3891dd8dbf2eSEric Paris static int selinux_kernel_module_request(char *kmod_name) 389225354c4fSEric Paris { 3893dd8dbf2eSEric Paris struct common_audit_data ad; 3894dd8dbf2eSEric Paris 389550c205f5SEric Paris ad.type = LSM_AUDIT_DATA_KMOD; 3896dd8dbf2eSEric Paris ad.u.kmod_name = kmod_name; 3897dd8dbf2eSEric Paris 3898be0554c9SStephen Smalley return avc_has_perm(current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM, 3899dd8dbf2eSEric Paris SYSTEM__MODULE_REQUEST, &ad); 390025354c4fSEric Paris } 390125354c4fSEric Paris 390261d612eaSJeff Vander Stoep static int selinux_kernel_module_from_file(struct file *file) 390361d612eaSJeff Vander Stoep { 390461d612eaSJeff Vander Stoep struct common_audit_data ad; 390561d612eaSJeff Vander Stoep struct inode_security_struct *isec; 390661d612eaSJeff Vander Stoep struct file_security_struct *fsec; 390761d612eaSJeff Vander Stoep u32 sid = current_sid(); 390861d612eaSJeff Vander Stoep int rc; 390961d612eaSJeff Vander Stoep 391061d612eaSJeff Vander Stoep /* init_module */ 391161d612eaSJeff Vander Stoep if (file == NULL) 391261d612eaSJeff Vander Stoep return avc_has_perm(sid, sid, SECCLASS_SYSTEM, 391361d612eaSJeff Vander Stoep SYSTEM__MODULE_LOAD, NULL); 391461d612eaSJeff Vander Stoep 391561d612eaSJeff Vander Stoep /* finit_module */ 391620cdef8dSPaul Moore 391743af5de7SVivek Goyal ad.type = LSM_AUDIT_DATA_FILE; 391843af5de7SVivek Goyal ad.u.file = file; 391961d612eaSJeff Vander Stoep 392061d612eaSJeff Vander Stoep fsec = file->f_security; 392161d612eaSJeff Vander Stoep if (sid != fsec->sid) { 392261d612eaSJeff Vander Stoep rc = avc_has_perm(sid, fsec->sid, SECCLASS_FD, FD__USE, &ad); 392361d612eaSJeff Vander Stoep if (rc) 392461d612eaSJeff Vander Stoep return rc; 392561d612eaSJeff Vander Stoep } 392661d612eaSJeff Vander Stoep 392720cdef8dSPaul Moore isec = inode_security(file_inode(file)); 392861d612eaSJeff Vander Stoep return avc_has_perm(sid, isec->sid, SECCLASS_SYSTEM, 392961d612eaSJeff Vander Stoep SYSTEM__MODULE_LOAD, &ad); 393061d612eaSJeff Vander Stoep } 393161d612eaSJeff Vander Stoep 393261d612eaSJeff Vander Stoep static int selinux_kernel_read_file(struct file *file, 393361d612eaSJeff Vander Stoep enum kernel_read_file_id id) 393461d612eaSJeff Vander Stoep { 393561d612eaSJeff Vander Stoep int rc = 0; 393661d612eaSJeff Vander Stoep 393761d612eaSJeff Vander Stoep switch (id) { 393861d612eaSJeff Vander Stoep case READING_MODULE: 393961d612eaSJeff Vander Stoep rc = selinux_kernel_module_from_file(file); 394061d612eaSJeff Vander Stoep break; 394161d612eaSJeff Vander Stoep default: 394261d612eaSJeff Vander Stoep break; 394361d612eaSJeff Vander Stoep } 394461d612eaSJeff Vander Stoep 394561d612eaSJeff Vander Stoep return rc; 394661d612eaSJeff Vander Stoep } 394761d612eaSJeff Vander Stoep 39481da177e4SLinus Torvalds static int selinux_task_setpgid(struct task_struct *p, pid_t pgid) 39491da177e4SLinus Torvalds { 3950be0554c9SStephen Smalley return avc_has_perm(current_sid(), task_sid(p), SECCLASS_PROCESS, 3951be0554c9SStephen Smalley PROCESS__SETPGID, NULL); 39521da177e4SLinus Torvalds } 39531da177e4SLinus Torvalds 39541da177e4SLinus Torvalds static int selinux_task_getpgid(struct task_struct *p) 39551da177e4SLinus Torvalds { 3956be0554c9SStephen Smalley return avc_has_perm(current_sid(), task_sid(p), SECCLASS_PROCESS, 3957be0554c9SStephen Smalley PROCESS__GETPGID, NULL); 39581da177e4SLinus Torvalds } 39591da177e4SLinus Torvalds 39601da177e4SLinus Torvalds static int selinux_task_getsid(struct task_struct *p) 39611da177e4SLinus Torvalds { 3962be0554c9SStephen Smalley return avc_has_perm(current_sid(), task_sid(p), SECCLASS_PROCESS, 3963be0554c9SStephen Smalley PROCESS__GETSESSION, NULL); 39641da177e4SLinus Torvalds } 39651da177e4SLinus Torvalds 3966f9008e4cSDavid Quigley static void selinux_task_getsecid(struct task_struct *p, u32 *secid) 3967f9008e4cSDavid Quigley { 3968275bb41eSDavid Howells *secid = task_sid(p); 3969f9008e4cSDavid Quigley } 3970f9008e4cSDavid Quigley 39711da177e4SLinus Torvalds static int selinux_task_setnice(struct task_struct *p, int nice) 39721da177e4SLinus Torvalds { 3973be0554c9SStephen Smalley return avc_has_perm(current_sid(), task_sid(p), SECCLASS_PROCESS, 3974be0554c9SStephen Smalley PROCESS__SETSCHED, NULL); 39751da177e4SLinus Torvalds } 39761da177e4SLinus Torvalds 397703e68060SJames Morris static int selinux_task_setioprio(struct task_struct *p, int ioprio) 397803e68060SJames Morris { 3979be0554c9SStephen Smalley return avc_has_perm(current_sid(), task_sid(p), SECCLASS_PROCESS, 3980be0554c9SStephen Smalley PROCESS__SETSCHED, NULL); 398103e68060SJames Morris } 398203e68060SJames Morris 3983a1836a42SDavid Quigley static int selinux_task_getioprio(struct task_struct *p) 3984a1836a42SDavid Quigley { 3985be0554c9SStephen Smalley return avc_has_perm(current_sid(), task_sid(p), SECCLASS_PROCESS, 3986be0554c9SStephen Smalley PROCESS__GETSCHED, NULL); 3987a1836a42SDavid Quigley } 3988a1836a42SDavid Quigley 39894298555dSCorentin LABBE static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred, 3990791ec491SStephen Smalley unsigned int flags) 3991791ec491SStephen Smalley { 3992791ec491SStephen Smalley u32 av = 0; 3993791ec491SStephen Smalley 399484e6885eSStephen Smalley if (!flags) 399584e6885eSStephen Smalley return 0; 3996791ec491SStephen Smalley if (flags & LSM_PRLIMIT_WRITE) 3997791ec491SStephen Smalley av |= PROCESS__SETRLIMIT; 3998791ec491SStephen Smalley if (flags & LSM_PRLIMIT_READ) 3999791ec491SStephen Smalley av |= PROCESS__GETRLIMIT; 4000791ec491SStephen Smalley return avc_has_perm(cred_sid(cred), cred_sid(tcred), 4001791ec491SStephen Smalley SECCLASS_PROCESS, av, NULL); 4002791ec491SStephen Smalley } 4003791ec491SStephen Smalley 40048fd00b4dSJiri Slaby static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource, 40058fd00b4dSJiri Slaby struct rlimit *new_rlim) 40061da177e4SLinus Torvalds { 40078fd00b4dSJiri Slaby struct rlimit *old_rlim = p->signal->rlim + resource; 40081da177e4SLinus Torvalds 40091da177e4SLinus Torvalds /* Control the ability to change the hard limit (whether 40101da177e4SLinus Torvalds lowering or raising it), so that the hard limit can 40111da177e4SLinus Torvalds later be used as a safe reset point for the soft limit 4012d84f4f99SDavid Howells upon context transitions. See selinux_bprm_committing_creds. */ 40131da177e4SLinus Torvalds if (old_rlim->rlim_max != new_rlim->rlim_max) 4014be0554c9SStephen Smalley return avc_has_perm(current_sid(), task_sid(p), 4015be0554c9SStephen Smalley SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL); 40161da177e4SLinus Torvalds 40171da177e4SLinus Torvalds return 0; 40181da177e4SLinus Torvalds } 40191da177e4SLinus Torvalds 4020b0ae1981SKOSAKI Motohiro static int selinux_task_setscheduler(struct task_struct *p) 40211da177e4SLinus Torvalds { 4022be0554c9SStephen Smalley return avc_has_perm(current_sid(), task_sid(p), SECCLASS_PROCESS, 4023be0554c9SStephen Smalley PROCESS__SETSCHED, NULL); 40241da177e4SLinus Torvalds } 40251da177e4SLinus Torvalds 40261da177e4SLinus Torvalds static int selinux_task_getscheduler(struct task_struct *p) 40271da177e4SLinus Torvalds { 4028be0554c9SStephen Smalley return avc_has_perm(current_sid(), task_sid(p), SECCLASS_PROCESS, 4029be0554c9SStephen Smalley PROCESS__GETSCHED, NULL); 40301da177e4SLinus Torvalds } 40311da177e4SLinus Torvalds 403235601547SDavid Quigley static int selinux_task_movememory(struct task_struct *p) 403335601547SDavid Quigley { 4034be0554c9SStephen Smalley return avc_has_perm(current_sid(), task_sid(p), SECCLASS_PROCESS, 4035be0554c9SStephen Smalley PROCESS__SETSCHED, NULL); 403635601547SDavid Quigley } 403735601547SDavid Quigley 4038f9008e4cSDavid Quigley static int selinux_task_kill(struct task_struct *p, struct siginfo *info, 4039*6b4f3d01SStephen Smalley int sig, const struct cred *cred) 40401da177e4SLinus Torvalds { 4041*6b4f3d01SStephen Smalley u32 secid; 40421da177e4SLinus Torvalds u32 perm; 40431da177e4SLinus Torvalds 40441da177e4SLinus Torvalds if (!sig) 40451da177e4SLinus Torvalds perm = PROCESS__SIGNULL; /* null signal; existence test */ 40461da177e4SLinus Torvalds else 40471da177e4SLinus Torvalds perm = signal_to_av(sig); 4048*6b4f3d01SStephen Smalley if (!cred) 4049be0554c9SStephen Smalley secid = current_sid(); 4050*6b4f3d01SStephen Smalley else 4051*6b4f3d01SStephen Smalley secid = cred_sid(cred); 4052be0554c9SStephen Smalley return avc_has_perm(secid, task_sid(p), SECCLASS_PROCESS, perm, NULL); 40531da177e4SLinus Torvalds } 40541da177e4SLinus Torvalds 40551da177e4SLinus Torvalds static void selinux_task_to_inode(struct task_struct *p, 40561da177e4SLinus Torvalds struct inode *inode) 40571da177e4SLinus Torvalds { 40581da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 4059275bb41eSDavid Howells u32 sid = task_sid(p); 40601da177e4SLinus Torvalds 40619287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 4062db978da8SAndreas Gruenbacher isec->sclass = inode_mode_to_security_class(inode->i_mode); 4063275bb41eSDavid Howells isec->sid = sid; 40646f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 40659287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 40661da177e4SLinus Torvalds } 40671da177e4SLinus Torvalds 40681da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 406967f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv4(struct sk_buff *skb, 40702bf49690SThomas Liu struct common_audit_data *ad, u8 *proto) 40711da177e4SLinus Torvalds { 40721da177e4SLinus Torvalds int offset, ihlen, ret = -EINVAL; 40731da177e4SLinus Torvalds struct iphdr _iph, *ih; 40741da177e4SLinus Torvalds 4075bbe735e4SArnaldo Carvalho de Melo offset = skb_network_offset(skb); 40761da177e4SLinus Torvalds ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph); 40771da177e4SLinus Torvalds if (ih == NULL) 40781da177e4SLinus Torvalds goto out; 40791da177e4SLinus Torvalds 40801da177e4SLinus Torvalds ihlen = ih->ihl * 4; 40811da177e4SLinus Torvalds if (ihlen < sizeof(_iph)) 40821da177e4SLinus Torvalds goto out; 40831da177e4SLinus Torvalds 408448c62af6SEric Paris ad->u.net->v4info.saddr = ih->saddr; 408548c62af6SEric Paris ad->u.net->v4info.daddr = ih->daddr; 40861da177e4SLinus Torvalds ret = 0; 40871da177e4SLinus Torvalds 408867f83cbfSVenkat Yekkirala if (proto) 408967f83cbfSVenkat Yekkirala *proto = ih->protocol; 409067f83cbfSVenkat Yekkirala 40911da177e4SLinus Torvalds switch (ih->protocol) { 40921da177e4SLinus Torvalds case IPPROTO_TCP: { 40931da177e4SLinus Torvalds struct tcphdr _tcph, *th; 40941da177e4SLinus Torvalds 40951da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 40961da177e4SLinus Torvalds break; 40971da177e4SLinus Torvalds 40981da177e4SLinus Torvalds offset += ihlen; 40991da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 41001da177e4SLinus Torvalds if (th == NULL) 41011da177e4SLinus Torvalds break; 41021da177e4SLinus Torvalds 410348c62af6SEric Paris ad->u.net->sport = th->source; 410448c62af6SEric Paris ad->u.net->dport = th->dest; 41051da177e4SLinus Torvalds break; 41061da177e4SLinus Torvalds } 41071da177e4SLinus Torvalds 41081da177e4SLinus Torvalds case IPPROTO_UDP: { 41091da177e4SLinus Torvalds struct udphdr _udph, *uh; 41101da177e4SLinus Torvalds 41111da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 41121da177e4SLinus Torvalds break; 41131da177e4SLinus Torvalds 41141da177e4SLinus Torvalds offset += ihlen; 41151da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 41161da177e4SLinus Torvalds if (uh == NULL) 41171da177e4SLinus Torvalds break; 41181da177e4SLinus Torvalds 411948c62af6SEric Paris ad->u.net->sport = uh->source; 412048c62af6SEric Paris ad->u.net->dport = uh->dest; 41211da177e4SLinus Torvalds break; 41221da177e4SLinus Torvalds } 41231da177e4SLinus Torvalds 41242ee92d46SJames Morris case IPPROTO_DCCP: { 41252ee92d46SJames Morris struct dccp_hdr _dccph, *dh; 41262ee92d46SJames Morris 41272ee92d46SJames Morris if (ntohs(ih->frag_off) & IP_OFFSET) 41282ee92d46SJames Morris break; 41292ee92d46SJames Morris 41302ee92d46SJames Morris offset += ihlen; 41312ee92d46SJames Morris dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); 41322ee92d46SJames Morris if (dh == NULL) 41332ee92d46SJames Morris break; 41342ee92d46SJames Morris 413548c62af6SEric Paris ad->u.net->sport = dh->dccph_sport; 413648c62af6SEric Paris ad->u.net->dport = dh->dccph_dport; 41372ee92d46SJames Morris break; 41382ee92d46SJames Morris } 41392ee92d46SJames Morris 41401da177e4SLinus Torvalds default: 41411da177e4SLinus Torvalds break; 41421da177e4SLinus Torvalds } 41431da177e4SLinus Torvalds out: 41441da177e4SLinus Torvalds return ret; 41451da177e4SLinus Torvalds } 41461da177e4SLinus Torvalds 41471a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 41481da177e4SLinus Torvalds 41491da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 415067f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv6(struct sk_buff *skb, 41512bf49690SThomas Liu struct common_audit_data *ad, u8 *proto) 41521da177e4SLinus Torvalds { 41531da177e4SLinus Torvalds u8 nexthdr; 41541da177e4SLinus Torvalds int ret = -EINVAL, offset; 41551da177e4SLinus Torvalds struct ipv6hdr _ipv6h, *ip6; 415675f2811cSJesse Gross __be16 frag_off; 41571da177e4SLinus Torvalds 4158bbe735e4SArnaldo Carvalho de Melo offset = skb_network_offset(skb); 41591da177e4SLinus Torvalds ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h); 41601da177e4SLinus Torvalds if (ip6 == NULL) 41611da177e4SLinus Torvalds goto out; 41621da177e4SLinus Torvalds 416348c62af6SEric Paris ad->u.net->v6info.saddr = ip6->saddr; 416448c62af6SEric Paris ad->u.net->v6info.daddr = ip6->daddr; 41651da177e4SLinus Torvalds ret = 0; 41661da177e4SLinus Torvalds 41671da177e4SLinus Torvalds nexthdr = ip6->nexthdr; 41681da177e4SLinus Torvalds offset += sizeof(_ipv6h); 416975f2811cSJesse Gross offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off); 41701da177e4SLinus Torvalds if (offset < 0) 41711da177e4SLinus Torvalds goto out; 41721da177e4SLinus Torvalds 417367f83cbfSVenkat Yekkirala if (proto) 417467f83cbfSVenkat Yekkirala *proto = nexthdr; 417567f83cbfSVenkat Yekkirala 41761da177e4SLinus Torvalds switch (nexthdr) { 41771da177e4SLinus Torvalds case IPPROTO_TCP: { 41781da177e4SLinus Torvalds struct tcphdr _tcph, *th; 41791da177e4SLinus Torvalds 41801da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 41811da177e4SLinus Torvalds if (th == NULL) 41821da177e4SLinus Torvalds break; 41831da177e4SLinus Torvalds 418448c62af6SEric Paris ad->u.net->sport = th->source; 418548c62af6SEric Paris ad->u.net->dport = th->dest; 41861da177e4SLinus Torvalds break; 41871da177e4SLinus Torvalds } 41881da177e4SLinus Torvalds 41891da177e4SLinus Torvalds case IPPROTO_UDP: { 41901da177e4SLinus Torvalds struct udphdr _udph, *uh; 41911da177e4SLinus Torvalds 41921da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 41931da177e4SLinus Torvalds if (uh == NULL) 41941da177e4SLinus Torvalds break; 41951da177e4SLinus Torvalds 419648c62af6SEric Paris ad->u.net->sport = uh->source; 419748c62af6SEric Paris ad->u.net->dport = uh->dest; 41981da177e4SLinus Torvalds break; 41991da177e4SLinus Torvalds } 42001da177e4SLinus Torvalds 42012ee92d46SJames Morris case IPPROTO_DCCP: { 42022ee92d46SJames Morris struct dccp_hdr _dccph, *dh; 42032ee92d46SJames Morris 42042ee92d46SJames Morris dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); 42052ee92d46SJames Morris if (dh == NULL) 42062ee92d46SJames Morris break; 42072ee92d46SJames Morris 420848c62af6SEric Paris ad->u.net->sport = dh->dccph_sport; 420948c62af6SEric Paris ad->u.net->dport = dh->dccph_dport; 42102ee92d46SJames Morris break; 42112ee92d46SJames Morris } 42122ee92d46SJames Morris 42131da177e4SLinus Torvalds /* includes fragments */ 42141da177e4SLinus Torvalds default: 42151da177e4SLinus Torvalds break; 42161da177e4SLinus Torvalds } 42171da177e4SLinus Torvalds out: 42181da177e4SLinus Torvalds return ret; 42191da177e4SLinus Torvalds } 42201da177e4SLinus Torvalds 42211da177e4SLinus Torvalds #endif /* IPV6 */ 42221da177e4SLinus Torvalds 42232bf49690SThomas Liu static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad, 4224cf9481e2SDavid Howells char **_addrp, int src, u8 *proto) 42251da177e4SLinus Torvalds { 4226cf9481e2SDavid Howells char *addrp; 4227cf9481e2SDavid Howells int ret; 42281da177e4SLinus Torvalds 422948c62af6SEric Paris switch (ad->u.net->family) { 42301da177e4SLinus Torvalds case PF_INET: 423167f83cbfSVenkat Yekkirala ret = selinux_parse_skb_ipv4(skb, ad, proto); 4232cf9481e2SDavid Howells if (ret) 4233cf9481e2SDavid Howells goto parse_error; 423448c62af6SEric Paris addrp = (char *)(src ? &ad->u.net->v4info.saddr : 423548c62af6SEric Paris &ad->u.net->v4info.daddr); 4236cf9481e2SDavid Howells goto okay; 42371da177e4SLinus Torvalds 42381a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 42391da177e4SLinus Torvalds case PF_INET6: 424067f83cbfSVenkat Yekkirala ret = selinux_parse_skb_ipv6(skb, ad, proto); 4241cf9481e2SDavid Howells if (ret) 4242cf9481e2SDavid Howells goto parse_error; 424348c62af6SEric Paris addrp = (char *)(src ? &ad->u.net->v6info.saddr : 424448c62af6SEric Paris &ad->u.net->v6info.daddr); 4245cf9481e2SDavid Howells goto okay; 42461da177e4SLinus Torvalds #endif /* IPV6 */ 42471da177e4SLinus Torvalds default: 4248cf9481e2SDavid Howells addrp = NULL; 4249cf9481e2SDavid Howells goto okay; 42501da177e4SLinus Torvalds } 42511da177e4SLinus Torvalds 4252cf9481e2SDavid Howells parse_error: 425371f1cb05SPaul Moore printk(KERN_WARNING 425471f1cb05SPaul Moore "SELinux: failure in selinux_parse_skb()," 425571f1cb05SPaul Moore " unable to parse packet\n"); 42561da177e4SLinus Torvalds return ret; 4257cf9481e2SDavid Howells 4258cf9481e2SDavid Howells okay: 4259cf9481e2SDavid Howells if (_addrp) 4260cf9481e2SDavid Howells *_addrp = addrp; 4261cf9481e2SDavid Howells return 0; 42621da177e4SLinus Torvalds } 42631da177e4SLinus Torvalds 42644f6a993fSPaul Moore /** 4265220deb96SPaul Moore * selinux_skb_peerlbl_sid - Determine the peer label of a packet 42664f6a993fSPaul Moore * @skb: the packet 426775e22910SPaul Moore * @family: protocol family 4268220deb96SPaul Moore * @sid: the packet's peer label SID 42694f6a993fSPaul Moore * 42704f6a993fSPaul Moore * Description: 4271220deb96SPaul Moore * Check the various different forms of network peer labeling and determine 4272220deb96SPaul Moore * the peer label/SID for the packet; most of the magic actually occurs in 4273220deb96SPaul Moore * the security server function security_net_peersid_cmp(). The function 4274220deb96SPaul Moore * returns zero if the value in @sid is valid (although it may be SECSID_NULL) 4275220deb96SPaul Moore * or -EACCES if @sid is invalid due to inconsistencies with the different 4276220deb96SPaul Moore * peer labels. 42774f6a993fSPaul Moore * 42784f6a993fSPaul Moore */ 4279220deb96SPaul Moore static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid) 42804f6a993fSPaul Moore { 428171f1cb05SPaul Moore int err; 42824f6a993fSPaul Moore u32 xfrm_sid; 42834f6a993fSPaul Moore u32 nlbl_sid; 4284220deb96SPaul Moore u32 nlbl_type; 42854f6a993fSPaul Moore 4286817eff71SPaul Moore err = selinux_xfrm_skb_sid(skb, &xfrm_sid); 4287bed4d7efSPaul Moore if (unlikely(err)) 4288bed4d7efSPaul Moore return -EACCES; 4289bed4d7efSPaul Moore err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid); 4290bed4d7efSPaul Moore if (unlikely(err)) 4291bed4d7efSPaul Moore return -EACCES; 4292220deb96SPaul Moore 429371f1cb05SPaul Moore err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid); 429471f1cb05SPaul Moore if (unlikely(err)) { 429571f1cb05SPaul Moore printk(KERN_WARNING 429671f1cb05SPaul Moore "SELinux: failure in selinux_skb_peerlbl_sid()," 429771f1cb05SPaul Moore " unable to determine packet's peer label\n"); 4298220deb96SPaul Moore return -EACCES; 429971f1cb05SPaul Moore } 4300220deb96SPaul Moore 4301220deb96SPaul Moore return 0; 43024f6a993fSPaul Moore } 43034f6a993fSPaul Moore 4304446b8024SPaul Moore /** 4305446b8024SPaul Moore * selinux_conn_sid - Determine the child socket label for a connection 4306446b8024SPaul Moore * @sk_sid: the parent socket's SID 4307446b8024SPaul Moore * @skb_sid: the packet's SID 4308446b8024SPaul Moore * @conn_sid: the resulting connection SID 4309446b8024SPaul Moore * 4310446b8024SPaul Moore * If @skb_sid is valid then the user:role:type information from @sk_sid is 4311446b8024SPaul Moore * combined with the MLS information from @skb_sid in order to create 4312446b8024SPaul Moore * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy 4313446b8024SPaul Moore * of @sk_sid. Returns zero on success, negative values on failure. 4314446b8024SPaul Moore * 4315446b8024SPaul Moore */ 4316446b8024SPaul Moore static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid) 4317446b8024SPaul Moore { 4318446b8024SPaul Moore int err = 0; 4319446b8024SPaul Moore 4320446b8024SPaul Moore if (skb_sid != SECSID_NULL) 4321446b8024SPaul Moore err = security_sid_mls_copy(sk_sid, skb_sid, conn_sid); 4322446b8024SPaul Moore else 4323446b8024SPaul Moore *conn_sid = sk_sid; 4324446b8024SPaul Moore 4325446b8024SPaul Moore return err; 4326446b8024SPaul Moore } 4327446b8024SPaul Moore 43281da177e4SLinus Torvalds /* socket security operations */ 4329d4f2d978SPaul Moore 43302ad18bdfSHarry Ciao static int socket_sockcreate_sid(const struct task_security_struct *tsec, 43312ad18bdfSHarry Ciao u16 secclass, u32 *socksid) 4332d4f2d978SPaul Moore { 43332ad18bdfSHarry Ciao if (tsec->sockcreate_sid > SECSID_NULL) { 43342ad18bdfSHarry Ciao *socksid = tsec->sockcreate_sid; 43352ad18bdfSHarry Ciao return 0; 43362ad18bdfSHarry Ciao } 43372ad18bdfSHarry Ciao 43382ad18bdfSHarry Ciao return security_transition_sid(tsec->sid, tsec->sid, secclass, NULL, 43392ad18bdfSHarry Ciao socksid); 4340d4f2d978SPaul Moore } 4341d4f2d978SPaul Moore 4342be0554c9SStephen Smalley static int sock_has_perm(struct sock *sk, u32 perms) 43431da177e4SLinus Torvalds { 4344253bfae6SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 43452bf49690SThomas Liu struct common_audit_data ad; 434648c62af6SEric Paris struct lsm_network_audit net = {0,}; 43471da177e4SLinus Torvalds 4348253bfae6SPaul Moore if (sksec->sid == SECINITSID_KERNEL) 4349253bfae6SPaul Moore return 0; 43501da177e4SLinus Torvalds 435150c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 435248c62af6SEric Paris ad.u.net = &net; 435348c62af6SEric Paris ad.u.net->sk = sk; 43541da177e4SLinus Torvalds 4355be0554c9SStephen Smalley return avc_has_perm(current_sid(), sksec->sid, sksec->sclass, perms, 4356be0554c9SStephen Smalley &ad); 43571da177e4SLinus Torvalds } 43581da177e4SLinus Torvalds 43591da177e4SLinus Torvalds static int selinux_socket_create(int family, int type, 43601da177e4SLinus Torvalds int protocol, int kern) 43611da177e4SLinus Torvalds { 43625fb49870SPaul Moore const struct task_security_struct *tsec = current_security(); 4363d4f2d978SPaul Moore u32 newsid; 4364275bb41eSDavid Howells u16 secclass; 43652ad18bdfSHarry Ciao int rc; 43661da177e4SLinus Torvalds 43671da177e4SLinus Torvalds if (kern) 4368d4f2d978SPaul Moore return 0; 43691da177e4SLinus Torvalds 4370275bb41eSDavid Howells secclass = socket_type_to_security_class(family, type, protocol); 43712ad18bdfSHarry Ciao rc = socket_sockcreate_sid(tsec, secclass, &newsid); 43722ad18bdfSHarry Ciao if (rc) 43732ad18bdfSHarry Ciao return rc; 43742ad18bdfSHarry Ciao 4375d4f2d978SPaul Moore return avc_has_perm(tsec->sid, newsid, secclass, SOCKET__CREATE, NULL); 43761da177e4SLinus Torvalds } 43771da177e4SLinus Torvalds 43787420ed23SVenkat Yekkirala static int selinux_socket_post_create(struct socket *sock, int family, 43791da177e4SLinus Torvalds int type, int protocol, int kern) 43801da177e4SLinus Torvalds { 43815fb49870SPaul Moore const struct task_security_struct *tsec = current_security(); 43825d226df4SAndreas Gruenbacher struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock)); 4383892c141eSVenkat Yekkirala struct sk_security_struct *sksec; 43849287aed2SAndreas Gruenbacher u16 sclass = socket_type_to_security_class(family, type, protocol); 43859287aed2SAndreas Gruenbacher u32 sid = SECINITSID_KERNEL; 4386275bb41eSDavid Howells int err = 0; 4387275bb41eSDavid Howells 43889287aed2SAndreas Gruenbacher if (!kern) { 43899287aed2SAndreas Gruenbacher err = socket_sockcreate_sid(tsec, sclass, &sid); 43902ad18bdfSHarry Ciao if (err) 43912ad18bdfSHarry Ciao return err; 43922ad18bdfSHarry Ciao } 4393275bb41eSDavid Howells 43949287aed2SAndreas Gruenbacher isec->sclass = sclass; 43959287aed2SAndreas Gruenbacher isec->sid = sid; 43966f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 43971da177e4SLinus Torvalds 4398892c141eSVenkat Yekkirala if (sock->sk) { 4399892c141eSVenkat Yekkirala sksec = sock->sk->sk_security; 44009287aed2SAndreas Gruenbacher sksec->sclass = sclass; 44019287aed2SAndreas Gruenbacher sksec->sid = sid; 4402389fb800SPaul Moore err = selinux_netlbl_socket_post_create(sock->sk, family); 4403892c141eSVenkat Yekkirala } 4404892c141eSVenkat Yekkirala 44057420ed23SVenkat Yekkirala return err; 44061da177e4SLinus Torvalds } 44071da177e4SLinus Torvalds 44081da177e4SLinus Torvalds /* Range of port numbers used to automatically bind. 44091da177e4SLinus Torvalds Need to determine whether we should perform a name_bind 44101da177e4SLinus Torvalds permission check between the socket and the port number. */ 44111da177e4SLinus Torvalds 44121da177e4SLinus Torvalds static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen) 44131da177e4SLinus Torvalds { 4414253bfae6SPaul Moore struct sock *sk = sock->sk; 44151da177e4SLinus Torvalds u16 family; 44161da177e4SLinus Torvalds int err; 44171da177e4SLinus Torvalds 4418be0554c9SStephen Smalley err = sock_has_perm(sk, SOCKET__BIND); 44191da177e4SLinus Torvalds if (err) 44201da177e4SLinus Torvalds goto out; 44211da177e4SLinus Torvalds 44221da177e4SLinus Torvalds /* 44231da177e4SLinus Torvalds * If PF_INET or PF_INET6, check name_bind permission for the port. 442413402580SJames Morris * Multiple address binding for SCTP is not supported yet: we just 442513402580SJames Morris * check the first address now. 44261da177e4SLinus Torvalds */ 4427253bfae6SPaul Moore family = sk->sk_family; 44281da177e4SLinus Torvalds if (family == PF_INET || family == PF_INET6) { 44291da177e4SLinus Torvalds char *addrp; 4430253bfae6SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 44312bf49690SThomas Liu struct common_audit_data ad; 443248c62af6SEric Paris struct lsm_network_audit net = {0,}; 44331da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 44341da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 44351da177e4SLinus Torvalds unsigned short snum; 4436e399f982SJames Morris u32 sid, node_perm; 44371da177e4SLinus Torvalds 44381da177e4SLinus Torvalds if (family == PF_INET) { 4439e2f586bdSAlexander Potapenko if (addrlen < sizeof(struct sockaddr_in)) { 4440e2f586bdSAlexander Potapenko err = -EINVAL; 4441e2f586bdSAlexander Potapenko goto out; 4442e2f586bdSAlexander Potapenko } 44431da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 44441da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 44451da177e4SLinus Torvalds addrp = (char *)&addr4->sin_addr.s_addr; 44461da177e4SLinus Torvalds } else { 4447e2f586bdSAlexander Potapenko if (addrlen < SIN6_LEN_RFC2133) { 4448e2f586bdSAlexander Potapenko err = -EINVAL; 4449e2f586bdSAlexander Potapenko goto out; 4450e2f586bdSAlexander Potapenko } 44511da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 44521da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 44531da177e4SLinus Torvalds addrp = (char *)&addr6->sin6_addr.s6_addr; 44541da177e4SLinus Torvalds } 44551da177e4SLinus Torvalds 4456227b60f5SStephen Hemminger if (snum) { 4457227b60f5SStephen Hemminger int low, high; 4458227b60f5SStephen Hemminger 44590bbf87d8SEric W. Biederman inet_get_local_port_range(sock_net(sk), &low, &high); 4460227b60f5SStephen Hemminger 44614548b683SKrister Johansen if (snum < max(inet_prot_sock(sock_net(sk)), low) || 44624548b683SKrister Johansen snum > high) { 44633e112172SPaul Moore err = sel_netport_sid(sk->sk_protocol, 44643e112172SPaul Moore snum, &sid); 44651da177e4SLinus Torvalds if (err) 44661da177e4SLinus Torvalds goto out; 446750c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 446848c62af6SEric Paris ad.u.net = &net; 446948c62af6SEric Paris ad.u.net->sport = htons(snum); 447048c62af6SEric Paris ad.u.net->family = family; 4471253bfae6SPaul Moore err = avc_has_perm(sksec->sid, sid, 4472253bfae6SPaul Moore sksec->sclass, 44731da177e4SLinus Torvalds SOCKET__NAME_BIND, &ad); 44741da177e4SLinus Torvalds if (err) 44751da177e4SLinus Torvalds goto out; 44761da177e4SLinus Torvalds } 4477227b60f5SStephen Hemminger } 44781da177e4SLinus Torvalds 4479253bfae6SPaul Moore switch (sksec->sclass) { 448013402580SJames Morris case SECCLASS_TCP_SOCKET: 44811da177e4SLinus Torvalds node_perm = TCP_SOCKET__NODE_BIND; 44821da177e4SLinus Torvalds break; 44831da177e4SLinus Torvalds 448413402580SJames Morris case SECCLASS_UDP_SOCKET: 44851da177e4SLinus Torvalds node_perm = UDP_SOCKET__NODE_BIND; 44861da177e4SLinus Torvalds break; 44871da177e4SLinus Torvalds 44882ee92d46SJames Morris case SECCLASS_DCCP_SOCKET: 44892ee92d46SJames Morris node_perm = DCCP_SOCKET__NODE_BIND; 44902ee92d46SJames Morris break; 44912ee92d46SJames Morris 44921da177e4SLinus Torvalds default: 44931da177e4SLinus Torvalds node_perm = RAWIP_SOCKET__NODE_BIND; 44941da177e4SLinus Torvalds break; 44951da177e4SLinus Torvalds } 44961da177e4SLinus Torvalds 4497224dfbd8SPaul Moore err = sel_netnode_sid(addrp, family, &sid); 44981da177e4SLinus Torvalds if (err) 44991da177e4SLinus Torvalds goto out; 45001da177e4SLinus Torvalds 450150c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 450248c62af6SEric Paris ad.u.net = &net; 450348c62af6SEric Paris ad.u.net->sport = htons(snum); 450448c62af6SEric Paris ad.u.net->family = family; 45051da177e4SLinus Torvalds 45061da177e4SLinus Torvalds if (family == PF_INET) 450748c62af6SEric Paris ad.u.net->v4info.saddr = addr4->sin_addr.s_addr; 45081da177e4SLinus Torvalds else 450948c62af6SEric Paris ad.u.net->v6info.saddr = addr6->sin6_addr; 45101da177e4SLinus Torvalds 4511253bfae6SPaul Moore err = avc_has_perm(sksec->sid, sid, 4512253bfae6SPaul Moore sksec->sclass, node_perm, &ad); 45131da177e4SLinus Torvalds if (err) 45141da177e4SLinus Torvalds goto out; 45151da177e4SLinus Torvalds } 45161da177e4SLinus Torvalds out: 45171da177e4SLinus Torvalds return err; 45181da177e4SLinus Torvalds } 45191da177e4SLinus Torvalds 45201da177e4SLinus Torvalds static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen) 45211da177e4SLinus Torvalds { 4522014ab19aSPaul Moore struct sock *sk = sock->sk; 4523253bfae6SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 45241da177e4SLinus Torvalds int err; 45251da177e4SLinus Torvalds 4526be0554c9SStephen Smalley err = sock_has_perm(sk, SOCKET__CONNECT); 45271da177e4SLinus Torvalds if (err) 45281da177e4SLinus Torvalds return err; 45291da177e4SLinus Torvalds 45301da177e4SLinus Torvalds /* 45312ee92d46SJames Morris * If a TCP or DCCP socket, check name_connect permission for the port. 45321da177e4SLinus Torvalds */ 4533253bfae6SPaul Moore if (sksec->sclass == SECCLASS_TCP_SOCKET || 4534253bfae6SPaul Moore sksec->sclass == SECCLASS_DCCP_SOCKET) { 45352bf49690SThomas Liu struct common_audit_data ad; 453648c62af6SEric Paris struct lsm_network_audit net = {0,}; 45371da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 45381da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 45391da177e4SLinus Torvalds unsigned short snum; 45402ee92d46SJames Morris u32 sid, perm; 45411da177e4SLinus Torvalds 45421da177e4SLinus Torvalds if (sk->sk_family == PF_INET) { 45431da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 4544911656f8SStephen Smalley if (addrlen < sizeof(struct sockaddr_in)) 45451da177e4SLinus Torvalds return -EINVAL; 45461da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 45471da177e4SLinus Torvalds } else { 45481da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 4549911656f8SStephen Smalley if (addrlen < SIN6_LEN_RFC2133) 45501da177e4SLinus Torvalds return -EINVAL; 45511da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 45521da177e4SLinus Torvalds } 45531da177e4SLinus Torvalds 45543e112172SPaul Moore err = sel_netport_sid(sk->sk_protocol, snum, &sid); 45551da177e4SLinus Torvalds if (err) 45561da177e4SLinus Torvalds goto out; 45571da177e4SLinus Torvalds 4558253bfae6SPaul Moore perm = (sksec->sclass == SECCLASS_TCP_SOCKET) ? 45592ee92d46SJames Morris TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT; 45602ee92d46SJames Morris 456150c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 456248c62af6SEric Paris ad.u.net = &net; 456348c62af6SEric Paris ad.u.net->dport = htons(snum); 456448c62af6SEric Paris ad.u.net->family = sk->sk_family; 4565253bfae6SPaul Moore err = avc_has_perm(sksec->sid, sid, sksec->sclass, perm, &ad); 45661da177e4SLinus Torvalds if (err) 45671da177e4SLinus Torvalds goto out; 45681da177e4SLinus Torvalds } 45691da177e4SLinus Torvalds 4570014ab19aSPaul Moore err = selinux_netlbl_socket_connect(sk, address); 4571014ab19aSPaul Moore 45721da177e4SLinus Torvalds out: 45731da177e4SLinus Torvalds return err; 45741da177e4SLinus Torvalds } 45751da177e4SLinus Torvalds 45761da177e4SLinus Torvalds static int selinux_socket_listen(struct socket *sock, int backlog) 45771da177e4SLinus Torvalds { 4578be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__LISTEN); 45791da177e4SLinus Torvalds } 45801da177e4SLinus Torvalds 45811da177e4SLinus Torvalds static int selinux_socket_accept(struct socket *sock, struct socket *newsock) 45821da177e4SLinus Torvalds { 45831da177e4SLinus Torvalds int err; 45841da177e4SLinus Torvalds struct inode_security_struct *isec; 45851da177e4SLinus Torvalds struct inode_security_struct *newisec; 45869287aed2SAndreas Gruenbacher u16 sclass; 45879287aed2SAndreas Gruenbacher u32 sid; 45881da177e4SLinus Torvalds 4589be0554c9SStephen Smalley err = sock_has_perm(sock->sk, SOCKET__ACCEPT); 45901da177e4SLinus Torvalds if (err) 45911da177e4SLinus Torvalds return err; 45921da177e4SLinus Torvalds 45935d226df4SAndreas Gruenbacher isec = inode_security_novalidate(SOCK_INODE(sock)); 45949287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 45959287aed2SAndreas Gruenbacher sclass = isec->sclass; 45969287aed2SAndreas Gruenbacher sid = isec->sid; 45979287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 45989287aed2SAndreas Gruenbacher 45999287aed2SAndreas Gruenbacher newisec = inode_security_novalidate(SOCK_INODE(newsock)); 46009287aed2SAndreas Gruenbacher newisec->sclass = sclass; 46019287aed2SAndreas Gruenbacher newisec->sid = sid; 46026f3be9f5SAndreas Gruenbacher newisec->initialized = LABEL_INITIALIZED; 46031da177e4SLinus Torvalds 46041da177e4SLinus Torvalds return 0; 46051da177e4SLinus Torvalds } 46061da177e4SLinus Torvalds 46071da177e4SLinus Torvalds static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg, 46081da177e4SLinus Torvalds int size) 46091da177e4SLinus Torvalds { 4610be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__WRITE); 46111da177e4SLinus Torvalds } 46121da177e4SLinus Torvalds 46131da177e4SLinus Torvalds static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg, 46141da177e4SLinus Torvalds int size, int flags) 46151da177e4SLinus Torvalds { 4616be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__READ); 46171da177e4SLinus Torvalds } 46181da177e4SLinus Torvalds 46191da177e4SLinus Torvalds static int selinux_socket_getsockname(struct socket *sock) 46201da177e4SLinus Torvalds { 4621be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__GETATTR); 46221da177e4SLinus Torvalds } 46231da177e4SLinus Torvalds 46241da177e4SLinus Torvalds static int selinux_socket_getpeername(struct socket *sock) 46251da177e4SLinus Torvalds { 4626be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__GETATTR); 46271da177e4SLinus Torvalds } 46281da177e4SLinus Torvalds 46291da177e4SLinus Torvalds static int selinux_socket_setsockopt(struct socket *sock, int level, int optname) 46301da177e4SLinus Torvalds { 4631f8687afeSPaul Moore int err; 4632f8687afeSPaul Moore 4633be0554c9SStephen Smalley err = sock_has_perm(sock->sk, SOCKET__SETOPT); 4634f8687afeSPaul Moore if (err) 4635f8687afeSPaul Moore return err; 4636f8687afeSPaul Moore 4637f8687afeSPaul Moore return selinux_netlbl_socket_setsockopt(sock, level, optname); 46381da177e4SLinus Torvalds } 46391da177e4SLinus Torvalds 46401da177e4SLinus Torvalds static int selinux_socket_getsockopt(struct socket *sock, int level, 46411da177e4SLinus Torvalds int optname) 46421da177e4SLinus Torvalds { 4643be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__GETOPT); 46441da177e4SLinus Torvalds } 46451da177e4SLinus Torvalds 46461da177e4SLinus Torvalds static int selinux_socket_shutdown(struct socket *sock, int how) 46471da177e4SLinus Torvalds { 4648be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__SHUTDOWN); 46491da177e4SLinus Torvalds } 46501da177e4SLinus Torvalds 46513610cda5SDavid S. Miller static int selinux_socket_unix_stream_connect(struct sock *sock, 46523610cda5SDavid S. Miller struct sock *other, 46531da177e4SLinus Torvalds struct sock *newsk) 46541da177e4SLinus Torvalds { 46553610cda5SDavid S. Miller struct sk_security_struct *sksec_sock = sock->sk_security; 46563610cda5SDavid S. Miller struct sk_security_struct *sksec_other = other->sk_security; 46574d1e2451SPaul Moore struct sk_security_struct *sksec_new = newsk->sk_security; 46582bf49690SThomas Liu struct common_audit_data ad; 465948c62af6SEric Paris struct lsm_network_audit net = {0,}; 46601da177e4SLinus Torvalds int err; 46611da177e4SLinus Torvalds 466250c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 466348c62af6SEric Paris ad.u.net = &net; 466448c62af6SEric Paris ad.u.net->sk = other; 46651da177e4SLinus Torvalds 46664d1e2451SPaul Moore err = avc_has_perm(sksec_sock->sid, sksec_other->sid, 46674d1e2451SPaul Moore sksec_other->sclass, 46681da177e4SLinus Torvalds UNIX_STREAM_SOCKET__CONNECTTO, &ad); 46691da177e4SLinus Torvalds if (err) 46701da177e4SLinus Torvalds return err; 46711da177e4SLinus Torvalds 46721da177e4SLinus Torvalds /* server child socket */ 46734d1e2451SPaul Moore sksec_new->peer_sid = sksec_sock->sid; 46744d1e2451SPaul Moore err = security_sid_mls_copy(sksec_other->sid, sksec_sock->sid, 46754d1e2451SPaul Moore &sksec_new->sid); 46764d1e2451SPaul Moore if (err) 46774237c75cSVenkat Yekkirala return err; 46784d1e2451SPaul Moore 46794d1e2451SPaul Moore /* connecting socket */ 46804d1e2451SPaul Moore sksec_sock->peer_sid = sksec_new->sid; 46814d1e2451SPaul Moore 46824d1e2451SPaul Moore return 0; 46831da177e4SLinus Torvalds } 46841da177e4SLinus Torvalds 46851da177e4SLinus Torvalds static int selinux_socket_unix_may_send(struct socket *sock, 46861da177e4SLinus Torvalds struct socket *other) 46871da177e4SLinus Torvalds { 4688253bfae6SPaul Moore struct sk_security_struct *ssec = sock->sk->sk_security; 4689253bfae6SPaul Moore struct sk_security_struct *osec = other->sk->sk_security; 46902bf49690SThomas Liu struct common_audit_data ad; 469148c62af6SEric Paris struct lsm_network_audit net = {0,}; 46921da177e4SLinus Torvalds 469350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 469448c62af6SEric Paris ad.u.net = &net; 469548c62af6SEric Paris ad.u.net->sk = other->sk; 46961da177e4SLinus Torvalds 4697253bfae6SPaul Moore return avc_has_perm(ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO, 4698253bfae6SPaul Moore &ad); 46991da177e4SLinus Torvalds } 47001da177e4SLinus Torvalds 4701cbe0d6e8SPaul Moore static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex, 4702cbe0d6e8SPaul Moore char *addrp, u16 family, u32 peer_sid, 47032bf49690SThomas Liu struct common_audit_data *ad) 4704effad8dfSPaul Moore { 4705effad8dfSPaul Moore int err; 4706effad8dfSPaul Moore u32 if_sid; 4707effad8dfSPaul Moore u32 node_sid; 4708effad8dfSPaul Moore 4709cbe0d6e8SPaul Moore err = sel_netif_sid(ns, ifindex, &if_sid); 4710effad8dfSPaul Moore if (err) 4711effad8dfSPaul Moore return err; 4712effad8dfSPaul Moore err = avc_has_perm(peer_sid, if_sid, 4713effad8dfSPaul Moore SECCLASS_NETIF, NETIF__INGRESS, ad); 4714effad8dfSPaul Moore if (err) 4715effad8dfSPaul Moore return err; 4716effad8dfSPaul Moore 4717effad8dfSPaul Moore err = sel_netnode_sid(addrp, family, &node_sid); 4718effad8dfSPaul Moore if (err) 4719effad8dfSPaul Moore return err; 4720effad8dfSPaul Moore return avc_has_perm(peer_sid, node_sid, 4721effad8dfSPaul Moore SECCLASS_NODE, NODE__RECVFROM, ad); 4722effad8dfSPaul Moore } 4723effad8dfSPaul Moore 4724220deb96SPaul Moore static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb, 4725d8395c87SPaul Moore u16 family) 4726220deb96SPaul Moore { 4727277d342fSPaul Moore int err = 0; 4728220deb96SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 4729220deb96SPaul Moore u32 sk_sid = sksec->sid; 47302bf49690SThomas Liu struct common_audit_data ad; 473148c62af6SEric Paris struct lsm_network_audit net = {0,}; 4732d8395c87SPaul Moore char *addrp; 4733d8395c87SPaul Moore 473450c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 473548c62af6SEric Paris ad.u.net = &net; 473648c62af6SEric Paris ad.u.net->netif = skb->skb_iif; 473748c62af6SEric Paris ad.u.net->family = family; 4738d8395c87SPaul Moore err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); 4739d8395c87SPaul Moore if (err) 4740d8395c87SPaul Moore return err; 4741220deb96SPaul Moore 474258bfbb51SPaul Moore if (selinux_secmark_enabled()) { 4743220deb96SPaul Moore err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET, 4744d8395c87SPaul Moore PACKET__RECV, &ad); 4745220deb96SPaul Moore if (err) 4746220deb96SPaul Moore return err; 474758bfbb51SPaul Moore } 4748220deb96SPaul Moore 4749d8395c87SPaul Moore err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad); 4750220deb96SPaul Moore if (err) 4751220deb96SPaul Moore return err; 4752d8395c87SPaul Moore err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad); 4753220deb96SPaul Moore 47544e5ab4cbSJames Morris return err; 47554e5ab4cbSJames Morris } 4756d28d1e08STrent Jaeger 47574e5ab4cbSJames Morris static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) 47584e5ab4cbSJames Morris { 4759220deb96SPaul Moore int err; 47604237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 4761220deb96SPaul Moore u16 family = sk->sk_family; 4762220deb96SPaul Moore u32 sk_sid = sksec->sid; 47632bf49690SThomas Liu struct common_audit_data ad; 476448c62af6SEric Paris struct lsm_network_audit net = {0,}; 4765220deb96SPaul Moore char *addrp; 4766d8395c87SPaul Moore u8 secmark_active; 4767d8395c87SPaul Moore u8 peerlbl_active; 47684e5ab4cbSJames Morris 47694e5ab4cbSJames Morris if (family != PF_INET && family != PF_INET6) 4770220deb96SPaul Moore return 0; 47714e5ab4cbSJames Morris 47724e5ab4cbSJames Morris /* Handle mapped IPv4 packets arriving via IPv6 sockets */ 477387fcd70dSAl Viro if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 47744e5ab4cbSJames Morris family = PF_INET; 47754e5ab4cbSJames Morris 4776d8395c87SPaul Moore /* If any sort of compatibility mode is enabled then handoff processing 4777d8395c87SPaul Moore * to the selinux_sock_rcv_skb_compat() function to deal with the 4778d8395c87SPaul Moore * special handling. We do this in an attempt to keep this function 4779d8395c87SPaul Moore * as fast and as clean as possible. */ 478058bfbb51SPaul Moore if (!selinux_policycap_netpeer) 4781d8395c87SPaul Moore return selinux_sock_rcv_skb_compat(sk, skb, family); 4782d8395c87SPaul Moore 4783d8395c87SPaul Moore secmark_active = selinux_secmark_enabled(); 47842be4d74fSChris PeBenito peerlbl_active = selinux_peerlbl_enabled(); 4785d8395c87SPaul Moore if (!secmark_active && !peerlbl_active) 4786d8395c87SPaul Moore return 0; 4787d8395c87SPaul Moore 478850c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 478948c62af6SEric Paris ad.u.net = &net; 479048c62af6SEric Paris ad.u.net->netif = skb->skb_iif; 479148c62af6SEric Paris ad.u.net->family = family; 4792224dfbd8SPaul Moore err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); 47934e5ab4cbSJames Morris if (err) 4794220deb96SPaul Moore return err; 47954e5ab4cbSJames Morris 4796d8395c87SPaul Moore if (peerlbl_active) { 4797d621d35eSPaul Moore u32 peer_sid; 4798220deb96SPaul Moore 4799220deb96SPaul Moore err = selinux_skb_peerlbl_sid(skb, family, &peer_sid); 4800220deb96SPaul Moore if (err) 4801220deb96SPaul Moore return err; 4802cbe0d6e8SPaul Moore err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif, 4803cbe0d6e8SPaul Moore addrp, family, peer_sid, &ad); 4804dfaebe98SPaul Moore if (err) { 4805a04e71f6SHuw Davies selinux_netlbl_err(skb, family, err, 0); 4806effad8dfSPaul Moore return err; 4807dfaebe98SPaul Moore } 4808d621d35eSPaul Moore err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER, 4809d621d35eSPaul Moore PEER__RECV, &ad); 481046d01d63SChad Hanson if (err) { 4811a04e71f6SHuw Davies selinux_netlbl_err(skb, family, err, 0); 481246d01d63SChad Hanson return err; 481346d01d63SChad Hanson } 4814d621d35eSPaul Moore } 4815d621d35eSPaul Moore 4816d8395c87SPaul Moore if (secmark_active) { 4817effad8dfSPaul Moore err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET, 4818effad8dfSPaul Moore PACKET__RECV, &ad); 4819effad8dfSPaul Moore if (err) 4820effad8dfSPaul Moore return err; 4821effad8dfSPaul Moore } 4822effad8dfSPaul Moore 4823d621d35eSPaul Moore return err; 48241da177e4SLinus Torvalds } 48251da177e4SLinus Torvalds 48262c7946a7SCatherine Zhang static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval, 48271da177e4SLinus Torvalds int __user *optlen, unsigned len) 48281da177e4SLinus Torvalds { 48291da177e4SLinus Torvalds int err = 0; 48301da177e4SLinus Torvalds char *scontext; 48311da177e4SLinus Torvalds u32 scontext_len; 4832253bfae6SPaul Moore struct sk_security_struct *sksec = sock->sk->sk_security; 48333de4bab5SPaul Moore u32 peer_sid = SECSID_NULL; 48341da177e4SLinus Torvalds 4835253bfae6SPaul Moore if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET || 4836253bfae6SPaul Moore sksec->sclass == SECCLASS_TCP_SOCKET) 4837dd3e7836SEric Paris peer_sid = sksec->peer_sid; 4838253bfae6SPaul Moore if (peer_sid == SECSID_NULL) 4839253bfae6SPaul Moore return -ENOPROTOOPT; 48401da177e4SLinus Torvalds 48412c7946a7SCatherine Zhang err = security_sid_to_context(peer_sid, &scontext, &scontext_len); 48421da177e4SLinus Torvalds if (err) 4843253bfae6SPaul Moore return err; 48441da177e4SLinus Torvalds 48451da177e4SLinus Torvalds if (scontext_len > len) { 48461da177e4SLinus Torvalds err = -ERANGE; 48471da177e4SLinus Torvalds goto out_len; 48481da177e4SLinus Torvalds } 48491da177e4SLinus Torvalds 48501da177e4SLinus Torvalds if (copy_to_user(optval, scontext, scontext_len)) 48511da177e4SLinus Torvalds err = -EFAULT; 48521da177e4SLinus Torvalds 48531da177e4SLinus Torvalds out_len: 48541da177e4SLinus Torvalds if (put_user(scontext_len, optlen)) 48551da177e4SLinus Torvalds err = -EFAULT; 48561da177e4SLinus Torvalds kfree(scontext); 48571da177e4SLinus Torvalds return err; 48581da177e4SLinus Torvalds } 48591da177e4SLinus Torvalds 4860dc49c1f9SCatherine Zhang static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid) 48612c7946a7SCatherine Zhang { 4862dc49c1f9SCatherine Zhang u32 peer_secid = SECSID_NULL; 486375e22910SPaul Moore u16 family; 4864899134f2SPaul Moore struct inode_security_struct *isec; 4865877ce7c1SCatherine Zhang 4866aa862900SPaul Moore if (skb && skb->protocol == htons(ETH_P_IP)) 4867aa862900SPaul Moore family = PF_INET; 4868aa862900SPaul Moore else if (skb && skb->protocol == htons(ETH_P_IPV6)) 4869aa862900SPaul Moore family = PF_INET6; 4870aa862900SPaul Moore else if (sock) 487175e22910SPaul Moore family = sock->sk->sk_family; 487275e22910SPaul Moore else 487375e22910SPaul Moore goto out; 487475e22910SPaul Moore 4875899134f2SPaul Moore if (sock && family == PF_UNIX) { 4876899134f2SPaul Moore isec = inode_security_novalidate(SOCK_INODE(sock)); 4877899134f2SPaul Moore peer_secid = isec->sid; 4878899134f2SPaul Moore } else if (skb) 4879220deb96SPaul Moore selinux_skb_peerlbl_sid(skb, family, &peer_secid); 48802c7946a7SCatherine Zhang 488175e22910SPaul Moore out: 4882dc49c1f9SCatherine Zhang *secid = peer_secid; 488375e22910SPaul Moore if (peer_secid == SECSID_NULL) 488475e22910SPaul Moore return -EINVAL; 488575e22910SPaul Moore return 0; 48862c7946a7SCatherine Zhang } 48872c7946a7SCatherine Zhang 48887d877f3bSAl Viro static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority) 48891da177e4SLinus Torvalds { 489084914b7eSPaul Moore struct sk_security_struct *sksec; 489184914b7eSPaul Moore 489284914b7eSPaul Moore sksec = kzalloc(sizeof(*sksec), priority); 489384914b7eSPaul Moore if (!sksec) 489484914b7eSPaul Moore return -ENOMEM; 489584914b7eSPaul Moore 489684914b7eSPaul Moore sksec->peer_sid = SECINITSID_UNLABELED; 489784914b7eSPaul Moore sksec->sid = SECINITSID_UNLABELED; 48985dee25d0SStephen Smalley sksec->sclass = SECCLASS_SOCKET; 489984914b7eSPaul Moore selinux_netlbl_sk_security_reset(sksec); 490084914b7eSPaul Moore sk->sk_security = sksec; 490184914b7eSPaul Moore 490284914b7eSPaul Moore return 0; 49031da177e4SLinus Torvalds } 49041da177e4SLinus Torvalds 49051da177e4SLinus Torvalds static void selinux_sk_free_security(struct sock *sk) 49061da177e4SLinus Torvalds { 490784914b7eSPaul Moore struct sk_security_struct *sksec = sk->sk_security; 490884914b7eSPaul Moore 490984914b7eSPaul Moore sk->sk_security = NULL; 491084914b7eSPaul Moore selinux_netlbl_sk_security_free(sksec); 491184914b7eSPaul Moore kfree(sksec); 49121da177e4SLinus Torvalds } 49131da177e4SLinus Torvalds 4914892c141eSVenkat Yekkirala static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk) 4915892c141eSVenkat Yekkirala { 4916dd3e7836SEric Paris struct sk_security_struct *sksec = sk->sk_security; 4917dd3e7836SEric Paris struct sk_security_struct *newsksec = newsk->sk_security; 4918892c141eSVenkat Yekkirala 4919dd3e7836SEric Paris newsksec->sid = sksec->sid; 4920dd3e7836SEric Paris newsksec->peer_sid = sksec->peer_sid; 4921dd3e7836SEric Paris newsksec->sclass = sksec->sclass; 492299f59ed0SPaul Moore 4923dd3e7836SEric Paris selinux_netlbl_sk_security_reset(newsksec); 4924892c141eSVenkat Yekkirala } 4925892c141eSVenkat Yekkirala 4926beb8d13bSVenkat Yekkirala static void selinux_sk_getsecid(struct sock *sk, u32 *secid) 4927d28d1e08STrent Jaeger { 4928d28d1e08STrent Jaeger if (!sk) 4929beb8d13bSVenkat Yekkirala *secid = SECINITSID_ANY_SOCKET; 4930892c141eSVenkat Yekkirala else { 4931892c141eSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 4932d28d1e08STrent Jaeger 4933beb8d13bSVenkat Yekkirala *secid = sksec->sid; 4934892c141eSVenkat Yekkirala } 4935d28d1e08STrent Jaeger } 4936d28d1e08STrent Jaeger 49379a673e56SAdrian Bunk static void selinux_sock_graft(struct sock *sk, struct socket *parent) 49384237c75cSVenkat Yekkirala { 49395d226df4SAndreas Gruenbacher struct inode_security_struct *isec = 49405d226df4SAndreas Gruenbacher inode_security_novalidate(SOCK_INODE(parent)); 49414237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 49424237c75cSVenkat Yekkirala 49432873ead7SPaul Moore if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 || 49442873ead7SPaul Moore sk->sk_family == PF_UNIX) 49454237c75cSVenkat Yekkirala isec->sid = sksec->sid; 4946220deb96SPaul Moore sksec->sclass = isec->sclass; 49474237c75cSVenkat Yekkirala } 49484237c75cSVenkat Yekkirala 49499a673e56SAdrian Bunk static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb, 49504237c75cSVenkat Yekkirala struct request_sock *req) 49514237c75cSVenkat Yekkirala { 49524237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 49534237c75cSVenkat Yekkirala int err; 49540b1f24e6SPaul Moore u16 family = req->rsk_ops->family; 4955446b8024SPaul Moore u32 connsid; 49564237c75cSVenkat Yekkirala u32 peersid; 49574237c75cSVenkat Yekkirala 4958aa862900SPaul Moore err = selinux_skb_peerlbl_sid(skb, family, &peersid); 4959220deb96SPaul Moore if (err) 4960220deb96SPaul Moore return err; 4961446b8024SPaul Moore err = selinux_conn_sid(sksec->sid, peersid, &connsid); 49624237c75cSVenkat Yekkirala if (err) 49634237c75cSVenkat Yekkirala return err; 4964446b8024SPaul Moore req->secid = connsid; 49656b877699SVenkat Yekkirala req->peer_secid = peersid; 4966389fb800SPaul Moore 4967389fb800SPaul Moore return selinux_netlbl_inet_conn_request(req, family); 49684237c75cSVenkat Yekkirala } 49694237c75cSVenkat Yekkirala 49709a673e56SAdrian Bunk static void selinux_inet_csk_clone(struct sock *newsk, 49719a673e56SAdrian Bunk const struct request_sock *req) 49724237c75cSVenkat Yekkirala { 49734237c75cSVenkat Yekkirala struct sk_security_struct *newsksec = newsk->sk_security; 49744237c75cSVenkat Yekkirala 49754237c75cSVenkat Yekkirala newsksec->sid = req->secid; 49766b877699SVenkat Yekkirala newsksec->peer_sid = req->peer_secid; 49774237c75cSVenkat Yekkirala /* NOTE: Ideally, we should also get the isec->sid for the 49784237c75cSVenkat Yekkirala new socket in sync, but we don't have the isec available yet. 49794237c75cSVenkat Yekkirala So we will wait until sock_graft to do it, by which 49804237c75cSVenkat Yekkirala time it will have been created and available. */ 498199f59ed0SPaul Moore 49829f2ad665SPaul Moore /* We don't need to take any sort of lock here as we are the only 49839f2ad665SPaul Moore * thread with access to newsksec */ 4984389fb800SPaul Moore selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family); 49854237c75cSVenkat Yekkirala } 49864237c75cSVenkat Yekkirala 4987014ab19aSPaul Moore static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb) 49886b877699SVenkat Yekkirala { 4989aa862900SPaul Moore u16 family = sk->sk_family; 49906b877699SVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 49916b877699SVenkat Yekkirala 4992aa862900SPaul Moore /* handle mapped IPv4 packets arriving via IPv6 sockets */ 4993aa862900SPaul Moore if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 4994aa862900SPaul Moore family = PF_INET; 4995aa862900SPaul Moore 4996aa862900SPaul Moore selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid); 49976b877699SVenkat Yekkirala } 49986b877699SVenkat Yekkirala 49992606fd1fSEric Paris static int selinux_secmark_relabel_packet(u32 sid) 50002606fd1fSEric Paris { 50012606fd1fSEric Paris const struct task_security_struct *__tsec; 50022606fd1fSEric Paris u32 tsid; 50032606fd1fSEric Paris 50042606fd1fSEric Paris __tsec = current_security(); 50052606fd1fSEric Paris tsid = __tsec->sid; 50062606fd1fSEric Paris 50072606fd1fSEric Paris return avc_has_perm(tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, NULL); 50082606fd1fSEric Paris } 50092606fd1fSEric Paris 50102606fd1fSEric Paris static void selinux_secmark_refcount_inc(void) 50112606fd1fSEric Paris { 50122606fd1fSEric Paris atomic_inc(&selinux_secmark_refcount); 50132606fd1fSEric Paris } 50142606fd1fSEric Paris 50152606fd1fSEric Paris static void selinux_secmark_refcount_dec(void) 50162606fd1fSEric Paris { 50172606fd1fSEric Paris atomic_dec(&selinux_secmark_refcount); 50182606fd1fSEric Paris } 50192606fd1fSEric Paris 50209a673e56SAdrian Bunk static void selinux_req_classify_flow(const struct request_sock *req, 50219a673e56SAdrian Bunk struct flowi *fl) 50224237c75cSVenkat Yekkirala { 50231d28f42cSDavid S. Miller fl->flowi_secid = req->secid; 50244237c75cSVenkat Yekkirala } 50254237c75cSVenkat Yekkirala 50265dbbaf2dSPaul Moore static int selinux_tun_dev_alloc_security(void **security) 50275dbbaf2dSPaul Moore { 50285dbbaf2dSPaul Moore struct tun_security_struct *tunsec; 50295dbbaf2dSPaul Moore 50305dbbaf2dSPaul Moore tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL); 50315dbbaf2dSPaul Moore if (!tunsec) 50325dbbaf2dSPaul Moore return -ENOMEM; 50335dbbaf2dSPaul Moore tunsec->sid = current_sid(); 50345dbbaf2dSPaul Moore 50355dbbaf2dSPaul Moore *security = tunsec; 50365dbbaf2dSPaul Moore return 0; 50375dbbaf2dSPaul Moore } 50385dbbaf2dSPaul Moore 50395dbbaf2dSPaul Moore static void selinux_tun_dev_free_security(void *security) 50405dbbaf2dSPaul Moore { 50415dbbaf2dSPaul Moore kfree(security); 50425dbbaf2dSPaul Moore } 50435dbbaf2dSPaul Moore 5044ed6d76e4SPaul Moore static int selinux_tun_dev_create(void) 5045ed6d76e4SPaul Moore { 5046ed6d76e4SPaul Moore u32 sid = current_sid(); 5047ed6d76e4SPaul Moore 5048ed6d76e4SPaul Moore /* we aren't taking into account the "sockcreate" SID since the socket 5049ed6d76e4SPaul Moore * that is being created here is not a socket in the traditional sense, 5050ed6d76e4SPaul Moore * instead it is a private sock, accessible only to the kernel, and 5051ed6d76e4SPaul Moore * representing a wide range of network traffic spanning multiple 5052ed6d76e4SPaul Moore * connections unlike traditional sockets - check the TUN driver to 5053ed6d76e4SPaul Moore * get a better understanding of why this socket is special */ 5054ed6d76e4SPaul Moore 5055ed6d76e4SPaul Moore return avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE, 5056ed6d76e4SPaul Moore NULL); 5057ed6d76e4SPaul Moore } 5058ed6d76e4SPaul Moore 50595dbbaf2dSPaul Moore static int selinux_tun_dev_attach_queue(void *security) 5060ed6d76e4SPaul Moore { 50615dbbaf2dSPaul Moore struct tun_security_struct *tunsec = security; 50625dbbaf2dSPaul Moore 50635dbbaf2dSPaul Moore return avc_has_perm(current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET, 50645dbbaf2dSPaul Moore TUN_SOCKET__ATTACH_QUEUE, NULL); 50655dbbaf2dSPaul Moore } 50665dbbaf2dSPaul Moore 50675dbbaf2dSPaul Moore static int selinux_tun_dev_attach(struct sock *sk, void *security) 50685dbbaf2dSPaul Moore { 50695dbbaf2dSPaul Moore struct tun_security_struct *tunsec = security; 5070ed6d76e4SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 5071ed6d76e4SPaul Moore 5072ed6d76e4SPaul Moore /* we don't currently perform any NetLabel based labeling here and it 5073ed6d76e4SPaul Moore * isn't clear that we would want to do so anyway; while we could apply 5074ed6d76e4SPaul Moore * labeling without the support of the TUN user the resulting labeled 5075ed6d76e4SPaul Moore * traffic from the other end of the connection would almost certainly 5076ed6d76e4SPaul Moore * cause confusion to the TUN user that had no idea network labeling 5077ed6d76e4SPaul Moore * protocols were being used */ 5078ed6d76e4SPaul Moore 50795dbbaf2dSPaul Moore sksec->sid = tunsec->sid; 5080ed6d76e4SPaul Moore sksec->sclass = SECCLASS_TUN_SOCKET; 50815dbbaf2dSPaul Moore 50825dbbaf2dSPaul Moore return 0; 5083ed6d76e4SPaul Moore } 5084ed6d76e4SPaul Moore 50855dbbaf2dSPaul Moore static int selinux_tun_dev_open(void *security) 5086ed6d76e4SPaul Moore { 50875dbbaf2dSPaul Moore struct tun_security_struct *tunsec = security; 5088ed6d76e4SPaul Moore u32 sid = current_sid(); 5089ed6d76e4SPaul Moore int err; 5090ed6d76e4SPaul Moore 50915dbbaf2dSPaul Moore err = avc_has_perm(sid, tunsec->sid, SECCLASS_TUN_SOCKET, 5092ed6d76e4SPaul Moore TUN_SOCKET__RELABELFROM, NULL); 5093ed6d76e4SPaul Moore if (err) 5094ed6d76e4SPaul Moore return err; 5095ed6d76e4SPaul Moore err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, 5096ed6d76e4SPaul Moore TUN_SOCKET__RELABELTO, NULL); 5097ed6d76e4SPaul Moore if (err) 5098ed6d76e4SPaul Moore return err; 50995dbbaf2dSPaul Moore tunsec->sid = sid; 5100ed6d76e4SPaul Moore 5101ed6d76e4SPaul Moore return 0; 5102ed6d76e4SPaul Moore } 5103ed6d76e4SPaul Moore 51041da177e4SLinus Torvalds static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb) 51051da177e4SLinus Torvalds { 51061da177e4SLinus Torvalds int err = 0; 51071da177e4SLinus Torvalds u32 perm; 51081da177e4SLinus Torvalds struct nlmsghdr *nlh; 5109253bfae6SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 51101da177e4SLinus Torvalds 511177954983SHong zhi guo if (skb->len < NLMSG_HDRLEN) { 51121da177e4SLinus Torvalds err = -EINVAL; 51131da177e4SLinus Torvalds goto out; 51141da177e4SLinus Torvalds } 5115b529ccf2SArnaldo Carvalho de Melo nlh = nlmsg_hdr(skb); 51161da177e4SLinus Torvalds 5117253bfae6SPaul Moore err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm); 51181da177e4SLinus Torvalds if (err) { 51191da177e4SLinus Torvalds if (err == -EINVAL) { 512076319946SVladis Dronov pr_warn_ratelimited("SELinux: unrecognized netlink" 512176319946SVladis Dronov " message: protocol=%hu nlmsg_type=%hu sclass=%s" 512276319946SVladis Dronov " pig=%d comm=%s\n", 5123cded3fffSMarek Milkovic sk->sk_protocol, nlh->nlmsg_type, 512476319946SVladis Dronov secclass_map[sksec->sclass - 1].name, 512576319946SVladis Dronov task_pid_nr(current), current->comm); 512639c9aedeSEric Paris if (!selinux_enforcing || security_get_allow_unknown()) 51271da177e4SLinus Torvalds err = 0; 51281da177e4SLinus Torvalds } 51291da177e4SLinus Torvalds 51301da177e4SLinus Torvalds /* Ignore */ 51311da177e4SLinus Torvalds if (err == -ENOENT) 51321da177e4SLinus Torvalds err = 0; 51331da177e4SLinus Torvalds goto out; 51341da177e4SLinus Torvalds } 51351da177e4SLinus Torvalds 5136be0554c9SStephen Smalley err = sock_has_perm(sk, perm); 51371da177e4SLinus Torvalds out: 51381da177e4SLinus Torvalds return err; 51391da177e4SLinus Torvalds } 51401da177e4SLinus Torvalds 51411da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER 51421da177e4SLinus Torvalds 5143cbe0d6e8SPaul Moore static unsigned int selinux_ip_forward(struct sk_buff *skb, 5144cbe0d6e8SPaul Moore const struct net_device *indev, 5145effad8dfSPaul Moore u16 family) 51461da177e4SLinus Torvalds { 5147dfaebe98SPaul Moore int err; 5148effad8dfSPaul Moore char *addrp; 5149effad8dfSPaul Moore u32 peer_sid; 51502bf49690SThomas Liu struct common_audit_data ad; 515148c62af6SEric Paris struct lsm_network_audit net = {0,}; 5152effad8dfSPaul Moore u8 secmark_active; 5153948bf85cSPaul Moore u8 netlbl_active; 5154effad8dfSPaul Moore u8 peerlbl_active; 51554237c75cSVenkat Yekkirala 5156effad8dfSPaul Moore if (!selinux_policycap_netpeer) 5157effad8dfSPaul Moore return NF_ACCEPT; 51584237c75cSVenkat Yekkirala 5159effad8dfSPaul Moore secmark_active = selinux_secmark_enabled(); 5160948bf85cSPaul Moore netlbl_active = netlbl_enabled(); 51612be4d74fSChris PeBenito peerlbl_active = selinux_peerlbl_enabled(); 5162effad8dfSPaul Moore if (!secmark_active && !peerlbl_active) 5163effad8dfSPaul Moore return NF_ACCEPT; 51644237c75cSVenkat Yekkirala 5165d8395c87SPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0) 5166d8395c87SPaul Moore return NF_DROP; 5167d8395c87SPaul Moore 516850c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 516948c62af6SEric Paris ad.u.net = &net; 5170cbe0d6e8SPaul Moore ad.u.net->netif = indev->ifindex; 517148c62af6SEric Paris ad.u.net->family = family; 5172effad8dfSPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0) 5173effad8dfSPaul Moore return NF_DROP; 51741da177e4SLinus Torvalds 5175dfaebe98SPaul Moore if (peerlbl_active) { 5176cbe0d6e8SPaul Moore err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex, 5177cbe0d6e8SPaul Moore addrp, family, peer_sid, &ad); 5178dfaebe98SPaul Moore if (err) { 5179a04e71f6SHuw Davies selinux_netlbl_err(skb, family, err, 1); 5180effad8dfSPaul Moore return NF_DROP; 5181dfaebe98SPaul Moore } 5182dfaebe98SPaul Moore } 5183effad8dfSPaul Moore 5184effad8dfSPaul Moore if (secmark_active) 5185effad8dfSPaul Moore if (avc_has_perm(peer_sid, skb->secmark, 5186effad8dfSPaul Moore SECCLASS_PACKET, PACKET__FORWARD_IN, &ad)) 5187effad8dfSPaul Moore return NF_DROP; 5188effad8dfSPaul Moore 5189948bf85cSPaul Moore if (netlbl_active) 5190948bf85cSPaul Moore /* we do this in the FORWARD path and not the POST_ROUTING 5191948bf85cSPaul Moore * path because we want to make sure we apply the necessary 5192948bf85cSPaul Moore * labeling before IPsec is applied so we can leverage AH 5193948bf85cSPaul Moore * protection */ 5194948bf85cSPaul Moore if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0) 5195948bf85cSPaul Moore return NF_DROP; 5196948bf85cSPaul Moore 5197effad8dfSPaul Moore return NF_ACCEPT; 5198effad8dfSPaul Moore } 5199effad8dfSPaul Moore 520006198b34SEric W. Biederman static unsigned int selinux_ipv4_forward(void *priv, 5201effad8dfSPaul Moore struct sk_buff *skb, 5202238e54c9SDavid S. Miller const struct nf_hook_state *state) 5203effad8dfSPaul Moore { 5204238e54c9SDavid S. Miller return selinux_ip_forward(skb, state->in, PF_INET); 5205effad8dfSPaul Moore } 5206effad8dfSPaul Moore 52071a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 520806198b34SEric W. Biederman static unsigned int selinux_ipv6_forward(void *priv, 5209effad8dfSPaul Moore struct sk_buff *skb, 5210238e54c9SDavid S. Miller const struct nf_hook_state *state) 5211effad8dfSPaul Moore { 5212238e54c9SDavid S. Miller return selinux_ip_forward(skb, state->in, PF_INET6); 5213effad8dfSPaul Moore } 5214effad8dfSPaul Moore #endif /* IPV6 */ 5215effad8dfSPaul Moore 5216948bf85cSPaul Moore static unsigned int selinux_ip_output(struct sk_buff *skb, 5217948bf85cSPaul Moore u16 family) 5218948bf85cSPaul Moore { 521947180068SPaul Moore struct sock *sk; 5220948bf85cSPaul Moore u32 sid; 5221948bf85cSPaul Moore 5222948bf85cSPaul Moore if (!netlbl_enabled()) 5223948bf85cSPaul Moore return NF_ACCEPT; 5224948bf85cSPaul Moore 5225948bf85cSPaul Moore /* we do this in the LOCAL_OUT path and not the POST_ROUTING path 5226948bf85cSPaul Moore * because we want to make sure we apply the necessary labeling 5227948bf85cSPaul Moore * before IPsec is applied so we can leverage AH protection */ 522847180068SPaul Moore sk = skb->sk; 522947180068SPaul Moore if (sk) { 523047180068SPaul Moore struct sk_security_struct *sksec; 523147180068SPaul Moore 5232e446f9dfSEric Dumazet if (sk_listener(sk)) 523347180068SPaul Moore /* if the socket is the listening state then this 523447180068SPaul Moore * packet is a SYN-ACK packet which means it needs to 523547180068SPaul Moore * be labeled based on the connection/request_sock and 523647180068SPaul Moore * not the parent socket. unfortunately, we can't 523747180068SPaul Moore * lookup the request_sock yet as it isn't queued on 523847180068SPaul Moore * the parent socket until after the SYN-ACK is sent. 523947180068SPaul Moore * the "solution" is to simply pass the packet as-is 524047180068SPaul Moore * as any IP option based labeling should be copied 524147180068SPaul Moore * from the initial connection request (in the IP 524247180068SPaul Moore * layer). it is far from ideal, but until we get a 524347180068SPaul Moore * security label in the packet itself this is the 524447180068SPaul Moore * best we can do. */ 524547180068SPaul Moore return NF_ACCEPT; 524647180068SPaul Moore 524747180068SPaul Moore /* standard practice, label using the parent socket */ 524847180068SPaul Moore sksec = sk->sk_security; 5249948bf85cSPaul Moore sid = sksec->sid; 5250948bf85cSPaul Moore } else 5251948bf85cSPaul Moore sid = SECINITSID_KERNEL; 5252948bf85cSPaul Moore if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0) 5253948bf85cSPaul Moore return NF_DROP; 5254948bf85cSPaul Moore 5255948bf85cSPaul Moore return NF_ACCEPT; 5256948bf85cSPaul Moore } 5257948bf85cSPaul Moore 525806198b34SEric W. Biederman static unsigned int selinux_ipv4_output(void *priv, 5259948bf85cSPaul Moore struct sk_buff *skb, 5260238e54c9SDavid S. Miller const struct nf_hook_state *state) 5261948bf85cSPaul Moore { 5262948bf85cSPaul Moore return selinux_ip_output(skb, PF_INET); 5263948bf85cSPaul Moore } 5264948bf85cSPaul Moore 52651a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 52662917f57bSHuw Davies static unsigned int selinux_ipv6_output(void *priv, 52672917f57bSHuw Davies struct sk_buff *skb, 52682917f57bSHuw Davies const struct nf_hook_state *state) 52692917f57bSHuw Davies { 52702917f57bSHuw Davies return selinux_ip_output(skb, PF_INET6); 52712917f57bSHuw Davies } 52722917f57bSHuw Davies #endif /* IPV6 */ 52732917f57bSHuw Davies 5274effad8dfSPaul Moore static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb, 5275effad8dfSPaul Moore int ifindex, 5276d8395c87SPaul Moore u16 family) 52774e5ab4cbSJames Morris { 527854abc686SEric Dumazet struct sock *sk = skb_to_full_sk(skb); 52794237c75cSVenkat Yekkirala struct sk_security_struct *sksec; 52802bf49690SThomas Liu struct common_audit_data ad; 528148c62af6SEric Paris struct lsm_network_audit net = {0,}; 5282d8395c87SPaul Moore char *addrp; 5283d8395c87SPaul Moore u8 proto; 52844e5ab4cbSJames Morris 5285effad8dfSPaul Moore if (sk == NULL) 5286effad8dfSPaul Moore return NF_ACCEPT; 52874237c75cSVenkat Yekkirala sksec = sk->sk_security; 52884e5ab4cbSJames Morris 528950c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 529048c62af6SEric Paris ad.u.net = &net; 529148c62af6SEric Paris ad.u.net->netif = ifindex; 529248c62af6SEric Paris ad.u.net->family = family; 5293d8395c87SPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto)) 5294d8395c87SPaul Moore return NF_DROP; 5295d8395c87SPaul Moore 529658bfbb51SPaul Moore if (selinux_secmark_enabled()) 5297effad8dfSPaul Moore if (avc_has_perm(sksec->sid, skb->secmark, 5298d8395c87SPaul Moore SECCLASS_PACKET, PACKET__SEND, &ad)) 52992fe66ec2SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 53001da177e4SLinus Torvalds 5301d8395c87SPaul Moore if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto)) 53022fe66ec2SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 5303effad8dfSPaul Moore 5304effad8dfSPaul Moore return NF_ACCEPT; 5305effad8dfSPaul Moore } 5306effad8dfSPaul Moore 5307cbe0d6e8SPaul Moore static unsigned int selinux_ip_postroute(struct sk_buff *skb, 5308cbe0d6e8SPaul Moore const struct net_device *outdev, 5309effad8dfSPaul Moore u16 family) 5310effad8dfSPaul Moore { 5311effad8dfSPaul Moore u32 secmark_perm; 5312effad8dfSPaul Moore u32 peer_sid; 5313cbe0d6e8SPaul Moore int ifindex = outdev->ifindex; 5314effad8dfSPaul Moore struct sock *sk; 53152bf49690SThomas Liu struct common_audit_data ad; 531648c62af6SEric Paris struct lsm_network_audit net = {0,}; 5317effad8dfSPaul Moore char *addrp; 5318effad8dfSPaul Moore u8 secmark_active; 5319effad8dfSPaul Moore u8 peerlbl_active; 5320effad8dfSPaul Moore 5321effad8dfSPaul Moore /* If any sort of compatibility mode is enabled then handoff processing 5322effad8dfSPaul Moore * to the selinux_ip_postroute_compat() function to deal with the 5323effad8dfSPaul Moore * special handling. We do this in an attempt to keep this function 5324effad8dfSPaul Moore * as fast and as clean as possible. */ 532558bfbb51SPaul Moore if (!selinux_policycap_netpeer) 5326d8395c87SPaul Moore return selinux_ip_postroute_compat(skb, ifindex, family); 5327c0828e50SPaul Moore 5328effad8dfSPaul Moore secmark_active = selinux_secmark_enabled(); 53292be4d74fSChris PeBenito peerlbl_active = selinux_peerlbl_enabled(); 5330effad8dfSPaul Moore if (!secmark_active && !peerlbl_active) 5331effad8dfSPaul Moore return NF_ACCEPT; 5332effad8dfSPaul Moore 533354abc686SEric Dumazet sk = skb_to_full_sk(skb); 5334c0828e50SPaul Moore 5335effad8dfSPaul Moore #ifdef CONFIG_XFRM 5336effad8dfSPaul Moore /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec 5337effad8dfSPaul Moore * packet transformation so allow the packet to pass without any checks 5338effad8dfSPaul Moore * since we'll have another chance to perform access control checks 5339effad8dfSPaul Moore * when the packet is on it's final way out. 5340effad8dfSPaul Moore * NOTE: there appear to be some IPv6 multicast cases where skb->dst 5341c0828e50SPaul Moore * is NULL, in this case go ahead and apply access control. 5342c0828e50SPaul Moore * NOTE: if this is a local socket (skb->sk != NULL) that is in the 5343c0828e50SPaul Moore * TCP listening state we cannot wait until the XFRM processing 5344c0828e50SPaul Moore * is done as we will miss out on the SA label if we do; 5345c0828e50SPaul Moore * unfortunately, this means more work, but it is only once per 5346c0828e50SPaul Moore * connection. */ 5347c0828e50SPaul Moore if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL && 5348e446f9dfSEric Dumazet !(sk && sk_listener(sk))) 5349effad8dfSPaul Moore return NF_ACCEPT; 5350effad8dfSPaul Moore #endif 5351effad8dfSPaul Moore 5352d8395c87SPaul Moore if (sk == NULL) { 5353446b8024SPaul Moore /* Without an associated socket the packet is either coming 5354446b8024SPaul Moore * from the kernel or it is being forwarded; check the packet 5355446b8024SPaul Moore * to determine which and if the packet is being forwarded 5356446b8024SPaul Moore * query the packet directly to determine the security label. */ 53574a7ab3dcSSteffen Klassert if (skb->skb_iif) { 5358d8395c87SPaul Moore secmark_perm = PACKET__FORWARD_OUT; 5359d8395c87SPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &peer_sid)) 536004f6d70fSEric Paris return NF_DROP; 53614a7ab3dcSSteffen Klassert } else { 53624a7ab3dcSSteffen Klassert secmark_perm = PACKET__SEND; 5363d8395c87SPaul Moore peer_sid = SECINITSID_KERNEL; 53644a7ab3dcSSteffen Klassert } 5365e446f9dfSEric Dumazet } else if (sk_listener(sk)) { 5366446b8024SPaul Moore /* Locally generated packet but the associated socket is in the 5367446b8024SPaul Moore * listening state which means this is a SYN-ACK packet. In 5368446b8024SPaul Moore * this particular case the correct security label is assigned 5369446b8024SPaul Moore * to the connection/request_sock but unfortunately we can't 5370446b8024SPaul Moore * query the request_sock as it isn't queued on the parent 5371446b8024SPaul Moore * socket until after the SYN-ACK packet is sent; the only 5372446b8024SPaul Moore * viable choice is to regenerate the label like we do in 5373446b8024SPaul Moore * selinux_inet_conn_request(). See also selinux_ip_output() 5374446b8024SPaul Moore * for similar problems. */ 5375446b8024SPaul Moore u32 skb_sid; 5376e446f9dfSEric Dumazet struct sk_security_struct *sksec; 5377e446f9dfSEric Dumazet 5378e446f9dfSEric Dumazet sksec = sk->sk_security; 5379446b8024SPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &skb_sid)) 5380446b8024SPaul Moore return NF_DROP; 5381c0828e50SPaul Moore /* At this point, if the returned skb peerlbl is SECSID_NULL 5382c0828e50SPaul Moore * and the packet has been through at least one XFRM 5383c0828e50SPaul Moore * transformation then we must be dealing with the "final" 5384c0828e50SPaul Moore * form of labeled IPsec packet; since we've already applied 5385c0828e50SPaul Moore * all of our access controls on this packet we can safely 5386c0828e50SPaul Moore * pass the packet. */ 5387c0828e50SPaul Moore if (skb_sid == SECSID_NULL) { 5388c0828e50SPaul Moore switch (family) { 5389c0828e50SPaul Moore case PF_INET: 5390c0828e50SPaul Moore if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) 5391c0828e50SPaul Moore return NF_ACCEPT; 5392c0828e50SPaul Moore break; 5393c0828e50SPaul Moore case PF_INET6: 5394c0828e50SPaul Moore if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) 5395c0828e50SPaul Moore return NF_ACCEPT; 5396a7a91a19SPaul Moore break; 5397c0828e50SPaul Moore default: 5398c0828e50SPaul Moore return NF_DROP_ERR(-ECONNREFUSED); 5399c0828e50SPaul Moore } 5400c0828e50SPaul Moore } 5401446b8024SPaul Moore if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid)) 5402446b8024SPaul Moore return NF_DROP; 5403446b8024SPaul Moore secmark_perm = PACKET__SEND; 5404d8395c87SPaul Moore } else { 5405446b8024SPaul Moore /* Locally generated packet, fetch the security label from the 5406446b8024SPaul Moore * associated socket. */ 5407effad8dfSPaul Moore struct sk_security_struct *sksec = sk->sk_security; 5408effad8dfSPaul Moore peer_sid = sksec->sid; 5409effad8dfSPaul Moore secmark_perm = PACKET__SEND; 5410effad8dfSPaul Moore } 5411effad8dfSPaul Moore 541250c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 541348c62af6SEric Paris ad.u.net = &net; 541448c62af6SEric Paris ad.u.net->netif = ifindex; 541548c62af6SEric Paris ad.u.net->family = family; 5416d8395c87SPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL)) 541704f6d70fSEric Paris return NF_DROP; 5418d8395c87SPaul Moore 5419effad8dfSPaul Moore if (secmark_active) 5420effad8dfSPaul Moore if (avc_has_perm(peer_sid, skb->secmark, 5421effad8dfSPaul Moore SECCLASS_PACKET, secmark_perm, &ad)) 54221f1aaf82SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 5423effad8dfSPaul Moore 5424effad8dfSPaul Moore if (peerlbl_active) { 5425effad8dfSPaul Moore u32 if_sid; 5426effad8dfSPaul Moore u32 node_sid; 5427effad8dfSPaul Moore 5428cbe0d6e8SPaul Moore if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid)) 542904f6d70fSEric Paris return NF_DROP; 5430effad8dfSPaul Moore if (avc_has_perm(peer_sid, if_sid, 5431effad8dfSPaul Moore SECCLASS_NETIF, NETIF__EGRESS, &ad)) 54321f1aaf82SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 5433effad8dfSPaul Moore 5434effad8dfSPaul Moore if (sel_netnode_sid(addrp, family, &node_sid)) 543504f6d70fSEric Paris return NF_DROP; 5436effad8dfSPaul Moore if (avc_has_perm(peer_sid, node_sid, 5437effad8dfSPaul Moore SECCLASS_NODE, NODE__SENDTO, &ad)) 54381f1aaf82SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 5439effad8dfSPaul Moore } 5440effad8dfSPaul Moore 5441effad8dfSPaul Moore return NF_ACCEPT; 5442effad8dfSPaul Moore } 5443effad8dfSPaul Moore 544406198b34SEric W. Biederman static unsigned int selinux_ipv4_postroute(void *priv, 5445a224be76SDavid S. Miller struct sk_buff *skb, 5446238e54c9SDavid S. Miller const struct nf_hook_state *state) 54471da177e4SLinus Torvalds { 5448238e54c9SDavid S. Miller return selinux_ip_postroute(skb, state->out, PF_INET); 54491da177e4SLinus Torvalds } 54501da177e4SLinus Torvalds 54511a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 545206198b34SEric W. Biederman static unsigned int selinux_ipv6_postroute(void *priv, 5453a224be76SDavid S. Miller struct sk_buff *skb, 5454238e54c9SDavid S. Miller const struct nf_hook_state *state) 54551da177e4SLinus Torvalds { 5456238e54c9SDavid S. Miller return selinux_ip_postroute(skb, state->out, PF_INET6); 54571da177e4SLinus Torvalds } 54581da177e4SLinus Torvalds #endif /* IPV6 */ 54591da177e4SLinus Torvalds 54601da177e4SLinus Torvalds #endif /* CONFIG_NETFILTER */ 54611da177e4SLinus Torvalds 54621da177e4SLinus Torvalds static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb) 54631da177e4SLinus Torvalds { 5464941fc5b2SStephen Smalley return selinux_nlmsg_perm(sk, skb); 54651da177e4SLinus Torvalds } 54661da177e4SLinus Torvalds 5467be0554c9SStephen Smalley static int ipc_alloc_security(struct kern_ipc_perm *perm, 54681da177e4SLinus Torvalds u16 sclass) 54691da177e4SLinus Torvalds { 54701da177e4SLinus Torvalds struct ipc_security_struct *isec; 54711da177e4SLinus Torvalds 547289d155efSJames Morris isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL); 54731da177e4SLinus Torvalds if (!isec) 54741da177e4SLinus Torvalds return -ENOMEM; 54751da177e4SLinus Torvalds 54761da177e4SLinus Torvalds isec->sclass = sclass; 5477be0554c9SStephen Smalley isec->sid = current_sid(); 54781da177e4SLinus Torvalds perm->security = isec; 54791da177e4SLinus Torvalds 54801da177e4SLinus Torvalds return 0; 54811da177e4SLinus Torvalds } 54821da177e4SLinus Torvalds 54831da177e4SLinus Torvalds static void ipc_free_security(struct kern_ipc_perm *perm) 54841da177e4SLinus Torvalds { 54851da177e4SLinus Torvalds struct ipc_security_struct *isec = perm->security; 54861da177e4SLinus Torvalds perm->security = NULL; 54871da177e4SLinus Torvalds kfree(isec); 54881da177e4SLinus Torvalds } 54891da177e4SLinus Torvalds 54901da177e4SLinus Torvalds static int msg_msg_alloc_security(struct msg_msg *msg) 54911da177e4SLinus Torvalds { 54921da177e4SLinus Torvalds struct msg_security_struct *msec; 54931da177e4SLinus Torvalds 549489d155efSJames Morris msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL); 54951da177e4SLinus Torvalds if (!msec) 54961da177e4SLinus Torvalds return -ENOMEM; 54971da177e4SLinus Torvalds 54981da177e4SLinus Torvalds msec->sid = SECINITSID_UNLABELED; 54991da177e4SLinus Torvalds msg->security = msec; 55001da177e4SLinus Torvalds 55011da177e4SLinus Torvalds return 0; 55021da177e4SLinus Torvalds } 55031da177e4SLinus Torvalds 55041da177e4SLinus Torvalds static void msg_msg_free_security(struct msg_msg *msg) 55051da177e4SLinus Torvalds { 55061da177e4SLinus Torvalds struct msg_security_struct *msec = msg->security; 55071da177e4SLinus Torvalds 55081da177e4SLinus Torvalds msg->security = NULL; 55091da177e4SLinus Torvalds kfree(msec); 55101da177e4SLinus Torvalds } 55111da177e4SLinus Torvalds 55121da177e4SLinus Torvalds static int ipc_has_perm(struct kern_ipc_perm *ipc_perms, 55136af963f1SStephen Smalley u32 perms) 55141da177e4SLinus Torvalds { 55151da177e4SLinus Torvalds struct ipc_security_struct *isec; 55162bf49690SThomas Liu struct common_audit_data ad; 5517275bb41eSDavid Howells u32 sid = current_sid(); 55181da177e4SLinus Torvalds 55191da177e4SLinus Torvalds isec = ipc_perms->security; 55201da177e4SLinus Torvalds 552150c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 55221da177e4SLinus Torvalds ad.u.ipc_id = ipc_perms->key; 55231da177e4SLinus Torvalds 5524275bb41eSDavid Howells return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad); 55251da177e4SLinus Torvalds } 55261da177e4SLinus Torvalds 55271da177e4SLinus Torvalds static int selinux_msg_msg_alloc_security(struct msg_msg *msg) 55281da177e4SLinus Torvalds { 55291da177e4SLinus Torvalds return msg_msg_alloc_security(msg); 55301da177e4SLinus Torvalds } 55311da177e4SLinus Torvalds 55321da177e4SLinus Torvalds static void selinux_msg_msg_free_security(struct msg_msg *msg) 55331da177e4SLinus Torvalds { 55341da177e4SLinus Torvalds msg_msg_free_security(msg); 55351da177e4SLinus Torvalds } 55361da177e4SLinus Torvalds 55371da177e4SLinus Torvalds /* message queue security operations */ 55381da177e4SLinus Torvalds static int selinux_msg_queue_alloc_security(struct msg_queue *msq) 55391da177e4SLinus Torvalds { 55401da177e4SLinus Torvalds struct ipc_security_struct *isec; 55412bf49690SThomas Liu struct common_audit_data ad; 5542275bb41eSDavid Howells u32 sid = current_sid(); 55431da177e4SLinus Torvalds int rc; 55441da177e4SLinus Torvalds 5545be0554c9SStephen Smalley rc = ipc_alloc_security(&msq->q_perm, SECCLASS_MSGQ); 55461da177e4SLinus Torvalds if (rc) 55471da177e4SLinus Torvalds return rc; 55481da177e4SLinus Torvalds 55491da177e4SLinus Torvalds isec = msq->q_perm.security; 55501da177e4SLinus Torvalds 555150c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 55521da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 55531da177e4SLinus Torvalds 5554275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ, 55551da177e4SLinus Torvalds MSGQ__CREATE, &ad); 55561da177e4SLinus Torvalds if (rc) { 55571da177e4SLinus Torvalds ipc_free_security(&msq->q_perm); 55581da177e4SLinus Torvalds return rc; 55591da177e4SLinus Torvalds } 55601da177e4SLinus Torvalds return 0; 55611da177e4SLinus Torvalds } 55621da177e4SLinus Torvalds 55631da177e4SLinus Torvalds static void selinux_msg_queue_free_security(struct msg_queue *msq) 55641da177e4SLinus Torvalds { 55651da177e4SLinus Torvalds ipc_free_security(&msq->q_perm); 55661da177e4SLinus Torvalds } 55671da177e4SLinus Torvalds 55681da177e4SLinus Torvalds static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg) 55691da177e4SLinus Torvalds { 55701da177e4SLinus Torvalds struct ipc_security_struct *isec; 55712bf49690SThomas Liu struct common_audit_data ad; 5572275bb41eSDavid Howells u32 sid = current_sid(); 55731da177e4SLinus Torvalds 55741da177e4SLinus Torvalds isec = msq->q_perm.security; 55751da177e4SLinus Torvalds 557650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 55771da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 55781da177e4SLinus Torvalds 5579275bb41eSDavid Howells return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ, 55801da177e4SLinus Torvalds MSGQ__ASSOCIATE, &ad); 55811da177e4SLinus Torvalds } 55821da177e4SLinus Torvalds 55831da177e4SLinus Torvalds static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd) 55841da177e4SLinus Torvalds { 55851da177e4SLinus Torvalds int err; 55861da177e4SLinus Torvalds int perms; 55871da177e4SLinus Torvalds 55881da177e4SLinus Torvalds switch (cmd) { 55891da177e4SLinus Torvalds case IPC_INFO: 55901da177e4SLinus Torvalds case MSG_INFO: 55911da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 5592be0554c9SStephen Smalley return avc_has_perm(current_sid(), SECINITSID_KERNEL, 5593be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); 55941da177e4SLinus Torvalds case IPC_STAT: 55951da177e4SLinus Torvalds case MSG_STAT: 55961da177e4SLinus Torvalds perms = MSGQ__GETATTR | MSGQ__ASSOCIATE; 55971da177e4SLinus Torvalds break; 55981da177e4SLinus Torvalds case IPC_SET: 55991da177e4SLinus Torvalds perms = MSGQ__SETATTR; 56001da177e4SLinus Torvalds break; 56011da177e4SLinus Torvalds case IPC_RMID: 56021da177e4SLinus Torvalds perms = MSGQ__DESTROY; 56031da177e4SLinus Torvalds break; 56041da177e4SLinus Torvalds default: 56051da177e4SLinus Torvalds return 0; 56061da177e4SLinus Torvalds } 56071da177e4SLinus Torvalds 56086af963f1SStephen Smalley err = ipc_has_perm(&msq->q_perm, perms); 56091da177e4SLinus Torvalds return err; 56101da177e4SLinus Torvalds } 56111da177e4SLinus Torvalds 56121da177e4SLinus Torvalds static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg) 56131da177e4SLinus Torvalds { 56141da177e4SLinus Torvalds struct ipc_security_struct *isec; 56151da177e4SLinus Torvalds struct msg_security_struct *msec; 56162bf49690SThomas Liu struct common_audit_data ad; 5617275bb41eSDavid Howells u32 sid = current_sid(); 56181da177e4SLinus Torvalds int rc; 56191da177e4SLinus Torvalds 56201da177e4SLinus Torvalds isec = msq->q_perm.security; 56211da177e4SLinus Torvalds msec = msg->security; 56221da177e4SLinus Torvalds 56231da177e4SLinus Torvalds /* 56241da177e4SLinus Torvalds * First time through, need to assign label to the message 56251da177e4SLinus Torvalds */ 56261da177e4SLinus Torvalds if (msec->sid == SECINITSID_UNLABELED) { 56271da177e4SLinus Torvalds /* 56281da177e4SLinus Torvalds * Compute new sid based on current process and 56291da177e4SLinus Torvalds * message queue this message will be stored in 56301da177e4SLinus Torvalds */ 5631275bb41eSDavid Howells rc = security_transition_sid(sid, isec->sid, SECCLASS_MSG, 5632652bb9b0SEric Paris NULL, &msec->sid); 56331da177e4SLinus Torvalds if (rc) 56341da177e4SLinus Torvalds return rc; 56351da177e4SLinus Torvalds } 56361da177e4SLinus Torvalds 563750c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 56381da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 56391da177e4SLinus Torvalds 56401da177e4SLinus Torvalds /* Can this process write to the queue? */ 5641275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ, 56421da177e4SLinus Torvalds MSGQ__WRITE, &ad); 56431da177e4SLinus Torvalds if (!rc) 56441da177e4SLinus Torvalds /* Can this process send the message */ 5645275bb41eSDavid Howells rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG, 5646275bb41eSDavid Howells MSG__SEND, &ad); 56471da177e4SLinus Torvalds if (!rc) 56481da177e4SLinus Torvalds /* Can the message be put in the queue? */ 5649275bb41eSDavid Howells rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ, 5650275bb41eSDavid Howells MSGQ__ENQUEUE, &ad); 56511da177e4SLinus Torvalds 56521da177e4SLinus Torvalds return rc; 56531da177e4SLinus Torvalds } 56541da177e4SLinus Torvalds 56551da177e4SLinus Torvalds static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg, 56561da177e4SLinus Torvalds struct task_struct *target, 56571da177e4SLinus Torvalds long type, int mode) 56581da177e4SLinus Torvalds { 56591da177e4SLinus Torvalds struct ipc_security_struct *isec; 56601da177e4SLinus Torvalds struct msg_security_struct *msec; 56612bf49690SThomas Liu struct common_audit_data ad; 5662275bb41eSDavid Howells u32 sid = task_sid(target); 56631da177e4SLinus Torvalds int rc; 56641da177e4SLinus Torvalds 56651da177e4SLinus Torvalds isec = msq->q_perm.security; 56661da177e4SLinus Torvalds msec = msg->security; 56671da177e4SLinus Torvalds 566850c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 56691da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 56701da177e4SLinus Torvalds 5671275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, 56721da177e4SLinus Torvalds SECCLASS_MSGQ, MSGQ__READ, &ad); 56731da177e4SLinus Torvalds if (!rc) 5674275bb41eSDavid Howells rc = avc_has_perm(sid, msec->sid, 56751da177e4SLinus Torvalds SECCLASS_MSG, MSG__RECEIVE, &ad); 56761da177e4SLinus Torvalds return rc; 56771da177e4SLinus Torvalds } 56781da177e4SLinus Torvalds 56791da177e4SLinus Torvalds /* Shared Memory security operations */ 56801da177e4SLinus Torvalds static int selinux_shm_alloc_security(struct shmid_kernel *shp) 56811da177e4SLinus Torvalds { 56821da177e4SLinus Torvalds struct ipc_security_struct *isec; 56832bf49690SThomas Liu struct common_audit_data ad; 5684275bb41eSDavid Howells u32 sid = current_sid(); 56851da177e4SLinus Torvalds int rc; 56861da177e4SLinus Torvalds 5687be0554c9SStephen Smalley rc = ipc_alloc_security(&shp->shm_perm, SECCLASS_SHM); 56881da177e4SLinus Torvalds if (rc) 56891da177e4SLinus Torvalds return rc; 56901da177e4SLinus Torvalds 56911da177e4SLinus Torvalds isec = shp->shm_perm.security; 56921da177e4SLinus Torvalds 569350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 56941da177e4SLinus Torvalds ad.u.ipc_id = shp->shm_perm.key; 56951da177e4SLinus Torvalds 5696275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, SECCLASS_SHM, 56971da177e4SLinus Torvalds SHM__CREATE, &ad); 56981da177e4SLinus Torvalds if (rc) { 56991da177e4SLinus Torvalds ipc_free_security(&shp->shm_perm); 57001da177e4SLinus Torvalds return rc; 57011da177e4SLinus Torvalds } 57021da177e4SLinus Torvalds return 0; 57031da177e4SLinus Torvalds } 57041da177e4SLinus Torvalds 57051da177e4SLinus Torvalds static void selinux_shm_free_security(struct shmid_kernel *shp) 57061da177e4SLinus Torvalds { 57071da177e4SLinus Torvalds ipc_free_security(&shp->shm_perm); 57081da177e4SLinus Torvalds } 57091da177e4SLinus Torvalds 57101da177e4SLinus Torvalds static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg) 57111da177e4SLinus Torvalds { 57121da177e4SLinus Torvalds struct ipc_security_struct *isec; 57132bf49690SThomas Liu struct common_audit_data ad; 5714275bb41eSDavid Howells u32 sid = current_sid(); 57151da177e4SLinus Torvalds 57161da177e4SLinus Torvalds isec = shp->shm_perm.security; 57171da177e4SLinus Torvalds 571850c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 57191da177e4SLinus Torvalds ad.u.ipc_id = shp->shm_perm.key; 57201da177e4SLinus Torvalds 5721275bb41eSDavid Howells return avc_has_perm(sid, isec->sid, SECCLASS_SHM, 57221da177e4SLinus Torvalds SHM__ASSOCIATE, &ad); 57231da177e4SLinus Torvalds } 57241da177e4SLinus Torvalds 57251da177e4SLinus Torvalds /* Note, at this point, shp is locked down */ 57261da177e4SLinus Torvalds static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd) 57271da177e4SLinus Torvalds { 57281da177e4SLinus Torvalds int perms; 57291da177e4SLinus Torvalds int err; 57301da177e4SLinus Torvalds 57311da177e4SLinus Torvalds switch (cmd) { 57321da177e4SLinus Torvalds case IPC_INFO: 57331da177e4SLinus Torvalds case SHM_INFO: 57341da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 5735be0554c9SStephen Smalley return avc_has_perm(current_sid(), SECINITSID_KERNEL, 5736be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); 57371da177e4SLinus Torvalds case IPC_STAT: 57381da177e4SLinus Torvalds case SHM_STAT: 57391da177e4SLinus Torvalds perms = SHM__GETATTR | SHM__ASSOCIATE; 57401da177e4SLinus Torvalds break; 57411da177e4SLinus Torvalds case IPC_SET: 57421da177e4SLinus Torvalds perms = SHM__SETATTR; 57431da177e4SLinus Torvalds break; 57441da177e4SLinus Torvalds case SHM_LOCK: 57451da177e4SLinus Torvalds case SHM_UNLOCK: 57461da177e4SLinus Torvalds perms = SHM__LOCK; 57471da177e4SLinus Torvalds break; 57481da177e4SLinus Torvalds case IPC_RMID: 57491da177e4SLinus Torvalds perms = SHM__DESTROY; 57501da177e4SLinus Torvalds break; 57511da177e4SLinus Torvalds default: 57521da177e4SLinus Torvalds return 0; 57531da177e4SLinus Torvalds } 57541da177e4SLinus Torvalds 57556af963f1SStephen Smalley err = ipc_has_perm(&shp->shm_perm, perms); 57561da177e4SLinus Torvalds return err; 57571da177e4SLinus Torvalds } 57581da177e4SLinus Torvalds 57591da177e4SLinus Torvalds static int selinux_shm_shmat(struct shmid_kernel *shp, 57601da177e4SLinus Torvalds char __user *shmaddr, int shmflg) 57611da177e4SLinus Torvalds { 57621da177e4SLinus Torvalds u32 perms; 57631da177e4SLinus Torvalds 57641da177e4SLinus Torvalds if (shmflg & SHM_RDONLY) 57651da177e4SLinus Torvalds perms = SHM__READ; 57661da177e4SLinus Torvalds else 57671da177e4SLinus Torvalds perms = SHM__READ | SHM__WRITE; 57681da177e4SLinus Torvalds 57696af963f1SStephen Smalley return ipc_has_perm(&shp->shm_perm, perms); 57701da177e4SLinus Torvalds } 57711da177e4SLinus Torvalds 57721da177e4SLinus Torvalds /* Semaphore security operations */ 57731da177e4SLinus Torvalds static int selinux_sem_alloc_security(struct sem_array *sma) 57741da177e4SLinus Torvalds { 57751da177e4SLinus Torvalds struct ipc_security_struct *isec; 57762bf49690SThomas Liu struct common_audit_data ad; 5777275bb41eSDavid Howells u32 sid = current_sid(); 57781da177e4SLinus Torvalds int rc; 57791da177e4SLinus Torvalds 5780be0554c9SStephen Smalley rc = ipc_alloc_security(&sma->sem_perm, SECCLASS_SEM); 57811da177e4SLinus Torvalds if (rc) 57821da177e4SLinus Torvalds return rc; 57831da177e4SLinus Torvalds 57841da177e4SLinus Torvalds isec = sma->sem_perm.security; 57851da177e4SLinus Torvalds 578650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 57871da177e4SLinus Torvalds ad.u.ipc_id = sma->sem_perm.key; 57881da177e4SLinus Torvalds 5789275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, SECCLASS_SEM, 57901da177e4SLinus Torvalds SEM__CREATE, &ad); 57911da177e4SLinus Torvalds if (rc) { 57921da177e4SLinus Torvalds ipc_free_security(&sma->sem_perm); 57931da177e4SLinus Torvalds return rc; 57941da177e4SLinus Torvalds } 57951da177e4SLinus Torvalds return 0; 57961da177e4SLinus Torvalds } 57971da177e4SLinus Torvalds 57981da177e4SLinus Torvalds static void selinux_sem_free_security(struct sem_array *sma) 57991da177e4SLinus Torvalds { 58001da177e4SLinus Torvalds ipc_free_security(&sma->sem_perm); 58011da177e4SLinus Torvalds } 58021da177e4SLinus Torvalds 58031da177e4SLinus Torvalds static int selinux_sem_associate(struct sem_array *sma, int semflg) 58041da177e4SLinus Torvalds { 58051da177e4SLinus Torvalds struct ipc_security_struct *isec; 58062bf49690SThomas Liu struct common_audit_data ad; 5807275bb41eSDavid Howells u32 sid = current_sid(); 58081da177e4SLinus Torvalds 58091da177e4SLinus Torvalds isec = sma->sem_perm.security; 58101da177e4SLinus Torvalds 581150c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 58121da177e4SLinus Torvalds ad.u.ipc_id = sma->sem_perm.key; 58131da177e4SLinus Torvalds 5814275bb41eSDavid Howells return avc_has_perm(sid, isec->sid, SECCLASS_SEM, 58151da177e4SLinus Torvalds SEM__ASSOCIATE, &ad); 58161da177e4SLinus Torvalds } 58171da177e4SLinus Torvalds 58181da177e4SLinus Torvalds /* Note, at this point, sma is locked down */ 58191da177e4SLinus Torvalds static int selinux_sem_semctl(struct sem_array *sma, int cmd) 58201da177e4SLinus Torvalds { 58211da177e4SLinus Torvalds int err; 58221da177e4SLinus Torvalds u32 perms; 58231da177e4SLinus Torvalds 58241da177e4SLinus Torvalds switch (cmd) { 58251da177e4SLinus Torvalds case IPC_INFO: 58261da177e4SLinus Torvalds case SEM_INFO: 58271da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 5828be0554c9SStephen Smalley return avc_has_perm(current_sid(), SECINITSID_KERNEL, 5829be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); 58301da177e4SLinus Torvalds case GETPID: 58311da177e4SLinus Torvalds case GETNCNT: 58321da177e4SLinus Torvalds case GETZCNT: 58331da177e4SLinus Torvalds perms = SEM__GETATTR; 58341da177e4SLinus Torvalds break; 58351da177e4SLinus Torvalds case GETVAL: 58361da177e4SLinus Torvalds case GETALL: 58371da177e4SLinus Torvalds perms = SEM__READ; 58381da177e4SLinus Torvalds break; 58391da177e4SLinus Torvalds case SETVAL: 58401da177e4SLinus Torvalds case SETALL: 58411da177e4SLinus Torvalds perms = SEM__WRITE; 58421da177e4SLinus Torvalds break; 58431da177e4SLinus Torvalds case IPC_RMID: 58441da177e4SLinus Torvalds perms = SEM__DESTROY; 58451da177e4SLinus Torvalds break; 58461da177e4SLinus Torvalds case IPC_SET: 58471da177e4SLinus Torvalds perms = SEM__SETATTR; 58481da177e4SLinus Torvalds break; 58491da177e4SLinus Torvalds case IPC_STAT: 58501da177e4SLinus Torvalds case SEM_STAT: 58511da177e4SLinus Torvalds perms = SEM__GETATTR | SEM__ASSOCIATE; 58521da177e4SLinus Torvalds break; 58531da177e4SLinus Torvalds default: 58541da177e4SLinus Torvalds return 0; 58551da177e4SLinus Torvalds } 58561da177e4SLinus Torvalds 58576af963f1SStephen Smalley err = ipc_has_perm(&sma->sem_perm, perms); 58581da177e4SLinus Torvalds return err; 58591da177e4SLinus Torvalds } 58601da177e4SLinus Torvalds 58611da177e4SLinus Torvalds static int selinux_sem_semop(struct sem_array *sma, 58621da177e4SLinus Torvalds struct sembuf *sops, unsigned nsops, int alter) 58631da177e4SLinus Torvalds { 58641da177e4SLinus Torvalds u32 perms; 58651da177e4SLinus Torvalds 58661da177e4SLinus Torvalds if (alter) 58671da177e4SLinus Torvalds perms = SEM__READ | SEM__WRITE; 58681da177e4SLinus Torvalds else 58691da177e4SLinus Torvalds perms = SEM__READ; 58701da177e4SLinus Torvalds 58716af963f1SStephen Smalley return ipc_has_perm(&sma->sem_perm, perms); 58721da177e4SLinus Torvalds } 58731da177e4SLinus Torvalds 58741da177e4SLinus Torvalds static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag) 58751da177e4SLinus Torvalds { 58761da177e4SLinus Torvalds u32 av = 0; 58771da177e4SLinus Torvalds 58781da177e4SLinus Torvalds av = 0; 58791da177e4SLinus Torvalds if (flag & S_IRUGO) 58801da177e4SLinus Torvalds av |= IPC__UNIX_READ; 58811da177e4SLinus Torvalds if (flag & S_IWUGO) 58821da177e4SLinus Torvalds av |= IPC__UNIX_WRITE; 58831da177e4SLinus Torvalds 58841da177e4SLinus Torvalds if (av == 0) 58851da177e4SLinus Torvalds return 0; 58861da177e4SLinus Torvalds 58876af963f1SStephen Smalley return ipc_has_perm(ipcp, av); 58881da177e4SLinus Torvalds } 58891da177e4SLinus Torvalds 5890713a04aeSAhmed S. Darwish static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid) 5891713a04aeSAhmed S. Darwish { 5892713a04aeSAhmed S. Darwish struct ipc_security_struct *isec = ipcp->security; 5893713a04aeSAhmed S. Darwish *secid = isec->sid; 5894713a04aeSAhmed S. Darwish } 5895713a04aeSAhmed S. Darwish 58961da177e4SLinus Torvalds static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode) 58971da177e4SLinus Torvalds { 58981da177e4SLinus Torvalds if (inode) 58991da177e4SLinus Torvalds inode_doinit_with_dentry(inode, dentry); 59001da177e4SLinus Torvalds } 59011da177e4SLinus Torvalds 59021da177e4SLinus Torvalds static int selinux_getprocattr(struct task_struct *p, 590304ff9708SAl Viro char *name, char **value) 59041da177e4SLinus Torvalds { 5905275bb41eSDavid Howells const struct task_security_struct *__tsec; 59068c8570fbSDustin Kirkland u32 sid; 59071da177e4SLinus Torvalds int error; 590804ff9708SAl Viro unsigned len; 59091da177e4SLinus Torvalds 5910275bb41eSDavid Howells rcu_read_lock(); 5911275bb41eSDavid Howells __tsec = __task_cred(p)->security; 59121da177e4SLinus Torvalds 5913be0554c9SStephen Smalley if (current != p) { 5914be0554c9SStephen Smalley error = avc_has_perm(current_sid(), __tsec->sid, 5915be0554c9SStephen Smalley SECCLASS_PROCESS, PROCESS__GETATTR, NULL); 5916be0554c9SStephen Smalley if (error) 5917be0554c9SStephen Smalley goto bad; 5918be0554c9SStephen Smalley } 5919be0554c9SStephen Smalley 59201da177e4SLinus Torvalds if (!strcmp(name, "current")) 5921275bb41eSDavid Howells sid = __tsec->sid; 59221da177e4SLinus Torvalds else if (!strcmp(name, "prev")) 5923275bb41eSDavid Howells sid = __tsec->osid; 59241da177e4SLinus Torvalds else if (!strcmp(name, "exec")) 5925275bb41eSDavid Howells sid = __tsec->exec_sid; 59261da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 5927275bb41eSDavid Howells sid = __tsec->create_sid; 59284eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) 5929275bb41eSDavid Howells sid = __tsec->keycreate_sid; 593042c3e03eSEric Paris else if (!strcmp(name, "sockcreate")) 5931275bb41eSDavid Howells sid = __tsec->sockcreate_sid; 5932be0554c9SStephen Smalley else { 5933be0554c9SStephen Smalley error = -EINVAL; 5934be0554c9SStephen Smalley goto bad; 5935be0554c9SStephen Smalley } 5936275bb41eSDavid Howells rcu_read_unlock(); 59371da177e4SLinus Torvalds 59381da177e4SLinus Torvalds if (!sid) 59391da177e4SLinus Torvalds return 0; 59401da177e4SLinus Torvalds 594104ff9708SAl Viro error = security_sid_to_context(sid, value, &len); 594204ff9708SAl Viro if (error) 594304ff9708SAl Viro return error; 594404ff9708SAl Viro return len; 5945275bb41eSDavid Howells 5946be0554c9SStephen Smalley bad: 5947275bb41eSDavid Howells rcu_read_unlock(); 5948be0554c9SStephen Smalley return error; 59491da177e4SLinus Torvalds } 59501da177e4SLinus Torvalds 5951b21507e2SStephen Smalley static int selinux_setprocattr(const char *name, void *value, size_t size) 59521da177e4SLinus Torvalds { 59531da177e4SLinus Torvalds struct task_security_struct *tsec; 5954d84f4f99SDavid Howells struct cred *new; 5955be0554c9SStephen Smalley u32 mysid = current_sid(), sid = 0, ptsid; 59561da177e4SLinus Torvalds int error; 59571da177e4SLinus Torvalds char *str = value; 59581da177e4SLinus Torvalds 59591da177e4SLinus Torvalds /* 59601da177e4SLinus Torvalds * Basic control over ability to set these attributes at all. 59611da177e4SLinus Torvalds */ 59621da177e4SLinus Torvalds if (!strcmp(name, "exec")) 5963be0554c9SStephen Smalley error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS, 5964be0554c9SStephen Smalley PROCESS__SETEXEC, NULL); 59651da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 5966be0554c9SStephen Smalley error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS, 5967be0554c9SStephen Smalley PROCESS__SETFSCREATE, NULL); 59684eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) 5969be0554c9SStephen Smalley error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS, 5970be0554c9SStephen Smalley PROCESS__SETKEYCREATE, NULL); 597142c3e03eSEric Paris else if (!strcmp(name, "sockcreate")) 5972be0554c9SStephen Smalley error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS, 5973be0554c9SStephen Smalley PROCESS__SETSOCKCREATE, NULL); 59741da177e4SLinus Torvalds else if (!strcmp(name, "current")) 5975be0554c9SStephen Smalley error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS, 5976be0554c9SStephen Smalley PROCESS__SETCURRENT, NULL); 59771da177e4SLinus Torvalds else 59781da177e4SLinus Torvalds error = -EINVAL; 59791da177e4SLinus Torvalds if (error) 59801da177e4SLinus Torvalds return error; 59811da177e4SLinus Torvalds 59821da177e4SLinus Torvalds /* Obtain a SID for the context, if one was specified. */ 5983a050a570SStephen Smalley if (size && str[0] && str[0] != '\n') { 59841da177e4SLinus Torvalds if (str[size-1] == '\n') { 59851da177e4SLinus Torvalds str[size-1] = 0; 59861da177e4SLinus Torvalds size--; 59871da177e4SLinus Torvalds } 598852a4c640SNikolay Aleksandrov error = security_context_to_sid(value, size, &sid, GFP_KERNEL); 598912b29f34SStephen Smalley if (error == -EINVAL && !strcmp(name, "fscreate")) { 5990db59000aSStephen Smalley if (!has_cap_mac_admin(true)) { 5991d6ea83ecSEric Paris struct audit_buffer *ab; 5992d6ea83ecSEric Paris size_t audit_size; 5993d6ea83ecSEric Paris 5994d6ea83ecSEric Paris /* We strip a nul only if it is at the end, otherwise the 5995d6ea83ecSEric Paris * context contains a nul and we should audit that */ 5996d6ea83ecSEric Paris if (str[size - 1] == '\0') 5997d6ea83ecSEric Paris audit_size = size - 1; 5998d6ea83ecSEric Paris else 5999d6ea83ecSEric Paris audit_size = size; 6000d6ea83ecSEric Paris ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR); 6001d6ea83ecSEric Paris audit_log_format(ab, "op=fscreate invalid_context="); 6002d6ea83ecSEric Paris audit_log_n_untrustedstring(ab, value, audit_size); 6003d6ea83ecSEric Paris audit_log_end(ab); 6004d6ea83ecSEric Paris 600512b29f34SStephen Smalley return error; 6006d6ea83ecSEric Paris } 600712b29f34SStephen Smalley error = security_context_to_sid_force(value, size, 600812b29f34SStephen Smalley &sid); 600912b29f34SStephen Smalley } 60101da177e4SLinus Torvalds if (error) 60111da177e4SLinus Torvalds return error; 60121da177e4SLinus Torvalds } 60131da177e4SLinus Torvalds 6014d84f4f99SDavid Howells new = prepare_creds(); 6015d84f4f99SDavid Howells if (!new) 6016d84f4f99SDavid Howells return -ENOMEM; 6017d84f4f99SDavid Howells 60181da177e4SLinus Torvalds /* Permission checking based on the specified context is 60191da177e4SLinus Torvalds performed during the actual operation (execve, 60201da177e4SLinus Torvalds open/mkdir/...), when we know the full context of the 6021d84f4f99SDavid Howells operation. See selinux_bprm_set_creds for the execve 60221da177e4SLinus Torvalds checks and may_create for the file creation checks. The 60231da177e4SLinus Torvalds operation will then fail if the context is not permitted. */ 6024d84f4f99SDavid Howells tsec = new->security; 6025d84f4f99SDavid Howells if (!strcmp(name, "exec")) { 60261da177e4SLinus Torvalds tsec->exec_sid = sid; 6027d84f4f99SDavid Howells } else if (!strcmp(name, "fscreate")) { 60281da177e4SLinus Torvalds tsec->create_sid = sid; 6029d84f4f99SDavid Howells } else if (!strcmp(name, "keycreate")) { 6030be0554c9SStephen Smalley error = avc_has_perm(mysid, sid, SECCLASS_KEY, KEY__CREATE, 6031be0554c9SStephen Smalley NULL); 60324eb582cfSMichael LeMay if (error) 6033d84f4f99SDavid Howells goto abort_change; 60344eb582cfSMichael LeMay tsec->keycreate_sid = sid; 6035d84f4f99SDavid Howells } else if (!strcmp(name, "sockcreate")) { 603642c3e03eSEric Paris tsec->sockcreate_sid = sid; 6037d84f4f99SDavid Howells } else if (!strcmp(name, "current")) { 6038d84f4f99SDavid Howells error = -EINVAL; 60391da177e4SLinus Torvalds if (sid == 0) 6040d84f4f99SDavid Howells goto abort_change; 6041d9250deaSKaiGai Kohei 6042d84f4f99SDavid Howells /* Only allow single threaded processes to change context */ 6043d84f4f99SDavid Howells error = -EPERM; 60445bb459bbSOleg Nesterov if (!current_is_single_threaded()) { 6045d84f4f99SDavid Howells error = security_bounded_transition(tsec->sid, sid); 6046d84f4f99SDavid Howells if (error) 6047d84f4f99SDavid Howells goto abort_change; 60481da177e4SLinus Torvalds } 60491da177e4SLinus Torvalds 60501da177e4SLinus Torvalds /* Check permissions for the transition. */ 60511da177e4SLinus Torvalds error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS, 60521da177e4SLinus Torvalds PROCESS__DYNTRANSITION, NULL); 60531da177e4SLinus Torvalds if (error) 6054d84f4f99SDavid Howells goto abort_change; 60551da177e4SLinus Torvalds 60561da177e4SLinus Torvalds /* Check for ptracing, and update the task SID if ok. 60571da177e4SLinus Torvalds Otherwise, leave SID unchanged and fail. */ 6058be0554c9SStephen Smalley ptsid = ptrace_parent_sid(); 60590c6181cbSPaul Moore if (ptsid != 0) { 6060d84f4f99SDavid Howells error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS, 6061d84f4f99SDavid Howells PROCESS__PTRACE, NULL); 6062d84f4f99SDavid Howells if (error) 6063d84f4f99SDavid Howells goto abort_change; 6064d84f4f99SDavid Howells } 6065d84f4f99SDavid Howells 6066d84f4f99SDavid Howells tsec->sid = sid; 6067d84f4f99SDavid Howells } else { 6068d84f4f99SDavid Howells error = -EINVAL; 6069d84f4f99SDavid Howells goto abort_change; 6070d84f4f99SDavid Howells } 6071d84f4f99SDavid Howells 6072d84f4f99SDavid Howells commit_creds(new); 60731da177e4SLinus Torvalds return size; 6074d84f4f99SDavid Howells 6075d84f4f99SDavid Howells abort_change: 6076d84f4f99SDavid Howells abort_creds(new); 6077d84f4f99SDavid Howells return error; 60781da177e4SLinus Torvalds } 60791da177e4SLinus Torvalds 6080746df9b5SDavid Quigley static int selinux_ismaclabel(const char *name) 6081746df9b5SDavid Quigley { 6082746df9b5SDavid Quigley return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0); 6083746df9b5SDavid Quigley } 6084746df9b5SDavid Quigley 6085dc49c1f9SCatherine Zhang static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) 6086dc49c1f9SCatherine Zhang { 6087dc49c1f9SCatherine Zhang return security_sid_to_context(secid, secdata, seclen); 6088dc49c1f9SCatherine Zhang } 6089dc49c1f9SCatherine Zhang 60907bf570dcSDavid Howells static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid) 609163cb3449SDavid Howells { 609252a4c640SNikolay Aleksandrov return security_context_to_sid(secdata, seclen, secid, GFP_KERNEL); 609363cb3449SDavid Howells } 609463cb3449SDavid Howells 6095dc49c1f9SCatherine Zhang static void selinux_release_secctx(char *secdata, u32 seclen) 6096dc49c1f9SCatherine Zhang { 6097dc49c1f9SCatherine Zhang kfree(secdata); 6098dc49c1f9SCatherine Zhang } 6099dc49c1f9SCatherine Zhang 61006f3be9f5SAndreas Gruenbacher static void selinux_inode_invalidate_secctx(struct inode *inode) 61016f3be9f5SAndreas Gruenbacher { 61026f3be9f5SAndreas Gruenbacher struct inode_security_struct *isec = inode->i_security; 61036f3be9f5SAndreas Gruenbacher 61049287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 61056f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INVALID; 61069287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 61076f3be9f5SAndreas Gruenbacher } 61086f3be9f5SAndreas Gruenbacher 61091ee65e37SDavid P. Quigley /* 61101ee65e37SDavid P. Quigley * called with inode->i_mutex locked 61111ee65e37SDavid P. Quigley */ 61121ee65e37SDavid P. Quigley static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen) 61131ee65e37SDavid P. Quigley { 61141ee65e37SDavid P. Quigley return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0); 61151ee65e37SDavid P. Quigley } 61161ee65e37SDavid P. Quigley 61171ee65e37SDavid P. Quigley /* 61181ee65e37SDavid P. Quigley * called with inode->i_mutex locked 61191ee65e37SDavid P. Quigley */ 61201ee65e37SDavid P. Quigley static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen) 61211ee65e37SDavid P. Quigley { 61221ee65e37SDavid P. Quigley return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0); 61231ee65e37SDavid P. Quigley } 61241ee65e37SDavid P. Quigley 61251ee65e37SDavid P. Quigley static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen) 61261ee65e37SDavid P. Quigley { 61271ee65e37SDavid P. Quigley int len = 0; 61281ee65e37SDavid P. Quigley len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX, 61291ee65e37SDavid P. Quigley ctx, true); 61301ee65e37SDavid P. Quigley if (len < 0) 61311ee65e37SDavid P. Quigley return len; 61321ee65e37SDavid P. Quigley *ctxlen = len; 61331ee65e37SDavid P. Quigley return 0; 61341ee65e37SDavid P. Quigley } 6135d720024eSMichael LeMay #ifdef CONFIG_KEYS 6136d720024eSMichael LeMay 6137d84f4f99SDavid Howells static int selinux_key_alloc(struct key *k, const struct cred *cred, 61387e047ef5SDavid Howells unsigned long flags) 6139d720024eSMichael LeMay { 6140d84f4f99SDavid Howells const struct task_security_struct *tsec; 6141d720024eSMichael LeMay struct key_security_struct *ksec; 6142d720024eSMichael LeMay 6143d720024eSMichael LeMay ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL); 6144d720024eSMichael LeMay if (!ksec) 6145d720024eSMichael LeMay return -ENOMEM; 6146d720024eSMichael LeMay 6147d84f4f99SDavid Howells tsec = cred->security; 6148d84f4f99SDavid Howells if (tsec->keycreate_sid) 6149d84f4f99SDavid Howells ksec->sid = tsec->keycreate_sid; 61504eb582cfSMichael LeMay else 6151d84f4f99SDavid Howells ksec->sid = tsec->sid; 6152d720024eSMichael LeMay 6153275bb41eSDavid Howells k->security = ksec; 6154d720024eSMichael LeMay return 0; 6155d720024eSMichael LeMay } 6156d720024eSMichael LeMay 6157d720024eSMichael LeMay static void selinux_key_free(struct key *k) 6158d720024eSMichael LeMay { 6159d720024eSMichael LeMay struct key_security_struct *ksec = k->security; 6160d720024eSMichael LeMay 6161d720024eSMichael LeMay k->security = NULL; 6162d720024eSMichael LeMay kfree(ksec); 6163d720024eSMichael LeMay } 6164d720024eSMichael LeMay 6165d720024eSMichael LeMay static int selinux_key_permission(key_ref_t key_ref, 6166d84f4f99SDavid Howells const struct cred *cred, 6167f5895943SDavid Howells unsigned perm) 6168d720024eSMichael LeMay { 6169d720024eSMichael LeMay struct key *key; 6170d720024eSMichael LeMay struct key_security_struct *ksec; 6171275bb41eSDavid Howells u32 sid; 6172d720024eSMichael LeMay 6173d720024eSMichael LeMay /* if no specific permissions are requested, we skip the 6174d720024eSMichael LeMay permission check. No serious, additional covert channels 6175d720024eSMichael LeMay appear to be created. */ 6176d720024eSMichael LeMay if (perm == 0) 6177d720024eSMichael LeMay return 0; 6178d720024eSMichael LeMay 6179d84f4f99SDavid Howells sid = cred_sid(cred); 6180275bb41eSDavid Howells 6181275bb41eSDavid Howells key = key_ref_to_ptr(key_ref); 6182275bb41eSDavid Howells ksec = key->security; 6183275bb41eSDavid Howells 6184275bb41eSDavid Howells return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL); 6185d720024eSMichael LeMay } 6186d720024eSMichael LeMay 618770a5bb72SDavid Howells static int selinux_key_getsecurity(struct key *key, char **_buffer) 618870a5bb72SDavid Howells { 618970a5bb72SDavid Howells struct key_security_struct *ksec = key->security; 619070a5bb72SDavid Howells char *context = NULL; 619170a5bb72SDavid Howells unsigned len; 619270a5bb72SDavid Howells int rc; 619370a5bb72SDavid Howells 619470a5bb72SDavid Howells rc = security_sid_to_context(ksec->sid, &context, &len); 619570a5bb72SDavid Howells if (!rc) 619670a5bb72SDavid Howells rc = len; 619770a5bb72SDavid Howells *_buffer = context; 619870a5bb72SDavid Howells return rc; 619970a5bb72SDavid Howells } 62003a976fa6SDaniel Jurgens #endif 620170a5bb72SDavid Howells 62023a976fa6SDaniel Jurgens #ifdef CONFIG_SECURITY_INFINIBAND 6203cfc4d882SDaniel Jurgens static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val) 6204cfc4d882SDaniel Jurgens { 6205cfc4d882SDaniel Jurgens struct common_audit_data ad; 6206cfc4d882SDaniel Jurgens int err; 6207cfc4d882SDaniel Jurgens u32 sid = 0; 6208cfc4d882SDaniel Jurgens struct ib_security_struct *sec = ib_sec; 6209cfc4d882SDaniel Jurgens struct lsm_ibpkey_audit ibpkey; 6210cfc4d882SDaniel Jurgens 6211409dcf31SDaniel Jurgens err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid); 6212cfc4d882SDaniel Jurgens if (err) 6213cfc4d882SDaniel Jurgens return err; 6214cfc4d882SDaniel Jurgens 6215cfc4d882SDaniel Jurgens ad.type = LSM_AUDIT_DATA_IBPKEY; 6216cfc4d882SDaniel Jurgens ibpkey.subnet_prefix = subnet_prefix; 6217cfc4d882SDaniel Jurgens ibpkey.pkey = pkey_val; 6218cfc4d882SDaniel Jurgens ad.u.ibpkey = &ibpkey; 6219cfc4d882SDaniel Jurgens return avc_has_perm(sec->sid, sid, 6220cfc4d882SDaniel Jurgens SECCLASS_INFINIBAND_PKEY, 6221cfc4d882SDaniel Jurgens INFINIBAND_PKEY__ACCESS, &ad); 6222cfc4d882SDaniel Jurgens } 6223cfc4d882SDaniel Jurgens 6224ab861dfcSDaniel Jurgens static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name, 6225ab861dfcSDaniel Jurgens u8 port_num) 6226ab861dfcSDaniel Jurgens { 6227ab861dfcSDaniel Jurgens struct common_audit_data ad; 6228ab861dfcSDaniel Jurgens int err; 6229ab861dfcSDaniel Jurgens u32 sid = 0; 6230ab861dfcSDaniel Jurgens struct ib_security_struct *sec = ib_sec; 6231ab861dfcSDaniel Jurgens struct lsm_ibendport_audit ibendport; 6232ab861dfcSDaniel Jurgens 6233ab861dfcSDaniel Jurgens err = security_ib_endport_sid(dev_name, port_num, &sid); 6234ab861dfcSDaniel Jurgens 6235ab861dfcSDaniel Jurgens if (err) 6236ab861dfcSDaniel Jurgens return err; 6237ab861dfcSDaniel Jurgens 6238ab861dfcSDaniel Jurgens ad.type = LSM_AUDIT_DATA_IBENDPORT; 6239ab861dfcSDaniel Jurgens strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name)); 6240ab861dfcSDaniel Jurgens ibendport.port = port_num; 6241ab861dfcSDaniel Jurgens ad.u.ibendport = &ibendport; 6242ab861dfcSDaniel Jurgens return avc_has_perm(sec->sid, sid, 6243ab861dfcSDaniel Jurgens SECCLASS_INFINIBAND_ENDPORT, 6244ab861dfcSDaniel Jurgens INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad); 6245ab861dfcSDaniel Jurgens } 6246ab861dfcSDaniel Jurgens 62473a976fa6SDaniel Jurgens static int selinux_ib_alloc_security(void **ib_sec) 62483a976fa6SDaniel Jurgens { 62493a976fa6SDaniel Jurgens struct ib_security_struct *sec; 62503a976fa6SDaniel Jurgens 62513a976fa6SDaniel Jurgens sec = kzalloc(sizeof(*sec), GFP_KERNEL); 62523a976fa6SDaniel Jurgens if (!sec) 62533a976fa6SDaniel Jurgens return -ENOMEM; 62543a976fa6SDaniel Jurgens sec->sid = current_sid(); 62553a976fa6SDaniel Jurgens 62563a976fa6SDaniel Jurgens *ib_sec = sec; 62573a976fa6SDaniel Jurgens return 0; 62583a976fa6SDaniel Jurgens } 62593a976fa6SDaniel Jurgens 62603a976fa6SDaniel Jurgens static void selinux_ib_free_security(void *ib_sec) 62613a976fa6SDaniel Jurgens { 62623a976fa6SDaniel Jurgens kfree(ib_sec); 62633a976fa6SDaniel Jurgens } 6264d720024eSMichael LeMay #endif 6265d720024eSMichael LeMay 6266ec27c356SChenbo Feng #ifdef CONFIG_BPF_SYSCALL 6267ec27c356SChenbo Feng static int selinux_bpf(int cmd, union bpf_attr *attr, 6268ec27c356SChenbo Feng unsigned int size) 6269ec27c356SChenbo Feng { 6270ec27c356SChenbo Feng u32 sid = current_sid(); 6271ec27c356SChenbo Feng int ret; 6272ec27c356SChenbo Feng 6273ec27c356SChenbo Feng switch (cmd) { 6274ec27c356SChenbo Feng case BPF_MAP_CREATE: 6275ec27c356SChenbo Feng ret = avc_has_perm(sid, sid, SECCLASS_BPF, BPF__MAP_CREATE, 6276ec27c356SChenbo Feng NULL); 6277ec27c356SChenbo Feng break; 6278ec27c356SChenbo Feng case BPF_PROG_LOAD: 6279ec27c356SChenbo Feng ret = avc_has_perm(sid, sid, SECCLASS_BPF, BPF__PROG_LOAD, 6280ec27c356SChenbo Feng NULL); 6281ec27c356SChenbo Feng break; 6282ec27c356SChenbo Feng default: 6283ec27c356SChenbo Feng ret = 0; 6284ec27c356SChenbo Feng break; 6285ec27c356SChenbo Feng } 6286ec27c356SChenbo Feng 6287ec27c356SChenbo Feng return ret; 6288ec27c356SChenbo Feng } 6289ec27c356SChenbo Feng 6290ec27c356SChenbo Feng static u32 bpf_map_fmode_to_av(fmode_t fmode) 6291ec27c356SChenbo Feng { 6292ec27c356SChenbo Feng u32 av = 0; 6293ec27c356SChenbo Feng 6294ec27c356SChenbo Feng if (fmode & FMODE_READ) 6295ec27c356SChenbo Feng av |= BPF__MAP_READ; 6296ec27c356SChenbo Feng if (fmode & FMODE_WRITE) 6297ec27c356SChenbo Feng av |= BPF__MAP_WRITE; 6298ec27c356SChenbo Feng return av; 6299ec27c356SChenbo Feng } 6300ec27c356SChenbo Feng 6301f66e448cSChenbo Feng /* This function will check the file pass through unix socket or binder to see 6302f66e448cSChenbo Feng * if it is a bpf related object. And apply correspinding checks on the bpf 6303f66e448cSChenbo Feng * object based on the type. The bpf maps and programs, not like other files and 6304f66e448cSChenbo Feng * socket, are using a shared anonymous inode inside the kernel as their inode. 6305f66e448cSChenbo Feng * So checking that inode cannot identify if the process have privilege to 6306f66e448cSChenbo Feng * access the bpf object and that's why we have to add this additional check in 6307f66e448cSChenbo Feng * selinux_file_receive and selinux_binder_transfer_files. 6308f66e448cSChenbo Feng */ 6309f66e448cSChenbo Feng static int bpf_fd_pass(struct file *file, u32 sid) 6310f66e448cSChenbo Feng { 6311f66e448cSChenbo Feng struct bpf_security_struct *bpfsec; 6312f66e448cSChenbo Feng struct bpf_prog *prog; 6313f66e448cSChenbo Feng struct bpf_map *map; 6314f66e448cSChenbo Feng int ret; 6315f66e448cSChenbo Feng 6316f66e448cSChenbo Feng if (file->f_op == &bpf_map_fops) { 6317f66e448cSChenbo Feng map = file->private_data; 6318f66e448cSChenbo Feng bpfsec = map->security; 6319f66e448cSChenbo Feng ret = avc_has_perm(sid, bpfsec->sid, SECCLASS_BPF, 6320f66e448cSChenbo Feng bpf_map_fmode_to_av(file->f_mode), NULL); 6321f66e448cSChenbo Feng if (ret) 6322f66e448cSChenbo Feng return ret; 6323f66e448cSChenbo Feng } else if (file->f_op == &bpf_prog_fops) { 6324f66e448cSChenbo Feng prog = file->private_data; 6325f66e448cSChenbo Feng bpfsec = prog->aux->security; 6326f66e448cSChenbo Feng ret = avc_has_perm(sid, bpfsec->sid, SECCLASS_BPF, 6327f66e448cSChenbo Feng BPF__PROG_RUN, NULL); 6328f66e448cSChenbo Feng if (ret) 6329f66e448cSChenbo Feng return ret; 6330f66e448cSChenbo Feng } 6331f66e448cSChenbo Feng return 0; 6332f66e448cSChenbo Feng } 6333f66e448cSChenbo Feng 6334ec27c356SChenbo Feng static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode) 6335ec27c356SChenbo Feng { 6336ec27c356SChenbo Feng u32 sid = current_sid(); 6337ec27c356SChenbo Feng struct bpf_security_struct *bpfsec; 6338ec27c356SChenbo Feng 6339ec27c356SChenbo Feng bpfsec = map->security; 6340ec27c356SChenbo Feng return avc_has_perm(sid, bpfsec->sid, SECCLASS_BPF, 6341ec27c356SChenbo Feng bpf_map_fmode_to_av(fmode), NULL); 6342ec27c356SChenbo Feng } 6343ec27c356SChenbo Feng 6344ec27c356SChenbo Feng static int selinux_bpf_prog(struct bpf_prog *prog) 6345ec27c356SChenbo Feng { 6346ec27c356SChenbo Feng u32 sid = current_sid(); 6347ec27c356SChenbo Feng struct bpf_security_struct *bpfsec; 6348ec27c356SChenbo Feng 6349ec27c356SChenbo Feng bpfsec = prog->aux->security; 6350ec27c356SChenbo Feng return avc_has_perm(sid, bpfsec->sid, SECCLASS_BPF, 6351ec27c356SChenbo Feng BPF__PROG_RUN, NULL); 6352ec27c356SChenbo Feng } 6353ec27c356SChenbo Feng 6354ec27c356SChenbo Feng static int selinux_bpf_map_alloc(struct bpf_map *map) 6355ec27c356SChenbo Feng { 6356ec27c356SChenbo Feng struct bpf_security_struct *bpfsec; 6357ec27c356SChenbo Feng 6358ec27c356SChenbo Feng bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL); 6359ec27c356SChenbo Feng if (!bpfsec) 6360ec27c356SChenbo Feng return -ENOMEM; 6361ec27c356SChenbo Feng 6362ec27c356SChenbo Feng bpfsec->sid = current_sid(); 6363ec27c356SChenbo Feng map->security = bpfsec; 6364ec27c356SChenbo Feng 6365ec27c356SChenbo Feng return 0; 6366ec27c356SChenbo Feng } 6367ec27c356SChenbo Feng 6368ec27c356SChenbo Feng static void selinux_bpf_map_free(struct bpf_map *map) 6369ec27c356SChenbo Feng { 6370ec27c356SChenbo Feng struct bpf_security_struct *bpfsec = map->security; 6371ec27c356SChenbo Feng 6372ec27c356SChenbo Feng map->security = NULL; 6373ec27c356SChenbo Feng kfree(bpfsec); 6374ec27c356SChenbo Feng } 6375ec27c356SChenbo Feng 6376ec27c356SChenbo Feng static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux) 6377ec27c356SChenbo Feng { 6378ec27c356SChenbo Feng struct bpf_security_struct *bpfsec; 6379ec27c356SChenbo Feng 6380ec27c356SChenbo Feng bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL); 6381ec27c356SChenbo Feng if (!bpfsec) 6382ec27c356SChenbo Feng return -ENOMEM; 6383ec27c356SChenbo Feng 6384ec27c356SChenbo Feng bpfsec->sid = current_sid(); 6385ec27c356SChenbo Feng aux->security = bpfsec; 6386ec27c356SChenbo Feng 6387ec27c356SChenbo Feng return 0; 6388ec27c356SChenbo Feng } 6389ec27c356SChenbo Feng 6390ec27c356SChenbo Feng static void selinux_bpf_prog_free(struct bpf_prog_aux *aux) 6391ec27c356SChenbo Feng { 6392ec27c356SChenbo Feng struct bpf_security_struct *bpfsec = aux->security; 6393ec27c356SChenbo Feng 6394ec27c356SChenbo Feng aux->security = NULL; 6395ec27c356SChenbo Feng kfree(bpfsec); 6396ec27c356SChenbo Feng } 6397ec27c356SChenbo Feng #endif 6398ec27c356SChenbo Feng 6399ca97d939SJames Morris static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = { 6400e20b043aSCasey Schaufler LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr), 6401e20b043aSCasey Schaufler LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction), 6402e20b043aSCasey Schaufler LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder), 6403e20b043aSCasey Schaufler LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file), 6404076c54c5SAhmed S. Darwish 6405e20b043aSCasey Schaufler LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check), 6406e20b043aSCasey Schaufler LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme), 6407e20b043aSCasey Schaufler LSM_HOOK_INIT(capget, selinux_capget), 6408e20b043aSCasey Schaufler LSM_HOOK_INIT(capset, selinux_capset), 6409e20b043aSCasey Schaufler LSM_HOOK_INIT(capable, selinux_capable), 6410e20b043aSCasey Schaufler LSM_HOOK_INIT(quotactl, selinux_quotactl), 6411e20b043aSCasey Schaufler LSM_HOOK_INIT(quota_on, selinux_quota_on), 6412e20b043aSCasey Schaufler LSM_HOOK_INIT(syslog, selinux_syslog), 6413e20b043aSCasey Schaufler LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory), 641479af7307SStephen Smalley 6415e20b043aSCasey Schaufler LSM_HOOK_INIT(netlink_send, selinux_netlink_send), 64161da177e4SLinus Torvalds 6417e20b043aSCasey Schaufler LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds), 6418e20b043aSCasey Schaufler LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds), 6419e20b043aSCasey Schaufler LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds), 64201da177e4SLinus Torvalds 6421e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security), 6422e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security), 6423e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_copy_data, selinux_sb_copy_data), 6424e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_remount, selinux_sb_remount), 6425e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount), 6426e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options), 6427e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs), 6428e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_mount, selinux_mount), 6429e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_umount, selinux_umount), 6430e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts), 6431e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts), 6432e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_parse_opts_str, selinux_parse_opts_str), 64331da177e4SLinus Torvalds 6434e20b043aSCasey Schaufler LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security), 6435a518b0a5SVivek Goyal LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as), 6436e0007529SEric Paris 6437e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security), 6438e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security), 6439e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security), 6440e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_create, selinux_inode_create), 6441e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_link, selinux_inode_link), 6442e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink), 6443e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink), 6444e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir), 6445e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir), 6446e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod), 6447e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_rename, selinux_inode_rename), 6448e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink), 6449e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link), 6450e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_permission, selinux_inode_permission), 6451e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr), 6452e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr), 6453e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr), 6454e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr), 6455e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr), 6456e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr), 6457e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr), 6458e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity), 6459e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity), 6460e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity), 6461e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid), 646256909eb3SVivek Goyal LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up), 646319472b69SVivek Goyal LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr), 64641da177e4SLinus Torvalds 6465e20b043aSCasey Schaufler LSM_HOOK_INIT(file_permission, selinux_file_permission), 6466e20b043aSCasey Schaufler LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security), 6467e20b043aSCasey Schaufler LSM_HOOK_INIT(file_free_security, selinux_file_free_security), 6468e20b043aSCasey Schaufler LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl), 6469e20b043aSCasey Schaufler LSM_HOOK_INIT(mmap_file, selinux_mmap_file), 6470e20b043aSCasey Schaufler LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr), 6471e20b043aSCasey Schaufler LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect), 6472e20b043aSCasey Schaufler LSM_HOOK_INIT(file_lock, selinux_file_lock), 6473e20b043aSCasey Schaufler LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl), 6474e20b043aSCasey Schaufler LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner), 6475e20b043aSCasey Schaufler LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask), 6476e20b043aSCasey Schaufler LSM_HOOK_INIT(file_receive, selinux_file_receive), 64771da177e4SLinus Torvalds 6478e20b043aSCasey Schaufler LSM_HOOK_INIT(file_open, selinux_file_open), 64791da177e4SLinus Torvalds 6480a79be238STetsuo Handa LSM_HOOK_INIT(task_alloc, selinux_task_alloc), 6481e20b043aSCasey Schaufler LSM_HOOK_INIT(cred_alloc_blank, selinux_cred_alloc_blank), 6482e20b043aSCasey Schaufler LSM_HOOK_INIT(cred_free, selinux_cred_free), 6483e20b043aSCasey Schaufler LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare), 6484e20b043aSCasey Schaufler LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer), 6485e20b043aSCasey Schaufler LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as), 6486e20b043aSCasey Schaufler LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as), 6487e20b043aSCasey Schaufler LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request), 648861d612eaSJeff Vander Stoep LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file), 6489e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid), 6490e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid), 6491e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getsid, selinux_task_getsid), 6492e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid), 6493e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setnice, selinux_task_setnice), 6494e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio), 6495e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio), 6496791ec491SStephen Smalley LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit), 6497e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit), 6498e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler), 6499e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler), 6500e20b043aSCasey Schaufler LSM_HOOK_INIT(task_movememory, selinux_task_movememory), 6501e20b043aSCasey Schaufler LSM_HOOK_INIT(task_kill, selinux_task_kill), 6502e20b043aSCasey Schaufler LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode), 6503788e7dd4SYuichi Nakamura 6504e20b043aSCasey Schaufler LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission), 6505e20b043aSCasey Schaufler LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid), 65061da177e4SLinus Torvalds 6507e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security), 6508e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_msg_free_security, selinux_msg_msg_free_security), 65091da177e4SLinus Torvalds 6510e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_alloc_security, 6511e20b043aSCasey Schaufler selinux_msg_queue_alloc_security), 6512e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_free_security, selinux_msg_queue_free_security), 6513e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate), 6514e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl), 6515e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd), 6516e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv), 65171da177e4SLinus Torvalds 6518e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security), 6519e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_free_security, selinux_shm_free_security), 6520e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_associate, selinux_shm_associate), 6521e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl), 6522e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat), 65231da177e4SLinus Torvalds 6524e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security), 6525e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_free_security, selinux_sem_free_security), 6526e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_associate, selinux_sem_associate), 6527e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl), 6528e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_semop, selinux_sem_semop), 65291da177e4SLinus Torvalds 6530e20b043aSCasey Schaufler LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate), 65311da177e4SLinus Torvalds 6532e20b043aSCasey Schaufler LSM_HOOK_INIT(getprocattr, selinux_getprocattr), 6533e20b043aSCasey Schaufler LSM_HOOK_INIT(setprocattr, selinux_setprocattr), 65341da177e4SLinus Torvalds 6535e20b043aSCasey Schaufler LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel), 6536e20b043aSCasey Schaufler LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx), 6537e20b043aSCasey Schaufler LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid), 6538e20b043aSCasey Schaufler LSM_HOOK_INIT(release_secctx, selinux_release_secctx), 65396f3be9f5SAndreas Gruenbacher LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx), 6540e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx), 6541e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx), 6542e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx), 65431da177e4SLinus Torvalds 6544e20b043aSCasey Schaufler LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect), 6545e20b043aSCasey Schaufler LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send), 6546dc49c1f9SCatherine Zhang 6547e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_create, selinux_socket_create), 6548e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create), 6549e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_bind, selinux_socket_bind), 6550e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_connect, selinux_socket_connect), 6551e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_listen, selinux_socket_listen), 6552e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_accept, selinux_socket_accept), 6553e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg), 6554e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg), 6555e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname), 6556e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername), 6557e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt), 6558e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt), 6559e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown), 6560e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb), 6561e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getpeersec_stream, 6562e20b043aSCasey Schaufler selinux_socket_getpeersec_stream), 6563e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram), 6564e20b043aSCasey Schaufler LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security), 6565e20b043aSCasey Schaufler LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security), 6566e20b043aSCasey Schaufler LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security), 6567e20b043aSCasey Schaufler LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid), 6568e20b043aSCasey Schaufler LSM_HOOK_INIT(sock_graft, selinux_sock_graft), 6569e20b043aSCasey Schaufler LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request), 6570e20b043aSCasey Schaufler LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone), 6571e20b043aSCasey Schaufler LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established), 6572e20b043aSCasey Schaufler LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet), 6573e20b043aSCasey Schaufler LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc), 6574e20b043aSCasey Schaufler LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec), 6575e20b043aSCasey Schaufler LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow), 6576e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security), 6577e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security), 6578e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create), 6579e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue), 6580e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach), 6581e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open), 65823a976fa6SDaniel Jurgens #ifdef CONFIG_SECURITY_INFINIBAND 6583cfc4d882SDaniel Jurgens LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access), 6584ab861dfcSDaniel Jurgens LSM_HOOK_INIT(ib_endport_manage_subnet, 6585ab861dfcSDaniel Jurgens selinux_ib_endport_manage_subnet), 65863a976fa6SDaniel Jurgens LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security), 65873a976fa6SDaniel Jurgens LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security), 65883a976fa6SDaniel Jurgens #endif 6589d28d1e08STrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM 6590e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc), 6591e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone), 6592e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free), 6593e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete), 6594e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc), 6595e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_alloc_acquire, 6596e20b043aSCasey Schaufler selinux_xfrm_state_alloc_acquire), 6597e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free), 6598e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete), 6599e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup), 6600e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_pol_flow_match, 6601e20b043aSCasey Schaufler selinux_xfrm_state_pol_flow_match), 6602e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session), 66031da177e4SLinus Torvalds #endif 6604d720024eSMichael LeMay 6605d720024eSMichael LeMay #ifdef CONFIG_KEYS 6606e20b043aSCasey Schaufler LSM_HOOK_INIT(key_alloc, selinux_key_alloc), 6607e20b043aSCasey Schaufler LSM_HOOK_INIT(key_free, selinux_key_free), 6608e20b043aSCasey Schaufler LSM_HOOK_INIT(key_permission, selinux_key_permission), 6609e20b043aSCasey Schaufler LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity), 6610d720024eSMichael LeMay #endif 66119d57a7f9SAhmed S. Darwish 66129d57a7f9SAhmed S. Darwish #ifdef CONFIG_AUDIT 6613e20b043aSCasey Schaufler LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init), 6614e20b043aSCasey Schaufler LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known), 6615e20b043aSCasey Schaufler LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match), 6616e20b043aSCasey Schaufler LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free), 66179d57a7f9SAhmed S. Darwish #endif 6618ec27c356SChenbo Feng 6619ec27c356SChenbo Feng #ifdef CONFIG_BPF_SYSCALL 6620ec27c356SChenbo Feng LSM_HOOK_INIT(bpf, selinux_bpf), 6621ec27c356SChenbo Feng LSM_HOOK_INIT(bpf_map, selinux_bpf_map), 6622ec27c356SChenbo Feng LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog), 6623ec27c356SChenbo Feng LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc), 6624ec27c356SChenbo Feng LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc), 6625ec27c356SChenbo Feng LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free), 6626ec27c356SChenbo Feng LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free), 6627ec27c356SChenbo Feng #endif 66281da177e4SLinus Torvalds }; 66291da177e4SLinus Torvalds 66301da177e4SLinus Torvalds static __init int selinux_init(void) 66311da177e4SLinus Torvalds { 6632b1d9e6b0SCasey Schaufler if (!security_module_enable("selinux")) { 6633076c54c5SAhmed S. Darwish selinux_enabled = 0; 6634076c54c5SAhmed S. Darwish return 0; 6635076c54c5SAhmed S. Darwish } 6636076c54c5SAhmed S. Darwish 66371da177e4SLinus Torvalds if (!selinux_enabled) { 66381da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Disabled at boot.\n"); 66391da177e4SLinus Torvalds return 0; 66401da177e4SLinus Torvalds } 66411da177e4SLinus Torvalds 66421da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Initializing.\n"); 66431da177e4SLinus Torvalds 66441da177e4SLinus Torvalds /* Set the security state for the initial task. */ 6645d84f4f99SDavid Howells cred_init_security(); 66461da177e4SLinus Torvalds 6647fcaaade1SStephen Smalley default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC); 6648fcaaade1SStephen Smalley 66497cae7e26SJames Morris sel_inode_cache = kmem_cache_create("selinux_inode_security", 66507cae7e26SJames Morris sizeof(struct inode_security_struct), 665120c2df83SPaul Mundt 0, SLAB_PANIC, NULL); 665263205654SSangwoo file_security_cache = kmem_cache_create("selinux_file_security", 665363205654SSangwoo sizeof(struct file_security_struct), 665463205654SSangwoo 0, SLAB_PANIC, NULL); 66551da177e4SLinus Torvalds avc_init(); 66561da177e4SLinus Torvalds 6657d69dece5SCasey Schaufler security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux"); 66581da177e4SLinus Torvalds 6659615e51fdSPaul Moore if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET)) 6660615e51fdSPaul Moore panic("SELinux: Unable to register AVC netcache callback\n"); 6661615e51fdSPaul Moore 66628f408ab6SDaniel Jurgens if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET)) 66638f408ab6SDaniel Jurgens panic("SELinux: Unable to register AVC LSM notifier callback\n"); 66648f408ab6SDaniel Jurgens 6665828dfe1dSEric Paris if (selinux_enforcing) 6666fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Starting in enforcing mode\n"); 6667828dfe1dSEric Paris else 6668fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Starting in permissive mode\n"); 6669d720024eSMichael LeMay 66701da177e4SLinus Torvalds return 0; 66711da177e4SLinus Torvalds } 66721da177e4SLinus Torvalds 6673e8c26255SAl Viro static void delayed_superblock_init(struct super_block *sb, void *unused) 6674e8c26255SAl Viro { 6675e8c26255SAl Viro superblock_doinit(sb, NULL); 6676e8c26255SAl Viro } 6677e8c26255SAl Viro 66781da177e4SLinus Torvalds void selinux_complete_init(void) 66791da177e4SLinus Torvalds { 6680fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Completing initialization.\n"); 66811da177e4SLinus Torvalds 66821da177e4SLinus Torvalds /* Set up any superblocks initialized prior to the policy load. */ 6683fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Setting up existing superblocks.\n"); 6684e8c26255SAl Viro iterate_supers(delayed_superblock_init, NULL); 66851da177e4SLinus Torvalds } 66861da177e4SLinus Torvalds 66871da177e4SLinus Torvalds /* SELinux requires early initialization in order to label 66881da177e4SLinus Torvalds all processes and objects when they are created. */ 66891da177e4SLinus Torvalds security_initcall(selinux_init); 66901da177e4SLinus Torvalds 6691c2b507fdSStephen Smalley #if defined(CONFIG_NETFILTER) 66921da177e4SLinus Torvalds 6693591bb278SFlorian Westphal static const struct nf_hook_ops selinux_nf_ops[] = { 6694effad8dfSPaul Moore { 6695effad8dfSPaul Moore .hook = selinux_ipv4_postroute, 66962597a834SAlban Crequy .pf = NFPROTO_IPV4, 66976e23ae2aSPatrick McHardy .hooknum = NF_INET_POST_ROUTING, 66981da177e4SLinus Torvalds .priority = NF_IP_PRI_SELINUX_LAST, 6699effad8dfSPaul Moore }, 6700effad8dfSPaul Moore { 6701effad8dfSPaul Moore .hook = selinux_ipv4_forward, 67022597a834SAlban Crequy .pf = NFPROTO_IPV4, 6703effad8dfSPaul Moore .hooknum = NF_INET_FORWARD, 6704effad8dfSPaul Moore .priority = NF_IP_PRI_SELINUX_FIRST, 6705948bf85cSPaul Moore }, 6706948bf85cSPaul Moore { 6707948bf85cSPaul Moore .hook = selinux_ipv4_output, 67082597a834SAlban Crequy .pf = NFPROTO_IPV4, 6709948bf85cSPaul Moore .hooknum = NF_INET_LOCAL_OUT, 6710948bf85cSPaul Moore .priority = NF_IP_PRI_SELINUX_FIRST, 671125db6beaSJiri Pirko }, 67121a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 6713effad8dfSPaul Moore { 6714effad8dfSPaul Moore .hook = selinux_ipv6_postroute, 67152597a834SAlban Crequy .pf = NFPROTO_IPV6, 67166e23ae2aSPatrick McHardy .hooknum = NF_INET_POST_ROUTING, 67171da177e4SLinus Torvalds .priority = NF_IP6_PRI_SELINUX_LAST, 6718effad8dfSPaul Moore }, 6719effad8dfSPaul Moore { 6720effad8dfSPaul Moore .hook = selinux_ipv6_forward, 67212597a834SAlban Crequy .pf = NFPROTO_IPV6, 6722effad8dfSPaul Moore .hooknum = NF_INET_FORWARD, 6723effad8dfSPaul Moore .priority = NF_IP6_PRI_SELINUX_FIRST, 672425db6beaSJiri Pirko }, 67252917f57bSHuw Davies { 67262917f57bSHuw Davies .hook = selinux_ipv6_output, 67272917f57bSHuw Davies .pf = NFPROTO_IPV6, 67282917f57bSHuw Davies .hooknum = NF_INET_LOCAL_OUT, 67292917f57bSHuw Davies .priority = NF_IP6_PRI_SELINUX_FIRST, 67302917f57bSHuw Davies }, 67311da177e4SLinus Torvalds #endif /* IPV6 */ 673225db6beaSJiri Pirko }; 67331da177e4SLinus Torvalds 67348e71bf75SFlorian Westphal static int __net_init selinux_nf_register(struct net *net) 67358e71bf75SFlorian Westphal { 67368e71bf75SFlorian Westphal return nf_register_net_hooks(net, selinux_nf_ops, 67378e71bf75SFlorian Westphal ARRAY_SIZE(selinux_nf_ops)); 67388e71bf75SFlorian Westphal } 67398e71bf75SFlorian Westphal 67408e71bf75SFlorian Westphal static void __net_exit selinux_nf_unregister(struct net *net) 67418e71bf75SFlorian Westphal { 67428e71bf75SFlorian Westphal nf_unregister_net_hooks(net, selinux_nf_ops, 67438e71bf75SFlorian Westphal ARRAY_SIZE(selinux_nf_ops)); 67448e71bf75SFlorian Westphal } 67458e71bf75SFlorian Westphal 67468e71bf75SFlorian Westphal static struct pernet_operations selinux_net_ops = { 67478e71bf75SFlorian Westphal .init = selinux_nf_register, 67488e71bf75SFlorian Westphal .exit = selinux_nf_unregister, 67498e71bf75SFlorian Westphal }; 67508e71bf75SFlorian Westphal 67511da177e4SLinus Torvalds static int __init selinux_nf_ip_init(void) 67521da177e4SLinus Torvalds { 675325db6beaSJiri Pirko int err; 67541da177e4SLinus Torvalds 67551da177e4SLinus Torvalds if (!selinux_enabled) 675625db6beaSJiri Pirko return 0; 67571da177e4SLinus Torvalds 6758fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Registering netfilter hooks\n"); 67591da177e4SLinus Torvalds 67608e71bf75SFlorian Westphal err = register_pernet_subsys(&selinux_net_ops); 67611da177e4SLinus Torvalds if (err) 67628e71bf75SFlorian Westphal panic("SELinux: register_pernet_subsys: error %d\n", err); 67631da177e4SLinus Torvalds 676425db6beaSJiri Pirko return 0; 67651da177e4SLinus Torvalds } 67661da177e4SLinus Torvalds __initcall(selinux_nf_ip_init); 67671da177e4SLinus Torvalds 67681da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 67691da177e4SLinus Torvalds static void selinux_nf_ip_exit(void) 67701da177e4SLinus Torvalds { 6771fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Unregistering netfilter hooks\n"); 67721da177e4SLinus Torvalds 67738e71bf75SFlorian Westphal unregister_pernet_subsys(&selinux_net_ops); 67741da177e4SLinus Torvalds } 67751da177e4SLinus Torvalds #endif 67761da177e4SLinus Torvalds 6777c2b507fdSStephen Smalley #else /* CONFIG_NETFILTER */ 67781da177e4SLinus Torvalds 67791da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 67801da177e4SLinus Torvalds #define selinux_nf_ip_exit() 67811da177e4SLinus Torvalds #endif 67821da177e4SLinus Torvalds 6783c2b507fdSStephen Smalley #endif /* CONFIG_NETFILTER */ 67841da177e4SLinus Torvalds 67851da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 6786828dfe1dSEric Paris static int selinux_disabled; 6787828dfe1dSEric Paris 67881da177e4SLinus Torvalds int selinux_disable(void) 67891da177e4SLinus Torvalds { 67901da177e4SLinus Torvalds if (ss_initialized) { 67911da177e4SLinus Torvalds /* Not permitted after initial policy load. */ 67921da177e4SLinus Torvalds return -EINVAL; 67931da177e4SLinus Torvalds } 67941da177e4SLinus Torvalds 67951da177e4SLinus Torvalds if (selinux_disabled) { 67961da177e4SLinus Torvalds /* Only do this once. */ 67971da177e4SLinus Torvalds return -EINVAL; 67981da177e4SLinus Torvalds } 67991da177e4SLinus Torvalds 68001da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Disabled at runtime.\n"); 68011da177e4SLinus Torvalds 68021da177e4SLinus Torvalds selinux_disabled = 1; 680330d55280SStephen Smalley selinux_enabled = 0; 68041da177e4SLinus Torvalds 6805b1d9e6b0SCasey Schaufler security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks)); 68061da177e4SLinus Torvalds 6807af8ff049SEric Paris /* Try to destroy the avc node cache */ 6808af8ff049SEric Paris avc_disable(); 6809af8ff049SEric Paris 68101da177e4SLinus Torvalds /* Unregister netfilter hooks. */ 68111da177e4SLinus Torvalds selinux_nf_ip_exit(); 68121da177e4SLinus Torvalds 68131da177e4SLinus Torvalds /* Unregister selinuxfs. */ 68141da177e4SLinus Torvalds exit_sel_fs(); 68151da177e4SLinus Torvalds 68161da177e4SLinus Torvalds return 0; 68171da177e4SLinus Torvalds } 68181da177e4SLinus Torvalds #endif 6819