11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * NSA Security-Enhanced Linux (SELinux) security module 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * This file contains the SELinux hook function implementations. 51da177e4SLinus Torvalds * 67efbb60bSStephen Smalley * Authors: Stephen Smalley, <sds@tycho.nsa.gov> 71da177e4SLinus Torvalds * Chris Vance, <cvance@nai.com> 81da177e4SLinus Torvalds * Wayne Salamon, <wsalamon@nai.com> 91da177e4SLinus Torvalds * James Morris <jmorris@redhat.com> 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * Copyright (C) 2001,2002 Networks Associates Technology, Inc. 122069f457SEric Paris * Copyright (C) 2003-2008 Red Hat, Inc., James Morris <jmorris@redhat.com> 132069f457SEric Paris * Eric Paris <eparis@redhat.com> 141da177e4SLinus Torvalds * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc. 151da177e4SLinus Torvalds * <dgoeddel@trustedcs.com> 16ed6d76e4SPaul Moore * Copyright (C) 2006, 2007, 2009 Hewlett-Packard Development Company, L.P. 1782c21bfaSPaul Moore * Paul Moore <paul@paul-moore.com> 18788e7dd4SYuichi Nakamura * Copyright (C) 2007 Hitachi Software Engineering Co., Ltd. 19788e7dd4SYuichi Nakamura * Yuichi Nakamura <ynakam@hitachisoft.jp> 203a976fa6SDaniel Jurgens * Copyright (C) 2016 Mellanox Technologies 211da177e4SLinus Torvalds * 221da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 231da177e4SLinus Torvalds * it under the terms of the GNU General Public License version 2, 241da177e4SLinus Torvalds * as published by the Free Software Foundation. 251da177e4SLinus Torvalds */ 261da177e4SLinus Torvalds 271da177e4SLinus Torvalds #include <linux/init.h> 280b24dcb7SEric Paris #include <linux/kd.h> 291da177e4SLinus Torvalds #include <linux/kernel.h> 300d094efeSRoland McGrath #include <linux/tracehook.h> 311da177e4SLinus Torvalds #include <linux/errno.h> 323f07c014SIngo Molnar #include <linux/sched/signal.h> 3329930025SIngo Molnar #include <linux/sched/task.h> 343c4ed7bdSCasey Schaufler #include <linux/lsm_hooks.h> 351da177e4SLinus Torvalds #include <linux/xattr.h> 361da177e4SLinus Torvalds #include <linux/capability.h> 371da177e4SLinus Torvalds #include <linux/unistd.h> 381da177e4SLinus Torvalds #include <linux/mm.h> 391da177e4SLinus Torvalds #include <linux/mman.h> 401da177e4SLinus Torvalds #include <linux/slab.h> 411da177e4SLinus Torvalds #include <linux/pagemap.h> 420b24dcb7SEric Paris #include <linux/proc_fs.h> 431da177e4SLinus Torvalds #include <linux/swap.h> 441da177e4SLinus Torvalds #include <linux/spinlock.h> 451da177e4SLinus Torvalds #include <linux/syscalls.h> 462a7dba39SEric Paris #include <linux/dcache.h> 471da177e4SLinus Torvalds #include <linux/file.h> 489f3acc31SAl Viro #include <linux/fdtable.h> 491da177e4SLinus Torvalds #include <linux/namei.h> 501da177e4SLinus Torvalds #include <linux/mount.h> 511da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h> 521da177e4SLinus Torvalds #include <linux/netfilter_ipv6.h> 531da177e4SLinus Torvalds #include <linux/tty.h> 541da177e4SLinus Torvalds #include <net/icmp.h> 55227b60f5SStephen Hemminger #include <net/ip.h> /* for local_port_range[] */ 561da177e4SLinus Torvalds #include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */ 5747180068SPaul Moore #include <net/inet_connection_sock.h> 58220deb96SPaul Moore #include <net/net_namespace.h> 59d621d35eSPaul Moore #include <net/netlabel.h> 60f5269710SEric Paris #include <linux/uaccess.h> 611da177e4SLinus Torvalds #include <asm/ioctls.h> 6260063497SArun Sharma #include <linux/atomic.h> 631da177e4SLinus Torvalds #include <linux/bitops.h> 641da177e4SLinus Torvalds #include <linux/interrupt.h> 651da177e4SLinus Torvalds #include <linux/netdevice.h> /* for network interface checks */ 6677954983SHong zhi guo #include <net/netlink.h> 671da177e4SLinus Torvalds #include <linux/tcp.h> 681da177e4SLinus Torvalds #include <linux/udp.h> 692ee92d46SJames Morris #include <linux/dccp.h> 70d452930fSRichard Haines #include <linux/sctp.h> 71d452930fSRichard Haines #include <net/sctp/structs.h> 721da177e4SLinus Torvalds #include <linux/quota.h> 731da177e4SLinus Torvalds #include <linux/un.h> /* for Unix socket types */ 741da177e4SLinus Torvalds #include <net/af_unix.h> /* for Unix socket types */ 751da177e4SLinus Torvalds #include <linux/parser.h> 761da177e4SLinus Torvalds #include <linux/nfs_mount.h> 771da177e4SLinus Torvalds #include <net/ipv6.h> 781da177e4SLinus Torvalds #include <linux/hugetlb.h> 791da177e4SLinus Torvalds #include <linux/personality.h> 801da177e4SLinus Torvalds #include <linux/audit.h> 816931dfc9SEric Paris #include <linux/string.h> 8223970741SEric Paris #include <linux/mutex.h> 83f06febc9SFrank Mayhar #include <linux/posix-timers.h> 8400234592SKees Cook #include <linux/syslog.h> 853486740aSSerge E. Hallyn #include <linux/user_namespace.h> 8644fc7ea0SPaul Gortmaker #include <linux/export.h> 8740401530SAl Viro #include <linux/msg.h> 8840401530SAl Viro #include <linux/shm.h> 89ec27c356SChenbo Feng #include <linux/bpf.h> 90e262e32dSDavid Howells #include <uapi/linux/mount.h> 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds #include "avc.h" 931da177e4SLinus Torvalds #include "objsec.h" 941da177e4SLinus Torvalds #include "netif.h" 95224dfbd8SPaul Moore #include "netnode.h" 963e112172SPaul Moore #include "netport.h" 97409dcf31SDaniel Jurgens #include "ibpkey.h" 98d28d1e08STrent Jaeger #include "xfrm.h" 99c60475bfSPaul Moore #include "netlabel.h" 1009d57a7f9SAhmed S. Darwish #include "audit.h" 1017b98a585SJames Morris #include "avc_ss.h" 1021da177e4SLinus Torvalds 103aa8e712cSStephen Smalley struct selinux_state selinux_state; 104aa8e712cSStephen Smalley 105d621d35eSPaul Moore /* SECMARK reference count */ 10656a4ca99SJames Morris static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0); 107d621d35eSPaul Moore 1081da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP 109aa8e712cSStephen Smalley static int selinux_enforcing_boot; 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds static int __init enforcing_setup(char *str) 1121da177e4SLinus Torvalds { 113f5269710SEric Paris unsigned long enforcing; 11429707b20SJingoo Han if (!kstrtoul(str, 0, &enforcing)) 115aa8e712cSStephen Smalley selinux_enforcing_boot = enforcing ? 1 : 0; 1161da177e4SLinus Torvalds return 1; 1171da177e4SLinus Torvalds } 1181da177e4SLinus Torvalds __setup("enforcing=", enforcing_setup); 119aa8e712cSStephen Smalley #else 120aa8e712cSStephen Smalley #define selinux_enforcing_boot 1 1211da177e4SLinus Torvalds #endif 1221da177e4SLinus Torvalds 123be6ec88fSKees Cook int selinux_enabled __lsm_ro_after_init = 1; 1241da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM 1251da177e4SLinus Torvalds static int __init selinux_enabled_setup(char *str) 1261da177e4SLinus Torvalds { 127f5269710SEric Paris unsigned long enabled; 12829707b20SJingoo Han if (!kstrtoul(str, 0, &enabled)) 129f5269710SEric Paris selinux_enabled = enabled ? 1 : 0; 1301da177e4SLinus Torvalds return 1; 1311da177e4SLinus Torvalds } 1321da177e4SLinus Torvalds __setup("selinux=", selinux_enabled_setup); 1331da177e4SLinus Torvalds #endif 1341da177e4SLinus Torvalds 135aa8e712cSStephen Smalley static unsigned int selinux_checkreqprot_boot = 136aa8e712cSStephen Smalley CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE; 137aa8e712cSStephen Smalley 138aa8e712cSStephen Smalley static int __init checkreqprot_setup(char *str) 139aa8e712cSStephen Smalley { 140aa8e712cSStephen Smalley unsigned long checkreqprot; 141aa8e712cSStephen Smalley 142aa8e712cSStephen Smalley if (!kstrtoul(str, 0, &checkreqprot)) 143aa8e712cSStephen Smalley selinux_checkreqprot_boot = checkreqprot ? 1 : 0; 144aa8e712cSStephen Smalley return 1; 145aa8e712cSStephen Smalley } 146aa8e712cSStephen Smalley __setup("checkreqprot=", checkreqprot_setup); 147aa8e712cSStephen Smalley 148d621d35eSPaul Moore /** 149d621d35eSPaul Moore * selinux_secmark_enabled - Check to see if SECMARK is currently enabled 150d621d35eSPaul Moore * 151d621d35eSPaul Moore * Description: 152d621d35eSPaul Moore * This function checks the SECMARK reference counter to see if any SECMARK 153d621d35eSPaul Moore * targets are currently configured, if the reference counter is greater than 154d621d35eSPaul Moore * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is 1552be4d74fSChris PeBenito * enabled, false (0) if SECMARK is disabled. If the always_check_network 1562be4d74fSChris PeBenito * policy capability is enabled, SECMARK is always considered enabled. 157d621d35eSPaul Moore * 158d621d35eSPaul Moore */ 159d621d35eSPaul Moore static int selinux_secmark_enabled(void) 160d621d35eSPaul Moore { 161aa8e712cSStephen Smalley return (selinux_policycap_alwaysnetwork() || 162aa8e712cSStephen Smalley atomic_read(&selinux_secmark_refcount)); 1632be4d74fSChris PeBenito } 1642be4d74fSChris PeBenito 1652be4d74fSChris PeBenito /** 1662be4d74fSChris PeBenito * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled 1672be4d74fSChris PeBenito * 1682be4d74fSChris PeBenito * Description: 1692be4d74fSChris PeBenito * This function checks if NetLabel or labeled IPSEC is enabled. Returns true 1702be4d74fSChris PeBenito * (1) if any are enabled or false (0) if neither are enabled. If the 1712be4d74fSChris PeBenito * always_check_network policy capability is enabled, peer labeling 1722be4d74fSChris PeBenito * is always considered enabled. 1732be4d74fSChris PeBenito * 1742be4d74fSChris PeBenito */ 1752be4d74fSChris PeBenito static int selinux_peerlbl_enabled(void) 1762be4d74fSChris PeBenito { 177aa8e712cSStephen Smalley return (selinux_policycap_alwaysnetwork() || 178aa8e712cSStephen Smalley netlbl_enabled() || selinux_xfrm_enabled()); 179d621d35eSPaul Moore } 180d621d35eSPaul Moore 181615e51fdSPaul Moore static int selinux_netcache_avc_callback(u32 event) 182615e51fdSPaul Moore { 183615e51fdSPaul Moore if (event == AVC_CALLBACK_RESET) { 184615e51fdSPaul Moore sel_netif_flush(); 185615e51fdSPaul Moore sel_netnode_flush(); 186615e51fdSPaul Moore sel_netport_flush(); 187615e51fdSPaul Moore synchronize_net(); 188615e51fdSPaul Moore } 189615e51fdSPaul Moore return 0; 190615e51fdSPaul Moore } 191615e51fdSPaul Moore 1928f408ab6SDaniel Jurgens static int selinux_lsm_notifier_avc_callback(u32 event) 1938f408ab6SDaniel Jurgens { 194409dcf31SDaniel Jurgens if (event == AVC_CALLBACK_RESET) { 195409dcf31SDaniel Jurgens sel_ib_pkey_flush(); 1968f408ab6SDaniel Jurgens call_lsm_notifier(LSM_POLICY_CHANGE, NULL); 197409dcf31SDaniel Jurgens } 1988f408ab6SDaniel Jurgens 1998f408ab6SDaniel Jurgens return 0; 2008f408ab6SDaniel Jurgens } 2018f408ab6SDaniel Jurgens 202d84f4f99SDavid Howells /* 203d84f4f99SDavid Howells * initialise the security for the init task 204d84f4f99SDavid Howells */ 205d84f4f99SDavid Howells static void cred_init_security(void) 2061da177e4SLinus Torvalds { 2073b11a1deSDavid Howells struct cred *cred = (struct cred *) current->real_cred; 2081da177e4SLinus Torvalds struct task_security_struct *tsec; 2091da177e4SLinus Torvalds 210bbd3662aSCasey Schaufler lsm_early_cred(cred); 211bbd3662aSCasey Schaufler tsec = selinux_cred(cred); 212d84f4f99SDavid Howells tsec->osid = tsec->sid = SECINITSID_KERNEL; 2131da177e4SLinus Torvalds } 2141da177e4SLinus Torvalds 215275bb41eSDavid Howells /* 21688e67f3bSDavid Howells * get the security ID of a set of credentials 21788e67f3bSDavid Howells */ 21888e67f3bSDavid Howells static inline u32 cred_sid(const struct cred *cred) 21988e67f3bSDavid Howells { 22088e67f3bSDavid Howells const struct task_security_struct *tsec; 22188e67f3bSDavid Howells 2220c6cfa62SCasey Schaufler tsec = selinux_cred(cred); 22388e67f3bSDavid Howells return tsec->sid; 22488e67f3bSDavid Howells } 22588e67f3bSDavid Howells 22688e67f3bSDavid Howells /* 2273b11a1deSDavid Howells * get the objective security ID of a task 228275bb41eSDavid Howells */ 229275bb41eSDavid Howells static inline u32 task_sid(const struct task_struct *task) 230275bb41eSDavid Howells { 231275bb41eSDavid Howells u32 sid; 232275bb41eSDavid Howells 233275bb41eSDavid Howells rcu_read_lock(); 23488e67f3bSDavid Howells sid = cred_sid(__task_cred(task)); 235275bb41eSDavid Howells rcu_read_unlock(); 236275bb41eSDavid Howells return sid; 237275bb41eSDavid Howells } 238275bb41eSDavid Howells 23988e67f3bSDavid Howells /* Allocate and free functions for each kind of security blob. */ 24088e67f3bSDavid Howells 2411da177e4SLinus Torvalds static int inode_alloc_security(struct inode *inode) 2421da177e4SLinus Torvalds { 243afb1cbe3SCasey Schaufler struct inode_security_struct *isec = selinux_inode(inode); 244275bb41eSDavid Howells u32 sid = current_sid(); 2451da177e4SLinus Torvalds 2469287aed2SAndreas Gruenbacher spin_lock_init(&isec->lock); 2471da177e4SLinus Torvalds INIT_LIST_HEAD(&isec->list); 2481da177e4SLinus Torvalds isec->inode = inode; 2491da177e4SLinus Torvalds isec->sid = SECINITSID_UNLABELED; 2501da177e4SLinus Torvalds isec->sclass = SECCLASS_FILE; 251275bb41eSDavid Howells isec->task_sid = sid; 25242059112SAndreas Gruenbacher isec->initialized = LABEL_INVALID; 2531da177e4SLinus Torvalds 2541da177e4SLinus Torvalds return 0; 2551da177e4SLinus Torvalds } 2561da177e4SLinus Torvalds 2575d226df4SAndreas Gruenbacher static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry); 2585d226df4SAndreas Gruenbacher 2595d226df4SAndreas Gruenbacher /* 2605d226df4SAndreas Gruenbacher * Try reloading inode security labels that have been marked as invalid. The 2615d226df4SAndreas Gruenbacher * @may_sleep parameter indicates when sleeping and thus reloading labels is 26242059112SAndreas Gruenbacher * allowed; when set to false, returns -ECHILD when the label is 263e9193288SAl Viro * invalid. The @dentry parameter should be set to a dentry of the inode. 2645d226df4SAndreas Gruenbacher */ 2655d226df4SAndreas Gruenbacher static int __inode_security_revalidate(struct inode *inode, 266e9193288SAl Viro struct dentry *dentry, 2675d226df4SAndreas Gruenbacher bool may_sleep) 2685d226df4SAndreas Gruenbacher { 26980788c22SCasey Schaufler struct inode_security_struct *isec = selinux_inode(inode); 2705d226df4SAndreas Gruenbacher 2715d226df4SAndreas Gruenbacher might_sleep_if(may_sleep); 2725d226df4SAndreas Gruenbacher 273aa8e712cSStephen Smalley if (selinux_state.initialized && 274aa8e712cSStephen Smalley isec->initialized != LABEL_INITIALIZED) { 2755d226df4SAndreas Gruenbacher if (!may_sleep) 2765d226df4SAndreas Gruenbacher return -ECHILD; 2775d226df4SAndreas Gruenbacher 2785d226df4SAndreas Gruenbacher /* 2795d226df4SAndreas Gruenbacher * Try reloading the inode security label. This will fail if 2805d226df4SAndreas Gruenbacher * @opt_dentry is NULL and no dentry for this inode can be 2815d226df4SAndreas Gruenbacher * found; in that case, continue using the old label. 2825d226df4SAndreas Gruenbacher */ 283e9193288SAl Viro inode_doinit_with_dentry(inode, dentry); 2845d226df4SAndreas Gruenbacher } 2855d226df4SAndreas Gruenbacher return 0; 2865d226df4SAndreas Gruenbacher } 2875d226df4SAndreas Gruenbacher 2885d226df4SAndreas Gruenbacher static struct inode_security_struct *inode_security_novalidate(struct inode *inode) 2895d226df4SAndreas Gruenbacher { 29080788c22SCasey Schaufler return selinux_inode(inode); 2915d226df4SAndreas Gruenbacher } 2925d226df4SAndreas Gruenbacher 2935d226df4SAndreas Gruenbacher static struct inode_security_struct *inode_security_rcu(struct inode *inode, bool rcu) 2945d226df4SAndreas Gruenbacher { 2955d226df4SAndreas Gruenbacher int error; 2965d226df4SAndreas Gruenbacher 2975d226df4SAndreas Gruenbacher error = __inode_security_revalidate(inode, NULL, !rcu); 2985d226df4SAndreas Gruenbacher if (error) 2995d226df4SAndreas Gruenbacher return ERR_PTR(error); 30080788c22SCasey Schaufler return selinux_inode(inode); 3015d226df4SAndreas Gruenbacher } 3025d226df4SAndreas Gruenbacher 30383da53c5SAndreas Gruenbacher /* 30483da53c5SAndreas Gruenbacher * Get the security label of an inode. 30583da53c5SAndreas Gruenbacher */ 30683da53c5SAndreas Gruenbacher static struct inode_security_struct *inode_security(struct inode *inode) 30783da53c5SAndreas Gruenbacher { 3085d226df4SAndreas Gruenbacher __inode_security_revalidate(inode, NULL, true); 30980788c22SCasey Schaufler return selinux_inode(inode); 31083da53c5SAndreas Gruenbacher } 31183da53c5SAndreas Gruenbacher 3122c97165bSPaul Moore static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry) 3132c97165bSPaul Moore { 3142c97165bSPaul Moore struct inode *inode = d_backing_inode(dentry); 3152c97165bSPaul Moore 31680788c22SCasey Schaufler return selinux_inode(inode); 3172c97165bSPaul Moore } 3182c97165bSPaul Moore 31983da53c5SAndreas Gruenbacher /* 32083da53c5SAndreas Gruenbacher * Get the security label of a dentry's backing inode. 32183da53c5SAndreas Gruenbacher */ 32283da53c5SAndreas Gruenbacher static struct inode_security_struct *backing_inode_security(struct dentry *dentry) 32383da53c5SAndreas Gruenbacher { 32483da53c5SAndreas Gruenbacher struct inode *inode = d_backing_inode(dentry); 32583da53c5SAndreas Gruenbacher 3265d226df4SAndreas Gruenbacher __inode_security_revalidate(inode, dentry, true); 32780788c22SCasey Schaufler return selinux_inode(inode); 32883da53c5SAndreas Gruenbacher } 32983da53c5SAndreas Gruenbacher 3301da177e4SLinus Torvalds static void inode_free_security(struct inode *inode) 3311da177e4SLinus Torvalds { 33280788c22SCasey Schaufler struct inode_security_struct *isec = selinux_inode(inode); 333afb1cbe3SCasey Schaufler struct superblock_security_struct *sbsec; 3341da177e4SLinus Torvalds 335afb1cbe3SCasey Schaufler if (!isec) 336afb1cbe3SCasey Schaufler return; 337afb1cbe3SCasey Schaufler sbsec = inode->i_sb->s_security; 3389629d04aSWaiman Long /* 3399629d04aSWaiman Long * As not all inode security structures are in a list, we check for 3409629d04aSWaiman Long * empty list outside of the lock to make sure that we won't waste 3419629d04aSWaiman Long * time taking a lock doing nothing. 3429629d04aSWaiman Long * 3439629d04aSWaiman Long * The list_del_init() function can be safely called more than once. 3449629d04aSWaiman Long * It should not be possible for this function to be called with 3459629d04aSWaiman Long * concurrent list_add(), but for better safety against future changes 3469629d04aSWaiman Long * in the code, we use list_empty_careful() here. 3479629d04aSWaiman Long */ 3489629d04aSWaiman Long if (!list_empty_careful(&isec->list)) { 3491da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 3501da177e4SLinus Torvalds list_del_init(&isec->list); 3511da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 3529629d04aSWaiman Long } 3531da177e4SLinus Torvalds } 3541da177e4SLinus Torvalds 3551da177e4SLinus Torvalds static int file_alloc_security(struct file *file) 3561da177e4SLinus Torvalds { 35733bf60caSCasey Schaufler struct file_security_struct *fsec = selinux_file(file); 358275bb41eSDavid Howells u32 sid = current_sid(); 3591da177e4SLinus Torvalds 360275bb41eSDavid Howells fsec->sid = sid; 361275bb41eSDavid Howells fsec->fown_sid = sid; 3621da177e4SLinus Torvalds 3631da177e4SLinus Torvalds return 0; 3641da177e4SLinus Torvalds } 3651da177e4SLinus Torvalds 3661da177e4SLinus Torvalds static int superblock_alloc_security(struct super_block *sb) 3671da177e4SLinus Torvalds { 3681da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 3691da177e4SLinus Torvalds 37089d155efSJames Morris sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL); 3711da177e4SLinus Torvalds if (!sbsec) 3721da177e4SLinus Torvalds return -ENOMEM; 3731da177e4SLinus Torvalds 374bc7e982bSEric Paris mutex_init(&sbsec->lock); 3751da177e4SLinus Torvalds INIT_LIST_HEAD(&sbsec->isec_head); 3761da177e4SLinus Torvalds spin_lock_init(&sbsec->isec_lock); 3771da177e4SLinus Torvalds sbsec->sb = sb; 3781da177e4SLinus Torvalds sbsec->sid = SECINITSID_UNLABELED; 3791da177e4SLinus Torvalds sbsec->def_sid = SECINITSID_FILE; 380c312feb2SEric Paris sbsec->mntpoint_sid = SECINITSID_UNLABELED; 3811da177e4SLinus Torvalds sb->s_security = sbsec; 3821da177e4SLinus Torvalds 3831da177e4SLinus Torvalds return 0; 3841da177e4SLinus Torvalds } 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds static void superblock_free_security(struct super_block *sb) 3871da177e4SLinus Torvalds { 3881da177e4SLinus Torvalds struct superblock_security_struct *sbsec = sb->s_security; 3891da177e4SLinus Torvalds sb->s_security = NULL; 3901da177e4SLinus Torvalds kfree(sbsec); 3911da177e4SLinus Torvalds } 3921da177e4SLinus Torvalds 393bd323655SAl Viro struct selinux_mnt_opts { 394bd323655SAl Viro const char *fscontext, *context, *rootcontext, *defcontext; 395bd323655SAl Viro }; 396bd323655SAl Viro 397204cc0ccSAl Viro static void selinux_free_mnt_opts(void *mnt_opts) 398204cc0ccSAl Viro { 399bd323655SAl Viro struct selinux_mnt_opts *opts = mnt_opts; 400bd323655SAl Viro kfree(opts->fscontext); 401bd323655SAl Viro kfree(opts->context); 402bd323655SAl Viro kfree(opts->rootcontext); 403bd323655SAl Viro kfree(opts->defcontext); 404204cc0ccSAl Viro kfree(opts); 405204cc0ccSAl Viro } 406204cc0ccSAl Viro 4071da177e4SLinus Torvalds static inline int inode_doinit(struct inode *inode) 4081da177e4SLinus Torvalds { 4091da177e4SLinus Torvalds return inode_doinit_with_dentry(inode, NULL); 4101da177e4SLinus Torvalds } 4111da177e4SLinus Torvalds 4121da177e4SLinus Torvalds enum { 41331e87930SEric Paris Opt_error = -1, 4141da177e4SLinus Torvalds Opt_context = 1, 4151da177e4SLinus Torvalds Opt_fscontext = 2, 416c9180a57SEric Paris Opt_defcontext = 3, 417c9180a57SEric Paris Opt_rootcontext = 4, 418da3d76abSAl Viro Opt_seclabel = 5, 4191da177e4SLinus Torvalds }; 4201da177e4SLinus Torvalds 421da3d76abSAl Viro #define A(s, has_arg) {#s, sizeof(#s) - 1, Opt_##s, has_arg} 422169d68efSAl Viro static struct { 423169d68efSAl Viro const char *name; 424169d68efSAl Viro int len; 425169d68efSAl Viro int opt; 426169d68efSAl Viro bool has_arg; 427169d68efSAl Viro } tokens[] = { 428da3d76abSAl Viro A(context, true), 429da3d76abSAl Viro A(fscontext, true), 430da3d76abSAl Viro A(defcontext, true), 431da3d76abSAl Viro A(rootcontext, true), 432da3d76abSAl Viro A(seclabel, false), 4331da177e4SLinus Torvalds }; 434169d68efSAl Viro #undef A 435169d68efSAl Viro 436169d68efSAl Viro static int match_opt_prefix(char *s, int l, char **arg) 437169d68efSAl Viro { 438169d68efSAl Viro int i; 439169d68efSAl Viro 440169d68efSAl Viro for (i = 0; i < ARRAY_SIZE(tokens); i++) { 441169d68efSAl Viro size_t len = tokens[i].len; 442169d68efSAl Viro if (len > l || memcmp(s, tokens[i].name, len)) 443169d68efSAl Viro continue; 444169d68efSAl Viro if (tokens[i].has_arg) { 445169d68efSAl Viro if (len == l || s[len] != '=') 446169d68efSAl Viro continue; 447169d68efSAl Viro *arg = s + len + 1; 448169d68efSAl Viro } else if (len != l) 449169d68efSAl Viro continue; 450169d68efSAl Viro return tokens[i].opt; 451169d68efSAl Viro } 452169d68efSAl Viro return Opt_error; 453169d68efSAl Viro } 4541da177e4SLinus Torvalds 4551da177e4SLinus Torvalds #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n" 4561da177e4SLinus Torvalds 457c312feb2SEric Paris static int may_context_mount_sb_relabel(u32 sid, 458c312feb2SEric Paris struct superblock_security_struct *sbsec, 459275bb41eSDavid Howells const struct cred *cred) 460c312feb2SEric Paris { 4610c6cfa62SCasey Schaufler const struct task_security_struct *tsec = selinux_cred(cred); 462c312feb2SEric Paris int rc; 463c312feb2SEric Paris 4646b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 4656b6bc620SStephen Smalley tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 466c312feb2SEric Paris FILESYSTEM__RELABELFROM, NULL); 467c312feb2SEric Paris if (rc) 468c312feb2SEric Paris return rc; 469c312feb2SEric Paris 4706b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 4716b6bc620SStephen Smalley tsec->sid, sid, SECCLASS_FILESYSTEM, 472c312feb2SEric Paris FILESYSTEM__RELABELTO, NULL); 473c312feb2SEric Paris return rc; 474c312feb2SEric Paris } 475c312feb2SEric Paris 4760808925eSEric Paris static int may_context_mount_inode_relabel(u32 sid, 4770808925eSEric Paris struct superblock_security_struct *sbsec, 478275bb41eSDavid Howells const struct cred *cred) 4790808925eSEric Paris { 4800c6cfa62SCasey Schaufler const struct task_security_struct *tsec = selinux_cred(cred); 4810808925eSEric Paris int rc; 4826b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 4836b6bc620SStephen Smalley tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 4840808925eSEric Paris FILESYSTEM__RELABELFROM, NULL); 4850808925eSEric Paris if (rc) 4860808925eSEric Paris return rc; 4870808925eSEric Paris 4886b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 4896b6bc620SStephen Smalley sid, sbsec->sid, SECCLASS_FILESYSTEM, 4900808925eSEric Paris FILESYSTEM__ASSOCIATE, NULL); 4910808925eSEric Paris return rc; 4920808925eSEric Paris } 4930808925eSEric Paris 494b43e725dSEric Paris static int selinux_is_sblabel_mnt(struct super_block *sb) 495b43e725dSEric Paris { 496b43e725dSEric Paris struct superblock_security_struct *sbsec = sb->s_security; 497b43e725dSEric Paris 498d5f3a5f6SMark Salyzyn return sbsec->behavior == SECURITY_FS_USE_XATTR || 499b43e725dSEric Paris sbsec->behavior == SECURITY_FS_USE_TRANS || 500d5f3a5f6SMark Salyzyn sbsec->behavior == SECURITY_FS_USE_TASK || 5019fc2b4b4SJ. Bruce Fields sbsec->behavior == SECURITY_FS_USE_NATIVE || 502d5f3a5f6SMark Salyzyn /* Special handling. Genfs but also in-core setxattr handler */ 503d5f3a5f6SMark Salyzyn !strcmp(sb->s_type->name, "sysfs") || 504d5f3a5f6SMark Salyzyn !strcmp(sb->s_type->name, "pstore") || 505d5f3a5f6SMark Salyzyn !strcmp(sb->s_type->name, "debugfs") || 506a2c7c6fbSYongqin Liu !strcmp(sb->s_type->name, "tracefs") || 5072651225bSStephen Smalley !strcmp(sb->s_type->name, "rootfs") || 508aa8e712cSStephen Smalley (selinux_policycap_cgroupseclabel() && 5092651225bSStephen Smalley (!strcmp(sb->s_type->name, "cgroup") || 5102651225bSStephen Smalley !strcmp(sb->s_type->name, "cgroup2"))); 511b43e725dSEric Paris } 512b43e725dSEric Paris 513c9180a57SEric Paris static int sb_finish_set_opts(struct super_block *sb) 5141da177e4SLinus Torvalds { 5151da177e4SLinus Torvalds struct superblock_security_struct *sbsec = sb->s_security; 5161da177e4SLinus Torvalds struct dentry *root = sb->s_root; 517c6f493d6SDavid Howells struct inode *root_inode = d_backing_inode(root); 5181da177e4SLinus Torvalds int rc = 0; 5191da177e4SLinus Torvalds 5201da177e4SLinus Torvalds if (sbsec->behavior == SECURITY_FS_USE_XATTR) { 5211da177e4SLinus Torvalds /* Make sure that the xattr handler exists and that no 5221da177e4SLinus Torvalds error other than -ENODATA is returned by getxattr on 5231da177e4SLinus Torvalds the root directory. -ENODATA is ok, as this may be 5241da177e4SLinus Torvalds the first boot of the SELinux kernel before we have 5251da177e4SLinus Torvalds assigned xattr values to the filesystem. */ 5265d6c3191SAndreas Gruenbacher if (!(root_inode->i_opflags & IOP_XATTR)) { 527c103a91eSpeter enderborg pr_warn("SELinux: (dev %s, type %s) has no " 52829b1deb2SLinus Torvalds "xattr support\n", sb->s_id, sb->s_type->name); 5291da177e4SLinus Torvalds rc = -EOPNOTSUPP; 5301da177e4SLinus Torvalds goto out; 5311da177e4SLinus Torvalds } 5325d6c3191SAndreas Gruenbacher 5335d6c3191SAndreas Gruenbacher rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0); 5341da177e4SLinus Torvalds if (rc < 0 && rc != -ENODATA) { 5351da177e4SLinus Torvalds if (rc == -EOPNOTSUPP) 536c103a91eSpeter enderborg pr_warn("SELinux: (dev %s, type " 53729b1deb2SLinus Torvalds "%s) has no security xattr handler\n", 53829b1deb2SLinus Torvalds sb->s_id, sb->s_type->name); 5391da177e4SLinus Torvalds else 540c103a91eSpeter enderborg pr_warn("SELinux: (dev %s, type " 54129b1deb2SLinus Torvalds "%s) getxattr errno %d\n", sb->s_id, 54229b1deb2SLinus Torvalds sb->s_type->name, -rc); 5431da177e4SLinus Torvalds goto out; 5441da177e4SLinus Torvalds } 5451da177e4SLinus Torvalds } 5461da177e4SLinus Torvalds 547eadcabc6SEric Paris sbsec->flags |= SE_SBINITIALIZED; 5480b4d3452SScott Mayhew 5490b4d3452SScott Mayhew /* 5500b4d3452SScott Mayhew * Explicitly set or clear SBLABEL_MNT. It's not sufficient to simply 5510b4d3452SScott Mayhew * leave the flag untouched because sb_clone_mnt_opts might be handing 5520b4d3452SScott Mayhew * us a superblock that needs the flag to be cleared. 5530b4d3452SScott Mayhew */ 554b43e725dSEric Paris if (selinux_is_sblabel_mnt(sb)) 55512f348b9SEric Paris sbsec->flags |= SBLABEL_MNT; 5560b4d3452SScott Mayhew else 5570b4d3452SScott Mayhew sbsec->flags &= ~SBLABEL_MNT; 558ddd29ec6SDavid P. Quigley 5591da177e4SLinus Torvalds /* Initialize the root inode. */ 560c9180a57SEric Paris rc = inode_doinit_with_dentry(root_inode, root); 5611da177e4SLinus Torvalds 5621da177e4SLinus Torvalds /* Initialize any other inodes associated with the superblock, e.g. 5631da177e4SLinus Torvalds inodes created prior to initial policy load or inodes created 5641da177e4SLinus Torvalds during get_sb by a pseudo filesystem that directly 5651da177e4SLinus Torvalds populates itself. */ 5661da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 5678d64124aSAl Viro while (!list_empty(&sbsec->isec_head)) { 5681da177e4SLinus Torvalds struct inode_security_struct *isec = 5698d64124aSAl Viro list_first_entry(&sbsec->isec_head, 5701da177e4SLinus Torvalds struct inode_security_struct, list); 5711da177e4SLinus Torvalds struct inode *inode = isec->inode; 572923190d3SStephen Smalley list_del_init(&isec->list); 5731da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 5741da177e4SLinus Torvalds inode = igrab(inode); 5751da177e4SLinus Torvalds if (inode) { 5761da177e4SLinus Torvalds if (!IS_PRIVATE(inode)) 5771da177e4SLinus Torvalds inode_doinit(inode); 5781da177e4SLinus Torvalds iput(inode); 5791da177e4SLinus Torvalds } 5801da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 5811da177e4SLinus Torvalds } 5821da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 5831da177e4SLinus Torvalds out: 584c9180a57SEric Paris return rc; 585c9180a57SEric Paris } 586c9180a57SEric Paris 587c9180a57SEric Paris static int bad_option(struct superblock_security_struct *sbsec, char flag, 588c9180a57SEric Paris u32 old_sid, u32 new_sid) 589c9180a57SEric Paris { 5900d90a7ecSDavid P. Quigley char mnt_flags = sbsec->flags & SE_MNTMASK; 5910d90a7ecSDavid P. Quigley 592c9180a57SEric Paris /* check if the old mount command had the same options */ 5930d90a7ecSDavid P. Quigley if (sbsec->flags & SE_SBINITIALIZED) 594c9180a57SEric Paris if (!(sbsec->flags & flag) || 595c9180a57SEric Paris (old_sid != new_sid)) 596c9180a57SEric Paris return 1; 597c9180a57SEric Paris 598c9180a57SEric Paris /* check if we were passed the same options twice, 599c9180a57SEric Paris * aka someone passed context=a,context=b 600c9180a57SEric Paris */ 6010d90a7ecSDavid P. Quigley if (!(sbsec->flags & SE_SBINITIALIZED)) 6020d90a7ecSDavid P. Quigley if (mnt_flags & flag) 603c9180a57SEric Paris return 1; 604c9180a57SEric Paris return 0; 605c9180a57SEric Paris } 606e0007529SEric Paris 607bd323655SAl Viro static int parse_sid(struct super_block *sb, const char *s, u32 *sid) 608bd323655SAl Viro { 609bd323655SAl Viro int rc = security_context_str_to_sid(&selinux_state, s, 610bd323655SAl Viro sid, GFP_KERNEL); 611bd323655SAl Viro if (rc) 612bd323655SAl Viro pr_warn("SELinux: security_context_str_to_sid" 613bd323655SAl Viro "(%s) failed for (dev %s, type %s) errno=%d\n", 614bd323655SAl Viro s, sb->s_id, sb->s_type->name, rc); 615bd323655SAl Viro return rc; 616bd323655SAl Viro } 617bd323655SAl Viro 618c9180a57SEric Paris /* 619c9180a57SEric Paris * Allow filesystems with binary mount data to explicitly set mount point 620c9180a57SEric Paris * labeling information. 621c9180a57SEric Paris */ 622e0007529SEric Paris static int selinux_set_mnt_opts(struct super_block *sb, 623204cc0ccSAl Viro void *mnt_opts, 624649f6e77SDavid Quigley unsigned long kern_flags, 625649f6e77SDavid Quigley unsigned long *set_kern_flags) 626c9180a57SEric Paris { 627275bb41eSDavid Howells const struct cred *cred = current_cred(); 628c9180a57SEric Paris struct superblock_security_struct *sbsec = sb->s_security; 62983da53c5SAndreas Gruenbacher struct dentry *root = sbsec->sb->s_root; 630bd323655SAl Viro struct selinux_mnt_opts *opts = mnt_opts; 6312c97165bSPaul Moore struct inode_security_struct *root_isec; 632c9180a57SEric Paris u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0; 633c9180a57SEric Paris u32 defcontext_sid = 0; 634bd323655SAl Viro int rc = 0; 635c9180a57SEric Paris 636c9180a57SEric Paris mutex_lock(&sbsec->lock); 637c9180a57SEric Paris 638aa8e712cSStephen Smalley if (!selinux_state.initialized) { 639bd323655SAl Viro if (!opts) { 640c9180a57SEric Paris /* Defer initialization until selinux_complete_init, 641c9180a57SEric Paris after the initial policy is loaded and the security 642c9180a57SEric Paris server is ready to handle calls. */ 643c9180a57SEric Paris goto out; 644c9180a57SEric Paris } 645c9180a57SEric Paris rc = -EINVAL; 646c103a91eSpeter enderborg pr_warn("SELinux: Unable to set superblock options " 647744ba35eSEric Paris "before the security server is initialized\n"); 648c9180a57SEric Paris goto out; 649c9180a57SEric Paris } 650649f6e77SDavid Quigley if (kern_flags && !set_kern_flags) { 651649f6e77SDavid Quigley /* Specifying internal flags without providing a place to 652649f6e77SDavid Quigley * place the results is not allowed */ 653649f6e77SDavid Quigley rc = -EINVAL; 654649f6e77SDavid Quigley goto out; 655649f6e77SDavid Quigley } 656c9180a57SEric Paris 657c9180a57SEric Paris /* 658e0007529SEric Paris * Binary mount data FS will come through this function twice. Once 659e0007529SEric Paris * from an explicit call and once from the generic calls from the vfs. 660e0007529SEric Paris * Since the generic VFS calls will not contain any security mount data 661e0007529SEric Paris * we need to skip the double mount verification. 662e0007529SEric Paris * 663e0007529SEric Paris * This does open a hole in which we will not notice if the first 664e0007529SEric Paris * mount using this sb set explict options and a second mount using 665e0007529SEric Paris * this sb does not set any security options. (The first options 666e0007529SEric Paris * will be used for both mounts) 667e0007529SEric Paris */ 6680d90a7ecSDavid P. Quigley if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA) 669bd323655SAl Viro && !opts) 670e0007529SEric Paris goto out; 671e0007529SEric Paris 6722c97165bSPaul Moore root_isec = backing_inode_security_novalidate(root); 6732c97165bSPaul Moore 674e0007529SEric Paris /* 675c9180a57SEric Paris * parse the mount options, check if they are valid sids. 676c9180a57SEric Paris * also check if someone is trying to mount the same sb more 677c9180a57SEric Paris * than once with different security options. 678c9180a57SEric Paris */ 679bd323655SAl Viro if (opts) { 680bd323655SAl Viro if (opts->fscontext) { 681bd323655SAl Viro rc = parse_sid(sb, opts->fscontext, &fscontext_sid); 682bd323655SAl Viro if (rc) 683c9180a57SEric Paris goto out; 684c9180a57SEric Paris if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, 685c9180a57SEric Paris fscontext_sid)) 686c9180a57SEric Paris goto out_double_mount; 687c9180a57SEric Paris sbsec->flags |= FSCONTEXT_MNT; 688bd323655SAl Viro } 689bd323655SAl Viro if (opts->context) { 690bd323655SAl Viro rc = parse_sid(sb, opts->context, &context_sid); 691bd323655SAl Viro if (rc) 692bd323655SAl Viro goto out; 693c9180a57SEric Paris if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, 694c9180a57SEric Paris context_sid)) 695c9180a57SEric Paris goto out_double_mount; 696c9180a57SEric Paris sbsec->flags |= CONTEXT_MNT; 697bd323655SAl Viro } 698bd323655SAl Viro if (opts->rootcontext) { 699bd323655SAl Viro rc = parse_sid(sb, opts->rootcontext, &rootcontext_sid); 700bd323655SAl Viro if (rc) 701bd323655SAl Viro goto out; 702c9180a57SEric Paris if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, 703c9180a57SEric Paris rootcontext_sid)) 704c9180a57SEric Paris goto out_double_mount; 705c9180a57SEric Paris sbsec->flags |= ROOTCONTEXT_MNT; 706bd323655SAl Viro } 707bd323655SAl Viro if (opts->defcontext) { 708bd323655SAl Viro rc = parse_sid(sb, opts->defcontext, &defcontext_sid); 709bd323655SAl Viro if (rc) 710bd323655SAl Viro goto out; 711c9180a57SEric Paris if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, 712c9180a57SEric Paris defcontext_sid)) 713c9180a57SEric Paris goto out_double_mount; 714c9180a57SEric Paris sbsec->flags |= DEFCONTEXT_MNT; 715c9180a57SEric Paris } 716c9180a57SEric Paris } 717c9180a57SEric Paris 7180d90a7ecSDavid P. Quigley if (sbsec->flags & SE_SBINITIALIZED) { 719c9180a57SEric Paris /* previously mounted with options, but not on this attempt? */ 720bd323655SAl Viro if ((sbsec->flags & SE_MNTMASK) && !opts) 721c9180a57SEric Paris goto out_double_mount; 722c9180a57SEric Paris rc = 0; 723c9180a57SEric Paris goto out; 724c9180a57SEric Paris } 725c9180a57SEric Paris 726089be43eSJames Morris if (strcmp(sb->s_type->name, "proc") == 0) 727134509d5SStephen Smalley sbsec->flags |= SE_SBPROC | SE_SBGENFS; 728134509d5SStephen Smalley 7298e014720SStephen Smalley if (!strcmp(sb->s_type->name, "debugfs") || 7306a391183SJeff Vander Stoep !strcmp(sb->s_type->name, "tracefs") || 7318e014720SStephen Smalley !strcmp(sb->s_type->name, "sysfs") || 732901ef845SAntonio Murdaca !strcmp(sb->s_type->name, "pstore") || 733901ef845SAntonio Murdaca !strcmp(sb->s_type->name, "cgroup") || 734901ef845SAntonio Murdaca !strcmp(sb->s_type->name, "cgroup2")) 735134509d5SStephen Smalley sbsec->flags |= SE_SBGENFS; 736c9180a57SEric Paris 737eb9ae686SDavid Quigley if (!sbsec->behavior) { 738eb9ae686SDavid Quigley /* 739eb9ae686SDavid Quigley * Determine the labeling behavior to use for this 740eb9ae686SDavid Quigley * filesystem type. 741eb9ae686SDavid Quigley */ 742aa8e712cSStephen Smalley rc = security_fs_use(&selinux_state, sb); 743c9180a57SEric Paris if (rc) { 744c103a91eSpeter enderborg pr_warn("%s: security_fs_use(%s) returned %d\n", 745089be43eSJames Morris __func__, sb->s_type->name, rc); 746c9180a57SEric Paris goto out; 747c9180a57SEric Paris } 748eb9ae686SDavid Quigley } 749aad82892SSeth Forshee 750aad82892SSeth Forshee /* 75101593d32SStephen Smalley * If this is a user namespace mount and the filesystem type is not 75201593d32SStephen Smalley * explicitly whitelisted, then no contexts are allowed on the command 75301593d32SStephen Smalley * line and security labels must be ignored. 754aad82892SSeth Forshee */ 75501593d32SStephen Smalley if (sb->s_user_ns != &init_user_ns && 75601593d32SStephen Smalley strcmp(sb->s_type->name, "tmpfs") && 75701593d32SStephen Smalley strcmp(sb->s_type->name, "ramfs") && 75801593d32SStephen Smalley strcmp(sb->s_type->name, "devpts")) { 759aad82892SSeth Forshee if (context_sid || fscontext_sid || rootcontext_sid || 760aad82892SSeth Forshee defcontext_sid) { 761aad82892SSeth Forshee rc = -EACCES; 762aad82892SSeth Forshee goto out; 763aad82892SSeth Forshee } 764aad82892SSeth Forshee if (sbsec->behavior == SECURITY_FS_USE_XATTR) { 765aad82892SSeth Forshee sbsec->behavior = SECURITY_FS_USE_MNTPOINT; 766aa8e712cSStephen Smalley rc = security_transition_sid(&selinux_state, 767aa8e712cSStephen Smalley current_sid(), 768aa8e712cSStephen Smalley current_sid(), 769aad82892SSeth Forshee SECCLASS_FILE, NULL, 770aad82892SSeth Forshee &sbsec->mntpoint_sid); 771aad82892SSeth Forshee if (rc) 772aad82892SSeth Forshee goto out; 773aad82892SSeth Forshee } 774aad82892SSeth Forshee goto out_set_opts; 775aad82892SSeth Forshee } 776aad82892SSeth Forshee 777c9180a57SEric Paris /* sets the context of the superblock for the fs being mounted. */ 778c9180a57SEric Paris if (fscontext_sid) { 779275bb41eSDavid Howells rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred); 780c9180a57SEric Paris if (rc) 781c9180a57SEric Paris goto out; 782c9180a57SEric Paris 783c9180a57SEric Paris sbsec->sid = fscontext_sid; 784c9180a57SEric Paris } 785c9180a57SEric Paris 786c9180a57SEric Paris /* 787c9180a57SEric Paris * Switch to using mount point labeling behavior. 788c9180a57SEric Paris * sets the label used on all file below the mountpoint, and will set 789c9180a57SEric Paris * the superblock context if not already set. 790c9180a57SEric Paris */ 791eb9ae686SDavid Quigley if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) { 792eb9ae686SDavid Quigley sbsec->behavior = SECURITY_FS_USE_NATIVE; 793eb9ae686SDavid Quigley *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS; 794eb9ae686SDavid Quigley } 795eb9ae686SDavid Quigley 796c9180a57SEric Paris if (context_sid) { 797c9180a57SEric Paris if (!fscontext_sid) { 798275bb41eSDavid Howells rc = may_context_mount_sb_relabel(context_sid, sbsec, 799275bb41eSDavid Howells cred); 800c9180a57SEric Paris if (rc) 801c9180a57SEric Paris goto out; 802c9180a57SEric Paris sbsec->sid = context_sid; 803c9180a57SEric Paris } else { 804275bb41eSDavid Howells rc = may_context_mount_inode_relabel(context_sid, sbsec, 805275bb41eSDavid Howells cred); 806c9180a57SEric Paris if (rc) 807c9180a57SEric Paris goto out; 808c9180a57SEric Paris } 809c9180a57SEric Paris if (!rootcontext_sid) 810c9180a57SEric Paris rootcontext_sid = context_sid; 811c9180a57SEric Paris 812c9180a57SEric Paris sbsec->mntpoint_sid = context_sid; 813c9180a57SEric Paris sbsec->behavior = SECURITY_FS_USE_MNTPOINT; 814c9180a57SEric Paris } 815c9180a57SEric Paris 816c9180a57SEric Paris if (rootcontext_sid) { 817275bb41eSDavid Howells rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec, 818275bb41eSDavid Howells cred); 819c9180a57SEric Paris if (rc) 820c9180a57SEric Paris goto out; 821c9180a57SEric Paris 822c9180a57SEric Paris root_isec->sid = rootcontext_sid; 8236f3be9f5SAndreas Gruenbacher root_isec->initialized = LABEL_INITIALIZED; 824c9180a57SEric Paris } 825c9180a57SEric Paris 826c9180a57SEric Paris if (defcontext_sid) { 827eb9ae686SDavid Quigley if (sbsec->behavior != SECURITY_FS_USE_XATTR && 828eb9ae686SDavid Quigley sbsec->behavior != SECURITY_FS_USE_NATIVE) { 829c9180a57SEric Paris rc = -EINVAL; 830c103a91eSpeter enderborg pr_warn("SELinux: defcontext option is " 831c9180a57SEric Paris "invalid for this filesystem type\n"); 832c9180a57SEric Paris goto out; 833c9180a57SEric Paris } 834c9180a57SEric Paris 835c9180a57SEric Paris if (defcontext_sid != sbsec->def_sid) { 836c9180a57SEric Paris rc = may_context_mount_inode_relabel(defcontext_sid, 837275bb41eSDavid Howells sbsec, cred); 838c9180a57SEric Paris if (rc) 839c9180a57SEric Paris goto out; 840c9180a57SEric Paris } 841c9180a57SEric Paris 842c9180a57SEric Paris sbsec->def_sid = defcontext_sid; 843c9180a57SEric Paris } 844c9180a57SEric Paris 845aad82892SSeth Forshee out_set_opts: 846c9180a57SEric Paris rc = sb_finish_set_opts(sb); 847c9180a57SEric Paris out: 848bc7e982bSEric Paris mutex_unlock(&sbsec->lock); 8491da177e4SLinus Torvalds return rc; 850c9180a57SEric Paris out_double_mount: 851c9180a57SEric Paris rc = -EINVAL; 852c103a91eSpeter enderborg pr_warn("SELinux: mount invalid. Same superblock, different " 853bd323655SAl Viro "security settings for (dev %s, type %s)\n", sb->s_id, 854bd323655SAl Viro sb->s_type->name); 855c9180a57SEric Paris goto out; 856c9180a57SEric Paris } 857c9180a57SEric Paris 858094f7b69SJeff Layton static int selinux_cmp_sb_context(const struct super_block *oldsb, 859094f7b69SJeff Layton const struct super_block *newsb) 860094f7b69SJeff Layton { 861094f7b69SJeff Layton struct superblock_security_struct *old = oldsb->s_security; 862094f7b69SJeff Layton struct superblock_security_struct *new = newsb->s_security; 863094f7b69SJeff Layton char oldflags = old->flags & SE_MNTMASK; 864094f7b69SJeff Layton char newflags = new->flags & SE_MNTMASK; 865094f7b69SJeff Layton 866094f7b69SJeff Layton if (oldflags != newflags) 867094f7b69SJeff Layton goto mismatch; 868094f7b69SJeff Layton if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid) 869094f7b69SJeff Layton goto mismatch; 870094f7b69SJeff Layton if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid) 871094f7b69SJeff Layton goto mismatch; 872094f7b69SJeff Layton if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid) 873094f7b69SJeff Layton goto mismatch; 874094f7b69SJeff Layton if (oldflags & ROOTCONTEXT_MNT) { 87583da53c5SAndreas Gruenbacher struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root); 87683da53c5SAndreas Gruenbacher struct inode_security_struct *newroot = backing_inode_security(newsb->s_root); 877094f7b69SJeff Layton if (oldroot->sid != newroot->sid) 878094f7b69SJeff Layton goto mismatch; 879094f7b69SJeff Layton } 880094f7b69SJeff Layton return 0; 881094f7b69SJeff Layton mismatch: 882c103a91eSpeter enderborg pr_warn("SELinux: mount invalid. Same superblock, " 883094f7b69SJeff Layton "different security settings for (dev %s, " 884094f7b69SJeff Layton "type %s)\n", newsb->s_id, newsb->s_type->name); 885094f7b69SJeff Layton return -EBUSY; 886094f7b69SJeff Layton } 887094f7b69SJeff Layton 888094f7b69SJeff Layton static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb, 8890b4d3452SScott Mayhew struct super_block *newsb, 8900b4d3452SScott Mayhew unsigned long kern_flags, 8910b4d3452SScott Mayhew unsigned long *set_kern_flags) 892c9180a57SEric Paris { 8930b4d3452SScott Mayhew int rc = 0; 894c9180a57SEric Paris const struct superblock_security_struct *oldsbsec = oldsb->s_security; 895c9180a57SEric Paris struct superblock_security_struct *newsbsec = newsb->s_security; 896c9180a57SEric Paris 897c9180a57SEric Paris int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT); 898c9180a57SEric Paris int set_context = (oldsbsec->flags & CONTEXT_MNT); 899c9180a57SEric Paris int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT); 900c9180a57SEric Paris 9010f5e6420SEric Paris /* 9020f5e6420SEric Paris * if the parent was able to be mounted it clearly had no special lsm 903e8c26255SAl Viro * mount options. thus we can safely deal with this superblock later 9040f5e6420SEric Paris */ 905aa8e712cSStephen Smalley if (!selinux_state.initialized) 906094f7b69SJeff Layton return 0; 907c9180a57SEric Paris 9080b4d3452SScott Mayhew /* 9090b4d3452SScott Mayhew * Specifying internal flags without providing a place to 9100b4d3452SScott Mayhew * place the results is not allowed. 9110b4d3452SScott Mayhew */ 9120b4d3452SScott Mayhew if (kern_flags && !set_kern_flags) 9130b4d3452SScott Mayhew return -EINVAL; 9140b4d3452SScott Mayhew 915c9180a57SEric Paris /* how can we clone if the old one wasn't set up?? */ 9160d90a7ecSDavid P. Quigley BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED)); 917c9180a57SEric Paris 918094f7b69SJeff Layton /* if fs is reusing a sb, make sure that the contexts match */ 9190d90a7ecSDavid P. Quigley if (newsbsec->flags & SE_SBINITIALIZED) 920094f7b69SJeff Layton return selinux_cmp_sb_context(oldsb, newsb); 9215a552617SEric Paris 922c9180a57SEric Paris mutex_lock(&newsbsec->lock); 923c9180a57SEric Paris 924c9180a57SEric Paris newsbsec->flags = oldsbsec->flags; 925c9180a57SEric Paris 926c9180a57SEric Paris newsbsec->sid = oldsbsec->sid; 927c9180a57SEric Paris newsbsec->def_sid = oldsbsec->def_sid; 928c9180a57SEric Paris newsbsec->behavior = oldsbsec->behavior; 929c9180a57SEric Paris 9300b4d3452SScott Mayhew if (newsbsec->behavior == SECURITY_FS_USE_NATIVE && 9310b4d3452SScott Mayhew !(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) { 932aa8e712cSStephen Smalley rc = security_fs_use(&selinux_state, newsb); 9330b4d3452SScott Mayhew if (rc) 9340b4d3452SScott Mayhew goto out; 9350b4d3452SScott Mayhew } 9360b4d3452SScott Mayhew 9370b4d3452SScott Mayhew if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) { 9380b4d3452SScott Mayhew newsbsec->behavior = SECURITY_FS_USE_NATIVE; 9390b4d3452SScott Mayhew *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS; 9400b4d3452SScott Mayhew } 9410b4d3452SScott Mayhew 942c9180a57SEric Paris if (set_context) { 943c9180a57SEric Paris u32 sid = oldsbsec->mntpoint_sid; 944c9180a57SEric Paris 945c9180a57SEric Paris if (!set_fscontext) 946c9180a57SEric Paris newsbsec->sid = sid; 947c9180a57SEric Paris if (!set_rootcontext) { 94883da53c5SAndreas Gruenbacher struct inode_security_struct *newisec = backing_inode_security(newsb->s_root); 949c9180a57SEric Paris newisec->sid = sid; 950c9180a57SEric Paris } 951c9180a57SEric Paris newsbsec->mntpoint_sid = sid; 952c9180a57SEric Paris } 953c9180a57SEric Paris if (set_rootcontext) { 95483da53c5SAndreas Gruenbacher const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root); 95583da53c5SAndreas Gruenbacher struct inode_security_struct *newisec = backing_inode_security(newsb->s_root); 956c9180a57SEric Paris 957c9180a57SEric Paris newisec->sid = oldisec->sid; 958c9180a57SEric Paris } 959c9180a57SEric Paris 960c9180a57SEric Paris sb_finish_set_opts(newsb); 9610b4d3452SScott Mayhew out: 962c9180a57SEric Paris mutex_unlock(&newsbsec->lock); 9630b4d3452SScott Mayhew return rc; 964c9180a57SEric Paris } 965c9180a57SEric Paris 966ba641862SAl Viro static int selinux_add_opt(int token, const char *s, void **mnt_opts) 967c9180a57SEric Paris { 968ba641862SAl Viro struct selinux_mnt_opts *opts = *mnt_opts; 969c9180a57SEric Paris 970da3d76abSAl Viro if (token == Opt_seclabel) /* eaten and completely ignored */ 971e0007529SEric Paris return 0; 972e0007529SEric Paris 973ba641862SAl Viro if (!opts) { 974ba641862SAl Viro opts = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL); 975ba641862SAl Viro if (!opts) 976ba641862SAl Viro return -ENOMEM; 977ba641862SAl Viro *mnt_opts = opts; 978ba641862SAl Viro } 979ba641862SAl Viro if (!s) 980ba641862SAl Viro return -ENOMEM; 981ba641862SAl Viro switch (token) { 982ba641862SAl Viro case Opt_context: 983ba641862SAl Viro if (opts->context || opts->defcontext) 984ba641862SAl Viro goto Einval; 985ba641862SAl Viro opts->context = s; 986ba641862SAl Viro break; 987ba641862SAl Viro case Opt_fscontext: 988ba641862SAl Viro if (opts->fscontext) 989ba641862SAl Viro goto Einval; 990ba641862SAl Viro opts->fscontext = s; 991ba641862SAl Viro break; 992ba641862SAl Viro case Opt_rootcontext: 993ba641862SAl Viro if (opts->rootcontext) 994ba641862SAl Viro goto Einval; 995ba641862SAl Viro opts->rootcontext = s; 996ba641862SAl Viro break; 997ba641862SAl Viro case Opt_defcontext: 998ba641862SAl Viro if (opts->context || opts->defcontext) 999ba641862SAl Viro goto Einval; 1000ba641862SAl Viro opts->defcontext = s; 1001ba641862SAl Viro break; 1002ba641862SAl Viro } 1003ba641862SAl Viro return 0; 1004ba641862SAl Viro Einval: 1005ba641862SAl Viro pr_warn(SEL_MOUNT_FAIL_MSG); 1006ba641862SAl Viro return -EINVAL; 1007ba641862SAl Viro } 1008ba641862SAl Viro 1009757cbe59SAl Viro static int selinux_add_mnt_opt(const char *option, const char *val, int len, 1010204cc0ccSAl Viro void **mnt_opts) 1011c9180a57SEric Paris { 1012757cbe59SAl Viro int token = Opt_error; 1013757cbe59SAl Viro int rc, i; 1014c9180a57SEric Paris 1015757cbe59SAl Viro for (i = 0; i < ARRAY_SIZE(tokens); i++) { 1016757cbe59SAl Viro if (strcmp(option, tokens[i].name) == 0) { 1017757cbe59SAl Viro token = tokens[i].opt; 1018757cbe59SAl Viro break; 1019757cbe59SAl Viro } 1020169d68efSAl Viro } 1021169d68efSAl Viro 1022757cbe59SAl Viro if (token == Opt_error) 1023757cbe59SAl Viro return -EINVAL; 1024c9180a57SEric Paris 1025757cbe59SAl Viro if (token != Opt_seclabel) 1026757cbe59SAl Viro val = kmemdup_nul(val, len, GFP_KERNEL); 1027757cbe59SAl Viro rc = selinux_add_opt(token, val, mnt_opts); 1028757cbe59SAl Viro if (unlikely(rc)) { 1029757cbe59SAl Viro kfree(val); 1030757cbe59SAl Viro if (*mnt_opts) { 1031ba641862SAl Viro selinux_free_mnt_opts(*mnt_opts); 1032ba641862SAl Viro *mnt_opts = NULL; 1033757cbe59SAl Viro } 1034757cbe59SAl Viro } 1035c9180a57SEric Paris return rc; 10361da177e4SLinus Torvalds } 10371da177e4SLinus Torvalds 1038e3489f89SAl Viro static int show_sid(struct seq_file *m, u32 sid) 10392069f457SEric Paris { 1040e3489f89SAl Viro char *context = NULL; 1041e3489f89SAl Viro u32 len; 1042e3489f89SAl Viro int rc; 10432069f457SEric Paris 1044e3489f89SAl Viro rc = security_sid_to_context(&selinux_state, sid, 1045e3489f89SAl Viro &context, &len); 1046e3489f89SAl Viro if (!rc) { 1047e3489f89SAl Viro bool has_comma = context && strchr(context, ','); 104811689d47SDavid P. Quigley 10492069f457SEric Paris if (has_comma) 10502069f457SEric Paris seq_putc(m, '\"'); 1051e3489f89SAl Viro seq_escape(m, context, "\"\n\\"); 10522069f457SEric Paris if (has_comma) 10532069f457SEric Paris seq_putc(m, '\"'); 10542069f457SEric Paris } 10551da177e4SLinus Torvalds kfree(context); 10561da177e4SLinus Torvalds return rc; 10571da177e4SLinus Torvalds } 10582069f457SEric Paris 10592069f457SEric Paris static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb) 10602069f457SEric Paris { 1061e3489f89SAl Viro struct superblock_security_struct *sbsec = sb->s_security; 10622069f457SEric Paris int rc; 10632069f457SEric Paris 1064e3489f89SAl Viro if (!(sbsec->flags & SE_SBINITIALIZED)) 1065e3489f89SAl Viro return 0; 1066e3489f89SAl Viro 1067e3489f89SAl Viro if (!selinux_state.initialized) 1068e3489f89SAl Viro return 0; 1069e3489f89SAl Viro 1070e3489f89SAl Viro if (sbsec->flags & FSCONTEXT_MNT) { 1071e3489f89SAl Viro seq_putc(m, ','); 1072e3489f89SAl Viro seq_puts(m, FSCONTEXT_STR); 1073e3489f89SAl Viro rc = show_sid(m, sbsec->sid); 1074e3489f89SAl Viro if (rc) 10752069f457SEric Paris return rc; 1076383795c2SEric Paris } 1077e3489f89SAl Viro if (sbsec->flags & CONTEXT_MNT) { 1078e3489f89SAl Viro seq_putc(m, ','); 1079e3489f89SAl Viro seq_puts(m, CONTEXT_STR); 1080e3489f89SAl Viro rc = show_sid(m, sbsec->mntpoint_sid); 1081e3489f89SAl Viro if (rc) 10822069f457SEric Paris return rc; 10832069f457SEric Paris } 1084e3489f89SAl Viro if (sbsec->flags & DEFCONTEXT_MNT) { 1085e3489f89SAl Viro seq_putc(m, ','); 1086e3489f89SAl Viro seq_puts(m, DEFCONTEXT_STR); 1087e3489f89SAl Viro rc = show_sid(m, sbsec->def_sid); 1088e3489f89SAl Viro if (rc) 1089e3489f89SAl Viro return rc; 1090e3489f89SAl Viro } 1091e3489f89SAl Viro if (sbsec->flags & ROOTCONTEXT_MNT) { 1092e3489f89SAl Viro struct dentry *root = sbsec->sb->s_root; 1093e3489f89SAl Viro struct inode_security_struct *isec = backing_inode_security(root); 1094e3489f89SAl Viro seq_putc(m, ','); 1095e3489f89SAl Viro seq_puts(m, ROOTCONTEXT_STR); 1096e3489f89SAl Viro rc = show_sid(m, isec->sid); 1097e3489f89SAl Viro if (rc) 1098e3489f89SAl Viro return rc; 1099e3489f89SAl Viro } 1100e3489f89SAl Viro if (sbsec->flags & SBLABEL_MNT) { 1101e3489f89SAl Viro seq_putc(m, ','); 1102e3489f89SAl Viro seq_puts(m, LABELSUPP_STR); 1103e3489f89SAl Viro } 1104e3489f89SAl Viro return 0; 1105e3489f89SAl Viro } 11062069f457SEric Paris 11071da177e4SLinus Torvalds static inline u16 inode_mode_to_security_class(umode_t mode) 11081da177e4SLinus Torvalds { 11091da177e4SLinus Torvalds switch (mode & S_IFMT) { 11101da177e4SLinus Torvalds case S_IFSOCK: 11111da177e4SLinus Torvalds return SECCLASS_SOCK_FILE; 11121da177e4SLinus Torvalds case S_IFLNK: 11131da177e4SLinus Torvalds return SECCLASS_LNK_FILE; 11141da177e4SLinus Torvalds case S_IFREG: 11151da177e4SLinus Torvalds return SECCLASS_FILE; 11161da177e4SLinus Torvalds case S_IFBLK: 11171da177e4SLinus Torvalds return SECCLASS_BLK_FILE; 11181da177e4SLinus Torvalds case S_IFDIR: 11191da177e4SLinus Torvalds return SECCLASS_DIR; 11201da177e4SLinus Torvalds case S_IFCHR: 11211da177e4SLinus Torvalds return SECCLASS_CHR_FILE; 11221da177e4SLinus Torvalds case S_IFIFO: 11231da177e4SLinus Torvalds return SECCLASS_FIFO_FILE; 11241da177e4SLinus Torvalds 11251da177e4SLinus Torvalds } 11261da177e4SLinus Torvalds 11271da177e4SLinus Torvalds return SECCLASS_FILE; 11281da177e4SLinus Torvalds } 11291da177e4SLinus Torvalds 113013402580SJames Morris static inline int default_protocol_stream(int protocol) 113113402580SJames Morris { 113213402580SJames Morris return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP); 113313402580SJames Morris } 113413402580SJames Morris 113513402580SJames Morris static inline int default_protocol_dgram(int protocol) 113613402580SJames Morris { 113713402580SJames Morris return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP); 113813402580SJames Morris } 113913402580SJames Morris 11401da177e4SLinus Torvalds static inline u16 socket_type_to_security_class(int family, int type, int protocol) 11411da177e4SLinus Torvalds { 1142aa8e712cSStephen Smalley int extsockclass = selinux_policycap_extsockclass(); 1143da69a530SStephen Smalley 11441da177e4SLinus Torvalds switch (family) { 11451da177e4SLinus Torvalds case PF_UNIX: 11461da177e4SLinus Torvalds switch (type) { 11471da177e4SLinus Torvalds case SOCK_STREAM: 11481da177e4SLinus Torvalds case SOCK_SEQPACKET: 11491da177e4SLinus Torvalds return SECCLASS_UNIX_STREAM_SOCKET; 11501da177e4SLinus Torvalds case SOCK_DGRAM: 11512a764b52SLuis Ressel case SOCK_RAW: 11521da177e4SLinus Torvalds return SECCLASS_UNIX_DGRAM_SOCKET; 11531da177e4SLinus Torvalds } 11541da177e4SLinus Torvalds break; 11551da177e4SLinus Torvalds case PF_INET: 11561da177e4SLinus Torvalds case PF_INET6: 11571da177e4SLinus Torvalds switch (type) { 11581da177e4SLinus Torvalds case SOCK_STREAM: 1159da69a530SStephen Smalley case SOCK_SEQPACKET: 116013402580SJames Morris if (default_protocol_stream(protocol)) 11611da177e4SLinus Torvalds return SECCLASS_TCP_SOCKET; 1162da69a530SStephen Smalley else if (extsockclass && protocol == IPPROTO_SCTP) 1163da69a530SStephen Smalley return SECCLASS_SCTP_SOCKET; 116413402580SJames Morris else 116513402580SJames Morris return SECCLASS_RAWIP_SOCKET; 11661da177e4SLinus Torvalds case SOCK_DGRAM: 116713402580SJames Morris if (default_protocol_dgram(protocol)) 11681da177e4SLinus Torvalds return SECCLASS_UDP_SOCKET; 1169ef37979aSStephen Smalley else if (extsockclass && (protocol == IPPROTO_ICMP || 1170ef37979aSStephen Smalley protocol == IPPROTO_ICMPV6)) 1171da69a530SStephen Smalley return SECCLASS_ICMP_SOCKET; 117213402580SJames Morris else 117313402580SJames Morris return SECCLASS_RAWIP_SOCKET; 11742ee92d46SJames Morris case SOCK_DCCP: 11752ee92d46SJames Morris return SECCLASS_DCCP_SOCKET; 117613402580SJames Morris default: 11771da177e4SLinus Torvalds return SECCLASS_RAWIP_SOCKET; 11781da177e4SLinus Torvalds } 11791da177e4SLinus Torvalds break; 11801da177e4SLinus Torvalds case PF_NETLINK: 11811da177e4SLinus Torvalds switch (protocol) { 11821da177e4SLinus Torvalds case NETLINK_ROUTE: 11831da177e4SLinus Torvalds return SECCLASS_NETLINK_ROUTE_SOCKET; 11847f1fb60cSPavel Emelyanov case NETLINK_SOCK_DIAG: 11851da177e4SLinus Torvalds return SECCLASS_NETLINK_TCPDIAG_SOCKET; 11861da177e4SLinus Torvalds case NETLINK_NFLOG: 11871da177e4SLinus Torvalds return SECCLASS_NETLINK_NFLOG_SOCKET; 11881da177e4SLinus Torvalds case NETLINK_XFRM: 11891da177e4SLinus Torvalds return SECCLASS_NETLINK_XFRM_SOCKET; 11901da177e4SLinus Torvalds case NETLINK_SELINUX: 11911da177e4SLinus Torvalds return SECCLASS_NETLINK_SELINUX_SOCKET; 11926c6d2e9bSStephen Smalley case NETLINK_ISCSI: 11936c6d2e9bSStephen Smalley return SECCLASS_NETLINK_ISCSI_SOCKET; 11941da177e4SLinus Torvalds case NETLINK_AUDIT: 11951da177e4SLinus Torvalds return SECCLASS_NETLINK_AUDIT_SOCKET; 11966c6d2e9bSStephen Smalley case NETLINK_FIB_LOOKUP: 11976c6d2e9bSStephen Smalley return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET; 11986c6d2e9bSStephen Smalley case NETLINK_CONNECTOR: 11996c6d2e9bSStephen Smalley return SECCLASS_NETLINK_CONNECTOR_SOCKET; 12006c6d2e9bSStephen Smalley case NETLINK_NETFILTER: 12016c6d2e9bSStephen Smalley return SECCLASS_NETLINK_NETFILTER_SOCKET; 12021da177e4SLinus Torvalds case NETLINK_DNRTMSG: 12031da177e4SLinus Torvalds return SECCLASS_NETLINK_DNRT_SOCKET; 12040c9b7942SJames Morris case NETLINK_KOBJECT_UEVENT: 12050c9b7942SJames Morris return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET; 12066c6d2e9bSStephen Smalley case NETLINK_GENERIC: 12076c6d2e9bSStephen Smalley return SECCLASS_NETLINK_GENERIC_SOCKET; 12086c6d2e9bSStephen Smalley case NETLINK_SCSITRANSPORT: 12096c6d2e9bSStephen Smalley return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET; 12106c6d2e9bSStephen Smalley case NETLINK_RDMA: 12116c6d2e9bSStephen Smalley return SECCLASS_NETLINK_RDMA_SOCKET; 12126c6d2e9bSStephen Smalley case NETLINK_CRYPTO: 12136c6d2e9bSStephen Smalley return SECCLASS_NETLINK_CRYPTO_SOCKET; 12141da177e4SLinus Torvalds default: 12151da177e4SLinus Torvalds return SECCLASS_NETLINK_SOCKET; 12161da177e4SLinus Torvalds } 12171da177e4SLinus Torvalds case PF_PACKET: 12181da177e4SLinus Torvalds return SECCLASS_PACKET_SOCKET; 12191da177e4SLinus Torvalds case PF_KEY: 12201da177e4SLinus Torvalds return SECCLASS_KEY_SOCKET; 12213e3ff15eSChristopher J. PeBenito case PF_APPLETALK: 12223e3ff15eSChristopher J. PeBenito return SECCLASS_APPLETALK_SOCKET; 12231da177e4SLinus Torvalds } 12241da177e4SLinus Torvalds 1225da69a530SStephen Smalley if (extsockclass) { 1226da69a530SStephen Smalley switch (family) { 1227da69a530SStephen Smalley case PF_AX25: 1228da69a530SStephen Smalley return SECCLASS_AX25_SOCKET; 1229da69a530SStephen Smalley case PF_IPX: 1230da69a530SStephen Smalley return SECCLASS_IPX_SOCKET; 1231da69a530SStephen Smalley case PF_NETROM: 1232da69a530SStephen Smalley return SECCLASS_NETROM_SOCKET; 1233da69a530SStephen Smalley case PF_ATMPVC: 1234da69a530SStephen Smalley return SECCLASS_ATMPVC_SOCKET; 1235da69a530SStephen Smalley case PF_X25: 1236da69a530SStephen Smalley return SECCLASS_X25_SOCKET; 1237da69a530SStephen Smalley case PF_ROSE: 1238da69a530SStephen Smalley return SECCLASS_ROSE_SOCKET; 1239da69a530SStephen Smalley case PF_DECnet: 1240da69a530SStephen Smalley return SECCLASS_DECNET_SOCKET; 1241da69a530SStephen Smalley case PF_ATMSVC: 1242da69a530SStephen Smalley return SECCLASS_ATMSVC_SOCKET; 1243da69a530SStephen Smalley case PF_RDS: 1244da69a530SStephen Smalley return SECCLASS_RDS_SOCKET; 1245da69a530SStephen Smalley case PF_IRDA: 1246da69a530SStephen Smalley return SECCLASS_IRDA_SOCKET; 1247da69a530SStephen Smalley case PF_PPPOX: 1248da69a530SStephen Smalley return SECCLASS_PPPOX_SOCKET; 1249da69a530SStephen Smalley case PF_LLC: 1250da69a530SStephen Smalley return SECCLASS_LLC_SOCKET; 1251da69a530SStephen Smalley case PF_CAN: 1252da69a530SStephen Smalley return SECCLASS_CAN_SOCKET; 1253da69a530SStephen Smalley case PF_TIPC: 1254da69a530SStephen Smalley return SECCLASS_TIPC_SOCKET; 1255da69a530SStephen Smalley case PF_BLUETOOTH: 1256da69a530SStephen Smalley return SECCLASS_BLUETOOTH_SOCKET; 1257da69a530SStephen Smalley case PF_IUCV: 1258da69a530SStephen Smalley return SECCLASS_IUCV_SOCKET; 1259da69a530SStephen Smalley case PF_RXRPC: 1260da69a530SStephen Smalley return SECCLASS_RXRPC_SOCKET; 1261da69a530SStephen Smalley case PF_ISDN: 1262da69a530SStephen Smalley return SECCLASS_ISDN_SOCKET; 1263da69a530SStephen Smalley case PF_PHONET: 1264da69a530SStephen Smalley return SECCLASS_PHONET_SOCKET; 1265da69a530SStephen Smalley case PF_IEEE802154: 1266da69a530SStephen Smalley return SECCLASS_IEEE802154_SOCKET; 1267da69a530SStephen Smalley case PF_CAIF: 1268da69a530SStephen Smalley return SECCLASS_CAIF_SOCKET; 1269da69a530SStephen Smalley case PF_ALG: 1270da69a530SStephen Smalley return SECCLASS_ALG_SOCKET; 1271da69a530SStephen Smalley case PF_NFC: 1272da69a530SStephen Smalley return SECCLASS_NFC_SOCKET; 1273da69a530SStephen Smalley case PF_VSOCK: 1274da69a530SStephen Smalley return SECCLASS_VSOCK_SOCKET; 1275da69a530SStephen Smalley case PF_KCM: 1276da69a530SStephen Smalley return SECCLASS_KCM_SOCKET; 1277da69a530SStephen Smalley case PF_QIPCRTR: 1278da69a530SStephen Smalley return SECCLASS_QIPCRTR_SOCKET; 12793051bf36SLinus Torvalds case PF_SMC: 12803051bf36SLinus Torvalds return SECCLASS_SMC_SOCKET; 128168e8b849SBjörn Töpel case PF_XDP: 128268e8b849SBjörn Töpel return SECCLASS_XDP_SOCKET; 128368e8b849SBjörn Töpel #if PF_MAX > 45 1284da69a530SStephen Smalley #error New address family defined, please update this function. 1285da69a530SStephen Smalley #endif 1286da69a530SStephen Smalley } 1287da69a530SStephen Smalley } 1288da69a530SStephen Smalley 12891da177e4SLinus Torvalds return SECCLASS_SOCKET; 12901da177e4SLinus Torvalds } 12911da177e4SLinus Torvalds 1292134509d5SStephen Smalley static int selinux_genfs_get_sid(struct dentry *dentry, 12931da177e4SLinus Torvalds u16 tclass, 1294134509d5SStephen Smalley u16 flags, 12951da177e4SLinus Torvalds u32 *sid) 12961da177e4SLinus Torvalds { 12978e6c9693SLucian Adrian Grijincu int rc; 1298fc64005cSAl Viro struct super_block *sb = dentry->d_sb; 12998e6c9693SLucian Adrian Grijincu char *buffer, *path; 13001da177e4SLinus Torvalds 13011da177e4SLinus Torvalds buffer = (char *)__get_free_page(GFP_KERNEL); 13021da177e4SLinus Torvalds if (!buffer) 13031da177e4SLinus Torvalds return -ENOMEM; 13041da177e4SLinus Torvalds 13058e6c9693SLucian Adrian Grijincu path = dentry_path_raw(dentry, buffer, PAGE_SIZE); 13068e6c9693SLucian Adrian Grijincu if (IS_ERR(path)) 13078e6c9693SLucian Adrian Grijincu rc = PTR_ERR(path); 13088e6c9693SLucian Adrian Grijincu else { 1309134509d5SStephen Smalley if (flags & SE_SBPROC) { 13108e6c9693SLucian Adrian Grijincu /* each process gets a /proc/PID/ entry. Strip off the 13118e6c9693SLucian Adrian Grijincu * PID part to get a valid selinux labeling. 13128e6c9693SLucian Adrian Grijincu * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */ 13138e6c9693SLucian Adrian Grijincu while (path[1] >= '0' && path[1] <= '9') { 13148e6c9693SLucian Adrian Grijincu path[1] = '/'; 13158e6c9693SLucian Adrian Grijincu path++; 13161da177e4SLinus Torvalds } 1317134509d5SStephen Smalley } 1318aa8e712cSStephen Smalley rc = security_genfs_sid(&selinux_state, sb->s_type->name, 1319aa8e712cSStephen Smalley path, tclass, sid); 13207bb185edSStephen Smalley if (rc == -ENOENT) { 13217bb185edSStephen Smalley /* No match in policy, mark as unlabeled. */ 13227bb185edSStephen Smalley *sid = SECINITSID_UNLABELED; 13237bb185edSStephen Smalley rc = 0; 13247bb185edSStephen Smalley } 13258e6c9693SLucian Adrian Grijincu } 13261da177e4SLinus Torvalds free_page((unsigned long)buffer); 13271da177e4SLinus Torvalds return rc; 13281da177e4SLinus Torvalds } 13291da177e4SLinus Torvalds 13301da177e4SLinus Torvalds /* The inode's security attributes must be initialized before first use. */ 13311da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry) 13321da177e4SLinus Torvalds { 13331da177e4SLinus Torvalds struct superblock_security_struct *sbsec = NULL; 133480788c22SCasey Schaufler struct inode_security_struct *isec = selinux_inode(inode); 13359287aed2SAndreas Gruenbacher u32 task_sid, sid = 0; 13369287aed2SAndreas Gruenbacher u16 sclass; 13371da177e4SLinus Torvalds struct dentry *dentry; 13381da177e4SLinus Torvalds #define INITCONTEXTLEN 255 13391da177e4SLinus Torvalds char *context = NULL; 13401da177e4SLinus Torvalds unsigned len = 0; 13411da177e4SLinus Torvalds int rc = 0; 13421da177e4SLinus Torvalds 13436f3be9f5SAndreas Gruenbacher if (isec->initialized == LABEL_INITIALIZED) 134413457d07SAndreas Gruenbacher return 0; 13451da177e4SLinus Torvalds 13469287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 13476f3be9f5SAndreas Gruenbacher if (isec->initialized == LABEL_INITIALIZED) 134823970741SEric Paris goto out_unlock; 13491da177e4SLinus Torvalds 135013457d07SAndreas Gruenbacher if (isec->sclass == SECCLASS_FILE) 135113457d07SAndreas Gruenbacher isec->sclass = inode_mode_to_security_class(inode->i_mode); 135213457d07SAndreas Gruenbacher 13531da177e4SLinus Torvalds sbsec = inode->i_sb->s_security; 13540d90a7ecSDavid P. Quigley if (!(sbsec->flags & SE_SBINITIALIZED)) { 13551da177e4SLinus Torvalds /* Defer initialization until selinux_complete_init, 13561da177e4SLinus Torvalds after the initial policy is loaded and the security 13571da177e4SLinus Torvalds server is ready to handle calls. */ 13581da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 13591da177e4SLinus Torvalds if (list_empty(&isec->list)) 13601da177e4SLinus Torvalds list_add(&isec->list, &sbsec->isec_head); 13611da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 136223970741SEric Paris goto out_unlock; 13631da177e4SLinus Torvalds } 13641da177e4SLinus Torvalds 13659287aed2SAndreas Gruenbacher sclass = isec->sclass; 13669287aed2SAndreas Gruenbacher task_sid = isec->task_sid; 13679287aed2SAndreas Gruenbacher sid = isec->sid; 13689287aed2SAndreas Gruenbacher isec->initialized = LABEL_PENDING; 13699287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 13709287aed2SAndreas Gruenbacher 13711da177e4SLinus Torvalds switch (sbsec->behavior) { 1372eb9ae686SDavid Quigley case SECURITY_FS_USE_NATIVE: 1373eb9ae686SDavid Quigley break; 13741da177e4SLinus Torvalds case SECURITY_FS_USE_XATTR: 13755d6c3191SAndreas Gruenbacher if (!(inode->i_opflags & IOP_XATTR)) { 13769287aed2SAndreas Gruenbacher sid = sbsec->def_sid; 13771da177e4SLinus Torvalds break; 13781da177e4SLinus Torvalds } 13791da177e4SLinus Torvalds /* Need a dentry, since the xattr API requires one. 13801da177e4SLinus Torvalds Life would be simpler if we could just pass the inode. */ 13811da177e4SLinus Torvalds if (opt_dentry) { 13821da177e4SLinus Torvalds /* Called from d_instantiate or d_splice_alias. */ 13831da177e4SLinus Torvalds dentry = dget(opt_dentry); 13841da177e4SLinus Torvalds } else { 1385b127125dSAl Viro /* 1386b127125dSAl Viro * Called from selinux_complete_init, try to find a dentry. 1387b127125dSAl Viro * Some filesystems really want a connected one, so try 1388b127125dSAl Viro * that first. We could split SECURITY_FS_USE_XATTR in 1389b127125dSAl Viro * two, depending upon that... 1390b127125dSAl Viro */ 13911da177e4SLinus Torvalds dentry = d_find_alias(inode); 1392b127125dSAl Viro if (!dentry) 1393b127125dSAl Viro dentry = d_find_any_alias(inode); 13941da177e4SLinus Torvalds } 13951da177e4SLinus Torvalds if (!dentry) { 1396df7f54c0SEric Paris /* 1397df7f54c0SEric Paris * this is can be hit on boot when a file is accessed 1398df7f54c0SEric Paris * before the policy is loaded. When we load policy we 1399df7f54c0SEric Paris * may find inodes that have no dentry on the 1400df7f54c0SEric Paris * sbsec->isec_head list. No reason to complain as these 1401df7f54c0SEric Paris * will get fixed up the next time we go through 1402df7f54c0SEric Paris * inode_doinit with a dentry, before these inodes could 1403df7f54c0SEric Paris * be used again by userspace. 1404df7f54c0SEric Paris */ 14059287aed2SAndreas Gruenbacher goto out; 14061da177e4SLinus Torvalds } 14071da177e4SLinus Torvalds 14081da177e4SLinus Torvalds len = INITCONTEXTLEN; 14094cb912f1SEric Paris context = kmalloc(len+1, GFP_NOFS); 14101da177e4SLinus Torvalds if (!context) { 14111da177e4SLinus Torvalds rc = -ENOMEM; 14121da177e4SLinus Torvalds dput(dentry); 14139287aed2SAndreas Gruenbacher goto out; 14141da177e4SLinus Torvalds } 14154cb912f1SEric Paris context[len] = '\0'; 14165d6c3191SAndreas Gruenbacher rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len); 14171da177e4SLinus Torvalds if (rc == -ERANGE) { 1418314dabb8SJames Morris kfree(context); 1419314dabb8SJames Morris 14201da177e4SLinus Torvalds /* Need a larger buffer. Query for the right size. */ 14215d6c3191SAndreas Gruenbacher rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0); 14221da177e4SLinus Torvalds if (rc < 0) { 14231da177e4SLinus Torvalds dput(dentry); 14249287aed2SAndreas Gruenbacher goto out; 14251da177e4SLinus Torvalds } 14261da177e4SLinus Torvalds len = rc; 14274cb912f1SEric Paris context = kmalloc(len+1, GFP_NOFS); 14281da177e4SLinus Torvalds if (!context) { 14291da177e4SLinus Torvalds rc = -ENOMEM; 14301da177e4SLinus Torvalds dput(dentry); 14319287aed2SAndreas Gruenbacher goto out; 14321da177e4SLinus Torvalds } 14334cb912f1SEric Paris context[len] = '\0'; 14345d6c3191SAndreas Gruenbacher rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len); 14351da177e4SLinus Torvalds } 14361da177e4SLinus Torvalds dput(dentry); 14371da177e4SLinus Torvalds if (rc < 0) { 14381da177e4SLinus Torvalds if (rc != -ENODATA) { 1439c103a91eSpeter enderborg pr_warn("SELinux: %s: getxattr returned " 1440dd6f953aSHarvey Harrison "%d for dev=%s ino=%ld\n", __func__, 14411da177e4SLinus Torvalds -rc, inode->i_sb->s_id, inode->i_ino); 14421da177e4SLinus Torvalds kfree(context); 14439287aed2SAndreas Gruenbacher goto out; 14441da177e4SLinus Torvalds } 14451da177e4SLinus Torvalds /* Map ENODATA to the default file SID */ 14461da177e4SLinus Torvalds sid = sbsec->def_sid; 14471da177e4SLinus Torvalds rc = 0; 14481da177e4SLinus Torvalds } else { 1449aa8e712cSStephen Smalley rc = security_context_to_sid_default(&selinux_state, 1450aa8e712cSStephen Smalley context, rc, &sid, 1451869ab514SStephen Smalley sbsec->def_sid, 1452869ab514SStephen Smalley GFP_NOFS); 14531da177e4SLinus Torvalds if (rc) { 14544ba0a8adSEric Paris char *dev = inode->i_sb->s_id; 14554ba0a8adSEric Paris unsigned long ino = inode->i_ino; 14564ba0a8adSEric Paris 14574ba0a8adSEric Paris if (rc == -EINVAL) { 14584ba0a8adSEric Paris if (printk_ratelimit()) 1459c103a91eSpeter enderborg pr_notice("SELinux: inode=%lu on dev=%s was found to have an invalid " 14604ba0a8adSEric Paris "context=%s. This indicates you may need to relabel the inode or the " 14614ba0a8adSEric Paris "filesystem in question.\n", ino, dev, context); 14624ba0a8adSEric Paris } else { 1463c103a91eSpeter enderborg pr_warn("SELinux: %s: context_to_sid(%s) " 14641da177e4SLinus Torvalds "returned %d for dev=%s ino=%ld\n", 14654ba0a8adSEric Paris __func__, context, -rc, dev, ino); 14664ba0a8adSEric Paris } 14671da177e4SLinus Torvalds kfree(context); 14681da177e4SLinus Torvalds /* Leave with the unlabeled SID */ 14691da177e4SLinus Torvalds rc = 0; 14701da177e4SLinus Torvalds break; 14711da177e4SLinus Torvalds } 14721da177e4SLinus Torvalds } 14731da177e4SLinus Torvalds kfree(context); 14741da177e4SLinus Torvalds break; 14751da177e4SLinus Torvalds case SECURITY_FS_USE_TASK: 14769287aed2SAndreas Gruenbacher sid = task_sid; 14771da177e4SLinus Torvalds break; 14781da177e4SLinus Torvalds case SECURITY_FS_USE_TRANS: 14791da177e4SLinus Torvalds /* Default to the fs SID. */ 14809287aed2SAndreas Gruenbacher sid = sbsec->sid; 14811da177e4SLinus Torvalds 14821da177e4SLinus Torvalds /* Try to obtain a transition SID. */ 1483aa8e712cSStephen Smalley rc = security_transition_sid(&selinux_state, task_sid, sid, 1484aa8e712cSStephen Smalley sclass, NULL, &sid); 14851da177e4SLinus Torvalds if (rc) 14869287aed2SAndreas Gruenbacher goto out; 14871da177e4SLinus Torvalds break; 1488c312feb2SEric Paris case SECURITY_FS_USE_MNTPOINT: 14899287aed2SAndreas Gruenbacher sid = sbsec->mntpoint_sid; 1490c312feb2SEric Paris break; 14911da177e4SLinus Torvalds default: 1492c312feb2SEric Paris /* Default to the fs superblock SID. */ 14939287aed2SAndreas Gruenbacher sid = sbsec->sid; 14941da177e4SLinus Torvalds 1495134509d5SStephen Smalley if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) { 1496f64410ecSPaul Moore /* We must have a dentry to determine the label on 1497f64410ecSPaul Moore * procfs inodes */ 1498b127125dSAl Viro if (opt_dentry) { 1499f64410ecSPaul Moore /* Called from d_instantiate or 1500f64410ecSPaul Moore * d_splice_alias. */ 1501f64410ecSPaul Moore dentry = dget(opt_dentry); 1502b127125dSAl Viro } else { 1503f64410ecSPaul Moore /* Called from selinux_complete_init, try to 1504b127125dSAl Viro * find a dentry. Some filesystems really want 1505b127125dSAl Viro * a connected one, so try that first. 1506b127125dSAl Viro */ 1507f64410ecSPaul Moore dentry = d_find_alias(inode); 1508b127125dSAl Viro if (!dentry) 1509b127125dSAl Viro dentry = d_find_any_alias(inode); 1510b127125dSAl Viro } 1511f64410ecSPaul Moore /* 1512f64410ecSPaul Moore * This can be hit on boot when a file is accessed 1513f64410ecSPaul Moore * before the policy is loaded. When we load policy we 1514f64410ecSPaul Moore * may find inodes that have no dentry on the 1515f64410ecSPaul Moore * sbsec->isec_head list. No reason to complain as 1516f64410ecSPaul Moore * these will get fixed up the next time we go through 1517f64410ecSPaul Moore * inode_doinit() with a dentry, before these inodes 1518f64410ecSPaul Moore * could be used again by userspace. 1519f64410ecSPaul Moore */ 1520f64410ecSPaul Moore if (!dentry) 15219287aed2SAndreas Gruenbacher goto out; 15229287aed2SAndreas Gruenbacher rc = selinux_genfs_get_sid(dentry, sclass, 1523134509d5SStephen Smalley sbsec->flags, &sid); 1524f64410ecSPaul Moore dput(dentry); 15251da177e4SLinus Torvalds if (rc) 15269287aed2SAndreas Gruenbacher goto out; 15271da177e4SLinus Torvalds } 15281da177e4SLinus Torvalds break; 15291da177e4SLinus Torvalds } 15301da177e4SLinus Torvalds 15319287aed2SAndreas Gruenbacher out: 15329287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 15339287aed2SAndreas Gruenbacher if (isec->initialized == LABEL_PENDING) { 15349287aed2SAndreas Gruenbacher if (!sid || rc) { 15359287aed2SAndreas Gruenbacher isec->initialized = LABEL_INVALID; 15369287aed2SAndreas Gruenbacher goto out_unlock; 15379287aed2SAndreas Gruenbacher } 15389287aed2SAndreas Gruenbacher 15396f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 15409287aed2SAndreas Gruenbacher isec->sid = sid; 15419287aed2SAndreas Gruenbacher } 15421da177e4SLinus Torvalds 154323970741SEric Paris out_unlock: 15449287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 15451da177e4SLinus Torvalds return rc; 15461da177e4SLinus Torvalds } 15471da177e4SLinus Torvalds 15481da177e4SLinus Torvalds /* Convert a Linux signal to an access vector. */ 15491da177e4SLinus Torvalds static inline u32 signal_to_av(int sig) 15501da177e4SLinus Torvalds { 15511da177e4SLinus Torvalds u32 perm = 0; 15521da177e4SLinus Torvalds 15531da177e4SLinus Torvalds switch (sig) { 15541da177e4SLinus Torvalds case SIGCHLD: 15551da177e4SLinus Torvalds /* Commonly granted from child to parent. */ 15561da177e4SLinus Torvalds perm = PROCESS__SIGCHLD; 15571da177e4SLinus Torvalds break; 15581da177e4SLinus Torvalds case SIGKILL: 15591da177e4SLinus Torvalds /* Cannot be caught or ignored */ 15601da177e4SLinus Torvalds perm = PROCESS__SIGKILL; 15611da177e4SLinus Torvalds break; 15621da177e4SLinus Torvalds case SIGSTOP: 15631da177e4SLinus Torvalds /* Cannot be caught or ignored */ 15641da177e4SLinus Torvalds perm = PROCESS__SIGSTOP; 15651da177e4SLinus Torvalds break; 15661da177e4SLinus Torvalds default: 15671da177e4SLinus Torvalds /* All other signals. */ 15681da177e4SLinus Torvalds perm = PROCESS__SIGNAL; 15691da177e4SLinus Torvalds break; 15701da177e4SLinus Torvalds } 15711da177e4SLinus Torvalds 15721da177e4SLinus Torvalds return perm; 15731da177e4SLinus Torvalds } 15741da177e4SLinus Torvalds 1575b68e418cSStephen Smalley #if CAP_LAST_CAP > 63 1576b68e418cSStephen Smalley #error Fix SELinux to handle capabilities > 63. 1577b68e418cSStephen Smalley #endif 1578b68e418cSStephen Smalley 15791da177e4SLinus Torvalds /* Check whether a task is allowed to use a capability. */ 15806a9de491SEric Paris static int cred_has_capability(const struct cred *cred, 15818e4ff6f2SStephen Smalley int cap, int audit, bool initns) 15821da177e4SLinus Torvalds { 15832bf49690SThomas Liu struct common_audit_data ad; 158406112163SEric Paris struct av_decision avd; 1585b68e418cSStephen Smalley u16 sclass; 15863699c53cSDavid Howells u32 sid = cred_sid(cred); 1587b68e418cSStephen Smalley u32 av = CAP_TO_MASK(cap); 158806112163SEric Paris int rc; 15891da177e4SLinus Torvalds 159050c205f5SEric Paris ad.type = LSM_AUDIT_DATA_CAP; 15911da177e4SLinus Torvalds ad.u.cap = cap; 15921da177e4SLinus Torvalds 1593b68e418cSStephen Smalley switch (CAP_TO_INDEX(cap)) { 1594b68e418cSStephen Smalley case 0: 15958e4ff6f2SStephen Smalley sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS; 1596b68e418cSStephen Smalley break; 1597b68e418cSStephen Smalley case 1: 15988e4ff6f2SStephen Smalley sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS; 1599b68e418cSStephen Smalley break; 1600b68e418cSStephen Smalley default: 1601c103a91eSpeter enderborg pr_err("SELinux: out of range capability %d\n", cap); 1602b68e418cSStephen Smalley BUG(); 1603a35c6c83SEric Paris return -EINVAL; 1604b68e418cSStephen Smalley } 160506112163SEric Paris 16066b6bc620SStephen Smalley rc = avc_has_perm_noaudit(&selinux_state, 16076b6bc620SStephen Smalley sid, sid, sclass, av, 0, &avd); 16089ade0cf4SEric Paris if (audit == SECURITY_CAP_AUDIT) { 16096b6bc620SStephen Smalley int rc2 = avc_audit(&selinux_state, 16106b6bc620SStephen Smalley sid, sid, sclass, av, &avd, rc, &ad, 0); 16119ade0cf4SEric Paris if (rc2) 16129ade0cf4SEric Paris return rc2; 16139ade0cf4SEric Paris } 161406112163SEric Paris return rc; 16151da177e4SLinus Torvalds } 16161da177e4SLinus Torvalds 16171da177e4SLinus Torvalds /* Check whether a task has a particular permission to an inode. 16181da177e4SLinus Torvalds The 'adp' parameter is optional and allows other audit 16191da177e4SLinus Torvalds data to be passed (e.g. the dentry). */ 162088e67f3bSDavid Howells static int inode_has_perm(const struct cred *cred, 16211da177e4SLinus Torvalds struct inode *inode, 16221da177e4SLinus Torvalds u32 perms, 162319e49834SLinus Torvalds struct common_audit_data *adp) 16241da177e4SLinus Torvalds { 16251da177e4SLinus Torvalds struct inode_security_struct *isec; 1626275bb41eSDavid Howells u32 sid; 16271da177e4SLinus Torvalds 1628e0e81739SDavid Howells validate_creds(cred); 1629e0e81739SDavid Howells 1630bbaca6c2SStephen Smalley if (unlikely(IS_PRIVATE(inode))) 1631bbaca6c2SStephen Smalley return 0; 1632bbaca6c2SStephen Smalley 163388e67f3bSDavid Howells sid = cred_sid(cred); 163480788c22SCasey Schaufler isec = selinux_inode(inode); 16351da177e4SLinus Torvalds 16366b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 16376b6bc620SStephen Smalley sid, isec->sid, isec->sclass, perms, adp); 16381da177e4SLinus Torvalds } 16391da177e4SLinus Torvalds 16401da177e4SLinus Torvalds /* Same as inode_has_perm, but pass explicit audit data containing 16411da177e4SLinus Torvalds the dentry to help the auditing code to more easily generate the 16421da177e4SLinus Torvalds pathname if needed. */ 164388e67f3bSDavid Howells static inline int dentry_has_perm(const struct cred *cred, 16441da177e4SLinus Torvalds struct dentry *dentry, 16451da177e4SLinus Torvalds u32 av) 16461da177e4SLinus Torvalds { 1647c6f493d6SDavid Howells struct inode *inode = d_backing_inode(dentry); 16482bf49690SThomas Liu struct common_audit_data ad; 164988e67f3bSDavid Howells 165050c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 16512875fa00SEric Paris ad.u.dentry = dentry; 16525d226df4SAndreas Gruenbacher __inode_security_revalidate(inode, dentry, true); 165319e49834SLinus Torvalds return inode_has_perm(cred, inode, av, &ad); 16542875fa00SEric Paris } 16552875fa00SEric Paris 16562875fa00SEric Paris /* Same as inode_has_perm, but pass explicit audit data containing 16572875fa00SEric Paris the path to help the auditing code to more easily generate the 16582875fa00SEric Paris pathname if needed. */ 16592875fa00SEric Paris static inline int path_has_perm(const struct cred *cred, 16603f7036a0SAl Viro const struct path *path, 16612875fa00SEric Paris u32 av) 16622875fa00SEric Paris { 1663c6f493d6SDavid Howells struct inode *inode = d_backing_inode(path->dentry); 16642875fa00SEric Paris struct common_audit_data ad; 16652875fa00SEric Paris 166650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_PATH; 16672875fa00SEric Paris ad.u.path = *path; 16685d226df4SAndreas Gruenbacher __inode_security_revalidate(inode, path->dentry, true); 166919e49834SLinus Torvalds return inode_has_perm(cred, inode, av, &ad); 16701da177e4SLinus Torvalds } 16711da177e4SLinus Torvalds 167213f8e981SDavid Howells /* Same as path_has_perm, but uses the inode from the file struct. */ 167313f8e981SDavid Howells static inline int file_path_has_perm(const struct cred *cred, 167413f8e981SDavid Howells struct file *file, 167513f8e981SDavid Howells u32 av) 167613f8e981SDavid Howells { 167713f8e981SDavid Howells struct common_audit_data ad; 167813f8e981SDavid Howells 167943af5de7SVivek Goyal ad.type = LSM_AUDIT_DATA_FILE; 168043af5de7SVivek Goyal ad.u.file = file; 168119e49834SLinus Torvalds return inode_has_perm(cred, file_inode(file), av, &ad); 168213f8e981SDavid Howells } 168313f8e981SDavid Howells 1684f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL 1685f66e448cSChenbo Feng static int bpf_fd_pass(struct file *file, u32 sid); 1686f66e448cSChenbo Feng #endif 1687f66e448cSChenbo Feng 16881da177e4SLinus Torvalds /* Check whether a task can use an open file descriptor to 16891da177e4SLinus Torvalds access an inode in a given way. Check access to the 16901da177e4SLinus Torvalds descriptor itself, and then use dentry_has_perm to 16911da177e4SLinus Torvalds check a particular permission to the file. 16921da177e4SLinus Torvalds Access to the descriptor is implicitly granted if it 16931da177e4SLinus Torvalds has the same SID as the process. If av is zero, then 16941da177e4SLinus Torvalds access to the file is not checked, e.g. for cases 16951da177e4SLinus Torvalds where only the descriptor is affected like seek. */ 169688e67f3bSDavid Howells static int file_has_perm(const struct cred *cred, 16971da177e4SLinus Torvalds struct file *file, 16981da177e4SLinus Torvalds u32 av) 16991da177e4SLinus Torvalds { 1700bb6c6b02SCasey Schaufler struct file_security_struct *fsec = selinux_file(file); 1701496ad9aaSAl Viro struct inode *inode = file_inode(file); 17022bf49690SThomas Liu struct common_audit_data ad; 170388e67f3bSDavid Howells u32 sid = cred_sid(cred); 17041da177e4SLinus Torvalds int rc; 17051da177e4SLinus Torvalds 170643af5de7SVivek Goyal ad.type = LSM_AUDIT_DATA_FILE; 170743af5de7SVivek Goyal ad.u.file = file; 17081da177e4SLinus Torvalds 1709275bb41eSDavid Howells if (sid != fsec->sid) { 17106b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 17116b6bc620SStephen Smalley sid, fsec->sid, 17121da177e4SLinus Torvalds SECCLASS_FD, 17131da177e4SLinus Torvalds FD__USE, 17141da177e4SLinus Torvalds &ad); 17151da177e4SLinus Torvalds if (rc) 171688e67f3bSDavid Howells goto out; 17171da177e4SLinus Torvalds } 17181da177e4SLinus Torvalds 1719f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL 1720f66e448cSChenbo Feng rc = bpf_fd_pass(file, cred_sid(cred)); 1721f66e448cSChenbo Feng if (rc) 1722f66e448cSChenbo Feng return rc; 1723f66e448cSChenbo Feng #endif 1724f66e448cSChenbo Feng 17251da177e4SLinus Torvalds /* av is zero if only checking access to the descriptor. */ 172688e67f3bSDavid Howells rc = 0; 17271da177e4SLinus Torvalds if (av) 172819e49834SLinus Torvalds rc = inode_has_perm(cred, inode, av, &ad); 17291da177e4SLinus Torvalds 173088e67f3bSDavid Howells out: 173188e67f3bSDavid Howells return rc; 17321da177e4SLinus Torvalds } 17331da177e4SLinus Torvalds 1734c3c188b2SDavid Howells /* 1735c3c188b2SDavid Howells * Determine the label for an inode that might be unioned. 1736c3c188b2SDavid Howells */ 1737c957f6dfSVivek Goyal static int 1738c957f6dfSVivek Goyal selinux_determine_inode_label(const struct task_security_struct *tsec, 1739c957f6dfSVivek Goyal struct inode *dir, 1740c957f6dfSVivek Goyal const struct qstr *name, u16 tclass, 1741c3c188b2SDavid Howells u32 *_new_isid) 1742c3c188b2SDavid Howells { 1743c3c188b2SDavid Howells const struct superblock_security_struct *sbsec = dir->i_sb->s_security; 1744c3c188b2SDavid Howells 1745c3c188b2SDavid Howells if ((sbsec->flags & SE_SBINITIALIZED) && 1746c3c188b2SDavid Howells (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) { 1747c3c188b2SDavid Howells *_new_isid = sbsec->mntpoint_sid; 1748c3c188b2SDavid Howells } else if ((sbsec->flags & SBLABEL_MNT) && 1749c3c188b2SDavid Howells tsec->create_sid) { 1750c3c188b2SDavid Howells *_new_isid = tsec->create_sid; 1751c3c188b2SDavid Howells } else { 175220cdef8dSPaul Moore const struct inode_security_struct *dsec = inode_security(dir); 1753aa8e712cSStephen Smalley return security_transition_sid(&selinux_state, tsec->sid, 1754aa8e712cSStephen Smalley dsec->sid, tclass, 1755c3c188b2SDavid Howells name, _new_isid); 1756c3c188b2SDavid Howells } 1757c3c188b2SDavid Howells 1758c3c188b2SDavid Howells return 0; 1759c3c188b2SDavid Howells } 1760c3c188b2SDavid Howells 17611da177e4SLinus Torvalds /* Check whether a task can create a file. */ 17621da177e4SLinus Torvalds static int may_create(struct inode *dir, 17631da177e4SLinus Torvalds struct dentry *dentry, 17641da177e4SLinus Torvalds u16 tclass) 17651da177e4SLinus Torvalds { 17660c6cfa62SCasey Schaufler const struct task_security_struct *tsec = selinux_cred(current_cred()); 17671da177e4SLinus Torvalds struct inode_security_struct *dsec; 17681da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 1769275bb41eSDavid Howells u32 sid, newsid; 17702bf49690SThomas Liu struct common_audit_data ad; 17711da177e4SLinus Torvalds int rc; 17721da177e4SLinus Torvalds 177383da53c5SAndreas Gruenbacher dsec = inode_security(dir); 17741da177e4SLinus Torvalds sbsec = dir->i_sb->s_security; 17751da177e4SLinus Torvalds 1776275bb41eSDavid Howells sid = tsec->sid; 1777275bb41eSDavid Howells 177850c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 1779a269434dSEric Paris ad.u.dentry = dentry; 17801da177e4SLinus Torvalds 17816b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 17826b6bc620SStephen Smalley sid, dsec->sid, SECCLASS_DIR, 17831da177e4SLinus Torvalds DIR__ADD_NAME | DIR__SEARCH, 17841da177e4SLinus Torvalds &ad); 17851da177e4SLinus Torvalds if (rc) 17861da177e4SLinus Torvalds return rc; 17871da177e4SLinus Torvalds 17880c6cfa62SCasey Schaufler rc = selinux_determine_inode_label(selinux_cred(current_cred()), dir, 1789c957f6dfSVivek Goyal &dentry->d_name, tclass, &newsid); 17901da177e4SLinus Torvalds if (rc) 17911da177e4SLinus Torvalds return rc; 17921da177e4SLinus Torvalds 17936b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 17946b6bc620SStephen Smalley sid, newsid, tclass, FILE__CREATE, &ad); 17951da177e4SLinus Torvalds if (rc) 17961da177e4SLinus Torvalds return rc; 17971da177e4SLinus Torvalds 17986b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 17996b6bc620SStephen Smalley newsid, sbsec->sid, 18001da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 18011da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, &ad); 18021da177e4SLinus Torvalds } 18031da177e4SLinus Torvalds 18041da177e4SLinus Torvalds #define MAY_LINK 0 18051da177e4SLinus Torvalds #define MAY_UNLINK 1 18061da177e4SLinus Torvalds #define MAY_RMDIR 2 18071da177e4SLinus Torvalds 18081da177e4SLinus Torvalds /* Check whether a task can link, unlink, or rmdir a file/directory. */ 18091da177e4SLinus Torvalds static int may_link(struct inode *dir, 18101da177e4SLinus Torvalds struct dentry *dentry, 18111da177e4SLinus Torvalds int kind) 18121da177e4SLinus Torvalds 18131da177e4SLinus Torvalds { 18141da177e4SLinus Torvalds struct inode_security_struct *dsec, *isec; 18152bf49690SThomas Liu struct common_audit_data ad; 1816275bb41eSDavid Howells u32 sid = current_sid(); 18171da177e4SLinus Torvalds u32 av; 18181da177e4SLinus Torvalds int rc; 18191da177e4SLinus Torvalds 182083da53c5SAndreas Gruenbacher dsec = inode_security(dir); 182183da53c5SAndreas Gruenbacher isec = backing_inode_security(dentry); 18221da177e4SLinus Torvalds 182350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 1824a269434dSEric Paris ad.u.dentry = dentry; 18251da177e4SLinus Torvalds 18261da177e4SLinus Torvalds av = DIR__SEARCH; 18271da177e4SLinus Torvalds av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME); 18286b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 18296b6bc620SStephen Smalley sid, dsec->sid, SECCLASS_DIR, av, &ad); 18301da177e4SLinus Torvalds if (rc) 18311da177e4SLinus Torvalds return rc; 18321da177e4SLinus Torvalds 18331da177e4SLinus Torvalds switch (kind) { 18341da177e4SLinus Torvalds case MAY_LINK: 18351da177e4SLinus Torvalds av = FILE__LINK; 18361da177e4SLinus Torvalds break; 18371da177e4SLinus Torvalds case MAY_UNLINK: 18381da177e4SLinus Torvalds av = FILE__UNLINK; 18391da177e4SLinus Torvalds break; 18401da177e4SLinus Torvalds case MAY_RMDIR: 18411da177e4SLinus Torvalds av = DIR__RMDIR; 18421da177e4SLinus Torvalds break; 18431da177e4SLinus Torvalds default: 1844c103a91eSpeter enderborg pr_warn("SELinux: %s: unrecognized kind %d\n", 1845744ba35eSEric Paris __func__, kind); 18461da177e4SLinus Torvalds return 0; 18471da177e4SLinus Torvalds } 18481da177e4SLinus Torvalds 18496b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 18506b6bc620SStephen Smalley sid, isec->sid, isec->sclass, av, &ad); 18511da177e4SLinus Torvalds return rc; 18521da177e4SLinus Torvalds } 18531da177e4SLinus Torvalds 18541da177e4SLinus Torvalds static inline int may_rename(struct inode *old_dir, 18551da177e4SLinus Torvalds struct dentry *old_dentry, 18561da177e4SLinus Torvalds struct inode *new_dir, 18571da177e4SLinus Torvalds struct dentry *new_dentry) 18581da177e4SLinus Torvalds { 18591da177e4SLinus Torvalds struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec; 18602bf49690SThomas Liu struct common_audit_data ad; 1861275bb41eSDavid Howells u32 sid = current_sid(); 18621da177e4SLinus Torvalds u32 av; 18631da177e4SLinus Torvalds int old_is_dir, new_is_dir; 18641da177e4SLinus Torvalds int rc; 18651da177e4SLinus Torvalds 186683da53c5SAndreas Gruenbacher old_dsec = inode_security(old_dir); 186783da53c5SAndreas Gruenbacher old_isec = backing_inode_security(old_dentry); 1868e36cb0b8SDavid Howells old_is_dir = d_is_dir(old_dentry); 186983da53c5SAndreas Gruenbacher new_dsec = inode_security(new_dir); 18701da177e4SLinus Torvalds 187150c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 18721da177e4SLinus Torvalds 1873a269434dSEric Paris ad.u.dentry = old_dentry; 18746b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 18756b6bc620SStephen Smalley sid, old_dsec->sid, SECCLASS_DIR, 18761da177e4SLinus Torvalds DIR__REMOVE_NAME | DIR__SEARCH, &ad); 18771da177e4SLinus Torvalds if (rc) 18781da177e4SLinus Torvalds return rc; 18796b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 18806b6bc620SStephen Smalley sid, old_isec->sid, 18811da177e4SLinus Torvalds old_isec->sclass, FILE__RENAME, &ad); 18821da177e4SLinus Torvalds if (rc) 18831da177e4SLinus Torvalds return rc; 18841da177e4SLinus Torvalds if (old_is_dir && new_dir != old_dir) { 18856b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 18866b6bc620SStephen Smalley sid, old_isec->sid, 18871da177e4SLinus Torvalds old_isec->sclass, DIR__REPARENT, &ad); 18881da177e4SLinus Torvalds if (rc) 18891da177e4SLinus Torvalds return rc; 18901da177e4SLinus Torvalds } 18911da177e4SLinus Torvalds 1892a269434dSEric Paris ad.u.dentry = new_dentry; 18931da177e4SLinus Torvalds av = DIR__ADD_NAME | DIR__SEARCH; 18942c616d4dSDavid Howells if (d_is_positive(new_dentry)) 18951da177e4SLinus Torvalds av |= DIR__REMOVE_NAME; 18966b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 18976b6bc620SStephen Smalley sid, new_dsec->sid, SECCLASS_DIR, av, &ad); 18981da177e4SLinus Torvalds if (rc) 18991da177e4SLinus Torvalds return rc; 19002c616d4dSDavid Howells if (d_is_positive(new_dentry)) { 190183da53c5SAndreas Gruenbacher new_isec = backing_inode_security(new_dentry); 1902e36cb0b8SDavid Howells new_is_dir = d_is_dir(new_dentry); 19036b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 19046b6bc620SStephen Smalley sid, new_isec->sid, 19051da177e4SLinus Torvalds new_isec->sclass, 19061da177e4SLinus Torvalds (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad); 19071da177e4SLinus Torvalds if (rc) 19081da177e4SLinus Torvalds return rc; 19091da177e4SLinus Torvalds } 19101da177e4SLinus Torvalds 19111da177e4SLinus Torvalds return 0; 19121da177e4SLinus Torvalds } 19131da177e4SLinus Torvalds 19141da177e4SLinus Torvalds /* Check whether a task can perform a filesystem operation. */ 191588e67f3bSDavid Howells static int superblock_has_perm(const struct cred *cred, 19161da177e4SLinus Torvalds struct super_block *sb, 19171da177e4SLinus Torvalds u32 perms, 19182bf49690SThomas Liu struct common_audit_data *ad) 19191da177e4SLinus Torvalds { 19201da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 192188e67f3bSDavid Howells u32 sid = cred_sid(cred); 19221da177e4SLinus Torvalds 19231da177e4SLinus Torvalds sbsec = sb->s_security; 19246b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 19256b6bc620SStephen Smalley sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad); 19261da177e4SLinus Torvalds } 19271da177e4SLinus Torvalds 19281da177e4SLinus Torvalds /* Convert a Linux mode and permission mask to an access vector. */ 19291da177e4SLinus Torvalds static inline u32 file_mask_to_av(int mode, int mask) 19301da177e4SLinus Torvalds { 19311da177e4SLinus Torvalds u32 av = 0; 19321da177e4SLinus Torvalds 1933dba19c60SAl Viro if (!S_ISDIR(mode)) { 19341da177e4SLinus Torvalds if (mask & MAY_EXEC) 19351da177e4SLinus Torvalds av |= FILE__EXECUTE; 19361da177e4SLinus Torvalds if (mask & MAY_READ) 19371da177e4SLinus Torvalds av |= FILE__READ; 19381da177e4SLinus Torvalds 19391da177e4SLinus Torvalds if (mask & MAY_APPEND) 19401da177e4SLinus Torvalds av |= FILE__APPEND; 19411da177e4SLinus Torvalds else if (mask & MAY_WRITE) 19421da177e4SLinus Torvalds av |= FILE__WRITE; 19431da177e4SLinus Torvalds 19441da177e4SLinus Torvalds } else { 19451da177e4SLinus Torvalds if (mask & MAY_EXEC) 19461da177e4SLinus Torvalds av |= DIR__SEARCH; 19471da177e4SLinus Torvalds if (mask & MAY_WRITE) 19481da177e4SLinus Torvalds av |= DIR__WRITE; 19491da177e4SLinus Torvalds if (mask & MAY_READ) 19501da177e4SLinus Torvalds av |= DIR__READ; 19511da177e4SLinus Torvalds } 19521da177e4SLinus Torvalds 19531da177e4SLinus Torvalds return av; 19541da177e4SLinus Torvalds } 19551da177e4SLinus Torvalds 19561da177e4SLinus Torvalds /* Convert a Linux file to an access vector. */ 19571da177e4SLinus Torvalds static inline u32 file_to_av(struct file *file) 19581da177e4SLinus Torvalds { 19591da177e4SLinus Torvalds u32 av = 0; 19601da177e4SLinus Torvalds 19611da177e4SLinus Torvalds if (file->f_mode & FMODE_READ) 19621da177e4SLinus Torvalds av |= FILE__READ; 19631da177e4SLinus Torvalds if (file->f_mode & FMODE_WRITE) { 19641da177e4SLinus Torvalds if (file->f_flags & O_APPEND) 19651da177e4SLinus Torvalds av |= FILE__APPEND; 19661da177e4SLinus Torvalds else 19671da177e4SLinus Torvalds av |= FILE__WRITE; 19681da177e4SLinus Torvalds } 19690794c66dSStephen Smalley if (!av) { 19700794c66dSStephen Smalley /* 19710794c66dSStephen Smalley * Special file opened with flags 3 for ioctl-only use. 19720794c66dSStephen Smalley */ 19730794c66dSStephen Smalley av = FILE__IOCTL; 19740794c66dSStephen Smalley } 19751da177e4SLinus Torvalds 19761da177e4SLinus Torvalds return av; 19771da177e4SLinus Torvalds } 19781da177e4SLinus Torvalds 19798b6a5a37SEric Paris /* 19808b6a5a37SEric Paris * Convert a file to an access vector and include the correct open 19818b6a5a37SEric Paris * open permission. 19828b6a5a37SEric Paris */ 19838b6a5a37SEric Paris static inline u32 open_file_to_av(struct file *file) 19848b6a5a37SEric Paris { 19858b6a5a37SEric Paris u32 av = file_to_av(file); 1986ccb54478SStephen Smalley struct inode *inode = file_inode(file); 19878b6a5a37SEric Paris 1988aa8e712cSStephen Smalley if (selinux_policycap_openperm() && 1989aa8e712cSStephen Smalley inode->i_sb->s_magic != SOCKFS_MAGIC) 19908b6a5a37SEric Paris av |= FILE__OPEN; 199149b7b8deSEric Paris 19928b6a5a37SEric Paris return av; 19938b6a5a37SEric Paris } 19948b6a5a37SEric Paris 19951da177e4SLinus Torvalds /* Hook functions begin here. */ 19961da177e4SLinus Torvalds 199779af7307SStephen Smalley static int selinux_binder_set_context_mgr(struct task_struct *mgr) 199879af7307SStephen Smalley { 199979af7307SStephen Smalley u32 mysid = current_sid(); 200079af7307SStephen Smalley u32 mgrsid = task_sid(mgr); 200179af7307SStephen Smalley 20026b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 20036b6bc620SStephen Smalley mysid, mgrsid, SECCLASS_BINDER, 200479af7307SStephen Smalley BINDER__SET_CONTEXT_MGR, NULL); 200579af7307SStephen Smalley } 200679af7307SStephen Smalley 200779af7307SStephen Smalley static int selinux_binder_transaction(struct task_struct *from, 200879af7307SStephen Smalley struct task_struct *to) 200979af7307SStephen Smalley { 201079af7307SStephen Smalley u32 mysid = current_sid(); 201179af7307SStephen Smalley u32 fromsid = task_sid(from); 201279af7307SStephen Smalley u32 tosid = task_sid(to); 201379af7307SStephen Smalley int rc; 201479af7307SStephen Smalley 201579af7307SStephen Smalley if (mysid != fromsid) { 20166b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 20176b6bc620SStephen Smalley mysid, fromsid, SECCLASS_BINDER, 201879af7307SStephen Smalley BINDER__IMPERSONATE, NULL); 201979af7307SStephen Smalley if (rc) 202079af7307SStephen Smalley return rc; 202179af7307SStephen Smalley } 202279af7307SStephen Smalley 20236b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 20246b6bc620SStephen Smalley fromsid, tosid, SECCLASS_BINDER, BINDER__CALL, 202579af7307SStephen Smalley NULL); 202679af7307SStephen Smalley } 202779af7307SStephen Smalley 202879af7307SStephen Smalley static int selinux_binder_transfer_binder(struct task_struct *from, 202979af7307SStephen Smalley struct task_struct *to) 203079af7307SStephen Smalley { 203179af7307SStephen Smalley u32 fromsid = task_sid(from); 203279af7307SStephen Smalley u32 tosid = task_sid(to); 203379af7307SStephen Smalley 20346b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 20356b6bc620SStephen Smalley fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER, 203679af7307SStephen Smalley NULL); 203779af7307SStephen Smalley } 203879af7307SStephen Smalley 203979af7307SStephen Smalley static int selinux_binder_transfer_file(struct task_struct *from, 204079af7307SStephen Smalley struct task_struct *to, 204179af7307SStephen Smalley struct file *file) 204279af7307SStephen Smalley { 204379af7307SStephen Smalley u32 sid = task_sid(to); 2044bb6c6b02SCasey Schaufler struct file_security_struct *fsec = selinux_file(file); 204583da53c5SAndreas Gruenbacher struct dentry *dentry = file->f_path.dentry; 204620cdef8dSPaul Moore struct inode_security_struct *isec; 204779af7307SStephen Smalley struct common_audit_data ad; 204879af7307SStephen Smalley int rc; 204979af7307SStephen Smalley 205079af7307SStephen Smalley ad.type = LSM_AUDIT_DATA_PATH; 205179af7307SStephen Smalley ad.u.path = file->f_path; 205279af7307SStephen Smalley 205379af7307SStephen Smalley if (sid != fsec->sid) { 20546b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 20556b6bc620SStephen Smalley sid, fsec->sid, 205679af7307SStephen Smalley SECCLASS_FD, 205779af7307SStephen Smalley FD__USE, 205879af7307SStephen Smalley &ad); 205979af7307SStephen Smalley if (rc) 206079af7307SStephen Smalley return rc; 206179af7307SStephen Smalley } 206279af7307SStephen Smalley 2063f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL 2064f66e448cSChenbo Feng rc = bpf_fd_pass(file, sid); 2065f66e448cSChenbo Feng if (rc) 2066f66e448cSChenbo Feng return rc; 2067f66e448cSChenbo Feng #endif 2068f66e448cSChenbo Feng 206983da53c5SAndreas Gruenbacher if (unlikely(IS_PRIVATE(d_backing_inode(dentry)))) 207079af7307SStephen Smalley return 0; 207179af7307SStephen Smalley 207220cdef8dSPaul Moore isec = backing_inode_security(dentry); 20736b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 20746b6bc620SStephen Smalley sid, isec->sid, isec->sclass, file_to_av(file), 207579af7307SStephen Smalley &ad); 207679af7307SStephen Smalley } 207779af7307SStephen Smalley 20789e48858fSIngo Molnar static int selinux_ptrace_access_check(struct task_struct *child, 2079006ebb40SStephen Smalley unsigned int mode) 20801da177e4SLinus Torvalds { 2081275bb41eSDavid Howells u32 sid = current_sid(); 2082275bb41eSDavid Howells u32 csid = task_sid(child); 2083006ebb40SStephen Smalley 2084be0554c9SStephen Smalley if (mode & PTRACE_MODE_READ) 20856b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 20866b6bc620SStephen Smalley sid, csid, SECCLASS_FILE, FILE__READ, NULL); 2087be0554c9SStephen Smalley 20886b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 20896b6bc620SStephen Smalley sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL); 20905cd9c58fSDavid Howells } 20915cd9c58fSDavid Howells 20925cd9c58fSDavid Howells static int selinux_ptrace_traceme(struct task_struct *parent) 20935cd9c58fSDavid Howells { 20946b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 20956b6bc620SStephen Smalley task_sid(parent), current_sid(), SECCLASS_PROCESS, 2096be0554c9SStephen Smalley PROCESS__PTRACE, NULL); 20971da177e4SLinus Torvalds } 20981da177e4SLinus Torvalds 20991da177e4SLinus Torvalds static int selinux_capget(struct task_struct *target, kernel_cap_t *effective, 21001da177e4SLinus Torvalds kernel_cap_t *inheritable, kernel_cap_t *permitted) 21011da177e4SLinus Torvalds { 21026b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 21036b6bc620SStephen Smalley current_sid(), task_sid(target), SECCLASS_PROCESS, 2104be0554c9SStephen Smalley PROCESS__GETCAP, NULL); 21051da177e4SLinus Torvalds } 21061da177e4SLinus Torvalds 2107d84f4f99SDavid Howells static int selinux_capset(struct cred *new, const struct cred *old, 2108d84f4f99SDavid Howells const kernel_cap_t *effective, 210915a2460eSDavid Howells const kernel_cap_t *inheritable, 211015a2460eSDavid Howells const kernel_cap_t *permitted) 21111da177e4SLinus Torvalds { 21126b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 21136b6bc620SStephen Smalley cred_sid(old), cred_sid(new), SECCLASS_PROCESS, 2114be0554c9SStephen Smalley PROCESS__SETCAP, NULL); 21151da177e4SLinus Torvalds } 21161da177e4SLinus Torvalds 21175626d3e8SJames Morris /* 21185626d3e8SJames Morris * (This comment used to live with the selinux_task_setuid hook, 21195626d3e8SJames Morris * which was removed). 21205626d3e8SJames Morris * 21215626d3e8SJames Morris * Since setuid only affects the current process, and since the SELinux 21225626d3e8SJames Morris * controls are not based on the Linux identity attributes, SELinux does not 21235626d3e8SJames Morris * need to control this operation. However, SELinux does control the use of 21245626d3e8SJames Morris * the CAP_SETUID and CAP_SETGID capabilities using the capable hook. 21255626d3e8SJames Morris */ 21265626d3e8SJames Morris 21276a9de491SEric Paris static int selinux_capable(const struct cred *cred, struct user_namespace *ns, 21286a9de491SEric Paris int cap, int audit) 21291da177e4SLinus Torvalds { 21308e4ff6f2SStephen Smalley return cred_has_capability(cred, cap, audit, ns == &init_user_ns); 21311da177e4SLinus Torvalds } 21321da177e4SLinus Torvalds 21331da177e4SLinus Torvalds static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb) 21341da177e4SLinus Torvalds { 213588e67f3bSDavid Howells const struct cred *cred = current_cred(); 21361da177e4SLinus Torvalds int rc = 0; 21371da177e4SLinus Torvalds 21381da177e4SLinus Torvalds if (!sb) 21391da177e4SLinus Torvalds return 0; 21401da177e4SLinus Torvalds 21411da177e4SLinus Torvalds switch (cmds) { 21421da177e4SLinus Torvalds case Q_SYNC: 21431da177e4SLinus Torvalds case Q_QUOTAON: 21441da177e4SLinus Torvalds case Q_QUOTAOFF: 21451da177e4SLinus Torvalds case Q_SETINFO: 21461da177e4SLinus Torvalds case Q_SETQUOTA: 214788e67f3bSDavid Howells rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL); 21481da177e4SLinus Torvalds break; 21491da177e4SLinus Torvalds case Q_GETFMT: 21501da177e4SLinus Torvalds case Q_GETINFO: 21511da177e4SLinus Torvalds case Q_GETQUOTA: 215288e67f3bSDavid Howells rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL); 21531da177e4SLinus Torvalds break; 21541da177e4SLinus Torvalds default: 21551da177e4SLinus Torvalds rc = 0; /* let the kernel handle invalid cmds */ 21561da177e4SLinus Torvalds break; 21571da177e4SLinus Torvalds } 21581da177e4SLinus Torvalds return rc; 21591da177e4SLinus Torvalds } 21601da177e4SLinus Torvalds 21611da177e4SLinus Torvalds static int selinux_quota_on(struct dentry *dentry) 21621da177e4SLinus Torvalds { 216388e67f3bSDavid Howells const struct cred *cred = current_cred(); 216488e67f3bSDavid Howells 21652875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__QUOTAON); 21661da177e4SLinus Torvalds } 21671da177e4SLinus Torvalds 216812b3052cSEric Paris static int selinux_syslog(int type) 21691da177e4SLinus Torvalds { 21701da177e4SLinus Torvalds switch (type) { 2171d78ca3cdSKees Cook case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */ 2172d78ca3cdSKees Cook case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */ 21736b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 21746b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, 2175be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL); 2176d78ca3cdSKees Cook case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */ 2177d78ca3cdSKees Cook case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */ 2178d78ca3cdSKees Cook /* Set level of messages printed to console */ 2179d78ca3cdSKees Cook case SYSLOG_ACTION_CONSOLE_LEVEL: 21806b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 21816b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, 2182be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE, 2183be0554c9SStephen Smalley NULL); 21841da177e4SLinus Torvalds } 2185be0554c9SStephen Smalley /* All other syslog types */ 21866b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 21876b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, 2188be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL); 21891da177e4SLinus Torvalds } 21901da177e4SLinus Torvalds 21911da177e4SLinus Torvalds /* 21921da177e4SLinus Torvalds * Check that a process has enough memory to allocate a new virtual 21931da177e4SLinus Torvalds * mapping. 0 means there is enough memory for the allocation to 21941da177e4SLinus Torvalds * succeed and -ENOMEM implies there is not. 21951da177e4SLinus Torvalds * 21961da177e4SLinus Torvalds * Do not audit the selinux permission check, as this is applied to all 21971da177e4SLinus Torvalds * processes that allocate mappings. 21981da177e4SLinus Torvalds */ 219934b4e4aaSAlan Cox static int selinux_vm_enough_memory(struct mm_struct *mm, long pages) 22001da177e4SLinus Torvalds { 22011da177e4SLinus Torvalds int rc, cap_sys_admin = 0; 22021da177e4SLinus Torvalds 2203b1d9e6b0SCasey Schaufler rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN, 22048e4ff6f2SStephen Smalley SECURITY_CAP_NOAUDIT, true); 22051da177e4SLinus Torvalds if (rc == 0) 22061da177e4SLinus Torvalds cap_sys_admin = 1; 22071da177e4SLinus Torvalds 2208b1d9e6b0SCasey Schaufler return cap_sys_admin; 22091da177e4SLinus Torvalds } 22101da177e4SLinus Torvalds 22111da177e4SLinus Torvalds /* binprm security operations */ 22121da177e4SLinus Torvalds 2213be0554c9SStephen Smalley static u32 ptrace_parent_sid(void) 22140c6181cbSPaul Moore { 22150c6181cbSPaul Moore u32 sid = 0; 22160c6181cbSPaul Moore struct task_struct *tracer; 22170c6181cbSPaul Moore 22180c6181cbSPaul Moore rcu_read_lock(); 2219be0554c9SStephen Smalley tracer = ptrace_parent(current); 22200c6181cbSPaul Moore if (tracer) 22210c6181cbSPaul Moore sid = task_sid(tracer); 22220c6181cbSPaul Moore rcu_read_unlock(); 22230c6181cbSPaul Moore 22240c6181cbSPaul Moore return sid; 22250c6181cbSPaul Moore } 22260c6181cbSPaul Moore 22277b0d0b40SStephen Smalley static int check_nnp_nosuid(const struct linux_binprm *bprm, 22287b0d0b40SStephen Smalley const struct task_security_struct *old_tsec, 22297b0d0b40SStephen Smalley const struct task_security_struct *new_tsec) 22307b0d0b40SStephen Smalley { 22317b0d0b40SStephen Smalley int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS); 2232380cf5baSAndy Lutomirski int nosuid = !mnt_may_suid(bprm->file->f_path.mnt); 22337b0d0b40SStephen Smalley int rc; 2234af63f419SStephen Smalley u32 av; 22357b0d0b40SStephen Smalley 22367b0d0b40SStephen Smalley if (!nnp && !nosuid) 22377b0d0b40SStephen Smalley return 0; /* neither NNP nor nosuid */ 22387b0d0b40SStephen Smalley 22397b0d0b40SStephen Smalley if (new_tsec->sid == old_tsec->sid) 22407b0d0b40SStephen Smalley return 0; /* No change in credentials */ 22417b0d0b40SStephen Smalley 22427b0d0b40SStephen Smalley /* 2243af63f419SStephen Smalley * If the policy enables the nnp_nosuid_transition policy capability, 2244af63f419SStephen Smalley * then we permit transitions under NNP or nosuid if the 2245af63f419SStephen Smalley * policy allows the corresponding permission between 2246af63f419SStephen Smalley * the old and new contexts. 2247af63f419SStephen Smalley */ 2248aa8e712cSStephen Smalley if (selinux_policycap_nnp_nosuid_transition()) { 2249af63f419SStephen Smalley av = 0; 2250af63f419SStephen Smalley if (nnp) 2251af63f419SStephen Smalley av |= PROCESS2__NNP_TRANSITION; 2252af63f419SStephen Smalley if (nosuid) 2253af63f419SStephen Smalley av |= PROCESS2__NOSUID_TRANSITION; 22546b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 22556b6bc620SStephen Smalley old_tsec->sid, new_tsec->sid, 2256af63f419SStephen Smalley SECCLASS_PROCESS2, av, NULL); 2257af63f419SStephen Smalley if (!rc) 2258af63f419SStephen Smalley return 0; 2259af63f419SStephen Smalley } 2260af63f419SStephen Smalley 2261af63f419SStephen Smalley /* 2262af63f419SStephen Smalley * We also permit NNP or nosuid transitions to bounded SIDs, 2263af63f419SStephen Smalley * i.e. SIDs that are guaranteed to only be allowed a subset 2264af63f419SStephen Smalley * of the permissions of the current SID. 22657b0d0b40SStephen Smalley */ 2266aa8e712cSStephen Smalley rc = security_bounded_transition(&selinux_state, old_tsec->sid, 2267aa8e712cSStephen Smalley new_tsec->sid); 2268af63f419SStephen Smalley if (!rc) 2269af63f419SStephen Smalley return 0; 2270af63f419SStephen Smalley 22717b0d0b40SStephen Smalley /* 22727b0d0b40SStephen Smalley * On failure, preserve the errno values for NNP vs nosuid. 22737b0d0b40SStephen Smalley * NNP: Operation not permitted for caller. 22747b0d0b40SStephen Smalley * nosuid: Permission denied to file. 22757b0d0b40SStephen Smalley */ 22767b0d0b40SStephen Smalley if (nnp) 22777b0d0b40SStephen Smalley return -EPERM; 22787b0d0b40SStephen Smalley return -EACCES; 22797b0d0b40SStephen Smalley } 22807b0d0b40SStephen Smalley 2281a6f76f23SDavid Howells static int selinux_bprm_set_creds(struct linux_binprm *bprm) 22821da177e4SLinus Torvalds { 2283a6f76f23SDavid Howells const struct task_security_struct *old_tsec; 2284a6f76f23SDavid Howells struct task_security_struct *new_tsec; 22851da177e4SLinus Torvalds struct inode_security_struct *isec; 22862bf49690SThomas Liu struct common_audit_data ad; 2287496ad9aaSAl Viro struct inode *inode = file_inode(bprm->file); 22881da177e4SLinus Torvalds int rc; 22891da177e4SLinus Torvalds 2290a6f76f23SDavid Howells /* SELinux context only depends on initial program or script and not 2291a6f76f23SDavid Howells * the script interpreter */ 2292ddb4a144SKees Cook if (bprm->called_set_creds) 22931da177e4SLinus Torvalds return 0; 22941da177e4SLinus Torvalds 22950c6cfa62SCasey Schaufler old_tsec = selinux_cred(current_cred()); 22960c6cfa62SCasey Schaufler new_tsec = selinux_cred(bprm->cred); 229783da53c5SAndreas Gruenbacher isec = inode_security(inode); 22981da177e4SLinus Torvalds 22991da177e4SLinus Torvalds /* Default to the current task SID. */ 2300a6f76f23SDavid Howells new_tsec->sid = old_tsec->sid; 2301a6f76f23SDavid Howells new_tsec->osid = old_tsec->sid; 23021da177e4SLinus Torvalds 230328eba5bfSMichael LeMay /* Reset fs, key, and sock SIDs on execve. */ 2304a6f76f23SDavid Howells new_tsec->create_sid = 0; 2305a6f76f23SDavid Howells new_tsec->keycreate_sid = 0; 2306a6f76f23SDavid Howells new_tsec->sockcreate_sid = 0; 23071da177e4SLinus Torvalds 2308a6f76f23SDavid Howells if (old_tsec->exec_sid) { 2309a6f76f23SDavid Howells new_tsec->sid = old_tsec->exec_sid; 23101da177e4SLinus Torvalds /* Reset exec SID on execve. */ 2311a6f76f23SDavid Howells new_tsec->exec_sid = 0; 2312259e5e6cSAndy Lutomirski 23137b0d0b40SStephen Smalley /* Fail on NNP or nosuid if not an allowed transition. */ 23147b0d0b40SStephen Smalley rc = check_nnp_nosuid(bprm, old_tsec, new_tsec); 23157b0d0b40SStephen Smalley if (rc) 23167b0d0b40SStephen Smalley return rc; 23171da177e4SLinus Torvalds } else { 23181da177e4SLinus Torvalds /* Check for a default transition on this program. */ 2319aa8e712cSStephen Smalley rc = security_transition_sid(&selinux_state, old_tsec->sid, 2320aa8e712cSStephen Smalley isec->sid, SECCLASS_PROCESS, NULL, 2321652bb9b0SEric Paris &new_tsec->sid); 23221da177e4SLinus Torvalds if (rc) 23231da177e4SLinus Torvalds return rc; 23247b0d0b40SStephen Smalley 23257b0d0b40SStephen Smalley /* 23267b0d0b40SStephen Smalley * Fallback to old SID on NNP or nosuid if not an allowed 23277b0d0b40SStephen Smalley * transition. 23287b0d0b40SStephen Smalley */ 23297b0d0b40SStephen Smalley rc = check_nnp_nosuid(bprm, old_tsec, new_tsec); 23307b0d0b40SStephen Smalley if (rc) 23317b0d0b40SStephen Smalley new_tsec->sid = old_tsec->sid; 23321da177e4SLinus Torvalds } 23331da177e4SLinus Torvalds 233443af5de7SVivek Goyal ad.type = LSM_AUDIT_DATA_FILE; 233543af5de7SVivek Goyal ad.u.file = bprm->file; 23361da177e4SLinus Torvalds 2337a6f76f23SDavid Howells if (new_tsec->sid == old_tsec->sid) { 23386b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 23396b6bc620SStephen Smalley old_tsec->sid, isec->sid, 23401da177e4SLinus Torvalds SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad); 23411da177e4SLinus Torvalds if (rc) 23421da177e4SLinus Torvalds return rc; 23431da177e4SLinus Torvalds } else { 23441da177e4SLinus Torvalds /* Check permissions for the transition. */ 23456b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 23466b6bc620SStephen Smalley old_tsec->sid, new_tsec->sid, 23471da177e4SLinus Torvalds SECCLASS_PROCESS, PROCESS__TRANSITION, &ad); 23481da177e4SLinus Torvalds if (rc) 23491da177e4SLinus Torvalds return rc; 23501da177e4SLinus Torvalds 23516b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 23526b6bc620SStephen Smalley new_tsec->sid, isec->sid, 23531da177e4SLinus Torvalds SECCLASS_FILE, FILE__ENTRYPOINT, &ad); 23541da177e4SLinus Torvalds if (rc) 23551da177e4SLinus Torvalds return rc; 23561da177e4SLinus Torvalds 2357a6f76f23SDavid Howells /* Check for shared state */ 2358a6f76f23SDavid Howells if (bprm->unsafe & LSM_UNSAFE_SHARE) { 23596b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 23606b6bc620SStephen Smalley old_tsec->sid, new_tsec->sid, 2361a6f76f23SDavid Howells SECCLASS_PROCESS, PROCESS__SHARE, 2362a6f76f23SDavid Howells NULL); 2363a6f76f23SDavid Howells if (rc) 2364a6f76f23SDavid Howells return -EPERM; 23651da177e4SLinus Torvalds } 23661da177e4SLinus Torvalds 2367a6f76f23SDavid Howells /* Make sure that anyone attempting to ptrace over a task that 2368a6f76f23SDavid Howells * changes its SID has the appropriate permit */ 23699227dd2aSEric W. Biederman if (bprm->unsafe & LSM_UNSAFE_PTRACE) { 2370be0554c9SStephen Smalley u32 ptsid = ptrace_parent_sid(); 2371a6f76f23SDavid Howells if (ptsid != 0) { 23726b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 23736b6bc620SStephen Smalley ptsid, new_tsec->sid, 2374a6f76f23SDavid Howells SECCLASS_PROCESS, 2375a6f76f23SDavid Howells PROCESS__PTRACE, NULL); 2376a6f76f23SDavid Howells if (rc) 2377a6f76f23SDavid Howells return -EPERM; 2378a6f76f23SDavid Howells } 2379a6f76f23SDavid Howells } 2380a6f76f23SDavid Howells 2381a6f76f23SDavid Howells /* Clear any possibly unsafe personality bits on exec: */ 2382a6f76f23SDavid Howells bprm->per_clear |= PER_CLEAR_ON_SETID; 2383a6f76f23SDavid Howells 23841da177e4SLinus Torvalds /* Enable secure mode for SIDs transitions unless 23851da177e4SLinus Torvalds the noatsecure permission is granted between 23861da177e4SLinus Torvalds the two SIDs, i.e. ahp returns 0. */ 23876b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 23886b6bc620SStephen Smalley old_tsec->sid, new_tsec->sid, 238962874c3aSKees Cook SECCLASS_PROCESS, PROCESS__NOATSECURE, 239062874c3aSKees Cook NULL); 239162874c3aSKees Cook bprm->secureexec |= !!rc; 23921da177e4SLinus Torvalds } 23931da177e4SLinus Torvalds 239462874c3aSKees Cook return 0; 23951da177e4SLinus Torvalds } 23961da177e4SLinus Torvalds 2397c3c073f8SAl Viro static int match_file(const void *p, struct file *file, unsigned fd) 2398c3c073f8SAl Viro { 2399c3c073f8SAl Viro return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0; 2400c3c073f8SAl Viro } 2401c3c073f8SAl Viro 24021da177e4SLinus Torvalds /* Derived from fs/exec.c:flush_old_files. */ 2403745ca247SDavid Howells static inline void flush_unauthorized_files(const struct cred *cred, 2404745ca247SDavid Howells struct files_struct *files) 24051da177e4SLinus Torvalds { 24061da177e4SLinus Torvalds struct file *file, *devnull = NULL; 2407b20c8122SStephen Smalley struct tty_struct *tty; 240824ec839cSPeter Zijlstra int drop_tty = 0; 2409c3c073f8SAl Viro unsigned n; 24101da177e4SLinus Torvalds 241124ec839cSPeter Zijlstra tty = get_current_tty(); 24121da177e4SLinus Torvalds if (tty) { 24134a510969SPeter Hurley spin_lock(&tty->files_lock); 241437dd0bd0SEric Paris if (!list_empty(&tty->tty_files)) { 2415d996b62aSNick Piggin struct tty_file_private *file_priv; 241637dd0bd0SEric Paris 24171da177e4SLinus Torvalds /* Revalidate access to controlling tty. 241813f8e981SDavid Howells Use file_path_has_perm on the tty path directly 241913f8e981SDavid Howells rather than using file_has_perm, as this particular 242013f8e981SDavid Howells open file may belong to another process and we are 242113f8e981SDavid Howells only interested in the inode-based check here. */ 2422d996b62aSNick Piggin file_priv = list_first_entry(&tty->tty_files, 2423d996b62aSNick Piggin struct tty_file_private, list); 2424d996b62aSNick Piggin file = file_priv->file; 242513f8e981SDavid Howells if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE)) 242624ec839cSPeter Zijlstra drop_tty = 1; 24271da177e4SLinus Torvalds } 24284a510969SPeter Hurley spin_unlock(&tty->files_lock); 2429452a00d2SAlan Cox tty_kref_put(tty); 24301da177e4SLinus Torvalds } 243198a27ba4SEric W. Biederman /* Reset controlling tty. */ 243298a27ba4SEric W. Biederman if (drop_tty) 243398a27ba4SEric W. Biederman no_tty(); 24341da177e4SLinus Torvalds 24351da177e4SLinus Torvalds /* Revalidate access to inherited open files. */ 2436c3c073f8SAl Viro n = iterate_fd(files, 0, match_file, cred); 2437c3c073f8SAl Viro if (!n) /* none found? */ 2438c3c073f8SAl Viro return; 24391da177e4SLinus Torvalds 2440c3c073f8SAl Viro devnull = dentry_open(&selinux_null, O_RDWR, cred); 244145525b26SAl Viro if (IS_ERR(devnull)) 244245525b26SAl Viro devnull = NULL; 2443c3c073f8SAl Viro /* replace all the matching ones with this */ 2444c3c073f8SAl Viro do { 244545525b26SAl Viro replace_fd(n - 1, devnull, 0); 2446c3c073f8SAl Viro } while ((n = iterate_fd(files, n, match_file, cred)) != 0); 244745525b26SAl Viro if (devnull) 2448c3c073f8SAl Viro fput(devnull); 24491da177e4SLinus Torvalds } 24501da177e4SLinus Torvalds 24511da177e4SLinus Torvalds /* 2452a6f76f23SDavid Howells * Prepare a process for imminent new credential changes due to exec 24531da177e4SLinus Torvalds */ 2454a6f76f23SDavid Howells static void selinux_bprm_committing_creds(struct linux_binprm *bprm) 24551da177e4SLinus Torvalds { 2456a6f76f23SDavid Howells struct task_security_struct *new_tsec; 24571da177e4SLinus Torvalds struct rlimit *rlim, *initrlim; 24581da177e4SLinus Torvalds int rc, i; 24591da177e4SLinus Torvalds 24600c6cfa62SCasey Schaufler new_tsec = selinux_cred(bprm->cred); 2461a6f76f23SDavid Howells if (new_tsec->sid == new_tsec->osid) 24621da177e4SLinus Torvalds return; 24631da177e4SLinus Torvalds 24641da177e4SLinus Torvalds /* Close files for which the new task SID is not authorized. */ 2465a6f76f23SDavid Howells flush_unauthorized_files(bprm->cred, current->files); 24661da177e4SLinus Torvalds 2467a6f76f23SDavid Howells /* Always clear parent death signal on SID transitions. */ 2468a6f76f23SDavid Howells current->pdeath_signal = 0; 2469a6f76f23SDavid Howells 2470a6f76f23SDavid Howells /* Check whether the new SID can inherit resource limits from the old 2471a6f76f23SDavid Howells * SID. If not, reset all soft limits to the lower of the current 2472a6f76f23SDavid Howells * task's hard limit and the init task's soft limit. 2473a6f76f23SDavid Howells * 2474a6f76f23SDavid Howells * Note that the setting of hard limits (even to lower them) can be 2475a6f76f23SDavid Howells * controlled by the setrlimit check. The inclusion of the init task's 2476a6f76f23SDavid Howells * soft limit into the computation is to avoid resetting soft limits 2477a6f76f23SDavid Howells * higher than the default soft limit for cases where the default is 2478a6f76f23SDavid Howells * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK. 2479a6f76f23SDavid Howells */ 24806b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 24816b6bc620SStephen Smalley new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS, 2482a6f76f23SDavid Howells PROCESS__RLIMITINH, NULL); 2483a6f76f23SDavid Howells if (rc) { 2484eb2d55a3SOleg Nesterov /* protect against do_prlimit() */ 2485eb2d55a3SOleg Nesterov task_lock(current); 2486a6f76f23SDavid Howells for (i = 0; i < RLIM_NLIMITS; i++) { 2487a6f76f23SDavid Howells rlim = current->signal->rlim + i; 2488a6f76f23SDavid Howells initrlim = init_task.signal->rlim + i; 2489a6f76f23SDavid Howells rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur); 2490a6f76f23SDavid Howells } 2491eb2d55a3SOleg Nesterov task_unlock(current); 2492baa73d9eSNicolas Pitre if (IS_ENABLED(CONFIG_POSIX_TIMERS)) 2493eb2d55a3SOleg Nesterov update_rlimit_cpu(current, rlimit(RLIMIT_CPU)); 2494a6f76f23SDavid Howells } 2495a6f76f23SDavid Howells } 2496a6f76f23SDavid Howells 2497a6f76f23SDavid Howells /* 2498a6f76f23SDavid Howells * Clean up the process immediately after the installation of new credentials 2499a6f76f23SDavid Howells * due to exec 2500a6f76f23SDavid Howells */ 2501a6f76f23SDavid Howells static void selinux_bprm_committed_creds(struct linux_binprm *bprm) 2502a6f76f23SDavid Howells { 25030c6cfa62SCasey Schaufler const struct task_security_struct *tsec = selinux_cred(current_cred()); 2504a6f76f23SDavid Howells struct itimerval itimer; 2505a6f76f23SDavid Howells u32 osid, sid; 2506a6f76f23SDavid Howells int rc, i; 2507a6f76f23SDavid Howells 2508a6f76f23SDavid Howells osid = tsec->osid; 2509a6f76f23SDavid Howells sid = tsec->sid; 2510a6f76f23SDavid Howells 2511a6f76f23SDavid Howells if (sid == osid) 2512a6f76f23SDavid Howells return; 2513a6f76f23SDavid Howells 2514a6f76f23SDavid Howells /* Check whether the new SID can inherit signal state from the old SID. 2515a6f76f23SDavid Howells * If not, clear itimers to avoid subsequent signal generation and 2516a6f76f23SDavid Howells * flush and unblock signals. 2517a6f76f23SDavid Howells * 2518a6f76f23SDavid Howells * This must occur _after_ the task SID has been updated so that any 2519a6f76f23SDavid Howells * kill done after the flush will be checked against the new SID. 2520a6f76f23SDavid Howells */ 25216b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 25226b6bc620SStephen Smalley osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL); 25231da177e4SLinus Torvalds if (rc) { 2524baa73d9eSNicolas Pitre if (IS_ENABLED(CONFIG_POSIX_TIMERS)) { 25251da177e4SLinus Torvalds memset(&itimer, 0, sizeof itimer); 25261da177e4SLinus Torvalds for (i = 0; i < 3; i++) 25271da177e4SLinus Torvalds do_setitimer(i, &itimer, NULL); 2528baa73d9eSNicolas Pitre } 25291da177e4SLinus Torvalds spin_lock_irq(¤t->sighand->siglock); 25309e7c8f8cSOleg Nesterov if (!fatal_signal_pending(current)) { 25319e7c8f8cSOleg Nesterov flush_sigqueue(¤t->pending); 25329e7c8f8cSOleg Nesterov flush_sigqueue(¤t->signal->shared_pending); 25331da177e4SLinus Torvalds flush_signal_handlers(current, 1); 25341da177e4SLinus Torvalds sigemptyset(¤t->blocked); 25359e7c8f8cSOleg Nesterov recalc_sigpending(); 25363bcac026SDavid Howells } 25371da177e4SLinus Torvalds spin_unlock_irq(¤t->sighand->siglock); 25381da177e4SLinus Torvalds } 25391da177e4SLinus Torvalds 2540a6f76f23SDavid Howells /* Wake up the parent if it is waiting so that it can recheck 2541a6f76f23SDavid Howells * wait permission to the new task SID. */ 2542ecd6de3cSOleg Nesterov read_lock(&tasklist_lock); 25430b7570e7SOleg Nesterov __wake_up_parent(current, current->real_parent); 2544ecd6de3cSOleg Nesterov read_unlock(&tasklist_lock); 25451da177e4SLinus Torvalds } 25461da177e4SLinus Torvalds 25471da177e4SLinus Torvalds /* superblock security operations */ 25481da177e4SLinus Torvalds 25491da177e4SLinus Torvalds static int selinux_sb_alloc_security(struct super_block *sb) 25501da177e4SLinus Torvalds { 25511da177e4SLinus Torvalds return superblock_alloc_security(sb); 25521da177e4SLinus Torvalds } 25531da177e4SLinus Torvalds 25541da177e4SLinus Torvalds static void selinux_sb_free_security(struct super_block *sb) 25551da177e4SLinus Torvalds { 25561da177e4SLinus Torvalds superblock_free_security(sb); 25571da177e4SLinus Torvalds } 25581da177e4SLinus Torvalds 255999dbbb59SAl Viro static inline int opt_len(const char *s) 25601da177e4SLinus Torvalds { 256199dbbb59SAl Viro bool open_quote = false; 256299dbbb59SAl Viro int len; 256399dbbb59SAl Viro char c; 25641da177e4SLinus Torvalds 256599dbbb59SAl Viro for (len = 0; (c = s[len]) != '\0'; len++) { 256699dbbb59SAl Viro if (c == '"') 25673528a953SCory Olmo open_quote = !open_quote; 256899dbbb59SAl Viro if (c == ',' && !open_quote) 256999dbbb59SAl Viro break; 25701da177e4SLinus Torvalds } 257199dbbb59SAl Viro return len; 25721da177e4SLinus Torvalds } 25731da177e4SLinus Torvalds 2574204cc0ccSAl Viro static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts) 25755b400239SAl Viro { 257699dbbb59SAl Viro char *from = options; 257799dbbb59SAl Viro char *to = options; 257899dbbb59SAl Viro bool first = true; 25795b400239SAl Viro 258099dbbb59SAl Viro while (1) { 258199dbbb59SAl Viro int len = opt_len(from); 258299dbbb59SAl Viro int token, rc; 258399dbbb59SAl Viro char *arg = NULL; 258499dbbb59SAl Viro 258599dbbb59SAl Viro token = match_opt_prefix(from, len, &arg); 258699dbbb59SAl Viro 258799dbbb59SAl Viro if (token != Opt_error) { 258899dbbb59SAl Viro char *p, *q; 258999dbbb59SAl Viro 259099dbbb59SAl Viro /* strip quotes */ 259199dbbb59SAl Viro if (arg) { 259299dbbb59SAl Viro for (p = q = arg; p < from + len; p++) { 259399dbbb59SAl Viro char c = *p; 259499dbbb59SAl Viro if (c != '"') 259599dbbb59SAl Viro *q++ = c; 259699dbbb59SAl Viro } 259799dbbb59SAl Viro arg = kmemdup_nul(arg, q - arg, GFP_KERNEL); 259899dbbb59SAl Viro } 259999dbbb59SAl Viro rc = selinux_add_opt(token, arg, mnt_opts); 260099dbbb59SAl Viro if (unlikely(rc)) { 260199dbbb59SAl Viro kfree(arg); 260299dbbb59SAl Viro if (*mnt_opts) { 260399dbbb59SAl Viro selinux_free_mnt_opts(*mnt_opts); 260499dbbb59SAl Viro *mnt_opts = NULL; 260599dbbb59SAl Viro } 26061da177e4SLinus Torvalds return rc; 26071da177e4SLinus Torvalds } 260899dbbb59SAl Viro } else { 260999dbbb59SAl Viro if (!first) { // copy with preceding comma 261099dbbb59SAl Viro from--; 261199dbbb59SAl Viro len++; 261299dbbb59SAl Viro } 261399dbbb59SAl Viro if (to != from) 261499dbbb59SAl Viro memmove(to, from, len); 261599dbbb59SAl Viro to += len; 261699dbbb59SAl Viro first = false; 261799dbbb59SAl Viro } 261899dbbb59SAl Viro if (!from[len]) 261999dbbb59SAl Viro break; 262099dbbb59SAl Viro from += len + 1; 262199dbbb59SAl Viro } 262299dbbb59SAl Viro *to = '\0'; 262399dbbb59SAl Viro return 0; 26245b400239SAl Viro } 26251da177e4SLinus Torvalds 2626204cc0ccSAl Viro static int selinux_sb_remount(struct super_block *sb, void *mnt_opts) 2627026eb167SEric Paris { 2628bd323655SAl Viro struct selinux_mnt_opts *opts = mnt_opts; 2629026eb167SEric Paris struct superblock_security_struct *sbsec = sb->s_security; 2630bd323655SAl Viro u32 sid; 2631bd323655SAl Viro int rc; 2632026eb167SEric Paris 2633026eb167SEric Paris if (!(sbsec->flags & SE_SBINITIALIZED)) 2634026eb167SEric Paris return 0; 2635026eb167SEric Paris 2636204cc0ccSAl Viro if (!opts) 2637026eb167SEric Paris return 0; 2638026eb167SEric Paris 2639bd323655SAl Viro if (opts->fscontext) { 2640bd323655SAl Viro rc = parse_sid(sb, opts->fscontext, &sid); 2641026eb167SEric Paris if (rc) 2642c039bc3cSAl Viro return rc; 2643026eb167SEric Paris if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid)) 2644026eb167SEric Paris goto out_bad_option; 2645bd323655SAl Viro } 2646bd323655SAl Viro if (opts->context) { 2647bd323655SAl Viro rc = parse_sid(sb, opts->context, &sid); 2648bd323655SAl Viro if (rc) 2649bd323655SAl Viro return rc; 2650026eb167SEric Paris if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid)) 2651026eb167SEric Paris goto out_bad_option; 2652bd323655SAl Viro } 2653bd323655SAl Viro if (opts->rootcontext) { 2654026eb167SEric Paris struct inode_security_struct *root_isec; 265583da53c5SAndreas Gruenbacher root_isec = backing_inode_security(sb->s_root); 2656bd323655SAl Viro rc = parse_sid(sb, opts->rootcontext, &sid); 2657bd323655SAl Viro if (rc) 2658bd323655SAl Viro return rc; 2659026eb167SEric Paris if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid)) 2660026eb167SEric Paris goto out_bad_option; 2661026eb167SEric Paris } 2662bd323655SAl Viro if (opts->defcontext) { 2663bd323655SAl Viro rc = parse_sid(sb, opts->defcontext, &sid); 2664bd323655SAl Viro if (rc) 2665bd323655SAl Viro return rc; 2666026eb167SEric Paris if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid)) 2667026eb167SEric Paris goto out_bad_option; 2668026eb167SEric Paris } 2669c039bc3cSAl Viro return 0; 2670026eb167SEric Paris 2671026eb167SEric Paris out_bad_option: 2672c103a91eSpeter enderborg pr_warn("SELinux: unable to change security options " 267329b1deb2SLinus Torvalds "during remount (dev %s, type=%s)\n", sb->s_id, 267429b1deb2SLinus Torvalds sb->s_type->name); 2675c039bc3cSAl Viro return -EINVAL; 2676026eb167SEric Paris } 2677026eb167SEric Paris 2678a10d7c22SAl Viro static int selinux_sb_kern_mount(struct super_block *sb) 26791da177e4SLinus Torvalds { 268088e67f3bSDavid Howells const struct cred *cred = current_cred(); 26812bf49690SThomas Liu struct common_audit_data ad; 268274192246SJames Morris 268350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 2684a269434dSEric Paris ad.u.dentry = sb->s_root; 268588e67f3bSDavid Howells return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad); 26861da177e4SLinus Torvalds } 26871da177e4SLinus Torvalds 2688726c3342SDavid Howells static int selinux_sb_statfs(struct dentry *dentry) 26891da177e4SLinus Torvalds { 269088e67f3bSDavid Howells const struct cred *cred = current_cred(); 26912bf49690SThomas Liu struct common_audit_data ad; 26921da177e4SLinus Torvalds 269350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 2694a269434dSEric Paris ad.u.dentry = dentry->d_sb->s_root; 269588e67f3bSDavid Howells return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad); 26961da177e4SLinus Torvalds } 26971da177e4SLinus Torvalds 2698808d4e3cSAl Viro static int selinux_mount(const char *dev_name, 26998a04c43bSAl Viro const struct path *path, 2700808d4e3cSAl Viro const char *type, 27011da177e4SLinus Torvalds unsigned long flags, 27021da177e4SLinus Torvalds void *data) 27031da177e4SLinus Torvalds { 270488e67f3bSDavid Howells const struct cred *cred = current_cred(); 27051da177e4SLinus Torvalds 27061da177e4SLinus Torvalds if (flags & MS_REMOUNT) 2707d8c9584eSAl Viro return superblock_has_perm(cred, path->dentry->d_sb, 27081da177e4SLinus Torvalds FILESYSTEM__REMOUNT, NULL); 27091da177e4SLinus Torvalds else 27102875fa00SEric Paris return path_has_perm(cred, path, FILE__MOUNTON); 27111da177e4SLinus Torvalds } 27121da177e4SLinus Torvalds 27131da177e4SLinus Torvalds static int selinux_umount(struct vfsmount *mnt, int flags) 27141da177e4SLinus Torvalds { 271588e67f3bSDavid Howells const struct cred *cred = current_cred(); 27161da177e4SLinus Torvalds 271788e67f3bSDavid Howells return superblock_has_perm(cred, mnt->mnt_sb, 27181da177e4SLinus Torvalds FILESYSTEM__UNMOUNT, NULL); 27191da177e4SLinus Torvalds } 27201da177e4SLinus Torvalds 27211da177e4SLinus Torvalds /* inode security operations */ 27221da177e4SLinus Torvalds 27231da177e4SLinus Torvalds static int selinux_inode_alloc_security(struct inode *inode) 27241da177e4SLinus Torvalds { 27251da177e4SLinus Torvalds return inode_alloc_security(inode); 27261da177e4SLinus Torvalds } 27271da177e4SLinus Torvalds 27281da177e4SLinus Torvalds static void selinux_inode_free_security(struct inode *inode) 27291da177e4SLinus Torvalds { 27301da177e4SLinus Torvalds inode_free_security(inode); 27311da177e4SLinus Torvalds } 27321da177e4SLinus Torvalds 2733d47be3dfSDavid Quigley static int selinux_dentry_init_security(struct dentry *dentry, int mode, 27344f3ccd76SAl Viro const struct qstr *name, void **ctx, 2735d47be3dfSDavid Quigley u32 *ctxlen) 2736d47be3dfSDavid Quigley { 2737d47be3dfSDavid Quigley u32 newsid; 2738d47be3dfSDavid Quigley int rc; 2739d47be3dfSDavid Quigley 27400c6cfa62SCasey Schaufler rc = selinux_determine_inode_label(selinux_cred(current_cred()), 2741c957f6dfSVivek Goyal d_inode(dentry->d_parent), name, 2742d47be3dfSDavid Quigley inode_mode_to_security_class(mode), 2743d47be3dfSDavid Quigley &newsid); 2744c3c188b2SDavid Howells if (rc) 2745d47be3dfSDavid Quigley return rc; 2746d47be3dfSDavid Quigley 2747aa8e712cSStephen Smalley return security_sid_to_context(&selinux_state, newsid, (char **)ctx, 2748aa8e712cSStephen Smalley ctxlen); 2749d47be3dfSDavid Quigley } 2750d47be3dfSDavid Quigley 2751a518b0a5SVivek Goyal static int selinux_dentry_create_files_as(struct dentry *dentry, int mode, 2752a518b0a5SVivek Goyal struct qstr *name, 2753a518b0a5SVivek Goyal const struct cred *old, 2754a518b0a5SVivek Goyal struct cred *new) 2755a518b0a5SVivek Goyal { 2756a518b0a5SVivek Goyal u32 newsid; 2757a518b0a5SVivek Goyal int rc; 2758a518b0a5SVivek Goyal struct task_security_struct *tsec; 2759a518b0a5SVivek Goyal 27600c6cfa62SCasey Schaufler rc = selinux_determine_inode_label(selinux_cred(old), 2761a518b0a5SVivek Goyal d_inode(dentry->d_parent), name, 2762a518b0a5SVivek Goyal inode_mode_to_security_class(mode), 2763a518b0a5SVivek Goyal &newsid); 2764a518b0a5SVivek Goyal if (rc) 2765a518b0a5SVivek Goyal return rc; 2766a518b0a5SVivek Goyal 27670c6cfa62SCasey Schaufler tsec = selinux_cred(new); 2768a518b0a5SVivek Goyal tsec->create_sid = newsid; 2769a518b0a5SVivek Goyal return 0; 2770a518b0a5SVivek Goyal } 2771a518b0a5SVivek Goyal 27725e41ff9eSStephen Smalley static int selinux_inode_init_security(struct inode *inode, struct inode *dir, 27739548906bSTetsuo Handa const struct qstr *qstr, 27749548906bSTetsuo Handa const char **name, 27752a7dba39SEric Paris void **value, size_t *len) 27765e41ff9eSStephen Smalley { 27770c6cfa62SCasey Schaufler const struct task_security_struct *tsec = selinux_cred(current_cred()); 27785e41ff9eSStephen Smalley struct superblock_security_struct *sbsec; 2779c0d4f464SCorentin LABBE u32 newsid, clen; 27805e41ff9eSStephen Smalley int rc; 27819548906bSTetsuo Handa char *context; 27825e41ff9eSStephen Smalley 27835e41ff9eSStephen Smalley sbsec = dir->i_sb->s_security; 27845e41ff9eSStephen Smalley 27855e41ff9eSStephen Smalley newsid = tsec->create_sid; 2786275bb41eSDavid Howells 27870c6cfa62SCasey Schaufler rc = selinux_determine_inode_label(selinux_cred(current_cred()), 2788c3c188b2SDavid Howells dir, qstr, 27895e41ff9eSStephen Smalley inode_mode_to_security_class(inode->i_mode), 2790c3c188b2SDavid Howells &newsid); 2791c3c188b2SDavid Howells if (rc) 27925e41ff9eSStephen Smalley return rc; 27935e41ff9eSStephen Smalley 2794296fddf7SEric Paris /* Possibly defer initialization to selinux_complete_init. */ 27950d90a7ecSDavid P. Quigley if (sbsec->flags & SE_SBINITIALIZED) { 279680788c22SCasey Schaufler struct inode_security_struct *isec = selinux_inode(inode); 2797296fddf7SEric Paris isec->sclass = inode_mode_to_security_class(inode->i_mode); 2798296fddf7SEric Paris isec->sid = newsid; 27996f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 2800296fddf7SEric Paris } 28015e41ff9eSStephen Smalley 2802aa8e712cSStephen Smalley if (!selinux_state.initialized || !(sbsec->flags & SBLABEL_MNT)) 280325a74f3bSStephen Smalley return -EOPNOTSUPP; 280425a74f3bSStephen Smalley 28059548906bSTetsuo Handa if (name) 28069548906bSTetsuo Handa *name = XATTR_SELINUX_SUFFIX; 28075e41ff9eSStephen Smalley 2808570bc1c2SStephen Smalley if (value && len) { 2809aa8e712cSStephen Smalley rc = security_sid_to_context_force(&selinux_state, newsid, 2810aa8e712cSStephen Smalley &context, &clen); 28119548906bSTetsuo Handa if (rc) 28125e41ff9eSStephen Smalley return rc; 28135e41ff9eSStephen Smalley *value = context; 2814570bc1c2SStephen Smalley *len = clen; 2815570bc1c2SStephen Smalley } 28165e41ff9eSStephen Smalley 28175e41ff9eSStephen Smalley return 0; 28185e41ff9eSStephen Smalley } 28195e41ff9eSStephen Smalley 28204acdaf27SAl Viro static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode) 28211da177e4SLinus Torvalds { 28221da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_FILE); 28231da177e4SLinus Torvalds } 28241da177e4SLinus Torvalds 28251da177e4SLinus Torvalds static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 28261da177e4SLinus Torvalds { 28271da177e4SLinus Torvalds return may_link(dir, old_dentry, MAY_LINK); 28281da177e4SLinus Torvalds } 28291da177e4SLinus Torvalds 28301da177e4SLinus Torvalds static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry) 28311da177e4SLinus Torvalds { 28321da177e4SLinus Torvalds return may_link(dir, dentry, MAY_UNLINK); 28331da177e4SLinus Torvalds } 28341da177e4SLinus Torvalds 28351da177e4SLinus Torvalds static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name) 28361da177e4SLinus Torvalds { 28371da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_LNK_FILE); 28381da177e4SLinus Torvalds } 28391da177e4SLinus Torvalds 284018bb1db3SAl Viro static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask) 28411da177e4SLinus Torvalds { 28421da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_DIR); 28431da177e4SLinus Torvalds } 28441da177e4SLinus Torvalds 28451da177e4SLinus Torvalds static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry) 28461da177e4SLinus Torvalds { 28471da177e4SLinus Torvalds return may_link(dir, dentry, MAY_RMDIR); 28481da177e4SLinus Torvalds } 28491da177e4SLinus Torvalds 28501a67aafbSAl Viro static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) 28511da177e4SLinus Torvalds { 28521da177e4SLinus Torvalds return may_create(dir, dentry, inode_mode_to_security_class(mode)); 28531da177e4SLinus Torvalds } 28541da177e4SLinus Torvalds 28551da177e4SLinus Torvalds static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry, 28561da177e4SLinus Torvalds struct inode *new_inode, struct dentry *new_dentry) 28571da177e4SLinus Torvalds { 28581da177e4SLinus Torvalds return may_rename(old_inode, old_dentry, new_inode, new_dentry); 28591da177e4SLinus Torvalds } 28601da177e4SLinus Torvalds 28611da177e4SLinus Torvalds static int selinux_inode_readlink(struct dentry *dentry) 28621da177e4SLinus Torvalds { 286388e67f3bSDavid Howells const struct cred *cred = current_cred(); 286488e67f3bSDavid Howells 28652875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__READ); 28661da177e4SLinus Torvalds } 28671da177e4SLinus Torvalds 2868bda0be7aSNeilBrown static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode, 2869bda0be7aSNeilBrown bool rcu) 28701da177e4SLinus Torvalds { 287188e67f3bSDavid Howells const struct cred *cred = current_cred(); 2872bda0be7aSNeilBrown struct common_audit_data ad; 2873bda0be7aSNeilBrown struct inode_security_struct *isec; 2874bda0be7aSNeilBrown u32 sid; 28751da177e4SLinus Torvalds 2876bda0be7aSNeilBrown validate_creds(cred); 2877bda0be7aSNeilBrown 2878bda0be7aSNeilBrown ad.type = LSM_AUDIT_DATA_DENTRY; 2879bda0be7aSNeilBrown ad.u.dentry = dentry; 2880bda0be7aSNeilBrown sid = cred_sid(cred); 28815d226df4SAndreas Gruenbacher isec = inode_security_rcu(inode, rcu); 28825d226df4SAndreas Gruenbacher if (IS_ERR(isec)) 28835d226df4SAndreas Gruenbacher return PTR_ERR(isec); 2884bda0be7aSNeilBrown 28856b6bc620SStephen Smalley return avc_has_perm_flags(&selinux_state, 28866b6bc620SStephen Smalley sid, isec->sid, isec->sclass, FILE__READ, &ad, 2887bda0be7aSNeilBrown rcu ? MAY_NOT_BLOCK : 0); 28881da177e4SLinus Torvalds } 28891da177e4SLinus Torvalds 2890d4cf970dSEric Paris static noinline int audit_inode_permission(struct inode *inode, 2891d4cf970dSEric Paris u32 perms, u32 audited, u32 denied, 2892626b9740SStephen Smalley int result, 2893d4cf970dSEric Paris unsigned flags) 2894d4cf970dSEric Paris { 2895d4cf970dSEric Paris struct common_audit_data ad; 289680788c22SCasey Schaufler struct inode_security_struct *isec = selinux_inode(inode); 2897d4cf970dSEric Paris int rc; 2898d4cf970dSEric Paris 289950c205f5SEric Paris ad.type = LSM_AUDIT_DATA_INODE; 2900d4cf970dSEric Paris ad.u.inode = inode; 2901d4cf970dSEric Paris 29026b6bc620SStephen Smalley rc = slow_avc_audit(&selinux_state, 29036b6bc620SStephen Smalley current_sid(), isec->sid, isec->sclass, perms, 2904626b9740SStephen Smalley audited, denied, result, &ad, flags); 2905d4cf970dSEric Paris if (rc) 2906d4cf970dSEric Paris return rc; 2907d4cf970dSEric Paris return 0; 2908d4cf970dSEric Paris } 2909d4cf970dSEric Paris 2910e74f71ebSAl Viro static int selinux_inode_permission(struct inode *inode, int mask) 29111da177e4SLinus Torvalds { 291288e67f3bSDavid Howells const struct cred *cred = current_cred(); 2913b782e0a6SEric Paris u32 perms; 2914b782e0a6SEric Paris bool from_access; 2915cf1dd1daSAl Viro unsigned flags = mask & MAY_NOT_BLOCK; 29162e334057SEric Paris struct inode_security_struct *isec; 29172e334057SEric Paris u32 sid; 29182e334057SEric Paris struct av_decision avd; 29192e334057SEric Paris int rc, rc2; 29202e334057SEric Paris u32 audited, denied; 29211da177e4SLinus Torvalds 2922b782e0a6SEric Paris from_access = mask & MAY_ACCESS; 2923d09ca739SEric Paris mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND); 2924d09ca739SEric Paris 29251da177e4SLinus Torvalds /* No permission to check. Existence test. */ 2926b782e0a6SEric Paris if (!mask) 29271da177e4SLinus Torvalds return 0; 29281da177e4SLinus Torvalds 29292e334057SEric Paris validate_creds(cred); 2930b782e0a6SEric Paris 29312e334057SEric Paris if (unlikely(IS_PRIVATE(inode))) 29322e334057SEric Paris return 0; 2933b782e0a6SEric Paris 2934b782e0a6SEric Paris perms = file_mask_to_av(inode->i_mode, mask); 2935b782e0a6SEric Paris 29362e334057SEric Paris sid = cred_sid(cred); 29375d226df4SAndreas Gruenbacher isec = inode_security_rcu(inode, flags & MAY_NOT_BLOCK); 29385d226df4SAndreas Gruenbacher if (IS_ERR(isec)) 29395d226df4SAndreas Gruenbacher return PTR_ERR(isec); 29402e334057SEric Paris 29416b6bc620SStephen Smalley rc = avc_has_perm_noaudit(&selinux_state, 29426b6bc620SStephen Smalley sid, isec->sid, isec->sclass, perms, 0, &avd); 29432e334057SEric Paris audited = avc_audit_required(perms, &avd, rc, 29442e334057SEric Paris from_access ? FILE__AUDIT_ACCESS : 0, 29452e334057SEric Paris &denied); 29462e334057SEric Paris if (likely(!audited)) 29472e334057SEric Paris return rc; 29482e334057SEric Paris 2949626b9740SStephen Smalley rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags); 29502e334057SEric Paris if (rc2) 29512e334057SEric Paris return rc2; 29522e334057SEric Paris return rc; 29531da177e4SLinus Torvalds } 29541da177e4SLinus Torvalds 29551da177e4SLinus Torvalds static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr) 29561da177e4SLinus Torvalds { 295788e67f3bSDavid Howells const struct cred *cred = current_cred(); 2958ccb54478SStephen Smalley struct inode *inode = d_backing_inode(dentry); 2959bc6a6008SAmerigo Wang unsigned int ia_valid = iattr->ia_valid; 296095dbf739SEric Paris __u32 av = FILE__WRITE; 29611da177e4SLinus Torvalds 2962bc6a6008SAmerigo Wang /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */ 2963bc6a6008SAmerigo Wang if (ia_valid & ATTR_FORCE) { 2964bc6a6008SAmerigo Wang ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE | 2965bc6a6008SAmerigo Wang ATTR_FORCE); 2966bc6a6008SAmerigo Wang if (!ia_valid) 29671da177e4SLinus Torvalds return 0; 2968bc6a6008SAmerigo Wang } 29691da177e4SLinus Torvalds 2970bc6a6008SAmerigo Wang if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | 2971bc6a6008SAmerigo Wang ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET)) 29722875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__SETATTR); 29731da177e4SLinus Torvalds 2974aa8e712cSStephen Smalley if (selinux_policycap_openperm() && 2975ccb54478SStephen Smalley inode->i_sb->s_magic != SOCKFS_MAGIC && 2976ccb54478SStephen Smalley (ia_valid & ATTR_SIZE) && 2977ccb54478SStephen Smalley !(ia_valid & ATTR_FILE)) 297895dbf739SEric Paris av |= FILE__OPEN; 297995dbf739SEric Paris 298095dbf739SEric Paris return dentry_has_perm(cred, dentry, av); 29811da177e4SLinus Torvalds } 29821da177e4SLinus Torvalds 29833f7036a0SAl Viro static int selinux_inode_getattr(const struct path *path) 29841da177e4SLinus Torvalds { 29853f7036a0SAl Viro return path_has_perm(current_cred(), path, FILE__GETATTR); 29861da177e4SLinus Torvalds } 29871da177e4SLinus Torvalds 2988db59000aSStephen Smalley static bool has_cap_mac_admin(bool audit) 2989db59000aSStephen Smalley { 2990db59000aSStephen Smalley const struct cred *cred = current_cred(); 2991db59000aSStephen Smalley int cap_audit = audit ? SECURITY_CAP_AUDIT : SECURITY_CAP_NOAUDIT; 2992db59000aSStephen Smalley 2993db59000aSStephen Smalley if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, cap_audit)) 2994db59000aSStephen Smalley return false; 2995db59000aSStephen Smalley if (cred_has_capability(cred, CAP_MAC_ADMIN, cap_audit, true)) 2996db59000aSStephen Smalley return false; 2997db59000aSStephen Smalley return true; 2998db59000aSStephen Smalley } 2999db59000aSStephen Smalley 30008f0cfa52SDavid Howells static int selinux_inode_setxattr(struct dentry *dentry, const char *name, 30018f0cfa52SDavid Howells const void *value, size_t size, int flags) 30021da177e4SLinus Torvalds { 3003c6f493d6SDavid Howells struct inode *inode = d_backing_inode(dentry); 300420cdef8dSPaul Moore struct inode_security_struct *isec; 30051da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 30062bf49690SThomas Liu struct common_audit_data ad; 3007275bb41eSDavid Howells u32 newsid, sid = current_sid(); 30081da177e4SLinus Torvalds int rc = 0; 30091da177e4SLinus Torvalds 30106b240306SEric W. Biederman if (strcmp(name, XATTR_NAME_SELINUX)) { 30116b240306SEric W. Biederman rc = cap_inode_setxattr(dentry, name, value, size, flags); 30126b240306SEric W. Biederman if (rc) 30136b240306SEric W. Biederman return rc; 30146b240306SEric W. Biederman 30156b240306SEric W. Biederman /* Not an attribute we recognize, so just check the 30166b240306SEric W. Biederman ordinary setattr permission. */ 30176b240306SEric W. Biederman return dentry_has_perm(current_cred(), dentry, FILE__SETATTR); 30186b240306SEric W. Biederman } 30191da177e4SLinus Torvalds 30201da177e4SLinus Torvalds sbsec = inode->i_sb->s_security; 302112f348b9SEric Paris if (!(sbsec->flags & SBLABEL_MNT)) 30221da177e4SLinus Torvalds return -EOPNOTSUPP; 30231da177e4SLinus Torvalds 30242e149670SSerge E. Hallyn if (!inode_owner_or_capable(inode)) 30251da177e4SLinus Torvalds return -EPERM; 30261da177e4SLinus Torvalds 302750c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 3028a269434dSEric Paris ad.u.dentry = dentry; 30291da177e4SLinus Torvalds 303020cdef8dSPaul Moore isec = backing_inode_security(dentry); 30316b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 30326b6bc620SStephen Smalley sid, isec->sid, isec->sclass, 30331da177e4SLinus Torvalds FILE__RELABELFROM, &ad); 30341da177e4SLinus Torvalds if (rc) 30351da177e4SLinus Torvalds return rc; 30361da177e4SLinus Torvalds 3037aa8e712cSStephen Smalley rc = security_context_to_sid(&selinux_state, value, size, &newsid, 3038aa8e712cSStephen Smalley GFP_KERNEL); 303912b29f34SStephen Smalley if (rc == -EINVAL) { 3040db59000aSStephen Smalley if (!has_cap_mac_admin(true)) { 3041d6ea83ecSEric Paris struct audit_buffer *ab; 3042d6ea83ecSEric Paris size_t audit_size; 3043d6ea83ecSEric Paris 3044d6ea83ecSEric Paris /* We strip a nul only if it is at the end, otherwise the 3045d6ea83ecSEric Paris * context contains a nul and we should audit that */ 3046e3fea3f7SAl Viro if (value) { 3047add24372SColin Ian King const char *str = value; 3048add24372SColin Ian King 3049d6ea83ecSEric Paris if (str[size - 1] == '\0') 3050d6ea83ecSEric Paris audit_size = size - 1; 3051d6ea83ecSEric Paris else 3052d6ea83ecSEric Paris audit_size = size; 3053e3fea3f7SAl Viro } else { 3054e3fea3f7SAl Viro audit_size = 0; 3055e3fea3f7SAl Viro } 3056cdfb6b34SRichard Guy Briggs ab = audit_log_start(audit_context(), 3057cdfb6b34SRichard Guy Briggs GFP_ATOMIC, AUDIT_SELINUX_ERR); 3058d6ea83ecSEric Paris audit_log_format(ab, "op=setxattr invalid_context="); 3059d6ea83ecSEric Paris audit_log_n_untrustedstring(ab, value, audit_size); 3060d6ea83ecSEric Paris audit_log_end(ab); 3061d6ea83ecSEric Paris 306212b29f34SStephen Smalley return rc; 3063d6ea83ecSEric Paris } 3064aa8e712cSStephen Smalley rc = security_context_to_sid_force(&selinux_state, value, 3065aa8e712cSStephen Smalley size, &newsid); 306612b29f34SStephen Smalley } 30671da177e4SLinus Torvalds if (rc) 30681da177e4SLinus Torvalds return rc; 30691da177e4SLinus Torvalds 30706b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 30716b6bc620SStephen Smalley sid, newsid, isec->sclass, 30721da177e4SLinus Torvalds FILE__RELABELTO, &ad); 30731da177e4SLinus Torvalds if (rc) 30741da177e4SLinus Torvalds return rc; 30751da177e4SLinus Torvalds 3076aa8e712cSStephen Smalley rc = security_validate_transition(&selinux_state, isec->sid, newsid, 3077aa8e712cSStephen Smalley sid, isec->sclass); 30781da177e4SLinus Torvalds if (rc) 30791da177e4SLinus Torvalds return rc; 30801da177e4SLinus Torvalds 30816b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 30826b6bc620SStephen Smalley newsid, 30831da177e4SLinus Torvalds sbsec->sid, 30841da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 30851da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, 30861da177e4SLinus Torvalds &ad); 30871da177e4SLinus Torvalds } 30881da177e4SLinus Torvalds 30898f0cfa52SDavid Howells static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name, 30908f0cfa52SDavid Howells const void *value, size_t size, 30918f0cfa52SDavid Howells int flags) 30921da177e4SLinus Torvalds { 3093c6f493d6SDavid Howells struct inode *inode = d_backing_inode(dentry); 309420cdef8dSPaul Moore struct inode_security_struct *isec; 30951da177e4SLinus Torvalds u32 newsid; 30961da177e4SLinus Torvalds int rc; 30971da177e4SLinus Torvalds 30981da177e4SLinus Torvalds if (strcmp(name, XATTR_NAME_SELINUX)) { 30991da177e4SLinus Torvalds /* Not an attribute we recognize, so nothing to do. */ 31001da177e4SLinus Torvalds return; 31011da177e4SLinus Torvalds } 31021da177e4SLinus Torvalds 3103aa8e712cSStephen Smalley rc = security_context_to_sid_force(&selinux_state, value, size, 3104aa8e712cSStephen Smalley &newsid); 31051da177e4SLinus Torvalds if (rc) { 3106c103a91eSpeter enderborg pr_err("SELinux: unable to map context to SID" 310712b29f34SStephen Smalley "for (%s, %lu), rc=%d\n", 310812b29f34SStephen Smalley inode->i_sb->s_id, inode->i_ino, -rc); 31091da177e4SLinus Torvalds return; 31101da177e4SLinus Torvalds } 31111da177e4SLinus Torvalds 311220cdef8dSPaul Moore isec = backing_inode_security(dentry); 31139287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 3114aa9c2669SDavid Quigley isec->sclass = inode_mode_to_security_class(inode->i_mode); 31151da177e4SLinus Torvalds isec->sid = newsid; 31166f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 31179287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 3118aa9c2669SDavid Quigley 31191da177e4SLinus Torvalds return; 31201da177e4SLinus Torvalds } 31211da177e4SLinus Torvalds 31228f0cfa52SDavid Howells static int selinux_inode_getxattr(struct dentry *dentry, const char *name) 31231da177e4SLinus Torvalds { 312488e67f3bSDavid Howells const struct cred *cred = current_cred(); 312588e67f3bSDavid Howells 31262875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__GETATTR); 31271da177e4SLinus Torvalds } 31281da177e4SLinus Torvalds 31291da177e4SLinus Torvalds static int selinux_inode_listxattr(struct dentry *dentry) 31301da177e4SLinus Torvalds { 313188e67f3bSDavid Howells const struct cred *cred = current_cred(); 313288e67f3bSDavid Howells 31332875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__GETATTR); 31341da177e4SLinus Torvalds } 31351da177e4SLinus Torvalds 31368f0cfa52SDavid Howells static int selinux_inode_removexattr(struct dentry *dentry, const char *name) 31371da177e4SLinus Torvalds { 31386b240306SEric W. Biederman if (strcmp(name, XATTR_NAME_SELINUX)) { 31396b240306SEric W. Biederman int rc = cap_inode_removexattr(dentry, name); 31406b240306SEric W. Biederman if (rc) 31416b240306SEric W. Biederman return rc; 31426b240306SEric W. Biederman 31436b240306SEric W. Biederman /* Not an attribute we recognize, so just check the 31446b240306SEric W. Biederman ordinary setattr permission. */ 31456b240306SEric W. Biederman return dentry_has_perm(current_cred(), dentry, FILE__SETATTR); 31466b240306SEric W. Biederman } 31471da177e4SLinus Torvalds 31481da177e4SLinus Torvalds /* No one is allowed to remove a SELinux security label. 31491da177e4SLinus Torvalds You can change the label, but all data must be labeled. */ 31501da177e4SLinus Torvalds return -EACCES; 31511da177e4SLinus Torvalds } 31521da177e4SLinus Torvalds 3153d381d8a9SJames Morris /* 3154abc69bb6SStephen Smalley * Copy the inode security context value to the user. 3155d381d8a9SJames Morris * 3156d381d8a9SJames Morris * Permission check is handled by selinux_inode_getxattr hook. 3157d381d8a9SJames Morris */ 3158ea861dfdSAndreas Gruenbacher static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc) 31591da177e4SLinus Torvalds { 316042492594SDavid P. Quigley u32 size; 316142492594SDavid P. Quigley int error; 316242492594SDavid P. Quigley char *context = NULL; 316320cdef8dSPaul Moore struct inode_security_struct *isec; 31641da177e4SLinus Torvalds 31658c8570fbSDustin Kirkland if (strcmp(name, XATTR_SELINUX_SUFFIX)) 31668c8570fbSDustin Kirkland return -EOPNOTSUPP; 31671da177e4SLinus Torvalds 3168abc69bb6SStephen Smalley /* 3169abc69bb6SStephen Smalley * If the caller has CAP_MAC_ADMIN, then get the raw context 3170abc69bb6SStephen Smalley * value even if it is not defined by current policy; otherwise, 3171abc69bb6SStephen Smalley * use the in-core value under current policy. 3172abc69bb6SStephen Smalley * Use the non-auditing forms of the permission checks since 3173abc69bb6SStephen Smalley * getxattr may be called by unprivileged processes commonly 3174abc69bb6SStephen Smalley * and lack of permission just means that we fall back to the 3175abc69bb6SStephen Smalley * in-core context value, not a denial. 3176abc69bb6SStephen Smalley */ 317720cdef8dSPaul Moore isec = inode_security(inode); 3178db59000aSStephen Smalley if (has_cap_mac_admin(false)) 3179aa8e712cSStephen Smalley error = security_sid_to_context_force(&selinux_state, 3180aa8e712cSStephen Smalley isec->sid, &context, 3181abc69bb6SStephen Smalley &size); 3182abc69bb6SStephen Smalley else 3183aa8e712cSStephen Smalley error = security_sid_to_context(&selinux_state, isec->sid, 3184aa8e712cSStephen Smalley &context, &size); 318542492594SDavid P. Quigley if (error) 318642492594SDavid P. Quigley return error; 318742492594SDavid P. Quigley error = size; 318842492594SDavid P. Quigley if (alloc) { 318942492594SDavid P. Quigley *buffer = context; 319042492594SDavid P. Quigley goto out_nofree; 319142492594SDavid P. Quigley } 319242492594SDavid P. Quigley kfree(context); 319342492594SDavid P. Quigley out_nofree: 319442492594SDavid P. Quigley return error; 31951da177e4SLinus Torvalds } 31961da177e4SLinus Torvalds 31971da177e4SLinus Torvalds static int selinux_inode_setsecurity(struct inode *inode, const char *name, 31981da177e4SLinus Torvalds const void *value, size_t size, int flags) 31991da177e4SLinus Torvalds { 32002c97165bSPaul Moore struct inode_security_struct *isec = inode_security_novalidate(inode); 32011da177e4SLinus Torvalds u32 newsid; 32021da177e4SLinus Torvalds int rc; 32031da177e4SLinus Torvalds 32041da177e4SLinus Torvalds if (strcmp(name, XATTR_SELINUX_SUFFIX)) 32051da177e4SLinus Torvalds return -EOPNOTSUPP; 32061da177e4SLinus Torvalds 32071da177e4SLinus Torvalds if (!value || !size) 32081da177e4SLinus Torvalds return -EACCES; 32091da177e4SLinus Torvalds 3210aa8e712cSStephen Smalley rc = security_context_to_sid(&selinux_state, value, size, &newsid, 3211aa8e712cSStephen Smalley GFP_KERNEL); 32121da177e4SLinus Torvalds if (rc) 32131da177e4SLinus Torvalds return rc; 32141da177e4SLinus Torvalds 32159287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 3216aa9c2669SDavid Quigley isec->sclass = inode_mode_to_security_class(inode->i_mode); 32171da177e4SLinus Torvalds isec->sid = newsid; 32186f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 32199287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 32201da177e4SLinus Torvalds return 0; 32211da177e4SLinus Torvalds } 32221da177e4SLinus Torvalds 32231da177e4SLinus Torvalds static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size) 32241da177e4SLinus Torvalds { 32251da177e4SLinus Torvalds const int len = sizeof(XATTR_NAME_SELINUX); 32261da177e4SLinus Torvalds if (buffer && len <= buffer_size) 32271da177e4SLinus Torvalds memcpy(buffer, XATTR_NAME_SELINUX, len); 32281da177e4SLinus Torvalds return len; 32291da177e4SLinus Torvalds } 32301da177e4SLinus Torvalds 3231d6335d77SAndreas Gruenbacher static void selinux_inode_getsecid(struct inode *inode, u32 *secid) 3232713a04aeSAhmed S. Darwish { 3233e817c2f3SAndreas Gruenbacher struct inode_security_struct *isec = inode_security_novalidate(inode); 3234713a04aeSAhmed S. Darwish *secid = isec->sid; 3235713a04aeSAhmed S. Darwish } 3236713a04aeSAhmed S. Darwish 323756909eb3SVivek Goyal static int selinux_inode_copy_up(struct dentry *src, struct cred **new) 323856909eb3SVivek Goyal { 323956909eb3SVivek Goyal u32 sid; 324056909eb3SVivek Goyal struct task_security_struct *tsec; 324156909eb3SVivek Goyal struct cred *new_creds = *new; 324256909eb3SVivek Goyal 324356909eb3SVivek Goyal if (new_creds == NULL) { 324456909eb3SVivek Goyal new_creds = prepare_creds(); 324556909eb3SVivek Goyal if (!new_creds) 324656909eb3SVivek Goyal return -ENOMEM; 324756909eb3SVivek Goyal } 324856909eb3SVivek Goyal 32490c6cfa62SCasey Schaufler tsec = selinux_cred(new_creds); 325056909eb3SVivek Goyal /* Get label from overlay inode and set it in create_sid */ 325156909eb3SVivek Goyal selinux_inode_getsecid(d_inode(src), &sid); 325256909eb3SVivek Goyal tsec->create_sid = sid; 325356909eb3SVivek Goyal *new = new_creds; 325456909eb3SVivek Goyal return 0; 325556909eb3SVivek Goyal } 325656909eb3SVivek Goyal 325719472b69SVivek Goyal static int selinux_inode_copy_up_xattr(const char *name) 325819472b69SVivek Goyal { 325919472b69SVivek Goyal /* The copy_up hook above sets the initial context on an inode, but we 326019472b69SVivek Goyal * don't then want to overwrite it by blindly copying all the lower 326119472b69SVivek Goyal * xattrs up. Instead, we have to filter out SELinux-related xattrs. 326219472b69SVivek Goyal */ 326319472b69SVivek Goyal if (strcmp(name, XATTR_NAME_SELINUX) == 0) 326419472b69SVivek Goyal return 1; /* Discard */ 326519472b69SVivek Goyal /* 326619472b69SVivek Goyal * Any other attribute apart from SELINUX is not claimed, supported 326719472b69SVivek Goyal * by selinux. 326819472b69SVivek Goyal */ 326919472b69SVivek Goyal return -EOPNOTSUPP; 327019472b69SVivek Goyal } 327119472b69SVivek Goyal 32721da177e4SLinus Torvalds /* file security operations */ 32731da177e4SLinus Torvalds 3274788e7dd4SYuichi Nakamura static int selinux_revalidate_file_permission(struct file *file, int mask) 32751da177e4SLinus Torvalds { 327688e67f3bSDavid Howells const struct cred *cred = current_cred(); 3277496ad9aaSAl Viro struct inode *inode = file_inode(file); 32781da177e4SLinus Torvalds 32791da177e4SLinus Torvalds /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */ 32801da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE)) 32811da177e4SLinus Torvalds mask |= MAY_APPEND; 32821da177e4SLinus Torvalds 3283389fb800SPaul Moore return file_has_perm(cred, file, 32841da177e4SLinus Torvalds file_mask_to_av(inode->i_mode, mask)); 32851da177e4SLinus Torvalds } 32861da177e4SLinus Torvalds 3287788e7dd4SYuichi Nakamura static int selinux_file_permission(struct file *file, int mask) 3288788e7dd4SYuichi Nakamura { 3289496ad9aaSAl Viro struct inode *inode = file_inode(file); 3290bb6c6b02SCasey Schaufler struct file_security_struct *fsec = selinux_file(file); 3291b197367eSAndreas Gruenbacher struct inode_security_struct *isec; 329220dda18bSStephen Smalley u32 sid = current_sid(); 329320dda18bSStephen Smalley 3294389fb800SPaul Moore if (!mask) 3295788e7dd4SYuichi Nakamura /* No permission to check. Existence test. */ 3296788e7dd4SYuichi Nakamura return 0; 3297788e7dd4SYuichi Nakamura 3298b197367eSAndreas Gruenbacher isec = inode_security(inode); 329920dda18bSStephen Smalley if (sid == fsec->sid && fsec->isid == isec->sid && 33006b6bc620SStephen Smalley fsec->pseqno == avc_policy_seqno(&selinux_state)) 330183d49856SEric Paris /* No change since file_open check. */ 330220dda18bSStephen Smalley return 0; 330320dda18bSStephen Smalley 3304788e7dd4SYuichi Nakamura return selinux_revalidate_file_permission(file, mask); 3305788e7dd4SYuichi Nakamura } 3306788e7dd4SYuichi Nakamura 33071da177e4SLinus Torvalds static int selinux_file_alloc_security(struct file *file) 33081da177e4SLinus Torvalds { 33091da177e4SLinus Torvalds return file_alloc_security(file); 33101da177e4SLinus Torvalds } 33111da177e4SLinus Torvalds 3312fa1aa143SJeff Vander Stoep /* 3313fa1aa143SJeff Vander Stoep * Check whether a task has the ioctl permission and cmd 3314fa1aa143SJeff Vander Stoep * operation to an inode. 3315fa1aa143SJeff Vander Stoep */ 33161d2a168aSGeliang Tang static int ioctl_has_perm(const struct cred *cred, struct file *file, 3317fa1aa143SJeff Vander Stoep u32 requested, u16 cmd) 3318fa1aa143SJeff Vander Stoep { 3319fa1aa143SJeff Vander Stoep struct common_audit_data ad; 3320bb6c6b02SCasey Schaufler struct file_security_struct *fsec = selinux_file(file); 3321fa1aa143SJeff Vander Stoep struct inode *inode = file_inode(file); 332220cdef8dSPaul Moore struct inode_security_struct *isec; 3323fa1aa143SJeff Vander Stoep struct lsm_ioctlop_audit ioctl; 3324fa1aa143SJeff Vander Stoep u32 ssid = cred_sid(cred); 3325fa1aa143SJeff Vander Stoep int rc; 3326fa1aa143SJeff Vander Stoep u8 driver = cmd >> 8; 3327fa1aa143SJeff Vander Stoep u8 xperm = cmd & 0xff; 3328fa1aa143SJeff Vander Stoep 3329fa1aa143SJeff Vander Stoep ad.type = LSM_AUDIT_DATA_IOCTL_OP; 3330fa1aa143SJeff Vander Stoep ad.u.op = &ioctl; 3331fa1aa143SJeff Vander Stoep ad.u.op->cmd = cmd; 3332fa1aa143SJeff Vander Stoep ad.u.op->path = file->f_path; 3333fa1aa143SJeff Vander Stoep 3334fa1aa143SJeff Vander Stoep if (ssid != fsec->sid) { 33356b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 33366b6bc620SStephen Smalley ssid, fsec->sid, 3337fa1aa143SJeff Vander Stoep SECCLASS_FD, 3338fa1aa143SJeff Vander Stoep FD__USE, 3339fa1aa143SJeff Vander Stoep &ad); 3340fa1aa143SJeff Vander Stoep if (rc) 3341fa1aa143SJeff Vander Stoep goto out; 3342fa1aa143SJeff Vander Stoep } 3343fa1aa143SJeff Vander Stoep 3344fa1aa143SJeff Vander Stoep if (unlikely(IS_PRIVATE(inode))) 3345fa1aa143SJeff Vander Stoep return 0; 3346fa1aa143SJeff Vander Stoep 334720cdef8dSPaul Moore isec = inode_security(inode); 33486b6bc620SStephen Smalley rc = avc_has_extended_perms(&selinux_state, 33496b6bc620SStephen Smalley ssid, isec->sid, isec->sclass, 3350fa1aa143SJeff Vander Stoep requested, driver, xperm, &ad); 3351fa1aa143SJeff Vander Stoep out: 3352fa1aa143SJeff Vander Stoep return rc; 3353fa1aa143SJeff Vander Stoep } 3354fa1aa143SJeff Vander Stoep 33551da177e4SLinus Torvalds static int selinux_file_ioctl(struct file *file, unsigned int cmd, 33561da177e4SLinus Torvalds unsigned long arg) 33571da177e4SLinus Torvalds { 335888e67f3bSDavid Howells const struct cred *cred = current_cred(); 33590b24dcb7SEric Paris int error = 0; 33601da177e4SLinus Torvalds 33610b24dcb7SEric Paris switch (cmd) { 33620b24dcb7SEric Paris case FIONREAD: 33630b24dcb7SEric Paris /* fall through */ 33640b24dcb7SEric Paris case FIBMAP: 33650b24dcb7SEric Paris /* fall through */ 33660b24dcb7SEric Paris case FIGETBSZ: 33670b24dcb7SEric Paris /* fall through */ 33682f99c369SAl Viro case FS_IOC_GETFLAGS: 33690b24dcb7SEric Paris /* fall through */ 33702f99c369SAl Viro case FS_IOC_GETVERSION: 33710b24dcb7SEric Paris error = file_has_perm(cred, file, FILE__GETATTR); 33720b24dcb7SEric Paris break; 33731da177e4SLinus Torvalds 33742f99c369SAl Viro case FS_IOC_SETFLAGS: 33750b24dcb7SEric Paris /* fall through */ 33762f99c369SAl Viro case FS_IOC_SETVERSION: 33770b24dcb7SEric Paris error = file_has_perm(cred, file, FILE__SETATTR); 33780b24dcb7SEric Paris break; 33790b24dcb7SEric Paris 33800b24dcb7SEric Paris /* sys_ioctl() checks */ 33810b24dcb7SEric Paris case FIONBIO: 33820b24dcb7SEric Paris /* fall through */ 33830b24dcb7SEric Paris case FIOASYNC: 33840b24dcb7SEric Paris error = file_has_perm(cred, file, 0); 33850b24dcb7SEric Paris break; 33860b24dcb7SEric Paris 33870b24dcb7SEric Paris case KDSKBENT: 33880b24dcb7SEric Paris case KDSKBSENT: 33896a9de491SEric Paris error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG, 33908e4ff6f2SStephen Smalley SECURITY_CAP_AUDIT, true); 33910b24dcb7SEric Paris break; 33920b24dcb7SEric Paris 33930b24dcb7SEric Paris /* default case assumes that the command will go 33940b24dcb7SEric Paris * to the file's ioctl() function. 33950b24dcb7SEric Paris */ 33960b24dcb7SEric Paris default: 3397fa1aa143SJeff Vander Stoep error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd); 33980b24dcb7SEric Paris } 33990b24dcb7SEric Paris return error; 34001da177e4SLinus Torvalds } 34011da177e4SLinus Torvalds 3402fcaaade1SStephen Smalley static int default_noexec; 3403fcaaade1SStephen Smalley 34041da177e4SLinus Torvalds static int file_map_prot_check(struct file *file, unsigned long prot, int shared) 34051da177e4SLinus Torvalds { 340688e67f3bSDavid Howells const struct cred *cred = current_cred(); 3407be0554c9SStephen Smalley u32 sid = cred_sid(cred); 3408d84f4f99SDavid Howells int rc = 0; 340988e67f3bSDavid Howells 3410fcaaade1SStephen Smalley if (default_noexec && 3411892e8cacSStephen Smalley (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) || 3412892e8cacSStephen Smalley (!shared && (prot & PROT_WRITE)))) { 34131da177e4SLinus Torvalds /* 34141da177e4SLinus Torvalds * We are making executable an anonymous mapping or a 34151da177e4SLinus Torvalds * private file mapping that will also be writable. 34161da177e4SLinus Torvalds * This has an additional check. 34171da177e4SLinus Torvalds */ 34186b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 34196b6bc620SStephen Smalley sid, sid, SECCLASS_PROCESS, 3420be0554c9SStephen Smalley PROCESS__EXECMEM, NULL); 34211da177e4SLinus Torvalds if (rc) 3422d84f4f99SDavid Howells goto error; 34231da177e4SLinus Torvalds } 34241da177e4SLinus Torvalds 34251da177e4SLinus Torvalds if (file) { 34261da177e4SLinus Torvalds /* read access is always possible with a mapping */ 34271da177e4SLinus Torvalds u32 av = FILE__READ; 34281da177e4SLinus Torvalds 34291da177e4SLinus Torvalds /* write access only matters if the mapping is shared */ 34301da177e4SLinus Torvalds if (shared && (prot & PROT_WRITE)) 34311da177e4SLinus Torvalds av |= FILE__WRITE; 34321da177e4SLinus Torvalds 34331da177e4SLinus Torvalds if (prot & PROT_EXEC) 34341da177e4SLinus Torvalds av |= FILE__EXECUTE; 34351da177e4SLinus Torvalds 343688e67f3bSDavid Howells return file_has_perm(cred, file, av); 34371da177e4SLinus Torvalds } 3438d84f4f99SDavid Howells 3439d84f4f99SDavid Howells error: 3440d84f4f99SDavid Howells return rc; 34411da177e4SLinus Torvalds } 34421da177e4SLinus Torvalds 3443e5467859SAl Viro static int selinux_mmap_addr(unsigned long addr) 34441da177e4SLinus Torvalds { 3445b1d9e6b0SCasey Schaufler int rc = 0; 344698883bfdSPaul Moore 344798883bfdSPaul Moore if (addr < CONFIG_LSM_MMAP_MIN_ADDR) { 344898883bfdSPaul Moore u32 sid = current_sid(); 34496b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 34506b6bc620SStephen Smalley sid, sid, SECCLASS_MEMPROTECT, 345198883bfdSPaul Moore MEMPROTECT__MMAP_ZERO, NULL); 345298883bfdSPaul Moore } 345398883bfdSPaul Moore 345498883bfdSPaul Moore return rc; 3455e5467859SAl Viro } 34561da177e4SLinus Torvalds 3457e5467859SAl Viro static int selinux_mmap_file(struct file *file, unsigned long reqprot, 3458e5467859SAl Viro unsigned long prot, unsigned long flags) 3459e5467859SAl Viro { 34603ba4bf5fSStephen Smalley struct common_audit_data ad; 34613ba4bf5fSStephen Smalley int rc; 34623ba4bf5fSStephen Smalley 34633ba4bf5fSStephen Smalley if (file) { 34643ba4bf5fSStephen Smalley ad.type = LSM_AUDIT_DATA_FILE; 34653ba4bf5fSStephen Smalley ad.u.file = file; 34663ba4bf5fSStephen Smalley rc = inode_has_perm(current_cred(), file_inode(file), 34673ba4bf5fSStephen Smalley FILE__MAP, &ad); 34683ba4bf5fSStephen Smalley if (rc) 34693ba4bf5fSStephen Smalley return rc; 34703ba4bf5fSStephen Smalley } 34713ba4bf5fSStephen Smalley 3472aa8e712cSStephen Smalley if (selinux_state.checkreqprot) 34731da177e4SLinus Torvalds prot = reqprot; 34741da177e4SLinus Torvalds 34751da177e4SLinus Torvalds return file_map_prot_check(file, prot, 34761da177e4SLinus Torvalds (flags & MAP_TYPE) == MAP_SHARED); 34771da177e4SLinus Torvalds } 34781da177e4SLinus Torvalds 34791da177e4SLinus Torvalds static int selinux_file_mprotect(struct vm_area_struct *vma, 34801da177e4SLinus Torvalds unsigned long reqprot, 34811da177e4SLinus Torvalds unsigned long prot) 34821da177e4SLinus Torvalds { 348388e67f3bSDavid Howells const struct cred *cred = current_cred(); 3484be0554c9SStephen Smalley u32 sid = cred_sid(cred); 34851da177e4SLinus Torvalds 3486aa8e712cSStephen Smalley if (selinux_state.checkreqprot) 34871da177e4SLinus Torvalds prot = reqprot; 34881da177e4SLinus Torvalds 3489fcaaade1SStephen Smalley if (default_noexec && 3490fcaaade1SStephen Smalley (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) { 3491d541bbeeSJames Morris int rc = 0; 3492db4c9641SStephen Smalley if (vma->vm_start >= vma->vm_mm->start_brk && 3493db4c9641SStephen Smalley vma->vm_end <= vma->vm_mm->brk) { 34946b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 34956b6bc620SStephen Smalley sid, sid, SECCLASS_PROCESS, 3496be0554c9SStephen Smalley PROCESS__EXECHEAP, NULL); 3497db4c9641SStephen Smalley } else if (!vma->vm_file && 3498c2316dbfSStephen Smalley ((vma->vm_start <= vma->vm_mm->start_stack && 3499c2316dbfSStephen Smalley vma->vm_end >= vma->vm_mm->start_stack) || 3500d17af505SAndy Lutomirski vma_is_stack_for_current(vma))) { 35016b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 35026b6bc620SStephen Smalley sid, sid, SECCLASS_PROCESS, 3503be0554c9SStephen Smalley PROCESS__EXECSTACK, NULL); 3504db4c9641SStephen Smalley } else if (vma->vm_file && vma->anon_vma) { 3505db4c9641SStephen Smalley /* 3506db4c9641SStephen Smalley * We are making executable a file mapping that has 3507db4c9641SStephen Smalley * had some COW done. Since pages might have been 3508db4c9641SStephen Smalley * written, check ability to execute the possibly 3509db4c9641SStephen Smalley * modified content. This typically should only 3510db4c9641SStephen Smalley * occur for text relocations. 3511db4c9641SStephen Smalley */ 3512d84f4f99SDavid Howells rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD); 3513db4c9641SStephen Smalley } 35146b992197SLorenzo Hernandez García-Hierro if (rc) 35156b992197SLorenzo Hernandez García-Hierro return rc; 35166b992197SLorenzo Hernandez García-Hierro } 35171da177e4SLinus Torvalds 35181da177e4SLinus Torvalds return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED); 35191da177e4SLinus Torvalds } 35201da177e4SLinus Torvalds 35211da177e4SLinus Torvalds static int selinux_file_lock(struct file *file, unsigned int cmd) 35221da177e4SLinus Torvalds { 352388e67f3bSDavid Howells const struct cred *cred = current_cred(); 352488e67f3bSDavid Howells 352588e67f3bSDavid Howells return file_has_perm(cred, file, FILE__LOCK); 35261da177e4SLinus Torvalds } 35271da177e4SLinus Torvalds 35281da177e4SLinus Torvalds static int selinux_file_fcntl(struct file *file, unsigned int cmd, 35291da177e4SLinus Torvalds unsigned long arg) 35301da177e4SLinus Torvalds { 353188e67f3bSDavid Howells const struct cred *cred = current_cred(); 35321da177e4SLinus Torvalds int err = 0; 35331da177e4SLinus Torvalds 35341da177e4SLinus Torvalds switch (cmd) { 35351da177e4SLinus Torvalds case F_SETFL: 35361da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) { 353788e67f3bSDavid Howells err = file_has_perm(cred, file, FILE__WRITE); 35381da177e4SLinus Torvalds break; 35391da177e4SLinus Torvalds } 35401da177e4SLinus Torvalds /* fall through */ 35411da177e4SLinus Torvalds case F_SETOWN: 35421da177e4SLinus Torvalds case F_SETSIG: 35431da177e4SLinus Torvalds case F_GETFL: 35441da177e4SLinus Torvalds case F_GETOWN: 35451da177e4SLinus Torvalds case F_GETSIG: 35461d151c33SCyrill Gorcunov case F_GETOWNER_UIDS: 35471da177e4SLinus Torvalds /* Just check FD__USE permission */ 354888e67f3bSDavid Howells err = file_has_perm(cred, file, 0); 35491da177e4SLinus Torvalds break; 35501da177e4SLinus Torvalds case F_GETLK: 35511da177e4SLinus Torvalds case F_SETLK: 35521da177e4SLinus Torvalds case F_SETLKW: 35530d3f7a2dSJeff Layton case F_OFD_GETLK: 35540d3f7a2dSJeff Layton case F_OFD_SETLK: 35550d3f7a2dSJeff Layton case F_OFD_SETLKW: 35561da177e4SLinus Torvalds #if BITS_PER_LONG == 32 35571da177e4SLinus Torvalds case F_GETLK64: 35581da177e4SLinus Torvalds case F_SETLK64: 35591da177e4SLinus Torvalds case F_SETLKW64: 35601da177e4SLinus Torvalds #endif 356188e67f3bSDavid Howells err = file_has_perm(cred, file, FILE__LOCK); 35621da177e4SLinus Torvalds break; 35631da177e4SLinus Torvalds } 35641da177e4SLinus Torvalds 35651da177e4SLinus Torvalds return err; 35661da177e4SLinus Torvalds } 35671da177e4SLinus Torvalds 3568e0b93eddSJeff Layton static void selinux_file_set_fowner(struct file *file) 35691da177e4SLinus Torvalds { 35701da177e4SLinus Torvalds struct file_security_struct *fsec; 35711da177e4SLinus Torvalds 3572bb6c6b02SCasey Schaufler fsec = selinux_file(file); 3573275bb41eSDavid Howells fsec->fown_sid = current_sid(); 35741da177e4SLinus Torvalds } 35751da177e4SLinus Torvalds 35761da177e4SLinus Torvalds static int selinux_file_send_sigiotask(struct task_struct *tsk, 35771da177e4SLinus Torvalds struct fown_struct *fown, int signum) 35781da177e4SLinus Torvalds { 35791da177e4SLinus Torvalds struct file *file; 358065c90bcaSStephen Smalley u32 sid = task_sid(tsk); 35811da177e4SLinus Torvalds u32 perm; 35821da177e4SLinus Torvalds struct file_security_struct *fsec; 35831da177e4SLinus Torvalds 35841da177e4SLinus Torvalds /* struct fown_struct is never outside the context of a struct file */ 3585b385a144SRobert P. J. Day file = container_of(fown, struct file, f_owner); 35861da177e4SLinus Torvalds 3587bb6c6b02SCasey Schaufler fsec = selinux_file(file); 35881da177e4SLinus Torvalds 35891da177e4SLinus Torvalds if (!signum) 35901da177e4SLinus Torvalds perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */ 35911da177e4SLinus Torvalds else 35921da177e4SLinus Torvalds perm = signal_to_av(signum); 35931da177e4SLinus Torvalds 35946b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 35956b6bc620SStephen Smalley fsec->fown_sid, sid, 35961da177e4SLinus Torvalds SECCLASS_PROCESS, perm, NULL); 35971da177e4SLinus Torvalds } 35981da177e4SLinus Torvalds 35991da177e4SLinus Torvalds static int selinux_file_receive(struct file *file) 36001da177e4SLinus Torvalds { 360188e67f3bSDavid Howells const struct cred *cred = current_cred(); 360288e67f3bSDavid Howells 360388e67f3bSDavid Howells return file_has_perm(cred, file, file_to_av(file)); 36041da177e4SLinus Torvalds } 36051da177e4SLinus Torvalds 360694817692SAl Viro static int selinux_file_open(struct file *file) 3607788e7dd4SYuichi Nakamura { 3608788e7dd4SYuichi Nakamura struct file_security_struct *fsec; 3609788e7dd4SYuichi Nakamura struct inode_security_struct *isec; 3610d84f4f99SDavid Howells 3611bb6c6b02SCasey Schaufler fsec = selinux_file(file); 361283da53c5SAndreas Gruenbacher isec = inode_security(file_inode(file)); 3613788e7dd4SYuichi Nakamura /* 3614788e7dd4SYuichi Nakamura * Save inode label and policy sequence number 3615788e7dd4SYuichi Nakamura * at open-time so that selinux_file_permission 3616788e7dd4SYuichi Nakamura * can determine whether revalidation is necessary. 3617788e7dd4SYuichi Nakamura * Task label is already saved in the file security 3618788e7dd4SYuichi Nakamura * struct as its SID. 3619788e7dd4SYuichi Nakamura */ 3620788e7dd4SYuichi Nakamura fsec->isid = isec->sid; 36216b6bc620SStephen Smalley fsec->pseqno = avc_policy_seqno(&selinux_state); 3622788e7dd4SYuichi Nakamura /* 3623788e7dd4SYuichi Nakamura * Since the inode label or policy seqno may have changed 3624788e7dd4SYuichi Nakamura * between the selinux_inode_permission check and the saving 3625788e7dd4SYuichi Nakamura * of state above, recheck that access is still permitted. 3626788e7dd4SYuichi Nakamura * Otherwise, access might never be revalidated against the 3627788e7dd4SYuichi Nakamura * new inode label or new policy. 3628788e7dd4SYuichi Nakamura * This check is not redundant - do not remove. 3629788e7dd4SYuichi Nakamura */ 363094817692SAl Viro return file_path_has_perm(file->f_cred, file, open_file_to_av(file)); 3631788e7dd4SYuichi Nakamura } 3632788e7dd4SYuichi Nakamura 36331da177e4SLinus Torvalds /* task security operations */ 36341da177e4SLinus Torvalds 3635a79be238STetsuo Handa static int selinux_task_alloc(struct task_struct *task, 3636a79be238STetsuo Handa unsigned long clone_flags) 36371da177e4SLinus Torvalds { 3638be0554c9SStephen Smalley u32 sid = current_sid(); 3639be0554c9SStephen Smalley 36406b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 36416b6bc620SStephen Smalley sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL); 36421da177e4SLinus Torvalds } 36431da177e4SLinus Torvalds 3644f1752eecSDavid Howells /* 3645d84f4f99SDavid Howells * prepare a new set of credentials for modification 3646d84f4f99SDavid Howells */ 3647d84f4f99SDavid Howells static int selinux_cred_prepare(struct cred *new, const struct cred *old, 3648d84f4f99SDavid Howells gfp_t gfp) 3649d84f4f99SDavid Howells { 3650bbd3662aSCasey Schaufler const struct task_security_struct *old_tsec = selinux_cred(old); 3651bbd3662aSCasey Schaufler struct task_security_struct *tsec = selinux_cred(new); 3652d84f4f99SDavid Howells 3653bbd3662aSCasey Schaufler *tsec = *old_tsec; 3654d84f4f99SDavid Howells return 0; 3655d84f4f99SDavid Howells } 3656d84f4f99SDavid Howells 3657d84f4f99SDavid Howells /* 3658ee18d64cSDavid Howells * transfer the SELinux data to a blank set of creds 3659ee18d64cSDavid Howells */ 3660ee18d64cSDavid Howells static void selinux_cred_transfer(struct cred *new, const struct cred *old) 3661ee18d64cSDavid Howells { 36620c6cfa62SCasey Schaufler const struct task_security_struct *old_tsec = selinux_cred(old); 36630c6cfa62SCasey Schaufler struct task_security_struct *tsec = selinux_cred(new); 3664ee18d64cSDavid Howells 3665ee18d64cSDavid Howells *tsec = *old_tsec; 3666ee18d64cSDavid Howells } 3667ee18d64cSDavid Howells 36683ec30113SMatthew Garrett static void selinux_cred_getsecid(const struct cred *c, u32 *secid) 36693ec30113SMatthew Garrett { 36703ec30113SMatthew Garrett *secid = cred_sid(c); 36713ec30113SMatthew Garrett } 36723ec30113SMatthew Garrett 3673ee18d64cSDavid Howells /* 36743a3b7ce9SDavid Howells * set the security data for a kernel service 36753a3b7ce9SDavid Howells * - all the creation contexts are set to unlabelled 36763a3b7ce9SDavid Howells */ 36773a3b7ce9SDavid Howells static int selinux_kernel_act_as(struct cred *new, u32 secid) 36783a3b7ce9SDavid Howells { 36790c6cfa62SCasey Schaufler struct task_security_struct *tsec = selinux_cred(new); 36803a3b7ce9SDavid Howells u32 sid = current_sid(); 36813a3b7ce9SDavid Howells int ret; 36823a3b7ce9SDavid Howells 36836b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 36846b6bc620SStephen Smalley sid, secid, 36853a3b7ce9SDavid Howells SECCLASS_KERNEL_SERVICE, 36863a3b7ce9SDavid Howells KERNEL_SERVICE__USE_AS_OVERRIDE, 36873a3b7ce9SDavid Howells NULL); 36883a3b7ce9SDavid Howells if (ret == 0) { 36893a3b7ce9SDavid Howells tsec->sid = secid; 36903a3b7ce9SDavid Howells tsec->create_sid = 0; 36913a3b7ce9SDavid Howells tsec->keycreate_sid = 0; 36923a3b7ce9SDavid Howells tsec->sockcreate_sid = 0; 36933a3b7ce9SDavid Howells } 36943a3b7ce9SDavid Howells return ret; 36953a3b7ce9SDavid Howells } 36963a3b7ce9SDavid Howells 36973a3b7ce9SDavid Howells /* 36983a3b7ce9SDavid Howells * set the file creation context in a security record to the same as the 36993a3b7ce9SDavid Howells * objective context of the specified inode 37003a3b7ce9SDavid Howells */ 37013a3b7ce9SDavid Howells static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode) 37023a3b7ce9SDavid Howells { 370383da53c5SAndreas Gruenbacher struct inode_security_struct *isec = inode_security(inode); 37040c6cfa62SCasey Schaufler struct task_security_struct *tsec = selinux_cred(new); 37053a3b7ce9SDavid Howells u32 sid = current_sid(); 37063a3b7ce9SDavid Howells int ret; 37073a3b7ce9SDavid Howells 37086b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 37096b6bc620SStephen Smalley sid, isec->sid, 37103a3b7ce9SDavid Howells SECCLASS_KERNEL_SERVICE, 37113a3b7ce9SDavid Howells KERNEL_SERVICE__CREATE_FILES_AS, 37123a3b7ce9SDavid Howells NULL); 37133a3b7ce9SDavid Howells 37143a3b7ce9SDavid Howells if (ret == 0) 37153a3b7ce9SDavid Howells tsec->create_sid = isec->sid; 3716ef57471aSDavid Howells return ret; 37173a3b7ce9SDavid Howells } 37183a3b7ce9SDavid Howells 3719dd8dbf2eSEric Paris static int selinux_kernel_module_request(char *kmod_name) 372025354c4fSEric Paris { 3721dd8dbf2eSEric Paris struct common_audit_data ad; 3722dd8dbf2eSEric Paris 372350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_KMOD; 3724dd8dbf2eSEric Paris ad.u.kmod_name = kmod_name; 3725dd8dbf2eSEric Paris 37266b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 37276b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM, 3728dd8dbf2eSEric Paris SYSTEM__MODULE_REQUEST, &ad); 372925354c4fSEric Paris } 373025354c4fSEric Paris 373161d612eaSJeff Vander Stoep static int selinux_kernel_module_from_file(struct file *file) 373261d612eaSJeff Vander Stoep { 373361d612eaSJeff Vander Stoep struct common_audit_data ad; 373461d612eaSJeff Vander Stoep struct inode_security_struct *isec; 373561d612eaSJeff Vander Stoep struct file_security_struct *fsec; 373661d612eaSJeff Vander Stoep u32 sid = current_sid(); 373761d612eaSJeff Vander Stoep int rc; 373861d612eaSJeff Vander Stoep 373961d612eaSJeff Vander Stoep /* init_module */ 374061d612eaSJeff Vander Stoep if (file == NULL) 37416b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 37426b6bc620SStephen Smalley sid, sid, SECCLASS_SYSTEM, 374361d612eaSJeff Vander Stoep SYSTEM__MODULE_LOAD, NULL); 374461d612eaSJeff Vander Stoep 374561d612eaSJeff Vander Stoep /* finit_module */ 374620cdef8dSPaul Moore 374743af5de7SVivek Goyal ad.type = LSM_AUDIT_DATA_FILE; 374843af5de7SVivek Goyal ad.u.file = file; 374961d612eaSJeff Vander Stoep 3750bb6c6b02SCasey Schaufler fsec = selinux_file(file); 375161d612eaSJeff Vander Stoep if (sid != fsec->sid) { 37526b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 37536b6bc620SStephen Smalley sid, fsec->sid, SECCLASS_FD, FD__USE, &ad); 375461d612eaSJeff Vander Stoep if (rc) 375561d612eaSJeff Vander Stoep return rc; 375661d612eaSJeff Vander Stoep } 375761d612eaSJeff Vander Stoep 375820cdef8dSPaul Moore isec = inode_security(file_inode(file)); 37596b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 37606b6bc620SStephen Smalley sid, isec->sid, SECCLASS_SYSTEM, 376161d612eaSJeff Vander Stoep SYSTEM__MODULE_LOAD, &ad); 376261d612eaSJeff Vander Stoep } 376361d612eaSJeff Vander Stoep 376461d612eaSJeff Vander Stoep static int selinux_kernel_read_file(struct file *file, 376561d612eaSJeff Vander Stoep enum kernel_read_file_id id) 376661d612eaSJeff Vander Stoep { 376761d612eaSJeff Vander Stoep int rc = 0; 376861d612eaSJeff Vander Stoep 376961d612eaSJeff Vander Stoep switch (id) { 377061d612eaSJeff Vander Stoep case READING_MODULE: 377161d612eaSJeff Vander Stoep rc = selinux_kernel_module_from_file(file); 377261d612eaSJeff Vander Stoep break; 377361d612eaSJeff Vander Stoep default: 377461d612eaSJeff Vander Stoep break; 377561d612eaSJeff Vander Stoep } 377661d612eaSJeff Vander Stoep 377761d612eaSJeff Vander Stoep return rc; 377861d612eaSJeff Vander Stoep } 377961d612eaSJeff Vander Stoep 3780c77b8cdfSMimi Zohar static int selinux_kernel_load_data(enum kernel_load_data_id id) 3781c77b8cdfSMimi Zohar { 3782c77b8cdfSMimi Zohar int rc = 0; 3783c77b8cdfSMimi Zohar 3784c77b8cdfSMimi Zohar switch (id) { 3785c77b8cdfSMimi Zohar case LOADING_MODULE: 3786c77b8cdfSMimi Zohar rc = selinux_kernel_module_from_file(NULL); 3787c77b8cdfSMimi Zohar default: 3788c77b8cdfSMimi Zohar break; 3789c77b8cdfSMimi Zohar } 3790c77b8cdfSMimi Zohar 3791c77b8cdfSMimi Zohar return rc; 3792c77b8cdfSMimi Zohar } 3793c77b8cdfSMimi Zohar 37941da177e4SLinus Torvalds static int selinux_task_setpgid(struct task_struct *p, pid_t pgid) 37951da177e4SLinus Torvalds { 37966b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 37976b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 3798be0554c9SStephen Smalley PROCESS__SETPGID, NULL); 37991da177e4SLinus Torvalds } 38001da177e4SLinus Torvalds 38011da177e4SLinus Torvalds static int selinux_task_getpgid(struct task_struct *p) 38021da177e4SLinus Torvalds { 38036b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 38046b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 3805be0554c9SStephen Smalley PROCESS__GETPGID, NULL); 38061da177e4SLinus Torvalds } 38071da177e4SLinus Torvalds 38081da177e4SLinus Torvalds static int selinux_task_getsid(struct task_struct *p) 38091da177e4SLinus Torvalds { 38106b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 38116b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 3812be0554c9SStephen Smalley PROCESS__GETSESSION, NULL); 38131da177e4SLinus Torvalds } 38141da177e4SLinus Torvalds 3815f9008e4cSDavid Quigley static void selinux_task_getsecid(struct task_struct *p, u32 *secid) 3816f9008e4cSDavid Quigley { 3817275bb41eSDavid Howells *secid = task_sid(p); 3818f9008e4cSDavid Quigley } 3819f9008e4cSDavid Quigley 38201da177e4SLinus Torvalds static int selinux_task_setnice(struct task_struct *p, int nice) 38211da177e4SLinus Torvalds { 38226b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 38236b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 3824be0554c9SStephen Smalley PROCESS__SETSCHED, NULL); 38251da177e4SLinus Torvalds } 38261da177e4SLinus Torvalds 382703e68060SJames Morris static int selinux_task_setioprio(struct task_struct *p, int ioprio) 382803e68060SJames Morris { 38296b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 38306b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 3831be0554c9SStephen Smalley PROCESS__SETSCHED, NULL); 383203e68060SJames Morris } 383303e68060SJames Morris 3834a1836a42SDavid Quigley static int selinux_task_getioprio(struct task_struct *p) 3835a1836a42SDavid Quigley { 38366b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 38376b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 3838be0554c9SStephen Smalley PROCESS__GETSCHED, NULL); 3839a1836a42SDavid Quigley } 3840a1836a42SDavid Quigley 38414298555dSCorentin LABBE static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred, 3842791ec491SStephen Smalley unsigned int flags) 3843791ec491SStephen Smalley { 3844791ec491SStephen Smalley u32 av = 0; 3845791ec491SStephen Smalley 384684e6885eSStephen Smalley if (!flags) 384784e6885eSStephen Smalley return 0; 3848791ec491SStephen Smalley if (flags & LSM_PRLIMIT_WRITE) 3849791ec491SStephen Smalley av |= PROCESS__SETRLIMIT; 3850791ec491SStephen Smalley if (flags & LSM_PRLIMIT_READ) 3851791ec491SStephen Smalley av |= PROCESS__GETRLIMIT; 38526b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 38536b6bc620SStephen Smalley cred_sid(cred), cred_sid(tcred), 3854791ec491SStephen Smalley SECCLASS_PROCESS, av, NULL); 3855791ec491SStephen Smalley } 3856791ec491SStephen Smalley 38578fd00b4dSJiri Slaby static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource, 38588fd00b4dSJiri Slaby struct rlimit *new_rlim) 38591da177e4SLinus Torvalds { 38608fd00b4dSJiri Slaby struct rlimit *old_rlim = p->signal->rlim + resource; 38611da177e4SLinus Torvalds 38621da177e4SLinus Torvalds /* Control the ability to change the hard limit (whether 38631da177e4SLinus Torvalds lowering or raising it), so that the hard limit can 38641da177e4SLinus Torvalds later be used as a safe reset point for the soft limit 3865d84f4f99SDavid Howells upon context transitions. See selinux_bprm_committing_creds. */ 38661da177e4SLinus Torvalds if (old_rlim->rlim_max != new_rlim->rlim_max) 38676b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 38686b6bc620SStephen Smalley current_sid(), task_sid(p), 3869be0554c9SStephen Smalley SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL); 38701da177e4SLinus Torvalds 38711da177e4SLinus Torvalds return 0; 38721da177e4SLinus Torvalds } 38731da177e4SLinus Torvalds 3874b0ae1981SKOSAKI Motohiro static int selinux_task_setscheduler(struct task_struct *p) 38751da177e4SLinus Torvalds { 38766b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 38776b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 3878be0554c9SStephen Smalley PROCESS__SETSCHED, NULL); 38791da177e4SLinus Torvalds } 38801da177e4SLinus Torvalds 38811da177e4SLinus Torvalds static int selinux_task_getscheduler(struct task_struct *p) 38821da177e4SLinus Torvalds { 38836b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 38846b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 3885be0554c9SStephen Smalley PROCESS__GETSCHED, NULL); 38861da177e4SLinus Torvalds } 38871da177e4SLinus Torvalds 388835601547SDavid Quigley static int selinux_task_movememory(struct task_struct *p) 388935601547SDavid Quigley { 38906b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 38916b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 3892be0554c9SStephen Smalley PROCESS__SETSCHED, NULL); 389335601547SDavid Quigley } 389435601547SDavid Quigley 3895ae7795bcSEric W. Biederman static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info, 38966b4f3d01SStephen Smalley int sig, const struct cred *cred) 38971da177e4SLinus Torvalds { 38986b4f3d01SStephen Smalley u32 secid; 38991da177e4SLinus Torvalds u32 perm; 39001da177e4SLinus Torvalds 39011da177e4SLinus Torvalds if (!sig) 39021da177e4SLinus Torvalds perm = PROCESS__SIGNULL; /* null signal; existence test */ 39031da177e4SLinus Torvalds else 39041da177e4SLinus Torvalds perm = signal_to_av(sig); 39056b4f3d01SStephen Smalley if (!cred) 3906be0554c9SStephen Smalley secid = current_sid(); 39076b4f3d01SStephen Smalley else 39086b4f3d01SStephen Smalley secid = cred_sid(cred); 39096b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 39106b6bc620SStephen Smalley secid, task_sid(p), SECCLASS_PROCESS, perm, NULL); 39111da177e4SLinus Torvalds } 39121da177e4SLinus Torvalds 39131da177e4SLinus Torvalds static void selinux_task_to_inode(struct task_struct *p, 39141da177e4SLinus Torvalds struct inode *inode) 39151da177e4SLinus Torvalds { 391680788c22SCasey Schaufler struct inode_security_struct *isec = selinux_inode(inode); 3917275bb41eSDavid Howells u32 sid = task_sid(p); 39181da177e4SLinus Torvalds 39199287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 3920db978da8SAndreas Gruenbacher isec->sclass = inode_mode_to_security_class(inode->i_mode); 3921275bb41eSDavid Howells isec->sid = sid; 39226f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 39239287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 39241da177e4SLinus Torvalds } 39251da177e4SLinus Torvalds 39261da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 392767f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv4(struct sk_buff *skb, 39282bf49690SThomas Liu struct common_audit_data *ad, u8 *proto) 39291da177e4SLinus Torvalds { 39301da177e4SLinus Torvalds int offset, ihlen, ret = -EINVAL; 39311da177e4SLinus Torvalds struct iphdr _iph, *ih; 39321da177e4SLinus Torvalds 3933bbe735e4SArnaldo Carvalho de Melo offset = skb_network_offset(skb); 39341da177e4SLinus Torvalds ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph); 39351da177e4SLinus Torvalds if (ih == NULL) 39361da177e4SLinus Torvalds goto out; 39371da177e4SLinus Torvalds 39381da177e4SLinus Torvalds ihlen = ih->ihl * 4; 39391da177e4SLinus Torvalds if (ihlen < sizeof(_iph)) 39401da177e4SLinus Torvalds goto out; 39411da177e4SLinus Torvalds 394248c62af6SEric Paris ad->u.net->v4info.saddr = ih->saddr; 394348c62af6SEric Paris ad->u.net->v4info.daddr = ih->daddr; 39441da177e4SLinus Torvalds ret = 0; 39451da177e4SLinus Torvalds 394667f83cbfSVenkat Yekkirala if (proto) 394767f83cbfSVenkat Yekkirala *proto = ih->protocol; 394867f83cbfSVenkat Yekkirala 39491da177e4SLinus Torvalds switch (ih->protocol) { 39501da177e4SLinus Torvalds case IPPROTO_TCP: { 39511da177e4SLinus Torvalds struct tcphdr _tcph, *th; 39521da177e4SLinus Torvalds 39531da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 39541da177e4SLinus Torvalds break; 39551da177e4SLinus Torvalds 39561da177e4SLinus Torvalds offset += ihlen; 39571da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 39581da177e4SLinus Torvalds if (th == NULL) 39591da177e4SLinus Torvalds break; 39601da177e4SLinus Torvalds 396148c62af6SEric Paris ad->u.net->sport = th->source; 396248c62af6SEric Paris ad->u.net->dport = th->dest; 39631da177e4SLinus Torvalds break; 39641da177e4SLinus Torvalds } 39651da177e4SLinus Torvalds 39661da177e4SLinus Torvalds case IPPROTO_UDP: { 39671da177e4SLinus Torvalds struct udphdr _udph, *uh; 39681da177e4SLinus Torvalds 39691da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 39701da177e4SLinus Torvalds break; 39711da177e4SLinus Torvalds 39721da177e4SLinus Torvalds offset += ihlen; 39731da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 39741da177e4SLinus Torvalds if (uh == NULL) 39751da177e4SLinus Torvalds break; 39761da177e4SLinus Torvalds 397748c62af6SEric Paris ad->u.net->sport = uh->source; 397848c62af6SEric Paris ad->u.net->dport = uh->dest; 39791da177e4SLinus Torvalds break; 39801da177e4SLinus Torvalds } 39811da177e4SLinus Torvalds 39822ee92d46SJames Morris case IPPROTO_DCCP: { 39832ee92d46SJames Morris struct dccp_hdr _dccph, *dh; 39842ee92d46SJames Morris 39852ee92d46SJames Morris if (ntohs(ih->frag_off) & IP_OFFSET) 39862ee92d46SJames Morris break; 39872ee92d46SJames Morris 39882ee92d46SJames Morris offset += ihlen; 39892ee92d46SJames Morris dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); 39902ee92d46SJames Morris if (dh == NULL) 39912ee92d46SJames Morris break; 39922ee92d46SJames Morris 399348c62af6SEric Paris ad->u.net->sport = dh->dccph_sport; 399448c62af6SEric Paris ad->u.net->dport = dh->dccph_dport; 39952ee92d46SJames Morris break; 39962ee92d46SJames Morris } 39972ee92d46SJames Morris 3998d452930fSRichard Haines #if IS_ENABLED(CONFIG_IP_SCTP) 3999d452930fSRichard Haines case IPPROTO_SCTP: { 4000d452930fSRichard Haines struct sctphdr _sctph, *sh; 4001d452930fSRichard Haines 4002d452930fSRichard Haines if (ntohs(ih->frag_off) & IP_OFFSET) 4003d452930fSRichard Haines break; 4004d452930fSRichard Haines 4005d452930fSRichard Haines offset += ihlen; 4006d452930fSRichard Haines sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph); 4007d452930fSRichard Haines if (sh == NULL) 4008d452930fSRichard Haines break; 4009d452930fSRichard Haines 4010d452930fSRichard Haines ad->u.net->sport = sh->source; 4011d452930fSRichard Haines ad->u.net->dport = sh->dest; 4012d452930fSRichard Haines break; 4013d452930fSRichard Haines } 4014d452930fSRichard Haines #endif 40151da177e4SLinus Torvalds default: 40161da177e4SLinus Torvalds break; 40171da177e4SLinus Torvalds } 40181da177e4SLinus Torvalds out: 40191da177e4SLinus Torvalds return ret; 40201da177e4SLinus Torvalds } 40211da177e4SLinus Torvalds 40221a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 40231da177e4SLinus Torvalds 40241da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 402567f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv6(struct sk_buff *skb, 40262bf49690SThomas Liu struct common_audit_data *ad, u8 *proto) 40271da177e4SLinus Torvalds { 40281da177e4SLinus Torvalds u8 nexthdr; 40291da177e4SLinus Torvalds int ret = -EINVAL, offset; 40301da177e4SLinus Torvalds struct ipv6hdr _ipv6h, *ip6; 403175f2811cSJesse Gross __be16 frag_off; 40321da177e4SLinus Torvalds 4033bbe735e4SArnaldo Carvalho de Melo offset = skb_network_offset(skb); 40341da177e4SLinus Torvalds ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h); 40351da177e4SLinus Torvalds if (ip6 == NULL) 40361da177e4SLinus Torvalds goto out; 40371da177e4SLinus Torvalds 403848c62af6SEric Paris ad->u.net->v6info.saddr = ip6->saddr; 403948c62af6SEric Paris ad->u.net->v6info.daddr = ip6->daddr; 40401da177e4SLinus Torvalds ret = 0; 40411da177e4SLinus Torvalds 40421da177e4SLinus Torvalds nexthdr = ip6->nexthdr; 40431da177e4SLinus Torvalds offset += sizeof(_ipv6h); 404475f2811cSJesse Gross offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off); 40451da177e4SLinus Torvalds if (offset < 0) 40461da177e4SLinus Torvalds goto out; 40471da177e4SLinus Torvalds 404867f83cbfSVenkat Yekkirala if (proto) 404967f83cbfSVenkat Yekkirala *proto = nexthdr; 405067f83cbfSVenkat Yekkirala 40511da177e4SLinus Torvalds switch (nexthdr) { 40521da177e4SLinus Torvalds case IPPROTO_TCP: { 40531da177e4SLinus Torvalds struct tcphdr _tcph, *th; 40541da177e4SLinus Torvalds 40551da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 40561da177e4SLinus Torvalds if (th == NULL) 40571da177e4SLinus Torvalds break; 40581da177e4SLinus Torvalds 405948c62af6SEric Paris ad->u.net->sport = th->source; 406048c62af6SEric Paris ad->u.net->dport = th->dest; 40611da177e4SLinus Torvalds break; 40621da177e4SLinus Torvalds } 40631da177e4SLinus Torvalds 40641da177e4SLinus Torvalds case IPPROTO_UDP: { 40651da177e4SLinus Torvalds struct udphdr _udph, *uh; 40661da177e4SLinus Torvalds 40671da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 40681da177e4SLinus Torvalds if (uh == NULL) 40691da177e4SLinus Torvalds break; 40701da177e4SLinus Torvalds 407148c62af6SEric Paris ad->u.net->sport = uh->source; 407248c62af6SEric Paris ad->u.net->dport = uh->dest; 40731da177e4SLinus Torvalds break; 40741da177e4SLinus Torvalds } 40751da177e4SLinus Torvalds 40762ee92d46SJames Morris case IPPROTO_DCCP: { 40772ee92d46SJames Morris struct dccp_hdr _dccph, *dh; 40782ee92d46SJames Morris 40792ee92d46SJames Morris dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); 40802ee92d46SJames Morris if (dh == NULL) 40812ee92d46SJames Morris break; 40822ee92d46SJames Morris 408348c62af6SEric Paris ad->u.net->sport = dh->dccph_sport; 408448c62af6SEric Paris ad->u.net->dport = dh->dccph_dport; 40852ee92d46SJames Morris break; 40862ee92d46SJames Morris } 40872ee92d46SJames Morris 4088d452930fSRichard Haines #if IS_ENABLED(CONFIG_IP_SCTP) 4089d452930fSRichard Haines case IPPROTO_SCTP: { 4090d452930fSRichard Haines struct sctphdr _sctph, *sh; 4091d452930fSRichard Haines 4092d452930fSRichard Haines sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph); 4093d452930fSRichard Haines if (sh == NULL) 4094d452930fSRichard Haines break; 4095d452930fSRichard Haines 4096d452930fSRichard Haines ad->u.net->sport = sh->source; 4097d452930fSRichard Haines ad->u.net->dport = sh->dest; 4098d452930fSRichard Haines break; 4099d452930fSRichard Haines } 4100d452930fSRichard Haines #endif 41011da177e4SLinus Torvalds /* includes fragments */ 41021da177e4SLinus Torvalds default: 41031da177e4SLinus Torvalds break; 41041da177e4SLinus Torvalds } 41051da177e4SLinus Torvalds out: 41061da177e4SLinus Torvalds return ret; 41071da177e4SLinus Torvalds } 41081da177e4SLinus Torvalds 41091da177e4SLinus Torvalds #endif /* IPV6 */ 41101da177e4SLinus Torvalds 41112bf49690SThomas Liu static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad, 4112cf9481e2SDavid Howells char **_addrp, int src, u8 *proto) 41131da177e4SLinus Torvalds { 4114cf9481e2SDavid Howells char *addrp; 4115cf9481e2SDavid Howells int ret; 41161da177e4SLinus Torvalds 411748c62af6SEric Paris switch (ad->u.net->family) { 41181da177e4SLinus Torvalds case PF_INET: 411967f83cbfSVenkat Yekkirala ret = selinux_parse_skb_ipv4(skb, ad, proto); 4120cf9481e2SDavid Howells if (ret) 4121cf9481e2SDavid Howells goto parse_error; 412248c62af6SEric Paris addrp = (char *)(src ? &ad->u.net->v4info.saddr : 412348c62af6SEric Paris &ad->u.net->v4info.daddr); 4124cf9481e2SDavid Howells goto okay; 41251da177e4SLinus Torvalds 41261a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 41271da177e4SLinus Torvalds case PF_INET6: 412867f83cbfSVenkat Yekkirala ret = selinux_parse_skb_ipv6(skb, ad, proto); 4129cf9481e2SDavid Howells if (ret) 4130cf9481e2SDavid Howells goto parse_error; 413148c62af6SEric Paris addrp = (char *)(src ? &ad->u.net->v6info.saddr : 413248c62af6SEric Paris &ad->u.net->v6info.daddr); 4133cf9481e2SDavid Howells goto okay; 41341da177e4SLinus Torvalds #endif /* IPV6 */ 41351da177e4SLinus Torvalds default: 4136cf9481e2SDavid Howells addrp = NULL; 4137cf9481e2SDavid Howells goto okay; 41381da177e4SLinus Torvalds } 41391da177e4SLinus Torvalds 4140cf9481e2SDavid Howells parse_error: 4141c103a91eSpeter enderborg pr_warn( 414271f1cb05SPaul Moore "SELinux: failure in selinux_parse_skb()," 414371f1cb05SPaul Moore " unable to parse packet\n"); 41441da177e4SLinus Torvalds return ret; 4145cf9481e2SDavid Howells 4146cf9481e2SDavid Howells okay: 4147cf9481e2SDavid Howells if (_addrp) 4148cf9481e2SDavid Howells *_addrp = addrp; 4149cf9481e2SDavid Howells return 0; 41501da177e4SLinus Torvalds } 41511da177e4SLinus Torvalds 41524f6a993fSPaul Moore /** 4153220deb96SPaul Moore * selinux_skb_peerlbl_sid - Determine the peer label of a packet 41544f6a993fSPaul Moore * @skb: the packet 415575e22910SPaul Moore * @family: protocol family 4156220deb96SPaul Moore * @sid: the packet's peer label SID 41574f6a993fSPaul Moore * 41584f6a993fSPaul Moore * Description: 4159220deb96SPaul Moore * Check the various different forms of network peer labeling and determine 4160220deb96SPaul Moore * the peer label/SID for the packet; most of the magic actually occurs in 4161220deb96SPaul Moore * the security server function security_net_peersid_cmp(). The function 4162220deb96SPaul Moore * returns zero if the value in @sid is valid (although it may be SECSID_NULL) 4163220deb96SPaul Moore * or -EACCES if @sid is invalid due to inconsistencies with the different 4164220deb96SPaul Moore * peer labels. 41654f6a993fSPaul Moore * 41664f6a993fSPaul Moore */ 4167220deb96SPaul Moore static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid) 41684f6a993fSPaul Moore { 416971f1cb05SPaul Moore int err; 41704f6a993fSPaul Moore u32 xfrm_sid; 41714f6a993fSPaul Moore u32 nlbl_sid; 4172220deb96SPaul Moore u32 nlbl_type; 41734f6a993fSPaul Moore 4174817eff71SPaul Moore err = selinux_xfrm_skb_sid(skb, &xfrm_sid); 4175bed4d7efSPaul Moore if (unlikely(err)) 4176bed4d7efSPaul Moore return -EACCES; 4177bed4d7efSPaul Moore err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid); 4178bed4d7efSPaul Moore if (unlikely(err)) 4179bed4d7efSPaul Moore return -EACCES; 4180220deb96SPaul Moore 4181aa8e712cSStephen Smalley err = security_net_peersid_resolve(&selinux_state, nlbl_sid, 4182aa8e712cSStephen Smalley nlbl_type, xfrm_sid, sid); 418371f1cb05SPaul Moore if (unlikely(err)) { 4184c103a91eSpeter enderborg pr_warn( 418571f1cb05SPaul Moore "SELinux: failure in selinux_skb_peerlbl_sid()," 418671f1cb05SPaul Moore " unable to determine packet's peer label\n"); 4187220deb96SPaul Moore return -EACCES; 418871f1cb05SPaul Moore } 4189220deb96SPaul Moore 4190220deb96SPaul Moore return 0; 41914f6a993fSPaul Moore } 41924f6a993fSPaul Moore 4193446b8024SPaul Moore /** 4194446b8024SPaul Moore * selinux_conn_sid - Determine the child socket label for a connection 4195446b8024SPaul Moore * @sk_sid: the parent socket's SID 4196446b8024SPaul Moore * @skb_sid: the packet's SID 4197446b8024SPaul Moore * @conn_sid: the resulting connection SID 4198446b8024SPaul Moore * 4199446b8024SPaul Moore * If @skb_sid is valid then the user:role:type information from @sk_sid is 4200446b8024SPaul Moore * combined with the MLS information from @skb_sid in order to create 4201446b8024SPaul Moore * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy 4202446b8024SPaul Moore * of @sk_sid. Returns zero on success, negative values on failure. 4203446b8024SPaul Moore * 4204446b8024SPaul Moore */ 4205446b8024SPaul Moore static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid) 4206446b8024SPaul Moore { 4207446b8024SPaul Moore int err = 0; 4208446b8024SPaul Moore 4209446b8024SPaul Moore if (skb_sid != SECSID_NULL) 4210aa8e712cSStephen Smalley err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid, 4211aa8e712cSStephen Smalley conn_sid); 4212446b8024SPaul Moore else 4213446b8024SPaul Moore *conn_sid = sk_sid; 4214446b8024SPaul Moore 4215446b8024SPaul Moore return err; 4216446b8024SPaul Moore } 4217446b8024SPaul Moore 42181da177e4SLinus Torvalds /* socket security operations */ 4219d4f2d978SPaul Moore 42202ad18bdfSHarry Ciao static int socket_sockcreate_sid(const struct task_security_struct *tsec, 42212ad18bdfSHarry Ciao u16 secclass, u32 *socksid) 4222d4f2d978SPaul Moore { 42232ad18bdfSHarry Ciao if (tsec->sockcreate_sid > SECSID_NULL) { 42242ad18bdfSHarry Ciao *socksid = tsec->sockcreate_sid; 42252ad18bdfSHarry Ciao return 0; 42262ad18bdfSHarry Ciao } 42272ad18bdfSHarry Ciao 4228aa8e712cSStephen Smalley return security_transition_sid(&selinux_state, tsec->sid, tsec->sid, 4229aa8e712cSStephen Smalley secclass, NULL, socksid); 4230d4f2d978SPaul Moore } 4231d4f2d978SPaul Moore 4232be0554c9SStephen Smalley static int sock_has_perm(struct sock *sk, u32 perms) 42331da177e4SLinus Torvalds { 4234253bfae6SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 42352bf49690SThomas Liu struct common_audit_data ad; 423648c62af6SEric Paris struct lsm_network_audit net = {0,}; 42371da177e4SLinus Torvalds 4238253bfae6SPaul Moore if (sksec->sid == SECINITSID_KERNEL) 4239253bfae6SPaul Moore return 0; 42401da177e4SLinus Torvalds 424150c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 424248c62af6SEric Paris ad.u.net = &net; 424348c62af6SEric Paris ad.u.net->sk = sk; 42441da177e4SLinus Torvalds 42456b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 42466b6bc620SStephen Smalley current_sid(), sksec->sid, sksec->sclass, perms, 4247be0554c9SStephen Smalley &ad); 42481da177e4SLinus Torvalds } 42491da177e4SLinus Torvalds 42501da177e4SLinus Torvalds static int selinux_socket_create(int family, int type, 42511da177e4SLinus Torvalds int protocol, int kern) 42521da177e4SLinus Torvalds { 42530c6cfa62SCasey Schaufler const struct task_security_struct *tsec = selinux_cred(current_cred()); 4254d4f2d978SPaul Moore u32 newsid; 4255275bb41eSDavid Howells u16 secclass; 42562ad18bdfSHarry Ciao int rc; 42571da177e4SLinus Torvalds 42581da177e4SLinus Torvalds if (kern) 4259d4f2d978SPaul Moore return 0; 42601da177e4SLinus Torvalds 4261275bb41eSDavid Howells secclass = socket_type_to_security_class(family, type, protocol); 42622ad18bdfSHarry Ciao rc = socket_sockcreate_sid(tsec, secclass, &newsid); 42632ad18bdfSHarry Ciao if (rc) 42642ad18bdfSHarry Ciao return rc; 42652ad18bdfSHarry Ciao 42666b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 42676b6bc620SStephen Smalley tsec->sid, newsid, secclass, SOCKET__CREATE, NULL); 42681da177e4SLinus Torvalds } 42691da177e4SLinus Torvalds 42707420ed23SVenkat Yekkirala static int selinux_socket_post_create(struct socket *sock, int family, 42711da177e4SLinus Torvalds int type, int protocol, int kern) 42721da177e4SLinus Torvalds { 42730c6cfa62SCasey Schaufler const struct task_security_struct *tsec = selinux_cred(current_cred()); 42745d226df4SAndreas Gruenbacher struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock)); 4275892c141eSVenkat Yekkirala struct sk_security_struct *sksec; 42769287aed2SAndreas Gruenbacher u16 sclass = socket_type_to_security_class(family, type, protocol); 42779287aed2SAndreas Gruenbacher u32 sid = SECINITSID_KERNEL; 4278275bb41eSDavid Howells int err = 0; 4279275bb41eSDavid Howells 42809287aed2SAndreas Gruenbacher if (!kern) { 42819287aed2SAndreas Gruenbacher err = socket_sockcreate_sid(tsec, sclass, &sid); 42822ad18bdfSHarry Ciao if (err) 42832ad18bdfSHarry Ciao return err; 42842ad18bdfSHarry Ciao } 4285275bb41eSDavid Howells 42869287aed2SAndreas Gruenbacher isec->sclass = sclass; 42879287aed2SAndreas Gruenbacher isec->sid = sid; 42886f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 42891da177e4SLinus Torvalds 4290892c141eSVenkat Yekkirala if (sock->sk) { 4291892c141eSVenkat Yekkirala sksec = sock->sk->sk_security; 42929287aed2SAndreas Gruenbacher sksec->sclass = sclass; 42939287aed2SAndreas Gruenbacher sksec->sid = sid; 4294d452930fSRichard Haines /* Allows detection of the first association on this socket */ 4295d452930fSRichard Haines if (sksec->sclass == SECCLASS_SCTP_SOCKET) 4296d452930fSRichard Haines sksec->sctp_assoc_state = SCTP_ASSOC_UNSET; 4297d452930fSRichard Haines 4298389fb800SPaul Moore err = selinux_netlbl_socket_post_create(sock->sk, family); 4299892c141eSVenkat Yekkirala } 4300892c141eSVenkat Yekkirala 43017420ed23SVenkat Yekkirala return err; 43021da177e4SLinus Torvalds } 43031da177e4SLinus Torvalds 43040b811db2SDavid Herrmann static int selinux_socket_socketpair(struct socket *socka, 43050b811db2SDavid Herrmann struct socket *sockb) 43060b811db2SDavid Herrmann { 43070b811db2SDavid Herrmann struct sk_security_struct *sksec_a = socka->sk->sk_security; 43080b811db2SDavid Herrmann struct sk_security_struct *sksec_b = sockb->sk->sk_security; 43090b811db2SDavid Herrmann 43100b811db2SDavid Herrmann sksec_a->peer_sid = sksec_b->sid; 43110b811db2SDavid Herrmann sksec_b->peer_sid = sksec_a->sid; 43120b811db2SDavid Herrmann 43130b811db2SDavid Herrmann return 0; 43140b811db2SDavid Herrmann } 43150b811db2SDavid Herrmann 43161da177e4SLinus Torvalds /* Range of port numbers used to automatically bind. 43171da177e4SLinus Torvalds Need to determine whether we should perform a name_bind 43181da177e4SLinus Torvalds permission check between the socket and the port number. */ 43191da177e4SLinus Torvalds 43201da177e4SLinus Torvalds static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen) 43211da177e4SLinus Torvalds { 4322253bfae6SPaul Moore struct sock *sk = sock->sk; 43230f8db8ccSAlexey Kodanev struct sk_security_struct *sksec = sk->sk_security; 43241da177e4SLinus Torvalds u16 family; 43251da177e4SLinus Torvalds int err; 43261da177e4SLinus Torvalds 4327be0554c9SStephen Smalley err = sock_has_perm(sk, SOCKET__BIND); 43281da177e4SLinus Torvalds if (err) 43291da177e4SLinus Torvalds goto out; 43301da177e4SLinus Torvalds 4331d452930fSRichard Haines /* If PF_INET or PF_INET6, check name_bind permission for the port. */ 4332253bfae6SPaul Moore family = sk->sk_family; 43331da177e4SLinus Torvalds if (family == PF_INET || family == PF_INET6) { 43341da177e4SLinus Torvalds char *addrp; 43352bf49690SThomas Liu struct common_audit_data ad; 433648c62af6SEric Paris struct lsm_network_audit net = {0,}; 43371da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 43381da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 43390f8db8ccSAlexey Kodanev u16 family_sa = address->sa_family; 43401da177e4SLinus Torvalds unsigned short snum; 4341e399f982SJames Morris u32 sid, node_perm; 43421da177e4SLinus Torvalds 4343d452930fSRichard Haines /* 4344d452930fSRichard Haines * sctp_bindx(3) calls via selinux_sctp_bind_connect() 4345d452930fSRichard Haines * that validates multiple binding addresses. Because of this 4346d452930fSRichard Haines * need to check address->sa_family as it is possible to have 4347d452930fSRichard Haines * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET. 4348d452930fSRichard Haines */ 43490f8db8ccSAlexey Kodanev switch (family_sa) { 43500f8db8ccSAlexey Kodanev case AF_UNSPEC: 435168741a8aSRichard Haines case AF_INET: 435268741a8aSRichard Haines if (addrlen < sizeof(struct sockaddr_in)) 435368741a8aSRichard Haines return -EINVAL; 43541da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 43550f8db8ccSAlexey Kodanev if (family_sa == AF_UNSPEC) { 43560f8db8ccSAlexey Kodanev /* see __inet_bind(), we only want to allow 43570f8db8ccSAlexey Kodanev * AF_UNSPEC if the address is INADDR_ANY 43580f8db8ccSAlexey Kodanev */ 43590f8db8ccSAlexey Kodanev if (addr4->sin_addr.s_addr != htonl(INADDR_ANY)) 43600f8db8ccSAlexey Kodanev goto err_af; 43610f8db8ccSAlexey Kodanev family_sa = AF_INET; 43620f8db8ccSAlexey Kodanev } 43631da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 43641da177e4SLinus Torvalds addrp = (char *)&addr4->sin_addr.s_addr; 436568741a8aSRichard Haines break; 436668741a8aSRichard Haines case AF_INET6: 436768741a8aSRichard Haines if (addrlen < SIN6_LEN_RFC2133) 436868741a8aSRichard Haines return -EINVAL; 43691da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 43701da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 43711da177e4SLinus Torvalds addrp = (char *)&addr6->sin6_addr.s6_addr; 437268741a8aSRichard Haines break; 437368741a8aSRichard Haines default: 43740f8db8ccSAlexey Kodanev goto err_af; 43751da177e4SLinus Torvalds } 43761da177e4SLinus Torvalds 437788b7d370SAlexey Kodanev ad.type = LSM_AUDIT_DATA_NET; 437888b7d370SAlexey Kodanev ad.u.net = &net; 437988b7d370SAlexey Kodanev ad.u.net->sport = htons(snum); 438088b7d370SAlexey Kodanev ad.u.net->family = family_sa; 438188b7d370SAlexey Kodanev 4382227b60f5SStephen Hemminger if (snum) { 4383227b60f5SStephen Hemminger int low, high; 4384227b60f5SStephen Hemminger 43850bbf87d8SEric W. Biederman inet_get_local_port_range(sock_net(sk), &low, &high); 4386227b60f5SStephen Hemminger 43874548b683SKrister Johansen if (snum < max(inet_prot_sock(sock_net(sk)), low) || 43884548b683SKrister Johansen snum > high) { 43893e112172SPaul Moore err = sel_netport_sid(sk->sk_protocol, 43903e112172SPaul Moore snum, &sid); 43911da177e4SLinus Torvalds if (err) 43921da177e4SLinus Torvalds goto out; 43936b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 43946b6bc620SStephen Smalley sksec->sid, sid, 4395253bfae6SPaul Moore sksec->sclass, 43961da177e4SLinus Torvalds SOCKET__NAME_BIND, &ad); 43971da177e4SLinus Torvalds if (err) 43981da177e4SLinus Torvalds goto out; 43991da177e4SLinus Torvalds } 4400227b60f5SStephen Hemminger } 44011da177e4SLinus Torvalds 4402253bfae6SPaul Moore switch (sksec->sclass) { 440313402580SJames Morris case SECCLASS_TCP_SOCKET: 44041da177e4SLinus Torvalds node_perm = TCP_SOCKET__NODE_BIND; 44051da177e4SLinus Torvalds break; 44061da177e4SLinus Torvalds 440713402580SJames Morris case SECCLASS_UDP_SOCKET: 44081da177e4SLinus Torvalds node_perm = UDP_SOCKET__NODE_BIND; 44091da177e4SLinus Torvalds break; 44101da177e4SLinus Torvalds 44112ee92d46SJames Morris case SECCLASS_DCCP_SOCKET: 44122ee92d46SJames Morris node_perm = DCCP_SOCKET__NODE_BIND; 44132ee92d46SJames Morris break; 44142ee92d46SJames Morris 4415d452930fSRichard Haines case SECCLASS_SCTP_SOCKET: 4416d452930fSRichard Haines node_perm = SCTP_SOCKET__NODE_BIND; 4417d452930fSRichard Haines break; 4418d452930fSRichard Haines 44191da177e4SLinus Torvalds default: 44201da177e4SLinus Torvalds node_perm = RAWIP_SOCKET__NODE_BIND; 44211da177e4SLinus Torvalds break; 44221da177e4SLinus Torvalds } 44231da177e4SLinus Torvalds 442488b7d370SAlexey Kodanev err = sel_netnode_sid(addrp, family_sa, &sid); 44251da177e4SLinus Torvalds if (err) 44261da177e4SLinus Torvalds goto out; 44271da177e4SLinus Torvalds 44280f8db8ccSAlexey Kodanev if (family_sa == AF_INET) 442948c62af6SEric Paris ad.u.net->v4info.saddr = addr4->sin_addr.s_addr; 44301da177e4SLinus Torvalds else 443148c62af6SEric Paris ad.u.net->v6info.saddr = addr6->sin6_addr; 44321da177e4SLinus Torvalds 44336b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 44346b6bc620SStephen Smalley sksec->sid, sid, 4435253bfae6SPaul Moore sksec->sclass, node_perm, &ad); 44361da177e4SLinus Torvalds if (err) 44371da177e4SLinus Torvalds goto out; 44381da177e4SLinus Torvalds } 44391da177e4SLinus Torvalds out: 44401da177e4SLinus Torvalds return err; 44410f8db8ccSAlexey Kodanev err_af: 44420f8db8ccSAlexey Kodanev /* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */ 44430f8db8ccSAlexey Kodanev if (sksec->sclass == SECCLASS_SCTP_SOCKET) 44440f8db8ccSAlexey Kodanev return -EINVAL; 44450f8db8ccSAlexey Kodanev return -EAFNOSUPPORT; 44461da177e4SLinus Torvalds } 44471da177e4SLinus Torvalds 4448d452930fSRichard Haines /* This supports connect(2) and SCTP connect services such as sctp_connectx(3) 44495fb94e9cSMauro Carvalho Chehab * and sctp_sendmsg(3) as described in Documentation/security/LSM-sctp.rst 4450d452930fSRichard Haines */ 4451d452930fSRichard Haines static int selinux_socket_connect_helper(struct socket *sock, 4452d452930fSRichard Haines struct sockaddr *address, int addrlen) 44531da177e4SLinus Torvalds { 4454014ab19aSPaul Moore struct sock *sk = sock->sk; 4455253bfae6SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 44561da177e4SLinus Torvalds int err; 44571da177e4SLinus Torvalds 4458be0554c9SStephen Smalley err = sock_has_perm(sk, SOCKET__CONNECT); 44591da177e4SLinus Torvalds if (err) 44601da177e4SLinus Torvalds return err; 44611da177e4SLinus Torvalds 44621da177e4SLinus Torvalds /* 4463d452930fSRichard Haines * If a TCP, DCCP or SCTP socket, check name_connect permission 4464d452930fSRichard Haines * for the port. 44651da177e4SLinus Torvalds */ 4466253bfae6SPaul Moore if (sksec->sclass == SECCLASS_TCP_SOCKET || 4467d452930fSRichard Haines sksec->sclass == SECCLASS_DCCP_SOCKET || 4468d452930fSRichard Haines sksec->sclass == SECCLASS_SCTP_SOCKET) { 44692bf49690SThomas Liu struct common_audit_data ad; 447048c62af6SEric Paris struct lsm_network_audit net = {0,}; 44711da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 44721da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 44731da177e4SLinus Torvalds unsigned short snum; 44742ee92d46SJames Morris u32 sid, perm; 44751da177e4SLinus Torvalds 4476d452930fSRichard Haines /* sctp_connectx(3) calls via selinux_sctp_bind_connect() 4477d452930fSRichard Haines * that validates multiple connect addresses. Because of this 4478d452930fSRichard Haines * need to check address->sa_family as it is possible to have 4479d452930fSRichard Haines * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET. 4480d452930fSRichard Haines */ 448168741a8aSRichard Haines switch (address->sa_family) { 448268741a8aSRichard Haines case AF_INET: 44831da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 4484911656f8SStephen Smalley if (addrlen < sizeof(struct sockaddr_in)) 44851da177e4SLinus Torvalds return -EINVAL; 44861da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 448768741a8aSRichard Haines break; 448868741a8aSRichard Haines case AF_INET6: 44891da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 4490911656f8SStephen Smalley if (addrlen < SIN6_LEN_RFC2133) 44911da177e4SLinus Torvalds return -EINVAL; 44921da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 449368741a8aSRichard Haines break; 449468741a8aSRichard Haines default: 449568741a8aSRichard Haines /* Note that SCTP services expect -EINVAL, whereas 449668741a8aSRichard Haines * others expect -EAFNOSUPPORT. 449768741a8aSRichard Haines */ 449868741a8aSRichard Haines if (sksec->sclass == SECCLASS_SCTP_SOCKET) 449968741a8aSRichard Haines return -EINVAL; 450068741a8aSRichard Haines else 450168741a8aSRichard Haines return -EAFNOSUPPORT; 45021da177e4SLinus Torvalds } 45031da177e4SLinus Torvalds 45043e112172SPaul Moore err = sel_netport_sid(sk->sk_protocol, snum, &sid); 45051da177e4SLinus Torvalds if (err) 4506d452930fSRichard Haines return err; 45071da177e4SLinus Torvalds 4508d452930fSRichard Haines switch (sksec->sclass) { 4509d452930fSRichard Haines case SECCLASS_TCP_SOCKET: 4510d452930fSRichard Haines perm = TCP_SOCKET__NAME_CONNECT; 4511d452930fSRichard Haines break; 4512d452930fSRichard Haines case SECCLASS_DCCP_SOCKET: 4513d452930fSRichard Haines perm = DCCP_SOCKET__NAME_CONNECT; 4514d452930fSRichard Haines break; 4515d452930fSRichard Haines case SECCLASS_SCTP_SOCKET: 4516d452930fSRichard Haines perm = SCTP_SOCKET__NAME_CONNECT; 4517d452930fSRichard Haines break; 4518d452930fSRichard Haines } 45192ee92d46SJames Morris 452050c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 452148c62af6SEric Paris ad.u.net = &net; 452248c62af6SEric Paris ad.u.net->dport = htons(snum); 452388b7d370SAlexey Kodanev ad.u.net->family = address->sa_family; 45246b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 45256b6bc620SStephen Smalley sksec->sid, sid, sksec->sclass, perm, &ad); 45261da177e4SLinus Torvalds if (err) 4527d452930fSRichard Haines return err; 45281da177e4SLinus Torvalds } 45291da177e4SLinus Torvalds 4530d452930fSRichard Haines return 0; 4531d452930fSRichard Haines } 4532014ab19aSPaul Moore 4533d452930fSRichard Haines /* Supports connect(2), see comments in selinux_socket_connect_helper() */ 4534d452930fSRichard Haines static int selinux_socket_connect(struct socket *sock, 4535d452930fSRichard Haines struct sockaddr *address, int addrlen) 4536d452930fSRichard Haines { 4537d452930fSRichard Haines int err; 4538d452930fSRichard Haines struct sock *sk = sock->sk; 4539d452930fSRichard Haines 4540d452930fSRichard Haines err = selinux_socket_connect_helper(sock, address, addrlen); 4541d452930fSRichard Haines if (err) 45421da177e4SLinus Torvalds return err; 4543d452930fSRichard Haines 4544d452930fSRichard Haines return selinux_netlbl_socket_connect(sk, address); 45451da177e4SLinus Torvalds } 45461da177e4SLinus Torvalds 45471da177e4SLinus Torvalds static int selinux_socket_listen(struct socket *sock, int backlog) 45481da177e4SLinus Torvalds { 4549be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__LISTEN); 45501da177e4SLinus Torvalds } 45511da177e4SLinus Torvalds 45521da177e4SLinus Torvalds static int selinux_socket_accept(struct socket *sock, struct socket *newsock) 45531da177e4SLinus Torvalds { 45541da177e4SLinus Torvalds int err; 45551da177e4SLinus Torvalds struct inode_security_struct *isec; 45561da177e4SLinus Torvalds struct inode_security_struct *newisec; 45579287aed2SAndreas Gruenbacher u16 sclass; 45589287aed2SAndreas Gruenbacher u32 sid; 45591da177e4SLinus Torvalds 4560be0554c9SStephen Smalley err = sock_has_perm(sock->sk, SOCKET__ACCEPT); 45611da177e4SLinus Torvalds if (err) 45621da177e4SLinus Torvalds return err; 45631da177e4SLinus Torvalds 45645d226df4SAndreas Gruenbacher isec = inode_security_novalidate(SOCK_INODE(sock)); 45659287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 45669287aed2SAndreas Gruenbacher sclass = isec->sclass; 45679287aed2SAndreas Gruenbacher sid = isec->sid; 45689287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 45699287aed2SAndreas Gruenbacher 45709287aed2SAndreas Gruenbacher newisec = inode_security_novalidate(SOCK_INODE(newsock)); 45719287aed2SAndreas Gruenbacher newisec->sclass = sclass; 45729287aed2SAndreas Gruenbacher newisec->sid = sid; 45736f3be9f5SAndreas Gruenbacher newisec->initialized = LABEL_INITIALIZED; 45741da177e4SLinus Torvalds 45751da177e4SLinus Torvalds return 0; 45761da177e4SLinus Torvalds } 45771da177e4SLinus Torvalds 45781da177e4SLinus Torvalds static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg, 45791da177e4SLinus Torvalds int size) 45801da177e4SLinus Torvalds { 4581be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__WRITE); 45821da177e4SLinus Torvalds } 45831da177e4SLinus Torvalds 45841da177e4SLinus Torvalds static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg, 45851da177e4SLinus Torvalds int size, int flags) 45861da177e4SLinus Torvalds { 4587be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__READ); 45881da177e4SLinus Torvalds } 45891da177e4SLinus Torvalds 45901da177e4SLinus Torvalds static int selinux_socket_getsockname(struct socket *sock) 45911da177e4SLinus Torvalds { 4592be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__GETATTR); 45931da177e4SLinus Torvalds } 45941da177e4SLinus Torvalds 45951da177e4SLinus Torvalds static int selinux_socket_getpeername(struct socket *sock) 45961da177e4SLinus Torvalds { 4597be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__GETATTR); 45981da177e4SLinus Torvalds } 45991da177e4SLinus Torvalds 46001da177e4SLinus Torvalds static int selinux_socket_setsockopt(struct socket *sock, int level, int optname) 46011da177e4SLinus Torvalds { 4602f8687afeSPaul Moore int err; 4603f8687afeSPaul Moore 4604be0554c9SStephen Smalley err = sock_has_perm(sock->sk, SOCKET__SETOPT); 4605f8687afeSPaul Moore if (err) 4606f8687afeSPaul Moore return err; 4607f8687afeSPaul Moore 4608f8687afeSPaul Moore return selinux_netlbl_socket_setsockopt(sock, level, optname); 46091da177e4SLinus Torvalds } 46101da177e4SLinus Torvalds 46111da177e4SLinus Torvalds static int selinux_socket_getsockopt(struct socket *sock, int level, 46121da177e4SLinus Torvalds int optname) 46131da177e4SLinus Torvalds { 4614be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__GETOPT); 46151da177e4SLinus Torvalds } 46161da177e4SLinus Torvalds 46171da177e4SLinus Torvalds static int selinux_socket_shutdown(struct socket *sock, int how) 46181da177e4SLinus Torvalds { 4619be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__SHUTDOWN); 46201da177e4SLinus Torvalds } 46211da177e4SLinus Torvalds 46223610cda5SDavid S. Miller static int selinux_socket_unix_stream_connect(struct sock *sock, 46233610cda5SDavid S. Miller struct sock *other, 46241da177e4SLinus Torvalds struct sock *newsk) 46251da177e4SLinus Torvalds { 46263610cda5SDavid S. Miller struct sk_security_struct *sksec_sock = sock->sk_security; 46273610cda5SDavid S. Miller struct sk_security_struct *sksec_other = other->sk_security; 46284d1e2451SPaul Moore struct sk_security_struct *sksec_new = newsk->sk_security; 46292bf49690SThomas Liu struct common_audit_data ad; 463048c62af6SEric Paris struct lsm_network_audit net = {0,}; 46311da177e4SLinus Torvalds int err; 46321da177e4SLinus Torvalds 463350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 463448c62af6SEric Paris ad.u.net = &net; 463548c62af6SEric Paris ad.u.net->sk = other; 46361da177e4SLinus Torvalds 46376b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 46386b6bc620SStephen Smalley sksec_sock->sid, sksec_other->sid, 46394d1e2451SPaul Moore sksec_other->sclass, 46401da177e4SLinus Torvalds UNIX_STREAM_SOCKET__CONNECTTO, &ad); 46411da177e4SLinus Torvalds if (err) 46421da177e4SLinus Torvalds return err; 46431da177e4SLinus Torvalds 46441da177e4SLinus Torvalds /* server child socket */ 46454d1e2451SPaul Moore sksec_new->peer_sid = sksec_sock->sid; 4646aa8e712cSStephen Smalley err = security_sid_mls_copy(&selinux_state, sksec_other->sid, 4647aa8e712cSStephen Smalley sksec_sock->sid, &sksec_new->sid); 46484d1e2451SPaul Moore if (err) 46494237c75cSVenkat Yekkirala return err; 46504d1e2451SPaul Moore 46514d1e2451SPaul Moore /* connecting socket */ 46524d1e2451SPaul Moore sksec_sock->peer_sid = sksec_new->sid; 46534d1e2451SPaul Moore 46544d1e2451SPaul Moore return 0; 46551da177e4SLinus Torvalds } 46561da177e4SLinus Torvalds 46571da177e4SLinus Torvalds static int selinux_socket_unix_may_send(struct socket *sock, 46581da177e4SLinus Torvalds struct socket *other) 46591da177e4SLinus Torvalds { 4660253bfae6SPaul Moore struct sk_security_struct *ssec = sock->sk->sk_security; 4661253bfae6SPaul Moore struct sk_security_struct *osec = other->sk->sk_security; 46622bf49690SThomas Liu struct common_audit_data ad; 466348c62af6SEric Paris struct lsm_network_audit net = {0,}; 46641da177e4SLinus Torvalds 466550c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 466648c62af6SEric Paris ad.u.net = &net; 466748c62af6SEric Paris ad.u.net->sk = other->sk; 46681da177e4SLinus Torvalds 46696b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 46706b6bc620SStephen Smalley ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO, 4671253bfae6SPaul Moore &ad); 46721da177e4SLinus Torvalds } 46731da177e4SLinus Torvalds 4674cbe0d6e8SPaul Moore static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex, 4675cbe0d6e8SPaul Moore char *addrp, u16 family, u32 peer_sid, 46762bf49690SThomas Liu struct common_audit_data *ad) 4677effad8dfSPaul Moore { 4678effad8dfSPaul Moore int err; 4679effad8dfSPaul Moore u32 if_sid; 4680effad8dfSPaul Moore u32 node_sid; 4681effad8dfSPaul Moore 4682cbe0d6e8SPaul Moore err = sel_netif_sid(ns, ifindex, &if_sid); 4683effad8dfSPaul Moore if (err) 4684effad8dfSPaul Moore return err; 46856b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 46866b6bc620SStephen Smalley peer_sid, if_sid, 4687effad8dfSPaul Moore SECCLASS_NETIF, NETIF__INGRESS, ad); 4688effad8dfSPaul Moore if (err) 4689effad8dfSPaul Moore return err; 4690effad8dfSPaul Moore 4691effad8dfSPaul Moore err = sel_netnode_sid(addrp, family, &node_sid); 4692effad8dfSPaul Moore if (err) 4693effad8dfSPaul Moore return err; 46946b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 46956b6bc620SStephen Smalley peer_sid, node_sid, 4696effad8dfSPaul Moore SECCLASS_NODE, NODE__RECVFROM, ad); 4697effad8dfSPaul Moore } 4698effad8dfSPaul Moore 4699220deb96SPaul Moore static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb, 4700d8395c87SPaul Moore u16 family) 4701220deb96SPaul Moore { 4702277d342fSPaul Moore int err = 0; 4703220deb96SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 4704220deb96SPaul Moore u32 sk_sid = sksec->sid; 47052bf49690SThomas Liu struct common_audit_data ad; 470648c62af6SEric Paris struct lsm_network_audit net = {0,}; 4707d8395c87SPaul Moore char *addrp; 4708d8395c87SPaul Moore 470950c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 471048c62af6SEric Paris ad.u.net = &net; 471148c62af6SEric Paris ad.u.net->netif = skb->skb_iif; 471248c62af6SEric Paris ad.u.net->family = family; 4713d8395c87SPaul Moore err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); 4714d8395c87SPaul Moore if (err) 4715d8395c87SPaul Moore return err; 4716220deb96SPaul Moore 471758bfbb51SPaul Moore if (selinux_secmark_enabled()) { 47186b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 47196b6bc620SStephen Smalley sk_sid, skb->secmark, SECCLASS_PACKET, 4720d8395c87SPaul Moore PACKET__RECV, &ad); 4721220deb96SPaul Moore if (err) 4722220deb96SPaul Moore return err; 472358bfbb51SPaul Moore } 4724220deb96SPaul Moore 4725d8395c87SPaul Moore err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad); 4726220deb96SPaul Moore if (err) 4727220deb96SPaul Moore return err; 4728d8395c87SPaul Moore err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad); 4729220deb96SPaul Moore 47304e5ab4cbSJames Morris return err; 47314e5ab4cbSJames Morris } 4732d28d1e08STrent Jaeger 47334e5ab4cbSJames Morris static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) 47344e5ab4cbSJames Morris { 4735220deb96SPaul Moore int err; 47364237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 4737220deb96SPaul Moore u16 family = sk->sk_family; 4738220deb96SPaul Moore u32 sk_sid = sksec->sid; 47392bf49690SThomas Liu struct common_audit_data ad; 474048c62af6SEric Paris struct lsm_network_audit net = {0,}; 4741220deb96SPaul Moore char *addrp; 4742d8395c87SPaul Moore u8 secmark_active; 4743d8395c87SPaul Moore u8 peerlbl_active; 47444e5ab4cbSJames Morris 47454e5ab4cbSJames Morris if (family != PF_INET && family != PF_INET6) 4746220deb96SPaul Moore return 0; 47474e5ab4cbSJames Morris 47484e5ab4cbSJames Morris /* Handle mapped IPv4 packets arriving via IPv6 sockets */ 474987fcd70dSAl Viro if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 47504e5ab4cbSJames Morris family = PF_INET; 47514e5ab4cbSJames Morris 4752d8395c87SPaul Moore /* If any sort of compatibility mode is enabled then handoff processing 4753d8395c87SPaul Moore * to the selinux_sock_rcv_skb_compat() function to deal with the 4754d8395c87SPaul Moore * special handling. We do this in an attempt to keep this function 4755d8395c87SPaul Moore * as fast and as clean as possible. */ 4756aa8e712cSStephen Smalley if (!selinux_policycap_netpeer()) 4757d8395c87SPaul Moore return selinux_sock_rcv_skb_compat(sk, skb, family); 4758d8395c87SPaul Moore 4759d8395c87SPaul Moore secmark_active = selinux_secmark_enabled(); 47602be4d74fSChris PeBenito peerlbl_active = selinux_peerlbl_enabled(); 4761d8395c87SPaul Moore if (!secmark_active && !peerlbl_active) 4762d8395c87SPaul Moore return 0; 4763d8395c87SPaul Moore 476450c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 476548c62af6SEric Paris ad.u.net = &net; 476648c62af6SEric Paris ad.u.net->netif = skb->skb_iif; 476748c62af6SEric Paris ad.u.net->family = family; 4768224dfbd8SPaul Moore err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); 47694e5ab4cbSJames Morris if (err) 4770220deb96SPaul Moore return err; 47714e5ab4cbSJames Morris 4772d8395c87SPaul Moore if (peerlbl_active) { 4773d621d35eSPaul Moore u32 peer_sid; 4774220deb96SPaul Moore 4775220deb96SPaul Moore err = selinux_skb_peerlbl_sid(skb, family, &peer_sid); 4776220deb96SPaul Moore if (err) 4777220deb96SPaul Moore return err; 4778cbe0d6e8SPaul Moore err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif, 4779cbe0d6e8SPaul Moore addrp, family, peer_sid, &ad); 4780dfaebe98SPaul Moore if (err) { 4781a04e71f6SHuw Davies selinux_netlbl_err(skb, family, err, 0); 4782effad8dfSPaul Moore return err; 4783dfaebe98SPaul Moore } 47846b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 47856b6bc620SStephen Smalley sk_sid, peer_sid, SECCLASS_PEER, 4786d621d35eSPaul Moore PEER__RECV, &ad); 478746d01d63SChad Hanson if (err) { 4788a04e71f6SHuw Davies selinux_netlbl_err(skb, family, err, 0); 478946d01d63SChad Hanson return err; 479046d01d63SChad Hanson } 4791d621d35eSPaul Moore } 4792d621d35eSPaul Moore 4793d8395c87SPaul Moore if (secmark_active) { 47946b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 47956b6bc620SStephen Smalley sk_sid, skb->secmark, SECCLASS_PACKET, 4796effad8dfSPaul Moore PACKET__RECV, &ad); 4797effad8dfSPaul Moore if (err) 4798effad8dfSPaul Moore return err; 4799effad8dfSPaul Moore } 4800effad8dfSPaul Moore 4801d621d35eSPaul Moore return err; 48021da177e4SLinus Torvalds } 48031da177e4SLinus Torvalds 48042c7946a7SCatherine Zhang static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval, 48051da177e4SLinus Torvalds int __user *optlen, unsigned len) 48061da177e4SLinus Torvalds { 48071da177e4SLinus Torvalds int err = 0; 48081da177e4SLinus Torvalds char *scontext; 48091da177e4SLinus Torvalds u32 scontext_len; 4810253bfae6SPaul Moore struct sk_security_struct *sksec = sock->sk->sk_security; 48113de4bab5SPaul Moore u32 peer_sid = SECSID_NULL; 48121da177e4SLinus Torvalds 4813253bfae6SPaul Moore if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET || 4814d452930fSRichard Haines sksec->sclass == SECCLASS_TCP_SOCKET || 4815d452930fSRichard Haines sksec->sclass == SECCLASS_SCTP_SOCKET) 4816dd3e7836SEric Paris peer_sid = sksec->peer_sid; 4817253bfae6SPaul Moore if (peer_sid == SECSID_NULL) 4818253bfae6SPaul Moore return -ENOPROTOOPT; 48191da177e4SLinus Torvalds 4820aa8e712cSStephen Smalley err = security_sid_to_context(&selinux_state, peer_sid, &scontext, 4821aa8e712cSStephen Smalley &scontext_len); 48221da177e4SLinus Torvalds if (err) 4823253bfae6SPaul Moore return err; 48241da177e4SLinus Torvalds 48251da177e4SLinus Torvalds if (scontext_len > len) { 48261da177e4SLinus Torvalds err = -ERANGE; 48271da177e4SLinus Torvalds goto out_len; 48281da177e4SLinus Torvalds } 48291da177e4SLinus Torvalds 48301da177e4SLinus Torvalds if (copy_to_user(optval, scontext, scontext_len)) 48311da177e4SLinus Torvalds err = -EFAULT; 48321da177e4SLinus Torvalds 48331da177e4SLinus Torvalds out_len: 48341da177e4SLinus Torvalds if (put_user(scontext_len, optlen)) 48351da177e4SLinus Torvalds err = -EFAULT; 48361da177e4SLinus Torvalds kfree(scontext); 48371da177e4SLinus Torvalds return err; 48381da177e4SLinus Torvalds } 48391da177e4SLinus Torvalds 4840dc49c1f9SCatherine Zhang static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid) 48412c7946a7SCatherine Zhang { 4842dc49c1f9SCatherine Zhang u32 peer_secid = SECSID_NULL; 484375e22910SPaul Moore u16 family; 4844899134f2SPaul Moore struct inode_security_struct *isec; 4845877ce7c1SCatherine Zhang 4846aa862900SPaul Moore if (skb && skb->protocol == htons(ETH_P_IP)) 4847aa862900SPaul Moore family = PF_INET; 4848aa862900SPaul Moore else if (skb && skb->protocol == htons(ETH_P_IPV6)) 4849aa862900SPaul Moore family = PF_INET6; 4850aa862900SPaul Moore else if (sock) 485175e22910SPaul Moore family = sock->sk->sk_family; 485275e22910SPaul Moore else 485375e22910SPaul Moore goto out; 485475e22910SPaul Moore 4855899134f2SPaul Moore if (sock && family == PF_UNIX) { 4856899134f2SPaul Moore isec = inode_security_novalidate(SOCK_INODE(sock)); 4857899134f2SPaul Moore peer_secid = isec->sid; 4858899134f2SPaul Moore } else if (skb) 4859220deb96SPaul Moore selinux_skb_peerlbl_sid(skb, family, &peer_secid); 48602c7946a7SCatherine Zhang 486175e22910SPaul Moore out: 4862dc49c1f9SCatherine Zhang *secid = peer_secid; 486375e22910SPaul Moore if (peer_secid == SECSID_NULL) 486475e22910SPaul Moore return -EINVAL; 486575e22910SPaul Moore return 0; 48662c7946a7SCatherine Zhang } 48672c7946a7SCatherine Zhang 48687d877f3bSAl Viro static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority) 48691da177e4SLinus Torvalds { 487084914b7eSPaul Moore struct sk_security_struct *sksec; 487184914b7eSPaul Moore 487284914b7eSPaul Moore sksec = kzalloc(sizeof(*sksec), priority); 487384914b7eSPaul Moore if (!sksec) 487484914b7eSPaul Moore return -ENOMEM; 487584914b7eSPaul Moore 487684914b7eSPaul Moore sksec->peer_sid = SECINITSID_UNLABELED; 487784914b7eSPaul Moore sksec->sid = SECINITSID_UNLABELED; 48785dee25d0SStephen Smalley sksec->sclass = SECCLASS_SOCKET; 487984914b7eSPaul Moore selinux_netlbl_sk_security_reset(sksec); 488084914b7eSPaul Moore sk->sk_security = sksec; 488184914b7eSPaul Moore 488284914b7eSPaul Moore return 0; 48831da177e4SLinus Torvalds } 48841da177e4SLinus Torvalds 48851da177e4SLinus Torvalds static void selinux_sk_free_security(struct sock *sk) 48861da177e4SLinus Torvalds { 488784914b7eSPaul Moore struct sk_security_struct *sksec = sk->sk_security; 488884914b7eSPaul Moore 488984914b7eSPaul Moore sk->sk_security = NULL; 489084914b7eSPaul Moore selinux_netlbl_sk_security_free(sksec); 489184914b7eSPaul Moore kfree(sksec); 48921da177e4SLinus Torvalds } 48931da177e4SLinus Torvalds 4894892c141eSVenkat Yekkirala static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk) 4895892c141eSVenkat Yekkirala { 4896dd3e7836SEric Paris struct sk_security_struct *sksec = sk->sk_security; 4897dd3e7836SEric Paris struct sk_security_struct *newsksec = newsk->sk_security; 4898892c141eSVenkat Yekkirala 4899dd3e7836SEric Paris newsksec->sid = sksec->sid; 4900dd3e7836SEric Paris newsksec->peer_sid = sksec->peer_sid; 4901dd3e7836SEric Paris newsksec->sclass = sksec->sclass; 490299f59ed0SPaul Moore 4903dd3e7836SEric Paris selinux_netlbl_sk_security_reset(newsksec); 4904892c141eSVenkat Yekkirala } 4905892c141eSVenkat Yekkirala 4906beb8d13bSVenkat Yekkirala static void selinux_sk_getsecid(struct sock *sk, u32 *secid) 4907d28d1e08STrent Jaeger { 4908d28d1e08STrent Jaeger if (!sk) 4909beb8d13bSVenkat Yekkirala *secid = SECINITSID_ANY_SOCKET; 4910892c141eSVenkat Yekkirala else { 4911892c141eSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 4912d28d1e08STrent Jaeger 4913beb8d13bSVenkat Yekkirala *secid = sksec->sid; 4914892c141eSVenkat Yekkirala } 4915d28d1e08STrent Jaeger } 4916d28d1e08STrent Jaeger 49179a673e56SAdrian Bunk static void selinux_sock_graft(struct sock *sk, struct socket *parent) 49184237c75cSVenkat Yekkirala { 49195d226df4SAndreas Gruenbacher struct inode_security_struct *isec = 49205d226df4SAndreas Gruenbacher inode_security_novalidate(SOCK_INODE(parent)); 49214237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 49224237c75cSVenkat Yekkirala 49232873ead7SPaul Moore if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 || 49242873ead7SPaul Moore sk->sk_family == PF_UNIX) 49254237c75cSVenkat Yekkirala isec->sid = sksec->sid; 4926220deb96SPaul Moore sksec->sclass = isec->sclass; 49274237c75cSVenkat Yekkirala } 49284237c75cSVenkat Yekkirala 4929d452930fSRichard Haines /* Called whenever SCTP receives an INIT chunk. This happens when an incoming 4930d452930fSRichard Haines * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association 4931d452930fSRichard Haines * already present). 4932d452930fSRichard Haines */ 4933d452930fSRichard Haines static int selinux_sctp_assoc_request(struct sctp_endpoint *ep, 4934d452930fSRichard Haines struct sk_buff *skb) 4935d452930fSRichard Haines { 4936d452930fSRichard Haines struct sk_security_struct *sksec = ep->base.sk->sk_security; 4937d452930fSRichard Haines struct common_audit_data ad; 4938d452930fSRichard Haines struct lsm_network_audit net = {0,}; 4939d452930fSRichard Haines u8 peerlbl_active; 4940d452930fSRichard Haines u32 peer_sid = SECINITSID_UNLABELED; 4941d452930fSRichard Haines u32 conn_sid; 4942d452930fSRichard Haines int err = 0; 4943d452930fSRichard Haines 4944aa8e712cSStephen Smalley if (!selinux_policycap_extsockclass()) 4945d452930fSRichard Haines return 0; 4946d452930fSRichard Haines 4947d452930fSRichard Haines peerlbl_active = selinux_peerlbl_enabled(); 4948d452930fSRichard Haines 4949d452930fSRichard Haines if (peerlbl_active) { 4950d452930fSRichard Haines /* This will return peer_sid = SECSID_NULL if there are 4951d452930fSRichard Haines * no peer labels, see security_net_peersid_resolve(). 4952d452930fSRichard Haines */ 4953d452930fSRichard Haines err = selinux_skb_peerlbl_sid(skb, ep->base.sk->sk_family, 4954d452930fSRichard Haines &peer_sid); 4955d452930fSRichard Haines if (err) 4956d452930fSRichard Haines return err; 4957d452930fSRichard Haines 4958d452930fSRichard Haines if (peer_sid == SECSID_NULL) 4959d452930fSRichard Haines peer_sid = SECINITSID_UNLABELED; 4960d452930fSRichard Haines } 4961d452930fSRichard Haines 4962d452930fSRichard Haines if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) { 4963d452930fSRichard Haines sksec->sctp_assoc_state = SCTP_ASSOC_SET; 4964d452930fSRichard Haines 4965d452930fSRichard Haines /* Here as first association on socket. As the peer SID 4966d452930fSRichard Haines * was allowed by peer recv (and the netif/node checks), 4967d452930fSRichard Haines * then it is approved by policy and used as the primary 4968d452930fSRichard Haines * peer SID for getpeercon(3). 4969d452930fSRichard Haines */ 4970d452930fSRichard Haines sksec->peer_sid = peer_sid; 4971d452930fSRichard Haines } else if (sksec->peer_sid != peer_sid) { 4972d452930fSRichard Haines /* Other association peer SIDs are checked to enforce 4973d452930fSRichard Haines * consistency among the peer SIDs. 4974d452930fSRichard Haines */ 4975d452930fSRichard Haines ad.type = LSM_AUDIT_DATA_NET; 4976d452930fSRichard Haines ad.u.net = &net; 4977d452930fSRichard Haines ad.u.net->sk = ep->base.sk; 49786b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 49796b6bc620SStephen Smalley sksec->peer_sid, peer_sid, sksec->sclass, 4980d452930fSRichard Haines SCTP_SOCKET__ASSOCIATION, &ad); 4981d452930fSRichard Haines if (err) 4982d452930fSRichard Haines return err; 4983d452930fSRichard Haines } 4984d452930fSRichard Haines 4985d452930fSRichard Haines /* Compute the MLS component for the connection and store 4986d452930fSRichard Haines * the information in ep. This will be used by SCTP TCP type 4987d452930fSRichard Haines * sockets and peeled off connections as they cause a new 4988d452930fSRichard Haines * socket to be generated. selinux_sctp_sk_clone() will then 4989d452930fSRichard Haines * plug this into the new socket. 4990d452930fSRichard Haines */ 4991d452930fSRichard Haines err = selinux_conn_sid(sksec->sid, peer_sid, &conn_sid); 4992d452930fSRichard Haines if (err) 4993d452930fSRichard Haines return err; 4994d452930fSRichard Haines 4995d452930fSRichard Haines ep->secid = conn_sid; 4996d452930fSRichard Haines ep->peer_secid = peer_sid; 4997d452930fSRichard Haines 4998d452930fSRichard Haines /* Set any NetLabel labels including CIPSO/CALIPSO options. */ 4999d452930fSRichard Haines return selinux_netlbl_sctp_assoc_request(ep, skb); 5000d452930fSRichard Haines } 5001d452930fSRichard Haines 5002d452930fSRichard Haines /* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting 5003d452930fSRichard Haines * based on their @optname. 5004d452930fSRichard Haines */ 5005d452930fSRichard Haines static int selinux_sctp_bind_connect(struct sock *sk, int optname, 5006d452930fSRichard Haines struct sockaddr *address, 5007d452930fSRichard Haines int addrlen) 5008d452930fSRichard Haines { 5009d452930fSRichard Haines int len, err = 0, walk_size = 0; 5010d452930fSRichard Haines void *addr_buf; 5011d452930fSRichard Haines struct sockaddr *addr; 5012d452930fSRichard Haines struct socket *sock; 5013d452930fSRichard Haines 5014aa8e712cSStephen Smalley if (!selinux_policycap_extsockclass()) 5015d452930fSRichard Haines return 0; 5016d452930fSRichard Haines 5017d452930fSRichard Haines /* Process one or more addresses that may be IPv4 or IPv6 */ 5018d452930fSRichard Haines sock = sk->sk_socket; 5019d452930fSRichard Haines addr_buf = address; 5020d452930fSRichard Haines 5021d452930fSRichard Haines while (walk_size < addrlen) { 5022c138325fSOndrej Mosnacek if (walk_size + sizeof(sa_family_t) > addrlen) 5023c138325fSOndrej Mosnacek return -EINVAL; 5024c138325fSOndrej Mosnacek 5025d452930fSRichard Haines addr = addr_buf; 5026d452930fSRichard Haines switch (addr->sa_family) { 50274152dc91SAlexey Kodanev case AF_UNSPEC: 5028d452930fSRichard Haines case AF_INET: 5029d452930fSRichard Haines len = sizeof(struct sockaddr_in); 5030d452930fSRichard Haines break; 5031d452930fSRichard Haines case AF_INET6: 5032d452930fSRichard Haines len = sizeof(struct sockaddr_in6); 5033d452930fSRichard Haines break; 5034d452930fSRichard Haines default: 50354152dc91SAlexey Kodanev return -EINVAL; 5036d452930fSRichard Haines } 5037d452930fSRichard Haines 5038d452930fSRichard Haines err = -EINVAL; 5039d452930fSRichard Haines switch (optname) { 5040d452930fSRichard Haines /* Bind checks */ 5041d452930fSRichard Haines case SCTP_PRIMARY_ADDR: 5042d452930fSRichard Haines case SCTP_SET_PEER_PRIMARY_ADDR: 5043d452930fSRichard Haines case SCTP_SOCKOPT_BINDX_ADD: 5044d452930fSRichard Haines err = selinux_socket_bind(sock, addr, len); 5045d452930fSRichard Haines break; 5046d452930fSRichard Haines /* Connect checks */ 5047d452930fSRichard Haines case SCTP_SOCKOPT_CONNECTX: 5048d452930fSRichard Haines case SCTP_PARAM_SET_PRIMARY: 5049d452930fSRichard Haines case SCTP_PARAM_ADD_IP: 5050d452930fSRichard Haines case SCTP_SENDMSG_CONNECT: 5051d452930fSRichard Haines err = selinux_socket_connect_helper(sock, addr, len); 5052d452930fSRichard Haines if (err) 5053d452930fSRichard Haines return err; 5054d452930fSRichard Haines 5055d452930fSRichard Haines /* As selinux_sctp_bind_connect() is called by the 5056d452930fSRichard Haines * SCTP protocol layer, the socket is already locked, 5057d452930fSRichard Haines * therefore selinux_netlbl_socket_connect_locked() is 5058d452930fSRichard Haines * is called here. The situations handled are: 5059d452930fSRichard Haines * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2), 5060d452930fSRichard Haines * whenever a new IP address is added or when a new 5061d452930fSRichard Haines * primary address is selected. 5062d452930fSRichard Haines * Note that an SCTP connect(2) call happens before 5063d452930fSRichard Haines * the SCTP protocol layer and is handled via 5064d452930fSRichard Haines * selinux_socket_connect(). 5065d452930fSRichard Haines */ 5066d452930fSRichard Haines err = selinux_netlbl_socket_connect_locked(sk, addr); 5067d452930fSRichard Haines break; 5068d452930fSRichard Haines } 5069d452930fSRichard Haines 5070d452930fSRichard Haines if (err) 5071d452930fSRichard Haines return err; 5072d452930fSRichard Haines 5073d452930fSRichard Haines addr_buf += len; 5074d452930fSRichard Haines walk_size += len; 5075d452930fSRichard Haines } 5076d452930fSRichard Haines 5077d452930fSRichard Haines return 0; 5078d452930fSRichard Haines } 5079d452930fSRichard Haines 5080d452930fSRichard Haines /* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */ 5081d452930fSRichard Haines static void selinux_sctp_sk_clone(struct sctp_endpoint *ep, struct sock *sk, 5082d452930fSRichard Haines struct sock *newsk) 5083d452930fSRichard Haines { 5084d452930fSRichard Haines struct sk_security_struct *sksec = sk->sk_security; 5085d452930fSRichard Haines struct sk_security_struct *newsksec = newsk->sk_security; 5086d452930fSRichard Haines 5087d452930fSRichard Haines /* If policy does not support SECCLASS_SCTP_SOCKET then call 5088d452930fSRichard Haines * the non-sctp clone version. 5089d452930fSRichard Haines */ 5090aa8e712cSStephen Smalley if (!selinux_policycap_extsockclass()) 5091d452930fSRichard Haines return selinux_sk_clone_security(sk, newsk); 5092d452930fSRichard Haines 5093d452930fSRichard Haines newsksec->sid = ep->secid; 5094d452930fSRichard Haines newsksec->peer_sid = ep->peer_secid; 5095d452930fSRichard Haines newsksec->sclass = sksec->sclass; 5096d452930fSRichard Haines selinux_netlbl_sctp_sk_clone(sk, newsk); 5097d452930fSRichard Haines } 5098d452930fSRichard Haines 50999a673e56SAdrian Bunk static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb, 51004237c75cSVenkat Yekkirala struct request_sock *req) 51014237c75cSVenkat Yekkirala { 51024237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 51034237c75cSVenkat Yekkirala int err; 51040b1f24e6SPaul Moore u16 family = req->rsk_ops->family; 5105446b8024SPaul Moore u32 connsid; 51064237c75cSVenkat Yekkirala u32 peersid; 51074237c75cSVenkat Yekkirala 5108aa862900SPaul Moore err = selinux_skb_peerlbl_sid(skb, family, &peersid); 5109220deb96SPaul Moore if (err) 5110220deb96SPaul Moore return err; 5111446b8024SPaul Moore err = selinux_conn_sid(sksec->sid, peersid, &connsid); 51124237c75cSVenkat Yekkirala if (err) 51134237c75cSVenkat Yekkirala return err; 5114446b8024SPaul Moore req->secid = connsid; 51156b877699SVenkat Yekkirala req->peer_secid = peersid; 5116389fb800SPaul Moore 5117389fb800SPaul Moore return selinux_netlbl_inet_conn_request(req, family); 51184237c75cSVenkat Yekkirala } 51194237c75cSVenkat Yekkirala 51209a673e56SAdrian Bunk static void selinux_inet_csk_clone(struct sock *newsk, 51219a673e56SAdrian Bunk const struct request_sock *req) 51224237c75cSVenkat Yekkirala { 51234237c75cSVenkat Yekkirala struct sk_security_struct *newsksec = newsk->sk_security; 51244237c75cSVenkat Yekkirala 51254237c75cSVenkat Yekkirala newsksec->sid = req->secid; 51266b877699SVenkat Yekkirala newsksec->peer_sid = req->peer_secid; 51274237c75cSVenkat Yekkirala /* NOTE: Ideally, we should also get the isec->sid for the 51284237c75cSVenkat Yekkirala new socket in sync, but we don't have the isec available yet. 51294237c75cSVenkat Yekkirala So we will wait until sock_graft to do it, by which 51304237c75cSVenkat Yekkirala time it will have been created and available. */ 513199f59ed0SPaul Moore 51329f2ad665SPaul Moore /* We don't need to take any sort of lock here as we are the only 51339f2ad665SPaul Moore * thread with access to newsksec */ 5134389fb800SPaul Moore selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family); 51354237c75cSVenkat Yekkirala } 51364237c75cSVenkat Yekkirala 5137014ab19aSPaul Moore static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb) 51386b877699SVenkat Yekkirala { 5139aa862900SPaul Moore u16 family = sk->sk_family; 51406b877699SVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 51416b877699SVenkat Yekkirala 5142aa862900SPaul Moore /* handle mapped IPv4 packets arriving via IPv6 sockets */ 5143aa862900SPaul Moore if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 5144aa862900SPaul Moore family = PF_INET; 5145aa862900SPaul Moore 5146aa862900SPaul Moore selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid); 51476b877699SVenkat Yekkirala } 51486b877699SVenkat Yekkirala 51492606fd1fSEric Paris static int selinux_secmark_relabel_packet(u32 sid) 51502606fd1fSEric Paris { 51512606fd1fSEric Paris const struct task_security_struct *__tsec; 51522606fd1fSEric Paris u32 tsid; 51532606fd1fSEric Paris 51540c6cfa62SCasey Schaufler __tsec = selinux_cred(current_cred()); 51552606fd1fSEric Paris tsid = __tsec->sid; 51562606fd1fSEric Paris 51576b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 51586b6bc620SStephen Smalley tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, 51596b6bc620SStephen Smalley NULL); 51602606fd1fSEric Paris } 51612606fd1fSEric Paris 51622606fd1fSEric Paris static void selinux_secmark_refcount_inc(void) 51632606fd1fSEric Paris { 51642606fd1fSEric Paris atomic_inc(&selinux_secmark_refcount); 51652606fd1fSEric Paris } 51662606fd1fSEric Paris 51672606fd1fSEric Paris static void selinux_secmark_refcount_dec(void) 51682606fd1fSEric Paris { 51692606fd1fSEric Paris atomic_dec(&selinux_secmark_refcount); 51702606fd1fSEric Paris } 51712606fd1fSEric Paris 51729a673e56SAdrian Bunk static void selinux_req_classify_flow(const struct request_sock *req, 51739a673e56SAdrian Bunk struct flowi *fl) 51744237c75cSVenkat Yekkirala { 51751d28f42cSDavid S. Miller fl->flowi_secid = req->secid; 51764237c75cSVenkat Yekkirala } 51774237c75cSVenkat Yekkirala 51785dbbaf2dSPaul Moore static int selinux_tun_dev_alloc_security(void **security) 51795dbbaf2dSPaul Moore { 51805dbbaf2dSPaul Moore struct tun_security_struct *tunsec; 51815dbbaf2dSPaul Moore 51825dbbaf2dSPaul Moore tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL); 51835dbbaf2dSPaul Moore if (!tunsec) 51845dbbaf2dSPaul Moore return -ENOMEM; 51855dbbaf2dSPaul Moore tunsec->sid = current_sid(); 51865dbbaf2dSPaul Moore 51875dbbaf2dSPaul Moore *security = tunsec; 51885dbbaf2dSPaul Moore return 0; 51895dbbaf2dSPaul Moore } 51905dbbaf2dSPaul Moore 51915dbbaf2dSPaul Moore static void selinux_tun_dev_free_security(void *security) 51925dbbaf2dSPaul Moore { 51935dbbaf2dSPaul Moore kfree(security); 51945dbbaf2dSPaul Moore } 51955dbbaf2dSPaul Moore 5196ed6d76e4SPaul Moore static int selinux_tun_dev_create(void) 5197ed6d76e4SPaul Moore { 5198ed6d76e4SPaul Moore u32 sid = current_sid(); 5199ed6d76e4SPaul Moore 5200ed6d76e4SPaul Moore /* we aren't taking into account the "sockcreate" SID since the socket 5201ed6d76e4SPaul Moore * that is being created here is not a socket in the traditional sense, 5202ed6d76e4SPaul Moore * instead it is a private sock, accessible only to the kernel, and 5203ed6d76e4SPaul Moore * representing a wide range of network traffic spanning multiple 5204ed6d76e4SPaul Moore * connections unlike traditional sockets - check the TUN driver to 5205ed6d76e4SPaul Moore * get a better understanding of why this socket is special */ 5206ed6d76e4SPaul Moore 52076b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 52086b6bc620SStephen Smalley sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE, 5209ed6d76e4SPaul Moore NULL); 5210ed6d76e4SPaul Moore } 5211ed6d76e4SPaul Moore 52125dbbaf2dSPaul Moore static int selinux_tun_dev_attach_queue(void *security) 5213ed6d76e4SPaul Moore { 52145dbbaf2dSPaul Moore struct tun_security_struct *tunsec = security; 52155dbbaf2dSPaul Moore 52166b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 52176b6bc620SStephen Smalley current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET, 52185dbbaf2dSPaul Moore TUN_SOCKET__ATTACH_QUEUE, NULL); 52195dbbaf2dSPaul Moore } 52205dbbaf2dSPaul Moore 52215dbbaf2dSPaul Moore static int selinux_tun_dev_attach(struct sock *sk, void *security) 52225dbbaf2dSPaul Moore { 52235dbbaf2dSPaul Moore struct tun_security_struct *tunsec = security; 5224ed6d76e4SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 5225ed6d76e4SPaul Moore 5226ed6d76e4SPaul Moore /* we don't currently perform any NetLabel based labeling here and it 5227ed6d76e4SPaul Moore * isn't clear that we would want to do so anyway; while we could apply 5228ed6d76e4SPaul Moore * labeling without the support of the TUN user the resulting labeled 5229ed6d76e4SPaul Moore * traffic from the other end of the connection would almost certainly 5230ed6d76e4SPaul Moore * cause confusion to the TUN user that had no idea network labeling 5231ed6d76e4SPaul Moore * protocols were being used */ 5232ed6d76e4SPaul Moore 52335dbbaf2dSPaul Moore sksec->sid = tunsec->sid; 5234ed6d76e4SPaul Moore sksec->sclass = SECCLASS_TUN_SOCKET; 52355dbbaf2dSPaul Moore 52365dbbaf2dSPaul Moore return 0; 5237ed6d76e4SPaul Moore } 5238ed6d76e4SPaul Moore 52395dbbaf2dSPaul Moore static int selinux_tun_dev_open(void *security) 5240ed6d76e4SPaul Moore { 52415dbbaf2dSPaul Moore struct tun_security_struct *tunsec = security; 5242ed6d76e4SPaul Moore u32 sid = current_sid(); 5243ed6d76e4SPaul Moore int err; 5244ed6d76e4SPaul Moore 52456b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 52466b6bc620SStephen Smalley sid, tunsec->sid, SECCLASS_TUN_SOCKET, 5247ed6d76e4SPaul Moore TUN_SOCKET__RELABELFROM, NULL); 5248ed6d76e4SPaul Moore if (err) 5249ed6d76e4SPaul Moore return err; 52506b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 52516b6bc620SStephen Smalley sid, sid, SECCLASS_TUN_SOCKET, 5252ed6d76e4SPaul Moore TUN_SOCKET__RELABELTO, NULL); 5253ed6d76e4SPaul Moore if (err) 5254ed6d76e4SPaul Moore return err; 52555dbbaf2dSPaul Moore tunsec->sid = sid; 5256ed6d76e4SPaul Moore 5257ed6d76e4SPaul Moore return 0; 5258ed6d76e4SPaul Moore } 5259ed6d76e4SPaul Moore 52601da177e4SLinus Torvalds static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb) 52611da177e4SLinus Torvalds { 52621da177e4SLinus Torvalds int err = 0; 52631da177e4SLinus Torvalds u32 perm; 52641da177e4SLinus Torvalds struct nlmsghdr *nlh; 5265253bfae6SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 52661da177e4SLinus Torvalds 526777954983SHong zhi guo if (skb->len < NLMSG_HDRLEN) { 52681da177e4SLinus Torvalds err = -EINVAL; 52691da177e4SLinus Torvalds goto out; 52701da177e4SLinus Torvalds } 5271b529ccf2SArnaldo Carvalho de Melo nlh = nlmsg_hdr(skb); 52721da177e4SLinus Torvalds 5273253bfae6SPaul Moore err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm); 52741da177e4SLinus Torvalds if (err) { 52751da177e4SLinus Torvalds if (err == -EINVAL) { 527676319946SVladis Dronov pr_warn_ratelimited("SELinux: unrecognized netlink" 527776319946SVladis Dronov " message: protocol=%hu nlmsg_type=%hu sclass=%s" 527876319946SVladis Dronov " pig=%d comm=%s\n", 5279cded3fffSMarek Milkovic sk->sk_protocol, nlh->nlmsg_type, 528076319946SVladis Dronov secclass_map[sksec->sclass - 1].name, 528176319946SVladis Dronov task_pid_nr(current), current->comm); 5282e5a5ca96SPaul Moore if (!enforcing_enabled(&selinux_state) || 5283aa8e712cSStephen Smalley security_get_allow_unknown(&selinux_state)) 52841da177e4SLinus Torvalds err = 0; 52851da177e4SLinus Torvalds } 52861da177e4SLinus Torvalds 52871da177e4SLinus Torvalds /* Ignore */ 52881da177e4SLinus Torvalds if (err == -ENOENT) 52891da177e4SLinus Torvalds err = 0; 52901da177e4SLinus Torvalds goto out; 52911da177e4SLinus Torvalds } 52921da177e4SLinus Torvalds 5293be0554c9SStephen Smalley err = sock_has_perm(sk, perm); 52941da177e4SLinus Torvalds out: 52951da177e4SLinus Torvalds return err; 52961da177e4SLinus Torvalds } 52971da177e4SLinus Torvalds 52981da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER 52991da177e4SLinus Torvalds 5300cbe0d6e8SPaul Moore static unsigned int selinux_ip_forward(struct sk_buff *skb, 5301cbe0d6e8SPaul Moore const struct net_device *indev, 5302effad8dfSPaul Moore u16 family) 53031da177e4SLinus Torvalds { 5304dfaebe98SPaul Moore int err; 5305effad8dfSPaul Moore char *addrp; 5306effad8dfSPaul Moore u32 peer_sid; 53072bf49690SThomas Liu struct common_audit_data ad; 530848c62af6SEric Paris struct lsm_network_audit net = {0,}; 5309effad8dfSPaul Moore u8 secmark_active; 5310948bf85cSPaul Moore u8 netlbl_active; 5311effad8dfSPaul Moore u8 peerlbl_active; 53124237c75cSVenkat Yekkirala 5313aa8e712cSStephen Smalley if (!selinux_policycap_netpeer()) 5314effad8dfSPaul Moore return NF_ACCEPT; 53154237c75cSVenkat Yekkirala 5316effad8dfSPaul Moore secmark_active = selinux_secmark_enabled(); 5317948bf85cSPaul Moore netlbl_active = netlbl_enabled(); 53182be4d74fSChris PeBenito peerlbl_active = selinux_peerlbl_enabled(); 5319effad8dfSPaul Moore if (!secmark_active && !peerlbl_active) 5320effad8dfSPaul Moore return NF_ACCEPT; 53214237c75cSVenkat Yekkirala 5322d8395c87SPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0) 5323d8395c87SPaul Moore return NF_DROP; 5324d8395c87SPaul Moore 532550c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 532648c62af6SEric Paris ad.u.net = &net; 5327cbe0d6e8SPaul Moore ad.u.net->netif = indev->ifindex; 532848c62af6SEric Paris ad.u.net->family = family; 5329effad8dfSPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0) 5330effad8dfSPaul Moore return NF_DROP; 53311da177e4SLinus Torvalds 5332dfaebe98SPaul Moore if (peerlbl_active) { 5333cbe0d6e8SPaul Moore err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex, 5334cbe0d6e8SPaul Moore addrp, family, peer_sid, &ad); 5335dfaebe98SPaul Moore if (err) { 5336a04e71f6SHuw Davies selinux_netlbl_err(skb, family, err, 1); 5337effad8dfSPaul Moore return NF_DROP; 5338dfaebe98SPaul Moore } 5339dfaebe98SPaul Moore } 5340effad8dfSPaul Moore 5341effad8dfSPaul Moore if (secmark_active) 53426b6bc620SStephen Smalley if (avc_has_perm(&selinux_state, 53436b6bc620SStephen Smalley peer_sid, skb->secmark, 5344effad8dfSPaul Moore SECCLASS_PACKET, PACKET__FORWARD_IN, &ad)) 5345effad8dfSPaul Moore return NF_DROP; 5346effad8dfSPaul Moore 5347948bf85cSPaul Moore if (netlbl_active) 5348948bf85cSPaul Moore /* we do this in the FORWARD path and not the POST_ROUTING 5349948bf85cSPaul Moore * path because we want to make sure we apply the necessary 5350948bf85cSPaul Moore * labeling before IPsec is applied so we can leverage AH 5351948bf85cSPaul Moore * protection */ 5352948bf85cSPaul Moore if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0) 5353948bf85cSPaul Moore return NF_DROP; 5354948bf85cSPaul Moore 5355effad8dfSPaul Moore return NF_ACCEPT; 5356effad8dfSPaul Moore } 5357effad8dfSPaul Moore 535806198b34SEric W. Biederman static unsigned int selinux_ipv4_forward(void *priv, 5359effad8dfSPaul Moore struct sk_buff *skb, 5360238e54c9SDavid S. Miller const struct nf_hook_state *state) 5361effad8dfSPaul Moore { 5362238e54c9SDavid S. Miller return selinux_ip_forward(skb, state->in, PF_INET); 5363effad8dfSPaul Moore } 5364effad8dfSPaul Moore 53651a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 536606198b34SEric W. Biederman static unsigned int selinux_ipv6_forward(void *priv, 5367effad8dfSPaul Moore struct sk_buff *skb, 5368238e54c9SDavid S. Miller const struct nf_hook_state *state) 5369effad8dfSPaul Moore { 5370238e54c9SDavid S. Miller return selinux_ip_forward(skb, state->in, PF_INET6); 5371effad8dfSPaul Moore } 5372effad8dfSPaul Moore #endif /* IPV6 */ 5373effad8dfSPaul Moore 5374948bf85cSPaul Moore static unsigned int selinux_ip_output(struct sk_buff *skb, 5375948bf85cSPaul Moore u16 family) 5376948bf85cSPaul Moore { 537747180068SPaul Moore struct sock *sk; 5378948bf85cSPaul Moore u32 sid; 5379948bf85cSPaul Moore 5380948bf85cSPaul Moore if (!netlbl_enabled()) 5381948bf85cSPaul Moore return NF_ACCEPT; 5382948bf85cSPaul Moore 5383948bf85cSPaul Moore /* we do this in the LOCAL_OUT path and not the POST_ROUTING path 5384948bf85cSPaul Moore * because we want to make sure we apply the necessary labeling 5385948bf85cSPaul Moore * before IPsec is applied so we can leverage AH protection */ 538647180068SPaul Moore sk = skb->sk; 538747180068SPaul Moore if (sk) { 538847180068SPaul Moore struct sk_security_struct *sksec; 538947180068SPaul Moore 5390e446f9dfSEric Dumazet if (sk_listener(sk)) 539147180068SPaul Moore /* if the socket is the listening state then this 539247180068SPaul Moore * packet is a SYN-ACK packet which means it needs to 539347180068SPaul Moore * be labeled based on the connection/request_sock and 539447180068SPaul Moore * not the parent socket. unfortunately, we can't 539547180068SPaul Moore * lookup the request_sock yet as it isn't queued on 539647180068SPaul Moore * the parent socket until after the SYN-ACK is sent. 539747180068SPaul Moore * the "solution" is to simply pass the packet as-is 539847180068SPaul Moore * as any IP option based labeling should be copied 539947180068SPaul Moore * from the initial connection request (in the IP 540047180068SPaul Moore * layer). it is far from ideal, but until we get a 540147180068SPaul Moore * security label in the packet itself this is the 540247180068SPaul Moore * best we can do. */ 540347180068SPaul Moore return NF_ACCEPT; 540447180068SPaul Moore 540547180068SPaul Moore /* standard practice, label using the parent socket */ 540647180068SPaul Moore sksec = sk->sk_security; 5407948bf85cSPaul Moore sid = sksec->sid; 5408948bf85cSPaul Moore } else 5409948bf85cSPaul Moore sid = SECINITSID_KERNEL; 5410948bf85cSPaul Moore if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0) 5411948bf85cSPaul Moore return NF_DROP; 5412948bf85cSPaul Moore 5413948bf85cSPaul Moore return NF_ACCEPT; 5414948bf85cSPaul Moore } 5415948bf85cSPaul Moore 541606198b34SEric W. Biederman static unsigned int selinux_ipv4_output(void *priv, 5417948bf85cSPaul Moore struct sk_buff *skb, 5418238e54c9SDavid S. Miller const struct nf_hook_state *state) 5419948bf85cSPaul Moore { 5420948bf85cSPaul Moore return selinux_ip_output(skb, PF_INET); 5421948bf85cSPaul Moore } 5422948bf85cSPaul Moore 54231a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 54242917f57bSHuw Davies static unsigned int selinux_ipv6_output(void *priv, 54252917f57bSHuw Davies struct sk_buff *skb, 54262917f57bSHuw Davies const struct nf_hook_state *state) 54272917f57bSHuw Davies { 54282917f57bSHuw Davies return selinux_ip_output(skb, PF_INET6); 54292917f57bSHuw Davies } 54302917f57bSHuw Davies #endif /* IPV6 */ 54312917f57bSHuw Davies 5432effad8dfSPaul Moore static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb, 5433effad8dfSPaul Moore int ifindex, 5434d8395c87SPaul Moore u16 family) 54354e5ab4cbSJames Morris { 543654abc686SEric Dumazet struct sock *sk = skb_to_full_sk(skb); 54374237c75cSVenkat Yekkirala struct sk_security_struct *sksec; 54382bf49690SThomas Liu struct common_audit_data ad; 543948c62af6SEric Paris struct lsm_network_audit net = {0,}; 5440d8395c87SPaul Moore char *addrp; 5441d8395c87SPaul Moore u8 proto; 54424e5ab4cbSJames Morris 5443effad8dfSPaul Moore if (sk == NULL) 5444effad8dfSPaul Moore return NF_ACCEPT; 54454237c75cSVenkat Yekkirala sksec = sk->sk_security; 54464e5ab4cbSJames Morris 544750c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 544848c62af6SEric Paris ad.u.net = &net; 544948c62af6SEric Paris ad.u.net->netif = ifindex; 545048c62af6SEric Paris ad.u.net->family = family; 5451d8395c87SPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto)) 5452d8395c87SPaul Moore return NF_DROP; 5453d8395c87SPaul Moore 545458bfbb51SPaul Moore if (selinux_secmark_enabled()) 54556b6bc620SStephen Smalley if (avc_has_perm(&selinux_state, 54566b6bc620SStephen Smalley sksec->sid, skb->secmark, 5457d8395c87SPaul Moore SECCLASS_PACKET, PACKET__SEND, &ad)) 54582fe66ec2SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 54591da177e4SLinus Torvalds 5460d8395c87SPaul Moore if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto)) 54612fe66ec2SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 5462effad8dfSPaul Moore 5463effad8dfSPaul Moore return NF_ACCEPT; 5464effad8dfSPaul Moore } 5465effad8dfSPaul Moore 5466cbe0d6e8SPaul Moore static unsigned int selinux_ip_postroute(struct sk_buff *skb, 5467cbe0d6e8SPaul Moore const struct net_device *outdev, 5468effad8dfSPaul Moore u16 family) 5469effad8dfSPaul Moore { 5470effad8dfSPaul Moore u32 secmark_perm; 5471effad8dfSPaul Moore u32 peer_sid; 5472cbe0d6e8SPaul Moore int ifindex = outdev->ifindex; 5473effad8dfSPaul Moore struct sock *sk; 54742bf49690SThomas Liu struct common_audit_data ad; 547548c62af6SEric Paris struct lsm_network_audit net = {0,}; 5476effad8dfSPaul Moore char *addrp; 5477effad8dfSPaul Moore u8 secmark_active; 5478effad8dfSPaul Moore u8 peerlbl_active; 5479effad8dfSPaul Moore 5480effad8dfSPaul Moore /* If any sort of compatibility mode is enabled then handoff processing 5481effad8dfSPaul Moore * to the selinux_ip_postroute_compat() function to deal with the 5482effad8dfSPaul Moore * special handling. We do this in an attempt to keep this function 5483effad8dfSPaul Moore * as fast and as clean as possible. */ 5484aa8e712cSStephen Smalley if (!selinux_policycap_netpeer()) 5485d8395c87SPaul Moore return selinux_ip_postroute_compat(skb, ifindex, family); 5486c0828e50SPaul Moore 5487effad8dfSPaul Moore secmark_active = selinux_secmark_enabled(); 54882be4d74fSChris PeBenito peerlbl_active = selinux_peerlbl_enabled(); 5489effad8dfSPaul Moore if (!secmark_active && !peerlbl_active) 5490effad8dfSPaul Moore return NF_ACCEPT; 5491effad8dfSPaul Moore 549254abc686SEric Dumazet sk = skb_to_full_sk(skb); 5493c0828e50SPaul Moore 5494effad8dfSPaul Moore #ifdef CONFIG_XFRM 5495effad8dfSPaul Moore /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec 5496effad8dfSPaul Moore * packet transformation so allow the packet to pass without any checks 5497effad8dfSPaul Moore * since we'll have another chance to perform access control checks 5498effad8dfSPaul Moore * when the packet is on it's final way out. 5499effad8dfSPaul Moore * NOTE: there appear to be some IPv6 multicast cases where skb->dst 5500c0828e50SPaul Moore * is NULL, in this case go ahead and apply access control. 5501c0828e50SPaul Moore * NOTE: if this is a local socket (skb->sk != NULL) that is in the 5502c0828e50SPaul Moore * TCP listening state we cannot wait until the XFRM processing 5503c0828e50SPaul Moore * is done as we will miss out on the SA label if we do; 5504c0828e50SPaul Moore * unfortunately, this means more work, but it is only once per 5505c0828e50SPaul Moore * connection. */ 5506c0828e50SPaul Moore if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL && 5507e446f9dfSEric Dumazet !(sk && sk_listener(sk))) 5508effad8dfSPaul Moore return NF_ACCEPT; 5509effad8dfSPaul Moore #endif 5510effad8dfSPaul Moore 5511d8395c87SPaul Moore if (sk == NULL) { 5512446b8024SPaul Moore /* Without an associated socket the packet is either coming 5513446b8024SPaul Moore * from the kernel or it is being forwarded; check the packet 5514446b8024SPaul Moore * to determine which and if the packet is being forwarded 5515446b8024SPaul Moore * query the packet directly to determine the security label. */ 55164a7ab3dcSSteffen Klassert if (skb->skb_iif) { 5517d8395c87SPaul Moore secmark_perm = PACKET__FORWARD_OUT; 5518d8395c87SPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &peer_sid)) 551904f6d70fSEric Paris return NF_DROP; 55204a7ab3dcSSteffen Klassert } else { 55214a7ab3dcSSteffen Klassert secmark_perm = PACKET__SEND; 5522d8395c87SPaul Moore peer_sid = SECINITSID_KERNEL; 55234a7ab3dcSSteffen Klassert } 5524e446f9dfSEric Dumazet } else if (sk_listener(sk)) { 5525446b8024SPaul Moore /* Locally generated packet but the associated socket is in the 5526446b8024SPaul Moore * listening state which means this is a SYN-ACK packet. In 5527446b8024SPaul Moore * this particular case the correct security label is assigned 5528446b8024SPaul Moore * to the connection/request_sock but unfortunately we can't 5529446b8024SPaul Moore * query the request_sock as it isn't queued on the parent 5530446b8024SPaul Moore * socket until after the SYN-ACK packet is sent; the only 5531446b8024SPaul Moore * viable choice is to regenerate the label like we do in 5532446b8024SPaul Moore * selinux_inet_conn_request(). See also selinux_ip_output() 5533446b8024SPaul Moore * for similar problems. */ 5534446b8024SPaul Moore u32 skb_sid; 5535e446f9dfSEric Dumazet struct sk_security_struct *sksec; 5536e446f9dfSEric Dumazet 5537e446f9dfSEric Dumazet sksec = sk->sk_security; 5538446b8024SPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &skb_sid)) 5539446b8024SPaul Moore return NF_DROP; 5540c0828e50SPaul Moore /* At this point, if the returned skb peerlbl is SECSID_NULL 5541c0828e50SPaul Moore * and the packet has been through at least one XFRM 5542c0828e50SPaul Moore * transformation then we must be dealing with the "final" 5543c0828e50SPaul Moore * form of labeled IPsec packet; since we've already applied 5544c0828e50SPaul Moore * all of our access controls on this packet we can safely 5545c0828e50SPaul Moore * pass the packet. */ 5546c0828e50SPaul Moore if (skb_sid == SECSID_NULL) { 5547c0828e50SPaul Moore switch (family) { 5548c0828e50SPaul Moore case PF_INET: 5549c0828e50SPaul Moore if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) 5550c0828e50SPaul Moore return NF_ACCEPT; 5551c0828e50SPaul Moore break; 5552c0828e50SPaul Moore case PF_INET6: 5553c0828e50SPaul Moore if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) 5554c0828e50SPaul Moore return NF_ACCEPT; 5555a7a91a19SPaul Moore break; 5556c0828e50SPaul Moore default: 5557c0828e50SPaul Moore return NF_DROP_ERR(-ECONNREFUSED); 5558c0828e50SPaul Moore } 5559c0828e50SPaul Moore } 5560446b8024SPaul Moore if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid)) 5561446b8024SPaul Moore return NF_DROP; 5562446b8024SPaul Moore secmark_perm = PACKET__SEND; 5563d8395c87SPaul Moore } else { 5564446b8024SPaul Moore /* Locally generated packet, fetch the security label from the 5565446b8024SPaul Moore * associated socket. */ 5566effad8dfSPaul Moore struct sk_security_struct *sksec = sk->sk_security; 5567effad8dfSPaul Moore peer_sid = sksec->sid; 5568effad8dfSPaul Moore secmark_perm = PACKET__SEND; 5569effad8dfSPaul Moore } 5570effad8dfSPaul Moore 557150c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 557248c62af6SEric Paris ad.u.net = &net; 557348c62af6SEric Paris ad.u.net->netif = ifindex; 557448c62af6SEric Paris ad.u.net->family = family; 5575d8395c87SPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL)) 557604f6d70fSEric Paris return NF_DROP; 5577d8395c87SPaul Moore 5578effad8dfSPaul Moore if (secmark_active) 55796b6bc620SStephen Smalley if (avc_has_perm(&selinux_state, 55806b6bc620SStephen Smalley peer_sid, skb->secmark, 5581effad8dfSPaul Moore SECCLASS_PACKET, secmark_perm, &ad)) 55821f1aaf82SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 5583effad8dfSPaul Moore 5584effad8dfSPaul Moore if (peerlbl_active) { 5585effad8dfSPaul Moore u32 if_sid; 5586effad8dfSPaul Moore u32 node_sid; 5587effad8dfSPaul Moore 5588cbe0d6e8SPaul Moore if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid)) 558904f6d70fSEric Paris return NF_DROP; 55906b6bc620SStephen Smalley if (avc_has_perm(&selinux_state, 55916b6bc620SStephen Smalley peer_sid, if_sid, 5592effad8dfSPaul Moore SECCLASS_NETIF, NETIF__EGRESS, &ad)) 55931f1aaf82SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 5594effad8dfSPaul Moore 5595effad8dfSPaul Moore if (sel_netnode_sid(addrp, family, &node_sid)) 559604f6d70fSEric Paris return NF_DROP; 55976b6bc620SStephen Smalley if (avc_has_perm(&selinux_state, 55986b6bc620SStephen Smalley peer_sid, node_sid, 5599effad8dfSPaul Moore SECCLASS_NODE, NODE__SENDTO, &ad)) 56001f1aaf82SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 5601effad8dfSPaul Moore } 5602effad8dfSPaul Moore 5603effad8dfSPaul Moore return NF_ACCEPT; 5604effad8dfSPaul Moore } 5605effad8dfSPaul Moore 560606198b34SEric W. Biederman static unsigned int selinux_ipv4_postroute(void *priv, 5607a224be76SDavid S. Miller struct sk_buff *skb, 5608238e54c9SDavid S. Miller const struct nf_hook_state *state) 56091da177e4SLinus Torvalds { 5610238e54c9SDavid S. Miller return selinux_ip_postroute(skb, state->out, PF_INET); 56111da177e4SLinus Torvalds } 56121da177e4SLinus Torvalds 56131a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 561406198b34SEric W. Biederman static unsigned int selinux_ipv6_postroute(void *priv, 5615a224be76SDavid S. Miller struct sk_buff *skb, 5616238e54c9SDavid S. Miller const struct nf_hook_state *state) 56171da177e4SLinus Torvalds { 5618238e54c9SDavid S. Miller return selinux_ip_postroute(skb, state->out, PF_INET6); 56191da177e4SLinus Torvalds } 56201da177e4SLinus Torvalds #endif /* IPV6 */ 56211da177e4SLinus Torvalds 56221da177e4SLinus Torvalds #endif /* CONFIG_NETFILTER */ 56231da177e4SLinus Torvalds 56241da177e4SLinus Torvalds static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb) 56251da177e4SLinus Torvalds { 5626941fc5b2SStephen Smalley return selinux_nlmsg_perm(sk, skb); 56271da177e4SLinus Torvalds } 56281da177e4SLinus Torvalds 5629be0554c9SStephen Smalley static int ipc_alloc_security(struct kern_ipc_perm *perm, 56301da177e4SLinus Torvalds u16 sclass) 56311da177e4SLinus Torvalds { 56321da177e4SLinus Torvalds struct ipc_security_struct *isec; 56331da177e4SLinus Torvalds 563489d155efSJames Morris isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL); 56351da177e4SLinus Torvalds if (!isec) 56361da177e4SLinus Torvalds return -ENOMEM; 56371da177e4SLinus Torvalds 56381da177e4SLinus Torvalds isec->sclass = sclass; 5639be0554c9SStephen Smalley isec->sid = current_sid(); 56401da177e4SLinus Torvalds perm->security = isec; 56411da177e4SLinus Torvalds 56421da177e4SLinus Torvalds return 0; 56431da177e4SLinus Torvalds } 56441da177e4SLinus Torvalds 56451da177e4SLinus Torvalds static void ipc_free_security(struct kern_ipc_perm *perm) 56461da177e4SLinus Torvalds { 56471da177e4SLinus Torvalds struct ipc_security_struct *isec = perm->security; 56481da177e4SLinus Torvalds perm->security = NULL; 56491da177e4SLinus Torvalds kfree(isec); 56501da177e4SLinus Torvalds } 56511da177e4SLinus Torvalds 56521da177e4SLinus Torvalds static int msg_msg_alloc_security(struct msg_msg *msg) 56531da177e4SLinus Torvalds { 56541da177e4SLinus Torvalds struct msg_security_struct *msec; 56551da177e4SLinus Torvalds 565689d155efSJames Morris msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL); 56571da177e4SLinus Torvalds if (!msec) 56581da177e4SLinus Torvalds return -ENOMEM; 56591da177e4SLinus Torvalds 56601da177e4SLinus Torvalds msec->sid = SECINITSID_UNLABELED; 56611da177e4SLinus Torvalds msg->security = msec; 56621da177e4SLinus Torvalds 56631da177e4SLinus Torvalds return 0; 56641da177e4SLinus Torvalds } 56651da177e4SLinus Torvalds 56661da177e4SLinus Torvalds static void msg_msg_free_security(struct msg_msg *msg) 56671da177e4SLinus Torvalds { 56681da177e4SLinus Torvalds struct msg_security_struct *msec = msg->security; 56691da177e4SLinus Torvalds 56701da177e4SLinus Torvalds msg->security = NULL; 56711da177e4SLinus Torvalds kfree(msec); 56721da177e4SLinus Torvalds } 56731da177e4SLinus Torvalds 56741da177e4SLinus Torvalds static int ipc_has_perm(struct kern_ipc_perm *ipc_perms, 56756af963f1SStephen Smalley u32 perms) 56761da177e4SLinus Torvalds { 56771da177e4SLinus Torvalds struct ipc_security_struct *isec; 56782bf49690SThomas Liu struct common_audit_data ad; 5679275bb41eSDavid Howells u32 sid = current_sid(); 56801da177e4SLinus Torvalds 5681*7c653828SCasey Schaufler isec = selinux_ipc(ipc_perms); 56821da177e4SLinus Torvalds 568350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 56841da177e4SLinus Torvalds ad.u.ipc_id = ipc_perms->key; 56851da177e4SLinus Torvalds 56866b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 56876b6bc620SStephen Smalley sid, isec->sid, isec->sclass, perms, &ad); 56881da177e4SLinus Torvalds } 56891da177e4SLinus Torvalds 56901da177e4SLinus Torvalds static int selinux_msg_msg_alloc_security(struct msg_msg *msg) 56911da177e4SLinus Torvalds { 56921da177e4SLinus Torvalds return msg_msg_alloc_security(msg); 56931da177e4SLinus Torvalds } 56941da177e4SLinus Torvalds 56951da177e4SLinus Torvalds static void selinux_msg_msg_free_security(struct msg_msg *msg) 56961da177e4SLinus Torvalds { 56971da177e4SLinus Torvalds msg_msg_free_security(msg); 56981da177e4SLinus Torvalds } 56991da177e4SLinus Torvalds 57001da177e4SLinus Torvalds /* message queue security operations */ 5701d8c6e854SEric W. Biederman static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq) 57021da177e4SLinus Torvalds { 57031da177e4SLinus Torvalds struct ipc_security_struct *isec; 57042bf49690SThomas Liu struct common_audit_data ad; 5705275bb41eSDavid Howells u32 sid = current_sid(); 57061da177e4SLinus Torvalds int rc; 57071da177e4SLinus Torvalds 5708d8c6e854SEric W. Biederman rc = ipc_alloc_security(msq, SECCLASS_MSGQ); 57091da177e4SLinus Torvalds if (rc) 57101da177e4SLinus Torvalds return rc; 57111da177e4SLinus Torvalds 5712d8c6e854SEric W. Biederman isec = msq->security; 57131da177e4SLinus Torvalds 571450c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 5715d8c6e854SEric W. Biederman ad.u.ipc_id = msq->key; 57161da177e4SLinus Torvalds 57176b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 57186b6bc620SStephen Smalley sid, isec->sid, SECCLASS_MSGQ, 57191da177e4SLinus Torvalds MSGQ__CREATE, &ad); 57201da177e4SLinus Torvalds if (rc) { 5721d8c6e854SEric W. Biederman ipc_free_security(msq); 57221da177e4SLinus Torvalds return rc; 57231da177e4SLinus Torvalds } 57241da177e4SLinus Torvalds return 0; 57251da177e4SLinus Torvalds } 57261da177e4SLinus Torvalds 5727d8c6e854SEric W. Biederman static void selinux_msg_queue_free_security(struct kern_ipc_perm *msq) 57281da177e4SLinus Torvalds { 5729d8c6e854SEric W. Biederman ipc_free_security(msq); 57301da177e4SLinus Torvalds } 57311da177e4SLinus Torvalds 5732d8c6e854SEric W. Biederman static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg) 57331da177e4SLinus Torvalds { 57341da177e4SLinus Torvalds struct ipc_security_struct *isec; 57352bf49690SThomas Liu struct common_audit_data ad; 5736275bb41eSDavid Howells u32 sid = current_sid(); 57371da177e4SLinus Torvalds 5738*7c653828SCasey Schaufler isec = selinux_ipc(msq); 57391da177e4SLinus Torvalds 574050c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 5741d8c6e854SEric W. Biederman ad.u.ipc_id = msq->key; 57421da177e4SLinus Torvalds 57436b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 57446b6bc620SStephen Smalley sid, isec->sid, SECCLASS_MSGQ, 57451da177e4SLinus Torvalds MSGQ__ASSOCIATE, &ad); 57461da177e4SLinus Torvalds } 57471da177e4SLinus Torvalds 5748d8c6e854SEric W. Biederman static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd) 57491da177e4SLinus Torvalds { 57501da177e4SLinus Torvalds int err; 57511da177e4SLinus Torvalds int perms; 57521da177e4SLinus Torvalds 57531da177e4SLinus Torvalds switch (cmd) { 57541da177e4SLinus Torvalds case IPC_INFO: 57551da177e4SLinus Torvalds case MSG_INFO: 57561da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 57576b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 57586b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, 5759be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); 57601da177e4SLinus Torvalds case IPC_STAT: 57611da177e4SLinus Torvalds case MSG_STAT: 576223c8cec8SDavidlohr Bueso case MSG_STAT_ANY: 57631da177e4SLinus Torvalds perms = MSGQ__GETATTR | MSGQ__ASSOCIATE; 57641da177e4SLinus Torvalds break; 57651da177e4SLinus Torvalds case IPC_SET: 57661da177e4SLinus Torvalds perms = MSGQ__SETATTR; 57671da177e4SLinus Torvalds break; 57681da177e4SLinus Torvalds case IPC_RMID: 57691da177e4SLinus Torvalds perms = MSGQ__DESTROY; 57701da177e4SLinus Torvalds break; 57711da177e4SLinus Torvalds default: 57721da177e4SLinus Torvalds return 0; 57731da177e4SLinus Torvalds } 57741da177e4SLinus Torvalds 5775d8c6e854SEric W. Biederman err = ipc_has_perm(msq, perms); 57761da177e4SLinus Torvalds return err; 57771da177e4SLinus Torvalds } 57781da177e4SLinus Torvalds 5779d8c6e854SEric W. Biederman static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg) 57801da177e4SLinus Torvalds { 57811da177e4SLinus Torvalds struct ipc_security_struct *isec; 57821da177e4SLinus Torvalds struct msg_security_struct *msec; 57832bf49690SThomas Liu struct common_audit_data ad; 5784275bb41eSDavid Howells u32 sid = current_sid(); 57851da177e4SLinus Torvalds int rc; 57861da177e4SLinus Torvalds 5787*7c653828SCasey Schaufler isec = selinux_ipc(msq); 5788*7c653828SCasey Schaufler msec = selinux_msg_msg(msg); 57891da177e4SLinus Torvalds 57901da177e4SLinus Torvalds /* 57911da177e4SLinus Torvalds * First time through, need to assign label to the message 57921da177e4SLinus Torvalds */ 57931da177e4SLinus Torvalds if (msec->sid == SECINITSID_UNLABELED) { 57941da177e4SLinus Torvalds /* 57951da177e4SLinus Torvalds * Compute new sid based on current process and 57961da177e4SLinus Torvalds * message queue this message will be stored in 57971da177e4SLinus Torvalds */ 5798aa8e712cSStephen Smalley rc = security_transition_sid(&selinux_state, sid, isec->sid, 5799aa8e712cSStephen Smalley SECCLASS_MSG, NULL, &msec->sid); 58001da177e4SLinus Torvalds if (rc) 58011da177e4SLinus Torvalds return rc; 58021da177e4SLinus Torvalds } 58031da177e4SLinus Torvalds 580450c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 5805d8c6e854SEric W. Biederman ad.u.ipc_id = msq->key; 58061da177e4SLinus Torvalds 58071da177e4SLinus Torvalds /* Can this process write to the queue? */ 58086b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 58096b6bc620SStephen Smalley sid, isec->sid, SECCLASS_MSGQ, 58101da177e4SLinus Torvalds MSGQ__WRITE, &ad); 58111da177e4SLinus Torvalds if (!rc) 58121da177e4SLinus Torvalds /* Can this process send the message */ 58136b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 58146b6bc620SStephen Smalley sid, msec->sid, SECCLASS_MSG, 5815275bb41eSDavid Howells MSG__SEND, &ad); 58161da177e4SLinus Torvalds if (!rc) 58171da177e4SLinus Torvalds /* Can the message be put in the queue? */ 58186b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 58196b6bc620SStephen Smalley msec->sid, isec->sid, SECCLASS_MSGQ, 5820275bb41eSDavid Howells MSGQ__ENQUEUE, &ad); 58211da177e4SLinus Torvalds 58221da177e4SLinus Torvalds return rc; 58231da177e4SLinus Torvalds } 58241da177e4SLinus Torvalds 5825d8c6e854SEric W. Biederman static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg, 58261da177e4SLinus Torvalds struct task_struct *target, 58271da177e4SLinus Torvalds long type, int mode) 58281da177e4SLinus Torvalds { 58291da177e4SLinus Torvalds struct ipc_security_struct *isec; 58301da177e4SLinus Torvalds struct msg_security_struct *msec; 58312bf49690SThomas Liu struct common_audit_data ad; 5832275bb41eSDavid Howells u32 sid = task_sid(target); 58331da177e4SLinus Torvalds int rc; 58341da177e4SLinus Torvalds 5835*7c653828SCasey Schaufler isec = selinux_ipc(msq); 5836*7c653828SCasey Schaufler msec = selinux_msg_msg(msg); 58371da177e4SLinus Torvalds 583850c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 5839d8c6e854SEric W. Biederman ad.u.ipc_id = msq->key; 58401da177e4SLinus Torvalds 58416b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 58426b6bc620SStephen Smalley sid, isec->sid, 58431da177e4SLinus Torvalds SECCLASS_MSGQ, MSGQ__READ, &ad); 58441da177e4SLinus Torvalds if (!rc) 58456b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 58466b6bc620SStephen Smalley sid, msec->sid, 58471da177e4SLinus Torvalds SECCLASS_MSG, MSG__RECEIVE, &ad); 58481da177e4SLinus Torvalds return rc; 58491da177e4SLinus Torvalds } 58501da177e4SLinus Torvalds 58511da177e4SLinus Torvalds /* Shared Memory security operations */ 58527191adffSEric W. Biederman static int selinux_shm_alloc_security(struct kern_ipc_perm *shp) 58531da177e4SLinus Torvalds { 58541da177e4SLinus Torvalds struct ipc_security_struct *isec; 58552bf49690SThomas Liu struct common_audit_data ad; 5856275bb41eSDavid Howells u32 sid = current_sid(); 58571da177e4SLinus Torvalds int rc; 58581da177e4SLinus Torvalds 58597191adffSEric W. Biederman rc = ipc_alloc_security(shp, SECCLASS_SHM); 58601da177e4SLinus Torvalds if (rc) 58611da177e4SLinus Torvalds return rc; 58621da177e4SLinus Torvalds 58637191adffSEric W. Biederman isec = shp->security; 58641da177e4SLinus Torvalds 586550c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 58667191adffSEric W. Biederman ad.u.ipc_id = shp->key; 58671da177e4SLinus Torvalds 58686b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 58696b6bc620SStephen Smalley sid, isec->sid, SECCLASS_SHM, 58701da177e4SLinus Torvalds SHM__CREATE, &ad); 58711da177e4SLinus Torvalds if (rc) { 58727191adffSEric W. Biederman ipc_free_security(shp); 58731da177e4SLinus Torvalds return rc; 58741da177e4SLinus Torvalds } 58751da177e4SLinus Torvalds return 0; 58761da177e4SLinus Torvalds } 58771da177e4SLinus Torvalds 58787191adffSEric W. Biederman static void selinux_shm_free_security(struct kern_ipc_perm *shp) 58791da177e4SLinus Torvalds { 58807191adffSEric W. Biederman ipc_free_security(shp); 58811da177e4SLinus Torvalds } 58821da177e4SLinus Torvalds 58837191adffSEric W. Biederman static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg) 58841da177e4SLinus Torvalds { 58851da177e4SLinus Torvalds struct ipc_security_struct *isec; 58862bf49690SThomas Liu struct common_audit_data ad; 5887275bb41eSDavid Howells u32 sid = current_sid(); 58881da177e4SLinus Torvalds 5889*7c653828SCasey Schaufler isec = selinux_ipc(shp); 58901da177e4SLinus Torvalds 589150c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 58927191adffSEric W. Biederman ad.u.ipc_id = shp->key; 58931da177e4SLinus Torvalds 58946b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 58956b6bc620SStephen Smalley sid, isec->sid, SECCLASS_SHM, 58961da177e4SLinus Torvalds SHM__ASSOCIATE, &ad); 58971da177e4SLinus Torvalds } 58981da177e4SLinus Torvalds 58991da177e4SLinus Torvalds /* Note, at this point, shp is locked down */ 59007191adffSEric W. Biederman static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd) 59011da177e4SLinus Torvalds { 59021da177e4SLinus Torvalds int perms; 59031da177e4SLinus Torvalds int err; 59041da177e4SLinus Torvalds 59051da177e4SLinus Torvalds switch (cmd) { 59061da177e4SLinus Torvalds case IPC_INFO: 59071da177e4SLinus Torvalds case SHM_INFO: 59081da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 59096b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 59106b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, 5911be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); 59121da177e4SLinus Torvalds case IPC_STAT: 59131da177e4SLinus Torvalds case SHM_STAT: 5914c21a6970SDavidlohr Bueso case SHM_STAT_ANY: 59151da177e4SLinus Torvalds perms = SHM__GETATTR | SHM__ASSOCIATE; 59161da177e4SLinus Torvalds break; 59171da177e4SLinus Torvalds case IPC_SET: 59181da177e4SLinus Torvalds perms = SHM__SETATTR; 59191da177e4SLinus Torvalds break; 59201da177e4SLinus Torvalds case SHM_LOCK: 59211da177e4SLinus Torvalds case SHM_UNLOCK: 59221da177e4SLinus Torvalds perms = SHM__LOCK; 59231da177e4SLinus Torvalds break; 59241da177e4SLinus Torvalds case IPC_RMID: 59251da177e4SLinus Torvalds perms = SHM__DESTROY; 59261da177e4SLinus Torvalds break; 59271da177e4SLinus Torvalds default: 59281da177e4SLinus Torvalds return 0; 59291da177e4SLinus Torvalds } 59301da177e4SLinus Torvalds 59317191adffSEric W. Biederman err = ipc_has_perm(shp, perms); 59321da177e4SLinus Torvalds return err; 59331da177e4SLinus Torvalds } 59341da177e4SLinus Torvalds 59357191adffSEric W. Biederman static int selinux_shm_shmat(struct kern_ipc_perm *shp, 59361da177e4SLinus Torvalds char __user *shmaddr, int shmflg) 59371da177e4SLinus Torvalds { 59381da177e4SLinus Torvalds u32 perms; 59391da177e4SLinus Torvalds 59401da177e4SLinus Torvalds if (shmflg & SHM_RDONLY) 59411da177e4SLinus Torvalds perms = SHM__READ; 59421da177e4SLinus Torvalds else 59431da177e4SLinus Torvalds perms = SHM__READ | SHM__WRITE; 59441da177e4SLinus Torvalds 59457191adffSEric W. Biederman return ipc_has_perm(shp, perms); 59461da177e4SLinus Torvalds } 59471da177e4SLinus Torvalds 59481da177e4SLinus Torvalds /* Semaphore security operations */ 5949aefad959SEric W. Biederman static int selinux_sem_alloc_security(struct kern_ipc_perm *sma) 59501da177e4SLinus Torvalds { 59511da177e4SLinus Torvalds struct ipc_security_struct *isec; 59522bf49690SThomas Liu struct common_audit_data ad; 5953275bb41eSDavid Howells u32 sid = current_sid(); 59541da177e4SLinus Torvalds int rc; 59551da177e4SLinus Torvalds 5956aefad959SEric W. Biederman rc = ipc_alloc_security(sma, SECCLASS_SEM); 59571da177e4SLinus Torvalds if (rc) 59581da177e4SLinus Torvalds return rc; 59591da177e4SLinus Torvalds 5960aefad959SEric W. Biederman isec = sma->security; 59611da177e4SLinus Torvalds 596250c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 5963aefad959SEric W. Biederman ad.u.ipc_id = sma->key; 59641da177e4SLinus Torvalds 59656b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 59666b6bc620SStephen Smalley sid, isec->sid, SECCLASS_SEM, 59671da177e4SLinus Torvalds SEM__CREATE, &ad); 59681da177e4SLinus Torvalds if (rc) { 5969aefad959SEric W. Biederman ipc_free_security(sma); 59701da177e4SLinus Torvalds return rc; 59711da177e4SLinus Torvalds } 59721da177e4SLinus Torvalds return 0; 59731da177e4SLinus Torvalds } 59741da177e4SLinus Torvalds 5975aefad959SEric W. Biederman static void selinux_sem_free_security(struct kern_ipc_perm *sma) 59761da177e4SLinus Torvalds { 5977aefad959SEric W. Biederman ipc_free_security(sma); 59781da177e4SLinus Torvalds } 59791da177e4SLinus Torvalds 5980aefad959SEric W. Biederman static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg) 59811da177e4SLinus Torvalds { 59821da177e4SLinus Torvalds struct ipc_security_struct *isec; 59832bf49690SThomas Liu struct common_audit_data ad; 5984275bb41eSDavid Howells u32 sid = current_sid(); 59851da177e4SLinus Torvalds 5986*7c653828SCasey Schaufler isec = selinux_ipc(sma); 59871da177e4SLinus Torvalds 598850c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 5989aefad959SEric W. Biederman ad.u.ipc_id = sma->key; 59901da177e4SLinus Torvalds 59916b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 59926b6bc620SStephen Smalley sid, isec->sid, SECCLASS_SEM, 59931da177e4SLinus Torvalds SEM__ASSOCIATE, &ad); 59941da177e4SLinus Torvalds } 59951da177e4SLinus Torvalds 59961da177e4SLinus Torvalds /* Note, at this point, sma is locked down */ 5997aefad959SEric W. Biederman static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd) 59981da177e4SLinus Torvalds { 59991da177e4SLinus Torvalds int err; 60001da177e4SLinus Torvalds u32 perms; 60011da177e4SLinus Torvalds 60021da177e4SLinus Torvalds switch (cmd) { 60031da177e4SLinus Torvalds case IPC_INFO: 60041da177e4SLinus Torvalds case SEM_INFO: 60051da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 60066b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 60076b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, 6008be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); 60091da177e4SLinus Torvalds case GETPID: 60101da177e4SLinus Torvalds case GETNCNT: 60111da177e4SLinus Torvalds case GETZCNT: 60121da177e4SLinus Torvalds perms = SEM__GETATTR; 60131da177e4SLinus Torvalds break; 60141da177e4SLinus Torvalds case GETVAL: 60151da177e4SLinus Torvalds case GETALL: 60161da177e4SLinus Torvalds perms = SEM__READ; 60171da177e4SLinus Torvalds break; 60181da177e4SLinus Torvalds case SETVAL: 60191da177e4SLinus Torvalds case SETALL: 60201da177e4SLinus Torvalds perms = SEM__WRITE; 60211da177e4SLinus Torvalds break; 60221da177e4SLinus Torvalds case IPC_RMID: 60231da177e4SLinus Torvalds perms = SEM__DESTROY; 60241da177e4SLinus Torvalds break; 60251da177e4SLinus Torvalds case IPC_SET: 60261da177e4SLinus Torvalds perms = SEM__SETATTR; 60271da177e4SLinus Torvalds break; 60281da177e4SLinus Torvalds case IPC_STAT: 60291da177e4SLinus Torvalds case SEM_STAT: 6030a280d6dcSDavidlohr Bueso case SEM_STAT_ANY: 60311da177e4SLinus Torvalds perms = SEM__GETATTR | SEM__ASSOCIATE; 60321da177e4SLinus Torvalds break; 60331da177e4SLinus Torvalds default: 60341da177e4SLinus Torvalds return 0; 60351da177e4SLinus Torvalds } 60361da177e4SLinus Torvalds 6037aefad959SEric W. Biederman err = ipc_has_perm(sma, perms); 60381da177e4SLinus Torvalds return err; 60391da177e4SLinus Torvalds } 60401da177e4SLinus Torvalds 6041aefad959SEric W. Biederman static int selinux_sem_semop(struct kern_ipc_perm *sma, 60421da177e4SLinus Torvalds struct sembuf *sops, unsigned nsops, int alter) 60431da177e4SLinus Torvalds { 60441da177e4SLinus Torvalds u32 perms; 60451da177e4SLinus Torvalds 60461da177e4SLinus Torvalds if (alter) 60471da177e4SLinus Torvalds perms = SEM__READ | SEM__WRITE; 60481da177e4SLinus Torvalds else 60491da177e4SLinus Torvalds perms = SEM__READ; 60501da177e4SLinus Torvalds 6051aefad959SEric W. Biederman return ipc_has_perm(sma, perms); 60521da177e4SLinus Torvalds } 60531da177e4SLinus Torvalds 60541da177e4SLinus Torvalds static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag) 60551da177e4SLinus Torvalds { 60561da177e4SLinus Torvalds u32 av = 0; 60571da177e4SLinus Torvalds 60581da177e4SLinus Torvalds av = 0; 60591da177e4SLinus Torvalds if (flag & S_IRUGO) 60601da177e4SLinus Torvalds av |= IPC__UNIX_READ; 60611da177e4SLinus Torvalds if (flag & S_IWUGO) 60621da177e4SLinus Torvalds av |= IPC__UNIX_WRITE; 60631da177e4SLinus Torvalds 60641da177e4SLinus Torvalds if (av == 0) 60651da177e4SLinus Torvalds return 0; 60661da177e4SLinus Torvalds 60676af963f1SStephen Smalley return ipc_has_perm(ipcp, av); 60681da177e4SLinus Torvalds } 60691da177e4SLinus Torvalds 6070713a04aeSAhmed S. Darwish static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid) 6071713a04aeSAhmed S. Darwish { 6072*7c653828SCasey Schaufler struct ipc_security_struct *isec = selinux_ipc(ipcp); 6073713a04aeSAhmed S. Darwish *secid = isec->sid; 6074713a04aeSAhmed S. Darwish } 6075713a04aeSAhmed S. Darwish 60761da177e4SLinus Torvalds static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode) 60771da177e4SLinus Torvalds { 60781da177e4SLinus Torvalds if (inode) 60791da177e4SLinus Torvalds inode_doinit_with_dentry(inode, dentry); 60801da177e4SLinus Torvalds } 60811da177e4SLinus Torvalds 60821da177e4SLinus Torvalds static int selinux_getprocattr(struct task_struct *p, 608304ff9708SAl Viro char *name, char **value) 60841da177e4SLinus Torvalds { 6085275bb41eSDavid Howells const struct task_security_struct *__tsec; 60868c8570fbSDustin Kirkland u32 sid; 60871da177e4SLinus Torvalds int error; 608804ff9708SAl Viro unsigned len; 60891da177e4SLinus Torvalds 6090275bb41eSDavid Howells rcu_read_lock(); 60910c6cfa62SCasey Schaufler __tsec = selinux_cred(__task_cred(p)); 60921da177e4SLinus Torvalds 6093be0554c9SStephen Smalley if (current != p) { 60946b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 60956b6bc620SStephen Smalley current_sid(), __tsec->sid, 6096be0554c9SStephen Smalley SECCLASS_PROCESS, PROCESS__GETATTR, NULL); 6097be0554c9SStephen Smalley if (error) 6098be0554c9SStephen Smalley goto bad; 6099be0554c9SStephen Smalley } 6100be0554c9SStephen Smalley 61011da177e4SLinus Torvalds if (!strcmp(name, "current")) 6102275bb41eSDavid Howells sid = __tsec->sid; 61031da177e4SLinus Torvalds else if (!strcmp(name, "prev")) 6104275bb41eSDavid Howells sid = __tsec->osid; 61051da177e4SLinus Torvalds else if (!strcmp(name, "exec")) 6106275bb41eSDavid Howells sid = __tsec->exec_sid; 61071da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 6108275bb41eSDavid Howells sid = __tsec->create_sid; 61094eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) 6110275bb41eSDavid Howells sid = __tsec->keycreate_sid; 611142c3e03eSEric Paris else if (!strcmp(name, "sockcreate")) 6112275bb41eSDavid Howells sid = __tsec->sockcreate_sid; 6113be0554c9SStephen Smalley else { 6114be0554c9SStephen Smalley error = -EINVAL; 6115be0554c9SStephen Smalley goto bad; 6116be0554c9SStephen Smalley } 6117275bb41eSDavid Howells rcu_read_unlock(); 61181da177e4SLinus Torvalds 61191da177e4SLinus Torvalds if (!sid) 61201da177e4SLinus Torvalds return 0; 61211da177e4SLinus Torvalds 6122aa8e712cSStephen Smalley error = security_sid_to_context(&selinux_state, sid, value, &len); 612304ff9708SAl Viro if (error) 612404ff9708SAl Viro return error; 612504ff9708SAl Viro return len; 6126275bb41eSDavid Howells 6127be0554c9SStephen Smalley bad: 6128275bb41eSDavid Howells rcu_read_unlock(); 6129be0554c9SStephen Smalley return error; 61301da177e4SLinus Torvalds } 61311da177e4SLinus Torvalds 6132b21507e2SStephen Smalley static int selinux_setprocattr(const char *name, void *value, size_t size) 61331da177e4SLinus Torvalds { 61341da177e4SLinus Torvalds struct task_security_struct *tsec; 6135d84f4f99SDavid Howells struct cred *new; 6136be0554c9SStephen Smalley u32 mysid = current_sid(), sid = 0, ptsid; 61371da177e4SLinus Torvalds int error; 61381da177e4SLinus Torvalds char *str = value; 61391da177e4SLinus Torvalds 61401da177e4SLinus Torvalds /* 61411da177e4SLinus Torvalds * Basic control over ability to set these attributes at all. 61421da177e4SLinus Torvalds */ 61431da177e4SLinus Torvalds if (!strcmp(name, "exec")) 61446b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 61456b6bc620SStephen Smalley mysid, mysid, SECCLASS_PROCESS, 6146be0554c9SStephen Smalley PROCESS__SETEXEC, NULL); 61471da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 61486b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 61496b6bc620SStephen Smalley mysid, mysid, SECCLASS_PROCESS, 6150be0554c9SStephen Smalley PROCESS__SETFSCREATE, NULL); 61514eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) 61526b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 61536b6bc620SStephen Smalley mysid, mysid, SECCLASS_PROCESS, 6154be0554c9SStephen Smalley PROCESS__SETKEYCREATE, NULL); 615542c3e03eSEric Paris else if (!strcmp(name, "sockcreate")) 61566b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 61576b6bc620SStephen Smalley mysid, mysid, SECCLASS_PROCESS, 6158be0554c9SStephen Smalley PROCESS__SETSOCKCREATE, NULL); 61591da177e4SLinus Torvalds else if (!strcmp(name, "current")) 61606b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 61616b6bc620SStephen Smalley mysid, mysid, SECCLASS_PROCESS, 6162be0554c9SStephen Smalley PROCESS__SETCURRENT, NULL); 61631da177e4SLinus Torvalds else 61641da177e4SLinus Torvalds error = -EINVAL; 61651da177e4SLinus Torvalds if (error) 61661da177e4SLinus Torvalds return error; 61671da177e4SLinus Torvalds 61681da177e4SLinus Torvalds /* Obtain a SID for the context, if one was specified. */ 6169a050a570SStephen Smalley if (size && str[0] && str[0] != '\n') { 61701da177e4SLinus Torvalds if (str[size-1] == '\n') { 61711da177e4SLinus Torvalds str[size-1] = 0; 61721da177e4SLinus Torvalds size--; 61731da177e4SLinus Torvalds } 6174aa8e712cSStephen Smalley error = security_context_to_sid(&selinux_state, value, size, 6175aa8e712cSStephen Smalley &sid, GFP_KERNEL); 617612b29f34SStephen Smalley if (error == -EINVAL && !strcmp(name, "fscreate")) { 6177db59000aSStephen Smalley if (!has_cap_mac_admin(true)) { 6178d6ea83ecSEric Paris struct audit_buffer *ab; 6179d6ea83ecSEric Paris size_t audit_size; 6180d6ea83ecSEric Paris 6181d6ea83ecSEric Paris /* We strip a nul only if it is at the end, otherwise the 6182d6ea83ecSEric Paris * context contains a nul and we should audit that */ 6183d6ea83ecSEric Paris if (str[size - 1] == '\0') 6184d6ea83ecSEric Paris audit_size = size - 1; 6185d6ea83ecSEric Paris else 6186d6ea83ecSEric Paris audit_size = size; 6187cdfb6b34SRichard Guy Briggs ab = audit_log_start(audit_context(), 6188cdfb6b34SRichard Guy Briggs GFP_ATOMIC, 6189cdfb6b34SRichard Guy Briggs AUDIT_SELINUX_ERR); 6190d6ea83ecSEric Paris audit_log_format(ab, "op=fscreate invalid_context="); 6191d6ea83ecSEric Paris audit_log_n_untrustedstring(ab, value, audit_size); 6192d6ea83ecSEric Paris audit_log_end(ab); 6193d6ea83ecSEric Paris 619412b29f34SStephen Smalley return error; 6195d6ea83ecSEric Paris } 6196aa8e712cSStephen Smalley error = security_context_to_sid_force( 6197aa8e712cSStephen Smalley &selinux_state, 6198aa8e712cSStephen Smalley value, size, &sid); 619912b29f34SStephen Smalley } 62001da177e4SLinus Torvalds if (error) 62011da177e4SLinus Torvalds return error; 62021da177e4SLinus Torvalds } 62031da177e4SLinus Torvalds 6204d84f4f99SDavid Howells new = prepare_creds(); 6205d84f4f99SDavid Howells if (!new) 6206d84f4f99SDavid Howells return -ENOMEM; 6207d84f4f99SDavid Howells 62081da177e4SLinus Torvalds /* Permission checking based on the specified context is 62091da177e4SLinus Torvalds performed during the actual operation (execve, 62101da177e4SLinus Torvalds open/mkdir/...), when we know the full context of the 6211d84f4f99SDavid Howells operation. See selinux_bprm_set_creds for the execve 62121da177e4SLinus Torvalds checks and may_create for the file creation checks. The 62131da177e4SLinus Torvalds operation will then fail if the context is not permitted. */ 62140c6cfa62SCasey Schaufler tsec = selinux_cred(new); 6215d84f4f99SDavid Howells if (!strcmp(name, "exec")) { 62161da177e4SLinus Torvalds tsec->exec_sid = sid; 6217d84f4f99SDavid Howells } else if (!strcmp(name, "fscreate")) { 62181da177e4SLinus Torvalds tsec->create_sid = sid; 6219d84f4f99SDavid Howells } else if (!strcmp(name, "keycreate")) { 62206b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 62216b6bc620SStephen Smalley mysid, sid, SECCLASS_KEY, KEY__CREATE, 6222be0554c9SStephen Smalley NULL); 62234eb582cfSMichael LeMay if (error) 6224d84f4f99SDavid Howells goto abort_change; 62254eb582cfSMichael LeMay tsec->keycreate_sid = sid; 6226d84f4f99SDavid Howells } else if (!strcmp(name, "sockcreate")) { 622742c3e03eSEric Paris tsec->sockcreate_sid = sid; 6228d84f4f99SDavid Howells } else if (!strcmp(name, "current")) { 6229d84f4f99SDavid Howells error = -EINVAL; 62301da177e4SLinus Torvalds if (sid == 0) 6231d84f4f99SDavid Howells goto abort_change; 6232d9250deaSKaiGai Kohei 6233d84f4f99SDavid Howells /* Only allow single threaded processes to change context */ 6234d84f4f99SDavid Howells error = -EPERM; 62355bb459bbSOleg Nesterov if (!current_is_single_threaded()) { 6236aa8e712cSStephen Smalley error = security_bounded_transition(&selinux_state, 6237aa8e712cSStephen Smalley tsec->sid, sid); 6238d84f4f99SDavid Howells if (error) 6239d84f4f99SDavid Howells goto abort_change; 62401da177e4SLinus Torvalds } 62411da177e4SLinus Torvalds 62421da177e4SLinus Torvalds /* Check permissions for the transition. */ 62436b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 62446b6bc620SStephen Smalley tsec->sid, sid, SECCLASS_PROCESS, 62451da177e4SLinus Torvalds PROCESS__DYNTRANSITION, NULL); 62461da177e4SLinus Torvalds if (error) 6247d84f4f99SDavid Howells goto abort_change; 62481da177e4SLinus Torvalds 62491da177e4SLinus Torvalds /* Check for ptracing, and update the task SID if ok. 62501da177e4SLinus Torvalds Otherwise, leave SID unchanged and fail. */ 6251be0554c9SStephen Smalley ptsid = ptrace_parent_sid(); 62520c6181cbSPaul Moore if (ptsid != 0) { 62536b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 62546b6bc620SStephen Smalley ptsid, sid, SECCLASS_PROCESS, 6255d84f4f99SDavid Howells PROCESS__PTRACE, NULL); 6256d84f4f99SDavid Howells if (error) 6257d84f4f99SDavid Howells goto abort_change; 6258d84f4f99SDavid Howells } 6259d84f4f99SDavid Howells 6260d84f4f99SDavid Howells tsec->sid = sid; 6261d84f4f99SDavid Howells } else { 6262d84f4f99SDavid Howells error = -EINVAL; 6263d84f4f99SDavid Howells goto abort_change; 6264d84f4f99SDavid Howells } 6265d84f4f99SDavid Howells 6266d84f4f99SDavid Howells commit_creds(new); 62671da177e4SLinus Torvalds return size; 6268d84f4f99SDavid Howells 6269d84f4f99SDavid Howells abort_change: 6270d84f4f99SDavid Howells abort_creds(new); 6271d84f4f99SDavid Howells return error; 62721da177e4SLinus Torvalds } 62731da177e4SLinus Torvalds 6274746df9b5SDavid Quigley static int selinux_ismaclabel(const char *name) 6275746df9b5SDavid Quigley { 6276746df9b5SDavid Quigley return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0); 6277746df9b5SDavid Quigley } 6278746df9b5SDavid Quigley 6279dc49c1f9SCatherine Zhang static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) 6280dc49c1f9SCatherine Zhang { 6281aa8e712cSStephen Smalley return security_sid_to_context(&selinux_state, secid, 6282aa8e712cSStephen Smalley secdata, seclen); 6283dc49c1f9SCatherine Zhang } 6284dc49c1f9SCatherine Zhang 62857bf570dcSDavid Howells static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid) 628663cb3449SDavid Howells { 6287aa8e712cSStephen Smalley return security_context_to_sid(&selinux_state, secdata, seclen, 6288aa8e712cSStephen Smalley secid, GFP_KERNEL); 628963cb3449SDavid Howells } 629063cb3449SDavid Howells 6291dc49c1f9SCatherine Zhang static void selinux_release_secctx(char *secdata, u32 seclen) 6292dc49c1f9SCatherine Zhang { 6293dc49c1f9SCatherine Zhang kfree(secdata); 6294dc49c1f9SCatherine Zhang } 6295dc49c1f9SCatherine Zhang 62966f3be9f5SAndreas Gruenbacher static void selinux_inode_invalidate_secctx(struct inode *inode) 62976f3be9f5SAndreas Gruenbacher { 629880788c22SCasey Schaufler struct inode_security_struct *isec = selinux_inode(inode); 62996f3be9f5SAndreas Gruenbacher 63009287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 63016f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INVALID; 63029287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 63036f3be9f5SAndreas Gruenbacher } 63046f3be9f5SAndreas Gruenbacher 63051ee65e37SDavid P. Quigley /* 63061ee65e37SDavid P. Quigley * called with inode->i_mutex locked 63071ee65e37SDavid P. Quigley */ 63081ee65e37SDavid P. Quigley static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen) 63091ee65e37SDavid P. Quigley { 63101ee65e37SDavid P. Quigley return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0); 63111ee65e37SDavid P. Quigley } 63121ee65e37SDavid P. Quigley 63131ee65e37SDavid P. Quigley /* 63141ee65e37SDavid P. Quigley * called with inode->i_mutex locked 63151ee65e37SDavid P. Quigley */ 63161ee65e37SDavid P. Quigley static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen) 63171ee65e37SDavid P. Quigley { 63181ee65e37SDavid P. Quigley return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0); 63191ee65e37SDavid P. Quigley } 63201ee65e37SDavid P. Quigley 63211ee65e37SDavid P. Quigley static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen) 63221ee65e37SDavid P. Quigley { 63231ee65e37SDavid P. Quigley int len = 0; 63241ee65e37SDavid P. Quigley len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX, 63251ee65e37SDavid P. Quigley ctx, true); 63261ee65e37SDavid P. Quigley if (len < 0) 63271ee65e37SDavid P. Quigley return len; 63281ee65e37SDavid P. Quigley *ctxlen = len; 63291ee65e37SDavid P. Quigley return 0; 63301ee65e37SDavid P. Quigley } 6331d720024eSMichael LeMay #ifdef CONFIG_KEYS 6332d720024eSMichael LeMay 6333d84f4f99SDavid Howells static int selinux_key_alloc(struct key *k, const struct cred *cred, 63347e047ef5SDavid Howells unsigned long flags) 6335d720024eSMichael LeMay { 6336d84f4f99SDavid Howells const struct task_security_struct *tsec; 6337d720024eSMichael LeMay struct key_security_struct *ksec; 6338d720024eSMichael LeMay 6339d720024eSMichael LeMay ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL); 6340d720024eSMichael LeMay if (!ksec) 6341d720024eSMichael LeMay return -ENOMEM; 6342d720024eSMichael LeMay 63430c6cfa62SCasey Schaufler tsec = selinux_cred(cred); 6344d84f4f99SDavid Howells if (tsec->keycreate_sid) 6345d84f4f99SDavid Howells ksec->sid = tsec->keycreate_sid; 63464eb582cfSMichael LeMay else 6347d84f4f99SDavid Howells ksec->sid = tsec->sid; 6348d720024eSMichael LeMay 6349275bb41eSDavid Howells k->security = ksec; 6350d720024eSMichael LeMay return 0; 6351d720024eSMichael LeMay } 6352d720024eSMichael LeMay 6353d720024eSMichael LeMay static void selinux_key_free(struct key *k) 6354d720024eSMichael LeMay { 6355d720024eSMichael LeMay struct key_security_struct *ksec = k->security; 6356d720024eSMichael LeMay 6357d720024eSMichael LeMay k->security = NULL; 6358d720024eSMichael LeMay kfree(ksec); 6359d720024eSMichael LeMay } 6360d720024eSMichael LeMay 6361d720024eSMichael LeMay static int selinux_key_permission(key_ref_t key_ref, 6362d84f4f99SDavid Howells const struct cred *cred, 6363f5895943SDavid Howells unsigned perm) 6364d720024eSMichael LeMay { 6365d720024eSMichael LeMay struct key *key; 6366d720024eSMichael LeMay struct key_security_struct *ksec; 6367275bb41eSDavid Howells u32 sid; 6368d720024eSMichael LeMay 6369d720024eSMichael LeMay /* if no specific permissions are requested, we skip the 6370d720024eSMichael LeMay permission check. No serious, additional covert channels 6371d720024eSMichael LeMay appear to be created. */ 6372d720024eSMichael LeMay if (perm == 0) 6373d720024eSMichael LeMay return 0; 6374d720024eSMichael LeMay 6375d84f4f99SDavid Howells sid = cred_sid(cred); 6376275bb41eSDavid Howells 6377275bb41eSDavid Howells key = key_ref_to_ptr(key_ref); 6378275bb41eSDavid Howells ksec = key->security; 6379275bb41eSDavid Howells 63806b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 63816b6bc620SStephen Smalley sid, ksec->sid, SECCLASS_KEY, perm, NULL); 6382d720024eSMichael LeMay } 6383d720024eSMichael LeMay 638470a5bb72SDavid Howells static int selinux_key_getsecurity(struct key *key, char **_buffer) 638570a5bb72SDavid Howells { 638670a5bb72SDavid Howells struct key_security_struct *ksec = key->security; 638770a5bb72SDavid Howells char *context = NULL; 638870a5bb72SDavid Howells unsigned len; 638970a5bb72SDavid Howells int rc; 639070a5bb72SDavid Howells 6391aa8e712cSStephen Smalley rc = security_sid_to_context(&selinux_state, ksec->sid, 6392aa8e712cSStephen Smalley &context, &len); 639370a5bb72SDavid Howells if (!rc) 639470a5bb72SDavid Howells rc = len; 639570a5bb72SDavid Howells *_buffer = context; 639670a5bb72SDavid Howells return rc; 639770a5bb72SDavid Howells } 63983a976fa6SDaniel Jurgens #endif 639970a5bb72SDavid Howells 64003a976fa6SDaniel Jurgens #ifdef CONFIG_SECURITY_INFINIBAND 6401cfc4d882SDaniel Jurgens static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val) 6402cfc4d882SDaniel Jurgens { 6403cfc4d882SDaniel Jurgens struct common_audit_data ad; 6404cfc4d882SDaniel Jurgens int err; 6405cfc4d882SDaniel Jurgens u32 sid = 0; 6406cfc4d882SDaniel Jurgens struct ib_security_struct *sec = ib_sec; 6407cfc4d882SDaniel Jurgens struct lsm_ibpkey_audit ibpkey; 6408cfc4d882SDaniel Jurgens 6409409dcf31SDaniel Jurgens err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid); 6410cfc4d882SDaniel Jurgens if (err) 6411cfc4d882SDaniel Jurgens return err; 6412cfc4d882SDaniel Jurgens 6413cfc4d882SDaniel Jurgens ad.type = LSM_AUDIT_DATA_IBPKEY; 6414cfc4d882SDaniel Jurgens ibpkey.subnet_prefix = subnet_prefix; 6415cfc4d882SDaniel Jurgens ibpkey.pkey = pkey_val; 6416cfc4d882SDaniel Jurgens ad.u.ibpkey = &ibpkey; 64176b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 64186b6bc620SStephen Smalley sec->sid, sid, 6419cfc4d882SDaniel Jurgens SECCLASS_INFINIBAND_PKEY, 6420cfc4d882SDaniel Jurgens INFINIBAND_PKEY__ACCESS, &ad); 6421cfc4d882SDaniel Jurgens } 6422cfc4d882SDaniel Jurgens 6423ab861dfcSDaniel Jurgens static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name, 6424ab861dfcSDaniel Jurgens u8 port_num) 6425ab861dfcSDaniel Jurgens { 6426ab861dfcSDaniel Jurgens struct common_audit_data ad; 6427ab861dfcSDaniel Jurgens int err; 6428ab861dfcSDaniel Jurgens u32 sid = 0; 6429ab861dfcSDaniel Jurgens struct ib_security_struct *sec = ib_sec; 6430ab861dfcSDaniel Jurgens struct lsm_ibendport_audit ibendport; 6431ab861dfcSDaniel Jurgens 6432aa8e712cSStephen Smalley err = security_ib_endport_sid(&selinux_state, dev_name, port_num, 6433aa8e712cSStephen Smalley &sid); 6434ab861dfcSDaniel Jurgens 6435ab861dfcSDaniel Jurgens if (err) 6436ab861dfcSDaniel Jurgens return err; 6437ab861dfcSDaniel Jurgens 6438ab861dfcSDaniel Jurgens ad.type = LSM_AUDIT_DATA_IBENDPORT; 6439ab861dfcSDaniel Jurgens strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name)); 6440ab861dfcSDaniel Jurgens ibendport.port = port_num; 6441ab861dfcSDaniel Jurgens ad.u.ibendport = &ibendport; 64426b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 64436b6bc620SStephen Smalley sec->sid, sid, 6444ab861dfcSDaniel Jurgens SECCLASS_INFINIBAND_ENDPORT, 6445ab861dfcSDaniel Jurgens INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad); 6446ab861dfcSDaniel Jurgens } 6447ab861dfcSDaniel Jurgens 64483a976fa6SDaniel Jurgens static int selinux_ib_alloc_security(void **ib_sec) 64493a976fa6SDaniel Jurgens { 64503a976fa6SDaniel Jurgens struct ib_security_struct *sec; 64513a976fa6SDaniel Jurgens 64523a976fa6SDaniel Jurgens sec = kzalloc(sizeof(*sec), GFP_KERNEL); 64533a976fa6SDaniel Jurgens if (!sec) 64543a976fa6SDaniel Jurgens return -ENOMEM; 64553a976fa6SDaniel Jurgens sec->sid = current_sid(); 64563a976fa6SDaniel Jurgens 64573a976fa6SDaniel Jurgens *ib_sec = sec; 64583a976fa6SDaniel Jurgens return 0; 64593a976fa6SDaniel Jurgens } 64603a976fa6SDaniel Jurgens 64613a976fa6SDaniel Jurgens static void selinux_ib_free_security(void *ib_sec) 64623a976fa6SDaniel Jurgens { 64633a976fa6SDaniel Jurgens kfree(ib_sec); 64643a976fa6SDaniel Jurgens } 6465d720024eSMichael LeMay #endif 6466d720024eSMichael LeMay 6467ec27c356SChenbo Feng #ifdef CONFIG_BPF_SYSCALL 6468ec27c356SChenbo Feng static int selinux_bpf(int cmd, union bpf_attr *attr, 6469ec27c356SChenbo Feng unsigned int size) 6470ec27c356SChenbo Feng { 6471ec27c356SChenbo Feng u32 sid = current_sid(); 6472ec27c356SChenbo Feng int ret; 6473ec27c356SChenbo Feng 6474ec27c356SChenbo Feng switch (cmd) { 6475ec27c356SChenbo Feng case BPF_MAP_CREATE: 64766b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 64776b6bc620SStephen Smalley sid, sid, SECCLASS_BPF, BPF__MAP_CREATE, 6478ec27c356SChenbo Feng NULL); 6479ec27c356SChenbo Feng break; 6480ec27c356SChenbo Feng case BPF_PROG_LOAD: 64816b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 64826b6bc620SStephen Smalley sid, sid, SECCLASS_BPF, BPF__PROG_LOAD, 6483ec27c356SChenbo Feng NULL); 6484ec27c356SChenbo Feng break; 6485ec27c356SChenbo Feng default: 6486ec27c356SChenbo Feng ret = 0; 6487ec27c356SChenbo Feng break; 6488ec27c356SChenbo Feng } 6489ec27c356SChenbo Feng 6490ec27c356SChenbo Feng return ret; 6491ec27c356SChenbo Feng } 6492ec27c356SChenbo Feng 6493ec27c356SChenbo Feng static u32 bpf_map_fmode_to_av(fmode_t fmode) 6494ec27c356SChenbo Feng { 6495ec27c356SChenbo Feng u32 av = 0; 6496ec27c356SChenbo Feng 6497ec27c356SChenbo Feng if (fmode & FMODE_READ) 6498ec27c356SChenbo Feng av |= BPF__MAP_READ; 6499ec27c356SChenbo Feng if (fmode & FMODE_WRITE) 6500ec27c356SChenbo Feng av |= BPF__MAP_WRITE; 6501ec27c356SChenbo Feng return av; 6502ec27c356SChenbo Feng } 6503ec27c356SChenbo Feng 6504f66e448cSChenbo Feng /* This function will check the file pass through unix socket or binder to see 6505f66e448cSChenbo Feng * if it is a bpf related object. And apply correspinding checks on the bpf 6506f66e448cSChenbo Feng * object based on the type. The bpf maps and programs, not like other files and 6507f66e448cSChenbo Feng * socket, are using a shared anonymous inode inside the kernel as their inode. 6508f66e448cSChenbo Feng * So checking that inode cannot identify if the process have privilege to 6509f66e448cSChenbo Feng * access the bpf object and that's why we have to add this additional check in 6510f66e448cSChenbo Feng * selinux_file_receive and selinux_binder_transfer_files. 6511f66e448cSChenbo Feng */ 6512f66e448cSChenbo Feng static int bpf_fd_pass(struct file *file, u32 sid) 6513f66e448cSChenbo Feng { 6514f66e448cSChenbo Feng struct bpf_security_struct *bpfsec; 6515f66e448cSChenbo Feng struct bpf_prog *prog; 6516f66e448cSChenbo Feng struct bpf_map *map; 6517f66e448cSChenbo Feng int ret; 6518f66e448cSChenbo Feng 6519f66e448cSChenbo Feng if (file->f_op == &bpf_map_fops) { 6520f66e448cSChenbo Feng map = file->private_data; 6521f66e448cSChenbo Feng bpfsec = map->security; 65226b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 65236b6bc620SStephen Smalley sid, bpfsec->sid, SECCLASS_BPF, 6524f66e448cSChenbo Feng bpf_map_fmode_to_av(file->f_mode), NULL); 6525f66e448cSChenbo Feng if (ret) 6526f66e448cSChenbo Feng return ret; 6527f66e448cSChenbo Feng } else if (file->f_op == &bpf_prog_fops) { 6528f66e448cSChenbo Feng prog = file->private_data; 6529f66e448cSChenbo Feng bpfsec = prog->aux->security; 65306b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 65316b6bc620SStephen Smalley sid, bpfsec->sid, SECCLASS_BPF, 6532f66e448cSChenbo Feng BPF__PROG_RUN, NULL); 6533f66e448cSChenbo Feng if (ret) 6534f66e448cSChenbo Feng return ret; 6535f66e448cSChenbo Feng } 6536f66e448cSChenbo Feng return 0; 6537f66e448cSChenbo Feng } 6538f66e448cSChenbo Feng 6539ec27c356SChenbo Feng static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode) 6540ec27c356SChenbo Feng { 6541ec27c356SChenbo Feng u32 sid = current_sid(); 6542ec27c356SChenbo Feng struct bpf_security_struct *bpfsec; 6543ec27c356SChenbo Feng 6544ec27c356SChenbo Feng bpfsec = map->security; 65456b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 65466b6bc620SStephen Smalley sid, bpfsec->sid, SECCLASS_BPF, 6547ec27c356SChenbo Feng bpf_map_fmode_to_av(fmode), NULL); 6548ec27c356SChenbo Feng } 6549ec27c356SChenbo Feng 6550ec27c356SChenbo Feng static int selinux_bpf_prog(struct bpf_prog *prog) 6551ec27c356SChenbo Feng { 6552ec27c356SChenbo Feng u32 sid = current_sid(); 6553ec27c356SChenbo Feng struct bpf_security_struct *bpfsec; 6554ec27c356SChenbo Feng 6555ec27c356SChenbo Feng bpfsec = prog->aux->security; 65566b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 65576b6bc620SStephen Smalley sid, bpfsec->sid, SECCLASS_BPF, 6558ec27c356SChenbo Feng BPF__PROG_RUN, NULL); 6559ec27c356SChenbo Feng } 6560ec27c356SChenbo Feng 6561ec27c356SChenbo Feng static int selinux_bpf_map_alloc(struct bpf_map *map) 6562ec27c356SChenbo Feng { 6563ec27c356SChenbo Feng struct bpf_security_struct *bpfsec; 6564ec27c356SChenbo Feng 6565ec27c356SChenbo Feng bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL); 6566ec27c356SChenbo Feng if (!bpfsec) 6567ec27c356SChenbo Feng return -ENOMEM; 6568ec27c356SChenbo Feng 6569ec27c356SChenbo Feng bpfsec->sid = current_sid(); 6570ec27c356SChenbo Feng map->security = bpfsec; 6571ec27c356SChenbo Feng 6572ec27c356SChenbo Feng return 0; 6573ec27c356SChenbo Feng } 6574ec27c356SChenbo Feng 6575ec27c356SChenbo Feng static void selinux_bpf_map_free(struct bpf_map *map) 6576ec27c356SChenbo Feng { 6577ec27c356SChenbo Feng struct bpf_security_struct *bpfsec = map->security; 6578ec27c356SChenbo Feng 6579ec27c356SChenbo Feng map->security = NULL; 6580ec27c356SChenbo Feng kfree(bpfsec); 6581ec27c356SChenbo Feng } 6582ec27c356SChenbo Feng 6583ec27c356SChenbo Feng static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux) 6584ec27c356SChenbo Feng { 6585ec27c356SChenbo Feng struct bpf_security_struct *bpfsec; 6586ec27c356SChenbo Feng 6587ec27c356SChenbo Feng bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL); 6588ec27c356SChenbo Feng if (!bpfsec) 6589ec27c356SChenbo Feng return -ENOMEM; 6590ec27c356SChenbo Feng 6591ec27c356SChenbo Feng bpfsec->sid = current_sid(); 6592ec27c356SChenbo Feng aux->security = bpfsec; 6593ec27c356SChenbo Feng 6594ec27c356SChenbo Feng return 0; 6595ec27c356SChenbo Feng } 6596ec27c356SChenbo Feng 6597ec27c356SChenbo Feng static void selinux_bpf_prog_free(struct bpf_prog_aux *aux) 6598ec27c356SChenbo Feng { 6599ec27c356SChenbo Feng struct bpf_security_struct *bpfsec = aux->security; 6600ec27c356SChenbo Feng 6601ec27c356SChenbo Feng aux->security = NULL; 6602ec27c356SChenbo Feng kfree(bpfsec); 6603ec27c356SChenbo Feng } 6604ec27c356SChenbo Feng #endif 6605ec27c356SChenbo Feng 6606bbd3662aSCasey Schaufler struct lsm_blob_sizes selinux_blob_sizes __lsm_ro_after_init = { 6607bbd3662aSCasey Schaufler .lbs_cred = sizeof(struct task_security_struct), 660833bf60caSCasey Schaufler .lbs_file = sizeof(struct file_security_struct), 6609afb1cbe3SCasey Schaufler .lbs_inode = sizeof(struct inode_security_struct), 6610bbd3662aSCasey Schaufler }; 6611bbd3662aSCasey Schaufler 6612ca97d939SJames Morris static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = { 6613e20b043aSCasey Schaufler LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr), 6614e20b043aSCasey Schaufler LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction), 6615e20b043aSCasey Schaufler LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder), 6616e20b043aSCasey Schaufler LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file), 6617076c54c5SAhmed S. Darwish 6618e20b043aSCasey Schaufler LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check), 6619e20b043aSCasey Schaufler LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme), 6620e20b043aSCasey Schaufler LSM_HOOK_INIT(capget, selinux_capget), 6621e20b043aSCasey Schaufler LSM_HOOK_INIT(capset, selinux_capset), 6622e20b043aSCasey Schaufler LSM_HOOK_INIT(capable, selinux_capable), 6623e20b043aSCasey Schaufler LSM_HOOK_INIT(quotactl, selinux_quotactl), 6624e20b043aSCasey Schaufler LSM_HOOK_INIT(quota_on, selinux_quota_on), 6625e20b043aSCasey Schaufler LSM_HOOK_INIT(syslog, selinux_syslog), 6626e20b043aSCasey Schaufler LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory), 662779af7307SStephen Smalley 6628e20b043aSCasey Schaufler LSM_HOOK_INIT(netlink_send, selinux_netlink_send), 66291da177e4SLinus Torvalds 6630e20b043aSCasey Schaufler LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds), 6631e20b043aSCasey Schaufler LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds), 6632e20b043aSCasey Schaufler LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds), 66331da177e4SLinus Torvalds 6634e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security), 6635e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security), 66365b400239SAl Viro LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts), 6637204cc0ccSAl Viro LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts), 6638e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_remount, selinux_sb_remount), 6639e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount), 6640e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options), 6641e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs), 6642e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_mount, selinux_mount), 6643e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_umount, selinux_umount), 6644e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts), 6645e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts), 6646757cbe59SAl Viro LSM_HOOK_INIT(sb_add_mnt_opt, selinux_add_mnt_opt), 66471da177e4SLinus Torvalds 6648e20b043aSCasey Schaufler LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security), 6649a518b0a5SVivek Goyal LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as), 6650e0007529SEric Paris 6651e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security), 6652e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security), 6653e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security), 6654e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_create, selinux_inode_create), 6655e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_link, selinux_inode_link), 6656e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink), 6657e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink), 6658e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir), 6659e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir), 6660e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod), 6661e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_rename, selinux_inode_rename), 6662e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink), 6663e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link), 6664e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_permission, selinux_inode_permission), 6665e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr), 6666e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr), 6667e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr), 6668e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr), 6669e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr), 6670e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr), 6671e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr), 6672e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity), 6673e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity), 6674e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity), 6675e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid), 667656909eb3SVivek Goyal LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up), 667719472b69SVivek Goyal LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr), 66781da177e4SLinus Torvalds 6679e20b043aSCasey Schaufler LSM_HOOK_INIT(file_permission, selinux_file_permission), 6680e20b043aSCasey Schaufler LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security), 6681e20b043aSCasey Schaufler LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl), 6682e20b043aSCasey Schaufler LSM_HOOK_INIT(mmap_file, selinux_mmap_file), 6683e20b043aSCasey Schaufler LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr), 6684e20b043aSCasey Schaufler LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect), 6685e20b043aSCasey Schaufler LSM_HOOK_INIT(file_lock, selinux_file_lock), 6686e20b043aSCasey Schaufler LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl), 6687e20b043aSCasey Schaufler LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner), 6688e20b043aSCasey Schaufler LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask), 6689e20b043aSCasey Schaufler LSM_HOOK_INIT(file_receive, selinux_file_receive), 66901da177e4SLinus Torvalds 6691e20b043aSCasey Schaufler LSM_HOOK_INIT(file_open, selinux_file_open), 66921da177e4SLinus Torvalds 6693a79be238STetsuo Handa LSM_HOOK_INIT(task_alloc, selinux_task_alloc), 6694e20b043aSCasey Schaufler LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare), 6695e20b043aSCasey Schaufler LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer), 66963ec30113SMatthew Garrett LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid), 6697e20b043aSCasey Schaufler LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as), 6698e20b043aSCasey Schaufler LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as), 6699e20b043aSCasey Schaufler LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request), 6700c77b8cdfSMimi Zohar LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data), 670161d612eaSJeff Vander Stoep LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file), 6702e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid), 6703e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid), 6704e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getsid, selinux_task_getsid), 6705e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid), 6706e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setnice, selinux_task_setnice), 6707e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio), 6708e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio), 6709791ec491SStephen Smalley LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit), 6710e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit), 6711e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler), 6712e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler), 6713e20b043aSCasey Schaufler LSM_HOOK_INIT(task_movememory, selinux_task_movememory), 6714e20b043aSCasey Schaufler LSM_HOOK_INIT(task_kill, selinux_task_kill), 6715e20b043aSCasey Schaufler LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode), 6716788e7dd4SYuichi Nakamura 6717e20b043aSCasey Schaufler LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission), 6718e20b043aSCasey Schaufler LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid), 67191da177e4SLinus Torvalds 6720e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security), 6721e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_msg_free_security, selinux_msg_msg_free_security), 67221da177e4SLinus Torvalds 6723e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_alloc_security, 6724e20b043aSCasey Schaufler selinux_msg_queue_alloc_security), 6725e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_free_security, selinux_msg_queue_free_security), 6726e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate), 6727e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl), 6728e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd), 6729e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv), 67301da177e4SLinus Torvalds 6731e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security), 6732e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_free_security, selinux_shm_free_security), 6733e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_associate, selinux_shm_associate), 6734e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl), 6735e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat), 67361da177e4SLinus Torvalds 6737e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security), 6738e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_free_security, selinux_sem_free_security), 6739e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_associate, selinux_sem_associate), 6740e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl), 6741e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_semop, selinux_sem_semop), 67421da177e4SLinus Torvalds 6743e20b043aSCasey Schaufler LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate), 67441da177e4SLinus Torvalds 6745e20b043aSCasey Schaufler LSM_HOOK_INIT(getprocattr, selinux_getprocattr), 6746e20b043aSCasey Schaufler LSM_HOOK_INIT(setprocattr, selinux_setprocattr), 67471da177e4SLinus Torvalds 6748e20b043aSCasey Schaufler LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel), 6749e20b043aSCasey Schaufler LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx), 6750e20b043aSCasey Schaufler LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid), 6751e20b043aSCasey Schaufler LSM_HOOK_INIT(release_secctx, selinux_release_secctx), 67526f3be9f5SAndreas Gruenbacher LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx), 6753e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx), 6754e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx), 6755e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx), 67561da177e4SLinus Torvalds 6757e20b043aSCasey Schaufler LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect), 6758e20b043aSCasey Schaufler LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send), 6759dc49c1f9SCatherine Zhang 6760e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_create, selinux_socket_create), 6761e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create), 67620b811db2SDavid Herrmann LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair), 6763e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_bind, selinux_socket_bind), 6764e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_connect, selinux_socket_connect), 6765e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_listen, selinux_socket_listen), 6766e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_accept, selinux_socket_accept), 6767e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg), 6768e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg), 6769e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname), 6770e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername), 6771e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt), 6772e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt), 6773e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown), 6774e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb), 6775e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getpeersec_stream, 6776e20b043aSCasey Schaufler selinux_socket_getpeersec_stream), 6777e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram), 6778e20b043aSCasey Schaufler LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security), 6779e20b043aSCasey Schaufler LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security), 6780e20b043aSCasey Schaufler LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security), 6781e20b043aSCasey Schaufler LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid), 6782e20b043aSCasey Schaufler LSM_HOOK_INIT(sock_graft, selinux_sock_graft), 6783d452930fSRichard Haines LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request), 6784d452930fSRichard Haines LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone), 6785d452930fSRichard Haines LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect), 6786e20b043aSCasey Schaufler LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request), 6787e20b043aSCasey Schaufler LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone), 6788e20b043aSCasey Schaufler LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established), 6789e20b043aSCasey Schaufler LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet), 6790e20b043aSCasey Schaufler LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc), 6791e20b043aSCasey Schaufler LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec), 6792e20b043aSCasey Schaufler LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow), 6793e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security), 6794e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security), 6795e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create), 6796e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue), 6797e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach), 6798e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open), 67993a976fa6SDaniel Jurgens #ifdef CONFIG_SECURITY_INFINIBAND 6800cfc4d882SDaniel Jurgens LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access), 6801ab861dfcSDaniel Jurgens LSM_HOOK_INIT(ib_endport_manage_subnet, 6802ab861dfcSDaniel Jurgens selinux_ib_endport_manage_subnet), 68033a976fa6SDaniel Jurgens LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security), 68043a976fa6SDaniel Jurgens LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security), 68053a976fa6SDaniel Jurgens #endif 6806d28d1e08STrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM 6807e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc), 6808e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone), 6809e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free), 6810e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete), 6811e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc), 6812e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_alloc_acquire, 6813e20b043aSCasey Schaufler selinux_xfrm_state_alloc_acquire), 6814e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free), 6815e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete), 6816e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup), 6817e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_pol_flow_match, 6818e20b043aSCasey Schaufler selinux_xfrm_state_pol_flow_match), 6819e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session), 68201da177e4SLinus Torvalds #endif 6821d720024eSMichael LeMay 6822d720024eSMichael LeMay #ifdef CONFIG_KEYS 6823e20b043aSCasey Schaufler LSM_HOOK_INIT(key_alloc, selinux_key_alloc), 6824e20b043aSCasey Schaufler LSM_HOOK_INIT(key_free, selinux_key_free), 6825e20b043aSCasey Schaufler LSM_HOOK_INIT(key_permission, selinux_key_permission), 6826e20b043aSCasey Schaufler LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity), 6827d720024eSMichael LeMay #endif 68289d57a7f9SAhmed S. Darwish 68299d57a7f9SAhmed S. Darwish #ifdef CONFIG_AUDIT 6830e20b043aSCasey Schaufler LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init), 6831e20b043aSCasey Schaufler LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known), 6832e20b043aSCasey Schaufler LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match), 6833e20b043aSCasey Schaufler LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free), 68349d57a7f9SAhmed S. Darwish #endif 6835ec27c356SChenbo Feng 6836ec27c356SChenbo Feng #ifdef CONFIG_BPF_SYSCALL 6837ec27c356SChenbo Feng LSM_HOOK_INIT(bpf, selinux_bpf), 6838ec27c356SChenbo Feng LSM_HOOK_INIT(bpf_map, selinux_bpf_map), 6839ec27c356SChenbo Feng LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog), 6840ec27c356SChenbo Feng LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc), 6841ec27c356SChenbo Feng LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc), 6842ec27c356SChenbo Feng LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free), 6843ec27c356SChenbo Feng LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free), 6844ec27c356SChenbo Feng #endif 68451da177e4SLinus Torvalds }; 68461da177e4SLinus Torvalds 68471da177e4SLinus Torvalds static __init int selinux_init(void) 68481da177e4SLinus Torvalds { 6849c103a91eSpeter enderborg pr_info("SELinux: Initializing.\n"); 68501da177e4SLinus Torvalds 6851aa8e712cSStephen Smalley memset(&selinux_state, 0, sizeof(selinux_state)); 6852e5a5ca96SPaul Moore enforcing_set(&selinux_state, selinux_enforcing_boot); 6853aa8e712cSStephen Smalley selinux_state.checkreqprot = selinux_checkreqprot_boot; 6854aa8e712cSStephen Smalley selinux_ss_init(&selinux_state.ss); 68556b6bc620SStephen Smalley selinux_avc_init(&selinux_state.avc); 6856aa8e712cSStephen Smalley 68571da177e4SLinus Torvalds /* Set the security state for the initial task. */ 6858d84f4f99SDavid Howells cred_init_security(); 68591da177e4SLinus Torvalds 6860fcaaade1SStephen Smalley default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC); 6861fcaaade1SStephen Smalley 68621da177e4SLinus Torvalds avc_init(); 68631da177e4SLinus Torvalds 6864aa8e712cSStephen Smalley avtab_cache_init(); 6865aa8e712cSStephen Smalley 6866aa8e712cSStephen Smalley ebitmap_cache_init(); 6867aa8e712cSStephen Smalley 6868aa8e712cSStephen Smalley hashtab_cache_init(); 6869aa8e712cSStephen Smalley 6870d69dece5SCasey Schaufler security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux"); 68711da177e4SLinus Torvalds 6872615e51fdSPaul Moore if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET)) 6873615e51fdSPaul Moore panic("SELinux: Unable to register AVC netcache callback\n"); 6874615e51fdSPaul Moore 68758f408ab6SDaniel Jurgens if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET)) 68768f408ab6SDaniel Jurgens panic("SELinux: Unable to register AVC LSM notifier callback\n"); 68778f408ab6SDaniel Jurgens 6878aa8e712cSStephen Smalley if (selinux_enforcing_boot) 6879c103a91eSpeter enderborg pr_debug("SELinux: Starting in enforcing mode\n"); 6880828dfe1dSEric Paris else 6881c103a91eSpeter enderborg pr_debug("SELinux: Starting in permissive mode\n"); 6882d720024eSMichael LeMay 68831da177e4SLinus Torvalds return 0; 68841da177e4SLinus Torvalds } 68851da177e4SLinus Torvalds 6886e8c26255SAl Viro static void delayed_superblock_init(struct super_block *sb, void *unused) 6887e8c26255SAl Viro { 6888204cc0ccSAl Viro selinux_set_mnt_opts(sb, NULL, 0, NULL); 6889e8c26255SAl Viro } 6890e8c26255SAl Viro 68911da177e4SLinus Torvalds void selinux_complete_init(void) 68921da177e4SLinus Torvalds { 6893c103a91eSpeter enderborg pr_debug("SELinux: Completing initialization.\n"); 68941da177e4SLinus Torvalds 68951da177e4SLinus Torvalds /* Set up any superblocks initialized prior to the policy load. */ 6896c103a91eSpeter enderborg pr_debug("SELinux: Setting up existing superblocks.\n"); 6897e8c26255SAl Viro iterate_supers(delayed_superblock_init, NULL); 68981da177e4SLinus Torvalds } 68991da177e4SLinus Torvalds 69001da177e4SLinus Torvalds /* SELinux requires early initialization in order to label 69011da177e4SLinus Torvalds all processes and objects when they are created. */ 69023d6e5f6dSKees Cook DEFINE_LSM(selinux) = { 690307aed2f2SKees Cook .name = "selinux", 690414bd99c8SKees Cook .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE, 6905c5459b82SKees Cook .enabled = &selinux_enabled, 6906bbd3662aSCasey Schaufler .blobs = &selinux_blob_sizes, 69073d6e5f6dSKees Cook .init = selinux_init, 69083d6e5f6dSKees Cook }; 69091da177e4SLinus Torvalds 6910c2b507fdSStephen Smalley #if defined(CONFIG_NETFILTER) 69111da177e4SLinus Torvalds 6912591bb278SFlorian Westphal static const struct nf_hook_ops selinux_nf_ops[] = { 6913effad8dfSPaul Moore { 6914effad8dfSPaul Moore .hook = selinux_ipv4_postroute, 69152597a834SAlban Crequy .pf = NFPROTO_IPV4, 69166e23ae2aSPatrick McHardy .hooknum = NF_INET_POST_ROUTING, 69171da177e4SLinus Torvalds .priority = NF_IP_PRI_SELINUX_LAST, 6918effad8dfSPaul Moore }, 6919effad8dfSPaul Moore { 6920effad8dfSPaul Moore .hook = selinux_ipv4_forward, 69212597a834SAlban Crequy .pf = NFPROTO_IPV4, 6922effad8dfSPaul Moore .hooknum = NF_INET_FORWARD, 6923effad8dfSPaul Moore .priority = NF_IP_PRI_SELINUX_FIRST, 6924948bf85cSPaul Moore }, 6925948bf85cSPaul Moore { 6926948bf85cSPaul Moore .hook = selinux_ipv4_output, 69272597a834SAlban Crequy .pf = NFPROTO_IPV4, 6928948bf85cSPaul Moore .hooknum = NF_INET_LOCAL_OUT, 6929948bf85cSPaul Moore .priority = NF_IP_PRI_SELINUX_FIRST, 693025db6beaSJiri Pirko }, 69311a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 6932effad8dfSPaul Moore { 6933effad8dfSPaul Moore .hook = selinux_ipv6_postroute, 69342597a834SAlban Crequy .pf = NFPROTO_IPV6, 69356e23ae2aSPatrick McHardy .hooknum = NF_INET_POST_ROUTING, 69361da177e4SLinus Torvalds .priority = NF_IP6_PRI_SELINUX_LAST, 6937effad8dfSPaul Moore }, 6938effad8dfSPaul Moore { 6939effad8dfSPaul Moore .hook = selinux_ipv6_forward, 69402597a834SAlban Crequy .pf = NFPROTO_IPV6, 6941effad8dfSPaul Moore .hooknum = NF_INET_FORWARD, 6942effad8dfSPaul Moore .priority = NF_IP6_PRI_SELINUX_FIRST, 694325db6beaSJiri Pirko }, 69442917f57bSHuw Davies { 69452917f57bSHuw Davies .hook = selinux_ipv6_output, 69462917f57bSHuw Davies .pf = NFPROTO_IPV6, 69472917f57bSHuw Davies .hooknum = NF_INET_LOCAL_OUT, 69482917f57bSHuw Davies .priority = NF_IP6_PRI_SELINUX_FIRST, 69492917f57bSHuw Davies }, 69501da177e4SLinus Torvalds #endif /* IPV6 */ 695125db6beaSJiri Pirko }; 69521da177e4SLinus Torvalds 69538e71bf75SFlorian Westphal static int __net_init selinux_nf_register(struct net *net) 69548e71bf75SFlorian Westphal { 69558e71bf75SFlorian Westphal return nf_register_net_hooks(net, selinux_nf_ops, 69568e71bf75SFlorian Westphal ARRAY_SIZE(selinux_nf_ops)); 69578e71bf75SFlorian Westphal } 69588e71bf75SFlorian Westphal 69598e71bf75SFlorian Westphal static void __net_exit selinux_nf_unregister(struct net *net) 69608e71bf75SFlorian Westphal { 69618e71bf75SFlorian Westphal nf_unregister_net_hooks(net, selinux_nf_ops, 69628e71bf75SFlorian Westphal ARRAY_SIZE(selinux_nf_ops)); 69638e71bf75SFlorian Westphal } 69648e71bf75SFlorian Westphal 69658e71bf75SFlorian Westphal static struct pernet_operations selinux_net_ops = { 69668e71bf75SFlorian Westphal .init = selinux_nf_register, 69678e71bf75SFlorian Westphal .exit = selinux_nf_unregister, 69688e71bf75SFlorian Westphal }; 69698e71bf75SFlorian Westphal 69701da177e4SLinus Torvalds static int __init selinux_nf_ip_init(void) 69711da177e4SLinus Torvalds { 697225db6beaSJiri Pirko int err; 69731da177e4SLinus Torvalds 69741da177e4SLinus Torvalds if (!selinux_enabled) 697525db6beaSJiri Pirko return 0; 69761da177e4SLinus Torvalds 6977c103a91eSpeter enderborg pr_debug("SELinux: Registering netfilter hooks\n"); 69781da177e4SLinus Torvalds 69798e71bf75SFlorian Westphal err = register_pernet_subsys(&selinux_net_ops); 69801da177e4SLinus Torvalds if (err) 69818e71bf75SFlorian Westphal panic("SELinux: register_pernet_subsys: error %d\n", err); 69821da177e4SLinus Torvalds 698325db6beaSJiri Pirko return 0; 69841da177e4SLinus Torvalds } 69851da177e4SLinus Torvalds __initcall(selinux_nf_ip_init); 69861da177e4SLinus Torvalds 69871da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 69881da177e4SLinus Torvalds static void selinux_nf_ip_exit(void) 69891da177e4SLinus Torvalds { 6990c103a91eSpeter enderborg pr_debug("SELinux: Unregistering netfilter hooks\n"); 69911da177e4SLinus Torvalds 69928e71bf75SFlorian Westphal unregister_pernet_subsys(&selinux_net_ops); 69931da177e4SLinus Torvalds } 69941da177e4SLinus Torvalds #endif 69951da177e4SLinus Torvalds 6996c2b507fdSStephen Smalley #else /* CONFIG_NETFILTER */ 69971da177e4SLinus Torvalds 69981da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 69991da177e4SLinus Torvalds #define selinux_nf_ip_exit() 70001da177e4SLinus Torvalds #endif 70011da177e4SLinus Torvalds 7002c2b507fdSStephen Smalley #endif /* CONFIG_NETFILTER */ 70031da177e4SLinus Torvalds 70041da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 7005aa8e712cSStephen Smalley int selinux_disable(struct selinux_state *state) 70061da177e4SLinus Torvalds { 7007aa8e712cSStephen Smalley if (state->initialized) { 70081da177e4SLinus Torvalds /* Not permitted after initial policy load. */ 70091da177e4SLinus Torvalds return -EINVAL; 70101da177e4SLinus Torvalds } 70111da177e4SLinus Torvalds 7012aa8e712cSStephen Smalley if (state->disabled) { 70131da177e4SLinus Torvalds /* Only do this once. */ 70141da177e4SLinus Torvalds return -EINVAL; 70151da177e4SLinus Torvalds } 70161da177e4SLinus Torvalds 7017aa8e712cSStephen Smalley state->disabled = 1; 7018aa8e712cSStephen Smalley 7019c103a91eSpeter enderborg pr_info("SELinux: Disabled at runtime.\n"); 70201da177e4SLinus Torvalds 702130d55280SStephen Smalley selinux_enabled = 0; 70221da177e4SLinus Torvalds 7023b1d9e6b0SCasey Schaufler security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks)); 70241da177e4SLinus Torvalds 7025af8ff049SEric Paris /* Try to destroy the avc node cache */ 7026af8ff049SEric Paris avc_disable(); 7027af8ff049SEric Paris 70281da177e4SLinus Torvalds /* Unregister netfilter hooks. */ 70291da177e4SLinus Torvalds selinux_nf_ip_exit(); 70301da177e4SLinus Torvalds 70311da177e4SLinus Torvalds /* Unregister selinuxfs. */ 70321da177e4SLinus Torvalds exit_sel_fs(); 70331da177e4SLinus Torvalds 70341da177e4SLinus Torvalds return 0; 70351da177e4SLinus Torvalds } 70361da177e4SLinus Torvalds #endif 7037