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 145aa8e712cSStephen Smalley if (!kstrtoul(str, 0, &checkreqprot)) 146aa8e712cSStephen Smalley selinux_checkreqprot_boot = checkreqprot ? 1 : 0; 147aa8e712cSStephen Smalley return 1; 148aa8e712cSStephen Smalley } 149aa8e712cSStephen Smalley __setup("checkreqprot=", checkreqprot_setup); 150aa8e712cSStephen Smalley 151d621d35eSPaul Moore /** 152d621d35eSPaul Moore * selinux_secmark_enabled - Check to see if SECMARK is currently enabled 153d621d35eSPaul Moore * 154d621d35eSPaul Moore * Description: 155d621d35eSPaul Moore * This function checks the SECMARK reference counter to see if any SECMARK 156d621d35eSPaul Moore * targets are currently configured, if the reference counter is greater than 157d621d35eSPaul Moore * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is 1582be4d74fSChris PeBenito * enabled, false (0) if SECMARK is disabled. If the always_check_network 1592be4d74fSChris PeBenito * policy capability is enabled, SECMARK is always considered enabled. 160d621d35eSPaul Moore * 161d621d35eSPaul Moore */ 162d621d35eSPaul Moore static int selinux_secmark_enabled(void) 163d621d35eSPaul Moore { 164aa8e712cSStephen Smalley return (selinux_policycap_alwaysnetwork() || 165aa8e712cSStephen Smalley atomic_read(&selinux_secmark_refcount)); 1662be4d74fSChris PeBenito } 1672be4d74fSChris PeBenito 1682be4d74fSChris PeBenito /** 1692be4d74fSChris PeBenito * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled 1702be4d74fSChris PeBenito * 1712be4d74fSChris PeBenito * Description: 1722be4d74fSChris PeBenito * This function checks if NetLabel or labeled IPSEC is enabled. Returns true 1732be4d74fSChris PeBenito * (1) if any are enabled or false (0) if neither are enabled. If the 1742be4d74fSChris PeBenito * always_check_network policy capability is enabled, peer labeling 1752be4d74fSChris PeBenito * is always considered enabled. 1762be4d74fSChris PeBenito * 1772be4d74fSChris PeBenito */ 1782be4d74fSChris PeBenito static int selinux_peerlbl_enabled(void) 1792be4d74fSChris PeBenito { 180aa8e712cSStephen Smalley return (selinux_policycap_alwaysnetwork() || 181aa8e712cSStephen Smalley netlbl_enabled() || selinux_xfrm_enabled()); 182d621d35eSPaul Moore } 183d621d35eSPaul Moore 184615e51fdSPaul Moore static int selinux_netcache_avc_callback(u32 event) 185615e51fdSPaul Moore { 186615e51fdSPaul Moore if (event == AVC_CALLBACK_RESET) { 187615e51fdSPaul Moore sel_netif_flush(); 188615e51fdSPaul Moore sel_netnode_flush(); 189615e51fdSPaul Moore sel_netport_flush(); 190615e51fdSPaul Moore synchronize_net(); 191615e51fdSPaul Moore } 192615e51fdSPaul Moore return 0; 193615e51fdSPaul Moore } 194615e51fdSPaul Moore 1958f408ab6SDaniel Jurgens static int selinux_lsm_notifier_avc_callback(u32 event) 1968f408ab6SDaniel Jurgens { 197409dcf31SDaniel Jurgens if (event == AVC_CALLBACK_RESET) { 198409dcf31SDaniel Jurgens sel_ib_pkey_flush(); 19942df744cSJanne Karhunen call_blocking_lsm_notifier(LSM_POLICY_CHANGE, NULL); 200409dcf31SDaniel Jurgens } 2018f408ab6SDaniel Jurgens 2028f408ab6SDaniel Jurgens return 0; 2038f408ab6SDaniel Jurgens } 2048f408ab6SDaniel Jurgens 205d84f4f99SDavid Howells /* 206d84f4f99SDavid Howells * initialise the security for the init task 207d84f4f99SDavid Howells */ 208d84f4f99SDavid Howells static void cred_init_security(void) 2091da177e4SLinus Torvalds { 2103b11a1deSDavid Howells struct cred *cred = (struct cred *) current->real_cred; 2111da177e4SLinus Torvalds struct task_security_struct *tsec; 2121da177e4SLinus Torvalds 213bbd3662aSCasey Schaufler tsec = selinux_cred(cred); 214d84f4f99SDavid Howells tsec->osid = tsec->sid = SECINITSID_KERNEL; 2151da177e4SLinus Torvalds } 2161da177e4SLinus Torvalds 217275bb41eSDavid Howells /* 21888e67f3bSDavid Howells * get the security ID of a set of credentials 21988e67f3bSDavid Howells */ 22088e67f3bSDavid Howells static inline u32 cred_sid(const struct cred *cred) 22188e67f3bSDavid Howells { 22288e67f3bSDavid Howells const struct task_security_struct *tsec; 22388e67f3bSDavid Howells 2240c6cfa62SCasey Schaufler tsec = selinux_cred(cred); 22588e67f3bSDavid Howells return tsec->sid; 22688e67f3bSDavid Howells } 22788e67f3bSDavid Howells 22888e67f3bSDavid Howells /* 2293b11a1deSDavid Howells * get the objective security ID of a task 230275bb41eSDavid Howells */ 231275bb41eSDavid Howells static inline u32 task_sid(const struct task_struct *task) 232275bb41eSDavid Howells { 233275bb41eSDavid Howells u32 sid; 234275bb41eSDavid Howells 235275bb41eSDavid Howells rcu_read_lock(); 23688e67f3bSDavid Howells sid = cred_sid(__task_cred(task)); 237275bb41eSDavid Howells rcu_read_unlock(); 238275bb41eSDavid Howells return sid; 239275bb41eSDavid Howells } 240275bb41eSDavid Howells 2415d226df4SAndreas Gruenbacher static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry); 2425d226df4SAndreas Gruenbacher 2435d226df4SAndreas Gruenbacher /* 2445d226df4SAndreas Gruenbacher * Try reloading inode security labels that have been marked as invalid. The 2455d226df4SAndreas Gruenbacher * @may_sleep parameter indicates when sleeping and thus reloading labels is 24642059112SAndreas Gruenbacher * allowed; when set to false, returns -ECHILD when the label is 247e9193288SAl Viro * invalid. The @dentry parameter should be set to a dentry of the inode. 2485d226df4SAndreas Gruenbacher */ 2495d226df4SAndreas Gruenbacher static int __inode_security_revalidate(struct inode *inode, 250e9193288SAl Viro struct dentry *dentry, 2515d226df4SAndreas Gruenbacher bool may_sleep) 2525d226df4SAndreas Gruenbacher { 25380788c22SCasey Schaufler struct inode_security_struct *isec = selinux_inode(inode); 2545d226df4SAndreas Gruenbacher 2555d226df4SAndreas Gruenbacher might_sleep_if(may_sleep); 2565d226df4SAndreas Gruenbacher 25765cddd50SOndrej Mosnacek if (selinux_initialized(&selinux_state) && 258aa8e712cSStephen Smalley isec->initialized != LABEL_INITIALIZED) { 2595d226df4SAndreas Gruenbacher if (!may_sleep) 2605d226df4SAndreas Gruenbacher return -ECHILD; 2615d226df4SAndreas Gruenbacher 2625d226df4SAndreas Gruenbacher /* 2635d226df4SAndreas Gruenbacher * Try reloading the inode security label. This will fail if 2645d226df4SAndreas Gruenbacher * @opt_dentry is NULL and no dentry for this inode can be 2655d226df4SAndreas Gruenbacher * found; in that case, continue using the old label. 2665d226df4SAndreas Gruenbacher */ 267e9193288SAl Viro inode_doinit_with_dentry(inode, dentry); 2685d226df4SAndreas Gruenbacher } 2695d226df4SAndreas Gruenbacher return 0; 2705d226df4SAndreas Gruenbacher } 2715d226df4SAndreas Gruenbacher 2725d226df4SAndreas Gruenbacher static struct inode_security_struct *inode_security_novalidate(struct inode *inode) 2735d226df4SAndreas Gruenbacher { 27480788c22SCasey Schaufler return selinux_inode(inode); 2755d226df4SAndreas Gruenbacher } 2765d226df4SAndreas Gruenbacher 2775d226df4SAndreas Gruenbacher static struct inode_security_struct *inode_security_rcu(struct inode *inode, bool rcu) 2785d226df4SAndreas Gruenbacher { 2795d226df4SAndreas Gruenbacher int error; 2805d226df4SAndreas Gruenbacher 2815d226df4SAndreas Gruenbacher error = __inode_security_revalidate(inode, NULL, !rcu); 2825d226df4SAndreas Gruenbacher if (error) 2835d226df4SAndreas Gruenbacher return ERR_PTR(error); 28480788c22SCasey Schaufler return selinux_inode(inode); 2855d226df4SAndreas Gruenbacher } 2865d226df4SAndreas Gruenbacher 28783da53c5SAndreas Gruenbacher /* 28883da53c5SAndreas Gruenbacher * Get the security label of an inode. 28983da53c5SAndreas Gruenbacher */ 29083da53c5SAndreas Gruenbacher static struct inode_security_struct *inode_security(struct inode *inode) 29183da53c5SAndreas Gruenbacher { 2925d226df4SAndreas Gruenbacher __inode_security_revalidate(inode, NULL, true); 29380788c22SCasey Schaufler return selinux_inode(inode); 29483da53c5SAndreas Gruenbacher } 29583da53c5SAndreas Gruenbacher 2962c97165bSPaul Moore static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry) 2972c97165bSPaul Moore { 2982c97165bSPaul Moore struct inode *inode = d_backing_inode(dentry); 2992c97165bSPaul Moore 30080788c22SCasey Schaufler return selinux_inode(inode); 3012c97165bSPaul Moore } 3022c97165bSPaul Moore 30383da53c5SAndreas Gruenbacher /* 30483da53c5SAndreas Gruenbacher * Get the security label of a dentry's backing inode. 30583da53c5SAndreas Gruenbacher */ 30683da53c5SAndreas Gruenbacher static struct inode_security_struct *backing_inode_security(struct dentry *dentry) 30783da53c5SAndreas Gruenbacher { 30883da53c5SAndreas Gruenbacher struct inode *inode = d_backing_inode(dentry); 30983da53c5SAndreas Gruenbacher 3105d226df4SAndreas Gruenbacher __inode_security_revalidate(inode, dentry, true); 31180788c22SCasey Schaufler return selinux_inode(inode); 31283da53c5SAndreas Gruenbacher } 31383da53c5SAndreas Gruenbacher 3141da177e4SLinus Torvalds static void inode_free_security(struct inode *inode) 3151da177e4SLinus Torvalds { 31680788c22SCasey Schaufler struct inode_security_struct *isec = selinux_inode(inode); 317afb1cbe3SCasey Schaufler struct superblock_security_struct *sbsec; 3181da177e4SLinus Torvalds 319afb1cbe3SCasey Schaufler if (!isec) 320afb1cbe3SCasey Schaufler return; 321afb1cbe3SCasey Schaufler sbsec = inode->i_sb->s_security; 3229629d04aSWaiman Long /* 3239629d04aSWaiman Long * As not all inode security structures are in a list, we check for 3249629d04aSWaiman Long * empty list outside of the lock to make sure that we won't waste 3259629d04aSWaiman Long * time taking a lock doing nothing. 3269629d04aSWaiman Long * 3279629d04aSWaiman Long * The list_del_init() function can be safely called more than once. 3289629d04aSWaiman Long * It should not be possible for this function to be called with 3299629d04aSWaiman Long * concurrent list_add(), but for better safety against future changes 3309629d04aSWaiman Long * in the code, we use list_empty_careful() here. 3319629d04aSWaiman Long */ 3329629d04aSWaiman Long if (!list_empty_careful(&isec->list)) { 3331da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 3341da177e4SLinus Torvalds list_del_init(&isec->list); 3351da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 3369629d04aSWaiman Long } 3371da177e4SLinus Torvalds } 3381da177e4SLinus Torvalds 3391da177e4SLinus Torvalds static void superblock_free_security(struct super_block *sb) 3401da177e4SLinus Torvalds { 3411da177e4SLinus Torvalds struct superblock_security_struct *sbsec = sb->s_security; 3421da177e4SLinus Torvalds sb->s_security = NULL; 3431da177e4SLinus Torvalds kfree(sbsec); 3441da177e4SLinus Torvalds } 3451da177e4SLinus Torvalds 346bd323655SAl Viro struct selinux_mnt_opts { 347bd323655SAl Viro const char *fscontext, *context, *rootcontext, *defcontext; 348bd323655SAl Viro }; 349bd323655SAl Viro 350204cc0ccSAl Viro static void selinux_free_mnt_opts(void *mnt_opts) 351204cc0ccSAl Viro { 352bd323655SAl Viro struct selinux_mnt_opts *opts = mnt_opts; 353bd323655SAl Viro kfree(opts->fscontext); 354bd323655SAl Viro kfree(opts->context); 355bd323655SAl Viro kfree(opts->rootcontext); 356bd323655SAl Viro kfree(opts->defcontext); 357204cc0ccSAl Viro kfree(opts); 358204cc0ccSAl Viro } 359204cc0ccSAl Viro 3601da177e4SLinus Torvalds enum { 36131e87930SEric Paris Opt_error = -1, 362442155c1SDavid Howells Opt_context = 0, 363442155c1SDavid Howells Opt_defcontext = 1, 3641da177e4SLinus Torvalds Opt_fscontext = 2, 365442155c1SDavid Howells Opt_rootcontext = 3, 366442155c1SDavid Howells Opt_seclabel = 4, 3671da177e4SLinus Torvalds }; 3681da177e4SLinus Torvalds 369da3d76abSAl Viro #define A(s, has_arg) {#s, sizeof(#s) - 1, Opt_##s, has_arg} 370169d68efSAl Viro static struct { 371169d68efSAl Viro const char *name; 372169d68efSAl Viro int len; 373169d68efSAl Viro int opt; 374169d68efSAl Viro bool has_arg; 375169d68efSAl Viro } tokens[] = { 376da3d76abSAl Viro A(context, true), 377da3d76abSAl Viro A(fscontext, true), 378da3d76abSAl Viro A(defcontext, true), 379da3d76abSAl Viro A(rootcontext, true), 380da3d76abSAl Viro A(seclabel, false), 3811da177e4SLinus Torvalds }; 382169d68efSAl Viro #undef A 383169d68efSAl Viro 384169d68efSAl Viro static int match_opt_prefix(char *s, int l, char **arg) 385169d68efSAl Viro { 386169d68efSAl Viro int i; 387169d68efSAl Viro 388169d68efSAl Viro for (i = 0; i < ARRAY_SIZE(tokens); i++) { 389169d68efSAl Viro size_t len = tokens[i].len; 390169d68efSAl Viro if (len > l || memcmp(s, tokens[i].name, len)) 391169d68efSAl Viro continue; 392169d68efSAl Viro if (tokens[i].has_arg) { 393169d68efSAl Viro if (len == l || s[len] != '=') 394169d68efSAl Viro continue; 395169d68efSAl Viro *arg = s + len + 1; 396169d68efSAl Viro } else if (len != l) 397169d68efSAl Viro continue; 398169d68efSAl Viro return tokens[i].opt; 399169d68efSAl Viro } 400169d68efSAl Viro return Opt_error; 401169d68efSAl Viro } 4021da177e4SLinus Torvalds 4031da177e4SLinus Torvalds #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n" 4041da177e4SLinus Torvalds 405c312feb2SEric Paris static int may_context_mount_sb_relabel(u32 sid, 406c312feb2SEric Paris struct superblock_security_struct *sbsec, 407275bb41eSDavid Howells const struct cred *cred) 408c312feb2SEric Paris { 4090c6cfa62SCasey Schaufler const struct task_security_struct *tsec = selinux_cred(cred); 410c312feb2SEric Paris int rc; 411c312feb2SEric Paris 4126b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 4136b6bc620SStephen Smalley tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 414c312feb2SEric Paris FILESYSTEM__RELABELFROM, NULL); 415c312feb2SEric Paris if (rc) 416c312feb2SEric Paris return rc; 417c312feb2SEric Paris 4186b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 4196b6bc620SStephen Smalley tsec->sid, sid, SECCLASS_FILESYSTEM, 420c312feb2SEric Paris FILESYSTEM__RELABELTO, NULL); 421c312feb2SEric Paris return rc; 422c312feb2SEric Paris } 423c312feb2SEric Paris 4240808925eSEric Paris static int may_context_mount_inode_relabel(u32 sid, 4250808925eSEric Paris struct superblock_security_struct *sbsec, 426275bb41eSDavid Howells const struct cred *cred) 4270808925eSEric Paris { 4280c6cfa62SCasey Schaufler const struct task_security_struct *tsec = selinux_cred(cred); 4290808925eSEric Paris int rc; 4306b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 4316b6bc620SStephen Smalley tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 4320808925eSEric Paris FILESYSTEM__RELABELFROM, NULL); 4330808925eSEric Paris if (rc) 4340808925eSEric Paris return rc; 4350808925eSEric Paris 4366b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 4376b6bc620SStephen Smalley sid, sbsec->sid, SECCLASS_FILESYSTEM, 4380808925eSEric Paris FILESYSTEM__ASSOCIATE, NULL); 4390808925eSEric Paris return rc; 4400808925eSEric Paris } 4410808925eSEric Paris 442a83d6ddaSOndrej Mosnacek static int selinux_is_genfs_special_handling(struct super_block *sb) 443b43e725dSEric Paris { 444d5f3a5f6SMark Salyzyn /* Special handling. Genfs but also in-core setxattr handler */ 445a83d6ddaSOndrej Mosnacek return !strcmp(sb->s_type->name, "sysfs") || 446d5f3a5f6SMark Salyzyn !strcmp(sb->s_type->name, "pstore") || 447d5f3a5f6SMark Salyzyn !strcmp(sb->s_type->name, "debugfs") || 448a2c7c6fbSYongqin Liu !strcmp(sb->s_type->name, "tracefs") || 4492651225bSStephen Smalley !strcmp(sb->s_type->name, "rootfs") || 450aa8e712cSStephen Smalley (selinux_policycap_cgroupseclabel() && 4512651225bSStephen Smalley (!strcmp(sb->s_type->name, "cgroup") || 4522651225bSStephen Smalley !strcmp(sb->s_type->name, "cgroup2"))); 453b43e725dSEric Paris } 454b43e725dSEric Paris 455a83d6ddaSOndrej Mosnacek static int selinux_is_sblabel_mnt(struct super_block *sb) 456a83d6ddaSOndrej Mosnacek { 457a83d6ddaSOndrej Mosnacek struct superblock_security_struct *sbsec = sb->s_security; 458a83d6ddaSOndrej Mosnacek 459a83d6ddaSOndrej Mosnacek /* 460a83d6ddaSOndrej Mosnacek * IMPORTANT: Double-check logic in this function when adding a new 461a83d6ddaSOndrej Mosnacek * SECURITY_FS_USE_* definition! 462a83d6ddaSOndrej Mosnacek */ 463a83d6ddaSOndrej Mosnacek BUILD_BUG_ON(SECURITY_FS_USE_MAX != 7); 464a83d6ddaSOndrej Mosnacek 465a83d6ddaSOndrej Mosnacek switch (sbsec->behavior) { 466a83d6ddaSOndrej Mosnacek case SECURITY_FS_USE_XATTR: 467a83d6ddaSOndrej Mosnacek case SECURITY_FS_USE_TRANS: 468a83d6ddaSOndrej Mosnacek case SECURITY_FS_USE_TASK: 469a83d6ddaSOndrej Mosnacek case SECURITY_FS_USE_NATIVE: 470a83d6ddaSOndrej Mosnacek return 1; 471a83d6ddaSOndrej Mosnacek 472a83d6ddaSOndrej Mosnacek case SECURITY_FS_USE_GENFS: 473a83d6ddaSOndrej Mosnacek return selinux_is_genfs_special_handling(sb); 474a83d6ddaSOndrej Mosnacek 475a83d6ddaSOndrej Mosnacek /* Never allow relabeling on context mounts */ 476a83d6ddaSOndrej Mosnacek case SECURITY_FS_USE_MNTPOINT: 477a83d6ddaSOndrej Mosnacek case SECURITY_FS_USE_NONE: 478a83d6ddaSOndrej Mosnacek default: 479a83d6ddaSOndrej Mosnacek return 0; 480a83d6ddaSOndrej Mosnacek } 481a83d6ddaSOndrej Mosnacek } 482a83d6ddaSOndrej Mosnacek 483c9180a57SEric Paris static int sb_finish_set_opts(struct super_block *sb) 4841da177e4SLinus Torvalds { 4851da177e4SLinus Torvalds struct superblock_security_struct *sbsec = sb->s_security; 4861da177e4SLinus Torvalds struct dentry *root = sb->s_root; 487c6f493d6SDavid Howells struct inode *root_inode = d_backing_inode(root); 4881da177e4SLinus Torvalds int rc = 0; 4891da177e4SLinus Torvalds 4901da177e4SLinus Torvalds if (sbsec->behavior == SECURITY_FS_USE_XATTR) { 4911da177e4SLinus Torvalds /* Make sure that the xattr handler exists and that no 4921da177e4SLinus Torvalds error other than -ENODATA is returned by getxattr on 4931da177e4SLinus Torvalds the root directory. -ENODATA is ok, as this may be 4941da177e4SLinus Torvalds the first boot of the SELinux kernel before we have 4951da177e4SLinus Torvalds assigned xattr values to the filesystem. */ 4965d6c3191SAndreas Gruenbacher if (!(root_inode->i_opflags & IOP_XATTR)) { 497c103a91eSpeter enderborg pr_warn("SELinux: (dev %s, type %s) has no " 49829b1deb2SLinus Torvalds "xattr support\n", sb->s_id, sb->s_type->name); 4991da177e4SLinus Torvalds rc = -EOPNOTSUPP; 5001da177e4SLinus Torvalds goto out; 5011da177e4SLinus Torvalds } 5025d6c3191SAndreas Gruenbacher 5035d6c3191SAndreas Gruenbacher rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0); 5041da177e4SLinus Torvalds if (rc < 0 && rc != -ENODATA) { 5051da177e4SLinus Torvalds if (rc == -EOPNOTSUPP) 506c103a91eSpeter enderborg pr_warn("SELinux: (dev %s, type " 50729b1deb2SLinus Torvalds "%s) has no security xattr handler\n", 50829b1deb2SLinus Torvalds sb->s_id, sb->s_type->name); 5091da177e4SLinus Torvalds else 510c103a91eSpeter enderborg pr_warn("SELinux: (dev %s, type " 51129b1deb2SLinus Torvalds "%s) getxattr errno %d\n", sb->s_id, 51229b1deb2SLinus Torvalds sb->s_type->name, -rc); 5131da177e4SLinus Torvalds goto out; 5141da177e4SLinus Torvalds } 5151da177e4SLinus Torvalds } 5161da177e4SLinus Torvalds 517eadcabc6SEric Paris sbsec->flags |= SE_SBINITIALIZED; 5180b4d3452SScott Mayhew 5190b4d3452SScott Mayhew /* 5200b4d3452SScott Mayhew * Explicitly set or clear SBLABEL_MNT. It's not sufficient to simply 5210b4d3452SScott Mayhew * leave the flag untouched because sb_clone_mnt_opts might be handing 5220b4d3452SScott Mayhew * us a superblock that needs the flag to be cleared. 5230b4d3452SScott Mayhew */ 524b43e725dSEric Paris if (selinux_is_sblabel_mnt(sb)) 52512f348b9SEric Paris sbsec->flags |= SBLABEL_MNT; 5260b4d3452SScott Mayhew else 5270b4d3452SScott Mayhew sbsec->flags &= ~SBLABEL_MNT; 528ddd29ec6SDavid P. Quigley 5291da177e4SLinus Torvalds /* Initialize the root inode. */ 530c9180a57SEric Paris rc = inode_doinit_with_dentry(root_inode, root); 5311da177e4SLinus Torvalds 5321da177e4SLinus Torvalds /* Initialize any other inodes associated with the superblock, e.g. 5331da177e4SLinus Torvalds inodes created prior to initial policy load or inodes created 5341da177e4SLinus Torvalds during get_sb by a pseudo filesystem that directly 5351da177e4SLinus Torvalds populates itself. */ 5361da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 5378d64124aSAl Viro while (!list_empty(&sbsec->isec_head)) { 5381da177e4SLinus Torvalds struct inode_security_struct *isec = 5398d64124aSAl Viro list_first_entry(&sbsec->isec_head, 5401da177e4SLinus Torvalds struct inode_security_struct, list); 5411da177e4SLinus Torvalds struct inode *inode = isec->inode; 542923190d3SStephen Smalley list_del_init(&isec->list); 5431da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 5441da177e4SLinus Torvalds inode = igrab(inode); 5451da177e4SLinus Torvalds if (inode) { 5461da177e4SLinus Torvalds if (!IS_PRIVATE(inode)) 547cb89e246SPaul Moore inode_doinit_with_dentry(inode, NULL); 5481da177e4SLinus Torvalds iput(inode); 5491da177e4SLinus Torvalds } 5501da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 5511da177e4SLinus Torvalds } 5521da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 5531da177e4SLinus Torvalds out: 554c9180a57SEric Paris return rc; 555c9180a57SEric Paris } 556c9180a57SEric Paris 557c9180a57SEric Paris static int bad_option(struct superblock_security_struct *sbsec, char flag, 558c9180a57SEric Paris u32 old_sid, u32 new_sid) 559c9180a57SEric Paris { 5600d90a7ecSDavid P. Quigley char mnt_flags = sbsec->flags & SE_MNTMASK; 5610d90a7ecSDavid P. Quigley 562c9180a57SEric Paris /* check if the old mount command had the same options */ 5630d90a7ecSDavid P. Quigley if (sbsec->flags & SE_SBINITIALIZED) 564c9180a57SEric Paris if (!(sbsec->flags & flag) || 565c9180a57SEric Paris (old_sid != new_sid)) 566c9180a57SEric Paris return 1; 567c9180a57SEric Paris 568c9180a57SEric Paris /* check if we were passed the same options twice, 569c9180a57SEric Paris * aka someone passed context=a,context=b 570c9180a57SEric Paris */ 5710d90a7ecSDavid P. Quigley if (!(sbsec->flags & SE_SBINITIALIZED)) 5720d90a7ecSDavid P. Quigley if (mnt_flags & flag) 573c9180a57SEric Paris return 1; 574c9180a57SEric Paris return 0; 575c9180a57SEric Paris } 576e0007529SEric Paris 577bd323655SAl Viro static int parse_sid(struct super_block *sb, const char *s, u32 *sid) 578bd323655SAl Viro { 579bd323655SAl Viro int rc = security_context_str_to_sid(&selinux_state, s, 580bd323655SAl Viro sid, GFP_KERNEL); 581bd323655SAl Viro if (rc) 582bd323655SAl Viro pr_warn("SELinux: security_context_str_to_sid" 583bd323655SAl Viro "(%s) failed for (dev %s, type %s) errno=%d\n", 584bd323655SAl Viro s, sb->s_id, sb->s_type->name, rc); 585bd323655SAl Viro return rc; 586bd323655SAl Viro } 587bd323655SAl Viro 588c9180a57SEric Paris /* 589c9180a57SEric Paris * Allow filesystems with binary mount data to explicitly set mount point 590c9180a57SEric Paris * labeling information. 591c9180a57SEric Paris */ 592e0007529SEric Paris static int selinux_set_mnt_opts(struct super_block *sb, 593204cc0ccSAl Viro void *mnt_opts, 594649f6e77SDavid Quigley unsigned long kern_flags, 595649f6e77SDavid Quigley unsigned long *set_kern_flags) 596c9180a57SEric Paris { 597275bb41eSDavid Howells const struct cred *cred = current_cred(); 598c9180a57SEric Paris struct superblock_security_struct *sbsec = sb->s_security; 59983da53c5SAndreas Gruenbacher struct dentry *root = sbsec->sb->s_root; 600bd323655SAl Viro struct selinux_mnt_opts *opts = mnt_opts; 6012c97165bSPaul Moore struct inode_security_struct *root_isec; 602c9180a57SEric Paris u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0; 603c9180a57SEric Paris u32 defcontext_sid = 0; 604bd323655SAl Viro int rc = 0; 605c9180a57SEric Paris 606c9180a57SEric Paris mutex_lock(&sbsec->lock); 607c9180a57SEric Paris 60865cddd50SOndrej Mosnacek if (!selinux_initialized(&selinux_state)) { 609bd323655SAl Viro if (!opts) { 610c9180a57SEric Paris /* Defer initialization until selinux_complete_init, 611c9180a57SEric Paris after the initial policy is loaded and the security 612c9180a57SEric Paris server is ready to handle calls. */ 613c9180a57SEric Paris goto out; 614c9180a57SEric Paris } 615c9180a57SEric Paris rc = -EINVAL; 616c103a91eSpeter enderborg pr_warn("SELinux: Unable to set superblock options " 617744ba35eSEric Paris "before the security server is initialized\n"); 618c9180a57SEric Paris goto out; 619c9180a57SEric Paris } 620649f6e77SDavid Quigley if (kern_flags && !set_kern_flags) { 621649f6e77SDavid Quigley /* Specifying internal flags without providing a place to 622649f6e77SDavid Quigley * place the results is not allowed */ 623649f6e77SDavid Quigley rc = -EINVAL; 624649f6e77SDavid Quigley goto out; 625649f6e77SDavid Quigley } 626c9180a57SEric Paris 627c9180a57SEric Paris /* 628e0007529SEric Paris * Binary mount data FS will come through this function twice. Once 629e0007529SEric Paris * from an explicit call and once from the generic calls from the vfs. 630e0007529SEric Paris * Since the generic VFS calls will not contain any security mount data 631e0007529SEric Paris * we need to skip the double mount verification. 632e0007529SEric Paris * 633e0007529SEric Paris * This does open a hole in which we will not notice if the first 634e0007529SEric Paris * mount using this sb set explict options and a second mount using 635e0007529SEric Paris * this sb does not set any security options. (The first options 636e0007529SEric Paris * will be used for both mounts) 637e0007529SEric Paris */ 6380d90a7ecSDavid P. Quigley if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA) 639bd323655SAl Viro && !opts) 640e0007529SEric Paris goto out; 641e0007529SEric Paris 6422c97165bSPaul Moore root_isec = backing_inode_security_novalidate(root); 6432c97165bSPaul Moore 644e0007529SEric Paris /* 645c9180a57SEric Paris * parse the mount options, check if they are valid sids. 646c9180a57SEric Paris * also check if someone is trying to mount the same sb more 647c9180a57SEric Paris * than once with different security options. 648c9180a57SEric Paris */ 649bd323655SAl Viro if (opts) { 650bd323655SAl Viro if (opts->fscontext) { 651bd323655SAl Viro rc = parse_sid(sb, opts->fscontext, &fscontext_sid); 652bd323655SAl Viro if (rc) 653c9180a57SEric Paris goto out; 654c9180a57SEric Paris if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, 655c9180a57SEric Paris fscontext_sid)) 656c9180a57SEric Paris goto out_double_mount; 657c9180a57SEric Paris sbsec->flags |= FSCONTEXT_MNT; 658bd323655SAl Viro } 659bd323655SAl Viro if (opts->context) { 660bd323655SAl Viro rc = parse_sid(sb, opts->context, &context_sid); 661bd323655SAl Viro if (rc) 662bd323655SAl Viro goto out; 663c9180a57SEric Paris if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, 664c9180a57SEric Paris context_sid)) 665c9180a57SEric Paris goto out_double_mount; 666c9180a57SEric Paris sbsec->flags |= CONTEXT_MNT; 667bd323655SAl Viro } 668bd323655SAl Viro if (opts->rootcontext) { 669bd323655SAl Viro rc = parse_sid(sb, opts->rootcontext, &rootcontext_sid); 670bd323655SAl Viro if (rc) 671bd323655SAl Viro goto out; 672c9180a57SEric Paris if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, 673c9180a57SEric Paris rootcontext_sid)) 674c9180a57SEric Paris goto out_double_mount; 675c9180a57SEric Paris sbsec->flags |= ROOTCONTEXT_MNT; 676bd323655SAl Viro } 677bd323655SAl Viro if (opts->defcontext) { 678bd323655SAl Viro rc = parse_sid(sb, opts->defcontext, &defcontext_sid); 679bd323655SAl Viro if (rc) 680bd323655SAl Viro goto out; 681c9180a57SEric Paris if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, 682c9180a57SEric Paris defcontext_sid)) 683c9180a57SEric Paris goto out_double_mount; 684c9180a57SEric Paris sbsec->flags |= DEFCONTEXT_MNT; 685c9180a57SEric Paris } 686c9180a57SEric Paris } 687c9180a57SEric Paris 6880d90a7ecSDavid P. Quigley if (sbsec->flags & SE_SBINITIALIZED) { 689c9180a57SEric Paris /* previously mounted with options, but not on this attempt? */ 690bd323655SAl Viro if ((sbsec->flags & SE_MNTMASK) && !opts) 691c9180a57SEric Paris goto out_double_mount; 692c9180a57SEric Paris rc = 0; 693c9180a57SEric Paris goto out; 694c9180a57SEric Paris } 695c9180a57SEric Paris 696089be43eSJames Morris if (strcmp(sb->s_type->name, "proc") == 0) 697134509d5SStephen Smalley sbsec->flags |= SE_SBPROC | SE_SBGENFS; 698134509d5SStephen Smalley 6998e014720SStephen Smalley if (!strcmp(sb->s_type->name, "debugfs") || 7006a391183SJeff Vander Stoep !strcmp(sb->s_type->name, "tracefs") || 7017a4b5194SHridya Valsaraju !strcmp(sb->s_type->name, "binderfs") || 702b754026bSOndrej Mosnacek !strcmp(sb->s_type->name, "pstore")) 703b754026bSOndrej Mosnacek sbsec->flags |= SE_SBGENFS; 704b754026bSOndrej Mosnacek 705b754026bSOndrej Mosnacek if (!strcmp(sb->s_type->name, "sysfs") || 706901ef845SAntonio Murdaca !strcmp(sb->s_type->name, "cgroup") || 707901ef845SAntonio Murdaca !strcmp(sb->s_type->name, "cgroup2")) 708b754026bSOndrej Mosnacek sbsec->flags |= SE_SBGENFS | SE_SBGENFS_XATTR; 709c9180a57SEric Paris 710eb9ae686SDavid Quigley if (!sbsec->behavior) { 711eb9ae686SDavid Quigley /* 712eb9ae686SDavid Quigley * Determine the labeling behavior to use for this 713eb9ae686SDavid Quigley * filesystem type. 714eb9ae686SDavid Quigley */ 715aa8e712cSStephen Smalley rc = security_fs_use(&selinux_state, sb); 716c9180a57SEric Paris if (rc) { 717c103a91eSpeter enderborg pr_warn("%s: security_fs_use(%s) returned %d\n", 718089be43eSJames Morris __func__, sb->s_type->name, rc); 719c9180a57SEric Paris goto out; 720c9180a57SEric Paris } 721eb9ae686SDavid Quigley } 722aad82892SSeth Forshee 723aad82892SSeth Forshee /* 72401593d32SStephen Smalley * If this is a user namespace mount and the filesystem type is not 72501593d32SStephen Smalley * explicitly whitelisted, then no contexts are allowed on the command 72601593d32SStephen Smalley * line and security labels must be ignored. 727aad82892SSeth Forshee */ 72801593d32SStephen Smalley if (sb->s_user_ns != &init_user_ns && 72901593d32SStephen Smalley strcmp(sb->s_type->name, "tmpfs") && 73001593d32SStephen Smalley strcmp(sb->s_type->name, "ramfs") && 73101593d32SStephen Smalley strcmp(sb->s_type->name, "devpts")) { 732aad82892SSeth Forshee if (context_sid || fscontext_sid || rootcontext_sid || 733aad82892SSeth Forshee defcontext_sid) { 734aad82892SSeth Forshee rc = -EACCES; 735aad82892SSeth Forshee goto out; 736aad82892SSeth Forshee } 737aad82892SSeth Forshee if (sbsec->behavior == SECURITY_FS_USE_XATTR) { 738aad82892SSeth Forshee sbsec->behavior = SECURITY_FS_USE_MNTPOINT; 739aa8e712cSStephen Smalley rc = security_transition_sid(&selinux_state, 740aa8e712cSStephen Smalley current_sid(), 741aa8e712cSStephen Smalley current_sid(), 742aad82892SSeth Forshee SECCLASS_FILE, NULL, 743aad82892SSeth Forshee &sbsec->mntpoint_sid); 744aad82892SSeth Forshee if (rc) 745aad82892SSeth Forshee goto out; 746aad82892SSeth Forshee } 747aad82892SSeth Forshee goto out_set_opts; 748aad82892SSeth Forshee } 749aad82892SSeth Forshee 750c9180a57SEric Paris /* sets the context of the superblock for the fs being mounted. */ 751c9180a57SEric Paris if (fscontext_sid) { 752275bb41eSDavid Howells rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred); 753c9180a57SEric Paris if (rc) 754c9180a57SEric Paris goto out; 755c9180a57SEric Paris 756c9180a57SEric Paris sbsec->sid = fscontext_sid; 757c9180a57SEric Paris } 758c9180a57SEric Paris 759c9180a57SEric Paris /* 760c9180a57SEric Paris * Switch to using mount point labeling behavior. 761c9180a57SEric Paris * sets the label used on all file below the mountpoint, and will set 762c9180a57SEric Paris * the superblock context if not already set. 763c9180a57SEric Paris */ 764eb9ae686SDavid Quigley if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) { 765eb9ae686SDavid Quigley sbsec->behavior = SECURITY_FS_USE_NATIVE; 766eb9ae686SDavid Quigley *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS; 767eb9ae686SDavid Quigley } 768eb9ae686SDavid Quigley 769c9180a57SEric Paris if (context_sid) { 770c9180a57SEric Paris if (!fscontext_sid) { 771275bb41eSDavid Howells rc = may_context_mount_sb_relabel(context_sid, sbsec, 772275bb41eSDavid Howells cred); 773c9180a57SEric Paris if (rc) 774c9180a57SEric Paris goto out; 775c9180a57SEric Paris sbsec->sid = context_sid; 776c9180a57SEric Paris } else { 777275bb41eSDavid Howells rc = may_context_mount_inode_relabel(context_sid, sbsec, 778275bb41eSDavid Howells cred); 779c9180a57SEric Paris if (rc) 780c9180a57SEric Paris goto out; 781c9180a57SEric Paris } 782c9180a57SEric Paris if (!rootcontext_sid) 783c9180a57SEric Paris rootcontext_sid = context_sid; 784c9180a57SEric Paris 785c9180a57SEric Paris sbsec->mntpoint_sid = context_sid; 786c9180a57SEric Paris sbsec->behavior = SECURITY_FS_USE_MNTPOINT; 787c9180a57SEric Paris } 788c9180a57SEric Paris 789c9180a57SEric Paris if (rootcontext_sid) { 790275bb41eSDavid Howells rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec, 791275bb41eSDavid Howells cred); 792c9180a57SEric Paris if (rc) 793c9180a57SEric Paris goto out; 794c9180a57SEric Paris 795c9180a57SEric Paris root_isec->sid = rootcontext_sid; 7966f3be9f5SAndreas Gruenbacher root_isec->initialized = LABEL_INITIALIZED; 797c9180a57SEric Paris } 798c9180a57SEric Paris 799c9180a57SEric Paris if (defcontext_sid) { 800eb9ae686SDavid Quigley if (sbsec->behavior != SECURITY_FS_USE_XATTR && 801eb9ae686SDavid Quigley sbsec->behavior != SECURITY_FS_USE_NATIVE) { 802c9180a57SEric Paris rc = -EINVAL; 803c103a91eSpeter enderborg pr_warn("SELinux: defcontext option is " 804c9180a57SEric Paris "invalid for this filesystem type\n"); 805c9180a57SEric Paris goto out; 806c9180a57SEric Paris } 807c9180a57SEric Paris 808c9180a57SEric Paris if (defcontext_sid != sbsec->def_sid) { 809c9180a57SEric Paris rc = may_context_mount_inode_relabel(defcontext_sid, 810275bb41eSDavid Howells sbsec, cred); 811c9180a57SEric Paris if (rc) 812c9180a57SEric Paris goto out; 813c9180a57SEric Paris } 814c9180a57SEric Paris 815c9180a57SEric Paris sbsec->def_sid = defcontext_sid; 816c9180a57SEric Paris } 817c9180a57SEric Paris 818aad82892SSeth Forshee out_set_opts: 819c9180a57SEric Paris rc = sb_finish_set_opts(sb); 820c9180a57SEric Paris out: 821bc7e982bSEric Paris mutex_unlock(&sbsec->lock); 8221da177e4SLinus Torvalds return rc; 823c9180a57SEric Paris out_double_mount: 824c9180a57SEric Paris rc = -EINVAL; 825c103a91eSpeter enderborg pr_warn("SELinux: mount invalid. Same superblock, different " 826bd323655SAl Viro "security settings for (dev %s, type %s)\n", sb->s_id, 827bd323655SAl Viro sb->s_type->name); 828c9180a57SEric Paris goto out; 829c9180a57SEric Paris } 830c9180a57SEric Paris 831094f7b69SJeff Layton static int selinux_cmp_sb_context(const struct super_block *oldsb, 832094f7b69SJeff Layton const struct super_block *newsb) 833094f7b69SJeff Layton { 834094f7b69SJeff Layton struct superblock_security_struct *old = oldsb->s_security; 835094f7b69SJeff Layton struct superblock_security_struct *new = newsb->s_security; 836094f7b69SJeff Layton char oldflags = old->flags & SE_MNTMASK; 837094f7b69SJeff Layton char newflags = new->flags & SE_MNTMASK; 838094f7b69SJeff Layton 839094f7b69SJeff Layton if (oldflags != newflags) 840094f7b69SJeff Layton goto mismatch; 841094f7b69SJeff Layton if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid) 842094f7b69SJeff Layton goto mismatch; 843094f7b69SJeff Layton if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid) 844094f7b69SJeff Layton goto mismatch; 845094f7b69SJeff Layton if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid) 846094f7b69SJeff Layton goto mismatch; 847094f7b69SJeff Layton if (oldflags & ROOTCONTEXT_MNT) { 84883da53c5SAndreas Gruenbacher struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root); 84983da53c5SAndreas Gruenbacher struct inode_security_struct *newroot = backing_inode_security(newsb->s_root); 850094f7b69SJeff Layton if (oldroot->sid != newroot->sid) 851094f7b69SJeff Layton goto mismatch; 852094f7b69SJeff Layton } 853094f7b69SJeff Layton return 0; 854094f7b69SJeff Layton mismatch: 855c103a91eSpeter enderborg pr_warn("SELinux: mount invalid. Same superblock, " 856094f7b69SJeff Layton "different security settings for (dev %s, " 857094f7b69SJeff Layton "type %s)\n", newsb->s_id, newsb->s_type->name); 858094f7b69SJeff Layton return -EBUSY; 859094f7b69SJeff Layton } 860094f7b69SJeff Layton 861094f7b69SJeff Layton static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb, 8620b4d3452SScott Mayhew struct super_block *newsb, 8630b4d3452SScott Mayhew unsigned long kern_flags, 8640b4d3452SScott Mayhew unsigned long *set_kern_flags) 865c9180a57SEric Paris { 8660b4d3452SScott Mayhew int rc = 0; 867c9180a57SEric Paris const struct superblock_security_struct *oldsbsec = oldsb->s_security; 868c9180a57SEric Paris struct superblock_security_struct *newsbsec = newsb->s_security; 869c9180a57SEric Paris 870c9180a57SEric Paris int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT); 871c9180a57SEric Paris int set_context = (oldsbsec->flags & CONTEXT_MNT); 872c9180a57SEric Paris int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT); 873c9180a57SEric Paris 8740f5e6420SEric Paris /* 8750f5e6420SEric Paris * if the parent was able to be mounted it clearly had no special lsm 876e8c26255SAl Viro * mount options. thus we can safely deal with this superblock later 8770f5e6420SEric Paris */ 87865cddd50SOndrej Mosnacek if (!selinux_initialized(&selinux_state)) 879094f7b69SJeff Layton return 0; 880c9180a57SEric Paris 8810b4d3452SScott Mayhew /* 8820b4d3452SScott Mayhew * Specifying internal flags without providing a place to 8830b4d3452SScott Mayhew * place the results is not allowed. 8840b4d3452SScott Mayhew */ 8850b4d3452SScott Mayhew if (kern_flags && !set_kern_flags) 8860b4d3452SScott Mayhew return -EINVAL; 8870b4d3452SScott Mayhew 888c9180a57SEric Paris /* how can we clone if the old one wasn't set up?? */ 8890d90a7ecSDavid P. Quigley BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED)); 890c9180a57SEric Paris 891094f7b69SJeff Layton /* if fs is reusing a sb, make sure that the contexts match */ 8923815a245SJ. Bruce Fields if (newsbsec->flags & SE_SBINITIALIZED) { 8933815a245SJ. Bruce Fields if ((kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) 8943815a245SJ. Bruce Fields *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS; 895094f7b69SJeff Layton return selinux_cmp_sb_context(oldsb, newsb); 8963815a245SJ. Bruce Fields } 8975a552617SEric Paris 898c9180a57SEric Paris mutex_lock(&newsbsec->lock); 899c9180a57SEric Paris 900c9180a57SEric Paris newsbsec->flags = oldsbsec->flags; 901c9180a57SEric Paris 902c9180a57SEric Paris newsbsec->sid = oldsbsec->sid; 903c9180a57SEric Paris newsbsec->def_sid = oldsbsec->def_sid; 904c9180a57SEric Paris newsbsec->behavior = oldsbsec->behavior; 905c9180a57SEric Paris 9060b4d3452SScott Mayhew if (newsbsec->behavior == SECURITY_FS_USE_NATIVE && 9070b4d3452SScott Mayhew !(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) { 908aa8e712cSStephen Smalley rc = security_fs_use(&selinux_state, newsb); 9090b4d3452SScott Mayhew if (rc) 9100b4d3452SScott Mayhew goto out; 9110b4d3452SScott Mayhew } 9120b4d3452SScott Mayhew 9130b4d3452SScott Mayhew if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) { 9140b4d3452SScott Mayhew newsbsec->behavior = SECURITY_FS_USE_NATIVE; 9150b4d3452SScott Mayhew *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS; 9160b4d3452SScott Mayhew } 9170b4d3452SScott Mayhew 918c9180a57SEric Paris if (set_context) { 919c9180a57SEric Paris u32 sid = oldsbsec->mntpoint_sid; 920c9180a57SEric Paris 921c9180a57SEric Paris if (!set_fscontext) 922c9180a57SEric Paris newsbsec->sid = sid; 923c9180a57SEric Paris if (!set_rootcontext) { 92483da53c5SAndreas Gruenbacher struct inode_security_struct *newisec = backing_inode_security(newsb->s_root); 925c9180a57SEric Paris newisec->sid = sid; 926c9180a57SEric Paris } 927c9180a57SEric Paris newsbsec->mntpoint_sid = sid; 928c9180a57SEric Paris } 929c9180a57SEric Paris if (set_rootcontext) { 93083da53c5SAndreas Gruenbacher const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root); 93183da53c5SAndreas Gruenbacher struct inode_security_struct *newisec = backing_inode_security(newsb->s_root); 932c9180a57SEric Paris 933c9180a57SEric Paris newisec->sid = oldisec->sid; 934c9180a57SEric Paris } 935c9180a57SEric Paris 936c9180a57SEric Paris sb_finish_set_opts(newsb); 9370b4d3452SScott Mayhew out: 938c9180a57SEric Paris mutex_unlock(&newsbsec->lock); 9390b4d3452SScott Mayhew return rc; 940c9180a57SEric Paris } 941c9180a57SEric Paris 942ba641862SAl Viro static int selinux_add_opt(int token, const char *s, void **mnt_opts) 943c9180a57SEric Paris { 944ba641862SAl Viro struct selinux_mnt_opts *opts = *mnt_opts; 945c9180a57SEric Paris 946da3d76abSAl Viro if (token == Opt_seclabel) /* eaten and completely ignored */ 947e0007529SEric Paris return 0; 948e0007529SEric Paris 949ba641862SAl Viro if (!opts) { 950ba641862SAl Viro opts = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL); 951ba641862SAl Viro if (!opts) 952ba641862SAl Viro return -ENOMEM; 953ba641862SAl Viro *mnt_opts = opts; 954ba641862SAl Viro } 955ba641862SAl Viro if (!s) 956ba641862SAl Viro return -ENOMEM; 957ba641862SAl Viro switch (token) { 958ba641862SAl Viro case Opt_context: 959ba641862SAl Viro if (opts->context || opts->defcontext) 960ba641862SAl Viro goto Einval; 961ba641862SAl Viro opts->context = s; 962ba641862SAl Viro break; 963ba641862SAl Viro case Opt_fscontext: 964ba641862SAl Viro if (opts->fscontext) 965ba641862SAl Viro goto Einval; 966ba641862SAl Viro opts->fscontext = s; 967ba641862SAl Viro break; 968ba641862SAl Viro case Opt_rootcontext: 969ba641862SAl Viro if (opts->rootcontext) 970ba641862SAl Viro goto Einval; 971ba641862SAl Viro opts->rootcontext = s; 972ba641862SAl Viro break; 973ba641862SAl Viro case Opt_defcontext: 974ba641862SAl Viro if (opts->context || opts->defcontext) 975ba641862SAl Viro goto Einval; 976ba641862SAl Viro opts->defcontext = s; 977ba641862SAl Viro break; 978ba641862SAl Viro } 979ba641862SAl Viro return 0; 980ba641862SAl Viro Einval: 981ba641862SAl Viro pr_warn(SEL_MOUNT_FAIL_MSG); 982ba641862SAl Viro return -EINVAL; 983ba641862SAl Viro } 984ba641862SAl Viro 985757cbe59SAl Viro static int selinux_add_mnt_opt(const char *option, const char *val, int len, 986204cc0ccSAl Viro void **mnt_opts) 987c9180a57SEric Paris { 988757cbe59SAl Viro int token = Opt_error; 989757cbe59SAl Viro int rc, i; 990c9180a57SEric Paris 991757cbe59SAl Viro for (i = 0; i < ARRAY_SIZE(tokens); i++) { 992757cbe59SAl Viro if (strcmp(option, tokens[i].name) == 0) { 993757cbe59SAl Viro token = tokens[i].opt; 994757cbe59SAl Viro break; 995757cbe59SAl Viro } 996169d68efSAl Viro } 997169d68efSAl Viro 998757cbe59SAl Viro if (token == Opt_error) 999757cbe59SAl Viro return -EINVAL; 1000c9180a57SEric Paris 1001e2e0e097SGen Zhang if (token != Opt_seclabel) { 1002757cbe59SAl Viro val = kmemdup_nul(val, len, GFP_KERNEL); 1003e2e0e097SGen Zhang if (!val) { 1004e2e0e097SGen Zhang rc = -ENOMEM; 1005e2e0e097SGen Zhang goto free_opt; 1006e2e0e097SGen Zhang } 1007e2e0e097SGen Zhang } 1008757cbe59SAl Viro rc = selinux_add_opt(token, val, mnt_opts); 1009757cbe59SAl Viro if (unlikely(rc)) { 1010757cbe59SAl Viro kfree(val); 1011e2e0e097SGen Zhang goto free_opt; 1012e2e0e097SGen Zhang } 1013e2e0e097SGen Zhang return rc; 1014e2e0e097SGen Zhang 1015e2e0e097SGen Zhang free_opt: 1016757cbe59SAl Viro if (*mnt_opts) { 1017ba641862SAl Viro selinux_free_mnt_opts(*mnt_opts); 1018ba641862SAl Viro *mnt_opts = NULL; 1019757cbe59SAl Viro } 1020c9180a57SEric Paris return rc; 10211da177e4SLinus Torvalds } 10221da177e4SLinus Torvalds 1023e3489f89SAl Viro static int show_sid(struct seq_file *m, u32 sid) 10242069f457SEric Paris { 1025e3489f89SAl Viro char *context = NULL; 1026e3489f89SAl Viro u32 len; 1027e3489f89SAl Viro int rc; 10282069f457SEric Paris 1029e3489f89SAl Viro rc = security_sid_to_context(&selinux_state, sid, 1030e3489f89SAl Viro &context, &len); 1031e3489f89SAl Viro if (!rc) { 1032e3489f89SAl Viro bool has_comma = context && strchr(context, ','); 103311689d47SDavid P. Quigley 1034442155c1SDavid Howells seq_putc(m, '='); 10352069f457SEric Paris if (has_comma) 10362069f457SEric Paris seq_putc(m, '\"'); 1037e3489f89SAl Viro seq_escape(m, context, "\"\n\\"); 10382069f457SEric Paris if (has_comma) 10392069f457SEric Paris seq_putc(m, '\"'); 10402069f457SEric Paris } 10411da177e4SLinus Torvalds kfree(context); 10421da177e4SLinus Torvalds return rc; 10431da177e4SLinus Torvalds } 10442069f457SEric Paris 10452069f457SEric Paris static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb) 10462069f457SEric Paris { 1047e3489f89SAl Viro struct superblock_security_struct *sbsec = sb->s_security; 10482069f457SEric Paris int rc; 10492069f457SEric Paris 1050e3489f89SAl Viro if (!(sbsec->flags & SE_SBINITIALIZED)) 1051e3489f89SAl Viro return 0; 1052e3489f89SAl Viro 105365cddd50SOndrej Mosnacek if (!selinux_initialized(&selinux_state)) 1054e3489f89SAl Viro return 0; 1055e3489f89SAl Viro 1056e3489f89SAl Viro if (sbsec->flags & FSCONTEXT_MNT) { 1057e3489f89SAl Viro seq_putc(m, ','); 1058e3489f89SAl Viro seq_puts(m, FSCONTEXT_STR); 1059e3489f89SAl Viro rc = show_sid(m, sbsec->sid); 1060e3489f89SAl Viro if (rc) 10612069f457SEric Paris return rc; 1062383795c2SEric Paris } 1063e3489f89SAl Viro if (sbsec->flags & CONTEXT_MNT) { 1064e3489f89SAl Viro seq_putc(m, ','); 1065e3489f89SAl Viro seq_puts(m, CONTEXT_STR); 1066e3489f89SAl Viro rc = show_sid(m, sbsec->mntpoint_sid); 1067e3489f89SAl Viro if (rc) 10682069f457SEric Paris return rc; 10692069f457SEric Paris } 1070e3489f89SAl Viro if (sbsec->flags & DEFCONTEXT_MNT) { 1071e3489f89SAl Viro seq_putc(m, ','); 1072e3489f89SAl Viro seq_puts(m, DEFCONTEXT_STR); 1073e3489f89SAl Viro rc = show_sid(m, sbsec->def_sid); 1074e3489f89SAl Viro if (rc) 1075e3489f89SAl Viro return rc; 1076e3489f89SAl Viro } 1077e3489f89SAl Viro if (sbsec->flags & ROOTCONTEXT_MNT) { 1078e3489f89SAl Viro struct dentry *root = sbsec->sb->s_root; 1079e3489f89SAl Viro struct inode_security_struct *isec = backing_inode_security(root); 1080e3489f89SAl Viro seq_putc(m, ','); 1081e3489f89SAl Viro seq_puts(m, ROOTCONTEXT_STR); 1082e3489f89SAl Viro rc = show_sid(m, isec->sid); 1083e3489f89SAl Viro if (rc) 1084e3489f89SAl Viro return rc; 1085e3489f89SAl Viro } 1086e3489f89SAl Viro if (sbsec->flags & SBLABEL_MNT) { 1087e3489f89SAl Viro seq_putc(m, ','); 1088442155c1SDavid Howells seq_puts(m, SECLABEL_STR); 1089e3489f89SAl Viro } 1090e3489f89SAl Viro return 0; 1091e3489f89SAl Viro } 10922069f457SEric Paris 10931da177e4SLinus Torvalds static inline u16 inode_mode_to_security_class(umode_t mode) 10941da177e4SLinus Torvalds { 10951da177e4SLinus Torvalds switch (mode & S_IFMT) { 10961da177e4SLinus Torvalds case S_IFSOCK: 10971da177e4SLinus Torvalds return SECCLASS_SOCK_FILE; 10981da177e4SLinus Torvalds case S_IFLNK: 10991da177e4SLinus Torvalds return SECCLASS_LNK_FILE; 11001da177e4SLinus Torvalds case S_IFREG: 11011da177e4SLinus Torvalds return SECCLASS_FILE; 11021da177e4SLinus Torvalds case S_IFBLK: 11031da177e4SLinus Torvalds return SECCLASS_BLK_FILE; 11041da177e4SLinus Torvalds case S_IFDIR: 11051da177e4SLinus Torvalds return SECCLASS_DIR; 11061da177e4SLinus Torvalds case S_IFCHR: 11071da177e4SLinus Torvalds return SECCLASS_CHR_FILE; 11081da177e4SLinus Torvalds case S_IFIFO: 11091da177e4SLinus Torvalds return SECCLASS_FIFO_FILE; 11101da177e4SLinus Torvalds 11111da177e4SLinus Torvalds } 11121da177e4SLinus Torvalds 11131da177e4SLinus Torvalds return SECCLASS_FILE; 11141da177e4SLinus Torvalds } 11151da177e4SLinus Torvalds 111613402580SJames Morris static inline int default_protocol_stream(int protocol) 111713402580SJames Morris { 111813402580SJames Morris return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP); 111913402580SJames Morris } 112013402580SJames Morris 112113402580SJames Morris static inline int default_protocol_dgram(int protocol) 112213402580SJames Morris { 112313402580SJames Morris return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP); 112413402580SJames Morris } 112513402580SJames Morris 11261da177e4SLinus Torvalds static inline u16 socket_type_to_security_class(int family, int type, int protocol) 11271da177e4SLinus Torvalds { 1128aa8e712cSStephen Smalley int extsockclass = selinux_policycap_extsockclass(); 1129da69a530SStephen Smalley 11301da177e4SLinus Torvalds switch (family) { 11311da177e4SLinus Torvalds case PF_UNIX: 11321da177e4SLinus Torvalds switch (type) { 11331da177e4SLinus Torvalds case SOCK_STREAM: 11341da177e4SLinus Torvalds case SOCK_SEQPACKET: 11351da177e4SLinus Torvalds return SECCLASS_UNIX_STREAM_SOCKET; 11361da177e4SLinus Torvalds case SOCK_DGRAM: 11372a764b52SLuis Ressel case SOCK_RAW: 11381da177e4SLinus Torvalds return SECCLASS_UNIX_DGRAM_SOCKET; 11391da177e4SLinus Torvalds } 11401da177e4SLinus Torvalds break; 11411da177e4SLinus Torvalds case PF_INET: 11421da177e4SLinus Torvalds case PF_INET6: 11431da177e4SLinus Torvalds switch (type) { 11441da177e4SLinus Torvalds case SOCK_STREAM: 1145da69a530SStephen Smalley case SOCK_SEQPACKET: 114613402580SJames Morris if (default_protocol_stream(protocol)) 11471da177e4SLinus Torvalds return SECCLASS_TCP_SOCKET; 1148da69a530SStephen Smalley else if (extsockclass && protocol == IPPROTO_SCTP) 1149da69a530SStephen Smalley return SECCLASS_SCTP_SOCKET; 115013402580SJames Morris else 115113402580SJames Morris return SECCLASS_RAWIP_SOCKET; 11521da177e4SLinus Torvalds case SOCK_DGRAM: 115313402580SJames Morris if (default_protocol_dgram(protocol)) 11541da177e4SLinus Torvalds return SECCLASS_UDP_SOCKET; 1155ef37979aSStephen Smalley else if (extsockclass && (protocol == IPPROTO_ICMP || 1156ef37979aSStephen Smalley protocol == IPPROTO_ICMPV6)) 1157da69a530SStephen Smalley return SECCLASS_ICMP_SOCKET; 115813402580SJames Morris else 115913402580SJames Morris return SECCLASS_RAWIP_SOCKET; 11602ee92d46SJames Morris case SOCK_DCCP: 11612ee92d46SJames Morris return SECCLASS_DCCP_SOCKET; 116213402580SJames Morris default: 11631da177e4SLinus Torvalds return SECCLASS_RAWIP_SOCKET; 11641da177e4SLinus Torvalds } 11651da177e4SLinus Torvalds break; 11661da177e4SLinus Torvalds case PF_NETLINK: 11671da177e4SLinus Torvalds switch (protocol) { 11681da177e4SLinus Torvalds case NETLINK_ROUTE: 11691da177e4SLinus Torvalds return SECCLASS_NETLINK_ROUTE_SOCKET; 11707f1fb60cSPavel Emelyanov case NETLINK_SOCK_DIAG: 11711da177e4SLinus Torvalds return SECCLASS_NETLINK_TCPDIAG_SOCKET; 11721da177e4SLinus Torvalds case NETLINK_NFLOG: 11731da177e4SLinus Torvalds return SECCLASS_NETLINK_NFLOG_SOCKET; 11741da177e4SLinus Torvalds case NETLINK_XFRM: 11751da177e4SLinus Torvalds return SECCLASS_NETLINK_XFRM_SOCKET; 11761da177e4SLinus Torvalds case NETLINK_SELINUX: 11771da177e4SLinus Torvalds return SECCLASS_NETLINK_SELINUX_SOCKET; 11786c6d2e9bSStephen Smalley case NETLINK_ISCSI: 11796c6d2e9bSStephen Smalley return SECCLASS_NETLINK_ISCSI_SOCKET; 11801da177e4SLinus Torvalds case NETLINK_AUDIT: 11811da177e4SLinus Torvalds return SECCLASS_NETLINK_AUDIT_SOCKET; 11826c6d2e9bSStephen Smalley case NETLINK_FIB_LOOKUP: 11836c6d2e9bSStephen Smalley return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET; 11846c6d2e9bSStephen Smalley case NETLINK_CONNECTOR: 11856c6d2e9bSStephen Smalley return SECCLASS_NETLINK_CONNECTOR_SOCKET; 11866c6d2e9bSStephen Smalley case NETLINK_NETFILTER: 11876c6d2e9bSStephen Smalley return SECCLASS_NETLINK_NETFILTER_SOCKET; 11881da177e4SLinus Torvalds case NETLINK_DNRTMSG: 11891da177e4SLinus Torvalds return SECCLASS_NETLINK_DNRT_SOCKET; 11900c9b7942SJames Morris case NETLINK_KOBJECT_UEVENT: 11910c9b7942SJames Morris return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET; 11926c6d2e9bSStephen Smalley case NETLINK_GENERIC: 11936c6d2e9bSStephen Smalley return SECCLASS_NETLINK_GENERIC_SOCKET; 11946c6d2e9bSStephen Smalley case NETLINK_SCSITRANSPORT: 11956c6d2e9bSStephen Smalley return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET; 11966c6d2e9bSStephen Smalley case NETLINK_RDMA: 11976c6d2e9bSStephen Smalley return SECCLASS_NETLINK_RDMA_SOCKET; 11986c6d2e9bSStephen Smalley case NETLINK_CRYPTO: 11996c6d2e9bSStephen Smalley return SECCLASS_NETLINK_CRYPTO_SOCKET; 12001da177e4SLinus Torvalds default: 12011da177e4SLinus Torvalds return SECCLASS_NETLINK_SOCKET; 12021da177e4SLinus Torvalds } 12031da177e4SLinus Torvalds case PF_PACKET: 12041da177e4SLinus Torvalds return SECCLASS_PACKET_SOCKET; 12051da177e4SLinus Torvalds case PF_KEY: 12061da177e4SLinus Torvalds return SECCLASS_KEY_SOCKET; 12073e3ff15eSChristopher J. PeBenito case PF_APPLETALK: 12083e3ff15eSChristopher J. PeBenito return SECCLASS_APPLETALK_SOCKET; 12091da177e4SLinus Torvalds } 12101da177e4SLinus Torvalds 1211da69a530SStephen Smalley if (extsockclass) { 1212da69a530SStephen Smalley switch (family) { 1213da69a530SStephen Smalley case PF_AX25: 1214da69a530SStephen Smalley return SECCLASS_AX25_SOCKET; 1215da69a530SStephen Smalley case PF_IPX: 1216da69a530SStephen Smalley return SECCLASS_IPX_SOCKET; 1217da69a530SStephen Smalley case PF_NETROM: 1218da69a530SStephen Smalley return SECCLASS_NETROM_SOCKET; 1219da69a530SStephen Smalley case PF_ATMPVC: 1220da69a530SStephen Smalley return SECCLASS_ATMPVC_SOCKET; 1221da69a530SStephen Smalley case PF_X25: 1222da69a530SStephen Smalley return SECCLASS_X25_SOCKET; 1223da69a530SStephen Smalley case PF_ROSE: 1224da69a530SStephen Smalley return SECCLASS_ROSE_SOCKET; 1225da69a530SStephen Smalley case PF_DECnet: 1226da69a530SStephen Smalley return SECCLASS_DECNET_SOCKET; 1227da69a530SStephen Smalley case PF_ATMSVC: 1228da69a530SStephen Smalley return SECCLASS_ATMSVC_SOCKET; 1229da69a530SStephen Smalley case PF_RDS: 1230da69a530SStephen Smalley return SECCLASS_RDS_SOCKET; 1231da69a530SStephen Smalley case PF_IRDA: 1232da69a530SStephen Smalley return SECCLASS_IRDA_SOCKET; 1233da69a530SStephen Smalley case PF_PPPOX: 1234da69a530SStephen Smalley return SECCLASS_PPPOX_SOCKET; 1235da69a530SStephen Smalley case PF_LLC: 1236da69a530SStephen Smalley return SECCLASS_LLC_SOCKET; 1237da69a530SStephen Smalley case PF_CAN: 1238da69a530SStephen Smalley return SECCLASS_CAN_SOCKET; 1239da69a530SStephen Smalley case PF_TIPC: 1240da69a530SStephen Smalley return SECCLASS_TIPC_SOCKET; 1241da69a530SStephen Smalley case PF_BLUETOOTH: 1242da69a530SStephen Smalley return SECCLASS_BLUETOOTH_SOCKET; 1243da69a530SStephen Smalley case PF_IUCV: 1244da69a530SStephen Smalley return SECCLASS_IUCV_SOCKET; 1245da69a530SStephen Smalley case PF_RXRPC: 1246da69a530SStephen Smalley return SECCLASS_RXRPC_SOCKET; 1247da69a530SStephen Smalley case PF_ISDN: 1248da69a530SStephen Smalley return SECCLASS_ISDN_SOCKET; 1249da69a530SStephen Smalley case PF_PHONET: 1250da69a530SStephen Smalley return SECCLASS_PHONET_SOCKET; 1251da69a530SStephen Smalley case PF_IEEE802154: 1252da69a530SStephen Smalley return SECCLASS_IEEE802154_SOCKET; 1253da69a530SStephen Smalley case PF_CAIF: 1254da69a530SStephen Smalley return SECCLASS_CAIF_SOCKET; 1255da69a530SStephen Smalley case PF_ALG: 1256da69a530SStephen Smalley return SECCLASS_ALG_SOCKET; 1257da69a530SStephen Smalley case PF_NFC: 1258da69a530SStephen Smalley return SECCLASS_NFC_SOCKET; 1259da69a530SStephen Smalley case PF_VSOCK: 1260da69a530SStephen Smalley return SECCLASS_VSOCK_SOCKET; 1261da69a530SStephen Smalley case PF_KCM: 1262da69a530SStephen Smalley return SECCLASS_KCM_SOCKET; 1263da69a530SStephen Smalley case PF_QIPCRTR: 1264da69a530SStephen Smalley return SECCLASS_QIPCRTR_SOCKET; 12653051bf36SLinus Torvalds case PF_SMC: 12663051bf36SLinus Torvalds return SECCLASS_SMC_SOCKET; 126768e8b849SBjörn Töpel case PF_XDP: 126868e8b849SBjörn Töpel return SECCLASS_XDP_SOCKET; 126968e8b849SBjörn Töpel #if PF_MAX > 45 1270da69a530SStephen Smalley #error New address family defined, please update this function. 1271da69a530SStephen Smalley #endif 1272da69a530SStephen Smalley } 1273da69a530SStephen Smalley } 1274da69a530SStephen Smalley 12751da177e4SLinus Torvalds return SECCLASS_SOCKET; 12761da177e4SLinus Torvalds } 12771da177e4SLinus Torvalds 1278134509d5SStephen Smalley static int selinux_genfs_get_sid(struct dentry *dentry, 12791da177e4SLinus Torvalds u16 tclass, 1280134509d5SStephen Smalley u16 flags, 12811da177e4SLinus Torvalds u32 *sid) 12821da177e4SLinus Torvalds { 12838e6c9693SLucian Adrian Grijincu int rc; 1284fc64005cSAl Viro struct super_block *sb = dentry->d_sb; 12858e6c9693SLucian Adrian Grijincu char *buffer, *path; 12861da177e4SLinus Torvalds 12871da177e4SLinus Torvalds buffer = (char *)__get_free_page(GFP_KERNEL); 12881da177e4SLinus Torvalds if (!buffer) 12891da177e4SLinus Torvalds return -ENOMEM; 12901da177e4SLinus Torvalds 12918e6c9693SLucian Adrian Grijincu path = dentry_path_raw(dentry, buffer, PAGE_SIZE); 12928e6c9693SLucian Adrian Grijincu if (IS_ERR(path)) 12938e6c9693SLucian Adrian Grijincu rc = PTR_ERR(path); 12948e6c9693SLucian Adrian Grijincu else { 1295134509d5SStephen Smalley if (flags & SE_SBPROC) { 12968e6c9693SLucian Adrian Grijincu /* each process gets a /proc/PID/ entry. Strip off the 12978e6c9693SLucian Adrian Grijincu * PID part to get a valid selinux labeling. 12988e6c9693SLucian Adrian Grijincu * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */ 12998e6c9693SLucian Adrian Grijincu while (path[1] >= '0' && path[1] <= '9') { 13008e6c9693SLucian Adrian Grijincu path[1] = '/'; 13018e6c9693SLucian Adrian Grijincu path++; 13021da177e4SLinus Torvalds } 1303134509d5SStephen Smalley } 1304aa8e712cSStephen Smalley rc = security_genfs_sid(&selinux_state, sb->s_type->name, 1305aa8e712cSStephen Smalley path, tclass, sid); 13067bb185edSStephen Smalley if (rc == -ENOENT) { 13077bb185edSStephen Smalley /* No match in policy, mark as unlabeled. */ 13087bb185edSStephen Smalley *sid = SECINITSID_UNLABELED; 13097bb185edSStephen Smalley rc = 0; 13107bb185edSStephen Smalley } 13118e6c9693SLucian Adrian Grijincu } 13121da177e4SLinus Torvalds free_page((unsigned long)buffer); 13131da177e4SLinus Torvalds return rc; 13141da177e4SLinus Torvalds } 13151da177e4SLinus Torvalds 1316b754026bSOndrej Mosnacek static int inode_doinit_use_xattr(struct inode *inode, struct dentry *dentry, 1317b754026bSOndrej Mosnacek u32 def_sid, u32 *sid) 1318b754026bSOndrej Mosnacek { 1319b754026bSOndrej Mosnacek #define INITCONTEXTLEN 255 1320b754026bSOndrej Mosnacek char *context; 1321b754026bSOndrej Mosnacek unsigned int len; 1322b754026bSOndrej Mosnacek int rc; 1323b754026bSOndrej Mosnacek 1324b754026bSOndrej Mosnacek len = INITCONTEXTLEN; 1325b754026bSOndrej Mosnacek context = kmalloc(len + 1, GFP_NOFS); 1326b754026bSOndrej Mosnacek if (!context) 1327b754026bSOndrej Mosnacek return -ENOMEM; 1328b754026bSOndrej Mosnacek 1329b754026bSOndrej Mosnacek context[len] = '\0'; 1330b754026bSOndrej Mosnacek rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len); 1331b754026bSOndrej Mosnacek if (rc == -ERANGE) { 1332b754026bSOndrej Mosnacek kfree(context); 1333b754026bSOndrej Mosnacek 1334b754026bSOndrej Mosnacek /* Need a larger buffer. Query for the right size. */ 1335b754026bSOndrej Mosnacek rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0); 1336b754026bSOndrej Mosnacek if (rc < 0) 1337b754026bSOndrej Mosnacek return rc; 1338b754026bSOndrej Mosnacek 1339b754026bSOndrej Mosnacek len = rc; 1340b754026bSOndrej Mosnacek context = kmalloc(len + 1, GFP_NOFS); 1341b754026bSOndrej Mosnacek if (!context) 1342b754026bSOndrej Mosnacek return -ENOMEM; 1343b754026bSOndrej Mosnacek 1344b754026bSOndrej Mosnacek context[len] = '\0'; 1345b754026bSOndrej Mosnacek rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, 1346b754026bSOndrej Mosnacek context, len); 1347b754026bSOndrej Mosnacek } 1348b754026bSOndrej Mosnacek if (rc < 0) { 1349b754026bSOndrej Mosnacek kfree(context); 1350b754026bSOndrej Mosnacek if (rc != -ENODATA) { 1351b754026bSOndrej Mosnacek pr_warn("SELinux: %s: getxattr returned %d for dev=%s ino=%ld\n", 1352b754026bSOndrej Mosnacek __func__, -rc, inode->i_sb->s_id, inode->i_ino); 1353b754026bSOndrej Mosnacek return rc; 1354b754026bSOndrej Mosnacek } 1355b754026bSOndrej Mosnacek *sid = def_sid; 1356b754026bSOndrej Mosnacek return 0; 1357b754026bSOndrej Mosnacek } 1358b754026bSOndrej Mosnacek 1359b754026bSOndrej Mosnacek rc = security_context_to_sid_default(&selinux_state, context, rc, sid, 1360b754026bSOndrej Mosnacek def_sid, GFP_NOFS); 1361b754026bSOndrej Mosnacek if (rc) { 1362b754026bSOndrej Mosnacek char *dev = inode->i_sb->s_id; 1363b754026bSOndrej Mosnacek unsigned long ino = inode->i_ino; 1364b754026bSOndrej Mosnacek 1365b754026bSOndrej Mosnacek if (rc == -EINVAL) { 1366b754026bSOndrej 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", 1367b754026bSOndrej Mosnacek ino, dev, context); 1368b754026bSOndrej Mosnacek } else { 1369b754026bSOndrej Mosnacek pr_warn("SELinux: %s: context_to_sid(%s) returned %d for dev=%s ino=%ld\n", 1370b754026bSOndrej Mosnacek __func__, context, -rc, dev, ino); 1371b754026bSOndrej Mosnacek } 1372b754026bSOndrej Mosnacek } 1373b754026bSOndrej Mosnacek kfree(context); 1374b754026bSOndrej Mosnacek return 0; 1375b754026bSOndrej Mosnacek } 1376b754026bSOndrej Mosnacek 13771da177e4SLinus Torvalds /* The inode's security attributes must be initialized before first use. */ 13781da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry) 13791da177e4SLinus Torvalds { 13801da177e4SLinus Torvalds struct superblock_security_struct *sbsec = NULL; 138180788c22SCasey Schaufler struct inode_security_struct *isec = selinux_inode(inode); 13829287aed2SAndreas Gruenbacher u32 task_sid, sid = 0; 13839287aed2SAndreas Gruenbacher u16 sclass; 13841da177e4SLinus Torvalds struct dentry *dentry; 13851da177e4SLinus Torvalds int rc = 0; 13861da177e4SLinus Torvalds 13876f3be9f5SAndreas Gruenbacher if (isec->initialized == LABEL_INITIALIZED) 138813457d07SAndreas Gruenbacher return 0; 13891da177e4SLinus Torvalds 13909287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 13916f3be9f5SAndreas Gruenbacher if (isec->initialized == LABEL_INITIALIZED) 139223970741SEric Paris goto out_unlock; 13931da177e4SLinus Torvalds 139413457d07SAndreas Gruenbacher if (isec->sclass == SECCLASS_FILE) 139513457d07SAndreas Gruenbacher isec->sclass = inode_mode_to_security_class(inode->i_mode); 139613457d07SAndreas Gruenbacher 13971da177e4SLinus Torvalds sbsec = inode->i_sb->s_security; 13980d90a7ecSDavid P. Quigley if (!(sbsec->flags & SE_SBINITIALIZED)) { 13991da177e4SLinus Torvalds /* Defer initialization until selinux_complete_init, 14001da177e4SLinus Torvalds after the initial policy is loaded and the security 14011da177e4SLinus Torvalds server is ready to handle calls. */ 14021da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 14031da177e4SLinus Torvalds if (list_empty(&isec->list)) 14041da177e4SLinus Torvalds list_add(&isec->list, &sbsec->isec_head); 14051da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 140623970741SEric Paris goto out_unlock; 14071da177e4SLinus Torvalds } 14081da177e4SLinus Torvalds 14099287aed2SAndreas Gruenbacher sclass = isec->sclass; 14109287aed2SAndreas Gruenbacher task_sid = isec->task_sid; 14119287aed2SAndreas Gruenbacher sid = isec->sid; 14129287aed2SAndreas Gruenbacher isec->initialized = LABEL_PENDING; 14139287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 14149287aed2SAndreas Gruenbacher 14151da177e4SLinus Torvalds switch (sbsec->behavior) { 1416eb9ae686SDavid Quigley case SECURITY_FS_USE_NATIVE: 1417eb9ae686SDavid Quigley break; 14181da177e4SLinus Torvalds case SECURITY_FS_USE_XATTR: 14195d6c3191SAndreas Gruenbacher if (!(inode->i_opflags & IOP_XATTR)) { 14209287aed2SAndreas Gruenbacher sid = sbsec->def_sid; 14211da177e4SLinus Torvalds break; 14221da177e4SLinus Torvalds } 14231da177e4SLinus Torvalds /* Need a dentry, since the xattr API requires one. 14241da177e4SLinus Torvalds Life would be simpler if we could just pass the inode. */ 14251da177e4SLinus Torvalds if (opt_dentry) { 14261da177e4SLinus Torvalds /* Called from d_instantiate or d_splice_alias. */ 14271da177e4SLinus Torvalds dentry = dget(opt_dentry); 14281da177e4SLinus Torvalds } else { 1429b127125dSAl Viro /* 1430b127125dSAl Viro * Called from selinux_complete_init, try to find a dentry. 1431b127125dSAl Viro * Some filesystems really want a connected one, so try 1432b127125dSAl Viro * that first. We could split SECURITY_FS_USE_XATTR in 1433b127125dSAl Viro * two, depending upon that... 1434b127125dSAl Viro */ 14351da177e4SLinus Torvalds dentry = d_find_alias(inode); 1436b127125dSAl Viro if (!dentry) 1437b127125dSAl Viro dentry = d_find_any_alias(inode); 14381da177e4SLinus Torvalds } 14391da177e4SLinus Torvalds if (!dentry) { 1440df7f54c0SEric Paris /* 1441df7f54c0SEric Paris * this is can be hit on boot when a file is accessed 1442df7f54c0SEric Paris * before the policy is loaded. When we load policy we 1443df7f54c0SEric Paris * may find inodes that have no dentry on the 1444df7f54c0SEric Paris * sbsec->isec_head list. No reason to complain as these 1445df7f54c0SEric Paris * will get fixed up the next time we go through 1446df7f54c0SEric Paris * inode_doinit with a dentry, before these inodes could 1447df7f54c0SEric Paris * be used again by userspace. 1448df7f54c0SEric Paris */ 14499287aed2SAndreas Gruenbacher goto out; 14501da177e4SLinus Torvalds } 14511da177e4SLinus Torvalds 1452b754026bSOndrej Mosnacek rc = inode_doinit_use_xattr(inode, dentry, sbsec->def_sid, 1453b754026bSOndrej Mosnacek &sid); 14541da177e4SLinus Torvalds dput(dentry); 1455b754026bSOndrej Mosnacek if (rc) 14569287aed2SAndreas Gruenbacher goto out; 14571da177e4SLinus Torvalds break; 14581da177e4SLinus Torvalds case SECURITY_FS_USE_TASK: 14599287aed2SAndreas Gruenbacher sid = task_sid; 14601da177e4SLinus Torvalds break; 14611da177e4SLinus Torvalds case SECURITY_FS_USE_TRANS: 14621da177e4SLinus Torvalds /* Default to the fs SID. */ 14639287aed2SAndreas Gruenbacher sid = sbsec->sid; 14641da177e4SLinus Torvalds 14651da177e4SLinus Torvalds /* Try to obtain a transition SID. */ 1466aa8e712cSStephen Smalley rc = security_transition_sid(&selinux_state, task_sid, sid, 1467aa8e712cSStephen Smalley sclass, NULL, &sid); 14681da177e4SLinus Torvalds if (rc) 14699287aed2SAndreas Gruenbacher goto out; 14701da177e4SLinus Torvalds break; 1471c312feb2SEric Paris case SECURITY_FS_USE_MNTPOINT: 14729287aed2SAndreas Gruenbacher sid = sbsec->mntpoint_sid; 1473c312feb2SEric Paris break; 14741da177e4SLinus Torvalds default: 1475c312feb2SEric Paris /* Default to the fs superblock SID. */ 14769287aed2SAndreas Gruenbacher sid = sbsec->sid; 14771da177e4SLinus Torvalds 1478134509d5SStephen Smalley if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) { 1479f64410ecSPaul Moore /* We must have a dentry to determine the label on 1480f64410ecSPaul Moore * procfs inodes */ 1481b127125dSAl Viro if (opt_dentry) { 1482f64410ecSPaul Moore /* Called from d_instantiate or 1483f64410ecSPaul Moore * d_splice_alias. */ 1484f64410ecSPaul Moore dentry = dget(opt_dentry); 1485b127125dSAl Viro } else { 1486f64410ecSPaul Moore /* Called from selinux_complete_init, try to 1487b127125dSAl Viro * find a dentry. Some filesystems really want 1488b127125dSAl Viro * a connected one, so try that first. 1489b127125dSAl Viro */ 1490f64410ecSPaul Moore dentry = d_find_alias(inode); 1491b127125dSAl Viro if (!dentry) 1492b127125dSAl Viro dentry = d_find_any_alias(inode); 1493b127125dSAl Viro } 1494f64410ecSPaul Moore /* 1495f64410ecSPaul Moore * This can be hit on boot when a file is accessed 1496f64410ecSPaul Moore * before the policy is loaded. When we load policy we 1497f64410ecSPaul Moore * may find inodes that have no dentry on the 1498f64410ecSPaul Moore * sbsec->isec_head list. No reason to complain as 1499f64410ecSPaul Moore * these will get fixed up the next time we go through 1500f64410ecSPaul Moore * inode_doinit() with a dentry, before these inodes 1501f64410ecSPaul Moore * could be used again by userspace. 1502f64410ecSPaul Moore */ 1503f64410ecSPaul Moore if (!dentry) 15049287aed2SAndreas Gruenbacher goto out; 15059287aed2SAndreas Gruenbacher rc = selinux_genfs_get_sid(dentry, sclass, 1506134509d5SStephen Smalley sbsec->flags, &sid); 1507b754026bSOndrej Mosnacek if (rc) { 1508f64410ecSPaul Moore dput(dentry); 15099287aed2SAndreas Gruenbacher goto out; 15101da177e4SLinus Torvalds } 1511b754026bSOndrej Mosnacek 1512b754026bSOndrej Mosnacek if ((sbsec->flags & SE_SBGENFS_XATTR) && 1513b754026bSOndrej Mosnacek (inode->i_opflags & IOP_XATTR)) { 1514b754026bSOndrej Mosnacek rc = inode_doinit_use_xattr(inode, dentry, 1515b754026bSOndrej Mosnacek sid, &sid); 1516b754026bSOndrej Mosnacek if (rc) { 1517b754026bSOndrej Mosnacek dput(dentry); 1518b754026bSOndrej Mosnacek goto out; 1519b754026bSOndrej Mosnacek } 1520b754026bSOndrej Mosnacek } 1521b754026bSOndrej Mosnacek dput(dentry); 1522b754026bSOndrej Mosnacek } 15231da177e4SLinus Torvalds break; 15241da177e4SLinus Torvalds } 15251da177e4SLinus Torvalds 15269287aed2SAndreas Gruenbacher out: 15279287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 15289287aed2SAndreas Gruenbacher if (isec->initialized == LABEL_PENDING) { 15299287aed2SAndreas Gruenbacher if (!sid || rc) { 15309287aed2SAndreas Gruenbacher isec->initialized = LABEL_INVALID; 15319287aed2SAndreas Gruenbacher goto out_unlock; 15329287aed2SAndreas Gruenbacher } 15339287aed2SAndreas Gruenbacher 15346f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 15359287aed2SAndreas Gruenbacher isec->sid = sid; 15369287aed2SAndreas Gruenbacher } 15371da177e4SLinus Torvalds 153823970741SEric Paris out_unlock: 15399287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 15401da177e4SLinus Torvalds return rc; 15411da177e4SLinus Torvalds } 15421da177e4SLinus Torvalds 15431da177e4SLinus Torvalds /* Convert a Linux signal to an access vector. */ 15441da177e4SLinus Torvalds static inline u32 signal_to_av(int sig) 15451da177e4SLinus Torvalds { 15461da177e4SLinus Torvalds u32 perm = 0; 15471da177e4SLinus Torvalds 15481da177e4SLinus Torvalds switch (sig) { 15491da177e4SLinus Torvalds case SIGCHLD: 15501da177e4SLinus Torvalds /* Commonly granted from child to parent. */ 15511da177e4SLinus Torvalds perm = PROCESS__SIGCHLD; 15521da177e4SLinus Torvalds break; 15531da177e4SLinus Torvalds case SIGKILL: 15541da177e4SLinus Torvalds /* Cannot be caught or ignored */ 15551da177e4SLinus Torvalds perm = PROCESS__SIGKILL; 15561da177e4SLinus Torvalds break; 15571da177e4SLinus Torvalds case SIGSTOP: 15581da177e4SLinus Torvalds /* Cannot be caught or ignored */ 15591da177e4SLinus Torvalds perm = PROCESS__SIGSTOP; 15601da177e4SLinus Torvalds break; 15611da177e4SLinus Torvalds default: 15621da177e4SLinus Torvalds /* All other signals. */ 15631da177e4SLinus Torvalds perm = PROCESS__SIGNAL; 15641da177e4SLinus Torvalds break; 15651da177e4SLinus Torvalds } 15661da177e4SLinus Torvalds 15671da177e4SLinus Torvalds return perm; 15681da177e4SLinus Torvalds } 15691da177e4SLinus Torvalds 1570b68e418cSStephen Smalley #if CAP_LAST_CAP > 63 1571b68e418cSStephen Smalley #error Fix SELinux to handle capabilities > 63. 1572b68e418cSStephen Smalley #endif 1573b68e418cSStephen Smalley 15741da177e4SLinus Torvalds /* Check whether a task is allowed to use a capability. */ 15756a9de491SEric Paris static int cred_has_capability(const struct cred *cred, 1576c1a85a00SMicah Morton int cap, unsigned int opts, bool initns) 15771da177e4SLinus Torvalds { 15782bf49690SThomas Liu struct common_audit_data ad; 157906112163SEric Paris struct av_decision avd; 1580b68e418cSStephen Smalley u16 sclass; 15813699c53cSDavid Howells u32 sid = cred_sid(cred); 1582b68e418cSStephen Smalley u32 av = CAP_TO_MASK(cap); 158306112163SEric Paris int rc; 15841da177e4SLinus Torvalds 158550c205f5SEric Paris ad.type = LSM_AUDIT_DATA_CAP; 15861da177e4SLinus Torvalds ad.u.cap = cap; 15871da177e4SLinus Torvalds 1588b68e418cSStephen Smalley switch (CAP_TO_INDEX(cap)) { 1589b68e418cSStephen Smalley case 0: 15908e4ff6f2SStephen Smalley sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS; 1591b68e418cSStephen Smalley break; 1592b68e418cSStephen Smalley case 1: 15938e4ff6f2SStephen Smalley sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS; 1594b68e418cSStephen Smalley break; 1595b68e418cSStephen Smalley default: 1596c103a91eSpeter enderborg pr_err("SELinux: out of range capability %d\n", cap); 1597b68e418cSStephen Smalley BUG(); 1598a35c6c83SEric Paris return -EINVAL; 1599b68e418cSStephen Smalley } 160006112163SEric Paris 16016b6bc620SStephen Smalley rc = avc_has_perm_noaudit(&selinux_state, 16026b6bc620SStephen Smalley sid, sid, sclass, av, 0, &avd); 1603c1a85a00SMicah Morton if (!(opts & CAP_OPT_NOAUDIT)) { 16046b6bc620SStephen Smalley int rc2 = avc_audit(&selinux_state, 16056b6bc620SStephen Smalley sid, sid, sclass, av, &avd, rc, &ad, 0); 16069ade0cf4SEric Paris if (rc2) 16079ade0cf4SEric Paris return rc2; 16089ade0cf4SEric Paris } 160906112163SEric Paris return rc; 16101da177e4SLinus Torvalds } 16111da177e4SLinus Torvalds 16121da177e4SLinus Torvalds /* Check whether a task has a particular permission to an inode. 16131da177e4SLinus Torvalds The 'adp' parameter is optional and allows other audit 16141da177e4SLinus Torvalds data to be passed (e.g. the dentry). */ 161588e67f3bSDavid Howells static int inode_has_perm(const struct cred *cred, 16161da177e4SLinus Torvalds struct inode *inode, 16171da177e4SLinus Torvalds u32 perms, 161819e49834SLinus Torvalds struct common_audit_data *adp) 16191da177e4SLinus Torvalds { 16201da177e4SLinus Torvalds struct inode_security_struct *isec; 1621275bb41eSDavid Howells u32 sid; 16221da177e4SLinus Torvalds 1623e0e81739SDavid Howells validate_creds(cred); 1624e0e81739SDavid Howells 1625bbaca6c2SStephen Smalley if (unlikely(IS_PRIVATE(inode))) 1626bbaca6c2SStephen Smalley return 0; 1627bbaca6c2SStephen Smalley 162888e67f3bSDavid Howells sid = cred_sid(cred); 162980788c22SCasey Schaufler isec = selinux_inode(inode); 16301da177e4SLinus Torvalds 16316b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 16326b6bc620SStephen Smalley sid, isec->sid, isec->sclass, perms, adp); 16331da177e4SLinus Torvalds } 16341da177e4SLinus Torvalds 16351da177e4SLinus Torvalds /* Same as inode_has_perm, but pass explicit audit data containing 16361da177e4SLinus Torvalds the dentry to help the auditing code to more easily generate the 16371da177e4SLinus Torvalds pathname if needed. */ 163888e67f3bSDavid Howells static inline int dentry_has_perm(const struct cred *cred, 16391da177e4SLinus Torvalds struct dentry *dentry, 16401da177e4SLinus Torvalds u32 av) 16411da177e4SLinus Torvalds { 1642c6f493d6SDavid Howells struct inode *inode = d_backing_inode(dentry); 16432bf49690SThomas Liu struct common_audit_data ad; 164488e67f3bSDavid Howells 164550c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 16462875fa00SEric Paris ad.u.dentry = dentry; 16475d226df4SAndreas Gruenbacher __inode_security_revalidate(inode, dentry, true); 164819e49834SLinus Torvalds return inode_has_perm(cred, inode, av, &ad); 16492875fa00SEric Paris } 16502875fa00SEric Paris 16512875fa00SEric Paris /* Same as inode_has_perm, but pass explicit audit data containing 16522875fa00SEric Paris the path to help the auditing code to more easily generate the 16532875fa00SEric Paris pathname if needed. */ 16542875fa00SEric Paris static inline int path_has_perm(const struct cred *cred, 16553f7036a0SAl Viro const struct path *path, 16562875fa00SEric Paris u32 av) 16572875fa00SEric Paris { 1658c6f493d6SDavid Howells struct inode *inode = d_backing_inode(path->dentry); 16592875fa00SEric Paris struct common_audit_data ad; 16602875fa00SEric Paris 166150c205f5SEric Paris ad.type = LSM_AUDIT_DATA_PATH; 16622875fa00SEric Paris ad.u.path = *path; 16635d226df4SAndreas Gruenbacher __inode_security_revalidate(inode, path->dentry, true); 166419e49834SLinus Torvalds return inode_has_perm(cred, inode, av, &ad); 16651da177e4SLinus Torvalds } 16661da177e4SLinus Torvalds 166713f8e981SDavid Howells /* Same as path_has_perm, but uses the inode from the file struct. */ 166813f8e981SDavid Howells static inline int file_path_has_perm(const struct cred *cred, 166913f8e981SDavid Howells struct file *file, 167013f8e981SDavid Howells u32 av) 167113f8e981SDavid Howells { 167213f8e981SDavid Howells struct common_audit_data ad; 167313f8e981SDavid Howells 167443af5de7SVivek Goyal ad.type = LSM_AUDIT_DATA_FILE; 167543af5de7SVivek Goyal ad.u.file = file; 167619e49834SLinus Torvalds return inode_has_perm(cred, file_inode(file), av, &ad); 167713f8e981SDavid Howells } 167813f8e981SDavid Howells 1679f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL 1680f66e448cSChenbo Feng static int bpf_fd_pass(struct file *file, u32 sid); 1681f66e448cSChenbo Feng #endif 1682f66e448cSChenbo Feng 16831da177e4SLinus Torvalds /* Check whether a task can use an open file descriptor to 16841da177e4SLinus Torvalds access an inode in a given way. Check access to the 16851da177e4SLinus Torvalds descriptor itself, and then use dentry_has_perm to 16861da177e4SLinus Torvalds check a particular permission to the file. 16871da177e4SLinus Torvalds Access to the descriptor is implicitly granted if it 16881da177e4SLinus Torvalds has the same SID as the process. If av is zero, then 16891da177e4SLinus Torvalds access to the file is not checked, e.g. for cases 16901da177e4SLinus Torvalds where only the descriptor is affected like seek. */ 169188e67f3bSDavid Howells static int file_has_perm(const struct cred *cred, 16921da177e4SLinus Torvalds struct file *file, 16931da177e4SLinus Torvalds u32 av) 16941da177e4SLinus Torvalds { 1695bb6c6b02SCasey Schaufler struct file_security_struct *fsec = selinux_file(file); 1696496ad9aaSAl Viro struct inode *inode = file_inode(file); 16972bf49690SThomas Liu struct common_audit_data ad; 169888e67f3bSDavid Howells u32 sid = cred_sid(cred); 16991da177e4SLinus Torvalds int rc; 17001da177e4SLinus Torvalds 170143af5de7SVivek Goyal ad.type = LSM_AUDIT_DATA_FILE; 170243af5de7SVivek Goyal ad.u.file = file; 17031da177e4SLinus Torvalds 1704275bb41eSDavid Howells if (sid != fsec->sid) { 17056b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 17066b6bc620SStephen Smalley sid, fsec->sid, 17071da177e4SLinus Torvalds SECCLASS_FD, 17081da177e4SLinus Torvalds FD__USE, 17091da177e4SLinus Torvalds &ad); 17101da177e4SLinus Torvalds if (rc) 171188e67f3bSDavid Howells goto out; 17121da177e4SLinus Torvalds } 17131da177e4SLinus Torvalds 1714f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL 1715f66e448cSChenbo Feng rc = bpf_fd_pass(file, cred_sid(cred)); 1716f66e448cSChenbo Feng if (rc) 1717f66e448cSChenbo Feng return rc; 1718f66e448cSChenbo Feng #endif 1719f66e448cSChenbo Feng 17201da177e4SLinus Torvalds /* av is zero if only checking access to the descriptor. */ 172188e67f3bSDavid Howells rc = 0; 17221da177e4SLinus Torvalds if (av) 172319e49834SLinus Torvalds rc = inode_has_perm(cred, inode, av, &ad); 17241da177e4SLinus Torvalds 172588e67f3bSDavid Howells out: 172688e67f3bSDavid Howells return rc; 17271da177e4SLinus Torvalds } 17281da177e4SLinus Torvalds 1729c3c188b2SDavid Howells /* 1730c3c188b2SDavid Howells * Determine the label for an inode that might be unioned. 1731c3c188b2SDavid Howells */ 1732c957f6dfSVivek Goyal static int 1733c957f6dfSVivek Goyal selinux_determine_inode_label(const struct task_security_struct *tsec, 1734c957f6dfSVivek Goyal struct inode *dir, 1735c957f6dfSVivek Goyal const struct qstr *name, u16 tclass, 1736c3c188b2SDavid Howells u32 *_new_isid) 1737c3c188b2SDavid Howells { 1738c3c188b2SDavid Howells const struct superblock_security_struct *sbsec = dir->i_sb->s_security; 1739c3c188b2SDavid Howells 1740c3c188b2SDavid Howells if ((sbsec->flags & SE_SBINITIALIZED) && 1741c3c188b2SDavid Howells (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) { 1742c3c188b2SDavid Howells *_new_isid = sbsec->mntpoint_sid; 1743c3c188b2SDavid Howells } else if ((sbsec->flags & SBLABEL_MNT) && 1744c3c188b2SDavid Howells tsec->create_sid) { 1745c3c188b2SDavid Howells *_new_isid = tsec->create_sid; 1746c3c188b2SDavid Howells } else { 174720cdef8dSPaul Moore const struct inode_security_struct *dsec = inode_security(dir); 1748aa8e712cSStephen Smalley return security_transition_sid(&selinux_state, tsec->sid, 1749aa8e712cSStephen Smalley dsec->sid, tclass, 1750c3c188b2SDavid Howells name, _new_isid); 1751c3c188b2SDavid Howells } 1752c3c188b2SDavid Howells 1753c3c188b2SDavid Howells return 0; 1754c3c188b2SDavid Howells } 1755c3c188b2SDavid Howells 17561da177e4SLinus Torvalds /* Check whether a task can create a file. */ 17571da177e4SLinus Torvalds static int may_create(struct inode *dir, 17581da177e4SLinus Torvalds struct dentry *dentry, 17591da177e4SLinus Torvalds u16 tclass) 17601da177e4SLinus Torvalds { 17610c6cfa62SCasey Schaufler const struct task_security_struct *tsec = selinux_cred(current_cred()); 17621da177e4SLinus Torvalds struct inode_security_struct *dsec; 17631da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 1764275bb41eSDavid Howells u32 sid, newsid; 17652bf49690SThomas Liu struct common_audit_data ad; 17661da177e4SLinus Torvalds int rc; 17671da177e4SLinus Torvalds 176883da53c5SAndreas Gruenbacher dsec = inode_security(dir); 17691da177e4SLinus Torvalds sbsec = dir->i_sb->s_security; 17701da177e4SLinus Torvalds 1771275bb41eSDavid Howells sid = tsec->sid; 1772275bb41eSDavid Howells 177350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 1774a269434dSEric Paris ad.u.dentry = dentry; 17751da177e4SLinus Torvalds 17766b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 17776b6bc620SStephen Smalley sid, dsec->sid, SECCLASS_DIR, 17781da177e4SLinus Torvalds DIR__ADD_NAME | DIR__SEARCH, 17791da177e4SLinus Torvalds &ad); 17801da177e4SLinus Torvalds if (rc) 17811da177e4SLinus Torvalds return rc; 17821da177e4SLinus Torvalds 1783210a2928SYang Guo rc = selinux_determine_inode_label(tsec, dir, &dentry->d_name, tclass, 1784210a2928SYang Guo &newsid); 17851da177e4SLinus Torvalds if (rc) 17861da177e4SLinus Torvalds return rc; 17871da177e4SLinus Torvalds 17886b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 17896b6bc620SStephen Smalley sid, newsid, tclass, FILE__CREATE, &ad); 17901da177e4SLinus Torvalds if (rc) 17911da177e4SLinus Torvalds return rc; 17921da177e4SLinus Torvalds 17936b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 17946b6bc620SStephen Smalley newsid, sbsec->sid, 17951da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 17961da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, &ad); 17971da177e4SLinus Torvalds } 17981da177e4SLinus Torvalds 17991da177e4SLinus Torvalds #define MAY_LINK 0 18001da177e4SLinus Torvalds #define MAY_UNLINK 1 18011da177e4SLinus Torvalds #define MAY_RMDIR 2 18021da177e4SLinus Torvalds 18031da177e4SLinus Torvalds /* Check whether a task can link, unlink, or rmdir a file/directory. */ 18041da177e4SLinus Torvalds static int may_link(struct inode *dir, 18051da177e4SLinus Torvalds struct dentry *dentry, 18061da177e4SLinus Torvalds int kind) 18071da177e4SLinus Torvalds 18081da177e4SLinus Torvalds { 18091da177e4SLinus Torvalds struct inode_security_struct *dsec, *isec; 18102bf49690SThomas Liu struct common_audit_data ad; 1811275bb41eSDavid Howells u32 sid = current_sid(); 18121da177e4SLinus Torvalds u32 av; 18131da177e4SLinus Torvalds int rc; 18141da177e4SLinus Torvalds 181583da53c5SAndreas Gruenbacher dsec = inode_security(dir); 181683da53c5SAndreas Gruenbacher isec = backing_inode_security(dentry); 18171da177e4SLinus Torvalds 181850c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 1819a269434dSEric Paris ad.u.dentry = dentry; 18201da177e4SLinus Torvalds 18211da177e4SLinus Torvalds av = DIR__SEARCH; 18221da177e4SLinus Torvalds av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME); 18236b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 18246b6bc620SStephen Smalley sid, dsec->sid, SECCLASS_DIR, av, &ad); 18251da177e4SLinus Torvalds if (rc) 18261da177e4SLinus Torvalds return rc; 18271da177e4SLinus Torvalds 18281da177e4SLinus Torvalds switch (kind) { 18291da177e4SLinus Torvalds case MAY_LINK: 18301da177e4SLinus Torvalds av = FILE__LINK; 18311da177e4SLinus Torvalds break; 18321da177e4SLinus Torvalds case MAY_UNLINK: 18331da177e4SLinus Torvalds av = FILE__UNLINK; 18341da177e4SLinus Torvalds break; 18351da177e4SLinus Torvalds case MAY_RMDIR: 18361da177e4SLinus Torvalds av = DIR__RMDIR; 18371da177e4SLinus Torvalds break; 18381da177e4SLinus Torvalds default: 1839c103a91eSpeter enderborg pr_warn("SELinux: %s: unrecognized kind %d\n", 1840744ba35eSEric Paris __func__, kind); 18411da177e4SLinus Torvalds return 0; 18421da177e4SLinus Torvalds } 18431da177e4SLinus Torvalds 18446b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 18456b6bc620SStephen Smalley sid, isec->sid, isec->sclass, av, &ad); 18461da177e4SLinus Torvalds return rc; 18471da177e4SLinus Torvalds } 18481da177e4SLinus Torvalds 18491da177e4SLinus Torvalds static inline int may_rename(struct inode *old_dir, 18501da177e4SLinus Torvalds struct dentry *old_dentry, 18511da177e4SLinus Torvalds struct inode *new_dir, 18521da177e4SLinus Torvalds struct dentry *new_dentry) 18531da177e4SLinus Torvalds { 18541da177e4SLinus Torvalds struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec; 18552bf49690SThomas Liu struct common_audit_data ad; 1856275bb41eSDavid Howells u32 sid = current_sid(); 18571da177e4SLinus Torvalds u32 av; 18581da177e4SLinus Torvalds int old_is_dir, new_is_dir; 18591da177e4SLinus Torvalds int rc; 18601da177e4SLinus Torvalds 186183da53c5SAndreas Gruenbacher old_dsec = inode_security(old_dir); 186283da53c5SAndreas Gruenbacher old_isec = backing_inode_security(old_dentry); 1863e36cb0b8SDavid Howells old_is_dir = d_is_dir(old_dentry); 186483da53c5SAndreas Gruenbacher new_dsec = inode_security(new_dir); 18651da177e4SLinus Torvalds 186650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 18671da177e4SLinus Torvalds 1868a269434dSEric Paris ad.u.dentry = old_dentry; 18696b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 18706b6bc620SStephen Smalley sid, old_dsec->sid, SECCLASS_DIR, 18711da177e4SLinus Torvalds DIR__REMOVE_NAME | DIR__SEARCH, &ad); 18721da177e4SLinus Torvalds if (rc) 18731da177e4SLinus Torvalds return rc; 18746b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 18756b6bc620SStephen Smalley sid, old_isec->sid, 18761da177e4SLinus Torvalds old_isec->sclass, FILE__RENAME, &ad); 18771da177e4SLinus Torvalds if (rc) 18781da177e4SLinus Torvalds return rc; 18791da177e4SLinus Torvalds if (old_is_dir && new_dir != old_dir) { 18806b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 18816b6bc620SStephen Smalley sid, old_isec->sid, 18821da177e4SLinus Torvalds old_isec->sclass, DIR__REPARENT, &ad); 18831da177e4SLinus Torvalds if (rc) 18841da177e4SLinus Torvalds return rc; 18851da177e4SLinus Torvalds } 18861da177e4SLinus Torvalds 1887a269434dSEric Paris ad.u.dentry = new_dentry; 18881da177e4SLinus Torvalds av = DIR__ADD_NAME | DIR__SEARCH; 18892c616d4dSDavid Howells if (d_is_positive(new_dentry)) 18901da177e4SLinus Torvalds av |= DIR__REMOVE_NAME; 18916b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 18926b6bc620SStephen Smalley sid, new_dsec->sid, SECCLASS_DIR, av, &ad); 18931da177e4SLinus Torvalds if (rc) 18941da177e4SLinus Torvalds return rc; 18952c616d4dSDavid Howells if (d_is_positive(new_dentry)) { 189683da53c5SAndreas Gruenbacher new_isec = backing_inode_security(new_dentry); 1897e36cb0b8SDavid Howells new_is_dir = d_is_dir(new_dentry); 18986b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 18996b6bc620SStephen Smalley sid, new_isec->sid, 19001da177e4SLinus Torvalds new_isec->sclass, 19011da177e4SLinus Torvalds (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad); 19021da177e4SLinus Torvalds if (rc) 19031da177e4SLinus Torvalds return rc; 19041da177e4SLinus Torvalds } 19051da177e4SLinus Torvalds 19061da177e4SLinus Torvalds return 0; 19071da177e4SLinus Torvalds } 19081da177e4SLinus Torvalds 19091da177e4SLinus Torvalds /* Check whether a task can perform a filesystem operation. */ 191088e67f3bSDavid Howells static int superblock_has_perm(const struct cred *cred, 19111da177e4SLinus Torvalds struct super_block *sb, 19121da177e4SLinus Torvalds u32 perms, 19132bf49690SThomas Liu struct common_audit_data *ad) 19141da177e4SLinus Torvalds { 19151da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 191688e67f3bSDavid Howells u32 sid = cred_sid(cred); 19171da177e4SLinus Torvalds 19181da177e4SLinus Torvalds sbsec = sb->s_security; 19196b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 19206b6bc620SStephen Smalley sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad); 19211da177e4SLinus Torvalds } 19221da177e4SLinus Torvalds 19231da177e4SLinus Torvalds /* Convert a Linux mode and permission mask to an access vector. */ 19241da177e4SLinus Torvalds static inline u32 file_mask_to_av(int mode, int mask) 19251da177e4SLinus Torvalds { 19261da177e4SLinus Torvalds u32 av = 0; 19271da177e4SLinus Torvalds 1928dba19c60SAl Viro if (!S_ISDIR(mode)) { 19291da177e4SLinus Torvalds if (mask & MAY_EXEC) 19301da177e4SLinus Torvalds av |= FILE__EXECUTE; 19311da177e4SLinus Torvalds if (mask & MAY_READ) 19321da177e4SLinus Torvalds av |= FILE__READ; 19331da177e4SLinus Torvalds 19341da177e4SLinus Torvalds if (mask & MAY_APPEND) 19351da177e4SLinus Torvalds av |= FILE__APPEND; 19361da177e4SLinus Torvalds else if (mask & MAY_WRITE) 19371da177e4SLinus Torvalds av |= FILE__WRITE; 19381da177e4SLinus Torvalds 19391da177e4SLinus Torvalds } else { 19401da177e4SLinus Torvalds if (mask & MAY_EXEC) 19411da177e4SLinus Torvalds av |= DIR__SEARCH; 19421da177e4SLinus Torvalds if (mask & MAY_WRITE) 19431da177e4SLinus Torvalds av |= DIR__WRITE; 19441da177e4SLinus Torvalds if (mask & MAY_READ) 19451da177e4SLinus Torvalds av |= DIR__READ; 19461da177e4SLinus Torvalds } 19471da177e4SLinus Torvalds 19481da177e4SLinus Torvalds return av; 19491da177e4SLinus Torvalds } 19501da177e4SLinus Torvalds 19511da177e4SLinus Torvalds /* Convert a Linux file to an access vector. */ 19521da177e4SLinus Torvalds static inline u32 file_to_av(struct file *file) 19531da177e4SLinus Torvalds { 19541da177e4SLinus Torvalds u32 av = 0; 19551da177e4SLinus Torvalds 19561da177e4SLinus Torvalds if (file->f_mode & FMODE_READ) 19571da177e4SLinus Torvalds av |= FILE__READ; 19581da177e4SLinus Torvalds if (file->f_mode & FMODE_WRITE) { 19591da177e4SLinus Torvalds if (file->f_flags & O_APPEND) 19601da177e4SLinus Torvalds av |= FILE__APPEND; 19611da177e4SLinus Torvalds else 19621da177e4SLinus Torvalds av |= FILE__WRITE; 19631da177e4SLinus Torvalds } 19640794c66dSStephen Smalley if (!av) { 19650794c66dSStephen Smalley /* 19660794c66dSStephen Smalley * Special file opened with flags 3 for ioctl-only use. 19670794c66dSStephen Smalley */ 19680794c66dSStephen Smalley av = FILE__IOCTL; 19690794c66dSStephen Smalley } 19701da177e4SLinus Torvalds 19711da177e4SLinus Torvalds return av; 19721da177e4SLinus Torvalds } 19731da177e4SLinus Torvalds 19748b6a5a37SEric Paris /* 19758b6a5a37SEric Paris * Convert a file to an access vector and include the correct open 19768b6a5a37SEric Paris * open permission. 19778b6a5a37SEric Paris */ 19788b6a5a37SEric Paris static inline u32 open_file_to_av(struct file *file) 19798b6a5a37SEric Paris { 19808b6a5a37SEric Paris u32 av = file_to_av(file); 1981ccb54478SStephen Smalley struct inode *inode = file_inode(file); 19828b6a5a37SEric Paris 1983aa8e712cSStephen Smalley if (selinux_policycap_openperm() && 1984aa8e712cSStephen Smalley inode->i_sb->s_magic != SOCKFS_MAGIC) 19858b6a5a37SEric Paris av |= FILE__OPEN; 198649b7b8deSEric Paris 19878b6a5a37SEric Paris return av; 19888b6a5a37SEric Paris } 19898b6a5a37SEric Paris 19901da177e4SLinus Torvalds /* Hook functions begin here. */ 19911da177e4SLinus Torvalds 199279af7307SStephen Smalley static int selinux_binder_set_context_mgr(struct task_struct *mgr) 199379af7307SStephen Smalley { 199479af7307SStephen Smalley u32 mysid = current_sid(); 199579af7307SStephen Smalley u32 mgrsid = task_sid(mgr); 199679af7307SStephen Smalley 19976b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 19986b6bc620SStephen Smalley mysid, mgrsid, SECCLASS_BINDER, 199979af7307SStephen Smalley BINDER__SET_CONTEXT_MGR, NULL); 200079af7307SStephen Smalley } 200179af7307SStephen Smalley 200279af7307SStephen Smalley static int selinux_binder_transaction(struct task_struct *from, 200379af7307SStephen Smalley struct task_struct *to) 200479af7307SStephen Smalley { 200579af7307SStephen Smalley u32 mysid = current_sid(); 200679af7307SStephen Smalley u32 fromsid = task_sid(from); 200779af7307SStephen Smalley u32 tosid = task_sid(to); 200879af7307SStephen Smalley int rc; 200979af7307SStephen Smalley 201079af7307SStephen Smalley if (mysid != fromsid) { 20116b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 20126b6bc620SStephen Smalley mysid, fromsid, SECCLASS_BINDER, 201379af7307SStephen Smalley BINDER__IMPERSONATE, NULL); 201479af7307SStephen Smalley if (rc) 201579af7307SStephen Smalley return rc; 201679af7307SStephen Smalley } 201779af7307SStephen Smalley 20186b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 20196b6bc620SStephen Smalley fromsid, tosid, SECCLASS_BINDER, BINDER__CALL, 202079af7307SStephen Smalley NULL); 202179af7307SStephen Smalley } 202279af7307SStephen Smalley 202379af7307SStephen Smalley static int selinux_binder_transfer_binder(struct task_struct *from, 202479af7307SStephen Smalley struct task_struct *to) 202579af7307SStephen Smalley { 202679af7307SStephen Smalley u32 fromsid = task_sid(from); 202779af7307SStephen Smalley u32 tosid = task_sid(to); 202879af7307SStephen Smalley 20296b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 20306b6bc620SStephen Smalley fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER, 203179af7307SStephen Smalley NULL); 203279af7307SStephen Smalley } 203379af7307SStephen Smalley 203479af7307SStephen Smalley static int selinux_binder_transfer_file(struct task_struct *from, 203579af7307SStephen Smalley struct task_struct *to, 203679af7307SStephen Smalley struct file *file) 203779af7307SStephen Smalley { 203879af7307SStephen Smalley u32 sid = task_sid(to); 2039bb6c6b02SCasey Schaufler struct file_security_struct *fsec = selinux_file(file); 204083da53c5SAndreas Gruenbacher struct dentry *dentry = file->f_path.dentry; 204120cdef8dSPaul Moore struct inode_security_struct *isec; 204279af7307SStephen Smalley struct common_audit_data ad; 204379af7307SStephen Smalley int rc; 204479af7307SStephen Smalley 204579af7307SStephen Smalley ad.type = LSM_AUDIT_DATA_PATH; 204679af7307SStephen Smalley ad.u.path = file->f_path; 204779af7307SStephen Smalley 204879af7307SStephen Smalley if (sid != fsec->sid) { 20496b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 20506b6bc620SStephen Smalley sid, fsec->sid, 205179af7307SStephen Smalley SECCLASS_FD, 205279af7307SStephen Smalley FD__USE, 205379af7307SStephen Smalley &ad); 205479af7307SStephen Smalley if (rc) 205579af7307SStephen Smalley return rc; 205679af7307SStephen Smalley } 205779af7307SStephen Smalley 2058f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL 2059f66e448cSChenbo Feng rc = bpf_fd_pass(file, sid); 2060f66e448cSChenbo Feng if (rc) 2061f66e448cSChenbo Feng return rc; 2062f66e448cSChenbo Feng #endif 2063f66e448cSChenbo Feng 206483da53c5SAndreas Gruenbacher if (unlikely(IS_PRIVATE(d_backing_inode(dentry)))) 206579af7307SStephen Smalley return 0; 206679af7307SStephen Smalley 206720cdef8dSPaul Moore isec = backing_inode_security(dentry); 20686b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 20696b6bc620SStephen Smalley sid, isec->sid, isec->sclass, file_to_av(file), 207079af7307SStephen Smalley &ad); 207179af7307SStephen Smalley } 207279af7307SStephen Smalley 20739e48858fSIngo Molnar static int selinux_ptrace_access_check(struct task_struct *child, 2074006ebb40SStephen Smalley unsigned int mode) 20751da177e4SLinus Torvalds { 2076275bb41eSDavid Howells u32 sid = current_sid(); 2077275bb41eSDavid Howells u32 csid = task_sid(child); 2078006ebb40SStephen Smalley 2079be0554c9SStephen Smalley if (mode & PTRACE_MODE_READ) 20806b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 20816b6bc620SStephen Smalley sid, csid, SECCLASS_FILE, FILE__READ, NULL); 2082be0554c9SStephen Smalley 20836b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 20846b6bc620SStephen Smalley sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL); 20855cd9c58fSDavid Howells } 20865cd9c58fSDavid Howells 20875cd9c58fSDavid Howells static int selinux_ptrace_traceme(struct task_struct *parent) 20885cd9c58fSDavid Howells { 20896b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 20906b6bc620SStephen Smalley task_sid(parent), current_sid(), SECCLASS_PROCESS, 2091be0554c9SStephen Smalley PROCESS__PTRACE, NULL); 20921da177e4SLinus Torvalds } 20931da177e4SLinus Torvalds 20941da177e4SLinus Torvalds static int selinux_capget(struct task_struct *target, kernel_cap_t *effective, 20951da177e4SLinus Torvalds kernel_cap_t *inheritable, kernel_cap_t *permitted) 20961da177e4SLinus Torvalds { 20976b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 20986b6bc620SStephen Smalley current_sid(), task_sid(target), SECCLASS_PROCESS, 2099be0554c9SStephen Smalley PROCESS__GETCAP, NULL); 21001da177e4SLinus Torvalds } 21011da177e4SLinus Torvalds 2102d84f4f99SDavid Howells static int selinux_capset(struct cred *new, const struct cred *old, 2103d84f4f99SDavid Howells const kernel_cap_t *effective, 210415a2460eSDavid Howells const kernel_cap_t *inheritable, 210515a2460eSDavid Howells const kernel_cap_t *permitted) 21061da177e4SLinus Torvalds { 21076b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 21086b6bc620SStephen Smalley cred_sid(old), cred_sid(new), SECCLASS_PROCESS, 2109be0554c9SStephen Smalley PROCESS__SETCAP, NULL); 21101da177e4SLinus Torvalds } 21111da177e4SLinus Torvalds 21125626d3e8SJames Morris /* 21135626d3e8SJames Morris * (This comment used to live with the selinux_task_setuid hook, 21145626d3e8SJames Morris * which was removed). 21155626d3e8SJames Morris * 21165626d3e8SJames Morris * Since setuid only affects the current process, and since the SELinux 21175626d3e8SJames Morris * controls are not based on the Linux identity attributes, SELinux does not 21185626d3e8SJames Morris * need to control this operation. However, SELinux does control the use of 21195626d3e8SJames Morris * the CAP_SETUID and CAP_SETGID capabilities using the capable hook. 21205626d3e8SJames Morris */ 21215626d3e8SJames Morris 21226a9de491SEric Paris static int selinux_capable(const struct cred *cred, struct user_namespace *ns, 2123c1a85a00SMicah Morton int cap, unsigned int opts) 21241da177e4SLinus Torvalds { 2125c1a85a00SMicah Morton return cred_has_capability(cred, cap, opts, ns == &init_user_ns); 21261da177e4SLinus Torvalds } 21271da177e4SLinus Torvalds 21281da177e4SLinus Torvalds static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb) 21291da177e4SLinus Torvalds { 213088e67f3bSDavid Howells const struct cred *cred = current_cred(); 21311da177e4SLinus Torvalds int rc = 0; 21321da177e4SLinus Torvalds 21331da177e4SLinus Torvalds if (!sb) 21341da177e4SLinus Torvalds return 0; 21351da177e4SLinus Torvalds 21361da177e4SLinus Torvalds switch (cmds) { 21371da177e4SLinus Torvalds case Q_SYNC: 21381da177e4SLinus Torvalds case Q_QUOTAON: 21391da177e4SLinus Torvalds case Q_QUOTAOFF: 21401da177e4SLinus Torvalds case Q_SETINFO: 21411da177e4SLinus Torvalds case Q_SETQUOTA: 214288e67f3bSDavid Howells rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL); 21431da177e4SLinus Torvalds break; 21441da177e4SLinus Torvalds case Q_GETFMT: 21451da177e4SLinus Torvalds case Q_GETINFO: 21461da177e4SLinus Torvalds case Q_GETQUOTA: 214788e67f3bSDavid Howells rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL); 21481da177e4SLinus Torvalds break; 21491da177e4SLinus Torvalds default: 21501da177e4SLinus Torvalds rc = 0; /* let the kernel handle invalid cmds */ 21511da177e4SLinus Torvalds break; 21521da177e4SLinus Torvalds } 21531da177e4SLinus Torvalds return rc; 21541da177e4SLinus Torvalds } 21551da177e4SLinus Torvalds 21561da177e4SLinus Torvalds static int selinux_quota_on(struct dentry *dentry) 21571da177e4SLinus Torvalds { 215888e67f3bSDavid Howells const struct cred *cred = current_cred(); 215988e67f3bSDavid Howells 21602875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__QUOTAON); 21611da177e4SLinus Torvalds } 21621da177e4SLinus Torvalds 216312b3052cSEric Paris static int selinux_syslog(int type) 21641da177e4SLinus Torvalds { 21651da177e4SLinus Torvalds switch (type) { 2166d78ca3cdSKees Cook case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */ 2167d78ca3cdSKees Cook case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */ 21686b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 21696b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, 2170be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL); 2171d78ca3cdSKees Cook case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */ 2172d78ca3cdSKees Cook case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */ 2173d78ca3cdSKees Cook /* Set level of messages printed to console */ 2174d78ca3cdSKees Cook case SYSLOG_ACTION_CONSOLE_LEVEL: 21756b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 21766b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, 2177be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE, 2178be0554c9SStephen Smalley NULL); 21791da177e4SLinus Torvalds } 2180be0554c9SStephen Smalley /* All other syslog types */ 21816b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 21826b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, 2183be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL); 21841da177e4SLinus Torvalds } 21851da177e4SLinus Torvalds 21861da177e4SLinus Torvalds /* 21871da177e4SLinus Torvalds * Check that a process has enough memory to allocate a new virtual 21881da177e4SLinus Torvalds * mapping. 0 means there is enough memory for the allocation to 21891da177e4SLinus Torvalds * succeed and -ENOMEM implies there is not. 21901da177e4SLinus Torvalds * 21911da177e4SLinus Torvalds * Do not audit the selinux permission check, as this is applied to all 21921da177e4SLinus Torvalds * processes that allocate mappings. 21931da177e4SLinus Torvalds */ 219434b4e4aaSAlan Cox static int selinux_vm_enough_memory(struct mm_struct *mm, long pages) 21951da177e4SLinus Torvalds { 21961da177e4SLinus Torvalds int rc, cap_sys_admin = 0; 21971da177e4SLinus Torvalds 2198b1d9e6b0SCasey Schaufler rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN, 2199c1a85a00SMicah Morton CAP_OPT_NOAUDIT, true); 22001da177e4SLinus Torvalds if (rc == 0) 22011da177e4SLinus Torvalds cap_sys_admin = 1; 22021da177e4SLinus Torvalds 2203b1d9e6b0SCasey Schaufler return cap_sys_admin; 22041da177e4SLinus Torvalds } 22051da177e4SLinus Torvalds 22061da177e4SLinus Torvalds /* binprm security operations */ 22071da177e4SLinus Torvalds 2208be0554c9SStephen Smalley static u32 ptrace_parent_sid(void) 22090c6181cbSPaul Moore { 22100c6181cbSPaul Moore u32 sid = 0; 22110c6181cbSPaul Moore struct task_struct *tracer; 22120c6181cbSPaul Moore 22130c6181cbSPaul Moore rcu_read_lock(); 2214be0554c9SStephen Smalley tracer = ptrace_parent(current); 22150c6181cbSPaul Moore if (tracer) 22160c6181cbSPaul Moore sid = task_sid(tracer); 22170c6181cbSPaul Moore rcu_read_unlock(); 22180c6181cbSPaul Moore 22190c6181cbSPaul Moore return sid; 22200c6181cbSPaul Moore } 22210c6181cbSPaul Moore 22227b0d0b40SStephen Smalley static int check_nnp_nosuid(const struct linux_binprm *bprm, 22237b0d0b40SStephen Smalley const struct task_security_struct *old_tsec, 22247b0d0b40SStephen Smalley const struct task_security_struct *new_tsec) 22257b0d0b40SStephen Smalley { 22267b0d0b40SStephen Smalley int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS); 2227380cf5baSAndy Lutomirski int nosuid = !mnt_may_suid(bprm->file->f_path.mnt); 22287b0d0b40SStephen Smalley int rc; 2229af63f419SStephen Smalley u32 av; 22307b0d0b40SStephen Smalley 22317b0d0b40SStephen Smalley if (!nnp && !nosuid) 22327b0d0b40SStephen Smalley return 0; /* neither NNP nor nosuid */ 22337b0d0b40SStephen Smalley 22347b0d0b40SStephen Smalley if (new_tsec->sid == old_tsec->sid) 22357b0d0b40SStephen Smalley return 0; /* No change in credentials */ 22367b0d0b40SStephen Smalley 22377b0d0b40SStephen Smalley /* 2238af63f419SStephen Smalley * If the policy enables the nnp_nosuid_transition policy capability, 2239af63f419SStephen Smalley * then we permit transitions under NNP or nosuid if the 2240af63f419SStephen Smalley * policy allows the corresponding permission between 2241af63f419SStephen Smalley * the old and new contexts. 2242af63f419SStephen Smalley */ 2243aa8e712cSStephen Smalley if (selinux_policycap_nnp_nosuid_transition()) { 2244af63f419SStephen Smalley av = 0; 2245af63f419SStephen Smalley if (nnp) 2246af63f419SStephen Smalley av |= PROCESS2__NNP_TRANSITION; 2247af63f419SStephen Smalley if (nosuid) 2248af63f419SStephen Smalley av |= PROCESS2__NOSUID_TRANSITION; 22496b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 22506b6bc620SStephen Smalley old_tsec->sid, new_tsec->sid, 2251af63f419SStephen Smalley SECCLASS_PROCESS2, av, NULL); 2252af63f419SStephen Smalley if (!rc) 2253af63f419SStephen Smalley return 0; 2254af63f419SStephen Smalley } 2255af63f419SStephen Smalley 2256af63f419SStephen Smalley /* 2257af63f419SStephen Smalley * We also permit NNP or nosuid transitions to bounded SIDs, 2258af63f419SStephen Smalley * i.e. SIDs that are guaranteed to only be allowed a subset 2259af63f419SStephen Smalley * of the permissions of the current SID. 22607b0d0b40SStephen Smalley */ 2261aa8e712cSStephen Smalley rc = security_bounded_transition(&selinux_state, old_tsec->sid, 2262aa8e712cSStephen Smalley new_tsec->sid); 2263af63f419SStephen Smalley if (!rc) 2264af63f419SStephen Smalley return 0; 2265af63f419SStephen Smalley 22667b0d0b40SStephen Smalley /* 22677b0d0b40SStephen Smalley * On failure, preserve the errno values for NNP vs nosuid. 22687b0d0b40SStephen Smalley * NNP: Operation not permitted for caller. 22697b0d0b40SStephen Smalley * nosuid: Permission denied to file. 22707b0d0b40SStephen Smalley */ 22717b0d0b40SStephen Smalley if (nnp) 22727b0d0b40SStephen Smalley return -EPERM; 22737b0d0b40SStephen Smalley return -EACCES; 22747b0d0b40SStephen Smalley } 22757b0d0b40SStephen Smalley 2276a6f76f23SDavid Howells static int selinux_bprm_set_creds(struct linux_binprm *bprm) 22771da177e4SLinus Torvalds { 2278a6f76f23SDavid Howells const struct task_security_struct *old_tsec; 2279a6f76f23SDavid Howells struct task_security_struct *new_tsec; 22801da177e4SLinus Torvalds struct inode_security_struct *isec; 22812bf49690SThomas Liu struct common_audit_data ad; 2282496ad9aaSAl Viro struct inode *inode = file_inode(bprm->file); 22831da177e4SLinus Torvalds int rc; 22841da177e4SLinus Torvalds 2285a6f76f23SDavid Howells /* SELinux context only depends on initial program or script and not 2286a6f76f23SDavid Howells * the script interpreter */ 2287ddb4a144SKees Cook if (bprm->called_set_creds) 22881da177e4SLinus Torvalds return 0; 22891da177e4SLinus Torvalds 22900c6cfa62SCasey Schaufler old_tsec = selinux_cred(current_cred()); 22910c6cfa62SCasey Schaufler new_tsec = selinux_cred(bprm->cred); 229283da53c5SAndreas Gruenbacher isec = inode_security(inode); 22931da177e4SLinus Torvalds 22941da177e4SLinus Torvalds /* Default to the current task SID. */ 2295a6f76f23SDavid Howells new_tsec->sid = old_tsec->sid; 2296a6f76f23SDavid Howells new_tsec->osid = old_tsec->sid; 22971da177e4SLinus Torvalds 229828eba5bfSMichael LeMay /* Reset fs, key, and sock SIDs on execve. */ 2299a6f76f23SDavid Howells new_tsec->create_sid = 0; 2300a6f76f23SDavid Howells new_tsec->keycreate_sid = 0; 2301a6f76f23SDavid Howells new_tsec->sockcreate_sid = 0; 23021da177e4SLinus Torvalds 2303a6f76f23SDavid Howells if (old_tsec->exec_sid) { 2304a6f76f23SDavid Howells new_tsec->sid = old_tsec->exec_sid; 23051da177e4SLinus Torvalds /* Reset exec SID on execve. */ 2306a6f76f23SDavid Howells new_tsec->exec_sid = 0; 2307259e5e6cSAndy Lutomirski 23087b0d0b40SStephen Smalley /* Fail on NNP or nosuid if not an allowed transition. */ 23097b0d0b40SStephen Smalley rc = check_nnp_nosuid(bprm, old_tsec, new_tsec); 23107b0d0b40SStephen Smalley if (rc) 23117b0d0b40SStephen Smalley return rc; 23121da177e4SLinus Torvalds } else { 23131da177e4SLinus Torvalds /* Check for a default transition on this program. */ 2314aa8e712cSStephen Smalley rc = security_transition_sid(&selinux_state, old_tsec->sid, 2315aa8e712cSStephen Smalley isec->sid, SECCLASS_PROCESS, NULL, 2316652bb9b0SEric Paris &new_tsec->sid); 23171da177e4SLinus Torvalds if (rc) 23181da177e4SLinus Torvalds return rc; 23197b0d0b40SStephen Smalley 23207b0d0b40SStephen Smalley /* 23217b0d0b40SStephen Smalley * Fallback to old SID on NNP or nosuid if not an allowed 23227b0d0b40SStephen Smalley * transition. 23237b0d0b40SStephen Smalley */ 23247b0d0b40SStephen Smalley rc = check_nnp_nosuid(bprm, old_tsec, new_tsec); 23257b0d0b40SStephen Smalley if (rc) 23267b0d0b40SStephen Smalley new_tsec->sid = old_tsec->sid; 23271da177e4SLinus Torvalds } 23281da177e4SLinus Torvalds 232943af5de7SVivek Goyal ad.type = LSM_AUDIT_DATA_FILE; 233043af5de7SVivek Goyal ad.u.file = bprm->file; 23311da177e4SLinus Torvalds 2332a6f76f23SDavid Howells if (new_tsec->sid == old_tsec->sid) { 23336b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 23346b6bc620SStephen Smalley old_tsec->sid, isec->sid, 23351da177e4SLinus Torvalds SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad); 23361da177e4SLinus Torvalds if (rc) 23371da177e4SLinus Torvalds return rc; 23381da177e4SLinus Torvalds } else { 23391da177e4SLinus Torvalds /* Check permissions for the transition. */ 23406b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 23416b6bc620SStephen Smalley old_tsec->sid, new_tsec->sid, 23421da177e4SLinus Torvalds SECCLASS_PROCESS, PROCESS__TRANSITION, &ad); 23431da177e4SLinus Torvalds if (rc) 23441da177e4SLinus Torvalds return rc; 23451da177e4SLinus Torvalds 23466b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 23476b6bc620SStephen Smalley new_tsec->sid, isec->sid, 23481da177e4SLinus Torvalds SECCLASS_FILE, FILE__ENTRYPOINT, &ad); 23491da177e4SLinus Torvalds if (rc) 23501da177e4SLinus Torvalds return rc; 23511da177e4SLinus Torvalds 2352a6f76f23SDavid Howells /* Check for shared state */ 2353a6f76f23SDavid Howells if (bprm->unsafe & LSM_UNSAFE_SHARE) { 23546b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 23556b6bc620SStephen Smalley old_tsec->sid, new_tsec->sid, 2356a6f76f23SDavid Howells SECCLASS_PROCESS, PROCESS__SHARE, 2357a6f76f23SDavid Howells NULL); 2358a6f76f23SDavid Howells if (rc) 2359a6f76f23SDavid Howells return -EPERM; 23601da177e4SLinus Torvalds } 23611da177e4SLinus Torvalds 2362a6f76f23SDavid Howells /* Make sure that anyone attempting to ptrace over a task that 2363a6f76f23SDavid Howells * changes its SID has the appropriate permit */ 23649227dd2aSEric W. Biederman if (bprm->unsafe & LSM_UNSAFE_PTRACE) { 2365be0554c9SStephen Smalley u32 ptsid = ptrace_parent_sid(); 2366a6f76f23SDavid Howells if (ptsid != 0) { 23676b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 23686b6bc620SStephen Smalley ptsid, new_tsec->sid, 2369a6f76f23SDavid Howells SECCLASS_PROCESS, 2370a6f76f23SDavid Howells PROCESS__PTRACE, NULL); 2371a6f76f23SDavid Howells if (rc) 2372a6f76f23SDavid Howells return -EPERM; 2373a6f76f23SDavid Howells } 2374a6f76f23SDavid Howells } 2375a6f76f23SDavid Howells 2376a6f76f23SDavid Howells /* Clear any possibly unsafe personality bits on exec: */ 2377a6f76f23SDavid Howells bprm->per_clear |= PER_CLEAR_ON_SETID; 2378a6f76f23SDavid Howells 23791da177e4SLinus Torvalds /* Enable secure mode for SIDs transitions unless 23801da177e4SLinus Torvalds the noatsecure permission is granted between 23811da177e4SLinus Torvalds the two SIDs, i.e. ahp returns 0. */ 23826b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 23836b6bc620SStephen Smalley old_tsec->sid, new_tsec->sid, 238462874c3aSKees Cook SECCLASS_PROCESS, PROCESS__NOATSECURE, 238562874c3aSKees Cook NULL); 238662874c3aSKees Cook bprm->secureexec |= !!rc; 23871da177e4SLinus Torvalds } 23881da177e4SLinus Torvalds 238962874c3aSKees Cook return 0; 23901da177e4SLinus Torvalds } 23911da177e4SLinus Torvalds 2392c3c073f8SAl Viro static int match_file(const void *p, struct file *file, unsigned fd) 2393c3c073f8SAl Viro { 2394c3c073f8SAl Viro return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0; 2395c3c073f8SAl Viro } 2396c3c073f8SAl Viro 23971da177e4SLinus Torvalds /* Derived from fs/exec.c:flush_old_files. */ 2398745ca247SDavid Howells static inline void flush_unauthorized_files(const struct cred *cred, 2399745ca247SDavid Howells struct files_struct *files) 24001da177e4SLinus Torvalds { 24011da177e4SLinus Torvalds struct file *file, *devnull = NULL; 2402b20c8122SStephen Smalley struct tty_struct *tty; 240324ec839cSPeter Zijlstra int drop_tty = 0; 2404c3c073f8SAl Viro unsigned n; 24051da177e4SLinus Torvalds 240624ec839cSPeter Zijlstra tty = get_current_tty(); 24071da177e4SLinus Torvalds if (tty) { 24084a510969SPeter Hurley spin_lock(&tty->files_lock); 240937dd0bd0SEric Paris if (!list_empty(&tty->tty_files)) { 2410d996b62aSNick Piggin struct tty_file_private *file_priv; 241137dd0bd0SEric Paris 24121da177e4SLinus Torvalds /* Revalidate access to controlling tty. 241313f8e981SDavid Howells Use file_path_has_perm on the tty path directly 241413f8e981SDavid Howells rather than using file_has_perm, as this particular 241513f8e981SDavid Howells open file may belong to another process and we are 241613f8e981SDavid Howells only interested in the inode-based check here. */ 2417d996b62aSNick Piggin file_priv = list_first_entry(&tty->tty_files, 2418d996b62aSNick Piggin struct tty_file_private, list); 2419d996b62aSNick Piggin file = file_priv->file; 242013f8e981SDavid Howells if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE)) 242124ec839cSPeter Zijlstra drop_tty = 1; 24221da177e4SLinus Torvalds } 24234a510969SPeter Hurley spin_unlock(&tty->files_lock); 2424452a00d2SAlan Cox tty_kref_put(tty); 24251da177e4SLinus Torvalds } 242698a27ba4SEric W. Biederman /* Reset controlling tty. */ 242798a27ba4SEric W. Biederman if (drop_tty) 242898a27ba4SEric W. Biederman no_tty(); 24291da177e4SLinus Torvalds 24301da177e4SLinus Torvalds /* Revalidate access to inherited open files. */ 2431c3c073f8SAl Viro n = iterate_fd(files, 0, match_file, cred); 2432c3c073f8SAl Viro if (!n) /* none found? */ 2433c3c073f8SAl Viro return; 24341da177e4SLinus Torvalds 2435c3c073f8SAl Viro devnull = dentry_open(&selinux_null, O_RDWR, cred); 243645525b26SAl Viro if (IS_ERR(devnull)) 243745525b26SAl Viro devnull = NULL; 2438c3c073f8SAl Viro /* replace all the matching ones with this */ 2439c3c073f8SAl Viro do { 244045525b26SAl Viro replace_fd(n - 1, devnull, 0); 2441c3c073f8SAl Viro } while ((n = iterate_fd(files, n, match_file, cred)) != 0); 244245525b26SAl Viro if (devnull) 2443c3c073f8SAl Viro fput(devnull); 24441da177e4SLinus Torvalds } 24451da177e4SLinus Torvalds 24461da177e4SLinus Torvalds /* 2447a6f76f23SDavid Howells * Prepare a process for imminent new credential changes due to exec 24481da177e4SLinus Torvalds */ 2449a6f76f23SDavid Howells static void selinux_bprm_committing_creds(struct linux_binprm *bprm) 24501da177e4SLinus Torvalds { 2451a6f76f23SDavid Howells struct task_security_struct *new_tsec; 24521da177e4SLinus Torvalds struct rlimit *rlim, *initrlim; 24531da177e4SLinus Torvalds int rc, i; 24541da177e4SLinus Torvalds 24550c6cfa62SCasey Schaufler new_tsec = selinux_cred(bprm->cred); 2456a6f76f23SDavid Howells if (new_tsec->sid == new_tsec->osid) 24571da177e4SLinus Torvalds return; 24581da177e4SLinus Torvalds 24591da177e4SLinus Torvalds /* Close files for which the new task SID is not authorized. */ 2460a6f76f23SDavid Howells flush_unauthorized_files(bprm->cred, current->files); 24611da177e4SLinus Torvalds 2462a6f76f23SDavid Howells /* Always clear parent death signal on SID transitions. */ 2463a6f76f23SDavid Howells current->pdeath_signal = 0; 2464a6f76f23SDavid Howells 2465a6f76f23SDavid Howells /* Check whether the new SID can inherit resource limits from the old 2466a6f76f23SDavid Howells * SID. If not, reset all soft limits to the lower of the current 2467a6f76f23SDavid Howells * task's hard limit and the init task's soft limit. 2468a6f76f23SDavid Howells * 2469a6f76f23SDavid Howells * Note that the setting of hard limits (even to lower them) can be 2470a6f76f23SDavid Howells * controlled by the setrlimit check. The inclusion of the init task's 2471a6f76f23SDavid Howells * soft limit into the computation is to avoid resetting soft limits 2472a6f76f23SDavid Howells * higher than the default soft limit for cases where the default is 2473a6f76f23SDavid Howells * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK. 2474a6f76f23SDavid Howells */ 24756b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 24766b6bc620SStephen Smalley new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS, 2477a6f76f23SDavid Howells PROCESS__RLIMITINH, NULL); 2478a6f76f23SDavid Howells if (rc) { 2479eb2d55a3SOleg Nesterov /* protect against do_prlimit() */ 2480eb2d55a3SOleg Nesterov task_lock(current); 2481a6f76f23SDavid Howells for (i = 0; i < RLIM_NLIMITS; i++) { 2482a6f76f23SDavid Howells rlim = current->signal->rlim + i; 2483a6f76f23SDavid Howells initrlim = init_task.signal->rlim + i; 2484a6f76f23SDavid Howells rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur); 2485a6f76f23SDavid Howells } 2486eb2d55a3SOleg Nesterov task_unlock(current); 2487baa73d9eSNicolas Pitre if (IS_ENABLED(CONFIG_POSIX_TIMERS)) 2488eb2d55a3SOleg Nesterov update_rlimit_cpu(current, rlimit(RLIMIT_CPU)); 2489a6f76f23SDavid Howells } 2490a6f76f23SDavid Howells } 2491a6f76f23SDavid Howells 2492a6f76f23SDavid Howells /* 2493a6f76f23SDavid Howells * Clean up the process immediately after the installation of new credentials 2494a6f76f23SDavid Howells * due to exec 2495a6f76f23SDavid Howells */ 2496a6f76f23SDavid Howells static void selinux_bprm_committed_creds(struct linux_binprm *bprm) 2497a6f76f23SDavid Howells { 24980c6cfa62SCasey Schaufler const struct task_security_struct *tsec = selinux_cred(current_cred()); 2499a6f76f23SDavid Howells u32 osid, sid; 2500ddbc7d06SArnd Bergmann int rc; 2501a6f76f23SDavid Howells 2502a6f76f23SDavid Howells osid = tsec->osid; 2503a6f76f23SDavid Howells sid = tsec->sid; 2504a6f76f23SDavid Howells 2505a6f76f23SDavid Howells if (sid == osid) 2506a6f76f23SDavid Howells return; 2507a6f76f23SDavid Howells 2508a6f76f23SDavid Howells /* Check whether the new SID can inherit signal state from the old SID. 2509a6f76f23SDavid Howells * If not, clear itimers to avoid subsequent signal generation and 2510a6f76f23SDavid Howells * flush and unblock signals. 2511a6f76f23SDavid Howells * 2512a6f76f23SDavid Howells * This must occur _after_ the task SID has been updated so that any 2513a6f76f23SDavid Howells * kill done after the flush will be checked against the new SID. 2514a6f76f23SDavid Howells */ 25156b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 25166b6bc620SStephen Smalley osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL); 25171da177e4SLinus Torvalds if (rc) { 2518ddbc7d06SArnd Bergmann clear_itimer(); 2519ddbc7d06SArnd Bergmann 25201da177e4SLinus Torvalds spin_lock_irq(¤t->sighand->siglock); 25219e7c8f8cSOleg Nesterov if (!fatal_signal_pending(current)) { 25229e7c8f8cSOleg Nesterov flush_sigqueue(¤t->pending); 25239e7c8f8cSOleg Nesterov flush_sigqueue(¤t->signal->shared_pending); 25241da177e4SLinus Torvalds flush_signal_handlers(current, 1); 25251da177e4SLinus Torvalds sigemptyset(¤t->blocked); 25269e7c8f8cSOleg Nesterov recalc_sigpending(); 25273bcac026SDavid Howells } 25281da177e4SLinus Torvalds spin_unlock_irq(¤t->sighand->siglock); 25291da177e4SLinus Torvalds } 25301da177e4SLinus Torvalds 2531a6f76f23SDavid Howells /* Wake up the parent if it is waiting so that it can recheck 2532a6f76f23SDavid Howells * wait permission to the new task SID. */ 2533ecd6de3cSOleg Nesterov read_lock(&tasklist_lock); 25340b7570e7SOleg Nesterov __wake_up_parent(current, current->real_parent); 2535ecd6de3cSOleg Nesterov read_unlock(&tasklist_lock); 25361da177e4SLinus Torvalds } 25371da177e4SLinus Torvalds 25381da177e4SLinus Torvalds /* superblock security operations */ 25391da177e4SLinus Torvalds 25401da177e4SLinus Torvalds static int selinux_sb_alloc_security(struct super_block *sb) 25411da177e4SLinus Torvalds { 2542cb89e246SPaul Moore struct superblock_security_struct *sbsec; 2543cb89e246SPaul Moore 2544cb89e246SPaul Moore sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL); 2545cb89e246SPaul Moore if (!sbsec) 2546cb89e246SPaul Moore return -ENOMEM; 2547cb89e246SPaul Moore 2548cb89e246SPaul Moore mutex_init(&sbsec->lock); 2549cb89e246SPaul Moore INIT_LIST_HEAD(&sbsec->isec_head); 2550cb89e246SPaul Moore spin_lock_init(&sbsec->isec_lock); 2551cb89e246SPaul Moore sbsec->sb = sb; 2552cb89e246SPaul Moore sbsec->sid = SECINITSID_UNLABELED; 2553cb89e246SPaul Moore sbsec->def_sid = SECINITSID_FILE; 2554cb89e246SPaul Moore sbsec->mntpoint_sid = SECINITSID_UNLABELED; 2555cb89e246SPaul Moore sb->s_security = sbsec; 2556cb89e246SPaul Moore 2557cb89e246SPaul Moore return 0; 25581da177e4SLinus Torvalds } 25591da177e4SLinus Torvalds 25601da177e4SLinus Torvalds static void selinux_sb_free_security(struct super_block *sb) 25611da177e4SLinus Torvalds { 25621da177e4SLinus Torvalds superblock_free_security(sb); 25631da177e4SLinus Torvalds } 25641da177e4SLinus Torvalds 256599dbbb59SAl Viro static inline int opt_len(const char *s) 25661da177e4SLinus Torvalds { 256799dbbb59SAl Viro bool open_quote = false; 256899dbbb59SAl Viro int len; 256999dbbb59SAl Viro char c; 25701da177e4SLinus Torvalds 257199dbbb59SAl Viro for (len = 0; (c = s[len]) != '\0'; len++) { 257299dbbb59SAl Viro if (c == '"') 25733528a953SCory Olmo open_quote = !open_quote; 257499dbbb59SAl Viro if (c == ',' && !open_quote) 257599dbbb59SAl Viro break; 25761da177e4SLinus Torvalds } 257799dbbb59SAl Viro return len; 25781da177e4SLinus Torvalds } 25791da177e4SLinus Torvalds 2580204cc0ccSAl Viro static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts) 25815b400239SAl Viro { 258299dbbb59SAl Viro char *from = options; 258399dbbb59SAl Viro char *to = options; 258499dbbb59SAl Viro bool first = true; 2585fec63753SGen Zhang int rc; 25865b400239SAl Viro 258799dbbb59SAl Viro while (1) { 258899dbbb59SAl Viro int len = opt_len(from); 2589fec63753SGen Zhang int token; 259099dbbb59SAl Viro char *arg = NULL; 259199dbbb59SAl Viro 259299dbbb59SAl Viro token = match_opt_prefix(from, len, &arg); 259399dbbb59SAl Viro 259499dbbb59SAl Viro if (token != Opt_error) { 259599dbbb59SAl Viro char *p, *q; 259699dbbb59SAl Viro 259799dbbb59SAl Viro /* strip quotes */ 259899dbbb59SAl Viro if (arg) { 259999dbbb59SAl Viro for (p = q = arg; p < from + len; p++) { 260099dbbb59SAl Viro char c = *p; 260199dbbb59SAl Viro if (c != '"') 260299dbbb59SAl Viro *q++ = c; 260399dbbb59SAl Viro } 260499dbbb59SAl Viro arg = kmemdup_nul(arg, q - arg, GFP_KERNEL); 2605fec63753SGen Zhang if (!arg) { 2606fec63753SGen Zhang rc = -ENOMEM; 2607fec63753SGen Zhang goto free_opt; 2608fec63753SGen Zhang } 260999dbbb59SAl Viro } 261099dbbb59SAl Viro rc = selinux_add_opt(token, arg, mnt_opts); 261199dbbb59SAl Viro if (unlikely(rc)) { 261299dbbb59SAl Viro kfree(arg); 2613fec63753SGen Zhang goto free_opt; 26141da177e4SLinus Torvalds } 261599dbbb59SAl Viro } else { 261699dbbb59SAl Viro if (!first) { // copy with preceding comma 261799dbbb59SAl Viro from--; 261899dbbb59SAl Viro len++; 261999dbbb59SAl Viro } 262099dbbb59SAl Viro if (to != from) 262199dbbb59SAl Viro memmove(to, from, len); 262299dbbb59SAl Viro to += len; 262399dbbb59SAl Viro first = false; 262499dbbb59SAl Viro } 262599dbbb59SAl Viro if (!from[len]) 262699dbbb59SAl Viro break; 262799dbbb59SAl Viro from += len + 1; 262899dbbb59SAl Viro } 262999dbbb59SAl Viro *to = '\0'; 263099dbbb59SAl Viro return 0; 2631fec63753SGen Zhang 2632fec63753SGen Zhang free_opt: 2633fec63753SGen Zhang if (*mnt_opts) { 2634fec63753SGen Zhang selinux_free_mnt_opts(*mnt_opts); 2635fec63753SGen Zhang *mnt_opts = NULL; 2636fec63753SGen Zhang } 2637fec63753SGen Zhang return rc; 26385b400239SAl Viro } 26391da177e4SLinus Torvalds 2640204cc0ccSAl Viro static int selinux_sb_remount(struct super_block *sb, void *mnt_opts) 2641026eb167SEric Paris { 2642bd323655SAl Viro struct selinux_mnt_opts *opts = mnt_opts; 2643026eb167SEric Paris struct superblock_security_struct *sbsec = sb->s_security; 2644bd323655SAl Viro u32 sid; 2645bd323655SAl Viro int rc; 2646026eb167SEric Paris 2647026eb167SEric Paris if (!(sbsec->flags & SE_SBINITIALIZED)) 2648026eb167SEric Paris return 0; 2649026eb167SEric Paris 2650204cc0ccSAl Viro if (!opts) 2651026eb167SEric Paris return 0; 2652026eb167SEric Paris 2653bd323655SAl Viro if (opts->fscontext) { 2654bd323655SAl Viro rc = parse_sid(sb, opts->fscontext, &sid); 2655026eb167SEric Paris if (rc) 2656c039bc3cSAl Viro return rc; 2657026eb167SEric Paris if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid)) 2658026eb167SEric Paris goto out_bad_option; 2659bd323655SAl Viro } 2660bd323655SAl Viro if (opts->context) { 2661bd323655SAl Viro rc = parse_sid(sb, opts->context, &sid); 2662bd323655SAl Viro if (rc) 2663bd323655SAl Viro return rc; 2664026eb167SEric Paris if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid)) 2665026eb167SEric Paris goto out_bad_option; 2666bd323655SAl Viro } 2667bd323655SAl Viro if (opts->rootcontext) { 2668026eb167SEric Paris struct inode_security_struct *root_isec; 266983da53c5SAndreas Gruenbacher root_isec = backing_inode_security(sb->s_root); 2670bd323655SAl Viro rc = parse_sid(sb, opts->rootcontext, &sid); 2671bd323655SAl Viro if (rc) 2672bd323655SAl Viro return rc; 2673026eb167SEric Paris if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid)) 2674026eb167SEric Paris goto out_bad_option; 2675026eb167SEric Paris } 2676bd323655SAl Viro if (opts->defcontext) { 2677bd323655SAl Viro rc = parse_sid(sb, opts->defcontext, &sid); 2678bd323655SAl Viro if (rc) 2679bd323655SAl Viro return rc; 2680026eb167SEric Paris if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid)) 2681026eb167SEric Paris goto out_bad_option; 2682026eb167SEric Paris } 2683c039bc3cSAl Viro return 0; 2684026eb167SEric Paris 2685026eb167SEric Paris out_bad_option: 2686c103a91eSpeter enderborg pr_warn("SELinux: unable to change security options " 268729b1deb2SLinus Torvalds "during remount (dev %s, type=%s)\n", sb->s_id, 268829b1deb2SLinus Torvalds sb->s_type->name); 2689c039bc3cSAl Viro return -EINVAL; 2690026eb167SEric Paris } 2691026eb167SEric Paris 2692a10d7c22SAl Viro static int selinux_sb_kern_mount(struct super_block *sb) 26931da177e4SLinus Torvalds { 269488e67f3bSDavid Howells const struct cred *cred = current_cred(); 26952bf49690SThomas Liu struct common_audit_data ad; 269674192246SJames Morris 269750c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 2698a269434dSEric Paris ad.u.dentry = sb->s_root; 269988e67f3bSDavid Howells return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad); 27001da177e4SLinus Torvalds } 27011da177e4SLinus Torvalds 2702726c3342SDavid Howells static int selinux_sb_statfs(struct dentry *dentry) 27031da177e4SLinus Torvalds { 270488e67f3bSDavid Howells const struct cred *cred = current_cred(); 27052bf49690SThomas Liu struct common_audit_data ad; 27061da177e4SLinus Torvalds 270750c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 2708a269434dSEric Paris ad.u.dentry = dentry->d_sb->s_root; 270988e67f3bSDavid Howells return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad); 27101da177e4SLinus Torvalds } 27111da177e4SLinus Torvalds 2712808d4e3cSAl Viro static int selinux_mount(const char *dev_name, 27138a04c43bSAl Viro const struct path *path, 2714808d4e3cSAl Viro const char *type, 27151da177e4SLinus Torvalds unsigned long flags, 27161da177e4SLinus Torvalds void *data) 27171da177e4SLinus Torvalds { 271888e67f3bSDavid Howells const struct cred *cred = current_cred(); 27191da177e4SLinus Torvalds 27201da177e4SLinus Torvalds if (flags & MS_REMOUNT) 2721d8c9584eSAl Viro return superblock_has_perm(cred, path->dentry->d_sb, 27221da177e4SLinus Torvalds FILESYSTEM__REMOUNT, NULL); 27231da177e4SLinus Torvalds else 27242875fa00SEric Paris return path_has_perm(cred, path, FILE__MOUNTON); 27251da177e4SLinus Torvalds } 27261da177e4SLinus Torvalds 272798aa0034SStephen Smalley static int selinux_move_mount(const struct path *from_path, 272898aa0034SStephen Smalley const struct path *to_path) 272998aa0034SStephen Smalley { 273098aa0034SStephen Smalley const struct cred *cred = current_cred(); 273198aa0034SStephen Smalley 273298aa0034SStephen Smalley return path_has_perm(cred, to_path, FILE__MOUNTON); 273398aa0034SStephen Smalley } 273498aa0034SStephen Smalley 27351da177e4SLinus Torvalds static int selinux_umount(struct vfsmount *mnt, int flags) 27361da177e4SLinus Torvalds { 273788e67f3bSDavid Howells const struct cred *cred = current_cred(); 27381da177e4SLinus Torvalds 273988e67f3bSDavid Howells return superblock_has_perm(cred, mnt->mnt_sb, 27401da177e4SLinus Torvalds FILESYSTEM__UNMOUNT, NULL); 27411da177e4SLinus Torvalds } 27421da177e4SLinus Torvalds 27430b52075eSAl Viro static int selinux_fs_context_dup(struct fs_context *fc, 27440b52075eSAl Viro struct fs_context *src_fc) 27450b52075eSAl Viro { 27460b52075eSAl Viro const struct selinux_mnt_opts *src = src_fc->security; 27470b52075eSAl Viro struct selinux_mnt_opts *opts; 27480b52075eSAl Viro 27490b52075eSAl Viro if (!src) 27500b52075eSAl Viro return 0; 27510b52075eSAl Viro 27520b52075eSAl Viro fc->security = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL); 27530b52075eSAl Viro if (!fc->security) 27540b52075eSAl Viro return -ENOMEM; 27550b52075eSAl Viro 27560b52075eSAl Viro opts = fc->security; 27570b52075eSAl Viro 27580b52075eSAl Viro if (src->fscontext) { 27590b52075eSAl Viro opts->fscontext = kstrdup(src->fscontext, GFP_KERNEL); 27600b52075eSAl Viro if (!opts->fscontext) 27610b52075eSAl Viro return -ENOMEM; 27620b52075eSAl Viro } 27630b52075eSAl Viro if (src->context) { 27640b52075eSAl Viro opts->context = kstrdup(src->context, GFP_KERNEL); 27650b52075eSAl Viro if (!opts->context) 27660b52075eSAl Viro return -ENOMEM; 27670b52075eSAl Viro } 27680b52075eSAl Viro if (src->rootcontext) { 27690b52075eSAl Viro opts->rootcontext = kstrdup(src->rootcontext, GFP_KERNEL); 27700b52075eSAl Viro if (!opts->rootcontext) 27710b52075eSAl Viro return -ENOMEM; 27720b52075eSAl Viro } 27730b52075eSAl Viro if (src->defcontext) { 27740b52075eSAl Viro opts->defcontext = kstrdup(src->defcontext, GFP_KERNEL); 27750b52075eSAl Viro if (!opts->defcontext) 27760b52075eSAl Viro return -ENOMEM; 27770b52075eSAl Viro } 27780b52075eSAl Viro return 0; 27790b52075eSAl Viro } 27800b52075eSAl Viro 2781d7167b14SAl Viro static const struct fs_parameter_spec selinux_fs_parameters[] = { 2782442155c1SDavid Howells fsparam_string(CONTEXT_STR, Opt_context), 2783442155c1SDavid Howells fsparam_string(DEFCONTEXT_STR, Opt_defcontext), 2784442155c1SDavid Howells fsparam_string(FSCONTEXT_STR, Opt_fscontext), 2785442155c1SDavid Howells fsparam_string(ROOTCONTEXT_STR, Opt_rootcontext), 2786442155c1SDavid Howells fsparam_flag (SECLABEL_STR, Opt_seclabel), 2787442155c1SDavid Howells {} 2788442155c1SDavid Howells }; 2789442155c1SDavid Howells 2790442155c1SDavid Howells static int selinux_fs_context_parse_param(struct fs_context *fc, 2791442155c1SDavid Howells struct fs_parameter *param) 2792442155c1SDavid Howells { 2793442155c1SDavid Howells struct fs_parse_result result; 2794442155c1SDavid Howells int opt, rc; 2795442155c1SDavid Howells 2796d7167b14SAl Viro opt = fs_parse(fc, selinux_fs_parameters, param, &result); 2797442155c1SDavid Howells if (opt < 0) 2798442155c1SDavid Howells return opt; 2799442155c1SDavid Howells 2800442155c1SDavid Howells rc = selinux_add_opt(opt, param->string, &fc->security); 2801442155c1SDavid Howells if (!rc) { 2802442155c1SDavid Howells param->string = NULL; 2803442155c1SDavid Howells rc = 1; 2804442155c1SDavid Howells } 2805442155c1SDavid Howells return rc; 2806442155c1SDavid Howells } 2807442155c1SDavid Howells 28081da177e4SLinus Torvalds /* inode security operations */ 28091da177e4SLinus Torvalds 28101da177e4SLinus Torvalds static int selinux_inode_alloc_security(struct inode *inode) 28111da177e4SLinus Torvalds { 2812cb89e246SPaul Moore struct inode_security_struct *isec = selinux_inode(inode); 2813cb89e246SPaul Moore u32 sid = current_sid(); 2814cb89e246SPaul Moore 2815cb89e246SPaul Moore spin_lock_init(&isec->lock); 2816cb89e246SPaul Moore INIT_LIST_HEAD(&isec->list); 2817cb89e246SPaul Moore isec->inode = inode; 2818cb89e246SPaul Moore isec->sid = SECINITSID_UNLABELED; 2819cb89e246SPaul Moore isec->sclass = SECCLASS_FILE; 2820cb89e246SPaul Moore isec->task_sid = sid; 2821cb89e246SPaul Moore isec->initialized = LABEL_INVALID; 2822cb89e246SPaul Moore 2823cb89e246SPaul Moore return 0; 28241da177e4SLinus Torvalds } 28251da177e4SLinus Torvalds 28261da177e4SLinus Torvalds static void selinux_inode_free_security(struct inode *inode) 28271da177e4SLinus Torvalds { 28281da177e4SLinus Torvalds inode_free_security(inode); 28291da177e4SLinus Torvalds } 28301da177e4SLinus Torvalds 2831d47be3dfSDavid Quigley static int selinux_dentry_init_security(struct dentry *dentry, int mode, 28324f3ccd76SAl Viro const struct qstr *name, void **ctx, 2833d47be3dfSDavid Quigley u32 *ctxlen) 2834d47be3dfSDavid Quigley { 2835d47be3dfSDavid Quigley u32 newsid; 2836d47be3dfSDavid Quigley int rc; 2837d47be3dfSDavid Quigley 28380c6cfa62SCasey Schaufler rc = selinux_determine_inode_label(selinux_cred(current_cred()), 2839c957f6dfSVivek Goyal d_inode(dentry->d_parent), name, 2840d47be3dfSDavid Quigley inode_mode_to_security_class(mode), 2841d47be3dfSDavid Quigley &newsid); 2842c3c188b2SDavid Howells if (rc) 2843d47be3dfSDavid Quigley return rc; 2844d47be3dfSDavid Quigley 2845aa8e712cSStephen Smalley return security_sid_to_context(&selinux_state, newsid, (char **)ctx, 2846aa8e712cSStephen Smalley ctxlen); 2847d47be3dfSDavid Quigley } 2848d47be3dfSDavid Quigley 2849a518b0a5SVivek Goyal static int selinux_dentry_create_files_as(struct dentry *dentry, int mode, 2850a518b0a5SVivek Goyal struct qstr *name, 2851a518b0a5SVivek Goyal const struct cred *old, 2852a518b0a5SVivek Goyal struct cred *new) 2853a518b0a5SVivek Goyal { 2854a518b0a5SVivek Goyal u32 newsid; 2855a518b0a5SVivek Goyal int rc; 2856a518b0a5SVivek Goyal struct task_security_struct *tsec; 2857a518b0a5SVivek Goyal 28580c6cfa62SCasey Schaufler rc = selinux_determine_inode_label(selinux_cred(old), 2859a518b0a5SVivek Goyal d_inode(dentry->d_parent), name, 2860a518b0a5SVivek Goyal inode_mode_to_security_class(mode), 2861a518b0a5SVivek Goyal &newsid); 2862a518b0a5SVivek Goyal if (rc) 2863a518b0a5SVivek Goyal return rc; 2864a518b0a5SVivek Goyal 28650c6cfa62SCasey Schaufler tsec = selinux_cred(new); 2866a518b0a5SVivek Goyal tsec->create_sid = newsid; 2867a518b0a5SVivek Goyal return 0; 2868a518b0a5SVivek Goyal } 2869a518b0a5SVivek Goyal 28705e41ff9eSStephen Smalley static int selinux_inode_init_security(struct inode *inode, struct inode *dir, 28719548906bSTetsuo Handa const struct qstr *qstr, 28729548906bSTetsuo Handa const char **name, 28732a7dba39SEric Paris void **value, size_t *len) 28745e41ff9eSStephen Smalley { 28750c6cfa62SCasey Schaufler const struct task_security_struct *tsec = selinux_cred(current_cred()); 28765e41ff9eSStephen Smalley struct superblock_security_struct *sbsec; 2877c0d4f464SCorentin LABBE u32 newsid, clen; 28785e41ff9eSStephen Smalley int rc; 28799548906bSTetsuo Handa char *context; 28805e41ff9eSStephen Smalley 28815e41ff9eSStephen Smalley sbsec = dir->i_sb->s_security; 28825e41ff9eSStephen Smalley 28835e41ff9eSStephen Smalley newsid = tsec->create_sid; 2884275bb41eSDavid Howells 2885210a2928SYang Guo rc = selinux_determine_inode_label(tsec, dir, qstr, 28865e41ff9eSStephen Smalley inode_mode_to_security_class(inode->i_mode), 2887c3c188b2SDavid Howells &newsid); 2888c3c188b2SDavid Howells if (rc) 28895e41ff9eSStephen Smalley return rc; 28905e41ff9eSStephen Smalley 2891296fddf7SEric Paris /* Possibly defer initialization to selinux_complete_init. */ 28920d90a7ecSDavid P. Quigley if (sbsec->flags & SE_SBINITIALIZED) { 289380788c22SCasey Schaufler struct inode_security_struct *isec = selinux_inode(inode); 2894296fddf7SEric Paris isec->sclass = inode_mode_to_security_class(inode->i_mode); 2895296fddf7SEric Paris isec->sid = newsid; 28966f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 2897296fddf7SEric Paris } 28985e41ff9eSStephen Smalley 289965cddd50SOndrej Mosnacek if (!selinux_initialized(&selinux_state) || 290065cddd50SOndrej Mosnacek !(sbsec->flags & SBLABEL_MNT)) 290125a74f3bSStephen Smalley return -EOPNOTSUPP; 290225a74f3bSStephen Smalley 29039548906bSTetsuo Handa if (name) 29049548906bSTetsuo Handa *name = XATTR_SELINUX_SUFFIX; 29055e41ff9eSStephen Smalley 2906570bc1c2SStephen Smalley if (value && len) { 2907aa8e712cSStephen Smalley rc = security_sid_to_context_force(&selinux_state, newsid, 2908aa8e712cSStephen Smalley &context, &clen); 29099548906bSTetsuo Handa if (rc) 29105e41ff9eSStephen Smalley return rc; 29115e41ff9eSStephen Smalley *value = context; 2912570bc1c2SStephen Smalley *len = clen; 2913570bc1c2SStephen Smalley } 29145e41ff9eSStephen Smalley 29155e41ff9eSStephen Smalley return 0; 29165e41ff9eSStephen Smalley } 29175e41ff9eSStephen Smalley 29184acdaf27SAl Viro static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode) 29191da177e4SLinus Torvalds { 29201da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_FILE); 29211da177e4SLinus Torvalds } 29221da177e4SLinus Torvalds 29231da177e4SLinus Torvalds static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 29241da177e4SLinus Torvalds { 29251da177e4SLinus Torvalds return may_link(dir, old_dentry, MAY_LINK); 29261da177e4SLinus Torvalds } 29271da177e4SLinus Torvalds 29281da177e4SLinus Torvalds static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry) 29291da177e4SLinus Torvalds { 29301da177e4SLinus Torvalds return may_link(dir, dentry, MAY_UNLINK); 29311da177e4SLinus Torvalds } 29321da177e4SLinus Torvalds 29331da177e4SLinus Torvalds static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name) 29341da177e4SLinus Torvalds { 29351da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_LNK_FILE); 29361da177e4SLinus Torvalds } 29371da177e4SLinus Torvalds 293818bb1db3SAl Viro static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask) 29391da177e4SLinus Torvalds { 29401da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_DIR); 29411da177e4SLinus Torvalds } 29421da177e4SLinus Torvalds 29431da177e4SLinus Torvalds static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry) 29441da177e4SLinus Torvalds { 29451da177e4SLinus Torvalds return may_link(dir, dentry, MAY_RMDIR); 29461da177e4SLinus Torvalds } 29471da177e4SLinus Torvalds 29481a67aafbSAl Viro static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) 29491da177e4SLinus Torvalds { 29501da177e4SLinus Torvalds return may_create(dir, dentry, inode_mode_to_security_class(mode)); 29511da177e4SLinus Torvalds } 29521da177e4SLinus Torvalds 29531da177e4SLinus Torvalds static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry, 29541da177e4SLinus Torvalds struct inode *new_inode, struct dentry *new_dentry) 29551da177e4SLinus Torvalds { 29561da177e4SLinus Torvalds return may_rename(old_inode, old_dentry, new_inode, new_dentry); 29571da177e4SLinus Torvalds } 29581da177e4SLinus Torvalds 29591da177e4SLinus Torvalds static int selinux_inode_readlink(struct dentry *dentry) 29601da177e4SLinus Torvalds { 296188e67f3bSDavid Howells const struct cred *cred = current_cred(); 296288e67f3bSDavid Howells 29632875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__READ); 29641da177e4SLinus Torvalds } 29651da177e4SLinus Torvalds 2966bda0be7aSNeilBrown static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode, 2967bda0be7aSNeilBrown bool rcu) 29681da177e4SLinus Torvalds { 296988e67f3bSDavid Howells const struct cred *cred = current_cred(); 2970bda0be7aSNeilBrown struct common_audit_data ad; 2971bda0be7aSNeilBrown struct inode_security_struct *isec; 2972bda0be7aSNeilBrown u32 sid; 29731da177e4SLinus Torvalds 2974bda0be7aSNeilBrown validate_creds(cred); 2975bda0be7aSNeilBrown 2976bda0be7aSNeilBrown ad.type = LSM_AUDIT_DATA_DENTRY; 2977bda0be7aSNeilBrown ad.u.dentry = dentry; 2978bda0be7aSNeilBrown sid = cred_sid(cred); 29795d226df4SAndreas Gruenbacher isec = inode_security_rcu(inode, rcu); 29805d226df4SAndreas Gruenbacher if (IS_ERR(isec)) 29815d226df4SAndreas Gruenbacher return PTR_ERR(isec); 2982bda0be7aSNeilBrown 29831a37079cSStephen Smalley return avc_has_perm_flags(&selinux_state, 29841a37079cSStephen Smalley sid, isec->sid, isec->sclass, FILE__READ, &ad, 29851a37079cSStephen Smalley rcu ? MAY_NOT_BLOCK : 0); 29861da177e4SLinus Torvalds } 29871da177e4SLinus Torvalds 2988d4cf970dSEric Paris static noinline int audit_inode_permission(struct inode *inode, 2989d4cf970dSEric Paris u32 perms, u32 audited, u32 denied, 29900188d5c0SStephen Smalley int result) 2991d4cf970dSEric Paris { 2992d4cf970dSEric Paris struct common_audit_data ad; 299380788c22SCasey Schaufler struct inode_security_struct *isec = selinux_inode(inode); 2994d4cf970dSEric Paris int rc; 2995d4cf970dSEric Paris 299650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_INODE; 2997d4cf970dSEric Paris ad.u.inode = inode; 2998d4cf970dSEric Paris 29996b6bc620SStephen Smalley rc = slow_avc_audit(&selinux_state, 30006b6bc620SStephen Smalley current_sid(), isec->sid, isec->sclass, perms, 30010188d5c0SStephen Smalley audited, denied, result, &ad); 3002d4cf970dSEric Paris if (rc) 3003d4cf970dSEric Paris return rc; 3004d4cf970dSEric Paris return 0; 3005d4cf970dSEric Paris } 3006d4cf970dSEric Paris 3007e74f71ebSAl Viro static int selinux_inode_permission(struct inode *inode, int mask) 30081da177e4SLinus Torvalds { 300988e67f3bSDavid Howells const struct cred *cred = current_cred(); 3010b782e0a6SEric Paris u32 perms; 3011b782e0a6SEric Paris bool from_access; 30125298d0b9SStephen Smalley bool no_block = mask & MAY_NOT_BLOCK; 30132e334057SEric Paris struct inode_security_struct *isec; 30142e334057SEric Paris u32 sid; 30152e334057SEric Paris struct av_decision avd; 30162e334057SEric Paris int rc, rc2; 30172e334057SEric Paris u32 audited, denied; 30181da177e4SLinus Torvalds 3019b782e0a6SEric Paris from_access = mask & MAY_ACCESS; 3020d09ca739SEric Paris mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND); 3021d09ca739SEric Paris 30221da177e4SLinus Torvalds /* No permission to check. Existence test. */ 3023b782e0a6SEric Paris if (!mask) 30241da177e4SLinus Torvalds return 0; 30251da177e4SLinus Torvalds 30262e334057SEric Paris validate_creds(cred); 3027b782e0a6SEric Paris 30282e334057SEric Paris if (unlikely(IS_PRIVATE(inode))) 30292e334057SEric Paris return 0; 3030b782e0a6SEric Paris 3031b782e0a6SEric Paris perms = file_mask_to_av(inode->i_mode, mask); 3032b782e0a6SEric Paris 30332e334057SEric Paris sid = cred_sid(cred); 30345298d0b9SStephen Smalley isec = inode_security_rcu(inode, no_block); 30355d226df4SAndreas Gruenbacher if (IS_ERR(isec)) 30365d226df4SAndreas Gruenbacher return PTR_ERR(isec); 30372e334057SEric Paris 30386b6bc620SStephen Smalley rc = avc_has_perm_noaudit(&selinux_state, 30393a28cff3SStephen Smalley sid, isec->sid, isec->sclass, perms, 30405298d0b9SStephen Smalley no_block ? AVC_NONBLOCKING : 0, 30413a28cff3SStephen Smalley &avd); 30422e334057SEric Paris audited = avc_audit_required(perms, &avd, rc, 30432e334057SEric Paris from_access ? FILE__AUDIT_ACCESS : 0, 30442e334057SEric Paris &denied); 30452e334057SEric Paris if (likely(!audited)) 30462e334057SEric Paris return rc; 30472e334057SEric Paris 30480188d5c0SStephen Smalley /* fall back to ref-walk if we have to generate audit */ 30495298d0b9SStephen Smalley if (no_block) 30500188d5c0SStephen Smalley return -ECHILD; 30510188d5c0SStephen Smalley 30520188d5c0SStephen Smalley rc2 = audit_inode_permission(inode, perms, audited, denied, rc); 30532e334057SEric Paris if (rc2) 30542e334057SEric Paris return rc2; 30552e334057SEric Paris return rc; 30561da177e4SLinus Torvalds } 30571da177e4SLinus Torvalds 30581da177e4SLinus Torvalds static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr) 30591da177e4SLinus Torvalds { 306088e67f3bSDavid Howells const struct cred *cred = current_cred(); 3061ccb54478SStephen Smalley struct inode *inode = d_backing_inode(dentry); 3062bc6a6008SAmerigo Wang unsigned int ia_valid = iattr->ia_valid; 306395dbf739SEric Paris __u32 av = FILE__WRITE; 30641da177e4SLinus Torvalds 3065bc6a6008SAmerigo Wang /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */ 3066bc6a6008SAmerigo Wang if (ia_valid & ATTR_FORCE) { 3067bc6a6008SAmerigo Wang ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE | 3068bc6a6008SAmerigo Wang ATTR_FORCE); 3069bc6a6008SAmerigo Wang if (!ia_valid) 30701da177e4SLinus Torvalds return 0; 3071bc6a6008SAmerigo Wang } 30721da177e4SLinus Torvalds 3073bc6a6008SAmerigo Wang if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | 3074bc6a6008SAmerigo Wang ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET)) 30752875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__SETATTR); 30761da177e4SLinus Torvalds 3077aa8e712cSStephen Smalley if (selinux_policycap_openperm() && 3078ccb54478SStephen Smalley inode->i_sb->s_magic != SOCKFS_MAGIC && 3079ccb54478SStephen Smalley (ia_valid & ATTR_SIZE) && 3080ccb54478SStephen Smalley !(ia_valid & ATTR_FILE)) 308195dbf739SEric Paris av |= FILE__OPEN; 308295dbf739SEric Paris 308395dbf739SEric Paris return dentry_has_perm(cred, dentry, av); 30841da177e4SLinus Torvalds } 30851da177e4SLinus Torvalds 30863f7036a0SAl Viro static int selinux_inode_getattr(const struct path *path) 30871da177e4SLinus Torvalds { 30883f7036a0SAl Viro return path_has_perm(current_cred(), path, FILE__GETATTR); 30891da177e4SLinus Torvalds } 30901da177e4SLinus Torvalds 3091db59000aSStephen Smalley static bool has_cap_mac_admin(bool audit) 3092db59000aSStephen Smalley { 3093db59000aSStephen Smalley const struct cred *cred = current_cred(); 3094c1a85a00SMicah Morton unsigned int opts = audit ? CAP_OPT_NONE : CAP_OPT_NOAUDIT; 3095db59000aSStephen Smalley 3096c1a85a00SMicah Morton if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, opts)) 3097db59000aSStephen Smalley return false; 3098c1a85a00SMicah Morton if (cred_has_capability(cred, CAP_MAC_ADMIN, opts, true)) 3099db59000aSStephen Smalley return false; 3100db59000aSStephen Smalley return true; 3101db59000aSStephen Smalley } 3102db59000aSStephen Smalley 31038f0cfa52SDavid Howells static int selinux_inode_setxattr(struct dentry *dentry, const char *name, 31048f0cfa52SDavid Howells const void *value, size_t size, int flags) 31051da177e4SLinus Torvalds { 3106c6f493d6SDavid Howells struct inode *inode = d_backing_inode(dentry); 310720cdef8dSPaul Moore struct inode_security_struct *isec; 31081da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 31092bf49690SThomas Liu struct common_audit_data ad; 3110275bb41eSDavid Howells u32 newsid, sid = current_sid(); 31111da177e4SLinus Torvalds int rc = 0; 31121da177e4SLinus Torvalds 31136b240306SEric W. Biederman if (strcmp(name, XATTR_NAME_SELINUX)) { 31146b240306SEric W. Biederman rc = cap_inode_setxattr(dentry, name, value, size, flags); 31156b240306SEric W. Biederman if (rc) 31166b240306SEric W. Biederman return rc; 31176b240306SEric W. Biederman 31186b240306SEric W. Biederman /* Not an attribute we recognize, so just check the 31196b240306SEric W. Biederman ordinary setattr permission. */ 31206b240306SEric W. Biederman return dentry_has_perm(current_cred(), dentry, FILE__SETATTR); 31216b240306SEric W. Biederman } 31221da177e4SLinus Torvalds 312365cddd50SOndrej Mosnacek if (!selinux_initialized(&selinux_state)) 31243e3e24b4SJonathan Lebon return (inode_owner_or_capable(inode) ? 0 : -EPERM); 31253e3e24b4SJonathan Lebon 31261da177e4SLinus Torvalds sbsec = inode->i_sb->s_security; 312712f348b9SEric Paris if (!(sbsec->flags & SBLABEL_MNT)) 31281da177e4SLinus Torvalds return -EOPNOTSUPP; 31291da177e4SLinus Torvalds 31302e149670SSerge E. Hallyn if (!inode_owner_or_capable(inode)) 31311da177e4SLinus Torvalds return -EPERM; 31321da177e4SLinus Torvalds 313350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 3134a269434dSEric Paris ad.u.dentry = dentry; 31351da177e4SLinus Torvalds 313620cdef8dSPaul Moore isec = backing_inode_security(dentry); 31376b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 31386b6bc620SStephen Smalley sid, isec->sid, isec->sclass, 31391da177e4SLinus Torvalds FILE__RELABELFROM, &ad); 31401da177e4SLinus Torvalds if (rc) 31411da177e4SLinus Torvalds return rc; 31421da177e4SLinus Torvalds 3143aa8e712cSStephen Smalley rc = security_context_to_sid(&selinux_state, value, size, &newsid, 3144aa8e712cSStephen Smalley GFP_KERNEL); 314512b29f34SStephen Smalley if (rc == -EINVAL) { 3146db59000aSStephen Smalley if (!has_cap_mac_admin(true)) { 3147d6ea83ecSEric Paris struct audit_buffer *ab; 3148d6ea83ecSEric Paris size_t audit_size; 3149d6ea83ecSEric Paris 3150d6ea83ecSEric Paris /* We strip a nul only if it is at the end, otherwise the 3151d6ea83ecSEric Paris * context contains a nul and we should audit that */ 3152e3fea3f7SAl Viro if (value) { 3153add24372SColin Ian King const char *str = value; 3154add24372SColin Ian King 3155d6ea83ecSEric Paris if (str[size - 1] == '\0') 3156d6ea83ecSEric Paris audit_size = size - 1; 3157d6ea83ecSEric Paris else 3158d6ea83ecSEric Paris audit_size = size; 3159e3fea3f7SAl Viro } else { 3160e3fea3f7SAl Viro audit_size = 0; 3161e3fea3f7SAl Viro } 3162cdfb6b34SRichard Guy Briggs ab = audit_log_start(audit_context(), 3163cdfb6b34SRichard Guy Briggs GFP_ATOMIC, AUDIT_SELINUX_ERR); 3164d6ea83ecSEric Paris audit_log_format(ab, "op=setxattr invalid_context="); 3165d6ea83ecSEric Paris audit_log_n_untrustedstring(ab, value, audit_size); 3166d6ea83ecSEric Paris audit_log_end(ab); 3167d6ea83ecSEric Paris 316812b29f34SStephen Smalley return rc; 3169d6ea83ecSEric Paris } 3170aa8e712cSStephen Smalley rc = security_context_to_sid_force(&selinux_state, value, 3171aa8e712cSStephen Smalley size, &newsid); 317212b29f34SStephen Smalley } 31731da177e4SLinus Torvalds if (rc) 31741da177e4SLinus Torvalds return rc; 31751da177e4SLinus Torvalds 31766b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 31776b6bc620SStephen Smalley sid, newsid, isec->sclass, 31781da177e4SLinus Torvalds FILE__RELABELTO, &ad); 31791da177e4SLinus Torvalds if (rc) 31801da177e4SLinus Torvalds return rc; 31811da177e4SLinus Torvalds 3182aa8e712cSStephen Smalley rc = security_validate_transition(&selinux_state, isec->sid, newsid, 3183aa8e712cSStephen Smalley sid, isec->sclass); 31841da177e4SLinus Torvalds if (rc) 31851da177e4SLinus Torvalds return rc; 31861da177e4SLinus Torvalds 31876b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 31886b6bc620SStephen Smalley newsid, 31891da177e4SLinus Torvalds sbsec->sid, 31901da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 31911da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, 31921da177e4SLinus Torvalds &ad); 31931da177e4SLinus Torvalds } 31941da177e4SLinus Torvalds 31958f0cfa52SDavid Howells static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name, 31968f0cfa52SDavid Howells const void *value, size_t size, 31978f0cfa52SDavid Howells int flags) 31981da177e4SLinus Torvalds { 3199c6f493d6SDavid Howells struct inode *inode = d_backing_inode(dentry); 320020cdef8dSPaul Moore struct inode_security_struct *isec; 32011da177e4SLinus Torvalds u32 newsid; 32021da177e4SLinus Torvalds int rc; 32031da177e4SLinus Torvalds 32041da177e4SLinus Torvalds if (strcmp(name, XATTR_NAME_SELINUX)) { 32051da177e4SLinus Torvalds /* Not an attribute we recognize, so nothing to do. */ 32061da177e4SLinus Torvalds return; 32071da177e4SLinus Torvalds } 32081da177e4SLinus Torvalds 320965cddd50SOndrej Mosnacek if (!selinux_initialized(&selinux_state)) { 32103e3e24b4SJonathan Lebon /* If we haven't even been initialized, then we can't validate 32113e3e24b4SJonathan Lebon * against a policy, so leave the label as invalid. It may 32123e3e24b4SJonathan Lebon * resolve to a valid label on the next revalidation try if 32133e3e24b4SJonathan Lebon * we've since initialized. 32143e3e24b4SJonathan Lebon */ 32153e3e24b4SJonathan Lebon return; 32163e3e24b4SJonathan Lebon } 32173e3e24b4SJonathan Lebon 3218aa8e712cSStephen Smalley rc = security_context_to_sid_force(&selinux_state, value, size, 3219aa8e712cSStephen Smalley &newsid); 32201da177e4SLinus Torvalds if (rc) { 3221c103a91eSpeter enderborg pr_err("SELinux: unable to map context to SID" 322212b29f34SStephen Smalley "for (%s, %lu), rc=%d\n", 322312b29f34SStephen Smalley inode->i_sb->s_id, inode->i_ino, -rc); 32241da177e4SLinus Torvalds return; 32251da177e4SLinus Torvalds } 32261da177e4SLinus Torvalds 322720cdef8dSPaul Moore isec = backing_inode_security(dentry); 32289287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 3229aa9c2669SDavid Quigley isec->sclass = inode_mode_to_security_class(inode->i_mode); 32301da177e4SLinus Torvalds isec->sid = newsid; 32316f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 32329287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 3233aa9c2669SDavid Quigley 32341da177e4SLinus Torvalds return; 32351da177e4SLinus Torvalds } 32361da177e4SLinus Torvalds 32378f0cfa52SDavid Howells static int selinux_inode_getxattr(struct dentry *dentry, const char *name) 32381da177e4SLinus Torvalds { 323988e67f3bSDavid Howells const struct cred *cred = current_cred(); 324088e67f3bSDavid Howells 32412875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__GETATTR); 32421da177e4SLinus Torvalds } 32431da177e4SLinus Torvalds 32441da177e4SLinus Torvalds static int selinux_inode_listxattr(struct dentry *dentry) 32451da177e4SLinus Torvalds { 324688e67f3bSDavid Howells const struct cred *cred = current_cred(); 324788e67f3bSDavid Howells 32482875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__GETATTR); 32491da177e4SLinus Torvalds } 32501da177e4SLinus Torvalds 32518f0cfa52SDavid Howells static int selinux_inode_removexattr(struct dentry *dentry, const char *name) 32521da177e4SLinus Torvalds { 32536b240306SEric W. Biederman if (strcmp(name, XATTR_NAME_SELINUX)) { 32546b240306SEric W. Biederman int rc = cap_inode_removexattr(dentry, name); 32556b240306SEric W. Biederman if (rc) 32566b240306SEric W. Biederman return rc; 32576b240306SEric W. Biederman 32586b240306SEric W. Biederman /* Not an attribute we recognize, so just check the 32596b240306SEric W. Biederman ordinary setattr permission. */ 32606b240306SEric W. Biederman return dentry_has_perm(current_cred(), dentry, FILE__SETATTR); 32616b240306SEric W. Biederman } 32621da177e4SLinus Torvalds 32631da177e4SLinus Torvalds /* No one is allowed to remove a SELinux security label. 32641da177e4SLinus Torvalds You can change the label, but all data must be labeled. */ 32651da177e4SLinus Torvalds return -EACCES; 32661da177e4SLinus Torvalds } 32671da177e4SLinus Torvalds 3268ac5656d8SAaron Goidel static int selinux_path_notify(const struct path *path, u64 mask, 3269ac5656d8SAaron Goidel unsigned int obj_type) 3270ac5656d8SAaron Goidel { 3271ac5656d8SAaron Goidel int ret; 3272ac5656d8SAaron Goidel u32 perm; 3273ac5656d8SAaron Goidel 3274ac5656d8SAaron Goidel struct common_audit_data ad; 3275ac5656d8SAaron Goidel 3276ac5656d8SAaron Goidel ad.type = LSM_AUDIT_DATA_PATH; 3277ac5656d8SAaron Goidel ad.u.path = *path; 3278ac5656d8SAaron Goidel 3279ac5656d8SAaron Goidel /* 3280ac5656d8SAaron Goidel * Set permission needed based on the type of mark being set. 3281ac5656d8SAaron Goidel * Performs an additional check for sb watches. 3282ac5656d8SAaron Goidel */ 3283ac5656d8SAaron Goidel switch (obj_type) { 3284ac5656d8SAaron Goidel case FSNOTIFY_OBJ_TYPE_VFSMOUNT: 3285ac5656d8SAaron Goidel perm = FILE__WATCH_MOUNT; 3286ac5656d8SAaron Goidel break; 3287ac5656d8SAaron Goidel case FSNOTIFY_OBJ_TYPE_SB: 3288ac5656d8SAaron Goidel perm = FILE__WATCH_SB; 3289ac5656d8SAaron Goidel ret = superblock_has_perm(current_cred(), path->dentry->d_sb, 3290ac5656d8SAaron Goidel FILESYSTEM__WATCH, &ad); 3291ac5656d8SAaron Goidel if (ret) 3292ac5656d8SAaron Goidel return ret; 3293ac5656d8SAaron Goidel break; 3294ac5656d8SAaron Goidel case FSNOTIFY_OBJ_TYPE_INODE: 3295ac5656d8SAaron Goidel perm = FILE__WATCH; 3296ac5656d8SAaron Goidel break; 3297ac5656d8SAaron Goidel default: 3298ac5656d8SAaron Goidel return -EINVAL; 3299ac5656d8SAaron Goidel } 3300ac5656d8SAaron Goidel 3301ac5656d8SAaron Goidel /* blocking watches require the file:watch_with_perm permission */ 3302ac5656d8SAaron Goidel if (mask & (ALL_FSNOTIFY_PERM_EVENTS)) 3303ac5656d8SAaron Goidel perm |= FILE__WATCH_WITH_PERM; 3304ac5656d8SAaron Goidel 3305ac5656d8SAaron Goidel /* watches on read-like events need the file:watch_reads permission */ 3306ac5656d8SAaron Goidel if (mask & (FS_ACCESS | FS_ACCESS_PERM | FS_CLOSE_NOWRITE)) 3307ac5656d8SAaron Goidel perm |= FILE__WATCH_READS; 3308ac5656d8SAaron Goidel 3309ac5656d8SAaron Goidel return path_has_perm(current_cred(), path, perm); 3310ac5656d8SAaron Goidel } 3311ac5656d8SAaron Goidel 3312d381d8a9SJames Morris /* 3313abc69bb6SStephen Smalley * Copy the inode security context value to the user. 3314d381d8a9SJames Morris * 3315d381d8a9SJames Morris * Permission check is handled by selinux_inode_getxattr hook. 3316d381d8a9SJames Morris */ 3317ea861dfdSAndreas Gruenbacher static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc) 33181da177e4SLinus Torvalds { 331942492594SDavid P. Quigley u32 size; 332042492594SDavid P. Quigley int error; 332142492594SDavid P. Quigley char *context = NULL; 332220cdef8dSPaul Moore struct inode_security_struct *isec; 33231da177e4SLinus Torvalds 33248c8570fbSDustin Kirkland if (strcmp(name, XATTR_SELINUX_SUFFIX)) 33258c8570fbSDustin Kirkland return -EOPNOTSUPP; 33261da177e4SLinus Torvalds 3327abc69bb6SStephen Smalley /* 3328abc69bb6SStephen Smalley * If the caller has CAP_MAC_ADMIN, then get the raw context 3329abc69bb6SStephen Smalley * value even if it is not defined by current policy; otherwise, 3330abc69bb6SStephen Smalley * use the in-core value under current policy. 3331abc69bb6SStephen Smalley * Use the non-auditing forms of the permission checks since 3332abc69bb6SStephen Smalley * getxattr may be called by unprivileged processes commonly 3333abc69bb6SStephen Smalley * and lack of permission just means that we fall back to the 3334abc69bb6SStephen Smalley * in-core context value, not a denial. 3335abc69bb6SStephen Smalley */ 333620cdef8dSPaul Moore isec = inode_security(inode); 3337db59000aSStephen Smalley if (has_cap_mac_admin(false)) 3338aa8e712cSStephen Smalley error = security_sid_to_context_force(&selinux_state, 3339aa8e712cSStephen Smalley isec->sid, &context, 3340abc69bb6SStephen Smalley &size); 3341abc69bb6SStephen Smalley else 3342aa8e712cSStephen Smalley error = security_sid_to_context(&selinux_state, isec->sid, 3343aa8e712cSStephen Smalley &context, &size); 334442492594SDavid P. Quigley if (error) 334542492594SDavid P. Quigley return error; 334642492594SDavid P. Quigley error = size; 334742492594SDavid P. Quigley if (alloc) { 334842492594SDavid P. Quigley *buffer = context; 334942492594SDavid P. Quigley goto out_nofree; 335042492594SDavid P. Quigley } 335142492594SDavid P. Quigley kfree(context); 335242492594SDavid P. Quigley out_nofree: 335342492594SDavid P. Quigley return error; 33541da177e4SLinus Torvalds } 33551da177e4SLinus Torvalds 33561da177e4SLinus Torvalds static int selinux_inode_setsecurity(struct inode *inode, const char *name, 33571da177e4SLinus Torvalds const void *value, size_t size, int flags) 33581da177e4SLinus Torvalds { 33592c97165bSPaul Moore struct inode_security_struct *isec = inode_security_novalidate(inode); 336053e0c2aaSOndrej Mosnacek struct superblock_security_struct *sbsec = inode->i_sb->s_security; 33611da177e4SLinus Torvalds u32 newsid; 33621da177e4SLinus Torvalds int rc; 33631da177e4SLinus Torvalds 33641da177e4SLinus Torvalds if (strcmp(name, XATTR_SELINUX_SUFFIX)) 33651da177e4SLinus Torvalds return -EOPNOTSUPP; 33661da177e4SLinus Torvalds 336753e0c2aaSOndrej Mosnacek if (!(sbsec->flags & SBLABEL_MNT)) 336853e0c2aaSOndrej Mosnacek return -EOPNOTSUPP; 336953e0c2aaSOndrej Mosnacek 33701da177e4SLinus Torvalds if (!value || !size) 33711da177e4SLinus Torvalds return -EACCES; 33721da177e4SLinus Torvalds 3373aa8e712cSStephen Smalley rc = security_context_to_sid(&selinux_state, value, size, &newsid, 3374aa8e712cSStephen Smalley GFP_KERNEL); 33751da177e4SLinus Torvalds if (rc) 33761da177e4SLinus Torvalds return rc; 33771da177e4SLinus Torvalds 33789287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 3379aa9c2669SDavid Quigley isec->sclass = inode_mode_to_security_class(inode->i_mode); 33801da177e4SLinus Torvalds isec->sid = newsid; 33816f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 33829287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 33831da177e4SLinus Torvalds return 0; 33841da177e4SLinus Torvalds } 33851da177e4SLinus Torvalds 33861da177e4SLinus Torvalds static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size) 33871da177e4SLinus Torvalds { 33881da177e4SLinus Torvalds const int len = sizeof(XATTR_NAME_SELINUX); 33891da177e4SLinus Torvalds if (buffer && len <= buffer_size) 33901da177e4SLinus Torvalds memcpy(buffer, XATTR_NAME_SELINUX, len); 33911da177e4SLinus Torvalds return len; 33921da177e4SLinus Torvalds } 33931da177e4SLinus Torvalds 3394d6335d77SAndreas Gruenbacher static void selinux_inode_getsecid(struct inode *inode, u32 *secid) 3395713a04aeSAhmed S. Darwish { 3396e817c2f3SAndreas Gruenbacher struct inode_security_struct *isec = inode_security_novalidate(inode); 3397713a04aeSAhmed S. Darwish *secid = isec->sid; 3398713a04aeSAhmed S. Darwish } 3399713a04aeSAhmed S. Darwish 340056909eb3SVivek Goyal static int selinux_inode_copy_up(struct dentry *src, struct cred **new) 340156909eb3SVivek Goyal { 340256909eb3SVivek Goyal u32 sid; 340356909eb3SVivek Goyal struct task_security_struct *tsec; 340456909eb3SVivek Goyal struct cred *new_creds = *new; 340556909eb3SVivek Goyal 340656909eb3SVivek Goyal if (new_creds == NULL) { 340756909eb3SVivek Goyal new_creds = prepare_creds(); 340856909eb3SVivek Goyal if (!new_creds) 340956909eb3SVivek Goyal return -ENOMEM; 341056909eb3SVivek Goyal } 341156909eb3SVivek Goyal 34120c6cfa62SCasey Schaufler tsec = selinux_cred(new_creds); 341356909eb3SVivek Goyal /* Get label from overlay inode and set it in create_sid */ 341456909eb3SVivek Goyal selinux_inode_getsecid(d_inode(src), &sid); 341556909eb3SVivek Goyal tsec->create_sid = sid; 341656909eb3SVivek Goyal *new = new_creds; 341756909eb3SVivek Goyal return 0; 341856909eb3SVivek Goyal } 341956909eb3SVivek Goyal 342019472b69SVivek Goyal static int selinux_inode_copy_up_xattr(const char *name) 342119472b69SVivek Goyal { 342219472b69SVivek Goyal /* The copy_up hook above sets the initial context on an inode, but we 342319472b69SVivek Goyal * don't then want to overwrite it by blindly copying all the lower 342419472b69SVivek Goyal * xattrs up. Instead, we have to filter out SELinux-related xattrs. 342519472b69SVivek Goyal */ 342619472b69SVivek Goyal if (strcmp(name, XATTR_NAME_SELINUX) == 0) 342719472b69SVivek Goyal return 1; /* Discard */ 342819472b69SVivek Goyal /* 342919472b69SVivek Goyal * Any other attribute apart from SELINUX is not claimed, supported 343019472b69SVivek Goyal * by selinux. 343119472b69SVivek Goyal */ 343219472b69SVivek Goyal return -EOPNOTSUPP; 343319472b69SVivek Goyal } 343419472b69SVivek Goyal 3435ec882da5SOndrej Mosnacek /* kernfs node operations */ 3436ec882da5SOndrej Mosnacek 3437c72c4cdeSYueHaibing static int selinux_kernfs_init_security(struct kernfs_node *kn_dir, 3438ec882da5SOndrej Mosnacek struct kernfs_node *kn) 3439ec882da5SOndrej Mosnacek { 3440169ce0c0SStephen Smalley const struct task_security_struct *tsec = selinux_cred(current_cred()); 3441ec882da5SOndrej Mosnacek u32 parent_sid, newsid, clen; 3442ec882da5SOndrej Mosnacek int rc; 3443ec882da5SOndrej Mosnacek char *context; 3444ec882da5SOndrej Mosnacek 34451537ad15SOndrej Mosnacek rc = kernfs_xattr_get(kn_dir, XATTR_NAME_SELINUX, NULL, 0); 3446ec882da5SOndrej Mosnacek if (rc == -ENODATA) 3447ec882da5SOndrej Mosnacek return 0; 3448ec882da5SOndrej Mosnacek else if (rc < 0) 3449ec882da5SOndrej Mosnacek return rc; 3450ec882da5SOndrej Mosnacek 3451ec882da5SOndrej Mosnacek clen = (u32)rc; 3452ec882da5SOndrej Mosnacek context = kmalloc(clen, GFP_KERNEL); 3453ec882da5SOndrej Mosnacek if (!context) 3454ec882da5SOndrej Mosnacek return -ENOMEM; 3455ec882da5SOndrej Mosnacek 34561537ad15SOndrej Mosnacek rc = kernfs_xattr_get(kn_dir, XATTR_NAME_SELINUX, context, clen); 3457ec882da5SOndrej Mosnacek if (rc < 0) { 3458ec882da5SOndrej Mosnacek kfree(context); 3459ec882da5SOndrej Mosnacek return rc; 3460ec882da5SOndrej Mosnacek } 3461ec882da5SOndrej Mosnacek 3462ec882da5SOndrej Mosnacek rc = security_context_to_sid(&selinux_state, context, clen, &parent_sid, 3463ec882da5SOndrej Mosnacek GFP_KERNEL); 3464ec882da5SOndrej Mosnacek kfree(context); 3465ec882da5SOndrej Mosnacek if (rc) 3466ec882da5SOndrej Mosnacek return rc; 3467ec882da5SOndrej Mosnacek 3468ec882da5SOndrej Mosnacek if (tsec->create_sid) { 3469ec882da5SOndrej Mosnacek newsid = tsec->create_sid; 3470ec882da5SOndrej Mosnacek } else { 3471ec882da5SOndrej Mosnacek u16 secclass = inode_mode_to_security_class(kn->mode); 3472ec882da5SOndrej Mosnacek struct qstr q; 3473ec882da5SOndrej Mosnacek 3474ec882da5SOndrej Mosnacek q.name = kn->name; 3475ec882da5SOndrej Mosnacek q.hash_len = hashlen_string(kn_dir, kn->name); 3476ec882da5SOndrej Mosnacek 3477ec882da5SOndrej Mosnacek rc = security_transition_sid(&selinux_state, tsec->sid, 3478ec882da5SOndrej Mosnacek parent_sid, secclass, &q, 3479ec882da5SOndrej Mosnacek &newsid); 3480ec882da5SOndrej Mosnacek if (rc) 3481ec882da5SOndrej Mosnacek return rc; 3482ec882da5SOndrej Mosnacek } 3483ec882da5SOndrej Mosnacek 3484ec882da5SOndrej Mosnacek rc = security_sid_to_context_force(&selinux_state, newsid, 3485ec882da5SOndrej Mosnacek &context, &clen); 3486ec882da5SOndrej Mosnacek if (rc) 3487ec882da5SOndrej Mosnacek return rc; 3488ec882da5SOndrej Mosnacek 34891537ad15SOndrej Mosnacek rc = kernfs_xattr_set(kn, XATTR_NAME_SELINUX, context, clen, 3490ec882da5SOndrej Mosnacek XATTR_CREATE); 3491ec882da5SOndrej Mosnacek kfree(context); 3492ec882da5SOndrej Mosnacek return rc; 3493ec882da5SOndrej Mosnacek } 3494ec882da5SOndrej Mosnacek 3495ec882da5SOndrej Mosnacek 34961da177e4SLinus Torvalds /* file security operations */ 34971da177e4SLinus Torvalds 3498788e7dd4SYuichi Nakamura static int selinux_revalidate_file_permission(struct file *file, int mask) 34991da177e4SLinus Torvalds { 350088e67f3bSDavid Howells const struct cred *cred = current_cred(); 3501496ad9aaSAl Viro struct inode *inode = file_inode(file); 35021da177e4SLinus Torvalds 35031da177e4SLinus Torvalds /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */ 35041da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE)) 35051da177e4SLinus Torvalds mask |= MAY_APPEND; 35061da177e4SLinus Torvalds 3507389fb800SPaul Moore return file_has_perm(cred, file, 35081da177e4SLinus Torvalds file_mask_to_av(inode->i_mode, mask)); 35091da177e4SLinus Torvalds } 35101da177e4SLinus Torvalds 3511788e7dd4SYuichi Nakamura static int selinux_file_permission(struct file *file, int mask) 3512788e7dd4SYuichi Nakamura { 3513496ad9aaSAl Viro struct inode *inode = file_inode(file); 3514bb6c6b02SCasey Schaufler struct file_security_struct *fsec = selinux_file(file); 3515b197367eSAndreas Gruenbacher struct inode_security_struct *isec; 351620dda18bSStephen Smalley u32 sid = current_sid(); 351720dda18bSStephen Smalley 3518389fb800SPaul Moore if (!mask) 3519788e7dd4SYuichi Nakamura /* No permission to check. Existence test. */ 3520788e7dd4SYuichi Nakamura return 0; 3521788e7dd4SYuichi Nakamura 3522b197367eSAndreas Gruenbacher isec = inode_security(inode); 352320dda18bSStephen Smalley if (sid == fsec->sid && fsec->isid == isec->sid && 35246b6bc620SStephen Smalley fsec->pseqno == avc_policy_seqno(&selinux_state)) 352583d49856SEric Paris /* No change since file_open check. */ 352620dda18bSStephen Smalley return 0; 352720dda18bSStephen Smalley 3528788e7dd4SYuichi Nakamura return selinux_revalidate_file_permission(file, mask); 3529788e7dd4SYuichi Nakamura } 3530788e7dd4SYuichi Nakamura 35311da177e4SLinus Torvalds static int selinux_file_alloc_security(struct file *file) 35321da177e4SLinus Torvalds { 3533cb89e246SPaul Moore struct file_security_struct *fsec = selinux_file(file); 3534cb89e246SPaul Moore u32 sid = current_sid(); 3535cb89e246SPaul Moore 3536cb89e246SPaul Moore fsec->sid = sid; 3537cb89e246SPaul Moore fsec->fown_sid = sid; 3538cb89e246SPaul Moore 3539cb89e246SPaul Moore return 0; 35401da177e4SLinus Torvalds } 35411da177e4SLinus Torvalds 3542fa1aa143SJeff Vander Stoep /* 3543fa1aa143SJeff Vander Stoep * Check whether a task has the ioctl permission and cmd 3544fa1aa143SJeff Vander Stoep * operation to an inode. 3545fa1aa143SJeff Vander Stoep */ 35461d2a168aSGeliang Tang static int ioctl_has_perm(const struct cred *cred, struct file *file, 3547fa1aa143SJeff Vander Stoep u32 requested, u16 cmd) 3548fa1aa143SJeff Vander Stoep { 3549fa1aa143SJeff Vander Stoep struct common_audit_data ad; 3550bb6c6b02SCasey Schaufler struct file_security_struct *fsec = selinux_file(file); 3551fa1aa143SJeff Vander Stoep struct inode *inode = file_inode(file); 355220cdef8dSPaul Moore struct inode_security_struct *isec; 3553fa1aa143SJeff Vander Stoep struct lsm_ioctlop_audit ioctl; 3554fa1aa143SJeff Vander Stoep u32 ssid = cred_sid(cred); 3555fa1aa143SJeff Vander Stoep int rc; 3556fa1aa143SJeff Vander Stoep u8 driver = cmd >> 8; 3557fa1aa143SJeff Vander Stoep u8 xperm = cmd & 0xff; 3558fa1aa143SJeff Vander Stoep 3559fa1aa143SJeff Vander Stoep ad.type = LSM_AUDIT_DATA_IOCTL_OP; 3560fa1aa143SJeff Vander Stoep ad.u.op = &ioctl; 3561fa1aa143SJeff Vander Stoep ad.u.op->cmd = cmd; 3562fa1aa143SJeff Vander Stoep ad.u.op->path = file->f_path; 3563fa1aa143SJeff Vander Stoep 3564fa1aa143SJeff Vander Stoep if (ssid != fsec->sid) { 35656b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 35666b6bc620SStephen Smalley ssid, fsec->sid, 3567fa1aa143SJeff Vander Stoep SECCLASS_FD, 3568fa1aa143SJeff Vander Stoep FD__USE, 3569fa1aa143SJeff Vander Stoep &ad); 3570fa1aa143SJeff Vander Stoep if (rc) 3571fa1aa143SJeff Vander Stoep goto out; 3572fa1aa143SJeff Vander Stoep } 3573fa1aa143SJeff Vander Stoep 3574fa1aa143SJeff Vander Stoep if (unlikely(IS_PRIVATE(inode))) 3575fa1aa143SJeff Vander Stoep return 0; 3576fa1aa143SJeff Vander Stoep 357720cdef8dSPaul Moore isec = inode_security(inode); 35786b6bc620SStephen Smalley rc = avc_has_extended_perms(&selinux_state, 35796b6bc620SStephen Smalley ssid, isec->sid, isec->sclass, 3580fa1aa143SJeff Vander Stoep requested, driver, xperm, &ad); 3581fa1aa143SJeff Vander Stoep out: 3582fa1aa143SJeff Vander Stoep return rc; 3583fa1aa143SJeff Vander Stoep } 3584fa1aa143SJeff Vander Stoep 35851da177e4SLinus Torvalds static int selinux_file_ioctl(struct file *file, unsigned int cmd, 35861da177e4SLinus Torvalds unsigned long arg) 35871da177e4SLinus Torvalds { 358888e67f3bSDavid Howells const struct cred *cred = current_cred(); 35890b24dcb7SEric Paris int error = 0; 35901da177e4SLinus Torvalds 35910b24dcb7SEric Paris switch (cmd) { 35920b24dcb7SEric Paris case FIONREAD: 35930b24dcb7SEric Paris /* fall through */ 35940b24dcb7SEric Paris case FIBMAP: 35950b24dcb7SEric Paris /* fall through */ 35960b24dcb7SEric Paris case FIGETBSZ: 35970b24dcb7SEric Paris /* fall through */ 35982f99c369SAl Viro case FS_IOC_GETFLAGS: 35990b24dcb7SEric Paris /* fall through */ 36002f99c369SAl Viro case FS_IOC_GETVERSION: 36010b24dcb7SEric Paris error = file_has_perm(cred, file, FILE__GETATTR); 36020b24dcb7SEric Paris break; 36031da177e4SLinus Torvalds 36042f99c369SAl Viro case FS_IOC_SETFLAGS: 36050b24dcb7SEric Paris /* fall through */ 36062f99c369SAl Viro case FS_IOC_SETVERSION: 36070b24dcb7SEric Paris error = file_has_perm(cred, file, FILE__SETATTR); 36080b24dcb7SEric Paris break; 36090b24dcb7SEric Paris 36100b24dcb7SEric Paris /* sys_ioctl() checks */ 36110b24dcb7SEric Paris case FIONBIO: 36120b24dcb7SEric Paris /* fall through */ 36130b24dcb7SEric Paris case FIOASYNC: 36140b24dcb7SEric Paris error = file_has_perm(cred, file, 0); 36150b24dcb7SEric Paris break; 36160b24dcb7SEric Paris 36170b24dcb7SEric Paris case KDSKBENT: 36180b24dcb7SEric Paris case KDSKBSENT: 36196a9de491SEric Paris error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG, 3620c1a85a00SMicah Morton CAP_OPT_NONE, true); 36210b24dcb7SEric Paris break; 36220b24dcb7SEric Paris 36230b24dcb7SEric Paris /* default case assumes that the command will go 36240b24dcb7SEric Paris * to the file's ioctl() function. 36250b24dcb7SEric Paris */ 36260b24dcb7SEric Paris default: 3627fa1aa143SJeff Vander Stoep error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd); 36280b24dcb7SEric Paris } 36290b24dcb7SEric Paris return error; 36301da177e4SLinus Torvalds } 36311da177e4SLinus Torvalds 3632b78b7d59SStephen Smalley static int default_noexec __ro_after_init; 3633fcaaade1SStephen Smalley 36341da177e4SLinus Torvalds static int file_map_prot_check(struct file *file, unsigned long prot, int shared) 36351da177e4SLinus Torvalds { 363688e67f3bSDavid Howells const struct cred *cred = current_cred(); 3637be0554c9SStephen Smalley u32 sid = cred_sid(cred); 3638d84f4f99SDavid Howells int rc = 0; 363988e67f3bSDavid Howells 3640fcaaade1SStephen Smalley if (default_noexec && 3641892e8cacSStephen Smalley (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) || 3642892e8cacSStephen Smalley (!shared && (prot & PROT_WRITE)))) { 36431da177e4SLinus Torvalds /* 36441da177e4SLinus Torvalds * We are making executable an anonymous mapping or a 36451da177e4SLinus Torvalds * private file mapping that will also be writable. 36461da177e4SLinus Torvalds * This has an additional check. 36471da177e4SLinus Torvalds */ 36486b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 36496b6bc620SStephen Smalley sid, sid, SECCLASS_PROCESS, 3650be0554c9SStephen Smalley PROCESS__EXECMEM, NULL); 36511da177e4SLinus Torvalds if (rc) 3652d84f4f99SDavid Howells goto error; 36531da177e4SLinus Torvalds } 36541da177e4SLinus Torvalds 36551da177e4SLinus Torvalds if (file) { 36561da177e4SLinus Torvalds /* read access is always possible with a mapping */ 36571da177e4SLinus Torvalds u32 av = FILE__READ; 36581da177e4SLinus Torvalds 36591da177e4SLinus Torvalds /* write access only matters if the mapping is shared */ 36601da177e4SLinus Torvalds if (shared && (prot & PROT_WRITE)) 36611da177e4SLinus Torvalds av |= FILE__WRITE; 36621da177e4SLinus Torvalds 36631da177e4SLinus Torvalds if (prot & PROT_EXEC) 36641da177e4SLinus Torvalds av |= FILE__EXECUTE; 36651da177e4SLinus Torvalds 366688e67f3bSDavid Howells return file_has_perm(cred, file, av); 36671da177e4SLinus Torvalds } 3668d84f4f99SDavid Howells 3669d84f4f99SDavid Howells error: 3670d84f4f99SDavid Howells return rc; 36711da177e4SLinus Torvalds } 36721da177e4SLinus Torvalds 3673e5467859SAl Viro static int selinux_mmap_addr(unsigned long addr) 36741da177e4SLinus Torvalds { 3675b1d9e6b0SCasey Schaufler int rc = 0; 367698883bfdSPaul Moore 367798883bfdSPaul Moore if (addr < CONFIG_LSM_MMAP_MIN_ADDR) { 367898883bfdSPaul Moore u32 sid = current_sid(); 36796b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 36806b6bc620SStephen Smalley sid, sid, SECCLASS_MEMPROTECT, 368198883bfdSPaul Moore MEMPROTECT__MMAP_ZERO, NULL); 368298883bfdSPaul Moore } 368398883bfdSPaul Moore 368498883bfdSPaul Moore return rc; 3685e5467859SAl Viro } 36861da177e4SLinus Torvalds 3687e5467859SAl Viro static int selinux_mmap_file(struct file *file, unsigned long reqprot, 3688e5467859SAl Viro unsigned long prot, unsigned long flags) 3689e5467859SAl Viro { 36903ba4bf5fSStephen Smalley struct common_audit_data ad; 36913ba4bf5fSStephen Smalley int rc; 36923ba4bf5fSStephen Smalley 36933ba4bf5fSStephen Smalley if (file) { 36943ba4bf5fSStephen Smalley ad.type = LSM_AUDIT_DATA_FILE; 36953ba4bf5fSStephen Smalley ad.u.file = file; 36963ba4bf5fSStephen Smalley rc = inode_has_perm(current_cred(), file_inode(file), 36973ba4bf5fSStephen Smalley FILE__MAP, &ad); 36983ba4bf5fSStephen Smalley if (rc) 36993ba4bf5fSStephen Smalley return rc; 37003ba4bf5fSStephen Smalley } 37013ba4bf5fSStephen Smalley 3702aa8e712cSStephen Smalley if (selinux_state.checkreqprot) 37031da177e4SLinus Torvalds prot = reqprot; 37041da177e4SLinus Torvalds 37051da177e4SLinus Torvalds return file_map_prot_check(file, prot, 37061da177e4SLinus Torvalds (flags & MAP_TYPE) == MAP_SHARED); 37071da177e4SLinus Torvalds } 37081da177e4SLinus Torvalds 37091da177e4SLinus Torvalds static int selinux_file_mprotect(struct vm_area_struct *vma, 37101da177e4SLinus Torvalds unsigned long reqprot, 37111da177e4SLinus Torvalds unsigned long prot) 37121da177e4SLinus Torvalds { 371388e67f3bSDavid Howells const struct cred *cred = current_cred(); 3714be0554c9SStephen Smalley u32 sid = cred_sid(cred); 37151da177e4SLinus Torvalds 3716aa8e712cSStephen Smalley if (selinux_state.checkreqprot) 37171da177e4SLinus Torvalds prot = reqprot; 37181da177e4SLinus Torvalds 3719fcaaade1SStephen Smalley if (default_noexec && 3720fcaaade1SStephen Smalley (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) { 3721d541bbeeSJames Morris int rc = 0; 3722db4c9641SStephen Smalley if (vma->vm_start >= vma->vm_mm->start_brk && 3723db4c9641SStephen Smalley vma->vm_end <= vma->vm_mm->brk) { 37246b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 37256b6bc620SStephen Smalley sid, sid, SECCLASS_PROCESS, 3726be0554c9SStephen Smalley PROCESS__EXECHEAP, NULL); 3727db4c9641SStephen Smalley } else if (!vma->vm_file && 3728c2316dbfSStephen Smalley ((vma->vm_start <= vma->vm_mm->start_stack && 3729c2316dbfSStephen Smalley vma->vm_end >= vma->vm_mm->start_stack) || 3730d17af505SAndy Lutomirski vma_is_stack_for_current(vma))) { 37316b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 37326b6bc620SStephen Smalley sid, sid, SECCLASS_PROCESS, 3733be0554c9SStephen Smalley PROCESS__EXECSTACK, NULL); 3734db4c9641SStephen Smalley } else if (vma->vm_file && vma->anon_vma) { 3735db4c9641SStephen Smalley /* 3736db4c9641SStephen Smalley * We are making executable a file mapping that has 3737db4c9641SStephen Smalley * had some COW done. Since pages might have been 3738db4c9641SStephen Smalley * written, check ability to execute the possibly 3739db4c9641SStephen Smalley * modified content. This typically should only 3740db4c9641SStephen Smalley * occur for text relocations. 3741db4c9641SStephen Smalley */ 3742d84f4f99SDavid Howells rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD); 3743db4c9641SStephen Smalley } 37446b992197SLorenzo Hernandez García-Hierro if (rc) 37456b992197SLorenzo Hernandez García-Hierro return rc; 37466b992197SLorenzo Hernandez García-Hierro } 37471da177e4SLinus Torvalds 37481da177e4SLinus Torvalds return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED); 37491da177e4SLinus Torvalds } 37501da177e4SLinus Torvalds 37511da177e4SLinus Torvalds static int selinux_file_lock(struct file *file, unsigned int cmd) 37521da177e4SLinus Torvalds { 375388e67f3bSDavid Howells const struct cred *cred = current_cred(); 375488e67f3bSDavid Howells 375588e67f3bSDavid Howells return file_has_perm(cred, file, FILE__LOCK); 37561da177e4SLinus Torvalds } 37571da177e4SLinus Torvalds 37581da177e4SLinus Torvalds static int selinux_file_fcntl(struct file *file, unsigned int cmd, 37591da177e4SLinus Torvalds unsigned long arg) 37601da177e4SLinus Torvalds { 376188e67f3bSDavid Howells const struct cred *cred = current_cred(); 37621da177e4SLinus Torvalds int err = 0; 37631da177e4SLinus Torvalds 37641da177e4SLinus Torvalds switch (cmd) { 37651da177e4SLinus Torvalds case F_SETFL: 37661da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) { 376788e67f3bSDavid Howells err = file_has_perm(cred, file, FILE__WRITE); 37681da177e4SLinus Torvalds break; 37691da177e4SLinus Torvalds } 37701da177e4SLinus Torvalds /* fall through */ 37711da177e4SLinus Torvalds case F_SETOWN: 37721da177e4SLinus Torvalds case F_SETSIG: 37731da177e4SLinus Torvalds case F_GETFL: 37741da177e4SLinus Torvalds case F_GETOWN: 37751da177e4SLinus Torvalds case F_GETSIG: 37761d151c33SCyrill Gorcunov case F_GETOWNER_UIDS: 37771da177e4SLinus Torvalds /* Just check FD__USE permission */ 377888e67f3bSDavid Howells err = file_has_perm(cred, file, 0); 37791da177e4SLinus Torvalds break; 37801da177e4SLinus Torvalds case F_GETLK: 37811da177e4SLinus Torvalds case F_SETLK: 37821da177e4SLinus Torvalds case F_SETLKW: 37830d3f7a2dSJeff Layton case F_OFD_GETLK: 37840d3f7a2dSJeff Layton case F_OFD_SETLK: 37850d3f7a2dSJeff Layton case F_OFD_SETLKW: 37861da177e4SLinus Torvalds #if BITS_PER_LONG == 32 37871da177e4SLinus Torvalds case F_GETLK64: 37881da177e4SLinus Torvalds case F_SETLK64: 37891da177e4SLinus Torvalds case F_SETLKW64: 37901da177e4SLinus Torvalds #endif 379188e67f3bSDavid Howells err = file_has_perm(cred, file, FILE__LOCK); 37921da177e4SLinus Torvalds break; 37931da177e4SLinus Torvalds } 37941da177e4SLinus Torvalds 37951da177e4SLinus Torvalds return err; 37961da177e4SLinus Torvalds } 37971da177e4SLinus Torvalds 3798e0b93eddSJeff Layton static void selinux_file_set_fowner(struct file *file) 37991da177e4SLinus Torvalds { 38001da177e4SLinus Torvalds struct file_security_struct *fsec; 38011da177e4SLinus Torvalds 3802bb6c6b02SCasey Schaufler fsec = selinux_file(file); 3803275bb41eSDavid Howells fsec->fown_sid = current_sid(); 38041da177e4SLinus Torvalds } 38051da177e4SLinus Torvalds 38061da177e4SLinus Torvalds static int selinux_file_send_sigiotask(struct task_struct *tsk, 38071da177e4SLinus Torvalds struct fown_struct *fown, int signum) 38081da177e4SLinus Torvalds { 38091da177e4SLinus Torvalds struct file *file; 381065c90bcaSStephen Smalley u32 sid = task_sid(tsk); 38111da177e4SLinus Torvalds u32 perm; 38121da177e4SLinus Torvalds struct file_security_struct *fsec; 38131da177e4SLinus Torvalds 38141da177e4SLinus Torvalds /* struct fown_struct is never outside the context of a struct file */ 3815b385a144SRobert P. J. Day file = container_of(fown, struct file, f_owner); 38161da177e4SLinus Torvalds 3817bb6c6b02SCasey Schaufler fsec = selinux_file(file); 38181da177e4SLinus Torvalds 38191da177e4SLinus Torvalds if (!signum) 38201da177e4SLinus Torvalds perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */ 38211da177e4SLinus Torvalds else 38221da177e4SLinus Torvalds perm = signal_to_av(signum); 38231da177e4SLinus Torvalds 38246b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 38256b6bc620SStephen Smalley fsec->fown_sid, sid, 38261da177e4SLinus Torvalds SECCLASS_PROCESS, perm, NULL); 38271da177e4SLinus Torvalds } 38281da177e4SLinus Torvalds 38291da177e4SLinus Torvalds static int selinux_file_receive(struct file *file) 38301da177e4SLinus Torvalds { 383188e67f3bSDavid Howells const struct cred *cred = current_cred(); 383288e67f3bSDavid Howells 383388e67f3bSDavid Howells return file_has_perm(cred, file, file_to_av(file)); 38341da177e4SLinus Torvalds } 38351da177e4SLinus Torvalds 383694817692SAl Viro static int selinux_file_open(struct file *file) 3837788e7dd4SYuichi Nakamura { 3838788e7dd4SYuichi Nakamura struct file_security_struct *fsec; 3839788e7dd4SYuichi Nakamura struct inode_security_struct *isec; 3840d84f4f99SDavid Howells 3841bb6c6b02SCasey Schaufler fsec = selinux_file(file); 384283da53c5SAndreas Gruenbacher isec = inode_security(file_inode(file)); 3843788e7dd4SYuichi Nakamura /* 3844788e7dd4SYuichi Nakamura * Save inode label and policy sequence number 3845788e7dd4SYuichi Nakamura * at open-time so that selinux_file_permission 3846788e7dd4SYuichi Nakamura * can determine whether revalidation is necessary. 3847788e7dd4SYuichi Nakamura * Task label is already saved in the file security 3848788e7dd4SYuichi Nakamura * struct as its SID. 3849788e7dd4SYuichi Nakamura */ 3850788e7dd4SYuichi Nakamura fsec->isid = isec->sid; 38516b6bc620SStephen Smalley fsec->pseqno = avc_policy_seqno(&selinux_state); 3852788e7dd4SYuichi Nakamura /* 3853788e7dd4SYuichi Nakamura * Since the inode label or policy seqno may have changed 3854788e7dd4SYuichi Nakamura * between the selinux_inode_permission check and the saving 3855788e7dd4SYuichi Nakamura * of state above, recheck that access is still permitted. 3856788e7dd4SYuichi Nakamura * Otherwise, access might never be revalidated against the 3857788e7dd4SYuichi Nakamura * new inode label or new policy. 3858788e7dd4SYuichi Nakamura * This check is not redundant - do not remove. 3859788e7dd4SYuichi Nakamura */ 386094817692SAl Viro return file_path_has_perm(file->f_cred, file, open_file_to_av(file)); 3861788e7dd4SYuichi Nakamura } 3862788e7dd4SYuichi Nakamura 38631da177e4SLinus Torvalds /* task security operations */ 38641da177e4SLinus Torvalds 3865a79be238STetsuo Handa static int selinux_task_alloc(struct task_struct *task, 3866a79be238STetsuo Handa unsigned long clone_flags) 38671da177e4SLinus Torvalds { 3868be0554c9SStephen Smalley u32 sid = current_sid(); 3869be0554c9SStephen Smalley 38706b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 38716b6bc620SStephen Smalley sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL); 38721da177e4SLinus Torvalds } 38731da177e4SLinus Torvalds 3874f1752eecSDavid Howells /* 3875d84f4f99SDavid Howells * prepare a new set of credentials for modification 3876d84f4f99SDavid Howells */ 3877d84f4f99SDavid Howells static int selinux_cred_prepare(struct cred *new, const struct cred *old, 3878d84f4f99SDavid Howells gfp_t gfp) 3879d84f4f99SDavid Howells { 3880bbd3662aSCasey Schaufler const struct task_security_struct *old_tsec = selinux_cred(old); 3881bbd3662aSCasey Schaufler struct task_security_struct *tsec = selinux_cred(new); 3882d84f4f99SDavid Howells 3883bbd3662aSCasey Schaufler *tsec = *old_tsec; 3884d84f4f99SDavid Howells return 0; 3885d84f4f99SDavid Howells } 3886d84f4f99SDavid Howells 3887d84f4f99SDavid Howells /* 3888ee18d64cSDavid Howells * transfer the SELinux data to a blank set of creds 3889ee18d64cSDavid Howells */ 3890ee18d64cSDavid Howells static void selinux_cred_transfer(struct cred *new, const struct cred *old) 3891ee18d64cSDavid Howells { 38920c6cfa62SCasey Schaufler const struct task_security_struct *old_tsec = selinux_cred(old); 38930c6cfa62SCasey Schaufler struct task_security_struct *tsec = selinux_cred(new); 3894ee18d64cSDavid Howells 3895ee18d64cSDavid Howells *tsec = *old_tsec; 3896ee18d64cSDavid Howells } 3897ee18d64cSDavid Howells 38983ec30113SMatthew Garrett static void selinux_cred_getsecid(const struct cred *c, u32 *secid) 38993ec30113SMatthew Garrett { 39003ec30113SMatthew Garrett *secid = cred_sid(c); 39013ec30113SMatthew Garrett } 39023ec30113SMatthew Garrett 3903ee18d64cSDavid Howells /* 39043a3b7ce9SDavid Howells * set the security data for a kernel service 39053a3b7ce9SDavid Howells * - all the creation contexts are set to unlabelled 39063a3b7ce9SDavid Howells */ 39073a3b7ce9SDavid Howells static int selinux_kernel_act_as(struct cred *new, u32 secid) 39083a3b7ce9SDavid Howells { 39090c6cfa62SCasey Schaufler struct task_security_struct *tsec = selinux_cred(new); 39103a3b7ce9SDavid Howells u32 sid = current_sid(); 39113a3b7ce9SDavid Howells int ret; 39123a3b7ce9SDavid Howells 39136b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 39146b6bc620SStephen Smalley sid, secid, 39153a3b7ce9SDavid Howells SECCLASS_KERNEL_SERVICE, 39163a3b7ce9SDavid Howells KERNEL_SERVICE__USE_AS_OVERRIDE, 39173a3b7ce9SDavid Howells NULL); 39183a3b7ce9SDavid Howells if (ret == 0) { 39193a3b7ce9SDavid Howells tsec->sid = secid; 39203a3b7ce9SDavid Howells tsec->create_sid = 0; 39213a3b7ce9SDavid Howells tsec->keycreate_sid = 0; 39223a3b7ce9SDavid Howells tsec->sockcreate_sid = 0; 39233a3b7ce9SDavid Howells } 39243a3b7ce9SDavid Howells return ret; 39253a3b7ce9SDavid Howells } 39263a3b7ce9SDavid Howells 39273a3b7ce9SDavid Howells /* 39283a3b7ce9SDavid Howells * set the file creation context in a security record to the same as the 39293a3b7ce9SDavid Howells * objective context of the specified inode 39303a3b7ce9SDavid Howells */ 39313a3b7ce9SDavid Howells static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode) 39323a3b7ce9SDavid Howells { 393383da53c5SAndreas Gruenbacher struct inode_security_struct *isec = inode_security(inode); 39340c6cfa62SCasey Schaufler struct task_security_struct *tsec = selinux_cred(new); 39353a3b7ce9SDavid Howells u32 sid = current_sid(); 39363a3b7ce9SDavid Howells int ret; 39373a3b7ce9SDavid Howells 39386b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 39396b6bc620SStephen Smalley sid, isec->sid, 39403a3b7ce9SDavid Howells SECCLASS_KERNEL_SERVICE, 39413a3b7ce9SDavid Howells KERNEL_SERVICE__CREATE_FILES_AS, 39423a3b7ce9SDavid Howells NULL); 39433a3b7ce9SDavid Howells 39443a3b7ce9SDavid Howells if (ret == 0) 39453a3b7ce9SDavid Howells tsec->create_sid = isec->sid; 3946ef57471aSDavid Howells return ret; 39473a3b7ce9SDavid Howells } 39483a3b7ce9SDavid Howells 3949dd8dbf2eSEric Paris static int selinux_kernel_module_request(char *kmod_name) 395025354c4fSEric Paris { 3951dd8dbf2eSEric Paris struct common_audit_data ad; 3952dd8dbf2eSEric Paris 395350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_KMOD; 3954dd8dbf2eSEric Paris ad.u.kmod_name = kmod_name; 3955dd8dbf2eSEric Paris 39566b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 39576b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM, 3958dd8dbf2eSEric Paris SYSTEM__MODULE_REQUEST, &ad); 395925354c4fSEric Paris } 396025354c4fSEric Paris 396161d612eaSJeff Vander Stoep static int selinux_kernel_module_from_file(struct file *file) 396261d612eaSJeff Vander Stoep { 396361d612eaSJeff Vander Stoep struct common_audit_data ad; 396461d612eaSJeff Vander Stoep struct inode_security_struct *isec; 396561d612eaSJeff Vander Stoep struct file_security_struct *fsec; 396661d612eaSJeff Vander Stoep u32 sid = current_sid(); 396761d612eaSJeff Vander Stoep int rc; 396861d612eaSJeff Vander Stoep 396961d612eaSJeff Vander Stoep /* init_module */ 397061d612eaSJeff Vander Stoep if (file == NULL) 39716b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 39726b6bc620SStephen Smalley sid, sid, SECCLASS_SYSTEM, 397361d612eaSJeff Vander Stoep SYSTEM__MODULE_LOAD, NULL); 397461d612eaSJeff Vander Stoep 397561d612eaSJeff Vander Stoep /* finit_module */ 397620cdef8dSPaul Moore 397743af5de7SVivek Goyal ad.type = LSM_AUDIT_DATA_FILE; 397843af5de7SVivek Goyal ad.u.file = file; 397961d612eaSJeff Vander Stoep 3980bb6c6b02SCasey Schaufler fsec = selinux_file(file); 398161d612eaSJeff Vander Stoep if (sid != fsec->sid) { 39826b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 39836b6bc620SStephen Smalley sid, fsec->sid, SECCLASS_FD, FD__USE, &ad); 398461d612eaSJeff Vander Stoep if (rc) 398561d612eaSJeff Vander Stoep return rc; 398661d612eaSJeff Vander Stoep } 398761d612eaSJeff Vander Stoep 398820cdef8dSPaul Moore isec = inode_security(file_inode(file)); 39896b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 39906b6bc620SStephen Smalley sid, isec->sid, SECCLASS_SYSTEM, 399161d612eaSJeff Vander Stoep SYSTEM__MODULE_LOAD, &ad); 399261d612eaSJeff Vander Stoep } 399361d612eaSJeff Vander Stoep 399461d612eaSJeff Vander Stoep static int selinux_kernel_read_file(struct file *file, 399561d612eaSJeff Vander Stoep enum kernel_read_file_id id) 399661d612eaSJeff Vander Stoep { 399761d612eaSJeff Vander Stoep int rc = 0; 399861d612eaSJeff Vander Stoep 399961d612eaSJeff Vander Stoep switch (id) { 400061d612eaSJeff Vander Stoep case READING_MODULE: 400161d612eaSJeff Vander Stoep rc = selinux_kernel_module_from_file(file); 400261d612eaSJeff Vander Stoep break; 400361d612eaSJeff Vander Stoep default: 400461d612eaSJeff Vander Stoep break; 400561d612eaSJeff Vander Stoep } 400661d612eaSJeff Vander Stoep 400761d612eaSJeff Vander Stoep return rc; 400861d612eaSJeff Vander Stoep } 400961d612eaSJeff Vander Stoep 4010c77b8cdfSMimi Zohar static int selinux_kernel_load_data(enum kernel_load_data_id id) 4011c77b8cdfSMimi Zohar { 4012c77b8cdfSMimi Zohar int rc = 0; 4013c77b8cdfSMimi Zohar 4014c77b8cdfSMimi Zohar switch (id) { 4015c77b8cdfSMimi Zohar case LOADING_MODULE: 4016c77b8cdfSMimi Zohar rc = selinux_kernel_module_from_file(NULL); 4017c77b8cdfSMimi Zohar default: 4018c77b8cdfSMimi Zohar break; 4019c77b8cdfSMimi Zohar } 4020c77b8cdfSMimi Zohar 4021c77b8cdfSMimi Zohar return rc; 4022c77b8cdfSMimi Zohar } 4023c77b8cdfSMimi Zohar 40241da177e4SLinus Torvalds static int selinux_task_setpgid(struct task_struct *p, pid_t pgid) 40251da177e4SLinus Torvalds { 40266b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 40276b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 4028be0554c9SStephen Smalley PROCESS__SETPGID, NULL); 40291da177e4SLinus Torvalds } 40301da177e4SLinus Torvalds 40311da177e4SLinus Torvalds static int selinux_task_getpgid(struct task_struct *p) 40321da177e4SLinus Torvalds { 40336b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 40346b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 4035be0554c9SStephen Smalley PROCESS__GETPGID, NULL); 40361da177e4SLinus Torvalds } 40371da177e4SLinus Torvalds 40381da177e4SLinus Torvalds static int selinux_task_getsid(struct task_struct *p) 40391da177e4SLinus Torvalds { 40406b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 40416b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 4042be0554c9SStephen Smalley PROCESS__GETSESSION, NULL); 40431da177e4SLinus Torvalds } 40441da177e4SLinus Torvalds 4045f9008e4cSDavid Quigley static void selinux_task_getsecid(struct task_struct *p, u32 *secid) 4046f9008e4cSDavid Quigley { 4047275bb41eSDavid Howells *secid = task_sid(p); 4048f9008e4cSDavid Quigley } 4049f9008e4cSDavid Quigley 40501da177e4SLinus Torvalds static int selinux_task_setnice(struct task_struct *p, int nice) 40511da177e4SLinus Torvalds { 40526b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 40536b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 4054be0554c9SStephen Smalley PROCESS__SETSCHED, NULL); 40551da177e4SLinus Torvalds } 40561da177e4SLinus Torvalds 405703e68060SJames Morris static int selinux_task_setioprio(struct task_struct *p, int ioprio) 405803e68060SJames Morris { 40596b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 40606b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 4061be0554c9SStephen Smalley PROCESS__SETSCHED, NULL); 406203e68060SJames Morris } 406303e68060SJames Morris 4064a1836a42SDavid Quigley static int selinux_task_getioprio(struct task_struct *p) 4065a1836a42SDavid Quigley { 40666b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 40676b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 4068be0554c9SStephen Smalley PROCESS__GETSCHED, NULL); 4069a1836a42SDavid Quigley } 4070a1836a42SDavid Quigley 40714298555dSCorentin LABBE static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred, 4072791ec491SStephen Smalley unsigned int flags) 4073791ec491SStephen Smalley { 4074791ec491SStephen Smalley u32 av = 0; 4075791ec491SStephen Smalley 407684e6885eSStephen Smalley if (!flags) 407784e6885eSStephen Smalley return 0; 4078791ec491SStephen Smalley if (flags & LSM_PRLIMIT_WRITE) 4079791ec491SStephen Smalley av |= PROCESS__SETRLIMIT; 4080791ec491SStephen Smalley if (flags & LSM_PRLIMIT_READ) 4081791ec491SStephen Smalley av |= PROCESS__GETRLIMIT; 40826b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 40836b6bc620SStephen Smalley cred_sid(cred), cred_sid(tcred), 4084791ec491SStephen Smalley SECCLASS_PROCESS, av, NULL); 4085791ec491SStephen Smalley } 4086791ec491SStephen Smalley 40878fd00b4dSJiri Slaby static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource, 40888fd00b4dSJiri Slaby struct rlimit *new_rlim) 40891da177e4SLinus Torvalds { 40908fd00b4dSJiri Slaby struct rlimit *old_rlim = p->signal->rlim + resource; 40911da177e4SLinus Torvalds 40921da177e4SLinus Torvalds /* Control the ability to change the hard limit (whether 40931da177e4SLinus Torvalds lowering or raising it), so that the hard limit can 40941da177e4SLinus Torvalds later be used as a safe reset point for the soft limit 4095d84f4f99SDavid Howells upon context transitions. See selinux_bprm_committing_creds. */ 40961da177e4SLinus Torvalds if (old_rlim->rlim_max != new_rlim->rlim_max) 40976b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 40986b6bc620SStephen Smalley current_sid(), task_sid(p), 4099be0554c9SStephen Smalley SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL); 41001da177e4SLinus Torvalds 41011da177e4SLinus Torvalds return 0; 41021da177e4SLinus Torvalds } 41031da177e4SLinus Torvalds 4104b0ae1981SKOSAKI Motohiro static int selinux_task_setscheduler(struct task_struct *p) 41051da177e4SLinus Torvalds { 41066b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 41076b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 4108be0554c9SStephen Smalley PROCESS__SETSCHED, NULL); 41091da177e4SLinus Torvalds } 41101da177e4SLinus Torvalds 41111da177e4SLinus Torvalds static int selinux_task_getscheduler(struct task_struct *p) 41121da177e4SLinus Torvalds { 41136b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 41146b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 4115be0554c9SStephen Smalley PROCESS__GETSCHED, NULL); 41161da177e4SLinus Torvalds } 41171da177e4SLinus Torvalds 411835601547SDavid Quigley static int selinux_task_movememory(struct task_struct *p) 411935601547SDavid Quigley { 41206b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 41216b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 4122be0554c9SStephen Smalley PROCESS__SETSCHED, NULL); 412335601547SDavid Quigley } 412435601547SDavid Quigley 4125ae7795bcSEric W. Biederman static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info, 41266b4f3d01SStephen Smalley int sig, const struct cred *cred) 41271da177e4SLinus Torvalds { 41286b4f3d01SStephen Smalley u32 secid; 41291da177e4SLinus Torvalds u32 perm; 41301da177e4SLinus Torvalds 41311da177e4SLinus Torvalds if (!sig) 41321da177e4SLinus Torvalds perm = PROCESS__SIGNULL; /* null signal; existence test */ 41331da177e4SLinus Torvalds else 41341da177e4SLinus Torvalds perm = signal_to_av(sig); 41356b4f3d01SStephen Smalley if (!cred) 4136be0554c9SStephen Smalley secid = current_sid(); 41376b4f3d01SStephen Smalley else 41386b4f3d01SStephen Smalley secid = cred_sid(cred); 41396b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 41406b6bc620SStephen Smalley secid, task_sid(p), SECCLASS_PROCESS, perm, NULL); 41411da177e4SLinus Torvalds } 41421da177e4SLinus Torvalds 41431da177e4SLinus Torvalds static void selinux_task_to_inode(struct task_struct *p, 41441da177e4SLinus Torvalds struct inode *inode) 41451da177e4SLinus Torvalds { 414680788c22SCasey Schaufler struct inode_security_struct *isec = selinux_inode(inode); 4147275bb41eSDavid Howells u32 sid = task_sid(p); 41481da177e4SLinus Torvalds 41499287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 4150db978da8SAndreas Gruenbacher isec->sclass = inode_mode_to_security_class(inode->i_mode); 4151275bb41eSDavid Howells isec->sid = sid; 41526f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 41539287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 41541da177e4SLinus Torvalds } 41551da177e4SLinus Torvalds 41561da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 415767f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv4(struct sk_buff *skb, 41582bf49690SThomas Liu struct common_audit_data *ad, u8 *proto) 41591da177e4SLinus Torvalds { 41601da177e4SLinus Torvalds int offset, ihlen, ret = -EINVAL; 41611da177e4SLinus Torvalds struct iphdr _iph, *ih; 41621da177e4SLinus Torvalds 4163bbe735e4SArnaldo Carvalho de Melo offset = skb_network_offset(skb); 41641da177e4SLinus Torvalds ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph); 41651da177e4SLinus Torvalds if (ih == NULL) 41661da177e4SLinus Torvalds goto out; 41671da177e4SLinus Torvalds 41681da177e4SLinus Torvalds ihlen = ih->ihl * 4; 41691da177e4SLinus Torvalds if (ihlen < sizeof(_iph)) 41701da177e4SLinus Torvalds goto out; 41711da177e4SLinus Torvalds 417248c62af6SEric Paris ad->u.net->v4info.saddr = ih->saddr; 417348c62af6SEric Paris ad->u.net->v4info.daddr = ih->daddr; 41741da177e4SLinus Torvalds ret = 0; 41751da177e4SLinus Torvalds 417667f83cbfSVenkat Yekkirala if (proto) 417767f83cbfSVenkat Yekkirala *proto = ih->protocol; 417867f83cbfSVenkat Yekkirala 41791da177e4SLinus Torvalds switch (ih->protocol) { 41801da177e4SLinus Torvalds case IPPROTO_TCP: { 41811da177e4SLinus Torvalds struct tcphdr _tcph, *th; 41821da177e4SLinus Torvalds 41831da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 41841da177e4SLinus Torvalds break; 41851da177e4SLinus Torvalds 41861da177e4SLinus Torvalds offset += ihlen; 41871da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 41881da177e4SLinus Torvalds if (th == NULL) 41891da177e4SLinus Torvalds break; 41901da177e4SLinus Torvalds 419148c62af6SEric Paris ad->u.net->sport = th->source; 419248c62af6SEric Paris ad->u.net->dport = th->dest; 41931da177e4SLinus Torvalds break; 41941da177e4SLinus Torvalds } 41951da177e4SLinus Torvalds 41961da177e4SLinus Torvalds case IPPROTO_UDP: { 41971da177e4SLinus Torvalds struct udphdr _udph, *uh; 41981da177e4SLinus Torvalds 41991da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 42001da177e4SLinus Torvalds break; 42011da177e4SLinus Torvalds 42021da177e4SLinus Torvalds offset += ihlen; 42031da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 42041da177e4SLinus Torvalds if (uh == NULL) 42051da177e4SLinus Torvalds break; 42061da177e4SLinus Torvalds 420748c62af6SEric Paris ad->u.net->sport = uh->source; 420848c62af6SEric Paris ad->u.net->dport = uh->dest; 42091da177e4SLinus Torvalds break; 42101da177e4SLinus Torvalds } 42111da177e4SLinus Torvalds 42122ee92d46SJames Morris case IPPROTO_DCCP: { 42132ee92d46SJames Morris struct dccp_hdr _dccph, *dh; 42142ee92d46SJames Morris 42152ee92d46SJames Morris if (ntohs(ih->frag_off) & IP_OFFSET) 42162ee92d46SJames Morris break; 42172ee92d46SJames Morris 42182ee92d46SJames Morris offset += ihlen; 42192ee92d46SJames Morris dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); 42202ee92d46SJames Morris if (dh == NULL) 42212ee92d46SJames Morris break; 42222ee92d46SJames Morris 422348c62af6SEric Paris ad->u.net->sport = dh->dccph_sport; 422448c62af6SEric Paris ad->u.net->dport = dh->dccph_dport; 42252ee92d46SJames Morris break; 42262ee92d46SJames Morris } 42272ee92d46SJames Morris 4228d452930fSRichard Haines #if IS_ENABLED(CONFIG_IP_SCTP) 4229d452930fSRichard Haines case IPPROTO_SCTP: { 4230d452930fSRichard Haines struct sctphdr _sctph, *sh; 4231d452930fSRichard Haines 4232d452930fSRichard Haines if (ntohs(ih->frag_off) & IP_OFFSET) 4233d452930fSRichard Haines break; 4234d452930fSRichard Haines 4235d452930fSRichard Haines offset += ihlen; 4236d452930fSRichard Haines sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph); 4237d452930fSRichard Haines if (sh == NULL) 4238d452930fSRichard Haines break; 4239d452930fSRichard Haines 4240d452930fSRichard Haines ad->u.net->sport = sh->source; 4241d452930fSRichard Haines ad->u.net->dport = sh->dest; 4242d452930fSRichard Haines break; 4243d452930fSRichard Haines } 4244d452930fSRichard Haines #endif 42451da177e4SLinus Torvalds default: 42461da177e4SLinus Torvalds break; 42471da177e4SLinus Torvalds } 42481da177e4SLinus Torvalds out: 42491da177e4SLinus Torvalds return ret; 42501da177e4SLinus Torvalds } 42511da177e4SLinus Torvalds 42521a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 42531da177e4SLinus Torvalds 42541da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 425567f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv6(struct sk_buff *skb, 42562bf49690SThomas Liu struct common_audit_data *ad, u8 *proto) 42571da177e4SLinus Torvalds { 42581da177e4SLinus Torvalds u8 nexthdr; 42591da177e4SLinus Torvalds int ret = -EINVAL, offset; 42601da177e4SLinus Torvalds struct ipv6hdr _ipv6h, *ip6; 426175f2811cSJesse Gross __be16 frag_off; 42621da177e4SLinus Torvalds 4263bbe735e4SArnaldo Carvalho de Melo offset = skb_network_offset(skb); 42641da177e4SLinus Torvalds ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h); 42651da177e4SLinus Torvalds if (ip6 == NULL) 42661da177e4SLinus Torvalds goto out; 42671da177e4SLinus Torvalds 426848c62af6SEric Paris ad->u.net->v6info.saddr = ip6->saddr; 426948c62af6SEric Paris ad->u.net->v6info.daddr = ip6->daddr; 42701da177e4SLinus Torvalds ret = 0; 42711da177e4SLinus Torvalds 42721da177e4SLinus Torvalds nexthdr = ip6->nexthdr; 42731da177e4SLinus Torvalds offset += sizeof(_ipv6h); 427475f2811cSJesse Gross offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off); 42751da177e4SLinus Torvalds if (offset < 0) 42761da177e4SLinus Torvalds goto out; 42771da177e4SLinus Torvalds 427867f83cbfSVenkat Yekkirala if (proto) 427967f83cbfSVenkat Yekkirala *proto = nexthdr; 428067f83cbfSVenkat Yekkirala 42811da177e4SLinus Torvalds switch (nexthdr) { 42821da177e4SLinus Torvalds case IPPROTO_TCP: { 42831da177e4SLinus Torvalds struct tcphdr _tcph, *th; 42841da177e4SLinus Torvalds 42851da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 42861da177e4SLinus Torvalds if (th == NULL) 42871da177e4SLinus Torvalds break; 42881da177e4SLinus Torvalds 428948c62af6SEric Paris ad->u.net->sport = th->source; 429048c62af6SEric Paris ad->u.net->dport = th->dest; 42911da177e4SLinus Torvalds break; 42921da177e4SLinus Torvalds } 42931da177e4SLinus Torvalds 42941da177e4SLinus Torvalds case IPPROTO_UDP: { 42951da177e4SLinus Torvalds struct udphdr _udph, *uh; 42961da177e4SLinus Torvalds 42971da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 42981da177e4SLinus Torvalds if (uh == NULL) 42991da177e4SLinus Torvalds break; 43001da177e4SLinus Torvalds 430148c62af6SEric Paris ad->u.net->sport = uh->source; 430248c62af6SEric Paris ad->u.net->dport = uh->dest; 43031da177e4SLinus Torvalds break; 43041da177e4SLinus Torvalds } 43051da177e4SLinus Torvalds 43062ee92d46SJames Morris case IPPROTO_DCCP: { 43072ee92d46SJames Morris struct dccp_hdr _dccph, *dh; 43082ee92d46SJames Morris 43092ee92d46SJames Morris dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); 43102ee92d46SJames Morris if (dh == NULL) 43112ee92d46SJames Morris break; 43122ee92d46SJames Morris 431348c62af6SEric Paris ad->u.net->sport = dh->dccph_sport; 431448c62af6SEric Paris ad->u.net->dport = dh->dccph_dport; 43152ee92d46SJames Morris break; 43162ee92d46SJames Morris } 43172ee92d46SJames Morris 4318d452930fSRichard Haines #if IS_ENABLED(CONFIG_IP_SCTP) 4319d452930fSRichard Haines case IPPROTO_SCTP: { 4320d452930fSRichard Haines struct sctphdr _sctph, *sh; 4321d452930fSRichard Haines 4322d452930fSRichard Haines sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph); 4323d452930fSRichard Haines if (sh == NULL) 4324d452930fSRichard Haines break; 4325d452930fSRichard Haines 4326d452930fSRichard Haines ad->u.net->sport = sh->source; 4327d452930fSRichard Haines ad->u.net->dport = sh->dest; 4328d452930fSRichard Haines break; 4329d452930fSRichard Haines } 4330d452930fSRichard Haines #endif 43311da177e4SLinus Torvalds /* includes fragments */ 43321da177e4SLinus Torvalds default: 43331da177e4SLinus Torvalds break; 43341da177e4SLinus Torvalds } 43351da177e4SLinus Torvalds out: 43361da177e4SLinus Torvalds return ret; 43371da177e4SLinus Torvalds } 43381da177e4SLinus Torvalds 43391da177e4SLinus Torvalds #endif /* IPV6 */ 43401da177e4SLinus Torvalds 43412bf49690SThomas Liu static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad, 4342cf9481e2SDavid Howells char **_addrp, int src, u8 *proto) 43431da177e4SLinus Torvalds { 4344cf9481e2SDavid Howells char *addrp; 4345cf9481e2SDavid Howells int ret; 43461da177e4SLinus Torvalds 434748c62af6SEric Paris switch (ad->u.net->family) { 43481da177e4SLinus Torvalds case PF_INET: 434967f83cbfSVenkat Yekkirala ret = selinux_parse_skb_ipv4(skb, ad, proto); 4350cf9481e2SDavid Howells if (ret) 4351cf9481e2SDavid Howells goto parse_error; 435248c62af6SEric Paris addrp = (char *)(src ? &ad->u.net->v4info.saddr : 435348c62af6SEric Paris &ad->u.net->v4info.daddr); 4354cf9481e2SDavid Howells goto okay; 43551da177e4SLinus Torvalds 43561a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 43571da177e4SLinus Torvalds case PF_INET6: 435867f83cbfSVenkat Yekkirala ret = selinux_parse_skb_ipv6(skb, ad, proto); 4359cf9481e2SDavid Howells if (ret) 4360cf9481e2SDavid Howells goto parse_error; 436148c62af6SEric Paris addrp = (char *)(src ? &ad->u.net->v6info.saddr : 436248c62af6SEric Paris &ad->u.net->v6info.daddr); 4363cf9481e2SDavid Howells goto okay; 43641da177e4SLinus Torvalds #endif /* IPV6 */ 43651da177e4SLinus Torvalds default: 4366cf9481e2SDavid Howells addrp = NULL; 4367cf9481e2SDavid Howells goto okay; 43681da177e4SLinus Torvalds } 43691da177e4SLinus Torvalds 4370cf9481e2SDavid Howells parse_error: 4371c103a91eSpeter enderborg pr_warn( 437271f1cb05SPaul Moore "SELinux: failure in selinux_parse_skb()," 437371f1cb05SPaul Moore " unable to parse packet\n"); 43741da177e4SLinus Torvalds return ret; 4375cf9481e2SDavid Howells 4376cf9481e2SDavid Howells okay: 4377cf9481e2SDavid Howells if (_addrp) 4378cf9481e2SDavid Howells *_addrp = addrp; 4379cf9481e2SDavid Howells return 0; 43801da177e4SLinus Torvalds } 43811da177e4SLinus Torvalds 43824f6a993fSPaul Moore /** 4383220deb96SPaul Moore * selinux_skb_peerlbl_sid - Determine the peer label of a packet 43844f6a993fSPaul Moore * @skb: the packet 438575e22910SPaul Moore * @family: protocol family 4386220deb96SPaul Moore * @sid: the packet's peer label SID 43874f6a993fSPaul Moore * 43884f6a993fSPaul Moore * Description: 4389220deb96SPaul Moore * Check the various different forms of network peer labeling and determine 4390220deb96SPaul Moore * the peer label/SID for the packet; most of the magic actually occurs in 4391220deb96SPaul Moore * the security server function security_net_peersid_cmp(). The function 4392220deb96SPaul Moore * returns zero if the value in @sid is valid (although it may be SECSID_NULL) 4393220deb96SPaul Moore * or -EACCES if @sid is invalid due to inconsistencies with the different 4394220deb96SPaul Moore * peer labels. 43954f6a993fSPaul Moore * 43964f6a993fSPaul Moore */ 4397220deb96SPaul Moore static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid) 43984f6a993fSPaul Moore { 439971f1cb05SPaul Moore int err; 44004f6a993fSPaul Moore u32 xfrm_sid; 44014f6a993fSPaul Moore u32 nlbl_sid; 4402220deb96SPaul Moore u32 nlbl_type; 44034f6a993fSPaul Moore 4404817eff71SPaul Moore err = selinux_xfrm_skb_sid(skb, &xfrm_sid); 4405bed4d7efSPaul Moore if (unlikely(err)) 4406bed4d7efSPaul Moore return -EACCES; 4407bed4d7efSPaul Moore err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid); 4408bed4d7efSPaul Moore if (unlikely(err)) 4409bed4d7efSPaul Moore return -EACCES; 4410220deb96SPaul Moore 4411aa8e712cSStephen Smalley err = security_net_peersid_resolve(&selinux_state, nlbl_sid, 4412aa8e712cSStephen Smalley nlbl_type, xfrm_sid, sid); 441371f1cb05SPaul Moore if (unlikely(err)) { 4414c103a91eSpeter enderborg pr_warn( 441571f1cb05SPaul Moore "SELinux: failure in selinux_skb_peerlbl_sid()," 441671f1cb05SPaul Moore " unable to determine packet's peer label\n"); 4417220deb96SPaul Moore return -EACCES; 441871f1cb05SPaul Moore } 4419220deb96SPaul Moore 4420220deb96SPaul Moore return 0; 44214f6a993fSPaul Moore } 44224f6a993fSPaul Moore 4423446b8024SPaul Moore /** 4424446b8024SPaul Moore * selinux_conn_sid - Determine the child socket label for a connection 4425446b8024SPaul Moore * @sk_sid: the parent socket's SID 4426446b8024SPaul Moore * @skb_sid: the packet's SID 4427446b8024SPaul Moore * @conn_sid: the resulting connection SID 4428446b8024SPaul Moore * 4429446b8024SPaul Moore * If @skb_sid is valid then the user:role:type information from @sk_sid is 4430446b8024SPaul Moore * combined with the MLS information from @skb_sid in order to create 4431446b8024SPaul Moore * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy 4432446b8024SPaul Moore * of @sk_sid. Returns zero on success, negative values on failure. 4433446b8024SPaul Moore * 4434446b8024SPaul Moore */ 4435446b8024SPaul Moore static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid) 4436446b8024SPaul Moore { 4437446b8024SPaul Moore int err = 0; 4438446b8024SPaul Moore 4439446b8024SPaul Moore if (skb_sid != SECSID_NULL) 4440aa8e712cSStephen Smalley err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid, 4441aa8e712cSStephen Smalley conn_sid); 4442446b8024SPaul Moore else 4443446b8024SPaul Moore *conn_sid = sk_sid; 4444446b8024SPaul Moore 4445446b8024SPaul Moore return err; 4446446b8024SPaul Moore } 4447446b8024SPaul Moore 44481da177e4SLinus Torvalds /* socket security operations */ 4449d4f2d978SPaul Moore 44502ad18bdfSHarry Ciao static int socket_sockcreate_sid(const struct task_security_struct *tsec, 44512ad18bdfSHarry Ciao u16 secclass, u32 *socksid) 4452d4f2d978SPaul Moore { 44532ad18bdfSHarry Ciao if (tsec->sockcreate_sid > SECSID_NULL) { 44542ad18bdfSHarry Ciao *socksid = tsec->sockcreate_sid; 44552ad18bdfSHarry Ciao return 0; 44562ad18bdfSHarry Ciao } 44572ad18bdfSHarry Ciao 4458aa8e712cSStephen Smalley return security_transition_sid(&selinux_state, tsec->sid, tsec->sid, 4459aa8e712cSStephen Smalley secclass, NULL, socksid); 4460d4f2d978SPaul Moore } 4461d4f2d978SPaul Moore 4462be0554c9SStephen Smalley static int sock_has_perm(struct sock *sk, u32 perms) 44631da177e4SLinus Torvalds { 4464253bfae6SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 44652bf49690SThomas Liu struct common_audit_data ad; 446648c62af6SEric Paris struct lsm_network_audit net = {0,}; 44671da177e4SLinus Torvalds 4468253bfae6SPaul Moore if (sksec->sid == SECINITSID_KERNEL) 4469253bfae6SPaul Moore return 0; 44701da177e4SLinus Torvalds 447150c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 447248c62af6SEric Paris ad.u.net = &net; 447348c62af6SEric Paris ad.u.net->sk = sk; 44741da177e4SLinus Torvalds 44756b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 44766b6bc620SStephen Smalley current_sid(), sksec->sid, sksec->sclass, perms, 4477be0554c9SStephen Smalley &ad); 44781da177e4SLinus Torvalds } 44791da177e4SLinus Torvalds 44801da177e4SLinus Torvalds static int selinux_socket_create(int family, int type, 44811da177e4SLinus Torvalds int protocol, int kern) 44821da177e4SLinus Torvalds { 44830c6cfa62SCasey Schaufler const struct task_security_struct *tsec = selinux_cred(current_cred()); 4484d4f2d978SPaul Moore u32 newsid; 4485275bb41eSDavid Howells u16 secclass; 44862ad18bdfSHarry Ciao int rc; 44871da177e4SLinus Torvalds 44881da177e4SLinus Torvalds if (kern) 4489d4f2d978SPaul Moore return 0; 44901da177e4SLinus Torvalds 4491275bb41eSDavid Howells secclass = socket_type_to_security_class(family, type, protocol); 44922ad18bdfSHarry Ciao rc = socket_sockcreate_sid(tsec, secclass, &newsid); 44932ad18bdfSHarry Ciao if (rc) 44942ad18bdfSHarry Ciao return rc; 44952ad18bdfSHarry Ciao 44966b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 44976b6bc620SStephen Smalley tsec->sid, newsid, secclass, SOCKET__CREATE, NULL); 44981da177e4SLinus Torvalds } 44991da177e4SLinus Torvalds 45007420ed23SVenkat Yekkirala static int selinux_socket_post_create(struct socket *sock, int family, 45011da177e4SLinus Torvalds int type, int protocol, int kern) 45021da177e4SLinus Torvalds { 45030c6cfa62SCasey Schaufler const struct task_security_struct *tsec = selinux_cred(current_cred()); 45045d226df4SAndreas Gruenbacher struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock)); 4505892c141eSVenkat Yekkirala struct sk_security_struct *sksec; 45069287aed2SAndreas Gruenbacher u16 sclass = socket_type_to_security_class(family, type, protocol); 45079287aed2SAndreas Gruenbacher u32 sid = SECINITSID_KERNEL; 4508275bb41eSDavid Howells int err = 0; 4509275bb41eSDavid Howells 45109287aed2SAndreas Gruenbacher if (!kern) { 45119287aed2SAndreas Gruenbacher err = socket_sockcreate_sid(tsec, sclass, &sid); 45122ad18bdfSHarry Ciao if (err) 45132ad18bdfSHarry Ciao return err; 45142ad18bdfSHarry Ciao } 4515275bb41eSDavid Howells 45169287aed2SAndreas Gruenbacher isec->sclass = sclass; 45179287aed2SAndreas Gruenbacher isec->sid = sid; 45186f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 45191da177e4SLinus Torvalds 4520892c141eSVenkat Yekkirala if (sock->sk) { 4521892c141eSVenkat Yekkirala sksec = sock->sk->sk_security; 45229287aed2SAndreas Gruenbacher sksec->sclass = sclass; 45239287aed2SAndreas Gruenbacher sksec->sid = sid; 4524d452930fSRichard Haines /* Allows detection of the first association on this socket */ 4525d452930fSRichard Haines if (sksec->sclass == SECCLASS_SCTP_SOCKET) 4526d452930fSRichard Haines sksec->sctp_assoc_state = SCTP_ASSOC_UNSET; 4527d452930fSRichard Haines 4528389fb800SPaul Moore err = selinux_netlbl_socket_post_create(sock->sk, family); 4529892c141eSVenkat Yekkirala } 4530892c141eSVenkat Yekkirala 45317420ed23SVenkat Yekkirala return err; 45321da177e4SLinus Torvalds } 45331da177e4SLinus Torvalds 45340b811db2SDavid Herrmann static int selinux_socket_socketpair(struct socket *socka, 45350b811db2SDavid Herrmann struct socket *sockb) 45360b811db2SDavid Herrmann { 45370b811db2SDavid Herrmann struct sk_security_struct *sksec_a = socka->sk->sk_security; 45380b811db2SDavid Herrmann struct sk_security_struct *sksec_b = sockb->sk->sk_security; 45390b811db2SDavid Herrmann 45400b811db2SDavid Herrmann sksec_a->peer_sid = sksec_b->sid; 45410b811db2SDavid Herrmann sksec_b->peer_sid = sksec_a->sid; 45420b811db2SDavid Herrmann 45430b811db2SDavid Herrmann return 0; 45440b811db2SDavid Herrmann } 45450b811db2SDavid Herrmann 45461da177e4SLinus Torvalds /* Range of port numbers used to automatically bind. 45471da177e4SLinus Torvalds Need to determine whether we should perform a name_bind 45481da177e4SLinus Torvalds permission check between the socket and the port number. */ 45491da177e4SLinus Torvalds 45501da177e4SLinus Torvalds static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen) 45511da177e4SLinus Torvalds { 4552253bfae6SPaul Moore struct sock *sk = sock->sk; 45530f8db8ccSAlexey Kodanev struct sk_security_struct *sksec = sk->sk_security; 45541da177e4SLinus Torvalds u16 family; 45551da177e4SLinus Torvalds int err; 45561da177e4SLinus Torvalds 4557be0554c9SStephen Smalley err = sock_has_perm(sk, SOCKET__BIND); 45581da177e4SLinus Torvalds if (err) 45591da177e4SLinus Torvalds goto out; 45601da177e4SLinus Torvalds 4561d452930fSRichard Haines /* If PF_INET or PF_INET6, check name_bind permission for the port. */ 4562253bfae6SPaul Moore family = sk->sk_family; 45631da177e4SLinus Torvalds if (family == PF_INET || family == PF_INET6) { 45641da177e4SLinus Torvalds char *addrp; 45652bf49690SThomas Liu struct common_audit_data ad; 456648c62af6SEric Paris struct lsm_network_audit net = {0,}; 45671da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 45681da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 4569c750e692STetsuo Handa u16 family_sa; 45701da177e4SLinus Torvalds unsigned short snum; 4571e399f982SJames Morris u32 sid, node_perm; 45721da177e4SLinus Torvalds 4573d452930fSRichard Haines /* 4574d452930fSRichard Haines * sctp_bindx(3) calls via selinux_sctp_bind_connect() 4575d452930fSRichard Haines * that validates multiple binding addresses. Because of this 4576d452930fSRichard Haines * need to check address->sa_family as it is possible to have 4577d452930fSRichard Haines * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET. 4578d452930fSRichard Haines */ 4579c750e692STetsuo Handa if (addrlen < offsetofend(struct sockaddr, sa_family)) 4580c750e692STetsuo Handa return -EINVAL; 4581c750e692STetsuo Handa family_sa = address->sa_family; 45820f8db8ccSAlexey Kodanev switch (family_sa) { 45830f8db8ccSAlexey Kodanev case AF_UNSPEC: 458468741a8aSRichard Haines case AF_INET: 458568741a8aSRichard Haines if (addrlen < sizeof(struct sockaddr_in)) 458668741a8aSRichard Haines return -EINVAL; 45871da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 45880f8db8ccSAlexey Kodanev if (family_sa == AF_UNSPEC) { 45890f8db8ccSAlexey Kodanev /* see __inet_bind(), we only want to allow 45900f8db8ccSAlexey Kodanev * AF_UNSPEC if the address is INADDR_ANY 45910f8db8ccSAlexey Kodanev */ 45920f8db8ccSAlexey Kodanev if (addr4->sin_addr.s_addr != htonl(INADDR_ANY)) 45930f8db8ccSAlexey Kodanev goto err_af; 45940f8db8ccSAlexey Kodanev family_sa = AF_INET; 45950f8db8ccSAlexey Kodanev } 45961da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 45971da177e4SLinus Torvalds addrp = (char *)&addr4->sin_addr.s_addr; 459868741a8aSRichard Haines break; 459968741a8aSRichard Haines case AF_INET6: 460068741a8aSRichard Haines if (addrlen < SIN6_LEN_RFC2133) 460168741a8aSRichard Haines return -EINVAL; 46021da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 46031da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 46041da177e4SLinus Torvalds addrp = (char *)&addr6->sin6_addr.s6_addr; 460568741a8aSRichard Haines break; 460668741a8aSRichard Haines default: 46070f8db8ccSAlexey Kodanev goto err_af; 46081da177e4SLinus Torvalds } 46091da177e4SLinus Torvalds 461088b7d370SAlexey Kodanev ad.type = LSM_AUDIT_DATA_NET; 461188b7d370SAlexey Kodanev ad.u.net = &net; 461288b7d370SAlexey Kodanev ad.u.net->sport = htons(snum); 461388b7d370SAlexey Kodanev ad.u.net->family = family_sa; 461488b7d370SAlexey Kodanev 4615227b60f5SStephen Hemminger if (snum) { 4616227b60f5SStephen Hemminger int low, high; 4617227b60f5SStephen Hemminger 46180bbf87d8SEric W. Biederman inet_get_local_port_range(sock_net(sk), &low, &high); 4619227b60f5SStephen Hemminger 462082f31ebfSMaciej Żenczykowski if (inet_port_requires_bind_service(sock_net(sk), snum) || 462182f31ebfSMaciej Żenczykowski snum < low || snum > high) { 46223e112172SPaul Moore err = sel_netport_sid(sk->sk_protocol, 46233e112172SPaul Moore snum, &sid); 46241da177e4SLinus Torvalds if (err) 46251da177e4SLinus Torvalds goto out; 46266b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 46276b6bc620SStephen Smalley sksec->sid, sid, 4628253bfae6SPaul Moore sksec->sclass, 46291da177e4SLinus Torvalds SOCKET__NAME_BIND, &ad); 46301da177e4SLinus Torvalds if (err) 46311da177e4SLinus Torvalds goto out; 46321da177e4SLinus Torvalds } 4633227b60f5SStephen Hemminger } 46341da177e4SLinus Torvalds 4635253bfae6SPaul Moore switch (sksec->sclass) { 463613402580SJames Morris case SECCLASS_TCP_SOCKET: 46371da177e4SLinus Torvalds node_perm = TCP_SOCKET__NODE_BIND; 46381da177e4SLinus Torvalds break; 46391da177e4SLinus Torvalds 464013402580SJames Morris case SECCLASS_UDP_SOCKET: 46411da177e4SLinus Torvalds node_perm = UDP_SOCKET__NODE_BIND; 46421da177e4SLinus Torvalds break; 46431da177e4SLinus Torvalds 46442ee92d46SJames Morris case SECCLASS_DCCP_SOCKET: 46452ee92d46SJames Morris node_perm = DCCP_SOCKET__NODE_BIND; 46462ee92d46SJames Morris break; 46472ee92d46SJames Morris 4648d452930fSRichard Haines case SECCLASS_SCTP_SOCKET: 4649d452930fSRichard Haines node_perm = SCTP_SOCKET__NODE_BIND; 4650d452930fSRichard Haines break; 4651d452930fSRichard Haines 46521da177e4SLinus Torvalds default: 46531da177e4SLinus Torvalds node_perm = RAWIP_SOCKET__NODE_BIND; 46541da177e4SLinus Torvalds break; 46551da177e4SLinus Torvalds } 46561da177e4SLinus Torvalds 465788b7d370SAlexey Kodanev err = sel_netnode_sid(addrp, family_sa, &sid); 46581da177e4SLinus Torvalds if (err) 46591da177e4SLinus Torvalds goto out; 46601da177e4SLinus Torvalds 46610f8db8ccSAlexey Kodanev if (family_sa == AF_INET) 466248c62af6SEric Paris ad.u.net->v4info.saddr = addr4->sin_addr.s_addr; 46631da177e4SLinus Torvalds else 466448c62af6SEric Paris ad.u.net->v6info.saddr = addr6->sin6_addr; 46651da177e4SLinus Torvalds 46666b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 46676b6bc620SStephen Smalley sksec->sid, sid, 4668253bfae6SPaul Moore sksec->sclass, node_perm, &ad); 46691da177e4SLinus Torvalds if (err) 46701da177e4SLinus Torvalds goto out; 46711da177e4SLinus Torvalds } 46721da177e4SLinus Torvalds out: 46731da177e4SLinus Torvalds return err; 46740f8db8ccSAlexey Kodanev err_af: 46750f8db8ccSAlexey Kodanev /* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */ 46760f8db8ccSAlexey Kodanev if (sksec->sclass == SECCLASS_SCTP_SOCKET) 46770f8db8ccSAlexey Kodanev return -EINVAL; 46780f8db8ccSAlexey Kodanev return -EAFNOSUPPORT; 46791da177e4SLinus Torvalds } 46801da177e4SLinus Torvalds 4681d452930fSRichard Haines /* This supports connect(2) and SCTP connect services such as sctp_connectx(3) 4682d61330c6SKees Cook * and sctp_sendmsg(3) as described in Documentation/security/SCTP.rst 4683d452930fSRichard Haines */ 4684d452930fSRichard Haines static int selinux_socket_connect_helper(struct socket *sock, 4685d452930fSRichard Haines struct sockaddr *address, int addrlen) 46861da177e4SLinus Torvalds { 4687014ab19aSPaul Moore struct sock *sk = sock->sk; 4688253bfae6SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 46891da177e4SLinus Torvalds int err; 46901da177e4SLinus Torvalds 4691be0554c9SStephen Smalley err = sock_has_perm(sk, SOCKET__CONNECT); 46921da177e4SLinus Torvalds if (err) 46931da177e4SLinus Torvalds return err; 469405174c95SPaolo Abeni if (addrlen < offsetofend(struct sockaddr, sa_family)) 469505174c95SPaolo Abeni return -EINVAL; 469605174c95SPaolo Abeni 469705174c95SPaolo Abeni /* connect(AF_UNSPEC) has special handling, as it is a documented 469805174c95SPaolo Abeni * way to disconnect the socket 469905174c95SPaolo Abeni */ 470005174c95SPaolo Abeni if (address->sa_family == AF_UNSPEC) 470105174c95SPaolo Abeni return 0; 47021da177e4SLinus Torvalds 47031da177e4SLinus Torvalds /* 4704d452930fSRichard Haines * If a TCP, DCCP or SCTP socket, check name_connect permission 4705d452930fSRichard Haines * for the port. 47061da177e4SLinus Torvalds */ 4707253bfae6SPaul Moore if (sksec->sclass == SECCLASS_TCP_SOCKET || 4708d452930fSRichard Haines sksec->sclass == SECCLASS_DCCP_SOCKET || 4709d452930fSRichard Haines sksec->sclass == SECCLASS_SCTP_SOCKET) { 47102bf49690SThomas Liu struct common_audit_data ad; 471148c62af6SEric Paris struct lsm_network_audit net = {0,}; 47121da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 47131da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 47141da177e4SLinus Torvalds unsigned short snum; 47152ee92d46SJames Morris u32 sid, perm; 47161da177e4SLinus Torvalds 4717d452930fSRichard Haines /* sctp_connectx(3) calls via selinux_sctp_bind_connect() 4718d452930fSRichard Haines * that validates multiple connect addresses. Because of this 4719d452930fSRichard Haines * need to check address->sa_family as it is possible to have 4720d452930fSRichard Haines * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET. 4721d452930fSRichard Haines */ 472268741a8aSRichard Haines switch (address->sa_family) { 472368741a8aSRichard Haines case AF_INET: 47241da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 4725911656f8SStephen Smalley if (addrlen < sizeof(struct sockaddr_in)) 47261da177e4SLinus Torvalds return -EINVAL; 47271da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 472868741a8aSRichard Haines break; 472968741a8aSRichard Haines case AF_INET6: 47301da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 4731911656f8SStephen Smalley if (addrlen < SIN6_LEN_RFC2133) 47321da177e4SLinus Torvalds return -EINVAL; 47331da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 473468741a8aSRichard Haines break; 473568741a8aSRichard Haines default: 473668741a8aSRichard Haines /* Note that SCTP services expect -EINVAL, whereas 473768741a8aSRichard Haines * others expect -EAFNOSUPPORT. 473868741a8aSRichard Haines */ 473968741a8aSRichard Haines if (sksec->sclass == SECCLASS_SCTP_SOCKET) 474068741a8aSRichard Haines return -EINVAL; 474168741a8aSRichard Haines else 474268741a8aSRichard Haines return -EAFNOSUPPORT; 47431da177e4SLinus Torvalds } 47441da177e4SLinus Torvalds 47453e112172SPaul Moore err = sel_netport_sid(sk->sk_protocol, snum, &sid); 47461da177e4SLinus Torvalds if (err) 4747d452930fSRichard Haines return err; 47481da177e4SLinus Torvalds 4749d452930fSRichard Haines switch (sksec->sclass) { 4750d452930fSRichard Haines case SECCLASS_TCP_SOCKET: 4751d452930fSRichard Haines perm = TCP_SOCKET__NAME_CONNECT; 4752d452930fSRichard Haines break; 4753d452930fSRichard Haines case SECCLASS_DCCP_SOCKET: 4754d452930fSRichard Haines perm = DCCP_SOCKET__NAME_CONNECT; 4755d452930fSRichard Haines break; 4756d452930fSRichard Haines case SECCLASS_SCTP_SOCKET: 4757d452930fSRichard Haines perm = SCTP_SOCKET__NAME_CONNECT; 4758d452930fSRichard Haines break; 4759d452930fSRichard Haines } 47602ee92d46SJames Morris 476150c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 476248c62af6SEric Paris ad.u.net = &net; 476348c62af6SEric Paris ad.u.net->dport = htons(snum); 476488b7d370SAlexey Kodanev ad.u.net->family = address->sa_family; 47656b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 47666b6bc620SStephen Smalley sksec->sid, sid, sksec->sclass, perm, &ad); 47671da177e4SLinus Torvalds if (err) 4768d452930fSRichard Haines return err; 47691da177e4SLinus Torvalds } 47701da177e4SLinus Torvalds 4771d452930fSRichard Haines return 0; 4772d452930fSRichard Haines } 4773014ab19aSPaul Moore 4774d452930fSRichard Haines /* Supports connect(2), see comments in selinux_socket_connect_helper() */ 4775d452930fSRichard Haines static int selinux_socket_connect(struct socket *sock, 4776d452930fSRichard Haines struct sockaddr *address, int addrlen) 4777d452930fSRichard Haines { 4778d452930fSRichard Haines int err; 4779d452930fSRichard Haines struct sock *sk = sock->sk; 4780d452930fSRichard Haines 4781d452930fSRichard Haines err = selinux_socket_connect_helper(sock, address, addrlen); 4782d452930fSRichard Haines if (err) 47831da177e4SLinus Torvalds return err; 4784d452930fSRichard Haines 4785d452930fSRichard Haines return selinux_netlbl_socket_connect(sk, address); 47861da177e4SLinus Torvalds } 47871da177e4SLinus Torvalds 47881da177e4SLinus Torvalds static int selinux_socket_listen(struct socket *sock, int backlog) 47891da177e4SLinus Torvalds { 4790be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__LISTEN); 47911da177e4SLinus Torvalds } 47921da177e4SLinus Torvalds 47931da177e4SLinus Torvalds static int selinux_socket_accept(struct socket *sock, struct socket *newsock) 47941da177e4SLinus Torvalds { 47951da177e4SLinus Torvalds int err; 47961da177e4SLinus Torvalds struct inode_security_struct *isec; 47971da177e4SLinus Torvalds struct inode_security_struct *newisec; 47989287aed2SAndreas Gruenbacher u16 sclass; 47999287aed2SAndreas Gruenbacher u32 sid; 48001da177e4SLinus Torvalds 4801be0554c9SStephen Smalley err = sock_has_perm(sock->sk, SOCKET__ACCEPT); 48021da177e4SLinus Torvalds if (err) 48031da177e4SLinus Torvalds return err; 48041da177e4SLinus Torvalds 48055d226df4SAndreas Gruenbacher isec = inode_security_novalidate(SOCK_INODE(sock)); 48069287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 48079287aed2SAndreas Gruenbacher sclass = isec->sclass; 48089287aed2SAndreas Gruenbacher sid = isec->sid; 48099287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 48109287aed2SAndreas Gruenbacher 48119287aed2SAndreas Gruenbacher newisec = inode_security_novalidate(SOCK_INODE(newsock)); 48129287aed2SAndreas Gruenbacher newisec->sclass = sclass; 48139287aed2SAndreas Gruenbacher newisec->sid = sid; 48146f3be9f5SAndreas Gruenbacher newisec->initialized = LABEL_INITIALIZED; 48151da177e4SLinus Torvalds 48161da177e4SLinus Torvalds return 0; 48171da177e4SLinus Torvalds } 48181da177e4SLinus Torvalds 48191da177e4SLinus Torvalds static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg, 48201da177e4SLinus Torvalds int size) 48211da177e4SLinus Torvalds { 4822be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__WRITE); 48231da177e4SLinus Torvalds } 48241da177e4SLinus Torvalds 48251da177e4SLinus Torvalds static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg, 48261da177e4SLinus Torvalds int size, int flags) 48271da177e4SLinus Torvalds { 4828be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__READ); 48291da177e4SLinus Torvalds } 48301da177e4SLinus Torvalds 48311da177e4SLinus Torvalds static int selinux_socket_getsockname(struct socket *sock) 48321da177e4SLinus Torvalds { 4833be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__GETATTR); 48341da177e4SLinus Torvalds } 48351da177e4SLinus Torvalds 48361da177e4SLinus Torvalds static int selinux_socket_getpeername(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_setsockopt(struct socket *sock, int level, int optname) 48421da177e4SLinus Torvalds { 4843f8687afeSPaul Moore int err; 4844f8687afeSPaul Moore 4845be0554c9SStephen Smalley err = sock_has_perm(sock->sk, SOCKET__SETOPT); 4846f8687afeSPaul Moore if (err) 4847f8687afeSPaul Moore return err; 4848f8687afeSPaul Moore 4849f8687afeSPaul Moore return selinux_netlbl_socket_setsockopt(sock, level, optname); 48501da177e4SLinus Torvalds } 48511da177e4SLinus Torvalds 48521da177e4SLinus Torvalds static int selinux_socket_getsockopt(struct socket *sock, int level, 48531da177e4SLinus Torvalds int optname) 48541da177e4SLinus Torvalds { 4855be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__GETOPT); 48561da177e4SLinus Torvalds } 48571da177e4SLinus Torvalds 48581da177e4SLinus Torvalds static int selinux_socket_shutdown(struct socket *sock, int how) 48591da177e4SLinus Torvalds { 4860be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__SHUTDOWN); 48611da177e4SLinus Torvalds } 48621da177e4SLinus Torvalds 48633610cda5SDavid S. Miller static int selinux_socket_unix_stream_connect(struct sock *sock, 48643610cda5SDavid S. Miller struct sock *other, 48651da177e4SLinus Torvalds struct sock *newsk) 48661da177e4SLinus Torvalds { 48673610cda5SDavid S. Miller struct sk_security_struct *sksec_sock = sock->sk_security; 48683610cda5SDavid S. Miller struct sk_security_struct *sksec_other = other->sk_security; 48694d1e2451SPaul Moore struct sk_security_struct *sksec_new = newsk->sk_security; 48702bf49690SThomas Liu struct common_audit_data ad; 487148c62af6SEric Paris struct lsm_network_audit net = {0,}; 48721da177e4SLinus Torvalds int err; 48731da177e4SLinus Torvalds 487450c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 487548c62af6SEric Paris ad.u.net = &net; 487648c62af6SEric Paris ad.u.net->sk = other; 48771da177e4SLinus Torvalds 48786b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 48796b6bc620SStephen Smalley sksec_sock->sid, sksec_other->sid, 48804d1e2451SPaul Moore sksec_other->sclass, 48811da177e4SLinus Torvalds UNIX_STREAM_SOCKET__CONNECTTO, &ad); 48821da177e4SLinus Torvalds if (err) 48831da177e4SLinus Torvalds return err; 48841da177e4SLinus Torvalds 48851da177e4SLinus Torvalds /* server child socket */ 48864d1e2451SPaul Moore sksec_new->peer_sid = sksec_sock->sid; 4887aa8e712cSStephen Smalley err = security_sid_mls_copy(&selinux_state, sksec_other->sid, 4888aa8e712cSStephen Smalley sksec_sock->sid, &sksec_new->sid); 48894d1e2451SPaul Moore if (err) 48904237c75cSVenkat Yekkirala return err; 48914d1e2451SPaul Moore 48924d1e2451SPaul Moore /* connecting socket */ 48934d1e2451SPaul Moore sksec_sock->peer_sid = sksec_new->sid; 48944d1e2451SPaul Moore 48954d1e2451SPaul Moore return 0; 48961da177e4SLinus Torvalds } 48971da177e4SLinus Torvalds 48981da177e4SLinus Torvalds static int selinux_socket_unix_may_send(struct socket *sock, 48991da177e4SLinus Torvalds struct socket *other) 49001da177e4SLinus Torvalds { 4901253bfae6SPaul Moore struct sk_security_struct *ssec = sock->sk->sk_security; 4902253bfae6SPaul Moore struct sk_security_struct *osec = other->sk->sk_security; 49032bf49690SThomas Liu struct common_audit_data ad; 490448c62af6SEric Paris struct lsm_network_audit net = {0,}; 49051da177e4SLinus Torvalds 490650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 490748c62af6SEric Paris ad.u.net = &net; 490848c62af6SEric Paris ad.u.net->sk = other->sk; 49091da177e4SLinus Torvalds 49106b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 49116b6bc620SStephen Smalley ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO, 4912253bfae6SPaul Moore &ad); 49131da177e4SLinus Torvalds } 49141da177e4SLinus Torvalds 4915cbe0d6e8SPaul Moore static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex, 4916cbe0d6e8SPaul Moore char *addrp, u16 family, u32 peer_sid, 49172bf49690SThomas Liu struct common_audit_data *ad) 4918effad8dfSPaul Moore { 4919effad8dfSPaul Moore int err; 4920effad8dfSPaul Moore u32 if_sid; 4921effad8dfSPaul Moore u32 node_sid; 4922effad8dfSPaul Moore 4923cbe0d6e8SPaul Moore err = sel_netif_sid(ns, ifindex, &if_sid); 4924effad8dfSPaul Moore if (err) 4925effad8dfSPaul Moore return err; 49266b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 49276b6bc620SStephen Smalley peer_sid, if_sid, 4928effad8dfSPaul Moore SECCLASS_NETIF, NETIF__INGRESS, ad); 4929effad8dfSPaul Moore if (err) 4930effad8dfSPaul Moore return err; 4931effad8dfSPaul Moore 4932effad8dfSPaul Moore err = sel_netnode_sid(addrp, family, &node_sid); 4933effad8dfSPaul Moore if (err) 4934effad8dfSPaul Moore return err; 49356b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 49366b6bc620SStephen Smalley peer_sid, node_sid, 4937effad8dfSPaul Moore SECCLASS_NODE, NODE__RECVFROM, ad); 4938effad8dfSPaul Moore } 4939effad8dfSPaul Moore 4940220deb96SPaul Moore static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb, 4941d8395c87SPaul Moore u16 family) 4942220deb96SPaul Moore { 4943277d342fSPaul Moore int err = 0; 4944220deb96SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 4945220deb96SPaul Moore u32 sk_sid = sksec->sid; 49462bf49690SThomas Liu struct common_audit_data ad; 494748c62af6SEric Paris struct lsm_network_audit net = {0,}; 4948d8395c87SPaul Moore char *addrp; 4949d8395c87SPaul Moore 495050c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 495148c62af6SEric Paris ad.u.net = &net; 495248c62af6SEric Paris ad.u.net->netif = skb->skb_iif; 495348c62af6SEric Paris ad.u.net->family = family; 4954d8395c87SPaul Moore err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); 4955d8395c87SPaul Moore if (err) 4956d8395c87SPaul Moore return err; 4957220deb96SPaul Moore 495858bfbb51SPaul Moore if (selinux_secmark_enabled()) { 49596b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 49606b6bc620SStephen Smalley sk_sid, skb->secmark, SECCLASS_PACKET, 4961d8395c87SPaul Moore PACKET__RECV, &ad); 4962220deb96SPaul Moore if (err) 4963220deb96SPaul Moore return err; 496458bfbb51SPaul Moore } 4965220deb96SPaul Moore 4966d8395c87SPaul Moore err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad); 4967220deb96SPaul Moore if (err) 4968220deb96SPaul Moore return err; 4969d8395c87SPaul Moore err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad); 4970220deb96SPaul Moore 49714e5ab4cbSJames Morris return err; 49724e5ab4cbSJames Morris } 4973d28d1e08STrent Jaeger 49744e5ab4cbSJames Morris static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) 49754e5ab4cbSJames Morris { 4976220deb96SPaul Moore int err; 49774237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 4978220deb96SPaul Moore u16 family = sk->sk_family; 4979220deb96SPaul Moore u32 sk_sid = sksec->sid; 49802bf49690SThomas Liu struct common_audit_data ad; 498148c62af6SEric Paris struct lsm_network_audit net = {0,}; 4982220deb96SPaul Moore char *addrp; 4983d8395c87SPaul Moore u8 secmark_active; 4984d8395c87SPaul Moore u8 peerlbl_active; 49854e5ab4cbSJames Morris 49864e5ab4cbSJames Morris if (family != PF_INET && family != PF_INET6) 4987220deb96SPaul Moore return 0; 49884e5ab4cbSJames Morris 49894e5ab4cbSJames Morris /* Handle mapped IPv4 packets arriving via IPv6 sockets */ 499087fcd70dSAl Viro if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 49914e5ab4cbSJames Morris family = PF_INET; 49924e5ab4cbSJames Morris 4993d8395c87SPaul Moore /* If any sort of compatibility mode is enabled then handoff processing 4994d8395c87SPaul Moore * to the selinux_sock_rcv_skb_compat() function to deal with the 4995d8395c87SPaul Moore * special handling. We do this in an attempt to keep this function 4996d8395c87SPaul Moore * as fast and as clean as possible. */ 4997aa8e712cSStephen Smalley if (!selinux_policycap_netpeer()) 4998d8395c87SPaul Moore return selinux_sock_rcv_skb_compat(sk, skb, family); 4999d8395c87SPaul Moore 5000d8395c87SPaul Moore secmark_active = selinux_secmark_enabled(); 50012be4d74fSChris PeBenito peerlbl_active = selinux_peerlbl_enabled(); 5002d8395c87SPaul Moore if (!secmark_active && !peerlbl_active) 5003d8395c87SPaul Moore return 0; 5004d8395c87SPaul Moore 500550c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 500648c62af6SEric Paris ad.u.net = &net; 500748c62af6SEric Paris ad.u.net->netif = skb->skb_iif; 500848c62af6SEric Paris ad.u.net->family = family; 5009224dfbd8SPaul Moore err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); 50104e5ab4cbSJames Morris if (err) 5011220deb96SPaul Moore return err; 50124e5ab4cbSJames Morris 5013d8395c87SPaul Moore if (peerlbl_active) { 5014d621d35eSPaul Moore u32 peer_sid; 5015220deb96SPaul Moore 5016220deb96SPaul Moore err = selinux_skb_peerlbl_sid(skb, family, &peer_sid); 5017220deb96SPaul Moore if (err) 5018220deb96SPaul Moore return err; 5019cbe0d6e8SPaul Moore err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif, 5020cbe0d6e8SPaul Moore addrp, family, peer_sid, &ad); 5021dfaebe98SPaul Moore if (err) { 5022a04e71f6SHuw Davies selinux_netlbl_err(skb, family, err, 0); 5023effad8dfSPaul Moore return err; 5024dfaebe98SPaul Moore } 50256b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 50266b6bc620SStephen Smalley sk_sid, peer_sid, SECCLASS_PEER, 5027d621d35eSPaul Moore PEER__RECV, &ad); 502846d01d63SChad Hanson if (err) { 5029a04e71f6SHuw Davies selinux_netlbl_err(skb, family, err, 0); 503046d01d63SChad Hanson return err; 503146d01d63SChad Hanson } 5032d621d35eSPaul Moore } 5033d621d35eSPaul Moore 5034d8395c87SPaul Moore if (secmark_active) { 50356b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 50366b6bc620SStephen Smalley sk_sid, skb->secmark, SECCLASS_PACKET, 5037effad8dfSPaul Moore PACKET__RECV, &ad); 5038effad8dfSPaul Moore if (err) 5039effad8dfSPaul Moore return err; 5040effad8dfSPaul Moore } 5041effad8dfSPaul Moore 5042d621d35eSPaul Moore return err; 50431da177e4SLinus Torvalds } 50441da177e4SLinus Torvalds 50452c7946a7SCatherine Zhang static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval, 50461da177e4SLinus Torvalds int __user *optlen, unsigned len) 50471da177e4SLinus Torvalds { 50481da177e4SLinus Torvalds int err = 0; 50491da177e4SLinus Torvalds char *scontext; 50501da177e4SLinus Torvalds u32 scontext_len; 5051253bfae6SPaul Moore struct sk_security_struct *sksec = sock->sk->sk_security; 50523de4bab5SPaul Moore u32 peer_sid = SECSID_NULL; 50531da177e4SLinus Torvalds 5054253bfae6SPaul Moore if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET || 5055d452930fSRichard Haines sksec->sclass == SECCLASS_TCP_SOCKET || 5056d452930fSRichard Haines sksec->sclass == SECCLASS_SCTP_SOCKET) 5057dd3e7836SEric Paris peer_sid = sksec->peer_sid; 5058253bfae6SPaul Moore if (peer_sid == SECSID_NULL) 5059253bfae6SPaul Moore return -ENOPROTOOPT; 50601da177e4SLinus Torvalds 5061aa8e712cSStephen Smalley err = security_sid_to_context(&selinux_state, peer_sid, &scontext, 5062aa8e712cSStephen Smalley &scontext_len); 50631da177e4SLinus Torvalds if (err) 5064253bfae6SPaul Moore return err; 50651da177e4SLinus Torvalds 50661da177e4SLinus Torvalds if (scontext_len > len) { 50671da177e4SLinus Torvalds err = -ERANGE; 50681da177e4SLinus Torvalds goto out_len; 50691da177e4SLinus Torvalds } 50701da177e4SLinus Torvalds 50711da177e4SLinus Torvalds if (copy_to_user(optval, scontext, scontext_len)) 50721da177e4SLinus Torvalds err = -EFAULT; 50731da177e4SLinus Torvalds 50741da177e4SLinus Torvalds out_len: 50751da177e4SLinus Torvalds if (put_user(scontext_len, optlen)) 50761da177e4SLinus Torvalds err = -EFAULT; 50771da177e4SLinus Torvalds kfree(scontext); 50781da177e4SLinus Torvalds return err; 50791da177e4SLinus Torvalds } 50801da177e4SLinus Torvalds 5081dc49c1f9SCatherine Zhang static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid) 50822c7946a7SCatherine Zhang { 5083dc49c1f9SCatherine Zhang u32 peer_secid = SECSID_NULL; 508475e22910SPaul Moore u16 family; 5085899134f2SPaul Moore struct inode_security_struct *isec; 5086877ce7c1SCatherine Zhang 5087aa862900SPaul Moore if (skb && skb->protocol == htons(ETH_P_IP)) 5088aa862900SPaul Moore family = PF_INET; 5089aa862900SPaul Moore else if (skb && skb->protocol == htons(ETH_P_IPV6)) 5090aa862900SPaul Moore family = PF_INET6; 5091aa862900SPaul Moore else if (sock) 509275e22910SPaul Moore family = sock->sk->sk_family; 509375e22910SPaul Moore else 509475e22910SPaul Moore goto out; 509575e22910SPaul Moore 5096899134f2SPaul Moore if (sock && family == PF_UNIX) { 5097899134f2SPaul Moore isec = inode_security_novalidate(SOCK_INODE(sock)); 5098899134f2SPaul Moore peer_secid = isec->sid; 5099899134f2SPaul Moore } else if (skb) 5100220deb96SPaul Moore selinux_skb_peerlbl_sid(skb, family, &peer_secid); 51012c7946a7SCatherine Zhang 510275e22910SPaul Moore out: 5103dc49c1f9SCatherine Zhang *secid = peer_secid; 510475e22910SPaul Moore if (peer_secid == SECSID_NULL) 510575e22910SPaul Moore return -EINVAL; 510675e22910SPaul Moore return 0; 51072c7946a7SCatherine Zhang } 51082c7946a7SCatherine Zhang 51097d877f3bSAl Viro static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority) 51101da177e4SLinus Torvalds { 511184914b7eSPaul Moore struct sk_security_struct *sksec; 511284914b7eSPaul Moore 511384914b7eSPaul Moore sksec = kzalloc(sizeof(*sksec), priority); 511484914b7eSPaul Moore if (!sksec) 511584914b7eSPaul Moore return -ENOMEM; 511684914b7eSPaul Moore 511784914b7eSPaul Moore sksec->peer_sid = SECINITSID_UNLABELED; 511884914b7eSPaul Moore sksec->sid = SECINITSID_UNLABELED; 51195dee25d0SStephen Smalley sksec->sclass = SECCLASS_SOCKET; 512084914b7eSPaul Moore selinux_netlbl_sk_security_reset(sksec); 512184914b7eSPaul Moore sk->sk_security = sksec; 512284914b7eSPaul Moore 512384914b7eSPaul Moore return 0; 51241da177e4SLinus Torvalds } 51251da177e4SLinus Torvalds 51261da177e4SLinus Torvalds static void selinux_sk_free_security(struct sock *sk) 51271da177e4SLinus Torvalds { 512884914b7eSPaul Moore struct sk_security_struct *sksec = sk->sk_security; 512984914b7eSPaul Moore 513084914b7eSPaul Moore sk->sk_security = NULL; 513184914b7eSPaul Moore selinux_netlbl_sk_security_free(sksec); 513284914b7eSPaul Moore kfree(sksec); 51331da177e4SLinus Torvalds } 51341da177e4SLinus Torvalds 5135892c141eSVenkat Yekkirala static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk) 5136892c141eSVenkat Yekkirala { 5137dd3e7836SEric Paris struct sk_security_struct *sksec = sk->sk_security; 5138dd3e7836SEric Paris struct sk_security_struct *newsksec = newsk->sk_security; 5139892c141eSVenkat Yekkirala 5140dd3e7836SEric Paris newsksec->sid = sksec->sid; 5141dd3e7836SEric Paris newsksec->peer_sid = sksec->peer_sid; 5142dd3e7836SEric Paris newsksec->sclass = sksec->sclass; 514399f59ed0SPaul Moore 5144dd3e7836SEric Paris selinux_netlbl_sk_security_reset(newsksec); 5145892c141eSVenkat Yekkirala } 5146892c141eSVenkat Yekkirala 5147beb8d13bSVenkat Yekkirala static void selinux_sk_getsecid(struct sock *sk, u32 *secid) 5148d28d1e08STrent Jaeger { 5149d28d1e08STrent Jaeger if (!sk) 5150beb8d13bSVenkat Yekkirala *secid = SECINITSID_ANY_SOCKET; 5151892c141eSVenkat Yekkirala else { 5152892c141eSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 5153d28d1e08STrent Jaeger 5154beb8d13bSVenkat Yekkirala *secid = sksec->sid; 5155892c141eSVenkat Yekkirala } 5156d28d1e08STrent Jaeger } 5157d28d1e08STrent Jaeger 51589a673e56SAdrian Bunk static void selinux_sock_graft(struct sock *sk, struct socket *parent) 51594237c75cSVenkat Yekkirala { 51605d226df4SAndreas Gruenbacher struct inode_security_struct *isec = 51615d226df4SAndreas Gruenbacher inode_security_novalidate(SOCK_INODE(parent)); 51624237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 51634237c75cSVenkat Yekkirala 51642873ead7SPaul Moore if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 || 51652873ead7SPaul Moore sk->sk_family == PF_UNIX) 51664237c75cSVenkat Yekkirala isec->sid = sksec->sid; 5167220deb96SPaul Moore sksec->sclass = isec->sclass; 51684237c75cSVenkat Yekkirala } 51694237c75cSVenkat Yekkirala 5170d452930fSRichard Haines /* Called whenever SCTP receives an INIT chunk. This happens when an incoming 5171d452930fSRichard Haines * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association 5172d452930fSRichard Haines * already present). 5173d452930fSRichard Haines */ 5174d452930fSRichard Haines static int selinux_sctp_assoc_request(struct sctp_endpoint *ep, 5175d452930fSRichard Haines struct sk_buff *skb) 5176d452930fSRichard Haines { 5177d452930fSRichard Haines struct sk_security_struct *sksec = ep->base.sk->sk_security; 5178d452930fSRichard Haines struct common_audit_data ad; 5179d452930fSRichard Haines struct lsm_network_audit net = {0,}; 5180d452930fSRichard Haines u8 peerlbl_active; 5181d452930fSRichard Haines u32 peer_sid = SECINITSID_UNLABELED; 5182d452930fSRichard Haines u32 conn_sid; 5183d452930fSRichard Haines int err = 0; 5184d452930fSRichard Haines 5185aa8e712cSStephen Smalley if (!selinux_policycap_extsockclass()) 5186d452930fSRichard Haines return 0; 5187d452930fSRichard Haines 5188d452930fSRichard Haines peerlbl_active = selinux_peerlbl_enabled(); 5189d452930fSRichard Haines 5190d452930fSRichard Haines if (peerlbl_active) { 5191d452930fSRichard Haines /* This will return peer_sid = SECSID_NULL if there are 5192d452930fSRichard Haines * no peer labels, see security_net_peersid_resolve(). 5193d452930fSRichard Haines */ 5194d452930fSRichard Haines err = selinux_skb_peerlbl_sid(skb, ep->base.sk->sk_family, 5195d452930fSRichard Haines &peer_sid); 5196d452930fSRichard Haines if (err) 5197d452930fSRichard Haines return err; 5198d452930fSRichard Haines 5199d452930fSRichard Haines if (peer_sid == SECSID_NULL) 5200d452930fSRichard Haines peer_sid = SECINITSID_UNLABELED; 5201d452930fSRichard Haines } 5202d452930fSRichard Haines 5203d452930fSRichard Haines if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) { 5204d452930fSRichard Haines sksec->sctp_assoc_state = SCTP_ASSOC_SET; 5205d452930fSRichard Haines 5206d452930fSRichard Haines /* Here as first association on socket. As the peer SID 5207d452930fSRichard Haines * was allowed by peer recv (and the netif/node checks), 5208d452930fSRichard Haines * then it is approved by policy and used as the primary 5209d452930fSRichard Haines * peer SID for getpeercon(3). 5210d452930fSRichard Haines */ 5211d452930fSRichard Haines sksec->peer_sid = peer_sid; 5212d452930fSRichard Haines } else if (sksec->peer_sid != peer_sid) { 5213d452930fSRichard Haines /* Other association peer SIDs are checked to enforce 5214d452930fSRichard Haines * consistency among the peer SIDs. 5215d452930fSRichard Haines */ 5216d452930fSRichard Haines ad.type = LSM_AUDIT_DATA_NET; 5217d452930fSRichard Haines ad.u.net = &net; 5218d452930fSRichard Haines ad.u.net->sk = ep->base.sk; 52196b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 52206b6bc620SStephen Smalley sksec->peer_sid, peer_sid, sksec->sclass, 5221d452930fSRichard Haines SCTP_SOCKET__ASSOCIATION, &ad); 5222d452930fSRichard Haines if (err) 5223d452930fSRichard Haines return err; 5224d452930fSRichard Haines } 5225d452930fSRichard Haines 5226d452930fSRichard Haines /* Compute the MLS component for the connection and store 5227d452930fSRichard Haines * the information in ep. This will be used by SCTP TCP type 5228d452930fSRichard Haines * sockets and peeled off connections as they cause a new 5229d452930fSRichard Haines * socket to be generated. selinux_sctp_sk_clone() will then 5230d452930fSRichard Haines * plug this into the new socket. 5231d452930fSRichard Haines */ 5232d452930fSRichard Haines err = selinux_conn_sid(sksec->sid, peer_sid, &conn_sid); 5233d452930fSRichard Haines if (err) 5234d452930fSRichard Haines return err; 5235d452930fSRichard Haines 5236d452930fSRichard Haines ep->secid = conn_sid; 5237d452930fSRichard Haines ep->peer_secid = peer_sid; 5238d452930fSRichard Haines 5239d452930fSRichard Haines /* Set any NetLabel labels including CIPSO/CALIPSO options. */ 5240d452930fSRichard Haines return selinux_netlbl_sctp_assoc_request(ep, skb); 5241d452930fSRichard Haines } 5242d452930fSRichard Haines 5243d452930fSRichard Haines /* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting 5244d452930fSRichard Haines * based on their @optname. 5245d452930fSRichard Haines */ 5246d452930fSRichard Haines static int selinux_sctp_bind_connect(struct sock *sk, int optname, 5247d452930fSRichard Haines struct sockaddr *address, 5248d452930fSRichard Haines int addrlen) 5249d452930fSRichard Haines { 5250d452930fSRichard Haines int len, err = 0, walk_size = 0; 5251d452930fSRichard Haines void *addr_buf; 5252d452930fSRichard Haines struct sockaddr *addr; 5253d452930fSRichard Haines struct socket *sock; 5254d452930fSRichard Haines 5255aa8e712cSStephen Smalley if (!selinux_policycap_extsockclass()) 5256d452930fSRichard Haines return 0; 5257d452930fSRichard Haines 5258d452930fSRichard Haines /* Process one or more addresses that may be IPv4 or IPv6 */ 5259d452930fSRichard Haines sock = sk->sk_socket; 5260d452930fSRichard Haines addr_buf = address; 5261d452930fSRichard Haines 5262d452930fSRichard Haines while (walk_size < addrlen) { 5263c138325fSOndrej Mosnacek if (walk_size + sizeof(sa_family_t) > addrlen) 5264c138325fSOndrej Mosnacek return -EINVAL; 5265c138325fSOndrej Mosnacek 5266d452930fSRichard Haines addr = addr_buf; 5267d452930fSRichard Haines switch (addr->sa_family) { 52684152dc91SAlexey Kodanev case AF_UNSPEC: 5269d452930fSRichard Haines case AF_INET: 5270d452930fSRichard Haines len = sizeof(struct sockaddr_in); 5271d452930fSRichard Haines break; 5272d452930fSRichard Haines case AF_INET6: 5273d452930fSRichard Haines len = sizeof(struct sockaddr_in6); 5274d452930fSRichard Haines break; 5275d452930fSRichard Haines default: 52764152dc91SAlexey Kodanev return -EINVAL; 5277d452930fSRichard Haines } 5278d452930fSRichard Haines 5279292c997aSXin Long if (walk_size + len > addrlen) 5280292c997aSXin Long return -EINVAL; 5281292c997aSXin Long 5282d452930fSRichard Haines err = -EINVAL; 5283d452930fSRichard Haines switch (optname) { 5284d452930fSRichard Haines /* Bind checks */ 5285d452930fSRichard Haines case SCTP_PRIMARY_ADDR: 5286d452930fSRichard Haines case SCTP_SET_PEER_PRIMARY_ADDR: 5287d452930fSRichard Haines case SCTP_SOCKOPT_BINDX_ADD: 5288d452930fSRichard Haines err = selinux_socket_bind(sock, addr, len); 5289d452930fSRichard Haines break; 5290d452930fSRichard Haines /* Connect checks */ 5291d452930fSRichard Haines case SCTP_SOCKOPT_CONNECTX: 5292d452930fSRichard Haines case SCTP_PARAM_SET_PRIMARY: 5293d452930fSRichard Haines case SCTP_PARAM_ADD_IP: 5294d452930fSRichard Haines case SCTP_SENDMSG_CONNECT: 5295d452930fSRichard Haines err = selinux_socket_connect_helper(sock, addr, len); 5296d452930fSRichard Haines if (err) 5297d452930fSRichard Haines return err; 5298d452930fSRichard Haines 5299d452930fSRichard Haines /* As selinux_sctp_bind_connect() is called by the 5300d452930fSRichard Haines * SCTP protocol layer, the socket is already locked, 5301d452930fSRichard Haines * therefore selinux_netlbl_socket_connect_locked() is 5302d452930fSRichard Haines * is called here. The situations handled are: 5303d452930fSRichard Haines * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2), 5304d452930fSRichard Haines * whenever a new IP address is added or when a new 5305d452930fSRichard Haines * primary address is selected. 5306d452930fSRichard Haines * Note that an SCTP connect(2) call happens before 5307d452930fSRichard Haines * the SCTP protocol layer and is handled via 5308d452930fSRichard Haines * selinux_socket_connect(). 5309d452930fSRichard Haines */ 5310d452930fSRichard Haines err = selinux_netlbl_socket_connect_locked(sk, addr); 5311d452930fSRichard Haines break; 5312d452930fSRichard Haines } 5313d452930fSRichard Haines 5314d452930fSRichard Haines if (err) 5315d452930fSRichard Haines return err; 5316d452930fSRichard Haines 5317d452930fSRichard Haines addr_buf += len; 5318d452930fSRichard Haines walk_size += len; 5319d452930fSRichard Haines } 5320d452930fSRichard Haines 5321d452930fSRichard Haines return 0; 5322d452930fSRichard Haines } 5323d452930fSRichard Haines 5324d452930fSRichard Haines /* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */ 5325d452930fSRichard Haines static void selinux_sctp_sk_clone(struct sctp_endpoint *ep, struct sock *sk, 5326d452930fSRichard Haines struct sock *newsk) 5327d452930fSRichard Haines { 5328d452930fSRichard Haines struct sk_security_struct *sksec = sk->sk_security; 5329d452930fSRichard Haines struct sk_security_struct *newsksec = newsk->sk_security; 5330d452930fSRichard Haines 5331d452930fSRichard Haines /* If policy does not support SECCLASS_SCTP_SOCKET then call 5332d452930fSRichard Haines * the non-sctp clone version. 5333d452930fSRichard Haines */ 5334aa8e712cSStephen Smalley if (!selinux_policycap_extsockclass()) 5335d452930fSRichard Haines return selinux_sk_clone_security(sk, newsk); 5336d452930fSRichard Haines 5337d452930fSRichard Haines newsksec->sid = ep->secid; 5338d452930fSRichard Haines newsksec->peer_sid = ep->peer_secid; 5339d452930fSRichard Haines newsksec->sclass = sksec->sclass; 5340d452930fSRichard Haines selinux_netlbl_sctp_sk_clone(sk, newsk); 5341d452930fSRichard Haines } 5342d452930fSRichard Haines 53439a673e56SAdrian Bunk static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb, 53444237c75cSVenkat Yekkirala struct request_sock *req) 53454237c75cSVenkat Yekkirala { 53464237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 53474237c75cSVenkat Yekkirala int err; 53480b1f24e6SPaul Moore u16 family = req->rsk_ops->family; 5349446b8024SPaul Moore u32 connsid; 53504237c75cSVenkat Yekkirala u32 peersid; 53514237c75cSVenkat Yekkirala 5352aa862900SPaul Moore err = selinux_skb_peerlbl_sid(skb, family, &peersid); 5353220deb96SPaul Moore if (err) 5354220deb96SPaul Moore return err; 5355446b8024SPaul Moore err = selinux_conn_sid(sksec->sid, peersid, &connsid); 53564237c75cSVenkat Yekkirala if (err) 53574237c75cSVenkat Yekkirala return err; 5358446b8024SPaul Moore req->secid = connsid; 53596b877699SVenkat Yekkirala req->peer_secid = peersid; 5360389fb800SPaul Moore 5361389fb800SPaul Moore return selinux_netlbl_inet_conn_request(req, family); 53624237c75cSVenkat Yekkirala } 53634237c75cSVenkat Yekkirala 53649a673e56SAdrian Bunk static void selinux_inet_csk_clone(struct sock *newsk, 53659a673e56SAdrian Bunk const struct request_sock *req) 53664237c75cSVenkat Yekkirala { 53674237c75cSVenkat Yekkirala struct sk_security_struct *newsksec = newsk->sk_security; 53684237c75cSVenkat Yekkirala 53694237c75cSVenkat Yekkirala newsksec->sid = req->secid; 53706b877699SVenkat Yekkirala newsksec->peer_sid = req->peer_secid; 53714237c75cSVenkat Yekkirala /* NOTE: Ideally, we should also get the isec->sid for the 53724237c75cSVenkat Yekkirala new socket in sync, but we don't have the isec available yet. 53734237c75cSVenkat Yekkirala So we will wait until sock_graft to do it, by which 53744237c75cSVenkat Yekkirala time it will have been created and available. */ 537599f59ed0SPaul Moore 53769f2ad665SPaul Moore /* We don't need to take any sort of lock here as we are the only 53779f2ad665SPaul Moore * thread with access to newsksec */ 5378389fb800SPaul Moore selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family); 53794237c75cSVenkat Yekkirala } 53804237c75cSVenkat Yekkirala 5381014ab19aSPaul Moore static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb) 53826b877699SVenkat Yekkirala { 5383aa862900SPaul Moore u16 family = sk->sk_family; 53846b877699SVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 53856b877699SVenkat Yekkirala 5386aa862900SPaul Moore /* handle mapped IPv4 packets arriving via IPv6 sockets */ 5387aa862900SPaul Moore if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 5388aa862900SPaul Moore family = PF_INET; 5389aa862900SPaul Moore 5390aa862900SPaul Moore selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid); 53916b877699SVenkat Yekkirala } 53926b877699SVenkat Yekkirala 53932606fd1fSEric Paris static int selinux_secmark_relabel_packet(u32 sid) 53942606fd1fSEric Paris { 53952606fd1fSEric Paris const struct task_security_struct *__tsec; 53962606fd1fSEric Paris u32 tsid; 53972606fd1fSEric Paris 53980c6cfa62SCasey Schaufler __tsec = selinux_cred(current_cred()); 53992606fd1fSEric Paris tsid = __tsec->sid; 54002606fd1fSEric Paris 54016b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 54026b6bc620SStephen Smalley tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, 54036b6bc620SStephen Smalley NULL); 54042606fd1fSEric Paris } 54052606fd1fSEric Paris 54062606fd1fSEric Paris static void selinux_secmark_refcount_inc(void) 54072606fd1fSEric Paris { 54082606fd1fSEric Paris atomic_inc(&selinux_secmark_refcount); 54092606fd1fSEric Paris } 54102606fd1fSEric Paris 54112606fd1fSEric Paris static void selinux_secmark_refcount_dec(void) 54122606fd1fSEric Paris { 54132606fd1fSEric Paris atomic_dec(&selinux_secmark_refcount); 54142606fd1fSEric Paris } 54152606fd1fSEric Paris 54169a673e56SAdrian Bunk static void selinux_req_classify_flow(const struct request_sock *req, 54179a673e56SAdrian Bunk struct flowi *fl) 54184237c75cSVenkat Yekkirala { 54191d28f42cSDavid S. Miller fl->flowi_secid = req->secid; 54204237c75cSVenkat Yekkirala } 54214237c75cSVenkat Yekkirala 54225dbbaf2dSPaul Moore static int selinux_tun_dev_alloc_security(void **security) 54235dbbaf2dSPaul Moore { 54245dbbaf2dSPaul Moore struct tun_security_struct *tunsec; 54255dbbaf2dSPaul Moore 54265dbbaf2dSPaul Moore tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL); 54275dbbaf2dSPaul Moore if (!tunsec) 54285dbbaf2dSPaul Moore return -ENOMEM; 54295dbbaf2dSPaul Moore tunsec->sid = current_sid(); 54305dbbaf2dSPaul Moore 54315dbbaf2dSPaul Moore *security = tunsec; 54325dbbaf2dSPaul Moore return 0; 54335dbbaf2dSPaul Moore } 54345dbbaf2dSPaul Moore 54355dbbaf2dSPaul Moore static void selinux_tun_dev_free_security(void *security) 54365dbbaf2dSPaul Moore { 54375dbbaf2dSPaul Moore kfree(security); 54385dbbaf2dSPaul Moore } 54395dbbaf2dSPaul Moore 5440ed6d76e4SPaul Moore static int selinux_tun_dev_create(void) 5441ed6d76e4SPaul Moore { 5442ed6d76e4SPaul Moore u32 sid = current_sid(); 5443ed6d76e4SPaul Moore 5444ed6d76e4SPaul Moore /* we aren't taking into account the "sockcreate" SID since the socket 5445ed6d76e4SPaul Moore * that is being created here is not a socket in the traditional sense, 5446ed6d76e4SPaul Moore * instead it is a private sock, accessible only to the kernel, and 5447ed6d76e4SPaul Moore * representing a wide range of network traffic spanning multiple 5448ed6d76e4SPaul Moore * connections unlike traditional sockets - check the TUN driver to 5449ed6d76e4SPaul Moore * get a better understanding of why this socket is special */ 5450ed6d76e4SPaul Moore 54516b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 54526b6bc620SStephen Smalley sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE, 5453ed6d76e4SPaul Moore NULL); 5454ed6d76e4SPaul Moore } 5455ed6d76e4SPaul Moore 54565dbbaf2dSPaul Moore static int selinux_tun_dev_attach_queue(void *security) 5457ed6d76e4SPaul Moore { 54585dbbaf2dSPaul Moore struct tun_security_struct *tunsec = security; 54595dbbaf2dSPaul Moore 54606b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 54616b6bc620SStephen Smalley current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET, 54625dbbaf2dSPaul Moore TUN_SOCKET__ATTACH_QUEUE, NULL); 54635dbbaf2dSPaul Moore } 54645dbbaf2dSPaul Moore 54655dbbaf2dSPaul Moore static int selinux_tun_dev_attach(struct sock *sk, void *security) 54665dbbaf2dSPaul Moore { 54675dbbaf2dSPaul Moore struct tun_security_struct *tunsec = security; 5468ed6d76e4SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 5469ed6d76e4SPaul Moore 5470ed6d76e4SPaul Moore /* we don't currently perform any NetLabel based labeling here and it 5471ed6d76e4SPaul Moore * isn't clear that we would want to do so anyway; while we could apply 5472ed6d76e4SPaul Moore * labeling without the support of the TUN user the resulting labeled 5473ed6d76e4SPaul Moore * traffic from the other end of the connection would almost certainly 5474ed6d76e4SPaul Moore * cause confusion to the TUN user that had no idea network labeling 5475ed6d76e4SPaul Moore * protocols were being used */ 5476ed6d76e4SPaul Moore 54775dbbaf2dSPaul Moore sksec->sid = tunsec->sid; 5478ed6d76e4SPaul Moore sksec->sclass = SECCLASS_TUN_SOCKET; 54795dbbaf2dSPaul Moore 54805dbbaf2dSPaul Moore return 0; 5481ed6d76e4SPaul Moore } 5482ed6d76e4SPaul Moore 54835dbbaf2dSPaul Moore static int selinux_tun_dev_open(void *security) 5484ed6d76e4SPaul Moore { 54855dbbaf2dSPaul Moore struct tun_security_struct *tunsec = security; 5486ed6d76e4SPaul Moore u32 sid = current_sid(); 5487ed6d76e4SPaul Moore int err; 5488ed6d76e4SPaul Moore 54896b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 54906b6bc620SStephen Smalley sid, tunsec->sid, SECCLASS_TUN_SOCKET, 5491ed6d76e4SPaul Moore TUN_SOCKET__RELABELFROM, NULL); 5492ed6d76e4SPaul Moore if (err) 5493ed6d76e4SPaul Moore return err; 54946b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 54956b6bc620SStephen Smalley sid, sid, SECCLASS_TUN_SOCKET, 5496ed6d76e4SPaul Moore TUN_SOCKET__RELABELTO, NULL); 5497ed6d76e4SPaul Moore if (err) 5498ed6d76e4SPaul Moore return err; 54995dbbaf2dSPaul Moore tunsec->sid = sid; 5500ed6d76e4SPaul Moore 5501ed6d76e4SPaul Moore return 0; 5502ed6d76e4SPaul Moore } 5503ed6d76e4SPaul Moore 55041da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER 55051da177e4SLinus Torvalds 5506cbe0d6e8SPaul Moore static unsigned int selinux_ip_forward(struct sk_buff *skb, 5507cbe0d6e8SPaul Moore const struct net_device *indev, 5508effad8dfSPaul Moore u16 family) 55091da177e4SLinus Torvalds { 5510dfaebe98SPaul Moore int err; 5511effad8dfSPaul Moore char *addrp; 5512effad8dfSPaul Moore u32 peer_sid; 55132bf49690SThomas Liu struct common_audit_data ad; 551448c62af6SEric Paris struct lsm_network_audit net = {0,}; 5515effad8dfSPaul Moore u8 secmark_active; 5516948bf85cSPaul Moore u8 netlbl_active; 5517effad8dfSPaul Moore u8 peerlbl_active; 55184237c75cSVenkat Yekkirala 5519aa8e712cSStephen Smalley if (!selinux_policycap_netpeer()) 5520effad8dfSPaul Moore return NF_ACCEPT; 55214237c75cSVenkat Yekkirala 5522effad8dfSPaul Moore secmark_active = selinux_secmark_enabled(); 5523948bf85cSPaul Moore netlbl_active = netlbl_enabled(); 55242be4d74fSChris PeBenito peerlbl_active = selinux_peerlbl_enabled(); 5525effad8dfSPaul Moore if (!secmark_active && !peerlbl_active) 5526effad8dfSPaul Moore return NF_ACCEPT; 55274237c75cSVenkat Yekkirala 5528d8395c87SPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0) 5529d8395c87SPaul Moore return NF_DROP; 5530d8395c87SPaul Moore 553150c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 553248c62af6SEric Paris ad.u.net = &net; 5533cbe0d6e8SPaul Moore ad.u.net->netif = indev->ifindex; 553448c62af6SEric Paris ad.u.net->family = family; 5535effad8dfSPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0) 5536effad8dfSPaul Moore return NF_DROP; 55371da177e4SLinus Torvalds 5538dfaebe98SPaul Moore if (peerlbl_active) { 5539cbe0d6e8SPaul Moore err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex, 5540cbe0d6e8SPaul Moore addrp, family, peer_sid, &ad); 5541dfaebe98SPaul Moore if (err) { 5542a04e71f6SHuw Davies selinux_netlbl_err(skb, family, err, 1); 5543effad8dfSPaul Moore return NF_DROP; 5544dfaebe98SPaul Moore } 5545dfaebe98SPaul Moore } 5546effad8dfSPaul Moore 5547effad8dfSPaul Moore if (secmark_active) 55486b6bc620SStephen Smalley if (avc_has_perm(&selinux_state, 55496b6bc620SStephen Smalley peer_sid, skb->secmark, 5550effad8dfSPaul Moore SECCLASS_PACKET, PACKET__FORWARD_IN, &ad)) 5551effad8dfSPaul Moore return NF_DROP; 5552effad8dfSPaul Moore 5553948bf85cSPaul Moore if (netlbl_active) 5554948bf85cSPaul Moore /* we do this in the FORWARD path and not the POST_ROUTING 5555948bf85cSPaul Moore * path because we want to make sure we apply the necessary 5556948bf85cSPaul Moore * labeling before IPsec is applied so we can leverage AH 5557948bf85cSPaul Moore * protection */ 5558948bf85cSPaul Moore if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0) 5559948bf85cSPaul Moore return NF_DROP; 5560948bf85cSPaul Moore 5561effad8dfSPaul Moore return NF_ACCEPT; 5562effad8dfSPaul Moore } 5563effad8dfSPaul Moore 556406198b34SEric W. Biederman static unsigned int selinux_ipv4_forward(void *priv, 5565effad8dfSPaul Moore struct sk_buff *skb, 5566238e54c9SDavid S. Miller const struct nf_hook_state *state) 5567effad8dfSPaul Moore { 5568238e54c9SDavid S. Miller return selinux_ip_forward(skb, state->in, PF_INET); 5569effad8dfSPaul Moore } 5570effad8dfSPaul Moore 55711a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 557206198b34SEric W. Biederman static unsigned int selinux_ipv6_forward(void *priv, 5573effad8dfSPaul Moore struct sk_buff *skb, 5574238e54c9SDavid S. Miller const struct nf_hook_state *state) 5575effad8dfSPaul Moore { 5576238e54c9SDavid S. Miller return selinux_ip_forward(skb, state->in, PF_INET6); 5577effad8dfSPaul Moore } 5578effad8dfSPaul Moore #endif /* IPV6 */ 5579effad8dfSPaul Moore 5580948bf85cSPaul Moore static unsigned int selinux_ip_output(struct sk_buff *skb, 5581948bf85cSPaul Moore u16 family) 5582948bf85cSPaul Moore { 558347180068SPaul Moore struct sock *sk; 5584948bf85cSPaul Moore u32 sid; 5585948bf85cSPaul Moore 5586948bf85cSPaul Moore if (!netlbl_enabled()) 5587948bf85cSPaul Moore return NF_ACCEPT; 5588948bf85cSPaul Moore 5589948bf85cSPaul Moore /* we do this in the LOCAL_OUT path and not the POST_ROUTING path 5590948bf85cSPaul Moore * because we want to make sure we apply the necessary labeling 5591948bf85cSPaul Moore * before IPsec is applied so we can leverage AH protection */ 559247180068SPaul Moore sk = skb->sk; 559347180068SPaul Moore if (sk) { 559447180068SPaul Moore struct sk_security_struct *sksec; 559547180068SPaul Moore 5596e446f9dfSEric Dumazet if (sk_listener(sk)) 559747180068SPaul Moore /* if the socket is the listening state then this 559847180068SPaul Moore * packet is a SYN-ACK packet which means it needs to 559947180068SPaul Moore * be labeled based on the connection/request_sock and 560047180068SPaul Moore * not the parent socket. unfortunately, we can't 560147180068SPaul Moore * lookup the request_sock yet as it isn't queued on 560247180068SPaul Moore * the parent socket until after the SYN-ACK is sent. 560347180068SPaul Moore * the "solution" is to simply pass the packet as-is 560447180068SPaul Moore * as any IP option based labeling should be copied 560547180068SPaul Moore * from the initial connection request (in the IP 560647180068SPaul Moore * layer). it is far from ideal, but until we get a 560747180068SPaul Moore * security label in the packet itself this is the 560847180068SPaul Moore * best we can do. */ 560947180068SPaul Moore return NF_ACCEPT; 561047180068SPaul Moore 561147180068SPaul Moore /* standard practice, label using the parent socket */ 561247180068SPaul Moore sksec = sk->sk_security; 5613948bf85cSPaul Moore sid = sksec->sid; 5614948bf85cSPaul Moore } else 5615948bf85cSPaul Moore sid = SECINITSID_KERNEL; 5616948bf85cSPaul Moore if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0) 5617948bf85cSPaul Moore return NF_DROP; 5618948bf85cSPaul Moore 5619948bf85cSPaul Moore return NF_ACCEPT; 5620948bf85cSPaul Moore } 5621948bf85cSPaul Moore 562206198b34SEric W. Biederman static unsigned int selinux_ipv4_output(void *priv, 5623948bf85cSPaul Moore struct sk_buff *skb, 5624238e54c9SDavid S. Miller const struct nf_hook_state *state) 5625948bf85cSPaul Moore { 5626948bf85cSPaul Moore return selinux_ip_output(skb, PF_INET); 5627948bf85cSPaul Moore } 5628948bf85cSPaul Moore 56291a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 56302917f57bSHuw Davies static unsigned int selinux_ipv6_output(void *priv, 56312917f57bSHuw Davies struct sk_buff *skb, 56322917f57bSHuw Davies const struct nf_hook_state *state) 56332917f57bSHuw Davies { 56342917f57bSHuw Davies return selinux_ip_output(skb, PF_INET6); 56352917f57bSHuw Davies } 56362917f57bSHuw Davies #endif /* IPV6 */ 56372917f57bSHuw Davies 5638effad8dfSPaul Moore static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb, 5639effad8dfSPaul Moore int ifindex, 5640d8395c87SPaul Moore u16 family) 56414e5ab4cbSJames Morris { 564254abc686SEric Dumazet struct sock *sk = skb_to_full_sk(skb); 56434237c75cSVenkat Yekkirala struct sk_security_struct *sksec; 56442bf49690SThomas Liu struct common_audit_data ad; 564548c62af6SEric Paris struct lsm_network_audit net = {0,}; 5646d8395c87SPaul Moore char *addrp; 5647d8395c87SPaul Moore u8 proto; 56484e5ab4cbSJames Morris 5649effad8dfSPaul Moore if (sk == NULL) 5650effad8dfSPaul Moore return NF_ACCEPT; 56514237c75cSVenkat Yekkirala sksec = sk->sk_security; 56524e5ab4cbSJames Morris 565350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 565448c62af6SEric Paris ad.u.net = &net; 565548c62af6SEric Paris ad.u.net->netif = ifindex; 565648c62af6SEric Paris ad.u.net->family = family; 5657d8395c87SPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto)) 5658d8395c87SPaul Moore return NF_DROP; 5659d8395c87SPaul Moore 566058bfbb51SPaul Moore if (selinux_secmark_enabled()) 56616b6bc620SStephen Smalley if (avc_has_perm(&selinux_state, 56626b6bc620SStephen Smalley sksec->sid, skb->secmark, 5663d8395c87SPaul Moore SECCLASS_PACKET, PACKET__SEND, &ad)) 56642fe66ec2SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 56651da177e4SLinus Torvalds 5666d8395c87SPaul Moore if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto)) 56672fe66ec2SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 5668effad8dfSPaul Moore 5669effad8dfSPaul Moore return NF_ACCEPT; 5670effad8dfSPaul Moore } 5671effad8dfSPaul Moore 5672cbe0d6e8SPaul Moore static unsigned int selinux_ip_postroute(struct sk_buff *skb, 5673cbe0d6e8SPaul Moore const struct net_device *outdev, 5674effad8dfSPaul Moore u16 family) 5675effad8dfSPaul Moore { 5676effad8dfSPaul Moore u32 secmark_perm; 5677effad8dfSPaul Moore u32 peer_sid; 5678cbe0d6e8SPaul Moore int ifindex = outdev->ifindex; 5679effad8dfSPaul Moore struct sock *sk; 56802bf49690SThomas Liu struct common_audit_data ad; 568148c62af6SEric Paris struct lsm_network_audit net = {0,}; 5682effad8dfSPaul Moore char *addrp; 5683effad8dfSPaul Moore u8 secmark_active; 5684effad8dfSPaul Moore u8 peerlbl_active; 5685effad8dfSPaul Moore 5686effad8dfSPaul Moore /* If any sort of compatibility mode is enabled then handoff processing 5687effad8dfSPaul Moore * to the selinux_ip_postroute_compat() function to deal with the 5688effad8dfSPaul Moore * special handling. We do this in an attempt to keep this function 5689effad8dfSPaul Moore * as fast and as clean as possible. */ 5690aa8e712cSStephen Smalley if (!selinux_policycap_netpeer()) 5691d8395c87SPaul Moore return selinux_ip_postroute_compat(skb, ifindex, family); 5692c0828e50SPaul Moore 5693effad8dfSPaul Moore secmark_active = selinux_secmark_enabled(); 56942be4d74fSChris PeBenito peerlbl_active = selinux_peerlbl_enabled(); 5695effad8dfSPaul Moore if (!secmark_active && !peerlbl_active) 5696effad8dfSPaul Moore return NF_ACCEPT; 5697effad8dfSPaul Moore 569854abc686SEric Dumazet sk = skb_to_full_sk(skb); 5699c0828e50SPaul Moore 5700effad8dfSPaul Moore #ifdef CONFIG_XFRM 5701effad8dfSPaul Moore /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec 5702effad8dfSPaul Moore * packet transformation so allow the packet to pass without any checks 5703effad8dfSPaul Moore * since we'll have another chance to perform access control checks 5704effad8dfSPaul Moore * when the packet is on it's final way out. 5705effad8dfSPaul Moore * NOTE: there appear to be some IPv6 multicast cases where skb->dst 5706c0828e50SPaul Moore * is NULL, in this case go ahead and apply access control. 5707c0828e50SPaul Moore * NOTE: if this is a local socket (skb->sk != NULL) that is in the 5708c0828e50SPaul Moore * TCP listening state we cannot wait until the XFRM processing 5709c0828e50SPaul Moore * is done as we will miss out on the SA label if we do; 5710c0828e50SPaul Moore * unfortunately, this means more work, but it is only once per 5711c0828e50SPaul Moore * connection. */ 5712c0828e50SPaul Moore if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL && 5713e446f9dfSEric Dumazet !(sk && sk_listener(sk))) 5714effad8dfSPaul Moore return NF_ACCEPT; 5715effad8dfSPaul Moore #endif 5716effad8dfSPaul Moore 5717d8395c87SPaul Moore if (sk == NULL) { 5718446b8024SPaul Moore /* Without an associated socket the packet is either coming 5719446b8024SPaul Moore * from the kernel or it is being forwarded; check the packet 5720446b8024SPaul Moore * to determine which and if the packet is being forwarded 5721446b8024SPaul Moore * query the packet directly to determine the security label. */ 57224a7ab3dcSSteffen Klassert if (skb->skb_iif) { 5723d8395c87SPaul Moore secmark_perm = PACKET__FORWARD_OUT; 5724d8395c87SPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &peer_sid)) 572504f6d70fSEric Paris return NF_DROP; 57264a7ab3dcSSteffen Klassert } else { 57274a7ab3dcSSteffen Klassert secmark_perm = PACKET__SEND; 5728d8395c87SPaul Moore peer_sid = SECINITSID_KERNEL; 57294a7ab3dcSSteffen Klassert } 5730e446f9dfSEric Dumazet } else if (sk_listener(sk)) { 5731446b8024SPaul Moore /* Locally generated packet but the associated socket is in the 5732446b8024SPaul Moore * listening state which means this is a SYN-ACK packet. In 5733446b8024SPaul Moore * this particular case the correct security label is assigned 5734446b8024SPaul Moore * to the connection/request_sock but unfortunately we can't 5735446b8024SPaul Moore * query the request_sock as it isn't queued on the parent 5736446b8024SPaul Moore * socket until after the SYN-ACK packet is sent; the only 5737446b8024SPaul Moore * viable choice is to regenerate the label like we do in 5738446b8024SPaul Moore * selinux_inet_conn_request(). See also selinux_ip_output() 5739446b8024SPaul Moore * for similar problems. */ 5740446b8024SPaul Moore u32 skb_sid; 5741e446f9dfSEric Dumazet struct sk_security_struct *sksec; 5742e446f9dfSEric Dumazet 5743e446f9dfSEric Dumazet sksec = sk->sk_security; 5744446b8024SPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &skb_sid)) 5745446b8024SPaul Moore return NF_DROP; 5746c0828e50SPaul Moore /* At this point, if the returned skb peerlbl is SECSID_NULL 5747c0828e50SPaul Moore * and the packet has been through at least one XFRM 5748c0828e50SPaul Moore * transformation then we must be dealing with the "final" 5749c0828e50SPaul Moore * form of labeled IPsec packet; since we've already applied 5750c0828e50SPaul Moore * all of our access controls on this packet we can safely 5751c0828e50SPaul Moore * pass the packet. */ 5752c0828e50SPaul Moore if (skb_sid == SECSID_NULL) { 5753c0828e50SPaul Moore switch (family) { 5754c0828e50SPaul Moore case PF_INET: 5755c0828e50SPaul Moore if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) 5756c0828e50SPaul Moore return NF_ACCEPT; 5757c0828e50SPaul Moore break; 5758c0828e50SPaul Moore case PF_INET6: 5759c0828e50SPaul Moore if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) 5760c0828e50SPaul Moore return NF_ACCEPT; 5761a7a91a19SPaul Moore break; 5762c0828e50SPaul Moore default: 5763c0828e50SPaul Moore return NF_DROP_ERR(-ECONNREFUSED); 5764c0828e50SPaul Moore } 5765c0828e50SPaul Moore } 5766446b8024SPaul Moore if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid)) 5767446b8024SPaul Moore return NF_DROP; 5768446b8024SPaul Moore secmark_perm = PACKET__SEND; 5769d8395c87SPaul Moore } else { 5770446b8024SPaul Moore /* Locally generated packet, fetch the security label from the 5771446b8024SPaul Moore * associated socket. */ 5772effad8dfSPaul Moore struct sk_security_struct *sksec = sk->sk_security; 5773effad8dfSPaul Moore peer_sid = sksec->sid; 5774effad8dfSPaul Moore secmark_perm = PACKET__SEND; 5775effad8dfSPaul Moore } 5776effad8dfSPaul Moore 577750c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 577848c62af6SEric Paris ad.u.net = &net; 577948c62af6SEric Paris ad.u.net->netif = ifindex; 578048c62af6SEric Paris ad.u.net->family = family; 5781d8395c87SPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL)) 578204f6d70fSEric Paris return NF_DROP; 5783d8395c87SPaul Moore 5784effad8dfSPaul Moore if (secmark_active) 57856b6bc620SStephen Smalley if (avc_has_perm(&selinux_state, 57866b6bc620SStephen Smalley peer_sid, skb->secmark, 5787effad8dfSPaul Moore SECCLASS_PACKET, secmark_perm, &ad)) 57881f1aaf82SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 5789effad8dfSPaul Moore 5790effad8dfSPaul Moore if (peerlbl_active) { 5791effad8dfSPaul Moore u32 if_sid; 5792effad8dfSPaul Moore u32 node_sid; 5793effad8dfSPaul Moore 5794cbe0d6e8SPaul Moore if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid)) 579504f6d70fSEric Paris return NF_DROP; 57966b6bc620SStephen Smalley if (avc_has_perm(&selinux_state, 57976b6bc620SStephen Smalley peer_sid, if_sid, 5798effad8dfSPaul Moore SECCLASS_NETIF, NETIF__EGRESS, &ad)) 57991f1aaf82SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 5800effad8dfSPaul Moore 5801effad8dfSPaul Moore if (sel_netnode_sid(addrp, family, &node_sid)) 580204f6d70fSEric Paris return NF_DROP; 58036b6bc620SStephen Smalley if (avc_has_perm(&selinux_state, 58046b6bc620SStephen Smalley peer_sid, node_sid, 5805effad8dfSPaul Moore SECCLASS_NODE, NODE__SENDTO, &ad)) 58061f1aaf82SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 5807effad8dfSPaul Moore } 5808effad8dfSPaul Moore 5809effad8dfSPaul Moore return NF_ACCEPT; 5810effad8dfSPaul Moore } 5811effad8dfSPaul Moore 581206198b34SEric W. Biederman static unsigned int selinux_ipv4_postroute(void *priv, 5813a224be76SDavid S. Miller struct sk_buff *skb, 5814238e54c9SDavid S. Miller const struct nf_hook_state *state) 58151da177e4SLinus Torvalds { 5816238e54c9SDavid S. Miller return selinux_ip_postroute(skb, state->out, PF_INET); 58171da177e4SLinus Torvalds } 58181da177e4SLinus Torvalds 58191a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 582006198b34SEric W. Biederman static unsigned int selinux_ipv6_postroute(void *priv, 5821a224be76SDavid S. Miller struct sk_buff *skb, 5822238e54c9SDavid S. Miller const struct nf_hook_state *state) 58231da177e4SLinus Torvalds { 5824238e54c9SDavid S. Miller return selinux_ip_postroute(skb, state->out, PF_INET6); 58251da177e4SLinus Torvalds } 58261da177e4SLinus Torvalds #endif /* IPV6 */ 58271da177e4SLinus Torvalds 58281da177e4SLinus Torvalds #endif /* CONFIG_NETFILTER */ 58291da177e4SLinus Torvalds 58301da177e4SLinus Torvalds static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb) 58311da177e4SLinus Torvalds { 5832df4779b5SHuaisheng Ye int err = 0; 5833df4779b5SHuaisheng Ye u32 perm; 5834df4779b5SHuaisheng Ye struct nlmsghdr *nlh; 5835df4779b5SHuaisheng Ye struct sk_security_struct *sksec = sk->sk_security; 5836df4779b5SHuaisheng Ye 5837df4779b5SHuaisheng Ye if (skb->len < NLMSG_HDRLEN) { 5838df4779b5SHuaisheng Ye err = -EINVAL; 5839df4779b5SHuaisheng Ye goto out; 5840df4779b5SHuaisheng Ye } 5841df4779b5SHuaisheng Ye nlh = nlmsg_hdr(skb); 5842df4779b5SHuaisheng Ye 5843df4779b5SHuaisheng Ye err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm); 5844df4779b5SHuaisheng Ye if (err) { 5845df4779b5SHuaisheng Ye if (err == -EINVAL) { 5846df4779b5SHuaisheng Ye pr_warn_ratelimited("SELinux: unrecognized netlink" 5847df4779b5SHuaisheng Ye " message: protocol=%hu nlmsg_type=%hu sclass=%s" 5848df4779b5SHuaisheng Ye " pid=%d comm=%s\n", 5849df4779b5SHuaisheng Ye sk->sk_protocol, nlh->nlmsg_type, 5850df4779b5SHuaisheng Ye secclass_map[sksec->sclass - 1].name, 5851df4779b5SHuaisheng Ye task_pid_nr(current), current->comm); 5852df4779b5SHuaisheng Ye if (!enforcing_enabled(&selinux_state) || 5853df4779b5SHuaisheng Ye security_get_allow_unknown(&selinux_state)) 5854df4779b5SHuaisheng Ye err = 0; 5855df4779b5SHuaisheng Ye } 5856df4779b5SHuaisheng Ye 5857df4779b5SHuaisheng Ye /* Ignore */ 5858df4779b5SHuaisheng Ye if (err == -ENOENT) 5859df4779b5SHuaisheng Ye err = 0; 5860df4779b5SHuaisheng Ye goto out; 5861df4779b5SHuaisheng Ye } 5862df4779b5SHuaisheng Ye 5863df4779b5SHuaisheng Ye err = sock_has_perm(sk, perm); 5864df4779b5SHuaisheng Ye out: 5865df4779b5SHuaisheng Ye return err; 58661da177e4SLinus Torvalds } 58671da177e4SLinus Torvalds 5868ecd5f82eSCasey Schaufler static void ipc_init_security(struct ipc_security_struct *isec, u16 sclass) 58691da177e4SLinus Torvalds { 58701da177e4SLinus Torvalds isec->sclass = sclass; 5871be0554c9SStephen Smalley isec->sid = current_sid(); 58721da177e4SLinus Torvalds } 58731da177e4SLinus Torvalds 58741da177e4SLinus Torvalds static int ipc_has_perm(struct kern_ipc_perm *ipc_perms, 58756af963f1SStephen Smalley u32 perms) 58761da177e4SLinus Torvalds { 58771da177e4SLinus Torvalds struct ipc_security_struct *isec; 58782bf49690SThomas Liu struct common_audit_data ad; 5879275bb41eSDavid Howells u32 sid = current_sid(); 58801da177e4SLinus Torvalds 58817c653828SCasey Schaufler isec = selinux_ipc(ipc_perms); 58821da177e4SLinus Torvalds 588350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 58841da177e4SLinus Torvalds ad.u.ipc_id = ipc_perms->key; 58851da177e4SLinus Torvalds 58866b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 58876b6bc620SStephen Smalley sid, isec->sid, isec->sclass, perms, &ad); 58881da177e4SLinus Torvalds } 58891da177e4SLinus Torvalds 58901da177e4SLinus Torvalds static int selinux_msg_msg_alloc_security(struct msg_msg *msg) 58911da177e4SLinus Torvalds { 5892b82f3f68SHuaisheng Ye struct msg_security_struct *msec; 5893b82f3f68SHuaisheng Ye 5894b82f3f68SHuaisheng Ye msec = selinux_msg_msg(msg); 5895b82f3f68SHuaisheng Ye msec->sid = SECINITSID_UNLABELED; 5896b82f3f68SHuaisheng Ye 5897b82f3f68SHuaisheng Ye return 0; 58981da177e4SLinus Torvalds } 58991da177e4SLinus Torvalds 59001da177e4SLinus Torvalds /* message queue security operations */ 5901d8c6e854SEric W. Biederman static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq) 59021da177e4SLinus Torvalds { 59031da177e4SLinus Torvalds struct ipc_security_struct *isec; 59042bf49690SThomas Liu struct common_audit_data ad; 5905275bb41eSDavid Howells u32 sid = current_sid(); 59061da177e4SLinus Torvalds int rc; 59071da177e4SLinus Torvalds 5908ecd5f82eSCasey Schaufler isec = selinux_ipc(msq); 5909ecd5f82eSCasey Schaufler ipc_init_security(isec, SECCLASS_MSGQ); 59101da177e4SLinus Torvalds 591150c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 5912d8c6e854SEric W. Biederman ad.u.ipc_id = msq->key; 59131da177e4SLinus Torvalds 59146b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 59156b6bc620SStephen Smalley sid, isec->sid, SECCLASS_MSGQ, 59161da177e4SLinus Torvalds MSGQ__CREATE, &ad); 59171da177e4SLinus Torvalds return rc; 59181da177e4SLinus Torvalds } 59191da177e4SLinus Torvalds 5920d8c6e854SEric W. Biederman static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg) 59211da177e4SLinus Torvalds { 59221da177e4SLinus Torvalds struct ipc_security_struct *isec; 59232bf49690SThomas Liu struct common_audit_data ad; 5924275bb41eSDavid Howells u32 sid = current_sid(); 59251da177e4SLinus Torvalds 59267c653828SCasey Schaufler isec = selinux_ipc(msq); 59271da177e4SLinus Torvalds 592850c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 5929d8c6e854SEric W. Biederman ad.u.ipc_id = msq->key; 59301da177e4SLinus Torvalds 59316b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 59326b6bc620SStephen Smalley sid, isec->sid, SECCLASS_MSGQ, 59331da177e4SLinus Torvalds MSGQ__ASSOCIATE, &ad); 59341da177e4SLinus Torvalds } 59351da177e4SLinus Torvalds 5936d8c6e854SEric W. Biederman static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd) 59371da177e4SLinus Torvalds { 59381da177e4SLinus Torvalds int err; 59391da177e4SLinus Torvalds int perms; 59401da177e4SLinus Torvalds 59411da177e4SLinus Torvalds switch (cmd) { 59421da177e4SLinus Torvalds case IPC_INFO: 59431da177e4SLinus Torvalds case MSG_INFO: 59441da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 59456b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 59466b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, 5947be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); 59481da177e4SLinus Torvalds case IPC_STAT: 59491da177e4SLinus Torvalds case MSG_STAT: 595023c8cec8SDavidlohr Bueso case MSG_STAT_ANY: 59511da177e4SLinus Torvalds perms = MSGQ__GETATTR | MSGQ__ASSOCIATE; 59521da177e4SLinus Torvalds break; 59531da177e4SLinus Torvalds case IPC_SET: 59541da177e4SLinus Torvalds perms = MSGQ__SETATTR; 59551da177e4SLinus Torvalds break; 59561da177e4SLinus Torvalds case IPC_RMID: 59571da177e4SLinus Torvalds perms = MSGQ__DESTROY; 59581da177e4SLinus Torvalds break; 59591da177e4SLinus Torvalds default: 59601da177e4SLinus Torvalds return 0; 59611da177e4SLinus Torvalds } 59621da177e4SLinus Torvalds 5963d8c6e854SEric W. Biederman err = ipc_has_perm(msq, perms); 59641da177e4SLinus Torvalds return err; 59651da177e4SLinus Torvalds } 59661da177e4SLinus Torvalds 5967d8c6e854SEric W. Biederman static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg) 59681da177e4SLinus Torvalds { 59691da177e4SLinus Torvalds struct ipc_security_struct *isec; 59701da177e4SLinus Torvalds struct msg_security_struct *msec; 59712bf49690SThomas Liu struct common_audit_data ad; 5972275bb41eSDavid Howells u32 sid = current_sid(); 59731da177e4SLinus Torvalds int rc; 59741da177e4SLinus Torvalds 59757c653828SCasey Schaufler isec = selinux_ipc(msq); 59767c653828SCasey Schaufler msec = selinux_msg_msg(msg); 59771da177e4SLinus Torvalds 59781da177e4SLinus Torvalds /* 59791da177e4SLinus Torvalds * First time through, need to assign label to the message 59801da177e4SLinus Torvalds */ 59811da177e4SLinus Torvalds if (msec->sid == SECINITSID_UNLABELED) { 59821da177e4SLinus Torvalds /* 59831da177e4SLinus Torvalds * Compute new sid based on current process and 59841da177e4SLinus Torvalds * message queue this message will be stored in 59851da177e4SLinus Torvalds */ 5986aa8e712cSStephen Smalley rc = security_transition_sid(&selinux_state, sid, isec->sid, 5987aa8e712cSStephen Smalley SECCLASS_MSG, NULL, &msec->sid); 59881da177e4SLinus Torvalds if (rc) 59891da177e4SLinus Torvalds return rc; 59901da177e4SLinus Torvalds } 59911da177e4SLinus Torvalds 599250c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 5993d8c6e854SEric W. Biederman ad.u.ipc_id = msq->key; 59941da177e4SLinus Torvalds 59951da177e4SLinus Torvalds /* Can this process write to the queue? */ 59966b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 59976b6bc620SStephen Smalley sid, isec->sid, SECCLASS_MSGQ, 59981da177e4SLinus Torvalds MSGQ__WRITE, &ad); 59991da177e4SLinus Torvalds if (!rc) 60001da177e4SLinus Torvalds /* Can this process send the message */ 60016b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 60026b6bc620SStephen Smalley sid, msec->sid, SECCLASS_MSG, 6003275bb41eSDavid Howells MSG__SEND, &ad); 60041da177e4SLinus Torvalds if (!rc) 60051da177e4SLinus Torvalds /* Can the message be put in the queue? */ 60066b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 60076b6bc620SStephen Smalley msec->sid, isec->sid, SECCLASS_MSGQ, 6008275bb41eSDavid Howells MSGQ__ENQUEUE, &ad); 60091da177e4SLinus Torvalds 60101da177e4SLinus Torvalds return rc; 60111da177e4SLinus Torvalds } 60121da177e4SLinus Torvalds 6013d8c6e854SEric W. Biederman static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg, 60141da177e4SLinus Torvalds struct task_struct *target, 60151da177e4SLinus Torvalds long type, int mode) 60161da177e4SLinus Torvalds { 60171da177e4SLinus Torvalds struct ipc_security_struct *isec; 60181da177e4SLinus Torvalds struct msg_security_struct *msec; 60192bf49690SThomas Liu struct common_audit_data ad; 6020275bb41eSDavid Howells u32 sid = task_sid(target); 60211da177e4SLinus Torvalds int rc; 60221da177e4SLinus Torvalds 60237c653828SCasey Schaufler isec = selinux_ipc(msq); 60247c653828SCasey Schaufler msec = selinux_msg_msg(msg); 60251da177e4SLinus Torvalds 602650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 6027d8c6e854SEric W. Biederman ad.u.ipc_id = msq->key; 60281da177e4SLinus Torvalds 60296b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 60306b6bc620SStephen Smalley sid, isec->sid, 60311da177e4SLinus Torvalds SECCLASS_MSGQ, MSGQ__READ, &ad); 60321da177e4SLinus Torvalds if (!rc) 60336b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 60346b6bc620SStephen Smalley sid, msec->sid, 60351da177e4SLinus Torvalds SECCLASS_MSG, MSG__RECEIVE, &ad); 60361da177e4SLinus Torvalds return rc; 60371da177e4SLinus Torvalds } 60381da177e4SLinus Torvalds 60391da177e4SLinus Torvalds /* Shared Memory security operations */ 60407191adffSEric W. Biederman static int selinux_shm_alloc_security(struct kern_ipc_perm *shp) 60411da177e4SLinus Torvalds { 60421da177e4SLinus Torvalds struct ipc_security_struct *isec; 60432bf49690SThomas Liu struct common_audit_data ad; 6044275bb41eSDavid Howells u32 sid = current_sid(); 60451da177e4SLinus Torvalds int rc; 60461da177e4SLinus Torvalds 6047ecd5f82eSCasey Schaufler isec = selinux_ipc(shp); 6048ecd5f82eSCasey Schaufler ipc_init_security(isec, SECCLASS_SHM); 60491da177e4SLinus Torvalds 605050c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 60517191adffSEric W. Biederman ad.u.ipc_id = shp->key; 60521da177e4SLinus Torvalds 60536b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 60546b6bc620SStephen Smalley sid, isec->sid, SECCLASS_SHM, 60551da177e4SLinus Torvalds SHM__CREATE, &ad); 60561da177e4SLinus Torvalds return rc; 60571da177e4SLinus Torvalds } 60581da177e4SLinus Torvalds 60597191adffSEric W. Biederman static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg) 60601da177e4SLinus Torvalds { 60611da177e4SLinus Torvalds struct ipc_security_struct *isec; 60622bf49690SThomas Liu struct common_audit_data ad; 6063275bb41eSDavid Howells u32 sid = current_sid(); 60641da177e4SLinus Torvalds 60657c653828SCasey Schaufler isec = selinux_ipc(shp); 60661da177e4SLinus Torvalds 606750c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 60687191adffSEric W. Biederman ad.u.ipc_id = shp->key; 60691da177e4SLinus Torvalds 60706b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 60716b6bc620SStephen Smalley sid, isec->sid, SECCLASS_SHM, 60721da177e4SLinus Torvalds SHM__ASSOCIATE, &ad); 60731da177e4SLinus Torvalds } 60741da177e4SLinus Torvalds 60751da177e4SLinus Torvalds /* Note, at this point, shp is locked down */ 60767191adffSEric W. Biederman static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd) 60771da177e4SLinus Torvalds { 60781da177e4SLinus Torvalds int perms; 60791da177e4SLinus Torvalds int err; 60801da177e4SLinus Torvalds 60811da177e4SLinus Torvalds switch (cmd) { 60821da177e4SLinus Torvalds case IPC_INFO: 60831da177e4SLinus Torvalds case SHM_INFO: 60841da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 60856b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 60866b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, 6087be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); 60881da177e4SLinus Torvalds case IPC_STAT: 60891da177e4SLinus Torvalds case SHM_STAT: 6090c21a6970SDavidlohr Bueso case SHM_STAT_ANY: 60911da177e4SLinus Torvalds perms = SHM__GETATTR | SHM__ASSOCIATE; 60921da177e4SLinus Torvalds break; 60931da177e4SLinus Torvalds case IPC_SET: 60941da177e4SLinus Torvalds perms = SHM__SETATTR; 60951da177e4SLinus Torvalds break; 60961da177e4SLinus Torvalds case SHM_LOCK: 60971da177e4SLinus Torvalds case SHM_UNLOCK: 60981da177e4SLinus Torvalds perms = SHM__LOCK; 60991da177e4SLinus Torvalds break; 61001da177e4SLinus Torvalds case IPC_RMID: 61011da177e4SLinus Torvalds perms = SHM__DESTROY; 61021da177e4SLinus Torvalds break; 61031da177e4SLinus Torvalds default: 61041da177e4SLinus Torvalds return 0; 61051da177e4SLinus Torvalds } 61061da177e4SLinus Torvalds 61077191adffSEric W. Biederman err = ipc_has_perm(shp, perms); 61081da177e4SLinus Torvalds return err; 61091da177e4SLinus Torvalds } 61101da177e4SLinus Torvalds 61117191adffSEric W. Biederman static int selinux_shm_shmat(struct kern_ipc_perm *shp, 61121da177e4SLinus Torvalds char __user *shmaddr, int shmflg) 61131da177e4SLinus Torvalds { 61141da177e4SLinus Torvalds u32 perms; 61151da177e4SLinus Torvalds 61161da177e4SLinus Torvalds if (shmflg & SHM_RDONLY) 61171da177e4SLinus Torvalds perms = SHM__READ; 61181da177e4SLinus Torvalds else 61191da177e4SLinus Torvalds perms = SHM__READ | SHM__WRITE; 61201da177e4SLinus Torvalds 61217191adffSEric W. Biederman return ipc_has_perm(shp, perms); 61221da177e4SLinus Torvalds } 61231da177e4SLinus Torvalds 61241da177e4SLinus Torvalds /* Semaphore security operations */ 6125aefad959SEric W. Biederman static int selinux_sem_alloc_security(struct kern_ipc_perm *sma) 61261da177e4SLinus Torvalds { 61271da177e4SLinus Torvalds struct ipc_security_struct *isec; 61282bf49690SThomas Liu struct common_audit_data ad; 6129275bb41eSDavid Howells u32 sid = current_sid(); 61301da177e4SLinus Torvalds int rc; 61311da177e4SLinus Torvalds 6132ecd5f82eSCasey Schaufler isec = selinux_ipc(sma); 6133ecd5f82eSCasey Schaufler ipc_init_security(isec, SECCLASS_SEM); 61341da177e4SLinus Torvalds 613550c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 6136aefad959SEric W. Biederman ad.u.ipc_id = sma->key; 61371da177e4SLinus Torvalds 61386b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 61396b6bc620SStephen Smalley sid, isec->sid, SECCLASS_SEM, 61401da177e4SLinus Torvalds SEM__CREATE, &ad); 61411da177e4SLinus Torvalds return rc; 61421da177e4SLinus Torvalds } 61431da177e4SLinus Torvalds 6144aefad959SEric W. Biederman static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg) 61451da177e4SLinus Torvalds { 61461da177e4SLinus Torvalds struct ipc_security_struct *isec; 61472bf49690SThomas Liu struct common_audit_data ad; 6148275bb41eSDavid Howells u32 sid = current_sid(); 61491da177e4SLinus Torvalds 61507c653828SCasey Schaufler isec = selinux_ipc(sma); 61511da177e4SLinus Torvalds 615250c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 6153aefad959SEric W. Biederman ad.u.ipc_id = sma->key; 61541da177e4SLinus Torvalds 61556b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 61566b6bc620SStephen Smalley sid, isec->sid, SECCLASS_SEM, 61571da177e4SLinus Torvalds SEM__ASSOCIATE, &ad); 61581da177e4SLinus Torvalds } 61591da177e4SLinus Torvalds 61601da177e4SLinus Torvalds /* Note, at this point, sma is locked down */ 6161aefad959SEric W. Biederman static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd) 61621da177e4SLinus Torvalds { 61631da177e4SLinus Torvalds int err; 61641da177e4SLinus Torvalds u32 perms; 61651da177e4SLinus Torvalds 61661da177e4SLinus Torvalds switch (cmd) { 61671da177e4SLinus Torvalds case IPC_INFO: 61681da177e4SLinus Torvalds case SEM_INFO: 61691da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 61706b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 61716b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, 6172be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); 61731da177e4SLinus Torvalds case GETPID: 61741da177e4SLinus Torvalds case GETNCNT: 61751da177e4SLinus Torvalds case GETZCNT: 61761da177e4SLinus Torvalds perms = SEM__GETATTR; 61771da177e4SLinus Torvalds break; 61781da177e4SLinus Torvalds case GETVAL: 61791da177e4SLinus Torvalds case GETALL: 61801da177e4SLinus Torvalds perms = SEM__READ; 61811da177e4SLinus Torvalds break; 61821da177e4SLinus Torvalds case SETVAL: 61831da177e4SLinus Torvalds case SETALL: 61841da177e4SLinus Torvalds perms = SEM__WRITE; 61851da177e4SLinus Torvalds break; 61861da177e4SLinus Torvalds case IPC_RMID: 61871da177e4SLinus Torvalds perms = SEM__DESTROY; 61881da177e4SLinus Torvalds break; 61891da177e4SLinus Torvalds case IPC_SET: 61901da177e4SLinus Torvalds perms = SEM__SETATTR; 61911da177e4SLinus Torvalds break; 61921da177e4SLinus Torvalds case IPC_STAT: 61931da177e4SLinus Torvalds case SEM_STAT: 6194a280d6dcSDavidlohr Bueso case SEM_STAT_ANY: 61951da177e4SLinus Torvalds perms = SEM__GETATTR | SEM__ASSOCIATE; 61961da177e4SLinus Torvalds break; 61971da177e4SLinus Torvalds default: 61981da177e4SLinus Torvalds return 0; 61991da177e4SLinus Torvalds } 62001da177e4SLinus Torvalds 6201aefad959SEric W. Biederman err = ipc_has_perm(sma, perms); 62021da177e4SLinus Torvalds return err; 62031da177e4SLinus Torvalds } 62041da177e4SLinus Torvalds 6205aefad959SEric W. Biederman static int selinux_sem_semop(struct kern_ipc_perm *sma, 62061da177e4SLinus Torvalds struct sembuf *sops, unsigned nsops, int alter) 62071da177e4SLinus Torvalds { 62081da177e4SLinus Torvalds u32 perms; 62091da177e4SLinus Torvalds 62101da177e4SLinus Torvalds if (alter) 62111da177e4SLinus Torvalds perms = SEM__READ | SEM__WRITE; 62121da177e4SLinus Torvalds else 62131da177e4SLinus Torvalds perms = SEM__READ; 62141da177e4SLinus Torvalds 6215aefad959SEric W. Biederman return ipc_has_perm(sma, perms); 62161da177e4SLinus Torvalds } 62171da177e4SLinus Torvalds 62181da177e4SLinus Torvalds static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag) 62191da177e4SLinus Torvalds { 62201da177e4SLinus Torvalds u32 av = 0; 62211da177e4SLinus Torvalds 62221da177e4SLinus Torvalds av = 0; 62231da177e4SLinus Torvalds if (flag & S_IRUGO) 62241da177e4SLinus Torvalds av |= IPC__UNIX_READ; 62251da177e4SLinus Torvalds if (flag & S_IWUGO) 62261da177e4SLinus Torvalds av |= IPC__UNIX_WRITE; 62271da177e4SLinus Torvalds 62281da177e4SLinus Torvalds if (av == 0) 62291da177e4SLinus Torvalds return 0; 62301da177e4SLinus Torvalds 62316af963f1SStephen Smalley return ipc_has_perm(ipcp, av); 62321da177e4SLinus Torvalds } 62331da177e4SLinus Torvalds 6234713a04aeSAhmed S. Darwish static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid) 6235713a04aeSAhmed S. Darwish { 62367c653828SCasey Schaufler struct ipc_security_struct *isec = selinux_ipc(ipcp); 6237713a04aeSAhmed S. Darwish *secid = isec->sid; 6238713a04aeSAhmed S. Darwish } 6239713a04aeSAhmed S. Darwish 62401da177e4SLinus Torvalds static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode) 62411da177e4SLinus Torvalds { 62421da177e4SLinus Torvalds if (inode) 62431da177e4SLinus Torvalds inode_doinit_with_dentry(inode, dentry); 62441da177e4SLinus Torvalds } 62451da177e4SLinus Torvalds 62461da177e4SLinus Torvalds static int selinux_getprocattr(struct task_struct *p, 624704ff9708SAl Viro char *name, char **value) 62481da177e4SLinus Torvalds { 6249275bb41eSDavid Howells const struct task_security_struct *__tsec; 62508c8570fbSDustin Kirkland u32 sid; 62511da177e4SLinus Torvalds int error; 625204ff9708SAl Viro unsigned len; 62531da177e4SLinus Torvalds 6254275bb41eSDavid Howells rcu_read_lock(); 62550c6cfa62SCasey Schaufler __tsec = selinux_cred(__task_cred(p)); 62561da177e4SLinus Torvalds 6257be0554c9SStephen Smalley if (current != p) { 62586b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 62596b6bc620SStephen Smalley current_sid(), __tsec->sid, 6260be0554c9SStephen Smalley SECCLASS_PROCESS, PROCESS__GETATTR, NULL); 6261be0554c9SStephen Smalley if (error) 6262be0554c9SStephen Smalley goto bad; 6263be0554c9SStephen Smalley } 6264be0554c9SStephen Smalley 62651da177e4SLinus Torvalds if (!strcmp(name, "current")) 6266275bb41eSDavid Howells sid = __tsec->sid; 62671da177e4SLinus Torvalds else if (!strcmp(name, "prev")) 6268275bb41eSDavid Howells sid = __tsec->osid; 62691da177e4SLinus Torvalds else if (!strcmp(name, "exec")) 6270275bb41eSDavid Howells sid = __tsec->exec_sid; 62711da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 6272275bb41eSDavid Howells sid = __tsec->create_sid; 62734eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) 6274275bb41eSDavid Howells sid = __tsec->keycreate_sid; 627542c3e03eSEric Paris else if (!strcmp(name, "sockcreate")) 6276275bb41eSDavid Howells sid = __tsec->sockcreate_sid; 6277be0554c9SStephen Smalley else { 6278be0554c9SStephen Smalley error = -EINVAL; 6279be0554c9SStephen Smalley goto bad; 6280be0554c9SStephen Smalley } 6281275bb41eSDavid Howells rcu_read_unlock(); 62821da177e4SLinus Torvalds 62831da177e4SLinus Torvalds if (!sid) 62841da177e4SLinus Torvalds return 0; 62851da177e4SLinus Torvalds 6286aa8e712cSStephen Smalley error = security_sid_to_context(&selinux_state, sid, value, &len); 628704ff9708SAl Viro if (error) 628804ff9708SAl Viro return error; 628904ff9708SAl Viro return len; 6290275bb41eSDavid Howells 6291be0554c9SStephen Smalley bad: 6292275bb41eSDavid Howells rcu_read_unlock(); 6293be0554c9SStephen Smalley return error; 62941da177e4SLinus Torvalds } 62951da177e4SLinus Torvalds 6296b21507e2SStephen Smalley static int selinux_setprocattr(const char *name, void *value, size_t size) 62971da177e4SLinus Torvalds { 62981da177e4SLinus Torvalds struct task_security_struct *tsec; 6299d84f4f99SDavid Howells struct cred *new; 6300be0554c9SStephen Smalley u32 mysid = current_sid(), sid = 0, ptsid; 63011da177e4SLinus Torvalds int error; 63021da177e4SLinus Torvalds char *str = value; 63031da177e4SLinus Torvalds 63041da177e4SLinus Torvalds /* 63051da177e4SLinus Torvalds * Basic control over ability to set these attributes at all. 63061da177e4SLinus Torvalds */ 63071da177e4SLinus Torvalds if (!strcmp(name, "exec")) 63086b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 63096b6bc620SStephen Smalley mysid, mysid, SECCLASS_PROCESS, 6310be0554c9SStephen Smalley PROCESS__SETEXEC, NULL); 63111da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 63126b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 63136b6bc620SStephen Smalley mysid, mysid, SECCLASS_PROCESS, 6314be0554c9SStephen Smalley PROCESS__SETFSCREATE, NULL); 63154eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) 63166b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 63176b6bc620SStephen Smalley mysid, mysid, SECCLASS_PROCESS, 6318be0554c9SStephen Smalley PROCESS__SETKEYCREATE, NULL); 631942c3e03eSEric Paris else if (!strcmp(name, "sockcreate")) 63206b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 63216b6bc620SStephen Smalley mysid, mysid, SECCLASS_PROCESS, 6322be0554c9SStephen Smalley PROCESS__SETSOCKCREATE, NULL); 63231da177e4SLinus Torvalds else if (!strcmp(name, "current")) 63246b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 63256b6bc620SStephen Smalley mysid, mysid, SECCLASS_PROCESS, 6326be0554c9SStephen Smalley PROCESS__SETCURRENT, NULL); 63271da177e4SLinus Torvalds else 63281da177e4SLinus Torvalds error = -EINVAL; 63291da177e4SLinus Torvalds if (error) 63301da177e4SLinus Torvalds return error; 63311da177e4SLinus Torvalds 63321da177e4SLinus Torvalds /* Obtain a SID for the context, if one was specified. */ 6333a050a570SStephen Smalley if (size && str[0] && str[0] != '\n') { 63341da177e4SLinus Torvalds if (str[size-1] == '\n') { 63351da177e4SLinus Torvalds str[size-1] = 0; 63361da177e4SLinus Torvalds size--; 63371da177e4SLinus Torvalds } 6338aa8e712cSStephen Smalley error = security_context_to_sid(&selinux_state, value, size, 6339aa8e712cSStephen Smalley &sid, GFP_KERNEL); 634012b29f34SStephen Smalley if (error == -EINVAL && !strcmp(name, "fscreate")) { 6341db59000aSStephen Smalley if (!has_cap_mac_admin(true)) { 6342d6ea83ecSEric Paris struct audit_buffer *ab; 6343d6ea83ecSEric Paris size_t audit_size; 6344d6ea83ecSEric Paris 6345d6ea83ecSEric Paris /* We strip a nul only if it is at the end, otherwise the 6346d6ea83ecSEric Paris * context contains a nul and we should audit that */ 6347d6ea83ecSEric Paris if (str[size - 1] == '\0') 6348d6ea83ecSEric Paris audit_size = size - 1; 6349d6ea83ecSEric Paris else 6350d6ea83ecSEric Paris audit_size = size; 6351cdfb6b34SRichard Guy Briggs ab = audit_log_start(audit_context(), 6352cdfb6b34SRichard Guy Briggs GFP_ATOMIC, 6353cdfb6b34SRichard Guy Briggs AUDIT_SELINUX_ERR); 6354d6ea83ecSEric Paris audit_log_format(ab, "op=fscreate invalid_context="); 6355d6ea83ecSEric Paris audit_log_n_untrustedstring(ab, value, audit_size); 6356d6ea83ecSEric Paris audit_log_end(ab); 6357d6ea83ecSEric Paris 635812b29f34SStephen Smalley return error; 6359d6ea83ecSEric Paris } 6360aa8e712cSStephen Smalley error = security_context_to_sid_force( 6361aa8e712cSStephen Smalley &selinux_state, 6362aa8e712cSStephen Smalley value, size, &sid); 636312b29f34SStephen Smalley } 63641da177e4SLinus Torvalds if (error) 63651da177e4SLinus Torvalds return error; 63661da177e4SLinus Torvalds } 63671da177e4SLinus Torvalds 6368d84f4f99SDavid Howells new = prepare_creds(); 6369d84f4f99SDavid Howells if (!new) 6370d84f4f99SDavid Howells return -ENOMEM; 6371d84f4f99SDavid Howells 63721da177e4SLinus Torvalds /* Permission checking based on the specified context is 63731da177e4SLinus Torvalds performed during the actual operation (execve, 63741da177e4SLinus Torvalds open/mkdir/...), when we know the full context of the 6375d84f4f99SDavid Howells operation. See selinux_bprm_set_creds for the execve 63761da177e4SLinus Torvalds checks and may_create for the file creation checks. The 63771da177e4SLinus Torvalds operation will then fail if the context is not permitted. */ 63780c6cfa62SCasey Schaufler tsec = selinux_cred(new); 6379d84f4f99SDavid Howells if (!strcmp(name, "exec")) { 63801da177e4SLinus Torvalds tsec->exec_sid = sid; 6381d84f4f99SDavid Howells } else if (!strcmp(name, "fscreate")) { 63821da177e4SLinus Torvalds tsec->create_sid = sid; 6383d84f4f99SDavid Howells } else if (!strcmp(name, "keycreate")) { 6384464c258aSOndrej Mosnacek if (sid) { 6385464c258aSOndrej Mosnacek error = avc_has_perm(&selinux_state, mysid, sid, 6386464c258aSOndrej Mosnacek SECCLASS_KEY, KEY__CREATE, NULL); 63874eb582cfSMichael LeMay if (error) 6388d84f4f99SDavid Howells goto abort_change; 6389464c258aSOndrej Mosnacek } 63904eb582cfSMichael LeMay tsec->keycreate_sid = sid; 6391d84f4f99SDavid Howells } else if (!strcmp(name, "sockcreate")) { 639242c3e03eSEric Paris tsec->sockcreate_sid = sid; 6393d84f4f99SDavid Howells } else if (!strcmp(name, "current")) { 6394d84f4f99SDavid Howells error = -EINVAL; 63951da177e4SLinus Torvalds if (sid == 0) 6396d84f4f99SDavid Howells goto abort_change; 6397d9250deaSKaiGai Kohei 6398d84f4f99SDavid Howells /* Only allow single threaded processes to change context */ 6399d84f4f99SDavid Howells error = -EPERM; 64005bb459bbSOleg Nesterov if (!current_is_single_threaded()) { 6401aa8e712cSStephen Smalley error = security_bounded_transition(&selinux_state, 6402aa8e712cSStephen Smalley tsec->sid, sid); 6403d84f4f99SDavid Howells if (error) 6404d84f4f99SDavid Howells goto abort_change; 64051da177e4SLinus Torvalds } 64061da177e4SLinus Torvalds 64071da177e4SLinus Torvalds /* Check permissions for the transition. */ 64086b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 64096b6bc620SStephen Smalley tsec->sid, sid, SECCLASS_PROCESS, 64101da177e4SLinus Torvalds PROCESS__DYNTRANSITION, NULL); 64111da177e4SLinus Torvalds if (error) 6412d84f4f99SDavid Howells goto abort_change; 64131da177e4SLinus Torvalds 64141da177e4SLinus Torvalds /* Check for ptracing, and update the task SID if ok. 64151da177e4SLinus Torvalds Otherwise, leave SID unchanged and fail. */ 6416be0554c9SStephen Smalley ptsid = ptrace_parent_sid(); 64170c6181cbSPaul Moore if (ptsid != 0) { 64186b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 64196b6bc620SStephen Smalley ptsid, sid, SECCLASS_PROCESS, 6420d84f4f99SDavid Howells PROCESS__PTRACE, NULL); 6421d84f4f99SDavid Howells if (error) 6422d84f4f99SDavid Howells goto abort_change; 6423d84f4f99SDavid Howells } 6424d84f4f99SDavid Howells 6425d84f4f99SDavid Howells tsec->sid = sid; 6426d84f4f99SDavid Howells } else { 6427d84f4f99SDavid Howells error = -EINVAL; 6428d84f4f99SDavid Howells goto abort_change; 6429d84f4f99SDavid Howells } 6430d84f4f99SDavid Howells 6431d84f4f99SDavid Howells commit_creds(new); 64321da177e4SLinus Torvalds return size; 6433d84f4f99SDavid Howells 6434d84f4f99SDavid Howells abort_change: 6435d84f4f99SDavid Howells abort_creds(new); 6436d84f4f99SDavid Howells return error; 64371da177e4SLinus Torvalds } 64381da177e4SLinus Torvalds 6439746df9b5SDavid Quigley static int selinux_ismaclabel(const char *name) 6440746df9b5SDavid Quigley { 6441746df9b5SDavid Quigley return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0); 6442746df9b5SDavid Quigley } 6443746df9b5SDavid Quigley 6444dc49c1f9SCatherine Zhang static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) 6445dc49c1f9SCatherine Zhang { 6446aa8e712cSStephen Smalley return security_sid_to_context(&selinux_state, secid, 6447aa8e712cSStephen Smalley secdata, seclen); 6448dc49c1f9SCatherine Zhang } 6449dc49c1f9SCatherine Zhang 64507bf570dcSDavid Howells static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid) 645163cb3449SDavid Howells { 6452aa8e712cSStephen Smalley return security_context_to_sid(&selinux_state, secdata, seclen, 6453aa8e712cSStephen Smalley secid, GFP_KERNEL); 645463cb3449SDavid Howells } 645563cb3449SDavid Howells 6456dc49c1f9SCatherine Zhang static void selinux_release_secctx(char *secdata, u32 seclen) 6457dc49c1f9SCatherine Zhang { 6458dc49c1f9SCatherine Zhang kfree(secdata); 6459dc49c1f9SCatherine Zhang } 6460dc49c1f9SCatherine Zhang 64616f3be9f5SAndreas Gruenbacher static void selinux_inode_invalidate_secctx(struct inode *inode) 64626f3be9f5SAndreas Gruenbacher { 646380788c22SCasey Schaufler struct inode_security_struct *isec = selinux_inode(inode); 64646f3be9f5SAndreas Gruenbacher 64659287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 64666f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INVALID; 64679287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 64686f3be9f5SAndreas Gruenbacher } 64696f3be9f5SAndreas Gruenbacher 64701ee65e37SDavid P. Quigley /* 64711ee65e37SDavid P. Quigley * called with inode->i_mutex locked 64721ee65e37SDavid P. Quigley */ 64731ee65e37SDavid P. Quigley static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen) 64741ee65e37SDavid P. Quigley { 647553e0c2aaSOndrej Mosnacek int rc = selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, 647653e0c2aaSOndrej Mosnacek ctx, ctxlen, 0); 647753e0c2aaSOndrej Mosnacek /* Do not return error when suppressing label (SBLABEL_MNT not set). */ 647853e0c2aaSOndrej Mosnacek return rc == -EOPNOTSUPP ? 0 : rc; 64791ee65e37SDavid P. Quigley } 64801ee65e37SDavid P. Quigley 64811ee65e37SDavid P. Quigley /* 64821ee65e37SDavid P. Quigley * called with inode->i_mutex locked 64831ee65e37SDavid P. Quigley */ 64841ee65e37SDavid P. Quigley static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen) 64851ee65e37SDavid P. Quigley { 64861ee65e37SDavid P. Quigley return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0); 64871ee65e37SDavid P. Quigley } 64881ee65e37SDavid P. Quigley 64891ee65e37SDavid P. Quigley static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen) 64901ee65e37SDavid P. Quigley { 64911ee65e37SDavid P. Quigley int len = 0; 64921ee65e37SDavid P. Quigley len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX, 64931ee65e37SDavid P. Quigley ctx, true); 64941ee65e37SDavid P. Quigley if (len < 0) 64951ee65e37SDavid P. Quigley return len; 64961ee65e37SDavid P. Quigley *ctxlen = len; 64971ee65e37SDavid P. Quigley return 0; 64981ee65e37SDavid P. Quigley } 6499d720024eSMichael LeMay #ifdef CONFIG_KEYS 6500d720024eSMichael LeMay 6501d84f4f99SDavid Howells static int selinux_key_alloc(struct key *k, const struct cred *cred, 65027e047ef5SDavid Howells unsigned long flags) 6503d720024eSMichael LeMay { 6504d84f4f99SDavid Howells const struct task_security_struct *tsec; 6505d720024eSMichael LeMay struct key_security_struct *ksec; 6506d720024eSMichael LeMay 6507d720024eSMichael LeMay ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL); 6508d720024eSMichael LeMay if (!ksec) 6509d720024eSMichael LeMay return -ENOMEM; 6510d720024eSMichael LeMay 65110c6cfa62SCasey Schaufler tsec = selinux_cred(cred); 6512d84f4f99SDavid Howells if (tsec->keycreate_sid) 6513d84f4f99SDavid Howells ksec->sid = tsec->keycreate_sid; 65144eb582cfSMichael LeMay else 6515d84f4f99SDavid Howells ksec->sid = tsec->sid; 6516d720024eSMichael LeMay 6517275bb41eSDavid Howells k->security = ksec; 6518d720024eSMichael LeMay return 0; 6519d720024eSMichael LeMay } 6520d720024eSMichael LeMay 6521d720024eSMichael LeMay static void selinux_key_free(struct key *k) 6522d720024eSMichael LeMay { 6523d720024eSMichael LeMay struct key_security_struct *ksec = k->security; 6524d720024eSMichael LeMay 6525d720024eSMichael LeMay k->security = NULL; 6526d720024eSMichael LeMay kfree(ksec); 6527d720024eSMichael LeMay } 6528d720024eSMichael LeMay 6529d720024eSMichael LeMay static int selinux_key_permission(key_ref_t key_ref, 6530d84f4f99SDavid Howells const struct cred *cred, 6531f5895943SDavid Howells unsigned perm) 6532d720024eSMichael LeMay { 6533d720024eSMichael LeMay struct key *key; 6534d720024eSMichael LeMay struct key_security_struct *ksec; 6535275bb41eSDavid Howells u32 sid; 6536d720024eSMichael LeMay 6537d720024eSMichael LeMay /* if no specific permissions are requested, we skip the 6538d720024eSMichael LeMay permission check. No serious, additional covert channels 6539d720024eSMichael LeMay appear to be created. */ 6540d720024eSMichael LeMay if (perm == 0) 6541d720024eSMichael LeMay return 0; 6542d720024eSMichael LeMay 6543d84f4f99SDavid Howells sid = cred_sid(cred); 6544275bb41eSDavid Howells 6545275bb41eSDavid Howells key = key_ref_to_ptr(key_ref); 6546275bb41eSDavid Howells ksec = key->security; 6547275bb41eSDavid Howells 65486b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 6549028db3e2SLinus Torvalds sid, ksec->sid, SECCLASS_KEY, perm, NULL); 6550d720024eSMichael LeMay } 6551d720024eSMichael LeMay 655270a5bb72SDavid Howells static int selinux_key_getsecurity(struct key *key, char **_buffer) 655370a5bb72SDavid Howells { 655470a5bb72SDavid Howells struct key_security_struct *ksec = key->security; 655570a5bb72SDavid Howells char *context = NULL; 655670a5bb72SDavid Howells unsigned len; 655770a5bb72SDavid Howells int rc; 655870a5bb72SDavid Howells 6559aa8e712cSStephen Smalley rc = security_sid_to_context(&selinux_state, ksec->sid, 6560aa8e712cSStephen Smalley &context, &len); 656170a5bb72SDavid Howells if (!rc) 656270a5bb72SDavid Howells rc = len; 656370a5bb72SDavid Howells *_buffer = context; 656470a5bb72SDavid Howells return rc; 656570a5bb72SDavid Howells } 65663a976fa6SDaniel Jurgens #endif 656770a5bb72SDavid Howells 65683a976fa6SDaniel Jurgens #ifdef CONFIG_SECURITY_INFINIBAND 6569cfc4d882SDaniel Jurgens static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val) 6570cfc4d882SDaniel Jurgens { 6571cfc4d882SDaniel Jurgens struct common_audit_data ad; 6572cfc4d882SDaniel Jurgens int err; 6573cfc4d882SDaniel Jurgens u32 sid = 0; 6574cfc4d882SDaniel Jurgens struct ib_security_struct *sec = ib_sec; 6575cfc4d882SDaniel Jurgens struct lsm_ibpkey_audit ibpkey; 6576cfc4d882SDaniel Jurgens 6577409dcf31SDaniel Jurgens err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid); 6578cfc4d882SDaniel Jurgens if (err) 6579cfc4d882SDaniel Jurgens return err; 6580cfc4d882SDaniel Jurgens 6581cfc4d882SDaniel Jurgens ad.type = LSM_AUDIT_DATA_IBPKEY; 6582cfc4d882SDaniel Jurgens ibpkey.subnet_prefix = subnet_prefix; 6583cfc4d882SDaniel Jurgens ibpkey.pkey = pkey_val; 6584cfc4d882SDaniel Jurgens ad.u.ibpkey = &ibpkey; 65856b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 65866b6bc620SStephen Smalley sec->sid, sid, 6587cfc4d882SDaniel Jurgens SECCLASS_INFINIBAND_PKEY, 6588cfc4d882SDaniel Jurgens INFINIBAND_PKEY__ACCESS, &ad); 6589cfc4d882SDaniel Jurgens } 6590cfc4d882SDaniel Jurgens 6591ab861dfcSDaniel Jurgens static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name, 6592ab861dfcSDaniel Jurgens u8 port_num) 6593ab861dfcSDaniel Jurgens { 6594ab861dfcSDaniel Jurgens struct common_audit_data ad; 6595ab861dfcSDaniel Jurgens int err; 6596ab861dfcSDaniel Jurgens u32 sid = 0; 6597ab861dfcSDaniel Jurgens struct ib_security_struct *sec = ib_sec; 6598ab861dfcSDaniel Jurgens struct lsm_ibendport_audit ibendport; 6599ab861dfcSDaniel Jurgens 6600aa8e712cSStephen Smalley err = security_ib_endport_sid(&selinux_state, dev_name, port_num, 6601aa8e712cSStephen Smalley &sid); 6602ab861dfcSDaniel Jurgens 6603ab861dfcSDaniel Jurgens if (err) 6604ab861dfcSDaniel Jurgens return err; 6605ab861dfcSDaniel Jurgens 6606ab861dfcSDaniel Jurgens ad.type = LSM_AUDIT_DATA_IBENDPORT; 6607ab861dfcSDaniel Jurgens strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name)); 6608ab861dfcSDaniel Jurgens ibendport.port = port_num; 6609ab861dfcSDaniel Jurgens ad.u.ibendport = &ibendport; 66106b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 66116b6bc620SStephen Smalley sec->sid, sid, 6612ab861dfcSDaniel Jurgens SECCLASS_INFINIBAND_ENDPORT, 6613ab861dfcSDaniel Jurgens INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad); 6614ab861dfcSDaniel Jurgens } 6615ab861dfcSDaniel Jurgens 66163a976fa6SDaniel Jurgens static int selinux_ib_alloc_security(void **ib_sec) 66173a976fa6SDaniel Jurgens { 66183a976fa6SDaniel Jurgens struct ib_security_struct *sec; 66193a976fa6SDaniel Jurgens 66203a976fa6SDaniel Jurgens sec = kzalloc(sizeof(*sec), GFP_KERNEL); 66213a976fa6SDaniel Jurgens if (!sec) 66223a976fa6SDaniel Jurgens return -ENOMEM; 66233a976fa6SDaniel Jurgens sec->sid = current_sid(); 66243a976fa6SDaniel Jurgens 66253a976fa6SDaniel Jurgens *ib_sec = sec; 66263a976fa6SDaniel Jurgens return 0; 66273a976fa6SDaniel Jurgens } 66283a976fa6SDaniel Jurgens 66293a976fa6SDaniel Jurgens static void selinux_ib_free_security(void *ib_sec) 66303a976fa6SDaniel Jurgens { 66313a976fa6SDaniel Jurgens kfree(ib_sec); 66323a976fa6SDaniel Jurgens } 6633d720024eSMichael LeMay #endif 6634d720024eSMichael LeMay 6635ec27c356SChenbo Feng #ifdef CONFIG_BPF_SYSCALL 6636ec27c356SChenbo Feng static int selinux_bpf(int cmd, union bpf_attr *attr, 6637ec27c356SChenbo Feng unsigned int size) 6638ec27c356SChenbo Feng { 6639ec27c356SChenbo Feng u32 sid = current_sid(); 6640ec27c356SChenbo Feng int ret; 6641ec27c356SChenbo Feng 6642ec27c356SChenbo Feng switch (cmd) { 6643ec27c356SChenbo Feng case BPF_MAP_CREATE: 66446b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 66456b6bc620SStephen Smalley sid, sid, SECCLASS_BPF, BPF__MAP_CREATE, 6646ec27c356SChenbo Feng NULL); 6647ec27c356SChenbo Feng break; 6648ec27c356SChenbo Feng case BPF_PROG_LOAD: 66496b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 66506b6bc620SStephen Smalley sid, sid, SECCLASS_BPF, BPF__PROG_LOAD, 6651ec27c356SChenbo Feng NULL); 6652ec27c356SChenbo Feng break; 6653ec27c356SChenbo Feng default: 6654ec27c356SChenbo Feng ret = 0; 6655ec27c356SChenbo Feng break; 6656ec27c356SChenbo Feng } 6657ec27c356SChenbo Feng 6658ec27c356SChenbo Feng return ret; 6659ec27c356SChenbo Feng } 6660ec27c356SChenbo Feng 6661ec27c356SChenbo Feng static u32 bpf_map_fmode_to_av(fmode_t fmode) 6662ec27c356SChenbo Feng { 6663ec27c356SChenbo Feng u32 av = 0; 6664ec27c356SChenbo Feng 6665ec27c356SChenbo Feng if (fmode & FMODE_READ) 6666ec27c356SChenbo Feng av |= BPF__MAP_READ; 6667ec27c356SChenbo Feng if (fmode & FMODE_WRITE) 6668ec27c356SChenbo Feng av |= BPF__MAP_WRITE; 6669ec27c356SChenbo Feng return av; 6670ec27c356SChenbo Feng } 6671ec27c356SChenbo Feng 6672f66e448cSChenbo Feng /* This function will check the file pass through unix socket or binder to see 6673f66e448cSChenbo Feng * if it is a bpf related object. And apply correspinding checks on the bpf 6674f66e448cSChenbo Feng * object based on the type. The bpf maps and programs, not like other files and 6675f66e448cSChenbo Feng * socket, are using a shared anonymous inode inside the kernel as their inode. 6676f66e448cSChenbo Feng * So checking that inode cannot identify if the process have privilege to 6677f66e448cSChenbo Feng * access the bpf object and that's why we have to add this additional check in 6678f66e448cSChenbo Feng * selinux_file_receive and selinux_binder_transfer_files. 6679f66e448cSChenbo Feng */ 6680f66e448cSChenbo Feng static int bpf_fd_pass(struct file *file, u32 sid) 6681f66e448cSChenbo Feng { 6682f66e448cSChenbo Feng struct bpf_security_struct *bpfsec; 6683f66e448cSChenbo Feng struct bpf_prog *prog; 6684f66e448cSChenbo Feng struct bpf_map *map; 6685f66e448cSChenbo Feng int ret; 6686f66e448cSChenbo Feng 6687f66e448cSChenbo Feng if (file->f_op == &bpf_map_fops) { 6688f66e448cSChenbo Feng map = file->private_data; 6689f66e448cSChenbo Feng bpfsec = map->security; 66906b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 66916b6bc620SStephen Smalley sid, bpfsec->sid, SECCLASS_BPF, 6692f66e448cSChenbo Feng bpf_map_fmode_to_av(file->f_mode), NULL); 6693f66e448cSChenbo Feng if (ret) 6694f66e448cSChenbo Feng return ret; 6695f66e448cSChenbo Feng } else if (file->f_op == &bpf_prog_fops) { 6696f66e448cSChenbo Feng prog = file->private_data; 6697f66e448cSChenbo Feng bpfsec = prog->aux->security; 66986b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 66996b6bc620SStephen Smalley sid, bpfsec->sid, SECCLASS_BPF, 6700f66e448cSChenbo Feng BPF__PROG_RUN, NULL); 6701f66e448cSChenbo Feng if (ret) 6702f66e448cSChenbo Feng return ret; 6703f66e448cSChenbo Feng } 6704f66e448cSChenbo Feng return 0; 6705f66e448cSChenbo Feng } 6706f66e448cSChenbo Feng 6707ec27c356SChenbo Feng static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode) 6708ec27c356SChenbo Feng { 6709ec27c356SChenbo Feng u32 sid = current_sid(); 6710ec27c356SChenbo Feng struct bpf_security_struct *bpfsec; 6711ec27c356SChenbo Feng 6712ec27c356SChenbo Feng bpfsec = map->security; 67136b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 67146b6bc620SStephen Smalley sid, bpfsec->sid, SECCLASS_BPF, 6715ec27c356SChenbo Feng bpf_map_fmode_to_av(fmode), NULL); 6716ec27c356SChenbo Feng } 6717ec27c356SChenbo Feng 6718ec27c356SChenbo Feng static int selinux_bpf_prog(struct bpf_prog *prog) 6719ec27c356SChenbo Feng { 6720ec27c356SChenbo Feng u32 sid = current_sid(); 6721ec27c356SChenbo Feng struct bpf_security_struct *bpfsec; 6722ec27c356SChenbo Feng 6723ec27c356SChenbo Feng bpfsec = prog->aux->security; 67246b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 67256b6bc620SStephen Smalley sid, bpfsec->sid, SECCLASS_BPF, 6726ec27c356SChenbo Feng BPF__PROG_RUN, NULL); 6727ec27c356SChenbo Feng } 6728ec27c356SChenbo Feng 6729ec27c356SChenbo Feng static int selinux_bpf_map_alloc(struct bpf_map *map) 6730ec27c356SChenbo Feng { 6731ec27c356SChenbo Feng struct bpf_security_struct *bpfsec; 6732ec27c356SChenbo Feng 6733ec27c356SChenbo Feng bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL); 6734ec27c356SChenbo Feng if (!bpfsec) 6735ec27c356SChenbo Feng return -ENOMEM; 6736ec27c356SChenbo Feng 6737ec27c356SChenbo Feng bpfsec->sid = current_sid(); 6738ec27c356SChenbo Feng map->security = bpfsec; 6739ec27c356SChenbo Feng 6740ec27c356SChenbo Feng return 0; 6741ec27c356SChenbo Feng } 6742ec27c356SChenbo Feng 6743ec27c356SChenbo Feng static void selinux_bpf_map_free(struct bpf_map *map) 6744ec27c356SChenbo Feng { 6745ec27c356SChenbo Feng struct bpf_security_struct *bpfsec = map->security; 6746ec27c356SChenbo Feng 6747ec27c356SChenbo Feng map->security = NULL; 6748ec27c356SChenbo Feng kfree(bpfsec); 6749ec27c356SChenbo Feng } 6750ec27c356SChenbo Feng 6751ec27c356SChenbo Feng static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux) 6752ec27c356SChenbo Feng { 6753ec27c356SChenbo Feng struct bpf_security_struct *bpfsec; 6754ec27c356SChenbo Feng 6755ec27c356SChenbo Feng bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL); 6756ec27c356SChenbo Feng if (!bpfsec) 6757ec27c356SChenbo Feng return -ENOMEM; 6758ec27c356SChenbo Feng 6759ec27c356SChenbo Feng bpfsec->sid = current_sid(); 6760ec27c356SChenbo Feng aux->security = bpfsec; 6761ec27c356SChenbo Feng 6762ec27c356SChenbo Feng return 0; 6763ec27c356SChenbo Feng } 6764ec27c356SChenbo Feng 6765ec27c356SChenbo Feng static void selinux_bpf_prog_free(struct bpf_prog_aux *aux) 6766ec27c356SChenbo Feng { 6767ec27c356SChenbo Feng struct bpf_security_struct *bpfsec = aux->security; 6768ec27c356SChenbo Feng 6769ec27c356SChenbo Feng aux->security = NULL; 6770ec27c356SChenbo Feng kfree(bpfsec); 6771ec27c356SChenbo Feng } 6772ec27c356SChenbo Feng #endif 6773ec27c356SChenbo Feng 677459438b46SStephen Smalley static int selinux_lockdown(enum lockdown_reason what) 677559438b46SStephen Smalley { 677659438b46SStephen Smalley struct common_audit_data ad; 677759438b46SStephen Smalley u32 sid = current_sid(); 677859438b46SStephen Smalley int invalid_reason = (what <= LOCKDOWN_NONE) || 677959438b46SStephen Smalley (what == LOCKDOWN_INTEGRITY_MAX) || 678059438b46SStephen Smalley (what >= LOCKDOWN_CONFIDENTIALITY_MAX); 678159438b46SStephen Smalley 678259438b46SStephen Smalley if (WARN(invalid_reason, "Invalid lockdown reason")) { 678359438b46SStephen Smalley audit_log(audit_context(), 678459438b46SStephen Smalley GFP_ATOMIC, AUDIT_SELINUX_ERR, 678559438b46SStephen Smalley "lockdown_reason=invalid"); 678659438b46SStephen Smalley return -EINVAL; 678759438b46SStephen Smalley } 678859438b46SStephen Smalley 678959438b46SStephen Smalley ad.type = LSM_AUDIT_DATA_LOCKDOWN; 679059438b46SStephen Smalley ad.u.reason = what; 679159438b46SStephen Smalley 679259438b46SStephen Smalley if (what <= LOCKDOWN_INTEGRITY_MAX) 679359438b46SStephen Smalley return avc_has_perm(&selinux_state, 679459438b46SStephen Smalley sid, sid, SECCLASS_LOCKDOWN, 679559438b46SStephen Smalley LOCKDOWN__INTEGRITY, &ad); 679659438b46SStephen Smalley else 679759438b46SStephen Smalley return avc_has_perm(&selinux_state, 679859438b46SStephen Smalley sid, sid, SECCLASS_LOCKDOWN, 679959438b46SStephen Smalley LOCKDOWN__CONFIDENTIALITY, &ad); 680059438b46SStephen Smalley } 680159438b46SStephen Smalley 6802bbd3662aSCasey Schaufler struct lsm_blob_sizes selinux_blob_sizes __lsm_ro_after_init = { 6803bbd3662aSCasey Schaufler .lbs_cred = sizeof(struct task_security_struct), 680433bf60caSCasey Schaufler .lbs_file = sizeof(struct file_security_struct), 6805afb1cbe3SCasey Schaufler .lbs_inode = sizeof(struct inode_security_struct), 6806ecd5f82eSCasey Schaufler .lbs_ipc = sizeof(struct ipc_security_struct), 6807ecd5f82eSCasey Schaufler .lbs_msg_msg = sizeof(struct msg_security_struct), 6808bbd3662aSCasey Schaufler }; 6809bbd3662aSCasey Schaufler 6810da97e184SJoel Fernandes (Google) #ifdef CONFIG_PERF_EVENTS 6811da97e184SJoel Fernandes (Google) static int selinux_perf_event_open(struct perf_event_attr *attr, int type) 6812da97e184SJoel Fernandes (Google) { 6813da97e184SJoel Fernandes (Google) u32 requested, sid = current_sid(); 6814da97e184SJoel Fernandes (Google) 6815da97e184SJoel Fernandes (Google) if (type == PERF_SECURITY_OPEN) 6816da97e184SJoel Fernandes (Google) requested = PERF_EVENT__OPEN; 6817da97e184SJoel Fernandes (Google) else if (type == PERF_SECURITY_CPU) 6818da97e184SJoel Fernandes (Google) requested = PERF_EVENT__CPU; 6819da97e184SJoel Fernandes (Google) else if (type == PERF_SECURITY_KERNEL) 6820da97e184SJoel Fernandes (Google) requested = PERF_EVENT__KERNEL; 6821da97e184SJoel Fernandes (Google) else if (type == PERF_SECURITY_TRACEPOINT) 6822da97e184SJoel Fernandes (Google) requested = PERF_EVENT__TRACEPOINT; 6823da97e184SJoel Fernandes (Google) else 6824da97e184SJoel Fernandes (Google) return -EINVAL; 6825da97e184SJoel Fernandes (Google) 6826da97e184SJoel Fernandes (Google) return avc_has_perm(&selinux_state, sid, sid, SECCLASS_PERF_EVENT, 6827da97e184SJoel Fernandes (Google) requested, NULL); 6828da97e184SJoel Fernandes (Google) } 6829da97e184SJoel Fernandes (Google) 6830da97e184SJoel Fernandes (Google) static int selinux_perf_event_alloc(struct perf_event *event) 6831da97e184SJoel Fernandes (Google) { 6832da97e184SJoel Fernandes (Google) struct perf_event_security_struct *perfsec; 6833da97e184SJoel Fernandes (Google) 6834da97e184SJoel Fernandes (Google) perfsec = kzalloc(sizeof(*perfsec), GFP_KERNEL); 6835da97e184SJoel Fernandes (Google) if (!perfsec) 6836da97e184SJoel Fernandes (Google) return -ENOMEM; 6837da97e184SJoel Fernandes (Google) 6838da97e184SJoel Fernandes (Google) perfsec->sid = current_sid(); 6839da97e184SJoel Fernandes (Google) event->security = perfsec; 6840da97e184SJoel Fernandes (Google) 6841da97e184SJoel Fernandes (Google) return 0; 6842da97e184SJoel Fernandes (Google) } 6843da97e184SJoel Fernandes (Google) 6844da97e184SJoel Fernandes (Google) static void selinux_perf_event_free(struct perf_event *event) 6845da97e184SJoel Fernandes (Google) { 6846da97e184SJoel Fernandes (Google) struct perf_event_security_struct *perfsec = event->security; 6847da97e184SJoel Fernandes (Google) 6848da97e184SJoel Fernandes (Google) event->security = NULL; 6849da97e184SJoel Fernandes (Google) kfree(perfsec); 6850da97e184SJoel Fernandes (Google) } 6851da97e184SJoel Fernandes (Google) 6852da97e184SJoel Fernandes (Google) static int selinux_perf_event_read(struct perf_event *event) 6853da97e184SJoel Fernandes (Google) { 6854da97e184SJoel Fernandes (Google) struct perf_event_security_struct *perfsec = event->security; 6855da97e184SJoel Fernandes (Google) u32 sid = current_sid(); 6856da97e184SJoel Fernandes (Google) 6857da97e184SJoel Fernandes (Google) return avc_has_perm(&selinux_state, sid, perfsec->sid, 6858da97e184SJoel Fernandes (Google) SECCLASS_PERF_EVENT, PERF_EVENT__READ, NULL); 6859da97e184SJoel Fernandes (Google) } 6860da97e184SJoel Fernandes (Google) 6861da97e184SJoel Fernandes (Google) static int selinux_perf_event_write(struct perf_event *event) 6862da97e184SJoel Fernandes (Google) { 6863da97e184SJoel Fernandes (Google) struct perf_event_security_struct *perfsec = event->security; 6864da97e184SJoel Fernandes (Google) u32 sid = current_sid(); 6865da97e184SJoel Fernandes (Google) 6866da97e184SJoel Fernandes (Google) return avc_has_perm(&selinux_state, sid, perfsec->sid, 6867da97e184SJoel Fernandes (Google) SECCLASS_PERF_EVENT, PERF_EVENT__WRITE, NULL); 6868da97e184SJoel Fernandes (Google) } 6869da97e184SJoel Fernandes (Google) #endif 6870da97e184SJoel Fernandes (Google) 6871cfff75d8SOndrej Mosnacek /* 6872cfff75d8SOndrej Mosnacek * IMPORTANT NOTE: When adding new hooks, please be careful to keep this order: 6873cfff75d8SOndrej Mosnacek * 1. any hooks that don't belong to (2.) or (3.) below, 6874cfff75d8SOndrej Mosnacek * 2. hooks that both access structures allocated by other hooks, and allocate 6875cfff75d8SOndrej Mosnacek * structures that can be later accessed by other hooks (mostly "cloning" 6876cfff75d8SOndrej Mosnacek * hooks), 6877cfff75d8SOndrej Mosnacek * 3. hooks that only allocate structures that can be later accessed by other 6878cfff75d8SOndrej Mosnacek * hooks ("allocating" hooks). 6879cfff75d8SOndrej Mosnacek * 6880cfff75d8SOndrej Mosnacek * Please follow block comment delimiters in the list to keep this order. 6881cfff75d8SOndrej Mosnacek * 6882cfff75d8SOndrej Mosnacek * This ordering is needed for SELinux runtime disable to work at least somewhat 6883cfff75d8SOndrej Mosnacek * safely. Breaking the ordering rules above might lead to NULL pointer derefs 6884cfff75d8SOndrej Mosnacek * when disabling SELinux at runtime. 6885cfff75d8SOndrej Mosnacek */ 6886ca97d939SJames Morris static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = { 6887e20b043aSCasey Schaufler LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr), 6888e20b043aSCasey Schaufler LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction), 6889e20b043aSCasey Schaufler LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder), 6890e20b043aSCasey Schaufler LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file), 6891076c54c5SAhmed S. Darwish 6892e20b043aSCasey Schaufler LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check), 6893e20b043aSCasey Schaufler LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme), 6894e20b043aSCasey Schaufler LSM_HOOK_INIT(capget, selinux_capget), 6895e20b043aSCasey Schaufler LSM_HOOK_INIT(capset, selinux_capset), 6896e20b043aSCasey Schaufler LSM_HOOK_INIT(capable, selinux_capable), 6897e20b043aSCasey Schaufler LSM_HOOK_INIT(quotactl, selinux_quotactl), 6898e20b043aSCasey Schaufler LSM_HOOK_INIT(quota_on, selinux_quota_on), 6899e20b043aSCasey Schaufler LSM_HOOK_INIT(syslog, selinux_syslog), 6900e20b043aSCasey Schaufler LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory), 690179af7307SStephen Smalley 6902e20b043aSCasey Schaufler LSM_HOOK_INIT(netlink_send, selinux_netlink_send), 69031da177e4SLinus Torvalds 6904e20b043aSCasey Schaufler LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds), 6905e20b043aSCasey Schaufler LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds), 6906e20b043aSCasey Schaufler LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds), 69071da177e4SLinus Torvalds 6908e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security), 6909204cc0ccSAl Viro LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts), 6910e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_remount, selinux_sb_remount), 6911e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount), 6912e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options), 6913e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs), 6914e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_mount, selinux_mount), 6915e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_umount, selinux_umount), 6916e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts), 6917e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts), 69181da177e4SLinus Torvalds 691998aa0034SStephen Smalley LSM_HOOK_INIT(move_mount, selinux_move_mount), 692098aa0034SStephen Smalley 6921e20b043aSCasey Schaufler LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security), 6922a518b0a5SVivek Goyal LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as), 6923e0007529SEric Paris 6924e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security), 6925e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security), 6926e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_create, selinux_inode_create), 6927e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_link, selinux_inode_link), 6928e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink), 6929e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink), 6930e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir), 6931e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir), 6932e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod), 6933e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_rename, selinux_inode_rename), 6934e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink), 6935e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link), 6936e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_permission, selinux_inode_permission), 6937e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr), 6938e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr), 6939e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr), 6940e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr), 6941e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr), 6942e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr), 6943e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr), 6944e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity), 6945e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity), 6946e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity), 6947e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid), 694856909eb3SVivek Goyal LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up), 694919472b69SVivek Goyal LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr), 6950ac5656d8SAaron Goidel LSM_HOOK_INIT(path_notify, selinux_path_notify), 69511da177e4SLinus Torvalds 6952ec882da5SOndrej Mosnacek LSM_HOOK_INIT(kernfs_init_security, selinux_kernfs_init_security), 6953ec882da5SOndrej Mosnacek 6954e20b043aSCasey Schaufler LSM_HOOK_INIT(file_permission, selinux_file_permission), 6955e20b043aSCasey Schaufler LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security), 6956e20b043aSCasey Schaufler LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl), 6957e20b043aSCasey Schaufler LSM_HOOK_INIT(mmap_file, selinux_mmap_file), 6958e20b043aSCasey Schaufler LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr), 6959e20b043aSCasey Schaufler LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect), 6960e20b043aSCasey Schaufler LSM_HOOK_INIT(file_lock, selinux_file_lock), 6961e20b043aSCasey Schaufler LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl), 6962e20b043aSCasey Schaufler LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner), 6963e20b043aSCasey Schaufler LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask), 6964e20b043aSCasey Schaufler LSM_HOOK_INIT(file_receive, selinux_file_receive), 69651da177e4SLinus Torvalds 6966e20b043aSCasey Schaufler LSM_HOOK_INIT(file_open, selinux_file_open), 69671da177e4SLinus Torvalds 6968a79be238STetsuo Handa LSM_HOOK_INIT(task_alloc, selinux_task_alloc), 6969e20b043aSCasey Schaufler LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare), 6970e20b043aSCasey Schaufler LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer), 69713ec30113SMatthew Garrett LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid), 6972e20b043aSCasey Schaufler LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as), 6973e20b043aSCasey Schaufler LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as), 6974e20b043aSCasey Schaufler LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request), 6975c77b8cdfSMimi Zohar LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data), 697661d612eaSJeff Vander Stoep LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file), 6977e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid), 6978e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid), 6979e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getsid, selinux_task_getsid), 6980e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid), 6981e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setnice, selinux_task_setnice), 6982e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio), 6983e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio), 6984791ec491SStephen Smalley LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit), 6985e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit), 6986e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler), 6987e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler), 6988e20b043aSCasey Schaufler LSM_HOOK_INIT(task_movememory, selinux_task_movememory), 6989e20b043aSCasey Schaufler LSM_HOOK_INIT(task_kill, selinux_task_kill), 6990e20b043aSCasey Schaufler LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode), 6991788e7dd4SYuichi Nakamura 6992e20b043aSCasey Schaufler LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission), 6993e20b043aSCasey Schaufler LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid), 69941da177e4SLinus Torvalds 6995e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate), 6996e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl), 6997e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd), 6998e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv), 69991da177e4SLinus Torvalds 7000e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_associate, selinux_shm_associate), 7001e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl), 7002e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat), 70031da177e4SLinus Torvalds 7004e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_associate, selinux_sem_associate), 7005e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl), 7006e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_semop, selinux_sem_semop), 70071da177e4SLinus Torvalds 7008e20b043aSCasey Schaufler LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate), 70091da177e4SLinus Torvalds 7010e20b043aSCasey Schaufler LSM_HOOK_INIT(getprocattr, selinux_getprocattr), 7011e20b043aSCasey Schaufler LSM_HOOK_INIT(setprocattr, selinux_setprocattr), 70121da177e4SLinus Torvalds 7013e20b043aSCasey Schaufler LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel), 7014e20b043aSCasey Schaufler LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid), 7015e20b043aSCasey Schaufler LSM_HOOK_INIT(release_secctx, selinux_release_secctx), 70166f3be9f5SAndreas Gruenbacher LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx), 7017e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx), 7018e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx), 70191da177e4SLinus Torvalds 7020e20b043aSCasey Schaufler LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect), 7021e20b043aSCasey Schaufler LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send), 7022dc49c1f9SCatherine Zhang 7023e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_create, selinux_socket_create), 7024e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create), 70250b811db2SDavid Herrmann LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair), 7026e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_bind, selinux_socket_bind), 7027e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_connect, selinux_socket_connect), 7028e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_listen, selinux_socket_listen), 7029e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_accept, selinux_socket_accept), 7030e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg), 7031e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg), 7032e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname), 7033e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername), 7034e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt), 7035e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt), 7036e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown), 7037e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb), 7038e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getpeersec_stream, 7039e20b043aSCasey Schaufler selinux_socket_getpeersec_stream), 7040e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram), 7041e20b043aSCasey Schaufler LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security), 7042e20b043aSCasey Schaufler LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security), 7043e20b043aSCasey Schaufler LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid), 7044e20b043aSCasey Schaufler LSM_HOOK_INIT(sock_graft, selinux_sock_graft), 7045d452930fSRichard Haines LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request), 7046d452930fSRichard Haines LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone), 7047d452930fSRichard Haines LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect), 7048e20b043aSCasey Schaufler LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request), 7049e20b043aSCasey Schaufler LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone), 7050e20b043aSCasey Schaufler LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established), 7051e20b043aSCasey Schaufler LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet), 7052e20b043aSCasey Schaufler LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc), 7053e20b043aSCasey Schaufler LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec), 7054e20b043aSCasey Schaufler LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow), 7055e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security), 7056e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create), 7057e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue), 7058e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach), 7059e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open), 70603a976fa6SDaniel Jurgens #ifdef CONFIG_SECURITY_INFINIBAND 7061cfc4d882SDaniel Jurgens LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access), 7062ab861dfcSDaniel Jurgens LSM_HOOK_INIT(ib_endport_manage_subnet, 7063ab861dfcSDaniel Jurgens selinux_ib_endport_manage_subnet), 70643a976fa6SDaniel Jurgens LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security), 70653a976fa6SDaniel Jurgens #endif 7066d28d1e08STrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM 7067e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free), 7068e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete), 7069e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free), 7070e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete), 7071e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup), 7072e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_pol_flow_match, 7073e20b043aSCasey Schaufler selinux_xfrm_state_pol_flow_match), 7074e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session), 70751da177e4SLinus Torvalds #endif 7076d720024eSMichael LeMay 7077d720024eSMichael LeMay #ifdef CONFIG_KEYS 7078e20b043aSCasey Schaufler LSM_HOOK_INIT(key_free, selinux_key_free), 7079e20b043aSCasey Schaufler LSM_HOOK_INIT(key_permission, selinux_key_permission), 7080e20b043aSCasey Schaufler LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity), 7081d720024eSMichael LeMay #endif 70829d57a7f9SAhmed S. Darwish 70839d57a7f9SAhmed S. Darwish #ifdef CONFIG_AUDIT 7084e20b043aSCasey Schaufler LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known), 7085e20b043aSCasey Schaufler LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match), 7086e20b043aSCasey Schaufler LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free), 70879d57a7f9SAhmed S. Darwish #endif 7088ec27c356SChenbo Feng 7089ec27c356SChenbo Feng #ifdef CONFIG_BPF_SYSCALL 7090ec27c356SChenbo Feng LSM_HOOK_INIT(bpf, selinux_bpf), 7091ec27c356SChenbo Feng LSM_HOOK_INIT(bpf_map, selinux_bpf_map), 7092ec27c356SChenbo Feng LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog), 7093ec27c356SChenbo Feng LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free), 7094ec27c356SChenbo Feng LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free), 7095ec27c356SChenbo Feng #endif 7096da97e184SJoel Fernandes (Google) 7097da97e184SJoel Fernandes (Google) #ifdef CONFIG_PERF_EVENTS 7098da97e184SJoel Fernandes (Google) LSM_HOOK_INIT(perf_event_open, selinux_perf_event_open), 7099da97e184SJoel Fernandes (Google) LSM_HOOK_INIT(perf_event_free, selinux_perf_event_free), 7100da97e184SJoel Fernandes (Google) LSM_HOOK_INIT(perf_event_read, selinux_perf_event_read), 7101da97e184SJoel Fernandes (Google) LSM_HOOK_INIT(perf_event_write, selinux_perf_event_write), 7102da97e184SJoel Fernandes (Google) #endif 710359438b46SStephen Smalley 710459438b46SStephen Smalley LSM_HOOK_INIT(locked_down, selinux_lockdown), 7105cfff75d8SOndrej Mosnacek 7106cfff75d8SOndrej Mosnacek /* 7107cfff75d8SOndrej Mosnacek * PUT "CLONING" (ACCESSING + ALLOCATING) HOOKS HERE 7108cfff75d8SOndrej Mosnacek */ 7109cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(fs_context_dup, selinux_fs_context_dup), 7110cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(fs_context_parse_param, selinux_fs_context_parse_param), 7111cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts), 7112cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(sb_add_mnt_opt, selinux_add_mnt_opt), 7113cfff75d8SOndrej Mosnacek #ifdef CONFIG_SECURITY_NETWORK_XFRM 7114cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone), 7115cfff75d8SOndrej Mosnacek #endif 7116cfff75d8SOndrej Mosnacek 7117cfff75d8SOndrej Mosnacek /* 7118cfff75d8SOndrej Mosnacek * PUT "ALLOCATING" HOOKS HERE 7119cfff75d8SOndrej Mosnacek */ 7120cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security), 7121cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(msg_queue_alloc_security, 7122cfff75d8SOndrej Mosnacek selinux_msg_queue_alloc_security), 7123cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security), 7124cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security), 7125cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security), 7126cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security), 7127cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx), 7128cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx), 7129cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security), 7130cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security), 7131cfff75d8SOndrej Mosnacek #ifdef CONFIG_SECURITY_INFINIBAND 7132cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security), 7133cfff75d8SOndrej Mosnacek #endif 7134cfff75d8SOndrej Mosnacek #ifdef CONFIG_SECURITY_NETWORK_XFRM 7135cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc), 7136cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc), 7137cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(xfrm_state_alloc_acquire, 7138cfff75d8SOndrej Mosnacek selinux_xfrm_state_alloc_acquire), 7139cfff75d8SOndrej Mosnacek #endif 7140cfff75d8SOndrej Mosnacek #ifdef CONFIG_KEYS 7141cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(key_alloc, selinux_key_alloc), 7142cfff75d8SOndrej Mosnacek #endif 7143cfff75d8SOndrej Mosnacek #ifdef CONFIG_AUDIT 7144cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init), 7145cfff75d8SOndrej Mosnacek #endif 7146cfff75d8SOndrej Mosnacek #ifdef CONFIG_BPF_SYSCALL 7147cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc), 7148cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc), 7149cfff75d8SOndrej Mosnacek #endif 7150cfff75d8SOndrej Mosnacek #ifdef CONFIG_PERF_EVENTS 7151cfff75d8SOndrej Mosnacek LSM_HOOK_INIT(perf_event_alloc, selinux_perf_event_alloc), 7152cfff75d8SOndrej Mosnacek #endif 71531da177e4SLinus Torvalds }; 71541da177e4SLinus Torvalds 71551da177e4SLinus Torvalds static __init int selinux_init(void) 71561da177e4SLinus Torvalds { 7157c103a91eSpeter enderborg pr_info("SELinux: Initializing.\n"); 71581da177e4SLinus Torvalds 7159aa8e712cSStephen Smalley memset(&selinux_state, 0, sizeof(selinux_state)); 7160e5a5ca96SPaul Moore enforcing_set(&selinux_state, selinux_enforcing_boot); 7161aa8e712cSStephen Smalley selinux_state.checkreqprot = selinux_checkreqprot_boot; 7162aa8e712cSStephen Smalley selinux_ss_init(&selinux_state.ss); 71636b6bc620SStephen Smalley selinux_avc_init(&selinux_state.avc); 7164*4b36cb77SOndrej Mosnacek mutex_init(&selinux_state.status_lock); 7165aa8e712cSStephen Smalley 71661da177e4SLinus Torvalds /* Set the security state for the initial task. */ 7167d84f4f99SDavid Howells cred_init_security(); 71681da177e4SLinus Torvalds 7169fcaaade1SStephen Smalley default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC); 7170fcaaade1SStephen Smalley 71711da177e4SLinus Torvalds avc_init(); 71721da177e4SLinus Torvalds 7173aa8e712cSStephen Smalley avtab_cache_init(); 7174aa8e712cSStephen Smalley 7175aa8e712cSStephen Smalley ebitmap_cache_init(); 7176aa8e712cSStephen Smalley 7177aa8e712cSStephen Smalley hashtab_cache_init(); 7178aa8e712cSStephen Smalley 7179d69dece5SCasey Schaufler security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux"); 71801da177e4SLinus Torvalds 7181615e51fdSPaul Moore if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET)) 7182615e51fdSPaul Moore panic("SELinux: Unable to register AVC netcache callback\n"); 7183615e51fdSPaul Moore 71848f408ab6SDaniel Jurgens if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET)) 71858f408ab6SDaniel Jurgens panic("SELinux: Unable to register AVC LSM notifier callback\n"); 71868f408ab6SDaniel Jurgens 7187aa8e712cSStephen Smalley if (selinux_enforcing_boot) 7188c103a91eSpeter enderborg pr_debug("SELinux: Starting in enforcing mode\n"); 7189828dfe1dSEric Paris else 7190c103a91eSpeter enderborg pr_debug("SELinux: Starting in permissive mode\n"); 7191d720024eSMichael LeMay 7192d7167b14SAl Viro fs_validate_description("selinux", selinux_fs_parameters); 7193442155c1SDavid Howells 71941da177e4SLinus Torvalds return 0; 71951da177e4SLinus Torvalds } 71961da177e4SLinus Torvalds 7197e8c26255SAl Viro static void delayed_superblock_init(struct super_block *sb, void *unused) 7198e8c26255SAl Viro { 7199204cc0ccSAl Viro selinux_set_mnt_opts(sb, NULL, 0, NULL); 7200e8c26255SAl Viro } 7201e8c26255SAl Viro 72021da177e4SLinus Torvalds void selinux_complete_init(void) 72031da177e4SLinus Torvalds { 7204c103a91eSpeter enderborg pr_debug("SELinux: Completing initialization.\n"); 72051da177e4SLinus Torvalds 72061da177e4SLinus Torvalds /* Set up any superblocks initialized prior to the policy load. */ 7207c103a91eSpeter enderborg pr_debug("SELinux: Setting up existing superblocks.\n"); 7208e8c26255SAl Viro iterate_supers(delayed_superblock_init, NULL); 72091da177e4SLinus Torvalds } 72101da177e4SLinus Torvalds 72111da177e4SLinus Torvalds /* SELinux requires early initialization in order to label 72121da177e4SLinus Torvalds all processes and objects when they are created. */ 72133d6e5f6dSKees Cook DEFINE_LSM(selinux) = { 721407aed2f2SKees Cook .name = "selinux", 721514bd99c8SKees Cook .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE, 72166c5a682eSStephen Smalley .enabled = &selinux_enabled_boot, 7217bbd3662aSCasey Schaufler .blobs = &selinux_blob_sizes, 72183d6e5f6dSKees Cook .init = selinux_init, 72193d6e5f6dSKees Cook }; 72201da177e4SLinus Torvalds 7221c2b507fdSStephen Smalley #if defined(CONFIG_NETFILTER) 72221da177e4SLinus Torvalds 7223591bb278SFlorian Westphal static const struct nf_hook_ops selinux_nf_ops[] = { 7224effad8dfSPaul Moore { 7225effad8dfSPaul Moore .hook = selinux_ipv4_postroute, 72262597a834SAlban Crequy .pf = NFPROTO_IPV4, 72276e23ae2aSPatrick McHardy .hooknum = NF_INET_POST_ROUTING, 72281da177e4SLinus Torvalds .priority = NF_IP_PRI_SELINUX_LAST, 7229effad8dfSPaul Moore }, 7230effad8dfSPaul Moore { 7231effad8dfSPaul Moore .hook = selinux_ipv4_forward, 72322597a834SAlban Crequy .pf = NFPROTO_IPV4, 7233effad8dfSPaul Moore .hooknum = NF_INET_FORWARD, 7234effad8dfSPaul Moore .priority = NF_IP_PRI_SELINUX_FIRST, 7235948bf85cSPaul Moore }, 7236948bf85cSPaul Moore { 7237948bf85cSPaul Moore .hook = selinux_ipv4_output, 72382597a834SAlban Crequy .pf = NFPROTO_IPV4, 7239948bf85cSPaul Moore .hooknum = NF_INET_LOCAL_OUT, 7240948bf85cSPaul Moore .priority = NF_IP_PRI_SELINUX_FIRST, 724125db6beaSJiri Pirko }, 72421a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 7243effad8dfSPaul Moore { 7244effad8dfSPaul Moore .hook = selinux_ipv6_postroute, 72452597a834SAlban Crequy .pf = NFPROTO_IPV6, 72466e23ae2aSPatrick McHardy .hooknum = NF_INET_POST_ROUTING, 72471da177e4SLinus Torvalds .priority = NF_IP6_PRI_SELINUX_LAST, 7248effad8dfSPaul Moore }, 7249effad8dfSPaul Moore { 7250effad8dfSPaul Moore .hook = selinux_ipv6_forward, 72512597a834SAlban Crequy .pf = NFPROTO_IPV6, 7252effad8dfSPaul Moore .hooknum = NF_INET_FORWARD, 7253effad8dfSPaul Moore .priority = NF_IP6_PRI_SELINUX_FIRST, 725425db6beaSJiri Pirko }, 72552917f57bSHuw Davies { 72562917f57bSHuw Davies .hook = selinux_ipv6_output, 72572917f57bSHuw Davies .pf = NFPROTO_IPV6, 72582917f57bSHuw Davies .hooknum = NF_INET_LOCAL_OUT, 72592917f57bSHuw Davies .priority = NF_IP6_PRI_SELINUX_FIRST, 72602917f57bSHuw Davies }, 72611da177e4SLinus Torvalds #endif /* IPV6 */ 726225db6beaSJiri Pirko }; 72631da177e4SLinus Torvalds 72648e71bf75SFlorian Westphal static int __net_init selinux_nf_register(struct net *net) 72658e71bf75SFlorian Westphal { 72668e71bf75SFlorian Westphal return nf_register_net_hooks(net, selinux_nf_ops, 72678e71bf75SFlorian Westphal ARRAY_SIZE(selinux_nf_ops)); 72688e71bf75SFlorian Westphal } 72698e71bf75SFlorian Westphal 72708e71bf75SFlorian Westphal static void __net_exit selinux_nf_unregister(struct net *net) 72718e71bf75SFlorian Westphal { 72728e71bf75SFlorian Westphal nf_unregister_net_hooks(net, selinux_nf_ops, 72738e71bf75SFlorian Westphal ARRAY_SIZE(selinux_nf_ops)); 72748e71bf75SFlorian Westphal } 72758e71bf75SFlorian Westphal 72768e71bf75SFlorian Westphal static struct pernet_operations selinux_net_ops = { 72778e71bf75SFlorian Westphal .init = selinux_nf_register, 72788e71bf75SFlorian Westphal .exit = selinux_nf_unregister, 72798e71bf75SFlorian Westphal }; 72808e71bf75SFlorian Westphal 72811da177e4SLinus Torvalds static int __init selinux_nf_ip_init(void) 72821da177e4SLinus Torvalds { 728325db6beaSJiri Pirko int err; 72841da177e4SLinus Torvalds 72856c5a682eSStephen Smalley if (!selinux_enabled_boot) 728625db6beaSJiri Pirko return 0; 72871da177e4SLinus Torvalds 7288c103a91eSpeter enderborg pr_debug("SELinux: Registering netfilter hooks\n"); 72891da177e4SLinus Torvalds 72908e71bf75SFlorian Westphal err = register_pernet_subsys(&selinux_net_ops); 72911da177e4SLinus Torvalds if (err) 72928e71bf75SFlorian Westphal panic("SELinux: register_pernet_subsys: error %d\n", err); 72931da177e4SLinus Torvalds 729425db6beaSJiri Pirko return 0; 72951da177e4SLinus Torvalds } 72961da177e4SLinus Torvalds __initcall(selinux_nf_ip_init); 72971da177e4SLinus Torvalds 72981da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 72991da177e4SLinus Torvalds static void selinux_nf_ip_exit(void) 73001da177e4SLinus Torvalds { 7301c103a91eSpeter enderborg pr_debug("SELinux: Unregistering netfilter hooks\n"); 73021da177e4SLinus Torvalds 73038e71bf75SFlorian Westphal unregister_pernet_subsys(&selinux_net_ops); 73041da177e4SLinus Torvalds } 73051da177e4SLinus Torvalds #endif 73061da177e4SLinus Torvalds 7307c2b507fdSStephen Smalley #else /* CONFIG_NETFILTER */ 73081da177e4SLinus Torvalds 73091da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 73101da177e4SLinus Torvalds #define selinux_nf_ip_exit() 73111da177e4SLinus Torvalds #endif 73121da177e4SLinus Torvalds 7313c2b507fdSStephen Smalley #endif /* CONFIG_NETFILTER */ 73141da177e4SLinus Torvalds 73151da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 7316aa8e712cSStephen Smalley int selinux_disable(struct selinux_state *state) 73171da177e4SLinus Torvalds { 731865cddd50SOndrej Mosnacek if (selinux_initialized(state)) { 73191da177e4SLinus Torvalds /* Not permitted after initial policy load. */ 73201da177e4SLinus Torvalds return -EINVAL; 73211da177e4SLinus Torvalds } 73221da177e4SLinus Torvalds 732365cddd50SOndrej Mosnacek if (selinux_disabled(state)) { 73241da177e4SLinus Torvalds /* Only do this once. */ 73251da177e4SLinus Torvalds return -EINVAL; 73261da177e4SLinus Torvalds } 73271da177e4SLinus Torvalds 732865cddd50SOndrej Mosnacek selinux_mark_disabled(state); 7329aa8e712cSStephen Smalley 7330c103a91eSpeter enderborg pr_info("SELinux: Disabled at runtime.\n"); 73311da177e4SLinus Torvalds 7332cfff75d8SOndrej Mosnacek /* 7333cfff75d8SOndrej Mosnacek * Unregister netfilter hooks. 7334cfff75d8SOndrej Mosnacek * Must be done before security_delete_hooks() to avoid breaking 7335cfff75d8SOndrej Mosnacek * runtime disable. 7336cfff75d8SOndrej Mosnacek */ 7337cfff75d8SOndrej Mosnacek selinux_nf_ip_exit(); 7338cfff75d8SOndrej Mosnacek 7339b1d9e6b0SCasey Schaufler security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks)); 73401da177e4SLinus Torvalds 7341af8ff049SEric Paris /* Try to destroy the avc node cache */ 7342af8ff049SEric Paris avc_disable(); 7343af8ff049SEric Paris 73441da177e4SLinus Torvalds /* Unregister selinuxfs. */ 73451da177e4SLinus Torvalds exit_sel_fs(); 73461da177e4SLinus Torvalds 73471da177e4SLinus Torvalds return 0; 73481da177e4SLinus Torvalds } 73491da177e4SLinus Torvalds #endif 7350