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> 51442155c1SDavid Howells #include <linux/fs_context.h> 52442155c1SDavid Howells #include <linux/fs_parser.h> 531da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h> 541da177e4SLinus Torvalds #include <linux/netfilter_ipv6.h> 551da177e4SLinus Torvalds #include <linux/tty.h> 561da177e4SLinus Torvalds #include <net/icmp.h> 57227b60f5SStephen Hemminger #include <net/ip.h> /* for local_port_range[] */ 581da177e4SLinus Torvalds #include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */ 5947180068SPaul Moore #include <net/inet_connection_sock.h> 60220deb96SPaul Moore #include <net/net_namespace.h> 61d621d35eSPaul Moore #include <net/netlabel.h> 62f5269710SEric Paris #include <linux/uaccess.h> 631da177e4SLinus Torvalds #include <asm/ioctls.h> 6460063497SArun Sharma #include <linux/atomic.h> 651da177e4SLinus Torvalds #include <linux/bitops.h> 661da177e4SLinus Torvalds #include <linux/interrupt.h> 671da177e4SLinus Torvalds #include <linux/netdevice.h> /* for network interface checks */ 6877954983SHong zhi guo #include <net/netlink.h> 691da177e4SLinus Torvalds #include <linux/tcp.h> 701da177e4SLinus Torvalds #include <linux/udp.h> 712ee92d46SJames Morris #include <linux/dccp.h> 72d452930fSRichard Haines #include <linux/sctp.h> 73d452930fSRichard Haines #include <net/sctp/structs.h> 741da177e4SLinus Torvalds #include <linux/quota.h> 751da177e4SLinus Torvalds #include <linux/un.h> /* for Unix socket types */ 761da177e4SLinus Torvalds #include <net/af_unix.h> /* for Unix socket types */ 771da177e4SLinus Torvalds #include <linux/parser.h> 781da177e4SLinus Torvalds #include <linux/nfs_mount.h> 791da177e4SLinus Torvalds #include <net/ipv6.h> 801da177e4SLinus Torvalds #include <linux/hugetlb.h> 811da177e4SLinus Torvalds #include <linux/personality.h> 821da177e4SLinus Torvalds #include <linux/audit.h> 836931dfc9SEric Paris #include <linux/string.h> 84877ce7c1SCatherine Zhang #include <linux/selinux.h> 8523970741SEric Paris #include <linux/mutex.h> 86f06febc9SFrank Mayhar #include <linux/posix-timers.h> 8700234592SKees Cook #include <linux/syslog.h> 883486740aSSerge E. Hallyn #include <linux/user_namespace.h> 8944fc7ea0SPaul Gortmaker #include <linux/export.h> 9040401530SAl Viro #include <linux/msg.h> 9140401530SAl Viro #include <linux/shm.h> 92ec27c356SChenbo Feng #include <linux/bpf.h> 93e262e32dSDavid Howells #include <uapi/linux/mount.h> 941da177e4SLinus Torvalds 951da177e4SLinus Torvalds #include "avc.h" 961da177e4SLinus Torvalds #include "objsec.h" 971da177e4SLinus Torvalds #include "netif.h" 98224dfbd8SPaul Moore #include "netnode.h" 993e112172SPaul Moore #include "netport.h" 100409dcf31SDaniel Jurgens #include "ibpkey.h" 101d28d1e08STrent Jaeger #include "xfrm.h" 102c60475bfSPaul Moore #include "netlabel.h" 1039d57a7f9SAhmed S. Darwish #include "audit.h" 1047b98a585SJames Morris #include "avc_ss.h" 1051da177e4SLinus Torvalds 106aa8e712cSStephen Smalley struct selinux_state selinux_state; 107aa8e712cSStephen Smalley 108d621d35eSPaul Moore /* SECMARK reference count */ 10956a4ca99SJames Morris static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0); 110d621d35eSPaul Moore 1111da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP 112aa8e712cSStephen Smalley static int selinux_enforcing_boot; 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds static int __init enforcing_setup(char *str) 1151da177e4SLinus Torvalds { 116f5269710SEric Paris unsigned long enforcing; 11729707b20SJingoo Han if (!kstrtoul(str, 0, &enforcing)) 118aa8e712cSStephen Smalley selinux_enforcing_boot = enforcing ? 1 : 0; 1191da177e4SLinus Torvalds return 1; 1201da177e4SLinus Torvalds } 1211da177e4SLinus Torvalds __setup("enforcing=", enforcing_setup); 122aa8e712cSStephen Smalley #else 123aa8e712cSStephen Smalley #define selinux_enforcing_boot 1 1241da177e4SLinus Torvalds #endif 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM 1271da177e4SLinus Torvalds int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE; 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds static int __init selinux_enabled_setup(char *str) 1301da177e4SLinus Torvalds { 131f5269710SEric Paris unsigned long enabled; 13229707b20SJingoo Han if (!kstrtoul(str, 0, &enabled)) 133f5269710SEric Paris selinux_enabled = enabled ? 1 : 0; 1341da177e4SLinus Torvalds return 1; 1351da177e4SLinus Torvalds } 1361da177e4SLinus Torvalds __setup("selinux=", selinux_enabled_setup); 13730d55280SStephen Smalley #else 13830d55280SStephen Smalley int selinux_enabled = 1; 1391da177e4SLinus Torvalds #endif 1401da177e4SLinus Torvalds 141aa8e712cSStephen Smalley static unsigned int selinux_checkreqprot_boot = 142aa8e712cSStephen Smalley CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE; 143aa8e712cSStephen Smalley 144aa8e712cSStephen Smalley static int __init checkreqprot_setup(char *str) 145aa8e712cSStephen Smalley { 146aa8e712cSStephen Smalley unsigned long checkreqprot; 147aa8e712cSStephen Smalley 148aa8e712cSStephen Smalley if (!kstrtoul(str, 0, &checkreqprot)) 149aa8e712cSStephen Smalley selinux_checkreqprot_boot = checkreqprot ? 1 : 0; 150aa8e712cSStephen Smalley return 1; 151aa8e712cSStephen Smalley } 152aa8e712cSStephen Smalley __setup("checkreqprot=", checkreqprot_setup); 153aa8e712cSStephen Smalley 154e18b890bSChristoph Lameter static struct kmem_cache *sel_inode_cache; 15563205654SSangwoo static struct kmem_cache *file_security_cache; 1567cae7e26SJames Morris 157d621d35eSPaul Moore /** 158d621d35eSPaul Moore * selinux_secmark_enabled - Check to see if SECMARK is currently enabled 159d621d35eSPaul Moore * 160d621d35eSPaul Moore * Description: 161d621d35eSPaul Moore * This function checks the SECMARK reference counter to see if any SECMARK 162d621d35eSPaul Moore * targets are currently configured, if the reference counter is greater than 163d621d35eSPaul Moore * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is 1642be4d74fSChris PeBenito * enabled, false (0) if SECMARK is disabled. If the always_check_network 1652be4d74fSChris PeBenito * policy capability is enabled, SECMARK is always considered enabled. 166d621d35eSPaul Moore * 167d621d35eSPaul Moore */ 168d621d35eSPaul Moore static int selinux_secmark_enabled(void) 169d621d35eSPaul Moore { 170aa8e712cSStephen Smalley return (selinux_policycap_alwaysnetwork() || 171aa8e712cSStephen Smalley atomic_read(&selinux_secmark_refcount)); 1722be4d74fSChris PeBenito } 1732be4d74fSChris PeBenito 1742be4d74fSChris PeBenito /** 1752be4d74fSChris PeBenito * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled 1762be4d74fSChris PeBenito * 1772be4d74fSChris PeBenito * Description: 1782be4d74fSChris PeBenito * This function checks if NetLabel or labeled IPSEC is enabled. Returns true 1792be4d74fSChris PeBenito * (1) if any are enabled or false (0) if neither are enabled. If the 1802be4d74fSChris PeBenito * always_check_network policy capability is enabled, peer labeling 1812be4d74fSChris PeBenito * is always considered enabled. 1822be4d74fSChris PeBenito * 1832be4d74fSChris PeBenito */ 1842be4d74fSChris PeBenito static int selinux_peerlbl_enabled(void) 1852be4d74fSChris PeBenito { 186aa8e712cSStephen Smalley return (selinux_policycap_alwaysnetwork() || 187aa8e712cSStephen Smalley netlbl_enabled() || selinux_xfrm_enabled()); 188d621d35eSPaul Moore } 189d621d35eSPaul Moore 190615e51fdSPaul Moore static int selinux_netcache_avc_callback(u32 event) 191615e51fdSPaul Moore { 192615e51fdSPaul Moore if (event == AVC_CALLBACK_RESET) { 193615e51fdSPaul Moore sel_netif_flush(); 194615e51fdSPaul Moore sel_netnode_flush(); 195615e51fdSPaul Moore sel_netport_flush(); 196615e51fdSPaul Moore synchronize_net(); 197615e51fdSPaul Moore } 198615e51fdSPaul Moore return 0; 199615e51fdSPaul Moore } 200615e51fdSPaul Moore 2018f408ab6SDaniel Jurgens static int selinux_lsm_notifier_avc_callback(u32 event) 2028f408ab6SDaniel Jurgens { 203409dcf31SDaniel Jurgens if (event == AVC_CALLBACK_RESET) { 204409dcf31SDaniel Jurgens sel_ib_pkey_flush(); 2058f408ab6SDaniel Jurgens call_lsm_notifier(LSM_POLICY_CHANGE, NULL); 206409dcf31SDaniel Jurgens } 2078f408ab6SDaniel Jurgens 2088f408ab6SDaniel Jurgens return 0; 2098f408ab6SDaniel Jurgens } 2108f408ab6SDaniel Jurgens 211d84f4f99SDavid Howells /* 212d84f4f99SDavid Howells * initialise the security for the init task 213d84f4f99SDavid Howells */ 214d84f4f99SDavid Howells static void cred_init_security(void) 2151da177e4SLinus Torvalds { 2163b11a1deSDavid Howells struct cred *cred = (struct cred *) current->real_cred; 2171da177e4SLinus Torvalds struct task_security_struct *tsec; 2181da177e4SLinus Torvalds 21989d155efSJames Morris tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL); 2201da177e4SLinus Torvalds if (!tsec) 221d84f4f99SDavid Howells panic("SELinux: Failed to initialize initial task.\n"); 2221da177e4SLinus Torvalds 223d84f4f99SDavid Howells tsec->osid = tsec->sid = SECINITSID_KERNEL; 224f1752eecSDavid Howells cred->security = tsec; 2251da177e4SLinus Torvalds } 2261da177e4SLinus Torvalds 227275bb41eSDavid Howells /* 22888e67f3bSDavid Howells * get the security ID of a set of credentials 22988e67f3bSDavid Howells */ 23088e67f3bSDavid Howells static inline u32 cred_sid(const struct cred *cred) 23188e67f3bSDavid Howells { 23288e67f3bSDavid Howells const struct task_security_struct *tsec; 23388e67f3bSDavid Howells 23488e67f3bSDavid Howells tsec = cred->security; 23588e67f3bSDavid Howells return tsec->sid; 23688e67f3bSDavid Howells } 23788e67f3bSDavid Howells 23888e67f3bSDavid Howells /* 2393b11a1deSDavid Howells * get the objective security ID of a task 240275bb41eSDavid Howells */ 241275bb41eSDavid Howells static inline u32 task_sid(const struct task_struct *task) 242275bb41eSDavid Howells { 243275bb41eSDavid Howells u32 sid; 244275bb41eSDavid Howells 245275bb41eSDavid Howells rcu_read_lock(); 24688e67f3bSDavid Howells sid = cred_sid(__task_cred(task)); 247275bb41eSDavid Howells rcu_read_unlock(); 248275bb41eSDavid Howells return sid; 249275bb41eSDavid Howells } 250275bb41eSDavid Howells 25188e67f3bSDavid Howells /* Allocate and free functions for each kind of security blob. */ 25288e67f3bSDavid Howells 2531da177e4SLinus Torvalds static int inode_alloc_security(struct inode *inode) 2541da177e4SLinus Torvalds { 2551da177e4SLinus Torvalds struct inode_security_struct *isec; 256275bb41eSDavid Howells u32 sid = current_sid(); 2571da177e4SLinus Torvalds 258a02fe132SJosef Bacik isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS); 2591da177e4SLinus Torvalds if (!isec) 2601da177e4SLinus Torvalds return -ENOMEM; 2611da177e4SLinus Torvalds 2629287aed2SAndreas Gruenbacher spin_lock_init(&isec->lock); 2631da177e4SLinus Torvalds INIT_LIST_HEAD(&isec->list); 2641da177e4SLinus Torvalds isec->inode = inode; 2651da177e4SLinus Torvalds isec->sid = SECINITSID_UNLABELED; 2661da177e4SLinus Torvalds isec->sclass = SECCLASS_FILE; 267275bb41eSDavid Howells isec->task_sid = sid; 26842059112SAndreas Gruenbacher isec->initialized = LABEL_INVALID; 2691da177e4SLinus Torvalds inode->i_security = isec; 2701da177e4SLinus Torvalds 2711da177e4SLinus Torvalds return 0; 2721da177e4SLinus Torvalds } 2731da177e4SLinus Torvalds 2745d226df4SAndreas Gruenbacher static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry); 2755d226df4SAndreas Gruenbacher 2765d226df4SAndreas Gruenbacher /* 2775d226df4SAndreas Gruenbacher * Try reloading inode security labels that have been marked as invalid. The 2785d226df4SAndreas Gruenbacher * @may_sleep parameter indicates when sleeping and thus reloading labels is 27942059112SAndreas Gruenbacher * allowed; when set to false, returns -ECHILD when the label is 280e9193288SAl Viro * invalid. The @dentry parameter should be set to a dentry of the inode. 2815d226df4SAndreas Gruenbacher */ 2825d226df4SAndreas Gruenbacher static int __inode_security_revalidate(struct inode *inode, 283e9193288SAl Viro struct dentry *dentry, 2845d226df4SAndreas Gruenbacher bool may_sleep) 2855d226df4SAndreas Gruenbacher { 2865d226df4SAndreas Gruenbacher struct inode_security_struct *isec = inode->i_security; 2875d226df4SAndreas Gruenbacher 2885d226df4SAndreas Gruenbacher might_sleep_if(may_sleep); 2895d226df4SAndreas Gruenbacher 290aa8e712cSStephen Smalley if (selinux_state.initialized && 291aa8e712cSStephen Smalley isec->initialized != LABEL_INITIALIZED) { 2925d226df4SAndreas Gruenbacher if (!may_sleep) 2935d226df4SAndreas Gruenbacher return -ECHILD; 2945d226df4SAndreas Gruenbacher 2955d226df4SAndreas Gruenbacher /* 2965d226df4SAndreas Gruenbacher * Try reloading the inode security label. This will fail if 2975d226df4SAndreas Gruenbacher * @opt_dentry is NULL and no dentry for this inode can be 2985d226df4SAndreas Gruenbacher * found; in that case, continue using the old label. 2995d226df4SAndreas Gruenbacher */ 300e9193288SAl Viro inode_doinit_with_dentry(inode, dentry); 3015d226df4SAndreas Gruenbacher } 3025d226df4SAndreas Gruenbacher return 0; 3035d226df4SAndreas Gruenbacher } 3045d226df4SAndreas Gruenbacher 3055d226df4SAndreas Gruenbacher static struct inode_security_struct *inode_security_novalidate(struct inode *inode) 3065d226df4SAndreas Gruenbacher { 3075d226df4SAndreas Gruenbacher return inode->i_security; 3085d226df4SAndreas Gruenbacher } 3095d226df4SAndreas Gruenbacher 3105d226df4SAndreas Gruenbacher static struct inode_security_struct *inode_security_rcu(struct inode *inode, bool rcu) 3115d226df4SAndreas Gruenbacher { 3125d226df4SAndreas Gruenbacher int error; 3135d226df4SAndreas Gruenbacher 3145d226df4SAndreas Gruenbacher error = __inode_security_revalidate(inode, NULL, !rcu); 3155d226df4SAndreas Gruenbacher if (error) 3165d226df4SAndreas Gruenbacher return ERR_PTR(error); 3175d226df4SAndreas Gruenbacher return inode->i_security; 3185d226df4SAndreas Gruenbacher } 3195d226df4SAndreas Gruenbacher 32083da53c5SAndreas Gruenbacher /* 32183da53c5SAndreas Gruenbacher * Get the security label of an inode. 32283da53c5SAndreas Gruenbacher */ 32383da53c5SAndreas Gruenbacher static struct inode_security_struct *inode_security(struct inode *inode) 32483da53c5SAndreas Gruenbacher { 3255d226df4SAndreas Gruenbacher __inode_security_revalidate(inode, NULL, true); 32683da53c5SAndreas Gruenbacher return inode->i_security; 32783da53c5SAndreas Gruenbacher } 32883da53c5SAndreas Gruenbacher 3292c97165bSPaul Moore static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry) 3302c97165bSPaul Moore { 3312c97165bSPaul Moore struct inode *inode = d_backing_inode(dentry); 3322c97165bSPaul Moore 3332c97165bSPaul Moore return inode->i_security; 3342c97165bSPaul Moore } 3352c97165bSPaul Moore 33683da53c5SAndreas Gruenbacher /* 33783da53c5SAndreas Gruenbacher * Get the security label of a dentry's backing inode. 33883da53c5SAndreas Gruenbacher */ 33983da53c5SAndreas Gruenbacher static struct inode_security_struct *backing_inode_security(struct dentry *dentry) 34083da53c5SAndreas Gruenbacher { 34183da53c5SAndreas Gruenbacher struct inode *inode = d_backing_inode(dentry); 34283da53c5SAndreas Gruenbacher 3435d226df4SAndreas Gruenbacher __inode_security_revalidate(inode, dentry, true); 34483da53c5SAndreas Gruenbacher return inode->i_security; 34583da53c5SAndreas Gruenbacher } 34683da53c5SAndreas Gruenbacher 3473dc91d43SSteven Rostedt static void inode_free_rcu(struct rcu_head *head) 3483dc91d43SSteven Rostedt { 3493dc91d43SSteven Rostedt struct inode_security_struct *isec; 3503dc91d43SSteven Rostedt 3513dc91d43SSteven Rostedt isec = container_of(head, struct inode_security_struct, rcu); 3523dc91d43SSteven Rostedt kmem_cache_free(sel_inode_cache, isec); 3533dc91d43SSteven Rostedt } 3543dc91d43SSteven Rostedt 3551da177e4SLinus Torvalds static void inode_free_security(struct inode *inode) 3561da177e4SLinus Torvalds { 3571da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 3581da177e4SLinus Torvalds struct superblock_security_struct *sbsec = inode->i_sb->s_security; 3591da177e4SLinus Torvalds 3609629d04aSWaiman Long /* 3619629d04aSWaiman Long * As not all inode security structures are in a list, we check for 3629629d04aSWaiman Long * empty list outside of the lock to make sure that we won't waste 3639629d04aSWaiman Long * time taking a lock doing nothing. 3649629d04aSWaiman Long * 3659629d04aSWaiman Long * The list_del_init() function can be safely called more than once. 3669629d04aSWaiman Long * It should not be possible for this function to be called with 3679629d04aSWaiman Long * concurrent list_add(), but for better safety against future changes 3689629d04aSWaiman Long * in the code, we use list_empty_careful() here. 3699629d04aSWaiman Long */ 3709629d04aSWaiman Long if (!list_empty_careful(&isec->list)) { 3711da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 3721da177e4SLinus Torvalds list_del_init(&isec->list); 3731da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 3749629d04aSWaiman Long } 3751da177e4SLinus Torvalds 3763dc91d43SSteven Rostedt /* 3773dc91d43SSteven Rostedt * The inode may still be referenced in a path walk and 3783dc91d43SSteven Rostedt * a call to selinux_inode_permission() can be made 3793dc91d43SSteven Rostedt * after inode_free_security() is called. Ideally, the VFS 3803dc91d43SSteven Rostedt * wouldn't do this, but fixing that is a much harder 3813dc91d43SSteven Rostedt * job. For now, simply free the i_security via RCU, and 3823dc91d43SSteven Rostedt * leave the current inode->i_security pointer intact. 3833dc91d43SSteven Rostedt * The inode will be freed after the RCU grace period too. 3843dc91d43SSteven Rostedt */ 3853dc91d43SSteven Rostedt call_rcu(&isec->rcu, inode_free_rcu); 3861da177e4SLinus Torvalds } 3871da177e4SLinus Torvalds 3881da177e4SLinus Torvalds static int file_alloc_security(struct file *file) 3891da177e4SLinus Torvalds { 3901da177e4SLinus Torvalds struct file_security_struct *fsec; 391275bb41eSDavid Howells u32 sid = current_sid(); 3921da177e4SLinus Torvalds 39363205654SSangwoo fsec = kmem_cache_zalloc(file_security_cache, GFP_KERNEL); 3941da177e4SLinus Torvalds if (!fsec) 3951da177e4SLinus Torvalds return -ENOMEM; 3961da177e4SLinus Torvalds 397275bb41eSDavid Howells fsec->sid = sid; 398275bb41eSDavid Howells fsec->fown_sid = sid; 3991da177e4SLinus Torvalds file->f_security = fsec; 4001da177e4SLinus Torvalds 4011da177e4SLinus Torvalds return 0; 4021da177e4SLinus Torvalds } 4031da177e4SLinus Torvalds 4041da177e4SLinus Torvalds static void file_free_security(struct file *file) 4051da177e4SLinus Torvalds { 4061da177e4SLinus Torvalds struct file_security_struct *fsec = file->f_security; 4071da177e4SLinus Torvalds file->f_security = NULL; 40863205654SSangwoo kmem_cache_free(file_security_cache, fsec); 4091da177e4SLinus Torvalds } 4101da177e4SLinus Torvalds 4111da177e4SLinus Torvalds static int superblock_alloc_security(struct super_block *sb) 4121da177e4SLinus Torvalds { 4131da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 4141da177e4SLinus Torvalds 41589d155efSJames Morris sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL); 4161da177e4SLinus Torvalds if (!sbsec) 4171da177e4SLinus Torvalds return -ENOMEM; 4181da177e4SLinus Torvalds 419bc7e982bSEric Paris mutex_init(&sbsec->lock); 4201da177e4SLinus Torvalds INIT_LIST_HEAD(&sbsec->isec_head); 4211da177e4SLinus Torvalds spin_lock_init(&sbsec->isec_lock); 4221da177e4SLinus Torvalds sbsec->sb = sb; 4231da177e4SLinus Torvalds sbsec->sid = SECINITSID_UNLABELED; 4241da177e4SLinus Torvalds sbsec->def_sid = SECINITSID_FILE; 425c312feb2SEric Paris sbsec->mntpoint_sid = SECINITSID_UNLABELED; 4261da177e4SLinus Torvalds sb->s_security = sbsec; 4271da177e4SLinus Torvalds 4281da177e4SLinus Torvalds return 0; 4291da177e4SLinus Torvalds } 4301da177e4SLinus Torvalds 4311da177e4SLinus Torvalds static void superblock_free_security(struct super_block *sb) 4321da177e4SLinus Torvalds { 4331da177e4SLinus Torvalds struct superblock_security_struct *sbsec = sb->s_security; 4341da177e4SLinus Torvalds sb->s_security = NULL; 4351da177e4SLinus Torvalds kfree(sbsec); 4361da177e4SLinus Torvalds } 4371da177e4SLinus Torvalds 438bd323655SAl Viro struct selinux_mnt_opts { 439bd323655SAl Viro const char *fscontext, *context, *rootcontext, *defcontext; 440bd323655SAl Viro }; 441bd323655SAl Viro 442204cc0ccSAl Viro static void selinux_free_mnt_opts(void *mnt_opts) 443204cc0ccSAl Viro { 444bd323655SAl Viro struct selinux_mnt_opts *opts = mnt_opts; 445bd323655SAl Viro kfree(opts->fscontext); 446bd323655SAl Viro kfree(opts->context); 447bd323655SAl Viro kfree(opts->rootcontext); 448bd323655SAl Viro kfree(opts->defcontext); 449204cc0ccSAl Viro kfree(opts); 450204cc0ccSAl Viro } 451204cc0ccSAl Viro 4521da177e4SLinus Torvalds static inline int inode_doinit(struct inode *inode) 4531da177e4SLinus Torvalds { 4541da177e4SLinus Torvalds return inode_doinit_with_dentry(inode, NULL); 4551da177e4SLinus Torvalds } 4561da177e4SLinus Torvalds 4571da177e4SLinus Torvalds enum { 45831e87930SEric Paris Opt_error = -1, 459442155c1SDavid Howells Opt_context = 0, 460442155c1SDavid Howells Opt_defcontext = 1, 4611da177e4SLinus Torvalds Opt_fscontext = 2, 462442155c1SDavid Howells Opt_rootcontext = 3, 463442155c1SDavid Howells Opt_seclabel = 4, 4641da177e4SLinus Torvalds }; 4651da177e4SLinus Torvalds 466da3d76abSAl Viro #define A(s, has_arg) {#s, sizeof(#s) - 1, Opt_##s, has_arg} 467169d68efSAl Viro static struct { 468169d68efSAl Viro const char *name; 469169d68efSAl Viro int len; 470169d68efSAl Viro int opt; 471169d68efSAl Viro bool has_arg; 472169d68efSAl Viro } tokens[] = { 473da3d76abSAl Viro A(context, true), 474da3d76abSAl Viro A(fscontext, true), 475da3d76abSAl Viro A(defcontext, true), 476da3d76abSAl Viro A(rootcontext, true), 477da3d76abSAl Viro A(seclabel, false), 4781da177e4SLinus Torvalds }; 479169d68efSAl Viro #undef A 480169d68efSAl Viro 481169d68efSAl Viro static int match_opt_prefix(char *s, int l, char **arg) 482169d68efSAl Viro { 483169d68efSAl Viro int i; 484169d68efSAl Viro 485169d68efSAl Viro for (i = 0; i < ARRAY_SIZE(tokens); i++) { 486169d68efSAl Viro size_t len = tokens[i].len; 487169d68efSAl Viro if (len > l || memcmp(s, tokens[i].name, len)) 488169d68efSAl Viro continue; 489169d68efSAl Viro if (tokens[i].has_arg) { 490169d68efSAl Viro if (len == l || s[len] != '=') 491169d68efSAl Viro continue; 492169d68efSAl Viro *arg = s + len + 1; 493169d68efSAl Viro } else if (len != l) 494169d68efSAl Viro continue; 495169d68efSAl Viro return tokens[i].opt; 496169d68efSAl Viro } 497169d68efSAl Viro return Opt_error; 498169d68efSAl Viro } 4991da177e4SLinus Torvalds 5001da177e4SLinus Torvalds #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n" 5011da177e4SLinus Torvalds 502c312feb2SEric Paris static int may_context_mount_sb_relabel(u32 sid, 503c312feb2SEric Paris struct superblock_security_struct *sbsec, 504275bb41eSDavid Howells const struct cred *cred) 505c312feb2SEric Paris { 506275bb41eSDavid Howells const struct task_security_struct *tsec = cred->security; 507c312feb2SEric Paris int rc; 508c312feb2SEric Paris 5096b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 5106b6bc620SStephen Smalley tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 511c312feb2SEric Paris FILESYSTEM__RELABELFROM, NULL); 512c312feb2SEric Paris if (rc) 513c312feb2SEric Paris return rc; 514c312feb2SEric Paris 5156b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 5166b6bc620SStephen Smalley tsec->sid, sid, SECCLASS_FILESYSTEM, 517c312feb2SEric Paris FILESYSTEM__RELABELTO, NULL); 518c312feb2SEric Paris return rc; 519c312feb2SEric Paris } 520c312feb2SEric Paris 5210808925eSEric Paris static int may_context_mount_inode_relabel(u32 sid, 5220808925eSEric Paris struct superblock_security_struct *sbsec, 523275bb41eSDavid Howells const struct cred *cred) 5240808925eSEric Paris { 525275bb41eSDavid Howells const struct task_security_struct *tsec = cred->security; 5260808925eSEric Paris int rc; 5276b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 5286b6bc620SStephen Smalley tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 5290808925eSEric Paris FILESYSTEM__RELABELFROM, NULL); 5300808925eSEric Paris if (rc) 5310808925eSEric Paris return rc; 5320808925eSEric Paris 5336b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 5346b6bc620SStephen Smalley sid, sbsec->sid, SECCLASS_FILESYSTEM, 5350808925eSEric Paris FILESYSTEM__ASSOCIATE, NULL); 5360808925eSEric Paris return rc; 5370808925eSEric Paris } 5380808925eSEric Paris 539b43e725dSEric Paris static int selinux_is_sblabel_mnt(struct super_block *sb) 540b43e725dSEric Paris { 541b43e725dSEric Paris struct superblock_security_struct *sbsec = sb->s_security; 542b43e725dSEric Paris 543d5f3a5f6SMark Salyzyn return sbsec->behavior == SECURITY_FS_USE_XATTR || 544b43e725dSEric Paris sbsec->behavior == SECURITY_FS_USE_TRANS || 545d5f3a5f6SMark Salyzyn sbsec->behavior == SECURITY_FS_USE_TASK || 5469fc2b4b4SJ. Bruce Fields sbsec->behavior == SECURITY_FS_USE_NATIVE || 547d5f3a5f6SMark Salyzyn /* Special handling. Genfs but also in-core setxattr handler */ 548d5f3a5f6SMark Salyzyn !strcmp(sb->s_type->name, "sysfs") || 549d5f3a5f6SMark Salyzyn !strcmp(sb->s_type->name, "pstore") || 550d5f3a5f6SMark Salyzyn !strcmp(sb->s_type->name, "debugfs") || 551a2c7c6fbSYongqin Liu !strcmp(sb->s_type->name, "tracefs") || 5522651225bSStephen Smalley !strcmp(sb->s_type->name, "rootfs") || 553aa8e712cSStephen Smalley (selinux_policycap_cgroupseclabel() && 5542651225bSStephen Smalley (!strcmp(sb->s_type->name, "cgroup") || 5552651225bSStephen Smalley !strcmp(sb->s_type->name, "cgroup2"))); 556b43e725dSEric Paris } 557b43e725dSEric Paris 558c9180a57SEric Paris static int sb_finish_set_opts(struct super_block *sb) 5591da177e4SLinus Torvalds { 5601da177e4SLinus Torvalds struct superblock_security_struct *sbsec = sb->s_security; 5611da177e4SLinus Torvalds struct dentry *root = sb->s_root; 562c6f493d6SDavid Howells struct inode *root_inode = d_backing_inode(root); 5631da177e4SLinus Torvalds int rc = 0; 5641da177e4SLinus Torvalds 5651da177e4SLinus Torvalds if (sbsec->behavior == SECURITY_FS_USE_XATTR) { 5661da177e4SLinus Torvalds /* Make sure that the xattr handler exists and that no 5671da177e4SLinus Torvalds error other than -ENODATA is returned by getxattr on 5681da177e4SLinus Torvalds the root directory. -ENODATA is ok, as this may be 5691da177e4SLinus Torvalds the first boot of the SELinux kernel before we have 5701da177e4SLinus Torvalds assigned xattr values to the filesystem. */ 5715d6c3191SAndreas Gruenbacher if (!(root_inode->i_opflags & IOP_XATTR)) { 572c103a91eSpeter enderborg pr_warn("SELinux: (dev %s, type %s) has no " 57329b1deb2SLinus Torvalds "xattr support\n", sb->s_id, sb->s_type->name); 5741da177e4SLinus Torvalds rc = -EOPNOTSUPP; 5751da177e4SLinus Torvalds goto out; 5761da177e4SLinus Torvalds } 5775d6c3191SAndreas Gruenbacher 5785d6c3191SAndreas Gruenbacher rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0); 5791da177e4SLinus Torvalds if (rc < 0 && rc != -ENODATA) { 5801da177e4SLinus Torvalds if (rc == -EOPNOTSUPP) 581c103a91eSpeter enderborg pr_warn("SELinux: (dev %s, type " 58229b1deb2SLinus Torvalds "%s) has no security xattr handler\n", 58329b1deb2SLinus Torvalds sb->s_id, sb->s_type->name); 5841da177e4SLinus Torvalds else 585c103a91eSpeter enderborg pr_warn("SELinux: (dev %s, type " 58629b1deb2SLinus Torvalds "%s) getxattr errno %d\n", sb->s_id, 58729b1deb2SLinus Torvalds sb->s_type->name, -rc); 5881da177e4SLinus Torvalds goto out; 5891da177e4SLinus Torvalds } 5901da177e4SLinus Torvalds } 5911da177e4SLinus Torvalds 592eadcabc6SEric Paris sbsec->flags |= SE_SBINITIALIZED; 5930b4d3452SScott Mayhew 5940b4d3452SScott Mayhew /* 5950b4d3452SScott Mayhew * Explicitly set or clear SBLABEL_MNT. It's not sufficient to simply 5960b4d3452SScott Mayhew * leave the flag untouched because sb_clone_mnt_opts might be handing 5970b4d3452SScott Mayhew * us a superblock that needs the flag to be cleared. 5980b4d3452SScott Mayhew */ 599b43e725dSEric Paris if (selinux_is_sblabel_mnt(sb)) 60012f348b9SEric Paris sbsec->flags |= SBLABEL_MNT; 6010b4d3452SScott Mayhew else 6020b4d3452SScott Mayhew sbsec->flags &= ~SBLABEL_MNT; 603ddd29ec6SDavid P. Quigley 6041da177e4SLinus Torvalds /* Initialize the root inode. */ 605c9180a57SEric Paris rc = inode_doinit_with_dentry(root_inode, root); 6061da177e4SLinus Torvalds 6071da177e4SLinus Torvalds /* Initialize any other inodes associated with the superblock, e.g. 6081da177e4SLinus Torvalds inodes created prior to initial policy load or inodes created 6091da177e4SLinus Torvalds during get_sb by a pseudo filesystem that directly 6101da177e4SLinus Torvalds populates itself. */ 6111da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 6128d64124aSAl Viro while (!list_empty(&sbsec->isec_head)) { 6131da177e4SLinus Torvalds struct inode_security_struct *isec = 6148d64124aSAl Viro list_first_entry(&sbsec->isec_head, 6151da177e4SLinus Torvalds struct inode_security_struct, list); 6161da177e4SLinus Torvalds struct inode *inode = isec->inode; 617923190d3SStephen Smalley list_del_init(&isec->list); 6181da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 6191da177e4SLinus Torvalds inode = igrab(inode); 6201da177e4SLinus Torvalds if (inode) { 6211da177e4SLinus Torvalds if (!IS_PRIVATE(inode)) 6221da177e4SLinus Torvalds inode_doinit(inode); 6231da177e4SLinus Torvalds iput(inode); 6241da177e4SLinus Torvalds } 6251da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 6261da177e4SLinus Torvalds } 6271da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 6281da177e4SLinus Torvalds out: 629c9180a57SEric Paris return rc; 630c9180a57SEric Paris } 631c9180a57SEric Paris 632c9180a57SEric Paris static int bad_option(struct superblock_security_struct *sbsec, char flag, 633c9180a57SEric Paris u32 old_sid, u32 new_sid) 634c9180a57SEric Paris { 6350d90a7ecSDavid P. Quigley char mnt_flags = sbsec->flags & SE_MNTMASK; 6360d90a7ecSDavid P. Quigley 637c9180a57SEric Paris /* check if the old mount command had the same options */ 6380d90a7ecSDavid P. Quigley if (sbsec->flags & SE_SBINITIALIZED) 639c9180a57SEric Paris if (!(sbsec->flags & flag) || 640c9180a57SEric Paris (old_sid != new_sid)) 641c9180a57SEric Paris return 1; 642c9180a57SEric Paris 643c9180a57SEric Paris /* check if we were passed the same options twice, 644c9180a57SEric Paris * aka someone passed context=a,context=b 645c9180a57SEric Paris */ 6460d90a7ecSDavid P. Quigley if (!(sbsec->flags & SE_SBINITIALIZED)) 6470d90a7ecSDavid P. Quigley if (mnt_flags & flag) 648c9180a57SEric Paris return 1; 649c9180a57SEric Paris return 0; 650c9180a57SEric Paris } 651e0007529SEric Paris 652bd323655SAl Viro static int parse_sid(struct super_block *sb, const char *s, u32 *sid) 653bd323655SAl Viro { 654bd323655SAl Viro int rc = security_context_str_to_sid(&selinux_state, s, 655bd323655SAl Viro sid, GFP_KERNEL); 656bd323655SAl Viro if (rc) 657bd323655SAl Viro pr_warn("SELinux: security_context_str_to_sid" 658bd323655SAl Viro "(%s) failed for (dev %s, type %s) errno=%d\n", 659bd323655SAl Viro s, sb->s_id, sb->s_type->name, rc); 660bd323655SAl Viro return rc; 661bd323655SAl Viro } 662bd323655SAl Viro 663c9180a57SEric Paris /* 664c9180a57SEric Paris * Allow filesystems with binary mount data to explicitly set mount point 665c9180a57SEric Paris * labeling information. 666c9180a57SEric Paris */ 667e0007529SEric Paris static int selinux_set_mnt_opts(struct super_block *sb, 668204cc0ccSAl Viro void *mnt_opts, 669649f6e77SDavid Quigley unsigned long kern_flags, 670649f6e77SDavid Quigley unsigned long *set_kern_flags) 671c9180a57SEric Paris { 672275bb41eSDavid Howells const struct cred *cred = current_cred(); 673c9180a57SEric Paris struct superblock_security_struct *sbsec = sb->s_security; 67483da53c5SAndreas Gruenbacher struct dentry *root = sbsec->sb->s_root; 675bd323655SAl Viro struct selinux_mnt_opts *opts = mnt_opts; 6762c97165bSPaul Moore struct inode_security_struct *root_isec; 677c9180a57SEric Paris u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0; 678c9180a57SEric Paris u32 defcontext_sid = 0; 679bd323655SAl Viro int rc = 0; 680c9180a57SEric Paris 681c9180a57SEric Paris mutex_lock(&sbsec->lock); 682c9180a57SEric Paris 683aa8e712cSStephen Smalley if (!selinux_state.initialized) { 684bd323655SAl Viro if (!opts) { 685c9180a57SEric Paris /* Defer initialization until selinux_complete_init, 686c9180a57SEric Paris after the initial policy is loaded and the security 687c9180a57SEric Paris server is ready to handle calls. */ 688c9180a57SEric Paris goto out; 689c9180a57SEric Paris } 690c9180a57SEric Paris rc = -EINVAL; 691c103a91eSpeter enderborg pr_warn("SELinux: Unable to set superblock options " 692744ba35eSEric Paris "before the security server is initialized\n"); 693c9180a57SEric Paris goto out; 694c9180a57SEric Paris } 695649f6e77SDavid Quigley if (kern_flags && !set_kern_flags) { 696649f6e77SDavid Quigley /* Specifying internal flags without providing a place to 697649f6e77SDavid Quigley * place the results is not allowed */ 698649f6e77SDavid Quigley rc = -EINVAL; 699649f6e77SDavid Quigley goto out; 700649f6e77SDavid Quigley } 701c9180a57SEric Paris 702c9180a57SEric Paris /* 703e0007529SEric Paris * Binary mount data FS will come through this function twice. Once 704e0007529SEric Paris * from an explicit call and once from the generic calls from the vfs. 705e0007529SEric Paris * Since the generic VFS calls will not contain any security mount data 706e0007529SEric Paris * we need to skip the double mount verification. 707e0007529SEric Paris * 708e0007529SEric Paris * This does open a hole in which we will not notice if the first 709e0007529SEric Paris * mount using this sb set explict options and a second mount using 710e0007529SEric Paris * this sb does not set any security options. (The first options 711e0007529SEric Paris * will be used for both mounts) 712e0007529SEric Paris */ 7130d90a7ecSDavid P. Quigley if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA) 714bd323655SAl Viro && !opts) 715e0007529SEric Paris goto out; 716e0007529SEric Paris 7172c97165bSPaul Moore root_isec = backing_inode_security_novalidate(root); 7182c97165bSPaul Moore 719e0007529SEric Paris /* 720c9180a57SEric Paris * parse the mount options, check if they are valid sids. 721c9180a57SEric Paris * also check if someone is trying to mount the same sb more 722c9180a57SEric Paris * than once with different security options. 723c9180a57SEric Paris */ 724bd323655SAl Viro if (opts) { 725bd323655SAl Viro if (opts->fscontext) { 726bd323655SAl Viro rc = parse_sid(sb, opts->fscontext, &fscontext_sid); 727bd323655SAl Viro if (rc) 728c9180a57SEric Paris goto out; 729c9180a57SEric Paris if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, 730c9180a57SEric Paris fscontext_sid)) 731c9180a57SEric Paris goto out_double_mount; 732c9180a57SEric Paris sbsec->flags |= FSCONTEXT_MNT; 733bd323655SAl Viro } 734bd323655SAl Viro if (opts->context) { 735bd323655SAl Viro rc = parse_sid(sb, opts->context, &context_sid); 736bd323655SAl Viro if (rc) 737bd323655SAl Viro goto out; 738c9180a57SEric Paris if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, 739c9180a57SEric Paris context_sid)) 740c9180a57SEric Paris goto out_double_mount; 741c9180a57SEric Paris sbsec->flags |= CONTEXT_MNT; 742bd323655SAl Viro } 743bd323655SAl Viro if (opts->rootcontext) { 744bd323655SAl Viro rc = parse_sid(sb, opts->rootcontext, &rootcontext_sid); 745bd323655SAl Viro if (rc) 746bd323655SAl Viro goto out; 747c9180a57SEric Paris if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, 748c9180a57SEric Paris rootcontext_sid)) 749c9180a57SEric Paris goto out_double_mount; 750c9180a57SEric Paris sbsec->flags |= ROOTCONTEXT_MNT; 751bd323655SAl Viro } 752bd323655SAl Viro if (opts->defcontext) { 753bd323655SAl Viro rc = parse_sid(sb, opts->defcontext, &defcontext_sid); 754bd323655SAl Viro if (rc) 755bd323655SAl Viro goto out; 756c9180a57SEric Paris if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, 757c9180a57SEric Paris defcontext_sid)) 758c9180a57SEric Paris goto out_double_mount; 759c9180a57SEric Paris sbsec->flags |= DEFCONTEXT_MNT; 760c9180a57SEric Paris } 761c9180a57SEric Paris } 762c9180a57SEric Paris 7630d90a7ecSDavid P. Quigley if (sbsec->flags & SE_SBINITIALIZED) { 764c9180a57SEric Paris /* previously mounted with options, but not on this attempt? */ 765bd323655SAl Viro if ((sbsec->flags & SE_MNTMASK) && !opts) 766c9180a57SEric Paris goto out_double_mount; 767c9180a57SEric Paris rc = 0; 768c9180a57SEric Paris goto out; 769c9180a57SEric Paris } 770c9180a57SEric Paris 771089be43eSJames Morris if (strcmp(sb->s_type->name, "proc") == 0) 772134509d5SStephen Smalley sbsec->flags |= SE_SBPROC | SE_SBGENFS; 773134509d5SStephen Smalley 7748e014720SStephen Smalley if (!strcmp(sb->s_type->name, "debugfs") || 7756a391183SJeff Vander Stoep !strcmp(sb->s_type->name, "tracefs") || 7768e014720SStephen Smalley !strcmp(sb->s_type->name, "sysfs") || 777901ef845SAntonio Murdaca !strcmp(sb->s_type->name, "pstore") || 778901ef845SAntonio Murdaca !strcmp(sb->s_type->name, "cgroup") || 779901ef845SAntonio Murdaca !strcmp(sb->s_type->name, "cgroup2")) 780134509d5SStephen Smalley sbsec->flags |= SE_SBGENFS; 781c9180a57SEric Paris 782eb9ae686SDavid Quigley if (!sbsec->behavior) { 783eb9ae686SDavid Quigley /* 784eb9ae686SDavid Quigley * Determine the labeling behavior to use for this 785eb9ae686SDavid Quigley * filesystem type. 786eb9ae686SDavid Quigley */ 787aa8e712cSStephen Smalley rc = security_fs_use(&selinux_state, sb); 788c9180a57SEric Paris if (rc) { 789c103a91eSpeter enderborg pr_warn("%s: security_fs_use(%s) returned %d\n", 790089be43eSJames Morris __func__, sb->s_type->name, rc); 791c9180a57SEric Paris goto out; 792c9180a57SEric Paris } 793eb9ae686SDavid Quigley } 794aad82892SSeth Forshee 795aad82892SSeth Forshee /* 79601593d32SStephen Smalley * If this is a user namespace mount and the filesystem type is not 79701593d32SStephen Smalley * explicitly whitelisted, then no contexts are allowed on the command 79801593d32SStephen Smalley * line and security labels must be ignored. 799aad82892SSeth Forshee */ 80001593d32SStephen Smalley if (sb->s_user_ns != &init_user_ns && 80101593d32SStephen Smalley strcmp(sb->s_type->name, "tmpfs") && 80201593d32SStephen Smalley strcmp(sb->s_type->name, "ramfs") && 80301593d32SStephen Smalley strcmp(sb->s_type->name, "devpts")) { 804aad82892SSeth Forshee if (context_sid || fscontext_sid || rootcontext_sid || 805aad82892SSeth Forshee defcontext_sid) { 806aad82892SSeth Forshee rc = -EACCES; 807aad82892SSeth Forshee goto out; 808aad82892SSeth Forshee } 809aad82892SSeth Forshee if (sbsec->behavior == SECURITY_FS_USE_XATTR) { 810aad82892SSeth Forshee sbsec->behavior = SECURITY_FS_USE_MNTPOINT; 811aa8e712cSStephen Smalley rc = security_transition_sid(&selinux_state, 812aa8e712cSStephen Smalley current_sid(), 813aa8e712cSStephen Smalley current_sid(), 814aad82892SSeth Forshee SECCLASS_FILE, NULL, 815aad82892SSeth Forshee &sbsec->mntpoint_sid); 816aad82892SSeth Forshee if (rc) 817aad82892SSeth Forshee goto out; 818aad82892SSeth Forshee } 819aad82892SSeth Forshee goto out_set_opts; 820aad82892SSeth Forshee } 821aad82892SSeth Forshee 822c9180a57SEric Paris /* sets the context of the superblock for the fs being mounted. */ 823c9180a57SEric Paris if (fscontext_sid) { 824275bb41eSDavid Howells rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred); 825c9180a57SEric Paris if (rc) 826c9180a57SEric Paris goto out; 827c9180a57SEric Paris 828c9180a57SEric Paris sbsec->sid = fscontext_sid; 829c9180a57SEric Paris } 830c9180a57SEric Paris 831c9180a57SEric Paris /* 832c9180a57SEric Paris * Switch to using mount point labeling behavior. 833c9180a57SEric Paris * sets the label used on all file below the mountpoint, and will set 834c9180a57SEric Paris * the superblock context if not already set. 835c9180a57SEric Paris */ 836eb9ae686SDavid Quigley if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) { 837eb9ae686SDavid Quigley sbsec->behavior = SECURITY_FS_USE_NATIVE; 838eb9ae686SDavid Quigley *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS; 839eb9ae686SDavid Quigley } 840eb9ae686SDavid Quigley 841c9180a57SEric Paris if (context_sid) { 842c9180a57SEric Paris if (!fscontext_sid) { 843275bb41eSDavid Howells rc = may_context_mount_sb_relabel(context_sid, sbsec, 844275bb41eSDavid Howells cred); 845c9180a57SEric Paris if (rc) 846c9180a57SEric Paris goto out; 847c9180a57SEric Paris sbsec->sid = context_sid; 848c9180a57SEric Paris } else { 849275bb41eSDavid Howells rc = may_context_mount_inode_relabel(context_sid, sbsec, 850275bb41eSDavid Howells cred); 851c9180a57SEric Paris if (rc) 852c9180a57SEric Paris goto out; 853c9180a57SEric Paris } 854c9180a57SEric Paris if (!rootcontext_sid) 855c9180a57SEric Paris rootcontext_sid = context_sid; 856c9180a57SEric Paris 857c9180a57SEric Paris sbsec->mntpoint_sid = context_sid; 858c9180a57SEric Paris sbsec->behavior = SECURITY_FS_USE_MNTPOINT; 859c9180a57SEric Paris } 860c9180a57SEric Paris 861c9180a57SEric Paris if (rootcontext_sid) { 862275bb41eSDavid Howells rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec, 863275bb41eSDavid Howells cred); 864c9180a57SEric Paris if (rc) 865c9180a57SEric Paris goto out; 866c9180a57SEric Paris 867c9180a57SEric Paris root_isec->sid = rootcontext_sid; 8686f3be9f5SAndreas Gruenbacher root_isec->initialized = LABEL_INITIALIZED; 869c9180a57SEric Paris } 870c9180a57SEric Paris 871c9180a57SEric Paris if (defcontext_sid) { 872eb9ae686SDavid Quigley if (sbsec->behavior != SECURITY_FS_USE_XATTR && 873eb9ae686SDavid Quigley sbsec->behavior != SECURITY_FS_USE_NATIVE) { 874c9180a57SEric Paris rc = -EINVAL; 875c103a91eSpeter enderborg pr_warn("SELinux: defcontext option is " 876c9180a57SEric Paris "invalid for this filesystem type\n"); 877c9180a57SEric Paris goto out; 878c9180a57SEric Paris } 879c9180a57SEric Paris 880c9180a57SEric Paris if (defcontext_sid != sbsec->def_sid) { 881c9180a57SEric Paris rc = may_context_mount_inode_relabel(defcontext_sid, 882275bb41eSDavid Howells sbsec, cred); 883c9180a57SEric Paris if (rc) 884c9180a57SEric Paris goto out; 885c9180a57SEric Paris } 886c9180a57SEric Paris 887c9180a57SEric Paris sbsec->def_sid = defcontext_sid; 888c9180a57SEric Paris } 889c9180a57SEric Paris 890aad82892SSeth Forshee out_set_opts: 891c9180a57SEric Paris rc = sb_finish_set_opts(sb); 892c9180a57SEric Paris out: 893bc7e982bSEric Paris mutex_unlock(&sbsec->lock); 8941da177e4SLinus Torvalds return rc; 895c9180a57SEric Paris out_double_mount: 896c9180a57SEric Paris rc = -EINVAL; 897c103a91eSpeter enderborg pr_warn("SELinux: mount invalid. Same superblock, different " 898bd323655SAl Viro "security settings for (dev %s, type %s)\n", sb->s_id, 899bd323655SAl Viro sb->s_type->name); 900c9180a57SEric Paris goto out; 901c9180a57SEric Paris } 902c9180a57SEric Paris 903094f7b69SJeff Layton static int selinux_cmp_sb_context(const struct super_block *oldsb, 904094f7b69SJeff Layton const struct super_block *newsb) 905094f7b69SJeff Layton { 906094f7b69SJeff Layton struct superblock_security_struct *old = oldsb->s_security; 907094f7b69SJeff Layton struct superblock_security_struct *new = newsb->s_security; 908094f7b69SJeff Layton char oldflags = old->flags & SE_MNTMASK; 909094f7b69SJeff Layton char newflags = new->flags & SE_MNTMASK; 910094f7b69SJeff Layton 911094f7b69SJeff Layton if (oldflags != newflags) 912094f7b69SJeff Layton goto mismatch; 913094f7b69SJeff Layton if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid) 914094f7b69SJeff Layton goto mismatch; 915094f7b69SJeff Layton if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid) 916094f7b69SJeff Layton goto mismatch; 917094f7b69SJeff Layton if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid) 918094f7b69SJeff Layton goto mismatch; 919094f7b69SJeff Layton if (oldflags & ROOTCONTEXT_MNT) { 92083da53c5SAndreas Gruenbacher struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root); 92183da53c5SAndreas Gruenbacher struct inode_security_struct *newroot = backing_inode_security(newsb->s_root); 922094f7b69SJeff Layton if (oldroot->sid != newroot->sid) 923094f7b69SJeff Layton goto mismatch; 924094f7b69SJeff Layton } 925094f7b69SJeff Layton return 0; 926094f7b69SJeff Layton mismatch: 927c103a91eSpeter enderborg pr_warn("SELinux: mount invalid. Same superblock, " 928094f7b69SJeff Layton "different security settings for (dev %s, " 929094f7b69SJeff Layton "type %s)\n", newsb->s_id, newsb->s_type->name); 930094f7b69SJeff Layton return -EBUSY; 931094f7b69SJeff Layton } 932094f7b69SJeff Layton 933094f7b69SJeff Layton static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb, 9340b4d3452SScott Mayhew struct super_block *newsb, 9350b4d3452SScott Mayhew unsigned long kern_flags, 9360b4d3452SScott Mayhew unsigned long *set_kern_flags) 937c9180a57SEric Paris { 9380b4d3452SScott Mayhew int rc = 0; 939c9180a57SEric Paris const struct superblock_security_struct *oldsbsec = oldsb->s_security; 940c9180a57SEric Paris struct superblock_security_struct *newsbsec = newsb->s_security; 941c9180a57SEric Paris 942c9180a57SEric Paris int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT); 943c9180a57SEric Paris int set_context = (oldsbsec->flags & CONTEXT_MNT); 944c9180a57SEric Paris int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT); 945c9180a57SEric Paris 9460f5e6420SEric Paris /* 9470f5e6420SEric Paris * if the parent was able to be mounted it clearly had no special lsm 948e8c26255SAl Viro * mount options. thus we can safely deal with this superblock later 9490f5e6420SEric Paris */ 950aa8e712cSStephen Smalley if (!selinux_state.initialized) 951094f7b69SJeff Layton return 0; 952c9180a57SEric Paris 9530b4d3452SScott Mayhew /* 9540b4d3452SScott Mayhew * Specifying internal flags without providing a place to 9550b4d3452SScott Mayhew * place the results is not allowed. 9560b4d3452SScott Mayhew */ 9570b4d3452SScott Mayhew if (kern_flags && !set_kern_flags) 9580b4d3452SScott Mayhew return -EINVAL; 9590b4d3452SScott Mayhew 960c9180a57SEric Paris /* how can we clone if the old one wasn't set up?? */ 9610d90a7ecSDavid P. Quigley BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED)); 962c9180a57SEric Paris 963094f7b69SJeff Layton /* if fs is reusing a sb, make sure that the contexts match */ 9640d90a7ecSDavid P. Quigley if (newsbsec->flags & SE_SBINITIALIZED) 965094f7b69SJeff Layton return selinux_cmp_sb_context(oldsb, newsb); 9665a552617SEric Paris 967c9180a57SEric Paris mutex_lock(&newsbsec->lock); 968c9180a57SEric Paris 969c9180a57SEric Paris newsbsec->flags = oldsbsec->flags; 970c9180a57SEric Paris 971c9180a57SEric Paris newsbsec->sid = oldsbsec->sid; 972c9180a57SEric Paris newsbsec->def_sid = oldsbsec->def_sid; 973c9180a57SEric Paris newsbsec->behavior = oldsbsec->behavior; 974c9180a57SEric Paris 9750b4d3452SScott Mayhew if (newsbsec->behavior == SECURITY_FS_USE_NATIVE && 9760b4d3452SScott Mayhew !(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) { 977aa8e712cSStephen Smalley rc = security_fs_use(&selinux_state, newsb); 9780b4d3452SScott Mayhew if (rc) 9790b4d3452SScott Mayhew goto out; 9800b4d3452SScott Mayhew } 9810b4d3452SScott Mayhew 9820b4d3452SScott Mayhew if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) { 9830b4d3452SScott Mayhew newsbsec->behavior = SECURITY_FS_USE_NATIVE; 9840b4d3452SScott Mayhew *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS; 9850b4d3452SScott Mayhew } 9860b4d3452SScott Mayhew 987c9180a57SEric Paris if (set_context) { 988c9180a57SEric Paris u32 sid = oldsbsec->mntpoint_sid; 989c9180a57SEric Paris 990c9180a57SEric Paris if (!set_fscontext) 991c9180a57SEric Paris newsbsec->sid = sid; 992c9180a57SEric Paris if (!set_rootcontext) { 99383da53c5SAndreas Gruenbacher struct inode_security_struct *newisec = backing_inode_security(newsb->s_root); 994c9180a57SEric Paris newisec->sid = sid; 995c9180a57SEric Paris } 996c9180a57SEric Paris newsbsec->mntpoint_sid = sid; 997c9180a57SEric Paris } 998c9180a57SEric Paris if (set_rootcontext) { 99983da53c5SAndreas Gruenbacher const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root); 100083da53c5SAndreas Gruenbacher struct inode_security_struct *newisec = backing_inode_security(newsb->s_root); 1001c9180a57SEric Paris 1002c9180a57SEric Paris newisec->sid = oldisec->sid; 1003c9180a57SEric Paris } 1004c9180a57SEric Paris 1005c9180a57SEric Paris sb_finish_set_opts(newsb); 10060b4d3452SScott Mayhew out: 1007c9180a57SEric Paris mutex_unlock(&newsbsec->lock); 10080b4d3452SScott Mayhew return rc; 1009c9180a57SEric Paris } 1010c9180a57SEric Paris 1011ba641862SAl Viro static int selinux_add_opt(int token, const char *s, void **mnt_opts) 1012c9180a57SEric Paris { 1013ba641862SAl Viro struct selinux_mnt_opts *opts = *mnt_opts; 1014c9180a57SEric Paris 1015da3d76abSAl Viro if (token == Opt_seclabel) /* eaten and completely ignored */ 1016e0007529SEric Paris return 0; 1017e0007529SEric Paris 1018ba641862SAl Viro if (!opts) { 1019ba641862SAl Viro opts = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL); 1020ba641862SAl Viro if (!opts) 1021ba641862SAl Viro return -ENOMEM; 1022ba641862SAl Viro *mnt_opts = opts; 1023ba641862SAl Viro } 1024ba641862SAl Viro if (!s) 1025ba641862SAl Viro return -ENOMEM; 1026ba641862SAl Viro switch (token) { 1027ba641862SAl Viro case Opt_context: 1028ba641862SAl Viro if (opts->context || opts->defcontext) 1029ba641862SAl Viro goto Einval; 1030ba641862SAl Viro opts->context = s; 1031ba641862SAl Viro break; 1032ba641862SAl Viro case Opt_fscontext: 1033ba641862SAl Viro if (opts->fscontext) 1034ba641862SAl Viro goto Einval; 1035ba641862SAl Viro opts->fscontext = s; 1036ba641862SAl Viro break; 1037ba641862SAl Viro case Opt_rootcontext: 1038ba641862SAl Viro if (opts->rootcontext) 1039ba641862SAl Viro goto Einval; 1040ba641862SAl Viro opts->rootcontext = s; 1041ba641862SAl Viro break; 1042ba641862SAl Viro case Opt_defcontext: 1043ba641862SAl Viro if (opts->context || opts->defcontext) 1044ba641862SAl Viro goto Einval; 1045ba641862SAl Viro opts->defcontext = s; 1046ba641862SAl Viro break; 1047ba641862SAl Viro } 1048ba641862SAl Viro return 0; 1049ba641862SAl Viro Einval: 1050ba641862SAl Viro pr_warn(SEL_MOUNT_FAIL_MSG); 1051ba641862SAl Viro return -EINVAL; 1052ba641862SAl Viro } 1053ba641862SAl Viro 1054757cbe59SAl Viro static int selinux_add_mnt_opt(const char *option, const char *val, int len, 1055204cc0ccSAl Viro void **mnt_opts) 1056c9180a57SEric Paris { 1057757cbe59SAl Viro int token = Opt_error; 1058757cbe59SAl Viro int rc, i; 1059c9180a57SEric Paris 1060757cbe59SAl Viro for (i = 0; i < ARRAY_SIZE(tokens); i++) { 1061757cbe59SAl Viro if (strcmp(option, tokens[i].name) == 0) { 1062757cbe59SAl Viro token = tokens[i].opt; 1063757cbe59SAl Viro break; 1064757cbe59SAl Viro } 1065169d68efSAl Viro } 1066169d68efSAl Viro 1067757cbe59SAl Viro if (token == Opt_error) 1068757cbe59SAl Viro return -EINVAL; 1069c9180a57SEric Paris 1070757cbe59SAl Viro if (token != Opt_seclabel) 1071757cbe59SAl Viro val = kmemdup_nul(val, len, GFP_KERNEL); 1072757cbe59SAl Viro rc = selinux_add_opt(token, val, mnt_opts); 1073757cbe59SAl Viro if (unlikely(rc)) { 1074757cbe59SAl Viro kfree(val); 1075757cbe59SAl Viro if (*mnt_opts) { 1076ba641862SAl Viro selinux_free_mnt_opts(*mnt_opts); 1077ba641862SAl Viro *mnt_opts = NULL; 1078757cbe59SAl Viro } 1079757cbe59SAl Viro } 1080c9180a57SEric Paris return rc; 10811da177e4SLinus Torvalds } 10821da177e4SLinus Torvalds 1083e3489f89SAl Viro static int show_sid(struct seq_file *m, u32 sid) 10842069f457SEric Paris { 1085e3489f89SAl Viro char *context = NULL; 1086e3489f89SAl Viro u32 len; 1087e3489f89SAl Viro int rc; 10882069f457SEric Paris 1089e3489f89SAl Viro rc = security_sid_to_context(&selinux_state, sid, 1090e3489f89SAl Viro &context, &len); 1091e3489f89SAl Viro if (!rc) { 1092e3489f89SAl Viro bool has_comma = context && strchr(context, ','); 109311689d47SDavid P. Quigley 1094442155c1SDavid Howells seq_putc(m, '='); 10952069f457SEric Paris if (has_comma) 10962069f457SEric Paris seq_putc(m, '\"'); 1097e3489f89SAl Viro seq_escape(m, context, "\"\n\\"); 10982069f457SEric Paris if (has_comma) 10992069f457SEric Paris seq_putc(m, '\"'); 11002069f457SEric Paris } 11011da177e4SLinus Torvalds kfree(context); 11021da177e4SLinus Torvalds return rc; 11031da177e4SLinus Torvalds } 11042069f457SEric Paris 11052069f457SEric Paris static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb) 11062069f457SEric Paris { 1107e3489f89SAl Viro struct superblock_security_struct *sbsec = sb->s_security; 11082069f457SEric Paris int rc; 11092069f457SEric Paris 1110e3489f89SAl Viro if (!(sbsec->flags & SE_SBINITIALIZED)) 1111e3489f89SAl Viro return 0; 1112e3489f89SAl Viro 1113e3489f89SAl Viro if (!selinux_state.initialized) 1114e3489f89SAl Viro return 0; 1115e3489f89SAl Viro 1116e3489f89SAl Viro if (sbsec->flags & FSCONTEXT_MNT) { 1117e3489f89SAl Viro seq_putc(m, ','); 1118e3489f89SAl Viro seq_puts(m, FSCONTEXT_STR); 1119e3489f89SAl Viro rc = show_sid(m, sbsec->sid); 1120e3489f89SAl Viro if (rc) 11212069f457SEric Paris return rc; 1122383795c2SEric Paris } 1123e3489f89SAl Viro if (sbsec->flags & CONTEXT_MNT) { 1124e3489f89SAl Viro seq_putc(m, ','); 1125e3489f89SAl Viro seq_puts(m, CONTEXT_STR); 1126e3489f89SAl Viro rc = show_sid(m, sbsec->mntpoint_sid); 1127e3489f89SAl Viro if (rc) 11282069f457SEric Paris return rc; 11292069f457SEric Paris } 1130e3489f89SAl Viro if (sbsec->flags & DEFCONTEXT_MNT) { 1131e3489f89SAl Viro seq_putc(m, ','); 1132e3489f89SAl Viro seq_puts(m, DEFCONTEXT_STR); 1133e3489f89SAl Viro rc = show_sid(m, sbsec->def_sid); 1134e3489f89SAl Viro if (rc) 1135e3489f89SAl Viro return rc; 1136e3489f89SAl Viro } 1137e3489f89SAl Viro if (sbsec->flags & ROOTCONTEXT_MNT) { 1138e3489f89SAl Viro struct dentry *root = sbsec->sb->s_root; 1139e3489f89SAl Viro struct inode_security_struct *isec = backing_inode_security(root); 1140e3489f89SAl Viro seq_putc(m, ','); 1141e3489f89SAl Viro seq_puts(m, ROOTCONTEXT_STR); 1142e3489f89SAl Viro rc = show_sid(m, isec->sid); 1143e3489f89SAl Viro if (rc) 1144e3489f89SAl Viro return rc; 1145e3489f89SAl Viro } 1146e3489f89SAl Viro if (sbsec->flags & SBLABEL_MNT) { 1147e3489f89SAl Viro seq_putc(m, ','); 1148442155c1SDavid Howells seq_puts(m, SECLABEL_STR); 1149e3489f89SAl Viro } 1150e3489f89SAl Viro return 0; 1151e3489f89SAl Viro } 11522069f457SEric Paris 11531da177e4SLinus Torvalds static inline u16 inode_mode_to_security_class(umode_t mode) 11541da177e4SLinus Torvalds { 11551da177e4SLinus Torvalds switch (mode & S_IFMT) { 11561da177e4SLinus Torvalds case S_IFSOCK: 11571da177e4SLinus Torvalds return SECCLASS_SOCK_FILE; 11581da177e4SLinus Torvalds case S_IFLNK: 11591da177e4SLinus Torvalds return SECCLASS_LNK_FILE; 11601da177e4SLinus Torvalds case S_IFREG: 11611da177e4SLinus Torvalds return SECCLASS_FILE; 11621da177e4SLinus Torvalds case S_IFBLK: 11631da177e4SLinus Torvalds return SECCLASS_BLK_FILE; 11641da177e4SLinus Torvalds case S_IFDIR: 11651da177e4SLinus Torvalds return SECCLASS_DIR; 11661da177e4SLinus Torvalds case S_IFCHR: 11671da177e4SLinus Torvalds return SECCLASS_CHR_FILE; 11681da177e4SLinus Torvalds case S_IFIFO: 11691da177e4SLinus Torvalds return SECCLASS_FIFO_FILE; 11701da177e4SLinus Torvalds 11711da177e4SLinus Torvalds } 11721da177e4SLinus Torvalds 11731da177e4SLinus Torvalds return SECCLASS_FILE; 11741da177e4SLinus Torvalds } 11751da177e4SLinus Torvalds 117613402580SJames Morris static inline int default_protocol_stream(int protocol) 117713402580SJames Morris { 117813402580SJames Morris return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP); 117913402580SJames Morris } 118013402580SJames Morris 118113402580SJames Morris static inline int default_protocol_dgram(int protocol) 118213402580SJames Morris { 118313402580SJames Morris return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP); 118413402580SJames Morris } 118513402580SJames Morris 11861da177e4SLinus Torvalds static inline u16 socket_type_to_security_class(int family, int type, int protocol) 11871da177e4SLinus Torvalds { 1188aa8e712cSStephen Smalley int extsockclass = selinux_policycap_extsockclass(); 1189da69a530SStephen Smalley 11901da177e4SLinus Torvalds switch (family) { 11911da177e4SLinus Torvalds case PF_UNIX: 11921da177e4SLinus Torvalds switch (type) { 11931da177e4SLinus Torvalds case SOCK_STREAM: 11941da177e4SLinus Torvalds case SOCK_SEQPACKET: 11951da177e4SLinus Torvalds return SECCLASS_UNIX_STREAM_SOCKET; 11961da177e4SLinus Torvalds case SOCK_DGRAM: 11972a764b52SLuis Ressel case SOCK_RAW: 11981da177e4SLinus Torvalds return SECCLASS_UNIX_DGRAM_SOCKET; 11991da177e4SLinus Torvalds } 12001da177e4SLinus Torvalds break; 12011da177e4SLinus Torvalds case PF_INET: 12021da177e4SLinus Torvalds case PF_INET6: 12031da177e4SLinus Torvalds switch (type) { 12041da177e4SLinus Torvalds case SOCK_STREAM: 1205da69a530SStephen Smalley case SOCK_SEQPACKET: 120613402580SJames Morris if (default_protocol_stream(protocol)) 12071da177e4SLinus Torvalds return SECCLASS_TCP_SOCKET; 1208da69a530SStephen Smalley else if (extsockclass && protocol == IPPROTO_SCTP) 1209da69a530SStephen Smalley return SECCLASS_SCTP_SOCKET; 121013402580SJames Morris else 121113402580SJames Morris return SECCLASS_RAWIP_SOCKET; 12121da177e4SLinus Torvalds case SOCK_DGRAM: 121313402580SJames Morris if (default_protocol_dgram(protocol)) 12141da177e4SLinus Torvalds return SECCLASS_UDP_SOCKET; 1215ef37979aSStephen Smalley else if (extsockclass && (protocol == IPPROTO_ICMP || 1216ef37979aSStephen Smalley protocol == IPPROTO_ICMPV6)) 1217da69a530SStephen Smalley return SECCLASS_ICMP_SOCKET; 121813402580SJames Morris else 121913402580SJames Morris return SECCLASS_RAWIP_SOCKET; 12202ee92d46SJames Morris case SOCK_DCCP: 12212ee92d46SJames Morris return SECCLASS_DCCP_SOCKET; 122213402580SJames Morris default: 12231da177e4SLinus Torvalds return SECCLASS_RAWIP_SOCKET; 12241da177e4SLinus Torvalds } 12251da177e4SLinus Torvalds break; 12261da177e4SLinus Torvalds case PF_NETLINK: 12271da177e4SLinus Torvalds switch (protocol) { 12281da177e4SLinus Torvalds case NETLINK_ROUTE: 12291da177e4SLinus Torvalds return SECCLASS_NETLINK_ROUTE_SOCKET; 12307f1fb60cSPavel Emelyanov case NETLINK_SOCK_DIAG: 12311da177e4SLinus Torvalds return SECCLASS_NETLINK_TCPDIAG_SOCKET; 12321da177e4SLinus Torvalds case NETLINK_NFLOG: 12331da177e4SLinus Torvalds return SECCLASS_NETLINK_NFLOG_SOCKET; 12341da177e4SLinus Torvalds case NETLINK_XFRM: 12351da177e4SLinus Torvalds return SECCLASS_NETLINK_XFRM_SOCKET; 12361da177e4SLinus Torvalds case NETLINK_SELINUX: 12371da177e4SLinus Torvalds return SECCLASS_NETLINK_SELINUX_SOCKET; 12386c6d2e9bSStephen Smalley case NETLINK_ISCSI: 12396c6d2e9bSStephen Smalley return SECCLASS_NETLINK_ISCSI_SOCKET; 12401da177e4SLinus Torvalds case NETLINK_AUDIT: 12411da177e4SLinus Torvalds return SECCLASS_NETLINK_AUDIT_SOCKET; 12426c6d2e9bSStephen Smalley case NETLINK_FIB_LOOKUP: 12436c6d2e9bSStephen Smalley return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET; 12446c6d2e9bSStephen Smalley case NETLINK_CONNECTOR: 12456c6d2e9bSStephen Smalley return SECCLASS_NETLINK_CONNECTOR_SOCKET; 12466c6d2e9bSStephen Smalley case NETLINK_NETFILTER: 12476c6d2e9bSStephen Smalley return SECCLASS_NETLINK_NETFILTER_SOCKET; 12481da177e4SLinus Torvalds case NETLINK_DNRTMSG: 12491da177e4SLinus Torvalds return SECCLASS_NETLINK_DNRT_SOCKET; 12500c9b7942SJames Morris case NETLINK_KOBJECT_UEVENT: 12510c9b7942SJames Morris return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET; 12526c6d2e9bSStephen Smalley case NETLINK_GENERIC: 12536c6d2e9bSStephen Smalley return SECCLASS_NETLINK_GENERIC_SOCKET; 12546c6d2e9bSStephen Smalley case NETLINK_SCSITRANSPORT: 12556c6d2e9bSStephen Smalley return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET; 12566c6d2e9bSStephen Smalley case NETLINK_RDMA: 12576c6d2e9bSStephen Smalley return SECCLASS_NETLINK_RDMA_SOCKET; 12586c6d2e9bSStephen Smalley case NETLINK_CRYPTO: 12596c6d2e9bSStephen Smalley return SECCLASS_NETLINK_CRYPTO_SOCKET; 12601da177e4SLinus Torvalds default: 12611da177e4SLinus Torvalds return SECCLASS_NETLINK_SOCKET; 12621da177e4SLinus Torvalds } 12631da177e4SLinus Torvalds case PF_PACKET: 12641da177e4SLinus Torvalds return SECCLASS_PACKET_SOCKET; 12651da177e4SLinus Torvalds case PF_KEY: 12661da177e4SLinus Torvalds return SECCLASS_KEY_SOCKET; 12673e3ff15eSChristopher J. PeBenito case PF_APPLETALK: 12683e3ff15eSChristopher J. PeBenito return SECCLASS_APPLETALK_SOCKET; 12691da177e4SLinus Torvalds } 12701da177e4SLinus Torvalds 1271da69a530SStephen Smalley if (extsockclass) { 1272da69a530SStephen Smalley switch (family) { 1273da69a530SStephen Smalley case PF_AX25: 1274da69a530SStephen Smalley return SECCLASS_AX25_SOCKET; 1275da69a530SStephen Smalley case PF_IPX: 1276da69a530SStephen Smalley return SECCLASS_IPX_SOCKET; 1277da69a530SStephen Smalley case PF_NETROM: 1278da69a530SStephen Smalley return SECCLASS_NETROM_SOCKET; 1279da69a530SStephen Smalley case PF_ATMPVC: 1280da69a530SStephen Smalley return SECCLASS_ATMPVC_SOCKET; 1281da69a530SStephen Smalley case PF_X25: 1282da69a530SStephen Smalley return SECCLASS_X25_SOCKET; 1283da69a530SStephen Smalley case PF_ROSE: 1284da69a530SStephen Smalley return SECCLASS_ROSE_SOCKET; 1285da69a530SStephen Smalley case PF_DECnet: 1286da69a530SStephen Smalley return SECCLASS_DECNET_SOCKET; 1287da69a530SStephen Smalley case PF_ATMSVC: 1288da69a530SStephen Smalley return SECCLASS_ATMSVC_SOCKET; 1289da69a530SStephen Smalley case PF_RDS: 1290da69a530SStephen Smalley return SECCLASS_RDS_SOCKET; 1291da69a530SStephen Smalley case PF_IRDA: 1292da69a530SStephen Smalley return SECCLASS_IRDA_SOCKET; 1293da69a530SStephen Smalley case PF_PPPOX: 1294da69a530SStephen Smalley return SECCLASS_PPPOX_SOCKET; 1295da69a530SStephen Smalley case PF_LLC: 1296da69a530SStephen Smalley return SECCLASS_LLC_SOCKET; 1297da69a530SStephen Smalley case PF_CAN: 1298da69a530SStephen Smalley return SECCLASS_CAN_SOCKET; 1299da69a530SStephen Smalley case PF_TIPC: 1300da69a530SStephen Smalley return SECCLASS_TIPC_SOCKET; 1301da69a530SStephen Smalley case PF_BLUETOOTH: 1302da69a530SStephen Smalley return SECCLASS_BLUETOOTH_SOCKET; 1303da69a530SStephen Smalley case PF_IUCV: 1304da69a530SStephen Smalley return SECCLASS_IUCV_SOCKET; 1305da69a530SStephen Smalley case PF_RXRPC: 1306da69a530SStephen Smalley return SECCLASS_RXRPC_SOCKET; 1307da69a530SStephen Smalley case PF_ISDN: 1308da69a530SStephen Smalley return SECCLASS_ISDN_SOCKET; 1309da69a530SStephen Smalley case PF_PHONET: 1310da69a530SStephen Smalley return SECCLASS_PHONET_SOCKET; 1311da69a530SStephen Smalley case PF_IEEE802154: 1312da69a530SStephen Smalley return SECCLASS_IEEE802154_SOCKET; 1313da69a530SStephen Smalley case PF_CAIF: 1314da69a530SStephen Smalley return SECCLASS_CAIF_SOCKET; 1315da69a530SStephen Smalley case PF_ALG: 1316da69a530SStephen Smalley return SECCLASS_ALG_SOCKET; 1317da69a530SStephen Smalley case PF_NFC: 1318da69a530SStephen Smalley return SECCLASS_NFC_SOCKET; 1319da69a530SStephen Smalley case PF_VSOCK: 1320da69a530SStephen Smalley return SECCLASS_VSOCK_SOCKET; 1321da69a530SStephen Smalley case PF_KCM: 1322da69a530SStephen Smalley return SECCLASS_KCM_SOCKET; 1323da69a530SStephen Smalley case PF_QIPCRTR: 1324da69a530SStephen Smalley return SECCLASS_QIPCRTR_SOCKET; 13253051bf36SLinus Torvalds case PF_SMC: 13263051bf36SLinus Torvalds return SECCLASS_SMC_SOCKET; 132768e8b849SBjörn Töpel case PF_XDP: 132868e8b849SBjörn Töpel return SECCLASS_XDP_SOCKET; 132968e8b849SBjörn Töpel #if PF_MAX > 45 1330da69a530SStephen Smalley #error New address family defined, please update this function. 1331da69a530SStephen Smalley #endif 1332da69a530SStephen Smalley } 1333da69a530SStephen Smalley } 1334da69a530SStephen Smalley 13351da177e4SLinus Torvalds return SECCLASS_SOCKET; 13361da177e4SLinus Torvalds } 13371da177e4SLinus Torvalds 1338134509d5SStephen Smalley static int selinux_genfs_get_sid(struct dentry *dentry, 13391da177e4SLinus Torvalds u16 tclass, 1340134509d5SStephen Smalley u16 flags, 13411da177e4SLinus Torvalds u32 *sid) 13421da177e4SLinus Torvalds { 13438e6c9693SLucian Adrian Grijincu int rc; 1344fc64005cSAl Viro struct super_block *sb = dentry->d_sb; 13458e6c9693SLucian Adrian Grijincu char *buffer, *path; 13461da177e4SLinus Torvalds 13471da177e4SLinus Torvalds buffer = (char *)__get_free_page(GFP_KERNEL); 13481da177e4SLinus Torvalds if (!buffer) 13491da177e4SLinus Torvalds return -ENOMEM; 13501da177e4SLinus Torvalds 13518e6c9693SLucian Adrian Grijincu path = dentry_path_raw(dentry, buffer, PAGE_SIZE); 13528e6c9693SLucian Adrian Grijincu if (IS_ERR(path)) 13538e6c9693SLucian Adrian Grijincu rc = PTR_ERR(path); 13548e6c9693SLucian Adrian Grijincu else { 1355134509d5SStephen Smalley if (flags & SE_SBPROC) { 13568e6c9693SLucian Adrian Grijincu /* each process gets a /proc/PID/ entry. Strip off the 13578e6c9693SLucian Adrian Grijincu * PID part to get a valid selinux labeling. 13588e6c9693SLucian Adrian Grijincu * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */ 13598e6c9693SLucian Adrian Grijincu while (path[1] >= '0' && path[1] <= '9') { 13608e6c9693SLucian Adrian Grijincu path[1] = '/'; 13618e6c9693SLucian Adrian Grijincu path++; 13621da177e4SLinus Torvalds } 1363134509d5SStephen Smalley } 1364aa8e712cSStephen Smalley rc = security_genfs_sid(&selinux_state, sb->s_type->name, 1365aa8e712cSStephen Smalley path, tclass, sid); 13667bb185edSStephen Smalley if (rc == -ENOENT) { 13677bb185edSStephen Smalley /* No match in policy, mark as unlabeled. */ 13687bb185edSStephen Smalley *sid = SECINITSID_UNLABELED; 13697bb185edSStephen Smalley rc = 0; 13707bb185edSStephen Smalley } 13718e6c9693SLucian Adrian Grijincu } 13721da177e4SLinus Torvalds free_page((unsigned long)buffer); 13731da177e4SLinus Torvalds return rc; 13741da177e4SLinus Torvalds } 13751da177e4SLinus Torvalds 13761da177e4SLinus Torvalds /* The inode's security attributes must be initialized before first use. */ 13771da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry) 13781da177e4SLinus Torvalds { 13791da177e4SLinus Torvalds struct superblock_security_struct *sbsec = NULL; 13801da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 13819287aed2SAndreas Gruenbacher u32 task_sid, sid = 0; 13829287aed2SAndreas Gruenbacher u16 sclass; 13831da177e4SLinus Torvalds struct dentry *dentry; 13841da177e4SLinus Torvalds #define INITCONTEXTLEN 255 13851da177e4SLinus Torvalds char *context = NULL; 13861da177e4SLinus Torvalds unsigned len = 0; 13871da177e4SLinus Torvalds int rc = 0; 13881da177e4SLinus Torvalds 13896f3be9f5SAndreas Gruenbacher if (isec->initialized == LABEL_INITIALIZED) 139013457d07SAndreas Gruenbacher return 0; 13911da177e4SLinus Torvalds 13929287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 13936f3be9f5SAndreas Gruenbacher if (isec->initialized == LABEL_INITIALIZED) 139423970741SEric Paris goto out_unlock; 13951da177e4SLinus Torvalds 139613457d07SAndreas Gruenbacher if (isec->sclass == SECCLASS_FILE) 139713457d07SAndreas Gruenbacher isec->sclass = inode_mode_to_security_class(inode->i_mode); 139813457d07SAndreas Gruenbacher 13991da177e4SLinus Torvalds sbsec = inode->i_sb->s_security; 14000d90a7ecSDavid P. Quigley if (!(sbsec->flags & SE_SBINITIALIZED)) { 14011da177e4SLinus Torvalds /* Defer initialization until selinux_complete_init, 14021da177e4SLinus Torvalds after the initial policy is loaded and the security 14031da177e4SLinus Torvalds server is ready to handle calls. */ 14041da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 14051da177e4SLinus Torvalds if (list_empty(&isec->list)) 14061da177e4SLinus Torvalds list_add(&isec->list, &sbsec->isec_head); 14071da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 140823970741SEric Paris goto out_unlock; 14091da177e4SLinus Torvalds } 14101da177e4SLinus Torvalds 14119287aed2SAndreas Gruenbacher sclass = isec->sclass; 14129287aed2SAndreas Gruenbacher task_sid = isec->task_sid; 14139287aed2SAndreas Gruenbacher sid = isec->sid; 14149287aed2SAndreas Gruenbacher isec->initialized = LABEL_PENDING; 14159287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 14169287aed2SAndreas Gruenbacher 14171da177e4SLinus Torvalds switch (sbsec->behavior) { 1418eb9ae686SDavid Quigley case SECURITY_FS_USE_NATIVE: 1419eb9ae686SDavid Quigley break; 14201da177e4SLinus Torvalds case SECURITY_FS_USE_XATTR: 14215d6c3191SAndreas Gruenbacher if (!(inode->i_opflags & IOP_XATTR)) { 14229287aed2SAndreas Gruenbacher sid = sbsec->def_sid; 14231da177e4SLinus Torvalds break; 14241da177e4SLinus Torvalds } 14251da177e4SLinus Torvalds /* Need a dentry, since the xattr API requires one. 14261da177e4SLinus Torvalds Life would be simpler if we could just pass the inode. */ 14271da177e4SLinus Torvalds if (opt_dentry) { 14281da177e4SLinus Torvalds /* Called from d_instantiate or d_splice_alias. */ 14291da177e4SLinus Torvalds dentry = dget(opt_dentry); 14301da177e4SLinus Torvalds } else { 1431b127125dSAl Viro /* 1432b127125dSAl Viro * Called from selinux_complete_init, try to find a dentry. 1433b127125dSAl Viro * Some filesystems really want a connected one, so try 1434b127125dSAl Viro * that first. We could split SECURITY_FS_USE_XATTR in 1435b127125dSAl Viro * two, depending upon that... 1436b127125dSAl Viro */ 14371da177e4SLinus Torvalds dentry = d_find_alias(inode); 1438b127125dSAl Viro if (!dentry) 1439b127125dSAl Viro dentry = d_find_any_alias(inode); 14401da177e4SLinus Torvalds } 14411da177e4SLinus Torvalds if (!dentry) { 1442df7f54c0SEric Paris /* 1443df7f54c0SEric Paris * this is can be hit on boot when a file is accessed 1444df7f54c0SEric Paris * before the policy is loaded. When we load policy we 1445df7f54c0SEric Paris * may find inodes that have no dentry on the 1446df7f54c0SEric Paris * sbsec->isec_head list. No reason to complain as these 1447df7f54c0SEric Paris * will get fixed up the next time we go through 1448df7f54c0SEric Paris * inode_doinit with a dentry, before these inodes could 1449df7f54c0SEric Paris * be used again by userspace. 1450df7f54c0SEric Paris */ 14519287aed2SAndreas Gruenbacher goto out; 14521da177e4SLinus Torvalds } 14531da177e4SLinus Torvalds 14541da177e4SLinus Torvalds len = INITCONTEXTLEN; 14554cb912f1SEric Paris context = kmalloc(len+1, GFP_NOFS); 14561da177e4SLinus Torvalds if (!context) { 14571da177e4SLinus Torvalds rc = -ENOMEM; 14581da177e4SLinus Torvalds dput(dentry); 14599287aed2SAndreas Gruenbacher goto out; 14601da177e4SLinus Torvalds } 14614cb912f1SEric Paris context[len] = '\0'; 14625d6c3191SAndreas Gruenbacher rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len); 14631da177e4SLinus Torvalds if (rc == -ERANGE) { 1464314dabb8SJames Morris kfree(context); 1465314dabb8SJames Morris 14661da177e4SLinus Torvalds /* Need a larger buffer. Query for the right size. */ 14675d6c3191SAndreas Gruenbacher rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0); 14681da177e4SLinus Torvalds if (rc < 0) { 14691da177e4SLinus Torvalds dput(dentry); 14709287aed2SAndreas Gruenbacher goto out; 14711da177e4SLinus Torvalds } 14721da177e4SLinus Torvalds len = rc; 14734cb912f1SEric Paris context = kmalloc(len+1, GFP_NOFS); 14741da177e4SLinus Torvalds if (!context) { 14751da177e4SLinus Torvalds rc = -ENOMEM; 14761da177e4SLinus Torvalds dput(dentry); 14779287aed2SAndreas Gruenbacher goto out; 14781da177e4SLinus Torvalds } 14794cb912f1SEric Paris context[len] = '\0'; 14805d6c3191SAndreas Gruenbacher rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len); 14811da177e4SLinus Torvalds } 14821da177e4SLinus Torvalds dput(dentry); 14831da177e4SLinus Torvalds if (rc < 0) { 14841da177e4SLinus Torvalds if (rc != -ENODATA) { 1485c103a91eSpeter enderborg pr_warn("SELinux: %s: getxattr returned " 1486dd6f953aSHarvey Harrison "%d for dev=%s ino=%ld\n", __func__, 14871da177e4SLinus Torvalds -rc, inode->i_sb->s_id, inode->i_ino); 14881da177e4SLinus Torvalds kfree(context); 14899287aed2SAndreas Gruenbacher goto out; 14901da177e4SLinus Torvalds } 14911da177e4SLinus Torvalds /* Map ENODATA to the default file SID */ 14921da177e4SLinus Torvalds sid = sbsec->def_sid; 14931da177e4SLinus Torvalds rc = 0; 14941da177e4SLinus Torvalds } else { 1495aa8e712cSStephen Smalley rc = security_context_to_sid_default(&selinux_state, 1496aa8e712cSStephen Smalley context, rc, &sid, 1497869ab514SStephen Smalley sbsec->def_sid, 1498869ab514SStephen Smalley GFP_NOFS); 14991da177e4SLinus Torvalds if (rc) { 15004ba0a8adSEric Paris char *dev = inode->i_sb->s_id; 15014ba0a8adSEric Paris unsigned long ino = inode->i_ino; 15024ba0a8adSEric Paris 15034ba0a8adSEric Paris if (rc == -EINVAL) { 15044ba0a8adSEric Paris if (printk_ratelimit()) 1505c103a91eSpeter enderborg pr_notice("SELinux: inode=%lu on dev=%s was found to have an invalid " 15064ba0a8adSEric Paris "context=%s. This indicates you may need to relabel the inode or the " 15074ba0a8adSEric Paris "filesystem in question.\n", ino, dev, context); 15084ba0a8adSEric Paris } else { 1509c103a91eSpeter enderborg pr_warn("SELinux: %s: context_to_sid(%s) " 15101da177e4SLinus Torvalds "returned %d for dev=%s ino=%ld\n", 15114ba0a8adSEric Paris __func__, context, -rc, dev, ino); 15124ba0a8adSEric Paris } 15131da177e4SLinus Torvalds kfree(context); 15141da177e4SLinus Torvalds /* Leave with the unlabeled SID */ 15151da177e4SLinus Torvalds rc = 0; 15161da177e4SLinus Torvalds break; 15171da177e4SLinus Torvalds } 15181da177e4SLinus Torvalds } 15191da177e4SLinus Torvalds kfree(context); 15201da177e4SLinus Torvalds break; 15211da177e4SLinus Torvalds case SECURITY_FS_USE_TASK: 15229287aed2SAndreas Gruenbacher sid = task_sid; 15231da177e4SLinus Torvalds break; 15241da177e4SLinus Torvalds case SECURITY_FS_USE_TRANS: 15251da177e4SLinus Torvalds /* Default to the fs SID. */ 15269287aed2SAndreas Gruenbacher sid = sbsec->sid; 15271da177e4SLinus Torvalds 15281da177e4SLinus Torvalds /* Try to obtain a transition SID. */ 1529aa8e712cSStephen Smalley rc = security_transition_sid(&selinux_state, task_sid, sid, 1530aa8e712cSStephen Smalley sclass, NULL, &sid); 15311da177e4SLinus Torvalds if (rc) 15329287aed2SAndreas Gruenbacher goto out; 15331da177e4SLinus Torvalds break; 1534c312feb2SEric Paris case SECURITY_FS_USE_MNTPOINT: 15359287aed2SAndreas Gruenbacher sid = sbsec->mntpoint_sid; 1536c312feb2SEric Paris break; 15371da177e4SLinus Torvalds default: 1538c312feb2SEric Paris /* Default to the fs superblock SID. */ 15399287aed2SAndreas Gruenbacher sid = sbsec->sid; 15401da177e4SLinus Torvalds 1541134509d5SStephen Smalley if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) { 1542f64410ecSPaul Moore /* We must have a dentry to determine the label on 1543f64410ecSPaul Moore * procfs inodes */ 1544b127125dSAl Viro if (opt_dentry) { 1545f64410ecSPaul Moore /* Called from d_instantiate or 1546f64410ecSPaul Moore * d_splice_alias. */ 1547f64410ecSPaul Moore dentry = dget(opt_dentry); 1548b127125dSAl Viro } else { 1549f64410ecSPaul Moore /* Called from selinux_complete_init, try to 1550b127125dSAl Viro * find a dentry. Some filesystems really want 1551b127125dSAl Viro * a connected one, so try that first. 1552b127125dSAl Viro */ 1553f64410ecSPaul Moore dentry = d_find_alias(inode); 1554b127125dSAl Viro if (!dentry) 1555b127125dSAl Viro dentry = d_find_any_alias(inode); 1556b127125dSAl Viro } 1557f64410ecSPaul Moore /* 1558f64410ecSPaul Moore * This can be hit on boot when a file is accessed 1559f64410ecSPaul Moore * before the policy is loaded. When we load policy we 1560f64410ecSPaul Moore * may find inodes that have no dentry on the 1561f64410ecSPaul Moore * sbsec->isec_head list. No reason to complain as 1562f64410ecSPaul Moore * these will get fixed up the next time we go through 1563f64410ecSPaul Moore * inode_doinit() with a dentry, before these inodes 1564f64410ecSPaul Moore * could be used again by userspace. 1565f64410ecSPaul Moore */ 1566f64410ecSPaul Moore if (!dentry) 15679287aed2SAndreas Gruenbacher goto out; 15689287aed2SAndreas Gruenbacher rc = selinux_genfs_get_sid(dentry, sclass, 1569134509d5SStephen Smalley sbsec->flags, &sid); 1570f64410ecSPaul Moore dput(dentry); 15711da177e4SLinus Torvalds if (rc) 15729287aed2SAndreas Gruenbacher goto out; 15731da177e4SLinus Torvalds } 15741da177e4SLinus Torvalds break; 15751da177e4SLinus Torvalds } 15761da177e4SLinus Torvalds 15779287aed2SAndreas Gruenbacher out: 15789287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 15799287aed2SAndreas Gruenbacher if (isec->initialized == LABEL_PENDING) { 15809287aed2SAndreas Gruenbacher if (!sid || rc) { 15819287aed2SAndreas Gruenbacher isec->initialized = LABEL_INVALID; 15829287aed2SAndreas Gruenbacher goto out_unlock; 15839287aed2SAndreas Gruenbacher } 15849287aed2SAndreas Gruenbacher 15856f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 15869287aed2SAndreas Gruenbacher isec->sid = sid; 15879287aed2SAndreas Gruenbacher } 15881da177e4SLinus Torvalds 158923970741SEric Paris out_unlock: 15909287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 15911da177e4SLinus Torvalds return rc; 15921da177e4SLinus Torvalds } 15931da177e4SLinus Torvalds 15941da177e4SLinus Torvalds /* Convert a Linux signal to an access vector. */ 15951da177e4SLinus Torvalds static inline u32 signal_to_av(int sig) 15961da177e4SLinus Torvalds { 15971da177e4SLinus Torvalds u32 perm = 0; 15981da177e4SLinus Torvalds 15991da177e4SLinus Torvalds switch (sig) { 16001da177e4SLinus Torvalds case SIGCHLD: 16011da177e4SLinus Torvalds /* Commonly granted from child to parent. */ 16021da177e4SLinus Torvalds perm = PROCESS__SIGCHLD; 16031da177e4SLinus Torvalds break; 16041da177e4SLinus Torvalds case SIGKILL: 16051da177e4SLinus Torvalds /* Cannot be caught or ignored */ 16061da177e4SLinus Torvalds perm = PROCESS__SIGKILL; 16071da177e4SLinus Torvalds break; 16081da177e4SLinus Torvalds case SIGSTOP: 16091da177e4SLinus Torvalds /* Cannot be caught or ignored */ 16101da177e4SLinus Torvalds perm = PROCESS__SIGSTOP; 16111da177e4SLinus Torvalds break; 16121da177e4SLinus Torvalds default: 16131da177e4SLinus Torvalds /* All other signals. */ 16141da177e4SLinus Torvalds perm = PROCESS__SIGNAL; 16151da177e4SLinus Torvalds break; 16161da177e4SLinus Torvalds } 16171da177e4SLinus Torvalds 16181da177e4SLinus Torvalds return perm; 16191da177e4SLinus Torvalds } 16201da177e4SLinus Torvalds 1621b68e418cSStephen Smalley #if CAP_LAST_CAP > 63 1622b68e418cSStephen Smalley #error Fix SELinux to handle capabilities > 63. 1623b68e418cSStephen Smalley #endif 1624b68e418cSStephen Smalley 16251da177e4SLinus Torvalds /* Check whether a task is allowed to use a capability. */ 16266a9de491SEric Paris static int cred_has_capability(const struct cred *cred, 16278e4ff6f2SStephen Smalley int cap, int audit, bool initns) 16281da177e4SLinus Torvalds { 16292bf49690SThomas Liu struct common_audit_data ad; 163006112163SEric Paris struct av_decision avd; 1631b68e418cSStephen Smalley u16 sclass; 16323699c53cSDavid Howells u32 sid = cred_sid(cred); 1633b68e418cSStephen Smalley u32 av = CAP_TO_MASK(cap); 163406112163SEric Paris int rc; 16351da177e4SLinus Torvalds 163650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_CAP; 16371da177e4SLinus Torvalds ad.u.cap = cap; 16381da177e4SLinus Torvalds 1639b68e418cSStephen Smalley switch (CAP_TO_INDEX(cap)) { 1640b68e418cSStephen Smalley case 0: 16418e4ff6f2SStephen Smalley sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS; 1642b68e418cSStephen Smalley break; 1643b68e418cSStephen Smalley case 1: 16448e4ff6f2SStephen Smalley sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS; 1645b68e418cSStephen Smalley break; 1646b68e418cSStephen Smalley default: 1647c103a91eSpeter enderborg pr_err("SELinux: out of range capability %d\n", cap); 1648b68e418cSStephen Smalley BUG(); 1649a35c6c83SEric Paris return -EINVAL; 1650b68e418cSStephen Smalley } 165106112163SEric Paris 16526b6bc620SStephen Smalley rc = avc_has_perm_noaudit(&selinux_state, 16536b6bc620SStephen Smalley sid, sid, sclass, av, 0, &avd); 16549ade0cf4SEric Paris if (audit == SECURITY_CAP_AUDIT) { 16556b6bc620SStephen Smalley int rc2 = avc_audit(&selinux_state, 16566b6bc620SStephen Smalley sid, sid, sclass, av, &avd, rc, &ad, 0); 16579ade0cf4SEric Paris if (rc2) 16589ade0cf4SEric Paris return rc2; 16599ade0cf4SEric Paris } 166006112163SEric Paris return rc; 16611da177e4SLinus Torvalds } 16621da177e4SLinus Torvalds 16631da177e4SLinus Torvalds /* Check whether a task has a particular permission to an inode. 16641da177e4SLinus Torvalds The 'adp' parameter is optional and allows other audit 16651da177e4SLinus Torvalds data to be passed (e.g. the dentry). */ 166688e67f3bSDavid Howells static int inode_has_perm(const struct cred *cred, 16671da177e4SLinus Torvalds struct inode *inode, 16681da177e4SLinus Torvalds u32 perms, 166919e49834SLinus Torvalds struct common_audit_data *adp) 16701da177e4SLinus Torvalds { 16711da177e4SLinus Torvalds struct inode_security_struct *isec; 1672275bb41eSDavid Howells u32 sid; 16731da177e4SLinus Torvalds 1674e0e81739SDavid Howells validate_creds(cred); 1675e0e81739SDavid Howells 1676bbaca6c2SStephen Smalley if (unlikely(IS_PRIVATE(inode))) 1677bbaca6c2SStephen Smalley return 0; 1678bbaca6c2SStephen Smalley 167988e67f3bSDavid Howells sid = cred_sid(cred); 16801da177e4SLinus Torvalds isec = inode->i_security; 16811da177e4SLinus Torvalds 16826b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 16836b6bc620SStephen Smalley sid, isec->sid, isec->sclass, perms, adp); 16841da177e4SLinus Torvalds } 16851da177e4SLinus Torvalds 16861da177e4SLinus Torvalds /* Same as inode_has_perm, but pass explicit audit data containing 16871da177e4SLinus Torvalds the dentry to help the auditing code to more easily generate the 16881da177e4SLinus Torvalds pathname if needed. */ 168988e67f3bSDavid Howells static inline int dentry_has_perm(const struct cred *cred, 16901da177e4SLinus Torvalds struct dentry *dentry, 16911da177e4SLinus Torvalds u32 av) 16921da177e4SLinus Torvalds { 1693c6f493d6SDavid Howells struct inode *inode = d_backing_inode(dentry); 16942bf49690SThomas Liu struct common_audit_data ad; 169588e67f3bSDavid Howells 169650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 16972875fa00SEric Paris ad.u.dentry = dentry; 16985d226df4SAndreas Gruenbacher __inode_security_revalidate(inode, dentry, true); 169919e49834SLinus Torvalds return inode_has_perm(cred, inode, av, &ad); 17002875fa00SEric Paris } 17012875fa00SEric Paris 17022875fa00SEric Paris /* Same as inode_has_perm, but pass explicit audit data containing 17032875fa00SEric Paris the path to help the auditing code to more easily generate the 17042875fa00SEric Paris pathname if needed. */ 17052875fa00SEric Paris static inline int path_has_perm(const struct cred *cred, 17063f7036a0SAl Viro const struct path *path, 17072875fa00SEric Paris u32 av) 17082875fa00SEric Paris { 1709c6f493d6SDavid Howells struct inode *inode = d_backing_inode(path->dentry); 17102875fa00SEric Paris struct common_audit_data ad; 17112875fa00SEric Paris 171250c205f5SEric Paris ad.type = LSM_AUDIT_DATA_PATH; 17132875fa00SEric Paris ad.u.path = *path; 17145d226df4SAndreas Gruenbacher __inode_security_revalidate(inode, path->dentry, true); 171519e49834SLinus Torvalds return inode_has_perm(cred, inode, av, &ad); 17161da177e4SLinus Torvalds } 17171da177e4SLinus Torvalds 171813f8e981SDavid Howells /* Same as path_has_perm, but uses the inode from the file struct. */ 171913f8e981SDavid Howells static inline int file_path_has_perm(const struct cred *cred, 172013f8e981SDavid Howells struct file *file, 172113f8e981SDavid Howells u32 av) 172213f8e981SDavid Howells { 172313f8e981SDavid Howells struct common_audit_data ad; 172413f8e981SDavid Howells 172543af5de7SVivek Goyal ad.type = LSM_AUDIT_DATA_FILE; 172643af5de7SVivek Goyal ad.u.file = file; 172719e49834SLinus Torvalds return inode_has_perm(cred, file_inode(file), av, &ad); 172813f8e981SDavid Howells } 172913f8e981SDavid Howells 1730f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL 1731f66e448cSChenbo Feng static int bpf_fd_pass(struct file *file, u32 sid); 1732f66e448cSChenbo Feng #endif 1733f66e448cSChenbo Feng 17341da177e4SLinus Torvalds /* Check whether a task can use an open file descriptor to 17351da177e4SLinus Torvalds access an inode in a given way. Check access to the 17361da177e4SLinus Torvalds descriptor itself, and then use dentry_has_perm to 17371da177e4SLinus Torvalds check a particular permission to the file. 17381da177e4SLinus Torvalds Access to the descriptor is implicitly granted if it 17391da177e4SLinus Torvalds has the same SID as the process. If av is zero, then 17401da177e4SLinus Torvalds access to the file is not checked, e.g. for cases 17411da177e4SLinus Torvalds where only the descriptor is affected like seek. */ 174288e67f3bSDavid Howells static int file_has_perm(const struct cred *cred, 17431da177e4SLinus Torvalds struct file *file, 17441da177e4SLinus Torvalds u32 av) 17451da177e4SLinus Torvalds { 17461da177e4SLinus Torvalds struct file_security_struct *fsec = file->f_security; 1747496ad9aaSAl Viro struct inode *inode = file_inode(file); 17482bf49690SThomas Liu struct common_audit_data ad; 174988e67f3bSDavid Howells u32 sid = cred_sid(cred); 17501da177e4SLinus Torvalds int rc; 17511da177e4SLinus Torvalds 175243af5de7SVivek Goyal ad.type = LSM_AUDIT_DATA_FILE; 175343af5de7SVivek Goyal ad.u.file = file; 17541da177e4SLinus Torvalds 1755275bb41eSDavid Howells if (sid != fsec->sid) { 17566b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 17576b6bc620SStephen Smalley sid, fsec->sid, 17581da177e4SLinus Torvalds SECCLASS_FD, 17591da177e4SLinus Torvalds FD__USE, 17601da177e4SLinus Torvalds &ad); 17611da177e4SLinus Torvalds if (rc) 176288e67f3bSDavid Howells goto out; 17631da177e4SLinus Torvalds } 17641da177e4SLinus Torvalds 1765f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL 1766f66e448cSChenbo Feng rc = bpf_fd_pass(file, cred_sid(cred)); 1767f66e448cSChenbo Feng if (rc) 1768f66e448cSChenbo Feng return rc; 1769f66e448cSChenbo Feng #endif 1770f66e448cSChenbo Feng 17711da177e4SLinus Torvalds /* av is zero if only checking access to the descriptor. */ 177288e67f3bSDavid Howells rc = 0; 17731da177e4SLinus Torvalds if (av) 177419e49834SLinus Torvalds rc = inode_has_perm(cred, inode, av, &ad); 17751da177e4SLinus Torvalds 177688e67f3bSDavid Howells out: 177788e67f3bSDavid Howells return rc; 17781da177e4SLinus Torvalds } 17791da177e4SLinus Torvalds 1780c3c188b2SDavid Howells /* 1781c3c188b2SDavid Howells * Determine the label for an inode that might be unioned. 1782c3c188b2SDavid Howells */ 1783c957f6dfSVivek Goyal static int 1784c957f6dfSVivek Goyal selinux_determine_inode_label(const struct task_security_struct *tsec, 1785c957f6dfSVivek Goyal struct inode *dir, 1786c957f6dfSVivek Goyal const struct qstr *name, u16 tclass, 1787c3c188b2SDavid Howells u32 *_new_isid) 1788c3c188b2SDavid Howells { 1789c3c188b2SDavid Howells const struct superblock_security_struct *sbsec = dir->i_sb->s_security; 1790c3c188b2SDavid Howells 1791c3c188b2SDavid Howells if ((sbsec->flags & SE_SBINITIALIZED) && 1792c3c188b2SDavid Howells (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) { 1793c3c188b2SDavid Howells *_new_isid = sbsec->mntpoint_sid; 1794c3c188b2SDavid Howells } else if ((sbsec->flags & SBLABEL_MNT) && 1795c3c188b2SDavid Howells tsec->create_sid) { 1796c3c188b2SDavid Howells *_new_isid = tsec->create_sid; 1797c3c188b2SDavid Howells } else { 179820cdef8dSPaul Moore const struct inode_security_struct *dsec = inode_security(dir); 1799aa8e712cSStephen Smalley return security_transition_sid(&selinux_state, tsec->sid, 1800aa8e712cSStephen Smalley dsec->sid, tclass, 1801c3c188b2SDavid Howells name, _new_isid); 1802c3c188b2SDavid Howells } 1803c3c188b2SDavid Howells 1804c3c188b2SDavid Howells return 0; 1805c3c188b2SDavid Howells } 1806c3c188b2SDavid Howells 18071da177e4SLinus Torvalds /* Check whether a task can create a file. */ 18081da177e4SLinus Torvalds static int may_create(struct inode *dir, 18091da177e4SLinus Torvalds struct dentry *dentry, 18101da177e4SLinus Torvalds u16 tclass) 18111da177e4SLinus Torvalds { 18125fb49870SPaul Moore const struct task_security_struct *tsec = current_security(); 18131da177e4SLinus Torvalds struct inode_security_struct *dsec; 18141da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 1815275bb41eSDavid Howells u32 sid, newsid; 18162bf49690SThomas Liu struct common_audit_data ad; 18171da177e4SLinus Torvalds int rc; 18181da177e4SLinus Torvalds 181983da53c5SAndreas Gruenbacher dsec = inode_security(dir); 18201da177e4SLinus Torvalds sbsec = dir->i_sb->s_security; 18211da177e4SLinus Torvalds 1822275bb41eSDavid Howells sid = tsec->sid; 1823275bb41eSDavid Howells 182450c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 1825a269434dSEric Paris ad.u.dentry = dentry; 18261da177e4SLinus Torvalds 18276b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 18286b6bc620SStephen Smalley sid, dsec->sid, SECCLASS_DIR, 18291da177e4SLinus Torvalds DIR__ADD_NAME | DIR__SEARCH, 18301da177e4SLinus Torvalds &ad); 18311da177e4SLinus Torvalds if (rc) 18321da177e4SLinus Torvalds return rc; 18331da177e4SLinus Torvalds 1834c957f6dfSVivek Goyal rc = selinux_determine_inode_label(current_security(), dir, 1835c957f6dfSVivek Goyal &dentry->d_name, tclass, &newsid); 18361da177e4SLinus Torvalds if (rc) 18371da177e4SLinus Torvalds return rc; 18381da177e4SLinus Torvalds 18396b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 18406b6bc620SStephen Smalley sid, newsid, tclass, FILE__CREATE, &ad); 18411da177e4SLinus Torvalds if (rc) 18421da177e4SLinus Torvalds return rc; 18431da177e4SLinus Torvalds 18446b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 18456b6bc620SStephen Smalley newsid, sbsec->sid, 18461da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 18471da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, &ad); 18481da177e4SLinus Torvalds } 18491da177e4SLinus Torvalds 18501da177e4SLinus Torvalds #define MAY_LINK 0 18511da177e4SLinus Torvalds #define MAY_UNLINK 1 18521da177e4SLinus Torvalds #define MAY_RMDIR 2 18531da177e4SLinus Torvalds 18541da177e4SLinus Torvalds /* Check whether a task can link, unlink, or rmdir a file/directory. */ 18551da177e4SLinus Torvalds static int may_link(struct inode *dir, 18561da177e4SLinus Torvalds struct dentry *dentry, 18571da177e4SLinus Torvalds int kind) 18581da177e4SLinus Torvalds 18591da177e4SLinus Torvalds { 18601da177e4SLinus Torvalds struct inode_security_struct *dsec, *isec; 18612bf49690SThomas Liu struct common_audit_data ad; 1862275bb41eSDavid Howells u32 sid = current_sid(); 18631da177e4SLinus Torvalds u32 av; 18641da177e4SLinus Torvalds int rc; 18651da177e4SLinus Torvalds 186683da53c5SAndreas Gruenbacher dsec = inode_security(dir); 186783da53c5SAndreas Gruenbacher isec = backing_inode_security(dentry); 18681da177e4SLinus Torvalds 186950c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 1870a269434dSEric Paris ad.u.dentry = dentry; 18711da177e4SLinus Torvalds 18721da177e4SLinus Torvalds av = DIR__SEARCH; 18731da177e4SLinus Torvalds av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME); 18746b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 18756b6bc620SStephen Smalley sid, dsec->sid, SECCLASS_DIR, av, &ad); 18761da177e4SLinus Torvalds if (rc) 18771da177e4SLinus Torvalds return rc; 18781da177e4SLinus Torvalds 18791da177e4SLinus Torvalds switch (kind) { 18801da177e4SLinus Torvalds case MAY_LINK: 18811da177e4SLinus Torvalds av = FILE__LINK; 18821da177e4SLinus Torvalds break; 18831da177e4SLinus Torvalds case MAY_UNLINK: 18841da177e4SLinus Torvalds av = FILE__UNLINK; 18851da177e4SLinus Torvalds break; 18861da177e4SLinus Torvalds case MAY_RMDIR: 18871da177e4SLinus Torvalds av = DIR__RMDIR; 18881da177e4SLinus Torvalds break; 18891da177e4SLinus Torvalds default: 1890c103a91eSpeter enderborg pr_warn("SELinux: %s: unrecognized kind %d\n", 1891744ba35eSEric Paris __func__, kind); 18921da177e4SLinus Torvalds return 0; 18931da177e4SLinus Torvalds } 18941da177e4SLinus Torvalds 18956b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 18966b6bc620SStephen Smalley sid, isec->sid, isec->sclass, av, &ad); 18971da177e4SLinus Torvalds return rc; 18981da177e4SLinus Torvalds } 18991da177e4SLinus Torvalds 19001da177e4SLinus Torvalds static inline int may_rename(struct inode *old_dir, 19011da177e4SLinus Torvalds struct dentry *old_dentry, 19021da177e4SLinus Torvalds struct inode *new_dir, 19031da177e4SLinus Torvalds struct dentry *new_dentry) 19041da177e4SLinus Torvalds { 19051da177e4SLinus Torvalds struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec; 19062bf49690SThomas Liu struct common_audit_data ad; 1907275bb41eSDavid Howells u32 sid = current_sid(); 19081da177e4SLinus Torvalds u32 av; 19091da177e4SLinus Torvalds int old_is_dir, new_is_dir; 19101da177e4SLinus Torvalds int rc; 19111da177e4SLinus Torvalds 191283da53c5SAndreas Gruenbacher old_dsec = inode_security(old_dir); 191383da53c5SAndreas Gruenbacher old_isec = backing_inode_security(old_dentry); 1914e36cb0b8SDavid Howells old_is_dir = d_is_dir(old_dentry); 191583da53c5SAndreas Gruenbacher new_dsec = inode_security(new_dir); 19161da177e4SLinus Torvalds 191750c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 19181da177e4SLinus Torvalds 1919a269434dSEric Paris ad.u.dentry = old_dentry; 19206b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 19216b6bc620SStephen Smalley sid, old_dsec->sid, SECCLASS_DIR, 19221da177e4SLinus Torvalds DIR__REMOVE_NAME | DIR__SEARCH, &ad); 19231da177e4SLinus Torvalds if (rc) 19241da177e4SLinus Torvalds return rc; 19256b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 19266b6bc620SStephen Smalley sid, old_isec->sid, 19271da177e4SLinus Torvalds old_isec->sclass, FILE__RENAME, &ad); 19281da177e4SLinus Torvalds if (rc) 19291da177e4SLinus Torvalds return rc; 19301da177e4SLinus Torvalds if (old_is_dir && new_dir != old_dir) { 19316b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 19326b6bc620SStephen Smalley sid, old_isec->sid, 19331da177e4SLinus Torvalds old_isec->sclass, DIR__REPARENT, &ad); 19341da177e4SLinus Torvalds if (rc) 19351da177e4SLinus Torvalds return rc; 19361da177e4SLinus Torvalds } 19371da177e4SLinus Torvalds 1938a269434dSEric Paris ad.u.dentry = new_dentry; 19391da177e4SLinus Torvalds av = DIR__ADD_NAME | DIR__SEARCH; 19402c616d4dSDavid Howells if (d_is_positive(new_dentry)) 19411da177e4SLinus Torvalds av |= DIR__REMOVE_NAME; 19426b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 19436b6bc620SStephen Smalley sid, new_dsec->sid, SECCLASS_DIR, av, &ad); 19441da177e4SLinus Torvalds if (rc) 19451da177e4SLinus Torvalds return rc; 19462c616d4dSDavid Howells if (d_is_positive(new_dentry)) { 194783da53c5SAndreas Gruenbacher new_isec = backing_inode_security(new_dentry); 1948e36cb0b8SDavid Howells new_is_dir = d_is_dir(new_dentry); 19496b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 19506b6bc620SStephen Smalley sid, new_isec->sid, 19511da177e4SLinus Torvalds new_isec->sclass, 19521da177e4SLinus Torvalds (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad); 19531da177e4SLinus Torvalds if (rc) 19541da177e4SLinus Torvalds return rc; 19551da177e4SLinus Torvalds } 19561da177e4SLinus Torvalds 19571da177e4SLinus Torvalds return 0; 19581da177e4SLinus Torvalds } 19591da177e4SLinus Torvalds 19601da177e4SLinus Torvalds /* Check whether a task can perform a filesystem operation. */ 196188e67f3bSDavid Howells static int superblock_has_perm(const struct cred *cred, 19621da177e4SLinus Torvalds struct super_block *sb, 19631da177e4SLinus Torvalds u32 perms, 19642bf49690SThomas Liu struct common_audit_data *ad) 19651da177e4SLinus Torvalds { 19661da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 196788e67f3bSDavid Howells u32 sid = cred_sid(cred); 19681da177e4SLinus Torvalds 19691da177e4SLinus Torvalds sbsec = sb->s_security; 19706b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 19716b6bc620SStephen Smalley sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad); 19721da177e4SLinus Torvalds } 19731da177e4SLinus Torvalds 19741da177e4SLinus Torvalds /* Convert a Linux mode and permission mask to an access vector. */ 19751da177e4SLinus Torvalds static inline u32 file_mask_to_av(int mode, int mask) 19761da177e4SLinus Torvalds { 19771da177e4SLinus Torvalds u32 av = 0; 19781da177e4SLinus Torvalds 1979dba19c60SAl Viro if (!S_ISDIR(mode)) { 19801da177e4SLinus Torvalds if (mask & MAY_EXEC) 19811da177e4SLinus Torvalds av |= FILE__EXECUTE; 19821da177e4SLinus Torvalds if (mask & MAY_READ) 19831da177e4SLinus Torvalds av |= FILE__READ; 19841da177e4SLinus Torvalds 19851da177e4SLinus Torvalds if (mask & MAY_APPEND) 19861da177e4SLinus Torvalds av |= FILE__APPEND; 19871da177e4SLinus Torvalds else if (mask & MAY_WRITE) 19881da177e4SLinus Torvalds av |= FILE__WRITE; 19891da177e4SLinus Torvalds 19901da177e4SLinus Torvalds } else { 19911da177e4SLinus Torvalds if (mask & MAY_EXEC) 19921da177e4SLinus Torvalds av |= DIR__SEARCH; 19931da177e4SLinus Torvalds if (mask & MAY_WRITE) 19941da177e4SLinus Torvalds av |= DIR__WRITE; 19951da177e4SLinus Torvalds if (mask & MAY_READ) 19961da177e4SLinus Torvalds av |= DIR__READ; 19971da177e4SLinus Torvalds } 19981da177e4SLinus Torvalds 19991da177e4SLinus Torvalds return av; 20001da177e4SLinus Torvalds } 20011da177e4SLinus Torvalds 20021da177e4SLinus Torvalds /* Convert a Linux file to an access vector. */ 20031da177e4SLinus Torvalds static inline u32 file_to_av(struct file *file) 20041da177e4SLinus Torvalds { 20051da177e4SLinus Torvalds u32 av = 0; 20061da177e4SLinus Torvalds 20071da177e4SLinus Torvalds if (file->f_mode & FMODE_READ) 20081da177e4SLinus Torvalds av |= FILE__READ; 20091da177e4SLinus Torvalds if (file->f_mode & FMODE_WRITE) { 20101da177e4SLinus Torvalds if (file->f_flags & O_APPEND) 20111da177e4SLinus Torvalds av |= FILE__APPEND; 20121da177e4SLinus Torvalds else 20131da177e4SLinus Torvalds av |= FILE__WRITE; 20141da177e4SLinus Torvalds } 20150794c66dSStephen Smalley if (!av) { 20160794c66dSStephen Smalley /* 20170794c66dSStephen Smalley * Special file opened with flags 3 for ioctl-only use. 20180794c66dSStephen Smalley */ 20190794c66dSStephen Smalley av = FILE__IOCTL; 20200794c66dSStephen Smalley } 20211da177e4SLinus Torvalds 20221da177e4SLinus Torvalds return av; 20231da177e4SLinus Torvalds } 20241da177e4SLinus Torvalds 20258b6a5a37SEric Paris /* 20268b6a5a37SEric Paris * Convert a file to an access vector and include the correct open 20278b6a5a37SEric Paris * open permission. 20288b6a5a37SEric Paris */ 20298b6a5a37SEric Paris static inline u32 open_file_to_av(struct file *file) 20308b6a5a37SEric Paris { 20318b6a5a37SEric Paris u32 av = file_to_av(file); 2032ccb54478SStephen Smalley struct inode *inode = file_inode(file); 20338b6a5a37SEric Paris 2034aa8e712cSStephen Smalley if (selinux_policycap_openperm() && 2035aa8e712cSStephen Smalley inode->i_sb->s_magic != SOCKFS_MAGIC) 20368b6a5a37SEric Paris av |= FILE__OPEN; 203749b7b8deSEric Paris 20388b6a5a37SEric Paris return av; 20398b6a5a37SEric Paris } 20408b6a5a37SEric Paris 20411da177e4SLinus Torvalds /* Hook functions begin here. */ 20421da177e4SLinus Torvalds 204379af7307SStephen Smalley static int selinux_binder_set_context_mgr(struct task_struct *mgr) 204479af7307SStephen Smalley { 204579af7307SStephen Smalley u32 mysid = current_sid(); 204679af7307SStephen Smalley u32 mgrsid = task_sid(mgr); 204779af7307SStephen Smalley 20486b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 20496b6bc620SStephen Smalley mysid, mgrsid, SECCLASS_BINDER, 205079af7307SStephen Smalley BINDER__SET_CONTEXT_MGR, NULL); 205179af7307SStephen Smalley } 205279af7307SStephen Smalley 205379af7307SStephen Smalley static int selinux_binder_transaction(struct task_struct *from, 205479af7307SStephen Smalley struct task_struct *to) 205579af7307SStephen Smalley { 205679af7307SStephen Smalley u32 mysid = current_sid(); 205779af7307SStephen Smalley u32 fromsid = task_sid(from); 205879af7307SStephen Smalley u32 tosid = task_sid(to); 205979af7307SStephen Smalley int rc; 206079af7307SStephen Smalley 206179af7307SStephen Smalley if (mysid != fromsid) { 20626b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 20636b6bc620SStephen Smalley mysid, fromsid, SECCLASS_BINDER, 206479af7307SStephen Smalley BINDER__IMPERSONATE, NULL); 206579af7307SStephen Smalley if (rc) 206679af7307SStephen Smalley return rc; 206779af7307SStephen Smalley } 206879af7307SStephen Smalley 20696b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 20706b6bc620SStephen Smalley fromsid, tosid, SECCLASS_BINDER, BINDER__CALL, 207179af7307SStephen Smalley NULL); 207279af7307SStephen Smalley } 207379af7307SStephen Smalley 207479af7307SStephen Smalley static int selinux_binder_transfer_binder(struct task_struct *from, 207579af7307SStephen Smalley struct task_struct *to) 207679af7307SStephen Smalley { 207779af7307SStephen Smalley u32 fromsid = task_sid(from); 207879af7307SStephen Smalley u32 tosid = task_sid(to); 207979af7307SStephen Smalley 20806b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 20816b6bc620SStephen Smalley fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER, 208279af7307SStephen Smalley NULL); 208379af7307SStephen Smalley } 208479af7307SStephen Smalley 208579af7307SStephen Smalley static int selinux_binder_transfer_file(struct task_struct *from, 208679af7307SStephen Smalley struct task_struct *to, 208779af7307SStephen Smalley struct file *file) 208879af7307SStephen Smalley { 208979af7307SStephen Smalley u32 sid = task_sid(to); 209079af7307SStephen Smalley struct file_security_struct *fsec = file->f_security; 209183da53c5SAndreas Gruenbacher struct dentry *dentry = file->f_path.dentry; 209220cdef8dSPaul Moore struct inode_security_struct *isec; 209379af7307SStephen Smalley struct common_audit_data ad; 209479af7307SStephen Smalley int rc; 209579af7307SStephen Smalley 209679af7307SStephen Smalley ad.type = LSM_AUDIT_DATA_PATH; 209779af7307SStephen Smalley ad.u.path = file->f_path; 209879af7307SStephen Smalley 209979af7307SStephen Smalley if (sid != fsec->sid) { 21006b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 21016b6bc620SStephen Smalley sid, fsec->sid, 210279af7307SStephen Smalley SECCLASS_FD, 210379af7307SStephen Smalley FD__USE, 210479af7307SStephen Smalley &ad); 210579af7307SStephen Smalley if (rc) 210679af7307SStephen Smalley return rc; 210779af7307SStephen Smalley } 210879af7307SStephen Smalley 2109f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL 2110f66e448cSChenbo Feng rc = bpf_fd_pass(file, sid); 2111f66e448cSChenbo Feng if (rc) 2112f66e448cSChenbo Feng return rc; 2113f66e448cSChenbo Feng #endif 2114f66e448cSChenbo Feng 211583da53c5SAndreas Gruenbacher if (unlikely(IS_PRIVATE(d_backing_inode(dentry)))) 211679af7307SStephen Smalley return 0; 211779af7307SStephen Smalley 211820cdef8dSPaul Moore isec = backing_inode_security(dentry); 21196b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 21206b6bc620SStephen Smalley sid, isec->sid, isec->sclass, file_to_av(file), 212179af7307SStephen Smalley &ad); 212279af7307SStephen Smalley } 212379af7307SStephen Smalley 21249e48858fSIngo Molnar static int selinux_ptrace_access_check(struct task_struct *child, 2125006ebb40SStephen Smalley unsigned int mode) 21261da177e4SLinus Torvalds { 2127275bb41eSDavid Howells u32 sid = current_sid(); 2128275bb41eSDavid Howells u32 csid = task_sid(child); 2129006ebb40SStephen Smalley 2130be0554c9SStephen Smalley if (mode & PTRACE_MODE_READ) 21316b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 21326b6bc620SStephen Smalley sid, csid, SECCLASS_FILE, FILE__READ, NULL); 2133be0554c9SStephen Smalley 21346b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 21356b6bc620SStephen Smalley sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL); 21365cd9c58fSDavid Howells } 21375cd9c58fSDavid Howells 21385cd9c58fSDavid Howells static int selinux_ptrace_traceme(struct task_struct *parent) 21395cd9c58fSDavid Howells { 21406b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 21416b6bc620SStephen Smalley task_sid(parent), current_sid(), SECCLASS_PROCESS, 2142be0554c9SStephen Smalley PROCESS__PTRACE, NULL); 21431da177e4SLinus Torvalds } 21441da177e4SLinus Torvalds 21451da177e4SLinus Torvalds static int selinux_capget(struct task_struct *target, kernel_cap_t *effective, 21461da177e4SLinus Torvalds kernel_cap_t *inheritable, kernel_cap_t *permitted) 21471da177e4SLinus Torvalds { 21486b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 21496b6bc620SStephen Smalley current_sid(), task_sid(target), SECCLASS_PROCESS, 2150be0554c9SStephen Smalley PROCESS__GETCAP, NULL); 21511da177e4SLinus Torvalds } 21521da177e4SLinus Torvalds 2153d84f4f99SDavid Howells static int selinux_capset(struct cred *new, const struct cred *old, 2154d84f4f99SDavid Howells const kernel_cap_t *effective, 215515a2460eSDavid Howells const kernel_cap_t *inheritable, 215615a2460eSDavid Howells const kernel_cap_t *permitted) 21571da177e4SLinus Torvalds { 21586b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 21596b6bc620SStephen Smalley cred_sid(old), cred_sid(new), SECCLASS_PROCESS, 2160be0554c9SStephen Smalley PROCESS__SETCAP, NULL); 21611da177e4SLinus Torvalds } 21621da177e4SLinus Torvalds 21635626d3e8SJames Morris /* 21645626d3e8SJames Morris * (This comment used to live with the selinux_task_setuid hook, 21655626d3e8SJames Morris * which was removed). 21665626d3e8SJames Morris * 21675626d3e8SJames Morris * Since setuid only affects the current process, and since the SELinux 21685626d3e8SJames Morris * controls are not based on the Linux identity attributes, SELinux does not 21695626d3e8SJames Morris * need to control this operation. However, SELinux does control the use of 21705626d3e8SJames Morris * the CAP_SETUID and CAP_SETGID capabilities using the capable hook. 21715626d3e8SJames Morris */ 21725626d3e8SJames Morris 21736a9de491SEric Paris static int selinux_capable(const struct cred *cred, struct user_namespace *ns, 21746a9de491SEric Paris int cap, int audit) 21751da177e4SLinus Torvalds { 21768e4ff6f2SStephen Smalley return cred_has_capability(cred, cap, audit, ns == &init_user_ns); 21771da177e4SLinus Torvalds } 21781da177e4SLinus Torvalds 21791da177e4SLinus Torvalds static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb) 21801da177e4SLinus Torvalds { 218188e67f3bSDavid Howells const struct cred *cred = current_cred(); 21821da177e4SLinus Torvalds int rc = 0; 21831da177e4SLinus Torvalds 21841da177e4SLinus Torvalds if (!sb) 21851da177e4SLinus Torvalds return 0; 21861da177e4SLinus Torvalds 21871da177e4SLinus Torvalds switch (cmds) { 21881da177e4SLinus Torvalds case Q_SYNC: 21891da177e4SLinus Torvalds case Q_QUOTAON: 21901da177e4SLinus Torvalds case Q_QUOTAOFF: 21911da177e4SLinus Torvalds case Q_SETINFO: 21921da177e4SLinus Torvalds case Q_SETQUOTA: 219388e67f3bSDavid Howells rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL); 21941da177e4SLinus Torvalds break; 21951da177e4SLinus Torvalds case Q_GETFMT: 21961da177e4SLinus Torvalds case Q_GETINFO: 21971da177e4SLinus Torvalds case Q_GETQUOTA: 219888e67f3bSDavid Howells rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL); 21991da177e4SLinus Torvalds break; 22001da177e4SLinus Torvalds default: 22011da177e4SLinus Torvalds rc = 0; /* let the kernel handle invalid cmds */ 22021da177e4SLinus Torvalds break; 22031da177e4SLinus Torvalds } 22041da177e4SLinus Torvalds return rc; 22051da177e4SLinus Torvalds } 22061da177e4SLinus Torvalds 22071da177e4SLinus Torvalds static int selinux_quota_on(struct dentry *dentry) 22081da177e4SLinus Torvalds { 220988e67f3bSDavid Howells const struct cred *cred = current_cred(); 221088e67f3bSDavid Howells 22112875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__QUOTAON); 22121da177e4SLinus Torvalds } 22131da177e4SLinus Torvalds 221412b3052cSEric Paris static int selinux_syslog(int type) 22151da177e4SLinus Torvalds { 22161da177e4SLinus Torvalds switch (type) { 2217d78ca3cdSKees Cook case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */ 2218d78ca3cdSKees Cook case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */ 22196b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 22206b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, 2221be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL); 2222d78ca3cdSKees Cook case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */ 2223d78ca3cdSKees Cook case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */ 2224d78ca3cdSKees Cook /* Set level of messages printed to console */ 2225d78ca3cdSKees Cook case SYSLOG_ACTION_CONSOLE_LEVEL: 22266b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 22276b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, 2228be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE, 2229be0554c9SStephen Smalley NULL); 22301da177e4SLinus Torvalds } 2231be0554c9SStephen Smalley /* All other syslog types */ 22326b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 22336b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, 2234be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL); 22351da177e4SLinus Torvalds } 22361da177e4SLinus Torvalds 22371da177e4SLinus Torvalds /* 22381da177e4SLinus Torvalds * Check that a process has enough memory to allocate a new virtual 22391da177e4SLinus Torvalds * mapping. 0 means there is enough memory for the allocation to 22401da177e4SLinus Torvalds * succeed and -ENOMEM implies there is not. 22411da177e4SLinus Torvalds * 22421da177e4SLinus Torvalds * Do not audit the selinux permission check, as this is applied to all 22431da177e4SLinus Torvalds * processes that allocate mappings. 22441da177e4SLinus Torvalds */ 224534b4e4aaSAlan Cox static int selinux_vm_enough_memory(struct mm_struct *mm, long pages) 22461da177e4SLinus Torvalds { 22471da177e4SLinus Torvalds int rc, cap_sys_admin = 0; 22481da177e4SLinus Torvalds 2249b1d9e6b0SCasey Schaufler rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN, 22508e4ff6f2SStephen Smalley SECURITY_CAP_NOAUDIT, true); 22511da177e4SLinus Torvalds if (rc == 0) 22521da177e4SLinus Torvalds cap_sys_admin = 1; 22531da177e4SLinus Torvalds 2254b1d9e6b0SCasey Schaufler return cap_sys_admin; 22551da177e4SLinus Torvalds } 22561da177e4SLinus Torvalds 22571da177e4SLinus Torvalds /* binprm security operations */ 22581da177e4SLinus Torvalds 2259be0554c9SStephen Smalley static u32 ptrace_parent_sid(void) 22600c6181cbSPaul Moore { 22610c6181cbSPaul Moore u32 sid = 0; 22620c6181cbSPaul Moore struct task_struct *tracer; 22630c6181cbSPaul Moore 22640c6181cbSPaul Moore rcu_read_lock(); 2265be0554c9SStephen Smalley tracer = ptrace_parent(current); 22660c6181cbSPaul Moore if (tracer) 22670c6181cbSPaul Moore sid = task_sid(tracer); 22680c6181cbSPaul Moore rcu_read_unlock(); 22690c6181cbSPaul Moore 22700c6181cbSPaul Moore return sid; 22710c6181cbSPaul Moore } 22720c6181cbSPaul Moore 22737b0d0b40SStephen Smalley static int check_nnp_nosuid(const struct linux_binprm *bprm, 22747b0d0b40SStephen Smalley const struct task_security_struct *old_tsec, 22757b0d0b40SStephen Smalley const struct task_security_struct *new_tsec) 22767b0d0b40SStephen Smalley { 22777b0d0b40SStephen Smalley int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS); 2278380cf5baSAndy Lutomirski int nosuid = !mnt_may_suid(bprm->file->f_path.mnt); 22797b0d0b40SStephen Smalley int rc; 2280af63f419SStephen Smalley u32 av; 22817b0d0b40SStephen Smalley 22827b0d0b40SStephen Smalley if (!nnp && !nosuid) 22837b0d0b40SStephen Smalley return 0; /* neither NNP nor nosuid */ 22847b0d0b40SStephen Smalley 22857b0d0b40SStephen Smalley if (new_tsec->sid == old_tsec->sid) 22867b0d0b40SStephen Smalley return 0; /* No change in credentials */ 22877b0d0b40SStephen Smalley 22887b0d0b40SStephen Smalley /* 2289af63f419SStephen Smalley * If the policy enables the nnp_nosuid_transition policy capability, 2290af63f419SStephen Smalley * then we permit transitions under NNP or nosuid if the 2291af63f419SStephen Smalley * policy allows the corresponding permission between 2292af63f419SStephen Smalley * the old and new contexts. 2293af63f419SStephen Smalley */ 2294aa8e712cSStephen Smalley if (selinux_policycap_nnp_nosuid_transition()) { 2295af63f419SStephen Smalley av = 0; 2296af63f419SStephen Smalley if (nnp) 2297af63f419SStephen Smalley av |= PROCESS2__NNP_TRANSITION; 2298af63f419SStephen Smalley if (nosuid) 2299af63f419SStephen Smalley av |= PROCESS2__NOSUID_TRANSITION; 23006b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 23016b6bc620SStephen Smalley old_tsec->sid, new_tsec->sid, 2302af63f419SStephen Smalley SECCLASS_PROCESS2, av, NULL); 2303af63f419SStephen Smalley if (!rc) 2304af63f419SStephen Smalley return 0; 2305af63f419SStephen Smalley } 2306af63f419SStephen Smalley 2307af63f419SStephen Smalley /* 2308af63f419SStephen Smalley * We also permit NNP or nosuid transitions to bounded SIDs, 2309af63f419SStephen Smalley * i.e. SIDs that are guaranteed to only be allowed a subset 2310af63f419SStephen Smalley * of the permissions of the current SID. 23117b0d0b40SStephen Smalley */ 2312aa8e712cSStephen Smalley rc = security_bounded_transition(&selinux_state, old_tsec->sid, 2313aa8e712cSStephen Smalley new_tsec->sid); 2314af63f419SStephen Smalley if (!rc) 2315af63f419SStephen Smalley return 0; 2316af63f419SStephen Smalley 23177b0d0b40SStephen Smalley /* 23187b0d0b40SStephen Smalley * On failure, preserve the errno values for NNP vs nosuid. 23197b0d0b40SStephen Smalley * NNP: Operation not permitted for caller. 23207b0d0b40SStephen Smalley * nosuid: Permission denied to file. 23217b0d0b40SStephen Smalley */ 23227b0d0b40SStephen Smalley if (nnp) 23237b0d0b40SStephen Smalley return -EPERM; 23247b0d0b40SStephen Smalley return -EACCES; 23257b0d0b40SStephen Smalley } 23267b0d0b40SStephen Smalley 2327a6f76f23SDavid Howells static int selinux_bprm_set_creds(struct linux_binprm *bprm) 23281da177e4SLinus Torvalds { 2329a6f76f23SDavid Howells const struct task_security_struct *old_tsec; 2330a6f76f23SDavid Howells struct task_security_struct *new_tsec; 23311da177e4SLinus Torvalds struct inode_security_struct *isec; 23322bf49690SThomas Liu struct common_audit_data ad; 2333496ad9aaSAl Viro struct inode *inode = file_inode(bprm->file); 23341da177e4SLinus Torvalds int rc; 23351da177e4SLinus Torvalds 2336a6f76f23SDavid Howells /* SELinux context only depends on initial program or script and not 2337a6f76f23SDavid Howells * the script interpreter */ 2338ddb4a144SKees Cook if (bprm->called_set_creds) 23391da177e4SLinus Torvalds return 0; 23401da177e4SLinus Torvalds 2341a6f76f23SDavid Howells old_tsec = current_security(); 2342a6f76f23SDavid Howells new_tsec = bprm->cred->security; 234383da53c5SAndreas Gruenbacher isec = inode_security(inode); 23441da177e4SLinus Torvalds 23451da177e4SLinus Torvalds /* Default to the current task SID. */ 2346a6f76f23SDavid Howells new_tsec->sid = old_tsec->sid; 2347a6f76f23SDavid Howells new_tsec->osid = old_tsec->sid; 23481da177e4SLinus Torvalds 234928eba5bfSMichael LeMay /* Reset fs, key, and sock SIDs on execve. */ 2350a6f76f23SDavid Howells new_tsec->create_sid = 0; 2351a6f76f23SDavid Howells new_tsec->keycreate_sid = 0; 2352a6f76f23SDavid Howells new_tsec->sockcreate_sid = 0; 23531da177e4SLinus Torvalds 2354a6f76f23SDavid Howells if (old_tsec->exec_sid) { 2355a6f76f23SDavid Howells new_tsec->sid = old_tsec->exec_sid; 23561da177e4SLinus Torvalds /* Reset exec SID on execve. */ 2357a6f76f23SDavid Howells new_tsec->exec_sid = 0; 2358259e5e6cSAndy Lutomirski 23597b0d0b40SStephen Smalley /* Fail on NNP or nosuid if not an allowed transition. */ 23607b0d0b40SStephen Smalley rc = check_nnp_nosuid(bprm, old_tsec, new_tsec); 23617b0d0b40SStephen Smalley if (rc) 23627b0d0b40SStephen Smalley return rc; 23631da177e4SLinus Torvalds } else { 23641da177e4SLinus Torvalds /* Check for a default transition on this program. */ 2365aa8e712cSStephen Smalley rc = security_transition_sid(&selinux_state, old_tsec->sid, 2366aa8e712cSStephen Smalley isec->sid, SECCLASS_PROCESS, NULL, 2367652bb9b0SEric Paris &new_tsec->sid); 23681da177e4SLinus Torvalds if (rc) 23691da177e4SLinus Torvalds return rc; 23707b0d0b40SStephen Smalley 23717b0d0b40SStephen Smalley /* 23727b0d0b40SStephen Smalley * Fallback to old SID on NNP or nosuid if not an allowed 23737b0d0b40SStephen Smalley * transition. 23747b0d0b40SStephen Smalley */ 23757b0d0b40SStephen Smalley rc = check_nnp_nosuid(bprm, old_tsec, new_tsec); 23767b0d0b40SStephen Smalley if (rc) 23777b0d0b40SStephen Smalley new_tsec->sid = old_tsec->sid; 23781da177e4SLinus Torvalds } 23791da177e4SLinus Torvalds 238043af5de7SVivek Goyal ad.type = LSM_AUDIT_DATA_FILE; 238143af5de7SVivek Goyal ad.u.file = bprm->file; 23821da177e4SLinus Torvalds 2383a6f76f23SDavid Howells if (new_tsec->sid == old_tsec->sid) { 23846b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 23856b6bc620SStephen Smalley old_tsec->sid, isec->sid, 23861da177e4SLinus Torvalds SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad); 23871da177e4SLinus Torvalds if (rc) 23881da177e4SLinus Torvalds return rc; 23891da177e4SLinus Torvalds } else { 23901da177e4SLinus Torvalds /* Check permissions for the transition. */ 23916b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 23926b6bc620SStephen Smalley old_tsec->sid, new_tsec->sid, 23931da177e4SLinus Torvalds SECCLASS_PROCESS, PROCESS__TRANSITION, &ad); 23941da177e4SLinus Torvalds if (rc) 23951da177e4SLinus Torvalds return rc; 23961da177e4SLinus Torvalds 23976b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 23986b6bc620SStephen Smalley new_tsec->sid, isec->sid, 23991da177e4SLinus Torvalds SECCLASS_FILE, FILE__ENTRYPOINT, &ad); 24001da177e4SLinus Torvalds if (rc) 24011da177e4SLinus Torvalds return rc; 24021da177e4SLinus Torvalds 2403a6f76f23SDavid Howells /* Check for shared state */ 2404a6f76f23SDavid Howells if (bprm->unsafe & LSM_UNSAFE_SHARE) { 24056b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 24066b6bc620SStephen Smalley old_tsec->sid, new_tsec->sid, 2407a6f76f23SDavid Howells SECCLASS_PROCESS, PROCESS__SHARE, 2408a6f76f23SDavid Howells NULL); 2409a6f76f23SDavid Howells if (rc) 2410a6f76f23SDavid Howells return -EPERM; 24111da177e4SLinus Torvalds } 24121da177e4SLinus Torvalds 2413a6f76f23SDavid Howells /* Make sure that anyone attempting to ptrace over a task that 2414a6f76f23SDavid Howells * changes its SID has the appropriate permit */ 24159227dd2aSEric W. Biederman if (bprm->unsafe & LSM_UNSAFE_PTRACE) { 2416be0554c9SStephen Smalley u32 ptsid = ptrace_parent_sid(); 2417a6f76f23SDavid Howells if (ptsid != 0) { 24186b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 24196b6bc620SStephen Smalley ptsid, new_tsec->sid, 2420a6f76f23SDavid Howells SECCLASS_PROCESS, 2421a6f76f23SDavid Howells PROCESS__PTRACE, NULL); 2422a6f76f23SDavid Howells if (rc) 2423a6f76f23SDavid Howells return -EPERM; 2424a6f76f23SDavid Howells } 2425a6f76f23SDavid Howells } 2426a6f76f23SDavid Howells 2427a6f76f23SDavid Howells /* Clear any possibly unsafe personality bits on exec: */ 2428a6f76f23SDavid Howells bprm->per_clear |= PER_CLEAR_ON_SETID; 2429a6f76f23SDavid Howells 24301da177e4SLinus Torvalds /* Enable secure mode for SIDs transitions unless 24311da177e4SLinus Torvalds the noatsecure permission is granted between 24321da177e4SLinus Torvalds the two SIDs, i.e. ahp returns 0. */ 24336b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 24346b6bc620SStephen Smalley old_tsec->sid, new_tsec->sid, 243562874c3aSKees Cook SECCLASS_PROCESS, PROCESS__NOATSECURE, 243662874c3aSKees Cook NULL); 243762874c3aSKees Cook bprm->secureexec |= !!rc; 24381da177e4SLinus Torvalds } 24391da177e4SLinus Torvalds 244062874c3aSKees Cook return 0; 24411da177e4SLinus Torvalds } 24421da177e4SLinus Torvalds 2443c3c073f8SAl Viro static int match_file(const void *p, struct file *file, unsigned fd) 2444c3c073f8SAl Viro { 2445c3c073f8SAl Viro return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0; 2446c3c073f8SAl Viro } 2447c3c073f8SAl Viro 24481da177e4SLinus Torvalds /* Derived from fs/exec.c:flush_old_files. */ 2449745ca247SDavid Howells static inline void flush_unauthorized_files(const struct cred *cred, 2450745ca247SDavid Howells struct files_struct *files) 24511da177e4SLinus Torvalds { 24521da177e4SLinus Torvalds struct file *file, *devnull = NULL; 2453b20c8122SStephen Smalley struct tty_struct *tty; 245424ec839cSPeter Zijlstra int drop_tty = 0; 2455c3c073f8SAl Viro unsigned n; 24561da177e4SLinus Torvalds 245724ec839cSPeter Zijlstra tty = get_current_tty(); 24581da177e4SLinus Torvalds if (tty) { 24594a510969SPeter Hurley spin_lock(&tty->files_lock); 246037dd0bd0SEric Paris if (!list_empty(&tty->tty_files)) { 2461d996b62aSNick Piggin struct tty_file_private *file_priv; 246237dd0bd0SEric Paris 24631da177e4SLinus Torvalds /* Revalidate access to controlling tty. 246413f8e981SDavid Howells Use file_path_has_perm on the tty path directly 246513f8e981SDavid Howells rather than using file_has_perm, as this particular 246613f8e981SDavid Howells open file may belong to another process and we are 246713f8e981SDavid Howells only interested in the inode-based check here. */ 2468d996b62aSNick Piggin file_priv = list_first_entry(&tty->tty_files, 2469d996b62aSNick Piggin struct tty_file_private, list); 2470d996b62aSNick Piggin file = file_priv->file; 247113f8e981SDavid Howells if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE)) 247224ec839cSPeter Zijlstra drop_tty = 1; 24731da177e4SLinus Torvalds } 24744a510969SPeter Hurley spin_unlock(&tty->files_lock); 2475452a00d2SAlan Cox tty_kref_put(tty); 24761da177e4SLinus Torvalds } 247798a27ba4SEric W. Biederman /* Reset controlling tty. */ 247898a27ba4SEric W. Biederman if (drop_tty) 247998a27ba4SEric W. Biederman no_tty(); 24801da177e4SLinus Torvalds 24811da177e4SLinus Torvalds /* Revalidate access to inherited open files. */ 2482c3c073f8SAl Viro n = iterate_fd(files, 0, match_file, cred); 2483c3c073f8SAl Viro if (!n) /* none found? */ 2484c3c073f8SAl Viro return; 24851da177e4SLinus Torvalds 2486c3c073f8SAl Viro devnull = dentry_open(&selinux_null, O_RDWR, cred); 248745525b26SAl Viro if (IS_ERR(devnull)) 248845525b26SAl Viro devnull = NULL; 2489c3c073f8SAl Viro /* replace all the matching ones with this */ 2490c3c073f8SAl Viro do { 249145525b26SAl Viro replace_fd(n - 1, devnull, 0); 2492c3c073f8SAl Viro } while ((n = iterate_fd(files, n, match_file, cred)) != 0); 249345525b26SAl Viro if (devnull) 2494c3c073f8SAl Viro fput(devnull); 24951da177e4SLinus Torvalds } 24961da177e4SLinus Torvalds 24971da177e4SLinus Torvalds /* 2498a6f76f23SDavid Howells * Prepare a process for imminent new credential changes due to exec 24991da177e4SLinus Torvalds */ 2500a6f76f23SDavid Howells static void selinux_bprm_committing_creds(struct linux_binprm *bprm) 25011da177e4SLinus Torvalds { 2502a6f76f23SDavid Howells struct task_security_struct *new_tsec; 25031da177e4SLinus Torvalds struct rlimit *rlim, *initrlim; 25041da177e4SLinus Torvalds int rc, i; 25051da177e4SLinus Torvalds 2506a6f76f23SDavid Howells new_tsec = bprm->cred->security; 2507a6f76f23SDavid Howells if (new_tsec->sid == new_tsec->osid) 25081da177e4SLinus Torvalds return; 25091da177e4SLinus Torvalds 25101da177e4SLinus Torvalds /* Close files for which the new task SID is not authorized. */ 2511a6f76f23SDavid Howells flush_unauthorized_files(bprm->cred, current->files); 25121da177e4SLinus Torvalds 2513a6f76f23SDavid Howells /* Always clear parent death signal on SID transitions. */ 2514a6f76f23SDavid Howells current->pdeath_signal = 0; 2515a6f76f23SDavid Howells 2516a6f76f23SDavid Howells /* Check whether the new SID can inherit resource limits from the old 2517a6f76f23SDavid Howells * SID. If not, reset all soft limits to the lower of the current 2518a6f76f23SDavid Howells * task's hard limit and the init task's soft limit. 2519a6f76f23SDavid Howells * 2520a6f76f23SDavid Howells * Note that the setting of hard limits (even to lower them) can be 2521a6f76f23SDavid Howells * controlled by the setrlimit check. The inclusion of the init task's 2522a6f76f23SDavid Howells * soft limit into the computation is to avoid resetting soft limits 2523a6f76f23SDavid Howells * higher than the default soft limit for cases where the default is 2524a6f76f23SDavid Howells * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK. 2525a6f76f23SDavid Howells */ 25266b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 25276b6bc620SStephen Smalley new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS, 2528a6f76f23SDavid Howells PROCESS__RLIMITINH, NULL); 2529a6f76f23SDavid Howells if (rc) { 2530eb2d55a3SOleg Nesterov /* protect against do_prlimit() */ 2531eb2d55a3SOleg Nesterov task_lock(current); 2532a6f76f23SDavid Howells for (i = 0; i < RLIM_NLIMITS; i++) { 2533a6f76f23SDavid Howells rlim = current->signal->rlim + i; 2534a6f76f23SDavid Howells initrlim = init_task.signal->rlim + i; 2535a6f76f23SDavid Howells rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur); 2536a6f76f23SDavid Howells } 2537eb2d55a3SOleg Nesterov task_unlock(current); 2538baa73d9eSNicolas Pitre if (IS_ENABLED(CONFIG_POSIX_TIMERS)) 2539eb2d55a3SOleg Nesterov update_rlimit_cpu(current, rlimit(RLIMIT_CPU)); 2540a6f76f23SDavid Howells } 2541a6f76f23SDavid Howells } 2542a6f76f23SDavid Howells 2543a6f76f23SDavid Howells /* 2544a6f76f23SDavid Howells * Clean up the process immediately after the installation of new credentials 2545a6f76f23SDavid Howells * due to exec 2546a6f76f23SDavid Howells */ 2547a6f76f23SDavid Howells static void selinux_bprm_committed_creds(struct linux_binprm *bprm) 2548a6f76f23SDavid Howells { 2549a6f76f23SDavid Howells const struct task_security_struct *tsec = current_security(); 2550a6f76f23SDavid Howells struct itimerval itimer; 2551a6f76f23SDavid Howells u32 osid, sid; 2552a6f76f23SDavid Howells int rc, i; 2553a6f76f23SDavid Howells 2554a6f76f23SDavid Howells osid = tsec->osid; 2555a6f76f23SDavid Howells sid = tsec->sid; 2556a6f76f23SDavid Howells 2557a6f76f23SDavid Howells if (sid == osid) 2558a6f76f23SDavid Howells return; 2559a6f76f23SDavid Howells 2560a6f76f23SDavid Howells /* Check whether the new SID can inherit signal state from the old SID. 2561a6f76f23SDavid Howells * If not, clear itimers to avoid subsequent signal generation and 2562a6f76f23SDavid Howells * flush and unblock signals. 2563a6f76f23SDavid Howells * 2564a6f76f23SDavid Howells * This must occur _after_ the task SID has been updated so that any 2565a6f76f23SDavid Howells * kill done after the flush will be checked against the new SID. 2566a6f76f23SDavid Howells */ 25676b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 25686b6bc620SStephen Smalley osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL); 25691da177e4SLinus Torvalds if (rc) { 2570baa73d9eSNicolas Pitre if (IS_ENABLED(CONFIG_POSIX_TIMERS)) { 25711da177e4SLinus Torvalds memset(&itimer, 0, sizeof itimer); 25721da177e4SLinus Torvalds for (i = 0; i < 3; i++) 25731da177e4SLinus Torvalds do_setitimer(i, &itimer, NULL); 2574baa73d9eSNicolas Pitre } 25751da177e4SLinus Torvalds spin_lock_irq(¤t->sighand->siglock); 25769e7c8f8cSOleg Nesterov if (!fatal_signal_pending(current)) { 25779e7c8f8cSOleg Nesterov flush_sigqueue(¤t->pending); 25789e7c8f8cSOleg Nesterov flush_sigqueue(¤t->signal->shared_pending); 25791da177e4SLinus Torvalds flush_signal_handlers(current, 1); 25801da177e4SLinus Torvalds sigemptyset(¤t->blocked); 25819e7c8f8cSOleg Nesterov recalc_sigpending(); 25823bcac026SDavid Howells } 25831da177e4SLinus Torvalds spin_unlock_irq(¤t->sighand->siglock); 25841da177e4SLinus Torvalds } 25851da177e4SLinus Torvalds 2586a6f76f23SDavid Howells /* Wake up the parent if it is waiting so that it can recheck 2587a6f76f23SDavid Howells * wait permission to the new task SID. */ 2588ecd6de3cSOleg Nesterov read_lock(&tasklist_lock); 25890b7570e7SOleg Nesterov __wake_up_parent(current, current->real_parent); 2590ecd6de3cSOleg Nesterov read_unlock(&tasklist_lock); 25911da177e4SLinus Torvalds } 25921da177e4SLinus Torvalds 25931da177e4SLinus Torvalds /* superblock security operations */ 25941da177e4SLinus Torvalds 25951da177e4SLinus Torvalds static int selinux_sb_alloc_security(struct super_block *sb) 25961da177e4SLinus Torvalds { 25971da177e4SLinus Torvalds return superblock_alloc_security(sb); 25981da177e4SLinus Torvalds } 25991da177e4SLinus Torvalds 26001da177e4SLinus Torvalds static void selinux_sb_free_security(struct super_block *sb) 26011da177e4SLinus Torvalds { 26021da177e4SLinus Torvalds superblock_free_security(sb); 26031da177e4SLinus Torvalds } 26041da177e4SLinus Torvalds 260599dbbb59SAl Viro static inline int opt_len(const char *s) 26061da177e4SLinus Torvalds { 260799dbbb59SAl Viro bool open_quote = false; 260899dbbb59SAl Viro int len; 260999dbbb59SAl Viro char c; 26101da177e4SLinus Torvalds 261199dbbb59SAl Viro for (len = 0; (c = s[len]) != '\0'; len++) { 261299dbbb59SAl Viro if (c == '"') 26133528a953SCory Olmo open_quote = !open_quote; 261499dbbb59SAl Viro if (c == ',' && !open_quote) 261599dbbb59SAl Viro break; 26161da177e4SLinus Torvalds } 261799dbbb59SAl Viro return len; 26181da177e4SLinus Torvalds } 26191da177e4SLinus Torvalds 2620204cc0ccSAl Viro static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts) 26215b400239SAl Viro { 262299dbbb59SAl Viro char *from = options; 262399dbbb59SAl Viro char *to = options; 262499dbbb59SAl Viro bool first = true; 26255b400239SAl Viro 262699dbbb59SAl Viro while (1) { 262799dbbb59SAl Viro int len = opt_len(from); 262899dbbb59SAl Viro int token, rc; 262999dbbb59SAl Viro char *arg = NULL; 263099dbbb59SAl Viro 263199dbbb59SAl Viro token = match_opt_prefix(from, len, &arg); 263299dbbb59SAl Viro 263399dbbb59SAl Viro if (token != Opt_error) { 263499dbbb59SAl Viro char *p, *q; 263599dbbb59SAl Viro 263699dbbb59SAl Viro /* strip quotes */ 263799dbbb59SAl Viro if (arg) { 263899dbbb59SAl Viro for (p = q = arg; p < from + len; p++) { 263999dbbb59SAl Viro char c = *p; 264099dbbb59SAl Viro if (c != '"') 264199dbbb59SAl Viro *q++ = c; 264299dbbb59SAl Viro } 264399dbbb59SAl Viro arg = kmemdup_nul(arg, q - arg, GFP_KERNEL); 264499dbbb59SAl Viro } 264599dbbb59SAl Viro rc = selinux_add_opt(token, arg, mnt_opts); 264699dbbb59SAl Viro if (unlikely(rc)) { 264799dbbb59SAl Viro kfree(arg); 264899dbbb59SAl Viro if (*mnt_opts) { 264999dbbb59SAl Viro selinux_free_mnt_opts(*mnt_opts); 265099dbbb59SAl Viro *mnt_opts = NULL; 265199dbbb59SAl Viro } 26521da177e4SLinus Torvalds return rc; 26531da177e4SLinus Torvalds } 265499dbbb59SAl Viro } else { 265599dbbb59SAl Viro if (!first) { // copy with preceding comma 265699dbbb59SAl Viro from--; 265799dbbb59SAl Viro len++; 265899dbbb59SAl Viro } 265999dbbb59SAl Viro if (to != from) 266099dbbb59SAl Viro memmove(to, from, len); 266199dbbb59SAl Viro to += len; 266299dbbb59SAl Viro first = false; 266399dbbb59SAl Viro } 266499dbbb59SAl Viro if (!from[len]) 266599dbbb59SAl Viro break; 266699dbbb59SAl Viro from += len + 1; 266799dbbb59SAl Viro } 266899dbbb59SAl Viro *to = '\0'; 266999dbbb59SAl Viro return 0; 26705b400239SAl Viro } 26711da177e4SLinus Torvalds 2672204cc0ccSAl Viro static int selinux_sb_remount(struct super_block *sb, void *mnt_opts) 2673026eb167SEric Paris { 2674bd323655SAl Viro struct selinux_mnt_opts *opts = mnt_opts; 2675026eb167SEric Paris struct superblock_security_struct *sbsec = sb->s_security; 2676bd323655SAl Viro u32 sid; 2677bd323655SAl Viro int rc; 2678026eb167SEric Paris 2679026eb167SEric Paris if (!(sbsec->flags & SE_SBINITIALIZED)) 2680026eb167SEric Paris return 0; 2681026eb167SEric Paris 2682204cc0ccSAl Viro if (!opts) 2683026eb167SEric Paris return 0; 2684026eb167SEric Paris 2685bd323655SAl Viro if (opts->fscontext) { 2686bd323655SAl Viro rc = parse_sid(sb, opts->fscontext, &sid); 2687026eb167SEric Paris if (rc) 2688c039bc3cSAl Viro return rc; 2689026eb167SEric Paris if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid)) 2690026eb167SEric Paris goto out_bad_option; 2691bd323655SAl Viro } 2692bd323655SAl Viro if (opts->context) { 2693bd323655SAl Viro rc = parse_sid(sb, opts->context, &sid); 2694bd323655SAl Viro if (rc) 2695bd323655SAl Viro return rc; 2696026eb167SEric Paris if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid)) 2697026eb167SEric Paris goto out_bad_option; 2698bd323655SAl Viro } 2699bd323655SAl Viro if (opts->rootcontext) { 2700026eb167SEric Paris struct inode_security_struct *root_isec; 270183da53c5SAndreas Gruenbacher root_isec = backing_inode_security(sb->s_root); 2702bd323655SAl Viro rc = parse_sid(sb, opts->rootcontext, &sid); 2703bd323655SAl Viro if (rc) 2704bd323655SAl Viro return rc; 2705026eb167SEric Paris if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid)) 2706026eb167SEric Paris goto out_bad_option; 2707026eb167SEric Paris } 2708bd323655SAl Viro if (opts->defcontext) { 2709bd323655SAl Viro rc = parse_sid(sb, opts->defcontext, &sid); 2710bd323655SAl Viro if (rc) 2711bd323655SAl Viro return rc; 2712026eb167SEric Paris if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid)) 2713026eb167SEric Paris goto out_bad_option; 2714026eb167SEric Paris } 2715c039bc3cSAl Viro return 0; 2716026eb167SEric Paris 2717026eb167SEric Paris out_bad_option: 2718c103a91eSpeter enderborg pr_warn("SELinux: unable to change security options " 271929b1deb2SLinus Torvalds "during remount (dev %s, type=%s)\n", sb->s_id, 272029b1deb2SLinus Torvalds sb->s_type->name); 2721c039bc3cSAl Viro return -EINVAL; 2722026eb167SEric Paris } 2723026eb167SEric Paris 2724a10d7c22SAl Viro static int selinux_sb_kern_mount(struct super_block *sb) 27251da177e4SLinus Torvalds { 272688e67f3bSDavid Howells const struct cred *cred = current_cred(); 27272bf49690SThomas Liu struct common_audit_data ad; 272874192246SJames Morris 272950c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 2730a269434dSEric Paris ad.u.dentry = sb->s_root; 273188e67f3bSDavid Howells return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad); 27321da177e4SLinus Torvalds } 27331da177e4SLinus Torvalds 2734726c3342SDavid Howells static int selinux_sb_statfs(struct dentry *dentry) 27351da177e4SLinus Torvalds { 273688e67f3bSDavid Howells const struct cred *cred = current_cred(); 27372bf49690SThomas Liu struct common_audit_data ad; 27381da177e4SLinus Torvalds 273950c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 2740a269434dSEric Paris ad.u.dentry = dentry->d_sb->s_root; 274188e67f3bSDavid Howells return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad); 27421da177e4SLinus Torvalds } 27431da177e4SLinus Torvalds 2744808d4e3cSAl Viro static int selinux_mount(const char *dev_name, 27458a04c43bSAl Viro const struct path *path, 2746808d4e3cSAl Viro const char *type, 27471da177e4SLinus Torvalds unsigned long flags, 27481da177e4SLinus Torvalds void *data) 27491da177e4SLinus Torvalds { 275088e67f3bSDavid Howells const struct cred *cred = current_cred(); 27511da177e4SLinus Torvalds 27521da177e4SLinus Torvalds if (flags & MS_REMOUNT) 2753d8c9584eSAl Viro return superblock_has_perm(cred, path->dentry->d_sb, 27541da177e4SLinus Torvalds FILESYSTEM__REMOUNT, NULL); 27551da177e4SLinus Torvalds else 27562875fa00SEric Paris return path_has_perm(cred, path, FILE__MOUNTON); 27571da177e4SLinus Torvalds } 27581da177e4SLinus Torvalds 27591da177e4SLinus Torvalds static int selinux_umount(struct vfsmount *mnt, int flags) 27601da177e4SLinus Torvalds { 276188e67f3bSDavid Howells const struct cred *cred = current_cred(); 27621da177e4SLinus Torvalds 276388e67f3bSDavid Howells return superblock_has_perm(cred, mnt->mnt_sb, 27641da177e4SLinus Torvalds FILESYSTEM__UNMOUNT, NULL); 27651da177e4SLinus Torvalds } 27661da177e4SLinus Torvalds 2767*0b52075eSAl Viro static int selinux_fs_context_dup(struct fs_context *fc, 2768*0b52075eSAl Viro struct fs_context *src_fc) 2769*0b52075eSAl Viro { 2770*0b52075eSAl Viro const struct selinux_mnt_opts *src = src_fc->security; 2771*0b52075eSAl Viro struct selinux_mnt_opts *opts; 2772*0b52075eSAl Viro 2773*0b52075eSAl Viro if (!src) 2774*0b52075eSAl Viro return 0; 2775*0b52075eSAl Viro 2776*0b52075eSAl Viro fc->security = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL); 2777*0b52075eSAl Viro if (!fc->security) 2778*0b52075eSAl Viro return -ENOMEM; 2779*0b52075eSAl Viro 2780*0b52075eSAl Viro opts = fc->security; 2781*0b52075eSAl Viro 2782*0b52075eSAl Viro if (src->fscontext) { 2783*0b52075eSAl Viro opts->fscontext = kstrdup(src->fscontext, GFP_KERNEL); 2784*0b52075eSAl Viro if (!opts->fscontext) 2785*0b52075eSAl Viro return -ENOMEM; 2786*0b52075eSAl Viro } 2787*0b52075eSAl Viro if (src->context) { 2788*0b52075eSAl Viro opts->context = kstrdup(src->context, GFP_KERNEL); 2789*0b52075eSAl Viro if (!opts->context) 2790*0b52075eSAl Viro return -ENOMEM; 2791*0b52075eSAl Viro } 2792*0b52075eSAl Viro if (src->rootcontext) { 2793*0b52075eSAl Viro opts->rootcontext = kstrdup(src->rootcontext, GFP_KERNEL); 2794*0b52075eSAl Viro if (!opts->rootcontext) 2795*0b52075eSAl Viro return -ENOMEM; 2796*0b52075eSAl Viro } 2797*0b52075eSAl Viro if (src->defcontext) { 2798*0b52075eSAl Viro opts->defcontext = kstrdup(src->defcontext, GFP_KERNEL); 2799*0b52075eSAl Viro if (!opts->defcontext) 2800*0b52075eSAl Viro return -ENOMEM; 2801*0b52075eSAl Viro } 2802*0b52075eSAl Viro return 0; 2803*0b52075eSAl Viro } 2804*0b52075eSAl Viro 2805442155c1SDavid Howells static const struct fs_parameter_spec selinux_param_specs[] = { 2806442155c1SDavid Howells fsparam_string(CONTEXT_STR, Opt_context), 2807442155c1SDavid Howells fsparam_string(DEFCONTEXT_STR, Opt_defcontext), 2808442155c1SDavid Howells fsparam_string(FSCONTEXT_STR, Opt_fscontext), 2809442155c1SDavid Howells fsparam_string(ROOTCONTEXT_STR, Opt_rootcontext), 2810442155c1SDavid Howells fsparam_flag (SECLABEL_STR, Opt_seclabel), 2811442155c1SDavid Howells {} 2812442155c1SDavid Howells }; 2813442155c1SDavid Howells 2814442155c1SDavid Howells static const struct fs_parameter_description selinux_fs_parameters = { 2815442155c1SDavid Howells .name = "SELinux", 2816442155c1SDavid Howells .specs = selinux_param_specs, 2817442155c1SDavid Howells }; 2818442155c1SDavid Howells 2819442155c1SDavid Howells static int selinux_fs_context_parse_param(struct fs_context *fc, 2820442155c1SDavid Howells struct fs_parameter *param) 2821442155c1SDavid Howells { 2822442155c1SDavid Howells struct fs_parse_result result; 2823442155c1SDavid Howells int opt, rc; 2824442155c1SDavid Howells 2825442155c1SDavid Howells opt = fs_parse(fc, &selinux_fs_parameters, param, &result); 2826442155c1SDavid Howells if (opt < 0) 2827442155c1SDavid Howells return opt; 2828442155c1SDavid Howells 2829442155c1SDavid Howells rc = selinux_add_opt(opt, param->string, &fc->security); 2830442155c1SDavid Howells if (!rc) { 2831442155c1SDavid Howells param->string = NULL; 2832442155c1SDavid Howells rc = 1; 2833442155c1SDavid Howells } 2834442155c1SDavid Howells return rc; 2835442155c1SDavid Howells } 2836442155c1SDavid Howells 28371da177e4SLinus Torvalds /* inode security operations */ 28381da177e4SLinus Torvalds 28391da177e4SLinus Torvalds static int selinux_inode_alloc_security(struct inode *inode) 28401da177e4SLinus Torvalds { 28411da177e4SLinus Torvalds return inode_alloc_security(inode); 28421da177e4SLinus Torvalds } 28431da177e4SLinus Torvalds 28441da177e4SLinus Torvalds static void selinux_inode_free_security(struct inode *inode) 28451da177e4SLinus Torvalds { 28461da177e4SLinus Torvalds inode_free_security(inode); 28471da177e4SLinus Torvalds } 28481da177e4SLinus Torvalds 2849d47be3dfSDavid Quigley static int selinux_dentry_init_security(struct dentry *dentry, int mode, 28504f3ccd76SAl Viro const struct qstr *name, void **ctx, 2851d47be3dfSDavid Quigley u32 *ctxlen) 2852d47be3dfSDavid Quigley { 2853d47be3dfSDavid Quigley u32 newsid; 2854d47be3dfSDavid Quigley int rc; 2855d47be3dfSDavid Quigley 2856c957f6dfSVivek Goyal rc = selinux_determine_inode_label(current_security(), 2857c957f6dfSVivek Goyal d_inode(dentry->d_parent), name, 2858d47be3dfSDavid Quigley inode_mode_to_security_class(mode), 2859d47be3dfSDavid Quigley &newsid); 2860c3c188b2SDavid Howells if (rc) 2861d47be3dfSDavid Quigley return rc; 2862d47be3dfSDavid Quigley 2863aa8e712cSStephen Smalley return security_sid_to_context(&selinux_state, newsid, (char **)ctx, 2864aa8e712cSStephen Smalley ctxlen); 2865d47be3dfSDavid Quigley } 2866d47be3dfSDavid Quigley 2867a518b0a5SVivek Goyal static int selinux_dentry_create_files_as(struct dentry *dentry, int mode, 2868a518b0a5SVivek Goyal struct qstr *name, 2869a518b0a5SVivek Goyal const struct cred *old, 2870a518b0a5SVivek Goyal struct cred *new) 2871a518b0a5SVivek Goyal { 2872a518b0a5SVivek Goyal u32 newsid; 2873a518b0a5SVivek Goyal int rc; 2874a518b0a5SVivek Goyal struct task_security_struct *tsec; 2875a518b0a5SVivek Goyal 2876a518b0a5SVivek Goyal rc = selinux_determine_inode_label(old->security, 2877a518b0a5SVivek Goyal d_inode(dentry->d_parent), name, 2878a518b0a5SVivek Goyal inode_mode_to_security_class(mode), 2879a518b0a5SVivek Goyal &newsid); 2880a518b0a5SVivek Goyal if (rc) 2881a518b0a5SVivek Goyal return rc; 2882a518b0a5SVivek Goyal 2883a518b0a5SVivek Goyal tsec = new->security; 2884a518b0a5SVivek Goyal tsec->create_sid = newsid; 2885a518b0a5SVivek Goyal return 0; 2886a518b0a5SVivek Goyal } 2887a518b0a5SVivek Goyal 28885e41ff9eSStephen Smalley static int selinux_inode_init_security(struct inode *inode, struct inode *dir, 28899548906bSTetsuo Handa const struct qstr *qstr, 28909548906bSTetsuo Handa const char **name, 28912a7dba39SEric Paris void **value, size_t *len) 28925e41ff9eSStephen Smalley { 28935fb49870SPaul Moore const struct task_security_struct *tsec = current_security(); 28945e41ff9eSStephen Smalley struct superblock_security_struct *sbsec; 2895c0d4f464SCorentin LABBE u32 newsid, clen; 28965e41ff9eSStephen Smalley int rc; 28979548906bSTetsuo Handa char *context; 28985e41ff9eSStephen Smalley 28995e41ff9eSStephen Smalley sbsec = dir->i_sb->s_security; 29005e41ff9eSStephen Smalley 29015e41ff9eSStephen Smalley newsid = tsec->create_sid; 2902275bb41eSDavid Howells 2903c957f6dfSVivek Goyal rc = selinux_determine_inode_label(current_security(), 2904c3c188b2SDavid Howells dir, qstr, 29055e41ff9eSStephen Smalley inode_mode_to_security_class(inode->i_mode), 2906c3c188b2SDavid Howells &newsid); 2907c3c188b2SDavid Howells if (rc) 29085e41ff9eSStephen Smalley return rc; 29095e41ff9eSStephen Smalley 2910296fddf7SEric Paris /* Possibly defer initialization to selinux_complete_init. */ 29110d90a7ecSDavid P. Quigley if (sbsec->flags & SE_SBINITIALIZED) { 2912296fddf7SEric Paris struct inode_security_struct *isec = inode->i_security; 2913296fddf7SEric Paris isec->sclass = inode_mode_to_security_class(inode->i_mode); 2914296fddf7SEric Paris isec->sid = newsid; 29156f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 2916296fddf7SEric Paris } 29175e41ff9eSStephen Smalley 2918aa8e712cSStephen Smalley if (!selinux_state.initialized || !(sbsec->flags & SBLABEL_MNT)) 291925a74f3bSStephen Smalley return -EOPNOTSUPP; 292025a74f3bSStephen Smalley 29219548906bSTetsuo Handa if (name) 29229548906bSTetsuo Handa *name = XATTR_SELINUX_SUFFIX; 29235e41ff9eSStephen Smalley 2924570bc1c2SStephen Smalley if (value && len) { 2925aa8e712cSStephen Smalley rc = security_sid_to_context_force(&selinux_state, newsid, 2926aa8e712cSStephen Smalley &context, &clen); 29279548906bSTetsuo Handa if (rc) 29285e41ff9eSStephen Smalley return rc; 29295e41ff9eSStephen Smalley *value = context; 2930570bc1c2SStephen Smalley *len = clen; 2931570bc1c2SStephen Smalley } 29325e41ff9eSStephen Smalley 29335e41ff9eSStephen Smalley return 0; 29345e41ff9eSStephen Smalley } 29355e41ff9eSStephen Smalley 29364acdaf27SAl Viro static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode) 29371da177e4SLinus Torvalds { 29381da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_FILE); 29391da177e4SLinus Torvalds } 29401da177e4SLinus Torvalds 29411da177e4SLinus Torvalds static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 29421da177e4SLinus Torvalds { 29431da177e4SLinus Torvalds return may_link(dir, old_dentry, MAY_LINK); 29441da177e4SLinus Torvalds } 29451da177e4SLinus Torvalds 29461da177e4SLinus Torvalds static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry) 29471da177e4SLinus Torvalds { 29481da177e4SLinus Torvalds return may_link(dir, dentry, MAY_UNLINK); 29491da177e4SLinus Torvalds } 29501da177e4SLinus Torvalds 29511da177e4SLinus Torvalds static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name) 29521da177e4SLinus Torvalds { 29531da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_LNK_FILE); 29541da177e4SLinus Torvalds } 29551da177e4SLinus Torvalds 295618bb1db3SAl Viro static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask) 29571da177e4SLinus Torvalds { 29581da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_DIR); 29591da177e4SLinus Torvalds } 29601da177e4SLinus Torvalds 29611da177e4SLinus Torvalds static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry) 29621da177e4SLinus Torvalds { 29631da177e4SLinus Torvalds return may_link(dir, dentry, MAY_RMDIR); 29641da177e4SLinus Torvalds } 29651da177e4SLinus Torvalds 29661a67aafbSAl Viro static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) 29671da177e4SLinus Torvalds { 29681da177e4SLinus Torvalds return may_create(dir, dentry, inode_mode_to_security_class(mode)); 29691da177e4SLinus Torvalds } 29701da177e4SLinus Torvalds 29711da177e4SLinus Torvalds static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry, 29721da177e4SLinus Torvalds struct inode *new_inode, struct dentry *new_dentry) 29731da177e4SLinus Torvalds { 29741da177e4SLinus Torvalds return may_rename(old_inode, old_dentry, new_inode, new_dentry); 29751da177e4SLinus Torvalds } 29761da177e4SLinus Torvalds 29771da177e4SLinus Torvalds static int selinux_inode_readlink(struct dentry *dentry) 29781da177e4SLinus Torvalds { 297988e67f3bSDavid Howells const struct cred *cred = current_cred(); 298088e67f3bSDavid Howells 29812875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__READ); 29821da177e4SLinus Torvalds } 29831da177e4SLinus Torvalds 2984bda0be7aSNeilBrown static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode, 2985bda0be7aSNeilBrown bool rcu) 29861da177e4SLinus Torvalds { 298788e67f3bSDavid Howells const struct cred *cred = current_cred(); 2988bda0be7aSNeilBrown struct common_audit_data ad; 2989bda0be7aSNeilBrown struct inode_security_struct *isec; 2990bda0be7aSNeilBrown u32 sid; 29911da177e4SLinus Torvalds 2992bda0be7aSNeilBrown validate_creds(cred); 2993bda0be7aSNeilBrown 2994bda0be7aSNeilBrown ad.type = LSM_AUDIT_DATA_DENTRY; 2995bda0be7aSNeilBrown ad.u.dentry = dentry; 2996bda0be7aSNeilBrown sid = cred_sid(cred); 29975d226df4SAndreas Gruenbacher isec = inode_security_rcu(inode, rcu); 29985d226df4SAndreas Gruenbacher if (IS_ERR(isec)) 29995d226df4SAndreas Gruenbacher return PTR_ERR(isec); 3000bda0be7aSNeilBrown 30016b6bc620SStephen Smalley return avc_has_perm_flags(&selinux_state, 30026b6bc620SStephen Smalley sid, isec->sid, isec->sclass, FILE__READ, &ad, 3003bda0be7aSNeilBrown rcu ? MAY_NOT_BLOCK : 0); 30041da177e4SLinus Torvalds } 30051da177e4SLinus Torvalds 3006d4cf970dSEric Paris static noinline int audit_inode_permission(struct inode *inode, 3007d4cf970dSEric Paris u32 perms, u32 audited, u32 denied, 3008626b9740SStephen Smalley int result, 3009d4cf970dSEric Paris unsigned flags) 3010d4cf970dSEric Paris { 3011d4cf970dSEric Paris struct common_audit_data ad; 3012d4cf970dSEric Paris struct inode_security_struct *isec = inode->i_security; 3013d4cf970dSEric Paris int rc; 3014d4cf970dSEric Paris 301550c205f5SEric Paris ad.type = LSM_AUDIT_DATA_INODE; 3016d4cf970dSEric Paris ad.u.inode = inode; 3017d4cf970dSEric Paris 30186b6bc620SStephen Smalley rc = slow_avc_audit(&selinux_state, 30196b6bc620SStephen Smalley current_sid(), isec->sid, isec->sclass, perms, 3020626b9740SStephen Smalley audited, denied, result, &ad, flags); 3021d4cf970dSEric Paris if (rc) 3022d4cf970dSEric Paris return rc; 3023d4cf970dSEric Paris return 0; 3024d4cf970dSEric Paris } 3025d4cf970dSEric Paris 3026e74f71ebSAl Viro static int selinux_inode_permission(struct inode *inode, int mask) 30271da177e4SLinus Torvalds { 302888e67f3bSDavid Howells const struct cred *cred = current_cred(); 3029b782e0a6SEric Paris u32 perms; 3030b782e0a6SEric Paris bool from_access; 3031cf1dd1daSAl Viro unsigned flags = mask & MAY_NOT_BLOCK; 30322e334057SEric Paris struct inode_security_struct *isec; 30332e334057SEric Paris u32 sid; 30342e334057SEric Paris struct av_decision avd; 30352e334057SEric Paris int rc, rc2; 30362e334057SEric Paris u32 audited, denied; 30371da177e4SLinus Torvalds 3038b782e0a6SEric Paris from_access = mask & MAY_ACCESS; 3039d09ca739SEric Paris mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND); 3040d09ca739SEric Paris 30411da177e4SLinus Torvalds /* No permission to check. Existence test. */ 3042b782e0a6SEric Paris if (!mask) 30431da177e4SLinus Torvalds return 0; 30441da177e4SLinus Torvalds 30452e334057SEric Paris validate_creds(cred); 3046b782e0a6SEric Paris 30472e334057SEric Paris if (unlikely(IS_PRIVATE(inode))) 30482e334057SEric Paris return 0; 3049b782e0a6SEric Paris 3050b782e0a6SEric Paris perms = file_mask_to_av(inode->i_mode, mask); 3051b782e0a6SEric Paris 30522e334057SEric Paris sid = cred_sid(cred); 30535d226df4SAndreas Gruenbacher isec = inode_security_rcu(inode, flags & MAY_NOT_BLOCK); 30545d226df4SAndreas Gruenbacher if (IS_ERR(isec)) 30555d226df4SAndreas Gruenbacher return PTR_ERR(isec); 30562e334057SEric Paris 30576b6bc620SStephen Smalley rc = avc_has_perm_noaudit(&selinux_state, 30586b6bc620SStephen Smalley sid, isec->sid, isec->sclass, perms, 0, &avd); 30592e334057SEric Paris audited = avc_audit_required(perms, &avd, rc, 30602e334057SEric Paris from_access ? FILE__AUDIT_ACCESS : 0, 30612e334057SEric Paris &denied); 30622e334057SEric Paris if (likely(!audited)) 30632e334057SEric Paris return rc; 30642e334057SEric Paris 3065626b9740SStephen Smalley rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags); 30662e334057SEric Paris if (rc2) 30672e334057SEric Paris return rc2; 30682e334057SEric Paris return rc; 30691da177e4SLinus Torvalds } 30701da177e4SLinus Torvalds 30711da177e4SLinus Torvalds static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr) 30721da177e4SLinus Torvalds { 307388e67f3bSDavid Howells const struct cred *cred = current_cred(); 3074ccb54478SStephen Smalley struct inode *inode = d_backing_inode(dentry); 3075bc6a6008SAmerigo Wang unsigned int ia_valid = iattr->ia_valid; 307695dbf739SEric Paris __u32 av = FILE__WRITE; 30771da177e4SLinus Torvalds 3078bc6a6008SAmerigo Wang /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */ 3079bc6a6008SAmerigo Wang if (ia_valid & ATTR_FORCE) { 3080bc6a6008SAmerigo Wang ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE | 3081bc6a6008SAmerigo Wang ATTR_FORCE); 3082bc6a6008SAmerigo Wang if (!ia_valid) 30831da177e4SLinus Torvalds return 0; 3084bc6a6008SAmerigo Wang } 30851da177e4SLinus Torvalds 3086bc6a6008SAmerigo Wang if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | 3087bc6a6008SAmerigo Wang ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET)) 30882875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__SETATTR); 30891da177e4SLinus Torvalds 3090aa8e712cSStephen Smalley if (selinux_policycap_openperm() && 3091ccb54478SStephen Smalley inode->i_sb->s_magic != SOCKFS_MAGIC && 3092ccb54478SStephen Smalley (ia_valid & ATTR_SIZE) && 3093ccb54478SStephen Smalley !(ia_valid & ATTR_FILE)) 309495dbf739SEric Paris av |= FILE__OPEN; 309595dbf739SEric Paris 309695dbf739SEric Paris return dentry_has_perm(cred, dentry, av); 30971da177e4SLinus Torvalds } 30981da177e4SLinus Torvalds 30993f7036a0SAl Viro static int selinux_inode_getattr(const struct path *path) 31001da177e4SLinus Torvalds { 31013f7036a0SAl Viro return path_has_perm(current_cred(), path, FILE__GETATTR); 31021da177e4SLinus Torvalds } 31031da177e4SLinus Torvalds 3104db59000aSStephen Smalley static bool has_cap_mac_admin(bool audit) 3105db59000aSStephen Smalley { 3106db59000aSStephen Smalley const struct cred *cred = current_cred(); 3107db59000aSStephen Smalley int cap_audit = audit ? SECURITY_CAP_AUDIT : SECURITY_CAP_NOAUDIT; 3108db59000aSStephen Smalley 3109db59000aSStephen Smalley if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, cap_audit)) 3110db59000aSStephen Smalley return false; 3111db59000aSStephen Smalley if (cred_has_capability(cred, CAP_MAC_ADMIN, cap_audit, true)) 3112db59000aSStephen Smalley return false; 3113db59000aSStephen Smalley return true; 3114db59000aSStephen Smalley } 3115db59000aSStephen Smalley 31168f0cfa52SDavid Howells static int selinux_inode_setxattr(struct dentry *dentry, const char *name, 31178f0cfa52SDavid Howells const void *value, size_t size, int flags) 31181da177e4SLinus Torvalds { 3119c6f493d6SDavid Howells struct inode *inode = d_backing_inode(dentry); 312020cdef8dSPaul Moore struct inode_security_struct *isec; 31211da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 31222bf49690SThomas Liu struct common_audit_data ad; 3123275bb41eSDavid Howells u32 newsid, sid = current_sid(); 31241da177e4SLinus Torvalds int rc = 0; 31251da177e4SLinus Torvalds 31266b240306SEric W. Biederman if (strcmp(name, XATTR_NAME_SELINUX)) { 31276b240306SEric W. Biederman rc = cap_inode_setxattr(dentry, name, value, size, flags); 31286b240306SEric W. Biederman if (rc) 31296b240306SEric W. Biederman return rc; 31306b240306SEric W. Biederman 31316b240306SEric W. Biederman /* Not an attribute we recognize, so just check the 31326b240306SEric W. Biederman ordinary setattr permission. */ 31336b240306SEric W. Biederman return dentry_has_perm(current_cred(), dentry, FILE__SETATTR); 31346b240306SEric W. Biederman } 31351da177e4SLinus Torvalds 31361da177e4SLinus Torvalds sbsec = inode->i_sb->s_security; 313712f348b9SEric Paris if (!(sbsec->flags & SBLABEL_MNT)) 31381da177e4SLinus Torvalds return -EOPNOTSUPP; 31391da177e4SLinus Torvalds 31402e149670SSerge E. Hallyn if (!inode_owner_or_capable(inode)) 31411da177e4SLinus Torvalds return -EPERM; 31421da177e4SLinus Torvalds 314350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 3144a269434dSEric Paris ad.u.dentry = dentry; 31451da177e4SLinus Torvalds 314620cdef8dSPaul Moore isec = backing_inode_security(dentry); 31476b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 31486b6bc620SStephen Smalley sid, isec->sid, isec->sclass, 31491da177e4SLinus Torvalds FILE__RELABELFROM, &ad); 31501da177e4SLinus Torvalds if (rc) 31511da177e4SLinus Torvalds return rc; 31521da177e4SLinus Torvalds 3153aa8e712cSStephen Smalley rc = security_context_to_sid(&selinux_state, value, size, &newsid, 3154aa8e712cSStephen Smalley GFP_KERNEL); 315512b29f34SStephen Smalley if (rc == -EINVAL) { 3156db59000aSStephen Smalley if (!has_cap_mac_admin(true)) { 3157d6ea83ecSEric Paris struct audit_buffer *ab; 3158d6ea83ecSEric Paris size_t audit_size; 3159d6ea83ecSEric Paris 3160d6ea83ecSEric Paris /* We strip a nul only if it is at the end, otherwise the 3161d6ea83ecSEric Paris * context contains a nul and we should audit that */ 3162e3fea3f7SAl Viro if (value) { 3163add24372SColin Ian King const char *str = value; 3164add24372SColin Ian King 3165d6ea83ecSEric Paris if (str[size - 1] == '\0') 3166d6ea83ecSEric Paris audit_size = size - 1; 3167d6ea83ecSEric Paris else 3168d6ea83ecSEric Paris audit_size = size; 3169e3fea3f7SAl Viro } else { 3170e3fea3f7SAl Viro audit_size = 0; 3171e3fea3f7SAl Viro } 3172cdfb6b34SRichard Guy Briggs ab = audit_log_start(audit_context(), 3173cdfb6b34SRichard Guy Briggs GFP_ATOMIC, AUDIT_SELINUX_ERR); 3174d6ea83ecSEric Paris audit_log_format(ab, "op=setxattr invalid_context="); 3175d6ea83ecSEric Paris audit_log_n_untrustedstring(ab, value, audit_size); 3176d6ea83ecSEric Paris audit_log_end(ab); 3177d6ea83ecSEric Paris 317812b29f34SStephen Smalley return rc; 3179d6ea83ecSEric Paris } 3180aa8e712cSStephen Smalley rc = security_context_to_sid_force(&selinux_state, value, 3181aa8e712cSStephen Smalley size, &newsid); 318212b29f34SStephen Smalley } 31831da177e4SLinus Torvalds if (rc) 31841da177e4SLinus Torvalds return rc; 31851da177e4SLinus Torvalds 31866b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 31876b6bc620SStephen Smalley sid, newsid, isec->sclass, 31881da177e4SLinus Torvalds FILE__RELABELTO, &ad); 31891da177e4SLinus Torvalds if (rc) 31901da177e4SLinus Torvalds return rc; 31911da177e4SLinus Torvalds 3192aa8e712cSStephen Smalley rc = security_validate_transition(&selinux_state, isec->sid, newsid, 3193aa8e712cSStephen Smalley sid, isec->sclass); 31941da177e4SLinus Torvalds if (rc) 31951da177e4SLinus Torvalds return rc; 31961da177e4SLinus Torvalds 31976b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 31986b6bc620SStephen Smalley newsid, 31991da177e4SLinus Torvalds sbsec->sid, 32001da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 32011da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, 32021da177e4SLinus Torvalds &ad); 32031da177e4SLinus Torvalds } 32041da177e4SLinus Torvalds 32058f0cfa52SDavid Howells static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name, 32068f0cfa52SDavid Howells const void *value, size_t size, 32078f0cfa52SDavid Howells int flags) 32081da177e4SLinus Torvalds { 3209c6f493d6SDavid Howells struct inode *inode = d_backing_inode(dentry); 321020cdef8dSPaul Moore struct inode_security_struct *isec; 32111da177e4SLinus Torvalds u32 newsid; 32121da177e4SLinus Torvalds int rc; 32131da177e4SLinus Torvalds 32141da177e4SLinus Torvalds if (strcmp(name, XATTR_NAME_SELINUX)) { 32151da177e4SLinus Torvalds /* Not an attribute we recognize, so nothing to do. */ 32161da177e4SLinus Torvalds return; 32171da177e4SLinus Torvalds } 32181da177e4SLinus Torvalds 3219aa8e712cSStephen Smalley rc = security_context_to_sid_force(&selinux_state, value, size, 3220aa8e712cSStephen Smalley &newsid); 32211da177e4SLinus Torvalds if (rc) { 3222c103a91eSpeter enderborg pr_err("SELinux: unable to map context to SID" 322312b29f34SStephen Smalley "for (%s, %lu), rc=%d\n", 322412b29f34SStephen Smalley inode->i_sb->s_id, inode->i_ino, -rc); 32251da177e4SLinus Torvalds return; 32261da177e4SLinus Torvalds } 32271da177e4SLinus Torvalds 322820cdef8dSPaul Moore isec = backing_inode_security(dentry); 32299287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 3230aa9c2669SDavid Quigley isec->sclass = inode_mode_to_security_class(inode->i_mode); 32311da177e4SLinus Torvalds isec->sid = newsid; 32326f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 32339287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 3234aa9c2669SDavid Quigley 32351da177e4SLinus Torvalds return; 32361da177e4SLinus Torvalds } 32371da177e4SLinus Torvalds 32388f0cfa52SDavid Howells static int selinux_inode_getxattr(struct dentry *dentry, const char *name) 32391da177e4SLinus Torvalds { 324088e67f3bSDavid Howells const struct cred *cred = current_cred(); 324188e67f3bSDavid Howells 32422875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__GETATTR); 32431da177e4SLinus Torvalds } 32441da177e4SLinus Torvalds 32451da177e4SLinus Torvalds static int selinux_inode_listxattr(struct dentry *dentry) 32461da177e4SLinus Torvalds { 324788e67f3bSDavid Howells const struct cred *cred = current_cred(); 324888e67f3bSDavid Howells 32492875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__GETATTR); 32501da177e4SLinus Torvalds } 32511da177e4SLinus Torvalds 32528f0cfa52SDavid Howells static int selinux_inode_removexattr(struct dentry *dentry, const char *name) 32531da177e4SLinus Torvalds { 32546b240306SEric W. Biederman if (strcmp(name, XATTR_NAME_SELINUX)) { 32556b240306SEric W. Biederman int rc = cap_inode_removexattr(dentry, name); 32566b240306SEric W. Biederman if (rc) 32576b240306SEric W. Biederman return rc; 32586b240306SEric W. Biederman 32596b240306SEric W. Biederman /* Not an attribute we recognize, so just check the 32606b240306SEric W. Biederman ordinary setattr permission. */ 32616b240306SEric W. Biederman return dentry_has_perm(current_cred(), dentry, FILE__SETATTR); 32626b240306SEric W. Biederman } 32631da177e4SLinus Torvalds 32641da177e4SLinus Torvalds /* No one is allowed to remove a SELinux security label. 32651da177e4SLinus Torvalds You can change the label, but all data must be labeled. */ 32661da177e4SLinus Torvalds return -EACCES; 32671da177e4SLinus Torvalds } 32681da177e4SLinus Torvalds 3269d381d8a9SJames Morris /* 3270abc69bb6SStephen Smalley * Copy the inode security context value to the user. 3271d381d8a9SJames Morris * 3272d381d8a9SJames Morris * Permission check is handled by selinux_inode_getxattr hook. 3273d381d8a9SJames Morris */ 3274ea861dfdSAndreas Gruenbacher static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc) 32751da177e4SLinus Torvalds { 327642492594SDavid P. Quigley u32 size; 327742492594SDavid P. Quigley int error; 327842492594SDavid P. Quigley char *context = NULL; 327920cdef8dSPaul Moore struct inode_security_struct *isec; 32801da177e4SLinus Torvalds 32818c8570fbSDustin Kirkland if (strcmp(name, XATTR_SELINUX_SUFFIX)) 32828c8570fbSDustin Kirkland return -EOPNOTSUPP; 32831da177e4SLinus Torvalds 3284abc69bb6SStephen Smalley /* 3285abc69bb6SStephen Smalley * If the caller has CAP_MAC_ADMIN, then get the raw context 3286abc69bb6SStephen Smalley * value even if it is not defined by current policy; otherwise, 3287abc69bb6SStephen Smalley * use the in-core value under current policy. 3288abc69bb6SStephen Smalley * Use the non-auditing forms of the permission checks since 3289abc69bb6SStephen Smalley * getxattr may be called by unprivileged processes commonly 3290abc69bb6SStephen Smalley * and lack of permission just means that we fall back to the 3291abc69bb6SStephen Smalley * in-core context value, not a denial. 3292abc69bb6SStephen Smalley */ 329320cdef8dSPaul Moore isec = inode_security(inode); 3294db59000aSStephen Smalley if (has_cap_mac_admin(false)) 3295aa8e712cSStephen Smalley error = security_sid_to_context_force(&selinux_state, 3296aa8e712cSStephen Smalley isec->sid, &context, 3297abc69bb6SStephen Smalley &size); 3298abc69bb6SStephen Smalley else 3299aa8e712cSStephen Smalley error = security_sid_to_context(&selinux_state, isec->sid, 3300aa8e712cSStephen Smalley &context, &size); 330142492594SDavid P. Quigley if (error) 330242492594SDavid P. Quigley return error; 330342492594SDavid P. Quigley error = size; 330442492594SDavid P. Quigley if (alloc) { 330542492594SDavid P. Quigley *buffer = context; 330642492594SDavid P. Quigley goto out_nofree; 330742492594SDavid P. Quigley } 330842492594SDavid P. Quigley kfree(context); 330942492594SDavid P. Quigley out_nofree: 331042492594SDavid P. Quigley return error; 33111da177e4SLinus Torvalds } 33121da177e4SLinus Torvalds 33131da177e4SLinus Torvalds static int selinux_inode_setsecurity(struct inode *inode, const char *name, 33141da177e4SLinus Torvalds const void *value, size_t size, int flags) 33151da177e4SLinus Torvalds { 33162c97165bSPaul Moore struct inode_security_struct *isec = inode_security_novalidate(inode); 33171da177e4SLinus Torvalds u32 newsid; 33181da177e4SLinus Torvalds int rc; 33191da177e4SLinus Torvalds 33201da177e4SLinus Torvalds if (strcmp(name, XATTR_SELINUX_SUFFIX)) 33211da177e4SLinus Torvalds return -EOPNOTSUPP; 33221da177e4SLinus Torvalds 33231da177e4SLinus Torvalds if (!value || !size) 33241da177e4SLinus Torvalds return -EACCES; 33251da177e4SLinus Torvalds 3326aa8e712cSStephen Smalley rc = security_context_to_sid(&selinux_state, value, size, &newsid, 3327aa8e712cSStephen Smalley GFP_KERNEL); 33281da177e4SLinus Torvalds if (rc) 33291da177e4SLinus Torvalds return rc; 33301da177e4SLinus Torvalds 33319287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 3332aa9c2669SDavid Quigley isec->sclass = inode_mode_to_security_class(inode->i_mode); 33331da177e4SLinus Torvalds isec->sid = newsid; 33346f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 33359287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 33361da177e4SLinus Torvalds return 0; 33371da177e4SLinus Torvalds } 33381da177e4SLinus Torvalds 33391da177e4SLinus Torvalds static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size) 33401da177e4SLinus Torvalds { 33411da177e4SLinus Torvalds const int len = sizeof(XATTR_NAME_SELINUX); 33421da177e4SLinus Torvalds if (buffer && len <= buffer_size) 33431da177e4SLinus Torvalds memcpy(buffer, XATTR_NAME_SELINUX, len); 33441da177e4SLinus Torvalds return len; 33451da177e4SLinus Torvalds } 33461da177e4SLinus Torvalds 3347d6335d77SAndreas Gruenbacher static void selinux_inode_getsecid(struct inode *inode, u32 *secid) 3348713a04aeSAhmed S. Darwish { 3349e817c2f3SAndreas Gruenbacher struct inode_security_struct *isec = inode_security_novalidate(inode); 3350713a04aeSAhmed S. Darwish *secid = isec->sid; 3351713a04aeSAhmed S. Darwish } 3352713a04aeSAhmed S. Darwish 335356909eb3SVivek Goyal static int selinux_inode_copy_up(struct dentry *src, struct cred **new) 335456909eb3SVivek Goyal { 335556909eb3SVivek Goyal u32 sid; 335656909eb3SVivek Goyal struct task_security_struct *tsec; 335756909eb3SVivek Goyal struct cred *new_creds = *new; 335856909eb3SVivek Goyal 335956909eb3SVivek Goyal if (new_creds == NULL) { 336056909eb3SVivek Goyal new_creds = prepare_creds(); 336156909eb3SVivek Goyal if (!new_creds) 336256909eb3SVivek Goyal return -ENOMEM; 336356909eb3SVivek Goyal } 336456909eb3SVivek Goyal 336556909eb3SVivek Goyal tsec = new_creds->security; 336656909eb3SVivek Goyal /* Get label from overlay inode and set it in create_sid */ 336756909eb3SVivek Goyal selinux_inode_getsecid(d_inode(src), &sid); 336856909eb3SVivek Goyal tsec->create_sid = sid; 336956909eb3SVivek Goyal *new = new_creds; 337056909eb3SVivek Goyal return 0; 337156909eb3SVivek Goyal } 337256909eb3SVivek Goyal 337319472b69SVivek Goyal static int selinux_inode_copy_up_xattr(const char *name) 337419472b69SVivek Goyal { 337519472b69SVivek Goyal /* The copy_up hook above sets the initial context on an inode, but we 337619472b69SVivek Goyal * don't then want to overwrite it by blindly copying all the lower 337719472b69SVivek Goyal * xattrs up. Instead, we have to filter out SELinux-related xattrs. 337819472b69SVivek Goyal */ 337919472b69SVivek Goyal if (strcmp(name, XATTR_NAME_SELINUX) == 0) 338019472b69SVivek Goyal return 1; /* Discard */ 338119472b69SVivek Goyal /* 338219472b69SVivek Goyal * Any other attribute apart from SELINUX is not claimed, supported 338319472b69SVivek Goyal * by selinux. 338419472b69SVivek Goyal */ 338519472b69SVivek Goyal return -EOPNOTSUPP; 338619472b69SVivek Goyal } 338719472b69SVivek Goyal 33881da177e4SLinus Torvalds /* file security operations */ 33891da177e4SLinus Torvalds 3390788e7dd4SYuichi Nakamura static int selinux_revalidate_file_permission(struct file *file, int mask) 33911da177e4SLinus Torvalds { 339288e67f3bSDavid Howells const struct cred *cred = current_cred(); 3393496ad9aaSAl Viro struct inode *inode = file_inode(file); 33941da177e4SLinus Torvalds 33951da177e4SLinus Torvalds /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */ 33961da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE)) 33971da177e4SLinus Torvalds mask |= MAY_APPEND; 33981da177e4SLinus Torvalds 3399389fb800SPaul Moore return file_has_perm(cred, file, 34001da177e4SLinus Torvalds file_mask_to_av(inode->i_mode, mask)); 34011da177e4SLinus Torvalds } 34021da177e4SLinus Torvalds 3403788e7dd4SYuichi Nakamura static int selinux_file_permission(struct file *file, int mask) 3404788e7dd4SYuichi Nakamura { 3405496ad9aaSAl Viro struct inode *inode = file_inode(file); 340620dda18bSStephen Smalley struct file_security_struct *fsec = file->f_security; 3407b197367eSAndreas Gruenbacher struct inode_security_struct *isec; 340820dda18bSStephen Smalley u32 sid = current_sid(); 340920dda18bSStephen Smalley 3410389fb800SPaul Moore if (!mask) 3411788e7dd4SYuichi Nakamura /* No permission to check. Existence test. */ 3412788e7dd4SYuichi Nakamura return 0; 3413788e7dd4SYuichi Nakamura 3414b197367eSAndreas Gruenbacher isec = inode_security(inode); 341520dda18bSStephen Smalley if (sid == fsec->sid && fsec->isid == isec->sid && 34166b6bc620SStephen Smalley fsec->pseqno == avc_policy_seqno(&selinux_state)) 341783d49856SEric Paris /* No change since file_open check. */ 341820dda18bSStephen Smalley return 0; 341920dda18bSStephen Smalley 3420788e7dd4SYuichi Nakamura return selinux_revalidate_file_permission(file, mask); 3421788e7dd4SYuichi Nakamura } 3422788e7dd4SYuichi Nakamura 34231da177e4SLinus Torvalds static int selinux_file_alloc_security(struct file *file) 34241da177e4SLinus Torvalds { 34251da177e4SLinus Torvalds return file_alloc_security(file); 34261da177e4SLinus Torvalds } 34271da177e4SLinus Torvalds 34281da177e4SLinus Torvalds static void selinux_file_free_security(struct file *file) 34291da177e4SLinus Torvalds { 34301da177e4SLinus Torvalds file_free_security(file); 34311da177e4SLinus Torvalds } 34321da177e4SLinus Torvalds 3433fa1aa143SJeff Vander Stoep /* 3434fa1aa143SJeff Vander Stoep * Check whether a task has the ioctl permission and cmd 3435fa1aa143SJeff Vander Stoep * operation to an inode. 3436fa1aa143SJeff Vander Stoep */ 34371d2a168aSGeliang Tang static int ioctl_has_perm(const struct cred *cred, struct file *file, 3438fa1aa143SJeff Vander Stoep u32 requested, u16 cmd) 3439fa1aa143SJeff Vander Stoep { 3440fa1aa143SJeff Vander Stoep struct common_audit_data ad; 3441fa1aa143SJeff Vander Stoep struct file_security_struct *fsec = file->f_security; 3442fa1aa143SJeff Vander Stoep struct inode *inode = file_inode(file); 344320cdef8dSPaul Moore struct inode_security_struct *isec; 3444fa1aa143SJeff Vander Stoep struct lsm_ioctlop_audit ioctl; 3445fa1aa143SJeff Vander Stoep u32 ssid = cred_sid(cred); 3446fa1aa143SJeff Vander Stoep int rc; 3447fa1aa143SJeff Vander Stoep u8 driver = cmd >> 8; 3448fa1aa143SJeff Vander Stoep u8 xperm = cmd & 0xff; 3449fa1aa143SJeff Vander Stoep 3450fa1aa143SJeff Vander Stoep ad.type = LSM_AUDIT_DATA_IOCTL_OP; 3451fa1aa143SJeff Vander Stoep ad.u.op = &ioctl; 3452fa1aa143SJeff Vander Stoep ad.u.op->cmd = cmd; 3453fa1aa143SJeff Vander Stoep ad.u.op->path = file->f_path; 3454fa1aa143SJeff Vander Stoep 3455fa1aa143SJeff Vander Stoep if (ssid != fsec->sid) { 34566b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 34576b6bc620SStephen Smalley ssid, fsec->sid, 3458fa1aa143SJeff Vander Stoep SECCLASS_FD, 3459fa1aa143SJeff Vander Stoep FD__USE, 3460fa1aa143SJeff Vander Stoep &ad); 3461fa1aa143SJeff Vander Stoep if (rc) 3462fa1aa143SJeff Vander Stoep goto out; 3463fa1aa143SJeff Vander Stoep } 3464fa1aa143SJeff Vander Stoep 3465fa1aa143SJeff Vander Stoep if (unlikely(IS_PRIVATE(inode))) 3466fa1aa143SJeff Vander Stoep return 0; 3467fa1aa143SJeff Vander Stoep 346820cdef8dSPaul Moore isec = inode_security(inode); 34696b6bc620SStephen Smalley rc = avc_has_extended_perms(&selinux_state, 34706b6bc620SStephen Smalley ssid, isec->sid, isec->sclass, 3471fa1aa143SJeff Vander Stoep requested, driver, xperm, &ad); 3472fa1aa143SJeff Vander Stoep out: 3473fa1aa143SJeff Vander Stoep return rc; 3474fa1aa143SJeff Vander Stoep } 3475fa1aa143SJeff Vander Stoep 34761da177e4SLinus Torvalds static int selinux_file_ioctl(struct file *file, unsigned int cmd, 34771da177e4SLinus Torvalds unsigned long arg) 34781da177e4SLinus Torvalds { 347988e67f3bSDavid Howells const struct cred *cred = current_cred(); 34800b24dcb7SEric Paris int error = 0; 34811da177e4SLinus Torvalds 34820b24dcb7SEric Paris switch (cmd) { 34830b24dcb7SEric Paris case FIONREAD: 34840b24dcb7SEric Paris /* fall through */ 34850b24dcb7SEric Paris case FIBMAP: 34860b24dcb7SEric Paris /* fall through */ 34870b24dcb7SEric Paris case FIGETBSZ: 34880b24dcb7SEric Paris /* fall through */ 34892f99c369SAl Viro case FS_IOC_GETFLAGS: 34900b24dcb7SEric Paris /* fall through */ 34912f99c369SAl Viro case FS_IOC_GETVERSION: 34920b24dcb7SEric Paris error = file_has_perm(cred, file, FILE__GETATTR); 34930b24dcb7SEric Paris break; 34941da177e4SLinus Torvalds 34952f99c369SAl Viro case FS_IOC_SETFLAGS: 34960b24dcb7SEric Paris /* fall through */ 34972f99c369SAl Viro case FS_IOC_SETVERSION: 34980b24dcb7SEric Paris error = file_has_perm(cred, file, FILE__SETATTR); 34990b24dcb7SEric Paris break; 35000b24dcb7SEric Paris 35010b24dcb7SEric Paris /* sys_ioctl() checks */ 35020b24dcb7SEric Paris case FIONBIO: 35030b24dcb7SEric Paris /* fall through */ 35040b24dcb7SEric Paris case FIOASYNC: 35050b24dcb7SEric Paris error = file_has_perm(cred, file, 0); 35060b24dcb7SEric Paris break; 35070b24dcb7SEric Paris 35080b24dcb7SEric Paris case KDSKBENT: 35090b24dcb7SEric Paris case KDSKBSENT: 35106a9de491SEric Paris error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG, 35118e4ff6f2SStephen Smalley SECURITY_CAP_AUDIT, true); 35120b24dcb7SEric Paris break; 35130b24dcb7SEric Paris 35140b24dcb7SEric Paris /* default case assumes that the command will go 35150b24dcb7SEric Paris * to the file's ioctl() function. 35160b24dcb7SEric Paris */ 35170b24dcb7SEric Paris default: 3518fa1aa143SJeff Vander Stoep error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd); 35190b24dcb7SEric Paris } 35200b24dcb7SEric Paris return error; 35211da177e4SLinus Torvalds } 35221da177e4SLinus Torvalds 3523fcaaade1SStephen Smalley static int default_noexec; 3524fcaaade1SStephen Smalley 35251da177e4SLinus Torvalds static int file_map_prot_check(struct file *file, unsigned long prot, int shared) 35261da177e4SLinus Torvalds { 352788e67f3bSDavid Howells const struct cred *cred = current_cred(); 3528be0554c9SStephen Smalley u32 sid = cred_sid(cred); 3529d84f4f99SDavid Howells int rc = 0; 353088e67f3bSDavid Howells 3531fcaaade1SStephen Smalley if (default_noexec && 3532892e8cacSStephen Smalley (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) || 3533892e8cacSStephen Smalley (!shared && (prot & PROT_WRITE)))) { 35341da177e4SLinus Torvalds /* 35351da177e4SLinus Torvalds * We are making executable an anonymous mapping or a 35361da177e4SLinus Torvalds * private file mapping that will also be writable. 35371da177e4SLinus Torvalds * This has an additional check. 35381da177e4SLinus Torvalds */ 35396b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 35406b6bc620SStephen Smalley sid, sid, SECCLASS_PROCESS, 3541be0554c9SStephen Smalley PROCESS__EXECMEM, NULL); 35421da177e4SLinus Torvalds if (rc) 3543d84f4f99SDavid Howells goto error; 35441da177e4SLinus Torvalds } 35451da177e4SLinus Torvalds 35461da177e4SLinus Torvalds if (file) { 35471da177e4SLinus Torvalds /* read access is always possible with a mapping */ 35481da177e4SLinus Torvalds u32 av = FILE__READ; 35491da177e4SLinus Torvalds 35501da177e4SLinus Torvalds /* write access only matters if the mapping is shared */ 35511da177e4SLinus Torvalds if (shared && (prot & PROT_WRITE)) 35521da177e4SLinus Torvalds av |= FILE__WRITE; 35531da177e4SLinus Torvalds 35541da177e4SLinus Torvalds if (prot & PROT_EXEC) 35551da177e4SLinus Torvalds av |= FILE__EXECUTE; 35561da177e4SLinus Torvalds 355788e67f3bSDavid Howells return file_has_perm(cred, file, av); 35581da177e4SLinus Torvalds } 3559d84f4f99SDavid Howells 3560d84f4f99SDavid Howells error: 3561d84f4f99SDavid Howells return rc; 35621da177e4SLinus Torvalds } 35631da177e4SLinus Torvalds 3564e5467859SAl Viro static int selinux_mmap_addr(unsigned long addr) 35651da177e4SLinus Torvalds { 3566b1d9e6b0SCasey Schaufler int rc = 0; 356798883bfdSPaul Moore 356898883bfdSPaul Moore if (addr < CONFIG_LSM_MMAP_MIN_ADDR) { 356998883bfdSPaul Moore u32 sid = current_sid(); 35706b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 35716b6bc620SStephen Smalley sid, sid, SECCLASS_MEMPROTECT, 357298883bfdSPaul Moore MEMPROTECT__MMAP_ZERO, NULL); 357398883bfdSPaul Moore } 357498883bfdSPaul Moore 357598883bfdSPaul Moore return rc; 3576e5467859SAl Viro } 35771da177e4SLinus Torvalds 3578e5467859SAl Viro static int selinux_mmap_file(struct file *file, unsigned long reqprot, 3579e5467859SAl Viro unsigned long prot, unsigned long flags) 3580e5467859SAl Viro { 35813ba4bf5fSStephen Smalley struct common_audit_data ad; 35823ba4bf5fSStephen Smalley int rc; 35833ba4bf5fSStephen Smalley 35843ba4bf5fSStephen Smalley if (file) { 35853ba4bf5fSStephen Smalley ad.type = LSM_AUDIT_DATA_FILE; 35863ba4bf5fSStephen Smalley ad.u.file = file; 35873ba4bf5fSStephen Smalley rc = inode_has_perm(current_cred(), file_inode(file), 35883ba4bf5fSStephen Smalley FILE__MAP, &ad); 35893ba4bf5fSStephen Smalley if (rc) 35903ba4bf5fSStephen Smalley return rc; 35913ba4bf5fSStephen Smalley } 35923ba4bf5fSStephen Smalley 3593aa8e712cSStephen Smalley if (selinux_state.checkreqprot) 35941da177e4SLinus Torvalds prot = reqprot; 35951da177e4SLinus Torvalds 35961da177e4SLinus Torvalds return file_map_prot_check(file, prot, 35971da177e4SLinus Torvalds (flags & MAP_TYPE) == MAP_SHARED); 35981da177e4SLinus Torvalds } 35991da177e4SLinus Torvalds 36001da177e4SLinus Torvalds static int selinux_file_mprotect(struct vm_area_struct *vma, 36011da177e4SLinus Torvalds unsigned long reqprot, 36021da177e4SLinus Torvalds unsigned long prot) 36031da177e4SLinus Torvalds { 360488e67f3bSDavid Howells const struct cred *cred = current_cred(); 3605be0554c9SStephen Smalley u32 sid = cred_sid(cred); 36061da177e4SLinus Torvalds 3607aa8e712cSStephen Smalley if (selinux_state.checkreqprot) 36081da177e4SLinus Torvalds prot = reqprot; 36091da177e4SLinus Torvalds 3610fcaaade1SStephen Smalley if (default_noexec && 3611fcaaade1SStephen Smalley (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) { 3612d541bbeeSJames Morris int rc = 0; 3613db4c9641SStephen Smalley if (vma->vm_start >= vma->vm_mm->start_brk && 3614db4c9641SStephen Smalley vma->vm_end <= vma->vm_mm->brk) { 36156b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 36166b6bc620SStephen Smalley sid, sid, SECCLASS_PROCESS, 3617be0554c9SStephen Smalley PROCESS__EXECHEAP, NULL); 3618db4c9641SStephen Smalley } else if (!vma->vm_file && 3619c2316dbfSStephen Smalley ((vma->vm_start <= vma->vm_mm->start_stack && 3620c2316dbfSStephen Smalley vma->vm_end >= vma->vm_mm->start_stack) || 3621d17af505SAndy Lutomirski vma_is_stack_for_current(vma))) { 36226b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 36236b6bc620SStephen Smalley sid, sid, SECCLASS_PROCESS, 3624be0554c9SStephen Smalley PROCESS__EXECSTACK, NULL); 3625db4c9641SStephen Smalley } else if (vma->vm_file && vma->anon_vma) { 3626db4c9641SStephen Smalley /* 3627db4c9641SStephen Smalley * We are making executable a file mapping that has 3628db4c9641SStephen Smalley * had some COW done. Since pages might have been 3629db4c9641SStephen Smalley * written, check ability to execute the possibly 3630db4c9641SStephen Smalley * modified content. This typically should only 3631db4c9641SStephen Smalley * occur for text relocations. 3632db4c9641SStephen Smalley */ 3633d84f4f99SDavid Howells rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD); 3634db4c9641SStephen Smalley } 36356b992197SLorenzo Hernandez García-Hierro if (rc) 36366b992197SLorenzo Hernandez García-Hierro return rc; 36376b992197SLorenzo Hernandez García-Hierro } 36381da177e4SLinus Torvalds 36391da177e4SLinus Torvalds return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED); 36401da177e4SLinus Torvalds } 36411da177e4SLinus Torvalds 36421da177e4SLinus Torvalds static int selinux_file_lock(struct file *file, unsigned int cmd) 36431da177e4SLinus Torvalds { 364488e67f3bSDavid Howells const struct cred *cred = current_cred(); 364588e67f3bSDavid Howells 364688e67f3bSDavid Howells return file_has_perm(cred, file, FILE__LOCK); 36471da177e4SLinus Torvalds } 36481da177e4SLinus Torvalds 36491da177e4SLinus Torvalds static int selinux_file_fcntl(struct file *file, unsigned int cmd, 36501da177e4SLinus Torvalds unsigned long arg) 36511da177e4SLinus Torvalds { 365288e67f3bSDavid Howells const struct cred *cred = current_cred(); 36531da177e4SLinus Torvalds int err = 0; 36541da177e4SLinus Torvalds 36551da177e4SLinus Torvalds switch (cmd) { 36561da177e4SLinus Torvalds case F_SETFL: 36571da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) { 365888e67f3bSDavid Howells err = file_has_perm(cred, file, FILE__WRITE); 36591da177e4SLinus Torvalds break; 36601da177e4SLinus Torvalds } 36611da177e4SLinus Torvalds /* fall through */ 36621da177e4SLinus Torvalds case F_SETOWN: 36631da177e4SLinus Torvalds case F_SETSIG: 36641da177e4SLinus Torvalds case F_GETFL: 36651da177e4SLinus Torvalds case F_GETOWN: 36661da177e4SLinus Torvalds case F_GETSIG: 36671d151c33SCyrill Gorcunov case F_GETOWNER_UIDS: 36681da177e4SLinus Torvalds /* Just check FD__USE permission */ 366988e67f3bSDavid Howells err = file_has_perm(cred, file, 0); 36701da177e4SLinus Torvalds break; 36711da177e4SLinus Torvalds case F_GETLK: 36721da177e4SLinus Torvalds case F_SETLK: 36731da177e4SLinus Torvalds case F_SETLKW: 36740d3f7a2dSJeff Layton case F_OFD_GETLK: 36750d3f7a2dSJeff Layton case F_OFD_SETLK: 36760d3f7a2dSJeff Layton case F_OFD_SETLKW: 36771da177e4SLinus Torvalds #if BITS_PER_LONG == 32 36781da177e4SLinus Torvalds case F_GETLK64: 36791da177e4SLinus Torvalds case F_SETLK64: 36801da177e4SLinus Torvalds case F_SETLKW64: 36811da177e4SLinus Torvalds #endif 368288e67f3bSDavid Howells err = file_has_perm(cred, file, FILE__LOCK); 36831da177e4SLinus Torvalds break; 36841da177e4SLinus Torvalds } 36851da177e4SLinus Torvalds 36861da177e4SLinus Torvalds return err; 36871da177e4SLinus Torvalds } 36881da177e4SLinus Torvalds 3689e0b93eddSJeff Layton static void selinux_file_set_fowner(struct file *file) 36901da177e4SLinus Torvalds { 36911da177e4SLinus Torvalds struct file_security_struct *fsec; 36921da177e4SLinus Torvalds 36931da177e4SLinus Torvalds fsec = file->f_security; 3694275bb41eSDavid Howells fsec->fown_sid = current_sid(); 36951da177e4SLinus Torvalds } 36961da177e4SLinus Torvalds 36971da177e4SLinus Torvalds static int selinux_file_send_sigiotask(struct task_struct *tsk, 36981da177e4SLinus Torvalds struct fown_struct *fown, int signum) 36991da177e4SLinus Torvalds { 37001da177e4SLinus Torvalds struct file *file; 370165c90bcaSStephen Smalley u32 sid = task_sid(tsk); 37021da177e4SLinus Torvalds u32 perm; 37031da177e4SLinus Torvalds struct file_security_struct *fsec; 37041da177e4SLinus Torvalds 37051da177e4SLinus Torvalds /* struct fown_struct is never outside the context of a struct file */ 3706b385a144SRobert P. J. Day file = container_of(fown, struct file, f_owner); 37071da177e4SLinus Torvalds 37081da177e4SLinus Torvalds fsec = file->f_security; 37091da177e4SLinus Torvalds 37101da177e4SLinus Torvalds if (!signum) 37111da177e4SLinus Torvalds perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */ 37121da177e4SLinus Torvalds else 37131da177e4SLinus Torvalds perm = signal_to_av(signum); 37141da177e4SLinus Torvalds 37156b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 37166b6bc620SStephen Smalley fsec->fown_sid, sid, 37171da177e4SLinus Torvalds SECCLASS_PROCESS, perm, NULL); 37181da177e4SLinus Torvalds } 37191da177e4SLinus Torvalds 37201da177e4SLinus Torvalds static int selinux_file_receive(struct file *file) 37211da177e4SLinus Torvalds { 372288e67f3bSDavid Howells const struct cred *cred = current_cred(); 372388e67f3bSDavid Howells 372488e67f3bSDavid Howells return file_has_perm(cred, file, file_to_av(file)); 37251da177e4SLinus Torvalds } 37261da177e4SLinus Torvalds 372794817692SAl Viro static int selinux_file_open(struct file *file) 3728788e7dd4SYuichi Nakamura { 3729788e7dd4SYuichi Nakamura struct file_security_struct *fsec; 3730788e7dd4SYuichi Nakamura struct inode_security_struct *isec; 3731d84f4f99SDavid Howells 3732788e7dd4SYuichi Nakamura fsec = file->f_security; 373383da53c5SAndreas Gruenbacher isec = inode_security(file_inode(file)); 3734788e7dd4SYuichi Nakamura /* 3735788e7dd4SYuichi Nakamura * Save inode label and policy sequence number 3736788e7dd4SYuichi Nakamura * at open-time so that selinux_file_permission 3737788e7dd4SYuichi Nakamura * can determine whether revalidation is necessary. 3738788e7dd4SYuichi Nakamura * Task label is already saved in the file security 3739788e7dd4SYuichi Nakamura * struct as its SID. 3740788e7dd4SYuichi Nakamura */ 3741788e7dd4SYuichi Nakamura fsec->isid = isec->sid; 37426b6bc620SStephen Smalley fsec->pseqno = avc_policy_seqno(&selinux_state); 3743788e7dd4SYuichi Nakamura /* 3744788e7dd4SYuichi Nakamura * Since the inode label or policy seqno may have changed 3745788e7dd4SYuichi Nakamura * between the selinux_inode_permission check and the saving 3746788e7dd4SYuichi Nakamura * of state above, recheck that access is still permitted. 3747788e7dd4SYuichi Nakamura * Otherwise, access might never be revalidated against the 3748788e7dd4SYuichi Nakamura * new inode label or new policy. 3749788e7dd4SYuichi Nakamura * This check is not redundant - do not remove. 3750788e7dd4SYuichi Nakamura */ 375194817692SAl Viro return file_path_has_perm(file->f_cred, file, open_file_to_av(file)); 3752788e7dd4SYuichi Nakamura } 3753788e7dd4SYuichi Nakamura 37541da177e4SLinus Torvalds /* task security operations */ 37551da177e4SLinus Torvalds 3756a79be238STetsuo Handa static int selinux_task_alloc(struct task_struct *task, 3757a79be238STetsuo Handa unsigned long clone_flags) 37581da177e4SLinus Torvalds { 3759be0554c9SStephen Smalley u32 sid = current_sid(); 3760be0554c9SStephen Smalley 37616b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 37626b6bc620SStephen Smalley sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL); 37631da177e4SLinus Torvalds } 37641da177e4SLinus Torvalds 3765f1752eecSDavid Howells /* 3766ee18d64cSDavid Howells * allocate the SELinux part of blank credentials 3767ee18d64cSDavid Howells */ 3768ee18d64cSDavid Howells static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp) 3769ee18d64cSDavid Howells { 3770ee18d64cSDavid Howells struct task_security_struct *tsec; 3771ee18d64cSDavid Howells 3772ee18d64cSDavid Howells tsec = kzalloc(sizeof(struct task_security_struct), gfp); 3773ee18d64cSDavid Howells if (!tsec) 3774ee18d64cSDavid Howells return -ENOMEM; 3775ee18d64cSDavid Howells 3776ee18d64cSDavid Howells cred->security = tsec; 3777ee18d64cSDavid Howells return 0; 3778ee18d64cSDavid Howells } 3779ee18d64cSDavid Howells 3780ee18d64cSDavid Howells /* 3781f1752eecSDavid Howells * detach and free the LSM part of a set of credentials 3782f1752eecSDavid Howells */ 3783f1752eecSDavid Howells static void selinux_cred_free(struct cred *cred) 37841da177e4SLinus Torvalds { 3785f1752eecSDavid Howells struct task_security_struct *tsec = cred->security; 3786e0e81739SDavid Howells 37872edeaa34STetsuo Handa /* 37882edeaa34STetsuo Handa * cred->security == NULL if security_cred_alloc_blank() or 37892edeaa34STetsuo Handa * security_prepare_creds() returned an error. 37902edeaa34STetsuo Handa */ 37912edeaa34STetsuo Handa BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE); 3792e0e81739SDavid Howells cred->security = (void *) 0x7UL; 3793f1752eecSDavid Howells kfree(tsec); 37941da177e4SLinus Torvalds } 37951da177e4SLinus Torvalds 3796d84f4f99SDavid Howells /* 3797d84f4f99SDavid Howells * prepare a new set of credentials for modification 3798d84f4f99SDavid Howells */ 3799d84f4f99SDavid Howells static int selinux_cred_prepare(struct cred *new, const struct cred *old, 3800d84f4f99SDavid Howells gfp_t gfp) 3801d84f4f99SDavid Howells { 3802d84f4f99SDavid Howells const struct task_security_struct *old_tsec; 3803d84f4f99SDavid Howells struct task_security_struct *tsec; 3804d84f4f99SDavid Howells 3805d84f4f99SDavid Howells old_tsec = old->security; 3806d84f4f99SDavid Howells 3807d84f4f99SDavid Howells tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp); 3808d84f4f99SDavid Howells if (!tsec) 3809d84f4f99SDavid Howells return -ENOMEM; 3810d84f4f99SDavid Howells 3811d84f4f99SDavid Howells new->security = tsec; 3812d84f4f99SDavid Howells return 0; 3813d84f4f99SDavid Howells } 3814d84f4f99SDavid Howells 3815d84f4f99SDavid Howells /* 3816ee18d64cSDavid Howells * transfer the SELinux data to a blank set of creds 3817ee18d64cSDavid Howells */ 3818ee18d64cSDavid Howells static void selinux_cred_transfer(struct cred *new, const struct cred *old) 3819ee18d64cSDavid Howells { 3820ee18d64cSDavid Howells const struct task_security_struct *old_tsec = old->security; 3821ee18d64cSDavid Howells struct task_security_struct *tsec = new->security; 3822ee18d64cSDavid Howells 3823ee18d64cSDavid Howells *tsec = *old_tsec; 3824ee18d64cSDavid Howells } 3825ee18d64cSDavid Howells 38263ec30113SMatthew Garrett static void selinux_cred_getsecid(const struct cred *c, u32 *secid) 38273ec30113SMatthew Garrett { 38283ec30113SMatthew Garrett *secid = cred_sid(c); 38293ec30113SMatthew Garrett } 38303ec30113SMatthew Garrett 3831ee18d64cSDavid Howells /* 38323a3b7ce9SDavid Howells * set the security data for a kernel service 38333a3b7ce9SDavid Howells * - all the creation contexts are set to unlabelled 38343a3b7ce9SDavid Howells */ 38353a3b7ce9SDavid Howells static int selinux_kernel_act_as(struct cred *new, u32 secid) 38363a3b7ce9SDavid Howells { 38373a3b7ce9SDavid Howells struct task_security_struct *tsec = new->security; 38383a3b7ce9SDavid Howells u32 sid = current_sid(); 38393a3b7ce9SDavid Howells int ret; 38403a3b7ce9SDavid Howells 38416b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 38426b6bc620SStephen Smalley sid, secid, 38433a3b7ce9SDavid Howells SECCLASS_KERNEL_SERVICE, 38443a3b7ce9SDavid Howells KERNEL_SERVICE__USE_AS_OVERRIDE, 38453a3b7ce9SDavid Howells NULL); 38463a3b7ce9SDavid Howells if (ret == 0) { 38473a3b7ce9SDavid Howells tsec->sid = secid; 38483a3b7ce9SDavid Howells tsec->create_sid = 0; 38493a3b7ce9SDavid Howells tsec->keycreate_sid = 0; 38503a3b7ce9SDavid Howells tsec->sockcreate_sid = 0; 38513a3b7ce9SDavid Howells } 38523a3b7ce9SDavid Howells return ret; 38533a3b7ce9SDavid Howells } 38543a3b7ce9SDavid Howells 38553a3b7ce9SDavid Howells /* 38563a3b7ce9SDavid Howells * set the file creation context in a security record to the same as the 38573a3b7ce9SDavid Howells * objective context of the specified inode 38583a3b7ce9SDavid Howells */ 38593a3b7ce9SDavid Howells static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode) 38603a3b7ce9SDavid Howells { 386183da53c5SAndreas Gruenbacher struct inode_security_struct *isec = inode_security(inode); 38623a3b7ce9SDavid Howells struct task_security_struct *tsec = new->security; 38633a3b7ce9SDavid Howells u32 sid = current_sid(); 38643a3b7ce9SDavid Howells int ret; 38653a3b7ce9SDavid Howells 38666b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 38676b6bc620SStephen Smalley sid, isec->sid, 38683a3b7ce9SDavid Howells SECCLASS_KERNEL_SERVICE, 38693a3b7ce9SDavid Howells KERNEL_SERVICE__CREATE_FILES_AS, 38703a3b7ce9SDavid Howells NULL); 38713a3b7ce9SDavid Howells 38723a3b7ce9SDavid Howells if (ret == 0) 38733a3b7ce9SDavid Howells tsec->create_sid = isec->sid; 3874ef57471aSDavid Howells return ret; 38753a3b7ce9SDavid Howells } 38763a3b7ce9SDavid Howells 3877dd8dbf2eSEric Paris static int selinux_kernel_module_request(char *kmod_name) 387825354c4fSEric Paris { 3879dd8dbf2eSEric Paris struct common_audit_data ad; 3880dd8dbf2eSEric Paris 388150c205f5SEric Paris ad.type = LSM_AUDIT_DATA_KMOD; 3882dd8dbf2eSEric Paris ad.u.kmod_name = kmod_name; 3883dd8dbf2eSEric Paris 38846b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 38856b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM, 3886dd8dbf2eSEric Paris SYSTEM__MODULE_REQUEST, &ad); 388725354c4fSEric Paris } 388825354c4fSEric Paris 388961d612eaSJeff Vander Stoep static int selinux_kernel_module_from_file(struct file *file) 389061d612eaSJeff Vander Stoep { 389161d612eaSJeff Vander Stoep struct common_audit_data ad; 389261d612eaSJeff Vander Stoep struct inode_security_struct *isec; 389361d612eaSJeff Vander Stoep struct file_security_struct *fsec; 389461d612eaSJeff Vander Stoep u32 sid = current_sid(); 389561d612eaSJeff Vander Stoep int rc; 389661d612eaSJeff Vander Stoep 389761d612eaSJeff Vander Stoep /* init_module */ 389861d612eaSJeff Vander Stoep if (file == NULL) 38996b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 39006b6bc620SStephen Smalley sid, sid, SECCLASS_SYSTEM, 390161d612eaSJeff Vander Stoep SYSTEM__MODULE_LOAD, NULL); 390261d612eaSJeff Vander Stoep 390361d612eaSJeff Vander Stoep /* finit_module */ 390420cdef8dSPaul Moore 390543af5de7SVivek Goyal ad.type = LSM_AUDIT_DATA_FILE; 390643af5de7SVivek Goyal ad.u.file = file; 390761d612eaSJeff Vander Stoep 390861d612eaSJeff Vander Stoep fsec = file->f_security; 390961d612eaSJeff Vander Stoep if (sid != fsec->sid) { 39106b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 39116b6bc620SStephen Smalley sid, fsec->sid, SECCLASS_FD, FD__USE, &ad); 391261d612eaSJeff Vander Stoep if (rc) 391361d612eaSJeff Vander Stoep return rc; 391461d612eaSJeff Vander Stoep } 391561d612eaSJeff Vander Stoep 391620cdef8dSPaul Moore isec = inode_security(file_inode(file)); 39176b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 39186b6bc620SStephen Smalley sid, isec->sid, SECCLASS_SYSTEM, 391961d612eaSJeff Vander Stoep SYSTEM__MODULE_LOAD, &ad); 392061d612eaSJeff Vander Stoep } 392161d612eaSJeff Vander Stoep 392261d612eaSJeff Vander Stoep static int selinux_kernel_read_file(struct file *file, 392361d612eaSJeff Vander Stoep enum kernel_read_file_id id) 392461d612eaSJeff Vander Stoep { 392561d612eaSJeff Vander Stoep int rc = 0; 392661d612eaSJeff Vander Stoep 392761d612eaSJeff Vander Stoep switch (id) { 392861d612eaSJeff Vander Stoep case READING_MODULE: 392961d612eaSJeff Vander Stoep rc = selinux_kernel_module_from_file(file); 393061d612eaSJeff Vander Stoep break; 393161d612eaSJeff Vander Stoep default: 393261d612eaSJeff Vander Stoep break; 393361d612eaSJeff Vander Stoep } 393461d612eaSJeff Vander Stoep 393561d612eaSJeff Vander Stoep return rc; 393661d612eaSJeff Vander Stoep } 393761d612eaSJeff Vander Stoep 3938c77b8cdfSMimi Zohar static int selinux_kernel_load_data(enum kernel_load_data_id id) 3939c77b8cdfSMimi Zohar { 3940c77b8cdfSMimi Zohar int rc = 0; 3941c77b8cdfSMimi Zohar 3942c77b8cdfSMimi Zohar switch (id) { 3943c77b8cdfSMimi Zohar case LOADING_MODULE: 3944c77b8cdfSMimi Zohar rc = selinux_kernel_module_from_file(NULL); 3945c77b8cdfSMimi Zohar default: 3946c77b8cdfSMimi Zohar break; 3947c77b8cdfSMimi Zohar } 3948c77b8cdfSMimi Zohar 3949c77b8cdfSMimi Zohar return rc; 3950c77b8cdfSMimi Zohar } 3951c77b8cdfSMimi Zohar 39521da177e4SLinus Torvalds static int selinux_task_setpgid(struct task_struct *p, pid_t pgid) 39531da177e4SLinus Torvalds { 39546b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 39556b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 3956be0554c9SStephen Smalley PROCESS__SETPGID, NULL); 39571da177e4SLinus Torvalds } 39581da177e4SLinus Torvalds 39591da177e4SLinus Torvalds static int selinux_task_getpgid(struct task_struct *p) 39601da177e4SLinus Torvalds { 39616b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 39626b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 3963be0554c9SStephen Smalley PROCESS__GETPGID, NULL); 39641da177e4SLinus Torvalds } 39651da177e4SLinus Torvalds 39661da177e4SLinus Torvalds static int selinux_task_getsid(struct task_struct *p) 39671da177e4SLinus Torvalds { 39686b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 39696b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 3970be0554c9SStephen Smalley PROCESS__GETSESSION, NULL); 39711da177e4SLinus Torvalds } 39721da177e4SLinus Torvalds 3973f9008e4cSDavid Quigley static void selinux_task_getsecid(struct task_struct *p, u32 *secid) 3974f9008e4cSDavid Quigley { 3975275bb41eSDavid Howells *secid = task_sid(p); 3976f9008e4cSDavid Quigley } 3977f9008e4cSDavid Quigley 39781da177e4SLinus Torvalds static int selinux_task_setnice(struct task_struct *p, int nice) 39791da177e4SLinus Torvalds { 39806b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 39816b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 3982be0554c9SStephen Smalley PROCESS__SETSCHED, NULL); 39831da177e4SLinus Torvalds } 39841da177e4SLinus Torvalds 398503e68060SJames Morris static int selinux_task_setioprio(struct task_struct *p, int ioprio) 398603e68060SJames Morris { 39876b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 39886b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 3989be0554c9SStephen Smalley PROCESS__SETSCHED, NULL); 399003e68060SJames Morris } 399103e68060SJames Morris 3992a1836a42SDavid Quigley static int selinux_task_getioprio(struct task_struct *p) 3993a1836a42SDavid Quigley { 39946b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 39956b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 3996be0554c9SStephen Smalley PROCESS__GETSCHED, NULL); 3997a1836a42SDavid Quigley } 3998a1836a42SDavid Quigley 39994298555dSCorentin LABBE static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred, 4000791ec491SStephen Smalley unsigned int flags) 4001791ec491SStephen Smalley { 4002791ec491SStephen Smalley u32 av = 0; 4003791ec491SStephen Smalley 400484e6885eSStephen Smalley if (!flags) 400584e6885eSStephen Smalley return 0; 4006791ec491SStephen Smalley if (flags & LSM_PRLIMIT_WRITE) 4007791ec491SStephen Smalley av |= PROCESS__SETRLIMIT; 4008791ec491SStephen Smalley if (flags & LSM_PRLIMIT_READ) 4009791ec491SStephen Smalley av |= PROCESS__GETRLIMIT; 40106b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 40116b6bc620SStephen Smalley cred_sid(cred), cred_sid(tcred), 4012791ec491SStephen Smalley SECCLASS_PROCESS, av, NULL); 4013791ec491SStephen Smalley } 4014791ec491SStephen Smalley 40158fd00b4dSJiri Slaby static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource, 40168fd00b4dSJiri Slaby struct rlimit *new_rlim) 40171da177e4SLinus Torvalds { 40188fd00b4dSJiri Slaby struct rlimit *old_rlim = p->signal->rlim + resource; 40191da177e4SLinus Torvalds 40201da177e4SLinus Torvalds /* Control the ability to change the hard limit (whether 40211da177e4SLinus Torvalds lowering or raising it), so that the hard limit can 40221da177e4SLinus Torvalds later be used as a safe reset point for the soft limit 4023d84f4f99SDavid Howells upon context transitions. See selinux_bprm_committing_creds. */ 40241da177e4SLinus Torvalds if (old_rlim->rlim_max != new_rlim->rlim_max) 40256b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 40266b6bc620SStephen Smalley current_sid(), task_sid(p), 4027be0554c9SStephen Smalley SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL); 40281da177e4SLinus Torvalds 40291da177e4SLinus Torvalds return 0; 40301da177e4SLinus Torvalds } 40311da177e4SLinus Torvalds 4032b0ae1981SKOSAKI Motohiro static int selinux_task_setscheduler(struct task_struct *p) 40331da177e4SLinus Torvalds { 40346b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 40356b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 4036be0554c9SStephen Smalley PROCESS__SETSCHED, NULL); 40371da177e4SLinus Torvalds } 40381da177e4SLinus Torvalds 40391da177e4SLinus Torvalds static int selinux_task_getscheduler(struct task_struct *p) 40401da177e4SLinus Torvalds { 40416b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 40426b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 4043be0554c9SStephen Smalley PROCESS__GETSCHED, NULL); 40441da177e4SLinus Torvalds } 40451da177e4SLinus Torvalds 404635601547SDavid Quigley static int selinux_task_movememory(struct task_struct *p) 404735601547SDavid Quigley { 40486b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 40496b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 4050be0554c9SStephen Smalley PROCESS__SETSCHED, NULL); 405135601547SDavid Quigley } 405235601547SDavid Quigley 4053ae7795bcSEric W. Biederman static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info, 40546b4f3d01SStephen Smalley int sig, const struct cred *cred) 40551da177e4SLinus Torvalds { 40566b4f3d01SStephen Smalley u32 secid; 40571da177e4SLinus Torvalds u32 perm; 40581da177e4SLinus Torvalds 40591da177e4SLinus Torvalds if (!sig) 40601da177e4SLinus Torvalds perm = PROCESS__SIGNULL; /* null signal; existence test */ 40611da177e4SLinus Torvalds else 40621da177e4SLinus Torvalds perm = signal_to_av(sig); 40636b4f3d01SStephen Smalley if (!cred) 4064be0554c9SStephen Smalley secid = current_sid(); 40656b4f3d01SStephen Smalley else 40666b4f3d01SStephen Smalley secid = cred_sid(cred); 40676b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 40686b6bc620SStephen Smalley secid, task_sid(p), SECCLASS_PROCESS, perm, NULL); 40691da177e4SLinus Torvalds } 40701da177e4SLinus Torvalds 40711da177e4SLinus Torvalds static void selinux_task_to_inode(struct task_struct *p, 40721da177e4SLinus Torvalds struct inode *inode) 40731da177e4SLinus Torvalds { 40741da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 4075275bb41eSDavid Howells u32 sid = task_sid(p); 40761da177e4SLinus Torvalds 40779287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 4078db978da8SAndreas Gruenbacher isec->sclass = inode_mode_to_security_class(inode->i_mode); 4079275bb41eSDavid Howells isec->sid = sid; 40806f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 40819287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 40821da177e4SLinus Torvalds } 40831da177e4SLinus Torvalds 40841da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 408567f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv4(struct sk_buff *skb, 40862bf49690SThomas Liu struct common_audit_data *ad, u8 *proto) 40871da177e4SLinus Torvalds { 40881da177e4SLinus Torvalds int offset, ihlen, ret = -EINVAL; 40891da177e4SLinus Torvalds struct iphdr _iph, *ih; 40901da177e4SLinus Torvalds 4091bbe735e4SArnaldo Carvalho de Melo offset = skb_network_offset(skb); 40921da177e4SLinus Torvalds ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph); 40931da177e4SLinus Torvalds if (ih == NULL) 40941da177e4SLinus Torvalds goto out; 40951da177e4SLinus Torvalds 40961da177e4SLinus Torvalds ihlen = ih->ihl * 4; 40971da177e4SLinus Torvalds if (ihlen < sizeof(_iph)) 40981da177e4SLinus Torvalds goto out; 40991da177e4SLinus Torvalds 410048c62af6SEric Paris ad->u.net->v4info.saddr = ih->saddr; 410148c62af6SEric Paris ad->u.net->v4info.daddr = ih->daddr; 41021da177e4SLinus Torvalds ret = 0; 41031da177e4SLinus Torvalds 410467f83cbfSVenkat Yekkirala if (proto) 410567f83cbfSVenkat Yekkirala *proto = ih->protocol; 410667f83cbfSVenkat Yekkirala 41071da177e4SLinus Torvalds switch (ih->protocol) { 41081da177e4SLinus Torvalds case IPPROTO_TCP: { 41091da177e4SLinus Torvalds struct tcphdr _tcph, *th; 41101da177e4SLinus Torvalds 41111da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 41121da177e4SLinus Torvalds break; 41131da177e4SLinus Torvalds 41141da177e4SLinus Torvalds offset += ihlen; 41151da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 41161da177e4SLinus Torvalds if (th == NULL) 41171da177e4SLinus Torvalds break; 41181da177e4SLinus Torvalds 411948c62af6SEric Paris ad->u.net->sport = th->source; 412048c62af6SEric Paris ad->u.net->dport = th->dest; 41211da177e4SLinus Torvalds break; 41221da177e4SLinus Torvalds } 41231da177e4SLinus Torvalds 41241da177e4SLinus Torvalds case IPPROTO_UDP: { 41251da177e4SLinus Torvalds struct udphdr _udph, *uh; 41261da177e4SLinus Torvalds 41271da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 41281da177e4SLinus Torvalds break; 41291da177e4SLinus Torvalds 41301da177e4SLinus Torvalds offset += ihlen; 41311da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 41321da177e4SLinus Torvalds if (uh == NULL) 41331da177e4SLinus Torvalds break; 41341da177e4SLinus Torvalds 413548c62af6SEric Paris ad->u.net->sport = uh->source; 413648c62af6SEric Paris ad->u.net->dport = uh->dest; 41371da177e4SLinus Torvalds break; 41381da177e4SLinus Torvalds } 41391da177e4SLinus Torvalds 41402ee92d46SJames Morris case IPPROTO_DCCP: { 41412ee92d46SJames Morris struct dccp_hdr _dccph, *dh; 41422ee92d46SJames Morris 41432ee92d46SJames Morris if (ntohs(ih->frag_off) & IP_OFFSET) 41442ee92d46SJames Morris break; 41452ee92d46SJames Morris 41462ee92d46SJames Morris offset += ihlen; 41472ee92d46SJames Morris dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); 41482ee92d46SJames Morris if (dh == NULL) 41492ee92d46SJames Morris break; 41502ee92d46SJames Morris 415148c62af6SEric Paris ad->u.net->sport = dh->dccph_sport; 415248c62af6SEric Paris ad->u.net->dport = dh->dccph_dport; 41532ee92d46SJames Morris break; 41542ee92d46SJames Morris } 41552ee92d46SJames Morris 4156d452930fSRichard Haines #if IS_ENABLED(CONFIG_IP_SCTP) 4157d452930fSRichard Haines case IPPROTO_SCTP: { 4158d452930fSRichard Haines struct sctphdr _sctph, *sh; 4159d452930fSRichard Haines 4160d452930fSRichard Haines if (ntohs(ih->frag_off) & IP_OFFSET) 4161d452930fSRichard Haines break; 4162d452930fSRichard Haines 4163d452930fSRichard Haines offset += ihlen; 4164d452930fSRichard Haines sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph); 4165d452930fSRichard Haines if (sh == NULL) 4166d452930fSRichard Haines break; 4167d452930fSRichard Haines 4168d452930fSRichard Haines ad->u.net->sport = sh->source; 4169d452930fSRichard Haines ad->u.net->dport = sh->dest; 4170d452930fSRichard Haines break; 4171d452930fSRichard Haines } 4172d452930fSRichard Haines #endif 41731da177e4SLinus Torvalds default: 41741da177e4SLinus Torvalds break; 41751da177e4SLinus Torvalds } 41761da177e4SLinus Torvalds out: 41771da177e4SLinus Torvalds return ret; 41781da177e4SLinus Torvalds } 41791da177e4SLinus Torvalds 41801a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 41811da177e4SLinus Torvalds 41821da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 418367f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv6(struct sk_buff *skb, 41842bf49690SThomas Liu struct common_audit_data *ad, u8 *proto) 41851da177e4SLinus Torvalds { 41861da177e4SLinus Torvalds u8 nexthdr; 41871da177e4SLinus Torvalds int ret = -EINVAL, offset; 41881da177e4SLinus Torvalds struct ipv6hdr _ipv6h, *ip6; 418975f2811cSJesse Gross __be16 frag_off; 41901da177e4SLinus Torvalds 4191bbe735e4SArnaldo Carvalho de Melo offset = skb_network_offset(skb); 41921da177e4SLinus Torvalds ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h); 41931da177e4SLinus Torvalds if (ip6 == NULL) 41941da177e4SLinus Torvalds goto out; 41951da177e4SLinus Torvalds 419648c62af6SEric Paris ad->u.net->v6info.saddr = ip6->saddr; 419748c62af6SEric Paris ad->u.net->v6info.daddr = ip6->daddr; 41981da177e4SLinus Torvalds ret = 0; 41991da177e4SLinus Torvalds 42001da177e4SLinus Torvalds nexthdr = ip6->nexthdr; 42011da177e4SLinus Torvalds offset += sizeof(_ipv6h); 420275f2811cSJesse Gross offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off); 42031da177e4SLinus Torvalds if (offset < 0) 42041da177e4SLinus Torvalds goto out; 42051da177e4SLinus Torvalds 420667f83cbfSVenkat Yekkirala if (proto) 420767f83cbfSVenkat Yekkirala *proto = nexthdr; 420867f83cbfSVenkat Yekkirala 42091da177e4SLinus Torvalds switch (nexthdr) { 42101da177e4SLinus Torvalds case IPPROTO_TCP: { 42111da177e4SLinus Torvalds struct tcphdr _tcph, *th; 42121da177e4SLinus Torvalds 42131da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 42141da177e4SLinus Torvalds if (th == NULL) 42151da177e4SLinus Torvalds break; 42161da177e4SLinus Torvalds 421748c62af6SEric Paris ad->u.net->sport = th->source; 421848c62af6SEric Paris ad->u.net->dport = th->dest; 42191da177e4SLinus Torvalds break; 42201da177e4SLinus Torvalds } 42211da177e4SLinus Torvalds 42221da177e4SLinus Torvalds case IPPROTO_UDP: { 42231da177e4SLinus Torvalds struct udphdr _udph, *uh; 42241da177e4SLinus Torvalds 42251da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 42261da177e4SLinus Torvalds if (uh == NULL) 42271da177e4SLinus Torvalds break; 42281da177e4SLinus Torvalds 422948c62af6SEric Paris ad->u.net->sport = uh->source; 423048c62af6SEric Paris ad->u.net->dport = uh->dest; 42311da177e4SLinus Torvalds break; 42321da177e4SLinus Torvalds } 42331da177e4SLinus Torvalds 42342ee92d46SJames Morris case IPPROTO_DCCP: { 42352ee92d46SJames Morris struct dccp_hdr _dccph, *dh; 42362ee92d46SJames Morris 42372ee92d46SJames Morris dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); 42382ee92d46SJames Morris if (dh == NULL) 42392ee92d46SJames Morris break; 42402ee92d46SJames Morris 424148c62af6SEric Paris ad->u.net->sport = dh->dccph_sport; 424248c62af6SEric Paris ad->u.net->dport = dh->dccph_dport; 42432ee92d46SJames Morris break; 42442ee92d46SJames Morris } 42452ee92d46SJames Morris 4246d452930fSRichard Haines #if IS_ENABLED(CONFIG_IP_SCTP) 4247d452930fSRichard Haines case IPPROTO_SCTP: { 4248d452930fSRichard Haines struct sctphdr _sctph, *sh; 4249d452930fSRichard Haines 4250d452930fSRichard Haines sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph); 4251d452930fSRichard Haines if (sh == NULL) 4252d452930fSRichard Haines break; 4253d452930fSRichard Haines 4254d452930fSRichard Haines ad->u.net->sport = sh->source; 4255d452930fSRichard Haines ad->u.net->dport = sh->dest; 4256d452930fSRichard Haines break; 4257d452930fSRichard Haines } 4258d452930fSRichard Haines #endif 42591da177e4SLinus Torvalds /* includes fragments */ 42601da177e4SLinus Torvalds default: 42611da177e4SLinus Torvalds break; 42621da177e4SLinus Torvalds } 42631da177e4SLinus Torvalds out: 42641da177e4SLinus Torvalds return ret; 42651da177e4SLinus Torvalds } 42661da177e4SLinus Torvalds 42671da177e4SLinus Torvalds #endif /* IPV6 */ 42681da177e4SLinus Torvalds 42692bf49690SThomas Liu static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad, 4270cf9481e2SDavid Howells char **_addrp, int src, u8 *proto) 42711da177e4SLinus Torvalds { 4272cf9481e2SDavid Howells char *addrp; 4273cf9481e2SDavid Howells int ret; 42741da177e4SLinus Torvalds 427548c62af6SEric Paris switch (ad->u.net->family) { 42761da177e4SLinus Torvalds case PF_INET: 427767f83cbfSVenkat Yekkirala ret = selinux_parse_skb_ipv4(skb, ad, proto); 4278cf9481e2SDavid Howells if (ret) 4279cf9481e2SDavid Howells goto parse_error; 428048c62af6SEric Paris addrp = (char *)(src ? &ad->u.net->v4info.saddr : 428148c62af6SEric Paris &ad->u.net->v4info.daddr); 4282cf9481e2SDavid Howells goto okay; 42831da177e4SLinus Torvalds 42841a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 42851da177e4SLinus Torvalds case PF_INET6: 428667f83cbfSVenkat Yekkirala ret = selinux_parse_skb_ipv6(skb, ad, proto); 4287cf9481e2SDavid Howells if (ret) 4288cf9481e2SDavid Howells goto parse_error; 428948c62af6SEric Paris addrp = (char *)(src ? &ad->u.net->v6info.saddr : 429048c62af6SEric Paris &ad->u.net->v6info.daddr); 4291cf9481e2SDavid Howells goto okay; 42921da177e4SLinus Torvalds #endif /* IPV6 */ 42931da177e4SLinus Torvalds default: 4294cf9481e2SDavid Howells addrp = NULL; 4295cf9481e2SDavid Howells goto okay; 42961da177e4SLinus Torvalds } 42971da177e4SLinus Torvalds 4298cf9481e2SDavid Howells parse_error: 4299c103a91eSpeter enderborg pr_warn( 430071f1cb05SPaul Moore "SELinux: failure in selinux_parse_skb()," 430171f1cb05SPaul Moore " unable to parse packet\n"); 43021da177e4SLinus Torvalds return ret; 4303cf9481e2SDavid Howells 4304cf9481e2SDavid Howells okay: 4305cf9481e2SDavid Howells if (_addrp) 4306cf9481e2SDavid Howells *_addrp = addrp; 4307cf9481e2SDavid Howells return 0; 43081da177e4SLinus Torvalds } 43091da177e4SLinus Torvalds 43104f6a993fSPaul Moore /** 4311220deb96SPaul Moore * selinux_skb_peerlbl_sid - Determine the peer label of a packet 43124f6a993fSPaul Moore * @skb: the packet 431375e22910SPaul Moore * @family: protocol family 4314220deb96SPaul Moore * @sid: the packet's peer label SID 43154f6a993fSPaul Moore * 43164f6a993fSPaul Moore * Description: 4317220deb96SPaul Moore * Check the various different forms of network peer labeling and determine 4318220deb96SPaul Moore * the peer label/SID for the packet; most of the magic actually occurs in 4319220deb96SPaul Moore * the security server function security_net_peersid_cmp(). The function 4320220deb96SPaul Moore * returns zero if the value in @sid is valid (although it may be SECSID_NULL) 4321220deb96SPaul Moore * or -EACCES if @sid is invalid due to inconsistencies with the different 4322220deb96SPaul Moore * peer labels. 43234f6a993fSPaul Moore * 43244f6a993fSPaul Moore */ 4325220deb96SPaul Moore static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid) 43264f6a993fSPaul Moore { 432771f1cb05SPaul Moore int err; 43284f6a993fSPaul Moore u32 xfrm_sid; 43294f6a993fSPaul Moore u32 nlbl_sid; 4330220deb96SPaul Moore u32 nlbl_type; 43314f6a993fSPaul Moore 4332817eff71SPaul Moore err = selinux_xfrm_skb_sid(skb, &xfrm_sid); 4333bed4d7efSPaul Moore if (unlikely(err)) 4334bed4d7efSPaul Moore return -EACCES; 4335bed4d7efSPaul Moore err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid); 4336bed4d7efSPaul Moore if (unlikely(err)) 4337bed4d7efSPaul Moore return -EACCES; 4338220deb96SPaul Moore 4339aa8e712cSStephen Smalley err = security_net_peersid_resolve(&selinux_state, nlbl_sid, 4340aa8e712cSStephen Smalley nlbl_type, xfrm_sid, sid); 434171f1cb05SPaul Moore if (unlikely(err)) { 4342c103a91eSpeter enderborg pr_warn( 434371f1cb05SPaul Moore "SELinux: failure in selinux_skb_peerlbl_sid()," 434471f1cb05SPaul Moore " unable to determine packet's peer label\n"); 4345220deb96SPaul Moore return -EACCES; 434671f1cb05SPaul Moore } 4347220deb96SPaul Moore 4348220deb96SPaul Moore return 0; 43494f6a993fSPaul Moore } 43504f6a993fSPaul Moore 4351446b8024SPaul Moore /** 4352446b8024SPaul Moore * selinux_conn_sid - Determine the child socket label for a connection 4353446b8024SPaul Moore * @sk_sid: the parent socket's SID 4354446b8024SPaul Moore * @skb_sid: the packet's SID 4355446b8024SPaul Moore * @conn_sid: the resulting connection SID 4356446b8024SPaul Moore * 4357446b8024SPaul Moore * If @skb_sid is valid then the user:role:type information from @sk_sid is 4358446b8024SPaul Moore * combined with the MLS information from @skb_sid in order to create 4359446b8024SPaul Moore * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy 4360446b8024SPaul Moore * of @sk_sid. Returns zero on success, negative values on failure. 4361446b8024SPaul Moore * 4362446b8024SPaul Moore */ 4363446b8024SPaul Moore static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid) 4364446b8024SPaul Moore { 4365446b8024SPaul Moore int err = 0; 4366446b8024SPaul Moore 4367446b8024SPaul Moore if (skb_sid != SECSID_NULL) 4368aa8e712cSStephen Smalley err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid, 4369aa8e712cSStephen Smalley conn_sid); 4370446b8024SPaul Moore else 4371446b8024SPaul Moore *conn_sid = sk_sid; 4372446b8024SPaul Moore 4373446b8024SPaul Moore return err; 4374446b8024SPaul Moore } 4375446b8024SPaul Moore 43761da177e4SLinus Torvalds /* socket security operations */ 4377d4f2d978SPaul Moore 43782ad18bdfSHarry Ciao static int socket_sockcreate_sid(const struct task_security_struct *tsec, 43792ad18bdfSHarry Ciao u16 secclass, u32 *socksid) 4380d4f2d978SPaul Moore { 43812ad18bdfSHarry Ciao if (tsec->sockcreate_sid > SECSID_NULL) { 43822ad18bdfSHarry Ciao *socksid = tsec->sockcreate_sid; 43832ad18bdfSHarry Ciao return 0; 43842ad18bdfSHarry Ciao } 43852ad18bdfSHarry Ciao 4386aa8e712cSStephen Smalley return security_transition_sid(&selinux_state, tsec->sid, tsec->sid, 4387aa8e712cSStephen Smalley secclass, NULL, socksid); 4388d4f2d978SPaul Moore } 4389d4f2d978SPaul Moore 4390be0554c9SStephen Smalley static int sock_has_perm(struct sock *sk, u32 perms) 43911da177e4SLinus Torvalds { 4392253bfae6SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 43932bf49690SThomas Liu struct common_audit_data ad; 439448c62af6SEric Paris struct lsm_network_audit net = {0,}; 43951da177e4SLinus Torvalds 4396253bfae6SPaul Moore if (sksec->sid == SECINITSID_KERNEL) 4397253bfae6SPaul Moore return 0; 43981da177e4SLinus Torvalds 439950c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 440048c62af6SEric Paris ad.u.net = &net; 440148c62af6SEric Paris ad.u.net->sk = sk; 44021da177e4SLinus Torvalds 44036b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 44046b6bc620SStephen Smalley current_sid(), sksec->sid, sksec->sclass, perms, 4405be0554c9SStephen Smalley &ad); 44061da177e4SLinus Torvalds } 44071da177e4SLinus Torvalds 44081da177e4SLinus Torvalds static int selinux_socket_create(int family, int type, 44091da177e4SLinus Torvalds int protocol, int kern) 44101da177e4SLinus Torvalds { 44115fb49870SPaul Moore const struct task_security_struct *tsec = current_security(); 4412d4f2d978SPaul Moore u32 newsid; 4413275bb41eSDavid Howells u16 secclass; 44142ad18bdfSHarry Ciao int rc; 44151da177e4SLinus Torvalds 44161da177e4SLinus Torvalds if (kern) 4417d4f2d978SPaul Moore return 0; 44181da177e4SLinus Torvalds 4419275bb41eSDavid Howells secclass = socket_type_to_security_class(family, type, protocol); 44202ad18bdfSHarry Ciao rc = socket_sockcreate_sid(tsec, secclass, &newsid); 44212ad18bdfSHarry Ciao if (rc) 44222ad18bdfSHarry Ciao return rc; 44232ad18bdfSHarry Ciao 44246b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 44256b6bc620SStephen Smalley tsec->sid, newsid, secclass, SOCKET__CREATE, NULL); 44261da177e4SLinus Torvalds } 44271da177e4SLinus Torvalds 44287420ed23SVenkat Yekkirala static int selinux_socket_post_create(struct socket *sock, int family, 44291da177e4SLinus Torvalds int type, int protocol, int kern) 44301da177e4SLinus Torvalds { 44315fb49870SPaul Moore const struct task_security_struct *tsec = current_security(); 44325d226df4SAndreas Gruenbacher struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock)); 4433892c141eSVenkat Yekkirala struct sk_security_struct *sksec; 44349287aed2SAndreas Gruenbacher u16 sclass = socket_type_to_security_class(family, type, protocol); 44359287aed2SAndreas Gruenbacher u32 sid = SECINITSID_KERNEL; 4436275bb41eSDavid Howells int err = 0; 4437275bb41eSDavid Howells 44389287aed2SAndreas Gruenbacher if (!kern) { 44399287aed2SAndreas Gruenbacher err = socket_sockcreate_sid(tsec, sclass, &sid); 44402ad18bdfSHarry Ciao if (err) 44412ad18bdfSHarry Ciao return err; 44422ad18bdfSHarry Ciao } 4443275bb41eSDavid Howells 44449287aed2SAndreas Gruenbacher isec->sclass = sclass; 44459287aed2SAndreas Gruenbacher isec->sid = sid; 44466f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 44471da177e4SLinus Torvalds 4448892c141eSVenkat Yekkirala if (sock->sk) { 4449892c141eSVenkat Yekkirala sksec = sock->sk->sk_security; 44509287aed2SAndreas Gruenbacher sksec->sclass = sclass; 44519287aed2SAndreas Gruenbacher sksec->sid = sid; 4452d452930fSRichard Haines /* Allows detection of the first association on this socket */ 4453d452930fSRichard Haines if (sksec->sclass == SECCLASS_SCTP_SOCKET) 4454d452930fSRichard Haines sksec->sctp_assoc_state = SCTP_ASSOC_UNSET; 4455d452930fSRichard Haines 4456389fb800SPaul Moore err = selinux_netlbl_socket_post_create(sock->sk, family); 4457892c141eSVenkat Yekkirala } 4458892c141eSVenkat Yekkirala 44597420ed23SVenkat Yekkirala return err; 44601da177e4SLinus Torvalds } 44611da177e4SLinus Torvalds 44620b811db2SDavid Herrmann static int selinux_socket_socketpair(struct socket *socka, 44630b811db2SDavid Herrmann struct socket *sockb) 44640b811db2SDavid Herrmann { 44650b811db2SDavid Herrmann struct sk_security_struct *sksec_a = socka->sk->sk_security; 44660b811db2SDavid Herrmann struct sk_security_struct *sksec_b = sockb->sk->sk_security; 44670b811db2SDavid Herrmann 44680b811db2SDavid Herrmann sksec_a->peer_sid = sksec_b->sid; 44690b811db2SDavid Herrmann sksec_b->peer_sid = sksec_a->sid; 44700b811db2SDavid Herrmann 44710b811db2SDavid Herrmann return 0; 44720b811db2SDavid Herrmann } 44730b811db2SDavid Herrmann 44741da177e4SLinus Torvalds /* Range of port numbers used to automatically bind. 44751da177e4SLinus Torvalds Need to determine whether we should perform a name_bind 44761da177e4SLinus Torvalds permission check between the socket and the port number. */ 44771da177e4SLinus Torvalds 44781da177e4SLinus Torvalds static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen) 44791da177e4SLinus Torvalds { 4480253bfae6SPaul Moore struct sock *sk = sock->sk; 44810f8db8ccSAlexey Kodanev struct sk_security_struct *sksec = sk->sk_security; 44821da177e4SLinus Torvalds u16 family; 44831da177e4SLinus Torvalds int err; 44841da177e4SLinus Torvalds 4485be0554c9SStephen Smalley err = sock_has_perm(sk, SOCKET__BIND); 44861da177e4SLinus Torvalds if (err) 44871da177e4SLinus Torvalds goto out; 44881da177e4SLinus Torvalds 4489d452930fSRichard Haines /* If PF_INET or PF_INET6, check name_bind permission for the port. */ 4490253bfae6SPaul Moore family = sk->sk_family; 44911da177e4SLinus Torvalds if (family == PF_INET || family == PF_INET6) { 44921da177e4SLinus Torvalds char *addrp; 44932bf49690SThomas Liu struct common_audit_data ad; 449448c62af6SEric Paris struct lsm_network_audit net = {0,}; 44951da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 44961da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 44970f8db8ccSAlexey Kodanev u16 family_sa = address->sa_family; 44981da177e4SLinus Torvalds unsigned short snum; 4499e399f982SJames Morris u32 sid, node_perm; 45001da177e4SLinus Torvalds 4501d452930fSRichard Haines /* 4502d452930fSRichard Haines * sctp_bindx(3) calls via selinux_sctp_bind_connect() 4503d452930fSRichard Haines * that validates multiple binding addresses. Because of this 4504d452930fSRichard Haines * need to check address->sa_family as it is possible to have 4505d452930fSRichard Haines * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET. 4506d452930fSRichard Haines */ 45070f8db8ccSAlexey Kodanev switch (family_sa) { 45080f8db8ccSAlexey Kodanev case AF_UNSPEC: 450968741a8aSRichard Haines case AF_INET: 451068741a8aSRichard Haines if (addrlen < sizeof(struct sockaddr_in)) 451168741a8aSRichard Haines return -EINVAL; 45121da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 45130f8db8ccSAlexey Kodanev if (family_sa == AF_UNSPEC) { 45140f8db8ccSAlexey Kodanev /* see __inet_bind(), we only want to allow 45150f8db8ccSAlexey Kodanev * AF_UNSPEC if the address is INADDR_ANY 45160f8db8ccSAlexey Kodanev */ 45170f8db8ccSAlexey Kodanev if (addr4->sin_addr.s_addr != htonl(INADDR_ANY)) 45180f8db8ccSAlexey Kodanev goto err_af; 45190f8db8ccSAlexey Kodanev family_sa = AF_INET; 45200f8db8ccSAlexey Kodanev } 45211da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 45221da177e4SLinus Torvalds addrp = (char *)&addr4->sin_addr.s_addr; 452368741a8aSRichard Haines break; 452468741a8aSRichard Haines case AF_INET6: 452568741a8aSRichard Haines if (addrlen < SIN6_LEN_RFC2133) 452668741a8aSRichard Haines return -EINVAL; 45271da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 45281da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 45291da177e4SLinus Torvalds addrp = (char *)&addr6->sin6_addr.s6_addr; 453068741a8aSRichard Haines break; 453168741a8aSRichard Haines default: 45320f8db8ccSAlexey Kodanev goto err_af; 45331da177e4SLinus Torvalds } 45341da177e4SLinus Torvalds 453588b7d370SAlexey Kodanev ad.type = LSM_AUDIT_DATA_NET; 453688b7d370SAlexey Kodanev ad.u.net = &net; 453788b7d370SAlexey Kodanev ad.u.net->sport = htons(snum); 453888b7d370SAlexey Kodanev ad.u.net->family = family_sa; 453988b7d370SAlexey Kodanev 4540227b60f5SStephen Hemminger if (snum) { 4541227b60f5SStephen Hemminger int low, high; 4542227b60f5SStephen Hemminger 45430bbf87d8SEric W. Biederman inet_get_local_port_range(sock_net(sk), &low, &high); 4544227b60f5SStephen Hemminger 45454548b683SKrister Johansen if (snum < max(inet_prot_sock(sock_net(sk)), low) || 45464548b683SKrister Johansen snum > high) { 45473e112172SPaul Moore err = sel_netport_sid(sk->sk_protocol, 45483e112172SPaul Moore snum, &sid); 45491da177e4SLinus Torvalds if (err) 45501da177e4SLinus Torvalds goto out; 45516b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 45526b6bc620SStephen Smalley sksec->sid, sid, 4553253bfae6SPaul Moore sksec->sclass, 45541da177e4SLinus Torvalds SOCKET__NAME_BIND, &ad); 45551da177e4SLinus Torvalds if (err) 45561da177e4SLinus Torvalds goto out; 45571da177e4SLinus Torvalds } 4558227b60f5SStephen Hemminger } 45591da177e4SLinus Torvalds 4560253bfae6SPaul Moore switch (sksec->sclass) { 456113402580SJames Morris case SECCLASS_TCP_SOCKET: 45621da177e4SLinus Torvalds node_perm = TCP_SOCKET__NODE_BIND; 45631da177e4SLinus Torvalds break; 45641da177e4SLinus Torvalds 456513402580SJames Morris case SECCLASS_UDP_SOCKET: 45661da177e4SLinus Torvalds node_perm = UDP_SOCKET__NODE_BIND; 45671da177e4SLinus Torvalds break; 45681da177e4SLinus Torvalds 45692ee92d46SJames Morris case SECCLASS_DCCP_SOCKET: 45702ee92d46SJames Morris node_perm = DCCP_SOCKET__NODE_BIND; 45712ee92d46SJames Morris break; 45722ee92d46SJames Morris 4573d452930fSRichard Haines case SECCLASS_SCTP_SOCKET: 4574d452930fSRichard Haines node_perm = SCTP_SOCKET__NODE_BIND; 4575d452930fSRichard Haines break; 4576d452930fSRichard Haines 45771da177e4SLinus Torvalds default: 45781da177e4SLinus Torvalds node_perm = RAWIP_SOCKET__NODE_BIND; 45791da177e4SLinus Torvalds break; 45801da177e4SLinus Torvalds } 45811da177e4SLinus Torvalds 458288b7d370SAlexey Kodanev err = sel_netnode_sid(addrp, family_sa, &sid); 45831da177e4SLinus Torvalds if (err) 45841da177e4SLinus Torvalds goto out; 45851da177e4SLinus Torvalds 45860f8db8ccSAlexey Kodanev if (family_sa == AF_INET) 458748c62af6SEric Paris ad.u.net->v4info.saddr = addr4->sin_addr.s_addr; 45881da177e4SLinus Torvalds else 458948c62af6SEric Paris ad.u.net->v6info.saddr = addr6->sin6_addr; 45901da177e4SLinus Torvalds 45916b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 45926b6bc620SStephen Smalley sksec->sid, sid, 4593253bfae6SPaul Moore sksec->sclass, node_perm, &ad); 45941da177e4SLinus Torvalds if (err) 45951da177e4SLinus Torvalds goto out; 45961da177e4SLinus Torvalds } 45971da177e4SLinus Torvalds out: 45981da177e4SLinus Torvalds return err; 45990f8db8ccSAlexey Kodanev err_af: 46000f8db8ccSAlexey Kodanev /* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */ 46010f8db8ccSAlexey Kodanev if (sksec->sclass == SECCLASS_SCTP_SOCKET) 46020f8db8ccSAlexey Kodanev return -EINVAL; 46030f8db8ccSAlexey Kodanev return -EAFNOSUPPORT; 46041da177e4SLinus Torvalds } 46051da177e4SLinus Torvalds 4606d452930fSRichard Haines /* This supports connect(2) and SCTP connect services such as sctp_connectx(3) 46075fb94e9cSMauro Carvalho Chehab * and sctp_sendmsg(3) as described in Documentation/security/LSM-sctp.rst 4608d452930fSRichard Haines */ 4609d452930fSRichard Haines static int selinux_socket_connect_helper(struct socket *sock, 4610d452930fSRichard Haines struct sockaddr *address, int addrlen) 46111da177e4SLinus Torvalds { 4612014ab19aSPaul Moore struct sock *sk = sock->sk; 4613253bfae6SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 46141da177e4SLinus Torvalds int err; 46151da177e4SLinus Torvalds 4616be0554c9SStephen Smalley err = sock_has_perm(sk, SOCKET__CONNECT); 46171da177e4SLinus Torvalds if (err) 46181da177e4SLinus Torvalds return err; 46191da177e4SLinus Torvalds 46201da177e4SLinus Torvalds /* 4621d452930fSRichard Haines * If a TCP, DCCP or SCTP socket, check name_connect permission 4622d452930fSRichard Haines * for the port. 46231da177e4SLinus Torvalds */ 4624253bfae6SPaul Moore if (sksec->sclass == SECCLASS_TCP_SOCKET || 4625d452930fSRichard Haines sksec->sclass == SECCLASS_DCCP_SOCKET || 4626d452930fSRichard Haines sksec->sclass == SECCLASS_SCTP_SOCKET) { 46272bf49690SThomas Liu struct common_audit_data ad; 462848c62af6SEric Paris struct lsm_network_audit net = {0,}; 46291da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 46301da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 46311da177e4SLinus Torvalds unsigned short snum; 46322ee92d46SJames Morris u32 sid, perm; 46331da177e4SLinus Torvalds 4634d452930fSRichard Haines /* sctp_connectx(3) calls via selinux_sctp_bind_connect() 4635d452930fSRichard Haines * that validates multiple connect addresses. Because of this 4636d452930fSRichard Haines * need to check address->sa_family as it is possible to have 4637d452930fSRichard Haines * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET. 4638d452930fSRichard Haines */ 463968741a8aSRichard Haines switch (address->sa_family) { 464068741a8aSRichard Haines case AF_INET: 46411da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 4642911656f8SStephen Smalley if (addrlen < sizeof(struct sockaddr_in)) 46431da177e4SLinus Torvalds return -EINVAL; 46441da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 464568741a8aSRichard Haines break; 464668741a8aSRichard Haines case AF_INET6: 46471da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 4648911656f8SStephen Smalley if (addrlen < SIN6_LEN_RFC2133) 46491da177e4SLinus Torvalds return -EINVAL; 46501da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 465168741a8aSRichard Haines break; 465268741a8aSRichard Haines default: 465368741a8aSRichard Haines /* Note that SCTP services expect -EINVAL, whereas 465468741a8aSRichard Haines * others expect -EAFNOSUPPORT. 465568741a8aSRichard Haines */ 465668741a8aSRichard Haines if (sksec->sclass == SECCLASS_SCTP_SOCKET) 465768741a8aSRichard Haines return -EINVAL; 465868741a8aSRichard Haines else 465968741a8aSRichard Haines return -EAFNOSUPPORT; 46601da177e4SLinus Torvalds } 46611da177e4SLinus Torvalds 46623e112172SPaul Moore err = sel_netport_sid(sk->sk_protocol, snum, &sid); 46631da177e4SLinus Torvalds if (err) 4664d452930fSRichard Haines return err; 46651da177e4SLinus Torvalds 4666d452930fSRichard Haines switch (sksec->sclass) { 4667d452930fSRichard Haines case SECCLASS_TCP_SOCKET: 4668d452930fSRichard Haines perm = TCP_SOCKET__NAME_CONNECT; 4669d452930fSRichard Haines break; 4670d452930fSRichard Haines case SECCLASS_DCCP_SOCKET: 4671d452930fSRichard Haines perm = DCCP_SOCKET__NAME_CONNECT; 4672d452930fSRichard Haines break; 4673d452930fSRichard Haines case SECCLASS_SCTP_SOCKET: 4674d452930fSRichard Haines perm = SCTP_SOCKET__NAME_CONNECT; 4675d452930fSRichard Haines break; 4676d452930fSRichard Haines } 46772ee92d46SJames Morris 467850c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 467948c62af6SEric Paris ad.u.net = &net; 468048c62af6SEric Paris ad.u.net->dport = htons(snum); 468188b7d370SAlexey Kodanev ad.u.net->family = address->sa_family; 46826b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 46836b6bc620SStephen Smalley sksec->sid, sid, sksec->sclass, perm, &ad); 46841da177e4SLinus Torvalds if (err) 4685d452930fSRichard Haines return err; 46861da177e4SLinus Torvalds } 46871da177e4SLinus Torvalds 4688d452930fSRichard Haines return 0; 4689d452930fSRichard Haines } 4690014ab19aSPaul Moore 4691d452930fSRichard Haines /* Supports connect(2), see comments in selinux_socket_connect_helper() */ 4692d452930fSRichard Haines static int selinux_socket_connect(struct socket *sock, 4693d452930fSRichard Haines struct sockaddr *address, int addrlen) 4694d452930fSRichard Haines { 4695d452930fSRichard Haines int err; 4696d452930fSRichard Haines struct sock *sk = sock->sk; 4697d452930fSRichard Haines 4698d452930fSRichard Haines err = selinux_socket_connect_helper(sock, address, addrlen); 4699d452930fSRichard Haines if (err) 47001da177e4SLinus Torvalds return err; 4701d452930fSRichard Haines 4702d452930fSRichard Haines return selinux_netlbl_socket_connect(sk, address); 47031da177e4SLinus Torvalds } 47041da177e4SLinus Torvalds 47051da177e4SLinus Torvalds static int selinux_socket_listen(struct socket *sock, int backlog) 47061da177e4SLinus Torvalds { 4707be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__LISTEN); 47081da177e4SLinus Torvalds } 47091da177e4SLinus Torvalds 47101da177e4SLinus Torvalds static int selinux_socket_accept(struct socket *sock, struct socket *newsock) 47111da177e4SLinus Torvalds { 47121da177e4SLinus Torvalds int err; 47131da177e4SLinus Torvalds struct inode_security_struct *isec; 47141da177e4SLinus Torvalds struct inode_security_struct *newisec; 47159287aed2SAndreas Gruenbacher u16 sclass; 47169287aed2SAndreas Gruenbacher u32 sid; 47171da177e4SLinus Torvalds 4718be0554c9SStephen Smalley err = sock_has_perm(sock->sk, SOCKET__ACCEPT); 47191da177e4SLinus Torvalds if (err) 47201da177e4SLinus Torvalds return err; 47211da177e4SLinus Torvalds 47225d226df4SAndreas Gruenbacher isec = inode_security_novalidate(SOCK_INODE(sock)); 47239287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 47249287aed2SAndreas Gruenbacher sclass = isec->sclass; 47259287aed2SAndreas Gruenbacher sid = isec->sid; 47269287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 47279287aed2SAndreas Gruenbacher 47289287aed2SAndreas Gruenbacher newisec = inode_security_novalidate(SOCK_INODE(newsock)); 47299287aed2SAndreas Gruenbacher newisec->sclass = sclass; 47309287aed2SAndreas Gruenbacher newisec->sid = sid; 47316f3be9f5SAndreas Gruenbacher newisec->initialized = LABEL_INITIALIZED; 47321da177e4SLinus Torvalds 47331da177e4SLinus Torvalds return 0; 47341da177e4SLinus Torvalds } 47351da177e4SLinus Torvalds 47361da177e4SLinus Torvalds static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg, 47371da177e4SLinus Torvalds int size) 47381da177e4SLinus Torvalds { 4739be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__WRITE); 47401da177e4SLinus Torvalds } 47411da177e4SLinus Torvalds 47421da177e4SLinus Torvalds static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg, 47431da177e4SLinus Torvalds int size, int flags) 47441da177e4SLinus Torvalds { 4745be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__READ); 47461da177e4SLinus Torvalds } 47471da177e4SLinus Torvalds 47481da177e4SLinus Torvalds static int selinux_socket_getsockname(struct socket *sock) 47491da177e4SLinus Torvalds { 4750be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__GETATTR); 47511da177e4SLinus Torvalds } 47521da177e4SLinus Torvalds 47531da177e4SLinus Torvalds static int selinux_socket_getpeername(struct socket *sock) 47541da177e4SLinus Torvalds { 4755be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__GETATTR); 47561da177e4SLinus Torvalds } 47571da177e4SLinus Torvalds 47581da177e4SLinus Torvalds static int selinux_socket_setsockopt(struct socket *sock, int level, int optname) 47591da177e4SLinus Torvalds { 4760f8687afeSPaul Moore int err; 4761f8687afeSPaul Moore 4762be0554c9SStephen Smalley err = sock_has_perm(sock->sk, SOCKET__SETOPT); 4763f8687afeSPaul Moore if (err) 4764f8687afeSPaul Moore return err; 4765f8687afeSPaul Moore 4766f8687afeSPaul Moore return selinux_netlbl_socket_setsockopt(sock, level, optname); 47671da177e4SLinus Torvalds } 47681da177e4SLinus Torvalds 47691da177e4SLinus Torvalds static int selinux_socket_getsockopt(struct socket *sock, int level, 47701da177e4SLinus Torvalds int optname) 47711da177e4SLinus Torvalds { 4772be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__GETOPT); 47731da177e4SLinus Torvalds } 47741da177e4SLinus Torvalds 47751da177e4SLinus Torvalds static int selinux_socket_shutdown(struct socket *sock, int how) 47761da177e4SLinus Torvalds { 4777be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__SHUTDOWN); 47781da177e4SLinus Torvalds } 47791da177e4SLinus Torvalds 47803610cda5SDavid S. Miller static int selinux_socket_unix_stream_connect(struct sock *sock, 47813610cda5SDavid S. Miller struct sock *other, 47821da177e4SLinus Torvalds struct sock *newsk) 47831da177e4SLinus Torvalds { 47843610cda5SDavid S. Miller struct sk_security_struct *sksec_sock = sock->sk_security; 47853610cda5SDavid S. Miller struct sk_security_struct *sksec_other = other->sk_security; 47864d1e2451SPaul Moore struct sk_security_struct *sksec_new = newsk->sk_security; 47872bf49690SThomas Liu struct common_audit_data ad; 478848c62af6SEric Paris struct lsm_network_audit net = {0,}; 47891da177e4SLinus Torvalds int err; 47901da177e4SLinus Torvalds 479150c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 479248c62af6SEric Paris ad.u.net = &net; 479348c62af6SEric Paris ad.u.net->sk = other; 47941da177e4SLinus Torvalds 47956b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 47966b6bc620SStephen Smalley sksec_sock->sid, sksec_other->sid, 47974d1e2451SPaul Moore sksec_other->sclass, 47981da177e4SLinus Torvalds UNIX_STREAM_SOCKET__CONNECTTO, &ad); 47991da177e4SLinus Torvalds if (err) 48001da177e4SLinus Torvalds return err; 48011da177e4SLinus Torvalds 48021da177e4SLinus Torvalds /* server child socket */ 48034d1e2451SPaul Moore sksec_new->peer_sid = sksec_sock->sid; 4804aa8e712cSStephen Smalley err = security_sid_mls_copy(&selinux_state, sksec_other->sid, 4805aa8e712cSStephen Smalley sksec_sock->sid, &sksec_new->sid); 48064d1e2451SPaul Moore if (err) 48074237c75cSVenkat Yekkirala return err; 48084d1e2451SPaul Moore 48094d1e2451SPaul Moore /* connecting socket */ 48104d1e2451SPaul Moore sksec_sock->peer_sid = sksec_new->sid; 48114d1e2451SPaul Moore 48124d1e2451SPaul Moore return 0; 48131da177e4SLinus Torvalds } 48141da177e4SLinus Torvalds 48151da177e4SLinus Torvalds static int selinux_socket_unix_may_send(struct socket *sock, 48161da177e4SLinus Torvalds struct socket *other) 48171da177e4SLinus Torvalds { 4818253bfae6SPaul Moore struct sk_security_struct *ssec = sock->sk->sk_security; 4819253bfae6SPaul Moore struct sk_security_struct *osec = other->sk->sk_security; 48202bf49690SThomas Liu struct common_audit_data ad; 482148c62af6SEric Paris struct lsm_network_audit net = {0,}; 48221da177e4SLinus Torvalds 482350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 482448c62af6SEric Paris ad.u.net = &net; 482548c62af6SEric Paris ad.u.net->sk = other->sk; 48261da177e4SLinus Torvalds 48276b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 48286b6bc620SStephen Smalley ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO, 4829253bfae6SPaul Moore &ad); 48301da177e4SLinus Torvalds } 48311da177e4SLinus Torvalds 4832cbe0d6e8SPaul Moore static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex, 4833cbe0d6e8SPaul Moore char *addrp, u16 family, u32 peer_sid, 48342bf49690SThomas Liu struct common_audit_data *ad) 4835effad8dfSPaul Moore { 4836effad8dfSPaul Moore int err; 4837effad8dfSPaul Moore u32 if_sid; 4838effad8dfSPaul Moore u32 node_sid; 4839effad8dfSPaul Moore 4840cbe0d6e8SPaul Moore err = sel_netif_sid(ns, ifindex, &if_sid); 4841effad8dfSPaul Moore if (err) 4842effad8dfSPaul Moore return err; 48436b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 48446b6bc620SStephen Smalley peer_sid, if_sid, 4845effad8dfSPaul Moore SECCLASS_NETIF, NETIF__INGRESS, ad); 4846effad8dfSPaul Moore if (err) 4847effad8dfSPaul Moore return err; 4848effad8dfSPaul Moore 4849effad8dfSPaul Moore err = sel_netnode_sid(addrp, family, &node_sid); 4850effad8dfSPaul Moore if (err) 4851effad8dfSPaul Moore return err; 48526b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 48536b6bc620SStephen Smalley peer_sid, node_sid, 4854effad8dfSPaul Moore SECCLASS_NODE, NODE__RECVFROM, ad); 4855effad8dfSPaul Moore } 4856effad8dfSPaul Moore 4857220deb96SPaul Moore static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb, 4858d8395c87SPaul Moore u16 family) 4859220deb96SPaul Moore { 4860277d342fSPaul Moore int err = 0; 4861220deb96SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 4862220deb96SPaul Moore u32 sk_sid = sksec->sid; 48632bf49690SThomas Liu struct common_audit_data ad; 486448c62af6SEric Paris struct lsm_network_audit net = {0,}; 4865d8395c87SPaul Moore char *addrp; 4866d8395c87SPaul Moore 486750c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 486848c62af6SEric Paris ad.u.net = &net; 486948c62af6SEric Paris ad.u.net->netif = skb->skb_iif; 487048c62af6SEric Paris ad.u.net->family = family; 4871d8395c87SPaul Moore err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); 4872d8395c87SPaul Moore if (err) 4873d8395c87SPaul Moore return err; 4874220deb96SPaul Moore 487558bfbb51SPaul Moore if (selinux_secmark_enabled()) { 48766b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 48776b6bc620SStephen Smalley sk_sid, skb->secmark, SECCLASS_PACKET, 4878d8395c87SPaul Moore PACKET__RECV, &ad); 4879220deb96SPaul Moore if (err) 4880220deb96SPaul Moore return err; 488158bfbb51SPaul Moore } 4882220deb96SPaul Moore 4883d8395c87SPaul Moore err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad); 4884220deb96SPaul Moore if (err) 4885220deb96SPaul Moore return err; 4886d8395c87SPaul Moore err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad); 4887220deb96SPaul Moore 48884e5ab4cbSJames Morris return err; 48894e5ab4cbSJames Morris } 4890d28d1e08STrent Jaeger 48914e5ab4cbSJames Morris static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) 48924e5ab4cbSJames Morris { 4893220deb96SPaul Moore int err; 48944237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 4895220deb96SPaul Moore u16 family = sk->sk_family; 4896220deb96SPaul Moore u32 sk_sid = sksec->sid; 48972bf49690SThomas Liu struct common_audit_data ad; 489848c62af6SEric Paris struct lsm_network_audit net = {0,}; 4899220deb96SPaul Moore char *addrp; 4900d8395c87SPaul Moore u8 secmark_active; 4901d8395c87SPaul Moore u8 peerlbl_active; 49024e5ab4cbSJames Morris 49034e5ab4cbSJames Morris if (family != PF_INET && family != PF_INET6) 4904220deb96SPaul Moore return 0; 49054e5ab4cbSJames Morris 49064e5ab4cbSJames Morris /* Handle mapped IPv4 packets arriving via IPv6 sockets */ 490787fcd70dSAl Viro if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 49084e5ab4cbSJames Morris family = PF_INET; 49094e5ab4cbSJames Morris 4910d8395c87SPaul Moore /* If any sort of compatibility mode is enabled then handoff processing 4911d8395c87SPaul Moore * to the selinux_sock_rcv_skb_compat() function to deal with the 4912d8395c87SPaul Moore * special handling. We do this in an attempt to keep this function 4913d8395c87SPaul Moore * as fast and as clean as possible. */ 4914aa8e712cSStephen Smalley if (!selinux_policycap_netpeer()) 4915d8395c87SPaul Moore return selinux_sock_rcv_skb_compat(sk, skb, family); 4916d8395c87SPaul Moore 4917d8395c87SPaul Moore secmark_active = selinux_secmark_enabled(); 49182be4d74fSChris PeBenito peerlbl_active = selinux_peerlbl_enabled(); 4919d8395c87SPaul Moore if (!secmark_active && !peerlbl_active) 4920d8395c87SPaul Moore return 0; 4921d8395c87SPaul Moore 492250c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 492348c62af6SEric Paris ad.u.net = &net; 492448c62af6SEric Paris ad.u.net->netif = skb->skb_iif; 492548c62af6SEric Paris ad.u.net->family = family; 4926224dfbd8SPaul Moore err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); 49274e5ab4cbSJames Morris if (err) 4928220deb96SPaul Moore return err; 49294e5ab4cbSJames Morris 4930d8395c87SPaul Moore if (peerlbl_active) { 4931d621d35eSPaul Moore u32 peer_sid; 4932220deb96SPaul Moore 4933220deb96SPaul Moore err = selinux_skb_peerlbl_sid(skb, family, &peer_sid); 4934220deb96SPaul Moore if (err) 4935220deb96SPaul Moore return err; 4936cbe0d6e8SPaul Moore err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif, 4937cbe0d6e8SPaul Moore addrp, family, peer_sid, &ad); 4938dfaebe98SPaul Moore if (err) { 4939a04e71f6SHuw Davies selinux_netlbl_err(skb, family, err, 0); 4940effad8dfSPaul Moore return err; 4941dfaebe98SPaul Moore } 49426b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 49436b6bc620SStephen Smalley sk_sid, peer_sid, SECCLASS_PEER, 4944d621d35eSPaul Moore PEER__RECV, &ad); 494546d01d63SChad Hanson if (err) { 4946a04e71f6SHuw Davies selinux_netlbl_err(skb, family, err, 0); 494746d01d63SChad Hanson return err; 494846d01d63SChad Hanson } 4949d621d35eSPaul Moore } 4950d621d35eSPaul Moore 4951d8395c87SPaul Moore if (secmark_active) { 49526b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 49536b6bc620SStephen Smalley sk_sid, skb->secmark, SECCLASS_PACKET, 4954effad8dfSPaul Moore PACKET__RECV, &ad); 4955effad8dfSPaul Moore if (err) 4956effad8dfSPaul Moore return err; 4957effad8dfSPaul Moore } 4958effad8dfSPaul Moore 4959d621d35eSPaul Moore return err; 49601da177e4SLinus Torvalds } 49611da177e4SLinus Torvalds 49622c7946a7SCatherine Zhang static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval, 49631da177e4SLinus Torvalds int __user *optlen, unsigned len) 49641da177e4SLinus Torvalds { 49651da177e4SLinus Torvalds int err = 0; 49661da177e4SLinus Torvalds char *scontext; 49671da177e4SLinus Torvalds u32 scontext_len; 4968253bfae6SPaul Moore struct sk_security_struct *sksec = sock->sk->sk_security; 49693de4bab5SPaul Moore u32 peer_sid = SECSID_NULL; 49701da177e4SLinus Torvalds 4971253bfae6SPaul Moore if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET || 4972d452930fSRichard Haines sksec->sclass == SECCLASS_TCP_SOCKET || 4973d452930fSRichard Haines sksec->sclass == SECCLASS_SCTP_SOCKET) 4974dd3e7836SEric Paris peer_sid = sksec->peer_sid; 4975253bfae6SPaul Moore if (peer_sid == SECSID_NULL) 4976253bfae6SPaul Moore return -ENOPROTOOPT; 49771da177e4SLinus Torvalds 4978aa8e712cSStephen Smalley err = security_sid_to_context(&selinux_state, peer_sid, &scontext, 4979aa8e712cSStephen Smalley &scontext_len); 49801da177e4SLinus Torvalds if (err) 4981253bfae6SPaul Moore return err; 49821da177e4SLinus Torvalds 49831da177e4SLinus Torvalds if (scontext_len > len) { 49841da177e4SLinus Torvalds err = -ERANGE; 49851da177e4SLinus Torvalds goto out_len; 49861da177e4SLinus Torvalds } 49871da177e4SLinus Torvalds 49881da177e4SLinus Torvalds if (copy_to_user(optval, scontext, scontext_len)) 49891da177e4SLinus Torvalds err = -EFAULT; 49901da177e4SLinus Torvalds 49911da177e4SLinus Torvalds out_len: 49921da177e4SLinus Torvalds if (put_user(scontext_len, optlen)) 49931da177e4SLinus Torvalds err = -EFAULT; 49941da177e4SLinus Torvalds kfree(scontext); 49951da177e4SLinus Torvalds return err; 49961da177e4SLinus Torvalds } 49971da177e4SLinus Torvalds 4998dc49c1f9SCatherine Zhang static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid) 49992c7946a7SCatherine Zhang { 5000dc49c1f9SCatherine Zhang u32 peer_secid = SECSID_NULL; 500175e22910SPaul Moore u16 family; 5002899134f2SPaul Moore struct inode_security_struct *isec; 5003877ce7c1SCatherine Zhang 5004aa862900SPaul Moore if (skb && skb->protocol == htons(ETH_P_IP)) 5005aa862900SPaul Moore family = PF_INET; 5006aa862900SPaul Moore else if (skb && skb->protocol == htons(ETH_P_IPV6)) 5007aa862900SPaul Moore family = PF_INET6; 5008aa862900SPaul Moore else if (sock) 500975e22910SPaul Moore family = sock->sk->sk_family; 501075e22910SPaul Moore else 501175e22910SPaul Moore goto out; 501275e22910SPaul Moore 5013899134f2SPaul Moore if (sock && family == PF_UNIX) { 5014899134f2SPaul Moore isec = inode_security_novalidate(SOCK_INODE(sock)); 5015899134f2SPaul Moore peer_secid = isec->sid; 5016899134f2SPaul Moore } else if (skb) 5017220deb96SPaul Moore selinux_skb_peerlbl_sid(skb, family, &peer_secid); 50182c7946a7SCatherine Zhang 501975e22910SPaul Moore out: 5020dc49c1f9SCatherine Zhang *secid = peer_secid; 502175e22910SPaul Moore if (peer_secid == SECSID_NULL) 502275e22910SPaul Moore return -EINVAL; 502375e22910SPaul Moore return 0; 50242c7946a7SCatherine Zhang } 50252c7946a7SCatherine Zhang 50267d877f3bSAl Viro static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority) 50271da177e4SLinus Torvalds { 502884914b7eSPaul Moore struct sk_security_struct *sksec; 502984914b7eSPaul Moore 503084914b7eSPaul Moore sksec = kzalloc(sizeof(*sksec), priority); 503184914b7eSPaul Moore if (!sksec) 503284914b7eSPaul Moore return -ENOMEM; 503384914b7eSPaul Moore 503484914b7eSPaul Moore sksec->peer_sid = SECINITSID_UNLABELED; 503584914b7eSPaul Moore sksec->sid = SECINITSID_UNLABELED; 50365dee25d0SStephen Smalley sksec->sclass = SECCLASS_SOCKET; 503784914b7eSPaul Moore selinux_netlbl_sk_security_reset(sksec); 503884914b7eSPaul Moore sk->sk_security = sksec; 503984914b7eSPaul Moore 504084914b7eSPaul Moore return 0; 50411da177e4SLinus Torvalds } 50421da177e4SLinus Torvalds 50431da177e4SLinus Torvalds static void selinux_sk_free_security(struct sock *sk) 50441da177e4SLinus Torvalds { 504584914b7eSPaul Moore struct sk_security_struct *sksec = sk->sk_security; 504684914b7eSPaul Moore 504784914b7eSPaul Moore sk->sk_security = NULL; 504884914b7eSPaul Moore selinux_netlbl_sk_security_free(sksec); 504984914b7eSPaul Moore kfree(sksec); 50501da177e4SLinus Torvalds } 50511da177e4SLinus Torvalds 5052892c141eSVenkat Yekkirala static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk) 5053892c141eSVenkat Yekkirala { 5054dd3e7836SEric Paris struct sk_security_struct *sksec = sk->sk_security; 5055dd3e7836SEric Paris struct sk_security_struct *newsksec = newsk->sk_security; 5056892c141eSVenkat Yekkirala 5057dd3e7836SEric Paris newsksec->sid = sksec->sid; 5058dd3e7836SEric Paris newsksec->peer_sid = sksec->peer_sid; 5059dd3e7836SEric Paris newsksec->sclass = sksec->sclass; 506099f59ed0SPaul Moore 5061dd3e7836SEric Paris selinux_netlbl_sk_security_reset(newsksec); 5062892c141eSVenkat Yekkirala } 5063892c141eSVenkat Yekkirala 5064beb8d13bSVenkat Yekkirala static void selinux_sk_getsecid(struct sock *sk, u32 *secid) 5065d28d1e08STrent Jaeger { 5066d28d1e08STrent Jaeger if (!sk) 5067beb8d13bSVenkat Yekkirala *secid = SECINITSID_ANY_SOCKET; 5068892c141eSVenkat Yekkirala else { 5069892c141eSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 5070d28d1e08STrent Jaeger 5071beb8d13bSVenkat Yekkirala *secid = sksec->sid; 5072892c141eSVenkat Yekkirala } 5073d28d1e08STrent Jaeger } 5074d28d1e08STrent Jaeger 50759a673e56SAdrian Bunk static void selinux_sock_graft(struct sock *sk, struct socket *parent) 50764237c75cSVenkat Yekkirala { 50775d226df4SAndreas Gruenbacher struct inode_security_struct *isec = 50785d226df4SAndreas Gruenbacher inode_security_novalidate(SOCK_INODE(parent)); 50794237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 50804237c75cSVenkat Yekkirala 50812873ead7SPaul Moore if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 || 50822873ead7SPaul Moore sk->sk_family == PF_UNIX) 50834237c75cSVenkat Yekkirala isec->sid = sksec->sid; 5084220deb96SPaul Moore sksec->sclass = isec->sclass; 50854237c75cSVenkat Yekkirala } 50864237c75cSVenkat Yekkirala 5087d452930fSRichard Haines /* Called whenever SCTP receives an INIT chunk. This happens when an incoming 5088d452930fSRichard Haines * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association 5089d452930fSRichard Haines * already present). 5090d452930fSRichard Haines */ 5091d452930fSRichard Haines static int selinux_sctp_assoc_request(struct sctp_endpoint *ep, 5092d452930fSRichard Haines struct sk_buff *skb) 5093d452930fSRichard Haines { 5094d452930fSRichard Haines struct sk_security_struct *sksec = ep->base.sk->sk_security; 5095d452930fSRichard Haines struct common_audit_data ad; 5096d452930fSRichard Haines struct lsm_network_audit net = {0,}; 5097d452930fSRichard Haines u8 peerlbl_active; 5098d452930fSRichard Haines u32 peer_sid = SECINITSID_UNLABELED; 5099d452930fSRichard Haines u32 conn_sid; 5100d452930fSRichard Haines int err = 0; 5101d452930fSRichard Haines 5102aa8e712cSStephen Smalley if (!selinux_policycap_extsockclass()) 5103d452930fSRichard Haines return 0; 5104d452930fSRichard Haines 5105d452930fSRichard Haines peerlbl_active = selinux_peerlbl_enabled(); 5106d452930fSRichard Haines 5107d452930fSRichard Haines if (peerlbl_active) { 5108d452930fSRichard Haines /* This will return peer_sid = SECSID_NULL if there are 5109d452930fSRichard Haines * no peer labels, see security_net_peersid_resolve(). 5110d452930fSRichard Haines */ 5111d452930fSRichard Haines err = selinux_skb_peerlbl_sid(skb, ep->base.sk->sk_family, 5112d452930fSRichard Haines &peer_sid); 5113d452930fSRichard Haines if (err) 5114d452930fSRichard Haines return err; 5115d452930fSRichard Haines 5116d452930fSRichard Haines if (peer_sid == SECSID_NULL) 5117d452930fSRichard Haines peer_sid = SECINITSID_UNLABELED; 5118d452930fSRichard Haines } 5119d452930fSRichard Haines 5120d452930fSRichard Haines if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) { 5121d452930fSRichard Haines sksec->sctp_assoc_state = SCTP_ASSOC_SET; 5122d452930fSRichard Haines 5123d452930fSRichard Haines /* Here as first association on socket. As the peer SID 5124d452930fSRichard Haines * was allowed by peer recv (and the netif/node checks), 5125d452930fSRichard Haines * then it is approved by policy and used as the primary 5126d452930fSRichard Haines * peer SID for getpeercon(3). 5127d452930fSRichard Haines */ 5128d452930fSRichard Haines sksec->peer_sid = peer_sid; 5129d452930fSRichard Haines } else if (sksec->peer_sid != peer_sid) { 5130d452930fSRichard Haines /* Other association peer SIDs are checked to enforce 5131d452930fSRichard Haines * consistency among the peer SIDs. 5132d452930fSRichard Haines */ 5133d452930fSRichard Haines ad.type = LSM_AUDIT_DATA_NET; 5134d452930fSRichard Haines ad.u.net = &net; 5135d452930fSRichard Haines ad.u.net->sk = ep->base.sk; 51366b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 51376b6bc620SStephen Smalley sksec->peer_sid, peer_sid, sksec->sclass, 5138d452930fSRichard Haines SCTP_SOCKET__ASSOCIATION, &ad); 5139d452930fSRichard Haines if (err) 5140d452930fSRichard Haines return err; 5141d452930fSRichard Haines } 5142d452930fSRichard Haines 5143d452930fSRichard Haines /* Compute the MLS component for the connection and store 5144d452930fSRichard Haines * the information in ep. This will be used by SCTP TCP type 5145d452930fSRichard Haines * sockets and peeled off connections as they cause a new 5146d452930fSRichard Haines * socket to be generated. selinux_sctp_sk_clone() will then 5147d452930fSRichard Haines * plug this into the new socket. 5148d452930fSRichard Haines */ 5149d452930fSRichard Haines err = selinux_conn_sid(sksec->sid, peer_sid, &conn_sid); 5150d452930fSRichard Haines if (err) 5151d452930fSRichard Haines return err; 5152d452930fSRichard Haines 5153d452930fSRichard Haines ep->secid = conn_sid; 5154d452930fSRichard Haines ep->peer_secid = peer_sid; 5155d452930fSRichard Haines 5156d452930fSRichard Haines /* Set any NetLabel labels including CIPSO/CALIPSO options. */ 5157d452930fSRichard Haines return selinux_netlbl_sctp_assoc_request(ep, skb); 5158d452930fSRichard Haines } 5159d452930fSRichard Haines 5160d452930fSRichard Haines /* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting 5161d452930fSRichard Haines * based on their @optname. 5162d452930fSRichard Haines */ 5163d452930fSRichard Haines static int selinux_sctp_bind_connect(struct sock *sk, int optname, 5164d452930fSRichard Haines struct sockaddr *address, 5165d452930fSRichard Haines int addrlen) 5166d452930fSRichard Haines { 5167d452930fSRichard Haines int len, err = 0, walk_size = 0; 5168d452930fSRichard Haines void *addr_buf; 5169d452930fSRichard Haines struct sockaddr *addr; 5170d452930fSRichard Haines struct socket *sock; 5171d452930fSRichard Haines 5172aa8e712cSStephen Smalley if (!selinux_policycap_extsockclass()) 5173d452930fSRichard Haines return 0; 5174d452930fSRichard Haines 5175d452930fSRichard Haines /* Process one or more addresses that may be IPv4 or IPv6 */ 5176d452930fSRichard Haines sock = sk->sk_socket; 5177d452930fSRichard Haines addr_buf = address; 5178d452930fSRichard Haines 5179d452930fSRichard Haines while (walk_size < addrlen) { 5180c138325fSOndrej Mosnacek if (walk_size + sizeof(sa_family_t) > addrlen) 5181c138325fSOndrej Mosnacek return -EINVAL; 5182c138325fSOndrej Mosnacek 5183d452930fSRichard Haines addr = addr_buf; 5184d452930fSRichard Haines switch (addr->sa_family) { 51854152dc91SAlexey Kodanev case AF_UNSPEC: 5186d452930fSRichard Haines case AF_INET: 5187d452930fSRichard Haines len = sizeof(struct sockaddr_in); 5188d452930fSRichard Haines break; 5189d452930fSRichard Haines case AF_INET6: 5190d452930fSRichard Haines len = sizeof(struct sockaddr_in6); 5191d452930fSRichard Haines break; 5192d452930fSRichard Haines default: 51934152dc91SAlexey Kodanev return -EINVAL; 5194d452930fSRichard Haines } 5195d452930fSRichard Haines 5196d452930fSRichard Haines err = -EINVAL; 5197d452930fSRichard Haines switch (optname) { 5198d452930fSRichard Haines /* Bind checks */ 5199d452930fSRichard Haines case SCTP_PRIMARY_ADDR: 5200d452930fSRichard Haines case SCTP_SET_PEER_PRIMARY_ADDR: 5201d452930fSRichard Haines case SCTP_SOCKOPT_BINDX_ADD: 5202d452930fSRichard Haines err = selinux_socket_bind(sock, addr, len); 5203d452930fSRichard Haines break; 5204d452930fSRichard Haines /* Connect checks */ 5205d452930fSRichard Haines case SCTP_SOCKOPT_CONNECTX: 5206d452930fSRichard Haines case SCTP_PARAM_SET_PRIMARY: 5207d452930fSRichard Haines case SCTP_PARAM_ADD_IP: 5208d452930fSRichard Haines case SCTP_SENDMSG_CONNECT: 5209d452930fSRichard Haines err = selinux_socket_connect_helper(sock, addr, len); 5210d452930fSRichard Haines if (err) 5211d452930fSRichard Haines return err; 5212d452930fSRichard Haines 5213d452930fSRichard Haines /* As selinux_sctp_bind_connect() is called by the 5214d452930fSRichard Haines * SCTP protocol layer, the socket is already locked, 5215d452930fSRichard Haines * therefore selinux_netlbl_socket_connect_locked() is 5216d452930fSRichard Haines * is called here. The situations handled are: 5217d452930fSRichard Haines * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2), 5218d452930fSRichard Haines * whenever a new IP address is added or when a new 5219d452930fSRichard Haines * primary address is selected. 5220d452930fSRichard Haines * Note that an SCTP connect(2) call happens before 5221d452930fSRichard Haines * the SCTP protocol layer and is handled via 5222d452930fSRichard Haines * selinux_socket_connect(). 5223d452930fSRichard Haines */ 5224d452930fSRichard Haines err = selinux_netlbl_socket_connect_locked(sk, addr); 5225d452930fSRichard Haines break; 5226d452930fSRichard Haines } 5227d452930fSRichard Haines 5228d452930fSRichard Haines if (err) 5229d452930fSRichard Haines return err; 5230d452930fSRichard Haines 5231d452930fSRichard Haines addr_buf += len; 5232d452930fSRichard Haines walk_size += len; 5233d452930fSRichard Haines } 5234d452930fSRichard Haines 5235d452930fSRichard Haines return 0; 5236d452930fSRichard Haines } 5237d452930fSRichard Haines 5238d452930fSRichard Haines /* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */ 5239d452930fSRichard Haines static void selinux_sctp_sk_clone(struct sctp_endpoint *ep, struct sock *sk, 5240d452930fSRichard Haines struct sock *newsk) 5241d452930fSRichard Haines { 5242d452930fSRichard Haines struct sk_security_struct *sksec = sk->sk_security; 5243d452930fSRichard Haines struct sk_security_struct *newsksec = newsk->sk_security; 5244d452930fSRichard Haines 5245d452930fSRichard Haines /* If policy does not support SECCLASS_SCTP_SOCKET then call 5246d452930fSRichard Haines * the non-sctp clone version. 5247d452930fSRichard Haines */ 5248aa8e712cSStephen Smalley if (!selinux_policycap_extsockclass()) 5249d452930fSRichard Haines return selinux_sk_clone_security(sk, newsk); 5250d452930fSRichard Haines 5251d452930fSRichard Haines newsksec->sid = ep->secid; 5252d452930fSRichard Haines newsksec->peer_sid = ep->peer_secid; 5253d452930fSRichard Haines newsksec->sclass = sksec->sclass; 5254d452930fSRichard Haines selinux_netlbl_sctp_sk_clone(sk, newsk); 5255d452930fSRichard Haines } 5256d452930fSRichard Haines 52579a673e56SAdrian Bunk static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb, 52584237c75cSVenkat Yekkirala struct request_sock *req) 52594237c75cSVenkat Yekkirala { 52604237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 52614237c75cSVenkat Yekkirala int err; 52620b1f24e6SPaul Moore u16 family = req->rsk_ops->family; 5263446b8024SPaul Moore u32 connsid; 52644237c75cSVenkat Yekkirala u32 peersid; 52654237c75cSVenkat Yekkirala 5266aa862900SPaul Moore err = selinux_skb_peerlbl_sid(skb, family, &peersid); 5267220deb96SPaul Moore if (err) 5268220deb96SPaul Moore return err; 5269446b8024SPaul Moore err = selinux_conn_sid(sksec->sid, peersid, &connsid); 52704237c75cSVenkat Yekkirala if (err) 52714237c75cSVenkat Yekkirala return err; 5272446b8024SPaul Moore req->secid = connsid; 52736b877699SVenkat Yekkirala req->peer_secid = peersid; 5274389fb800SPaul Moore 5275389fb800SPaul Moore return selinux_netlbl_inet_conn_request(req, family); 52764237c75cSVenkat Yekkirala } 52774237c75cSVenkat Yekkirala 52789a673e56SAdrian Bunk static void selinux_inet_csk_clone(struct sock *newsk, 52799a673e56SAdrian Bunk const struct request_sock *req) 52804237c75cSVenkat Yekkirala { 52814237c75cSVenkat Yekkirala struct sk_security_struct *newsksec = newsk->sk_security; 52824237c75cSVenkat Yekkirala 52834237c75cSVenkat Yekkirala newsksec->sid = req->secid; 52846b877699SVenkat Yekkirala newsksec->peer_sid = req->peer_secid; 52854237c75cSVenkat Yekkirala /* NOTE: Ideally, we should also get the isec->sid for the 52864237c75cSVenkat Yekkirala new socket in sync, but we don't have the isec available yet. 52874237c75cSVenkat Yekkirala So we will wait until sock_graft to do it, by which 52884237c75cSVenkat Yekkirala time it will have been created and available. */ 528999f59ed0SPaul Moore 52909f2ad665SPaul Moore /* We don't need to take any sort of lock here as we are the only 52919f2ad665SPaul Moore * thread with access to newsksec */ 5292389fb800SPaul Moore selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family); 52934237c75cSVenkat Yekkirala } 52944237c75cSVenkat Yekkirala 5295014ab19aSPaul Moore static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb) 52966b877699SVenkat Yekkirala { 5297aa862900SPaul Moore u16 family = sk->sk_family; 52986b877699SVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 52996b877699SVenkat Yekkirala 5300aa862900SPaul Moore /* handle mapped IPv4 packets arriving via IPv6 sockets */ 5301aa862900SPaul Moore if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 5302aa862900SPaul Moore family = PF_INET; 5303aa862900SPaul Moore 5304aa862900SPaul Moore selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid); 53056b877699SVenkat Yekkirala } 53066b877699SVenkat Yekkirala 53072606fd1fSEric Paris static int selinux_secmark_relabel_packet(u32 sid) 53082606fd1fSEric Paris { 53092606fd1fSEric Paris const struct task_security_struct *__tsec; 53102606fd1fSEric Paris u32 tsid; 53112606fd1fSEric Paris 53122606fd1fSEric Paris __tsec = current_security(); 53132606fd1fSEric Paris tsid = __tsec->sid; 53142606fd1fSEric Paris 53156b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 53166b6bc620SStephen Smalley tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, 53176b6bc620SStephen Smalley NULL); 53182606fd1fSEric Paris } 53192606fd1fSEric Paris 53202606fd1fSEric Paris static void selinux_secmark_refcount_inc(void) 53212606fd1fSEric Paris { 53222606fd1fSEric Paris atomic_inc(&selinux_secmark_refcount); 53232606fd1fSEric Paris } 53242606fd1fSEric Paris 53252606fd1fSEric Paris static void selinux_secmark_refcount_dec(void) 53262606fd1fSEric Paris { 53272606fd1fSEric Paris atomic_dec(&selinux_secmark_refcount); 53282606fd1fSEric Paris } 53292606fd1fSEric Paris 53309a673e56SAdrian Bunk static void selinux_req_classify_flow(const struct request_sock *req, 53319a673e56SAdrian Bunk struct flowi *fl) 53324237c75cSVenkat Yekkirala { 53331d28f42cSDavid S. Miller fl->flowi_secid = req->secid; 53344237c75cSVenkat Yekkirala } 53354237c75cSVenkat Yekkirala 53365dbbaf2dSPaul Moore static int selinux_tun_dev_alloc_security(void **security) 53375dbbaf2dSPaul Moore { 53385dbbaf2dSPaul Moore struct tun_security_struct *tunsec; 53395dbbaf2dSPaul Moore 53405dbbaf2dSPaul Moore tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL); 53415dbbaf2dSPaul Moore if (!tunsec) 53425dbbaf2dSPaul Moore return -ENOMEM; 53435dbbaf2dSPaul Moore tunsec->sid = current_sid(); 53445dbbaf2dSPaul Moore 53455dbbaf2dSPaul Moore *security = tunsec; 53465dbbaf2dSPaul Moore return 0; 53475dbbaf2dSPaul Moore } 53485dbbaf2dSPaul Moore 53495dbbaf2dSPaul Moore static void selinux_tun_dev_free_security(void *security) 53505dbbaf2dSPaul Moore { 53515dbbaf2dSPaul Moore kfree(security); 53525dbbaf2dSPaul Moore } 53535dbbaf2dSPaul Moore 5354ed6d76e4SPaul Moore static int selinux_tun_dev_create(void) 5355ed6d76e4SPaul Moore { 5356ed6d76e4SPaul Moore u32 sid = current_sid(); 5357ed6d76e4SPaul Moore 5358ed6d76e4SPaul Moore /* we aren't taking into account the "sockcreate" SID since the socket 5359ed6d76e4SPaul Moore * that is being created here is not a socket in the traditional sense, 5360ed6d76e4SPaul Moore * instead it is a private sock, accessible only to the kernel, and 5361ed6d76e4SPaul Moore * representing a wide range of network traffic spanning multiple 5362ed6d76e4SPaul Moore * connections unlike traditional sockets - check the TUN driver to 5363ed6d76e4SPaul Moore * get a better understanding of why this socket is special */ 5364ed6d76e4SPaul Moore 53656b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 53666b6bc620SStephen Smalley sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE, 5367ed6d76e4SPaul Moore NULL); 5368ed6d76e4SPaul Moore } 5369ed6d76e4SPaul Moore 53705dbbaf2dSPaul Moore static int selinux_tun_dev_attach_queue(void *security) 5371ed6d76e4SPaul Moore { 53725dbbaf2dSPaul Moore struct tun_security_struct *tunsec = security; 53735dbbaf2dSPaul Moore 53746b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 53756b6bc620SStephen Smalley current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET, 53765dbbaf2dSPaul Moore TUN_SOCKET__ATTACH_QUEUE, NULL); 53775dbbaf2dSPaul Moore } 53785dbbaf2dSPaul Moore 53795dbbaf2dSPaul Moore static int selinux_tun_dev_attach(struct sock *sk, void *security) 53805dbbaf2dSPaul Moore { 53815dbbaf2dSPaul Moore struct tun_security_struct *tunsec = security; 5382ed6d76e4SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 5383ed6d76e4SPaul Moore 5384ed6d76e4SPaul Moore /* we don't currently perform any NetLabel based labeling here and it 5385ed6d76e4SPaul Moore * isn't clear that we would want to do so anyway; while we could apply 5386ed6d76e4SPaul Moore * labeling without the support of the TUN user the resulting labeled 5387ed6d76e4SPaul Moore * traffic from the other end of the connection would almost certainly 5388ed6d76e4SPaul Moore * cause confusion to the TUN user that had no idea network labeling 5389ed6d76e4SPaul Moore * protocols were being used */ 5390ed6d76e4SPaul Moore 53915dbbaf2dSPaul Moore sksec->sid = tunsec->sid; 5392ed6d76e4SPaul Moore sksec->sclass = SECCLASS_TUN_SOCKET; 53935dbbaf2dSPaul Moore 53945dbbaf2dSPaul Moore return 0; 5395ed6d76e4SPaul Moore } 5396ed6d76e4SPaul Moore 53975dbbaf2dSPaul Moore static int selinux_tun_dev_open(void *security) 5398ed6d76e4SPaul Moore { 53995dbbaf2dSPaul Moore struct tun_security_struct *tunsec = security; 5400ed6d76e4SPaul Moore u32 sid = current_sid(); 5401ed6d76e4SPaul Moore int err; 5402ed6d76e4SPaul Moore 54036b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 54046b6bc620SStephen Smalley sid, tunsec->sid, SECCLASS_TUN_SOCKET, 5405ed6d76e4SPaul Moore TUN_SOCKET__RELABELFROM, NULL); 5406ed6d76e4SPaul Moore if (err) 5407ed6d76e4SPaul Moore return err; 54086b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 54096b6bc620SStephen Smalley sid, sid, SECCLASS_TUN_SOCKET, 5410ed6d76e4SPaul Moore TUN_SOCKET__RELABELTO, NULL); 5411ed6d76e4SPaul Moore if (err) 5412ed6d76e4SPaul Moore return err; 54135dbbaf2dSPaul Moore tunsec->sid = sid; 5414ed6d76e4SPaul Moore 5415ed6d76e4SPaul Moore return 0; 5416ed6d76e4SPaul Moore } 5417ed6d76e4SPaul Moore 54181da177e4SLinus Torvalds static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb) 54191da177e4SLinus Torvalds { 54201da177e4SLinus Torvalds int err = 0; 54211da177e4SLinus Torvalds u32 perm; 54221da177e4SLinus Torvalds struct nlmsghdr *nlh; 5423253bfae6SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 54241da177e4SLinus Torvalds 542577954983SHong zhi guo if (skb->len < NLMSG_HDRLEN) { 54261da177e4SLinus Torvalds err = -EINVAL; 54271da177e4SLinus Torvalds goto out; 54281da177e4SLinus Torvalds } 5429b529ccf2SArnaldo Carvalho de Melo nlh = nlmsg_hdr(skb); 54301da177e4SLinus Torvalds 5431253bfae6SPaul Moore err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm); 54321da177e4SLinus Torvalds if (err) { 54331da177e4SLinus Torvalds if (err == -EINVAL) { 543476319946SVladis Dronov pr_warn_ratelimited("SELinux: unrecognized netlink" 543576319946SVladis Dronov " message: protocol=%hu nlmsg_type=%hu sclass=%s" 543676319946SVladis Dronov " pig=%d comm=%s\n", 5437cded3fffSMarek Milkovic sk->sk_protocol, nlh->nlmsg_type, 543876319946SVladis Dronov secclass_map[sksec->sclass - 1].name, 543976319946SVladis Dronov task_pid_nr(current), current->comm); 5440e5a5ca96SPaul Moore if (!enforcing_enabled(&selinux_state) || 5441aa8e712cSStephen Smalley security_get_allow_unknown(&selinux_state)) 54421da177e4SLinus Torvalds err = 0; 54431da177e4SLinus Torvalds } 54441da177e4SLinus Torvalds 54451da177e4SLinus Torvalds /* Ignore */ 54461da177e4SLinus Torvalds if (err == -ENOENT) 54471da177e4SLinus Torvalds err = 0; 54481da177e4SLinus Torvalds goto out; 54491da177e4SLinus Torvalds } 54501da177e4SLinus Torvalds 5451be0554c9SStephen Smalley err = sock_has_perm(sk, perm); 54521da177e4SLinus Torvalds out: 54531da177e4SLinus Torvalds return err; 54541da177e4SLinus Torvalds } 54551da177e4SLinus Torvalds 54561da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER 54571da177e4SLinus Torvalds 5458cbe0d6e8SPaul Moore static unsigned int selinux_ip_forward(struct sk_buff *skb, 5459cbe0d6e8SPaul Moore const struct net_device *indev, 5460effad8dfSPaul Moore u16 family) 54611da177e4SLinus Torvalds { 5462dfaebe98SPaul Moore int err; 5463effad8dfSPaul Moore char *addrp; 5464effad8dfSPaul Moore u32 peer_sid; 54652bf49690SThomas Liu struct common_audit_data ad; 546648c62af6SEric Paris struct lsm_network_audit net = {0,}; 5467effad8dfSPaul Moore u8 secmark_active; 5468948bf85cSPaul Moore u8 netlbl_active; 5469effad8dfSPaul Moore u8 peerlbl_active; 54704237c75cSVenkat Yekkirala 5471aa8e712cSStephen Smalley if (!selinux_policycap_netpeer()) 5472effad8dfSPaul Moore return NF_ACCEPT; 54734237c75cSVenkat Yekkirala 5474effad8dfSPaul Moore secmark_active = selinux_secmark_enabled(); 5475948bf85cSPaul Moore netlbl_active = netlbl_enabled(); 54762be4d74fSChris PeBenito peerlbl_active = selinux_peerlbl_enabled(); 5477effad8dfSPaul Moore if (!secmark_active && !peerlbl_active) 5478effad8dfSPaul Moore return NF_ACCEPT; 54794237c75cSVenkat Yekkirala 5480d8395c87SPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0) 5481d8395c87SPaul Moore return NF_DROP; 5482d8395c87SPaul Moore 548350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 548448c62af6SEric Paris ad.u.net = &net; 5485cbe0d6e8SPaul Moore ad.u.net->netif = indev->ifindex; 548648c62af6SEric Paris ad.u.net->family = family; 5487effad8dfSPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0) 5488effad8dfSPaul Moore return NF_DROP; 54891da177e4SLinus Torvalds 5490dfaebe98SPaul Moore if (peerlbl_active) { 5491cbe0d6e8SPaul Moore err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex, 5492cbe0d6e8SPaul Moore addrp, family, peer_sid, &ad); 5493dfaebe98SPaul Moore if (err) { 5494a04e71f6SHuw Davies selinux_netlbl_err(skb, family, err, 1); 5495effad8dfSPaul Moore return NF_DROP; 5496dfaebe98SPaul Moore } 5497dfaebe98SPaul Moore } 5498effad8dfSPaul Moore 5499effad8dfSPaul Moore if (secmark_active) 55006b6bc620SStephen Smalley if (avc_has_perm(&selinux_state, 55016b6bc620SStephen Smalley peer_sid, skb->secmark, 5502effad8dfSPaul Moore SECCLASS_PACKET, PACKET__FORWARD_IN, &ad)) 5503effad8dfSPaul Moore return NF_DROP; 5504effad8dfSPaul Moore 5505948bf85cSPaul Moore if (netlbl_active) 5506948bf85cSPaul Moore /* we do this in the FORWARD path and not the POST_ROUTING 5507948bf85cSPaul Moore * path because we want to make sure we apply the necessary 5508948bf85cSPaul Moore * labeling before IPsec is applied so we can leverage AH 5509948bf85cSPaul Moore * protection */ 5510948bf85cSPaul Moore if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0) 5511948bf85cSPaul Moore return NF_DROP; 5512948bf85cSPaul Moore 5513effad8dfSPaul Moore return NF_ACCEPT; 5514effad8dfSPaul Moore } 5515effad8dfSPaul Moore 551606198b34SEric W. Biederman static unsigned int selinux_ipv4_forward(void *priv, 5517effad8dfSPaul Moore struct sk_buff *skb, 5518238e54c9SDavid S. Miller const struct nf_hook_state *state) 5519effad8dfSPaul Moore { 5520238e54c9SDavid S. Miller return selinux_ip_forward(skb, state->in, PF_INET); 5521effad8dfSPaul Moore } 5522effad8dfSPaul Moore 55231a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 552406198b34SEric W. Biederman static unsigned int selinux_ipv6_forward(void *priv, 5525effad8dfSPaul Moore struct sk_buff *skb, 5526238e54c9SDavid S. Miller const struct nf_hook_state *state) 5527effad8dfSPaul Moore { 5528238e54c9SDavid S. Miller return selinux_ip_forward(skb, state->in, PF_INET6); 5529effad8dfSPaul Moore } 5530effad8dfSPaul Moore #endif /* IPV6 */ 5531effad8dfSPaul Moore 5532948bf85cSPaul Moore static unsigned int selinux_ip_output(struct sk_buff *skb, 5533948bf85cSPaul Moore u16 family) 5534948bf85cSPaul Moore { 553547180068SPaul Moore struct sock *sk; 5536948bf85cSPaul Moore u32 sid; 5537948bf85cSPaul Moore 5538948bf85cSPaul Moore if (!netlbl_enabled()) 5539948bf85cSPaul Moore return NF_ACCEPT; 5540948bf85cSPaul Moore 5541948bf85cSPaul Moore /* we do this in the LOCAL_OUT path and not the POST_ROUTING path 5542948bf85cSPaul Moore * because we want to make sure we apply the necessary labeling 5543948bf85cSPaul Moore * before IPsec is applied so we can leverage AH protection */ 554447180068SPaul Moore sk = skb->sk; 554547180068SPaul Moore if (sk) { 554647180068SPaul Moore struct sk_security_struct *sksec; 554747180068SPaul Moore 5548e446f9dfSEric Dumazet if (sk_listener(sk)) 554947180068SPaul Moore /* if the socket is the listening state then this 555047180068SPaul Moore * packet is a SYN-ACK packet which means it needs to 555147180068SPaul Moore * be labeled based on the connection/request_sock and 555247180068SPaul Moore * not the parent socket. unfortunately, we can't 555347180068SPaul Moore * lookup the request_sock yet as it isn't queued on 555447180068SPaul Moore * the parent socket until after the SYN-ACK is sent. 555547180068SPaul Moore * the "solution" is to simply pass the packet as-is 555647180068SPaul Moore * as any IP option based labeling should be copied 555747180068SPaul Moore * from the initial connection request (in the IP 555847180068SPaul Moore * layer). it is far from ideal, but until we get a 555947180068SPaul Moore * security label in the packet itself this is the 556047180068SPaul Moore * best we can do. */ 556147180068SPaul Moore return NF_ACCEPT; 556247180068SPaul Moore 556347180068SPaul Moore /* standard practice, label using the parent socket */ 556447180068SPaul Moore sksec = sk->sk_security; 5565948bf85cSPaul Moore sid = sksec->sid; 5566948bf85cSPaul Moore } else 5567948bf85cSPaul Moore sid = SECINITSID_KERNEL; 5568948bf85cSPaul Moore if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0) 5569948bf85cSPaul Moore return NF_DROP; 5570948bf85cSPaul Moore 5571948bf85cSPaul Moore return NF_ACCEPT; 5572948bf85cSPaul Moore } 5573948bf85cSPaul Moore 557406198b34SEric W. Biederman static unsigned int selinux_ipv4_output(void *priv, 5575948bf85cSPaul Moore struct sk_buff *skb, 5576238e54c9SDavid S. Miller const struct nf_hook_state *state) 5577948bf85cSPaul Moore { 5578948bf85cSPaul Moore return selinux_ip_output(skb, PF_INET); 5579948bf85cSPaul Moore } 5580948bf85cSPaul Moore 55811a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 55822917f57bSHuw Davies static unsigned int selinux_ipv6_output(void *priv, 55832917f57bSHuw Davies struct sk_buff *skb, 55842917f57bSHuw Davies const struct nf_hook_state *state) 55852917f57bSHuw Davies { 55862917f57bSHuw Davies return selinux_ip_output(skb, PF_INET6); 55872917f57bSHuw Davies } 55882917f57bSHuw Davies #endif /* IPV6 */ 55892917f57bSHuw Davies 5590effad8dfSPaul Moore static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb, 5591effad8dfSPaul Moore int ifindex, 5592d8395c87SPaul Moore u16 family) 55934e5ab4cbSJames Morris { 559454abc686SEric Dumazet struct sock *sk = skb_to_full_sk(skb); 55954237c75cSVenkat Yekkirala struct sk_security_struct *sksec; 55962bf49690SThomas Liu struct common_audit_data ad; 559748c62af6SEric Paris struct lsm_network_audit net = {0,}; 5598d8395c87SPaul Moore char *addrp; 5599d8395c87SPaul Moore u8 proto; 56004e5ab4cbSJames Morris 5601effad8dfSPaul Moore if (sk == NULL) 5602effad8dfSPaul Moore return NF_ACCEPT; 56034237c75cSVenkat Yekkirala sksec = sk->sk_security; 56044e5ab4cbSJames Morris 560550c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 560648c62af6SEric Paris ad.u.net = &net; 560748c62af6SEric Paris ad.u.net->netif = ifindex; 560848c62af6SEric Paris ad.u.net->family = family; 5609d8395c87SPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto)) 5610d8395c87SPaul Moore return NF_DROP; 5611d8395c87SPaul Moore 561258bfbb51SPaul Moore if (selinux_secmark_enabled()) 56136b6bc620SStephen Smalley if (avc_has_perm(&selinux_state, 56146b6bc620SStephen Smalley sksec->sid, skb->secmark, 5615d8395c87SPaul Moore SECCLASS_PACKET, PACKET__SEND, &ad)) 56162fe66ec2SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 56171da177e4SLinus Torvalds 5618d8395c87SPaul Moore if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto)) 56192fe66ec2SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 5620effad8dfSPaul Moore 5621effad8dfSPaul Moore return NF_ACCEPT; 5622effad8dfSPaul Moore } 5623effad8dfSPaul Moore 5624cbe0d6e8SPaul Moore static unsigned int selinux_ip_postroute(struct sk_buff *skb, 5625cbe0d6e8SPaul Moore const struct net_device *outdev, 5626effad8dfSPaul Moore u16 family) 5627effad8dfSPaul Moore { 5628effad8dfSPaul Moore u32 secmark_perm; 5629effad8dfSPaul Moore u32 peer_sid; 5630cbe0d6e8SPaul Moore int ifindex = outdev->ifindex; 5631effad8dfSPaul Moore struct sock *sk; 56322bf49690SThomas Liu struct common_audit_data ad; 563348c62af6SEric Paris struct lsm_network_audit net = {0,}; 5634effad8dfSPaul Moore char *addrp; 5635effad8dfSPaul Moore u8 secmark_active; 5636effad8dfSPaul Moore u8 peerlbl_active; 5637effad8dfSPaul Moore 5638effad8dfSPaul Moore /* If any sort of compatibility mode is enabled then handoff processing 5639effad8dfSPaul Moore * to the selinux_ip_postroute_compat() function to deal with the 5640effad8dfSPaul Moore * special handling. We do this in an attempt to keep this function 5641effad8dfSPaul Moore * as fast and as clean as possible. */ 5642aa8e712cSStephen Smalley if (!selinux_policycap_netpeer()) 5643d8395c87SPaul Moore return selinux_ip_postroute_compat(skb, ifindex, family); 5644c0828e50SPaul Moore 5645effad8dfSPaul Moore secmark_active = selinux_secmark_enabled(); 56462be4d74fSChris PeBenito peerlbl_active = selinux_peerlbl_enabled(); 5647effad8dfSPaul Moore if (!secmark_active && !peerlbl_active) 5648effad8dfSPaul Moore return NF_ACCEPT; 5649effad8dfSPaul Moore 565054abc686SEric Dumazet sk = skb_to_full_sk(skb); 5651c0828e50SPaul Moore 5652effad8dfSPaul Moore #ifdef CONFIG_XFRM 5653effad8dfSPaul Moore /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec 5654effad8dfSPaul Moore * packet transformation so allow the packet to pass without any checks 5655effad8dfSPaul Moore * since we'll have another chance to perform access control checks 5656effad8dfSPaul Moore * when the packet is on it's final way out. 5657effad8dfSPaul Moore * NOTE: there appear to be some IPv6 multicast cases where skb->dst 5658c0828e50SPaul Moore * is NULL, in this case go ahead and apply access control. 5659c0828e50SPaul Moore * NOTE: if this is a local socket (skb->sk != NULL) that is in the 5660c0828e50SPaul Moore * TCP listening state we cannot wait until the XFRM processing 5661c0828e50SPaul Moore * is done as we will miss out on the SA label if we do; 5662c0828e50SPaul Moore * unfortunately, this means more work, but it is only once per 5663c0828e50SPaul Moore * connection. */ 5664c0828e50SPaul Moore if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL && 5665e446f9dfSEric Dumazet !(sk && sk_listener(sk))) 5666effad8dfSPaul Moore return NF_ACCEPT; 5667effad8dfSPaul Moore #endif 5668effad8dfSPaul Moore 5669d8395c87SPaul Moore if (sk == NULL) { 5670446b8024SPaul Moore /* Without an associated socket the packet is either coming 5671446b8024SPaul Moore * from the kernel or it is being forwarded; check the packet 5672446b8024SPaul Moore * to determine which and if the packet is being forwarded 5673446b8024SPaul Moore * query the packet directly to determine the security label. */ 56744a7ab3dcSSteffen Klassert if (skb->skb_iif) { 5675d8395c87SPaul Moore secmark_perm = PACKET__FORWARD_OUT; 5676d8395c87SPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &peer_sid)) 567704f6d70fSEric Paris return NF_DROP; 56784a7ab3dcSSteffen Klassert } else { 56794a7ab3dcSSteffen Klassert secmark_perm = PACKET__SEND; 5680d8395c87SPaul Moore peer_sid = SECINITSID_KERNEL; 56814a7ab3dcSSteffen Klassert } 5682e446f9dfSEric Dumazet } else if (sk_listener(sk)) { 5683446b8024SPaul Moore /* Locally generated packet but the associated socket is in the 5684446b8024SPaul Moore * listening state which means this is a SYN-ACK packet. In 5685446b8024SPaul Moore * this particular case the correct security label is assigned 5686446b8024SPaul Moore * to the connection/request_sock but unfortunately we can't 5687446b8024SPaul Moore * query the request_sock as it isn't queued on the parent 5688446b8024SPaul Moore * socket until after the SYN-ACK packet is sent; the only 5689446b8024SPaul Moore * viable choice is to regenerate the label like we do in 5690446b8024SPaul Moore * selinux_inet_conn_request(). See also selinux_ip_output() 5691446b8024SPaul Moore * for similar problems. */ 5692446b8024SPaul Moore u32 skb_sid; 5693e446f9dfSEric Dumazet struct sk_security_struct *sksec; 5694e446f9dfSEric Dumazet 5695e446f9dfSEric Dumazet sksec = sk->sk_security; 5696446b8024SPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &skb_sid)) 5697446b8024SPaul Moore return NF_DROP; 5698c0828e50SPaul Moore /* At this point, if the returned skb peerlbl is SECSID_NULL 5699c0828e50SPaul Moore * and the packet has been through at least one XFRM 5700c0828e50SPaul Moore * transformation then we must be dealing with the "final" 5701c0828e50SPaul Moore * form of labeled IPsec packet; since we've already applied 5702c0828e50SPaul Moore * all of our access controls on this packet we can safely 5703c0828e50SPaul Moore * pass the packet. */ 5704c0828e50SPaul Moore if (skb_sid == SECSID_NULL) { 5705c0828e50SPaul Moore switch (family) { 5706c0828e50SPaul Moore case PF_INET: 5707c0828e50SPaul Moore if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) 5708c0828e50SPaul Moore return NF_ACCEPT; 5709c0828e50SPaul Moore break; 5710c0828e50SPaul Moore case PF_INET6: 5711c0828e50SPaul Moore if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) 5712c0828e50SPaul Moore return NF_ACCEPT; 5713a7a91a19SPaul Moore break; 5714c0828e50SPaul Moore default: 5715c0828e50SPaul Moore return NF_DROP_ERR(-ECONNREFUSED); 5716c0828e50SPaul Moore } 5717c0828e50SPaul Moore } 5718446b8024SPaul Moore if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid)) 5719446b8024SPaul Moore return NF_DROP; 5720446b8024SPaul Moore secmark_perm = PACKET__SEND; 5721d8395c87SPaul Moore } else { 5722446b8024SPaul Moore /* Locally generated packet, fetch the security label from the 5723446b8024SPaul Moore * associated socket. */ 5724effad8dfSPaul Moore struct sk_security_struct *sksec = sk->sk_security; 5725effad8dfSPaul Moore peer_sid = sksec->sid; 5726effad8dfSPaul Moore secmark_perm = PACKET__SEND; 5727effad8dfSPaul Moore } 5728effad8dfSPaul Moore 572950c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 573048c62af6SEric Paris ad.u.net = &net; 573148c62af6SEric Paris ad.u.net->netif = ifindex; 573248c62af6SEric Paris ad.u.net->family = family; 5733d8395c87SPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL)) 573404f6d70fSEric Paris return NF_DROP; 5735d8395c87SPaul Moore 5736effad8dfSPaul Moore if (secmark_active) 57376b6bc620SStephen Smalley if (avc_has_perm(&selinux_state, 57386b6bc620SStephen Smalley peer_sid, skb->secmark, 5739effad8dfSPaul Moore SECCLASS_PACKET, secmark_perm, &ad)) 57401f1aaf82SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 5741effad8dfSPaul Moore 5742effad8dfSPaul Moore if (peerlbl_active) { 5743effad8dfSPaul Moore u32 if_sid; 5744effad8dfSPaul Moore u32 node_sid; 5745effad8dfSPaul Moore 5746cbe0d6e8SPaul Moore if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid)) 574704f6d70fSEric Paris return NF_DROP; 57486b6bc620SStephen Smalley if (avc_has_perm(&selinux_state, 57496b6bc620SStephen Smalley peer_sid, if_sid, 5750effad8dfSPaul Moore SECCLASS_NETIF, NETIF__EGRESS, &ad)) 57511f1aaf82SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 5752effad8dfSPaul Moore 5753effad8dfSPaul Moore if (sel_netnode_sid(addrp, family, &node_sid)) 575404f6d70fSEric Paris return NF_DROP; 57556b6bc620SStephen Smalley if (avc_has_perm(&selinux_state, 57566b6bc620SStephen Smalley peer_sid, node_sid, 5757effad8dfSPaul Moore SECCLASS_NODE, NODE__SENDTO, &ad)) 57581f1aaf82SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 5759effad8dfSPaul Moore } 5760effad8dfSPaul Moore 5761effad8dfSPaul Moore return NF_ACCEPT; 5762effad8dfSPaul Moore } 5763effad8dfSPaul Moore 576406198b34SEric W. Biederman static unsigned int selinux_ipv4_postroute(void *priv, 5765a224be76SDavid S. Miller struct sk_buff *skb, 5766238e54c9SDavid S. Miller const struct nf_hook_state *state) 57671da177e4SLinus Torvalds { 5768238e54c9SDavid S. Miller return selinux_ip_postroute(skb, state->out, PF_INET); 57691da177e4SLinus Torvalds } 57701da177e4SLinus Torvalds 57711a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 577206198b34SEric W. Biederman static unsigned int selinux_ipv6_postroute(void *priv, 5773a224be76SDavid S. Miller struct sk_buff *skb, 5774238e54c9SDavid S. Miller const struct nf_hook_state *state) 57751da177e4SLinus Torvalds { 5776238e54c9SDavid S. Miller return selinux_ip_postroute(skb, state->out, PF_INET6); 57771da177e4SLinus Torvalds } 57781da177e4SLinus Torvalds #endif /* IPV6 */ 57791da177e4SLinus Torvalds 57801da177e4SLinus Torvalds #endif /* CONFIG_NETFILTER */ 57811da177e4SLinus Torvalds 57821da177e4SLinus Torvalds static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb) 57831da177e4SLinus Torvalds { 5784941fc5b2SStephen Smalley return selinux_nlmsg_perm(sk, skb); 57851da177e4SLinus Torvalds } 57861da177e4SLinus Torvalds 5787be0554c9SStephen Smalley static int ipc_alloc_security(struct kern_ipc_perm *perm, 57881da177e4SLinus Torvalds u16 sclass) 57891da177e4SLinus Torvalds { 57901da177e4SLinus Torvalds struct ipc_security_struct *isec; 57911da177e4SLinus Torvalds 579289d155efSJames Morris isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL); 57931da177e4SLinus Torvalds if (!isec) 57941da177e4SLinus Torvalds return -ENOMEM; 57951da177e4SLinus Torvalds 57961da177e4SLinus Torvalds isec->sclass = sclass; 5797be0554c9SStephen Smalley isec->sid = current_sid(); 57981da177e4SLinus Torvalds perm->security = isec; 57991da177e4SLinus Torvalds 58001da177e4SLinus Torvalds return 0; 58011da177e4SLinus Torvalds } 58021da177e4SLinus Torvalds 58031da177e4SLinus Torvalds static void ipc_free_security(struct kern_ipc_perm *perm) 58041da177e4SLinus Torvalds { 58051da177e4SLinus Torvalds struct ipc_security_struct *isec = perm->security; 58061da177e4SLinus Torvalds perm->security = NULL; 58071da177e4SLinus Torvalds kfree(isec); 58081da177e4SLinus Torvalds } 58091da177e4SLinus Torvalds 58101da177e4SLinus Torvalds static int msg_msg_alloc_security(struct msg_msg *msg) 58111da177e4SLinus Torvalds { 58121da177e4SLinus Torvalds struct msg_security_struct *msec; 58131da177e4SLinus Torvalds 581489d155efSJames Morris msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL); 58151da177e4SLinus Torvalds if (!msec) 58161da177e4SLinus Torvalds return -ENOMEM; 58171da177e4SLinus Torvalds 58181da177e4SLinus Torvalds msec->sid = SECINITSID_UNLABELED; 58191da177e4SLinus Torvalds msg->security = msec; 58201da177e4SLinus Torvalds 58211da177e4SLinus Torvalds return 0; 58221da177e4SLinus Torvalds } 58231da177e4SLinus Torvalds 58241da177e4SLinus Torvalds static void msg_msg_free_security(struct msg_msg *msg) 58251da177e4SLinus Torvalds { 58261da177e4SLinus Torvalds struct msg_security_struct *msec = msg->security; 58271da177e4SLinus Torvalds 58281da177e4SLinus Torvalds msg->security = NULL; 58291da177e4SLinus Torvalds kfree(msec); 58301da177e4SLinus Torvalds } 58311da177e4SLinus Torvalds 58321da177e4SLinus Torvalds static int ipc_has_perm(struct kern_ipc_perm *ipc_perms, 58336af963f1SStephen Smalley u32 perms) 58341da177e4SLinus Torvalds { 58351da177e4SLinus Torvalds struct ipc_security_struct *isec; 58362bf49690SThomas Liu struct common_audit_data ad; 5837275bb41eSDavid Howells u32 sid = current_sid(); 58381da177e4SLinus Torvalds 58391da177e4SLinus Torvalds isec = ipc_perms->security; 58401da177e4SLinus Torvalds 584150c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 58421da177e4SLinus Torvalds ad.u.ipc_id = ipc_perms->key; 58431da177e4SLinus Torvalds 58446b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 58456b6bc620SStephen Smalley sid, isec->sid, isec->sclass, perms, &ad); 58461da177e4SLinus Torvalds } 58471da177e4SLinus Torvalds 58481da177e4SLinus Torvalds static int selinux_msg_msg_alloc_security(struct msg_msg *msg) 58491da177e4SLinus Torvalds { 58501da177e4SLinus Torvalds return msg_msg_alloc_security(msg); 58511da177e4SLinus Torvalds } 58521da177e4SLinus Torvalds 58531da177e4SLinus Torvalds static void selinux_msg_msg_free_security(struct msg_msg *msg) 58541da177e4SLinus Torvalds { 58551da177e4SLinus Torvalds msg_msg_free_security(msg); 58561da177e4SLinus Torvalds } 58571da177e4SLinus Torvalds 58581da177e4SLinus Torvalds /* message queue security operations */ 5859d8c6e854SEric W. Biederman static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq) 58601da177e4SLinus Torvalds { 58611da177e4SLinus Torvalds struct ipc_security_struct *isec; 58622bf49690SThomas Liu struct common_audit_data ad; 5863275bb41eSDavid Howells u32 sid = current_sid(); 58641da177e4SLinus Torvalds int rc; 58651da177e4SLinus Torvalds 5866d8c6e854SEric W. Biederman rc = ipc_alloc_security(msq, SECCLASS_MSGQ); 58671da177e4SLinus Torvalds if (rc) 58681da177e4SLinus Torvalds return rc; 58691da177e4SLinus Torvalds 5870d8c6e854SEric W. Biederman isec = msq->security; 58711da177e4SLinus Torvalds 587250c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 5873d8c6e854SEric W. Biederman ad.u.ipc_id = msq->key; 58741da177e4SLinus Torvalds 58756b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 58766b6bc620SStephen Smalley sid, isec->sid, SECCLASS_MSGQ, 58771da177e4SLinus Torvalds MSGQ__CREATE, &ad); 58781da177e4SLinus Torvalds if (rc) { 5879d8c6e854SEric W. Biederman ipc_free_security(msq); 58801da177e4SLinus Torvalds return rc; 58811da177e4SLinus Torvalds } 58821da177e4SLinus Torvalds return 0; 58831da177e4SLinus Torvalds } 58841da177e4SLinus Torvalds 5885d8c6e854SEric W. Biederman static void selinux_msg_queue_free_security(struct kern_ipc_perm *msq) 58861da177e4SLinus Torvalds { 5887d8c6e854SEric W. Biederman ipc_free_security(msq); 58881da177e4SLinus Torvalds } 58891da177e4SLinus Torvalds 5890d8c6e854SEric W. Biederman static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg) 58911da177e4SLinus Torvalds { 58921da177e4SLinus Torvalds struct ipc_security_struct *isec; 58932bf49690SThomas Liu struct common_audit_data ad; 5894275bb41eSDavid Howells u32 sid = current_sid(); 58951da177e4SLinus Torvalds 5896d8c6e854SEric W. Biederman isec = msq->security; 58971da177e4SLinus Torvalds 589850c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 5899d8c6e854SEric W. Biederman ad.u.ipc_id = msq->key; 59001da177e4SLinus Torvalds 59016b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 59026b6bc620SStephen Smalley sid, isec->sid, SECCLASS_MSGQ, 59031da177e4SLinus Torvalds MSGQ__ASSOCIATE, &ad); 59041da177e4SLinus Torvalds } 59051da177e4SLinus Torvalds 5906d8c6e854SEric W. Biederman static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd) 59071da177e4SLinus Torvalds { 59081da177e4SLinus Torvalds int err; 59091da177e4SLinus Torvalds int perms; 59101da177e4SLinus Torvalds 59111da177e4SLinus Torvalds switch (cmd) { 59121da177e4SLinus Torvalds case IPC_INFO: 59131da177e4SLinus Torvalds case MSG_INFO: 59141da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 59156b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 59166b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, 5917be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); 59181da177e4SLinus Torvalds case IPC_STAT: 59191da177e4SLinus Torvalds case MSG_STAT: 592023c8cec8SDavidlohr Bueso case MSG_STAT_ANY: 59211da177e4SLinus Torvalds perms = MSGQ__GETATTR | MSGQ__ASSOCIATE; 59221da177e4SLinus Torvalds break; 59231da177e4SLinus Torvalds case IPC_SET: 59241da177e4SLinus Torvalds perms = MSGQ__SETATTR; 59251da177e4SLinus Torvalds break; 59261da177e4SLinus Torvalds case IPC_RMID: 59271da177e4SLinus Torvalds perms = MSGQ__DESTROY; 59281da177e4SLinus Torvalds break; 59291da177e4SLinus Torvalds default: 59301da177e4SLinus Torvalds return 0; 59311da177e4SLinus Torvalds } 59321da177e4SLinus Torvalds 5933d8c6e854SEric W. Biederman err = ipc_has_perm(msq, perms); 59341da177e4SLinus Torvalds return err; 59351da177e4SLinus Torvalds } 59361da177e4SLinus Torvalds 5937d8c6e854SEric W. Biederman static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg) 59381da177e4SLinus Torvalds { 59391da177e4SLinus Torvalds struct ipc_security_struct *isec; 59401da177e4SLinus Torvalds struct msg_security_struct *msec; 59412bf49690SThomas Liu struct common_audit_data ad; 5942275bb41eSDavid Howells u32 sid = current_sid(); 59431da177e4SLinus Torvalds int rc; 59441da177e4SLinus Torvalds 5945d8c6e854SEric W. Biederman isec = msq->security; 59461da177e4SLinus Torvalds msec = msg->security; 59471da177e4SLinus Torvalds 59481da177e4SLinus Torvalds /* 59491da177e4SLinus Torvalds * First time through, need to assign label to the message 59501da177e4SLinus Torvalds */ 59511da177e4SLinus Torvalds if (msec->sid == SECINITSID_UNLABELED) { 59521da177e4SLinus Torvalds /* 59531da177e4SLinus Torvalds * Compute new sid based on current process and 59541da177e4SLinus Torvalds * message queue this message will be stored in 59551da177e4SLinus Torvalds */ 5956aa8e712cSStephen Smalley rc = security_transition_sid(&selinux_state, sid, isec->sid, 5957aa8e712cSStephen Smalley SECCLASS_MSG, NULL, &msec->sid); 59581da177e4SLinus Torvalds if (rc) 59591da177e4SLinus Torvalds return rc; 59601da177e4SLinus Torvalds } 59611da177e4SLinus Torvalds 596250c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 5963d8c6e854SEric W. Biederman ad.u.ipc_id = msq->key; 59641da177e4SLinus Torvalds 59651da177e4SLinus Torvalds /* Can this process write to the queue? */ 59666b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 59676b6bc620SStephen Smalley sid, isec->sid, SECCLASS_MSGQ, 59681da177e4SLinus Torvalds MSGQ__WRITE, &ad); 59691da177e4SLinus Torvalds if (!rc) 59701da177e4SLinus Torvalds /* Can this process send the message */ 59716b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 59726b6bc620SStephen Smalley sid, msec->sid, SECCLASS_MSG, 5973275bb41eSDavid Howells MSG__SEND, &ad); 59741da177e4SLinus Torvalds if (!rc) 59751da177e4SLinus Torvalds /* Can the message be put in the queue? */ 59766b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 59776b6bc620SStephen Smalley msec->sid, isec->sid, SECCLASS_MSGQ, 5978275bb41eSDavid Howells MSGQ__ENQUEUE, &ad); 59791da177e4SLinus Torvalds 59801da177e4SLinus Torvalds return rc; 59811da177e4SLinus Torvalds } 59821da177e4SLinus Torvalds 5983d8c6e854SEric W. Biederman static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg, 59841da177e4SLinus Torvalds struct task_struct *target, 59851da177e4SLinus Torvalds long type, int mode) 59861da177e4SLinus Torvalds { 59871da177e4SLinus Torvalds struct ipc_security_struct *isec; 59881da177e4SLinus Torvalds struct msg_security_struct *msec; 59892bf49690SThomas Liu struct common_audit_data ad; 5990275bb41eSDavid Howells u32 sid = task_sid(target); 59911da177e4SLinus Torvalds int rc; 59921da177e4SLinus Torvalds 5993d8c6e854SEric W. Biederman isec = msq->security; 59941da177e4SLinus Torvalds msec = msg->security; 59951da177e4SLinus Torvalds 599650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 5997d8c6e854SEric W. Biederman ad.u.ipc_id = msq->key; 59981da177e4SLinus Torvalds 59996b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 60006b6bc620SStephen Smalley sid, isec->sid, 60011da177e4SLinus Torvalds SECCLASS_MSGQ, MSGQ__READ, &ad); 60021da177e4SLinus Torvalds if (!rc) 60036b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 60046b6bc620SStephen Smalley sid, msec->sid, 60051da177e4SLinus Torvalds SECCLASS_MSG, MSG__RECEIVE, &ad); 60061da177e4SLinus Torvalds return rc; 60071da177e4SLinus Torvalds } 60081da177e4SLinus Torvalds 60091da177e4SLinus Torvalds /* Shared Memory security operations */ 60107191adffSEric W. Biederman static int selinux_shm_alloc_security(struct kern_ipc_perm *shp) 60111da177e4SLinus Torvalds { 60121da177e4SLinus Torvalds struct ipc_security_struct *isec; 60132bf49690SThomas Liu struct common_audit_data ad; 6014275bb41eSDavid Howells u32 sid = current_sid(); 60151da177e4SLinus Torvalds int rc; 60161da177e4SLinus Torvalds 60177191adffSEric W. Biederman rc = ipc_alloc_security(shp, SECCLASS_SHM); 60181da177e4SLinus Torvalds if (rc) 60191da177e4SLinus Torvalds return rc; 60201da177e4SLinus Torvalds 60217191adffSEric W. Biederman isec = shp->security; 60221da177e4SLinus Torvalds 602350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 60247191adffSEric W. Biederman ad.u.ipc_id = shp->key; 60251da177e4SLinus Torvalds 60266b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 60276b6bc620SStephen Smalley sid, isec->sid, SECCLASS_SHM, 60281da177e4SLinus Torvalds SHM__CREATE, &ad); 60291da177e4SLinus Torvalds if (rc) { 60307191adffSEric W. Biederman ipc_free_security(shp); 60311da177e4SLinus Torvalds return rc; 60321da177e4SLinus Torvalds } 60331da177e4SLinus Torvalds return 0; 60341da177e4SLinus Torvalds } 60351da177e4SLinus Torvalds 60367191adffSEric W. Biederman static void selinux_shm_free_security(struct kern_ipc_perm *shp) 60371da177e4SLinus Torvalds { 60387191adffSEric W. Biederman ipc_free_security(shp); 60391da177e4SLinus Torvalds } 60401da177e4SLinus Torvalds 60417191adffSEric W. Biederman static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg) 60421da177e4SLinus Torvalds { 60431da177e4SLinus Torvalds struct ipc_security_struct *isec; 60442bf49690SThomas Liu struct common_audit_data ad; 6045275bb41eSDavid Howells u32 sid = current_sid(); 60461da177e4SLinus Torvalds 60477191adffSEric W. Biederman isec = shp->security; 60481da177e4SLinus Torvalds 604950c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 60507191adffSEric W. Biederman ad.u.ipc_id = shp->key; 60511da177e4SLinus Torvalds 60526b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 60536b6bc620SStephen Smalley sid, isec->sid, SECCLASS_SHM, 60541da177e4SLinus Torvalds SHM__ASSOCIATE, &ad); 60551da177e4SLinus Torvalds } 60561da177e4SLinus Torvalds 60571da177e4SLinus Torvalds /* Note, at this point, shp is locked down */ 60587191adffSEric W. Biederman static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd) 60591da177e4SLinus Torvalds { 60601da177e4SLinus Torvalds int perms; 60611da177e4SLinus Torvalds int err; 60621da177e4SLinus Torvalds 60631da177e4SLinus Torvalds switch (cmd) { 60641da177e4SLinus Torvalds case IPC_INFO: 60651da177e4SLinus Torvalds case SHM_INFO: 60661da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 60676b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 60686b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, 6069be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); 60701da177e4SLinus Torvalds case IPC_STAT: 60711da177e4SLinus Torvalds case SHM_STAT: 6072c21a6970SDavidlohr Bueso case SHM_STAT_ANY: 60731da177e4SLinus Torvalds perms = SHM__GETATTR | SHM__ASSOCIATE; 60741da177e4SLinus Torvalds break; 60751da177e4SLinus Torvalds case IPC_SET: 60761da177e4SLinus Torvalds perms = SHM__SETATTR; 60771da177e4SLinus Torvalds break; 60781da177e4SLinus Torvalds case SHM_LOCK: 60791da177e4SLinus Torvalds case SHM_UNLOCK: 60801da177e4SLinus Torvalds perms = SHM__LOCK; 60811da177e4SLinus Torvalds break; 60821da177e4SLinus Torvalds case IPC_RMID: 60831da177e4SLinus Torvalds perms = SHM__DESTROY; 60841da177e4SLinus Torvalds break; 60851da177e4SLinus Torvalds default: 60861da177e4SLinus Torvalds return 0; 60871da177e4SLinus Torvalds } 60881da177e4SLinus Torvalds 60897191adffSEric W. Biederman err = ipc_has_perm(shp, perms); 60901da177e4SLinus Torvalds return err; 60911da177e4SLinus Torvalds } 60921da177e4SLinus Torvalds 60937191adffSEric W. Biederman static int selinux_shm_shmat(struct kern_ipc_perm *shp, 60941da177e4SLinus Torvalds char __user *shmaddr, int shmflg) 60951da177e4SLinus Torvalds { 60961da177e4SLinus Torvalds u32 perms; 60971da177e4SLinus Torvalds 60981da177e4SLinus Torvalds if (shmflg & SHM_RDONLY) 60991da177e4SLinus Torvalds perms = SHM__READ; 61001da177e4SLinus Torvalds else 61011da177e4SLinus Torvalds perms = SHM__READ | SHM__WRITE; 61021da177e4SLinus Torvalds 61037191adffSEric W. Biederman return ipc_has_perm(shp, perms); 61041da177e4SLinus Torvalds } 61051da177e4SLinus Torvalds 61061da177e4SLinus Torvalds /* Semaphore security operations */ 6107aefad959SEric W. Biederman static int selinux_sem_alloc_security(struct kern_ipc_perm *sma) 61081da177e4SLinus Torvalds { 61091da177e4SLinus Torvalds struct ipc_security_struct *isec; 61102bf49690SThomas Liu struct common_audit_data ad; 6111275bb41eSDavid Howells u32 sid = current_sid(); 61121da177e4SLinus Torvalds int rc; 61131da177e4SLinus Torvalds 6114aefad959SEric W. Biederman rc = ipc_alloc_security(sma, SECCLASS_SEM); 61151da177e4SLinus Torvalds if (rc) 61161da177e4SLinus Torvalds return rc; 61171da177e4SLinus Torvalds 6118aefad959SEric W. Biederman isec = sma->security; 61191da177e4SLinus Torvalds 612050c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 6121aefad959SEric W. Biederman ad.u.ipc_id = sma->key; 61221da177e4SLinus Torvalds 61236b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 61246b6bc620SStephen Smalley sid, isec->sid, SECCLASS_SEM, 61251da177e4SLinus Torvalds SEM__CREATE, &ad); 61261da177e4SLinus Torvalds if (rc) { 6127aefad959SEric W. Biederman ipc_free_security(sma); 61281da177e4SLinus Torvalds return rc; 61291da177e4SLinus Torvalds } 61301da177e4SLinus Torvalds return 0; 61311da177e4SLinus Torvalds } 61321da177e4SLinus Torvalds 6133aefad959SEric W. Biederman static void selinux_sem_free_security(struct kern_ipc_perm *sma) 61341da177e4SLinus Torvalds { 6135aefad959SEric W. Biederman ipc_free_security(sma); 61361da177e4SLinus Torvalds } 61371da177e4SLinus Torvalds 6138aefad959SEric W. Biederman static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg) 61391da177e4SLinus Torvalds { 61401da177e4SLinus Torvalds struct ipc_security_struct *isec; 61412bf49690SThomas Liu struct common_audit_data ad; 6142275bb41eSDavid Howells u32 sid = current_sid(); 61431da177e4SLinus Torvalds 6144aefad959SEric W. Biederman isec = sma->security; 61451da177e4SLinus Torvalds 614650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 6147aefad959SEric W. Biederman ad.u.ipc_id = sma->key; 61481da177e4SLinus Torvalds 61496b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 61506b6bc620SStephen Smalley sid, isec->sid, SECCLASS_SEM, 61511da177e4SLinus Torvalds SEM__ASSOCIATE, &ad); 61521da177e4SLinus Torvalds } 61531da177e4SLinus Torvalds 61541da177e4SLinus Torvalds /* Note, at this point, sma is locked down */ 6155aefad959SEric W. Biederman static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd) 61561da177e4SLinus Torvalds { 61571da177e4SLinus Torvalds int err; 61581da177e4SLinus Torvalds u32 perms; 61591da177e4SLinus Torvalds 61601da177e4SLinus Torvalds switch (cmd) { 61611da177e4SLinus Torvalds case IPC_INFO: 61621da177e4SLinus Torvalds case SEM_INFO: 61631da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 61646b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 61656b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, 6166be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); 61671da177e4SLinus Torvalds case GETPID: 61681da177e4SLinus Torvalds case GETNCNT: 61691da177e4SLinus Torvalds case GETZCNT: 61701da177e4SLinus Torvalds perms = SEM__GETATTR; 61711da177e4SLinus Torvalds break; 61721da177e4SLinus Torvalds case GETVAL: 61731da177e4SLinus Torvalds case GETALL: 61741da177e4SLinus Torvalds perms = SEM__READ; 61751da177e4SLinus Torvalds break; 61761da177e4SLinus Torvalds case SETVAL: 61771da177e4SLinus Torvalds case SETALL: 61781da177e4SLinus Torvalds perms = SEM__WRITE; 61791da177e4SLinus Torvalds break; 61801da177e4SLinus Torvalds case IPC_RMID: 61811da177e4SLinus Torvalds perms = SEM__DESTROY; 61821da177e4SLinus Torvalds break; 61831da177e4SLinus Torvalds case IPC_SET: 61841da177e4SLinus Torvalds perms = SEM__SETATTR; 61851da177e4SLinus Torvalds break; 61861da177e4SLinus Torvalds case IPC_STAT: 61871da177e4SLinus Torvalds case SEM_STAT: 6188a280d6dcSDavidlohr Bueso case SEM_STAT_ANY: 61891da177e4SLinus Torvalds perms = SEM__GETATTR | SEM__ASSOCIATE; 61901da177e4SLinus Torvalds break; 61911da177e4SLinus Torvalds default: 61921da177e4SLinus Torvalds return 0; 61931da177e4SLinus Torvalds } 61941da177e4SLinus Torvalds 6195aefad959SEric W. Biederman err = ipc_has_perm(sma, perms); 61961da177e4SLinus Torvalds return err; 61971da177e4SLinus Torvalds } 61981da177e4SLinus Torvalds 6199aefad959SEric W. Biederman static int selinux_sem_semop(struct kern_ipc_perm *sma, 62001da177e4SLinus Torvalds struct sembuf *sops, unsigned nsops, int alter) 62011da177e4SLinus Torvalds { 62021da177e4SLinus Torvalds u32 perms; 62031da177e4SLinus Torvalds 62041da177e4SLinus Torvalds if (alter) 62051da177e4SLinus Torvalds perms = SEM__READ | SEM__WRITE; 62061da177e4SLinus Torvalds else 62071da177e4SLinus Torvalds perms = SEM__READ; 62081da177e4SLinus Torvalds 6209aefad959SEric W. Biederman return ipc_has_perm(sma, perms); 62101da177e4SLinus Torvalds } 62111da177e4SLinus Torvalds 62121da177e4SLinus Torvalds static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag) 62131da177e4SLinus Torvalds { 62141da177e4SLinus Torvalds u32 av = 0; 62151da177e4SLinus Torvalds 62161da177e4SLinus Torvalds av = 0; 62171da177e4SLinus Torvalds if (flag & S_IRUGO) 62181da177e4SLinus Torvalds av |= IPC__UNIX_READ; 62191da177e4SLinus Torvalds if (flag & S_IWUGO) 62201da177e4SLinus Torvalds av |= IPC__UNIX_WRITE; 62211da177e4SLinus Torvalds 62221da177e4SLinus Torvalds if (av == 0) 62231da177e4SLinus Torvalds return 0; 62241da177e4SLinus Torvalds 62256af963f1SStephen Smalley return ipc_has_perm(ipcp, av); 62261da177e4SLinus Torvalds } 62271da177e4SLinus Torvalds 6228713a04aeSAhmed S. Darwish static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid) 6229713a04aeSAhmed S. Darwish { 6230713a04aeSAhmed S. Darwish struct ipc_security_struct *isec = ipcp->security; 6231713a04aeSAhmed S. Darwish *secid = isec->sid; 6232713a04aeSAhmed S. Darwish } 6233713a04aeSAhmed S. Darwish 62341da177e4SLinus Torvalds static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode) 62351da177e4SLinus Torvalds { 62361da177e4SLinus Torvalds if (inode) 62371da177e4SLinus Torvalds inode_doinit_with_dentry(inode, dentry); 62381da177e4SLinus Torvalds } 62391da177e4SLinus Torvalds 62401da177e4SLinus Torvalds static int selinux_getprocattr(struct task_struct *p, 624104ff9708SAl Viro char *name, char **value) 62421da177e4SLinus Torvalds { 6243275bb41eSDavid Howells const struct task_security_struct *__tsec; 62448c8570fbSDustin Kirkland u32 sid; 62451da177e4SLinus Torvalds int error; 624604ff9708SAl Viro unsigned len; 62471da177e4SLinus Torvalds 6248275bb41eSDavid Howells rcu_read_lock(); 6249275bb41eSDavid Howells __tsec = __task_cred(p)->security; 62501da177e4SLinus Torvalds 6251be0554c9SStephen Smalley if (current != p) { 62526b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 62536b6bc620SStephen Smalley current_sid(), __tsec->sid, 6254be0554c9SStephen Smalley SECCLASS_PROCESS, PROCESS__GETATTR, NULL); 6255be0554c9SStephen Smalley if (error) 6256be0554c9SStephen Smalley goto bad; 6257be0554c9SStephen Smalley } 6258be0554c9SStephen Smalley 62591da177e4SLinus Torvalds if (!strcmp(name, "current")) 6260275bb41eSDavid Howells sid = __tsec->sid; 62611da177e4SLinus Torvalds else if (!strcmp(name, "prev")) 6262275bb41eSDavid Howells sid = __tsec->osid; 62631da177e4SLinus Torvalds else if (!strcmp(name, "exec")) 6264275bb41eSDavid Howells sid = __tsec->exec_sid; 62651da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 6266275bb41eSDavid Howells sid = __tsec->create_sid; 62674eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) 6268275bb41eSDavid Howells sid = __tsec->keycreate_sid; 626942c3e03eSEric Paris else if (!strcmp(name, "sockcreate")) 6270275bb41eSDavid Howells sid = __tsec->sockcreate_sid; 6271be0554c9SStephen Smalley else { 6272be0554c9SStephen Smalley error = -EINVAL; 6273be0554c9SStephen Smalley goto bad; 6274be0554c9SStephen Smalley } 6275275bb41eSDavid Howells rcu_read_unlock(); 62761da177e4SLinus Torvalds 62771da177e4SLinus Torvalds if (!sid) 62781da177e4SLinus Torvalds return 0; 62791da177e4SLinus Torvalds 6280aa8e712cSStephen Smalley error = security_sid_to_context(&selinux_state, sid, value, &len); 628104ff9708SAl Viro if (error) 628204ff9708SAl Viro return error; 628304ff9708SAl Viro return len; 6284275bb41eSDavid Howells 6285be0554c9SStephen Smalley bad: 6286275bb41eSDavid Howells rcu_read_unlock(); 6287be0554c9SStephen Smalley return error; 62881da177e4SLinus Torvalds } 62891da177e4SLinus Torvalds 6290b21507e2SStephen Smalley static int selinux_setprocattr(const char *name, void *value, size_t size) 62911da177e4SLinus Torvalds { 62921da177e4SLinus Torvalds struct task_security_struct *tsec; 6293d84f4f99SDavid Howells struct cred *new; 6294be0554c9SStephen Smalley u32 mysid = current_sid(), sid = 0, ptsid; 62951da177e4SLinus Torvalds int error; 62961da177e4SLinus Torvalds char *str = value; 62971da177e4SLinus Torvalds 62981da177e4SLinus Torvalds /* 62991da177e4SLinus Torvalds * Basic control over ability to set these attributes at all. 63001da177e4SLinus Torvalds */ 63011da177e4SLinus Torvalds if (!strcmp(name, "exec")) 63026b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 63036b6bc620SStephen Smalley mysid, mysid, SECCLASS_PROCESS, 6304be0554c9SStephen Smalley PROCESS__SETEXEC, NULL); 63051da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 63066b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 63076b6bc620SStephen Smalley mysid, mysid, SECCLASS_PROCESS, 6308be0554c9SStephen Smalley PROCESS__SETFSCREATE, NULL); 63094eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) 63106b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 63116b6bc620SStephen Smalley mysid, mysid, SECCLASS_PROCESS, 6312be0554c9SStephen Smalley PROCESS__SETKEYCREATE, NULL); 631342c3e03eSEric Paris else if (!strcmp(name, "sockcreate")) 63146b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 63156b6bc620SStephen Smalley mysid, mysid, SECCLASS_PROCESS, 6316be0554c9SStephen Smalley PROCESS__SETSOCKCREATE, NULL); 63171da177e4SLinus Torvalds else if (!strcmp(name, "current")) 63186b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 63196b6bc620SStephen Smalley mysid, mysid, SECCLASS_PROCESS, 6320be0554c9SStephen Smalley PROCESS__SETCURRENT, NULL); 63211da177e4SLinus Torvalds else 63221da177e4SLinus Torvalds error = -EINVAL; 63231da177e4SLinus Torvalds if (error) 63241da177e4SLinus Torvalds return error; 63251da177e4SLinus Torvalds 63261da177e4SLinus Torvalds /* Obtain a SID for the context, if one was specified. */ 6327a050a570SStephen Smalley if (size && str[0] && str[0] != '\n') { 63281da177e4SLinus Torvalds if (str[size-1] == '\n') { 63291da177e4SLinus Torvalds str[size-1] = 0; 63301da177e4SLinus Torvalds size--; 63311da177e4SLinus Torvalds } 6332aa8e712cSStephen Smalley error = security_context_to_sid(&selinux_state, value, size, 6333aa8e712cSStephen Smalley &sid, GFP_KERNEL); 633412b29f34SStephen Smalley if (error == -EINVAL && !strcmp(name, "fscreate")) { 6335db59000aSStephen Smalley if (!has_cap_mac_admin(true)) { 6336d6ea83ecSEric Paris struct audit_buffer *ab; 6337d6ea83ecSEric Paris size_t audit_size; 6338d6ea83ecSEric Paris 6339d6ea83ecSEric Paris /* We strip a nul only if it is at the end, otherwise the 6340d6ea83ecSEric Paris * context contains a nul and we should audit that */ 6341d6ea83ecSEric Paris if (str[size - 1] == '\0') 6342d6ea83ecSEric Paris audit_size = size - 1; 6343d6ea83ecSEric Paris else 6344d6ea83ecSEric Paris audit_size = size; 6345cdfb6b34SRichard Guy Briggs ab = audit_log_start(audit_context(), 6346cdfb6b34SRichard Guy Briggs GFP_ATOMIC, 6347cdfb6b34SRichard Guy Briggs AUDIT_SELINUX_ERR); 6348d6ea83ecSEric Paris audit_log_format(ab, "op=fscreate invalid_context="); 6349d6ea83ecSEric Paris audit_log_n_untrustedstring(ab, value, audit_size); 6350d6ea83ecSEric Paris audit_log_end(ab); 6351d6ea83ecSEric Paris 635212b29f34SStephen Smalley return error; 6353d6ea83ecSEric Paris } 6354aa8e712cSStephen Smalley error = security_context_to_sid_force( 6355aa8e712cSStephen Smalley &selinux_state, 6356aa8e712cSStephen Smalley value, size, &sid); 635712b29f34SStephen Smalley } 63581da177e4SLinus Torvalds if (error) 63591da177e4SLinus Torvalds return error; 63601da177e4SLinus Torvalds } 63611da177e4SLinus Torvalds 6362d84f4f99SDavid Howells new = prepare_creds(); 6363d84f4f99SDavid Howells if (!new) 6364d84f4f99SDavid Howells return -ENOMEM; 6365d84f4f99SDavid Howells 63661da177e4SLinus Torvalds /* Permission checking based on the specified context is 63671da177e4SLinus Torvalds performed during the actual operation (execve, 63681da177e4SLinus Torvalds open/mkdir/...), when we know the full context of the 6369d84f4f99SDavid Howells operation. See selinux_bprm_set_creds for the execve 63701da177e4SLinus Torvalds checks and may_create for the file creation checks. The 63711da177e4SLinus Torvalds operation will then fail if the context is not permitted. */ 6372d84f4f99SDavid Howells tsec = new->security; 6373d84f4f99SDavid Howells if (!strcmp(name, "exec")) { 63741da177e4SLinus Torvalds tsec->exec_sid = sid; 6375d84f4f99SDavid Howells } else if (!strcmp(name, "fscreate")) { 63761da177e4SLinus Torvalds tsec->create_sid = sid; 6377d84f4f99SDavid Howells } else if (!strcmp(name, "keycreate")) { 63786b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 63796b6bc620SStephen Smalley mysid, sid, SECCLASS_KEY, KEY__CREATE, 6380be0554c9SStephen Smalley NULL); 63814eb582cfSMichael LeMay if (error) 6382d84f4f99SDavid Howells goto abort_change; 63834eb582cfSMichael LeMay tsec->keycreate_sid = sid; 6384d84f4f99SDavid Howells } else if (!strcmp(name, "sockcreate")) { 638542c3e03eSEric Paris tsec->sockcreate_sid = sid; 6386d84f4f99SDavid Howells } else if (!strcmp(name, "current")) { 6387d84f4f99SDavid Howells error = -EINVAL; 63881da177e4SLinus Torvalds if (sid == 0) 6389d84f4f99SDavid Howells goto abort_change; 6390d9250deaSKaiGai Kohei 6391d84f4f99SDavid Howells /* Only allow single threaded processes to change context */ 6392d84f4f99SDavid Howells error = -EPERM; 63935bb459bbSOleg Nesterov if (!current_is_single_threaded()) { 6394aa8e712cSStephen Smalley error = security_bounded_transition(&selinux_state, 6395aa8e712cSStephen Smalley tsec->sid, sid); 6396d84f4f99SDavid Howells if (error) 6397d84f4f99SDavid Howells goto abort_change; 63981da177e4SLinus Torvalds } 63991da177e4SLinus Torvalds 64001da177e4SLinus Torvalds /* Check permissions for the transition. */ 64016b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 64026b6bc620SStephen Smalley tsec->sid, sid, SECCLASS_PROCESS, 64031da177e4SLinus Torvalds PROCESS__DYNTRANSITION, NULL); 64041da177e4SLinus Torvalds if (error) 6405d84f4f99SDavid Howells goto abort_change; 64061da177e4SLinus Torvalds 64071da177e4SLinus Torvalds /* Check for ptracing, and update the task SID if ok. 64081da177e4SLinus Torvalds Otherwise, leave SID unchanged and fail. */ 6409be0554c9SStephen Smalley ptsid = ptrace_parent_sid(); 64100c6181cbSPaul Moore if (ptsid != 0) { 64116b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 64126b6bc620SStephen Smalley ptsid, sid, SECCLASS_PROCESS, 6413d84f4f99SDavid Howells PROCESS__PTRACE, NULL); 6414d84f4f99SDavid Howells if (error) 6415d84f4f99SDavid Howells goto abort_change; 6416d84f4f99SDavid Howells } 6417d84f4f99SDavid Howells 6418d84f4f99SDavid Howells tsec->sid = sid; 6419d84f4f99SDavid Howells } else { 6420d84f4f99SDavid Howells error = -EINVAL; 6421d84f4f99SDavid Howells goto abort_change; 6422d84f4f99SDavid Howells } 6423d84f4f99SDavid Howells 6424d84f4f99SDavid Howells commit_creds(new); 64251da177e4SLinus Torvalds return size; 6426d84f4f99SDavid Howells 6427d84f4f99SDavid Howells abort_change: 6428d84f4f99SDavid Howells abort_creds(new); 6429d84f4f99SDavid Howells return error; 64301da177e4SLinus Torvalds } 64311da177e4SLinus Torvalds 6432746df9b5SDavid Quigley static int selinux_ismaclabel(const char *name) 6433746df9b5SDavid Quigley { 6434746df9b5SDavid Quigley return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0); 6435746df9b5SDavid Quigley } 6436746df9b5SDavid Quigley 6437dc49c1f9SCatherine Zhang static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) 6438dc49c1f9SCatherine Zhang { 6439aa8e712cSStephen Smalley return security_sid_to_context(&selinux_state, secid, 6440aa8e712cSStephen Smalley secdata, seclen); 6441dc49c1f9SCatherine Zhang } 6442dc49c1f9SCatherine Zhang 64437bf570dcSDavid Howells static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid) 644463cb3449SDavid Howells { 6445aa8e712cSStephen Smalley return security_context_to_sid(&selinux_state, secdata, seclen, 6446aa8e712cSStephen Smalley secid, GFP_KERNEL); 644763cb3449SDavid Howells } 644863cb3449SDavid Howells 6449dc49c1f9SCatherine Zhang static void selinux_release_secctx(char *secdata, u32 seclen) 6450dc49c1f9SCatherine Zhang { 6451dc49c1f9SCatherine Zhang kfree(secdata); 6452dc49c1f9SCatherine Zhang } 6453dc49c1f9SCatherine Zhang 64546f3be9f5SAndreas Gruenbacher static void selinux_inode_invalidate_secctx(struct inode *inode) 64556f3be9f5SAndreas Gruenbacher { 64566f3be9f5SAndreas Gruenbacher struct inode_security_struct *isec = inode->i_security; 64576f3be9f5SAndreas Gruenbacher 64589287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 64596f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INVALID; 64609287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 64616f3be9f5SAndreas Gruenbacher } 64626f3be9f5SAndreas Gruenbacher 64631ee65e37SDavid P. Quigley /* 64641ee65e37SDavid P. Quigley * called with inode->i_mutex locked 64651ee65e37SDavid P. Quigley */ 64661ee65e37SDavid P. Quigley static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen) 64671ee65e37SDavid P. Quigley { 64681ee65e37SDavid P. Quigley return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0); 64691ee65e37SDavid P. Quigley } 64701ee65e37SDavid P. Quigley 64711ee65e37SDavid P. Quigley /* 64721ee65e37SDavid P. Quigley * called with inode->i_mutex locked 64731ee65e37SDavid P. Quigley */ 64741ee65e37SDavid P. Quigley static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen) 64751ee65e37SDavid P. Quigley { 64761ee65e37SDavid P. Quigley return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0); 64771ee65e37SDavid P. Quigley } 64781ee65e37SDavid P. Quigley 64791ee65e37SDavid P. Quigley static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen) 64801ee65e37SDavid P. Quigley { 64811ee65e37SDavid P. Quigley int len = 0; 64821ee65e37SDavid P. Quigley len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX, 64831ee65e37SDavid P. Quigley ctx, true); 64841ee65e37SDavid P. Quigley if (len < 0) 64851ee65e37SDavid P. Quigley return len; 64861ee65e37SDavid P. Quigley *ctxlen = len; 64871ee65e37SDavid P. Quigley return 0; 64881ee65e37SDavid P. Quigley } 6489d720024eSMichael LeMay #ifdef CONFIG_KEYS 6490d720024eSMichael LeMay 6491d84f4f99SDavid Howells static int selinux_key_alloc(struct key *k, const struct cred *cred, 64927e047ef5SDavid Howells unsigned long flags) 6493d720024eSMichael LeMay { 6494d84f4f99SDavid Howells const struct task_security_struct *tsec; 6495d720024eSMichael LeMay struct key_security_struct *ksec; 6496d720024eSMichael LeMay 6497d720024eSMichael LeMay ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL); 6498d720024eSMichael LeMay if (!ksec) 6499d720024eSMichael LeMay return -ENOMEM; 6500d720024eSMichael LeMay 6501d84f4f99SDavid Howells tsec = cred->security; 6502d84f4f99SDavid Howells if (tsec->keycreate_sid) 6503d84f4f99SDavid Howells ksec->sid = tsec->keycreate_sid; 65044eb582cfSMichael LeMay else 6505d84f4f99SDavid Howells ksec->sid = tsec->sid; 6506d720024eSMichael LeMay 6507275bb41eSDavid Howells k->security = ksec; 6508d720024eSMichael LeMay return 0; 6509d720024eSMichael LeMay } 6510d720024eSMichael LeMay 6511d720024eSMichael LeMay static void selinux_key_free(struct key *k) 6512d720024eSMichael LeMay { 6513d720024eSMichael LeMay struct key_security_struct *ksec = k->security; 6514d720024eSMichael LeMay 6515d720024eSMichael LeMay k->security = NULL; 6516d720024eSMichael LeMay kfree(ksec); 6517d720024eSMichael LeMay } 6518d720024eSMichael LeMay 6519d720024eSMichael LeMay static int selinux_key_permission(key_ref_t key_ref, 6520d84f4f99SDavid Howells const struct cred *cred, 6521f5895943SDavid Howells unsigned perm) 6522d720024eSMichael LeMay { 6523d720024eSMichael LeMay struct key *key; 6524d720024eSMichael LeMay struct key_security_struct *ksec; 6525275bb41eSDavid Howells u32 sid; 6526d720024eSMichael LeMay 6527d720024eSMichael LeMay /* if no specific permissions are requested, we skip the 6528d720024eSMichael LeMay permission check. No serious, additional covert channels 6529d720024eSMichael LeMay appear to be created. */ 6530d720024eSMichael LeMay if (perm == 0) 6531d720024eSMichael LeMay return 0; 6532d720024eSMichael LeMay 6533d84f4f99SDavid Howells sid = cred_sid(cred); 6534275bb41eSDavid Howells 6535275bb41eSDavid Howells key = key_ref_to_ptr(key_ref); 6536275bb41eSDavid Howells ksec = key->security; 6537275bb41eSDavid Howells 65386b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 65396b6bc620SStephen Smalley sid, ksec->sid, SECCLASS_KEY, perm, NULL); 6540d720024eSMichael LeMay } 6541d720024eSMichael LeMay 654270a5bb72SDavid Howells static int selinux_key_getsecurity(struct key *key, char **_buffer) 654370a5bb72SDavid Howells { 654470a5bb72SDavid Howells struct key_security_struct *ksec = key->security; 654570a5bb72SDavid Howells char *context = NULL; 654670a5bb72SDavid Howells unsigned len; 654770a5bb72SDavid Howells int rc; 654870a5bb72SDavid Howells 6549aa8e712cSStephen Smalley rc = security_sid_to_context(&selinux_state, ksec->sid, 6550aa8e712cSStephen Smalley &context, &len); 655170a5bb72SDavid Howells if (!rc) 655270a5bb72SDavid Howells rc = len; 655370a5bb72SDavid Howells *_buffer = context; 655470a5bb72SDavid Howells return rc; 655570a5bb72SDavid Howells } 65563a976fa6SDaniel Jurgens #endif 655770a5bb72SDavid Howells 65583a976fa6SDaniel Jurgens #ifdef CONFIG_SECURITY_INFINIBAND 6559cfc4d882SDaniel Jurgens static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val) 6560cfc4d882SDaniel Jurgens { 6561cfc4d882SDaniel Jurgens struct common_audit_data ad; 6562cfc4d882SDaniel Jurgens int err; 6563cfc4d882SDaniel Jurgens u32 sid = 0; 6564cfc4d882SDaniel Jurgens struct ib_security_struct *sec = ib_sec; 6565cfc4d882SDaniel Jurgens struct lsm_ibpkey_audit ibpkey; 6566cfc4d882SDaniel Jurgens 6567409dcf31SDaniel Jurgens err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid); 6568cfc4d882SDaniel Jurgens if (err) 6569cfc4d882SDaniel Jurgens return err; 6570cfc4d882SDaniel Jurgens 6571cfc4d882SDaniel Jurgens ad.type = LSM_AUDIT_DATA_IBPKEY; 6572cfc4d882SDaniel Jurgens ibpkey.subnet_prefix = subnet_prefix; 6573cfc4d882SDaniel Jurgens ibpkey.pkey = pkey_val; 6574cfc4d882SDaniel Jurgens ad.u.ibpkey = &ibpkey; 65756b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 65766b6bc620SStephen Smalley sec->sid, sid, 6577cfc4d882SDaniel Jurgens SECCLASS_INFINIBAND_PKEY, 6578cfc4d882SDaniel Jurgens INFINIBAND_PKEY__ACCESS, &ad); 6579cfc4d882SDaniel Jurgens } 6580cfc4d882SDaniel Jurgens 6581ab861dfcSDaniel Jurgens static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name, 6582ab861dfcSDaniel Jurgens u8 port_num) 6583ab861dfcSDaniel Jurgens { 6584ab861dfcSDaniel Jurgens struct common_audit_data ad; 6585ab861dfcSDaniel Jurgens int err; 6586ab861dfcSDaniel Jurgens u32 sid = 0; 6587ab861dfcSDaniel Jurgens struct ib_security_struct *sec = ib_sec; 6588ab861dfcSDaniel Jurgens struct lsm_ibendport_audit ibendport; 6589ab861dfcSDaniel Jurgens 6590aa8e712cSStephen Smalley err = security_ib_endport_sid(&selinux_state, dev_name, port_num, 6591aa8e712cSStephen Smalley &sid); 6592ab861dfcSDaniel Jurgens 6593ab861dfcSDaniel Jurgens if (err) 6594ab861dfcSDaniel Jurgens return err; 6595ab861dfcSDaniel Jurgens 6596ab861dfcSDaniel Jurgens ad.type = LSM_AUDIT_DATA_IBENDPORT; 6597ab861dfcSDaniel Jurgens strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name)); 6598ab861dfcSDaniel Jurgens ibendport.port = port_num; 6599ab861dfcSDaniel Jurgens ad.u.ibendport = &ibendport; 66006b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 66016b6bc620SStephen Smalley sec->sid, sid, 6602ab861dfcSDaniel Jurgens SECCLASS_INFINIBAND_ENDPORT, 6603ab861dfcSDaniel Jurgens INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad); 6604ab861dfcSDaniel Jurgens } 6605ab861dfcSDaniel Jurgens 66063a976fa6SDaniel Jurgens static int selinux_ib_alloc_security(void **ib_sec) 66073a976fa6SDaniel Jurgens { 66083a976fa6SDaniel Jurgens struct ib_security_struct *sec; 66093a976fa6SDaniel Jurgens 66103a976fa6SDaniel Jurgens sec = kzalloc(sizeof(*sec), GFP_KERNEL); 66113a976fa6SDaniel Jurgens if (!sec) 66123a976fa6SDaniel Jurgens return -ENOMEM; 66133a976fa6SDaniel Jurgens sec->sid = current_sid(); 66143a976fa6SDaniel Jurgens 66153a976fa6SDaniel Jurgens *ib_sec = sec; 66163a976fa6SDaniel Jurgens return 0; 66173a976fa6SDaniel Jurgens } 66183a976fa6SDaniel Jurgens 66193a976fa6SDaniel Jurgens static void selinux_ib_free_security(void *ib_sec) 66203a976fa6SDaniel Jurgens { 66213a976fa6SDaniel Jurgens kfree(ib_sec); 66223a976fa6SDaniel Jurgens } 6623d720024eSMichael LeMay #endif 6624d720024eSMichael LeMay 6625ec27c356SChenbo Feng #ifdef CONFIG_BPF_SYSCALL 6626ec27c356SChenbo Feng static int selinux_bpf(int cmd, union bpf_attr *attr, 6627ec27c356SChenbo Feng unsigned int size) 6628ec27c356SChenbo Feng { 6629ec27c356SChenbo Feng u32 sid = current_sid(); 6630ec27c356SChenbo Feng int ret; 6631ec27c356SChenbo Feng 6632ec27c356SChenbo Feng switch (cmd) { 6633ec27c356SChenbo Feng case BPF_MAP_CREATE: 66346b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 66356b6bc620SStephen Smalley sid, sid, SECCLASS_BPF, BPF__MAP_CREATE, 6636ec27c356SChenbo Feng NULL); 6637ec27c356SChenbo Feng break; 6638ec27c356SChenbo Feng case BPF_PROG_LOAD: 66396b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 66406b6bc620SStephen Smalley sid, sid, SECCLASS_BPF, BPF__PROG_LOAD, 6641ec27c356SChenbo Feng NULL); 6642ec27c356SChenbo Feng break; 6643ec27c356SChenbo Feng default: 6644ec27c356SChenbo Feng ret = 0; 6645ec27c356SChenbo Feng break; 6646ec27c356SChenbo Feng } 6647ec27c356SChenbo Feng 6648ec27c356SChenbo Feng return ret; 6649ec27c356SChenbo Feng } 6650ec27c356SChenbo Feng 6651ec27c356SChenbo Feng static u32 bpf_map_fmode_to_av(fmode_t fmode) 6652ec27c356SChenbo Feng { 6653ec27c356SChenbo Feng u32 av = 0; 6654ec27c356SChenbo Feng 6655ec27c356SChenbo Feng if (fmode & FMODE_READ) 6656ec27c356SChenbo Feng av |= BPF__MAP_READ; 6657ec27c356SChenbo Feng if (fmode & FMODE_WRITE) 6658ec27c356SChenbo Feng av |= BPF__MAP_WRITE; 6659ec27c356SChenbo Feng return av; 6660ec27c356SChenbo Feng } 6661ec27c356SChenbo Feng 6662f66e448cSChenbo Feng /* This function will check the file pass through unix socket or binder to see 6663f66e448cSChenbo Feng * if it is a bpf related object. And apply correspinding checks on the bpf 6664f66e448cSChenbo Feng * object based on the type. The bpf maps and programs, not like other files and 6665f66e448cSChenbo Feng * socket, are using a shared anonymous inode inside the kernel as their inode. 6666f66e448cSChenbo Feng * So checking that inode cannot identify if the process have privilege to 6667f66e448cSChenbo Feng * access the bpf object and that's why we have to add this additional check in 6668f66e448cSChenbo Feng * selinux_file_receive and selinux_binder_transfer_files. 6669f66e448cSChenbo Feng */ 6670f66e448cSChenbo Feng static int bpf_fd_pass(struct file *file, u32 sid) 6671f66e448cSChenbo Feng { 6672f66e448cSChenbo Feng struct bpf_security_struct *bpfsec; 6673f66e448cSChenbo Feng struct bpf_prog *prog; 6674f66e448cSChenbo Feng struct bpf_map *map; 6675f66e448cSChenbo Feng int ret; 6676f66e448cSChenbo Feng 6677f66e448cSChenbo Feng if (file->f_op == &bpf_map_fops) { 6678f66e448cSChenbo Feng map = file->private_data; 6679f66e448cSChenbo Feng bpfsec = map->security; 66806b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 66816b6bc620SStephen Smalley sid, bpfsec->sid, SECCLASS_BPF, 6682f66e448cSChenbo Feng bpf_map_fmode_to_av(file->f_mode), NULL); 6683f66e448cSChenbo Feng if (ret) 6684f66e448cSChenbo Feng return ret; 6685f66e448cSChenbo Feng } else if (file->f_op == &bpf_prog_fops) { 6686f66e448cSChenbo Feng prog = file->private_data; 6687f66e448cSChenbo Feng bpfsec = prog->aux->security; 66886b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 66896b6bc620SStephen Smalley sid, bpfsec->sid, SECCLASS_BPF, 6690f66e448cSChenbo Feng BPF__PROG_RUN, NULL); 6691f66e448cSChenbo Feng if (ret) 6692f66e448cSChenbo Feng return ret; 6693f66e448cSChenbo Feng } 6694f66e448cSChenbo Feng return 0; 6695f66e448cSChenbo Feng } 6696f66e448cSChenbo Feng 6697ec27c356SChenbo Feng static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode) 6698ec27c356SChenbo Feng { 6699ec27c356SChenbo Feng u32 sid = current_sid(); 6700ec27c356SChenbo Feng struct bpf_security_struct *bpfsec; 6701ec27c356SChenbo Feng 6702ec27c356SChenbo Feng bpfsec = map->security; 67036b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 67046b6bc620SStephen Smalley sid, bpfsec->sid, SECCLASS_BPF, 6705ec27c356SChenbo Feng bpf_map_fmode_to_av(fmode), NULL); 6706ec27c356SChenbo Feng } 6707ec27c356SChenbo Feng 6708ec27c356SChenbo Feng static int selinux_bpf_prog(struct bpf_prog *prog) 6709ec27c356SChenbo Feng { 6710ec27c356SChenbo Feng u32 sid = current_sid(); 6711ec27c356SChenbo Feng struct bpf_security_struct *bpfsec; 6712ec27c356SChenbo Feng 6713ec27c356SChenbo Feng bpfsec = prog->aux->security; 67146b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 67156b6bc620SStephen Smalley sid, bpfsec->sid, SECCLASS_BPF, 6716ec27c356SChenbo Feng BPF__PROG_RUN, NULL); 6717ec27c356SChenbo Feng } 6718ec27c356SChenbo Feng 6719ec27c356SChenbo Feng static int selinux_bpf_map_alloc(struct bpf_map *map) 6720ec27c356SChenbo Feng { 6721ec27c356SChenbo Feng struct bpf_security_struct *bpfsec; 6722ec27c356SChenbo Feng 6723ec27c356SChenbo Feng bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL); 6724ec27c356SChenbo Feng if (!bpfsec) 6725ec27c356SChenbo Feng return -ENOMEM; 6726ec27c356SChenbo Feng 6727ec27c356SChenbo Feng bpfsec->sid = current_sid(); 6728ec27c356SChenbo Feng map->security = bpfsec; 6729ec27c356SChenbo Feng 6730ec27c356SChenbo Feng return 0; 6731ec27c356SChenbo Feng } 6732ec27c356SChenbo Feng 6733ec27c356SChenbo Feng static void selinux_bpf_map_free(struct bpf_map *map) 6734ec27c356SChenbo Feng { 6735ec27c356SChenbo Feng struct bpf_security_struct *bpfsec = map->security; 6736ec27c356SChenbo Feng 6737ec27c356SChenbo Feng map->security = NULL; 6738ec27c356SChenbo Feng kfree(bpfsec); 6739ec27c356SChenbo Feng } 6740ec27c356SChenbo Feng 6741ec27c356SChenbo Feng static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux) 6742ec27c356SChenbo Feng { 6743ec27c356SChenbo Feng struct bpf_security_struct *bpfsec; 6744ec27c356SChenbo Feng 6745ec27c356SChenbo Feng bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL); 6746ec27c356SChenbo Feng if (!bpfsec) 6747ec27c356SChenbo Feng return -ENOMEM; 6748ec27c356SChenbo Feng 6749ec27c356SChenbo Feng bpfsec->sid = current_sid(); 6750ec27c356SChenbo Feng aux->security = bpfsec; 6751ec27c356SChenbo Feng 6752ec27c356SChenbo Feng return 0; 6753ec27c356SChenbo Feng } 6754ec27c356SChenbo Feng 6755ec27c356SChenbo Feng static void selinux_bpf_prog_free(struct bpf_prog_aux *aux) 6756ec27c356SChenbo Feng { 6757ec27c356SChenbo Feng struct bpf_security_struct *bpfsec = aux->security; 6758ec27c356SChenbo Feng 6759ec27c356SChenbo Feng aux->security = NULL; 6760ec27c356SChenbo Feng kfree(bpfsec); 6761ec27c356SChenbo Feng } 6762ec27c356SChenbo Feng #endif 6763ec27c356SChenbo Feng 6764ca97d939SJames Morris static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = { 6765e20b043aSCasey Schaufler LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr), 6766e20b043aSCasey Schaufler LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction), 6767e20b043aSCasey Schaufler LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder), 6768e20b043aSCasey Schaufler LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file), 6769076c54c5SAhmed S. Darwish 6770e20b043aSCasey Schaufler LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check), 6771e20b043aSCasey Schaufler LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme), 6772e20b043aSCasey Schaufler LSM_HOOK_INIT(capget, selinux_capget), 6773e20b043aSCasey Schaufler LSM_HOOK_INIT(capset, selinux_capset), 6774e20b043aSCasey Schaufler LSM_HOOK_INIT(capable, selinux_capable), 6775e20b043aSCasey Schaufler LSM_HOOK_INIT(quotactl, selinux_quotactl), 6776e20b043aSCasey Schaufler LSM_HOOK_INIT(quota_on, selinux_quota_on), 6777e20b043aSCasey Schaufler LSM_HOOK_INIT(syslog, selinux_syslog), 6778e20b043aSCasey Schaufler LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory), 677979af7307SStephen Smalley 6780e20b043aSCasey Schaufler LSM_HOOK_INIT(netlink_send, selinux_netlink_send), 67811da177e4SLinus Torvalds 6782e20b043aSCasey Schaufler LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds), 6783e20b043aSCasey Schaufler LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds), 6784e20b043aSCasey Schaufler LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds), 67851da177e4SLinus Torvalds 6786*0b52075eSAl Viro LSM_HOOK_INIT(fs_context_dup, selinux_fs_context_dup), 6787442155c1SDavid Howells LSM_HOOK_INIT(fs_context_parse_param, selinux_fs_context_parse_param), 6788442155c1SDavid Howells 6789e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security), 6790e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security), 67915b400239SAl Viro LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts), 6792204cc0ccSAl Viro LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts), 6793e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_remount, selinux_sb_remount), 6794e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount), 6795e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options), 6796e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs), 6797e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_mount, selinux_mount), 6798e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_umount, selinux_umount), 6799e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts), 6800e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts), 6801757cbe59SAl Viro LSM_HOOK_INIT(sb_add_mnt_opt, selinux_add_mnt_opt), 68021da177e4SLinus Torvalds 6803e20b043aSCasey Schaufler LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security), 6804a518b0a5SVivek Goyal LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as), 6805e0007529SEric Paris 6806e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security), 6807e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security), 6808e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security), 6809e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_create, selinux_inode_create), 6810e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_link, selinux_inode_link), 6811e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink), 6812e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink), 6813e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir), 6814e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir), 6815e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod), 6816e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_rename, selinux_inode_rename), 6817e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink), 6818e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link), 6819e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_permission, selinux_inode_permission), 6820e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr), 6821e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr), 6822e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr), 6823e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr), 6824e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr), 6825e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr), 6826e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr), 6827e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity), 6828e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity), 6829e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity), 6830e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid), 683156909eb3SVivek Goyal LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up), 683219472b69SVivek Goyal LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr), 68331da177e4SLinus Torvalds 6834e20b043aSCasey Schaufler LSM_HOOK_INIT(file_permission, selinux_file_permission), 6835e20b043aSCasey Schaufler LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security), 6836e20b043aSCasey Schaufler LSM_HOOK_INIT(file_free_security, selinux_file_free_security), 6837e20b043aSCasey Schaufler LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl), 6838e20b043aSCasey Schaufler LSM_HOOK_INIT(mmap_file, selinux_mmap_file), 6839e20b043aSCasey Schaufler LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr), 6840e20b043aSCasey Schaufler LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect), 6841e20b043aSCasey Schaufler LSM_HOOK_INIT(file_lock, selinux_file_lock), 6842e20b043aSCasey Schaufler LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl), 6843e20b043aSCasey Schaufler LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner), 6844e20b043aSCasey Schaufler LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask), 6845e20b043aSCasey Schaufler LSM_HOOK_INIT(file_receive, selinux_file_receive), 68461da177e4SLinus Torvalds 6847e20b043aSCasey Schaufler LSM_HOOK_INIT(file_open, selinux_file_open), 68481da177e4SLinus Torvalds 6849a79be238STetsuo Handa LSM_HOOK_INIT(task_alloc, selinux_task_alloc), 6850e20b043aSCasey Schaufler LSM_HOOK_INIT(cred_alloc_blank, selinux_cred_alloc_blank), 6851e20b043aSCasey Schaufler LSM_HOOK_INIT(cred_free, selinux_cred_free), 6852e20b043aSCasey Schaufler LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare), 6853e20b043aSCasey Schaufler LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer), 68543ec30113SMatthew Garrett LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid), 6855e20b043aSCasey Schaufler LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as), 6856e20b043aSCasey Schaufler LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as), 6857e20b043aSCasey Schaufler LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request), 6858c77b8cdfSMimi Zohar LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data), 685961d612eaSJeff Vander Stoep LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file), 6860e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid), 6861e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid), 6862e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getsid, selinux_task_getsid), 6863e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid), 6864e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setnice, selinux_task_setnice), 6865e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio), 6866e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio), 6867791ec491SStephen Smalley LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit), 6868e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit), 6869e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler), 6870e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler), 6871e20b043aSCasey Schaufler LSM_HOOK_INIT(task_movememory, selinux_task_movememory), 6872e20b043aSCasey Schaufler LSM_HOOK_INIT(task_kill, selinux_task_kill), 6873e20b043aSCasey Schaufler LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode), 6874788e7dd4SYuichi Nakamura 6875e20b043aSCasey Schaufler LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission), 6876e20b043aSCasey Schaufler LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid), 68771da177e4SLinus Torvalds 6878e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security), 6879e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_msg_free_security, selinux_msg_msg_free_security), 68801da177e4SLinus Torvalds 6881e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_alloc_security, 6882e20b043aSCasey Schaufler selinux_msg_queue_alloc_security), 6883e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_free_security, selinux_msg_queue_free_security), 6884e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate), 6885e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl), 6886e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd), 6887e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv), 68881da177e4SLinus Torvalds 6889e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security), 6890e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_free_security, selinux_shm_free_security), 6891e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_associate, selinux_shm_associate), 6892e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl), 6893e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat), 68941da177e4SLinus Torvalds 6895e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security), 6896e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_free_security, selinux_sem_free_security), 6897e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_associate, selinux_sem_associate), 6898e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl), 6899e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_semop, selinux_sem_semop), 69001da177e4SLinus Torvalds 6901e20b043aSCasey Schaufler LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate), 69021da177e4SLinus Torvalds 6903e20b043aSCasey Schaufler LSM_HOOK_INIT(getprocattr, selinux_getprocattr), 6904e20b043aSCasey Schaufler LSM_HOOK_INIT(setprocattr, selinux_setprocattr), 69051da177e4SLinus Torvalds 6906e20b043aSCasey Schaufler LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel), 6907e20b043aSCasey Schaufler LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx), 6908e20b043aSCasey Schaufler LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid), 6909e20b043aSCasey Schaufler LSM_HOOK_INIT(release_secctx, selinux_release_secctx), 69106f3be9f5SAndreas Gruenbacher LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx), 6911e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx), 6912e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx), 6913e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx), 69141da177e4SLinus Torvalds 6915e20b043aSCasey Schaufler LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect), 6916e20b043aSCasey Schaufler LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send), 6917dc49c1f9SCatherine Zhang 6918e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_create, selinux_socket_create), 6919e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create), 69200b811db2SDavid Herrmann LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair), 6921e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_bind, selinux_socket_bind), 6922e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_connect, selinux_socket_connect), 6923e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_listen, selinux_socket_listen), 6924e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_accept, selinux_socket_accept), 6925e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg), 6926e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg), 6927e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname), 6928e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername), 6929e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt), 6930e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt), 6931e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown), 6932e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb), 6933e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getpeersec_stream, 6934e20b043aSCasey Schaufler selinux_socket_getpeersec_stream), 6935e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram), 6936e20b043aSCasey Schaufler LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security), 6937e20b043aSCasey Schaufler LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security), 6938e20b043aSCasey Schaufler LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security), 6939e20b043aSCasey Schaufler LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid), 6940e20b043aSCasey Schaufler LSM_HOOK_INIT(sock_graft, selinux_sock_graft), 6941d452930fSRichard Haines LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request), 6942d452930fSRichard Haines LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone), 6943d452930fSRichard Haines LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect), 6944e20b043aSCasey Schaufler LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request), 6945e20b043aSCasey Schaufler LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone), 6946e20b043aSCasey Schaufler LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established), 6947e20b043aSCasey Schaufler LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet), 6948e20b043aSCasey Schaufler LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc), 6949e20b043aSCasey Schaufler LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec), 6950e20b043aSCasey Schaufler LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow), 6951e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security), 6952e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security), 6953e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create), 6954e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue), 6955e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach), 6956e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open), 69573a976fa6SDaniel Jurgens #ifdef CONFIG_SECURITY_INFINIBAND 6958cfc4d882SDaniel Jurgens LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access), 6959ab861dfcSDaniel Jurgens LSM_HOOK_INIT(ib_endport_manage_subnet, 6960ab861dfcSDaniel Jurgens selinux_ib_endport_manage_subnet), 69613a976fa6SDaniel Jurgens LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security), 69623a976fa6SDaniel Jurgens LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security), 69633a976fa6SDaniel Jurgens #endif 6964d28d1e08STrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM 6965e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc), 6966e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone), 6967e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free), 6968e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete), 6969e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc), 6970e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_alloc_acquire, 6971e20b043aSCasey Schaufler selinux_xfrm_state_alloc_acquire), 6972e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free), 6973e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete), 6974e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup), 6975e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_pol_flow_match, 6976e20b043aSCasey Schaufler selinux_xfrm_state_pol_flow_match), 6977e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session), 69781da177e4SLinus Torvalds #endif 6979d720024eSMichael LeMay 6980d720024eSMichael LeMay #ifdef CONFIG_KEYS 6981e20b043aSCasey Schaufler LSM_HOOK_INIT(key_alloc, selinux_key_alloc), 6982e20b043aSCasey Schaufler LSM_HOOK_INIT(key_free, selinux_key_free), 6983e20b043aSCasey Schaufler LSM_HOOK_INIT(key_permission, selinux_key_permission), 6984e20b043aSCasey Schaufler LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity), 6985d720024eSMichael LeMay #endif 69869d57a7f9SAhmed S. Darwish 69879d57a7f9SAhmed S. Darwish #ifdef CONFIG_AUDIT 6988e20b043aSCasey Schaufler LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init), 6989e20b043aSCasey Schaufler LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known), 6990e20b043aSCasey Schaufler LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match), 6991e20b043aSCasey Schaufler LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free), 69929d57a7f9SAhmed S. Darwish #endif 6993ec27c356SChenbo Feng 6994ec27c356SChenbo Feng #ifdef CONFIG_BPF_SYSCALL 6995ec27c356SChenbo Feng LSM_HOOK_INIT(bpf, selinux_bpf), 6996ec27c356SChenbo Feng LSM_HOOK_INIT(bpf_map, selinux_bpf_map), 6997ec27c356SChenbo Feng LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog), 6998ec27c356SChenbo Feng LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc), 6999ec27c356SChenbo Feng LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc), 7000ec27c356SChenbo Feng LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free), 7001ec27c356SChenbo Feng LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free), 7002ec27c356SChenbo Feng #endif 70031da177e4SLinus Torvalds }; 70041da177e4SLinus Torvalds 70051da177e4SLinus Torvalds static __init int selinux_init(void) 70061da177e4SLinus Torvalds { 7007b1d9e6b0SCasey Schaufler if (!security_module_enable("selinux")) { 7008076c54c5SAhmed S. Darwish selinux_enabled = 0; 7009076c54c5SAhmed S. Darwish return 0; 7010076c54c5SAhmed S. Darwish } 7011076c54c5SAhmed S. Darwish 70121da177e4SLinus Torvalds if (!selinux_enabled) { 7013c103a91eSpeter enderborg pr_info("SELinux: Disabled at boot.\n"); 70141da177e4SLinus Torvalds return 0; 70151da177e4SLinus Torvalds } 70161da177e4SLinus Torvalds 7017c103a91eSpeter enderborg pr_info("SELinux: Initializing.\n"); 70181da177e4SLinus Torvalds 7019aa8e712cSStephen Smalley memset(&selinux_state, 0, sizeof(selinux_state)); 7020e5a5ca96SPaul Moore enforcing_set(&selinux_state, selinux_enforcing_boot); 7021aa8e712cSStephen Smalley selinux_state.checkreqprot = selinux_checkreqprot_boot; 7022aa8e712cSStephen Smalley selinux_ss_init(&selinux_state.ss); 70236b6bc620SStephen Smalley selinux_avc_init(&selinux_state.avc); 7024aa8e712cSStephen Smalley 70251da177e4SLinus Torvalds /* Set the security state for the initial task. */ 7026d84f4f99SDavid Howells cred_init_security(); 70271da177e4SLinus Torvalds 7028fcaaade1SStephen Smalley default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC); 7029fcaaade1SStephen Smalley 70307cae7e26SJames Morris sel_inode_cache = kmem_cache_create("selinux_inode_security", 70317cae7e26SJames Morris sizeof(struct inode_security_struct), 703220c2df83SPaul Mundt 0, SLAB_PANIC, NULL); 703363205654SSangwoo file_security_cache = kmem_cache_create("selinux_file_security", 703463205654SSangwoo sizeof(struct file_security_struct), 703563205654SSangwoo 0, SLAB_PANIC, NULL); 70361da177e4SLinus Torvalds avc_init(); 70371da177e4SLinus Torvalds 7038aa8e712cSStephen Smalley avtab_cache_init(); 7039aa8e712cSStephen Smalley 7040aa8e712cSStephen Smalley ebitmap_cache_init(); 7041aa8e712cSStephen Smalley 7042aa8e712cSStephen Smalley hashtab_cache_init(); 7043aa8e712cSStephen Smalley 7044d69dece5SCasey Schaufler security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux"); 70451da177e4SLinus Torvalds 7046615e51fdSPaul Moore if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET)) 7047615e51fdSPaul Moore panic("SELinux: Unable to register AVC netcache callback\n"); 7048615e51fdSPaul Moore 70498f408ab6SDaniel Jurgens if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET)) 70508f408ab6SDaniel Jurgens panic("SELinux: Unable to register AVC LSM notifier callback\n"); 70518f408ab6SDaniel Jurgens 7052aa8e712cSStephen Smalley if (selinux_enforcing_boot) 7053c103a91eSpeter enderborg pr_debug("SELinux: Starting in enforcing mode\n"); 7054828dfe1dSEric Paris else 7055c103a91eSpeter enderborg pr_debug("SELinux: Starting in permissive mode\n"); 7056d720024eSMichael LeMay 7057442155c1SDavid Howells fs_validate_description(&selinux_fs_parameters); 7058442155c1SDavid Howells 70591da177e4SLinus Torvalds return 0; 70601da177e4SLinus Torvalds } 70611da177e4SLinus Torvalds 7062e8c26255SAl Viro static void delayed_superblock_init(struct super_block *sb, void *unused) 7063e8c26255SAl Viro { 7064204cc0ccSAl Viro selinux_set_mnt_opts(sb, NULL, 0, NULL); 7065e8c26255SAl Viro } 7066e8c26255SAl Viro 70671da177e4SLinus Torvalds void selinux_complete_init(void) 70681da177e4SLinus Torvalds { 7069c103a91eSpeter enderborg pr_debug("SELinux: Completing initialization.\n"); 70701da177e4SLinus Torvalds 70711da177e4SLinus Torvalds /* Set up any superblocks initialized prior to the policy load. */ 7072c103a91eSpeter enderborg pr_debug("SELinux: Setting up existing superblocks.\n"); 7073e8c26255SAl Viro iterate_supers(delayed_superblock_init, NULL); 70741da177e4SLinus Torvalds } 70751da177e4SLinus Torvalds 70761da177e4SLinus Torvalds /* SELinux requires early initialization in order to label 70771da177e4SLinus Torvalds all processes and objects when they are created. */ 70783d6e5f6dSKees Cook DEFINE_LSM(selinux) = { 707907aed2f2SKees Cook .name = "selinux", 70803d6e5f6dSKees Cook .init = selinux_init, 70813d6e5f6dSKees Cook }; 70821da177e4SLinus Torvalds 7083c2b507fdSStephen Smalley #if defined(CONFIG_NETFILTER) 70841da177e4SLinus Torvalds 7085591bb278SFlorian Westphal static const struct nf_hook_ops selinux_nf_ops[] = { 7086effad8dfSPaul Moore { 7087effad8dfSPaul Moore .hook = selinux_ipv4_postroute, 70882597a834SAlban Crequy .pf = NFPROTO_IPV4, 70896e23ae2aSPatrick McHardy .hooknum = NF_INET_POST_ROUTING, 70901da177e4SLinus Torvalds .priority = NF_IP_PRI_SELINUX_LAST, 7091effad8dfSPaul Moore }, 7092effad8dfSPaul Moore { 7093effad8dfSPaul Moore .hook = selinux_ipv4_forward, 70942597a834SAlban Crequy .pf = NFPROTO_IPV4, 7095effad8dfSPaul Moore .hooknum = NF_INET_FORWARD, 7096effad8dfSPaul Moore .priority = NF_IP_PRI_SELINUX_FIRST, 7097948bf85cSPaul Moore }, 7098948bf85cSPaul Moore { 7099948bf85cSPaul Moore .hook = selinux_ipv4_output, 71002597a834SAlban Crequy .pf = NFPROTO_IPV4, 7101948bf85cSPaul Moore .hooknum = NF_INET_LOCAL_OUT, 7102948bf85cSPaul Moore .priority = NF_IP_PRI_SELINUX_FIRST, 710325db6beaSJiri Pirko }, 71041a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 7105effad8dfSPaul Moore { 7106effad8dfSPaul Moore .hook = selinux_ipv6_postroute, 71072597a834SAlban Crequy .pf = NFPROTO_IPV6, 71086e23ae2aSPatrick McHardy .hooknum = NF_INET_POST_ROUTING, 71091da177e4SLinus Torvalds .priority = NF_IP6_PRI_SELINUX_LAST, 7110effad8dfSPaul Moore }, 7111effad8dfSPaul Moore { 7112effad8dfSPaul Moore .hook = selinux_ipv6_forward, 71132597a834SAlban Crequy .pf = NFPROTO_IPV6, 7114effad8dfSPaul Moore .hooknum = NF_INET_FORWARD, 7115effad8dfSPaul Moore .priority = NF_IP6_PRI_SELINUX_FIRST, 711625db6beaSJiri Pirko }, 71172917f57bSHuw Davies { 71182917f57bSHuw Davies .hook = selinux_ipv6_output, 71192917f57bSHuw Davies .pf = NFPROTO_IPV6, 71202917f57bSHuw Davies .hooknum = NF_INET_LOCAL_OUT, 71212917f57bSHuw Davies .priority = NF_IP6_PRI_SELINUX_FIRST, 71222917f57bSHuw Davies }, 71231da177e4SLinus Torvalds #endif /* IPV6 */ 712425db6beaSJiri Pirko }; 71251da177e4SLinus Torvalds 71268e71bf75SFlorian Westphal static int __net_init selinux_nf_register(struct net *net) 71278e71bf75SFlorian Westphal { 71288e71bf75SFlorian Westphal return nf_register_net_hooks(net, selinux_nf_ops, 71298e71bf75SFlorian Westphal ARRAY_SIZE(selinux_nf_ops)); 71308e71bf75SFlorian Westphal } 71318e71bf75SFlorian Westphal 71328e71bf75SFlorian Westphal static void __net_exit selinux_nf_unregister(struct net *net) 71338e71bf75SFlorian Westphal { 71348e71bf75SFlorian Westphal nf_unregister_net_hooks(net, selinux_nf_ops, 71358e71bf75SFlorian Westphal ARRAY_SIZE(selinux_nf_ops)); 71368e71bf75SFlorian Westphal } 71378e71bf75SFlorian Westphal 71388e71bf75SFlorian Westphal static struct pernet_operations selinux_net_ops = { 71398e71bf75SFlorian Westphal .init = selinux_nf_register, 71408e71bf75SFlorian Westphal .exit = selinux_nf_unregister, 71418e71bf75SFlorian Westphal }; 71428e71bf75SFlorian Westphal 71431da177e4SLinus Torvalds static int __init selinux_nf_ip_init(void) 71441da177e4SLinus Torvalds { 714525db6beaSJiri Pirko int err; 71461da177e4SLinus Torvalds 71471da177e4SLinus Torvalds if (!selinux_enabled) 714825db6beaSJiri Pirko return 0; 71491da177e4SLinus Torvalds 7150c103a91eSpeter enderborg pr_debug("SELinux: Registering netfilter hooks\n"); 71511da177e4SLinus Torvalds 71528e71bf75SFlorian Westphal err = register_pernet_subsys(&selinux_net_ops); 71531da177e4SLinus Torvalds if (err) 71548e71bf75SFlorian Westphal panic("SELinux: register_pernet_subsys: error %d\n", err); 71551da177e4SLinus Torvalds 715625db6beaSJiri Pirko return 0; 71571da177e4SLinus Torvalds } 71581da177e4SLinus Torvalds __initcall(selinux_nf_ip_init); 71591da177e4SLinus Torvalds 71601da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 71611da177e4SLinus Torvalds static void selinux_nf_ip_exit(void) 71621da177e4SLinus Torvalds { 7163c103a91eSpeter enderborg pr_debug("SELinux: Unregistering netfilter hooks\n"); 71641da177e4SLinus Torvalds 71658e71bf75SFlorian Westphal unregister_pernet_subsys(&selinux_net_ops); 71661da177e4SLinus Torvalds } 71671da177e4SLinus Torvalds #endif 71681da177e4SLinus Torvalds 7169c2b507fdSStephen Smalley #else /* CONFIG_NETFILTER */ 71701da177e4SLinus Torvalds 71711da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 71721da177e4SLinus Torvalds #define selinux_nf_ip_exit() 71731da177e4SLinus Torvalds #endif 71741da177e4SLinus Torvalds 7175c2b507fdSStephen Smalley #endif /* CONFIG_NETFILTER */ 71761da177e4SLinus Torvalds 71771da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 7178aa8e712cSStephen Smalley int selinux_disable(struct selinux_state *state) 71791da177e4SLinus Torvalds { 7180aa8e712cSStephen Smalley if (state->initialized) { 71811da177e4SLinus Torvalds /* Not permitted after initial policy load. */ 71821da177e4SLinus Torvalds return -EINVAL; 71831da177e4SLinus Torvalds } 71841da177e4SLinus Torvalds 7185aa8e712cSStephen Smalley if (state->disabled) { 71861da177e4SLinus Torvalds /* Only do this once. */ 71871da177e4SLinus Torvalds return -EINVAL; 71881da177e4SLinus Torvalds } 71891da177e4SLinus Torvalds 7190aa8e712cSStephen Smalley state->disabled = 1; 7191aa8e712cSStephen Smalley 7192c103a91eSpeter enderborg pr_info("SELinux: Disabled at runtime.\n"); 71931da177e4SLinus Torvalds 719430d55280SStephen Smalley selinux_enabled = 0; 71951da177e4SLinus Torvalds 7196b1d9e6b0SCasey Schaufler security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks)); 71971da177e4SLinus Torvalds 7198af8ff049SEric Paris /* Try to destroy the avc node cache */ 7199af8ff049SEric Paris avc_disable(); 7200af8ff049SEric Paris 72011da177e4SLinus Torvalds /* Unregister netfilter hooks. */ 72021da177e4SLinus Torvalds selinux_nf_ip_exit(); 72031da177e4SLinus Torvalds 72041da177e4SLinus Torvalds /* Unregister selinuxfs. */ 72051da177e4SLinus Torvalds exit_sel_fs(); 72061da177e4SLinus Torvalds 72071da177e4SLinus Torvalds return 0; 72081da177e4SLinus Torvalds } 72091da177e4SLinus Torvalds #endif 7210