1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * NSA Security-Enhanced Linux (SELinux) security module 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * This file contains the SELinux hook function implementations. 61da177e4SLinus Torvalds * 77efbb60bSStephen Smalley * Authors: Stephen Smalley, <sds@tycho.nsa.gov> 81da177e4SLinus Torvalds * Chris Vance, <cvance@nai.com> 91da177e4SLinus Torvalds * Wayne Salamon, <wsalamon@nai.com> 101da177e4SLinus Torvalds * James Morris <jmorris@redhat.com> 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds * Copyright (C) 2001,2002 Networks Associates Technology, Inc. 132069f457SEric Paris * Copyright (C) 2003-2008 Red Hat, Inc., James Morris <jmorris@redhat.com> 142069f457SEric Paris * Eric Paris <eparis@redhat.com> 151da177e4SLinus Torvalds * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc. 161da177e4SLinus Torvalds * <dgoeddel@trustedcs.com> 17ed6d76e4SPaul Moore * Copyright (C) 2006, 2007, 2009 Hewlett-Packard Development Company, L.P. 1882c21bfaSPaul Moore * Paul Moore <paul@paul-moore.com> 19788e7dd4SYuichi Nakamura * Copyright (C) 2007 Hitachi Software Engineering Co., Ltd. 20788e7dd4SYuichi Nakamura * Yuichi Nakamura <ynakam@hitachisoft.jp> 213a976fa6SDaniel Jurgens * Copyright (C) 2016 Mellanox Technologies 221da177e4SLinus Torvalds */ 231da177e4SLinus Torvalds 241da177e4SLinus Torvalds #include <linux/init.h> 250b24dcb7SEric Paris #include <linux/kd.h> 261da177e4SLinus Torvalds #include <linux/kernel.h> 270d094efeSRoland McGrath #include <linux/tracehook.h> 281da177e4SLinus Torvalds #include <linux/errno.h> 293f07c014SIngo Molnar #include <linux/sched/signal.h> 3029930025SIngo Molnar #include <linux/sched/task.h> 313c4ed7bdSCasey Schaufler #include <linux/lsm_hooks.h> 321da177e4SLinus Torvalds #include <linux/xattr.h> 331da177e4SLinus Torvalds #include <linux/capability.h> 341da177e4SLinus Torvalds #include <linux/unistd.h> 351da177e4SLinus Torvalds #include <linux/mm.h> 361da177e4SLinus Torvalds #include <linux/mman.h> 371da177e4SLinus Torvalds #include <linux/slab.h> 381da177e4SLinus Torvalds #include <linux/pagemap.h> 390b24dcb7SEric Paris #include <linux/proc_fs.h> 401da177e4SLinus Torvalds #include <linux/swap.h> 411da177e4SLinus Torvalds #include <linux/spinlock.h> 421da177e4SLinus Torvalds #include <linux/syscalls.h> 432a7dba39SEric Paris #include <linux/dcache.h> 441da177e4SLinus Torvalds #include <linux/file.h> 459f3acc31SAl Viro #include <linux/fdtable.h> 461da177e4SLinus Torvalds #include <linux/namei.h> 471da177e4SLinus Torvalds #include <linux/mount.h> 48442155c1SDavid Howells #include <linux/fs_context.h> 49442155c1SDavid Howells #include <linux/fs_parser.h> 501da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h> 511da177e4SLinus Torvalds #include <linux/netfilter_ipv6.h> 521da177e4SLinus Torvalds #include <linux/tty.h> 531da177e4SLinus Torvalds #include <net/icmp.h> 54227b60f5SStephen Hemminger #include <net/ip.h> /* for local_port_range[] */ 551da177e4SLinus Torvalds #include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */ 5647180068SPaul Moore #include <net/inet_connection_sock.h> 57220deb96SPaul Moore #include <net/net_namespace.h> 58d621d35eSPaul Moore #include <net/netlabel.h> 59f5269710SEric Paris #include <linux/uaccess.h> 601da177e4SLinus Torvalds #include <asm/ioctls.h> 6160063497SArun Sharma #include <linux/atomic.h> 621da177e4SLinus Torvalds #include <linux/bitops.h> 631da177e4SLinus Torvalds #include <linux/interrupt.h> 641da177e4SLinus Torvalds #include <linux/netdevice.h> /* for network interface checks */ 6577954983SHong zhi guo #include <net/netlink.h> 661da177e4SLinus Torvalds #include <linux/tcp.h> 671da177e4SLinus Torvalds #include <linux/udp.h> 682ee92d46SJames Morris #include <linux/dccp.h> 69d452930fSRichard Haines #include <linux/sctp.h> 70d452930fSRichard Haines #include <net/sctp/structs.h> 711da177e4SLinus Torvalds #include <linux/quota.h> 721da177e4SLinus Torvalds #include <linux/un.h> /* for Unix socket types */ 731da177e4SLinus Torvalds #include <net/af_unix.h> /* for Unix socket types */ 741da177e4SLinus Torvalds #include <linux/parser.h> 751da177e4SLinus Torvalds #include <linux/nfs_mount.h> 761da177e4SLinus Torvalds #include <net/ipv6.h> 771da177e4SLinus Torvalds #include <linux/hugetlb.h> 781da177e4SLinus Torvalds #include <linux/personality.h> 791da177e4SLinus Torvalds #include <linux/audit.h> 806931dfc9SEric Paris #include <linux/string.h> 8123970741SEric Paris #include <linux/mutex.h> 82f06febc9SFrank Mayhar #include <linux/posix-timers.h> 8300234592SKees Cook #include <linux/syslog.h> 843486740aSSerge E. Hallyn #include <linux/user_namespace.h> 8544fc7ea0SPaul Gortmaker #include <linux/export.h> 8640401530SAl Viro #include <linux/msg.h> 8740401530SAl Viro #include <linux/shm.h> 88ec27c356SChenbo Feng #include <linux/bpf.h> 89ec882da5SOndrej Mosnacek #include <linux/kernfs.h> 90ec882da5SOndrej Mosnacek #include <linux/stringhash.h> /* for hashlen_string() */ 91e262e32dSDavid Howells #include <uapi/linux/mount.h> 92ac5656d8SAaron Goidel #include <linux/fsnotify.h> 93ac5656d8SAaron Goidel #include <linux/fanotify.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 1126c5a682eSStephen Smalley static int selinux_enforcing_boot __initdata; 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 1266c5a682eSStephen Smalley int selinux_enabled_boot __initdata = 1; 1271da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM 1281da177e4SLinus Torvalds static int __init selinux_enabled_setup(char *str) 1291da177e4SLinus Torvalds { 130f5269710SEric Paris unsigned long enabled; 13129707b20SJingoo Han if (!kstrtoul(str, 0, &enabled)) 1326c5a682eSStephen Smalley selinux_enabled_boot = enabled ? 1 : 0; 1331da177e4SLinus Torvalds return 1; 1341da177e4SLinus Torvalds } 1351da177e4SLinus Torvalds __setup("selinux=", selinux_enabled_setup); 1361da177e4SLinus Torvalds #endif 1371da177e4SLinus Torvalds 138aa8e712cSStephen Smalley static unsigned int selinux_checkreqprot_boot = 139aa8e712cSStephen Smalley CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE; 140aa8e712cSStephen Smalley 141aa8e712cSStephen Smalley static int __init checkreqprot_setup(char *str) 142aa8e712cSStephen Smalley { 143aa8e712cSStephen Smalley unsigned long checkreqprot; 144aa8e712cSStephen Smalley 145e9c38f9fSStephen Smalley if (!kstrtoul(str, 0, &checkreqprot)) { 146aa8e712cSStephen Smalley selinux_checkreqprot_boot = checkreqprot ? 1 : 0; 147e9c38f9fSStephen Smalley if (checkreqprot) 148e9c38f9fSStephen Smalley pr_warn("SELinux: checkreqprot set to 1 via kernel parameter. This is deprecated and will be rejected in a future kernel release.\n"); 149e9c38f9fSStephen Smalley } 150aa8e712cSStephen Smalley return 1; 151aa8e712cSStephen Smalley } 152aa8e712cSStephen Smalley __setup("checkreqprot=", checkreqprot_setup); 153aa8e712cSStephen Smalley 154d621d35eSPaul Moore /** 155d621d35eSPaul Moore * selinux_secmark_enabled - Check to see if SECMARK is currently enabled 156d621d35eSPaul Moore * 157d621d35eSPaul Moore * Description: 158d621d35eSPaul Moore * This function checks the SECMARK reference counter to see if any SECMARK 159d621d35eSPaul Moore * targets are currently configured, if the reference counter is greater than 160d621d35eSPaul Moore * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is 1612be4d74fSChris PeBenito * enabled, false (0) if SECMARK is disabled. If the always_check_network 1622be4d74fSChris PeBenito * policy capability is enabled, SECMARK is always considered enabled. 163d621d35eSPaul Moore * 164d621d35eSPaul Moore */ 165d621d35eSPaul Moore static int selinux_secmark_enabled(void) 166d621d35eSPaul Moore { 167aa8e712cSStephen Smalley return (selinux_policycap_alwaysnetwork() || 168aa8e712cSStephen Smalley atomic_read(&selinux_secmark_refcount)); 1692be4d74fSChris PeBenito } 1702be4d74fSChris PeBenito 1712be4d74fSChris PeBenito /** 1722be4d74fSChris PeBenito * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled 1732be4d74fSChris PeBenito * 1742be4d74fSChris PeBenito * Description: 1752be4d74fSChris PeBenito * This function checks if NetLabel or labeled IPSEC is enabled. Returns true 1762be4d74fSChris PeBenito * (1) if any are enabled or false (0) if neither are enabled. If the 1772be4d74fSChris PeBenito * always_check_network policy capability is enabled, peer labeling 1782be4d74fSChris PeBenito * is always considered enabled. 1792be4d74fSChris PeBenito * 1802be4d74fSChris PeBenito */ 1812be4d74fSChris PeBenito static int selinux_peerlbl_enabled(void) 1822be4d74fSChris PeBenito { 183aa8e712cSStephen Smalley return (selinux_policycap_alwaysnetwork() || 184aa8e712cSStephen Smalley netlbl_enabled() || selinux_xfrm_enabled()); 185d621d35eSPaul Moore } 186d621d35eSPaul Moore 187615e51fdSPaul Moore static int selinux_netcache_avc_callback(u32 event) 188615e51fdSPaul Moore { 189615e51fdSPaul Moore if (event == AVC_CALLBACK_RESET) { 190615e51fdSPaul Moore sel_netif_flush(); 191615e51fdSPaul Moore sel_netnode_flush(); 192615e51fdSPaul Moore sel_netport_flush(); 193615e51fdSPaul Moore synchronize_net(); 194615e51fdSPaul Moore } 195615e51fdSPaul Moore return 0; 196615e51fdSPaul Moore } 197615e51fdSPaul Moore 1988f408ab6SDaniel Jurgens static int selinux_lsm_notifier_avc_callback(u32 event) 1998f408ab6SDaniel Jurgens { 200409dcf31SDaniel Jurgens if (event == AVC_CALLBACK_RESET) { 201409dcf31SDaniel Jurgens sel_ib_pkey_flush(); 20242df744cSJanne Karhunen call_blocking_lsm_notifier(LSM_POLICY_CHANGE, NULL); 203409dcf31SDaniel Jurgens } 2048f408ab6SDaniel Jurgens 2058f408ab6SDaniel Jurgens return 0; 2068f408ab6SDaniel Jurgens } 2078f408ab6SDaniel Jurgens 208d84f4f99SDavid Howells /* 209d84f4f99SDavid Howells * initialise the security for the init task 210d84f4f99SDavid Howells */ 211d84f4f99SDavid Howells static void cred_init_security(void) 2121da177e4SLinus Torvalds { 2133b11a1deSDavid Howells struct cred *cred = (struct cred *) current->real_cred; 2141da177e4SLinus Torvalds struct task_security_struct *tsec; 2151da177e4SLinus Torvalds 216bbd3662aSCasey Schaufler tsec = selinux_cred(cred); 217d84f4f99SDavid Howells tsec->osid = tsec->sid = SECINITSID_KERNEL; 2181da177e4SLinus Torvalds } 2191da177e4SLinus Torvalds 220275bb41eSDavid Howells /* 22188e67f3bSDavid Howells * get the security ID of a set of credentials 22288e67f3bSDavid Howells */ 22388e67f3bSDavid Howells static inline u32 cred_sid(const struct cred *cred) 22488e67f3bSDavid Howells { 22588e67f3bSDavid Howells const struct task_security_struct *tsec; 22688e67f3bSDavid Howells 2270c6cfa62SCasey Schaufler tsec = selinux_cred(cred); 22888e67f3bSDavid Howells return tsec->sid; 22988e67f3bSDavid Howells } 23088e67f3bSDavid Howells 23188e67f3bSDavid Howells /* 2323b11a1deSDavid Howells * get the objective security ID of a task 233275bb41eSDavid Howells */ 234275bb41eSDavid Howells static inline u32 task_sid(const struct task_struct *task) 235275bb41eSDavid Howells { 236275bb41eSDavid Howells u32 sid; 237275bb41eSDavid Howells 238275bb41eSDavid Howells rcu_read_lock(); 23988e67f3bSDavid Howells sid = cred_sid(__task_cred(task)); 240275bb41eSDavid Howells rcu_read_unlock(); 241275bb41eSDavid Howells return sid; 242275bb41eSDavid Howells } 243275bb41eSDavid Howells 2445d226df4SAndreas Gruenbacher static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry); 2455d226df4SAndreas Gruenbacher 2465d226df4SAndreas Gruenbacher /* 2475d226df4SAndreas Gruenbacher * Try reloading inode security labels that have been marked as invalid. The 2485d226df4SAndreas Gruenbacher * @may_sleep parameter indicates when sleeping and thus reloading labels is 24942059112SAndreas Gruenbacher * allowed; when set to false, returns -ECHILD when the label is 250e9193288SAl Viro * invalid. The @dentry parameter should be set to a dentry of the inode. 2515d226df4SAndreas Gruenbacher */ 2525d226df4SAndreas Gruenbacher static int __inode_security_revalidate(struct inode *inode, 253e9193288SAl Viro struct dentry *dentry, 2545d226df4SAndreas Gruenbacher bool may_sleep) 2555d226df4SAndreas Gruenbacher { 25680788c22SCasey Schaufler struct inode_security_struct *isec = selinux_inode(inode); 2575d226df4SAndreas Gruenbacher 2585d226df4SAndreas Gruenbacher might_sleep_if(may_sleep); 2595d226df4SAndreas Gruenbacher 26065cddd50SOndrej Mosnacek if (selinux_initialized(&selinux_state) && 261aa8e712cSStephen Smalley isec->initialized != LABEL_INITIALIZED) { 2625d226df4SAndreas Gruenbacher if (!may_sleep) 2635d226df4SAndreas Gruenbacher return -ECHILD; 2645d226df4SAndreas Gruenbacher 2655d226df4SAndreas Gruenbacher /* 2665d226df4SAndreas Gruenbacher * Try reloading the inode security label. This will fail if 2675d226df4SAndreas Gruenbacher * @opt_dentry is NULL and no dentry for this inode can be 2685d226df4SAndreas Gruenbacher * found; in that case, continue using the old label. 2695d226df4SAndreas Gruenbacher */ 270e9193288SAl Viro inode_doinit_with_dentry(inode, dentry); 2715d226df4SAndreas Gruenbacher } 2725d226df4SAndreas Gruenbacher return 0; 2735d226df4SAndreas Gruenbacher } 2745d226df4SAndreas Gruenbacher 2755d226df4SAndreas Gruenbacher static struct inode_security_struct *inode_security_novalidate(struct inode *inode) 2765d226df4SAndreas Gruenbacher { 27780788c22SCasey Schaufler return selinux_inode(inode); 2785d226df4SAndreas Gruenbacher } 2795d226df4SAndreas Gruenbacher 2805d226df4SAndreas Gruenbacher static struct inode_security_struct *inode_security_rcu(struct inode *inode, bool rcu) 2815d226df4SAndreas Gruenbacher { 2825d226df4SAndreas Gruenbacher int error; 2835d226df4SAndreas Gruenbacher 2845d226df4SAndreas Gruenbacher error = __inode_security_revalidate(inode, NULL, !rcu); 2855d226df4SAndreas Gruenbacher if (error) 2865d226df4SAndreas Gruenbacher return ERR_PTR(error); 28780788c22SCasey Schaufler return selinux_inode(inode); 2885d226df4SAndreas Gruenbacher } 2895d226df4SAndreas Gruenbacher 29083da53c5SAndreas Gruenbacher /* 29183da53c5SAndreas Gruenbacher * Get the security label of an inode. 29283da53c5SAndreas Gruenbacher */ 29383da53c5SAndreas Gruenbacher static struct inode_security_struct *inode_security(struct inode *inode) 29483da53c5SAndreas Gruenbacher { 2955d226df4SAndreas Gruenbacher __inode_security_revalidate(inode, NULL, true); 29680788c22SCasey Schaufler return selinux_inode(inode); 29783da53c5SAndreas Gruenbacher } 29883da53c5SAndreas Gruenbacher 2992c97165bSPaul Moore static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry) 3002c97165bSPaul Moore { 3012c97165bSPaul Moore struct inode *inode = d_backing_inode(dentry); 3022c97165bSPaul Moore 30380788c22SCasey Schaufler return selinux_inode(inode); 3042c97165bSPaul Moore } 3052c97165bSPaul Moore 30683da53c5SAndreas Gruenbacher /* 30783da53c5SAndreas Gruenbacher * Get the security label of a dentry's backing inode. 30883da53c5SAndreas Gruenbacher */ 30983da53c5SAndreas Gruenbacher static struct inode_security_struct *backing_inode_security(struct dentry *dentry) 31083da53c5SAndreas Gruenbacher { 31183da53c5SAndreas Gruenbacher struct inode *inode = d_backing_inode(dentry); 31283da53c5SAndreas Gruenbacher 3135d226df4SAndreas Gruenbacher __inode_security_revalidate(inode, dentry, true); 31480788c22SCasey Schaufler return selinux_inode(inode); 31583da53c5SAndreas Gruenbacher } 31683da53c5SAndreas Gruenbacher 3171da177e4SLinus Torvalds static void inode_free_security(struct inode *inode) 3181da177e4SLinus Torvalds { 31980788c22SCasey Schaufler struct inode_security_struct *isec = selinux_inode(inode); 320afb1cbe3SCasey Schaufler struct superblock_security_struct *sbsec; 3211da177e4SLinus Torvalds 322afb1cbe3SCasey Schaufler if (!isec) 323afb1cbe3SCasey Schaufler return; 324afb1cbe3SCasey Schaufler sbsec = inode->i_sb->s_security; 3259629d04aSWaiman Long /* 3269629d04aSWaiman Long * As not all inode security structures are in a list, we check for 3279629d04aSWaiman Long * empty list outside of the lock to make sure that we won't waste 3289629d04aSWaiman Long * time taking a lock doing nothing. 3299629d04aSWaiman Long * 3309629d04aSWaiman Long * The list_del_init() function can be safely called more than once. 3319629d04aSWaiman Long * It should not be possible for this function to be called with 3329629d04aSWaiman Long * concurrent list_add(), but for better safety against future changes 3339629d04aSWaiman Long * in the code, we use list_empty_careful() here. 3349629d04aSWaiman Long */ 3359629d04aSWaiman Long if (!list_empty_careful(&isec->list)) { 3361da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 3371da177e4SLinus Torvalds list_del_init(&isec->list); 3381da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 3399629d04aSWaiman Long } 3401da177e4SLinus Torvalds } 3411da177e4SLinus Torvalds 3421da177e4SLinus Torvalds static void superblock_free_security(struct super_block *sb) 3431da177e4SLinus Torvalds { 3441da177e4SLinus Torvalds struct superblock_security_struct *sbsec = sb->s_security; 3451da177e4SLinus Torvalds sb->s_security = NULL; 3461da177e4SLinus Torvalds kfree(sbsec); 3471da177e4SLinus Torvalds } 3481da177e4SLinus Torvalds 349bd323655SAl Viro struct selinux_mnt_opts { 350bd323655SAl Viro const char *fscontext, *context, *rootcontext, *defcontext; 351bd323655SAl Viro }; 352bd323655SAl Viro 353204cc0ccSAl Viro static void selinux_free_mnt_opts(void *mnt_opts) 354204cc0ccSAl Viro { 355bd323655SAl Viro struct selinux_mnt_opts *opts = mnt_opts; 356bd323655SAl Viro kfree(opts->fscontext); 357bd323655SAl Viro kfree(opts->context); 358bd323655SAl Viro kfree(opts->rootcontext); 359bd323655SAl Viro kfree(opts->defcontext); 360204cc0ccSAl Viro kfree(opts); 361204cc0ccSAl Viro } 362204cc0ccSAl Viro 3631da177e4SLinus Torvalds enum { 36431e87930SEric Paris Opt_error = -1, 365442155c1SDavid Howells Opt_context = 0, 366442155c1SDavid Howells Opt_defcontext = 1, 3671da177e4SLinus Torvalds Opt_fscontext = 2, 368442155c1SDavid Howells Opt_rootcontext = 3, 369442155c1SDavid Howells Opt_seclabel = 4, 3701da177e4SLinus Torvalds }; 3711da177e4SLinus Torvalds 372da3d76abSAl Viro #define A(s, has_arg) {#s, sizeof(#s) - 1, Opt_##s, has_arg} 373169d68efSAl Viro static struct { 374169d68efSAl Viro const char *name; 375169d68efSAl Viro int len; 376169d68efSAl Viro int opt; 377169d68efSAl Viro bool has_arg; 378169d68efSAl Viro } tokens[] = { 379da3d76abSAl Viro A(context, true), 380da3d76abSAl Viro A(fscontext, true), 381da3d76abSAl Viro A(defcontext, true), 382da3d76abSAl Viro A(rootcontext, true), 383da3d76abSAl Viro A(seclabel, false), 3841da177e4SLinus Torvalds }; 385169d68efSAl Viro #undef A 386169d68efSAl Viro 387169d68efSAl Viro static int match_opt_prefix(char *s, int l, char **arg) 388169d68efSAl Viro { 389169d68efSAl Viro int i; 390169d68efSAl Viro 391169d68efSAl Viro for (i = 0; i < ARRAY_SIZE(tokens); i++) { 392169d68efSAl Viro size_t len = tokens[i].len; 393169d68efSAl Viro if (len > l || memcmp(s, tokens[i].name, len)) 394169d68efSAl Viro continue; 395169d68efSAl Viro if (tokens[i].has_arg) { 396169d68efSAl Viro if (len == l || s[len] != '=') 397169d68efSAl Viro continue; 398169d68efSAl Viro *arg = s + len + 1; 399169d68efSAl Viro } else if (len != l) 400169d68efSAl Viro continue; 401169d68efSAl Viro return tokens[i].opt; 402169d68efSAl Viro } 403169d68efSAl Viro return Opt_error; 404169d68efSAl Viro } 4051da177e4SLinus Torvalds 4061da177e4SLinus Torvalds #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n" 4071da177e4SLinus Torvalds 408c312feb2SEric Paris static int may_context_mount_sb_relabel(u32 sid, 409c312feb2SEric Paris struct superblock_security_struct *sbsec, 410275bb41eSDavid Howells const struct cred *cred) 411c312feb2SEric Paris { 4120c6cfa62SCasey Schaufler const struct task_security_struct *tsec = selinux_cred(cred); 413c312feb2SEric Paris int rc; 414c312feb2SEric Paris 4156b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 4166b6bc620SStephen Smalley tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 417c312feb2SEric Paris FILESYSTEM__RELABELFROM, NULL); 418c312feb2SEric Paris if (rc) 419c312feb2SEric Paris return rc; 420c312feb2SEric Paris 4216b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 4226b6bc620SStephen Smalley tsec->sid, sid, SECCLASS_FILESYSTEM, 423c312feb2SEric Paris FILESYSTEM__RELABELTO, NULL); 424c312feb2SEric Paris return rc; 425c312feb2SEric Paris } 426c312feb2SEric Paris 4270808925eSEric Paris static int may_context_mount_inode_relabel(u32 sid, 4280808925eSEric Paris struct superblock_security_struct *sbsec, 429275bb41eSDavid Howells const struct cred *cred) 4300808925eSEric Paris { 4310c6cfa62SCasey Schaufler const struct task_security_struct *tsec = selinux_cred(cred); 4320808925eSEric Paris int rc; 4336b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 4346b6bc620SStephen Smalley tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 4350808925eSEric Paris FILESYSTEM__RELABELFROM, NULL); 4360808925eSEric Paris if (rc) 4370808925eSEric Paris return rc; 4380808925eSEric Paris 4396b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 4406b6bc620SStephen Smalley sid, sbsec->sid, SECCLASS_FILESYSTEM, 4410808925eSEric Paris FILESYSTEM__ASSOCIATE, NULL); 4420808925eSEric Paris return rc; 4430808925eSEric Paris } 4440808925eSEric Paris 445a83d6ddaSOndrej Mosnacek static int selinux_is_genfs_special_handling(struct super_block *sb) 446b43e725dSEric Paris { 447d5f3a5f6SMark Salyzyn /* Special handling. Genfs but also in-core setxattr handler */ 448a83d6ddaSOndrej Mosnacek return !strcmp(sb->s_type->name, "sysfs") || 449d5f3a5f6SMark Salyzyn !strcmp(sb->s_type->name, "pstore") || 450d5f3a5f6SMark Salyzyn !strcmp(sb->s_type->name, "debugfs") || 451a2c7c6fbSYongqin Liu !strcmp(sb->s_type->name, "tracefs") || 4522651225bSStephen Smalley !strcmp(sb->s_type->name, "rootfs") || 453aa8e712cSStephen Smalley (selinux_policycap_cgroupseclabel() && 4542651225bSStephen Smalley (!strcmp(sb->s_type->name, "cgroup") || 4552651225bSStephen Smalley !strcmp(sb->s_type->name, "cgroup2"))); 456b43e725dSEric Paris } 457b43e725dSEric Paris 458a83d6ddaSOndrej Mosnacek static int selinux_is_sblabel_mnt(struct super_block *sb) 459a83d6ddaSOndrej Mosnacek { 460a83d6ddaSOndrej Mosnacek struct superblock_security_struct *sbsec = sb->s_security; 461a83d6ddaSOndrej Mosnacek 462a83d6ddaSOndrej Mosnacek /* 463a83d6ddaSOndrej Mosnacek * IMPORTANT: Double-check logic in this function when adding a new 464a83d6ddaSOndrej Mosnacek * SECURITY_FS_USE_* definition! 465a83d6ddaSOndrej Mosnacek */ 466a83d6ddaSOndrej Mosnacek BUILD_BUG_ON(SECURITY_FS_USE_MAX != 7); 467a83d6ddaSOndrej Mosnacek 468a83d6ddaSOndrej Mosnacek switch (sbsec->behavior) { 469a83d6ddaSOndrej Mosnacek case SECURITY_FS_USE_XATTR: 470a83d6ddaSOndrej Mosnacek case SECURITY_FS_USE_TRANS: 471a83d6ddaSOndrej Mosnacek case SECURITY_FS_USE_TASK: 472a83d6ddaSOndrej Mosnacek case SECURITY_FS_USE_NATIVE: 473a83d6ddaSOndrej Mosnacek return 1; 474a83d6ddaSOndrej Mosnacek 475a83d6ddaSOndrej Mosnacek case SECURITY_FS_USE_GENFS: 476a83d6ddaSOndrej Mosnacek return selinux_is_genfs_special_handling(sb); 477a83d6ddaSOndrej Mosnacek 478a83d6ddaSOndrej Mosnacek /* Never allow relabeling on context mounts */ 479a83d6ddaSOndrej Mosnacek case SECURITY_FS_USE_MNTPOINT: 480a83d6ddaSOndrej Mosnacek case SECURITY_FS_USE_NONE: 481a83d6ddaSOndrej Mosnacek default: 482a83d6ddaSOndrej Mosnacek return 0; 483a83d6ddaSOndrej Mosnacek } 484a83d6ddaSOndrej Mosnacek } 485a83d6ddaSOndrej Mosnacek 486c9180a57SEric Paris static int sb_finish_set_opts(struct super_block *sb) 4871da177e4SLinus Torvalds { 4881da177e4SLinus Torvalds struct superblock_security_struct *sbsec = sb->s_security; 4891da177e4SLinus Torvalds struct dentry *root = sb->s_root; 490c6f493d6SDavid Howells struct inode *root_inode = d_backing_inode(root); 4911da177e4SLinus Torvalds int rc = 0; 4921da177e4SLinus Torvalds 4931da177e4SLinus Torvalds if (sbsec->behavior == SECURITY_FS_USE_XATTR) { 4941da177e4SLinus Torvalds /* Make sure that the xattr handler exists and that no 4951da177e4SLinus Torvalds error other than -ENODATA is returned by getxattr on 4961da177e4SLinus Torvalds the root directory. -ENODATA is ok, as this may be 4971da177e4SLinus Torvalds the first boot of the SELinux kernel before we have 4981da177e4SLinus Torvalds assigned xattr values to the filesystem. */ 4995d6c3191SAndreas Gruenbacher if (!(root_inode->i_opflags & IOP_XATTR)) { 500c103a91eSpeter enderborg pr_warn("SELinux: (dev %s, type %s) has no " 50129b1deb2SLinus Torvalds "xattr support\n", sb->s_id, sb->s_type->name); 5021da177e4SLinus Torvalds rc = -EOPNOTSUPP; 5031da177e4SLinus Torvalds goto out; 5041da177e4SLinus Torvalds } 5055d6c3191SAndreas Gruenbacher 5065d6c3191SAndreas Gruenbacher rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0); 5071da177e4SLinus Torvalds if (rc < 0 && rc != -ENODATA) { 5081da177e4SLinus Torvalds if (rc == -EOPNOTSUPP) 509c103a91eSpeter enderborg pr_warn("SELinux: (dev %s, type " 51029b1deb2SLinus Torvalds "%s) has no security xattr handler\n", 51129b1deb2SLinus Torvalds sb->s_id, sb->s_type->name); 5121da177e4SLinus Torvalds else 513c103a91eSpeter enderborg pr_warn("SELinux: (dev %s, type " 51429b1deb2SLinus Torvalds "%s) getxattr errno %d\n", sb->s_id, 51529b1deb2SLinus Torvalds sb->s_type->name, -rc); 5161da177e4SLinus Torvalds goto out; 5171da177e4SLinus Torvalds } 5181da177e4SLinus Torvalds } 5191da177e4SLinus Torvalds 520eadcabc6SEric Paris sbsec->flags |= SE_SBINITIALIZED; 5210b4d3452SScott Mayhew 5220b4d3452SScott Mayhew /* 5230b4d3452SScott Mayhew * Explicitly set or clear SBLABEL_MNT. It's not sufficient to simply 5240b4d3452SScott Mayhew * leave the flag untouched because sb_clone_mnt_opts might be handing 5250b4d3452SScott Mayhew * us a superblock that needs the flag to be cleared. 5260b4d3452SScott Mayhew */ 527b43e725dSEric Paris if (selinux_is_sblabel_mnt(sb)) 52812f348b9SEric Paris sbsec->flags |= SBLABEL_MNT; 5290b4d3452SScott Mayhew else 5300b4d3452SScott Mayhew sbsec->flags &= ~SBLABEL_MNT; 531ddd29ec6SDavid P. Quigley 5321da177e4SLinus Torvalds /* Initialize the root inode. */ 533c9180a57SEric Paris rc = inode_doinit_with_dentry(root_inode, root); 5341da177e4SLinus Torvalds 5351da177e4SLinus Torvalds /* Initialize any other inodes associated with the superblock, e.g. 5361da177e4SLinus Torvalds inodes created prior to initial policy load or inodes created 5371da177e4SLinus Torvalds during get_sb by a pseudo filesystem that directly 5381da177e4SLinus Torvalds populates itself. */ 5391da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 5408d64124aSAl Viro while (!list_empty(&sbsec->isec_head)) { 5411da177e4SLinus Torvalds struct inode_security_struct *isec = 5428d64124aSAl Viro list_first_entry(&sbsec->isec_head, 5431da177e4SLinus Torvalds struct inode_security_struct, list); 5441da177e4SLinus Torvalds struct inode *inode = isec->inode; 545923190d3SStephen Smalley list_del_init(&isec->list); 5461da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 5471da177e4SLinus Torvalds inode = igrab(inode); 5481da177e4SLinus Torvalds if (inode) { 5491da177e4SLinus Torvalds if (!IS_PRIVATE(inode)) 550cb89e246SPaul Moore inode_doinit_with_dentry(inode, NULL); 5511da177e4SLinus Torvalds iput(inode); 5521da177e4SLinus Torvalds } 5531da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 5541da177e4SLinus Torvalds } 5551da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 5561da177e4SLinus Torvalds out: 557c9180a57SEric Paris return rc; 558c9180a57SEric Paris } 559c9180a57SEric Paris 560c9180a57SEric Paris static int bad_option(struct superblock_security_struct *sbsec, char flag, 561c9180a57SEric Paris u32 old_sid, u32 new_sid) 562c9180a57SEric Paris { 5630d90a7ecSDavid P. Quigley char mnt_flags = sbsec->flags & SE_MNTMASK; 5640d90a7ecSDavid P. Quigley 565c9180a57SEric Paris /* check if the old mount command had the same options */ 5660d90a7ecSDavid P. Quigley if (sbsec->flags & SE_SBINITIALIZED) 567c9180a57SEric Paris if (!(sbsec->flags & flag) || 568c9180a57SEric Paris (old_sid != new_sid)) 569c9180a57SEric Paris return 1; 570c9180a57SEric Paris 571c9180a57SEric Paris /* check if we were passed the same options twice, 572c9180a57SEric Paris * aka someone passed context=a,context=b 573c9180a57SEric Paris */ 5740d90a7ecSDavid P. Quigley if (!(sbsec->flags & SE_SBINITIALIZED)) 5750d90a7ecSDavid P. Quigley if (mnt_flags & flag) 576c9180a57SEric Paris return 1; 577c9180a57SEric Paris return 0; 578c9180a57SEric Paris } 579e0007529SEric Paris 580bd323655SAl Viro static int parse_sid(struct super_block *sb, const char *s, u32 *sid) 581bd323655SAl Viro { 582bd323655SAl Viro int rc = security_context_str_to_sid(&selinux_state, s, 583bd323655SAl Viro sid, GFP_KERNEL); 584bd323655SAl Viro if (rc) 585bd323655SAl Viro pr_warn("SELinux: security_context_str_to_sid" 586bd323655SAl Viro "(%s) failed for (dev %s, type %s) errno=%d\n", 587bd323655SAl Viro s, sb->s_id, sb->s_type->name, rc); 588bd323655SAl Viro return rc; 589bd323655SAl Viro } 590bd323655SAl Viro 591c9180a57SEric Paris /* 592c9180a57SEric Paris * Allow filesystems with binary mount data to explicitly set mount point 593c9180a57SEric Paris * labeling information. 594c9180a57SEric Paris */ 595e0007529SEric Paris static int selinux_set_mnt_opts(struct super_block *sb, 596204cc0ccSAl Viro void *mnt_opts, 597649f6e77SDavid Quigley unsigned long kern_flags, 598649f6e77SDavid Quigley unsigned long *set_kern_flags) 599c9180a57SEric Paris { 600275bb41eSDavid Howells const struct cred *cred = current_cred(); 601c9180a57SEric Paris struct superblock_security_struct *sbsec = sb->s_security; 60283da53c5SAndreas Gruenbacher struct dentry *root = sbsec->sb->s_root; 603bd323655SAl Viro struct selinux_mnt_opts *opts = mnt_opts; 6042c97165bSPaul Moore struct inode_security_struct *root_isec; 605c9180a57SEric Paris u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0; 606c9180a57SEric Paris u32 defcontext_sid = 0; 607bd323655SAl Viro int rc = 0; 608c9180a57SEric Paris 609c9180a57SEric Paris mutex_lock(&sbsec->lock); 610c9180a57SEric Paris 61165cddd50SOndrej Mosnacek if (!selinux_initialized(&selinux_state)) { 612bd323655SAl Viro if (!opts) { 613c9180a57SEric Paris /* Defer initialization until selinux_complete_init, 614c9180a57SEric Paris after the initial policy is loaded and the security 615c9180a57SEric Paris server is ready to handle calls. */ 616c9180a57SEric Paris goto out; 617c9180a57SEric Paris } 618c9180a57SEric Paris rc = -EINVAL; 619c103a91eSpeter enderborg pr_warn("SELinux: Unable to set superblock options " 620744ba35eSEric Paris "before the security server is initialized\n"); 621c9180a57SEric Paris goto out; 622c9180a57SEric Paris } 623649f6e77SDavid Quigley if (kern_flags && !set_kern_flags) { 624649f6e77SDavid Quigley /* Specifying internal flags without providing a place to 625649f6e77SDavid Quigley * place the results is not allowed */ 626649f6e77SDavid Quigley rc = -EINVAL; 627649f6e77SDavid Quigley goto out; 628649f6e77SDavid Quigley } 629c9180a57SEric Paris 630c9180a57SEric Paris /* 631e0007529SEric Paris * Binary mount data FS will come through this function twice. Once 632e0007529SEric Paris * from an explicit call and once from the generic calls from the vfs. 633e0007529SEric Paris * Since the generic VFS calls will not contain any security mount data 634e0007529SEric Paris * we need to skip the double mount verification. 635e0007529SEric Paris * 636e0007529SEric Paris * This does open a hole in which we will not notice if the first 637e0007529SEric Paris * mount using this sb set explict options and a second mount using 638e0007529SEric Paris * this sb does not set any security options. (The first options 639e0007529SEric Paris * will be used for both mounts) 640e0007529SEric Paris */ 6410d90a7ecSDavid P. Quigley if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA) 642bd323655SAl Viro && !opts) 643e0007529SEric Paris goto out; 644e0007529SEric Paris 6452c97165bSPaul Moore root_isec = backing_inode_security_novalidate(root); 6462c97165bSPaul Moore 647e0007529SEric Paris /* 648c9180a57SEric Paris * parse the mount options, check if they are valid sids. 649c9180a57SEric Paris * also check if someone is trying to mount the same sb more 650c9180a57SEric Paris * than once with different security options. 651c9180a57SEric Paris */ 652bd323655SAl Viro if (opts) { 653bd323655SAl Viro if (opts->fscontext) { 654bd323655SAl Viro rc = parse_sid(sb, opts->fscontext, &fscontext_sid); 655bd323655SAl Viro if (rc) 656c9180a57SEric Paris goto out; 657c9180a57SEric Paris if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, 658c9180a57SEric Paris fscontext_sid)) 659c9180a57SEric Paris goto out_double_mount; 660c9180a57SEric Paris sbsec->flags |= FSCONTEXT_MNT; 661bd323655SAl Viro } 662bd323655SAl Viro if (opts->context) { 663bd323655SAl Viro rc = parse_sid(sb, opts->context, &context_sid); 664bd323655SAl Viro if (rc) 665bd323655SAl Viro goto out; 666c9180a57SEric Paris if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, 667c9180a57SEric Paris context_sid)) 668c9180a57SEric Paris goto out_double_mount; 669c9180a57SEric Paris sbsec->flags |= CONTEXT_MNT; 670bd323655SAl Viro } 671bd323655SAl Viro if (opts->rootcontext) { 672bd323655SAl Viro rc = parse_sid(sb, opts->rootcontext, &rootcontext_sid); 673bd323655SAl Viro if (rc) 674bd323655SAl Viro goto out; 675c9180a57SEric Paris if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, 676c9180a57SEric Paris rootcontext_sid)) 677c9180a57SEric Paris goto out_double_mount; 678c9180a57SEric Paris sbsec->flags |= ROOTCONTEXT_MNT; 679bd323655SAl Viro } 680bd323655SAl Viro if (opts->defcontext) { 681bd323655SAl Viro rc = parse_sid(sb, opts->defcontext, &defcontext_sid); 682bd323655SAl Viro if (rc) 683bd323655SAl Viro goto out; 684c9180a57SEric Paris if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, 685c9180a57SEric Paris defcontext_sid)) 686c9180a57SEric Paris goto out_double_mount; 687c9180a57SEric Paris sbsec->flags |= DEFCONTEXT_MNT; 688c9180a57SEric Paris } 689c9180a57SEric Paris } 690c9180a57SEric Paris 6910d90a7ecSDavid P. Quigley if (sbsec->flags & SE_SBINITIALIZED) { 692c9180a57SEric Paris /* previously mounted with options, but not on this attempt? */ 693bd323655SAl Viro if ((sbsec->flags & SE_MNTMASK) && !opts) 694c9180a57SEric Paris goto out_double_mount; 695c9180a57SEric Paris rc = 0; 696c9180a57SEric Paris goto out; 697c9180a57SEric Paris } 698c9180a57SEric Paris 699089be43eSJames Morris if (strcmp(sb->s_type->name, "proc") == 0) 700134509d5SStephen Smalley sbsec->flags |= SE_SBPROC | SE_SBGENFS; 701134509d5SStephen Smalley 7028e014720SStephen Smalley if (!strcmp(sb->s_type->name, "debugfs") || 7036a391183SJeff Vander Stoep !strcmp(sb->s_type->name, "tracefs") || 7047a4b5194SHridya Valsaraju !strcmp(sb->s_type->name, "binderfs") || 705b754026bSOndrej Mosnacek !strcmp(sb->s_type->name, "pstore")) 706b754026bSOndrej Mosnacek sbsec->flags |= SE_SBGENFS; 707b754026bSOndrej Mosnacek 708b754026bSOndrej Mosnacek if (!strcmp(sb->s_type->name, "sysfs") || 709901ef845SAntonio Murdaca !strcmp(sb->s_type->name, "cgroup") || 710901ef845SAntonio Murdaca !strcmp(sb->s_type->name, "cgroup2")) 711b754026bSOndrej Mosnacek sbsec->flags |= SE_SBGENFS | SE_SBGENFS_XATTR; 712c9180a57SEric Paris 713eb9ae686SDavid Quigley if (!sbsec->behavior) { 714eb9ae686SDavid Quigley /* 715eb9ae686SDavid Quigley * Determine the labeling behavior to use for this 716eb9ae686SDavid Quigley * filesystem type. 717eb9ae686SDavid Quigley */ 718aa8e712cSStephen Smalley rc = security_fs_use(&selinux_state, sb); 719c9180a57SEric Paris if (rc) { 720c103a91eSpeter enderborg pr_warn("%s: security_fs_use(%s) returned %d\n", 721089be43eSJames Morris __func__, sb->s_type->name, rc); 722c9180a57SEric Paris goto out; 723c9180a57SEric Paris } 724eb9ae686SDavid Quigley } 725aad82892SSeth Forshee 726aad82892SSeth Forshee /* 72701593d32SStephen Smalley * If this is a user namespace mount and the filesystem type is not 72801593d32SStephen Smalley * explicitly whitelisted, then no contexts are allowed on the command 72901593d32SStephen Smalley * line and security labels must be ignored. 730aad82892SSeth Forshee */ 73101593d32SStephen Smalley if (sb->s_user_ns != &init_user_ns && 73201593d32SStephen Smalley strcmp(sb->s_type->name, "tmpfs") && 73301593d32SStephen Smalley strcmp(sb->s_type->name, "ramfs") && 73401593d32SStephen Smalley strcmp(sb->s_type->name, "devpts")) { 735aad82892SSeth Forshee if (context_sid || fscontext_sid || rootcontext_sid || 736aad82892SSeth Forshee defcontext_sid) { 737aad82892SSeth Forshee rc = -EACCES; 738aad82892SSeth Forshee goto out; 739aad82892SSeth Forshee } 740aad82892SSeth Forshee if (sbsec->behavior == SECURITY_FS_USE_XATTR) { 741aad82892SSeth Forshee sbsec->behavior = SECURITY_FS_USE_MNTPOINT; 742aa8e712cSStephen Smalley rc = security_transition_sid(&selinux_state, 743aa8e712cSStephen Smalley current_sid(), 744aa8e712cSStephen Smalley current_sid(), 745aad82892SSeth Forshee SECCLASS_FILE, NULL, 746aad82892SSeth Forshee &sbsec->mntpoint_sid); 747aad82892SSeth Forshee if (rc) 748aad82892SSeth Forshee goto out; 749aad82892SSeth Forshee } 750aad82892SSeth Forshee goto out_set_opts; 751aad82892SSeth Forshee } 752aad82892SSeth Forshee 753c9180a57SEric Paris /* sets the context of the superblock for the fs being mounted. */ 754c9180a57SEric Paris if (fscontext_sid) { 755275bb41eSDavid Howells rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred); 756c9180a57SEric Paris if (rc) 757c9180a57SEric Paris goto out; 758c9180a57SEric Paris 759c9180a57SEric Paris sbsec->sid = fscontext_sid; 760c9180a57SEric Paris } 761c9180a57SEric Paris 762c9180a57SEric Paris /* 763c9180a57SEric Paris * Switch to using mount point labeling behavior. 764c9180a57SEric Paris * sets the label used on all file below the mountpoint, and will set 765c9180a57SEric Paris * the superblock context if not already set. 766c9180a57SEric Paris */ 767eb9ae686SDavid Quigley if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) { 768eb9ae686SDavid Quigley sbsec->behavior = SECURITY_FS_USE_NATIVE; 769eb9ae686SDavid Quigley *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS; 770eb9ae686SDavid Quigley } 771eb9ae686SDavid Quigley 772c9180a57SEric Paris if (context_sid) { 773c9180a57SEric Paris if (!fscontext_sid) { 774275bb41eSDavid Howells rc = may_context_mount_sb_relabel(context_sid, sbsec, 775275bb41eSDavid Howells cred); 776c9180a57SEric Paris if (rc) 777c9180a57SEric Paris goto out; 778c9180a57SEric Paris sbsec->sid = context_sid; 779c9180a57SEric Paris } else { 780275bb41eSDavid Howells rc = may_context_mount_inode_relabel(context_sid, sbsec, 781275bb41eSDavid Howells cred); 782c9180a57SEric Paris if (rc) 783c9180a57SEric Paris goto out; 784c9180a57SEric Paris } 785c9180a57SEric Paris if (!rootcontext_sid) 786c9180a57SEric Paris rootcontext_sid = context_sid; 787c9180a57SEric Paris 788c9180a57SEric Paris sbsec->mntpoint_sid = context_sid; 789c9180a57SEric Paris sbsec->behavior = SECURITY_FS_USE_MNTPOINT; 790c9180a57SEric Paris } 791c9180a57SEric Paris 792c9180a57SEric Paris if (rootcontext_sid) { 793275bb41eSDavid Howells rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec, 794275bb41eSDavid Howells cred); 795c9180a57SEric Paris if (rc) 796c9180a57SEric Paris goto out; 797c9180a57SEric Paris 798c9180a57SEric Paris root_isec->sid = rootcontext_sid; 7996f3be9f5SAndreas Gruenbacher root_isec->initialized = LABEL_INITIALIZED; 800c9180a57SEric Paris } 801c9180a57SEric Paris 802c9180a57SEric Paris if (defcontext_sid) { 803eb9ae686SDavid Quigley if (sbsec->behavior != SECURITY_FS_USE_XATTR && 804eb9ae686SDavid Quigley sbsec->behavior != SECURITY_FS_USE_NATIVE) { 805c9180a57SEric Paris rc = -EINVAL; 806c103a91eSpeter enderborg pr_warn("SELinux: defcontext option is " 807c9180a57SEric Paris "invalid for this filesystem type\n"); 808c9180a57SEric Paris goto out; 809c9180a57SEric Paris } 810c9180a57SEric Paris 811c9180a57SEric Paris if (defcontext_sid != sbsec->def_sid) { 812c9180a57SEric Paris rc = may_context_mount_inode_relabel(defcontext_sid, 813275bb41eSDavid Howells sbsec, cred); 814c9180a57SEric Paris if (rc) 815c9180a57SEric Paris goto out; 816c9180a57SEric Paris } 817c9180a57SEric Paris 818c9180a57SEric Paris sbsec->def_sid = defcontext_sid; 819c9180a57SEric Paris } 820c9180a57SEric Paris 821aad82892SSeth Forshee out_set_opts: 822c9180a57SEric Paris rc = sb_finish_set_opts(sb); 823c9180a57SEric Paris out: 824bc7e982bSEric Paris mutex_unlock(&sbsec->lock); 8251da177e4SLinus Torvalds return rc; 826c9180a57SEric Paris out_double_mount: 827c9180a57SEric Paris rc = -EINVAL; 828c103a91eSpeter enderborg pr_warn("SELinux: mount invalid. Same superblock, different " 829bd323655SAl Viro "security settings for (dev %s, type %s)\n", sb->s_id, 830bd323655SAl Viro sb->s_type->name); 831c9180a57SEric Paris goto out; 832c9180a57SEric Paris } 833c9180a57SEric Paris 834094f7b69SJeff Layton static int selinux_cmp_sb_context(const struct super_block *oldsb, 835094f7b69SJeff Layton const struct super_block *newsb) 836094f7b69SJeff Layton { 837094f7b69SJeff Layton struct superblock_security_struct *old = oldsb->s_security; 838094f7b69SJeff Layton struct superblock_security_struct *new = newsb->s_security; 839094f7b69SJeff Layton char oldflags = old->flags & SE_MNTMASK; 840094f7b69SJeff Layton char newflags = new->flags & SE_MNTMASK; 841094f7b69SJeff Layton 842094f7b69SJeff Layton if (oldflags != newflags) 843094f7b69SJeff Layton goto mismatch; 844094f7b69SJeff Layton if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid) 845094f7b69SJeff Layton goto mismatch; 846094f7b69SJeff Layton if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid) 847094f7b69SJeff Layton goto mismatch; 848094f7b69SJeff Layton if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid) 849094f7b69SJeff Layton goto mismatch; 850094f7b69SJeff Layton if (oldflags & ROOTCONTEXT_MNT) { 85183da53c5SAndreas Gruenbacher struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root); 85283da53c5SAndreas Gruenbacher struct inode_security_struct *newroot = backing_inode_security(newsb->s_root); 853094f7b69SJeff Layton if (oldroot->sid != newroot->sid) 854094f7b69SJeff Layton goto mismatch; 855094f7b69SJeff Layton } 856094f7b69SJeff Layton return 0; 857094f7b69SJeff Layton mismatch: 858c103a91eSpeter enderborg pr_warn("SELinux: mount invalid. Same superblock, " 859094f7b69SJeff Layton "different security settings for (dev %s, " 860094f7b69SJeff Layton "type %s)\n", newsb->s_id, newsb->s_type->name); 861094f7b69SJeff Layton return -EBUSY; 862094f7b69SJeff Layton } 863094f7b69SJeff Layton 864094f7b69SJeff Layton static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb, 8650b4d3452SScott Mayhew struct super_block *newsb, 8660b4d3452SScott Mayhew unsigned long kern_flags, 8670b4d3452SScott Mayhew unsigned long *set_kern_flags) 868c9180a57SEric Paris { 8690b4d3452SScott Mayhew int rc = 0; 870c9180a57SEric Paris const struct superblock_security_struct *oldsbsec = oldsb->s_security; 871c9180a57SEric Paris struct superblock_security_struct *newsbsec = newsb->s_security; 872c9180a57SEric Paris 873c9180a57SEric Paris int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT); 874c9180a57SEric Paris int set_context = (oldsbsec->flags & CONTEXT_MNT); 875c9180a57SEric Paris int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT); 876c9180a57SEric Paris 8770f5e6420SEric Paris /* 8780f5e6420SEric Paris * if the parent was able to be mounted it clearly had no special lsm 879e8c26255SAl Viro * mount options. thus we can safely deal with this superblock later 8800f5e6420SEric Paris */ 88165cddd50SOndrej Mosnacek if (!selinux_initialized(&selinux_state)) 882094f7b69SJeff Layton return 0; 883c9180a57SEric Paris 8840b4d3452SScott Mayhew /* 8850b4d3452SScott Mayhew * Specifying internal flags without providing a place to 8860b4d3452SScott Mayhew * place the results is not allowed. 8870b4d3452SScott Mayhew */ 8880b4d3452SScott Mayhew if (kern_flags && !set_kern_flags) 8890b4d3452SScott Mayhew return -EINVAL; 8900b4d3452SScott Mayhew 891c9180a57SEric Paris /* how can we clone if the old one wasn't set up?? */ 8920d90a7ecSDavid P. Quigley BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED)); 893c9180a57SEric Paris 894094f7b69SJeff Layton /* if fs is reusing a sb, make sure that the contexts match */ 8953815a245SJ. Bruce Fields if (newsbsec->flags & SE_SBINITIALIZED) { 8963815a245SJ. Bruce Fields if ((kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) 8973815a245SJ. Bruce Fields *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS; 898094f7b69SJeff Layton return selinux_cmp_sb_context(oldsb, newsb); 8993815a245SJ. Bruce Fields } 9005a552617SEric Paris 901c9180a57SEric Paris mutex_lock(&newsbsec->lock); 902c9180a57SEric Paris 903c9180a57SEric Paris newsbsec->flags = oldsbsec->flags; 904c9180a57SEric Paris 905c9180a57SEric Paris newsbsec->sid = oldsbsec->sid; 906c9180a57SEric Paris newsbsec->def_sid = oldsbsec->def_sid; 907c9180a57SEric Paris newsbsec->behavior = oldsbsec->behavior; 908c9180a57SEric Paris 9090b4d3452SScott Mayhew if (newsbsec->behavior == SECURITY_FS_USE_NATIVE && 9100b4d3452SScott Mayhew !(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) { 911aa8e712cSStephen Smalley rc = security_fs_use(&selinux_state, newsb); 9120b4d3452SScott Mayhew if (rc) 9130b4d3452SScott Mayhew goto out; 9140b4d3452SScott Mayhew } 9150b4d3452SScott Mayhew 9160b4d3452SScott Mayhew if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) { 9170b4d3452SScott Mayhew newsbsec->behavior = SECURITY_FS_USE_NATIVE; 9180b4d3452SScott Mayhew *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS; 9190b4d3452SScott Mayhew } 9200b4d3452SScott Mayhew 921c9180a57SEric Paris if (set_context) { 922c9180a57SEric Paris u32 sid = oldsbsec->mntpoint_sid; 923c9180a57SEric Paris 924c9180a57SEric Paris if (!set_fscontext) 925c9180a57SEric Paris newsbsec->sid = sid; 926c9180a57SEric Paris if (!set_rootcontext) { 92783da53c5SAndreas Gruenbacher struct inode_security_struct *newisec = backing_inode_security(newsb->s_root); 928c9180a57SEric Paris newisec->sid = sid; 929c9180a57SEric Paris } 930c9180a57SEric Paris newsbsec->mntpoint_sid = sid; 931c9180a57SEric Paris } 932c9180a57SEric Paris if (set_rootcontext) { 93383da53c5SAndreas Gruenbacher const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root); 93483da53c5SAndreas Gruenbacher struct inode_security_struct *newisec = backing_inode_security(newsb->s_root); 935c9180a57SEric Paris 936c9180a57SEric Paris newisec->sid = oldisec->sid; 937c9180a57SEric Paris } 938c9180a57SEric Paris 939c9180a57SEric Paris sb_finish_set_opts(newsb); 9400b4d3452SScott Mayhew out: 941c9180a57SEric Paris mutex_unlock(&newsbsec->lock); 9420b4d3452SScott Mayhew return rc; 943c9180a57SEric Paris } 944c9180a57SEric Paris 945ba641862SAl Viro static int selinux_add_opt(int token, const char *s, void **mnt_opts) 946c9180a57SEric Paris { 947ba641862SAl Viro struct selinux_mnt_opts *opts = *mnt_opts; 948c9180a57SEric Paris 949da3d76abSAl Viro if (token == Opt_seclabel) /* eaten and completely ignored */ 950e0007529SEric Paris return 0; 951e0007529SEric Paris 952ba641862SAl Viro if (!opts) { 953ba641862SAl Viro opts = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL); 954ba641862SAl Viro if (!opts) 955ba641862SAl Viro return -ENOMEM; 956ba641862SAl Viro *mnt_opts = opts; 957ba641862SAl Viro } 958ba641862SAl Viro if (!s) 959ba641862SAl Viro return -ENOMEM; 960ba641862SAl Viro switch (token) { 961ba641862SAl Viro case Opt_context: 962ba641862SAl Viro if (opts->context || opts->defcontext) 963ba641862SAl Viro goto Einval; 964ba641862SAl Viro opts->context = s; 965ba641862SAl Viro break; 966ba641862SAl Viro case Opt_fscontext: 967ba641862SAl Viro if (opts->fscontext) 968ba641862SAl Viro goto Einval; 969ba641862SAl Viro opts->fscontext = s; 970ba641862SAl Viro break; 971ba641862SAl Viro case Opt_rootcontext: 972ba641862SAl Viro if (opts->rootcontext) 973ba641862SAl Viro goto Einval; 974ba641862SAl Viro opts->rootcontext = s; 975ba641862SAl Viro break; 976ba641862SAl Viro case Opt_defcontext: 977ba641862SAl Viro if (opts->context || opts->defcontext) 978ba641862SAl Viro goto Einval; 979ba641862SAl Viro opts->defcontext = s; 980ba641862SAl Viro break; 981ba641862SAl Viro } 982ba641862SAl Viro return 0; 983ba641862SAl Viro Einval: 984ba641862SAl Viro pr_warn(SEL_MOUNT_FAIL_MSG); 985ba641862SAl Viro return -EINVAL; 986ba641862SAl Viro } 987ba641862SAl Viro 988757cbe59SAl Viro static int selinux_add_mnt_opt(const char *option, const char *val, int len, 989204cc0ccSAl Viro void **mnt_opts) 990c9180a57SEric Paris { 991757cbe59SAl Viro int token = Opt_error; 992757cbe59SAl Viro int rc, i; 993c9180a57SEric Paris 994757cbe59SAl Viro for (i = 0; i < ARRAY_SIZE(tokens); i++) { 995757cbe59SAl Viro if (strcmp(option, tokens[i].name) == 0) { 996757cbe59SAl Viro token = tokens[i].opt; 997757cbe59SAl Viro break; 998757cbe59SAl Viro } 999169d68efSAl Viro } 1000169d68efSAl Viro 1001757cbe59SAl Viro if (token == Opt_error) 1002757cbe59SAl Viro return -EINVAL; 1003c9180a57SEric Paris 1004e2e0e097SGen Zhang if (token != Opt_seclabel) { 1005757cbe59SAl Viro val = kmemdup_nul(val, len, GFP_KERNEL); 1006e2e0e097SGen Zhang if (!val) { 1007e2e0e097SGen Zhang rc = -ENOMEM; 1008e2e0e097SGen Zhang goto free_opt; 1009e2e0e097SGen Zhang } 1010e2e0e097SGen Zhang } 1011757cbe59SAl Viro rc = selinux_add_opt(token, val, mnt_opts); 1012757cbe59SAl Viro if (unlikely(rc)) { 1013757cbe59SAl Viro kfree(val); 1014e2e0e097SGen Zhang goto free_opt; 1015e2e0e097SGen Zhang } 1016e2e0e097SGen Zhang return rc; 1017e2e0e097SGen Zhang 1018e2e0e097SGen Zhang free_opt: 1019757cbe59SAl Viro if (*mnt_opts) { 1020ba641862SAl Viro selinux_free_mnt_opts(*mnt_opts); 1021ba641862SAl Viro *mnt_opts = NULL; 1022757cbe59SAl Viro } 1023c9180a57SEric Paris return rc; 10241da177e4SLinus Torvalds } 10251da177e4SLinus Torvalds 1026e3489f89SAl Viro static int show_sid(struct seq_file *m, u32 sid) 10272069f457SEric Paris { 1028e3489f89SAl Viro char *context = NULL; 1029e3489f89SAl Viro u32 len; 1030e3489f89SAl Viro int rc; 10312069f457SEric Paris 1032e3489f89SAl Viro rc = security_sid_to_context(&selinux_state, sid, 1033e3489f89SAl Viro &context, &len); 1034e3489f89SAl Viro if (!rc) { 1035e3489f89SAl Viro bool has_comma = context && strchr(context, ','); 103611689d47SDavid P. Quigley 1037442155c1SDavid Howells seq_putc(m, '='); 10382069f457SEric Paris if (has_comma) 10392069f457SEric Paris seq_putc(m, '\"'); 1040e3489f89SAl Viro seq_escape(m, context, "\"\n\\"); 10412069f457SEric Paris if (has_comma) 10422069f457SEric Paris seq_putc(m, '\"'); 10432069f457SEric Paris } 10441da177e4SLinus Torvalds kfree(context); 10451da177e4SLinus Torvalds return rc; 10461da177e4SLinus Torvalds } 10472069f457SEric Paris 10482069f457SEric Paris static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb) 10492069f457SEric Paris { 1050e3489f89SAl Viro struct superblock_security_struct *sbsec = sb->s_security; 10512069f457SEric Paris int rc; 10522069f457SEric Paris 1053e3489f89SAl Viro if (!(sbsec->flags & SE_SBINITIALIZED)) 1054e3489f89SAl Viro return 0; 1055e3489f89SAl Viro 105665cddd50SOndrej Mosnacek if (!selinux_initialized(&selinux_state)) 1057e3489f89SAl Viro return 0; 1058e3489f89SAl Viro 1059e3489f89SAl Viro if (sbsec->flags & FSCONTEXT_MNT) { 1060e3489f89SAl Viro seq_putc(m, ','); 1061e3489f89SAl Viro seq_puts(m, FSCONTEXT_STR); 1062e3489f89SAl Viro rc = show_sid(m, sbsec->sid); 1063e3489f89SAl Viro if (rc) 10642069f457SEric Paris return rc; 1065383795c2SEric Paris } 1066e3489f89SAl Viro if (sbsec->flags & CONTEXT_MNT) { 1067e3489f89SAl Viro seq_putc(m, ','); 1068e3489f89SAl Viro seq_puts(m, CONTEXT_STR); 1069e3489f89SAl Viro rc = show_sid(m, sbsec->mntpoint_sid); 1070e3489f89SAl Viro if (rc) 10712069f457SEric Paris return rc; 10722069f457SEric Paris } 1073e3489f89SAl Viro if (sbsec->flags & DEFCONTEXT_MNT) { 1074e3489f89SAl Viro seq_putc(m, ','); 1075e3489f89SAl Viro seq_puts(m, DEFCONTEXT_STR); 1076e3489f89SAl Viro rc = show_sid(m, sbsec->def_sid); 1077e3489f89SAl Viro if (rc) 1078e3489f89SAl Viro return rc; 1079e3489f89SAl Viro } 1080e3489f89SAl Viro if (sbsec->flags & ROOTCONTEXT_MNT) { 1081e3489f89SAl Viro struct dentry *root = sbsec->sb->s_root; 1082e3489f89SAl Viro struct inode_security_struct *isec = backing_inode_security(root); 1083e3489f89SAl Viro seq_putc(m, ','); 1084e3489f89SAl Viro seq_puts(m, ROOTCONTEXT_STR); 1085e3489f89SAl Viro rc = show_sid(m, isec->sid); 1086e3489f89SAl Viro if (rc) 1087e3489f89SAl Viro return rc; 1088e3489f89SAl Viro } 1089e3489f89SAl Viro if (sbsec->flags & SBLABEL_MNT) { 1090e3489f89SAl Viro seq_putc(m, ','); 1091442155c1SDavid Howells seq_puts(m, SECLABEL_STR); 1092e3489f89SAl Viro } 1093e3489f89SAl Viro return 0; 1094e3489f89SAl Viro } 10952069f457SEric Paris 10961da177e4SLinus Torvalds static inline u16 inode_mode_to_security_class(umode_t mode) 10971da177e4SLinus Torvalds { 10981da177e4SLinus Torvalds switch (mode & S_IFMT) { 10991da177e4SLinus Torvalds case S_IFSOCK: 11001da177e4SLinus Torvalds return SECCLASS_SOCK_FILE; 11011da177e4SLinus Torvalds case S_IFLNK: 11021da177e4SLinus Torvalds return SECCLASS_LNK_FILE; 11031da177e4SLinus Torvalds case S_IFREG: 11041da177e4SLinus Torvalds return SECCLASS_FILE; 11051da177e4SLinus Torvalds case S_IFBLK: 11061da177e4SLinus Torvalds return SECCLASS_BLK_FILE; 11071da177e4SLinus Torvalds case S_IFDIR: 11081da177e4SLinus Torvalds return SECCLASS_DIR; 11091da177e4SLinus Torvalds case S_IFCHR: 11101da177e4SLinus Torvalds return SECCLASS_CHR_FILE; 11111da177e4SLinus Torvalds case S_IFIFO: 11121da177e4SLinus Torvalds return SECCLASS_FIFO_FILE; 11131da177e4SLinus Torvalds 11141da177e4SLinus Torvalds } 11151da177e4SLinus Torvalds 11161da177e4SLinus Torvalds return SECCLASS_FILE; 11171da177e4SLinus Torvalds } 11181da177e4SLinus Torvalds 111913402580SJames Morris static inline int default_protocol_stream(int protocol) 112013402580SJames Morris { 112113402580SJames Morris return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP); 112213402580SJames Morris } 112313402580SJames Morris 112413402580SJames Morris static inline int default_protocol_dgram(int protocol) 112513402580SJames Morris { 112613402580SJames Morris return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP); 112713402580SJames Morris } 112813402580SJames Morris 11291da177e4SLinus Torvalds static inline u16 socket_type_to_security_class(int family, int type, int protocol) 11301da177e4SLinus Torvalds { 1131aa8e712cSStephen Smalley int extsockclass = selinux_policycap_extsockclass(); 1132da69a530SStephen Smalley 11331da177e4SLinus Torvalds switch (family) { 11341da177e4SLinus Torvalds case PF_UNIX: 11351da177e4SLinus Torvalds switch (type) { 11361da177e4SLinus Torvalds case SOCK_STREAM: 11371da177e4SLinus Torvalds case SOCK_SEQPACKET: 11381da177e4SLinus Torvalds return SECCLASS_UNIX_STREAM_SOCKET; 11391da177e4SLinus Torvalds case SOCK_DGRAM: 11402a764b52SLuis Ressel case SOCK_RAW: 11411da177e4SLinus Torvalds return SECCLASS_UNIX_DGRAM_SOCKET; 11421da177e4SLinus Torvalds } 11431da177e4SLinus Torvalds break; 11441da177e4SLinus Torvalds case PF_INET: 11451da177e4SLinus Torvalds case PF_INET6: 11461da177e4SLinus Torvalds switch (type) { 11471da177e4SLinus Torvalds case SOCK_STREAM: 1148da69a530SStephen Smalley case SOCK_SEQPACKET: 114913402580SJames Morris if (default_protocol_stream(protocol)) 11501da177e4SLinus Torvalds return SECCLASS_TCP_SOCKET; 1151da69a530SStephen Smalley else if (extsockclass && protocol == IPPROTO_SCTP) 1152da69a530SStephen Smalley return SECCLASS_SCTP_SOCKET; 115313402580SJames Morris else 115413402580SJames Morris return SECCLASS_RAWIP_SOCKET; 11551da177e4SLinus Torvalds case SOCK_DGRAM: 115613402580SJames Morris if (default_protocol_dgram(protocol)) 11571da177e4SLinus Torvalds return SECCLASS_UDP_SOCKET; 1158ef37979aSStephen Smalley else if (extsockclass && (protocol == IPPROTO_ICMP || 1159ef37979aSStephen Smalley protocol == IPPROTO_ICMPV6)) 1160da69a530SStephen Smalley return SECCLASS_ICMP_SOCKET; 116113402580SJames Morris else 116213402580SJames Morris return SECCLASS_RAWIP_SOCKET; 11632ee92d46SJames Morris case SOCK_DCCP: 11642ee92d46SJames Morris return SECCLASS_DCCP_SOCKET; 116513402580SJames Morris default: 11661da177e4SLinus Torvalds return SECCLASS_RAWIP_SOCKET; 11671da177e4SLinus Torvalds } 11681da177e4SLinus Torvalds break; 11691da177e4SLinus Torvalds case PF_NETLINK: 11701da177e4SLinus Torvalds switch (protocol) { 11711da177e4SLinus Torvalds case NETLINK_ROUTE: 11721da177e4SLinus Torvalds return SECCLASS_NETLINK_ROUTE_SOCKET; 11737f1fb60cSPavel Emelyanov case NETLINK_SOCK_DIAG: 11741da177e4SLinus Torvalds return SECCLASS_NETLINK_TCPDIAG_SOCKET; 11751da177e4SLinus Torvalds case NETLINK_NFLOG: 11761da177e4SLinus Torvalds return SECCLASS_NETLINK_NFLOG_SOCKET; 11771da177e4SLinus Torvalds case NETLINK_XFRM: 11781da177e4SLinus Torvalds return SECCLASS_NETLINK_XFRM_SOCKET; 11791da177e4SLinus Torvalds case NETLINK_SELINUX: 11801da177e4SLinus Torvalds return SECCLASS_NETLINK_SELINUX_SOCKET; 11816c6d2e9bSStephen Smalley case NETLINK_ISCSI: 11826c6d2e9bSStephen Smalley return SECCLASS_NETLINK_ISCSI_SOCKET; 11831da177e4SLinus Torvalds case NETLINK_AUDIT: 11841da177e4SLinus Torvalds return SECCLASS_NETLINK_AUDIT_SOCKET; 11856c6d2e9bSStephen Smalley case NETLINK_FIB_LOOKUP: 11866c6d2e9bSStephen Smalley return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET; 11876c6d2e9bSStephen Smalley case NETLINK_CONNECTOR: 11886c6d2e9bSStephen Smalley return SECCLASS_NETLINK_CONNECTOR_SOCKET; 11896c6d2e9bSStephen Smalley case NETLINK_NETFILTER: 11906c6d2e9bSStephen Smalley return SECCLASS_NETLINK_NETFILTER_SOCKET; 11911da177e4SLinus Torvalds case NETLINK_DNRTMSG: 11921da177e4SLinus Torvalds return SECCLASS_NETLINK_DNRT_SOCKET; 11930c9b7942SJames Morris case NETLINK_KOBJECT_UEVENT: 11940c9b7942SJames Morris return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET; 11956c6d2e9bSStephen Smalley case NETLINK_GENERIC: 11966c6d2e9bSStephen Smalley return SECCLASS_NETLINK_GENERIC_SOCKET; 11976c6d2e9bSStephen Smalley case NETLINK_SCSITRANSPORT: 11986c6d2e9bSStephen Smalley return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET; 11996c6d2e9bSStephen Smalley case NETLINK_RDMA: 12006c6d2e9bSStephen Smalley return SECCLASS_NETLINK_RDMA_SOCKET; 12016c6d2e9bSStephen Smalley case NETLINK_CRYPTO: 12026c6d2e9bSStephen Smalley return SECCLASS_NETLINK_CRYPTO_SOCKET; 12031da177e4SLinus Torvalds default: 12041da177e4SLinus Torvalds return SECCLASS_NETLINK_SOCKET; 12051da177e4SLinus Torvalds } 12061da177e4SLinus Torvalds case PF_PACKET: 12071da177e4SLinus Torvalds return SECCLASS_PACKET_SOCKET; 12081da177e4SLinus Torvalds case PF_KEY: 12091da177e4SLinus Torvalds return SECCLASS_KEY_SOCKET; 12103e3ff15eSChristopher J. PeBenito case PF_APPLETALK: 12113e3ff15eSChristopher J. PeBenito return SECCLASS_APPLETALK_SOCKET; 12121da177e4SLinus Torvalds } 12131da177e4SLinus Torvalds 1214da69a530SStephen Smalley if (extsockclass) { 1215da69a530SStephen Smalley switch (family) { 1216da69a530SStephen Smalley case PF_AX25: 1217da69a530SStephen Smalley return SECCLASS_AX25_SOCKET; 1218da69a530SStephen Smalley case PF_IPX: 1219da69a530SStephen Smalley return SECCLASS_IPX_SOCKET; 1220da69a530SStephen Smalley case PF_NETROM: 1221da69a530SStephen Smalley return SECCLASS_NETROM_SOCKET; 1222da69a530SStephen Smalley case PF_ATMPVC: 1223da69a530SStephen Smalley return SECCLASS_ATMPVC_SOCKET; 1224da69a530SStephen Smalley case PF_X25: 1225da69a530SStephen Smalley return SECCLASS_X25_SOCKET; 1226da69a530SStephen Smalley case PF_ROSE: 1227da69a530SStephen Smalley return SECCLASS_ROSE_SOCKET; 1228da69a530SStephen Smalley case PF_DECnet: 1229da69a530SStephen Smalley return SECCLASS_DECNET_SOCKET; 1230da69a530SStephen Smalley case PF_ATMSVC: 1231da69a530SStephen Smalley return SECCLASS_ATMSVC_SOCKET; 1232da69a530SStephen Smalley case PF_RDS: 1233da69a530SStephen Smalley return SECCLASS_RDS_SOCKET; 1234da69a530SStephen Smalley case PF_IRDA: 1235da69a530SStephen Smalley return SECCLASS_IRDA_SOCKET; 1236da69a530SStephen Smalley case PF_PPPOX: 1237da69a530SStephen Smalley return SECCLASS_PPPOX_SOCKET; 1238da69a530SStephen Smalley case PF_LLC: 1239da69a530SStephen Smalley return SECCLASS_LLC_SOCKET; 1240da69a530SStephen Smalley case PF_CAN: 1241da69a530SStephen Smalley return SECCLASS_CAN_SOCKET; 1242da69a530SStephen Smalley case PF_TIPC: 1243da69a530SStephen Smalley return SECCLASS_TIPC_SOCKET; 1244da69a530SStephen Smalley case PF_BLUETOOTH: 1245da69a530SStephen Smalley return SECCLASS_BLUETOOTH_SOCKET; 1246da69a530SStephen Smalley case PF_IUCV: 1247da69a530SStephen Smalley return SECCLASS_IUCV_SOCKET; 1248da69a530SStephen Smalley case PF_RXRPC: 1249da69a530SStephen Smalley return SECCLASS_RXRPC_SOCKET; 1250da69a530SStephen Smalley case PF_ISDN: 1251da69a530SStephen Smalley return SECCLASS_ISDN_SOCKET; 1252da69a530SStephen Smalley case PF_PHONET: 1253da69a530SStephen Smalley return SECCLASS_PHONET_SOCKET; 1254da69a530SStephen Smalley case PF_IEEE802154: 1255da69a530SStephen Smalley return SECCLASS_IEEE802154_SOCKET; 1256da69a530SStephen Smalley case PF_CAIF: 1257da69a530SStephen Smalley return SECCLASS_CAIF_SOCKET; 1258da69a530SStephen Smalley case PF_ALG: 1259da69a530SStephen Smalley return SECCLASS_ALG_SOCKET; 1260da69a530SStephen Smalley case PF_NFC: 1261da69a530SStephen Smalley return SECCLASS_NFC_SOCKET; 1262da69a530SStephen Smalley case PF_VSOCK: 1263da69a530SStephen Smalley return SECCLASS_VSOCK_SOCKET; 1264da69a530SStephen Smalley case PF_KCM: 1265da69a530SStephen Smalley return SECCLASS_KCM_SOCKET; 1266da69a530SStephen Smalley case PF_QIPCRTR: 1267da69a530SStephen Smalley return SECCLASS_QIPCRTR_SOCKET; 12683051bf36SLinus Torvalds case PF_SMC: 12693051bf36SLinus Torvalds return SECCLASS_SMC_SOCKET; 127068e8b849SBjörn Töpel case PF_XDP: 127168e8b849SBjörn Töpel return SECCLASS_XDP_SOCKET; 127268e8b849SBjörn Töpel #if PF_MAX > 45 1273da69a530SStephen Smalley #error New address family defined, please update this function. 1274da69a530SStephen Smalley #endif 1275da69a530SStephen Smalley } 1276da69a530SStephen Smalley } 1277da69a530SStephen Smalley 12781da177e4SLinus Torvalds return SECCLASS_SOCKET; 12791da177e4SLinus Torvalds } 12801da177e4SLinus Torvalds 1281134509d5SStephen Smalley static int selinux_genfs_get_sid(struct dentry *dentry, 12821da177e4SLinus Torvalds u16 tclass, 1283134509d5SStephen Smalley u16 flags, 12841da177e4SLinus Torvalds u32 *sid) 12851da177e4SLinus Torvalds { 12868e6c9693SLucian Adrian Grijincu int rc; 1287fc64005cSAl Viro struct super_block *sb = dentry->d_sb; 12888e6c9693SLucian Adrian Grijincu char *buffer, *path; 12891da177e4SLinus Torvalds 12901da177e4SLinus Torvalds buffer = (char *)__get_free_page(GFP_KERNEL); 12911da177e4SLinus Torvalds if (!buffer) 12921da177e4SLinus Torvalds return -ENOMEM; 12931da177e4SLinus Torvalds 12948e6c9693SLucian Adrian Grijincu path = dentry_path_raw(dentry, buffer, PAGE_SIZE); 12958e6c9693SLucian Adrian Grijincu if (IS_ERR(path)) 12968e6c9693SLucian Adrian Grijincu rc = PTR_ERR(path); 12978e6c9693SLucian Adrian Grijincu else { 1298134509d5SStephen Smalley if (flags & SE_SBPROC) { 12998e6c9693SLucian Adrian Grijincu /* each process gets a /proc/PID/ entry. Strip off the 13008e6c9693SLucian Adrian Grijincu * PID part to get a valid selinux labeling. 13018e6c9693SLucian Adrian Grijincu * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */ 13028e6c9693SLucian Adrian Grijincu while (path[1] >= '0' && path[1] <= '9') { 13038e6c9693SLucian Adrian Grijincu path[1] = '/'; 13048e6c9693SLucian Adrian Grijincu path++; 13051da177e4SLinus Torvalds } 1306134509d5SStephen Smalley } 1307aa8e712cSStephen Smalley rc = security_genfs_sid(&selinux_state, sb->s_type->name, 1308aa8e712cSStephen Smalley path, tclass, sid); 13097bb185edSStephen Smalley if (rc == -ENOENT) { 13107bb185edSStephen Smalley /* No match in policy, mark as unlabeled. */ 13117bb185edSStephen Smalley *sid = SECINITSID_UNLABELED; 13127bb185edSStephen Smalley rc = 0; 13137bb185edSStephen Smalley } 13148e6c9693SLucian Adrian Grijincu } 13151da177e4SLinus Torvalds free_page((unsigned long)buffer); 13161da177e4SLinus Torvalds return rc; 13171da177e4SLinus Torvalds } 13181da177e4SLinus Torvalds 1319b754026bSOndrej Mosnacek static int inode_doinit_use_xattr(struct inode *inode, struct dentry *dentry, 1320b754026bSOndrej Mosnacek u32 def_sid, u32 *sid) 1321b754026bSOndrej Mosnacek { 1322b754026bSOndrej Mosnacek #define INITCONTEXTLEN 255 1323b754026bSOndrej Mosnacek char *context; 1324b754026bSOndrej Mosnacek unsigned int len; 1325b754026bSOndrej Mosnacek int rc; 1326b754026bSOndrej Mosnacek 1327b754026bSOndrej Mosnacek len = INITCONTEXTLEN; 1328b754026bSOndrej Mosnacek context = kmalloc(len + 1, GFP_NOFS); 1329b754026bSOndrej Mosnacek if (!context) 1330b754026bSOndrej Mosnacek return -ENOMEM; 1331b754026bSOndrej Mosnacek 1332b754026bSOndrej Mosnacek context[len] = '\0'; 1333b754026bSOndrej Mosnacek rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len); 1334b754026bSOndrej Mosnacek if (rc == -ERANGE) { 1335b754026bSOndrej Mosnacek kfree(context); 1336b754026bSOndrej Mosnacek 1337b754026bSOndrej Mosnacek /* Need a larger buffer. Query for the right size. */ 1338b754026bSOndrej Mosnacek rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0); 1339b754026bSOndrej Mosnacek if (rc < 0) 1340b754026bSOndrej Mosnacek return rc; 1341b754026bSOndrej Mosnacek 1342b754026bSOndrej Mosnacek len = rc; 1343b754026bSOndrej Mosnacek context = kmalloc(len + 1, GFP_NOFS); 1344b754026bSOndrej Mosnacek if (!context) 1345b754026bSOndrej Mosnacek return -ENOMEM; 1346b754026bSOndrej Mosnacek 1347b754026bSOndrej Mosnacek context[len] = '\0'; 1348b754026bSOndrej Mosnacek rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, 1349b754026bSOndrej Mosnacek context, len); 1350b754026bSOndrej Mosnacek } 1351b754026bSOndrej Mosnacek if (rc < 0) { 1352b754026bSOndrej Mosnacek kfree(context); 1353b754026bSOndrej Mosnacek if (rc != -ENODATA) { 1354b754026bSOndrej Mosnacek pr_warn("SELinux: %s: getxattr returned %d for dev=%s ino=%ld\n", 1355b754026bSOndrej Mosnacek __func__, -rc, inode->i_sb->s_id, inode->i_ino); 1356b754026bSOndrej Mosnacek return rc; 1357b754026bSOndrej Mosnacek } 1358b754026bSOndrej Mosnacek *sid = def_sid; 1359b754026bSOndrej Mosnacek return 0; 1360b754026bSOndrej Mosnacek } 1361b754026bSOndrej Mosnacek 1362b754026bSOndrej Mosnacek rc = security_context_to_sid_default(&selinux_state, context, rc, sid, 1363b754026bSOndrej Mosnacek def_sid, GFP_NOFS); 1364b754026bSOndrej Mosnacek if (rc) { 1365b754026bSOndrej Mosnacek char *dev = inode->i_sb->s_id; 1366b754026bSOndrej Mosnacek unsigned long ino = inode->i_ino; 1367b754026bSOndrej Mosnacek 1368b754026bSOndrej Mosnacek if (rc == -EINVAL) { 1369b754026bSOndrej Mosnacek pr_notice_ratelimited("SELinux: inode=%lu on dev=%s was found to have an invalid context=%s. This indicates you may need to relabel the inode or the filesystem in question.\n", 1370b754026bSOndrej Mosnacek ino, dev, context); 1371b754026bSOndrej Mosnacek } else { 1372b754026bSOndrej Mosnacek pr_warn("SELinux: %s: context_to_sid(%s) returned %d for dev=%s ino=%ld\n", 1373b754026bSOndrej Mosnacek __func__, context, -rc, dev, ino); 1374b754026bSOndrej Mosnacek } 1375b754026bSOndrej Mosnacek } 1376b754026bSOndrej Mosnacek kfree(context); 1377b754026bSOndrej Mosnacek return 0; 1378b754026bSOndrej Mosnacek } 1379b754026bSOndrej Mosnacek 13801da177e4SLinus Torvalds /* The inode's security attributes must be initialized before first use. */ 13811da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry) 13821da177e4SLinus Torvalds { 13831da177e4SLinus Torvalds struct superblock_security_struct *sbsec = NULL; 138480788c22SCasey Schaufler struct inode_security_struct *isec = selinux_inode(inode); 13859287aed2SAndreas Gruenbacher u32 task_sid, sid = 0; 13869287aed2SAndreas Gruenbacher u16 sclass; 13871da177e4SLinus Torvalds struct dentry *dentry; 13881da177e4SLinus Torvalds int rc = 0; 13891da177e4SLinus Torvalds 13906f3be9f5SAndreas Gruenbacher if (isec->initialized == LABEL_INITIALIZED) 139113457d07SAndreas Gruenbacher return 0; 13921da177e4SLinus Torvalds 13939287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 13946f3be9f5SAndreas Gruenbacher if (isec->initialized == LABEL_INITIALIZED) 139523970741SEric Paris goto out_unlock; 13961da177e4SLinus Torvalds 139713457d07SAndreas Gruenbacher if (isec->sclass == SECCLASS_FILE) 139813457d07SAndreas Gruenbacher isec->sclass = inode_mode_to_security_class(inode->i_mode); 139913457d07SAndreas Gruenbacher 14001da177e4SLinus Torvalds sbsec = inode->i_sb->s_security; 14010d90a7ecSDavid P. Quigley if (!(sbsec->flags & SE_SBINITIALIZED)) { 14021da177e4SLinus Torvalds /* Defer initialization until selinux_complete_init, 14031da177e4SLinus Torvalds after the initial policy is loaded and the security 14041da177e4SLinus Torvalds server is ready to handle calls. */ 14051da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 14061da177e4SLinus Torvalds if (list_empty(&isec->list)) 14071da177e4SLinus Torvalds list_add(&isec->list, &sbsec->isec_head); 14081da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 140923970741SEric Paris goto out_unlock; 14101da177e4SLinus Torvalds } 14111da177e4SLinus Torvalds 14129287aed2SAndreas Gruenbacher sclass = isec->sclass; 14139287aed2SAndreas Gruenbacher task_sid = isec->task_sid; 14149287aed2SAndreas Gruenbacher sid = isec->sid; 14159287aed2SAndreas Gruenbacher isec->initialized = LABEL_PENDING; 14169287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 14179287aed2SAndreas Gruenbacher 14181da177e4SLinus Torvalds switch (sbsec->behavior) { 1419eb9ae686SDavid Quigley case SECURITY_FS_USE_NATIVE: 1420eb9ae686SDavid Quigley break; 14211da177e4SLinus Torvalds case SECURITY_FS_USE_XATTR: 14225d6c3191SAndreas Gruenbacher if (!(inode->i_opflags & IOP_XATTR)) { 14239287aed2SAndreas Gruenbacher sid = sbsec->def_sid; 14241da177e4SLinus Torvalds break; 14251da177e4SLinus Torvalds } 14261da177e4SLinus Torvalds /* Need a dentry, since the xattr API requires one. 14271da177e4SLinus Torvalds Life would be simpler if we could just pass the inode. */ 14281da177e4SLinus Torvalds if (opt_dentry) { 14291da177e4SLinus Torvalds /* Called from d_instantiate or d_splice_alias. */ 14301da177e4SLinus Torvalds dentry = dget(opt_dentry); 14311da177e4SLinus Torvalds } else { 1432b127125dSAl Viro /* 1433b127125dSAl Viro * Called from selinux_complete_init, try to find a dentry. 1434b127125dSAl Viro * Some filesystems really want a connected one, so try 1435b127125dSAl Viro * that first. We could split SECURITY_FS_USE_XATTR in 1436b127125dSAl Viro * two, depending upon that... 1437b127125dSAl Viro */ 14381da177e4SLinus Torvalds dentry = d_find_alias(inode); 1439b127125dSAl Viro if (!dentry) 1440b127125dSAl Viro dentry = d_find_any_alias(inode); 14411da177e4SLinus Torvalds } 14421da177e4SLinus Torvalds if (!dentry) { 1443df7f54c0SEric Paris /* 1444df7f54c0SEric Paris * this is can be hit on boot when a file is accessed 1445df7f54c0SEric Paris * before the policy is loaded. When we load policy we 1446df7f54c0SEric Paris * may find inodes that have no dentry on the 1447df7f54c0SEric Paris * sbsec->isec_head list. No reason to complain as these 1448df7f54c0SEric Paris * will get fixed up the next time we go through 1449df7f54c0SEric Paris * inode_doinit with a dentry, before these inodes could 1450df7f54c0SEric Paris * be used again by userspace. 1451df7f54c0SEric Paris */ 14529287aed2SAndreas Gruenbacher goto out; 14531da177e4SLinus Torvalds } 14541da177e4SLinus Torvalds 1455b754026bSOndrej Mosnacek rc = inode_doinit_use_xattr(inode, dentry, sbsec->def_sid, 1456b754026bSOndrej Mosnacek &sid); 14571da177e4SLinus Torvalds dput(dentry); 1458b754026bSOndrej Mosnacek if (rc) 14599287aed2SAndreas Gruenbacher goto out; 14601da177e4SLinus Torvalds break; 14611da177e4SLinus Torvalds case SECURITY_FS_USE_TASK: 14629287aed2SAndreas Gruenbacher sid = task_sid; 14631da177e4SLinus Torvalds break; 14641da177e4SLinus Torvalds case SECURITY_FS_USE_TRANS: 14651da177e4SLinus Torvalds /* Default to the fs SID. */ 14669287aed2SAndreas Gruenbacher sid = sbsec->sid; 14671da177e4SLinus Torvalds 14681da177e4SLinus Torvalds /* Try to obtain a transition SID. */ 1469aa8e712cSStephen Smalley rc = security_transition_sid(&selinux_state, task_sid, sid, 1470aa8e712cSStephen Smalley sclass, NULL, &sid); 14711da177e4SLinus Torvalds if (rc) 14729287aed2SAndreas Gruenbacher goto out; 14731da177e4SLinus Torvalds break; 1474c312feb2SEric Paris case SECURITY_FS_USE_MNTPOINT: 14759287aed2SAndreas Gruenbacher sid = sbsec->mntpoint_sid; 1476c312feb2SEric Paris break; 14771da177e4SLinus Torvalds default: 1478c312feb2SEric Paris /* Default to the fs superblock SID. */ 14799287aed2SAndreas Gruenbacher sid = sbsec->sid; 14801da177e4SLinus Torvalds 1481*7470d0d1SChristian Göttsche if ((sbsec->flags & SE_SBGENFS) && 1482*7470d0d1SChristian Göttsche (!S_ISLNK(inode->i_mode) || 1483*7470d0d1SChristian Göttsche selinux_policycap_genfs_seclabel_symlinks())) { 1484f64410ecSPaul Moore /* We must have a dentry to determine the label on 1485f64410ecSPaul Moore * procfs inodes */ 1486b127125dSAl Viro if (opt_dentry) { 1487f64410ecSPaul Moore /* Called from d_instantiate or 1488f64410ecSPaul Moore * d_splice_alias. */ 1489f64410ecSPaul Moore dentry = dget(opt_dentry); 1490b127125dSAl Viro } else { 1491f64410ecSPaul Moore /* Called from selinux_complete_init, try to 1492b127125dSAl Viro * find a dentry. Some filesystems really want 1493b127125dSAl Viro * a connected one, so try that first. 1494b127125dSAl Viro */ 1495f64410ecSPaul Moore dentry = d_find_alias(inode); 1496b127125dSAl Viro if (!dentry) 1497b127125dSAl Viro dentry = d_find_any_alias(inode); 1498b127125dSAl Viro } 1499f64410ecSPaul Moore /* 1500f64410ecSPaul Moore * This can be hit on boot when a file is accessed 1501f64410ecSPaul Moore * before the policy is loaded. When we load policy we 1502f64410ecSPaul Moore * may find inodes that have no dentry on the 1503f64410ecSPaul Moore * sbsec->isec_head list. No reason to complain as 1504f64410ecSPaul Moore * these will get fixed up the next time we go through 1505f64410ecSPaul Moore * inode_doinit() with a dentry, before these inodes 1506f64410ecSPaul Moore * could be used again by userspace. 1507f64410ecSPaul Moore */ 1508f64410ecSPaul Moore if (!dentry) 15099287aed2SAndreas Gruenbacher goto out; 15109287aed2SAndreas Gruenbacher rc = selinux_genfs_get_sid(dentry, sclass, 1511134509d5SStephen Smalley sbsec->flags, &sid); 1512b754026bSOndrej Mosnacek if (rc) { 1513f64410ecSPaul Moore dput(dentry); 15149287aed2SAndreas Gruenbacher goto out; 15151da177e4SLinus Torvalds } 1516b754026bSOndrej Mosnacek 1517b754026bSOndrej Mosnacek if ((sbsec->flags & SE_SBGENFS_XATTR) && 1518b754026bSOndrej Mosnacek (inode->i_opflags & IOP_XATTR)) { 1519b754026bSOndrej Mosnacek rc = inode_doinit_use_xattr(inode, dentry, 1520b754026bSOndrej Mosnacek sid, &sid); 1521b754026bSOndrej Mosnacek if (rc) { 1522b754026bSOndrej Mosnacek dput(dentry); 1523b754026bSOndrej Mosnacek goto out; 1524b754026bSOndrej Mosnacek } 1525b754026bSOndrej Mosnacek } 1526b754026bSOndrej Mosnacek dput(dentry); 1527b754026bSOndrej Mosnacek } 15281da177e4SLinus Torvalds break; 15291da177e4SLinus Torvalds } 15301da177e4SLinus Torvalds 15319287aed2SAndreas Gruenbacher out: 15329287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 15339287aed2SAndreas Gruenbacher if (isec->initialized == LABEL_PENDING) { 15349287aed2SAndreas Gruenbacher if (!sid || rc) { 15359287aed2SAndreas Gruenbacher isec->initialized = LABEL_INVALID; 15369287aed2SAndreas Gruenbacher goto out_unlock; 15379287aed2SAndreas Gruenbacher } 15389287aed2SAndreas Gruenbacher 15396f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 15409287aed2SAndreas Gruenbacher isec->sid = sid; 15419287aed2SAndreas Gruenbacher } 15421da177e4SLinus Torvalds 154323970741SEric Paris out_unlock: 15449287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 15451da177e4SLinus Torvalds return rc; 15461da177e4SLinus Torvalds } 15471da177e4SLinus Torvalds 15481da177e4SLinus Torvalds /* Convert a Linux signal to an access vector. */ 15491da177e4SLinus Torvalds static inline u32 signal_to_av(int sig) 15501da177e4SLinus Torvalds { 15511da177e4SLinus Torvalds u32 perm = 0; 15521da177e4SLinus Torvalds 15531da177e4SLinus Torvalds switch (sig) { 15541da177e4SLinus Torvalds case SIGCHLD: 15551da177e4SLinus Torvalds /* Commonly granted from child to parent. */ 15561da177e4SLinus Torvalds perm = PROCESS__SIGCHLD; 15571da177e4SLinus Torvalds break; 15581da177e4SLinus Torvalds case SIGKILL: 15591da177e4SLinus Torvalds /* Cannot be caught or ignored */ 15601da177e4SLinus Torvalds perm = PROCESS__SIGKILL; 15611da177e4SLinus Torvalds break; 15621da177e4SLinus Torvalds case SIGSTOP: 15631da177e4SLinus Torvalds /* Cannot be caught or ignored */ 15641da177e4SLinus Torvalds perm = PROCESS__SIGSTOP; 15651da177e4SLinus Torvalds break; 15661da177e4SLinus Torvalds default: 15671da177e4SLinus Torvalds /* All other signals. */ 15681da177e4SLinus Torvalds perm = PROCESS__SIGNAL; 15691da177e4SLinus Torvalds break; 15701da177e4SLinus Torvalds } 15711da177e4SLinus Torvalds 15721da177e4SLinus Torvalds return perm; 15731da177e4SLinus Torvalds } 15741da177e4SLinus Torvalds 1575b68e418cSStephen Smalley #if CAP_LAST_CAP > 63 1576b68e418cSStephen Smalley #error Fix SELinux to handle capabilities > 63. 1577b68e418cSStephen Smalley #endif 1578b68e418cSStephen Smalley 15791da177e4SLinus Torvalds /* Check whether a task is allowed to use a capability. */ 15806a9de491SEric Paris static int cred_has_capability(const struct cred *cred, 1581c1a85a00SMicah Morton int cap, unsigned int opts, bool initns) 15821da177e4SLinus Torvalds { 15832bf49690SThomas Liu struct common_audit_data ad; 158406112163SEric Paris struct av_decision avd; 1585b68e418cSStephen Smalley u16 sclass; 15863699c53cSDavid Howells u32 sid = cred_sid(cred); 1587b68e418cSStephen Smalley u32 av = CAP_TO_MASK(cap); 158806112163SEric Paris int rc; 15891da177e4SLinus Torvalds 159050c205f5SEric Paris ad.type = LSM_AUDIT_DATA_CAP; 15911da177e4SLinus Torvalds ad.u.cap = cap; 15921da177e4SLinus Torvalds 1593b68e418cSStephen Smalley switch (CAP_TO_INDEX(cap)) { 1594b68e418cSStephen Smalley case 0: 15958e4ff6f2SStephen Smalley sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS; 1596b68e418cSStephen Smalley break; 1597b68e418cSStephen Smalley case 1: 15988e4ff6f2SStephen Smalley sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS; 1599b68e418cSStephen Smalley break; 1600b68e418cSStephen Smalley default: 1601c103a91eSpeter enderborg pr_err("SELinux: out of range capability %d\n", cap); 1602b68e418cSStephen Smalley BUG(); 1603a35c6c83SEric Paris return -EINVAL; 1604b68e418cSStephen Smalley } 160506112163SEric Paris 16066b6bc620SStephen Smalley rc = avc_has_perm_noaudit(&selinux_state, 16076b6bc620SStephen Smalley sid, sid, sclass, av, 0, &avd); 1608c1a85a00SMicah Morton if (!(opts & CAP_OPT_NOAUDIT)) { 16096b6bc620SStephen Smalley int rc2 = avc_audit(&selinux_state, 16106b6bc620SStephen Smalley sid, sid, sclass, av, &avd, rc, &ad, 0); 16119ade0cf4SEric Paris if (rc2) 16129ade0cf4SEric Paris return rc2; 16139ade0cf4SEric Paris } 161406112163SEric Paris return rc; 16151da177e4SLinus Torvalds } 16161da177e4SLinus Torvalds 16171da177e4SLinus Torvalds /* Check whether a task has a particular permission to an inode. 16181da177e4SLinus Torvalds The 'adp' parameter is optional and allows other audit 16191da177e4SLinus Torvalds data to be passed (e.g. the dentry). */ 162088e67f3bSDavid Howells static int inode_has_perm(const struct cred *cred, 16211da177e4SLinus Torvalds struct inode *inode, 16221da177e4SLinus Torvalds u32 perms, 162319e49834SLinus Torvalds struct common_audit_data *adp) 16241da177e4SLinus Torvalds { 16251da177e4SLinus Torvalds struct inode_security_struct *isec; 1626275bb41eSDavid Howells u32 sid; 16271da177e4SLinus Torvalds 1628e0e81739SDavid Howells validate_creds(cred); 1629e0e81739SDavid Howells 1630bbaca6c2SStephen Smalley if (unlikely(IS_PRIVATE(inode))) 1631bbaca6c2SStephen Smalley return 0; 1632bbaca6c2SStephen Smalley 163388e67f3bSDavid Howells sid = cred_sid(cred); 163480788c22SCasey Schaufler isec = selinux_inode(inode); 16351da177e4SLinus Torvalds 16366b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 16376b6bc620SStephen Smalley sid, isec->sid, isec->sclass, perms, adp); 16381da177e4SLinus Torvalds } 16391da177e4SLinus Torvalds 16401da177e4SLinus Torvalds /* Same as inode_has_perm, but pass explicit audit data containing 16411da177e4SLinus Torvalds the dentry to help the auditing code to more easily generate the 16421da177e4SLinus Torvalds pathname if needed. */ 164388e67f3bSDavid Howells static inline int dentry_has_perm(const struct cred *cred, 16441da177e4SLinus Torvalds struct dentry *dentry, 16451da177e4SLinus Torvalds u32 av) 16461da177e4SLinus Torvalds { 1647c6f493d6SDavid Howells struct inode *inode = d_backing_inode(dentry); 16482bf49690SThomas Liu struct common_audit_data ad; 164988e67f3bSDavid Howells 165050c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 16512875fa00SEric Paris ad.u.dentry = dentry; 16525d226df4SAndreas Gruenbacher __inode_security_revalidate(inode, dentry, true); 165319e49834SLinus Torvalds return inode_has_perm(cred, inode, av, &ad); 16542875fa00SEric Paris } 16552875fa00SEric Paris 16562875fa00SEric Paris /* Same as inode_has_perm, but pass explicit audit data containing 16572875fa00SEric Paris the path to help the auditing code to more easily generate the 16582875fa00SEric Paris pathname if needed. */ 16592875fa00SEric Paris static inline int path_has_perm(const struct cred *cred, 16603f7036a0SAl Viro const struct path *path, 16612875fa00SEric Paris u32 av) 16622875fa00SEric Paris { 1663c6f493d6SDavid Howells struct inode *inode = d_backing_inode(path->dentry); 16642875fa00SEric Paris struct common_audit_data ad; 16652875fa00SEric Paris 166650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_PATH; 16672875fa00SEric Paris ad.u.path = *path; 16685d226df4SAndreas Gruenbacher __inode_security_revalidate(inode, path->dentry, true); 166919e49834SLinus Torvalds return inode_has_perm(cred, inode, av, &ad); 16701da177e4SLinus Torvalds } 16711da177e4SLinus Torvalds 167213f8e981SDavid Howells /* Same as path_has_perm, but uses the inode from the file struct. */ 167313f8e981SDavid Howells static inline int file_path_has_perm(const struct cred *cred, 167413f8e981SDavid Howells struct file *file, 167513f8e981SDavid Howells u32 av) 167613f8e981SDavid Howells { 167713f8e981SDavid Howells struct common_audit_data ad; 167813f8e981SDavid Howells 167943af5de7SVivek Goyal ad.type = LSM_AUDIT_DATA_FILE; 168043af5de7SVivek Goyal ad.u.file = file; 168119e49834SLinus Torvalds return inode_has_perm(cred, file_inode(file), av, &ad); 168213f8e981SDavid Howells } 168313f8e981SDavid Howells 1684f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL 1685f66e448cSChenbo Feng static int bpf_fd_pass(struct file *file, u32 sid); 1686f66e448cSChenbo Feng #endif 1687f66e448cSChenbo Feng 16881da177e4SLinus Torvalds /* Check whether a task can use an open file descriptor to 16891da177e4SLinus Torvalds access an inode in a given way. Check access to the 16901da177e4SLinus Torvalds descriptor itself, and then use dentry_has_perm to 16911da177e4SLinus Torvalds check a particular permission to the file. 16921da177e4SLinus Torvalds Access to the descriptor is implicitly granted if it 16931da177e4SLinus Torvalds has the same SID as the process. If av is zero, then 16941da177e4SLinus Torvalds access to the file is not checked, e.g. for cases 16951da177e4SLinus Torvalds where only the descriptor is affected like seek. */ 169688e67f3bSDavid Howells static int file_has_perm(const struct cred *cred, 16971da177e4SLinus Torvalds struct file *file, 16981da177e4SLinus Torvalds u32 av) 16991da177e4SLinus Torvalds { 1700bb6c6b02SCasey Schaufler struct file_security_struct *fsec = selinux_file(file); 1701496ad9aaSAl Viro struct inode *inode = file_inode(file); 17022bf49690SThomas Liu struct common_audit_data ad; 170388e67f3bSDavid Howells u32 sid = cred_sid(cred); 17041da177e4SLinus Torvalds int rc; 17051da177e4SLinus Torvalds 170643af5de7SVivek Goyal ad.type = LSM_AUDIT_DATA_FILE; 170743af5de7SVivek Goyal ad.u.file = file; 17081da177e4SLinus Torvalds 1709275bb41eSDavid Howells if (sid != fsec->sid) { 17106b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 17116b6bc620SStephen Smalley sid, fsec->sid, 17121da177e4SLinus Torvalds SECCLASS_FD, 17131da177e4SLinus Torvalds FD__USE, 17141da177e4SLinus Torvalds &ad); 17151da177e4SLinus Torvalds if (rc) 171688e67f3bSDavid Howells goto out; 17171da177e4SLinus Torvalds } 17181da177e4SLinus Torvalds 1719f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL 1720f66e448cSChenbo Feng rc = bpf_fd_pass(file, cred_sid(cred)); 1721f66e448cSChenbo Feng if (rc) 1722f66e448cSChenbo Feng return rc; 1723f66e448cSChenbo Feng #endif 1724f66e448cSChenbo Feng 17251da177e4SLinus Torvalds /* av is zero if only checking access to the descriptor. */ 172688e67f3bSDavid Howells rc = 0; 17271da177e4SLinus Torvalds if (av) 172819e49834SLinus Torvalds rc = inode_has_perm(cred, inode, av, &ad); 17291da177e4SLinus Torvalds 173088e67f3bSDavid Howells out: 173188e67f3bSDavid Howells return rc; 17321da177e4SLinus Torvalds } 17331da177e4SLinus Torvalds 1734c3c188b2SDavid Howells /* 1735c3c188b2SDavid Howells * Determine the label for an inode that might be unioned. 1736c3c188b2SDavid Howells */ 1737c957f6dfSVivek Goyal static int 1738c957f6dfSVivek Goyal selinux_determine_inode_label(const struct task_security_struct *tsec, 1739c957f6dfSVivek Goyal struct inode *dir, 1740c957f6dfSVivek Goyal const struct qstr *name, u16 tclass, 1741c3c188b2SDavid Howells u32 *_new_isid) 1742c3c188b2SDavid Howells { 1743c3c188b2SDavid Howells const struct superblock_security_struct *sbsec = dir->i_sb->s_security; 1744c3c188b2SDavid Howells 1745c3c188b2SDavid Howells if ((sbsec->flags & SE_SBINITIALIZED) && 1746c3c188b2SDavid Howells (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) { 1747c3c188b2SDavid Howells *_new_isid = sbsec->mntpoint_sid; 1748c3c188b2SDavid Howells } else if ((sbsec->flags & SBLABEL_MNT) && 1749c3c188b2SDavid Howells tsec->create_sid) { 1750c3c188b2SDavid Howells *_new_isid = tsec->create_sid; 1751c3c188b2SDavid Howells } else { 175220cdef8dSPaul Moore const struct inode_security_struct *dsec = inode_security(dir); 1753aa8e712cSStephen Smalley return security_transition_sid(&selinux_state, tsec->sid, 1754aa8e712cSStephen Smalley dsec->sid, tclass, 1755c3c188b2SDavid Howells name, _new_isid); 1756c3c188b2SDavid Howells } 1757c3c188b2SDavid Howells 1758c3c188b2SDavid Howells return 0; 1759c3c188b2SDavid Howells } 1760c3c188b2SDavid Howells 17611da177e4SLinus Torvalds /* Check whether a task can create a file. */ 17621da177e4SLinus Torvalds static int may_create(struct inode *dir, 17631da177e4SLinus Torvalds struct dentry *dentry, 17641da177e4SLinus Torvalds u16 tclass) 17651da177e4SLinus Torvalds { 17660c6cfa62SCasey Schaufler const struct task_security_struct *tsec = selinux_cred(current_cred()); 17671da177e4SLinus Torvalds struct inode_security_struct *dsec; 17681da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 1769275bb41eSDavid Howells u32 sid, newsid; 17702bf49690SThomas Liu struct common_audit_data ad; 17711da177e4SLinus Torvalds int rc; 17721da177e4SLinus Torvalds 177383da53c5SAndreas Gruenbacher dsec = inode_security(dir); 17741da177e4SLinus Torvalds sbsec = dir->i_sb->s_security; 17751da177e4SLinus Torvalds 1776275bb41eSDavid Howells sid = tsec->sid; 1777275bb41eSDavid Howells 177850c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 1779a269434dSEric Paris ad.u.dentry = dentry; 17801da177e4SLinus Torvalds 17816b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 17826b6bc620SStephen Smalley sid, dsec->sid, SECCLASS_DIR, 17831da177e4SLinus Torvalds DIR__ADD_NAME | DIR__SEARCH, 17841da177e4SLinus Torvalds &ad); 17851da177e4SLinus Torvalds if (rc) 17861da177e4SLinus Torvalds return rc; 17871da177e4SLinus Torvalds 1788210a2928SYang Guo rc = selinux_determine_inode_label(tsec, dir, &dentry->d_name, tclass, 1789210a2928SYang Guo &newsid); 17901da177e4SLinus Torvalds if (rc) 17911da177e4SLinus Torvalds return rc; 17921da177e4SLinus Torvalds 17936b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 17946b6bc620SStephen Smalley sid, newsid, tclass, FILE__CREATE, &ad); 17951da177e4SLinus Torvalds if (rc) 17961da177e4SLinus Torvalds return rc; 17971da177e4SLinus Torvalds 17986b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 17996b6bc620SStephen Smalley newsid, sbsec->sid, 18001da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 18011da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, &ad); 18021da177e4SLinus Torvalds } 18031da177e4SLinus Torvalds 18041da177e4SLinus Torvalds #define MAY_LINK 0 18051da177e4SLinus Torvalds #define MAY_UNLINK 1 18061da177e4SLinus Torvalds #define MAY_RMDIR 2 18071da177e4SLinus Torvalds 18081da177e4SLinus Torvalds /* Check whether a task can link, unlink, or rmdir a file/directory. */ 18091da177e4SLinus Torvalds static int may_link(struct inode *dir, 18101da177e4SLinus Torvalds struct dentry *dentry, 18111da177e4SLinus Torvalds int kind) 18121da177e4SLinus Torvalds 18131da177e4SLinus Torvalds { 18141da177e4SLinus Torvalds struct inode_security_struct *dsec, *isec; 18152bf49690SThomas Liu struct common_audit_data ad; 1816275bb41eSDavid Howells u32 sid = current_sid(); 18171da177e4SLinus Torvalds u32 av; 18181da177e4SLinus Torvalds int rc; 18191da177e4SLinus Torvalds 182083da53c5SAndreas Gruenbacher dsec = inode_security(dir); 182183da53c5SAndreas Gruenbacher isec = backing_inode_security(dentry); 18221da177e4SLinus Torvalds 182350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 1824a269434dSEric Paris ad.u.dentry = dentry; 18251da177e4SLinus Torvalds 18261da177e4SLinus Torvalds av = DIR__SEARCH; 18271da177e4SLinus Torvalds av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME); 18286b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 18296b6bc620SStephen Smalley sid, dsec->sid, SECCLASS_DIR, av, &ad); 18301da177e4SLinus Torvalds if (rc) 18311da177e4SLinus Torvalds return rc; 18321da177e4SLinus Torvalds 18331da177e4SLinus Torvalds switch (kind) { 18341da177e4SLinus Torvalds case MAY_LINK: 18351da177e4SLinus Torvalds av = FILE__LINK; 18361da177e4SLinus Torvalds break; 18371da177e4SLinus Torvalds case MAY_UNLINK: 18381da177e4SLinus Torvalds av = FILE__UNLINK; 18391da177e4SLinus Torvalds break; 18401da177e4SLinus Torvalds case MAY_RMDIR: 18411da177e4SLinus Torvalds av = DIR__RMDIR; 18421da177e4SLinus Torvalds break; 18431da177e4SLinus Torvalds default: 1844c103a91eSpeter enderborg pr_warn("SELinux: %s: unrecognized kind %d\n", 1845744ba35eSEric Paris __func__, kind); 18461da177e4SLinus Torvalds return 0; 18471da177e4SLinus Torvalds } 18481da177e4SLinus Torvalds 18496b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 18506b6bc620SStephen Smalley sid, isec->sid, isec->sclass, av, &ad); 18511da177e4SLinus Torvalds return rc; 18521da177e4SLinus Torvalds } 18531da177e4SLinus Torvalds 18541da177e4SLinus Torvalds static inline int may_rename(struct inode *old_dir, 18551da177e4SLinus Torvalds struct dentry *old_dentry, 18561da177e4SLinus Torvalds struct inode *new_dir, 18571da177e4SLinus Torvalds struct dentry *new_dentry) 18581da177e4SLinus Torvalds { 18591da177e4SLinus Torvalds struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec; 18602bf49690SThomas Liu struct common_audit_data ad; 1861275bb41eSDavid Howells u32 sid = current_sid(); 18621da177e4SLinus Torvalds u32 av; 18631da177e4SLinus Torvalds int old_is_dir, new_is_dir; 18641da177e4SLinus Torvalds int rc; 18651da177e4SLinus Torvalds 186683da53c5SAndreas Gruenbacher old_dsec = inode_security(old_dir); 186783da53c5SAndreas Gruenbacher old_isec = backing_inode_security(old_dentry); 1868e36cb0b8SDavid Howells old_is_dir = d_is_dir(old_dentry); 186983da53c5SAndreas Gruenbacher new_dsec = inode_security(new_dir); 18701da177e4SLinus Torvalds 187150c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 18721da177e4SLinus Torvalds 1873a269434dSEric Paris ad.u.dentry = old_dentry; 18746b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 18756b6bc620SStephen Smalley sid, old_dsec->sid, SECCLASS_DIR, 18761da177e4SLinus Torvalds DIR__REMOVE_NAME | DIR__SEARCH, &ad); 18771da177e4SLinus Torvalds if (rc) 18781da177e4SLinus Torvalds return rc; 18796b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 18806b6bc620SStephen Smalley sid, old_isec->sid, 18811da177e4SLinus Torvalds old_isec->sclass, FILE__RENAME, &ad); 18821da177e4SLinus Torvalds if (rc) 18831da177e4SLinus Torvalds return rc; 18841da177e4SLinus Torvalds if (old_is_dir && new_dir != old_dir) { 18856b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 18866b6bc620SStephen Smalley sid, old_isec->sid, 18871da177e4SLinus Torvalds old_isec->sclass, DIR__REPARENT, &ad); 18881da177e4SLinus Torvalds if (rc) 18891da177e4SLinus Torvalds return rc; 18901da177e4SLinus Torvalds } 18911da177e4SLinus Torvalds 1892a269434dSEric Paris ad.u.dentry = new_dentry; 18931da177e4SLinus Torvalds av = DIR__ADD_NAME | DIR__SEARCH; 18942c616d4dSDavid Howells if (d_is_positive(new_dentry)) 18951da177e4SLinus Torvalds av |= DIR__REMOVE_NAME; 18966b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 18976b6bc620SStephen Smalley sid, new_dsec->sid, SECCLASS_DIR, av, &ad); 18981da177e4SLinus Torvalds if (rc) 18991da177e4SLinus Torvalds return rc; 19002c616d4dSDavid Howells if (d_is_positive(new_dentry)) { 190183da53c5SAndreas Gruenbacher new_isec = backing_inode_security(new_dentry); 1902e36cb0b8SDavid Howells new_is_dir = d_is_dir(new_dentry); 19036b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 19046b6bc620SStephen Smalley sid, new_isec->sid, 19051da177e4SLinus Torvalds new_isec->sclass, 19061da177e4SLinus Torvalds (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad); 19071da177e4SLinus Torvalds if (rc) 19081da177e4SLinus Torvalds return rc; 19091da177e4SLinus Torvalds } 19101da177e4SLinus Torvalds 19111da177e4SLinus Torvalds return 0; 19121da177e4SLinus Torvalds } 19131da177e4SLinus Torvalds 19141da177e4SLinus Torvalds /* Check whether a task can perform a filesystem operation. */ 191588e67f3bSDavid Howells static int superblock_has_perm(const struct cred *cred, 19161da177e4SLinus Torvalds struct super_block *sb, 19171da177e4SLinus Torvalds u32 perms, 19182bf49690SThomas Liu struct common_audit_data *ad) 19191da177e4SLinus Torvalds { 19201da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 192188e67f3bSDavid Howells u32 sid = cred_sid(cred); 19221da177e4SLinus Torvalds 19231da177e4SLinus Torvalds sbsec = sb->s_security; 19246b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 19256b6bc620SStephen Smalley sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad); 19261da177e4SLinus Torvalds } 19271da177e4SLinus Torvalds 19281da177e4SLinus Torvalds /* Convert a Linux mode and permission mask to an access vector. */ 19291da177e4SLinus Torvalds static inline u32 file_mask_to_av(int mode, int mask) 19301da177e4SLinus Torvalds { 19311da177e4SLinus Torvalds u32 av = 0; 19321da177e4SLinus Torvalds 1933dba19c60SAl Viro if (!S_ISDIR(mode)) { 19341da177e4SLinus Torvalds if (mask & MAY_EXEC) 19351da177e4SLinus Torvalds av |= FILE__EXECUTE; 19361da177e4SLinus Torvalds if (mask & MAY_READ) 19371da177e4SLinus Torvalds av |= FILE__READ; 19381da177e4SLinus Torvalds 19391da177e4SLinus Torvalds if (mask & MAY_APPEND) 19401da177e4SLinus Torvalds av |= FILE__APPEND; 19411da177e4SLinus Torvalds else if (mask & MAY_WRITE) 19421da177e4SLinus Torvalds av |= FILE__WRITE; 19431da177e4SLinus Torvalds 19441da177e4SLinus Torvalds } else { 19451da177e4SLinus Torvalds if (mask & MAY_EXEC) 19461da177e4SLinus Torvalds av |= DIR__SEARCH; 19471da177e4SLinus Torvalds if (mask & MAY_WRITE) 19481da177e4SLinus Torvalds av |= DIR__WRITE; 19491da177e4SLinus Torvalds if (mask & MAY_READ) 19501da177e4SLinus Torvalds av |= DIR__READ; 19511da177e4SLinus Torvalds } 19521da177e4SLinus Torvalds 19531da177e4SLinus Torvalds return av; 19541da177e4SLinus Torvalds } 19551da177e4SLinus Torvalds 19561da177e4SLinus Torvalds /* Convert a Linux file to an access vector. */ 19571da177e4SLinus Torvalds static inline u32 file_to_av(struct file *file) 19581da177e4SLinus Torvalds { 19591da177e4SLinus Torvalds u32 av = 0; 19601da177e4SLinus Torvalds 19611da177e4SLinus Torvalds if (file->f_mode & FMODE_READ) 19621da177e4SLinus Torvalds av |= FILE__READ; 19631da177e4SLinus Torvalds if (file->f_mode & FMODE_WRITE) { 19641da177e4SLinus Torvalds if (file->f_flags & O_APPEND) 19651da177e4SLinus Torvalds av |= FILE__APPEND; 19661da177e4SLinus Torvalds else 19671da177e4SLinus Torvalds av |= FILE__WRITE; 19681da177e4SLinus Torvalds } 19690794c66dSStephen Smalley if (!av) { 19700794c66dSStephen Smalley /* 19710794c66dSStephen Smalley * Special file opened with flags 3 for ioctl-only use. 19720794c66dSStephen Smalley */ 19730794c66dSStephen Smalley av = FILE__IOCTL; 19740794c66dSStephen Smalley } 19751da177e4SLinus Torvalds 19761da177e4SLinus Torvalds return av; 19771da177e4SLinus Torvalds } 19781da177e4SLinus Torvalds 19798b6a5a37SEric Paris /* 19808b6a5a37SEric Paris * Convert a file to an access vector and include the correct open 19818b6a5a37SEric Paris * open permission. 19828b6a5a37SEric Paris */ 19838b6a5a37SEric Paris static inline u32 open_file_to_av(struct file *file) 19848b6a5a37SEric Paris { 19858b6a5a37SEric Paris u32 av = file_to_av(file); 1986ccb54478SStephen Smalley struct inode *inode = file_inode(file); 19878b6a5a37SEric Paris 1988aa8e712cSStephen Smalley if (selinux_policycap_openperm() && 1989aa8e712cSStephen Smalley inode->i_sb->s_magic != SOCKFS_MAGIC) 19908b6a5a37SEric Paris av |= FILE__OPEN; 199149b7b8deSEric Paris 19928b6a5a37SEric Paris return av; 19938b6a5a37SEric Paris } 19948b6a5a37SEric Paris 19951da177e4SLinus Torvalds /* Hook functions begin here. */ 19961da177e4SLinus Torvalds 199779af7307SStephen Smalley static int selinux_binder_set_context_mgr(struct task_struct *mgr) 199879af7307SStephen Smalley { 199979af7307SStephen Smalley u32 mysid = current_sid(); 200079af7307SStephen Smalley u32 mgrsid = task_sid(mgr); 200179af7307SStephen Smalley 20026b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 20036b6bc620SStephen Smalley mysid, mgrsid, SECCLASS_BINDER, 200479af7307SStephen Smalley BINDER__SET_CONTEXT_MGR, NULL); 200579af7307SStephen Smalley } 200679af7307SStephen Smalley 200779af7307SStephen Smalley static int selinux_binder_transaction(struct task_struct *from, 200879af7307SStephen Smalley struct task_struct *to) 200979af7307SStephen Smalley { 201079af7307SStephen Smalley u32 mysid = current_sid(); 201179af7307SStephen Smalley u32 fromsid = task_sid(from); 201279af7307SStephen Smalley u32 tosid = task_sid(to); 201379af7307SStephen Smalley int rc; 201479af7307SStephen Smalley 201579af7307SStephen Smalley if (mysid != fromsid) { 20166b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 20176b6bc620SStephen Smalley mysid, fromsid, SECCLASS_BINDER, 201879af7307SStephen Smalley BINDER__IMPERSONATE, NULL); 201979af7307SStephen Smalley if (rc) 202079af7307SStephen Smalley return rc; 202179af7307SStephen Smalley } 202279af7307SStephen Smalley 20236b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 20246b6bc620SStephen Smalley fromsid, tosid, SECCLASS_BINDER, BINDER__CALL, 202579af7307SStephen Smalley NULL); 202679af7307SStephen Smalley } 202779af7307SStephen Smalley 202879af7307SStephen Smalley static int selinux_binder_transfer_binder(struct task_struct *from, 202979af7307SStephen Smalley struct task_struct *to) 203079af7307SStephen Smalley { 203179af7307SStephen Smalley u32 fromsid = task_sid(from); 203279af7307SStephen Smalley u32 tosid = task_sid(to); 203379af7307SStephen Smalley 20346b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 20356b6bc620SStephen Smalley fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER, 203679af7307SStephen Smalley NULL); 203779af7307SStephen Smalley } 203879af7307SStephen Smalley 203979af7307SStephen Smalley static int selinux_binder_transfer_file(struct task_struct *from, 204079af7307SStephen Smalley struct task_struct *to, 204179af7307SStephen Smalley struct file *file) 204279af7307SStephen Smalley { 204379af7307SStephen Smalley u32 sid = task_sid(to); 2044bb6c6b02SCasey Schaufler struct file_security_struct *fsec = selinux_file(file); 204583da53c5SAndreas Gruenbacher struct dentry *dentry = file->f_path.dentry; 204620cdef8dSPaul Moore struct inode_security_struct *isec; 204779af7307SStephen Smalley struct common_audit_data ad; 204879af7307SStephen Smalley int rc; 204979af7307SStephen Smalley 205079af7307SStephen Smalley ad.type = LSM_AUDIT_DATA_PATH; 205179af7307SStephen Smalley ad.u.path = file->f_path; 205279af7307SStephen Smalley 205379af7307SStephen Smalley if (sid != fsec->sid) { 20546b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 20556b6bc620SStephen Smalley sid, fsec->sid, 205679af7307SStephen Smalley SECCLASS_FD, 205779af7307SStephen Smalley FD__USE, 205879af7307SStephen Smalley &ad); 205979af7307SStephen Smalley if (rc) 206079af7307SStephen Smalley return rc; 206179af7307SStephen Smalley } 206279af7307SStephen Smalley 2063f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL 2064f66e448cSChenbo Feng rc = bpf_fd_pass(file, sid); 2065f66e448cSChenbo Feng if (rc) 2066f66e448cSChenbo Feng return rc; 2067f66e448cSChenbo Feng #endif 2068f66e448cSChenbo Feng 206983da53c5SAndreas Gruenbacher if (unlikely(IS_PRIVATE(d_backing_inode(dentry)))) 207079af7307SStephen Smalley return 0; 207179af7307SStephen Smalley 207220cdef8dSPaul Moore isec = backing_inode_security(dentry); 20736b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 20746b6bc620SStephen Smalley sid, isec->sid, isec->sclass, file_to_av(file), 207579af7307SStephen Smalley &ad); 207679af7307SStephen Smalley } 207779af7307SStephen Smalley 20789e48858fSIngo Molnar static int selinux_ptrace_access_check(struct task_struct *child, 2079006ebb40SStephen Smalley unsigned int mode) 20801da177e4SLinus Torvalds { 2081275bb41eSDavid Howells u32 sid = current_sid(); 2082275bb41eSDavid Howells u32 csid = task_sid(child); 2083006ebb40SStephen Smalley 2084be0554c9SStephen Smalley if (mode & PTRACE_MODE_READ) 20856b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 20866b6bc620SStephen Smalley sid, csid, SECCLASS_FILE, FILE__READ, NULL); 2087be0554c9SStephen Smalley 20886b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 20896b6bc620SStephen Smalley sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL); 20905cd9c58fSDavid Howells } 20915cd9c58fSDavid Howells 20925cd9c58fSDavid Howells static int selinux_ptrace_traceme(struct task_struct *parent) 20935cd9c58fSDavid Howells { 20946b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 20956b6bc620SStephen Smalley task_sid(parent), current_sid(), SECCLASS_PROCESS, 2096be0554c9SStephen Smalley PROCESS__PTRACE, NULL); 20971da177e4SLinus Torvalds } 20981da177e4SLinus Torvalds 20991da177e4SLinus Torvalds static int selinux_capget(struct task_struct *target, kernel_cap_t *effective, 21001da177e4SLinus Torvalds kernel_cap_t *inheritable, kernel_cap_t *permitted) 21011da177e4SLinus Torvalds { 21026b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 21036b6bc620SStephen Smalley current_sid(), task_sid(target), SECCLASS_PROCESS, 2104be0554c9SStephen Smalley PROCESS__GETCAP, NULL); 21051da177e4SLinus Torvalds } 21061da177e4SLinus Torvalds 2107d84f4f99SDavid Howells static int selinux_capset(struct cred *new, const struct cred *old, 2108d84f4f99SDavid Howells const kernel_cap_t *effective, 210915a2460eSDavid Howells const kernel_cap_t *inheritable, 211015a2460eSDavid Howells const kernel_cap_t *permitted) 21111da177e4SLinus Torvalds { 21126b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 21136b6bc620SStephen Smalley cred_sid(old), cred_sid(new), SECCLASS_PROCESS, 2114be0554c9SStephen Smalley PROCESS__SETCAP, NULL); 21151da177e4SLinus Torvalds } 21161da177e4SLinus Torvalds 21175626d3e8SJames Morris /* 21185626d3e8SJames Morris * (This comment used to live with the selinux_task_setuid hook, 21195626d3e8SJames Morris * which was removed). 21205626d3e8SJames Morris * 21215626d3e8SJames Morris * Since setuid only affects the current process, and since the SELinux 21225626d3e8SJames Morris * controls are not based on the Linux identity attributes, SELinux does not 21235626d3e8SJames Morris * need to control this operation. However, SELinux does control the use of 21245626d3e8SJames Morris * the CAP_SETUID and CAP_SETGID capabilities using the capable hook. 21255626d3e8SJames Morris */ 21265626d3e8SJames Morris 21276a9de491SEric Paris static int selinux_capable(const struct cred *cred, struct user_namespace *ns, 2128c1a85a00SMicah Morton int cap, unsigned int opts) 21291da177e4SLinus Torvalds { 2130c1a85a00SMicah Morton return cred_has_capability(cred, cap, opts, ns == &init_user_ns); 21311da177e4SLinus Torvalds } 21321da177e4SLinus Torvalds 21331da177e4SLinus Torvalds static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb) 21341da177e4SLinus Torvalds { 213588e67f3bSDavid Howells const struct cred *cred = current_cred(); 21361da177e4SLinus Torvalds int rc = 0; 21371da177e4SLinus Torvalds 21381da177e4SLinus Torvalds if (!sb) 21391da177e4SLinus Torvalds return 0; 21401da177e4SLinus Torvalds 21411da177e4SLinus Torvalds switch (cmds) { 21421da177e4SLinus Torvalds case Q_SYNC: 21431da177e4SLinus Torvalds case Q_QUOTAON: 21441da177e4SLinus Torvalds case Q_QUOTAOFF: 21451da177e4SLinus Torvalds case Q_SETINFO: 21461da177e4SLinus Torvalds case Q_SETQUOTA: 214788e67f3bSDavid Howells rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL); 21481da177e4SLinus Torvalds break; 21491da177e4SLinus Torvalds case Q_GETFMT: 21501da177e4SLinus Torvalds case Q_GETINFO: 21511da177e4SLinus Torvalds case Q_GETQUOTA: 215288e67f3bSDavid Howells rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL); 21531da177e4SLinus Torvalds break; 21541da177e4SLinus Torvalds default: 21551da177e4SLinus Torvalds rc = 0; /* let the kernel handle invalid cmds */ 21561da177e4SLinus Torvalds break; 21571da177e4SLinus Torvalds } 21581da177e4SLinus Torvalds return rc; 21591da177e4SLinus Torvalds } 21601da177e4SLinus Torvalds 21611da177e4SLinus Torvalds static int selinux_quota_on(struct dentry *dentry) 21621da177e4SLinus Torvalds { 216388e67f3bSDavid Howells const struct cred *cred = current_cred(); 216488e67f3bSDavid Howells 21652875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__QUOTAON); 21661da177e4SLinus Torvalds } 21671da177e4SLinus Torvalds 216812b3052cSEric Paris static int selinux_syslog(int type) 21691da177e4SLinus Torvalds { 21701da177e4SLinus Torvalds switch (type) { 2171d78ca3cdSKees Cook case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */ 2172d78ca3cdSKees Cook case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */ 21736b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 21746b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, 2175be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL); 2176d78ca3cdSKees Cook case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */ 2177d78ca3cdSKees Cook case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */ 2178d78ca3cdSKees Cook /* Set level of messages printed to console */ 2179d78ca3cdSKees Cook case SYSLOG_ACTION_CONSOLE_LEVEL: 21806b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 21816b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, 2182be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE, 2183be0554c9SStephen Smalley NULL); 21841da177e4SLinus Torvalds } 2185be0554c9SStephen Smalley /* All other syslog types */ 21866b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 21876b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, 2188be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL); 21891da177e4SLinus Torvalds } 21901da177e4SLinus Torvalds 21911da177e4SLinus Torvalds /* 21921da177e4SLinus Torvalds * Check that a process has enough memory to allocate a new virtual 21931da177e4SLinus Torvalds * mapping. 0 means there is enough memory for the allocation to 21941da177e4SLinus Torvalds * succeed and -ENOMEM implies there is not. 21951da177e4SLinus Torvalds * 21961da177e4SLinus Torvalds * Do not audit the selinux permission check, as this is applied to all 21971da177e4SLinus Torvalds * processes that allocate mappings. 21981da177e4SLinus Torvalds */ 219934b4e4aaSAlan Cox static int selinux_vm_enough_memory(struct mm_struct *mm, long pages) 22001da177e4SLinus Torvalds { 22011da177e4SLinus Torvalds int rc, cap_sys_admin = 0; 22021da177e4SLinus Torvalds 2203b1d9e6b0SCasey Schaufler rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN, 2204c1a85a00SMicah Morton CAP_OPT_NOAUDIT, true); 22051da177e4SLinus Torvalds if (rc == 0) 22061da177e4SLinus Torvalds cap_sys_admin = 1; 22071da177e4SLinus Torvalds 2208b1d9e6b0SCasey Schaufler return cap_sys_admin; 22091da177e4SLinus Torvalds } 22101da177e4SLinus Torvalds 22111da177e4SLinus Torvalds /* binprm security operations */ 22121da177e4SLinus Torvalds 2213be0554c9SStephen Smalley static u32 ptrace_parent_sid(void) 22140c6181cbSPaul Moore { 22150c6181cbSPaul Moore u32 sid = 0; 22160c6181cbSPaul Moore struct task_struct *tracer; 22170c6181cbSPaul Moore 22180c6181cbSPaul Moore rcu_read_lock(); 2219be0554c9SStephen Smalley tracer = ptrace_parent(current); 22200c6181cbSPaul Moore if (tracer) 22210c6181cbSPaul Moore sid = task_sid(tracer); 22220c6181cbSPaul Moore rcu_read_unlock(); 22230c6181cbSPaul Moore 22240c6181cbSPaul Moore return sid; 22250c6181cbSPaul Moore } 22260c6181cbSPaul Moore 22277b0d0b40SStephen Smalley static int check_nnp_nosuid(const struct linux_binprm *bprm, 22287b0d0b40SStephen Smalley const struct task_security_struct *old_tsec, 22297b0d0b40SStephen Smalley const struct task_security_struct *new_tsec) 22307b0d0b40SStephen Smalley { 22317b0d0b40SStephen Smalley int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS); 2232380cf5baSAndy Lutomirski int nosuid = !mnt_may_suid(bprm->file->f_path.mnt); 22337b0d0b40SStephen Smalley int rc; 2234af63f419SStephen Smalley u32 av; 22357b0d0b40SStephen Smalley 22367b0d0b40SStephen Smalley if (!nnp && !nosuid) 22377b0d0b40SStephen Smalley return 0; /* neither NNP nor nosuid */ 22387b0d0b40SStephen Smalley 22397b0d0b40SStephen Smalley if (new_tsec->sid == old_tsec->sid) 22407b0d0b40SStephen Smalley return 0; /* No change in credentials */ 22417b0d0b40SStephen Smalley 22427b0d0b40SStephen Smalley /* 2243af63f419SStephen Smalley * If the policy enables the nnp_nosuid_transition policy capability, 2244af63f419SStephen Smalley * then we permit transitions under NNP or nosuid if the 2245af63f419SStephen Smalley * policy allows the corresponding permission between 2246af63f419SStephen Smalley * the old and new contexts. 2247af63f419SStephen Smalley */ 2248aa8e712cSStephen Smalley if (selinux_policycap_nnp_nosuid_transition()) { 2249af63f419SStephen Smalley av = 0; 2250af63f419SStephen Smalley if (nnp) 2251af63f419SStephen Smalley av |= PROCESS2__NNP_TRANSITION; 2252af63f419SStephen Smalley if (nosuid) 2253af63f419SStephen Smalley av |= PROCESS2__NOSUID_TRANSITION; 22546b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 22556b6bc620SStephen Smalley old_tsec->sid, new_tsec->sid, 2256af63f419SStephen Smalley SECCLASS_PROCESS2, av, NULL); 2257af63f419SStephen Smalley if (!rc) 2258af63f419SStephen Smalley return 0; 2259af63f419SStephen Smalley } 2260af63f419SStephen Smalley 2261af63f419SStephen Smalley /* 2262af63f419SStephen Smalley * We also permit NNP or nosuid transitions to bounded SIDs, 2263af63f419SStephen Smalley * i.e. SIDs that are guaranteed to only be allowed a subset 2264af63f419SStephen Smalley * of the permissions of the current SID. 22657b0d0b40SStephen Smalley */ 2266aa8e712cSStephen Smalley rc = security_bounded_transition(&selinux_state, old_tsec->sid, 2267aa8e712cSStephen Smalley new_tsec->sid); 2268af63f419SStephen Smalley if (!rc) 2269af63f419SStephen Smalley return 0; 2270af63f419SStephen Smalley 22717b0d0b40SStephen Smalley /* 22727b0d0b40SStephen Smalley * On failure, preserve the errno values for NNP vs nosuid. 22737b0d0b40SStephen Smalley * NNP: Operation not permitted for caller. 22747b0d0b40SStephen Smalley * nosuid: Permission denied to file. 22757b0d0b40SStephen Smalley */ 22767b0d0b40SStephen Smalley if (nnp) 22777b0d0b40SStephen Smalley return -EPERM; 22787b0d0b40SStephen Smalley return -EACCES; 22797b0d0b40SStephen Smalley } 22807b0d0b40SStephen Smalley 2281a6f76f23SDavid Howells static int selinux_bprm_set_creds(struct linux_binprm *bprm) 22821da177e4SLinus Torvalds { 2283a6f76f23SDavid Howells const struct task_security_struct *old_tsec; 2284a6f76f23SDavid Howells struct task_security_struct *new_tsec; 22851da177e4SLinus Torvalds struct inode_security_struct *isec; 22862bf49690SThomas Liu struct common_audit_data ad; 2287496ad9aaSAl Viro struct inode *inode = file_inode(bprm->file); 22881da177e4SLinus Torvalds int rc; 22891da177e4SLinus Torvalds 2290a6f76f23SDavid Howells /* SELinux context only depends on initial program or script and not 2291a6f76f23SDavid Howells * the script interpreter */ 2292ddb4a144SKees Cook if (bprm->called_set_creds) 22931da177e4SLinus Torvalds return 0; 22941da177e4SLinus Torvalds 22950c6cfa62SCasey Schaufler old_tsec = selinux_cred(current_cred()); 22960c6cfa62SCasey Schaufler new_tsec = selinux_cred(bprm->cred); 229783da53c5SAndreas Gruenbacher isec = inode_security(inode); 22981da177e4SLinus Torvalds 22991da177e4SLinus Torvalds /* Default to the current task SID. */ 2300a6f76f23SDavid Howells new_tsec->sid = old_tsec->sid; 2301a6f76f23SDavid Howells new_tsec->osid = old_tsec->sid; 23021da177e4SLinus Torvalds 230328eba5bfSMichael LeMay /* Reset fs, key, and sock SIDs on execve. */ 2304a6f76f23SDavid Howells new_tsec->create_sid = 0; 2305a6f76f23SDavid Howells new_tsec->keycreate_sid = 0; 2306a6f76f23SDavid Howells new_tsec->sockcreate_sid = 0; 23071da177e4SLinus Torvalds 2308a6f76f23SDavid Howells if (old_tsec->exec_sid) { 2309a6f76f23SDavid Howells new_tsec->sid = old_tsec->exec_sid; 23101da177e4SLinus Torvalds /* Reset exec SID on execve. */ 2311a6f76f23SDavid Howells new_tsec->exec_sid = 0; 2312259e5e6cSAndy Lutomirski 23137b0d0b40SStephen Smalley /* Fail on NNP or nosuid if not an allowed transition. */ 23147b0d0b40SStephen Smalley rc = check_nnp_nosuid(bprm, old_tsec, new_tsec); 23157b0d0b40SStephen Smalley if (rc) 23167b0d0b40SStephen Smalley return rc; 23171da177e4SLinus Torvalds } else { 23181da177e4SLinus Torvalds /* Check for a default transition on this program. */ 2319aa8e712cSStephen Smalley rc = security_transition_sid(&selinux_state, old_tsec->sid, 2320aa8e712cSStephen Smalley isec->sid, SECCLASS_PROCESS, NULL, 2321652bb9b0SEric Paris &new_tsec->sid); 23221da177e4SLinus Torvalds if (rc) 23231da177e4SLinus Torvalds return rc; 23247b0d0b40SStephen Smalley 23257b0d0b40SStephen Smalley /* 23267b0d0b40SStephen Smalley * Fallback to old SID on NNP or nosuid if not an allowed 23277b0d0b40SStephen Smalley * transition. 23287b0d0b40SStephen Smalley */ 23297b0d0b40SStephen Smalley rc = check_nnp_nosuid(bprm, old_tsec, new_tsec); 23307b0d0b40SStephen Smalley if (rc) 23317b0d0b40SStephen Smalley new_tsec->sid = old_tsec->sid; 23321da177e4SLinus Torvalds } 23331da177e4SLinus Torvalds 233443af5de7SVivek Goyal ad.type = LSM_AUDIT_DATA_FILE; 233543af5de7SVivek Goyal ad.u.file = bprm->file; 23361da177e4SLinus Torvalds 2337a6f76f23SDavid Howells if (new_tsec->sid == old_tsec->sid) { 23386b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 23396b6bc620SStephen Smalley old_tsec->sid, isec->sid, 23401da177e4SLinus Torvalds SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad); 23411da177e4SLinus Torvalds if (rc) 23421da177e4SLinus Torvalds return rc; 23431da177e4SLinus Torvalds } else { 23441da177e4SLinus Torvalds /* Check permissions for the transition. */ 23456b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 23466b6bc620SStephen Smalley old_tsec->sid, new_tsec->sid, 23471da177e4SLinus Torvalds SECCLASS_PROCESS, PROCESS__TRANSITION, &ad); 23481da177e4SLinus Torvalds if (rc) 23491da177e4SLinus Torvalds return rc; 23501da177e4SLinus Torvalds 23516b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 23526b6bc620SStephen Smalley new_tsec->sid, isec->sid, 23531da177e4SLinus Torvalds SECCLASS_FILE, FILE__ENTRYPOINT, &ad); 23541da177e4SLinus Torvalds if (rc) 23551da177e4SLinus Torvalds return rc; 23561da177e4SLinus Torvalds 2357a6f76f23SDavid Howells /* Check for shared state */ 2358a6f76f23SDavid Howells if (bprm->unsafe & LSM_UNSAFE_SHARE) { 23596b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 23606b6bc620SStephen Smalley old_tsec->sid, new_tsec->sid, 2361a6f76f23SDavid Howells SECCLASS_PROCESS, PROCESS__SHARE, 2362a6f76f23SDavid Howells NULL); 2363a6f76f23SDavid Howells if (rc) 2364a6f76f23SDavid Howells return -EPERM; 23651da177e4SLinus Torvalds } 23661da177e4SLinus Torvalds 2367a6f76f23SDavid Howells /* Make sure that anyone attempting to ptrace over a task that 2368a6f76f23SDavid Howells * changes its SID has the appropriate permit */ 23699227dd2aSEric W. Biederman if (bprm->unsafe & LSM_UNSAFE_PTRACE) { 2370be0554c9SStephen Smalley u32 ptsid = ptrace_parent_sid(); 2371a6f76f23SDavid Howells if (ptsid != 0) { 23726b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 23736b6bc620SStephen Smalley ptsid, new_tsec->sid, 2374a6f76f23SDavid Howells SECCLASS_PROCESS, 2375a6f76f23SDavid Howells PROCESS__PTRACE, NULL); 2376a6f76f23SDavid Howells if (rc) 2377a6f76f23SDavid Howells return -EPERM; 2378a6f76f23SDavid Howells } 2379a6f76f23SDavid Howells } 2380a6f76f23SDavid Howells 2381a6f76f23SDavid Howells /* Clear any possibly unsafe personality bits on exec: */ 2382a6f76f23SDavid Howells bprm->per_clear |= PER_CLEAR_ON_SETID; 2383a6f76f23SDavid Howells 23841da177e4SLinus Torvalds /* Enable secure mode for SIDs transitions unless 23851da177e4SLinus Torvalds the noatsecure permission is granted between 23861da177e4SLinus Torvalds the two SIDs, i.e. ahp returns 0. */ 23876b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 23886b6bc620SStephen Smalley old_tsec->sid, new_tsec->sid, 238962874c3aSKees Cook SECCLASS_PROCESS, PROCESS__NOATSECURE, 239062874c3aSKees Cook NULL); 239162874c3aSKees Cook bprm->secureexec |= !!rc; 23921da177e4SLinus Torvalds } 23931da177e4SLinus Torvalds 239462874c3aSKees Cook return 0; 23951da177e4SLinus Torvalds } 23961da177e4SLinus Torvalds 2397c3c073f8SAl Viro static int match_file(const void *p, struct file *file, unsigned fd) 2398c3c073f8SAl Viro { 2399c3c073f8SAl Viro return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0; 2400c3c073f8SAl Viro } 2401c3c073f8SAl Viro 24021da177e4SLinus Torvalds /* Derived from fs/exec.c:flush_old_files. */ 2403745ca247SDavid Howells static inline void flush_unauthorized_files(const struct cred *cred, 2404745ca247SDavid Howells struct files_struct *files) 24051da177e4SLinus Torvalds { 24061da177e4SLinus Torvalds struct file *file, *devnull = NULL; 2407b20c8122SStephen Smalley struct tty_struct *tty; 240824ec839cSPeter Zijlstra int drop_tty = 0; 2409c3c073f8SAl Viro unsigned n; 24101da177e4SLinus Torvalds 241124ec839cSPeter Zijlstra tty = get_current_tty(); 24121da177e4SLinus Torvalds if (tty) { 24134a510969SPeter Hurley spin_lock(&tty->files_lock); 241437dd0bd0SEric Paris if (!list_empty(&tty->tty_files)) { 2415d996b62aSNick Piggin struct tty_file_private *file_priv; 241637dd0bd0SEric Paris 24171da177e4SLinus Torvalds /* Revalidate access to controlling tty. 241813f8e981SDavid Howells Use file_path_has_perm on the tty path directly 241913f8e981SDavid Howells rather than using file_has_perm, as this particular 242013f8e981SDavid Howells open file may belong to another process and we are 242113f8e981SDavid Howells only interested in the inode-based check here. */ 2422d996b62aSNick Piggin file_priv = list_first_entry(&tty->tty_files, 2423d996b62aSNick Piggin struct tty_file_private, list); 2424d996b62aSNick Piggin file = file_priv->file; 242513f8e981SDavid Howells if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE)) 242624ec839cSPeter Zijlstra drop_tty = 1; 24271da177e4SLinus Torvalds } 24284a510969SPeter Hurley spin_unlock(&tty->files_lock); 2429452a00d2SAlan Cox tty_kref_put(tty); 24301da177e4SLinus Torvalds } 243198a27ba4SEric W. Biederman /* Reset controlling tty. */ 243298a27ba4SEric W. Biederman if (drop_tty) 243398a27ba4SEric W. Biederman no_tty(); 24341da177e4SLinus Torvalds 24351da177e4SLinus Torvalds /* Revalidate access to inherited open files. */ 2436c3c073f8SAl Viro n = iterate_fd(files, 0, match_file, cred); 2437c3c073f8SAl Viro if (!n) /* none found? */ 2438c3c073f8SAl Viro return; 24391da177e4SLinus Torvalds 2440c3c073f8SAl Viro devnull = dentry_open(&selinux_null, O_RDWR, cred); 244145525b26SAl Viro if (IS_ERR(devnull)) 244245525b26SAl Viro devnull = NULL; 2443c3c073f8SAl Viro /* replace all the matching ones with this */ 2444c3c073f8SAl Viro do { 244545525b26SAl Viro replace_fd(n - 1, devnull, 0); 2446c3c073f8SAl Viro } while ((n = iterate_fd(files, n, match_file, cred)) != 0); 244745525b26SAl Viro if (devnull) 2448c3c073f8SAl Viro fput(devnull); 24491da177e4SLinus Torvalds } 24501da177e4SLinus Torvalds 24511da177e4SLinus Torvalds /* 2452a6f76f23SDavid Howells * Prepare a process for imminent new credential changes due to exec 24531da177e4SLinus Torvalds */ 2454a6f76f23SDavid Howells static void selinux_bprm_committing_creds(struct linux_binprm *bprm) 24551da177e4SLinus Torvalds { 2456a6f76f23SDavid Howells struct task_security_struct *new_tsec; 24571da177e4SLinus Torvalds struct rlimit *rlim, *initrlim; 24581da177e4SLinus Torvalds int rc, i; 24591da177e4SLinus Torvalds 24600c6cfa62SCasey Schaufler new_tsec = selinux_cred(bprm->cred); 2461a6f76f23SDavid Howells if (new_tsec->sid == new_tsec->osid) 24621da177e4SLinus Torvalds return; 24631da177e4SLinus Torvalds 24641da177e4SLinus Torvalds /* Close files for which the new task SID is not authorized. */ 2465a6f76f23SDavid Howells flush_unauthorized_files(bprm->cred, current->files); 24661da177e4SLinus Torvalds 2467a6f76f23SDavid Howells /* Always clear parent death signal on SID transitions. */ 2468a6f76f23SDavid Howells current->pdeath_signal = 0; 2469a6f76f23SDavid Howells 2470a6f76f23SDavid Howells /* Check whether the new SID can inherit resource limits from the old 2471a6f76f23SDavid Howells * SID. If not, reset all soft limits to the lower of the current 2472a6f76f23SDavid Howells * task's hard limit and the init task's soft limit. 2473a6f76f23SDavid Howells * 2474a6f76f23SDavid Howells * Note that the setting of hard limits (even to lower them) can be 2475a6f76f23SDavid Howells * controlled by the setrlimit check. The inclusion of the init task's 2476a6f76f23SDavid Howells * soft limit into the computation is to avoid resetting soft limits 2477a6f76f23SDavid Howells * higher than the default soft limit for cases where the default is 2478a6f76f23SDavid Howells * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK. 2479a6f76f23SDavid Howells */ 24806b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 24816b6bc620SStephen Smalley new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS, 2482a6f76f23SDavid Howells PROCESS__RLIMITINH, NULL); 2483a6f76f23SDavid Howells if (rc) { 2484eb2d55a3SOleg Nesterov /* protect against do_prlimit() */ 2485eb2d55a3SOleg Nesterov task_lock(current); 2486a6f76f23SDavid Howells for (i = 0; i < RLIM_NLIMITS; i++) { 2487a6f76f23SDavid Howells rlim = current->signal->rlim + i; 2488a6f76f23SDavid Howells initrlim = init_task.signal->rlim + i; 2489a6f76f23SDavid Howells rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur); 2490a6f76f23SDavid Howells } 2491eb2d55a3SOleg Nesterov task_unlock(current); 2492baa73d9eSNicolas Pitre if (IS_ENABLED(CONFIG_POSIX_TIMERS)) 2493eb2d55a3SOleg Nesterov update_rlimit_cpu(current, rlimit(RLIMIT_CPU)); 2494a6f76f23SDavid Howells } 2495a6f76f23SDavid Howells } 2496a6f76f23SDavid Howells 2497a6f76f23SDavid Howells /* 2498a6f76f23SDavid Howells * Clean up the process immediately after the installation of new credentials 2499a6f76f23SDavid Howells * due to exec 2500a6f76f23SDavid Howells */ 2501a6f76f23SDavid Howells static void selinux_bprm_committed_creds(struct linux_binprm *bprm) 2502a6f76f23SDavid Howells { 25030c6cfa62SCasey Schaufler const struct task_security_struct *tsec = selinux_cred(current_cred()); 2504a6f76f23SDavid Howells u32 osid, sid; 2505ddbc7d06SArnd Bergmann int rc; 2506a6f76f23SDavid Howells 2507a6f76f23SDavid Howells osid = tsec->osid; 2508a6f76f23SDavid Howells sid = tsec->sid; 2509a6f76f23SDavid Howells 2510a6f76f23SDavid Howells if (sid == osid) 2511a6f76f23SDavid Howells return; 2512a6f76f23SDavid Howells 2513a6f76f23SDavid Howells /* Check whether the new SID can inherit signal state from the old SID. 2514a6f76f23SDavid Howells * If not, clear itimers to avoid subsequent signal generation and 2515a6f76f23SDavid Howells * flush and unblock signals. 2516a6f76f23SDavid Howells * 2517a6f76f23SDavid Howells * This must occur _after_ the task SID has been updated so that any 2518a6f76f23SDavid Howells * kill done after the flush will be checked against the new SID. 2519a6f76f23SDavid Howells */ 25206b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 25216b6bc620SStephen Smalley osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL); 25221da177e4SLinus Torvalds if (rc) { 2523ddbc7d06SArnd Bergmann clear_itimer(); 2524ddbc7d06SArnd Bergmann 25251da177e4SLinus Torvalds spin_lock_irq(¤t->sighand->siglock); 25269e7c8f8cSOleg Nesterov if (!fatal_signal_pending(current)) { 25279e7c8f8cSOleg Nesterov flush_sigqueue(¤t->pending); 25289e7c8f8cSOleg Nesterov flush_sigqueue(¤t->signal->shared_pending); 25291da177e4SLinus Torvalds flush_signal_handlers(current, 1); 25301da177e4SLinus Torvalds sigemptyset(¤t->blocked); 25319e7c8f8cSOleg Nesterov recalc_sigpending(); 25323bcac026SDavid Howells } 25331da177e4SLinus Torvalds spin_unlock_irq(¤t->sighand->siglock); 25341da177e4SLinus Torvalds } 25351da177e4SLinus Torvalds 2536a6f76f23SDavid Howells /* Wake up the parent if it is waiting so that it can recheck 2537a6f76f23SDavid Howells * wait permission to the new task SID. */ 2538ecd6de3cSOleg Nesterov read_lock(&tasklist_lock); 25390b7570e7SOleg Nesterov __wake_up_parent(current, current->real_parent); 2540ecd6de3cSOleg Nesterov read_unlock(&tasklist_lock); 25411da177e4SLinus Torvalds } 25421da177e4SLinus Torvalds 25431da177e4SLinus Torvalds /* superblock security operations */ 25441da177e4SLinus Torvalds 25451da177e4SLinus Torvalds static int selinux_sb_alloc_security(struct super_block *sb) 25461da177e4SLinus Torvalds { 2547cb89e246SPaul Moore struct superblock_security_struct *sbsec; 2548cb89e246SPaul Moore 2549cb89e246SPaul Moore sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL); 2550cb89e246SPaul Moore if (!sbsec) 2551cb89e246SPaul Moore return -ENOMEM; 2552cb89e246SPaul Moore 2553cb89e246SPaul Moore mutex_init(&sbsec->lock); 2554cb89e246SPaul Moore INIT_LIST_HEAD(&sbsec->isec_head); 2555cb89e246SPaul Moore spin_lock_init(&sbsec->isec_lock); 2556cb89e246SPaul Moore sbsec->sb = sb; 2557cb89e246SPaul Moore sbsec->sid = SECINITSID_UNLABELED; 2558cb89e246SPaul Moore sbsec->def_sid = SECINITSID_FILE; 2559cb89e246SPaul Moore sbsec->mntpoint_sid = SECINITSID_UNLABELED; 2560cb89e246SPaul Moore sb->s_security = sbsec; 2561cb89e246SPaul Moore 2562cb89e246SPaul Moore return 0; 25631da177e4SLinus Torvalds } 25641da177e4SLinus Torvalds 25651da177e4SLinus Torvalds static void selinux_sb_free_security(struct super_block *sb) 25661da177e4SLinus Torvalds { 25671da177e4SLinus Torvalds superblock_free_security(sb); 25681da177e4SLinus Torvalds } 25691da177e4SLinus Torvalds 257099dbbb59SAl Viro static inline int opt_len(const char *s) 25711da177e4SLinus Torvalds { 257299dbbb59SAl Viro bool open_quote = false; 257399dbbb59SAl Viro int len; 257499dbbb59SAl Viro char c; 25751da177e4SLinus Torvalds 257699dbbb59SAl Viro for (len = 0; (c = s[len]) != '\0'; len++) { 257799dbbb59SAl Viro if (c == '"') 25783528a953SCory Olmo open_quote = !open_quote; 257999dbbb59SAl Viro if (c == ',' && !open_quote) 258099dbbb59SAl Viro break; 25811da177e4SLinus Torvalds } 258299dbbb59SAl Viro return len; 25831da177e4SLinus Torvalds } 25841da177e4SLinus Torvalds 2585204cc0ccSAl Viro static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts) 25865b400239SAl Viro { 258799dbbb59SAl Viro char *from = options; 258899dbbb59SAl Viro char *to = options; 258999dbbb59SAl Viro bool first = true; 2590fec63753SGen Zhang int rc; 25915b400239SAl Viro 259299dbbb59SAl Viro while (1) { 259399dbbb59SAl Viro int len = opt_len(from); 2594fec63753SGen Zhang int token; 259599dbbb59SAl Viro char *arg = NULL; 259699dbbb59SAl Viro 259799dbbb59SAl Viro token = match_opt_prefix(from, len, &arg); 259899dbbb59SAl Viro 259999dbbb59SAl Viro if (token != Opt_error) { 260099dbbb59SAl Viro char *p, *q; 260199dbbb59SAl Viro 260299dbbb59SAl Viro /* strip quotes */ 260399dbbb59SAl Viro if (arg) { 260499dbbb59SAl Viro for (p = q = arg; p < from + len; p++) { 260599dbbb59SAl Viro char c = *p; 260699dbbb59SAl Viro if (c != '"') 260799dbbb59SAl Viro *q++ = c; 260899dbbb59SAl Viro } 260999dbbb59SAl Viro arg = kmemdup_nul(arg, q - arg, GFP_KERNEL); 2610fec63753SGen Zhang if (!arg) { 2611fec63753SGen Zhang rc = -ENOMEM; 2612fec63753SGen Zhang goto free_opt; 2613fec63753SGen Zhang } 261499dbbb59SAl Viro } 261599dbbb59SAl Viro rc = selinux_add_opt(token, arg, mnt_opts); 261699dbbb59SAl Viro if (unlikely(rc)) { 261799dbbb59SAl Viro kfree(arg); 2618fec63753SGen Zhang goto free_opt; 26191da177e4SLinus Torvalds } 262099dbbb59SAl Viro } else { 262199dbbb59SAl Viro if (!first) { // copy with preceding comma 262299dbbb59SAl Viro from--; 262399dbbb59SAl Viro len++; 262499dbbb59SAl Viro } 262599dbbb59SAl Viro if (to != from) 262699dbbb59SAl Viro memmove(to, from, len); 262799dbbb59SAl Viro to += len; 262899dbbb59SAl Viro first = false; 262999dbbb59SAl Viro } 263099dbbb59SAl Viro if (!from[len]) 263199dbbb59SAl Viro break; 263299dbbb59SAl Viro from += len + 1; 263399dbbb59SAl Viro } 263499dbbb59SAl Viro *to = '\0'; 263599dbbb59SAl Viro return 0; 2636fec63753SGen Zhang 2637fec63753SGen Zhang free_opt: 2638fec63753SGen Zhang if (*mnt_opts) { 2639fec63753SGen Zhang selinux_free_mnt_opts(*mnt_opts); 2640fec63753SGen Zhang *mnt_opts = NULL; 2641fec63753SGen Zhang } 2642fec63753SGen Zhang return rc; 26435b400239SAl Viro } 26441da177e4SLinus Torvalds 2645204cc0ccSAl Viro static int selinux_sb_remount(struct super_block *sb, void *mnt_opts) 2646026eb167SEric Paris { 2647bd323655SAl Viro struct selinux_mnt_opts *opts = mnt_opts; 2648026eb167SEric Paris struct superblock_security_struct *sbsec = sb->s_security; 2649bd323655SAl Viro u32 sid; 2650bd323655SAl Viro int rc; 2651026eb167SEric Paris 2652026eb167SEric Paris if (!(sbsec->flags & SE_SBINITIALIZED)) 2653026eb167SEric Paris return 0; 2654026eb167SEric Paris 2655204cc0ccSAl Viro if (!opts) 2656026eb167SEric Paris return 0; 2657026eb167SEric Paris 2658bd323655SAl Viro if (opts->fscontext) { 2659bd323655SAl Viro rc = parse_sid(sb, opts->fscontext, &sid); 2660026eb167SEric Paris if (rc) 2661c039bc3cSAl Viro return rc; 2662026eb167SEric Paris if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid)) 2663026eb167SEric Paris goto out_bad_option; 2664bd323655SAl Viro } 2665bd323655SAl Viro if (opts->context) { 2666bd323655SAl Viro rc = parse_sid(sb, opts->context, &sid); 2667bd323655SAl Viro if (rc) 2668bd323655SAl Viro return rc; 2669026eb167SEric Paris if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid)) 2670026eb167SEric Paris goto out_bad_option; 2671bd323655SAl Viro } 2672bd323655SAl Viro if (opts->rootcontext) { 2673026eb167SEric Paris struct inode_security_struct *root_isec; 267483da53c5SAndreas Gruenbacher root_isec = backing_inode_security(sb->s_root); 2675bd323655SAl Viro rc = parse_sid(sb, opts->rootcontext, &sid); 2676bd323655SAl Viro if (rc) 2677bd323655SAl Viro return rc; 2678026eb167SEric Paris if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid)) 2679026eb167SEric Paris goto out_bad_option; 2680026eb167SEric Paris } 2681bd323655SAl Viro if (opts->defcontext) { 2682bd323655SAl Viro rc = parse_sid(sb, opts->defcontext, &sid); 2683bd323655SAl Viro if (rc) 2684bd323655SAl Viro return rc; 2685026eb167SEric Paris if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid)) 2686026eb167SEric Paris goto out_bad_option; 2687026eb167SEric Paris } 2688c039bc3cSAl Viro return 0; 2689026eb167SEric Paris 2690026eb167SEric Paris out_bad_option: 2691c103a91eSpeter enderborg pr_warn("SELinux: unable to change security options " 269229b1deb2SLinus Torvalds "during remount (dev %s, type=%s)\n", sb->s_id, 269329b1deb2SLinus Torvalds sb->s_type->name); 2694c039bc3cSAl Viro return -EINVAL; 2695026eb167SEric Paris } 2696026eb167SEric Paris 2697a10d7c22SAl Viro static int selinux_sb_kern_mount(struct super_block *sb) 26981da177e4SLinus Torvalds { 269988e67f3bSDavid Howells const struct cred *cred = current_cred(); 27002bf49690SThomas Liu struct common_audit_data ad; 270174192246SJames Morris 270250c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 2703a269434dSEric Paris ad.u.dentry = sb->s_root; 270488e67f3bSDavid Howells return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad); 27051da177e4SLinus Torvalds } 27061da177e4SLinus Torvalds 2707726c3342SDavid Howells static int selinux_sb_statfs(struct dentry *dentry) 27081da177e4SLinus Torvalds { 270988e67f3bSDavid Howells const struct cred *cred = current_cred(); 27102bf49690SThomas Liu struct common_audit_data ad; 27111da177e4SLinus Torvalds 271250c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 2713a269434dSEric Paris ad.u.dentry = dentry->d_sb->s_root; 271488e67f3bSDavid Howells return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad); 27151da177e4SLinus Torvalds } 27161da177e4SLinus Torvalds 2717808d4e3cSAl Viro static int selinux_mount(const char *dev_name, 27188a04c43bSAl Viro const struct path *path, 2719808d4e3cSAl Viro const char *type, 27201da177e4SLinus Torvalds unsigned long flags, 27211da177e4SLinus Torvalds void *data) 27221da177e4SLinus Torvalds { 272388e67f3bSDavid Howells const struct cred *cred = current_cred(); 27241da177e4SLinus Torvalds 27251da177e4SLinus Torvalds if (flags & MS_REMOUNT) 2726d8c9584eSAl Viro return superblock_has_perm(cred, path->dentry->d_sb, 27271da177e4SLinus Torvalds FILESYSTEM__REMOUNT, NULL); 27281da177e4SLinus Torvalds else 27292875fa00SEric Paris return path_has_perm(cred, path, FILE__MOUNTON); 27301da177e4SLinus Torvalds } 27311da177e4SLinus Torvalds 273298aa0034SStephen Smalley static int selinux_move_mount(const struct path *from_path, 273398aa0034SStephen Smalley const struct path *to_path) 273498aa0034SStephen Smalley { 273598aa0034SStephen Smalley const struct cred *cred = current_cred(); 273698aa0034SStephen Smalley 273798aa0034SStephen Smalley return path_has_perm(cred, to_path, FILE__MOUNTON); 273898aa0034SStephen Smalley } 273998aa0034SStephen Smalley 27401da177e4SLinus Torvalds static int selinux_umount(struct vfsmount *mnt, int flags) 27411da177e4SLinus Torvalds { 274288e67f3bSDavid Howells const struct cred *cred = current_cred(); 27431da177e4SLinus Torvalds 274488e67f3bSDavid Howells return superblock_has_perm(cred, mnt->mnt_sb, 27451da177e4SLinus Torvalds FILESYSTEM__UNMOUNT, NULL); 27461da177e4SLinus Torvalds } 27471da177e4SLinus Torvalds 27480b52075eSAl Viro static int selinux_fs_context_dup(struct fs_context *fc, 27490b52075eSAl Viro struct fs_context *src_fc) 27500b52075eSAl Viro { 27510b52075eSAl Viro const struct selinux_mnt_opts *src = src_fc->security; 27520b52075eSAl Viro struct selinux_mnt_opts *opts; 27530b52075eSAl Viro 27540b52075eSAl Viro if (!src) 27550b52075eSAl Viro return 0; 27560b52075eSAl Viro 27570b52075eSAl Viro fc->security = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL); 27580b52075eSAl Viro if (!fc->security) 27590b52075eSAl Viro return -ENOMEM; 27600b52075eSAl Viro 27610b52075eSAl Viro opts = fc->security; 27620b52075eSAl Viro 27630b52075eSAl Viro if (src->fscontext) { 27640b52075eSAl Viro opts->fscontext = kstrdup(src->fscontext, GFP_KERNEL); 27650b52075eSAl Viro if (!opts->fscontext) 27660b52075eSAl Viro return -ENOMEM; 27670b52075eSAl Viro } 27680b52075eSAl Viro if (src->context) { 27690b52075eSAl Viro opts->context = kstrdup(src->context, GFP_KERNEL); 27700b52075eSAl Viro if (!opts->context) 27710b52075eSAl Viro return -ENOMEM; 27720b52075eSAl Viro } 27730b52075eSAl Viro if (src->rootcontext) { 27740b52075eSAl Viro opts->rootcontext = kstrdup(src->rootcontext, GFP_KERNEL); 27750b52075eSAl Viro if (!opts->rootcontext) 27760b52075eSAl Viro return -ENOMEM; 27770b52075eSAl Viro } 27780b52075eSAl Viro if (src->defcontext) { 27790b52075eSAl Viro opts->defcontext = kstrdup(src->defcontext, GFP_KERNEL); 27800b52075eSAl Viro if (!opts->defcontext) 27810b52075eSAl Viro return -ENOMEM; 27820b52075eSAl Viro } 27830b52075eSAl Viro return 0; 27840b52075eSAl Viro } 27850b52075eSAl Viro 2786d7167b14SAl Viro static const struct fs_parameter_spec selinux_fs_parameters[] = { 2787442155c1SDavid Howells fsparam_string(CONTEXT_STR, Opt_context), 2788442155c1SDavid Howells fsparam_string(DEFCONTEXT_STR, Opt_defcontext), 2789442155c1SDavid Howells fsparam_string(FSCONTEXT_STR, Opt_fscontext), 2790442155c1SDavid Howells fsparam_string(ROOTCONTEXT_STR, Opt_rootcontext), 2791442155c1SDavid Howells fsparam_flag (SECLABEL_STR, Opt_seclabel), 2792442155c1SDavid Howells {} 2793442155c1SDavid Howells }; 2794442155c1SDavid Howells 2795442155c1SDavid Howells static int selinux_fs_context_parse_param(struct fs_context *fc, 2796442155c1SDavid Howells struct fs_parameter *param) 2797442155c1SDavid Howells { 2798442155c1SDavid Howells struct fs_parse_result result; 2799442155c1SDavid Howells int opt, rc; 2800442155c1SDavid Howells 2801d7167b14SAl Viro opt = fs_parse(fc, selinux_fs_parameters, param, &result); 2802442155c1SDavid Howells if (opt < 0) 2803442155c1SDavid Howells return opt; 2804442155c1SDavid Howells 2805442155c1SDavid Howells rc = selinux_add_opt(opt, param->string, &fc->security); 2806442155c1SDavid Howells if (!rc) { 2807442155c1SDavid Howells param->string = NULL; 2808442155c1SDavid Howells rc = 1; 2809442155c1SDavid Howells } 2810442155c1SDavid Howells return rc; 2811442155c1SDavid Howells } 2812442155c1SDavid Howells 28131da177e4SLinus Torvalds /* inode security operations */ 28141da177e4SLinus Torvalds 28151da177e4SLinus Torvalds static int selinux_inode_alloc_security(struct inode *inode) 28161da177e4SLinus Torvalds { 2817cb89e246SPaul Moore struct inode_security_struct *isec = selinux_inode(inode); 2818cb89e246SPaul Moore u32 sid = current_sid(); 2819cb89e246SPaul Moore 2820cb89e246SPaul Moore spin_lock_init(&isec->lock); 2821cb89e246SPaul Moore INIT_LIST_HEAD(&isec->list); 2822cb89e246SPaul Moore isec->inode = inode; 2823cb89e246SPaul Moore isec->sid = SECINITSID_UNLABELED; 2824cb89e246SPaul Moore isec->sclass = SECCLASS_FILE; 2825cb89e246SPaul Moore isec->task_sid = sid; 2826cb89e246SPaul Moore isec->initialized = LABEL_INVALID; 2827cb89e246SPaul Moore 2828cb89e246SPaul Moore return 0; 28291da177e4SLinus Torvalds } 28301da177e4SLinus Torvalds 28311da177e4SLinus Torvalds static void selinux_inode_free_security(struct inode *inode) 28321da177e4SLinus Torvalds { 28331da177e4SLinus Torvalds inode_free_security(inode); 28341da177e4SLinus Torvalds } 28351da177e4SLinus Torvalds 2836d47be3dfSDavid Quigley static int selinux_dentry_init_security(struct dentry *dentry, int mode, 28374f3ccd76SAl Viro const struct qstr *name, void **ctx, 2838d47be3dfSDavid Quigley u32 *ctxlen) 2839d47be3dfSDavid Quigley { 2840d47be3dfSDavid Quigley u32 newsid; 2841d47be3dfSDavid Quigley int rc; 2842d47be3dfSDavid Quigley 28430c6cfa62SCasey Schaufler rc = selinux_determine_inode_label(selinux_cred(current_cred()), 2844c957f6dfSVivek Goyal d_inode(dentry->d_parent), name, 2845d47be3dfSDavid Quigley inode_mode_to_security_class(mode), 2846d47be3dfSDavid Quigley &newsid); 2847c3c188b2SDavid Howells if (rc) 2848d47be3dfSDavid Quigley return rc; 2849d47be3dfSDavid Quigley 2850aa8e712cSStephen Smalley return security_sid_to_context(&selinux_state, newsid, (char **)ctx, 2851aa8e712cSStephen Smalley ctxlen); 2852d47be3dfSDavid Quigley } 2853d47be3dfSDavid Quigley 2854a518b0a5SVivek Goyal static int selinux_dentry_create_files_as(struct dentry *dentry, int mode, 2855a518b0a5SVivek Goyal struct qstr *name, 2856a518b0a5SVivek Goyal const struct cred *old, 2857a518b0a5SVivek Goyal struct cred *new) 2858a518b0a5SVivek Goyal { 2859a518b0a5SVivek Goyal u32 newsid; 2860a518b0a5SVivek Goyal int rc; 2861a518b0a5SVivek Goyal struct task_security_struct *tsec; 2862a518b0a5SVivek Goyal 28630c6cfa62SCasey Schaufler rc = selinux_determine_inode_label(selinux_cred(old), 2864a518b0a5SVivek Goyal d_inode(dentry->d_parent), name, 2865a518b0a5SVivek Goyal inode_mode_to_security_class(mode), 2866a518b0a5SVivek Goyal &newsid); 2867a518b0a5SVivek Goyal if (rc) 2868a518b0a5SVivek Goyal return rc; 2869a518b0a5SVivek Goyal 28700c6cfa62SCasey Schaufler tsec = selinux_cred(new); 2871a518b0a5SVivek Goyal tsec->create_sid = newsid; 2872a518b0a5SVivek Goyal return 0; 2873a518b0a5SVivek Goyal } 2874a518b0a5SVivek Goyal 28755e41ff9eSStephen Smalley static int selinux_inode_init_security(struct inode *inode, struct inode *dir, 28769548906bSTetsuo Handa const struct qstr *qstr, 28779548906bSTetsuo Handa const char **name, 28782a7dba39SEric Paris void **value, size_t *len) 28795e41ff9eSStephen Smalley { 28800c6cfa62SCasey Schaufler const struct task_security_struct *tsec = selinux_cred(current_cred()); 28815e41ff9eSStephen Smalley struct superblock_security_struct *sbsec; 2882c0d4f464SCorentin LABBE u32 newsid, clen; 28835e41ff9eSStephen Smalley int rc; 28849548906bSTetsuo Handa char *context; 28855e41ff9eSStephen Smalley 28865e41ff9eSStephen Smalley sbsec = dir->i_sb->s_security; 28875e41ff9eSStephen Smalley 28885e41ff9eSStephen Smalley newsid = tsec->create_sid; 2889275bb41eSDavid Howells 2890210a2928SYang Guo rc = selinux_determine_inode_label(tsec, dir, qstr, 28915e41ff9eSStephen Smalley inode_mode_to_security_class(inode->i_mode), 2892c3c188b2SDavid Howells &newsid); 2893c3c188b2SDavid Howells if (rc) 28945e41ff9eSStephen Smalley return rc; 28955e41ff9eSStephen Smalley 2896296fddf7SEric Paris /* Possibly defer initialization to selinux_complete_init. */ 28970d90a7ecSDavid P. Quigley if (sbsec->flags & SE_SBINITIALIZED) { 289880788c22SCasey Schaufler struct inode_security_struct *isec = selinux_inode(inode); 2899296fddf7SEric Paris isec->sclass = inode_mode_to_security_class(inode->i_mode); 2900296fddf7SEric Paris isec->sid = newsid; 29016f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 2902296fddf7SEric Paris } 29035e41ff9eSStephen Smalley 290465cddd50SOndrej Mosnacek if (!selinux_initialized(&selinux_state) || 290565cddd50SOndrej Mosnacek !(sbsec->flags & SBLABEL_MNT)) 290625a74f3bSStephen Smalley return -EOPNOTSUPP; 290725a74f3bSStephen Smalley 29089548906bSTetsuo Handa if (name) 29099548906bSTetsuo Handa *name = XATTR_SELINUX_SUFFIX; 29105e41ff9eSStephen Smalley 2911570bc1c2SStephen Smalley if (value && len) { 2912aa8e712cSStephen Smalley rc = security_sid_to_context_force(&selinux_state, newsid, 2913aa8e712cSStephen Smalley &context, &clen); 29149548906bSTetsuo Handa if (rc) 29155e41ff9eSStephen Smalley return rc; 29165e41ff9eSStephen Smalley *value = context; 2917570bc1c2SStephen Smalley *len = clen; 2918570bc1c2SStephen Smalley } 29195e41ff9eSStephen Smalley 29205e41ff9eSStephen Smalley return 0; 29215e41ff9eSStephen Smalley } 29225e41ff9eSStephen Smalley 29234acdaf27SAl Viro static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode) 29241da177e4SLinus Torvalds { 29251da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_FILE); 29261da177e4SLinus Torvalds } 29271da177e4SLinus Torvalds 29281da177e4SLinus Torvalds static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 29291da177e4SLinus Torvalds { 29301da177e4SLinus Torvalds return may_link(dir, old_dentry, MAY_LINK); 29311da177e4SLinus Torvalds } 29321da177e4SLinus Torvalds 29331da177e4SLinus Torvalds static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry) 29341da177e4SLinus Torvalds { 29351da177e4SLinus Torvalds return may_link(dir, dentry, MAY_UNLINK); 29361da177e4SLinus Torvalds } 29371da177e4SLinus Torvalds 29381da177e4SLinus Torvalds static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name) 29391da177e4SLinus Torvalds { 29401da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_LNK_FILE); 29411da177e4SLinus Torvalds } 29421da177e4SLinus Torvalds 294318bb1db3SAl Viro static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask) 29441da177e4SLinus Torvalds { 29451da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_DIR); 29461da177e4SLinus Torvalds } 29471da177e4SLinus Torvalds 29481da177e4SLinus Torvalds static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry) 29491da177e4SLinus Torvalds { 29501da177e4SLinus Torvalds return may_link(dir, dentry, MAY_RMDIR); 29511da177e4SLinus Torvalds } 29521da177e4SLinus Torvalds 29531a67aafbSAl Viro static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) 29541da177e4SLinus Torvalds { 29551da177e4SLinus Torvalds return may_create(dir, dentry, inode_mode_to_security_class(mode)); 29561da177e4SLinus Torvalds } 29571da177e4SLinus Torvalds 29581da177e4SLinus Torvalds static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry, 29591da177e4SLinus Torvalds struct inode *new_inode, struct dentry *new_dentry) 29601da177e4SLinus Torvalds { 29611da177e4SLinus Torvalds return may_rename(old_inode, old_dentry, new_inode, new_dentry); 29621da177e4SLinus Torvalds } 29631da177e4SLinus Torvalds 29641da177e4SLinus Torvalds static int selinux_inode_readlink(struct dentry *dentry) 29651da177e4SLinus Torvalds { 296688e67f3bSDavid Howells const struct cred *cred = current_cred(); 296788e67f3bSDavid Howells 29682875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__READ); 29691da177e4SLinus Torvalds } 29701da177e4SLinus Torvalds 2971bda0be7aSNeilBrown static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode, 2972bda0be7aSNeilBrown bool rcu) 29731da177e4SLinus Torvalds { 297488e67f3bSDavid Howells const struct cred *cred = current_cred(); 2975bda0be7aSNeilBrown struct common_audit_data ad; 2976bda0be7aSNeilBrown struct inode_security_struct *isec; 2977bda0be7aSNeilBrown u32 sid; 29781da177e4SLinus Torvalds 2979bda0be7aSNeilBrown validate_creds(cred); 2980bda0be7aSNeilBrown 2981bda0be7aSNeilBrown ad.type = LSM_AUDIT_DATA_DENTRY; 2982bda0be7aSNeilBrown ad.u.dentry = dentry; 2983bda0be7aSNeilBrown sid = cred_sid(cred); 29845d226df4SAndreas Gruenbacher isec = inode_security_rcu(inode, rcu); 29855d226df4SAndreas Gruenbacher if (IS_ERR(isec)) 29865d226df4SAndreas Gruenbacher return PTR_ERR(isec); 2987bda0be7aSNeilBrown 29881a37079cSStephen Smalley return avc_has_perm_flags(&selinux_state, 29891a37079cSStephen Smalley sid, isec->sid, isec->sclass, FILE__READ, &ad, 29901a37079cSStephen Smalley rcu ? MAY_NOT_BLOCK : 0); 29911da177e4SLinus Torvalds } 29921da177e4SLinus Torvalds 2993d4cf970dSEric Paris static noinline int audit_inode_permission(struct inode *inode, 2994d4cf970dSEric Paris u32 perms, u32 audited, u32 denied, 29950188d5c0SStephen Smalley int result) 2996d4cf970dSEric Paris { 2997d4cf970dSEric Paris struct common_audit_data ad; 299880788c22SCasey Schaufler struct inode_security_struct *isec = selinux_inode(inode); 2999d4cf970dSEric Paris int rc; 3000d4cf970dSEric Paris 300150c205f5SEric Paris ad.type = LSM_AUDIT_DATA_INODE; 3002d4cf970dSEric Paris ad.u.inode = inode; 3003d4cf970dSEric Paris 30046b6bc620SStephen Smalley rc = slow_avc_audit(&selinux_state, 30056b6bc620SStephen Smalley current_sid(), isec->sid, isec->sclass, perms, 30060188d5c0SStephen Smalley audited, denied, result, &ad); 3007d4cf970dSEric Paris if (rc) 3008d4cf970dSEric Paris return rc; 3009d4cf970dSEric Paris return 0; 3010d4cf970dSEric Paris } 3011d4cf970dSEric Paris 3012e74f71ebSAl Viro static int selinux_inode_permission(struct inode *inode, int mask) 30131da177e4SLinus Torvalds { 301488e67f3bSDavid Howells const struct cred *cred = current_cred(); 3015b782e0a6SEric Paris u32 perms; 3016b782e0a6SEric Paris bool from_access; 30175298d0b9SStephen Smalley bool no_block = mask & MAY_NOT_BLOCK; 30182e334057SEric Paris struct inode_security_struct *isec; 30192e334057SEric Paris u32 sid; 30202e334057SEric Paris struct av_decision avd; 30212e334057SEric Paris int rc, rc2; 30222e334057SEric Paris u32 audited, denied; 30231da177e4SLinus Torvalds 3024b782e0a6SEric Paris from_access = mask & MAY_ACCESS; 3025d09ca739SEric Paris mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND); 3026d09ca739SEric Paris 30271da177e4SLinus Torvalds /* No permission to check. Existence test. */ 3028b782e0a6SEric Paris if (!mask) 30291da177e4SLinus Torvalds return 0; 30301da177e4SLinus Torvalds 30312e334057SEric Paris validate_creds(cred); 3032b782e0a6SEric Paris 30332e334057SEric Paris if (unlikely(IS_PRIVATE(inode))) 30342e334057SEric Paris return 0; 3035b782e0a6SEric Paris 3036b782e0a6SEric Paris perms = file_mask_to_av(inode->i_mode, mask); 3037b782e0a6SEric Paris 30382e334057SEric Paris sid = cred_sid(cred); 30395298d0b9SStephen Smalley isec = inode_security_rcu(inode, no_block); 30405d226df4SAndreas Gruenbacher if (IS_ERR(isec)) 30415d226df4SAndreas Gruenbacher return PTR_ERR(isec); 30422e334057SEric Paris 30436b6bc620SStephen Smalley rc = avc_has_perm_noaudit(&selinux_state, 30443a28cff3SStephen Smalley sid, isec->sid, isec->sclass, perms, 30455298d0b9SStephen Smalley no_block ? AVC_NONBLOCKING : 0, 30463a28cff3SStephen Smalley &avd); 30472e334057SEric Paris audited = avc_audit_required(perms, &avd, rc, 30482e334057SEric Paris from_access ? FILE__AUDIT_ACCESS : 0, 30492e334057SEric Paris &denied); 30502e334057SEric Paris if (likely(!audited)) 30512e334057SEric Paris return rc; 30522e334057SEric Paris 30530188d5c0SStephen Smalley /* fall back to ref-walk if we have to generate audit */ 30545298d0b9SStephen Smalley if (no_block) 30550188d5c0SStephen Smalley return -ECHILD; 30560188d5c0SStephen Smalley 30570188d5c0SStephen Smalley rc2 = audit_inode_permission(inode, perms, audited, denied, rc); 30582e334057SEric Paris if (rc2) 30592e334057SEric Paris return rc2; 30602e334057SEric Paris return rc; 30611da177e4SLinus Torvalds } 30621da177e4SLinus Torvalds 30631da177e4SLinus Torvalds static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr) 30641da177e4SLinus Torvalds { 306588e67f3bSDavid Howells const struct cred *cred = current_cred(); 3066ccb54478SStephen Smalley struct inode *inode = d_backing_inode(dentry); 3067bc6a6008SAmerigo Wang unsigned int ia_valid = iattr->ia_valid; 306895dbf739SEric Paris __u32 av = FILE__WRITE; 30691da177e4SLinus Torvalds 3070bc6a6008SAmerigo Wang /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */ 3071bc6a6008SAmerigo Wang if (ia_valid & ATTR_FORCE) { 3072bc6a6008SAmerigo Wang ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE | 3073bc6a6008SAmerigo Wang ATTR_FORCE); 3074bc6a6008SAmerigo Wang if (!ia_valid) 30751da177e4SLinus Torvalds return 0; 3076bc6a6008SAmerigo Wang } 30771da177e4SLinus Torvalds 3078bc6a6008SAmerigo Wang if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | 3079bc6a6008SAmerigo Wang ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET)) 30802875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__SETATTR); 30811da177e4SLinus Torvalds 3082aa8e712cSStephen Smalley if (selinux_policycap_openperm() && 3083ccb54478SStephen Smalley inode->i_sb->s_magic != SOCKFS_MAGIC && 3084ccb54478SStephen Smalley (ia_valid & ATTR_SIZE) && 3085ccb54478SStephen Smalley !(ia_valid & ATTR_FILE)) 308695dbf739SEric Paris av |= FILE__OPEN; 308795dbf739SEric Paris 308895dbf739SEric Paris return dentry_has_perm(cred, dentry, av); 30891da177e4SLinus Torvalds } 30901da177e4SLinus Torvalds 30913f7036a0SAl Viro static int selinux_inode_getattr(const struct path *path) 30921da177e4SLinus Torvalds { 30933f7036a0SAl Viro return path_has_perm(current_cred(), path, FILE__GETATTR); 30941da177e4SLinus Torvalds } 30951da177e4SLinus Torvalds 3096db59000aSStephen Smalley static bool has_cap_mac_admin(bool audit) 3097db59000aSStephen Smalley { 3098db59000aSStephen Smalley const struct cred *cred = current_cred(); 3099c1a85a00SMicah Morton unsigned int opts = audit ? CAP_OPT_NONE : CAP_OPT_NOAUDIT; 3100db59000aSStephen Smalley 3101c1a85a00SMicah Morton if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, opts)) 3102db59000aSStephen Smalley return false; 3103c1a85a00SMicah Morton if (cred_has_capability(cred, CAP_MAC_ADMIN, opts, true)) 3104db59000aSStephen Smalley return false; 3105db59000aSStephen Smalley return true; 3106db59000aSStephen Smalley } 3107db59000aSStephen Smalley 31088f0cfa52SDavid Howells static int selinux_inode_setxattr(struct dentry *dentry, const char *name, 31098f0cfa52SDavid Howells const void *value, size_t size, int flags) 31101da177e4SLinus Torvalds { 3111c6f493d6SDavid Howells struct inode *inode = d_backing_inode(dentry); 311220cdef8dSPaul Moore struct inode_security_struct *isec; 31131da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 31142bf49690SThomas Liu struct common_audit_data ad; 3115275bb41eSDavid Howells u32 newsid, sid = current_sid(); 31161da177e4SLinus Torvalds int rc = 0; 31171da177e4SLinus Torvalds 31186b240306SEric W. Biederman if (strcmp(name, XATTR_NAME_SELINUX)) { 31196b240306SEric W. Biederman rc = cap_inode_setxattr(dentry, name, value, size, flags); 31206b240306SEric W. Biederman if (rc) 31216b240306SEric W. Biederman return rc; 31226b240306SEric W. Biederman 31236b240306SEric W. Biederman /* Not an attribute we recognize, so just check the 31246b240306SEric W. Biederman ordinary setattr permission. */ 31256b240306SEric W. Biederman return dentry_has_perm(current_cred(), dentry, FILE__SETATTR); 31266b240306SEric W. Biederman } 31271da177e4SLinus Torvalds 312865cddd50SOndrej Mosnacek if (!selinux_initialized(&selinux_state)) 31293e3e24b4SJonathan Lebon return (inode_owner_or_capable(inode) ? 0 : -EPERM); 31303e3e24b4SJonathan Lebon 31311da177e4SLinus Torvalds sbsec = inode->i_sb->s_security; 313212f348b9SEric Paris if (!(sbsec->flags & SBLABEL_MNT)) 31331da177e4SLinus Torvalds return -EOPNOTSUPP; 31341da177e4SLinus Torvalds 31352e149670SSerge E. Hallyn if (!inode_owner_or_capable(inode)) 31361da177e4SLinus Torvalds return -EPERM; 31371da177e4SLinus Torvalds 313850c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 3139a269434dSEric Paris ad.u.dentry = dentry; 31401da177e4SLinus Torvalds 314120cdef8dSPaul Moore isec = backing_inode_security(dentry); 31426b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 31436b6bc620SStephen Smalley sid, isec->sid, isec->sclass, 31441da177e4SLinus Torvalds FILE__RELABELFROM, &ad); 31451da177e4SLinus Torvalds if (rc) 31461da177e4SLinus Torvalds return rc; 31471da177e4SLinus Torvalds 3148aa8e712cSStephen Smalley rc = security_context_to_sid(&selinux_state, value, size, &newsid, 3149aa8e712cSStephen Smalley GFP_KERNEL); 315012b29f34SStephen Smalley if (rc == -EINVAL) { 3151db59000aSStephen Smalley if (!has_cap_mac_admin(true)) { 3152d6ea83ecSEric Paris struct audit_buffer *ab; 3153d6ea83ecSEric Paris size_t audit_size; 3154d6ea83ecSEric Paris 3155d6ea83ecSEric Paris /* We strip a nul only if it is at the end, otherwise the 3156d6ea83ecSEric Paris * context contains a nul and we should audit that */ 3157e3fea3f7SAl Viro if (value) { 3158add24372SColin Ian King const char *str = value; 3159add24372SColin Ian King 3160d6ea83ecSEric Paris if (str[size - 1] == '\0') 3161d6ea83ecSEric Paris audit_size = size - 1; 3162d6ea83ecSEric Paris else 3163d6ea83ecSEric Paris audit_size = size; 3164e3fea3f7SAl Viro } else { 3165e3fea3f7SAl Viro audit_size = 0; 3166e3fea3f7SAl Viro } 3167cdfb6b34SRichard Guy Briggs ab = audit_log_start(audit_context(), 3168cdfb6b34SRichard Guy Briggs GFP_ATOMIC, AUDIT_SELINUX_ERR); 3169d6ea83ecSEric Paris audit_log_format(ab, "op=setxattr invalid_context="); 3170d6ea83ecSEric Paris audit_log_n_untrustedstring(ab, value, audit_size); 3171d6ea83ecSEric Paris audit_log_end(ab); 3172d6ea83ecSEric Paris 317312b29f34SStephen Smalley return rc; 3174d6ea83ecSEric Paris } 3175aa8e712cSStephen Smalley rc = security_context_to_sid_force(&selinux_state, value, 3176aa8e712cSStephen Smalley size, &newsid); 317712b29f34SStephen Smalley } 31781da177e4SLinus Torvalds if (rc) 31791da177e4SLinus Torvalds return rc; 31801da177e4SLinus Torvalds 31816b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 31826b6bc620SStephen Smalley sid, newsid, isec->sclass, 31831da177e4SLinus Torvalds FILE__RELABELTO, &ad); 31841da177e4SLinus Torvalds if (rc) 31851da177e4SLinus Torvalds return rc; 31861da177e4SLinus Torvalds 3187aa8e712cSStephen Smalley rc = security_validate_transition(&selinux_state, isec->sid, newsid, 3188aa8e712cSStephen Smalley sid, isec->sclass); 31891da177e4SLinus Torvalds if (rc) 31901da177e4SLinus Torvalds return rc; 31911da177e4SLinus Torvalds 31926b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 31936b6bc620SStephen Smalley newsid, 31941da177e4SLinus Torvalds sbsec->sid, 31951da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 31961da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, 31971da177e4SLinus Torvalds &ad); 31981da177e4SLinus Torvalds } 31991da177e4SLinus Torvalds 32008f0cfa52SDavid Howells static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name, 32018f0cfa52SDavid Howells const void *value, size_t size, 32028f0cfa52SDavid Howells int flags) 32031da177e4SLinus Torvalds { 3204c6f493d6SDavid Howells struct inode *inode = d_backing_inode(dentry); 320520cdef8dSPaul Moore struct inode_security_struct *isec; 32061da177e4SLinus Torvalds u32 newsid; 32071da177e4SLinus Torvalds int rc; 32081da177e4SLinus Torvalds 32091da177e4SLinus Torvalds if (strcmp(name, XATTR_NAME_SELINUX)) { 32101da177e4SLinus Torvalds /* Not an attribute we recognize, so nothing to do. */ 32111da177e4SLinus Torvalds return; 32121da177e4SLinus Torvalds } 32131da177e4SLinus Torvalds 321465cddd50SOndrej Mosnacek if (!selinux_initialized(&selinux_state)) { 32153e3e24b4SJonathan Lebon /* If we haven't even been initialized, then we can't validate 32163e3e24b4SJonathan Lebon * against a policy, so leave the label as invalid. It may 32173e3e24b4SJonathan Lebon * resolve to a valid label on the next revalidation try if 32183e3e24b4SJonathan Lebon * we've since initialized. 32193e3e24b4SJonathan Lebon */ 32203e3e24b4SJonathan Lebon return; 32213e3e24b4SJonathan Lebon } 32223e3e24b4SJonathan Lebon 3223aa8e712cSStephen Smalley rc = security_context_to_sid_force(&selinux_state, value, size, 3224aa8e712cSStephen Smalley &newsid); 32251da177e4SLinus Torvalds if (rc) { 3226c103a91eSpeter enderborg pr_err("SELinux: unable to map context to SID" 322712b29f34SStephen Smalley "for (%s, %lu), rc=%d\n", 322812b29f34SStephen Smalley inode->i_sb->s_id, inode->i_ino, -rc); 32291da177e4SLinus Torvalds return; 32301da177e4SLinus Torvalds } 32311da177e4SLinus Torvalds 323220cdef8dSPaul Moore isec = backing_inode_security(dentry); 32339287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 3234aa9c2669SDavid Quigley isec->sclass = inode_mode_to_security_class(inode->i_mode); 32351da177e4SLinus Torvalds isec->sid = newsid; 32366f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 32379287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 3238aa9c2669SDavid Quigley 32391da177e4SLinus Torvalds return; 32401da177e4SLinus Torvalds } 32411da177e4SLinus Torvalds 32428f0cfa52SDavid Howells static int selinux_inode_getxattr(struct dentry *dentry, const char *name) 32431da177e4SLinus Torvalds { 324488e67f3bSDavid Howells const struct cred *cred = current_cred(); 324588e67f3bSDavid Howells 32462875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__GETATTR); 32471da177e4SLinus Torvalds } 32481da177e4SLinus Torvalds 32491da177e4SLinus Torvalds static int selinux_inode_listxattr(struct dentry *dentry) 32501da177e4SLinus Torvalds { 325188e67f3bSDavid Howells const struct cred *cred = current_cred(); 325288e67f3bSDavid Howells 32532875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__GETATTR); 32541da177e4SLinus Torvalds } 32551da177e4SLinus Torvalds 32568f0cfa52SDavid Howells static int selinux_inode_removexattr(struct dentry *dentry, const char *name) 32571da177e4SLinus Torvalds { 32586b240306SEric W. Biederman if (strcmp(name, XATTR_NAME_SELINUX)) { 32596b240306SEric W. Biederman int rc = cap_inode_removexattr(dentry, name); 32606b240306SEric W. Biederman if (rc) 32616b240306SEric W. Biederman return rc; 32626b240306SEric W. Biederman 32636b240306SEric W. Biederman /* Not an attribute we recognize, so just check the 32646b240306SEric W. Biederman ordinary setattr permission. */ 32656b240306SEric W. Biederman return dentry_has_perm(current_cred(), dentry, FILE__SETATTR); 32666b240306SEric W. Biederman } 32671da177e4SLinus Torvalds 32681da177e4SLinus Torvalds /* No one is allowed to remove a SELinux security label. 32691da177e4SLinus Torvalds You can change the label, but all data must be labeled. */ 32701da177e4SLinus Torvalds return -EACCES; 32711da177e4SLinus Torvalds } 32721da177e4SLinus Torvalds 3273ac5656d8SAaron Goidel static int selinux_path_notify(const struct path *path, u64 mask, 3274ac5656d8SAaron Goidel unsigned int obj_type) 3275ac5656d8SAaron Goidel { 3276ac5656d8SAaron Goidel int ret; 3277ac5656d8SAaron Goidel u32 perm; 3278ac5656d8SAaron Goidel 3279ac5656d8SAaron Goidel struct common_audit_data ad; 3280ac5656d8SAaron Goidel 3281ac5656d8SAaron Goidel ad.type = LSM_AUDIT_DATA_PATH; 3282ac5656d8SAaron Goidel ad.u.path = *path; 3283ac5656d8SAaron Goidel 3284ac5656d8SAaron Goidel /* 3285ac5656d8SAaron Goidel * Set permission needed based on the type of mark being set. 3286ac5656d8SAaron Goidel * Performs an additional check for sb watches. 3287ac5656d8SAaron Goidel */ 3288ac5656d8SAaron Goidel switch (obj_type) { 3289ac5656d8SAaron Goidel case FSNOTIFY_OBJ_TYPE_VFSMOUNT: 3290ac5656d8SAaron Goidel perm = FILE__WATCH_MOUNT; 3291ac5656d8SAaron Goidel break; 3292ac5656d8SAaron Goidel case FSNOTIFY_OBJ_TYPE_SB: 3293ac5656d8SAaron Goidel perm = FILE__WATCH_SB; 3294ac5656d8SAaron Goidel ret = superblock_has_perm(current_cred(), path->dentry->d_sb, 3295ac5656d8SAaron Goidel FILESYSTEM__WATCH, &ad); 3296ac5656d8SAaron Goidel if (ret) 3297ac5656d8SAaron Goidel return ret; 3298ac5656d8SAaron Goidel break; 3299ac5656d8SAaron Goidel case FSNOTIFY_OBJ_TYPE_INODE: 3300ac5656d8SAaron Goidel perm = FILE__WATCH; 3301ac5656d8SAaron Goidel break; 3302ac5656d8SAaron Goidel default: 3303ac5656d8SAaron Goidel return -EINVAL; 3304ac5656d8SAaron Goidel } 3305ac5656d8SAaron Goidel 3306ac5656d8SAaron Goidel /* blocking watches require the file:watch_with_perm permission */ 3307ac5656d8SAaron Goidel if (mask & (ALL_FSNOTIFY_PERM_EVENTS)) 3308ac5656d8SAaron Goidel perm |= FILE__WATCH_WITH_PERM; 3309ac5656d8SAaron Goidel 3310ac5656d8SAaron Goidel /* watches on read-like events need the file:watch_reads permission */ 3311ac5656d8SAaron Goidel if (mask & (FS_ACCESS | FS_ACCESS_PERM | FS_CLOSE_NOWRITE)) 3312ac5656d8SAaron Goidel perm |= FILE__WATCH_READS; 3313ac5656d8SAaron Goidel 3314ac5656d8SAaron Goidel return path_has_perm(current_cred(), path, perm); 3315ac5656d8SAaron Goidel } 3316ac5656d8SAaron Goidel 3317d381d8a9SJames Morris /* 3318abc69bb6SStephen Smalley * Copy the inode security context value to the user. 3319d381d8a9SJames Morris * 3320d381d8a9SJames Morris * Permission check is handled by selinux_inode_getxattr hook. 3321d381d8a9SJames Morris */ 3322ea861dfdSAndreas Gruenbacher static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc) 33231da177e4SLinus Torvalds { 332442492594SDavid P. Quigley u32 size; 332542492594SDavid P. Quigley int error; 332642492594SDavid P. Quigley char *context = NULL; 332720cdef8dSPaul Moore struct inode_security_struct *isec; 33281da177e4SLinus Torvalds 33298c8570fbSDustin Kirkland if (strcmp(name, XATTR_SELINUX_SUFFIX)) 33308c8570fbSDustin Kirkland return -EOPNOTSUPP; 33311da177e4SLinus Torvalds 3332abc69bb6SStephen Smalley /* 3333abc69bb6SStephen Smalley * If the caller has CAP_MAC_ADMIN, then get the raw context 3334abc69bb6SStephen Smalley * value even if it is not defined by current policy; otherwise, 3335abc69bb6SStephen Smalley * use the in-core value under current policy. 3336abc69bb6SStephen Smalley * Use the non-auditing forms of the permission checks since 3337abc69bb6SStephen Smalley * getxattr may be called by unprivileged processes commonly 3338abc69bb6SStephen Smalley * and lack of permission just means that we fall back to the 3339abc69bb6SStephen Smalley * in-core context value, not a denial. 3340abc69bb6SStephen Smalley */ 334120cdef8dSPaul Moore isec = inode_security(inode); 3342db59000aSStephen Smalley if (has_cap_mac_admin(false)) 3343aa8e712cSStephen Smalley error = security_sid_to_context_force(&selinux_state, 3344aa8e712cSStephen Smalley isec->sid, &context, 3345abc69bb6SStephen Smalley &size); 3346abc69bb6SStephen Smalley else 3347aa8e712cSStephen Smalley error = security_sid_to_context(&selinux_state, isec->sid, 3348aa8e712cSStephen Smalley &context, &size); 334942492594SDavid P. Quigley if (error) 335042492594SDavid P. Quigley return error; 335142492594SDavid P. Quigley error = size; 335242492594SDavid P. Quigley if (alloc) { 335342492594SDavid P. Quigley *buffer = context; 335442492594SDavid P. Quigley goto out_nofree; 335542492594SDavid P. Quigley } 335642492594SDavid P. Quigley kfree(context); 335742492594SDavid P. Quigley out_nofree: 335842492594SDavid P. Quigley return error; 33591da177e4SLinus Torvalds } 33601da177e4SLinus Torvalds 33611da177e4SLinus Torvalds static int selinux_inode_setsecurity(struct inode *inode, const char *name, 33621da177e4SLinus Torvalds const void *value, size_t size, int flags) 33631da177e4SLinus Torvalds { 33642c97165bSPaul Moore struct inode_security_struct *isec = inode_security_novalidate(inode); 336553e0c2aaSOndrej Mosnacek struct superblock_security_struct *sbsec = inode->i_sb->s_security; 33661da177e4SLinus Torvalds u32 newsid; 33671da177e4SLinus Torvalds int rc; 33681da177e4SLinus Torvalds 33691da177e4SLinus Torvalds if (strcmp(name, XATTR_SELINUX_SUFFIX)) 33701da177e4SLinus Torvalds return -EOPNOTSUPP; 33711da177e4SLinus Torvalds 337253e0c2aaSOndrej Mosnacek if (!(sbsec->flags & SBLABEL_MNT)) 337353e0c2aaSOndrej Mosnacek return -EOPNOTSUPP; 337453e0c2aaSOndrej Mosnacek 33751da177e4SLinus Torvalds if (!value || !size) 33761da177e4SLinus Torvalds return -EACCES; 33771da177e4SLinus Torvalds 3378aa8e712cSStephen Smalley rc = security_context_to_sid(&selinux_state, value, size, &newsid, 3379aa8e712cSStephen Smalley GFP_KERNEL); 33801da177e4SLinus Torvalds if (rc) 33811da177e4SLinus Torvalds return rc; 33821da177e4SLinus Torvalds 33839287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 3384aa9c2669SDavid Quigley isec->sclass = inode_mode_to_security_class(inode->i_mode); 33851da177e4SLinus Torvalds isec->sid = newsid; 33866f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 33879287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 33881da177e4SLinus Torvalds return 0; 33891da177e4SLinus Torvalds } 33901da177e4SLinus Torvalds 33911da177e4SLinus Torvalds static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size) 33921da177e4SLinus Torvalds { 33931da177e4SLinus Torvalds const int len = sizeof(XATTR_NAME_SELINUX); 33941da177e4SLinus Torvalds if (buffer && len <= buffer_size) 33951da177e4SLinus Torvalds memcpy(buffer, XATTR_NAME_SELINUX, len); 33961da177e4SLinus Torvalds return len; 33971da177e4SLinus Torvalds } 33981da177e4SLinus Torvalds 3399d6335d77SAndreas Gruenbacher static void selinux_inode_getsecid(struct inode *inode, u32 *secid) 3400713a04aeSAhmed S. Darwish { 3401e817c2f3SAndreas Gruenbacher struct inode_security_struct *isec = inode_security_novalidate(inode); 3402713a04aeSAhmed S. Darwish *secid = isec->sid; 3403713a04aeSAhmed S. Darwish } 3404713a04aeSAhmed S. Darwish 340556909eb3SVivek Goyal static int selinux_inode_copy_up(struct dentry *src, struct cred **new) 340656909eb3SVivek Goyal { 340756909eb3SVivek Goyal u32 sid; 340856909eb3SVivek Goyal struct task_security_struct *tsec; 340956909eb3SVivek Goyal struct cred *new_creds = *new; 341056909eb3SVivek Goyal 341156909eb3SVivek Goyal if (new_creds == NULL) { 341256909eb3SVivek Goyal new_creds = prepare_creds(); 341356909eb3SVivek Goyal if (!new_creds) 341456909eb3SVivek Goyal return -ENOMEM; 341556909eb3SVivek Goyal } 341656909eb3SVivek Goyal 34170c6cfa62SCasey Schaufler tsec = selinux_cred(new_creds); 341856909eb3SVivek Goyal /* Get label from overlay inode and set it in create_sid */ 341956909eb3SVivek Goyal selinux_inode_getsecid(d_inode(src), &sid); 342056909eb3SVivek Goyal tsec->create_sid = sid; 342156909eb3SVivek Goyal *new = new_creds; 342256909eb3SVivek Goyal return 0; 342356909eb3SVivek Goyal } 342456909eb3SVivek Goyal 342519472b69SVivek Goyal static int selinux_inode_copy_up_xattr(const char *name) 342619472b69SVivek Goyal { 342719472b69SVivek Goyal /* The copy_up hook above sets the initial context on an inode, but we 342819472b69SVivek Goyal * don't then want to overwrite it by blindly copying all the lower 342919472b69SVivek Goyal * xattrs up. Instead, we have to filter out SELinux-related xattrs. 343019472b69SVivek Goyal */ 343119472b69SVivek Goyal if (strcmp(name, XATTR_NAME_SELINUX) == 0) 343219472b69SVivek Goyal return 1; /* Discard */ 343319472b69SVivek Goyal /* 343419472b69SVivek Goyal * Any other attribute apart from SELINUX is not claimed, supported 343519472b69SVivek Goyal * by selinux. 343619472b69SVivek Goyal */ 343719472b69SVivek Goyal return -EOPNOTSUPP; 343819472b69SVivek Goyal } 343919472b69SVivek Goyal 3440ec882da5SOndrej Mosnacek /* kernfs node operations */ 3441ec882da5SOndrej Mosnacek 3442c72c4cdeSYueHaibing static int selinux_kernfs_init_security(struct kernfs_node *kn_dir, 3443ec882da5SOndrej Mosnacek struct kernfs_node *kn) 3444ec882da5SOndrej Mosnacek { 3445169ce0c0SStephen Smalley const struct task_security_struct *tsec = selinux_cred(current_cred()); 3446ec882da5SOndrej Mosnacek u32 parent_sid, newsid, clen; 3447ec882da5SOndrej Mosnacek int rc; 3448ec882da5SOndrej Mosnacek char *context; 3449ec882da5SOndrej Mosnacek 34501537ad15SOndrej Mosnacek rc = kernfs_xattr_get(kn_dir, XATTR_NAME_SELINUX, NULL, 0); 3451ec882da5SOndrej Mosnacek if (rc == -ENODATA) 3452ec882da5SOndrej Mosnacek return 0; 3453ec882da5SOndrej Mosnacek else if (rc < 0) 3454ec882da5SOndrej Mosnacek return rc; 3455ec882da5SOndrej Mosnacek 3456ec882da5SOndrej Mosnacek clen = (u32)rc; 3457ec882da5SOndrej Mosnacek context = kmalloc(clen, GFP_KERNEL); 3458ec882da5SOndrej Mosnacek if (!context) 3459ec882da5SOndrej Mosnacek return -ENOMEM; 3460ec882da5SOndrej Mosnacek 34611537ad15SOndrej Mosnacek rc = kernfs_xattr_get(kn_dir, XATTR_NAME_SELINUX, context, clen); 3462ec882da5SOndrej Mosnacek if (rc < 0) { 3463ec882da5SOndrej Mosnacek kfree(context); 3464ec882da5SOndrej Mosnacek return rc; 3465ec882da5SOndrej Mosnacek } 3466ec882da5SOndrej Mosnacek 3467ec882da5SOndrej Mosnacek rc = security_context_to_sid(&selinux_state, context, clen, &parent_sid, 3468ec882da5SOndrej Mosnacek GFP_KERNEL); 3469ec882da5SOndrej Mosnacek kfree(context); 3470ec882da5SOndrej Mosnacek if (rc) 3471ec882da5SOndrej Mosnacek return rc; 3472ec882da5SOndrej Mosnacek 3473ec882da5SOndrej Mosnacek if (tsec->create_sid) { 3474ec882da5SOndrej Mosnacek newsid = tsec->create_sid; 3475ec882da5SOndrej Mosnacek } else { 3476ec882da5SOndrej Mosnacek u16 secclass = inode_mode_to_security_class(kn->mode); 3477ec882da5SOndrej Mosnacek struct qstr q; 3478ec882da5SOndrej Mosnacek 3479ec882da5SOndrej Mosnacek q.name = kn->name; 3480ec882da5SOndrej Mosnacek q.hash_len = hashlen_string(kn_dir, kn->name); 3481ec882da5SOndrej Mosnacek 3482ec882da5SOndrej Mosnacek rc = security_transition_sid(&selinux_state, tsec->sid, 3483ec882da5SOndrej Mosnacek parent_sid, secclass, &q, 3484ec882da5SOndrej Mosnacek &newsid); 3485ec882da5SOndrej Mosnacek if (rc) 3486ec882da5SOndrej Mosnacek return rc; 3487ec882da5SOndrej Mosnacek } 3488ec882da5SOndrej Mosnacek 3489ec882da5SOndrej Mosnacek rc = security_sid_to_context_force(&selinux_state, newsid, 3490ec882da5SOndrej Mosnacek &context, &clen); 3491ec882da5SOndrej Mosnacek if (rc) 3492ec882da5SOndrej Mosnacek return rc; 3493ec882da5SOndrej Mosnacek 34941537ad15SOndrej Mosnacek rc = kernfs_xattr_set(kn, XATTR_NAME_SELINUX, context, clen, 3495ec882da5SOndrej Mosnacek XATTR_CREATE); 3496ec882da5SOndrej Mosnacek kfree(context); 3497ec882da5SOndrej Mosnacek return rc; 3498ec882da5SOndrej Mosnacek } 3499ec882da5SOndrej Mosnacek 3500ec882da5SOndrej Mosnacek 35011da177e4SLinus Torvalds /* file security operations */ 35021da177e4SLinus Torvalds 3503788e7dd4SYuichi Nakamura static int selinux_revalidate_file_permission(struct file *file, int mask) 35041da177e4SLinus Torvalds { 350588e67f3bSDavid Howells const struct cred *cred = current_cred(); 3506496ad9aaSAl Viro struct inode *inode = file_inode(file); 35071da177e4SLinus Torvalds 35081da177e4SLinus Torvalds /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */ 35091da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE)) 35101da177e4SLinus Torvalds mask |= MAY_APPEND; 35111da177e4SLinus Torvalds 3512389fb800SPaul Moore return file_has_perm(cred, file, 35131da177e4SLinus Torvalds file_mask_to_av(inode->i_mode, mask)); 35141da177e4SLinus Torvalds } 35151da177e4SLinus Torvalds 3516788e7dd4SYuichi Nakamura static int selinux_file_permission(struct file *file, int mask) 3517788e7dd4SYuichi Nakamura { 3518496ad9aaSAl Viro struct inode *inode = file_inode(file); 3519bb6c6b02SCasey Schaufler struct file_security_struct *fsec = selinux_file(file); 3520b197367eSAndreas Gruenbacher struct inode_security_struct *isec; 352120dda18bSStephen Smalley u32 sid = current_sid(); 352220dda18bSStephen Smalley 3523389fb800SPaul Moore if (!mask) 3524788e7dd4SYuichi Nakamura /* No permission to check. Existence test. */ 3525788e7dd4SYuichi Nakamura return 0; 3526788e7dd4SYuichi Nakamura 3527b197367eSAndreas Gruenbacher isec = inode_security(inode); 352820dda18bSStephen Smalley if (sid == fsec->sid && fsec->isid == isec->sid && 35296b6bc620SStephen Smalley fsec->pseqno == avc_policy_seqno(&selinux_state)) 353083d49856SEric Paris /* No change since file_open check. */ 353120dda18bSStephen Smalley return 0; 353220dda18bSStephen Smalley 3533788e7dd4SYuichi Nakamura return selinux_revalidate_file_permission(file, mask); 3534788e7dd4SYuichi Nakamura } 3535788e7dd4SYuichi Nakamura 35361da177e4SLinus Torvalds static int selinux_file_alloc_security(struct file *file) 35371da177e4SLinus Torvalds { 3538cb89e246SPaul Moore struct file_security_struct *fsec = selinux_file(file); 3539cb89e246SPaul Moore u32 sid = current_sid(); 3540cb89e246SPaul Moore 3541cb89e246SPaul Moore fsec->sid = sid; 3542cb89e246SPaul Moore fsec->fown_sid = sid; 3543cb89e246SPaul Moore 3544cb89e246SPaul Moore return 0; 35451da177e4SLinus Torvalds } 35461da177e4SLinus Torvalds 3547fa1aa143SJeff Vander Stoep /* 3548fa1aa143SJeff Vander Stoep * Check whether a task has the ioctl permission and cmd 3549fa1aa143SJeff Vander Stoep * operation to an inode. 3550fa1aa143SJeff Vander Stoep */ 35511d2a168aSGeliang Tang static int ioctl_has_perm(const struct cred *cred, struct file *file, 3552fa1aa143SJeff Vander Stoep u32 requested, u16 cmd) 3553fa1aa143SJeff Vander Stoep { 3554fa1aa143SJeff Vander Stoep struct common_audit_data ad; 3555bb6c6b02SCasey Schaufler struct file_security_struct *fsec = selinux_file(file); 3556fa1aa143SJeff Vander Stoep struct inode *inode = file_inode(file); 355720cdef8dSPaul Moore struct inode_security_struct *isec; 3558fa1aa143SJeff Vander Stoep struct lsm_ioctlop_audit ioctl; 3559fa1aa143SJeff Vander Stoep u32 ssid = cred_sid(cred); 3560fa1aa143SJeff Vander Stoep int rc; 3561fa1aa143SJeff Vander Stoep u8 driver = cmd >> 8; 3562fa1aa143SJeff Vander Stoep u8 xperm = cmd & 0xff; 3563fa1aa143SJeff Vander Stoep 3564fa1aa143SJeff Vander Stoep ad.type = LSM_AUDIT_DATA_IOCTL_OP; 3565fa1aa143SJeff Vander Stoep ad.u.op = &ioctl; 3566fa1aa143SJeff Vander Stoep ad.u.op->cmd = cmd; 3567fa1aa143SJeff Vander Stoep ad.u.op->path = file->f_path; 3568fa1aa143SJeff Vander Stoep 3569fa1aa143SJeff Vander Stoep if (ssid != fsec->sid) { 35706b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 35716b6bc620SStephen Smalley ssid, fsec->sid, 3572fa1aa143SJeff Vander Stoep SECCLASS_FD, 3573fa1aa143SJeff Vander Stoep FD__USE, 3574fa1aa143SJeff Vander Stoep &ad); 3575fa1aa143SJeff Vander Stoep if (rc) 3576fa1aa143SJeff Vander Stoep goto out; 3577fa1aa143SJeff Vander Stoep } 3578fa1aa143SJeff Vander Stoep 3579fa1aa143SJeff Vander Stoep if (unlikely(IS_PRIVATE(inode))) 3580fa1aa143SJeff Vander Stoep return 0; 3581fa1aa143SJeff Vander Stoep 358220cdef8dSPaul Moore isec = inode_security(inode); 35836b6bc620SStephen Smalley rc = avc_has_extended_perms(&selinux_state, 35846b6bc620SStephen Smalley ssid, isec->sid, isec->sclass, 3585fa1aa143SJeff Vander Stoep requested, driver, xperm, &ad); 3586fa1aa143SJeff Vander Stoep out: 3587fa1aa143SJeff Vander Stoep return rc; 3588fa1aa143SJeff Vander Stoep } 3589fa1aa143SJeff Vander Stoep 35901da177e4SLinus Torvalds static int selinux_file_ioctl(struct file *file, unsigned int cmd, 35911da177e4SLinus Torvalds unsigned long arg) 35921da177e4SLinus Torvalds { 359388e67f3bSDavid Howells const struct cred *cred = current_cred(); 35940b24dcb7SEric Paris int error = 0; 35951da177e4SLinus Torvalds 35960b24dcb7SEric Paris switch (cmd) { 35970b24dcb7SEric Paris case FIONREAD: 35980b24dcb7SEric Paris /* fall through */ 35990b24dcb7SEric Paris case FIBMAP: 36000b24dcb7SEric Paris /* fall through */ 36010b24dcb7SEric Paris case FIGETBSZ: 36020b24dcb7SEric Paris /* fall through */ 36032f99c369SAl Viro case FS_IOC_GETFLAGS: 36040b24dcb7SEric Paris /* fall through */ 36052f99c369SAl Viro case FS_IOC_GETVERSION: 36060b24dcb7SEric Paris error = file_has_perm(cred, file, FILE__GETATTR); 36070b24dcb7SEric Paris break; 36081da177e4SLinus Torvalds 36092f99c369SAl Viro case FS_IOC_SETFLAGS: 36100b24dcb7SEric Paris /* fall through */ 36112f99c369SAl Viro case FS_IOC_SETVERSION: 36120b24dcb7SEric Paris error = file_has_perm(cred, file, FILE__SETATTR); 36130b24dcb7SEric Paris break; 36140b24dcb7SEric Paris 36150b24dcb7SEric Paris /* sys_ioctl() checks */ 36160b24dcb7SEric Paris case FIONBIO: 36170b24dcb7SEric Paris /* fall through */ 36180b24dcb7SEric Paris case FIOASYNC: 36190b24dcb7SEric Paris error = file_has_perm(cred, file, 0); 36200b24dcb7SEric Paris break; 36210b24dcb7SEric Paris 36220b24dcb7SEric Paris case KDSKBENT: 36230b24dcb7SEric Paris case KDSKBSENT: 36246a9de491SEric Paris error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG, 3625c1a85a00SMicah Morton CAP_OPT_NONE, true); 36260b24dcb7SEric Paris break; 36270b24dcb7SEric Paris 36280b24dcb7SEric Paris /* default case assumes that the command will go 36290b24dcb7SEric Paris * to the file's ioctl() function. 36300b24dcb7SEric Paris */ 36310b24dcb7SEric Paris default: 3632fa1aa143SJeff Vander Stoep error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd); 36330b24dcb7SEric Paris } 36340b24dcb7SEric Paris return error; 36351da177e4SLinus Torvalds } 36361da177e4SLinus Torvalds 3637b78b7d59SStephen Smalley static int default_noexec __ro_after_init; 3638fcaaade1SStephen Smalley 36391da177e4SLinus Torvalds static int file_map_prot_check(struct file *file, unsigned long prot, int shared) 36401da177e4SLinus Torvalds { 364188e67f3bSDavid Howells const struct cred *cred = current_cred(); 3642be0554c9SStephen Smalley u32 sid = cred_sid(cred); 3643d84f4f99SDavid Howells int rc = 0; 364488e67f3bSDavid Howells 3645fcaaade1SStephen Smalley if (default_noexec && 3646892e8cacSStephen Smalley (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) || 3647892e8cacSStephen Smalley (!shared && (prot & PROT_WRITE)))) { 36481da177e4SLinus Torvalds /* 36491da177e4SLinus Torvalds * We are making executable an anonymous mapping or a 36501da177e4SLinus Torvalds * private file mapping that will also be writable. 36511da177e4SLinus Torvalds * This has an additional check. 36521da177e4SLinus Torvalds */ 36536b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 36546b6bc620SStephen Smalley sid, sid, SECCLASS_PROCESS, 3655be0554c9SStephen Smalley PROCESS__EXECMEM, NULL); 36561da177e4SLinus Torvalds if (rc) 3657d84f4f99SDavid Howells goto error; 36581da177e4SLinus Torvalds } 36591da177e4SLinus Torvalds 36601da177e4SLinus Torvalds if (file) { 36611da177e4SLinus Torvalds /* read access is always possible with a mapping */ 36621da177e4SLinus Torvalds u32 av = FILE__READ; 36631da177e4SLinus Torvalds 36641da177e4SLinus Torvalds /* write access only matters if the mapping is shared */ 36651da177e4SLinus Torvalds if (shared && (prot & PROT_WRITE)) 36661da177e4SLinus Torvalds av |= FILE__WRITE; 36671da177e4SLinus Torvalds 36681da177e4SLinus Torvalds if (prot & PROT_EXEC) 36691da177e4SLinus Torvalds av |= FILE__EXECUTE; 36701da177e4SLinus Torvalds 367188e67f3bSDavid Howells return file_has_perm(cred, file, av); 36721da177e4SLinus Torvalds } 3673d84f4f99SDavid Howells 3674d84f4f99SDavid Howells error: 3675d84f4f99SDavid Howells return rc; 36761da177e4SLinus Torvalds } 36771da177e4SLinus Torvalds 3678e5467859SAl Viro static int selinux_mmap_addr(unsigned long addr) 36791da177e4SLinus Torvalds { 3680b1d9e6b0SCasey Schaufler int rc = 0; 368198883bfdSPaul Moore 368298883bfdSPaul Moore if (addr < CONFIG_LSM_MMAP_MIN_ADDR) { 368398883bfdSPaul Moore u32 sid = current_sid(); 36846b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 36856b6bc620SStephen Smalley sid, sid, SECCLASS_MEMPROTECT, 368698883bfdSPaul Moore MEMPROTECT__MMAP_ZERO, NULL); 368798883bfdSPaul Moore } 368898883bfdSPaul Moore 368998883bfdSPaul Moore return rc; 3690e5467859SAl Viro } 36911da177e4SLinus Torvalds 3692e5467859SAl Viro static int selinux_mmap_file(struct file *file, unsigned long reqprot, 3693e5467859SAl Viro unsigned long prot, unsigned long flags) 3694e5467859SAl Viro { 36953ba4bf5fSStephen Smalley struct common_audit_data ad; 36963ba4bf5fSStephen Smalley int rc; 36973ba4bf5fSStephen Smalley 36983ba4bf5fSStephen Smalley if (file) { 36993ba4bf5fSStephen Smalley ad.type = LSM_AUDIT_DATA_FILE; 37003ba4bf5fSStephen Smalley ad.u.file = file; 37013ba4bf5fSStephen Smalley rc = inode_has_perm(current_cred(), file_inode(file), 37023ba4bf5fSStephen Smalley FILE__MAP, &ad); 37033ba4bf5fSStephen Smalley if (rc) 37043ba4bf5fSStephen Smalley return rc; 37053ba4bf5fSStephen Smalley } 37063ba4bf5fSStephen Smalley 3707aa8e712cSStephen Smalley if (selinux_state.checkreqprot) 37081da177e4SLinus Torvalds prot = reqprot; 37091da177e4SLinus Torvalds 37101da177e4SLinus Torvalds return file_map_prot_check(file, prot, 37111da177e4SLinus Torvalds (flags & MAP_TYPE) == MAP_SHARED); 37121da177e4SLinus Torvalds } 37131da177e4SLinus Torvalds 37141da177e4SLinus Torvalds static int selinux_file_mprotect(struct vm_area_struct *vma, 37151da177e4SLinus Torvalds unsigned long reqprot, 37161da177e4SLinus Torvalds unsigned long prot) 37171da177e4SLinus Torvalds { 371888e67f3bSDavid Howells const struct cred *cred = current_cred(); 3719be0554c9SStephen Smalley u32 sid = cred_sid(cred); 37201da177e4SLinus Torvalds 3721aa8e712cSStephen Smalley if (selinux_state.checkreqprot) 37221da177e4SLinus Torvalds prot = reqprot; 37231da177e4SLinus Torvalds 3724fcaaade1SStephen Smalley if (default_noexec && 3725fcaaade1SStephen Smalley (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) { 3726d541bbeeSJames Morris int rc = 0; 3727db4c9641SStephen Smalley if (vma->vm_start >= vma->vm_mm->start_brk && 3728db4c9641SStephen Smalley vma->vm_end <= vma->vm_mm->brk) { 37296b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 37306b6bc620SStephen Smalley sid, sid, SECCLASS_PROCESS, 3731be0554c9SStephen Smalley PROCESS__EXECHEAP, NULL); 3732db4c9641SStephen Smalley } else if (!vma->vm_file && 3733c2316dbfSStephen Smalley ((vma->vm_start <= vma->vm_mm->start_stack && 3734c2316dbfSStephen Smalley vma->vm_end >= vma->vm_mm->start_stack) || 3735d17af505SAndy Lutomirski vma_is_stack_for_current(vma))) { 37366b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 37376b6bc620SStephen Smalley sid, sid, SECCLASS_PROCESS, 3738be0554c9SStephen Smalley PROCESS__EXECSTACK, NULL); 3739db4c9641SStephen Smalley } else if (vma->vm_file && vma->anon_vma) { 3740db4c9641SStephen Smalley /* 3741db4c9641SStephen Smalley * We are making executable a file mapping that has 3742db4c9641SStephen Smalley * had some COW done. Since pages might have been 3743db4c9641SStephen Smalley * written, check ability to execute the possibly 3744db4c9641SStephen Smalley * modified content. This typically should only 3745db4c9641SStephen Smalley * occur for text relocations. 3746db4c9641SStephen Smalley */ 3747d84f4f99SDavid Howells rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD); 3748db4c9641SStephen Smalley } 37496b992197SLorenzo Hernandez García-Hierro if (rc) 37506b992197SLorenzo Hernandez García-Hierro return rc; 37516b992197SLorenzo Hernandez García-Hierro } 37521da177e4SLinus Torvalds 37531da177e4SLinus Torvalds return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED); 37541da177e4SLinus Torvalds } 37551da177e4SLinus Torvalds 37561da177e4SLinus Torvalds static int selinux_file_lock(struct file *file, unsigned int cmd) 37571da177e4SLinus Torvalds { 375888e67f3bSDavid Howells const struct cred *cred = current_cred(); 375988e67f3bSDavid Howells 376088e67f3bSDavid Howells return file_has_perm(cred, file, FILE__LOCK); 37611da177e4SLinus Torvalds } 37621da177e4SLinus Torvalds 37631da177e4SLinus Torvalds static int selinux_file_fcntl(struct file *file, unsigned int cmd, 37641da177e4SLinus Torvalds unsigned long arg) 37651da177e4SLinus Torvalds { 376688e67f3bSDavid Howells const struct cred *cred = current_cred(); 37671da177e4SLinus Torvalds int err = 0; 37681da177e4SLinus Torvalds 37691da177e4SLinus Torvalds switch (cmd) { 37701da177e4SLinus Torvalds case F_SETFL: 37711da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) { 377288e67f3bSDavid Howells err = file_has_perm(cred, file, FILE__WRITE); 37731da177e4SLinus Torvalds break; 37741da177e4SLinus Torvalds } 37751da177e4SLinus Torvalds /* fall through */ 37761da177e4SLinus Torvalds case F_SETOWN: 37771da177e4SLinus Torvalds case F_SETSIG: 37781da177e4SLinus Torvalds case F_GETFL: 37791da177e4SLinus Torvalds case F_GETOWN: 37801da177e4SLinus Torvalds case F_GETSIG: 37811d151c33SCyrill Gorcunov case F_GETOWNER_UIDS: 37821da177e4SLinus Torvalds /* Just check FD__USE permission */ 378388e67f3bSDavid Howells err = file_has_perm(cred, file, 0); 37841da177e4SLinus Torvalds break; 37851da177e4SLinus Torvalds case F_GETLK: 37861da177e4SLinus Torvalds case F_SETLK: 37871da177e4SLinus Torvalds case F_SETLKW: 37880d3f7a2dSJeff Layton case F_OFD_GETLK: 37890d3f7a2dSJeff Layton case F_OFD_SETLK: 37900d3f7a2dSJeff Layton case F_OFD_SETLKW: 37911da177e4SLinus Torvalds #if BITS_PER_LONG == 32 37921da177e4SLinus Torvalds case F_GETLK64: 37931da177e4SLinus Torvalds case F_SETLK64: 37941da177e4SLinus Torvalds case F_SETLKW64: 37951da177e4SLinus Torvalds #endif 379688e67f3bSDavid Howells err = file_has_perm(cred, file, FILE__LOCK); 37971da177e4SLinus Torvalds break; 37981da177e4SLinus Torvalds } 37991da177e4SLinus Torvalds 38001da177e4SLinus Torvalds return err; 38011da177e4SLinus Torvalds } 38021da177e4SLinus Torvalds 3803e0b93eddSJeff Layton static void selinux_file_set_fowner(struct file *file) 38041da177e4SLinus Torvalds { 38051da177e4SLinus Torvalds struct file_security_struct *fsec; 38061da177e4SLinus Torvalds 3807bb6c6b02SCasey Schaufler fsec = selinux_file(file); 3808275bb41eSDavid Howells fsec->fown_sid = current_sid(); 38091da177e4SLinus Torvalds } 38101da177e4SLinus Torvalds 38111da177e4SLinus Torvalds static int selinux_file_send_sigiotask(struct task_struct *tsk, 38121da177e4SLinus Torvalds struct fown_struct *fown, int signum) 38131da177e4SLinus Torvalds { 38141da177e4SLinus Torvalds struct file *file; 381565c90bcaSStephen Smalley u32 sid = task_sid(tsk); 38161da177e4SLinus Torvalds u32 perm; 38171da177e4SLinus Torvalds struct file_security_struct *fsec; 38181da177e4SLinus Torvalds 38191da177e4SLinus Torvalds /* struct fown_struct is never outside the context of a struct file */ 3820b385a144SRobert P. J. Day file = container_of(fown, struct file, f_owner); 38211da177e4SLinus Torvalds 3822bb6c6b02SCasey Schaufler fsec = selinux_file(file); 38231da177e4SLinus Torvalds 38241da177e4SLinus Torvalds if (!signum) 38251da177e4SLinus Torvalds perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */ 38261da177e4SLinus Torvalds else 38271da177e4SLinus Torvalds perm = signal_to_av(signum); 38281da177e4SLinus Torvalds 38296b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 38306b6bc620SStephen Smalley fsec->fown_sid, sid, 38311da177e4SLinus Torvalds SECCLASS_PROCESS, perm, NULL); 38321da177e4SLinus Torvalds } 38331da177e4SLinus Torvalds 38341da177e4SLinus Torvalds static int selinux_file_receive(struct file *file) 38351da177e4SLinus Torvalds { 383688e67f3bSDavid Howells const struct cred *cred = current_cred(); 383788e67f3bSDavid Howells 383888e67f3bSDavid Howells return file_has_perm(cred, file, file_to_av(file)); 38391da177e4SLinus Torvalds } 38401da177e4SLinus Torvalds 384194817692SAl Viro static int selinux_file_open(struct file *file) 3842788e7dd4SYuichi Nakamura { 3843788e7dd4SYuichi Nakamura struct file_security_struct *fsec; 3844788e7dd4SYuichi Nakamura struct inode_security_struct *isec; 3845d84f4f99SDavid Howells 3846bb6c6b02SCasey Schaufler fsec = selinux_file(file); 384783da53c5SAndreas Gruenbacher isec = inode_security(file_inode(file)); 3848788e7dd4SYuichi Nakamura /* 3849788e7dd4SYuichi Nakamura * Save inode label and policy sequence number 3850788e7dd4SYuichi Nakamura * at open-time so that selinux_file_permission 3851788e7dd4SYuichi Nakamura * can determine whether revalidation is necessary. 3852788e7dd4SYuichi Nakamura * Task label is already saved in the file security 3853788e7dd4SYuichi Nakamura * struct as its SID. 3854788e7dd4SYuichi Nakamura */ 3855788e7dd4SYuichi Nakamura fsec->isid = isec->sid; 38566b6bc620SStephen Smalley fsec->pseqno = avc_policy_seqno(&selinux_state); 3857788e7dd4SYuichi Nakamura /* 3858788e7dd4SYuichi Nakamura * Since the inode label or policy seqno may have changed 3859788e7dd4SYuichi Nakamura * between the selinux_inode_permission check and the saving 3860788e7dd4SYuichi Nakamura * of state above, recheck that access is still permitted. 3861788e7dd4SYuichi Nakamura * Otherwise, access might never be revalidated against the 3862788e7dd4SYuichi Nakamura * new inode label or new policy. 3863788e7dd4SYuichi Nakamura * This check is not redundant - do not remove. 3864788e7dd4SYuichi Nakamura */ 386594817692SAl Viro return file_path_has_perm(file->f_cred, file, open_file_to_av(file)); 3866788e7dd4SYuichi Nakamura } 3867788e7dd4SYuichi Nakamura 38681da177e4SLinus Torvalds /* task security operations */ 38691da177e4SLinus Torvalds 3870a79be238STetsuo Handa static int selinux_task_alloc(struct task_struct *task, 3871a79be238STetsuo Handa unsigned long clone_flags) 38721da177e4SLinus Torvalds { 3873be0554c9SStephen Smalley u32 sid = current_sid(); 3874be0554c9SStephen Smalley 38756b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 38766b6bc620SStephen Smalley sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL); 38771da177e4SLinus Torvalds } 38781da177e4SLinus Torvalds 3879f1752eecSDavid Howells /* 3880d84f4f99SDavid Howells * prepare a new set of credentials for modification 3881d84f4f99SDavid Howells */ 3882d84f4f99SDavid Howells static int selinux_cred_prepare(struct cred *new, const struct cred *old, 3883d84f4f99SDavid Howells gfp_t gfp) 3884d84f4f99SDavid Howells { 3885bbd3662aSCasey Schaufler const struct task_security_struct *old_tsec = selinux_cred(old); 3886bbd3662aSCasey Schaufler struct task_security_struct *tsec = selinux_cred(new); 3887d84f4f99SDavid Howells 3888bbd3662aSCasey Schaufler *tsec = *old_tsec; 3889d84f4f99SDavid Howells return 0; 3890d84f4f99SDavid Howells } 3891d84f4f99SDavid Howells 3892d84f4f99SDavid Howells /* 3893ee18d64cSDavid Howells * transfer the SELinux data to a blank set of creds 3894ee18d64cSDavid Howells */ 3895ee18d64cSDavid Howells static void selinux_cred_transfer(struct cred *new, const struct cred *old) 3896ee18d64cSDavid Howells { 38970c6cfa62SCasey Schaufler const struct task_security_struct *old_tsec = selinux_cred(old); 38980c6cfa62SCasey Schaufler struct task_security_struct *tsec = selinux_cred(new); 3899ee18d64cSDavid Howells 3900ee18d64cSDavid Howells *tsec = *old_tsec; 3901ee18d64cSDavid Howells } 3902ee18d64cSDavid Howells 39033ec30113SMatthew Garrett static void selinux_cred_getsecid(const struct cred *c, u32 *secid) 39043ec30113SMatthew Garrett { 39053ec30113SMatthew Garrett *secid = cred_sid(c); 39063ec30113SMatthew Garrett } 39073ec30113SMatthew Garrett 3908ee18d64cSDavid Howells /* 39093a3b7ce9SDavid Howells * set the security data for a kernel service 39103a3b7ce9SDavid Howells * - all the creation contexts are set to unlabelled 39113a3b7ce9SDavid Howells */ 39123a3b7ce9SDavid Howells static int selinux_kernel_act_as(struct cred *new, u32 secid) 39133a3b7ce9SDavid Howells { 39140c6cfa62SCasey Schaufler struct task_security_struct *tsec = selinux_cred(new); 39153a3b7ce9SDavid Howells u32 sid = current_sid(); 39163a3b7ce9SDavid Howells int ret; 39173a3b7ce9SDavid Howells 39186b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 39196b6bc620SStephen Smalley sid, secid, 39203a3b7ce9SDavid Howells SECCLASS_KERNEL_SERVICE, 39213a3b7ce9SDavid Howells KERNEL_SERVICE__USE_AS_OVERRIDE, 39223a3b7ce9SDavid Howells NULL); 39233a3b7ce9SDavid Howells if (ret == 0) { 39243a3b7ce9SDavid Howells tsec->sid = secid; 39253a3b7ce9SDavid Howells tsec->create_sid = 0; 39263a3b7ce9SDavid Howells tsec->keycreate_sid = 0; 39273a3b7ce9SDavid Howells tsec->sockcreate_sid = 0; 39283a3b7ce9SDavid Howells } 39293a3b7ce9SDavid Howells return ret; 39303a3b7ce9SDavid Howells } 39313a3b7ce9SDavid Howells 39323a3b7ce9SDavid Howells /* 39333a3b7ce9SDavid Howells * set the file creation context in a security record to the same as the 39343a3b7ce9SDavid Howells * objective context of the specified inode 39353a3b7ce9SDavid Howells */ 39363a3b7ce9SDavid Howells static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode) 39373a3b7ce9SDavid Howells { 393883da53c5SAndreas Gruenbacher struct inode_security_struct *isec = inode_security(inode); 39390c6cfa62SCasey Schaufler struct task_security_struct *tsec = selinux_cred(new); 39403a3b7ce9SDavid Howells u32 sid = current_sid(); 39413a3b7ce9SDavid Howells int ret; 39423a3b7ce9SDavid Howells 39436b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 39446b6bc620SStephen Smalley sid, isec->sid, 39453a3b7ce9SDavid Howells SECCLASS_KERNEL_SERVICE, 39463a3b7ce9SDavid Howells KERNEL_SERVICE__CREATE_FILES_AS, 39473a3b7ce9SDavid Howells NULL); 39483a3b7ce9SDavid Howells 39493a3b7ce9SDavid Howells if (ret == 0) 39503a3b7ce9SDavid Howells tsec->create_sid = isec->sid; 3951ef57471aSDavid Howells return ret; 39523a3b7ce9SDavid Howells } 39533a3b7ce9SDavid Howells 3954dd8dbf2eSEric Paris static int selinux_kernel_module_request(char *kmod_name) 395525354c4fSEric Paris { 3956dd8dbf2eSEric Paris struct common_audit_data ad; 3957dd8dbf2eSEric Paris 395850c205f5SEric Paris ad.type = LSM_AUDIT_DATA_KMOD; 3959dd8dbf2eSEric Paris ad.u.kmod_name = kmod_name; 3960dd8dbf2eSEric Paris 39616b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 39626b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM, 3963dd8dbf2eSEric Paris SYSTEM__MODULE_REQUEST, &ad); 396425354c4fSEric Paris } 396525354c4fSEric Paris 396661d612eaSJeff Vander Stoep static int selinux_kernel_module_from_file(struct file *file) 396761d612eaSJeff Vander Stoep { 396861d612eaSJeff Vander Stoep struct common_audit_data ad; 396961d612eaSJeff Vander Stoep struct inode_security_struct *isec; 397061d612eaSJeff Vander Stoep struct file_security_struct *fsec; 397161d612eaSJeff Vander Stoep u32 sid = current_sid(); 397261d612eaSJeff Vander Stoep int rc; 397361d612eaSJeff Vander Stoep 397461d612eaSJeff Vander Stoep /* init_module */ 397561d612eaSJeff Vander Stoep if (file == NULL) 39766b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 39776b6bc620SStephen Smalley sid, sid, SECCLASS_SYSTEM, 397861d612eaSJeff Vander Stoep SYSTEM__MODULE_LOAD, NULL); 397961d612eaSJeff Vander Stoep 398061d612eaSJeff Vander Stoep /* finit_module */ 398120cdef8dSPaul Moore 398243af5de7SVivek Goyal ad.type = LSM_AUDIT_DATA_FILE; 398343af5de7SVivek Goyal ad.u.file = file; 398461d612eaSJeff Vander Stoep 3985bb6c6b02SCasey Schaufler fsec = selinux_file(file); 398661d612eaSJeff Vander Stoep if (sid != fsec->sid) { 39876b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 39886b6bc620SStephen Smalley sid, fsec->sid, SECCLASS_FD, FD__USE, &ad); 398961d612eaSJeff Vander Stoep if (rc) 399061d612eaSJeff Vander Stoep return rc; 399161d612eaSJeff Vander Stoep } 399261d612eaSJeff Vander Stoep 399320cdef8dSPaul Moore isec = inode_security(file_inode(file)); 39946b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 39956b6bc620SStephen Smalley sid, isec->sid, SECCLASS_SYSTEM, 399661d612eaSJeff Vander Stoep SYSTEM__MODULE_LOAD, &ad); 399761d612eaSJeff Vander Stoep } 399861d612eaSJeff Vander Stoep 399961d612eaSJeff Vander Stoep static int selinux_kernel_read_file(struct file *file, 400061d612eaSJeff Vander Stoep enum kernel_read_file_id id) 400161d612eaSJeff Vander Stoep { 400261d612eaSJeff Vander Stoep int rc = 0; 400361d612eaSJeff Vander Stoep 400461d612eaSJeff Vander Stoep switch (id) { 400561d612eaSJeff Vander Stoep case READING_MODULE: 400661d612eaSJeff Vander Stoep rc = selinux_kernel_module_from_file(file); 400761d612eaSJeff Vander Stoep break; 400861d612eaSJeff Vander Stoep default: 400961d612eaSJeff Vander Stoep break; 401061d612eaSJeff Vander Stoep } 401161d612eaSJeff Vander Stoep 401261d612eaSJeff Vander Stoep return rc; 401361d612eaSJeff Vander Stoep } 401461d612eaSJeff Vander Stoep 4015c77b8cdfSMimi Zohar static int selinux_kernel_load_data(enum kernel_load_data_id id) 4016c77b8cdfSMimi Zohar { 4017c77b8cdfSMimi Zohar int rc = 0; 4018c77b8cdfSMimi Zohar 4019c77b8cdfSMimi Zohar switch (id) { 4020c77b8cdfSMimi Zohar case LOADING_MODULE: 4021c77b8cdfSMimi Zohar rc = selinux_kernel_module_from_file(NULL); 4022c77b8cdfSMimi Zohar default: 4023c77b8cdfSMimi Zohar break; 4024c77b8cdfSMimi Zohar } 4025c77b8cdfSMimi Zohar 4026c77b8cdfSMimi Zohar return rc; 4027c77b8cdfSMimi Zohar } 4028c77b8cdfSMimi Zohar 40291da177e4SLinus Torvalds static int selinux_task_setpgid(struct task_struct *p, pid_t pgid) 40301da177e4SLinus Torvalds { 40316b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 40326b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 4033be0554c9SStephen Smalley PROCESS__SETPGID, NULL); 40341da177e4SLinus Torvalds } 40351da177e4SLinus Torvalds 40361da177e4SLinus Torvalds static int selinux_task_getpgid(struct task_struct *p) 40371da177e4SLinus Torvalds { 40386b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 40396b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 4040be0554c9SStephen Smalley PROCESS__GETPGID, NULL); 40411da177e4SLinus Torvalds } 40421da177e4SLinus Torvalds 40431da177e4SLinus Torvalds static int selinux_task_getsid(struct task_struct *p) 40441da177e4SLinus Torvalds { 40456b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 40466b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 4047be0554c9SStephen Smalley PROCESS__GETSESSION, NULL); 40481da177e4SLinus Torvalds } 40491da177e4SLinus Torvalds 4050f9008e4cSDavid Quigley static void selinux_task_getsecid(struct task_struct *p, u32 *secid) 4051f9008e4cSDavid Quigley { 4052275bb41eSDavid Howells *secid = task_sid(p); 4053f9008e4cSDavid Quigley } 4054f9008e4cSDavid Quigley 40551da177e4SLinus Torvalds static int selinux_task_setnice(struct task_struct *p, int nice) 40561da177e4SLinus Torvalds { 40576b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 40586b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 4059be0554c9SStephen Smalley PROCESS__SETSCHED, NULL); 40601da177e4SLinus Torvalds } 40611da177e4SLinus Torvalds 406203e68060SJames Morris static int selinux_task_setioprio(struct task_struct *p, int ioprio) 406303e68060SJames Morris { 40646b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 40656b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 4066be0554c9SStephen Smalley PROCESS__SETSCHED, NULL); 406703e68060SJames Morris } 406803e68060SJames Morris 4069a1836a42SDavid Quigley static int selinux_task_getioprio(struct task_struct *p) 4070a1836a42SDavid Quigley { 40716b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 40726b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 4073be0554c9SStephen Smalley PROCESS__GETSCHED, NULL); 4074a1836a42SDavid Quigley } 4075a1836a42SDavid Quigley 40764298555dSCorentin LABBE static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred, 4077791ec491SStephen Smalley unsigned int flags) 4078791ec491SStephen Smalley { 4079791ec491SStephen Smalley u32 av = 0; 4080791ec491SStephen Smalley 408184e6885eSStephen Smalley if (!flags) 408284e6885eSStephen Smalley return 0; 4083791ec491SStephen Smalley if (flags & LSM_PRLIMIT_WRITE) 4084791ec491SStephen Smalley av |= PROCESS__SETRLIMIT; 4085791ec491SStephen Smalley if (flags & LSM_PRLIMIT_READ) 4086791ec491SStephen Smalley av |= PROCESS__GETRLIMIT; 40876b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 40886b6bc620SStephen Smalley cred_sid(cred), cred_sid(tcred), 4089791ec491SStephen Smalley SECCLASS_PROCESS, av, NULL); 4090791ec491SStephen Smalley } 4091791ec491SStephen Smalley 40928fd00b4dSJiri Slaby static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource, 40938fd00b4dSJiri Slaby struct rlimit *new_rlim) 40941da177e4SLinus Torvalds { 40958fd00b4dSJiri Slaby struct rlimit *old_rlim = p->signal->rlim + resource; 40961da177e4SLinus Torvalds 40971da177e4SLinus Torvalds /* Control the ability to change the hard limit (whether 40981da177e4SLinus Torvalds lowering or raising it), so that the hard limit can 40991da177e4SLinus Torvalds later be used as a safe reset point for the soft limit 4100d84f4f99SDavid Howells upon context transitions. See selinux_bprm_committing_creds. */ 41011da177e4SLinus Torvalds if (old_rlim->rlim_max != new_rlim->rlim_max) 41026b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 41036b6bc620SStephen Smalley current_sid(), task_sid(p), 4104be0554c9SStephen Smalley SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL); 41051da177e4SLinus Torvalds 41061da177e4SLinus Torvalds return 0; 41071da177e4SLinus Torvalds } 41081da177e4SLinus Torvalds 4109b0ae1981SKOSAKI Motohiro static int selinux_task_setscheduler(struct task_struct *p) 41101da177e4SLinus Torvalds { 41116b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 41126b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 4113be0554c9SStephen Smalley PROCESS__SETSCHED, NULL); 41141da177e4SLinus Torvalds } 41151da177e4SLinus Torvalds 41161da177e4SLinus Torvalds static int selinux_task_getscheduler(struct task_struct *p) 41171da177e4SLinus Torvalds { 41186b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 41196b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 4120be0554c9SStephen Smalley PROCESS__GETSCHED, NULL); 41211da177e4SLinus Torvalds } 41221da177e4SLinus Torvalds 412335601547SDavid Quigley static int selinux_task_movememory(struct task_struct *p) 412435601547SDavid Quigley { 41256b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 41266b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 4127be0554c9SStephen Smalley PROCESS__SETSCHED, NULL); 412835601547SDavid Quigley } 412935601547SDavid Quigley 4130ae7795bcSEric W. Biederman static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info, 41316b4f3d01SStephen Smalley int sig, const struct cred *cred) 41321da177e4SLinus Torvalds { 41336b4f3d01SStephen Smalley u32 secid; 41341da177e4SLinus Torvalds u32 perm; 41351da177e4SLinus Torvalds 41361da177e4SLinus Torvalds if (!sig) 41371da177e4SLinus Torvalds perm = PROCESS__SIGNULL; /* null signal; existence test */ 41381da177e4SLinus Torvalds else 41391da177e4SLinus Torvalds perm = signal_to_av(sig); 41406b4f3d01SStephen Smalley if (!cred) 4141be0554c9SStephen Smalley secid = current_sid(); 41426b4f3d01SStephen Smalley else 41436b4f3d01SStephen Smalley secid = cred_sid(cred); 41446b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 41456b6bc620SStephen Smalley secid, task_sid(p), SECCLASS_PROCESS, perm, NULL); 41461da177e4SLinus Torvalds } 41471da177e4SLinus Torvalds 41481da177e4SLinus Torvalds static void selinux_task_to_inode(struct task_struct *p, 41491da177e4SLinus Torvalds struct inode *inode) 41501da177e4SLinus Torvalds { 415180788c22SCasey Schaufler struct inode_security_struct *isec = selinux_inode(inode); 4152275bb41eSDavid Howells u32 sid = task_sid(p); 41531da177e4SLinus Torvalds 41549287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 4155db978da8SAndreas Gruenbacher isec->sclass = inode_mode_to_security_class(inode->i_mode); 4156275bb41eSDavid Howells isec->sid = sid; 41576f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 41589287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 41591da177e4SLinus Torvalds } 41601da177e4SLinus Torvalds 41611da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 416267f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv4(struct sk_buff *skb, 41632bf49690SThomas Liu struct common_audit_data *ad, u8 *proto) 41641da177e4SLinus Torvalds { 41651da177e4SLinus Torvalds int offset, ihlen, ret = -EINVAL; 41661da177e4SLinus Torvalds struct iphdr _iph, *ih; 41671da177e4SLinus Torvalds 4168bbe735e4SArnaldo Carvalho de Melo offset = skb_network_offset(skb); 41691da177e4SLinus Torvalds ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph); 41701da177e4SLinus Torvalds if (ih == NULL) 41711da177e4SLinus Torvalds goto out; 41721da177e4SLinus Torvalds 41731da177e4SLinus Torvalds ihlen = ih->ihl * 4; 41741da177e4SLinus Torvalds if (ihlen < sizeof(_iph)) 41751da177e4SLinus Torvalds goto out; 41761da177e4SLinus Torvalds 417748c62af6SEric Paris ad->u.net->v4info.saddr = ih->saddr; 417848c62af6SEric Paris ad->u.net->v4info.daddr = ih->daddr; 41791da177e4SLinus Torvalds ret = 0; 41801da177e4SLinus Torvalds 418167f83cbfSVenkat Yekkirala if (proto) 418267f83cbfSVenkat Yekkirala *proto = ih->protocol; 418367f83cbfSVenkat Yekkirala 41841da177e4SLinus Torvalds switch (ih->protocol) { 41851da177e4SLinus Torvalds case IPPROTO_TCP: { 41861da177e4SLinus Torvalds struct tcphdr _tcph, *th; 41871da177e4SLinus Torvalds 41881da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 41891da177e4SLinus Torvalds break; 41901da177e4SLinus Torvalds 41911da177e4SLinus Torvalds offset += ihlen; 41921da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 41931da177e4SLinus Torvalds if (th == NULL) 41941da177e4SLinus Torvalds break; 41951da177e4SLinus Torvalds 419648c62af6SEric Paris ad->u.net->sport = th->source; 419748c62af6SEric Paris ad->u.net->dport = th->dest; 41981da177e4SLinus Torvalds break; 41991da177e4SLinus Torvalds } 42001da177e4SLinus Torvalds 42011da177e4SLinus Torvalds case IPPROTO_UDP: { 42021da177e4SLinus Torvalds struct udphdr _udph, *uh; 42031da177e4SLinus Torvalds 42041da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 42051da177e4SLinus Torvalds break; 42061da177e4SLinus Torvalds 42071da177e4SLinus Torvalds offset += ihlen; 42081da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 42091da177e4SLinus Torvalds if (uh == NULL) 42101da177e4SLinus Torvalds break; 42111da177e4SLinus Torvalds 421248c62af6SEric Paris ad->u.net->sport = uh->source; 421348c62af6SEric Paris ad->u.net->dport = uh->dest; 42141da177e4SLinus Torvalds break; 42151da177e4SLinus Torvalds } 42161da177e4SLinus Torvalds 42172ee92d46SJames Morris case IPPROTO_DCCP: { 42182ee92d46SJames Morris struct dccp_hdr _dccph, *dh; 42192ee92d46SJames Morris 42202ee92d46SJames Morris if (ntohs(ih->frag_off) & IP_OFFSET) 42212ee92d46SJames Morris break; 42222ee92d46SJames Morris 42232ee92d46SJames Morris offset += ihlen; 42242ee92d46SJames Morris dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); 42252ee92d46SJames Morris if (dh == NULL) 42262ee92d46SJames Morris break; 42272ee92d46SJames Morris 422848c62af6SEric Paris ad->u.net->sport = dh->dccph_sport; 422948c62af6SEric Paris ad->u.net->dport = dh->dccph_dport; 42302ee92d46SJames Morris break; 42312ee92d46SJames Morris } 42322ee92d46SJames Morris 4233d452930fSRichard Haines #if IS_ENABLED(CONFIG_IP_SCTP) 4234d452930fSRichard Haines case IPPROTO_SCTP: { 4235d452930fSRichard Haines struct sctphdr _sctph, *sh; 4236d452930fSRichard Haines 4237d452930fSRichard Haines if (ntohs(ih->frag_off) & IP_OFFSET) 4238d452930fSRichard Haines break; 4239d452930fSRichard Haines 4240d452930fSRichard Haines offset += ihlen; 4241d452930fSRichard Haines sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph); 4242d452930fSRichard Haines if (sh == NULL) 4243d452930fSRichard Haines break; 4244d452930fSRichard Haines 4245d452930fSRichard Haines ad->u.net->sport = sh->source; 4246d452930fSRichard Haines ad->u.net->dport = sh->dest; 4247d452930fSRichard Haines break; 4248d452930fSRichard Haines } 4249d452930fSRichard Haines #endif 42501da177e4SLinus Torvalds default: 42511da177e4SLinus Torvalds break; 42521da177e4SLinus Torvalds } 42531da177e4SLinus Torvalds out: 42541da177e4SLinus Torvalds return ret; 42551da177e4SLinus Torvalds } 42561da177e4SLinus Torvalds 42571a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 42581da177e4SLinus Torvalds 42591da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 426067f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv6(struct sk_buff *skb, 42612bf49690SThomas Liu struct common_audit_data *ad, u8 *proto) 42621da177e4SLinus Torvalds { 42631da177e4SLinus Torvalds u8 nexthdr; 42641da177e4SLinus Torvalds int ret = -EINVAL, offset; 42651da177e4SLinus Torvalds struct ipv6hdr _ipv6h, *ip6; 426675f2811cSJesse Gross __be16 frag_off; 42671da177e4SLinus Torvalds 4268bbe735e4SArnaldo Carvalho de Melo offset = skb_network_offset(skb); 42691da177e4SLinus Torvalds ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h); 42701da177e4SLinus Torvalds if (ip6 == NULL) 42711da177e4SLinus Torvalds goto out; 42721da177e4SLinus Torvalds 427348c62af6SEric Paris ad->u.net->v6info.saddr = ip6->saddr; 427448c62af6SEric Paris ad->u.net->v6info.daddr = ip6->daddr; 42751da177e4SLinus Torvalds ret = 0; 42761da177e4SLinus Torvalds 42771da177e4SLinus Torvalds nexthdr = ip6->nexthdr; 42781da177e4SLinus Torvalds offset += sizeof(_ipv6h); 427975f2811cSJesse Gross offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off); 42801da177e4SLinus Torvalds if (offset < 0) 42811da177e4SLinus Torvalds goto out; 42821da177e4SLinus Torvalds 428367f83cbfSVenkat Yekkirala if (proto) 428467f83cbfSVenkat Yekkirala *proto = nexthdr; 428567f83cbfSVenkat Yekkirala 42861da177e4SLinus Torvalds switch (nexthdr) { 42871da177e4SLinus Torvalds case IPPROTO_TCP: { 42881da177e4SLinus Torvalds struct tcphdr _tcph, *th; 42891da177e4SLinus Torvalds 42901da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 42911da177e4SLinus Torvalds if (th == NULL) 42921da177e4SLinus Torvalds break; 42931da177e4SLinus Torvalds 429448c62af6SEric Paris ad->u.net->sport = th->source; 429548c62af6SEric Paris ad->u.net->dport = th->dest; 42961da177e4SLinus Torvalds break; 42971da177e4SLinus Torvalds } 42981da177e4SLinus Torvalds 42991da177e4SLinus Torvalds case IPPROTO_UDP: { 43001da177e4SLinus Torvalds struct udphdr _udph, *uh; 43011da177e4SLinus Torvalds 43021da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 43031da177e4SLinus Torvalds if (uh == NULL) 43041da177e4SLinus Torvalds break; 43051da177e4SLinus Torvalds 430648c62af6SEric Paris ad->u.net->sport = uh->source; 430748c62af6SEric Paris ad->u.net->dport = uh->dest; 43081da177e4SLinus Torvalds break; 43091da177e4SLinus Torvalds } 43101da177e4SLinus Torvalds 43112ee92d46SJames Morris case IPPROTO_DCCP: { 43122ee92d46SJames Morris struct dccp_hdr _dccph, *dh; 43132ee92d46SJames Morris 43142ee92d46SJames Morris dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); 43152ee92d46SJames Morris if (dh == NULL) 43162ee92d46SJames Morris break; 43172ee92d46SJames Morris 431848c62af6SEric Paris ad->u.net->sport = dh->dccph_sport; 431948c62af6SEric Paris ad->u.net->dport = dh->dccph_dport; 43202ee92d46SJames Morris break; 43212ee92d46SJames Morris } 43222ee92d46SJames Morris 4323d452930fSRichard Haines #if IS_ENABLED(CONFIG_IP_SCTP) 4324d452930fSRichard Haines case IPPROTO_SCTP: { 4325d452930fSRichard Haines struct sctphdr _sctph, *sh; 4326d452930fSRichard Haines 4327d452930fSRichard Haines sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph); 4328d452930fSRichard Haines if (sh == NULL) 4329d452930fSRichard Haines break; 4330d452930fSRichard Haines 4331d452930fSRichard Haines ad->u.net->sport = sh->source; 4332d452930fSRichard Haines ad->u.net->dport = sh->dest; 4333d452930fSRichard Haines break; 4334d452930fSRichard Haines } 4335d452930fSRichard Haines #endif 43361da177e4SLinus Torvalds /* includes fragments */ 43371da177e4SLinus Torvalds default: 43381da177e4SLinus Torvalds break; 43391da177e4SLinus Torvalds } 43401da177e4SLinus Torvalds out: 43411da177e4SLinus Torvalds return ret; 43421da177e4SLinus Torvalds } 43431da177e4SLinus Torvalds 43441da177e4SLinus Torvalds #endif /* IPV6 */ 43451da177e4SLinus Torvalds 43462bf49690SThomas Liu static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad, 4347cf9481e2SDavid Howells char **_addrp, int src, u8 *proto) 43481da177e4SLinus Torvalds { 4349cf9481e2SDavid Howells char *addrp; 4350cf9481e2SDavid Howells int ret; 43511da177e4SLinus Torvalds 435248c62af6SEric Paris switch (ad->u.net->family) { 43531da177e4SLinus Torvalds case PF_INET: 435467f83cbfSVenkat Yekkirala ret = selinux_parse_skb_ipv4(skb, ad, proto); 4355cf9481e2SDavid Howells if (ret) 4356cf9481e2SDavid Howells goto parse_error; 435748c62af6SEric Paris addrp = (char *)(src ? &ad->u.net->v4info.saddr : 435848c62af6SEric Paris &ad->u.net->v4info.daddr); 4359cf9481e2SDavid Howells goto okay; 43601da177e4SLinus Torvalds 43611a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 43621da177e4SLinus Torvalds case PF_INET6: 436367f83cbfSVenkat Yekkirala ret = selinux_parse_skb_ipv6(skb, ad, proto); 4364cf9481e2SDavid Howells if (ret) 4365cf9481e2SDavid Howells goto parse_error; 436648c62af6SEric Paris addrp = (char *)(src ? &ad->u.net->v6info.saddr : 436748c62af6SEric Paris &ad->u.net->v6info.daddr); 4368cf9481e2SDavid Howells goto okay; 43691da177e4SLinus Torvalds #endif /* IPV6 */ 43701da177e4SLinus Torvalds default: 4371cf9481e2SDavid Howells addrp = NULL; 4372cf9481e2SDavid Howells goto okay; 43731da177e4SLinus Torvalds } 43741da177e4SLinus Torvalds 4375cf9481e2SDavid Howells parse_error: 4376c103a91eSpeter enderborg pr_warn( 437771f1cb05SPaul Moore "SELinux: failure in selinux_parse_skb()," 437871f1cb05SPaul Moore " unable to parse packet\n"); 43791da177e4SLinus Torvalds return ret; 4380cf9481e2SDavid Howells 4381cf9481e2SDavid Howells okay: 4382cf9481e2SDavid Howells if (_addrp) 4383cf9481e2SDavid Howells *_addrp = addrp; 4384cf9481e2SDavid Howells return 0; 43851da177e4SLinus Torvalds } 43861da177e4SLinus Torvalds 43874f6a993fSPaul Moore /** 4388220deb96SPaul Moore * selinux_skb_peerlbl_sid - Determine the peer label of a packet 43894f6a993fSPaul Moore * @skb: the packet 439075e22910SPaul Moore * @family: protocol family 4391220deb96SPaul Moore * @sid: the packet's peer label SID 43924f6a993fSPaul Moore * 43934f6a993fSPaul Moore * Description: 4394220deb96SPaul Moore * Check the various different forms of network peer labeling and determine 4395220deb96SPaul Moore * the peer label/SID for the packet; most of the magic actually occurs in 4396220deb96SPaul Moore * the security server function security_net_peersid_cmp(). The function 4397220deb96SPaul Moore * returns zero if the value in @sid is valid (although it may be SECSID_NULL) 4398220deb96SPaul Moore * or -EACCES if @sid is invalid due to inconsistencies with the different 4399220deb96SPaul Moore * peer labels. 44004f6a993fSPaul Moore * 44014f6a993fSPaul Moore */ 4402220deb96SPaul Moore static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid) 44034f6a993fSPaul Moore { 440471f1cb05SPaul Moore int err; 44054f6a993fSPaul Moore u32 xfrm_sid; 44064f6a993fSPaul Moore u32 nlbl_sid; 4407220deb96SPaul Moore u32 nlbl_type; 44084f6a993fSPaul Moore 4409817eff71SPaul Moore err = selinux_xfrm_skb_sid(skb, &xfrm_sid); 4410bed4d7efSPaul Moore if (unlikely(err)) 4411bed4d7efSPaul Moore return -EACCES; 4412bed4d7efSPaul Moore err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid); 4413bed4d7efSPaul Moore if (unlikely(err)) 4414bed4d7efSPaul Moore return -EACCES; 4415220deb96SPaul Moore 4416aa8e712cSStephen Smalley err = security_net_peersid_resolve(&selinux_state, nlbl_sid, 4417aa8e712cSStephen Smalley nlbl_type, xfrm_sid, sid); 441871f1cb05SPaul Moore if (unlikely(err)) { 4419c103a91eSpeter enderborg pr_warn( 442071f1cb05SPaul Moore "SELinux: failure in selinux_skb_peerlbl_sid()," 442171f1cb05SPaul Moore " unable to determine packet's peer label\n"); 4422220deb96SPaul Moore return -EACCES; 442371f1cb05SPaul Moore } 4424220deb96SPaul Moore 4425220deb96SPaul Moore return 0; 44264f6a993fSPaul Moore } 44274f6a993fSPaul Moore 4428446b8024SPaul Moore /** 4429446b8024SPaul Moore * selinux_conn_sid - Determine the child socket label for a connection 4430446b8024SPaul Moore * @sk_sid: the parent socket's SID 4431446b8024SPaul Moore * @skb_sid: the packet's SID 4432446b8024SPaul Moore * @conn_sid: the resulting connection SID 4433446b8024SPaul Moore * 4434446b8024SPaul Moore * If @skb_sid is valid then the user:role:type information from @sk_sid is 4435446b8024SPaul Moore * combined with the MLS information from @skb_sid in order to create 4436446b8024SPaul Moore * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy 4437446b8024SPaul Moore * of @sk_sid. Returns zero on success, negative values on failure. 4438446b8024SPaul Moore * 4439446b8024SPaul Moore */ 4440446b8024SPaul Moore static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid) 4441446b8024SPaul Moore { 4442446b8024SPaul Moore int err = 0; 4443446b8024SPaul Moore 4444446b8024SPaul Moore if (skb_sid != SECSID_NULL) 4445aa8e712cSStephen Smalley err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid, 4446aa8e712cSStephen Smalley conn_sid); 4447446b8024SPaul Moore else 4448446b8024SPaul Moore *conn_sid = sk_sid; 4449446b8024SPaul Moore 4450446b8024SPaul Moore return err; 4451446b8024SPaul Moore } 4452446b8024SPaul Moore 44531da177e4SLinus Torvalds /* socket security operations */ 4454d4f2d978SPaul Moore 44552ad18bdfSHarry Ciao static int socket_sockcreate_sid(const struct task_security_struct *tsec, 44562ad18bdfSHarry Ciao u16 secclass, u32 *socksid) 4457d4f2d978SPaul Moore { 44582ad18bdfSHarry Ciao if (tsec->sockcreate_sid > SECSID_NULL) { 44592ad18bdfSHarry Ciao *socksid = tsec->sockcreate_sid; 44602ad18bdfSHarry Ciao return 0; 44612ad18bdfSHarry Ciao } 44622ad18bdfSHarry Ciao 4463aa8e712cSStephen Smalley return security_transition_sid(&selinux_state, tsec->sid, tsec->sid, 4464aa8e712cSStephen Smalley secclass, NULL, socksid); 4465d4f2d978SPaul Moore } 4466d4f2d978SPaul Moore 4467be0554c9SStephen Smalley static int sock_has_perm(struct sock *sk, u32 perms) 44681da177e4SLinus Torvalds { 4469253bfae6SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 44702bf49690SThomas Liu struct common_audit_data ad; 447148c62af6SEric Paris struct lsm_network_audit net = {0,}; 44721da177e4SLinus Torvalds 4473253bfae6SPaul Moore if (sksec->sid == SECINITSID_KERNEL) 4474253bfae6SPaul Moore return 0; 44751da177e4SLinus Torvalds 447650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 447748c62af6SEric Paris ad.u.net = &net; 447848c62af6SEric Paris ad.u.net->sk = sk; 44791da177e4SLinus Torvalds 44806b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 44816b6bc620SStephen Smalley current_sid(), sksec->sid, sksec->sclass, perms, 4482be0554c9SStephen Smalley &ad); 44831da177e4SLinus Torvalds } 44841da177e4SLinus Torvalds 44851da177e4SLinus Torvalds static int selinux_socket_create(int family, int type, 44861da177e4SLinus Torvalds int protocol, int kern) 44871da177e4SLinus Torvalds { 44880c6cfa62SCasey Schaufler const struct task_security_struct *tsec = selinux_cred(current_cred()); 4489d4f2d978SPaul Moore u32 newsid; 4490275bb41eSDavid Howells u16 secclass; 44912ad18bdfSHarry Ciao int rc; 44921da177e4SLinus Torvalds 44931da177e4SLinus Torvalds if (kern) 4494d4f2d978SPaul Moore return 0; 44951da177e4SLinus Torvalds 4496275bb41eSDavid Howells secclass = socket_type_to_security_class(family, type, protocol); 44972ad18bdfSHarry Ciao rc = socket_sockcreate_sid(tsec, secclass, &newsid); 44982ad18bdfSHarry Ciao if (rc) 44992ad18bdfSHarry Ciao return rc; 45002ad18bdfSHarry Ciao 45016b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 45026b6bc620SStephen Smalley tsec->sid, newsid, secclass, SOCKET__CREATE, NULL); 45031da177e4SLinus Torvalds } 45041da177e4SLinus Torvalds 45057420ed23SVenkat Yekkirala static int selinux_socket_post_create(struct socket *sock, int family, 45061da177e4SLinus Torvalds int type, int protocol, int kern) 45071da177e4SLinus Torvalds { 45080c6cfa62SCasey Schaufler const struct task_security_struct *tsec = selinux_cred(current_cred()); 45095d226df4SAndreas Gruenbacher struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock)); 4510892c141eSVenkat Yekkirala struct sk_security_struct *sksec; 45119287aed2SAndreas Gruenbacher u16 sclass = socket_type_to_security_class(family, type, protocol); 45129287aed2SAndreas Gruenbacher u32 sid = SECINITSID_KERNEL; 4513275bb41eSDavid Howells int err = 0; 4514275bb41eSDavid Howells 45159287aed2SAndreas Gruenbacher if (!kern) { 45169287aed2SAndreas Gruenbacher err = socket_sockcreate_sid(tsec, sclass, &sid); 45172ad18bdfSHarry Ciao if (err) 45182ad18bdfSHarry Ciao return err; 45192ad18bdfSHarry Ciao } 4520275bb41eSDavid Howells 45219287aed2SAndreas Gruenbacher isec->sclass = sclass; 45229287aed2SAndreas Gruenbacher isec->sid = sid; 45236f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 45241da177e4SLinus Torvalds 4525892c141eSVenkat Yekkirala if (sock->sk) { 4526892c141eSVenkat Yekkirala sksec = sock->sk->sk_security; 45279287aed2SAndreas Gruenbacher sksec->sclass = sclass; 45289287aed2SAndreas Gruenbacher sksec->sid = sid; 4529d452930fSRichard Haines /* Allows detection of the first association on this socket */ 4530d452930fSRichard Haines if (sksec->sclass == SECCLASS_SCTP_SOCKET) 4531d452930fSRichard Haines sksec->sctp_assoc_state = SCTP_ASSOC_UNSET; 4532d452930fSRichard Haines 4533389fb800SPaul Moore err = selinux_netlbl_socket_post_create(sock->sk, family); 4534892c141eSVenkat Yekkirala } 4535892c141eSVenkat Yekkirala 45367420ed23SVenkat Yekkirala return err; 45371da177e4SLinus Torvalds } 45381da177e4SLinus Torvalds 45390b811db2SDavid Herrmann static int selinux_socket_socketpair(struct socket *socka, 45400b811db2SDavid Herrmann struct socket *sockb) 45410b811db2SDavid Herrmann { 45420b811db2SDavid Herrmann struct sk_security_struct *sksec_a = socka->sk->sk_security; 45430b811db2SDavid Herrmann struct sk_security_struct *sksec_b = sockb->sk->sk_security; 45440b811db2SDavid Herrmann 45450b811db2SDavid Herrmann sksec_a->peer_sid = sksec_b->sid; 45460b811db2SDavid Herrmann sksec_b->peer_sid = sksec_a->sid; 45470b811db2SDavid Herrmann 45480b811db2SDavid Herrmann return 0; 45490b811db2SDavid Herrmann } 45500b811db2SDavid Herrmann 45511da177e4SLinus Torvalds /* Range of port numbers used to automatically bind. 45521da177e4SLinus Torvalds Need to determine whether we should perform a name_bind 45531da177e4SLinus Torvalds permission check between the socket and the port number. */ 45541da177e4SLinus Torvalds 45551da177e4SLinus Torvalds static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen) 45561da177e4SLinus Torvalds { 4557253bfae6SPaul Moore struct sock *sk = sock->sk; 45580f8db8ccSAlexey Kodanev struct sk_security_struct *sksec = sk->sk_security; 45591da177e4SLinus Torvalds u16 family; 45601da177e4SLinus Torvalds int err; 45611da177e4SLinus Torvalds 4562be0554c9SStephen Smalley err = sock_has_perm(sk, SOCKET__BIND); 45631da177e4SLinus Torvalds if (err) 45641da177e4SLinus Torvalds goto out; 45651da177e4SLinus Torvalds 4566d452930fSRichard Haines /* If PF_INET or PF_INET6, check name_bind permission for the port. */ 4567253bfae6SPaul Moore family = sk->sk_family; 45681da177e4SLinus Torvalds if (family == PF_INET || family == PF_INET6) { 45691da177e4SLinus Torvalds char *addrp; 45702bf49690SThomas Liu struct common_audit_data ad; 457148c62af6SEric Paris struct lsm_network_audit net = {0,}; 45721da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 45731da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 4574c750e692STetsuo Handa u16 family_sa; 45751da177e4SLinus Torvalds unsigned short snum; 4576e399f982SJames Morris u32 sid, node_perm; 45771da177e4SLinus Torvalds 4578d452930fSRichard Haines /* 4579d452930fSRichard Haines * sctp_bindx(3) calls via selinux_sctp_bind_connect() 4580d452930fSRichard Haines * that validates multiple binding addresses. Because of this 4581d452930fSRichard Haines * need to check address->sa_family as it is possible to have 4582d452930fSRichard Haines * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET. 4583d452930fSRichard Haines */ 4584c750e692STetsuo Handa if (addrlen < offsetofend(struct sockaddr, sa_family)) 4585c750e692STetsuo Handa return -EINVAL; 4586c750e692STetsuo Handa family_sa = address->sa_family; 45870f8db8ccSAlexey Kodanev switch (family_sa) { 45880f8db8ccSAlexey Kodanev case AF_UNSPEC: 458968741a8aSRichard Haines case AF_INET: 459068741a8aSRichard Haines if (addrlen < sizeof(struct sockaddr_in)) 459168741a8aSRichard Haines return -EINVAL; 45921da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 45930f8db8ccSAlexey Kodanev if (family_sa == AF_UNSPEC) { 45940f8db8ccSAlexey Kodanev /* see __inet_bind(), we only want to allow 45950f8db8ccSAlexey Kodanev * AF_UNSPEC if the address is INADDR_ANY 45960f8db8ccSAlexey Kodanev */ 45970f8db8ccSAlexey Kodanev if (addr4->sin_addr.s_addr != htonl(INADDR_ANY)) 45980f8db8ccSAlexey Kodanev goto err_af; 45990f8db8ccSAlexey Kodanev family_sa = AF_INET; 46000f8db8ccSAlexey Kodanev } 46011da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 46021da177e4SLinus Torvalds addrp = (char *)&addr4->sin_addr.s_addr; 460368741a8aSRichard Haines break; 460468741a8aSRichard Haines case AF_INET6: 460568741a8aSRichard Haines if (addrlen < SIN6_LEN_RFC2133) 460668741a8aSRichard Haines return -EINVAL; 46071da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 46081da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 46091da177e4SLinus Torvalds addrp = (char *)&addr6->sin6_addr.s6_addr; 461068741a8aSRichard Haines break; 461168741a8aSRichard Haines default: 46120f8db8ccSAlexey Kodanev goto err_af; 46131da177e4SLinus Torvalds } 46141da177e4SLinus Torvalds 461588b7d370SAlexey Kodanev ad.type = LSM_AUDIT_DATA_NET; 461688b7d370SAlexey Kodanev ad.u.net = &net; 461788b7d370SAlexey Kodanev ad.u.net->sport = htons(snum); 461888b7d370SAlexey Kodanev ad.u.net->family = family_sa; 461988b7d370SAlexey Kodanev 4620227b60f5SStephen Hemminger if (snum) { 4621227b60f5SStephen Hemminger int low, high; 4622227b60f5SStephen Hemminger 46230bbf87d8SEric W. Biederman inet_get_local_port_range(sock_net(sk), &low, &high); 4624227b60f5SStephen Hemminger 462582f31ebfSMaciej Żenczykowski if (inet_port_requires_bind_service(sock_net(sk), snum) || 462682f31ebfSMaciej Żenczykowski snum < low || snum > high) { 46273e112172SPaul Moore err = sel_netport_sid(sk->sk_protocol, 46283e112172SPaul Moore snum, &sid); 46291da177e4SLinus Torvalds if (err) 46301da177e4SLinus Torvalds goto out; 46316b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 46326b6bc620SStephen Smalley sksec->sid, sid, 4633253bfae6SPaul Moore sksec->sclass, 46341da177e4SLinus Torvalds SOCKET__NAME_BIND, &ad); 46351da177e4SLinus Torvalds if (err) 46361da177e4SLinus Torvalds goto out; 46371da177e4SLinus Torvalds } 4638227b60f5SStephen Hemminger } 46391da177e4SLinus Torvalds 4640253bfae6SPaul Moore switch (sksec->sclass) { 464113402580SJames Morris case SECCLASS_TCP_SOCKET: 46421da177e4SLinus Torvalds node_perm = TCP_SOCKET__NODE_BIND; 46431da177e4SLinus Torvalds break; 46441da177e4SLinus Torvalds 464513402580SJames Morris case SECCLASS_UDP_SOCKET: 46461da177e4SLinus Torvalds node_perm = UDP_SOCKET__NODE_BIND; 46471da177e4SLinus Torvalds break; 46481da177e4SLinus Torvalds 46492ee92d46SJames Morris case SECCLASS_DCCP_SOCKET: 46502ee92d46SJames Morris node_perm = DCCP_SOCKET__NODE_BIND; 46512ee92d46SJames Morris break; 46522ee92d46SJames Morris 4653d452930fSRichard Haines case SECCLASS_SCTP_SOCKET: 4654d452930fSRichard Haines node_perm = SCTP_SOCKET__NODE_BIND; 4655d452930fSRichard Haines break; 4656d452930fSRichard Haines 46571da177e4SLinus Torvalds default: 46581da177e4SLinus Torvalds node_perm = RAWIP_SOCKET__NODE_BIND; 46591da177e4SLinus Torvalds break; 46601da177e4SLinus Torvalds } 46611da177e4SLinus Torvalds 466288b7d370SAlexey Kodanev err = sel_netnode_sid(addrp, family_sa, &sid); 46631da177e4SLinus Torvalds if (err) 46641da177e4SLinus Torvalds goto out; 46651da177e4SLinus Torvalds 46660f8db8ccSAlexey Kodanev if (family_sa == AF_INET) 466748c62af6SEric Paris ad.u.net->v4info.saddr = addr4->sin_addr.s_addr; 46681da177e4SLinus Torvalds else 466948c62af6SEric Paris ad.u.net->v6info.saddr = addr6->sin6_addr; 46701da177e4SLinus Torvalds 46716b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 46726b6bc620SStephen Smalley sksec->sid, sid, 4673253bfae6SPaul Moore sksec->sclass, node_perm, &ad); 46741da177e4SLinus Torvalds if (err) 46751da177e4SLinus Torvalds goto out; 46761da177e4SLinus Torvalds } 46771da177e4SLinus Torvalds out: 46781da177e4SLinus Torvalds return err; 46790f8db8ccSAlexey Kodanev err_af: 46800f8db8ccSAlexey Kodanev /* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */ 46810f8db8ccSAlexey Kodanev if (sksec->sclass == SECCLASS_SCTP_SOCKET) 46820f8db8ccSAlexey Kodanev return -EINVAL; 46830f8db8ccSAlexey Kodanev return -EAFNOSUPPORT; 46841da177e4SLinus Torvalds } 46851da177e4SLinus Torvalds 4686d452930fSRichard Haines /* This supports connect(2) and SCTP connect services such as sctp_connectx(3) 4687d61330c6SKees Cook * and sctp_sendmsg(3) as described in Documentation/security/SCTP.rst 4688d452930fSRichard Haines */ 4689d452930fSRichard Haines static int selinux_socket_connect_helper(struct socket *sock, 4690d452930fSRichard Haines struct sockaddr *address, int addrlen) 46911da177e4SLinus Torvalds { 4692014ab19aSPaul Moore struct sock *sk = sock->sk; 4693253bfae6SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 46941da177e4SLinus Torvalds int err; 46951da177e4SLinus Torvalds 4696be0554c9SStephen Smalley err = sock_has_perm(sk, SOCKET__CONNECT); 46971da177e4SLinus Torvalds if (err) 46981da177e4SLinus Torvalds return err; 469905174c95SPaolo Abeni if (addrlen < offsetofend(struct sockaddr, sa_family)) 470005174c95SPaolo Abeni return -EINVAL; 470105174c95SPaolo Abeni 470205174c95SPaolo Abeni /* connect(AF_UNSPEC) has special handling, as it is a documented 470305174c95SPaolo Abeni * way to disconnect the socket 470405174c95SPaolo Abeni */ 470505174c95SPaolo Abeni if (address->sa_family == AF_UNSPEC) 470605174c95SPaolo Abeni return 0; 47071da177e4SLinus Torvalds 47081da177e4SLinus Torvalds /* 4709d452930fSRichard Haines * If a TCP, DCCP or SCTP socket, check name_connect permission 4710d452930fSRichard Haines * for the port. 47111da177e4SLinus Torvalds */ 4712253bfae6SPaul Moore if (sksec->sclass == SECCLASS_TCP_SOCKET || 4713d452930fSRichard Haines sksec->sclass == SECCLASS_DCCP_SOCKET || 4714d452930fSRichard Haines sksec->sclass == SECCLASS_SCTP_SOCKET) { 47152bf49690SThomas Liu struct common_audit_data ad; 471648c62af6SEric Paris struct lsm_network_audit net = {0,}; 47171da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 47181da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 47191da177e4SLinus Torvalds unsigned short snum; 47202ee92d46SJames Morris u32 sid, perm; 47211da177e4SLinus Torvalds 4722d452930fSRichard Haines /* sctp_connectx(3) calls via selinux_sctp_bind_connect() 4723d452930fSRichard Haines * that validates multiple connect addresses. Because of this 4724d452930fSRichard Haines * need to check address->sa_family as it is possible to have 4725d452930fSRichard Haines * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET. 4726d452930fSRichard Haines */ 472768741a8aSRichard Haines switch (address->sa_family) { 472868741a8aSRichard Haines case AF_INET: 47291da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 4730911656f8SStephen Smalley if (addrlen < sizeof(struct sockaddr_in)) 47311da177e4SLinus Torvalds return -EINVAL; 47321da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 473368741a8aSRichard Haines break; 473468741a8aSRichard Haines case AF_INET6: 47351da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 4736911656f8SStephen Smalley if (addrlen < SIN6_LEN_RFC2133) 47371da177e4SLinus Torvalds return -EINVAL; 47381da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 473968741a8aSRichard Haines break; 474068741a8aSRichard Haines default: 474168741a8aSRichard Haines /* Note that SCTP services expect -EINVAL, whereas 474268741a8aSRichard Haines * others expect -EAFNOSUPPORT. 474368741a8aSRichard Haines */ 474468741a8aSRichard Haines if (sksec->sclass == SECCLASS_SCTP_SOCKET) 474568741a8aSRichard Haines return -EINVAL; 474668741a8aSRichard Haines else 474768741a8aSRichard Haines return -EAFNOSUPPORT; 47481da177e4SLinus Torvalds } 47491da177e4SLinus Torvalds 47503e112172SPaul Moore err = sel_netport_sid(sk->sk_protocol, snum, &sid); 47511da177e4SLinus Torvalds if (err) 4752d452930fSRichard Haines return err; 47531da177e4SLinus Torvalds 4754d452930fSRichard Haines switch (sksec->sclass) { 4755d452930fSRichard Haines case SECCLASS_TCP_SOCKET: 4756d452930fSRichard Haines perm = TCP_SOCKET__NAME_CONNECT; 4757d452930fSRichard Haines break; 4758d452930fSRichard Haines case SECCLASS_DCCP_SOCKET: 4759d452930fSRichard Haines perm = DCCP_SOCKET__NAME_CONNECT; 4760d452930fSRichard Haines break; 4761d452930fSRichard Haines case SECCLASS_SCTP_SOCKET: 4762d452930fSRichard Haines perm = SCTP_SOCKET__NAME_CONNECT; 4763d452930fSRichard Haines break; 4764d452930fSRichard Haines } 47652ee92d46SJames Morris 476650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 476748c62af6SEric Paris ad.u.net = &net; 476848c62af6SEric Paris ad.u.net->dport = htons(snum); 476988b7d370SAlexey Kodanev ad.u.net->family = address->sa_family; 47706b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 47716b6bc620SStephen Smalley sksec->sid, sid, sksec->sclass, perm, &ad); 47721da177e4SLinus Torvalds if (err) 4773d452930fSRichard Haines return err; 47741da177e4SLinus Torvalds } 47751da177e4SLinus Torvalds 4776d452930fSRichard Haines return 0; 4777d452930fSRichard Haines } 4778014ab19aSPaul Moore 4779d452930fSRichard Haines /* Supports connect(2), see comments in selinux_socket_connect_helper() */ 4780d452930fSRichard Haines static int selinux_socket_connect(struct socket *sock, 4781d452930fSRichard Haines struct sockaddr *address, int addrlen) 4782d452930fSRichard Haines { 4783d452930fSRichard Haines int err; 4784d452930fSRichard Haines struct sock *sk = sock->sk; 4785d452930fSRichard Haines 4786d452930fSRichard Haines err = selinux_socket_connect_helper(sock, address, addrlen); 4787d452930fSRichard Haines if (err) 47881da177e4SLinus Torvalds return err; 4789d452930fSRichard Haines 4790d452930fSRichard Haines return selinux_netlbl_socket_connect(sk, address); 47911da177e4SLinus Torvalds } 47921da177e4SLinus Torvalds 47931da177e4SLinus Torvalds static int selinux_socket_listen(struct socket *sock, int backlog) 47941da177e4SLinus Torvalds { 4795be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__LISTEN); 47961da177e4SLinus Torvalds } 47971da177e4SLinus Torvalds 47981da177e4SLinus Torvalds static int selinux_socket_accept(struct socket *sock, struct socket *newsock) 47991da177e4SLinus Torvalds { 48001da177e4SLinus Torvalds int err; 48011da177e4SLinus Torvalds struct inode_security_struct *isec; 48021da177e4SLinus Torvalds struct inode_security_struct *newisec; 48039287aed2SAndreas Gruenbacher u16 sclass; 48049287aed2SAndreas Gruenbacher u32 sid; 48051da177e4SLinus Torvalds 4806be0554c9SStephen Smalley err = sock_has_perm(sock->sk, SOCKET__ACCEPT); 48071da177e4SLinus Torvalds if (err) 48081da177e4SLinus Torvalds return err; 48091da177e4SLinus Torvalds 48105d226df4SAndreas Gruenbacher isec = inode_security_novalidate(SOCK_INODE(sock)); 48119287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 48129287aed2SAndreas Gruenbacher sclass = isec->sclass; 48139287aed2SAndreas Gruenbacher sid = isec->sid; 48149287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 48159287aed2SAndreas Gruenbacher 48169287aed2SAndreas Gruenbacher newisec = inode_security_novalidate(SOCK_INODE(newsock)); 48179287aed2SAndreas Gruenbacher newisec->sclass = sclass; 48189287aed2SAndreas Gruenbacher newisec->sid = sid; 48196f3be9f5SAndreas Gruenbacher newisec->initialized = LABEL_INITIALIZED; 48201da177e4SLinus Torvalds 48211da177e4SLinus Torvalds return 0; 48221da177e4SLinus Torvalds } 48231da177e4SLinus Torvalds 48241da177e4SLinus Torvalds static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg, 48251da177e4SLinus Torvalds int size) 48261da177e4SLinus Torvalds { 4827be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__WRITE); 48281da177e4SLinus Torvalds } 48291da177e4SLinus Torvalds 48301da177e4SLinus Torvalds static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg, 48311da177e4SLinus Torvalds int size, int flags) 48321da177e4SLinus Torvalds { 4833be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__READ); 48341da177e4SLinus Torvalds } 48351da177e4SLinus Torvalds 48361da177e4SLinus Torvalds static int selinux_socket_getsockname(struct socket *sock) 48371da177e4SLinus Torvalds { 4838be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__GETATTR); 48391da177e4SLinus Torvalds } 48401da177e4SLinus Torvalds 48411da177e4SLinus Torvalds static int selinux_socket_getpeername(struct socket *sock) 48421da177e4SLinus Torvalds { 4843be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__GETATTR); 48441da177e4SLinus Torvalds } 48451da177e4SLinus Torvalds 48461da177e4SLinus Torvalds static int selinux_socket_setsockopt(struct socket *sock, int level, int optname) 48471da177e4SLinus Torvalds { 4848f8687afeSPaul Moore int err; 4849f8687afeSPaul Moore 4850be0554c9SStephen Smalley err = sock_has_perm(sock->sk, SOCKET__SETOPT); 4851f8687afeSPaul Moore if (err) 4852f8687afeSPaul Moore return err; 4853f8687afeSPaul Moore 4854f8687afeSPaul Moore return selinux_netlbl_socket_setsockopt(sock, level, optname); 48551da177e4SLinus Torvalds } 48561da177e4SLinus Torvalds 48571da177e4SLinus Torvalds static int selinux_socket_getsockopt(struct socket *sock, int level, 48581da177e4SLinus Torvalds int optname) 48591da177e4SLinus Torvalds { 4860be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__GETOPT); 48611da177e4SLinus Torvalds } 48621da177e4SLinus Torvalds 48631da177e4SLinus Torvalds static int selinux_socket_shutdown(struct socket *sock, int how) 48641da177e4SLinus Torvalds { 4865be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__SHUTDOWN); 48661da177e4SLinus Torvalds } 48671da177e4SLinus Torvalds 48683610cda5SDavid S. Miller static int selinux_socket_unix_stream_connect(struct sock *sock, 48693610cda5SDavid S. Miller struct sock *other, 48701da177e4SLinus Torvalds struct sock *newsk) 48711da177e4SLinus Torvalds { 48723610cda5SDavid S. Miller struct sk_security_struct *sksec_sock = sock->sk_security; 48733610cda5SDavid S. Miller struct sk_security_struct *sksec_other = other->sk_security; 48744d1e2451SPaul Moore struct sk_security_struct *sksec_new = newsk->sk_security; 48752bf49690SThomas Liu struct common_audit_data ad; 487648c62af6SEric Paris struct lsm_network_audit net = {0,}; 48771da177e4SLinus Torvalds int err; 48781da177e4SLinus Torvalds 487950c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 488048c62af6SEric Paris ad.u.net = &net; 488148c62af6SEric Paris ad.u.net->sk = other; 48821da177e4SLinus Torvalds 48836b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 48846b6bc620SStephen Smalley sksec_sock->sid, sksec_other->sid, 48854d1e2451SPaul Moore sksec_other->sclass, 48861da177e4SLinus Torvalds UNIX_STREAM_SOCKET__CONNECTTO, &ad); 48871da177e4SLinus Torvalds if (err) 48881da177e4SLinus Torvalds return err; 48891da177e4SLinus Torvalds 48901da177e4SLinus Torvalds /* server child socket */ 48914d1e2451SPaul Moore sksec_new->peer_sid = sksec_sock->sid; 4892aa8e712cSStephen Smalley err = security_sid_mls_copy(&selinux_state, sksec_other->sid, 4893aa8e712cSStephen Smalley sksec_sock->sid, &sksec_new->sid); 48944d1e2451SPaul Moore if (err) 48954237c75cSVenkat Yekkirala return err; 48964d1e2451SPaul Moore 48974d1e2451SPaul Moore /* connecting socket */ 48984d1e2451SPaul Moore sksec_sock->peer_sid = sksec_new->sid; 48994d1e2451SPaul Moore 49004d1e2451SPaul Moore return 0; 49011da177e4SLinus Torvalds } 49021da177e4SLinus Torvalds 49031da177e4SLinus Torvalds static int selinux_socket_unix_may_send(struct socket *sock, 49041da177e4SLinus Torvalds struct socket *other) 49051da177e4SLinus Torvalds { 4906253bfae6SPaul Moore struct sk_security_struct *ssec = sock->sk->sk_security; 4907253bfae6SPaul Moore struct sk_security_struct *osec = other->sk->sk_security; 49082bf49690SThomas Liu struct common_audit_data ad; 490948c62af6SEric Paris struct lsm_network_audit net = {0,}; 49101da177e4SLinus Torvalds 491150c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 491248c62af6SEric Paris ad.u.net = &net; 491348c62af6SEric Paris ad.u.net->sk = other->sk; 49141da177e4SLinus Torvalds 49156b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 49166b6bc620SStephen Smalley ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO, 4917253bfae6SPaul Moore &ad); 49181da177e4SLinus Torvalds } 49191da177e4SLinus Torvalds 4920cbe0d6e8SPaul Moore static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex, 4921cbe0d6e8SPaul Moore char *addrp, u16 family, u32 peer_sid, 49222bf49690SThomas Liu struct common_audit_data *ad) 4923effad8dfSPaul Moore { 4924effad8dfSPaul Moore int err; 4925effad8dfSPaul Moore u32 if_sid; 4926effad8dfSPaul Moore u32 node_sid; 4927effad8dfSPaul Moore 4928cbe0d6e8SPaul Moore err = sel_netif_sid(ns, ifindex, &if_sid); 4929effad8dfSPaul Moore if (err) 4930effad8dfSPaul Moore return err; 49316b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 49326b6bc620SStephen Smalley peer_sid, if_sid, 4933effad8dfSPaul Moore SECCLASS_NETIF, NETIF__INGRESS, ad); 4934effad8dfSPaul Moore if (err) 4935effad8dfSPaul Moore return err; 4936effad8dfSPaul Moore 4937effad8dfSPaul Moore err = sel_netnode_sid(addrp, family, &node_sid); 4938effad8dfSPaul Moore if (err) 4939effad8dfSPaul Moore return err; 49406b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 49416b6bc620SStephen Smalley peer_sid, node_sid, 4942effad8dfSPaul Moore SECCLASS_NODE, NODE__RECVFROM, ad); 4943effad8dfSPaul Moore } 4944effad8dfSPaul Moore 4945220deb96SPaul Moore static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb, 4946d8395c87SPaul Moore u16 family) 4947220deb96SPaul Moore { 4948277d342fSPaul Moore int err = 0; 4949220deb96SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 4950220deb96SPaul Moore u32 sk_sid = sksec->sid; 49512bf49690SThomas Liu struct common_audit_data ad; 495248c62af6SEric Paris struct lsm_network_audit net = {0,}; 4953d8395c87SPaul Moore char *addrp; 4954d8395c87SPaul Moore 495550c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 495648c62af6SEric Paris ad.u.net = &net; 495748c62af6SEric Paris ad.u.net->netif = skb->skb_iif; 495848c62af6SEric Paris ad.u.net->family = family; 4959d8395c87SPaul Moore err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); 4960d8395c87SPaul Moore if (err) 4961d8395c87SPaul Moore return err; 4962220deb96SPaul Moore 496358bfbb51SPaul Moore if (selinux_secmark_enabled()) { 49646b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 49656b6bc620SStephen Smalley sk_sid, skb->secmark, SECCLASS_PACKET, 4966d8395c87SPaul Moore PACKET__RECV, &ad); 4967220deb96SPaul Moore if (err) 4968220deb96SPaul Moore return err; 496958bfbb51SPaul Moore } 4970220deb96SPaul Moore 4971d8395c87SPaul Moore err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad); 4972220deb96SPaul Moore if (err) 4973220deb96SPaul Moore return err; 4974d8395c87SPaul Moore err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad); 4975220deb96SPaul Moore 49764e5ab4cbSJames Morris return err; 49774e5ab4cbSJames Morris } 4978d28d1e08STrent Jaeger 49794e5ab4cbSJames Morris static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) 49804e5ab4cbSJames Morris { 4981220deb96SPaul Moore int err; 49824237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 4983220deb96SPaul Moore u16 family = sk->sk_family; 4984220deb96SPaul Moore u32 sk_sid = sksec->sid; 49852bf49690SThomas Liu struct common_audit_data ad; 498648c62af6SEric Paris struct lsm_network_audit net = {0,}; 4987220deb96SPaul Moore char *addrp; 4988d8395c87SPaul Moore u8 secmark_active; 4989d8395c87SPaul Moore u8 peerlbl_active; 49904e5ab4cbSJames Morris 49914e5ab4cbSJames Morris if (family != PF_INET && family != PF_INET6) 4992220deb96SPaul Moore return 0; 49934e5ab4cbSJames Morris 49944e5ab4cbSJames Morris /* Handle mapped IPv4 packets arriving via IPv6 sockets */ 499587fcd70dSAl Viro if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 49964e5ab4cbSJames Morris family = PF_INET; 49974e5ab4cbSJames Morris 4998d8395c87SPaul Moore /* If any sort of compatibility mode is enabled then handoff processing 4999d8395c87SPaul Moore * to the selinux_sock_rcv_skb_compat() function to deal with the 5000d8395c87SPaul Moore * special handling. We do this in an attempt to keep this function 5001d8395c87SPaul Moore * as fast and as clean as possible. */ 5002aa8e712cSStephen Smalley if (!selinux_policycap_netpeer()) 5003d8395c87SPaul Moore return selinux_sock_rcv_skb_compat(sk, skb, family); 5004d8395c87SPaul Moore 5005d8395c87SPaul Moore secmark_active = selinux_secmark_enabled(); 50062be4d74fSChris PeBenito peerlbl_active = selinux_peerlbl_enabled(); 5007d8395c87SPaul Moore if (!secmark_active && !peerlbl_active) 5008d8395c87SPaul Moore return 0; 5009d8395c87SPaul Moore 501050c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 501148c62af6SEric Paris ad.u.net = &net; 501248c62af6SEric Paris ad.u.net->netif = skb->skb_iif; 501348c62af6SEric Paris ad.u.net->family = family; 5014224dfbd8SPaul Moore err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); 50154e5ab4cbSJames Morris if (err) 5016220deb96SPaul Moore return err; 50174e5ab4cbSJames Morris 5018d8395c87SPaul Moore if (peerlbl_active) { 5019d621d35eSPaul Moore u32 peer_sid; 5020220deb96SPaul Moore 5021220deb96SPaul Moore err = selinux_skb_peerlbl_sid(skb, family, &peer_sid); 5022220deb96SPaul Moore if (err) 5023220deb96SPaul Moore return err; 5024cbe0d6e8SPaul Moore err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif, 5025cbe0d6e8SPaul Moore addrp, family, peer_sid, &ad); 5026dfaebe98SPaul Moore if (err) { 5027a04e71f6SHuw Davies selinux_netlbl_err(skb, family, err, 0); 5028effad8dfSPaul Moore return err; 5029dfaebe98SPaul Moore } 50306b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 50316b6bc620SStephen Smalley sk_sid, peer_sid, SECCLASS_PEER, 5032d621d35eSPaul Moore PEER__RECV, &ad); 503346d01d63SChad Hanson if (err) { 5034a04e71f6SHuw Davies selinux_netlbl_err(skb, family, err, 0); 503546d01d63SChad Hanson return err; 503646d01d63SChad Hanson } 5037d621d35eSPaul Moore } 5038d621d35eSPaul Moore 5039d8395c87SPaul Moore if (secmark_active) { 50406b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 50416b6bc620SStephen Smalley sk_sid, skb->secmark, SECCLASS_PACKET, 5042effad8dfSPaul Moore PACKET__RECV, &ad); 5043effad8dfSPaul Moore if (err) 5044effad8dfSPaul Moore return err; 5045effad8dfSPaul Moore } 5046effad8dfSPaul Moore 5047d621d35eSPaul Moore return err; 50481da177e4SLinus Torvalds } 50491da177e4SLinus Torvalds 50502c7946a7SCatherine Zhang static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval, 50511da177e4SLinus Torvalds int __user *optlen, unsigned len) 50521da177e4SLinus Torvalds { 50531da177e4SLinus Torvalds int err = 0; 50541da177e4SLinus Torvalds char *scontext; 50551da177e4SLinus Torvalds u32 scontext_len; 5056253bfae6SPaul Moore struct sk_security_struct *sksec = sock->sk->sk_security; 50573de4bab5SPaul Moore u32 peer_sid = SECSID_NULL; 50581da177e4SLinus Torvalds 5059253bfae6SPaul Moore if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET || 5060d452930fSRichard Haines sksec->sclass == SECCLASS_TCP_SOCKET || 5061d452930fSRichard Haines sksec->sclass == SECCLASS_SCTP_SOCKET) 5062dd3e7836SEric Paris peer_sid = sksec->peer_sid; 5063253bfae6SPaul Moore if (peer_sid == SECSID_NULL) 5064253bfae6SPaul Moore return -ENOPROTOOPT; 50651da177e4SLinus Torvalds 5066aa8e712cSStephen Smalley err = security_sid_to_context(&selinux_state, peer_sid, &scontext, 5067aa8e712cSStephen Smalley &scontext_len); 50681da177e4SLinus Torvalds if (err) 5069253bfae6SPaul Moore return err; 50701da177e4SLinus Torvalds 50711da177e4SLinus Torvalds if (scontext_len > len) { 50721da177e4SLinus Torvalds err = -ERANGE; 50731da177e4SLinus Torvalds goto out_len; 50741da177e4SLinus Torvalds } 50751da177e4SLinus Torvalds 50761da177e4SLinus Torvalds if (copy_to_user(optval, scontext, scontext_len)) 50771da177e4SLinus Torvalds err = -EFAULT; 50781da177e4SLinus Torvalds 50791da177e4SLinus Torvalds out_len: 50801da177e4SLinus Torvalds if (put_user(scontext_len, optlen)) 50811da177e4SLinus Torvalds err = -EFAULT; 50821da177e4SLinus Torvalds kfree(scontext); 50831da177e4SLinus Torvalds return err; 50841da177e4SLinus Torvalds } 50851da177e4SLinus Torvalds 5086dc49c1f9SCatherine Zhang static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid) 50872c7946a7SCatherine Zhang { 5088dc49c1f9SCatherine Zhang u32 peer_secid = SECSID_NULL; 508975e22910SPaul Moore u16 family; 5090899134f2SPaul Moore struct inode_security_struct *isec; 5091877ce7c1SCatherine Zhang 5092aa862900SPaul Moore if (skb && skb->protocol == htons(ETH_P_IP)) 5093aa862900SPaul Moore family = PF_INET; 5094aa862900SPaul Moore else if (skb && skb->protocol == htons(ETH_P_IPV6)) 5095aa862900SPaul Moore family = PF_INET6; 5096aa862900SPaul Moore else if (sock) 509775e22910SPaul Moore family = sock->sk->sk_family; 509875e22910SPaul Moore else 509975e22910SPaul Moore goto out; 510075e22910SPaul Moore 5101899134f2SPaul Moore if (sock && family == PF_UNIX) { 5102899134f2SPaul Moore isec = inode_security_novalidate(SOCK_INODE(sock)); 5103899134f2SPaul Moore peer_secid = isec->sid; 5104899134f2SPaul Moore } else if (skb) 5105220deb96SPaul Moore selinux_skb_peerlbl_sid(skb, family, &peer_secid); 51062c7946a7SCatherine Zhang 510775e22910SPaul Moore out: 5108dc49c1f9SCatherine Zhang *secid = peer_secid; 510975e22910SPaul Moore if (peer_secid == SECSID_NULL) 511075e22910SPaul Moore return -EINVAL; 511175e22910SPaul Moore return 0; 51122c7946a7SCatherine Zhang } 51132c7946a7SCatherine Zhang 51147d877f3bSAl Viro static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority) 51151da177e4SLinus Torvalds { 511684914b7eSPaul Moore struct sk_security_struct *sksec; 511784914b7eSPaul Moore 511884914b7eSPaul Moore sksec = kzalloc(sizeof(*sksec), priority); 511984914b7eSPaul Moore if (!sksec) 512084914b7eSPaul Moore return -ENOMEM; 512184914b7eSPaul Moore 512284914b7eSPaul Moore sksec->peer_sid = SECINITSID_UNLABELED; 512384914b7eSPaul Moore sksec->sid = SECINITSID_UNLABELED; 51245dee25d0SStephen Smalley sksec->sclass = SECCLASS_SOCKET; 512584914b7eSPaul Moore selinux_netlbl_sk_security_reset(sksec); 512684914b7eSPaul Moore sk->sk_security = sksec; 512784914b7eSPaul Moore 512884914b7eSPaul Moore return 0; 51291da177e4SLinus Torvalds } 51301da177e4SLinus Torvalds 51311da177e4SLinus Torvalds static void selinux_sk_free_security(struct sock *sk) 51321da177e4SLinus Torvalds { 513384914b7eSPaul Moore struct sk_security_struct *sksec = sk->sk_security; 513484914b7eSPaul Moore 513584914b7eSPaul Moore sk->sk_security = NULL; 513684914b7eSPaul Moore selinux_netlbl_sk_security_free(sksec); 513784914b7eSPaul Moore kfree(sksec); 51381da177e4SLinus Torvalds } 51391da177e4SLinus Torvalds 5140892c141eSVenkat Yekkirala static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk) 5141892c141eSVenkat Yekkirala { 5142dd3e7836SEric Paris struct sk_security_struct *sksec = sk->sk_security; 5143dd3e7836SEric Paris struct sk_security_struct *newsksec = newsk->sk_security; 5144892c141eSVenkat Yekkirala 5145dd3e7836SEric Paris newsksec->sid = sksec->sid; 5146dd3e7836SEric Paris newsksec->peer_sid = sksec->peer_sid; 5147dd3e7836SEric Paris newsksec->sclass = sksec->sclass; 514899f59ed0SPaul Moore 5149dd3e7836SEric Paris selinux_netlbl_sk_security_reset(newsksec); 5150892c141eSVenkat Yekkirala } 5151892c141eSVenkat Yekkirala 5152beb8d13bSVenkat Yekkirala static void selinux_sk_getsecid(struct sock *sk, u32 *secid) 5153d28d1e08STrent Jaeger { 5154d28d1e08STrent Jaeger if (!sk) 5155beb8d13bSVenkat Yekkirala *secid = SECINITSID_ANY_SOCKET; 5156892c141eSVenkat Yekkirala else { 5157892c141eSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 5158d28d1e08STrent Jaeger 5159beb8d13bSVenkat Yekkirala *secid = sksec->sid; 5160892c141eSVenkat Yekkirala } 5161d28d1e08STrent Jaeger } 5162d28d1e08STrent Jaeger 51639a673e56SAdrian Bunk static void selinux_sock_graft(struct sock *sk, struct socket *parent) 51644237c75cSVenkat Yekkirala { 51655d226df4SAndreas Gruenbacher struct inode_security_struct *isec = 51665d226df4SAndreas Gruenbacher inode_security_novalidate(SOCK_INODE(parent)); 51674237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 51684237c75cSVenkat Yekkirala 51692873ead7SPaul Moore if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 || 51702873ead7SPaul Moore sk->sk_family == PF_UNIX) 51714237c75cSVenkat Yekkirala isec->sid = sksec->sid; 5172220deb96SPaul Moore sksec->sclass = isec->sclass; 51734237c75cSVenkat Yekkirala } 51744237c75cSVenkat Yekkirala 5175d452930fSRichard Haines /* Called whenever SCTP receives an INIT chunk. This happens when an incoming 5176d452930fSRichard Haines * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association 5177d452930fSRichard Haines * already present). 5178d452930fSRichard Haines */ 5179d452930fSRichard Haines static int selinux_sctp_assoc_request(struct sctp_endpoint *ep, 5180d452930fSRichard Haines struct sk_buff *skb) 5181d452930fSRichard Haines { 5182d452930fSRichard Haines struct sk_security_struct *sksec = ep->base.sk->sk_security; 5183d452930fSRichard Haines struct common_audit_data ad; 5184d452930fSRichard Haines struct lsm_network_audit net = {0,}; 5185d452930fSRichard Haines u8 peerlbl_active; 5186d452930fSRichard Haines u32 peer_sid = SECINITSID_UNLABELED; 5187d452930fSRichard Haines u32 conn_sid; 5188d452930fSRichard Haines int err = 0; 5189d452930fSRichard Haines 5190aa8e712cSStephen Smalley if (!selinux_policycap_extsockclass()) 5191d452930fSRichard Haines return 0; 5192d452930fSRichard Haines 5193d452930fSRichard Haines peerlbl_active = selinux_peerlbl_enabled(); 5194d452930fSRichard Haines 5195d452930fSRichard Haines if (peerlbl_active) { 5196d452930fSRichard Haines /* This will return peer_sid = SECSID_NULL if there are 5197d452930fSRichard Haines * no peer labels, see security_net_peersid_resolve(). 5198d452930fSRichard Haines */ 5199d452930fSRichard Haines err = selinux_skb_peerlbl_sid(skb, ep->base.sk->sk_family, 5200d452930fSRichard Haines &peer_sid); 5201d452930fSRichard Haines if (err) 5202d452930fSRichard Haines return err; 5203d452930fSRichard Haines 5204d452930fSRichard Haines if (peer_sid == SECSID_NULL) 5205d452930fSRichard Haines peer_sid = SECINITSID_UNLABELED; 5206d452930fSRichard Haines } 5207d452930fSRichard Haines 5208d452930fSRichard Haines if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) { 5209d452930fSRichard Haines sksec->sctp_assoc_state = SCTP_ASSOC_SET; 5210d452930fSRichard Haines 5211d452930fSRichard Haines /* Here as first association on socket. As the peer SID 5212d452930fSRichard Haines * was allowed by peer recv (and the netif/node checks), 5213d452930fSRichard Haines * then it is approved by policy and used as the primary 5214d452930fSRichard Haines * peer SID for getpeercon(3). 5215d452930fSRichard Haines */ 5216d452930fSRichard Haines sksec->peer_sid = peer_sid; 5217d452930fSRichard Haines } else if (sksec->peer_sid != peer_sid) { 5218d452930fSRichard Haines /* Other association peer SIDs are checked to enforce 5219d452930fSRichard Haines * consistency among the peer SIDs. 5220d452930fSRichard Haines */ 5221d452930fSRichard Haines ad.type = LSM_AUDIT_DATA_NET; 5222d452930fSRichard Haines ad.u.net = &net; 5223d452930fSRichard Haines ad.u.net->sk = ep->base.sk; 52246b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 52256b6bc620SStephen Smalley sksec->peer_sid, peer_sid, sksec->sclass, 5226d452930fSRichard Haines SCTP_SOCKET__ASSOCIATION, &ad); 5227d452930fSRichard Haines if (err) 5228d452930fSRichard Haines return err; 5229d452930fSRichard Haines } 5230d452930fSRichard Haines 5231d452930fSRichard Haines /* Compute the MLS component for the connection and store 5232d452930fSRichard Haines * the information in ep. This will be used by SCTP TCP type 5233d452930fSRichard Haines * sockets and peeled off connections as they cause a new 5234d452930fSRichard Haines * socket to be generated. selinux_sctp_sk_clone() will then 5235d452930fSRichard Haines * plug this into the new socket. 5236d452930fSRichard Haines */ 5237d452930fSRichard Haines err = selinux_conn_sid(sksec->sid, peer_sid, &conn_sid); 5238d452930fSRichard Haines if (err) 5239d452930fSRichard Haines return err; 5240d452930fSRichard Haines 5241d452930fSRichard Haines ep->secid = conn_sid; 5242d452930fSRichard Haines ep->peer_secid = peer_sid; 5243d452930fSRichard Haines 5244d452930fSRichard Haines /* Set any NetLabel labels including CIPSO/CALIPSO options. */ 5245d452930fSRichard Haines return selinux_netlbl_sctp_assoc_request(ep, skb); 5246d452930fSRichard Haines } 5247d452930fSRichard Haines 5248d452930fSRichard Haines /* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting 5249d452930fSRichard Haines * based on their @optname. 5250d452930fSRichard Haines */ 5251d452930fSRichard Haines static int selinux_sctp_bind_connect(struct sock *sk, int optname, 5252d452930fSRichard Haines struct sockaddr *address, 5253d452930fSRichard Haines int addrlen) 5254d452930fSRichard Haines { 5255d452930fSRichard Haines int len, err = 0, walk_size = 0; 5256d452930fSRichard Haines void *addr_buf; 5257d452930fSRichard Haines struct sockaddr *addr; 5258d452930fSRichard Haines struct socket *sock; 5259d452930fSRichard Haines 5260aa8e712cSStephen Smalley if (!selinux_policycap_extsockclass()) 5261d452930fSRichard Haines return 0; 5262d452930fSRichard Haines 5263d452930fSRichard Haines /* Process one or more addresses that may be IPv4 or IPv6 */ 5264d452930fSRichard Haines sock = sk->sk_socket; 5265d452930fSRichard Haines addr_buf = address; 5266d452930fSRichard Haines 5267d452930fSRichard Haines while (walk_size < addrlen) { 5268c138325fSOndrej Mosnacek if (walk_size + sizeof(sa_family_t) > addrlen) 5269c138325fSOndrej Mosnacek return -EINVAL; 5270c138325fSOndrej Mosnacek 5271d452930fSRichard Haines addr = addr_buf; 5272d452930fSRichard Haines switch (addr->sa_family) { 52734152dc91SAlexey Kodanev case AF_UNSPEC: 5274d452930fSRichard Haines case AF_INET: 5275d452930fSRichard Haines len = sizeof(struct sockaddr_in); 5276d452930fSRichard Haines break; 5277d452930fSRichard Haines case AF_INET6: 5278d452930fSRichard Haines len = sizeof(struct sockaddr_in6); 5279d452930fSRichard Haines break; 5280d452930fSRichard Haines default: 52814152dc91SAlexey Kodanev return -EINVAL; 5282d452930fSRichard Haines } 5283d452930fSRichard Haines 5284292c997aSXin Long if (walk_size + len > addrlen) 5285292c997aSXin Long return -EINVAL; 5286292c997aSXin Long 5287d452930fSRichard Haines err = -EINVAL; 5288d452930fSRichard Haines switch (optname) { 5289d452930fSRichard Haines /* Bind checks */ 5290d452930fSRichard Haines case SCTP_PRIMARY_ADDR: 5291d452930fSRichard Haines case SCTP_SET_PEER_PRIMARY_ADDR: 5292d452930fSRichard Haines case SCTP_SOCKOPT_BINDX_ADD: 5293d452930fSRichard Haines err = selinux_socket_bind(sock, addr, len); 5294d452930fSRichard Haines break; 5295d452930fSRichard Haines /* Connect checks */ 5296d452930fSRichard Haines case SCTP_SOCKOPT_CONNECTX: 5297d452930fSRichard Haines case SCTP_PARAM_SET_PRIMARY: 5298d452930fSRichard Haines case SCTP_PARAM_ADD_IP: 5299d452930fSRichard Haines case SCTP_SENDMSG_CONNECT: 5300d452930fSRichard Haines err = selinux_socket_connect_helper(sock, addr, len); 5301d452930fSRichard Haines if (err) 5302d452930fSRichard Haines return err; 5303d452930fSRichard Haines 5304d452930fSRichard Haines /* As selinux_sctp_bind_connect() is called by the 5305d452930fSRichard Haines * SCTP protocol layer, the socket is already locked, 5306d452930fSRichard Haines * therefore selinux_netlbl_socket_connect_locked() is 5307d452930fSRichard Haines * is called here. The situations handled are: 5308d452930fSRichard Haines * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2), 5309d452930fSRichard Haines * whenever a new IP address is added or when a new 5310d452930fSRichard Haines * primary address is selected. 5311d452930fSRichard Haines * Note that an SCTP connect(2) call happens before 5312d452930fSRichard Haines * the SCTP protocol layer and is handled via 5313d452930fSRichard Haines * selinux_socket_connect(). 5314d452930fSRichard Haines */ 5315d452930fSRichard Haines err = selinux_netlbl_socket_connect_locked(sk, addr); 5316d452930fSRichard Haines break; 5317d452930fSRichard Haines } 5318d452930fSRichard Haines 5319d452930fSRichard Haines if (err) 5320d452930fSRichard Haines return err; 5321d452930fSRichard Haines 5322d452930fSRichard Haines addr_buf += len; 5323d452930fSRichard Haines walk_size += len; 5324d452930fSRichard Haines } 5325d452930fSRichard Haines 5326d452930fSRichard Haines return 0; 5327d452930fSRichard Haines } 5328d452930fSRichard Haines 5329d452930fSRichard Haines /* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */ 5330d452930fSRichard Haines static void selinux_sctp_sk_clone(struct sctp_endpoint *ep, struct sock *sk, 5331d452930fSRichard Haines struct sock *newsk) 5332d452930fSRichard Haines { 5333d452930fSRichard Haines struct sk_security_struct *sksec = sk->sk_security; 5334d452930fSRichard Haines struct sk_security_struct *newsksec = newsk->sk_security; 5335d452930fSRichard Haines 5336d452930fSRichard Haines /* If policy does not support SECCLASS_SCTP_SOCKET then call 5337d452930fSRichard Haines * the non-sctp clone version. 5338d452930fSRichard Haines */ 5339aa8e712cSStephen Smalley if (!selinux_policycap_extsockclass()) 5340d452930fSRichard Haines return selinux_sk_clone_security(sk, newsk); 5341d452930fSRichard Haines 5342d452930fSRichard Haines newsksec->sid = ep->secid; 5343d452930fSRichard Haines newsksec->peer_sid = ep->peer_secid; 5344d452930fSRichard Haines newsksec->sclass = sksec->sclass; 5345d452930fSRichard Haines selinux_netlbl_sctp_sk_clone(sk, newsk); 5346d452930fSRichard Haines } 5347d452930fSRichard Haines 53489a673e56SAdrian Bunk static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb, 53494237c75cSVenkat Yekkirala struct request_sock *req) 53504237c75cSVenkat Yekkirala { 53514237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 53524237c75cSVenkat Yekkirala int err; 53530b1f24e6SPaul Moore u16 family = req->rsk_ops->family; 5354446b8024SPaul Moore u32 connsid; 53554237c75cSVenkat Yekkirala u32 peersid; 53564237c75cSVenkat Yekkirala 5357aa862900SPaul Moore err = selinux_skb_peerlbl_sid(skb, family, &peersid); 5358220deb96SPaul Moore if (err) 5359220deb96SPaul Moore return err; 5360446b8024SPaul Moore err = selinux_conn_sid(sksec->sid, peersid, &connsid); 53614237c75cSVenkat Yekkirala if (err) 53624237c75cSVenkat Yekkirala return err; 5363446b8024SPaul Moore req->secid = connsid; 53646b877699SVenkat Yekkirala req->peer_secid = peersid; 5365389fb800SPaul Moore 5366389fb800SPaul Moore return selinux_netlbl_inet_conn_request(req, family); 53674237c75cSVenkat Yekkirala } 53684237c75cSVenkat Yekkirala 53699a673e56SAdrian Bunk static void selinux_inet_csk_clone(struct sock *newsk, 53709a673e56SAdrian Bunk const struct request_sock *req) 53714237c75cSVenkat Yekkirala { 53724237c75cSVenkat Yekkirala struct sk_security_struct *newsksec = newsk->sk_security; 53734237c75cSVenkat Yekkirala 53744237c75cSVenkat Yekkirala newsksec->sid = req->secid; 53756b877699SVenkat Yekkirala newsksec->peer_sid = req->peer_secid; 53764237c75cSVenkat Yekkirala /* NOTE: Ideally, we should also get the isec->sid for the 53774237c75cSVenkat Yekkirala new socket in sync, but we don't have the isec available yet. 53784237c75cSVenkat Yekkirala So we will wait until sock_graft to do it, by which 53794237c75cSVenkat Yekkirala time it will have been created and available. */ 538099f59ed0SPaul Moore 53819f2ad665SPaul Moore /* We don't need to take any sort of lock here as we are the only 53829f2ad665SPaul Moore * thread with access to newsksec */ 5383389fb800SPaul Moore selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family); 53844237c75cSVenkat Yekkirala } 53854237c75cSVenkat Yekkirala 5386014ab19aSPaul Moore static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb) 53876b877699SVenkat Yekkirala { 5388aa862900SPaul Moore u16 family = sk->sk_family; 53896b877699SVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 53906b877699SVenkat Yekkirala 5391aa862900SPaul Moore /* handle mapped IPv4 packets arriving via IPv6 sockets */ 5392aa862900SPaul Moore if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 5393aa862900SPaul Moore family = PF_INET; 5394aa862900SPaul Moore 5395aa862900SPaul Moore selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid); 53966b877699SVenkat Yekkirala } 53976b877699SVenkat Yekkirala 53982606fd1fSEric Paris static int selinux_secmark_relabel_packet(u32 sid) 53992606fd1fSEric Paris { 54002606fd1fSEric Paris const struct task_security_struct *__tsec; 54012606fd1fSEric Paris u32 tsid; 54022606fd1fSEric Paris 54030c6cfa62SCasey Schaufler __tsec = selinux_cred(current_cred()); 54042606fd1fSEric Paris tsid = __tsec->sid; 54052606fd1fSEric Paris 54066b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 54076b6bc620SStephen Smalley tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, 54086b6bc620SStephen Smalley NULL); 54092606fd1fSEric Paris } 54102606fd1fSEric Paris 54112606fd1fSEric Paris static void selinux_secmark_refcount_inc(void) 54122606fd1fSEric Paris { 54132606fd1fSEric Paris atomic_inc(&selinux_secmark_refcount); 54142606fd1fSEric Paris } 54152606fd1fSEric Paris 54162606fd1fSEric Paris static void selinux_secmark_refcount_dec(void) 54172606fd1fSEric Paris { 54182606fd1fSEric Paris atomic_dec(&selinux_secmark_refcount); 54192606fd1fSEric Paris } 54202606fd1fSEric Paris 54219a673e56SAdrian Bunk static void selinux_req_classify_flow(const struct request_sock *req, 54229a673e56SAdrian Bunk struct flowi *fl) 54234237c75cSVenkat Yekkirala { 54241d28f42cSDavid S. Miller fl->flowi_secid = req->secid; 54254237c75cSVenkat Yekkirala } 54264237c75cSVenkat Yekkirala 54275dbbaf2dSPaul Moore static int selinux_tun_dev_alloc_security(void **security) 54285dbbaf2dSPaul Moore { 54295dbbaf2dSPaul Moore struct tun_security_struct *tunsec; 54305dbbaf2dSPaul Moore 54315dbbaf2dSPaul Moore tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL); 54325dbbaf2dSPaul Moore if (!tunsec) 54335dbbaf2dSPaul Moore return -ENOMEM; 54345dbbaf2dSPaul Moore tunsec->sid = current_sid(); 54355dbbaf2dSPaul Moore 54365dbbaf2dSPaul Moore *security = tunsec; 54375dbbaf2dSPaul Moore return 0; 54385dbbaf2dSPaul Moore } 54395dbbaf2dSPaul Moore 54405dbbaf2dSPaul Moore static void selinux_tun_dev_free_security(void *security) 54415dbbaf2dSPaul Moore { 54425dbbaf2dSPaul Moore kfree(security); 54435dbbaf2dSPaul Moore } 54445dbbaf2dSPaul Moore 5445ed6d76e4SPaul Moore static int selinux_tun_dev_create(void) 5446ed6d76e4SPaul Moore { 5447ed6d76e4SPaul Moore u32 sid = current_sid(); 5448ed6d76e4SPaul Moore 5449ed6d76e4SPaul Moore /* we aren't taking into account the "sockcreate" SID since the socket 5450ed6d76e4SPaul Moore * that is being created here is not a socket in the traditional sense, 5451ed6d76e4SPaul Moore * instead it is a private sock, accessible only to the kernel, and 5452ed6d76e4SPaul Moore * representing a wide range of network traffic spanning multiple 5453ed6d76e4SPaul Moore * connections unlike traditional sockets - check the TUN driver to 5454ed6d76e4SPaul Moore * get a better understanding of why this socket is special */ 5455ed6d76e4SPaul Moore 54566b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 54576b6bc620SStephen Smalley sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE, 5458ed6d76e4SPaul Moore NULL); 5459ed6d76e4SPaul Moore } 5460ed6d76e4SPaul Moore 54615dbbaf2dSPaul Moore static int selinux_tun_dev_attach_queue(void *security) 5462ed6d76e4SPaul Moore { 54635dbbaf2dSPaul Moore struct tun_security_struct *tunsec = security; 54645dbbaf2dSPaul Moore 54656b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 54666b6bc620SStephen Smalley current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET, 54675dbbaf2dSPaul Moore TUN_SOCKET__ATTACH_QUEUE, NULL); 54685dbbaf2dSPaul Moore } 54695dbbaf2dSPaul Moore 54705dbbaf2dSPaul Moore static int selinux_tun_dev_attach(struct sock *sk, void *security) 54715dbbaf2dSPaul Moore { 54725dbbaf2dSPaul Moore struct tun_security_struct *tunsec = security; 5473ed6d76e4SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 5474ed6d76e4SPaul Moore 5475ed6d76e4SPaul Moore /* we don't currently perform any NetLabel based labeling here and it 5476ed6d76e4SPaul Moore * isn't clear that we would want to do so anyway; while we could apply 5477ed6d76e4SPaul Moore * labeling without the support of the TUN user the resulting labeled 5478ed6d76e4SPaul Moore * traffic from the other end of the connection would almost certainly 5479ed6d76e4SPaul Moore * cause confusion to the TUN user that had no idea network labeling 5480ed6d76e4SPaul Moore * protocols were being used */ 5481ed6d76e4SPaul Moore 54825dbbaf2dSPaul Moore sksec->sid = tunsec->sid; 5483ed6d76e4SPaul Moore sksec->sclass = SECCLASS_TUN_SOCKET; 54845dbbaf2dSPaul Moore 54855dbbaf2dSPaul Moore return 0; 5486ed6d76e4SPaul Moore } 5487ed6d76e4SPaul Moore 54885dbbaf2dSPaul Moore static int selinux_tun_dev_open(void *security) 5489ed6d76e4SPaul Moore { 54905dbbaf2dSPaul Moore struct tun_security_struct *tunsec = security; 5491ed6d76e4SPaul Moore u32 sid = current_sid(); 5492ed6d76e4SPaul Moore int err; 5493ed6d76e4SPaul Moore 54946b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 54956b6bc620SStephen Smalley sid, tunsec->sid, SECCLASS_TUN_SOCKET, 5496ed6d76e4SPaul Moore TUN_SOCKET__RELABELFROM, NULL); 5497ed6d76e4SPaul Moore if (err) 5498ed6d76e4SPaul Moore return err; 54996b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 55006b6bc620SStephen Smalley sid, sid, SECCLASS_TUN_SOCKET, 5501ed6d76e4SPaul Moore TUN_SOCKET__RELABELTO, NULL); 5502ed6d76e4SPaul Moore if (err) 5503ed6d76e4SPaul Moore return err; 55045dbbaf2dSPaul Moore tunsec->sid = sid; 5505ed6d76e4SPaul Moore 5506ed6d76e4SPaul Moore return 0; 5507ed6d76e4SPaul Moore } 5508ed6d76e4SPaul Moore 55091da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER 55101da177e4SLinus Torvalds 5511cbe0d6e8SPaul Moore static unsigned int selinux_ip_forward(struct sk_buff *skb, 5512cbe0d6e8SPaul Moore const struct net_device *indev, 5513effad8dfSPaul Moore u16 family) 55141da177e4SLinus Torvalds { 5515dfaebe98SPaul Moore int err; 5516effad8dfSPaul Moore char *addrp; 5517effad8dfSPaul Moore u32 peer_sid; 55182bf49690SThomas Liu struct common_audit_data ad; 551948c62af6SEric Paris struct lsm_network_audit net = {0,}; 5520effad8dfSPaul Moore u8 secmark_active; 5521948bf85cSPaul Moore u8 netlbl_active; 5522effad8dfSPaul Moore u8 peerlbl_active; 55234237c75cSVenkat Yekkirala 5524aa8e712cSStephen Smalley if (!selinux_policycap_netpeer()) 5525effad8dfSPaul Moore return NF_ACCEPT; 55264237c75cSVenkat Yekkirala 5527effad8dfSPaul Moore secmark_active = selinux_secmark_enabled(); 5528948bf85cSPaul Moore netlbl_active = netlbl_enabled(); 55292be4d74fSChris PeBenito peerlbl_active = selinux_peerlbl_enabled(); 5530effad8dfSPaul Moore if (!secmark_active && !peerlbl_active) 5531effad8dfSPaul Moore return NF_ACCEPT; 55324237c75cSVenkat Yekkirala 5533d8395c87SPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0) 5534d8395c87SPaul Moore return NF_DROP; 5535d8395c87SPaul Moore 553650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 553748c62af6SEric Paris ad.u.net = &net; 5538cbe0d6e8SPaul Moore ad.u.net->netif = indev->ifindex; 553948c62af6SEric Paris ad.u.net->family = family; 5540effad8dfSPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0) 5541effad8dfSPaul Moore return NF_DROP; 55421da177e4SLinus Torvalds 5543dfaebe98SPaul Moore if (peerlbl_active) { 5544cbe0d6e8SPaul Moore err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex, 5545cbe0d6e8SPaul Moore addrp, family, peer_sid, &ad); 5546dfaebe98SPaul Moore if (err) { 5547a04e71f6SHuw Davies selinux_netlbl_err(skb, family, err, 1); 5548effad8dfSPaul Moore return NF_DROP; 5549dfaebe98SPaul Moore } 5550dfaebe98SPaul Moore } 5551effad8dfSPaul Moore 5552effad8dfSPaul Moore if (secmark_active) 55536b6bc620SStephen Smalley if (avc_has_perm(&selinux_state, 55546b6bc620SStephen Smalley peer_sid, skb->secmark, 5555effad8dfSPaul Moore SECCLASS_PACKET, PACKET__FORWARD_IN, &ad)) 5556effad8dfSPaul Moore return NF_DROP; 5557effad8dfSPaul Moore 5558948bf85cSPaul Moore if (netlbl_active) 5559948bf85cSPaul Moore /* we do this in the FORWARD path and not the POST_ROUTING 5560948bf85cSPaul Moore * path because we want to make sure we apply the necessary 5561948bf85cSPaul Moore * labeling before IPsec is applied so we can leverage AH 5562948bf85cSPaul Moore * protection */ 5563948bf85cSPaul Moore if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0) 5564948bf85cSPaul Moore return NF_DROP; 5565948bf85cSPaul Moore 5566effad8dfSPaul Moore return NF_ACCEPT; 5567effad8dfSPaul Moore } 5568effad8dfSPaul Moore 556906198b34SEric W. Biederman static unsigned int selinux_ipv4_forward(void *priv, 5570effad8dfSPaul Moore struct sk_buff *skb, 5571238e54c9SDavid S. Miller const struct nf_hook_state *state) 5572effad8dfSPaul Moore { 5573238e54c9SDavid S. Miller return selinux_ip_forward(skb, state->in, PF_INET); 5574effad8dfSPaul Moore } 5575effad8dfSPaul Moore 55761a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 557706198b34SEric W. Biederman static unsigned int selinux_ipv6_forward(void *priv, 5578effad8dfSPaul Moore struct sk_buff *skb, 5579238e54c9SDavid S. Miller const struct nf_hook_state *state) 5580effad8dfSPaul Moore { 5581238e54c9SDavid S. Miller return selinux_ip_forward(skb, state->in, PF_INET6); 5582effad8dfSPaul Moore } 5583effad8dfSPaul Moore #endif /* IPV6 */ 5584effad8dfSPaul Moore 5585948bf85cSPaul Moore static unsigned int selinux_ip_output(struct sk_buff *skb, 5586948bf85cSPaul Moore u16 family) 5587948bf85cSPaul Moore { 558847180068SPaul Moore struct sock *sk; 5589948bf85cSPaul Moore u32 sid; 5590948bf85cSPaul Moore 5591948bf85cSPaul Moore if (!netlbl_enabled()) 5592948bf85cSPaul Moore return NF_ACCEPT; 5593948bf85cSPaul Moore 5594948bf85cSPaul Moore /* we do this in the LOCAL_OUT path and not the POST_ROUTING path 5595948bf85cSPaul Moore * because we want to make sure we apply the necessary labeling 5596948bf85cSPaul Moore * before IPsec is applied so we can leverage AH protection */ 559747180068SPaul Moore sk = skb->sk; 559847180068SPaul Moore if (sk) { 559947180068SPaul Moore struct sk_security_struct *sksec; 560047180068SPaul Moore 5601e446f9dfSEric Dumazet if (sk_listener(sk)) 560247180068SPaul Moore /* if the socket is the listening state then this 560347180068SPaul Moore * packet is a SYN-ACK packet which means it needs to 560447180068SPaul Moore * be labeled based on the connection/request_sock and 560547180068SPaul Moore * not the parent socket. unfortunately, we can't 560647180068SPaul Moore * lookup the request_sock yet as it isn't queued on 560747180068SPaul Moore * the parent socket until after the SYN-ACK is sent. 560847180068SPaul Moore * the "solution" is to simply pass the packet as-is 560947180068SPaul Moore * as any IP option based labeling should be copied 561047180068SPaul Moore * from the initial connection request (in the IP 561147180068SPaul Moore * layer). it is far from ideal, but until we get a 561247180068SPaul Moore * security label in the packet itself this is the 561347180068SPaul Moore * best we can do. */ 561447180068SPaul Moore return NF_ACCEPT; 561547180068SPaul Moore 561647180068SPaul Moore /* standard practice, label using the parent socket */ 561747180068SPaul Moore sksec = sk->sk_security; 5618948bf85cSPaul Moore sid = sksec->sid; 5619948bf85cSPaul Moore } else 5620948bf85cSPaul Moore sid = SECINITSID_KERNEL; 5621948bf85cSPaul Moore if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0) 5622948bf85cSPaul Moore return NF_DROP; 5623948bf85cSPaul Moore 5624948bf85cSPaul Moore return NF_ACCEPT; 5625948bf85cSPaul Moore } 5626948bf85cSPaul Moore 562706198b34SEric W. Biederman static unsigned int selinux_ipv4_output(void *priv, 5628948bf85cSPaul Moore struct sk_buff *skb, 5629238e54c9SDavid S. Miller const struct nf_hook_state *state) 5630948bf85cSPaul Moore { 5631948bf85cSPaul Moore return selinux_ip_output(skb, PF_INET); 5632948bf85cSPaul Moore } 5633948bf85cSPaul Moore 56341a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 56352917f57bSHuw Davies static unsigned int selinux_ipv6_output(void *priv, 56362917f57bSHuw Davies struct sk_buff *skb, 56372917f57bSHuw Davies const struct nf_hook_state *state) 56382917f57bSHuw Davies { 56392917f57bSHuw Davies return selinux_ip_output(skb, PF_INET6); 56402917f57bSHuw Davies } 56412917f57bSHuw Davies #endif /* IPV6 */ 56422917f57bSHuw Davies 5643effad8dfSPaul Moore static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb, 5644effad8dfSPaul Moore int ifindex, 5645d8395c87SPaul Moore u16 family) 56464e5ab4cbSJames Morris { 564754abc686SEric Dumazet struct sock *sk = skb_to_full_sk(skb); 56484237c75cSVenkat Yekkirala struct sk_security_struct *sksec; 56492bf49690SThomas Liu struct common_audit_data ad; 565048c62af6SEric Paris struct lsm_network_audit net = {0,}; 5651d8395c87SPaul Moore char *addrp; 5652d8395c87SPaul Moore u8 proto; 56534e5ab4cbSJames Morris 5654effad8dfSPaul Moore if (sk == NULL) 5655effad8dfSPaul Moore return NF_ACCEPT; 56564237c75cSVenkat Yekkirala sksec = sk->sk_security; 56574e5ab4cbSJames Morris 565850c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 565948c62af6SEric Paris ad.u.net = &net; 566048c62af6SEric Paris ad.u.net->netif = ifindex; 566148c62af6SEric Paris ad.u.net->family = family; 5662d8395c87SPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto)) 5663d8395c87SPaul Moore return NF_DROP; 5664d8395c87SPaul Moore 566558bfbb51SPaul Moore if (selinux_secmark_enabled()) 56666b6bc620SStephen Smalley if (avc_has_perm(&selinux_state, 56676b6bc620SStephen Smalley sksec->sid, skb->secmark, 5668d8395c87SPaul Moore SECCLASS_PACKET, PACKET__SEND, &ad)) 56692fe66ec2SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 56701da177e4SLinus Torvalds 5671d8395c87SPaul Moore if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto)) 56722fe66ec2SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 5673effad8dfSPaul Moore 5674effad8dfSPaul Moore return NF_ACCEPT; 5675effad8dfSPaul Moore } 5676effad8dfSPaul Moore 5677cbe0d6e8SPaul Moore static unsigned int selinux_ip_postroute(struct sk_buff *skb, 5678cbe0d6e8SPaul Moore const struct net_device *outdev, 5679effad8dfSPaul Moore u16 family) 5680effad8dfSPaul Moore { 5681effad8dfSPaul Moore u32 secmark_perm; 5682effad8dfSPaul Moore u32 peer_sid; 5683cbe0d6e8SPaul Moore int ifindex = outdev->ifindex; 5684effad8dfSPaul Moore struct sock *sk; 56852bf49690SThomas Liu struct common_audit_data ad; 568648c62af6SEric Paris struct lsm_network_audit net = {0,}; 5687effad8dfSPaul Moore char *addrp; 5688effad8dfSPaul Moore u8 secmark_active; 5689effad8dfSPaul Moore u8 peerlbl_active; 5690effad8dfSPaul Moore 5691effad8dfSPaul Moore /* If any sort of compatibility mode is enabled then handoff processing 5692effad8dfSPaul Moore * to the selinux_ip_postroute_compat() function to deal with the 5693effad8dfSPaul Moore * special handling. We do this in an attempt to keep this function 5694effad8dfSPaul Moore * as fast and as clean as possible. */ 5695aa8e712cSStephen Smalley if (!selinux_policycap_netpeer()) 5696d8395c87SPaul Moore return selinux_ip_postroute_compat(skb, ifindex, family); 5697c0828e50SPaul Moore 5698effad8dfSPaul Moore secmark_active = selinux_secmark_enabled(); 56992be4d74fSChris PeBenito peerlbl_active = selinux_peerlbl_enabled(); 5700effad8dfSPaul Moore if (!secmark_active && !peerlbl_active) 5701effad8dfSPaul Moore return NF_ACCEPT; 5702effad8dfSPaul Moore 570354abc686SEric Dumazet sk = skb_to_full_sk(skb); 5704c0828e50SPaul Moore 5705effad8dfSPaul Moore #ifdef CONFIG_XFRM 5706effad8dfSPaul Moore /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec 5707effad8dfSPaul Moore * packet transformation so allow the packet to pass without any checks 5708effad8dfSPaul Moore * since we'll have another chance to perform access control checks 5709effad8dfSPaul Moore * when the packet is on it's final way out. 5710effad8dfSPaul Moore * NOTE: there appear to be some IPv6 multicast cases where skb->dst 5711c0828e50SPaul Moore * is NULL, in this case go ahead and apply access control. 5712c0828e50SPaul Moore * NOTE: if this is a local socket (skb->sk != NULL) that is in the 5713c0828e50SPaul Moore * TCP listening state we cannot wait until the XFRM processing 5714c0828e50SPaul Moore * is done as we will miss out on the SA label if we do; 5715c0828e50SPaul Moore * unfortunately, this means more work, but it is only once per 5716c0828e50SPaul Moore * connection. */ 5717c0828e50SPaul Moore if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL && 5718e446f9dfSEric Dumazet !(sk && sk_listener(sk))) 5719effad8dfSPaul Moore return NF_ACCEPT; 5720effad8dfSPaul Moore #endif 5721effad8dfSPaul Moore 5722d8395c87SPaul Moore if (sk == NULL) { 5723446b8024SPaul Moore /* Without an associated socket the packet is either coming 5724446b8024SPaul Moore * from the kernel or it is being forwarded; check the packet 5725446b8024SPaul Moore * to determine which and if the packet is being forwarded 5726446b8024SPaul Moore * query the packet directly to determine the security label. */ 57274a7ab3dcSSteffen Klassert if (skb->skb_iif) { 5728d8395c87SPaul Moore secmark_perm = PACKET__FORWARD_OUT; 5729d8395c87SPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &peer_sid)) 573004f6d70fSEric Paris return NF_DROP; 57314a7ab3dcSSteffen Klassert } else { 57324a7ab3dcSSteffen Klassert secmark_perm = PACKET__SEND; 5733d8395c87SPaul Moore peer_sid = SECINITSID_KERNEL; 57344a7ab3dcSSteffen Klassert } 5735e446f9dfSEric Dumazet } else if (sk_listener(sk)) { 5736446b8024SPaul Moore /* Locally generated packet but the associated socket is in the 5737446b8024SPaul Moore * listening state which means this is a SYN-ACK packet. In 5738446b8024SPaul Moore * this particular case the correct security label is assigned 5739446b8024SPaul Moore * to the connection/request_sock but unfortunately we can't 5740446b8024SPaul Moore * query the request_sock as it isn't queued on the parent 5741446b8024SPaul Moore * socket until after the SYN-ACK packet is sent; the only 5742446b8024SPaul Moore * viable choice is to regenerate the label like we do in 5743446b8024SPaul Moore * selinux_inet_conn_request(). See also selinux_ip_output() 5744446b8024SPaul Moore * for similar problems. */ 5745446b8024SPaul Moore u32 skb_sid; 5746e446f9dfSEric Dumazet struct sk_security_struct *sksec; 5747e446f9dfSEric Dumazet 5748e446f9dfSEric Dumazet sksec = sk->sk_security; 5749446b8024SPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &skb_sid)) 5750446b8024SPaul Moore return NF_DROP; 5751c0828e50SPaul Moore /* At this point, if the returned skb peerlbl is SECSID_NULL 5752c0828e50SPaul Moore * and the packet has been through at least one XFRM 5753c0828e50SPaul Moore * transformation then we must be dealing with the "final" 5754c0828e50SPaul Moore * form of labeled IPsec packet; since we've already applied 5755c0828e50SPaul Moore * all of our access controls on this packet we can safely 5756c0828e50SPaul Moore * pass the packet. */ 5757c0828e50SPaul Moore if (skb_sid == SECSID_NULL) { 5758c0828e50SPaul Moore switch (family) { 5759c0828e50SPaul Moore case PF_INET: 5760c0828e50SPaul Moore if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) 5761c0828e50SPaul Moore return NF_ACCEPT; 5762c0828e50SPaul Moore break; 5763c0828e50SPaul Moore case PF_INET6: 5764c0828e50SPaul Moore if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) 5765c0828e50SPaul Moore return NF_ACCEPT; 5766a7a91a19SPaul Moore break; 5767c0828e50SPaul Moore default: 5768c0828e50SPaul Moore return NF_DROP_ERR(-ECONNREFUSED); 5769c0828e50SPaul Moore } 5770c0828e50SPaul Moore } 5771446b8024SPaul Moore if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid)) 5772446b8024SPaul Moore return NF_DROP; 5773446b8024SPaul Moore secmark_perm = PACKET__SEND; 5774d8395c87SPaul Moore } else { 5775446b8024SPaul Moore /* Locally generated packet, fetch the security label from the 5776446b8024SPaul Moore * associated socket. */ 5777effad8dfSPaul Moore struct sk_security_struct *sksec = sk->sk_security; 5778effad8dfSPaul Moore peer_sid = sksec->sid; 5779effad8dfSPaul Moore secmark_perm = PACKET__SEND; 5780effad8dfSPaul Moore } 5781effad8dfSPaul Moore 578250c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 578348c62af6SEric Paris ad.u.net = &net; 578448c62af6SEric Paris ad.u.net->netif = ifindex; 578548c62af6SEric Paris ad.u.net->family = family; 5786d8395c87SPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL)) 578704f6d70fSEric Paris return NF_DROP; 5788d8395c87SPaul Moore 5789effad8dfSPaul Moore if (secmark_active) 57906b6bc620SStephen Smalley if (avc_has_perm(&selinux_state, 57916b6bc620SStephen Smalley peer_sid, skb->secmark, 5792effad8dfSPaul Moore SECCLASS_PACKET, secmark_perm, &ad)) 57931f1aaf82SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 5794effad8dfSPaul Moore 5795effad8dfSPaul Moore if (peerlbl_active) { 5796effad8dfSPaul Moore u32 if_sid; 5797effad8dfSPaul Moore u32 node_sid; 5798effad8dfSPaul Moore 5799cbe0d6e8SPaul Moore if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid)) 580004f6d70fSEric Paris return NF_DROP; 58016b6bc620SStephen Smalley if (avc_has_perm(&selinux_state, 58026b6bc620SStephen Smalley peer_sid, if_sid, 5803effad8dfSPaul Moore SECCLASS_NETIF, NETIF__EGRESS, &ad)) 58041f1aaf82SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 5805effad8dfSPaul Moore 5806effad8dfSPaul Moore if (sel_netnode_sid(addrp, family, &node_sid)) 580704f6d70fSEric Paris return NF_DROP; 58086b6bc620SStephen Smalley if (avc_has_perm(&selinux_state, 58096b6bc620SStephen Smalley peer_sid, node_sid, 5810effad8dfSPaul Moore SECCLASS_NODE, NODE__SENDTO, &ad)) 58111f1aaf82SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 5812effad8dfSPaul Moore } 5813effad8dfSPaul Moore 5814effad8dfSPaul Moore return NF_ACCEPT; 5815effad8dfSPaul Moore } 5816effad8dfSPaul Moore 581706198b34SEric W. Biederman static unsigned int selinux_ipv4_postroute(void *priv, 5818a224be76SDavid S. Miller struct sk_buff *skb, 5819238e54c9SDavid S. Miller const struct nf_hook_state *state) 58201da177e4SLinus Torvalds { 5821238e54c9SDavid S. Miller return selinux_ip_postroute(skb, state->out, PF_INET); 58221da177e4SLinus Torvalds } 58231da177e4SLinus Torvalds 58241a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 582506198b34SEric W. Biederman static unsigned int selinux_ipv6_postroute(void *priv, 5826a224be76SDavid S. Miller struct sk_buff *skb, 5827238e54c9SDavid S. Miller const struct nf_hook_state *state) 58281da177e4SLinus Torvalds { 5829238e54c9SDavid S. Miller return selinux_ip_postroute(skb, state->out, PF_INET6); 58301da177e4SLinus Torvalds } 58311da177e4SLinus Torvalds #endif /* IPV6 */ 58321da177e4SLinus Torvalds 58331da177e4SLinus Torvalds #endif /* CONFIG_NETFILTER */ 58341da177e4SLinus Torvalds 58351da177e4SLinus Torvalds static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb) 58361da177e4SLinus Torvalds { 5837df4779b5SHuaisheng Ye int err = 0; 5838df4779b5SHuaisheng Ye u32 perm; 5839df4779b5SHuaisheng Ye struct nlmsghdr *nlh; 5840df4779b5SHuaisheng Ye struct sk_security_struct *sksec = sk->sk_security; 5841df4779b5SHuaisheng Ye 5842df4779b5SHuaisheng Ye if (skb->len < NLMSG_HDRLEN) { 5843df4779b5SHuaisheng Ye err = -EINVAL; 5844df4779b5SHuaisheng Ye goto out; 5845df4779b5SHuaisheng Ye } 5846df4779b5SHuaisheng Ye nlh = nlmsg_hdr(skb); 5847df4779b5SHuaisheng Ye 5848df4779b5SHuaisheng Ye err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm); 5849df4779b5SHuaisheng Ye if (err) { 5850df4779b5SHuaisheng Ye if (err == -EINVAL) { 5851df4779b5SHuaisheng Ye pr_warn_ratelimited("SELinux: unrecognized netlink" 5852df4779b5SHuaisheng Ye " message: protocol=%hu nlmsg_type=%hu sclass=%s" 5853df4779b5SHuaisheng Ye " pid=%d comm=%s\n", 5854df4779b5SHuaisheng Ye sk->sk_protocol, nlh->nlmsg_type, 5855df4779b5SHuaisheng Ye secclass_map[sksec->sclass - 1].name, 5856df4779b5SHuaisheng Ye task_pid_nr(current), current->comm); 5857df4779b5SHuaisheng Ye if (!enforcing_enabled(&selinux_state) || 5858df4779b5SHuaisheng Ye security_get_allow_unknown(&selinux_state)) 5859df4779b5SHuaisheng Ye err = 0; 5860df4779b5SHuaisheng Ye } 5861df4779b5SHuaisheng Ye 5862df4779b5SHuaisheng Ye /* Ignore */ 5863df4779b5SHuaisheng Ye if (err == -ENOENT) 5864df4779b5SHuaisheng Ye err = 0; 5865df4779b5SHuaisheng Ye goto out; 5866df4779b5SHuaisheng Ye } 5867df4779b5SHuaisheng Ye 5868df4779b5SHuaisheng Ye err = sock_has_perm(sk, perm); 5869df4779b5SHuaisheng Ye out: 5870df4779b5SHuaisheng Ye return err; 58711da177e4SLinus Torvalds } 58721da177e4SLinus Torvalds 5873ecd5f82eSCasey Schaufler static void ipc_init_security(struct ipc_security_struct *isec, u16 sclass) 58741da177e4SLinus Torvalds { 58751da177e4SLinus Torvalds isec->sclass = sclass; 5876be0554c9SStephen Smalley isec->sid = current_sid(); 58771da177e4SLinus Torvalds } 58781da177e4SLinus Torvalds 58791da177e4SLinus Torvalds static int ipc_has_perm(struct kern_ipc_perm *ipc_perms, 58806af963f1SStephen Smalley u32 perms) 58811da177e4SLinus Torvalds { 58821da177e4SLinus Torvalds struct ipc_security_struct *isec; 58832bf49690SThomas Liu struct common_audit_data ad; 5884275bb41eSDavid Howells u32 sid = current_sid(); 58851da177e4SLinus Torvalds 58867c653828SCasey Schaufler isec = selinux_ipc(ipc_perms); 58871da177e4SLinus Torvalds 588850c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 58891da177e4SLinus Torvalds ad.u.ipc_id = ipc_perms->key; 58901da177e4SLinus Torvalds 58916b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 58926b6bc620SStephen Smalley sid, isec->sid, isec->sclass, perms, &ad); 58931da177e4SLinus Torvalds } 58941da177e4SLinus Torvalds 58951da177e4SLinus Torvalds static int selinux_msg_msg_alloc_security(struct msg_msg *msg) 58961da177e4SLinus Torvalds { 5897b82f3f68SHuaisheng Ye struct msg_security_struct *msec; 5898b82f3f68SHuaisheng Ye 5899b82f3f68SHuaisheng Ye msec = selinux_msg_msg(msg); 5900b82f3f68SHuaisheng Ye msec->sid = SECINITSID_UNLABELED; 5901b82f3f68SHuaisheng Ye 5902b82f3f68SHuaisheng Ye return 0; 59031da177e4SLinus Torvalds } 59041da177e4SLinus Torvalds 59051da177e4SLinus Torvalds /* message queue security operations */ 5906d8c6e854SEric W. Biederman static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq) 59071da177e4SLinus Torvalds { 59081da177e4SLinus Torvalds struct ipc_security_struct *isec; 59092bf49690SThomas Liu struct common_audit_data ad; 5910275bb41eSDavid Howells u32 sid = current_sid(); 59111da177e4SLinus Torvalds int rc; 59121da177e4SLinus Torvalds 5913ecd5f82eSCasey Schaufler isec = selinux_ipc(msq); 5914ecd5f82eSCasey Schaufler ipc_init_security(isec, SECCLASS_MSGQ); 59151da177e4SLinus Torvalds 591650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 5917d8c6e854SEric W. Biederman ad.u.ipc_id = msq->key; 59181da177e4SLinus Torvalds 59196b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 59206b6bc620SStephen Smalley sid, isec->sid, SECCLASS_MSGQ, 59211da177e4SLinus Torvalds MSGQ__CREATE, &ad); 59221da177e4SLinus Torvalds return rc; 59231da177e4SLinus Torvalds } 59241da177e4SLinus Torvalds 5925d8c6e854SEric W. Biederman static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg) 59261da177e4SLinus Torvalds { 59271da177e4SLinus Torvalds struct ipc_security_struct *isec; 59282bf49690SThomas Liu struct common_audit_data ad; 5929275bb41eSDavid Howells u32 sid = current_sid(); 59301da177e4SLinus Torvalds 59317c653828SCasey Schaufler isec = selinux_ipc(msq); 59321da177e4SLinus Torvalds 593350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 5934d8c6e854SEric W. Biederman ad.u.ipc_id = msq->key; 59351da177e4SLinus Torvalds 59366b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 59376b6bc620SStephen Smalley sid, isec->sid, SECCLASS_MSGQ, 59381da177e4SLinus Torvalds MSGQ__ASSOCIATE, &ad); 59391da177e4SLinus Torvalds } 59401da177e4SLinus Torvalds 5941d8c6e854SEric W. Biederman static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd) 59421da177e4SLinus Torvalds { 59431da177e4SLinus Torvalds int err; 59441da177e4SLinus Torvalds int perms; 59451da177e4SLinus Torvalds 59461da177e4SLinus Torvalds switch (cmd) { 59471da177e4SLinus Torvalds case IPC_INFO: 59481da177e4SLinus Torvalds case MSG_INFO: 59491da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 59506b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 59516b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, 5952be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); 59531da177e4SLinus Torvalds case IPC_STAT: 59541da177e4SLinus Torvalds case MSG_STAT: 595523c8cec8SDavidlohr Bueso case MSG_STAT_ANY: 59561da177e4SLinus Torvalds perms = MSGQ__GETATTR | MSGQ__ASSOCIATE; 59571da177e4SLinus Torvalds break; 59581da177e4SLinus Torvalds case IPC_SET: 59591da177e4SLinus Torvalds perms = MSGQ__SETATTR; 59601da177e4SLinus Torvalds break; 59611da177e4SLinus Torvalds case IPC_RMID: 59621da177e4SLinus Torvalds perms = MSGQ__DESTROY; 59631da177e4SLinus Torvalds break; 59641da177e4SLinus Torvalds default: 59651da177e4SLinus Torvalds return 0; 59661da177e4SLinus Torvalds } 59671da177e4SLinus Torvalds 5968d8c6e854SEric W. Biederman err = ipc_has_perm(msq, perms); 59691da177e4SLinus Torvalds return err; 59701da177e4SLinus Torvalds } 59711da177e4SLinus Torvalds 5972d8c6e854SEric W. Biederman static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg) 59731da177e4SLinus Torvalds { 59741da177e4SLinus Torvalds struct ipc_security_struct *isec; 59751da177e4SLinus Torvalds struct msg_security_struct *msec; 59762bf49690SThomas Liu struct common_audit_data ad; 5977275bb41eSDavid Howells u32 sid = current_sid(); 59781da177e4SLinus Torvalds int rc; 59791da177e4SLinus Torvalds 59807c653828SCasey Schaufler isec = selinux_ipc(msq); 59817c653828SCasey Schaufler msec = selinux_msg_msg(msg); 59821da177e4SLinus Torvalds 59831da177e4SLinus Torvalds /* 59841da177e4SLinus Torvalds * First time through, need to assign label to the message 59851da177e4SLinus Torvalds */ 59861da177e4SLinus Torvalds if (msec->sid == SECINITSID_UNLABELED) { 59871da177e4SLinus Torvalds /* 59881da177e4SLinus Torvalds * Compute new sid based on current process and 59891da177e4SLinus Torvalds * message queue this message will be stored in 59901da177e4SLinus Torvalds */ 5991aa8e712cSStephen Smalley rc = security_transition_sid(&selinux_state, sid, isec->sid, 5992aa8e712cSStephen Smalley SECCLASS_MSG, NULL, &msec->sid); 59931da177e4SLinus Torvalds if (rc) 59941da177e4SLinus Torvalds return rc; 59951da177e4SLinus Torvalds } 59961da177e4SLinus Torvalds 599750c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 5998d8c6e854SEric W. Biederman ad.u.ipc_id = msq->key; 59991da177e4SLinus Torvalds 60001da177e4SLinus Torvalds /* Can this process write to the queue? */ 60016b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 60026b6bc620SStephen Smalley sid, isec->sid, SECCLASS_MSGQ, 60031da177e4SLinus Torvalds MSGQ__WRITE, &ad); 60041da177e4SLinus Torvalds if (!rc) 60051da177e4SLinus Torvalds /* Can this process send the message */ 60066b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 60076b6bc620SStephen Smalley sid, msec->sid, SECCLASS_MSG, 6008275bb41eSDavid Howells MSG__SEND, &ad); 60091da177e4SLinus Torvalds if (!rc) 60101da177e4SLinus Torvalds /* Can the message be put in the queue? */ 60116b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 60126b6bc620SStephen Smalley msec->sid, isec->sid, SECCLASS_MSGQ, 6013275bb41eSDavid Howells MSGQ__ENQUEUE, &ad); 60141da177e4SLinus Torvalds 60151da177e4SLinus Torvalds return rc; 60161da177e4SLinus Torvalds } 60171da177e4SLinus Torvalds 6018d8c6e854SEric W. Biederman static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg, 60191da177e4SLinus Torvalds struct task_struct *target, 60201da177e4SLinus Torvalds long type, int mode) 60211da177e4SLinus Torvalds { 60221da177e4SLinus Torvalds struct ipc_security_struct *isec; 60231da177e4SLinus Torvalds struct msg_security_struct *msec; 60242bf49690SThomas Liu struct common_audit_data ad; 6025275bb41eSDavid Howells u32 sid = task_sid(target); 60261da177e4SLinus Torvalds int rc; 60271da177e4SLinus Torvalds 60287c653828SCasey Schaufler isec = selinux_ipc(msq); 60297c653828SCasey Schaufler msec = selinux_msg_msg(msg); 60301da177e4SLinus Torvalds 603150c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 6032d8c6e854SEric W. Biederman ad.u.ipc_id = msq->key; 60331da177e4SLinus Torvalds 60346b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 60356b6bc620SStephen Smalley sid, isec->sid, 60361da177e4SLinus Torvalds SECCLASS_MSGQ, MSGQ__READ, &ad); 60371da177e4SLinus Torvalds if (!rc) 60386b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 60396b6bc620SStephen Smalley sid, msec->sid, 60401da177e4SLinus Torvalds SECCLASS_MSG, MSG__RECEIVE, &ad); 60411da177e4SLinus Torvalds return rc; 60421da177e4SLinus Torvalds } 60431da177e4SLinus Torvalds 60441da177e4SLinus Torvalds /* Shared Memory security operations */ 60457191adffSEric W. Biederman static int selinux_shm_alloc_security(struct kern_ipc_perm *shp) 60461da177e4SLinus Torvalds { 60471da177e4SLinus Torvalds struct ipc_security_struct *isec; 60482bf49690SThomas Liu struct common_audit_data ad; 6049275bb41eSDavid Howells u32 sid = current_sid(); 60501da177e4SLinus Torvalds int rc; 60511da177e4SLinus Torvalds 6052ecd5f82eSCasey Schaufler isec = selinux_ipc(shp); 6053ecd5f82eSCasey Schaufler ipc_init_security(isec, SECCLASS_SHM); 60541da177e4SLinus Torvalds 605550c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 60567191adffSEric W. Biederman ad.u.ipc_id = shp->key; 60571da177e4SLinus Torvalds 60586b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 60596b6bc620SStephen Smalley sid, isec->sid, SECCLASS_SHM, 60601da177e4SLinus Torvalds SHM__CREATE, &ad); 60611da177e4SLinus Torvalds return rc; 60621da177e4SLinus Torvalds } 60631da177e4SLinus Torvalds 60647191adffSEric W. Biederman static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg) 60651da177e4SLinus Torvalds { 60661da177e4SLinus Torvalds struct ipc_security_struct *isec; 60672bf49690SThomas Liu struct common_audit_data ad; 6068275bb41eSDavid Howells u32 sid = current_sid(); 60691da177e4SLinus Torvalds 60707c653828SCasey Schaufler isec = selinux_ipc(shp); 60711da177e4SLinus Torvalds 607250c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 60737191adffSEric W. Biederman ad.u.ipc_id = shp->key; 60741da177e4SLinus Torvalds 60756b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 60766b6bc620SStephen Smalley sid, isec->sid, SECCLASS_SHM, 60771da177e4SLinus Torvalds SHM__ASSOCIATE, &ad); 60781da177e4SLinus Torvalds } 60791da177e4SLinus Torvalds 60801da177e4SLinus Torvalds /* Note, at this point, shp is locked down */ 60817191adffSEric W. Biederman static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd) 60821da177e4SLinus Torvalds { 60831da177e4SLinus Torvalds int perms; 60841da177e4SLinus Torvalds int err; 60851da177e4SLinus Torvalds 60861da177e4SLinus Torvalds switch (cmd) { 60871da177e4SLinus Torvalds case IPC_INFO: 60881da177e4SLinus Torvalds case SHM_INFO: 60891da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 60906b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 60916b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, 6092be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); 60931da177e4SLinus Torvalds case IPC_STAT: 60941da177e4SLinus Torvalds case SHM_STAT: 6095c21a6970SDavidlohr Bueso case SHM_STAT_ANY: 60961da177e4SLinus Torvalds perms = SHM__GETATTR | SHM__ASSOCIATE; 60971da177e4SLinus Torvalds break; 60981da177e4SLinus Torvalds case IPC_SET: 60991da177e4SLinus Torvalds perms = SHM__SETATTR; 61001da177e4SLinus Torvalds break; 61011da177e4SLinus Torvalds case SHM_LOCK: 61021da177e4SLinus Torvalds case SHM_UNLOCK: 61031da177e4SLinus Torvalds perms = SHM__LOCK; 61041da177e4SLinus Torvalds break; 61051da177e4SLinus Torvalds case IPC_RMID: 61061da177e4SLinus Torvalds perms = SHM__DESTROY; 61071da177e4SLinus Torvalds break; 61081da177e4SLinus Torvalds default: 61091da177e4SLinus Torvalds return 0; 61101da177e4SLinus Torvalds } 61111da177e4SLinus Torvalds 61127191adffSEric W. Biederman err = ipc_has_perm(shp, perms); 61131da177e4SLinus Torvalds return err; 61141da177e4SLinus Torvalds } 61151da177e4SLinus Torvalds 61167191adffSEric W. Biederman static int selinux_shm_shmat(struct kern_ipc_perm *shp, 61171da177e4SLinus Torvalds char __user *shmaddr, int shmflg) 61181da177e4SLinus Torvalds { 61191da177e4SLinus Torvalds u32 perms; 61201da177e4SLinus Torvalds 61211da177e4SLinus Torvalds if (shmflg & SHM_RDONLY) 61221da177e4SLinus Torvalds perms = SHM__READ; 61231da177e4SLinus Torvalds else 61241da177e4SLinus Torvalds perms = SHM__READ | SHM__WRITE; 61251da177e4SLinus Torvalds 61267191adffSEric W. Biederman return ipc_has_perm(shp, perms); 61271da177e4SLinus Torvalds } 61281da177e4SLinus Torvalds 61291da177e4SLinus Torvalds /* Semaphore security operations */ 6130aefad959SEric W. Biederman static int selinux_sem_alloc_security(struct kern_ipc_perm *sma) 61311da177e4SLinus Torvalds { 61321da177e4SLinus Torvalds struct ipc_security_struct *isec; 61332bf49690SThomas Liu struct common_audit_data ad; 6134275bb41eSDavid Howells u32 sid = current_sid(); 61351da177e4SLinus Torvalds int rc; 61361da177e4SLinus Torvalds 6137ecd5f82eSCasey Schaufler isec = selinux_ipc(sma); 6138ecd5f82eSCasey Schaufler ipc_init_security(isec, SECCLASS_SEM); 61391da177e4SLinus Torvalds 614050c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 6141aefad959SEric W. Biederman ad.u.ipc_id = sma->key; 61421da177e4SLinus Torvalds 61436b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 61446b6bc620SStephen Smalley sid, isec->sid, SECCLASS_SEM, 61451da177e4SLinus Torvalds SEM__CREATE, &ad); 61461da177e4SLinus Torvalds return rc; 61471da177e4SLinus Torvalds } 61481da177e4SLinus Torvalds 6149aefad959SEric W. Biederman static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg) 61501da177e4SLinus Torvalds { 61511da177e4SLinus Torvalds struct ipc_security_struct *isec; 61522bf49690SThomas Liu struct common_audit_data ad; 6153275bb41eSDavid Howells u32 sid = current_sid(); 61541da177e4SLinus Torvalds 61557c653828SCasey Schaufler isec = selinux_ipc(sma); 61561da177e4SLinus Torvalds 615750c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 6158aefad959SEric W. Biederman ad.u.ipc_id = sma->key; 61591da177e4SLinus Torvalds 61606b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 61616b6bc620SStephen Smalley sid, isec->sid, SECCLASS_SEM, 61621da177e4SLinus Torvalds SEM__ASSOCIATE, &ad); 61631da177e4SLinus Torvalds } 61641da177e4SLinus Torvalds 61651da177e4SLinus Torvalds /* Note, at this point, sma is locked down */ 6166aefad959SEric W. Biederman static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd) 61671da177e4SLinus Torvalds { 61681da177e4SLinus Torvalds int err; 61691da177e4SLinus Torvalds u32 perms; 61701da177e4SLinus Torvalds 61711da177e4SLinus Torvalds switch (cmd) { 61721da177e4SLinus Torvalds case IPC_INFO: 61731da177e4SLinus Torvalds case SEM_INFO: 61741da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 61756b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 61766b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, 6177be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); 61781da177e4SLinus Torvalds case GETPID: 61791da177e4SLinus Torvalds case GETNCNT: 61801da177e4SLinus Torvalds case GETZCNT: 61811da177e4SLinus Torvalds perms = SEM__GETATTR; 61821da177e4SLinus Torvalds break; 61831da177e4SLinus Torvalds case GETVAL: 61841da177e4SLinus Torvalds case GETALL: 61851da177e4SLinus Torvalds perms = SEM__READ; 61861da177e4SLinus Torvalds break; 61871da177e4SLinus Torvalds case SETVAL: 61881da177e4SLinus Torvalds case SETALL: 61891da177e4SLinus Torvalds perms = SEM__WRITE; 61901da177e4SLinus Torvalds break; 61911da177e4SLinus Torvalds case IPC_RMID: 61921da177e4SLinus Torvalds perms = SEM__DESTROY; 61931da177e4SLinus Torvalds break; 61941da177e4SLinus Torvalds case IPC_SET: 61951da177e4SLinus Torvalds perms = SEM__SETATTR; 61961da177e4SLinus Torvalds break; 61971da177e4SLinus Torvalds case IPC_STAT: 61981da177e4SLinus Torvalds case SEM_STAT: 6199a280d6dcSDavidlohr Bueso case SEM_STAT_ANY: 62001da177e4SLinus Torvalds perms = SEM__GETATTR | SEM__ASSOCIATE; 62011da177e4SLinus Torvalds break; 62021da177e4SLinus Torvalds default: 62031da177e4SLinus Torvalds return 0; 62041da177e4SLinus Torvalds } 62051da177e4SLinus Torvalds 6206aefad959SEric W. Biederman err = ipc_has_perm(sma, perms); 62071da177e4SLinus Torvalds return err; 62081da177e4SLinus Torvalds } 62091da177e4SLinus Torvalds 6210aefad959SEric W. Biederman static int selinux_sem_semop(struct kern_ipc_perm *sma, 62111da177e4SLinus Torvalds struct sembuf *sops, unsigned nsops, int alter) 62121da177e4SLinus Torvalds { 62131da177e4SLinus Torvalds u32 perms; 62141da177e4SLinus Torvalds 62151da177e4SLinus Torvalds if (alter) 62161da177e4SLinus Torvalds perms = SEM__READ | SEM__WRITE; 62171da177e4SLinus Torvalds else 62181da177e4SLinus Torvalds perms = SEM__READ; 62191da177e4SLinus Torvalds 6220aefad959SEric W. Biederman return ipc_has_perm(sma, perms); 62211da177e4SLinus Torvalds } 62221da177e4SLinus Torvalds 62231da177e4SLinus Torvalds static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag) 62241da177e4SLinus Torvalds { 62251da177e4SLinus Torvalds u32 av = 0; 62261da177e4SLinus Torvalds 62271da177e4SLinus Torvalds av = 0; 62281da177e4SLinus Torvalds if (flag & S_IRUGO) 62291da177e4SLinus Torvalds av |= IPC__UNIX_READ; 62301da177e4SLinus Torvalds if (flag & S_IWUGO) 62311da177e4SLinus Torvalds av |= IPC__UNIX_WRITE; 62321da177e4SLinus Torvalds 62331da177e4SLinus Torvalds if (av == 0) 62341da177e4SLinus Torvalds return 0; 62351da177e4SLinus Torvalds 62366af963f1SStephen Smalley return ipc_has_perm(ipcp, av); 62371da177e4SLinus Torvalds } 62381da177e4SLinus Torvalds 6239713a04aeSAhmed S. Darwish static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid) 6240713a04aeSAhmed S. Darwish { 62417c653828SCasey Schaufler struct ipc_security_struct *isec = selinux_ipc(ipcp); 6242713a04aeSAhmed S. Darwish *secid = isec->sid; 6243713a04aeSAhmed S. Darwish } 6244713a04aeSAhmed S. Darwish 62451da177e4SLinus Torvalds static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode) 62461da177e4SLinus Torvalds { 62471da177e4SLinus Torvalds if (inode) 62481da177e4SLinus Torvalds inode_doinit_with_dentry(inode, dentry); 62491da177e4SLinus Torvalds } 62501da177e4SLinus Torvalds 62511da177e4SLinus Torvalds static int selinux_getprocattr(struct task_struct *p, 625204ff9708SAl Viro char *name, char **value) 62531da177e4SLinus Torvalds { 6254275bb41eSDavid Howells const struct task_security_struct *__tsec; 62558c8570fbSDustin Kirkland u32 sid; 62561da177e4SLinus Torvalds int error; 625704ff9708SAl Viro unsigned len; 62581da177e4SLinus Torvalds 6259275bb41eSDavid Howells rcu_read_lock(); 62600c6cfa62SCasey Schaufler __tsec = selinux_cred(__task_cred(p)); 62611da177e4SLinus Torvalds 6262be0554c9SStephen Smalley if (current != p) { 62636b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 62646b6bc620SStephen Smalley current_sid(), __tsec->sid, 6265be0554c9SStephen Smalley SECCLASS_PROCESS, PROCESS__GETATTR, NULL); 6266be0554c9SStephen Smalley if (error) 6267be0554c9SStephen Smalley goto bad; 6268be0554c9SStephen Smalley } 6269be0554c9SStephen Smalley 62701da177e4SLinus Torvalds if (!strcmp(name, "current")) 6271275bb41eSDavid Howells sid = __tsec->sid; 62721da177e4SLinus Torvalds else if (!strcmp(name, "prev")) 6273275bb41eSDavid Howells sid = __tsec->osid; 62741da177e4SLinus Torvalds else if (!strcmp(name, "exec")) 6275275bb41eSDavid Howells sid = __tsec->exec_sid; 62761da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 6277275bb41eSDavid Howells sid = __tsec->create_sid; 62784eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) 6279275bb41eSDavid Howells sid = __tsec->keycreate_sid; 628042c3e03eSEric Paris else if (!strcmp(name, "sockcreate")) 6281275bb41eSDavid Howells sid = __tsec->sockcreate_sid; 6282be0554c9SStephen Smalley else { 6283be0554c9SStephen Smalley error = -EINVAL; 6284be0554c9SStephen Smalley goto bad; 6285be0554c9SStephen Smalley } 6286275bb41eSDavid Howells rcu_read_unlock(); 62871da177e4SLinus Torvalds 62881da177e4SLinus Torvalds if (!sid) 62891da177e4SLinus Torvalds return 0; 62901da177e4SLinus Torvalds 6291aa8e712cSStephen Smalley error = security_sid_to_context(&selinux_state, sid, value, &len); 629204ff9708SAl Viro if (error) 629304ff9708SAl Viro return error; 629404ff9708SAl Viro return len; 6295275bb41eSDavid Howells 6296be0554c9SStephen Smalley bad: 6297275bb41eSDavid Howells rcu_read_unlock(); 6298be0554c9SStephen Smalley return error; 62991da177e4SLinus Torvalds } 63001da177e4SLinus Torvalds 6301b21507e2SStephen Smalley static int selinux_setprocattr(const char *name, void *value, size_t size) 63021da177e4SLinus Torvalds { 63031da177e4SLinus Torvalds struct task_security_struct *tsec; 6304d84f4f99SDavid Howells struct cred *new; 6305be0554c9SStephen Smalley u32 mysid = current_sid(), sid = 0, ptsid; 63061da177e4SLinus Torvalds int error; 63071da177e4SLinus Torvalds char *str = value; 63081da177e4SLinus Torvalds 63091da177e4SLinus Torvalds /* 63101da177e4SLinus Torvalds * Basic control over ability to set these attributes at all. 63111da177e4SLinus Torvalds */ 63121da177e4SLinus Torvalds if (!strcmp(name, "exec")) 63136b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 63146b6bc620SStephen Smalley mysid, mysid, SECCLASS_PROCESS, 6315be0554c9SStephen Smalley PROCESS__SETEXEC, NULL); 63161da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 63176b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 63186b6bc620SStephen Smalley mysid, mysid, SECCLASS_PROCESS, 6319be0554c9SStephen Smalley PROCESS__SETFSCREATE, NULL); 63204eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) 63216b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 63226b6bc620SStephen Smalley mysid, mysid, SECCLASS_PROCESS, 6323be0554c9SStephen Smalley PROCESS__SETKEYCREATE, NULL); 632442c3e03eSEric Paris else if (!strcmp(name, "sockcreate")) 63256b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 63266b6bc620SStephen Smalley mysid, mysid, SECCLASS_PROCESS, 6327be0554c9SStephen Smalley PROCESS__SETSOCKCREATE, NULL); 63281da177e4SLinus Torvalds else if (!strcmp(name, "current")) 63296b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 63306b6bc620SStephen Smalley mysid, mysid, SECCLASS_PROCESS, 6331be0554c9SStephen Smalley PROCESS__SETCURRENT, NULL); 63321da177e4SLinus Torvalds else 63331da177e4SLinus Torvalds error = -EINVAL; 63341da177e4SLinus Torvalds if (error) 63351da177e4SLinus Torvalds return error; 63361da177e4SLinus Torvalds 63371da177e4SLinus Torvalds /* Obtain a SID for the context, if one was specified. */ 6338a050a570SStephen Smalley if (size && str[0] && str[0] != '\n') { 63391da177e4SLinus Torvalds if (str[size-1] == '\n') { 63401da177e4SLinus Torvalds str[size-1] = 0; 63411da177e4SLinus Torvalds size--; 63421da177e4SLinus Torvalds } 6343aa8e712cSStephen Smalley error = security_context_to_sid(&selinux_state, value, size, 6344aa8e712cSStephen Smalley &sid, GFP_KERNEL); 634512b29f34SStephen Smalley if (error == -EINVAL && !strcmp(name, "fscreate")) { 6346db59000aSStephen Smalley if (!has_cap_mac_admin(true)) { 6347d6ea83ecSEric Paris struct audit_buffer *ab; 6348d6ea83ecSEric Paris size_t audit_size; 6349d6ea83ecSEric Paris 6350d6ea83ecSEric Paris /* We strip a nul only if it is at the end, otherwise the 6351d6ea83ecSEric Paris * context contains a nul and we should audit that */ 6352d6ea83ecSEric Paris if (str[size - 1] == '\0') 6353d6ea83ecSEric Paris audit_size = size - 1; 6354d6ea83ecSEric Paris else 6355d6ea83ecSEric Paris audit_size = size; 6356cdfb6b34SRichard Guy Briggs ab = audit_log_start(audit_context(), 6357cdfb6b34SRichard Guy Briggs GFP_ATOMIC, 6358cdfb6b34SRichard Guy Briggs AUDIT_SELINUX_ERR); 6359d6ea83ecSEric Paris audit_log_format(ab, "op=fscreate invalid_context="); 6360d6ea83ecSEric Paris audit_log_n_untrustedstring(ab, value, audit_size); 6361d6ea83ecSEric Paris audit_log_end(ab); 6362d6ea83ecSEric Paris 636312b29f34SStephen Smalley return error; 6364d6ea83ecSEric Paris } 6365aa8e712cSStephen Smalley error = security_context_to_sid_force( 6366aa8e712cSStephen Smalley &selinux_state, 6367aa8e712cSStephen Smalley value, size, &sid); 636812b29f34SStephen Smalley } 63691da177e4SLinus Torvalds if (error) 63701da177e4SLinus Torvalds return error; 63711da177e4SLinus Torvalds } 63721da177e4SLinus Torvalds 6373d84f4f99SDavid Howells new = prepare_creds(); 6374d84f4f99SDavid Howells if (!new) 6375d84f4f99SDavid Howells return -ENOMEM; 6376d84f4f99SDavid Howells 63771da177e4SLinus Torvalds /* Permission checking based on the specified context is 63781da177e4SLinus Torvalds performed during the actual operation (execve, 63791da177e4SLinus Torvalds open/mkdir/...), when we know the full context of the 6380d84f4f99SDavid Howells operation. See selinux_bprm_set_creds for the execve 63811da177e4SLinus Torvalds checks and may_create for the file creation checks. The 63821da177e4SLinus Torvalds operation will then fail if the context is not permitted. */ 63830c6cfa62SCasey Schaufler tsec = selinux_cred(new); 6384d84f4f99SDavid Howells if (!strcmp(name, "exec")) { 63851da177e4SLinus Torvalds tsec->exec_sid = sid; 6386d84f4f99SDavid Howells } else if (!strcmp(name, "fscreate")) { 63871da177e4SLinus Torvalds tsec->create_sid = sid; 6388d84f4f99SDavid Howells } else if (!strcmp(name, "keycreate")) { 6389464c258aSOndrej Mosnacek if (sid) { 6390464c258aSOndrej Mosnacek error = avc_has_perm(&selinux_state, mysid, sid, 6391464c258aSOndrej Mosnacek SECCLASS_KEY, KEY__CREATE, NULL); 63924eb582cfSMichael LeMay if (error) 6393d84f4f99SDavid Howells goto abort_change; 6394464c258aSOndrej Mosnacek } 63954eb582cfSMichael LeMay tsec->keycreate_sid = sid; 6396d84f4f99SDavid Howells } else if (!strcmp(name, "sockcreate")) { 639742c3e03eSEric Paris tsec->sockcreate_sid = sid; 6398d84f4f99SDavid Howells } else if (!strcmp(name, "current")) { 6399d84f4f99SDavid Howells error = -EINVAL; 64001da177e4SLinus Torvalds if (sid == 0) 6401d84f4f99SDavid Howells goto abort_change; 6402d9250deaSKaiGai Kohei 6403d84f4f99SDavid Howells /* Only allow single threaded processes to change context */ 6404d84f4f99SDavid Howells error = -EPERM; 64055bb459bbSOleg Nesterov if (!current_is_single_threaded()) { 6406aa8e712cSStephen Smalley error = security_bounded_transition(&selinux_state, 6407aa8e712cSStephen Smalley tsec->sid, sid); 6408d84f4f99SDavid Howells if (error) 6409d84f4f99SDavid Howells goto abort_change; 64101da177e4SLinus Torvalds } 64111da177e4SLinus Torvalds 64121da177e4SLinus Torvalds /* Check permissions for the transition. */ 64136b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 64146b6bc620SStephen Smalley tsec->sid, sid, SECCLASS_PROCESS, 64151da177e4SLinus Torvalds PROCESS__DYNTRANSITION, NULL); 64161da177e4SLinus Torvalds if (error) 6417d84f4f99SDavid Howells goto abort_change; 64181da177e4SLinus Torvalds 64191da177e4SLinus Torvalds /* Check for ptracing, and update the task SID if ok. 64201da177e4SLinus Torvalds Otherwise, leave SID unchanged and fail. */ 6421be0554c9SStephen Smalley ptsid = ptrace_parent_sid(); 64220c6181cbSPaul Moore if (ptsid != 0) { 64236b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 64246b6bc620SStephen Smalley ptsid, sid, SECCLASS_PROCESS, 6425d84f4f99SDavid Howells PROCESS__PTRACE, NULL); 6426d84f4f99SDavid Howells if (error) 6427d84f4f99SDavid Howells goto abort_change; 6428d84f4f99SDavid Howells } 6429d84f4f99SDavid Howells 6430d84f4f99SDavid Howells tsec->sid = sid; 6431d84f4f99SDavid Howells } else { 6432d84f4f99SDavid Howells error = -EINVAL; 6433d84f4f99SDavid Howells goto abort_change; 6434d84f4f99SDavid Howells } 6435d84f4f99SDavid Howells 6436d84f4f99SDavid Howells commit_creds(new); 64371da177e4SLinus Torvalds return size; 6438d84f4f99SDavid Howells 6439d84f4f99SDavid Howells abort_change: 6440d84f4f99SDavid Howells abort_creds(new); 6441d84f4f99SDavid Howells return error; 64421da177e4SLinus Torvalds } 64431da177e4SLinus Torvalds 6444746df9b5SDavid Quigley static int selinux_ismaclabel(const char *name) 6445746df9b5SDavid Quigley { 6446746df9b5SDavid Quigley return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0); 6447746df9b5SDavid Quigley } 6448746df9b5SDavid Quigley 6449dc49c1f9SCatherine Zhang static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) 6450dc49c1f9SCatherine Zhang { 6451aa8e712cSStephen Smalley return security_sid_to_context(&selinux_state, secid, 6452aa8e712cSStephen Smalley secdata, seclen); 6453dc49c1f9SCatherine Zhang } 6454dc49c1f9SCatherine Zhang 64557bf570dcSDavid Howells static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid) 645663cb3449SDavid Howells { 6457aa8e712cSStephen Smalley return security_context_to_sid(&selinux_state, secdata, seclen, 6458aa8e712cSStephen Smalley secid, GFP_KERNEL); 645963cb3449SDavid Howells } 646063cb3449SDavid Howells 6461dc49c1f9SCatherine Zhang static void selinux_release_secctx(char *secdata, u32 seclen) 6462dc49c1f9SCatherine Zhang { 6463dc49c1f9SCatherine Zhang kfree(secdata); 6464dc49c1f9SCatherine Zhang } 6465dc49c1f9SCatherine Zhang 64666f3be9f5SAndreas Gruenbacher static void selinux_inode_invalidate_secctx(struct inode *inode) 64676f3be9f5SAndreas Gruenbacher { 646880788c22SCasey Schaufler struct inode_security_struct *isec = selinux_inode(inode); 64696f3be9f5SAndreas Gruenbacher 64709287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 64716f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INVALID; 64729287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 64736f3be9f5SAndreas Gruenbacher } 64746f3be9f5SAndreas Gruenbacher 64751ee65e37SDavid P. Quigley /* 64761ee65e37SDavid P. Quigley * called with inode->i_mutex locked 64771ee65e37SDavid P. Quigley */ 64781ee65e37SDavid P. Quigley static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen) 64791ee65e37SDavid P. Quigley { 648053e0c2aaSOndrej Mosnacek int rc = selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, 648153e0c2aaSOndrej Mosnacek ctx, ctxlen, 0); 648253e0c2aaSOndrej Mosnacek /* Do not return error when suppressing label (SBLABEL_MNT not set). */ 648353e0c2aaSOndrej Mosnacek return rc == -EOPNOTSUPP ? 0 : rc; 64841ee65e37SDavid P. Quigley } 64851ee65e37SDavid P. Quigley 64861ee65e37SDavid P. Quigley /* 64871ee65e37SDavid P. Quigley * called with inode->i_mutex locked 64881ee65e37SDavid P. Quigley */ 64891ee65e37SDavid P. Quigley static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen) 64901ee65e37SDavid P. Quigley { 64911ee65e37SDavid P. Quigley return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0); 64921ee65e37SDavid P. Quigley } 64931ee65e37SDavid P. Quigley 64941ee65e37SDavid P. Quigley static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen) 64951ee65e37SDavid P. Quigley { 64961ee65e37SDavid P. Quigley int len = 0; 64971ee65e37SDavid P. Quigley len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX, 64981ee65e37SDavid P. Quigley ctx, true); 64991ee65e37SDavid P. Quigley if (len < 0) 65001ee65e37SDavid P. Quigley return len; 65011ee65e37SDavid P. Quigley *ctxlen = len; 65021ee65e37SDavid P. Quigley return 0; 65031ee65e37SDavid P. Quigley } 6504d720024eSMichael LeMay #ifdef CONFIG_KEYS 6505d720024eSMichael LeMay 6506d84f4f99SDavid Howells static int selinux_key_alloc(struct key *k, const struct cred *cred, 65077e047ef5SDavid Howells unsigned long flags) 6508d720024eSMichael LeMay { 6509d84f4f99SDavid Howells const struct task_security_struct *tsec; 6510d720024eSMichael LeMay struct key_security_struct *ksec; 6511d720024eSMichael LeMay 6512d720024eSMichael LeMay ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL); 6513d720024eSMichael LeMay if (!ksec) 6514d720024eSMichael LeMay return -ENOMEM; 6515d720024eSMichael LeMay 65160c6cfa62SCasey Schaufler tsec = selinux_cred(cred); 6517d84f4f99SDavid Howells if (tsec->keycreate_sid) 6518d84f4f99SDavid Howells ksec->sid = tsec->keycreate_sid; 65194eb582cfSMichael LeMay else 6520d84f4f99SDavid Howells ksec->sid = tsec->sid; 6521d720024eSMichael LeMay 6522275bb41eSDavid Howells k->security = ksec; 6523d720024eSMichael LeMay return 0; 6524d720024eSMichael LeMay } 6525d720024eSMichael LeMay 6526d720024eSMichael LeMay static void selinux_key_free(struct key *k) 6527d720024eSMichael LeMay { 6528d720024eSMichael LeMay struct key_security_struct *ksec = k->security; 6529d720024eSMichael LeMay 6530d720024eSMichael LeMay k->security = NULL; 6531d720024eSMichael LeMay kfree(ksec); 6532d720024eSMichael LeMay } 6533d720024eSMichael LeMay 6534d720024eSMichael LeMay static int selinux_key_permission(key_ref_t key_ref, 6535d84f4f99SDavid Howells const struct cred *cred, 6536f5895943SDavid Howells unsigned perm) 6537d720024eSMichael LeMay { 6538d720024eSMichael LeMay struct key *key; 6539d720024eSMichael LeMay struct key_security_struct *ksec; 6540275bb41eSDavid Howells u32 sid; 6541d720024eSMichael LeMay 6542d720024eSMichael LeMay /* if no specific permissions are requested, we skip the 6543d720024eSMichael LeMay permission check. No serious, additional covert channels 6544d720024eSMichael LeMay appear to be created. */ 6545d720024eSMichael LeMay if (perm == 0) 6546d720024eSMichael LeMay return 0; 6547d720024eSMichael LeMay 6548d84f4f99SDavid Howells sid = cred_sid(cred); 6549275bb41eSDavid Howells 6550275bb41eSDavid Howells key = key_ref_to_ptr(key_ref); 6551275bb41eSDavid Howells ksec = key->security; 6552275bb41eSDavid Howells 65536b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 6554028db3e2SLinus Torvalds sid, ksec->sid, SECCLASS_KEY, perm, NULL); 6555d720024eSMichael LeMay } 6556d720024eSMichael LeMay 655770a5bb72SDavid Howells static int selinux_key_getsecurity(struct key *key, char **_buffer) 655870a5bb72SDavid Howells { 655970a5bb72SDavid Howells struct key_security_struct *ksec = key->security; 656070a5bb72SDavid Howells char *context = NULL; 656170a5bb72SDavid Howells unsigned len; 656270a5bb72SDavid Howells int rc; 656370a5bb72SDavid Howells 6564aa8e712cSStephen Smalley rc = security_sid_to_context(&selinux_state, ksec->sid, 6565aa8e712cSStephen Smalley &context, &len); 656670a5bb72SDavid Howells if (!rc) 656770a5bb72SDavid Howells rc = len; 656870a5bb72SDavid Howells *_buffer = context; 656970a5bb72SDavid Howells return rc; 657070a5bb72SDavid Howells } 65713a976fa6SDaniel Jurgens #endif 657270a5bb72SDavid Howells 65733a976fa6SDaniel Jurgens #ifdef CONFIG_SECURITY_INFINIBAND 6574cfc4d882SDaniel Jurgens static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val) 6575cfc4d882SDaniel Jurgens { 6576cfc4d882SDaniel Jurgens struct common_audit_data ad; 6577cfc4d882SDaniel Jurgens int err; 6578cfc4d882SDaniel Jurgens u32 sid = 0; 6579cfc4d882SDaniel Jurgens struct ib_security_struct *sec = ib_sec; 6580cfc4d882SDaniel Jurgens struct lsm_ibpkey_audit ibpkey; 6581cfc4d882SDaniel Jurgens 6582409dcf31SDaniel Jurgens err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid); 6583cfc4d882SDaniel Jurgens if (err) 6584cfc4d882SDaniel Jurgens return err; 6585cfc4d882SDaniel Jurgens 6586cfc4d882SDaniel Jurgens ad.type = LSM_AUDIT_DATA_IBPKEY; 6587cfc4d882SDaniel Jurgens ibpkey.subnet_prefix = subnet_prefix; 6588cfc4d882SDaniel Jurgens ibpkey.pkey = pkey_val; 6589cfc4d882SDaniel Jurgens ad.u.ibpkey = &ibpkey; 65906b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 65916b6bc620SStephen Smalley sec->sid, sid, 6592cfc4d882SDaniel Jurgens SECCLASS_INFINIBAND_PKEY, 6593cfc4d882SDaniel Jurgens INFINIBAND_PKEY__ACCESS, &ad); 6594cfc4d882SDaniel Jurgens } 6595cfc4d882SDaniel Jurgens 6596ab861dfcSDaniel Jurgens static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name, 6597ab861dfcSDaniel Jurgens u8 port_num) 6598ab861dfcSDaniel Jurgens { 6599ab861dfcSDaniel Jurgens struct common_audit_data ad; 6600ab861dfcSDaniel Jurgens int err; 6601ab861dfcSDaniel Jurgens u32 sid = 0; 6602ab861dfcSDaniel Jurgens struct ib_security_struct *sec = ib_sec; 6603ab861dfcSDaniel Jurgens struct lsm_ibendport_audit ibendport; 6604ab861dfcSDaniel Jurgens 6605aa8e712cSStephen Smalley err = security_ib_endport_sid(&selinux_state, dev_name, port_num, 6606aa8e712cSStephen Smalley &sid); 6607ab861dfcSDaniel Jurgens 6608ab861dfcSDaniel Jurgens if (err) 6609ab861dfcSDaniel Jurgens return err; 6610ab861dfcSDaniel Jurgens 6611ab861dfcSDaniel Jurgens ad.type = LSM_AUDIT_DATA_IBENDPORT; 6612ab861dfcSDaniel Jurgens strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name)); 6613ab861dfcSDaniel Jurgens ibendport.port = port_num; 6614ab861dfcSDaniel Jurgens ad.u.ibendport = &ibendport; 66156b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 66166b6bc620SStephen Smalley sec->sid, sid, 6617ab861dfcSDaniel Jurgens SECCLASS_INFINIBAND_ENDPORT, 6618ab861dfcSDaniel Jurgens INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad); 6619ab861dfcSDaniel Jurgens } 6620ab861dfcSDaniel Jurgens 66213a976fa6SDaniel Jurgens static int selinux_ib_alloc_security(void **ib_sec) 66223a976fa6SDaniel Jurgens { 66233a976fa6SDaniel Jurgens struct ib_security_struct *sec; 66243a976fa6SDaniel Jurgens 66253a976fa6SDaniel Jurgens sec = kzalloc(sizeof(*sec), GFP_KERNEL); 66263a976fa6SDaniel Jurgens if (!sec) 66273a976fa6SDaniel Jurgens return -ENOMEM; 66283a976fa6SDaniel Jurgens sec->sid = current_sid(); 66293a976fa6SDaniel Jurgens 66303a976fa6SDaniel Jurgens *ib_sec = sec; 66313a976fa6SDaniel Jurgens return 0; 66323a976fa6SDaniel Jurgens } 66333a976fa6SDaniel Jurgens 66343a976fa6SDaniel Jurgens static void selinux_ib_free_security(void *ib_sec) 66353a976fa6SDaniel Jurgens { 66363a976fa6SDaniel Jurgens kfree(ib_sec); 66373a976fa6SDaniel Jurgens } 6638d720024eSMichael LeMay #endif 6639d720024eSMichael LeMay 6640ec27c356SChenbo Feng #ifdef CONFIG_BPF_SYSCALL 6641ec27c356SChenbo Feng static int selinux_bpf(int cmd, union bpf_attr *attr, 6642ec27c356SChenbo Feng unsigned int size) 6643ec27c356SChenbo Feng { 6644ec27c356SChenbo Feng u32 sid = current_sid(); 6645ec27c356SChenbo Feng int ret; 6646ec27c356SChenbo Feng 6647ec27c356SChenbo Feng switch (cmd) { 6648ec27c356SChenbo Feng case BPF_MAP_CREATE: 66496b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 66506b6bc620SStephen Smalley sid, sid, SECCLASS_BPF, BPF__MAP_CREATE, 6651ec27c356SChenbo Feng NULL); 6652ec27c356SChenbo Feng break; 6653ec27c356SChenbo Feng case BPF_PROG_LOAD: 66546b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 66556b6bc620SStephen Smalley sid, sid, SECCLASS_BPF, BPF__PROG_LOAD, 6656ec27c356SChenbo Feng NULL); 6657ec27c356SChenbo Feng break; 6658ec27c356SChenbo Feng default: 6659ec27c356SChenbo Feng ret = 0; 6660ec27c356SChenbo Feng break; 6661ec27c356SChenbo Feng } 6662ec27c356SChenbo Feng 6663ec27c356SChenbo Feng return ret; 6664ec27c356SChenbo Feng } 6665ec27c356SChenbo Feng 6666ec27c356SChenbo Feng static u32 bpf_map_fmode_to_av(fmode_t fmode) 6667ec27c356SChenbo Feng { 6668ec27c356SChenbo Feng u32 av = 0; 6669ec27c356SChenbo Feng 6670ec27c356SChenbo Feng if (fmode & FMODE_READ) 6671ec27c356SChenbo Feng av |= BPF__MAP_READ; 6672ec27c356SChenbo Feng if (fmode & FMODE_WRITE) 6673ec27c356SChenbo Feng av |= BPF__MAP_WRITE; 6674ec27c356SChenbo Feng return av; 6675ec27c356SChenbo Feng } 6676ec27c356SChenbo Feng 6677f66e448cSChenbo Feng /* This function will check the file pass through unix socket or binder to see 6678f66e448cSChenbo Feng * if it is a bpf related object. And apply correspinding checks on the bpf 6679f66e448cSChenbo Feng * object based on the type. The bpf maps and programs, not like other files and 6680f66e448cSChenbo Feng * socket, are using a shared anonymous inode inside the kernel as their inode. 6681f66e448cSChenbo Feng * So checking that inode cannot identify if the process have privilege to 6682f66e448cSChenbo Feng * access the bpf object and that's why we have to add this additional check in 6683f66e448cSChenbo Feng * selinux_file_receive and selinux_binder_transfer_files. 6684f66e448cSChenbo Feng */ 6685f66e448cSChenbo Feng static int bpf_fd_pass(struct file *file, u32 sid) 6686f66e448cSChenbo Feng { 6687f66e448cSChenbo Feng struct bpf_security_struct *bpfsec; 6688f66e448cSChenbo Feng struct bpf_prog *prog; 6689f66e448cSChenbo Feng struct bpf_map *map; 6690f66e448cSChenbo Feng int ret; 6691f66e448cSChenbo Feng 6692f66e448cSChenbo Feng if (file->f_op == &bpf_map_fops) { 6693f66e448cSChenbo Feng map = file->private_data; 6694f66e448cSChenbo Feng bpfsec = map->security; 66956b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 66966b6bc620SStephen Smalley sid, bpfsec->sid, SECCLASS_BPF, 6697f66e448cSChenbo Feng bpf_map_fmode_to_av(file->f_mode), NULL); 6698f66e448cSChenbo Feng if (ret) 6699f66e448cSChenbo Feng return ret; 6700f66e448cSChenbo Feng } else if (file->f_op == &bpf_prog_fops) { 6701f66e448cSChenbo Feng prog = file->private_data; 6702f66e448cSChenbo Feng bpfsec = prog->aux->security; 67036b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 67046b6bc620SStephen Smalley sid, bpfsec->sid, SECCLASS_BPF, 6705f66e448cSChenbo Feng BPF__PROG_RUN, NULL); 6706f66e448cSChenbo Feng if (ret) 6707f66e448cSChenbo Feng return ret; 6708f66e448cSChenbo Feng } 6709f66e448cSChenbo Feng return 0; 6710f66e448cSChenbo Feng } 6711f66e448cSChenbo Feng 6712ec27c356SChenbo Feng static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode) 6713ec27c356SChenbo Feng { 6714ec27c356SChenbo Feng u32 sid = current_sid(); 6715ec27c356SChenbo Feng struct bpf_security_struct *bpfsec; 6716ec27c356SChenbo Feng 6717ec27c356SChenbo Feng bpfsec = map->security; 67186b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 67196b6bc620SStephen Smalley sid, bpfsec->sid, SECCLASS_BPF, 6720ec27c356SChenbo Feng bpf_map_fmode_to_av(fmode), NULL); 6721ec27c356SChenbo Feng } 6722ec27c356SChenbo Feng 6723ec27c356SChenbo Feng static int selinux_bpf_prog(struct bpf_prog *prog) 6724ec27c356SChenbo Feng { 6725ec27c356SChenbo Feng u32 sid = current_sid(); 6726ec27c356SChenbo Feng struct bpf_security_struct *bpfsec; 6727ec27c356SChenbo Feng 6728ec27c356SChenbo Feng bpfsec = prog->aux->security; 67296b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 67306b6bc620SStephen Smalley sid, bpfsec->sid, SECCLASS_BPF, 6731ec27c356SChenbo Feng BPF__PROG_RUN, NULL); 6732ec27c356SChenbo Feng } 6733ec27c356SChenbo Feng 6734ec27c356SChenbo Feng static int selinux_bpf_map_alloc(struct bpf_map *map) 6735ec27c356SChenbo Feng { 6736ec27c356SChenbo Feng struct bpf_security_struct *bpfsec; 6737ec27c356SChenbo Feng 6738ec27c356SChenbo Feng bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL); 6739ec27c356SChenbo Feng if (!bpfsec) 6740ec27c356SChenbo Feng return -ENOMEM; 6741ec27c356SChenbo Feng 6742ec27c356SChenbo Feng bpfsec->sid = current_sid(); 6743ec27c356SChenbo Feng map->security = bpfsec; 6744ec27c356SChenbo Feng 6745ec27c356SChenbo Feng return 0; 6746ec27c356SChenbo Feng } 6747ec27c356SChenbo Feng 6748ec27c356SChenbo Feng static void selinux_bpf_map_free(struct bpf_map *map) 6749ec27c356SChenbo Feng { 6750ec27c356SChenbo Feng struct bpf_security_struct *bpfsec = map->security; 6751ec27c356SChenbo Feng 6752ec27c356SChenbo Feng map->security = NULL; 6753ec27c356SChenbo Feng kfree(bpfsec); 6754ec27c356SChenbo Feng } 6755ec27c356SChenbo Feng 6756ec27c356SChenbo Feng static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux) 6757ec27c356SChenbo Feng { 6758ec27c356SChenbo Feng struct bpf_security_struct *bpfsec; 6759ec27c356SChenbo Feng 6760ec27c356SChenbo Feng bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL); 6761ec27c356SChenbo Feng if (!bpfsec) 6762ec27c356SChenbo Feng return -ENOMEM; 6763ec27c356SChenbo Feng 6764ec27c356SChenbo Feng bpfsec->sid = current_sid(); 6765ec27c356SChenbo Feng aux->security = bpfsec; 6766ec27c356SChenbo Feng 6767ec27c356SChenbo Feng return 0; 6768ec27c356SChenbo Feng } 6769ec27c356SChenbo Feng 6770ec27c356SChenbo Feng static void selinux_bpf_prog_free(struct bpf_prog_aux *aux) 6771ec27c356SChenbo Feng { 6772ec27c356SChenbo Feng struct bpf_security_struct *bpfsec = aux->security; 6773ec27c356SChenbo Feng 6774ec27c356SChenbo Feng aux->security = NULL; 6775ec27c356SChenbo Feng kfree(bpfsec); 6776ec27c356SChenbo Feng } 6777ec27c356SChenbo Feng #endif 6778ec27c356SChenbo Feng 677959438b46SStephen Smalley static int selinux_lockdown(enum lockdown_reason what) 678059438b46SStephen Smalley { 678159438b46SStephen Smalley struct common_audit_data ad; 678259438b46SStephen Smalley u32 sid = current_sid(); 678359438b46SStephen Smalley int invalid_reason = (what <= LOCKDOWN_NONE) || 678459438b46SStephen Smalley (what == LOCKDOWN_INTEGRITY_MAX) || 678559438b46SStephen Smalley (what >= LOCKDOWN_CONFIDENTIALITY_MAX); 678659438b46SStephen Smalley 678759438b46SStephen Smalley if (WARN(invalid_reason, "Invalid lockdown reason")) { 678859438b46SStephen Smalley audit_log(audit_context(), 678959438b46SStephen Smalley GFP_ATOMIC, AUDIT_SELINUX_ERR, 679059438b46SStephen Smalley "lockdown_reason=invalid"); 679159438b46SStephen Smalley return -EINVAL; 679259438b46SStephen Smalley } 679359438b46SStephen Smalley 679459438b46SStephen Smalley ad.type = LSM_AUDIT_DATA_LOCKDOWN; 679559438b46SStephen Smalley ad.u.reason = what; 679659438b46SStephen Smalley 679759438b46SStephen Smalley if (what <= LOCKDOWN_INTEGRITY_MAX) 679859438b46SStephen Smalley return avc_has_perm(&selinux_state, 679959438b46SStephen Smalley sid, sid, SECCLASS_LOCKDOWN, 680059438b46SStephen Smalley LOCKDOWN__INTEGRITY, &ad); 680159438b46SStephen Smalley else 680259438b46SStephen Smalley return avc_has_perm(&selinux_state, 680359438b46SStephen Smalley sid, sid, SECCLASS_LOCKDOWN, 680459438b46SStephen Smalley LOCKDOWN__CONFIDENTIALITY, &ad); 680559438b46SStephen Smalley } 680659438b46SStephen Smalley 6807bbd3662aSCasey Schaufler struct lsm_blob_sizes selinux_blob_sizes __lsm_ro_after_init = { 6808bbd3662aSCasey Schaufler .lbs_cred = sizeof(struct task_security_struct), 680933bf60caSCasey Schaufler .lbs_file = sizeof(struct file_security_struct), 6810afb1cbe3SCasey Schaufler .lbs_inode = sizeof(struct inode_security_struct), 6811ecd5f82eSCasey Schaufler .lbs_ipc = sizeof(struct ipc_security_struct), 6812ecd5f82eSCasey Schaufler .lbs_msg_msg = sizeof(struct msg_security_struct), 6813bbd3662aSCasey Schaufler }; 6814bbd3662aSCasey Schaufler 6815da97e184SJoel Fernandes (Google) #ifdef CONFIG_PERF_EVENTS 6816da97e184SJoel Fernandes (Google) static int selinux_perf_event_open(struct perf_event_attr *attr, int type) 6817da97e184SJoel Fernandes (Google) { 6818da97e184SJoel Fernandes (Google) u32 requested, sid = current_sid(); 6819da97e184SJoel Fernandes (Google) 6820da97e184SJoel Fernandes (Google) if (type == PERF_SECURITY_OPEN) 6821da97e184SJoel Fernandes (Google) requested = PERF_EVENT__OPEN; 6822da97e184SJoel Fernandes (Google) else if (type == PERF_SECURITY_CPU) 6823da97e184SJoel Fernandes (Google) requested = PERF_EVENT__CPU; 6824da97e184SJoel Fernandes (Google) else if (type == PERF_SECURITY_KERNEL) 6825da97e184SJoel Fernandes (Google) requested = PERF_EVENT__KERNEL; 6826da97e184SJoel Fernandes (Google) else if (type == PERF_SECURITY_TRACEPOINT) 6827da97e184SJoel Fernandes (Google) requested = PERF_EVENT__TRACEPOINT; 6828da97e184SJoel Fernandes (Google) else 6829da97e184SJoel Fernandes (Google) return -EINVAL; 6830da97e184SJoel Fernandes (Google) 6831da97e184SJoel Fernandes (Google) return avc_has_perm(&selinux_state, sid, sid, SECCLASS_PERF_EVENT, 6832da97e184SJoel Fernandes (Google) requested, NULL); 6833da97e184SJoel Fernandes (Google) } 6834da97e184SJoel Fernandes (Google) 6835da97e184SJoel Fernandes (Google) static int selinux_perf_event_alloc(struct perf_event *event) 6836da97e184SJoel Fernandes (Google) { 6837da97e184SJoel Fernandes (Google) struct perf_event_security_struct *perfsec; 6838da97e184SJoel Fernandes (Google) 6839da97e184SJoel Fernandes (Google) perfsec = kzalloc(sizeof(*perfsec), GFP_KERNEL); 6840da97e184SJoel Fernandes (Google) if (!perfsec) 6841da97e184SJoel Fernandes (Google) return -ENOMEM; 6842da97e184SJoel Fernandes (Google) 6843da97e184SJoel Fernandes (Google) perfsec->sid = current_sid(); 6844da97e184SJoel Fernandes (Google) event->security = perfsec; 6845da97e184SJoel Fernandes (Google) 6846da97e184SJoel Fernandes (Google) return 0; 6847da97e184SJoel Fernandes (Google) } 6848da97e184SJoel Fernandes (Google) 6849da97e184SJoel Fernandes (Google) static void selinux_perf_event_free(struct perf_event *event) 6850da97e184SJoel Fernandes (Google) { 6851da97e184SJoel Fernandes (Google) struct perf_event_security_struct *perfsec = event->security; 6852da97e184SJoel Fernandes (Google) 6853da97e184SJoel Fernandes (Google) event->security = NULL; 6854da97e184SJoel Fernandes (Google) kfree(perfsec); 6855da97e184SJoel Fernandes (Google) } 6856da97e184SJoel Fernandes (Google) 6857da97e184SJoel Fernandes (Google) static int selinux_perf_event_read(struct perf_event *event) 6858da97e184SJoel Fernandes (Google) { 6859da97e184SJoel Fernandes (Google) struct perf_event_security_struct *perfsec = event->security; 6860da97e184SJoel Fernandes (Google) u32 sid = current_sid(); 6861da97e184SJoel Fernandes (Google) 6862da97e184SJoel Fernandes (Google) return avc_has_perm(&selinux_state, sid, perfsec->sid, 6863da97e184SJoel Fernandes (Google) SECCLASS_PERF_EVENT, PERF_EVENT__READ, NULL); 6864da97e184SJoel Fernandes (Google) } 6865da97e184SJoel Fernandes (Google) 6866da97e184SJoel Fernandes (Google) static int selinux_perf_event_write(struct perf_event *event) 6867da97e184SJoel Fernandes (Google) { 6868da97e184SJoel Fernandes (Google) struct perf_event_security_struct *perfsec = event->security; 6869da97e184SJoel Fernandes (Google) u32 sid = current_sid(); 6870da97e184SJoel Fernandes (Google) 6871da97e184SJoel Fernandes (Google) return avc_has_perm(&selinux_state, sid, perfsec->sid, 6872da97e184SJoel Fernandes (Google) SECCLASS_PERF_EVENT, PERF_EVENT__WRITE, NULL); 6873da97e184SJoel Fernandes (Google) } 6874da97e184SJoel Fernandes (Google) #endif 6875da97e184SJoel Fernandes (Google) 6876cfff75d8SOndrej Mosnacek /* 6877cfff75d8SOndrej Mosnacek * IMPORTANT NOTE: When adding new hooks, please be careful to keep this order: 6878cfff75d8SOndrej Mosnacek * 1. any hooks that don't belong to (2.) or (3.) below, 6879cfff75d8SOndrej Mosnacek * 2. hooks that both access structures allocated by other hooks, and allocate 6880cfff75d8SOndrej Mosnacek * structures that can be later accessed by other hooks (mostly "cloning" 6881cfff75d8SOndrej Mosnacek * hooks), 6882cfff75d8SOndrej Mosnacek * 3. hooks that only allocate structures that can be later accessed by other 6883cfff75d8SOndrej Mosnacek * hooks ("allocating" hooks). 6884cfff75d8SOndrej Mosnacek * 6885cfff75d8SOndrej Mosnacek * Please follow block comment delimiters in the list to keep this order. 6886cfff75d8SOndrej Mosnacek * 6887cfff75d8SOndrej Mosnacek * This ordering is needed for SELinux runtime disable to work at least somewhat 6888cfff75d8SOndrej Mosnacek * safely. Breaking the ordering rules above might lead to NULL pointer derefs 6889cfff75d8SOndrej Mosnacek * when disabling SELinux at runtime. 6890cfff75d8SOndrej Mosnacek */ 6891ca97d939SJames Morris static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = { 6892e20b043aSCasey Schaufler LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr), 6893e20b043aSCasey Schaufler LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction), 6894e20b043aSCasey Schaufler LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder), 6895e20b043aSCasey Schaufler LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file), 6896076c54c5SAhmed S. Darwish 6897e20b043aSCasey Schaufler LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check), 6898e20b043aSCasey Schaufler LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme), 6899e20b043aSCasey Schaufler LSM_HOOK_INIT(capget, selinux_capget), 6900e20b043aSCasey Schaufler LSM_HOOK_INIT(capset, selinux_capset), 6901e20b043aSCasey Schaufler LSM_HOOK_INIT(capable, selinux_capable), 6902e20b043aSCasey Schaufler LSM_HOOK_INIT(quotactl, selinux_quotactl), 6903e20b043aSCasey Schaufler LSM_HOOK_INIT(quota_on, selinux_quota_on), 6904e20b043aSCasey Schaufler LSM_HOOK_INIT(syslog, selinux_syslog), 6905e20b043aSCasey Schaufler LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory), 690679af7307SStephen Smalley 6907e20b043aSCasey Schaufler LSM_HOOK_INIT(netlink_send, selinux_netlink_send), 69081da177e4SLinus Torvalds 6909e20b043aSCasey Schaufler LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds), 6910e20b043aSCasey Schaufler LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds), 6911e20b043aSCasey Schaufler LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds), 69121da177e4SLinus Torvalds 6913e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security), 6914204cc0ccSAl Viro LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts), 6915e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_remount, selinux_sb_remount), 6916e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount), 6917e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options), 6918e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs), 6919e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_mount, selinux_mount), 6920e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_umount, selinux_umount), 6921e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts), 6922e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts), 69231da177e4SLinus Torvalds 692498aa0034SStephen Smalley LSM_HOOK_INIT(move_mount, selinux_move_mount), 692598aa0034SStephen Smalley 6926e20b043aSCasey Schaufler LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security), 6927a518b0a5SVivek Goyal LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as), 6928e0007529SEric Paris 6929e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security), 6930e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security), 6931e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_create, selinux_inode_create), 6932e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_link, selinux_inode_link), 6933e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink), 6934e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink), 6935e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir), 6936e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir), 6937e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod), 6938e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_rename, selinux_inode_rename), 6939e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink), 6940e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link), 6941e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_permission, selinux_inode_permission), 6942e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr), 6943e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr), 6944e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr), 6945e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr), 6946e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr), 6947e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr), 6948e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr), 6949e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity), 6950e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity), 6951e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity), 6952e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid), 695356909eb3SVivek Goyal LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up), 695419472b69SVivek Goyal LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr), 6955ac5656d8SAaron Goidel LSM_HOOK_INIT(path_notify, selinux_path_notify), 69561da177e4SLinus Torvalds 6957ec882da5SOndrej Mosnacek LSM_HOOK_INIT(kernfs_init_security, selinux_kernfs_init_security), 6958ec882da5SOndrej Mosnacek 6959e20b043aSCasey Schaufler LSM_HOOK_INIT(file_permission, selinux_file_permission), 6960e20b043aSCasey Schaufler LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security), 6961e20b043aSCasey Schaufler LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl), 6962e20b043aSCasey Schaufler LSM_HOOK_INIT(mmap_file, selinux_mmap_file), 6963e20b043aSCasey Schaufler LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr), 6964e20b043aSCasey Schaufler LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect), 6965e20b043aSCasey Schaufler LSM_HOOK_INIT(file_lock, selinux_file_lock), 6966e20b043aSCasey Schaufler LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl), 6967e20b043aSCasey Schaufler LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner), 6968e20b043aSCasey Schaufler LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask), 6969e20b043aSCasey Schaufler LSM_HOOK_INIT(file_receive, selinux_file_receive), 69701da177e4SLinus Torvalds 6971e20b043aSCasey Schaufler LSM_HOOK_INIT(file_open, selinux_file_open), 69721da177e4SLinus Torvalds 6973a79be238STetsuo Handa LSM_HOOK_INIT(task_alloc, selinux_task_alloc), 6974e20b043aSCasey Schaufler LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare), 6975e20b043aSCasey Schaufler LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer), 69763ec30113SMatthew Garrett LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid), 6977e20b043aSCasey Schaufler LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as), 6978e20b043aSCasey Schaufler LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as), 6979e20b043aSCasey Schaufler LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request), 6980c77b8cdfSMimi Zohar LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data), 698161d612eaSJeff Vander Stoep LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file), 6982e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid), 6983e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid), 6984e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getsid, selinux_task_getsid), 6985e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid), 6986e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setnice, selinux_task_setnice), 6987e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio), 6988e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio), 6989791ec491SStephen Smalley LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit), 6990e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit), 6991e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler), 6992e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler), 6993e20b043aSCasey Schaufler LSM_HOOK_INIT(task_movememory, selinux_task_movememory), 6994e20b043aSCasey Schaufler LSM_HOOK_INIT(task_kill, selinux_task_kill), 6995e20b043aSCasey Schaufler LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode), 6996788e7dd4SYuichi Nakamura 6997e20b043aSCasey Schaufler LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission), 6998e20b043aSCasey Schaufler LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid), 69991da177e4SLinus Torvalds 7000e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate), 7001e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl), 7002e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd), 7003e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv), 70041da177e4SLinus Torvalds 7005e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_associate, selinux_shm_associate), 7006e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl), 7007e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat), 70081da177e4SLinus Torvalds 7009e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_associate, selinux_sem_associate), 7010e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl), 7011e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_semop, selinux_sem_semop), 70121da177e4SLinus Torvalds 7013e20b043aSCasey Schaufler LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate), 70141da177e4SLinus Torvalds 7015e20b043aSCasey Schaufler LSM_HOOK_INIT(getprocattr, selinux_getprocattr), 7016e20b043aSCasey Schaufler LSM_HOOK_INIT(setprocattr, selinux_setprocattr), 70171da177e4SLinus Torvalds 7018e20b043aSCasey Schaufler LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel), 7019e20b043aSCasey Schaufler LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid), 7020e20b043aSCasey Schaufler LSM_HOOK_INIT(release_secctx, selinux_release_secctx), 70216f3be9f5SAndreas Gruenbacher LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx), 7022e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx), 7023e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx), 70241da177e4SLinus Torvalds 7025e20b043aSCasey Schaufler LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect), 7026e20b043aSCasey Schaufler LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send), 7027dc49c1f9SCatherine Zhang 7028e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_create, selinux_socket_create), 7029e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create), 70300b811db2SDavid Herrmann LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair), 7031e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_bind, selinux_socket_bind), 7032e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_connect, selinux_socket_connect), 7033e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_listen, selinux_socket_listen), 7034e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_accept, selinux_socket_accept), 7035e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg), 7036e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg), 7037e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname), 7038e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername), 7039e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt), 7040e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt), 7041e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown), 7042e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb), 7043e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getpeersec_stream, 7044e20b043aSCasey Schaufler selinux_socket_getpeersec_stream), 7045e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram), 7046e20b043aSCasey Schaufler LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security), 7047e20b043aSCasey Schaufler LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security), 7048e20b043aSCasey Schaufler LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid), 7049e20b043aSCasey Schaufler LSM_HOOK_INIT(sock_graft, selinux_sock_graft), 7050d452930fSRichard Haines LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request), 7051d452930fSRichard Haines LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone), 7052d452930fSRichard Haines LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect), 7053e20b043aSCasey Schaufler LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request), 7054e20b043aSCasey Schaufler LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone), 7055e20b043aSCasey Schaufler LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established), 7056e20b043aSCasey Schaufler LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet), 7057e20b043aSCasey Schaufler LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc), 7058e20b043aSCasey Schaufler LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec), 7059e20b043aSCasey Schaufler LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow), 7060e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security), 7061e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create), 7062e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue), 7063e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach), 7064e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open), 70653a976fa6SDaniel Jurgens #ifdef CONFIG_SECURITY_INFINIBAND 7066cfc4d882SDaniel Jurgens LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access), 7067ab861dfcSDaniel Jurgens LSM_HOOK_INIT(ib_endport_manage_subnet, 7068ab861dfcSDaniel Jurgens selinux_ib_endport_manage_subnet), 70693a976fa6SDaniel Jurgens LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security), 70703a976fa6SDaniel Jurgens #endif 7071d28d1e08STrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM 7072e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free), 7073e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete), 7074e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free), 7075e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete), 7076e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup), 7077e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_pol_flow_match, 7078e20b043aSCasey Schaufler selinux_xfrm_state_pol_flow_match), 7079e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session), 70801da177e4SLinus Torvalds #endif 7081d720024eSMichael LeMay 7082d720024eSMichael LeMay #ifdef CONFIG_KEYS 7083e20b043aSCasey Schaufler LSM_HOOK_INIT(key_free, selinux_key_free), 7084e20b043aSCasey Schaufler LSM_HOOK_INIT(key_permission, selinux_key_permission), 7085e20b043aSCasey Schaufler LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity), 7086d720024eSMichael LeMay #endif 70879d57a7f9SAhmed S. Darwish 70889d57a7f9SAhmed S. Darwish #ifdef CONFIG_AUDIT 7089e20b043aSCasey Schaufler LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known), 7090e20b043aSCasey Schaufler LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match), 7091e20b043aSCasey Schaufler LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free), 70929d57a7f9SAhmed S. Darwish #endif 7093ec27c356SChenbo Feng 7094ec27c356SChenbo Feng #ifdef CONFIG_BPF_SYSCALL 7095ec27c356SChenbo Feng LSM_HOOK_INIT(bpf, selinux_bpf), 7096ec27c356SChenbo Feng LSM_HOOK_INIT(bpf_map, selinux_bpf_map), 7097ec27c356SChenbo Feng LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog), 7098ec27c356SChenbo Feng LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free), 7099ec27c356SChenbo Feng LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free), 7100ec27c356SChenbo Feng #endif 7101da97e184SJoel Fernandes (Google) 7102da97e184SJoel Fernandes (Google) #ifdef CONFIG_PERF_EVENTS 7103da97e184SJoel Fernandes (Google) LSM_HOOK_INIT(perf_event_open, selinux_perf_event_open), 7104da97e184SJoel Fernandes (Google) LSM_HOOK_INIT(perf_event_free, selinux_perf_event_free), 7105da97e184SJoel Fernandes (Google) LSM_HOOK_INIT(perf_event_read, selinux_perf_event_read), 7106da97e184SJoel Fernandes (Google) LSM_HOOK_INIT(perf_event_write, selinux_perf_event_write), 7107da97e184SJoel Fernandes (Google) #endif 710859438b46SStephen Smalley 710959438b46SStephen Smalley LSM_HOOK_INIT(locked_down, selinux_lockdown), 7110cfff75d8SOndrej Mosnacek 7111cfff75d8SOndrej Mosnacek /* 7112cfff75d8SOndrej Mosnacek * PUT "CLONING" (ACCESSING + ALLOCATING) HOOKS HERE 7113cfff75d8SOndrej Mosnacek */ 7114cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(fs_context_dup, selinux_fs_context_dup), 7115cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(fs_context_parse_param, selinux_fs_context_parse_param), 7116cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts), 7117cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(sb_add_mnt_opt, selinux_add_mnt_opt), 7118cfff75d8SOndrej Mosnacek #ifdef CONFIG_SECURITY_NETWORK_XFRM 7119cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone), 7120cfff75d8SOndrej Mosnacek #endif 7121cfff75d8SOndrej Mosnacek 7122cfff75d8SOndrej Mosnacek /* 7123cfff75d8SOndrej Mosnacek * PUT "ALLOCATING" HOOKS HERE 7124cfff75d8SOndrej Mosnacek */ 7125cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security), 7126cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(msg_queue_alloc_security, 7127cfff75d8SOndrej Mosnacek selinux_msg_queue_alloc_security), 7128cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security), 7129cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security), 7130cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security), 7131cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security), 7132cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx), 7133cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx), 7134cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security), 7135cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security), 7136cfff75d8SOndrej Mosnacek #ifdef CONFIG_SECURITY_INFINIBAND 7137cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security), 7138cfff75d8SOndrej Mosnacek #endif 7139cfff75d8SOndrej Mosnacek #ifdef CONFIG_SECURITY_NETWORK_XFRM 7140cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc), 7141cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc), 7142cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(xfrm_state_alloc_acquire, 7143cfff75d8SOndrej Mosnacek selinux_xfrm_state_alloc_acquire), 7144cfff75d8SOndrej Mosnacek #endif 7145cfff75d8SOndrej Mosnacek #ifdef CONFIG_KEYS 7146cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(key_alloc, selinux_key_alloc), 7147cfff75d8SOndrej Mosnacek #endif 7148cfff75d8SOndrej Mosnacek #ifdef CONFIG_AUDIT 7149cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init), 7150cfff75d8SOndrej Mosnacek #endif 7151cfff75d8SOndrej Mosnacek #ifdef CONFIG_BPF_SYSCALL 7152cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc), 7153cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc), 7154cfff75d8SOndrej Mosnacek #endif 7155cfff75d8SOndrej Mosnacek #ifdef CONFIG_PERF_EVENTS 7156cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(perf_event_alloc, selinux_perf_event_alloc), 7157cfff75d8SOndrej Mosnacek #endif 71581da177e4SLinus Torvalds }; 71591da177e4SLinus Torvalds 71601da177e4SLinus Torvalds static __init int selinux_init(void) 71611da177e4SLinus Torvalds { 7162c103a91eSpeter enderborg pr_info("SELinux: Initializing.\n"); 71631da177e4SLinus Torvalds 7164aa8e712cSStephen Smalley memset(&selinux_state, 0, sizeof(selinux_state)); 7165e5a5ca96SPaul Moore enforcing_set(&selinux_state, selinux_enforcing_boot); 7166aa8e712cSStephen Smalley selinux_state.checkreqprot = selinux_checkreqprot_boot; 7167aa8e712cSStephen Smalley selinux_ss_init(&selinux_state.ss); 71686b6bc620SStephen Smalley selinux_avc_init(&selinux_state.avc); 71694b36cb77SOndrej Mosnacek mutex_init(&selinux_state.status_lock); 7170aa8e712cSStephen Smalley 71711da177e4SLinus Torvalds /* Set the security state for the initial task. */ 7172d84f4f99SDavid Howells cred_init_security(); 71731da177e4SLinus Torvalds 7174fcaaade1SStephen Smalley default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC); 7175fcaaade1SStephen Smalley 71761da177e4SLinus Torvalds avc_init(); 71771da177e4SLinus Torvalds 7178aa8e712cSStephen Smalley avtab_cache_init(); 7179aa8e712cSStephen Smalley 7180aa8e712cSStephen Smalley ebitmap_cache_init(); 7181aa8e712cSStephen Smalley 7182aa8e712cSStephen Smalley hashtab_cache_init(); 7183aa8e712cSStephen Smalley 7184d69dece5SCasey Schaufler security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux"); 71851da177e4SLinus Torvalds 7186615e51fdSPaul Moore if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET)) 7187615e51fdSPaul Moore panic("SELinux: Unable to register AVC netcache callback\n"); 7188615e51fdSPaul Moore 71898f408ab6SDaniel Jurgens if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET)) 71908f408ab6SDaniel Jurgens panic("SELinux: Unable to register AVC LSM notifier callback\n"); 71918f408ab6SDaniel Jurgens 7192aa8e712cSStephen Smalley if (selinux_enforcing_boot) 7193c103a91eSpeter enderborg pr_debug("SELinux: Starting in enforcing mode\n"); 7194828dfe1dSEric Paris else 7195c103a91eSpeter enderborg pr_debug("SELinux: Starting in permissive mode\n"); 7196d720024eSMichael LeMay 7197d7167b14SAl Viro fs_validate_description("selinux", selinux_fs_parameters); 7198442155c1SDavid Howells 71991da177e4SLinus Torvalds return 0; 72001da177e4SLinus Torvalds } 72011da177e4SLinus Torvalds 7202e8c26255SAl Viro static void delayed_superblock_init(struct super_block *sb, void *unused) 7203e8c26255SAl Viro { 7204204cc0ccSAl Viro selinux_set_mnt_opts(sb, NULL, 0, NULL); 7205e8c26255SAl Viro } 7206e8c26255SAl Viro 72071da177e4SLinus Torvalds void selinux_complete_init(void) 72081da177e4SLinus Torvalds { 7209c103a91eSpeter enderborg pr_debug("SELinux: Completing initialization.\n"); 72101da177e4SLinus Torvalds 72111da177e4SLinus Torvalds /* Set up any superblocks initialized prior to the policy load. */ 7212c103a91eSpeter enderborg pr_debug("SELinux: Setting up existing superblocks.\n"); 7213e8c26255SAl Viro iterate_supers(delayed_superblock_init, NULL); 72141da177e4SLinus Torvalds } 72151da177e4SLinus Torvalds 72161da177e4SLinus Torvalds /* SELinux requires early initialization in order to label 72171da177e4SLinus Torvalds all processes and objects when they are created. */ 72183d6e5f6dSKees Cook DEFINE_LSM(selinux) = { 721907aed2f2SKees Cook .name = "selinux", 722014bd99c8SKees Cook .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE, 72216c5a682eSStephen Smalley .enabled = &selinux_enabled_boot, 7222bbd3662aSCasey Schaufler .blobs = &selinux_blob_sizes, 72233d6e5f6dSKees Cook .init = selinux_init, 72243d6e5f6dSKees Cook }; 72251da177e4SLinus Torvalds 7226c2b507fdSStephen Smalley #if defined(CONFIG_NETFILTER) 72271da177e4SLinus Torvalds 7228591bb278SFlorian Westphal static const struct nf_hook_ops selinux_nf_ops[] = { 7229effad8dfSPaul Moore { 7230effad8dfSPaul Moore .hook = selinux_ipv4_postroute, 72312597a834SAlban Crequy .pf = NFPROTO_IPV4, 72326e23ae2aSPatrick McHardy .hooknum = NF_INET_POST_ROUTING, 72331da177e4SLinus Torvalds .priority = NF_IP_PRI_SELINUX_LAST, 7234effad8dfSPaul Moore }, 7235effad8dfSPaul Moore { 7236effad8dfSPaul Moore .hook = selinux_ipv4_forward, 72372597a834SAlban Crequy .pf = NFPROTO_IPV4, 7238effad8dfSPaul Moore .hooknum = NF_INET_FORWARD, 7239effad8dfSPaul Moore .priority = NF_IP_PRI_SELINUX_FIRST, 7240948bf85cSPaul Moore }, 7241948bf85cSPaul Moore { 7242948bf85cSPaul Moore .hook = selinux_ipv4_output, 72432597a834SAlban Crequy .pf = NFPROTO_IPV4, 7244948bf85cSPaul Moore .hooknum = NF_INET_LOCAL_OUT, 7245948bf85cSPaul Moore .priority = NF_IP_PRI_SELINUX_FIRST, 724625db6beaSJiri Pirko }, 72471a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 7248effad8dfSPaul Moore { 7249effad8dfSPaul Moore .hook = selinux_ipv6_postroute, 72502597a834SAlban Crequy .pf = NFPROTO_IPV6, 72516e23ae2aSPatrick McHardy .hooknum = NF_INET_POST_ROUTING, 72521da177e4SLinus Torvalds .priority = NF_IP6_PRI_SELINUX_LAST, 7253effad8dfSPaul Moore }, 7254effad8dfSPaul Moore { 7255effad8dfSPaul Moore .hook = selinux_ipv6_forward, 72562597a834SAlban Crequy .pf = NFPROTO_IPV6, 7257effad8dfSPaul Moore .hooknum = NF_INET_FORWARD, 7258effad8dfSPaul Moore .priority = NF_IP6_PRI_SELINUX_FIRST, 725925db6beaSJiri Pirko }, 72602917f57bSHuw Davies { 72612917f57bSHuw Davies .hook = selinux_ipv6_output, 72622917f57bSHuw Davies .pf = NFPROTO_IPV6, 72632917f57bSHuw Davies .hooknum = NF_INET_LOCAL_OUT, 72642917f57bSHuw Davies .priority = NF_IP6_PRI_SELINUX_FIRST, 72652917f57bSHuw Davies }, 72661da177e4SLinus Torvalds #endif /* IPV6 */ 726725db6beaSJiri Pirko }; 72681da177e4SLinus Torvalds 72698e71bf75SFlorian Westphal static int __net_init selinux_nf_register(struct net *net) 72708e71bf75SFlorian Westphal { 72718e71bf75SFlorian Westphal return nf_register_net_hooks(net, selinux_nf_ops, 72728e71bf75SFlorian Westphal ARRAY_SIZE(selinux_nf_ops)); 72738e71bf75SFlorian Westphal } 72748e71bf75SFlorian Westphal 72758e71bf75SFlorian Westphal static void __net_exit selinux_nf_unregister(struct net *net) 72768e71bf75SFlorian Westphal { 72778e71bf75SFlorian Westphal nf_unregister_net_hooks(net, selinux_nf_ops, 72788e71bf75SFlorian Westphal ARRAY_SIZE(selinux_nf_ops)); 72798e71bf75SFlorian Westphal } 72808e71bf75SFlorian Westphal 72818e71bf75SFlorian Westphal static struct pernet_operations selinux_net_ops = { 72828e71bf75SFlorian Westphal .init = selinux_nf_register, 72838e71bf75SFlorian Westphal .exit = selinux_nf_unregister, 72848e71bf75SFlorian Westphal }; 72858e71bf75SFlorian Westphal 72861da177e4SLinus Torvalds static int __init selinux_nf_ip_init(void) 72871da177e4SLinus Torvalds { 728825db6beaSJiri Pirko int err; 72891da177e4SLinus Torvalds 72906c5a682eSStephen Smalley if (!selinux_enabled_boot) 729125db6beaSJiri Pirko return 0; 72921da177e4SLinus Torvalds 7293c103a91eSpeter enderborg pr_debug("SELinux: Registering netfilter hooks\n"); 72941da177e4SLinus Torvalds 72958e71bf75SFlorian Westphal err = register_pernet_subsys(&selinux_net_ops); 72961da177e4SLinus Torvalds if (err) 72978e71bf75SFlorian Westphal panic("SELinux: register_pernet_subsys: error %d\n", err); 72981da177e4SLinus Torvalds 729925db6beaSJiri Pirko return 0; 73001da177e4SLinus Torvalds } 73011da177e4SLinus Torvalds __initcall(selinux_nf_ip_init); 73021da177e4SLinus Torvalds 73031da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 73041da177e4SLinus Torvalds static void selinux_nf_ip_exit(void) 73051da177e4SLinus Torvalds { 7306c103a91eSpeter enderborg pr_debug("SELinux: Unregistering netfilter hooks\n"); 73071da177e4SLinus Torvalds 73088e71bf75SFlorian Westphal unregister_pernet_subsys(&selinux_net_ops); 73091da177e4SLinus Torvalds } 73101da177e4SLinus Torvalds #endif 73111da177e4SLinus Torvalds 7312c2b507fdSStephen Smalley #else /* CONFIG_NETFILTER */ 73131da177e4SLinus Torvalds 73141da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 73151da177e4SLinus Torvalds #define selinux_nf_ip_exit() 73161da177e4SLinus Torvalds #endif 73171da177e4SLinus Torvalds 7318c2b507fdSStephen Smalley #endif /* CONFIG_NETFILTER */ 73191da177e4SLinus Torvalds 73201da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 7321aa8e712cSStephen Smalley int selinux_disable(struct selinux_state *state) 73221da177e4SLinus Torvalds { 732365cddd50SOndrej Mosnacek if (selinux_initialized(state)) { 73241da177e4SLinus Torvalds /* Not permitted after initial policy load. */ 73251da177e4SLinus Torvalds return -EINVAL; 73261da177e4SLinus Torvalds } 73271da177e4SLinus Torvalds 732865cddd50SOndrej Mosnacek if (selinux_disabled(state)) { 73291da177e4SLinus Torvalds /* Only do this once. */ 73301da177e4SLinus Torvalds return -EINVAL; 73311da177e4SLinus Torvalds } 73321da177e4SLinus Torvalds 733365cddd50SOndrej Mosnacek selinux_mark_disabled(state); 7334aa8e712cSStephen Smalley 7335c103a91eSpeter enderborg pr_info("SELinux: Disabled at runtime.\n"); 73361da177e4SLinus Torvalds 7337cfff75d8SOndrej Mosnacek /* 7338cfff75d8SOndrej Mosnacek * Unregister netfilter hooks. 7339cfff75d8SOndrej Mosnacek * Must be done before security_delete_hooks() to avoid breaking 7340cfff75d8SOndrej Mosnacek * runtime disable. 7341cfff75d8SOndrej Mosnacek */ 7342cfff75d8SOndrej Mosnacek selinux_nf_ip_exit(); 7343cfff75d8SOndrej Mosnacek 7344b1d9e6b0SCasey Schaufler security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks)); 73451da177e4SLinus Torvalds 7346af8ff049SEric Paris /* Try to destroy the avc node cache */ 7347af8ff049SEric Paris avc_disable(); 7348af8ff049SEric Paris 73491da177e4SLinus Torvalds /* Unregister selinuxfs. */ 73501da177e4SLinus Torvalds exit_sel_fs(); 73511da177e4SLinus Torvalds 73521da177e4SLinus Torvalds return 0; 73531da177e4SLinus Torvalds } 73541da177e4SLinus Torvalds #endif 7355