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> 881da177e4SLinus Torvalds 891da177e4SLinus Torvalds #include "avc.h" 901da177e4SLinus Torvalds #include "objsec.h" 911da177e4SLinus Torvalds #include "netif.h" 92224dfbd8SPaul Moore #include "netnode.h" 933e112172SPaul Moore #include "netport.h" 94409dcf31SDaniel Jurgens #include "ibpkey.h" 95d28d1e08STrent Jaeger #include "xfrm.h" 96c60475bfSPaul Moore #include "netlabel.h" 979d57a7f9SAhmed S. Darwish #include "audit.h" 987b98a585SJames Morris #include "avc_ss.h" 991da177e4SLinus Torvalds 100d621d35eSPaul Moore /* SECMARK reference count */ 10156a4ca99SJames Morris static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0); 102d621d35eSPaul Moore 1031da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP 104828dfe1dSEric Paris int selinux_enforcing; 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds static int __init enforcing_setup(char *str) 1071da177e4SLinus Torvalds { 108f5269710SEric Paris unsigned long enforcing; 10929707b20SJingoo Han if (!kstrtoul(str, 0, &enforcing)) 110f5269710SEric Paris selinux_enforcing = enforcing ? 1 : 0; 1111da177e4SLinus Torvalds return 1; 1121da177e4SLinus Torvalds } 1131da177e4SLinus Torvalds __setup("enforcing=", enforcing_setup); 1141da177e4SLinus Torvalds #endif 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM 1171da177e4SLinus Torvalds int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE; 1181da177e4SLinus Torvalds 1191da177e4SLinus Torvalds static int __init selinux_enabled_setup(char *str) 1201da177e4SLinus Torvalds { 121f5269710SEric Paris unsigned long enabled; 12229707b20SJingoo Han if (!kstrtoul(str, 0, &enabled)) 123f5269710SEric Paris selinux_enabled = enabled ? 1 : 0; 1241da177e4SLinus Torvalds return 1; 1251da177e4SLinus Torvalds } 1261da177e4SLinus Torvalds __setup("selinux=", selinux_enabled_setup); 12730d55280SStephen Smalley #else 12830d55280SStephen Smalley int selinux_enabled = 1; 1291da177e4SLinus Torvalds #endif 1301da177e4SLinus Torvalds 131e18b890bSChristoph Lameter static struct kmem_cache *sel_inode_cache; 13263205654SSangwoo static struct kmem_cache *file_security_cache; 1337cae7e26SJames Morris 134d621d35eSPaul Moore /** 135d621d35eSPaul Moore * selinux_secmark_enabled - Check to see if SECMARK is currently enabled 136d621d35eSPaul Moore * 137d621d35eSPaul Moore * Description: 138d621d35eSPaul Moore * This function checks the SECMARK reference counter to see if any SECMARK 139d621d35eSPaul Moore * targets are currently configured, if the reference counter is greater than 140d621d35eSPaul Moore * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is 1412be4d74fSChris PeBenito * enabled, false (0) if SECMARK is disabled. If the always_check_network 1422be4d74fSChris PeBenito * policy capability is enabled, SECMARK is always considered enabled. 143d621d35eSPaul Moore * 144d621d35eSPaul Moore */ 145d621d35eSPaul Moore static int selinux_secmark_enabled(void) 146d621d35eSPaul Moore { 1472be4d74fSChris PeBenito return (selinux_policycap_alwaysnetwork || atomic_read(&selinux_secmark_refcount)); 1482be4d74fSChris PeBenito } 1492be4d74fSChris PeBenito 1502be4d74fSChris PeBenito /** 1512be4d74fSChris PeBenito * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled 1522be4d74fSChris PeBenito * 1532be4d74fSChris PeBenito * Description: 1542be4d74fSChris PeBenito * This function checks if NetLabel or labeled IPSEC is enabled. Returns true 1552be4d74fSChris PeBenito * (1) if any are enabled or false (0) if neither are enabled. If the 1562be4d74fSChris PeBenito * always_check_network policy capability is enabled, peer labeling 1572be4d74fSChris PeBenito * is always considered enabled. 1582be4d74fSChris PeBenito * 1592be4d74fSChris PeBenito */ 1602be4d74fSChris PeBenito static int selinux_peerlbl_enabled(void) 1612be4d74fSChris PeBenito { 1622be4d74fSChris PeBenito return (selinux_policycap_alwaysnetwork || netlbl_enabled() || selinux_xfrm_enabled()); 163d621d35eSPaul Moore } 164d621d35eSPaul Moore 165615e51fdSPaul Moore static int selinux_netcache_avc_callback(u32 event) 166615e51fdSPaul Moore { 167615e51fdSPaul Moore if (event == AVC_CALLBACK_RESET) { 168615e51fdSPaul Moore sel_netif_flush(); 169615e51fdSPaul Moore sel_netnode_flush(); 170615e51fdSPaul Moore sel_netport_flush(); 171615e51fdSPaul Moore synchronize_net(); 172615e51fdSPaul Moore } 173615e51fdSPaul Moore return 0; 174615e51fdSPaul Moore } 175615e51fdSPaul Moore 1768f408ab6SDaniel Jurgens static int selinux_lsm_notifier_avc_callback(u32 event) 1778f408ab6SDaniel Jurgens { 178409dcf31SDaniel Jurgens if (event == AVC_CALLBACK_RESET) { 179409dcf31SDaniel Jurgens sel_ib_pkey_flush(); 1808f408ab6SDaniel Jurgens call_lsm_notifier(LSM_POLICY_CHANGE, NULL); 181409dcf31SDaniel Jurgens } 1828f408ab6SDaniel Jurgens 1838f408ab6SDaniel Jurgens return 0; 1848f408ab6SDaniel Jurgens } 1858f408ab6SDaniel Jurgens 186d84f4f99SDavid Howells /* 187d84f4f99SDavid Howells * initialise the security for the init task 188d84f4f99SDavid Howells */ 189d84f4f99SDavid Howells static void cred_init_security(void) 1901da177e4SLinus Torvalds { 1913b11a1deSDavid Howells struct cred *cred = (struct cred *) current->real_cred; 1921da177e4SLinus Torvalds struct task_security_struct *tsec; 1931da177e4SLinus Torvalds 19489d155efSJames Morris tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL); 1951da177e4SLinus Torvalds if (!tsec) 196d84f4f99SDavid Howells panic("SELinux: Failed to initialize initial task.\n"); 1971da177e4SLinus Torvalds 198d84f4f99SDavid Howells tsec->osid = tsec->sid = SECINITSID_KERNEL; 199f1752eecSDavid Howells cred->security = tsec; 2001da177e4SLinus Torvalds } 2011da177e4SLinus Torvalds 202275bb41eSDavid Howells /* 20388e67f3bSDavid Howells * get the security ID of a set of credentials 20488e67f3bSDavid Howells */ 20588e67f3bSDavid Howells static inline u32 cred_sid(const struct cred *cred) 20688e67f3bSDavid Howells { 20788e67f3bSDavid Howells const struct task_security_struct *tsec; 20888e67f3bSDavid Howells 20988e67f3bSDavid Howells tsec = cred->security; 21088e67f3bSDavid Howells return tsec->sid; 21188e67f3bSDavid Howells } 21288e67f3bSDavid Howells 21388e67f3bSDavid Howells /* 2143b11a1deSDavid Howells * get the objective security ID of a task 215275bb41eSDavid Howells */ 216275bb41eSDavid Howells static inline u32 task_sid(const struct task_struct *task) 217275bb41eSDavid Howells { 218275bb41eSDavid Howells u32 sid; 219275bb41eSDavid Howells 220275bb41eSDavid Howells rcu_read_lock(); 22188e67f3bSDavid Howells sid = cred_sid(__task_cred(task)); 222275bb41eSDavid Howells rcu_read_unlock(); 223275bb41eSDavid Howells return sid; 224275bb41eSDavid Howells } 225275bb41eSDavid Howells 22688e67f3bSDavid Howells /* Allocate and free functions for each kind of security blob. */ 22788e67f3bSDavid Howells 2281da177e4SLinus Torvalds static int inode_alloc_security(struct inode *inode) 2291da177e4SLinus Torvalds { 2301da177e4SLinus Torvalds struct inode_security_struct *isec; 231275bb41eSDavid Howells u32 sid = current_sid(); 2321da177e4SLinus Torvalds 233a02fe132SJosef Bacik isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS); 2341da177e4SLinus Torvalds if (!isec) 2351da177e4SLinus Torvalds return -ENOMEM; 2361da177e4SLinus Torvalds 2379287aed2SAndreas Gruenbacher spin_lock_init(&isec->lock); 2381da177e4SLinus Torvalds INIT_LIST_HEAD(&isec->list); 2391da177e4SLinus Torvalds isec->inode = inode; 2401da177e4SLinus Torvalds isec->sid = SECINITSID_UNLABELED; 2411da177e4SLinus Torvalds isec->sclass = SECCLASS_FILE; 242275bb41eSDavid Howells isec->task_sid = sid; 24342059112SAndreas Gruenbacher isec->initialized = LABEL_INVALID; 2441da177e4SLinus Torvalds inode->i_security = isec; 2451da177e4SLinus Torvalds 2461da177e4SLinus Torvalds return 0; 2471da177e4SLinus Torvalds } 2481da177e4SLinus Torvalds 2495d226df4SAndreas Gruenbacher static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry); 2505d226df4SAndreas Gruenbacher 2515d226df4SAndreas Gruenbacher /* 2525d226df4SAndreas Gruenbacher * Try reloading inode security labels that have been marked as invalid. The 2535d226df4SAndreas Gruenbacher * @may_sleep parameter indicates when sleeping and thus reloading labels is 25442059112SAndreas Gruenbacher * allowed; when set to false, returns -ECHILD when the label is 2555d226df4SAndreas Gruenbacher * invalid. The @opt_dentry parameter should be set to a dentry of the inode; 2565d226df4SAndreas Gruenbacher * when no dentry is available, set it to NULL instead. 2575d226df4SAndreas Gruenbacher */ 2585d226df4SAndreas Gruenbacher static int __inode_security_revalidate(struct inode *inode, 2595d226df4SAndreas Gruenbacher struct dentry *opt_dentry, 2605d226df4SAndreas Gruenbacher bool may_sleep) 2615d226df4SAndreas Gruenbacher { 2625d226df4SAndreas Gruenbacher struct inode_security_struct *isec = inode->i_security; 2635d226df4SAndreas Gruenbacher 2645d226df4SAndreas Gruenbacher might_sleep_if(may_sleep); 2655d226df4SAndreas Gruenbacher 2661ac42476SPaul Moore if (ss_initialized && isec->initialized != LABEL_INITIALIZED) { 2675d226df4SAndreas Gruenbacher if (!may_sleep) 2685d226df4SAndreas Gruenbacher return -ECHILD; 2695d226df4SAndreas Gruenbacher 2705d226df4SAndreas Gruenbacher /* 2715d226df4SAndreas Gruenbacher * Try reloading the inode security label. This will fail if 2725d226df4SAndreas Gruenbacher * @opt_dentry is NULL and no dentry for this inode can be 2735d226df4SAndreas Gruenbacher * found; in that case, continue using the old label. 2745d226df4SAndreas Gruenbacher */ 2755d226df4SAndreas Gruenbacher inode_doinit_with_dentry(inode, opt_dentry); 2765d226df4SAndreas Gruenbacher } 2775d226df4SAndreas Gruenbacher return 0; 2785d226df4SAndreas Gruenbacher } 2795d226df4SAndreas Gruenbacher 2805d226df4SAndreas Gruenbacher static struct inode_security_struct *inode_security_novalidate(struct inode *inode) 2815d226df4SAndreas Gruenbacher { 2825d226df4SAndreas Gruenbacher return inode->i_security; 2835d226df4SAndreas Gruenbacher } 2845d226df4SAndreas Gruenbacher 2855d226df4SAndreas Gruenbacher static struct inode_security_struct *inode_security_rcu(struct inode *inode, bool rcu) 2865d226df4SAndreas Gruenbacher { 2875d226df4SAndreas Gruenbacher int error; 2885d226df4SAndreas Gruenbacher 2895d226df4SAndreas Gruenbacher error = __inode_security_revalidate(inode, NULL, !rcu); 2905d226df4SAndreas Gruenbacher if (error) 2915d226df4SAndreas Gruenbacher return ERR_PTR(error); 2925d226df4SAndreas Gruenbacher return inode->i_security; 2935d226df4SAndreas Gruenbacher } 2945d226df4SAndreas Gruenbacher 29583da53c5SAndreas Gruenbacher /* 29683da53c5SAndreas Gruenbacher * Get the security label of an inode. 29783da53c5SAndreas Gruenbacher */ 29883da53c5SAndreas Gruenbacher static struct inode_security_struct *inode_security(struct inode *inode) 29983da53c5SAndreas Gruenbacher { 3005d226df4SAndreas Gruenbacher __inode_security_revalidate(inode, NULL, true); 30183da53c5SAndreas Gruenbacher return inode->i_security; 30283da53c5SAndreas Gruenbacher } 30383da53c5SAndreas Gruenbacher 3042c97165bSPaul Moore static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry) 3052c97165bSPaul Moore { 3062c97165bSPaul Moore struct inode *inode = d_backing_inode(dentry); 3072c97165bSPaul Moore 3082c97165bSPaul Moore return inode->i_security; 3092c97165bSPaul Moore } 3102c97165bSPaul Moore 31183da53c5SAndreas Gruenbacher /* 31283da53c5SAndreas Gruenbacher * Get the security label of a dentry's backing inode. 31383da53c5SAndreas Gruenbacher */ 31483da53c5SAndreas Gruenbacher static struct inode_security_struct *backing_inode_security(struct dentry *dentry) 31583da53c5SAndreas Gruenbacher { 31683da53c5SAndreas Gruenbacher struct inode *inode = d_backing_inode(dentry); 31783da53c5SAndreas Gruenbacher 3185d226df4SAndreas Gruenbacher __inode_security_revalidate(inode, dentry, true); 31983da53c5SAndreas Gruenbacher return inode->i_security; 32083da53c5SAndreas Gruenbacher } 32183da53c5SAndreas Gruenbacher 3223dc91d43SSteven Rostedt static void inode_free_rcu(struct rcu_head *head) 3233dc91d43SSteven Rostedt { 3243dc91d43SSteven Rostedt struct inode_security_struct *isec; 3253dc91d43SSteven Rostedt 3263dc91d43SSteven Rostedt isec = container_of(head, struct inode_security_struct, rcu); 3273dc91d43SSteven Rostedt kmem_cache_free(sel_inode_cache, isec); 3283dc91d43SSteven Rostedt } 3293dc91d43SSteven Rostedt 3301da177e4SLinus Torvalds static void inode_free_security(struct inode *inode) 3311da177e4SLinus Torvalds { 3321da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 3331da177e4SLinus Torvalds struct superblock_security_struct *sbsec = inode->i_sb->s_security; 3341da177e4SLinus Torvalds 3359629d04aSWaiman Long /* 3369629d04aSWaiman Long * As not all inode security structures are in a list, we check for 3379629d04aSWaiman Long * empty list outside of the lock to make sure that we won't waste 3389629d04aSWaiman Long * time taking a lock doing nothing. 3399629d04aSWaiman Long * 3409629d04aSWaiman Long * The list_del_init() function can be safely called more than once. 3419629d04aSWaiman Long * It should not be possible for this function to be called with 3429629d04aSWaiman Long * concurrent list_add(), but for better safety against future changes 3439629d04aSWaiman Long * in the code, we use list_empty_careful() here. 3449629d04aSWaiman Long */ 3459629d04aSWaiman Long if (!list_empty_careful(&isec->list)) { 3461da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 3471da177e4SLinus Torvalds list_del_init(&isec->list); 3481da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 3499629d04aSWaiman Long } 3501da177e4SLinus Torvalds 3513dc91d43SSteven Rostedt /* 3523dc91d43SSteven Rostedt * The inode may still be referenced in a path walk and 3533dc91d43SSteven Rostedt * a call to selinux_inode_permission() can be made 3543dc91d43SSteven Rostedt * after inode_free_security() is called. Ideally, the VFS 3553dc91d43SSteven Rostedt * wouldn't do this, but fixing that is a much harder 3563dc91d43SSteven Rostedt * job. For now, simply free the i_security via RCU, and 3573dc91d43SSteven Rostedt * leave the current inode->i_security pointer intact. 3583dc91d43SSteven Rostedt * The inode will be freed after the RCU grace period too. 3593dc91d43SSteven Rostedt */ 3603dc91d43SSteven Rostedt call_rcu(&isec->rcu, inode_free_rcu); 3611da177e4SLinus Torvalds } 3621da177e4SLinus Torvalds 3631da177e4SLinus Torvalds static int file_alloc_security(struct file *file) 3641da177e4SLinus Torvalds { 3651da177e4SLinus Torvalds struct file_security_struct *fsec; 366275bb41eSDavid Howells u32 sid = current_sid(); 3671da177e4SLinus Torvalds 36863205654SSangwoo fsec = kmem_cache_zalloc(file_security_cache, GFP_KERNEL); 3691da177e4SLinus Torvalds if (!fsec) 3701da177e4SLinus Torvalds return -ENOMEM; 3711da177e4SLinus Torvalds 372275bb41eSDavid Howells fsec->sid = sid; 373275bb41eSDavid Howells fsec->fown_sid = sid; 3741da177e4SLinus Torvalds file->f_security = fsec; 3751da177e4SLinus Torvalds 3761da177e4SLinus Torvalds return 0; 3771da177e4SLinus Torvalds } 3781da177e4SLinus Torvalds 3791da177e4SLinus Torvalds static void file_free_security(struct file *file) 3801da177e4SLinus Torvalds { 3811da177e4SLinus Torvalds struct file_security_struct *fsec = file->f_security; 3821da177e4SLinus Torvalds file->f_security = NULL; 38363205654SSangwoo kmem_cache_free(file_security_cache, fsec); 3841da177e4SLinus Torvalds } 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds static int superblock_alloc_security(struct super_block *sb) 3871da177e4SLinus Torvalds { 3881da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 3891da177e4SLinus Torvalds 39089d155efSJames Morris sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL); 3911da177e4SLinus Torvalds if (!sbsec) 3921da177e4SLinus Torvalds return -ENOMEM; 3931da177e4SLinus Torvalds 394bc7e982bSEric Paris mutex_init(&sbsec->lock); 3951da177e4SLinus Torvalds INIT_LIST_HEAD(&sbsec->isec_head); 3961da177e4SLinus Torvalds spin_lock_init(&sbsec->isec_lock); 3971da177e4SLinus Torvalds sbsec->sb = sb; 3981da177e4SLinus Torvalds sbsec->sid = SECINITSID_UNLABELED; 3991da177e4SLinus Torvalds sbsec->def_sid = SECINITSID_FILE; 400c312feb2SEric Paris sbsec->mntpoint_sid = SECINITSID_UNLABELED; 4011da177e4SLinus Torvalds sb->s_security = sbsec; 4021da177e4SLinus Torvalds 4031da177e4SLinus Torvalds return 0; 4041da177e4SLinus Torvalds } 4051da177e4SLinus Torvalds 4061da177e4SLinus Torvalds static void superblock_free_security(struct super_block *sb) 4071da177e4SLinus Torvalds { 4081da177e4SLinus Torvalds struct superblock_security_struct *sbsec = sb->s_security; 4091da177e4SLinus Torvalds sb->s_security = NULL; 4101da177e4SLinus Torvalds kfree(sbsec); 4111da177e4SLinus Torvalds } 4121da177e4SLinus Torvalds 4131da177e4SLinus Torvalds static inline int inode_doinit(struct inode *inode) 4141da177e4SLinus Torvalds { 4151da177e4SLinus Torvalds return inode_doinit_with_dentry(inode, NULL); 4161da177e4SLinus Torvalds } 4171da177e4SLinus Torvalds 4181da177e4SLinus Torvalds enum { 41931e87930SEric Paris Opt_error = -1, 4201da177e4SLinus Torvalds Opt_context = 1, 4211da177e4SLinus Torvalds Opt_fscontext = 2, 422c9180a57SEric Paris Opt_defcontext = 3, 423c9180a57SEric Paris Opt_rootcontext = 4, 42411689d47SDavid P. Quigley Opt_labelsupport = 5, 425d355987fSEric Paris Opt_nextmntopt = 6, 4261da177e4SLinus Torvalds }; 4271da177e4SLinus Torvalds 428d355987fSEric Paris #define NUM_SEL_MNT_OPTS (Opt_nextmntopt - 1) 429d355987fSEric Paris 430a447c093SSteven Whitehouse static const match_table_t tokens = { 431832cbd9aSEric Paris {Opt_context, CONTEXT_STR "%s"}, 432832cbd9aSEric Paris {Opt_fscontext, FSCONTEXT_STR "%s"}, 433832cbd9aSEric Paris {Opt_defcontext, DEFCONTEXT_STR "%s"}, 434832cbd9aSEric Paris {Opt_rootcontext, ROOTCONTEXT_STR "%s"}, 43511689d47SDavid P. Quigley {Opt_labelsupport, LABELSUPP_STR}, 43631e87930SEric Paris {Opt_error, NULL}, 4371da177e4SLinus Torvalds }; 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n" 4401da177e4SLinus Torvalds 441c312feb2SEric Paris static int may_context_mount_sb_relabel(u32 sid, 442c312feb2SEric Paris struct superblock_security_struct *sbsec, 443275bb41eSDavid Howells const struct cred *cred) 444c312feb2SEric Paris { 445275bb41eSDavid Howells const struct task_security_struct *tsec = cred->security; 446c312feb2SEric Paris int rc; 447c312feb2SEric Paris 448c312feb2SEric Paris rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 449c312feb2SEric Paris FILESYSTEM__RELABELFROM, NULL); 450c312feb2SEric Paris if (rc) 451c312feb2SEric Paris return rc; 452c312feb2SEric Paris 453c312feb2SEric Paris rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM, 454c312feb2SEric Paris FILESYSTEM__RELABELTO, NULL); 455c312feb2SEric Paris return rc; 456c312feb2SEric Paris } 457c312feb2SEric Paris 4580808925eSEric Paris static int may_context_mount_inode_relabel(u32 sid, 4590808925eSEric Paris struct superblock_security_struct *sbsec, 460275bb41eSDavid Howells const struct cred *cred) 4610808925eSEric Paris { 462275bb41eSDavid Howells const struct task_security_struct *tsec = cred->security; 4630808925eSEric Paris int rc; 4640808925eSEric Paris rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 4650808925eSEric Paris FILESYSTEM__RELABELFROM, NULL); 4660808925eSEric Paris if (rc) 4670808925eSEric Paris return rc; 4680808925eSEric Paris 4690808925eSEric Paris rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, 4700808925eSEric Paris FILESYSTEM__ASSOCIATE, NULL); 4710808925eSEric Paris return rc; 4720808925eSEric Paris } 4730808925eSEric Paris 474b43e725dSEric Paris static int selinux_is_sblabel_mnt(struct super_block *sb) 475b43e725dSEric Paris { 476b43e725dSEric Paris struct superblock_security_struct *sbsec = sb->s_security; 477b43e725dSEric Paris 478d5f3a5f6SMark Salyzyn return sbsec->behavior == SECURITY_FS_USE_XATTR || 479b43e725dSEric Paris sbsec->behavior == SECURITY_FS_USE_TRANS || 480d5f3a5f6SMark Salyzyn sbsec->behavior == SECURITY_FS_USE_TASK || 4819fc2b4b4SJ. Bruce Fields sbsec->behavior == SECURITY_FS_USE_NATIVE || 482d5f3a5f6SMark Salyzyn /* Special handling. Genfs but also in-core setxattr handler */ 483d5f3a5f6SMark Salyzyn !strcmp(sb->s_type->name, "sysfs") || 484d5f3a5f6SMark Salyzyn !strcmp(sb->s_type->name, "pstore") || 485d5f3a5f6SMark Salyzyn !strcmp(sb->s_type->name, "debugfs") || 486a2c7c6fbSYongqin Liu !strcmp(sb->s_type->name, "tracefs") || 4872651225bSStephen Smalley !strcmp(sb->s_type->name, "rootfs") || 4882651225bSStephen Smalley (selinux_policycap_cgroupseclabel && 4892651225bSStephen Smalley (!strcmp(sb->s_type->name, "cgroup") || 4902651225bSStephen Smalley !strcmp(sb->s_type->name, "cgroup2"))); 491b43e725dSEric Paris } 492b43e725dSEric Paris 493c9180a57SEric Paris static int sb_finish_set_opts(struct super_block *sb) 4941da177e4SLinus Torvalds { 4951da177e4SLinus Torvalds struct superblock_security_struct *sbsec = sb->s_security; 4961da177e4SLinus Torvalds struct dentry *root = sb->s_root; 497c6f493d6SDavid Howells struct inode *root_inode = d_backing_inode(root); 4981da177e4SLinus Torvalds int rc = 0; 4991da177e4SLinus Torvalds 5001da177e4SLinus Torvalds if (sbsec->behavior == SECURITY_FS_USE_XATTR) { 5011da177e4SLinus Torvalds /* Make sure that the xattr handler exists and that no 5021da177e4SLinus Torvalds error other than -ENODATA is returned by getxattr on 5031da177e4SLinus Torvalds the root directory. -ENODATA is ok, as this may be 5041da177e4SLinus Torvalds the first boot of the SELinux kernel before we have 5051da177e4SLinus Torvalds assigned xattr values to the filesystem. */ 5065d6c3191SAndreas Gruenbacher if (!(root_inode->i_opflags & IOP_XATTR)) { 50729b1deb2SLinus Torvalds printk(KERN_WARNING "SELinux: (dev %s, type %s) has no " 50829b1deb2SLinus Torvalds "xattr support\n", sb->s_id, sb->s_type->name); 5091da177e4SLinus Torvalds rc = -EOPNOTSUPP; 5101da177e4SLinus Torvalds goto out; 5111da177e4SLinus Torvalds } 5125d6c3191SAndreas Gruenbacher 5135d6c3191SAndreas Gruenbacher rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0); 5141da177e4SLinus Torvalds if (rc < 0 && rc != -ENODATA) { 5151da177e4SLinus Torvalds if (rc == -EOPNOTSUPP) 5161da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: (dev %s, type " 51729b1deb2SLinus Torvalds "%s) has no security xattr handler\n", 51829b1deb2SLinus Torvalds sb->s_id, sb->s_type->name); 5191da177e4SLinus Torvalds else 5201da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: (dev %s, type " 52129b1deb2SLinus Torvalds "%s) getxattr errno %d\n", sb->s_id, 52229b1deb2SLinus Torvalds sb->s_type->name, -rc); 5231da177e4SLinus Torvalds goto out; 5241da177e4SLinus Torvalds } 5251da177e4SLinus Torvalds } 5261da177e4SLinus Torvalds 527eadcabc6SEric Paris sbsec->flags |= SE_SBINITIALIZED; 5280b4d3452SScott Mayhew 5290b4d3452SScott Mayhew /* 5300b4d3452SScott Mayhew * Explicitly set or clear SBLABEL_MNT. It's not sufficient to simply 5310b4d3452SScott Mayhew * leave the flag untouched because sb_clone_mnt_opts might be handing 5320b4d3452SScott Mayhew * us a superblock that needs the flag to be cleared. 5330b4d3452SScott Mayhew */ 534b43e725dSEric Paris if (selinux_is_sblabel_mnt(sb)) 53512f348b9SEric Paris sbsec->flags |= SBLABEL_MNT; 5360b4d3452SScott Mayhew else 5370b4d3452SScott Mayhew sbsec->flags &= ~SBLABEL_MNT; 538ddd29ec6SDavid P. Quigley 5391da177e4SLinus Torvalds /* Initialize the root inode. */ 540c9180a57SEric Paris rc = inode_doinit_with_dentry(root_inode, root); 5411da177e4SLinus Torvalds 5421da177e4SLinus Torvalds /* Initialize any other inodes associated with the superblock, e.g. 5431da177e4SLinus Torvalds inodes created prior to initial policy load or inodes created 5441da177e4SLinus Torvalds during get_sb by a pseudo filesystem that directly 5451da177e4SLinus Torvalds populates itself. */ 5461da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 5471da177e4SLinus Torvalds next_inode: 5481da177e4SLinus Torvalds if (!list_empty(&sbsec->isec_head)) { 5491da177e4SLinus Torvalds struct inode_security_struct *isec = 5501da177e4SLinus Torvalds list_entry(sbsec->isec_head.next, 5511da177e4SLinus Torvalds struct inode_security_struct, list); 5521da177e4SLinus Torvalds struct inode *inode = isec->inode; 553923190d3SStephen Smalley list_del_init(&isec->list); 5541da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 5551da177e4SLinus Torvalds inode = igrab(inode); 5561da177e4SLinus Torvalds if (inode) { 5571da177e4SLinus Torvalds if (!IS_PRIVATE(inode)) 5581da177e4SLinus Torvalds inode_doinit(inode); 5591da177e4SLinus Torvalds iput(inode); 5601da177e4SLinus Torvalds } 5611da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 5621da177e4SLinus Torvalds goto next_inode; 5631da177e4SLinus Torvalds } 5641da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 5651da177e4SLinus Torvalds out: 566c9180a57SEric Paris return rc; 567c9180a57SEric Paris } 568c9180a57SEric Paris 569c9180a57SEric Paris /* 570c9180a57SEric Paris * This function should allow an FS to ask what it's mount security 571c9180a57SEric Paris * options were so it can use those later for submounts, displaying 572c9180a57SEric Paris * mount options, or whatever. 573c9180a57SEric Paris */ 574c9180a57SEric Paris static int selinux_get_mnt_opts(const struct super_block *sb, 575e0007529SEric Paris struct security_mnt_opts *opts) 576c9180a57SEric Paris { 577c9180a57SEric Paris int rc = 0, i; 578c9180a57SEric Paris struct superblock_security_struct *sbsec = sb->s_security; 579c9180a57SEric Paris char *context = NULL; 580c9180a57SEric Paris u32 len; 581c9180a57SEric Paris char tmp; 582c9180a57SEric Paris 583e0007529SEric Paris security_init_mnt_opts(opts); 584c9180a57SEric Paris 5850d90a7ecSDavid P. Quigley if (!(sbsec->flags & SE_SBINITIALIZED)) 586c9180a57SEric Paris return -EINVAL; 587c9180a57SEric Paris 588c9180a57SEric Paris if (!ss_initialized) 589c9180a57SEric Paris return -EINVAL; 590c9180a57SEric Paris 591af8e50ccSEric Paris /* make sure we always check enough bits to cover the mask */ 592af8e50ccSEric Paris BUILD_BUG_ON(SE_MNTMASK >= (1 << NUM_SEL_MNT_OPTS)); 593af8e50ccSEric Paris 5940d90a7ecSDavid P. Quigley tmp = sbsec->flags & SE_MNTMASK; 595c9180a57SEric Paris /* count the number of mount options for this sb */ 596af8e50ccSEric Paris for (i = 0; i < NUM_SEL_MNT_OPTS; i++) { 597c9180a57SEric Paris if (tmp & 0x01) 598e0007529SEric Paris opts->num_mnt_opts++; 599c9180a57SEric Paris tmp >>= 1; 600c9180a57SEric Paris } 60111689d47SDavid P. Quigley /* Check if the Label support flag is set */ 6020b4bdb35SEric Paris if (sbsec->flags & SBLABEL_MNT) 60311689d47SDavid P. Quigley opts->num_mnt_opts++; 604c9180a57SEric Paris 605e0007529SEric Paris opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC); 606e0007529SEric Paris if (!opts->mnt_opts) { 607c9180a57SEric Paris rc = -ENOMEM; 608c9180a57SEric Paris goto out_free; 609c9180a57SEric Paris } 610c9180a57SEric Paris 611e0007529SEric Paris opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC); 612e0007529SEric Paris if (!opts->mnt_opts_flags) { 613c9180a57SEric Paris rc = -ENOMEM; 614c9180a57SEric Paris goto out_free; 615c9180a57SEric Paris } 616c9180a57SEric Paris 617c9180a57SEric Paris i = 0; 618c9180a57SEric Paris if (sbsec->flags & FSCONTEXT_MNT) { 619c9180a57SEric Paris rc = security_sid_to_context(sbsec->sid, &context, &len); 620c9180a57SEric Paris if (rc) 621c9180a57SEric Paris goto out_free; 622e0007529SEric Paris opts->mnt_opts[i] = context; 623e0007529SEric Paris opts->mnt_opts_flags[i++] = FSCONTEXT_MNT; 624c9180a57SEric Paris } 625c9180a57SEric Paris if (sbsec->flags & CONTEXT_MNT) { 626c9180a57SEric Paris rc = security_sid_to_context(sbsec->mntpoint_sid, &context, &len); 627c9180a57SEric Paris if (rc) 628c9180a57SEric Paris goto out_free; 629e0007529SEric Paris opts->mnt_opts[i] = context; 630e0007529SEric Paris opts->mnt_opts_flags[i++] = CONTEXT_MNT; 631c9180a57SEric Paris } 632c9180a57SEric Paris if (sbsec->flags & DEFCONTEXT_MNT) { 633c9180a57SEric Paris rc = security_sid_to_context(sbsec->def_sid, &context, &len); 634c9180a57SEric Paris if (rc) 635c9180a57SEric Paris goto out_free; 636e0007529SEric Paris opts->mnt_opts[i] = context; 637e0007529SEric Paris opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT; 638c9180a57SEric Paris } 639c9180a57SEric Paris if (sbsec->flags & ROOTCONTEXT_MNT) { 64083da53c5SAndreas Gruenbacher struct dentry *root = sbsec->sb->s_root; 64183da53c5SAndreas Gruenbacher struct inode_security_struct *isec = backing_inode_security(root); 642c9180a57SEric Paris 643c9180a57SEric Paris rc = security_sid_to_context(isec->sid, &context, &len); 644c9180a57SEric Paris if (rc) 645c9180a57SEric Paris goto out_free; 646e0007529SEric Paris opts->mnt_opts[i] = context; 647e0007529SEric Paris opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT; 648c9180a57SEric Paris } 64912f348b9SEric Paris if (sbsec->flags & SBLABEL_MNT) { 65011689d47SDavid P. Quigley opts->mnt_opts[i] = NULL; 65112f348b9SEric Paris opts->mnt_opts_flags[i++] = SBLABEL_MNT; 65211689d47SDavid P. Quigley } 653c9180a57SEric Paris 654e0007529SEric Paris BUG_ON(i != opts->num_mnt_opts); 655c9180a57SEric Paris 656c9180a57SEric Paris return 0; 657c9180a57SEric Paris 658c9180a57SEric Paris out_free: 659e0007529SEric Paris security_free_mnt_opts(opts); 660c9180a57SEric Paris return rc; 661c9180a57SEric Paris } 662c9180a57SEric Paris 663c9180a57SEric Paris static int bad_option(struct superblock_security_struct *sbsec, char flag, 664c9180a57SEric Paris u32 old_sid, u32 new_sid) 665c9180a57SEric Paris { 6660d90a7ecSDavid P. Quigley char mnt_flags = sbsec->flags & SE_MNTMASK; 6670d90a7ecSDavid P. Quigley 668c9180a57SEric Paris /* check if the old mount command had the same options */ 6690d90a7ecSDavid P. Quigley if (sbsec->flags & SE_SBINITIALIZED) 670c9180a57SEric Paris if (!(sbsec->flags & flag) || 671c9180a57SEric Paris (old_sid != new_sid)) 672c9180a57SEric Paris return 1; 673c9180a57SEric Paris 674c9180a57SEric Paris /* check if we were passed the same options twice, 675c9180a57SEric Paris * aka someone passed context=a,context=b 676c9180a57SEric Paris */ 6770d90a7ecSDavid P. Quigley if (!(sbsec->flags & SE_SBINITIALIZED)) 6780d90a7ecSDavid P. Quigley if (mnt_flags & flag) 679c9180a57SEric Paris return 1; 680c9180a57SEric Paris return 0; 681c9180a57SEric Paris } 682e0007529SEric Paris 683c9180a57SEric Paris /* 684c9180a57SEric Paris * Allow filesystems with binary mount data to explicitly set mount point 685c9180a57SEric Paris * labeling information. 686c9180a57SEric Paris */ 687e0007529SEric Paris static int selinux_set_mnt_opts(struct super_block *sb, 688649f6e77SDavid Quigley struct security_mnt_opts *opts, 689649f6e77SDavid Quigley unsigned long kern_flags, 690649f6e77SDavid Quigley unsigned long *set_kern_flags) 691c9180a57SEric Paris { 692275bb41eSDavid Howells const struct cred *cred = current_cred(); 693c9180a57SEric Paris int rc = 0, i; 694c9180a57SEric Paris struct superblock_security_struct *sbsec = sb->s_security; 69529b1deb2SLinus Torvalds const char *name = sb->s_type->name; 69683da53c5SAndreas Gruenbacher struct dentry *root = sbsec->sb->s_root; 6972c97165bSPaul Moore struct inode_security_struct *root_isec; 698c9180a57SEric Paris u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0; 699c9180a57SEric Paris u32 defcontext_sid = 0; 700e0007529SEric Paris char **mount_options = opts->mnt_opts; 701e0007529SEric Paris int *flags = opts->mnt_opts_flags; 702e0007529SEric Paris int num_opts = opts->num_mnt_opts; 703c9180a57SEric Paris 704c9180a57SEric Paris mutex_lock(&sbsec->lock); 705c9180a57SEric Paris 706c9180a57SEric Paris if (!ss_initialized) { 707c9180a57SEric Paris if (!num_opts) { 708c9180a57SEric Paris /* Defer initialization until selinux_complete_init, 709c9180a57SEric Paris after the initial policy is loaded and the security 710c9180a57SEric Paris server is ready to handle calls. */ 711c9180a57SEric Paris goto out; 712c9180a57SEric Paris } 713c9180a57SEric Paris rc = -EINVAL; 714744ba35eSEric Paris printk(KERN_WARNING "SELinux: Unable to set superblock options " 715744ba35eSEric Paris "before the security server is initialized\n"); 716c9180a57SEric Paris goto out; 717c9180a57SEric Paris } 718649f6e77SDavid Quigley if (kern_flags && !set_kern_flags) { 719649f6e77SDavid Quigley /* Specifying internal flags without providing a place to 720649f6e77SDavid Quigley * place the results is not allowed */ 721649f6e77SDavid Quigley rc = -EINVAL; 722649f6e77SDavid Quigley goto out; 723649f6e77SDavid Quigley } 724c9180a57SEric Paris 725c9180a57SEric Paris /* 726e0007529SEric Paris * Binary mount data FS will come through this function twice. Once 727e0007529SEric Paris * from an explicit call and once from the generic calls from the vfs. 728e0007529SEric Paris * Since the generic VFS calls will not contain any security mount data 729e0007529SEric Paris * we need to skip the double mount verification. 730e0007529SEric Paris * 731e0007529SEric Paris * This does open a hole in which we will not notice if the first 732e0007529SEric Paris * mount using this sb set explict options and a second mount using 733e0007529SEric Paris * this sb does not set any security options. (The first options 734e0007529SEric Paris * will be used for both mounts) 735e0007529SEric Paris */ 7360d90a7ecSDavid P. Quigley if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA) 737e0007529SEric Paris && (num_opts == 0)) 738e0007529SEric Paris goto out; 739e0007529SEric Paris 7402c97165bSPaul Moore root_isec = backing_inode_security_novalidate(root); 7412c97165bSPaul Moore 742e0007529SEric Paris /* 743c9180a57SEric Paris * parse the mount options, check if they are valid sids. 744c9180a57SEric Paris * also check if someone is trying to mount the same sb more 745c9180a57SEric Paris * than once with different security options. 746c9180a57SEric Paris */ 747c9180a57SEric Paris for (i = 0; i < num_opts; i++) { 748c9180a57SEric Paris u32 sid; 74911689d47SDavid P. Quigley 75012f348b9SEric Paris if (flags[i] == SBLABEL_MNT) 75111689d47SDavid P. Quigley continue; 75244be2f65SRasmus Villemoes rc = security_context_str_to_sid(mount_options[i], &sid, GFP_KERNEL); 753c9180a57SEric Paris if (rc) { 75444be2f65SRasmus Villemoes printk(KERN_WARNING "SELinux: security_context_str_to_sid" 75529b1deb2SLinus Torvalds "(%s) failed for (dev %s, type %s) errno=%d\n", 75629b1deb2SLinus Torvalds mount_options[i], sb->s_id, name, rc); 757c9180a57SEric Paris goto out; 758c9180a57SEric Paris } 759c9180a57SEric Paris switch (flags[i]) { 760c9180a57SEric Paris case FSCONTEXT_MNT: 761c9180a57SEric Paris fscontext_sid = sid; 762c9180a57SEric Paris 763c9180a57SEric Paris if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, 764c9180a57SEric Paris fscontext_sid)) 765c9180a57SEric Paris goto out_double_mount; 766c9180a57SEric Paris 767c9180a57SEric Paris sbsec->flags |= FSCONTEXT_MNT; 768c9180a57SEric Paris break; 769c9180a57SEric Paris case CONTEXT_MNT: 770c9180a57SEric Paris context_sid = sid; 771c9180a57SEric Paris 772c9180a57SEric Paris if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, 773c9180a57SEric Paris context_sid)) 774c9180a57SEric Paris goto out_double_mount; 775c9180a57SEric Paris 776c9180a57SEric Paris sbsec->flags |= CONTEXT_MNT; 777c9180a57SEric Paris break; 778c9180a57SEric Paris case ROOTCONTEXT_MNT: 779c9180a57SEric Paris rootcontext_sid = sid; 780c9180a57SEric Paris 781c9180a57SEric Paris if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, 782c9180a57SEric Paris rootcontext_sid)) 783c9180a57SEric Paris goto out_double_mount; 784c9180a57SEric Paris 785c9180a57SEric Paris sbsec->flags |= ROOTCONTEXT_MNT; 786c9180a57SEric Paris 787c9180a57SEric Paris break; 788c9180a57SEric Paris case DEFCONTEXT_MNT: 789c9180a57SEric Paris defcontext_sid = sid; 790c9180a57SEric Paris 791c9180a57SEric Paris if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, 792c9180a57SEric Paris defcontext_sid)) 793c9180a57SEric Paris goto out_double_mount; 794c9180a57SEric Paris 795c9180a57SEric Paris sbsec->flags |= DEFCONTEXT_MNT; 796c9180a57SEric Paris 797c9180a57SEric Paris break; 798c9180a57SEric Paris default: 799c9180a57SEric Paris rc = -EINVAL; 800c9180a57SEric Paris goto out; 801c9180a57SEric Paris } 802c9180a57SEric Paris } 803c9180a57SEric Paris 8040d90a7ecSDavid P. Quigley if (sbsec->flags & SE_SBINITIALIZED) { 805c9180a57SEric Paris /* previously mounted with options, but not on this attempt? */ 8060d90a7ecSDavid P. Quigley if ((sbsec->flags & SE_MNTMASK) && !num_opts) 807c9180a57SEric Paris goto out_double_mount; 808c9180a57SEric Paris rc = 0; 809c9180a57SEric Paris goto out; 810c9180a57SEric Paris } 811c9180a57SEric Paris 812089be43eSJames Morris if (strcmp(sb->s_type->name, "proc") == 0) 813134509d5SStephen Smalley sbsec->flags |= SE_SBPROC | SE_SBGENFS; 814134509d5SStephen Smalley 8158e014720SStephen Smalley if (!strcmp(sb->s_type->name, "debugfs") || 8166a391183SJeff Vander Stoep !strcmp(sb->s_type->name, "tracefs") || 8178e014720SStephen Smalley !strcmp(sb->s_type->name, "sysfs") || 818901ef845SAntonio Murdaca !strcmp(sb->s_type->name, "pstore") || 819901ef845SAntonio Murdaca !strcmp(sb->s_type->name, "cgroup") || 820901ef845SAntonio Murdaca !strcmp(sb->s_type->name, "cgroup2")) 821134509d5SStephen Smalley sbsec->flags |= SE_SBGENFS; 822c9180a57SEric Paris 823eb9ae686SDavid Quigley if (!sbsec->behavior) { 824eb9ae686SDavid Quigley /* 825eb9ae686SDavid Quigley * Determine the labeling behavior to use for this 826eb9ae686SDavid Quigley * filesystem type. 827eb9ae686SDavid Quigley */ 828a64c54cfSEric Paris rc = security_fs_use(sb); 829c9180a57SEric Paris if (rc) { 830eb9ae686SDavid Quigley printk(KERN_WARNING 831eb9ae686SDavid Quigley "%s: security_fs_use(%s) returned %d\n", 832089be43eSJames Morris __func__, sb->s_type->name, rc); 833c9180a57SEric Paris goto out; 834c9180a57SEric Paris } 835eb9ae686SDavid Quigley } 836aad82892SSeth Forshee 837aad82892SSeth Forshee /* 83801593d32SStephen Smalley * If this is a user namespace mount and the filesystem type is not 83901593d32SStephen Smalley * explicitly whitelisted, then no contexts are allowed on the command 84001593d32SStephen Smalley * line and security labels must be ignored. 841aad82892SSeth Forshee */ 84201593d32SStephen Smalley if (sb->s_user_ns != &init_user_ns && 84301593d32SStephen Smalley strcmp(sb->s_type->name, "tmpfs") && 84401593d32SStephen Smalley strcmp(sb->s_type->name, "ramfs") && 84501593d32SStephen Smalley strcmp(sb->s_type->name, "devpts")) { 846aad82892SSeth Forshee if (context_sid || fscontext_sid || rootcontext_sid || 847aad82892SSeth Forshee defcontext_sid) { 848aad82892SSeth Forshee rc = -EACCES; 849aad82892SSeth Forshee goto out; 850aad82892SSeth Forshee } 851aad82892SSeth Forshee if (sbsec->behavior == SECURITY_FS_USE_XATTR) { 852aad82892SSeth Forshee sbsec->behavior = SECURITY_FS_USE_MNTPOINT; 853aad82892SSeth Forshee rc = security_transition_sid(current_sid(), current_sid(), 854aad82892SSeth Forshee SECCLASS_FILE, NULL, 855aad82892SSeth Forshee &sbsec->mntpoint_sid); 856aad82892SSeth Forshee if (rc) 857aad82892SSeth Forshee goto out; 858aad82892SSeth Forshee } 859aad82892SSeth Forshee goto out_set_opts; 860aad82892SSeth Forshee } 861aad82892SSeth Forshee 862c9180a57SEric Paris /* sets the context of the superblock for the fs being mounted. */ 863c9180a57SEric Paris if (fscontext_sid) { 864275bb41eSDavid Howells rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred); 865c9180a57SEric Paris if (rc) 866c9180a57SEric Paris goto out; 867c9180a57SEric Paris 868c9180a57SEric Paris sbsec->sid = fscontext_sid; 869c9180a57SEric Paris } 870c9180a57SEric Paris 871c9180a57SEric Paris /* 872c9180a57SEric Paris * Switch to using mount point labeling behavior. 873c9180a57SEric Paris * sets the label used on all file below the mountpoint, and will set 874c9180a57SEric Paris * the superblock context if not already set. 875c9180a57SEric Paris */ 876eb9ae686SDavid Quigley if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) { 877eb9ae686SDavid Quigley sbsec->behavior = SECURITY_FS_USE_NATIVE; 878eb9ae686SDavid Quigley *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS; 879eb9ae686SDavid Quigley } 880eb9ae686SDavid Quigley 881c9180a57SEric Paris if (context_sid) { 882c9180a57SEric Paris if (!fscontext_sid) { 883275bb41eSDavid Howells rc = may_context_mount_sb_relabel(context_sid, sbsec, 884275bb41eSDavid Howells cred); 885c9180a57SEric Paris if (rc) 886c9180a57SEric Paris goto out; 887c9180a57SEric Paris sbsec->sid = context_sid; 888c9180a57SEric Paris } else { 889275bb41eSDavid Howells rc = may_context_mount_inode_relabel(context_sid, sbsec, 890275bb41eSDavid Howells cred); 891c9180a57SEric Paris if (rc) 892c9180a57SEric Paris goto out; 893c9180a57SEric Paris } 894c9180a57SEric Paris if (!rootcontext_sid) 895c9180a57SEric Paris rootcontext_sid = context_sid; 896c9180a57SEric Paris 897c9180a57SEric Paris sbsec->mntpoint_sid = context_sid; 898c9180a57SEric Paris sbsec->behavior = SECURITY_FS_USE_MNTPOINT; 899c9180a57SEric Paris } 900c9180a57SEric Paris 901c9180a57SEric Paris if (rootcontext_sid) { 902275bb41eSDavid Howells rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec, 903275bb41eSDavid Howells cred); 904c9180a57SEric Paris if (rc) 905c9180a57SEric Paris goto out; 906c9180a57SEric Paris 907c9180a57SEric Paris root_isec->sid = rootcontext_sid; 9086f3be9f5SAndreas Gruenbacher root_isec->initialized = LABEL_INITIALIZED; 909c9180a57SEric Paris } 910c9180a57SEric Paris 911c9180a57SEric Paris if (defcontext_sid) { 912eb9ae686SDavid Quigley if (sbsec->behavior != SECURITY_FS_USE_XATTR && 913eb9ae686SDavid Quigley sbsec->behavior != SECURITY_FS_USE_NATIVE) { 914c9180a57SEric Paris rc = -EINVAL; 915c9180a57SEric Paris printk(KERN_WARNING "SELinux: defcontext option is " 916c9180a57SEric Paris "invalid for this filesystem type\n"); 917c9180a57SEric Paris goto out; 918c9180a57SEric Paris } 919c9180a57SEric Paris 920c9180a57SEric Paris if (defcontext_sid != sbsec->def_sid) { 921c9180a57SEric Paris rc = may_context_mount_inode_relabel(defcontext_sid, 922275bb41eSDavid Howells sbsec, cred); 923c9180a57SEric Paris if (rc) 924c9180a57SEric Paris goto out; 925c9180a57SEric Paris } 926c9180a57SEric Paris 927c9180a57SEric Paris sbsec->def_sid = defcontext_sid; 928c9180a57SEric Paris } 929c9180a57SEric Paris 930aad82892SSeth Forshee out_set_opts: 931c9180a57SEric Paris rc = sb_finish_set_opts(sb); 932c9180a57SEric Paris out: 933bc7e982bSEric Paris mutex_unlock(&sbsec->lock); 9341da177e4SLinus Torvalds return rc; 935c9180a57SEric Paris out_double_mount: 936c9180a57SEric Paris rc = -EINVAL; 937c9180a57SEric Paris printk(KERN_WARNING "SELinux: mount invalid. Same superblock, different " 93829b1deb2SLinus Torvalds "security settings for (dev %s, type %s)\n", sb->s_id, name); 939c9180a57SEric Paris goto out; 940c9180a57SEric Paris } 941c9180a57SEric Paris 942094f7b69SJeff Layton static int selinux_cmp_sb_context(const struct super_block *oldsb, 943094f7b69SJeff Layton const struct super_block *newsb) 944094f7b69SJeff Layton { 945094f7b69SJeff Layton struct superblock_security_struct *old = oldsb->s_security; 946094f7b69SJeff Layton struct superblock_security_struct *new = newsb->s_security; 947094f7b69SJeff Layton char oldflags = old->flags & SE_MNTMASK; 948094f7b69SJeff Layton char newflags = new->flags & SE_MNTMASK; 949094f7b69SJeff Layton 950094f7b69SJeff Layton if (oldflags != newflags) 951094f7b69SJeff Layton goto mismatch; 952094f7b69SJeff Layton if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid) 953094f7b69SJeff Layton goto mismatch; 954094f7b69SJeff Layton if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid) 955094f7b69SJeff Layton goto mismatch; 956094f7b69SJeff Layton if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid) 957094f7b69SJeff Layton goto mismatch; 958094f7b69SJeff Layton if (oldflags & ROOTCONTEXT_MNT) { 95983da53c5SAndreas Gruenbacher struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root); 96083da53c5SAndreas Gruenbacher struct inode_security_struct *newroot = backing_inode_security(newsb->s_root); 961094f7b69SJeff Layton if (oldroot->sid != newroot->sid) 962094f7b69SJeff Layton goto mismatch; 963094f7b69SJeff Layton } 964094f7b69SJeff Layton return 0; 965094f7b69SJeff Layton mismatch: 966094f7b69SJeff Layton printk(KERN_WARNING "SELinux: mount invalid. Same superblock, " 967094f7b69SJeff Layton "different security settings for (dev %s, " 968094f7b69SJeff Layton "type %s)\n", newsb->s_id, newsb->s_type->name); 969094f7b69SJeff Layton return -EBUSY; 970094f7b69SJeff Layton } 971094f7b69SJeff Layton 972094f7b69SJeff Layton static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb, 9730b4d3452SScott Mayhew struct super_block *newsb, 9740b4d3452SScott Mayhew unsigned long kern_flags, 9750b4d3452SScott Mayhew unsigned long *set_kern_flags) 976c9180a57SEric Paris { 9770b4d3452SScott Mayhew int rc = 0; 978c9180a57SEric Paris const struct superblock_security_struct *oldsbsec = oldsb->s_security; 979c9180a57SEric Paris struct superblock_security_struct *newsbsec = newsb->s_security; 980c9180a57SEric Paris 981c9180a57SEric Paris int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT); 982c9180a57SEric Paris int set_context = (oldsbsec->flags & CONTEXT_MNT); 983c9180a57SEric Paris int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT); 984c9180a57SEric Paris 9850f5e6420SEric Paris /* 9860f5e6420SEric Paris * if the parent was able to be mounted it clearly had no special lsm 987e8c26255SAl Viro * mount options. thus we can safely deal with this superblock later 9880f5e6420SEric Paris */ 989e8c26255SAl Viro if (!ss_initialized) 990094f7b69SJeff Layton return 0; 991c9180a57SEric Paris 9920b4d3452SScott Mayhew /* 9930b4d3452SScott Mayhew * Specifying internal flags without providing a place to 9940b4d3452SScott Mayhew * place the results is not allowed. 9950b4d3452SScott Mayhew */ 9960b4d3452SScott Mayhew if (kern_flags && !set_kern_flags) 9970b4d3452SScott Mayhew return -EINVAL; 9980b4d3452SScott Mayhew 999c9180a57SEric Paris /* how can we clone if the old one wasn't set up?? */ 10000d90a7ecSDavid P. Quigley BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED)); 1001c9180a57SEric Paris 1002094f7b69SJeff Layton /* if fs is reusing a sb, make sure that the contexts match */ 10030d90a7ecSDavid P. Quigley if (newsbsec->flags & SE_SBINITIALIZED) 1004094f7b69SJeff Layton return selinux_cmp_sb_context(oldsb, newsb); 10055a552617SEric Paris 1006c9180a57SEric Paris mutex_lock(&newsbsec->lock); 1007c9180a57SEric Paris 1008c9180a57SEric Paris newsbsec->flags = oldsbsec->flags; 1009c9180a57SEric Paris 1010c9180a57SEric Paris newsbsec->sid = oldsbsec->sid; 1011c9180a57SEric Paris newsbsec->def_sid = oldsbsec->def_sid; 1012c9180a57SEric Paris newsbsec->behavior = oldsbsec->behavior; 1013c9180a57SEric Paris 10140b4d3452SScott Mayhew if (newsbsec->behavior == SECURITY_FS_USE_NATIVE && 10150b4d3452SScott Mayhew !(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) { 10160b4d3452SScott Mayhew rc = security_fs_use(newsb); 10170b4d3452SScott Mayhew if (rc) 10180b4d3452SScott Mayhew goto out; 10190b4d3452SScott Mayhew } 10200b4d3452SScott Mayhew 10210b4d3452SScott Mayhew if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) { 10220b4d3452SScott Mayhew newsbsec->behavior = SECURITY_FS_USE_NATIVE; 10230b4d3452SScott Mayhew *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS; 10240b4d3452SScott Mayhew } 10250b4d3452SScott Mayhew 1026c9180a57SEric Paris if (set_context) { 1027c9180a57SEric Paris u32 sid = oldsbsec->mntpoint_sid; 1028c9180a57SEric Paris 1029c9180a57SEric Paris if (!set_fscontext) 1030c9180a57SEric Paris newsbsec->sid = sid; 1031c9180a57SEric Paris if (!set_rootcontext) { 103283da53c5SAndreas Gruenbacher struct inode_security_struct *newisec = backing_inode_security(newsb->s_root); 1033c9180a57SEric Paris newisec->sid = sid; 1034c9180a57SEric Paris } 1035c9180a57SEric Paris newsbsec->mntpoint_sid = sid; 1036c9180a57SEric Paris } 1037c9180a57SEric Paris if (set_rootcontext) { 103883da53c5SAndreas Gruenbacher const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root); 103983da53c5SAndreas Gruenbacher struct inode_security_struct *newisec = backing_inode_security(newsb->s_root); 1040c9180a57SEric Paris 1041c9180a57SEric Paris newisec->sid = oldisec->sid; 1042c9180a57SEric Paris } 1043c9180a57SEric Paris 1044c9180a57SEric Paris sb_finish_set_opts(newsb); 10450b4d3452SScott Mayhew out: 1046c9180a57SEric Paris mutex_unlock(&newsbsec->lock); 10470b4d3452SScott Mayhew return rc; 1048c9180a57SEric Paris } 1049c9180a57SEric Paris 10502e1479d9SAdrian Bunk static int selinux_parse_opts_str(char *options, 10512e1479d9SAdrian Bunk struct security_mnt_opts *opts) 1052c9180a57SEric Paris { 1053e0007529SEric Paris char *p; 1054c9180a57SEric Paris char *context = NULL, *defcontext = NULL; 1055c9180a57SEric Paris char *fscontext = NULL, *rootcontext = NULL; 1056e0007529SEric Paris int rc, num_mnt_opts = 0; 1057c9180a57SEric Paris 1058e0007529SEric Paris opts->num_mnt_opts = 0; 1059c9180a57SEric Paris 1060c9180a57SEric Paris /* Standard string-based options. */ 1061c9180a57SEric Paris while ((p = strsep(&options, "|")) != NULL) { 1062c9180a57SEric Paris int token; 1063c9180a57SEric Paris substring_t args[MAX_OPT_ARGS]; 1064c9180a57SEric Paris 1065c9180a57SEric Paris if (!*p) 1066c9180a57SEric Paris continue; 1067c9180a57SEric Paris 1068c9180a57SEric Paris token = match_token(p, tokens, args); 1069c9180a57SEric Paris 1070c9180a57SEric Paris switch (token) { 1071c9180a57SEric Paris case Opt_context: 1072c9180a57SEric Paris if (context || defcontext) { 1073c9180a57SEric Paris rc = -EINVAL; 1074c9180a57SEric Paris printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 1075c9180a57SEric Paris goto out_err; 1076c9180a57SEric Paris } 1077c9180a57SEric Paris context = match_strdup(&args[0]); 1078c9180a57SEric Paris if (!context) { 1079c9180a57SEric Paris rc = -ENOMEM; 1080c9180a57SEric Paris goto out_err; 1081c9180a57SEric Paris } 1082c9180a57SEric Paris break; 1083c9180a57SEric Paris 1084c9180a57SEric Paris case Opt_fscontext: 1085c9180a57SEric Paris if (fscontext) { 1086c9180a57SEric Paris rc = -EINVAL; 1087c9180a57SEric Paris printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 1088c9180a57SEric Paris goto out_err; 1089c9180a57SEric Paris } 1090c9180a57SEric Paris fscontext = match_strdup(&args[0]); 1091c9180a57SEric Paris if (!fscontext) { 1092c9180a57SEric Paris rc = -ENOMEM; 1093c9180a57SEric Paris goto out_err; 1094c9180a57SEric Paris } 1095c9180a57SEric Paris break; 1096c9180a57SEric Paris 1097c9180a57SEric Paris case Opt_rootcontext: 1098c9180a57SEric Paris if (rootcontext) { 1099c9180a57SEric Paris rc = -EINVAL; 1100c9180a57SEric Paris printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 1101c9180a57SEric Paris goto out_err; 1102c9180a57SEric Paris } 1103c9180a57SEric Paris rootcontext = match_strdup(&args[0]); 1104c9180a57SEric Paris if (!rootcontext) { 1105c9180a57SEric Paris rc = -ENOMEM; 1106c9180a57SEric Paris goto out_err; 1107c9180a57SEric Paris } 1108c9180a57SEric Paris break; 1109c9180a57SEric Paris 1110c9180a57SEric Paris case Opt_defcontext: 1111c9180a57SEric Paris if (context || defcontext) { 1112c9180a57SEric Paris rc = -EINVAL; 1113c9180a57SEric Paris printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 1114c9180a57SEric Paris goto out_err; 1115c9180a57SEric Paris } 1116c9180a57SEric Paris defcontext = match_strdup(&args[0]); 1117c9180a57SEric Paris if (!defcontext) { 1118c9180a57SEric Paris rc = -ENOMEM; 1119c9180a57SEric Paris goto out_err; 1120c9180a57SEric Paris } 1121c9180a57SEric Paris break; 112211689d47SDavid P. Quigley case Opt_labelsupport: 112311689d47SDavid P. Quigley break; 1124c9180a57SEric Paris default: 1125c9180a57SEric Paris rc = -EINVAL; 1126c9180a57SEric Paris printk(KERN_WARNING "SELinux: unknown mount option\n"); 1127c9180a57SEric Paris goto out_err; 1128c9180a57SEric Paris 1129c9180a57SEric Paris } 1130c9180a57SEric Paris } 1131c9180a57SEric Paris 1132e0007529SEric Paris rc = -ENOMEM; 11338931c3bdSTetsuo Handa opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_KERNEL); 1134e0007529SEric Paris if (!opts->mnt_opts) 1135e0007529SEric Paris goto out_err; 1136e0007529SEric Paris 11378931c3bdSTetsuo Handa opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), 11388931c3bdSTetsuo Handa GFP_KERNEL); 1139023f108dSPaul Moore if (!opts->mnt_opts_flags) 1140e0007529SEric Paris goto out_err; 1141c9180a57SEric Paris 1142e0007529SEric Paris if (fscontext) { 1143e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = fscontext; 1144e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT; 1145e0007529SEric Paris } 1146e0007529SEric Paris if (context) { 1147e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = context; 1148e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT; 1149e0007529SEric Paris } 1150e0007529SEric Paris if (rootcontext) { 1151e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = rootcontext; 1152e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT; 1153e0007529SEric Paris } 1154e0007529SEric Paris if (defcontext) { 1155e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = defcontext; 1156e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT; 1157e0007529SEric Paris } 1158e0007529SEric Paris 1159e0007529SEric Paris opts->num_mnt_opts = num_mnt_opts; 1160e0007529SEric Paris return 0; 1161e0007529SEric Paris 1162c9180a57SEric Paris out_err: 1163023f108dSPaul Moore security_free_mnt_opts(opts); 1164c9180a57SEric Paris kfree(context); 1165c9180a57SEric Paris kfree(defcontext); 1166c9180a57SEric Paris kfree(fscontext); 1167c9180a57SEric Paris kfree(rootcontext); 1168c9180a57SEric Paris return rc; 11691da177e4SLinus Torvalds } 1170e0007529SEric Paris /* 1171e0007529SEric Paris * string mount options parsing and call set the sbsec 1172e0007529SEric Paris */ 1173e0007529SEric Paris static int superblock_doinit(struct super_block *sb, void *data) 1174e0007529SEric Paris { 1175e0007529SEric Paris int rc = 0; 1176e0007529SEric Paris char *options = data; 1177e0007529SEric Paris struct security_mnt_opts opts; 1178e0007529SEric Paris 1179e0007529SEric Paris security_init_mnt_opts(&opts); 1180e0007529SEric Paris 1181e0007529SEric Paris if (!data) 1182e0007529SEric Paris goto out; 1183e0007529SEric Paris 1184e0007529SEric Paris BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA); 1185e0007529SEric Paris 1186e0007529SEric Paris rc = selinux_parse_opts_str(options, &opts); 1187e0007529SEric Paris if (rc) 1188e0007529SEric Paris goto out_err; 1189e0007529SEric Paris 1190e0007529SEric Paris out: 1191649f6e77SDavid Quigley rc = selinux_set_mnt_opts(sb, &opts, 0, NULL); 1192e0007529SEric Paris 1193e0007529SEric Paris out_err: 1194e0007529SEric Paris security_free_mnt_opts(&opts); 1195e0007529SEric Paris return rc; 1196e0007529SEric Paris } 11971da177e4SLinus Torvalds 11983583a711SAdrian Bunk static void selinux_write_opts(struct seq_file *m, 11993583a711SAdrian Bunk struct security_mnt_opts *opts) 12002069f457SEric Paris { 12012069f457SEric Paris int i; 12022069f457SEric Paris char *prefix; 12032069f457SEric Paris 12042069f457SEric Paris for (i = 0; i < opts->num_mnt_opts; i++) { 120511689d47SDavid P. Quigley char *has_comma; 120611689d47SDavid P. Quigley 120711689d47SDavid P. Quigley if (opts->mnt_opts[i]) 120811689d47SDavid P. Quigley has_comma = strchr(opts->mnt_opts[i], ','); 120911689d47SDavid P. Quigley else 121011689d47SDavid P. Quigley has_comma = NULL; 12112069f457SEric Paris 12122069f457SEric Paris switch (opts->mnt_opts_flags[i]) { 12132069f457SEric Paris case CONTEXT_MNT: 12142069f457SEric Paris prefix = CONTEXT_STR; 12152069f457SEric Paris break; 12162069f457SEric Paris case FSCONTEXT_MNT: 12172069f457SEric Paris prefix = FSCONTEXT_STR; 12182069f457SEric Paris break; 12192069f457SEric Paris case ROOTCONTEXT_MNT: 12202069f457SEric Paris prefix = ROOTCONTEXT_STR; 12212069f457SEric Paris break; 12222069f457SEric Paris case DEFCONTEXT_MNT: 12232069f457SEric Paris prefix = DEFCONTEXT_STR; 12242069f457SEric Paris break; 122512f348b9SEric Paris case SBLABEL_MNT: 122611689d47SDavid P. Quigley seq_putc(m, ','); 122711689d47SDavid P. Quigley seq_puts(m, LABELSUPP_STR); 122811689d47SDavid P. Quigley continue; 12292069f457SEric Paris default: 12302069f457SEric Paris BUG(); 1231a35c6c83SEric Paris return; 12322069f457SEric Paris }; 12332069f457SEric Paris /* we need a comma before each option */ 12342069f457SEric Paris seq_putc(m, ','); 12352069f457SEric Paris seq_puts(m, prefix); 12362069f457SEric Paris if (has_comma) 12372069f457SEric Paris seq_putc(m, '\"'); 1238a068acf2SKees Cook seq_escape(m, opts->mnt_opts[i], "\"\n\\"); 12392069f457SEric Paris if (has_comma) 12402069f457SEric Paris seq_putc(m, '\"'); 12412069f457SEric Paris } 12422069f457SEric Paris } 12432069f457SEric Paris 12442069f457SEric Paris static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb) 12452069f457SEric Paris { 12462069f457SEric Paris struct security_mnt_opts opts; 12472069f457SEric Paris int rc; 12482069f457SEric Paris 12492069f457SEric Paris rc = selinux_get_mnt_opts(sb, &opts); 1250383795c2SEric Paris if (rc) { 1251383795c2SEric Paris /* before policy load we may get EINVAL, don't show anything */ 1252383795c2SEric Paris if (rc == -EINVAL) 1253383795c2SEric Paris rc = 0; 12542069f457SEric Paris return rc; 1255383795c2SEric Paris } 12562069f457SEric Paris 12572069f457SEric Paris selinux_write_opts(m, &opts); 12582069f457SEric Paris 12592069f457SEric Paris security_free_mnt_opts(&opts); 12602069f457SEric Paris 12612069f457SEric Paris return rc; 12622069f457SEric Paris } 12632069f457SEric Paris 12641da177e4SLinus Torvalds static inline u16 inode_mode_to_security_class(umode_t mode) 12651da177e4SLinus Torvalds { 12661da177e4SLinus Torvalds switch (mode & S_IFMT) { 12671da177e4SLinus Torvalds case S_IFSOCK: 12681da177e4SLinus Torvalds return SECCLASS_SOCK_FILE; 12691da177e4SLinus Torvalds case S_IFLNK: 12701da177e4SLinus Torvalds return SECCLASS_LNK_FILE; 12711da177e4SLinus Torvalds case S_IFREG: 12721da177e4SLinus Torvalds return SECCLASS_FILE; 12731da177e4SLinus Torvalds case S_IFBLK: 12741da177e4SLinus Torvalds return SECCLASS_BLK_FILE; 12751da177e4SLinus Torvalds case S_IFDIR: 12761da177e4SLinus Torvalds return SECCLASS_DIR; 12771da177e4SLinus Torvalds case S_IFCHR: 12781da177e4SLinus Torvalds return SECCLASS_CHR_FILE; 12791da177e4SLinus Torvalds case S_IFIFO: 12801da177e4SLinus Torvalds return SECCLASS_FIFO_FILE; 12811da177e4SLinus Torvalds 12821da177e4SLinus Torvalds } 12831da177e4SLinus Torvalds 12841da177e4SLinus Torvalds return SECCLASS_FILE; 12851da177e4SLinus Torvalds } 12861da177e4SLinus Torvalds 128713402580SJames Morris static inline int default_protocol_stream(int protocol) 128813402580SJames Morris { 128913402580SJames Morris return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP); 129013402580SJames Morris } 129113402580SJames Morris 129213402580SJames Morris static inline int default_protocol_dgram(int protocol) 129313402580SJames Morris { 129413402580SJames Morris return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP); 129513402580SJames Morris } 129613402580SJames Morris 12971da177e4SLinus Torvalds static inline u16 socket_type_to_security_class(int family, int type, int protocol) 12981da177e4SLinus Torvalds { 1299da69a530SStephen Smalley int extsockclass = selinux_policycap_extsockclass; 1300da69a530SStephen Smalley 13011da177e4SLinus Torvalds switch (family) { 13021da177e4SLinus Torvalds case PF_UNIX: 13031da177e4SLinus Torvalds switch (type) { 13041da177e4SLinus Torvalds case SOCK_STREAM: 13051da177e4SLinus Torvalds case SOCK_SEQPACKET: 13061da177e4SLinus Torvalds return SECCLASS_UNIX_STREAM_SOCKET; 13071da177e4SLinus Torvalds case SOCK_DGRAM: 13082a764b52SLuis Ressel case SOCK_RAW: 13091da177e4SLinus Torvalds return SECCLASS_UNIX_DGRAM_SOCKET; 13101da177e4SLinus Torvalds } 13111da177e4SLinus Torvalds break; 13121da177e4SLinus Torvalds case PF_INET: 13131da177e4SLinus Torvalds case PF_INET6: 13141da177e4SLinus Torvalds switch (type) { 13151da177e4SLinus Torvalds case SOCK_STREAM: 1316da69a530SStephen Smalley case SOCK_SEQPACKET: 131713402580SJames Morris if (default_protocol_stream(protocol)) 13181da177e4SLinus Torvalds return SECCLASS_TCP_SOCKET; 1319da69a530SStephen Smalley else if (extsockclass && protocol == IPPROTO_SCTP) 1320da69a530SStephen Smalley return SECCLASS_SCTP_SOCKET; 132113402580SJames Morris else 132213402580SJames Morris return SECCLASS_RAWIP_SOCKET; 13231da177e4SLinus Torvalds case SOCK_DGRAM: 132413402580SJames Morris if (default_protocol_dgram(protocol)) 13251da177e4SLinus Torvalds return SECCLASS_UDP_SOCKET; 1326ef37979aSStephen Smalley else if (extsockclass && (protocol == IPPROTO_ICMP || 1327ef37979aSStephen Smalley protocol == IPPROTO_ICMPV6)) 1328da69a530SStephen Smalley return SECCLASS_ICMP_SOCKET; 132913402580SJames Morris else 133013402580SJames Morris return SECCLASS_RAWIP_SOCKET; 13312ee92d46SJames Morris case SOCK_DCCP: 13322ee92d46SJames Morris return SECCLASS_DCCP_SOCKET; 133313402580SJames Morris default: 13341da177e4SLinus Torvalds return SECCLASS_RAWIP_SOCKET; 13351da177e4SLinus Torvalds } 13361da177e4SLinus Torvalds break; 13371da177e4SLinus Torvalds case PF_NETLINK: 13381da177e4SLinus Torvalds switch (protocol) { 13391da177e4SLinus Torvalds case NETLINK_ROUTE: 13401da177e4SLinus Torvalds return SECCLASS_NETLINK_ROUTE_SOCKET; 13417f1fb60cSPavel Emelyanov case NETLINK_SOCK_DIAG: 13421da177e4SLinus Torvalds return SECCLASS_NETLINK_TCPDIAG_SOCKET; 13431da177e4SLinus Torvalds case NETLINK_NFLOG: 13441da177e4SLinus Torvalds return SECCLASS_NETLINK_NFLOG_SOCKET; 13451da177e4SLinus Torvalds case NETLINK_XFRM: 13461da177e4SLinus Torvalds return SECCLASS_NETLINK_XFRM_SOCKET; 13471da177e4SLinus Torvalds case NETLINK_SELINUX: 13481da177e4SLinus Torvalds return SECCLASS_NETLINK_SELINUX_SOCKET; 13496c6d2e9bSStephen Smalley case NETLINK_ISCSI: 13506c6d2e9bSStephen Smalley return SECCLASS_NETLINK_ISCSI_SOCKET; 13511da177e4SLinus Torvalds case NETLINK_AUDIT: 13521da177e4SLinus Torvalds return SECCLASS_NETLINK_AUDIT_SOCKET; 13536c6d2e9bSStephen Smalley case NETLINK_FIB_LOOKUP: 13546c6d2e9bSStephen Smalley return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET; 13556c6d2e9bSStephen Smalley case NETLINK_CONNECTOR: 13566c6d2e9bSStephen Smalley return SECCLASS_NETLINK_CONNECTOR_SOCKET; 13576c6d2e9bSStephen Smalley case NETLINK_NETFILTER: 13586c6d2e9bSStephen Smalley return SECCLASS_NETLINK_NETFILTER_SOCKET; 13591da177e4SLinus Torvalds case NETLINK_DNRTMSG: 13601da177e4SLinus Torvalds return SECCLASS_NETLINK_DNRT_SOCKET; 13610c9b7942SJames Morris case NETLINK_KOBJECT_UEVENT: 13620c9b7942SJames Morris return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET; 13636c6d2e9bSStephen Smalley case NETLINK_GENERIC: 13646c6d2e9bSStephen Smalley return SECCLASS_NETLINK_GENERIC_SOCKET; 13656c6d2e9bSStephen Smalley case NETLINK_SCSITRANSPORT: 13666c6d2e9bSStephen Smalley return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET; 13676c6d2e9bSStephen Smalley case NETLINK_RDMA: 13686c6d2e9bSStephen Smalley return SECCLASS_NETLINK_RDMA_SOCKET; 13696c6d2e9bSStephen Smalley case NETLINK_CRYPTO: 13706c6d2e9bSStephen Smalley return SECCLASS_NETLINK_CRYPTO_SOCKET; 13711da177e4SLinus Torvalds default: 13721da177e4SLinus Torvalds return SECCLASS_NETLINK_SOCKET; 13731da177e4SLinus Torvalds } 13741da177e4SLinus Torvalds case PF_PACKET: 13751da177e4SLinus Torvalds return SECCLASS_PACKET_SOCKET; 13761da177e4SLinus Torvalds case PF_KEY: 13771da177e4SLinus Torvalds return SECCLASS_KEY_SOCKET; 13783e3ff15eSChristopher J. PeBenito case PF_APPLETALK: 13793e3ff15eSChristopher J. PeBenito return SECCLASS_APPLETALK_SOCKET; 13801da177e4SLinus Torvalds } 13811da177e4SLinus Torvalds 1382da69a530SStephen Smalley if (extsockclass) { 1383da69a530SStephen Smalley switch (family) { 1384da69a530SStephen Smalley case PF_AX25: 1385da69a530SStephen Smalley return SECCLASS_AX25_SOCKET; 1386da69a530SStephen Smalley case PF_IPX: 1387da69a530SStephen Smalley return SECCLASS_IPX_SOCKET; 1388da69a530SStephen Smalley case PF_NETROM: 1389da69a530SStephen Smalley return SECCLASS_NETROM_SOCKET; 1390da69a530SStephen Smalley case PF_ATMPVC: 1391da69a530SStephen Smalley return SECCLASS_ATMPVC_SOCKET; 1392da69a530SStephen Smalley case PF_X25: 1393da69a530SStephen Smalley return SECCLASS_X25_SOCKET; 1394da69a530SStephen Smalley case PF_ROSE: 1395da69a530SStephen Smalley return SECCLASS_ROSE_SOCKET; 1396da69a530SStephen Smalley case PF_DECnet: 1397da69a530SStephen Smalley return SECCLASS_DECNET_SOCKET; 1398da69a530SStephen Smalley case PF_ATMSVC: 1399da69a530SStephen Smalley return SECCLASS_ATMSVC_SOCKET; 1400da69a530SStephen Smalley case PF_RDS: 1401da69a530SStephen Smalley return SECCLASS_RDS_SOCKET; 1402da69a530SStephen Smalley case PF_IRDA: 1403da69a530SStephen Smalley return SECCLASS_IRDA_SOCKET; 1404da69a530SStephen Smalley case PF_PPPOX: 1405da69a530SStephen Smalley return SECCLASS_PPPOX_SOCKET; 1406da69a530SStephen Smalley case PF_LLC: 1407da69a530SStephen Smalley return SECCLASS_LLC_SOCKET; 1408da69a530SStephen Smalley case PF_CAN: 1409da69a530SStephen Smalley return SECCLASS_CAN_SOCKET; 1410da69a530SStephen Smalley case PF_TIPC: 1411da69a530SStephen Smalley return SECCLASS_TIPC_SOCKET; 1412da69a530SStephen Smalley case PF_BLUETOOTH: 1413da69a530SStephen Smalley return SECCLASS_BLUETOOTH_SOCKET; 1414da69a530SStephen Smalley case PF_IUCV: 1415da69a530SStephen Smalley return SECCLASS_IUCV_SOCKET; 1416da69a530SStephen Smalley case PF_RXRPC: 1417da69a530SStephen Smalley return SECCLASS_RXRPC_SOCKET; 1418da69a530SStephen Smalley case PF_ISDN: 1419da69a530SStephen Smalley return SECCLASS_ISDN_SOCKET; 1420da69a530SStephen Smalley case PF_PHONET: 1421da69a530SStephen Smalley return SECCLASS_PHONET_SOCKET; 1422da69a530SStephen Smalley case PF_IEEE802154: 1423da69a530SStephen Smalley return SECCLASS_IEEE802154_SOCKET; 1424da69a530SStephen Smalley case PF_CAIF: 1425da69a530SStephen Smalley return SECCLASS_CAIF_SOCKET; 1426da69a530SStephen Smalley case PF_ALG: 1427da69a530SStephen Smalley return SECCLASS_ALG_SOCKET; 1428da69a530SStephen Smalley case PF_NFC: 1429da69a530SStephen Smalley return SECCLASS_NFC_SOCKET; 1430da69a530SStephen Smalley case PF_VSOCK: 1431da69a530SStephen Smalley return SECCLASS_VSOCK_SOCKET; 1432da69a530SStephen Smalley case PF_KCM: 1433da69a530SStephen Smalley return SECCLASS_KCM_SOCKET; 1434da69a530SStephen Smalley case PF_QIPCRTR: 1435da69a530SStephen Smalley return SECCLASS_QIPCRTR_SOCKET; 14363051bf36SLinus Torvalds case PF_SMC: 14373051bf36SLinus Torvalds return SECCLASS_SMC_SOCKET; 14383051bf36SLinus Torvalds #if PF_MAX > 44 1439da69a530SStephen Smalley #error New address family defined, please update this function. 1440da69a530SStephen Smalley #endif 1441da69a530SStephen Smalley } 1442da69a530SStephen Smalley } 1443da69a530SStephen Smalley 14441da177e4SLinus Torvalds return SECCLASS_SOCKET; 14451da177e4SLinus Torvalds } 14461da177e4SLinus Torvalds 1447134509d5SStephen Smalley static int selinux_genfs_get_sid(struct dentry *dentry, 14481da177e4SLinus Torvalds u16 tclass, 1449134509d5SStephen Smalley u16 flags, 14501da177e4SLinus Torvalds u32 *sid) 14511da177e4SLinus Torvalds { 14528e6c9693SLucian Adrian Grijincu int rc; 1453fc64005cSAl Viro struct super_block *sb = dentry->d_sb; 14548e6c9693SLucian Adrian Grijincu char *buffer, *path; 14551da177e4SLinus Torvalds 14561da177e4SLinus Torvalds buffer = (char *)__get_free_page(GFP_KERNEL); 14571da177e4SLinus Torvalds if (!buffer) 14581da177e4SLinus Torvalds return -ENOMEM; 14591da177e4SLinus Torvalds 14608e6c9693SLucian Adrian Grijincu path = dentry_path_raw(dentry, buffer, PAGE_SIZE); 14618e6c9693SLucian Adrian Grijincu if (IS_ERR(path)) 14628e6c9693SLucian Adrian Grijincu rc = PTR_ERR(path); 14638e6c9693SLucian Adrian Grijincu else { 1464134509d5SStephen Smalley if (flags & SE_SBPROC) { 14658e6c9693SLucian Adrian Grijincu /* each process gets a /proc/PID/ entry. Strip off the 14668e6c9693SLucian Adrian Grijincu * PID part to get a valid selinux labeling. 14678e6c9693SLucian Adrian Grijincu * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */ 14688e6c9693SLucian Adrian Grijincu while (path[1] >= '0' && path[1] <= '9') { 14698e6c9693SLucian Adrian Grijincu path[1] = '/'; 14708e6c9693SLucian Adrian Grijincu path++; 14711da177e4SLinus Torvalds } 1472134509d5SStephen Smalley } 1473134509d5SStephen Smalley rc = security_genfs_sid(sb->s_type->name, path, tclass, sid); 14748e6c9693SLucian Adrian Grijincu } 14751da177e4SLinus Torvalds free_page((unsigned long)buffer); 14761da177e4SLinus Torvalds return rc; 14771da177e4SLinus Torvalds } 14781da177e4SLinus Torvalds 14791da177e4SLinus Torvalds /* The inode's security attributes must be initialized before first use. */ 14801da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry) 14811da177e4SLinus Torvalds { 14821da177e4SLinus Torvalds struct superblock_security_struct *sbsec = NULL; 14831da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 14849287aed2SAndreas Gruenbacher u32 task_sid, sid = 0; 14859287aed2SAndreas Gruenbacher u16 sclass; 14861da177e4SLinus Torvalds struct dentry *dentry; 14871da177e4SLinus Torvalds #define INITCONTEXTLEN 255 14881da177e4SLinus Torvalds char *context = NULL; 14891da177e4SLinus Torvalds unsigned len = 0; 14901da177e4SLinus Torvalds int rc = 0; 14911da177e4SLinus Torvalds 14926f3be9f5SAndreas Gruenbacher if (isec->initialized == LABEL_INITIALIZED) 149313457d07SAndreas Gruenbacher return 0; 14941da177e4SLinus Torvalds 14959287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 14966f3be9f5SAndreas Gruenbacher if (isec->initialized == LABEL_INITIALIZED) 149723970741SEric Paris goto out_unlock; 14981da177e4SLinus Torvalds 149913457d07SAndreas Gruenbacher if (isec->sclass == SECCLASS_FILE) 150013457d07SAndreas Gruenbacher isec->sclass = inode_mode_to_security_class(inode->i_mode); 150113457d07SAndreas Gruenbacher 15021da177e4SLinus Torvalds sbsec = inode->i_sb->s_security; 15030d90a7ecSDavid P. Quigley if (!(sbsec->flags & SE_SBINITIALIZED)) { 15041da177e4SLinus Torvalds /* Defer initialization until selinux_complete_init, 15051da177e4SLinus Torvalds after the initial policy is loaded and the security 15061da177e4SLinus Torvalds server is ready to handle calls. */ 15071da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 15081da177e4SLinus Torvalds if (list_empty(&isec->list)) 15091da177e4SLinus Torvalds list_add(&isec->list, &sbsec->isec_head); 15101da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 151123970741SEric Paris goto out_unlock; 15121da177e4SLinus Torvalds } 15131da177e4SLinus Torvalds 15149287aed2SAndreas Gruenbacher sclass = isec->sclass; 15159287aed2SAndreas Gruenbacher task_sid = isec->task_sid; 15169287aed2SAndreas Gruenbacher sid = isec->sid; 15179287aed2SAndreas Gruenbacher isec->initialized = LABEL_PENDING; 15189287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 15199287aed2SAndreas Gruenbacher 15201da177e4SLinus Torvalds switch (sbsec->behavior) { 1521eb9ae686SDavid Quigley case SECURITY_FS_USE_NATIVE: 1522eb9ae686SDavid Quigley break; 15231da177e4SLinus Torvalds case SECURITY_FS_USE_XATTR: 15245d6c3191SAndreas Gruenbacher if (!(inode->i_opflags & IOP_XATTR)) { 15259287aed2SAndreas Gruenbacher sid = sbsec->def_sid; 15261da177e4SLinus Torvalds break; 15271da177e4SLinus Torvalds } 15281da177e4SLinus Torvalds /* Need a dentry, since the xattr API requires one. 15291da177e4SLinus Torvalds Life would be simpler if we could just pass the inode. */ 15301da177e4SLinus Torvalds if (opt_dentry) { 15311da177e4SLinus Torvalds /* Called from d_instantiate or d_splice_alias. */ 15321da177e4SLinus Torvalds dentry = dget(opt_dentry); 15331da177e4SLinus Torvalds } else { 15341da177e4SLinus Torvalds /* Called from selinux_complete_init, try to find a dentry. */ 15351da177e4SLinus Torvalds dentry = d_find_alias(inode); 15361da177e4SLinus Torvalds } 15371da177e4SLinus Torvalds if (!dentry) { 1538df7f54c0SEric Paris /* 1539df7f54c0SEric Paris * this is can be hit on boot when a file is accessed 1540df7f54c0SEric Paris * before the policy is loaded. When we load policy we 1541df7f54c0SEric Paris * may find inodes that have no dentry on the 1542df7f54c0SEric Paris * sbsec->isec_head list. No reason to complain as these 1543df7f54c0SEric Paris * will get fixed up the next time we go through 1544df7f54c0SEric Paris * inode_doinit with a dentry, before these inodes could 1545df7f54c0SEric Paris * be used again by userspace. 1546df7f54c0SEric Paris */ 15479287aed2SAndreas Gruenbacher goto out; 15481da177e4SLinus Torvalds } 15491da177e4SLinus Torvalds 15501da177e4SLinus Torvalds len = INITCONTEXTLEN; 15514cb912f1SEric Paris context = kmalloc(len+1, GFP_NOFS); 15521da177e4SLinus Torvalds if (!context) { 15531da177e4SLinus Torvalds rc = -ENOMEM; 15541da177e4SLinus Torvalds dput(dentry); 15559287aed2SAndreas Gruenbacher goto out; 15561da177e4SLinus Torvalds } 15574cb912f1SEric Paris context[len] = '\0'; 15585d6c3191SAndreas Gruenbacher rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len); 15591da177e4SLinus Torvalds if (rc == -ERANGE) { 1560314dabb8SJames Morris kfree(context); 1561314dabb8SJames Morris 15621da177e4SLinus Torvalds /* Need a larger buffer. Query for the right size. */ 15635d6c3191SAndreas Gruenbacher rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0); 15641da177e4SLinus Torvalds if (rc < 0) { 15651da177e4SLinus Torvalds dput(dentry); 15669287aed2SAndreas Gruenbacher goto out; 15671da177e4SLinus Torvalds } 15681da177e4SLinus Torvalds len = rc; 15694cb912f1SEric Paris context = kmalloc(len+1, GFP_NOFS); 15701da177e4SLinus Torvalds if (!context) { 15711da177e4SLinus Torvalds rc = -ENOMEM; 15721da177e4SLinus Torvalds dput(dentry); 15739287aed2SAndreas Gruenbacher goto out; 15741da177e4SLinus Torvalds } 15754cb912f1SEric Paris context[len] = '\0'; 15765d6c3191SAndreas Gruenbacher rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len); 15771da177e4SLinus Torvalds } 15781da177e4SLinus Torvalds dput(dentry); 15791da177e4SLinus Torvalds if (rc < 0) { 15801da177e4SLinus Torvalds if (rc != -ENODATA) { 1581744ba35eSEric Paris printk(KERN_WARNING "SELinux: %s: getxattr returned " 1582dd6f953aSHarvey Harrison "%d for dev=%s ino=%ld\n", __func__, 15831da177e4SLinus Torvalds -rc, inode->i_sb->s_id, inode->i_ino); 15841da177e4SLinus Torvalds kfree(context); 15859287aed2SAndreas Gruenbacher goto out; 15861da177e4SLinus Torvalds } 15871da177e4SLinus Torvalds /* Map ENODATA to the default file SID */ 15881da177e4SLinus Torvalds sid = sbsec->def_sid; 15891da177e4SLinus Torvalds rc = 0; 15901da177e4SLinus Torvalds } else { 1591f5c1d5b2SJames Morris rc = security_context_to_sid_default(context, rc, &sid, 1592869ab514SStephen Smalley sbsec->def_sid, 1593869ab514SStephen Smalley GFP_NOFS); 15941da177e4SLinus Torvalds if (rc) { 15954ba0a8adSEric Paris char *dev = inode->i_sb->s_id; 15964ba0a8adSEric Paris unsigned long ino = inode->i_ino; 15974ba0a8adSEric Paris 15984ba0a8adSEric Paris if (rc == -EINVAL) { 15994ba0a8adSEric Paris if (printk_ratelimit()) 16004ba0a8adSEric Paris printk(KERN_NOTICE "SELinux: inode=%lu on dev=%s was found to have an invalid " 16014ba0a8adSEric Paris "context=%s. This indicates you may need to relabel the inode or the " 16024ba0a8adSEric Paris "filesystem in question.\n", ino, dev, context); 16034ba0a8adSEric Paris } else { 1604744ba35eSEric Paris printk(KERN_WARNING "SELinux: %s: context_to_sid(%s) " 16051da177e4SLinus Torvalds "returned %d for dev=%s ino=%ld\n", 16064ba0a8adSEric Paris __func__, context, -rc, dev, ino); 16074ba0a8adSEric Paris } 16081da177e4SLinus Torvalds kfree(context); 16091da177e4SLinus Torvalds /* Leave with the unlabeled SID */ 16101da177e4SLinus Torvalds rc = 0; 16111da177e4SLinus Torvalds break; 16121da177e4SLinus Torvalds } 16131da177e4SLinus Torvalds } 16141da177e4SLinus Torvalds kfree(context); 16151da177e4SLinus Torvalds break; 16161da177e4SLinus Torvalds case SECURITY_FS_USE_TASK: 16179287aed2SAndreas Gruenbacher sid = task_sid; 16181da177e4SLinus Torvalds break; 16191da177e4SLinus Torvalds case SECURITY_FS_USE_TRANS: 16201da177e4SLinus Torvalds /* Default to the fs SID. */ 16219287aed2SAndreas Gruenbacher sid = sbsec->sid; 16221da177e4SLinus Torvalds 16231da177e4SLinus Torvalds /* Try to obtain a transition SID. */ 16249287aed2SAndreas Gruenbacher rc = security_transition_sid(task_sid, sid, sclass, NULL, &sid); 16251da177e4SLinus Torvalds if (rc) 16269287aed2SAndreas Gruenbacher goto out; 16271da177e4SLinus Torvalds break; 1628c312feb2SEric Paris case SECURITY_FS_USE_MNTPOINT: 16299287aed2SAndreas Gruenbacher sid = sbsec->mntpoint_sid; 1630c312feb2SEric Paris break; 16311da177e4SLinus Torvalds default: 1632c312feb2SEric Paris /* Default to the fs superblock SID. */ 16339287aed2SAndreas Gruenbacher sid = sbsec->sid; 16341da177e4SLinus Torvalds 1635134509d5SStephen Smalley if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) { 1636f64410ecSPaul Moore /* We must have a dentry to determine the label on 1637f64410ecSPaul Moore * procfs inodes */ 1638f64410ecSPaul Moore if (opt_dentry) 1639f64410ecSPaul Moore /* Called from d_instantiate or 1640f64410ecSPaul Moore * d_splice_alias. */ 1641f64410ecSPaul Moore dentry = dget(opt_dentry); 1642f64410ecSPaul Moore else 1643f64410ecSPaul Moore /* Called from selinux_complete_init, try to 1644f64410ecSPaul Moore * find a dentry. */ 1645f64410ecSPaul Moore dentry = d_find_alias(inode); 1646f64410ecSPaul Moore /* 1647f64410ecSPaul Moore * This can be hit on boot when a file is accessed 1648f64410ecSPaul Moore * before the policy is loaded. When we load policy we 1649f64410ecSPaul Moore * may find inodes that have no dentry on the 1650f64410ecSPaul Moore * sbsec->isec_head list. No reason to complain as 1651f64410ecSPaul Moore * these will get fixed up the next time we go through 1652f64410ecSPaul Moore * inode_doinit() with a dentry, before these inodes 1653f64410ecSPaul Moore * could be used again by userspace. 1654f64410ecSPaul Moore */ 1655f64410ecSPaul Moore if (!dentry) 16569287aed2SAndreas Gruenbacher goto out; 16579287aed2SAndreas Gruenbacher rc = selinux_genfs_get_sid(dentry, sclass, 1658134509d5SStephen Smalley sbsec->flags, &sid); 1659f64410ecSPaul Moore dput(dentry); 16601da177e4SLinus Torvalds if (rc) 16619287aed2SAndreas Gruenbacher goto out; 16621da177e4SLinus Torvalds } 16631da177e4SLinus Torvalds break; 16641da177e4SLinus Torvalds } 16651da177e4SLinus Torvalds 16669287aed2SAndreas Gruenbacher out: 16679287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 16689287aed2SAndreas Gruenbacher if (isec->initialized == LABEL_PENDING) { 16699287aed2SAndreas Gruenbacher if (!sid || rc) { 16709287aed2SAndreas Gruenbacher isec->initialized = LABEL_INVALID; 16719287aed2SAndreas Gruenbacher goto out_unlock; 16729287aed2SAndreas Gruenbacher } 16739287aed2SAndreas Gruenbacher 16746f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 16759287aed2SAndreas Gruenbacher isec->sid = sid; 16769287aed2SAndreas Gruenbacher } 16771da177e4SLinus Torvalds 167823970741SEric Paris out_unlock: 16799287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 16801da177e4SLinus Torvalds return rc; 16811da177e4SLinus Torvalds } 16821da177e4SLinus Torvalds 16831da177e4SLinus Torvalds /* Convert a Linux signal to an access vector. */ 16841da177e4SLinus Torvalds static inline u32 signal_to_av(int sig) 16851da177e4SLinus Torvalds { 16861da177e4SLinus Torvalds u32 perm = 0; 16871da177e4SLinus Torvalds 16881da177e4SLinus Torvalds switch (sig) { 16891da177e4SLinus Torvalds case SIGCHLD: 16901da177e4SLinus Torvalds /* Commonly granted from child to parent. */ 16911da177e4SLinus Torvalds perm = PROCESS__SIGCHLD; 16921da177e4SLinus Torvalds break; 16931da177e4SLinus Torvalds case SIGKILL: 16941da177e4SLinus Torvalds /* Cannot be caught or ignored */ 16951da177e4SLinus Torvalds perm = PROCESS__SIGKILL; 16961da177e4SLinus Torvalds break; 16971da177e4SLinus Torvalds case SIGSTOP: 16981da177e4SLinus Torvalds /* Cannot be caught or ignored */ 16991da177e4SLinus Torvalds perm = PROCESS__SIGSTOP; 17001da177e4SLinus Torvalds break; 17011da177e4SLinus Torvalds default: 17021da177e4SLinus Torvalds /* All other signals. */ 17031da177e4SLinus Torvalds perm = PROCESS__SIGNAL; 17041da177e4SLinus Torvalds break; 17051da177e4SLinus Torvalds } 17061da177e4SLinus Torvalds 17071da177e4SLinus Torvalds return perm; 17081da177e4SLinus Torvalds } 17091da177e4SLinus Torvalds 1710b68e418cSStephen Smalley #if CAP_LAST_CAP > 63 1711b68e418cSStephen Smalley #error Fix SELinux to handle capabilities > 63. 1712b68e418cSStephen Smalley #endif 1713b68e418cSStephen Smalley 17141da177e4SLinus Torvalds /* Check whether a task is allowed to use a capability. */ 17156a9de491SEric Paris static int cred_has_capability(const struct cred *cred, 17168e4ff6f2SStephen Smalley int cap, int audit, bool initns) 17171da177e4SLinus Torvalds { 17182bf49690SThomas Liu struct common_audit_data ad; 171906112163SEric Paris struct av_decision avd; 1720b68e418cSStephen Smalley u16 sclass; 17213699c53cSDavid Howells u32 sid = cred_sid(cred); 1722b68e418cSStephen Smalley u32 av = CAP_TO_MASK(cap); 172306112163SEric Paris int rc; 17241da177e4SLinus Torvalds 172550c205f5SEric Paris ad.type = LSM_AUDIT_DATA_CAP; 17261da177e4SLinus Torvalds ad.u.cap = cap; 17271da177e4SLinus Torvalds 1728b68e418cSStephen Smalley switch (CAP_TO_INDEX(cap)) { 1729b68e418cSStephen Smalley case 0: 17308e4ff6f2SStephen Smalley sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS; 1731b68e418cSStephen Smalley break; 1732b68e418cSStephen Smalley case 1: 17338e4ff6f2SStephen Smalley sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS; 1734b68e418cSStephen Smalley break; 1735b68e418cSStephen Smalley default: 1736b68e418cSStephen Smalley printk(KERN_ERR 1737b68e418cSStephen Smalley "SELinux: out of range capability %d\n", cap); 1738b68e418cSStephen Smalley BUG(); 1739a35c6c83SEric Paris return -EINVAL; 1740b68e418cSStephen Smalley } 174106112163SEric Paris 1742275bb41eSDavid Howells rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd); 17439ade0cf4SEric Paris if (audit == SECURITY_CAP_AUDIT) { 17447b20ea25SNeilBrown int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad, 0); 17459ade0cf4SEric Paris if (rc2) 17469ade0cf4SEric Paris return rc2; 17479ade0cf4SEric Paris } 174806112163SEric Paris return rc; 17491da177e4SLinus Torvalds } 17501da177e4SLinus Torvalds 17511da177e4SLinus Torvalds /* Check whether a task has a particular permission to an inode. 17521da177e4SLinus Torvalds The 'adp' parameter is optional and allows other audit 17531da177e4SLinus Torvalds data to be passed (e.g. the dentry). */ 175488e67f3bSDavid Howells static int inode_has_perm(const struct cred *cred, 17551da177e4SLinus Torvalds struct inode *inode, 17561da177e4SLinus Torvalds u32 perms, 175719e49834SLinus Torvalds struct common_audit_data *adp) 17581da177e4SLinus Torvalds { 17591da177e4SLinus Torvalds struct inode_security_struct *isec; 1760275bb41eSDavid Howells u32 sid; 17611da177e4SLinus Torvalds 1762e0e81739SDavid Howells validate_creds(cred); 1763e0e81739SDavid Howells 1764bbaca6c2SStephen Smalley if (unlikely(IS_PRIVATE(inode))) 1765bbaca6c2SStephen Smalley return 0; 1766bbaca6c2SStephen Smalley 176788e67f3bSDavid Howells sid = cred_sid(cred); 17681da177e4SLinus Torvalds isec = inode->i_security; 17691da177e4SLinus Torvalds 177019e49834SLinus Torvalds return avc_has_perm(sid, isec->sid, isec->sclass, perms, adp); 17711da177e4SLinus Torvalds } 17721da177e4SLinus Torvalds 17731da177e4SLinus Torvalds /* Same as inode_has_perm, but pass explicit audit data containing 17741da177e4SLinus Torvalds the dentry to help the auditing code to more easily generate the 17751da177e4SLinus Torvalds pathname if needed. */ 177688e67f3bSDavid Howells static inline int dentry_has_perm(const struct cred *cred, 17771da177e4SLinus Torvalds struct dentry *dentry, 17781da177e4SLinus Torvalds u32 av) 17791da177e4SLinus Torvalds { 1780c6f493d6SDavid Howells struct inode *inode = d_backing_inode(dentry); 17812bf49690SThomas Liu struct common_audit_data ad; 178288e67f3bSDavid Howells 178350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 17842875fa00SEric Paris ad.u.dentry = dentry; 17855d226df4SAndreas Gruenbacher __inode_security_revalidate(inode, dentry, true); 178619e49834SLinus Torvalds return inode_has_perm(cred, inode, av, &ad); 17872875fa00SEric Paris } 17882875fa00SEric Paris 17892875fa00SEric Paris /* Same as inode_has_perm, but pass explicit audit data containing 17902875fa00SEric Paris the path to help the auditing code to more easily generate the 17912875fa00SEric Paris pathname if needed. */ 17922875fa00SEric Paris static inline int path_has_perm(const struct cred *cred, 17933f7036a0SAl Viro const struct path *path, 17942875fa00SEric Paris u32 av) 17952875fa00SEric Paris { 1796c6f493d6SDavid Howells struct inode *inode = d_backing_inode(path->dentry); 17972875fa00SEric Paris struct common_audit_data ad; 17982875fa00SEric Paris 179950c205f5SEric Paris ad.type = LSM_AUDIT_DATA_PATH; 18002875fa00SEric Paris ad.u.path = *path; 18015d226df4SAndreas Gruenbacher __inode_security_revalidate(inode, path->dentry, true); 180219e49834SLinus Torvalds return inode_has_perm(cred, inode, av, &ad); 18031da177e4SLinus Torvalds } 18041da177e4SLinus Torvalds 180513f8e981SDavid Howells /* Same as path_has_perm, but uses the inode from the file struct. */ 180613f8e981SDavid Howells static inline int file_path_has_perm(const struct cred *cred, 180713f8e981SDavid Howells struct file *file, 180813f8e981SDavid Howells u32 av) 180913f8e981SDavid Howells { 181013f8e981SDavid Howells struct common_audit_data ad; 181113f8e981SDavid Howells 181243af5de7SVivek Goyal ad.type = LSM_AUDIT_DATA_FILE; 181343af5de7SVivek Goyal ad.u.file = file; 181419e49834SLinus Torvalds return inode_has_perm(cred, file_inode(file), av, &ad); 181513f8e981SDavid Howells } 181613f8e981SDavid Howells 18171da177e4SLinus Torvalds /* Check whether a task can use an open file descriptor to 18181da177e4SLinus Torvalds access an inode in a given way. Check access to the 18191da177e4SLinus Torvalds descriptor itself, and then use dentry_has_perm to 18201da177e4SLinus Torvalds check a particular permission to the file. 18211da177e4SLinus Torvalds Access to the descriptor is implicitly granted if it 18221da177e4SLinus Torvalds has the same SID as the process. If av is zero, then 18231da177e4SLinus Torvalds access to the file is not checked, e.g. for cases 18241da177e4SLinus Torvalds where only the descriptor is affected like seek. */ 182588e67f3bSDavid Howells static int file_has_perm(const struct cred *cred, 18261da177e4SLinus Torvalds struct file *file, 18271da177e4SLinus Torvalds u32 av) 18281da177e4SLinus Torvalds { 18291da177e4SLinus Torvalds struct file_security_struct *fsec = file->f_security; 1830496ad9aaSAl Viro struct inode *inode = file_inode(file); 18312bf49690SThomas Liu struct common_audit_data ad; 183288e67f3bSDavid Howells u32 sid = cred_sid(cred); 18331da177e4SLinus Torvalds int rc; 18341da177e4SLinus Torvalds 183543af5de7SVivek Goyal ad.type = LSM_AUDIT_DATA_FILE; 183643af5de7SVivek Goyal ad.u.file = file; 18371da177e4SLinus Torvalds 1838275bb41eSDavid Howells if (sid != fsec->sid) { 1839275bb41eSDavid Howells rc = avc_has_perm(sid, fsec->sid, 18401da177e4SLinus Torvalds SECCLASS_FD, 18411da177e4SLinus Torvalds FD__USE, 18421da177e4SLinus Torvalds &ad); 18431da177e4SLinus Torvalds if (rc) 184488e67f3bSDavid Howells goto out; 18451da177e4SLinus Torvalds } 18461da177e4SLinus Torvalds 18471da177e4SLinus Torvalds /* av is zero if only checking access to the descriptor. */ 184888e67f3bSDavid Howells rc = 0; 18491da177e4SLinus Torvalds if (av) 185019e49834SLinus Torvalds rc = inode_has_perm(cred, inode, av, &ad); 18511da177e4SLinus Torvalds 185288e67f3bSDavid Howells out: 185388e67f3bSDavid Howells return rc; 18541da177e4SLinus Torvalds } 18551da177e4SLinus Torvalds 1856c3c188b2SDavid Howells /* 1857c3c188b2SDavid Howells * Determine the label for an inode that might be unioned. 1858c3c188b2SDavid Howells */ 1859c957f6dfSVivek Goyal static int 1860c957f6dfSVivek Goyal selinux_determine_inode_label(const struct task_security_struct *tsec, 1861c957f6dfSVivek Goyal struct inode *dir, 1862c957f6dfSVivek Goyal const struct qstr *name, u16 tclass, 1863c3c188b2SDavid Howells u32 *_new_isid) 1864c3c188b2SDavid Howells { 1865c3c188b2SDavid Howells const struct superblock_security_struct *sbsec = dir->i_sb->s_security; 1866c3c188b2SDavid Howells 1867c3c188b2SDavid Howells if ((sbsec->flags & SE_SBINITIALIZED) && 1868c3c188b2SDavid Howells (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) { 1869c3c188b2SDavid Howells *_new_isid = sbsec->mntpoint_sid; 1870c3c188b2SDavid Howells } else if ((sbsec->flags & SBLABEL_MNT) && 1871c3c188b2SDavid Howells tsec->create_sid) { 1872c3c188b2SDavid Howells *_new_isid = tsec->create_sid; 1873c3c188b2SDavid Howells } else { 187420cdef8dSPaul Moore const struct inode_security_struct *dsec = inode_security(dir); 1875c3c188b2SDavid Howells return security_transition_sid(tsec->sid, dsec->sid, tclass, 1876c3c188b2SDavid Howells name, _new_isid); 1877c3c188b2SDavid Howells } 1878c3c188b2SDavid Howells 1879c3c188b2SDavid Howells return 0; 1880c3c188b2SDavid Howells } 1881c3c188b2SDavid Howells 18821da177e4SLinus Torvalds /* Check whether a task can create a file. */ 18831da177e4SLinus Torvalds static int may_create(struct inode *dir, 18841da177e4SLinus Torvalds struct dentry *dentry, 18851da177e4SLinus Torvalds u16 tclass) 18861da177e4SLinus Torvalds { 18875fb49870SPaul Moore const struct task_security_struct *tsec = current_security(); 18881da177e4SLinus Torvalds struct inode_security_struct *dsec; 18891da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 1890275bb41eSDavid Howells u32 sid, newsid; 18912bf49690SThomas Liu struct common_audit_data ad; 18921da177e4SLinus Torvalds int rc; 18931da177e4SLinus Torvalds 189483da53c5SAndreas Gruenbacher dsec = inode_security(dir); 18951da177e4SLinus Torvalds sbsec = dir->i_sb->s_security; 18961da177e4SLinus Torvalds 1897275bb41eSDavid Howells sid = tsec->sid; 1898275bb41eSDavid Howells 189950c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 1900a269434dSEric Paris ad.u.dentry = dentry; 19011da177e4SLinus Torvalds 1902275bb41eSDavid Howells rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, 19031da177e4SLinus Torvalds DIR__ADD_NAME | DIR__SEARCH, 19041da177e4SLinus Torvalds &ad); 19051da177e4SLinus Torvalds if (rc) 19061da177e4SLinus Torvalds return rc; 19071da177e4SLinus Torvalds 1908c957f6dfSVivek Goyal rc = selinux_determine_inode_label(current_security(), dir, 1909c957f6dfSVivek Goyal &dentry->d_name, tclass, &newsid); 19101da177e4SLinus Torvalds if (rc) 19111da177e4SLinus Torvalds return rc; 19121da177e4SLinus Torvalds 1913275bb41eSDavid Howells rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad); 19141da177e4SLinus Torvalds if (rc) 19151da177e4SLinus Torvalds return rc; 19161da177e4SLinus Torvalds 19171da177e4SLinus Torvalds return avc_has_perm(newsid, sbsec->sid, 19181da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 19191da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, &ad); 19201da177e4SLinus Torvalds } 19211da177e4SLinus Torvalds 19221da177e4SLinus Torvalds #define MAY_LINK 0 19231da177e4SLinus Torvalds #define MAY_UNLINK 1 19241da177e4SLinus Torvalds #define MAY_RMDIR 2 19251da177e4SLinus Torvalds 19261da177e4SLinus Torvalds /* Check whether a task can link, unlink, or rmdir a file/directory. */ 19271da177e4SLinus Torvalds static int may_link(struct inode *dir, 19281da177e4SLinus Torvalds struct dentry *dentry, 19291da177e4SLinus Torvalds int kind) 19301da177e4SLinus Torvalds 19311da177e4SLinus Torvalds { 19321da177e4SLinus Torvalds struct inode_security_struct *dsec, *isec; 19332bf49690SThomas Liu struct common_audit_data ad; 1934275bb41eSDavid Howells u32 sid = current_sid(); 19351da177e4SLinus Torvalds u32 av; 19361da177e4SLinus Torvalds int rc; 19371da177e4SLinus Torvalds 193883da53c5SAndreas Gruenbacher dsec = inode_security(dir); 193983da53c5SAndreas Gruenbacher isec = backing_inode_security(dentry); 19401da177e4SLinus Torvalds 194150c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 1942a269434dSEric Paris ad.u.dentry = dentry; 19431da177e4SLinus Torvalds 19441da177e4SLinus Torvalds av = DIR__SEARCH; 19451da177e4SLinus Torvalds av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME); 1946275bb41eSDavid Howells rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad); 19471da177e4SLinus Torvalds if (rc) 19481da177e4SLinus Torvalds return rc; 19491da177e4SLinus Torvalds 19501da177e4SLinus Torvalds switch (kind) { 19511da177e4SLinus Torvalds case MAY_LINK: 19521da177e4SLinus Torvalds av = FILE__LINK; 19531da177e4SLinus Torvalds break; 19541da177e4SLinus Torvalds case MAY_UNLINK: 19551da177e4SLinus Torvalds av = FILE__UNLINK; 19561da177e4SLinus Torvalds break; 19571da177e4SLinus Torvalds case MAY_RMDIR: 19581da177e4SLinus Torvalds av = DIR__RMDIR; 19591da177e4SLinus Torvalds break; 19601da177e4SLinus Torvalds default: 1961744ba35eSEric Paris printk(KERN_WARNING "SELinux: %s: unrecognized kind %d\n", 1962744ba35eSEric Paris __func__, kind); 19631da177e4SLinus Torvalds return 0; 19641da177e4SLinus Torvalds } 19651da177e4SLinus Torvalds 1966275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad); 19671da177e4SLinus Torvalds return rc; 19681da177e4SLinus Torvalds } 19691da177e4SLinus Torvalds 19701da177e4SLinus Torvalds static inline int may_rename(struct inode *old_dir, 19711da177e4SLinus Torvalds struct dentry *old_dentry, 19721da177e4SLinus Torvalds struct inode *new_dir, 19731da177e4SLinus Torvalds struct dentry *new_dentry) 19741da177e4SLinus Torvalds { 19751da177e4SLinus Torvalds struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec; 19762bf49690SThomas Liu struct common_audit_data ad; 1977275bb41eSDavid Howells u32 sid = current_sid(); 19781da177e4SLinus Torvalds u32 av; 19791da177e4SLinus Torvalds int old_is_dir, new_is_dir; 19801da177e4SLinus Torvalds int rc; 19811da177e4SLinus Torvalds 198283da53c5SAndreas Gruenbacher old_dsec = inode_security(old_dir); 198383da53c5SAndreas Gruenbacher old_isec = backing_inode_security(old_dentry); 1984e36cb0b8SDavid Howells old_is_dir = d_is_dir(old_dentry); 198583da53c5SAndreas Gruenbacher new_dsec = inode_security(new_dir); 19861da177e4SLinus Torvalds 198750c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 19881da177e4SLinus Torvalds 1989a269434dSEric Paris ad.u.dentry = old_dentry; 1990275bb41eSDavid Howells rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR, 19911da177e4SLinus Torvalds DIR__REMOVE_NAME | DIR__SEARCH, &ad); 19921da177e4SLinus Torvalds if (rc) 19931da177e4SLinus Torvalds return rc; 1994275bb41eSDavid Howells rc = avc_has_perm(sid, old_isec->sid, 19951da177e4SLinus Torvalds old_isec->sclass, FILE__RENAME, &ad); 19961da177e4SLinus Torvalds if (rc) 19971da177e4SLinus Torvalds return rc; 19981da177e4SLinus Torvalds if (old_is_dir && new_dir != old_dir) { 1999275bb41eSDavid Howells rc = avc_has_perm(sid, old_isec->sid, 20001da177e4SLinus Torvalds old_isec->sclass, DIR__REPARENT, &ad); 20011da177e4SLinus Torvalds if (rc) 20021da177e4SLinus Torvalds return rc; 20031da177e4SLinus Torvalds } 20041da177e4SLinus Torvalds 2005a269434dSEric Paris ad.u.dentry = new_dentry; 20061da177e4SLinus Torvalds av = DIR__ADD_NAME | DIR__SEARCH; 20072c616d4dSDavid Howells if (d_is_positive(new_dentry)) 20081da177e4SLinus Torvalds av |= DIR__REMOVE_NAME; 2009275bb41eSDavid Howells rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad); 20101da177e4SLinus Torvalds if (rc) 20111da177e4SLinus Torvalds return rc; 20122c616d4dSDavid Howells if (d_is_positive(new_dentry)) { 201383da53c5SAndreas Gruenbacher new_isec = backing_inode_security(new_dentry); 2014e36cb0b8SDavid Howells new_is_dir = d_is_dir(new_dentry); 2015275bb41eSDavid Howells rc = avc_has_perm(sid, new_isec->sid, 20161da177e4SLinus Torvalds new_isec->sclass, 20171da177e4SLinus Torvalds (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad); 20181da177e4SLinus Torvalds if (rc) 20191da177e4SLinus Torvalds return rc; 20201da177e4SLinus Torvalds } 20211da177e4SLinus Torvalds 20221da177e4SLinus Torvalds return 0; 20231da177e4SLinus Torvalds } 20241da177e4SLinus Torvalds 20251da177e4SLinus Torvalds /* Check whether a task can perform a filesystem operation. */ 202688e67f3bSDavid Howells static int superblock_has_perm(const struct cred *cred, 20271da177e4SLinus Torvalds struct super_block *sb, 20281da177e4SLinus Torvalds u32 perms, 20292bf49690SThomas Liu struct common_audit_data *ad) 20301da177e4SLinus Torvalds { 20311da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 203288e67f3bSDavid Howells u32 sid = cred_sid(cred); 20331da177e4SLinus Torvalds 20341da177e4SLinus Torvalds sbsec = sb->s_security; 2035275bb41eSDavid Howells return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad); 20361da177e4SLinus Torvalds } 20371da177e4SLinus Torvalds 20381da177e4SLinus Torvalds /* Convert a Linux mode and permission mask to an access vector. */ 20391da177e4SLinus Torvalds static inline u32 file_mask_to_av(int mode, int mask) 20401da177e4SLinus Torvalds { 20411da177e4SLinus Torvalds u32 av = 0; 20421da177e4SLinus Torvalds 2043dba19c60SAl Viro if (!S_ISDIR(mode)) { 20441da177e4SLinus Torvalds if (mask & MAY_EXEC) 20451da177e4SLinus Torvalds av |= FILE__EXECUTE; 20461da177e4SLinus Torvalds if (mask & MAY_READ) 20471da177e4SLinus Torvalds av |= FILE__READ; 20481da177e4SLinus Torvalds 20491da177e4SLinus Torvalds if (mask & MAY_APPEND) 20501da177e4SLinus Torvalds av |= FILE__APPEND; 20511da177e4SLinus Torvalds else if (mask & MAY_WRITE) 20521da177e4SLinus Torvalds av |= FILE__WRITE; 20531da177e4SLinus Torvalds 20541da177e4SLinus Torvalds } else { 20551da177e4SLinus Torvalds if (mask & MAY_EXEC) 20561da177e4SLinus Torvalds av |= DIR__SEARCH; 20571da177e4SLinus Torvalds if (mask & MAY_WRITE) 20581da177e4SLinus Torvalds av |= DIR__WRITE; 20591da177e4SLinus Torvalds if (mask & MAY_READ) 20601da177e4SLinus Torvalds av |= DIR__READ; 20611da177e4SLinus Torvalds } 20621da177e4SLinus Torvalds 20631da177e4SLinus Torvalds return av; 20641da177e4SLinus Torvalds } 20651da177e4SLinus Torvalds 20661da177e4SLinus Torvalds /* Convert a Linux file to an access vector. */ 20671da177e4SLinus Torvalds static inline u32 file_to_av(struct file *file) 20681da177e4SLinus Torvalds { 20691da177e4SLinus Torvalds u32 av = 0; 20701da177e4SLinus Torvalds 20711da177e4SLinus Torvalds if (file->f_mode & FMODE_READ) 20721da177e4SLinus Torvalds av |= FILE__READ; 20731da177e4SLinus Torvalds if (file->f_mode & FMODE_WRITE) { 20741da177e4SLinus Torvalds if (file->f_flags & O_APPEND) 20751da177e4SLinus Torvalds av |= FILE__APPEND; 20761da177e4SLinus Torvalds else 20771da177e4SLinus Torvalds av |= FILE__WRITE; 20781da177e4SLinus Torvalds } 20790794c66dSStephen Smalley if (!av) { 20800794c66dSStephen Smalley /* 20810794c66dSStephen Smalley * Special file opened with flags 3 for ioctl-only use. 20820794c66dSStephen Smalley */ 20830794c66dSStephen Smalley av = FILE__IOCTL; 20840794c66dSStephen Smalley } 20851da177e4SLinus Torvalds 20861da177e4SLinus Torvalds return av; 20871da177e4SLinus Torvalds } 20881da177e4SLinus Torvalds 20898b6a5a37SEric Paris /* 20908b6a5a37SEric Paris * Convert a file to an access vector and include the correct open 20918b6a5a37SEric Paris * open permission. 20928b6a5a37SEric Paris */ 20938b6a5a37SEric Paris static inline u32 open_file_to_av(struct file *file) 20948b6a5a37SEric Paris { 20958b6a5a37SEric Paris u32 av = file_to_av(file); 2096ccb54478SStephen Smalley struct inode *inode = file_inode(file); 20978b6a5a37SEric Paris 2098ccb54478SStephen Smalley if (selinux_policycap_openperm && inode->i_sb->s_magic != SOCKFS_MAGIC) 20998b6a5a37SEric Paris av |= FILE__OPEN; 210049b7b8deSEric Paris 21018b6a5a37SEric Paris return av; 21028b6a5a37SEric Paris } 21038b6a5a37SEric Paris 21041da177e4SLinus Torvalds /* Hook functions begin here. */ 21051da177e4SLinus Torvalds 210679af7307SStephen Smalley static int selinux_binder_set_context_mgr(struct task_struct *mgr) 210779af7307SStephen Smalley { 210879af7307SStephen Smalley u32 mysid = current_sid(); 210979af7307SStephen Smalley u32 mgrsid = task_sid(mgr); 211079af7307SStephen Smalley 211179af7307SStephen Smalley return avc_has_perm(mysid, mgrsid, SECCLASS_BINDER, 211279af7307SStephen Smalley BINDER__SET_CONTEXT_MGR, NULL); 211379af7307SStephen Smalley } 211479af7307SStephen Smalley 211579af7307SStephen Smalley static int selinux_binder_transaction(struct task_struct *from, 211679af7307SStephen Smalley struct task_struct *to) 211779af7307SStephen Smalley { 211879af7307SStephen Smalley u32 mysid = current_sid(); 211979af7307SStephen Smalley u32 fromsid = task_sid(from); 212079af7307SStephen Smalley u32 tosid = task_sid(to); 212179af7307SStephen Smalley int rc; 212279af7307SStephen Smalley 212379af7307SStephen Smalley if (mysid != fromsid) { 212479af7307SStephen Smalley rc = avc_has_perm(mysid, fromsid, SECCLASS_BINDER, 212579af7307SStephen Smalley BINDER__IMPERSONATE, NULL); 212679af7307SStephen Smalley if (rc) 212779af7307SStephen Smalley return rc; 212879af7307SStephen Smalley } 212979af7307SStephen Smalley 213079af7307SStephen Smalley return avc_has_perm(fromsid, tosid, SECCLASS_BINDER, BINDER__CALL, 213179af7307SStephen Smalley NULL); 213279af7307SStephen Smalley } 213379af7307SStephen Smalley 213479af7307SStephen Smalley static int selinux_binder_transfer_binder(struct task_struct *from, 213579af7307SStephen Smalley struct task_struct *to) 213679af7307SStephen Smalley { 213779af7307SStephen Smalley u32 fromsid = task_sid(from); 213879af7307SStephen Smalley u32 tosid = task_sid(to); 213979af7307SStephen Smalley 214079af7307SStephen Smalley return avc_has_perm(fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER, 214179af7307SStephen Smalley NULL); 214279af7307SStephen Smalley } 214379af7307SStephen Smalley 214479af7307SStephen Smalley static int selinux_binder_transfer_file(struct task_struct *from, 214579af7307SStephen Smalley struct task_struct *to, 214679af7307SStephen Smalley struct file *file) 214779af7307SStephen Smalley { 214879af7307SStephen Smalley u32 sid = task_sid(to); 214979af7307SStephen Smalley struct file_security_struct *fsec = file->f_security; 215083da53c5SAndreas Gruenbacher struct dentry *dentry = file->f_path.dentry; 215120cdef8dSPaul Moore struct inode_security_struct *isec; 215279af7307SStephen Smalley struct common_audit_data ad; 215379af7307SStephen Smalley int rc; 215479af7307SStephen Smalley 215579af7307SStephen Smalley ad.type = LSM_AUDIT_DATA_PATH; 215679af7307SStephen Smalley ad.u.path = file->f_path; 215779af7307SStephen Smalley 215879af7307SStephen Smalley if (sid != fsec->sid) { 215979af7307SStephen Smalley rc = avc_has_perm(sid, fsec->sid, 216079af7307SStephen Smalley SECCLASS_FD, 216179af7307SStephen Smalley FD__USE, 216279af7307SStephen Smalley &ad); 216379af7307SStephen Smalley if (rc) 216479af7307SStephen Smalley return rc; 216579af7307SStephen Smalley } 216679af7307SStephen Smalley 216783da53c5SAndreas Gruenbacher if (unlikely(IS_PRIVATE(d_backing_inode(dentry)))) 216879af7307SStephen Smalley return 0; 216979af7307SStephen Smalley 217020cdef8dSPaul Moore isec = backing_inode_security(dentry); 217179af7307SStephen Smalley return avc_has_perm(sid, isec->sid, isec->sclass, file_to_av(file), 217279af7307SStephen Smalley &ad); 217379af7307SStephen Smalley } 217479af7307SStephen Smalley 21759e48858fSIngo Molnar static int selinux_ptrace_access_check(struct task_struct *child, 2176006ebb40SStephen Smalley unsigned int mode) 21771da177e4SLinus Torvalds { 2178275bb41eSDavid Howells u32 sid = current_sid(); 2179275bb41eSDavid Howells u32 csid = task_sid(child); 2180006ebb40SStephen Smalley 2181be0554c9SStephen Smalley if (mode & PTRACE_MODE_READ) 2182be0554c9SStephen Smalley return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, NULL); 2183be0554c9SStephen Smalley 2184be0554c9SStephen Smalley return avc_has_perm(sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL); 21855cd9c58fSDavid Howells } 21865cd9c58fSDavid Howells 21875cd9c58fSDavid Howells static int selinux_ptrace_traceme(struct task_struct *parent) 21885cd9c58fSDavid Howells { 2189be0554c9SStephen Smalley return avc_has_perm(task_sid(parent), current_sid(), SECCLASS_PROCESS, 2190be0554c9SStephen Smalley PROCESS__PTRACE, NULL); 21911da177e4SLinus Torvalds } 21921da177e4SLinus Torvalds 21931da177e4SLinus Torvalds static int selinux_capget(struct task_struct *target, kernel_cap_t *effective, 21941da177e4SLinus Torvalds kernel_cap_t *inheritable, kernel_cap_t *permitted) 21951da177e4SLinus Torvalds { 2196be0554c9SStephen Smalley return avc_has_perm(current_sid(), task_sid(target), SECCLASS_PROCESS, 2197be0554c9SStephen Smalley PROCESS__GETCAP, NULL); 21981da177e4SLinus Torvalds } 21991da177e4SLinus Torvalds 2200d84f4f99SDavid Howells static int selinux_capset(struct cred *new, const struct cred *old, 2201d84f4f99SDavid Howells const kernel_cap_t *effective, 220215a2460eSDavid Howells const kernel_cap_t *inheritable, 220315a2460eSDavid Howells const kernel_cap_t *permitted) 22041da177e4SLinus Torvalds { 2205be0554c9SStephen Smalley return avc_has_perm(cred_sid(old), cred_sid(new), SECCLASS_PROCESS, 2206be0554c9SStephen Smalley PROCESS__SETCAP, NULL); 22071da177e4SLinus Torvalds } 22081da177e4SLinus Torvalds 22095626d3e8SJames Morris /* 22105626d3e8SJames Morris * (This comment used to live with the selinux_task_setuid hook, 22115626d3e8SJames Morris * which was removed). 22125626d3e8SJames Morris * 22135626d3e8SJames Morris * Since setuid only affects the current process, and since the SELinux 22145626d3e8SJames Morris * controls are not based on the Linux identity attributes, SELinux does not 22155626d3e8SJames Morris * need to control this operation. However, SELinux does control the use of 22165626d3e8SJames Morris * the CAP_SETUID and CAP_SETGID capabilities using the capable hook. 22175626d3e8SJames Morris */ 22185626d3e8SJames Morris 22196a9de491SEric Paris static int selinux_capable(const struct cred *cred, struct user_namespace *ns, 22206a9de491SEric Paris int cap, int audit) 22211da177e4SLinus Torvalds { 22228e4ff6f2SStephen Smalley return cred_has_capability(cred, cap, audit, ns == &init_user_ns); 22231da177e4SLinus Torvalds } 22241da177e4SLinus Torvalds 22251da177e4SLinus Torvalds static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb) 22261da177e4SLinus Torvalds { 222788e67f3bSDavid Howells const struct cred *cred = current_cred(); 22281da177e4SLinus Torvalds int rc = 0; 22291da177e4SLinus Torvalds 22301da177e4SLinus Torvalds if (!sb) 22311da177e4SLinus Torvalds return 0; 22321da177e4SLinus Torvalds 22331da177e4SLinus Torvalds switch (cmds) { 22341da177e4SLinus Torvalds case Q_SYNC: 22351da177e4SLinus Torvalds case Q_QUOTAON: 22361da177e4SLinus Torvalds case Q_QUOTAOFF: 22371da177e4SLinus Torvalds case Q_SETINFO: 22381da177e4SLinus Torvalds case Q_SETQUOTA: 223988e67f3bSDavid Howells rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL); 22401da177e4SLinus Torvalds break; 22411da177e4SLinus Torvalds case Q_GETFMT: 22421da177e4SLinus Torvalds case Q_GETINFO: 22431da177e4SLinus Torvalds case Q_GETQUOTA: 224488e67f3bSDavid Howells rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL); 22451da177e4SLinus Torvalds break; 22461da177e4SLinus Torvalds default: 22471da177e4SLinus Torvalds rc = 0; /* let the kernel handle invalid cmds */ 22481da177e4SLinus Torvalds break; 22491da177e4SLinus Torvalds } 22501da177e4SLinus Torvalds return rc; 22511da177e4SLinus Torvalds } 22521da177e4SLinus Torvalds 22531da177e4SLinus Torvalds static int selinux_quota_on(struct dentry *dentry) 22541da177e4SLinus Torvalds { 225588e67f3bSDavid Howells const struct cred *cred = current_cred(); 225688e67f3bSDavid Howells 22572875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__QUOTAON); 22581da177e4SLinus Torvalds } 22591da177e4SLinus Torvalds 226012b3052cSEric Paris static int selinux_syslog(int type) 22611da177e4SLinus Torvalds { 22621da177e4SLinus Torvalds switch (type) { 2263d78ca3cdSKees Cook case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */ 2264d78ca3cdSKees Cook case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */ 2265be0554c9SStephen Smalley return avc_has_perm(current_sid(), SECINITSID_KERNEL, 2266be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL); 2267d78ca3cdSKees Cook case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */ 2268d78ca3cdSKees Cook case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */ 2269d78ca3cdSKees Cook /* Set level of messages printed to console */ 2270d78ca3cdSKees Cook case SYSLOG_ACTION_CONSOLE_LEVEL: 2271be0554c9SStephen Smalley return avc_has_perm(current_sid(), SECINITSID_KERNEL, 2272be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE, 2273be0554c9SStephen Smalley NULL); 22741da177e4SLinus Torvalds } 2275be0554c9SStephen Smalley /* All other syslog types */ 2276be0554c9SStephen Smalley return avc_has_perm(current_sid(), SECINITSID_KERNEL, 2277be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL); 22781da177e4SLinus Torvalds } 22791da177e4SLinus Torvalds 22801da177e4SLinus Torvalds /* 22811da177e4SLinus Torvalds * Check that a process has enough memory to allocate a new virtual 22821da177e4SLinus Torvalds * mapping. 0 means there is enough memory for the allocation to 22831da177e4SLinus Torvalds * succeed and -ENOMEM implies there is not. 22841da177e4SLinus Torvalds * 22851da177e4SLinus Torvalds * Do not audit the selinux permission check, as this is applied to all 22861da177e4SLinus Torvalds * processes that allocate mappings. 22871da177e4SLinus Torvalds */ 228834b4e4aaSAlan Cox static int selinux_vm_enough_memory(struct mm_struct *mm, long pages) 22891da177e4SLinus Torvalds { 22901da177e4SLinus Torvalds int rc, cap_sys_admin = 0; 22911da177e4SLinus Torvalds 2292b1d9e6b0SCasey Schaufler rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN, 22938e4ff6f2SStephen Smalley SECURITY_CAP_NOAUDIT, true); 22941da177e4SLinus Torvalds if (rc == 0) 22951da177e4SLinus Torvalds cap_sys_admin = 1; 22961da177e4SLinus Torvalds 2297b1d9e6b0SCasey Schaufler return cap_sys_admin; 22981da177e4SLinus Torvalds } 22991da177e4SLinus Torvalds 23001da177e4SLinus Torvalds /* binprm security operations */ 23011da177e4SLinus Torvalds 2302be0554c9SStephen Smalley static u32 ptrace_parent_sid(void) 23030c6181cbSPaul Moore { 23040c6181cbSPaul Moore u32 sid = 0; 23050c6181cbSPaul Moore struct task_struct *tracer; 23060c6181cbSPaul Moore 23070c6181cbSPaul Moore rcu_read_lock(); 2308be0554c9SStephen Smalley tracer = ptrace_parent(current); 23090c6181cbSPaul Moore if (tracer) 23100c6181cbSPaul Moore sid = task_sid(tracer); 23110c6181cbSPaul Moore rcu_read_unlock(); 23120c6181cbSPaul Moore 23130c6181cbSPaul Moore return sid; 23140c6181cbSPaul Moore } 23150c6181cbSPaul Moore 23167b0d0b40SStephen Smalley static int check_nnp_nosuid(const struct linux_binprm *bprm, 23177b0d0b40SStephen Smalley const struct task_security_struct *old_tsec, 23187b0d0b40SStephen Smalley const struct task_security_struct *new_tsec) 23197b0d0b40SStephen Smalley { 23207b0d0b40SStephen Smalley int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS); 2321380cf5baSAndy Lutomirski int nosuid = !mnt_may_suid(bprm->file->f_path.mnt); 23227b0d0b40SStephen Smalley int rc; 2323af63f419SStephen Smalley u32 av; 23247b0d0b40SStephen Smalley 23257b0d0b40SStephen Smalley if (!nnp && !nosuid) 23267b0d0b40SStephen Smalley return 0; /* neither NNP nor nosuid */ 23277b0d0b40SStephen Smalley 23287b0d0b40SStephen Smalley if (new_tsec->sid == old_tsec->sid) 23297b0d0b40SStephen Smalley return 0; /* No change in credentials */ 23307b0d0b40SStephen Smalley 23317b0d0b40SStephen Smalley /* 2332af63f419SStephen Smalley * If the policy enables the nnp_nosuid_transition policy capability, 2333af63f419SStephen Smalley * then we permit transitions under NNP or nosuid if the 2334af63f419SStephen Smalley * policy allows the corresponding permission between 2335af63f419SStephen Smalley * the old and new contexts. 2336af63f419SStephen Smalley */ 2337af63f419SStephen Smalley if (selinux_policycap_nnp_nosuid_transition) { 2338af63f419SStephen Smalley av = 0; 2339af63f419SStephen Smalley if (nnp) 2340af63f419SStephen Smalley av |= PROCESS2__NNP_TRANSITION; 2341af63f419SStephen Smalley if (nosuid) 2342af63f419SStephen Smalley av |= PROCESS2__NOSUID_TRANSITION; 2343af63f419SStephen Smalley rc = avc_has_perm(old_tsec->sid, new_tsec->sid, 2344af63f419SStephen Smalley SECCLASS_PROCESS2, av, NULL); 2345af63f419SStephen Smalley if (!rc) 2346af63f419SStephen Smalley return 0; 2347af63f419SStephen Smalley } 2348af63f419SStephen Smalley 2349af63f419SStephen Smalley /* 2350af63f419SStephen Smalley * We also permit NNP or nosuid transitions to bounded SIDs, 2351af63f419SStephen Smalley * i.e. SIDs that are guaranteed to only be allowed a subset 2352af63f419SStephen Smalley * of the permissions of the current SID. 23537b0d0b40SStephen Smalley */ 23547b0d0b40SStephen Smalley rc = security_bounded_transition(old_tsec->sid, new_tsec->sid); 2355af63f419SStephen Smalley if (!rc) 2356af63f419SStephen Smalley return 0; 2357af63f419SStephen Smalley 23587b0d0b40SStephen Smalley /* 23597b0d0b40SStephen Smalley * On failure, preserve the errno values for NNP vs nosuid. 23607b0d0b40SStephen Smalley * NNP: Operation not permitted for caller. 23617b0d0b40SStephen Smalley * nosuid: Permission denied to file. 23627b0d0b40SStephen Smalley */ 23637b0d0b40SStephen Smalley if (nnp) 23647b0d0b40SStephen Smalley return -EPERM; 23657b0d0b40SStephen Smalley return -EACCES; 23667b0d0b40SStephen Smalley } 23677b0d0b40SStephen Smalley 2368a6f76f23SDavid Howells static int selinux_bprm_set_creds(struct linux_binprm *bprm) 23691da177e4SLinus Torvalds { 2370a6f76f23SDavid Howells const struct task_security_struct *old_tsec; 2371a6f76f23SDavid Howells struct task_security_struct *new_tsec; 23721da177e4SLinus Torvalds struct inode_security_struct *isec; 23732bf49690SThomas Liu struct common_audit_data ad; 2374496ad9aaSAl Viro struct inode *inode = file_inode(bprm->file); 23751da177e4SLinus Torvalds int rc; 23761da177e4SLinus Torvalds 2377a6f76f23SDavid Howells /* SELinux context only depends on initial program or script and not 2378a6f76f23SDavid Howells * the script interpreter */ 2379ddb4a144SKees Cook if (bprm->called_set_creds) 23801da177e4SLinus Torvalds return 0; 23811da177e4SLinus Torvalds 2382a6f76f23SDavid Howells old_tsec = current_security(); 2383a6f76f23SDavid Howells new_tsec = bprm->cred->security; 238483da53c5SAndreas Gruenbacher isec = inode_security(inode); 23851da177e4SLinus Torvalds 23861da177e4SLinus Torvalds /* Default to the current task SID. */ 2387a6f76f23SDavid Howells new_tsec->sid = old_tsec->sid; 2388a6f76f23SDavid Howells new_tsec->osid = old_tsec->sid; 23891da177e4SLinus Torvalds 239028eba5bfSMichael LeMay /* Reset fs, key, and sock SIDs on execve. */ 2391a6f76f23SDavid Howells new_tsec->create_sid = 0; 2392a6f76f23SDavid Howells new_tsec->keycreate_sid = 0; 2393a6f76f23SDavid Howells new_tsec->sockcreate_sid = 0; 23941da177e4SLinus Torvalds 2395a6f76f23SDavid Howells if (old_tsec->exec_sid) { 2396a6f76f23SDavid Howells new_tsec->sid = old_tsec->exec_sid; 23971da177e4SLinus Torvalds /* Reset exec SID on execve. */ 2398a6f76f23SDavid Howells new_tsec->exec_sid = 0; 2399259e5e6cSAndy Lutomirski 24007b0d0b40SStephen Smalley /* Fail on NNP or nosuid if not an allowed transition. */ 24017b0d0b40SStephen Smalley rc = check_nnp_nosuid(bprm, old_tsec, new_tsec); 24027b0d0b40SStephen Smalley if (rc) 24037b0d0b40SStephen Smalley return rc; 24041da177e4SLinus Torvalds } else { 24051da177e4SLinus Torvalds /* Check for a default transition on this program. */ 2406a6f76f23SDavid Howells rc = security_transition_sid(old_tsec->sid, isec->sid, 2407652bb9b0SEric Paris SECCLASS_PROCESS, NULL, 2408652bb9b0SEric Paris &new_tsec->sid); 24091da177e4SLinus Torvalds if (rc) 24101da177e4SLinus Torvalds return rc; 24117b0d0b40SStephen Smalley 24127b0d0b40SStephen Smalley /* 24137b0d0b40SStephen Smalley * Fallback to old SID on NNP or nosuid if not an allowed 24147b0d0b40SStephen Smalley * transition. 24157b0d0b40SStephen Smalley */ 24167b0d0b40SStephen Smalley rc = check_nnp_nosuid(bprm, old_tsec, new_tsec); 24177b0d0b40SStephen Smalley if (rc) 24187b0d0b40SStephen Smalley new_tsec->sid = old_tsec->sid; 24191da177e4SLinus Torvalds } 24201da177e4SLinus Torvalds 242143af5de7SVivek Goyal ad.type = LSM_AUDIT_DATA_FILE; 242243af5de7SVivek Goyal ad.u.file = bprm->file; 24231da177e4SLinus Torvalds 2424a6f76f23SDavid Howells if (new_tsec->sid == old_tsec->sid) { 2425a6f76f23SDavid Howells rc = avc_has_perm(old_tsec->sid, isec->sid, 24261da177e4SLinus Torvalds SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad); 24271da177e4SLinus Torvalds if (rc) 24281da177e4SLinus Torvalds return rc; 24291da177e4SLinus Torvalds } else { 24301da177e4SLinus Torvalds /* Check permissions for the transition. */ 2431a6f76f23SDavid Howells rc = avc_has_perm(old_tsec->sid, new_tsec->sid, 24321da177e4SLinus Torvalds SECCLASS_PROCESS, PROCESS__TRANSITION, &ad); 24331da177e4SLinus Torvalds if (rc) 24341da177e4SLinus Torvalds return rc; 24351da177e4SLinus Torvalds 2436a6f76f23SDavid Howells rc = avc_has_perm(new_tsec->sid, isec->sid, 24371da177e4SLinus Torvalds SECCLASS_FILE, FILE__ENTRYPOINT, &ad); 24381da177e4SLinus Torvalds if (rc) 24391da177e4SLinus Torvalds return rc; 24401da177e4SLinus Torvalds 2441a6f76f23SDavid Howells /* Check for shared state */ 2442a6f76f23SDavid Howells if (bprm->unsafe & LSM_UNSAFE_SHARE) { 2443a6f76f23SDavid Howells rc = avc_has_perm(old_tsec->sid, new_tsec->sid, 2444a6f76f23SDavid Howells SECCLASS_PROCESS, PROCESS__SHARE, 2445a6f76f23SDavid Howells NULL); 2446a6f76f23SDavid Howells if (rc) 2447a6f76f23SDavid Howells return -EPERM; 24481da177e4SLinus Torvalds } 24491da177e4SLinus Torvalds 2450a6f76f23SDavid Howells /* Make sure that anyone attempting to ptrace over a task that 2451a6f76f23SDavid Howells * changes its SID has the appropriate permit */ 24529227dd2aSEric W. Biederman if (bprm->unsafe & LSM_UNSAFE_PTRACE) { 2453be0554c9SStephen Smalley u32 ptsid = ptrace_parent_sid(); 2454a6f76f23SDavid Howells if (ptsid != 0) { 2455a6f76f23SDavid Howells rc = avc_has_perm(ptsid, new_tsec->sid, 2456a6f76f23SDavid Howells SECCLASS_PROCESS, 2457a6f76f23SDavid Howells PROCESS__PTRACE, NULL); 2458a6f76f23SDavid Howells if (rc) 2459a6f76f23SDavid Howells return -EPERM; 2460a6f76f23SDavid Howells } 2461a6f76f23SDavid Howells } 2462a6f76f23SDavid Howells 2463a6f76f23SDavid Howells /* Clear any possibly unsafe personality bits on exec: */ 2464a6f76f23SDavid Howells bprm->per_clear |= PER_CLEAR_ON_SETID; 2465a6f76f23SDavid Howells 24661da177e4SLinus Torvalds /* Enable secure mode for SIDs transitions unless 24671da177e4SLinus Torvalds the noatsecure permission is granted between 24681da177e4SLinus Torvalds the two SIDs, i.e. ahp returns 0. */ 246962874c3aSKees Cook rc = avc_has_perm(old_tsec->sid, new_tsec->sid, 247062874c3aSKees Cook SECCLASS_PROCESS, PROCESS__NOATSECURE, 247162874c3aSKees Cook NULL); 247262874c3aSKees Cook bprm->secureexec |= !!rc; 24731da177e4SLinus Torvalds } 24741da177e4SLinus Torvalds 247562874c3aSKees Cook return 0; 24761da177e4SLinus Torvalds } 24771da177e4SLinus Torvalds 2478c3c073f8SAl Viro static int match_file(const void *p, struct file *file, unsigned fd) 2479c3c073f8SAl Viro { 2480c3c073f8SAl Viro return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0; 2481c3c073f8SAl Viro } 2482c3c073f8SAl Viro 24831da177e4SLinus Torvalds /* Derived from fs/exec.c:flush_old_files. */ 2484745ca247SDavid Howells static inline void flush_unauthorized_files(const struct cred *cred, 2485745ca247SDavid Howells struct files_struct *files) 24861da177e4SLinus Torvalds { 24871da177e4SLinus Torvalds struct file *file, *devnull = NULL; 2488b20c8122SStephen Smalley struct tty_struct *tty; 248924ec839cSPeter Zijlstra int drop_tty = 0; 2490c3c073f8SAl Viro unsigned n; 24911da177e4SLinus Torvalds 249224ec839cSPeter Zijlstra tty = get_current_tty(); 24931da177e4SLinus Torvalds if (tty) { 24944a510969SPeter Hurley spin_lock(&tty->files_lock); 249537dd0bd0SEric Paris if (!list_empty(&tty->tty_files)) { 2496d996b62aSNick Piggin struct tty_file_private *file_priv; 249737dd0bd0SEric Paris 24981da177e4SLinus Torvalds /* Revalidate access to controlling tty. 249913f8e981SDavid Howells Use file_path_has_perm on the tty path directly 250013f8e981SDavid Howells rather than using file_has_perm, as this particular 250113f8e981SDavid Howells open file may belong to another process and we are 250213f8e981SDavid Howells only interested in the inode-based check here. */ 2503d996b62aSNick Piggin file_priv = list_first_entry(&tty->tty_files, 2504d996b62aSNick Piggin struct tty_file_private, list); 2505d996b62aSNick Piggin file = file_priv->file; 250613f8e981SDavid Howells if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE)) 250724ec839cSPeter Zijlstra drop_tty = 1; 25081da177e4SLinus Torvalds } 25094a510969SPeter Hurley spin_unlock(&tty->files_lock); 2510452a00d2SAlan Cox tty_kref_put(tty); 25111da177e4SLinus Torvalds } 251298a27ba4SEric W. Biederman /* Reset controlling tty. */ 251398a27ba4SEric W. Biederman if (drop_tty) 251498a27ba4SEric W. Biederman no_tty(); 25151da177e4SLinus Torvalds 25161da177e4SLinus Torvalds /* Revalidate access to inherited open files. */ 2517c3c073f8SAl Viro n = iterate_fd(files, 0, match_file, cred); 2518c3c073f8SAl Viro if (!n) /* none found? */ 2519c3c073f8SAl Viro return; 25201da177e4SLinus Torvalds 2521c3c073f8SAl Viro devnull = dentry_open(&selinux_null, O_RDWR, cred); 252245525b26SAl Viro if (IS_ERR(devnull)) 252345525b26SAl Viro devnull = NULL; 2524c3c073f8SAl Viro /* replace all the matching ones with this */ 2525c3c073f8SAl Viro do { 252645525b26SAl Viro replace_fd(n - 1, devnull, 0); 2527c3c073f8SAl Viro } while ((n = iterate_fd(files, n, match_file, cred)) != 0); 252845525b26SAl Viro if (devnull) 2529c3c073f8SAl Viro fput(devnull); 25301da177e4SLinus Torvalds } 25311da177e4SLinus Torvalds 25321da177e4SLinus Torvalds /* 2533a6f76f23SDavid Howells * Prepare a process for imminent new credential changes due to exec 25341da177e4SLinus Torvalds */ 2535a6f76f23SDavid Howells static void selinux_bprm_committing_creds(struct linux_binprm *bprm) 25361da177e4SLinus Torvalds { 2537a6f76f23SDavid Howells struct task_security_struct *new_tsec; 25381da177e4SLinus Torvalds struct rlimit *rlim, *initrlim; 25391da177e4SLinus Torvalds int rc, i; 25401da177e4SLinus Torvalds 2541a6f76f23SDavid Howells new_tsec = bprm->cred->security; 2542a6f76f23SDavid Howells if (new_tsec->sid == new_tsec->osid) 25431da177e4SLinus Torvalds return; 25441da177e4SLinus Torvalds 25451da177e4SLinus Torvalds /* Close files for which the new task SID is not authorized. */ 2546a6f76f23SDavid Howells flush_unauthorized_files(bprm->cred, current->files); 25471da177e4SLinus Torvalds 2548a6f76f23SDavid Howells /* Always clear parent death signal on SID transitions. */ 2549a6f76f23SDavid Howells current->pdeath_signal = 0; 2550a6f76f23SDavid Howells 2551a6f76f23SDavid Howells /* Check whether the new SID can inherit resource limits from the old 2552a6f76f23SDavid Howells * SID. If not, reset all soft limits to the lower of the current 2553a6f76f23SDavid Howells * task's hard limit and the init task's soft limit. 2554a6f76f23SDavid Howells * 2555a6f76f23SDavid Howells * Note that the setting of hard limits (even to lower them) can be 2556a6f76f23SDavid Howells * controlled by the setrlimit check. The inclusion of the init task's 2557a6f76f23SDavid Howells * soft limit into the computation is to avoid resetting soft limits 2558a6f76f23SDavid Howells * higher than the default soft limit for cases where the default is 2559a6f76f23SDavid Howells * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK. 2560a6f76f23SDavid Howells */ 2561a6f76f23SDavid Howells rc = avc_has_perm(new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS, 2562a6f76f23SDavid Howells PROCESS__RLIMITINH, NULL); 2563a6f76f23SDavid Howells if (rc) { 2564eb2d55a3SOleg Nesterov /* protect against do_prlimit() */ 2565eb2d55a3SOleg Nesterov task_lock(current); 2566a6f76f23SDavid Howells for (i = 0; i < RLIM_NLIMITS; i++) { 2567a6f76f23SDavid Howells rlim = current->signal->rlim + i; 2568a6f76f23SDavid Howells initrlim = init_task.signal->rlim + i; 2569a6f76f23SDavid Howells rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur); 2570a6f76f23SDavid Howells } 2571eb2d55a3SOleg Nesterov task_unlock(current); 2572baa73d9eSNicolas Pitre if (IS_ENABLED(CONFIG_POSIX_TIMERS)) 2573eb2d55a3SOleg Nesterov update_rlimit_cpu(current, rlimit(RLIMIT_CPU)); 2574a6f76f23SDavid Howells } 2575a6f76f23SDavid Howells } 2576a6f76f23SDavid Howells 2577a6f76f23SDavid Howells /* 2578a6f76f23SDavid Howells * Clean up the process immediately after the installation of new credentials 2579a6f76f23SDavid Howells * due to exec 2580a6f76f23SDavid Howells */ 2581a6f76f23SDavid Howells static void selinux_bprm_committed_creds(struct linux_binprm *bprm) 2582a6f76f23SDavid Howells { 2583a6f76f23SDavid Howells const struct task_security_struct *tsec = current_security(); 2584a6f76f23SDavid Howells struct itimerval itimer; 2585a6f76f23SDavid Howells u32 osid, sid; 2586a6f76f23SDavid Howells int rc, i; 2587a6f76f23SDavid Howells 2588a6f76f23SDavid Howells osid = tsec->osid; 2589a6f76f23SDavid Howells sid = tsec->sid; 2590a6f76f23SDavid Howells 2591a6f76f23SDavid Howells if (sid == osid) 2592a6f76f23SDavid Howells return; 2593a6f76f23SDavid Howells 2594a6f76f23SDavid Howells /* Check whether the new SID can inherit signal state from the old SID. 2595a6f76f23SDavid Howells * If not, clear itimers to avoid subsequent signal generation and 2596a6f76f23SDavid Howells * flush and unblock signals. 2597a6f76f23SDavid Howells * 2598a6f76f23SDavid Howells * This must occur _after_ the task SID has been updated so that any 2599a6f76f23SDavid Howells * kill done after the flush will be checked against the new SID. 2600a6f76f23SDavid Howells */ 2601a6f76f23SDavid Howells rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL); 26021da177e4SLinus Torvalds if (rc) { 2603baa73d9eSNicolas Pitre if (IS_ENABLED(CONFIG_POSIX_TIMERS)) { 26041da177e4SLinus Torvalds memset(&itimer, 0, sizeof itimer); 26051da177e4SLinus Torvalds for (i = 0; i < 3; i++) 26061da177e4SLinus Torvalds do_setitimer(i, &itimer, NULL); 2607baa73d9eSNicolas Pitre } 26081da177e4SLinus Torvalds spin_lock_irq(¤t->sighand->siglock); 26099e7c8f8cSOleg Nesterov if (!fatal_signal_pending(current)) { 26109e7c8f8cSOleg Nesterov flush_sigqueue(¤t->pending); 26119e7c8f8cSOleg Nesterov flush_sigqueue(¤t->signal->shared_pending); 26121da177e4SLinus Torvalds flush_signal_handlers(current, 1); 26131da177e4SLinus Torvalds sigemptyset(¤t->blocked); 26149e7c8f8cSOleg Nesterov recalc_sigpending(); 26153bcac026SDavid Howells } 26161da177e4SLinus Torvalds spin_unlock_irq(¤t->sighand->siglock); 26171da177e4SLinus Torvalds } 26181da177e4SLinus Torvalds 2619a6f76f23SDavid Howells /* Wake up the parent if it is waiting so that it can recheck 2620a6f76f23SDavid Howells * wait permission to the new task SID. */ 2621ecd6de3cSOleg Nesterov read_lock(&tasklist_lock); 26220b7570e7SOleg Nesterov __wake_up_parent(current, current->real_parent); 2623ecd6de3cSOleg Nesterov read_unlock(&tasklist_lock); 26241da177e4SLinus Torvalds } 26251da177e4SLinus Torvalds 26261da177e4SLinus Torvalds /* superblock security operations */ 26271da177e4SLinus Torvalds 26281da177e4SLinus Torvalds static int selinux_sb_alloc_security(struct super_block *sb) 26291da177e4SLinus Torvalds { 26301da177e4SLinus Torvalds return superblock_alloc_security(sb); 26311da177e4SLinus Torvalds } 26321da177e4SLinus Torvalds 26331da177e4SLinus Torvalds static void selinux_sb_free_security(struct super_block *sb) 26341da177e4SLinus Torvalds { 26351da177e4SLinus Torvalds superblock_free_security(sb); 26361da177e4SLinus Torvalds } 26371da177e4SLinus Torvalds 26381da177e4SLinus Torvalds static inline int match_prefix(char *prefix, int plen, char *option, int olen) 26391da177e4SLinus Torvalds { 26401da177e4SLinus Torvalds if (plen > olen) 26411da177e4SLinus Torvalds return 0; 26421da177e4SLinus Torvalds 26431da177e4SLinus Torvalds return !memcmp(prefix, option, plen); 26441da177e4SLinus Torvalds } 26451da177e4SLinus Torvalds 26461da177e4SLinus Torvalds static inline int selinux_option(char *option, int len) 26471da177e4SLinus Torvalds { 2648832cbd9aSEric Paris return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) || 2649832cbd9aSEric Paris match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) || 2650832cbd9aSEric Paris match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) || 265111689d47SDavid P. Quigley match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) || 265211689d47SDavid P. Quigley match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len)); 26531da177e4SLinus Torvalds } 26541da177e4SLinus Torvalds 26551da177e4SLinus Torvalds static inline void take_option(char **to, char *from, int *first, int len) 26561da177e4SLinus Torvalds { 26571da177e4SLinus Torvalds if (!*first) { 26581da177e4SLinus Torvalds **to = ','; 26591da177e4SLinus Torvalds *to += 1; 26603528a953SCory Olmo } else 26611da177e4SLinus Torvalds *first = 0; 26621da177e4SLinus Torvalds memcpy(*to, from, len); 26631da177e4SLinus Torvalds *to += len; 26641da177e4SLinus Torvalds } 26651da177e4SLinus Torvalds 26663528a953SCory Olmo static inline void take_selinux_option(char **to, char *from, int *first, 26673528a953SCory Olmo int len) 26683528a953SCory Olmo { 26693528a953SCory Olmo int current_size = 0; 26703528a953SCory Olmo 26713528a953SCory Olmo if (!*first) { 26723528a953SCory Olmo **to = '|'; 26733528a953SCory Olmo *to += 1; 2674828dfe1dSEric Paris } else 26753528a953SCory Olmo *first = 0; 26763528a953SCory Olmo 26773528a953SCory Olmo while (current_size < len) { 26783528a953SCory Olmo if (*from != '"') { 26793528a953SCory Olmo **to = *from; 26803528a953SCory Olmo *to += 1; 26813528a953SCory Olmo } 26823528a953SCory Olmo from += 1; 26833528a953SCory Olmo current_size += 1; 26843528a953SCory Olmo } 26853528a953SCory Olmo } 26863528a953SCory Olmo 2687e0007529SEric Paris static int selinux_sb_copy_data(char *orig, char *copy) 26881da177e4SLinus Torvalds { 26891da177e4SLinus Torvalds int fnosec, fsec, rc = 0; 26901da177e4SLinus Torvalds char *in_save, *in_curr, *in_end; 26911da177e4SLinus Torvalds char *sec_curr, *nosec_save, *nosec; 26923528a953SCory Olmo int open_quote = 0; 26931da177e4SLinus Torvalds 26941da177e4SLinus Torvalds in_curr = orig; 26951da177e4SLinus Torvalds sec_curr = copy; 26961da177e4SLinus Torvalds 26971da177e4SLinus Torvalds nosec = (char *)get_zeroed_page(GFP_KERNEL); 26981da177e4SLinus Torvalds if (!nosec) { 26991da177e4SLinus Torvalds rc = -ENOMEM; 27001da177e4SLinus Torvalds goto out; 27011da177e4SLinus Torvalds } 27021da177e4SLinus Torvalds 27031da177e4SLinus Torvalds nosec_save = nosec; 27041da177e4SLinus Torvalds fnosec = fsec = 1; 27051da177e4SLinus Torvalds in_save = in_end = orig; 27061da177e4SLinus Torvalds 27071da177e4SLinus Torvalds do { 27083528a953SCory Olmo if (*in_end == '"') 27093528a953SCory Olmo open_quote = !open_quote; 27103528a953SCory Olmo if ((*in_end == ',' && open_quote == 0) || 27113528a953SCory Olmo *in_end == '\0') { 27121da177e4SLinus Torvalds int len = in_end - in_curr; 27131da177e4SLinus Torvalds 27141da177e4SLinus Torvalds if (selinux_option(in_curr, len)) 27153528a953SCory Olmo take_selinux_option(&sec_curr, in_curr, &fsec, len); 27161da177e4SLinus Torvalds else 27171da177e4SLinus Torvalds take_option(&nosec, in_curr, &fnosec, len); 27181da177e4SLinus Torvalds 27191da177e4SLinus Torvalds in_curr = in_end + 1; 27201da177e4SLinus Torvalds } 27211da177e4SLinus Torvalds } while (*in_end++); 27221da177e4SLinus Torvalds 27236931dfc9SEric Paris strcpy(in_save, nosec_save); 2724da3caa20SGerald Schaefer free_page((unsigned long)nosec_save); 27251da177e4SLinus Torvalds out: 27261da177e4SLinus Torvalds return rc; 27271da177e4SLinus Torvalds } 27281da177e4SLinus Torvalds 2729026eb167SEric Paris static int selinux_sb_remount(struct super_block *sb, void *data) 2730026eb167SEric Paris { 2731026eb167SEric Paris int rc, i, *flags; 2732026eb167SEric Paris struct security_mnt_opts opts; 2733026eb167SEric Paris char *secdata, **mount_options; 2734026eb167SEric Paris struct superblock_security_struct *sbsec = sb->s_security; 2735026eb167SEric Paris 2736026eb167SEric Paris if (!(sbsec->flags & SE_SBINITIALIZED)) 2737026eb167SEric Paris return 0; 2738026eb167SEric Paris 2739026eb167SEric Paris if (!data) 2740026eb167SEric Paris return 0; 2741026eb167SEric Paris 2742026eb167SEric Paris if (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA) 2743026eb167SEric Paris return 0; 2744026eb167SEric Paris 2745026eb167SEric Paris security_init_mnt_opts(&opts); 2746026eb167SEric Paris secdata = alloc_secdata(); 2747026eb167SEric Paris if (!secdata) 2748026eb167SEric Paris return -ENOMEM; 2749026eb167SEric Paris rc = selinux_sb_copy_data(data, secdata); 2750026eb167SEric Paris if (rc) 2751026eb167SEric Paris goto out_free_secdata; 2752026eb167SEric Paris 2753026eb167SEric Paris rc = selinux_parse_opts_str(secdata, &opts); 2754026eb167SEric Paris if (rc) 2755026eb167SEric Paris goto out_free_secdata; 2756026eb167SEric Paris 2757026eb167SEric Paris mount_options = opts.mnt_opts; 2758026eb167SEric Paris flags = opts.mnt_opts_flags; 2759026eb167SEric Paris 2760026eb167SEric Paris for (i = 0; i < opts.num_mnt_opts; i++) { 2761026eb167SEric Paris u32 sid; 2762026eb167SEric Paris 276312f348b9SEric Paris if (flags[i] == SBLABEL_MNT) 2764026eb167SEric Paris continue; 276544be2f65SRasmus Villemoes rc = security_context_str_to_sid(mount_options[i], &sid, GFP_KERNEL); 2766026eb167SEric Paris if (rc) { 276744be2f65SRasmus Villemoes printk(KERN_WARNING "SELinux: security_context_str_to_sid" 276829b1deb2SLinus Torvalds "(%s) failed for (dev %s, type %s) errno=%d\n", 276929b1deb2SLinus Torvalds mount_options[i], sb->s_id, sb->s_type->name, rc); 2770026eb167SEric Paris goto out_free_opts; 2771026eb167SEric Paris } 2772026eb167SEric Paris rc = -EINVAL; 2773026eb167SEric Paris switch (flags[i]) { 2774026eb167SEric Paris case FSCONTEXT_MNT: 2775026eb167SEric Paris if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid)) 2776026eb167SEric Paris goto out_bad_option; 2777026eb167SEric Paris break; 2778026eb167SEric Paris case CONTEXT_MNT: 2779026eb167SEric Paris if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid)) 2780026eb167SEric Paris goto out_bad_option; 2781026eb167SEric Paris break; 2782026eb167SEric Paris case ROOTCONTEXT_MNT: { 2783026eb167SEric Paris struct inode_security_struct *root_isec; 278483da53c5SAndreas Gruenbacher root_isec = backing_inode_security(sb->s_root); 2785026eb167SEric Paris 2786026eb167SEric Paris if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid)) 2787026eb167SEric Paris goto out_bad_option; 2788026eb167SEric Paris break; 2789026eb167SEric Paris } 2790026eb167SEric Paris case DEFCONTEXT_MNT: 2791026eb167SEric Paris if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid)) 2792026eb167SEric Paris goto out_bad_option; 2793026eb167SEric Paris break; 2794026eb167SEric Paris default: 2795026eb167SEric Paris goto out_free_opts; 2796026eb167SEric Paris } 2797026eb167SEric Paris } 2798026eb167SEric Paris 2799026eb167SEric Paris rc = 0; 2800026eb167SEric Paris out_free_opts: 2801026eb167SEric Paris security_free_mnt_opts(&opts); 2802026eb167SEric Paris out_free_secdata: 2803026eb167SEric Paris free_secdata(secdata); 2804026eb167SEric Paris return rc; 2805026eb167SEric Paris out_bad_option: 2806026eb167SEric Paris printk(KERN_WARNING "SELinux: unable to change security options " 280729b1deb2SLinus Torvalds "during remount (dev %s, type=%s)\n", sb->s_id, 280829b1deb2SLinus Torvalds sb->s_type->name); 2809026eb167SEric Paris goto out_free_opts; 2810026eb167SEric Paris } 2811026eb167SEric Paris 281212204e24SJames Morris static int selinux_sb_kern_mount(struct super_block *sb, int flags, void *data) 28131da177e4SLinus Torvalds { 281488e67f3bSDavid Howells const struct cred *cred = current_cred(); 28152bf49690SThomas Liu struct common_audit_data ad; 28161da177e4SLinus Torvalds int rc; 28171da177e4SLinus Torvalds 28181da177e4SLinus Torvalds rc = superblock_doinit(sb, data); 28191da177e4SLinus Torvalds if (rc) 28201da177e4SLinus Torvalds return rc; 28211da177e4SLinus Torvalds 282274192246SJames Morris /* Allow all mounts performed by the kernel */ 282374192246SJames Morris if (flags & MS_KERNMOUNT) 282474192246SJames Morris return 0; 282574192246SJames Morris 282650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 2827a269434dSEric Paris ad.u.dentry = sb->s_root; 282888e67f3bSDavid Howells return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad); 28291da177e4SLinus Torvalds } 28301da177e4SLinus Torvalds 2831726c3342SDavid Howells static int selinux_sb_statfs(struct dentry *dentry) 28321da177e4SLinus Torvalds { 283388e67f3bSDavid Howells const struct cred *cred = current_cred(); 28342bf49690SThomas Liu struct common_audit_data ad; 28351da177e4SLinus Torvalds 283650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 2837a269434dSEric Paris ad.u.dentry = dentry->d_sb->s_root; 283888e67f3bSDavid Howells return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad); 28391da177e4SLinus Torvalds } 28401da177e4SLinus Torvalds 2841808d4e3cSAl Viro static int selinux_mount(const char *dev_name, 28428a04c43bSAl Viro const struct path *path, 2843808d4e3cSAl Viro const char *type, 28441da177e4SLinus Torvalds unsigned long flags, 28451da177e4SLinus Torvalds void *data) 28461da177e4SLinus Torvalds { 284788e67f3bSDavid Howells const struct cred *cred = current_cred(); 28481da177e4SLinus Torvalds 28491da177e4SLinus Torvalds if (flags & MS_REMOUNT) 2850d8c9584eSAl Viro return superblock_has_perm(cred, path->dentry->d_sb, 28511da177e4SLinus Torvalds FILESYSTEM__REMOUNT, NULL); 28521da177e4SLinus Torvalds else 28532875fa00SEric Paris return path_has_perm(cred, path, FILE__MOUNTON); 28541da177e4SLinus Torvalds } 28551da177e4SLinus Torvalds 28561da177e4SLinus Torvalds static int selinux_umount(struct vfsmount *mnt, int flags) 28571da177e4SLinus Torvalds { 285888e67f3bSDavid Howells const struct cred *cred = current_cred(); 28591da177e4SLinus Torvalds 286088e67f3bSDavid Howells return superblock_has_perm(cred, mnt->mnt_sb, 28611da177e4SLinus Torvalds FILESYSTEM__UNMOUNT, NULL); 28621da177e4SLinus Torvalds } 28631da177e4SLinus Torvalds 28641da177e4SLinus Torvalds /* inode security operations */ 28651da177e4SLinus Torvalds 28661da177e4SLinus Torvalds static int selinux_inode_alloc_security(struct inode *inode) 28671da177e4SLinus Torvalds { 28681da177e4SLinus Torvalds return inode_alloc_security(inode); 28691da177e4SLinus Torvalds } 28701da177e4SLinus Torvalds 28711da177e4SLinus Torvalds static void selinux_inode_free_security(struct inode *inode) 28721da177e4SLinus Torvalds { 28731da177e4SLinus Torvalds inode_free_security(inode); 28741da177e4SLinus Torvalds } 28751da177e4SLinus Torvalds 2876d47be3dfSDavid Quigley static int selinux_dentry_init_security(struct dentry *dentry, int mode, 28774f3ccd76SAl Viro const struct qstr *name, void **ctx, 2878d47be3dfSDavid Quigley u32 *ctxlen) 2879d47be3dfSDavid Quigley { 2880d47be3dfSDavid Quigley u32 newsid; 2881d47be3dfSDavid Quigley int rc; 2882d47be3dfSDavid Quigley 2883c957f6dfSVivek Goyal rc = selinux_determine_inode_label(current_security(), 2884c957f6dfSVivek Goyal d_inode(dentry->d_parent), name, 2885d47be3dfSDavid Quigley inode_mode_to_security_class(mode), 2886d47be3dfSDavid Quigley &newsid); 2887c3c188b2SDavid Howells if (rc) 2888d47be3dfSDavid Quigley return rc; 2889d47be3dfSDavid Quigley 2890d47be3dfSDavid Quigley return security_sid_to_context(newsid, (char **)ctx, ctxlen); 2891d47be3dfSDavid Quigley } 2892d47be3dfSDavid Quigley 2893a518b0a5SVivek Goyal static int selinux_dentry_create_files_as(struct dentry *dentry, int mode, 2894a518b0a5SVivek Goyal struct qstr *name, 2895a518b0a5SVivek Goyal const struct cred *old, 2896a518b0a5SVivek Goyal struct cred *new) 2897a518b0a5SVivek Goyal { 2898a518b0a5SVivek Goyal u32 newsid; 2899a518b0a5SVivek Goyal int rc; 2900a518b0a5SVivek Goyal struct task_security_struct *tsec; 2901a518b0a5SVivek Goyal 2902a518b0a5SVivek Goyal rc = selinux_determine_inode_label(old->security, 2903a518b0a5SVivek Goyal d_inode(dentry->d_parent), name, 2904a518b0a5SVivek Goyal inode_mode_to_security_class(mode), 2905a518b0a5SVivek Goyal &newsid); 2906a518b0a5SVivek Goyal if (rc) 2907a518b0a5SVivek Goyal return rc; 2908a518b0a5SVivek Goyal 2909a518b0a5SVivek Goyal tsec = new->security; 2910a518b0a5SVivek Goyal tsec->create_sid = newsid; 2911a518b0a5SVivek Goyal return 0; 2912a518b0a5SVivek Goyal } 2913a518b0a5SVivek Goyal 29145e41ff9eSStephen Smalley static int selinux_inode_init_security(struct inode *inode, struct inode *dir, 29159548906bSTetsuo Handa const struct qstr *qstr, 29169548906bSTetsuo Handa const char **name, 29172a7dba39SEric Paris void **value, size_t *len) 29185e41ff9eSStephen Smalley { 29195fb49870SPaul Moore const struct task_security_struct *tsec = current_security(); 29205e41ff9eSStephen Smalley struct superblock_security_struct *sbsec; 2921c0d4f464SCorentin LABBE u32 newsid, clen; 29225e41ff9eSStephen Smalley int rc; 29239548906bSTetsuo Handa char *context; 29245e41ff9eSStephen Smalley 29255e41ff9eSStephen Smalley sbsec = dir->i_sb->s_security; 29265e41ff9eSStephen Smalley 29275e41ff9eSStephen Smalley newsid = tsec->create_sid; 2928275bb41eSDavid Howells 2929c957f6dfSVivek Goyal rc = selinux_determine_inode_label(current_security(), 2930c3c188b2SDavid Howells dir, qstr, 29315e41ff9eSStephen Smalley inode_mode_to_security_class(inode->i_mode), 2932c3c188b2SDavid Howells &newsid); 2933c3c188b2SDavid Howells if (rc) 29345e41ff9eSStephen Smalley return rc; 29355e41ff9eSStephen Smalley 2936296fddf7SEric Paris /* Possibly defer initialization to selinux_complete_init. */ 29370d90a7ecSDavid P. Quigley if (sbsec->flags & SE_SBINITIALIZED) { 2938296fddf7SEric Paris struct inode_security_struct *isec = inode->i_security; 2939296fddf7SEric Paris isec->sclass = inode_mode_to_security_class(inode->i_mode); 2940296fddf7SEric Paris isec->sid = newsid; 29416f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 2942296fddf7SEric Paris } 29435e41ff9eSStephen Smalley 294412f348b9SEric Paris if (!ss_initialized || !(sbsec->flags & SBLABEL_MNT)) 294525a74f3bSStephen Smalley return -EOPNOTSUPP; 294625a74f3bSStephen Smalley 29479548906bSTetsuo Handa if (name) 29489548906bSTetsuo Handa *name = XATTR_SELINUX_SUFFIX; 29495e41ff9eSStephen Smalley 2950570bc1c2SStephen Smalley if (value && len) { 295112b29f34SStephen Smalley rc = security_sid_to_context_force(newsid, &context, &clen); 29529548906bSTetsuo Handa if (rc) 29535e41ff9eSStephen Smalley return rc; 29545e41ff9eSStephen Smalley *value = context; 2955570bc1c2SStephen Smalley *len = clen; 2956570bc1c2SStephen Smalley } 29575e41ff9eSStephen Smalley 29585e41ff9eSStephen Smalley return 0; 29595e41ff9eSStephen Smalley } 29605e41ff9eSStephen Smalley 29614acdaf27SAl Viro static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode) 29621da177e4SLinus Torvalds { 29631da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_FILE); 29641da177e4SLinus Torvalds } 29651da177e4SLinus Torvalds 29661da177e4SLinus Torvalds static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 29671da177e4SLinus Torvalds { 29681da177e4SLinus Torvalds return may_link(dir, old_dentry, MAY_LINK); 29691da177e4SLinus Torvalds } 29701da177e4SLinus Torvalds 29711da177e4SLinus Torvalds static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry) 29721da177e4SLinus Torvalds { 29731da177e4SLinus Torvalds return may_link(dir, dentry, MAY_UNLINK); 29741da177e4SLinus Torvalds } 29751da177e4SLinus Torvalds 29761da177e4SLinus Torvalds static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name) 29771da177e4SLinus Torvalds { 29781da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_LNK_FILE); 29791da177e4SLinus Torvalds } 29801da177e4SLinus Torvalds 298118bb1db3SAl Viro static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask) 29821da177e4SLinus Torvalds { 29831da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_DIR); 29841da177e4SLinus Torvalds } 29851da177e4SLinus Torvalds 29861da177e4SLinus Torvalds static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry) 29871da177e4SLinus Torvalds { 29881da177e4SLinus Torvalds return may_link(dir, dentry, MAY_RMDIR); 29891da177e4SLinus Torvalds } 29901da177e4SLinus Torvalds 29911a67aafbSAl Viro static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) 29921da177e4SLinus Torvalds { 29931da177e4SLinus Torvalds return may_create(dir, dentry, inode_mode_to_security_class(mode)); 29941da177e4SLinus Torvalds } 29951da177e4SLinus Torvalds 29961da177e4SLinus Torvalds static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry, 29971da177e4SLinus Torvalds struct inode *new_inode, struct dentry *new_dentry) 29981da177e4SLinus Torvalds { 29991da177e4SLinus Torvalds return may_rename(old_inode, old_dentry, new_inode, new_dentry); 30001da177e4SLinus Torvalds } 30011da177e4SLinus Torvalds 30021da177e4SLinus Torvalds static int selinux_inode_readlink(struct dentry *dentry) 30031da177e4SLinus Torvalds { 300488e67f3bSDavid Howells const struct cred *cred = current_cred(); 300588e67f3bSDavid Howells 30062875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__READ); 30071da177e4SLinus Torvalds } 30081da177e4SLinus Torvalds 3009bda0be7aSNeilBrown static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode, 3010bda0be7aSNeilBrown bool rcu) 30111da177e4SLinus Torvalds { 301288e67f3bSDavid Howells const struct cred *cred = current_cred(); 3013bda0be7aSNeilBrown struct common_audit_data ad; 3014bda0be7aSNeilBrown struct inode_security_struct *isec; 3015bda0be7aSNeilBrown u32 sid; 30161da177e4SLinus Torvalds 3017bda0be7aSNeilBrown validate_creds(cred); 3018bda0be7aSNeilBrown 3019bda0be7aSNeilBrown ad.type = LSM_AUDIT_DATA_DENTRY; 3020bda0be7aSNeilBrown ad.u.dentry = dentry; 3021bda0be7aSNeilBrown sid = cred_sid(cred); 30225d226df4SAndreas Gruenbacher isec = inode_security_rcu(inode, rcu); 30235d226df4SAndreas Gruenbacher if (IS_ERR(isec)) 30245d226df4SAndreas Gruenbacher return PTR_ERR(isec); 3025bda0be7aSNeilBrown 3026bda0be7aSNeilBrown return avc_has_perm_flags(sid, isec->sid, isec->sclass, FILE__READ, &ad, 3027bda0be7aSNeilBrown rcu ? MAY_NOT_BLOCK : 0); 30281da177e4SLinus Torvalds } 30291da177e4SLinus Torvalds 3030d4cf970dSEric Paris static noinline int audit_inode_permission(struct inode *inode, 3031d4cf970dSEric Paris u32 perms, u32 audited, u32 denied, 3032626b9740SStephen Smalley int result, 3033d4cf970dSEric Paris unsigned flags) 3034d4cf970dSEric Paris { 3035d4cf970dSEric Paris struct common_audit_data ad; 3036d4cf970dSEric Paris struct inode_security_struct *isec = inode->i_security; 3037d4cf970dSEric Paris int rc; 3038d4cf970dSEric Paris 303950c205f5SEric Paris ad.type = LSM_AUDIT_DATA_INODE; 3040d4cf970dSEric Paris ad.u.inode = inode; 3041d4cf970dSEric Paris 3042d4cf970dSEric Paris rc = slow_avc_audit(current_sid(), isec->sid, isec->sclass, perms, 3043626b9740SStephen Smalley audited, denied, result, &ad, flags); 3044d4cf970dSEric Paris if (rc) 3045d4cf970dSEric Paris return rc; 3046d4cf970dSEric Paris return 0; 3047d4cf970dSEric Paris } 3048d4cf970dSEric Paris 3049e74f71ebSAl Viro static int selinux_inode_permission(struct inode *inode, int mask) 30501da177e4SLinus Torvalds { 305188e67f3bSDavid Howells const struct cred *cred = current_cred(); 3052b782e0a6SEric Paris u32 perms; 3053b782e0a6SEric Paris bool from_access; 3054cf1dd1daSAl Viro unsigned flags = mask & MAY_NOT_BLOCK; 30552e334057SEric Paris struct inode_security_struct *isec; 30562e334057SEric Paris u32 sid; 30572e334057SEric Paris struct av_decision avd; 30582e334057SEric Paris int rc, rc2; 30592e334057SEric Paris u32 audited, denied; 30601da177e4SLinus Torvalds 3061b782e0a6SEric Paris from_access = mask & MAY_ACCESS; 3062d09ca739SEric Paris mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND); 3063d09ca739SEric Paris 30641da177e4SLinus Torvalds /* No permission to check. Existence test. */ 3065b782e0a6SEric Paris if (!mask) 30661da177e4SLinus Torvalds return 0; 30671da177e4SLinus Torvalds 30682e334057SEric Paris validate_creds(cred); 3069b782e0a6SEric Paris 30702e334057SEric Paris if (unlikely(IS_PRIVATE(inode))) 30712e334057SEric Paris return 0; 3072b782e0a6SEric Paris 3073b782e0a6SEric Paris perms = file_mask_to_av(inode->i_mode, mask); 3074b782e0a6SEric Paris 30752e334057SEric Paris sid = cred_sid(cred); 30765d226df4SAndreas Gruenbacher isec = inode_security_rcu(inode, flags & MAY_NOT_BLOCK); 30775d226df4SAndreas Gruenbacher if (IS_ERR(isec)) 30785d226df4SAndreas Gruenbacher return PTR_ERR(isec); 30792e334057SEric Paris 30802e334057SEric Paris rc = avc_has_perm_noaudit(sid, isec->sid, isec->sclass, perms, 0, &avd); 30812e334057SEric Paris audited = avc_audit_required(perms, &avd, rc, 30822e334057SEric Paris from_access ? FILE__AUDIT_ACCESS : 0, 30832e334057SEric Paris &denied); 30842e334057SEric Paris if (likely(!audited)) 30852e334057SEric Paris return rc; 30862e334057SEric Paris 3087626b9740SStephen Smalley rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags); 30882e334057SEric Paris if (rc2) 30892e334057SEric Paris return rc2; 30902e334057SEric Paris return rc; 30911da177e4SLinus Torvalds } 30921da177e4SLinus Torvalds 30931da177e4SLinus Torvalds static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr) 30941da177e4SLinus Torvalds { 309588e67f3bSDavid Howells const struct cred *cred = current_cred(); 3096ccb54478SStephen Smalley struct inode *inode = d_backing_inode(dentry); 3097bc6a6008SAmerigo Wang unsigned int ia_valid = iattr->ia_valid; 309895dbf739SEric Paris __u32 av = FILE__WRITE; 30991da177e4SLinus Torvalds 3100bc6a6008SAmerigo Wang /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */ 3101bc6a6008SAmerigo Wang if (ia_valid & ATTR_FORCE) { 3102bc6a6008SAmerigo Wang ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE | 3103bc6a6008SAmerigo Wang ATTR_FORCE); 3104bc6a6008SAmerigo Wang if (!ia_valid) 31051da177e4SLinus Torvalds return 0; 3106bc6a6008SAmerigo Wang } 31071da177e4SLinus Torvalds 3108bc6a6008SAmerigo Wang if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | 3109bc6a6008SAmerigo Wang ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET)) 31102875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__SETATTR); 31111da177e4SLinus Torvalds 3112ccb54478SStephen Smalley if (selinux_policycap_openperm && 3113ccb54478SStephen Smalley inode->i_sb->s_magic != SOCKFS_MAGIC && 3114ccb54478SStephen Smalley (ia_valid & ATTR_SIZE) && 3115ccb54478SStephen Smalley !(ia_valid & ATTR_FILE)) 311695dbf739SEric Paris av |= FILE__OPEN; 311795dbf739SEric Paris 311895dbf739SEric Paris return dentry_has_perm(cred, dentry, av); 31191da177e4SLinus Torvalds } 31201da177e4SLinus Torvalds 31213f7036a0SAl Viro static int selinux_inode_getattr(const struct path *path) 31221da177e4SLinus Torvalds { 31233f7036a0SAl Viro return path_has_perm(current_cred(), path, FILE__GETATTR); 31241da177e4SLinus Torvalds } 31251da177e4SLinus Torvalds 3126db59000aSStephen Smalley static bool has_cap_mac_admin(bool audit) 3127db59000aSStephen Smalley { 3128db59000aSStephen Smalley const struct cred *cred = current_cred(); 3129db59000aSStephen Smalley int cap_audit = audit ? SECURITY_CAP_AUDIT : SECURITY_CAP_NOAUDIT; 3130db59000aSStephen Smalley 3131db59000aSStephen Smalley if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, cap_audit)) 3132db59000aSStephen Smalley return false; 3133db59000aSStephen Smalley if (cred_has_capability(cred, CAP_MAC_ADMIN, cap_audit, true)) 3134db59000aSStephen Smalley return false; 3135db59000aSStephen Smalley return true; 3136db59000aSStephen Smalley } 3137db59000aSStephen Smalley 31388f0cfa52SDavid Howells static int selinux_inode_setxattr(struct dentry *dentry, const char *name, 31398f0cfa52SDavid Howells const void *value, size_t size, int flags) 31401da177e4SLinus Torvalds { 3141c6f493d6SDavid Howells struct inode *inode = d_backing_inode(dentry); 314220cdef8dSPaul Moore struct inode_security_struct *isec; 31431da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 31442bf49690SThomas Liu struct common_audit_data ad; 3145275bb41eSDavid Howells u32 newsid, sid = current_sid(); 31461da177e4SLinus Torvalds int rc = 0; 31471da177e4SLinus Torvalds 31486b240306SEric W. Biederman if (strcmp(name, XATTR_NAME_SELINUX)) { 31496b240306SEric W. Biederman rc = cap_inode_setxattr(dentry, name, value, size, flags); 31506b240306SEric W. Biederman if (rc) 31516b240306SEric W. Biederman return rc; 31526b240306SEric W. Biederman 31536b240306SEric W. Biederman /* Not an attribute we recognize, so just check the 31546b240306SEric W. Biederman ordinary setattr permission. */ 31556b240306SEric W. Biederman return dentry_has_perm(current_cred(), dentry, FILE__SETATTR); 31566b240306SEric W. Biederman } 31571da177e4SLinus Torvalds 31581da177e4SLinus Torvalds sbsec = inode->i_sb->s_security; 315912f348b9SEric Paris if (!(sbsec->flags & SBLABEL_MNT)) 31601da177e4SLinus Torvalds return -EOPNOTSUPP; 31611da177e4SLinus Torvalds 31622e149670SSerge E. Hallyn if (!inode_owner_or_capable(inode)) 31631da177e4SLinus Torvalds return -EPERM; 31641da177e4SLinus Torvalds 316550c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 3166a269434dSEric Paris ad.u.dentry = dentry; 31671da177e4SLinus Torvalds 316820cdef8dSPaul Moore isec = backing_inode_security(dentry); 3169275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, isec->sclass, 31701da177e4SLinus Torvalds FILE__RELABELFROM, &ad); 31711da177e4SLinus Torvalds if (rc) 31721da177e4SLinus Torvalds return rc; 31731da177e4SLinus Torvalds 317452a4c640SNikolay Aleksandrov rc = security_context_to_sid(value, size, &newsid, GFP_KERNEL); 317512b29f34SStephen Smalley if (rc == -EINVAL) { 3176db59000aSStephen Smalley if (!has_cap_mac_admin(true)) { 3177d6ea83ecSEric Paris struct audit_buffer *ab; 3178d6ea83ecSEric Paris size_t audit_size; 3179d6ea83ecSEric Paris 3180d6ea83ecSEric Paris /* We strip a nul only if it is at the end, otherwise the 3181d6ea83ecSEric Paris * context contains a nul and we should audit that */ 3182e3fea3f7SAl Viro if (value) { 3183*add24372SColin Ian King const char *str = value; 3184*add24372SColin Ian King 3185d6ea83ecSEric Paris if (str[size - 1] == '\0') 3186d6ea83ecSEric Paris audit_size = size - 1; 3187d6ea83ecSEric Paris else 3188d6ea83ecSEric Paris audit_size = size; 3189e3fea3f7SAl Viro } else { 3190e3fea3f7SAl Viro audit_size = 0; 3191e3fea3f7SAl Viro } 3192d6ea83ecSEric Paris ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR); 3193d6ea83ecSEric Paris audit_log_format(ab, "op=setxattr invalid_context="); 3194d6ea83ecSEric Paris audit_log_n_untrustedstring(ab, value, audit_size); 3195d6ea83ecSEric Paris audit_log_end(ab); 3196d6ea83ecSEric Paris 319712b29f34SStephen Smalley return rc; 3198d6ea83ecSEric Paris } 319912b29f34SStephen Smalley rc = security_context_to_sid_force(value, size, &newsid); 320012b29f34SStephen Smalley } 32011da177e4SLinus Torvalds if (rc) 32021da177e4SLinus Torvalds return rc; 32031da177e4SLinus Torvalds 3204275bb41eSDavid Howells rc = avc_has_perm(sid, newsid, isec->sclass, 32051da177e4SLinus Torvalds FILE__RELABELTO, &ad); 32061da177e4SLinus Torvalds if (rc) 32071da177e4SLinus Torvalds return rc; 32081da177e4SLinus Torvalds 3209275bb41eSDavid Howells rc = security_validate_transition(isec->sid, newsid, sid, 32101da177e4SLinus Torvalds isec->sclass); 32111da177e4SLinus Torvalds if (rc) 32121da177e4SLinus Torvalds return rc; 32131da177e4SLinus Torvalds 32141da177e4SLinus Torvalds return avc_has_perm(newsid, 32151da177e4SLinus Torvalds sbsec->sid, 32161da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 32171da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, 32181da177e4SLinus Torvalds &ad); 32191da177e4SLinus Torvalds } 32201da177e4SLinus Torvalds 32218f0cfa52SDavid Howells static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name, 32228f0cfa52SDavid Howells const void *value, size_t size, 32238f0cfa52SDavid Howells int flags) 32241da177e4SLinus Torvalds { 3225c6f493d6SDavid Howells struct inode *inode = d_backing_inode(dentry); 322620cdef8dSPaul Moore struct inode_security_struct *isec; 32271da177e4SLinus Torvalds u32 newsid; 32281da177e4SLinus Torvalds int rc; 32291da177e4SLinus Torvalds 32301da177e4SLinus Torvalds if (strcmp(name, XATTR_NAME_SELINUX)) { 32311da177e4SLinus Torvalds /* Not an attribute we recognize, so nothing to do. */ 32321da177e4SLinus Torvalds return; 32331da177e4SLinus Torvalds } 32341da177e4SLinus Torvalds 323512b29f34SStephen Smalley rc = security_context_to_sid_force(value, size, &newsid); 32361da177e4SLinus Torvalds if (rc) { 323712b29f34SStephen Smalley printk(KERN_ERR "SELinux: unable to map context to SID" 323812b29f34SStephen Smalley "for (%s, %lu), rc=%d\n", 323912b29f34SStephen Smalley inode->i_sb->s_id, inode->i_ino, -rc); 32401da177e4SLinus Torvalds return; 32411da177e4SLinus Torvalds } 32421da177e4SLinus Torvalds 324320cdef8dSPaul Moore isec = backing_inode_security(dentry); 32449287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 3245aa9c2669SDavid Quigley isec->sclass = inode_mode_to_security_class(inode->i_mode); 32461da177e4SLinus Torvalds isec->sid = newsid; 32476f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 32489287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 3249aa9c2669SDavid Quigley 32501da177e4SLinus Torvalds return; 32511da177e4SLinus Torvalds } 32521da177e4SLinus Torvalds 32538f0cfa52SDavid Howells static int selinux_inode_getxattr(struct dentry *dentry, const char *name) 32541da177e4SLinus Torvalds { 325588e67f3bSDavid Howells const struct cred *cred = current_cred(); 325688e67f3bSDavid Howells 32572875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__GETATTR); 32581da177e4SLinus Torvalds } 32591da177e4SLinus Torvalds 32601da177e4SLinus Torvalds static int selinux_inode_listxattr(struct dentry *dentry) 32611da177e4SLinus Torvalds { 326288e67f3bSDavid Howells const struct cred *cred = current_cred(); 326388e67f3bSDavid Howells 32642875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__GETATTR); 32651da177e4SLinus Torvalds } 32661da177e4SLinus Torvalds 32678f0cfa52SDavid Howells static int selinux_inode_removexattr(struct dentry *dentry, const char *name) 32681da177e4SLinus Torvalds { 32696b240306SEric W. Biederman if (strcmp(name, XATTR_NAME_SELINUX)) { 32706b240306SEric W. Biederman int rc = cap_inode_removexattr(dentry, name); 32716b240306SEric W. Biederman if (rc) 32726b240306SEric W. Biederman return rc; 32736b240306SEric W. Biederman 32746b240306SEric W. Biederman /* Not an attribute we recognize, so just check the 32756b240306SEric W. Biederman ordinary setattr permission. */ 32766b240306SEric W. Biederman return dentry_has_perm(current_cred(), dentry, FILE__SETATTR); 32776b240306SEric W. Biederman } 32781da177e4SLinus Torvalds 32791da177e4SLinus Torvalds /* No one is allowed to remove a SELinux security label. 32801da177e4SLinus Torvalds You can change the label, but all data must be labeled. */ 32811da177e4SLinus Torvalds return -EACCES; 32821da177e4SLinus Torvalds } 32831da177e4SLinus Torvalds 3284d381d8a9SJames Morris /* 3285abc69bb6SStephen Smalley * Copy the inode security context value to the user. 3286d381d8a9SJames Morris * 3287d381d8a9SJames Morris * Permission check is handled by selinux_inode_getxattr hook. 3288d381d8a9SJames Morris */ 3289ea861dfdSAndreas Gruenbacher static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc) 32901da177e4SLinus Torvalds { 329142492594SDavid P. Quigley u32 size; 329242492594SDavid P. Quigley int error; 329342492594SDavid P. Quigley char *context = NULL; 329420cdef8dSPaul Moore struct inode_security_struct *isec; 32951da177e4SLinus Torvalds 32968c8570fbSDustin Kirkland if (strcmp(name, XATTR_SELINUX_SUFFIX)) 32978c8570fbSDustin Kirkland return -EOPNOTSUPP; 32981da177e4SLinus Torvalds 3299abc69bb6SStephen Smalley /* 3300abc69bb6SStephen Smalley * If the caller has CAP_MAC_ADMIN, then get the raw context 3301abc69bb6SStephen Smalley * value even if it is not defined by current policy; otherwise, 3302abc69bb6SStephen Smalley * use the in-core value under current policy. 3303abc69bb6SStephen Smalley * Use the non-auditing forms of the permission checks since 3304abc69bb6SStephen Smalley * getxattr may be called by unprivileged processes commonly 3305abc69bb6SStephen Smalley * and lack of permission just means that we fall back to the 3306abc69bb6SStephen Smalley * in-core context value, not a denial. 3307abc69bb6SStephen Smalley */ 330820cdef8dSPaul Moore isec = inode_security(inode); 3309db59000aSStephen Smalley if (has_cap_mac_admin(false)) 3310abc69bb6SStephen Smalley error = security_sid_to_context_force(isec->sid, &context, 3311abc69bb6SStephen Smalley &size); 3312abc69bb6SStephen Smalley else 331342492594SDavid P. Quigley error = security_sid_to_context(isec->sid, &context, &size); 331442492594SDavid P. Quigley if (error) 331542492594SDavid P. Quigley return error; 331642492594SDavid P. Quigley error = size; 331742492594SDavid P. Quigley if (alloc) { 331842492594SDavid P. Quigley *buffer = context; 331942492594SDavid P. Quigley goto out_nofree; 332042492594SDavid P. Quigley } 332142492594SDavid P. Quigley kfree(context); 332242492594SDavid P. Quigley out_nofree: 332342492594SDavid P. Quigley return error; 33241da177e4SLinus Torvalds } 33251da177e4SLinus Torvalds 33261da177e4SLinus Torvalds static int selinux_inode_setsecurity(struct inode *inode, const char *name, 33271da177e4SLinus Torvalds const void *value, size_t size, int flags) 33281da177e4SLinus Torvalds { 33292c97165bSPaul Moore struct inode_security_struct *isec = inode_security_novalidate(inode); 33301da177e4SLinus Torvalds u32 newsid; 33311da177e4SLinus Torvalds int rc; 33321da177e4SLinus Torvalds 33331da177e4SLinus Torvalds if (strcmp(name, XATTR_SELINUX_SUFFIX)) 33341da177e4SLinus Torvalds return -EOPNOTSUPP; 33351da177e4SLinus Torvalds 33361da177e4SLinus Torvalds if (!value || !size) 33371da177e4SLinus Torvalds return -EACCES; 33381da177e4SLinus Torvalds 333920ba96aeSRasmus Villemoes rc = security_context_to_sid(value, size, &newsid, GFP_KERNEL); 33401da177e4SLinus Torvalds if (rc) 33411da177e4SLinus Torvalds return rc; 33421da177e4SLinus Torvalds 33439287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 3344aa9c2669SDavid Quigley isec->sclass = inode_mode_to_security_class(inode->i_mode); 33451da177e4SLinus Torvalds isec->sid = newsid; 33466f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 33479287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 33481da177e4SLinus Torvalds return 0; 33491da177e4SLinus Torvalds } 33501da177e4SLinus Torvalds 33511da177e4SLinus Torvalds static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size) 33521da177e4SLinus Torvalds { 33531da177e4SLinus Torvalds const int len = sizeof(XATTR_NAME_SELINUX); 33541da177e4SLinus Torvalds if (buffer && len <= buffer_size) 33551da177e4SLinus Torvalds memcpy(buffer, XATTR_NAME_SELINUX, len); 33561da177e4SLinus Torvalds return len; 33571da177e4SLinus Torvalds } 33581da177e4SLinus Torvalds 3359d6335d77SAndreas Gruenbacher static void selinux_inode_getsecid(struct inode *inode, u32 *secid) 3360713a04aeSAhmed S. Darwish { 3361e817c2f3SAndreas Gruenbacher struct inode_security_struct *isec = inode_security_novalidate(inode); 3362713a04aeSAhmed S. Darwish *secid = isec->sid; 3363713a04aeSAhmed S. Darwish } 3364713a04aeSAhmed S. Darwish 336556909eb3SVivek Goyal static int selinux_inode_copy_up(struct dentry *src, struct cred **new) 336656909eb3SVivek Goyal { 336756909eb3SVivek Goyal u32 sid; 336856909eb3SVivek Goyal struct task_security_struct *tsec; 336956909eb3SVivek Goyal struct cred *new_creds = *new; 337056909eb3SVivek Goyal 337156909eb3SVivek Goyal if (new_creds == NULL) { 337256909eb3SVivek Goyal new_creds = prepare_creds(); 337356909eb3SVivek Goyal if (!new_creds) 337456909eb3SVivek Goyal return -ENOMEM; 337556909eb3SVivek Goyal } 337656909eb3SVivek Goyal 337756909eb3SVivek Goyal tsec = new_creds->security; 337856909eb3SVivek Goyal /* Get label from overlay inode and set it in create_sid */ 337956909eb3SVivek Goyal selinux_inode_getsecid(d_inode(src), &sid); 338056909eb3SVivek Goyal tsec->create_sid = sid; 338156909eb3SVivek Goyal *new = new_creds; 338256909eb3SVivek Goyal return 0; 338356909eb3SVivek Goyal } 338456909eb3SVivek Goyal 338519472b69SVivek Goyal static int selinux_inode_copy_up_xattr(const char *name) 338619472b69SVivek Goyal { 338719472b69SVivek Goyal /* The copy_up hook above sets the initial context on an inode, but we 338819472b69SVivek Goyal * don't then want to overwrite it by blindly copying all the lower 338919472b69SVivek Goyal * xattrs up. Instead, we have to filter out SELinux-related xattrs. 339019472b69SVivek Goyal */ 339119472b69SVivek Goyal if (strcmp(name, XATTR_NAME_SELINUX) == 0) 339219472b69SVivek Goyal return 1; /* Discard */ 339319472b69SVivek Goyal /* 339419472b69SVivek Goyal * Any other attribute apart from SELINUX is not claimed, supported 339519472b69SVivek Goyal * by selinux. 339619472b69SVivek Goyal */ 339719472b69SVivek Goyal return -EOPNOTSUPP; 339819472b69SVivek Goyal } 339919472b69SVivek Goyal 34001da177e4SLinus Torvalds /* file security operations */ 34011da177e4SLinus Torvalds 3402788e7dd4SYuichi Nakamura static int selinux_revalidate_file_permission(struct file *file, int mask) 34031da177e4SLinus Torvalds { 340488e67f3bSDavid Howells const struct cred *cred = current_cred(); 3405496ad9aaSAl Viro struct inode *inode = file_inode(file); 34061da177e4SLinus Torvalds 34071da177e4SLinus Torvalds /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */ 34081da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE)) 34091da177e4SLinus Torvalds mask |= MAY_APPEND; 34101da177e4SLinus Torvalds 3411389fb800SPaul Moore return file_has_perm(cred, file, 34121da177e4SLinus Torvalds file_mask_to_av(inode->i_mode, mask)); 34131da177e4SLinus Torvalds } 34141da177e4SLinus Torvalds 3415788e7dd4SYuichi Nakamura static int selinux_file_permission(struct file *file, int mask) 3416788e7dd4SYuichi Nakamura { 3417496ad9aaSAl Viro struct inode *inode = file_inode(file); 341820dda18bSStephen Smalley struct file_security_struct *fsec = file->f_security; 3419b197367eSAndreas Gruenbacher struct inode_security_struct *isec; 342020dda18bSStephen Smalley u32 sid = current_sid(); 342120dda18bSStephen Smalley 3422389fb800SPaul Moore if (!mask) 3423788e7dd4SYuichi Nakamura /* No permission to check. Existence test. */ 3424788e7dd4SYuichi Nakamura return 0; 3425788e7dd4SYuichi Nakamura 3426b197367eSAndreas Gruenbacher isec = inode_security(inode); 342720dda18bSStephen Smalley if (sid == fsec->sid && fsec->isid == isec->sid && 342820dda18bSStephen Smalley fsec->pseqno == avc_policy_seqno()) 342983d49856SEric Paris /* No change since file_open check. */ 343020dda18bSStephen Smalley return 0; 343120dda18bSStephen Smalley 3432788e7dd4SYuichi Nakamura return selinux_revalidate_file_permission(file, mask); 3433788e7dd4SYuichi Nakamura } 3434788e7dd4SYuichi Nakamura 34351da177e4SLinus Torvalds static int selinux_file_alloc_security(struct file *file) 34361da177e4SLinus Torvalds { 34371da177e4SLinus Torvalds return file_alloc_security(file); 34381da177e4SLinus Torvalds } 34391da177e4SLinus Torvalds 34401da177e4SLinus Torvalds static void selinux_file_free_security(struct file *file) 34411da177e4SLinus Torvalds { 34421da177e4SLinus Torvalds file_free_security(file); 34431da177e4SLinus Torvalds } 34441da177e4SLinus Torvalds 3445fa1aa143SJeff Vander Stoep /* 3446fa1aa143SJeff Vander Stoep * Check whether a task has the ioctl permission and cmd 3447fa1aa143SJeff Vander Stoep * operation to an inode. 3448fa1aa143SJeff Vander Stoep */ 34491d2a168aSGeliang Tang static int ioctl_has_perm(const struct cred *cred, struct file *file, 3450fa1aa143SJeff Vander Stoep u32 requested, u16 cmd) 3451fa1aa143SJeff Vander Stoep { 3452fa1aa143SJeff Vander Stoep struct common_audit_data ad; 3453fa1aa143SJeff Vander Stoep struct file_security_struct *fsec = file->f_security; 3454fa1aa143SJeff Vander Stoep struct inode *inode = file_inode(file); 345520cdef8dSPaul Moore struct inode_security_struct *isec; 3456fa1aa143SJeff Vander Stoep struct lsm_ioctlop_audit ioctl; 3457fa1aa143SJeff Vander Stoep u32 ssid = cred_sid(cred); 3458fa1aa143SJeff Vander Stoep int rc; 3459fa1aa143SJeff Vander Stoep u8 driver = cmd >> 8; 3460fa1aa143SJeff Vander Stoep u8 xperm = cmd & 0xff; 3461fa1aa143SJeff Vander Stoep 3462fa1aa143SJeff Vander Stoep ad.type = LSM_AUDIT_DATA_IOCTL_OP; 3463fa1aa143SJeff Vander Stoep ad.u.op = &ioctl; 3464fa1aa143SJeff Vander Stoep ad.u.op->cmd = cmd; 3465fa1aa143SJeff Vander Stoep ad.u.op->path = file->f_path; 3466fa1aa143SJeff Vander Stoep 3467fa1aa143SJeff Vander Stoep if (ssid != fsec->sid) { 3468fa1aa143SJeff Vander Stoep rc = avc_has_perm(ssid, fsec->sid, 3469fa1aa143SJeff Vander Stoep SECCLASS_FD, 3470fa1aa143SJeff Vander Stoep FD__USE, 3471fa1aa143SJeff Vander Stoep &ad); 3472fa1aa143SJeff Vander Stoep if (rc) 3473fa1aa143SJeff Vander Stoep goto out; 3474fa1aa143SJeff Vander Stoep } 3475fa1aa143SJeff Vander Stoep 3476fa1aa143SJeff Vander Stoep if (unlikely(IS_PRIVATE(inode))) 3477fa1aa143SJeff Vander Stoep return 0; 3478fa1aa143SJeff Vander Stoep 347920cdef8dSPaul Moore isec = inode_security(inode); 3480fa1aa143SJeff Vander Stoep rc = avc_has_extended_perms(ssid, isec->sid, isec->sclass, 3481fa1aa143SJeff Vander Stoep requested, driver, xperm, &ad); 3482fa1aa143SJeff Vander Stoep out: 3483fa1aa143SJeff Vander Stoep return rc; 3484fa1aa143SJeff Vander Stoep } 3485fa1aa143SJeff Vander Stoep 34861da177e4SLinus Torvalds static int selinux_file_ioctl(struct file *file, unsigned int cmd, 34871da177e4SLinus Torvalds unsigned long arg) 34881da177e4SLinus Torvalds { 348988e67f3bSDavid Howells const struct cred *cred = current_cred(); 34900b24dcb7SEric Paris int error = 0; 34911da177e4SLinus Torvalds 34920b24dcb7SEric Paris switch (cmd) { 34930b24dcb7SEric Paris case FIONREAD: 34940b24dcb7SEric Paris /* fall through */ 34950b24dcb7SEric Paris case FIBMAP: 34960b24dcb7SEric Paris /* fall through */ 34970b24dcb7SEric Paris case FIGETBSZ: 34980b24dcb7SEric Paris /* fall through */ 34992f99c369SAl Viro case FS_IOC_GETFLAGS: 35000b24dcb7SEric Paris /* fall through */ 35012f99c369SAl Viro case FS_IOC_GETVERSION: 35020b24dcb7SEric Paris error = file_has_perm(cred, file, FILE__GETATTR); 35030b24dcb7SEric Paris break; 35041da177e4SLinus Torvalds 35052f99c369SAl Viro case FS_IOC_SETFLAGS: 35060b24dcb7SEric Paris /* fall through */ 35072f99c369SAl Viro case FS_IOC_SETVERSION: 35080b24dcb7SEric Paris error = file_has_perm(cred, file, FILE__SETATTR); 35090b24dcb7SEric Paris break; 35100b24dcb7SEric Paris 35110b24dcb7SEric Paris /* sys_ioctl() checks */ 35120b24dcb7SEric Paris case FIONBIO: 35130b24dcb7SEric Paris /* fall through */ 35140b24dcb7SEric Paris case FIOASYNC: 35150b24dcb7SEric Paris error = file_has_perm(cred, file, 0); 35160b24dcb7SEric Paris break; 35170b24dcb7SEric Paris 35180b24dcb7SEric Paris case KDSKBENT: 35190b24dcb7SEric Paris case KDSKBSENT: 35206a9de491SEric Paris error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG, 35218e4ff6f2SStephen Smalley SECURITY_CAP_AUDIT, true); 35220b24dcb7SEric Paris break; 35230b24dcb7SEric Paris 35240b24dcb7SEric Paris /* default case assumes that the command will go 35250b24dcb7SEric Paris * to the file's ioctl() function. 35260b24dcb7SEric Paris */ 35270b24dcb7SEric Paris default: 3528fa1aa143SJeff Vander Stoep error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd); 35290b24dcb7SEric Paris } 35300b24dcb7SEric Paris return error; 35311da177e4SLinus Torvalds } 35321da177e4SLinus Torvalds 3533fcaaade1SStephen Smalley static int default_noexec; 3534fcaaade1SStephen Smalley 35351da177e4SLinus Torvalds static int file_map_prot_check(struct file *file, unsigned long prot, int shared) 35361da177e4SLinus Torvalds { 353788e67f3bSDavid Howells const struct cred *cred = current_cred(); 3538be0554c9SStephen Smalley u32 sid = cred_sid(cred); 3539d84f4f99SDavid Howells int rc = 0; 354088e67f3bSDavid Howells 3541fcaaade1SStephen Smalley if (default_noexec && 3542892e8cacSStephen Smalley (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) || 3543892e8cacSStephen Smalley (!shared && (prot & PROT_WRITE)))) { 35441da177e4SLinus Torvalds /* 35451da177e4SLinus Torvalds * We are making executable an anonymous mapping or a 35461da177e4SLinus Torvalds * private file mapping that will also be writable. 35471da177e4SLinus Torvalds * This has an additional check. 35481da177e4SLinus Torvalds */ 3549be0554c9SStephen Smalley rc = avc_has_perm(sid, sid, SECCLASS_PROCESS, 3550be0554c9SStephen Smalley PROCESS__EXECMEM, NULL); 35511da177e4SLinus Torvalds if (rc) 3552d84f4f99SDavid Howells goto error; 35531da177e4SLinus Torvalds } 35541da177e4SLinus Torvalds 35551da177e4SLinus Torvalds if (file) { 35561da177e4SLinus Torvalds /* read access is always possible with a mapping */ 35571da177e4SLinus Torvalds u32 av = FILE__READ; 35581da177e4SLinus Torvalds 35591da177e4SLinus Torvalds /* write access only matters if the mapping is shared */ 35601da177e4SLinus Torvalds if (shared && (prot & PROT_WRITE)) 35611da177e4SLinus Torvalds av |= FILE__WRITE; 35621da177e4SLinus Torvalds 35631da177e4SLinus Torvalds if (prot & PROT_EXEC) 35641da177e4SLinus Torvalds av |= FILE__EXECUTE; 35651da177e4SLinus Torvalds 356688e67f3bSDavid Howells return file_has_perm(cred, file, av); 35671da177e4SLinus Torvalds } 3568d84f4f99SDavid Howells 3569d84f4f99SDavid Howells error: 3570d84f4f99SDavid Howells return rc; 35711da177e4SLinus Torvalds } 35721da177e4SLinus Torvalds 3573e5467859SAl Viro static int selinux_mmap_addr(unsigned long addr) 35741da177e4SLinus Torvalds { 3575b1d9e6b0SCasey Schaufler int rc = 0; 357698883bfdSPaul Moore 357798883bfdSPaul Moore if (addr < CONFIG_LSM_MMAP_MIN_ADDR) { 357898883bfdSPaul Moore u32 sid = current_sid(); 357998883bfdSPaul Moore rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT, 358098883bfdSPaul Moore MEMPROTECT__MMAP_ZERO, NULL); 358198883bfdSPaul Moore } 358298883bfdSPaul Moore 358398883bfdSPaul Moore return rc; 3584e5467859SAl Viro } 35851da177e4SLinus Torvalds 3586e5467859SAl Viro static int selinux_mmap_file(struct file *file, unsigned long reqprot, 3587e5467859SAl Viro unsigned long prot, unsigned long flags) 3588e5467859SAl Viro { 35893ba4bf5fSStephen Smalley struct common_audit_data ad; 35903ba4bf5fSStephen Smalley int rc; 35913ba4bf5fSStephen Smalley 35923ba4bf5fSStephen Smalley if (file) { 35933ba4bf5fSStephen Smalley ad.type = LSM_AUDIT_DATA_FILE; 35943ba4bf5fSStephen Smalley ad.u.file = file; 35953ba4bf5fSStephen Smalley rc = inode_has_perm(current_cred(), file_inode(file), 35963ba4bf5fSStephen Smalley FILE__MAP, &ad); 35973ba4bf5fSStephen Smalley if (rc) 35983ba4bf5fSStephen Smalley return rc; 35993ba4bf5fSStephen Smalley } 36003ba4bf5fSStephen Smalley 36011da177e4SLinus Torvalds if (selinux_checkreqprot) 36021da177e4SLinus Torvalds prot = reqprot; 36031da177e4SLinus Torvalds 36041da177e4SLinus Torvalds return file_map_prot_check(file, prot, 36051da177e4SLinus Torvalds (flags & MAP_TYPE) == MAP_SHARED); 36061da177e4SLinus Torvalds } 36071da177e4SLinus Torvalds 36081da177e4SLinus Torvalds static int selinux_file_mprotect(struct vm_area_struct *vma, 36091da177e4SLinus Torvalds unsigned long reqprot, 36101da177e4SLinus Torvalds unsigned long prot) 36111da177e4SLinus Torvalds { 361288e67f3bSDavid Howells const struct cred *cred = current_cred(); 3613be0554c9SStephen Smalley u32 sid = cred_sid(cred); 36141da177e4SLinus Torvalds 36151da177e4SLinus Torvalds if (selinux_checkreqprot) 36161da177e4SLinus Torvalds prot = reqprot; 36171da177e4SLinus Torvalds 3618fcaaade1SStephen Smalley if (default_noexec && 3619fcaaade1SStephen Smalley (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) { 3620d541bbeeSJames Morris int rc = 0; 3621db4c9641SStephen Smalley if (vma->vm_start >= vma->vm_mm->start_brk && 3622db4c9641SStephen Smalley vma->vm_end <= vma->vm_mm->brk) { 3623be0554c9SStephen Smalley rc = avc_has_perm(sid, sid, SECCLASS_PROCESS, 3624be0554c9SStephen Smalley PROCESS__EXECHEAP, NULL); 3625db4c9641SStephen Smalley } else if (!vma->vm_file && 3626c2316dbfSStephen Smalley ((vma->vm_start <= vma->vm_mm->start_stack && 3627c2316dbfSStephen Smalley vma->vm_end >= vma->vm_mm->start_stack) || 3628d17af505SAndy Lutomirski vma_is_stack_for_current(vma))) { 3629be0554c9SStephen Smalley rc = avc_has_perm(sid, sid, SECCLASS_PROCESS, 3630be0554c9SStephen Smalley PROCESS__EXECSTACK, NULL); 3631db4c9641SStephen Smalley } else if (vma->vm_file && vma->anon_vma) { 3632db4c9641SStephen Smalley /* 3633db4c9641SStephen Smalley * We are making executable a file mapping that has 3634db4c9641SStephen Smalley * had some COW done. Since pages might have been 3635db4c9641SStephen Smalley * written, check ability to execute the possibly 3636db4c9641SStephen Smalley * modified content. This typically should only 3637db4c9641SStephen Smalley * occur for text relocations. 3638db4c9641SStephen Smalley */ 3639d84f4f99SDavid Howells rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD); 3640db4c9641SStephen Smalley } 36416b992197SLorenzo Hernandez García-Hierro if (rc) 36426b992197SLorenzo Hernandez García-Hierro return rc; 36436b992197SLorenzo Hernandez García-Hierro } 36441da177e4SLinus Torvalds 36451da177e4SLinus Torvalds return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED); 36461da177e4SLinus Torvalds } 36471da177e4SLinus Torvalds 36481da177e4SLinus Torvalds static int selinux_file_lock(struct file *file, unsigned int cmd) 36491da177e4SLinus Torvalds { 365088e67f3bSDavid Howells const struct cred *cred = current_cred(); 365188e67f3bSDavid Howells 365288e67f3bSDavid Howells return file_has_perm(cred, file, FILE__LOCK); 36531da177e4SLinus Torvalds } 36541da177e4SLinus Torvalds 36551da177e4SLinus Torvalds static int selinux_file_fcntl(struct file *file, unsigned int cmd, 36561da177e4SLinus Torvalds unsigned long arg) 36571da177e4SLinus Torvalds { 365888e67f3bSDavid Howells const struct cred *cred = current_cred(); 36591da177e4SLinus Torvalds int err = 0; 36601da177e4SLinus Torvalds 36611da177e4SLinus Torvalds switch (cmd) { 36621da177e4SLinus Torvalds case F_SETFL: 36631da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) { 366488e67f3bSDavid Howells err = file_has_perm(cred, file, FILE__WRITE); 36651da177e4SLinus Torvalds break; 36661da177e4SLinus Torvalds } 36671da177e4SLinus Torvalds /* fall through */ 36681da177e4SLinus Torvalds case F_SETOWN: 36691da177e4SLinus Torvalds case F_SETSIG: 36701da177e4SLinus Torvalds case F_GETFL: 36711da177e4SLinus Torvalds case F_GETOWN: 36721da177e4SLinus Torvalds case F_GETSIG: 36731d151c33SCyrill Gorcunov case F_GETOWNER_UIDS: 36741da177e4SLinus Torvalds /* Just check FD__USE permission */ 367588e67f3bSDavid Howells err = file_has_perm(cred, file, 0); 36761da177e4SLinus Torvalds break; 36771da177e4SLinus Torvalds case F_GETLK: 36781da177e4SLinus Torvalds case F_SETLK: 36791da177e4SLinus Torvalds case F_SETLKW: 36800d3f7a2dSJeff Layton case F_OFD_GETLK: 36810d3f7a2dSJeff Layton case F_OFD_SETLK: 36820d3f7a2dSJeff Layton case F_OFD_SETLKW: 36831da177e4SLinus Torvalds #if BITS_PER_LONG == 32 36841da177e4SLinus Torvalds case F_GETLK64: 36851da177e4SLinus Torvalds case F_SETLK64: 36861da177e4SLinus Torvalds case F_SETLKW64: 36871da177e4SLinus Torvalds #endif 368888e67f3bSDavid Howells err = file_has_perm(cred, file, FILE__LOCK); 36891da177e4SLinus Torvalds break; 36901da177e4SLinus Torvalds } 36911da177e4SLinus Torvalds 36921da177e4SLinus Torvalds return err; 36931da177e4SLinus Torvalds } 36941da177e4SLinus Torvalds 3695e0b93eddSJeff Layton static void selinux_file_set_fowner(struct file *file) 36961da177e4SLinus Torvalds { 36971da177e4SLinus Torvalds struct file_security_struct *fsec; 36981da177e4SLinus Torvalds 36991da177e4SLinus Torvalds fsec = file->f_security; 3700275bb41eSDavid Howells fsec->fown_sid = current_sid(); 37011da177e4SLinus Torvalds } 37021da177e4SLinus Torvalds 37031da177e4SLinus Torvalds static int selinux_file_send_sigiotask(struct task_struct *tsk, 37041da177e4SLinus Torvalds struct fown_struct *fown, int signum) 37051da177e4SLinus Torvalds { 37061da177e4SLinus Torvalds struct file *file; 370765c90bcaSStephen Smalley u32 sid = task_sid(tsk); 37081da177e4SLinus Torvalds u32 perm; 37091da177e4SLinus Torvalds struct file_security_struct *fsec; 37101da177e4SLinus Torvalds 37111da177e4SLinus Torvalds /* struct fown_struct is never outside the context of a struct file */ 3712b385a144SRobert P. J. Day file = container_of(fown, struct file, f_owner); 37131da177e4SLinus Torvalds 37141da177e4SLinus Torvalds fsec = file->f_security; 37151da177e4SLinus Torvalds 37161da177e4SLinus Torvalds if (!signum) 37171da177e4SLinus Torvalds perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */ 37181da177e4SLinus Torvalds else 37191da177e4SLinus Torvalds perm = signal_to_av(signum); 37201da177e4SLinus Torvalds 3721275bb41eSDavid Howells return avc_has_perm(fsec->fown_sid, sid, 37221da177e4SLinus Torvalds SECCLASS_PROCESS, perm, NULL); 37231da177e4SLinus Torvalds } 37241da177e4SLinus Torvalds 37251da177e4SLinus Torvalds static int selinux_file_receive(struct file *file) 37261da177e4SLinus Torvalds { 372788e67f3bSDavid Howells const struct cred *cred = current_cred(); 372888e67f3bSDavid Howells 372988e67f3bSDavid Howells return file_has_perm(cred, file, file_to_av(file)); 37301da177e4SLinus Torvalds } 37311da177e4SLinus Torvalds 373283d49856SEric Paris static int selinux_file_open(struct file *file, const struct cred *cred) 3733788e7dd4SYuichi Nakamura { 3734788e7dd4SYuichi Nakamura struct file_security_struct *fsec; 3735788e7dd4SYuichi Nakamura struct inode_security_struct *isec; 3736d84f4f99SDavid Howells 3737788e7dd4SYuichi Nakamura fsec = file->f_security; 373883da53c5SAndreas Gruenbacher isec = inode_security(file_inode(file)); 3739788e7dd4SYuichi Nakamura /* 3740788e7dd4SYuichi Nakamura * Save inode label and policy sequence number 3741788e7dd4SYuichi Nakamura * at open-time so that selinux_file_permission 3742788e7dd4SYuichi Nakamura * can determine whether revalidation is necessary. 3743788e7dd4SYuichi Nakamura * Task label is already saved in the file security 3744788e7dd4SYuichi Nakamura * struct as its SID. 3745788e7dd4SYuichi Nakamura */ 3746788e7dd4SYuichi Nakamura fsec->isid = isec->sid; 3747788e7dd4SYuichi Nakamura fsec->pseqno = avc_policy_seqno(); 3748788e7dd4SYuichi Nakamura /* 3749788e7dd4SYuichi Nakamura * Since the inode label or policy seqno may have changed 3750788e7dd4SYuichi Nakamura * between the selinux_inode_permission check and the saving 3751788e7dd4SYuichi Nakamura * of state above, recheck that access is still permitted. 3752788e7dd4SYuichi Nakamura * Otherwise, access might never be revalidated against the 3753788e7dd4SYuichi Nakamura * new inode label or new policy. 3754788e7dd4SYuichi Nakamura * This check is not redundant - do not remove. 3755788e7dd4SYuichi Nakamura */ 375613f8e981SDavid Howells return file_path_has_perm(cred, file, open_file_to_av(file)); 3757788e7dd4SYuichi Nakamura } 3758788e7dd4SYuichi Nakamura 37591da177e4SLinus Torvalds /* task security operations */ 37601da177e4SLinus Torvalds 3761a79be238STetsuo Handa static int selinux_task_alloc(struct task_struct *task, 3762a79be238STetsuo Handa unsigned long clone_flags) 37631da177e4SLinus Torvalds { 3764be0554c9SStephen Smalley u32 sid = current_sid(); 3765be0554c9SStephen Smalley 3766be0554c9SStephen Smalley return avc_has_perm(sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL); 37671da177e4SLinus Torvalds } 37681da177e4SLinus Torvalds 3769f1752eecSDavid Howells /* 3770ee18d64cSDavid Howells * allocate the SELinux part of blank credentials 3771ee18d64cSDavid Howells */ 3772ee18d64cSDavid Howells static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp) 3773ee18d64cSDavid Howells { 3774ee18d64cSDavid Howells struct task_security_struct *tsec; 3775ee18d64cSDavid Howells 3776ee18d64cSDavid Howells tsec = kzalloc(sizeof(struct task_security_struct), gfp); 3777ee18d64cSDavid Howells if (!tsec) 3778ee18d64cSDavid Howells return -ENOMEM; 3779ee18d64cSDavid Howells 3780ee18d64cSDavid Howells cred->security = tsec; 3781ee18d64cSDavid Howells return 0; 3782ee18d64cSDavid Howells } 3783ee18d64cSDavid Howells 3784ee18d64cSDavid Howells /* 3785f1752eecSDavid Howells * detach and free the LSM part of a set of credentials 3786f1752eecSDavid Howells */ 3787f1752eecSDavid Howells static void selinux_cred_free(struct cred *cred) 37881da177e4SLinus Torvalds { 3789f1752eecSDavid Howells struct task_security_struct *tsec = cred->security; 3790e0e81739SDavid Howells 37912edeaa34STetsuo Handa /* 37922edeaa34STetsuo Handa * cred->security == NULL if security_cred_alloc_blank() or 37932edeaa34STetsuo Handa * security_prepare_creds() returned an error. 37942edeaa34STetsuo Handa */ 37952edeaa34STetsuo Handa BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE); 3796e0e81739SDavid Howells cred->security = (void *) 0x7UL; 3797f1752eecSDavid Howells kfree(tsec); 37981da177e4SLinus Torvalds } 37991da177e4SLinus Torvalds 3800d84f4f99SDavid Howells /* 3801d84f4f99SDavid Howells * prepare a new set of credentials for modification 3802d84f4f99SDavid Howells */ 3803d84f4f99SDavid Howells static int selinux_cred_prepare(struct cred *new, const struct cred *old, 3804d84f4f99SDavid Howells gfp_t gfp) 3805d84f4f99SDavid Howells { 3806d84f4f99SDavid Howells const struct task_security_struct *old_tsec; 3807d84f4f99SDavid Howells struct task_security_struct *tsec; 3808d84f4f99SDavid Howells 3809d84f4f99SDavid Howells old_tsec = old->security; 3810d84f4f99SDavid Howells 3811d84f4f99SDavid Howells tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp); 3812d84f4f99SDavid Howells if (!tsec) 3813d84f4f99SDavid Howells return -ENOMEM; 3814d84f4f99SDavid Howells 3815d84f4f99SDavid Howells new->security = tsec; 3816d84f4f99SDavid Howells return 0; 3817d84f4f99SDavid Howells } 3818d84f4f99SDavid Howells 3819d84f4f99SDavid Howells /* 3820ee18d64cSDavid Howells * transfer the SELinux data to a blank set of creds 3821ee18d64cSDavid Howells */ 3822ee18d64cSDavid Howells static void selinux_cred_transfer(struct cred *new, const struct cred *old) 3823ee18d64cSDavid Howells { 3824ee18d64cSDavid Howells const struct task_security_struct *old_tsec = old->security; 3825ee18d64cSDavid Howells struct task_security_struct *tsec = new->security; 3826ee18d64cSDavid Howells 3827ee18d64cSDavid Howells *tsec = *old_tsec; 3828ee18d64cSDavid Howells } 3829ee18d64cSDavid Howells 3830ee18d64cSDavid Howells /* 38313a3b7ce9SDavid Howells * set the security data for a kernel service 38323a3b7ce9SDavid Howells * - all the creation contexts are set to unlabelled 38333a3b7ce9SDavid Howells */ 38343a3b7ce9SDavid Howells static int selinux_kernel_act_as(struct cred *new, u32 secid) 38353a3b7ce9SDavid Howells { 38363a3b7ce9SDavid Howells struct task_security_struct *tsec = new->security; 38373a3b7ce9SDavid Howells u32 sid = current_sid(); 38383a3b7ce9SDavid Howells int ret; 38393a3b7ce9SDavid Howells 38403a3b7ce9SDavid Howells ret = avc_has_perm(sid, secid, 38413a3b7ce9SDavid Howells SECCLASS_KERNEL_SERVICE, 38423a3b7ce9SDavid Howells KERNEL_SERVICE__USE_AS_OVERRIDE, 38433a3b7ce9SDavid Howells NULL); 38443a3b7ce9SDavid Howells if (ret == 0) { 38453a3b7ce9SDavid Howells tsec->sid = secid; 38463a3b7ce9SDavid Howells tsec->create_sid = 0; 38473a3b7ce9SDavid Howells tsec->keycreate_sid = 0; 38483a3b7ce9SDavid Howells tsec->sockcreate_sid = 0; 38493a3b7ce9SDavid Howells } 38503a3b7ce9SDavid Howells return ret; 38513a3b7ce9SDavid Howells } 38523a3b7ce9SDavid Howells 38533a3b7ce9SDavid Howells /* 38543a3b7ce9SDavid Howells * set the file creation context in a security record to the same as the 38553a3b7ce9SDavid Howells * objective context of the specified inode 38563a3b7ce9SDavid Howells */ 38573a3b7ce9SDavid Howells static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode) 38583a3b7ce9SDavid Howells { 385983da53c5SAndreas Gruenbacher struct inode_security_struct *isec = inode_security(inode); 38603a3b7ce9SDavid Howells struct task_security_struct *tsec = new->security; 38613a3b7ce9SDavid Howells u32 sid = current_sid(); 38623a3b7ce9SDavid Howells int ret; 38633a3b7ce9SDavid Howells 38643a3b7ce9SDavid Howells ret = avc_has_perm(sid, isec->sid, 38653a3b7ce9SDavid Howells SECCLASS_KERNEL_SERVICE, 38663a3b7ce9SDavid Howells KERNEL_SERVICE__CREATE_FILES_AS, 38673a3b7ce9SDavid Howells NULL); 38683a3b7ce9SDavid Howells 38693a3b7ce9SDavid Howells if (ret == 0) 38703a3b7ce9SDavid Howells tsec->create_sid = isec->sid; 3871ef57471aSDavid Howells return ret; 38723a3b7ce9SDavid Howells } 38733a3b7ce9SDavid Howells 3874dd8dbf2eSEric Paris static int selinux_kernel_module_request(char *kmod_name) 387525354c4fSEric Paris { 3876dd8dbf2eSEric Paris struct common_audit_data ad; 3877dd8dbf2eSEric Paris 387850c205f5SEric Paris ad.type = LSM_AUDIT_DATA_KMOD; 3879dd8dbf2eSEric Paris ad.u.kmod_name = kmod_name; 3880dd8dbf2eSEric Paris 3881be0554c9SStephen Smalley return avc_has_perm(current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM, 3882dd8dbf2eSEric Paris SYSTEM__MODULE_REQUEST, &ad); 388325354c4fSEric Paris } 388425354c4fSEric Paris 388561d612eaSJeff Vander Stoep static int selinux_kernel_module_from_file(struct file *file) 388661d612eaSJeff Vander Stoep { 388761d612eaSJeff Vander Stoep struct common_audit_data ad; 388861d612eaSJeff Vander Stoep struct inode_security_struct *isec; 388961d612eaSJeff Vander Stoep struct file_security_struct *fsec; 389061d612eaSJeff Vander Stoep u32 sid = current_sid(); 389161d612eaSJeff Vander Stoep int rc; 389261d612eaSJeff Vander Stoep 389361d612eaSJeff Vander Stoep /* init_module */ 389461d612eaSJeff Vander Stoep if (file == NULL) 389561d612eaSJeff Vander Stoep return avc_has_perm(sid, sid, SECCLASS_SYSTEM, 389661d612eaSJeff Vander Stoep SYSTEM__MODULE_LOAD, NULL); 389761d612eaSJeff Vander Stoep 389861d612eaSJeff Vander Stoep /* finit_module */ 389920cdef8dSPaul Moore 390043af5de7SVivek Goyal ad.type = LSM_AUDIT_DATA_FILE; 390143af5de7SVivek Goyal ad.u.file = file; 390261d612eaSJeff Vander Stoep 390361d612eaSJeff Vander Stoep fsec = file->f_security; 390461d612eaSJeff Vander Stoep if (sid != fsec->sid) { 390561d612eaSJeff Vander Stoep rc = avc_has_perm(sid, fsec->sid, SECCLASS_FD, FD__USE, &ad); 390661d612eaSJeff Vander Stoep if (rc) 390761d612eaSJeff Vander Stoep return rc; 390861d612eaSJeff Vander Stoep } 390961d612eaSJeff Vander Stoep 391020cdef8dSPaul Moore isec = inode_security(file_inode(file)); 391161d612eaSJeff Vander Stoep return avc_has_perm(sid, isec->sid, SECCLASS_SYSTEM, 391261d612eaSJeff Vander Stoep SYSTEM__MODULE_LOAD, &ad); 391361d612eaSJeff Vander Stoep } 391461d612eaSJeff Vander Stoep 391561d612eaSJeff Vander Stoep static int selinux_kernel_read_file(struct file *file, 391661d612eaSJeff Vander Stoep enum kernel_read_file_id id) 391761d612eaSJeff Vander Stoep { 391861d612eaSJeff Vander Stoep int rc = 0; 391961d612eaSJeff Vander Stoep 392061d612eaSJeff Vander Stoep switch (id) { 392161d612eaSJeff Vander Stoep case READING_MODULE: 392261d612eaSJeff Vander Stoep rc = selinux_kernel_module_from_file(file); 392361d612eaSJeff Vander Stoep break; 392461d612eaSJeff Vander Stoep default: 392561d612eaSJeff Vander Stoep break; 392661d612eaSJeff Vander Stoep } 392761d612eaSJeff Vander Stoep 392861d612eaSJeff Vander Stoep return rc; 392961d612eaSJeff Vander Stoep } 393061d612eaSJeff Vander Stoep 39311da177e4SLinus Torvalds static int selinux_task_setpgid(struct task_struct *p, pid_t pgid) 39321da177e4SLinus Torvalds { 3933be0554c9SStephen Smalley return avc_has_perm(current_sid(), task_sid(p), SECCLASS_PROCESS, 3934be0554c9SStephen Smalley PROCESS__SETPGID, NULL); 39351da177e4SLinus Torvalds } 39361da177e4SLinus Torvalds 39371da177e4SLinus Torvalds static int selinux_task_getpgid(struct task_struct *p) 39381da177e4SLinus Torvalds { 3939be0554c9SStephen Smalley return avc_has_perm(current_sid(), task_sid(p), SECCLASS_PROCESS, 3940be0554c9SStephen Smalley PROCESS__GETPGID, NULL); 39411da177e4SLinus Torvalds } 39421da177e4SLinus Torvalds 39431da177e4SLinus Torvalds static int selinux_task_getsid(struct task_struct *p) 39441da177e4SLinus Torvalds { 3945be0554c9SStephen Smalley return avc_has_perm(current_sid(), task_sid(p), SECCLASS_PROCESS, 3946be0554c9SStephen Smalley PROCESS__GETSESSION, NULL); 39471da177e4SLinus Torvalds } 39481da177e4SLinus Torvalds 3949f9008e4cSDavid Quigley static void selinux_task_getsecid(struct task_struct *p, u32 *secid) 3950f9008e4cSDavid Quigley { 3951275bb41eSDavid Howells *secid = task_sid(p); 3952f9008e4cSDavid Quigley } 3953f9008e4cSDavid Quigley 39541da177e4SLinus Torvalds static int selinux_task_setnice(struct task_struct *p, int nice) 39551da177e4SLinus Torvalds { 3956be0554c9SStephen Smalley return avc_has_perm(current_sid(), task_sid(p), SECCLASS_PROCESS, 3957be0554c9SStephen Smalley PROCESS__SETSCHED, NULL); 39581da177e4SLinus Torvalds } 39591da177e4SLinus Torvalds 396003e68060SJames Morris static int selinux_task_setioprio(struct task_struct *p, int ioprio) 396103e68060SJames Morris { 3962be0554c9SStephen Smalley return avc_has_perm(current_sid(), task_sid(p), SECCLASS_PROCESS, 3963be0554c9SStephen Smalley PROCESS__SETSCHED, NULL); 396403e68060SJames Morris } 396503e68060SJames Morris 3966a1836a42SDavid Quigley static int selinux_task_getioprio(struct task_struct *p) 3967a1836a42SDavid Quigley { 3968be0554c9SStephen Smalley return avc_has_perm(current_sid(), task_sid(p), SECCLASS_PROCESS, 3969be0554c9SStephen Smalley PROCESS__GETSCHED, NULL); 3970a1836a42SDavid Quigley } 3971a1836a42SDavid Quigley 39724298555dSCorentin LABBE static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred, 3973791ec491SStephen Smalley unsigned int flags) 3974791ec491SStephen Smalley { 3975791ec491SStephen Smalley u32 av = 0; 3976791ec491SStephen Smalley 397784e6885eSStephen Smalley if (!flags) 397884e6885eSStephen Smalley return 0; 3979791ec491SStephen Smalley if (flags & LSM_PRLIMIT_WRITE) 3980791ec491SStephen Smalley av |= PROCESS__SETRLIMIT; 3981791ec491SStephen Smalley if (flags & LSM_PRLIMIT_READ) 3982791ec491SStephen Smalley av |= PROCESS__GETRLIMIT; 3983791ec491SStephen Smalley return avc_has_perm(cred_sid(cred), cred_sid(tcred), 3984791ec491SStephen Smalley SECCLASS_PROCESS, av, NULL); 3985791ec491SStephen Smalley } 3986791ec491SStephen Smalley 39878fd00b4dSJiri Slaby static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource, 39888fd00b4dSJiri Slaby struct rlimit *new_rlim) 39891da177e4SLinus Torvalds { 39908fd00b4dSJiri Slaby struct rlimit *old_rlim = p->signal->rlim + resource; 39911da177e4SLinus Torvalds 39921da177e4SLinus Torvalds /* Control the ability to change the hard limit (whether 39931da177e4SLinus Torvalds lowering or raising it), so that the hard limit can 39941da177e4SLinus Torvalds later be used as a safe reset point for the soft limit 3995d84f4f99SDavid Howells upon context transitions. See selinux_bprm_committing_creds. */ 39961da177e4SLinus Torvalds if (old_rlim->rlim_max != new_rlim->rlim_max) 3997be0554c9SStephen Smalley return avc_has_perm(current_sid(), task_sid(p), 3998be0554c9SStephen Smalley SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL); 39991da177e4SLinus Torvalds 40001da177e4SLinus Torvalds return 0; 40011da177e4SLinus Torvalds } 40021da177e4SLinus Torvalds 4003b0ae1981SKOSAKI Motohiro static int selinux_task_setscheduler(struct task_struct *p) 40041da177e4SLinus Torvalds { 4005be0554c9SStephen Smalley return avc_has_perm(current_sid(), task_sid(p), SECCLASS_PROCESS, 4006be0554c9SStephen Smalley PROCESS__SETSCHED, NULL); 40071da177e4SLinus Torvalds } 40081da177e4SLinus Torvalds 40091da177e4SLinus Torvalds static int selinux_task_getscheduler(struct task_struct *p) 40101da177e4SLinus Torvalds { 4011be0554c9SStephen Smalley return avc_has_perm(current_sid(), task_sid(p), SECCLASS_PROCESS, 4012be0554c9SStephen Smalley PROCESS__GETSCHED, NULL); 40131da177e4SLinus Torvalds } 40141da177e4SLinus Torvalds 401535601547SDavid Quigley static int selinux_task_movememory(struct task_struct *p) 401635601547SDavid Quigley { 4017be0554c9SStephen Smalley return avc_has_perm(current_sid(), task_sid(p), SECCLASS_PROCESS, 4018be0554c9SStephen Smalley PROCESS__SETSCHED, NULL); 401935601547SDavid Quigley } 402035601547SDavid Quigley 4021f9008e4cSDavid Quigley static int selinux_task_kill(struct task_struct *p, struct siginfo *info, 4022f9008e4cSDavid Quigley int sig, u32 secid) 40231da177e4SLinus Torvalds { 40241da177e4SLinus Torvalds u32 perm; 40251da177e4SLinus Torvalds 40261da177e4SLinus Torvalds if (!sig) 40271da177e4SLinus Torvalds perm = PROCESS__SIGNULL; /* null signal; existence test */ 40281da177e4SLinus Torvalds else 40291da177e4SLinus Torvalds perm = signal_to_av(sig); 4030be0554c9SStephen Smalley if (!secid) 4031be0554c9SStephen Smalley secid = current_sid(); 4032be0554c9SStephen Smalley return avc_has_perm(secid, task_sid(p), SECCLASS_PROCESS, perm, NULL); 40331da177e4SLinus Torvalds } 40341da177e4SLinus Torvalds 40351da177e4SLinus Torvalds static void selinux_task_to_inode(struct task_struct *p, 40361da177e4SLinus Torvalds struct inode *inode) 40371da177e4SLinus Torvalds { 40381da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 4039275bb41eSDavid Howells u32 sid = task_sid(p); 40401da177e4SLinus Torvalds 40419287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 4042db978da8SAndreas Gruenbacher isec->sclass = inode_mode_to_security_class(inode->i_mode); 4043275bb41eSDavid Howells isec->sid = sid; 40446f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 40459287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 40461da177e4SLinus Torvalds } 40471da177e4SLinus Torvalds 40481da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 404967f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv4(struct sk_buff *skb, 40502bf49690SThomas Liu struct common_audit_data *ad, u8 *proto) 40511da177e4SLinus Torvalds { 40521da177e4SLinus Torvalds int offset, ihlen, ret = -EINVAL; 40531da177e4SLinus Torvalds struct iphdr _iph, *ih; 40541da177e4SLinus Torvalds 4055bbe735e4SArnaldo Carvalho de Melo offset = skb_network_offset(skb); 40561da177e4SLinus Torvalds ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph); 40571da177e4SLinus Torvalds if (ih == NULL) 40581da177e4SLinus Torvalds goto out; 40591da177e4SLinus Torvalds 40601da177e4SLinus Torvalds ihlen = ih->ihl * 4; 40611da177e4SLinus Torvalds if (ihlen < sizeof(_iph)) 40621da177e4SLinus Torvalds goto out; 40631da177e4SLinus Torvalds 406448c62af6SEric Paris ad->u.net->v4info.saddr = ih->saddr; 406548c62af6SEric Paris ad->u.net->v4info.daddr = ih->daddr; 40661da177e4SLinus Torvalds ret = 0; 40671da177e4SLinus Torvalds 406867f83cbfSVenkat Yekkirala if (proto) 406967f83cbfSVenkat Yekkirala *proto = ih->protocol; 407067f83cbfSVenkat Yekkirala 40711da177e4SLinus Torvalds switch (ih->protocol) { 40721da177e4SLinus Torvalds case IPPROTO_TCP: { 40731da177e4SLinus Torvalds struct tcphdr _tcph, *th; 40741da177e4SLinus Torvalds 40751da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 40761da177e4SLinus Torvalds break; 40771da177e4SLinus Torvalds 40781da177e4SLinus Torvalds offset += ihlen; 40791da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 40801da177e4SLinus Torvalds if (th == NULL) 40811da177e4SLinus Torvalds break; 40821da177e4SLinus Torvalds 408348c62af6SEric Paris ad->u.net->sport = th->source; 408448c62af6SEric Paris ad->u.net->dport = th->dest; 40851da177e4SLinus Torvalds break; 40861da177e4SLinus Torvalds } 40871da177e4SLinus Torvalds 40881da177e4SLinus Torvalds case IPPROTO_UDP: { 40891da177e4SLinus Torvalds struct udphdr _udph, *uh; 40901da177e4SLinus Torvalds 40911da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 40921da177e4SLinus Torvalds break; 40931da177e4SLinus Torvalds 40941da177e4SLinus Torvalds offset += ihlen; 40951da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 40961da177e4SLinus Torvalds if (uh == NULL) 40971da177e4SLinus Torvalds break; 40981da177e4SLinus Torvalds 409948c62af6SEric Paris ad->u.net->sport = uh->source; 410048c62af6SEric Paris ad->u.net->dport = uh->dest; 41011da177e4SLinus Torvalds break; 41021da177e4SLinus Torvalds } 41031da177e4SLinus Torvalds 41042ee92d46SJames Morris case IPPROTO_DCCP: { 41052ee92d46SJames Morris struct dccp_hdr _dccph, *dh; 41062ee92d46SJames Morris 41072ee92d46SJames Morris if (ntohs(ih->frag_off) & IP_OFFSET) 41082ee92d46SJames Morris break; 41092ee92d46SJames Morris 41102ee92d46SJames Morris offset += ihlen; 41112ee92d46SJames Morris dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); 41122ee92d46SJames Morris if (dh == NULL) 41132ee92d46SJames Morris break; 41142ee92d46SJames Morris 411548c62af6SEric Paris ad->u.net->sport = dh->dccph_sport; 411648c62af6SEric Paris ad->u.net->dport = dh->dccph_dport; 41172ee92d46SJames Morris break; 41182ee92d46SJames Morris } 41192ee92d46SJames Morris 41201da177e4SLinus Torvalds default: 41211da177e4SLinus Torvalds break; 41221da177e4SLinus Torvalds } 41231da177e4SLinus Torvalds out: 41241da177e4SLinus Torvalds return ret; 41251da177e4SLinus Torvalds } 41261da177e4SLinus Torvalds 41271a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 41281da177e4SLinus Torvalds 41291da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 413067f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv6(struct sk_buff *skb, 41312bf49690SThomas Liu struct common_audit_data *ad, u8 *proto) 41321da177e4SLinus Torvalds { 41331da177e4SLinus Torvalds u8 nexthdr; 41341da177e4SLinus Torvalds int ret = -EINVAL, offset; 41351da177e4SLinus Torvalds struct ipv6hdr _ipv6h, *ip6; 413675f2811cSJesse Gross __be16 frag_off; 41371da177e4SLinus Torvalds 4138bbe735e4SArnaldo Carvalho de Melo offset = skb_network_offset(skb); 41391da177e4SLinus Torvalds ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h); 41401da177e4SLinus Torvalds if (ip6 == NULL) 41411da177e4SLinus Torvalds goto out; 41421da177e4SLinus Torvalds 414348c62af6SEric Paris ad->u.net->v6info.saddr = ip6->saddr; 414448c62af6SEric Paris ad->u.net->v6info.daddr = ip6->daddr; 41451da177e4SLinus Torvalds ret = 0; 41461da177e4SLinus Torvalds 41471da177e4SLinus Torvalds nexthdr = ip6->nexthdr; 41481da177e4SLinus Torvalds offset += sizeof(_ipv6h); 414975f2811cSJesse Gross offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off); 41501da177e4SLinus Torvalds if (offset < 0) 41511da177e4SLinus Torvalds goto out; 41521da177e4SLinus Torvalds 415367f83cbfSVenkat Yekkirala if (proto) 415467f83cbfSVenkat Yekkirala *proto = nexthdr; 415567f83cbfSVenkat Yekkirala 41561da177e4SLinus Torvalds switch (nexthdr) { 41571da177e4SLinus Torvalds case IPPROTO_TCP: { 41581da177e4SLinus Torvalds struct tcphdr _tcph, *th; 41591da177e4SLinus Torvalds 41601da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 41611da177e4SLinus Torvalds if (th == NULL) 41621da177e4SLinus Torvalds break; 41631da177e4SLinus Torvalds 416448c62af6SEric Paris ad->u.net->sport = th->source; 416548c62af6SEric Paris ad->u.net->dport = th->dest; 41661da177e4SLinus Torvalds break; 41671da177e4SLinus Torvalds } 41681da177e4SLinus Torvalds 41691da177e4SLinus Torvalds case IPPROTO_UDP: { 41701da177e4SLinus Torvalds struct udphdr _udph, *uh; 41711da177e4SLinus Torvalds 41721da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 41731da177e4SLinus Torvalds if (uh == NULL) 41741da177e4SLinus Torvalds break; 41751da177e4SLinus Torvalds 417648c62af6SEric Paris ad->u.net->sport = uh->source; 417748c62af6SEric Paris ad->u.net->dport = uh->dest; 41781da177e4SLinus Torvalds break; 41791da177e4SLinus Torvalds } 41801da177e4SLinus Torvalds 41812ee92d46SJames Morris case IPPROTO_DCCP: { 41822ee92d46SJames Morris struct dccp_hdr _dccph, *dh; 41832ee92d46SJames Morris 41842ee92d46SJames Morris dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); 41852ee92d46SJames Morris if (dh == NULL) 41862ee92d46SJames Morris break; 41872ee92d46SJames Morris 418848c62af6SEric Paris ad->u.net->sport = dh->dccph_sport; 418948c62af6SEric Paris ad->u.net->dport = dh->dccph_dport; 41902ee92d46SJames Morris break; 41912ee92d46SJames Morris } 41922ee92d46SJames Morris 41931da177e4SLinus Torvalds /* includes fragments */ 41941da177e4SLinus Torvalds default: 41951da177e4SLinus Torvalds break; 41961da177e4SLinus Torvalds } 41971da177e4SLinus Torvalds out: 41981da177e4SLinus Torvalds return ret; 41991da177e4SLinus Torvalds } 42001da177e4SLinus Torvalds 42011da177e4SLinus Torvalds #endif /* IPV6 */ 42021da177e4SLinus Torvalds 42032bf49690SThomas Liu static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad, 4204cf9481e2SDavid Howells char **_addrp, int src, u8 *proto) 42051da177e4SLinus Torvalds { 4206cf9481e2SDavid Howells char *addrp; 4207cf9481e2SDavid Howells int ret; 42081da177e4SLinus Torvalds 420948c62af6SEric Paris switch (ad->u.net->family) { 42101da177e4SLinus Torvalds case PF_INET: 421167f83cbfSVenkat Yekkirala ret = selinux_parse_skb_ipv4(skb, ad, proto); 4212cf9481e2SDavid Howells if (ret) 4213cf9481e2SDavid Howells goto parse_error; 421448c62af6SEric Paris addrp = (char *)(src ? &ad->u.net->v4info.saddr : 421548c62af6SEric Paris &ad->u.net->v4info.daddr); 4216cf9481e2SDavid Howells goto okay; 42171da177e4SLinus Torvalds 42181a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 42191da177e4SLinus Torvalds case PF_INET6: 422067f83cbfSVenkat Yekkirala ret = selinux_parse_skb_ipv6(skb, ad, proto); 4221cf9481e2SDavid Howells if (ret) 4222cf9481e2SDavid Howells goto parse_error; 422348c62af6SEric Paris addrp = (char *)(src ? &ad->u.net->v6info.saddr : 422448c62af6SEric Paris &ad->u.net->v6info.daddr); 4225cf9481e2SDavid Howells goto okay; 42261da177e4SLinus Torvalds #endif /* IPV6 */ 42271da177e4SLinus Torvalds default: 4228cf9481e2SDavid Howells addrp = NULL; 4229cf9481e2SDavid Howells goto okay; 42301da177e4SLinus Torvalds } 42311da177e4SLinus Torvalds 4232cf9481e2SDavid Howells parse_error: 423371f1cb05SPaul Moore printk(KERN_WARNING 423471f1cb05SPaul Moore "SELinux: failure in selinux_parse_skb()," 423571f1cb05SPaul Moore " unable to parse packet\n"); 42361da177e4SLinus Torvalds return ret; 4237cf9481e2SDavid Howells 4238cf9481e2SDavid Howells okay: 4239cf9481e2SDavid Howells if (_addrp) 4240cf9481e2SDavid Howells *_addrp = addrp; 4241cf9481e2SDavid Howells return 0; 42421da177e4SLinus Torvalds } 42431da177e4SLinus Torvalds 42444f6a993fSPaul Moore /** 4245220deb96SPaul Moore * selinux_skb_peerlbl_sid - Determine the peer label of a packet 42464f6a993fSPaul Moore * @skb: the packet 424775e22910SPaul Moore * @family: protocol family 4248220deb96SPaul Moore * @sid: the packet's peer label SID 42494f6a993fSPaul Moore * 42504f6a993fSPaul Moore * Description: 4251220deb96SPaul Moore * Check the various different forms of network peer labeling and determine 4252220deb96SPaul Moore * the peer label/SID for the packet; most of the magic actually occurs in 4253220deb96SPaul Moore * the security server function security_net_peersid_cmp(). The function 4254220deb96SPaul Moore * returns zero if the value in @sid is valid (although it may be SECSID_NULL) 4255220deb96SPaul Moore * or -EACCES if @sid is invalid due to inconsistencies with the different 4256220deb96SPaul Moore * peer labels. 42574f6a993fSPaul Moore * 42584f6a993fSPaul Moore */ 4259220deb96SPaul Moore static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid) 42604f6a993fSPaul Moore { 426171f1cb05SPaul Moore int err; 42624f6a993fSPaul Moore u32 xfrm_sid; 42634f6a993fSPaul Moore u32 nlbl_sid; 4264220deb96SPaul Moore u32 nlbl_type; 42654f6a993fSPaul Moore 4266817eff71SPaul Moore err = selinux_xfrm_skb_sid(skb, &xfrm_sid); 4267bed4d7efSPaul Moore if (unlikely(err)) 4268bed4d7efSPaul Moore return -EACCES; 4269bed4d7efSPaul Moore err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid); 4270bed4d7efSPaul Moore if (unlikely(err)) 4271bed4d7efSPaul Moore return -EACCES; 4272220deb96SPaul Moore 427371f1cb05SPaul Moore err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid); 427471f1cb05SPaul Moore if (unlikely(err)) { 427571f1cb05SPaul Moore printk(KERN_WARNING 427671f1cb05SPaul Moore "SELinux: failure in selinux_skb_peerlbl_sid()," 427771f1cb05SPaul Moore " unable to determine packet's peer label\n"); 4278220deb96SPaul Moore return -EACCES; 427971f1cb05SPaul Moore } 4280220deb96SPaul Moore 4281220deb96SPaul Moore return 0; 42824f6a993fSPaul Moore } 42834f6a993fSPaul Moore 4284446b8024SPaul Moore /** 4285446b8024SPaul Moore * selinux_conn_sid - Determine the child socket label for a connection 4286446b8024SPaul Moore * @sk_sid: the parent socket's SID 4287446b8024SPaul Moore * @skb_sid: the packet's SID 4288446b8024SPaul Moore * @conn_sid: the resulting connection SID 4289446b8024SPaul Moore * 4290446b8024SPaul Moore * If @skb_sid is valid then the user:role:type information from @sk_sid is 4291446b8024SPaul Moore * combined with the MLS information from @skb_sid in order to create 4292446b8024SPaul Moore * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy 4293446b8024SPaul Moore * of @sk_sid. Returns zero on success, negative values on failure. 4294446b8024SPaul Moore * 4295446b8024SPaul Moore */ 4296446b8024SPaul Moore static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid) 4297446b8024SPaul Moore { 4298446b8024SPaul Moore int err = 0; 4299446b8024SPaul Moore 4300446b8024SPaul Moore if (skb_sid != SECSID_NULL) 4301446b8024SPaul Moore err = security_sid_mls_copy(sk_sid, skb_sid, conn_sid); 4302446b8024SPaul Moore else 4303446b8024SPaul Moore *conn_sid = sk_sid; 4304446b8024SPaul Moore 4305446b8024SPaul Moore return err; 4306446b8024SPaul Moore } 4307446b8024SPaul Moore 43081da177e4SLinus Torvalds /* socket security operations */ 4309d4f2d978SPaul Moore 43102ad18bdfSHarry Ciao static int socket_sockcreate_sid(const struct task_security_struct *tsec, 43112ad18bdfSHarry Ciao u16 secclass, u32 *socksid) 4312d4f2d978SPaul Moore { 43132ad18bdfSHarry Ciao if (tsec->sockcreate_sid > SECSID_NULL) { 43142ad18bdfSHarry Ciao *socksid = tsec->sockcreate_sid; 43152ad18bdfSHarry Ciao return 0; 43162ad18bdfSHarry Ciao } 43172ad18bdfSHarry Ciao 43182ad18bdfSHarry Ciao return security_transition_sid(tsec->sid, tsec->sid, secclass, NULL, 43192ad18bdfSHarry Ciao socksid); 4320d4f2d978SPaul Moore } 4321d4f2d978SPaul Moore 4322be0554c9SStephen Smalley static int sock_has_perm(struct sock *sk, u32 perms) 43231da177e4SLinus Torvalds { 4324253bfae6SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 43252bf49690SThomas Liu struct common_audit_data ad; 432648c62af6SEric Paris struct lsm_network_audit net = {0,}; 43271da177e4SLinus Torvalds 4328253bfae6SPaul Moore if (sksec->sid == SECINITSID_KERNEL) 4329253bfae6SPaul Moore return 0; 43301da177e4SLinus Torvalds 433150c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 433248c62af6SEric Paris ad.u.net = &net; 433348c62af6SEric Paris ad.u.net->sk = sk; 43341da177e4SLinus Torvalds 4335be0554c9SStephen Smalley return avc_has_perm(current_sid(), sksec->sid, sksec->sclass, perms, 4336be0554c9SStephen Smalley &ad); 43371da177e4SLinus Torvalds } 43381da177e4SLinus Torvalds 43391da177e4SLinus Torvalds static int selinux_socket_create(int family, int type, 43401da177e4SLinus Torvalds int protocol, int kern) 43411da177e4SLinus Torvalds { 43425fb49870SPaul Moore const struct task_security_struct *tsec = current_security(); 4343d4f2d978SPaul Moore u32 newsid; 4344275bb41eSDavid Howells u16 secclass; 43452ad18bdfSHarry Ciao int rc; 43461da177e4SLinus Torvalds 43471da177e4SLinus Torvalds if (kern) 4348d4f2d978SPaul Moore return 0; 43491da177e4SLinus Torvalds 4350275bb41eSDavid Howells secclass = socket_type_to_security_class(family, type, protocol); 43512ad18bdfSHarry Ciao rc = socket_sockcreate_sid(tsec, secclass, &newsid); 43522ad18bdfSHarry Ciao if (rc) 43532ad18bdfSHarry Ciao return rc; 43542ad18bdfSHarry Ciao 4355d4f2d978SPaul Moore return avc_has_perm(tsec->sid, newsid, secclass, SOCKET__CREATE, NULL); 43561da177e4SLinus Torvalds } 43571da177e4SLinus Torvalds 43587420ed23SVenkat Yekkirala static int selinux_socket_post_create(struct socket *sock, int family, 43591da177e4SLinus Torvalds int type, int protocol, int kern) 43601da177e4SLinus Torvalds { 43615fb49870SPaul Moore const struct task_security_struct *tsec = current_security(); 43625d226df4SAndreas Gruenbacher struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock)); 4363892c141eSVenkat Yekkirala struct sk_security_struct *sksec; 43649287aed2SAndreas Gruenbacher u16 sclass = socket_type_to_security_class(family, type, protocol); 43659287aed2SAndreas Gruenbacher u32 sid = SECINITSID_KERNEL; 4366275bb41eSDavid Howells int err = 0; 4367275bb41eSDavid Howells 43689287aed2SAndreas Gruenbacher if (!kern) { 43699287aed2SAndreas Gruenbacher err = socket_sockcreate_sid(tsec, sclass, &sid); 43702ad18bdfSHarry Ciao if (err) 43712ad18bdfSHarry Ciao return err; 43722ad18bdfSHarry Ciao } 4373275bb41eSDavid Howells 43749287aed2SAndreas Gruenbacher isec->sclass = sclass; 43759287aed2SAndreas Gruenbacher isec->sid = sid; 43766f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 43771da177e4SLinus Torvalds 4378892c141eSVenkat Yekkirala if (sock->sk) { 4379892c141eSVenkat Yekkirala sksec = sock->sk->sk_security; 43809287aed2SAndreas Gruenbacher sksec->sclass = sclass; 43819287aed2SAndreas Gruenbacher sksec->sid = sid; 4382389fb800SPaul Moore err = selinux_netlbl_socket_post_create(sock->sk, family); 4383892c141eSVenkat Yekkirala } 4384892c141eSVenkat Yekkirala 43857420ed23SVenkat Yekkirala return err; 43861da177e4SLinus Torvalds } 43871da177e4SLinus Torvalds 43881da177e4SLinus Torvalds /* Range of port numbers used to automatically bind. 43891da177e4SLinus Torvalds Need to determine whether we should perform a name_bind 43901da177e4SLinus Torvalds permission check between the socket and the port number. */ 43911da177e4SLinus Torvalds 43921da177e4SLinus Torvalds static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen) 43931da177e4SLinus Torvalds { 4394253bfae6SPaul Moore struct sock *sk = sock->sk; 43951da177e4SLinus Torvalds u16 family; 43961da177e4SLinus Torvalds int err; 43971da177e4SLinus Torvalds 4398be0554c9SStephen Smalley err = sock_has_perm(sk, SOCKET__BIND); 43991da177e4SLinus Torvalds if (err) 44001da177e4SLinus Torvalds goto out; 44011da177e4SLinus Torvalds 44021da177e4SLinus Torvalds /* 44031da177e4SLinus Torvalds * If PF_INET or PF_INET6, check name_bind permission for the port. 440413402580SJames Morris * Multiple address binding for SCTP is not supported yet: we just 440513402580SJames Morris * check the first address now. 44061da177e4SLinus Torvalds */ 4407253bfae6SPaul Moore family = sk->sk_family; 44081da177e4SLinus Torvalds if (family == PF_INET || family == PF_INET6) { 44091da177e4SLinus Torvalds char *addrp; 4410253bfae6SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 44112bf49690SThomas Liu struct common_audit_data ad; 441248c62af6SEric Paris struct lsm_network_audit net = {0,}; 44131da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 44141da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 44151da177e4SLinus Torvalds unsigned short snum; 4416e399f982SJames Morris u32 sid, node_perm; 44171da177e4SLinus Torvalds 44181da177e4SLinus Torvalds if (family == PF_INET) { 4419e2f586bdSAlexander Potapenko if (addrlen < sizeof(struct sockaddr_in)) { 4420e2f586bdSAlexander Potapenko err = -EINVAL; 4421e2f586bdSAlexander Potapenko goto out; 4422e2f586bdSAlexander Potapenko } 44231da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 44241da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 44251da177e4SLinus Torvalds addrp = (char *)&addr4->sin_addr.s_addr; 44261da177e4SLinus Torvalds } else { 4427e2f586bdSAlexander Potapenko if (addrlen < SIN6_LEN_RFC2133) { 4428e2f586bdSAlexander Potapenko err = -EINVAL; 4429e2f586bdSAlexander Potapenko goto out; 4430e2f586bdSAlexander Potapenko } 44311da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 44321da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 44331da177e4SLinus Torvalds addrp = (char *)&addr6->sin6_addr.s6_addr; 44341da177e4SLinus Torvalds } 44351da177e4SLinus Torvalds 4436227b60f5SStephen Hemminger if (snum) { 4437227b60f5SStephen Hemminger int low, high; 4438227b60f5SStephen Hemminger 44390bbf87d8SEric W. Biederman inet_get_local_port_range(sock_net(sk), &low, &high); 4440227b60f5SStephen Hemminger 44414548b683SKrister Johansen if (snum < max(inet_prot_sock(sock_net(sk)), low) || 44424548b683SKrister Johansen snum > high) { 44433e112172SPaul Moore err = sel_netport_sid(sk->sk_protocol, 44443e112172SPaul Moore snum, &sid); 44451da177e4SLinus Torvalds if (err) 44461da177e4SLinus Torvalds goto out; 444750c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 444848c62af6SEric Paris ad.u.net = &net; 444948c62af6SEric Paris ad.u.net->sport = htons(snum); 445048c62af6SEric Paris ad.u.net->family = family; 4451253bfae6SPaul Moore err = avc_has_perm(sksec->sid, sid, 4452253bfae6SPaul Moore sksec->sclass, 44531da177e4SLinus Torvalds SOCKET__NAME_BIND, &ad); 44541da177e4SLinus Torvalds if (err) 44551da177e4SLinus Torvalds goto out; 44561da177e4SLinus Torvalds } 4457227b60f5SStephen Hemminger } 44581da177e4SLinus Torvalds 4459253bfae6SPaul Moore switch (sksec->sclass) { 446013402580SJames Morris case SECCLASS_TCP_SOCKET: 44611da177e4SLinus Torvalds node_perm = TCP_SOCKET__NODE_BIND; 44621da177e4SLinus Torvalds break; 44631da177e4SLinus Torvalds 446413402580SJames Morris case SECCLASS_UDP_SOCKET: 44651da177e4SLinus Torvalds node_perm = UDP_SOCKET__NODE_BIND; 44661da177e4SLinus Torvalds break; 44671da177e4SLinus Torvalds 44682ee92d46SJames Morris case SECCLASS_DCCP_SOCKET: 44692ee92d46SJames Morris node_perm = DCCP_SOCKET__NODE_BIND; 44702ee92d46SJames Morris break; 44712ee92d46SJames Morris 44721da177e4SLinus Torvalds default: 44731da177e4SLinus Torvalds node_perm = RAWIP_SOCKET__NODE_BIND; 44741da177e4SLinus Torvalds break; 44751da177e4SLinus Torvalds } 44761da177e4SLinus Torvalds 4477224dfbd8SPaul Moore err = sel_netnode_sid(addrp, family, &sid); 44781da177e4SLinus Torvalds if (err) 44791da177e4SLinus Torvalds goto out; 44801da177e4SLinus Torvalds 448150c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 448248c62af6SEric Paris ad.u.net = &net; 448348c62af6SEric Paris ad.u.net->sport = htons(snum); 448448c62af6SEric Paris ad.u.net->family = family; 44851da177e4SLinus Torvalds 44861da177e4SLinus Torvalds if (family == PF_INET) 448748c62af6SEric Paris ad.u.net->v4info.saddr = addr4->sin_addr.s_addr; 44881da177e4SLinus Torvalds else 448948c62af6SEric Paris ad.u.net->v6info.saddr = addr6->sin6_addr; 44901da177e4SLinus Torvalds 4491253bfae6SPaul Moore err = avc_has_perm(sksec->sid, sid, 4492253bfae6SPaul Moore sksec->sclass, node_perm, &ad); 44931da177e4SLinus Torvalds if (err) 44941da177e4SLinus Torvalds goto out; 44951da177e4SLinus Torvalds } 44961da177e4SLinus Torvalds out: 44971da177e4SLinus Torvalds return err; 44981da177e4SLinus Torvalds } 44991da177e4SLinus Torvalds 45001da177e4SLinus Torvalds static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen) 45011da177e4SLinus Torvalds { 4502014ab19aSPaul Moore struct sock *sk = sock->sk; 4503253bfae6SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 45041da177e4SLinus Torvalds int err; 45051da177e4SLinus Torvalds 4506be0554c9SStephen Smalley err = sock_has_perm(sk, SOCKET__CONNECT); 45071da177e4SLinus Torvalds if (err) 45081da177e4SLinus Torvalds return err; 45091da177e4SLinus Torvalds 45101da177e4SLinus Torvalds /* 45112ee92d46SJames Morris * If a TCP or DCCP socket, check name_connect permission for the port. 45121da177e4SLinus Torvalds */ 4513253bfae6SPaul Moore if (sksec->sclass == SECCLASS_TCP_SOCKET || 4514253bfae6SPaul Moore sksec->sclass == SECCLASS_DCCP_SOCKET) { 45152bf49690SThomas Liu struct common_audit_data ad; 451648c62af6SEric Paris struct lsm_network_audit net = {0,}; 45171da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 45181da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 45191da177e4SLinus Torvalds unsigned short snum; 45202ee92d46SJames Morris u32 sid, perm; 45211da177e4SLinus Torvalds 45221da177e4SLinus Torvalds if (sk->sk_family == PF_INET) { 45231da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 4524911656f8SStephen Smalley if (addrlen < sizeof(struct sockaddr_in)) 45251da177e4SLinus Torvalds return -EINVAL; 45261da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 45271da177e4SLinus Torvalds } else { 45281da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 4529911656f8SStephen Smalley if (addrlen < SIN6_LEN_RFC2133) 45301da177e4SLinus Torvalds return -EINVAL; 45311da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 45321da177e4SLinus Torvalds } 45331da177e4SLinus Torvalds 45343e112172SPaul Moore err = sel_netport_sid(sk->sk_protocol, snum, &sid); 45351da177e4SLinus Torvalds if (err) 45361da177e4SLinus Torvalds goto out; 45371da177e4SLinus Torvalds 4538253bfae6SPaul Moore perm = (sksec->sclass == SECCLASS_TCP_SOCKET) ? 45392ee92d46SJames Morris TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT; 45402ee92d46SJames Morris 454150c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 454248c62af6SEric Paris ad.u.net = &net; 454348c62af6SEric Paris ad.u.net->dport = htons(snum); 454448c62af6SEric Paris ad.u.net->family = sk->sk_family; 4545253bfae6SPaul Moore err = avc_has_perm(sksec->sid, sid, sksec->sclass, perm, &ad); 45461da177e4SLinus Torvalds if (err) 45471da177e4SLinus Torvalds goto out; 45481da177e4SLinus Torvalds } 45491da177e4SLinus Torvalds 4550014ab19aSPaul Moore err = selinux_netlbl_socket_connect(sk, address); 4551014ab19aSPaul Moore 45521da177e4SLinus Torvalds out: 45531da177e4SLinus Torvalds return err; 45541da177e4SLinus Torvalds } 45551da177e4SLinus Torvalds 45561da177e4SLinus Torvalds static int selinux_socket_listen(struct socket *sock, int backlog) 45571da177e4SLinus Torvalds { 4558be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__LISTEN); 45591da177e4SLinus Torvalds } 45601da177e4SLinus Torvalds 45611da177e4SLinus Torvalds static int selinux_socket_accept(struct socket *sock, struct socket *newsock) 45621da177e4SLinus Torvalds { 45631da177e4SLinus Torvalds int err; 45641da177e4SLinus Torvalds struct inode_security_struct *isec; 45651da177e4SLinus Torvalds struct inode_security_struct *newisec; 45669287aed2SAndreas Gruenbacher u16 sclass; 45679287aed2SAndreas Gruenbacher u32 sid; 45681da177e4SLinus Torvalds 4569be0554c9SStephen Smalley err = sock_has_perm(sock->sk, SOCKET__ACCEPT); 45701da177e4SLinus Torvalds if (err) 45711da177e4SLinus Torvalds return err; 45721da177e4SLinus Torvalds 45735d226df4SAndreas Gruenbacher isec = inode_security_novalidate(SOCK_INODE(sock)); 45749287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 45759287aed2SAndreas Gruenbacher sclass = isec->sclass; 45769287aed2SAndreas Gruenbacher sid = isec->sid; 45779287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 45789287aed2SAndreas Gruenbacher 45799287aed2SAndreas Gruenbacher newisec = inode_security_novalidate(SOCK_INODE(newsock)); 45809287aed2SAndreas Gruenbacher newisec->sclass = sclass; 45819287aed2SAndreas Gruenbacher newisec->sid = sid; 45826f3be9f5SAndreas Gruenbacher newisec->initialized = LABEL_INITIALIZED; 45831da177e4SLinus Torvalds 45841da177e4SLinus Torvalds return 0; 45851da177e4SLinus Torvalds } 45861da177e4SLinus Torvalds 45871da177e4SLinus Torvalds static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg, 45881da177e4SLinus Torvalds int size) 45891da177e4SLinus Torvalds { 4590be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__WRITE); 45911da177e4SLinus Torvalds } 45921da177e4SLinus Torvalds 45931da177e4SLinus Torvalds static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg, 45941da177e4SLinus Torvalds int size, int flags) 45951da177e4SLinus Torvalds { 4596be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__READ); 45971da177e4SLinus Torvalds } 45981da177e4SLinus Torvalds 45991da177e4SLinus Torvalds static int selinux_socket_getsockname(struct socket *sock) 46001da177e4SLinus Torvalds { 4601be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__GETATTR); 46021da177e4SLinus Torvalds } 46031da177e4SLinus Torvalds 46041da177e4SLinus Torvalds static int selinux_socket_getpeername(struct socket *sock) 46051da177e4SLinus Torvalds { 4606be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__GETATTR); 46071da177e4SLinus Torvalds } 46081da177e4SLinus Torvalds 46091da177e4SLinus Torvalds static int selinux_socket_setsockopt(struct socket *sock, int level, int optname) 46101da177e4SLinus Torvalds { 4611f8687afeSPaul Moore int err; 4612f8687afeSPaul Moore 4613be0554c9SStephen Smalley err = sock_has_perm(sock->sk, SOCKET__SETOPT); 4614f8687afeSPaul Moore if (err) 4615f8687afeSPaul Moore return err; 4616f8687afeSPaul Moore 4617f8687afeSPaul Moore return selinux_netlbl_socket_setsockopt(sock, level, optname); 46181da177e4SLinus Torvalds } 46191da177e4SLinus Torvalds 46201da177e4SLinus Torvalds static int selinux_socket_getsockopt(struct socket *sock, int level, 46211da177e4SLinus Torvalds int optname) 46221da177e4SLinus Torvalds { 4623be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__GETOPT); 46241da177e4SLinus Torvalds } 46251da177e4SLinus Torvalds 46261da177e4SLinus Torvalds static int selinux_socket_shutdown(struct socket *sock, int how) 46271da177e4SLinus Torvalds { 4628be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__SHUTDOWN); 46291da177e4SLinus Torvalds } 46301da177e4SLinus Torvalds 46313610cda5SDavid S. Miller static int selinux_socket_unix_stream_connect(struct sock *sock, 46323610cda5SDavid S. Miller struct sock *other, 46331da177e4SLinus Torvalds struct sock *newsk) 46341da177e4SLinus Torvalds { 46353610cda5SDavid S. Miller struct sk_security_struct *sksec_sock = sock->sk_security; 46363610cda5SDavid S. Miller struct sk_security_struct *sksec_other = other->sk_security; 46374d1e2451SPaul Moore struct sk_security_struct *sksec_new = newsk->sk_security; 46382bf49690SThomas Liu struct common_audit_data ad; 463948c62af6SEric Paris struct lsm_network_audit net = {0,}; 46401da177e4SLinus Torvalds int err; 46411da177e4SLinus Torvalds 464250c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 464348c62af6SEric Paris ad.u.net = &net; 464448c62af6SEric Paris ad.u.net->sk = other; 46451da177e4SLinus Torvalds 46464d1e2451SPaul Moore err = avc_has_perm(sksec_sock->sid, sksec_other->sid, 46474d1e2451SPaul Moore sksec_other->sclass, 46481da177e4SLinus Torvalds UNIX_STREAM_SOCKET__CONNECTTO, &ad); 46491da177e4SLinus Torvalds if (err) 46501da177e4SLinus Torvalds return err; 46511da177e4SLinus Torvalds 46521da177e4SLinus Torvalds /* server child socket */ 46534d1e2451SPaul Moore sksec_new->peer_sid = sksec_sock->sid; 46544d1e2451SPaul Moore err = security_sid_mls_copy(sksec_other->sid, sksec_sock->sid, 46554d1e2451SPaul Moore &sksec_new->sid); 46564d1e2451SPaul Moore if (err) 46574237c75cSVenkat Yekkirala return err; 46584d1e2451SPaul Moore 46594d1e2451SPaul Moore /* connecting socket */ 46604d1e2451SPaul Moore sksec_sock->peer_sid = sksec_new->sid; 46614d1e2451SPaul Moore 46624d1e2451SPaul Moore return 0; 46631da177e4SLinus Torvalds } 46641da177e4SLinus Torvalds 46651da177e4SLinus Torvalds static int selinux_socket_unix_may_send(struct socket *sock, 46661da177e4SLinus Torvalds struct socket *other) 46671da177e4SLinus Torvalds { 4668253bfae6SPaul Moore struct sk_security_struct *ssec = sock->sk->sk_security; 4669253bfae6SPaul Moore struct sk_security_struct *osec = other->sk->sk_security; 46702bf49690SThomas Liu struct common_audit_data ad; 467148c62af6SEric Paris struct lsm_network_audit net = {0,}; 46721da177e4SLinus Torvalds 467350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 467448c62af6SEric Paris ad.u.net = &net; 467548c62af6SEric Paris ad.u.net->sk = other->sk; 46761da177e4SLinus Torvalds 4677253bfae6SPaul Moore return avc_has_perm(ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO, 4678253bfae6SPaul Moore &ad); 46791da177e4SLinus Torvalds } 46801da177e4SLinus Torvalds 4681cbe0d6e8SPaul Moore static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex, 4682cbe0d6e8SPaul Moore char *addrp, u16 family, u32 peer_sid, 46832bf49690SThomas Liu struct common_audit_data *ad) 4684effad8dfSPaul Moore { 4685effad8dfSPaul Moore int err; 4686effad8dfSPaul Moore u32 if_sid; 4687effad8dfSPaul Moore u32 node_sid; 4688effad8dfSPaul Moore 4689cbe0d6e8SPaul Moore err = sel_netif_sid(ns, ifindex, &if_sid); 4690effad8dfSPaul Moore if (err) 4691effad8dfSPaul Moore return err; 4692effad8dfSPaul Moore err = avc_has_perm(peer_sid, if_sid, 4693effad8dfSPaul Moore SECCLASS_NETIF, NETIF__INGRESS, ad); 4694effad8dfSPaul Moore if (err) 4695effad8dfSPaul Moore return err; 4696effad8dfSPaul Moore 4697effad8dfSPaul Moore err = sel_netnode_sid(addrp, family, &node_sid); 4698effad8dfSPaul Moore if (err) 4699effad8dfSPaul Moore return err; 4700effad8dfSPaul Moore return avc_has_perm(peer_sid, node_sid, 4701effad8dfSPaul Moore SECCLASS_NODE, NODE__RECVFROM, ad); 4702effad8dfSPaul Moore } 4703effad8dfSPaul Moore 4704220deb96SPaul Moore static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb, 4705d8395c87SPaul Moore u16 family) 4706220deb96SPaul Moore { 4707277d342fSPaul Moore int err = 0; 4708220deb96SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 4709220deb96SPaul Moore u32 sk_sid = sksec->sid; 47102bf49690SThomas Liu struct common_audit_data ad; 471148c62af6SEric Paris struct lsm_network_audit net = {0,}; 4712d8395c87SPaul Moore char *addrp; 4713d8395c87SPaul Moore 471450c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 471548c62af6SEric Paris ad.u.net = &net; 471648c62af6SEric Paris ad.u.net->netif = skb->skb_iif; 471748c62af6SEric Paris ad.u.net->family = family; 4718d8395c87SPaul Moore err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); 4719d8395c87SPaul Moore if (err) 4720d8395c87SPaul Moore return err; 4721220deb96SPaul Moore 472258bfbb51SPaul Moore if (selinux_secmark_enabled()) { 4723220deb96SPaul Moore err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET, 4724d8395c87SPaul Moore PACKET__RECV, &ad); 4725220deb96SPaul Moore if (err) 4726220deb96SPaul Moore return err; 472758bfbb51SPaul Moore } 4728220deb96SPaul Moore 4729d8395c87SPaul Moore err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad); 4730220deb96SPaul Moore if (err) 4731220deb96SPaul Moore return err; 4732d8395c87SPaul Moore err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad); 4733220deb96SPaul Moore 47344e5ab4cbSJames Morris return err; 47354e5ab4cbSJames Morris } 4736d28d1e08STrent Jaeger 47374e5ab4cbSJames Morris static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) 47384e5ab4cbSJames Morris { 4739220deb96SPaul Moore int err; 47404237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 4741220deb96SPaul Moore u16 family = sk->sk_family; 4742220deb96SPaul Moore u32 sk_sid = sksec->sid; 47432bf49690SThomas Liu struct common_audit_data ad; 474448c62af6SEric Paris struct lsm_network_audit net = {0,}; 4745220deb96SPaul Moore char *addrp; 4746d8395c87SPaul Moore u8 secmark_active; 4747d8395c87SPaul Moore u8 peerlbl_active; 47484e5ab4cbSJames Morris 47494e5ab4cbSJames Morris if (family != PF_INET && family != PF_INET6) 4750220deb96SPaul Moore return 0; 47514e5ab4cbSJames Morris 47524e5ab4cbSJames Morris /* Handle mapped IPv4 packets arriving via IPv6 sockets */ 475387fcd70dSAl Viro if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 47544e5ab4cbSJames Morris family = PF_INET; 47554e5ab4cbSJames Morris 4756d8395c87SPaul Moore /* If any sort of compatibility mode is enabled then handoff processing 4757d8395c87SPaul Moore * to the selinux_sock_rcv_skb_compat() function to deal with the 4758d8395c87SPaul Moore * special handling. We do this in an attempt to keep this function 4759d8395c87SPaul Moore * as fast and as clean as possible. */ 476058bfbb51SPaul Moore if (!selinux_policycap_netpeer) 4761d8395c87SPaul Moore return selinux_sock_rcv_skb_compat(sk, skb, family); 4762d8395c87SPaul Moore 4763d8395c87SPaul Moore secmark_active = selinux_secmark_enabled(); 47642be4d74fSChris PeBenito peerlbl_active = selinux_peerlbl_enabled(); 4765d8395c87SPaul Moore if (!secmark_active && !peerlbl_active) 4766d8395c87SPaul Moore return 0; 4767d8395c87SPaul Moore 476850c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 476948c62af6SEric Paris ad.u.net = &net; 477048c62af6SEric Paris ad.u.net->netif = skb->skb_iif; 477148c62af6SEric Paris ad.u.net->family = family; 4772224dfbd8SPaul Moore err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); 47734e5ab4cbSJames Morris if (err) 4774220deb96SPaul Moore return err; 47754e5ab4cbSJames Morris 4776d8395c87SPaul Moore if (peerlbl_active) { 4777d621d35eSPaul Moore u32 peer_sid; 4778220deb96SPaul Moore 4779220deb96SPaul Moore err = selinux_skb_peerlbl_sid(skb, family, &peer_sid); 4780220deb96SPaul Moore if (err) 4781220deb96SPaul Moore return err; 4782cbe0d6e8SPaul Moore err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif, 4783cbe0d6e8SPaul Moore addrp, family, peer_sid, &ad); 4784dfaebe98SPaul Moore if (err) { 4785a04e71f6SHuw Davies selinux_netlbl_err(skb, family, err, 0); 4786effad8dfSPaul Moore return err; 4787dfaebe98SPaul Moore } 4788d621d35eSPaul Moore err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER, 4789d621d35eSPaul Moore PEER__RECV, &ad); 479046d01d63SChad Hanson if (err) { 4791a04e71f6SHuw Davies selinux_netlbl_err(skb, family, err, 0); 479246d01d63SChad Hanson return err; 479346d01d63SChad Hanson } 4794d621d35eSPaul Moore } 4795d621d35eSPaul Moore 4796d8395c87SPaul Moore if (secmark_active) { 4797effad8dfSPaul Moore err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET, 4798effad8dfSPaul Moore PACKET__RECV, &ad); 4799effad8dfSPaul Moore if (err) 4800effad8dfSPaul Moore return err; 4801effad8dfSPaul Moore } 4802effad8dfSPaul Moore 4803d621d35eSPaul Moore return err; 48041da177e4SLinus Torvalds } 48051da177e4SLinus Torvalds 48062c7946a7SCatherine Zhang static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval, 48071da177e4SLinus Torvalds int __user *optlen, unsigned len) 48081da177e4SLinus Torvalds { 48091da177e4SLinus Torvalds int err = 0; 48101da177e4SLinus Torvalds char *scontext; 48111da177e4SLinus Torvalds u32 scontext_len; 4812253bfae6SPaul Moore struct sk_security_struct *sksec = sock->sk->sk_security; 48133de4bab5SPaul Moore u32 peer_sid = SECSID_NULL; 48141da177e4SLinus Torvalds 4815253bfae6SPaul Moore if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET || 4816253bfae6SPaul Moore sksec->sclass == SECCLASS_TCP_SOCKET) 4817dd3e7836SEric Paris peer_sid = sksec->peer_sid; 4818253bfae6SPaul Moore if (peer_sid == SECSID_NULL) 4819253bfae6SPaul Moore return -ENOPROTOOPT; 48201da177e4SLinus Torvalds 48212c7946a7SCatherine Zhang err = security_sid_to_context(peer_sid, &scontext, &scontext_len); 48221da177e4SLinus Torvalds if (err) 4823253bfae6SPaul Moore return err; 48241da177e4SLinus Torvalds 48251da177e4SLinus Torvalds if (scontext_len > len) { 48261da177e4SLinus Torvalds err = -ERANGE; 48271da177e4SLinus Torvalds goto out_len; 48281da177e4SLinus Torvalds } 48291da177e4SLinus Torvalds 48301da177e4SLinus Torvalds if (copy_to_user(optval, scontext, scontext_len)) 48311da177e4SLinus Torvalds err = -EFAULT; 48321da177e4SLinus Torvalds 48331da177e4SLinus Torvalds out_len: 48341da177e4SLinus Torvalds if (put_user(scontext_len, optlen)) 48351da177e4SLinus Torvalds err = -EFAULT; 48361da177e4SLinus Torvalds kfree(scontext); 48371da177e4SLinus Torvalds return err; 48381da177e4SLinus Torvalds } 48391da177e4SLinus Torvalds 4840dc49c1f9SCatherine Zhang static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid) 48412c7946a7SCatherine Zhang { 4842dc49c1f9SCatherine Zhang u32 peer_secid = SECSID_NULL; 484375e22910SPaul Moore u16 family; 4844899134f2SPaul Moore struct inode_security_struct *isec; 4845877ce7c1SCatherine Zhang 4846aa862900SPaul Moore if (skb && skb->protocol == htons(ETH_P_IP)) 4847aa862900SPaul Moore family = PF_INET; 4848aa862900SPaul Moore else if (skb && skb->protocol == htons(ETH_P_IPV6)) 4849aa862900SPaul Moore family = PF_INET6; 4850aa862900SPaul Moore else if (sock) 485175e22910SPaul Moore family = sock->sk->sk_family; 485275e22910SPaul Moore else 485375e22910SPaul Moore goto out; 485475e22910SPaul Moore 4855899134f2SPaul Moore if (sock && family == PF_UNIX) { 4856899134f2SPaul Moore isec = inode_security_novalidate(SOCK_INODE(sock)); 4857899134f2SPaul Moore peer_secid = isec->sid; 4858899134f2SPaul Moore } else if (skb) 4859220deb96SPaul Moore selinux_skb_peerlbl_sid(skb, family, &peer_secid); 48602c7946a7SCatherine Zhang 486175e22910SPaul Moore out: 4862dc49c1f9SCatherine Zhang *secid = peer_secid; 486375e22910SPaul Moore if (peer_secid == SECSID_NULL) 486475e22910SPaul Moore return -EINVAL; 486575e22910SPaul Moore return 0; 48662c7946a7SCatherine Zhang } 48672c7946a7SCatherine Zhang 48687d877f3bSAl Viro static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority) 48691da177e4SLinus Torvalds { 487084914b7eSPaul Moore struct sk_security_struct *sksec; 487184914b7eSPaul Moore 487284914b7eSPaul Moore sksec = kzalloc(sizeof(*sksec), priority); 487384914b7eSPaul Moore if (!sksec) 487484914b7eSPaul Moore return -ENOMEM; 487584914b7eSPaul Moore 487684914b7eSPaul Moore sksec->peer_sid = SECINITSID_UNLABELED; 487784914b7eSPaul Moore sksec->sid = SECINITSID_UNLABELED; 48785dee25d0SStephen Smalley sksec->sclass = SECCLASS_SOCKET; 487984914b7eSPaul Moore selinux_netlbl_sk_security_reset(sksec); 488084914b7eSPaul Moore sk->sk_security = sksec; 488184914b7eSPaul Moore 488284914b7eSPaul Moore return 0; 48831da177e4SLinus Torvalds } 48841da177e4SLinus Torvalds 48851da177e4SLinus Torvalds static void selinux_sk_free_security(struct sock *sk) 48861da177e4SLinus Torvalds { 488784914b7eSPaul Moore struct sk_security_struct *sksec = sk->sk_security; 488884914b7eSPaul Moore 488984914b7eSPaul Moore sk->sk_security = NULL; 489084914b7eSPaul Moore selinux_netlbl_sk_security_free(sksec); 489184914b7eSPaul Moore kfree(sksec); 48921da177e4SLinus Torvalds } 48931da177e4SLinus Torvalds 4894892c141eSVenkat Yekkirala static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk) 4895892c141eSVenkat Yekkirala { 4896dd3e7836SEric Paris struct sk_security_struct *sksec = sk->sk_security; 4897dd3e7836SEric Paris struct sk_security_struct *newsksec = newsk->sk_security; 4898892c141eSVenkat Yekkirala 4899dd3e7836SEric Paris newsksec->sid = sksec->sid; 4900dd3e7836SEric Paris newsksec->peer_sid = sksec->peer_sid; 4901dd3e7836SEric Paris newsksec->sclass = sksec->sclass; 490299f59ed0SPaul Moore 4903dd3e7836SEric Paris selinux_netlbl_sk_security_reset(newsksec); 4904892c141eSVenkat Yekkirala } 4905892c141eSVenkat Yekkirala 4906beb8d13bSVenkat Yekkirala static void selinux_sk_getsecid(struct sock *sk, u32 *secid) 4907d28d1e08STrent Jaeger { 4908d28d1e08STrent Jaeger if (!sk) 4909beb8d13bSVenkat Yekkirala *secid = SECINITSID_ANY_SOCKET; 4910892c141eSVenkat Yekkirala else { 4911892c141eSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 4912d28d1e08STrent Jaeger 4913beb8d13bSVenkat Yekkirala *secid = sksec->sid; 4914892c141eSVenkat Yekkirala } 4915d28d1e08STrent Jaeger } 4916d28d1e08STrent Jaeger 49179a673e56SAdrian Bunk static void selinux_sock_graft(struct sock *sk, struct socket *parent) 49184237c75cSVenkat Yekkirala { 49195d226df4SAndreas Gruenbacher struct inode_security_struct *isec = 49205d226df4SAndreas Gruenbacher inode_security_novalidate(SOCK_INODE(parent)); 49214237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 49224237c75cSVenkat Yekkirala 49232873ead7SPaul Moore if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 || 49242873ead7SPaul Moore sk->sk_family == PF_UNIX) 49254237c75cSVenkat Yekkirala isec->sid = sksec->sid; 4926220deb96SPaul Moore sksec->sclass = isec->sclass; 49274237c75cSVenkat Yekkirala } 49284237c75cSVenkat Yekkirala 49299a673e56SAdrian Bunk static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb, 49304237c75cSVenkat Yekkirala struct request_sock *req) 49314237c75cSVenkat Yekkirala { 49324237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 49334237c75cSVenkat Yekkirala int err; 49340b1f24e6SPaul Moore u16 family = req->rsk_ops->family; 4935446b8024SPaul Moore u32 connsid; 49364237c75cSVenkat Yekkirala u32 peersid; 49374237c75cSVenkat Yekkirala 4938aa862900SPaul Moore err = selinux_skb_peerlbl_sid(skb, family, &peersid); 4939220deb96SPaul Moore if (err) 4940220deb96SPaul Moore return err; 4941446b8024SPaul Moore err = selinux_conn_sid(sksec->sid, peersid, &connsid); 49424237c75cSVenkat Yekkirala if (err) 49434237c75cSVenkat Yekkirala return err; 4944446b8024SPaul Moore req->secid = connsid; 49456b877699SVenkat Yekkirala req->peer_secid = peersid; 4946389fb800SPaul Moore 4947389fb800SPaul Moore return selinux_netlbl_inet_conn_request(req, family); 49484237c75cSVenkat Yekkirala } 49494237c75cSVenkat Yekkirala 49509a673e56SAdrian Bunk static void selinux_inet_csk_clone(struct sock *newsk, 49519a673e56SAdrian Bunk const struct request_sock *req) 49524237c75cSVenkat Yekkirala { 49534237c75cSVenkat Yekkirala struct sk_security_struct *newsksec = newsk->sk_security; 49544237c75cSVenkat Yekkirala 49554237c75cSVenkat Yekkirala newsksec->sid = req->secid; 49566b877699SVenkat Yekkirala newsksec->peer_sid = req->peer_secid; 49574237c75cSVenkat Yekkirala /* NOTE: Ideally, we should also get the isec->sid for the 49584237c75cSVenkat Yekkirala new socket in sync, but we don't have the isec available yet. 49594237c75cSVenkat Yekkirala So we will wait until sock_graft to do it, by which 49604237c75cSVenkat Yekkirala time it will have been created and available. */ 496199f59ed0SPaul Moore 49629f2ad665SPaul Moore /* We don't need to take any sort of lock here as we are the only 49639f2ad665SPaul Moore * thread with access to newsksec */ 4964389fb800SPaul Moore selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family); 49654237c75cSVenkat Yekkirala } 49664237c75cSVenkat Yekkirala 4967014ab19aSPaul Moore static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb) 49686b877699SVenkat Yekkirala { 4969aa862900SPaul Moore u16 family = sk->sk_family; 49706b877699SVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 49716b877699SVenkat Yekkirala 4972aa862900SPaul Moore /* handle mapped IPv4 packets arriving via IPv6 sockets */ 4973aa862900SPaul Moore if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 4974aa862900SPaul Moore family = PF_INET; 4975aa862900SPaul Moore 4976aa862900SPaul Moore selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid); 49776b877699SVenkat Yekkirala } 49786b877699SVenkat Yekkirala 49792606fd1fSEric Paris static int selinux_secmark_relabel_packet(u32 sid) 49802606fd1fSEric Paris { 49812606fd1fSEric Paris const struct task_security_struct *__tsec; 49822606fd1fSEric Paris u32 tsid; 49832606fd1fSEric Paris 49842606fd1fSEric Paris __tsec = current_security(); 49852606fd1fSEric Paris tsid = __tsec->sid; 49862606fd1fSEric Paris 49872606fd1fSEric Paris return avc_has_perm(tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, NULL); 49882606fd1fSEric Paris } 49892606fd1fSEric Paris 49902606fd1fSEric Paris static void selinux_secmark_refcount_inc(void) 49912606fd1fSEric Paris { 49922606fd1fSEric Paris atomic_inc(&selinux_secmark_refcount); 49932606fd1fSEric Paris } 49942606fd1fSEric Paris 49952606fd1fSEric Paris static void selinux_secmark_refcount_dec(void) 49962606fd1fSEric Paris { 49972606fd1fSEric Paris atomic_dec(&selinux_secmark_refcount); 49982606fd1fSEric Paris } 49992606fd1fSEric Paris 50009a673e56SAdrian Bunk static void selinux_req_classify_flow(const struct request_sock *req, 50019a673e56SAdrian Bunk struct flowi *fl) 50024237c75cSVenkat Yekkirala { 50031d28f42cSDavid S. Miller fl->flowi_secid = req->secid; 50044237c75cSVenkat Yekkirala } 50054237c75cSVenkat Yekkirala 50065dbbaf2dSPaul Moore static int selinux_tun_dev_alloc_security(void **security) 50075dbbaf2dSPaul Moore { 50085dbbaf2dSPaul Moore struct tun_security_struct *tunsec; 50095dbbaf2dSPaul Moore 50105dbbaf2dSPaul Moore tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL); 50115dbbaf2dSPaul Moore if (!tunsec) 50125dbbaf2dSPaul Moore return -ENOMEM; 50135dbbaf2dSPaul Moore tunsec->sid = current_sid(); 50145dbbaf2dSPaul Moore 50155dbbaf2dSPaul Moore *security = tunsec; 50165dbbaf2dSPaul Moore return 0; 50175dbbaf2dSPaul Moore } 50185dbbaf2dSPaul Moore 50195dbbaf2dSPaul Moore static void selinux_tun_dev_free_security(void *security) 50205dbbaf2dSPaul Moore { 50215dbbaf2dSPaul Moore kfree(security); 50225dbbaf2dSPaul Moore } 50235dbbaf2dSPaul Moore 5024ed6d76e4SPaul Moore static int selinux_tun_dev_create(void) 5025ed6d76e4SPaul Moore { 5026ed6d76e4SPaul Moore u32 sid = current_sid(); 5027ed6d76e4SPaul Moore 5028ed6d76e4SPaul Moore /* we aren't taking into account the "sockcreate" SID since the socket 5029ed6d76e4SPaul Moore * that is being created here is not a socket in the traditional sense, 5030ed6d76e4SPaul Moore * instead it is a private sock, accessible only to the kernel, and 5031ed6d76e4SPaul Moore * representing a wide range of network traffic spanning multiple 5032ed6d76e4SPaul Moore * connections unlike traditional sockets - check the TUN driver to 5033ed6d76e4SPaul Moore * get a better understanding of why this socket is special */ 5034ed6d76e4SPaul Moore 5035ed6d76e4SPaul Moore return avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE, 5036ed6d76e4SPaul Moore NULL); 5037ed6d76e4SPaul Moore } 5038ed6d76e4SPaul Moore 50395dbbaf2dSPaul Moore static int selinux_tun_dev_attach_queue(void *security) 5040ed6d76e4SPaul Moore { 50415dbbaf2dSPaul Moore struct tun_security_struct *tunsec = security; 50425dbbaf2dSPaul Moore 50435dbbaf2dSPaul Moore return avc_has_perm(current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET, 50445dbbaf2dSPaul Moore TUN_SOCKET__ATTACH_QUEUE, NULL); 50455dbbaf2dSPaul Moore } 50465dbbaf2dSPaul Moore 50475dbbaf2dSPaul Moore static int selinux_tun_dev_attach(struct sock *sk, void *security) 50485dbbaf2dSPaul Moore { 50495dbbaf2dSPaul Moore struct tun_security_struct *tunsec = security; 5050ed6d76e4SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 5051ed6d76e4SPaul Moore 5052ed6d76e4SPaul Moore /* we don't currently perform any NetLabel based labeling here and it 5053ed6d76e4SPaul Moore * isn't clear that we would want to do so anyway; while we could apply 5054ed6d76e4SPaul Moore * labeling without the support of the TUN user the resulting labeled 5055ed6d76e4SPaul Moore * traffic from the other end of the connection would almost certainly 5056ed6d76e4SPaul Moore * cause confusion to the TUN user that had no idea network labeling 5057ed6d76e4SPaul Moore * protocols were being used */ 5058ed6d76e4SPaul Moore 50595dbbaf2dSPaul Moore sksec->sid = tunsec->sid; 5060ed6d76e4SPaul Moore sksec->sclass = SECCLASS_TUN_SOCKET; 50615dbbaf2dSPaul Moore 50625dbbaf2dSPaul Moore return 0; 5063ed6d76e4SPaul Moore } 5064ed6d76e4SPaul Moore 50655dbbaf2dSPaul Moore static int selinux_tun_dev_open(void *security) 5066ed6d76e4SPaul Moore { 50675dbbaf2dSPaul Moore struct tun_security_struct *tunsec = security; 5068ed6d76e4SPaul Moore u32 sid = current_sid(); 5069ed6d76e4SPaul Moore int err; 5070ed6d76e4SPaul Moore 50715dbbaf2dSPaul Moore err = avc_has_perm(sid, tunsec->sid, SECCLASS_TUN_SOCKET, 5072ed6d76e4SPaul Moore TUN_SOCKET__RELABELFROM, NULL); 5073ed6d76e4SPaul Moore if (err) 5074ed6d76e4SPaul Moore return err; 5075ed6d76e4SPaul Moore err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, 5076ed6d76e4SPaul Moore TUN_SOCKET__RELABELTO, NULL); 5077ed6d76e4SPaul Moore if (err) 5078ed6d76e4SPaul Moore return err; 50795dbbaf2dSPaul Moore tunsec->sid = sid; 5080ed6d76e4SPaul Moore 5081ed6d76e4SPaul Moore return 0; 5082ed6d76e4SPaul Moore } 5083ed6d76e4SPaul Moore 50841da177e4SLinus Torvalds static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb) 50851da177e4SLinus Torvalds { 50861da177e4SLinus Torvalds int err = 0; 50871da177e4SLinus Torvalds u32 perm; 50881da177e4SLinus Torvalds struct nlmsghdr *nlh; 5089253bfae6SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 50901da177e4SLinus Torvalds 509177954983SHong zhi guo if (skb->len < NLMSG_HDRLEN) { 50921da177e4SLinus Torvalds err = -EINVAL; 50931da177e4SLinus Torvalds goto out; 50941da177e4SLinus Torvalds } 5095b529ccf2SArnaldo Carvalho de Melo nlh = nlmsg_hdr(skb); 50961da177e4SLinus Torvalds 5097253bfae6SPaul Moore err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm); 50981da177e4SLinus Torvalds if (err) { 50991da177e4SLinus Torvalds if (err == -EINVAL) { 510076319946SVladis Dronov pr_warn_ratelimited("SELinux: unrecognized netlink" 510176319946SVladis Dronov " message: protocol=%hu nlmsg_type=%hu sclass=%s" 510276319946SVladis Dronov " pig=%d comm=%s\n", 5103cded3fffSMarek Milkovic sk->sk_protocol, nlh->nlmsg_type, 510476319946SVladis Dronov secclass_map[sksec->sclass - 1].name, 510576319946SVladis Dronov task_pid_nr(current), current->comm); 510639c9aedeSEric Paris if (!selinux_enforcing || security_get_allow_unknown()) 51071da177e4SLinus Torvalds err = 0; 51081da177e4SLinus Torvalds } 51091da177e4SLinus Torvalds 51101da177e4SLinus Torvalds /* Ignore */ 51111da177e4SLinus Torvalds if (err == -ENOENT) 51121da177e4SLinus Torvalds err = 0; 51131da177e4SLinus Torvalds goto out; 51141da177e4SLinus Torvalds } 51151da177e4SLinus Torvalds 5116be0554c9SStephen Smalley err = sock_has_perm(sk, perm); 51171da177e4SLinus Torvalds out: 51181da177e4SLinus Torvalds return err; 51191da177e4SLinus Torvalds } 51201da177e4SLinus Torvalds 51211da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER 51221da177e4SLinus Torvalds 5123cbe0d6e8SPaul Moore static unsigned int selinux_ip_forward(struct sk_buff *skb, 5124cbe0d6e8SPaul Moore const struct net_device *indev, 5125effad8dfSPaul Moore u16 family) 51261da177e4SLinus Torvalds { 5127dfaebe98SPaul Moore int err; 5128effad8dfSPaul Moore char *addrp; 5129effad8dfSPaul Moore u32 peer_sid; 51302bf49690SThomas Liu struct common_audit_data ad; 513148c62af6SEric Paris struct lsm_network_audit net = {0,}; 5132effad8dfSPaul Moore u8 secmark_active; 5133948bf85cSPaul Moore u8 netlbl_active; 5134effad8dfSPaul Moore u8 peerlbl_active; 51354237c75cSVenkat Yekkirala 5136effad8dfSPaul Moore if (!selinux_policycap_netpeer) 5137effad8dfSPaul Moore return NF_ACCEPT; 51384237c75cSVenkat Yekkirala 5139effad8dfSPaul Moore secmark_active = selinux_secmark_enabled(); 5140948bf85cSPaul Moore netlbl_active = netlbl_enabled(); 51412be4d74fSChris PeBenito peerlbl_active = selinux_peerlbl_enabled(); 5142effad8dfSPaul Moore if (!secmark_active && !peerlbl_active) 5143effad8dfSPaul Moore return NF_ACCEPT; 51444237c75cSVenkat Yekkirala 5145d8395c87SPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0) 5146d8395c87SPaul Moore return NF_DROP; 5147d8395c87SPaul Moore 514850c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 514948c62af6SEric Paris ad.u.net = &net; 5150cbe0d6e8SPaul Moore ad.u.net->netif = indev->ifindex; 515148c62af6SEric Paris ad.u.net->family = family; 5152effad8dfSPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0) 5153effad8dfSPaul Moore return NF_DROP; 51541da177e4SLinus Torvalds 5155dfaebe98SPaul Moore if (peerlbl_active) { 5156cbe0d6e8SPaul Moore err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex, 5157cbe0d6e8SPaul Moore addrp, family, peer_sid, &ad); 5158dfaebe98SPaul Moore if (err) { 5159a04e71f6SHuw Davies selinux_netlbl_err(skb, family, err, 1); 5160effad8dfSPaul Moore return NF_DROP; 5161dfaebe98SPaul Moore } 5162dfaebe98SPaul Moore } 5163effad8dfSPaul Moore 5164effad8dfSPaul Moore if (secmark_active) 5165effad8dfSPaul Moore if (avc_has_perm(peer_sid, skb->secmark, 5166effad8dfSPaul Moore SECCLASS_PACKET, PACKET__FORWARD_IN, &ad)) 5167effad8dfSPaul Moore return NF_DROP; 5168effad8dfSPaul Moore 5169948bf85cSPaul Moore if (netlbl_active) 5170948bf85cSPaul Moore /* we do this in the FORWARD path and not the POST_ROUTING 5171948bf85cSPaul Moore * path because we want to make sure we apply the necessary 5172948bf85cSPaul Moore * labeling before IPsec is applied so we can leverage AH 5173948bf85cSPaul Moore * protection */ 5174948bf85cSPaul Moore if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0) 5175948bf85cSPaul Moore return NF_DROP; 5176948bf85cSPaul Moore 5177effad8dfSPaul Moore return NF_ACCEPT; 5178effad8dfSPaul Moore } 5179effad8dfSPaul Moore 518006198b34SEric W. Biederman static unsigned int selinux_ipv4_forward(void *priv, 5181effad8dfSPaul Moore struct sk_buff *skb, 5182238e54c9SDavid S. Miller const struct nf_hook_state *state) 5183effad8dfSPaul Moore { 5184238e54c9SDavid S. Miller return selinux_ip_forward(skb, state->in, PF_INET); 5185effad8dfSPaul Moore } 5186effad8dfSPaul Moore 51871a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 518806198b34SEric W. Biederman static unsigned int selinux_ipv6_forward(void *priv, 5189effad8dfSPaul Moore struct sk_buff *skb, 5190238e54c9SDavid S. Miller const struct nf_hook_state *state) 5191effad8dfSPaul Moore { 5192238e54c9SDavid S. Miller return selinux_ip_forward(skb, state->in, PF_INET6); 5193effad8dfSPaul Moore } 5194effad8dfSPaul Moore #endif /* IPV6 */ 5195effad8dfSPaul Moore 5196948bf85cSPaul Moore static unsigned int selinux_ip_output(struct sk_buff *skb, 5197948bf85cSPaul Moore u16 family) 5198948bf85cSPaul Moore { 519947180068SPaul Moore struct sock *sk; 5200948bf85cSPaul Moore u32 sid; 5201948bf85cSPaul Moore 5202948bf85cSPaul Moore if (!netlbl_enabled()) 5203948bf85cSPaul Moore return NF_ACCEPT; 5204948bf85cSPaul Moore 5205948bf85cSPaul Moore /* we do this in the LOCAL_OUT path and not the POST_ROUTING path 5206948bf85cSPaul Moore * because we want to make sure we apply the necessary labeling 5207948bf85cSPaul Moore * before IPsec is applied so we can leverage AH protection */ 520847180068SPaul Moore sk = skb->sk; 520947180068SPaul Moore if (sk) { 521047180068SPaul Moore struct sk_security_struct *sksec; 521147180068SPaul Moore 5212e446f9dfSEric Dumazet if (sk_listener(sk)) 521347180068SPaul Moore /* if the socket is the listening state then this 521447180068SPaul Moore * packet is a SYN-ACK packet which means it needs to 521547180068SPaul Moore * be labeled based on the connection/request_sock and 521647180068SPaul Moore * not the parent socket. unfortunately, we can't 521747180068SPaul Moore * lookup the request_sock yet as it isn't queued on 521847180068SPaul Moore * the parent socket until after the SYN-ACK is sent. 521947180068SPaul Moore * the "solution" is to simply pass the packet as-is 522047180068SPaul Moore * as any IP option based labeling should be copied 522147180068SPaul Moore * from the initial connection request (in the IP 522247180068SPaul Moore * layer). it is far from ideal, but until we get a 522347180068SPaul Moore * security label in the packet itself this is the 522447180068SPaul Moore * best we can do. */ 522547180068SPaul Moore return NF_ACCEPT; 522647180068SPaul Moore 522747180068SPaul Moore /* standard practice, label using the parent socket */ 522847180068SPaul Moore sksec = sk->sk_security; 5229948bf85cSPaul Moore sid = sksec->sid; 5230948bf85cSPaul Moore } else 5231948bf85cSPaul Moore sid = SECINITSID_KERNEL; 5232948bf85cSPaul Moore if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0) 5233948bf85cSPaul Moore return NF_DROP; 5234948bf85cSPaul Moore 5235948bf85cSPaul Moore return NF_ACCEPT; 5236948bf85cSPaul Moore } 5237948bf85cSPaul Moore 523806198b34SEric W. Biederman static unsigned int selinux_ipv4_output(void *priv, 5239948bf85cSPaul Moore struct sk_buff *skb, 5240238e54c9SDavid S. Miller const struct nf_hook_state *state) 5241948bf85cSPaul Moore { 5242948bf85cSPaul Moore return selinux_ip_output(skb, PF_INET); 5243948bf85cSPaul Moore } 5244948bf85cSPaul Moore 52451a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 52462917f57bSHuw Davies static unsigned int selinux_ipv6_output(void *priv, 52472917f57bSHuw Davies struct sk_buff *skb, 52482917f57bSHuw Davies const struct nf_hook_state *state) 52492917f57bSHuw Davies { 52502917f57bSHuw Davies return selinux_ip_output(skb, PF_INET6); 52512917f57bSHuw Davies } 52522917f57bSHuw Davies #endif /* IPV6 */ 52532917f57bSHuw Davies 5254effad8dfSPaul Moore static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb, 5255effad8dfSPaul Moore int ifindex, 5256d8395c87SPaul Moore u16 family) 52574e5ab4cbSJames Morris { 525854abc686SEric Dumazet struct sock *sk = skb_to_full_sk(skb); 52594237c75cSVenkat Yekkirala struct sk_security_struct *sksec; 52602bf49690SThomas Liu struct common_audit_data ad; 526148c62af6SEric Paris struct lsm_network_audit net = {0,}; 5262d8395c87SPaul Moore char *addrp; 5263d8395c87SPaul Moore u8 proto; 52644e5ab4cbSJames Morris 5265effad8dfSPaul Moore if (sk == NULL) 5266effad8dfSPaul Moore return NF_ACCEPT; 52674237c75cSVenkat Yekkirala sksec = sk->sk_security; 52684e5ab4cbSJames Morris 526950c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 527048c62af6SEric Paris ad.u.net = &net; 527148c62af6SEric Paris ad.u.net->netif = ifindex; 527248c62af6SEric Paris ad.u.net->family = family; 5273d8395c87SPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto)) 5274d8395c87SPaul Moore return NF_DROP; 5275d8395c87SPaul Moore 527658bfbb51SPaul Moore if (selinux_secmark_enabled()) 5277effad8dfSPaul Moore if (avc_has_perm(sksec->sid, skb->secmark, 5278d8395c87SPaul Moore SECCLASS_PACKET, PACKET__SEND, &ad)) 52792fe66ec2SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 52801da177e4SLinus Torvalds 5281d8395c87SPaul Moore if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto)) 52822fe66ec2SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 5283effad8dfSPaul Moore 5284effad8dfSPaul Moore return NF_ACCEPT; 5285effad8dfSPaul Moore } 5286effad8dfSPaul Moore 5287cbe0d6e8SPaul Moore static unsigned int selinux_ip_postroute(struct sk_buff *skb, 5288cbe0d6e8SPaul Moore const struct net_device *outdev, 5289effad8dfSPaul Moore u16 family) 5290effad8dfSPaul Moore { 5291effad8dfSPaul Moore u32 secmark_perm; 5292effad8dfSPaul Moore u32 peer_sid; 5293cbe0d6e8SPaul Moore int ifindex = outdev->ifindex; 5294effad8dfSPaul Moore struct sock *sk; 52952bf49690SThomas Liu struct common_audit_data ad; 529648c62af6SEric Paris struct lsm_network_audit net = {0,}; 5297effad8dfSPaul Moore char *addrp; 5298effad8dfSPaul Moore u8 secmark_active; 5299effad8dfSPaul Moore u8 peerlbl_active; 5300effad8dfSPaul Moore 5301effad8dfSPaul Moore /* If any sort of compatibility mode is enabled then handoff processing 5302effad8dfSPaul Moore * to the selinux_ip_postroute_compat() function to deal with the 5303effad8dfSPaul Moore * special handling. We do this in an attempt to keep this function 5304effad8dfSPaul Moore * as fast and as clean as possible. */ 530558bfbb51SPaul Moore if (!selinux_policycap_netpeer) 5306d8395c87SPaul Moore return selinux_ip_postroute_compat(skb, ifindex, family); 5307c0828e50SPaul Moore 5308effad8dfSPaul Moore secmark_active = selinux_secmark_enabled(); 53092be4d74fSChris PeBenito peerlbl_active = selinux_peerlbl_enabled(); 5310effad8dfSPaul Moore if (!secmark_active && !peerlbl_active) 5311effad8dfSPaul Moore return NF_ACCEPT; 5312effad8dfSPaul Moore 531354abc686SEric Dumazet sk = skb_to_full_sk(skb); 5314c0828e50SPaul Moore 5315effad8dfSPaul Moore #ifdef CONFIG_XFRM 5316effad8dfSPaul Moore /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec 5317effad8dfSPaul Moore * packet transformation so allow the packet to pass without any checks 5318effad8dfSPaul Moore * since we'll have another chance to perform access control checks 5319effad8dfSPaul Moore * when the packet is on it's final way out. 5320effad8dfSPaul Moore * NOTE: there appear to be some IPv6 multicast cases where skb->dst 5321c0828e50SPaul Moore * is NULL, in this case go ahead and apply access control. 5322c0828e50SPaul Moore * NOTE: if this is a local socket (skb->sk != NULL) that is in the 5323c0828e50SPaul Moore * TCP listening state we cannot wait until the XFRM processing 5324c0828e50SPaul Moore * is done as we will miss out on the SA label if we do; 5325c0828e50SPaul Moore * unfortunately, this means more work, but it is only once per 5326c0828e50SPaul Moore * connection. */ 5327c0828e50SPaul Moore if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL && 5328e446f9dfSEric Dumazet !(sk && sk_listener(sk))) 5329effad8dfSPaul Moore return NF_ACCEPT; 5330effad8dfSPaul Moore #endif 5331effad8dfSPaul Moore 5332d8395c87SPaul Moore if (sk == NULL) { 5333446b8024SPaul Moore /* Without an associated socket the packet is either coming 5334446b8024SPaul Moore * from the kernel or it is being forwarded; check the packet 5335446b8024SPaul Moore * to determine which and if the packet is being forwarded 5336446b8024SPaul Moore * query the packet directly to determine the security label. */ 53374a7ab3dcSSteffen Klassert if (skb->skb_iif) { 5338d8395c87SPaul Moore secmark_perm = PACKET__FORWARD_OUT; 5339d8395c87SPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &peer_sid)) 534004f6d70fSEric Paris return NF_DROP; 53414a7ab3dcSSteffen Klassert } else { 53424a7ab3dcSSteffen Klassert secmark_perm = PACKET__SEND; 5343d8395c87SPaul Moore peer_sid = SECINITSID_KERNEL; 53444a7ab3dcSSteffen Klassert } 5345e446f9dfSEric Dumazet } else if (sk_listener(sk)) { 5346446b8024SPaul Moore /* Locally generated packet but the associated socket is in the 5347446b8024SPaul Moore * listening state which means this is a SYN-ACK packet. In 5348446b8024SPaul Moore * this particular case the correct security label is assigned 5349446b8024SPaul Moore * to the connection/request_sock but unfortunately we can't 5350446b8024SPaul Moore * query the request_sock as it isn't queued on the parent 5351446b8024SPaul Moore * socket until after the SYN-ACK packet is sent; the only 5352446b8024SPaul Moore * viable choice is to regenerate the label like we do in 5353446b8024SPaul Moore * selinux_inet_conn_request(). See also selinux_ip_output() 5354446b8024SPaul Moore * for similar problems. */ 5355446b8024SPaul Moore u32 skb_sid; 5356e446f9dfSEric Dumazet struct sk_security_struct *sksec; 5357e446f9dfSEric Dumazet 5358e446f9dfSEric Dumazet sksec = sk->sk_security; 5359446b8024SPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &skb_sid)) 5360446b8024SPaul Moore return NF_DROP; 5361c0828e50SPaul Moore /* At this point, if the returned skb peerlbl is SECSID_NULL 5362c0828e50SPaul Moore * and the packet has been through at least one XFRM 5363c0828e50SPaul Moore * transformation then we must be dealing with the "final" 5364c0828e50SPaul Moore * form of labeled IPsec packet; since we've already applied 5365c0828e50SPaul Moore * all of our access controls on this packet we can safely 5366c0828e50SPaul Moore * pass the packet. */ 5367c0828e50SPaul Moore if (skb_sid == SECSID_NULL) { 5368c0828e50SPaul Moore switch (family) { 5369c0828e50SPaul Moore case PF_INET: 5370c0828e50SPaul Moore if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) 5371c0828e50SPaul Moore return NF_ACCEPT; 5372c0828e50SPaul Moore break; 5373c0828e50SPaul Moore case PF_INET6: 5374c0828e50SPaul Moore if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) 5375c0828e50SPaul Moore return NF_ACCEPT; 5376a7a91a19SPaul Moore break; 5377c0828e50SPaul Moore default: 5378c0828e50SPaul Moore return NF_DROP_ERR(-ECONNREFUSED); 5379c0828e50SPaul Moore } 5380c0828e50SPaul Moore } 5381446b8024SPaul Moore if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid)) 5382446b8024SPaul Moore return NF_DROP; 5383446b8024SPaul Moore secmark_perm = PACKET__SEND; 5384d8395c87SPaul Moore } else { 5385446b8024SPaul Moore /* Locally generated packet, fetch the security label from the 5386446b8024SPaul Moore * associated socket. */ 5387effad8dfSPaul Moore struct sk_security_struct *sksec = sk->sk_security; 5388effad8dfSPaul Moore peer_sid = sksec->sid; 5389effad8dfSPaul Moore secmark_perm = PACKET__SEND; 5390effad8dfSPaul Moore } 5391effad8dfSPaul Moore 539250c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 539348c62af6SEric Paris ad.u.net = &net; 539448c62af6SEric Paris ad.u.net->netif = ifindex; 539548c62af6SEric Paris ad.u.net->family = family; 5396d8395c87SPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL)) 539704f6d70fSEric Paris return NF_DROP; 5398d8395c87SPaul Moore 5399effad8dfSPaul Moore if (secmark_active) 5400effad8dfSPaul Moore if (avc_has_perm(peer_sid, skb->secmark, 5401effad8dfSPaul Moore SECCLASS_PACKET, secmark_perm, &ad)) 54021f1aaf82SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 5403effad8dfSPaul Moore 5404effad8dfSPaul Moore if (peerlbl_active) { 5405effad8dfSPaul Moore u32 if_sid; 5406effad8dfSPaul Moore u32 node_sid; 5407effad8dfSPaul Moore 5408cbe0d6e8SPaul Moore if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid)) 540904f6d70fSEric Paris return NF_DROP; 5410effad8dfSPaul Moore if (avc_has_perm(peer_sid, if_sid, 5411effad8dfSPaul Moore SECCLASS_NETIF, NETIF__EGRESS, &ad)) 54121f1aaf82SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 5413effad8dfSPaul Moore 5414effad8dfSPaul Moore if (sel_netnode_sid(addrp, family, &node_sid)) 541504f6d70fSEric Paris return NF_DROP; 5416effad8dfSPaul Moore if (avc_has_perm(peer_sid, node_sid, 5417effad8dfSPaul Moore SECCLASS_NODE, NODE__SENDTO, &ad)) 54181f1aaf82SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 5419effad8dfSPaul Moore } 5420effad8dfSPaul Moore 5421effad8dfSPaul Moore return NF_ACCEPT; 5422effad8dfSPaul Moore } 5423effad8dfSPaul Moore 542406198b34SEric W. Biederman static unsigned int selinux_ipv4_postroute(void *priv, 5425a224be76SDavid S. Miller struct sk_buff *skb, 5426238e54c9SDavid S. Miller const struct nf_hook_state *state) 54271da177e4SLinus Torvalds { 5428238e54c9SDavid S. Miller return selinux_ip_postroute(skb, state->out, PF_INET); 54291da177e4SLinus Torvalds } 54301da177e4SLinus Torvalds 54311a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 543206198b34SEric W. Biederman static unsigned int selinux_ipv6_postroute(void *priv, 5433a224be76SDavid S. Miller struct sk_buff *skb, 5434238e54c9SDavid S. Miller const struct nf_hook_state *state) 54351da177e4SLinus Torvalds { 5436238e54c9SDavid S. Miller return selinux_ip_postroute(skb, state->out, PF_INET6); 54371da177e4SLinus Torvalds } 54381da177e4SLinus Torvalds #endif /* IPV6 */ 54391da177e4SLinus Torvalds 54401da177e4SLinus Torvalds #endif /* CONFIG_NETFILTER */ 54411da177e4SLinus Torvalds 54421da177e4SLinus Torvalds static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb) 54431da177e4SLinus Torvalds { 5444941fc5b2SStephen Smalley return selinux_nlmsg_perm(sk, skb); 54451da177e4SLinus Torvalds } 54461da177e4SLinus Torvalds 5447be0554c9SStephen Smalley static int ipc_alloc_security(struct kern_ipc_perm *perm, 54481da177e4SLinus Torvalds u16 sclass) 54491da177e4SLinus Torvalds { 54501da177e4SLinus Torvalds struct ipc_security_struct *isec; 54511da177e4SLinus Torvalds 545289d155efSJames Morris isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL); 54531da177e4SLinus Torvalds if (!isec) 54541da177e4SLinus Torvalds return -ENOMEM; 54551da177e4SLinus Torvalds 54561da177e4SLinus Torvalds isec->sclass = sclass; 5457be0554c9SStephen Smalley isec->sid = current_sid(); 54581da177e4SLinus Torvalds perm->security = isec; 54591da177e4SLinus Torvalds 54601da177e4SLinus Torvalds return 0; 54611da177e4SLinus Torvalds } 54621da177e4SLinus Torvalds 54631da177e4SLinus Torvalds static void ipc_free_security(struct kern_ipc_perm *perm) 54641da177e4SLinus Torvalds { 54651da177e4SLinus Torvalds struct ipc_security_struct *isec = perm->security; 54661da177e4SLinus Torvalds perm->security = NULL; 54671da177e4SLinus Torvalds kfree(isec); 54681da177e4SLinus Torvalds } 54691da177e4SLinus Torvalds 54701da177e4SLinus Torvalds static int msg_msg_alloc_security(struct msg_msg *msg) 54711da177e4SLinus Torvalds { 54721da177e4SLinus Torvalds struct msg_security_struct *msec; 54731da177e4SLinus Torvalds 547489d155efSJames Morris msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL); 54751da177e4SLinus Torvalds if (!msec) 54761da177e4SLinus Torvalds return -ENOMEM; 54771da177e4SLinus Torvalds 54781da177e4SLinus Torvalds msec->sid = SECINITSID_UNLABELED; 54791da177e4SLinus Torvalds msg->security = msec; 54801da177e4SLinus Torvalds 54811da177e4SLinus Torvalds return 0; 54821da177e4SLinus Torvalds } 54831da177e4SLinus Torvalds 54841da177e4SLinus Torvalds static void msg_msg_free_security(struct msg_msg *msg) 54851da177e4SLinus Torvalds { 54861da177e4SLinus Torvalds struct msg_security_struct *msec = msg->security; 54871da177e4SLinus Torvalds 54881da177e4SLinus Torvalds msg->security = NULL; 54891da177e4SLinus Torvalds kfree(msec); 54901da177e4SLinus Torvalds } 54911da177e4SLinus Torvalds 54921da177e4SLinus Torvalds static int ipc_has_perm(struct kern_ipc_perm *ipc_perms, 54936af963f1SStephen Smalley u32 perms) 54941da177e4SLinus Torvalds { 54951da177e4SLinus Torvalds struct ipc_security_struct *isec; 54962bf49690SThomas Liu struct common_audit_data ad; 5497275bb41eSDavid Howells u32 sid = current_sid(); 54981da177e4SLinus Torvalds 54991da177e4SLinus Torvalds isec = ipc_perms->security; 55001da177e4SLinus Torvalds 550150c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 55021da177e4SLinus Torvalds ad.u.ipc_id = ipc_perms->key; 55031da177e4SLinus Torvalds 5504275bb41eSDavid Howells return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad); 55051da177e4SLinus Torvalds } 55061da177e4SLinus Torvalds 55071da177e4SLinus Torvalds static int selinux_msg_msg_alloc_security(struct msg_msg *msg) 55081da177e4SLinus Torvalds { 55091da177e4SLinus Torvalds return msg_msg_alloc_security(msg); 55101da177e4SLinus Torvalds } 55111da177e4SLinus Torvalds 55121da177e4SLinus Torvalds static void selinux_msg_msg_free_security(struct msg_msg *msg) 55131da177e4SLinus Torvalds { 55141da177e4SLinus Torvalds msg_msg_free_security(msg); 55151da177e4SLinus Torvalds } 55161da177e4SLinus Torvalds 55171da177e4SLinus Torvalds /* message queue security operations */ 55181da177e4SLinus Torvalds static int selinux_msg_queue_alloc_security(struct msg_queue *msq) 55191da177e4SLinus Torvalds { 55201da177e4SLinus Torvalds struct ipc_security_struct *isec; 55212bf49690SThomas Liu struct common_audit_data ad; 5522275bb41eSDavid Howells u32 sid = current_sid(); 55231da177e4SLinus Torvalds int rc; 55241da177e4SLinus Torvalds 5525be0554c9SStephen Smalley rc = ipc_alloc_security(&msq->q_perm, SECCLASS_MSGQ); 55261da177e4SLinus Torvalds if (rc) 55271da177e4SLinus Torvalds return rc; 55281da177e4SLinus Torvalds 55291da177e4SLinus Torvalds isec = msq->q_perm.security; 55301da177e4SLinus Torvalds 553150c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 55321da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 55331da177e4SLinus Torvalds 5534275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ, 55351da177e4SLinus Torvalds MSGQ__CREATE, &ad); 55361da177e4SLinus Torvalds if (rc) { 55371da177e4SLinus Torvalds ipc_free_security(&msq->q_perm); 55381da177e4SLinus Torvalds return rc; 55391da177e4SLinus Torvalds } 55401da177e4SLinus Torvalds return 0; 55411da177e4SLinus Torvalds } 55421da177e4SLinus Torvalds 55431da177e4SLinus Torvalds static void selinux_msg_queue_free_security(struct msg_queue *msq) 55441da177e4SLinus Torvalds { 55451da177e4SLinus Torvalds ipc_free_security(&msq->q_perm); 55461da177e4SLinus Torvalds } 55471da177e4SLinus Torvalds 55481da177e4SLinus Torvalds static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg) 55491da177e4SLinus Torvalds { 55501da177e4SLinus Torvalds struct ipc_security_struct *isec; 55512bf49690SThomas Liu struct common_audit_data ad; 5552275bb41eSDavid Howells u32 sid = current_sid(); 55531da177e4SLinus Torvalds 55541da177e4SLinus Torvalds isec = msq->q_perm.security; 55551da177e4SLinus Torvalds 555650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 55571da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 55581da177e4SLinus Torvalds 5559275bb41eSDavid Howells return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ, 55601da177e4SLinus Torvalds MSGQ__ASSOCIATE, &ad); 55611da177e4SLinus Torvalds } 55621da177e4SLinus Torvalds 55631da177e4SLinus Torvalds static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd) 55641da177e4SLinus Torvalds { 55651da177e4SLinus Torvalds int err; 55661da177e4SLinus Torvalds int perms; 55671da177e4SLinus Torvalds 55681da177e4SLinus Torvalds switch (cmd) { 55691da177e4SLinus Torvalds case IPC_INFO: 55701da177e4SLinus Torvalds case MSG_INFO: 55711da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 5572be0554c9SStephen Smalley return avc_has_perm(current_sid(), SECINITSID_KERNEL, 5573be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); 55741da177e4SLinus Torvalds case IPC_STAT: 55751da177e4SLinus Torvalds case MSG_STAT: 55761da177e4SLinus Torvalds perms = MSGQ__GETATTR | MSGQ__ASSOCIATE; 55771da177e4SLinus Torvalds break; 55781da177e4SLinus Torvalds case IPC_SET: 55791da177e4SLinus Torvalds perms = MSGQ__SETATTR; 55801da177e4SLinus Torvalds break; 55811da177e4SLinus Torvalds case IPC_RMID: 55821da177e4SLinus Torvalds perms = MSGQ__DESTROY; 55831da177e4SLinus Torvalds break; 55841da177e4SLinus Torvalds default: 55851da177e4SLinus Torvalds return 0; 55861da177e4SLinus Torvalds } 55871da177e4SLinus Torvalds 55886af963f1SStephen Smalley err = ipc_has_perm(&msq->q_perm, perms); 55891da177e4SLinus Torvalds return err; 55901da177e4SLinus Torvalds } 55911da177e4SLinus Torvalds 55921da177e4SLinus Torvalds static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg) 55931da177e4SLinus Torvalds { 55941da177e4SLinus Torvalds struct ipc_security_struct *isec; 55951da177e4SLinus Torvalds struct msg_security_struct *msec; 55962bf49690SThomas Liu struct common_audit_data ad; 5597275bb41eSDavid Howells u32 sid = current_sid(); 55981da177e4SLinus Torvalds int rc; 55991da177e4SLinus Torvalds 56001da177e4SLinus Torvalds isec = msq->q_perm.security; 56011da177e4SLinus Torvalds msec = msg->security; 56021da177e4SLinus Torvalds 56031da177e4SLinus Torvalds /* 56041da177e4SLinus Torvalds * First time through, need to assign label to the message 56051da177e4SLinus Torvalds */ 56061da177e4SLinus Torvalds if (msec->sid == SECINITSID_UNLABELED) { 56071da177e4SLinus Torvalds /* 56081da177e4SLinus Torvalds * Compute new sid based on current process and 56091da177e4SLinus Torvalds * message queue this message will be stored in 56101da177e4SLinus Torvalds */ 5611275bb41eSDavid Howells rc = security_transition_sid(sid, isec->sid, SECCLASS_MSG, 5612652bb9b0SEric Paris NULL, &msec->sid); 56131da177e4SLinus Torvalds if (rc) 56141da177e4SLinus Torvalds return rc; 56151da177e4SLinus Torvalds } 56161da177e4SLinus Torvalds 561750c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 56181da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 56191da177e4SLinus Torvalds 56201da177e4SLinus Torvalds /* Can this process write to the queue? */ 5621275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ, 56221da177e4SLinus Torvalds MSGQ__WRITE, &ad); 56231da177e4SLinus Torvalds if (!rc) 56241da177e4SLinus Torvalds /* Can this process send the message */ 5625275bb41eSDavid Howells rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG, 5626275bb41eSDavid Howells MSG__SEND, &ad); 56271da177e4SLinus Torvalds if (!rc) 56281da177e4SLinus Torvalds /* Can the message be put in the queue? */ 5629275bb41eSDavid Howells rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ, 5630275bb41eSDavid Howells MSGQ__ENQUEUE, &ad); 56311da177e4SLinus Torvalds 56321da177e4SLinus Torvalds return rc; 56331da177e4SLinus Torvalds } 56341da177e4SLinus Torvalds 56351da177e4SLinus Torvalds static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg, 56361da177e4SLinus Torvalds struct task_struct *target, 56371da177e4SLinus Torvalds long type, int mode) 56381da177e4SLinus Torvalds { 56391da177e4SLinus Torvalds struct ipc_security_struct *isec; 56401da177e4SLinus Torvalds struct msg_security_struct *msec; 56412bf49690SThomas Liu struct common_audit_data ad; 5642275bb41eSDavid Howells u32 sid = task_sid(target); 56431da177e4SLinus Torvalds int rc; 56441da177e4SLinus Torvalds 56451da177e4SLinus Torvalds isec = msq->q_perm.security; 56461da177e4SLinus Torvalds msec = msg->security; 56471da177e4SLinus Torvalds 564850c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 56491da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 56501da177e4SLinus Torvalds 5651275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, 56521da177e4SLinus Torvalds SECCLASS_MSGQ, MSGQ__READ, &ad); 56531da177e4SLinus Torvalds if (!rc) 5654275bb41eSDavid Howells rc = avc_has_perm(sid, msec->sid, 56551da177e4SLinus Torvalds SECCLASS_MSG, MSG__RECEIVE, &ad); 56561da177e4SLinus Torvalds return rc; 56571da177e4SLinus Torvalds } 56581da177e4SLinus Torvalds 56591da177e4SLinus Torvalds /* Shared Memory security operations */ 56601da177e4SLinus Torvalds static int selinux_shm_alloc_security(struct shmid_kernel *shp) 56611da177e4SLinus Torvalds { 56621da177e4SLinus Torvalds struct ipc_security_struct *isec; 56632bf49690SThomas Liu struct common_audit_data ad; 5664275bb41eSDavid Howells u32 sid = current_sid(); 56651da177e4SLinus Torvalds int rc; 56661da177e4SLinus Torvalds 5667be0554c9SStephen Smalley rc = ipc_alloc_security(&shp->shm_perm, SECCLASS_SHM); 56681da177e4SLinus Torvalds if (rc) 56691da177e4SLinus Torvalds return rc; 56701da177e4SLinus Torvalds 56711da177e4SLinus Torvalds isec = shp->shm_perm.security; 56721da177e4SLinus Torvalds 567350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 56741da177e4SLinus Torvalds ad.u.ipc_id = shp->shm_perm.key; 56751da177e4SLinus Torvalds 5676275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, SECCLASS_SHM, 56771da177e4SLinus Torvalds SHM__CREATE, &ad); 56781da177e4SLinus Torvalds if (rc) { 56791da177e4SLinus Torvalds ipc_free_security(&shp->shm_perm); 56801da177e4SLinus Torvalds return rc; 56811da177e4SLinus Torvalds } 56821da177e4SLinus Torvalds return 0; 56831da177e4SLinus Torvalds } 56841da177e4SLinus Torvalds 56851da177e4SLinus Torvalds static void selinux_shm_free_security(struct shmid_kernel *shp) 56861da177e4SLinus Torvalds { 56871da177e4SLinus Torvalds ipc_free_security(&shp->shm_perm); 56881da177e4SLinus Torvalds } 56891da177e4SLinus Torvalds 56901da177e4SLinus Torvalds static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg) 56911da177e4SLinus Torvalds { 56921da177e4SLinus Torvalds struct ipc_security_struct *isec; 56932bf49690SThomas Liu struct common_audit_data ad; 5694275bb41eSDavid Howells u32 sid = current_sid(); 56951da177e4SLinus Torvalds 56961da177e4SLinus Torvalds isec = shp->shm_perm.security; 56971da177e4SLinus Torvalds 569850c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 56991da177e4SLinus Torvalds ad.u.ipc_id = shp->shm_perm.key; 57001da177e4SLinus Torvalds 5701275bb41eSDavid Howells return avc_has_perm(sid, isec->sid, SECCLASS_SHM, 57021da177e4SLinus Torvalds SHM__ASSOCIATE, &ad); 57031da177e4SLinus Torvalds } 57041da177e4SLinus Torvalds 57051da177e4SLinus Torvalds /* Note, at this point, shp is locked down */ 57061da177e4SLinus Torvalds static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd) 57071da177e4SLinus Torvalds { 57081da177e4SLinus Torvalds int perms; 57091da177e4SLinus Torvalds int err; 57101da177e4SLinus Torvalds 57111da177e4SLinus Torvalds switch (cmd) { 57121da177e4SLinus Torvalds case IPC_INFO: 57131da177e4SLinus Torvalds case SHM_INFO: 57141da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 5715be0554c9SStephen Smalley return avc_has_perm(current_sid(), SECINITSID_KERNEL, 5716be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); 57171da177e4SLinus Torvalds case IPC_STAT: 57181da177e4SLinus Torvalds case SHM_STAT: 57191da177e4SLinus Torvalds perms = SHM__GETATTR | SHM__ASSOCIATE; 57201da177e4SLinus Torvalds break; 57211da177e4SLinus Torvalds case IPC_SET: 57221da177e4SLinus Torvalds perms = SHM__SETATTR; 57231da177e4SLinus Torvalds break; 57241da177e4SLinus Torvalds case SHM_LOCK: 57251da177e4SLinus Torvalds case SHM_UNLOCK: 57261da177e4SLinus Torvalds perms = SHM__LOCK; 57271da177e4SLinus Torvalds break; 57281da177e4SLinus Torvalds case IPC_RMID: 57291da177e4SLinus Torvalds perms = SHM__DESTROY; 57301da177e4SLinus Torvalds break; 57311da177e4SLinus Torvalds default: 57321da177e4SLinus Torvalds return 0; 57331da177e4SLinus Torvalds } 57341da177e4SLinus Torvalds 57356af963f1SStephen Smalley err = ipc_has_perm(&shp->shm_perm, perms); 57361da177e4SLinus Torvalds return err; 57371da177e4SLinus Torvalds } 57381da177e4SLinus Torvalds 57391da177e4SLinus Torvalds static int selinux_shm_shmat(struct shmid_kernel *shp, 57401da177e4SLinus Torvalds char __user *shmaddr, int shmflg) 57411da177e4SLinus Torvalds { 57421da177e4SLinus Torvalds u32 perms; 57431da177e4SLinus Torvalds 57441da177e4SLinus Torvalds if (shmflg & SHM_RDONLY) 57451da177e4SLinus Torvalds perms = SHM__READ; 57461da177e4SLinus Torvalds else 57471da177e4SLinus Torvalds perms = SHM__READ | SHM__WRITE; 57481da177e4SLinus Torvalds 57496af963f1SStephen Smalley return ipc_has_perm(&shp->shm_perm, perms); 57501da177e4SLinus Torvalds } 57511da177e4SLinus Torvalds 57521da177e4SLinus Torvalds /* Semaphore security operations */ 57531da177e4SLinus Torvalds static int selinux_sem_alloc_security(struct sem_array *sma) 57541da177e4SLinus Torvalds { 57551da177e4SLinus Torvalds struct ipc_security_struct *isec; 57562bf49690SThomas Liu struct common_audit_data ad; 5757275bb41eSDavid Howells u32 sid = current_sid(); 57581da177e4SLinus Torvalds int rc; 57591da177e4SLinus Torvalds 5760be0554c9SStephen Smalley rc = ipc_alloc_security(&sma->sem_perm, SECCLASS_SEM); 57611da177e4SLinus Torvalds if (rc) 57621da177e4SLinus Torvalds return rc; 57631da177e4SLinus Torvalds 57641da177e4SLinus Torvalds isec = sma->sem_perm.security; 57651da177e4SLinus Torvalds 576650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 57671da177e4SLinus Torvalds ad.u.ipc_id = sma->sem_perm.key; 57681da177e4SLinus Torvalds 5769275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, SECCLASS_SEM, 57701da177e4SLinus Torvalds SEM__CREATE, &ad); 57711da177e4SLinus Torvalds if (rc) { 57721da177e4SLinus Torvalds ipc_free_security(&sma->sem_perm); 57731da177e4SLinus Torvalds return rc; 57741da177e4SLinus Torvalds } 57751da177e4SLinus Torvalds return 0; 57761da177e4SLinus Torvalds } 57771da177e4SLinus Torvalds 57781da177e4SLinus Torvalds static void selinux_sem_free_security(struct sem_array *sma) 57791da177e4SLinus Torvalds { 57801da177e4SLinus Torvalds ipc_free_security(&sma->sem_perm); 57811da177e4SLinus Torvalds } 57821da177e4SLinus Torvalds 57831da177e4SLinus Torvalds static int selinux_sem_associate(struct sem_array *sma, int semflg) 57841da177e4SLinus Torvalds { 57851da177e4SLinus Torvalds struct ipc_security_struct *isec; 57862bf49690SThomas Liu struct common_audit_data ad; 5787275bb41eSDavid Howells u32 sid = current_sid(); 57881da177e4SLinus Torvalds 57891da177e4SLinus Torvalds isec = sma->sem_perm.security; 57901da177e4SLinus Torvalds 579150c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 57921da177e4SLinus Torvalds ad.u.ipc_id = sma->sem_perm.key; 57931da177e4SLinus Torvalds 5794275bb41eSDavid Howells return avc_has_perm(sid, isec->sid, SECCLASS_SEM, 57951da177e4SLinus Torvalds SEM__ASSOCIATE, &ad); 57961da177e4SLinus Torvalds } 57971da177e4SLinus Torvalds 57981da177e4SLinus Torvalds /* Note, at this point, sma is locked down */ 57991da177e4SLinus Torvalds static int selinux_sem_semctl(struct sem_array *sma, int cmd) 58001da177e4SLinus Torvalds { 58011da177e4SLinus Torvalds int err; 58021da177e4SLinus Torvalds u32 perms; 58031da177e4SLinus Torvalds 58041da177e4SLinus Torvalds switch (cmd) { 58051da177e4SLinus Torvalds case IPC_INFO: 58061da177e4SLinus Torvalds case SEM_INFO: 58071da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 5808be0554c9SStephen Smalley return avc_has_perm(current_sid(), SECINITSID_KERNEL, 5809be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); 58101da177e4SLinus Torvalds case GETPID: 58111da177e4SLinus Torvalds case GETNCNT: 58121da177e4SLinus Torvalds case GETZCNT: 58131da177e4SLinus Torvalds perms = SEM__GETATTR; 58141da177e4SLinus Torvalds break; 58151da177e4SLinus Torvalds case GETVAL: 58161da177e4SLinus Torvalds case GETALL: 58171da177e4SLinus Torvalds perms = SEM__READ; 58181da177e4SLinus Torvalds break; 58191da177e4SLinus Torvalds case SETVAL: 58201da177e4SLinus Torvalds case SETALL: 58211da177e4SLinus Torvalds perms = SEM__WRITE; 58221da177e4SLinus Torvalds break; 58231da177e4SLinus Torvalds case IPC_RMID: 58241da177e4SLinus Torvalds perms = SEM__DESTROY; 58251da177e4SLinus Torvalds break; 58261da177e4SLinus Torvalds case IPC_SET: 58271da177e4SLinus Torvalds perms = SEM__SETATTR; 58281da177e4SLinus Torvalds break; 58291da177e4SLinus Torvalds case IPC_STAT: 58301da177e4SLinus Torvalds case SEM_STAT: 58311da177e4SLinus Torvalds perms = SEM__GETATTR | SEM__ASSOCIATE; 58321da177e4SLinus Torvalds break; 58331da177e4SLinus Torvalds default: 58341da177e4SLinus Torvalds return 0; 58351da177e4SLinus Torvalds } 58361da177e4SLinus Torvalds 58376af963f1SStephen Smalley err = ipc_has_perm(&sma->sem_perm, perms); 58381da177e4SLinus Torvalds return err; 58391da177e4SLinus Torvalds } 58401da177e4SLinus Torvalds 58411da177e4SLinus Torvalds static int selinux_sem_semop(struct sem_array *sma, 58421da177e4SLinus Torvalds struct sembuf *sops, unsigned nsops, int alter) 58431da177e4SLinus Torvalds { 58441da177e4SLinus Torvalds u32 perms; 58451da177e4SLinus Torvalds 58461da177e4SLinus Torvalds if (alter) 58471da177e4SLinus Torvalds perms = SEM__READ | SEM__WRITE; 58481da177e4SLinus Torvalds else 58491da177e4SLinus Torvalds perms = SEM__READ; 58501da177e4SLinus Torvalds 58516af963f1SStephen Smalley return ipc_has_perm(&sma->sem_perm, perms); 58521da177e4SLinus Torvalds } 58531da177e4SLinus Torvalds 58541da177e4SLinus Torvalds static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag) 58551da177e4SLinus Torvalds { 58561da177e4SLinus Torvalds u32 av = 0; 58571da177e4SLinus Torvalds 58581da177e4SLinus Torvalds av = 0; 58591da177e4SLinus Torvalds if (flag & S_IRUGO) 58601da177e4SLinus Torvalds av |= IPC__UNIX_READ; 58611da177e4SLinus Torvalds if (flag & S_IWUGO) 58621da177e4SLinus Torvalds av |= IPC__UNIX_WRITE; 58631da177e4SLinus Torvalds 58641da177e4SLinus Torvalds if (av == 0) 58651da177e4SLinus Torvalds return 0; 58661da177e4SLinus Torvalds 58676af963f1SStephen Smalley return ipc_has_perm(ipcp, av); 58681da177e4SLinus Torvalds } 58691da177e4SLinus Torvalds 5870713a04aeSAhmed S. Darwish static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid) 5871713a04aeSAhmed S. Darwish { 5872713a04aeSAhmed S. Darwish struct ipc_security_struct *isec = ipcp->security; 5873713a04aeSAhmed S. Darwish *secid = isec->sid; 5874713a04aeSAhmed S. Darwish } 5875713a04aeSAhmed S. Darwish 58761da177e4SLinus Torvalds static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode) 58771da177e4SLinus Torvalds { 58781da177e4SLinus Torvalds if (inode) 58791da177e4SLinus Torvalds inode_doinit_with_dentry(inode, dentry); 58801da177e4SLinus Torvalds } 58811da177e4SLinus Torvalds 58821da177e4SLinus Torvalds static int selinux_getprocattr(struct task_struct *p, 588304ff9708SAl Viro char *name, char **value) 58841da177e4SLinus Torvalds { 5885275bb41eSDavid Howells const struct task_security_struct *__tsec; 58868c8570fbSDustin Kirkland u32 sid; 58871da177e4SLinus Torvalds int error; 588804ff9708SAl Viro unsigned len; 58891da177e4SLinus Torvalds 5890275bb41eSDavid Howells rcu_read_lock(); 5891275bb41eSDavid Howells __tsec = __task_cred(p)->security; 58921da177e4SLinus Torvalds 5893be0554c9SStephen Smalley if (current != p) { 5894be0554c9SStephen Smalley error = avc_has_perm(current_sid(), __tsec->sid, 5895be0554c9SStephen Smalley SECCLASS_PROCESS, PROCESS__GETATTR, NULL); 5896be0554c9SStephen Smalley if (error) 5897be0554c9SStephen Smalley goto bad; 5898be0554c9SStephen Smalley } 5899be0554c9SStephen Smalley 59001da177e4SLinus Torvalds if (!strcmp(name, "current")) 5901275bb41eSDavid Howells sid = __tsec->sid; 59021da177e4SLinus Torvalds else if (!strcmp(name, "prev")) 5903275bb41eSDavid Howells sid = __tsec->osid; 59041da177e4SLinus Torvalds else if (!strcmp(name, "exec")) 5905275bb41eSDavid Howells sid = __tsec->exec_sid; 59061da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 5907275bb41eSDavid Howells sid = __tsec->create_sid; 59084eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) 5909275bb41eSDavid Howells sid = __tsec->keycreate_sid; 591042c3e03eSEric Paris else if (!strcmp(name, "sockcreate")) 5911275bb41eSDavid Howells sid = __tsec->sockcreate_sid; 5912be0554c9SStephen Smalley else { 5913be0554c9SStephen Smalley error = -EINVAL; 5914be0554c9SStephen Smalley goto bad; 5915be0554c9SStephen Smalley } 5916275bb41eSDavid Howells rcu_read_unlock(); 59171da177e4SLinus Torvalds 59181da177e4SLinus Torvalds if (!sid) 59191da177e4SLinus Torvalds return 0; 59201da177e4SLinus Torvalds 592104ff9708SAl Viro error = security_sid_to_context(sid, value, &len); 592204ff9708SAl Viro if (error) 592304ff9708SAl Viro return error; 592404ff9708SAl Viro return len; 5925275bb41eSDavid Howells 5926be0554c9SStephen Smalley bad: 5927275bb41eSDavid Howells rcu_read_unlock(); 5928be0554c9SStephen Smalley return error; 59291da177e4SLinus Torvalds } 59301da177e4SLinus Torvalds 5931b21507e2SStephen Smalley static int selinux_setprocattr(const char *name, void *value, size_t size) 59321da177e4SLinus Torvalds { 59331da177e4SLinus Torvalds struct task_security_struct *tsec; 5934d84f4f99SDavid Howells struct cred *new; 5935be0554c9SStephen Smalley u32 mysid = current_sid(), sid = 0, ptsid; 59361da177e4SLinus Torvalds int error; 59371da177e4SLinus Torvalds char *str = value; 59381da177e4SLinus Torvalds 59391da177e4SLinus Torvalds /* 59401da177e4SLinus Torvalds * Basic control over ability to set these attributes at all. 59411da177e4SLinus Torvalds */ 59421da177e4SLinus Torvalds if (!strcmp(name, "exec")) 5943be0554c9SStephen Smalley error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS, 5944be0554c9SStephen Smalley PROCESS__SETEXEC, NULL); 59451da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 5946be0554c9SStephen Smalley error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS, 5947be0554c9SStephen Smalley PROCESS__SETFSCREATE, NULL); 59484eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) 5949be0554c9SStephen Smalley error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS, 5950be0554c9SStephen Smalley PROCESS__SETKEYCREATE, NULL); 595142c3e03eSEric Paris else if (!strcmp(name, "sockcreate")) 5952be0554c9SStephen Smalley error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS, 5953be0554c9SStephen Smalley PROCESS__SETSOCKCREATE, NULL); 59541da177e4SLinus Torvalds else if (!strcmp(name, "current")) 5955be0554c9SStephen Smalley error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS, 5956be0554c9SStephen Smalley PROCESS__SETCURRENT, NULL); 59571da177e4SLinus Torvalds else 59581da177e4SLinus Torvalds error = -EINVAL; 59591da177e4SLinus Torvalds if (error) 59601da177e4SLinus Torvalds return error; 59611da177e4SLinus Torvalds 59621da177e4SLinus Torvalds /* Obtain a SID for the context, if one was specified. */ 5963a050a570SStephen Smalley if (size && str[0] && str[0] != '\n') { 59641da177e4SLinus Torvalds if (str[size-1] == '\n') { 59651da177e4SLinus Torvalds str[size-1] = 0; 59661da177e4SLinus Torvalds size--; 59671da177e4SLinus Torvalds } 596852a4c640SNikolay Aleksandrov error = security_context_to_sid(value, size, &sid, GFP_KERNEL); 596912b29f34SStephen Smalley if (error == -EINVAL && !strcmp(name, "fscreate")) { 5970db59000aSStephen Smalley if (!has_cap_mac_admin(true)) { 5971d6ea83ecSEric Paris struct audit_buffer *ab; 5972d6ea83ecSEric Paris size_t audit_size; 5973d6ea83ecSEric Paris 5974d6ea83ecSEric Paris /* We strip a nul only if it is at the end, otherwise the 5975d6ea83ecSEric Paris * context contains a nul and we should audit that */ 5976d6ea83ecSEric Paris if (str[size - 1] == '\0') 5977d6ea83ecSEric Paris audit_size = size - 1; 5978d6ea83ecSEric Paris else 5979d6ea83ecSEric Paris audit_size = size; 5980d6ea83ecSEric Paris ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR); 5981d6ea83ecSEric Paris audit_log_format(ab, "op=fscreate invalid_context="); 5982d6ea83ecSEric Paris audit_log_n_untrustedstring(ab, value, audit_size); 5983d6ea83ecSEric Paris audit_log_end(ab); 5984d6ea83ecSEric Paris 598512b29f34SStephen Smalley return error; 5986d6ea83ecSEric Paris } 598712b29f34SStephen Smalley error = security_context_to_sid_force(value, size, 598812b29f34SStephen Smalley &sid); 598912b29f34SStephen Smalley } 59901da177e4SLinus Torvalds if (error) 59911da177e4SLinus Torvalds return error; 59921da177e4SLinus Torvalds } 59931da177e4SLinus Torvalds 5994d84f4f99SDavid Howells new = prepare_creds(); 5995d84f4f99SDavid Howells if (!new) 5996d84f4f99SDavid Howells return -ENOMEM; 5997d84f4f99SDavid Howells 59981da177e4SLinus Torvalds /* Permission checking based on the specified context is 59991da177e4SLinus Torvalds performed during the actual operation (execve, 60001da177e4SLinus Torvalds open/mkdir/...), when we know the full context of the 6001d84f4f99SDavid Howells operation. See selinux_bprm_set_creds for the execve 60021da177e4SLinus Torvalds checks and may_create for the file creation checks. The 60031da177e4SLinus Torvalds operation will then fail if the context is not permitted. */ 6004d84f4f99SDavid Howells tsec = new->security; 6005d84f4f99SDavid Howells if (!strcmp(name, "exec")) { 60061da177e4SLinus Torvalds tsec->exec_sid = sid; 6007d84f4f99SDavid Howells } else if (!strcmp(name, "fscreate")) { 60081da177e4SLinus Torvalds tsec->create_sid = sid; 6009d84f4f99SDavid Howells } else if (!strcmp(name, "keycreate")) { 6010be0554c9SStephen Smalley error = avc_has_perm(mysid, sid, SECCLASS_KEY, KEY__CREATE, 6011be0554c9SStephen Smalley NULL); 60124eb582cfSMichael LeMay if (error) 6013d84f4f99SDavid Howells goto abort_change; 60144eb582cfSMichael LeMay tsec->keycreate_sid = sid; 6015d84f4f99SDavid Howells } else if (!strcmp(name, "sockcreate")) { 601642c3e03eSEric Paris tsec->sockcreate_sid = sid; 6017d84f4f99SDavid Howells } else if (!strcmp(name, "current")) { 6018d84f4f99SDavid Howells error = -EINVAL; 60191da177e4SLinus Torvalds if (sid == 0) 6020d84f4f99SDavid Howells goto abort_change; 6021d9250deaSKaiGai Kohei 6022d84f4f99SDavid Howells /* Only allow single threaded processes to change context */ 6023d84f4f99SDavid Howells error = -EPERM; 60245bb459bbSOleg Nesterov if (!current_is_single_threaded()) { 6025d84f4f99SDavid Howells error = security_bounded_transition(tsec->sid, sid); 6026d84f4f99SDavid Howells if (error) 6027d84f4f99SDavid Howells goto abort_change; 60281da177e4SLinus Torvalds } 60291da177e4SLinus Torvalds 60301da177e4SLinus Torvalds /* Check permissions for the transition. */ 60311da177e4SLinus Torvalds error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS, 60321da177e4SLinus Torvalds PROCESS__DYNTRANSITION, NULL); 60331da177e4SLinus Torvalds if (error) 6034d84f4f99SDavid Howells goto abort_change; 60351da177e4SLinus Torvalds 60361da177e4SLinus Torvalds /* Check for ptracing, and update the task SID if ok. 60371da177e4SLinus Torvalds Otherwise, leave SID unchanged and fail. */ 6038be0554c9SStephen Smalley ptsid = ptrace_parent_sid(); 60390c6181cbSPaul Moore if (ptsid != 0) { 6040d84f4f99SDavid Howells error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS, 6041d84f4f99SDavid Howells PROCESS__PTRACE, NULL); 6042d84f4f99SDavid Howells if (error) 6043d84f4f99SDavid Howells goto abort_change; 6044d84f4f99SDavid Howells } 6045d84f4f99SDavid Howells 6046d84f4f99SDavid Howells tsec->sid = sid; 6047d84f4f99SDavid Howells } else { 6048d84f4f99SDavid Howells error = -EINVAL; 6049d84f4f99SDavid Howells goto abort_change; 6050d84f4f99SDavid Howells } 6051d84f4f99SDavid Howells 6052d84f4f99SDavid Howells commit_creds(new); 60531da177e4SLinus Torvalds return size; 6054d84f4f99SDavid Howells 6055d84f4f99SDavid Howells abort_change: 6056d84f4f99SDavid Howells abort_creds(new); 6057d84f4f99SDavid Howells return error; 60581da177e4SLinus Torvalds } 60591da177e4SLinus Torvalds 6060746df9b5SDavid Quigley static int selinux_ismaclabel(const char *name) 6061746df9b5SDavid Quigley { 6062746df9b5SDavid Quigley return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0); 6063746df9b5SDavid Quigley } 6064746df9b5SDavid Quigley 6065dc49c1f9SCatherine Zhang static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) 6066dc49c1f9SCatherine Zhang { 6067dc49c1f9SCatherine Zhang return security_sid_to_context(secid, secdata, seclen); 6068dc49c1f9SCatherine Zhang } 6069dc49c1f9SCatherine Zhang 60707bf570dcSDavid Howells static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid) 607163cb3449SDavid Howells { 607252a4c640SNikolay Aleksandrov return security_context_to_sid(secdata, seclen, secid, GFP_KERNEL); 607363cb3449SDavid Howells } 607463cb3449SDavid Howells 6075dc49c1f9SCatherine Zhang static void selinux_release_secctx(char *secdata, u32 seclen) 6076dc49c1f9SCatherine Zhang { 6077dc49c1f9SCatherine Zhang kfree(secdata); 6078dc49c1f9SCatherine Zhang } 6079dc49c1f9SCatherine Zhang 60806f3be9f5SAndreas Gruenbacher static void selinux_inode_invalidate_secctx(struct inode *inode) 60816f3be9f5SAndreas Gruenbacher { 60826f3be9f5SAndreas Gruenbacher struct inode_security_struct *isec = inode->i_security; 60836f3be9f5SAndreas Gruenbacher 60849287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 60856f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INVALID; 60869287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 60876f3be9f5SAndreas Gruenbacher } 60886f3be9f5SAndreas Gruenbacher 60891ee65e37SDavid P. Quigley /* 60901ee65e37SDavid P. Quigley * called with inode->i_mutex locked 60911ee65e37SDavid P. Quigley */ 60921ee65e37SDavid P. Quigley static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen) 60931ee65e37SDavid P. Quigley { 60941ee65e37SDavid P. Quigley return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0); 60951ee65e37SDavid P. Quigley } 60961ee65e37SDavid P. Quigley 60971ee65e37SDavid P. Quigley /* 60981ee65e37SDavid P. Quigley * called with inode->i_mutex locked 60991ee65e37SDavid P. Quigley */ 61001ee65e37SDavid P. Quigley static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen) 61011ee65e37SDavid P. Quigley { 61021ee65e37SDavid P. Quigley return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0); 61031ee65e37SDavid P. Quigley } 61041ee65e37SDavid P. Quigley 61051ee65e37SDavid P. Quigley static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen) 61061ee65e37SDavid P. Quigley { 61071ee65e37SDavid P. Quigley int len = 0; 61081ee65e37SDavid P. Quigley len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX, 61091ee65e37SDavid P. Quigley ctx, true); 61101ee65e37SDavid P. Quigley if (len < 0) 61111ee65e37SDavid P. Quigley return len; 61121ee65e37SDavid P. Quigley *ctxlen = len; 61131ee65e37SDavid P. Quigley return 0; 61141ee65e37SDavid P. Quigley } 6115d720024eSMichael LeMay #ifdef CONFIG_KEYS 6116d720024eSMichael LeMay 6117d84f4f99SDavid Howells static int selinux_key_alloc(struct key *k, const struct cred *cred, 61187e047ef5SDavid Howells unsigned long flags) 6119d720024eSMichael LeMay { 6120d84f4f99SDavid Howells const struct task_security_struct *tsec; 6121d720024eSMichael LeMay struct key_security_struct *ksec; 6122d720024eSMichael LeMay 6123d720024eSMichael LeMay ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL); 6124d720024eSMichael LeMay if (!ksec) 6125d720024eSMichael LeMay return -ENOMEM; 6126d720024eSMichael LeMay 6127d84f4f99SDavid Howells tsec = cred->security; 6128d84f4f99SDavid Howells if (tsec->keycreate_sid) 6129d84f4f99SDavid Howells ksec->sid = tsec->keycreate_sid; 61304eb582cfSMichael LeMay else 6131d84f4f99SDavid Howells ksec->sid = tsec->sid; 6132d720024eSMichael LeMay 6133275bb41eSDavid Howells k->security = ksec; 6134d720024eSMichael LeMay return 0; 6135d720024eSMichael LeMay } 6136d720024eSMichael LeMay 6137d720024eSMichael LeMay static void selinux_key_free(struct key *k) 6138d720024eSMichael LeMay { 6139d720024eSMichael LeMay struct key_security_struct *ksec = k->security; 6140d720024eSMichael LeMay 6141d720024eSMichael LeMay k->security = NULL; 6142d720024eSMichael LeMay kfree(ksec); 6143d720024eSMichael LeMay } 6144d720024eSMichael LeMay 6145d720024eSMichael LeMay static int selinux_key_permission(key_ref_t key_ref, 6146d84f4f99SDavid Howells const struct cred *cred, 6147f5895943SDavid Howells unsigned perm) 6148d720024eSMichael LeMay { 6149d720024eSMichael LeMay struct key *key; 6150d720024eSMichael LeMay struct key_security_struct *ksec; 6151275bb41eSDavid Howells u32 sid; 6152d720024eSMichael LeMay 6153d720024eSMichael LeMay /* if no specific permissions are requested, we skip the 6154d720024eSMichael LeMay permission check. No serious, additional covert channels 6155d720024eSMichael LeMay appear to be created. */ 6156d720024eSMichael LeMay if (perm == 0) 6157d720024eSMichael LeMay return 0; 6158d720024eSMichael LeMay 6159d84f4f99SDavid Howells sid = cred_sid(cred); 6160275bb41eSDavid Howells 6161275bb41eSDavid Howells key = key_ref_to_ptr(key_ref); 6162275bb41eSDavid Howells ksec = key->security; 6163275bb41eSDavid Howells 6164275bb41eSDavid Howells return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL); 6165d720024eSMichael LeMay } 6166d720024eSMichael LeMay 616770a5bb72SDavid Howells static int selinux_key_getsecurity(struct key *key, char **_buffer) 616870a5bb72SDavid Howells { 616970a5bb72SDavid Howells struct key_security_struct *ksec = key->security; 617070a5bb72SDavid Howells char *context = NULL; 617170a5bb72SDavid Howells unsigned len; 617270a5bb72SDavid Howells int rc; 617370a5bb72SDavid Howells 617470a5bb72SDavid Howells rc = security_sid_to_context(ksec->sid, &context, &len); 617570a5bb72SDavid Howells if (!rc) 617670a5bb72SDavid Howells rc = len; 617770a5bb72SDavid Howells *_buffer = context; 617870a5bb72SDavid Howells return rc; 617970a5bb72SDavid Howells } 61803a976fa6SDaniel Jurgens #endif 618170a5bb72SDavid Howells 61823a976fa6SDaniel Jurgens #ifdef CONFIG_SECURITY_INFINIBAND 6183cfc4d882SDaniel Jurgens static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val) 6184cfc4d882SDaniel Jurgens { 6185cfc4d882SDaniel Jurgens struct common_audit_data ad; 6186cfc4d882SDaniel Jurgens int err; 6187cfc4d882SDaniel Jurgens u32 sid = 0; 6188cfc4d882SDaniel Jurgens struct ib_security_struct *sec = ib_sec; 6189cfc4d882SDaniel Jurgens struct lsm_ibpkey_audit ibpkey; 6190cfc4d882SDaniel Jurgens 6191409dcf31SDaniel Jurgens err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid); 6192cfc4d882SDaniel Jurgens if (err) 6193cfc4d882SDaniel Jurgens return err; 6194cfc4d882SDaniel Jurgens 6195cfc4d882SDaniel Jurgens ad.type = LSM_AUDIT_DATA_IBPKEY; 6196cfc4d882SDaniel Jurgens ibpkey.subnet_prefix = subnet_prefix; 6197cfc4d882SDaniel Jurgens ibpkey.pkey = pkey_val; 6198cfc4d882SDaniel Jurgens ad.u.ibpkey = &ibpkey; 6199cfc4d882SDaniel Jurgens return avc_has_perm(sec->sid, sid, 6200cfc4d882SDaniel Jurgens SECCLASS_INFINIBAND_PKEY, 6201cfc4d882SDaniel Jurgens INFINIBAND_PKEY__ACCESS, &ad); 6202cfc4d882SDaniel Jurgens } 6203cfc4d882SDaniel Jurgens 6204ab861dfcSDaniel Jurgens static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name, 6205ab861dfcSDaniel Jurgens u8 port_num) 6206ab861dfcSDaniel Jurgens { 6207ab861dfcSDaniel Jurgens struct common_audit_data ad; 6208ab861dfcSDaniel Jurgens int err; 6209ab861dfcSDaniel Jurgens u32 sid = 0; 6210ab861dfcSDaniel Jurgens struct ib_security_struct *sec = ib_sec; 6211ab861dfcSDaniel Jurgens struct lsm_ibendport_audit ibendport; 6212ab861dfcSDaniel Jurgens 6213ab861dfcSDaniel Jurgens err = security_ib_endport_sid(dev_name, port_num, &sid); 6214ab861dfcSDaniel Jurgens 6215ab861dfcSDaniel Jurgens if (err) 6216ab861dfcSDaniel Jurgens return err; 6217ab861dfcSDaniel Jurgens 6218ab861dfcSDaniel Jurgens ad.type = LSM_AUDIT_DATA_IBENDPORT; 6219ab861dfcSDaniel Jurgens strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name)); 6220ab861dfcSDaniel Jurgens ibendport.port = port_num; 6221ab861dfcSDaniel Jurgens ad.u.ibendport = &ibendport; 6222ab861dfcSDaniel Jurgens return avc_has_perm(sec->sid, sid, 6223ab861dfcSDaniel Jurgens SECCLASS_INFINIBAND_ENDPORT, 6224ab861dfcSDaniel Jurgens INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad); 6225ab861dfcSDaniel Jurgens } 6226ab861dfcSDaniel Jurgens 62273a976fa6SDaniel Jurgens static int selinux_ib_alloc_security(void **ib_sec) 62283a976fa6SDaniel Jurgens { 62293a976fa6SDaniel Jurgens struct ib_security_struct *sec; 62303a976fa6SDaniel Jurgens 62313a976fa6SDaniel Jurgens sec = kzalloc(sizeof(*sec), GFP_KERNEL); 62323a976fa6SDaniel Jurgens if (!sec) 62333a976fa6SDaniel Jurgens return -ENOMEM; 62343a976fa6SDaniel Jurgens sec->sid = current_sid(); 62353a976fa6SDaniel Jurgens 62363a976fa6SDaniel Jurgens *ib_sec = sec; 62373a976fa6SDaniel Jurgens return 0; 62383a976fa6SDaniel Jurgens } 62393a976fa6SDaniel Jurgens 62403a976fa6SDaniel Jurgens static void selinux_ib_free_security(void *ib_sec) 62413a976fa6SDaniel Jurgens { 62423a976fa6SDaniel Jurgens kfree(ib_sec); 62433a976fa6SDaniel Jurgens } 6244d720024eSMichael LeMay #endif 6245d720024eSMichael LeMay 6246ca97d939SJames Morris static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = { 6247e20b043aSCasey Schaufler LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr), 6248e20b043aSCasey Schaufler LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction), 6249e20b043aSCasey Schaufler LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder), 6250e20b043aSCasey Schaufler LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file), 6251076c54c5SAhmed S. Darwish 6252e20b043aSCasey Schaufler LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check), 6253e20b043aSCasey Schaufler LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme), 6254e20b043aSCasey Schaufler LSM_HOOK_INIT(capget, selinux_capget), 6255e20b043aSCasey Schaufler LSM_HOOK_INIT(capset, selinux_capset), 6256e20b043aSCasey Schaufler LSM_HOOK_INIT(capable, selinux_capable), 6257e20b043aSCasey Schaufler LSM_HOOK_INIT(quotactl, selinux_quotactl), 6258e20b043aSCasey Schaufler LSM_HOOK_INIT(quota_on, selinux_quota_on), 6259e20b043aSCasey Schaufler LSM_HOOK_INIT(syslog, selinux_syslog), 6260e20b043aSCasey Schaufler LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory), 626179af7307SStephen Smalley 6262e20b043aSCasey Schaufler LSM_HOOK_INIT(netlink_send, selinux_netlink_send), 62631da177e4SLinus Torvalds 6264e20b043aSCasey Schaufler LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds), 6265e20b043aSCasey Schaufler LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds), 6266e20b043aSCasey Schaufler LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds), 62671da177e4SLinus Torvalds 6268e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security), 6269e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security), 6270e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_copy_data, selinux_sb_copy_data), 6271e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_remount, selinux_sb_remount), 6272e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount), 6273e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options), 6274e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs), 6275e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_mount, selinux_mount), 6276e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_umount, selinux_umount), 6277e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts), 6278e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts), 6279e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_parse_opts_str, selinux_parse_opts_str), 62801da177e4SLinus Torvalds 6281e20b043aSCasey Schaufler LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security), 6282a518b0a5SVivek Goyal LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as), 6283e0007529SEric Paris 6284e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security), 6285e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security), 6286e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security), 6287e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_create, selinux_inode_create), 6288e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_link, selinux_inode_link), 6289e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink), 6290e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink), 6291e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir), 6292e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir), 6293e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod), 6294e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_rename, selinux_inode_rename), 6295e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink), 6296e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link), 6297e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_permission, selinux_inode_permission), 6298e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr), 6299e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr), 6300e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr), 6301e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr), 6302e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr), 6303e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr), 6304e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr), 6305e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity), 6306e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity), 6307e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity), 6308e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid), 630956909eb3SVivek Goyal LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up), 631019472b69SVivek Goyal LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr), 63111da177e4SLinus Torvalds 6312e20b043aSCasey Schaufler LSM_HOOK_INIT(file_permission, selinux_file_permission), 6313e20b043aSCasey Schaufler LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security), 6314e20b043aSCasey Schaufler LSM_HOOK_INIT(file_free_security, selinux_file_free_security), 6315e20b043aSCasey Schaufler LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl), 6316e20b043aSCasey Schaufler LSM_HOOK_INIT(mmap_file, selinux_mmap_file), 6317e20b043aSCasey Schaufler LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr), 6318e20b043aSCasey Schaufler LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect), 6319e20b043aSCasey Schaufler LSM_HOOK_INIT(file_lock, selinux_file_lock), 6320e20b043aSCasey Schaufler LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl), 6321e20b043aSCasey Schaufler LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner), 6322e20b043aSCasey Schaufler LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask), 6323e20b043aSCasey Schaufler LSM_HOOK_INIT(file_receive, selinux_file_receive), 63241da177e4SLinus Torvalds 6325e20b043aSCasey Schaufler LSM_HOOK_INIT(file_open, selinux_file_open), 63261da177e4SLinus Torvalds 6327a79be238STetsuo Handa LSM_HOOK_INIT(task_alloc, selinux_task_alloc), 6328e20b043aSCasey Schaufler LSM_HOOK_INIT(cred_alloc_blank, selinux_cred_alloc_blank), 6329e20b043aSCasey Schaufler LSM_HOOK_INIT(cred_free, selinux_cred_free), 6330e20b043aSCasey Schaufler LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare), 6331e20b043aSCasey Schaufler LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer), 6332e20b043aSCasey Schaufler LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as), 6333e20b043aSCasey Schaufler LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as), 6334e20b043aSCasey Schaufler LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request), 633561d612eaSJeff Vander Stoep LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file), 6336e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid), 6337e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid), 6338e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getsid, selinux_task_getsid), 6339e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid), 6340e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setnice, selinux_task_setnice), 6341e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio), 6342e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio), 6343791ec491SStephen Smalley LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit), 6344e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit), 6345e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler), 6346e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler), 6347e20b043aSCasey Schaufler LSM_HOOK_INIT(task_movememory, selinux_task_movememory), 6348e20b043aSCasey Schaufler LSM_HOOK_INIT(task_kill, selinux_task_kill), 6349e20b043aSCasey Schaufler LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode), 6350788e7dd4SYuichi Nakamura 6351e20b043aSCasey Schaufler LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission), 6352e20b043aSCasey Schaufler LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid), 63531da177e4SLinus Torvalds 6354e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security), 6355e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_msg_free_security, selinux_msg_msg_free_security), 63561da177e4SLinus Torvalds 6357e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_alloc_security, 6358e20b043aSCasey Schaufler selinux_msg_queue_alloc_security), 6359e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_free_security, selinux_msg_queue_free_security), 6360e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate), 6361e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl), 6362e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd), 6363e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv), 63641da177e4SLinus Torvalds 6365e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security), 6366e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_free_security, selinux_shm_free_security), 6367e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_associate, selinux_shm_associate), 6368e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl), 6369e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat), 63701da177e4SLinus Torvalds 6371e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security), 6372e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_free_security, selinux_sem_free_security), 6373e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_associate, selinux_sem_associate), 6374e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl), 6375e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_semop, selinux_sem_semop), 63761da177e4SLinus Torvalds 6377e20b043aSCasey Schaufler LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate), 63781da177e4SLinus Torvalds 6379e20b043aSCasey Schaufler LSM_HOOK_INIT(getprocattr, selinux_getprocattr), 6380e20b043aSCasey Schaufler LSM_HOOK_INIT(setprocattr, selinux_setprocattr), 63811da177e4SLinus Torvalds 6382e20b043aSCasey Schaufler LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel), 6383e20b043aSCasey Schaufler LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx), 6384e20b043aSCasey Schaufler LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid), 6385e20b043aSCasey Schaufler LSM_HOOK_INIT(release_secctx, selinux_release_secctx), 63866f3be9f5SAndreas Gruenbacher LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx), 6387e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx), 6388e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx), 6389e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx), 63901da177e4SLinus Torvalds 6391e20b043aSCasey Schaufler LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect), 6392e20b043aSCasey Schaufler LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send), 6393dc49c1f9SCatherine Zhang 6394e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_create, selinux_socket_create), 6395e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create), 6396e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_bind, selinux_socket_bind), 6397e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_connect, selinux_socket_connect), 6398e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_listen, selinux_socket_listen), 6399e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_accept, selinux_socket_accept), 6400e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg), 6401e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg), 6402e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname), 6403e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername), 6404e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt), 6405e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt), 6406e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown), 6407e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb), 6408e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getpeersec_stream, 6409e20b043aSCasey Schaufler selinux_socket_getpeersec_stream), 6410e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram), 6411e20b043aSCasey Schaufler LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security), 6412e20b043aSCasey Schaufler LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security), 6413e20b043aSCasey Schaufler LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security), 6414e20b043aSCasey Schaufler LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid), 6415e20b043aSCasey Schaufler LSM_HOOK_INIT(sock_graft, selinux_sock_graft), 6416e20b043aSCasey Schaufler LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request), 6417e20b043aSCasey Schaufler LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone), 6418e20b043aSCasey Schaufler LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established), 6419e20b043aSCasey Schaufler LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet), 6420e20b043aSCasey Schaufler LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc), 6421e20b043aSCasey Schaufler LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec), 6422e20b043aSCasey Schaufler LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow), 6423e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security), 6424e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security), 6425e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create), 6426e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue), 6427e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach), 6428e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open), 64293a976fa6SDaniel Jurgens #ifdef CONFIG_SECURITY_INFINIBAND 6430cfc4d882SDaniel Jurgens LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access), 6431ab861dfcSDaniel Jurgens LSM_HOOK_INIT(ib_endport_manage_subnet, 6432ab861dfcSDaniel Jurgens selinux_ib_endport_manage_subnet), 64333a976fa6SDaniel Jurgens LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security), 64343a976fa6SDaniel Jurgens LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security), 64353a976fa6SDaniel Jurgens #endif 6436d28d1e08STrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM 6437e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc), 6438e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone), 6439e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free), 6440e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete), 6441e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc), 6442e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_alloc_acquire, 6443e20b043aSCasey Schaufler selinux_xfrm_state_alloc_acquire), 6444e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free), 6445e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete), 6446e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup), 6447e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_pol_flow_match, 6448e20b043aSCasey Schaufler selinux_xfrm_state_pol_flow_match), 6449e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session), 64501da177e4SLinus Torvalds #endif 6451d720024eSMichael LeMay 6452d720024eSMichael LeMay #ifdef CONFIG_KEYS 6453e20b043aSCasey Schaufler LSM_HOOK_INIT(key_alloc, selinux_key_alloc), 6454e20b043aSCasey Schaufler LSM_HOOK_INIT(key_free, selinux_key_free), 6455e20b043aSCasey Schaufler LSM_HOOK_INIT(key_permission, selinux_key_permission), 6456e20b043aSCasey Schaufler LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity), 6457d720024eSMichael LeMay #endif 64589d57a7f9SAhmed S. Darwish 64599d57a7f9SAhmed S. Darwish #ifdef CONFIG_AUDIT 6460e20b043aSCasey Schaufler LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init), 6461e20b043aSCasey Schaufler LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known), 6462e20b043aSCasey Schaufler LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match), 6463e20b043aSCasey Schaufler LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free), 64649d57a7f9SAhmed S. Darwish #endif 64651da177e4SLinus Torvalds }; 64661da177e4SLinus Torvalds 64671da177e4SLinus Torvalds static __init int selinux_init(void) 64681da177e4SLinus Torvalds { 6469b1d9e6b0SCasey Schaufler if (!security_module_enable("selinux")) { 6470076c54c5SAhmed S. Darwish selinux_enabled = 0; 6471076c54c5SAhmed S. Darwish return 0; 6472076c54c5SAhmed S. Darwish } 6473076c54c5SAhmed S. Darwish 64741da177e4SLinus Torvalds if (!selinux_enabled) { 64751da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Disabled at boot.\n"); 64761da177e4SLinus Torvalds return 0; 64771da177e4SLinus Torvalds } 64781da177e4SLinus Torvalds 64791da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Initializing.\n"); 64801da177e4SLinus Torvalds 64811da177e4SLinus Torvalds /* Set the security state for the initial task. */ 6482d84f4f99SDavid Howells cred_init_security(); 64831da177e4SLinus Torvalds 6484fcaaade1SStephen Smalley default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC); 6485fcaaade1SStephen Smalley 64867cae7e26SJames Morris sel_inode_cache = kmem_cache_create("selinux_inode_security", 64877cae7e26SJames Morris sizeof(struct inode_security_struct), 648820c2df83SPaul Mundt 0, SLAB_PANIC, NULL); 648963205654SSangwoo file_security_cache = kmem_cache_create("selinux_file_security", 649063205654SSangwoo sizeof(struct file_security_struct), 649163205654SSangwoo 0, SLAB_PANIC, NULL); 64921da177e4SLinus Torvalds avc_init(); 64931da177e4SLinus Torvalds 6494d69dece5SCasey Schaufler security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux"); 64951da177e4SLinus Torvalds 6496615e51fdSPaul Moore if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET)) 6497615e51fdSPaul Moore panic("SELinux: Unable to register AVC netcache callback\n"); 6498615e51fdSPaul Moore 64998f408ab6SDaniel Jurgens if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET)) 65008f408ab6SDaniel Jurgens panic("SELinux: Unable to register AVC LSM notifier callback\n"); 65018f408ab6SDaniel Jurgens 6502828dfe1dSEric Paris if (selinux_enforcing) 6503fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Starting in enforcing mode\n"); 6504828dfe1dSEric Paris else 6505fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Starting in permissive mode\n"); 6506d720024eSMichael LeMay 65071da177e4SLinus Torvalds return 0; 65081da177e4SLinus Torvalds } 65091da177e4SLinus Torvalds 6510e8c26255SAl Viro static void delayed_superblock_init(struct super_block *sb, void *unused) 6511e8c26255SAl Viro { 6512e8c26255SAl Viro superblock_doinit(sb, NULL); 6513e8c26255SAl Viro } 6514e8c26255SAl Viro 65151da177e4SLinus Torvalds void selinux_complete_init(void) 65161da177e4SLinus Torvalds { 6517fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Completing initialization.\n"); 65181da177e4SLinus Torvalds 65191da177e4SLinus Torvalds /* Set up any superblocks initialized prior to the policy load. */ 6520fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Setting up existing superblocks.\n"); 6521e8c26255SAl Viro iterate_supers(delayed_superblock_init, NULL); 65221da177e4SLinus Torvalds } 65231da177e4SLinus Torvalds 65241da177e4SLinus Torvalds /* SELinux requires early initialization in order to label 65251da177e4SLinus Torvalds all processes and objects when they are created. */ 65261da177e4SLinus Torvalds security_initcall(selinux_init); 65271da177e4SLinus Torvalds 6528c2b507fdSStephen Smalley #if defined(CONFIG_NETFILTER) 65291da177e4SLinus Torvalds 6530591bb278SFlorian Westphal static const struct nf_hook_ops selinux_nf_ops[] = { 6531effad8dfSPaul Moore { 6532effad8dfSPaul Moore .hook = selinux_ipv4_postroute, 65332597a834SAlban Crequy .pf = NFPROTO_IPV4, 65346e23ae2aSPatrick McHardy .hooknum = NF_INET_POST_ROUTING, 65351da177e4SLinus Torvalds .priority = NF_IP_PRI_SELINUX_LAST, 6536effad8dfSPaul Moore }, 6537effad8dfSPaul Moore { 6538effad8dfSPaul Moore .hook = selinux_ipv4_forward, 65392597a834SAlban Crequy .pf = NFPROTO_IPV4, 6540effad8dfSPaul Moore .hooknum = NF_INET_FORWARD, 6541effad8dfSPaul Moore .priority = NF_IP_PRI_SELINUX_FIRST, 6542948bf85cSPaul Moore }, 6543948bf85cSPaul Moore { 6544948bf85cSPaul Moore .hook = selinux_ipv4_output, 65452597a834SAlban Crequy .pf = NFPROTO_IPV4, 6546948bf85cSPaul Moore .hooknum = NF_INET_LOCAL_OUT, 6547948bf85cSPaul Moore .priority = NF_IP_PRI_SELINUX_FIRST, 654825db6beaSJiri Pirko }, 65491a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 6550effad8dfSPaul Moore { 6551effad8dfSPaul Moore .hook = selinux_ipv6_postroute, 65522597a834SAlban Crequy .pf = NFPROTO_IPV6, 65536e23ae2aSPatrick McHardy .hooknum = NF_INET_POST_ROUTING, 65541da177e4SLinus Torvalds .priority = NF_IP6_PRI_SELINUX_LAST, 6555effad8dfSPaul Moore }, 6556effad8dfSPaul Moore { 6557effad8dfSPaul Moore .hook = selinux_ipv6_forward, 65582597a834SAlban Crequy .pf = NFPROTO_IPV6, 6559effad8dfSPaul Moore .hooknum = NF_INET_FORWARD, 6560effad8dfSPaul Moore .priority = NF_IP6_PRI_SELINUX_FIRST, 656125db6beaSJiri Pirko }, 65622917f57bSHuw Davies { 65632917f57bSHuw Davies .hook = selinux_ipv6_output, 65642917f57bSHuw Davies .pf = NFPROTO_IPV6, 65652917f57bSHuw Davies .hooknum = NF_INET_LOCAL_OUT, 65662917f57bSHuw Davies .priority = NF_IP6_PRI_SELINUX_FIRST, 65672917f57bSHuw Davies }, 65681da177e4SLinus Torvalds #endif /* IPV6 */ 656925db6beaSJiri Pirko }; 65701da177e4SLinus Torvalds 65718e71bf75SFlorian Westphal static int __net_init selinux_nf_register(struct net *net) 65728e71bf75SFlorian Westphal { 65738e71bf75SFlorian Westphal return nf_register_net_hooks(net, selinux_nf_ops, 65748e71bf75SFlorian Westphal ARRAY_SIZE(selinux_nf_ops)); 65758e71bf75SFlorian Westphal } 65768e71bf75SFlorian Westphal 65778e71bf75SFlorian Westphal static void __net_exit selinux_nf_unregister(struct net *net) 65788e71bf75SFlorian Westphal { 65798e71bf75SFlorian Westphal nf_unregister_net_hooks(net, selinux_nf_ops, 65808e71bf75SFlorian Westphal ARRAY_SIZE(selinux_nf_ops)); 65818e71bf75SFlorian Westphal } 65828e71bf75SFlorian Westphal 65838e71bf75SFlorian Westphal static struct pernet_operations selinux_net_ops = { 65848e71bf75SFlorian Westphal .init = selinux_nf_register, 65858e71bf75SFlorian Westphal .exit = selinux_nf_unregister, 65868e71bf75SFlorian Westphal }; 65878e71bf75SFlorian Westphal 65881da177e4SLinus Torvalds static int __init selinux_nf_ip_init(void) 65891da177e4SLinus Torvalds { 659025db6beaSJiri Pirko int err; 65911da177e4SLinus Torvalds 65921da177e4SLinus Torvalds if (!selinux_enabled) 659325db6beaSJiri Pirko return 0; 65941da177e4SLinus Torvalds 6595fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Registering netfilter hooks\n"); 65961da177e4SLinus Torvalds 65978e71bf75SFlorian Westphal err = register_pernet_subsys(&selinux_net_ops); 65981da177e4SLinus Torvalds if (err) 65998e71bf75SFlorian Westphal panic("SELinux: register_pernet_subsys: error %d\n", err); 66001da177e4SLinus Torvalds 660125db6beaSJiri Pirko return 0; 66021da177e4SLinus Torvalds } 66031da177e4SLinus Torvalds __initcall(selinux_nf_ip_init); 66041da177e4SLinus Torvalds 66051da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 66061da177e4SLinus Torvalds static void selinux_nf_ip_exit(void) 66071da177e4SLinus Torvalds { 6608fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Unregistering netfilter hooks\n"); 66091da177e4SLinus Torvalds 66108e71bf75SFlorian Westphal unregister_pernet_subsys(&selinux_net_ops); 66111da177e4SLinus Torvalds } 66121da177e4SLinus Torvalds #endif 66131da177e4SLinus Torvalds 6614c2b507fdSStephen Smalley #else /* CONFIG_NETFILTER */ 66151da177e4SLinus Torvalds 66161da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 66171da177e4SLinus Torvalds #define selinux_nf_ip_exit() 66181da177e4SLinus Torvalds #endif 66191da177e4SLinus Torvalds 6620c2b507fdSStephen Smalley #endif /* CONFIG_NETFILTER */ 66211da177e4SLinus Torvalds 66221da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 6623828dfe1dSEric Paris static int selinux_disabled; 6624828dfe1dSEric Paris 66251da177e4SLinus Torvalds int selinux_disable(void) 66261da177e4SLinus Torvalds { 66271da177e4SLinus Torvalds if (ss_initialized) { 66281da177e4SLinus Torvalds /* Not permitted after initial policy load. */ 66291da177e4SLinus Torvalds return -EINVAL; 66301da177e4SLinus Torvalds } 66311da177e4SLinus Torvalds 66321da177e4SLinus Torvalds if (selinux_disabled) { 66331da177e4SLinus Torvalds /* Only do this once. */ 66341da177e4SLinus Torvalds return -EINVAL; 66351da177e4SLinus Torvalds } 66361da177e4SLinus Torvalds 66371da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Disabled at runtime.\n"); 66381da177e4SLinus Torvalds 66391da177e4SLinus Torvalds selinux_disabled = 1; 664030d55280SStephen Smalley selinux_enabled = 0; 66411da177e4SLinus Torvalds 6642b1d9e6b0SCasey Schaufler security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks)); 66431da177e4SLinus Torvalds 6644af8ff049SEric Paris /* Try to destroy the avc node cache */ 6645af8ff049SEric Paris avc_disable(); 6646af8ff049SEric Paris 66471da177e4SLinus Torvalds /* Unregister netfilter hooks. */ 66481da177e4SLinus Torvalds selinux_nf_ip_exit(); 66491da177e4SLinus Torvalds 66501da177e4SLinus Torvalds /* Unregister selinuxfs. */ 66511da177e4SLinus Torvalds exit_sel_fs(); 66521da177e4SLinus Torvalds 66531da177e4SLinus Torvalds return 0; 66541da177e4SLinus Torvalds } 66551da177e4SLinus Torvalds #endif 6656