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> 70d452930fSRichard Haines #include <linux/sctp.h> 71d452930fSRichard Haines #include <net/sctp/structs.h> 721da177e4SLinus Torvalds #include <linux/quota.h> 731da177e4SLinus Torvalds #include <linux/un.h> /* for Unix socket types */ 741da177e4SLinus Torvalds #include <net/af_unix.h> /* for Unix socket types */ 751da177e4SLinus Torvalds #include <linux/parser.h> 761da177e4SLinus Torvalds #include <linux/nfs_mount.h> 771da177e4SLinus Torvalds #include <net/ipv6.h> 781da177e4SLinus Torvalds #include <linux/hugetlb.h> 791da177e4SLinus Torvalds #include <linux/personality.h> 801da177e4SLinus Torvalds #include <linux/audit.h> 816931dfc9SEric Paris #include <linux/string.h> 8223970741SEric Paris #include <linux/mutex.h> 83f06febc9SFrank Mayhar #include <linux/posix-timers.h> 8400234592SKees Cook #include <linux/syslog.h> 853486740aSSerge E. Hallyn #include <linux/user_namespace.h> 8644fc7ea0SPaul Gortmaker #include <linux/export.h> 8740401530SAl Viro #include <linux/msg.h> 8840401530SAl Viro #include <linux/shm.h> 89ec27c356SChenbo Feng #include <linux/bpf.h> 90e262e32dSDavid Howells #include <uapi/linux/mount.h> 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds #include "avc.h" 931da177e4SLinus Torvalds #include "objsec.h" 941da177e4SLinus Torvalds #include "netif.h" 95224dfbd8SPaul Moore #include "netnode.h" 963e112172SPaul Moore #include "netport.h" 97409dcf31SDaniel Jurgens #include "ibpkey.h" 98d28d1e08STrent Jaeger #include "xfrm.h" 99c60475bfSPaul Moore #include "netlabel.h" 1009d57a7f9SAhmed S. Darwish #include "audit.h" 1017b98a585SJames Morris #include "avc_ss.h" 1021da177e4SLinus Torvalds 103aa8e712cSStephen Smalley struct selinux_state selinux_state; 104aa8e712cSStephen Smalley 105d621d35eSPaul Moore /* SECMARK reference count */ 10656a4ca99SJames Morris static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0); 107d621d35eSPaul Moore 1081da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP 109aa8e712cSStephen Smalley static int selinux_enforcing_boot; 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds static int __init enforcing_setup(char *str) 1121da177e4SLinus Torvalds { 113f5269710SEric Paris unsigned long enforcing; 11429707b20SJingoo Han if (!kstrtoul(str, 0, &enforcing)) 115aa8e712cSStephen Smalley selinux_enforcing_boot = enforcing ? 1 : 0; 1161da177e4SLinus Torvalds return 1; 1171da177e4SLinus Torvalds } 1181da177e4SLinus Torvalds __setup("enforcing=", enforcing_setup); 119aa8e712cSStephen Smalley #else 120aa8e712cSStephen Smalley #define selinux_enforcing_boot 1 1211da177e4SLinus Torvalds #endif 1221da177e4SLinus Torvalds 123be6ec88fSKees Cook int selinux_enabled __lsm_ro_after_init = 1; 1241da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM 1251da177e4SLinus Torvalds static int __init selinux_enabled_setup(char *str) 1261da177e4SLinus Torvalds { 127f5269710SEric Paris unsigned long enabled; 12829707b20SJingoo Han if (!kstrtoul(str, 0, &enabled)) 129f5269710SEric Paris selinux_enabled = enabled ? 1 : 0; 1301da177e4SLinus Torvalds return 1; 1311da177e4SLinus Torvalds } 1321da177e4SLinus Torvalds __setup("selinux=", selinux_enabled_setup); 1331da177e4SLinus Torvalds #endif 1341da177e4SLinus Torvalds 135aa8e712cSStephen Smalley static unsigned int selinux_checkreqprot_boot = 136aa8e712cSStephen Smalley CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE; 137aa8e712cSStephen Smalley 138aa8e712cSStephen Smalley static int __init checkreqprot_setup(char *str) 139aa8e712cSStephen Smalley { 140aa8e712cSStephen Smalley unsigned long checkreqprot; 141aa8e712cSStephen Smalley 142aa8e712cSStephen Smalley if (!kstrtoul(str, 0, &checkreqprot)) 143aa8e712cSStephen Smalley selinux_checkreqprot_boot = checkreqprot ? 1 : 0; 144aa8e712cSStephen Smalley return 1; 145aa8e712cSStephen Smalley } 146aa8e712cSStephen Smalley __setup("checkreqprot=", checkreqprot_setup); 147aa8e712cSStephen Smalley 148e18b890bSChristoph Lameter static struct kmem_cache *sel_inode_cache; 14963205654SSangwoo static struct kmem_cache *file_security_cache; 1507cae7e26SJames Morris 151d621d35eSPaul Moore /** 152d621d35eSPaul Moore * selinux_secmark_enabled - Check to see if SECMARK is currently enabled 153d621d35eSPaul Moore * 154d621d35eSPaul Moore * Description: 155d621d35eSPaul Moore * This function checks the SECMARK reference counter to see if any SECMARK 156d621d35eSPaul Moore * targets are currently configured, if the reference counter is greater than 157d621d35eSPaul Moore * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is 1582be4d74fSChris PeBenito * enabled, false (0) if SECMARK is disabled. If the always_check_network 1592be4d74fSChris PeBenito * policy capability is enabled, SECMARK is always considered enabled. 160d621d35eSPaul Moore * 161d621d35eSPaul Moore */ 162d621d35eSPaul Moore static int selinux_secmark_enabled(void) 163d621d35eSPaul Moore { 164aa8e712cSStephen Smalley return (selinux_policycap_alwaysnetwork() || 165aa8e712cSStephen Smalley atomic_read(&selinux_secmark_refcount)); 1662be4d74fSChris PeBenito } 1672be4d74fSChris PeBenito 1682be4d74fSChris PeBenito /** 1692be4d74fSChris PeBenito * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled 1702be4d74fSChris PeBenito * 1712be4d74fSChris PeBenito * Description: 1722be4d74fSChris PeBenito * This function checks if NetLabel or labeled IPSEC is enabled. Returns true 1732be4d74fSChris PeBenito * (1) if any are enabled or false (0) if neither are enabled. If the 1742be4d74fSChris PeBenito * always_check_network policy capability is enabled, peer labeling 1752be4d74fSChris PeBenito * is always considered enabled. 1762be4d74fSChris PeBenito * 1772be4d74fSChris PeBenito */ 1782be4d74fSChris PeBenito static int selinux_peerlbl_enabled(void) 1792be4d74fSChris PeBenito { 180aa8e712cSStephen Smalley return (selinux_policycap_alwaysnetwork() || 181aa8e712cSStephen Smalley netlbl_enabled() || selinux_xfrm_enabled()); 182d621d35eSPaul Moore } 183d621d35eSPaul Moore 184615e51fdSPaul Moore static int selinux_netcache_avc_callback(u32 event) 185615e51fdSPaul Moore { 186615e51fdSPaul Moore if (event == AVC_CALLBACK_RESET) { 187615e51fdSPaul Moore sel_netif_flush(); 188615e51fdSPaul Moore sel_netnode_flush(); 189615e51fdSPaul Moore sel_netport_flush(); 190615e51fdSPaul Moore synchronize_net(); 191615e51fdSPaul Moore } 192615e51fdSPaul Moore return 0; 193615e51fdSPaul Moore } 194615e51fdSPaul Moore 1958f408ab6SDaniel Jurgens static int selinux_lsm_notifier_avc_callback(u32 event) 1968f408ab6SDaniel Jurgens { 197409dcf31SDaniel Jurgens if (event == AVC_CALLBACK_RESET) { 198409dcf31SDaniel Jurgens sel_ib_pkey_flush(); 1998f408ab6SDaniel Jurgens call_lsm_notifier(LSM_POLICY_CHANGE, NULL); 200409dcf31SDaniel Jurgens } 2018f408ab6SDaniel Jurgens 2028f408ab6SDaniel Jurgens return 0; 2038f408ab6SDaniel Jurgens } 2048f408ab6SDaniel Jurgens 205d84f4f99SDavid Howells /* 206d84f4f99SDavid Howells * initialise the security for the init task 207d84f4f99SDavid Howells */ 208d84f4f99SDavid Howells static void cred_init_security(void) 2091da177e4SLinus Torvalds { 2103b11a1deSDavid Howells struct cred *cred = (struct cred *) current->real_cred; 2111da177e4SLinus Torvalds struct task_security_struct *tsec; 2121da177e4SLinus Torvalds 213bbd3662aSCasey Schaufler lsm_early_cred(cred); 214bbd3662aSCasey Schaufler tsec = selinux_cred(cred); 215d84f4f99SDavid Howells tsec->osid = tsec->sid = SECINITSID_KERNEL; 2161da177e4SLinus Torvalds } 2171da177e4SLinus Torvalds 218275bb41eSDavid Howells /* 21988e67f3bSDavid Howells * get the security ID of a set of credentials 22088e67f3bSDavid Howells */ 22188e67f3bSDavid Howells static inline u32 cred_sid(const struct cred *cred) 22288e67f3bSDavid Howells { 22388e67f3bSDavid Howells const struct task_security_struct *tsec; 22488e67f3bSDavid Howells 2250c6cfa62SCasey Schaufler tsec = selinux_cred(cred); 22688e67f3bSDavid Howells return tsec->sid; 22788e67f3bSDavid Howells } 22888e67f3bSDavid Howells 22988e67f3bSDavid Howells /* 2303b11a1deSDavid Howells * get the objective security ID of a task 231275bb41eSDavid Howells */ 232275bb41eSDavid Howells static inline u32 task_sid(const struct task_struct *task) 233275bb41eSDavid Howells { 234275bb41eSDavid Howells u32 sid; 235275bb41eSDavid Howells 236275bb41eSDavid Howells rcu_read_lock(); 23788e67f3bSDavid Howells sid = cred_sid(__task_cred(task)); 238275bb41eSDavid Howells rcu_read_unlock(); 239275bb41eSDavid Howells return sid; 240275bb41eSDavid Howells } 241275bb41eSDavid Howells 24288e67f3bSDavid Howells /* Allocate and free functions for each kind of security blob. */ 24388e67f3bSDavid Howells 2441da177e4SLinus Torvalds static int inode_alloc_security(struct inode *inode) 2451da177e4SLinus Torvalds { 2461da177e4SLinus Torvalds struct inode_security_struct *isec; 247275bb41eSDavid Howells u32 sid = current_sid(); 2481da177e4SLinus Torvalds 249a02fe132SJosef Bacik isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS); 2501da177e4SLinus Torvalds if (!isec) 2511da177e4SLinus Torvalds return -ENOMEM; 2521da177e4SLinus Torvalds 2539287aed2SAndreas Gruenbacher spin_lock_init(&isec->lock); 2541da177e4SLinus Torvalds INIT_LIST_HEAD(&isec->list); 2551da177e4SLinus Torvalds isec->inode = inode; 2561da177e4SLinus Torvalds isec->sid = SECINITSID_UNLABELED; 2571da177e4SLinus Torvalds isec->sclass = SECCLASS_FILE; 258275bb41eSDavid Howells isec->task_sid = sid; 25942059112SAndreas Gruenbacher isec->initialized = LABEL_INVALID; 2601da177e4SLinus Torvalds inode->i_security = isec; 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds return 0; 2631da177e4SLinus Torvalds } 2641da177e4SLinus Torvalds 2655d226df4SAndreas Gruenbacher static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry); 2665d226df4SAndreas Gruenbacher 2675d226df4SAndreas Gruenbacher /* 2685d226df4SAndreas Gruenbacher * Try reloading inode security labels that have been marked as invalid. The 2695d226df4SAndreas Gruenbacher * @may_sleep parameter indicates when sleeping and thus reloading labels is 27042059112SAndreas Gruenbacher * allowed; when set to false, returns -ECHILD when the label is 271e9193288SAl Viro * invalid. The @dentry parameter should be set to a dentry of the inode. 2725d226df4SAndreas Gruenbacher */ 2735d226df4SAndreas Gruenbacher static int __inode_security_revalidate(struct inode *inode, 274e9193288SAl Viro struct dentry *dentry, 2755d226df4SAndreas Gruenbacher bool may_sleep) 2765d226df4SAndreas Gruenbacher { 2775d226df4SAndreas Gruenbacher struct inode_security_struct *isec = inode->i_security; 2785d226df4SAndreas Gruenbacher 2795d226df4SAndreas Gruenbacher might_sleep_if(may_sleep); 2805d226df4SAndreas Gruenbacher 281aa8e712cSStephen Smalley if (selinux_state.initialized && 282aa8e712cSStephen Smalley isec->initialized != LABEL_INITIALIZED) { 2835d226df4SAndreas Gruenbacher if (!may_sleep) 2845d226df4SAndreas Gruenbacher return -ECHILD; 2855d226df4SAndreas Gruenbacher 2865d226df4SAndreas Gruenbacher /* 2875d226df4SAndreas Gruenbacher * Try reloading the inode security label. This will fail if 2885d226df4SAndreas Gruenbacher * @opt_dentry is NULL and no dentry for this inode can be 2895d226df4SAndreas Gruenbacher * found; in that case, continue using the old label. 2905d226df4SAndreas Gruenbacher */ 291e9193288SAl Viro inode_doinit_with_dentry(inode, dentry); 2925d226df4SAndreas Gruenbacher } 2935d226df4SAndreas Gruenbacher return 0; 2945d226df4SAndreas Gruenbacher } 2955d226df4SAndreas Gruenbacher 2965d226df4SAndreas Gruenbacher static struct inode_security_struct *inode_security_novalidate(struct inode *inode) 2975d226df4SAndreas Gruenbacher { 2985d226df4SAndreas Gruenbacher return inode->i_security; 2995d226df4SAndreas Gruenbacher } 3005d226df4SAndreas Gruenbacher 3015d226df4SAndreas Gruenbacher static struct inode_security_struct *inode_security_rcu(struct inode *inode, bool rcu) 3025d226df4SAndreas Gruenbacher { 3035d226df4SAndreas Gruenbacher int error; 3045d226df4SAndreas Gruenbacher 3055d226df4SAndreas Gruenbacher error = __inode_security_revalidate(inode, NULL, !rcu); 3065d226df4SAndreas Gruenbacher if (error) 3075d226df4SAndreas Gruenbacher return ERR_PTR(error); 3085d226df4SAndreas Gruenbacher return inode->i_security; 3095d226df4SAndreas Gruenbacher } 3105d226df4SAndreas Gruenbacher 31183da53c5SAndreas Gruenbacher /* 31283da53c5SAndreas Gruenbacher * Get the security label of an inode. 31383da53c5SAndreas Gruenbacher */ 31483da53c5SAndreas Gruenbacher static struct inode_security_struct *inode_security(struct inode *inode) 31583da53c5SAndreas Gruenbacher { 3165d226df4SAndreas Gruenbacher __inode_security_revalidate(inode, NULL, true); 31783da53c5SAndreas Gruenbacher return inode->i_security; 31883da53c5SAndreas Gruenbacher } 31983da53c5SAndreas Gruenbacher 3202c97165bSPaul Moore static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry) 3212c97165bSPaul Moore { 3222c97165bSPaul Moore struct inode *inode = d_backing_inode(dentry); 3232c97165bSPaul Moore 3242c97165bSPaul Moore return inode->i_security; 3252c97165bSPaul Moore } 3262c97165bSPaul Moore 32783da53c5SAndreas Gruenbacher /* 32883da53c5SAndreas Gruenbacher * Get the security label of a dentry's backing inode. 32983da53c5SAndreas Gruenbacher */ 33083da53c5SAndreas Gruenbacher static struct inode_security_struct *backing_inode_security(struct dentry *dentry) 33183da53c5SAndreas Gruenbacher { 33283da53c5SAndreas Gruenbacher struct inode *inode = d_backing_inode(dentry); 33383da53c5SAndreas Gruenbacher 3345d226df4SAndreas Gruenbacher __inode_security_revalidate(inode, dentry, true); 33583da53c5SAndreas Gruenbacher return inode->i_security; 33683da53c5SAndreas Gruenbacher } 33783da53c5SAndreas Gruenbacher 3383dc91d43SSteven Rostedt static void inode_free_rcu(struct rcu_head *head) 3393dc91d43SSteven Rostedt { 3403dc91d43SSteven Rostedt struct inode_security_struct *isec; 3413dc91d43SSteven Rostedt 3423dc91d43SSteven Rostedt isec = container_of(head, struct inode_security_struct, rcu); 3433dc91d43SSteven Rostedt kmem_cache_free(sel_inode_cache, isec); 3443dc91d43SSteven Rostedt } 3453dc91d43SSteven Rostedt 3461da177e4SLinus Torvalds static void inode_free_security(struct inode *inode) 3471da177e4SLinus Torvalds { 3481da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 3491da177e4SLinus Torvalds struct superblock_security_struct *sbsec = inode->i_sb->s_security; 3501da177e4SLinus Torvalds 3519629d04aSWaiman Long /* 3529629d04aSWaiman Long * As not all inode security structures are in a list, we check for 3539629d04aSWaiman Long * empty list outside of the lock to make sure that we won't waste 3549629d04aSWaiman Long * time taking a lock doing nothing. 3559629d04aSWaiman Long * 3569629d04aSWaiman Long * The list_del_init() function can be safely called more than once. 3579629d04aSWaiman Long * It should not be possible for this function to be called with 3589629d04aSWaiman Long * concurrent list_add(), but for better safety against future changes 3599629d04aSWaiman Long * in the code, we use list_empty_careful() here. 3609629d04aSWaiman Long */ 3619629d04aSWaiman Long if (!list_empty_careful(&isec->list)) { 3621da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 3631da177e4SLinus Torvalds list_del_init(&isec->list); 3641da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 3659629d04aSWaiman Long } 3661da177e4SLinus Torvalds 3673dc91d43SSteven Rostedt /* 3683dc91d43SSteven Rostedt * The inode may still be referenced in a path walk and 3693dc91d43SSteven Rostedt * a call to selinux_inode_permission() can be made 3703dc91d43SSteven Rostedt * after inode_free_security() is called. Ideally, the VFS 3713dc91d43SSteven Rostedt * wouldn't do this, but fixing that is a much harder 3723dc91d43SSteven Rostedt * job. For now, simply free the i_security via RCU, and 3733dc91d43SSteven Rostedt * leave the current inode->i_security pointer intact. 3743dc91d43SSteven Rostedt * The inode will be freed after the RCU grace period too. 3753dc91d43SSteven Rostedt */ 3763dc91d43SSteven Rostedt call_rcu(&isec->rcu, inode_free_rcu); 3771da177e4SLinus Torvalds } 3781da177e4SLinus Torvalds 3791da177e4SLinus Torvalds static int file_alloc_security(struct file *file) 3801da177e4SLinus Torvalds { 3811da177e4SLinus Torvalds struct file_security_struct *fsec; 382275bb41eSDavid Howells u32 sid = current_sid(); 3831da177e4SLinus Torvalds 38463205654SSangwoo fsec = kmem_cache_zalloc(file_security_cache, GFP_KERNEL); 3851da177e4SLinus Torvalds if (!fsec) 3861da177e4SLinus Torvalds return -ENOMEM; 3871da177e4SLinus Torvalds 388275bb41eSDavid Howells fsec->sid = sid; 389275bb41eSDavid Howells fsec->fown_sid = sid; 3901da177e4SLinus Torvalds file->f_security = fsec; 3911da177e4SLinus Torvalds 3921da177e4SLinus Torvalds return 0; 3931da177e4SLinus Torvalds } 3941da177e4SLinus Torvalds 3951da177e4SLinus Torvalds static void file_free_security(struct file *file) 3961da177e4SLinus Torvalds { 397*bb6c6b02SCasey Schaufler struct file_security_struct *fsec = selinux_file(file); 3981da177e4SLinus Torvalds file->f_security = NULL; 39963205654SSangwoo kmem_cache_free(file_security_cache, fsec); 4001da177e4SLinus Torvalds } 4011da177e4SLinus Torvalds 4021da177e4SLinus Torvalds static int superblock_alloc_security(struct super_block *sb) 4031da177e4SLinus Torvalds { 4041da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 4051da177e4SLinus Torvalds 40689d155efSJames Morris sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL); 4071da177e4SLinus Torvalds if (!sbsec) 4081da177e4SLinus Torvalds return -ENOMEM; 4091da177e4SLinus Torvalds 410bc7e982bSEric Paris mutex_init(&sbsec->lock); 4111da177e4SLinus Torvalds INIT_LIST_HEAD(&sbsec->isec_head); 4121da177e4SLinus Torvalds spin_lock_init(&sbsec->isec_lock); 4131da177e4SLinus Torvalds sbsec->sb = sb; 4141da177e4SLinus Torvalds sbsec->sid = SECINITSID_UNLABELED; 4151da177e4SLinus Torvalds sbsec->def_sid = SECINITSID_FILE; 416c312feb2SEric Paris sbsec->mntpoint_sid = SECINITSID_UNLABELED; 4171da177e4SLinus Torvalds sb->s_security = sbsec; 4181da177e4SLinus Torvalds 4191da177e4SLinus Torvalds return 0; 4201da177e4SLinus Torvalds } 4211da177e4SLinus Torvalds 4221da177e4SLinus Torvalds static void superblock_free_security(struct super_block *sb) 4231da177e4SLinus Torvalds { 4241da177e4SLinus Torvalds struct superblock_security_struct *sbsec = sb->s_security; 4251da177e4SLinus Torvalds sb->s_security = NULL; 4261da177e4SLinus Torvalds kfree(sbsec); 4271da177e4SLinus Torvalds } 4281da177e4SLinus Torvalds 429bd323655SAl Viro struct selinux_mnt_opts { 430bd323655SAl Viro const char *fscontext, *context, *rootcontext, *defcontext; 431bd323655SAl Viro }; 432bd323655SAl Viro 433204cc0ccSAl Viro static void selinux_free_mnt_opts(void *mnt_opts) 434204cc0ccSAl Viro { 435bd323655SAl Viro struct selinux_mnt_opts *opts = mnt_opts; 436bd323655SAl Viro kfree(opts->fscontext); 437bd323655SAl Viro kfree(opts->context); 438bd323655SAl Viro kfree(opts->rootcontext); 439bd323655SAl Viro kfree(opts->defcontext); 440204cc0ccSAl Viro kfree(opts); 441204cc0ccSAl Viro } 442204cc0ccSAl Viro 4431da177e4SLinus Torvalds static inline int inode_doinit(struct inode *inode) 4441da177e4SLinus Torvalds { 4451da177e4SLinus Torvalds return inode_doinit_with_dentry(inode, NULL); 4461da177e4SLinus Torvalds } 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds enum { 44931e87930SEric Paris Opt_error = -1, 4501da177e4SLinus Torvalds Opt_context = 1, 4511da177e4SLinus Torvalds Opt_fscontext = 2, 452c9180a57SEric Paris Opt_defcontext = 3, 453c9180a57SEric Paris Opt_rootcontext = 4, 454da3d76abSAl Viro Opt_seclabel = 5, 4551da177e4SLinus Torvalds }; 4561da177e4SLinus Torvalds 457da3d76abSAl Viro #define A(s, has_arg) {#s, sizeof(#s) - 1, Opt_##s, has_arg} 458169d68efSAl Viro static struct { 459169d68efSAl Viro const char *name; 460169d68efSAl Viro int len; 461169d68efSAl Viro int opt; 462169d68efSAl Viro bool has_arg; 463169d68efSAl Viro } tokens[] = { 464da3d76abSAl Viro A(context, true), 465da3d76abSAl Viro A(fscontext, true), 466da3d76abSAl Viro A(defcontext, true), 467da3d76abSAl Viro A(rootcontext, true), 468da3d76abSAl Viro A(seclabel, false), 4691da177e4SLinus Torvalds }; 470169d68efSAl Viro #undef A 471169d68efSAl Viro 472169d68efSAl Viro static int match_opt_prefix(char *s, int l, char **arg) 473169d68efSAl Viro { 474169d68efSAl Viro int i; 475169d68efSAl Viro 476169d68efSAl Viro for (i = 0; i < ARRAY_SIZE(tokens); i++) { 477169d68efSAl Viro size_t len = tokens[i].len; 478169d68efSAl Viro if (len > l || memcmp(s, tokens[i].name, len)) 479169d68efSAl Viro continue; 480169d68efSAl Viro if (tokens[i].has_arg) { 481169d68efSAl Viro if (len == l || s[len] != '=') 482169d68efSAl Viro continue; 483169d68efSAl Viro *arg = s + len + 1; 484169d68efSAl Viro } else if (len != l) 485169d68efSAl Viro continue; 486169d68efSAl Viro return tokens[i].opt; 487169d68efSAl Viro } 488169d68efSAl Viro return Opt_error; 489169d68efSAl Viro } 4901da177e4SLinus Torvalds 4911da177e4SLinus Torvalds #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n" 4921da177e4SLinus Torvalds 493c312feb2SEric Paris static int may_context_mount_sb_relabel(u32 sid, 494c312feb2SEric Paris struct superblock_security_struct *sbsec, 495275bb41eSDavid Howells const struct cred *cred) 496c312feb2SEric Paris { 4970c6cfa62SCasey Schaufler const struct task_security_struct *tsec = selinux_cred(cred); 498c312feb2SEric Paris int rc; 499c312feb2SEric Paris 5006b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 5016b6bc620SStephen Smalley tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 502c312feb2SEric Paris FILESYSTEM__RELABELFROM, NULL); 503c312feb2SEric Paris if (rc) 504c312feb2SEric Paris return rc; 505c312feb2SEric Paris 5066b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 5076b6bc620SStephen Smalley tsec->sid, sid, SECCLASS_FILESYSTEM, 508c312feb2SEric Paris FILESYSTEM__RELABELTO, NULL); 509c312feb2SEric Paris return rc; 510c312feb2SEric Paris } 511c312feb2SEric Paris 5120808925eSEric Paris static int may_context_mount_inode_relabel(u32 sid, 5130808925eSEric Paris struct superblock_security_struct *sbsec, 514275bb41eSDavid Howells const struct cred *cred) 5150808925eSEric Paris { 5160c6cfa62SCasey Schaufler const struct task_security_struct *tsec = selinux_cred(cred); 5170808925eSEric Paris int rc; 5186b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 5196b6bc620SStephen Smalley tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 5200808925eSEric Paris FILESYSTEM__RELABELFROM, NULL); 5210808925eSEric Paris if (rc) 5220808925eSEric Paris return rc; 5230808925eSEric Paris 5246b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 5256b6bc620SStephen Smalley sid, sbsec->sid, SECCLASS_FILESYSTEM, 5260808925eSEric Paris FILESYSTEM__ASSOCIATE, NULL); 5270808925eSEric Paris return rc; 5280808925eSEric Paris } 5290808925eSEric Paris 530b43e725dSEric Paris static int selinux_is_sblabel_mnt(struct super_block *sb) 531b43e725dSEric Paris { 532b43e725dSEric Paris struct superblock_security_struct *sbsec = sb->s_security; 533b43e725dSEric Paris 534d5f3a5f6SMark Salyzyn return sbsec->behavior == SECURITY_FS_USE_XATTR || 535b43e725dSEric Paris sbsec->behavior == SECURITY_FS_USE_TRANS || 536d5f3a5f6SMark Salyzyn sbsec->behavior == SECURITY_FS_USE_TASK || 5379fc2b4b4SJ. Bruce Fields sbsec->behavior == SECURITY_FS_USE_NATIVE || 538d5f3a5f6SMark Salyzyn /* Special handling. Genfs but also in-core setxattr handler */ 539d5f3a5f6SMark Salyzyn !strcmp(sb->s_type->name, "sysfs") || 540d5f3a5f6SMark Salyzyn !strcmp(sb->s_type->name, "pstore") || 541d5f3a5f6SMark Salyzyn !strcmp(sb->s_type->name, "debugfs") || 542a2c7c6fbSYongqin Liu !strcmp(sb->s_type->name, "tracefs") || 5432651225bSStephen Smalley !strcmp(sb->s_type->name, "rootfs") || 544aa8e712cSStephen Smalley (selinux_policycap_cgroupseclabel() && 5452651225bSStephen Smalley (!strcmp(sb->s_type->name, "cgroup") || 5462651225bSStephen Smalley !strcmp(sb->s_type->name, "cgroup2"))); 547b43e725dSEric Paris } 548b43e725dSEric Paris 549c9180a57SEric Paris static int sb_finish_set_opts(struct super_block *sb) 5501da177e4SLinus Torvalds { 5511da177e4SLinus Torvalds struct superblock_security_struct *sbsec = sb->s_security; 5521da177e4SLinus Torvalds struct dentry *root = sb->s_root; 553c6f493d6SDavid Howells struct inode *root_inode = d_backing_inode(root); 5541da177e4SLinus Torvalds int rc = 0; 5551da177e4SLinus Torvalds 5561da177e4SLinus Torvalds if (sbsec->behavior == SECURITY_FS_USE_XATTR) { 5571da177e4SLinus Torvalds /* Make sure that the xattr handler exists and that no 5581da177e4SLinus Torvalds error other than -ENODATA is returned by getxattr on 5591da177e4SLinus Torvalds the root directory. -ENODATA is ok, as this may be 5601da177e4SLinus Torvalds the first boot of the SELinux kernel before we have 5611da177e4SLinus Torvalds assigned xattr values to the filesystem. */ 5625d6c3191SAndreas Gruenbacher if (!(root_inode->i_opflags & IOP_XATTR)) { 563c103a91eSpeter enderborg pr_warn("SELinux: (dev %s, type %s) has no " 56429b1deb2SLinus Torvalds "xattr support\n", sb->s_id, sb->s_type->name); 5651da177e4SLinus Torvalds rc = -EOPNOTSUPP; 5661da177e4SLinus Torvalds goto out; 5671da177e4SLinus Torvalds } 5685d6c3191SAndreas Gruenbacher 5695d6c3191SAndreas Gruenbacher rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0); 5701da177e4SLinus Torvalds if (rc < 0 && rc != -ENODATA) { 5711da177e4SLinus Torvalds if (rc == -EOPNOTSUPP) 572c103a91eSpeter enderborg pr_warn("SELinux: (dev %s, type " 57329b1deb2SLinus Torvalds "%s) has no security xattr handler\n", 57429b1deb2SLinus Torvalds sb->s_id, sb->s_type->name); 5751da177e4SLinus Torvalds else 576c103a91eSpeter enderborg pr_warn("SELinux: (dev %s, type " 57729b1deb2SLinus Torvalds "%s) getxattr errno %d\n", sb->s_id, 57829b1deb2SLinus Torvalds sb->s_type->name, -rc); 5791da177e4SLinus Torvalds goto out; 5801da177e4SLinus Torvalds } 5811da177e4SLinus Torvalds } 5821da177e4SLinus Torvalds 583eadcabc6SEric Paris sbsec->flags |= SE_SBINITIALIZED; 5840b4d3452SScott Mayhew 5850b4d3452SScott Mayhew /* 5860b4d3452SScott Mayhew * Explicitly set or clear SBLABEL_MNT. It's not sufficient to simply 5870b4d3452SScott Mayhew * leave the flag untouched because sb_clone_mnt_opts might be handing 5880b4d3452SScott Mayhew * us a superblock that needs the flag to be cleared. 5890b4d3452SScott Mayhew */ 590b43e725dSEric Paris if (selinux_is_sblabel_mnt(sb)) 59112f348b9SEric Paris sbsec->flags |= SBLABEL_MNT; 5920b4d3452SScott Mayhew else 5930b4d3452SScott Mayhew sbsec->flags &= ~SBLABEL_MNT; 594ddd29ec6SDavid P. Quigley 5951da177e4SLinus Torvalds /* Initialize the root inode. */ 596c9180a57SEric Paris rc = inode_doinit_with_dentry(root_inode, root); 5971da177e4SLinus Torvalds 5981da177e4SLinus Torvalds /* Initialize any other inodes associated with the superblock, e.g. 5991da177e4SLinus Torvalds inodes created prior to initial policy load or inodes created 6001da177e4SLinus Torvalds during get_sb by a pseudo filesystem that directly 6011da177e4SLinus Torvalds populates itself. */ 6021da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 6038d64124aSAl Viro while (!list_empty(&sbsec->isec_head)) { 6041da177e4SLinus Torvalds struct inode_security_struct *isec = 6058d64124aSAl Viro list_first_entry(&sbsec->isec_head, 6061da177e4SLinus Torvalds struct inode_security_struct, list); 6071da177e4SLinus Torvalds struct inode *inode = isec->inode; 608923190d3SStephen Smalley list_del_init(&isec->list); 6091da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 6101da177e4SLinus Torvalds inode = igrab(inode); 6111da177e4SLinus Torvalds if (inode) { 6121da177e4SLinus Torvalds if (!IS_PRIVATE(inode)) 6131da177e4SLinus Torvalds inode_doinit(inode); 6141da177e4SLinus Torvalds iput(inode); 6151da177e4SLinus Torvalds } 6161da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 6171da177e4SLinus Torvalds } 6181da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 6191da177e4SLinus Torvalds out: 620c9180a57SEric Paris return rc; 621c9180a57SEric Paris } 622c9180a57SEric Paris 623c9180a57SEric Paris static int bad_option(struct superblock_security_struct *sbsec, char flag, 624c9180a57SEric Paris u32 old_sid, u32 new_sid) 625c9180a57SEric Paris { 6260d90a7ecSDavid P. Quigley char mnt_flags = sbsec->flags & SE_MNTMASK; 6270d90a7ecSDavid P. Quigley 628c9180a57SEric Paris /* check if the old mount command had the same options */ 6290d90a7ecSDavid P. Quigley if (sbsec->flags & SE_SBINITIALIZED) 630c9180a57SEric Paris if (!(sbsec->flags & flag) || 631c9180a57SEric Paris (old_sid != new_sid)) 632c9180a57SEric Paris return 1; 633c9180a57SEric Paris 634c9180a57SEric Paris /* check if we were passed the same options twice, 635c9180a57SEric Paris * aka someone passed context=a,context=b 636c9180a57SEric Paris */ 6370d90a7ecSDavid P. Quigley if (!(sbsec->flags & SE_SBINITIALIZED)) 6380d90a7ecSDavid P. Quigley if (mnt_flags & flag) 639c9180a57SEric Paris return 1; 640c9180a57SEric Paris return 0; 641c9180a57SEric Paris } 642e0007529SEric Paris 643bd323655SAl Viro static int parse_sid(struct super_block *sb, const char *s, u32 *sid) 644bd323655SAl Viro { 645bd323655SAl Viro int rc = security_context_str_to_sid(&selinux_state, s, 646bd323655SAl Viro sid, GFP_KERNEL); 647bd323655SAl Viro if (rc) 648bd323655SAl Viro pr_warn("SELinux: security_context_str_to_sid" 649bd323655SAl Viro "(%s) failed for (dev %s, type %s) errno=%d\n", 650bd323655SAl Viro s, sb->s_id, sb->s_type->name, rc); 651bd323655SAl Viro return rc; 652bd323655SAl Viro } 653bd323655SAl Viro 654c9180a57SEric Paris /* 655c9180a57SEric Paris * Allow filesystems with binary mount data to explicitly set mount point 656c9180a57SEric Paris * labeling information. 657c9180a57SEric Paris */ 658e0007529SEric Paris static int selinux_set_mnt_opts(struct super_block *sb, 659204cc0ccSAl Viro void *mnt_opts, 660649f6e77SDavid Quigley unsigned long kern_flags, 661649f6e77SDavid Quigley unsigned long *set_kern_flags) 662c9180a57SEric Paris { 663275bb41eSDavid Howells const struct cred *cred = current_cred(); 664c9180a57SEric Paris struct superblock_security_struct *sbsec = sb->s_security; 66583da53c5SAndreas Gruenbacher struct dentry *root = sbsec->sb->s_root; 666bd323655SAl Viro struct selinux_mnt_opts *opts = mnt_opts; 6672c97165bSPaul Moore struct inode_security_struct *root_isec; 668c9180a57SEric Paris u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0; 669c9180a57SEric Paris u32 defcontext_sid = 0; 670bd323655SAl Viro int rc = 0; 671c9180a57SEric Paris 672c9180a57SEric Paris mutex_lock(&sbsec->lock); 673c9180a57SEric Paris 674aa8e712cSStephen Smalley if (!selinux_state.initialized) { 675bd323655SAl Viro if (!opts) { 676c9180a57SEric Paris /* Defer initialization until selinux_complete_init, 677c9180a57SEric Paris after the initial policy is loaded and the security 678c9180a57SEric Paris server is ready to handle calls. */ 679c9180a57SEric Paris goto out; 680c9180a57SEric Paris } 681c9180a57SEric Paris rc = -EINVAL; 682c103a91eSpeter enderborg pr_warn("SELinux: Unable to set superblock options " 683744ba35eSEric Paris "before the security server is initialized\n"); 684c9180a57SEric Paris goto out; 685c9180a57SEric Paris } 686649f6e77SDavid Quigley if (kern_flags && !set_kern_flags) { 687649f6e77SDavid Quigley /* Specifying internal flags without providing a place to 688649f6e77SDavid Quigley * place the results is not allowed */ 689649f6e77SDavid Quigley rc = -EINVAL; 690649f6e77SDavid Quigley goto out; 691649f6e77SDavid Quigley } 692c9180a57SEric Paris 693c9180a57SEric Paris /* 694e0007529SEric Paris * Binary mount data FS will come through this function twice. Once 695e0007529SEric Paris * from an explicit call and once from the generic calls from the vfs. 696e0007529SEric Paris * Since the generic VFS calls will not contain any security mount data 697e0007529SEric Paris * we need to skip the double mount verification. 698e0007529SEric Paris * 699e0007529SEric Paris * This does open a hole in which we will not notice if the first 700e0007529SEric Paris * mount using this sb set explict options and a second mount using 701e0007529SEric Paris * this sb does not set any security options. (The first options 702e0007529SEric Paris * will be used for both mounts) 703e0007529SEric Paris */ 7040d90a7ecSDavid P. Quigley if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA) 705bd323655SAl Viro && !opts) 706e0007529SEric Paris goto out; 707e0007529SEric Paris 7082c97165bSPaul Moore root_isec = backing_inode_security_novalidate(root); 7092c97165bSPaul Moore 710e0007529SEric Paris /* 711c9180a57SEric Paris * parse the mount options, check if they are valid sids. 712c9180a57SEric Paris * also check if someone is trying to mount the same sb more 713c9180a57SEric Paris * than once with different security options. 714c9180a57SEric Paris */ 715bd323655SAl Viro if (opts) { 716bd323655SAl Viro if (opts->fscontext) { 717bd323655SAl Viro rc = parse_sid(sb, opts->fscontext, &fscontext_sid); 718bd323655SAl Viro if (rc) 719c9180a57SEric Paris goto out; 720c9180a57SEric Paris if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, 721c9180a57SEric Paris fscontext_sid)) 722c9180a57SEric Paris goto out_double_mount; 723c9180a57SEric Paris sbsec->flags |= FSCONTEXT_MNT; 724bd323655SAl Viro } 725bd323655SAl Viro if (opts->context) { 726bd323655SAl Viro rc = parse_sid(sb, opts->context, &context_sid); 727bd323655SAl Viro if (rc) 728bd323655SAl Viro goto out; 729c9180a57SEric Paris if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, 730c9180a57SEric Paris context_sid)) 731c9180a57SEric Paris goto out_double_mount; 732c9180a57SEric Paris sbsec->flags |= CONTEXT_MNT; 733bd323655SAl Viro } 734bd323655SAl Viro if (opts->rootcontext) { 735bd323655SAl Viro rc = parse_sid(sb, opts->rootcontext, &rootcontext_sid); 736bd323655SAl Viro if (rc) 737bd323655SAl Viro goto out; 738c9180a57SEric Paris if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, 739c9180a57SEric Paris rootcontext_sid)) 740c9180a57SEric Paris goto out_double_mount; 741c9180a57SEric Paris sbsec->flags |= ROOTCONTEXT_MNT; 742bd323655SAl Viro } 743bd323655SAl Viro if (opts->defcontext) { 744bd323655SAl Viro rc = parse_sid(sb, opts->defcontext, &defcontext_sid); 745bd323655SAl Viro if (rc) 746bd323655SAl Viro goto out; 747c9180a57SEric Paris if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, 748c9180a57SEric Paris defcontext_sid)) 749c9180a57SEric Paris goto out_double_mount; 750c9180a57SEric Paris sbsec->flags |= DEFCONTEXT_MNT; 751c9180a57SEric Paris } 752c9180a57SEric Paris } 753c9180a57SEric Paris 7540d90a7ecSDavid P. Quigley if (sbsec->flags & SE_SBINITIALIZED) { 755c9180a57SEric Paris /* previously mounted with options, but not on this attempt? */ 756bd323655SAl Viro if ((sbsec->flags & SE_MNTMASK) && !opts) 757c9180a57SEric Paris goto out_double_mount; 758c9180a57SEric Paris rc = 0; 759c9180a57SEric Paris goto out; 760c9180a57SEric Paris } 761c9180a57SEric Paris 762089be43eSJames Morris if (strcmp(sb->s_type->name, "proc") == 0) 763134509d5SStephen Smalley sbsec->flags |= SE_SBPROC | SE_SBGENFS; 764134509d5SStephen Smalley 7658e014720SStephen Smalley if (!strcmp(sb->s_type->name, "debugfs") || 7666a391183SJeff Vander Stoep !strcmp(sb->s_type->name, "tracefs") || 7678e014720SStephen Smalley !strcmp(sb->s_type->name, "sysfs") || 768901ef845SAntonio Murdaca !strcmp(sb->s_type->name, "pstore") || 769901ef845SAntonio Murdaca !strcmp(sb->s_type->name, "cgroup") || 770901ef845SAntonio Murdaca !strcmp(sb->s_type->name, "cgroup2")) 771134509d5SStephen Smalley sbsec->flags |= SE_SBGENFS; 772c9180a57SEric Paris 773eb9ae686SDavid Quigley if (!sbsec->behavior) { 774eb9ae686SDavid Quigley /* 775eb9ae686SDavid Quigley * Determine the labeling behavior to use for this 776eb9ae686SDavid Quigley * filesystem type. 777eb9ae686SDavid Quigley */ 778aa8e712cSStephen Smalley rc = security_fs_use(&selinux_state, sb); 779c9180a57SEric Paris if (rc) { 780c103a91eSpeter enderborg pr_warn("%s: security_fs_use(%s) returned %d\n", 781089be43eSJames Morris __func__, sb->s_type->name, rc); 782c9180a57SEric Paris goto out; 783c9180a57SEric Paris } 784eb9ae686SDavid Quigley } 785aad82892SSeth Forshee 786aad82892SSeth Forshee /* 78701593d32SStephen Smalley * If this is a user namespace mount and the filesystem type is not 78801593d32SStephen Smalley * explicitly whitelisted, then no contexts are allowed on the command 78901593d32SStephen Smalley * line and security labels must be ignored. 790aad82892SSeth Forshee */ 79101593d32SStephen Smalley if (sb->s_user_ns != &init_user_ns && 79201593d32SStephen Smalley strcmp(sb->s_type->name, "tmpfs") && 79301593d32SStephen Smalley strcmp(sb->s_type->name, "ramfs") && 79401593d32SStephen Smalley strcmp(sb->s_type->name, "devpts")) { 795aad82892SSeth Forshee if (context_sid || fscontext_sid || rootcontext_sid || 796aad82892SSeth Forshee defcontext_sid) { 797aad82892SSeth Forshee rc = -EACCES; 798aad82892SSeth Forshee goto out; 799aad82892SSeth Forshee } 800aad82892SSeth Forshee if (sbsec->behavior == SECURITY_FS_USE_XATTR) { 801aad82892SSeth Forshee sbsec->behavior = SECURITY_FS_USE_MNTPOINT; 802aa8e712cSStephen Smalley rc = security_transition_sid(&selinux_state, 803aa8e712cSStephen Smalley current_sid(), 804aa8e712cSStephen Smalley current_sid(), 805aad82892SSeth Forshee SECCLASS_FILE, NULL, 806aad82892SSeth Forshee &sbsec->mntpoint_sid); 807aad82892SSeth Forshee if (rc) 808aad82892SSeth Forshee goto out; 809aad82892SSeth Forshee } 810aad82892SSeth Forshee goto out_set_opts; 811aad82892SSeth Forshee } 812aad82892SSeth Forshee 813c9180a57SEric Paris /* sets the context of the superblock for the fs being mounted. */ 814c9180a57SEric Paris if (fscontext_sid) { 815275bb41eSDavid Howells rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred); 816c9180a57SEric Paris if (rc) 817c9180a57SEric Paris goto out; 818c9180a57SEric Paris 819c9180a57SEric Paris sbsec->sid = fscontext_sid; 820c9180a57SEric Paris } 821c9180a57SEric Paris 822c9180a57SEric Paris /* 823c9180a57SEric Paris * Switch to using mount point labeling behavior. 824c9180a57SEric Paris * sets the label used on all file below the mountpoint, and will set 825c9180a57SEric Paris * the superblock context if not already set. 826c9180a57SEric Paris */ 827eb9ae686SDavid Quigley if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) { 828eb9ae686SDavid Quigley sbsec->behavior = SECURITY_FS_USE_NATIVE; 829eb9ae686SDavid Quigley *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS; 830eb9ae686SDavid Quigley } 831eb9ae686SDavid Quigley 832c9180a57SEric Paris if (context_sid) { 833c9180a57SEric Paris if (!fscontext_sid) { 834275bb41eSDavid Howells rc = may_context_mount_sb_relabel(context_sid, sbsec, 835275bb41eSDavid Howells cred); 836c9180a57SEric Paris if (rc) 837c9180a57SEric Paris goto out; 838c9180a57SEric Paris sbsec->sid = context_sid; 839c9180a57SEric Paris } else { 840275bb41eSDavid Howells rc = may_context_mount_inode_relabel(context_sid, sbsec, 841275bb41eSDavid Howells cred); 842c9180a57SEric Paris if (rc) 843c9180a57SEric Paris goto out; 844c9180a57SEric Paris } 845c9180a57SEric Paris if (!rootcontext_sid) 846c9180a57SEric Paris rootcontext_sid = context_sid; 847c9180a57SEric Paris 848c9180a57SEric Paris sbsec->mntpoint_sid = context_sid; 849c9180a57SEric Paris sbsec->behavior = SECURITY_FS_USE_MNTPOINT; 850c9180a57SEric Paris } 851c9180a57SEric Paris 852c9180a57SEric Paris if (rootcontext_sid) { 853275bb41eSDavid Howells rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec, 854275bb41eSDavid Howells cred); 855c9180a57SEric Paris if (rc) 856c9180a57SEric Paris goto out; 857c9180a57SEric Paris 858c9180a57SEric Paris root_isec->sid = rootcontext_sid; 8596f3be9f5SAndreas Gruenbacher root_isec->initialized = LABEL_INITIALIZED; 860c9180a57SEric Paris } 861c9180a57SEric Paris 862c9180a57SEric Paris if (defcontext_sid) { 863eb9ae686SDavid Quigley if (sbsec->behavior != SECURITY_FS_USE_XATTR && 864eb9ae686SDavid Quigley sbsec->behavior != SECURITY_FS_USE_NATIVE) { 865c9180a57SEric Paris rc = -EINVAL; 866c103a91eSpeter enderborg pr_warn("SELinux: defcontext option is " 867c9180a57SEric Paris "invalid for this filesystem type\n"); 868c9180a57SEric Paris goto out; 869c9180a57SEric Paris } 870c9180a57SEric Paris 871c9180a57SEric Paris if (defcontext_sid != sbsec->def_sid) { 872c9180a57SEric Paris rc = may_context_mount_inode_relabel(defcontext_sid, 873275bb41eSDavid Howells sbsec, cred); 874c9180a57SEric Paris if (rc) 875c9180a57SEric Paris goto out; 876c9180a57SEric Paris } 877c9180a57SEric Paris 878c9180a57SEric Paris sbsec->def_sid = defcontext_sid; 879c9180a57SEric Paris } 880c9180a57SEric Paris 881aad82892SSeth Forshee out_set_opts: 882c9180a57SEric Paris rc = sb_finish_set_opts(sb); 883c9180a57SEric Paris out: 884bc7e982bSEric Paris mutex_unlock(&sbsec->lock); 8851da177e4SLinus Torvalds return rc; 886c9180a57SEric Paris out_double_mount: 887c9180a57SEric Paris rc = -EINVAL; 888c103a91eSpeter enderborg pr_warn("SELinux: mount invalid. Same superblock, different " 889bd323655SAl Viro "security settings for (dev %s, type %s)\n", sb->s_id, 890bd323655SAl Viro sb->s_type->name); 891c9180a57SEric Paris goto out; 892c9180a57SEric Paris } 893c9180a57SEric Paris 894094f7b69SJeff Layton static int selinux_cmp_sb_context(const struct super_block *oldsb, 895094f7b69SJeff Layton const struct super_block *newsb) 896094f7b69SJeff Layton { 897094f7b69SJeff Layton struct superblock_security_struct *old = oldsb->s_security; 898094f7b69SJeff Layton struct superblock_security_struct *new = newsb->s_security; 899094f7b69SJeff Layton char oldflags = old->flags & SE_MNTMASK; 900094f7b69SJeff Layton char newflags = new->flags & SE_MNTMASK; 901094f7b69SJeff Layton 902094f7b69SJeff Layton if (oldflags != newflags) 903094f7b69SJeff Layton goto mismatch; 904094f7b69SJeff Layton if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid) 905094f7b69SJeff Layton goto mismatch; 906094f7b69SJeff Layton if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid) 907094f7b69SJeff Layton goto mismatch; 908094f7b69SJeff Layton if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid) 909094f7b69SJeff Layton goto mismatch; 910094f7b69SJeff Layton if (oldflags & ROOTCONTEXT_MNT) { 91183da53c5SAndreas Gruenbacher struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root); 91283da53c5SAndreas Gruenbacher struct inode_security_struct *newroot = backing_inode_security(newsb->s_root); 913094f7b69SJeff Layton if (oldroot->sid != newroot->sid) 914094f7b69SJeff Layton goto mismatch; 915094f7b69SJeff Layton } 916094f7b69SJeff Layton return 0; 917094f7b69SJeff Layton mismatch: 918c103a91eSpeter enderborg pr_warn("SELinux: mount invalid. Same superblock, " 919094f7b69SJeff Layton "different security settings for (dev %s, " 920094f7b69SJeff Layton "type %s)\n", newsb->s_id, newsb->s_type->name); 921094f7b69SJeff Layton return -EBUSY; 922094f7b69SJeff Layton } 923094f7b69SJeff Layton 924094f7b69SJeff Layton static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb, 9250b4d3452SScott Mayhew struct super_block *newsb, 9260b4d3452SScott Mayhew unsigned long kern_flags, 9270b4d3452SScott Mayhew unsigned long *set_kern_flags) 928c9180a57SEric Paris { 9290b4d3452SScott Mayhew int rc = 0; 930c9180a57SEric Paris const struct superblock_security_struct *oldsbsec = oldsb->s_security; 931c9180a57SEric Paris struct superblock_security_struct *newsbsec = newsb->s_security; 932c9180a57SEric Paris 933c9180a57SEric Paris int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT); 934c9180a57SEric Paris int set_context = (oldsbsec->flags & CONTEXT_MNT); 935c9180a57SEric Paris int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT); 936c9180a57SEric Paris 9370f5e6420SEric Paris /* 9380f5e6420SEric Paris * if the parent was able to be mounted it clearly had no special lsm 939e8c26255SAl Viro * mount options. thus we can safely deal with this superblock later 9400f5e6420SEric Paris */ 941aa8e712cSStephen Smalley if (!selinux_state.initialized) 942094f7b69SJeff Layton return 0; 943c9180a57SEric Paris 9440b4d3452SScott Mayhew /* 9450b4d3452SScott Mayhew * Specifying internal flags without providing a place to 9460b4d3452SScott Mayhew * place the results is not allowed. 9470b4d3452SScott Mayhew */ 9480b4d3452SScott Mayhew if (kern_flags && !set_kern_flags) 9490b4d3452SScott Mayhew return -EINVAL; 9500b4d3452SScott Mayhew 951c9180a57SEric Paris /* how can we clone if the old one wasn't set up?? */ 9520d90a7ecSDavid P. Quigley BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED)); 953c9180a57SEric Paris 954094f7b69SJeff Layton /* if fs is reusing a sb, make sure that the contexts match */ 9550d90a7ecSDavid P. Quigley if (newsbsec->flags & SE_SBINITIALIZED) 956094f7b69SJeff Layton return selinux_cmp_sb_context(oldsb, newsb); 9575a552617SEric Paris 958c9180a57SEric Paris mutex_lock(&newsbsec->lock); 959c9180a57SEric Paris 960c9180a57SEric Paris newsbsec->flags = oldsbsec->flags; 961c9180a57SEric Paris 962c9180a57SEric Paris newsbsec->sid = oldsbsec->sid; 963c9180a57SEric Paris newsbsec->def_sid = oldsbsec->def_sid; 964c9180a57SEric Paris newsbsec->behavior = oldsbsec->behavior; 965c9180a57SEric Paris 9660b4d3452SScott Mayhew if (newsbsec->behavior == SECURITY_FS_USE_NATIVE && 9670b4d3452SScott Mayhew !(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) { 968aa8e712cSStephen Smalley rc = security_fs_use(&selinux_state, newsb); 9690b4d3452SScott Mayhew if (rc) 9700b4d3452SScott Mayhew goto out; 9710b4d3452SScott Mayhew } 9720b4d3452SScott Mayhew 9730b4d3452SScott Mayhew if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) { 9740b4d3452SScott Mayhew newsbsec->behavior = SECURITY_FS_USE_NATIVE; 9750b4d3452SScott Mayhew *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS; 9760b4d3452SScott Mayhew } 9770b4d3452SScott Mayhew 978c9180a57SEric Paris if (set_context) { 979c9180a57SEric Paris u32 sid = oldsbsec->mntpoint_sid; 980c9180a57SEric Paris 981c9180a57SEric Paris if (!set_fscontext) 982c9180a57SEric Paris newsbsec->sid = sid; 983c9180a57SEric Paris if (!set_rootcontext) { 98483da53c5SAndreas Gruenbacher struct inode_security_struct *newisec = backing_inode_security(newsb->s_root); 985c9180a57SEric Paris newisec->sid = sid; 986c9180a57SEric Paris } 987c9180a57SEric Paris newsbsec->mntpoint_sid = sid; 988c9180a57SEric Paris } 989c9180a57SEric Paris if (set_rootcontext) { 99083da53c5SAndreas Gruenbacher const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root); 99183da53c5SAndreas Gruenbacher struct inode_security_struct *newisec = backing_inode_security(newsb->s_root); 992c9180a57SEric Paris 993c9180a57SEric Paris newisec->sid = oldisec->sid; 994c9180a57SEric Paris } 995c9180a57SEric Paris 996c9180a57SEric Paris sb_finish_set_opts(newsb); 9970b4d3452SScott Mayhew out: 998c9180a57SEric Paris mutex_unlock(&newsbsec->lock); 9990b4d3452SScott Mayhew return rc; 1000c9180a57SEric Paris } 1001c9180a57SEric Paris 1002ba641862SAl Viro static int selinux_add_opt(int token, const char *s, void **mnt_opts) 1003c9180a57SEric Paris { 1004ba641862SAl Viro struct selinux_mnt_opts *opts = *mnt_opts; 1005c9180a57SEric Paris 1006da3d76abSAl Viro if (token == Opt_seclabel) /* eaten and completely ignored */ 1007e0007529SEric Paris return 0; 1008e0007529SEric Paris 1009ba641862SAl Viro if (!opts) { 1010ba641862SAl Viro opts = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL); 1011ba641862SAl Viro if (!opts) 1012ba641862SAl Viro return -ENOMEM; 1013ba641862SAl Viro *mnt_opts = opts; 1014ba641862SAl Viro } 1015ba641862SAl Viro if (!s) 1016ba641862SAl Viro return -ENOMEM; 1017ba641862SAl Viro switch (token) { 1018ba641862SAl Viro case Opt_context: 1019ba641862SAl Viro if (opts->context || opts->defcontext) 1020ba641862SAl Viro goto Einval; 1021ba641862SAl Viro opts->context = s; 1022ba641862SAl Viro break; 1023ba641862SAl Viro case Opt_fscontext: 1024ba641862SAl Viro if (opts->fscontext) 1025ba641862SAl Viro goto Einval; 1026ba641862SAl Viro opts->fscontext = s; 1027ba641862SAl Viro break; 1028ba641862SAl Viro case Opt_rootcontext: 1029ba641862SAl Viro if (opts->rootcontext) 1030ba641862SAl Viro goto Einval; 1031ba641862SAl Viro opts->rootcontext = s; 1032ba641862SAl Viro break; 1033ba641862SAl Viro case Opt_defcontext: 1034ba641862SAl Viro if (opts->context || opts->defcontext) 1035ba641862SAl Viro goto Einval; 1036ba641862SAl Viro opts->defcontext = s; 1037ba641862SAl Viro break; 1038ba641862SAl Viro } 1039ba641862SAl Viro return 0; 1040ba641862SAl Viro Einval: 1041ba641862SAl Viro pr_warn(SEL_MOUNT_FAIL_MSG); 1042ba641862SAl Viro return -EINVAL; 1043ba641862SAl Viro } 1044ba641862SAl Viro 1045757cbe59SAl Viro static int selinux_add_mnt_opt(const char *option, const char *val, int len, 1046204cc0ccSAl Viro void **mnt_opts) 1047c9180a57SEric Paris { 1048757cbe59SAl Viro int token = Opt_error; 1049757cbe59SAl Viro int rc, i; 1050c9180a57SEric Paris 1051757cbe59SAl Viro for (i = 0; i < ARRAY_SIZE(tokens); i++) { 1052757cbe59SAl Viro if (strcmp(option, tokens[i].name) == 0) { 1053757cbe59SAl Viro token = tokens[i].opt; 1054757cbe59SAl Viro break; 1055757cbe59SAl Viro } 1056169d68efSAl Viro } 1057169d68efSAl Viro 1058757cbe59SAl Viro if (token == Opt_error) 1059757cbe59SAl Viro return -EINVAL; 1060c9180a57SEric Paris 1061757cbe59SAl Viro if (token != Opt_seclabel) 1062757cbe59SAl Viro val = kmemdup_nul(val, len, GFP_KERNEL); 1063757cbe59SAl Viro rc = selinux_add_opt(token, val, mnt_opts); 1064757cbe59SAl Viro if (unlikely(rc)) { 1065757cbe59SAl Viro kfree(val); 1066757cbe59SAl Viro if (*mnt_opts) { 1067ba641862SAl Viro selinux_free_mnt_opts(*mnt_opts); 1068ba641862SAl Viro *mnt_opts = NULL; 1069757cbe59SAl Viro } 1070757cbe59SAl Viro } 1071c9180a57SEric Paris return rc; 10721da177e4SLinus Torvalds } 10731da177e4SLinus Torvalds 1074e3489f89SAl Viro static int show_sid(struct seq_file *m, u32 sid) 10752069f457SEric Paris { 1076e3489f89SAl Viro char *context = NULL; 1077e3489f89SAl Viro u32 len; 1078e3489f89SAl Viro int rc; 10792069f457SEric Paris 1080e3489f89SAl Viro rc = security_sid_to_context(&selinux_state, sid, 1081e3489f89SAl Viro &context, &len); 1082e3489f89SAl Viro if (!rc) { 1083e3489f89SAl Viro bool has_comma = context && strchr(context, ','); 108411689d47SDavid P. Quigley 10852069f457SEric Paris if (has_comma) 10862069f457SEric Paris seq_putc(m, '\"'); 1087e3489f89SAl Viro seq_escape(m, context, "\"\n\\"); 10882069f457SEric Paris if (has_comma) 10892069f457SEric Paris seq_putc(m, '\"'); 10902069f457SEric Paris } 10911da177e4SLinus Torvalds kfree(context); 10921da177e4SLinus Torvalds return rc; 10931da177e4SLinus Torvalds } 10942069f457SEric Paris 10952069f457SEric Paris static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb) 10962069f457SEric Paris { 1097e3489f89SAl Viro struct superblock_security_struct *sbsec = sb->s_security; 10982069f457SEric Paris int rc; 10992069f457SEric Paris 1100e3489f89SAl Viro if (!(sbsec->flags & SE_SBINITIALIZED)) 1101e3489f89SAl Viro return 0; 1102e3489f89SAl Viro 1103e3489f89SAl Viro if (!selinux_state.initialized) 1104e3489f89SAl Viro return 0; 1105e3489f89SAl Viro 1106e3489f89SAl Viro if (sbsec->flags & FSCONTEXT_MNT) { 1107e3489f89SAl Viro seq_putc(m, ','); 1108e3489f89SAl Viro seq_puts(m, FSCONTEXT_STR); 1109e3489f89SAl Viro rc = show_sid(m, sbsec->sid); 1110e3489f89SAl Viro if (rc) 11112069f457SEric Paris return rc; 1112383795c2SEric Paris } 1113e3489f89SAl Viro if (sbsec->flags & CONTEXT_MNT) { 1114e3489f89SAl Viro seq_putc(m, ','); 1115e3489f89SAl Viro seq_puts(m, CONTEXT_STR); 1116e3489f89SAl Viro rc = show_sid(m, sbsec->mntpoint_sid); 1117e3489f89SAl Viro if (rc) 11182069f457SEric Paris return rc; 11192069f457SEric Paris } 1120e3489f89SAl Viro if (sbsec->flags & DEFCONTEXT_MNT) { 1121e3489f89SAl Viro seq_putc(m, ','); 1122e3489f89SAl Viro seq_puts(m, DEFCONTEXT_STR); 1123e3489f89SAl Viro rc = show_sid(m, sbsec->def_sid); 1124e3489f89SAl Viro if (rc) 1125e3489f89SAl Viro return rc; 1126e3489f89SAl Viro } 1127e3489f89SAl Viro if (sbsec->flags & ROOTCONTEXT_MNT) { 1128e3489f89SAl Viro struct dentry *root = sbsec->sb->s_root; 1129e3489f89SAl Viro struct inode_security_struct *isec = backing_inode_security(root); 1130e3489f89SAl Viro seq_putc(m, ','); 1131e3489f89SAl Viro seq_puts(m, ROOTCONTEXT_STR); 1132e3489f89SAl Viro rc = show_sid(m, isec->sid); 1133e3489f89SAl Viro if (rc) 1134e3489f89SAl Viro return rc; 1135e3489f89SAl Viro } 1136e3489f89SAl Viro if (sbsec->flags & SBLABEL_MNT) { 1137e3489f89SAl Viro seq_putc(m, ','); 1138e3489f89SAl Viro seq_puts(m, LABELSUPP_STR); 1139e3489f89SAl Viro } 1140e3489f89SAl Viro return 0; 1141e3489f89SAl Viro } 11422069f457SEric Paris 11431da177e4SLinus Torvalds static inline u16 inode_mode_to_security_class(umode_t mode) 11441da177e4SLinus Torvalds { 11451da177e4SLinus Torvalds switch (mode & S_IFMT) { 11461da177e4SLinus Torvalds case S_IFSOCK: 11471da177e4SLinus Torvalds return SECCLASS_SOCK_FILE; 11481da177e4SLinus Torvalds case S_IFLNK: 11491da177e4SLinus Torvalds return SECCLASS_LNK_FILE; 11501da177e4SLinus Torvalds case S_IFREG: 11511da177e4SLinus Torvalds return SECCLASS_FILE; 11521da177e4SLinus Torvalds case S_IFBLK: 11531da177e4SLinus Torvalds return SECCLASS_BLK_FILE; 11541da177e4SLinus Torvalds case S_IFDIR: 11551da177e4SLinus Torvalds return SECCLASS_DIR; 11561da177e4SLinus Torvalds case S_IFCHR: 11571da177e4SLinus Torvalds return SECCLASS_CHR_FILE; 11581da177e4SLinus Torvalds case S_IFIFO: 11591da177e4SLinus Torvalds return SECCLASS_FIFO_FILE; 11601da177e4SLinus Torvalds 11611da177e4SLinus Torvalds } 11621da177e4SLinus Torvalds 11631da177e4SLinus Torvalds return SECCLASS_FILE; 11641da177e4SLinus Torvalds } 11651da177e4SLinus Torvalds 116613402580SJames Morris static inline int default_protocol_stream(int protocol) 116713402580SJames Morris { 116813402580SJames Morris return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP); 116913402580SJames Morris } 117013402580SJames Morris 117113402580SJames Morris static inline int default_protocol_dgram(int protocol) 117213402580SJames Morris { 117313402580SJames Morris return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP); 117413402580SJames Morris } 117513402580SJames Morris 11761da177e4SLinus Torvalds static inline u16 socket_type_to_security_class(int family, int type, int protocol) 11771da177e4SLinus Torvalds { 1178aa8e712cSStephen Smalley int extsockclass = selinux_policycap_extsockclass(); 1179da69a530SStephen Smalley 11801da177e4SLinus Torvalds switch (family) { 11811da177e4SLinus Torvalds case PF_UNIX: 11821da177e4SLinus Torvalds switch (type) { 11831da177e4SLinus Torvalds case SOCK_STREAM: 11841da177e4SLinus Torvalds case SOCK_SEQPACKET: 11851da177e4SLinus Torvalds return SECCLASS_UNIX_STREAM_SOCKET; 11861da177e4SLinus Torvalds case SOCK_DGRAM: 11872a764b52SLuis Ressel case SOCK_RAW: 11881da177e4SLinus Torvalds return SECCLASS_UNIX_DGRAM_SOCKET; 11891da177e4SLinus Torvalds } 11901da177e4SLinus Torvalds break; 11911da177e4SLinus Torvalds case PF_INET: 11921da177e4SLinus Torvalds case PF_INET6: 11931da177e4SLinus Torvalds switch (type) { 11941da177e4SLinus Torvalds case SOCK_STREAM: 1195da69a530SStephen Smalley case SOCK_SEQPACKET: 119613402580SJames Morris if (default_protocol_stream(protocol)) 11971da177e4SLinus Torvalds return SECCLASS_TCP_SOCKET; 1198da69a530SStephen Smalley else if (extsockclass && protocol == IPPROTO_SCTP) 1199da69a530SStephen Smalley return SECCLASS_SCTP_SOCKET; 120013402580SJames Morris else 120113402580SJames Morris return SECCLASS_RAWIP_SOCKET; 12021da177e4SLinus Torvalds case SOCK_DGRAM: 120313402580SJames Morris if (default_protocol_dgram(protocol)) 12041da177e4SLinus Torvalds return SECCLASS_UDP_SOCKET; 1205ef37979aSStephen Smalley else if (extsockclass && (protocol == IPPROTO_ICMP || 1206ef37979aSStephen Smalley protocol == IPPROTO_ICMPV6)) 1207da69a530SStephen Smalley return SECCLASS_ICMP_SOCKET; 120813402580SJames Morris else 120913402580SJames Morris return SECCLASS_RAWIP_SOCKET; 12102ee92d46SJames Morris case SOCK_DCCP: 12112ee92d46SJames Morris return SECCLASS_DCCP_SOCKET; 121213402580SJames Morris default: 12131da177e4SLinus Torvalds return SECCLASS_RAWIP_SOCKET; 12141da177e4SLinus Torvalds } 12151da177e4SLinus Torvalds break; 12161da177e4SLinus Torvalds case PF_NETLINK: 12171da177e4SLinus Torvalds switch (protocol) { 12181da177e4SLinus Torvalds case NETLINK_ROUTE: 12191da177e4SLinus Torvalds return SECCLASS_NETLINK_ROUTE_SOCKET; 12207f1fb60cSPavel Emelyanov case NETLINK_SOCK_DIAG: 12211da177e4SLinus Torvalds return SECCLASS_NETLINK_TCPDIAG_SOCKET; 12221da177e4SLinus Torvalds case NETLINK_NFLOG: 12231da177e4SLinus Torvalds return SECCLASS_NETLINK_NFLOG_SOCKET; 12241da177e4SLinus Torvalds case NETLINK_XFRM: 12251da177e4SLinus Torvalds return SECCLASS_NETLINK_XFRM_SOCKET; 12261da177e4SLinus Torvalds case NETLINK_SELINUX: 12271da177e4SLinus Torvalds return SECCLASS_NETLINK_SELINUX_SOCKET; 12286c6d2e9bSStephen Smalley case NETLINK_ISCSI: 12296c6d2e9bSStephen Smalley return SECCLASS_NETLINK_ISCSI_SOCKET; 12301da177e4SLinus Torvalds case NETLINK_AUDIT: 12311da177e4SLinus Torvalds return SECCLASS_NETLINK_AUDIT_SOCKET; 12326c6d2e9bSStephen Smalley case NETLINK_FIB_LOOKUP: 12336c6d2e9bSStephen Smalley return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET; 12346c6d2e9bSStephen Smalley case NETLINK_CONNECTOR: 12356c6d2e9bSStephen Smalley return SECCLASS_NETLINK_CONNECTOR_SOCKET; 12366c6d2e9bSStephen Smalley case NETLINK_NETFILTER: 12376c6d2e9bSStephen Smalley return SECCLASS_NETLINK_NETFILTER_SOCKET; 12381da177e4SLinus Torvalds case NETLINK_DNRTMSG: 12391da177e4SLinus Torvalds return SECCLASS_NETLINK_DNRT_SOCKET; 12400c9b7942SJames Morris case NETLINK_KOBJECT_UEVENT: 12410c9b7942SJames Morris return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET; 12426c6d2e9bSStephen Smalley case NETLINK_GENERIC: 12436c6d2e9bSStephen Smalley return SECCLASS_NETLINK_GENERIC_SOCKET; 12446c6d2e9bSStephen Smalley case NETLINK_SCSITRANSPORT: 12456c6d2e9bSStephen Smalley return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET; 12466c6d2e9bSStephen Smalley case NETLINK_RDMA: 12476c6d2e9bSStephen Smalley return SECCLASS_NETLINK_RDMA_SOCKET; 12486c6d2e9bSStephen Smalley case NETLINK_CRYPTO: 12496c6d2e9bSStephen Smalley return SECCLASS_NETLINK_CRYPTO_SOCKET; 12501da177e4SLinus Torvalds default: 12511da177e4SLinus Torvalds return SECCLASS_NETLINK_SOCKET; 12521da177e4SLinus Torvalds } 12531da177e4SLinus Torvalds case PF_PACKET: 12541da177e4SLinus Torvalds return SECCLASS_PACKET_SOCKET; 12551da177e4SLinus Torvalds case PF_KEY: 12561da177e4SLinus Torvalds return SECCLASS_KEY_SOCKET; 12573e3ff15eSChristopher J. PeBenito case PF_APPLETALK: 12583e3ff15eSChristopher J. PeBenito return SECCLASS_APPLETALK_SOCKET; 12591da177e4SLinus Torvalds } 12601da177e4SLinus Torvalds 1261da69a530SStephen Smalley if (extsockclass) { 1262da69a530SStephen Smalley switch (family) { 1263da69a530SStephen Smalley case PF_AX25: 1264da69a530SStephen Smalley return SECCLASS_AX25_SOCKET; 1265da69a530SStephen Smalley case PF_IPX: 1266da69a530SStephen Smalley return SECCLASS_IPX_SOCKET; 1267da69a530SStephen Smalley case PF_NETROM: 1268da69a530SStephen Smalley return SECCLASS_NETROM_SOCKET; 1269da69a530SStephen Smalley case PF_ATMPVC: 1270da69a530SStephen Smalley return SECCLASS_ATMPVC_SOCKET; 1271da69a530SStephen Smalley case PF_X25: 1272da69a530SStephen Smalley return SECCLASS_X25_SOCKET; 1273da69a530SStephen Smalley case PF_ROSE: 1274da69a530SStephen Smalley return SECCLASS_ROSE_SOCKET; 1275da69a530SStephen Smalley case PF_DECnet: 1276da69a530SStephen Smalley return SECCLASS_DECNET_SOCKET; 1277da69a530SStephen Smalley case PF_ATMSVC: 1278da69a530SStephen Smalley return SECCLASS_ATMSVC_SOCKET; 1279da69a530SStephen Smalley case PF_RDS: 1280da69a530SStephen Smalley return SECCLASS_RDS_SOCKET; 1281da69a530SStephen Smalley case PF_IRDA: 1282da69a530SStephen Smalley return SECCLASS_IRDA_SOCKET; 1283da69a530SStephen Smalley case PF_PPPOX: 1284da69a530SStephen Smalley return SECCLASS_PPPOX_SOCKET; 1285da69a530SStephen Smalley case PF_LLC: 1286da69a530SStephen Smalley return SECCLASS_LLC_SOCKET; 1287da69a530SStephen Smalley case PF_CAN: 1288da69a530SStephen Smalley return SECCLASS_CAN_SOCKET; 1289da69a530SStephen Smalley case PF_TIPC: 1290da69a530SStephen Smalley return SECCLASS_TIPC_SOCKET; 1291da69a530SStephen Smalley case PF_BLUETOOTH: 1292da69a530SStephen Smalley return SECCLASS_BLUETOOTH_SOCKET; 1293da69a530SStephen Smalley case PF_IUCV: 1294da69a530SStephen Smalley return SECCLASS_IUCV_SOCKET; 1295da69a530SStephen Smalley case PF_RXRPC: 1296da69a530SStephen Smalley return SECCLASS_RXRPC_SOCKET; 1297da69a530SStephen Smalley case PF_ISDN: 1298da69a530SStephen Smalley return SECCLASS_ISDN_SOCKET; 1299da69a530SStephen Smalley case PF_PHONET: 1300da69a530SStephen Smalley return SECCLASS_PHONET_SOCKET; 1301da69a530SStephen Smalley case PF_IEEE802154: 1302da69a530SStephen Smalley return SECCLASS_IEEE802154_SOCKET; 1303da69a530SStephen Smalley case PF_CAIF: 1304da69a530SStephen Smalley return SECCLASS_CAIF_SOCKET; 1305da69a530SStephen Smalley case PF_ALG: 1306da69a530SStephen Smalley return SECCLASS_ALG_SOCKET; 1307da69a530SStephen Smalley case PF_NFC: 1308da69a530SStephen Smalley return SECCLASS_NFC_SOCKET; 1309da69a530SStephen Smalley case PF_VSOCK: 1310da69a530SStephen Smalley return SECCLASS_VSOCK_SOCKET; 1311da69a530SStephen Smalley case PF_KCM: 1312da69a530SStephen Smalley return SECCLASS_KCM_SOCKET; 1313da69a530SStephen Smalley case PF_QIPCRTR: 1314da69a530SStephen Smalley return SECCLASS_QIPCRTR_SOCKET; 13153051bf36SLinus Torvalds case PF_SMC: 13163051bf36SLinus Torvalds return SECCLASS_SMC_SOCKET; 131768e8b849SBjörn Töpel case PF_XDP: 131868e8b849SBjörn Töpel return SECCLASS_XDP_SOCKET; 131968e8b849SBjörn Töpel #if PF_MAX > 45 1320da69a530SStephen Smalley #error New address family defined, please update this function. 1321da69a530SStephen Smalley #endif 1322da69a530SStephen Smalley } 1323da69a530SStephen Smalley } 1324da69a530SStephen Smalley 13251da177e4SLinus Torvalds return SECCLASS_SOCKET; 13261da177e4SLinus Torvalds } 13271da177e4SLinus Torvalds 1328134509d5SStephen Smalley static int selinux_genfs_get_sid(struct dentry *dentry, 13291da177e4SLinus Torvalds u16 tclass, 1330134509d5SStephen Smalley u16 flags, 13311da177e4SLinus Torvalds u32 *sid) 13321da177e4SLinus Torvalds { 13338e6c9693SLucian Adrian Grijincu int rc; 1334fc64005cSAl Viro struct super_block *sb = dentry->d_sb; 13358e6c9693SLucian Adrian Grijincu char *buffer, *path; 13361da177e4SLinus Torvalds 13371da177e4SLinus Torvalds buffer = (char *)__get_free_page(GFP_KERNEL); 13381da177e4SLinus Torvalds if (!buffer) 13391da177e4SLinus Torvalds return -ENOMEM; 13401da177e4SLinus Torvalds 13418e6c9693SLucian Adrian Grijincu path = dentry_path_raw(dentry, buffer, PAGE_SIZE); 13428e6c9693SLucian Adrian Grijincu if (IS_ERR(path)) 13438e6c9693SLucian Adrian Grijincu rc = PTR_ERR(path); 13448e6c9693SLucian Adrian Grijincu else { 1345134509d5SStephen Smalley if (flags & SE_SBPROC) { 13468e6c9693SLucian Adrian Grijincu /* each process gets a /proc/PID/ entry. Strip off the 13478e6c9693SLucian Adrian Grijincu * PID part to get a valid selinux labeling. 13488e6c9693SLucian Adrian Grijincu * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */ 13498e6c9693SLucian Adrian Grijincu while (path[1] >= '0' && path[1] <= '9') { 13508e6c9693SLucian Adrian Grijincu path[1] = '/'; 13518e6c9693SLucian Adrian Grijincu path++; 13521da177e4SLinus Torvalds } 1353134509d5SStephen Smalley } 1354aa8e712cSStephen Smalley rc = security_genfs_sid(&selinux_state, sb->s_type->name, 1355aa8e712cSStephen Smalley path, tclass, sid); 13567bb185edSStephen Smalley if (rc == -ENOENT) { 13577bb185edSStephen Smalley /* No match in policy, mark as unlabeled. */ 13587bb185edSStephen Smalley *sid = SECINITSID_UNLABELED; 13597bb185edSStephen Smalley rc = 0; 13607bb185edSStephen Smalley } 13618e6c9693SLucian Adrian Grijincu } 13621da177e4SLinus Torvalds free_page((unsigned long)buffer); 13631da177e4SLinus Torvalds return rc; 13641da177e4SLinus Torvalds } 13651da177e4SLinus Torvalds 13661da177e4SLinus Torvalds /* The inode's security attributes must be initialized before first use. */ 13671da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry) 13681da177e4SLinus Torvalds { 13691da177e4SLinus Torvalds struct superblock_security_struct *sbsec = NULL; 13701da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 13719287aed2SAndreas Gruenbacher u32 task_sid, sid = 0; 13729287aed2SAndreas Gruenbacher u16 sclass; 13731da177e4SLinus Torvalds struct dentry *dentry; 13741da177e4SLinus Torvalds #define INITCONTEXTLEN 255 13751da177e4SLinus Torvalds char *context = NULL; 13761da177e4SLinus Torvalds unsigned len = 0; 13771da177e4SLinus Torvalds int rc = 0; 13781da177e4SLinus Torvalds 13796f3be9f5SAndreas Gruenbacher if (isec->initialized == LABEL_INITIALIZED) 138013457d07SAndreas Gruenbacher return 0; 13811da177e4SLinus Torvalds 13829287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 13836f3be9f5SAndreas Gruenbacher if (isec->initialized == LABEL_INITIALIZED) 138423970741SEric Paris goto out_unlock; 13851da177e4SLinus Torvalds 138613457d07SAndreas Gruenbacher if (isec->sclass == SECCLASS_FILE) 138713457d07SAndreas Gruenbacher isec->sclass = inode_mode_to_security_class(inode->i_mode); 138813457d07SAndreas Gruenbacher 13891da177e4SLinus Torvalds sbsec = inode->i_sb->s_security; 13900d90a7ecSDavid P. Quigley if (!(sbsec->flags & SE_SBINITIALIZED)) { 13911da177e4SLinus Torvalds /* Defer initialization until selinux_complete_init, 13921da177e4SLinus Torvalds after the initial policy is loaded and the security 13931da177e4SLinus Torvalds server is ready to handle calls. */ 13941da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 13951da177e4SLinus Torvalds if (list_empty(&isec->list)) 13961da177e4SLinus Torvalds list_add(&isec->list, &sbsec->isec_head); 13971da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 139823970741SEric Paris goto out_unlock; 13991da177e4SLinus Torvalds } 14001da177e4SLinus Torvalds 14019287aed2SAndreas Gruenbacher sclass = isec->sclass; 14029287aed2SAndreas Gruenbacher task_sid = isec->task_sid; 14039287aed2SAndreas Gruenbacher sid = isec->sid; 14049287aed2SAndreas Gruenbacher isec->initialized = LABEL_PENDING; 14059287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 14069287aed2SAndreas Gruenbacher 14071da177e4SLinus Torvalds switch (sbsec->behavior) { 1408eb9ae686SDavid Quigley case SECURITY_FS_USE_NATIVE: 1409eb9ae686SDavid Quigley break; 14101da177e4SLinus Torvalds case SECURITY_FS_USE_XATTR: 14115d6c3191SAndreas Gruenbacher if (!(inode->i_opflags & IOP_XATTR)) { 14129287aed2SAndreas Gruenbacher sid = sbsec->def_sid; 14131da177e4SLinus Torvalds break; 14141da177e4SLinus Torvalds } 14151da177e4SLinus Torvalds /* Need a dentry, since the xattr API requires one. 14161da177e4SLinus Torvalds Life would be simpler if we could just pass the inode. */ 14171da177e4SLinus Torvalds if (opt_dentry) { 14181da177e4SLinus Torvalds /* Called from d_instantiate or d_splice_alias. */ 14191da177e4SLinus Torvalds dentry = dget(opt_dentry); 14201da177e4SLinus Torvalds } else { 1421b127125dSAl Viro /* 1422b127125dSAl Viro * Called from selinux_complete_init, try to find a dentry. 1423b127125dSAl Viro * Some filesystems really want a connected one, so try 1424b127125dSAl Viro * that first. We could split SECURITY_FS_USE_XATTR in 1425b127125dSAl Viro * two, depending upon that... 1426b127125dSAl Viro */ 14271da177e4SLinus Torvalds dentry = d_find_alias(inode); 1428b127125dSAl Viro if (!dentry) 1429b127125dSAl Viro dentry = d_find_any_alias(inode); 14301da177e4SLinus Torvalds } 14311da177e4SLinus Torvalds if (!dentry) { 1432df7f54c0SEric Paris /* 1433df7f54c0SEric Paris * this is can be hit on boot when a file is accessed 1434df7f54c0SEric Paris * before the policy is loaded. When we load policy we 1435df7f54c0SEric Paris * may find inodes that have no dentry on the 1436df7f54c0SEric Paris * sbsec->isec_head list. No reason to complain as these 1437df7f54c0SEric Paris * will get fixed up the next time we go through 1438df7f54c0SEric Paris * inode_doinit with a dentry, before these inodes could 1439df7f54c0SEric Paris * be used again by userspace. 1440df7f54c0SEric Paris */ 14419287aed2SAndreas Gruenbacher goto out; 14421da177e4SLinus Torvalds } 14431da177e4SLinus Torvalds 14441da177e4SLinus Torvalds len = INITCONTEXTLEN; 14454cb912f1SEric Paris context = kmalloc(len+1, GFP_NOFS); 14461da177e4SLinus Torvalds if (!context) { 14471da177e4SLinus Torvalds rc = -ENOMEM; 14481da177e4SLinus Torvalds dput(dentry); 14499287aed2SAndreas Gruenbacher goto out; 14501da177e4SLinus Torvalds } 14514cb912f1SEric Paris context[len] = '\0'; 14525d6c3191SAndreas Gruenbacher rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len); 14531da177e4SLinus Torvalds if (rc == -ERANGE) { 1454314dabb8SJames Morris kfree(context); 1455314dabb8SJames Morris 14561da177e4SLinus Torvalds /* Need a larger buffer. Query for the right size. */ 14575d6c3191SAndreas Gruenbacher rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0); 14581da177e4SLinus Torvalds if (rc < 0) { 14591da177e4SLinus Torvalds dput(dentry); 14609287aed2SAndreas Gruenbacher goto out; 14611da177e4SLinus Torvalds } 14621da177e4SLinus Torvalds len = rc; 14634cb912f1SEric Paris context = kmalloc(len+1, GFP_NOFS); 14641da177e4SLinus Torvalds if (!context) { 14651da177e4SLinus Torvalds rc = -ENOMEM; 14661da177e4SLinus Torvalds dput(dentry); 14679287aed2SAndreas Gruenbacher goto out; 14681da177e4SLinus Torvalds } 14694cb912f1SEric Paris context[len] = '\0'; 14705d6c3191SAndreas Gruenbacher rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len); 14711da177e4SLinus Torvalds } 14721da177e4SLinus Torvalds dput(dentry); 14731da177e4SLinus Torvalds if (rc < 0) { 14741da177e4SLinus Torvalds if (rc != -ENODATA) { 1475c103a91eSpeter enderborg pr_warn("SELinux: %s: getxattr returned " 1476dd6f953aSHarvey Harrison "%d for dev=%s ino=%ld\n", __func__, 14771da177e4SLinus Torvalds -rc, inode->i_sb->s_id, inode->i_ino); 14781da177e4SLinus Torvalds kfree(context); 14799287aed2SAndreas Gruenbacher goto out; 14801da177e4SLinus Torvalds } 14811da177e4SLinus Torvalds /* Map ENODATA to the default file SID */ 14821da177e4SLinus Torvalds sid = sbsec->def_sid; 14831da177e4SLinus Torvalds rc = 0; 14841da177e4SLinus Torvalds } else { 1485aa8e712cSStephen Smalley rc = security_context_to_sid_default(&selinux_state, 1486aa8e712cSStephen Smalley context, rc, &sid, 1487869ab514SStephen Smalley sbsec->def_sid, 1488869ab514SStephen Smalley GFP_NOFS); 14891da177e4SLinus Torvalds if (rc) { 14904ba0a8adSEric Paris char *dev = inode->i_sb->s_id; 14914ba0a8adSEric Paris unsigned long ino = inode->i_ino; 14924ba0a8adSEric Paris 14934ba0a8adSEric Paris if (rc == -EINVAL) { 14944ba0a8adSEric Paris if (printk_ratelimit()) 1495c103a91eSpeter enderborg pr_notice("SELinux: inode=%lu on dev=%s was found to have an invalid " 14964ba0a8adSEric Paris "context=%s. This indicates you may need to relabel the inode or the " 14974ba0a8adSEric Paris "filesystem in question.\n", ino, dev, context); 14984ba0a8adSEric Paris } else { 1499c103a91eSpeter enderborg pr_warn("SELinux: %s: context_to_sid(%s) " 15001da177e4SLinus Torvalds "returned %d for dev=%s ino=%ld\n", 15014ba0a8adSEric Paris __func__, context, -rc, dev, ino); 15024ba0a8adSEric Paris } 15031da177e4SLinus Torvalds kfree(context); 15041da177e4SLinus Torvalds /* Leave with the unlabeled SID */ 15051da177e4SLinus Torvalds rc = 0; 15061da177e4SLinus Torvalds break; 15071da177e4SLinus Torvalds } 15081da177e4SLinus Torvalds } 15091da177e4SLinus Torvalds kfree(context); 15101da177e4SLinus Torvalds break; 15111da177e4SLinus Torvalds case SECURITY_FS_USE_TASK: 15129287aed2SAndreas Gruenbacher sid = task_sid; 15131da177e4SLinus Torvalds break; 15141da177e4SLinus Torvalds case SECURITY_FS_USE_TRANS: 15151da177e4SLinus Torvalds /* Default to the fs SID. */ 15169287aed2SAndreas Gruenbacher sid = sbsec->sid; 15171da177e4SLinus Torvalds 15181da177e4SLinus Torvalds /* Try to obtain a transition SID. */ 1519aa8e712cSStephen Smalley rc = security_transition_sid(&selinux_state, task_sid, sid, 1520aa8e712cSStephen Smalley sclass, NULL, &sid); 15211da177e4SLinus Torvalds if (rc) 15229287aed2SAndreas Gruenbacher goto out; 15231da177e4SLinus Torvalds break; 1524c312feb2SEric Paris case SECURITY_FS_USE_MNTPOINT: 15259287aed2SAndreas Gruenbacher sid = sbsec->mntpoint_sid; 1526c312feb2SEric Paris break; 15271da177e4SLinus Torvalds default: 1528c312feb2SEric Paris /* Default to the fs superblock SID. */ 15299287aed2SAndreas Gruenbacher sid = sbsec->sid; 15301da177e4SLinus Torvalds 1531134509d5SStephen Smalley if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) { 1532f64410ecSPaul Moore /* We must have a dentry to determine the label on 1533f64410ecSPaul Moore * procfs inodes */ 1534b127125dSAl Viro if (opt_dentry) { 1535f64410ecSPaul Moore /* Called from d_instantiate or 1536f64410ecSPaul Moore * d_splice_alias. */ 1537f64410ecSPaul Moore dentry = dget(opt_dentry); 1538b127125dSAl Viro } else { 1539f64410ecSPaul Moore /* Called from selinux_complete_init, try to 1540b127125dSAl Viro * find a dentry. Some filesystems really want 1541b127125dSAl Viro * a connected one, so try that first. 1542b127125dSAl Viro */ 1543f64410ecSPaul Moore dentry = d_find_alias(inode); 1544b127125dSAl Viro if (!dentry) 1545b127125dSAl Viro dentry = d_find_any_alias(inode); 1546b127125dSAl Viro } 1547f64410ecSPaul Moore /* 1548f64410ecSPaul Moore * This can be hit on boot when a file is accessed 1549f64410ecSPaul Moore * before the policy is loaded. When we load policy we 1550f64410ecSPaul Moore * may find inodes that have no dentry on the 1551f64410ecSPaul Moore * sbsec->isec_head list. No reason to complain as 1552f64410ecSPaul Moore * these will get fixed up the next time we go through 1553f64410ecSPaul Moore * inode_doinit() with a dentry, before these inodes 1554f64410ecSPaul Moore * could be used again by userspace. 1555f64410ecSPaul Moore */ 1556f64410ecSPaul Moore if (!dentry) 15579287aed2SAndreas Gruenbacher goto out; 15589287aed2SAndreas Gruenbacher rc = selinux_genfs_get_sid(dentry, sclass, 1559134509d5SStephen Smalley sbsec->flags, &sid); 1560f64410ecSPaul Moore dput(dentry); 15611da177e4SLinus Torvalds if (rc) 15629287aed2SAndreas Gruenbacher goto out; 15631da177e4SLinus Torvalds } 15641da177e4SLinus Torvalds break; 15651da177e4SLinus Torvalds } 15661da177e4SLinus Torvalds 15679287aed2SAndreas Gruenbacher out: 15689287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 15699287aed2SAndreas Gruenbacher if (isec->initialized == LABEL_PENDING) { 15709287aed2SAndreas Gruenbacher if (!sid || rc) { 15719287aed2SAndreas Gruenbacher isec->initialized = LABEL_INVALID; 15729287aed2SAndreas Gruenbacher goto out_unlock; 15739287aed2SAndreas Gruenbacher } 15749287aed2SAndreas Gruenbacher 15756f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 15769287aed2SAndreas Gruenbacher isec->sid = sid; 15779287aed2SAndreas Gruenbacher } 15781da177e4SLinus Torvalds 157923970741SEric Paris out_unlock: 15809287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 15811da177e4SLinus Torvalds return rc; 15821da177e4SLinus Torvalds } 15831da177e4SLinus Torvalds 15841da177e4SLinus Torvalds /* Convert a Linux signal to an access vector. */ 15851da177e4SLinus Torvalds static inline u32 signal_to_av(int sig) 15861da177e4SLinus Torvalds { 15871da177e4SLinus Torvalds u32 perm = 0; 15881da177e4SLinus Torvalds 15891da177e4SLinus Torvalds switch (sig) { 15901da177e4SLinus Torvalds case SIGCHLD: 15911da177e4SLinus Torvalds /* Commonly granted from child to parent. */ 15921da177e4SLinus Torvalds perm = PROCESS__SIGCHLD; 15931da177e4SLinus Torvalds break; 15941da177e4SLinus Torvalds case SIGKILL: 15951da177e4SLinus Torvalds /* Cannot be caught or ignored */ 15961da177e4SLinus Torvalds perm = PROCESS__SIGKILL; 15971da177e4SLinus Torvalds break; 15981da177e4SLinus Torvalds case SIGSTOP: 15991da177e4SLinus Torvalds /* Cannot be caught or ignored */ 16001da177e4SLinus Torvalds perm = PROCESS__SIGSTOP; 16011da177e4SLinus Torvalds break; 16021da177e4SLinus Torvalds default: 16031da177e4SLinus Torvalds /* All other signals. */ 16041da177e4SLinus Torvalds perm = PROCESS__SIGNAL; 16051da177e4SLinus Torvalds break; 16061da177e4SLinus Torvalds } 16071da177e4SLinus Torvalds 16081da177e4SLinus Torvalds return perm; 16091da177e4SLinus Torvalds } 16101da177e4SLinus Torvalds 1611b68e418cSStephen Smalley #if CAP_LAST_CAP > 63 1612b68e418cSStephen Smalley #error Fix SELinux to handle capabilities > 63. 1613b68e418cSStephen Smalley #endif 1614b68e418cSStephen Smalley 16151da177e4SLinus Torvalds /* Check whether a task is allowed to use a capability. */ 16166a9de491SEric Paris static int cred_has_capability(const struct cred *cred, 16178e4ff6f2SStephen Smalley int cap, int audit, bool initns) 16181da177e4SLinus Torvalds { 16192bf49690SThomas Liu struct common_audit_data ad; 162006112163SEric Paris struct av_decision avd; 1621b68e418cSStephen Smalley u16 sclass; 16223699c53cSDavid Howells u32 sid = cred_sid(cred); 1623b68e418cSStephen Smalley u32 av = CAP_TO_MASK(cap); 162406112163SEric Paris int rc; 16251da177e4SLinus Torvalds 162650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_CAP; 16271da177e4SLinus Torvalds ad.u.cap = cap; 16281da177e4SLinus Torvalds 1629b68e418cSStephen Smalley switch (CAP_TO_INDEX(cap)) { 1630b68e418cSStephen Smalley case 0: 16318e4ff6f2SStephen Smalley sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS; 1632b68e418cSStephen Smalley break; 1633b68e418cSStephen Smalley case 1: 16348e4ff6f2SStephen Smalley sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS; 1635b68e418cSStephen Smalley break; 1636b68e418cSStephen Smalley default: 1637c103a91eSpeter enderborg pr_err("SELinux: out of range capability %d\n", cap); 1638b68e418cSStephen Smalley BUG(); 1639a35c6c83SEric Paris return -EINVAL; 1640b68e418cSStephen Smalley } 164106112163SEric Paris 16426b6bc620SStephen Smalley rc = avc_has_perm_noaudit(&selinux_state, 16436b6bc620SStephen Smalley sid, sid, sclass, av, 0, &avd); 16449ade0cf4SEric Paris if (audit == SECURITY_CAP_AUDIT) { 16456b6bc620SStephen Smalley int rc2 = avc_audit(&selinux_state, 16466b6bc620SStephen Smalley sid, sid, sclass, av, &avd, rc, &ad, 0); 16479ade0cf4SEric Paris if (rc2) 16489ade0cf4SEric Paris return rc2; 16499ade0cf4SEric Paris } 165006112163SEric Paris return rc; 16511da177e4SLinus Torvalds } 16521da177e4SLinus Torvalds 16531da177e4SLinus Torvalds /* Check whether a task has a particular permission to an inode. 16541da177e4SLinus Torvalds The 'adp' parameter is optional and allows other audit 16551da177e4SLinus Torvalds data to be passed (e.g. the dentry). */ 165688e67f3bSDavid Howells static int inode_has_perm(const struct cred *cred, 16571da177e4SLinus Torvalds struct inode *inode, 16581da177e4SLinus Torvalds u32 perms, 165919e49834SLinus Torvalds struct common_audit_data *adp) 16601da177e4SLinus Torvalds { 16611da177e4SLinus Torvalds struct inode_security_struct *isec; 1662275bb41eSDavid Howells u32 sid; 16631da177e4SLinus Torvalds 1664e0e81739SDavid Howells validate_creds(cred); 1665e0e81739SDavid Howells 1666bbaca6c2SStephen Smalley if (unlikely(IS_PRIVATE(inode))) 1667bbaca6c2SStephen Smalley return 0; 1668bbaca6c2SStephen Smalley 166988e67f3bSDavid Howells sid = cred_sid(cred); 16701da177e4SLinus Torvalds isec = inode->i_security; 16711da177e4SLinus Torvalds 16726b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 16736b6bc620SStephen Smalley sid, isec->sid, isec->sclass, perms, adp); 16741da177e4SLinus Torvalds } 16751da177e4SLinus Torvalds 16761da177e4SLinus Torvalds /* Same as inode_has_perm, but pass explicit audit data containing 16771da177e4SLinus Torvalds the dentry to help the auditing code to more easily generate the 16781da177e4SLinus Torvalds pathname if needed. */ 167988e67f3bSDavid Howells static inline int dentry_has_perm(const struct cred *cred, 16801da177e4SLinus Torvalds struct dentry *dentry, 16811da177e4SLinus Torvalds u32 av) 16821da177e4SLinus Torvalds { 1683c6f493d6SDavid Howells struct inode *inode = d_backing_inode(dentry); 16842bf49690SThomas Liu struct common_audit_data ad; 168588e67f3bSDavid Howells 168650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 16872875fa00SEric Paris ad.u.dentry = dentry; 16885d226df4SAndreas Gruenbacher __inode_security_revalidate(inode, dentry, true); 168919e49834SLinus Torvalds return inode_has_perm(cred, inode, av, &ad); 16902875fa00SEric Paris } 16912875fa00SEric Paris 16922875fa00SEric Paris /* Same as inode_has_perm, but pass explicit audit data containing 16932875fa00SEric Paris the path to help the auditing code to more easily generate the 16942875fa00SEric Paris pathname if needed. */ 16952875fa00SEric Paris static inline int path_has_perm(const struct cred *cred, 16963f7036a0SAl Viro const struct path *path, 16972875fa00SEric Paris u32 av) 16982875fa00SEric Paris { 1699c6f493d6SDavid Howells struct inode *inode = d_backing_inode(path->dentry); 17002875fa00SEric Paris struct common_audit_data ad; 17012875fa00SEric Paris 170250c205f5SEric Paris ad.type = LSM_AUDIT_DATA_PATH; 17032875fa00SEric Paris ad.u.path = *path; 17045d226df4SAndreas Gruenbacher __inode_security_revalidate(inode, path->dentry, true); 170519e49834SLinus Torvalds return inode_has_perm(cred, inode, av, &ad); 17061da177e4SLinus Torvalds } 17071da177e4SLinus Torvalds 170813f8e981SDavid Howells /* Same as path_has_perm, but uses the inode from the file struct. */ 170913f8e981SDavid Howells static inline int file_path_has_perm(const struct cred *cred, 171013f8e981SDavid Howells struct file *file, 171113f8e981SDavid Howells u32 av) 171213f8e981SDavid Howells { 171313f8e981SDavid Howells struct common_audit_data ad; 171413f8e981SDavid Howells 171543af5de7SVivek Goyal ad.type = LSM_AUDIT_DATA_FILE; 171643af5de7SVivek Goyal ad.u.file = file; 171719e49834SLinus Torvalds return inode_has_perm(cred, file_inode(file), av, &ad); 171813f8e981SDavid Howells } 171913f8e981SDavid Howells 1720f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL 1721f66e448cSChenbo Feng static int bpf_fd_pass(struct file *file, u32 sid); 1722f66e448cSChenbo Feng #endif 1723f66e448cSChenbo Feng 17241da177e4SLinus Torvalds /* Check whether a task can use an open file descriptor to 17251da177e4SLinus Torvalds access an inode in a given way. Check access to the 17261da177e4SLinus Torvalds descriptor itself, and then use dentry_has_perm to 17271da177e4SLinus Torvalds check a particular permission to the file. 17281da177e4SLinus Torvalds Access to the descriptor is implicitly granted if it 17291da177e4SLinus Torvalds has the same SID as the process. If av is zero, then 17301da177e4SLinus Torvalds access to the file is not checked, e.g. for cases 17311da177e4SLinus Torvalds where only the descriptor is affected like seek. */ 173288e67f3bSDavid Howells static int file_has_perm(const struct cred *cred, 17331da177e4SLinus Torvalds struct file *file, 17341da177e4SLinus Torvalds u32 av) 17351da177e4SLinus Torvalds { 1736*bb6c6b02SCasey Schaufler struct file_security_struct *fsec = selinux_file(file); 1737496ad9aaSAl Viro struct inode *inode = file_inode(file); 17382bf49690SThomas Liu struct common_audit_data ad; 173988e67f3bSDavid Howells u32 sid = cred_sid(cred); 17401da177e4SLinus Torvalds int rc; 17411da177e4SLinus Torvalds 174243af5de7SVivek Goyal ad.type = LSM_AUDIT_DATA_FILE; 174343af5de7SVivek Goyal ad.u.file = file; 17441da177e4SLinus Torvalds 1745275bb41eSDavid Howells if (sid != fsec->sid) { 17466b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 17476b6bc620SStephen Smalley sid, fsec->sid, 17481da177e4SLinus Torvalds SECCLASS_FD, 17491da177e4SLinus Torvalds FD__USE, 17501da177e4SLinus Torvalds &ad); 17511da177e4SLinus Torvalds if (rc) 175288e67f3bSDavid Howells goto out; 17531da177e4SLinus Torvalds } 17541da177e4SLinus Torvalds 1755f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL 1756f66e448cSChenbo Feng rc = bpf_fd_pass(file, cred_sid(cred)); 1757f66e448cSChenbo Feng if (rc) 1758f66e448cSChenbo Feng return rc; 1759f66e448cSChenbo Feng #endif 1760f66e448cSChenbo Feng 17611da177e4SLinus Torvalds /* av is zero if only checking access to the descriptor. */ 176288e67f3bSDavid Howells rc = 0; 17631da177e4SLinus Torvalds if (av) 176419e49834SLinus Torvalds rc = inode_has_perm(cred, inode, av, &ad); 17651da177e4SLinus Torvalds 176688e67f3bSDavid Howells out: 176788e67f3bSDavid Howells return rc; 17681da177e4SLinus Torvalds } 17691da177e4SLinus Torvalds 1770c3c188b2SDavid Howells /* 1771c3c188b2SDavid Howells * Determine the label for an inode that might be unioned. 1772c3c188b2SDavid Howells */ 1773c957f6dfSVivek Goyal static int 1774c957f6dfSVivek Goyal selinux_determine_inode_label(const struct task_security_struct *tsec, 1775c957f6dfSVivek Goyal struct inode *dir, 1776c957f6dfSVivek Goyal const struct qstr *name, u16 tclass, 1777c3c188b2SDavid Howells u32 *_new_isid) 1778c3c188b2SDavid Howells { 1779c3c188b2SDavid Howells const struct superblock_security_struct *sbsec = dir->i_sb->s_security; 1780c3c188b2SDavid Howells 1781c3c188b2SDavid Howells if ((sbsec->flags & SE_SBINITIALIZED) && 1782c3c188b2SDavid Howells (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) { 1783c3c188b2SDavid Howells *_new_isid = sbsec->mntpoint_sid; 1784c3c188b2SDavid Howells } else if ((sbsec->flags & SBLABEL_MNT) && 1785c3c188b2SDavid Howells tsec->create_sid) { 1786c3c188b2SDavid Howells *_new_isid = tsec->create_sid; 1787c3c188b2SDavid Howells } else { 178820cdef8dSPaul Moore const struct inode_security_struct *dsec = inode_security(dir); 1789aa8e712cSStephen Smalley return security_transition_sid(&selinux_state, tsec->sid, 1790aa8e712cSStephen Smalley dsec->sid, tclass, 1791c3c188b2SDavid Howells name, _new_isid); 1792c3c188b2SDavid Howells } 1793c3c188b2SDavid Howells 1794c3c188b2SDavid Howells return 0; 1795c3c188b2SDavid Howells } 1796c3c188b2SDavid Howells 17971da177e4SLinus Torvalds /* Check whether a task can create a file. */ 17981da177e4SLinus Torvalds static int may_create(struct inode *dir, 17991da177e4SLinus Torvalds struct dentry *dentry, 18001da177e4SLinus Torvalds u16 tclass) 18011da177e4SLinus Torvalds { 18020c6cfa62SCasey Schaufler const struct task_security_struct *tsec = selinux_cred(current_cred()); 18031da177e4SLinus Torvalds struct inode_security_struct *dsec; 18041da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 1805275bb41eSDavid Howells u32 sid, newsid; 18062bf49690SThomas Liu struct common_audit_data ad; 18071da177e4SLinus Torvalds int rc; 18081da177e4SLinus Torvalds 180983da53c5SAndreas Gruenbacher dsec = inode_security(dir); 18101da177e4SLinus Torvalds sbsec = dir->i_sb->s_security; 18111da177e4SLinus Torvalds 1812275bb41eSDavid Howells sid = tsec->sid; 1813275bb41eSDavid Howells 181450c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 1815a269434dSEric Paris ad.u.dentry = dentry; 18161da177e4SLinus Torvalds 18176b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 18186b6bc620SStephen Smalley sid, dsec->sid, SECCLASS_DIR, 18191da177e4SLinus Torvalds DIR__ADD_NAME | DIR__SEARCH, 18201da177e4SLinus Torvalds &ad); 18211da177e4SLinus Torvalds if (rc) 18221da177e4SLinus Torvalds return rc; 18231da177e4SLinus Torvalds 18240c6cfa62SCasey Schaufler rc = selinux_determine_inode_label(selinux_cred(current_cred()), dir, 1825c957f6dfSVivek Goyal &dentry->d_name, tclass, &newsid); 18261da177e4SLinus Torvalds if (rc) 18271da177e4SLinus Torvalds return rc; 18281da177e4SLinus Torvalds 18296b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 18306b6bc620SStephen Smalley sid, newsid, tclass, FILE__CREATE, &ad); 18311da177e4SLinus Torvalds if (rc) 18321da177e4SLinus Torvalds return rc; 18331da177e4SLinus Torvalds 18346b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 18356b6bc620SStephen Smalley newsid, sbsec->sid, 18361da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 18371da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, &ad); 18381da177e4SLinus Torvalds } 18391da177e4SLinus Torvalds 18401da177e4SLinus Torvalds #define MAY_LINK 0 18411da177e4SLinus Torvalds #define MAY_UNLINK 1 18421da177e4SLinus Torvalds #define MAY_RMDIR 2 18431da177e4SLinus Torvalds 18441da177e4SLinus Torvalds /* Check whether a task can link, unlink, or rmdir a file/directory. */ 18451da177e4SLinus Torvalds static int may_link(struct inode *dir, 18461da177e4SLinus Torvalds struct dentry *dentry, 18471da177e4SLinus Torvalds int kind) 18481da177e4SLinus Torvalds 18491da177e4SLinus Torvalds { 18501da177e4SLinus Torvalds struct inode_security_struct *dsec, *isec; 18512bf49690SThomas Liu struct common_audit_data ad; 1852275bb41eSDavid Howells u32 sid = current_sid(); 18531da177e4SLinus Torvalds u32 av; 18541da177e4SLinus Torvalds int rc; 18551da177e4SLinus Torvalds 185683da53c5SAndreas Gruenbacher dsec = inode_security(dir); 185783da53c5SAndreas Gruenbacher isec = backing_inode_security(dentry); 18581da177e4SLinus Torvalds 185950c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 1860a269434dSEric Paris ad.u.dentry = dentry; 18611da177e4SLinus Torvalds 18621da177e4SLinus Torvalds av = DIR__SEARCH; 18631da177e4SLinus Torvalds av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME); 18646b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 18656b6bc620SStephen Smalley sid, dsec->sid, SECCLASS_DIR, av, &ad); 18661da177e4SLinus Torvalds if (rc) 18671da177e4SLinus Torvalds return rc; 18681da177e4SLinus Torvalds 18691da177e4SLinus Torvalds switch (kind) { 18701da177e4SLinus Torvalds case MAY_LINK: 18711da177e4SLinus Torvalds av = FILE__LINK; 18721da177e4SLinus Torvalds break; 18731da177e4SLinus Torvalds case MAY_UNLINK: 18741da177e4SLinus Torvalds av = FILE__UNLINK; 18751da177e4SLinus Torvalds break; 18761da177e4SLinus Torvalds case MAY_RMDIR: 18771da177e4SLinus Torvalds av = DIR__RMDIR; 18781da177e4SLinus Torvalds break; 18791da177e4SLinus Torvalds default: 1880c103a91eSpeter enderborg pr_warn("SELinux: %s: unrecognized kind %d\n", 1881744ba35eSEric Paris __func__, kind); 18821da177e4SLinus Torvalds return 0; 18831da177e4SLinus Torvalds } 18841da177e4SLinus Torvalds 18856b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 18866b6bc620SStephen Smalley sid, isec->sid, isec->sclass, av, &ad); 18871da177e4SLinus Torvalds return rc; 18881da177e4SLinus Torvalds } 18891da177e4SLinus Torvalds 18901da177e4SLinus Torvalds static inline int may_rename(struct inode *old_dir, 18911da177e4SLinus Torvalds struct dentry *old_dentry, 18921da177e4SLinus Torvalds struct inode *new_dir, 18931da177e4SLinus Torvalds struct dentry *new_dentry) 18941da177e4SLinus Torvalds { 18951da177e4SLinus Torvalds struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec; 18962bf49690SThomas Liu struct common_audit_data ad; 1897275bb41eSDavid Howells u32 sid = current_sid(); 18981da177e4SLinus Torvalds u32 av; 18991da177e4SLinus Torvalds int old_is_dir, new_is_dir; 19001da177e4SLinus Torvalds int rc; 19011da177e4SLinus Torvalds 190283da53c5SAndreas Gruenbacher old_dsec = inode_security(old_dir); 190383da53c5SAndreas Gruenbacher old_isec = backing_inode_security(old_dentry); 1904e36cb0b8SDavid Howells old_is_dir = d_is_dir(old_dentry); 190583da53c5SAndreas Gruenbacher new_dsec = inode_security(new_dir); 19061da177e4SLinus Torvalds 190750c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 19081da177e4SLinus Torvalds 1909a269434dSEric Paris ad.u.dentry = old_dentry; 19106b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 19116b6bc620SStephen Smalley sid, old_dsec->sid, SECCLASS_DIR, 19121da177e4SLinus Torvalds DIR__REMOVE_NAME | DIR__SEARCH, &ad); 19131da177e4SLinus Torvalds if (rc) 19141da177e4SLinus Torvalds return rc; 19156b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 19166b6bc620SStephen Smalley sid, old_isec->sid, 19171da177e4SLinus Torvalds old_isec->sclass, FILE__RENAME, &ad); 19181da177e4SLinus Torvalds if (rc) 19191da177e4SLinus Torvalds return rc; 19201da177e4SLinus Torvalds if (old_is_dir && new_dir != old_dir) { 19216b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 19226b6bc620SStephen Smalley sid, old_isec->sid, 19231da177e4SLinus Torvalds old_isec->sclass, DIR__REPARENT, &ad); 19241da177e4SLinus Torvalds if (rc) 19251da177e4SLinus Torvalds return rc; 19261da177e4SLinus Torvalds } 19271da177e4SLinus Torvalds 1928a269434dSEric Paris ad.u.dentry = new_dentry; 19291da177e4SLinus Torvalds av = DIR__ADD_NAME | DIR__SEARCH; 19302c616d4dSDavid Howells if (d_is_positive(new_dentry)) 19311da177e4SLinus Torvalds av |= DIR__REMOVE_NAME; 19326b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 19336b6bc620SStephen Smalley sid, new_dsec->sid, SECCLASS_DIR, av, &ad); 19341da177e4SLinus Torvalds if (rc) 19351da177e4SLinus Torvalds return rc; 19362c616d4dSDavid Howells if (d_is_positive(new_dentry)) { 193783da53c5SAndreas Gruenbacher new_isec = backing_inode_security(new_dentry); 1938e36cb0b8SDavid Howells new_is_dir = d_is_dir(new_dentry); 19396b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 19406b6bc620SStephen Smalley sid, new_isec->sid, 19411da177e4SLinus Torvalds new_isec->sclass, 19421da177e4SLinus Torvalds (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad); 19431da177e4SLinus Torvalds if (rc) 19441da177e4SLinus Torvalds return rc; 19451da177e4SLinus Torvalds } 19461da177e4SLinus Torvalds 19471da177e4SLinus Torvalds return 0; 19481da177e4SLinus Torvalds } 19491da177e4SLinus Torvalds 19501da177e4SLinus Torvalds /* Check whether a task can perform a filesystem operation. */ 195188e67f3bSDavid Howells static int superblock_has_perm(const struct cred *cred, 19521da177e4SLinus Torvalds struct super_block *sb, 19531da177e4SLinus Torvalds u32 perms, 19542bf49690SThomas Liu struct common_audit_data *ad) 19551da177e4SLinus Torvalds { 19561da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 195788e67f3bSDavid Howells u32 sid = cred_sid(cred); 19581da177e4SLinus Torvalds 19591da177e4SLinus Torvalds sbsec = sb->s_security; 19606b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 19616b6bc620SStephen Smalley sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad); 19621da177e4SLinus Torvalds } 19631da177e4SLinus Torvalds 19641da177e4SLinus Torvalds /* Convert a Linux mode and permission mask to an access vector. */ 19651da177e4SLinus Torvalds static inline u32 file_mask_to_av(int mode, int mask) 19661da177e4SLinus Torvalds { 19671da177e4SLinus Torvalds u32 av = 0; 19681da177e4SLinus Torvalds 1969dba19c60SAl Viro if (!S_ISDIR(mode)) { 19701da177e4SLinus Torvalds if (mask & MAY_EXEC) 19711da177e4SLinus Torvalds av |= FILE__EXECUTE; 19721da177e4SLinus Torvalds if (mask & MAY_READ) 19731da177e4SLinus Torvalds av |= FILE__READ; 19741da177e4SLinus Torvalds 19751da177e4SLinus Torvalds if (mask & MAY_APPEND) 19761da177e4SLinus Torvalds av |= FILE__APPEND; 19771da177e4SLinus Torvalds else if (mask & MAY_WRITE) 19781da177e4SLinus Torvalds av |= FILE__WRITE; 19791da177e4SLinus Torvalds 19801da177e4SLinus Torvalds } else { 19811da177e4SLinus Torvalds if (mask & MAY_EXEC) 19821da177e4SLinus Torvalds av |= DIR__SEARCH; 19831da177e4SLinus Torvalds if (mask & MAY_WRITE) 19841da177e4SLinus Torvalds av |= DIR__WRITE; 19851da177e4SLinus Torvalds if (mask & MAY_READ) 19861da177e4SLinus Torvalds av |= DIR__READ; 19871da177e4SLinus Torvalds } 19881da177e4SLinus Torvalds 19891da177e4SLinus Torvalds return av; 19901da177e4SLinus Torvalds } 19911da177e4SLinus Torvalds 19921da177e4SLinus Torvalds /* Convert a Linux file to an access vector. */ 19931da177e4SLinus Torvalds static inline u32 file_to_av(struct file *file) 19941da177e4SLinus Torvalds { 19951da177e4SLinus Torvalds u32 av = 0; 19961da177e4SLinus Torvalds 19971da177e4SLinus Torvalds if (file->f_mode & FMODE_READ) 19981da177e4SLinus Torvalds av |= FILE__READ; 19991da177e4SLinus Torvalds if (file->f_mode & FMODE_WRITE) { 20001da177e4SLinus Torvalds if (file->f_flags & O_APPEND) 20011da177e4SLinus Torvalds av |= FILE__APPEND; 20021da177e4SLinus Torvalds else 20031da177e4SLinus Torvalds av |= FILE__WRITE; 20041da177e4SLinus Torvalds } 20050794c66dSStephen Smalley if (!av) { 20060794c66dSStephen Smalley /* 20070794c66dSStephen Smalley * Special file opened with flags 3 for ioctl-only use. 20080794c66dSStephen Smalley */ 20090794c66dSStephen Smalley av = FILE__IOCTL; 20100794c66dSStephen Smalley } 20111da177e4SLinus Torvalds 20121da177e4SLinus Torvalds return av; 20131da177e4SLinus Torvalds } 20141da177e4SLinus Torvalds 20158b6a5a37SEric Paris /* 20168b6a5a37SEric Paris * Convert a file to an access vector and include the correct open 20178b6a5a37SEric Paris * open permission. 20188b6a5a37SEric Paris */ 20198b6a5a37SEric Paris static inline u32 open_file_to_av(struct file *file) 20208b6a5a37SEric Paris { 20218b6a5a37SEric Paris u32 av = file_to_av(file); 2022ccb54478SStephen Smalley struct inode *inode = file_inode(file); 20238b6a5a37SEric Paris 2024aa8e712cSStephen Smalley if (selinux_policycap_openperm() && 2025aa8e712cSStephen Smalley inode->i_sb->s_magic != SOCKFS_MAGIC) 20268b6a5a37SEric Paris av |= FILE__OPEN; 202749b7b8deSEric Paris 20288b6a5a37SEric Paris return av; 20298b6a5a37SEric Paris } 20308b6a5a37SEric Paris 20311da177e4SLinus Torvalds /* Hook functions begin here. */ 20321da177e4SLinus Torvalds 203379af7307SStephen Smalley static int selinux_binder_set_context_mgr(struct task_struct *mgr) 203479af7307SStephen Smalley { 203579af7307SStephen Smalley u32 mysid = current_sid(); 203679af7307SStephen Smalley u32 mgrsid = task_sid(mgr); 203779af7307SStephen Smalley 20386b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 20396b6bc620SStephen Smalley mysid, mgrsid, SECCLASS_BINDER, 204079af7307SStephen Smalley BINDER__SET_CONTEXT_MGR, NULL); 204179af7307SStephen Smalley } 204279af7307SStephen Smalley 204379af7307SStephen Smalley static int selinux_binder_transaction(struct task_struct *from, 204479af7307SStephen Smalley struct task_struct *to) 204579af7307SStephen Smalley { 204679af7307SStephen Smalley u32 mysid = current_sid(); 204779af7307SStephen Smalley u32 fromsid = task_sid(from); 204879af7307SStephen Smalley u32 tosid = task_sid(to); 204979af7307SStephen Smalley int rc; 205079af7307SStephen Smalley 205179af7307SStephen Smalley if (mysid != fromsid) { 20526b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 20536b6bc620SStephen Smalley mysid, fromsid, SECCLASS_BINDER, 205479af7307SStephen Smalley BINDER__IMPERSONATE, NULL); 205579af7307SStephen Smalley if (rc) 205679af7307SStephen Smalley return rc; 205779af7307SStephen Smalley } 205879af7307SStephen Smalley 20596b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 20606b6bc620SStephen Smalley fromsid, tosid, SECCLASS_BINDER, BINDER__CALL, 206179af7307SStephen Smalley NULL); 206279af7307SStephen Smalley } 206379af7307SStephen Smalley 206479af7307SStephen Smalley static int selinux_binder_transfer_binder(struct task_struct *from, 206579af7307SStephen Smalley struct task_struct *to) 206679af7307SStephen Smalley { 206779af7307SStephen Smalley u32 fromsid = task_sid(from); 206879af7307SStephen Smalley u32 tosid = task_sid(to); 206979af7307SStephen Smalley 20706b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 20716b6bc620SStephen Smalley fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER, 207279af7307SStephen Smalley NULL); 207379af7307SStephen Smalley } 207479af7307SStephen Smalley 207579af7307SStephen Smalley static int selinux_binder_transfer_file(struct task_struct *from, 207679af7307SStephen Smalley struct task_struct *to, 207779af7307SStephen Smalley struct file *file) 207879af7307SStephen Smalley { 207979af7307SStephen Smalley u32 sid = task_sid(to); 2080*bb6c6b02SCasey Schaufler struct file_security_struct *fsec = selinux_file(file); 208183da53c5SAndreas Gruenbacher struct dentry *dentry = file->f_path.dentry; 208220cdef8dSPaul Moore struct inode_security_struct *isec; 208379af7307SStephen Smalley struct common_audit_data ad; 208479af7307SStephen Smalley int rc; 208579af7307SStephen Smalley 208679af7307SStephen Smalley ad.type = LSM_AUDIT_DATA_PATH; 208779af7307SStephen Smalley ad.u.path = file->f_path; 208879af7307SStephen Smalley 208979af7307SStephen Smalley if (sid != fsec->sid) { 20906b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 20916b6bc620SStephen Smalley sid, fsec->sid, 209279af7307SStephen Smalley SECCLASS_FD, 209379af7307SStephen Smalley FD__USE, 209479af7307SStephen Smalley &ad); 209579af7307SStephen Smalley if (rc) 209679af7307SStephen Smalley return rc; 209779af7307SStephen Smalley } 209879af7307SStephen Smalley 2099f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL 2100f66e448cSChenbo Feng rc = bpf_fd_pass(file, sid); 2101f66e448cSChenbo Feng if (rc) 2102f66e448cSChenbo Feng return rc; 2103f66e448cSChenbo Feng #endif 2104f66e448cSChenbo Feng 210583da53c5SAndreas Gruenbacher if (unlikely(IS_PRIVATE(d_backing_inode(dentry)))) 210679af7307SStephen Smalley return 0; 210779af7307SStephen Smalley 210820cdef8dSPaul Moore isec = backing_inode_security(dentry); 21096b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 21106b6bc620SStephen Smalley sid, isec->sid, isec->sclass, file_to_av(file), 211179af7307SStephen Smalley &ad); 211279af7307SStephen Smalley } 211379af7307SStephen Smalley 21149e48858fSIngo Molnar static int selinux_ptrace_access_check(struct task_struct *child, 2115006ebb40SStephen Smalley unsigned int mode) 21161da177e4SLinus Torvalds { 2117275bb41eSDavid Howells u32 sid = current_sid(); 2118275bb41eSDavid Howells u32 csid = task_sid(child); 2119006ebb40SStephen Smalley 2120be0554c9SStephen Smalley if (mode & PTRACE_MODE_READ) 21216b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 21226b6bc620SStephen Smalley sid, csid, SECCLASS_FILE, FILE__READ, NULL); 2123be0554c9SStephen Smalley 21246b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 21256b6bc620SStephen Smalley sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL); 21265cd9c58fSDavid Howells } 21275cd9c58fSDavid Howells 21285cd9c58fSDavid Howells static int selinux_ptrace_traceme(struct task_struct *parent) 21295cd9c58fSDavid Howells { 21306b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 21316b6bc620SStephen Smalley task_sid(parent), current_sid(), SECCLASS_PROCESS, 2132be0554c9SStephen Smalley PROCESS__PTRACE, NULL); 21331da177e4SLinus Torvalds } 21341da177e4SLinus Torvalds 21351da177e4SLinus Torvalds static int selinux_capget(struct task_struct *target, kernel_cap_t *effective, 21361da177e4SLinus Torvalds kernel_cap_t *inheritable, kernel_cap_t *permitted) 21371da177e4SLinus Torvalds { 21386b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 21396b6bc620SStephen Smalley current_sid(), task_sid(target), SECCLASS_PROCESS, 2140be0554c9SStephen Smalley PROCESS__GETCAP, NULL); 21411da177e4SLinus Torvalds } 21421da177e4SLinus Torvalds 2143d84f4f99SDavid Howells static int selinux_capset(struct cred *new, const struct cred *old, 2144d84f4f99SDavid Howells const kernel_cap_t *effective, 214515a2460eSDavid Howells const kernel_cap_t *inheritable, 214615a2460eSDavid Howells const kernel_cap_t *permitted) 21471da177e4SLinus Torvalds { 21486b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 21496b6bc620SStephen Smalley cred_sid(old), cred_sid(new), SECCLASS_PROCESS, 2150be0554c9SStephen Smalley PROCESS__SETCAP, NULL); 21511da177e4SLinus Torvalds } 21521da177e4SLinus Torvalds 21535626d3e8SJames Morris /* 21545626d3e8SJames Morris * (This comment used to live with the selinux_task_setuid hook, 21555626d3e8SJames Morris * which was removed). 21565626d3e8SJames Morris * 21575626d3e8SJames Morris * Since setuid only affects the current process, and since the SELinux 21585626d3e8SJames Morris * controls are not based on the Linux identity attributes, SELinux does not 21595626d3e8SJames Morris * need to control this operation. However, SELinux does control the use of 21605626d3e8SJames Morris * the CAP_SETUID and CAP_SETGID capabilities using the capable hook. 21615626d3e8SJames Morris */ 21625626d3e8SJames Morris 21636a9de491SEric Paris static int selinux_capable(const struct cred *cred, struct user_namespace *ns, 21646a9de491SEric Paris int cap, int audit) 21651da177e4SLinus Torvalds { 21668e4ff6f2SStephen Smalley return cred_has_capability(cred, cap, audit, ns == &init_user_ns); 21671da177e4SLinus Torvalds } 21681da177e4SLinus Torvalds 21691da177e4SLinus Torvalds static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb) 21701da177e4SLinus Torvalds { 217188e67f3bSDavid Howells const struct cred *cred = current_cred(); 21721da177e4SLinus Torvalds int rc = 0; 21731da177e4SLinus Torvalds 21741da177e4SLinus Torvalds if (!sb) 21751da177e4SLinus Torvalds return 0; 21761da177e4SLinus Torvalds 21771da177e4SLinus Torvalds switch (cmds) { 21781da177e4SLinus Torvalds case Q_SYNC: 21791da177e4SLinus Torvalds case Q_QUOTAON: 21801da177e4SLinus Torvalds case Q_QUOTAOFF: 21811da177e4SLinus Torvalds case Q_SETINFO: 21821da177e4SLinus Torvalds case Q_SETQUOTA: 218388e67f3bSDavid Howells rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL); 21841da177e4SLinus Torvalds break; 21851da177e4SLinus Torvalds case Q_GETFMT: 21861da177e4SLinus Torvalds case Q_GETINFO: 21871da177e4SLinus Torvalds case Q_GETQUOTA: 218888e67f3bSDavid Howells rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL); 21891da177e4SLinus Torvalds break; 21901da177e4SLinus Torvalds default: 21911da177e4SLinus Torvalds rc = 0; /* let the kernel handle invalid cmds */ 21921da177e4SLinus Torvalds break; 21931da177e4SLinus Torvalds } 21941da177e4SLinus Torvalds return rc; 21951da177e4SLinus Torvalds } 21961da177e4SLinus Torvalds 21971da177e4SLinus Torvalds static int selinux_quota_on(struct dentry *dentry) 21981da177e4SLinus Torvalds { 219988e67f3bSDavid Howells const struct cred *cred = current_cred(); 220088e67f3bSDavid Howells 22012875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__QUOTAON); 22021da177e4SLinus Torvalds } 22031da177e4SLinus Torvalds 220412b3052cSEric Paris static int selinux_syslog(int type) 22051da177e4SLinus Torvalds { 22061da177e4SLinus Torvalds switch (type) { 2207d78ca3cdSKees Cook case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */ 2208d78ca3cdSKees Cook case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */ 22096b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 22106b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, 2211be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL); 2212d78ca3cdSKees Cook case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */ 2213d78ca3cdSKees Cook case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */ 2214d78ca3cdSKees Cook /* Set level of messages printed to console */ 2215d78ca3cdSKees Cook case SYSLOG_ACTION_CONSOLE_LEVEL: 22166b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 22176b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, 2218be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE, 2219be0554c9SStephen Smalley NULL); 22201da177e4SLinus Torvalds } 2221be0554c9SStephen Smalley /* All other syslog types */ 22226b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 22236b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, 2224be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL); 22251da177e4SLinus Torvalds } 22261da177e4SLinus Torvalds 22271da177e4SLinus Torvalds /* 22281da177e4SLinus Torvalds * Check that a process has enough memory to allocate a new virtual 22291da177e4SLinus Torvalds * mapping. 0 means there is enough memory for the allocation to 22301da177e4SLinus Torvalds * succeed and -ENOMEM implies there is not. 22311da177e4SLinus Torvalds * 22321da177e4SLinus Torvalds * Do not audit the selinux permission check, as this is applied to all 22331da177e4SLinus Torvalds * processes that allocate mappings. 22341da177e4SLinus Torvalds */ 223534b4e4aaSAlan Cox static int selinux_vm_enough_memory(struct mm_struct *mm, long pages) 22361da177e4SLinus Torvalds { 22371da177e4SLinus Torvalds int rc, cap_sys_admin = 0; 22381da177e4SLinus Torvalds 2239b1d9e6b0SCasey Schaufler rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN, 22408e4ff6f2SStephen Smalley SECURITY_CAP_NOAUDIT, true); 22411da177e4SLinus Torvalds if (rc == 0) 22421da177e4SLinus Torvalds cap_sys_admin = 1; 22431da177e4SLinus Torvalds 2244b1d9e6b0SCasey Schaufler return cap_sys_admin; 22451da177e4SLinus Torvalds } 22461da177e4SLinus Torvalds 22471da177e4SLinus Torvalds /* binprm security operations */ 22481da177e4SLinus Torvalds 2249be0554c9SStephen Smalley static u32 ptrace_parent_sid(void) 22500c6181cbSPaul Moore { 22510c6181cbSPaul Moore u32 sid = 0; 22520c6181cbSPaul Moore struct task_struct *tracer; 22530c6181cbSPaul Moore 22540c6181cbSPaul Moore rcu_read_lock(); 2255be0554c9SStephen Smalley tracer = ptrace_parent(current); 22560c6181cbSPaul Moore if (tracer) 22570c6181cbSPaul Moore sid = task_sid(tracer); 22580c6181cbSPaul Moore rcu_read_unlock(); 22590c6181cbSPaul Moore 22600c6181cbSPaul Moore return sid; 22610c6181cbSPaul Moore } 22620c6181cbSPaul Moore 22637b0d0b40SStephen Smalley static int check_nnp_nosuid(const struct linux_binprm *bprm, 22647b0d0b40SStephen Smalley const struct task_security_struct *old_tsec, 22657b0d0b40SStephen Smalley const struct task_security_struct *new_tsec) 22667b0d0b40SStephen Smalley { 22677b0d0b40SStephen Smalley int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS); 2268380cf5baSAndy Lutomirski int nosuid = !mnt_may_suid(bprm->file->f_path.mnt); 22697b0d0b40SStephen Smalley int rc; 2270af63f419SStephen Smalley u32 av; 22717b0d0b40SStephen Smalley 22727b0d0b40SStephen Smalley if (!nnp && !nosuid) 22737b0d0b40SStephen Smalley return 0; /* neither NNP nor nosuid */ 22747b0d0b40SStephen Smalley 22757b0d0b40SStephen Smalley if (new_tsec->sid == old_tsec->sid) 22767b0d0b40SStephen Smalley return 0; /* No change in credentials */ 22777b0d0b40SStephen Smalley 22787b0d0b40SStephen Smalley /* 2279af63f419SStephen Smalley * If the policy enables the nnp_nosuid_transition policy capability, 2280af63f419SStephen Smalley * then we permit transitions under NNP or nosuid if the 2281af63f419SStephen Smalley * policy allows the corresponding permission between 2282af63f419SStephen Smalley * the old and new contexts. 2283af63f419SStephen Smalley */ 2284aa8e712cSStephen Smalley if (selinux_policycap_nnp_nosuid_transition()) { 2285af63f419SStephen Smalley av = 0; 2286af63f419SStephen Smalley if (nnp) 2287af63f419SStephen Smalley av |= PROCESS2__NNP_TRANSITION; 2288af63f419SStephen Smalley if (nosuid) 2289af63f419SStephen Smalley av |= PROCESS2__NOSUID_TRANSITION; 22906b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 22916b6bc620SStephen Smalley old_tsec->sid, new_tsec->sid, 2292af63f419SStephen Smalley SECCLASS_PROCESS2, av, NULL); 2293af63f419SStephen Smalley if (!rc) 2294af63f419SStephen Smalley return 0; 2295af63f419SStephen Smalley } 2296af63f419SStephen Smalley 2297af63f419SStephen Smalley /* 2298af63f419SStephen Smalley * We also permit NNP or nosuid transitions to bounded SIDs, 2299af63f419SStephen Smalley * i.e. SIDs that are guaranteed to only be allowed a subset 2300af63f419SStephen Smalley * of the permissions of the current SID. 23017b0d0b40SStephen Smalley */ 2302aa8e712cSStephen Smalley rc = security_bounded_transition(&selinux_state, old_tsec->sid, 2303aa8e712cSStephen Smalley new_tsec->sid); 2304af63f419SStephen Smalley if (!rc) 2305af63f419SStephen Smalley return 0; 2306af63f419SStephen Smalley 23077b0d0b40SStephen Smalley /* 23087b0d0b40SStephen Smalley * On failure, preserve the errno values for NNP vs nosuid. 23097b0d0b40SStephen Smalley * NNP: Operation not permitted for caller. 23107b0d0b40SStephen Smalley * nosuid: Permission denied to file. 23117b0d0b40SStephen Smalley */ 23127b0d0b40SStephen Smalley if (nnp) 23137b0d0b40SStephen Smalley return -EPERM; 23147b0d0b40SStephen Smalley return -EACCES; 23157b0d0b40SStephen Smalley } 23167b0d0b40SStephen Smalley 2317a6f76f23SDavid Howells static int selinux_bprm_set_creds(struct linux_binprm *bprm) 23181da177e4SLinus Torvalds { 2319a6f76f23SDavid Howells const struct task_security_struct *old_tsec; 2320a6f76f23SDavid Howells struct task_security_struct *new_tsec; 23211da177e4SLinus Torvalds struct inode_security_struct *isec; 23222bf49690SThomas Liu struct common_audit_data ad; 2323496ad9aaSAl Viro struct inode *inode = file_inode(bprm->file); 23241da177e4SLinus Torvalds int rc; 23251da177e4SLinus Torvalds 2326a6f76f23SDavid Howells /* SELinux context only depends on initial program or script and not 2327a6f76f23SDavid Howells * the script interpreter */ 2328ddb4a144SKees Cook if (bprm->called_set_creds) 23291da177e4SLinus Torvalds return 0; 23301da177e4SLinus Torvalds 23310c6cfa62SCasey Schaufler old_tsec = selinux_cred(current_cred()); 23320c6cfa62SCasey Schaufler new_tsec = selinux_cred(bprm->cred); 233383da53c5SAndreas Gruenbacher isec = inode_security(inode); 23341da177e4SLinus Torvalds 23351da177e4SLinus Torvalds /* Default to the current task SID. */ 2336a6f76f23SDavid Howells new_tsec->sid = old_tsec->sid; 2337a6f76f23SDavid Howells new_tsec->osid = old_tsec->sid; 23381da177e4SLinus Torvalds 233928eba5bfSMichael LeMay /* Reset fs, key, and sock SIDs on execve. */ 2340a6f76f23SDavid Howells new_tsec->create_sid = 0; 2341a6f76f23SDavid Howells new_tsec->keycreate_sid = 0; 2342a6f76f23SDavid Howells new_tsec->sockcreate_sid = 0; 23431da177e4SLinus Torvalds 2344a6f76f23SDavid Howells if (old_tsec->exec_sid) { 2345a6f76f23SDavid Howells new_tsec->sid = old_tsec->exec_sid; 23461da177e4SLinus Torvalds /* Reset exec SID on execve. */ 2347a6f76f23SDavid Howells new_tsec->exec_sid = 0; 2348259e5e6cSAndy Lutomirski 23497b0d0b40SStephen Smalley /* Fail on NNP or nosuid if not an allowed transition. */ 23507b0d0b40SStephen Smalley rc = check_nnp_nosuid(bprm, old_tsec, new_tsec); 23517b0d0b40SStephen Smalley if (rc) 23527b0d0b40SStephen Smalley return rc; 23531da177e4SLinus Torvalds } else { 23541da177e4SLinus Torvalds /* Check for a default transition on this program. */ 2355aa8e712cSStephen Smalley rc = security_transition_sid(&selinux_state, old_tsec->sid, 2356aa8e712cSStephen Smalley isec->sid, SECCLASS_PROCESS, NULL, 2357652bb9b0SEric Paris &new_tsec->sid); 23581da177e4SLinus Torvalds if (rc) 23591da177e4SLinus Torvalds return rc; 23607b0d0b40SStephen Smalley 23617b0d0b40SStephen Smalley /* 23627b0d0b40SStephen Smalley * Fallback to old SID on NNP or nosuid if not an allowed 23637b0d0b40SStephen Smalley * transition. 23647b0d0b40SStephen Smalley */ 23657b0d0b40SStephen Smalley rc = check_nnp_nosuid(bprm, old_tsec, new_tsec); 23667b0d0b40SStephen Smalley if (rc) 23677b0d0b40SStephen Smalley new_tsec->sid = old_tsec->sid; 23681da177e4SLinus Torvalds } 23691da177e4SLinus Torvalds 237043af5de7SVivek Goyal ad.type = LSM_AUDIT_DATA_FILE; 237143af5de7SVivek Goyal ad.u.file = bprm->file; 23721da177e4SLinus Torvalds 2373a6f76f23SDavid Howells if (new_tsec->sid == old_tsec->sid) { 23746b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 23756b6bc620SStephen Smalley old_tsec->sid, isec->sid, 23761da177e4SLinus Torvalds SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad); 23771da177e4SLinus Torvalds if (rc) 23781da177e4SLinus Torvalds return rc; 23791da177e4SLinus Torvalds } else { 23801da177e4SLinus Torvalds /* Check permissions for the transition. */ 23816b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 23826b6bc620SStephen Smalley old_tsec->sid, new_tsec->sid, 23831da177e4SLinus Torvalds SECCLASS_PROCESS, PROCESS__TRANSITION, &ad); 23841da177e4SLinus Torvalds if (rc) 23851da177e4SLinus Torvalds return rc; 23861da177e4SLinus Torvalds 23876b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 23886b6bc620SStephen Smalley new_tsec->sid, isec->sid, 23891da177e4SLinus Torvalds SECCLASS_FILE, FILE__ENTRYPOINT, &ad); 23901da177e4SLinus Torvalds if (rc) 23911da177e4SLinus Torvalds return rc; 23921da177e4SLinus Torvalds 2393a6f76f23SDavid Howells /* Check for shared state */ 2394a6f76f23SDavid Howells if (bprm->unsafe & LSM_UNSAFE_SHARE) { 23956b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 23966b6bc620SStephen Smalley old_tsec->sid, new_tsec->sid, 2397a6f76f23SDavid Howells SECCLASS_PROCESS, PROCESS__SHARE, 2398a6f76f23SDavid Howells NULL); 2399a6f76f23SDavid Howells if (rc) 2400a6f76f23SDavid Howells return -EPERM; 24011da177e4SLinus Torvalds } 24021da177e4SLinus Torvalds 2403a6f76f23SDavid Howells /* Make sure that anyone attempting to ptrace over a task that 2404a6f76f23SDavid Howells * changes its SID has the appropriate permit */ 24059227dd2aSEric W. Biederman if (bprm->unsafe & LSM_UNSAFE_PTRACE) { 2406be0554c9SStephen Smalley u32 ptsid = ptrace_parent_sid(); 2407a6f76f23SDavid Howells if (ptsid != 0) { 24086b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 24096b6bc620SStephen Smalley ptsid, new_tsec->sid, 2410a6f76f23SDavid Howells SECCLASS_PROCESS, 2411a6f76f23SDavid Howells PROCESS__PTRACE, NULL); 2412a6f76f23SDavid Howells if (rc) 2413a6f76f23SDavid Howells return -EPERM; 2414a6f76f23SDavid Howells } 2415a6f76f23SDavid Howells } 2416a6f76f23SDavid Howells 2417a6f76f23SDavid Howells /* Clear any possibly unsafe personality bits on exec: */ 2418a6f76f23SDavid Howells bprm->per_clear |= PER_CLEAR_ON_SETID; 2419a6f76f23SDavid Howells 24201da177e4SLinus Torvalds /* Enable secure mode for SIDs transitions unless 24211da177e4SLinus Torvalds the noatsecure permission is granted between 24221da177e4SLinus Torvalds the two SIDs, i.e. ahp returns 0. */ 24236b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 24246b6bc620SStephen Smalley old_tsec->sid, new_tsec->sid, 242562874c3aSKees Cook SECCLASS_PROCESS, PROCESS__NOATSECURE, 242662874c3aSKees Cook NULL); 242762874c3aSKees Cook bprm->secureexec |= !!rc; 24281da177e4SLinus Torvalds } 24291da177e4SLinus Torvalds 243062874c3aSKees Cook return 0; 24311da177e4SLinus Torvalds } 24321da177e4SLinus Torvalds 2433c3c073f8SAl Viro static int match_file(const void *p, struct file *file, unsigned fd) 2434c3c073f8SAl Viro { 2435c3c073f8SAl Viro return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0; 2436c3c073f8SAl Viro } 2437c3c073f8SAl Viro 24381da177e4SLinus Torvalds /* Derived from fs/exec.c:flush_old_files. */ 2439745ca247SDavid Howells static inline void flush_unauthorized_files(const struct cred *cred, 2440745ca247SDavid Howells struct files_struct *files) 24411da177e4SLinus Torvalds { 24421da177e4SLinus Torvalds struct file *file, *devnull = NULL; 2443b20c8122SStephen Smalley struct tty_struct *tty; 244424ec839cSPeter Zijlstra int drop_tty = 0; 2445c3c073f8SAl Viro unsigned n; 24461da177e4SLinus Torvalds 244724ec839cSPeter Zijlstra tty = get_current_tty(); 24481da177e4SLinus Torvalds if (tty) { 24494a510969SPeter Hurley spin_lock(&tty->files_lock); 245037dd0bd0SEric Paris if (!list_empty(&tty->tty_files)) { 2451d996b62aSNick Piggin struct tty_file_private *file_priv; 245237dd0bd0SEric Paris 24531da177e4SLinus Torvalds /* Revalidate access to controlling tty. 245413f8e981SDavid Howells Use file_path_has_perm on the tty path directly 245513f8e981SDavid Howells rather than using file_has_perm, as this particular 245613f8e981SDavid Howells open file may belong to another process and we are 245713f8e981SDavid Howells only interested in the inode-based check here. */ 2458d996b62aSNick Piggin file_priv = list_first_entry(&tty->tty_files, 2459d996b62aSNick Piggin struct tty_file_private, list); 2460d996b62aSNick Piggin file = file_priv->file; 246113f8e981SDavid Howells if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE)) 246224ec839cSPeter Zijlstra drop_tty = 1; 24631da177e4SLinus Torvalds } 24644a510969SPeter Hurley spin_unlock(&tty->files_lock); 2465452a00d2SAlan Cox tty_kref_put(tty); 24661da177e4SLinus Torvalds } 246798a27ba4SEric W. Biederman /* Reset controlling tty. */ 246898a27ba4SEric W. Biederman if (drop_tty) 246998a27ba4SEric W. Biederman no_tty(); 24701da177e4SLinus Torvalds 24711da177e4SLinus Torvalds /* Revalidate access to inherited open files. */ 2472c3c073f8SAl Viro n = iterate_fd(files, 0, match_file, cred); 2473c3c073f8SAl Viro if (!n) /* none found? */ 2474c3c073f8SAl Viro return; 24751da177e4SLinus Torvalds 2476c3c073f8SAl Viro devnull = dentry_open(&selinux_null, O_RDWR, cred); 247745525b26SAl Viro if (IS_ERR(devnull)) 247845525b26SAl Viro devnull = NULL; 2479c3c073f8SAl Viro /* replace all the matching ones with this */ 2480c3c073f8SAl Viro do { 248145525b26SAl Viro replace_fd(n - 1, devnull, 0); 2482c3c073f8SAl Viro } while ((n = iterate_fd(files, n, match_file, cred)) != 0); 248345525b26SAl Viro if (devnull) 2484c3c073f8SAl Viro fput(devnull); 24851da177e4SLinus Torvalds } 24861da177e4SLinus Torvalds 24871da177e4SLinus Torvalds /* 2488a6f76f23SDavid Howells * Prepare a process for imminent new credential changes due to exec 24891da177e4SLinus Torvalds */ 2490a6f76f23SDavid Howells static void selinux_bprm_committing_creds(struct linux_binprm *bprm) 24911da177e4SLinus Torvalds { 2492a6f76f23SDavid Howells struct task_security_struct *new_tsec; 24931da177e4SLinus Torvalds struct rlimit *rlim, *initrlim; 24941da177e4SLinus Torvalds int rc, i; 24951da177e4SLinus Torvalds 24960c6cfa62SCasey Schaufler new_tsec = selinux_cred(bprm->cred); 2497a6f76f23SDavid Howells if (new_tsec->sid == new_tsec->osid) 24981da177e4SLinus Torvalds return; 24991da177e4SLinus Torvalds 25001da177e4SLinus Torvalds /* Close files for which the new task SID is not authorized. */ 2501a6f76f23SDavid Howells flush_unauthorized_files(bprm->cred, current->files); 25021da177e4SLinus Torvalds 2503a6f76f23SDavid Howells /* Always clear parent death signal on SID transitions. */ 2504a6f76f23SDavid Howells current->pdeath_signal = 0; 2505a6f76f23SDavid Howells 2506a6f76f23SDavid Howells /* Check whether the new SID can inherit resource limits from the old 2507a6f76f23SDavid Howells * SID. If not, reset all soft limits to the lower of the current 2508a6f76f23SDavid Howells * task's hard limit and the init task's soft limit. 2509a6f76f23SDavid Howells * 2510a6f76f23SDavid Howells * Note that the setting of hard limits (even to lower them) can be 2511a6f76f23SDavid Howells * controlled by the setrlimit check. The inclusion of the init task's 2512a6f76f23SDavid Howells * soft limit into the computation is to avoid resetting soft limits 2513a6f76f23SDavid Howells * higher than the default soft limit for cases where the default is 2514a6f76f23SDavid Howells * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK. 2515a6f76f23SDavid Howells */ 25166b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 25176b6bc620SStephen Smalley new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS, 2518a6f76f23SDavid Howells PROCESS__RLIMITINH, NULL); 2519a6f76f23SDavid Howells if (rc) { 2520eb2d55a3SOleg Nesterov /* protect against do_prlimit() */ 2521eb2d55a3SOleg Nesterov task_lock(current); 2522a6f76f23SDavid Howells for (i = 0; i < RLIM_NLIMITS; i++) { 2523a6f76f23SDavid Howells rlim = current->signal->rlim + i; 2524a6f76f23SDavid Howells initrlim = init_task.signal->rlim + i; 2525a6f76f23SDavid Howells rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur); 2526a6f76f23SDavid Howells } 2527eb2d55a3SOleg Nesterov task_unlock(current); 2528baa73d9eSNicolas Pitre if (IS_ENABLED(CONFIG_POSIX_TIMERS)) 2529eb2d55a3SOleg Nesterov update_rlimit_cpu(current, rlimit(RLIMIT_CPU)); 2530a6f76f23SDavid Howells } 2531a6f76f23SDavid Howells } 2532a6f76f23SDavid Howells 2533a6f76f23SDavid Howells /* 2534a6f76f23SDavid Howells * Clean up the process immediately after the installation of new credentials 2535a6f76f23SDavid Howells * due to exec 2536a6f76f23SDavid Howells */ 2537a6f76f23SDavid Howells static void selinux_bprm_committed_creds(struct linux_binprm *bprm) 2538a6f76f23SDavid Howells { 25390c6cfa62SCasey Schaufler const struct task_security_struct *tsec = selinux_cred(current_cred()); 2540a6f76f23SDavid Howells struct itimerval itimer; 2541a6f76f23SDavid Howells u32 osid, sid; 2542a6f76f23SDavid Howells int rc, i; 2543a6f76f23SDavid Howells 2544a6f76f23SDavid Howells osid = tsec->osid; 2545a6f76f23SDavid Howells sid = tsec->sid; 2546a6f76f23SDavid Howells 2547a6f76f23SDavid Howells if (sid == osid) 2548a6f76f23SDavid Howells return; 2549a6f76f23SDavid Howells 2550a6f76f23SDavid Howells /* Check whether the new SID can inherit signal state from the old SID. 2551a6f76f23SDavid Howells * If not, clear itimers to avoid subsequent signal generation and 2552a6f76f23SDavid Howells * flush and unblock signals. 2553a6f76f23SDavid Howells * 2554a6f76f23SDavid Howells * This must occur _after_ the task SID has been updated so that any 2555a6f76f23SDavid Howells * kill done after the flush will be checked against the new SID. 2556a6f76f23SDavid Howells */ 25576b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 25586b6bc620SStephen Smalley osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL); 25591da177e4SLinus Torvalds if (rc) { 2560baa73d9eSNicolas Pitre if (IS_ENABLED(CONFIG_POSIX_TIMERS)) { 25611da177e4SLinus Torvalds memset(&itimer, 0, sizeof itimer); 25621da177e4SLinus Torvalds for (i = 0; i < 3; i++) 25631da177e4SLinus Torvalds do_setitimer(i, &itimer, NULL); 2564baa73d9eSNicolas Pitre } 25651da177e4SLinus Torvalds spin_lock_irq(¤t->sighand->siglock); 25669e7c8f8cSOleg Nesterov if (!fatal_signal_pending(current)) { 25679e7c8f8cSOleg Nesterov flush_sigqueue(¤t->pending); 25689e7c8f8cSOleg Nesterov flush_sigqueue(¤t->signal->shared_pending); 25691da177e4SLinus Torvalds flush_signal_handlers(current, 1); 25701da177e4SLinus Torvalds sigemptyset(¤t->blocked); 25719e7c8f8cSOleg Nesterov recalc_sigpending(); 25723bcac026SDavid Howells } 25731da177e4SLinus Torvalds spin_unlock_irq(¤t->sighand->siglock); 25741da177e4SLinus Torvalds } 25751da177e4SLinus Torvalds 2576a6f76f23SDavid Howells /* Wake up the parent if it is waiting so that it can recheck 2577a6f76f23SDavid Howells * wait permission to the new task SID. */ 2578ecd6de3cSOleg Nesterov read_lock(&tasklist_lock); 25790b7570e7SOleg Nesterov __wake_up_parent(current, current->real_parent); 2580ecd6de3cSOleg Nesterov read_unlock(&tasklist_lock); 25811da177e4SLinus Torvalds } 25821da177e4SLinus Torvalds 25831da177e4SLinus Torvalds /* superblock security operations */ 25841da177e4SLinus Torvalds 25851da177e4SLinus Torvalds static int selinux_sb_alloc_security(struct super_block *sb) 25861da177e4SLinus Torvalds { 25871da177e4SLinus Torvalds return superblock_alloc_security(sb); 25881da177e4SLinus Torvalds } 25891da177e4SLinus Torvalds 25901da177e4SLinus Torvalds static void selinux_sb_free_security(struct super_block *sb) 25911da177e4SLinus Torvalds { 25921da177e4SLinus Torvalds superblock_free_security(sb); 25931da177e4SLinus Torvalds } 25941da177e4SLinus Torvalds 259599dbbb59SAl Viro static inline int opt_len(const char *s) 25961da177e4SLinus Torvalds { 259799dbbb59SAl Viro bool open_quote = false; 259899dbbb59SAl Viro int len; 259999dbbb59SAl Viro char c; 26001da177e4SLinus Torvalds 260199dbbb59SAl Viro for (len = 0; (c = s[len]) != '\0'; len++) { 260299dbbb59SAl Viro if (c == '"') 26033528a953SCory Olmo open_quote = !open_quote; 260499dbbb59SAl Viro if (c == ',' && !open_quote) 260599dbbb59SAl Viro break; 26061da177e4SLinus Torvalds } 260799dbbb59SAl Viro return len; 26081da177e4SLinus Torvalds } 26091da177e4SLinus Torvalds 2610204cc0ccSAl Viro static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts) 26115b400239SAl Viro { 261299dbbb59SAl Viro char *from = options; 261399dbbb59SAl Viro char *to = options; 261499dbbb59SAl Viro bool first = true; 26155b400239SAl Viro 261699dbbb59SAl Viro while (1) { 261799dbbb59SAl Viro int len = opt_len(from); 261899dbbb59SAl Viro int token, rc; 261999dbbb59SAl Viro char *arg = NULL; 262099dbbb59SAl Viro 262199dbbb59SAl Viro token = match_opt_prefix(from, len, &arg); 262299dbbb59SAl Viro 262399dbbb59SAl Viro if (token != Opt_error) { 262499dbbb59SAl Viro char *p, *q; 262599dbbb59SAl Viro 262699dbbb59SAl Viro /* strip quotes */ 262799dbbb59SAl Viro if (arg) { 262899dbbb59SAl Viro for (p = q = arg; p < from + len; p++) { 262999dbbb59SAl Viro char c = *p; 263099dbbb59SAl Viro if (c != '"') 263199dbbb59SAl Viro *q++ = c; 263299dbbb59SAl Viro } 263399dbbb59SAl Viro arg = kmemdup_nul(arg, q - arg, GFP_KERNEL); 263499dbbb59SAl Viro } 263599dbbb59SAl Viro rc = selinux_add_opt(token, arg, mnt_opts); 263699dbbb59SAl Viro if (unlikely(rc)) { 263799dbbb59SAl Viro kfree(arg); 263899dbbb59SAl Viro if (*mnt_opts) { 263999dbbb59SAl Viro selinux_free_mnt_opts(*mnt_opts); 264099dbbb59SAl Viro *mnt_opts = NULL; 264199dbbb59SAl Viro } 26421da177e4SLinus Torvalds return rc; 26431da177e4SLinus Torvalds } 264499dbbb59SAl Viro } else { 264599dbbb59SAl Viro if (!first) { // copy with preceding comma 264699dbbb59SAl Viro from--; 264799dbbb59SAl Viro len++; 264899dbbb59SAl Viro } 264999dbbb59SAl Viro if (to != from) 265099dbbb59SAl Viro memmove(to, from, len); 265199dbbb59SAl Viro to += len; 265299dbbb59SAl Viro first = false; 265399dbbb59SAl Viro } 265499dbbb59SAl Viro if (!from[len]) 265599dbbb59SAl Viro break; 265699dbbb59SAl Viro from += len + 1; 265799dbbb59SAl Viro } 265899dbbb59SAl Viro *to = '\0'; 265999dbbb59SAl Viro return 0; 26605b400239SAl Viro } 26611da177e4SLinus Torvalds 2662204cc0ccSAl Viro static int selinux_sb_remount(struct super_block *sb, void *mnt_opts) 2663026eb167SEric Paris { 2664bd323655SAl Viro struct selinux_mnt_opts *opts = mnt_opts; 2665026eb167SEric Paris struct superblock_security_struct *sbsec = sb->s_security; 2666bd323655SAl Viro u32 sid; 2667bd323655SAl Viro int rc; 2668026eb167SEric Paris 2669026eb167SEric Paris if (!(sbsec->flags & SE_SBINITIALIZED)) 2670026eb167SEric Paris return 0; 2671026eb167SEric Paris 2672204cc0ccSAl Viro if (!opts) 2673026eb167SEric Paris return 0; 2674026eb167SEric Paris 2675bd323655SAl Viro if (opts->fscontext) { 2676bd323655SAl Viro rc = parse_sid(sb, opts->fscontext, &sid); 2677026eb167SEric Paris if (rc) 2678c039bc3cSAl Viro return rc; 2679026eb167SEric Paris if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid)) 2680026eb167SEric Paris goto out_bad_option; 2681bd323655SAl Viro } 2682bd323655SAl Viro if (opts->context) { 2683bd323655SAl Viro rc = parse_sid(sb, opts->context, &sid); 2684bd323655SAl Viro if (rc) 2685bd323655SAl Viro return rc; 2686026eb167SEric Paris if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid)) 2687026eb167SEric Paris goto out_bad_option; 2688bd323655SAl Viro } 2689bd323655SAl Viro if (opts->rootcontext) { 2690026eb167SEric Paris struct inode_security_struct *root_isec; 269183da53c5SAndreas Gruenbacher root_isec = backing_inode_security(sb->s_root); 2692bd323655SAl Viro rc = parse_sid(sb, opts->rootcontext, &sid); 2693bd323655SAl Viro if (rc) 2694bd323655SAl Viro return rc; 2695026eb167SEric Paris if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid)) 2696026eb167SEric Paris goto out_bad_option; 2697026eb167SEric Paris } 2698bd323655SAl Viro if (opts->defcontext) { 2699bd323655SAl Viro rc = parse_sid(sb, opts->defcontext, &sid); 2700bd323655SAl Viro if (rc) 2701bd323655SAl Viro return rc; 2702026eb167SEric Paris if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid)) 2703026eb167SEric Paris goto out_bad_option; 2704026eb167SEric Paris } 2705c039bc3cSAl Viro return 0; 2706026eb167SEric Paris 2707026eb167SEric Paris out_bad_option: 2708c103a91eSpeter enderborg pr_warn("SELinux: unable to change security options " 270929b1deb2SLinus Torvalds "during remount (dev %s, type=%s)\n", sb->s_id, 271029b1deb2SLinus Torvalds sb->s_type->name); 2711c039bc3cSAl Viro return -EINVAL; 2712026eb167SEric Paris } 2713026eb167SEric Paris 2714a10d7c22SAl Viro static int selinux_sb_kern_mount(struct super_block *sb) 27151da177e4SLinus Torvalds { 271688e67f3bSDavid Howells const struct cred *cred = current_cred(); 27172bf49690SThomas Liu struct common_audit_data ad; 271874192246SJames Morris 271950c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 2720a269434dSEric Paris ad.u.dentry = sb->s_root; 272188e67f3bSDavid Howells return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad); 27221da177e4SLinus Torvalds } 27231da177e4SLinus Torvalds 2724726c3342SDavid Howells static int selinux_sb_statfs(struct dentry *dentry) 27251da177e4SLinus Torvalds { 272688e67f3bSDavid Howells const struct cred *cred = current_cred(); 27272bf49690SThomas Liu struct common_audit_data ad; 27281da177e4SLinus Torvalds 272950c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 2730a269434dSEric Paris ad.u.dentry = dentry->d_sb->s_root; 273188e67f3bSDavid Howells return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad); 27321da177e4SLinus Torvalds } 27331da177e4SLinus Torvalds 2734808d4e3cSAl Viro static int selinux_mount(const char *dev_name, 27358a04c43bSAl Viro const struct path *path, 2736808d4e3cSAl Viro const char *type, 27371da177e4SLinus Torvalds unsigned long flags, 27381da177e4SLinus Torvalds void *data) 27391da177e4SLinus Torvalds { 274088e67f3bSDavid Howells const struct cred *cred = current_cred(); 27411da177e4SLinus Torvalds 27421da177e4SLinus Torvalds if (flags & MS_REMOUNT) 2743d8c9584eSAl Viro return superblock_has_perm(cred, path->dentry->d_sb, 27441da177e4SLinus Torvalds FILESYSTEM__REMOUNT, NULL); 27451da177e4SLinus Torvalds else 27462875fa00SEric Paris return path_has_perm(cred, path, FILE__MOUNTON); 27471da177e4SLinus Torvalds } 27481da177e4SLinus Torvalds 27491da177e4SLinus Torvalds static int selinux_umount(struct vfsmount *mnt, int flags) 27501da177e4SLinus Torvalds { 275188e67f3bSDavid Howells const struct cred *cred = current_cred(); 27521da177e4SLinus Torvalds 275388e67f3bSDavid Howells return superblock_has_perm(cred, mnt->mnt_sb, 27541da177e4SLinus Torvalds FILESYSTEM__UNMOUNT, NULL); 27551da177e4SLinus Torvalds } 27561da177e4SLinus Torvalds 27571da177e4SLinus Torvalds /* inode security operations */ 27581da177e4SLinus Torvalds 27591da177e4SLinus Torvalds static int selinux_inode_alloc_security(struct inode *inode) 27601da177e4SLinus Torvalds { 27611da177e4SLinus Torvalds return inode_alloc_security(inode); 27621da177e4SLinus Torvalds } 27631da177e4SLinus Torvalds 27641da177e4SLinus Torvalds static void selinux_inode_free_security(struct inode *inode) 27651da177e4SLinus Torvalds { 27661da177e4SLinus Torvalds inode_free_security(inode); 27671da177e4SLinus Torvalds } 27681da177e4SLinus Torvalds 2769d47be3dfSDavid Quigley static int selinux_dentry_init_security(struct dentry *dentry, int mode, 27704f3ccd76SAl Viro const struct qstr *name, void **ctx, 2771d47be3dfSDavid Quigley u32 *ctxlen) 2772d47be3dfSDavid Quigley { 2773d47be3dfSDavid Quigley u32 newsid; 2774d47be3dfSDavid Quigley int rc; 2775d47be3dfSDavid Quigley 27760c6cfa62SCasey Schaufler rc = selinux_determine_inode_label(selinux_cred(current_cred()), 2777c957f6dfSVivek Goyal d_inode(dentry->d_parent), name, 2778d47be3dfSDavid Quigley inode_mode_to_security_class(mode), 2779d47be3dfSDavid Quigley &newsid); 2780c3c188b2SDavid Howells if (rc) 2781d47be3dfSDavid Quigley return rc; 2782d47be3dfSDavid Quigley 2783aa8e712cSStephen Smalley return security_sid_to_context(&selinux_state, newsid, (char **)ctx, 2784aa8e712cSStephen Smalley ctxlen); 2785d47be3dfSDavid Quigley } 2786d47be3dfSDavid Quigley 2787a518b0a5SVivek Goyal static int selinux_dentry_create_files_as(struct dentry *dentry, int mode, 2788a518b0a5SVivek Goyal struct qstr *name, 2789a518b0a5SVivek Goyal const struct cred *old, 2790a518b0a5SVivek Goyal struct cred *new) 2791a518b0a5SVivek Goyal { 2792a518b0a5SVivek Goyal u32 newsid; 2793a518b0a5SVivek Goyal int rc; 2794a518b0a5SVivek Goyal struct task_security_struct *tsec; 2795a518b0a5SVivek Goyal 27960c6cfa62SCasey Schaufler rc = selinux_determine_inode_label(selinux_cred(old), 2797a518b0a5SVivek Goyal d_inode(dentry->d_parent), name, 2798a518b0a5SVivek Goyal inode_mode_to_security_class(mode), 2799a518b0a5SVivek Goyal &newsid); 2800a518b0a5SVivek Goyal if (rc) 2801a518b0a5SVivek Goyal return rc; 2802a518b0a5SVivek Goyal 28030c6cfa62SCasey Schaufler tsec = selinux_cred(new); 2804a518b0a5SVivek Goyal tsec->create_sid = newsid; 2805a518b0a5SVivek Goyal return 0; 2806a518b0a5SVivek Goyal } 2807a518b0a5SVivek Goyal 28085e41ff9eSStephen Smalley static int selinux_inode_init_security(struct inode *inode, struct inode *dir, 28099548906bSTetsuo Handa const struct qstr *qstr, 28109548906bSTetsuo Handa const char **name, 28112a7dba39SEric Paris void **value, size_t *len) 28125e41ff9eSStephen Smalley { 28130c6cfa62SCasey Schaufler const struct task_security_struct *tsec = selinux_cred(current_cred()); 28145e41ff9eSStephen Smalley struct superblock_security_struct *sbsec; 2815c0d4f464SCorentin LABBE u32 newsid, clen; 28165e41ff9eSStephen Smalley int rc; 28179548906bSTetsuo Handa char *context; 28185e41ff9eSStephen Smalley 28195e41ff9eSStephen Smalley sbsec = dir->i_sb->s_security; 28205e41ff9eSStephen Smalley 28215e41ff9eSStephen Smalley newsid = tsec->create_sid; 2822275bb41eSDavid Howells 28230c6cfa62SCasey Schaufler rc = selinux_determine_inode_label(selinux_cred(current_cred()), 2824c3c188b2SDavid Howells dir, qstr, 28255e41ff9eSStephen Smalley inode_mode_to_security_class(inode->i_mode), 2826c3c188b2SDavid Howells &newsid); 2827c3c188b2SDavid Howells if (rc) 28285e41ff9eSStephen Smalley return rc; 28295e41ff9eSStephen Smalley 2830296fddf7SEric Paris /* Possibly defer initialization to selinux_complete_init. */ 28310d90a7ecSDavid P. Quigley if (sbsec->flags & SE_SBINITIALIZED) { 2832296fddf7SEric Paris struct inode_security_struct *isec = inode->i_security; 2833296fddf7SEric Paris isec->sclass = inode_mode_to_security_class(inode->i_mode); 2834296fddf7SEric Paris isec->sid = newsid; 28356f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 2836296fddf7SEric Paris } 28375e41ff9eSStephen Smalley 2838aa8e712cSStephen Smalley if (!selinux_state.initialized || !(sbsec->flags & SBLABEL_MNT)) 283925a74f3bSStephen Smalley return -EOPNOTSUPP; 284025a74f3bSStephen Smalley 28419548906bSTetsuo Handa if (name) 28429548906bSTetsuo Handa *name = XATTR_SELINUX_SUFFIX; 28435e41ff9eSStephen Smalley 2844570bc1c2SStephen Smalley if (value && len) { 2845aa8e712cSStephen Smalley rc = security_sid_to_context_force(&selinux_state, newsid, 2846aa8e712cSStephen Smalley &context, &clen); 28479548906bSTetsuo Handa if (rc) 28485e41ff9eSStephen Smalley return rc; 28495e41ff9eSStephen Smalley *value = context; 2850570bc1c2SStephen Smalley *len = clen; 2851570bc1c2SStephen Smalley } 28525e41ff9eSStephen Smalley 28535e41ff9eSStephen Smalley return 0; 28545e41ff9eSStephen Smalley } 28555e41ff9eSStephen Smalley 28564acdaf27SAl Viro static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode) 28571da177e4SLinus Torvalds { 28581da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_FILE); 28591da177e4SLinus Torvalds } 28601da177e4SLinus Torvalds 28611da177e4SLinus Torvalds static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 28621da177e4SLinus Torvalds { 28631da177e4SLinus Torvalds return may_link(dir, old_dentry, MAY_LINK); 28641da177e4SLinus Torvalds } 28651da177e4SLinus Torvalds 28661da177e4SLinus Torvalds static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry) 28671da177e4SLinus Torvalds { 28681da177e4SLinus Torvalds return may_link(dir, dentry, MAY_UNLINK); 28691da177e4SLinus Torvalds } 28701da177e4SLinus Torvalds 28711da177e4SLinus Torvalds static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name) 28721da177e4SLinus Torvalds { 28731da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_LNK_FILE); 28741da177e4SLinus Torvalds } 28751da177e4SLinus Torvalds 287618bb1db3SAl Viro static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask) 28771da177e4SLinus Torvalds { 28781da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_DIR); 28791da177e4SLinus Torvalds } 28801da177e4SLinus Torvalds 28811da177e4SLinus Torvalds static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry) 28821da177e4SLinus Torvalds { 28831da177e4SLinus Torvalds return may_link(dir, dentry, MAY_RMDIR); 28841da177e4SLinus Torvalds } 28851da177e4SLinus Torvalds 28861a67aafbSAl Viro static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) 28871da177e4SLinus Torvalds { 28881da177e4SLinus Torvalds return may_create(dir, dentry, inode_mode_to_security_class(mode)); 28891da177e4SLinus Torvalds } 28901da177e4SLinus Torvalds 28911da177e4SLinus Torvalds static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry, 28921da177e4SLinus Torvalds struct inode *new_inode, struct dentry *new_dentry) 28931da177e4SLinus Torvalds { 28941da177e4SLinus Torvalds return may_rename(old_inode, old_dentry, new_inode, new_dentry); 28951da177e4SLinus Torvalds } 28961da177e4SLinus Torvalds 28971da177e4SLinus Torvalds static int selinux_inode_readlink(struct dentry *dentry) 28981da177e4SLinus Torvalds { 289988e67f3bSDavid Howells const struct cred *cred = current_cred(); 290088e67f3bSDavid Howells 29012875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__READ); 29021da177e4SLinus Torvalds } 29031da177e4SLinus Torvalds 2904bda0be7aSNeilBrown static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode, 2905bda0be7aSNeilBrown bool rcu) 29061da177e4SLinus Torvalds { 290788e67f3bSDavid Howells const struct cred *cred = current_cred(); 2908bda0be7aSNeilBrown struct common_audit_data ad; 2909bda0be7aSNeilBrown struct inode_security_struct *isec; 2910bda0be7aSNeilBrown u32 sid; 29111da177e4SLinus Torvalds 2912bda0be7aSNeilBrown validate_creds(cred); 2913bda0be7aSNeilBrown 2914bda0be7aSNeilBrown ad.type = LSM_AUDIT_DATA_DENTRY; 2915bda0be7aSNeilBrown ad.u.dentry = dentry; 2916bda0be7aSNeilBrown sid = cred_sid(cred); 29175d226df4SAndreas Gruenbacher isec = inode_security_rcu(inode, rcu); 29185d226df4SAndreas Gruenbacher if (IS_ERR(isec)) 29195d226df4SAndreas Gruenbacher return PTR_ERR(isec); 2920bda0be7aSNeilBrown 29216b6bc620SStephen Smalley return avc_has_perm_flags(&selinux_state, 29226b6bc620SStephen Smalley sid, isec->sid, isec->sclass, FILE__READ, &ad, 2923bda0be7aSNeilBrown rcu ? MAY_NOT_BLOCK : 0); 29241da177e4SLinus Torvalds } 29251da177e4SLinus Torvalds 2926d4cf970dSEric Paris static noinline int audit_inode_permission(struct inode *inode, 2927d4cf970dSEric Paris u32 perms, u32 audited, u32 denied, 2928626b9740SStephen Smalley int result, 2929d4cf970dSEric Paris unsigned flags) 2930d4cf970dSEric Paris { 2931d4cf970dSEric Paris struct common_audit_data ad; 2932d4cf970dSEric Paris struct inode_security_struct *isec = inode->i_security; 2933d4cf970dSEric Paris int rc; 2934d4cf970dSEric Paris 293550c205f5SEric Paris ad.type = LSM_AUDIT_DATA_INODE; 2936d4cf970dSEric Paris ad.u.inode = inode; 2937d4cf970dSEric Paris 29386b6bc620SStephen Smalley rc = slow_avc_audit(&selinux_state, 29396b6bc620SStephen Smalley current_sid(), isec->sid, isec->sclass, perms, 2940626b9740SStephen Smalley audited, denied, result, &ad, flags); 2941d4cf970dSEric Paris if (rc) 2942d4cf970dSEric Paris return rc; 2943d4cf970dSEric Paris return 0; 2944d4cf970dSEric Paris } 2945d4cf970dSEric Paris 2946e74f71ebSAl Viro static int selinux_inode_permission(struct inode *inode, int mask) 29471da177e4SLinus Torvalds { 294888e67f3bSDavid Howells const struct cred *cred = current_cred(); 2949b782e0a6SEric Paris u32 perms; 2950b782e0a6SEric Paris bool from_access; 2951cf1dd1daSAl Viro unsigned flags = mask & MAY_NOT_BLOCK; 29522e334057SEric Paris struct inode_security_struct *isec; 29532e334057SEric Paris u32 sid; 29542e334057SEric Paris struct av_decision avd; 29552e334057SEric Paris int rc, rc2; 29562e334057SEric Paris u32 audited, denied; 29571da177e4SLinus Torvalds 2958b782e0a6SEric Paris from_access = mask & MAY_ACCESS; 2959d09ca739SEric Paris mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND); 2960d09ca739SEric Paris 29611da177e4SLinus Torvalds /* No permission to check. Existence test. */ 2962b782e0a6SEric Paris if (!mask) 29631da177e4SLinus Torvalds return 0; 29641da177e4SLinus Torvalds 29652e334057SEric Paris validate_creds(cred); 2966b782e0a6SEric Paris 29672e334057SEric Paris if (unlikely(IS_PRIVATE(inode))) 29682e334057SEric Paris return 0; 2969b782e0a6SEric Paris 2970b782e0a6SEric Paris perms = file_mask_to_av(inode->i_mode, mask); 2971b782e0a6SEric Paris 29722e334057SEric Paris sid = cred_sid(cred); 29735d226df4SAndreas Gruenbacher isec = inode_security_rcu(inode, flags & MAY_NOT_BLOCK); 29745d226df4SAndreas Gruenbacher if (IS_ERR(isec)) 29755d226df4SAndreas Gruenbacher return PTR_ERR(isec); 29762e334057SEric Paris 29776b6bc620SStephen Smalley rc = avc_has_perm_noaudit(&selinux_state, 29786b6bc620SStephen Smalley sid, isec->sid, isec->sclass, perms, 0, &avd); 29792e334057SEric Paris audited = avc_audit_required(perms, &avd, rc, 29802e334057SEric Paris from_access ? FILE__AUDIT_ACCESS : 0, 29812e334057SEric Paris &denied); 29822e334057SEric Paris if (likely(!audited)) 29832e334057SEric Paris return rc; 29842e334057SEric Paris 2985626b9740SStephen Smalley rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags); 29862e334057SEric Paris if (rc2) 29872e334057SEric Paris return rc2; 29882e334057SEric Paris return rc; 29891da177e4SLinus Torvalds } 29901da177e4SLinus Torvalds 29911da177e4SLinus Torvalds static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr) 29921da177e4SLinus Torvalds { 299388e67f3bSDavid Howells const struct cred *cred = current_cred(); 2994ccb54478SStephen Smalley struct inode *inode = d_backing_inode(dentry); 2995bc6a6008SAmerigo Wang unsigned int ia_valid = iattr->ia_valid; 299695dbf739SEric Paris __u32 av = FILE__WRITE; 29971da177e4SLinus Torvalds 2998bc6a6008SAmerigo Wang /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */ 2999bc6a6008SAmerigo Wang if (ia_valid & ATTR_FORCE) { 3000bc6a6008SAmerigo Wang ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE | 3001bc6a6008SAmerigo Wang ATTR_FORCE); 3002bc6a6008SAmerigo Wang if (!ia_valid) 30031da177e4SLinus Torvalds return 0; 3004bc6a6008SAmerigo Wang } 30051da177e4SLinus Torvalds 3006bc6a6008SAmerigo Wang if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | 3007bc6a6008SAmerigo Wang ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET)) 30082875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__SETATTR); 30091da177e4SLinus Torvalds 3010aa8e712cSStephen Smalley if (selinux_policycap_openperm() && 3011ccb54478SStephen Smalley inode->i_sb->s_magic != SOCKFS_MAGIC && 3012ccb54478SStephen Smalley (ia_valid & ATTR_SIZE) && 3013ccb54478SStephen Smalley !(ia_valid & ATTR_FILE)) 301495dbf739SEric Paris av |= FILE__OPEN; 301595dbf739SEric Paris 301695dbf739SEric Paris return dentry_has_perm(cred, dentry, av); 30171da177e4SLinus Torvalds } 30181da177e4SLinus Torvalds 30193f7036a0SAl Viro static int selinux_inode_getattr(const struct path *path) 30201da177e4SLinus Torvalds { 30213f7036a0SAl Viro return path_has_perm(current_cred(), path, FILE__GETATTR); 30221da177e4SLinus Torvalds } 30231da177e4SLinus Torvalds 3024db59000aSStephen Smalley static bool has_cap_mac_admin(bool audit) 3025db59000aSStephen Smalley { 3026db59000aSStephen Smalley const struct cred *cred = current_cred(); 3027db59000aSStephen Smalley int cap_audit = audit ? SECURITY_CAP_AUDIT : SECURITY_CAP_NOAUDIT; 3028db59000aSStephen Smalley 3029db59000aSStephen Smalley if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, cap_audit)) 3030db59000aSStephen Smalley return false; 3031db59000aSStephen Smalley if (cred_has_capability(cred, CAP_MAC_ADMIN, cap_audit, true)) 3032db59000aSStephen Smalley return false; 3033db59000aSStephen Smalley return true; 3034db59000aSStephen Smalley } 3035db59000aSStephen Smalley 30368f0cfa52SDavid Howells static int selinux_inode_setxattr(struct dentry *dentry, const char *name, 30378f0cfa52SDavid Howells const void *value, size_t size, int flags) 30381da177e4SLinus Torvalds { 3039c6f493d6SDavid Howells struct inode *inode = d_backing_inode(dentry); 304020cdef8dSPaul Moore struct inode_security_struct *isec; 30411da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 30422bf49690SThomas Liu struct common_audit_data ad; 3043275bb41eSDavid Howells u32 newsid, sid = current_sid(); 30441da177e4SLinus Torvalds int rc = 0; 30451da177e4SLinus Torvalds 30466b240306SEric W. Biederman if (strcmp(name, XATTR_NAME_SELINUX)) { 30476b240306SEric W. Biederman rc = cap_inode_setxattr(dentry, name, value, size, flags); 30486b240306SEric W. Biederman if (rc) 30496b240306SEric W. Biederman return rc; 30506b240306SEric W. Biederman 30516b240306SEric W. Biederman /* Not an attribute we recognize, so just check the 30526b240306SEric W. Biederman ordinary setattr permission. */ 30536b240306SEric W. Biederman return dentry_has_perm(current_cred(), dentry, FILE__SETATTR); 30546b240306SEric W. Biederman } 30551da177e4SLinus Torvalds 30561da177e4SLinus Torvalds sbsec = inode->i_sb->s_security; 305712f348b9SEric Paris if (!(sbsec->flags & SBLABEL_MNT)) 30581da177e4SLinus Torvalds return -EOPNOTSUPP; 30591da177e4SLinus Torvalds 30602e149670SSerge E. Hallyn if (!inode_owner_or_capable(inode)) 30611da177e4SLinus Torvalds return -EPERM; 30621da177e4SLinus Torvalds 306350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 3064a269434dSEric Paris ad.u.dentry = dentry; 30651da177e4SLinus Torvalds 306620cdef8dSPaul Moore isec = backing_inode_security(dentry); 30676b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 30686b6bc620SStephen Smalley sid, isec->sid, isec->sclass, 30691da177e4SLinus Torvalds FILE__RELABELFROM, &ad); 30701da177e4SLinus Torvalds if (rc) 30711da177e4SLinus Torvalds return rc; 30721da177e4SLinus Torvalds 3073aa8e712cSStephen Smalley rc = security_context_to_sid(&selinux_state, value, size, &newsid, 3074aa8e712cSStephen Smalley GFP_KERNEL); 307512b29f34SStephen Smalley if (rc == -EINVAL) { 3076db59000aSStephen Smalley if (!has_cap_mac_admin(true)) { 3077d6ea83ecSEric Paris struct audit_buffer *ab; 3078d6ea83ecSEric Paris size_t audit_size; 3079d6ea83ecSEric Paris 3080d6ea83ecSEric Paris /* We strip a nul only if it is at the end, otherwise the 3081d6ea83ecSEric Paris * context contains a nul and we should audit that */ 3082e3fea3f7SAl Viro if (value) { 3083add24372SColin Ian King const char *str = value; 3084add24372SColin Ian King 3085d6ea83ecSEric Paris if (str[size - 1] == '\0') 3086d6ea83ecSEric Paris audit_size = size - 1; 3087d6ea83ecSEric Paris else 3088d6ea83ecSEric Paris audit_size = size; 3089e3fea3f7SAl Viro } else { 3090e3fea3f7SAl Viro audit_size = 0; 3091e3fea3f7SAl Viro } 3092cdfb6b34SRichard Guy Briggs ab = audit_log_start(audit_context(), 3093cdfb6b34SRichard Guy Briggs GFP_ATOMIC, AUDIT_SELINUX_ERR); 3094d6ea83ecSEric Paris audit_log_format(ab, "op=setxattr invalid_context="); 3095d6ea83ecSEric Paris audit_log_n_untrustedstring(ab, value, audit_size); 3096d6ea83ecSEric Paris audit_log_end(ab); 3097d6ea83ecSEric Paris 309812b29f34SStephen Smalley return rc; 3099d6ea83ecSEric Paris } 3100aa8e712cSStephen Smalley rc = security_context_to_sid_force(&selinux_state, value, 3101aa8e712cSStephen Smalley size, &newsid); 310212b29f34SStephen Smalley } 31031da177e4SLinus Torvalds if (rc) 31041da177e4SLinus Torvalds return rc; 31051da177e4SLinus Torvalds 31066b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 31076b6bc620SStephen Smalley sid, newsid, isec->sclass, 31081da177e4SLinus Torvalds FILE__RELABELTO, &ad); 31091da177e4SLinus Torvalds if (rc) 31101da177e4SLinus Torvalds return rc; 31111da177e4SLinus Torvalds 3112aa8e712cSStephen Smalley rc = security_validate_transition(&selinux_state, isec->sid, newsid, 3113aa8e712cSStephen Smalley sid, isec->sclass); 31141da177e4SLinus Torvalds if (rc) 31151da177e4SLinus Torvalds return rc; 31161da177e4SLinus Torvalds 31176b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 31186b6bc620SStephen Smalley newsid, 31191da177e4SLinus Torvalds sbsec->sid, 31201da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 31211da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, 31221da177e4SLinus Torvalds &ad); 31231da177e4SLinus Torvalds } 31241da177e4SLinus Torvalds 31258f0cfa52SDavid Howells static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name, 31268f0cfa52SDavid Howells const void *value, size_t size, 31278f0cfa52SDavid Howells int flags) 31281da177e4SLinus Torvalds { 3129c6f493d6SDavid Howells struct inode *inode = d_backing_inode(dentry); 313020cdef8dSPaul Moore struct inode_security_struct *isec; 31311da177e4SLinus Torvalds u32 newsid; 31321da177e4SLinus Torvalds int rc; 31331da177e4SLinus Torvalds 31341da177e4SLinus Torvalds if (strcmp(name, XATTR_NAME_SELINUX)) { 31351da177e4SLinus Torvalds /* Not an attribute we recognize, so nothing to do. */ 31361da177e4SLinus Torvalds return; 31371da177e4SLinus Torvalds } 31381da177e4SLinus Torvalds 3139aa8e712cSStephen Smalley rc = security_context_to_sid_force(&selinux_state, value, size, 3140aa8e712cSStephen Smalley &newsid); 31411da177e4SLinus Torvalds if (rc) { 3142c103a91eSpeter enderborg pr_err("SELinux: unable to map context to SID" 314312b29f34SStephen Smalley "for (%s, %lu), rc=%d\n", 314412b29f34SStephen Smalley inode->i_sb->s_id, inode->i_ino, -rc); 31451da177e4SLinus Torvalds return; 31461da177e4SLinus Torvalds } 31471da177e4SLinus Torvalds 314820cdef8dSPaul Moore isec = backing_inode_security(dentry); 31499287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 3150aa9c2669SDavid Quigley isec->sclass = inode_mode_to_security_class(inode->i_mode); 31511da177e4SLinus Torvalds isec->sid = newsid; 31526f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 31539287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 3154aa9c2669SDavid Quigley 31551da177e4SLinus Torvalds return; 31561da177e4SLinus Torvalds } 31571da177e4SLinus Torvalds 31588f0cfa52SDavid Howells static int selinux_inode_getxattr(struct dentry *dentry, const char *name) 31591da177e4SLinus Torvalds { 316088e67f3bSDavid Howells const struct cred *cred = current_cred(); 316188e67f3bSDavid Howells 31622875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__GETATTR); 31631da177e4SLinus Torvalds } 31641da177e4SLinus Torvalds 31651da177e4SLinus Torvalds static int selinux_inode_listxattr(struct dentry *dentry) 31661da177e4SLinus Torvalds { 316788e67f3bSDavid Howells const struct cred *cred = current_cred(); 316888e67f3bSDavid Howells 31692875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__GETATTR); 31701da177e4SLinus Torvalds } 31711da177e4SLinus Torvalds 31728f0cfa52SDavid Howells static int selinux_inode_removexattr(struct dentry *dentry, const char *name) 31731da177e4SLinus Torvalds { 31746b240306SEric W. Biederman if (strcmp(name, XATTR_NAME_SELINUX)) { 31756b240306SEric W. Biederman int rc = cap_inode_removexattr(dentry, name); 31766b240306SEric W. Biederman if (rc) 31776b240306SEric W. Biederman return rc; 31786b240306SEric W. Biederman 31796b240306SEric W. Biederman /* Not an attribute we recognize, so just check the 31806b240306SEric W. Biederman ordinary setattr permission. */ 31816b240306SEric W. Biederman return dentry_has_perm(current_cred(), dentry, FILE__SETATTR); 31826b240306SEric W. Biederman } 31831da177e4SLinus Torvalds 31841da177e4SLinus Torvalds /* No one is allowed to remove a SELinux security label. 31851da177e4SLinus Torvalds You can change the label, but all data must be labeled. */ 31861da177e4SLinus Torvalds return -EACCES; 31871da177e4SLinus Torvalds } 31881da177e4SLinus Torvalds 3189d381d8a9SJames Morris /* 3190abc69bb6SStephen Smalley * Copy the inode security context value to the user. 3191d381d8a9SJames Morris * 3192d381d8a9SJames Morris * Permission check is handled by selinux_inode_getxattr hook. 3193d381d8a9SJames Morris */ 3194ea861dfdSAndreas Gruenbacher static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc) 31951da177e4SLinus Torvalds { 319642492594SDavid P. Quigley u32 size; 319742492594SDavid P. Quigley int error; 319842492594SDavid P. Quigley char *context = NULL; 319920cdef8dSPaul Moore struct inode_security_struct *isec; 32001da177e4SLinus Torvalds 32018c8570fbSDustin Kirkland if (strcmp(name, XATTR_SELINUX_SUFFIX)) 32028c8570fbSDustin Kirkland return -EOPNOTSUPP; 32031da177e4SLinus Torvalds 3204abc69bb6SStephen Smalley /* 3205abc69bb6SStephen Smalley * If the caller has CAP_MAC_ADMIN, then get the raw context 3206abc69bb6SStephen Smalley * value even if it is not defined by current policy; otherwise, 3207abc69bb6SStephen Smalley * use the in-core value under current policy. 3208abc69bb6SStephen Smalley * Use the non-auditing forms of the permission checks since 3209abc69bb6SStephen Smalley * getxattr may be called by unprivileged processes commonly 3210abc69bb6SStephen Smalley * and lack of permission just means that we fall back to the 3211abc69bb6SStephen Smalley * in-core context value, not a denial. 3212abc69bb6SStephen Smalley */ 321320cdef8dSPaul Moore isec = inode_security(inode); 3214db59000aSStephen Smalley if (has_cap_mac_admin(false)) 3215aa8e712cSStephen Smalley error = security_sid_to_context_force(&selinux_state, 3216aa8e712cSStephen Smalley isec->sid, &context, 3217abc69bb6SStephen Smalley &size); 3218abc69bb6SStephen Smalley else 3219aa8e712cSStephen Smalley error = security_sid_to_context(&selinux_state, isec->sid, 3220aa8e712cSStephen Smalley &context, &size); 322142492594SDavid P. Quigley if (error) 322242492594SDavid P. Quigley return error; 322342492594SDavid P. Quigley error = size; 322442492594SDavid P. Quigley if (alloc) { 322542492594SDavid P. Quigley *buffer = context; 322642492594SDavid P. Quigley goto out_nofree; 322742492594SDavid P. Quigley } 322842492594SDavid P. Quigley kfree(context); 322942492594SDavid P. Quigley out_nofree: 323042492594SDavid P. Quigley return error; 32311da177e4SLinus Torvalds } 32321da177e4SLinus Torvalds 32331da177e4SLinus Torvalds static int selinux_inode_setsecurity(struct inode *inode, const char *name, 32341da177e4SLinus Torvalds const void *value, size_t size, int flags) 32351da177e4SLinus Torvalds { 32362c97165bSPaul Moore struct inode_security_struct *isec = inode_security_novalidate(inode); 32371da177e4SLinus Torvalds u32 newsid; 32381da177e4SLinus Torvalds int rc; 32391da177e4SLinus Torvalds 32401da177e4SLinus Torvalds if (strcmp(name, XATTR_SELINUX_SUFFIX)) 32411da177e4SLinus Torvalds return -EOPNOTSUPP; 32421da177e4SLinus Torvalds 32431da177e4SLinus Torvalds if (!value || !size) 32441da177e4SLinus Torvalds return -EACCES; 32451da177e4SLinus Torvalds 3246aa8e712cSStephen Smalley rc = security_context_to_sid(&selinux_state, value, size, &newsid, 3247aa8e712cSStephen Smalley GFP_KERNEL); 32481da177e4SLinus Torvalds if (rc) 32491da177e4SLinus Torvalds return rc; 32501da177e4SLinus Torvalds 32519287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 3252aa9c2669SDavid Quigley isec->sclass = inode_mode_to_security_class(inode->i_mode); 32531da177e4SLinus Torvalds isec->sid = newsid; 32546f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 32559287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 32561da177e4SLinus Torvalds return 0; 32571da177e4SLinus Torvalds } 32581da177e4SLinus Torvalds 32591da177e4SLinus Torvalds static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size) 32601da177e4SLinus Torvalds { 32611da177e4SLinus Torvalds const int len = sizeof(XATTR_NAME_SELINUX); 32621da177e4SLinus Torvalds if (buffer && len <= buffer_size) 32631da177e4SLinus Torvalds memcpy(buffer, XATTR_NAME_SELINUX, len); 32641da177e4SLinus Torvalds return len; 32651da177e4SLinus Torvalds } 32661da177e4SLinus Torvalds 3267d6335d77SAndreas Gruenbacher static void selinux_inode_getsecid(struct inode *inode, u32 *secid) 3268713a04aeSAhmed S. Darwish { 3269e817c2f3SAndreas Gruenbacher struct inode_security_struct *isec = inode_security_novalidate(inode); 3270713a04aeSAhmed S. Darwish *secid = isec->sid; 3271713a04aeSAhmed S. Darwish } 3272713a04aeSAhmed S. Darwish 327356909eb3SVivek Goyal static int selinux_inode_copy_up(struct dentry *src, struct cred **new) 327456909eb3SVivek Goyal { 327556909eb3SVivek Goyal u32 sid; 327656909eb3SVivek Goyal struct task_security_struct *tsec; 327756909eb3SVivek Goyal struct cred *new_creds = *new; 327856909eb3SVivek Goyal 327956909eb3SVivek Goyal if (new_creds == NULL) { 328056909eb3SVivek Goyal new_creds = prepare_creds(); 328156909eb3SVivek Goyal if (!new_creds) 328256909eb3SVivek Goyal return -ENOMEM; 328356909eb3SVivek Goyal } 328456909eb3SVivek Goyal 32850c6cfa62SCasey Schaufler tsec = selinux_cred(new_creds); 328656909eb3SVivek Goyal /* Get label from overlay inode and set it in create_sid */ 328756909eb3SVivek Goyal selinux_inode_getsecid(d_inode(src), &sid); 328856909eb3SVivek Goyal tsec->create_sid = sid; 328956909eb3SVivek Goyal *new = new_creds; 329056909eb3SVivek Goyal return 0; 329156909eb3SVivek Goyal } 329256909eb3SVivek Goyal 329319472b69SVivek Goyal static int selinux_inode_copy_up_xattr(const char *name) 329419472b69SVivek Goyal { 329519472b69SVivek Goyal /* The copy_up hook above sets the initial context on an inode, but we 329619472b69SVivek Goyal * don't then want to overwrite it by blindly copying all the lower 329719472b69SVivek Goyal * xattrs up. Instead, we have to filter out SELinux-related xattrs. 329819472b69SVivek Goyal */ 329919472b69SVivek Goyal if (strcmp(name, XATTR_NAME_SELINUX) == 0) 330019472b69SVivek Goyal return 1; /* Discard */ 330119472b69SVivek Goyal /* 330219472b69SVivek Goyal * Any other attribute apart from SELINUX is not claimed, supported 330319472b69SVivek Goyal * by selinux. 330419472b69SVivek Goyal */ 330519472b69SVivek Goyal return -EOPNOTSUPP; 330619472b69SVivek Goyal } 330719472b69SVivek Goyal 33081da177e4SLinus Torvalds /* file security operations */ 33091da177e4SLinus Torvalds 3310788e7dd4SYuichi Nakamura static int selinux_revalidate_file_permission(struct file *file, int mask) 33111da177e4SLinus Torvalds { 331288e67f3bSDavid Howells const struct cred *cred = current_cred(); 3313496ad9aaSAl Viro struct inode *inode = file_inode(file); 33141da177e4SLinus Torvalds 33151da177e4SLinus Torvalds /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */ 33161da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE)) 33171da177e4SLinus Torvalds mask |= MAY_APPEND; 33181da177e4SLinus Torvalds 3319389fb800SPaul Moore return file_has_perm(cred, file, 33201da177e4SLinus Torvalds file_mask_to_av(inode->i_mode, mask)); 33211da177e4SLinus Torvalds } 33221da177e4SLinus Torvalds 3323788e7dd4SYuichi Nakamura static int selinux_file_permission(struct file *file, int mask) 3324788e7dd4SYuichi Nakamura { 3325496ad9aaSAl Viro struct inode *inode = file_inode(file); 3326*bb6c6b02SCasey Schaufler struct file_security_struct *fsec = selinux_file(file); 3327b197367eSAndreas Gruenbacher struct inode_security_struct *isec; 332820dda18bSStephen Smalley u32 sid = current_sid(); 332920dda18bSStephen Smalley 3330389fb800SPaul Moore if (!mask) 3331788e7dd4SYuichi Nakamura /* No permission to check. Existence test. */ 3332788e7dd4SYuichi Nakamura return 0; 3333788e7dd4SYuichi Nakamura 3334b197367eSAndreas Gruenbacher isec = inode_security(inode); 333520dda18bSStephen Smalley if (sid == fsec->sid && fsec->isid == isec->sid && 33366b6bc620SStephen Smalley fsec->pseqno == avc_policy_seqno(&selinux_state)) 333783d49856SEric Paris /* No change since file_open check. */ 333820dda18bSStephen Smalley return 0; 333920dda18bSStephen Smalley 3340788e7dd4SYuichi Nakamura return selinux_revalidate_file_permission(file, mask); 3341788e7dd4SYuichi Nakamura } 3342788e7dd4SYuichi Nakamura 33431da177e4SLinus Torvalds static int selinux_file_alloc_security(struct file *file) 33441da177e4SLinus Torvalds { 33451da177e4SLinus Torvalds return file_alloc_security(file); 33461da177e4SLinus Torvalds } 33471da177e4SLinus Torvalds 33481da177e4SLinus Torvalds static void selinux_file_free_security(struct file *file) 33491da177e4SLinus Torvalds { 33501da177e4SLinus Torvalds file_free_security(file); 33511da177e4SLinus Torvalds } 33521da177e4SLinus Torvalds 3353fa1aa143SJeff Vander Stoep /* 3354fa1aa143SJeff Vander Stoep * Check whether a task has the ioctl permission and cmd 3355fa1aa143SJeff Vander Stoep * operation to an inode. 3356fa1aa143SJeff Vander Stoep */ 33571d2a168aSGeliang Tang static int ioctl_has_perm(const struct cred *cred, struct file *file, 3358fa1aa143SJeff Vander Stoep u32 requested, u16 cmd) 3359fa1aa143SJeff Vander Stoep { 3360fa1aa143SJeff Vander Stoep struct common_audit_data ad; 3361*bb6c6b02SCasey Schaufler struct file_security_struct *fsec = selinux_file(file); 3362fa1aa143SJeff Vander Stoep struct inode *inode = file_inode(file); 336320cdef8dSPaul Moore struct inode_security_struct *isec; 3364fa1aa143SJeff Vander Stoep struct lsm_ioctlop_audit ioctl; 3365fa1aa143SJeff Vander Stoep u32 ssid = cred_sid(cred); 3366fa1aa143SJeff Vander Stoep int rc; 3367fa1aa143SJeff Vander Stoep u8 driver = cmd >> 8; 3368fa1aa143SJeff Vander Stoep u8 xperm = cmd & 0xff; 3369fa1aa143SJeff Vander Stoep 3370fa1aa143SJeff Vander Stoep ad.type = LSM_AUDIT_DATA_IOCTL_OP; 3371fa1aa143SJeff Vander Stoep ad.u.op = &ioctl; 3372fa1aa143SJeff Vander Stoep ad.u.op->cmd = cmd; 3373fa1aa143SJeff Vander Stoep ad.u.op->path = file->f_path; 3374fa1aa143SJeff Vander Stoep 3375fa1aa143SJeff Vander Stoep if (ssid != fsec->sid) { 33766b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 33776b6bc620SStephen Smalley ssid, fsec->sid, 3378fa1aa143SJeff Vander Stoep SECCLASS_FD, 3379fa1aa143SJeff Vander Stoep FD__USE, 3380fa1aa143SJeff Vander Stoep &ad); 3381fa1aa143SJeff Vander Stoep if (rc) 3382fa1aa143SJeff Vander Stoep goto out; 3383fa1aa143SJeff Vander Stoep } 3384fa1aa143SJeff Vander Stoep 3385fa1aa143SJeff Vander Stoep if (unlikely(IS_PRIVATE(inode))) 3386fa1aa143SJeff Vander Stoep return 0; 3387fa1aa143SJeff Vander Stoep 338820cdef8dSPaul Moore isec = inode_security(inode); 33896b6bc620SStephen Smalley rc = avc_has_extended_perms(&selinux_state, 33906b6bc620SStephen Smalley ssid, isec->sid, isec->sclass, 3391fa1aa143SJeff Vander Stoep requested, driver, xperm, &ad); 3392fa1aa143SJeff Vander Stoep out: 3393fa1aa143SJeff Vander Stoep return rc; 3394fa1aa143SJeff Vander Stoep } 3395fa1aa143SJeff Vander Stoep 33961da177e4SLinus Torvalds static int selinux_file_ioctl(struct file *file, unsigned int cmd, 33971da177e4SLinus Torvalds unsigned long arg) 33981da177e4SLinus Torvalds { 339988e67f3bSDavid Howells const struct cred *cred = current_cred(); 34000b24dcb7SEric Paris int error = 0; 34011da177e4SLinus Torvalds 34020b24dcb7SEric Paris switch (cmd) { 34030b24dcb7SEric Paris case FIONREAD: 34040b24dcb7SEric Paris /* fall through */ 34050b24dcb7SEric Paris case FIBMAP: 34060b24dcb7SEric Paris /* fall through */ 34070b24dcb7SEric Paris case FIGETBSZ: 34080b24dcb7SEric Paris /* fall through */ 34092f99c369SAl Viro case FS_IOC_GETFLAGS: 34100b24dcb7SEric Paris /* fall through */ 34112f99c369SAl Viro case FS_IOC_GETVERSION: 34120b24dcb7SEric Paris error = file_has_perm(cred, file, FILE__GETATTR); 34130b24dcb7SEric Paris break; 34141da177e4SLinus Torvalds 34152f99c369SAl Viro case FS_IOC_SETFLAGS: 34160b24dcb7SEric Paris /* fall through */ 34172f99c369SAl Viro case FS_IOC_SETVERSION: 34180b24dcb7SEric Paris error = file_has_perm(cred, file, FILE__SETATTR); 34190b24dcb7SEric Paris break; 34200b24dcb7SEric Paris 34210b24dcb7SEric Paris /* sys_ioctl() checks */ 34220b24dcb7SEric Paris case FIONBIO: 34230b24dcb7SEric Paris /* fall through */ 34240b24dcb7SEric Paris case FIOASYNC: 34250b24dcb7SEric Paris error = file_has_perm(cred, file, 0); 34260b24dcb7SEric Paris break; 34270b24dcb7SEric Paris 34280b24dcb7SEric Paris case KDSKBENT: 34290b24dcb7SEric Paris case KDSKBSENT: 34306a9de491SEric Paris error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG, 34318e4ff6f2SStephen Smalley SECURITY_CAP_AUDIT, true); 34320b24dcb7SEric Paris break; 34330b24dcb7SEric Paris 34340b24dcb7SEric Paris /* default case assumes that the command will go 34350b24dcb7SEric Paris * to the file's ioctl() function. 34360b24dcb7SEric Paris */ 34370b24dcb7SEric Paris default: 3438fa1aa143SJeff Vander Stoep error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd); 34390b24dcb7SEric Paris } 34400b24dcb7SEric Paris return error; 34411da177e4SLinus Torvalds } 34421da177e4SLinus Torvalds 3443fcaaade1SStephen Smalley static int default_noexec; 3444fcaaade1SStephen Smalley 34451da177e4SLinus Torvalds static int file_map_prot_check(struct file *file, unsigned long prot, int shared) 34461da177e4SLinus Torvalds { 344788e67f3bSDavid Howells const struct cred *cred = current_cred(); 3448be0554c9SStephen Smalley u32 sid = cred_sid(cred); 3449d84f4f99SDavid Howells int rc = 0; 345088e67f3bSDavid Howells 3451fcaaade1SStephen Smalley if (default_noexec && 3452892e8cacSStephen Smalley (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) || 3453892e8cacSStephen Smalley (!shared && (prot & PROT_WRITE)))) { 34541da177e4SLinus Torvalds /* 34551da177e4SLinus Torvalds * We are making executable an anonymous mapping or a 34561da177e4SLinus Torvalds * private file mapping that will also be writable. 34571da177e4SLinus Torvalds * This has an additional check. 34581da177e4SLinus Torvalds */ 34596b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 34606b6bc620SStephen Smalley sid, sid, SECCLASS_PROCESS, 3461be0554c9SStephen Smalley PROCESS__EXECMEM, NULL); 34621da177e4SLinus Torvalds if (rc) 3463d84f4f99SDavid Howells goto error; 34641da177e4SLinus Torvalds } 34651da177e4SLinus Torvalds 34661da177e4SLinus Torvalds if (file) { 34671da177e4SLinus Torvalds /* read access is always possible with a mapping */ 34681da177e4SLinus Torvalds u32 av = FILE__READ; 34691da177e4SLinus Torvalds 34701da177e4SLinus Torvalds /* write access only matters if the mapping is shared */ 34711da177e4SLinus Torvalds if (shared && (prot & PROT_WRITE)) 34721da177e4SLinus Torvalds av |= FILE__WRITE; 34731da177e4SLinus Torvalds 34741da177e4SLinus Torvalds if (prot & PROT_EXEC) 34751da177e4SLinus Torvalds av |= FILE__EXECUTE; 34761da177e4SLinus Torvalds 347788e67f3bSDavid Howells return file_has_perm(cred, file, av); 34781da177e4SLinus Torvalds } 3479d84f4f99SDavid Howells 3480d84f4f99SDavid Howells error: 3481d84f4f99SDavid Howells return rc; 34821da177e4SLinus Torvalds } 34831da177e4SLinus Torvalds 3484e5467859SAl Viro static int selinux_mmap_addr(unsigned long addr) 34851da177e4SLinus Torvalds { 3486b1d9e6b0SCasey Schaufler int rc = 0; 348798883bfdSPaul Moore 348898883bfdSPaul Moore if (addr < CONFIG_LSM_MMAP_MIN_ADDR) { 348998883bfdSPaul Moore u32 sid = current_sid(); 34906b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 34916b6bc620SStephen Smalley sid, sid, SECCLASS_MEMPROTECT, 349298883bfdSPaul Moore MEMPROTECT__MMAP_ZERO, NULL); 349398883bfdSPaul Moore } 349498883bfdSPaul Moore 349598883bfdSPaul Moore return rc; 3496e5467859SAl Viro } 34971da177e4SLinus Torvalds 3498e5467859SAl Viro static int selinux_mmap_file(struct file *file, unsigned long reqprot, 3499e5467859SAl Viro unsigned long prot, unsigned long flags) 3500e5467859SAl Viro { 35013ba4bf5fSStephen Smalley struct common_audit_data ad; 35023ba4bf5fSStephen Smalley int rc; 35033ba4bf5fSStephen Smalley 35043ba4bf5fSStephen Smalley if (file) { 35053ba4bf5fSStephen Smalley ad.type = LSM_AUDIT_DATA_FILE; 35063ba4bf5fSStephen Smalley ad.u.file = file; 35073ba4bf5fSStephen Smalley rc = inode_has_perm(current_cred(), file_inode(file), 35083ba4bf5fSStephen Smalley FILE__MAP, &ad); 35093ba4bf5fSStephen Smalley if (rc) 35103ba4bf5fSStephen Smalley return rc; 35113ba4bf5fSStephen Smalley } 35123ba4bf5fSStephen Smalley 3513aa8e712cSStephen Smalley if (selinux_state.checkreqprot) 35141da177e4SLinus Torvalds prot = reqprot; 35151da177e4SLinus Torvalds 35161da177e4SLinus Torvalds return file_map_prot_check(file, prot, 35171da177e4SLinus Torvalds (flags & MAP_TYPE) == MAP_SHARED); 35181da177e4SLinus Torvalds } 35191da177e4SLinus Torvalds 35201da177e4SLinus Torvalds static int selinux_file_mprotect(struct vm_area_struct *vma, 35211da177e4SLinus Torvalds unsigned long reqprot, 35221da177e4SLinus Torvalds unsigned long prot) 35231da177e4SLinus Torvalds { 352488e67f3bSDavid Howells const struct cred *cred = current_cred(); 3525be0554c9SStephen Smalley u32 sid = cred_sid(cred); 35261da177e4SLinus Torvalds 3527aa8e712cSStephen Smalley if (selinux_state.checkreqprot) 35281da177e4SLinus Torvalds prot = reqprot; 35291da177e4SLinus Torvalds 3530fcaaade1SStephen Smalley if (default_noexec && 3531fcaaade1SStephen Smalley (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) { 3532d541bbeeSJames Morris int rc = 0; 3533db4c9641SStephen Smalley if (vma->vm_start >= vma->vm_mm->start_brk && 3534db4c9641SStephen Smalley vma->vm_end <= vma->vm_mm->brk) { 35356b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 35366b6bc620SStephen Smalley sid, sid, SECCLASS_PROCESS, 3537be0554c9SStephen Smalley PROCESS__EXECHEAP, NULL); 3538db4c9641SStephen Smalley } else if (!vma->vm_file && 3539c2316dbfSStephen Smalley ((vma->vm_start <= vma->vm_mm->start_stack && 3540c2316dbfSStephen Smalley vma->vm_end >= vma->vm_mm->start_stack) || 3541d17af505SAndy Lutomirski vma_is_stack_for_current(vma))) { 35426b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 35436b6bc620SStephen Smalley sid, sid, SECCLASS_PROCESS, 3544be0554c9SStephen Smalley PROCESS__EXECSTACK, NULL); 3545db4c9641SStephen Smalley } else if (vma->vm_file && vma->anon_vma) { 3546db4c9641SStephen Smalley /* 3547db4c9641SStephen Smalley * We are making executable a file mapping that has 3548db4c9641SStephen Smalley * had some COW done. Since pages might have been 3549db4c9641SStephen Smalley * written, check ability to execute the possibly 3550db4c9641SStephen Smalley * modified content. This typically should only 3551db4c9641SStephen Smalley * occur for text relocations. 3552db4c9641SStephen Smalley */ 3553d84f4f99SDavid Howells rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD); 3554db4c9641SStephen Smalley } 35556b992197SLorenzo Hernandez García-Hierro if (rc) 35566b992197SLorenzo Hernandez García-Hierro return rc; 35576b992197SLorenzo Hernandez García-Hierro } 35581da177e4SLinus Torvalds 35591da177e4SLinus Torvalds return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED); 35601da177e4SLinus Torvalds } 35611da177e4SLinus Torvalds 35621da177e4SLinus Torvalds static int selinux_file_lock(struct file *file, unsigned int cmd) 35631da177e4SLinus Torvalds { 356488e67f3bSDavid Howells const struct cred *cred = current_cred(); 356588e67f3bSDavid Howells 356688e67f3bSDavid Howells return file_has_perm(cred, file, FILE__LOCK); 35671da177e4SLinus Torvalds } 35681da177e4SLinus Torvalds 35691da177e4SLinus Torvalds static int selinux_file_fcntl(struct file *file, unsigned int cmd, 35701da177e4SLinus Torvalds unsigned long arg) 35711da177e4SLinus Torvalds { 357288e67f3bSDavid Howells const struct cred *cred = current_cred(); 35731da177e4SLinus Torvalds int err = 0; 35741da177e4SLinus Torvalds 35751da177e4SLinus Torvalds switch (cmd) { 35761da177e4SLinus Torvalds case F_SETFL: 35771da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) { 357888e67f3bSDavid Howells err = file_has_perm(cred, file, FILE__WRITE); 35791da177e4SLinus Torvalds break; 35801da177e4SLinus Torvalds } 35811da177e4SLinus Torvalds /* fall through */ 35821da177e4SLinus Torvalds case F_SETOWN: 35831da177e4SLinus Torvalds case F_SETSIG: 35841da177e4SLinus Torvalds case F_GETFL: 35851da177e4SLinus Torvalds case F_GETOWN: 35861da177e4SLinus Torvalds case F_GETSIG: 35871d151c33SCyrill Gorcunov case F_GETOWNER_UIDS: 35881da177e4SLinus Torvalds /* Just check FD__USE permission */ 358988e67f3bSDavid Howells err = file_has_perm(cred, file, 0); 35901da177e4SLinus Torvalds break; 35911da177e4SLinus Torvalds case F_GETLK: 35921da177e4SLinus Torvalds case F_SETLK: 35931da177e4SLinus Torvalds case F_SETLKW: 35940d3f7a2dSJeff Layton case F_OFD_GETLK: 35950d3f7a2dSJeff Layton case F_OFD_SETLK: 35960d3f7a2dSJeff Layton case F_OFD_SETLKW: 35971da177e4SLinus Torvalds #if BITS_PER_LONG == 32 35981da177e4SLinus Torvalds case F_GETLK64: 35991da177e4SLinus Torvalds case F_SETLK64: 36001da177e4SLinus Torvalds case F_SETLKW64: 36011da177e4SLinus Torvalds #endif 360288e67f3bSDavid Howells err = file_has_perm(cred, file, FILE__LOCK); 36031da177e4SLinus Torvalds break; 36041da177e4SLinus Torvalds } 36051da177e4SLinus Torvalds 36061da177e4SLinus Torvalds return err; 36071da177e4SLinus Torvalds } 36081da177e4SLinus Torvalds 3609e0b93eddSJeff Layton static void selinux_file_set_fowner(struct file *file) 36101da177e4SLinus Torvalds { 36111da177e4SLinus Torvalds struct file_security_struct *fsec; 36121da177e4SLinus Torvalds 3613*bb6c6b02SCasey Schaufler fsec = selinux_file(file); 3614275bb41eSDavid Howells fsec->fown_sid = current_sid(); 36151da177e4SLinus Torvalds } 36161da177e4SLinus Torvalds 36171da177e4SLinus Torvalds static int selinux_file_send_sigiotask(struct task_struct *tsk, 36181da177e4SLinus Torvalds struct fown_struct *fown, int signum) 36191da177e4SLinus Torvalds { 36201da177e4SLinus Torvalds struct file *file; 362165c90bcaSStephen Smalley u32 sid = task_sid(tsk); 36221da177e4SLinus Torvalds u32 perm; 36231da177e4SLinus Torvalds struct file_security_struct *fsec; 36241da177e4SLinus Torvalds 36251da177e4SLinus Torvalds /* struct fown_struct is never outside the context of a struct file */ 3626b385a144SRobert P. J. Day file = container_of(fown, struct file, f_owner); 36271da177e4SLinus Torvalds 3628*bb6c6b02SCasey Schaufler fsec = selinux_file(file); 36291da177e4SLinus Torvalds 36301da177e4SLinus Torvalds if (!signum) 36311da177e4SLinus Torvalds perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */ 36321da177e4SLinus Torvalds else 36331da177e4SLinus Torvalds perm = signal_to_av(signum); 36341da177e4SLinus Torvalds 36356b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 36366b6bc620SStephen Smalley fsec->fown_sid, sid, 36371da177e4SLinus Torvalds SECCLASS_PROCESS, perm, NULL); 36381da177e4SLinus Torvalds } 36391da177e4SLinus Torvalds 36401da177e4SLinus Torvalds static int selinux_file_receive(struct file *file) 36411da177e4SLinus Torvalds { 364288e67f3bSDavid Howells const struct cred *cred = current_cred(); 364388e67f3bSDavid Howells 364488e67f3bSDavid Howells return file_has_perm(cred, file, file_to_av(file)); 36451da177e4SLinus Torvalds } 36461da177e4SLinus Torvalds 364794817692SAl Viro static int selinux_file_open(struct file *file) 3648788e7dd4SYuichi Nakamura { 3649788e7dd4SYuichi Nakamura struct file_security_struct *fsec; 3650788e7dd4SYuichi Nakamura struct inode_security_struct *isec; 3651d84f4f99SDavid Howells 3652*bb6c6b02SCasey Schaufler fsec = selinux_file(file); 365383da53c5SAndreas Gruenbacher isec = inode_security(file_inode(file)); 3654788e7dd4SYuichi Nakamura /* 3655788e7dd4SYuichi Nakamura * Save inode label and policy sequence number 3656788e7dd4SYuichi Nakamura * at open-time so that selinux_file_permission 3657788e7dd4SYuichi Nakamura * can determine whether revalidation is necessary. 3658788e7dd4SYuichi Nakamura * Task label is already saved in the file security 3659788e7dd4SYuichi Nakamura * struct as its SID. 3660788e7dd4SYuichi Nakamura */ 3661788e7dd4SYuichi Nakamura fsec->isid = isec->sid; 36626b6bc620SStephen Smalley fsec->pseqno = avc_policy_seqno(&selinux_state); 3663788e7dd4SYuichi Nakamura /* 3664788e7dd4SYuichi Nakamura * Since the inode label or policy seqno may have changed 3665788e7dd4SYuichi Nakamura * between the selinux_inode_permission check and the saving 3666788e7dd4SYuichi Nakamura * of state above, recheck that access is still permitted. 3667788e7dd4SYuichi Nakamura * Otherwise, access might never be revalidated against the 3668788e7dd4SYuichi Nakamura * new inode label or new policy. 3669788e7dd4SYuichi Nakamura * This check is not redundant - do not remove. 3670788e7dd4SYuichi Nakamura */ 367194817692SAl Viro return file_path_has_perm(file->f_cred, file, open_file_to_av(file)); 3672788e7dd4SYuichi Nakamura } 3673788e7dd4SYuichi Nakamura 36741da177e4SLinus Torvalds /* task security operations */ 36751da177e4SLinus Torvalds 3676a79be238STetsuo Handa static int selinux_task_alloc(struct task_struct *task, 3677a79be238STetsuo Handa unsigned long clone_flags) 36781da177e4SLinus Torvalds { 3679be0554c9SStephen Smalley u32 sid = current_sid(); 3680be0554c9SStephen Smalley 36816b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 36826b6bc620SStephen Smalley sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL); 36831da177e4SLinus Torvalds } 36841da177e4SLinus Torvalds 3685f1752eecSDavid Howells /* 3686d84f4f99SDavid Howells * prepare a new set of credentials for modification 3687d84f4f99SDavid Howells */ 3688d84f4f99SDavid Howells static int selinux_cred_prepare(struct cred *new, const struct cred *old, 3689d84f4f99SDavid Howells gfp_t gfp) 3690d84f4f99SDavid Howells { 3691bbd3662aSCasey Schaufler const struct task_security_struct *old_tsec = selinux_cred(old); 3692bbd3662aSCasey Schaufler struct task_security_struct *tsec = selinux_cred(new); 3693d84f4f99SDavid Howells 3694bbd3662aSCasey Schaufler *tsec = *old_tsec; 3695d84f4f99SDavid Howells return 0; 3696d84f4f99SDavid Howells } 3697d84f4f99SDavid Howells 3698d84f4f99SDavid Howells /* 3699ee18d64cSDavid Howells * transfer the SELinux data to a blank set of creds 3700ee18d64cSDavid Howells */ 3701ee18d64cSDavid Howells static void selinux_cred_transfer(struct cred *new, const struct cred *old) 3702ee18d64cSDavid Howells { 37030c6cfa62SCasey Schaufler const struct task_security_struct *old_tsec = selinux_cred(old); 37040c6cfa62SCasey Schaufler struct task_security_struct *tsec = selinux_cred(new); 3705ee18d64cSDavid Howells 3706ee18d64cSDavid Howells *tsec = *old_tsec; 3707ee18d64cSDavid Howells } 3708ee18d64cSDavid Howells 37093ec30113SMatthew Garrett static void selinux_cred_getsecid(const struct cred *c, u32 *secid) 37103ec30113SMatthew Garrett { 37113ec30113SMatthew Garrett *secid = cred_sid(c); 37123ec30113SMatthew Garrett } 37133ec30113SMatthew Garrett 3714ee18d64cSDavid Howells /* 37153a3b7ce9SDavid Howells * set the security data for a kernel service 37163a3b7ce9SDavid Howells * - all the creation contexts are set to unlabelled 37173a3b7ce9SDavid Howells */ 37183a3b7ce9SDavid Howells static int selinux_kernel_act_as(struct cred *new, u32 secid) 37193a3b7ce9SDavid Howells { 37200c6cfa62SCasey Schaufler struct task_security_struct *tsec = selinux_cred(new); 37213a3b7ce9SDavid Howells u32 sid = current_sid(); 37223a3b7ce9SDavid Howells int ret; 37233a3b7ce9SDavid Howells 37246b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 37256b6bc620SStephen Smalley sid, secid, 37263a3b7ce9SDavid Howells SECCLASS_KERNEL_SERVICE, 37273a3b7ce9SDavid Howells KERNEL_SERVICE__USE_AS_OVERRIDE, 37283a3b7ce9SDavid Howells NULL); 37293a3b7ce9SDavid Howells if (ret == 0) { 37303a3b7ce9SDavid Howells tsec->sid = secid; 37313a3b7ce9SDavid Howells tsec->create_sid = 0; 37323a3b7ce9SDavid Howells tsec->keycreate_sid = 0; 37333a3b7ce9SDavid Howells tsec->sockcreate_sid = 0; 37343a3b7ce9SDavid Howells } 37353a3b7ce9SDavid Howells return ret; 37363a3b7ce9SDavid Howells } 37373a3b7ce9SDavid Howells 37383a3b7ce9SDavid Howells /* 37393a3b7ce9SDavid Howells * set the file creation context in a security record to the same as the 37403a3b7ce9SDavid Howells * objective context of the specified inode 37413a3b7ce9SDavid Howells */ 37423a3b7ce9SDavid Howells static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode) 37433a3b7ce9SDavid Howells { 374483da53c5SAndreas Gruenbacher struct inode_security_struct *isec = inode_security(inode); 37450c6cfa62SCasey Schaufler struct task_security_struct *tsec = selinux_cred(new); 37463a3b7ce9SDavid Howells u32 sid = current_sid(); 37473a3b7ce9SDavid Howells int ret; 37483a3b7ce9SDavid Howells 37496b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 37506b6bc620SStephen Smalley sid, isec->sid, 37513a3b7ce9SDavid Howells SECCLASS_KERNEL_SERVICE, 37523a3b7ce9SDavid Howells KERNEL_SERVICE__CREATE_FILES_AS, 37533a3b7ce9SDavid Howells NULL); 37543a3b7ce9SDavid Howells 37553a3b7ce9SDavid Howells if (ret == 0) 37563a3b7ce9SDavid Howells tsec->create_sid = isec->sid; 3757ef57471aSDavid Howells return ret; 37583a3b7ce9SDavid Howells } 37593a3b7ce9SDavid Howells 3760dd8dbf2eSEric Paris static int selinux_kernel_module_request(char *kmod_name) 376125354c4fSEric Paris { 3762dd8dbf2eSEric Paris struct common_audit_data ad; 3763dd8dbf2eSEric Paris 376450c205f5SEric Paris ad.type = LSM_AUDIT_DATA_KMOD; 3765dd8dbf2eSEric Paris ad.u.kmod_name = kmod_name; 3766dd8dbf2eSEric Paris 37676b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 37686b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM, 3769dd8dbf2eSEric Paris SYSTEM__MODULE_REQUEST, &ad); 377025354c4fSEric Paris } 377125354c4fSEric Paris 377261d612eaSJeff Vander Stoep static int selinux_kernel_module_from_file(struct file *file) 377361d612eaSJeff Vander Stoep { 377461d612eaSJeff Vander Stoep struct common_audit_data ad; 377561d612eaSJeff Vander Stoep struct inode_security_struct *isec; 377661d612eaSJeff Vander Stoep struct file_security_struct *fsec; 377761d612eaSJeff Vander Stoep u32 sid = current_sid(); 377861d612eaSJeff Vander Stoep int rc; 377961d612eaSJeff Vander Stoep 378061d612eaSJeff Vander Stoep /* init_module */ 378161d612eaSJeff Vander Stoep if (file == NULL) 37826b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 37836b6bc620SStephen Smalley sid, sid, SECCLASS_SYSTEM, 378461d612eaSJeff Vander Stoep SYSTEM__MODULE_LOAD, NULL); 378561d612eaSJeff Vander Stoep 378661d612eaSJeff Vander Stoep /* finit_module */ 378720cdef8dSPaul Moore 378843af5de7SVivek Goyal ad.type = LSM_AUDIT_DATA_FILE; 378943af5de7SVivek Goyal ad.u.file = file; 379061d612eaSJeff Vander Stoep 3791*bb6c6b02SCasey Schaufler fsec = selinux_file(file); 379261d612eaSJeff Vander Stoep if (sid != fsec->sid) { 37936b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 37946b6bc620SStephen Smalley sid, fsec->sid, SECCLASS_FD, FD__USE, &ad); 379561d612eaSJeff Vander Stoep if (rc) 379661d612eaSJeff Vander Stoep return rc; 379761d612eaSJeff Vander Stoep } 379861d612eaSJeff Vander Stoep 379920cdef8dSPaul Moore isec = inode_security(file_inode(file)); 38006b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 38016b6bc620SStephen Smalley sid, isec->sid, SECCLASS_SYSTEM, 380261d612eaSJeff Vander Stoep SYSTEM__MODULE_LOAD, &ad); 380361d612eaSJeff Vander Stoep } 380461d612eaSJeff Vander Stoep 380561d612eaSJeff Vander Stoep static int selinux_kernel_read_file(struct file *file, 380661d612eaSJeff Vander Stoep enum kernel_read_file_id id) 380761d612eaSJeff Vander Stoep { 380861d612eaSJeff Vander Stoep int rc = 0; 380961d612eaSJeff Vander Stoep 381061d612eaSJeff Vander Stoep switch (id) { 381161d612eaSJeff Vander Stoep case READING_MODULE: 381261d612eaSJeff Vander Stoep rc = selinux_kernel_module_from_file(file); 381361d612eaSJeff Vander Stoep break; 381461d612eaSJeff Vander Stoep default: 381561d612eaSJeff Vander Stoep break; 381661d612eaSJeff Vander Stoep } 381761d612eaSJeff Vander Stoep 381861d612eaSJeff Vander Stoep return rc; 381961d612eaSJeff Vander Stoep } 382061d612eaSJeff Vander Stoep 3821c77b8cdfSMimi Zohar static int selinux_kernel_load_data(enum kernel_load_data_id id) 3822c77b8cdfSMimi Zohar { 3823c77b8cdfSMimi Zohar int rc = 0; 3824c77b8cdfSMimi Zohar 3825c77b8cdfSMimi Zohar switch (id) { 3826c77b8cdfSMimi Zohar case LOADING_MODULE: 3827c77b8cdfSMimi Zohar rc = selinux_kernel_module_from_file(NULL); 3828c77b8cdfSMimi Zohar default: 3829c77b8cdfSMimi Zohar break; 3830c77b8cdfSMimi Zohar } 3831c77b8cdfSMimi Zohar 3832c77b8cdfSMimi Zohar return rc; 3833c77b8cdfSMimi Zohar } 3834c77b8cdfSMimi Zohar 38351da177e4SLinus Torvalds static int selinux_task_setpgid(struct task_struct *p, pid_t pgid) 38361da177e4SLinus Torvalds { 38376b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 38386b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 3839be0554c9SStephen Smalley PROCESS__SETPGID, NULL); 38401da177e4SLinus Torvalds } 38411da177e4SLinus Torvalds 38421da177e4SLinus Torvalds static int selinux_task_getpgid(struct task_struct *p) 38431da177e4SLinus Torvalds { 38446b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 38456b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 3846be0554c9SStephen Smalley PROCESS__GETPGID, NULL); 38471da177e4SLinus Torvalds } 38481da177e4SLinus Torvalds 38491da177e4SLinus Torvalds static int selinux_task_getsid(struct task_struct *p) 38501da177e4SLinus Torvalds { 38516b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 38526b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 3853be0554c9SStephen Smalley PROCESS__GETSESSION, NULL); 38541da177e4SLinus Torvalds } 38551da177e4SLinus Torvalds 3856f9008e4cSDavid Quigley static void selinux_task_getsecid(struct task_struct *p, u32 *secid) 3857f9008e4cSDavid Quigley { 3858275bb41eSDavid Howells *secid = task_sid(p); 3859f9008e4cSDavid Quigley } 3860f9008e4cSDavid Quigley 38611da177e4SLinus Torvalds static int selinux_task_setnice(struct task_struct *p, int nice) 38621da177e4SLinus Torvalds { 38636b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 38646b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 3865be0554c9SStephen Smalley PROCESS__SETSCHED, NULL); 38661da177e4SLinus Torvalds } 38671da177e4SLinus Torvalds 386803e68060SJames Morris static int selinux_task_setioprio(struct task_struct *p, int ioprio) 386903e68060SJames Morris { 38706b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 38716b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 3872be0554c9SStephen Smalley PROCESS__SETSCHED, NULL); 387303e68060SJames Morris } 387403e68060SJames Morris 3875a1836a42SDavid Quigley static int selinux_task_getioprio(struct task_struct *p) 3876a1836a42SDavid Quigley { 38776b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 38786b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 3879be0554c9SStephen Smalley PROCESS__GETSCHED, NULL); 3880a1836a42SDavid Quigley } 3881a1836a42SDavid Quigley 38824298555dSCorentin LABBE static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred, 3883791ec491SStephen Smalley unsigned int flags) 3884791ec491SStephen Smalley { 3885791ec491SStephen Smalley u32 av = 0; 3886791ec491SStephen Smalley 388784e6885eSStephen Smalley if (!flags) 388884e6885eSStephen Smalley return 0; 3889791ec491SStephen Smalley if (flags & LSM_PRLIMIT_WRITE) 3890791ec491SStephen Smalley av |= PROCESS__SETRLIMIT; 3891791ec491SStephen Smalley if (flags & LSM_PRLIMIT_READ) 3892791ec491SStephen Smalley av |= PROCESS__GETRLIMIT; 38936b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 38946b6bc620SStephen Smalley cred_sid(cred), cred_sid(tcred), 3895791ec491SStephen Smalley SECCLASS_PROCESS, av, NULL); 3896791ec491SStephen Smalley } 3897791ec491SStephen Smalley 38988fd00b4dSJiri Slaby static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource, 38998fd00b4dSJiri Slaby struct rlimit *new_rlim) 39001da177e4SLinus Torvalds { 39018fd00b4dSJiri Slaby struct rlimit *old_rlim = p->signal->rlim + resource; 39021da177e4SLinus Torvalds 39031da177e4SLinus Torvalds /* Control the ability to change the hard limit (whether 39041da177e4SLinus Torvalds lowering or raising it), so that the hard limit can 39051da177e4SLinus Torvalds later be used as a safe reset point for the soft limit 3906d84f4f99SDavid Howells upon context transitions. See selinux_bprm_committing_creds. */ 39071da177e4SLinus Torvalds if (old_rlim->rlim_max != new_rlim->rlim_max) 39086b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 39096b6bc620SStephen Smalley current_sid(), task_sid(p), 3910be0554c9SStephen Smalley SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL); 39111da177e4SLinus Torvalds 39121da177e4SLinus Torvalds return 0; 39131da177e4SLinus Torvalds } 39141da177e4SLinus Torvalds 3915b0ae1981SKOSAKI Motohiro static int selinux_task_setscheduler(struct task_struct *p) 39161da177e4SLinus Torvalds { 39176b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 39186b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 3919be0554c9SStephen Smalley PROCESS__SETSCHED, NULL); 39201da177e4SLinus Torvalds } 39211da177e4SLinus Torvalds 39221da177e4SLinus Torvalds static int selinux_task_getscheduler(struct task_struct *p) 39231da177e4SLinus Torvalds { 39246b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 39256b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 3926be0554c9SStephen Smalley PROCESS__GETSCHED, NULL); 39271da177e4SLinus Torvalds } 39281da177e4SLinus Torvalds 392935601547SDavid Quigley static int selinux_task_movememory(struct task_struct *p) 393035601547SDavid Quigley { 39316b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 39326b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 3933be0554c9SStephen Smalley PROCESS__SETSCHED, NULL); 393435601547SDavid Quigley } 393535601547SDavid Quigley 3936ae7795bcSEric W. Biederman static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info, 39376b4f3d01SStephen Smalley int sig, const struct cred *cred) 39381da177e4SLinus Torvalds { 39396b4f3d01SStephen Smalley u32 secid; 39401da177e4SLinus Torvalds u32 perm; 39411da177e4SLinus Torvalds 39421da177e4SLinus Torvalds if (!sig) 39431da177e4SLinus Torvalds perm = PROCESS__SIGNULL; /* null signal; existence test */ 39441da177e4SLinus Torvalds else 39451da177e4SLinus Torvalds perm = signal_to_av(sig); 39466b4f3d01SStephen Smalley if (!cred) 3947be0554c9SStephen Smalley secid = current_sid(); 39486b4f3d01SStephen Smalley else 39496b4f3d01SStephen Smalley secid = cred_sid(cred); 39506b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 39516b6bc620SStephen Smalley secid, task_sid(p), SECCLASS_PROCESS, perm, NULL); 39521da177e4SLinus Torvalds } 39531da177e4SLinus Torvalds 39541da177e4SLinus Torvalds static void selinux_task_to_inode(struct task_struct *p, 39551da177e4SLinus Torvalds struct inode *inode) 39561da177e4SLinus Torvalds { 39571da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 3958275bb41eSDavid Howells u32 sid = task_sid(p); 39591da177e4SLinus Torvalds 39609287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 3961db978da8SAndreas Gruenbacher isec->sclass = inode_mode_to_security_class(inode->i_mode); 3962275bb41eSDavid Howells isec->sid = sid; 39636f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 39649287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 39651da177e4SLinus Torvalds } 39661da177e4SLinus Torvalds 39671da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 396867f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv4(struct sk_buff *skb, 39692bf49690SThomas Liu struct common_audit_data *ad, u8 *proto) 39701da177e4SLinus Torvalds { 39711da177e4SLinus Torvalds int offset, ihlen, ret = -EINVAL; 39721da177e4SLinus Torvalds struct iphdr _iph, *ih; 39731da177e4SLinus Torvalds 3974bbe735e4SArnaldo Carvalho de Melo offset = skb_network_offset(skb); 39751da177e4SLinus Torvalds ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph); 39761da177e4SLinus Torvalds if (ih == NULL) 39771da177e4SLinus Torvalds goto out; 39781da177e4SLinus Torvalds 39791da177e4SLinus Torvalds ihlen = ih->ihl * 4; 39801da177e4SLinus Torvalds if (ihlen < sizeof(_iph)) 39811da177e4SLinus Torvalds goto out; 39821da177e4SLinus Torvalds 398348c62af6SEric Paris ad->u.net->v4info.saddr = ih->saddr; 398448c62af6SEric Paris ad->u.net->v4info.daddr = ih->daddr; 39851da177e4SLinus Torvalds ret = 0; 39861da177e4SLinus Torvalds 398767f83cbfSVenkat Yekkirala if (proto) 398867f83cbfSVenkat Yekkirala *proto = ih->protocol; 398967f83cbfSVenkat Yekkirala 39901da177e4SLinus Torvalds switch (ih->protocol) { 39911da177e4SLinus Torvalds case IPPROTO_TCP: { 39921da177e4SLinus Torvalds struct tcphdr _tcph, *th; 39931da177e4SLinus Torvalds 39941da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 39951da177e4SLinus Torvalds break; 39961da177e4SLinus Torvalds 39971da177e4SLinus Torvalds offset += ihlen; 39981da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 39991da177e4SLinus Torvalds if (th == NULL) 40001da177e4SLinus Torvalds break; 40011da177e4SLinus Torvalds 400248c62af6SEric Paris ad->u.net->sport = th->source; 400348c62af6SEric Paris ad->u.net->dport = th->dest; 40041da177e4SLinus Torvalds break; 40051da177e4SLinus Torvalds } 40061da177e4SLinus Torvalds 40071da177e4SLinus Torvalds case IPPROTO_UDP: { 40081da177e4SLinus Torvalds struct udphdr _udph, *uh; 40091da177e4SLinus Torvalds 40101da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 40111da177e4SLinus Torvalds break; 40121da177e4SLinus Torvalds 40131da177e4SLinus Torvalds offset += ihlen; 40141da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 40151da177e4SLinus Torvalds if (uh == NULL) 40161da177e4SLinus Torvalds break; 40171da177e4SLinus Torvalds 401848c62af6SEric Paris ad->u.net->sport = uh->source; 401948c62af6SEric Paris ad->u.net->dport = uh->dest; 40201da177e4SLinus Torvalds break; 40211da177e4SLinus Torvalds } 40221da177e4SLinus Torvalds 40232ee92d46SJames Morris case IPPROTO_DCCP: { 40242ee92d46SJames Morris struct dccp_hdr _dccph, *dh; 40252ee92d46SJames Morris 40262ee92d46SJames Morris if (ntohs(ih->frag_off) & IP_OFFSET) 40272ee92d46SJames Morris break; 40282ee92d46SJames Morris 40292ee92d46SJames Morris offset += ihlen; 40302ee92d46SJames Morris dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); 40312ee92d46SJames Morris if (dh == NULL) 40322ee92d46SJames Morris break; 40332ee92d46SJames Morris 403448c62af6SEric Paris ad->u.net->sport = dh->dccph_sport; 403548c62af6SEric Paris ad->u.net->dport = dh->dccph_dport; 40362ee92d46SJames Morris break; 40372ee92d46SJames Morris } 40382ee92d46SJames Morris 4039d452930fSRichard Haines #if IS_ENABLED(CONFIG_IP_SCTP) 4040d452930fSRichard Haines case IPPROTO_SCTP: { 4041d452930fSRichard Haines struct sctphdr _sctph, *sh; 4042d452930fSRichard Haines 4043d452930fSRichard Haines if (ntohs(ih->frag_off) & IP_OFFSET) 4044d452930fSRichard Haines break; 4045d452930fSRichard Haines 4046d452930fSRichard Haines offset += ihlen; 4047d452930fSRichard Haines sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph); 4048d452930fSRichard Haines if (sh == NULL) 4049d452930fSRichard Haines break; 4050d452930fSRichard Haines 4051d452930fSRichard Haines ad->u.net->sport = sh->source; 4052d452930fSRichard Haines ad->u.net->dport = sh->dest; 4053d452930fSRichard Haines break; 4054d452930fSRichard Haines } 4055d452930fSRichard Haines #endif 40561da177e4SLinus Torvalds default: 40571da177e4SLinus Torvalds break; 40581da177e4SLinus Torvalds } 40591da177e4SLinus Torvalds out: 40601da177e4SLinus Torvalds return ret; 40611da177e4SLinus Torvalds } 40621da177e4SLinus Torvalds 40631a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 40641da177e4SLinus Torvalds 40651da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 406667f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv6(struct sk_buff *skb, 40672bf49690SThomas Liu struct common_audit_data *ad, u8 *proto) 40681da177e4SLinus Torvalds { 40691da177e4SLinus Torvalds u8 nexthdr; 40701da177e4SLinus Torvalds int ret = -EINVAL, offset; 40711da177e4SLinus Torvalds struct ipv6hdr _ipv6h, *ip6; 407275f2811cSJesse Gross __be16 frag_off; 40731da177e4SLinus Torvalds 4074bbe735e4SArnaldo Carvalho de Melo offset = skb_network_offset(skb); 40751da177e4SLinus Torvalds ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h); 40761da177e4SLinus Torvalds if (ip6 == NULL) 40771da177e4SLinus Torvalds goto out; 40781da177e4SLinus Torvalds 407948c62af6SEric Paris ad->u.net->v6info.saddr = ip6->saddr; 408048c62af6SEric Paris ad->u.net->v6info.daddr = ip6->daddr; 40811da177e4SLinus Torvalds ret = 0; 40821da177e4SLinus Torvalds 40831da177e4SLinus Torvalds nexthdr = ip6->nexthdr; 40841da177e4SLinus Torvalds offset += sizeof(_ipv6h); 408575f2811cSJesse Gross offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off); 40861da177e4SLinus Torvalds if (offset < 0) 40871da177e4SLinus Torvalds goto out; 40881da177e4SLinus Torvalds 408967f83cbfSVenkat Yekkirala if (proto) 409067f83cbfSVenkat Yekkirala *proto = nexthdr; 409167f83cbfSVenkat Yekkirala 40921da177e4SLinus Torvalds switch (nexthdr) { 40931da177e4SLinus Torvalds case IPPROTO_TCP: { 40941da177e4SLinus Torvalds struct tcphdr _tcph, *th; 40951da177e4SLinus Torvalds 40961da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 40971da177e4SLinus Torvalds if (th == NULL) 40981da177e4SLinus Torvalds break; 40991da177e4SLinus Torvalds 410048c62af6SEric Paris ad->u.net->sport = th->source; 410148c62af6SEric Paris ad->u.net->dport = th->dest; 41021da177e4SLinus Torvalds break; 41031da177e4SLinus Torvalds } 41041da177e4SLinus Torvalds 41051da177e4SLinus Torvalds case IPPROTO_UDP: { 41061da177e4SLinus Torvalds struct udphdr _udph, *uh; 41071da177e4SLinus Torvalds 41081da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 41091da177e4SLinus Torvalds if (uh == NULL) 41101da177e4SLinus Torvalds break; 41111da177e4SLinus Torvalds 411248c62af6SEric Paris ad->u.net->sport = uh->source; 411348c62af6SEric Paris ad->u.net->dport = uh->dest; 41141da177e4SLinus Torvalds break; 41151da177e4SLinus Torvalds } 41161da177e4SLinus Torvalds 41172ee92d46SJames Morris case IPPROTO_DCCP: { 41182ee92d46SJames Morris struct dccp_hdr _dccph, *dh; 41192ee92d46SJames Morris 41202ee92d46SJames Morris dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); 41212ee92d46SJames Morris if (dh == NULL) 41222ee92d46SJames Morris break; 41232ee92d46SJames Morris 412448c62af6SEric Paris ad->u.net->sport = dh->dccph_sport; 412548c62af6SEric Paris ad->u.net->dport = dh->dccph_dport; 41262ee92d46SJames Morris break; 41272ee92d46SJames Morris } 41282ee92d46SJames Morris 4129d452930fSRichard Haines #if IS_ENABLED(CONFIG_IP_SCTP) 4130d452930fSRichard Haines case IPPROTO_SCTP: { 4131d452930fSRichard Haines struct sctphdr _sctph, *sh; 4132d452930fSRichard Haines 4133d452930fSRichard Haines sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph); 4134d452930fSRichard Haines if (sh == NULL) 4135d452930fSRichard Haines break; 4136d452930fSRichard Haines 4137d452930fSRichard Haines ad->u.net->sport = sh->source; 4138d452930fSRichard Haines ad->u.net->dport = sh->dest; 4139d452930fSRichard Haines break; 4140d452930fSRichard Haines } 4141d452930fSRichard Haines #endif 41421da177e4SLinus Torvalds /* includes fragments */ 41431da177e4SLinus Torvalds default: 41441da177e4SLinus Torvalds break; 41451da177e4SLinus Torvalds } 41461da177e4SLinus Torvalds out: 41471da177e4SLinus Torvalds return ret; 41481da177e4SLinus Torvalds } 41491da177e4SLinus Torvalds 41501da177e4SLinus Torvalds #endif /* IPV6 */ 41511da177e4SLinus Torvalds 41522bf49690SThomas Liu static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad, 4153cf9481e2SDavid Howells char **_addrp, int src, u8 *proto) 41541da177e4SLinus Torvalds { 4155cf9481e2SDavid Howells char *addrp; 4156cf9481e2SDavid Howells int ret; 41571da177e4SLinus Torvalds 415848c62af6SEric Paris switch (ad->u.net->family) { 41591da177e4SLinus Torvalds case PF_INET: 416067f83cbfSVenkat Yekkirala ret = selinux_parse_skb_ipv4(skb, ad, proto); 4161cf9481e2SDavid Howells if (ret) 4162cf9481e2SDavid Howells goto parse_error; 416348c62af6SEric Paris addrp = (char *)(src ? &ad->u.net->v4info.saddr : 416448c62af6SEric Paris &ad->u.net->v4info.daddr); 4165cf9481e2SDavid Howells goto okay; 41661da177e4SLinus Torvalds 41671a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 41681da177e4SLinus Torvalds case PF_INET6: 416967f83cbfSVenkat Yekkirala ret = selinux_parse_skb_ipv6(skb, ad, proto); 4170cf9481e2SDavid Howells if (ret) 4171cf9481e2SDavid Howells goto parse_error; 417248c62af6SEric Paris addrp = (char *)(src ? &ad->u.net->v6info.saddr : 417348c62af6SEric Paris &ad->u.net->v6info.daddr); 4174cf9481e2SDavid Howells goto okay; 41751da177e4SLinus Torvalds #endif /* IPV6 */ 41761da177e4SLinus Torvalds default: 4177cf9481e2SDavid Howells addrp = NULL; 4178cf9481e2SDavid Howells goto okay; 41791da177e4SLinus Torvalds } 41801da177e4SLinus Torvalds 4181cf9481e2SDavid Howells parse_error: 4182c103a91eSpeter enderborg pr_warn( 418371f1cb05SPaul Moore "SELinux: failure in selinux_parse_skb()," 418471f1cb05SPaul Moore " unable to parse packet\n"); 41851da177e4SLinus Torvalds return ret; 4186cf9481e2SDavid Howells 4187cf9481e2SDavid Howells okay: 4188cf9481e2SDavid Howells if (_addrp) 4189cf9481e2SDavid Howells *_addrp = addrp; 4190cf9481e2SDavid Howells return 0; 41911da177e4SLinus Torvalds } 41921da177e4SLinus Torvalds 41934f6a993fSPaul Moore /** 4194220deb96SPaul Moore * selinux_skb_peerlbl_sid - Determine the peer label of a packet 41954f6a993fSPaul Moore * @skb: the packet 419675e22910SPaul Moore * @family: protocol family 4197220deb96SPaul Moore * @sid: the packet's peer label SID 41984f6a993fSPaul Moore * 41994f6a993fSPaul Moore * Description: 4200220deb96SPaul Moore * Check the various different forms of network peer labeling and determine 4201220deb96SPaul Moore * the peer label/SID for the packet; most of the magic actually occurs in 4202220deb96SPaul Moore * the security server function security_net_peersid_cmp(). The function 4203220deb96SPaul Moore * returns zero if the value in @sid is valid (although it may be SECSID_NULL) 4204220deb96SPaul Moore * or -EACCES if @sid is invalid due to inconsistencies with the different 4205220deb96SPaul Moore * peer labels. 42064f6a993fSPaul Moore * 42074f6a993fSPaul Moore */ 4208220deb96SPaul Moore static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid) 42094f6a993fSPaul Moore { 421071f1cb05SPaul Moore int err; 42114f6a993fSPaul Moore u32 xfrm_sid; 42124f6a993fSPaul Moore u32 nlbl_sid; 4213220deb96SPaul Moore u32 nlbl_type; 42144f6a993fSPaul Moore 4215817eff71SPaul Moore err = selinux_xfrm_skb_sid(skb, &xfrm_sid); 4216bed4d7efSPaul Moore if (unlikely(err)) 4217bed4d7efSPaul Moore return -EACCES; 4218bed4d7efSPaul Moore err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid); 4219bed4d7efSPaul Moore if (unlikely(err)) 4220bed4d7efSPaul Moore return -EACCES; 4221220deb96SPaul Moore 4222aa8e712cSStephen Smalley err = security_net_peersid_resolve(&selinux_state, nlbl_sid, 4223aa8e712cSStephen Smalley nlbl_type, xfrm_sid, sid); 422471f1cb05SPaul Moore if (unlikely(err)) { 4225c103a91eSpeter enderborg pr_warn( 422671f1cb05SPaul Moore "SELinux: failure in selinux_skb_peerlbl_sid()," 422771f1cb05SPaul Moore " unable to determine packet's peer label\n"); 4228220deb96SPaul Moore return -EACCES; 422971f1cb05SPaul Moore } 4230220deb96SPaul Moore 4231220deb96SPaul Moore return 0; 42324f6a993fSPaul Moore } 42334f6a993fSPaul Moore 4234446b8024SPaul Moore /** 4235446b8024SPaul Moore * selinux_conn_sid - Determine the child socket label for a connection 4236446b8024SPaul Moore * @sk_sid: the parent socket's SID 4237446b8024SPaul Moore * @skb_sid: the packet's SID 4238446b8024SPaul Moore * @conn_sid: the resulting connection SID 4239446b8024SPaul Moore * 4240446b8024SPaul Moore * If @skb_sid is valid then the user:role:type information from @sk_sid is 4241446b8024SPaul Moore * combined with the MLS information from @skb_sid in order to create 4242446b8024SPaul Moore * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy 4243446b8024SPaul Moore * of @sk_sid. Returns zero on success, negative values on failure. 4244446b8024SPaul Moore * 4245446b8024SPaul Moore */ 4246446b8024SPaul Moore static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid) 4247446b8024SPaul Moore { 4248446b8024SPaul Moore int err = 0; 4249446b8024SPaul Moore 4250446b8024SPaul Moore if (skb_sid != SECSID_NULL) 4251aa8e712cSStephen Smalley err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid, 4252aa8e712cSStephen Smalley conn_sid); 4253446b8024SPaul Moore else 4254446b8024SPaul Moore *conn_sid = sk_sid; 4255446b8024SPaul Moore 4256446b8024SPaul Moore return err; 4257446b8024SPaul Moore } 4258446b8024SPaul Moore 42591da177e4SLinus Torvalds /* socket security operations */ 4260d4f2d978SPaul Moore 42612ad18bdfSHarry Ciao static int socket_sockcreate_sid(const struct task_security_struct *tsec, 42622ad18bdfSHarry Ciao u16 secclass, u32 *socksid) 4263d4f2d978SPaul Moore { 42642ad18bdfSHarry Ciao if (tsec->sockcreate_sid > SECSID_NULL) { 42652ad18bdfSHarry Ciao *socksid = tsec->sockcreate_sid; 42662ad18bdfSHarry Ciao return 0; 42672ad18bdfSHarry Ciao } 42682ad18bdfSHarry Ciao 4269aa8e712cSStephen Smalley return security_transition_sid(&selinux_state, tsec->sid, tsec->sid, 4270aa8e712cSStephen Smalley secclass, NULL, socksid); 4271d4f2d978SPaul Moore } 4272d4f2d978SPaul Moore 4273be0554c9SStephen Smalley static int sock_has_perm(struct sock *sk, u32 perms) 42741da177e4SLinus Torvalds { 4275253bfae6SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 42762bf49690SThomas Liu struct common_audit_data ad; 427748c62af6SEric Paris struct lsm_network_audit net = {0,}; 42781da177e4SLinus Torvalds 4279253bfae6SPaul Moore if (sksec->sid == SECINITSID_KERNEL) 4280253bfae6SPaul Moore return 0; 42811da177e4SLinus Torvalds 428250c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 428348c62af6SEric Paris ad.u.net = &net; 428448c62af6SEric Paris ad.u.net->sk = sk; 42851da177e4SLinus Torvalds 42866b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 42876b6bc620SStephen Smalley current_sid(), sksec->sid, sksec->sclass, perms, 4288be0554c9SStephen Smalley &ad); 42891da177e4SLinus Torvalds } 42901da177e4SLinus Torvalds 42911da177e4SLinus Torvalds static int selinux_socket_create(int family, int type, 42921da177e4SLinus Torvalds int protocol, int kern) 42931da177e4SLinus Torvalds { 42940c6cfa62SCasey Schaufler const struct task_security_struct *tsec = selinux_cred(current_cred()); 4295d4f2d978SPaul Moore u32 newsid; 4296275bb41eSDavid Howells u16 secclass; 42972ad18bdfSHarry Ciao int rc; 42981da177e4SLinus Torvalds 42991da177e4SLinus Torvalds if (kern) 4300d4f2d978SPaul Moore return 0; 43011da177e4SLinus Torvalds 4302275bb41eSDavid Howells secclass = socket_type_to_security_class(family, type, protocol); 43032ad18bdfSHarry Ciao rc = socket_sockcreate_sid(tsec, secclass, &newsid); 43042ad18bdfSHarry Ciao if (rc) 43052ad18bdfSHarry Ciao return rc; 43062ad18bdfSHarry Ciao 43076b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 43086b6bc620SStephen Smalley tsec->sid, newsid, secclass, SOCKET__CREATE, NULL); 43091da177e4SLinus Torvalds } 43101da177e4SLinus Torvalds 43117420ed23SVenkat Yekkirala static int selinux_socket_post_create(struct socket *sock, int family, 43121da177e4SLinus Torvalds int type, int protocol, int kern) 43131da177e4SLinus Torvalds { 43140c6cfa62SCasey Schaufler const struct task_security_struct *tsec = selinux_cred(current_cred()); 43155d226df4SAndreas Gruenbacher struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock)); 4316892c141eSVenkat Yekkirala struct sk_security_struct *sksec; 43179287aed2SAndreas Gruenbacher u16 sclass = socket_type_to_security_class(family, type, protocol); 43189287aed2SAndreas Gruenbacher u32 sid = SECINITSID_KERNEL; 4319275bb41eSDavid Howells int err = 0; 4320275bb41eSDavid Howells 43219287aed2SAndreas Gruenbacher if (!kern) { 43229287aed2SAndreas Gruenbacher err = socket_sockcreate_sid(tsec, sclass, &sid); 43232ad18bdfSHarry Ciao if (err) 43242ad18bdfSHarry Ciao return err; 43252ad18bdfSHarry Ciao } 4326275bb41eSDavid Howells 43279287aed2SAndreas Gruenbacher isec->sclass = sclass; 43289287aed2SAndreas Gruenbacher isec->sid = sid; 43296f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 43301da177e4SLinus Torvalds 4331892c141eSVenkat Yekkirala if (sock->sk) { 4332892c141eSVenkat Yekkirala sksec = sock->sk->sk_security; 43339287aed2SAndreas Gruenbacher sksec->sclass = sclass; 43349287aed2SAndreas Gruenbacher sksec->sid = sid; 4335d452930fSRichard Haines /* Allows detection of the first association on this socket */ 4336d452930fSRichard Haines if (sksec->sclass == SECCLASS_SCTP_SOCKET) 4337d452930fSRichard Haines sksec->sctp_assoc_state = SCTP_ASSOC_UNSET; 4338d452930fSRichard Haines 4339389fb800SPaul Moore err = selinux_netlbl_socket_post_create(sock->sk, family); 4340892c141eSVenkat Yekkirala } 4341892c141eSVenkat Yekkirala 43427420ed23SVenkat Yekkirala return err; 43431da177e4SLinus Torvalds } 43441da177e4SLinus Torvalds 43450b811db2SDavid Herrmann static int selinux_socket_socketpair(struct socket *socka, 43460b811db2SDavid Herrmann struct socket *sockb) 43470b811db2SDavid Herrmann { 43480b811db2SDavid Herrmann struct sk_security_struct *sksec_a = socka->sk->sk_security; 43490b811db2SDavid Herrmann struct sk_security_struct *sksec_b = sockb->sk->sk_security; 43500b811db2SDavid Herrmann 43510b811db2SDavid Herrmann sksec_a->peer_sid = sksec_b->sid; 43520b811db2SDavid Herrmann sksec_b->peer_sid = sksec_a->sid; 43530b811db2SDavid Herrmann 43540b811db2SDavid Herrmann return 0; 43550b811db2SDavid Herrmann } 43560b811db2SDavid Herrmann 43571da177e4SLinus Torvalds /* Range of port numbers used to automatically bind. 43581da177e4SLinus Torvalds Need to determine whether we should perform a name_bind 43591da177e4SLinus Torvalds permission check between the socket and the port number. */ 43601da177e4SLinus Torvalds 43611da177e4SLinus Torvalds static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen) 43621da177e4SLinus Torvalds { 4363253bfae6SPaul Moore struct sock *sk = sock->sk; 43640f8db8ccSAlexey Kodanev struct sk_security_struct *sksec = sk->sk_security; 43651da177e4SLinus Torvalds u16 family; 43661da177e4SLinus Torvalds int err; 43671da177e4SLinus Torvalds 4368be0554c9SStephen Smalley err = sock_has_perm(sk, SOCKET__BIND); 43691da177e4SLinus Torvalds if (err) 43701da177e4SLinus Torvalds goto out; 43711da177e4SLinus Torvalds 4372d452930fSRichard Haines /* If PF_INET or PF_INET6, check name_bind permission for the port. */ 4373253bfae6SPaul Moore family = sk->sk_family; 43741da177e4SLinus Torvalds if (family == PF_INET || family == PF_INET6) { 43751da177e4SLinus Torvalds char *addrp; 43762bf49690SThomas Liu struct common_audit_data ad; 437748c62af6SEric Paris struct lsm_network_audit net = {0,}; 43781da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 43791da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 43800f8db8ccSAlexey Kodanev u16 family_sa = address->sa_family; 43811da177e4SLinus Torvalds unsigned short snum; 4382e399f982SJames Morris u32 sid, node_perm; 43831da177e4SLinus Torvalds 4384d452930fSRichard Haines /* 4385d452930fSRichard Haines * sctp_bindx(3) calls via selinux_sctp_bind_connect() 4386d452930fSRichard Haines * that validates multiple binding addresses. Because of this 4387d452930fSRichard Haines * need to check address->sa_family as it is possible to have 4388d452930fSRichard Haines * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET. 4389d452930fSRichard Haines */ 43900f8db8ccSAlexey Kodanev switch (family_sa) { 43910f8db8ccSAlexey Kodanev case AF_UNSPEC: 439268741a8aSRichard Haines case AF_INET: 439368741a8aSRichard Haines if (addrlen < sizeof(struct sockaddr_in)) 439468741a8aSRichard Haines return -EINVAL; 43951da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 43960f8db8ccSAlexey Kodanev if (family_sa == AF_UNSPEC) { 43970f8db8ccSAlexey Kodanev /* see __inet_bind(), we only want to allow 43980f8db8ccSAlexey Kodanev * AF_UNSPEC if the address is INADDR_ANY 43990f8db8ccSAlexey Kodanev */ 44000f8db8ccSAlexey Kodanev if (addr4->sin_addr.s_addr != htonl(INADDR_ANY)) 44010f8db8ccSAlexey Kodanev goto err_af; 44020f8db8ccSAlexey Kodanev family_sa = AF_INET; 44030f8db8ccSAlexey Kodanev } 44041da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 44051da177e4SLinus Torvalds addrp = (char *)&addr4->sin_addr.s_addr; 440668741a8aSRichard Haines break; 440768741a8aSRichard Haines case AF_INET6: 440868741a8aSRichard Haines if (addrlen < SIN6_LEN_RFC2133) 440968741a8aSRichard Haines return -EINVAL; 44101da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 44111da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 44121da177e4SLinus Torvalds addrp = (char *)&addr6->sin6_addr.s6_addr; 441368741a8aSRichard Haines break; 441468741a8aSRichard Haines default: 44150f8db8ccSAlexey Kodanev goto err_af; 44161da177e4SLinus Torvalds } 44171da177e4SLinus Torvalds 441888b7d370SAlexey Kodanev ad.type = LSM_AUDIT_DATA_NET; 441988b7d370SAlexey Kodanev ad.u.net = &net; 442088b7d370SAlexey Kodanev ad.u.net->sport = htons(snum); 442188b7d370SAlexey Kodanev ad.u.net->family = family_sa; 442288b7d370SAlexey Kodanev 4423227b60f5SStephen Hemminger if (snum) { 4424227b60f5SStephen Hemminger int low, high; 4425227b60f5SStephen Hemminger 44260bbf87d8SEric W. Biederman inet_get_local_port_range(sock_net(sk), &low, &high); 4427227b60f5SStephen Hemminger 44284548b683SKrister Johansen if (snum < max(inet_prot_sock(sock_net(sk)), low) || 44294548b683SKrister Johansen snum > high) { 44303e112172SPaul Moore err = sel_netport_sid(sk->sk_protocol, 44313e112172SPaul Moore snum, &sid); 44321da177e4SLinus Torvalds if (err) 44331da177e4SLinus Torvalds goto out; 44346b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 44356b6bc620SStephen Smalley sksec->sid, sid, 4436253bfae6SPaul Moore sksec->sclass, 44371da177e4SLinus Torvalds SOCKET__NAME_BIND, &ad); 44381da177e4SLinus Torvalds if (err) 44391da177e4SLinus Torvalds goto out; 44401da177e4SLinus Torvalds } 4441227b60f5SStephen Hemminger } 44421da177e4SLinus Torvalds 4443253bfae6SPaul Moore switch (sksec->sclass) { 444413402580SJames Morris case SECCLASS_TCP_SOCKET: 44451da177e4SLinus Torvalds node_perm = TCP_SOCKET__NODE_BIND; 44461da177e4SLinus Torvalds break; 44471da177e4SLinus Torvalds 444813402580SJames Morris case SECCLASS_UDP_SOCKET: 44491da177e4SLinus Torvalds node_perm = UDP_SOCKET__NODE_BIND; 44501da177e4SLinus Torvalds break; 44511da177e4SLinus Torvalds 44522ee92d46SJames Morris case SECCLASS_DCCP_SOCKET: 44532ee92d46SJames Morris node_perm = DCCP_SOCKET__NODE_BIND; 44542ee92d46SJames Morris break; 44552ee92d46SJames Morris 4456d452930fSRichard Haines case SECCLASS_SCTP_SOCKET: 4457d452930fSRichard Haines node_perm = SCTP_SOCKET__NODE_BIND; 4458d452930fSRichard Haines break; 4459d452930fSRichard Haines 44601da177e4SLinus Torvalds default: 44611da177e4SLinus Torvalds node_perm = RAWIP_SOCKET__NODE_BIND; 44621da177e4SLinus Torvalds break; 44631da177e4SLinus Torvalds } 44641da177e4SLinus Torvalds 446588b7d370SAlexey Kodanev err = sel_netnode_sid(addrp, family_sa, &sid); 44661da177e4SLinus Torvalds if (err) 44671da177e4SLinus Torvalds goto out; 44681da177e4SLinus Torvalds 44690f8db8ccSAlexey Kodanev if (family_sa == AF_INET) 447048c62af6SEric Paris ad.u.net->v4info.saddr = addr4->sin_addr.s_addr; 44711da177e4SLinus Torvalds else 447248c62af6SEric Paris ad.u.net->v6info.saddr = addr6->sin6_addr; 44731da177e4SLinus Torvalds 44746b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 44756b6bc620SStephen Smalley sksec->sid, sid, 4476253bfae6SPaul Moore sksec->sclass, node_perm, &ad); 44771da177e4SLinus Torvalds if (err) 44781da177e4SLinus Torvalds goto out; 44791da177e4SLinus Torvalds } 44801da177e4SLinus Torvalds out: 44811da177e4SLinus Torvalds return err; 44820f8db8ccSAlexey Kodanev err_af: 44830f8db8ccSAlexey Kodanev /* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */ 44840f8db8ccSAlexey Kodanev if (sksec->sclass == SECCLASS_SCTP_SOCKET) 44850f8db8ccSAlexey Kodanev return -EINVAL; 44860f8db8ccSAlexey Kodanev return -EAFNOSUPPORT; 44871da177e4SLinus Torvalds } 44881da177e4SLinus Torvalds 4489d452930fSRichard Haines /* This supports connect(2) and SCTP connect services such as sctp_connectx(3) 44905fb94e9cSMauro Carvalho Chehab * and sctp_sendmsg(3) as described in Documentation/security/LSM-sctp.rst 4491d452930fSRichard Haines */ 4492d452930fSRichard Haines static int selinux_socket_connect_helper(struct socket *sock, 4493d452930fSRichard Haines struct sockaddr *address, int addrlen) 44941da177e4SLinus Torvalds { 4495014ab19aSPaul Moore struct sock *sk = sock->sk; 4496253bfae6SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 44971da177e4SLinus Torvalds int err; 44981da177e4SLinus Torvalds 4499be0554c9SStephen Smalley err = sock_has_perm(sk, SOCKET__CONNECT); 45001da177e4SLinus Torvalds if (err) 45011da177e4SLinus Torvalds return err; 45021da177e4SLinus Torvalds 45031da177e4SLinus Torvalds /* 4504d452930fSRichard Haines * If a TCP, DCCP or SCTP socket, check name_connect permission 4505d452930fSRichard Haines * for the port. 45061da177e4SLinus Torvalds */ 4507253bfae6SPaul Moore if (sksec->sclass == SECCLASS_TCP_SOCKET || 4508d452930fSRichard Haines sksec->sclass == SECCLASS_DCCP_SOCKET || 4509d452930fSRichard Haines sksec->sclass == SECCLASS_SCTP_SOCKET) { 45102bf49690SThomas Liu struct common_audit_data ad; 451148c62af6SEric Paris struct lsm_network_audit net = {0,}; 45121da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 45131da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 45141da177e4SLinus Torvalds unsigned short snum; 45152ee92d46SJames Morris u32 sid, perm; 45161da177e4SLinus Torvalds 4517d452930fSRichard Haines /* sctp_connectx(3) calls via selinux_sctp_bind_connect() 4518d452930fSRichard Haines * that validates multiple connect addresses. Because of this 4519d452930fSRichard Haines * need to check address->sa_family as it is possible to have 4520d452930fSRichard Haines * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET. 4521d452930fSRichard Haines */ 452268741a8aSRichard Haines switch (address->sa_family) { 452368741a8aSRichard Haines case AF_INET: 45241da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 4525911656f8SStephen Smalley if (addrlen < sizeof(struct sockaddr_in)) 45261da177e4SLinus Torvalds return -EINVAL; 45271da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 452868741a8aSRichard Haines break; 452968741a8aSRichard Haines case AF_INET6: 45301da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 4531911656f8SStephen Smalley if (addrlen < SIN6_LEN_RFC2133) 45321da177e4SLinus Torvalds return -EINVAL; 45331da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 453468741a8aSRichard Haines break; 453568741a8aSRichard Haines default: 453668741a8aSRichard Haines /* Note that SCTP services expect -EINVAL, whereas 453768741a8aSRichard Haines * others expect -EAFNOSUPPORT. 453868741a8aSRichard Haines */ 453968741a8aSRichard Haines if (sksec->sclass == SECCLASS_SCTP_SOCKET) 454068741a8aSRichard Haines return -EINVAL; 454168741a8aSRichard Haines else 454268741a8aSRichard Haines return -EAFNOSUPPORT; 45431da177e4SLinus Torvalds } 45441da177e4SLinus Torvalds 45453e112172SPaul Moore err = sel_netport_sid(sk->sk_protocol, snum, &sid); 45461da177e4SLinus Torvalds if (err) 4547d452930fSRichard Haines return err; 45481da177e4SLinus Torvalds 4549d452930fSRichard Haines switch (sksec->sclass) { 4550d452930fSRichard Haines case SECCLASS_TCP_SOCKET: 4551d452930fSRichard Haines perm = TCP_SOCKET__NAME_CONNECT; 4552d452930fSRichard Haines break; 4553d452930fSRichard Haines case SECCLASS_DCCP_SOCKET: 4554d452930fSRichard Haines perm = DCCP_SOCKET__NAME_CONNECT; 4555d452930fSRichard Haines break; 4556d452930fSRichard Haines case SECCLASS_SCTP_SOCKET: 4557d452930fSRichard Haines perm = SCTP_SOCKET__NAME_CONNECT; 4558d452930fSRichard Haines break; 4559d452930fSRichard Haines } 45602ee92d46SJames Morris 456150c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 456248c62af6SEric Paris ad.u.net = &net; 456348c62af6SEric Paris ad.u.net->dport = htons(snum); 456488b7d370SAlexey Kodanev ad.u.net->family = address->sa_family; 45656b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 45666b6bc620SStephen Smalley sksec->sid, sid, sksec->sclass, perm, &ad); 45671da177e4SLinus Torvalds if (err) 4568d452930fSRichard Haines return err; 45691da177e4SLinus Torvalds } 45701da177e4SLinus Torvalds 4571d452930fSRichard Haines return 0; 4572d452930fSRichard Haines } 4573014ab19aSPaul Moore 4574d452930fSRichard Haines /* Supports connect(2), see comments in selinux_socket_connect_helper() */ 4575d452930fSRichard Haines static int selinux_socket_connect(struct socket *sock, 4576d452930fSRichard Haines struct sockaddr *address, int addrlen) 4577d452930fSRichard Haines { 4578d452930fSRichard Haines int err; 4579d452930fSRichard Haines struct sock *sk = sock->sk; 4580d452930fSRichard Haines 4581d452930fSRichard Haines err = selinux_socket_connect_helper(sock, address, addrlen); 4582d452930fSRichard Haines if (err) 45831da177e4SLinus Torvalds return err; 4584d452930fSRichard Haines 4585d452930fSRichard Haines return selinux_netlbl_socket_connect(sk, address); 45861da177e4SLinus Torvalds } 45871da177e4SLinus Torvalds 45881da177e4SLinus Torvalds static int selinux_socket_listen(struct socket *sock, int backlog) 45891da177e4SLinus Torvalds { 4590be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__LISTEN); 45911da177e4SLinus Torvalds } 45921da177e4SLinus Torvalds 45931da177e4SLinus Torvalds static int selinux_socket_accept(struct socket *sock, struct socket *newsock) 45941da177e4SLinus Torvalds { 45951da177e4SLinus Torvalds int err; 45961da177e4SLinus Torvalds struct inode_security_struct *isec; 45971da177e4SLinus Torvalds struct inode_security_struct *newisec; 45989287aed2SAndreas Gruenbacher u16 sclass; 45999287aed2SAndreas Gruenbacher u32 sid; 46001da177e4SLinus Torvalds 4601be0554c9SStephen Smalley err = sock_has_perm(sock->sk, SOCKET__ACCEPT); 46021da177e4SLinus Torvalds if (err) 46031da177e4SLinus Torvalds return err; 46041da177e4SLinus Torvalds 46055d226df4SAndreas Gruenbacher isec = inode_security_novalidate(SOCK_INODE(sock)); 46069287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 46079287aed2SAndreas Gruenbacher sclass = isec->sclass; 46089287aed2SAndreas Gruenbacher sid = isec->sid; 46099287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 46109287aed2SAndreas Gruenbacher 46119287aed2SAndreas Gruenbacher newisec = inode_security_novalidate(SOCK_INODE(newsock)); 46129287aed2SAndreas Gruenbacher newisec->sclass = sclass; 46139287aed2SAndreas Gruenbacher newisec->sid = sid; 46146f3be9f5SAndreas Gruenbacher newisec->initialized = LABEL_INITIALIZED; 46151da177e4SLinus Torvalds 46161da177e4SLinus Torvalds return 0; 46171da177e4SLinus Torvalds } 46181da177e4SLinus Torvalds 46191da177e4SLinus Torvalds static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg, 46201da177e4SLinus Torvalds int size) 46211da177e4SLinus Torvalds { 4622be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__WRITE); 46231da177e4SLinus Torvalds } 46241da177e4SLinus Torvalds 46251da177e4SLinus Torvalds static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg, 46261da177e4SLinus Torvalds int size, int flags) 46271da177e4SLinus Torvalds { 4628be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__READ); 46291da177e4SLinus Torvalds } 46301da177e4SLinus Torvalds 46311da177e4SLinus Torvalds static int selinux_socket_getsockname(struct socket *sock) 46321da177e4SLinus Torvalds { 4633be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__GETATTR); 46341da177e4SLinus Torvalds } 46351da177e4SLinus Torvalds 46361da177e4SLinus Torvalds static int selinux_socket_getpeername(struct socket *sock) 46371da177e4SLinus Torvalds { 4638be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__GETATTR); 46391da177e4SLinus Torvalds } 46401da177e4SLinus Torvalds 46411da177e4SLinus Torvalds static int selinux_socket_setsockopt(struct socket *sock, int level, int optname) 46421da177e4SLinus Torvalds { 4643f8687afeSPaul Moore int err; 4644f8687afeSPaul Moore 4645be0554c9SStephen Smalley err = sock_has_perm(sock->sk, SOCKET__SETOPT); 4646f8687afeSPaul Moore if (err) 4647f8687afeSPaul Moore return err; 4648f8687afeSPaul Moore 4649f8687afeSPaul Moore return selinux_netlbl_socket_setsockopt(sock, level, optname); 46501da177e4SLinus Torvalds } 46511da177e4SLinus Torvalds 46521da177e4SLinus Torvalds static int selinux_socket_getsockopt(struct socket *sock, int level, 46531da177e4SLinus Torvalds int optname) 46541da177e4SLinus Torvalds { 4655be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__GETOPT); 46561da177e4SLinus Torvalds } 46571da177e4SLinus Torvalds 46581da177e4SLinus Torvalds static int selinux_socket_shutdown(struct socket *sock, int how) 46591da177e4SLinus Torvalds { 4660be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__SHUTDOWN); 46611da177e4SLinus Torvalds } 46621da177e4SLinus Torvalds 46633610cda5SDavid S. Miller static int selinux_socket_unix_stream_connect(struct sock *sock, 46643610cda5SDavid S. Miller struct sock *other, 46651da177e4SLinus Torvalds struct sock *newsk) 46661da177e4SLinus Torvalds { 46673610cda5SDavid S. Miller struct sk_security_struct *sksec_sock = sock->sk_security; 46683610cda5SDavid S. Miller struct sk_security_struct *sksec_other = other->sk_security; 46694d1e2451SPaul Moore struct sk_security_struct *sksec_new = newsk->sk_security; 46702bf49690SThomas Liu struct common_audit_data ad; 467148c62af6SEric Paris struct lsm_network_audit net = {0,}; 46721da177e4SLinus Torvalds int err; 46731da177e4SLinus Torvalds 467450c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 467548c62af6SEric Paris ad.u.net = &net; 467648c62af6SEric Paris ad.u.net->sk = other; 46771da177e4SLinus Torvalds 46786b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 46796b6bc620SStephen Smalley sksec_sock->sid, sksec_other->sid, 46804d1e2451SPaul Moore sksec_other->sclass, 46811da177e4SLinus Torvalds UNIX_STREAM_SOCKET__CONNECTTO, &ad); 46821da177e4SLinus Torvalds if (err) 46831da177e4SLinus Torvalds return err; 46841da177e4SLinus Torvalds 46851da177e4SLinus Torvalds /* server child socket */ 46864d1e2451SPaul Moore sksec_new->peer_sid = sksec_sock->sid; 4687aa8e712cSStephen Smalley err = security_sid_mls_copy(&selinux_state, sksec_other->sid, 4688aa8e712cSStephen Smalley sksec_sock->sid, &sksec_new->sid); 46894d1e2451SPaul Moore if (err) 46904237c75cSVenkat Yekkirala return err; 46914d1e2451SPaul Moore 46924d1e2451SPaul Moore /* connecting socket */ 46934d1e2451SPaul Moore sksec_sock->peer_sid = sksec_new->sid; 46944d1e2451SPaul Moore 46954d1e2451SPaul Moore return 0; 46961da177e4SLinus Torvalds } 46971da177e4SLinus Torvalds 46981da177e4SLinus Torvalds static int selinux_socket_unix_may_send(struct socket *sock, 46991da177e4SLinus Torvalds struct socket *other) 47001da177e4SLinus Torvalds { 4701253bfae6SPaul Moore struct sk_security_struct *ssec = sock->sk->sk_security; 4702253bfae6SPaul Moore struct sk_security_struct *osec = other->sk->sk_security; 47032bf49690SThomas Liu struct common_audit_data ad; 470448c62af6SEric Paris struct lsm_network_audit net = {0,}; 47051da177e4SLinus Torvalds 470650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 470748c62af6SEric Paris ad.u.net = &net; 470848c62af6SEric Paris ad.u.net->sk = other->sk; 47091da177e4SLinus Torvalds 47106b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 47116b6bc620SStephen Smalley ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO, 4712253bfae6SPaul Moore &ad); 47131da177e4SLinus Torvalds } 47141da177e4SLinus Torvalds 4715cbe0d6e8SPaul Moore static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex, 4716cbe0d6e8SPaul Moore char *addrp, u16 family, u32 peer_sid, 47172bf49690SThomas Liu struct common_audit_data *ad) 4718effad8dfSPaul Moore { 4719effad8dfSPaul Moore int err; 4720effad8dfSPaul Moore u32 if_sid; 4721effad8dfSPaul Moore u32 node_sid; 4722effad8dfSPaul Moore 4723cbe0d6e8SPaul Moore err = sel_netif_sid(ns, ifindex, &if_sid); 4724effad8dfSPaul Moore if (err) 4725effad8dfSPaul Moore return err; 47266b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 47276b6bc620SStephen Smalley peer_sid, if_sid, 4728effad8dfSPaul Moore SECCLASS_NETIF, NETIF__INGRESS, ad); 4729effad8dfSPaul Moore if (err) 4730effad8dfSPaul Moore return err; 4731effad8dfSPaul Moore 4732effad8dfSPaul Moore err = sel_netnode_sid(addrp, family, &node_sid); 4733effad8dfSPaul Moore if (err) 4734effad8dfSPaul Moore return err; 47356b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 47366b6bc620SStephen Smalley peer_sid, node_sid, 4737effad8dfSPaul Moore SECCLASS_NODE, NODE__RECVFROM, ad); 4738effad8dfSPaul Moore } 4739effad8dfSPaul Moore 4740220deb96SPaul Moore static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb, 4741d8395c87SPaul Moore u16 family) 4742220deb96SPaul Moore { 4743277d342fSPaul Moore int err = 0; 4744220deb96SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 4745220deb96SPaul Moore u32 sk_sid = sksec->sid; 47462bf49690SThomas Liu struct common_audit_data ad; 474748c62af6SEric Paris struct lsm_network_audit net = {0,}; 4748d8395c87SPaul Moore char *addrp; 4749d8395c87SPaul Moore 475050c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 475148c62af6SEric Paris ad.u.net = &net; 475248c62af6SEric Paris ad.u.net->netif = skb->skb_iif; 475348c62af6SEric Paris ad.u.net->family = family; 4754d8395c87SPaul Moore err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); 4755d8395c87SPaul Moore if (err) 4756d8395c87SPaul Moore return err; 4757220deb96SPaul Moore 475858bfbb51SPaul Moore if (selinux_secmark_enabled()) { 47596b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 47606b6bc620SStephen Smalley sk_sid, skb->secmark, SECCLASS_PACKET, 4761d8395c87SPaul Moore PACKET__RECV, &ad); 4762220deb96SPaul Moore if (err) 4763220deb96SPaul Moore return err; 476458bfbb51SPaul Moore } 4765220deb96SPaul Moore 4766d8395c87SPaul Moore err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad); 4767220deb96SPaul Moore if (err) 4768220deb96SPaul Moore return err; 4769d8395c87SPaul Moore err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad); 4770220deb96SPaul Moore 47714e5ab4cbSJames Morris return err; 47724e5ab4cbSJames Morris } 4773d28d1e08STrent Jaeger 47744e5ab4cbSJames Morris static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) 47754e5ab4cbSJames Morris { 4776220deb96SPaul Moore int err; 47774237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 4778220deb96SPaul Moore u16 family = sk->sk_family; 4779220deb96SPaul Moore u32 sk_sid = sksec->sid; 47802bf49690SThomas Liu struct common_audit_data ad; 478148c62af6SEric Paris struct lsm_network_audit net = {0,}; 4782220deb96SPaul Moore char *addrp; 4783d8395c87SPaul Moore u8 secmark_active; 4784d8395c87SPaul Moore u8 peerlbl_active; 47854e5ab4cbSJames Morris 47864e5ab4cbSJames Morris if (family != PF_INET && family != PF_INET6) 4787220deb96SPaul Moore return 0; 47884e5ab4cbSJames Morris 47894e5ab4cbSJames Morris /* Handle mapped IPv4 packets arriving via IPv6 sockets */ 479087fcd70dSAl Viro if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 47914e5ab4cbSJames Morris family = PF_INET; 47924e5ab4cbSJames Morris 4793d8395c87SPaul Moore /* If any sort of compatibility mode is enabled then handoff processing 4794d8395c87SPaul Moore * to the selinux_sock_rcv_skb_compat() function to deal with the 4795d8395c87SPaul Moore * special handling. We do this in an attempt to keep this function 4796d8395c87SPaul Moore * as fast and as clean as possible. */ 4797aa8e712cSStephen Smalley if (!selinux_policycap_netpeer()) 4798d8395c87SPaul Moore return selinux_sock_rcv_skb_compat(sk, skb, family); 4799d8395c87SPaul Moore 4800d8395c87SPaul Moore secmark_active = selinux_secmark_enabled(); 48012be4d74fSChris PeBenito peerlbl_active = selinux_peerlbl_enabled(); 4802d8395c87SPaul Moore if (!secmark_active && !peerlbl_active) 4803d8395c87SPaul Moore return 0; 4804d8395c87SPaul Moore 480550c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 480648c62af6SEric Paris ad.u.net = &net; 480748c62af6SEric Paris ad.u.net->netif = skb->skb_iif; 480848c62af6SEric Paris ad.u.net->family = family; 4809224dfbd8SPaul Moore err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); 48104e5ab4cbSJames Morris if (err) 4811220deb96SPaul Moore return err; 48124e5ab4cbSJames Morris 4813d8395c87SPaul Moore if (peerlbl_active) { 4814d621d35eSPaul Moore u32 peer_sid; 4815220deb96SPaul Moore 4816220deb96SPaul Moore err = selinux_skb_peerlbl_sid(skb, family, &peer_sid); 4817220deb96SPaul Moore if (err) 4818220deb96SPaul Moore return err; 4819cbe0d6e8SPaul Moore err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif, 4820cbe0d6e8SPaul Moore addrp, family, peer_sid, &ad); 4821dfaebe98SPaul Moore if (err) { 4822a04e71f6SHuw Davies selinux_netlbl_err(skb, family, err, 0); 4823effad8dfSPaul Moore return err; 4824dfaebe98SPaul Moore } 48256b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 48266b6bc620SStephen Smalley sk_sid, peer_sid, SECCLASS_PEER, 4827d621d35eSPaul Moore PEER__RECV, &ad); 482846d01d63SChad Hanson if (err) { 4829a04e71f6SHuw Davies selinux_netlbl_err(skb, family, err, 0); 483046d01d63SChad Hanson return err; 483146d01d63SChad Hanson } 4832d621d35eSPaul Moore } 4833d621d35eSPaul Moore 4834d8395c87SPaul Moore if (secmark_active) { 48356b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 48366b6bc620SStephen Smalley sk_sid, skb->secmark, SECCLASS_PACKET, 4837effad8dfSPaul Moore PACKET__RECV, &ad); 4838effad8dfSPaul Moore if (err) 4839effad8dfSPaul Moore return err; 4840effad8dfSPaul Moore } 4841effad8dfSPaul Moore 4842d621d35eSPaul Moore return err; 48431da177e4SLinus Torvalds } 48441da177e4SLinus Torvalds 48452c7946a7SCatherine Zhang static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval, 48461da177e4SLinus Torvalds int __user *optlen, unsigned len) 48471da177e4SLinus Torvalds { 48481da177e4SLinus Torvalds int err = 0; 48491da177e4SLinus Torvalds char *scontext; 48501da177e4SLinus Torvalds u32 scontext_len; 4851253bfae6SPaul Moore struct sk_security_struct *sksec = sock->sk->sk_security; 48523de4bab5SPaul Moore u32 peer_sid = SECSID_NULL; 48531da177e4SLinus Torvalds 4854253bfae6SPaul Moore if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET || 4855d452930fSRichard Haines sksec->sclass == SECCLASS_TCP_SOCKET || 4856d452930fSRichard Haines sksec->sclass == SECCLASS_SCTP_SOCKET) 4857dd3e7836SEric Paris peer_sid = sksec->peer_sid; 4858253bfae6SPaul Moore if (peer_sid == SECSID_NULL) 4859253bfae6SPaul Moore return -ENOPROTOOPT; 48601da177e4SLinus Torvalds 4861aa8e712cSStephen Smalley err = security_sid_to_context(&selinux_state, peer_sid, &scontext, 4862aa8e712cSStephen Smalley &scontext_len); 48631da177e4SLinus Torvalds if (err) 4864253bfae6SPaul Moore return err; 48651da177e4SLinus Torvalds 48661da177e4SLinus Torvalds if (scontext_len > len) { 48671da177e4SLinus Torvalds err = -ERANGE; 48681da177e4SLinus Torvalds goto out_len; 48691da177e4SLinus Torvalds } 48701da177e4SLinus Torvalds 48711da177e4SLinus Torvalds if (copy_to_user(optval, scontext, scontext_len)) 48721da177e4SLinus Torvalds err = -EFAULT; 48731da177e4SLinus Torvalds 48741da177e4SLinus Torvalds out_len: 48751da177e4SLinus Torvalds if (put_user(scontext_len, optlen)) 48761da177e4SLinus Torvalds err = -EFAULT; 48771da177e4SLinus Torvalds kfree(scontext); 48781da177e4SLinus Torvalds return err; 48791da177e4SLinus Torvalds } 48801da177e4SLinus Torvalds 4881dc49c1f9SCatherine Zhang static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid) 48822c7946a7SCatherine Zhang { 4883dc49c1f9SCatherine Zhang u32 peer_secid = SECSID_NULL; 488475e22910SPaul Moore u16 family; 4885899134f2SPaul Moore struct inode_security_struct *isec; 4886877ce7c1SCatherine Zhang 4887aa862900SPaul Moore if (skb && skb->protocol == htons(ETH_P_IP)) 4888aa862900SPaul Moore family = PF_INET; 4889aa862900SPaul Moore else if (skb && skb->protocol == htons(ETH_P_IPV6)) 4890aa862900SPaul Moore family = PF_INET6; 4891aa862900SPaul Moore else if (sock) 489275e22910SPaul Moore family = sock->sk->sk_family; 489375e22910SPaul Moore else 489475e22910SPaul Moore goto out; 489575e22910SPaul Moore 4896899134f2SPaul Moore if (sock && family == PF_UNIX) { 4897899134f2SPaul Moore isec = inode_security_novalidate(SOCK_INODE(sock)); 4898899134f2SPaul Moore peer_secid = isec->sid; 4899899134f2SPaul Moore } else if (skb) 4900220deb96SPaul Moore selinux_skb_peerlbl_sid(skb, family, &peer_secid); 49012c7946a7SCatherine Zhang 490275e22910SPaul Moore out: 4903dc49c1f9SCatherine Zhang *secid = peer_secid; 490475e22910SPaul Moore if (peer_secid == SECSID_NULL) 490575e22910SPaul Moore return -EINVAL; 490675e22910SPaul Moore return 0; 49072c7946a7SCatherine Zhang } 49082c7946a7SCatherine Zhang 49097d877f3bSAl Viro static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority) 49101da177e4SLinus Torvalds { 491184914b7eSPaul Moore struct sk_security_struct *sksec; 491284914b7eSPaul Moore 491384914b7eSPaul Moore sksec = kzalloc(sizeof(*sksec), priority); 491484914b7eSPaul Moore if (!sksec) 491584914b7eSPaul Moore return -ENOMEM; 491684914b7eSPaul Moore 491784914b7eSPaul Moore sksec->peer_sid = SECINITSID_UNLABELED; 491884914b7eSPaul Moore sksec->sid = SECINITSID_UNLABELED; 49195dee25d0SStephen Smalley sksec->sclass = SECCLASS_SOCKET; 492084914b7eSPaul Moore selinux_netlbl_sk_security_reset(sksec); 492184914b7eSPaul Moore sk->sk_security = sksec; 492284914b7eSPaul Moore 492384914b7eSPaul Moore return 0; 49241da177e4SLinus Torvalds } 49251da177e4SLinus Torvalds 49261da177e4SLinus Torvalds static void selinux_sk_free_security(struct sock *sk) 49271da177e4SLinus Torvalds { 492884914b7eSPaul Moore struct sk_security_struct *sksec = sk->sk_security; 492984914b7eSPaul Moore 493084914b7eSPaul Moore sk->sk_security = NULL; 493184914b7eSPaul Moore selinux_netlbl_sk_security_free(sksec); 493284914b7eSPaul Moore kfree(sksec); 49331da177e4SLinus Torvalds } 49341da177e4SLinus Torvalds 4935892c141eSVenkat Yekkirala static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk) 4936892c141eSVenkat Yekkirala { 4937dd3e7836SEric Paris struct sk_security_struct *sksec = sk->sk_security; 4938dd3e7836SEric Paris struct sk_security_struct *newsksec = newsk->sk_security; 4939892c141eSVenkat Yekkirala 4940dd3e7836SEric Paris newsksec->sid = sksec->sid; 4941dd3e7836SEric Paris newsksec->peer_sid = sksec->peer_sid; 4942dd3e7836SEric Paris newsksec->sclass = sksec->sclass; 494399f59ed0SPaul Moore 4944dd3e7836SEric Paris selinux_netlbl_sk_security_reset(newsksec); 4945892c141eSVenkat Yekkirala } 4946892c141eSVenkat Yekkirala 4947beb8d13bSVenkat Yekkirala static void selinux_sk_getsecid(struct sock *sk, u32 *secid) 4948d28d1e08STrent Jaeger { 4949d28d1e08STrent Jaeger if (!sk) 4950beb8d13bSVenkat Yekkirala *secid = SECINITSID_ANY_SOCKET; 4951892c141eSVenkat Yekkirala else { 4952892c141eSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 4953d28d1e08STrent Jaeger 4954beb8d13bSVenkat Yekkirala *secid = sksec->sid; 4955892c141eSVenkat Yekkirala } 4956d28d1e08STrent Jaeger } 4957d28d1e08STrent Jaeger 49589a673e56SAdrian Bunk static void selinux_sock_graft(struct sock *sk, struct socket *parent) 49594237c75cSVenkat Yekkirala { 49605d226df4SAndreas Gruenbacher struct inode_security_struct *isec = 49615d226df4SAndreas Gruenbacher inode_security_novalidate(SOCK_INODE(parent)); 49624237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 49634237c75cSVenkat Yekkirala 49642873ead7SPaul Moore if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 || 49652873ead7SPaul Moore sk->sk_family == PF_UNIX) 49664237c75cSVenkat Yekkirala isec->sid = sksec->sid; 4967220deb96SPaul Moore sksec->sclass = isec->sclass; 49684237c75cSVenkat Yekkirala } 49694237c75cSVenkat Yekkirala 4970d452930fSRichard Haines /* Called whenever SCTP receives an INIT chunk. This happens when an incoming 4971d452930fSRichard Haines * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association 4972d452930fSRichard Haines * already present). 4973d452930fSRichard Haines */ 4974d452930fSRichard Haines static int selinux_sctp_assoc_request(struct sctp_endpoint *ep, 4975d452930fSRichard Haines struct sk_buff *skb) 4976d452930fSRichard Haines { 4977d452930fSRichard Haines struct sk_security_struct *sksec = ep->base.sk->sk_security; 4978d452930fSRichard Haines struct common_audit_data ad; 4979d452930fSRichard Haines struct lsm_network_audit net = {0,}; 4980d452930fSRichard Haines u8 peerlbl_active; 4981d452930fSRichard Haines u32 peer_sid = SECINITSID_UNLABELED; 4982d452930fSRichard Haines u32 conn_sid; 4983d452930fSRichard Haines int err = 0; 4984d452930fSRichard Haines 4985aa8e712cSStephen Smalley if (!selinux_policycap_extsockclass()) 4986d452930fSRichard Haines return 0; 4987d452930fSRichard Haines 4988d452930fSRichard Haines peerlbl_active = selinux_peerlbl_enabled(); 4989d452930fSRichard Haines 4990d452930fSRichard Haines if (peerlbl_active) { 4991d452930fSRichard Haines /* This will return peer_sid = SECSID_NULL if there are 4992d452930fSRichard Haines * no peer labels, see security_net_peersid_resolve(). 4993d452930fSRichard Haines */ 4994d452930fSRichard Haines err = selinux_skb_peerlbl_sid(skb, ep->base.sk->sk_family, 4995d452930fSRichard Haines &peer_sid); 4996d452930fSRichard Haines if (err) 4997d452930fSRichard Haines return err; 4998d452930fSRichard Haines 4999d452930fSRichard Haines if (peer_sid == SECSID_NULL) 5000d452930fSRichard Haines peer_sid = SECINITSID_UNLABELED; 5001d452930fSRichard Haines } 5002d452930fSRichard Haines 5003d452930fSRichard Haines if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) { 5004d452930fSRichard Haines sksec->sctp_assoc_state = SCTP_ASSOC_SET; 5005d452930fSRichard Haines 5006d452930fSRichard Haines /* Here as first association on socket. As the peer SID 5007d452930fSRichard Haines * was allowed by peer recv (and the netif/node checks), 5008d452930fSRichard Haines * then it is approved by policy and used as the primary 5009d452930fSRichard Haines * peer SID for getpeercon(3). 5010d452930fSRichard Haines */ 5011d452930fSRichard Haines sksec->peer_sid = peer_sid; 5012d452930fSRichard Haines } else if (sksec->peer_sid != peer_sid) { 5013d452930fSRichard Haines /* Other association peer SIDs are checked to enforce 5014d452930fSRichard Haines * consistency among the peer SIDs. 5015d452930fSRichard Haines */ 5016d452930fSRichard Haines ad.type = LSM_AUDIT_DATA_NET; 5017d452930fSRichard Haines ad.u.net = &net; 5018d452930fSRichard Haines ad.u.net->sk = ep->base.sk; 50196b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 50206b6bc620SStephen Smalley sksec->peer_sid, peer_sid, sksec->sclass, 5021d452930fSRichard Haines SCTP_SOCKET__ASSOCIATION, &ad); 5022d452930fSRichard Haines if (err) 5023d452930fSRichard Haines return err; 5024d452930fSRichard Haines } 5025d452930fSRichard Haines 5026d452930fSRichard Haines /* Compute the MLS component for the connection and store 5027d452930fSRichard Haines * the information in ep. This will be used by SCTP TCP type 5028d452930fSRichard Haines * sockets and peeled off connections as they cause a new 5029d452930fSRichard Haines * socket to be generated. selinux_sctp_sk_clone() will then 5030d452930fSRichard Haines * plug this into the new socket. 5031d452930fSRichard Haines */ 5032d452930fSRichard Haines err = selinux_conn_sid(sksec->sid, peer_sid, &conn_sid); 5033d452930fSRichard Haines if (err) 5034d452930fSRichard Haines return err; 5035d452930fSRichard Haines 5036d452930fSRichard Haines ep->secid = conn_sid; 5037d452930fSRichard Haines ep->peer_secid = peer_sid; 5038d452930fSRichard Haines 5039d452930fSRichard Haines /* Set any NetLabel labels including CIPSO/CALIPSO options. */ 5040d452930fSRichard Haines return selinux_netlbl_sctp_assoc_request(ep, skb); 5041d452930fSRichard Haines } 5042d452930fSRichard Haines 5043d452930fSRichard Haines /* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting 5044d452930fSRichard Haines * based on their @optname. 5045d452930fSRichard Haines */ 5046d452930fSRichard Haines static int selinux_sctp_bind_connect(struct sock *sk, int optname, 5047d452930fSRichard Haines struct sockaddr *address, 5048d452930fSRichard Haines int addrlen) 5049d452930fSRichard Haines { 5050d452930fSRichard Haines int len, err = 0, walk_size = 0; 5051d452930fSRichard Haines void *addr_buf; 5052d452930fSRichard Haines struct sockaddr *addr; 5053d452930fSRichard Haines struct socket *sock; 5054d452930fSRichard Haines 5055aa8e712cSStephen Smalley if (!selinux_policycap_extsockclass()) 5056d452930fSRichard Haines return 0; 5057d452930fSRichard Haines 5058d452930fSRichard Haines /* Process one or more addresses that may be IPv4 or IPv6 */ 5059d452930fSRichard Haines sock = sk->sk_socket; 5060d452930fSRichard Haines addr_buf = address; 5061d452930fSRichard Haines 5062d452930fSRichard Haines while (walk_size < addrlen) { 5063c138325fSOndrej Mosnacek if (walk_size + sizeof(sa_family_t) > addrlen) 5064c138325fSOndrej Mosnacek return -EINVAL; 5065c138325fSOndrej Mosnacek 5066d452930fSRichard Haines addr = addr_buf; 5067d452930fSRichard Haines switch (addr->sa_family) { 50684152dc91SAlexey Kodanev case AF_UNSPEC: 5069d452930fSRichard Haines case AF_INET: 5070d452930fSRichard Haines len = sizeof(struct sockaddr_in); 5071d452930fSRichard Haines break; 5072d452930fSRichard Haines case AF_INET6: 5073d452930fSRichard Haines len = sizeof(struct sockaddr_in6); 5074d452930fSRichard Haines break; 5075d452930fSRichard Haines default: 50764152dc91SAlexey Kodanev return -EINVAL; 5077d452930fSRichard Haines } 5078d452930fSRichard Haines 5079d452930fSRichard Haines err = -EINVAL; 5080d452930fSRichard Haines switch (optname) { 5081d452930fSRichard Haines /* Bind checks */ 5082d452930fSRichard Haines case SCTP_PRIMARY_ADDR: 5083d452930fSRichard Haines case SCTP_SET_PEER_PRIMARY_ADDR: 5084d452930fSRichard Haines case SCTP_SOCKOPT_BINDX_ADD: 5085d452930fSRichard Haines err = selinux_socket_bind(sock, addr, len); 5086d452930fSRichard Haines break; 5087d452930fSRichard Haines /* Connect checks */ 5088d452930fSRichard Haines case SCTP_SOCKOPT_CONNECTX: 5089d452930fSRichard Haines case SCTP_PARAM_SET_PRIMARY: 5090d452930fSRichard Haines case SCTP_PARAM_ADD_IP: 5091d452930fSRichard Haines case SCTP_SENDMSG_CONNECT: 5092d452930fSRichard Haines err = selinux_socket_connect_helper(sock, addr, len); 5093d452930fSRichard Haines if (err) 5094d452930fSRichard Haines return err; 5095d452930fSRichard Haines 5096d452930fSRichard Haines /* As selinux_sctp_bind_connect() is called by the 5097d452930fSRichard Haines * SCTP protocol layer, the socket is already locked, 5098d452930fSRichard Haines * therefore selinux_netlbl_socket_connect_locked() is 5099d452930fSRichard Haines * is called here. The situations handled are: 5100d452930fSRichard Haines * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2), 5101d452930fSRichard Haines * whenever a new IP address is added or when a new 5102d452930fSRichard Haines * primary address is selected. 5103d452930fSRichard Haines * Note that an SCTP connect(2) call happens before 5104d452930fSRichard Haines * the SCTP protocol layer and is handled via 5105d452930fSRichard Haines * selinux_socket_connect(). 5106d452930fSRichard Haines */ 5107d452930fSRichard Haines err = selinux_netlbl_socket_connect_locked(sk, addr); 5108d452930fSRichard Haines break; 5109d452930fSRichard Haines } 5110d452930fSRichard Haines 5111d452930fSRichard Haines if (err) 5112d452930fSRichard Haines return err; 5113d452930fSRichard Haines 5114d452930fSRichard Haines addr_buf += len; 5115d452930fSRichard Haines walk_size += len; 5116d452930fSRichard Haines } 5117d452930fSRichard Haines 5118d452930fSRichard Haines return 0; 5119d452930fSRichard Haines } 5120d452930fSRichard Haines 5121d452930fSRichard Haines /* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */ 5122d452930fSRichard Haines static void selinux_sctp_sk_clone(struct sctp_endpoint *ep, struct sock *sk, 5123d452930fSRichard Haines struct sock *newsk) 5124d452930fSRichard Haines { 5125d452930fSRichard Haines struct sk_security_struct *sksec = sk->sk_security; 5126d452930fSRichard Haines struct sk_security_struct *newsksec = newsk->sk_security; 5127d452930fSRichard Haines 5128d452930fSRichard Haines /* If policy does not support SECCLASS_SCTP_SOCKET then call 5129d452930fSRichard Haines * the non-sctp clone version. 5130d452930fSRichard Haines */ 5131aa8e712cSStephen Smalley if (!selinux_policycap_extsockclass()) 5132d452930fSRichard Haines return selinux_sk_clone_security(sk, newsk); 5133d452930fSRichard Haines 5134d452930fSRichard Haines newsksec->sid = ep->secid; 5135d452930fSRichard Haines newsksec->peer_sid = ep->peer_secid; 5136d452930fSRichard Haines newsksec->sclass = sksec->sclass; 5137d452930fSRichard Haines selinux_netlbl_sctp_sk_clone(sk, newsk); 5138d452930fSRichard Haines } 5139d452930fSRichard Haines 51409a673e56SAdrian Bunk static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb, 51414237c75cSVenkat Yekkirala struct request_sock *req) 51424237c75cSVenkat Yekkirala { 51434237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 51444237c75cSVenkat Yekkirala int err; 51450b1f24e6SPaul Moore u16 family = req->rsk_ops->family; 5146446b8024SPaul Moore u32 connsid; 51474237c75cSVenkat Yekkirala u32 peersid; 51484237c75cSVenkat Yekkirala 5149aa862900SPaul Moore err = selinux_skb_peerlbl_sid(skb, family, &peersid); 5150220deb96SPaul Moore if (err) 5151220deb96SPaul Moore return err; 5152446b8024SPaul Moore err = selinux_conn_sid(sksec->sid, peersid, &connsid); 51534237c75cSVenkat Yekkirala if (err) 51544237c75cSVenkat Yekkirala return err; 5155446b8024SPaul Moore req->secid = connsid; 51566b877699SVenkat Yekkirala req->peer_secid = peersid; 5157389fb800SPaul Moore 5158389fb800SPaul Moore return selinux_netlbl_inet_conn_request(req, family); 51594237c75cSVenkat Yekkirala } 51604237c75cSVenkat Yekkirala 51619a673e56SAdrian Bunk static void selinux_inet_csk_clone(struct sock *newsk, 51629a673e56SAdrian Bunk const struct request_sock *req) 51634237c75cSVenkat Yekkirala { 51644237c75cSVenkat Yekkirala struct sk_security_struct *newsksec = newsk->sk_security; 51654237c75cSVenkat Yekkirala 51664237c75cSVenkat Yekkirala newsksec->sid = req->secid; 51676b877699SVenkat Yekkirala newsksec->peer_sid = req->peer_secid; 51684237c75cSVenkat Yekkirala /* NOTE: Ideally, we should also get the isec->sid for the 51694237c75cSVenkat Yekkirala new socket in sync, but we don't have the isec available yet. 51704237c75cSVenkat Yekkirala So we will wait until sock_graft to do it, by which 51714237c75cSVenkat Yekkirala time it will have been created and available. */ 517299f59ed0SPaul Moore 51739f2ad665SPaul Moore /* We don't need to take any sort of lock here as we are the only 51749f2ad665SPaul Moore * thread with access to newsksec */ 5175389fb800SPaul Moore selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family); 51764237c75cSVenkat Yekkirala } 51774237c75cSVenkat Yekkirala 5178014ab19aSPaul Moore static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb) 51796b877699SVenkat Yekkirala { 5180aa862900SPaul Moore u16 family = sk->sk_family; 51816b877699SVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 51826b877699SVenkat Yekkirala 5183aa862900SPaul Moore /* handle mapped IPv4 packets arriving via IPv6 sockets */ 5184aa862900SPaul Moore if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 5185aa862900SPaul Moore family = PF_INET; 5186aa862900SPaul Moore 5187aa862900SPaul Moore selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid); 51886b877699SVenkat Yekkirala } 51896b877699SVenkat Yekkirala 51902606fd1fSEric Paris static int selinux_secmark_relabel_packet(u32 sid) 51912606fd1fSEric Paris { 51922606fd1fSEric Paris const struct task_security_struct *__tsec; 51932606fd1fSEric Paris u32 tsid; 51942606fd1fSEric Paris 51950c6cfa62SCasey Schaufler __tsec = selinux_cred(current_cred()); 51962606fd1fSEric Paris tsid = __tsec->sid; 51972606fd1fSEric Paris 51986b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 51996b6bc620SStephen Smalley tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, 52006b6bc620SStephen Smalley NULL); 52012606fd1fSEric Paris } 52022606fd1fSEric Paris 52032606fd1fSEric Paris static void selinux_secmark_refcount_inc(void) 52042606fd1fSEric Paris { 52052606fd1fSEric Paris atomic_inc(&selinux_secmark_refcount); 52062606fd1fSEric Paris } 52072606fd1fSEric Paris 52082606fd1fSEric Paris static void selinux_secmark_refcount_dec(void) 52092606fd1fSEric Paris { 52102606fd1fSEric Paris atomic_dec(&selinux_secmark_refcount); 52112606fd1fSEric Paris } 52122606fd1fSEric Paris 52139a673e56SAdrian Bunk static void selinux_req_classify_flow(const struct request_sock *req, 52149a673e56SAdrian Bunk struct flowi *fl) 52154237c75cSVenkat Yekkirala { 52161d28f42cSDavid S. Miller fl->flowi_secid = req->secid; 52174237c75cSVenkat Yekkirala } 52184237c75cSVenkat Yekkirala 52195dbbaf2dSPaul Moore static int selinux_tun_dev_alloc_security(void **security) 52205dbbaf2dSPaul Moore { 52215dbbaf2dSPaul Moore struct tun_security_struct *tunsec; 52225dbbaf2dSPaul Moore 52235dbbaf2dSPaul Moore tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL); 52245dbbaf2dSPaul Moore if (!tunsec) 52255dbbaf2dSPaul Moore return -ENOMEM; 52265dbbaf2dSPaul Moore tunsec->sid = current_sid(); 52275dbbaf2dSPaul Moore 52285dbbaf2dSPaul Moore *security = tunsec; 52295dbbaf2dSPaul Moore return 0; 52305dbbaf2dSPaul Moore } 52315dbbaf2dSPaul Moore 52325dbbaf2dSPaul Moore static void selinux_tun_dev_free_security(void *security) 52335dbbaf2dSPaul Moore { 52345dbbaf2dSPaul Moore kfree(security); 52355dbbaf2dSPaul Moore } 52365dbbaf2dSPaul Moore 5237ed6d76e4SPaul Moore static int selinux_tun_dev_create(void) 5238ed6d76e4SPaul Moore { 5239ed6d76e4SPaul Moore u32 sid = current_sid(); 5240ed6d76e4SPaul Moore 5241ed6d76e4SPaul Moore /* we aren't taking into account the "sockcreate" SID since the socket 5242ed6d76e4SPaul Moore * that is being created here is not a socket in the traditional sense, 5243ed6d76e4SPaul Moore * instead it is a private sock, accessible only to the kernel, and 5244ed6d76e4SPaul Moore * representing a wide range of network traffic spanning multiple 5245ed6d76e4SPaul Moore * connections unlike traditional sockets - check the TUN driver to 5246ed6d76e4SPaul Moore * get a better understanding of why this socket is special */ 5247ed6d76e4SPaul Moore 52486b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 52496b6bc620SStephen Smalley sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE, 5250ed6d76e4SPaul Moore NULL); 5251ed6d76e4SPaul Moore } 5252ed6d76e4SPaul Moore 52535dbbaf2dSPaul Moore static int selinux_tun_dev_attach_queue(void *security) 5254ed6d76e4SPaul Moore { 52555dbbaf2dSPaul Moore struct tun_security_struct *tunsec = security; 52565dbbaf2dSPaul Moore 52576b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 52586b6bc620SStephen Smalley current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET, 52595dbbaf2dSPaul Moore TUN_SOCKET__ATTACH_QUEUE, NULL); 52605dbbaf2dSPaul Moore } 52615dbbaf2dSPaul Moore 52625dbbaf2dSPaul Moore static int selinux_tun_dev_attach(struct sock *sk, void *security) 52635dbbaf2dSPaul Moore { 52645dbbaf2dSPaul Moore struct tun_security_struct *tunsec = security; 5265ed6d76e4SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 5266ed6d76e4SPaul Moore 5267ed6d76e4SPaul Moore /* we don't currently perform any NetLabel based labeling here and it 5268ed6d76e4SPaul Moore * isn't clear that we would want to do so anyway; while we could apply 5269ed6d76e4SPaul Moore * labeling without the support of the TUN user the resulting labeled 5270ed6d76e4SPaul Moore * traffic from the other end of the connection would almost certainly 5271ed6d76e4SPaul Moore * cause confusion to the TUN user that had no idea network labeling 5272ed6d76e4SPaul Moore * protocols were being used */ 5273ed6d76e4SPaul Moore 52745dbbaf2dSPaul Moore sksec->sid = tunsec->sid; 5275ed6d76e4SPaul Moore sksec->sclass = SECCLASS_TUN_SOCKET; 52765dbbaf2dSPaul Moore 52775dbbaf2dSPaul Moore return 0; 5278ed6d76e4SPaul Moore } 5279ed6d76e4SPaul Moore 52805dbbaf2dSPaul Moore static int selinux_tun_dev_open(void *security) 5281ed6d76e4SPaul Moore { 52825dbbaf2dSPaul Moore struct tun_security_struct *tunsec = security; 5283ed6d76e4SPaul Moore u32 sid = current_sid(); 5284ed6d76e4SPaul Moore int err; 5285ed6d76e4SPaul Moore 52866b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 52876b6bc620SStephen Smalley sid, tunsec->sid, SECCLASS_TUN_SOCKET, 5288ed6d76e4SPaul Moore TUN_SOCKET__RELABELFROM, NULL); 5289ed6d76e4SPaul Moore if (err) 5290ed6d76e4SPaul Moore return err; 52916b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 52926b6bc620SStephen Smalley sid, sid, SECCLASS_TUN_SOCKET, 5293ed6d76e4SPaul Moore TUN_SOCKET__RELABELTO, NULL); 5294ed6d76e4SPaul Moore if (err) 5295ed6d76e4SPaul Moore return err; 52965dbbaf2dSPaul Moore tunsec->sid = sid; 5297ed6d76e4SPaul Moore 5298ed6d76e4SPaul Moore return 0; 5299ed6d76e4SPaul Moore } 5300ed6d76e4SPaul Moore 53011da177e4SLinus Torvalds static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb) 53021da177e4SLinus Torvalds { 53031da177e4SLinus Torvalds int err = 0; 53041da177e4SLinus Torvalds u32 perm; 53051da177e4SLinus Torvalds struct nlmsghdr *nlh; 5306253bfae6SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 53071da177e4SLinus Torvalds 530877954983SHong zhi guo if (skb->len < NLMSG_HDRLEN) { 53091da177e4SLinus Torvalds err = -EINVAL; 53101da177e4SLinus Torvalds goto out; 53111da177e4SLinus Torvalds } 5312b529ccf2SArnaldo Carvalho de Melo nlh = nlmsg_hdr(skb); 53131da177e4SLinus Torvalds 5314253bfae6SPaul Moore err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm); 53151da177e4SLinus Torvalds if (err) { 53161da177e4SLinus Torvalds if (err == -EINVAL) { 531776319946SVladis Dronov pr_warn_ratelimited("SELinux: unrecognized netlink" 531876319946SVladis Dronov " message: protocol=%hu nlmsg_type=%hu sclass=%s" 531976319946SVladis Dronov " pig=%d comm=%s\n", 5320cded3fffSMarek Milkovic sk->sk_protocol, nlh->nlmsg_type, 532176319946SVladis Dronov secclass_map[sksec->sclass - 1].name, 532276319946SVladis Dronov task_pid_nr(current), current->comm); 5323e5a5ca96SPaul Moore if (!enforcing_enabled(&selinux_state) || 5324aa8e712cSStephen Smalley security_get_allow_unknown(&selinux_state)) 53251da177e4SLinus Torvalds err = 0; 53261da177e4SLinus Torvalds } 53271da177e4SLinus Torvalds 53281da177e4SLinus Torvalds /* Ignore */ 53291da177e4SLinus Torvalds if (err == -ENOENT) 53301da177e4SLinus Torvalds err = 0; 53311da177e4SLinus Torvalds goto out; 53321da177e4SLinus Torvalds } 53331da177e4SLinus Torvalds 5334be0554c9SStephen Smalley err = sock_has_perm(sk, perm); 53351da177e4SLinus Torvalds out: 53361da177e4SLinus Torvalds return err; 53371da177e4SLinus Torvalds } 53381da177e4SLinus Torvalds 53391da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER 53401da177e4SLinus Torvalds 5341cbe0d6e8SPaul Moore static unsigned int selinux_ip_forward(struct sk_buff *skb, 5342cbe0d6e8SPaul Moore const struct net_device *indev, 5343effad8dfSPaul Moore u16 family) 53441da177e4SLinus Torvalds { 5345dfaebe98SPaul Moore int err; 5346effad8dfSPaul Moore char *addrp; 5347effad8dfSPaul Moore u32 peer_sid; 53482bf49690SThomas Liu struct common_audit_data ad; 534948c62af6SEric Paris struct lsm_network_audit net = {0,}; 5350effad8dfSPaul Moore u8 secmark_active; 5351948bf85cSPaul Moore u8 netlbl_active; 5352effad8dfSPaul Moore u8 peerlbl_active; 53534237c75cSVenkat Yekkirala 5354aa8e712cSStephen Smalley if (!selinux_policycap_netpeer()) 5355effad8dfSPaul Moore return NF_ACCEPT; 53564237c75cSVenkat Yekkirala 5357effad8dfSPaul Moore secmark_active = selinux_secmark_enabled(); 5358948bf85cSPaul Moore netlbl_active = netlbl_enabled(); 53592be4d74fSChris PeBenito peerlbl_active = selinux_peerlbl_enabled(); 5360effad8dfSPaul Moore if (!secmark_active && !peerlbl_active) 5361effad8dfSPaul Moore return NF_ACCEPT; 53624237c75cSVenkat Yekkirala 5363d8395c87SPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0) 5364d8395c87SPaul Moore return NF_DROP; 5365d8395c87SPaul Moore 536650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 536748c62af6SEric Paris ad.u.net = &net; 5368cbe0d6e8SPaul Moore ad.u.net->netif = indev->ifindex; 536948c62af6SEric Paris ad.u.net->family = family; 5370effad8dfSPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0) 5371effad8dfSPaul Moore return NF_DROP; 53721da177e4SLinus Torvalds 5373dfaebe98SPaul Moore if (peerlbl_active) { 5374cbe0d6e8SPaul Moore err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex, 5375cbe0d6e8SPaul Moore addrp, family, peer_sid, &ad); 5376dfaebe98SPaul Moore if (err) { 5377a04e71f6SHuw Davies selinux_netlbl_err(skb, family, err, 1); 5378effad8dfSPaul Moore return NF_DROP; 5379dfaebe98SPaul Moore } 5380dfaebe98SPaul Moore } 5381effad8dfSPaul Moore 5382effad8dfSPaul Moore if (secmark_active) 53836b6bc620SStephen Smalley if (avc_has_perm(&selinux_state, 53846b6bc620SStephen Smalley peer_sid, skb->secmark, 5385effad8dfSPaul Moore SECCLASS_PACKET, PACKET__FORWARD_IN, &ad)) 5386effad8dfSPaul Moore return NF_DROP; 5387effad8dfSPaul Moore 5388948bf85cSPaul Moore if (netlbl_active) 5389948bf85cSPaul Moore /* we do this in the FORWARD path and not the POST_ROUTING 5390948bf85cSPaul Moore * path because we want to make sure we apply the necessary 5391948bf85cSPaul Moore * labeling before IPsec is applied so we can leverage AH 5392948bf85cSPaul Moore * protection */ 5393948bf85cSPaul Moore if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0) 5394948bf85cSPaul Moore return NF_DROP; 5395948bf85cSPaul Moore 5396effad8dfSPaul Moore return NF_ACCEPT; 5397effad8dfSPaul Moore } 5398effad8dfSPaul Moore 539906198b34SEric W. Biederman static unsigned int selinux_ipv4_forward(void *priv, 5400effad8dfSPaul Moore struct sk_buff *skb, 5401238e54c9SDavid S. Miller const struct nf_hook_state *state) 5402effad8dfSPaul Moore { 5403238e54c9SDavid S. Miller return selinux_ip_forward(skb, state->in, PF_INET); 5404effad8dfSPaul Moore } 5405effad8dfSPaul Moore 54061a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 540706198b34SEric W. Biederman static unsigned int selinux_ipv6_forward(void *priv, 5408effad8dfSPaul Moore struct sk_buff *skb, 5409238e54c9SDavid S. Miller const struct nf_hook_state *state) 5410effad8dfSPaul Moore { 5411238e54c9SDavid S. Miller return selinux_ip_forward(skb, state->in, PF_INET6); 5412effad8dfSPaul Moore } 5413effad8dfSPaul Moore #endif /* IPV6 */ 5414effad8dfSPaul Moore 5415948bf85cSPaul Moore static unsigned int selinux_ip_output(struct sk_buff *skb, 5416948bf85cSPaul Moore u16 family) 5417948bf85cSPaul Moore { 541847180068SPaul Moore struct sock *sk; 5419948bf85cSPaul Moore u32 sid; 5420948bf85cSPaul Moore 5421948bf85cSPaul Moore if (!netlbl_enabled()) 5422948bf85cSPaul Moore return NF_ACCEPT; 5423948bf85cSPaul Moore 5424948bf85cSPaul Moore /* we do this in the LOCAL_OUT path and not the POST_ROUTING path 5425948bf85cSPaul Moore * because we want to make sure we apply the necessary labeling 5426948bf85cSPaul Moore * before IPsec is applied so we can leverage AH protection */ 542747180068SPaul Moore sk = skb->sk; 542847180068SPaul Moore if (sk) { 542947180068SPaul Moore struct sk_security_struct *sksec; 543047180068SPaul Moore 5431e446f9dfSEric Dumazet if (sk_listener(sk)) 543247180068SPaul Moore /* if the socket is the listening state then this 543347180068SPaul Moore * packet is a SYN-ACK packet which means it needs to 543447180068SPaul Moore * be labeled based on the connection/request_sock and 543547180068SPaul Moore * not the parent socket. unfortunately, we can't 543647180068SPaul Moore * lookup the request_sock yet as it isn't queued on 543747180068SPaul Moore * the parent socket until after the SYN-ACK is sent. 543847180068SPaul Moore * the "solution" is to simply pass the packet as-is 543947180068SPaul Moore * as any IP option based labeling should be copied 544047180068SPaul Moore * from the initial connection request (in the IP 544147180068SPaul Moore * layer). it is far from ideal, but until we get a 544247180068SPaul Moore * security label in the packet itself this is the 544347180068SPaul Moore * best we can do. */ 544447180068SPaul Moore return NF_ACCEPT; 544547180068SPaul Moore 544647180068SPaul Moore /* standard practice, label using the parent socket */ 544747180068SPaul Moore sksec = sk->sk_security; 5448948bf85cSPaul Moore sid = sksec->sid; 5449948bf85cSPaul Moore } else 5450948bf85cSPaul Moore sid = SECINITSID_KERNEL; 5451948bf85cSPaul Moore if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0) 5452948bf85cSPaul Moore return NF_DROP; 5453948bf85cSPaul Moore 5454948bf85cSPaul Moore return NF_ACCEPT; 5455948bf85cSPaul Moore } 5456948bf85cSPaul Moore 545706198b34SEric W. Biederman static unsigned int selinux_ipv4_output(void *priv, 5458948bf85cSPaul Moore struct sk_buff *skb, 5459238e54c9SDavid S. Miller const struct nf_hook_state *state) 5460948bf85cSPaul Moore { 5461948bf85cSPaul Moore return selinux_ip_output(skb, PF_INET); 5462948bf85cSPaul Moore } 5463948bf85cSPaul Moore 54641a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 54652917f57bSHuw Davies static unsigned int selinux_ipv6_output(void *priv, 54662917f57bSHuw Davies struct sk_buff *skb, 54672917f57bSHuw Davies const struct nf_hook_state *state) 54682917f57bSHuw Davies { 54692917f57bSHuw Davies return selinux_ip_output(skb, PF_INET6); 54702917f57bSHuw Davies } 54712917f57bSHuw Davies #endif /* IPV6 */ 54722917f57bSHuw Davies 5473effad8dfSPaul Moore static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb, 5474effad8dfSPaul Moore int ifindex, 5475d8395c87SPaul Moore u16 family) 54764e5ab4cbSJames Morris { 547754abc686SEric Dumazet struct sock *sk = skb_to_full_sk(skb); 54784237c75cSVenkat Yekkirala struct sk_security_struct *sksec; 54792bf49690SThomas Liu struct common_audit_data ad; 548048c62af6SEric Paris struct lsm_network_audit net = {0,}; 5481d8395c87SPaul Moore char *addrp; 5482d8395c87SPaul Moore u8 proto; 54834e5ab4cbSJames Morris 5484effad8dfSPaul Moore if (sk == NULL) 5485effad8dfSPaul Moore return NF_ACCEPT; 54864237c75cSVenkat Yekkirala sksec = sk->sk_security; 54874e5ab4cbSJames Morris 548850c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 548948c62af6SEric Paris ad.u.net = &net; 549048c62af6SEric Paris ad.u.net->netif = ifindex; 549148c62af6SEric Paris ad.u.net->family = family; 5492d8395c87SPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto)) 5493d8395c87SPaul Moore return NF_DROP; 5494d8395c87SPaul Moore 549558bfbb51SPaul Moore if (selinux_secmark_enabled()) 54966b6bc620SStephen Smalley if (avc_has_perm(&selinux_state, 54976b6bc620SStephen Smalley sksec->sid, skb->secmark, 5498d8395c87SPaul Moore SECCLASS_PACKET, PACKET__SEND, &ad)) 54992fe66ec2SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 55001da177e4SLinus Torvalds 5501d8395c87SPaul Moore if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto)) 55022fe66ec2SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 5503effad8dfSPaul Moore 5504effad8dfSPaul Moore return NF_ACCEPT; 5505effad8dfSPaul Moore } 5506effad8dfSPaul Moore 5507cbe0d6e8SPaul Moore static unsigned int selinux_ip_postroute(struct sk_buff *skb, 5508cbe0d6e8SPaul Moore const struct net_device *outdev, 5509effad8dfSPaul Moore u16 family) 5510effad8dfSPaul Moore { 5511effad8dfSPaul Moore u32 secmark_perm; 5512effad8dfSPaul Moore u32 peer_sid; 5513cbe0d6e8SPaul Moore int ifindex = outdev->ifindex; 5514effad8dfSPaul Moore struct sock *sk; 55152bf49690SThomas Liu struct common_audit_data ad; 551648c62af6SEric Paris struct lsm_network_audit net = {0,}; 5517effad8dfSPaul Moore char *addrp; 5518effad8dfSPaul Moore u8 secmark_active; 5519effad8dfSPaul Moore u8 peerlbl_active; 5520effad8dfSPaul Moore 5521effad8dfSPaul Moore /* If any sort of compatibility mode is enabled then handoff processing 5522effad8dfSPaul Moore * to the selinux_ip_postroute_compat() function to deal with the 5523effad8dfSPaul Moore * special handling. We do this in an attempt to keep this function 5524effad8dfSPaul Moore * as fast and as clean as possible. */ 5525aa8e712cSStephen Smalley if (!selinux_policycap_netpeer()) 5526d8395c87SPaul Moore return selinux_ip_postroute_compat(skb, ifindex, family); 5527c0828e50SPaul Moore 5528effad8dfSPaul Moore secmark_active = selinux_secmark_enabled(); 55292be4d74fSChris PeBenito peerlbl_active = selinux_peerlbl_enabled(); 5530effad8dfSPaul Moore if (!secmark_active && !peerlbl_active) 5531effad8dfSPaul Moore return NF_ACCEPT; 5532effad8dfSPaul Moore 553354abc686SEric Dumazet sk = skb_to_full_sk(skb); 5534c0828e50SPaul Moore 5535effad8dfSPaul Moore #ifdef CONFIG_XFRM 5536effad8dfSPaul Moore /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec 5537effad8dfSPaul Moore * packet transformation so allow the packet to pass without any checks 5538effad8dfSPaul Moore * since we'll have another chance to perform access control checks 5539effad8dfSPaul Moore * when the packet is on it's final way out. 5540effad8dfSPaul Moore * NOTE: there appear to be some IPv6 multicast cases where skb->dst 5541c0828e50SPaul Moore * is NULL, in this case go ahead and apply access control. 5542c0828e50SPaul Moore * NOTE: if this is a local socket (skb->sk != NULL) that is in the 5543c0828e50SPaul Moore * TCP listening state we cannot wait until the XFRM processing 5544c0828e50SPaul Moore * is done as we will miss out on the SA label if we do; 5545c0828e50SPaul Moore * unfortunately, this means more work, but it is only once per 5546c0828e50SPaul Moore * connection. */ 5547c0828e50SPaul Moore if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL && 5548e446f9dfSEric Dumazet !(sk && sk_listener(sk))) 5549effad8dfSPaul Moore return NF_ACCEPT; 5550effad8dfSPaul Moore #endif 5551effad8dfSPaul Moore 5552d8395c87SPaul Moore if (sk == NULL) { 5553446b8024SPaul Moore /* Without an associated socket the packet is either coming 5554446b8024SPaul Moore * from the kernel or it is being forwarded; check the packet 5555446b8024SPaul Moore * to determine which and if the packet is being forwarded 5556446b8024SPaul Moore * query the packet directly to determine the security label. */ 55574a7ab3dcSSteffen Klassert if (skb->skb_iif) { 5558d8395c87SPaul Moore secmark_perm = PACKET__FORWARD_OUT; 5559d8395c87SPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &peer_sid)) 556004f6d70fSEric Paris return NF_DROP; 55614a7ab3dcSSteffen Klassert } else { 55624a7ab3dcSSteffen Klassert secmark_perm = PACKET__SEND; 5563d8395c87SPaul Moore peer_sid = SECINITSID_KERNEL; 55644a7ab3dcSSteffen Klassert } 5565e446f9dfSEric Dumazet } else if (sk_listener(sk)) { 5566446b8024SPaul Moore /* Locally generated packet but the associated socket is in the 5567446b8024SPaul Moore * listening state which means this is a SYN-ACK packet. In 5568446b8024SPaul Moore * this particular case the correct security label is assigned 5569446b8024SPaul Moore * to the connection/request_sock but unfortunately we can't 5570446b8024SPaul Moore * query the request_sock as it isn't queued on the parent 5571446b8024SPaul Moore * socket until after the SYN-ACK packet is sent; the only 5572446b8024SPaul Moore * viable choice is to regenerate the label like we do in 5573446b8024SPaul Moore * selinux_inet_conn_request(). See also selinux_ip_output() 5574446b8024SPaul Moore * for similar problems. */ 5575446b8024SPaul Moore u32 skb_sid; 5576e446f9dfSEric Dumazet struct sk_security_struct *sksec; 5577e446f9dfSEric Dumazet 5578e446f9dfSEric Dumazet sksec = sk->sk_security; 5579446b8024SPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &skb_sid)) 5580446b8024SPaul Moore return NF_DROP; 5581c0828e50SPaul Moore /* At this point, if the returned skb peerlbl is SECSID_NULL 5582c0828e50SPaul Moore * and the packet has been through at least one XFRM 5583c0828e50SPaul Moore * transformation then we must be dealing with the "final" 5584c0828e50SPaul Moore * form of labeled IPsec packet; since we've already applied 5585c0828e50SPaul Moore * all of our access controls on this packet we can safely 5586c0828e50SPaul Moore * pass the packet. */ 5587c0828e50SPaul Moore if (skb_sid == SECSID_NULL) { 5588c0828e50SPaul Moore switch (family) { 5589c0828e50SPaul Moore case PF_INET: 5590c0828e50SPaul Moore if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) 5591c0828e50SPaul Moore return NF_ACCEPT; 5592c0828e50SPaul Moore break; 5593c0828e50SPaul Moore case PF_INET6: 5594c0828e50SPaul Moore if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) 5595c0828e50SPaul Moore return NF_ACCEPT; 5596a7a91a19SPaul Moore break; 5597c0828e50SPaul Moore default: 5598c0828e50SPaul Moore return NF_DROP_ERR(-ECONNREFUSED); 5599c0828e50SPaul Moore } 5600c0828e50SPaul Moore } 5601446b8024SPaul Moore if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid)) 5602446b8024SPaul Moore return NF_DROP; 5603446b8024SPaul Moore secmark_perm = PACKET__SEND; 5604d8395c87SPaul Moore } else { 5605446b8024SPaul Moore /* Locally generated packet, fetch the security label from the 5606446b8024SPaul Moore * associated socket. */ 5607effad8dfSPaul Moore struct sk_security_struct *sksec = sk->sk_security; 5608effad8dfSPaul Moore peer_sid = sksec->sid; 5609effad8dfSPaul Moore secmark_perm = PACKET__SEND; 5610effad8dfSPaul Moore } 5611effad8dfSPaul Moore 561250c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 561348c62af6SEric Paris ad.u.net = &net; 561448c62af6SEric Paris ad.u.net->netif = ifindex; 561548c62af6SEric Paris ad.u.net->family = family; 5616d8395c87SPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL)) 561704f6d70fSEric Paris return NF_DROP; 5618d8395c87SPaul Moore 5619effad8dfSPaul Moore if (secmark_active) 56206b6bc620SStephen Smalley if (avc_has_perm(&selinux_state, 56216b6bc620SStephen Smalley peer_sid, skb->secmark, 5622effad8dfSPaul Moore SECCLASS_PACKET, secmark_perm, &ad)) 56231f1aaf82SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 5624effad8dfSPaul Moore 5625effad8dfSPaul Moore if (peerlbl_active) { 5626effad8dfSPaul Moore u32 if_sid; 5627effad8dfSPaul Moore u32 node_sid; 5628effad8dfSPaul Moore 5629cbe0d6e8SPaul Moore if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid)) 563004f6d70fSEric Paris return NF_DROP; 56316b6bc620SStephen Smalley if (avc_has_perm(&selinux_state, 56326b6bc620SStephen Smalley peer_sid, if_sid, 5633effad8dfSPaul Moore SECCLASS_NETIF, NETIF__EGRESS, &ad)) 56341f1aaf82SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 5635effad8dfSPaul Moore 5636effad8dfSPaul Moore if (sel_netnode_sid(addrp, family, &node_sid)) 563704f6d70fSEric Paris return NF_DROP; 56386b6bc620SStephen Smalley if (avc_has_perm(&selinux_state, 56396b6bc620SStephen Smalley peer_sid, node_sid, 5640effad8dfSPaul Moore SECCLASS_NODE, NODE__SENDTO, &ad)) 56411f1aaf82SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 5642effad8dfSPaul Moore } 5643effad8dfSPaul Moore 5644effad8dfSPaul Moore return NF_ACCEPT; 5645effad8dfSPaul Moore } 5646effad8dfSPaul Moore 564706198b34SEric W. Biederman static unsigned int selinux_ipv4_postroute(void *priv, 5648a224be76SDavid S. Miller struct sk_buff *skb, 5649238e54c9SDavid S. Miller const struct nf_hook_state *state) 56501da177e4SLinus Torvalds { 5651238e54c9SDavid S. Miller return selinux_ip_postroute(skb, state->out, PF_INET); 56521da177e4SLinus Torvalds } 56531da177e4SLinus Torvalds 56541a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 565506198b34SEric W. Biederman static unsigned int selinux_ipv6_postroute(void *priv, 5656a224be76SDavid S. Miller struct sk_buff *skb, 5657238e54c9SDavid S. Miller const struct nf_hook_state *state) 56581da177e4SLinus Torvalds { 5659238e54c9SDavid S. Miller return selinux_ip_postroute(skb, state->out, PF_INET6); 56601da177e4SLinus Torvalds } 56611da177e4SLinus Torvalds #endif /* IPV6 */ 56621da177e4SLinus Torvalds 56631da177e4SLinus Torvalds #endif /* CONFIG_NETFILTER */ 56641da177e4SLinus Torvalds 56651da177e4SLinus Torvalds static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb) 56661da177e4SLinus Torvalds { 5667941fc5b2SStephen Smalley return selinux_nlmsg_perm(sk, skb); 56681da177e4SLinus Torvalds } 56691da177e4SLinus Torvalds 5670be0554c9SStephen Smalley static int ipc_alloc_security(struct kern_ipc_perm *perm, 56711da177e4SLinus Torvalds u16 sclass) 56721da177e4SLinus Torvalds { 56731da177e4SLinus Torvalds struct ipc_security_struct *isec; 56741da177e4SLinus Torvalds 567589d155efSJames Morris isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL); 56761da177e4SLinus Torvalds if (!isec) 56771da177e4SLinus Torvalds return -ENOMEM; 56781da177e4SLinus Torvalds 56791da177e4SLinus Torvalds isec->sclass = sclass; 5680be0554c9SStephen Smalley isec->sid = current_sid(); 56811da177e4SLinus Torvalds perm->security = isec; 56821da177e4SLinus Torvalds 56831da177e4SLinus Torvalds return 0; 56841da177e4SLinus Torvalds } 56851da177e4SLinus Torvalds 56861da177e4SLinus Torvalds static void ipc_free_security(struct kern_ipc_perm *perm) 56871da177e4SLinus Torvalds { 56881da177e4SLinus Torvalds struct ipc_security_struct *isec = perm->security; 56891da177e4SLinus Torvalds perm->security = NULL; 56901da177e4SLinus Torvalds kfree(isec); 56911da177e4SLinus Torvalds } 56921da177e4SLinus Torvalds 56931da177e4SLinus Torvalds static int msg_msg_alloc_security(struct msg_msg *msg) 56941da177e4SLinus Torvalds { 56951da177e4SLinus Torvalds struct msg_security_struct *msec; 56961da177e4SLinus Torvalds 569789d155efSJames Morris msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL); 56981da177e4SLinus Torvalds if (!msec) 56991da177e4SLinus Torvalds return -ENOMEM; 57001da177e4SLinus Torvalds 57011da177e4SLinus Torvalds msec->sid = SECINITSID_UNLABELED; 57021da177e4SLinus Torvalds msg->security = msec; 57031da177e4SLinus Torvalds 57041da177e4SLinus Torvalds return 0; 57051da177e4SLinus Torvalds } 57061da177e4SLinus Torvalds 57071da177e4SLinus Torvalds static void msg_msg_free_security(struct msg_msg *msg) 57081da177e4SLinus Torvalds { 57091da177e4SLinus Torvalds struct msg_security_struct *msec = msg->security; 57101da177e4SLinus Torvalds 57111da177e4SLinus Torvalds msg->security = NULL; 57121da177e4SLinus Torvalds kfree(msec); 57131da177e4SLinus Torvalds } 57141da177e4SLinus Torvalds 57151da177e4SLinus Torvalds static int ipc_has_perm(struct kern_ipc_perm *ipc_perms, 57166af963f1SStephen Smalley u32 perms) 57171da177e4SLinus Torvalds { 57181da177e4SLinus Torvalds struct ipc_security_struct *isec; 57192bf49690SThomas Liu struct common_audit_data ad; 5720275bb41eSDavid Howells u32 sid = current_sid(); 57211da177e4SLinus Torvalds 57221da177e4SLinus Torvalds isec = ipc_perms->security; 57231da177e4SLinus Torvalds 572450c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 57251da177e4SLinus Torvalds ad.u.ipc_id = ipc_perms->key; 57261da177e4SLinus Torvalds 57276b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 57286b6bc620SStephen Smalley sid, isec->sid, isec->sclass, perms, &ad); 57291da177e4SLinus Torvalds } 57301da177e4SLinus Torvalds 57311da177e4SLinus Torvalds static int selinux_msg_msg_alloc_security(struct msg_msg *msg) 57321da177e4SLinus Torvalds { 57331da177e4SLinus Torvalds return msg_msg_alloc_security(msg); 57341da177e4SLinus Torvalds } 57351da177e4SLinus Torvalds 57361da177e4SLinus Torvalds static void selinux_msg_msg_free_security(struct msg_msg *msg) 57371da177e4SLinus Torvalds { 57381da177e4SLinus Torvalds msg_msg_free_security(msg); 57391da177e4SLinus Torvalds } 57401da177e4SLinus Torvalds 57411da177e4SLinus Torvalds /* message queue security operations */ 5742d8c6e854SEric W. Biederman static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq) 57431da177e4SLinus Torvalds { 57441da177e4SLinus Torvalds struct ipc_security_struct *isec; 57452bf49690SThomas Liu struct common_audit_data ad; 5746275bb41eSDavid Howells u32 sid = current_sid(); 57471da177e4SLinus Torvalds int rc; 57481da177e4SLinus Torvalds 5749d8c6e854SEric W. Biederman rc = ipc_alloc_security(msq, SECCLASS_MSGQ); 57501da177e4SLinus Torvalds if (rc) 57511da177e4SLinus Torvalds return rc; 57521da177e4SLinus Torvalds 5753d8c6e854SEric W. Biederman isec = msq->security; 57541da177e4SLinus Torvalds 575550c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 5756d8c6e854SEric W. Biederman ad.u.ipc_id = msq->key; 57571da177e4SLinus Torvalds 57586b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 57596b6bc620SStephen Smalley sid, isec->sid, SECCLASS_MSGQ, 57601da177e4SLinus Torvalds MSGQ__CREATE, &ad); 57611da177e4SLinus Torvalds if (rc) { 5762d8c6e854SEric W. Biederman ipc_free_security(msq); 57631da177e4SLinus Torvalds return rc; 57641da177e4SLinus Torvalds } 57651da177e4SLinus Torvalds return 0; 57661da177e4SLinus Torvalds } 57671da177e4SLinus Torvalds 5768d8c6e854SEric W. Biederman static void selinux_msg_queue_free_security(struct kern_ipc_perm *msq) 57691da177e4SLinus Torvalds { 5770d8c6e854SEric W. Biederman ipc_free_security(msq); 57711da177e4SLinus Torvalds } 57721da177e4SLinus Torvalds 5773d8c6e854SEric W. Biederman static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg) 57741da177e4SLinus Torvalds { 57751da177e4SLinus Torvalds struct ipc_security_struct *isec; 57762bf49690SThomas Liu struct common_audit_data ad; 5777275bb41eSDavid Howells u32 sid = current_sid(); 57781da177e4SLinus Torvalds 5779d8c6e854SEric W. Biederman isec = msq->security; 57801da177e4SLinus Torvalds 578150c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 5782d8c6e854SEric W. Biederman ad.u.ipc_id = msq->key; 57831da177e4SLinus Torvalds 57846b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 57856b6bc620SStephen Smalley sid, isec->sid, SECCLASS_MSGQ, 57861da177e4SLinus Torvalds MSGQ__ASSOCIATE, &ad); 57871da177e4SLinus Torvalds } 57881da177e4SLinus Torvalds 5789d8c6e854SEric W. Biederman static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd) 57901da177e4SLinus Torvalds { 57911da177e4SLinus Torvalds int err; 57921da177e4SLinus Torvalds int perms; 57931da177e4SLinus Torvalds 57941da177e4SLinus Torvalds switch (cmd) { 57951da177e4SLinus Torvalds case IPC_INFO: 57961da177e4SLinus Torvalds case MSG_INFO: 57971da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 57986b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 57996b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, 5800be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); 58011da177e4SLinus Torvalds case IPC_STAT: 58021da177e4SLinus Torvalds case MSG_STAT: 580323c8cec8SDavidlohr Bueso case MSG_STAT_ANY: 58041da177e4SLinus Torvalds perms = MSGQ__GETATTR | MSGQ__ASSOCIATE; 58051da177e4SLinus Torvalds break; 58061da177e4SLinus Torvalds case IPC_SET: 58071da177e4SLinus Torvalds perms = MSGQ__SETATTR; 58081da177e4SLinus Torvalds break; 58091da177e4SLinus Torvalds case IPC_RMID: 58101da177e4SLinus Torvalds perms = MSGQ__DESTROY; 58111da177e4SLinus Torvalds break; 58121da177e4SLinus Torvalds default: 58131da177e4SLinus Torvalds return 0; 58141da177e4SLinus Torvalds } 58151da177e4SLinus Torvalds 5816d8c6e854SEric W. Biederman err = ipc_has_perm(msq, perms); 58171da177e4SLinus Torvalds return err; 58181da177e4SLinus Torvalds } 58191da177e4SLinus Torvalds 5820d8c6e854SEric W. Biederman static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg) 58211da177e4SLinus Torvalds { 58221da177e4SLinus Torvalds struct ipc_security_struct *isec; 58231da177e4SLinus Torvalds struct msg_security_struct *msec; 58242bf49690SThomas Liu struct common_audit_data ad; 5825275bb41eSDavid Howells u32 sid = current_sid(); 58261da177e4SLinus Torvalds int rc; 58271da177e4SLinus Torvalds 5828d8c6e854SEric W. Biederman isec = msq->security; 58291da177e4SLinus Torvalds msec = msg->security; 58301da177e4SLinus Torvalds 58311da177e4SLinus Torvalds /* 58321da177e4SLinus Torvalds * First time through, need to assign label to the message 58331da177e4SLinus Torvalds */ 58341da177e4SLinus Torvalds if (msec->sid == SECINITSID_UNLABELED) { 58351da177e4SLinus Torvalds /* 58361da177e4SLinus Torvalds * Compute new sid based on current process and 58371da177e4SLinus Torvalds * message queue this message will be stored in 58381da177e4SLinus Torvalds */ 5839aa8e712cSStephen Smalley rc = security_transition_sid(&selinux_state, sid, isec->sid, 5840aa8e712cSStephen Smalley SECCLASS_MSG, NULL, &msec->sid); 58411da177e4SLinus Torvalds if (rc) 58421da177e4SLinus Torvalds return rc; 58431da177e4SLinus Torvalds } 58441da177e4SLinus Torvalds 584550c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 5846d8c6e854SEric W. Biederman ad.u.ipc_id = msq->key; 58471da177e4SLinus Torvalds 58481da177e4SLinus Torvalds /* Can this process write to the queue? */ 58496b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 58506b6bc620SStephen Smalley sid, isec->sid, SECCLASS_MSGQ, 58511da177e4SLinus Torvalds MSGQ__WRITE, &ad); 58521da177e4SLinus Torvalds if (!rc) 58531da177e4SLinus Torvalds /* Can this process send the message */ 58546b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 58556b6bc620SStephen Smalley sid, msec->sid, SECCLASS_MSG, 5856275bb41eSDavid Howells MSG__SEND, &ad); 58571da177e4SLinus Torvalds if (!rc) 58581da177e4SLinus Torvalds /* Can the message be put in the queue? */ 58596b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 58606b6bc620SStephen Smalley msec->sid, isec->sid, SECCLASS_MSGQ, 5861275bb41eSDavid Howells MSGQ__ENQUEUE, &ad); 58621da177e4SLinus Torvalds 58631da177e4SLinus Torvalds return rc; 58641da177e4SLinus Torvalds } 58651da177e4SLinus Torvalds 5866d8c6e854SEric W. Biederman static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg, 58671da177e4SLinus Torvalds struct task_struct *target, 58681da177e4SLinus Torvalds long type, int mode) 58691da177e4SLinus Torvalds { 58701da177e4SLinus Torvalds struct ipc_security_struct *isec; 58711da177e4SLinus Torvalds struct msg_security_struct *msec; 58722bf49690SThomas Liu struct common_audit_data ad; 5873275bb41eSDavid Howells u32 sid = task_sid(target); 58741da177e4SLinus Torvalds int rc; 58751da177e4SLinus Torvalds 5876d8c6e854SEric W. Biederman isec = msq->security; 58771da177e4SLinus Torvalds msec = msg->security; 58781da177e4SLinus Torvalds 587950c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 5880d8c6e854SEric W. Biederman ad.u.ipc_id = msq->key; 58811da177e4SLinus Torvalds 58826b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 58836b6bc620SStephen Smalley sid, isec->sid, 58841da177e4SLinus Torvalds SECCLASS_MSGQ, MSGQ__READ, &ad); 58851da177e4SLinus Torvalds if (!rc) 58866b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 58876b6bc620SStephen Smalley sid, msec->sid, 58881da177e4SLinus Torvalds SECCLASS_MSG, MSG__RECEIVE, &ad); 58891da177e4SLinus Torvalds return rc; 58901da177e4SLinus Torvalds } 58911da177e4SLinus Torvalds 58921da177e4SLinus Torvalds /* Shared Memory security operations */ 58937191adffSEric W. Biederman static int selinux_shm_alloc_security(struct kern_ipc_perm *shp) 58941da177e4SLinus Torvalds { 58951da177e4SLinus Torvalds struct ipc_security_struct *isec; 58962bf49690SThomas Liu struct common_audit_data ad; 5897275bb41eSDavid Howells u32 sid = current_sid(); 58981da177e4SLinus Torvalds int rc; 58991da177e4SLinus Torvalds 59007191adffSEric W. Biederman rc = ipc_alloc_security(shp, SECCLASS_SHM); 59011da177e4SLinus Torvalds if (rc) 59021da177e4SLinus Torvalds return rc; 59031da177e4SLinus Torvalds 59047191adffSEric W. Biederman isec = shp->security; 59051da177e4SLinus Torvalds 590650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 59077191adffSEric W. Biederman ad.u.ipc_id = shp->key; 59081da177e4SLinus Torvalds 59096b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 59106b6bc620SStephen Smalley sid, isec->sid, SECCLASS_SHM, 59111da177e4SLinus Torvalds SHM__CREATE, &ad); 59121da177e4SLinus Torvalds if (rc) { 59137191adffSEric W. Biederman ipc_free_security(shp); 59141da177e4SLinus Torvalds return rc; 59151da177e4SLinus Torvalds } 59161da177e4SLinus Torvalds return 0; 59171da177e4SLinus Torvalds } 59181da177e4SLinus Torvalds 59197191adffSEric W. Biederman static void selinux_shm_free_security(struct kern_ipc_perm *shp) 59201da177e4SLinus Torvalds { 59217191adffSEric W. Biederman ipc_free_security(shp); 59221da177e4SLinus Torvalds } 59231da177e4SLinus Torvalds 59247191adffSEric W. Biederman static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg) 59251da177e4SLinus Torvalds { 59261da177e4SLinus Torvalds struct ipc_security_struct *isec; 59272bf49690SThomas Liu struct common_audit_data ad; 5928275bb41eSDavid Howells u32 sid = current_sid(); 59291da177e4SLinus Torvalds 59307191adffSEric W. Biederman isec = shp->security; 59311da177e4SLinus Torvalds 593250c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 59337191adffSEric W. Biederman ad.u.ipc_id = shp->key; 59341da177e4SLinus Torvalds 59356b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 59366b6bc620SStephen Smalley sid, isec->sid, SECCLASS_SHM, 59371da177e4SLinus Torvalds SHM__ASSOCIATE, &ad); 59381da177e4SLinus Torvalds } 59391da177e4SLinus Torvalds 59401da177e4SLinus Torvalds /* Note, at this point, shp is locked down */ 59417191adffSEric W. Biederman static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd) 59421da177e4SLinus Torvalds { 59431da177e4SLinus Torvalds int perms; 59441da177e4SLinus Torvalds int err; 59451da177e4SLinus Torvalds 59461da177e4SLinus Torvalds switch (cmd) { 59471da177e4SLinus Torvalds case IPC_INFO: 59481da177e4SLinus Torvalds case SHM_INFO: 59491da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 59506b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 59516b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, 5952be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); 59531da177e4SLinus Torvalds case IPC_STAT: 59541da177e4SLinus Torvalds case SHM_STAT: 5955c21a6970SDavidlohr Bueso case SHM_STAT_ANY: 59561da177e4SLinus Torvalds perms = SHM__GETATTR | SHM__ASSOCIATE; 59571da177e4SLinus Torvalds break; 59581da177e4SLinus Torvalds case IPC_SET: 59591da177e4SLinus Torvalds perms = SHM__SETATTR; 59601da177e4SLinus Torvalds break; 59611da177e4SLinus Torvalds case SHM_LOCK: 59621da177e4SLinus Torvalds case SHM_UNLOCK: 59631da177e4SLinus Torvalds perms = SHM__LOCK; 59641da177e4SLinus Torvalds break; 59651da177e4SLinus Torvalds case IPC_RMID: 59661da177e4SLinus Torvalds perms = SHM__DESTROY; 59671da177e4SLinus Torvalds break; 59681da177e4SLinus Torvalds default: 59691da177e4SLinus Torvalds return 0; 59701da177e4SLinus Torvalds } 59711da177e4SLinus Torvalds 59727191adffSEric W. Biederman err = ipc_has_perm(shp, perms); 59731da177e4SLinus Torvalds return err; 59741da177e4SLinus Torvalds } 59751da177e4SLinus Torvalds 59767191adffSEric W. Biederman static int selinux_shm_shmat(struct kern_ipc_perm *shp, 59771da177e4SLinus Torvalds char __user *shmaddr, int shmflg) 59781da177e4SLinus Torvalds { 59791da177e4SLinus Torvalds u32 perms; 59801da177e4SLinus Torvalds 59811da177e4SLinus Torvalds if (shmflg & SHM_RDONLY) 59821da177e4SLinus Torvalds perms = SHM__READ; 59831da177e4SLinus Torvalds else 59841da177e4SLinus Torvalds perms = SHM__READ | SHM__WRITE; 59851da177e4SLinus Torvalds 59867191adffSEric W. Biederman return ipc_has_perm(shp, perms); 59871da177e4SLinus Torvalds } 59881da177e4SLinus Torvalds 59891da177e4SLinus Torvalds /* Semaphore security operations */ 5990aefad959SEric W. Biederman static int selinux_sem_alloc_security(struct kern_ipc_perm *sma) 59911da177e4SLinus Torvalds { 59921da177e4SLinus Torvalds struct ipc_security_struct *isec; 59932bf49690SThomas Liu struct common_audit_data ad; 5994275bb41eSDavid Howells u32 sid = current_sid(); 59951da177e4SLinus Torvalds int rc; 59961da177e4SLinus Torvalds 5997aefad959SEric W. Biederman rc = ipc_alloc_security(sma, SECCLASS_SEM); 59981da177e4SLinus Torvalds if (rc) 59991da177e4SLinus Torvalds return rc; 60001da177e4SLinus Torvalds 6001aefad959SEric W. Biederman isec = sma->security; 60021da177e4SLinus Torvalds 600350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 6004aefad959SEric W. Biederman ad.u.ipc_id = sma->key; 60051da177e4SLinus Torvalds 60066b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 60076b6bc620SStephen Smalley sid, isec->sid, SECCLASS_SEM, 60081da177e4SLinus Torvalds SEM__CREATE, &ad); 60091da177e4SLinus Torvalds if (rc) { 6010aefad959SEric W. Biederman ipc_free_security(sma); 60111da177e4SLinus Torvalds return rc; 60121da177e4SLinus Torvalds } 60131da177e4SLinus Torvalds return 0; 60141da177e4SLinus Torvalds } 60151da177e4SLinus Torvalds 6016aefad959SEric W. Biederman static void selinux_sem_free_security(struct kern_ipc_perm *sma) 60171da177e4SLinus Torvalds { 6018aefad959SEric W. Biederman ipc_free_security(sma); 60191da177e4SLinus Torvalds } 60201da177e4SLinus Torvalds 6021aefad959SEric W. Biederman static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg) 60221da177e4SLinus Torvalds { 60231da177e4SLinus Torvalds struct ipc_security_struct *isec; 60242bf49690SThomas Liu struct common_audit_data ad; 6025275bb41eSDavid Howells u32 sid = current_sid(); 60261da177e4SLinus Torvalds 6027aefad959SEric W. Biederman isec = sma->security; 60281da177e4SLinus Torvalds 602950c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 6030aefad959SEric W. Biederman ad.u.ipc_id = sma->key; 60311da177e4SLinus Torvalds 60326b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 60336b6bc620SStephen Smalley sid, isec->sid, SECCLASS_SEM, 60341da177e4SLinus Torvalds SEM__ASSOCIATE, &ad); 60351da177e4SLinus Torvalds } 60361da177e4SLinus Torvalds 60371da177e4SLinus Torvalds /* Note, at this point, sma is locked down */ 6038aefad959SEric W. Biederman static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd) 60391da177e4SLinus Torvalds { 60401da177e4SLinus Torvalds int err; 60411da177e4SLinus Torvalds u32 perms; 60421da177e4SLinus Torvalds 60431da177e4SLinus Torvalds switch (cmd) { 60441da177e4SLinus Torvalds case IPC_INFO: 60451da177e4SLinus Torvalds case SEM_INFO: 60461da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 60476b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 60486b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, 6049be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); 60501da177e4SLinus Torvalds case GETPID: 60511da177e4SLinus Torvalds case GETNCNT: 60521da177e4SLinus Torvalds case GETZCNT: 60531da177e4SLinus Torvalds perms = SEM__GETATTR; 60541da177e4SLinus Torvalds break; 60551da177e4SLinus Torvalds case GETVAL: 60561da177e4SLinus Torvalds case GETALL: 60571da177e4SLinus Torvalds perms = SEM__READ; 60581da177e4SLinus Torvalds break; 60591da177e4SLinus Torvalds case SETVAL: 60601da177e4SLinus Torvalds case SETALL: 60611da177e4SLinus Torvalds perms = SEM__WRITE; 60621da177e4SLinus Torvalds break; 60631da177e4SLinus Torvalds case IPC_RMID: 60641da177e4SLinus Torvalds perms = SEM__DESTROY; 60651da177e4SLinus Torvalds break; 60661da177e4SLinus Torvalds case IPC_SET: 60671da177e4SLinus Torvalds perms = SEM__SETATTR; 60681da177e4SLinus Torvalds break; 60691da177e4SLinus Torvalds case IPC_STAT: 60701da177e4SLinus Torvalds case SEM_STAT: 6071a280d6dcSDavidlohr Bueso case SEM_STAT_ANY: 60721da177e4SLinus Torvalds perms = SEM__GETATTR | SEM__ASSOCIATE; 60731da177e4SLinus Torvalds break; 60741da177e4SLinus Torvalds default: 60751da177e4SLinus Torvalds return 0; 60761da177e4SLinus Torvalds } 60771da177e4SLinus Torvalds 6078aefad959SEric W. Biederman err = ipc_has_perm(sma, perms); 60791da177e4SLinus Torvalds return err; 60801da177e4SLinus Torvalds } 60811da177e4SLinus Torvalds 6082aefad959SEric W. Biederman static int selinux_sem_semop(struct kern_ipc_perm *sma, 60831da177e4SLinus Torvalds struct sembuf *sops, unsigned nsops, int alter) 60841da177e4SLinus Torvalds { 60851da177e4SLinus Torvalds u32 perms; 60861da177e4SLinus Torvalds 60871da177e4SLinus Torvalds if (alter) 60881da177e4SLinus Torvalds perms = SEM__READ | SEM__WRITE; 60891da177e4SLinus Torvalds else 60901da177e4SLinus Torvalds perms = SEM__READ; 60911da177e4SLinus Torvalds 6092aefad959SEric W. Biederman return ipc_has_perm(sma, perms); 60931da177e4SLinus Torvalds } 60941da177e4SLinus Torvalds 60951da177e4SLinus Torvalds static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag) 60961da177e4SLinus Torvalds { 60971da177e4SLinus Torvalds u32 av = 0; 60981da177e4SLinus Torvalds 60991da177e4SLinus Torvalds av = 0; 61001da177e4SLinus Torvalds if (flag & S_IRUGO) 61011da177e4SLinus Torvalds av |= IPC__UNIX_READ; 61021da177e4SLinus Torvalds if (flag & S_IWUGO) 61031da177e4SLinus Torvalds av |= IPC__UNIX_WRITE; 61041da177e4SLinus Torvalds 61051da177e4SLinus Torvalds if (av == 0) 61061da177e4SLinus Torvalds return 0; 61071da177e4SLinus Torvalds 61086af963f1SStephen Smalley return ipc_has_perm(ipcp, av); 61091da177e4SLinus Torvalds } 61101da177e4SLinus Torvalds 6111713a04aeSAhmed S. Darwish static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid) 6112713a04aeSAhmed S. Darwish { 6113713a04aeSAhmed S. Darwish struct ipc_security_struct *isec = ipcp->security; 6114713a04aeSAhmed S. Darwish *secid = isec->sid; 6115713a04aeSAhmed S. Darwish } 6116713a04aeSAhmed S. Darwish 61171da177e4SLinus Torvalds static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode) 61181da177e4SLinus Torvalds { 61191da177e4SLinus Torvalds if (inode) 61201da177e4SLinus Torvalds inode_doinit_with_dentry(inode, dentry); 61211da177e4SLinus Torvalds } 61221da177e4SLinus Torvalds 61231da177e4SLinus Torvalds static int selinux_getprocattr(struct task_struct *p, 612404ff9708SAl Viro char *name, char **value) 61251da177e4SLinus Torvalds { 6126275bb41eSDavid Howells const struct task_security_struct *__tsec; 61278c8570fbSDustin Kirkland u32 sid; 61281da177e4SLinus Torvalds int error; 612904ff9708SAl Viro unsigned len; 61301da177e4SLinus Torvalds 6131275bb41eSDavid Howells rcu_read_lock(); 61320c6cfa62SCasey Schaufler __tsec = selinux_cred(__task_cred(p)); 61331da177e4SLinus Torvalds 6134be0554c9SStephen Smalley if (current != p) { 61356b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 61366b6bc620SStephen Smalley current_sid(), __tsec->sid, 6137be0554c9SStephen Smalley SECCLASS_PROCESS, PROCESS__GETATTR, NULL); 6138be0554c9SStephen Smalley if (error) 6139be0554c9SStephen Smalley goto bad; 6140be0554c9SStephen Smalley } 6141be0554c9SStephen Smalley 61421da177e4SLinus Torvalds if (!strcmp(name, "current")) 6143275bb41eSDavid Howells sid = __tsec->sid; 61441da177e4SLinus Torvalds else if (!strcmp(name, "prev")) 6145275bb41eSDavid Howells sid = __tsec->osid; 61461da177e4SLinus Torvalds else if (!strcmp(name, "exec")) 6147275bb41eSDavid Howells sid = __tsec->exec_sid; 61481da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 6149275bb41eSDavid Howells sid = __tsec->create_sid; 61504eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) 6151275bb41eSDavid Howells sid = __tsec->keycreate_sid; 615242c3e03eSEric Paris else if (!strcmp(name, "sockcreate")) 6153275bb41eSDavid Howells sid = __tsec->sockcreate_sid; 6154be0554c9SStephen Smalley else { 6155be0554c9SStephen Smalley error = -EINVAL; 6156be0554c9SStephen Smalley goto bad; 6157be0554c9SStephen Smalley } 6158275bb41eSDavid Howells rcu_read_unlock(); 61591da177e4SLinus Torvalds 61601da177e4SLinus Torvalds if (!sid) 61611da177e4SLinus Torvalds return 0; 61621da177e4SLinus Torvalds 6163aa8e712cSStephen Smalley error = security_sid_to_context(&selinux_state, sid, value, &len); 616404ff9708SAl Viro if (error) 616504ff9708SAl Viro return error; 616604ff9708SAl Viro return len; 6167275bb41eSDavid Howells 6168be0554c9SStephen Smalley bad: 6169275bb41eSDavid Howells rcu_read_unlock(); 6170be0554c9SStephen Smalley return error; 61711da177e4SLinus Torvalds } 61721da177e4SLinus Torvalds 6173b21507e2SStephen Smalley static int selinux_setprocattr(const char *name, void *value, size_t size) 61741da177e4SLinus Torvalds { 61751da177e4SLinus Torvalds struct task_security_struct *tsec; 6176d84f4f99SDavid Howells struct cred *new; 6177be0554c9SStephen Smalley u32 mysid = current_sid(), sid = 0, ptsid; 61781da177e4SLinus Torvalds int error; 61791da177e4SLinus Torvalds char *str = value; 61801da177e4SLinus Torvalds 61811da177e4SLinus Torvalds /* 61821da177e4SLinus Torvalds * Basic control over ability to set these attributes at all. 61831da177e4SLinus Torvalds */ 61841da177e4SLinus Torvalds if (!strcmp(name, "exec")) 61856b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 61866b6bc620SStephen Smalley mysid, mysid, SECCLASS_PROCESS, 6187be0554c9SStephen Smalley PROCESS__SETEXEC, NULL); 61881da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 61896b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 61906b6bc620SStephen Smalley mysid, mysid, SECCLASS_PROCESS, 6191be0554c9SStephen Smalley PROCESS__SETFSCREATE, NULL); 61924eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) 61936b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 61946b6bc620SStephen Smalley mysid, mysid, SECCLASS_PROCESS, 6195be0554c9SStephen Smalley PROCESS__SETKEYCREATE, NULL); 619642c3e03eSEric Paris else if (!strcmp(name, "sockcreate")) 61976b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 61986b6bc620SStephen Smalley mysid, mysid, SECCLASS_PROCESS, 6199be0554c9SStephen Smalley PROCESS__SETSOCKCREATE, NULL); 62001da177e4SLinus Torvalds else if (!strcmp(name, "current")) 62016b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 62026b6bc620SStephen Smalley mysid, mysid, SECCLASS_PROCESS, 6203be0554c9SStephen Smalley PROCESS__SETCURRENT, NULL); 62041da177e4SLinus Torvalds else 62051da177e4SLinus Torvalds error = -EINVAL; 62061da177e4SLinus Torvalds if (error) 62071da177e4SLinus Torvalds return error; 62081da177e4SLinus Torvalds 62091da177e4SLinus Torvalds /* Obtain a SID for the context, if one was specified. */ 6210a050a570SStephen Smalley if (size && str[0] && str[0] != '\n') { 62111da177e4SLinus Torvalds if (str[size-1] == '\n') { 62121da177e4SLinus Torvalds str[size-1] = 0; 62131da177e4SLinus Torvalds size--; 62141da177e4SLinus Torvalds } 6215aa8e712cSStephen Smalley error = security_context_to_sid(&selinux_state, value, size, 6216aa8e712cSStephen Smalley &sid, GFP_KERNEL); 621712b29f34SStephen Smalley if (error == -EINVAL && !strcmp(name, "fscreate")) { 6218db59000aSStephen Smalley if (!has_cap_mac_admin(true)) { 6219d6ea83ecSEric Paris struct audit_buffer *ab; 6220d6ea83ecSEric Paris size_t audit_size; 6221d6ea83ecSEric Paris 6222d6ea83ecSEric Paris /* We strip a nul only if it is at the end, otherwise the 6223d6ea83ecSEric Paris * context contains a nul and we should audit that */ 6224d6ea83ecSEric Paris if (str[size - 1] == '\0') 6225d6ea83ecSEric Paris audit_size = size - 1; 6226d6ea83ecSEric Paris else 6227d6ea83ecSEric Paris audit_size = size; 6228cdfb6b34SRichard Guy Briggs ab = audit_log_start(audit_context(), 6229cdfb6b34SRichard Guy Briggs GFP_ATOMIC, 6230cdfb6b34SRichard Guy Briggs AUDIT_SELINUX_ERR); 6231d6ea83ecSEric Paris audit_log_format(ab, "op=fscreate invalid_context="); 6232d6ea83ecSEric Paris audit_log_n_untrustedstring(ab, value, audit_size); 6233d6ea83ecSEric Paris audit_log_end(ab); 6234d6ea83ecSEric Paris 623512b29f34SStephen Smalley return error; 6236d6ea83ecSEric Paris } 6237aa8e712cSStephen Smalley error = security_context_to_sid_force( 6238aa8e712cSStephen Smalley &selinux_state, 6239aa8e712cSStephen Smalley value, size, &sid); 624012b29f34SStephen Smalley } 62411da177e4SLinus Torvalds if (error) 62421da177e4SLinus Torvalds return error; 62431da177e4SLinus Torvalds } 62441da177e4SLinus Torvalds 6245d84f4f99SDavid Howells new = prepare_creds(); 6246d84f4f99SDavid Howells if (!new) 6247d84f4f99SDavid Howells return -ENOMEM; 6248d84f4f99SDavid Howells 62491da177e4SLinus Torvalds /* Permission checking based on the specified context is 62501da177e4SLinus Torvalds performed during the actual operation (execve, 62511da177e4SLinus Torvalds open/mkdir/...), when we know the full context of the 6252d84f4f99SDavid Howells operation. See selinux_bprm_set_creds for the execve 62531da177e4SLinus Torvalds checks and may_create for the file creation checks. The 62541da177e4SLinus Torvalds operation will then fail if the context is not permitted. */ 62550c6cfa62SCasey Schaufler tsec = selinux_cred(new); 6256d84f4f99SDavid Howells if (!strcmp(name, "exec")) { 62571da177e4SLinus Torvalds tsec->exec_sid = sid; 6258d84f4f99SDavid Howells } else if (!strcmp(name, "fscreate")) { 62591da177e4SLinus Torvalds tsec->create_sid = sid; 6260d84f4f99SDavid Howells } else if (!strcmp(name, "keycreate")) { 62616b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 62626b6bc620SStephen Smalley mysid, sid, SECCLASS_KEY, KEY__CREATE, 6263be0554c9SStephen Smalley NULL); 62644eb582cfSMichael LeMay if (error) 6265d84f4f99SDavid Howells goto abort_change; 62664eb582cfSMichael LeMay tsec->keycreate_sid = sid; 6267d84f4f99SDavid Howells } else if (!strcmp(name, "sockcreate")) { 626842c3e03eSEric Paris tsec->sockcreate_sid = sid; 6269d84f4f99SDavid Howells } else if (!strcmp(name, "current")) { 6270d84f4f99SDavid Howells error = -EINVAL; 62711da177e4SLinus Torvalds if (sid == 0) 6272d84f4f99SDavid Howells goto abort_change; 6273d9250deaSKaiGai Kohei 6274d84f4f99SDavid Howells /* Only allow single threaded processes to change context */ 6275d84f4f99SDavid Howells error = -EPERM; 62765bb459bbSOleg Nesterov if (!current_is_single_threaded()) { 6277aa8e712cSStephen Smalley error = security_bounded_transition(&selinux_state, 6278aa8e712cSStephen Smalley tsec->sid, sid); 6279d84f4f99SDavid Howells if (error) 6280d84f4f99SDavid Howells goto abort_change; 62811da177e4SLinus Torvalds } 62821da177e4SLinus Torvalds 62831da177e4SLinus Torvalds /* Check permissions for the transition. */ 62846b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 62856b6bc620SStephen Smalley tsec->sid, sid, SECCLASS_PROCESS, 62861da177e4SLinus Torvalds PROCESS__DYNTRANSITION, NULL); 62871da177e4SLinus Torvalds if (error) 6288d84f4f99SDavid Howells goto abort_change; 62891da177e4SLinus Torvalds 62901da177e4SLinus Torvalds /* Check for ptracing, and update the task SID if ok. 62911da177e4SLinus Torvalds Otherwise, leave SID unchanged and fail. */ 6292be0554c9SStephen Smalley ptsid = ptrace_parent_sid(); 62930c6181cbSPaul Moore if (ptsid != 0) { 62946b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 62956b6bc620SStephen Smalley ptsid, sid, SECCLASS_PROCESS, 6296d84f4f99SDavid Howells PROCESS__PTRACE, NULL); 6297d84f4f99SDavid Howells if (error) 6298d84f4f99SDavid Howells goto abort_change; 6299d84f4f99SDavid Howells } 6300d84f4f99SDavid Howells 6301d84f4f99SDavid Howells tsec->sid = sid; 6302d84f4f99SDavid Howells } else { 6303d84f4f99SDavid Howells error = -EINVAL; 6304d84f4f99SDavid Howells goto abort_change; 6305d84f4f99SDavid Howells } 6306d84f4f99SDavid Howells 6307d84f4f99SDavid Howells commit_creds(new); 63081da177e4SLinus Torvalds return size; 6309d84f4f99SDavid Howells 6310d84f4f99SDavid Howells abort_change: 6311d84f4f99SDavid Howells abort_creds(new); 6312d84f4f99SDavid Howells return error; 63131da177e4SLinus Torvalds } 63141da177e4SLinus Torvalds 6315746df9b5SDavid Quigley static int selinux_ismaclabel(const char *name) 6316746df9b5SDavid Quigley { 6317746df9b5SDavid Quigley return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0); 6318746df9b5SDavid Quigley } 6319746df9b5SDavid Quigley 6320dc49c1f9SCatherine Zhang static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) 6321dc49c1f9SCatherine Zhang { 6322aa8e712cSStephen Smalley return security_sid_to_context(&selinux_state, secid, 6323aa8e712cSStephen Smalley secdata, seclen); 6324dc49c1f9SCatherine Zhang } 6325dc49c1f9SCatherine Zhang 63267bf570dcSDavid Howells static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid) 632763cb3449SDavid Howells { 6328aa8e712cSStephen Smalley return security_context_to_sid(&selinux_state, secdata, seclen, 6329aa8e712cSStephen Smalley secid, GFP_KERNEL); 633063cb3449SDavid Howells } 633163cb3449SDavid Howells 6332dc49c1f9SCatherine Zhang static void selinux_release_secctx(char *secdata, u32 seclen) 6333dc49c1f9SCatherine Zhang { 6334dc49c1f9SCatherine Zhang kfree(secdata); 6335dc49c1f9SCatherine Zhang } 6336dc49c1f9SCatherine Zhang 63376f3be9f5SAndreas Gruenbacher static void selinux_inode_invalidate_secctx(struct inode *inode) 63386f3be9f5SAndreas Gruenbacher { 63396f3be9f5SAndreas Gruenbacher struct inode_security_struct *isec = inode->i_security; 63406f3be9f5SAndreas Gruenbacher 63419287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 63426f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INVALID; 63439287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 63446f3be9f5SAndreas Gruenbacher } 63456f3be9f5SAndreas Gruenbacher 63461ee65e37SDavid P. Quigley /* 63471ee65e37SDavid P. Quigley * called with inode->i_mutex locked 63481ee65e37SDavid P. Quigley */ 63491ee65e37SDavid P. Quigley static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen) 63501ee65e37SDavid P. Quigley { 63511ee65e37SDavid P. Quigley return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0); 63521ee65e37SDavid P. Quigley } 63531ee65e37SDavid P. Quigley 63541ee65e37SDavid P. Quigley /* 63551ee65e37SDavid P. Quigley * called with inode->i_mutex locked 63561ee65e37SDavid P. Quigley */ 63571ee65e37SDavid P. Quigley static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen) 63581ee65e37SDavid P. Quigley { 63591ee65e37SDavid P. Quigley return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0); 63601ee65e37SDavid P. Quigley } 63611ee65e37SDavid P. Quigley 63621ee65e37SDavid P. Quigley static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen) 63631ee65e37SDavid P. Quigley { 63641ee65e37SDavid P. Quigley int len = 0; 63651ee65e37SDavid P. Quigley len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX, 63661ee65e37SDavid P. Quigley ctx, true); 63671ee65e37SDavid P. Quigley if (len < 0) 63681ee65e37SDavid P. Quigley return len; 63691ee65e37SDavid P. Quigley *ctxlen = len; 63701ee65e37SDavid P. Quigley return 0; 63711ee65e37SDavid P. Quigley } 6372d720024eSMichael LeMay #ifdef CONFIG_KEYS 6373d720024eSMichael LeMay 6374d84f4f99SDavid Howells static int selinux_key_alloc(struct key *k, const struct cred *cred, 63757e047ef5SDavid Howells unsigned long flags) 6376d720024eSMichael LeMay { 6377d84f4f99SDavid Howells const struct task_security_struct *tsec; 6378d720024eSMichael LeMay struct key_security_struct *ksec; 6379d720024eSMichael LeMay 6380d720024eSMichael LeMay ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL); 6381d720024eSMichael LeMay if (!ksec) 6382d720024eSMichael LeMay return -ENOMEM; 6383d720024eSMichael LeMay 63840c6cfa62SCasey Schaufler tsec = selinux_cred(cred); 6385d84f4f99SDavid Howells if (tsec->keycreate_sid) 6386d84f4f99SDavid Howells ksec->sid = tsec->keycreate_sid; 63874eb582cfSMichael LeMay else 6388d84f4f99SDavid Howells ksec->sid = tsec->sid; 6389d720024eSMichael LeMay 6390275bb41eSDavid Howells k->security = ksec; 6391d720024eSMichael LeMay return 0; 6392d720024eSMichael LeMay } 6393d720024eSMichael LeMay 6394d720024eSMichael LeMay static void selinux_key_free(struct key *k) 6395d720024eSMichael LeMay { 6396d720024eSMichael LeMay struct key_security_struct *ksec = k->security; 6397d720024eSMichael LeMay 6398d720024eSMichael LeMay k->security = NULL; 6399d720024eSMichael LeMay kfree(ksec); 6400d720024eSMichael LeMay } 6401d720024eSMichael LeMay 6402d720024eSMichael LeMay static int selinux_key_permission(key_ref_t key_ref, 6403d84f4f99SDavid Howells const struct cred *cred, 6404f5895943SDavid Howells unsigned perm) 6405d720024eSMichael LeMay { 6406d720024eSMichael LeMay struct key *key; 6407d720024eSMichael LeMay struct key_security_struct *ksec; 6408275bb41eSDavid Howells u32 sid; 6409d720024eSMichael LeMay 6410d720024eSMichael LeMay /* if no specific permissions are requested, we skip the 6411d720024eSMichael LeMay permission check. No serious, additional covert channels 6412d720024eSMichael LeMay appear to be created. */ 6413d720024eSMichael LeMay if (perm == 0) 6414d720024eSMichael LeMay return 0; 6415d720024eSMichael LeMay 6416d84f4f99SDavid Howells sid = cred_sid(cred); 6417275bb41eSDavid Howells 6418275bb41eSDavid Howells key = key_ref_to_ptr(key_ref); 6419275bb41eSDavid Howells ksec = key->security; 6420275bb41eSDavid Howells 64216b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 64226b6bc620SStephen Smalley sid, ksec->sid, SECCLASS_KEY, perm, NULL); 6423d720024eSMichael LeMay } 6424d720024eSMichael LeMay 642570a5bb72SDavid Howells static int selinux_key_getsecurity(struct key *key, char **_buffer) 642670a5bb72SDavid Howells { 642770a5bb72SDavid Howells struct key_security_struct *ksec = key->security; 642870a5bb72SDavid Howells char *context = NULL; 642970a5bb72SDavid Howells unsigned len; 643070a5bb72SDavid Howells int rc; 643170a5bb72SDavid Howells 6432aa8e712cSStephen Smalley rc = security_sid_to_context(&selinux_state, ksec->sid, 6433aa8e712cSStephen Smalley &context, &len); 643470a5bb72SDavid Howells if (!rc) 643570a5bb72SDavid Howells rc = len; 643670a5bb72SDavid Howells *_buffer = context; 643770a5bb72SDavid Howells return rc; 643870a5bb72SDavid Howells } 64393a976fa6SDaniel Jurgens #endif 644070a5bb72SDavid Howells 64413a976fa6SDaniel Jurgens #ifdef CONFIG_SECURITY_INFINIBAND 6442cfc4d882SDaniel Jurgens static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val) 6443cfc4d882SDaniel Jurgens { 6444cfc4d882SDaniel Jurgens struct common_audit_data ad; 6445cfc4d882SDaniel Jurgens int err; 6446cfc4d882SDaniel Jurgens u32 sid = 0; 6447cfc4d882SDaniel Jurgens struct ib_security_struct *sec = ib_sec; 6448cfc4d882SDaniel Jurgens struct lsm_ibpkey_audit ibpkey; 6449cfc4d882SDaniel Jurgens 6450409dcf31SDaniel Jurgens err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid); 6451cfc4d882SDaniel Jurgens if (err) 6452cfc4d882SDaniel Jurgens return err; 6453cfc4d882SDaniel Jurgens 6454cfc4d882SDaniel Jurgens ad.type = LSM_AUDIT_DATA_IBPKEY; 6455cfc4d882SDaniel Jurgens ibpkey.subnet_prefix = subnet_prefix; 6456cfc4d882SDaniel Jurgens ibpkey.pkey = pkey_val; 6457cfc4d882SDaniel Jurgens ad.u.ibpkey = &ibpkey; 64586b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 64596b6bc620SStephen Smalley sec->sid, sid, 6460cfc4d882SDaniel Jurgens SECCLASS_INFINIBAND_PKEY, 6461cfc4d882SDaniel Jurgens INFINIBAND_PKEY__ACCESS, &ad); 6462cfc4d882SDaniel Jurgens } 6463cfc4d882SDaniel Jurgens 6464ab861dfcSDaniel Jurgens static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name, 6465ab861dfcSDaniel Jurgens u8 port_num) 6466ab861dfcSDaniel Jurgens { 6467ab861dfcSDaniel Jurgens struct common_audit_data ad; 6468ab861dfcSDaniel Jurgens int err; 6469ab861dfcSDaniel Jurgens u32 sid = 0; 6470ab861dfcSDaniel Jurgens struct ib_security_struct *sec = ib_sec; 6471ab861dfcSDaniel Jurgens struct lsm_ibendport_audit ibendport; 6472ab861dfcSDaniel Jurgens 6473aa8e712cSStephen Smalley err = security_ib_endport_sid(&selinux_state, dev_name, port_num, 6474aa8e712cSStephen Smalley &sid); 6475ab861dfcSDaniel Jurgens 6476ab861dfcSDaniel Jurgens if (err) 6477ab861dfcSDaniel Jurgens return err; 6478ab861dfcSDaniel Jurgens 6479ab861dfcSDaniel Jurgens ad.type = LSM_AUDIT_DATA_IBENDPORT; 6480ab861dfcSDaniel Jurgens strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name)); 6481ab861dfcSDaniel Jurgens ibendport.port = port_num; 6482ab861dfcSDaniel Jurgens ad.u.ibendport = &ibendport; 64836b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 64846b6bc620SStephen Smalley sec->sid, sid, 6485ab861dfcSDaniel Jurgens SECCLASS_INFINIBAND_ENDPORT, 6486ab861dfcSDaniel Jurgens INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad); 6487ab861dfcSDaniel Jurgens } 6488ab861dfcSDaniel Jurgens 64893a976fa6SDaniel Jurgens static int selinux_ib_alloc_security(void **ib_sec) 64903a976fa6SDaniel Jurgens { 64913a976fa6SDaniel Jurgens struct ib_security_struct *sec; 64923a976fa6SDaniel Jurgens 64933a976fa6SDaniel Jurgens sec = kzalloc(sizeof(*sec), GFP_KERNEL); 64943a976fa6SDaniel Jurgens if (!sec) 64953a976fa6SDaniel Jurgens return -ENOMEM; 64963a976fa6SDaniel Jurgens sec->sid = current_sid(); 64973a976fa6SDaniel Jurgens 64983a976fa6SDaniel Jurgens *ib_sec = sec; 64993a976fa6SDaniel Jurgens return 0; 65003a976fa6SDaniel Jurgens } 65013a976fa6SDaniel Jurgens 65023a976fa6SDaniel Jurgens static void selinux_ib_free_security(void *ib_sec) 65033a976fa6SDaniel Jurgens { 65043a976fa6SDaniel Jurgens kfree(ib_sec); 65053a976fa6SDaniel Jurgens } 6506d720024eSMichael LeMay #endif 6507d720024eSMichael LeMay 6508ec27c356SChenbo Feng #ifdef CONFIG_BPF_SYSCALL 6509ec27c356SChenbo Feng static int selinux_bpf(int cmd, union bpf_attr *attr, 6510ec27c356SChenbo Feng unsigned int size) 6511ec27c356SChenbo Feng { 6512ec27c356SChenbo Feng u32 sid = current_sid(); 6513ec27c356SChenbo Feng int ret; 6514ec27c356SChenbo Feng 6515ec27c356SChenbo Feng switch (cmd) { 6516ec27c356SChenbo Feng case BPF_MAP_CREATE: 65176b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 65186b6bc620SStephen Smalley sid, sid, SECCLASS_BPF, BPF__MAP_CREATE, 6519ec27c356SChenbo Feng NULL); 6520ec27c356SChenbo Feng break; 6521ec27c356SChenbo Feng case BPF_PROG_LOAD: 65226b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 65236b6bc620SStephen Smalley sid, sid, SECCLASS_BPF, BPF__PROG_LOAD, 6524ec27c356SChenbo Feng NULL); 6525ec27c356SChenbo Feng break; 6526ec27c356SChenbo Feng default: 6527ec27c356SChenbo Feng ret = 0; 6528ec27c356SChenbo Feng break; 6529ec27c356SChenbo Feng } 6530ec27c356SChenbo Feng 6531ec27c356SChenbo Feng return ret; 6532ec27c356SChenbo Feng } 6533ec27c356SChenbo Feng 6534ec27c356SChenbo Feng static u32 bpf_map_fmode_to_av(fmode_t fmode) 6535ec27c356SChenbo Feng { 6536ec27c356SChenbo Feng u32 av = 0; 6537ec27c356SChenbo Feng 6538ec27c356SChenbo Feng if (fmode & FMODE_READ) 6539ec27c356SChenbo Feng av |= BPF__MAP_READ; 6540ec27c356SChenbo Feng if (fmode & FMODE_WRITE) 6541ec27c356SChenbo Feng av |= BPF__MAP_WRITE; 6542ec27c356SChenbo Feng return av; 6543ec27c356SChenbo Feng } 6544ec27c356SChenbo Feng 6545f66e448cSChenbo Feng /* This function will check the file pass through unix socket or binder to see 6546f66e448cSChenbo Feng * if it is a bpf related object. And apply correspinding checks on the bpf 6547f66e448cSChenbo Feng * object based on the type. The bpf maps and programs, not like other files and 6548f66e448cSChenbo Feng * socket, are using a shared anonymous inode inside the kernel as their inode. 6549f66e448cSChenbo Feng * So checking that inode cannot identify if the process have privilege to 6550f66e448cSChenbo Feng * access the bpf object and that's why we have to add this additional check in 6551f66e448cSChenbo Feng * selinux_file_receive and selinux_binder_transfer_files. 6552f66e448cSChenbo Feng */ 6553f66e448cSChenbo Feng static int bpf_fd_pass(struct file *file, u32 sid) 6554f66e448cSChenbo Feng { 6555f66e448cSChenbo Feng struct bpf_security_struct *bpfsec; 6556f66e448cSChenbo Feng struct bpf_prog *prog; 6557f66e448cSChenbo Feng struct bpf_map *map; 6558f66e448cSChenbo Feng int ret; 6559f66e448cSChenbo Feng 6560f66e448cSChenbo Feng if (file->f_op == &bpf_map_fops) { 6561f66e448cSChenbo Feng map = file->private_data; 6562f66e448cSChenbo Feng bpfsec = map->security; 65636b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 65646b6bc620SStephen Smalley sid, bpfsec->sid, SECCLASS_BPF, 6565f66e448cSChenbo Feng bpf_map_fmode_to_av(file->f_mode), NULL); 6566f66e448cSChenbo Feng if (ret) 6567f66e448cSChenbo Feng return ret; 6568f66e448cSChenbo Feng } else if (file->f_op == &bpf_prog_fops) { 6569f66e448cSChenbo Feng prog = file->private_data; 6570f66e448cSChenbo Feng bpfsec = prog->aux->security; 65716b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 65726b6bc620SStephen Smalley sid, bpfsec->sid, SECCLASS_BPF, 6573f66e448cSChenbo Feng BPF__PROG_RUN, NULL); 6574f66e448cSChenbo Feng if (ret) 6575f66e448cSChenbo Feng return ret; 6576f66e448cSChenbo Feng } 6577f66e448cSChenbo Feng return 0; 6578f66e448cSChenbo Feng } 6579f66e448cSChenbo Feng 6580ec27c356SChenbo Feng static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode) 6581ec27c356SChenbo Feng { 6582ec27c356SChenbo Feng u32 sid = current_sid(); 6583ec27c356SChenbo Feng struct bpf_security_struct *bpfsec; 6584ec27c356SChenbo Feng 6585ec27c356SChenbo Feng bpfsec = map->security; 65866b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 65876b6bc620SStephen Smalley sid, bpfsec->sid, SECCLASS_BPF, 6588ec27c356SChenbo Feng bpf_map_fmode_to_av(fmode), NULL); 6589ec27c356SChenbo Feng } 6590ec27c356SChenbo Feng 6591ec27c356SChenbo Feng static int selinux_bpf_prog(struct bpf_prog *prog) 6592ec27c356SChenbo Feng { 6593ec27c356SChenbo Feng u32 sid = current_sid(); 6594ec27c356SChenbo Feng struct bpf_security_struct *bpfsec; 6595ec27c356SChenbo Feng 6596ec27c356SChenbo Feng bpfsec = prog->aux->security; 65976b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 65986b6bc620SStephen Smalley sid, bpfsec->sid, SECCLASS_BPF, 6599ec27c356SChenbo Feng BPF__PROG_RUN, NULL); 6600ec27c356SChenbo Feng } 6601ec27c356SChenbo Feng 6602ec27c356SChenbo Feng static int selinux_bpf_map_alloc(struct bpf_map *map) 6603ec27c356SChenbo Feng { 6604ec27c356SChenbo Feng struct bpf_security_struct *bpfsec; 6605ec27c356SChenbo Feng 6606ec27c356SChenbo Feng bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL); 6607ec27c356SChenbo Feng if (!bpfsec) 6608ec27c356SChenbo Feng return -ENOMEM; 6609ec27c356SChenbo Feng 6610ec27c356SChenbo Feng bpfsec->sid = current_sid(); 6611ec27c356SChenbo Feng map->security = bpfsec; 6612ec27c356SChenbo Feng 6613ec27c356SChenbo Feng return 0; 6614ec27c356SChenbo Feng } 6615ec27c356SChenbo Feng 6616ec27c356SChenbo Feng static void selinux_bpf_map_free(struct bpf_map *map) 6617ec27c356SChenbo Feng { 6618ec27c356SChenbo Feng struct bpf_security_struct *bpfsec = map->security; 6619ec27c356SChenbo Feng 6620ec27c356SChenbo Feng map->security = NULL; 6621ec27c356SChenbo Feng kfree(bpfsec); 6622ec27c356SChenbo Feng } 6623ec27c356SChenbo Feng 6624ec27c356SChenbo Feng static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux) 6625ec27c356SChenbo Feng { 6626ec27c356SChenbo Feng struct bpf_security_struct *bpfsec; 6627ec27c356SChenbo Feng 6628ec27c356SChenbo Feng bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL); 6629ec27c356SChenbo Feng if (!bpfsec) 6630ec27c356SChenbo Feng return -ENOMEM; 6631ec27c356SChenbo Feng 6632ec27c356SChenbo Feng bpfsec->sid = current_sid(); 6633ec27c356SChenbo Feng aux->security = bpfsec; 6634ec27c356SChenbo Feng 6635ec27c356SChenbo Feng return 0; 6636ec27c356SChenbo Feng } 6637ec27c356SChenbo Feng 6638ec27c356SChenbo Feng static void selinux_bpf_prog_free(struct bpf_prog_aux *aux) 6639ec27c356SChenbo Feng { 6640ec27c356SChenbo Feng struct bpf_security_struct *bpfsec = aux->security; 6641ec27c356SChenbo Feng 6642ec27c356SChenbo Feng aux->security = NULL; 6643ec27c356SChenbo Feng kfree(bpfsec); 6644ec27c356SChenbo Feng } 6645ec27c356SChenbo Feng #endif 6646ec27c356SChenbo Feng 6647bbd3662aSCasey Schaufler struct lsm_blob_sizes selinux_blob_sizes __lsm_ro_after_init = { 6648bbd3662aSCasey Schaufler .lbs_cred = sizeof(struct task_security_struct), 6649bbd3662aSCasey Schaufler }; 6650bbd3662aSCasey Schaufler 6651ca97d939SJames Morris static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = { 6652e20b043aSCasey Schaufler LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr), 6653e20b043aSCasey Schaufler LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction), 6654e20b043aSCasey Schaufler LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder), 6655e20b043aSCasey Schaufler LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file), 6656076c54c5SAhmed S. Darwish 6657e20b043aSCasey Schaufler LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check), 6658e20b043aSCasey Schaufler LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme), 6659e20b043aSCasey Schaufler LSM_HOOK_INIT(capget, selinux_capget), 6660e20b043aSCasey Schaufler LSM_HOOK_INIT(capset, selinux_capset), 6661e20b043aSCasey Schaufler LSM_HOOK_INIT(capable, selinux_capable), 6662e20b043aSCasey Schaufler LSM_HOOK_INIT(quotactl, selinux_quotactl), 6663e20b043aSCasey Schaufler LSM_HOOK_INIT(quota_on, selinux_quota_on), 6664e20b043aSCasey Schaufler LSM_HOOK_INIT(syslog, selinux_syslog), 6665e20b043aSCasey Schaufler LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory), 666679af7307SStephen Smalley 6667e20b043aSCasey Schaufler LSM_HOOK_INIT(netlink_send, selinux_netlink_send), 66681da177e4SLinus Torvalds 6669e20b043aSCasey Schaufler LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds), 6670e20b043aSCasey Schaufler LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds), 6671e20b043aSCasey Schaufler LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds), 66721da177e4SLinus Torvalds 6673e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security), 6674e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security), 66755b400239SAl Viro LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts), 6676204cc0ccSAl Viro LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts), 6677e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_remount, selinux_sb_remount), 6678e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount), 6679e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options), 6680e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs), 6681e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_mount, selinux_mount), 6682e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_umount, selinux_umount), 6683e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts), 6684e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts), 6685757cbe59SAl Viro LSM_HOOK_INIT(sb_add_mnt_opt, selinux_add_mnt_opt), 66861da177e4SLinus Torvalds 6687e20b043aSCasey Schaufler LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security), 6688a518b0a5SVivek Goyal LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as), 6689e0007529SEric Paris 6690e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security), 6691e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security), 6692e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security), 6693e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_create, selinux_inode_create), 6694e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_link, selinux_inode_link), 6695e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink), 6696e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink), 6697e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir), 6698e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir), 6699e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod), 6700e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_rename, selinux_inode_rename), 6701e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink), 6702e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link), 6703e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_permission, selinux_inode_permission), 6704e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr), 6705e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr), 6706e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr), 6707e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr), 6708e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr), 6709e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr), 6710e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr), 6711e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity), 6712e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity), 6713e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity), 6714e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid), 671556909eb3SVivek Goyal LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up), 671619472b69SVivek Goyal LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr), 67171da177e4SLinus Torvalds 6718e20b043aSCasey Schaufler LSM_HOOK_INIT(file_permission, selinux_file_permission), 6719e20b043aSCasey Schaufler LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security), 6720e20b043aSCasey Schaufler LSM_HOOK_INIT(file_free_security, selinux_file_free_security), 6721e20b043aSCasey Schaufler LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl), 6722e20b043aSCasey Schaufler LSM_HOOK_INIT(mmap_file, selinux_mmap_file), 6723e20b043aSCasey Schaufler LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr), 6724e20b043aSCasey Schaufler LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect), 6725e20b043aSCasey Schaufler LSM_HOOK_INIT(file_lock, selinux_file_lock), 6726e20b043aSCasey Schaufler LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl), 6727e20b043aSCasey Schaufler LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner), 6728e20b043aSCasey Schaufler LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask), 6729e20b043aSCasey Schaufler LSM_HOOK_INIT(file_receive, selinux_file_receive), 67301da177e4SLinus Torvalds 6731e20b043aSCasey Schaufler LSM_HOOK_INIT(file_open, selinux_file_open), 67321da177e4SLinus Torvalds 6733a79be238STetsuo Handa LSM_HOOK_INIT(task_alloc, selinux_task_alloc), 6734e20b043aSCasey Schaufler LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare), 6735e20b043aSCasey Schaufler LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer), 67363ec30113SMatthew Garrett LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid), 6737e20b043aSCasey Schaufler LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as), 6738e20b043aSCasey Schaufler LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as), 6739e20b043aSCasey Schaufler LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request), 6740c77b8cdfSMimi Zohar LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data), 674161d612eaSJeff Vander Stoep LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file), 6742e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid), 6743e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid), 6744e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getsid, selinux_task_getsid), 6745e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid), 6746e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setnice, selinux_task_setnice), 6747e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio), 6748e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio), 6749791ec491SStephen Smalley LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit), 6750e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit), 6751e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler), 6752e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler), 6753e20b043aSCasey Schaufler LSM_HOOK_INIT(task_movememory, selinux_task_movememory), 6754e20b043aSCasey Schaufler LSM_HOOK_INIT(task_kill, selinux_task_kill), 6755e20b043aSCasey Schaufler LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode), 6756788e7dd4SYuichi Nakamura 6757e20b043aSCasey Schaufler LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission), 6758e20b043aSCasey Schaufler LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid), 67591da177e4SLinus Torvalds 6760e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security), 6761e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_msg_free_security, selinux_msg_msg_free_security), 67621da177e4SLinus Torvalds 6763e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_alloc_security, 6764e20b043aSCasey Schaufler selinux_msg_queue_alloc_security), 6765e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_free_security, selinux_msg_queue_free_security), 6766e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate), 6767e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl), 6768e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd), 6769e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv), 67701da177e4SLinus Torvalds 6771e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security), 6772e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_free_security, selinux_shm_free_security), 6773e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_associate, selinux_shm_associate), 6774e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl), 6775e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat), 67761da177e4SLinus Torvalds 6777e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security), 6778e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_free_security, selinux_sem_free_security), 6779e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_associate, selinux_sem_associate), 6780e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl), 6781e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_semop, selinux_sem_semop), 67821da177e4SLinus Torvalds 6783e20b043aSCasey Schaufler LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate), 67841da177e4SLinus Torvalds 6785e20b043aSCasey Schaufler LSM_HOOK_INIT(getprocattr, selinux_getprocattr), 6786e20b043aSCasey Schaufler LSM_HOOK_INIT(setprocattr, selinux_setprocattr), 67871da177e4SLinus Torvalds 6788e20b043aSCasey Schaufler LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel), 6789e20b043aSCasey Schaufler LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx), 6790e20b043aSCasey Schaufler LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid), 6791e20b043aSCasey Schaufler LSM_HOOK_INIT(release_secctx, selinux_release_secctx), 67926f3be9f5SAndreas Gruenbacher LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx), 6793e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx), 6794e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx), 6795e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx), 67961da177e4SLinus Torvalds 6797e20b043aSCasey Schaufler LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect), 6798e20b043aSCasey Schaufler LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send), 6799dc49c1f9SCatherine Zhang 6800e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_create, selinux_socket_create), 6801e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create), 68020b811db2SDavid Herrmann LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair), 6803e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_bind, selinux_socket_bind), 6804e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_connect, selinux_socket_connect), 6805e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_listen, selinux_socket_listen), 6806e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_accept, selinux_socket_accept), 6807e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg), 6808e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg), 6809e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname), 6810e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername), 6811e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt), 6812e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt), 6813e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown), 6814e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb), 6815e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getpeersec_stream, 6816e20b043aSCasey Schaufler selinux_socket_getpeersec_stream), 6817e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram), 6818e20b043aSCasey Schaufler LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security), 6819e20b043aSCasey Schaufler LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security), 6820e20b043aSCasey Schaufler LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security), 6821e20b043aSCasey Schaufler LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid), 6822e20b043aSCasey Schaufler LSM_HOOK_INIT(sock_graft, selinux_sock_graft), 6823d452930fSRichard Haines LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request), 6824d452930fSRichard Haines LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone), 6825d452930fSRichard Haines LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect), 6826e20b043aSCasey Schaufler LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request), 6827e20b043aSCasey Schaufler LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone), 6828e20b043aSCasey Schaufler LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established), 6829e20b043aSCasey Schaufler LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet), 6830e20b043aSCasey Schaufler LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc), 6831e20b043aSCasey Schaufler LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec), 6832e20b043aSCasey Schaufler LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow), 6833e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security), 6834e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security), 6835e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create), 6836e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue), 6837e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach), 6838e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open), 68393a976fa6SDaniel Jurgens #ifdef CONFIG_SECURITY_INFINIBAND 6840cfc4d882SDaniel Jurgens LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access), 6841ab861dfcSDaniel Jurgens LSM_HOOK_INIT(ib_endport_manage_subnet, 6842ab861dfcSDaniel Jurgens selinux_ib_endport_manage_subnet), 68433a976fa6SDaniel Jurgens LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security), 68443a976fa6SDaniel Jurgens LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security), 68453a976fa6SDaniel Jurgens #endif 6846d28d1e08STrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM 6847e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc), 6848e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone), 6849e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free), 6850e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete), 6851e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc), 6852e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_alloc_acquire, 6853e20b043aSCasey Schaufler selinux_xfrm_state_alloc_acquire), 6854e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free), 6855e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete), 6856e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup), 6857e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_pol_flow_match, 6858e20b043aSCasey Schaufler selinux_xfrm_state_pol_flow_match), 6859e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session), 68601da177e4SLinus Torvalds #endif 6861d720024eSMichael LeMay 6862d720024eSMichael LeMay #ifdef CONFIG_KEYS 6863e20b043aSCasey Schaufler LSM_HOOK_INIT(key_alloc, selinux_key_alloc), 6864e20b043aSCasey Schaufler LSM_HOOK_INIT(key_free, selinux_key_free), 6865e20b043aSCasey Schaufler LSM_HOOK_INIT(key_permission, selinux_key_permission), 6866e20b043aSCasey Schaufler LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity), 6867d720024eSMichael LeMay #endif 68689d57a7f9SAhmed S. Darwish 68699d57a7f9SAhmed S. Darwish #ifdef CONFIG_AUDIT 6870e20b043aSCasey Schaufler LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init), 6871e20b043aSCasey Schaufler LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known), 6872e20b043aSCasey Schaufler LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match), 6873e20b043aSCasey Schaufler LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free), 68749d57a7f9SAhmed S. Darwish #endif 6875ec27c356SChenbo Feng 6876ec27c356SChenbo Feng #ifdef CONFIG_BPF_SYSCALL 6877ec27c356SChenbo Feng LSM_HOOK_INIT(bpf, selinux_bpf), 6878ec27c356SChenbo Feng LSM_HOOK_INIT(bpf_map, selinux_bpf_map), 6879ec27c356SChenbo Feng LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog), 6880ec27c356SChenbo Feng LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc), 6881ec27c356SChenbo Feng LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc), 6882ec27c356SChenbo Feng LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free), 6883ec27c356SChenbo Feng LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free), 6884ec27c356SChenbo Feng #endif 68851da177e4SLinus Torvalds }; 68861da177e4SLinus Torvalds 68871da177e4SLinus Torvalds static __init int selinux_init(void) 68881da177e4SLinus Torvalds { 6889c103a91eSpeter enderborg pr_info("SELinux: Initializing.\n"); 68901da177e4SLinus Torvalds 6891aa8e712cSStephen Smalley memset(&selinux_state, 0, sizeof(selinux_state)); 6892e5a5ca96SPaul Moore enforcing_set(&selinux_state, selinux_enforcing_boot); 6893aa8e712cSStephen Smalley selinux_state.checkreqprot = selinux_checkreqprot_boot; 6894aa8e712cSStephen Smalley selinux_ss_init(&selinux_state.ss); 68956b6bc620SStephen Smalley selinux_avc_init(&selinux_state.avc); 6896aa8e712cSStephen Smalley 68971da177e4SLinus Torvalds /* Set the security state for the initial task. */ 6898d84f4f99SDavid Howells cred_init_security(); 68991da177e4SLinus Torvalds 6900fcaaade1SStephen Smalley default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC); 6901fcaaade1SStephen Smalley 69027cae7e26SJames Morris sel_inode_cache = kmem_cache_create("selinux_inode_security", 69037cae7e26SJames Morris sizeof(struct inode_security_struct), 690420c2df83SPaul Mundt 0, SLAB_PANIC, NULL); 690563205654SSangwoo file_security_cache = kmem_cache_create("selinux_file_security", 690663205654SSangwoo sizeof(struct file_security_struct), 690763205654SSangwoo 0, SLAB_PANIC, NULL); 69081da177e4SLinus Torvalds avc_init(); 69091da177e4SLinus Torvalds 6910aa8e712cSStephen Smalley avtab_cache_init(); 6911aa8e712cSStephen Smalley 6912aa8e712cSStephen Smalley ebitmap_cache_init(); 6913aa8e712cSStephen Smalley 6914aa8e712cSStephen Smalley hashtab_cache_init(); 6915aa8e712cSStephen Smalley 6916d69dece5SCasey Schaufler security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux"); 69171da177e4SLinus Torvalds 6918615e51fdSPaul Moore if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET)) 6919615e51fdSPaul Moore panic("SELinux: Unable to register AVC netcache callback\n"); 6920615e51fdSPaul Moore 69218f408ab6SDaniel Jurgens if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET)) 69228f408ab6SDaniel Jurgens panic("SELinux: Unable to register AVC LSM notifier callback\n"); 69238f408ab6SDaniel Jurgens 6924aa8e712cSStephen Smalley if (selinux_enforcing_boot) 6925c103a91eSpeter enderborg pr_debug("SELinux: Starting in enforcing mode\n"); 6926828dfe1dSEric Paris else 6927c103a91eSpeter enderborg pr_debug("SELinux: Starting in permissive mode\n"); 6928d720024eSMichael LeMay 69291da177e4SLinus Torvalds return 0; 69301da177e4SLinus Torvalds } 69311da177e4SLinus Torvalds 6932e8c26255SAl Viro static void delayed_superblock_init(struct super_block *sb, void *unused) 6933e8c26255SAl Viro { 6934204cc0ccSAl Viro selinux_set_mnt_opts(sb, NULL, 0, NULL); 6935e8c26255SAl Viro } 6936e8c26255SAl Viro 69371da177e4SLinus Torvalds void selinux_complete_init(void) 69381da177e4SLinus Torvalds { 6939c103a91eSpeter enderborg pr_debug("SELinux: Completing initialization.\n"); 69401da177e4SLinus Torvalds 69411da177e4SLinus Torvalds /* Set up any superblocks initialized prior to the policy load. */ 6942c103a91eSpeter enderborg pr_debug("SELinux: Setting up existing superblocks.\n"); 6943e8c26255SAl Viro iterate_supers(delayed_superblock_init, NULL); 69441da177e4SLinus Torvalds } 69451da177e4SLinus Torvalds 69461da177e4SLinus Torvalds /* SELinux requires early initialization in order to label 69471da177e4SLinus Torvalds all processes and objects when they are created. */ 69483d6e5f6dSKees Cook DEFINE_LSM(selinux) = { 694907aed2f2SKees Cook .name = "selinux", 695014bd99c8SKees Cook .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE, 6951c5459b82SKees Cook .enabled = &selinux_enabled, 6952bbd3662aSCasey Schaufler .blobs = &selinux_blob_sizes, 69533d6e5f6dSKees Cook .init = selinux_init, 69543d6e5f6dSKees Cook }; 69551da177e4SLinus Torvalds 6956c2b507fdSStephen Smalley #if defined(CONFIG_NETFILTER) 69571da177e4SLinus Torvalds 6958591bb278SFlorian Westphal static const struct nf_hook_ops selinux_nf_ops[] = { 6959effad8dfSPaul Moore { 6960effad8dfSPaul Moore .hook = selinux_ipv4_postroute, 69612597a834SAlban Crequy .pf = NFPROTO_IPV4, 69626e23ae2aSPatrick McHardy .hooknum = NF_INET_POST_ROUTING, 69631da177e4SLinus Torvalds .priority = NF_IP_PRI_SELINUX_LAST, 6964effad8dfSPaul Moore }, 6965effad8dfSPaul Moore { 6966effad8dfSPaul Moore .hook = selinux_ipv4_forward, 69672597a834SAlban Crequy .pf = NFPROTO_IPV4, 6968effad8dfSPaul Moore .hooknum = NF_INET_FORWARD, 6969effad8dfSPaul Moore .priority = NF_IP_PRI_SELINUX_FIRST, 6970948bf85cSPaul Moore }, 6971948bf85cSPaul Moore { 6972948bf85cSPaul Moore .hook = selinux_ipv4_output, 69732597a834SAlban Crequy .pf = NFPROTO_IPV4, 6974948bf85cSPaul Moore .hooknum = NF_INET_LOCAL_OUT, 6975948bf85cSPaul Moore .priority = NF_IP_PRI_SELINUX_FIRST, 697625db6beaSJiri Pirko }, 69771a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 6978effad8dfSPaul Moore { 6979effad8dfSPaul Moore .hook = selinux_ipv6_postroute, 69802597a834SAlban Crequy .pf = NFPROTO_IPV6, 69816e23ae2aSPatrick McHardy .hooknum = NF_INET_POST_ROUTING, 69821da177e4SLinus Torvalds .priority = NF_IP6_PRI_SELINUX_LAST, 6983effad8dfSPaul Moore }, 6984effad8dfSPaul Moore { 6985effad8dfSPaul Moore .hook = selinux_ipv6_forward, 69862597a834SAlban Crequy .pf = NFPROTO_IPV6, 6987effad8dfSPaul Moore .hooknum = NF_INET_FORWARD, 6988effad8dfSPaul Moore .priority = NF_IP6_PRI_SELINUX_FIRST, 698925db6beaSJiri Pirko }, 69902917f57bSHuw Davies { 69912917f57bSHuw Davies .hook = selinux_ipv6_output, 69922917f57bSHuw Davies .pf = NFPROTO_IPV6, 69932917f57bSHuw Davies .hooknum = NF_INET_LOCAL_OUT, 69942917f57bSHuw Davies .priority = NF_IP6_PRI_SELINUX_FIRST, 69952917f57bSHuw Davies }, 69961da177e4SLinus Torvalds #endif /* IPV6 */ 699725db6beaSJiri Pirko }; 69981da177e4SLinus Torvalds 69998e71bf75SFlorian Westphal static int __net_init selinux_nf_register(struct net *net) 70008e71bf75SFlorian Westphal { 70018e71bf75SFlorian Westphal return nf_register_net_hooks(net, selinux_nf_ops, 70028e71bf75SFlorian Westphal ARRAY_SIZE(selinux_nf_ops)); 70038e71bf75SFlorian Westphal } 70048e71bf75SFlorian Westphal 70058e71bf75SFlorian Westphal static void __net_exit selinux_nf_unregister(struct net *net) 70068e71bf75SFlorian Westphal { 70078e71bf75SFlorian Westphal nf_unregister_net_hooks(net, selinux_nf_ops, 70088e71bf75SFlorian Westphal ARRAY_SIZE(selinux_nf_ops)); 70098e71bf75SFlorian Westphal } 70108e71bf75SFlorian Westphal 70118e71bf75SFlorian Westphal static struct pernet_operations selinux_net_ops = { 70128e71bf75SFlorian Westphal .init = selinux_nf_register, 70138e71bf75SFlorian Westphal .exit = selinux_nf_unregister, 70148e71bf75SFlorian Westphal }; 70158e71bf75SFlorian Westphal 70161da177e4SLinus Torvalds static int __init selinux_nf_ip_init(void) 70171da177e4SLinus Torvalds { 701825db6beaSJiri Pirko int err; 70191da177e4SLinus Torvalds 70201da177e4SLinus Torvalds if (!selinux_enabled) 702125db6beaSJiri Pirko return 0; 70221da177e4SLinus Torvalds 7023c103a91eSpeter enderborg pr_debug("SELinux: Registering netfilter hooks\n"); 70241da177e4SLinus Torvalds 70258e71bf75SFlorian Westphal err = register_pernet_subsys(&selinux_net_ops); 70261da177e4SLinus Torvalds if (err) 70278e71bf75SFlorian Westphal panic("SELinux: register_pernet_subsys: error %d\n", err); 70281da177e4SLinus Torvalds 702925db6beaSJiri Pirko return 0; 70301da177e4SLinus Torvalds } 70311da177e4SLinus Torvalds __initcall(selinux_nf_ip_init); 70321da177e4SLinus Torvalds 70331da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 70341da177e4SLinus Torvalds static void selinux_nf_ip_exit(void) 70351da177e4SLinus Torvalds { 7036c103a91eSpeter enderborg pr_debug("SELinux: Unregistering netfilter hooks\n"); 70371da177e4SLinus Torvalds 70388e71bf75SFlorian Westphal unregister_pernet_subsys(&selinux_net_ops); 70391da177e4SLinus Torvalds } 70401da177e4SLinus Torvalds #endif 70411da177e4SLinus Torvalds 7042c2b507fdSStephen Smalley #else /* CONFIG_NETFILTER */ 70431da177e4SLinus Torvalds 70441da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 70451da177e4SLinus Torvalds #define selinux_nf_ip_exit() 70461da177e4SLinus Torvalds #endif 70471da177e4SLinus Torvalds 7048c2b507fdSStephen Smalley #endif /* CONFIG_NETFILTER */ 70491da177e4SLinus Torvalds 70501da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 7051aa8e712cSStephen Smalley int selinux_disable(struct selinux_state *state) 70521da177e4SLinus Torvalds { 7053aa8e712cSStephen Smalley if (state->initialized) { 70541da177e4SLinus Torvalds /* Not permitted after initial policy load. */ 70551da177e4SLinus Torvalds return -EINVAL; 70561da177e4SLinus Torvalds } 70571da177e4SLinus Torvalds 7058aa8e712cSStephen Smalley if (state->disabled) { 70591da177e4SLinus Torvalds /* Only do this once. */ 70601da177e4SLinus Torvalds return -EINVAL; 70611da177e4SLinus Torvalds } 70621da177e4SLinus Torvalds 7063aa8e712cSStephen Smalley state->disabled = 1; 7064aa8e712cSStephen Smalley 7065c103a91eSpeter enderborg pr_info("SELinux: Disabled at runtime.\n"); 70661da177e4SLinus Torvalds 706730d55280SStephen Smalley selinux_enabled = 0; 70681da177e4SLinus Torvalds 7069b1d9e6b0SCasey Schaufler security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks)); 70701da177e4SLinus Torvalds 7071af8ff049SEric Paris /* Try to destroy the avc node cache */ 7072af8ff049SEric Paris avc_disable(); 7073af8ff049SEric Paris 70741da177e4SLinus Torvalds /* Unregister netfilter hooks. */ 70751da177e4SLinus Torvalds selinux_nf_ip_exit(); 70761da177e4SLinus Torvalds 70771da177e4SLinus Torvalds /* Unregister selinuxfs. */ 70781da177e4SLinus Torvalds exit_sel_fs(); 70791da177e4SLinus Torvalds 70801da177e4SLinus Torvalds return 0; 70811da177e4SLinus Torvalds } 70821da177e4SLinus Torvalds #endif 7083