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> 82877ce7c1SCatherine Zhang #include <linux/selinux.h> 8323970741SEric Paris #include <linux/mutex.h> 84f06febc9SFrank Mayhar #include <linux/posix-timers.h> 8500234592SKees Cook #include <linux/syslog.h> 863486740aSSerge E. Hallyn #include <linux/user_namespace.h> 8744fc7ea0SPaul Gortmaker #include <linux/export.h> 8840401530SAl Viro #include <linux/msg.h> 8940401530SAl Viro #include <linux/shm.h> 90ec27c356SChenbo Feng #include <linux/bpf.h> 91e262e32dSDavid Howells #include <uapi/linux/mount.h> 921da177e4SLinus Torvalds 931da177e4SLinus Torvalds #include "avc.h" 941da177e4SLinus Torvalds #include "objsec.h" 951da177e4SLinus Torvalds #include "netif.h" 96224dfbd8SPaul Moore #include "netnode.h" 973e112172SPaul Moore #include "netport.h" 98409dcf31SDaniel Jurgens #include "ibpkey.h" 99d28d1e08STrent Jaeger #include "xfrm.h" 100c60475bfSPaul Moore #include "netlabel.h" 1019d57a7f9SAhmed S. Darwish #include "audit.h" 1027b98a585SJames Morris #include "avc_ss.h" 1031da177e4SLinus Torvalds 104aa8e712cSStephen Smalley struct selinux_state selinux_state; 105aa8e712cSStephen Smalley 106d621d35eSPaul Moore /* SECMARK reference count */ 10756a4ca99SJames Morris static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0); 108d621d35eSPaul Moore 1091da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP 110aa8e712cSStephen Smalley static int selinux_enforcing_boot; 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds static int __init enforcing_setup(char *str) 1131da177e4SLinus Torvalds { 114f5269710SEric Paris unsigned long enforcing; 11529707b20SJingoo Han if (!kstrtoul(str, 0, &enforcing)) 116aa8e712cSStephen Smalley selinux_enforcing_boot = enforcing ? 1 : 0; 1171da177e4SLinus Torvalds return 1; 1181da177e4SLinus Torvalds } 1191da177e4SLinus Torvalds __setup("enforcing=", enforcing_setup); 120aa8e712cSStephen Smalley #else 121aa8e712cSStephen Smalley #define selinux_enforcing_boot 1 1221da177e4SLinus Torvalds #endif 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM 1251da177e4SLinus Torvalds int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE; 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds static int __init selinux_enabled_setup(char *str) 1281da177e4SLinus Torvalds { 129f5269710SEric Paris unsigned long enabled; 13029707b20SJingoo Han if (!kstrtoul(str, 0, &enabled)) 131f5269710SEric Paris selinux_enabled = enabled ? 1 : 0; 1321da177e4SLinus Torvalds return 1; 1331da177e4SLinus Torvalds } 1341da177e4SLinus Torvalds __setup("selinux=", selinux_enabled_setup); 13530d55280SStephen Smalley #else 13630d55280SStephen Smalley int selinux_enabled = 1; 1371da177e4SLinus Torvalds #endif 1381da177e4SLinus Torvalds 139aa8e712cSStephen Smalley static unsigned int selinux_checkreqprot_boot = 140aa8e712cSStephen Smalley CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE; 141aa8e712cSStephen Smalley 142aa8e712cSStephen Smalley static int __init checkreqprot_setup(char *str) 143aa8e712cSStephen Smalley { 144aa8e712cSStephen Smalley unsigned long checkreqprot; 145aa8e712cSStephen Smalley 146aa8e712cSStephen Smalley if (!kstrtoul(str, 0, &checkreqprot)) 147aa8e712cSStephen Smalley selinux_checkreqprot_boot = checkreqprot ? 1 : 0; 148aa8e712cSStephen Smalley return 1; 149aa8e712cSStephen Smalley } 150aa8e712cSStephen Smalley __setup("checkreqprot=", checkreqprot_setup); 151aa8e712cSStephen Smalley 152e18b890bSChristoph Lameter static struct kmem_cache *sel_inode_cache; 15363205654SSangwoo static struct kmem_cache *file_security_cache; 1547cae7e26SJames Morris 155d621d35eSPaul Moore /** 156d621d35eSPaul Moore * selinux_secmark_enabled - Check to see if SECMARK is currently enabled 157d621d35eSPaul Moore * 158d621d35eSPaul Moore * Description: 159d621d35eSPaul Moore * This function checks the SECMARK reference counter to see if any SECMARK 160d621d35eSPaul Moore * targets are currently configured, if the reference counter is greater than 161d621d35eSPaul Moore * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is 1622be4d74fSChris PeBenito * enabled, false (0) if SECMARK is disabled. If the always_check_network 1632be4d74fSChris PeBenito * policy capability is enabled, SECMARK is always considered enabled. 164d621d35eSPaul Moore * 165d621d35eSPaul Moore */ 166d621d35eSPaul Moore static int selinux_secmark_enabled(void) 167d621d35eSPaul Moore { 168aa8e712cSStephen Smalley return (selinux_policycap_alwaysnetwork() || 169aa8e712cSStephen Smalley atomic_read(&selinux_secmark_refcount)); 1702be4d74fSChris PeBenito } 1712be4d74fSChris PeBenito 1722be4d74fSChris PeBenito /** 1732be4d74fSChris PeBenito * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled 1742be4d74fSChris PeBenito * 1752be4d74fSChris PeBenito * Description: 1762be4d74fSChris PeBenito * This function checks if NetLabel or labeled IPSEC is enabled. Returns true 1772be4d74fSChris PeBenito * (1) if any are enabled or false (0) if neither are enabled. If the 1782be4d74fSChris PeBenito * always_check_network policy capability is enabled, peer labeling 1792be4d74fSChris PeBenito * is always considered enabled. 1802be4d74fSChris PeBenito * 1812be4d74fSChris PeBenito */ 1822be4d74fSChris PeBenito static int selinux_peerlbl_enabled(void) 1832be4d74fSChris PeBenito { 184aa8e712cSStephen Smalley return (selinux_policycap_alwaysnetwork() || 185aa8e712cSStephen Smalley netlbl_enabled() || selinux_xfrm_enabled()); 186d621d35eSPaul Moore } 187d621d35eSPaul Moore 188615e51fdSPaul Moore static int selinux_netcache_avc_callback(u32 event) 189615e51fdSPaul Moore { 190615e51fdSPaul Moore if (event == AVC_CALLBACK_RESET) { 191615e51fdSPaul Moore sel_netif_flush(); 192615e51fdSPaul Moore sel_netnode_flush(); 193615e51fdSPaul Moore sel_netport_flush(); 194615e51fdSPaul Moore synchronize_net(); 195615e51fdSPaul Moore } 196615e51fdSPaul Moore return 0; 197615e51fdSPaul Moore } 198615e51fdSPaul Moore 1998f408ab6SDaniel Jurgens static int selinux_lsm_notifier_avc_callback(u32 event) 2008f408ab6SDaniel Jurgens { 201409dcf31SDaniel Jurgens if (event == AVC_CALLBACK_RESET) { 202409dcf31SDaniel Jurgens sel_ib_pkey_flush(); 2038f408ab6SDaniel Jurgens call_lsm_notifier(LSM_POLICY_CHANGE, NULL); 204409dcf31SDaniel Jurgens } 2058f408ab6SDaniel Jurgens 2068f408ab6SDaniel Jurgens return 0; 2078f408ab6SDaniel Jurgens } 2088f408ab6SDaniel Jurgens 209d84f4f99SDavid Howells /* 210d84f4f99SDavid Howells * initialise the security for the init task 211d84f4f99SDavid Howells */ 212d84f4f99SDavid Howells static void cred_init_security(void) 2131da177e4SLinus Torvalds { 2143b11a1deSDavid Howells struct cred *cred = (struct cred *) current->real_cred; 2151da177e4SLinus Torvalds struct task_security_struct *tsec; 2161da177e4SLinus Torvalds 21789d155efSJames Morris tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL); 2181da177e4SLinus Torvalds if (!tsec) 219d84f4f99SDavid Howells panic("SELinux: Failed to initialize initial task.\n"); 2201da177e4SLinus Torvalds 221d84f4f99SDavid Howells tsec->osid = tsec->sid = SECINITSID_KERNEL; 222f1752eecSDavid Howells cred->security = tsec; 2231da177e4SLinus Torvalds } 2241da177e4SLinus Torvalds 225275bb41eSDavid Howells /* 22688e67f3bSDavid Howells * get the security ID of a set of credentials 22788e67f3bSDavid Howells */ 22888e67f3bSDavid Howells static inline u32 cred_sid(const struct cred *cred) 22988e67f3bSDavid Howells { 23088e67f3bSDavid Howells const struct task_security_struct *tsec; 23188e67f3bSDavid Howells 23288e67f3bSDavid Howells tsec = cred->security; 23388e67f3bSDavid Howells return tsec->sid; 23488e67f3bSDavid Howells } 23588e67f3bSDavid Howells 23688e67f3bSDavid Howells /* 2373b11a1deSDavid Howells * get the objective security ID of a task 238275bb41eSDavid Howells */ 239275bb41eSDavid Howells static inline u32 task_sid(const struct task_struct *task) 240275bb41eSDavid Howells { 241275bb41eSDavid Howells u32 sid; 242275bb41eSDavid Howells 243275bb41eSDavid Howells rcu_read_lock(); 24488e67f3bSDavid Howells sid = cred_sid(__task_cred(task)); 245275bb41eSDavid Howells rcu_read_unlock(); 246275bb41eSDavid Howells return sid; 247275bb41eSDavid Howells } 248275bb41eSDavid Howells 24988e67f3bSDavid Howells /* Allocate and free functions for each kind of security blob. */ 25088e67f3bSDavid Howells 2511da177e4SLinus Torvalds static int inode_alloc_security(struct inode *inode) 2521da177e4SLinus Torvalds { 2531da177e4SLinus Torvalds struct inode_security_struct *isec; 254275bb41eSDavid Howells u32 sid = current_sid(); 2551da177e4SLinus Torvalds 256a02fe132SJosef Bacik isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS); 2571da177e4SLinus Torvalds if (!isec) 2581da177e4SLinus Torvalds return -ENOMEM; 2591da177e4SLinus Torvalds 2609287aed2SAndreas Gruenbacher spin_lock_init(&isec->lock); 2611da177e4SLinus Torvalds INIT_LIST_HEAD(&isec->list); 2621da177e4SLinus Torvalds isec->inode = inode; 2631da177e4SLinus Torvalds isec->sid = SECINITSID_UNLABELED; 2641da177e4SLinus Torvalds isec->sclass = SECCLASS_FILE; 265275bb41eSDavid Howells isec->task_sid = sid; 26642059112SAndreas Gruenbacher isec->initialized = LABEL_INVALID; 2671da177e4SLinus Torvalds inode->i_security = isec; 2681da177e4SLinus Torvalds 2691da177e4SLinus Torvalds return 0; 2701da177e4SLinus Torvalds } 2711da177e4SLinus Torvalds 2725d226df4SAndreas Gruenbacher static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry); 2735d226df4SAndreas Gruenbacher 2745d226df4SAndreas Gruenbacher /* 2755d226df4SAndreas Gruenbacher * Try reloading inode security labels that have been marked as invalid. The 2765d226df4SAndreas Gruenbacher * @may_sleep parameter indicates when sleeping and thus reloading labels is 27742059112SAndreas Gruenbacher * allowed; when set to false, returns -ECHILD when the label is 278e9193288SAl Viro * invalid. The @dentry parameter should be set to a dentry of the inode. 2795d226df4SAndreas Gruenbacher */ 2805d226df4SAndreas Gruenbacher static int __inode_security_revalidate(struct inode *inode, 281e9193288SAl Viro struct dentry *dentry, 2825d226df4SAndreas Gruenbacher bool may_sleep) 2835d226df4SAndreas Gruenbacher { 2845d226df4SAndreas Gruenbacher struct inode_security_struct *isec = inode->i_security; 2855d226df4SAndreas Gruenbacher 2865d226df4SAndreas Gruenbacher might_sleep_if(may_sleep); 2875d226df4SAndreas Gruenbacher 288aa8e712cSStephen Smalley if (selinux_state.initialized && 289aa8e712cSStephen Smalley isec->initialized != LABEL_INITIALIZED) { 2905d226df4SAndreas Gruenbacher if (!may_sleep) 2915d226df4SAndreas Gruenbacher return -ECHILD; 2925d226df4SAndreas Gruenbacher 2935d226df4SAndreas Gruenbacher /* 2945d226df4SAndreas Gruenbacher * Try reloading the inode security label. This will fail if 2955d226df4SAndreas Gruenbacher * @opt_dentry is NULL and no dentry for this inode can be 2965d226df4SAndreas Gruenbacher * found; in that case, continue using the old label. 2975d226df4SAndreas Gruenbacher */ 298e9193288SAl Viro inode_doinit_with_dentry(inode, dentry); 2995d226df4SAndreas Gruenbacher } 3005d226df4SAndreas Gruenbacher return 0; 3015d226df4SAndreas Gruenbacher } 3025d226df4SAndreas Gruenbacher 3035d226df4SAndreas Gruenbacher static struct inode_security_struct *inode_security_novalidate(struct inode *inode) 3045d226df4SAndreas Gruenbacher { 3055d226df4SAndreas Gruenbacher return inode->i_security; 3065d226df4SAndreas Gruenbacher } 3075d226df4SAndreas Gruenbacher 3085d226df4SAndreas Gruenbacher static struct inode_security_struct *inode_security_rcu(struct inode *inode, bool rcu) 3095d226df4SAndreas Gruenbacher { 3105d226df4SAndreas Gruenbacher int error; 3115d226df4SAndreas Gruenbacher 3125d226df4SAndreas Gruenbacher error = __inode_security_revalidate(inode, NULL, !rcu); 3135d226df4SAndreas Gruenbacher if (error) 3145d226df4SAndreas Gruenbacher return ERR_PTR(error); 3155d226df4SAndreas Gruenbacher return inode->i_security; 3165d226df4SAndreas Gruenbacher } 3175d226df4SAndreas Gruenbacher 31883da53c5SAndreas Gruenbacher /* 31983da53c5SAndreas Gruenbacher * Get the security label of an inode. 32083da53c5SAndreas Gruenbacher */ 32183da53c5SAndreas Gruenbacher static struct inode_security_struct *inode_security(struct inode *inode) 32283da53c5SAndreas Gruenbacher { 3235d226df4SAndreas Gruenbacher __inode_security_revalidate(inode, NULL, true); 32483da53c5SAndreas Gruenbacher return inode->i_security; 32583da53c5SAndreas Gruenbacher } 32683da53c5SAndreas Gruenbacher 3272c97165bSPaul Moore static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry) 3282c97165bSPaul Moore { 3292c97165bSPaul Moore struct inode *inode = d_backing_inode(dentry); 3302c97165bSPaul Moore 3312c97165bSPaul Moore return inode->i_security; 3322c97165bSPaul Moore } 3332c97165bSPaul Moore 33483da53c5SAndreas Gruenbacher /* 33583da53c5SAndreas Gruenbacher * Get the security label of a dentry's backing inode. 33683da53c5SAndreas Gruenbacher */ 33783da53c5SAndreas Gruenbacher static struct inode_security_struct *backing_inode_security(struct dentry *dentry) 33883da53c5SAndreas Gruenbacher { 33983da53c5SAndreas Gruenbacher struct inode *inode = d_backing_inode(dentry); 34083da53c5SAndreas Gruenbacher 3415d226df4SAndreas Gruenbacher __inode_security_revalidate(inode, dentry, true); 34283da53c5SAndreas Gruenbacher return inode->i_security; 34383da53c5SAndreas Gruenbacher } 34483da53c5SAndreas Gruenbacher 3453dc91d43SSteven Rostedt static void inode_free_rcu(struct rcu_head *head) 3463dc91d43SSteven Rostedt { 3473dc91d43SSteven Rostedt struct inode_security_struct *isec; 3483dc91d43SSteven Rostedt 3493dc91d43SSteven Rostedt isec = container_of(head, struct inode_security_struct, rcu); 3503dc91d43SSteven Rostedt kmem_cache_free(sel_inode_cache, isec); 3513dc91d43SSteven Rostedt } 3523dc91d43SSteven Rostedt 3531da177e4SLinus Torvalds static void inode_free_security(struct inode *inode) 3541da177e4SLinus Torvalds { 3551da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 3561da177e4SLinus Torvalds struct superblock_security_struct *sbsec = inode->i_sb->s_security; 3571da177e4SLinus Torvalds 3589629d04aSWaiman Long /* 3599629d04aSWaiman Long * As not all inode security structures are in a list, we check for 3609629d04aSWaiman Long * empty list outside of the lock to make sure that we won't waste 3619629d04aSWaiman Long * time taking a lock doing nothing. 3629629d04aSWaiman Long * 3639629d04aSWaiman Long * The list_del_init() function can be safely called more than once. 3649629d04aSWaiman Long * It should not be possible for this function to be called with 3659629d04aSWaiman Long * concurrent list_add(), but for better safety against future changes 3669629d04aSWaiman Long * in the code, we use list_empty_careful() here. 3679629d04aSWaiman Long */ 3689629d04aSWaiman Long if (!list_empty_careful(&isec->list)) { 3691da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 3701da177e4SLinus Torvalds list_del_init(&isec->list); 3711da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 3729629d04aSWaiman Long } 3731da177e4SLinus Torvalds 3743dc91d43SSteven Rostedt /* 3753dc91d43SSteven Rostedt * The inode may still be referenced in a path walk and 3763dc91d43SSteven Rostedt * a call to selinux_inode_permission() can be made 3773dc91d43SSteven Rostedt * after inode_free_security() is called. Ideally, the VFS 3783dc91d43SSteven Rostedt * wouldn't do this, but fixing that is a much harder 3793dc91d43SSteven Rostedt * job. For now, simply free the i_security via RCU, and 3803dc91d43SSteven Rostedt * leave the current inode->i_security pointer intact. 3813dc91d43SSteven Rostedt * The inode will be freed after the RCU grace period too. 3823dc91d43SSteven Rostedt */ 3833dc91d43SSteven Rostedt call_rcu(&isec->rcu, inode_free_rcu); 3841da177e4SLinus Torvalds } 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds static int file_alloc_security(struct file *file) 3871da177e4SLinus Torvalds { 3881da177e4SLinus Torvalds struct file_security_struct *fsec; 389275bb41eSDavid Howells u32 sid = current_sid(); 3901da177e4SLinus Torvalds 39163205654SSangwoo fsec = kmem_cache_zalloc(file_security_cache, GFP_KERNEL); 3921da177e4SLinus Torvalds if (!fsec) 3931da177e4SLinus Torvalds return -ENOMEM; 3941da177e4SLinus Torvalds 395275bb41eSDavid Howells fsec->sid = sid; 396275bb41eSDavid Howells fsec->fown_sid = sid; 3971da177e4SLinus Torvalds file->f_security = fsec; 3981da177e4SLinus Torvalds 3991da177e4SLinus Torvalds return 0; 4001da177e4SLinus Torvalds } 4011da177e4SLinus Torvalds 4021da177e4SLinus Torvalds static void file_free_security(struct file *file) 4031da177e4SLinus Torvalds { 4041da177e4SLinus Torvalds struct file_security_struct *fsec = file->f_security; 4051da177e4SLinus Torvalds file->f_security = NULL; 40663205654SSangwoo kmem_cache_free(file_security_cache, fsec); 4071da177e4SLinus Torvalds } 4081da177e4SLinus Torvalds 4091da177e4SLinus Torvalds static int superblock_alloc_security(struct super_block *sb) 4101da177e4SLinus Torvalds { 4111da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 4121da177e4SLinus Torvalds 41389d155efSJames Morris sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL); 4141da177e4SLinus Torvalds if (!sbsec) 4151da177e4SLinus Torvalds return -ENOMEM; 4161da177e4SLinus Torvalds 417bc7e982bSEric Paris mutex_init(&sbsec->lock); 4181da177e4SLinus Torvalds INIT_LIST_HEAD(&sbsec->isec_head); 4191da177e4SLinus Torvalds spin_lock_init(&sbsec->isec_lock); 4201da177e4SLinus Torvalds sbsec->sb = sb; 4211da177e4SLinus Torvalds sbsec->sid = SECINITSID_UNLABELED; 4221da177e4SLinus Torvalds sbsec->def_sid = SECINITSID_FILE; 423c312feb2SEric Paris sbsec->mntpoint_sid = SECINITSID_UNLABELED; 4241da177e4SLinus Torvalds sb->s_security = sbsec; 4251da177e4SLinus Torvalds 4261da177e4SLinus Torvalds return 0; 4271da177e4SLinus Torvalds } 4281da177e4SLinus Torvalds 4291da177e4SLinus Torvalds static void superblock_free_security(struct super_block *sb) 4301da177e4SLinus Torvalds { 4311da177e4SLinus Torvalds struct superblock_security_struct *sbsec = sb->s_security; 4321da177e4SLinus Torvalds sb->s_security = NULL; 4331da177e4SLinus Torvalds kfree(sbsec); 4341da177e4SLinus Torvalds } 4351da177e4SLinus Torvalds 436bd323655SAl Viro struct selinux_mnt_opts { 437bd323655SAl Viro const char *fscontext, *context, *rootcontext, *defcontext; 438bd323655SAl Viro }; 439bd323655SAl Viro 440204cc0ccSAl Viro static void selinux_free_mnt_opts(void *mnt_opts) 441204cc0ccSAl Viro { 442bd323655SAl Viro struct selinux_mnt_opts *opts = mnt_opts; 443bd323655SAl Viro kfree(opts->fscontext); 444bd323655SAl Viro kfree(opts->context); 445bd323655SAl Viro kfree(opts->rootcontext); 446bd323655SAl Viro kfree(opts->defcontext); 447204cc0ccSAl Viro kfree(opts); 448204cc0ccSAl Viro } 449204cc0ccSAl Viro 4501da177e4SLinus Torvalds static inline int inode_doinit(struct inode *inode) 4511da177e4SLinus Torvalds { 4521da177e4SLinus Torvalds return inode_doinit_with_dentry(inode, NULL); 4531da177e4SLinus Torvalds } 4541da177e4SLinus Torvalds 4551da177e4SLinus Torvalds enum { 45631e87930SEric Paris Opt_error = -1, 4571da177e4SLinus Torvalds Opt_context = 1, 4581da177e4SLinus Torvalds Opt_fscontext = 2, 459c9180a57SEric Paris Opt_defcontext = 3, 460c9180a57SEric Paris Opt_rootcontext = 4, 461da3d76abSAl Viro Opt_seclabel = 5, 4621da177e4SLinus Torvalds }; 4631da177e4SLinus Torvalds 464da3d76abSAl Viro #define A(s, has_arg) {#s, sizeof(#s) - 1, Opt_##s, has_arg} 465169d68efSAl Viro static struct { 466169d68efSAl Viro const char *name; 467169d68efSAl Viro int len; 468169d68efSAl Viro int opt; 469169d68efSAl Viro bool has_arg; 470169d68efSAl Viro } tokens[] = { 471da3d76abSAl Viro A(context, true), 472da3d76abSAl Viro A(fscontext, true), 473da3d76abSAl Viro A(defcontext, true), 474da3d76abSAl Viro A(rootcontext, true), 475da3d76abSAl Viro A(seclabel, false), 4761da177e4SLinus Torvalds }; 477169d68efSAl Viro #undef A 478169d68efSAl Viro 479169d68efSAl Viro static int match_opt_prefix(char *s, int l, char **arg) 480169d68efSAl Viro { 481169d68efSAl Viro int i; 482169d68efSAl Viro 483169d68efSAl Viro for (i = 0; i < ARRAY_SIZE(tokens); i++) { 484169d68efSAl Viro size_t len = tokens[i].len; 485169d68efSAl Viro if (len > l || memcmp(s, tokens[i].name, len)) 486169d68efSAl Viro continue; 487169d68efSAl Viro if (tokens[i].has_arg) { 488169d68efSAl Viro if (len == l || s[len] != '=') 489169d68efSAl Viro continue; 490169d68efSAl Viro *arg = s + len + 1; 491169d68efSAl Viro } else if (len != l) 492169d68efSAl Viro continue; 493169d68efSAl Viro return tokens[i].opt; 494169d68efSAl Viro } 495169d68efSAl Viro return Opt_error; 496169d68efSAl Viro } 4971da177e4SLinus Torvalds 4981da177e4SLinus Torvalds #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n" 4991da177e4SLinus Torvalds 500c312feb2SEric Paris static int may_context_mount_sb_relabel(u32 sid, 501c312feb2SEric Paris struct superblock_security_struct *sbsec, 502275bb41eSDavid Howells const struct cred *cred) 503c312feb2SEric Paris { 504275bb41eSDavid Howells const struct task_security_struct *tsec = cred->security; 505c312feb2SEric Paris int rc; 506c312feb2SEric Paris 5076b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 5086b6bc620SStephen Smalley tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 509c312feb2SEric Paris FILESYSTEM__RELABELFROM, NULL); 510c312feb2SEric Paris if (rc) 511c312feb2SEric Paris return rc; 512c312feb2SEric Paris 5136b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 5146b6bc620SStephen Smalley tsec->sid, sid, SECCLASS_FILESYSTEM, 515c312feb2SEric Paris FILESYSTEM__RELABELTO, NULL); 516c312feb2SEric Paris return rc; 517c312feb2SEric Paris } 518c312feb2SEric Paris 5190808925eSEric Paris static int may_context_mount_inode_relabel(u32 sid, 5200808925eSEric Paris struct superblock_security_struct *sbsec, 521275bb41eSDavid Howells const struct cred *cred) 5220808925eSEric Paris { 523275bb41eSDavid Howells const struct task_security_struct *tsec = cred->security; 5240808925eSEric Paris int rc; 5256b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 5266b6bc620SStephen Smalley tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 5270808925eSEric Paris FILESYSTEM__RELABELFROM, NULL); 5280808925eSEric Paris if (rc) 5290808925eSEric Paris return rc; 5300808925eSEric Paris 5316b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 5326b6bc620SStephen Smalley sid, sbsec->sid, SECCLASS_FILESYSTEM, 5330808925eSEric Paris FILESYSTEM__ASSOCIATE, NULL); 5340808925eSEric Paris return rc; 5350808925eSEric Paris } 5360808925eSEric Paris 537a83d6ddaSOndrej Mosnacek static int selinux_is_genfs_special_handling(struct super_block *sb) 538b43e725dSEric Paris { 539d5f3a5f6SMark Salyzyn /* Special handling. Genfs but also in-core setxattr handler */ 540a83d6ddaSOndrej Mosnacek return !strcmp(sb->s_type->name, "sysfs") || 541d5f3a5f6SMark Salyzyn !strcmp(sb->s_type->name, "pstore") || 542d5f3a5f6SMark Salyzyn !strcmp(sb->s_type->name, "debugfs") || 543a2c7c6fbSYongqin Liu !strcmp(sb->s_type->name, "tracefs") || 5442651225bSStephen Smalley !strcmp(sb->s_type->name, "rootfs") || 545aa8e712cSStephen Smalley (selinux_policycap_cgroupseclabel() && 5462651225bSStephen Smalley (!strcmp(sb->s_type->name, "cgroup") || 5472651225bSStephen Smalley !strcmp(sb->s_type->name, "cgroup2"))); 548b43e725dSEric Paris } 549b43e725dSEric Paris 550a83d6ddaSOndrej Mosnacek static int selinux_is_sblabel_mnt(struct super_block *sb) 551a83d6ddaSOndrej Mosnacek { 552a83d6ddaSOndrej Mosnacek struct superblock_security_struct *sbsec = sb->s_security; 553a83d6ddaSOndrej Mosnacek 554a83d6ddaSOndrej Mosnacek /* 555a83d6ddaSOndrej Mosnacek * IMPORTANT: Double-check logic in this function when adding a new 556a83d6ddaSOndrej Mosnacek * SECURITY_FS_USE_* definition! 557a83d6ddaSOndrej Mosnacek */ 558a83d6ddaSOndrej Mosnacek BUILD_BUG_ON(SECURITY_FS_USE_MAX != 7); 559a83d6ddaSOndrej Mosnacek 560a83d6ddaSOndrej Mosnacek switch (sbsec->behavior) { 561a83d6ddaSOndrej Mosnacek case SECURITY_FS_USE_XATTR: 562a83d6ddaSOndrej Mosnacek case SECURITY_FS_USE_TRANS: 563a83d6ddaSOndrej Mosnacek case SECURITY_FS_USE_TASK: 564a83d6ddaSOndrej Mosnacek case SECURITY_FS_USE_NATIVE: 565a83d6ddaSOndrej Mosnacek return 1; 566a83d6ddaSOndrej Mosnacek 567a83d6ddaSOndrej Mosnacek case SECURITY_FS_USE_GENFS: 568a83d6ddaSOndrej Mosnacek return selinux_is_genfs_special_handling(sb); 569a83d6ddaSOndrej Mosnacek 570a83d6ddaSOndrej Mosnacek /* Never allow relabeling on context mounts */ 571a83d6ddaSOndrej Mosnacek case SECURITY_FS_USE_MNTPOINT: 572a83d6ddaSOndrej Mosnacek case SECURITY_FS_USE_NONE: 573a83d6ddaSOndrej Mosnacek default: 574a83d6ddaSOndrej Mosnacek return 0; 575a83d6ddaSOndrej Mosnacek } 576a83d6ddaSOndrej Mosnacek } 577a83d6ddaSOndrej Mosnacek 578c9180a57SEric Paris static int sb_finish_set_opts(struct super_block *sb) 5791da177e4SLinus Torvalds { 5801da177e4SLinus Torvalds struct superblock_security_struct *sbsec = sb->s_security; 5811da177e4SLinus Torvalds struct dentry *root = sb->s_root; 582c6f493d6SDavid Howells struct inode *root_inode = d_backing_inode(root); 5831da177e4SLinus Torvalds int rc = 0; 5841da177e4SLinus Torvalds 5851da177e4SLinus Torvalds if (sbsec->behavior == SECURITY_FS_USE_XATTR) { 5861da177e4SLinus Torvalds /* Make sure that the xattr handler exists and that no 5871da177e4SLinus Torvalds error other than -ENODATA is returned by getxattr on 5881da177e4SLinus Torvalds the root directory. -ENODATA is ok, as this may be 5891da177e4SLinus Torvalds the first boot of the SELinux kernel before we have 5901da177e4SLinus Torvalds assigned xattr values to the filesystem. */ 5915d6c3191SAndreas Gruenbacher if (!(root_inode->i_opflags & IOP_XATTR)) { 592c103a91eSpeter enderborg pr_warn("SELinux: (dev %s, type %s) has no " 59329b1deb2SLinus Torvalds "xattr support\n", sb->s_id, sb->s_type->name); 5941da177e4SLinus Torvalds rc = -EOPNOTSUPP; 5951da177e4SLinus Torvalds goto out; 5961da177e4SLinus Torvalds } 5975d6c3191SAndreas Gruenbacher 5985d6c3191SAndreas Gruenbacher rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0); 5991da177e4SLinus Torvalds if (rc < 0 && rc != -ENODATA) { 6001da177e4SLinus Torvalds if (rc == -EOPNOTSUPP) 601c103a91eSpeter enderborg pr_warn("SELinux: (dev %s, type " 60229b1deb2SLinus Torvalds "%s) has no security xattr handler\n", 60329b1deb2SLinus Torvalds sb->s_id, sb->s_type->name); 6041da177e4SLinus Torvalds else 605c103a91eSpeter enderborg pr_warn("SELinux: (dev %s, type " 60629b1deb2SLinus Torvalds "%s) getxattr errno %d\n", sb->s_id, 60729b1deb2SLinus Torvalds sb->s_type->name, -rc); 6081da177e4SLinus Torvalds goto out; 6091da177e4SLinus Torvalds } 6101da177e4SLinus Torvalds } 6111da177e4SLinus Torvalds 612eadcabc6SEric Paris sbsec->flags |= SE_SBINITIALIZED; 6130b4d3452SScott Mayhew 6140b4d3452SScott Mayhew /* 6150b4d3452SScott Mayhew * Explicitly set or clear SBLABEL_MNT. It's not sufficient to simply 6160b4d3452SScott Mayhew * leave the flag untouched because sb_clone_mnt_opts might be handing 6170b4d3452SScott Mayhew * us a superblock that needs the flag to be cleared. 6180b4d3452SScott Mayhew */ 619b43e725dSEric Paris if (selinux_is_sblabel_mnt(sb)) 62012f348b9SEric Paris sbsec->flags |= SBLABEL_MNT; 6210b4d3452SScott Mayhew else 6220b4d3452SScott Mayhew sbsec->flags &= ~SBLABEL_MNT; 623ddd29ec6SDavid P. Quigley 6241da177e4SLinus Torvalds /* Initialize the root inode. */ 625c9180a57SEric Paris rc = inode_doinit_with_dentry(root_inode, root); 6261da177e4SLinus Torvalds 6271da177e4SLinus Torvalds /* Initialize any other inodes associated with the superblock, e.g. 6281da177e4SLinus Torvalds inodes created prior to initial policy load or inodes created 6291da177e4SLinus Torvalds during get_sb by a pseudo filesystem that directly 6301da177e4SLinus Torvalds populates itself. */ 6311da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 6328d64124aSAl Viro while (!list_empty(&sbsec->isec_head)) { 6331da177e4SLinus Torvalds struct inode_security_struct *isec = 6348d64124aSAl Viro list_first_entry(&sbsec->isec_head, 6351da177e4SLinus Torvalds struct inode_security_struct, list); 6361da177e4SLinus Torvalds struct inode *inode = isec->inode; 637923190d3SStephen Smalley list_del_init(&isec->list); 6381da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 6391da177e4SLinus Torvalds inode = igrab(inode); 6401da177e4SLinus Torvalds if (inode) { 6411da177e4SLinus Torvalds if (!IS_PRIVATE(inode)) 6421da177e4SLinus Torvalds inode_doinit(inode); 6431da177e4SLinus Torvalds iput(inode); 6441da177e4SLinus Torvalds } 6451da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 6461da177e4SLinus Torvalds } 6471da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 6481da177e4SLinus Torvalds out: 649c9180a57SEric Paris return rc; 650c9180a57SEric Paris } 651c9180a57SEric Paris 652c9180a57SEric Paris static int bad_option(struct superblock_security_struct *sbsec, char flag, 653c9180a57SEric Paris u32 old_sid, u32 new_sid) 654c9180a57SEric Paris { 6550d90a7ecSDavid P. Quigley char mnt_flags = sbsec->flags & SE_MNTMASK; 6560d90a7ecSDavid P. Quigley 657c9180a57SEric Paris /* check if the old mount command had the same options */ 6580d90a7ecSDavid P. Quigley if (sbsec->flags & SE_SBINITIALIZED) 659c9180a57SEric Paris if (!(sbsec->flags & flag) || 660c9180a57SEric Paris (old_sid != new_sid)) 661c9180a57SEric Paris return 1; 662c9180a57SEric Paris 663c9180a57SEric Paris /* check if we were passed the same options twice, 664c9180a57SEric Paris * aka someone passed context=a,context=b 665c9180a57SEric Paris */ 6660d90a7ecSDavid P. Quigley if (!(sbsec->flags & SE_SBINITIALIZED)) 6670d90a7ecSDavid P. Quigley if (mnt_flags & flag) 668c9180a57SEric Paris return 1; 669c9180a57SEric Paris return 0; 670c9180a57SEric Paris } 671e0007529SEric Paris 672bd323655SAl Viro static int parse_sid(struct super_block *sb, const char *s, u32 *sid) 673bd323655SAl Viro { 674bd323655SAl Viro int rc = security_context_str_to_sid(&selinux_state, s, 675bd323655SAl Viro sid, GFP_KERNEL); 676bd323655SAl Viro if (rc) 677bd323655SAl Viro pr_warn("SELinux: security_context_str_to_sid" 678bd323655SAl Viro "(%s) failed for (dev %s, type %s) errno=%d\n", 679bd323655SAl Viro s, sb->s_id, sb->s_type->name, rc); 680bd323655SAl Viro return rc; 681bd323655SAl Viro } 682bd323655SAl Viro 683c9180a57SEric Paris /* 684c9180a57SEric Paris * Allow filesystems with binary mount data to explicitly set mount point 685c9180a57SEric Paris * labeling information. 686c9180a57SEric Paris */ 687e0007529SEric Paris static int selinux_set_mnt_opts(struct super_block *sb, 688204cc0ccSAl Viro void *mnt_opts, 689649f6e77SDavid Quigley unsigned long kern_flags, 690649f6e77SDavid Quigley unsigned long *set_kern_flags) 691c9180a57SEric Paris { 692275bb41eSDavid Howells const struct cred *cred = current_cred(); 693c9180a57SEric Paris struct superblock_security_struct *sbsec = sb->s_security; 69483da53c5SAndreas Gruenbacher struct dentry *root = sbsec->sb->s_root; 695bd323655SAl Viro struct selinux_mnt_opts *opts = mnt_opts; 6962c97165bSPaul Moore struct inode_security_struct *root_isec; 697c9180a57SEric Paris u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0; 698c9180a57SEric Paris u32 defcontext_sid = 0; 699bd323655SAl Viro int rc = 0; 700c9180a57SEric Paris 701c9180a57SEric Paris mutex_lock(&sbsec->lock); 702c9180a57SEric Paris 703aa8e712cSStephen Smalley if (!selinux_state.initialized) { 704bd323655SAl Viro if (!opts) { 705c9180a57SEric Paris /* Defer initialization until selinux_complete_init, 706c9180a57SEric Paris after the initial policy is loaded and the security 707c9180a57SEric Paris server is ready to handle calls. */ 708c9180a57SEric Paris goto out; 709c9180a57SEric Paris } 710c9180a57SEric Paris rc = -EINVAL; 711c103a91eSpeter enderborg pr_warn("SELinux: Unable to set superblock options " 712744ba35eSEric Paris "before the security server is initialized\n"); 713c9180a57SEric Paris goto out; 714c9180a57SEric Paris } 715649f6e77SDavid Quigley if (kern_flags && !set_kern_flags) { 716649f6e77SDavid Quigley /* Specifying internal flags without providing a place to 717649f6e77SDavid Quigley * place the results is not allowed */ 718649f6e77SDavid Quigley rc = -EINVAL; 719649f6e77SDavid Quigley goto out; 720649f6e77SDavid Quigley } 721c9180a57SEric Paris 722c9180a57SEric Paris /* 723e0007529SEric Paris * Binary mount data FS will come through this function twice. Once 724e0007529SEric Paris * from an explicit call and once from the generic calls from the vfs. 725e0007529SEric Paris * Since the generic VFS calls will not contain any security mount data 726e0007529SEric Paris * we need to skip the double mount verification. 727e0007529SEric Paris * 728e0007529SEric Paris * This does open a hole in which we will not notice if the first 729e0007529SEric Paris * mount using this sb set explict options and a second mount using 730e0007529SEric Paris * this sb does not set any security options. (The first options 731e0007529SEric Paris * will be used for both mounts) 732e0007529SEric Paris */ 7330d90a7ecSDavid P. Quigley if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA) 734bd323655SAl Viro && !opts) 735e0007529SEric Paris goto out; 736e0007529SEric Paris 7372c97165bSPaul Moore root_isec = backing_inode_security_novalidate(root); 7382c97165bSPaul Moore 739e0007529SEric Paris /* 740c9180a57SEric Paris * parse the mount options, check if they are valid sids. 741c9180a57SEric Paris * also check if someone is trying to mount the same sb more 742c9180a57SEric Paris * than once with different security options. 743c9180a57SEric Paris */ 744bd323655SAl Viro if (opts) { 745bd323655SAl Viro if (opts->fscontext) { 746bd323655SAl Viro rc = parse_sid(sb, opts->fscontext, &fscontext_sid); 747bd323655SAl Viro if (rc) 748c9180a57SEric Paris goto out; 749c9180a57SEric Paris if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, 750c9180a57SEric Paris fscontext_sid)) 751c9180a57SEric Paris goto out_double_mount; 752c9180a57SEric Paris sbsec->flags |= FSCONTEXT_MNT; 753bd323655SAl Viro } 754bd323655SAl Viro if (opts->context) { 755bd323655SAl Viro rc = parse_sid(sb, opts->context, &context_sid); 756bd323655SAl Viro if (rc) 757bd323655SAl Viro goto out; 758c9180a57SEric Paris if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, 759c9180a57SEric Paris context_sid)) 760c9180a57SEric Paris goto out_double_mount; 761c9180a57SEric Paris sbsec->flags |= CONTEXT_MNT; 762bd323655SAl Viro } 763bd323655SAl Viro if (opts->rootcontext) { 764bd323655SAl Viro rc = parse_sid(sb, opts->rootcontext, &rootcontext_sid); 765bd323655SAl Viro if (rc) 766bd323655SAl Viro goto out; 767c9180a57SEric Paris if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, 768c9180a57SEric Paris rootcontext_sid)) 769c9180a57SEric Paris goto out_double_mount; 770c9180a57SEric Paris sbsec->flags |= ROOTCONTEXT_MNT; 771bd323655SAl Viro } 772bd323655SAl Viro if (opts->defcontext) { 773bd323655SAl Viro rc = parse_sid(sb, opts->defcontext, &defcontext_sid); 774bd323655SAl Viro if (rc) 775bd323655SAl Viro goto out; 776c9180a57SEric Paris if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, 777c9180a57SEric Paris defcontext_sid)) 778c9180a57SEric Paris goto out_double_mount; 779c9180a57SEric Paris sbsec->flags |= DEFCONTEXT_MNT; 780c9180a57SEric Paris } 781c9180a57SEric Paris } 782c9180a57SEric Paris 7830d90a7ecSDavid P. Quigley if (sbsec->flags & SE_SBINITIALIZED) { 784c9180a57SEric Paris /* previously mounted with options, but not on this attempt? */ 785bd323655SAl Viro if ((sbsec->flags & SE_MNTMASK) && !opts) 786c9180a57SEric Paris goto out_double_mount; 787c9180a57SEric Paris rc = 0; 788c9180a57SEric Paris goto out; 789c9180a57SEric Paris } 790c9180a57SEric Paris 791089be43eSJames Morris if (strcmp(sb->s_type->name, "proc") == 0) 792134509d5SStephen Smalley sbsec->flags |= SE_SBPROC | SE_SBGENFS; 793134509d5SStephen Smalley 7948e014720SStephen Smalley if (!strcmp(sb->s_type->name, "debugfs") || 7956a391183SJeff Vander Stoep !strcmp(sb->s_type->name, "tracefs") || 7968e014720SStephen Smalley !strcmp(sb->s_type->name, "sysfs") || 797901ef845SAntonio Murdaca !strcmp(sb->s_type->name, "pstore") || 798901ef845SAntonio Murdaca !strcmp(sb->s_type->name, "cgroup") || 799901ef845SAntonio Murdaca !strcmp(sb->s_type->name, "cgroup2")) 800134509d5SStephen Smalley sbsec->flags |= SE_SBGENFS; 801c9180a57SEric Paris 802eb9ae686SDavid Quigley if (!sbsec->behavior) { 803eb9ae686SDavid Quigley /* 804eb9ae686SDavid Quigley * Determine the labeling behavior to use for this 805eb9ae686SDavid Quigley * filesystem type. 806eb9ae686SDavid Quigley */ 807aa8e712cSStephen Smalley rc = security_fs_use(&selinux_state, sb); 808c9180a57SEric Paris if (rc) { 809c103a91eSpeter enderborg pr_warn("%s: security_fs_use(%s) returned %d\n", 810089be43eSJames Morris __func__, sb->s_type->name, rc); 811c9180a57SEric Paris goto out; 812c9180a57SEric Paris } 813eb9ae686SDavid Quigley } 814aad82892SSeth Forshee 815aad82892SSeth Forshee /* 81601593d32SStephen Smalley * If this is a user namespace mount and the filesystem type is not 81701593d32SStephen Smalley * explicitly whitelisted, then no contexts are allowed on the command 81801593d32SStephen Smalley * line and security labels must be ignored. 819aad82892SSeth Forshee */ 82001593d32SStephen Smalley if (sb->s_user_ns != &init_user_ns && 82101593d32SStephen Smalley strcmp(sb->s_type->name, "tmpfs") && 82201593d32SStephen Smalley strcmp(sb->s_type->name, "ramfs") && 82301593d32SStephen Smalley strcmp(sb->s_type->name, "devpts")) { 824aad82892SSeth Forshee if (context_sid || fscontext_sid || rootcontext_sid || 825aad82892SSeth Forshee defcontext_sid) { 826aad82892SSeth Forshee rc = -EACCES; 827aad82892SSeth Forshee goto out; 828aad82892SSeth Forshee } 829aad82892SSeth Forshee if (sbsec->behavior == SECURITY_FS_USE_XATTR) { 830aad82892SSeth Forshee sbsec->behavior = SECURITY_FS_USE_MNTPOINT; 831aa8e712cSStephen Smalley rc = security_transition_sid(&selinux_state, 832aa8e712cSStephen Smalley current_sid(), 833aa8e712cSStephen Smalley current_sid(), 834aad82892SSeth Forshee SECCLASS_FILE, NULL, 835aad82892SSeth Forshee &sbsec->mntpoint_sid); 836aad82892SSeth Forshee if (rc) 837aad82892SSeth Forshee goto out; 838aad82892SSeth Forshee } 839aad82892SSeth Forshee goto out_set_opts; 840aad82892SSeth Forshee } 841aad82892SSeth Forshee 842c9180a57SEric Paris /* sets the context of the superblock for the fs being mounted. */ 843c9180a57SEric Paris if (fscontext_sid) { 844275bb41eSDavid Howells rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred); 845c9180a57SEric Paris if (rc) 846c9180a57SEric Paris goto out; 847c9180a57SEric Paris 848c9180a57SEric Paris sbsec->sid = fscontext_sid; 849c9180a57SEric Paris } 850c9180a57SEric Paris 851c9180a57SEric Paris /* 852c9180a57SEric Paris * Switch to using mount point labeling behavior. 853c9180a57SEric Paris * sets the label used on all file below the mountpoint, and will set 854c9180a57SEric Paris * the superblock context if not already set. 855c9180a57SEric Paris */ 856eb9ae686SDavid Quigley if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) { 857eb9ae686SDavid Quigley sbsec->behavior = SECURITY_FS_USE_NATIVE; 858eb9ae686SDavid Quigley *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS; 859eb9ae686SDavid Quigley } 860eb9ae686SDavid Quigley 861c9180a57SEric Paris if (context_sid) { 862c9180a57SEric Paris if (!fscontext_sid) { 863275bb41eSDavid Howells rc = may_context_mount_sb_relabel(context_sid, sbsec, 864275bb41eSDavid Howells cred); 865c9180a57SEric Paris if (rc) 866c9180a57SEric Paris goto out; 867c9180a57SEric Paris sbsec->sid = context_sid; 868c9180a57SEric Paris } else { 869275bb41eSDavid Howells rc = may_context_mount_inode_relabel(context_sid, sbsec, 870275bb41eSDavid Howells cred); 871c9180a57SEric Paris if (rc) 872c9180a57SEric Paris goto out; 873c9180a57SEric Paris } 874c9180a57SEric Paris if (!rootcontext_sid) 875c9180a57SEric Paris rootcontext_sid = context_sid; 876c9180a57SEric Paris 877c9180a57SEric Paris sbsec->mntpoint_sid = context_sid; 878c9180a57SEric Paris sbsec->behavior = SECURITY_FS_USE_MNTPOINT; 879c9180a57SEric Paris } 880c9180a57SEric Paris 881c9180a57SEric Paris if (rootcontext_sid) { 882275bb41eSDavid Howells rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec, 883275bb41eSDavid Howells cred); 884c9180a57SEric Paris if (rc) 885c9180a57SEric Paris goto out; 886c9180a57SEric Paris 887c9180a57SEric Paris root_isec->sid = rootcontext_sid; 8886f3be9f5SAndreas Gruenbacher root_isec->initialized = LABEL_INITIALIZED; 889c9180a57SEric Paris } 890c9180a57SEric Paris 891c9180a57SEric Paris if (defcontext_sid) { 892eb9ae686SDavid Quigley if (sbsec->behavior != SECURITY_FS_USE_XATTR && 893eb9ae686SDavid Quigley sbsec->behavior != SECURITY_FS_USE_NATIVE) { 894c9180a57SEric Paris rc = -EINVAL; 895c103a91eSpeter enderborg pr_warn("SELinux: defcontext option is " 896c9180a57SEric Paris "invalid for this filesystem type\n"); 897c9180a57SEric Paris goto out; 898c9180a57SEric Paris } 899c9180a57SEric Paris 900c9180a57SEric Paris if (defcontext_sid != sbsec->def_sid) { 901c9180a57SEric Paris rc = may_context_mount_inode_relabel(defcontext_sid, 902275bb41eSDavid Howells sbsec, cred); 903c9180a57SEric Paris if (rc) 904c9180a57SEric Paris goto out; 905c9180a57SEric Paris } 906c9180a57SEric Paris 907c9180a57SEric Paris sbsec->def_sid = defcontext_sid; 908c9180a57SEric Paris } 909c9180a57SEric Paris 910aad82892SSeth Forshee out_set_opts: 911c9180a57SEric Paris rc = sb_finish_set_opts(sb); 912c9180a57SEric Paris out: 913bc7e982bSEric Paris mutex_unlock(&sbsec->lock); 9141da177e4SLinus Torvalds return rc; 915c9180a57SEric Paris out_double_mount: 916c9180a57SEric Paris rc = -EINVAL; 917c103a91eSpeter enderborg pr_warn("SELinux: mount invalid. Same superblock, different " 918bd323655SAl Viro "security settings for (dev %s, type %s)\n", sb->s_id, 919bd323655SAl Viro sb->s_type->name); 920c9180a57SEric Paris goto out; 921c9180a57SEric Paris } 922c9180a57SEric Paris 923094f7b69SJeff Layton static int selinux_cmp_sb_context(const struct super_block *oldsb, 924094f7b69SJeff Layton const struct super_block *newsb) 925094f7b69SJeff Layton { 926094f7b69SJeff Layton struct superblock_security_struct *old = oldsb->s_security; 927094f7b69SJeff Layton struct superblock_security_struct *new = newsb->s_security; 928094f7b69SJeff Layton char oldflags = old->flags & SE_MNTMASK; 929094f7b69SJeff Layton char newflags = new->flags & SE_MNTMASK; 930094f7b69SJeff Layton 931094f7b69SJeff Layton if (oldflags != newflags) 932094f7b69SJeff Layton goto mismatch; 933094f7b69SJeff Layton if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid) 934094f7b69SJeff Layton goto mismatch; 935094f7b69SJeff Layton if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid) 936094f7b69SJeff Layton goto mismatch; 937094f7b69SJeff Layton if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid) 938094f7b69SJeff Layton goto mismatch; 939094f7b69SJeff Layton if (oldflags & ROOTCONTEXT_MNT) { 94083da53c5SAndreas Gruenbacher struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root); 94183da53c5SAndreas Gruenbacher struct inode_security_struct *newroot = backing_inode_security(newsb->s_root); 942094f7b69SJeff Layton if (oldroot->sid != newroot->sid) 943094f7b69SJeff Layton goto mismatch; 944094f7b69SJeff Layton } 945094f7b69SJeff Layton return 0; 946094f7b69SJeff Layton mismatch: 947c103a91eSpeter enderborg pr_warn("SELinux: mount invalid. Same superblock, " 948094f7b69SJeff Layton "different security settings for (dev %s, " 949094f7b69SJeff Layton "type %s)\n", newsb->s_id, newsb->s_type->name); 950094f7b69SJeff Layton return -EBUSY; 951094f7b69SJeff Layton } 952094f7b69SJeff Layton 953094f7b69SJeff Layton static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb, 9540b4d3452SScott Mayhew struct super_block *newsb, 9550b4d3452SScott Mayhew unsigned long kern_flags, 9560b4d3452SScott Mayhew unsigned long *set_kern_flags) 957c9180a57SEric Paris { 9580b4d3452SScott Mayhew int rc = 0; 959c9180a57SEric Paris const struct superblock_security_struct *oldsbsec = oldsb->s_security; 960c9180a57SEric Paris struct superblock_security_struct *newsbsec = newsb->s_security; 961c9180a57SEric Paris 962c9180a57SEric Paris int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT); 963c9180a57SEric Paris int set_context = (oldsbsec->flags & CONTEXT_MNT); 964c9180a57SEric Paris int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT); 965c9180a57SEric Paris 9660f5e6420SEric Paris /* 9670f5e6420SEric Paris * if the parent was able to be mounted it clearly had no special lsm 968e8c26255SAl Viro * mount options. thus we can safely deal with this superblock later 9690f5e6420SEric Paris */ 970aa8e712cSStephen Smalley if (!selinux_state.initialized) 971094f7b69SJeff Layton return 0; 972c9180a57SEric Paris 9730b4d3452SScott Mayhew /* 9740b4d3452SScott Mayhew * Specifying internal flags without providing a place to 9750b4d3452SScott Mayhew * place the results is not allowed. 9760b4d3452SScott Mayhew */ 9770b4d3452SScott Mayhew if (kern_flags && !set_kern_flags) 9780b4d3452SScott Mayhew return -EINVAL; 9790b4d3452SScott Mayhew 980c9180a57SEric Paris /* how can we clone if the old one wasn't set up?? */ 9810d90a7ecSDavid P. Quigley BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED)); 982c9180a57SEric Paris 983094f7b69SJeff Layton /* if fs is reusing a sb, make sure that the contexts match */ 9840d90a7ecSDavid P. Quigley if (newsbsec->flags & SE_SBINITIALIZED) 985094f7b69SJeff Layton return selinux_cmp_sb_context(oldsb, newsb); 9865a552617SEric Paris 987c9180a57SEric Paris mutex_lock(&newsbsec->lock); 988c9180a57SEric Paris 989c9180a57SEric Paris newsbsec->flags = oldsbsec->flags; 990c9180a57SEric Paris 991c9180a57SEric Paris newsbsec->sid = oldsbsec->sid; 992c9180a57SEric Paris newsbsec->def_sid = oldsbsec->def_sid; 993c9180a57SEric Paris newsbsec->behavior = oldsbsec->behavior; 994c9180a57SEric Paris 9950b4d3452SScott Mayhew if (newsbsec->behavior == SECURITY_FS_USE_NATIVE && 9960b4d3452SScott Mayhew !(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) { 997aa8e712cSStephen Smalley rc = security_fs_use(&selinux_state, newsb); 9980b4d3452SScott Mayhew if (rc) 9990b4d3452SScott Mayhew goto out; 10000b4d3452SScott Mayhew } 10010b4d3452SScott Mayhew 10020b4d3452SScott Mayhew if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) { 10030b4d3452SScott Mayhew newsbsec->behavior = SECURITY_FS_USE_NATIVE; 10040b4d3452SScott Mayhew *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS; 10050b4d3452SScott Mayhew } 10060b4d3452SScott Mayhew 1007c9180a57SEric Paris if (set_context) { 1008c9180a57SEric Paris u32 sid = oldsbsec->mntpoint_sid; 1009c9180a57SEric Paris 1010c9180a57SEric Paris if (!set_fscontext) 1011c9180a57SEric Paris newsbsec->sid = sid; 1012c9180a57SEric Paris if (!set_rootcontext) { 101383da53c5SAndreas Gruenbacher struct inode_security_struct *newisec = backing_inode_security(newsb->s_root); 1014c9180a57SEric Paris newisec->sid = sid; 1015c9180a57SEric Paris } 1016c9180a57SEric Paris newsbsec->mntpoint_sid = sid; 1017c9180a57SEric Paris } 1018c9180a57SEric Paris if (set_rootcontext) { 101983da53c5SAndreas Gruenbacher const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root); 102083da53c5SAndreas Gruenbacher struct inode_security_struct *newisec = backing_inode_security(newsb->s_root); 1021c9180a57SEric Paris 1022c9180a57SEric Paris newisec->sid = oldisec->sid; 1023c9180a57SEric Paris } 1024c9180a57SEric Paris 1025c9180a57SEric Paris sb_finish_set_opts(newsb); 10260b4d3452SScott Mayhew out: 1027c9180a57SEric Paris mutex_unlock(&newsbsec->lock); 10280b4d3452SScott Mayhew return rc; 1029c9180a57SEric Paris } 1030c9180a57SEric Paris 1031ba641862SAl Viro static int selinux_add_opt(int token, const char *s, void **mnt_opts) 1032c9180a57SEric Paris { 1033ba641862SAl Viro struct selinux_mnt_opts *opts = *mnt_opts; 1034c9180a57SEric Paris 1035da3d76abSAl Viro if (token == Opt_seclabel) /* eaten and completely ignored */ 1036e0007529SEric Paris return 0; 1037e0007529SEric Paris 1038ba641862SAl Viro if (!opts) { 1039ba641862SAl Viro opts = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL); 1040ba641862SAl Viro if (!opts) 1041ba641862SAl Viro return -ENOMEM; 1042ba641862SAl Viro *mnt_opts = opts; 1043ba641862SAl Viro } 1044ba641862SAl Viro if (!s) 1045ba641862SAl Viro return -ENOMEM; 1046ba641862SAl Viro switch (token) { 1047ba641862SAl Viro case Opt_context: 1048ba641862SAl Viro if (opts->context || opts->defcontext) 1049ba641862SAl Viro goto Einval; 1050ba641862SAl Viro opts->context = s; 1051ba641862SAl Viro break; 1052ba641862SAl Viro case Opt_fscontext: 1053ba641862SAl Viro if (opts->fscontext) 1054ba641862SAl Viro goto Einval; 1055ba641862SAl Viro opts->fscontext = s; 1056ba641862SAl Viro break; 1057ba641862SAl Viro case Opt_rootcontext: 1058ba641862SAl Viro if (opts->rootcontext) 1059ba641862SAl Viro goto Einval; 1060ba641862SAl Viro opts->rootcontext = s; 1061ba641862SAl Viro break; 1062ba641862SAl Viro case Opt_defcontext: 1063ba641862SAl Viro if (opts->context || opts->defcontext) 1064ba641862SAl Viro goto Einval; 1065ba641862SAl Viro opts->defcontext = s; 1066ba641862SAl Viro break; 1067ba641862SAl Viro } 1068ba641862SAl Viro return 0; 1069ba641862SAl Viro Einval: 1070ba641862SAl Viro pr_warn(SEL_MOUNT_FAIL_MSG); 1071ba641862SAl Viro return -EINVAL; 1072ba641862SAl Viro } 1073ba641862SAl Viro 1074757cbe59SAl Viro static int selinux_add_mnt_opt(const char *option, const char *val, int len, 1075204cc0ccSAl Viro void **mnt_opts) 1076c9180a57SEric Paris { 1077757cbe59SAl Viro int token = Opt_error; 1078757cbe59SAl Viro int rc, i; 1079c9180a57SEric Paris 1080757cbe59SAl Viro for (i = 0; i < ARRAY_SIZE(tokens); i++) { 1081757cbe59SAl Viro if (strcmp(option, tokens[i].name) == 0) { 1082757cbe59SAl Viro token = tokens[i].opt; 1083757cbe59SAl Viro break; 1084757cbe59SAl Viro } 1085169d68efSAl Viro } 1086169d68efSAl Viro 1087757cbe59SAl Viro if (token == Opt_error) 1088757cbe59SAl Viro return -EINVAL; 1089c9180a57SEric Paris 1090757cbe59SAl Viro if (token != Opt_seclabel) 1091757cbe59SAl Viro val = kmemdup_nul(val, len, GFP_KERNEL); 1092757cbe59SAl Viro rc = selinux_add_opt(token, val, mnt_opts); 1093757cbe59SAl Viro if (unlikely(rc)) { 1094757cbe59SAl Viro kfree(val); 1095757cbe59SAl Viro if (*mnt_opts) { 1096ba641862SAl Viro selinux_free_mnt_opts(*mnt_opts); 1097ba641862SAl Viro *mnt_opts = NULL; 1098757cbe59SAl Viro } 1099757cbe59SAl Viro } 1100c9180a57SEric Paris return rc; 11011da177e4SLinus Torvalds } 11021da177e4SLinus Torvalds 1103e3489f89SAl Viro static int show_sid(struct seq_file *m, u32 sid) 11042069f457SEric Paris { 1105e3489f89SAl Viro char *context = NULL; 1106e3489f89SAl Viro u32 len; 1107e3489f89SAl Viro int rc; 11082069f457SEric Paris 1109e3489f89SAl Viro rc = security_sid_to_context(&selinux_state, sid, 1110e3489f89SAl Viro &context, &len); 1111e3489f89SAl Viro if (!rc) { 1112e3489f89SAl Viro bool has_comma = context && strchr(context, ','); 111311689d47SDavid P. Quigley 11142069f457SEric Paris if (has_comma) 11152069f457SEric Paris seq_putc(m, '\"'); 1116e3489f89SAl Viro seq_escape(m, context, "\"\n\\"); 11172069f457SEric Paris if (has_comma) 11182069f457SEric Paris seq_putc(m, '\"'); 11192069f457SEric Paris } 11201da177e4SLinus Torvalds kfree(context); 11211da177e4SLinus Torvalds return rc; 11221da177e4SLinus Torvalds } 11232069f457SEric Paris 11242069f457SEric Paris static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb) 11252069f457SEric Paris { 1126e3489f89SAl Viro struct superblock_security_struct *sbsec = sb->s_security; 11272069f457SEric Paris int rc; 11282069f457SEric Paris 1129e3489f89SAl Viro if (!(sbsec->flags & SE_SBINITIALIZED)) 1130e3489f89SAl Viro return 0; 1131e3489f89SAl Viro 1132e3489f89SAl Viro if (!selinux_state.initialized) 1133e3489f89SAl Viro return 0; 1134e3489f89SAl Viro 1135e3489f89SAl Viro if (sbsec->flags & FSCONTEXT_MNT) { 1136e3489f89SAl Viro seq_putc(m, ','); 1137e3489f89SAl Viro seq_puts(m, FSCONTEXT_STR); 1138e3489f89SAl Viro rc = show_sid(m, sbsec->sid); 1139e3489f89SAl Viro if (rc) 11402069f457SEric Paris return rc; 1141383795c2SEric Paris } 1142e3489f89SAl Viro if (sbsec->flags & CONTEXT_MNT) { 1143e3489f89SAl Viro seq_putc(m, ','); 1144e3489f89SAl Viro seq_puts(m, CONTEXT_STR); 1145e3489f89SAl Viro rc = show_sid(m, sbsec->mntpoint_sid); 1146e3489f89SAl Viro if (rc) 11472069f457SEric Paris return rc; 11482069f457SEric Paris } 1149e3489f89SAl Viro if (sbsec->flags & DEFCONTEXT_MNT) { 1150e3489f89SAl Viro seq_putc(m, ','); 1151e3489f89SAl Viro seq_puts(m, DEFCONTEXT_STR); 1152e3489f89SAl Viro rc = show_sid(m, sbsec->def_sid); 1153e3489f89SAl Viro if (rc) 1154e3489f89SAl Viro return rc; 1155e3489f89SAl Viro } 1156e3489f89SAl Viro if (sbsec->flags & ROOTCONTEXT_MNT) { 1157e3489f89SAl Viro struct dentry *root = sbsec->sb->s_root; 1158e3489f89SAl Viro struct inode_security_struct *isec = backing_inode_security(root); 1159e3489f89SAl Viro seq_putc(m, ','); 1160e3489f89SAl Viro seq_puts(m, ROOTCONTEXT_STR); 1161e3489f89SAl Viro rc = show_sid(m, isec->sid); 1162e3489f89SAl Viro if (rc) 1163e3489f89SAl Viro return rc; 1164e3489f89SAl Viro } 1165e3489f89SAl Viro if (sbsec->flags & SBLABEL_MNT) { 1166e3489f89SAl Viro seq_putc(m, ','); 1167e3489f89SAl Viro seq_puts(m, LABELSUPP_STR); 1168e3489f89SAl Viro } 1169e3489f89SAl Viro return 0; 1170e3489f89SAl Viro } 11712069f457SEric Paris 11721da177e4SLinus Torvalds static inline u16 inode_mode_to_security_class(umode_t mode) 11731da177e4SLinus Torvalds { 11741da177e4SLinus Torvalds switch (mode & S_IFMT) { 11751da177e4SLinus Torvalds case S_IFSOCK: 11761da177e4SLinus Torvalds return SECCLASS_SOCK_FILE; 11771da177e4SLinus Torvalds case S_IFLNK: 11781da177e4SLinus Torvalds return SECCLASS_LNK_FILE; 11791da177e4SLinus Torvalds case S_IFREG: 11801da177e4SLinus Torvalds return SECCLASS_FILE; 11811da177e4SLinus Torvalds case S_IFBLK: 11821da177e4SLinus Torvalds return SECCLASS_BLK_FILE; 11831da177e4SLinus Torvalds case S_IFDIR: 11841da177e4SLinus Torvalds return SECCLASS_DIR; 11851da177e4SLinus Torvalds case S_IFCHR: 11861da177e4SLinus Torvalds return SECCLASS_CHR_FILE; 11871da177e4SLinus Torvalds case S_IFIFO: 11881da177e4SLinus Torvalds return SECCLASS_FIFO_FILE; 11891da177e4SLinus Torvalds 11901da177e4SLinus Torvalds } 11911da177e4SLinus Torvalds 11921da177e4SLinus Torvalds return SECCLASS_FILE; 11931da177e4SLinus Torvalds } 11941da177e4SLinus Torvalds 119513402580SJames Morris static inline int default_protocol_stream(int protocol) 119613402580SJames Morris { 119713402580SJames Morris return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP); 119813402580SJames Morris } 119913402580SJames Morris 120013402580SJames Morris static inline int default_protocol_dgram(int protocol) 120113402580SJames Morris { 120213402580SJames Morris return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP); 120313402580SJames Morris } 120413402580SJames Morris 12051da177e4SLinus Torvalds static inline u16 socket_type_to_security_class(int family, int type, int protocol) 12061da177e4SLinus Torvalds { 1207aa8e712cSStephen Smalley int extsockclass = selinux_policycap_extsockclass(); 1208da69a530SStephen Smalley 12091da177e4SLinus Torvalds switch (family) { 12101da177e4SLinus Torvalds case PF_UNIX: 12111da177e4SLinus Torvalds switch (type) { 12121da177e4SLinus Torvalds case SOCK_STREAM: 12131da177e4SLinus Torvalds case SOCK_SEQPACKET: 12141da177e4SLinus Torvalds return SECCLASS_UNIX_STREAM_SOCKET; 12151da177e4SLinus Torvalds case SOCK_DGRAM: 12162a764b52SLuis Ressel case SOCK_RAW: 12171da177e4SLinus Torvalds return SECCLASS_UNIX_DGRAM_SOCKET; 12181da177e4SLinus Torvalds } 12191da177e4SLinus Torvalds break; 12201da177e4SLinus Torvalds case PF_INET: 12211da177e4SLinus Torvalds case PF_INET6: 12221da177e4SLinus Torvalds switch (type) { 12231da177e4SLinus Torvalds case SOCK_STREAM: 1224da69a530SStephen Smalley case SOCK_SEQPACKET: 122513402580SJames Morris if (default_protocol_stream(protocol)) 12261da177e4SLinus Torvalds return SECCLASS_TCP_SOCKET; 1227da69a530SStephen Smalley else if (extsockclass && protocol == IPPROTO_SCTP) 1228da69a530SStephen Smalley return SECCLASS_SCTP_SOCKET; 122913402580SJames Morris else 123013402580SJames Morris return SECCLASS_RAWIP_SOCKET; 12311da177e4SLinus Torvalds case SOCK_DGRAM: 123213402580SJames Morris if (default_protocol_dgram(protocol)) 12331da177e4SLinus Torvalds return SECCLASS_UDP_SOCKET; 1234ef37979aSStephen Smalley else if (extsockclass && (protocol == IPPROTO_ICMP || 1235ef37979aSStephen Smalley protocol == IPPROTO_ICMPV6)) 1236da69a530SStephen Smalley return SECCLASS_ICMP_SOCKET; 123713402580SJames Morris else 123813402580SJames Morris return SECCLASS_RAWIP_SOCKET; 12392ee92d46SJames Morris case SOCK_DCCP: 12402ee92d46SJames Morris return SECCLASS_DCCP_SOCKET; 124113402580SJames Morris default: 12421da177e4SLinus Torvalds return SECCLASS_RAWIP_SOCKET; 12431da177e4SLinus Torvalds } 12441da177e4SLinus Torvalds break; 12451da177e4SLinus Torvalds case PF_NETLINK: 12461da177e4SLinus Torvalds switch (protocol) { 12471da177e4SLinus Torvalds case NETLINK_ROUTE: 12481da177e4SLinus Torvalds return SECCLASS_NETLINK_ROUTE_SOCKET; 12497f1fb60cSPavel Emelyanov case NETLINK_SOCK_DIAG: 12501da177e4SLinus Torvalds return SECCLASS_NETLINK_TCPDIAG_SOCKET; 12511da177e4SLinus Torvalds case NETLINK_NFLOG: 12521da177e4SLinus Torvalds return SECCLASS_NETLINK_NFLOG_SOCKET; 12531da177e4SLinus Torvalds case NETLINK_XFRM: 12541da177e4SLinus Torvalds return SECCLASS_NETLINK_XFRM_SOCKET; 12551da177e4SLinus Torvalds case NETLINK_SELINUX: 12561da177e4SLinus Torvalds return SECCLASS_NETLINK_SELINUX_SOCKET; 12576c6d2e9bSStephen Smalley case NETLINK_ISCSI: 12586c6d2e9bSStephen Smalley return SECCLASS_NETLINK_ISCSI_SOCKET; 12591da177e4SLinus Torvalds case NETLINK_AUDIT: 12601da177e4SLinus Torvalds return SECCLASS_NETLINK_AUDIT_SOCKET; 12616c6d2e9bSStephen Smalley case NETLINK_FIB_LOOKUP: 12626c6d2e9bSStephen Smalley return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET; 12636c6d2e9bSStephen Smalley case NETLINK_CONNECTOR: 12646c6d2e9bSStephen Smalley return SECCLASS_NETLINK_CONNECTOR_SOCKET; 12656c6d2e9bSStephen Smalley case NETLINK_NETFILTER: 12666c6d2e9bSStephen Smalley return SECCLASS_NETLINK_NETFILTER_SOCKET; 12671da177e4SLinus Torvalds case NETLINK_DNRTMSG: 12681da177e4SLinus Torvalds return SECCLASS_NETLINK_DNRT_SOCKET; 12690c9b7942SJames Morris case NETLINK_KOBJECT_UEVENT: 12700c9b7942SJames Morris return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET; 12716c6d2e9bSStephen Smalley case NETLINK_GENERIC: 12726c6d2e9bSStephen Smalley return SECCLASS_NETLINK_GENERIC_SOCKET; 12736c6d2e9bSStephen Smalley case NETLINK_SCSITRANSPORT: 12746c6d2e9bSStephen Smalley return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET; 12756c6d2e9bSStephen Smalley case NETLINK_RDMA: 12766c6d2e9bSStephen Smalley return SECCLASS_NETLINK_RDMA_SOCKET; 12776c6d2e9bSStephen Smalley case NETLINK_CRYPTO: 12786c6d2e9bSStephen Smalley return SECCLASS_NETLINK_CRYPTO_SOCKET; 12791da177e4SLinus Torvalds default: 12801da177e4SLinus Torvalds return SECCLASS_NETLINK_SOCKET; 12811da177e4SLinus Torvalds } 12821da177e4SLinus Torvalds case PF_PACKET: 12831da177e4SLinus Torvalds return SECCLASS_PACKET_SOCKET; 12841da177e4SLinus Torvalds case PF_KEY: 12851da177e4SLinus Torvalds return SECCLASS_KEY_SOCKET; 12863e3ff15eSChristopher J. PeBenito case PF_APPLETALK: 12873e3ff15eSChristopher J. PeBenito return SECCLASS_APPLETALK_SOCKET; 12881da177e4SLinus Torvalds } 12891da177e4SLinus Torvalds 1290da69a530SStephen Smalley if (extsockclass) { 1291da69a530SStephen Smalley switch (family) { 1292da69a530SStephen Smalley case PF_AX25: 1293da69a530SStephen Smalley return SECCLASS_AX25_SOCKET; 1294da69a530SStephen Smalley case PF_IPX: 1295da69a530SStephen Smalley return SECCLASS_IPX_SOCKET; 1296da69a530SStephen Smalley case PF_NETROM: 1297da69a530SStephen Smalley return SECCLASS_NETROM_SOCKET; 1298da69a530SStephen Smalley case PF_ATMPVC: 1299da69a530SStephen Smalley return SECCLASS_ATMPVC_SOCKET; 1300da69a530SStephen Smalley case PF_X25: 1301da69a530SStephen Smalley return SECCLASS_X25_SOCKET; 1302da69a530SStephen Smalley case PF_ROSE: 1303da69a530SStephen Smalley return SECCLASS_ROSE_SOCKET; 1304da69a530SStephen Smalley case PF_DECnet: 1305da69a530SStephen Smalley return SECCLASS_DECNET_SOCKET; 1306da69a530SStephen Smalley case PF_ATMSVC: 1307da69a530SStephen Smalley return SECCLASS_ATMSVC_SOCKET; 1308da69a530SStephen Smalley case PF_RDS: 1309da69a530SStephen Smalley return SECCLASS_RDS_SOCKET; 1310da69a530SStephen Smalley case PF_IRDA: 1311da69a530SStephen Smalley return SECCLASS_IRDA_SOCKET; 1312da69a530SStephen Smalley case PF_PPPOX: 1313da69a530SStephen Smalley return SECCLASS_PPPOX_SOCKET; 1314da69a530SStephen Smalley case PF_LLC: 1315da69a530SStephen Smalley return SECCLASS_LLC_SOCKET; 1316da69a530SStephen Smalley case PF_CAN: 1317da69a530SStephen Smalley return SECCLASS_CAN_SOCKET; 1318da69a530SStephen Smalley case PF_TIPC: 1319da69a530SStephen Smalley return SECCLASS_TIPC_SOCKET; 1320da69a530SStephen Smalley case PF_BLUETOOTH: 1321da69a530SStephen Smalley return SECCLASS_BLUETOOTH_SOCKET; 1322da69a530SStephen Smalley case PF_IUCV: 1323da69a530SStephen Smalley return SECCLASS_IUCV_SOCKET; 1324da69a530SStephen Smalley case PF_RXRPC: 1325da69a530SStephen Smalley return SECCLASS_RXRPC_SOCKET; 1326da69a530SStephen Smalley case PF_ISDN: 1327da69a530SStephen Smalley return SECCLASS_ISDN_SOCKET; 1328da69a530SStephen Smalley case PF_PHONET: 1329da69a530SStephen Smalley return SECCLASS_PHONET_SOCKET; 1330da69a530SStephen Smalley case PF_IEEE802154: 1331da69a530SStephen Smalley return SECCLASS_IEEE802154_SOCKET; 1332da69a530SStephen Smalley case PF_CAIF: 1333da69a530SStephen Smalley return SECCLASS_CAIF_SOCKET; 1334da69a530SStephen Smalley case PF_ALG: 1335da69a530SStephen Smalley return SECCLASS_ALG_SOCKET; 1336da69a530SStephen Smalley case PF_NFC: 1337da69a530SStephen Smalley return SECCLASS_NFC_SOCKET; 1338da69a530SStephen Smalley case PF_VSOCK: 1339da69a530SStephen Smalley return SECCLASS_VSOCK_SOCKET; 1340da69a530SStephen Smalley case PF_KCM: 1341da69a530SStephen Smalley return SECCLASS_KCM_SOCKET; 1342da69a530SStephen Smalley case PF_QIPCRTR: 1343da69a530SStephen Smalley return SECCLASS_QIPCRTR_SOCKET; 13443051bf36SLinus Torvalds case PF_SMC: 13453051bf36SLinus Torvalds return SECCLASS_SMC_SOCKET; 134668e8b849SBjörn Töpel case PF_XDP: 134768e8b849SBjörn Töpel return SECCLASS_XDP_SOCKET; 134868e8b849SBjörn Töpel #if PF_MAX > 45 1349da69a530SStephen Smalley #error New address family defined, please update this function. 1350da69a530SStephen Smalley #endif 1351da69a530SStephen Smalley } 1352da69a530SStephen Smalley } 1353da69a530SStephen Smalley 13541da177e4SLinus Torvalds return SECCLASS_SOCKET; 13551da177e4SLinus Torvalds } 13561da177e4SLinus Torvalds 1357134509d5SStephen Smalley static int selinux_genfs_get_sid(struct dentry *dentry, 13581da177e4SLinus Torvalds u16 tclass, 1359134509d5SStephen Smalley u16 flags, 13601da177e4SLinus Torvalds u32 *sid) 13611da177e4SLinus Torvalds { 13628e6c9693SLucian Adrian Grijincu int rc; 1363fc64005cSAl Viro struct super_block *sb = dentry->d_sb; 13648e6c9693SLucian Adrian Grijincu char *buffer, *path; 13651da177e4SLinus Torvalds 13661da177e4SLinus Torvalds buffer = (char *)__get_free_page(GFP_KERNEL); 13671da177e4SLinus Torvalds if (!buffer) 13681da177e4SLinus Torvalds return -ENOMEM; 13691da177e4SLinus Torvalds 13708e6c9693SLucian Adrian Grijincu path = dentry_path_raw(dentry, buffer, PAGE_SIZE); 13718e6c9693SLucian Adrian Grijincu if (IS_ERR(path)) 13728e6c9693SLucian Adrian Grijincu rc = PTR_ERR(path); 13738e6c9693SLucian Adrian Grijincu else { 1374134509d5SStephen Smalley if (flags & SE_SBPROC) { 13758e6c9693SLucian Adrian Grijincu /* each process gets a /proc/PID/ entry. Strip off the 13768e6c9693SLucian Adrian Grijincu * PID part to get a valid selinux labeling. 13778e6c9693SLucian Adrian Grijincu * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */ 13788e6c9693SLucian Adrian Grijincu while (path[1] >= '0' && path[1] <= '9') { 13798e6c9693SLucian Adrian Grijincu path[1] = '/'; 13808e6c9693SLucian Adrian Grijincu path++; 13811da177e4SLinus Torvalds } 1382134509d5SStephen Smalley } 1383aa8e712cSStephen Smalley rc = security_genfs_sid(&selinux_state, sb->s_type->name, 1384aa8e712cSStephen Smalley path, tclass, sid); 13857bb185edSStephen Smalley if (rc == -ENOENT) { 13867bb185edSStephen Smalley /* No match in policy, mark as unlabeled. */ 13877bb185edSStephen Smalley *sid = SECINITSID_UNLABELED; 13887bb185edSStephen Smalley rc = 0; 13897bb185edSStephen Smalley } 13908e6c9693SLucian Adrian Grijincu } 13911da177e4SLinus Torvalds free_page((unsigned long)buffer); 13921da177e4SLinus Torvalds return rc; 13931da177e4SLinus Torvalds } 13941da177e4SLinus Torvalds 13951da177e4SLinus Torvalds /* The inode's security attributes must be initialized before first use. */ 13961da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry) 13971da177e4SLinus Torvalds { 13981da177e4SLinus Torvalds struct superblock_security_struct *sbsec = NULL; 13991da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 14009287aed2SAndreas Gruenbacher u32 task_sid, sid = 0; 14019287aed2SAndreas Gruenbacher u16 sclass; 14021da177e4SLinus Torvalds struct dentry *dentry; 14031da177e4SLinus Torvalds #define INITCONTEXTLEN 255 14041da177e4SLinus Torvalds char *context = NULL; 14051da177e4SLinus Torvalds unsigned len = 0; 14061da177e4SLinus Torvalds int rc = 0; 14071da177e4SLinus Torvalds 14086f3be9f5SAndreas Gruenbacher if (isec->initialized == LABEL_INITIALIZED) 140913457d07SAndreas Gruenbacher return 0; 14101da177e4SLinus Torvalds 14119287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 14126f3be9f5SAndreas Gruenbacher if (isec->initialized == LABEL_INITIALIZED) 141323970741SEric Paris goto out_unlock; 14141da177e4SLinus Torvalds 141513457d07SAndreas Gruenbacher if (isec->sclass == SECCLASS_FILE) 141613457d07SAndreas Gruenbacher isec->sclass = inode_mode_to_security_class(inode->i_mode); 141713457d07SAndreas Gruenbacher 14181da177e4SLinus Torvalds sbsec = inode->i_sb->s_security; 14190d90a7ecSDavid P. Quigley if (!(sbsec->flags & SE_SBINITIALIZED)) { 14201da177e4SLinus Torvalds /* Defer initialization until selinux_complete_init, 14211da177e4SLinus Torvalds after the initial policy is loaded and the security 14221da177e4SLinus Torvalds server is ready to handle calls. */ 14231da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 14241da177e4SLinus Torvalds if (list_empty(&isec->list)) 14251da177e4SLinus Torvalds list_add(&isec->list, &sbsec->isec_head); 14261da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 142723970741SEric Paris goto out_unlock; 14281da177e4SLinus Torvalds } 14291da177e4SLinus Torvalds 14309287aed2SAndreas Gruenbacher sclass = isec->sclass; 14319287aed2SAndreas Gruenbacher task_sid = isec->task_sid; 14329287aed2SAndreas Gruenbacher sid = isec->sid; 14339287aed2SAndreas Gruenbacher isec->initialized = LABEL_PENDING; 14349287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 14359287aed2SAndreas Gruenbacher 14361da177e4SLinus Torvalds switch (sbsec->behavior) { 1437eb9ae686SDavid Quigley case SECURITY_FS_USE_NATIVE: 1438eb9ae686SDavid Quigley break; 14391da177e4SLinus Torvalds case SECURITY_FS_USE_XATTR: 14405d6c3191SAndreas Gruenbacher if (!(inode->i_opflags & IOP_XATTR)) { 14419287aed2SAndreas Gruenbacher sid = sbsec->def_sid; 14421da177e4SLinus Torvalds break; 14431da177e4SLinus Torvalds } 14441da177e4SLinus Torvalds /* Need a dentry, since the xattr API requires one. 14451da177e4SLinus Torvalds Life would be simpler if we could just pass the inode. */ 14461da177e4SLinus Torvalds if (opt_dentry) { 14471da177e4SLinus Torvalds /* Called from d_instantiate or d_splice_alias. */ 14481da177e4SLinus Torvalds dentry = dget(opt_dentry); 14491da177e4SLinus Torvalds } else { 1450b127125dSAl Viro /* 1451b127125dSAl Viro * Called from selinux_complete_init, try to find a dentry. 1452b127125dSAl Viro * Some filesystems really want a connected one, so try 1453b127125dSAl Viro * that first. We could split SECURITY_FS_USE_XATTR in 1454b127125dSAl Viro * two, depending upon that... 1455b127125dSAl Viro */ 14561da177e4SLinus Torvalds dentry = d_find_alias(inode); 1457b127125dSAl Viro if (!dentry) 1458b127125dSAl Viro dentry = d_find_any_alias(inode); 14591da177e4SLinus Torvalds } 14601da177e4SLinus Torvalds if (!dentry) { 1461df7f54c0SEric Paris /* 1462df7f54c0SEric Paris * this is can be hit on boot when a file is accessed 1463df7f54c0SEric Paris * before the policy is loaded. When we load policy we 1464df7f54c0SEric Paris * may find inodes that have no dentry on the 1465df7f54c0SEric Paris * sbsec->isec_head list. No reason to complain as these 1466df7f54c0SEric Paris * will get fixed up the next time we go through 1467df7f54c0SEric Paris * inode_doinit with a dentry, before these inodes could 1468df7f54c0SEric Paris * be used again by userspace. 1469df7f54c0SEric Paris */ 14709287aed2SAndreas Gruenbacher goto out; 14711da177e4SLinus Torvalds } 14721da177e4SLinus Torvalds 14731da177e4SLinus Torvalds len = INITCONTEXTLEN; 14744cb912f1SEric Paris context = kmalloc(len+1, GFP_NOFS); 14751da177e4SLinus Torvalds if (!context) { 14761da177e4SLinus Torvalds rc = -ENOMEM; 14771da177e4SLinus Torvalds dput(dentry); 14789287aed2SAndreas Gruenbacher goto out; 14791da177e4SLinus Torvalds } 14804cb912f1SEric Paris context[len] = '\0'; 14815d6c3191SAndreas Gruenbacher rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len); 14821da177e4SLinus Torvalds if (rc == -ERANGE) { 1483314dabb8SJames Morris kfree(context); 1484314dabb8SJames Morris 14851da177e4SLinus Torvalds /* Need a larger buffer. Query for the right size. */ 14865d6c3191SAndreas Gruenbacher rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0); 14871da177e4SLinus Torvalds if (rc < 0) { 14881da177e4SLinus Torvalds dput(dentry); 14899287aed2SAndreas Gruenbacher goto out; 14901da177e4SLinus Torvalds } 14911da177e4SLinus Torvalds len = rc; 14924cb912f1SEric Paris context = kmalloc(len+1, GFP_NOFS); 14931da177e4SLinus Torvalds if (!context) { 14941da177e4SLinus Torvalds rc = -ENOMEM; 14951da177e4SLinus Torvalds dput(dentry); 14969287aed2SAndreas Gruenbacher goto out; 14971da177e4SLinus Torvalds } 14984cb912f1SEric Paris context[len] = '\0'; 14995d6c3191SAndreas Gruenbacher rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len); 15001da177e4SLinus Torvalds } 15011da177e4SLinus Torvalds dput(dentry); 15021da177e4SLinus Torvalds if (rc < 0) { 15031da177e4SLinus Torvalds if (rc != -ENODATA) { 1504c103a91eSpeter enderborg pr_warn("SELinux: %s: getxattr returned " 1505dd6f953aSHarvey Harrison "%d for dev=%s ino=%ld\n", __func__, 15061da177e4SLinus Torvalds -rc, inode->i_sb->s_id, inode->i_ino); 15071da177e4SLinus Torvalds kfree(context); 15089287aed2SAndreas Gruenbacher goto out; 15091da177e4SLinus Torvalds } 15101da177e4SLinus Torvalds /* Map ENODATA to the default file SID */ 15111da177e4SLinus Torvalds sid = sbsec->def_sid; 15121da177e4SLinus Torvalds rc = 0; 15131da177e4SLinus Torvalds } else { 1514aa8e712cSStephen Smalley rc = security_context_to_sid_default(&selinux_state, 1515aa8e712cSStephen Smalley context, rc, &sid, 1516869ab514SStephen Smalley sbsec->def_sid, 1517869ab514SStephen Smalley GFP_NOFS); 15181da177e4SLinus Torvalds if (rc) { 15194ba0a8adSEric Paris char *dev = inode->i_sb->s_id; 15204ba0a8adSEric Paris unsigned long ino = inode->i_ino; 15214ba0a8adSEric Paris 15224ba0a8adSEric Paris if (rc == -EINVAL) { 15234ba0a8adSEric Paris if (printk_ratelimit()) 1524c103a91eSpeter enderborg pr_notice("SELinux: inode=%lu on dev=%s was found to have an invalid " 15254ba0a8adSEric Paris "context=%s. This indicates you may need to relabel the inode or the " 15264ba0a8adSEric Paris "filesystem in question.\n", ino, dev, context); 15274ba0a8adSEric Paris } else { 1528c103a91eSpeter enderborg pr_warn("SELinux: %s: context_to_sid(%s) " 15291da177e4SLinus Torvalds "returned %d for dev=%s ino=%ld\n", 15304ba0a8adSEric Paris __func__, context, -rc, dev, ino); 15314ba0a8adSEric Paris } 15321da177e4SLinus Torvalds kfree(context); 15331da177e4SLinus Torvalds /* Leave with the unlabeled SID */ 15341da177e4SLinus Torvalds rc = 0; 15351da177e4SLinus Torvalds break; 15361da177e4SLinus Torvalds } 15371da177e4SLinus Torvalds } 15381da177e4SLinus Torvalds kfree(context); 15391da177e4SLinus Torvalds break; 15401da177e4SLinus Torvalds case SECURITY_FS_USE_TASK: 15419287aed2SAndreas Gruenbacher sid = task_sid; 15421da177e4SLinus Torvalds break; 15431da177e4SLinus Torvalds case SECURITY_FS_USE_TRANS: 15441da177e4SLinus Torvalds /* Default to the fs SID. */ 15459287aed2SAndreas Gruenbacher sid = sbsec->sid; 15461da177e4SLinus Torvalds 15471da177e4SLinus Torvalds /* Try to obtain a transition SID. */ 1548aa8e712cSStephen Smalley rc = security_transition_sid(&selinux_state, task_sid, sid, 1549aa8e712cSStephen Smalley sclass, NULL, &sid); 15501da177e4SLinus Torvalds if (rc) 15519287aed2SAndreas Gruenbacher goto out; 15521da177e4SLinus Torvalds break; 1553c312feb2SEric Paris case SECURITY_FS_USE_MNTPOINT: 15549287aed2SAndreas Gruenbacher sid = sbsec->mntpoint_sid; 1555c312feb2SEric Paris break; 15561da177e4SLinus Torvalds default: 1557c312feb2SEric Paris /* Default to the fs superblock SID. */ 15589287aed2SAndreas Gruenbacher sid = sbsec->sid; 15591da177e4SLinus Torvalds 1560134509d5SStephen Smalley if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) { 1561f64410ecSPaul Moore /* We must have a dentry to determine the label on 1562f64410ecSPaul Moore * procfs inodes */ 1563b127125dSAl Viro if (opt_dentry) { 1564f64410ecSPaul Moore /* Called from d_instantiate or 1565f64410ecSPaul Moore * d_splice_alias. */ 1566f64410ecSPaul Moore dentry = dget(opt_dentry); 1567b127125dSAl Viro } else { 1568f64410ecSPaul Moore /* Called from selinux_complete_init, try to 1569b127125dSAl Viro * find a dentry. Some filesystems really want 1570b127125dSAl Viro * a connected one, so try that first. 1571b127125dSAl Viro */ 1572f64410ecSPaul Moore dentry = d_find_alias(inode); 1573b127125dSAl Viro if (!dentry) 1574b127125dSAl Viro dentry = d_find_any_alias(inode); 1575b127125dSAl Viro } 1576f64410ecSPaul Moore /* 1577f64410ecSPaul Moore * This can be hit on boot when a file is accessed 1578f64410ecSPaul Moore * before the policy is loaded. When we load policy we 1579f64410ecSPaul Moore * may find inodes that have no dentry on the 1580f64410ecSPaul Moore * sbsec->isec_head list. No reason to complain as 1581f64410ecSPaul Moore * these will get fixed up the next time we go through 1582f64410ecSPaul Moore * inode_doinit() with a dentry, before these inodes 1583f64410ecSPaul Moore * could be used again by userspace. 1584f64410ecSPaul Moore */ 1585f64410ecSPaul Moore if (!dentry) 15869287aed2SAndreas Gruenbacher goto out; 15879287aed2SAndreas Gruenbacher rc = selinux_genfs_get_sid(dentry, sclass, 1588134509d5SStephen Smalley sbsec->flags, &sid); 1589f64410ecSPaul Moore dput(dentry); 15901da177e4SLinus Torvalds if (rc) 15919287aed2SAndreas Gruenbacher goto out; 15921da177e4SLinus Torvalds } 15931da177e4SLinus Torvalds break; 15941da177e4SLinus Torvalds } 15951da177e4SLinus Torvalds 15969287aed2SAndreas Gruenbacher out: 15979287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 15989287aed2SAndreas Gruenbacher if (isec->initialized == LABEL_PENDING) { 15999287aed2SAndreas Gruenbacher if (!sid || rc) { 16009287aed2SAndreas Gruenbacher isec->initialized = LABEL_INVALID; 16019287aed2SAndreas Gruenbacher goto out_unlock; 16029287aed2SAndreas Gruenbacher } 16039287aed2SAndreas Gruenbacher 16046f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 16059287aed2SAndreas Gruenbacher isec->sid = sid; 16069287aed2SAndreas Gruenbacher } 16071da177e4SLinus Torvalds 160823970741SEric Paris out_unlock: 16099287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 16101da177e4SLinus Torvalds return rc; 16111da177e4SLinus Torvalds } 16121da177e4SLinus Torvalds 16131da177e4SLinus Torvalds /* Convert a Linux signal to an access vector. */ 16141da177e4SLinus Torvalds static inline u32 signal_to_av(int sig) 16151da177e4SLinus Torvalds { 16161da177e4SLinus Torvalds u32 perm = 0; 16171da177e4SLinus Torvalds 16181da177e4SLinus Torvalds switch (sig) { 16191da177e4SLinus Torvalds case SIGCHLD: 16201da177e4SLinus Torvalds /* Commonly granted from child to parent. */ 16211da177e4SLinus Torvalds perm = PROCESS__SIGCHLD; 16221da177e4SLinus Torvalds break; 16231da177e4SLinus Torvalds case SIGKILL: 16241da177e4SLinus Torvalds /* Cannot be caught or ignored */ 16251da177e4SLinus Torvalds perm = PROCESS__SIGKILL; 16261da177e4SLinus Torvalds break; 16271da177e4SLinus Torvalds case SIGSTOP: 16281da177e4SLinus Torvalds /* Cannot be caught or ignored */ 16291da177e4SLinus Torvalds perm = PROCESS__SIGSTOP; 16301da177e4SLinus Torvalds break; 16311da177e4SLinus Torvalds default: 16321da177e4SLinus Torvalds /* All other signals. */ 16331da177e4SLinus Torvalds perm = PROCESS__SIGNAL; 16341da177e4SLinus Torvalds break; 16351da177e4SLinus Torvalds } 16361da177e4SLinus Torvalds 16371da177e4SLinus Torvalds return perm; 16381da177e4SLinus Torvalds } 16391da177e4SLinus Torvalds 1640b68e418cSStephen Smalley #if CAP_LAST_CAP > 63 1641b68e418cSStephen Smalley #error Fix SELinux to handle capabilities > 63. 1642b68e418cSStephen Smalley #endif 1643b68e418cSStephen Smalley 16441da177e4SLinus Torvalds /* Check whether a task is allowed to use a capability. */ 16456a9de491SEric Paris static int cred_has_capability(const struct cred *cred, 16468e4ff6f2SStephen Smalley int cap, int audit, bool initns) 16471da177e4SLinus Torvalds { 16482bf49690SThomas Liu struct common_audit_data ad; 164906112163SEric Paris struct av_decision avd; 1650b68e418cSStephen Smalley u16 sclass; 16513699c53cSDavid Howells u32 sid = cred_sid(cred); 1652b68e418cSStephen Smalley u32 av = CAP_TO_MASK(cap); 165306112163SEric Paris int rc; 16541da177e4SLinus Torvalds 165550c205f5SEric Paris ad.type = LSM_AUDIT_DATA_CAP; 16561da177e4SLinus Torvalds ad.u.cap = cap; 16571da177e4SLinus Torvalds 1658b68e418cSStephen Smalley switch (CAP_TO_INDEX(cap)) { 1659b68e418cSStephen Smalley case 0: 16608e4ff6f2SStephen Smalley sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS; 1661b68e418cSStephen Smalley break; 1662b68e418cSStephen Smalley case 1: 16638e4ff6f2SStephen Smalley sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS; 1664b68e418cSStephen Smalley break; 1665b68e418cSStephen Smalley default: 1666c103a91eSpeter enderborg pr_err("SELinux: out of range capability %d\n", cap); 1667b68e418cSStephen Smalley BUG(); 1668a35c6c83SEric Paris return -EINVAL; 1669b68e418cSStephen Smalley } 167006112163SEric Paris 16716b6bc620SStephen Smalley rc = avc_has_perm_noaudit(&selinux_state, 16726b6bc620SStephen Smalley sid, sid, sclass, av, 0, &avd); 16739ade0cf4SEric Paris if (audit == SECURITY_CAP_AUDIT) { 16746b6bc620SStephen Smalley int rc2 = avc_audit(&selinux_state, 16756b6bc620SStephen Smalley sid, sid, sclass, av, &avd, rc, &ad, 0); 16769ade0cf4SEric Paris if (rc2) 16779ade0cf4SEric Paris return rc2; 16789ade0cf4SEric Paris } 167906112163SEric Paris return rc; 16801da177e4SLinus Torvalds } 16811da177e4SLinus Torvalds 16821da177e4SLinus Torvalds /* Check whether a task has a particular permission to an inode. 16831da177e4SLinus Torvalds The 'adp' parameter is optional and allows other audit 16841da177e4SLinus Torvalds data to be passed (e.g. the dentry). */ 168588e67f3bSDavid Howells static int inode_has_perm(const struct cred *cred, 16861da177e4SLinus Torvalds struct inode *inode, 16871da177e4SLinus Torvalds u32 perms, 168819e49834SLinus Torvalds struct common_audit_data *adp) 16891da177e4SLinus Torvalds { 16901da177e4SLinus Torvalds struct inode_security_struct *isec; 1691275bb41eSDavid Howells u32 sid; 16921da177e4SLinus Torvalds 1693e0e81739SDavid Howells validate_creds(cred); 1694e0e81739SDavid Howells 1695bbaca6c2SStephen Smalley if (unlikely(IS_PRIVATE(inode))) 1696bbaca6c2SStephen Smalley return 0; 1697bbaca6c2SStephen Smalley 169888e67f3bSDavid Howells sid = cred_sid(cred); 16991da177e4SLinus Torvalds isec = inode->i_security; 17001da177e4SLinus Torvalds 17016b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 17026b6bc620SStephen Smalley sid, isec->sid, isec->sclass, perms, adp); 17031da177e4SLinus Torvalds } 17041da177e4SLinus Torvalds 17051da177e4SLinus Torvalds /* Same as inode_has_perm, but pass explicit audit data containing 17061da177e4SLinus Torvalds the dentry to help the auditing code to more easily generate the 17071da177e4SLinus Torvalds pathname if needed. */ 170888e67f3bSDavid Howells static inline int dentry_has_perm(const struct cred *cred, 17091da177e4SLinus Torvalds struct dentry *dentry, 17101da177e4SLinus Torvalds u32 av) 17111da177e4SLinus Torvalds { 1712c6f493d6SDavid Howells struct inode *inode = d_backing_inode(dentry); 17132bf49690SThomas Liu struct common_audit_data ad; 171488e67f3bSDavid Howells 171550c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 17162875fa00SEric Paris ad.u.dentry = dentry; 17175d226df4SAndreas Gruenbacher __inode_security_revalidate(inode, dentry, true); 171819e49834SLinus Torvalds return inode_has_perm(cred, inode, av, &ad); 17192875fa00SEric Paris } 17202875fa00SEric Paris 17212875fa00SEric Paris /* Same as inode_has_perm, but pass explicit audit data containing 17222875fa00SEric Paris the path to help the auditing code to more easily generate the 17232875fa00SEric Paris pathname if needed. */ 17242875fa00SEric Paris static inline int path_has_perm(const struct cred *cred, 17253f7036a0SAl Viro const struct path *path, 17262875fa00SEric Paris u32 av) 17272875fa00SEric Paris { 1728c6f493d6SDavid Howells struct inode *inode = d_backing_inode(path->dentry); 17292875fa00SEric Paris struct common_audit_data ad; 17302875fa00SEric Paris 173150c205f5SEric Paris ad.type = LSM_AUDIT_DATA_PATH; 17322875fa00SEric Paris ad.u.path = *path; 17335d226df4SAndreas Gruenbacher __inode_security_revalidate(inode, path->dentry, true); 173419e49834SLinus Torvalds return inode_has_perm(cred, inode, av, &ad); 17351da177e4SLinus Torvalds } 17361da177e4SLinus Torvalds 173713f8e981SDavid Howells /* Same as path_has_perm, but uses the inode from the file struct. */ 173813f8e981SDavid Howells static inline int file_path_has_perm(const struct cred *cred, 173913f8e981SDavid Howells struct file *file, 174013f8e981SDavid Howells u32 av) 174113f8e981SDavid Howells { 174213f8e981SDavid Howells struct common_audit_data ad; 174313f8e981SDavid Howells 174443af5de7SVivek Goyal ad.type = LSM_AUDIT_DATA_FILE; 174543af5de7SVivek Goyal ad.u.file = file; 174619e49834SLinus Torvalds return inode_has_perm(cred, file_inode(file), av, &ad); 174713f8e981SDavid Howells } 174813f8e981SDavid Howells 1749f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL 1750f66e448cSChenbo Feng static int bpf_fd_pass(struct file *file, u32 sid); 1751f66e448cSChenbo Feng #endif 1752f66e448cSChenbo Feng 17531da177e4SLinus Torvalds /* Check whether a task can use an open file descriptor to 17541da177e4SLinus Torvalds access an inode in a given way. Check access to the 17551da177e4SLinus Torvalds descriptor itself, and then use dentry_has_perm to 17561da177e4SLinus Torvalds check a particular permission to the file. 17571da177e4SLinus Torvalds Access to the descriptor is implicitly granted if it 17581da177e4SLinus Torvalds has the same SID as the process. If av is zero, then 17591da177e4SLinus Torvalds access to the file is not checked, e.g. for cases 17601da177e4SLinus Torvalds where only the descriptor is affected like seek. */ 176188e67f3bSDavid Howells static int file_has_perm(const struct cred *cred, 17621da177e4SLinus Torvalds struct file *file, 17631da177e4SLinus Torvalds u32 av) 17641da177e4SLinus Torvalds { 17651da177e4SLinus Torvalds struct file_security_struct *fsec = file->f_security; 1766496ad9aaSAl Viro struct inode *inode = file_inode(file); 17672bf49690SThomas Liu struct common_audit_data ad; 176888e67f3bSDavid Howells u32 sid = cred_sid(cred); 17691da177e4SLinus Torvalds int rc; 17701da177e4SLinus Torvalds 177143af5de7SVivek Goyal ad.type = LSM_AUDIT_DATA_FILE; 177243af5de7SVivek Goyal ad.u.file = file; 17731da177e4SLinus Torvalds 1774275bb41eSDavid Howells if (sid != fsec->sid) { 17756b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 17766b6bc620SStephen Smalley sid, fsec->sid, 17771da177e4SLinus Torvalds SECCLASS_FD, 17781da177e4SLinus Torvalds FD__USE, 17791da177e4SLinus Torvalds &ad); 17801da177e4SLinus Torvalds if (rc) 178188e67f3bSDavid Howells goto out; 17821da177e4SLinus Torvalds } 17831da177e4SLinus Torvalds 1784f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL 1785f66e448cSChenbo Feng rc = bpf_fd_pass(file, cred_sid(cred)); 1786f66e448cSChenbo Feng if (rc) 1787f66e448cSChenbo Feng return rc; 1788f66e448cSChenbo Feng #endif 1789f66e448cSChenbo Feng 17901da177e4SLinus Torvalds /* av is zero if only checking access to the descriptor. */ 179188e67f3bSDavid Howells rc = 0; 17921da177e4SLinus Torvalds if (av) 179319e49834SLinus Torvalds rc = inode_has_perm(cred, inode, av, &ad); 17941da177e4SLinus Torvalds 179588e67f3bSDavid Howells out: 179688e67f3bSDavid Howells return rc; 17971da177e4SLinus Torvalds } 17981da177e4SLinus Torvalds 1799c3c188b2SDavid Howells /* 1800c3c188b2SDavid Howells * Determine the label for an inode that might be unioned. 1801c3c188b2SDavid Howells */ 1802c957f6dfSVivek Goyal static int 1803c957f6dfSVivek Goyal selinux_determine_inode_label(const struct task_security_struct *tsec, 1804c957f6dfSVivek Goyal struct inode *dir, 1805c957f6dfSVivek Goyal const struct qstr *name, u16 tclass, 1806c3c188b2SDavid Howells u32 *_new_isid) 1807c3c188b2SDavid Howells { 1808c3c188b2SDavid Howells const struct superblock_security_struct *sbsec = dir->i_sb->s_security; 1809c3c188b2SDavid Howells 1810c3c188b2SDavid Howells if ((sbsec->flags & SE_SBINITIALIZED) && 1811c3c188b2SDavid Howells (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) { 1812c3c188b2SDavid Howells *_new_isid = sbsec->mntpoint_sid; 1813c3c188b2SDavid Howells } else if ((sbsec->flags & SBLABEL_MNT) && 1814c3c188b2SDavid Howells tsec->create_sid) { 1815c3c188b2SDavid Howells *_new_isid = tsec->create_sid; 1816c3c188b2SDavid Howells } else { 181720cdef8dSPaul Moore const struct inode_security_struct *dsec = inode_security(dir); 1818aa8e712cSStephen Smalley return security_transition_sid(&selinux_state, tsec->sid, 1819aa8e712cSStephen Smalley dsec->sid, tclass, 1820c3c188b2SDavid Howells name, _new_isid); 1821c3c188b2SDavid Howells } 1822c3c188b2SDavid Howells 1823c3c188b2SDavid Howells return 0; 1824c3c188b2SDavid Howells } 1825c3c188b2SDavid Howells 18261da177e4SLinus Torvalds /* Check whether a task can create a file. */ 18271da177e4SLinus Torvalds static int may_create(struct inode *dir, 18281da177e4SLinus Torvalds struct dentry *dentry, 18291da177e4SLinus Torvalds u16 tclass) 18301da177e4SLinus Torvalds { 18315fb49870SPaul Moore const struct task_security_struct *tsec = current_security(); 18321da177e4SLinus Torvalds struct inode_security_struct *dsec; 18331da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 1834275bb41eSDavid Howells u32 sid, newsid; 18352bf49690SThomas Liu struct common_audit_data ad; 18361da177e4SLinus Torvalds int rc; 18371da177e4SLinus Torvalds 183883da53c5SAndreas Gruenbacher dsec = inode_security(dir); 18391da177e4SLinus Torvalds sbsec = dir->i_sb->s_security; 18401da177e4SLinus Torvalds 1841275bb41eSDavid Howells sid = tsec->sid; 1842275bb41eSDavid Howells 184350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 1844a269434dSEric Paris ad.u.dentry = dentry; 18451da177e4SLinus Torvalds 18466b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 18476b6bc620SStephen Smalley sid, dsec->sid, SECCLASS_DIR, 18481da177e4SLinus Torvalds DIR__ADD_NAME | DIR__SEARCH, 18491da177e4SLinus Torvalds &ad); 18501da177e4SLinus Torvalds if (rc) 18511da177e4SLinus Torvalds return rc; 18521da177e4SLinus Torvalds 1853c957f6dfSVivek Goyal rc = selinux_determine_inode_label(current_security(), dir, 1854c957f6dfSVivek Goyal &dentry->d_name, tclass, &newsid); 18551da177e4SLinus Torvalds if (rc) 18561da177e4SLinus Torvalds return rc; 18571da177e4SLinus Torvalds 18586b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 18596b6bc620SStephen Smalley sid, newsid, tclass, FILE__CREATE, &ad); 18601da177e4SLinus Torvalds if (rc) 18611da177e4SLinus Torvalds return rc; 18621da177e4SLinus Torvalds 18636b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 18646b6bc620SStephen Smalley newsid, sbsec->sid, 18651da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 18661da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, &ad); 18671da177e4SLinus Torvalds } 18681da177e4SLinus Torvalds 18691da177e4SLinus Torvalds #define MAY_LINK 0 18701da177e4SLinus Torvalds #define MAY_UNLINK 1 18711da177e4SLinus Torvalds #define MAY_RMDIR 2 18721da177e4SLinus Torvalds 18731da177e4SLinus Torvalds /* Check whether a task can link, unlink, or rmdir a file/directory. */ 18741da177e4SLinus Torvalds static int may_link(struct inode *dir, 18751da177e4SLinus Torvalds struct dentry *dentry, 18761da177e4SLinus Torvalds int kind) 18771da177e4SLinus Torvalds 18781da177e4SLinus Torvalds { 18791da177e4SLinus Torvalds struct inode_security_struct *dsec, *isec; 18802bf49690SThomas Liu struct common_audit_data ad; 1881275bb41eSDavid Howells u32 sid = current_sid(); 18821da177e4SLinus Torvalds u32 av; 18831da177e4SLinus Torvalds int rc; 18841da177e4SLinus Torvalds 188583da53c5SAndreas Gruenbacher dsec = inode_security(dir); 188683da53c5SAndreas Gruenbacher isec = backing_inode_security(dentry); 18871da177e4SLinus Torvalds 188850c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 1889a269434dSEric Paris ad.u.dentry = dentry; 18901da177e4SLinus Torvalds 18911da177e4SLinus Torvalds av = DIR__SEARCH; 18921da177e4SLinus Torvalds av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME); 18936b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 18946b6bc620SStephen Smalley sid, dsec->sid, SECCLASS_DIR, av, &ad); 18951da177e4SLinus Torvalds if (rc) 18961da177e4SLinus Torvalds return rc; 18971da177e4SLinus Torvalds 18981da177e4SLinus Torvalds switch (kind) { 18991da177e4SLinus Torvalds case MAY_LINK: 19001da177e4SLinus Torvalds av = FILE__LINK; 19011da177e4SLinus Torvalds break; 19021da177e4SLinus Torvalds case MAY_UNLINK: 19031da177e4SLinus Torvalds av = FILE__UNLINK; 19041da177e4SLinus Torvalds break; 19051da177e4SLinus Torvalds case MAY_RMDIR: 19061da177e4SLinus Torvalds av = DIR__RMDIR; 19071da177e4SLinus Torvalds break; 19081da177e4SLinus Torvalds default: 1909c103a91eSpeter enderborg pr_warn("SELinux: %s: unrecognized kind %d\n", 1910744ba35eSEric Paris __func__, kind); 19111da177e4SLinus Torvalds return 0; 19121da177e4SLinus Torvalds } 19131da177e4SLinus Torvalds 19146b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 19156b6bc620SStephen Smalley sid, isec->sid, isec->sclass, av, &ad); 19161da177e4SLinus Torvalds return rc; 19171da177e4SLinus Torvalds } 19181da177e4SLinus Torvalds 19191da177e4SLinus Torvalds static inline int may_rename(struct inode *old_dir, 19201da177e4SLinus Torvalds struct dentry *old_dentry, 19211da177e4SLinus Torvalds struct inode *new_dir, 19221da177e4SLinus Torvalds struct dentry *new_dentry) 19231da177e4SLinus Torvalds { 19241da177e4SLinus Torvalds struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec; 19252bf49690SThomas Liu struct common_audit_data ad; 1926275bb41eSDavid Howells u32 sid = current_sid(); 19271da177e4SLinus Torvalds u32 av; 19281da177e4SLinus Torvalds int old_is_dir, new_is_dir; 19291da177e4SLinus Torvalds int rc; 19301da177e4SLinus Torvalds 193183da53c5SAndreas Gruenbacher old_dsec = inode_security(old_dir); 193283da53c5SAndreas Gruenbacher old_isec = backing_inode_security(old_dentry); 1933e36cb0b8SDavid Howells old_is_dir = d_is_dir(old_dentry); 193483da53c5SAndreas Gruenbacher new_dsec = inode_security(new_dir); 19351da177e4SLinus Torvalds 193650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 19371da177e4SLinus Torvalds 1938a269434dSEric Paris ad.u.dentry = old_dentry; 19396b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 19406b6bc620SStephen Smalley sid, old_dsec->sid, SECCLASS_DIR, 19411da177e4SLinus Torvalds DIR__REMOVE_NAME | DIR__SEARCH, &ad); 19421da177e4SLinus Torvalds if (rc) 19431da177e4SLinus Torvalds return rc; 19446b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 19456b6bc620SStephen Smalley sid, old_isec->sid, 19461da177e4SLinus Torvalds old_isec->sclass, FILE__RENAME, &ad); 19471da177e4SLinus Torvalds if (rc) 19481da177e4SLinus Torvalds return rc; 19491da177e4SLinus Torvalds if (old_is_dir && new_dir != old_dir) { 19506b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 19516b6bc620SStephen Smalley sid, old_isec->sid, 19521da177e4SLinus Torvalds old_isec->sclass, DIR__REPARENT, &ad); 19531da177e4SLinus Torvalds if (rc) 19541da177e4SLinus Torvalds return rc; 19551da177e4SLinus Torvalds } 19561da177e4SLinus Torvalds 1957a269434dSEric Paris ad.u.dentry = new_dentry; 19581da177e4SLinus Torvalds av = DIR__ADD_NAME | DIR__SEARCH; 19592c616d4dSDavid Howells if (d_is_positive(new_dentry)) 19601da177e4SLinus Torvalds av |= DIR__REMOVE_NAME; 19616b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 19626b6bc620SStephen Smalley sid, new_dsec->sid, SECCLASS_DIR, av, &ad); 19631da177e4SLinus Torvalds if (rc) 19641da177e4SLinus Torvalds return rc; 19652c616d4dSDavid Howells if (d_is_positive(new_dentry)) { 196683da53c5SAndreas Gruenbacher new_isec = backing_inode_security(new_dentry); 1967e36cb0b8SDavid Howells new_is_dir = d_is_dir(new_dentry); 19686b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 19696b6bc620SStephen Smalley sid, new_isec->sid, 19701da177e4SLinus Torvalds new_isec->sclass, 19711da177e4SLinus Torvalds (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad); 19721da177e4SLinus Torvalds if (rc) 19731da177e4SLinus Torvalds return rc; 19741da177e4SLinus Torvalds } 19751da177e4SLinus Torvalds 19761da177e4SLinus Torvalds return 0; 19771da177e4SLinus Torvalds } 19781da177e4SLinus Torvalds 19791da177e4SLinus Torvalds /* Check whether a task can perform a filesystem operation. */ 198088e67f3bSDavid Howells static int superblock_has_perm(const struct cred *cred, 19811da177e4SLinus Torvalds struct super_block *sb, 19821da177e4SLinus Torvalds u32 perms, 19832bf49690SThomas Liu struct common_audit_data *ad) 19841da177e4SLinus Torvalds { 19851da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 198688e67f3bSDavid Howells u32 sid = cred_sid(cred); 19871da177e4SLinus Torvalds 19881da177e4SLinus Torvalds sbsec = sb->s_security; 19896b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 19906b6bc620SStephen Smalley sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad); 19911da177e4SLinus Torvalds } 19921da177e4SLinus Torvalds 19931da177e4SLinus Torvalds /* Convert a Linux mode and permission mask to an access vector. */ 19941da177e4SLinus Torvalds static inline u32 file_mask_to_av(int mode, int mask) 19951da177e4SLinus Torvalds { 19961da177e4SLinus Torvalds u32 av = 0; 19971da177e4SLinus Torvalds 1998dba19c60SAl Viro if (!S_ISDIR(mode)) { 19991da177e4SLinus Torvalds if (mask & MAY_EXEC) 20001da177e4SLinus Torvalds av |= FILE__EXECUTE; 20011da177e4SLinus Torvalds if (mask & MAY_READ) 20021da177e4SLinus Torvalds av |= FILE__READ; 20031da177e4SLinus Torvalds 20041da177e4SLinus Torvalds if (mask & MAY_APPEND) 20051da177e4SLinus Torvalds av |= FILE__APPEND; 20061da177e4SLinus Torvalds else if (mask & MAY_WRITE) 20071da177e4SLinus Torvalds av |= FILE__WRITE; 20081da177e4SLinus Torvalds 20091da177e4SLinus Torvalds } else { 20101da177e4SLinus Torvalds if (mask & MAY_EXEC) 20111da177e4SLinus Torvalds av |= DIR__SEARCH; 20121da177e4SLinus Torvalds if (mask & MAY_WRITE) 20131da177e4SLinus Torvalds av |= DIR__WRITE; 20141da177e4SLinus Torvalds if (mask & MAY_READ) 20151da177e4SLinus Torvalds av |= DIR__READ; 20161da177e4SLinus Torvalds } 20171da177e4SLinus Torvalds 20181da177e4SLinus Torvalds return av; 20191da177e4SLinus Torvalds } 20201da177e4SLinus Torvalds 20211da177e4SLinus Torvalds /* Convert a Linux file to an access vector. */ 20221da177e4SLinus Torvalds static inline u32 file_to_av(struct file *file) 20231da177e4SLinus Torvalds { 20241da177e4SLinus Torvalds u32 av = 0; 20251da177e4SLinus Torvalds 20261da177e4SLinus Torvalds if (file->f_mode & FMODE_READ) 20271da177e4SLinus Torvalds av |= FILE__READ; 20281da177e4SLinus Torvalds if (file->f_mode & FMODE_WRITE) { 20291da177e4SLinus Torvalds if (file->f_flags & O_APPEND) 20301da177e4SLinus Torvalds av |= FILE__APPEND; 20311da177e4SLinus Torvalds else 20321da177e4SLinus Torvalds av |= FILE__WRITE; 20331da177e4SLinus Torvalds } 20340794c66dSStephen Smalley if (!av) { 20350794c66dSStephen Smalley /* 20360794c66dSStephen Smalley * Special file opened with flags 3 for ioctl-only use. 20370794c66dSStephen Smalley */ 20380794c66dSStephen Smalley av = FILE__IOCTL; 20390794c66dSStephen Smalley } 20401da177e4SLinus Torvalds 20411da177e4SLinus Torvalds return av; 20421da177e4SLinus Torvalds } 20431da177e4SLinus Torvalds 20448b6a5a37SEric Paris /* 20458b6a5a37SEric Paris * Convert a file to an access vector and include the correct open 20468b6a5a37SEric Paris * open permission. 20478b6a5a37SEric Paris */ 20488b6a5a37SEric Paris static inline u32 open_file_to_av(struct file *file) 20498b6a5a37SEric Paris { 20508b6a5a37SEric Paris u32 av = file_to_av(file); 2051ccb54478SStephen Smalley struct inode *inode = file_inode(file); 20528b6a5a37SEric Paris 2053aa8e712cSStephen Smalley if (selinux_policycap_openperm() && 2054aa8e712cSStephen Smalley inode->i_sb->s_magic != SOCKFS_MAGIC) 20558b6a5a37SEric Paris av |= FILE__OPEN; 205649b7b8deSEric Paris 20578b6a5a37SEric Paris return av; 20588b6a5a37SEric Paris } 20598b6a5a37SEric Paris 20601da177e4SLinus Torvalds /* Hook functions begin here. */ 20611da177e4SLinus Torvalds 206279af7307SStephen Smalley static int selinux_binder_set_context_mgr(struct task_struct *mgr) 206379af7307SStephen Smalley { 206479af7307SStephen Smalley u32 mysid = current_sid(); 206579af7307SStephen Smalley u32 mgrsid = task_sid(mgr); 206679af7307SStephen Smalley 20676b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 20686b6bc620SStephen Smalley mysid, mgrsid, SECCLASS_BINDER, 206979af7307SStephen Smalley BINDER__SET_CONTEXT_MGR, NULL); 207079af7307SStephen Smalley } 207179af7307SStephen Smalley 207279af7307SStephen Smalley static int selinux_binder_transaction(struct task_struct *from, 207379af7307SStephen Smalley struct task_struct *to) 207479af7307SStephen Smalley { 207579af7307SStephen Smalley u32 mysid = current_sid(); 207679af7307SStephen Smalley u32 fromsid = task_sid(from); 207779af7307SStephen Smalley u32 tosid = task_sid(to); 207879af7307SStephen Smalley int rc; 207979af7307SStephen Smalley 208079af7307SStephen Smalley if (mysid != fromsid) { 20816b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 20826b6bc620SStephen Smalley mysid, fromsid, SECCLASS_BINDER, 208379af7307SStephen Smalley BINDER__IMPERSONATE, NULL); 208479af7307SStephen Smalley if (rc) 208579af7307SStephen Smalley return rc; 208679af7307SStephen Smalley } 208779af7307SStephen Smalley 20886b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 20896b6bc620SStephen Smalley fromsid, tosid, SECCLASS_BINDER, BINDER__CALL, 209079af7307SStephen Smalley NULL); 209179af7307SStephen Smalley } 209279af7307SStephen Smalley 209379af7307SStephen Smalley static int selinux_binder_transfer_binder(struct task_struct *from, 209479af7307SStephen Smalley struct task_struct *to) 209579af7307SStephen Smalley { 209679af7307SStephen Smalley u32 fromsid = task_sid(from); 209779af7307SStephen Smalley u32 tosid = task_sid(to); 209879af7307SStephen Smalley 20996b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 21006b6bc620SStephen Smalley fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER, 210179af7307SStephen Smalley NULL); 210279af7307SStephen Smalley } 210379af7307SStephen Smalley 210479af7307SStephen Smalley static int selinux_binder_transfer_file(struct task_struct *from, 210579af7307SStephen Smalley struct task_struct *to, 210679af7307SStephen Smalley struct file *file) 210779af7307SStephen Smalley { 210879af7307SStephen Smalley u32 sid = task_sid(to); 210979af7307SStephen Smalley struct file_security_struct *fsec = file->f_security; 211083da53c5SAndreas Gruenbacher struct dentry *dentry = file->f_path.dentry; 211120cdef8dSPaul Moore struct inode_security_struct *isec; 211279af7307SStephen Smalley struct common_audit_data ad; 211379af7307SStephen Smalley int rc; 211479af7307SStephen Smalley 211579af7307SStephen Smalley ad.type = LSM_AUDIT_DATA_PATH; 211679af7307SStephen Smalley ad.u.path = file->f_path; 211779af7307SStephen Smalley 211879af7307SStephen Smalley if (sid != fsec->sid) { 21196b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 21206b6bc620SStephen Smalley sid, fsec->sid, 212179af7307SStephen Smalley SECCLASS_FD, 212279af7307SStephen Smalley FD__USE, 212379af7307SStephen Smalley &ad); 212479af7307SStephen Smalley if (rc) 212579af7307SStephen Smalley return rc; 212679af7307SStephen Smalley } 212779af7307SStephen Smalley 2128f66e448cSChenbo Feng #ifdef CONFIG_BPF_SYSCALL 2129f66e448cSChenbo Feng rc = bpf_fd_pass(file, sid); 2130f66e448cSChenbo Feng if (rc) 2131f66e448cSChenbo Feng return rc; 2132f66e448cSChenbo Feng #endif 2133f66e448cSChenbo Feng 213483da53c5SAndreas Gruenbacher if (unlikely(IS_PRIVATE(d_backing_inode(dentry)))) 213579af7307SStephen Smalley return 0; 213679af7307SStephen Smalley 213720cdef8dSPaul Moore isec = backing_inode_security(dentry); 21386b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 21396b6bc620SStephen Smalley sid, isec->sid, isec->sclass, file_to_av(file), 214079af7307SStephen Smalley &ad); 214179af7307SStephen Smalley } 214279af7307SStephen Smalley 21439e48858fSIngo Molnar static int selinux_ptrace_access_check(struct task_struct *child, 2144006ebb40SStephen Smalley unsigned int mode) 21451da177e4SLinus Torvalds { 2146275bb41eSDavid Howells u32 sid = current_sid(); 2147275bb41eSDavid Howells u32 csid = task_sid(child); 2148006ebb40SStephen Smalley 2149be0554c9SStephen Smalley if (mode & PTRACE_MODE_READ) 21506b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 21516b6bc620SStephen Smalley sid, csid, SECCLASS_FILE, FILE__READ, NULL); 2152be0554c9SStephen Smalley 21536b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 21546b6bc620SStephen Smalley sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL); 21555cd9c58fSDavid Howells } 21565cd9c58fSDavid Howells 21575cd9c58fSDavid Howells static int selinux_ptrace_traceme(struct task_struct *parent) 21585cd9c58fSDavid Howells { 21596b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 21606b6bc620SStephen Smalley task_sid(parent), current_sid(), SECCLASS_PROCESS, 2161be0554c9SStephen Smalley PROCESS__PTRACE, NULL); 21621da177e4SLinus Torvalds } 21631da177e4SLinus Torvalds 21641da177e4SLinus Torvalds static int selinux_capget(struct task_struct *target, kernel_cap_t *effective, 21651da177e4SLinus Torvalds kernel_cap_t *inheritable, kernel_cap_t *permitted) 21661da177e4SLinus Torvalds { 21676b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 21686b6bc620SStephen Smalley current_sid(), task_sid(target), SECCLASS_PROCESS, 2169be0554c9SStephen Smalley PROCESS__GETCAP, NULL); 21701da177e4SLinus Torvalds } 21711da177e4SLinus Torvalds 2172d84f4f99SDavid Howells static int selinux_capset(struct cred *new, const struct cred *old, 2173d84f4f99SDavid Howells const kernel_cap_t *effective, 217415a2460eSDavid Howells const kernel_cap_t *inheritable, 217515a2460eSDavid Howells const kernel_cap_t *permitted) 21761da177e4SLinus Torvalds { 21776b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 21786b6bc620SStephen Smalley cred_sid(old), cred_sid(new), SECCLASS_PROCESS, 2179be0554c9SStephen Smalley PROCESS__SETCAP, NULL); 21801da177e4SLinus Torvalds } 21811da177e4SLinus Torvalds 21825626d3e8SJames Morris /* 21835626d3e8SJames Morris * (This comment used to live with the selinux_task_setuid hook, 21845626d3e8SJames Morris * which was removed). 21855626d3e8SJames Morris * 21865626d3e8SJames Morris * Since setuid only affects the current process, and since the SELinux 21875626d3e8SJames Morris * controls are not based on the Linux identity attributes, SELinux does not 21885626d3e8SJames Morris * need to control this operation. However, SELinux does control the use of 21895626d3e8SJames Morris * the CAP_SETUID and CAP_SETGID capabilities using the capable hook. 21905626d3e8SJames Morris */ 21915626d3e8SJames Morris 21926a9de491SEric Paris static int selinux_capable(const struct cred *cred, struct user_namespace *ns, 21936a9de491SEric Paris int cap, int audit) 21941da177e4SLinus Torvalds { 21958e4ff6f2SStephen Smalley return cred_has_capability(cred, cap, audit, ns == &init_user_ns); 21961da177e4SLinus Torvalds } 21971da177e4SLinus Torvalds 21981da177e4SLinus Torvalds static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb) 21991da177e4SLinus Torvalds { 220088e67f3bSDavid Howells const struct cred *cred = current_cred(); 22011da177e4SLinus Torvalds int rc = 0; 22021da177e4SLinus Torvalds 22031da177e4SLinus Torvalds if (!sb) 22041da177e4SLinus Torvalds return 0; 22051da177e4SLinus Torvalds 22061da177e4SLinus Torvalds switch (cmds) { 22071da177e4SLinus Torvalds case Q_SYNC: 22081da177e4SLinus Torvalds case Q_QUOTAON: 22091da177e4SLinus Torvalds case Q_QUOTAOFF: 22101da177e4SLinus Torvalds case Q_SETINFO: 22111da177e4SLinus Torvalds case Q_SETQUOTA: 221288e67f3bSDavid Howells rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL); 22131da177e4SLinus Torvalds break; 22141da177e4SLinus Torvalds case Q_GETFMT: 22151da177e4SLinus Torvalds case Q_GETINFO: 22161da177e4SLinus Torvalds case Q_GETQUOTA: 221788e67f3bSDavid Howells rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL); 22181da177e4SLinus Torvalds break; 22191da177e4SLinus Torvalds default: 22201da177e4SLinus Torvalds rc = 0; /* let the kernel handle invalid cmds */ 22211da177e4SLinus Torvalds break; 22221da177e4SLinus Torvalds } 22231da177e4SLinus Torvalds return rc; 22241da177e4SLinus Torvalds } 22251da177e4SLinus Torvalds 22261da177e4SLinus Torvalds static int selinux_quota_on(struct dentry *dentry) 22271da177e4SLinus Torvalds { 222888e67f3bSDavid Howells const struct cred *cred = current_cred(); 222988e67f3bSDavid Howells 22302875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__QUOTAON); 22311da177e4SLinus Torvalds } 22321da177e4SLinus Torvalds 223312b3052cSEric Paris static int selinux_syslog(int type) 22341da177e4SLinus Torvalds { 22351da177e4SLinus Torvalds switch (type) { 2236d78ca3cdSKees Cook case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */ 2237d78ca3cdSKees Cook case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */ 22386b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 22396b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, 2240be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL); 2241d78ca3cdSKees Cook case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */ 2242d78ca3cdSKees Cook case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */ 2243d78ca3cdSKees Cook /* Set level of messages printed to console */ 2244d78ca3cdSKees Cook case SYSLOG_ACTION_CONSOLE_LEVEL: 22456b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 22466b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, 2247be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE, 2248be0554c9SStephen Smalley NULL); 22491da177e4SLinus Torvalds } 2250be0554c9SStephen Smalley /* All other syslog types */ 22516b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 22526b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, 2253be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL); 22541da177e4SLinus Torvalds } 22551da177e4SLinus Torvalds 22561da177e4SLinus Torvalds /* 22571da177e4SLinus Torvalds * Check that a process has enough memory to allocate a new virtual 22581da177e4SLinus Torvalds * mapping. 0 means there is enough memory for the allocation to 22591da177e4SLinus Torvalds * succeed and -ENOMEM implies there is not. 22601da177e4SLinus Torvalds * 22611da177e4SLinus Torvalds * Do not audit the selinux permission check, as this is applied to all 22621da177e4SLinus Torvalds * processes that allocate mappings. 22631da177e4SLinus Torvalds */ 226434b4e4aaSAlan Cox static int selinux_vm_enough_memory(struct mm_struct *mm, long pages) 22651da177e4SLinus Torvalds { 22661da177e4SLinus Torvalds int rc, cap_sys_admin = 0; 22671da177e4SLinus Torvalds 2268b1d9e6b0SCasey Schaufler rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN, 22698e4ff6f2SStephen Smalley SECURITY_CAP_NOAUDIT, true); 22701da177e4SLinus Torvalds if (rc == 0) 22711da177e4SLinus Torvalds cap_sys_admin = 1; 22721da177e4SLinus Torvalds 2273b1d9e6b0SCasey Schaufler return cap_sys_admin; 22741da177e4SLinus Torvalds } 22751da177e4SLinus Torvalds 22761da177e4SLinus Torvalds /* binprm security operations */ 22771da177e4SLinus Torvalds 2278be0554c9SStephen Smalley static u32 ptrace_parent_sid(void) 22790c6181cbSPaul Moore { 22800c6181cbSPaul Moore u32 sid = 0; 22810c6181cbSPaul Moore struct task_struct *tracer; 22820c6181cbSPaul Moore 22830c6181cbSPaul Moore rcu_read_lock(); 2284be0554c9SStephen Smalley tracer = ptrace_parent(current); 22850c6181cbSPaul Moore if (tracer) 22860c6181cbSPaul Moore sid = task_sid(tracer); 22870c6181cbSPaul Moore rcu_read_unlock(); 22880c6181cbSPaul Moore 22890c6181cbSPaul Moore return sid; 22900c6181cbSPaul Moore } 22910c6181cbSPaul Moore 22927b0d0b40SStephen Smalley static int check_nnp_nosuid(const struct linux_binprm *bprm, 22937b0d0b40SStephen Smalley const struct task_security_struct *old_tsec, 22947b0d0b40SStephen Smalley const struct task_security_struct *new_tsec) 22957b0d0b40SStephen Smalley { 22967b0d0b40SStephen Smalley int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS); 2297380cf5baSAndy Lutomirski int nosuid = !mnt_may_suid(bprm->file->f_path.mnt); 22987b0d0b40SStephen Smalley int rc; 2299af63f419SStephen Smalley u32 av; 23007b0d0b40SStephen Smalley 23017b0d0b40SStephen Smalley if (!nnp && !nosuid) 23027b0d0b40SStephen Smalley return 0; /* neither NNP nor nosuid */ 23037b0d0b40SStephen Smalley 23047b0d0b40SStephen Smalley if (new_tsec->sid == old_tsec->sid) 23057b0d0b40SStephen Smalley return 0; /* No change in credentials */ 23067b0d0b40SStephen Smalley 23077b0d0b40SStephen Smalley /* 2308af63f419SStephen Smalley * If the policy enables the nnp_nosuid_transition policy capability, 2309af63f419SStephen Smalley * then we permit transitions under NNP or nosuid if the 2310af63f419SStephen Smalley * policy allows the corresponding permission between 2311af63f419SStephen Smalley * the old and new contexts. 2312af63f419SStephen Smalley */ 2313aa8e712cSStephen Smalley if (selinux_policycap_nnp_nosuid_transition()) { 2314af63f419SStephen Smalley av = 0; 2315af63f419SStephen Smalley if (nnp) 2316af63f419SStephen Smalley av |= PROCESS2__NNP_TRANSITION; 2317af63f419SStephen Smalley if (nosuid) 2318af63f419SStephen Smalley av |= PROCESS2__NOSUID_TRANSITION; 23196b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 23206b6bc620SStephen Smalley old_tsec->sid, new_tsec->sid, 2321af63f419SStephen Smalley SECCLASS_PROCESS2, av, NULL); 2322af63f419SStephen Smalley if (!rc) 2323af63f419SStephen Smalley return 0; 2324af63f419SStephen Smalley } 2325af63f419SStephen Smalley 2326af63f419SStephen Smalley /* 2327af63f419SStephen Smalley * We also permit NNP or nosuid transitions to bounded SIDs, 2328af63f419SStephen Smalley * i.e. SIDs that are guaranteed to only be allowed a subset 2329af63f419SStephen Smalley * of the permissions of the current SID. 23307b0d0b40SStephen Smalley */ 2331aa8e712cSStephen Smalley rc = security_bounded_transition(&selinux_state, old_tsec->sid, 2332aa8e712cSStephen Smalley new_tsec->sid); 2333af63f419SStephen Smalley if (!rc) 2334af63f419SStephen Smalley return 0; 2335af63f419SStephen Smalley 23367b0d0b40SStephen Smalley /* 23377b0d0b40SStephen Smalley * On failure, preserve the errno values for NNP vs nosuid. 23387b0d0b40SStephen Smalley * NNP: Operation not permitted for caller. 23397b0d0b40SStephen Smalley * nosuid: Permission denied to file. 23407b0d0b40SStephen Smalley */ 23417b0d0b40SStephen Smalley if (nnp) 23427b0d0b40SStephen Smalley return -EPERM; 23437b0d0b40SStephen Smalley return -EACCES; 23447b0d0b40SStephen Smalley } 23457b0d0b40SStephen Smalley 2346a6f76f23SDavid Howells static int selinux_bprm_set_creds(struct linux_binprm *bprm) 23471da177e4SLinus Torvalds { 2348a6f76f23SDavid Howells const struct task_security_struct *old_tsec; 2349a6f76f23SDavid Howells struct task_security_struct *new_tsec; 23501da177e4SLinus Torvalds struct inode_security_struct *isec; 23512bf49690SThomas Liu struct common_audit_data ad; 2352496ad9aaSAl Viro struct inode *inode = file_inode(bprm->file); 23531da177e4SLinus Torvalds int rc; 23541da177e4SLinus Torvalds 2355a6f76f23SDavid Howells /* SELinux context only depends on initial program or script and not 2356a6f76f23SDavid Howells * the script interpreter */ 2357ddb4a144SKees Cook if (bprm->called_set_creds) 23581da177e4SLinus Torvalds return 0; 23591da177e4SLinus Torvalds 2360a6f76f23SDavid Howells old_tsec = current_security(); 2361a6f76f23SDavid Howells new_tsec = bprm->cred->security; 236283da53c5SAndreas Gruenbacher isec = inode_security(inode); 23631da177e4SLinus Torvalds 23641da177e4SLinus Torvalds /* Default to the current task SID. */ 2365a6f76f23SDavid Howells new_tsec->sid = old_tsec->sid; 2366a6f76f23SDavid Howells new_tsec->osid = old_tsec->sid; 23671da177e4SLinus Torvalds 236828eba5bfSMichael LeMay /* Reset fs, key, and sock SIDs on execve. */ 2369a6f76f23SDavid Howells new_tsec->create_sid = 0; 2370a6f76f23SDavid Howells new_tsec->keycreate_sid = 0; 2371a6f76f23SDavid Howells new_tsec->sockcreate_sid = 0; 23721da177e4SLinus Torvalds 2373a6f76f23SDavid Howells if (old_tsec->exec_sid) { 2374a6f76f23SDavid Howells new_tsec->sid = old_tsec->exec_sid; 23751da177e4SLinus Torvalds /* Reset exec SID on execve. */ 2376a6f76f23SDavid Howells new_tsec->exec_sid = 0; 2377259e5e6cSAndy Lutomirski 23787b0d0b40SStephen Smalley /* Fail on NNP or nosuid if not an allowed transition. */ 23797b0d0b40SStephen Smalley rc = check_nnp_nosuid(bprm, old_tsec, new_tsec); 23807b0d0b40SStephen Smalley if (rc) 23817b0d0b40SStephen Smalley return rc; 23821da177e4SLinus Torvalds } else { 23831da177e4SLinus Torvalds /* Check for a default transition on this program. */ 2384aa8e712cSStephen Smalley rc = security_transition_sid(&selinux_state, old_tsec->sid, 2385aa8e712cSStephen Smalley isec->sid, SECCLASS_PROCESS, NULL, 2386652bb9b0SEric Paris &new_tsec->sid); 23871da177e4SLinus Torvalds if (rc) 23881da177e4SLinus Torvalds return rc; 23897b0d0b40SStephen Smalley 23907b0d0b40SStephen Smalley /* 23917b0d0b40SStephen Smalley * Fallback to old SID on NNP or nosuid if not an allowed 23927b0d0b40SStephen Smalley * transition. 23937b0d0b40SStephen Smalley */ 23947b0d0b40SStephen Smalley rc = check_nnp_nosuid(bprm, old_tsec, new_tsec); 23957b0d0b40SStephen Smalley if (rc) 23967b0d0b40SStephen Smalley new_tsec->sid = old_tsec->sid; 23971da177e4SLinus Torvalds } 23981da177e4SLinus Torvalds 239943af5de7SVivek Goyal ad.type = LSM_AUDIT_DATA_FILE; 240043af5de7SVivek Goyal ad.u.file = bprm->file; 24011da177e4SLinus Torvalds 2402a6f76f23SDavid Howells if (new_tsec->sid == old_tsec->sid) { 24036b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 24046b6bc620SStephen Smalley old_tsec->sid, isec->sid, 24051da177e4SLinus Torvalds SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad); 24061da177e4SLinus Torvalds if (rc) 24071da177e4SLinus Torvalds return rc; 24081da177e4SLinus Torvalds } else { 24091da177e4SLinus Torvalds /* Check permissions for the transition. */ 24106b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 24116b6bc620SStephen Smalley old_tsec->sid, new_tsec->sid, 24121da177e4SLinus Torvalds SECCLASS_PROCESS, PROCESS__TRANSITION, &ad); 24131da177e4SLinus Torvalds if (rc) 24141da177e4SLinus Torvalds return rc; 24151da177e4SLinus Torvalds 24166b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 24176b6bc620SStephen Smalley new_tsec->sid, isec->sid, 24181da177e4SLinus Torvalds SECCLASS_FILE, FILE__ENTRYPOINT, &ad); 24191da177e4SLinus Torvalds if (rc) 24201da177e4SLinus Torvalds return rc; 24211da177e4SLinus Torvalds 2422a6f76f23SDavid Howells /* Check for shared state */ 2423a6f76f23SDavid Howells if (bprm->unsafe & LSM_UNSAFE_SHARE) { 24246b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 24256b6bc620SStephen Smalley old_tsec->sid, new_tsec->sid, 2426a6f76f23SDavid Howells SECCLASS_PROCESS, PROCESS__SHARE, 2427a6f76f23SDavid Howells NULL); 2428a6f76f23SDavid Howells if (rc) 2429a6f76f23SDavid Howells return -EPERM; 24301da177e4SLinus Torvalds } 24311da177e4SLinus Torvalds 2432a6f76f23SDavid Howells /* Make sure that anyone attempting to ptrace over a task that 2433a6f76f23SDavid Howells * changes its SID has the appropriate permit */ 24349227dd2aSEric W. Biederman if (bprm->unsafe & LSM_UNSAFE_PTRACE) { 2435be0554c9SStephen Smalley u32 ptsid = ptrace_parent_sid(); 2436a6f76f23SDavid Howells if (ptsid != 0) { 24376b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 24386b6bc620SStephen Smalley ptsid, new_tsec->sid, 2439a6f76f23SDavid Howells SECCLASS_PROCESS, 2440a6f76f23SDavid Howells PROCESS__PTRACE, NULL); 2441a6f76f23SDavid Howells if (rc) 2442a6f76f23SDavid Howells return -EPERM; 2443a6f76f23SDavid Howells } 2444a6f76f23SDavid Howells } 2445a6f76f23SDavid Howells 2446a6f76f23SDavid Howells /* Clear any possibly unsafe personality bits on exec: */ 2447a6f76f23SDavid Howells bprm->per_clear |= PER_CLEAR_ON_SETID; 2448a6f76f23SDavid Howells 24491da177e4SLinus Torvalds /* Enable secure mode for SIDs transitions unless 24501da177e4SLinus Torvalds the noatsecure permission is granted between 24511da177e4SLinus Torvalds the two SIDs, i.e. ahp returns 0. */ 24526b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 24536b6bc620SStephen Smalley old_tsec->sid, new_tsec->sid, 245462874c3aSKees Cook SECCLASS_PROCESS, PROCESS__NOATSECURE, 245562874c3aSKees Cook NULL); 245662874c3aSKees Cook bprm->secureexec |= !!rc; 24571da177e4SLinus Torvalds } 24581da177e4SLinus Torvalds 245962874c3aSKees Cook return 0; 24601da177e4SLinus Torvalds } 24611da177e4SLinus Torvalds 2462c3c073f8SAl Viro static int match_file(const void *p, struct file *file, unsigned fd) 2463c3c073f8SAl Viro { 2464c3c073f8SAl Viro return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0; 2465c3c073f8SAl Viro } 2466c3c073f8SAl Viro 24671da177e4SLinus Torvalds /* Derived from fs/exec.c:flush_old_files. */ 2468745ca247SDavid Howells static inline void flush_unauthorized_files(const struct cred *cred, 2469745ca247SDavid Howells struct files_struct *files) 24701da177e4SLinus Torvalds { 24711da177e4SLinus Torvalds struct file *file, *devnull = NULL; 2472b20c8122SStephen Smalley struct tty_struct *tty; 247324ec839cSPeter Zijlstra int drop_tty = 0; 2474c3c073f8SAl Viro unsigned n; 24751da177e4SLinus Torvalds 247624ec839cSPeter Zijlstra tty = get_current_tty(); 24771da177e4SLinus Torvalds if (tty) { 24784a510969SPeter Hurley spin_lock(&tty->files_lock); 247937dd0bd0SEric Paris if (!list_empty(&tty->tty_files)) { 2480d996b62aSNick Piggin struct tty_file_private *file_priv; 248137dd0bd0SEric Paris 24821da177e4SLinus Torvalds /* Revalidate access to controlling tty. 248313f8e981SDavid Howells Use file_path_has_perm on the tty path directly 248413f8e981SDavid Howells rather than using file_has_perm, as this particular 248513f8e981SDavid Howells open file may belong to another process and we are 248613f8e981SDavid Howells only interested in the inode-based check here. */ 2487d996b62aSNick Piggin file_priv = list_first_entry(&tty->tty_files, 2488d996b62aSNick Piggin struct tty_file_private, list); 2489d996b62aSNick Piggin file = file_priv->file; 249013f8e981SDavid Howells if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE)) 249124ec839cSPeter Zijlstra drop_tty = 1; 24921da177e4SLinus Torvalds } 24934a510969SPeter Hurley spin_unlock(&tty->files_lock); 2494452a00d2SAlan Cox tty_kref_put(tty); 24951da177e4SLinus Torvalds } 249698a27ba4SEric W. Biederman /* Reset controlling tty. */ 249798a27ba4SEric W. Biederman if (drop_tty) 249898a27ba4SEric W. Biederman no_tty(); 24991da177e4SLinus Torvalds 25001da177e4SLinus Torvalds /* Revalidate access to inherited open files. */ 2501c3c073f8SAl Viro n = iterate_fd(files, 0, match_file, cred); 2502c3c073f8SAl Viro if (!n) /* none found? */ 2503c3c073f8SAl Viro return; 25041da177e4SLinus Torvalds 2505c3c073f8SAl Viro devnull = dentry_open(&selinux_null, O_RDWR, cred); 250645525b26SAl Viro if (IS_ERR(devnull)) 250745525b26SAl Viro devnull = NULL; 2508c3c073f8SAl Viro /* replace all the matching ones with this */ 2509c3c073f8SAl Viro do { 251045525b26SAl Viro replace_fd(n - 1, devnull, 0); 2511c3c073f8SAl Viro } while ((n = iterate_fd(files, n, match_file, cred)) != 0); 251245525b26SAl Viro if (devnull) 2513c3c073f8SAl Viro fput(devnull); 25141da177e4SLinus Torvalds } 25151da177e4SLinus Torvalds 25161da177e4SLinus Torvalds /* 2517a6f76f23SDavid Howells * Prepare a process for imminent new credential changes due to exec 25181da177e4SLinus Torvalds */ 2519a6f76f23SDavid Howells static void selinux_bprm_committing_creds(struct linux_binprm *bprm) 25201da177e4SLinus Torvalds { 2521a6f76f23SDavid Howells struct task_security_struct *new_tsec; 25221da177e4SLinus Torvalds struct rlimit *rlim, *initrlim; 25231da177e4SLinus Torvalds int rc, i; 25241da177e4SLinus Torvalds 2525a6f76f23SDavid Howells new_tsec = bprm->cred->security; 2526a6f76f23SDavid Howells if (new_tsec->sid == new_tsec->osid) 25271da177e4SLinus Torvalds return; 25281da177e4SLinus Torvalds 25291da177e4SLinus Torvalds /* Close files for which the new task SID is not authorized. */ 2530a6f76f23SDavid Howells flush_unauthorized_files(bprm->cred, current->files); 25311da177e4SLinus Torvalds 2532a6f76f23SDavid Howells /* Always clear parent death signal on SID transitions. */ 2533a6f76f23SDavid Howells current->pdeath_signal = 0; 2534a6f76f23SDavid Howells 2535a6f76f23SDavid Howells /* Check whether the new SID can inherit resource limits from the old 2536a6f76f23SDavid Howells * SID. If not, reset all soft limits to the lower of the current 2537a6f76f23SDavid Howells * task's hard limit and the init task's soft limit. 2538a6f76f23SDavid Howells * 2539a6f76f23SDavid Howells * Note that the setting of hard limits (even to lower them) can be 2540a6f76f23SDavid Howells * controlled by the setrlimit check. The inclusion of the init task's 2541a6f76f23SDavid Howells * soft limit into the computation is to avoid resetting soft limits 2542a6f76f23SDavid Howells * higher than the default soft limit for cases where the default is 2543a6f76f23SDavid Howells * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK. 2544a6f76f23SDavid Howells */ 25456b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 25466b6bc620SStephen Smalley new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS, 2547a6f76f23SDavid Howells PROCESS__RLIMITINH, NULL); 2548a6f76f23SDavid Howells if (rc) { 2549eb2d55a3SOleg Nesterov /* protect against do_prlimit() */ 2550eb2d55a3SOleg Nesterov task_lock(current); 2551a6f76f23SDavid Howells for (i = 0; i < RLIM_NLIMITS; i++) { 2552a6f76f23SDavid Howells rlim = current->signal->rlim + i; 2553a6f76f23SDavid Howells initrlim = init_task.signal->rlim + i; 2554a6f76f23SDavid Howells rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur); 2555a6f76f23SDavid Howells } 2556eb2d55a3SOleg Nesterov task_unlock(current); 2557baa73d9eSNicolas Pitre if (IS_ENABLED(CONFIG_POSIX_TIMERS)) 2558eb2d55a3SOleg Nesterov update_rlimit_cpu(current, rlimit(RLIMIT_CPU)); 2559a6f76f23SDavid Howells } 2560a6f76f23SDavid Howells } 2561a6f76f23SDavid Howells 2562a6f76f23SDavid Howells /* 2563a6f76f23SDavid Howells * Clean up the process immediately after the installation of new credentials 2564a6f76f23SDavid Howells * due to exec 2565a6f76f23SDavid Howells */ 2566a6f76f23SDavid Howells static void selinux_bprm_committed_creds(struct linux_binprm *bprm) 2567a6f76f23SDavid Howells { 2568a6f76f23SDavid Howells const struct task_security_struct *tsec = current_security(); 2569a6f76f23SDavid Howells struct itimerval itimer; 2570a6f76f23SDavid Howells u32 osid, sid; 2571a6f76f23SDavid Howells int rc, i; 2572a6f76f23SDavid Howells 2573a6f76f23SDavid Howells osid = tsec->osid; 2574a6f76f23SDavid Howells sid = tsec->sid; 2575a6f76f23SDavid Howells 2576a6f76f23SDavid Howells if (sid == osid) 2577a6f76f23SDavid Howells return; 2578a6f76f23SDavid Howells 2579a6f76f23SDavid Howells /* Check whether the new SID can inherit signal state from the old SID. 2580a6f76f23SDavid Howells * If not, clear itimers to avoid subsequent signal generation and 2581a6f76f23SDavid Howells * flush and unblock signals. 2582a6f76f23SDavid Howells * 2583a6f76f23SDavid Howells * This must occur _after_ the task SID has been updated so that any 2584a6f76f23SDavid Howells * kill done after the flush will be checked against the new SID. 2585a6f76f23SDavid Howells */ 25866b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 25876b6bc620SStephen Smalley osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL); 25881da177e4SLinus Torvalds if (rc) { 2589baa73d9eSNicolas Pitre if (IS_ENABLED(CONFIG_POSIX_TIMERS)) { 25901da177e4SLinus Torvalds memset(&itimer, 0, sizeof itimer); 25911da177e4SLinus Torvalds for (i = 0; i < 3; i++) 25921da177e4SLinus Torvalds do_setitimer(i, &itimer, NULL); 2593baa73d9eSNicolas Pitre } 25941da177e4SLinus Torvalds spin_lock_irq(¤t->sighand->siglock); 25959e7c8f8cSOleg Nesterov if (!fatal_signal_pending(current)) { 25969e7c8f8cSOleg Nesterov flush_sigqueue(¤t->pending); 25979e7c8f8cSOleg Nesterov flush_sigqueue(¤t->signal->shared_pending); 25981da177e4SLinus Torvalds flush_signal_handlers(current, 1); 25991da177e4SLinus Torvalds sigemptyset(¤t->blocked); 26009e7c8f8cSOleg Nesterov recalc_sigpending(); 26013bcac026SDavid Howells } 26021da177e4SLinus Torvalds spin_unlock_irq(¤t->sighand->siglock); 26031da177e4SLinus Torvalds } 26041da177e4SLinus Torvalds 2605a6f76f23SDavid Howells /* Wake up the parent if it is waiting so that it can recheck 2606a6f76f23SDavid Howells * wait permission to the new task SID. */ 2607ecd6de3cSOleg Nesterov read_lock(&tasklist_lock); 26080b7570e7SOleg Nesterov __wake_up_parent(current, current->real_parent); 2609ecd6de3cSOleg Nesterov read_unlock(&tasklist_lock); 26101da177e4SLinus Torvalds } 26111da177e4SLinus Torvalds 26121da177e4SLinus Torvalds /* superblock security operations */ 26131da177e4SLinus Torvalds 26141da177e4SLinus Torvalds static int selinux_sb_alloc_security(struct super_block *sb) 26151da177e4SLinus Torvalds { 26161da177e4SLinus Torvalds return superblock_alloc_security(sb); 26171da177e4SLinus Torvalds } 26181da177e4SLinus Torvalds 26191da177e4SLinus Torvalds static void selinux_sb_free_security(struct super_block *sb) 26201da177e4SLinus Torvalds { 26211da177e4SLinus Torvalds superblock_free_security(sb); 26221da177e4SLinus Torvalds } 26231da177e4SLinus Torvalds 262499dbbb59SAl Viro static inline int opt_len(const char *s) 26251da177e4SLinus Torvalds { 262699dbbb59SAl Viro bool open_quote = false; 262799dbbb59SAl Viro int len; 262899dbbb59SAl Viro char c; 26291da177e4SLinus Torvalds 263099dbbb59SAl Viro for (len = 0; (c = s[len]) != '\0'; len++) { 263199dbbb59SAl Viro if (c == '"') 26323528a953SCory Olmo open_quote = !open_quote; 263399dbbb59SAl Viro if (c == ',' && !open_quote) 263499dbbb59SAl Viro break; 26351da177e4SLinus Torvalds } 263699dbbb59SAl Viro return len; 26371da177e4SLinus Torvalds } 26381da177e4SLinus Torvalds 2639204cc0ccSAl Viro static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts) 26405b400239SAl Viro { 264199dbbb59SAl Viro char *from = options; 264299dbbb59SAl Viro char *to = options; 264399dbbb59SAl Viro bool first = true; 26445b400239SAl Viro 264599dbbb59SAl Viro while (1) { 264699dbbb59SAl Viro int len = opt_len(from); 264799dbbb59SAl Viro int token, rc; 264899dbbb59SAl Viro char *arg = NULL; 264999dbbb59SAl Viro 265099dbbb59SAl Viro token = match_opt_prefix(from, len, &arg); 265199dbbb59SAl Viro 265299dbbb59SAl Viro if (token != Opt_error) { 265399dbbb59SAl Viro char *p, *q; 265499dbbb59SAl Viro 265599dbbb59SAl Viro /* strip quotes */ 265699dbbb59SAl Viro if (arg) { 265799dbbb59SAl Viro for (p = q = arg; p < from + len; p++) { 265899dbbb59SAl Viro char c = *p; 265999dbbb59SAl Viro if (c != '"') 266099dbbb59SAl Viro *q++ = c; 266199dbbb59SAl Viro } 266299dbbb59SAl Viro arg = kmemdup_nul(arg, q - arg, GFP_KERNEL); 266399dbbb59SAl Viro } 266499dbbb59SAl Viro rc = selinux_add_opt(token, arg, mnt_opts); 266599dbbb59SAl Viro if (unlikely(rc)) { 266699dbbb59SAl Viro kfree(arg); 266799dbbb59SAl Viro if (*mnt_opts) { 266899dbbb59SAl Viro selinux_free_mnt_opts(*mnt_opts); 266999dbbb59SAl Viro *mnt_opts = NULL; 267099dbbb59SAl Viro } 26711da177e4SLinus Torvalds return rc; 26721da177e4SLinus Torvalds } 267399dbbb59SAl Viro } else { 267499dbbb59SAl Viro if (!first) { // copy with preceding comma 267599dbbb59SAl Viro from--; 267699dbbb59SAl Viro len++; 267799dbbb59SAl Viro } 267899dbbb59SAl Viro if (to != from) 267999dbbb59SAl Viro memmove(to, from, len); 268099dbbb59SAl Viro to += len; 268199dbbb59SAl Viro first = false; 268299dbbb59SAl Viro } 268399dbbb59SAl Viro if (!from[len]) 268499dbbb59SAl Viro break; 268599dbbb59SAl Viro from += len + 1; 268699dbbb59SAl Viro } 268799dbbb59SAl Viro *to = '\0'; 268899dbbb59SAl Viro return 0; 26895b400239SAl Viro } 26901da177e4SLinus Torvalds 2691204cc0ccSAl Viro static int selinux_sb_remount(struct super_block *sb, void *mnt_opts) 2692026eb167SEric Paris { 2693bd323655SAl Viro struct selinux_mnt_opts *opts = mnt_opts; 2694026eb167SEric Paris struct superblock_security_struct *sbsec = sb->s_security; 2695bd323655SAl Viro u32 sid; 2696bd323655SAl Viro int rc; 2697026eb167SEric Paris 2698026eb167SEric Paris if (!(sbsec->flags & SE_SBINITIALIZED)) 2699026eb167SEric Paris return 0; 2700026eb167SEric Paris 2701204cc0ccSAl Viro if (!opts) 2702026eb167SEric Paris return 0; 2703026eb167SEric Paris 2704bd323655SAl Viro if (opts->fscontext) { 2705bd323655SAl Viro rc = parse_sid(sb, opts->fscontext, &sid); 2706026eb167SEric Paris if (rc) 2707c039bc3cSAl Viro return rc; 2708026eb167SEric Paris if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid)) 2709026eb167SEric Paris goto out_bad_option; 2710bd323655SAl Viro } 2711bd323655SAl Viro if (opts->context) { 2712bd323655SAl Viro rc = parse_sid(sb, opts->context, &sid); 2713bd323655SAl Viro if (rc) 2714bd323655SAl Viro return rc; 2715026eb167SEric Paris if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid)) 2716026eb167SEric Paris goto out_bad_option; 2717bd323655SAl Viro } 2718bd323655SAl Viro if (opts->rootcontext) { 2719026eb167SEric Paris struct inode_security_struct *root_isec; 272083da53c5SAndreas Gruenbacher root_isec = backing_inode_security(sb->s_root); 2721bd323655SAl Viro rc = parse_sid(sb, opts->rootcontext, &sid); 2722bd323655SAl Viro if (rc) 2723bd323655SAl Viro return rc; 2724026eb167SEric Paris if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid)) 2725026eb167SEric Paris goto out_bad_option; 2726026eb167SEric Paris } 2727bd323655SAl Viro if (opts->defcontext) { 2728bd323655SAl Viro rc = parse_sid(sb, opts->defcontext, &sid); 2729bd323655SAl Viro if (rc) 2730bd323655SAl Viro return rc; 2731026eb167SEric Paris if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid)) 2732026eb167SEric Paris goto out_bad_option; 2733026eb167SEric Paris } 2734c039bc3cSAl Viro return 0; 2735026eb167SEric Paris 2736026eb167SEric Paris out_bad_option: 2737c103a91eSpeter enderborg pr_warn("SELinux: unable to change security options " 273829b1deb2SLinus Torvalds "during remount (dev %s, type=%s)\n", sb->s_id, 273929b1deb2SLinus Torvalds sb->s_type->name); 2740c039bc3cSAl Viro return -EINVAL; 2741026eb167SEric Paris } 2742026eb167SEric Paris 2743a10d7c22SAl Viro static int selinux_sb_kern_mount(struct super_block *sb) 27441da177e4SLinus Torvalds { 274588e67f3bSDavid Howells const struct cred *cred = current_cred(); 27462bf49690SThomas Liu struct common_audit_data ad; 274774192246SJames Morris 274850c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 2749a269434dSEric Paris ad.u.dentry = sb->s_root; 275088e67f3bSDavid Howells return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad); 27511da177e4SLinus Torvalds } 27521da177e4SLinus Torvalds 2753726c3342SDavid Howells static int selinux_sb_statfs(struct dentry *dentry) 27541da177e4SLinus Torvalds { 275588e67f3bSDavid Howells const struct cred *cred = current_cred(); 27562bf49690SThomas Liu struct common_audit_data ad; 27571da177e4SLinus Torvalds 275850c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 2759a269434dSEric Paris ad.u.dentry = dentry->d_sb->s_root; 276088e67f3bSDavid Howells return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad); 27611da177e4SLinus Torvalds } 27621da177e4SLinus Torvalds 2763808d4e3cSAl Viro static int selinux_mount(const char *dev_name, 27648a04c43bSAl Viro const struct path *path, 2765808d4e3cSAl Viro const char *type, 27661da177e4SLinus Torvalds unsigned long flags, 27671da177e4SLinus Torvalds void *data) 27681da177e4SLinus Torvalds { 276988e67f3bSDavid Howells const struct cred *cred = current_cred(); 27701da177e4SLinus Torvalds 27711da177e4SLinus Torvalds if (flags & MS_REMOUNT) 2772d8c9584eSAl Viro return superblock_has_perm(cred, path->dentry->d_sb, 27731da177e4SLinus Torvalds FILESYSTEM__REMOUNT, NULL); 27741da177e4SLinus Torvalds else 27752875fa00SEric Paris return path_has_perm(cred, path, FILE__MOUNTON); 27761da177e4SLinus Torvalds } 27771da177e4SLinus Torvalds 27781da177e4SLinus Torvalds static int selinux_umount(struct vfsmount *mnt, int flags) 27791da177e4SLinus Torvalds { 278088e67f3bSDavid Howells const struct cred *cred = current_cred(); 27811da177e4SLinus Torvalds 278288e67f3bSDavid Howells return superblock_has_perm(cred, mnt->mnt_sb, 27831da177e4SLinus Torvalds FILESYSTEM__UNMOUNT, NULL); 27841da177e4SLinus Torvalds } 27851da177e4SLinus Torvalds 27861da177e4SLinus Torvalds /* inode security operations */ 27871da177e4SLinus Torvalds 27881da177e4SLinus Torvalds static int selinux_inode_alloc_security(struct inode *inode) 27891da177e4SLinus Torvalds { 27901da177e4SLinus Torvalds return inode_alloc_security(inode); 27911da177e4SLinus Torvalds } 27921da177e4SLinus Torvalds 27931da177e4SLinus Torvalds static void selinux_inode_free_security(struct inode *inode) 27941da177e4SLinus Torvalds { 27951da177e4SLinus Torvalds inode_free_security(inode); 27961da177e4SLinus Torvalds } 27971da177e4SLinus Torvalds 2798d47be3dfSDavid Quigley static int selinux_dentry_init_security(struct dentry *dentry, int mode, 27994f3ccd76SAl Viro const struct qstr *name, void **ctx, 2800d47be3dfSDavid Quigley u32 *ctxlen) 2801d47be3dfSDavid Quigley { 2802d47be3dfSDavid Quigley u32 newsid; 2803d47be3dfSDavid Quigley int rc; 2804d47be3dfSDavid Quigley 2805c957f6dfSVivek Goyal rc = selinux_determine_inode_label(current_security(), 2806c957f6dfSVivek Goyal d_inode(dentry->d_parent), name, 2807d47be3dfSDavid Quigley inode_mode_to_security_class(mode), 2808d47be3dfSDavid Quigley &newsid); 2809c3c188b2SDavid Howells if (rc) 2810d47be3dfSDavid Quigley return rc; 2811d47be3dfSDavid Quigley 2812aa8e712cSStephen Smalley return security_sid_to_context(&selinux_state, newsid, (char **)ctx, 2813aa8e712cSStephen Smalley ctxlen); 2814d47be3dfSDavid Quigley } 2815d47be3dfSDavid Quigley 2816a518b0a5SVivek Goyal static int selinux_dentry_create_files_as(struct dentry *dentry, int mode, 2817a518b0a5SVivek Goyal struct qstr *name, 2818a518b0a5SVivek Goyal const struct cred *old, 2819a518b0a5SVivek Goyal struct cred *new) 2820a518b0a5SVivek Goyal { 2821a518b0a5SVivek Goyal u32 newsid; 2822a518b0a5SVivek Goyal int rc; 2823a518b0a5SVivek Goyal struct task_security_struct *tsec; 2824a518b0a5SVivek Goyal 2825a518b0a5SVivek Goyal rc = selinux_determine_inode_label(old->security, 2826a518b0a5SVivek Goyal d_inode(dentry->d_parent), name, 2827a518b0a5SVivek Goyal inode_mode_to_security_class(mode), 2828a518b0a5SVivek Goyal &newsid); 2829a518b0a5SVivek Goyal if (rc) 2830a518b0a5SVivek Goyal return rc; 2831a518b0a5SVivek Goyal 2832a518b0a5SVivek Goyal tsec = new->security; 2833a518b0a5SVivek Goyal tsec->create_sid = newsid; 2834a518b0a5SVivek Goyal return 0; 2835a518b0a5SVivek Goyal } 2836a518b0a5SVivek Goyal 28375e41ff9eSStephen Smalley static int selinux_inode_init_security(struct inode *inode, struct inode *dir, 28389548906bSTetsuo Handa const struct qstr *qstr, 28399548906bSTetsuo Handa const char **name, 28402a7dba39SEric Paris void **value, size_t *len) 28415e41ff9eSStephen Smalley { 28425fb49870SPaul Moore const struct task_security_struct *tsec = current_security(); 28435e41ff9eSStephen Smalley struct superblock_security_struct *sbsec; 2844c0d4f464SCorentin LABBE u32 newsid, clen; 28455e41ff9eSStephen Smalley int rc; 28469548906bSTetsuo Handa char *context; 28475e41ff9eSStephen Smalley 28485e41ff9eSStephen Smalley sbsec = dir->i_sb->s_security; 28495e41ff9eSStephen Smalley 28505e41ff9eSStephen Smalley newsid = tsec->create_sid; 2851275bb41eSDavid Howells 2852c957f6dfSVivek Goyal rc = selinux_determine_inode_label(current_security(), 2853c3c188b2SDavid Howells dir, qstr, 28545e41ff9eSStephen Smalley inode_mode_to_security_class(inode->i_mode), 2855c3c188b2SDavid Howells &newsid); 2856c3c188b2SDavid Howells if (rc) 28575e41ff9eSStephen Smalley return rc; 28585e41ff9eSStephen Smalley 2859296fddf7SEric Paris /* Possibly defer initialization to selinux_complete_init. */ 28600d90a7ecSDavid P. Quigley if (sbsec->flags & SE_SBINITIALIZED) { 2861296fddf7SEric Paris struct inode_security_struct *isec = inode->i_security; 2862296fddf7SEric Paris isec->sclass = inode_mode_to_security_class(inode->i_mode); 2863296fddf7SEric Paris isec->sid = newsid; 28646f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 2865296fddf7SEric Paris } 28665e41ff9eSStephen Smalley 2867aa8e712cSStephen Smalley if (!selinux_state.initialized || !(sbsec->flags & SBLABEL_MNT)) 286825a74f3bSStephen Smalley return -EOPNOTSUPP; 286925a74f3bSStephen Smalley 28709548906bSTetsuo Handa if (name) 28719548906bSTetsuo Handa *name = XATTR_SELINUX_SUFFIX; 28725e41ff9eSStephen Smalley 2873570bc1c2SStephen Smalley if (value && len) { 2874aa8e712cSStephen Smalley rc = security_sid_to_context_force(&selinux_state, newsid, 2875aa8e712cSStephen Smalley &context, &clen); 28769548906bSTetsuo Handa if (rc) 28775e41ff9eSStephen Smalley return rc; 28785e41ff9eSStephen Smalley *value = context; 2879570bc1c2SStephen Smalley *len = clen; 2880570bc1c2SStephen Smalley } 28815e41ff9eSStephen Smalley 28825e41ff9eSStephen Smalley return 0; 28835e41ff9eSStephen Smalley } 28845e41ff9eSStephen Smalley 28854acdaf27SAl Viro static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode) 28861da177e4SLinus Torvalds { 28871da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_FILE); 28881da177e4SLinus Torvalds } 28891da177e4SLinus Torvalds 28901da177e4SLinus Torvalds static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 28911da177e4SLinus Torvalds { 28921da177e4SLinus Torvalds return may_link(dir, old_dentry, MAY_LINK); 28931da177e4SLinus Torvalds } 28941da177e4SLinus Torvalds 28951da177e4SLinus Torvalds static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry) 28961da177e4SLinus Torvalds { 28971da177e4SLinus Torvalds return may_link(dir, dentry, MAY_UNLINK); 28981da177e4SLinus Torvalds } 28991da177e4SLinus Torvalds 29001da177e4SLinus Torvalds static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name) 29011da177e4SLinus Torvalds { 29021da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_LNK_FILE); 29031da177e4SLinus Torvalds } 29041da177e4SLinus Torvalds 290518bb1db3SAl Viro static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask) 29061da177e4SLinus Torvalds { 29071da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_DIR); 29081da177e4SLinus Torvalds } 29091da177e4SLinus Torvalds 29101da177e4SLinus Torvalds static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry) 29111da177e4SLinus Torvalds { 29121da177e4SLinus Torvalds return may_link(dir, dentry, MAY_RMDIR); 29131da177e4SLinus Torvalds } 29141da177e4SLinus Torvalds 29151a67aafbSAl Viro static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) 29161da177e4SLinus Torvalds { 29171da177e4SLinus Torvalds return may_create(dir, dentry, inode_mode_to_security_class(mode)); 29181da177e4SLinus Torvalds } 29191da177e4SLinus Torvalds 29201da177e4SLinus Torvalds static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry, 29211da177e4SLinus Torvalds struct inode *new_inode, struct dentry *new_dentry) 29221da177e4SLinus Torvalds { 29231da177e4SLinus Torvalds return may_rename(old_inode, old_dentry, new_inode, new_dentry); 29241da177e4SLinus Torvalds } 29251da177e4SLinus Torvalds 29261da177e4SLinus Torvalds static int selinux_inode_readlink(struct dentry *dentry) 29271da177e4SLinus Torvalds { 292888e67f3bSDavid Howells const struct cred *cred = current_cred(); 292988e67f3bSDavid Howells 29302875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__READ); 29311da177e4SLinus Torvalds } 29321da177e4SLinus Torvalds 2933bda0be7aSNeilBrown static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode, 2934bda0be7aSNeilBrown bool rcu) 29351da177e4SLinus Torvalds { 293688e67f3bSDavid Howells const struct cred *cred = current_cred(); 2937bda0be7aSNeilBrown struct common_audit_data ad; 2938bda0be7aSNeilBrown struct inode_security_struct *isec; 2939bda0be7aSNeilBrown u32 sid; 29401da177e4SLinus Torvalds 2941bda0be7aSNeilBrown validate_creds(cred); 2942bda0be7aSNeilBrown 2943bda0be7aSNeilBrown ad.type = LSM_AUDIT_DATA_DENTRY; 2944bda0be7aSNeilBrown ad.u.dentry = dentry; 2945bda0be7aSNeilBrown sid = cred_sid(cred); 29465d226df4SAndreas Gruenbacher isec = inode_security_rcu(inode, rcu); 29475d226df4SAndreas Gruenbacher if (IS_ERR(isec)) 29485d226df4SAndreas Gruenbacher return PTR_ERR(isec); 2949bda0be7aSNeilBrown 2950e46e01eeSStephen Smalley return avc_has_perm(&selinux_state, 2951e46e01eeSStephen Smalley sid, isec->sid, isec->sclass, FILE__READ, &ad); 29521da177e4SLinus Torvalds } 29531da177e4SLinus Torvalds 2954d4cf970dSEric Paris static noinline int audit_inode_permission(struct inode *inode, 2955d4cf970dSEric Paris u32 perms, u32 audited, u32 denied, 2956626b9740SStephen Smalley int result, 2957d4cf970dSEric Paris unsigned flags) 2958d4cf970dSEric Paris { 2959d4cf970dSEric Paris struct common_audit_data ad; 2960d4cf970dSEric Paris struct inode_security_struct *isec = inode->i_security; 2961d4cf970dSEric Paris int rc; 2962d4cf970dSEric Paris 296350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_INODE; 2964d4cf970dSEric Paris ad.u.inode = inode; 2965d4cf970dSEric Paris 29666b6bc620SStephen Smalley rc = slow_avc_audit(&selinux_state, 29676b6bc620SStephen Smalley current_sid(), isec->sid, isec->sclass, perms, 2968626b9740SStephen Smalley audited, denied, result, &ad, flags); 2969d4cf970dSEric Paris if (rc) 2970d4cf970dSEric Paris return rc; 2971d4cf970dSEric Paris return 0; 2972d4cf970dSEric Paris } 2973d4cf970dSEric Paris 2974e74f71ebSAl Viro static int selinux_inode_permission(struct inode *inode, int mask) 29751da177e4SLinus Torvalds { 297688e67f3bSDavid Howells const struct cred *cred = current_cred(); 2977b782e0a6SEric Paris u32 perms; 2978b782e0a6SEric Paris bool from_access; 2979cf1dd1daSAl Viro unsigned flags = mask & MAY_NOT_BLOCK; 29802e334057SEric Paris struct inode_security_struct *isec; 29812e334057SEric Paris u32 sid; 29822e334057SEric Paris struct av_decision avd; 29832e334057SEric Paris int rc, rc2; 29842e334057SEric Paris u32 audited, denied; 29851da177e4SLinus Torvalds 2986b782e0a6SEric Paris from_access = mask & MAY_ACCESS; 2987d09ca739SEric Paris mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND); 2988d09ca739SEric Paris 29891da177e4SLinus Torvalds /* No permission to check. Existence test. */ 2990b782e0a6SEric Paris if (!mask) 29911da177e4SLinus Torvalds return 0; 29921da177e4SLinus Torvalds 29932e334057SEric Paris validate_creds(cred); 2994b782e0a6SEric Paris 29952e334057SEric Paris if (unlikely(IS_PRIVATE(inode))) 29962e334057SEric Paris return 0; 2997b782e0a6SEric Paris 2998b782e0a6SEric Paris perms = file_mask_to_av(inode->i_mode, mask); 2999b782e0a6SEric Paris 30002e334057SEric Paris sid = cred_sid(cred); 30015d226df4SAndreas Gruenbacher isec = inode_security_rcu(inode, flags & MAY_NOT_BLOCK); 30025d226df4SAndreas Gruenbacher if (IS_ERR(isec)) 30035d226df4SAndreas Gruenbacher return PTR_ERR(isec); 30042e334057SEric Paris 30056b6bc620SStephen Smalley rc = avc_has_perm_noaudit(&selinux_state, 30063a28cff3SStephen Smalley sid, isec->sid, isec->sclass, perms, 30073a28cff3SStephen Smalley (flags & MAY_NOT_BLOCK) ? AVC_NONBLOCKING : 0, 30083a28cff3SStephen Smalley &avd); 30092e334057SEric Paris audited = avc_audit_required(perms, &avd, rc, 30102e334057SEric Paris from_access ? FILE__AUDIT_ACCESS : 0, 30112e334057SEric Paris &denied); 30122e334057SEric Paris if (likely(!audited)) 30132e334057SEric Paris return rc; 30142e334057SEric Paris 3015626b9740SStephen Smalley rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags); 30162e334057SEric Paris if (rc2) 30172e334057SEric Paris return rc2; 30182e334057SEric Paris return rc; 30191da177e4SLinus Torvalds } 30201da177e4SLinus Torvalds 30211da177e4SLinus Torvalds static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr) 30221da177e4SLinus Torvalds { 302388e67f3bSDavid Howells const struct cred *cred = current_cred(); 3024ccb54478SStephen Smalley struct inode *inode = d_backing_inode(dentry); 3025bc6a6008SAmerigo Wang unsigned int ia_valid = iattr->ia_valid; 302695dbf739SEric Paris __u32 av = FILE__WRITE; 30271da177e4SLinus Torvalds 3028bc6a6008SAmerigo Wang /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */ 3029bc6a6008SAmerigo Wang if (ia_valid & ATTR_FORCE) { 3030bc6a6008SAmerigo Wang ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE | 3031bc6a6008SAmerigo Wang ATTR_FORCE); 3032bc6a6008SAmerigo Wang if (!ia_valid) 30331da177e4SLinus Torvalds return 0; 3034bc6a6008SAmerigo Wang } 30351da177e4SLinus Torvalds 3036bc6a6008SAmerigo Wang if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | 3037bc6a6008SAmerigo Wang ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET)) 30382875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__SETATTR); 30391da177e4SLinus Torvalds 3040aa8e712cSStephen Smalley if (selinux_policycap_openperm() && 3041ccb54478SStephen Smalley inode->i_sb->s_magic != SOCKFS_MAGIC && 3042ccb54478SStephen Smalley (ia_valid & ATTR_SIZE) && 3043ccb54478SStephen Smalley !(ia_valid & ATTR_FILE)) 304495dbf739SEric Paris av |= FILE__OPEN; 304595dbf739SEric Paris 304695dbf739SEric Paris return dentry_has_perm(cred, dentry, av); 30471da177e4SLinus Torvalds } 30481da177e4SLinus Torvalds 30493f7036a0SAl Viro static int selinux_inode_getattr(const struct path *path) 30501da177e4SLinus Torvalds { 30513f7036a0SAl Viro return path_has_perm(current_cred(), path, FILE__GETATTR); 30521da177e4SLinus Torvalds } 30531da177e4SLinus Torvalds 3054db59000aSStephen Smalley static bool has_cap_mac_admin(bool audit) 3055db59000aSStephen Smalley { 3056db59000aSStephen Smalley const struct cred *cred = current_cred(); 3057db59000aSStephen Smalley int cap_audit = audit ? SECURITY_CAP_AUDIT : SECURITY_CAP_NOAUDIT; 3058db59000aSStephen Smalley 3059db59000aSStephen Smalley if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, cap_audit)) 3060db59000aSStephen Smalley return false; 3061db59000aSStephen Smalley if (cred_has_capability(cred, CAP_MAC_ADMIN, cap_audit, true)) 3062db59000aSStephen Smalley return false; 3063db59000aSStephen Smalley return true; 3064db59000aSStephen Smalley } 3065db59000aSStephen Smalley 30668f0cfa52SDavid Howells static int selinux_inode_setxattr(struct dentry *dentry, const char *name, 30678f0cfa52SDavid Howells const void *value, size_t size, int flags) 30681da177e4SLinus Torvalds { 3069c6f493d6SDavid Howells struct inode *inode = d_backing_inode(dentry); 307020cdef8dSPaul Moore struct inode_security_struct *isec; 30711da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 30722bf49690SThomas Liu struct common_audit_data ad; 3073275bb41eSDavid Howells u32 newsid, sid = current_sid(); 30741da177e4SLinus Torvalds int rc = 0; 30751da177e4SLinus Torvalds 30766b240306SEric W. Biederman if (strcmp(name, XATTR_NAME_SELINUX)) { 30776b240306SEric W. Biederman rc = cap_inode_setxattr(dentry, name, value, size, flags); 30786b240306SEric W. Biederman if (rc) 30796b240306SEric W. Biederman return rc; 30806b240306SEric W. Biederman 30816b240306SEric W. Biederman /* Not an attribute we recognize, so just check the 30826b240306SEric W. Biederman ordinary setattr permission. */ 30836b240306SEric W. Biederman return dentry_has_perm(current_cred(), dentry, FILE__SETATTR); 30846b240306SEric W. Biederman } 30851da177e4SLinus Torvalds 30861da177e4SLinus Torvalds sbsec = inode->i_sb->s_security; 308712f348b9SEric Paris if (!(sbsec->flags & SBLABEL_MNT)) 30881da177e4SLinus Torvalds return -EOPNOTSUPP; 30891da177e4SLinus Torvalds 30902e149670SSerge E. Hallyn if (!inode_owner_or_capable(inode)) 30911da177e4SLinus Torvalds return -EPERM; 30921da177e4SLinus Torvalds 309350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 3094a269434dSEric Paris ad.u.dentry = dentry; 30951da177e4SLinus Torvalds 309620cdef8dSPaul Moore isec = backing_inode_security(dentry); 30976b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 30986b6bc620SStephen Smalley sid, isec->sid, isec->sclass, 30991da177e4SLinus Torvalds FILE__RELABELFROM, &ad); 31001da177e4SLinus Torvalds if (rc) 31011da177e4SLinus Torvalds return rc; 31021da177e4SLinus Torvalds 3103aa8e712cSStephen Smalley rc = security_context_to_sid(&selinux_state, value, size, &newsid, 3104aa8e712cSStephen Smalley GFP_KERNEL); 310512b29f34SStephen Smalley if (rc == -EINVAL) { 3106db59000aSStephen Smalley if (!has_cap_mac_admin(true)) { 3107d6ea83ecSEric Paris struct audit_buffer *ab; 3108d6ea83ecSEric Paris size_t audit_size; 3109d6ea83ecSEric Paris 3110d6ea83ecSEric Paris /* We strip a nul only if it is at the end, otherwise the 3111d6ea83ecSEric Paris * context contains a nul and we should audit that */ 3112e3fea3f7SAl Viro if (value) { 3113add24372SColin Ian King const char *str = value; 3114add24372SColin Ian King 3115d6ea83ecSEric Paris if (str[size - 1] == '\0') 3116d6ea83ecSEric Paris audit_size = size - 1; 3117d6ea83ecSEric Paris else 3118d6ea83ecSEric Paris audit_size = size; 3119e3fea3f7SAl Viro } else { 3120e3fea3f7SAl Viro audit_size = 0; 3121e3fea3f7SAl Viro } 3122cdfb6b34SRichard Guy Briggs ab = audit_log_start(audit_context(), 3123cdfb6b34SRichard Guy Briggs GFP_ATOMIC, AUDIT_SELINUX_ERR); 3124d6ea83ecSEric Paris audit_log_format(ab, "op=setxattr invalid_context="); 3125d6ea83ecSEric Paris audit_log_n_untrustedstring(ab, value, audit_size); 3126d6ea83ecSEric Paris audit_log_end(ab); 3127d6ea83ecSEric Paris 312812b29f34SStephen Smalley return rc; 3129d6ea83ecSEric Paris } 3130aa8e712cSStephen Smalley rc = security_context_to_sid_force(&selinux_state, value, 3131aa8e712cSStephen Smalley size, &newsid); 313212b29f34SStephen Smalley } 31331da177e4SLinus Torvalds if (rc) 31341da177e4SLinus Torvalds return rc; 31351da177e4SLinus Torvalds 31366b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 31376b6bc620SStephen Smalley sid, newsid, isec->sclass, 31381da177e4SLinus Torvalds FILE__RELABELTO, &ad); 31391da177e4SLinus Torvalds if (rc) 31401da177e4SLinus Torvalds return rc; 31411da177e4SLinus Torvalds 3142aa8e712cSStephen Smalley rc = security_validate_transition(&selinux_state, isec->sid, newsid, 3143aa8e712cSStephen Smalley sid, isec->sclass); 31441da177e4SLinus Torvalds if (rc) 31451da177e4SLinus Torvalds return rc; 31461da177e4SLinus Torvalds 31476b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 31486b6bc620SStephen Smalley newsid, 31491da177e4SLinus Torvalds sbsec->sid, 31501da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 31511da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, 31521da177e4SLinus Torvalds &ad); 31531da177e4SLinus Torvalds } 31541da177e4SLinus Torvalds 31558f0cfa52SDavid Howells static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name, 31568f0cfa52SDavid Howells const void *value, size_t size, 31578f0cfa52SDavid Howells int flags) 31581da177e4SLinus Torvalds { 3159c6f493d6SDavid Howells struct inode *inode = d_backing_inode(dentry); 316020cdef8dSPaul Moore struct inode_security_struct *isec; 31611da177e4SLinus Torvalds u32 newsid; 31621da177e4SLinus Torvalds int rc; 31631da177e4SLinus Torvalds 31641da177e4SLinus Torvalds if (strcmp(name, XATTR_NAME_SELINUX)) { 31651da177e4SLinus Torvalds /* Not an attribute we recognize, so nothing to do. */ 31661da177e4SLinus Torvalds return; 31671da177e4SLinus Torvalds } 31681da177e4SLinus Torvalds 3169aa8e712cSStephen Smalley rc = security_context_to_sid_force(&selinux_state, value, size, 3170aa8e712cSStephen Smalley &newsid); 31711da177e4SLinus Torvalds if (rc) { 3172c103a91eSpeter enderborg pr_err("SELinux: unable to map context to SID" 317312b29f34SStephen Smalley "for (%s, %lu), rc=%d\n", 317412b29f34SStephen Smalley inode->i_sb->s_id, inode->i_ino, -rc); 31751da177e4SLinus Torvalds return; 31761da177e4SLinus Torvalds } 31771da177e4SLinus Torvalds 317820cdef8dSPaul Moore isec = backing_inode_security(dentry); 31799287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 3180aa9c2669SDavid Quigley isec->sclass = inode_mode_to_security_class(inode->i_mode); 31811da177e4SLinus Torvalds isec->sid = newsid; 31826f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 31839287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 3184aa9c2669SDavid Quigley 31851da177e4SLinus Torvalds return; 31861da177e4SLinus Torvalds } 31871da177e4SLinus Torvalds 31888f0cfa52SDavid Howells static int selinux_inode_getxattr(struct dentry *dentry, const char *name) 31891da177e4SLinus Torvalds { 319088e67f3bSDavid Howells const struct cred *cred = current_cred(); 319188e67f3bSDavid Howells 31922875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__GETATTR); 31931da177e4SLinus Torvalds } 31941da177e4SLinus Torvalds 31951da177e4SLinus Torvalds static int selinux_inode_listxattr(struct dentry *dentry) 31961da177e4SLinus Torvalds { 319788e67f3bSDavid Howells const struct cred *cred = current_cred(); 319888e67f3bSDavid Howells 31992875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__GETATTR); 32001da177e4SLinus Torvalds } 32011da177e4SLinus Torvalds 32028f0cfa52SDavid Howells static int selinux_inode_removexattr(struct dentry *dentry, const char *name) 32031da177e4SLinus Torvalds { 32046b240306SEric W. Biederman if (strcmp(name, XATTR_NAME_SELINUX)) { 32056b240306SEric W. Biederman int rc = cap_inode_removexattr(dentry, name); 32066b240306SEric W. Biederman if (rc) 32076b240306SEric W. Biederman return rc; 32086b240306SEric W. Biederman 32096b240306SEric W. Biederman /* Not an attribute we recognize, so just check the 32106b240306SEric W. Biederman ordinary setattr permission. */ 32116b240306SEric W. Biederman return dentry_has_perm(current_cred(), dentry, FILE__SETATTR); 32126b240306SEric W. Biederman } 32131da177e4SLinus Torvalds 32141da177e4SLinus Torvalds /* No one is allowed to remove a SELinux security label. 32151da177e4SLinus Torvalds You can change the label, but all data must be labeled. */ 32161da177e4SLinus Torvalds return -EACCES; 32171da177e4SLinus Torvalds } 32181da177e4SLinus Torvalds 3219d381d8a9SJames Morris /* 3220abc69bb6SStephen Smalley * Copy the inode security context value to the user. 3221d381d8a9SJames Morris * 3222d381d8a9SJames Morris * Permission check is handled by selinux_inode_getxattr hook. 3223d381d8a9SJames Morris */ 3224ea861dfdSAndreas Gruenbacher static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc) 32251da177e4SLinus Torvalds { 322642492594SDavid P. Quigley u32 size; 322742492594SDavid P. Quigley int error; 322842492594SDavid P. Quigley char *context = NULL; 322920cdef8dSPaul Moore struct inode_security_struct *isec; 32301da177e4SLinus Torvalds 32318c8570fbSDustin Kirkland if (strcmp(name, XATTR_SELINUX_SUFFIX)) 32328c8570fbSDustin Kirkland return -EOPNOTSUPP; 32331da177e4SLinus Torvalds 3234abc69bb6SStephen Smalley /* 3235abc69bb6SStephen Smalley * If the caller has CAP_MAC_ADMIN, then get the raw context 3236abc69bb6SStephen Smalley * value even if it is not defined by current policy; otherwise, 3237abc69bb6SStephen Smalley * use the in-core value under current policy. 3238abc69bb6SStephen Smalley * Use the non-auditing forms of the permission checks since 3239abc69bb6SStephen Smalley * getxattr may be called by unprivileged processes commonly 3240abc69bb6SStephen Smalley * and lack of permission just means that we fall back to the 3241abc69bb6SStephen Smalley * in-core context value, not a denial. 3242abc69bb6SStephen Smalley */ 324320cdef8dSPaul Moore isec = inode_security(inode); 3244db59000aSStephen Smalley if (has_cap_mac_admin(false)) 3245aa8e712cSStephen Smalley error = security_sid_to_context_force(&selinux_state, 3246aa8e712cSStephen Smalley isec->sid, &context, 3247abc69bb6SStephen Smalley &size); 3248abc69bb6SStephen Smalley else 3249aa8e712cSStephen Smalley error = security_sid_to_context(&selinux_state, isec->sid, 3250aa8e712cSStephen Smalley &context, &size); 325142492594SDavid P. Quigley if (error) 325242492594SDavid P. Quigley return error; 325342492594SDavid P. Quigley error = size; 325442492594SDavid P. Quigley if (alloc) { 325542492594SDavid P. Quigley *buffer = context; 325642492594SDavid P. Quigley goto out_nofree; 325742492594SDavid P. Quigley } 325842492594SDavid P. Quigley kfree(context); 325942492594SDavid P. Quigley out_nofree: 326042492594SDavid P. Quigley return error; 32611da177e4SLinus Torvalds } 32621da177e4SLinus Torvalds 32631da177e4SLinus Torvalds static int selinux_inode_setsecurity(struct inode *inode, const char *name, 32641da177e4SLinus Torvalds const void *value, size_t size, int flags) 32651da177e4SLinus Torvalds { 32662c97165bSPaul Moore struct inode_security_struct *isec = inode_security_novalidate(inode); 326753e0c2aaSOndrej Mosnacek struct superblock_security_struct *sbsec = inode->i_sb->s_security; 32681da177e4SLinus Torvalds u32 newsid; 32691da177e4SLinus Torvalds int rc; 32701da177e4SLinus Torvalds 32711da177e4SLinus Torvalds if (strcmp(name, XATTR_SELINUX_SUFFIX)) 32721da177e4SLinus Torvalds return -EOPNOTSUPP; 32731da177e4SLinus Torvalds 327453e0c2aaSOndrej Mosnacek if (!(sbsec->flags & SBLABEL_MNT)) 327553e0c2aaSOndrej Mosnacek return -EOPNOTSUPP; 327653e0c2aaSOndrej Mosnacek 32771da177e4SLinus Torvalds if (!value || !size) 32781da177e4SLinus Torvalds return -EACCES; 32791da177e4SLinus Torvalds 3280aa8e712cSStephen Smalley rc = security_context_to_sid(&selinux_state, value, size, &newsid, 3281aa8e712cSStephen Smalley GFP_KERNEL); 32821da177e4SLinus Torvalds if (rc) 32831da177e4SLinus Torvalds return rc; 32841da177e4SLinus Torvalds 32859287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 3286aa9c2669SDavid Quigley isec->sclass = inode_mode_to_security_class(inode->i_mode); 32871da177e4SLinus Torvalds isec->sid = newsid; 32886f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 32899287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 32901da177e4SLinus Torvalds return 0; 32911da177e4SLinus Torvalds } 32921da177e4SLinus Torvalds 32931da177e4SLinus Torvalds static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size) 32941da177e4SLinus Torvalds { 32951da177e4SLinus Torvalds const int len = sizeof(XATTR_NAME_SELINUX); 32961da177e4SLinus Torvalds if (buffer && len <= buffer_size) 32971da177e4SLinus Torvalds memcpy(buffer, XATTR_NAME_SELINUX, len); 32981da177e4SLinus Torvalds return len; 32991da177e4SLinus Torvalds } 33001da177e4SLinus Torvalds 3301d6335d77SAndreas Gruenbacher static void selinux_inode_getsecid(struct inode *inode, u32 *secid) 3302713a04aeSAhmed S. Darwish { 3303e817c2f3SAndreas Gruenbacher struct inode_security_struct *isec = inode_security_novalidate(inode); 3304713a04aeSAhmed S. Darwish *secid = isec->sid; 3305713a04aeSAhmed S. Darwish } 3306713a04aeSAhmed S. Darwish 330756909eb3SVivek Goyal static int selinux_inode_copy_up(struct dentry *src, struct cred **new) 330856909eb3SVivek Goyal { 330956909eb3SVivek Goyal u32 sid; 331056909eb3SVivek Goyal struct task_security_struct *tsec; 331156909eb3SVivek Goyal struct cred *new_creds = *new; 331256909eb3SVivek Goyal 331356909eb3SVivek Goyal if (new_creds == NULL) { 331456909eb3SVivek Goyal new_creds = prepare_creds(); 331556909eb3SVivek Goyal if (!new_creds) 331656909eb3SVivek Goyal return -ENOMEM; 331756909eb3SVivek Goyal } 331856909eb3SVivek Goyal 331956909eb3SVivek Goyal tsec = new_creds->security; 332056909eb3SVivek Goyal /* Get label from overlay inode and set it in create_sid */ 332156909eb3SVivek Goyal selinux_inode_getsecid(d_inode(src), &sid); 332256909eb3SVivek Goyal tsec->create_sid = sid; 332356909eb3SVivek Goyal *new = new_creds; 332456909eb3SVivek Goyal return 0; 332556909eb3SVivek Goyal } 332656909eb3SVivek Goyal 332719472b69SVivek Goyal static int selinux_inode_copy_up_xattr(const char *name) 332819472b69SVivek Goyal { 332919472b69SVivek Goyal /* The copy_up hook above sets the initial context on an inode, but we 333019472b69SVivek Goyal * don't then want to overwrite it by blindly copying all the lower 333119472b69SVivek Goyal * xattrs up. Instead, we have to filter out SELinux-related xattrs. 333219472b69SVivek Goyal */ 333319472b69SVivek Goyal if (strcmp(name, XATTR_NAME_SELINUX) == 0) 333419472b69SVivek Goyal return 1; /* Discard */ 333519472b69SVivek Goyal /* 333619472b69SVivek Goyal * Any other attribute apart from SELINUX is not claimed, supported 333719472b69SVivek Goyal * by selinux. 333819472b69SVivek Goyal */ 333919472b69SVivek Goyal return -EOPNOTSUPP; 334019472b69SVivek Goyal } 334119472b69SVivek Goyal 33421da177e4SLinus Torvalds /* file security operations */ 33431da177e4SLinus Torvalds 3344788e7dd4SYuichi Nakamura static int selinux_revalidate_file_permission(struct file *file, int mask) 33451da177e4SLinus Torvalds { 334688e67f3bSDavid Howells const struct cred *cred = current_cred(); 3347496ad9aaSAl Viro struct inode *inode = file_inode(file); 33481da177e4SLinus Torvalds 33491da177e4SLinus Torvalds /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */ 33501da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE)) 33511da177e4SLinus Torvalds mask |= MAY_APPEND; 33521da177e4SLinus Torvalds 3353389fb800SPaul Moore return file_has_perm(cred, file, 33541da177e4SLinus Torvalds file_mask_to_av(inode->i_mode, mask)); 33551da177e4SLinus Torvalds } 33561da177e4SLinus Torvalds 3357788e7dd4SYuichi Nakamura static int selinux_file_permission(struct file *file, int mask) 3358788e7dd4SYuichi Nakamura { 3359496ad9aaSAl Viro struct inode *inode = file_inode(file); 336020dda18bSStephen Smalley struct file_security_struct *fsec = file->f_security; 3361b197367eSAndreas Gruenbacher struct inode_security_struct *isec; 336220dda18bSStephen Smalley u32 sid = current_sid(); 336320dda18bSStephen Smalley 3364389fb800SPaul Moore if (!mask) 3365788e7dd4SYuichi Nakamura /* No permission to check. Existence test. */ 3366788e7dd4SYuichi Nakamura return 0; 3367788e7dd4SYuichi Nakamura 3368b197367eSAndreas Gruenbacher isec = inode_security(inode); 336920dda18bSStephen Smalley if (sid == fsec->sid && fsec->isid == isec->sid && 33706b6bc620SStephen Smalley fsec->pseqno == avc_policy_seqno(&selinux_state)) 337183d49856SEric Paris /* No change since file_open check. */ 337220dda18bSStephen Smalley return 0; 337320dda18bSStephen Smalley 3374788e7dd4SYuichi Nakamura return selinux_revalidate_file_permission(file, mask); 3375788e7dd4SYuichi Nakamura } 3376788e7dd4SYuichi Nakamura 33771da177e4SLinus Torvalds static int selinux_file_alloc_security(struct file *file) 33781da177e4SLinus Torvalds { 33791da177e4SLinus Torvalds return file_alloc_security(file); 33801da177e4SLinus Torvalds } 33811da177e4SLinus Torvalds 33821da177e4SLinus Torvalds static void selinux_file_free_security(struct file *file) 33831da177e4SLinus Torvalds { 33841da177e4SLinus Torvalds file_free_security(file); 33851da177e4SLinus Torvalds } 33861da177e4SLinus Torvalds 3387fa1aa143SJeff Vander Stoep /* 3388fa1aa143SJeff Vander Stoep * Check whether a task has the ioctl permission and cmd 3389fa1aa143SJeff Vander Stoep * operation to an inode. 3390fa1aa143SJeff Vander Stoep */ 33911d2a168aSGeliang Tang static int ioctl_has_perm(const struct cred *cred, struct file *file, 3392fa1aa143SJeff Vander Stoep u32 requested, u16 cmd) 3393fa1aa143SJeff Vander Stoep { 3394fa1aa143SJeff Vander Stoep struct common_audit_data ad; 3395fa1aa143SJeff Vander Stoep struct file_security_struct *fsec = file->f_security; 3396fa1aa143SJeff Vander Stoep struct inode *inode = file_inode(file); 339720cdef8dSPaul Moore struct inode_security_struct *isec; 3398fa1aa143SJeff Vander Stoep struct lsm_ioctlop_audit ioctl; 3399fa1aa143SJeff Vander Stoep u32 ssid = cred_sid(cred); 3400fa1aa143SJeff Vander Stoep int rc; 3401fa1aa143SJeff Vander Stoep u8 driver = cmd >> 8; 3402fa1aa143SJeff Vander Stoep u8 xperm = cmd & 0xff; 3403fa1aa143SJeff Vander Stoep 3404fa1aa143SJeff Vander Stoep ad.type = LSM_AUDIT_DATA_IOCTL_OP; 3405fa1aa143SJeff Vander Stoep ad.u.op = &ioctl; 3406fa1aa143SJeff Vander Stoep ad.u.op->cmd = cmd; 3407fa1aa143SJeff Vander Stoep ad.u.op->path = file->f_path; 3408fa1aa143SJeff Vander Stoep 3409fa1aa143SJeff Vander Stoep if (ssid != fsec->sid) { 34106b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 34116b6bc620SStephen Smalley ssid, fsec->sid, 3412fa1aa143SJeff Vander Stoep SECCLASS_FD, 3413fa1aa143SJeff Vander Stoep FD__USE, 3414fa1aa143SJeff Vander Stoep &ad); 3415fa1aa143SJeff Vander Stoep if (rc) 3416fa1aa143SJeff Vander Stoep goto out; 3417fa1aa143SJeff Vander Stoep } 3418fa1aa143SJeff Vander Stoep 3419fa1aa143SJeff Vander Stoep if (unlikely(IS_PRIVATE(inode))) 3420fa1aa143SJeff Vander Stoep return 0; 3421fa1aa143SJeff Vander Stoep 342220cdef8dSPaul Moore isec = inode_security(inode); 34236b6bc620SStephen Smalley rc = avc_has_extended_perms(&selinux_state, 34246b6bc620SStephen Smalley ssid, isec->sid, isec->sclass, 3425fa1aa143SJeff Vander Stoep requested, driver, xperm, &ad); 3426fa1aa143SJeff Vander Stoep out: 3427fa1aa143SJeff Vander Stoep return rc; 3428fa1aa143SJeff Vander Stoep } 3429fa1aa143SJeff Vander Stoep 34301da177e4SLinus Torvalds static int selinux_file_ioctl(struct file *file, unsigned int cmd, 34311da177e4SLinus Torvalds unsigned long arg) 34321da177e4SLinus Torvalds { 343388e67f3bSDavid Howells const struct cred *cred = current_cred(); 34340b24dcb7SEric Paris int error = 0; 34351da177e4SLinus Torvalds 34360b24dcb7SEric Paris switch (cmd) { 34370b24dcb7SEric Paris case FIONREAD: 34380b24dcb7SEric Paris /* fall through */ 34390b24dcb7SEric Paris case FIBMAP: 34400b24dcb7SEric Paris /* fall through */ 34410b24dcb7SEric Paris case FIGETBSZ: 34420b24dcb7SEric Paris /* fall through */ 34432f99c369SAl Viro case FS_IOC_GETFLAGS: 34440b24dcb7SEric Paris /* fall through */ 34452f99c369SAl Viro case FS_IOC_GETVERSION: 34460b24dcb7SEric Paris error = file_has_perm(cred, file, FILE__GETATTR); 34470b24dcb7SEric Paris break; 34481da177e4SLinus Torvalds 34492f99c369SAl Viro case FS_IOC_SETFLAGS: 34500b24dcb7SEric Paris /* fall through */ 34512f99c369SAl Viro case FS_IOC_SETVERSION: 34520b24dcb7SEric Paris error = file_has_perm(cred, file, FILE__SETATTR); 34530b24dcb7SEric Paris break; 34540b24dcb7SEric Paris 34550b24dcb7SEric Paris /* sys_ioctl() checks */ 34560b24dcb7SEric Paris case FIONBIO: 34570b24dcb7SEric Paris /* fall through */ 34580b24dcb7SEric Paris case FIOASYNC: 34590b24dcb7SEric Paris error = file_has_perm(cred, file, 0); 34600b24dcb7SEric Paris break; 34610b24dcb7SEric Paris 34620b24dcb7SEric Paris case KDSKBENT: 34630b24dcb7SEric Paris case KDSKBSENT: 34646a9de491SEric Paris error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG, 34658e4ff6f2SStephen Smalley SECURITY_CAP_AUDIT, true); 34660b24dcb7SEric Paris break; 34670b24dcb7SEric Paris 34680b24dcb7SEric Paris /* default case assumes that the command will go 34690b24dcb7SEric Paris * to the file's ioctl() function. 34700b24dcb7SEric Paris */ 34710b24dcb7SEric Paris default: 3472fa1aa143SJeff Vander Stoep error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd); 34730b24dcb7SEric Paris } 34740b24dcb7SEric Paris return error; 34751da177e4SLinus Torvalds } 34761da177e4SLinus Torvalds 3477fcaaade1SStephen Smalley static int default_noexec; 3478fcaaade1SStephen Smalley 34791da177e4SLinus Torvalds static int file_map_prot_check(struct file *file, unsigned long prot, int shared) 34801da177e4SLinus Torvalds { 348188e67f3bSDavid Howells const struct cred *cred = current_cred(); 3482be0554c9SStephen Smalley u32 sid = cred_sid(cred); 3483d84f4f99SDavid Howells int rc = 0; 348488e67f3bSDavid Howells 3485fcaaade1SStephen Smalley if (default_noexec && 3486892e8cacSStephen Smalley (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) || 3487892e8cacSStephen Smalley (!shared && (prot & PROT_WRITE)))) { 34881da177e4SLinus Torvalds /* 34891da177e4SLinus Torvalds * We are making executable an anonymous mapping or a 34901da177e4SLinus Torvalds * private file mapping that will also be writable. 34911da177e4SLinus Torvalds * This has an additional check. 34921da177e4SLinus Torvalds */ 34936b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 34946b6bc620SStephen Smalley sid, sid, SECCLASS_PROCESS, 3495be0554c9SStephen Smalley PROCESS__EXECMEM, NULL); 34961da177e4SLinus Torvalds if (rc) 3497d84f4f99SDavid Howells goto error; 34981da177e4SLinus Torvalds } 34991da177e4SLinus Torvalds 35001da177e4SLinus Torvalds if (file) { 35011da177e4SLinus Torvalds /* read access is always possible with a mapping */ 35021da177e4SLinus Torvalds u32 av = FILE__READ; 35031da177e4SLinus Torvalds 35041da177e4SLinus Torvalds /* write access only matters if the mapping is shared */ 35051da177e4SLinus Torvalds if (shared && (prot & PROT_WRITE)) 35061da177e4SLinus Torvalds av |= FILE__WRITE; 35071da177e4SLinus Torvalds 35081da177e4SLinus Torvalds if (prot & PROT_EXEC) 35091da177e4SLinus Torvalds av |= FILE__EXECUTE; 35101da177e4SLinus Torvalds 351188e67f3bSDavid Howells return file_has_perm(cred, file, av); 35121da177e4SLinus Torvalds } 3513d84f4f99SDavid Howells 3514d84f4f99SDavid Howells error: 3515d84f4f99SDavid Howells return rc; 35161da177e4SLinus Torvalds } 35171da177e4SLinus Torvalds 3518e5467859SAl Viro static int selinux_mmap_addr(unsigned long addr) 35191da177e4SLinus Torvalds { 3520b1d9e6b0SCasey Schaufler int rc = 0; 352198883bfdSPaul Moore 352298883bfdSPaul Moore if (addr < CONFIG_LSM_MMAP_MIN_ADDR) { 352398883bfdSPaul Moore u32 sid = current_sid(); 35246b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 35256b6bc620SStephen Smalley sid, sid, SECCLASS_MEMPROTECT, 352698883bfdSPaul Moore MEMPROTECT__MMAP_ZERO, NULL); 352798883bfdSPaul Moore } 352898883bfdSPaul Moore 352998883bfdSPaul Moore return rc; 3530e5467859SAl Viro } 35311da177e4SLinus Torvalds 3532e5467859SAl Viro static int selinux_mmap_file(struct file *file, unsigned long reqprot, 3533e5467859SAl Viro unsigned long prot, unsigned long flags) 3534e5467859SAl Viro { 35353ba4bf5fSStephen Smalley struct common_audit_data ad; 35363ba4bf5fSStephen Smalley int rc; 35373ba4bf5fSStephen Smalley 35383ba4bf5fSStephen Smalley if (file) { 35393ba4bf5fSStephen Smalley ad.type = LSM_AUDIT_DATA_FILE; 35403ba4bf5fSStephen Smalley ad.u.file = file; 35413ba4bf5fSStephen Smalley rc = inode_has_perm(current_cred(), file_inode(file), 35423ba4bf5fSStephen Smalley FILE__MAP, &ad); 35433ba4bf5fSStephen Smalley if (rc) 35443ba4bf5fSStephen Smalley return rc; 35453ba4bf5fSStephen Smalley } 35463ba4bf5fSStephen Smalley 3547aa8e712cSStephen Smalley if (selinux_state.checkreqprot) 35481da177e4SLinus Torvalds prot = reqprot; 35491da177e4SLinus Torvalds 35501da177e4SLinus Torvalds return file_map_prot_check(file, prot, 35511da177e4SLinus Torvalds (flags & MAP_TYPE) == MAP_SHARED); 35521da177e4SLinus Torvalds } 35531da177e4SLinus Torvalds 35541da177e4SLinus Torvalds static int selinux_file_mprotect(struct vm_area_struct *vma, 35551da177e4SLinus Torvalds unsigned long reqprot, 35561da177e4SLinus Torvalds unsigned long prot) 35571da177e4SLinus Torvalds { 355888e67f3bSDavid Howells const struct cred *cred = current_cred(); 3559be0554c9SStephen Smalley u32 sid = cred_sid(cred); 35601da177e4SLinus Torvalds 3561aa8e712cSStephen Smalley if (selinux_state.checkreqprot) 35621da177e4SLinus Torvalds prot = reqprot; 35631da177e4SLinus Torvalds 3564fcaaade1SStephen Smalley if (default_noexec && 3565fcaaade1SStephen Smalley (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) { 3566d541bbeeSJames Morris int rc = 0; 3567db4c9641SStephen Smalley if (vma->vm_start >= vma->vm_mm->start_brk && 3568db4c9641SStephen Smalley vma->vm_end <= vma->vm_mm->brk) { 35696b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 35706b6bc620SStephen Smalley sid, sid, SECCLASS_PROCESS, 3571be0554c9SStephen Smalley PROCESS__EXECHEAP, NULL); 3572db4c9641SStephen Smalley } else if (!vma->vm_file && 3573c2316dbfSStephen Smalley ((vma->vm_start <= vma->vm_mm->start_stack && 3574c2316dbfSStephen Smalley vma->vm_end >= vma->vm_mm->start_stack) || 3575d17af505SAndy Lutomirski vma_is_stack_for_current(vma))) { 35766b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 35776b6bc620SStephen Smalley sid, sid, SECCLASS_PROCESS, 3578be0554c9SStephen Smalley PROCESS__EXECSTACK, NULL); 3579db4c9641SStephen Smalley } else if (vma->vm_file && vma->anon_vma) { 3580db4c9641SStephen Smalley /* 3581db4c9641SStephen Smalley * We are making executable a file mapping that has 3582db4c9641SStephen Smalley * had some COW done. Since pages might have been 3583db4c9641SStephen Smalley * written, check ability to execute the possibly 3584db4c9641SStephen Smalley * modified content. This typically should only 3585db4c9641SStephen Smalley * occur for text relocations. 3586db4c9641SStephen Smalley */ 3587d84f4f99SDavid Howells rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD); 3588db4c9641SStephen Smalley } 35896b992197SLorenzo Hernandez García-Hierro if (rc) 35906b992197SLorenzo Hernandez García-Hierro return rc; 35916b992197SLorenzo Hernandez García-Hierro } 35921da177e4SLinus Torvalds 35931da177e4SLinus Torvalds return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED); 35941da177e4SLinus Torvalds } 35951da177e4SLinus Torvalds 35961da177e4SLinus Torvalds static int selinux_file_lock(struct file *file, unsigned int cmd) 35971da177e4SLinus Torvalds { 359888e67f3bSDavid Howells const struct cred *cred = current_cred(); 359988e67f3bSDavid Howells 360088e67f3bSDavid Howells return file_has_perm(cred, file, FILE__LOCK); 36011da177e4SLinus Torvalds } 36021da177e4SLinus Torvalds 36031da177e4SLinus Torvalds static int selinux_file_fcntl(struct file *file, unsigned int cmd, 36041da177e4SLinus Torvalds unsigned long arg) 36051da177e4SLinus Torvalds { 360688e67f3bSDavid Howells const struct cred *cred = current_cred(); 36071da177e4SLinus Torvalds int err = 0; 36081da177e4SLinus Torvalds 36091da177e4SLinus Torvalds switch (cmd) { 36101da177e4SLinus Torvalds case F_SETFL: 36111da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) { 361288e67f3bSDavid Howells err = file_has_perm(cred, file, FILE__WRITE); 36131da177e4SLinus Torvalds break; 36141da177e4SLinus Torvalds } 36151da177e4SLinus Torvalds /* fall through */ 36161da177e4SLinus Torvalds case F_SETOWN: 36171da177e4SLinus Torvalds case F_SETSIG: 36181da177e4SLinus Torvalds case F_GETFL: 36191da177e4SLinus Torvalds case F_GETOWN: 36201da177e4SLinus Torvalds case F_GETSIG: 36211d151c33SCyrill Gorcunov case F_GETOWNER_UIDS: 36221da177e4SLinus Torvalds /* Just check FD__USE permission */ 362388e67f3bSDavid Howells err = file_has_perm(cred, file, 0); 36241da177e4SLinus Torvalds break; 36251da177e4SLinus Torvalds case F_GETLK: 36261da177e4SLinus Torvalds case F_SETLK: 36271da177e4SLinus Torvalds case F_SETLKW: 36280d3f7a2dSJeff Layton case F_OFD_GETLK: 36290d3f7a2dSJeff Layton case F_OFD_SETLK: 36300d3f7a2dSJeff Layton case F_OFD_SETLKW: 36311da177e4SLinus Torvalds #if BITS_PER_LONG == 32 36321da177e4SLinus Torvalds case F_GETLK64: 36331da177e4SLinus Torvalds case F_SETLK64: 36341da177e4SLinus Torvalds case F_SETLKW64: 36351da177e4SLinus Torvalds #endif 363688e67f3bSDavid Howells err = file_has_perm(cred, file, FILE__LOCK); 36371da177e4SLinus Torvalds break; 36381da177e4SLinus Torvalds } 36391da177e4SLinus Torvalds 36401da177e4SLinus Torvalds return err; 36411da177e4SLinus Torvalds } 36421da177e4SLinus Torvalds 3643e0b93eddSJeff Layton static void selinux_file_set_fowner(struct file *file) 36441da177e4SLinus Torvalds { 36451da177e4SLinus Torvalds struct file_security_struct *fsec; 36461da177e4SLinus Torvalds 36471da177e4SLinus Torvalds fsec = file->f_security; 3648275bb41eSDavid Howells fsec->fown_sid = current_sid(); 36491da177e4SLinus Torvalds } 36501da177e4SLinus Torvalds 36511da177e4SLinus Torvalds static int selinux_file_send_sigiotask(struct task_struct *tsk, 36521da177e4SLinus Torvalds struct fown_struct *fown, int signum) 36531da177e4SLinus Torvalds { 36541da177e4SLinus Torvalds struct file *file; 365565c90bcaSStephen Smalley u32 sid = task_sid(tsk); 36561da177e4SLinus Torvalds u32 perm; 36571da177e4SLinus Torvalds struct file_security_struct *fsec; 36581da177e4SLinus Torvalds 36591da177e4SLinus Torvalds /* struct fown_struct is never outside the context of a struct file */ 3660b385a144SRobert P. J. Day file = container_of(fown, struct file, f_owner); 36611da177e4SLinus Torvalds 36621da177e4SLinus Torvalds fsec = file->f_security; 36631da177e4SLinus Torvalds 36641da177e4SLinus Torvalds if (!signum) 36651da177e4SLinus Torvalds perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */ 36661da177e4SLinus Torvalds else 36671da177e4SLinus Torvalds perm = signal_to_av(signum); 36681da177e4SLinus Torvalds 36696b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 36706b6bc620SStephen Smalley fsec->fown_sid, sid, 36711da177e4SLinus Torvalds SECCLASS_PROCESS, perm, NULL); 36721da177e4SLinus Torvalds } 36731da177e4SLinus Torvalds 36741da177e4SLinus Torvalds static int selinux_file_receive(struct file *file) 36751da177e4SLinus Torvalds { 367688e67f3bSDavid Howells const struct cred *cred = current_cred(); 367788e67f3bSDavid Howells 367888e67f3bSDavid Howells return file_has_perm(cred, file, file_to_av(file)); 36791da177e4SLinus Torvalds } 36801da177e4SLinus Torvalds 368194817692SAl Viro static int selinux_file_open(struct file *file) 3682788e7dd4SYuichi Nakamura { 3683788e7dd4SYuichi Nakamura struct file_security_struct *fsec; 3684788e7dd4SYuichi Nakamura struct inode_security_struct *isec; 3685d84f4f99SDavid Howells 3686788e7dd4SYuichi Nakamura fsec = file->f_security; 368783da53c5SAndreas Gruenbacher isec = inode_security(file_inode(file)); 3688788e7dd4SYuichi Nakamura /* 3689788e7dd4SYuichi Nakamura * Save inode label and policy sequence number 3690788e7dd4SYuichi Nakamura * at open-time so that selinux_file_permission 3691788e7dd4SYuichi Nakamura * can determine whether revalidation is necessary. 3692788e7dd4SYuichi Nakamura * Task label is already saved in the file security 3693788e7dd4SYuichi Nakamura * struct as its SID. 3694788e7dd4SYuichi Nakamura */ 3695788e7dd4SYuichi Nakamura fsec->isid = isec->sid; 36966b6bc620SStephen Smalley fsec->pseqno = avc_policy_seqno(&selinux_state); 3697788e7dd4SYuichi Nakamura /* 3698788e7dd4SYuichi Nakamura * Since the inode label or policy seqno may have changed 3699788e7dd4SYuichi Nakamura * between the selinux_inode_permission check and the saving 3700788e7dd4SYuichi Nakamura * of state above, recheck that access is still permitted. 3701788e7dd4SYuichi Nakamura * Otherwise, access might never be revalidated against the 3702788e7dd4SYuichi Nakamura * new inode label or new policy. 3703788e7dd4SYuichi Nakamura * This check is not redundant - do not remove. 3704788e7dd4SYuichi Nakamura */ 370594817692SAl Viro return file_path_has_perm(file->f_cred, file, open_file_to_av(file)); 3706788e7dd4SYuichi Nakamura } 3707788e7dd4SYuichi Nakamura 37081da177e4SLinus Torvalds /* task security operations */ 37091da177e4SLinus Torvalds 3710a79be238STetsuo Handa static int selinux_task_alloc(struct task_struct *task, 3711a79be238STetsuo Handa unsigned long clone_flags) 37121da177e4SLinus Torvalds { 3713be0554c9SStephen Smalley u32 sid = current_sid(); 3714be0554c9SStephen Smalley 37156b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 37166b6bc620SStephen Smalley sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL); 37171da177e4SLinus Torvalds } 37181da177e4SLinus Torvalds 3719f1752eecSDavid Howells /* 3720ee18d64cSDavid Howells * allocate the SELinux part of blank credentials 3721ee18d64cSDavid Howells */ 3722ee18d64cSDavid Howells static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp) 3723ee18d64cSDavid Howells { 3724ee18d64cSDavid Howells struct task_security_struct *tsec; 3725ee18d64cSDavid Howells 3726ee18d64cSDavid Howells tsec = kzalloc(sizeof(struct task_security_struct), gfp); 3727ee18d64cSDavid Howells if (!tsec) 3728ee18d64cSDavid Howells return -ENOMEM; 3729ee18d64cSDavid Howells 3730ee18d64cSDavid Howells cred->security = tsec; 3731ee18d64cSDavid Howells return 0; 3732ee18d64cSDavid Howells } 3733ee18d64cSDavid Howells 3734ee18d64cSDavid Howells /* 3735f1752eecSDavid Howells * detach and free the LSM part of a set of credentials 3736f1752eecSDavid Howells */ 3737f1752eecSDavid Howells static void selinux_cred_free(struct cred *cred) 37381da177e4SLinus Torvalds { 3739f1752eecSDavid Howells struct task_security_struct *tsec = cred->security; 3740e0e81739SDavid Howells 37412edeaa34STetsuo Handa /* 37422edeaa34STetsuo Handa * cred->security == NULL if security_cred_alloc_blank() or 37432edeaa34STetsuo Handa * security_prepare_creds() returned an error. 37442edeaa34STetsuo Handa */ 37452edeaa34STetsuo Handa BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE); 3746e0e81739SDavid Howells cred->security = (void *) 0x7UL; 3747f1752eecSDavid Howells kfree(tsec); 37481da177e4SLinus Torvalds } 37491da177e4SLinus Torvalds 3750d84f4f99SDavid Howells /* 3751d84f4f99SDavid Howells * prepare a new set of credentials for modification 3752d84f4f99SDavid Howells */ 3753d84f4f99SDavid Howells static int selinux_cred_prepare(struct cred *new, const struct cred *old, 3754d84f4f99SDavid Howells gfp_t gfp) 3755d84f4f99SDavid Howells { 3756d84f4f99SDavid Howells const struct task_security_struct *old_tsec; 3757d84f4f99SDavid Howells struct task_security_struct *tsec; 3758d84f4f99SDavid Howells 3759d84f4f99SDavid Howells old_tsec = old->security; 3760d84f4f99SDavid Howells 3761d84f4f99SDavid Howells tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp); 3762d84f4f99SDavid Howells if (!tsec) 3763d84f4f99SDavid Howells return -ENOMEM; 3764d84f4f99SDavid Howells 3765d84f4f99SDavid Howells new->security = tsec; 3766d84f4f99SDavid Howells return 0; 3767d84f4f99SDavid Howells } 3768d84f4f99SDavid Howells 3769d84f4f99SDavid Howells /* 3770ee18d64cSDavid Howells * transfer the SELinux data to a blank set of creds 3771ee18d64cSDavid Howells */ 3772ee18d64cSDavid Howells static void selinux_cred_transfer(struct cred *new, const struct cred *old) 3773ee18d64cSDavid Howells { 3774ee18d64cSDavid Howells const struct task_security_struct *old_tsec = old->security; 3775ee18d64cSDavid Howells struct task_security_struct *tsec = new->security; 3776ee18d64cSDavid Howells 3777ee18d64cSDavid Howells *tsec = *old_tsec; 3778ee18d64cSDavid Howells } 3779ee18d64cSDavid Howells 37803ec30113SMatthew Garrett static void selinux_cred_getsecid(const struct cred *c, u32 *secid) 37813ec30113SMatthew Garrett { 37823ec30113SMatthew Garrett *secid = cred_sid(c); 37833ec30113SMatthew Garrett } 37843ec30113SMatthew Garrett 3785ee18d64cSDavid Howells /* 37863a3b7ce9SDavid Howells * set the security data for a kernel service 37873a3b7ce9SDavid Howells * - all the creation contexts are set to unlabelled 37883a3b7ce9SDavid Howells */ 37893a3b7ce9SDavid Howells static int selinux_kernel_act_as(struct cred *new, u32 secid) 37903a3b7ce9SDavid Howells { 37913a3b7ce9SDavid Howells struct task_security_struct *tsec = new->security; 37923a3b7ce9SDavid Howells u32 sid = current_sid(); 37933a3b7ce9SDavid Howells int ret; 37943a3b7ce9SDavid Howells 37956b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 37966b6bc620SStephen Smalley sid, secid, 37973a3b7ce9SDavid Howells SECCLASS_KERNEL_SERVICE, 37983a3b7ce9SDavid Howells KERNEL_SERVICE__USE_AS_OVERRIDE, 37993a3b7ce9SDavid Howells NULL); 38003a3b7ce9SDavid Howells if (ret == 0) { 38013a3b7ce9SDavid Howells tsec->sid = secid; 38023a3b7ce9SDavid Howells tsec->create_sid = 0; 38033a3b7ce9SDavid Howells tsec->keycreate_sid = 0; 38043a3b7ce9SDavid Howells tsec->sockcreate_sid = 0; 38053a3b7ce9SDavid Howells } 38063a3b7ce9SDavid Howells return ret; 38073a3b7ce9SDavid Howells } 38083a3b7ce9SDavid Howells 38093a3b7ce9SDavid Howells /* 38103a3b7ce9SDavid Howells * set the file creation context in a security record to the same as the 38113a3b7ce9SDavid Howells * objective context of the specified inode 38123a3b7ce9SDavid Howells */ 38133a3b7ce9SDavid Howells static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode) 38143a3b7ce9SDavid Howells { 381583da53c5SAndreas Gruenbacher struct inode_security_struct *isec = inode_security(inode); 38163a3b7ce9SDavid Howells struct task_security_struct *tsec = new->security; 38173a3b7ce9SDavid Howells u32 sid = current_sid(); 38183a3b7ce9SDavid Howells int ret; 38193a3b7ce9SDavid Howells 38206b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 38216b6bc620SStephen Smalley sid, isec->sid, 38223a3b7ce9SDavid Howells SECCLASS_KERNEL_SERVICE, 38233a3b7ce9SDavid Howells KERNEL_SERVICE__CREATE_FILES_AS, 38243a3b7ce9SDavid Howells NULL); 38253a3b7ce9SDavid Howells 38263a3b7ce9SDavid Howells if (ret == 0) 38273a3b7ce9SDavid Howells tsec->create_sid = isec->sid; 3828ef57471aSDavid Howells return ret; 38293a3b7ce9SDavid Howells } 38303a3b7ce9SDavid Howells 3831dd8dbf2eSEric Paris static int selinux_kernel_module_request(char *kmod_name) 383225354c4fSEric Paris { 3833dd8dbf2eSEric Paris struct common_audit_data ad; 3834dd8dbf2eSEric Paris 383550c205f5SEric Paris ad.type = LSM_AUDIT_DATA_KMOD; 3836dd8dbf2eSEric Paris ad.u.kmod_name = kmod_name; 3837dd8dbf2eSEric Paris 38386b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 38396b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM, 3840dd8dbf2eSEric Paris SYSTEM__MODULE_REQUEST, &ad); 384125354c4fSEric Paris } 384225354c4fSEric Paris 384361d612eaSJeff Vander Stoep static int selinux_kernel_module_from_file(struct file *file) 384461d612eaSJeff Vander Stoep { 384561d612eaSJeff Vander Stoep struct common_audit_data ad; 384661d612eaSJeff Vander Stoep struct inode_security_struct *isec; 384761d612eaSJeff Vander Stoep struct file_security_struct *fsec; 384861d612eaSJeff Vander Stoep u32 sid = current_sid(); 384961d612eaSJeff Vander Stoep int rc; 385061d612eaSJeff Vander Stoep 385161d612eaSJeff Vander Stoep /* init_module */ 385261d612eaSJeff Vander Stoep if (file == NULL) 38536b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 38546b6bc620SStephen Smalley sid, sid, SECCLASS_SYSTEM, 385561d612eaSJeff Vander Stoep SYSTEM__MODULE_LOAD, NULL); 385661d612eaSJeff Vander Stoep 385761d612eaSJeff Vander Stoep /* finit_module */ 385820cdef8dSPaul Moore 385943af5de7SVivek Goyal ad.type = LSM_AUDIT_DATA_FILE; 386043af5de7SVivek Goyal ad.u.file = file; 386161d612eaSJeff Vander Stoep 386261d612eaSJeff Vander Stoep fsec = file->f_security; 386361d612eaSJeff Vander Stoep if (sid != fsec->sid) { 38646b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 38656b6bc620SStephen Smalley sid, fsec->sid, SECCLASS_FD, FD__USE, &ad); 386661d612eaSJeff Vander Stoep if (rc) 386761d612eaSJeff Vander Stoep return rc; 386861d612eaSJeff Vander Stoep } 386961d612eaSJeff Vander Stoep 387020cdef8dSPaul Moore isec = inode_security(file_inode(file)); 38716b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 38726b6bc620SStephen Smalley sid, isec->sid, SECCLASS_SYSTEM, 387361d612eaSJeff Vander Stoep SYSTEM__MODULE_LOAD, &ad); 387461d612eaSJeff Vander Stoep } 387561d612eaSJeff Vander Stoep 387661d612eaSJeff Vander Stoep static int selinux_kernel_read_file(struct file *file, 387761d612eaSJeff Vander Stoep enum kernel_read_file_id id) 387861d612eaSJeff Vander Stoep { 387961d612eaSJeff Vander Stoep int rc = 0; 388061d612eaSJeff Vander Stoep 388161d612eaSJeff Vander Stoep switch (id) { 388261d612eaSJeff Vander Stoep case READING_MODULE: 388361d612eaSJeff Vander Stoep rc = selinux_kernel_module_from_file(file); 388461d612eaSJeff Vander Stoep break; 388561d612eaSJeff Vander Stoep default: 388661d612eaSJeff Vander Stoep break; 388761d612eaSJeff Vander Stoep } 388861d612eaSJeff Vander Stoep 388961d612eaSJeff Vander Stoep return rc; 389061d612eaSJeff Vander Stoep } 389161d612eaSJeff Vander Stoep 3892c77b8cdfSMimi Zohar static int selinux_kernel_load_data(enum kernel_load_data_id id) 3893c77b8cdfSMimi Zohar { 3894c77b8cdfSMimi Zohar int rc = 0; 3895c77b8cdfSMimi Zohar 3896c77b8cdfSMimi Zohar switch (id) { 3897c77b8cdfSMimi Zohar case LOADING_MODULE: 3898c77b8cdfSMimi Zohar rc = selinux_kernel_module_from_file(NULL); 3899c77b8cdfSMimi Zohar default: 3900c77b8cdfSMimi Zohar break; 3901c77b8cdfSMimi Zohar } 3902c77b8cdfSMimi Zohar 3903c77b8cdfSMimi Zohar return rc; 3904c77b8cdfSMimi Zohar } 3905c77b8cdfSMimi Zohar 39061da177e4SLinus Torvalds static int selinux_task_setpgid(struct task_struct *p, pid_t pgid) 39071da177e4SLinus Torvalds { 39086b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 39096b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 3910be0554c9SStephen Smalley PROCESS__SETPGID, NULL); 39111da177e4SLinus Torvalds } 39121da177e4SLinus Torvalds 39131da177e4SLinus Torvalds static int selinux_task_getpgid(struct task_struct *p) 39141da177e4SLinus Torvalds { 39156b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 39166b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 3917be0554c9SStephen Smalley PROCESS__GETPGID, NULL); 39181da177e4SLinus Torvalds } 39191da177e4SLinus Torvalds 39201da177e4SLinus Torvalds static int selinux_task_getsid(struct task_struct *p) 39211da177e4SLinus Torvalds { 39226b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 39236b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 3924be0554c9SStephen Smalley PROCESS__GETSESSION, NULL); 39251da177e4SLinus Torvalds } 39261da177e4SLinus Torvalds 3927f9008e4cSDavid Quigley static void selinux_task_getsecid(struct task_struct *p, u32 *secid) 3928f9008e4cSDavid Quigley { 3929275bb41eSDavid Howells *secid = task_sid(p); 3930f9008e4cSDavid Quigley } 3931f9008e4cSDavid Quigley 39321da177e4SLinus Torvalds static int selinux_task_setnice(struct task_struct *p, int nice) 39331da177e4SLinus Torvalds { 39346b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 39356b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 3936be0554c9SStephen Smalley PROCESS__SETSCHED, NULL); 39371da177e4SLinus Torvalds } 39381da177e4SLinus Torvalds 393903e68060SJames Morris static int selinux_task_setioprio(struct task_struct *p, int ioprio) 394003e68060SJames Morris { 39416b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 39426b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 3943be0554c9SStephen Smalley PROCESS__SETSCHED, NULL); 394403e68060SJames Morris } 394503e68060SJames Morris 3946a1836a42SDavid Quigley static int selinux_task_getioprio(struct task_struct *p) 3947a1836a42SDavid Quigley { 39486b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 39496b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 3950be0554c9SStephen Smalley PROCESS__GETSCHED, NULL); 3951a1836a42SDavid Quigley } 3952a1836a42SDavid Quigley 39534298555dSCorentin LABBE static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred, 3954791ec491SStephen Smalley unsigned int flags) 3955791ec491SStephen Smalley { 3956791ec491SStephen Smalley u32 av = 0; 3957791ec491SStephen Smalley 395884e6885eSStephen Smalley if (!flags) 395984e6885eSStephen Smalley return 0; 3960791ec491SStephen Smalley if (flags & LSM_PRLIMIT_WRITE) 3961791ec491SStephen Smalley av |= PROCESS__SETRLIMIT; 3962791ec491SStephen Smalley if (flags & LSM_PRLIMIT_READ) 3963791ec491SStephen Smalley av |= PROCESS__GETRLIMIT; 39646b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 39656b6bc620SStephen Smalley cred_sid(cred), cred_sid(tcred), 3966791ec491SStephen Smalley SECCLASS_PROCESS, av, NULL); 3967791ec491SStephen Smalley } 3968791ec491SStephen Smalley 39698fd00b4dSJiri Slaby static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource, 39708fd00b4dSJiri Slaby struct rlimit *new_rlim) 39711da177e4SLinus Torvalds { 39728fd00b4dSJiri Slaby struct rlimit *old_rlim = p->signal->rlim + resource; 39731da177e4SLinus Torvalds 39741da177e4SLinus Torvalds /* Control the ability to change the hard limit (whether 39751da177e4SLinus Torvalds lowering or raising it), so that the hard limit can 39761da177e4SLinus Torvalds later be used as a safe reset point for the soft limit 3977d84f4f99SDavid Howells upon context transitions. See selinux_bprm_committing_creds. */ 39781da177e4SLinus Torvalds if (old_rlim->rlim_max != new_rlim->rlim_max) 39796b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 39806b6bc620SStephen Smalley current_sid(), task_sid(p), 3981be0554c9SStephen Smalley SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL); 39821da177e4SLinus Torvalds 39831da177e4SLinus Torvalds return 0; 39841da177e4SLinus Torvalds } 39851da177e4SLinus Torvalds 3986b0ae1981SKOSAKI Motohiro static int selinux_task_setscheduler(struct task_struct *p) 39871da177e4SLinus Torvalds { 39886b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 39896b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 3990be0554c9SStephen Smalley PROCESS__SETSCHED, NULL); 39911da177e4SLinus Torvalds } 39921da177e4SLinus Torvalds 39931da177e4SLinus Torvalds static int selinux_task_getscheduler(struct task_struct *p) 39941da177e4SLinus Torvalds { 39956b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 39966b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 3997be0554c9SStephen Smalley PROCESS__GETSCHED, NULL); 39981da177e4SLinus Torvalds } 39991da177e4SLinus Torvalds 400035601547SDavid Quigley static int selinux_task_movememory(struct task_struct *p) 400135601547SDavid Quigley { 40026b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 40036b6bc620SStephen Smalley current_sid(), task_sid(p), SECCLASS_PROCESS, 4004be0554c9SStephen Smalley PROCESS__SETSCHED, NULL); 400535601547SDavid Quigley } 400635601547SDavid Quigley 4007ae7795bcSEric W. Biederman static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info, 40086b4f3d01SStephen Smalley int sig, const struct cred *cred) 40091da177e4SLinus Torvalds { 40106b4f3d01SStephen Smalley u32 secid; 40111da177e4SLinus Torvalds u32 perm; 40121da177e4SLinus Torvalds 40131da177e4SLinus Torvalds if (!sig) 40141da177e4SLinus Torvalds perm = PROCESS__SIGNULL; /* null signal; existence test */ 40151da177e4SLinus Torvalds else 40161da177e4SLinus Torvalds perm = signal_to_av(sig); 40176b4f3d01SStephen Smalley if (!cred) 4018be0554c9SStephen Smalley secid = current_sid(); 40196b4f3d01SStephen Smalley else 40206b4f3d01SStephen Smalley secid = cred_sid(cred); 40216b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 40226b6bc620SStephen Smalley secid, task_sid(p), SECCLASS_PROCESS, perm, NULL); 40231da177e4SLinus Torvalds } 40241da177e4SLinus Torvalds 40251da177e4SLinus Torvalds static void selinux_task_to_inode(struct task_struct *p, 40261da177e4SLinus Torvalds struct inode *inode) 40271da177e4SLinus Torvalds { 40281da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 4029275bb41eSDavid Howells u32 sid = task_sid(p); 40301da177e4SLinus Torvalds 40319287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 4032db978da8SAndreas Gruenbacher isec->sclass = inode_mode_to_security_class(inode->i_mode); 4033275bb41eSDavid Howells isec->sid = sid; 40346f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 40359287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 40361da177e4SLinus Torvalds } 40371da177e4SLinus Torvalds 40381da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 403967f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv4(struct sk_buff *skb, 40402bf49690SThomas Liu struct common_audit_data *ad, u8 *proto) 40411da177e4SLinus Torvalds { 40421da177e4SLinus Torvalds int offset, ihlen, ret = -EINVAL; 40431da177e4SLinus Torvalds struct iphdr _iph, *ih; 40441da177e4SLinus Torvalds 4045bbe735e4SArnaldo Carvalho de Melo offset = skb_network_offset(skb); 40461da177e4SLinus Torvalds ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph); 40471da177e4SLinus Torvalds if (ih == NULL) 40481da177e4SLinus Torvalds goto out; 40491da177e4SLinus Torvalds 40501da177e4SLinus Torvalds ihlen = ih->ihl * 4; 40511da177e4SLinus Torvalds if (ihlen < sizeof(_iph)) 40521da177e4SLinus Torvalds goto out; 40531da177e4SLinus Torvalds 405448c62af6SEric Paris ad->u.net->v4info.saddr = ih->saddr; 405548c62af6SEric Paris ad->u.net->v4info.daddr = ih->daddr; 40561da177e4SLinus Torvalds ret = 0; 40571da177e4SLinus Torvalds 405867f83cbfSVenkat Yekkirala if (proto) 405967f83cbfSVenkat Yekkirala *proto = ih->protocol; 406067f83cbfSVenkat Yekkirala 40611da177e4SLinus Torvalds switch (ih->protocol) { 40621da177e4SLinus Torvalds case IPPROTO_TCP: { 40631da177e4SLinus Torvalds struct tcphdr _tcph, *th; 40641da177e4SLinus Torvalds 40651da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 40661da177e4SLinus Torvalds break; 40671da177e4SLinus Torvalds 40681da177e4SLinus Torvalds offset += ihlen; 40691da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 40701da177e4SLinus Torvalds if (th == NULL) 40711da177e4SLinus Torvalds break; 40721da177e4SLinus Torvalds 407348c62af6SEric Paris ad->u.net->sport = th->source; 407448c62af6SEric Paris ad->u.net->dport = th->dest; 40751da177e4SLinus Torvalds break; 40761da177e4SLinus Torvalds } 40771da177e4SLinus Torvalds 40781da177e4SLinus Torvalds case IPPROTO_UDP: { 40791da177e4SLinus Torvalds struct udphdr _udph, *uh; 40801da177e4SLinus Torvalds 40811da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 40821da177e4SLinus Torvalds break; 40831da177e4SLinus Torvalds 40841da177e4SLinus Torvalds offset += ihlen; 40851da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 40861da177e4SLinus Torvalds if (uh == NULL) 40871da177e4SLinus Torvalds break; 40881da177e4SLinus Torvalds 408948c62af6SEric Paris ad->u.net->sport = uh->source; 409048c62af6SEric Paris ad->u.net->dport = uh->dest; 40911da177e4SLinus Torvalds break; 40921da177e4SLinus Torvalds } 40931da177e4SLinus Torvalds 40942ee92d46SJames Morris case IPPROTO_DCCP: { 40952ee92d46SJames Morris struct dccp_hdr _dccph, *dh; 40962ee92d46SJames Morris 40972ee92d46SJames Morris if (ntohs(ih->frag_off) & IP_OFFSET) 40982ee92d46SJames Morris break; 40992ee92d46SJames Morris 41002ee92d46SJames Morris offset += ihlen; 41012ee92d46SJames Morris dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); 41022ee92d46SJames Morris if (dh == NULL) 41032ee92d46SJames Morris break; 41042ee92d46SJames Morris 410548c62af6SEric Paris ad->u.net->sport = dh->dccph_sport; 410648c62af6SEric Paris ad->u.net->dport = dh->dccph_dport; 41072ee92d46SJames Morris break; 41082ee92d46SJames Morris } 41092ee92d46SJames Morris 4110d452930fSRichard Haines #if IS_ENABLED(CONFIG_IP_SCTP) 4111d452930fSRichard Haines case IPPROTO_SCTP: { 4112d452930fSRichard Haines struct sctphdr _sctph, *sh; 4113d452930fSRichard Haines 4114d452930fSRichard Haines if (ntohs(ih->frag_off) & IP_OFFSET) 4115d452930fSRichard Haines break; 4116d452930fSRichard Haines 4117d452930fSRichard Haines offset += ihlen; 4118d452930fSRichard Haines sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph); 4119d452930fSRichard Haines if (sh == NULL) 4120d452930fSRichard Haines break; 4121d452930fSRichard Haines 4122d452930fSRichard Haines ad->u.net->sport = sh->source; 4123d452930fSRichard Haines ad->u.net->dport = sh->dest; 4124d452930fSRichard Haines break; 4125d452930fSRichard Haines } 4126d452930fSRichard Haines #endif 41271da177e4SLinus Torvalds default: 41281da177e4SLinus Torvalds break; 41291da177e4SLinus Torvalds } 41301da177e4SLinus Torvalds out: 41311da177e4SLinus Torvalds return ret; 41321da177e4SLinus Torvalds } 41331da177e4SLinus Torvalds 41341a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 41351da177e4SLinus Torvalds 41361da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 413767f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv6(struct sk_buff *skb, 41382bf49690SThomas Liu struct common_audit_data *ad, u8 *proto) 41391da177e4SLinus Torvalds { 41401da177e4SLinus Torvalds u8 nexthdr; 41411da177e4SLinus Torvalds int ret = -EINVAL, offset; 41421da177e4SLinus Torvalds struct ipv6hdr _ipv6h, *ip6; 414375f2811cSJesse Gross __be16 frag_off; 41441da177e4SLinus Torvalds 4145bbe735e4SArnaldo Carvalho de Melo offset = skb_network_offset(skb); 41461da177e4SLinus Torvalds ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h); 41471da177e4SLinus Torvalds if (ip6 == NULL) 41481da177e4SLinus Torvalds goto out; 41491da177e4SLinus Torvalds 415048c62af6SEric Paris ad->u.net->v6info.saddr = ip6->saddr; 415148c62af6SEric Paris ad->u.net->v6info.daddr = ip6->daddr; 41521da177e4SLinus Torvalds ret = 0; 41531da177e4SLinus Torvalds 41541da177e4SLinus Torvalds nexthdr = ip6->nexthdr; 41551da177e4SLinus Torvalds offset += sizeof(_ipv6h); 415675f2811cSJesse Gross offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off); 41571da177e4SLinus Torvalds if (offset < 0) 41581da177e4SLinus Torvalds goto out; 41591da177e4SLinus Torvalds 416067f83cbfSVenkat Yekkirala if (proto) 416167f83cbfSVenkat Yekkirala *proto = nexthdr; 416267f83cbfSVenkat Yekkirala 41631da177e4SLinus Torvalds switch (nexthdr) { 41641da177e4SLinus Torvalds case IPPROTO_TCP: { 41651da177e4SLinus Torvalds struct tcphdr _tcph, *th; 41661da177e4SLinus Torvalds 41671da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 41681da177e4SLinus Torvalds if (th == NULL) 41691da177e4SLinus Torvalds break; 41701da177e4SLinus Torvalds 417148c62af6SEric Paris ad->u.net->sport = th->source; 417248c62af6SEric Paris ad->u.net->dport = th->dest; 41731da177e4SLinus Torvalds break; 41741da177e4SLinus Torvalds } 41751da177e4SLinus Torvalds 41761da177e4SLinus Torvalds case IPPROTO_UDP: { 41771da177e4SLinus Torvalds struct udphdr _udph, *uh; 41781da177e4SLinus Torvalds 41791da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 41801da177e4SLinus Torvalds if (uh == NULL) 41811da177e4SLinus Torvalds break; 41821da177e4SLinus Torvalds 418348c62af6SEric Paris ad->u.net->sport = uh->source; 418448c62af6SEric Paris ad->u.net->dport = uh->dest; 41851da177e4SLinus Torvalds break; 41861da177e4SLinus Torvalds } 41871da177e4SLinus Torvalds 41882ee92d46SJames Morris case IPPROTO_DCCP: { 41892ee92d46SJames Morris struct dccp_hdr _dccph, *dh; 41902ee92d46SJames Morris 41912ee92d46SJames Morris dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); 41922ee92d46SJames Morris if (dh == NULL) 41932ee92d46SJames Morris break; 41942ee92d46SJames Morris 419548c62af6SEric Paris ad->u.net->sport = dh->dccph_sport; 419648c62af6SEric Paris ad->u.net->dport = dh->dccph_dport; 41972ee92d46SJames Morris break; 41982ee92d46SJames Morris } 41992ee92d46SJames Morris 4200d452930fSRichard Haines #if IS_ENABLED(CONFIG_IP_SCTP) 4201d452930fSRichard Haines case IPPROTO_SCTP: { 4202d452930fSRichard Haines struct sctphdr _sctph, *sh; 4203d452930fSRichard Haines 4204d452930fSRichard Haines sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph); 4205d452930fSRichard Haines if (sh == NULL) 4206d452930fSRichard Haines break; 4207d452930fSRichard Haines 4208d452930fSRichard Haines ad->u.net->sport = sh->source; 4209d452930fSRichard Haines ad->u.net->dport = sh->dest; 4210d452930fSRichard Haines break; 4211d452930fSRichard Haines } 4212d452930fSRichard Haines #endif 42131da177e4SLinus Torvalds /* includes fragments */ 42141da177e4SLinus Torvalds default: 42151da177e4SLinus Torvalds break; 42161da177e4SLinus Torvalds } 42171da177e4SLinus Torvalds out: 42181da177e4SLinus Torvalds return ret; 42191da177e4SLinus Torvalds } 42201da177e4SLinus Torvalds 42211da177e4SLinus Torvalds #endif /* IPV6 */ 42221da177e4SLinus Torvalds 42232bf49690SThomas Liu static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad, 4224cf9481e2SDavid Howells char **_addrp, int src, u8 *proto) 42251da177e4SLinus Torvalds { 4226cf9481e2SDavid Howells char *addrp; 4227cf9481e2SDavid Howells int ret; 42281da177e4SLinus Torvalds 422948c62af6SEric Paris switch (ad->u.net->family) { 42301da177e4SLinus Torvalds case PF_INET: 423167f83cbfSVenkat Yekkirala ret = selinux_parse_skb_ipv4(skb, ad, proto); 4232cf9481e2SDavid Howells if (ret) 4233cf9481e2SDavid Howells goto parse_error; 423448c62af6SEric Paris addrp = (char *)(src ? &ad->u.net->v4info.saddr : 423548c62af6SEric Paris &ad->u.net->v4info.daddr); 4236cf9481e2SDavid Howells goto okay; 42371da177e4SLinus Torvalds 42381a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 42391da177e4SLinus Torvalds case PF_INET6: 424067f83cbfSVenkat Yekkirala ret = selinux_parse_skb_ipv6(skb, ad, proto); 4241cf9481e2SDavid Howells if (ret) 4242cf9481e2SDavid Howells goto parse_error; 424348c62af6SEric Paris addrp = (char *)(src ? &ad->u.net->v6info.saddr : 424448c62af6SEric Paris &ad->u.net->v6info.daddr); 4245cf9481e2SDavid Howells goto okay; 42461da177e4SLinus Torvalds #endif /* IPV6 */ 42471da177e4SLinus Torvalds default: 4248cf9481e2SDavid Howells addrp = NULL; 4249cf9481e2SDavid Howells goto okay; 42501da177e4SLinus Torvalds } 42511da177e4SLinus Torvalds 4252cf9481e2SDavid Howells parse_error: 4253c103a91eSpeter enderborg pr_warn( 425471f1cb05SPaul Moore "SELinux: failure in selinux_parse_skb()," 425571f1cb05SPaul Moore " unable to parse packet\n"); 42561da177e4SLinus Torvalds return ret; 4257cf9481e2SDavid Howells 4258cf9481e2SDavid Howells okay: 4259cf9481e2SDavid Howells if (_addrp) 4260cf9481e2SDavid Howells *_addrp = addrp; 4261cf9481e2SDavid Howells return 0; 42621da177e4SLinus Torvalds } 42631da177e4SLinus Torvalds 42644f6a993fSPaul Moore /** 4265220deb96SPaul Moore * selinux_skb_peerlbl_sid - Determine the peer label of a packet 42664f6a993fSPaul Moore * @skb: the packet 426775e22910SPaul Moore * @family: protocol family 4268220deb96SPaul Moore * @sid: the packet's peer label SID 42694f6a993fSPaul Moore * 42704f6a993fSPaul Moore * Description: 4271220deb96SPaul Moore * Check the various different forms of network peer labeling and determine 4272220deb96SPaul Moore * the peer label/SID for the packet; most of the magic actually occurs in 4273220deb96SPaul Moore * the security server function security_net_peersid_cmp(). The function 4274220deb96SPaul Moore * returns zero if the value in @sid is valid (although it may be SECSID_NULL) 4275220deb96SPaul Moore * or -EACCES if @sid is invalid due to inconsistencies with the different 4276220deb96SPaul Moore * peer labels. 42774f6a993fSPaul Moore * 42784f6a993fSPaul Moore */ 4279220deb96SPaul Moore static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid) 42804f6a993fSPaul Moore { 428171f1cb05SPaul Moore int err; 42824f6a993fSPaul Moore u32 xfrm_sid; 42834f6a993fSPaul Moore u32 nlbl_sid; 4284220deb96SPaul Moore u32 nlbl_type; 42854f6a993fSPaul Moore 4286817eff71SPaul Moore err = selinux_xfrm_skb_sid(skb, &xfrm_sid); 4287bed4d7efSPaul Moore if (unlikely(err)) 4288bed4d7efSPaul Moore return -EACCES; 4289bed4d7efSPaul Moore err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid); 4290bed4d7efSPaul Moore if (unlikely(err)) 4291bed4d7efSPaul Moore return -EACCES; 4292220deb96SPaul Moore 4293aa8e712cSStephen Smalley err = security_net_peersid_resolve(&selinux_state, nlbl_sid, 4294aa8e712cSStephen Smalley nlbl_type, xfrm_sid, sid); 429571f1cb05SPaul Moore if (unlikely(err)) { 4296c103a91eSpeter enderborg pr_warn( 429771f1cb05SPaul Moore "SELinux: failure in selinux_skb_peerlbl_sid()," 429871f1cb05SPaul Moore " unable to determine packet's peer label\n"); 4299220deb96SPaul Moore return -EACCES; 430071f1cb05SPaul Moore } 4301220deb96SPaul Moore 4302220deb96SPaul Moore return 0; 43034f6a993fSPaul Moore } 43044f6a993fSPaul Moore 4305446b8024SPaul Moore /** 4306446b8024SPaul Moore * selinux_conn_sid - Determine the child socket label for a connection 4307446b8024SPaul Moore * @sk_sid: the parent socket's SID 4308446b8024SPaul Moore * @skb_sid: the packet's SID 4309446b8024SPaul Moore * @conn_sid: the resulting connection SID 4310446b8024SPaul Moore * 4311446b8024SPaul Moore * If @skb_sid is valid then the user:role:type information from @sk_sid is 4312446b8024SPaul Moore * combined with the MLS information from @skb_sid in order to create 4313446b8024SPaul Moore * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy 4314446b8024SPaul Moore * of @sk_sid. Returns zero on success, negative values on failure. 4315446b8024SPaul Moore * 4316446b8024SPaul Moore */ 4317446b8024SPaul Moore static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid) 4318446b8024SPaul Moore { 4319446b8024SPaul Moore int err = 0; 4320446b8024SPaul Moore 4321446b8024SPaul Moore if (skb_sid != SECSID_NULL) 4322aa8e712cSStephen Smalley err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid, 4323aa8e712cSStephen Smalley conn_sid); 4324446b8024SPaul Moore else 4325446b8024SPaul Moore *conn_sid = sk_sid; 4326446b8024SPaul Moore 4327446b8024SPaul Moore return err; 4328446b8024SPaul Moore } 4329446b8024SPaul Moore 43301da177e4SLinus Torvalds /* socket security operations */ 4331d4f2d978SPaul Moore 43322ad18bdfSHarry Ciao static int socket_sockcreate_sid(const struct task_security_struct *tsec, 43332ad18bdfSHarry Ciao u16 secclass, u32 *socksid) 4334d4f2d978SPaul Moore { 43352ad18bdfSHarry Ciao if (tsec->sockcreate_sid > SECSID_NULL) { 43362ad18bdfSHarry Ciao *socksid = tsec->sockcreate_sid; 43372ad18bdfSHarry Ciao return 0; 43382ad18bdfSHarry Ciao } 43392ad18bdfSHarry Ciao 4340aa8e712cSStephen Smalley return security_transition_sid(&selinux_state, tsec->sid, tsec->sid, 4341aa8e712cSStephen Smalley secclass, NULL, socksid); 4342d4f2d978SPaul Moore } 4343d4f2d978SPaul Moore 4344be0554c9SStephen Smalley static int sock_has_perm(struct sock *sk, u32 perms) 43451da177e4SLinus Torvalds { 4346253bfae6SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 43472bf49690SThomas Liu struct common_audit_data ad; 434848c62af6SEric Paris struct lsm_network_audit net = {0,}; 43491da177e4SLinus Torvalds 4350253bfae6SPaul Moore if (sksec->sid == SECINITSID_KERNEL) 4351253bfae6SPaul Moore return 0; 43521da177e4SLinus Torvalds 435350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 435448c62af6SEric Paris ad.u.net = &net; 435548c62af6SEric Paris ad.u.net->sk = sk; 43561da177e4SLinus Torvalds 43576b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 43586b6bc620SStephen Smalley current_sid(), sksec->sid, sksec->sclass, perms, 4359be0554c9SStephen Smalley &ad); 43601da177e4SLinus Torvalds } 43611da177e4SLinus Torvalds 43621da177e4SLinus Torvalds static int selinux_socket_create(int family, int type, 43631da177e4SLinus Torvalds int protocol, int kern) 43641da177e4SLinus Torvalds { 43655fb49870SPaul Moore const struct task_security_struct *tsec = current_security(); 4366d4f2d978SPaul Moore u32 newsid; 4367275bb41eSDavid Howells u16 secclass; 43682ad18bdfSHarry Ciao int rc; 43691da177e4SLinus Torvalds 43701da177e4SLinus Torvalds if (kern) 4371d4f2d978SPaul Moore return 0; 43721da177e4SLinus Torvalds 4373275bb41eSDavid Howells secclass = socket_type_to_security_class(family, type, protocol); 43742ad18bdfSHarry Ciao rc = socket_sockcreate_sid(tsec, secclass, &newsid); 43752ad18bdfSHarry Ciao if (rc) 43762ad18bdfSHarry Ciao return rc; 43772ad18bdfSHarry Ciao 43786b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 43796b6bc620SStephen Smalley tsec->sid, newsid, secclass, SOCKET__CREATE, NULL); 43801da177e4SLinus Torvalds } 43811da177e4SLinus Torvalds 43827420ed23SVenkat Yekkirala static int selinux_socket_post_create(struct socket *sock, int family, 43831da177e4SLinus Torvalds int type, int protocol, int kern) 43841da177e4SLinus Torvalds { 43855fb49870SPaul Moore const struct task_security_struct *tsec = current_security(); 43865d226df4SAndreas Gruenbacher struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock)); 4387892c141eSVenkat Yekkirala struct sk_security_struct *sksec; 43889287aed2SAndreas Gruenbacher u16 sclass = socket_type_to_security_class(family, type, protocol); 43899287aed2SAndreas Gruenbacher u32 sid = SECINITSID_KERNEL; 4390275bb41eSDavid Howells int err = 0; 4391275bb41eSDavid Howells 43929287aed2SAndreas Gruenbacher if (!kern) { 43939287aed2SAndreas Gruenbacher err = socket_sockcreate_sid(tsec, sclass, &sid); 43942ad18bdfSHarry Ciao if (err) 43952ad18bdfSHarry Ciao return err; 43962ad18bdfSHarry Ciao } 4397275bb41eSDavid Howells 43989287aed2SAndreas Gruenbacher isec->sclass = sclass; 43999287aed2SAndreas Gruenbacher isec->sid = sid; 44006f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 44011da177e4SLinus Torvalds 4402892c141eSVenkat Yekkirala if (sock->sk) { 4403892c141eSVenkat Yekkirala sksec = sock->sk->sk_security; 44049287aed2SAndreas Gruenbacher sksec->sclass = sclass; 44059287aed2SAndreas Gruenbacher sksec->sid = sid; 4406d452930fSRichard Haines /* Allows detection of the first association on this socket */ 4407d452930fSRichard Haines if (sksec->sclass == SECCLASS_SCTP_SOCKET) 4408d452930fSRichard Haines sksec->sctp_assoc_state = SCTP_ASSOC_UNSET; 4409d452930fSRichard Haines 4410389fb800SPaul Moore err = selinux_netlbl_socket_post_create(sock->sk, family); 4411892c141eSVenkat Yekkirala } 4412892c141eSVenkat Yekkirala 44137420ed23SVenkat Yekkirala return err; 44141da177e4SLinus Torvalds } 44151da177e4SLinus Torvalds 44160b811db2SDavid Herrmann static int selinux_socket_socketpair(struct socket *socka, 44170b811db2SDavid Herrmann struct socket *sockb) 44180b811db2SDavid Herrmann { 44190b811db2SDavid Herrmann struct sk_security_struct *sksec_a = socka->sk->sk_security; 44200b811db2SDavid Herrmann struct sk_security_struct *sksec_b = sockb->sk->sk_security; 44210b811db2SDavid Herrmann 44220b811db2SDavid Herrmann sksec_a->peer_sid = sksec_b->sid; 44230b811db2SDavid Herrmann sksec_b->peer_sid = sksec_a->sid; 44240b811db2SDavid Herrmann 44250b811db2SDavid Herrmann return 0; 44260b811db2SDavid Herrmann } 44270b811db2SDavid Herrmann 44281da177e4SLinus Torvalds /* Range of port numbers used to automatically bind. 44291da177e4SLinus Torvalds Need to determine whether we should perform a name_bind 44301da177e4SLinus Torvalds permission check between the socket and the port number. */ 44311da177e4SLinus Torvalds 44321da177e4SLinus Torvalds static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen) 44331da177e4SLinus Torvalds { 4434253bfae6SPaul Moore struct sock *sk = sock->sk; 44350f8db8ccSAlexey Kodanev struct sk_security_struct *sksec = sk->sk_security; 44361da177e4SLinus Torvalds u16 family; 44371da177e4SLinus Torvalds int err; 44381da177e4SLinus Torvalds 4439be0554c9SStephen Smalley err = sock_has_perm(sk, SOCKET__BIND); 44401da177e4SLinus Torvalds if (err) 44411da177e4SLinus Torvalds goto out; 44421da177e4SLinus Torvalds 4443d452930fSRichard Haines /* If PF_INET or PF_INET6, check name_bind permission for the port. */ 4444253bfae6SPaul Moore family = sk->sk_family; 44451da177e4SLinus Torvalds if (family == PF_INET || family == PF_INET6) { 44461da177e4SLinus Torvalds char *addrp; 44472bf49690SThomas Liu struct common_audit_data ad; 444848c62af6SEric Paris struct lsm_network_audit net = {0,}; 44491da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 44501da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 44510f8db8ccSAlexey Kodanev u16 family_sa = address->sa_family; 44521da177e4SLinus Torvalds unsigned short snum; 4453e399f982SJames Morris u32 sid, node_perm; 44541da177e4SLinus Torvalds 4455d452930fSRichard Haines /* 4456d452930fSRichard Haines * sctp_bindx(3) calls via selinux_sctp_bind_connect() 4457d452930fSRichard Haines * that validates multiple binding addresses. Because of this 4458d452930fSRichard Haines * need to check address->sa_family as it is possible to have 4459d452930fSRichard Haines * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET. 4460d452930fSRichard Haines */ 44610f8db8ccSAlexey Kodanev switch (family_sa) { 44620f8db8ccSAlexey Kodanev case AF_UNSPEC: 446368741a8aSRichard Haines case AF_INET: 446468741a8aSRichard Haines if (addrlen < sizeof(struct sockaddr_in)) 446568741a8aSRichard Haines return -EINVAL; 44661da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 44670f8db8ccSAlexey Kodanev if (family_sa == AF_UNSPEC) { 44680f8db8ccSAlexey Kodanev /* see __inet_bind(), we only want to allow 44690f8db8ccSAlexey Kodanev * AF_UNSPEC if the address is INADDR_ANY 44700f8db8ccSAlexey Kodanev */ 44710f8db8ccSAlexey Kodanev if (addr4->sin_addr.s_addr != htonl(INADDR_ANY)) 44720f8db8ccSAlexey Kodanev goto err_af; 44730f8db8ccSAlexey Kodanev family_sa = AF_INET; 44740f8db8ccSAlexey Kodanev } 44751da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 44761da177e4SLinus Torvalds addrp = (char *)&addr4->sin_addr.s_addr; 447768741a8aSRichard Haines break; 447868741a8aSRichard Haines case AF_INET6: 447968741a8aSRichard Haines if (addrlen < SIN6_LEN_RFC2133) 448068741a8aSRichard Haines return -EINVAL; 44811da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 44821da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 44831da177e4SLinus Torvalds addrp = (char *)&addr6->sin6_addr.s6_addr; 448468741a8aSRichard Haines break; 448568741a8aSRichard Haines default: 44860f8db8ccSAlexey Kodanev goto err_af; 44871da177e4SLinus Torvalds } 44881da177e4SLinus Torvalds 448988b7d370SAlexey Kodanev ad.type = LSM_AUDIT_DATA_NET; 449088b7d370SAlexey Kodanev ad.u.net = &net; 449188b7d370SAlexey Kodanev ad.u.net->sport = htons(snum); 449288b7d370SAlexey Kodanev ad.u.net->family = family_sa; 449388b7d370SAlexey Kodanev 4494227b60f5SStephen Hemminger if (snum) { 4495227b60f5SStephen Hemminger int low, high; 4496227b60f5SStephen Hemminger 44970bbf87d8SEric W. Biederman inet_get_local_port_range(sock_net(sk), &low, &high); 4498227b60f5SStephen Hemminger 44994548b683SKrister Johansen if (snum < max(inet_prot_sock(sock_net(sk)), low) || 45004548b683SKrister Johansen snum > high) { 45013e112172SPaul Moore err = sel_netport_sid(sk->sk_protocol, 45023e112172SPaul Moore snum, &sid); 45031da177e4SLinus Torvalds if (err) 45041da177e4SLinus Torvalds goto out; 45056b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 45066b6bc620SStephen Smalley sksec->sid, sid, 4507253bfae6SPaul Moore sksec->sclass, 45081da177e4SLinus Torvalds SOCKET__NAME_BIND, &ad); 45091da177e4SLinus Torvalds if (err) 45101da177e4SLinus Torvalds goto out; 45111da177e4SLinus Torvalds } 4512227b60f5SStephen Hemminger } 45131da177e4SLinus Torvalds 4514253bfae6SPaul Moore switch (sksec->sclass) { 451513402580SJames Morris case SECCLASS_TCP_SOCKET: 45161da177e4SLinus Torvalds node_perm = TCP_SOCKET__NODE_BIND; 45171da177e4SLinus Torvalds break; 45181da177e4SLinus Torvalds 451913402580SJames Morris case SECCLASS_UDP_SOCKET: 45201da177e4SLinus Torvalds node_perm = UDP_SOCKET__NODE_BIND; 45211da177e4SLinus Torvalds break; 45221da177e4SLinus Torvalds 45232ee92d46SJames Morris case SECCLASS_DCCP_SOCKET: 45242ee92d46SJames Morris node_perm = DCCP_SOCKET__NODE_BIND; 45252ee92d46SJames Morris break; 45262ee92d46SJames Morris 4527d452930fSRichard Haines case SECCLASS_SCTP_SOCKET: 4528d452930fSRichard Haines node_perm = SCTP_SOCKET__NODE_BIND; 4529d452930fSRichard Haines break; 4530d452930fSRichard Haines 45311da177e4SLinus Torvalds default: 45321da177e4SLinus Torvalds node_perm = RAWIP_SOCKET__NODE_BIND; 45331da177e4SLinus Torvalds break; 45341da177e4SLinus Torvalds } 45351da177e4SLinus Torvalds 453688b7d370SAlexey Kodanev err = sel_netnode_sid(addrp, family_sa, &sid); 45371da177e4SLinus Torvalds if (err) 45381da177e4SLinus Torvalds goto out; 45391da177e4SLinus Torvalds 45400f8db8ccSAlexey Kodanev if (family_sa == AF_INET) 454148c62af6SEric Paris ad.u.net->v4info.saddr = addr4->sin_addr.s_addr; 45421da177e4SLinus Torvalds else 454348c62af6SEric Paris ad.u.net->v6info.saddr = addr6->sin6_addr; 45441da177e4SLinus Torvalds 45456b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 45466b6bc620SStephen Smalley sksec->sid, sid, 4547253bfae6SPaul Moore sksec->sclass, node_perm, &ad); 45481da177e4SLinus Torvalds if (err) 45491da177e4SLinus Torvalds goto out; 45501da177e4SLinus Torvalds } 45511da177e4SLinus Torvalds out: 45521da177e4SLinus Torvalds return err; 45530f8db8ccSAlexey Kodanev err_af: 45540f8db8ccSAlexey Kodanev /* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */ 45550f8db8ccSAlexey Kodanev if (sksec->sclass == SECCLASS_SCTP_SOCKET) 45560f8db8ccSAlexey Kodanev return -EINVAL; 45570f8db8ccSAlexey Kodanev return -EAFNOSUPPORT; 45581da177e4SLinus Torvalds } 45591da177e4SLinus Torvalds 4560d452930fSRichard Haines /* This supports connect(2) and SCTP connect services such as sctp_connectx(3) 45615fb94e9cSMauro Carvalho Chehab * and sctp_sendmsg(3) as described in Documentation/security/LSM-sctp.rst 4562d452930fSRichard Haines */ 4563d452930fSRichard Haines static int selinux_socket_connect_helper(struct socket *sock, 4564d452930fSRichard Haines struct sockaddr *address, int addrlen) 45651da177e4SLinus Torvalds { 4566014ab19aSPaul Moore struct sock *sk = sock->sk; 4567253bfae6SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 45681da177e4SLinus Torvalds int err; 45691da177e4SLinus Torvalds 4570be0554c9SStephen Smalley err = sock_has_perm(sk, SOCKET__CONNECT); 45711da177e4SLinus Torvalds if (err) 45721da177e4SLinus Torvalds return err; 45731da177e4SLinus Torvalds 45741da177e4SLinus Torvalds /* 4575d452930fSRichard Haines * If a TCP, DCCP or SCTP socket, check name_connect permission 4576d452930fSRichard Haines * for the port. 45771da177e4SLinus Torvalds */ 4578253bfae6SPaul Moore if (sksec->sclass == SECCLASS_TCP_SOCKET || 4579d452930fSRichard Haines sksec->sclass == SECCLASS_DCCP_SOCKET || 4580d452930fSRichard Haines sksec->sclass == SECCLASS_SCTP_SOCKET) { 45812bf49690SThomas Liu struct common_audit_data ad; 458248c62af6SEric Paris struct lsm_network_audit net = {0,}; 45831da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 45841da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 45851da177e4SLinus Torvalds unsigned short snum; 45862ee92d46SJames Morris u32 sid, perm; 45871da177e4SLinus Torvalds 4588d452930fSRichard Haines /* sctp_connectx(3) calls via selinux_sctp_bind_connect() 4589d452930fSRichard Haines * that validates multiple connect addresses. Because of this 4590d452930fSRichard Haines * need to check address->sa_family as it is possible to have 4591d452930fSRichard Haines * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET. 4592d452930fSRichard Haines */ 459368741a8aSRichard Haines switch (address->sa_family) { 459468741a8aSRichard Haines case AF_INET: 45951da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 4596911656f8SStephen Smalley if (addrlen < sizeof(struct sockaddr_in)) 45971da177e4SLinus Torvalds return -EINVAL; 45981da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 459968741a8aSRichard Haines break; 460068741a8aSRichard Haines case AF_INET6: 46011da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 4602911656f8SStephen Smalley if (addrlen < SIN6_LEN_RFC2133) 46031da177e4SLinus Torvalds return -EINVAL; 46041da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 460568741a8aSRichard Haines break; 460668741a8aSRichard Haines default: 460768741a8aSRichard Haines /* Note that SCTP services expect -EINVAL, whereas 460868741a8aSRichard Haines * others expect -EAFNOSUPPORT. 460968741a8aSRichard Haines */ 461068741a8aSRichard Haines if (sksec->sclass == SECCLASS_SCTP_SOCKET) 461168741a8aSRichard Haines return -EINVAL; 461268741a8aSRichard Haines else 461368741a8aSRichard Haines return -EAFNOSUPPORT; 46141da177e4SLinus Torvalds } 46151da177e4SLinus Torvalds 46163e112172SPaul Moore err = sel_netport_sid(sk->sk_protocol, snum, &sid); 46171da177e4SLinus Torvalds if (err) 4618d452930fSRichard Haines return err; 46191da177e4SLinus Torvalds 4620d452930fSRichard Haines switch (sksec->sclass) { 4621d452930fSRichard Haines case SECCLASS_TCP_SOCKET: 4622d452930fSRichard Haines perm = TCP_SOCKET__NAME_CONNECT; 4623d452930fSRichard Haines break; 4624d452930fSRichard Haines case SECCLASS_DCCP_SOCKET: 4625d452930fSRichard Haines perm = DCCP_SOCKET__NAME_CONNECT; 4626d452930fSRichard Haines break; 4627d452930fSRichard Haines case SECCLASS_SCTP_SOCKET: 4628d452930fSRichard Haines perm = SCTP_SOCKET__NAME_CONNECT; 4629d452930fSRichard Haines break; 4630d452930fSRichard Haines } 46312ee92d46SJames Morris 463250c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 463348c62af6SEric Paris ad.u.net = &net; 463448c62af6SEric Paris ad.u.net->dport = htons(snum); 463588b7d370SAlexey Kodanev ad.u.net->family = address->sa_family; 46366b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 46376b6bc620SStephen Smalley sksec->sid, sid, sksec->sclass, perm, &ad); 46381da177e4SLinus Torvalds if (err) 4639d452930fSRichard Haines return err; 46401da177e4SLinus Torvalds } 46411da177e4SLinus Torvalds 4642d452930fSRichard Haines return 0; 4643d452930fSRichard Haines } 4644014ab19aSPaul Moore 4645d452930fSRichard Haines /* Supports connect(2), see comments in selinux_socket_connect_helper() */ 4646d452930fSRichard Haines static int selinux_socket_connect(struct socket *sock, 4647d452930fSRichard Haines struct sockaddr *address, int addrlen) 4648d452930fSRichard Haines { 4649d452930fSRichard Haines int err; 4650d452930fSRichard Haines struct sock *sk = sock->sk; 4651d452930fSRichard Haines 4652d452930fSRichard Haines err = selinux_socket_connect_helper(sock, address, addrlen); 4653d452930fSRichard Haines if (err) 46541da177e4SLinus Torvalds return err; 4655d452930fSRichard Haines 4656d452930fSRichard Haines return selinux_netlbl_socket_connect(sk, address); 46571da177e4SLinus Torvalds } 46581da177e4SLinus Torvalds 46591da177e4SLinus Torvalds static int selinux_socket_listen(struct socket *sock, int backlog) 46601da177e4SLinus Torvalds { 4661be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__LISTEN); 46621da177e4SLinus Torvalds } 46631da177e4SLinus Torvalds 46641da177e4SLinus Torvalds static int selinux_socket_accept(struct socket *sock, struct socket *newsock) 46651da177e4SLinus Torvalds { 46661da177e4SLinus Torvalds int err; 46671da177e4SLinus Torvalds struct inode_security_struct *isec; 46681da177e4SLinus Torvalds struct inode_security_struct *newisec; 46699287aed2SAndreas Gruenbacher u16 sclass; 46709287aed2SAndreas Gruenbacher u32 sid; 46711da177e4SLinus Torvalds 4672be0554c9SStephen Smalley err = sock_has_perm(sock->sk, SOCKET__ACCEPT); 46731da177e4SLinus Torvalds if (err) 46741da177e4SLinus Torvalds return err; 46751da177e4SLinus Torvalds 46765d226df4SAndreas Gruenbacher isec = inode_security_novalidate(SOCK_INODE(sock)); 46779287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 46789287aed2SAndreas Gruenbacher sclass = isec->sclass; 46799287aed2SAndreas Gruenbacher sid = isec->sid; 46809287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 46819287aed2SAndreas Gruenbacher 46829287aed2SAndreas Gruenbacher newisec = inode_security_novalidate(SOCK_INODE(newsock)); 46839287aed2SAndreas Gruenbacher newisec->sclass = sclass; 46849287aed2SAndreas Gruenbacher newisec->sid = sid; 46856f3be9f5SAndreas Gruenbacher newisec->initialized = LABEL_INITIALIZED; 46861da177e4SLinus Torvalds 46871da177e4SLinus Torvalds return 0; 46881da177e4SLinus Torvalds } 46891da177e4SLinus Torvalds 46901da177e4SLinus Torvalds static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg, 46911da177e4SLinus Torvalds int size) 46921da177e4SLinus Torvalds { 4693be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__WRITE); 46941da177e4SLinus Torvalds } 46951da177e4SLinus Torvalds 46961da177e4SLinus Torvalds static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg, 46971da177e4SLinus Torvalds int size, int flags) 46981da177e4SLinus Torvalds { 4699be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__READ); 47001da177e4SLinus Torvalds } 47011da177e4SLinus Torvalds 47021da177e4SLinus Torvalds static int selinux_socket_getsockname(struct socket *sock) 47031da177e4SLinus Torvalds { 4704be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__GETATTR); 47051da177e4SLinus Torvalds } 47061da177e4SLinus Torvalds 47071da177e4SLinus Torvalds static int selinux_socket_getpeername(struct socket *sock) 47081da177e4SLinus Torvalds { 4709be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__GETATTR); 47101da177e4SLinus Torvalds } 47111da177e4SLinus Torvalds 47121da177e4SLinus Torvalds static int selinux_socket_setsockopt(struct socket *sock, int level, int optname) 47131da177e4SLinus Torvalds { 4714f8687afeSPaul Moore int err; 4715f8687afeSPaul Moore 4716be0554c9SStephen Smalley err = sock_has_perm(sock->sk, SOCKET__SETOPT); 4717f8687afeSPaul Moore if (err) 4718f8687afeSPaul Moore return err; 4719f8687afeSPaul Moore 4720f8687afeSPaul Moore return selinux_netlbl_socket_setsockopt(sock, level, optname); 47211da177e4SLinus Torvalds } 47221da177e4SLinus Torvalds 47231da177e4SLinus Torvalds static int selinux_socket_getsockopt(struct socket *sock, int level, 47241da177e4SLinus Torvalds int optname) 47251da177e4SLinus Torvalds { 4726be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__GETOPT); 47271da177e4SLinus Torvalds } 47281da177e4SLinus Torvalds 47291da177e4SLinus Torvalds static int selinux_socket_shutdown(struct socket *sock, int how) 47301da177e4SLinus Torvalds { 4731be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__SHUTDOWN); 47321da177e4SLinus Torvalds } 47331da177e4SLinus Torvalds 47343610cda5SDavid S. Miller static int selinux_socket_unix_stream_connect(struct sock *sock, 47353610cda5SDavid S. Miller struct sock *other, 47361da177e4SLinus Torvalds struct sock *newsk) 47371da177e4SLinus Torvalds { 47383610cda5SDavid S. Miller struct sk_security_struct *sksec_sock = sock->sk_security; 47393610cda5SDavid S. Miller struct sk_security_struct *sksec_other = other->sk_security; 47404d1e2451SPaul Moore struct sk_security_struct *sksec_new = newsk->sk_security; 47412bf49690SThomas Liu struct common_audit_data ad; 474248c62af6SEric Paris struct lsm_network_audit net = {0,}; 47431da177e4SLinus Torvalds int err; 47441da177e4SLinus Torvalds 474550c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 474648c62af6SEric Paris ad.u.net = &net; 474748c62af6SEric Paris ad.u.net->sk = other; 47481da177e4SLinus Torvalds 47496b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 47506b6bc620SStephen Smalley sksec_sock->sid, sksec_other->sid, 47514d1e2451SPaul Moore sksec_other->sclass, 47521da177e4SLinus Torvalds UNIX_STREAM_SOCKET__CONNECTTO, &ad); 47531da177e4SLinus Torvalds if (err) 47541da177e4SLinus Torvalds return err; 47551da177e4SLinus Torvalds 47561da177e4SLinus Torvalds /* server child socket */ 47574d1e2451SPaul Moore sksec_new->peer_sid = sksec_sock->sid; 4758aa8e712cSStephen Smalley err = security_sid_mls_copy(&selinux_state, sksec_other->sid, 4759aa8e712cSStephen Smalley sksec_sock->sid, &sksec_new->sid); 47604d1e2451SPaul Moore if (err) 47614237c75cSVenkat Yekkirala return err; 47624d1e2451SPaul Moore 47634d1e2451SPaul Moore /* connecting socket */ 47644d1e2451SPaul Moore sksec_sock->peer_sid = sksec_new->sid; 47654d1e2451SPaul Moore 47664d1e2451SPaul Moore return 0; 47671da177e4SLinus Torvalds } 47681da177e4SLinus Torvalds 47691da177e4SLinus Torvalds static int selinux_socket_unix_may_send(struct socket *sock, 47701da177e4SLinus Torvalds struct socket *other) 47711da177e4SLinus Torvalds { 4772253bfae6SPaul Moore struct sk_security_struct *ssec = sock->sk->sk_security; 4773253bfae6SPaul Moore struct sk_security_struct *osec = other->sk->sk_security; 47742bf49690SThomas Liu struct common_audit_data ad; 477548c62af6SEric Paris struct lsm_network_audit net = {0,}; 47761da177e4SLinus Torvalds 477750c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 477848c62af6SEric Paris ad.u.net = &net; 477948c62af6SEric Paris ad.u.net->sk = other->sk; 47801da177e4SLinus Torvalds 47816b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 47826b6bc620SStephen Smalley ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO, 4783253bfae6SPaul Moore &ad); 47841da177e4SLinus Torvalds } 47851da177e4SLinus Torvalds 4786cbe0d6e8SPaul Moore static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex, 4787cbe0d6e8SPaul Moore char *addrp, u16 family, u32 peer_sid, 47882bf49690SThomas Liu struct common_audit_data *ad) 4789effad8dfSPaul Moore { 4790effad8dfSPaul Moore int err; 4791effad8dfSPaul Moore u32 if_sid; 4792effad8dfSPaul Moore u32 node_sid; 4793effad8dfSPaul Moore 4794cbe0d6e8SPaul Moore err = sel_netif_sid(ns, ifindex, &if_sid); 4795effad8dfSPaul Moore if (err) 4796effad8dfSPaul Moore return err; 47976b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 47986b6bc620SStephen Smalley peer_sid, if_sid, 4799effad8dfSPaul Moore SECCLASS_NETIF, NETIF__INGRESS, ad); 4800effad8dfSPaul Moore if (err) 4801effad8dfSPaul Moore return err; 4802effad8dfSPaul Moore 4803effad8dfSPaul Moore err = sel_netnode_sid(addrp, family, &node_sid); 4804effad8dfSPaul Moore if (err) 4805effad8dfSPaul Moore return err; 48066b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 48076b6bc620SStephen Smalley peer_sid, node_sid, 4808effad8dfSPaul Moore SECCLASS_NODE, NODE__RECVFROM, ad); 4809effad8dfSPaul Moore } 4810effad8dfSPaul Moore 4811220deb96SPaul Moore static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb, 4812d8395c87SPaul Moore u16 family) 4813220deb96SPaul Moore { 4814277d342fSPaul Moore int err = 0; 4815220deb96SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 4816220deb96SPaul Moore u32 sk_sid = sksec->sid; 48172bf49690SThomas Liu struct common_audit_data ad; 481848c62af6SEric Paris struct lsm_network_audit net = {0,}; 4819d8395c87SPaul Moore char *addrp; 4820d8395c87SPaul Moore 482150c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 482248c62af6SEric Paris ad.u.net = &net; 482348c62af6SEric Paris ad.u.net->netif = skb->skb_iif; 482448c62af6SEric Paris ad.u.net->family = family; 4825d8395c87SPaul Moore err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); 4826d8395c87SPaul Moore if (err) 4827d8395c87SPaul Moore return err; 4828220deb96SPaul Moore 482958bfbb51SPaul Moore if (selinux_secmark_enabled()) { 48306b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 48316b6bc620SStephen Smalley sk_sid, skb->secmark, SECCLASS_PACKET, 4832d8395c87SPaul Moore PACKET__RECV, &ad); 4833220deb96SPaul Moore if (err) 4834220deb96SPaul Moore return err; 483558bfbb51SPaul Moore } 4836220deb96SPaul Moore 4837d8395c87SPaul Moore err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad); 4838220deb96SPaul Moore if (err) 4839220deb96SPaul Moore return err; 4840d8395c87SPaul Moore err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad); 4841220deb96SPaul Moore 48424e5ab4cbSJames Morris return err; 48434e5ab4cbSJames Morris } 4844d28d1e08STrent Jaeger 48454e5ab4cbSJames Morris static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) 48464e5ab4cbSJames Morris { 4847220deb96SPaul Moore int err; 48484237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 4849220deb96SPaul Moore u16 family = sk->sk_family; 4850220deb96SPaul Moore u32 sk_sid = sksec->sid; 48512bf49690SThomas Liu struct common_audit_data ad; 485248c62af6SEric Paris struct lsm_network_audit net = {0,}; 4853220deb96SPaul Moore char *addrp; 4854d8395c87SPaul Moore u8 secmark_active; 4855d8395c87SPaul Moore u8 peerlbl_active; 48564e5ab4cbSJames Morris 48574e5ab4cbSJames Morris if (family != PF_INET && family != PF_INET6) 4858220deb96SPaul Moore return 0; 48594e5ab4cbSJames Morris 48604e5ab4cbSJames Morris /* Handle mapped IPv4 packets arriving via IPv6 sockets */ 486187fcd70dSAl Viro if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 48624e5ab4cbSJames Morris family = PF_INET; 48634e5ab4cbSJames Morris 4864d8395c87SPaul Moore /* If any sort of compatibility mode is enabled then handoff processing 4865d8395c87SPaul Moore * to the selinux_sock_rcv_skb_compat() function to deal with the 4866d8395c87SPaul Moore * special handling. We do this in an attempt to keep this function 4867d8395c87SPaul Moore * as fast and as clean as possible. */ 4868aa8e712cSStephen Smalley if (!selinux_policycap_netpeer()) 4869d8395c87SPaul Moore return selinux_sock_rcv_skb_compat(sk, skb, family); 4870d8395c87SPaul Moore 4871d8395c87SPaul Moore secmark_active = selinux_secmark_enabled(); 48722be4d74fSChris PeBenito peerlbl_active = selinux_peerlbl_enabled(); 4873d8395c87SPaul Moore if (!secmark_active && !peerlbl_active) 4874d8395c87SPaul Moore return 0; 4875d8395c87SPaul Moore 487650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 487748c62af6SEric Paris ad.u.net = &net; 487848c62af6SEric Paris ad.u.net->netif = skb->skb_iif; 487948c62af6SEric Paris ad.u.net->family = family; 4880224dfbd8SPaul Moore err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); 48814e5ab4cbSJames Morris if (err) 4882220deb96SPaul Moore return err; 48834e5ab4cbSJames Morris 4884d8395c87SPaul Moore if (peerlbl_active) { 4885d621d35eSPaul Moore u32 peer_sid; 4886220deb96SPaul Moore 4887220deb96SPaul Moore err = selinux_skb_peerlbl_sid(skb, family, &peer_sid); 4888220deb96SPaul Moore if (err) 4889220deb96SPaul Moore return err; 4890cbe0d6e8SPaul Moore err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif, 4891cbe0d6e8SPaul Moore addrp, family, peer_sid, &ad); 4892dfaebe98SPaul Moore if (err) { 4893a04e71f6SHuw Davies selinux_netlbl_err(skb, family, err, 0); 4894effad8dfSPaul Moore return err; 4895dfaebe98SPaul Moore } 48966b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 48976b6bc620SStephen Smalley sk_sid, peer_sid, SECCLASS_PEER, 4898d621d35eSPaul Moore PEER__RECV, &ad); 489946d01d63SChad Hanson if (err) { 4900a04e71f6SHuw Davies selinux_netlbl_err(skb, family, err, 0); 490146d01d63SChad Hanson return err; 490246d01d63SChad Hanson } 4903d621d35eSPaul Moore } 4904d621d35eSPaul Moore 4905d8395c87SPaul Moore if (secmark_active) { 49066b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 49076b6bc620SStephen Smalley sk_sid, skb->secmark, SECCLASS_PACKET, 4908effad8dfSPaul Moore PACKET__RECV, &ad); 4909effad8dfSPaul Moore if (err) 4910effad8dfSPaul Moore return err; 4911effad8dfSPaul Moore } 4912effad8dfSPaul Moore 4913d621d35eSPaul Moore return err; 49141da177e4SLinus Torvalds } 49151da177e4SLinus Torvalds 49162c7946a7SCatherine Zhang static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval, 49171da177e4SLinus Torvalds int __user *optlen, unsigned len) 49181da177e4SLinus Torvalds { 49191da177e4SLinus Torvalds int err = 0; 49201da177e4SLinus Torvalds char *scontext; 49211da177e4SLinus Torvalds u32 scontext_len; 4922253bfae6SPaul Moore struct sk_security_struct *sksec = sock->sk->sk_security; 49233de4bab5SPaul Moore u32 peer_sid = SECSID_NULL; 49241da177e4SLinus Torvalds 4925253bfae6SPaul Moore if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET || 4926d452930fSRichard Haines sksec->sclass == SECCLASS_TCP_SOCKET || 4927d452930fSRichard Haines sksec->sclass == SECCLASS_SCTP_SOCKET) 4928dd3e7836SEric Paris peer_sid = sksec->peer_sid; 4929253bfae6SPaul Moore if (peer_sid == SECSID_NULL) 4930253bfae6SPaul Moore return -ENOPROTOOPT; 49311da177e4SLinus Torvalds 4932aa8e712cSStephen Smalley err = security_sid_to_context(&selinux_state, peer_sid, &scontext, 4933aa8e712cSStephen Smalley &scontext_len); 49341da177e4SLinus Torvalds if (err) 4935253bfae6SPaul Moore return err; 49361da177e4SLinus Torvalds 49371da177e4SLinus Torvalds if (scontext_len > len) { 49381da177e4SLinus Torvalds err = -ERANGE; 49391da177e4SLinus Torvalds goto out_len; 49401da177e4SLinus Torvalds } 49411da177e4SLinus Torvalds 49421da177e4SLinus Torvalds if (copy_to_user(optval, scontext, scontext_len)) 49431da177e4SLinus Torvalds err = -EFAULT; 49441da177e4SLinus Torvalds 49451da177e4SLinus Torvalds out_len: 49461da177e4SLinus Torvalds if (put_user(scontext_len, optlen)) 49471da177e4SLinus Torvalds err = -EFAULT; 49481da177e4SLinus Torvalds kfree(scontext); 49491da177e4SLinus Torvalds return err; 49501da177e4SLinus Torvalds } 49511da177e4SLinus Torvalds 4952dc49c1f9SCatherine Zhang static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid) 49532c7946a7SCatherine Zhang { 4954dc49c1f9SCatherine Zhang u32 peer_secid = SECSID_NULL; 495575e22910SPaul Moore u16 family; 4956899134f2SPaul Moore struct inode_security_struct *isec; 4957877ce7c1SCatherine Zhang 4958aa862900SPaul Moore if (skb && skb->protocol == htons(ETH_P_IP)) 4959aa862900SPaul Moore family = PF_INET; 4960aa862900SPaul Moore else if (skb && skb->protocol == htons(ETH_P_IPV6)) 4961aa862900SPaul Moore family = PF_INET6; 4962aa862900SPaul Moore else if (sock) 496375e22910SPaul Moore family = sock->sk->sk_family; 496475e22910SPaul Moore else 496575e22910SPaul Moore goto out; 496675e22910SPaul Moore 4967899134f2SPaul Moore if (sock && family == PF_UNIX) { 4968899134f2SPaul Moore isec = inode_security_novalidate(SOCK_INODE(sock)); 4969899134f2SPaul Moore peer_secid = isec->sid; 4970899134f2SPaul Moore } else if (skb) 4971220deb96SPaul Moore selinux_skb_peerlbl_sid(skb, family, &peer_secid); 49722c7946a7SCatherine Zhang 497375e22910SPaul Moore out: 4974dc49c1f9SCatherine Zhang *secid = peer_secid; 497575e22910SPaul Moore if (peer_secid == SECSID_NULL) 497675e22910SPaul Moore return -EINVAL; 497775e22910SPaul Moore return 0; 49782c7946a7SCatherine Zhang } 49792c7946a7SCatherine Zhang 49807d877f3bSAl Viro static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority) 49811da177e4SLinus Torvalds { 498284914b7eSPaul Moore struct sk_security_struct *sksec; 498384914b7eSPaul Moore 498484914b7eSPaul Moore sksec = kzalloc(sizeof(*sksec), priority); 498584914b7eSPaul Moore if (!sksec) 498684914b7eSPaul Moore return -ENOMEM; 498784914b7eSPaul Moore 498884914b7eSPaul Moore sksec->peer_sid = SECINITSID_UNLABELED; 498984914b7eSPaul Moore sksec->sid = SECINITSID_UNLABELED; 49905dee25d0SStephen Smalley sksec->sclass = SECCLASS_SOCKET; 499184914b7eSPaul Moore selinux_netlbl_sk_security_reset(sksec); 499284914b7eSPaul Moore sk->sk_security = sksec; 499384914b7eSPaul Moore 499484914b7eSPaul Moore return 0; 49951da177e4SLinus Torvalds } 49961da177e4SLinus Torvalds 49971da177e4SLinus Torvalds static void selinux_sk_free_security(struct sock *sk) 49981da177e4SLinus Torvalds { 499984914b7eSPaul Moore struct sk_security_struct *sksec = sk->sk_security; 500084914b7eSPaul Moore 500184914b7eSPaul Moore sk->sk_security = NULL; 500284914b7eSPaul Moore selinux_netlbl_sk_security_free(sksec); 500384914b7eSPaul Moore kfree(sksec); 50041da177e4SLinus Torvalds } 50051da177e4SLinus Torvalds 5006892c141eSVenkat Yekkirala static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk) 5007892c141eSVenkat Yekkirala { 5008dd3e7836SEric Paris struct sk_security_struct *sksec = sk->sk_security; 5009dd3e7836SEric Paris struct sk_security_struct *newsksec = newsk->sk_security; 5010892c141eSVenkat Yekkirala 5011dd3e7836SEric Paris newsksec->sid = sksec->sid; 5012dd3e7836SEric Paris newsksec->peer_sid = sksec->peer_sid; 5013dd3e7836SEric Paris newsksec->sclass = sksec->sclass; 501499f59ed0SPaul Moore 5015dd3e7836SEric Paris selinux_netlbl_sk_security_reset(newsksec); 5016892c141eSVenkat Yekkirala } 5017892c141eSVenkat Yekkirala 5018beb8d13bSVenkat Yekkirala static void selinux_sk_getsecid(struct sock *sk, u32 *secid) 5019d28d1e08STrent Jaeger { 5020d28d1e08STrent Jaeger if (!sk) 5021beb8d13bSVenkat Yekkirala *secid = SECINITSID_ANY_SOCKET; 5022892c141eSVenkat Yekkirala else { 5023892c141eSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 5024d28d1e08STrent Jaeger 5025beb8d13bSVenkat Yekkirala *secid = sksec->sid; 5026892c141eSVenkat Yekkirala } 5027d28d1e08STrent Jaeger } 5028d28d1e08STrent Jaeger 50299a673e56SAdrian Bunk static void selinux_sock_graft(struct sock *sk, struct socket *parent) 50304237c75cSVenkat Yekkirala { 50315d226df4SAndreas Gruenbacher struct inode_security_struct *isec = 50325d226df4SAndreas Gruenbacher inode_security_novalidate(SOCK_INODE(parent)); 50334237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 50344237c75cSVenkat Yekkirala 50352873ead7SPaul Moore if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 || 50362873ead7SPaul Moore sk->sk_family == PF_UNIX) 50374237c75cSVenkat Yekkirala isec->sid = sksec->sid; 5038220deb96SPaul Moore sksec->sclass = isec->sclass; 50394237c75cSVenkat Yekkirala } 50404237c75cSVenkat Yekkirala 5041d452930fSRichard Haines /* Called whenever SCTP receives an INIT chunk. This happens when an incoming 5042d452930fSRichard Haines * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association 5043d452930fSRichard Haines * already present). 5044d452930fSRichard Haines */ 5045d452930fSRichard Haines static int selinux_sctp_assoc_request(struct sctp_endpoint *ep, 5046d452930fSRichard Haines struct sk_buff *skb) 5047d452930fSRichard Haines { 5048d452930fSRichard Haines struct sk_security_struct *sksec = ep->base.sk->sk_security; 5049d452930fSRichard Haines struct common_audit_data ad; 5050d452930fSRichard Haines struct lsm_network_audit net = {0,}; 5051d452930fSRichard Haines u8 peerlbl_active; 5052d452930fSRichard Haines u32 peer_sid = SECINITSID_UNLABELED; 5053d452930fSRichard Haines u32 conn_sid; 5054d452930fSRichard Haines int err = 0; 5055d452930fSRichard Haines 5056aa8e712cSStephen Smalley if (!selinux_policycap_extsockclass()) 5057d452930fSRichard Haines return 0; 5058d452930fSRichard Haines 5059d452930fSRichard Haines peerlbl_active = selinux_peerlbl_enabled(); 5060d452930fSRichard Haines 5061d452930fSRichard Haines if (peerlbl_active) { 5062d452930fSRichard Haines /* This will return peer_sid = SECSID_NULL if there are 5063d452930fSRichard Haines * no peer labels, see security_net_peersid_resolve(). 5064d452930fSRichard Haines */ 5065d452930fSRichard Haines err = selinux_skb_peerlbl_sid(skb, ep->base.sk->sk_family, 5066d452930fSRichard Haines &peer_sid); 5067d452930fSRichard Haines if (err) 5068d452930fSRichard Haines return err; 5069d452930fSRichard Haines 5070d452930fSRichard Haines if (peer_sid == SECSID_NULL) 5071d452930fSRichard Haines peer_sid = SECINITSID_UNLABELED; 5072d452930fSRichard Haines } 5073d452930fSRichard Haines 5074d452930fSRichard Haines if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) { 5075d452930fSRichard Haines sksec->sctp_assoc_state = SCTP_ASSOC_SET; 5076d452930fSRichard Haines 5077d452930fSRichard Haines /* Here as first association on socket. As the peer SID 5078d452930fSRichard Haines * was allowed by peer recv (and the netif/node checks), 5079d452930fSRichard Haines * then it is approved by policy and used as the primary 5080d452930fSRichard Haines * peer SID for getpeercon(3). 5081d452930fSRichard Haines */ 5082d452930fSRichard Haines sksec->peer_sid = peer_sid; 5083d452930fSRichard Haines } else if (sksec->peer_sid != peer_sid) { 5084d452930fSRichard Haines /* Other association peer SIDs are checked to enforce 5085d452930fSRichard Haines * consistency among the peer SIDs. 5086d452930fSRichard Haines */ 5087d452930fSRichard Haines ad.type = LSM_AUDIT_DATA_NET; 5088d452930fSRichard Haines ad.u.net = &net; 5089d452930fSRichard Haines ad.u.net->sk = ep->base.sk; 50906b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 50916b6bc620SStephen Smalley sksec->peer_sid, peer_sid, sksec->sclass, 5092d452930fSRichard Haines SCTP_SOCKET__ASSOCIATION, &ad); 5093d452930fSRichard Haines if (err) 5094d452930fSRichard Haines return err; 5095d452930fSRichard Haines } 5096d452930fSRichard Haines 5097d452930fSRichard Haines /* Compute the MLS component for the connection and store 5098d452930fSRichard Haines * the information in ep. This will be used by SCTP TCP type 5099d452930fSRichard Haines * sockets and peeled off connections as they cause a new 5100d452930fSRichard Haines * socket to be generated. selinux_sctp_sk_clone() will then 5101d452930fSRichard Haines * plug this into the new socket. 5102d452930fSRichard Haines */ 5103d452930fSRichard Haines err = selinux_conn_sid(sksec->sid, peer_sid, &conn_sid); 5104d452930fSRichard Haines if (err) 5105d452930fSRichard Haines return err; 5106d452930fSRichard Haines 5107d452930fSRichard Haines ep->secid = conn_sid; 5108d452930fSRichard Haines ep->peer_secid = peer_sid; 5109d452930fSRichard Haines 5110d452930fSRichard Haines /* Set any NetLabel labels including CIPSO/CALIPSO options. */ 5111d452930fSRichard Haines return selinux_netlbl_sctp_assoc_request(ep, skb); 5112d452930fSRichard Haines } 5113d452930fSRichard Haines 5114d452930fSRichard Haines /* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting 5115d452930fSRichard Haines * based on their @optname. 5116d452930fSRichard Haines */ 5117d452930fSRichard Haines static int selinux_sctp_bind_connect(struct sock *sk, int optname, 5118d452930fSRichard Haines struct sockaddr *address, 5119d452930fSRichard Haines int addrlen) 5120d452930fSRichard Haines { 5121d452930fSRichard Haines int len, err = 0, walk_size = 0; 5122d452930fSRichard Haines void *addr_buf; 5123d452930fSRichard Haines struct sockaddr *addr; 5124d452930fSRichard Haines struct socket *sock; 5125d452930fSRichard Haines 5126aa8e712cSStephen Smalley if (!selinux_policycap_extsockclass()) 5127d452930fSRichard Haines return 0; 5128d452930fSRichard Haines 5129d452930fSRichard Haines /* Process one or more addresses that may be IPv4 or IPv6 */ 5130d452930fSRichard Haines sock = sk->sk_socket; 5131d452930fSRichard Haines addr_buf = address; 5132d452930fSRichard Haines 5133d452930fSRichard Haines while (walk_size < addrlen) { 5134c138325fSOndrej Mosnacek if (walk_size + sizeof(sa_family_t) > addrlen) 5135c138325fSOndrej Mosnacek return -EINVAL; 5136c138325fSOndrej Mosnacek 5137d452930fSRichard Haines addr = addr_buf; 5138d452930fSRichard Haines switch (addr->sa_family) { 51394152dc91SAlexey Kodanev case AF_UNSPEC: 5140d452930fSRichard Haines case AF_INET: 5141d452930fSRichard Haines len = sizeof(struct sockaddr_in); 5142d452930fSRichard Haines break; 5143d452930fSRichard Haines case AF_INET6: 5144d452930fSRichard Haines len = sizeof(struct sockaddr_in6); 5145d452930fSRichard Haines break; 5146d452930fSRichard Haines default: 51474152dc91SAlexey Kodanev return -EINVAL; 5148d452930fSRichard Haines } 5149d452930fSRichard Haines 5150*292c997aSXin Long if (walk_size + len > addrlen) 5151*292c997aSXin Long return -EINVAL; 5152*292c997aSXin Long 5153d452930fSRichard Haines err = -EINVAL; 5154d452930fSRichard Haines switch (optname) { 5155d452930fSRichard Haines /* Bind checks */ 5156d452930fSRichard Haines case SCTP_PRIMARY_ADDR: 5157d452930fSRichard Haines case SCTP_SET_PEER_PRIMARY_ADDR: 5158d452930fSRichard Haines case SCTP_SOCKOPT_BINDX_ADD: 5159d452930fSRichard Haines err = selinux_socket_bind(sock, addr, len); 5160d452930fSRichard Haines break; 5161d452930fSRichard Haines /* Connect checks */ 5162d452930fSRichard Haines case SCTP_SOCKOPT_CONNECTX: 5163d452930fSRichard Haines case SCTP_PARAM_SET_PRIMARY: 5164d452930fSRichard Haines case SCTP_PARAM_ADD_IP: 5165d452930fSRichard Haines case SCTP_SENDMSG_CONNECT: 5166d452930fSRichard Haines err = selinux_socket_connect_helper(sock, addr, len); 5167d452930fSRichard Haines if (err) 5168d452930fSRichard Haines return err; 5169d452930fSRichard Haines 5170d452930fSRichard Haines /* As selinux_sctp_bind_connect() is called by the 5171d452930fSRichard Haines * SCTP protocol layer, the socket is already locked, 5172d452930fSRichard Haines * therefore selinux_netlbl_socket_connect_locked() is 5173d452930fSRichard Haines * is called here. The situations handled are: 5174d452930fSRichard Haines * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2), 5175d452930fSRichard Haines * whenever a new IP address is added or when a new 5176d452930fSRichard Haines * primary address is selected. 5177d452930fSRichard Haines * Note that an SCTP connect(2) call happens before 5178d452930fSRichard Haines * the SCTP protocol layer and is handled via 5179d452930fSRichard Haines * selinux_socket_connect(). 5180d452930fSRichard Haines */ 5181d452930fSRichard Haines err = selinux_netlbl_socket_connect_locked(sk, addr); 5182d452930fSRichard Haines break; 5183d452930fSRichard Haines } 5184d452930fSRichard Haines 5185d452930fSRichard Haines if (err) 5186d452930fSRichard Haines return err; 5187d452930fSRichard Haines 5188d452930fSRichard Haines addr_buf += len; 5189d452930fSRichard Haines walk_size += len; 5190d452930fSRichard Haines } 5191d452930fSRichard Haines 5192d452930fSRichard Haines return 0; 5193d452930fSRichard Haines } 5194d452930fSRichard Haines 5195d452930fSRichard Haines /* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */ 5196d452930fSRichard Haines static void selinux_sctp_sk_clone(struct sctp_endpoint *ep, struct sock *sk, 5197d452930fSRichard Haines struct sock *newsk) 5198d452930fSRichard Haines { 5199d452930fSRichard Haines struct sk_security_struct *sksec = sk->sk_security; 5200d452930fSRichard Haines struct sk_security_struct *newsksec = newsk->sk_security; 5201d452930fSRichard Haines 5202d452930fSRichard Haines /* If policy does not support SECCLASS_SCTP_SOCKET then call 5203d452930fSRichard Haines * the non-sctp clone version. 5204d452930fSRichard Haines */ 5205aa8e712cSStephen Smalley if (!selinux_policycap_extsockclass()) 5206d452930fSRichard Haines return selinux_sk_clone_security(sk, newsk); 5207d452930fSRichard Haines 5208d452930fSRichard Haines newsksec->sid = ep->secid; 5209d452930fSRichard Haines newsksec->peer_sid = ep->peer_secid; 5210d452930fSRichard Haines newsksec->sclass = sksec->sclass; 5211d452930fSRichard Haines selinux_netlbl_sctp_sk_clone(sk, newsk); 5212d452930fSRichard Haines } 5213d452930fSRichard Haines 52149a673e56SAdrian Bunk static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb, 52154237c75cSVenkat Yekkirala struct request_sock *req) 52164237c75cSVenkat Yekkirala { 52174237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 52184237c75cSVenkat Yekkirala int err; 52190b1f24e6SPaul Moore u16 family = req->rsk_ops->family; 5220446b8024SPaul Moore u32 connsid; 52214237c75cSVenkat Yekkirala u32 peersid; 52224237c75cSVenkat Yekkirala 5223aa862900SPaul Moore err = selinux_skb_peerlbl_sid(skb, family, &peersid); 5224220deb96SPaul Moore if (err) 5225220deb96SPaul Moore return err; 5226446b8024SPaul Moore err = selinux_conn_sid(sksec->sid, peersid, &connsid); 52274237c75cSVenkat Yekkirala if (err) 52284237c75cSVenkat Yekkirala return err; 5229446b8024SPaul Moore req->secid = connsid; 52306b877699SVenkat Yekkirala req->peer_secid = peersid; 5231389fb800SPaul Moore 5232389fb800SPaul Moore return selinux_netlbl_inet_conn_request(req, family); 52334237c75cSVenkat Yekkirala } 52344237c75cSVenkat Yekkirala 52359a673e56SAdrian Bunk static void selinux_inet_csk_clone(struct sock *newsk, 52369a673e56SAdrian Bunk const struct request_sock *req) 52374237c75cSVenkat Yekkirala { 52384237c75cSVenkat Yekkirala struct sk_security_struct *newsksec = newsk->sk_security; 52394237c75cSVenkat Yekkirala 52404237c75cSVenkat Yekkirala newsksec->sid = req->secid; 52416b877699SVenkat Yekkirala newsksec->peer_sid = req->peer_secid; 52424237c75cSVenkat Yekkirala /* NOTE: Ideally, we should also get the isec->sid for the 52434237c75cSVenkat Yekkirala new socket in sync, but we don't have the isec available yet. 52444237c75cSVenkat Yekkirala So we will wait until sock_graft to do it, by which 52454237c75cSVenkat Yekkirala time it will have been created and available. */ 524699f59ed0SPaul Moore 52479f2ad665SPaul Moore /* We don't need to take any sort of lock here as we are the only 52489f2ad665SPaul Moore * thread with access to newsksec */ 5249389fb800SPaul Moore selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family); 52504237c75cSVenkat Yekkirala } 52514237c75cSVenkat Yekkirala 5252014ab19aSPaul Moore static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb) 52536b877699SVenkat Yekkirala { 5254aa862900SPaul Moore u16 family = sk->sk_family; 52556b877699SVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 52566b877699SVenkat Yekkirala 5257aa862900SPaul Moore /* handle mapped IPv4 packets arriving via IPv6 sockets */ 5258aa862900SPaul Moore if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 5259aa862900SPaul Moore family = PF_INET; 5260aa862900SPaul Moore 5261aa862900SPaul Moore selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid); 52626b877699SVenkat Yekkirala } 52636b877699SVenkat Yekkirala 52642606fd1fSEric Paris static int selinux_secmark_relabel_packet(u32 sid) 52652606fd1fSEric Paris { 52662606fd1fSEric Paris const struct task_security_struct *__tsec; 52672606fd1fSEric Paris u32 tsid; 52682606fd1fSEric Paris 52692606fd1fSEric Paris __tsec = current_security(); 52702606fd1fSEric Paris tsid = __tsec->sid; 52712606fd1fSEric Paris 52726b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 52736b6bc620SStephen Smalley tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, 52746b6bc620SStephen Smalley NULL); 52752606fd1fSEric Paris } 52762606fd1fSEric Paris 52772606fd1fSEric Paris static void selinux_secmark_refcount_inc(void) 52782606fd1fSEric Paris { 52792606fd1fSEric Paris atomic_inc(&selinux_secmark_refcount); 52802606fd1fSEric Paris } 52812606fd1fSEric Paris 52822606fd1fSEric Paris static void selinux_secmark_refcount_dec(void) 52832606fd1fSEric Paris { 52842606fd1fSEric Paris atomic_dec(&selinux_secmark_refcount); 52852606fd1fSEric Paris } 52862606fd1fSEric Paris 52879a673e56SAdrian Bunk static void selinux_req_classify_flow(const struct request_sock *req, 52889a673e56SAdrian Bunk struct flowi *fl) 52894237c75cSVenkat Yekkirala { 52901d28f42cSDavid S. Miller fl->flowi_secid = req->secid; 52914237c75cSVenkat Yekkirala } 52924237c75cSVenkat Yekkirala 52935dbbaf2dSPaul Moore static int selinux_tun_dev_alloc_security(void **security) 52945dbbaf2dSPaul Moore { 52955dbbaf2dSPaul Moore struct tun_security_struct *tunsec; 52965dbbaf2dSPaul Moore 52975dbbaf2dSPaul Moore tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL); 52985dbbaf2dSPaul Moore if (!tunsec) 52995dbbaf2dSPaul Moore return -ENOMEM; 53005dbbaf2dSPaul Moore tunsec->sid = current_sid(); 53015dbbaf2dSPaul Moore 53025dbbaf2dSPaul Moore *security = tunsec; 53035dbbaf2dSPaul Moore return 0; 53045dbbaf2dSPaul Moore } 53055dbbaf2dSPaul Moore 53065dbbaf2dSPaul Moore static void selinux_tun_dev_free_security(void *security) 53075dbbaf2dSPaul Moore { 53085dbbaf2dSPaul Moore kfree(security); 53095dbbaf2dSPaul Moore } 53105dbbaf2dSPaul Moore 5311ed6d76e4SPaul Moore static int selinux_tun_dev_create(void) 5312ed6d76e4SPaul Moore { 5313ed6d76e4SPaul Moore u32 sid = current_sid(); 5314ed6d76e4SPaul Moore 5315ed6d76e4SPaul Moore /* we aren't taking into account the "sockcreate" SID since the socket 5316ed6d76e4SPaul Moore * that is being created here is not a socket in the traditional sense, 5317ed6d76e4SPaul Moore * instead it is a private sock, accessible only to the kernel, and 5318ed6d76e4SPaul Moore * representing a wide range of network traffic spanning multiple 5319ed6d76e4SPaul Moore * connections unlike traditional sockets - check the TUN driver to 5320ed6d76e4SPaul Moore * get a better understanding of why this socket is special */ 5321ed6d76e4SPaul Moore 53226b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 53236b6bc620SStephen Smalley sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE, 5324ed6d76e4SPaul Moore NULL); 5325ed6d76e4SPaul Moore } 5326ed6d76e4SPaul Moore 53275dbbaf2dSPaul Moore static int selinux_tun_dev_attach_queue(void *security) 5328ed6d76e4SPaul Moore { 53295dbbaf2dSPaul Moore struct tun_security_struct *tunsec = security; 53305dbbaf2dSPaul Moore 53316b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 53326b6bc620SStephen Smalley current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET, 53335dbbaf2dSPaul Moore TUN_SOCKET__ATTACH_QUEUE, NULL); 53345dbbaf2dSPaul Moore } 53355dbbaf2dSPaul Moore 53365dbbaf2dSPaul Moore static int selinux_tun_dev_attach(struct sock *sk, void *security) 53375dbbaf2dSPaul Moore { 53385dbbaf2dSPaul Moore struct tun_security_struct *tunsec = security; 5339ed6d76e4SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 5340ed6d76e4SPaul Moore 5341ed6d76e4SPaul Moore /* we don't currently perform any NetLabel based labeling here and it 5342ed6d76e4SPaul Moore * isn't clear that we would want to do so anyway; while we could apply 5343ed6d76e4SPaul Moore * labeling without the support of the TUN user the resulting labeled 5344ed6d76e4SPaul Moore * traffic from the other end of the connection would almost certainly 5345ed6d76e4SPaul Moore * cause confusion to the TUN user that had no idea network labeling 5346ed6d76e4SPaul Moore * protocols were being used */ 5347ed6d76e4SPaul Moore 53485dbbaf2dSPaul Moore sksec->sid = tunsec->sid; 5349ed6d76e4SPaul Moore sksec->sclass = SECCLASS_TUN_SOCKET; 53505dbbaf2dSPaul Moore 53515dbbaf2dSPaul Moore return 0; 5352ed6d76e4SPaul Moore } 5353ed6d76e4SPaul Moore 53545dbbaf2dSPaul Moore static int selinux_tun_dev_open(void *security) 5355ed6d76e4SPaul Moore { 53565dbbaf2dSPaul Moore struct tun_security_struct *tunsec = security; 5357ed6d76e4SPaul Moore u32 sid = current_sid(); 5358ed6d76e4SPaul Moore int err; 5359ed6d76e4SPaul Moore 53606b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 53616b6bc620SStephen Smalley sid, tunsec->sid, SECCLASS_TUN_SOCKET, 5362ed6d76e4SPaul Moore TUN_SOCKET__RELABELFROM, NULL); 5363ed6d76e4SPaul Moore if (err) 5364ed6d76e4SPaul Moore return err; 53656b6bc620SStephen Smalley err = avc_has_perm(&selinux_state, 53666b6bc620SStephen Smalley sid, sid, SECCLASS_TUN_SOCKET, 5367ed6d76e4SPaul Moore TUN_SOCKET__RELABELTO, NULL); 5368ed6d76e4SPaul Moore if (err) 5369ed6d76e4SPaul Moore return err; 53705dbbaf2dSPaul Moore tunsec->sid = sid; 5371ed6d76e4SPaul Moore 5372ed6d76e4SPaul Moore return 0; 5373ed6d76e4SPaul Moore } 5374ed6d76e4SPaul Moore 53751da177e4SLinus Torvalds static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb) 53761da177e4SLinus Torvalds { 53771da177e4SLinus Torvalds int err = 0; 53781da177e4SLinus Torvalds u32 perm; 53791da177e4SLinus Torvalds struct nlmsghdr *nlh; 5380253bfae6SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 53811da177e4SLinus Torvalds 538277954983SHong zhi guo if (skb->len < NLMSG_HDRLEN) { 53831da177e4SLinus Torvalds err = -EINVAL; 53841da177e4SLinus Torvalds goto out; 53851da177e4SLinus Torvalds } 5386b529ccf2SArnaldo Carvalho de Melo nlh = nlmsg_hdr(skb); 53871da177e4SLinus Torvalds 5388253bfae6SPaul Moore err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm); 53891da177e4SLinus Torvalds if (err) { 53901da177e4SLinus Torvalds if (err == -EINVAL) { 539176319946SVladis Dronov pr_warn_ratelimited("SELinux: unrecognized netlink" 539276319946SVladis Dronov " message: protocol=%hu nlmsg_type=%hu sclass=%s" 539376319946SVladis Dronov " pig=%d comm=%s\n", 5394cded3fffSMarek Milkovic sk->sk_protocol, nlh->nlmsg_type, 539576319946SVladis Dronov secclass_map[sksec->sclass - 1].name, 539676319946SVladis Dronov task_pid_nr(current), current->comm); 5397e5a5ca96SPaul Moore if (!enforcing_enabled(&selinux_state) || 5398aa8e712cSStephen Smalley security_get_allow_unknown(&selinux_state)) 53991da177e4SLinus Torvalds err = 0; 54001da177e4SLinus Torvalds } 54011da177e4SLinus Torvalds 54021da177e4SLinus Torvalds /* Ignore */ 54031da177e4SLinus Torvalds if (err == -ENOENT) 54041da177e4SLinus Torvalds err = 0; 54051da177e4SLinus Torvalds goto out; 54061da177e4SLinus Torvalds } 54071da177e4SLinus Torvalds 5408be0554c9SStephen Smalley err = sock_has_perm(sk, perm); 54091da177e4SLinus Torvalds out: 54101da177e4SLinus Torvalds return err; 54111da177e4SLinus Torvalds } 54121da177e4SLinus Torvalds 54131da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER 54141da177e4SLinus Torvalds 5415cbe0d6e8SPaul Moore static unsigned int selinux_ip_forward(struct sk_buff *skb, 5416cbe0d6e8SPaul Moore const struct net_device *indev, 5417effad8dfSPaul Moore u16 family) 54181da177e4SLinus Torvalds { 5419dfaebe98SPaul Moore int err; 5420effad8dfSPaul Moore char *addrp; 5421effad8dfSPaul Moore u32 peer_sid; 54222bf49690SThomas Liu struct common_audit_data ad; 542348c62af6SEric Paris struct lsm_network_audit net = {0,}; 5424effad8dfSPaul Moore u8 secmark_active; 5425948bf85cSPaul Moore u8 netlbl_active; 5426effad8dfSPaul Moore u8 peerlbl_active; 54274237c75cSVenkat Yekkirala 5428aa8e712cSStephen Smalley if (!selinux_policycap_netpeer()) 5429effad8dfSPaul Moore return NF_ACCEPT; 54304237c75cSVenkat Yekkirala 5431effad8dfSPaul Moore secmark_active = selinux_secmark_enabled(); 5432948bf85cSPaul Moore netlbl_active = netlbl_enabled(); 54332be4d74fSChris PeBenito peerlbl_active = selinux_peerlbl_enabled(); 5434effad8dfSPaul Moore if (!secmark_active && !peerlbl_active) 5435effad8dfSPaul Moore return NF_ACCEPT; 54364237c75cSVenkat Yekkirala 5437d8395c87SPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0) 5438d8395c87SPaul Moore return NF_DROP; 5439d8395c87SPaul Moore 544050c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 544148c62af6SEric Paris ad.u.net = &net; 5442cbe0d6e8SPaul Moore ad.u.net->netif = indev->ifindex; 544348c62af6SEric Paris ad.u.net->family = family; 5444effad8dfSPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0) 5445effad8dfSPaul Moore return NF_DROP; 54461da177e4SLinus Torvalds 5447dfaebe98SPaul Moore if (peerlbl_active) { 5448cbe0d6e8SPaul Moore err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex, 5449cbe0d6e8SPaul Moore addrp, family, peer_sid, &ad); 5450dfaebe98SPaul Moore if (err) { 5451a04e71f6SHuw Davies selinux_netlbl_err(skb, family, err, 1); 5452effad8dfSPaul Moore return NF_DROP; 5453dfaebe98SPaul Moore } 5454dfaebe98SPaul Moore } 5455effad8dfSPaul Moore 5456effad8dfSPaul Moore if (secmark_active) 54576b6bc620SStephen Smalley if (avc_has_perm(&selinux_state, 54586b6bc620SStephen Smalley peer_sid, skb->secmark, 5459effad8dfSPaul Moore SECCLASS_PACKET, PACKET__FORWARD_IN, &ad)) 5460effad8dfSPaul Moore return NF_DROP; 5461effad8dfSPaul Moore 5462948bf85cSPaul Moore if (netlbl_active) 5463948bf85cSPaul Moore /* we do this in the FORWARD path and not the POST_ROUTING 5464948bf85cSPaul Moore * path because we want to make sure we apply the necessary 5465948bf85cSPaul Moore * labeling before IPsec is applied so we can leverage AH 5466948bf85cSPaul Moore * protection */ 5467948bf85cSPaul Moore if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0) 5468948bf85cSPaul Moore return NF_DROP; 5469948bf85cSPaul Moore 5470effad8dfSPaul Moore return NF_ACCEPT; 5471effad8dfSPaul Moore } 5472effad8dfSPaul Moore 547306198b34SEric W. Biederman static unsigned int selinux_ipv4_forward(void *priv, 5474effad8dfSPaul Moore struct sk_buff *skb, 5475238e54c9SDavid S. Miller const struct nf_hook_state *state) 5476effad8dfSPaul Moore { 5477238e54c9SDavid S. Miller return selinux_ip_forward(skb, state->in, PF_INET); 5478effad8dfSPaul Moore } 5479effad8dfSPaul Moore 54801a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 548106198b34SEric W. Biederman static unsigned int selinux_ipv6_forward(void *priv, 5482effad8dfSPaul Moore struct sk_buff *skb, 5483238e54c9SDavid S. Miller const struct nf_hook_state *state) 5484effad8dfSPaul Moore { 5485238e54c9SDavid S. Miller return selinux_ip_forward(skb, state->in, PF_INET6); 5486effad8dfSPaul Moore } 5487effad8dfSPaul Moore #endif /* IPV6 */ 5488effad8dfSPaul Moore 5489948bf85cSPaul Moore static unsigned int selinux_ip_output(struct sk_buff *skb, 5490948bf85cSPaul Moore u16 family) 5491948bf85cSPaul Moore { 549247180068SPaul Moore struct sock *sk; 5493948bf85cSPaul Moore u32 sid; 5494948bf85cSPaul Moore 5495948bf85cSPaul Moore if (!netlbl_enabled()) 5496948bf85cSPaul Moore return NF_ACCEPT; 5497948bf85cSPaul Moore 5498948bf85cSPaul Moore /* we do this in the LOCAL_OUT path and not the POST_ROUTING path 5499948bf85cSPaul Moore * because we want to make sure we apply the necessary labeling 5500948bf85cSPaul Moore * before IPsec is applied so we can leverage AH protection */ 550147180068SPaul Moore sk = skb->sk; 550247180068SPaul Moore if (sk) { 550347180068SPaul Moore struct sk_security_struct *sksec; 550447180068SPaul Moore 5505e446f9dfSEric Dumazet if (sk_listener(sk)) 550647180068SPaul Moore /* if the socket is the listening state then this 550747180068SPaul Moore * packet is a SYN-ACK packet which means it needs to 550847180068SPaul Moore * be labeled based on the connection/request_sock and 550947180068SPaul Moore * not the parent socket. unfortunately, we can't 551047180068SPaul Moore * lookup the request_sock yet as it isn't queued on 551147180068SPaul Moore * the parent socket until after the SYN-ACK is sent. 551247180068SPaul Moore * the "solution" is to simply pass the packet as-is 551347180068SPaul Moore * as any IP option based labeling should be copied 551447180068SPaul Moore * from the initial connection request (in the IP 551547180068SPaul Moore * layer). it is far from ideal, but until we get a 551647180068SPaul Moore * security label in the packet itself this is the 551747180068SPaul Moore * best we can do. */ 551847180068SPaul Moore return NF_ACCEPT; 551947180068SPaul Moore 552047180068SPaul Moore /* standard practice, label using the parent socket */ 552147180068SPaul Moore sksec = sk->sk_security; 5522948bf85cSPaul Moore sid = sksec->sid; 5523948bf85cSPaul Moore } else 5524948bf85cSPaul Moore sid = SECINITSID_KERNEL; 5525948bf85cSPaul Moore if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0) 5526948bf85cSPaul Moore return NF_DROP; 5527948bf85cSPaul Moore 5528948bf85cSPaul Moore return NF_ACCEPT; 5529948bf85cSPaul Moore } 5530948bf85cSPaul Moore 553106198b34SEric W. Biederman static unsigned int selinux_ipv4_output(void *priv, 5532948bf85cSPaul Moore struct sk_buff *skb, 5533238e54c9SDavid S. Miller const struct nf_hook_state *state) 5534948bf85cSPaul Moore { 5535948bf85cSPaul Moore return selinux_ip_output(skb, PF_INET); 5536948bf85cSPaul Moore } 5537948bf85cSPaul Moore 55381a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 55392917f57bSHuw Davies static unsigned int selinux_ipv6_output(void *priv, 55402917f57bSHuw Davies struct sk_buff *skb, 55412917f57bSHuw Davies const struct nf_hook_state *state) 55422917f57bSHuw Davies { 55432917f57bSHuw Davies return selinux_ip_output(skb, PF_INET6); 55442917f57bSHuw Davies } 55452917f57bSHuw Davies #endif /* IPV6 */ 55462917f57bSHuw Davies 5547effad8dfSPaul Moore static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb, 5548effad8dfSPaul Moore int ifindex, 5549d8395c87SPaul Moore u16 family) 55504e5ab4cbSJames Morris { 555154abc686SEric Dumazet struct sock *sk = skb_to_full_sk(skb); 55524237c75cSVenkat Yekkirala struct sk_security_struct *sksec; 55532bf49690SThomas Liu struct common_audit_data ad; 555448c62af6SEric Paris struct lsm_network_audit net = {0,}; 5555d8395c87SPaul Moore char *addrp; 5556d8395c87SPaul Moore u8 proto; 55574e5ab4cbSJames Morris 5558effad8dfSPaul Moore if (sk == NULL) 5559effad8dfSPaul Moore return NF_ACCEPT; 55604237c75cSVenkat Yekkirala sksec = sk->sk_security; 55614e5ab4cbSJames Morris 556250c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 556348c62af6SEric Paris ad.u.net = &net; 556448c62af6SEric Paris ad.u.net->netif = ifindex; 556548c62af6SEric Paris ad.u.net->family = family; 5566d8395c87SPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto)) 5567d8395c87SPaul Moore return NF_DROP; 5568d8395c87SPaul Moore 556958bfbb51SPaul Moore if (selinux_secmark_enabled()) 55706b6bc620SStephen Smalley if (avc_has_perm(&selinux_state, 55716b6bc620SStephen Smalley sksec->sid, skb->secmark, 5572d8395c87SPaul Moore SECCLASS_PACKET, PACKET__SEND, &ad)) 55732fe66ec2SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 55741da177e4SLinus Torvalds 5575d8395c87SPaul Moore if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto)) 55762fe66ec2SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 5577effad8dfSPaul Moore 5578effad8dfSPaul Moore return NF_ACCEPT; 5579effad8dfSPaul Moore } 5580effad8dfSPaul Moore 5581cbe0d6e8SPaul Moore static unsigned int selinux_ip_postroute(struct sk_buff *skb, 5582cbe0d6e8SPaul Moore const struct net_device *outdev, 5583effad8dfSPaul Moore u16 family) 5584effad8dfSPaul Moore { 5585effad8dfSPaul Moore u32 secmark_perm; 5586effad8dfSPaul Moore u32 peer_sid; 5587cbe0d6e8SPaul Moore int ifindex = outdev->ifindex; 5588effad8dfSPaul Moore struct sock *sk; 55892bf49690SThomas Liu struct common_audit_data ad; 559048c62af6SEric Paris struct lsm_network_audit net = {0,}; 5591effad8dfSPaul Moore char *addrp; 5592effad8dfSPaul Moore u8 secmark_active; 5593effad8dfSPaul Moore u8 peerlbl_active; 5594effad8dfSPaul Moore 5595effad8dfSPaul Moore /* If any sort of compatibility mode is enabled then handoff processing 5596effad8dfSPaul Moore * to the selinux_ip_postroute_compat() function to deal with the 5597effad8dfSPaul Moore * special handling. We do this in an attempt to keep this function 5598effad8dfSPaul Moore * as fast and as clean as possible. */ 5599aa8e712cSStephen Smalley if (!selinux_policycap_netpeer()) 5600d8395c87SPaul Moore return selinux_ip_postroute_compat(skb, ifindex, family); 5601c0828e50SPaul Moore 5602effad8dfSPaul Moore secmark_active = selinux_secmark_enabled(); 56032be4d74fSChris PeBenito peerlbl_active = selinux_peerlbl_enabled(); 5604effad8dfSPaul Moore if (!secmark_active && !peerlbl_active) 5605effad8dfSPaul Moore return NF_ACCEPT; 5606effad8dfSPaul Moore 560754abc686SEric Dumazet sk = skb_to_full_sk(skb); 5608c0828e50SPaul Moore 5609effad8dfSPaul Moore #ifdef CONFIG_XFRM 5610effad8dfSPaul Moore /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec 5611effad8dfSPaul Moore * packet transformation so allow the packet to pass without any checks 5612effad8dfSPaul Moore * since we'll have another chance to perform access control checks 5613effad8dfSPaul Moore * when the packet is on it's final way out. 5614effad8dfSPaul Moore * NOTE: there appear to be some IPv6 multicast cases where skb->dst 5615c0828e50SPaul Moore * is NULL, in this case go ahead and apply access control. 5616c0828e50SPaul Moore * NOTE: if this is a local socket (skb->sk != NULL) that is in the 5617c0828e50SPaul Moore * TCP listening state we cannot wait until the XFRM processing 5618c0828e50SPaul Moore * is done as we will miss out on the SA label if we do; 5619c0828e50SPaul Moore * unfortunately, this means more work, but it is only once per 5620c0828e50SPaul Moore * connection. */ 5621c0828e50SPaul Moore if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL && 5622e446f9dfSEric Dumazet !(sk && sk_listener(sk))) 5623effad8dfSPaul Moore return NF_ACCEPT; 5624effad8dfSPaul Moore #endif 5625effad8dfSPaul Moore 5626d8395c87SPaul Moore if (sk == NULL) { 5627446b8024SPaul Moore /* Without an associated socket the packet is either coming 5628446b8024SPaul Moore * from the kernel or it is being forwarded; check the packet 5629446b8024SPaul Moore * to determine which and if the packet is being forwarded 5630446b8024SPaul Moore * query the packet directly to determine the security label. */ 56314a7ab3dcSSteffen Klassert if (skb->skb_iif) { 5632d8395c87SPaul Moore secmark_perm = PACKET__FORWARD_OUT; 5633d8395c87SPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &peer_sid)) 563404f6d70fSEric Paris return NF_DROP; 56354a7ab3dcSSteffen Klassert } else { 56364a7ab3dcSSteffen Klassert secmark_perm = PACKET__SEND; 5637d8395c87SPaul Moore peer_sid = SECINITSID_KERNEL; 56384a7ab3dcSSteffen Klassert } 5639e446f9dfSEric Dumazet } else if (sk_listener(sk)) { 5640446b8024SPaul Moore /* Locally generated packet but the associated socket is in the 5641446b8024SPaul Moore * listening state which means this is a SYN-ACK packet. In 5642446b8024SPaul Moore * this particular case the correct security label is assigned 5643446b8024SPaul Moore * to the connection/request_sock but unfortunately we can't 5644446b8024SPaul Moore * query the request_sock as it isn't queued on the parent 5645446b8024SPaul Moore * socket until after the SYN-ACK packet is sent; the only 5646446b8024SPaul Moore * viable choice is to regenerate the label like we do in 5647446b8024SPaul Moore * selinux_inet_conn_request(). See also selinux_ip_output() 5648446b8024SPaul Moore * for similar problems. */ 5649446b8024SPaul Moore u32 skb_sid; 5650e446f9dfSEric Dumazet struct sk_security_struct *sksec; 5651e446f9dfSEric Dumazet 5652e446f9dfSEric Dumazet sksec = sk->sk_security; 5653446b8024SPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &skb_sid)) 5654446b8024SPaul Moore return NF_DROP; 5655c0828e50SPaul Moore /* At this point, if the returned skb peerlbl is SECSID_NULL 5656c0828e50SPaul Moore * and the packet has been through at least one XFRM 5657c0828e50SPaul Moore * transformation then we must be dealing with the "final" 5658c0828e50SPaul Moore * form of labeled IPsec packet; since we've already applied 5659c0828e50SPaul Moore * all of our access controls on this packet we can safely 5660c0828e50SPaul Moore * pass the packet. */ 5661c0828e50SPaul Moore if (skb_sid == SECSID_NULL) { 5662c0828e50SPaul Moore switch (family) { 5663c0828e50SPaul Moore case PF_INET: 5664c0828e50SPaul Moore if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) 5665c0828e50SPaul Moore return NF_ACCEPT; 5666c0828e50SPaul Moore break; 5667c0828e50SPaul Moore case PF_INET6: 5668c0828e50SPaul Moore if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) 5669c0828e50SPaul Moore return NF_ACCEPT; 5670a7a91a19SPaul Moore break; 5671c0828e50SPaul Moore default: 5672c0828e50SPaul Moore return NF_DROP_ERR(-ECONNREFUSED); 5673c0828e50SPaul Moore } 5674c0828e50SPaul Moore } 5675446b8024SPaul Moore if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid)) 5676446b8024SPaul Moore return NF_DROP; 5677446b8024SPaul Moore secmark_perm = PACKET__SEND; 5678d8395c87SPaul Moore } else { 5679446b8024SPaul Moore /* Locally generated packet, fetch the security label from the 5680446b8024SPaul Moore * associated socket. */ 5681effad8dfSPaul Moore struct sk_security_struct *sksec = sk->sk_security; 5682effad8dfSPaul Moore peer_sid = sksec->sid; 5683effad8dfSPaul Moore secmark_perm = PACKET__SEND; 5684effad8dfSPaul Moore } 5685effad8dfSPaul Moore 568650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 568748c62af6SEric Paris ad.u.net = &net; 568848c62af6SEric Paris ad.u.net->netif = ifindex; 568948c62af6SEric Paris ad.u.net->family = family; 5690d8395c87SPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL)) 569104f6d70fSEric Paris return NF_DROP; 5692d8395c87SPaul Moore 5693effad8dfSPaul Moore if (secmark_active) 56946b6bc620SStephen Smalley if (avc_has_perm(&selinux_state, 56956b6bc620SStephen Smalley peer_sid, skb->secmark, 5696effad8dfSPaul Moore SECCLASS_PACKET, secmark_perm, &ad)) 56971f1aaf82SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 5698effad8dfSPaul Moore 5699effad8dfSPaul Moore if (peerlbl_active) { 5700effad8dfSPaul Moore u32 if_sid; 5701effad8dfSPaul Moore u32 node_sid; 5702effad8dfSPaul Moore 5703cbe0d6e8SPaul Moore if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid)) 570404f6d70fSEric Paris return NF_DROP; 57056b6bc620SStephen Smalley if (avc_has_perm(&selinux_state, 57066b6bc620SStephen Smalley peer_sid, if_sid, 5707effad8dfSPaul Moore SECCLASS_NETIF, NETIF__EGRESS, &ad)) 57081f1aaf82SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 5709effad8dfSPaul Moore 5710effad8dfSPaul Moore if (sel_netnode_sid(addrp, family, &node_sid)) 571104f6d70fSEric Paris return NF_DROP; 57126b6bc620SStephen Smalley if (avc_has_perm(&selinux_state, 57136b6bc620SStephen Smalley peer_sid, node_sid, 5714effad8dfSPaul Moore SECCLASS_NODE, NODE__SENDTO, &ad)) 57151f1aaf82SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 5716effad8dfSPaul Moore } 5717effad8dfSPaul Moore 5718effad8dfSPaul Moore return NF_ACCEPT; 5719effad8dfSPaul Moore } 5720effad8dfSPaul Moore 572106198b34SEric W. Biederman static unsigned int selinux_ipv4_postroute(void *priv, 5722a224be76SDavid S. Miller struct sk_buff *skb, 5723238e54c9SDavid S. Miller const struct nf_hook_state *state) 57241da177e4SLinus Torvalds { 5725238e54c9SDavid S. Miller return selinux_ip_postroute(skb, state->out, PF_INET); 57261da177e4SLinus Torvalds } 57271da177e4SLinus Torvalds 57281a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 572906198b34SEric W. Biederman static unsigned int selinux_ipv6_postroute(void *priv, 5730a224be76SDavid S. Miller struct sk_buff *skb, 5731238e54c9SDavid S. Miller const struct nf_hook_state *state) 57321da177e4SLinus Torvalds { 5733238e54c9SDavid S. Miller return selinux_ip_postroute(skb, state->out, PF_INET6); 57341da177e4SLinus Torvalds } 57351da177e4SLinus Torvalds #endif /* IPV6 */ 57361da177e4SLinus Torvalds 57371da177e4SLinus Torvalds #endif /* CONFIG_NETFILTER */ 57381da177e4SLinus Torvalds 57391da177e4SLinus Torvalds static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb) 57401da177e4SLinus Torvalds { 5741941fc5b2SStephen Smalley return selinux_nlmsg_perm(sk, skb); 57421da177e4SLinus Torvalds } 57431da177e4SLinus Torvalds 5744be0554c9SStephen Smalley static int ipc_alloc_security(struct kern_ipc_perm *perm, 57451da177e4SLinus Torvalds u16 sclass) 57461da177e4SLinus Torvalds { 57471da177e4SLinus Torvalds struct ipc_security_struct *isec; 57481da177e4SLinus Torvalds 574989d155efSJames Morris isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL); 57501da177e4SLinus Torvalds if (!isec) 57511da177e4SLinus Torvalds return -ENOMEM; 57521da177e4SLinus Torvalds 57531da177e4SLinus Torvalds isec->sclass = sclass; 5754be0554c9SStephen Smalley isec->sid = current_sid(); 57551da177e4SLinus Torvalds perm->security = isec; 57561da177e4SLinus Torvalds 57571da177e4SLinus Torvalds return 0; 57581da177e4SLinus Torvalds } 57591da177e4SLinus Torvalds 57601da177e4SLinus Torvalds static void ipc_free_security(struct kern_ipc_perm *perm) 57611da177e4SLinus Torvalds { 57621da177e4SLinus Torvalds struct ipc_security_struct *isec = perm->security; 57631da177e4SLinus Torvalds perm->security = NULL; 57641da177e4SLinus Torvalds kfree(isec); 57651da177e4SLinus Torvalds } 57661da177e4SLinus Torvalds 57671da177e4SLinus Torvalds static int msg_msg_alloc_security(struct msg_msg *msg) 57681da177e4SLinus Torvalds { 57691da177e4SLinus Torvalds struct msg_security_struct *msec; 57701da177e4SLinus Torvalds 577189d155efSJames Morris msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL); 57721da177e4SLinus Torvalds if (!msec) 57731da177e4SLinus Torvalds return -ENOMEM; 57741da177e4SLinus Torvalds 57751da177e4SLinus Torvalds msec->sid = SECINITSID_UNLABELED; 57761da177e4SLinus Torvalds msg->security = msec; 57771da177e4SLinus Torvalds 57781da177e4SLinus Torvalds return 0; 57791da177e4SLinus Torvalds } 57801da177e4SLinus Torvalds 57811da177e4SLinus Torvalds static void msg_msg_free_security(struct msg_msg *msg) 57821da177e4SLinus Torvalds { 57831da177e4SLinus Torvalds struct msg_security_struct *msec = msg->security; 57841da177e4SLinus Torvalds 57851da177e4SLinus Torvalds msg->security = NULL; 57861da177e4SLinus Torvalds kfree(msec); 57871da177e4SLinus Torvalds } 57881da177e4SLinus Torvalds 57891da177e4SLinus Torvalds static int ipc_has_perm(struct kern_ipc_perm *ipc_perms, 57906af963f1SStephen Smalley u32 perms) 57911da177e4SLinus Torvalds { 57921da177e4SLinus Torvalds struct ipc_security_struct *isec; 57932bf49690SThomas Liu struct common_audit_data ad; 5794275bb41eSDavid Howells u32 sid = current_sid(); 57951da177e4SLinus Torvalds 57961da177e4SLinus Torvalds isec = ipc_perms->security; 57971da177e4SLinus Torvalds 579850c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 57991da177e4SLinus Torvalds ad.u.ipc_id = ipc_perms->key; 58001da177e4SLinus Torvalds 58016b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 58026b6bc620SStephen Smalley sid, isec->sid, isec->sclass, perms, &ad); 58031da177e4SLinus Torvalds } 58041da177e4SLinus Torvalds 58051da177e4SLinus Torvalds static int selinux_msg_msg_alloc_security(struct msg_msg *msg) 58061da177e4SLinus Torvalds { 58071da177e4SLinus Torvalds return msg_msg_alloc_security(msg); 58081da177e4SLinus Torvalds } 58091da177e4SLinus Torvalds 58101da177e4SLinus Torvalds static void selinux_msg_msg_free_security(struct msg_msg *msg) 58111da177e4SLinus Torvalds { 58121da177e4SLinus Torvalds msg_msg_free_security(msg); 58131da177e4SLinus Torvalds } 58141da177e4SLinus Torvalds 58151da177e4SLinus Torvalds /* message queue security operations */ 5816d8c6e854SEric W. Biederman static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq) 58171da177e4SLinus Torvalds { 58181da177e4SLinus Torvalds struct ipc_security_struct *isec; 58192bf49690SThomas Liu struct common_audit_data ad; 5820275bb41eSDavid Howells u32 sid = current_sid(); 58211da177e4SLinus Torvalds int rc; 58221da177e4SLinus Torvalds 5823d8c6e854SEric W. Biederman rc = ipc_alloc_security(msq, SECCLASS_MSGQ); 58241da177e4SLinus Torvalds if (rc) 58251da177e4SLinus Torvalds return rc; 58261da177e4SLinus Torvalds 5827d8c6e854SEric W. Biederman isec = msq->security; 58281da177e4SLinus Torvalds 582950c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 5830d8c6e854SEric W. Biederman ad.u.ipc_id = msq->key; 58311da177e4SLinus Torvalds 58326b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 58336b6bc620SStephen Smalley sid, isec->sid, SECCLASS_MSGQ, 58341da177e4SLinus Torvalds MSGQ__CREATE, &ad); 58351da177e4SLinus Torvalds if (rc) { 5836d8c6e854SEric W. Biederman ipc_free_security(msq); 58371da177e4SLinus Torvalds return rc; 58381da177e4SLinus Torvalds } 58391da177e4SLinus Torvalds return 0; 58401da177e4SLinus Torvalds } 58411da177e4SLinus Torvalds 5842d8c6e854SEric W. Biederman static void selinux_msg_queue_free_security(struct kern_ipc_perm *msq) 58431da177e4SLinus Torvalds { 5844d8c6e854SEric W. Biederman ipc_free_security(msq); 58451da177e4SLinus Torvalds } 58461da177e4SLinus Torvalds 5847d8c6e854SEric W. Biederman static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg) 58481da177e4SLinus Torvalds { 58491da177e4SLinus Torvalds struct ipc_security_struct *isec; 58502bf49690SThomas Liu struct common_audit_data ad; 5851275bb41eSDavid Howells u32 sid = current_sid(); 58521da177e4SLinus Torvalds 5853d8c6e854SEric W. Biederman isec = msq->security; 58541da177e4SLinus Torvalds 585550c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 5856d8c6e854SEric W. Biederman ad.u.ipc_id = msq->key; 58571da177e4SLinus Torvalds 58586b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 58596b6bc620SStephen Smalley sid, isec->sid, SECCLASS_MSGQ, 58601da177e4SLinus Torvalds MSGQ__ASSOCIATE, &ad); 58611da177e4SLinus Torvalds } 58621da177e4SLinus Torvalds 5863d8c6e854SEric W. Biederman static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd) 58641da177e4SLinus Torvalds { 58651da177e4SLinus Torvalds int err; 58661da177e4SLinus Torvalds int perms; 58671da177e4SLinus Torvalds 58681da177e4SLinus Torvalds switch (cmd) { 58691da177e4SLinus Torvalds case IPC_INFO: 58701da177e4SLinus Torvalds case MSG_INFO: 58711da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 58726b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 58736b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, 5874be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); 58751da177e4SLinus Torvalds case IPC_STAT: 58761da177e4SLinus Torvalds case MSG_STAT: 587723c8cec8SDavidlohr Bueso case MSG_STAT_ANY: 58781da177e4SLinus Torvalds perms = MSGQ__GETATTR | MSGQ__ASSOCIATE; 58791da177e4SLinus Torvalds break; 58801da177e4SLinus Torvalds case IPC_SET: 58811da177e4SLinus Torvalds perms = MSGQ__SETATTR; 58821da177e4SLinus Torvalds break; 58831da177e4SLinus Torvalds case IPC_RMID: 58841da177e4SLinus Torvalds perms = MSGQ__DESTROY; 58851da177e4SLinus Torvalds break; 58861da177e4SLinus Torvalds default: 58871da177e4SLinus Torvalds return 0; 58881da177e4SLinus Torvalds } 58891da177e4SLinus Torvalds 5890d8c6e854SEric W. Biederman err = ipc_has_perm(msq, perms); 58911da177e4SLinus Torvalds return err; 58921da177e4SLinus Torvalds } 58931da177e4SLinus Torvalds 5894d8c6e854SEric W. Biederman static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg) 58951da177e4SLinus Torvalds { 58961da177e4SLinus Torvalds struct ipc_security_struct *isec; 58971da177e4SLinus Torvalds struct msg_security_struct *msec; 58982bf49690SThomas Liu struct common_audit_data ad; 5899275bb41eSDavid Howells u32 sid = current_sid(); 59001da177e4SLinus Torvalds int rc; 59011da177e4SLinus Torvalds 5902d8c6e854SEric W. Biederman isec = msq->security; 59031da177e4SLinus Torvalds msec = msg->security; 59041da177e4SLinus Torvalds 59051da177e4SLinus Torvalds /* 59061da177e4SLinus Torvalds * First time through, need to assign label to the message 59071da177e4SLinus Torvalds */ 59081da177e4SLinus Torvalds if (msec->sid == SECINITSID_UNLABELED) { 59091da177e4SLinus Torvalds /* 59101da177e4SLinus Torvalds * Compute new sid based on current process and 59111da177e4SLinus Torvalds * message queue this message will be stored in 59121da177e4SLinus Torvalds */ 5913aa8e712cSStephen Smalley rc = security_transition_sid(&selinux_state, sid, isec->sid, 5914aa8e712cSStephen Smalley SECCLASS_MSG, NULL, &msec->sid); 59151da177e4SLinus Torvalds if (rc) 59161da177e4SLinus Torvalds return rc; 59171da177e4SLinus Torvalds } 59181da177e4SLinus Torvalds 591950c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 5920d8c6e854SEric W. Biederman ad.u.ipc_id = msq->key; 59211da177e4SLinus Torvalds 59221da177e4SLinus Torvalds /* Can this process write to the queue? */ 59236b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 59246b6bc620SStephen Smalley sid, isec->sid, SECCLASS_MSGQ, 59251da177e4SLinus Torvalds MSGQ__WRITE, &ad); 59261da177e4SLinus Torvalds if (!rc) 59271da177e4SLinus Torvalds /* Can this process send the message */ 59286b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 59296b6bc620SStephen Smalley sid, msec->sid, SECCLASS_MSG, 5930275bb41eSDavid Howells MSG__SEND, &ad); 59311da177e4SLinus Torvalds if (!rc) 59321da177e4SLinus Torvalds /* Can the message be put in the queue? */ 59336b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 59346b6bc620SStephen Smalley msec->sid, isec->sid, SECCLASS_MSGQ, 5935275bb41eSDavid Howells MSGQ__ENQUEUE, &ad); 59361da177e4SLinus Torvalds 59371da177e4SLinus Torvalds return rc; 59381da177e4SLinus Torvalds } 59391da177e4SLinus Torvalds 5940d8c6e854SEric W. Biederman static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg, 59411da177e4SLinus Torvalds struct task_struct *target, 59421da177e4SLinus Torvalds long type, int mode) 59431da177e4SLinus Torvalds { 59441da177e4SLinus Torvalds struct ipc_security_struct *isec; 59451da177e4SLinus Torvalds struct msg_security_struct *msec; 59462bf49690SThomas Liu struct common_audit_data ad; 5947275bb41eSDavid Howells u32 sid = task_sid(target); 59481da177e4SLinus Torvalds int rc; 59491da177e4SLinus Torvalds 5950d8c6e854SEric W. Biederman isec = msq->security; 59511da177e4SLinus Torvalds msec = msg->security; 59521da177e4SLinus Torvalds 595350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 5954d8c6e854SEric W. Biederman ad.u.ipc_id = msq->key; 59551da177e4SLinus Torvalds 59566b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 59576b6bc620SStephen Smalley sid, isec->sid, 59581da177e4SLinus Torvalds SECCLASS_MSGQ, MSGQ__READ, &ad); 59591da177e4SLinus Torvalds if (!rc) 59606b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 59616b6bc620SStephen Smalley sid, msec->sid, 59621da177e4SLinus Torvalds SECCLASS_MSG, MSG__RECEIVE, &ad); 59631da177e4SLinus Torvalds return rc; 59641da177e4SLinus Torvalds } 59651da177e4SLinus Torvalds 59661da177e4SLinus Torvalds /* Shared Memory security operations */ 59677191adffSEric W. Biederman static int selinux_shm_alloc_security(struct kern_ipc_perm *shp) 59681da177e4SLinus Torvalds { 59691da177e4SLinus Torvalds struct ipc_security_struct *isec; 59702bf49690SThomas Liu struct common_audit_data ad; 5971275bb41eSDavid Howells u32 sid = current_sid(); 59721da177e4SLinus Torvalds int rc; 59731da177e4SLinus Torvalds 59747191adffSEric W. Biederman rc = ipc_alloc_security(shp, SECCLASS_SHM); 59751da177e4SLinus Torvalds if (rc) 59761da177e4SLinus Torvalds return rc; 59771da177e4SLinus Torvalds 59787191adffSEric W. Biederman isec = shp->security; 59791da177e4SLinus Torvalds 598050c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 59817191adffSEric W. Biederman ad.u.ipc_id = shp->key; 59821da177e4SLinus Torvalds 59836b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 59846b6bc620SStephen Smalley sid, isec->sid, SECCLASS_SHM, 59851da177e4SLinus Torvalds SHM__CREATE, &ad); 59861da177e4SLinus Torvalds if (rc) { 59877191adffSEric W. Biederman ipc_free_security(shp); 59881da177e4SLinus Torvalds return rc; 59891da177e4SLinus Torvalds } 59901da177e4SLinus Torvalds return 0; 59911da177e4SLinus Torvalds } 59921da177e4SLinus Torvalds 59937191adffSEric W. Biederman static void selinux_shm_free_security(struct kern_ipc_perm *shp) 59941da177e4SLinus Torvalds { 59957191adffSEric W. Biederman ipc_free_security(shp); 59961da177e4SLinus Torvalds } 59971da177e4SLinus Torvalds 59987191adffSEric W. Biederman static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg) 59991da177e4SLinus Torvalds { 60001da177e4SLinus Torvalds struct ipc_security_struct *isec; 60012bf49690SThomas Liu struct common_audit_data ad; 6002275bb41eSDavid Howells u32 sid = current_sid(); 60031da177e4SLinus Torvalds 60047191adffSEric W. Biederman isec = shp->security; 60051da177e4SLinus Torvalds 600650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 60077191adffSEric W. Biederman ad.u.ipc_id = shp->key; 60081da177e4SLinus Torvalds 60096b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 60106b6bc620SStephen Smalley sid, isec->sid, SECCLASS_SHM, 60111da177e4SLinus Torvalds SHM__ASSOCIATE, &ad); 60121da177e4SLinus Torvalds } 60131da177e4SLinus Torvalds 60141da177e4SLinus Torvalds /* Note, at this point, shp is locked down */ 60157191adffSEric W. Biederman static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd) 60161da177e4SLinus Torvalds { 60171da177e4SLinus Torvalds int perms; 60181da177e4SLinus Torvalds int err; 60191da177e4SLinus Torvalds 60201da177e4SLinus Torvalds switch (cmd) { 60211da177e4SLinus Torvalds case IPC_INFO: 60221da177e4SLinus Torvalds case SHM_INFO: 60231da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 60246b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 60256b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, 6026be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); 60271da177e4SLinus Torvalds case IPC_STAT: 60281da177e4SLinus Torvalds case SHM_STAT: 6029c21a6970SDavidlohr Bueso case SHM_STAT_ANY: 60301da177e4SLinus Torvalds perms = SHM__GETATTR | SHM__ASSOCIATE; 60311da177e4SLinus Torvalds break; 60321da177e4SLinus Torvalds case IPC_SET: 60331da177e4SLinus Torvalds perms = SHM__SETATTR; 60341da177e4SLinus Torvalds break; 60351da177e4SLinus Torvalds case SHM_LOCK: 60361da177e4SLinus Torvalds case SHM_UNLOCK: 60371da177e4SLinus Torvalds perms = SHM__LOCK; 60381da177e4SLinus Torvalds break; 60391da177e4SLinus Torvalds case IPC_RMID: 60401da177e4SLinus Torvalds perms = SHM__DESTROY; 60411da177e4SLinus Torvalds break; 60421da177e4SLinus Torvalds default: 60431da177e4SLinus Torvalds return 0; 60441da177e4SLinus Torvalds } 60451da177e4SLinus Torvalds 60467191adffSEric W. Biederman err = ipc_has_perm(shp, perms); 60471da177e4SLinus Torvalds return err; 60481da177e4SLinus Torvalds } 60491da177e4SLinus Torvalds 60507191adffSEric W. Biederman static int selinux_shm_shmat(struct kern_ipc_perm *shp, 60511da177e4SLinus Torvalds char __user *shmaddr, int shmflg) 60521da177e4SLinus Torvalds { 60531da177e4SLinus Torvalds u32 perms; 60541da177e4SLinus Torvalds 60551da177e4SLinus Torvalds if (shmflg & SHM_RDONLY) 60561da177e4SLinus Torvalds perms = SHM__READ; 60571da177e4SLinus Torvalds else 60581da177e4SLinus Torvalds perms = SHM__READ | SHM__WRITE; 60591da177e4SLinus Torvalds 60607191adffSEric W. Biederman return ipc_has_perm(shp, perms); 60611da177e4SLinus Torvalds } 60621da177e4SLinus Torvalds 60631da177e4SLinus Torvalds /* Semaphore security operations */ 6064aefad959SEric W. Biederman static int selinux_sem_alloc_security(struct kern_ipc_perm *sma) 60651da177e4SLinus Torvalds { 60661da177e4SLinus Torvalds struct ipc_security_struct *isec; 60672bf49690SThomas Liu struct common_audit_data ad; 6068275bb41eSDavid Howells u32 sid = current_sid(); 60691da177e4SLinus Torvalds int rc; 60701da177e4SLinus Torvalds 6071aefad959SEric W. Biederman rc = ipc_alloc_security(sma, SECCLASS_SEM); 60721da177e4SLinus Torvalds if (rc) 60731da177e4SLinus Torvalds return rc; 60741da177e4SLinus Torvalds 6075aefad959SEric W. Biederman isec = sma->security; 60761da177e4SLinus Torvalds 607750c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 6078aefad959SEric W. Biederman ad.u.ipc_id = sma->key; 60791da177e4SLinus Torvalds 60806b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 60816b6bc620SStephen Smalley sid, isec->sid, SECCLASS_SEM, 60821da177e4SLinus Torvalds SEM__CREATE, &ad); 60831da177e4SLinus Torvalds if (rc) { 6084aefad959SEric W. Biederman ipc_free_security(sma); 60851da177e4SLinus Torvalds return rc; 60861da177e4SLinus Torvalds } 60871da177e4SLinus Torvalds return 0; 60881da177e4SLinus Torvalds } 60891da177e4SLinus Torvalds 6090aefad959SEric W. Biederman static void selinux_sem_free_security(struct kern_ipc_perm *sma) 60911da177e4SLinus Torvalds { 6092aefad959SEric W. Biederman ipc_free_security(sma); 60931da177e4SLinus Torvalds } 60941da177e4SLinus Torvalds 6095aefad959SEric W. Biederman static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg) 60961da177e4SLinus Torvalds { 60971da177e4SLinus Torvalds struct ipc_security_struct *isec; 60982bf49690SThomas Liu struct common_audit_data ad; 6099275bb41eSDavid Howells u32 sid = current_sid(); 61001da177e4SLinus Torvalds 6101aefad959SEric W. Biederman isec = sma->security; 61021da177e4SLinus Torvalds 610350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 6104aefad959SEric W. Biederman ad.u.ipc_id = sma->key; 61051da177e4SLinus Torvalds 61066b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 61076b6bc620SStephen Smalley sid, isec->sid, SECCLASS_SEM, 61081da177e4SLinus Torvalds SEM__ASSOCIATE, &ad); 61091da177e4SLinus Torvalds } 61101da177e4SLinus Torvalds 61111da177e4SLinus Torvalds /* Note, at this point, sma is locked down */ 6112aefad959SEric W. Biederman static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd) 61131da177e4SLinus Torvalds { 61141da177e4SLinus Torvalds int err; 61151da177e4SLinus Torvalds u32 perms; 61161da177e4SLinus Torvalds 61171da177e4SLinus Torvalds switch (cmd) { 61181da177e4SLinus Torvalds case IPC_INFO: 61191da177e4SLinus Torvalds case SEM_INFO: 61201da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 61216b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 61226b6bc620SStephen Smalley current_sid(), SECINITSID_KERNEL, 6123be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); 61241da177e4SLinus Torvalds case GETPID: 61251da177e4SLinus Torvalds case GETNCNT: 61261da177e4SLinus Torvalds case GETZCNT: 61271da177e4SLinus Torvalds perms = SEM__GETATTR; 61281da177e4SLinus Torvalds break; 61291da177e4SLinus Torvalds case GETVAL: 61301da177e4SLinus Torvalds case GETALL: 61311da177e4SLinus Torvalds perms = SEM__READ; 61321da177e4SLinus Torvalds break; 61331da177e4SLinus Torvalds case SETVAL: 61341da177e4SLinus Torvalds case SETALL: 61351da177e4SLinus Torvalds perms = SEM__WRITE; 61361da177e4SLinus Torvalds break; 61371da177e4SLinus Torvalds case IPC_RMID: 61381da177e4SLinus Torvalds perms = SEM__DESTROY; 61391da177e4SLinus Torvalds break; 61401da177e4SLinus Torvalds case IPC_SET: 61411da177e4SLinus Torvalds perms = SEM__SETATTR; 61421da177e4SLinus Torvalds break; 61431da177e4SLinus Torvalds case IPC_STAT: 61441da177e4SLinus Torvalds case SEM_STAT: 6145a280d6dcSDavidlohr Bueso case SEM_STAT_ANY: 61461da177e4SLinus Torvalds perms = SEM__GETATTR | SEM__ASSOCIATE; 61471da177e4SLinus Torvalds break; 61481da177e4SLinus Torvalds default: 61491da177e4SLinus Torvalds return 0; 61501da177e4SLinus Torvalds } 61511da177e4SLinus Torvalds 6152aefad959SEric W. Biederman err = ipc_has_perm(sma, perms); 61531da177e4SLinus Torvalds return err; 61541da177e4SLinus Torvalds } 61551da177e4SLinus Torvalds 6156aefad959SEric W. Biederman static int selinux_sem_semop(struct kern_ipc_perm *sma, 61571da177e4SLinus Torvalds struct sembuf *sops, unsigned nsops, int alter) 61581da177e4SLinus Torvalds { 61591da177e4SLinus Torvalds u32 perms; 61601da177e4SLinus Torvalds 61611da177e4SLinus Torvalds if (alter) 61621da177e4SLinus Torvalds perms = SEM__READ | SEM__WRITE; 61631da177e4SLinus Torvalds else 61641da177e4SLinus Torvalds perms = SEM__READ; 61651da177e4SLinus Torvalds 6166aefad959SEric W. Biederman return ipc_has_perm(sma, perms); 61671da177e4SLinus Torvalds } 61681da177e4SLinus Torvalds 61691da177e4SLinus Torvalds static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag) 61701da177e4SLinus Torvalds { 61711da177e4SLinus Torvalds u32 av = 0; 61721da177e4SLinus Torvalds 61731da177e4SLinus Torvalds av = 0; 61741da177e4SLinus Torvalds if (flag & S_IRUGO) 61751da177e4SLinus Torvalds av |= IPC__UNIX_READ; 61761da177e4SLinus Torvalds if (flag & S_IWUGO) 61771da177e4SLinus Torvalds av |= IPC__UNIX_WRITE; 61781da177e4SLinus Torvalds 61791da177e4SLinus Torvalds if (av == 0) 61801da177e4SLinus Torvalds return 0; 61811da177e4SLinus Torvalds 61826af963f1SStephen Smalley return ipc_has_perm(ipcp, av); 61831da177e4SLinus Torvalds } 61841da177e4SLinus Torvalds 6185713a04aeSAhmed S. Darwish static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid) 6186713a04aeSAhmed S. Darwish { 6187713a04aeSAhmed S. Darwish struct ipc_security_struct *isec = ipcp->security; 6188713a04aeSAhmed S. Darwish *secid = isec->sid; 6189713a04aeSAhmed S. Darwish } 6190713a04aeSAhmed S. Darwish 61911da177e4SLinus Torvalds static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode) 61921da177e4SLinus Torvalds { 61931da177e4SLinus Torvalds if (inode) 61941da177e4SLinus Torvalds inode_doinit_with_dentry(inode, dentry); 61951da177e4SLinus Torvalds } 61961da177e4SLinus Torvalds 61971da177e4SLinus Torvalds static int selinux_getprocattr(struct task_struct *p, 619804ff9708SAl Viro char *name, char **value) 61991da177e4SLinus Torvalds { 6200275bb41eSDavid Howells const struct task_security_struct *__tsec; 62018c8570fbSDustin Kirkland u32 sid; 62021da177e4SLinus Torvalds int error; 620304ff9708SAl Viro unsigned len; 62041da177e4SLinus Torvalds 6205275bb41eSDavid Howells rcu_read_lock(); 6206275bb41eSDavid Howells __tsec = __task_cred(p)->security; 62071da177e4SLinus Torvalds 6208be0554c9SStephen Smalley if (current != p) { 62096b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 62106b6bc620SStephen Smalley current_sid(), __tsec->sid, 6211be0554c9SStephen Smalley SECCLASS_PROCESS, PROCESS__GETATTR, NULL); 6212be0554c9SStephen Smalley if (error) 6213be0554c9SStephen Smalley goto bad; 6214be0554c9SStephen Smalley } 6215be0554c9SStephen Smalley 62161da177e4SLinus Torvalds if (!strcmp(name, "current")) 6217275bb41eSDavid Howells sid = __tsec->sid; 62181da177e4SLinus Torvalds else if (!strcmp(name, "prev")) 6219275bb41eSDavid Howells sid = __tsec->osid; 62201da177e4SLinus Torvalds else if (!strcmp(name, "exec")) 6221275bb41eSDavid Howells sid = __tsec->exec_sid; 62221da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 6223275bb41eSDavid Howells sid = __tsec->create_sid; 62244eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) 6225275bb41eSDavid Howells sid = __tsec->keycreate_sid; 622642c3e03eSEric Paris else if (!strcmp(name, "sockcreate")) 6227275bb41eSDavid Howells sid = __tsec->sockcreate_sid; 6228be0554c9SStephen Smalley else { 6229be0554c9SStephen Smalley error = -EINVAL; 6230be0554c9SStephen Smalley goto bad; 6231be0554c9SStephen Smalley } 6232275bb41eSDavid Howells rcu_read_unlock(); 62331da177e4SLinus Torvalds 62341da177e4SLinus Torvalds if (!sid) 62351da177e4SLinus Torvalds return 0; 62361da177e4SLinus Torvalds 6237aa8e712cSStephen Smalley error = security_sid_to_context(&selinux_state, sid, value, &len); 623804ff9708SAl Viro if (error) 623904ff9708SAl Viro return error; 624004ff9708SAl Viro return len; 6241275bb41eSDavid Howells 6242be0554c9SStephen Smalley bad: 6243275bb41eSDavid Howells rcu_read_unlock(); 6244be0554c9SStephen Smalley return error; 62451da177e4SLinus Torvalds } 62461da177e4SLinus Torvalds 6247b21507e2SStephen Smalley static int selinux_setprocattr(const char *name, void *value, size_t size) 62481da177e4SLinus Torvalds { 62491da177e4SLinus Torvalds struct task_security_struct *tsec; 6250d84f4f99SDavid Howells struct cred *new; 6251be0554c9SStephen Smalley u32 mysid = current_sid(), sid = 0, ptsid; 62521da177e4SLinus Torvalds int error; 62531da177e4SLinus Torvalds char *str = value; 62541da177e4SLinus Torvalds 62551da177e4SLinus Torvalds /* 62561da177e4SLinus Torvalds * Basic control over ability to set these attributes at all. 62571da177e4SLinus Torvalds */ 62581da177e4SLinus Torvalds if (!strcmp(name, "exec")) 62596b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 62606b6bc620SStephen Smalley mysid, mysid, SECCLASS_PROCESS, 6261be0554c9SStephen Smalley PROCESS__SETEXEC, NULL); 62621da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 62636b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 62646b6bc620SStephen Smalley mysid, mysid, SECCLASS_PROCESS, 6265be0554c9SStephen Smalley PROCESS__SETFSCREATE, NULL); 62664eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) 62676b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 62686b6bc620SStephen Smalley mysid, mysid, SECCLASS_PROCESS, 6269be0554c9SStephen Smalley PROCESS__SETKEYCREATE, NULL); 627042c3e03eSEric Paris else if (!strcmp(name, "sockcreate")) 62716b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 62726b6bc620SStephen Smalley mysid, mysid, SECCLASS_PROCESS, 6273be0554c9SStephen Smalley PROCESS__SETSOCKCREATE, NULL); 62741da177e4SLinus Torvalds else if (!strcmp(name, "current")) 62756b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 62766b6bc620SStephen Smalley mysid, mysid, SECCLASS_PROCESS, 6277be0554c9SStephen Smalley PROCESS__SETCURRENT, NULL); 62781da177e4SLinus Torvalds else 62791da177e4SLinus Torvalds error = -EINVAL; 62801da177e4SLinus Torvalds if (error) 62811da177e4SLinus Torvalds return error; 62821da177e4SLinus Torvalds 62831da177e4SLinus Torvalds /* Obtain a SID for the context, if one was specified. */ 6284a050a570SStephen Smalley if (size && str[0] && str[0] != '\n') { 62851da177e4SLinus Torvalds if (str[size-1] == '\n') { 62861da177e4SLinus Torvalds str[size-1] = 0; 62871da177e4SLinus Torvalds size--; 62881da177e4SLinus Torvalds } 6289aa8e712cSStephen Smalley error = security_context_to_sid(&selinux_state, value, size, 6290aa8e712cSStephen Smalley &sid, GFP_KERNEL); 629112b29f34SStephen Smalley if (error == -EINVAL && !strcmp(name, "fscreate")) { 6292db59000aSStephen Smalley if (!has_cap_mac_admin(true)) { 6293d6ea83ecSEric Paris struct audit_buffer *ab; 6294d6ea83ecSEric Paris size_t audit_size; 6295d6ea83ecSEric Paris 6296d6ea83ecSEric Paris /* We strip a nul only if it is at the end, otherwise the 6297d6ea83ecSEric Paris * context contains a nul and we should audit that */ 6298d6ea83ecSEric Paris if (str[size - 1] == '\0') 6299d6ea83ecSEric Paris audit_size = size - 1; 6300d6ea83ecSEric Paris else 6301d6ea83ecSEric Paris audit_size = size; 6302cdfb6b34SRichard Guy Briggs ab = audit_log_start(audit_context(), 6303cdfb6b34SRichard Guy Briggs GFP_ATOMIC, 6304cdfb6b34SRichard Guy Briggs AUDIT_SELINUX_ERR); 6305d6ea83ecSEric Paris audit_log_format(ab, "op=fscreate invalid_context="); 6306d6ea83ecSEric Paris audit_log_n_untrustedstring(ab, value, audit_size); 6307d6ea83ecSEric Paris audit_log_end(ab); 6308d6ea83ecSEric Paris 630912b29f34SStephen Smalley return error; 6310d6ea83ecSEric Paris } 6311aa8e712cSStephen Smalley error = security_context_to_sid_force( 6312aa8e712cSStephen Smalley &selinux_state, 6313aa8e712cSStephen Smalley value, size, &sid); 631412b29f34SStephen Smalley } 63151da177e4SLinus Torvalds if (error) 63161da177e4SLinus Torvalds return error; 63171da177e4SLinus Torvalds } 63181da177e4SLinus Torvalds 6319d84f4f99SDavid Howells new = prepare_creds(); 6320d84f4f99SDavid Howells if (!new) 6321d84f4f99SDavid Howells return -ENOMEM; 6322d84f4f99SDavid Howells 63231da177e4SLinus Torvalds /* Permission checking based on the specified context is 63241da177e4SLinus Torvalds performed during the actual operation (execve, 63251da177e4SLinus Torvalds open/mkdir/...), when we know the full context of the 6326d84f4f99SDavid Howells operation. See selinux_bprm_set_creds for the execve 63271da177e4SLinus Torvalds checks and may_create for the file creation checks. The 63281da177e4SLinus Torvalds operation will then fail if the context is not permitted. */ 6329d84f4f99SDavid Howells tsec = new->security; 6330d84f4f99SDavid Howells if (!strcmp(name, "exec")) { 63311da177e4SLinus Torvalds tsec->exec_sid = sid; 6332d84f4f99SDavid Howells } else if (!strcmp(name, "fscreate")) { 63331da177e4SLinus Torvalds tsec->create_sid = sid; 6334d84f4f99SDavid Howells } else if (!strcmp(name, "keycreate")) { 63356b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 63366b6bc620SStephen Smalley mysid, sid, SECCLASS_KEY, KEY__CREATE, 6337be0554c9SStephen Smalley NULL); 63384eb582cfSMichael LeMay if (error) 6339d84f4f99SDavid Howells goto abort_change; 63404eb582cfSMichael LeMay tsec->keycreate_sid = sid; 6341d84f4f99SDavid Howells } else if (!strcmp(name, "sockcreate")) { 634242c3e03eSEric Paris tsec->sockcreate_sid = sid; 6343d84f4f99SDavid Howells } else if (!strcmp(name, "current")) { 6344d84f4f99SDavid Howells error = -EINVAL; 63451da177e4SLinus Torvalds if (sid == 0) 6346d84f4f99SDavid Howells goto abort_change; 6347d9250deaSKaiGai Kohei 6348d84f4f99SDavid Howells /* Only allow single threaded processes to change context */ 6349d84f4f99SDavid Howells error = -EPERM; 63505bb459bbSOleg Nesterov if (!current_is_single_threaded()) { 6351aa8e712cSStephen Smalley error = security_bounded_transition(&selinux_state, 6352aa8e712cSStephen Smalley tsec->sid, sid); 6353d84f4f99SDavid Howells if (error) 6354d84f4f99SDavid Howells goto abort_change; 63551da177e4SLinus Torvalds } 63561da177e4SLinus Torvalds 63571da177e4SLinus Torvalds /* Check permissions for the transition. */ 63586b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 63596b6bc620SStephen Smalley tsec->sid, sid, SECCLASS_PROCESS, 63601da177e4SLinus Torvalds PROCESS__DYNTRANSITION, NULL); 63611da177e4SLinus Torvalds if (error) 6362d84f4f99SDavid Howells goto abort_change; 63631da177e4SLinus Torvalds 63641da177e4SLinus Torvalds /* Check for ptracing, and update the task SID if ok. 63651da177e4SLinus Torvalds Otherwise, leave SID unchanged and fail. */ 6366be0554c9SStephen Smalley ptsid = ptrace_parent_sid(); 63670c6181cbSPaul Moore if (ptsid != 0) { 63686b6bc620SStephen Smalley error = avc_has_perm(&selinux_state, 63696b6bc620SStephen Smalley ptsid, sid, SECCLASS_PROCESS, 6370d84f4f99SDavid Howells PROCESS__PTRACE, NULL); 6371d84f4f99SDavid Howells if (error) 6372d84f4f99SDavid Howells goto abort_change; 6373d84f4f99SDavid Howells } 6374d84f4f99SDavid Howells 6375d84f4f99SDavid Howells tsec->sid = sid; 6376d84f4f99SDavid Howells } else { 6377d84f4f99SDavid Howells error = -EINVAL; 6378d84f4f99SDavid Howells goto abort_change; 6379d84f4f99SDavid Howells } 6380d84f4f99SDavid Howells 6381d84f4f99SDavid Howells commit_creds(new); 63821da177e4SLinus Torvalds return size; 6383d84f4f99SDavid Howells 6384d84f4f99SDavid Howells abort_change: 6385d84f4f99SDavid Howells abort_creds(new); 6386d84f4f99SDavid Howells return error; 63871da177e4SLinus Torvalds } 63881da177e4SLinus Torvalds 6389746df9b5SDavid Quigley static int selinux_ismaclabel(const char *name) 6390746df9b5SDavid Quigley { 6391746df9b5SDavid Quigley return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0); 6392746df9b5SDavid Quigley } 6393746df9b5SDavid Quigley 6394dc49c1f9SCatherine Zhang static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) 6395dc49c1f9SCatherine Zhang { 6396aa8e712cSStephen Smalley return security_sid_to_context(&selinux_state, secid, 6397aa8e712cSStephen Smalley secdata, seclen); 6398dc49c1f9SCatherine Zhang } 6399dc49c1f9SCatherine Zhang 64007bf570dcSDavid Howells static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid) 640163cb3449SDavid Howells { 6402aa8e712cSStephen Smalley return security_context_to_sid(&selinux_state, secdata, seclen, 6403aa8e712cSStephen Smalley secid, GFP_KERNEL); 640463cb3449SDavid Howells } 640563cb3449SDavid Howells 6406dc49c1f9SCatherine Zhang static void selinux_release_secctx(char *secdata, u32 seclen) 6407dc49c1f9SCatherine Zhang { 6408dc49c1f9SCatherine Zhang kfree(secdata); 6409dc49c1f9SCatherine Zhang } 6410dc49c1f9SCatherine Zhang 64116f3be9f5SAndreas Gruenbacher static void selinux_inode_invalidate_secctx(struct inode *inode) 64126f3be9f5SAndreas Gruenbacher { 64136f3be9f5SAndreas Gruenbacher struct inode_security_struct *isec = inode->i_security; 64146f3be9f5SAndreas Gruenbacher 64159287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 64166f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INVALID; 64179287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 64186f3be9f5SAndreas Gruenbacher } 64196f3be9f5SAndreas Gruenbacher 64201ee65e37SDavid P. Quigley /* 64211ee65e37SDavid P. Quigley * called with inode->i_mutex locked 64221ee65e37SDavid P. Quigley */ 64231ee65e37SDavid P. Quigley static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen) 64241ee65e37SDavid P. Quigley { 642553e0c2aaSOndrej Mosnacek int rc = selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, 642653e0c2aaSOndrej Mosnacek ctx, ctxlen, 0); 642753e0c2aaSOndrej Mosnacek /* Do not return error when suppressing label (SBLABEL_MNT not set). */ 642853e0c2aaSOndrej Mosnacek return rc == -EOPNOTSUPP ? 0 : rc; 64291ee65e37SDavid P. Quigley } 64301ee65e37SDavid P. Quigley 64311ee65e37SDavid P. Quigley /* 64321ee65e37SDavid P. Quigley * called with inode->i_mutex locked 64331ee65e37SDavid P. Quigley */ 64341ee65e37SDavid P. Quigley static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen) 64351ee65e37SDavid P. Quigley { 64361ee65e37SDavid P. Quigley return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0); 64371ee65e37SDavid P. Quigley } 64381ee65e37SDavid P. Quigley 64391ee65e37SDavid P. Quigley static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen) 64401ee65e37SDavid P. Quigley { 64411ee65e37SDavid P. Quigley int len = 0; 64421ee65e37SDavid P. Quigley len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX, 64431ee65e37SDavid P. Quigley ctx, true); 64441ee65e37SDavid P. Quigley if (len < 0) 64451ee65e37SDavid P. Quigley return len; 64461ee65e37SDavid P. Quigley *ctxlen = len; 64471ee65e37SDavid P. Quigley return 0; 64481ee65e37SDavid P. Quigley } 6449d720024eSMichael LeMay #ifdef CONFIG_KEYS 6450d720024eSMichael LeMay 6451d84f4f99SDavid Howells static int selinux_key_alloc(struct key *k, const struct cred *cred, 64527e047ef5SDavid Howells unsigned long flags) 6453d720024eSMichael LeMay { 6454d84f4f99SDavid Howells const struct task_security_struct *tsec; 6455d720024eSMichael LeMay struct key_security_struct *ksec; 6456d720024eSMichael LeMay 6457d720024eSMichael LeMay ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL); 6458d720024eSMichael LeMay if (!ksec) 6459d720024eSMichael LeMay return -ENOMEM; 6460d720024eSMichael LeMay 6461d84f4f99SDavid Howells tsec = cred->security; 6462d84f4f99SDavid Howells if (tsec->keycreate_sid) 6463d84f4f99SDavid Howells ksec->sid = tsec->keycreate_sid; 64644eb582cfSMichael LeMay else 6465d84f4f99SDavid Howells ksec->sid = tsec->sid; 6466d720024eSMichael LeMay 6467275bb41eSDavid Howells k->security = ksec; 6468d720024eSMichael LeMay return 0; 6469d720024eSMichael LeMay } 6470d720024eSMichael LeMay 6471d720024eSMichael LeMay static void selinux_key_free(struct key *k) 6472d720024eSMichael LeMay { 6473d720024eSMichael LeMay struct key_security_struct *ksec = k->security; 6474d720024eSMichael LeMay 6475d720024eSMichael LeMay k->security = NULL; 6476d720024eSMichael LeMay kfree(ksec); 6477d720024eSMichael LeMay } 6478d720024eSMichael LeMay 6479d720024eSMichael LeMay static int selinux_key_permission(key_ref_t key_ref, 6480d84f4f99SDavid Howells const struct cred *cred, 6481f5895943SDavid Howells unsigned perm) 6482d720024eSMichael LeMay { 6483d720024eSMichael LeMay struct key *key; 6484d720024eSMichael LeMay struct key_security_struct *ksec; 6485275bb41eSDavid Howells u32 sid; 6486d720024eSMichael LeMay 6487d720024eSMichael LeMay /* if no specific permissions are requested, we skip the 6488d720024eSMichael LeMay permission check. No serious, additional covert channels 6489d720024eSMichael LeMay appear to be created. */ 6490d720024eSMichael LeMay if (perm == 0) 6491d720024eSMichael LeMay return 0; 6492d720024eSMichael LeMay 6493d84f4f99SDavid Howells sid = cred_sid(cred); 6494275bb41eSDavid Howells 6495275bb41eSDavid Howells key = key_ref_to_ptr(key_ref); 6496275bb41eSDavid Howells ksec = key->security; 6497275bb41eSDavid Howells 64986b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 64996b6bc620SStephen Smalley sid, ksec->sid, SECCLASS_KEY, perm, NULL); 6500d720024eSMichael LeMay } 6501d720024eSMichael LeMay 650270a5bb72SDavid Howells static int selinux_key_getsecurity(struct key *key, char **_buffer) 650370a5bb72SDavid Howells { 650470a5bb72SDavid Howells struct key_security_struct *ksec = key->security; 650570a5bb72SDavid Howells char *context = NULL; 650670a5bb72SDavid Howells unsigned len; 650770a5bb72SDavid Howells int rc; 650870a5bb72SDavid Howells 6509aa8e712cSStephen Smalley rc = security_sid_to_context(&selinux_state, ksec->sid, 6510aa8e712cSStephen Smalley &context, &len); 651170a5bb72SDavid Howells if (!rc) 651270a5bb72SDavid Howells rc = len; 651370a5bb72SDavid Howells *_buffer = context; 651470a5bb72SDavid Howells return rc; 651570a5bb72SDavid Howells } 65163a976fa6SDaniel Jurgens #endif 651770a5bb72SDavid Howells 65183a976fa6SDaniel Jurgens #ifdef CONFIG_SECURITY_INFINIBAND 6519cfc4d882SDaniel Jurgens static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val) 6520cfc4d882SDaniel Jurgens { 6521cfc4d882SDaniel Jurgens struct common_audit_data ad; 6522cfc4d882SDaniel Jurgens int err; 6523cfc4d882SDaniel Jurgens u32 sid = 0; 6524cfc4d882SDaniel Jurgens struct ib_security_struct *sec = ib_sec; 6525cfc4d882SDaniel Jurgens struct lsm_ibpkey_audit ibpkey; 6526cfc4d882SDaniel Jurgens 6527409dcf31SDaniel Jurgens err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid); 6528cfc4d882SDaniel Jurgens if (err) 6529cfc4d882SDaniel Jurgens return err; 6530cfc4d882SDaniel Jurgens 6531cfc4d882SDaniel Jurgens ad.type = LSM_AUDIT_DATA_IBPKEY; 6532cfc4d882SDaniel Jurgens ibpkey.subnet_prefix = subnet_prefix; 6533cfc4d882SDaniel Jurgens ibpkey.pkey = pkey_val; 6534cfc4d882SDaniel Jurgens ad.u.ibpkey = &ibpkey; 65356b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 65366b6bc620SStephen Smalley sec->sid, sid, 6537cfc4d882SDaniel Jurgens SECCLASS_INFINIBAND_PKEY, 6538cfc4d882SDaniel Jurgens INFINIBAND_PKEY__ACCESS, &ad); 6539cfc4d882SDaniel Jurgens } 6540cfc4d882SDaniel Jurgens 6541ab861dfcSDaniel Jurgens static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name, 6542ab861dfcSDaniel Jurgens u8 port_num) 6543ab861dfcSDaniel Jurgens { 6544ab861dfcSDaniel Jurgens struct common_audit_data ad; 6545ab861dfcSDaniel Jurgens int err; 6546ab861dfcSDaniel Jurgens u32 sid = 0; 6547ab861dfcSDaniel Jurgens struct ib_security_struct *sec = ib_sec; 6548ab861dfcSDaniel Jurgens struct lsm_ibendport_audit ibendport; 6549ab861dfcSDaniel Jurgens 6550aa8e712cSStephen Smalley err = security_ib_endport_sid(&selinux_state, dev_name, port_num, 6551aa8e712cSStephen Smalley &sid); 6552ab861dfcSDaniel Jurgens 6553ab861dfcSDaniel Jurgens if (err) 6554ab861dfcSDaniel Jurgens return err; 6555ab861dfcSDaniel Jurgens 6556ab861dfcSDaniel Jurgens ad.type = LSM_AUDIT_DATA_IBENDPORT; 6557ab861dfcSDaniel Jurgens strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name)); 6558ab861dfcSDaniel Jurgens ibendport.port = port_num; 6559ab861dfcSDaniel Jurgens ad.u.ibendport = &ibendport; 65606b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 65616b6bc620SStephen Smalley sec->sid, sid, 6562ab861dfcSDaniel Jurgens SECCLASS_INFINIBAND_ENDPORT, 6563ab861dfcSDaniel Jurgens INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad); 6564ab861dfcSDaniel Jurgens } 6565ab861dfcSDaniel Jurgens 65663a976fa6SDaniel Jurgens static int selinux_ib_alloc_security(void **ib_sec) 65673a976fa6SDaniel Jurgens { 65683a976fa6SDaniel Jurgens struct ib_security_struct *sec; 65693a976fa6SDaniel Jurgens 65703a976fa6SDaniel Jurgens sec = kzalloc(sizeof(*sec), GFP_KERNEL); 65713a976fa6SDaniel Jurgens if (!sec) 65723a976fa6SDaniel Jurgens return -ENOMEM; 65733a976fa6SDaniel Jurgens sec->sid = current_sid(); 65743a976fa6SDaniel Jurgens 65753a976fa6SDaniel Jurgens *ib_sec = sec; 65763a976fa6SDaniel Jurgens return 0; 65773a976fa6SDaniel Jurgens } 65783a976fa6SDaniel Jurgens 65793a976fa6SDaniel Jurgens static void selinux_ib_free_security(void *ib_sec) 65803a976fa6SDaniel Jurgens { 65813a976fa6SDaniel Jurgens kfree(ib_sec); 65823a976fa6SDaniel Jurgens } 6583d720024eSMichael LeMay #endif 6584d720024eSMichael LeMay 6585ec27c356SChenbo Feng #ifdef CONFIG_BPF_SYSCALL 6586ec27c356SChenbo Feng static int selinux_bpf(int cmd, union bpf_attr *attr, 6587ec27c356SChenbo Feng unsigned int size) 6588ec27c356SChenbo Feng { 6589ec27c356SChenbo Feng u32 sid = current_sid(); 6590ec27c356SChenbo Feng int ret; 6591ec27c356SChenbo Feng 6592ec27c356SChenbo Feng switch (cmd) { 6593ec27c356SChenbo Feng case BPF_MAP_CREATE: 65946b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 65956b6bc620SStephen Smalley sid, sid, SECCLASS_BPF, BPF__MAP_CREATE, 6596ec27c356SChenbo Feng NULL); 6597ec27c356SChenbo Feng break; 6598ec27c356SChenbo Feng case BPF_PROG_LOAD: 65996b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 66006b6bc620SStephen Smalley sid, sid, SECCLASS_BPF, BPF__PROG_LOAD, 6601ec27c356SChenbo Feng NULL); 6602ec27c356SChenbo Feng break; 6603ec27c356SChenbo Feng default: 6604ec27c356SChenbo Feng ret = 0; 6605ec27c356SChenbo Feng break; 6606ec27c356SChenbo Feng } 6607ec27c356SChenbo Feng 6608ec27c356SChenbo Feng return ret; 6609ec27c356SChenbo Feng } 6610ec27c356SChenbo Feng 6611ec27c356SChenbo Feng static u32 bpf_map_fmode_to_av(fmode_t fmode) 6612ec27c356SChenbo Feng { 6613ec27c356SChenbo Feng u32 av = 0; 6614ec27c356SChenbo Feng 6615ec27c356SChenbo Feng if (fmode & FMODE_READ) 6616ec27c356SChenbo Feng av |= BPF__MAP_READ; 6617ec27c356SChenbo Feng if (fmode & FMODE_WRITE) 6618ec27c356SChenbo Feng av |= BPF__MAP_WRITE; 6619ec27c356SChenbo Feng return av; 6620ec27c356SChenbo Feng } 6621ec27c356SChenbo Feng 6622f66e448cSChenbo Feng /* This function will check the file pass through unix socket or binder to see 6623f66e448cSChenbo Feng * if it is a bpf related object. And apply correspinding checks on the bpf 6624f66e448cSChenbo Feng * object based on the type. The bpf maps and programs, not like other files and 6625f66e448cSChenbo Feng * socket, are using a shared anonymous inode inside the kernel as their inode. 6626f66e448cSChenbo Feng * So checking that inode cannot identify if the process have privilege to 6627f66e448cSChenbo Feng * access the bpf object and that's why we have to add this additional check in 6628f66e448cSChenbo Feng * selinux_file_receive and selinux_binder_transfer_files. 6629f66e448cSChenbo Feng */ 6630f66e448cSChenbo Feng static int bpf_fd_pass(struct file *file, u32 sid) 6631f66e448cSChenbo Feng { 6632f66e448cSChenbo Feng struct bpf_security_struct *bpfsec; 6633f66e448cSChenbo Feng struct bpf_prog *prog; 6634f66e448cSChenbo Feng struct bpf_map *map; 6635f66e448cSChenbo Feng int ret; 6636f66e448cSChenbo Feng 6637f66e448cSChenbo Feng if (file->f_op == &bpf_map_fops) { 6638f66e448cSChenbo Feng map = file->private_data; 6639f66e448cSChenbo Feng bpfsec = map->security; 66406b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 66416b6bc620SStephen Smalley sid, bpfsec->sid, SECCLASS_BPF, 6642f66e448cSChenbo Feng bpf_map_fmode_to_av(file->f_mode), NULL); 6643f66e448cSChenbo Feng if (ret) 6644f66e448cSChenbo Feng return ret; 6645f66e448cSChenbo Feng } else if (file->f_op == &bpf_prog_fops) { 6646f66e448cSChenbo Feng prog = file->private_data; 6647f66e448cSChenbo Feng bpfsec = prog->aux->security; 66486b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 66496b6bc620SStephen Smalley sid, bpfsec->sid, SECCLASS_BPF, 6650f66e448cSChenbo Feng BPF__PROG_RUN, NULL); 6651f66e448cSChenbo Feng if (ret) 6652f66e448cSChenbo Feng return ret; 6653f66e448cSChenbo Feng } 6654f66e448cSChenbo Feng return 0; 6655f66e448cSChenbo Feng } 6656f66e448cSChenbo Feng 6657ec27c356SChenbo Feng static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode) 6658ec27c356SChenbo Feng { 6659ec27c356SChenbo Feng u32 sid = current_sid(); 6660ec27c356SChenbo Feng struct bpf_security_struct *bpfsec; 6661ec27c356SChenbo Feng 6662ec27c356SChenbo Feng bpfsec = map->security; 66636b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 66646b6bc620SStephen Smalley sid, bpfsec->sid, SECCLASS_BPF, 6665ec27c356SChenbo Feng bpf_map_fmode_to_av(fmode), NULL); 6666ec27c356SChenbo Feng } 6667ec27c356SChenbo Feng 6668ec27c356SChenbo Feng static int selinux_bpf_prog(struct bpf_prog *prog) 6669ec27c356SChenbo Feng { 6670ec27c356SChenbo Feng u32 sid = current_sid(); 6671ec27c356SChenbo Feng struct bpf_security_struct *bpfsec; 6672ec27c356SChenbo Feng 6673ec27c356SChenbo Feng bpfsec = prog->aux->security; 66746b6bc620SStephen Smalley return avc_has_perm(&selinux_state, 66756b6bc620SStephen Smalley sid, bpfsec->sid, SECCLASS_BPF, 6676ec27c356SChenbo Feng BPF__PROG_RUN, NULL); 6677ec27c356SChenbo Feng } 6678ec27c356SChenbo Feng 6679ec27c356SChenbo Feng static int selinux_bpf_map_alloc(struct bpf_map *map) 6680ec27c356SChenbo Feng { 6681ec27c356SChenbo Feng struct bpf_security_struct *bpfsec; 6682ec27c356SChenbo Feng 6683ec27c356SChenbo Feng bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL); 6684ec27c356SChenbo Feng if (!bpfsec) 6685ec27c356SChenbo Feng return -ENOMEM; 6686ec27c356SChenbo Feng 6687ec27c356SChenbo Feng bpfsec->sid = current_sid(); 6688ec27c356SChenbo Feng map->security = bpfsec; 6689ec27c356SChenbo Feng 6690ec27c356SChenbo Feng return 0; 6691ec27c356SChenbo Feng } 6692ec27c356SChenbo Feng 6693ec27c356SChenbo Feng static void selinux_bpf_map_free(struct bpf_map *map) 6694ec27c356SChenbo Feng { 6695ec27c356SChenbo Feng struct bpf_security_struct *bpfsec = map->security; 6696ec27c356SChenbo Feng 6697ec27c356SChenbo Feng map->security = NULL; 6698ec27c356SChenbo Feng kfree(bpfsec); 6699ec27c356SChenbo Feng } 6700ec27c356SChenbo Feng 6701ec27c356SChenbo Feng static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux) 6702ec27c356SChenbo Feng { 6703ec27c356SChenbo Feng struct bpf_security_struct *bpfsec; 6704ec27c356SChenbo Feng 6705ec27c356SChenbo Feng bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL); 6706ec27c356SChenbo Feng if (!bpfsec) 6707ec27c356SChenbo Feng return -ENOMEM; 6708ec27c356SChenbo Feng 6709ec27c356SChenbo Feng bpfsec->sid = current_sid(); 6710ec27c356SChenbo Feng aux->security = bpfsec; 6711ec27c356SChenbo Feng 6712ec27c356SChenbo Feng return 0; 6713ec27c356SChenbo Feng } 6714ec27c356SChenbo Feng 6715ec27c356SChenbo Feng static void selinux_bpf_prog_free(struct bpf_prog_aux *aux) 6716ec27c356SChenbo Feng { 6717ec27c356SChenbo Feng struct bpf_security_struct *bpfsec = aux->security; 6718ec27c356SChenbo Feng 6719ec27c356SChenbo Feng aux->security = NULL; 6720ec27c356SChenbo Feng kfree(bpfsec); 6721ec27c356SChenbo Feng } 6722ec27c356SChenbo Feng #endif 6723ec27c356SChenbo Feng 6724ca97d939SJames Morris static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = { 6725e20b043aSCasey Schaufler LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr), 6726e20b043aSCasey Schaufler LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction), 6727e20b043aSCasey Schaufler LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder), 6728e20b043aSCasey Schaufler LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file), 6729076c54c5SAhmed S. Darwish 6730e20b043aSCasey Schaufler LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check), 6731e20b043aSCasey Schaufler LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme), 6732e20b043aSCasey Schaufler LSM_HOOK_INIT(capget, selinux_capget), 6733e20b043aSCasey Schaufler LSM_HOOK_INIT(capset, selinux_capset), 6734e20b043aSCasey Schaufler LSM_HOOK_INIT(capable, selinux_capable), 6735e20b043aSCasey Schaufler LSM_HOOK_INIT(quotactl, selinux_quotactl), 6736e20b043aSCasey Schaufler LSM_HOOK_INIT(quota_on, selinux_quota_on), 6737e20b043aSCasey Schaufler LSM_HOOK_INIT(syslog, selinux_syslog), 6738e20b043aSCasey Schaufler LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory), 673979af7307SStephen Smalley 6740e20b043aSCasey Schaufler LSM_HOOK_INIT(netlink_send, selinux_netlink_send), 67411da177e4SLinus Torvalds 6742e20b043aSCasey Schaufler LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds), 6743e20b043aSCasey Schaufler LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds), 6744e20b043aSCasey Schaufler LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds), 67451da177e4SLinus Torvalds 6746e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security), 6747e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security), 67485b400239SAl Viro LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts), 6749204cc0ccSAl Viro LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts), 6750e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_remount, selinux_sb_remount), 6751e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount), 6752e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options), 6753e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs), 6754e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_mount, selinux_mount), 6755e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_umount, selinux_umount), 6756e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts), 6757e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts), 6758757cbe59SAl Viro LSM_HOOK_INIT(sb_add_mnt_opt, selinux_add_mnt_opt), 67591da177e4SLinus Torvalds 6760e20b043aSCasey Schaufler LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security), 6761a518b0a5SVivek Goyal LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as), 6762e0007529SEric Paris 6763e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security), 6764e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security), 6765e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security), 6766e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_create, selinux_inode_create), 6767e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_link, selinux_inode_link), 6768e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink), 6769e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink), 6770e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir), 6771e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir), 6772e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod), 6773e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_rename, selinux_inode_rename), 6774e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink), 6775e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link), 6776e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_permission, selinux_inode_permission), 6777e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr), 6778e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr), 6779e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr), 6780e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr), 6781e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr), 6782e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr), 6783e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr), 6784e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity), 6785e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity), 6786e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity), 6787e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid), 678856909eb3SVivek Goyal LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up), 678919472b69SVivek Goyal LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr), 67901da177e4SLinus Torvalds 6791e20b043aSCasey Schaufler LSM_HOOK_INIT(file_permission, selinux_file_permission), 6792e20b043aSCasey Schaufler LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security), 6793e20b043aSCasey Schaufler LSM_HOOK_INIT(file_free_security, selinux_file_free_security), 6794e20b043aSCasey Schaufler LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl), 6795e20b043aSCasey Schaufler LSM_HOOK_INIT(mmap_file, selinux_mmap_file), 6796e20b043aSCasey Schaufler LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr), 6797e20b043aSCasey Schaufler LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect), 6798e20b043aSCasey Schaufler LSM_HOOK_INIT(file_lock, selinux_file_lock), 6799e20b043aSCasey Schaufler LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl), 6800e20b043aSCasey Schaufler LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner), 6801e20b043aSCasey Schaufler LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask), 6802e20b043aSCasey Schaufler LSM_HOOK_INIT(file_receive, selinux_file_receive), 68031da177e4SLinus Torvalds 6804e20b043aSCasey Schaufler LSM_HOOK_INIT(file_open, selinux_file_open), 68051da177e4SLinus Torvalds 6806a79be238STetsuo Handa LSM_HOOK_INIT(task_alloc, selinux_task_alloc), 6807e20b043aSCasey Schaufler LSM_HOOK_INIT(cred_alloc_blank, selinux_cred_alloc_blank), 6808e20b043aSCasey Schaufler LSM_HOOK_INIT(cred_free, selinux_cred_free), 6809e20b043aSCasey Schaufler LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare), 6810e20b043aSCasey Schaufler LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer), 68113ec30113SMatthew Garrett LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid), 6812e20b043aSCasey Schaufler LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as), 6813e20b043aSCasey Schaufler LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as), 6814e20b043aSCasey Schaufler LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request), 6815c77b8cdfSMimi Zohar LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data), 681661d612eaSJeff Vander Stoep LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file), 6817e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid), 6818e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid), 6819e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getsid, selinux_task_getsid), 6820e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid), 6821e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setnice, selinux_task_setnice), 6822e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio), 6823e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio), 6824791ec491SStephen Smalley LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit), 6825e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit), 6826e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler), 6827e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler), 6828e20b043aSCasey Schaufler LSM_HOOK_INIT(task_movememory, selinux_task_movememory), 6829e20b043aSCasey Schaufler LSM_HOOK_INIT(task_kill, selinux_task_kill), 6830e20b043aSCasey Schaufler LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode), 6831788e7dd4SYuichi Nakamura 6832e20b043aSCasey Schaufler LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission), 6833e20b043aSCasey Schaufler LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid), 68341da177e4SLinus Torvalds 6835e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security), 6836e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_msg_free_security, selinux_msg_msg_free_security), 68371da177e4SLinus Torvalds 6838e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_alloc_security, 6839e20b043aSCasey Schaufler selinux_msg_queue_alloc_security), 6840e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_free_security, selinux_msg_queue_free_security), 6841e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate), 6842e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl), 6843e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd), 6844e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv), 68451da177e4SLinus Torvalds 6846e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security), 6847e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_free_security, selinux_shm_free_security), 6848e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_associate, selinux_shm_associate), 6849e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl), 6850e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat), 68511da177e4SLinus Torvalds 6852e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security), 6853e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_free_security, selinux_sem_free_security), 6854e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_associate, selinux_sem_associate), 6855e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl), 6856e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_semop, selinux_sem_semop), 68571da177e4SLinus Torvalds 6858e20b043aSCasey Schaufler LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate), 68591da177e4SLinus Torvalds 6860e20b043aSCasey Schaufler LSM_HOOK_INIT(getprocattr, selinux_getprocattr), 6861e20b043aSCasey Schaufler LSM_HOOK_INIT(setprocattr, selinux_setprocattr), 68621da177e4SLinus Torvalds 6863e20b043aSCasey Schaufler LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel), 6864e20b043aSCasey Schaufler LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx), 6865e20b043aSCasey Schaufler LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid), 6866e20b043aSCasey Schaufler LSM_HOOK_INIT(release_secctx, selinux_release_secctx), 68676f3be9f5SAndreas Gruenbacher LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx), 6868e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx), 6869e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx), 6870e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx), 68711da177e4SLinus Torvalds 6872e20b043aSCasey Schaufler LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect), 6873e20b043aSCasey Schaufler LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send), 6874dc49c1f9SCatherine Zhang 6875e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_create, selinux_socket_create), 6876e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create), 68770b811db2SDavid Herrmann LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair), 6878e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_bind, selinux_socket_bind), 6879e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_connect, selinux_socket_connect), 6880e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_listen, selinux_socket_listen), 6881e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_accept, selinux_socket_accept), 6882e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg), 6883e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg), 6884e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname), 6885e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername), 6886e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt), 6887e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt), 6888e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown), 6889e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb), 6890e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getpeersec_stream, 6891e20b043aSCasey Schaufler selinux_socket_getpeersec_stream), 6892e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram), 6893e20b043aSCasey Schaufler LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security), 6894e20b043aSCasey Schaufler LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security), 6895e20b043aSCasey Schaufler LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security), 6896e20b043aSCasey Schaufler LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid), 6897e20b043aSCasey Schaufler LSM_HOOK_INIT(sock_graft, selinux_sock_graft), 6898d452930fSRichard Haines LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request), 6899d452930fSRichard Haines LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone), 6900d452930fSRichard Haines LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect), 6901e20b043aSCasey Schaufler LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request), 6902e20b043aSCasey Schaufler LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone), 6903e20b043aSCasey Schaufler LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established), 6904e20b043aSCasey Schaufler LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet), 6905e20b043aSCasey Schaufler LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc), 6906e20b043aSCasey Schaufler LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec), 6907e20b043aSCasey Schaufler LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow), 6908e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security), 6909e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security), 6910e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create), 6911e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue), 6912e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach), 6913e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open), 69143a976fa6SDaniel Jurgens #ifdef CONFIG_SECURITY_INFINIBAND 6915cfc4d882SDaniel Jurgens LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access), 6916ab861dfcSDaniel Jurgens LSM_HOOK_INIT(ib_endport_manage_subnet, 6917ab861dfcSDaniel Jurgens selinux_ib_endport_manage_subnet), 69183a976fa6SDaniel Jurgens LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security), 69193a976fa6SDaniel Jurgens LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security), 69203a976fa6SDaniel Jurgens #endif 6921d28d1e08STrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM 6922e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc), 6923e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone), 6924e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free), 6925e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete), 6926e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc), 6927e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_alloc_acquire, 6928e20b043aSCasey Schaufler selinux_xfrm_state_alloc_acquire), 6929e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free), 6930e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete), 6931e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup), 6932e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_pol_flow_match, 6933e20b043aSCasey Schaufler selinux_xfrm_state_pol_flow_match), 6934e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session), 69351da177e4SLinus Torvalds #endif 6936d720024eSMichael LeMay 6937d720024eSMichael LeMay #ifdef CONFIG_KEYS 6938e20b043aSCasey Schaufler LSM_HOOK_INIT(key_alloc, selinux_key_alloc), 6939e20b043aSCasey Schaufler LSM_HOOK_INIT(key_free, selinux_key_free), 6940e20b043aSCasey Schaufler LSM_HOOK_INIT(key_permission, selinux_key_permission), 6941e20b043aSCasey Schaufler LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity), 6942d720024eSMichael LeMay #endif 69439d57a7f9SAhmed S. Darwish 69449d57a7f9SAhmed S. Darwish #ifdef CONFIG_AUDIT 6945e20b043aSCasey Schaufler LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init), 6946e20b043aSCasey Schaufler LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known), 6947e20b043aSCasey Schaufler LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match), 6948e20b043aSCasey Schaufler LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free), 69499d57a7f9SAhmed S. Darwish #endif 6950ec27c356SChenbo Feng 6951ec27c356SChenbo Feng #ifdef CONFIG_BPF_SYSCALL 6952ec27c356SChenbo Feng LSM_HOOK_INIT(bpf, selinux_bpf), 6953ec27c356SChenbo Feng LSM_HOOK_INIT(bpf_map, selinux_bpf_map), 6954ec27c356SChenbo Feng LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog), 6955ec27c356SChenbo Feng LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc), 6956ec27c356SChenbo Feng LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc), 6957ec27c356SChenbo Feng LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free), 6958ec27c356SChenbo Feng LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free), 6959ec27c356SChenbo Feng #endif 69601da177e4SLinus Torvalds }; 69611da177e4SLinus Torvalds 69621da177e4SLinus Torvalds static __init int selinux_init(void) 69631da177e4SLinus Torvalds { 6964b1d9e6b0SCasey Schaufler if (!security_module_enable("selinux")) { 6965076c54c5SAhmed S. Darwish selinux_enabled = 0; 6966076c54c5SAhmed S. Darwish return 0; 6967076c54c5SAhmed S. Darwish } 6968076c54c5SAhmed S. Darwish 69691da177e4SLinus Torvalds if (!selinux_enabled) { 6970c103a91eSpeter enderborg pr_info("SELinux: Disabled at boot.\n"); 69711da177e4SLinus Torvalds return 0; 69721da177e4SLinus Torvalds } 69731da177e4SLinus Torvalds 6974c103a91eSpeter enderborg pr_info("SELinux: Initializing.\n"); 69751da177e4SLinus Torvalds 6976aa8e712cSStephen Smalley memset(&selinux_state, 0, sizeof(selinux_state)); 6977e5a5ca96SPaul Moore enforcing_set(&selinux_state, selinux_enforcing_boot); 6978aa8e712cSStephen Smalley selinux_state.checkreqprot = selinux_checkreqprot_boot; 6979aa8e712cSStephen Smalley selinux_ss_init(&selinux_state.ss); 69806b6bc620SStephen Smalley selinux_avc_init(&selinux_state.avc); 6981aa8e712cSStephen Smalley 69821da177e4SLinus Torvalds /* Set the security state for the initial task. */ 6983d84f4f99SDavid Howells cred_init_security(); 69841da177e4SLinus Torvalds 6985fcaaade1SStephen Smalley default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC); 6986fcaaade1SStephen Smalley 69877cae7e26SJames Morris sel_inode_cache = kmem_cache_create("selinux_inode_security", 69887cae7e26SJames Morris sizeof(struct inode_security_struct), 698920c2df83SPaul Mundt 0, SLAB_PANIC, NULL); 699063205654SSangwoo file_security_cache = kmem_cache_create("selinux_file_security", 699163205654SSangwoo sizeof(struct file_security_struct), 699263205654SSangwoo 0, SLAB_PANIC, NULL); 69931da177e4SLinus Torvalds avc_init(); 69941da177e4SLinus Torvalds 6995aa8e712cSStephen Smalley avtab_cache_init(); 6996aa8e712cSStephen Smalley 6997aa8e712cSStephen Smalley ebitmap_cache_init(); 6998aa8e712cSStephen Smalley 6999aa8e712cSStephen Smalley hashtab_cache_init(); 7000aa8e712cSStephen Smalley 7001d69dece5SCasey Schaufler security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux"); 70021da177e4SLinus Torvalds 7003615e51fdSPaul Moore if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET)) 7004615e51fdSPaul Moore panic("SELinux: Unable to register AVC netcache callback\n"); 7005615e51fdSPaul Moore 70068f408ab6SDaniel Jurgens if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET)) 70078f408ab6SDaniel Jurgens panic("SELinux: Unable to register AVC LSM notifier callback\n"); 70088f408ab6SDaniel Jurgens 7009aa8e712cSStephen Smalley if (selinux_enforcing_boot) 7010c103a91eSpeter enderborg pr_debug("SELinux: Starting in enforcing mode\n"); 7011828dfe1dSEric Paris else 7012c103a91eSpeter enderborg pr_debug("SELinux: Starting in permissive mode\n"); 7013d720024eSMichael LeMay 70141da177e4SLinus Torvalds return 0; 70151da177e4SLinus Torvalds } 70161da177e4SLinus Torvalds 7017e8c26255SAl Viro static void delayed_superblock_init(struct super_block *sb, void *unused) 7018e8c26255SAl Viro { 7019204cc0ccSAl Viro selinux_set_mnt_opts(sb, NULL, 0, NULL); 7020e8c26255SAl Viro } 7021e8c26255SAl Viro 70221da177e4SLinus Torvalds void selinux_complete_init(void) 70231da177e4SLinus Torvalds { 7024c103a91eSpeter enderborg pr_debug("SELinux: Completing initialization.\n"); 70251da177e4SLinus Torvalds 70261da177e4SLinus Torvalds /* Set up any superblocks initialized prior to the policy load. */ 7027c103a91eSpeter enderborg pr_debug("SELinux: Setting up existing superblocks.\n"); 7028e8c26255SAl Viro iterate_supers(delayed_superblock_init, NULL); 70291da177e4SLinus Torvalds } 70301da177e4SLinus Torvalds 70311da177e4SLinus Torvalds /* SELinux requires early initialization in order to label 70321da177e4SLinus Torvalds all processes and objects when they are created. */ 70333d6e5f6dSKees Cook DEFINE_LSM(selinux) = { 703407aed2f2SKees Cook .name = "selinux", 70353d6e5f6dSKees Cook .init = selinux_init, 70363d6e5f6dSKees Cook }; 70371da177e4SLinus Torvalds 7038c2b507fdSStephen Smalley #if defined(CONFIG_NETFILTER) 70391da177e4SLinus Torvalds 7040591bb278SFlorian Westphal static const struct nf_hook_ops selinux_nf_ops[] = { 7041effad8dfSPaul Moore { 7042effad8dfSPaul Moore .hook = selinux_ipv4_postroute, 70432597a834SAlban Crequy .pf = NFPROTO_IPV4, 70446e23ae2aSPatrick McHardy .hooknum = NF_INET_POST_ROUTING, 70451da177e4SLinus Torvalds .priority = NF_IP_PRI_SELINUX_LAST, 7046effad8dfSPaul Moore }, 7047effad8dfSPaul Moore { 7048effad8dfSPaul Moore .hook = selinux_ipv4_forward, 70492597a834SAlban Crequy .pf = NFPROTO_IPV4, 7050effad8dfSPaul Moore .hooknum = NF_INET_FORWARD, 7051effad8dfSPaul Moore .priority = NF_IP_PRI_SELINUX_FIRST, 7052948bf85cSPaul Moore }, 7053948bf85cSPaul Moore { 7054948bf85cSPaul Moore .hook = selinux_ipv4_output, 70552597a834SAlban Crequy .pf = NFPROTO_IPV4, 7056948bf85cSPaul Moore .hooknum = NF_INET_LOCAL_OUT, 7057948bf85cSPaul Moore .priority = NF_IP_PRI_SELINUX_FIRST, 705825db6beaSJiri Pirko }, 70591a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 7060effad8dfSPaul Moore { 7061effad8dfSPaul Moore .hook = selinux_ipv6_postroute, 70622597a834SAlban Crequy .pf = NFPROTO_IPV6, 70636e23ae2aSPatrick McHardy .hooknum = NF_INET_POST_ROUTING, 70641da177e4SLinus Torvalds .priority = NF_IP6_PRI_SELINUX_LAST, 7065effad8dfSPaul Moore }, 7066effad8dfSPaul Moore { 7067effad8dfSPaul Moore .hook = selinux_ipv6_forward, 70682597a834SAlban Crequy .pf = NFPROTO_IPV6, 7069effad8dfSPaul Moore .hooknum = NF_INET_FORWARD, 7070effad8dfSPaul Moore .priority = NF_IP6_PRI_SELINUX_FIRST, 707125db6beaSJiri Pirko }, 70722917f57bSHuw Davies { 70732917f57bSHuw Davies .hook = selinux_ipv6_output, 70742917f57bSHuw Davies .pf = NFPROTO_IPV6, 70752917f57bSHuw Davies .hooknum = NF_INET_LOCAL_OUT, 70762917f57bSHuw Davies .priority = NF_IP6_PRI_SELINUX_FIRST, 70772917f57bSHuw Davies }, 70781da177e4SLinus Torvalds #endif /* IPV6 */ 707925db6beaSJiri Pirko }; 70801da177e4SLinus Torvalds 70818e71bf75SFlorian Westphal static int __net_init selinux_nf_register(struct net *net) 70828e71bf75SFlorian Westphal { 70838e71bf75SFlorian Westphal return nf_register_net_hooks(net, selinux_nf_ops, 70848e71bf75SFlorian Westphal ARRAY_SIZE(selinux_nf_ops)); 70858e71bf75SFlorian Westphal } 70868e71bf75SFlorian Westphal 70878e71bf75SFlorian Westphal static void __net_exit selinux_nf_unregister(struct net *net) 70888e71bf75SFlorian Westphal { 70898e71bf75SFlorian Westphal nf_unregister_net_hooks(net, selinux_nf_ops, 70908e71bf75SFlorian Westphal ARRAY_SIZE(selinux_nf_ops)); 70918e71bf75SFlorian Westphal } 70928e71bf75SFlorian Westphal 70938e71bf75SFlorian Westphal static struct pernet_operations selinux_net_ops = { 70948e71bf75SFlorian Westphal .init = selinux_nf_register, 70958e71bf75SFlorian Westphal .exit = selinux_nf_unregister, 70968e71bf75SFlorian Westphal }; 70978e71bf75SFlorian Westphal 70981da177e4SLinus Torvalds static int __init selinux_nf_ip_init(void) 70991da177e4SLinus Torvalds { 710025db6beaSJiri Pirko int err; 71011da177e4SLinus Torvalds 71021da177e4SLinus Torvalds if (!selinux_enabled) 710325db6beaSJiri Pirko return 0; 71041da177e4SLinus Torvalds 7105c103a91eSpeter enderborg pr_debug("SELinux: Registering netfilter hooks\n"); 71061da177e4SLinus Torvalds 71078e71bf75SFlorian Westphal err = register_pernet_subsys(&selinux_net_ops); 71081da177e4SLinus Torvalds if (err) 71098e71bf75SFlorian Westphal panic("SELinux: register_pernet_subsys: error %d\n", err); 71101da177e4SLinus Torvalds 711125db6beaSJiri Pirko return 0; 71121da177e4SLinus Torvalds } 71131da177e4SLinus Torvalds __initcall(selinux_nf_ip_init); 71141da177e4SLinus Torvalds 71151da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 71161da177e4SLinus Torvalds static void selinux_nf_ip_exit(void) 71171da177e4SLinus Torvalds { 7118c103a91eSpeter enderborg pr_debug("SELinux: Unregistering netfilter hooks\n"); 71191da177e4SLinus Torvalds 71208e71bf75SFlorian Westphal unregister_pernet_subsys(&selinux_net_ops); 71211da177e4SLinus Torvalds } 71221da177e4SLinus Torvalds #endif 71231da177e4SLinus Torvalds 7124c2b507fdSStephen Smalley #else /* CONFIG_NETFILTER */ 71251da177e4SLinus Torvalds 71261da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 71271da177e4SLinus Torvalds #define selinux_nf_ip_exit() 71281da177e4SLinus Torvalds #endif 71291da177e4SLinus Torvalds 7130c2b507fdSStephen Smalley #endif /* CONFIG_NETFILTER */ 71311da177e4SLinus Torvalds 71321da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 7133aa8e712cSStephen Smalley int selinux_disable(struct selinux_state *state) 71341da177e4SLinus Torvalds { 7135aa8e712cSStephen Smalley if (state->initialized) { 71361da177e4SLinus Torvalds /* Not permitted after initial policy load. */ 71371da177e4SLinus Torvalds return -EINVAL; 71381da177e4SLinus Torvalds } 71391da177e4SLinus Torvalds 7140aa8e712cSStephen Smalley if (state->disabled) { 71411da177e4SLinus Torvalds /* Only do this once. */ 71421da177e4SLinus Torvalds return -EINVAL; 71431da177e4SLinus Torvalds } 71441da177e4SLinus Torvalds 7145aa8e712cSStephen Smalley state->disabled = 1; 7146aa8e712cSStephen Smalley 7147c103a91eSpeter enderborg pr_info("SELinux: Disabled at runtime.\n"); 71481da177e4SLinus Torvalds 714930d55280SStephen Smalley selinux_enabled = 0; 71501da177e4SLinus Torvalds 7151b1d9e6b0SCasey Schaufler security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks)); 71521da177e4SLinus Torvalds 7153af8ff049SEric Paris /* Try to destroy the avc node cache */ 7154af8ff049SEric Paris avc_disable(); 7155af8ff049SEric Paris 71561da177e4SLinus Torvalds /* Unregister netfilter hooks. */ 71571da177e4SLinus Torvalds selinux_nf_ip_exit(); 71581da177e4SLinus Torvalds 71591da177e4SLinus Torvalds /* Unregister selinuxfs. */ 71601da177e4SLinus Torvalds exit_sel_fs(); 71611da177e4SLinus Torvalds 71621da177e4SLinus Torvalds return 0; 71631da177e4SLinus Torvalds } 71641da177e4SLinus Torvalds #endif 7165