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 * 61da177e4SLinus Torvalds * Authors: Stephen Smalley, <sds@epoch.ncsc.mil> 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> 201da177e4SLinus Torvalds * 211da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 221da177e4SLinus Torvalds * it under the terms of the GNU General Public License version 2, 231da177e4SLinus Torvalds * as published by the Free Software Foundation. 241da177e4SLinus Torvalds */ 251da177e4SLinus Torvalds 261da177e4SLinus Torvalds #include <linux/init.h> 270b24dcb7SEric Paris #include <linux/kd.h> 281da177e4SLinus Torvalds #include <linux/kernel.h> 290d094efeSRoland McGrath #include <linux/tracehook.h> 301da177e4SLinus Torvalds #include <linux/errno.h> 313f07c014SIngo Molnar #include <linux/sched/signal.h> 3229930025SIngo Molnar #include <linux/sched/task.h> 333c4ed7bdSCasey Schaufler #include <linux/lsm_hooks.h> 341da177e4SLinus Torvalds #include <linux/xattr.h> 351da177e4SLinus Torvalds #include <linux/capability.h> 361da177e4SLinus Torvalds #include <linux/unistd.h> 371da177e4SLinus Torvalds #include <linux/mm.h> 381da177e4SLinus Torvalds #include <linux/mman.h> 391da177e4SLinus Torvalds #include <linux/slab.h> 401da177e4SLinus Torvalds #include <linux/pagemap.h> 410b24dcb7SEric Paris #include <linux/proc_fs.h> 421da177e4SLinus Torvalds #include <linux/swap.h> 431da177e4SLinus Torvalds #include <linux/spinlock.h> 441da177e4SLinus Torvalds #include <linux/syscalls.h> 452a7dba39SEric Paris #include <linux/dcache.h> 461da177e4SLinus Torvalds #include <linux/file.h> 479f3acc31SAl Viro #include <linux/fdtable.h> 481da177e4SLinus Torvalds #include <linux/namei.h> 491da177e4SLinus Torvalds #include <linux/mount.h> 501da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h> 511da177e4SLinus Torvalds #include <linux/netfilter_ipv6.h> 521da177e4SLinus Torvalds #include <linux/tty.h> 531da177e4SLinus Torvalds #include <net/icmp.h> 54227b60f5SStephen Hemminger #include <net/ip.h> /* for local_port_range[] */ 551da177e4SLinus Torvalds #include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */ 5647180068SPaul Moore #include <net/inet_connection_sock.h> 57220deb96SPaul Moore #include <net/net_namespace.h> 58d621d35eSPaul Moore #include <net/netlabel.h> 59f5269710SEric Paris #include <linux/uaccess.h> 601da177e4SLinus Torvalds #include <asm/ioctls.h> 6160063497SArun Sharma #include <linux/atomic.h> 621da177e4SLinus Torvalds #include <linux/bitops.h> 631da177e4SLinus Torvalds #include <linux/interrupt.h> 641da177e4SLinus Torvalds #include <linux/netdevice.h> /* for network interface checks */ 6577954983SHong zhi guo #include <net/netlink.h> 661da177e4SLinus Torvalds #include <linux/tcp.h> 671da177e4SLinus Torvalds #include <linux/udp.h> 682ee92d46SJames Morris #include <linux/dccp.h> 691da177e4SLinus Torvalds #include <linux/quota.h> 701da177e4SLinus Torvalds #include <linux/un.h> /* for Unix socket types */ 711da177e4SLinus Torvalds #include <net/af_unix.h> /* for Unix socket types */ 721da177e4SLinus Torvalds #include <linux/parser.h> 731da177e4SLinus Torvalds #include <linux/nfs_mount.h> 741da177e4SLinus Torvalds #include <net/ipv6.h> 751da177e4SLinus Torvalds #include <linux/hugetlb.h> 761da177e4SLinus Torvalds #include <linux/personality.h> 771da177e4SLinus Torvalds #include <linux/audit.h> 786931dfc9SEric Paris #include <linux/string.h> 79877ce7c1SCatherine Zhang #include <linux/selinux.h> 8023970741SEric Paris #include <linux/mutex.h> 81f06febc9SFrank Mayhar #include <linux/posix-timers.h> 8200234592SKees Cook #include <linux/syslog.h> 833486740aSSerge E. Hallyn #include <linux/user_namespace.h> 8444fc7ea0SPaul Gortmaker #include <linux/export.h> 8540401530SAl Viro #include <linux/msg.h> 8640401530SAl Viro #include <linux/shm.h> 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds #include "avc.h" 891da177e4SLinus Torvalds #include "objsec.h" 901da177e4SLinus Torvalds #include "netif.h" 91224dfbd8SPaul Moore #include "netnode.h" 923e112172SPaul Moore #include "netport.h" 93d28d1e08STrent Jaeger #include "xfrm.h" 94c60475bfSPaul Moore #include "netlabel.h" 959d57a7f9SAhmed S. Darwish #include "audit.h" 967b98a585SJames Morris #include "avc_ss.h" 971da177e4SLinus Torvalds 98d621d35eSPaul Moore /* SECMARK reference count */ 9956a4ca99SJames Morris static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0); 100d621d35eSPaul Moore 1011da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP 102828dfe1dSEric Paris int selinux_enforcing; 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds static int __init enforcing_setup(char *str) 1051da177e4SLinus Torvalds { 106f5269710SEric Paris unsigned long enforcing; 10729707b20SJingoo Han if (!kstrtoul(str, 0, &enforcing)) 108f5269710SEric Paris selinux_enforcing = enforcing ? 1 : 0; 1091da177e4SLinus Torvalds return 1; 1101da177e4SLinus Torvalds } 1111da177e4SLinus Torvalds __setup("enforcing=", enforcing_setup); 1121da177e4SLinus Torvalds #endif 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM 1151da177e4SLinus Torvalds int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE; 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds static int __init selinux_enabled_setup(char *str) 1181da177e4SLinus Torvalds { 119f5269710SEric Paris unsigned long enabled; 12029707b20SJingoo Han if (!kstrtoul(str, 0, &enabled)) 121f5269710SEric Paris selinux_enabled = enabled ? 1 : 0; 1221da177e4SLinus Torvalds return 1; 1231da177e4SLinus Torvalds } 1241da177e4SLinus Torvalds __setup("selinux=", selinux_enabled_setup); 12530d55280SStephen Smalley #else 12630d55280SStephen Smalley int selinux_enabled = 1; 1271da177e4SLinus Torvalds #endif 1281da177e4SLinus Torvalds 129e18b890bSChristoph Lameter static struct kmem_cache *sel_inode_cache; 13063205654SSangwoo static struct kmem_cache *file_security_cache; 1317cae7e26SJames Morris 132d621d35eSPaul Moore /** 133d621d35eSPaul Moore * selinux_secmark_enabled - Check to see if SECMARK is currently enabled 134d621d35eSPaul Moore * 135d621d35eSPaul Moore * Description: 136d621d35eSPaul Moore * This function checks the SECMARK reference counter to see if any SECMARK 137d621d35eSPaul Moore * targets are currently configured, if the reference counter is greater than 138d621d35eSPaul Moore * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is 1392be4d74fSChris PeBenito * enabled, false (0) if SECMARK is disabled. If the always_check_network 1402be4d74fSChris PeBenito * policy capability is enabled, SECMARK is always considered enabled. 141d621d35eSPaul Moore * 142d621d35eSPaul Moore */ 143d621d35eSPaul Moore static int selinux_secmark_enabled(void) 144d621d35eSPaul Moore { 1452be4d74fSChris PeBenito return (selinux_policycap_alwaysnetwork || atomic_read(&selinux_secmark_refcount)); 1462be4d74fSChris PeBenito } 1472be4d74fSChris PeBenito 1482be4d74fSChris PeBenito /** 1492be4d74fSChris PeBenito * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled 1502be4d74fSChris PeBenito * 1512be4d74fSChris PeBenito * Description: 1522be4d74fSChris PeBenito * This function checks if NetLabel or labeled IPSEC is enabled. Returns true 1532be4d74fSChris PeBenito * (1) if any are enabled or false (0) if neither are enabled. If the 1542be4d74fSChris PeBenito * always_check_network policy capability is enabled, peer labeling 1552be4d74fSChris PeBenito * is always considered enabled. 1562be4d74fSChris PeBenito * 1572be4d74fSChris PeBenito */ 1582be4d74fSChris PeBenito static int selinux_peerlbl_enabled(void) 1592be4d74fSChris PeBenito { 1602be4d74fSChris PeBenito return (selinux_policycap_alwaysnetwork || netlbl_enabled() || selinux_xfrm_enabled()); 161d621d35eSPaul Moore } 162d621d35eSPaul Moore 163615e51fdSPaul Moore static int selinux_netcache_avc_callback(u32 event) 164615e51fdSPaul Moore { 165615e51fdSPaul Moore if (event == AVC_CALLBACK_RESET) { 166615e51fdSPaul Moore sel_netif_flush(); 167615e51fdSPaul Moore sel_netnode_flush(); 168615e51fdSPaul Moore sel_netport_flush(); 169615e51fdSPaul Moore synchronize_net(); 170615e51fdSPaul Moore } 171615e51fdSPaul Moore return 0; 172615e51fdSPaul Moore } 173615e51fdSPaul Moore 174d84f4f99SDavid Howells /* 175d84f4f99SDavid Howells * initialise the security for the init task 176d84f4f99SDavid Howells */ 177d84f4f99SDavid Howells static void cred_init_security(void) 1781da177e4SLinus Torvalds { 1793b11a1deSDavid Howells struct cred *cred = (struct cred *) current->real_cred; 1801da177e4SLinus Torvalds struct task_security_struct *tsec; 1811da177e4SLinus Torvalds 18289d155efSJames Morris tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL); 1831da177e4SLinus Torvalds if (!tsec) 184d84f4f99SDavid Howells panic("SELinux: Failed to initialize initial task.\n"); 1851da177e4SLinus Torvalds 186d84f4f99SDavid Howells tsec->osid = tsec->sid = SECINITSID_KERNEL; 187f1752eecSDavid Howells cred->security = tsec; 1881da177e4SLinus Torvalds } 1891da177e4SLinus Torvalds 190275bb41eSDavid Howells /* 19188e67f3bSDavid Howells * get the security ID of a set of credentials 19288e67f3bSDavid Howells */ 19388e67f3bSDavid Howells static inline u32 cred_sid(const struct cred *cred) 19488e67f3bSDavid Howells { 19588e67f3bSDavid Howells const struct task_security_struct *tsec; 19688e67f3bSDavid Howells 19788e67f3bSDavid Howells tsec = cred->security; 19888e67f3bSDavid Howells return tsec->sid; 19988e67f3bSDavid Howells } 20088e67f3bSDavid Howells 20188e67f3bSDavid Howells /* 2023b11a1deSDavid Howells * get the objective security ID of a task 203275bb41eSDavid Howells */ 204275bb41eSDavid Howells static inline u32 task_sid(const struct task_struct *task) 205275bb41eSDavid Howells { 206275bb41eSDavid Howells u32 sid; 207275bb41eSDavid Howells 208275bb41eSDavid Howells rcu_read_lock(); 20988e67f3bSDavid Howells sid = cred_sid(__task_cred(task)); 210275bb41eSDavid Howells rcu_read_unlock(); 211275bb41eSDavid Howells return sid; 212275bb41eSDavid Howells } 213275bb41eSDavid Howells 21488e67f3bSDavid Howells /* Allocate and free functions for each kind of security blob. */ 21588e67f3bSDavid Howells 2161da177e4SLinus Torvalds static int inode_alloc_security(struct inode *inode) 2171da177e4SLinus Torvalds { 2181da177e4SLinus Torvalds struct inode_security_struct *isec; 219275bb41eSDavid Howells u32 sid = current_sid(); 2201da177e4SLinus Torvalds 221a02fe132SJosef Bacik isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS); 2221da177e4SLinus Torvalds if (!isec) 2231da177e4SLinus Torvalds return -ENOMEM; 2241da177e4SLinus Torvalds 2259287aed2SAndreas Gruenbacher spin_lock_init(&isec->lock); 2261da177e4SLinus Torvalds INIT_LIST_HEAD(&isec->list); 2271da177e4SLinus Torvalds isec->inode = inode; 2281da177e4SLinus Torvalds isec->sid = SECINITSID_UNLABELED; 2291da177e4SLinus Torvalds isec->sclass = SECCLASS_FILE; 230275bb41eSDavid Howells isec->task_sid = sid; 23142059112SAndreas Gruenbacher isec->initialized = LABEL_INVALID; 2321da177e4SLinus Torvalds inode->i_security = isec; 2331da177e4SLinus Torvalds 2341da177e4SLinus Torvalds return 0; 2351da177e4SLinus Torvalds } 2361da177e4SLinus Torvalds 2375d226df4SAndreas Gruenbacher static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry); 2385d226df4SAndreas Gruenbacher 2395d226df4SAndreas Gruenbacher /* 2405d226df4SAndreas Gruenbacher * Try reloading inode security labels that have been marked as invalid. The 2415d226df4SAndreas Gruenbacher * @may_sleep parameter indicates when sleeping and thus reloading labels is 24242059112SAndreas Gruenbacher * allowed; when set to false, returns -ECHILD when the label is 2435d226df4SAndreas Gruenbacher * invalid. The @opt_dentry parameter should be set to a dentry of the inode; 2445d226df4SAndreas Gruenbacher * when no dentry is available, set it to NULL instead. 2455d226df4SAndreas Gruenbacher */ 2465d226df4SAndreas Gruenbacher static int __inode_security_revalidate(struct inode *inode, 2475d226df4SAndreas Gruenbacher struct dentry *opt_dentry, 2485d226df4SAndreas Gruenbacher bool may_sleep) 2495d226df4SAndreas Gruenbacher { 2505d226df4SAndreas Gruenbacher struct inode_security_struct *isec = inode->i_security; 2515d226df4SAndreas Gruenbacher 2525d226df4SAndreas Gruenbacher might_sleep_if(may_sleep); 2535d226df4SAndreas Gruenbacher 2541ac42476SPaul Moore if (ss_initialized && isec->initialized != LABEL_INITIALIZED) { 2555d226df4SAndreas Gruenbacher if (!may_sleep) 2565d226df4SAndreas Gruenbacher return -ECHILD; 2575d226df4SAndreas Gruenbacher 2585d226df4SAndreas Gruenbacher /* 2595d226df4SAndreas Gruenbacher * Try reloading the inode security label. This will fail if 2605d226df4SAndreas Gruenbacher * @opt_dentry is NULL and no dentry for this inode can be 2615d226df4SAndreas Gruenbacher * found; in that case, continue using the old label. 2625d226df4SAndreas Gruenbacher */ 2635d226df4SAndreas Gruenbacher inode_doinit_with_dentry(inode, opt_dentry); 2645d226df4SAndreas Gruenbacher } 2655d226df4SAndreas Gruenbacher return 0; 2665d226df4SAndreas Gruenbacher } 2675d226df4SAndreas Gruenbacher 2685d226df4SAndreas Gruenbacher static struct inode_security_struct *inode_security_novalidate(struct inode *inode) 2695d226df4SAndreas Gruenbacher { 2705d226df4SAndreas Gruenbacher return inode->i_security; 2715d226df4SAndreas Gruenbacher } 2725d226df4SAndreas Gruenbacher 2735d226df4SAndreas Gruenbacher static struct inode_security_struct *inode_security_rcu(struct inode *inode, bool rcu) 2745d226df4SAndreas Gruenbacher { 2755d226df4SAndreas Gruenbacher int error; 2765d226df4SAndreas Gruenbacher 2775d226df4SAndreas Gruenbacher error = __inode_security_revalidate(inode, NULL, !rcu); 2785d226df4SAndreas Gruenbacher if (error) 2795d226df4SAndreas Gruenbacher return ERR_PTR(error); 2805d226df4SAndreas Gruenbacher return inode->i_security; 2815d226df4SAndreas Gruenbacher } 2825d226df4SAndreas Gruenbacher 28383da53c5SAndreas Gruenbacher /* 28483da53c5SAndreas Gruenbacher * Get the security label of an inode. 28583da53c5SAndreas Gruenbacher */ 28683da53c5SAndreas Gruenbacher static struct inode_security_struct *inode_security(struct inode *inode) 28783da53c5SAndreas Gruenbacher { 2885d226df4SAndreas Gruenbacher __inode_security_revalidate(inode, NULL, true); 28983da53c5SAndreas Gruenbacher return inode->i_security; 29083da53c5SAndreas Gruenbacher } 29183da53c5SAndreas Gruenbacher 2922c97165bSPaul Moore static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry) 2932c97165bSPaul Moore { 2942c97165bSPaul Moore struct inode *inode = d_backing_inode(dentry); 2952c97165bSPaul Moore 2962c97165bSPaul Moore return inode->i_security; 2972c97165bSPaul Moore } 2982c97165bSPaul Moore 29983da53c5SAndreas Gruenbacher /* 30083da53c5SAndreas Gruenbacher * Get the security label of a dentry's backing inode. 30183da53c5SAndreas Gruenbacher */ 30283da53c5SAndreas Gruenbacher static struct inode_security_struct *backing_inode_security(struct dentry *dentry) 30383da53c5SAndreas Gruenbacher { 30483da53c5SAndreas Gruenbacher struct inode *inode = d_backing_inode(dentry); 30583da53c5SAndreas Gruenbacher 3065d226df4SAndreas Gruenbacher __inode_security_revalidate(inode, dentry, true); 30783da53c5SAndreas Gruenbacher return inode->i_security; 30883da53c5SAndreas Gruenbacher } 30983da53c5SAndreas Gruenbacher 3103dc91d43SSteven Rostedt static void inode_free_rcu(struct rcu_head *head) 3113dc91d43SSteven Rostedt { 3123dc91d43SSteven Rostedt struct inode_security_struct *isec; 3133dc91d43SSteven Rostedt 3143dc91d43SSteven Rostedt isec = container_of(head, struct inode_security_struct, rcu); 3153dc91d43SSteven Rostedt kmem_cache_free(sel_inode_cache, isec); 3163dc91d43SSteven Rostedt } 3173dc91d43SSteven Rostedt 3181da177e4SLinus Torvalds static void inode_free_security(struct inode *inode) 3191da177e4SLinus Torvalds { 3201da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 3211da177e4SLinus Torvalds struct superblock_security_struct *sbsec = inode->i_sb->s_security; 3221da177e4SLinus Torvalds 3239629d04aSWaiman Long /* 3249629d04aSWaiman Long * As not all inode security structures are in a list, we check for 3259629d04aSWaiman Long * empty list outside of the lock to make sure that we won't waste 3269629d04aSWaiman Long * time taking a lock doing nothing. 3279629d04aSWaiman Long * 3289629d04aSWaiman Long * The list_del_init() function can be safely called more than once. 3299629d04aSWaiman Long * It should not be possible for this function to be called with 3309629d04aSWaiman Long * concurrent list_add(), but for better safety against future changes 3319629d04aSWaiman Long * in the code, we use list_empty_careful() here. 3329629d04aSWaiman Long */ 3339629d04aSWaiman Long if (!list_empty_careful(&isec->list)) { 3341da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 3351da177e4SLinus Torvalds list_del_init(&isec->list); 3361da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 3379629d04aSWaiman Long } 3381da177e4SLinus Torvalds 3393dc91d43SSteven Rostedt /* 3403dc91d43SSteven Rostedt * The inode may still be referenced in a path walk and 3413dc91d43SSteven Rostedt * a call to selinux_inode_permission() can be made 3423dc91d43SSteven Rostedt * after inode_free_security() is called. Ideally, the VFS 3433dc91d43SSteven Rostedt * wouldn't do this, but fixing that is a much harder 3443dc91d43SSteven Rostedt * job. For now, simply free the i_security via RCU, and 3453dc91d43SSteven Rostedt * leave the current inode->i_security pointer intact. 3463dc91d43SSteven Rostedt * The inode will be freed after the RCU grace period too. 3473dc91d43SSteven Rostedt */ 3483dc91d43SSteven Rostedt call_rcu(&isec->rcu, inode_free_rcu); 3491da177e4SLinus Torvalds } 3501da177e4SLinus Torvalds 3511da177e4SLinus Torvalds static int file_alloc_security(struct file *file) 3521da177e4SLinus Torvalds { 3531da177e4SLinus Torvalds struct file_security_struct *fsec; 354275bb41eSDavid Howells u32 sid = current_sid(); 3551da177e4SLinus Torvalds 35663205654SSangwoo fsec = kmem_cache_zalloc(file_security_cache, GFP_KERNEL); 3571da177e4SLinus Torvalds if (!fsec) 3581da177e4SLinus Torvalds return -ENOMEM; 3591da177e4SLinus Torvalds 360275bb41eSDavid Howells fsec->sid = sid; 361275bb41eSDavid Howells fsec->fown_sid = sid; 3621da177e4SLinus Torvalds file->f_security = fsec; 3631da177e4SLinus Torvalds 3641da177e4SLinus Torvalds return 0; 3651da177e4SLinus Torvalds } 3661da177e4SLinus Torvalds 3671da177e4SLinus Torvalds static void file_free_security(struct file *file) 3681da177e4SLinus Torvalds { 3691da177e4SLinus Torvalds struct file_security_struct *fsec = file->f_security; 3701da177e4SLinus Torvalds file->f_security = NULL; 37163205654SSangwoo kmem_cache_free(file_security_cache, fsec); 3721da177e4SLinus Torvalds } 3731da177e4SLinus Torvalds 3741da177e4SLinus Torvalds static int superblock_alloc_security(struct super_block *sb) 3751da177e4SLinus Torvalds { 3761da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 3771da177e4SLinus Torvalds 37889d155efSJames Morris sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL); 3791da177e4SLinus Torvalds if (!sbsec) 3801da177e4SLinus Torvalds return -ENOMEM; 3811da177e4SLinus Torvalds 382bc7e982bSEric Paris mutex_init(&sbsec->lock); 3831da177e4SLinus Torvalds INIT_LIST_HEAD(&sbsec->isec_head); 3841da177e4SLinus Torvalds spin_lock_init(&sbsec->isec_lock); 3851da177e4SLinus Torvalds sbsec->sb = sb; 3861da177e4SLinus Torvalds sbsec->sid = SECINITSID_UNLABELED; 3871da177e4SLinus Torvalds sbsec->def_sid = SECINITSID_FILE; 388c312feb2SEric Paris sbsec->mntpoint_sid = SECINITSID_UNLABELED; 3891da177e4SLinus Torvalds sb->s_security = sbsec; 3901da177e4SLinus Torvalds 3911da177e4SLinus Torvalds return 0; 3921da177e4SLinus Torvalds } 3931da177e4SLinus Torvalds 3941da177e4SLinus Torvalds static void superblock_free_security(struct super_block *sb) 3951da177e4SLinus Torvalds { 3961da177e4SLinus Torvalds struct superblock_security_struct *sbsec = sb->s_security; 3971da177e4SLinus Torvalds sb->s_security = NULL; 3981da177e4SLinus Torvalds kfree(sbsec); 3991da177e4SLinus Torvalds } 4001da177e4SLinus Torvalds 4011da177e4SLinus Torvalds /* The file system's label must be initialized prior to use. */ 4021da177e4SLinus Torvalds 403eb9ae686SDavid Quigley static const char *labeling_behaviors[7] = { 4041da177e4SLinus Torvalds "uses xattr", 4051da177e4SLinus Torvalds "uses transition SIDs", 4061da177e4SLinus Torvalds "uses task SIDs", 4071da177e4SLinus Torvalds "uses genfs_contexts", 4081da177e4SLinus Torvalds "not configured for labeling", 4091da177e4SLinus Torvalds "uses mountpoint labeling", 410eb9ae686SDavid Quigley "uses native labeling", 4111da177e4SLinus Torvalds }; 4121da177e4SLinus Torvalds 4131da177e4SLinus Torvalds static inline int inode_doinit(struct inode *inode) 4141da177e4SLinus Torvalds { 4151da177e4SLinus Torvalds return inode_doinit_with_dentry(inode, NULL); 4161da177e4SLinus Torvalds } 4171da177e4SLinus Torvalds 4181da177e4SLinus Torvalds enum { 41931e87930SEric Paris Opt_error = -1, 4201da177e4SLinus Torvalds Opt_context = 1, 4211da177e4SLinus Torvalds Opt_fscontext = 2, 422c9180a57SEric Paris Opt_defcontext = 3, 423c9180a57SEric Paris Opt_rootcontext = 4, 42411689d47SDavid P. Quigley Opt_labelsupport = 5, 425d355987fSEric Paris Opt_nextmntopt = 6, 4261da177e4SLinus Torvalds }; 4271da177e4SLinus Torvalds 428d355987fSEric Paris #define NUM_SEL_MNT_OPTS (Opt_nextmntopt - 1) 429d355987fSEric Paris 430a447c093SSteven Whitehouse static const match_table_t tokens = { 431832cbd9aSEric Paris {Opt_context, CONTEXT_STR "%s"}, 432832cbd9aSEric Paris {Opt_fscontext, FSCONTEXT_STR "%s"}, 433832cbd9aSEric Paris {Opt_defcontext, DEFCONTEXT_STR "%s"}, 434832cbd9aSEric Paris {Opt_rootcontext, ROOTCONTEXT_STR "%s"}, 43511689d47SDavid P. Quigley {Opt_labelsupport, LABELSUPP_STR}, 43631e87930SEric Paris {Opt_error, NULL}, 4371da177e4SLinus Torvalds }; 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n" 4401da177e4SLinus Torvalds 441c312feb2SEric Paris static int may_context_mount_sb_relabel(u32 sid, 442c312feb2SEric Paris struct superblock_security_struct *sbsec, 443275bb41eSDavid Howells const struct cred *cred) 444c312feb2SEric Paris { 445275bb41eSDavid Howells const struct task_security_struct *tsec = cred->security; 446c312feb2SEric Paris int rc; 447c312feb2SEric Paris 448c312feb2SEric Paris rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 449c312feb2SEric Paris FILESYSTEM__RELABELFROM, NULL); 450c312feb2SEric Paris if (rc) 451c312feb2SEric Paris return rc; 452c312feb2SEric Paris 453c312feb2SEric Paris rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM, 454c312feb2SEric Paris FILESYSTEM__RELABELTO, NULL); 455c312feb2SEric Paris return rc; 456c312feb2SEric Paris } 457c312feb2SEric Paris 4580808925eSEric Paris static int may_context_mount_inode_relabel(u32 sid, 4590808925eSEric Paris struct superblock_security_struct *sbsec, 460275bb41eSDavid Howells const struct cred *cred) 4610808925eSEric Paris { 462275bb41eSDavid Howells const struct task_security_struct *tsec = cred->security; 4630808925eSEric Paris int rc; 4640808925eSEric Paris rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 4650808925eSEric Paris FILESYSTEM__RELABELFROM, NULL); 4660808925eSEric Paris if (rc) 4670808925eSEric Paris return rc; 4680808925eSEric Paris 4690808925eSEric Paris rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, 4700808925eSEric Paris FILESYSTEM__ASSOCIATE, NULL); 4710808925eSEric Paris return rc; 4720808925eSEric Paris } 4730808925eSEric Paris 474b43e725dSEric Paris static int selinux_is_sblabel_mnt(struct super_block *sb) 475b43e725dSEric Paris { 476b43e725dSEric Paris struct superblock_security_struct *sbsec = sb->s_security; 477b43e725dSEric Paris 478d5f3a5f6SMark Salyzyn return sbsec->behavior == SECURITY_FS_USE_XATTR || 479b43e725dSEric Paris sbsec->behavior == SECURITY_FS_USE_TRANS || 480d5f3a5f6SMark Salyzyn sbsec->behavior == SECURITY_FS_USE_TASK || 4819fc2b4b4SJ. Bruce Fields sbsec->behavior == SECURITY_FS_USE_NATIVE || 482d5f3a5f6SMark Salyzyn /* Special handling. Genfs but also in-core setxattr handler */ 483d5f3a5f6SMark Salyzyn !strcmp(sb->s_type->name, "sysfs") || 484d5f3a5f6SMark Salyzyn !strcmp(sb->s_type->name, "pstore") || 485d5f3a5f6SMark Salyzyn !strcmp(sb->s_type->name, "debugfs") || 486a2c7c6fbSYongqin Liu !strcmp(sb->s_type->name, "tracefs") || 4872651225bSStephen Smalley !strcmp(sb->s_type->name, "rootfs") || 4882651225bSStephen Smalley (selinux_policycap_cgroupseclabel && 4892651225bSStephen Smalley (!strcmp(sb->s_type->name, "cgroup") || 4902651225bSStephen Smalley !strcmp(sb->s_type->name, "cgroup2"))); 491b43e725dSEric Paris } 492b43e725dSEric Paris 493c9180a57SEric Paris static int sb_finish_set_opts(struct super_block *sb) 4941da177e4SLinus Torvalds { 4951da177e4SLinus Torvalds struct superblock_security_struct *sbsec = sb->s_security; 4961da177e4SLinus Torvalds struct dentry *root = sb->s_root; 497c6f493d6SDavid Howells struct inode *root_inode = d_backing_inode(root); 4981da177e4SLinus Torvalds int rc = 0; 4991da177e4SLinus Torvalds 5001da177e4SLinus Torvalds if (sbsec->behavior == SECURITY_FS_USE_XATTR) { 5011da177e4SLinus Torvalds /* Make sure that the xattr handler exists and that no 5021da177e4SLinus Torvalds error other than -ENODATA is returned by getxattr on 5031da177e4SLinus Torvalds the root directory. -ENODATA is ok, as this may be 5041da177e4SLinus Torvalds the first boot of the SELinux kernel before we have 5051da177e4SLinus Torvalds assigned xattr values to the filesystem. */ 5065d6c3191SAndreas Gruenbacher if (!(root_inode->i_opflags & IOP_XATTR)) { 50729b1deb2SLinus Torvalds printk(KERN_WARNING "SELinux: (dev %s, type %s) has no " 50829b1deb2SLinus Torvalds "xattr support\n", sb->s_id, sb->s_type->name); 5091da177e4SLinus Torvalds rc = -EOPNOTSUPP; 5101da177e4SLinus Torvalds goto out; 5111da177e4SLinus Torvalds } 5125d6c3191SAndreas Gruenbacher 5135d6c3191SAndreas Gruenbacher rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0); 5141da177e4SLinus Torvalds if (rc < 0 && rc != -ENODATA) { 5151da177e4SLinus Torvalds if (rc == -EOPNOTSUPP) 5161da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: (dev %s, type " 51729b1deb2SLinus Torvalds "%s) has no security xattr handler\n", 51829b1deb2SLinus Torvalds sb->s_id, sb->s_type->name); 5191da177e4SLinus Torvalds else 5201da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: (dev %s, type " 52129b1deb2SLinus Torvalds "%s) getxattr errno %d\n", sb->s_id, 52229b1deb2SLinus Torvalds sb->s_type->name, -rc); 5231da177e4SLinus Torvalds goto out; 5241da177e4SLinus Torvalds } 5251da177e4SLinus Torvalds } 5261da177e4SLinus Torvalds 527c9180a57SEric Paris if (sbsec->behavior > ARRAY_SIZE(labeling_behaviors)) 52829b1deb2SLinus Torvalds printk(KERN_ERR "SELinux: initialized (dev %s, type %s), unknown behavior\n", 52929b1deb2SLinus Torvalds sb->s_id, sb->s_type->name); 5301da177e4SLinus Torvalds 531eadcabc6SEric Paris sbsec->flags |= SE_SBINITIALIZED; 532b43e725dSEric Paris if (selinux_is_sblabel_mnt(sb)) 53312f348b9SEric Paris sbsec->flags |= SBLABEL_MNT; 534ddd29ec6SDavid P. Quigley 5351da177e4SLinus Torvalds /* Initialize the root inode. */ 536c9180a57SEric Paris rc = inode_doinit_with_dentry(root_inode, root); 5371da177e4SLinus Torvalds 5381da177e4SLinus Torvalds /* Initialize any other inodes associated with the superblock, e.g. 5391da177e4SLinus Torvalds inodes created prior to initial policy load or inodes created 5401da177e4SLinus Torvalds during get_sb by a pseudo filesystem that directly 5411da177e4SLinus Torvalds populates itself. */ 5421da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 5431da177e4SLinus Torvalds next_inode: 5441da177e4SLinus Torvalds if (!list_empty(&sbsec->isec_head)) { 5451da177e4SLinus Torvalds struct inode_security_struct *isec = 5461da177e4SLinus Torvalds list_entry(sbsec->isec_head.next, 5471da177e4SLinus Torvalds struct inode_security_struct, list); 5481da177e4SLinus Torvalds struct inode *inode = isec->inode; 549923190d3SStephen Smalley list_del_init(&isec->list); 5501da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 5511da177e4SLinus Torvalds inode = igrab(inode); 5521da177e4SLinus Torvalds if (inode) { 5531da177e4SLinus Torvalds if (!IS_PRIVATE(inode)) 5541da177e4SLinus Torvalds inode_doinit(inode); 5551da177e4SLinus Torvalds iput(inode); 5561da177e4SLinus Torvalds } 5571da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 5581da177e4SLinus Torvalds goto next_inode; 5591da177e4SLinus Torvalds } 5601da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 5611da177e4SLinus Torvalds out: 562c9180a57SEric Paris return rc; 563c9180a57SEric Paris } 564c9180a57SEric Paris 565c9180a57SEric Paris /* 566c9180a57SEric Paris * This function should allow an FS to ask what it's mount security 567c9180a57SEric Paris * options were so it can use those later for submounts, displaying 568c9180a57SEric Paris * mount options, or whatever. 569c9180a57SEric Paris */ 570c9180a57SEric Paris static int selinux_get_mnt_opts(const struct super_block *sb, 571e0007529SEric Paris struct security_mnt_opts *opts) 572c9180a57SEric Paris { 573c9180a57SEric Paris int rc = 0, i; 574c9180a57SEric Paris struct superblock_security_struct *sbsec = sb->s_security; 575c9180a57SEric Paris char *context = NULL; 576c9180a57SEric Paris u32 len; 577c9180a57SEric Paris char tmp; 578c9180a57SEric Paris 579e0007529SEric Paris security_init_mnt_opts(opts); 580c9180a57SEric Paris 5810d90a7ecSDavid P. Quigley if (!(sbsec->flags & SE_SBINITIALIZED)) 582c9180a57SEric Paris return -EINVAL; 583c9180a57SEric Paris 584c9180a57SEric Paris if (!ss_initialized) 585c9180a57SEric Paris return -EINVAL; 586c9180a57SEric Paris 587af8e50ccSEric Paris /* make sure we always check enough bits to cover the mask */ 588af8e50ccSEric Paris BUILD_BUG_ON(SE_MNTMASK >= (1 << NUM_SEL_MNT_OPTS)); 589af8e50ccSEric Paris 5900d90a7ecSDavid P. Quigley tmp = sbsec->flags & SE_MNTMASK; 591c9180a57SEric Paris /* count the number of mount options for this sb */ 592af8e50ccSEric Paris for (i = 0; i < NUM_SEL_MNT_OPTS; i++) { 593c9180a57SEric Paris if (tmp & 0x01) 594e0007529SEric Paris opts->num_mnt_opts++; 595c9180a57SEric Paris tmp >>= 1; 596c9180a57SEric Paris } 59711689d47SDavid P. Quigley /* Check if the Label support flag is set */ 5980b4bdb35SEric Paris if (sbsec->flags & SBLABEL_MNT) 59911689d47SDavid P. Quigley opts->num_mnt_opts++; 600c9180a57SEric Paris 601e0007529SEric Paris opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC); 602e0007529SEric Paris if (!opts->mnt_opts) { 603c9180a57SEric Paris rc = -ENOMEM; 604c9180a57SEric Paris goto out_free; 605c9180a57SEric Paris } 606c9180a57SEric Paris 607e0007529SEric Paris opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC); 608e0007529SEric Paris if (!opts->mnt_opts_flags) { 609c9180a57SEric Paris rc = -ENOMEM; 610c9180a57SEric Paris goto out_free; 611c9180a57SEric Paris } 612c9180a57SEric Paris 613c9180a57SEric Paris i = 0; 614c9180a57SEric Paris if (sbsec->flags & FSCONTEXT_MNT) { 615c9180a57SEric Paris rc = security_sid_to_context(sbsec->sid, &context, &len); 616c9180a57SEric Paris if (rc) 617c9180a57SEric Paris goto out_free; 618e0007529SEric Paris opts->mnt_opts[i] = context; 619e0007529SEric Paris opts->mnt_opts_flags[i++] = FSCONTEXT_MNT; 620c9180a57SEric Paris } 621c9180a57SEric Paris if (sbsec->flags & CONTEXT_MNT) { 622c9180a57SEric Paris rc = security_sid_to_context(sbsec->mntpoint_sid, &context, &len); 623c9180a57SEric Paris if (rc) 624c9180a57SEric Paris goto out_free; 625e0007529SEric Paris opts->mnt_opts[i] = context; 626e0007529SEric Paris opts->mnt_opts_flags[i++] = CONTEXT_MNT; 627c9180a57SEric Paris } 628c9180a57SEric Paris if (sbsec->flags & DEFCONTEXT_MNT) { 629c9180a57SEric Paris rc = security_sid_to_context(sbsec->def_sid, &context, &len); 630c9180a57SEric Paris if (rc) 631c9180a57SEric Paris goto out_free; 632e0007529SEric Paris opts->mnt_opts[i] = context; 633e0007529SEric Paris opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT; 634c9180a57SEric Paris } 635c9180a57SEric Paris if (sbsec->flags & ROOTCONTEXT_MNT) { 63683da53c5SAndreas Gruenbacher struct dentry *root = sbsec->sb->s_root; 63783da53c5SAndreas Gruenbacher struct inode_security_struct *isec = backing_inode_security(root); 638c9180a57SEric Paris 639c9180a57SEric Paris rc = security_sid_to_context(isec->sid, &context, &len); 640c9180a57SEric Paris if (rc) 641c9180a57SEric Paris goto out_free; 642e0007529SEric Paris opts->mnt_opts[i] = context; 643e0007529SEric Paris opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT; 644c9180a57SEric Paris } 64512f348b9SEric Paris if (sbsec->flags & SBLABEL_MNT) { 64611689d47SDavid P. Quigley opts->mnt_opts[i] = NULL; 64712f348b9SEric Paris opts->mnt_opts_flags[i++] = SBLABEL_MNT; 64811689d47SDavid P. Quigley } 649c9180a57SEric Paris 650e0007529SEric Paris BUG_ON(i != opts->num_mnt_opts); 651c9180a57SEric Paris 652c9180a57SEric Paris return 0; 653c9180a57SEric Paris 654c9180a57SEric Paris out_free: 655e0007529SEric Paris security_free_mnt_opts(opts); 656c9180a57SEric Paris return rc; 657c9180a57SEric Paris } 658c9180a57SEric Paris 659c9180a57SEric Paris static int bad_option(struct superblock_security_struct *sbsec, char flag, 660c9180a57SEric Paris u32 old_sid, u32 new_sid) 661c9180a57SEric Paris { 6620d90a7ecSDavid P. Quigley char mnt_flags = sbsec->flags & SE_MNTMASK; 6630d90a7ecSDavid P. Quigley 664c9180a57SEric Paris /* check if the old mount command had the same options */ 6650d90a7ecSDavid P. Quigley if (sbsec->flags & SE_SBINITIALIZED) 666c9180a57SEric Paris if (!(sbsec->flags & flag) || 667c9180a57SEric Paris (old_sid != new_sid)) 668c9180a57SEric Paris return 1; 669c9180a57SEric Paris 670c9180a57SEric Paris /* check if we were passed the same options twice, 671c9180a57SEric Paris * aka someone passed context=a,context=b 672c9180a57SEric Paris */ 6730d90a7ecSDavid P. Quigley if (!(sbsec->flags & SE_SBINITIALIZED)) 6740d90a7ecSDavid P. Quigley if (mnt_flags & flag) 675c9180a57SEric Paris return 1; 676c9180a57SEric Paris return 0; 677c9180a57SEric Paris } 678e0007529SEric Paris 679c9180a57SEric Paris /* 680c9180a57SEric Paris * Allow filesystems with binary mount data to explicitly set mount point 681c9180a57SEric Paris * labeling information. 682c9180a57SEric Paris */ 683e0007529SEric Paris static int selinux_set_mnt_opts(struct super_block *sb, 684649f6e77SDavid Quigley struct security_mnt_opts *opts, 685649f6e77SDavid Quigley unsigned long kern_flags, 686649f6e77SDavid Quigley unsigned long *set_kern_flags) 687c9180a57SEric Paris { 688275bb41eSDavid Howells const struct cred *cred = current_cred(); 689c9180a57SEric Paris int rc = 0, i; 690c9180a57SEric Paris struct superblock_security_struct *sbsec = sb->s_security; 69129b1deb2SLinus Torvalds const char *name = sb->s_type->name; 69283da53c5SAndreas Gruenbacher struct dentry *root = sbsec->sb->s_root; 6932c97165bSPaul Moore struct inode_security_struct *root_isec; 694c9180a57SEric Paris u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0; 695c9180a57SEric Paris u32 defcontext_sid = 0; 696e0007529SEric Paris char **mount_options = opts->mnt_opts; 697e0007529SEric Paris int *flags = opts->mnt_opts_flags; 698e0007529SEric Paris int num_opts = opts->num_mnt_opts; 699c9180a57SEric Paris 700c9180a57SEric Paris mutex_lock(&sbsec->lock); 701c9180a57SEric Paris 702c9180a57SEric Paris if (!ss_initialized) { 703c9180a57SEric Paris if (!num_opts) { 704c9180a57SEric Paris /* Defer initialization until selinux_complete_init, 705c9180a57SEric Paris after the initial policy is loaded and the security 706c9180a57SEric Paris server is ready to handle calls. */ 707c9180a57SEric Paris goto out; 708c9180a57SEric Paris } 709c9180a57SEric Paris rc = -EINVAL; 710744ba35eSEric Paris printk(KERN_WARNING "SELinux: Unable to set superblock options " 711744ba35eSEric Paris "before the security server is initialized\n"); 712c9180a57SEric Paris goto out; 713c9180a57SEric Paris } 714649f6e77SDavid Quigley if (kern_flags && !set_kern_flags) { 715649f6e77SDavid Quigley /* Specifying internal flags without providing a place to 716649f6e77SDavid Quigley * place the results is not allowed */ 717649f6e77SDavid Quigley rc = -EINVAL; 718649f6e77SDavid Quigley goto out; 719649f6e77SDavid Quigley } 720c9180a57SEric Paris 721c9180a57SEric Paris /* 722e0007529SEric Paris * Binary mount data FS will come through this function twice. Once 723e0007529SEric Paris * from an explicit call and once from the generic calls from the vfs. 724e0007529SEric Paris * Since the generic VFS calls will not contain any security mount data 725e0007529SEric Paris * we need to skip the double mount verification. 726e0007529SEric Paris * 727e0007529SEric Paris * This does open a hole in which we will not notice if the first 728e0007529SEric Paris * mount using this sb set explict options and a second mount using 729e0007529SEric Paris * this sb does not set any security options. (The first options 730e0007529SEric Paris * will be used for both mounts) 731e0007529SEric Paris */ 7320d90a7ecSDavid P. Quigley if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA) 733e0007529SEric Paris && (num_opts == 0)) 734e0007529SEric Paris goto out; 735e0007529SEric Paris 7362c97165bSPaul Moore root_isec = backing_inode_security_novalidate(root); 7372c97165bSPaul Moore 738e0007529SEric Paris /* 739c9180a57SEric Paris * parse the mount options, check if they are valid sids. 740c9180a57SEric Paris * also check if someone is trying to mount the same sb more 741c9180a57SEric Paris * than once with different security options. 742c9180a57SEric Paris */ 743c9180a57SEric Paris for (i = 0; i < num_opts; i++) { 744c9180a57SEric Paris u32 sid; 74511689d47SDavid P. Quigley 74612f348b9SEric Paris if (flags[i] == SBLABEL_MNT) 74711689d47SDavid P. Quigley continue; 74844be2f65SRasmus Villemoes rc = security_context_str_to_sid(mount_options[i], &sid, GFP_KERNEL); 749c9180a57SEric Paris if (rc) { 75044be2f65SRasmus Villemoes printk(KERN_WARNING "SELinux: security_context_str_to_sid" 75129b1deb2SLinus Torvalds "(%s) failed for (dev %s, type %s) errno=%d\n", 75229b1deb2SLinus Torvalds mount_options[i], sb->s_id, name, rc); 753c9180a57SEric Paris goto out; 754c9180a57SEric Paris } 755c9180a57SEric Paris switch (flags[i]) { 756c9180a57SEric Paris case FSCONTEXT_MNT: 757c9180a57SEric Paris fscontext_sid = sid; 758c9180a57SEric Paris 759c9180a57SEric Paris if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, 760c9180a57SEric Paris fscontext_sid)) 761c9180a57SEric Paris goto out_double_mount; 762c9180a57SEric Paris 763c9180a57SEric Paris sbsec->flags |= FSCONTEXT_MNT; 764c9180a57SEric Paris break; 765c9180a57SEric Paris case CONTEXT_MNT: 766c9180a57SEric Paris context_sid = sid; 767c9180a57SEric Paris 768c9180a57SEric Paris if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, 769c9180a57SEric Paris context_sid)) 770c9180a57SEric Paris goto out_double_mount; 771c9180a57SEric Paris 772c9180a57SEric Paris sbsec->flags |= CONTEXT_MNT; 773c9180a57SEric Paris break; 774c9180a57SEric Paris case ROOTCONTEXT_MNT: 775c9180a57SEric Paris rootcontext_sid = sid; 776c9180a57SEric Paris 777c9180a57SEric Paris if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, 778c9180a57SEric Paris rootcontext_sid)) 779c9180a57SEric Paris goto out_double_mount; 780c9180a57SEric Paris 781c9180a57SEric Paris sbsec->flags |= ROOTCONTEXT_MNT; 782c9180a57SEric Paris 783c9180a57SEric Paris break; 784c9180a57SEric Paris case DEFCONTEXT_MNT: 785c9180a57SEric Paris defcontext_sid = sid; 786c9180a57SEric Paris 787c9180a57SEric Paris if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, 788c9180a57SEric Paris defcontext_sid)) 789c9180a57SEric Paris goto out_double_mount; 790c9180a57SEric Paris 791c9180a57SEric Paris sbsec->flags |= DEFCONTEXT_MNT; 792c9180a57SEric Paris 793c9180a57SEric Paris break; 794c9180a57SEric Paris default: 795c9180a57SEric Paris rc = -EINVAL; 796c9180a57SEric Paris goto out; 797c9180a57SEric Paris } 798c9180a57SEric Paris } 799c9180a57SEric Paris 8000d90a7ecSDavid P. Quigley if (sbsec->flags & SE_SBINITIALIZED) { 801c9180a57SEric Paris /* previously mounted with options, but not on this attempt? */ 8020d90a7ecSDavid P. Quigley if ((sbsec->flags & SE_MNTMASK) && !num_opts) 803c9180a57SEric Paris goto out_double_mount; 804c9180a57SEric Paris rc = 0; 805c9180a57SEric Paris goto out; 806c9180a57SEric Paris } 807c9180a57SEric Paris 808089be43eSJames Morris if (strcmp(sb->s_type->name, "proc") == 0) 809134509d5SStephen Smalley sbsec->flags |= SE_SBPROC | SE_SBGENFS; 810134509d5SStephen Smalley 8118e014720SStephen Smalley if (!strcmp(sb->s_type->name, "debugfs") || 8128e014720SStephen Smalley !strcmp(sb->s_type->name, "sysfs") || 8138e014720SStephen Smalley !strcmp(sb->s_type->name, "pstore")) 814134509d5SStephen Smalley sbsec->flags |= SE_SBGENFS; 815c9180a57SEric Paris 816eb9ae686SDavid Quigley if (!sbsec->behavior) { 817eb9ae686SDavid Quigley /* 818eb9ae686SDavid Quigley * Determine the labeling behavior to use for this 819eb9ae686SDavid Quigley * filesystem type. 820eb9ae686SDavid Quigley */ 821a64c54cfSEric Paris rc = security_fs_use(sb); 822c9180a57SEric Paris if (rc) { 823eb9ae686SDavid Quigley printk(KERN_WARNING 824eb9ae686SDavid Quigley "%s: security_fs_use(%s) returned %d\n", 825089be43eSJames Morris __func__, sb->s_type->name, rc); 826c9180a57SEric Paris goto out; 827c9180a57SEric Paris } 828eb9ae686SDavid Quigley } 829aad82892SSeth Forshee 830aad82892SSeth Forshee /* 83101593d32SStephen Smalley * If this is a user namespace mount and the filesystem type is not 83201593d32SStephen Smalley * explicitly whitelisted, then no contexts are allowed on the command 83301593d32SStephen Smalley * line and security labels must be ignored. 834aad82892SSeth Forshee */ 83501593d32SStephen Smalley if (sb->s_user_ns != &init_user_ns && 83601593d32SStephen Smalley strcmp(sb->s_type->name, "tmpfs") && 83701593d32SStephen Smalley strcmp(sb->s_type->name, "ramfs") && 83801593d32SStephen Smalley strcmp(sb->s_type->name, "devpts")) { 839aad82892SSeth Forshee if (context_sid || fscontext_sid || rootcontext_sid || 840aad82892SSeth Forshee defcontext_sid) { 841aad82892SSeth Forshee rc = -EACCES; 842aad82892SSeth Forshee goto out; 843aad82892SSeth Forshee } 844aad82892SSeth Forshee if (sbsec->behavior == SECURITY_FS_USE_XATTR) { 845aad82892SSeth Forshee sbsec->behavior = SECURITY_FS_USE_MNTPOINT; 846aad82892SSeth Forshee rc = security_transition_sid(current_sid(), current_sid(), 847aad82892SSeth Forshee SECCLASS_FILE, NULL, 848aad82892SSeth Forshee &sbsec->mntpoint_sid); 849aad82892SSeth Forshee if (rc) 850aad82892SSeth Forshee goto out; 851aad82892SSeth Forshee } 852aad82892SSeth Forshee goto out_set_opts; 853aad82892SSeth Forshee } 854aad82892SSeth Forshee 855c9180a57SEric Paris /* sets the context of the superblock for the fs being mounted. */ 856c9180a57SEric Paris if (fscontext_sid) { 857275bb41eSDavid Howells rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred); 858c9180a57SEric Paris if (rc) 859c9180a57SEric Paris goto out; 860c9180a57SEric Paris 861c9180a57SEric Paris sbsec->sid = fscontext_sid; 862c9180a57SEric Paris } 863c9180a57SEric Paris 864c9180a57SEric Paris /* 865c9180a57SEric Paris * Switch to using mount point labeling behavior. 866c9180a57SEric Paris * sets the label used on all file below the mountpoint, and will set 867c9180a57SEric Paris * the superblock context if not already set. 868c9180a57SEric Paris */ 869eb9ae686SDavid Quigley if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) { 870eb9ae686SDavid Quigley sbsec->behavior = SECURITY_FS_USE_NATIVE; 871eb9ae686SDavid Quigley *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS; 872eb9ae686SDavid Quigley } 873eb9ae686SDavid Quigley 874c9180a57SEric Paris if (context_sid) { 875c9180a57SEric Paris if (!fscontext_sid) { 876275bb41eSDavid Howells rc = may_context_mount_sb_relabel(context_sid, sbsec, 877275bb41eSDavid Howells cred); 878c9180a57SEric Paris if (rc) 879c9180a57SEric Paris goto out; 880c9180a57SEric Paris sbsec->sid = context_sid; 881c9180a57SEric Paris } else { 882275bb41eSDavid Howells rc = may_context_mount_inode_relabel(context_sid, sbsec, 883275bb41eSDavid Howells cred); 884c9180a57SEric Paris if (rc) 885c9180a57SEric Paris goto out; 886c9180a57SEric Paris } 887c9180a57SEric Paris if (!rootcontext_sid) 888c9180a57SEric Paris rootcontext_sid = context_sid; 889c9180a57SEric Paris 890c9180a57SEric Paris sbsec->mntpoint_sid = context_sid; 891c9180a57SEric Paris sbsec->behavior = SECURITY_FS_USE_MNTPOINT; 892c9180a57SEric Paris } 893c9180a57SEric Paris 894c9180a57SEric Paris if (rootcontext_sid) { 895275bb41eSDavid Howells rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec, 896275bb41eSDavid Howells cred); 897c9180a57SEric Paris if (rc) 898c9180a57SEric Paris goto out; 899c9180a57SEric Paris 900c9180a57SEric Paris root_isec->sid = rootcontext_sid; 9016f3be9f5SAndreas Gruenbacher root_isec->initialized = LABEL_INITIALIZED; 902c9180a57SEric Paris } 903c9180a57SEric Paris 904c9180a57SEric Paris if (defcontext_sid) { 905eb9ae686SDavid Quigley if (sbsec->behavior != SECURITY_FS_USE_XATTR && 906eb9ae686SDavid Quigley sbsec->behavior != SECURITY_FS_USE_NATIVE) { 907c9180a57SEric Paris rc = -EINVAL; 908c9180a57SEric Paris printk(KERN_WARNING "SELinux: defcontext option is " 909c9180a57SEric Paris "invalid for this filesystem type\n"); 910c9180a57SEric Paris goto out; 911c9180a57SEric Paris } 912c9180a57SEric Paris 913c9180a57SEric Paris if (defcontext_sid != sbsec->def_sid) { 914c9180a57SEric Paris rc = may_context_mount_inode_relabel(defcontext_sid, 915275bb41eSDavid Howells sbsec, cred); 916c9180a57SEric Paris if (rc) 917c9180a57SEric Paris goto out; 918c9180a57SEric Paris } 919c9180a57SEric Paris 920c9180a57SEric Paris sbsec->def_sid = defcontext_sid; 921c9180a57SEric Paris } 922c9180a57SEric Paris 923aad82892SSeth Forshee out_set_opts: 924c9180a57SEric Paris rc = sb_finish_set_opts(sb); 925c9180a57SEric Paris out: 926bc7e982bSEric Paris mutex_unlock(&sbsec->lock); 9271da177e4SLinus Torvalds return rc; 928c9180a57SEric Paris out_double_mount: 929c9180a57SEric Paris rc = -EINVAL; 930c9180a57SEric Paris printk(KERN_WARNING "SELinux: mount invalid. Same superblock, different " 93129b1deb2SLinus Torvalds "security settings for (dev %s, type %s)\n", sb->s_id, name); 932c9180a57SEric Paris goto out; 933c9180a57SEric Paris } 934c9180a57SEric Paris 935094f7b69SJeff Layton static int selinux_cmp_sb_context(const struct super_block *oldsb, 936094f7b69SJeff Layton const struct super_block *newsb) 937094f7b69SJeff Layton { 938094f7b69SJeff Layton struct superblock_security_struct *old = oldsb->s_security; 939094f7b69SJeff Layton struct superblock_security_struct *new = newsb->s_security; 940094f7b69SJeff Layton char oldflags = old->flags & SE_MNTMASK; 941094f7b69SJeff Layton char newflags = new->flags & SE_MNTMASK; 942094f7b69SJeff Layton 943094f7b69SJeff Layton if (oldflags != newflags) 944094f7b69SJeff Layton goto mismatch; 945094f7b69SJeff Layton if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid) 946094f7b69SJeff Layton goto mismatch; 947094f7b69SJeff Layton if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid) 948094f7b69SJeff Layton goto mismatch; 949094f7b69SJeff Layton if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid) 950094f7b69SJeff Layton goto mismatch; 951094f7b69SJeff Layton if (oldflags & ROOTCONTEXT_MNT) { 95283da53c5SAndreas Gruenbacher struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root); 95383da53c5SAndreas Gruenbacher struct inode_security_struct *newroot = backing_inode_security(newsb->s_root); 954094f7b69SJeff Layton if (oldroot->sid != newroot->sid) 955094f7b69SJeff Layton goto mismatch; 956094f7b69SJeff Layton } 957094f7b69SJeff Layton return 0; 958094f7b69SJeff Layton mismatch: 959094f7b69SJeff Layton printk(KERN_WARNING "SELinux: mount invalid. Same superblock, " 960094f7b69SJeff Layton "different security settings for (dev %s, " 961094f7b69SJeff Layton "type %s)\n", newsb->s_id, newsb->s_type->name); 962094f7b69SJeff Layton return -EBUSY; 963094f7b69SJeff Layton } 964094f7b69SJeff Layton 965094f7b69SJeff Layton static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb, 966c9180a57SEric Paris struct super_block *newsb) 967c9180a57SEric Paris { 968c9180a57SEric Paris const struct superblock_security_struct *oldsbsec = oldsb->s_security; 969c9180a57SEric Paris struct superblock_security_struct *newsbsec = newsb->s_security; 970c9180a57SEric Paris 971c9180a57SEric Paris int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT); 972c9180a57SEric Paris int set_context = (oldsbsec->flags & CONTEXT_MNT); 973c9180a57SEric Paris int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT); 974c9180a57SEric Paris 9750f5e6420SEric Paris /* 9760f5e6420SEric Paris * if the parent was able to be mounted it clearly had no special lsm 977e8c26255SAl Viro * mount options. thus we can safely deal with this superblock later 9780f5e6420SEric Paris */ 979e8c26255SAl Viro if (!ss_initialized) 980094f7b69SJeff Layton return 0; 981c9180a57SEric Paris 982c9180a57SEric Paris /* how can we clone if the old one wasn't set up?? */ 9830d90a7ecSDavid P. Quigley BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED)); 984c9180a57SEric Paris 985094f7b69SJeff Layton /* if fs is reusing a sb, make sure that the contexts match */ 9860d90a7ecSDavid P. Quigley if (newsbsec->flags & SE_SBINITIALIZED) 987094f7b69SJeff Layton return selinux_cmp_sb_context(oldsb, newsb); 9885a552617SEric Paris 989c9180a57SEric Paris mutex_lock(&newsbsec->lock); 990c9180a57SEric Paris 991c9180a57SEric Paris newsbsec->flags = oldsbsec->flags; 992c9180a57SEric Paris 993c9180a57SEric Paris newsbsec->sid = oldsbsec->sid; 994c9180a57SEric Paris newsbsec->def_sid = oldsbsec->def_sid; 995c9180a57SEric Paris newsbsec->behavior = oldsbsec->behavior; 996c9180a57SEric Paris 997c9180a57SEric Paris if (set_context) { 998c9180a57SEric Paris u32 sid = oldsbsec->mntpoint_sid; 999c9180a57SEric Paris 1000c9180a57SEric Paris if (!set_fscontext) 1001c9180a57SEric Paris newsbsec->sid = sid; 1002c9180a57SEric Paris if (!set_rootcontext) { 100383da53c5SAndreas Gruenbacher struct inode_security_struct *newisec = backing_inode_security(newsb->s_root); 1004c9180a57SEric Paris newisec->sid = sid; 1005c9180a57SEric Paris } 1006c9180a57SEric Paris newsbsec->mntpoint_sid = sid; 1007c9180a57SEric Paris } 1008c9180a57SEric Paris if (set_rootcontext) { 100983da53c5SAndreas Gruenbacher const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root); 101083da53c5SAndreas Gruenbacher struct inode_security_struct *newisec = backing_inode_security(newsb->s_root); 1011c9180a57SEric Paris 1012c9180a57SEric Paris newisec->sid = oldisec->sid; 1013c9180a57SEric Paris } 1014c9180a57SEric Paris 1015c9180a57SEric Paris sb_finish_set_opts(newsb); 1016c9180a57SEric Paris mutex_unlock(&newsbsec->lock); 1017094f7b69SJeff Layton return 0; 1018c9180a57SEric Paris } 1019c9180a57SEric Paris 10202e1479d9SAdrian Bunk static int selinux_parse_opts_str(char *options, 10212e1479d9SAdrian Bunk struct security_mnt_opts *opts) 1022c9180a57SEric Paris { 1023e0007529SEric Paris char *p; 1024c9180a57SEric Paris char *context = NULL, *defcontext = NULL; 1025c9180a57SEric Paris char *fscontext = NULL, *rootcontext = NULL; 1026e0007529SEric Paris int rc, num_mnt_opts = 0; 1027c9180a57SEric Paris 1028e0007529SEric Paris opts->num_mnt_opts = 0; 1029c9180a57SEric Paris 1030c9180a57SEric Paris /* Standard string-based options. */ 1031c9180a57SEric Paris while ((p = strsep(&options, "|")) != NULL) { 1032c9180a57SEric Paris int token; 1033c9180a57SEric Paris substring_t args[MAX_OPT_ARGS]; 1034c9180a57SEric Paris 1035c9180a57SEric Paris if (!*p) 1036c9180a57SEric Paris continue; 1037c9180a57SEric Paris 1038c9180a57SEric Paris token = match_token(p, tokens, args); 1039c9180a57SEric Paris 1040c9180a57SEric Paris switch (token) { 1041c9180a57SEric Paris case Opt_context: 1042c9180a57SEric Paris if (context || defcontext) { 1043c9180a57SEric Paris rc = -EINVAL; 1044c9180a57SEric Paris printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 1045c9180a57SEric Paris goto out_err; 1046c9180a57SEric Paris } 1047c9180a57SEric Paris context = match_strdup(&args[0]); 1048c9180a57SEric Paris if (!context) { 1049c9180a57SEric Paris rc = -ENOMEM; 1050c9180a57SEric Paris goto out_err; 1051c9180a57SEric Paris } 1052c9180a57SEric Paris break; 1053c9180a57SEric Paris 1054c9180a57SEric Paris case Opt_fscontext: 1055c9180a57SEric Paris if (fscontext) { 1056c9180a57SEric Paris rc = -EINVAL; 1057c9180a57SEric Paris printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 1058c9180a57SEric Paris goto out_err; 1059c9180a57SEric Paris } 1060c9180a57SEric Paris fscontext = match_strdup(&args[0]); 1061c9180a57SEric Paris if (!fscontext) { 1062c9180a57SEric Paris rc = -ENOMEM; 1063c9180a57SEric Paris goto out_err; 1064c9180a57SEric Paris } 1065c9180a57SEric Paris break; 1066c9180a57SEric Paris 1067c9180a57SEric Paris case Opt_rootcontext: 1068c9180a57SEric Paris if (rootcontext) { 1069c9180a57SEric Paris rc = -EINVAL; 1070c9180a57SEric Paris printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 1071c9180a57SEric Paris goto out_err; 1072c9180a57SEric Paris } 1073c9180a57SEric Paris rootcontext = match_strdup(&args[0]); 1074c9180a57SEric Paris if (!rootcontext) { 1075c9180a57SEric Paris rc = -ENOMEM; 1076c9180a57SEric Paris goto out_err; 1077c9180a57SEric Paris } 1078c9180a57SEric Paris break; 1079c9180a57SEric Paris 1080c9180a57SEric Paris case Opt_defcontext: 1081c9180a57SEric Paris if (context || defcontext) { 1082c9180a57SEric Paris rc = -EINVAL; 1083c9180a57SEric Paris printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 1084c9180a57SEric Paris goto out_err; 1085c9180a57SEric Paris } 1086c9180a57SEric Paris defcontext = match_strdup(&args[0]); 1087c9180a57SEric Paris if (!defcontext) { 1088c9180a57SEric Paris rc = -ENOMEM; 1089c9180a57SEric Paris goto out_err; 1090c9180a57SEric Paris } 1091c9180a57SEric Paris break; 109211689d47SDavid P. Quigley case Opt_labelsupport: 109311689d47SDavid P. Quigley break; 1094c9180a57SEric Paris default: 1095c9180a57SEric Paris rc = -EINVAL; 1096c9180a57SEric Paris printk(KERN_WARNING "SELinux: unknown mount option\n"); 1097c9180a57SEric Paris goto out_err; 1098c9180a57SEric Paris 1099c9180a57SEric Paris } 1100c9180a57SEric Paris } 1101c9180a57SEric Paris 1102e0007529SEric Paris rc = -ENOMEM; 11038931c3bdSTetsuo Handa opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_KERNEL); 1104e0007529SEric Paris if (!opts->mnt_opts) 1105e0007529SEric Paris goto out_err; 1106e0007529SEric Paris 11078931c3bdSTetsuo Handa opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), 11088931c3bdSTetsuo Handa GFP_KERNEL); 1109e0007529SEric Paris if (!opts->mnt_opts_flags) { 1110e0007529SEric Paris kfree(opts->mnt_opts); 1111e0007529SEric Paris goto out_err; 1112c9180a57SEric Paris } 1113c9180a57SEric Paris 1114e0007529SEric Paris if (fscontext) { 1115e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = fscontext; 1116e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT; 1117e0007529SEric Paris } 1118e0007529SEric Paris if (context) { 1119e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = context; 1120e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT; 1121e0007529SEric Paris } 1122e0007529SEric Paris if (rootcontext) { 1123e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = rootcontext; 1124e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT; 1125e0007529SEric Paris } 1126e0007529SEric Paris if (defcontext) { 1127e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = defcontext; 1128e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT; 1129e0007529SEric Paris } 1130e0007529SEric Paris 1131e0007529SEric Paris opts->num_mnt_opts = num_mnt_opts; 1132e0007529SEric Paris return 0; 1133e0007529SEric Paris 1134c9180a57SEric Paris out_err: 1135c9180a57SEric Paris kfree(context); 1136c9180a57SEric Paris kfree(defcontext); 1137c9180a57SEric Paris kfree(fscontext); 1138c9180a57SEric Paris kfree(rootcontext); 1139c9180a57SEric Paris return rc; 11401da177e4SLinus Torvalds } 1141e0007529SEric Paris /* 1142e0007529SEric Paris * string mount options parsing and call set the sbsec 1143e0007529SEric Paris */ 1144e0007529SEric Paris static int superblock_doinit(struct super_block *sb, void *data) 1145e0007529SEric Paris { 1146e0007529SEric Paris int rc = 0; 1147e0007529SEric Paris char *options = data; 1148e0007529SEric Paris struct security_mnt_opts opts; 1149e0007529SEric Paris 1150e0007529SEric Paris security_init_mnt_opts(&opts); 1151e0007529SEric Paris 1152e0007529SEric Paris if (!data) 1153e0007529SEric Paris goto out; 1154e0007529SEric Paris 1155e0007529SEric Paris BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA); 1156e0007529SEric Paris 1157e0007529SEric Paris rc = selinux_parse_opts_str(options, &opts); 1158e0007529SEric Paris if (rc) 1159e0007529SEric Paris goto out_err; 1160e0007529SEric Paris 1161e0007529SEric Paris out: 1162649f6e77SDavid Quigley rc = selinux_set_mnt_opts(sb, &opts, 0, NULL); 1163e0007529SEric Paris 1164e0007529SEric Paris out_err: 1165e0007529SEric Paris security_free_mnt_opts(&opts); 1166e0007529SEric Paris return rc; 1167e0007529SEric Paris } 11681da177e4SLinus Torvalds 11693583a711SAdrian Bunk static void selinux_write_opts(struct seq_file *m, 11703583a711SAdrian Bunk struct security_mnt_opts *opts) 11712069f457SEric Paris { 11722069f457SEric Paris int i; 11732069f457SEric Paris char *prefix; 11742069f457SEric Paris 11752069f457SEric Paris for (i = 0; i < opts->num_mnt_opts; i++) { 117611689d47SDavid P. Quigley char *has_comma; 117711689d47SDavid P. Quigley 117811689d47SDavid P. Quigley if (opts->mnt_opts[i]) 117911689d47SDavid P. Quigley has_comma = strchr(opts->mnt_opts[i], ','); 118011689d47SDavid P. Quigley else 118111689d47SDavid P. Quigley has_comma = NULL; 11822069f457SEric Paris 11832069f457SEric Paris switch (opts->mnt_opts_flags[i]) { 11842069f457SEric Paris case CONTEXT_MNT: 11852069f457SEric Paris prefix = CONTEXT_STR; 11862069f457SEric Paris break; 11872069f457SEric Paris case FSCONTEXT_MNT: 11882069f457SEric Paris prefix = FSCONTEXT_STR; 11892069f457SEric Paris break; 11902069f457SEric Paris case ROOTCONTEXT_MNT: 11912069f457SEric Paris prefix = ROOTCONTEXT_STR; 11922069f457SEric Paris break; 11932069f457SEric Paris case DEFCONTEXT_MNT: 11942069f457SEric Paris prefix = DEFCONTEXT_STR; 11952069f457SEric Paris break; 119612f348b9SEric Paris case SBLABEL_MNT: 119711689d47SDavid P. Quigley seq_putc(m, ','); 119811689d47SDavid P. Quigley seq_puts(m, LABELSUPP_STR); 119911689d47SDavid P. Quigley continue; 12002069f457SEric Paris default: 12012069f457SEric Paris BUG(); 1202a35c6c83SEric Paris return; 12032069f457SEric Paris }; 12042069f457SEric Paris /* we need a comma before each option */ 12052069f457SEric Paris seq_putc(m, ','); 12062069f457SEric Paris seq_puts(m, prefix); 12072069f457SEric Paris if (has_comma) 12082069f457SEric Paris seq_putc(m, '\"'); 1209a068acf2SKees Cook seq_escape(m, opts->mnt_opts[i], "\"\n\\"); 12102069f457SEric Paris if (has_comma) 12112069f457SEric Paris seq_putc(m, '\"'); 12122069f457SEric Paris } 12132069f457SEric Paris } 12142069f457SEric Paris 12152069f457SEric Paris static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb) 12162069f457SEric Paris { 12172069f457SEric Paris struct security_mnt_opts opts; 12182069f457SEric Paris int rc; 12192069f457SEric Paris 12202069f457SEric Paris rc = selinux_get_mnt_opts(sb, &opts); 1221383795c2SEric Paris if (rc) { 1222383795c2SEric Paris /* before policy load we may get EINVAL, don't show anything */ 1223383795c2SEric Paris if (rc == -EINVAL) 1224383795c2SEric Paris rc = 0; 12252069f457SEric Paris return rc; 1226383795c2SEric Paris } 12272069f457SEric Paris 12282069f457SEric Paris selinux_write_opts(m, &opts); 12292069f457SEric Paris 12302069f457SEric Paris security_free_mnt_opts(&opts); 12312069f457SEric Paris 12322069f457SEric Paris return rc; 12332069f457SEric Paris } 12342069f457SEric Paris 12351da177e4SLinus Torvalds static inline u16 inode_mode_to_security_class(umode_t mode) 12361da177e4SLinus Torvalds { 12371da177e4SLinus Torvalds switch (mode & S_IFMT) { 12381da177e4SLinus Torvalds case S_IFSOCK: 12391da177e4SLinus Torvalds return SECCLASS_SOCK_FILE; 12401da177e4SLinus Torvalds case S_IFLNK: 12411da177e4SLinus Torvalds return SECCLASS_LNK_FILE; 12421da177e4SLinus Torvalds case S_IFREG: 12431da177e4SLinus Torvalds return SECCLASS_FILE; 12441da177e4SLinus Torvalds case S_IFBLK: 12451da177e4SLinus Torvalds return SECCLASS_BLK_FILE; 12461da177e4SLinus Torvalds case S_IFDIR: 12471da177e4SLinus Torvalds return SECCLASS_DIR; 12481da177e4SLinus Torvalds case S_IFCHR: 12491da177e4SLinus Torvalds return SECCLASS_CHR_FILE; 12501da177e4SLinus Torvalds case S_IFIFO: 12511da177e4SLinus Torvalds return SECCLASS_FIFO_FILE; 12521da177e4SLinus Torvalds 12531da177e4SLinus Torvalds } 12541da177e4SLinus Torvalds 12551da177e4SLinus Torvalds return SECCLASS_FILE; 12561da177e4SLinus Torvalds } 12571da177e4SLinus Torvalds 125813402580SJames Morris static inline int default_protocol_stream(int protocol) 125913402580SJames Morris { 126013402580SJames Morris return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP); 126113402580SJames Morris } 126213402580SJames Morris 126313402580SJames Morris static inline int default_protocol_dgram(int protocol) 126413402580SJames Morris { 126513402580SJames Morris return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP); 126613402580SJames Morris } 126713402580SJames Morris 12681da177e4SLinus Torvalds static inline u16 socket_type_to_security_class(int family, int type, int protocol) 12691da177e4SLinus Torvalds { 1270da69a530SStephen Smalley int extsockclass = selinux_policycap_extsockclass; 1271da69a530SStephen Smalley 12721da177e4SLinus Torvalds switch (family) { 12731da177e4SLinus Torvalds case PF_UNIX: 12741da177e4SLinus Torvalds switch (type) { 12751da177e4SLinus Torvalds case SOCK_STREAM: 12761da177e4SLinus Torvalds case SOCK_SEQPACKET: 12771da177e4SLinus Torvalds return SECCLASS_UNIX_STREAM_SOCKET; 12781da177e4SLinus Torvalds case SOCK_DGRAM: 12791da177e4SLinus Torvalds return SECCLASS_UNIX_DGRAM_SOCKET; 12801da177e4SLinus Torvalds } 12811da177e4SLinus Torvalds break; 12821da177e4SLinus Torvalds case PF_INET: 12831da177e4SLinus Torvalds case PF_INET6: 12841da177e4SLinus Torvalds switch (type) { 12851da177e4SLinus Torvalds case SOCK_STREAM: 1286da69a530SStephen Smalley case SOCK_SEQPACKET: 128713402580SJames Morris if (default_protocol_stream(protocol)) 12881da177e4SLinus Torvalds return SECCLASS_TCP_SOCKET; 1289da69a530SStephen Smalley else if (extsockclass && protocol == IPPROTO_SCTP) 1290da69a530SStephen Smalley return SECCLASS_SCTP_SOCKET; 129113402580SJames Morris else 129213402580SJames Morris return SECCLASS_RAWIP_SOCKET; 12931da177e4SLinus Torvalds case SOCK_DGRAM: 129413402580SJames Morris if (default_protocol_dgram(protocol)) 12951da177e4SLinus Torvalds return SECCLASS_UDP_SOCKET; 1296ef37979aSStephen Smalley else if (extsockclass && (protocol == IPPROTO_ICMP || 1297ef37979aSStephen Smalley protocol == IPPROTO_ICMPV6)) 1298da69a530SStephen Smalley return SECCLASS_ICMP_SOCKET; 129913402580SJames Morris else 130013402580SJames Morris return SECCLASS_RAWIP_SOCKET; 13012ee92d46SJames Morris case SOCK_DCCP: 13022ee92d46SJames Morris return SECCLASS_DCCP_SOCKET; 130313402580SJames Morris default: 13041da177e4SLinus Torvalds return SECCLASS_RAWIP_SOCKET; 13051da177e4SLinus Torvalds } 13061da177e4SLinus Torvalds break; 13071da177e4SLinus Torvalds case PF_NETLINK: 13081da177e4SLinus Torvalds switch (protocol) { 13091da177e4SLinus Torvalds case NETLINK_ROUTE: 13101da177e4SLinus Torvalds return SECCLASS_NETLINK_ROUTE_SOCKET; 13117f1fb60cSPavel Emelyanov case NETLINK_SOCK_DIAG: 13121da177e4SLinus Torvalds return SECCLASS_NETLINK_TCPDIAG_SOCKET; 13131da177e4SLinus Torvalds case NETLINK_NFLOG: 13141da177e4SLinus Torvalds return SECCLASS_NETLINK_NFLOG_SOCKET; 13151da177e4SLinus Torvalds case NETLINK_XFRM: 13161da177e4SLinus Torvalds return SECCLASS_NETLINK_XFRM_SOCKET; 13171da177e4SLinus Torvalds case NETLINK_SELINUX: 13181da177e4SLinus Torvalds return SECCLASS_NETLINK_SELINUX_SOCKET; 13196c6d2e9bSStephen Smalley case NETLINK_ISCSI: 13206c6d2e9bSStephen Smalley return SECCLASS_NETLINK_ISCSI_SOCKET; 13211da177e4SLinus Torvalds case NETLINK_AUDIT: 13221da177e4SLinus Torvalds return SECCLASS_NETLINK_AUDIT_SOCKET; 13236c6d2e9bSStephen Smalley case NETLINK_FIB_LOOKUP: 13246c6d2e9bSStephen Smalley return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET; 13256c6d2e9bSStephen Smalley case NETLINK_CONNECTOR: 13266c6d2e9bSStephen Smalley return SECCLASS_NETLINK_CONNECTOR_SOCKET; 13276c6d2e9bSStephen Smalley case NETLINK_NETFILTER: 13286c6d2e9bSStephen Smalley return SECCLASS_NETLINK_NETFILTER_SOCKET; 13291da177e4SLinus Torvalds case NETLINK_DNRTMSG: 13301da177e4SLinus Torvalds return SECCLASS_NETLINK_DNRT_SOCKET; 13310c9b7942SJames Morris case NETLINK_KOBJECT_UEVENT: 13320c9b7942SJames Morris return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET; 13336c6d2e9bSStephen Smalley case NETLINK_GENERIC: 13346c6d2e9bSStephen Smalley return SECCLASS_NETLINK_GENERIC_SOCKET; 13356c6d2e9bSStephen Smalley case NETLINK_SCSITRANSPORT: 13366c6d2e9bSStephen Smalley return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET; 13376c6d2e9bSStephen Smalley case NETLINK_RDMA: 13386c6d2e9bSStephen Smalley return SECCLASS_NETLINK_RDMA_SOCKET; 13396c6d2e9bSStephen Smalley case NETLINK_CRYPTO: 13406c6d2e9bSStephen Smalley return SECCLASS_NETLINK_CRYPTO_SOCKET; 13411da177e4SLinus Torvalds default: 13421da177e4SLinus Torvalds return SECCLASS_NETLINK_SOCKET; 13431da177e4SLinus Torvalds } 13441da177e4SLinus Torvalds case PF_PACKET: 13451da177e4SLinus Torvalds return SECCLASS_PACKET_SOCKET; 13461da177e4SLinus Torvalds case PF_KEY: 13471da177e4SLinus Torvalds return SECCLASS_KEY_SOCKET; 13483e3ff15eSChristopher J. PeBenito case PF_APPLETALK: 13493e3ff15eSChristopher J. PeBenito return SECCLASS_APPLETALK_SOCKET; 13501da177e4SLinus Torvalds } 13511da177e4SLinus Torvalds 1352da69a530SStephen Smalley if (extsockclass) { 1353da69a530SStephen Smalley switch (family) { 1354da69a530SStephen Smalley case PF_AX25: 1355da69a530SStephen Smalley return SECCLASS_AX25_SOCKET; 1356da69a530SStephen Smalley case PF_IPX: 1357da69a530SStephen Smalley return SECCLASS_IPX_SOCKET; 1358da69a530SStephen Smalley case PF_NETROM: 1359da69a530SStephen Smalley return SECCLASS_NETROM_SOCKET; 1360da69a530SStephen Smalley case PF_ATMPVC: 1361da69a530SStephen Smalley return SECCLASS_ATMPVC_SOCKET; 1362da69a530SStephen Smalley case PF_X25: 1363da69a530SStephen Smalley return SECCLASS_X25_SOCKET; 1364da69a530SStephen Smalley case PF_ROSE: 1365da69a530SStephen Smalley return SECCLASS_ROSE_SOCKET; 1366da69a530SStephen Smalley case PF_DECnet: 1367da69a530SStephen Smalley return SECCLASS_DECNET_SOCKET; 1368da69a530SStephen Smalley case PF_ATMSVC: 1369da69a530SStephen Smalley return SECCLASS_ATMSVC_SOCKET; 1370da69a530SStephen Smalley case PF_RDS: 1371da69a530SStephen Smalley return SECCLASS_RDS_SOCKET; 1372da69a530SStephen Smalley case PF_IRDA: 1373da69a530SStephen Smalley return SECCLASS_IRDA_SOCKET; 1374da69a530SStephen Smalley case PF_PPPOX: 1375da69a530SStephen Smalley return SECCLASS_PPPOX_SOCKET; 1376da69a530SStephen Smalley case PF_LLC: 1377da69a530SStephen Smalley return SECCLASS_LLC_SOCKET; 1378da69a530SStephen Smalley case PF_CAN: 1379da69a530SStephen Smalley return SECCLASS_CAN_SOCKET; 1380da69a530SStephen Smalley case PF_TIPC: 1381da69a530SStephen Smalley return SECCLASS_TIPC_SOCKET; 1382da69a530SStephen Smalley case PF_BLUETOOTH: 1383da69a530SStephen Smalley return SECCLASS_BLUETOOTH_SOCKET; 1384da69a530SStephen Smalley case PF_IUCV: 1385da69a530SStephen Smalley return SECCLASS_IUCV_SOCKET; 1386da69a530SStephen Smalley case PF_RXRPC: 1387da69a530SStephen Smalley return SECCLASS_RXRPC_SOCKET; 1388da69a530SStephen Smalley case PF_ISDN: 1389da69a530SStephen Smalley return SECCLASS_ISDN_SOCKET; 1390da69a530SStephen Smalley case PF_PHONET: 1391da69a530SStephen Smalley return SECCLASS_PHONET_SOCKET; 1392da69a530SStephen Smalley case PF_IEEE802154: 1393da69a530SStephen Smalley return SECCLASS_IEEE802154_SOCKET; 1394da69a530SStephen Smalley case PF_CAIF: 1395da69a530SStephen Smalley return SECCLASS_CAIF_SOCKET; 1396da69a530SStephen Smalley case PF_ALG: 1397da69a530SStephen Smalley return SECCLASS_ALG_SOCKET; 1398da69a530SStephen Smalley case PF_NFC: 1399da69a530SStephen Smalley return SECCLASS_NFC_SOCKET; 1400da69a530SStephen Smalley case PF_VSOCK: 1401da69a530SStephen Smalley return SECCLASS_VSOCK_SOCKET; 1402da69a530SStephen Smalley case PF_KCM: 1403da69a530SStephen Smalley return SECCLASS_KCM_SOCKET; 1404da69a530SStephen Smalley case PF_QIPCRTR: 1405da69a530SStephen Smalley return SECCLASS_QIPCRTR_SOCKET; 14063051bf36SLinus Torvalds case PF_SMC: 14073051bf36SLinus Torvalds return SECCLASS_SMC_SOCKET; 14083051bf36SLinus Torvalds #if PF_MAX > 44 1409da69a530SStephen Smalley #error New address family defined, please update this function. 1410da69a530SStephen Smalley #endif 1411da69a530SStephen Smalley } 1412da69a530SStephen Smalley } 1413da69a530SStephen Smalley 14141da177e4SLinus Torvalds return SECCLASS_SOCKET; 14151da177e4SLinus Torvalds } 14161da177e4SLinus Torvalds 1417134509d5SStephen Smalley static int selinux_genfs_get_sid(struct dentry *dentry, 14181da177e4SLinus Torvalds u16 tclass, 1419134509d5SStephen Smalley u16 flags, 14201da177e4SLinus Torvalds u32 *sid) 14211da177e4SLinus Torvalds { 14228e6c9693SLucian Adrian Grijincu int rc; 1423fc64005cSAl Viro struct super_block *sb = dentry->d_sb; 14248e6c9693SLucian Adrian Grijincu char *buffer, *path; 14251da177e4SLinus Torvalds 14261da177e4SLinus Torvalds buffer = (char *)__get_free_page(GFP_KERNEL); 14271da177e4SLinus Torvalds if (!buffer) 14281da177e4SLinus Torvalds return -ENOMEM; 14291da177e4SLinus Torvalds 14308e6c9693SLucian Adrian Grijincu path = dentry_path_raw(dentry, buffer, PAGE_SIZE); 14318e6c9693SLucian Adrian Grijincu if (IS_ERR(path)) 14328e6c9693SLucian Adrian Grijincu rc = PTR_ERR(path); 14338e6c9693SLucian Adrian Grijincu else { 1434134509d5SStephen Smalley if (flags & SE_SBPROC) { 14358e6c9693SLucian Adrian Grijincu /* each process gets a /proc/PID/ entry. Strip off the 14368e6c9693SLucian Adrian Grijincu * PID part to get a valid selinux labeling. 14378e6c9693SLucian Adrian Grijincu * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */ 14388e6c9693SLucian Adrian Grijincu while (path[1] >= '0' && path[1] <= '9') { 14398e6c9693SLucian Adrian Grijincu path[1] = '/'; 14408e6c9693SLucian Adrian Grijincu path++; 14411da177e4SLinus Torvalds } 1442134509d5SStephen Smalley } 1443134509d5SStephen Smalley rc = security_genfs_sid(sb->s_type->name, path, tclass, sid); 14448e6c9693SLucian Adrian Grijincu } 14451da177e4SLinus Torvalds free_page((unsigned long)buffer); 14461da177e4SLinus Torvalds return rc; 14471da177e4SLinus Torvalds } 14481da177e4SLinus Torvalds 14491da177e4SLinus Torvalds /* The inode's security attributes must be initialized before first use. */ 14501da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry) 14511da177e4SLinus Torvalds { 14521da177e4SLinus Torvalds struct superblock_security_struct *sbsec = NULL; 14531da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 14549287aed2SAndreas Gruenbacher u32 task_sid, sid = 0; 14559287aed2SAndreas Gruenbacher u16 sclass; 14561da177e4SLinus Torvalds struct dentry *dentry; 14571da177e4SLinus Torvalds #define INITCONTEXTLEN 255 14581da177e4SLinus Torvalds char *context = NULL; 14591da177e4SLinus Torvalds unsigned len = 0; 14601da177e4SLinus Torvalds int rc = 0; 14611da177e4SLinus Torvalds 14626f3be9f5SAndreas Gruenbacher if (isec->initialized == LABEL_INITIALIZED) 146313457d07SAndreas Gruenbacher return 0; 14641da177e4SLinus Torvalds 14659287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 14666f3be9f5SAndreas Gruenbacher if (isec->initialized == LABEL_INITIALIZED) 146723970741SEric Paris goto out_unlock; 14681da177e4SLinus Torvalds 146913457d07SAndreas Gruenbacher if (isec->sclass == SECCLASS_FILE) 147013457d07SAndreas Gruenbacher isec->sclass = inode_mode_to_security_class(inode->i_mode); 147113457d07SAndreas Gruenbacher 14721da177e4SLinus Torvalds sbsec = inode->i_sb->s_security; 14730d90a7ecSDavid P. Quigley if (!(sbsec->flags & SE_SBINITIALIZED)) { 14741da177e4SLinus Torvalds /* Defer initialization until selinux_complete_init, 14751da177e4SLinus Torvalds after the initial policy is loaded and the security 14761da177e4SLinus Torvalds server is ready to handle calls. */ 14771da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 14781da177e4SLinus Torvalds if (list_empty(&isec->list)) 14791da177e4SLinus Torvalds list_add(&isec->list, &sbsec->isec_head); 14801da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 148123970741SEric Paris goto out_unlock; 14821da177e4SLinus Torvalds } 14831da177e4SLinus Torvalds 14849287aed2SAndreas Gruenbacher sclass = isec->sclass; 14859287aed2SAndreas Gruenbacher task_sid = isec->task_sid; 14869287aed2SAndreas Gruenbacher sid = isec->sid; 14879287aed2SAndreas Gruenbacher isec->initialized = LABEL_PENDING; 14889287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 14899287aed2SAndreas Gruenbacher 14901da177e4SLinus Torvalds switch (sbsec->behavior) { 1491eb9ae686SDavid Quigley case SECURITY_FS_USE_NATIVE: 1492eb9ae686SDavid Quigley break; 14931da177e4SLinus Torvalds case SECURITY_FS_USE_XATTR: 14945d6c3191SAndreas Gruenbacher if (!(inode->i_opflags & IOP_XATTR)) { 14959287aed2SAndreas Gruenbacher sid = sbsec->def_sid; 14961da177e4SLinus Torvalds break; 14971da177e4SLinus Torvalds } 14981da177e4SLinus Torvalds /* Need a dentry, since the xattr API requires one. 14991da177e4SLinus Torvalds Life would be simpler if we could just pass the inode. */ 15001da177e4SLinus Torvalds if (opt_dentry) { 15011da177e4SLinus Torvalds /* Called from d_instantiate or d_splice_alias. */ 15021da177e4SLinus Torvalds dentry = dget(opt_dentry); 15031da177e4SLinus Torvalds } else { 15041da177e4SLinus Torvalds /* Called from selinux_complete_init, try to find a dentry. */ 15051da177e4SLinus Torvalds dentry = d_find_alias(inode); 15061da177e4SLinus Torvalds } 15071da177e4SLinus Torvalds if (!dentry) { 1508df7f54c0SEric Paris /* 1509df7f54c0SEric Paris * this is can be hit on boot when a file is accessed 1510df7f54c0SEric Paris * before the policy is loaded. When we load policy we 1511df7f54c0SEric Paris * may find inodes that have no dentry on the 1512df7f54c0SEric Paris * sbsec->isec_head list. No reason to complain as these 1513df7f54c0SEric Paris * will get fixed up the next time we go through 1514df7f54c0SEric Paris * inode_doinit with a dentry, before these inodes could 1515df7f54c0SEric Paris * be used again by userspace. 1516df7f54c0SEric Paris */ 15179287aed2SAndreas Gruenbacher goto out; 15181da177e4SLinus Torvalds } 15191da177e4SLinus Torvalds 15201da177e4SLinus Torvalds len = INITCONTEXTLEN; 15214cb912f1SEric Paris context = kmalloc(len+1, GFP_NOFS); 15221da177e4SLinus Torvalds if (!context) { 15231da177e4SLinus Torvalds rc = -ENOMEM; 15241da177e4SLinus Torvalds dput(dentry); 15259287aed2SAndreas Gruenbacher goto out; 15261da177e4SLinus Torvalds } 15274cb912f1SEric Paris context[len] = '\0'; 15285d6c3191SAndreas Gruenbacher rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len); 15291da177e4SLinus Torvalds if (rc == -ERANGE) { 1530314dabb8SJames Morris kfree(context); 1531314dabb8SJames Morris 15321da177e4SLinus Torvalds /* Need a larger buffer. Query for the right size. */ 15335d6c3191SAndreas Gruenbacher rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0); 15341da177e4SLinus Torvalds if (rc < 0) { 15351da177e4SLinus Torvalds dput(dentry); 15369287aed2SAndreas Gruenbacher goto out; 15371da177e4SLinus Torvalds } 15381da177e4SLinus Torvalds len = rc; 15394cb912f1SEric Paris context = kmalloc(len+1, GFP_NOFS); 15401da177e4SLinus Torvalds if (!context) { 15411da177e4SLinus Torvalds rc = -ENOMEM; 15421da177e4SLinus Torvalds dput(dentry); 15439287aed2SAndreas Gruenbacher goto out; 15441da177e4SLinus Torvalds } 15454cb912f1SEric Paris context[len] = '\0'; 15465d6c3191SAndreas Gruenbacher rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len); 15471da177e4SLinus Torvalds } 15481da177e4SLinus Torvalds dput(dentry); 15491da177e4SLinus Torvalds if (rc < 0) { 15501da177e4SLinus Torvalds if (rc != -ENODATA) { 1551744ba35eSEric Paris printk(KERN_WARNING "SELinux: %s: getxattr returned " 1552dd6f953aSHarvey Harrison "%d for dev=%s ino=%ld\n", __func__, 15531da177e4SLinus Torvalds -rc, inode->i_sb->s_id, inode->i_ino); 15541da177e4SLinus Torvalds kfree(context); 15559287aed2SAndreas Gruenbacher goto out; 15561da177e4SLinus Torvalds } 15571da177e4SLinus Torvalds /* Map ENODATA to the default file SID */ 15581da177e4SLinus Torvalds sid = sbsec->def_sid; 15591da177e4SLinus Torvalds rc = 0; 15601da177e4SLinus Torvalds } else { 1561f5c1d5b2SJames Morris rc = security_context_to_sid_default(context, rc, &sid, 1562869ab514SStephen Smalley sbsec->def_sid, 1563869ab514SStephen Smalley GFP_NOFS); 15641da177e4SLinus Torvalds if (rc) { 15654ba0a8adSEric Paris char *dev = inode->i_sb->s_id; 15664ba0a8adSEric Paris unsigned long ino = inode->i_ino; 15674ba0a8adSEric Paris 15684ba0a8adSEric Paris if (rc == -EINVAL) { 15694ba0a8adSEric Paris if (printk_ratelimit()) 15704ba0a8adSEric Paris printk(KERN_NOTICE "SELinux: inode=%lu on dev=%s was found to have an invalid " 15714ba0a8adSEric Paris "context=%s. This indicates you may need to relabel the inode or the " 15724ba0a8adSEric Paris "filesystem in question.\n", ino, dev, context); 15734ba0a8adSEric Paris } else { 1574744ba35eSEric Paris printk(KERN_WARNING "SELinux: %s: context_to_sid(%s) " 15751da177e4SLinus Torvalds "returned %d for dev=%s ino=%ld\n", 15764ba0a8adSEric Paris __func__, context, -rc, dev, ino); 15774ba0a8adSEric Paris } 15781da177e4SLinus Torvalds kfree(context); 15791da177e4SLinus Torvalds /* Leave with the unlabeled SID */ 15801da177e4SLinus Torvalds rc = 0; 15811da177e4SLinus Torvalds break; 15821da177e4SLinus Torvalds } 15831da177e4SLinus Torvalds } 15841da177e4SLinus Torvalds kfree(context); 15851da177e4SLinus Torvalds break; 15861da177e4SLinus Torvalds case SECURITY_FS_USE_TASK: 15879287aed2SAndreas Gruenbacher sid = task_sid; 15881da177e4SLinus Torvalds break; 15891da177e4SLinus Torvalds case SECURITY_FS_USE_TRANS: 15901da177e4SLinus Torvalds /* Default to the fs SID. */ 15919287aed2SAndreas Gruenbacher sid = sbsec->sid; 15921da177e4SLinus Torvalds 15931da177e4SLinus Torvalds /* Try to obtain a transition SID. */ 15949287aed2SAndreas Gruenbacher rc = security_transition_sid(task_sid, sid, sclass, NULL, &sid); 15951da177e4SLinus Torvalds if (rc) 15969287aed2SAndreas Gruenbacher goto out; 15971da177e4SLinus Torvalds break; 1598c312feb2SEric Paris case SECURITY_FS_USE_MNTPOINT: 15999287aed2SAndreas Gruenbacher sid = sbsec->mntpoint_sid; 1600c312feb2SEric Paris break; 16011da177e4SLinus Torvalds default: 1602c312feb2SEric Paris /* Default to the fs superblock SID. */ 16039287aed2SAndreas Gruenbacher sid = sbsec->sid; 16041da177e4SLinus Torvalds 1605134509d5SStephen Smalley if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) { 1606f64410ecSPaul Moore /* We must have a dentry to determine the label on 1607f64410ecSPaul Moore * procfs inodes */ 1608f64410ecSPaul Moore if (opt_dentry) 1609f64410ecSPaul Moore /* Called from d_instantiate or 1610f64410ecSPaul Moore * d_splice_alias. */ 1611f64410ecSPaul Moore dentry = dget(opt_dentry); 1612f64410ecSPaul Moore else 1613f64410ecSPaul Moore /* Called from selinux_complete_init, try to 1614f64410ecSPaul Moore * find a dentry. */ 1615f64410ecSPaul Moore dentry = d_find_alias(inode); 1616f64410ecSPaul Moore /* 1617f64410ecSPaul Moore * This can be hit on boot when a file is accessed 1618f64410ecSPaul Moore * before the policy is loaded. When we load policy we 1619f64410ecSPaul Moore * may find inodes that have no dentry on the 1620f64410ecSPaul Moore * sbsec->isec_head list. No reason to complain as 1621f64410ecSPaul Moore * these will get fixed up the next time we go through 1622f64410ecSPaul Moore * inode_doinit() with a dentry, before these inodes 1623f64410ecSPaul Moore * could be used again by userspace. 1624f64410ecSPaul Moore */ 1625f64410ecSPaul Moore if (!dentry) 16269287aed2SAndreas Gruenbacher goto out; 16279287aed2SAndreas Gruenbacher rc = selinux_genfs_get_sid(dentry, sclass, 1628134509d5SStephen Smalley sbsec->flags, &sid); 1629f64410ecSPaul Moore dput(dentry); 16301da177e4SLinus Torvalds if (rc) 16319287aed2SAndreas Gruenbacher goto out; 16321da177e4SLinus Torvalds } 16331da177e4SLinus Torvalds break; 16341da177e4SLinus Torvalds } 16351da177e4SLinus Torvalds 16369287aed2SAndreas Gruenbacher out: 16379287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 16389287aed2SAndreas Gruenbacher if (isec->initialized == LABEL_PENDING) { 16399287aed2SAndreas Gruenbacher if (!sid || rc) { 16409287aed2SAndreas Gruenbacher isec->initialized = LABEL_INVALID; 16419287aed2SAndreas Gruenbacher goto out_unlock; 16429287aed2SAndreas Gruenbacher } 16439287aed2SAndreas Gruenbacher 16446f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 16459287aed2SAndreas Gruenbacher isec->sid = sid; 16469287aed2SAndreas Gruenbacher } 16471da177e4SLinus Torvalds 164823970741SEric Paris out_unlock: 16499287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 16501da177e4SLinus Torvalds return rc; 16511da177e4SLinus Torvalds } 16521da177e4SLinus Torvalds 16531da177e4SLinus Torvalds /* Convert a Linux signal to an access vector. */ 16541da177e4SLinus Torvalds static inline u32 signal_to_av(int sig) 16551da177e4SLinus Torvalds { 16561da177e4SLinus Torvalds u32 perm = 0; 16571da177e4SLinus Torvalds 16581da177e4SLinus Torvalds switch (sig) { 16591da177e4SLinus Torvalds case SIGCHLD: 16601da177e4SLinus Torvalds /* Commonly granted from child to parent. */ 16611da177e4SLinus Torvalds perm = PROCESS__SIGCHLD; 16621da177e4SLinus Torvalds break; 16631da177e4SLinus Torvalds case SIGKILL: 16641da177e4SLinus Torvalds /* Cannot be caught or ignored */ 16651da177e4SLinus Torvalds perm = PROCESS__SIGKILL; 16661da177e4SLinus Torvalds break; 16671da177e4SLinus Torvalds case SIGSTOP: 16681da177e4SLinus Torvalds /* Cannot be caught or ignored */ 16691da177e4SLinus Torvalds perm = PROCESS__SIGSTOP; 16701da177e4SLinus Torvalds break; 16711da177e4SLinus Torvalds default: 16721da177e4SLinus Torvalds /* All other signals. */ 16731da177e4SLinus Torvalds perm = PROCESS__SIGNAL; 16741da177e4SLinus Torvalds break; 16751da177e4SLinus Torvalds } 16761da177e4SLinus Torvalds 16771da177e4SLinus Torvalds return perm; 16781da177e4SLinus Torvalds } 16791da177e4SLinus Torvalds 1680b68e418cSStephen Smalley #if CAP_LAST_CAP > 63 1681b68e418cSStephen Smalley #error Fix SELinux to handle capabilities > 63. 1682b68e418cSStephen Smalley #endif 1683b68e418cSStephen Smalley 16841da177e4SLinus Torvalds /* Check whether a task is allowed to use a capability. */ 16856a9de491SEric Paris static int cred_has_capability(const struct cred *cred, 16868e4ff6f2SStephen Smalley int cap, int audit, bool initns) 16871da177e4SLinus Torvalds { 16882bf49690SThomas Liu struct common_audit_data ad; 168906112163SEric Paris struct av_decision avd; 1690b68e418cSStephen Smalley u16 sclass; 16913699c53cSDavid Howells u32 sid = cred_sid(cred); 1692b68e418cSStephen Smalley u32 av = CAP_TO_MASK(cap); 169306112163SEric Paris int rc; 16941da177e4SLinus Torvalds 169550c205f5SEric Paris ad.type = LSM_AUDIT_DATA_CAP; 16961da177e4SLinus Torvalds ad.u.cap = cap; 16971da177e4SLinus Torvalds 1698b68e418cSStephen Smalley switch (CAP_TO_INDEX(cap)) { 1699b68e418cSStephen Smalley case 0: 17008e4ff6f2SStephen Smalley sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS; 1701b68e418cSStephen Smalley break; 1702b68e418cSStephen Smalley case 1: 17038e4ff6f2SStephen Smalley sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS; 1704b68e418cSStephen Smalley break; 1705b68e418cSStephen Smalley default: 1706b68e418cSStephen Smalley printk(KERN_ERR 1707b68e418cSStephen Smalley "SELinux: out of range capability %d\n", cap); 1708b68e418cSStephen Smalley BUG(); 1709a35c6c83SEric Paris return -EINVAL; 1710b68e418cSStephen Smalley } 171106112163SEric Paris 1712275bb41eSDavid Howells rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd); 17139ade0cf4SEric Paris if (audit == SECURITY_CAP_AUDIT) { 17147b20ea25SNeilBrown int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad, 0); 17159ade0cf4SEric Paris if (rc2) 17169ade0cf4SEric Paris return rc2; 17179ade0cf4SEric Paris } 171806112163SEric Paris return rc; 17191da177e4SLinus Torvalds } 17201da177e4SLinus Torvalds 17211da177e4SLinus Torvalds /* Check whether a task has a particular permission to an inode. 17221da177e4SLinus Torvalds The 'adp' parameter is optional and allows other audit 17231da177e4SLinus Torvalds data to be passed (e.g. the dentry). */ 172488e67f3bSDavid Howells static int inode_has_perm(const struct cred *cred, 17251da177e4SLinus Torvalds struct inode *inode, 17261da177e4SLinus Torvalds u32 perms, 172719e49834SLinus Torvalds struct common_audit_data *adp) 17281da177e4SLinus Torvalds { 17291da177e4SLinus Torvalds struct inode_security_struct *isec; 1730275bb41eSDavid Howells u32 sid; 17311da177e4SLinus Torvalds 1732e0e81739SDavid Howells validate_creds(cred); 1733e0e81739SDavid Howells 1734bbaca6c2SStephen Smalley if (unlikely(IS_PRIVATE(inode))) 1735bbaca6c2SStephen Smalley return 0; 1736bbaca6c2SStephen Smalley 173788e67f3bSDavid Howells sid = cred_sid(cred); 17381da177e4SLinus Torvalds isec = inode->i_security; 17391da177e4SLinus Torvalds 174019e49834SLinus Torvalds return avc_has_perm(sid, isec->sid, isec->sclass, perms, adp); 17411da177e4SLinus Torvalds } 17421da177e4SLinus Torvalds 17431da177e4SLinus Torvalds /* Same as inode_has_perm, but pass explicit audit data containing 17441da177e4SLinus Torvalds the dentry to help the auditing code to more easily generate the 17451da177e4SLinus Torvalds pathname if needed. */ 174688e67f3bSDavid Howells static inline int dentry_has_perm(const struct cred *cred, 17471da177e4SLinus Torvalds struct dentry *dentry, 17481da177e4SLinus Torvalds u32 av) 17491da177e4SLinus Torvalds { 1750c6f493d6SDavid Howells struct inode *inode = d_backing_inode(dentry); 17512bf49690SThomas Liu struct common_audit_data ad; 175288e67f3bSDavid Howells 175350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 17542875fa00SEric Paris ad.u.dentry = dentry; 17555d226df4SAndreas Gruenbacher __inode_security_revalidate(inode, dentry, true); 175619e49834SLinus Torvalds return inode_has_perm(cred, inode, av, &ad); 17572875fa00SEric Paris } 17582875fa00SEric Paris 17592875fa00SEric Paris /* Same as inode_has_perm, but pass explicit audit data containing 17602875fa00SEric Paris the path to help the auditing code to more easily generate the 17612875fa00SEric Paris pathname if needed. */ 17622875fa00SEric Paris static inline int path_has_perm(const struct cred *cred, 17633f7036a0SAl Viro const struct path *path, 17642875fa00SEric Paris u32 av) 17652875fa00SEric Paris { 1766c6f493d6SDavid Howells struct inode *inode = d_backing_inode(path->dentry); 17672875fa00SEric Paris struct common_audit_data ad; 17682875fa00SEric Paris 176950c205f5SEric Paris ad.type = LSM_AUDIT_DATA_PATH; 17702875fa00SEric Paris ad.u.path = *path; 17715d226df4SAndreas Gruenbacher __inode_security_revalidate(inode, path->dentry, true); 177219e49834SLinus Torvalds return inode_has_perm(cred, inode, av, &ad); 17731da177e4SLinus Torvalds } 17741da177e4SLinus Torvalds 177513f8e981SDavid Howells /* Same as path_has_perm, but uses the inode from the file struct. */ 177613f8e981SDavid Howells static inline int file_path_has_perm(const struct cred *cred, 177713f8e981SDavid Howells struct file *file, 177813f8e981SDavid Howells u32 av) 177913f8e981SDavid Howells { 178013f8e981SDavid Howells struct common_audit_data ad; 178113f8e981SDavid Howells 178243af5de7SVivek Goyal ad.type = LSM_AUDIT_DATA_FILE; 178343af5de7SVivek Goyal ad.u.file = file; 178419e49834SLinus Torvalds return inode_has_perm(cred, file_inode(file), av, &ad); 178513f8e981SDavid Howells } 178613f8e981SDavid Howells 17871da177e4SLinus Torvalds /* Check whether a task can use an open file descriptor to 17881da177e4SLinus Torvalds access an inode in a given way. Check access to the 17891da177e4SLinus Torvalds descriptor itself, and then use dentry_has_perm to 17901da177e4SLinus Torvalds check a particular permission to the file. 17911da177e4SLinus Torvalds Access to the descriptor is implicitly granted if it 17921da177e4SLinus Torvalds has the same SID as the process. If av is zero, then 17931da177e4SLinus Torvalds access to the file is not checked, e.g. for cases 17941da177e4SLinus Torvalds where only the descriptor is affected like seek. */ 179588e67f3bSDavid Howells static int file_has_perm(const struct cred *cred, 17961da177e4SLinus Torvalds struct file *file, 17971da177e4SLinus Torvalds u32 av) 17981da177e4SLinus Torvalds { 17991da177e4SLinus Torvalds struct file_security_struct *fsec = file->f_security; 1800496ad9aaSAl Viro struct inode *inode = file_inode(file); 18012bf49690SThomas Liu struct common_audit_data ad; 180288e67f3bSDavid Howells u32 sid = cred_sid(cred); 18031da177e4SLinus Torvalds int rc; 18041da177e4SLinus Torvalds 180543af5de7SVivek Goyal ad.type = LSM_AUDIT_DATA_FILE; 180643af5de7SVivek Goyal ad.u.file = file; 18071da177e4SLinus Torvalds 1808275bb41eSDavid Howells if (sid != fsec->sid) { 1809275bb41eSDavid Howells rc = avc_has_perm(sid, fsec->sid, 18101da177e4SLinus Torvalds SECCLASS_FD, 18111da177e4SLinus Torvalds FD__USE, 18121da177e4SLinus Torvalds &ad); 18131da177e4SLinus Torvalds if (rc) 181488e67f3bSDavid Howells goto out; 18151da177e4SLinus Torvalds } 18161da177e4SLinus Torvalds 18171da177e4SLinus Torvalds /* av is zero if only checking access to the descriptor. */ 181888e67f3bSDavid Howells rc = 0; 18191da177e4SLinus Torvalds if (av) 182019e49834SLinus Torvalds rc = inode_has_perm(cred, inode, av, &ad); 18211da177e4SLinus Torvalds 182288e67f3bSDavid Howells out: 182388e67f3bSDavid Howells return rc; 18241da177e4SLinus Torvalds } 18251da177e4SLinus Torvalds 1826c3c188b2SDavid Howells /* 1827c3c188b2SDavid Howells * Determine the label for an inode that might be unioned. 1828c3c188b2SDavid Howells */ 1829c957f6dfSVivek Goyal static int 1830c957f6dfSVivek Goyal selinux_determine_inode_label(const struct task_security_struct *tsec, 1831c957f6dfSVivek Goyal struct inode *dir, 1832c957f6dfSVivek Goyal const struct qstr *name, u16 tclass, 1833c3c188b2SDavid Howells u32 *_new_isid) 1834c3c188b2SDavid Howells { 1835c3c188b2SDavid Howells const struct superblock_security_struct *sbsec = dir->i_sb->s_security; 1836c3c188b2SDavid Howells 1837c3c188b2SDavid Howells if ((sbsec->flags & SE_SBINITIALIZED) && 1838c3c188b2SDavid Howells (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) { 1839c3c188b2SDavid Howells *_new_isid = sbsec->mntpoint_sid; 1840c3c188b2SDavid Howells } else if ((sbsec->flags & SBLABEL_MNT) && 1841c3c188b2SDavid Howells tsec->create_sid) { 1842c3c188b2SDavid Howells *_new_isid = tsec->create_sid; 1843c3c188b2SDavid Howells } else { 184420cdef8dSPaul Moore const struct inode_security_struct *dsec = inode_security(dir); 1845c3c188b2SDavid Howells return security_transition_sid(tsec->sid, dsec->sid, tclass, 1846c3c188b2SDavid Howells name, _new_isid); 1847c3c188b2SDavid Howells } 1848c3c188b2SDavid Howells 1849c3c188b2SDavid Howells return 0; 1850c3c188b2SDavid Howells } 1851c3c188b2SDavid Howells 18521da177e4SLinus Torvalds /* Check whether a task can create a file. */ 18531da177e4SLinus Torvalds static int may_create(struct inode *dir, 18541da177e4SLinus Torvalds struct dentry *dentry, 18551da177e4SLinus Torvalds u16 tclass) 18561da177e4SLinus Torvalds { 18575fb49870SPaul Moore const struct task_security_struct *tsec = current_security(); 18581da177e4SLinus Torvalds struct inode_security_struct *dsec; 18591da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 1860275bb41eSDavid Howells u32 sid, newsid; 18612bf49690SThomas Liu struct common_audit_data ad; 18621da177e4SLinus Torvalds int rc; 18631da177e4SLinus Torvalds 186483da53c5SAndreas Gruenbacher dsec = inode_security(dir); 18651da177e4SLinus Torvalds sbsec = dir->i_sb->s_security; 18661da177e4SLinus Torvalds 1867275bb41eSDavid Howells sid = tsec->sid; 1868275bb41eSDavid Howells 186950c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 1870a269434dSEric Paris ad.u.dentry = dentry; 18711da177e4SLinus Torvalds 1872275bb41eSDavid Howells rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, 18731da177e4SLinus Torvalds DIR__ADD_NAME | DIR__SEARCH, 18741da177e4SLinus Torvalds &ad); 18751da177e4SLinus Torvalds if (rc) 18761da177e4SLinus Torvalds return rc; 18771da177e4SLinus Torvalds 1878c957f6dfSVivek Goyal rc = selinux_determine_inode_label(current_security(), dir, 1879c957f6dfSVivek Goyal &dentry->d_name, tclass, &newsid); 18801da177e4SLinus Torvalds if (rc) 18811da177e4SLinus Torvalds return rc; 18821da177e4SLinus Torvalds 1883275bb41eSDavid Howells rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad); 18841da177e4SLinus Torvalds if (rc) 18851da177e4SLinus Torvalds return rc; 18861da177e4SLinus Torvalds 18871da177e4SLinus Torvalds return avc_has_perm(newsid, sbsec->sid, 18881da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 18891da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, &ad); 18901da177e4SLinus Torvalds } 18911da177e4SLinus Torvalds 18921da177e4SLinus Torvalds #define MAY_LINK 0 18931da177e4SLinus Torvalds #define MAY_UNLINK 1 18941da177e4SLinus Torvalds #define MAY_RMDIR 2 18951da177e4SLinus Torvalds 18961da177e4SLinus Torvalds /* Check whether a task can link, unlink, or rmdir a file/directory. */ 18971da177e4SLinus Torvalds static int may_link(struct inode *dir, 18981da177e4SLinus Torvalds struct dentry *dentry, 18991da177e4SLinus Torvalds int kind) 19001da177e4SLinus Torvalds 19011da177e4SLinus Torvalds { 19021da177e4SLinus Torvalds struct inode_security_struct *dsec, *isec; 19032bf49690SThomas Liu struct common_audit_data ad; 1904275bb41eSDavid Howells u32 sid = current_sid(); 19051da177e4SLinus Torvalds u32 av; 19061da177e4SLinus Torvalds int rc; 19071da177e4SLinus Torvalds 190883da53c5SAndreas Gruenbacher dsec = inode_security(dir); 190983da53c5SAndreas Gruenbacher isec = backing_inode_security(dentry); 19101da177e4SLinus Torvalds 191150c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 1912a269434dSEric Paris ad.u.dentry = dentry; 19131da177e4SLinus Torvalds 19141da177e4SLinus Torvalds av = DIR__SEARCH; 19151da177e4SLinus Torvalds av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME); 1916275bb41eSDavid Howells rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad); 19171da177e4SLinus Torvalds if (rc) 19181da177e4SLinus Torvalds return rc; 19191da177e4SLinus Torvalds 19201da177e4SLinus Torvalds switch (kind) { 19211da177e4SLinus Torvalds case MAY_LINK: 19221da177e4SLinus Torvalds av = FILE__LINK; 19231da177e4SLinus Torvalds break; 19241da177e4SLinus Torvalds case MAY_UNLINK: 19251da177e4SLinus Torvalds av = FILE__UNLINK; 19261da177e4SLinus Torvalds break; 19271da177e4SLinus Torvalds case MAY_RMDIR: 19281da177e4SLinus Torvalds av = DIR__RMDIR; 19291da177e4SLinus Torvalds break; 19301da177e4SLinus Torvalds default: 1931744ba35eSEric Paris printk(KERN_WARNING "SELinux: %s: unrecognized kind %d\n", 1932744ba35eSEric Paris __func__, kind); 19331da177e4SLinus Torvalds return 0; 19341da177e4SLinus Torvalds } 19351da177e4SLinus Torvalds 1936275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad); 19371da177e4SLinus Torvalds return rc; 19381da177e4SLinus Torvalds } 19391da177e4SLinus Torvalds 19401da177e4SLinus Torvalds static inline int may_rename(struct inode *old_dir, 19411da177e4SLinus Torvalds struct dentry *old_dentry, 19421da177e4SLinus Torvalds struct inode *new_dir, 19431da177e4SLinus Torvalds struct dentry *new_dentry) 19441da177e4SLinus Torvalds { 19451da177e4SLinus Torvalds struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec; 19462bf49690SThomas Liu struct common_audit_data ad; 1947275bb41eSDavid Howells u32 sid = current_sid(); 19481da177e4SLinus Torvalds u32 av; 19491da177e4SLinus Torvalds int old_is_dir, new_is_dir; 19501da177e4SLinus Torvalds int rc; 19511da177e4SLinus Torvalds 195283da53c5SAndreas Gruenbacher old_dsec = inode_security(old_dir); 195383da53c5SAndreas Gruenbacher old_isec = backing_inode_security(old_dentry); 1954e36cb0b8SDavid Howells old_is_dir = d_is_dir(old_dentry); 195583da53c5SAndreas Gruenbacher new_dsec = inode_security(new_dir); 19561da177e4SLinus Torvalds 195750c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 19581da177e4SLinus Torvalds 1959a269434dSEric Paris ad.u.dentry = old_dentry; 1960275bb41eSDavid Howells rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR, 19611da177e4SLinus Torvalds DIR__REMOVE_NAME | DIR__SEARCH, &ad); 19621da177e4SLinus Torvalds if (rc) 19631da177e4SLinus Torvalds return rc; 1964275bb41eSDavid Howells rc = avc_has_perm(sid, old_isec->sid, 19651da177e4SLinus Torvalds old_isec->sclass, FILE__RENAME, &ad); 19661da177e4SLinus Torvalds if (rc) 19671da177e4SLinus Torvalds return rc; 19681da177e4SLinus Torvalds if (old_is_dir && new_dir != old_dir) { 1969275bb41eSDavid Howells rc = avc_has_perm(sid, old_isec->sid, 19701da177e4SLinus Torvalds old_isec->sclass, DIR__REPARENT, &ad); 19711da177e4SLinus Torvalds if (rc) 19721da177e4SLinus Torvalds return rc; 19731da177e4SLinus Torvalds } 19741da177e4SLinus Torvalds 1975a269434dSEric Paris ad.u.dentry = new_dentry; 19761da177e4SLinus Torvalds av = DIR__ADD_NAME | DIR__SEARCH; 19772c616d4dSDavid Howells if (d_is_positive(new_dentry)) 19781da177e4SLinus Torvalds av |= DIR__REMOVE_NAME; 1979275bb41eSDavid Howells rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad); 19801da177e4SLinus Torvalds if (rc) 19811da177e4SLinus Torvalds return rc; 19822c616d4dSDavid Howells if (d_is_positive(new_dentry)) { 198383da53c5SAndreas Gruenbacher new_isec = backing_inode_security(new_dentry); 1984e36cb0b8SDavid Howells new_is_dir = d_is_dir(new_dentry); 1985275bb41eSDavid Howells rc = avc_has_perm(sid, new_isec->sid, 19861da177e4SLinus Torvalds new_isec->sclass, 19871da177e4SLinus Torvalds (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad); 19881da177e4SLinus Torvalds if (rc) 19891da177e4SLinus Torvalds return rc; 19901da177e4SLinus Torvalds } 19911da177e4SLinus Torvalds 19921da177e4SLinus Torvalds return 0; 19931da177e4SLinus Torvalds } 19941da177e4SLinus Torvalds 19951da177e4SLinus Torvalds /* Check whether a task can perform a filesystem operation. */ 199688e67f3bSDavid Howells static int superblock_has_perm(const struct cred *cred, 19971da177e4SLinus Torvalds struct super_block *sb, 19981da177e4SLinus Torvalds u32 perms, 19992bf49690SThomas Liu struct common_audit_data *ad) 20001da177e4SLinus Torvalds { 20011da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 200288e67f3bSDavid Howells u32 sid = cred_sid(cred); 20031da177e4SLinus Torvalds 20041da177e4SLinus Torvalds sbsec = sb->s_security; 2005275bb41eSDavid Howells return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad); 20061da177e4SLinus Torvalds } 20071da177e4SLinus Torvalds 20081da177e4SLinus Torvalds /* Convert a Linux mode and permission mask to an access vector. */ 20091da177e4SLinus Torvalds static inline u32 file_mask_to_av(int mode, int mask) 20101da177e4SLinus Torvalds { 20111da177e4SLinus Torvalds u32 av = 0; 20121da177e4SLinus Torvalds 2013dba19c60SAl Viro if (!S_ISDIR(mode)) { 20141da177e4SLinus Torvalds if (mask & MAY_EXEC) 20151da177e4SLinus Torvalds av |= FILE__EXECUTE; 20161da177e4SLinus Torvalds if (mask & MAY_READ) 20171da177e4SLinus Torvalds av |= FILE__READ; 20181da177e4SLinus Torvalds 20191da177e4SLinus Torvalds if (mask & MAY_APPEND) 20201da177e4SLinus Torvalds av |= FILE__APPEND; 20211da177e4SLinus Torvalds else if (mask & MAY_WRITE) 20221da177e4SLinus Torvalds av |= FILE__WRITE; 20231da177e4SLinus Torvalds 20241da177e4SLinus Torvalds } else { 20251da177e4SLinus Torvalds if (mask & MAY_EXEC) 20261da177e4SLinus Torvalds av |= DIR__SEARCH; 20271da177e4SLinus Torvalds if (mask & MAY_WRITE) 20281da177e4SLinus Torvalds av |= DIR__WRITE; 20291da177e4SLinus Torvalds if (mask & MAY_READ) 20301da177e4SLinus Torvalds av |= DIR__READ; 20311da177e4SLinus Torvalds } 20321da177e4SLinus Torvalds 20331da177e4SLinus Torvalds return av; 20341da177e4SLinus Torvalds } 20351da177e4SLinus Torvalds 20361da177e4SLinus Torvalds /* Convert a Linux file to an access vector. */ 20371da177e4SLinus Torvalds static inline u32 file_to_av(struct file *file) 20381da177e4SLinus Torvalds { 20391da177e4SLinus Torvalds u32 av = 0; 20401da177e4SLinus Torvalds 20411da177e4SLinus Torvalds if (file->f_mode & FMODE_READ) 20421da177e4SLinus Torvalds av |= FILE__READ; 20431da177e4SLinus Torvalds if (file->f_mode & FMODE_WRITE) { 20441da177e4SLinus Torvalds if (file->f_flags & O_APPEND) 20451da177e4SLinus Torvalds av |= FILE__APPEND; 20461da177e4SLinus Torvalds else 20471da177e4SLinus Torvalds av |= FILE__WRITE; 20481da177e4SLinus Torvalds } 20490794c66dSStephen Smalley if (!av) { 20500794c66dSStephen Smalley /* 20510794c66dSStephen Smalley * Special file opened with flags 3 for ioctl-only use. 20520794c66dSStephen Smalley */ 20530794c66dSStephen Smalley av = FILE__IOCTL; 20540794c66dSStephen Smalley } 20551da177e4SLinus Torvalds 20561da177e4SLinus Torvalds return av; 20571da177e4SLinus Torvalds } 20581da177e4SLinus Torvalds 20598b6a5a37SEric Paris /* 20608b6a5a37SEric Paris * Convert a file to an access vector and include the correct open 20618b6a5a37SEric Paris * open permission. 20628b6a5a37SEric Paris */ 20638b6a5a37SEric Paris static inline u32 open_file_to_av(struct file *file) 20648b6a5a37SEric Paris { 20658b6a5a37SEric Paris u32 av = file_to_av(file); 2066*ccb54478SStephen Smalley struct inode *inode = file_inode(file); 20678b6a5a37SEric Paris 2068*ccb54478SStephen Smalley if (selinux_policycap_openperm && inode->i_sb->s_magic != SOCKFS_MAGIC) 20698b6a5a37SEric Paris av |= FILE__OPEN; 207049b7b8deSEric Paris 20718b6a5a37SEric Paris return av; 20728b6a5a37SEric Paris } 20738b6a5a37SEric Paris 20741da177e4SLinus Torvalds /* Hook functions begin here. */ 20751da177e4SLinus Torvalds 207679af7307SStephen Smalley static int selinux_binder_set_context_mgr(struct task_struct *mgr) 207779af7307SStephen Smalley { 207879af7307SStephen Smalley u32 mysid = current_sid(); 207979af7307SStephen Smalley u32 mgrsid = task_sid(mgr); 208079af7307SStephen Smalley 208179af7307SStephen Smalley return avc_has_perm(mysid, mgrsid, SECCLASS_BINDER, 208279af7307SStephen Smalley BINDER__SET_CONTEXT_MGR, NULL); 208379af7307SStephen Smalley } 208479af7307SStephen Smalley 208579af7307SStephen Smalley static int selinux_binder_transaction(struct task_struct *from, 208679af7307SStephen Smalley struct task_struct *to) 208779af7307SStephen Smalley { 208879af7307SStephen Smalley u32 mysid = current_sid(); 208979af7307SStephen Smalley u32 fromsid = task_sid(from); 209079af7307SStephen Smalley u32 tosid = task_sid(to); 209179af7307SStephen Smalley int rc; 209279af7307SStephen Smalley 209379af7307SStephen Smalley if (mysid != fromsid) { 209479af7307SStephen Smalley rc = avc_has_perm(mysid, fromsid, SECCLASS_BINDER, 209579af7307SStephen Smalley BINDER__IMPERSONATE, NULL); 209679af7307SStephen Smalley if (rc) 209779af7307SStephen Smalley return rc; 209879af7307SStephen Smalley } 209979af7307SStephen Smalley 210079af7307SStephen Smalley return avc_has_perm(fromsid, tosid, SECCLASS_BINDER, BINDER__CALL, 210179af7307SStephen Smalley NULL); 210279af7307SStephen Smalley } 210379af7307SStephen Smalley 210479af7307SStephen Smalley static int selinux_binder_transfer_binder(struct task_struct *from, 210579af7307SStephen Smalley struct task_struct *to) 210679af7307SStephen Smalley { 210779af7307SStephen Smalley u32 fromsid = task_sid(from); 210879af7307SStephen Smalley u32 tosid = task_sid(to); 210979af7307SStephen Smalley 211079af7307SStephen Smalley return avc_has_perm(fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER, 211179af7307SStephen Smalley NULL); 211279af7307SStephen Smalley } 211379af7307SStephen Smalley 211479af7307SStephen Smalley static int selinux_binder_transfer_file(struct task_struct *from, 211579af7307SStephen Smalley struct task_struct *to, 211679af7307SStephen Smalley struct file *file) 211779af7307SStephen Smalley { 211879af7307SStephen Smalley u32 sid = task_sid(to); 211979af7307SStephen Smalley struct file_security_struct *fsec = file->f_security; 212083da53c5SAndreas Gruenbacher struct dentry *dentry = file->f_path.dentry; 212120cdef8dSPaul Moore struct inode_security_struct *isec; 212279af7307SStephen Smalley struct common_audit_data ad; 212379af7307SStephen Smalley int rc; 212479af7307SStephen Smalley 212579af7307SStephen Smalley ad.type = LSM_AUDIT_DATA_PATH; 212679af7307SStephen Smalley ad.u.path = file->f_path; 212779af7307SStephen Smalley 212879af7307SStephen Smalley if (sid != fsec->sid) { 212979af7307SStephen Smalley rc = avc_has_perm(sid, fsec->sid, 213079af7307SStephen Smalley SECCLASS_FD, 213179af7307SStephen Smalley FD__USE, 213279af7307SStephen Smalley &ad); 213379af7307SStephen Smalley if (rc) 213479af7307SStephen Smalley return rc; 213579af7307SStephen Smalley } 213679af7307SStephen Smalley 213783da53c5SAndreas Gruenbacher if (unlikely(IS_PRIVATE(d_backing_inode(dentry)))) 213879af7307SStephen Smalley return 0; 213979af7307SStephen Smalley 214020cdef8dSPaul Moore isec = backing_inode_security(dentry); 214179af7307SStephen Smalley return avc_has_perm(sid, isec->sid, isec->sclass, file_to_av(file), 214279af7307SStephen Smalley &ad); 214379af7307SStephen Smalley } 214479af7307SStephen Smalley 21459e48858fSIngo Molnar static int selinux_ptrace_access_check(struct task_struct *child, 2146006ebb40SStephen Smalley unsigned int mode) 21471da177e4SLinus Torvalds { 2148275bb41eSDavid Howells u32 sid = current_sid(); 2149275bb41eSDavid Howells u32 csid = task_sid(child); 2150006ebb40SStephen Smalley 2151be0554c9SStephen Smalley if (mode & PTRACE_MODE_READ) 2152be0554c9SStephen Smalley return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, NULL); 2153be0554c9SStephen Smalley 2154be0554c9SStephen Smalley return avc_has_perm(sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL); 21555cd9c58fSDavid Howells } 21565cd9c58fSDavid Howells 21575cd9c58fSDavid Howells static int selinux_ptrace_traceme(struct task_struct *parent) 21585cd9c58fSDavid Howells { 2159be0554c9SStephen Smalley return avc_has_perm(task_sid(parent), current_sid(), SECCLASS_PROCESS, 2160be0554c9SStephen Smalley PROCESS__PTRACE, NULL); 21611da177e4SLinus Torvalds } 21621da177e4SLinus Torvalds 21631da177e4SLinus Torvalds static int selinux_capget(struct task_struct *target, kernel_cap_t *effective, 21641da177e4SLinus Torvalds kernel_cap_t *inheritable, kernel_cap_t *permitted) 21651da177e4SLinus Torvalds { 2166be0554c9SStephen Smalley return avc_has_perm(current_sid(), task_sid(target), SECCLASS_PROCESS, 2167be0554c9SStephen Smalley PROCESS__GETCAP, NULL); 21681da177e4SLinus Torvalds } 21691da177e4SLinus Torvalds 2170d84f4f99SDavid Howells static int selinux_capset(struct cred *new, const struct cred *old, 2171d84f4f99SDavid Howells const kernel_cap_t *effective, 217215a2460eSDavid Howells const kernel_cap_t *inheritable, 217315a2460eSDavid Howells const kernel_cap_t *permitted) 21741da177e4SLinus Torvalds { 2175be0554c9SStephen Smalley return avc_has_perm(cred_sid(old), cred_sid(new), SECCLASS_PROCESS, 2176be0554c9SStephen Smalley PROCESS__SETCAP, NULL); 21771da177e4SLinus Torvalds } 21781da177e4SLinus Torvalds 21795626d3e8SJames Morris /* 21805626d3e8SJames Morris * (This comment used to live with the selinux_task_setuid hook, 21815626d3e8SJames Morris * which was removed). 21825626d3e8SJames Morris * 21835626d3e8SJames Morris * Since setuid only affects the current process, and since the SELinux 21845626d3e8SJames Morris * controls are not based on the Linux identity attributes, SELinux does not 21855626d3e8SJames Morris * need to control this operation. However, SELinux does control the use of 21865626d3e8SJames Morris * the CAP_SETUID and CAP_SETGID capabilities using the capable hook. 21875626d3e8SJames Morris */ 21885626d3e8SJames Morris 21896a9de491SEric Paris static int selinux_capable(const struct cred *cred, struct user_namespace *ns, 21906a9de491SEric Paris int cap, int audit) 21911da177e4SLinus Torvalds { 21928e4ff6f2SStephen Smalley return cred_has_capability(cred, cap, audit, ns == &init_user_ns); 21931da177e4SLinus Torvalds } 21941da177e4SLinus Torvalds 21951da177e4SLinus Torvalds static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb) 21961da177e4SLinus Torvalds { 219788e67f3bSDavid Howells const struct cred *cred = current_cred(); 21981da177e4SLinus Torvalds int rc = 0; 21991da177e4SLinus Torvalds 22001da177e4SLinus Torvalds if (!sb) 22011da177e4SLinus Torvalds return 0; 22021da177e4SLinus Torvalds 22031da177e4SLinus Torvalds switch (cmds) { 22041da177e4SLinus Torvalds case Q_SYNC: 22051da177e4SLinus Torvalds case Q_QUOTAON: 22061da177e4SLinus Torvalds case Q_QUOTAOFF: 22071da177e4SLinus Torvalds case Q_SETINFO: 22081da177e4SLinus Torvalds case Q_SETQUOTA: 220988e67f3bSDavid Howells rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL); 22101da177e4SLinus Torvalds break; 22111da177e4SLinus Torvalds case Q_GETFMT: 22121da177e4SLinus Torvalds case Q_GETINFO: 22131da177e4SLinus Torvalds case Q_GETQUOTA: 221488e67f3bSDavid Howells rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL); 22151da177e4SLinus Torvalds break; 22161da177e4SLinus Torvalds default: 22171da177e4SLinus Torvalds rc = 0; /* let the kernel handle invalid cmds */ 22181da177e4SLinus Torvalds break; 22191da177e4SLinus Torvalds } 22201da177e4SLinus Torvalds return rc; 22211da177e4SLinus Torvalds } 22221da177e4SLinus Torvalds 22231da177e4SLinus Torvalds static int selinux_quota_on(struct dentry *dentry) 22241da177e4SLinus Torvalds { 222588e67f3bSDavid Howells const struct cred *cred = current_cred(); 222688e67f3bSDavid Howells 22272875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__QUOTAON); 22281da177e4SLinus Torvalds } 22291da177e4SLinus Torvalds 223012b3052cSEric Paris static int selinux_syslog(int type) 22311da177e4SLinus Torvalds { 22321da177e4SLinus Torvalds switch (type) { 2233d78ca3cdSKees Cook case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */ 2234d78ca3cdSKees Cook case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */ 2235be0554c9SStephen Smalley return avc_has_perm(current_sid(), SECINITSID_KERNEL, 2236be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL); 2237d78ca3cdSKees Cook case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */ 2238d78ca3cdSKees Cook case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */ 2239d78ca3cdSKees Cook /* Set level of messages printed to console */ 2240d78ca3cdSKees Cook case SYSLOG_ACTION_CONSOLE_LEVEL: 2241be0554c9SStephen Smalley return avc_has_perm(current_sid(), SECINITSID_KERNEL, 2242be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE, 2243be0554c9SStephen Smalley NULL); 22441da177e4SLinus Torvalds } 2245be0554c9SStephen Smalley /* All other syslog types */ 2246be0554c9SStephen Smalley return avc_has_perm(current_sid(), SECINITSID_KERNEL, 2247be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL); 22481da177e4SLinus Torvalds } 22491da177e4SLinus Torvalds 22501da177e4SLinus Torvalds /* 22511da177e4SLinus Torvalds * Check that a process has enough memory to allocate a new virtual 22521da177e4SLinus Torvalds * mapping. 0 means there is enough memory for the allocation to 22531da177e4SLinus Torvalds * succeed and -ENOMEM implies there is not. 22541da177e4SLinus Torvalds * 22551da177e4SLinus Torvalds * Do not audit the selinux permission check, as this is applied to all 22561da177e4SLinus Torvalds * processes that allocate mappings. 22571da177e4SLinus Torvalds */ 225834b4e4aaSAlan Cox static int selinux_vm_enough_memory(struct mm_struct *mm, long pages) 22591da177e4SLinus Torvalds { 22601da177e4SLinus Torvalds int rc, cap_sys_admin = 0; 22611da177e4SLinus Torvalds 2262b1d9e6b0SCasey Schaufler rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN, 22638e4ff6f2SStephen Smalley SECURITY_CAP_NOAUDIT, true); 22641da177e4SLinus Torvalds if (rc == 0) 22651da177e4SLinus Torvalds cap_sys_admin = 1; 22661da177e4SLinus Torvalds 2267b1d9e6b0SCasey Schaufler return cap_sys_admin; 22681da177e4SLinus Torvalds } 22691da177e4SLinus Torvalds 22701da177e4SLinus Torvalds /* binprm security operations */ 22711da177e4SLinus Torvalds 2272be0554c9SStephen Smalley static u32 ptrace_parent_sid(void) 22730c6181cbSPaul Moore { 22740c6181cbSPaul Moore u32 sid = 0; 22750c6181cbSPaul Moore struct task_struct *tracer; 22760c6181cbSPaul Moore 22770c6181cbSPaul Moore rcu_read_lock(); 2278be0554c9SStephen Smalley tracer = ptrace_parent(current); 22790c6181cbSPaul Moore if (tracer) 22800c6181cbSPaul Moore sid = task_sid(tracer); 22810c6181cbSPaul Moore rcu_read_unlock(); 22820c6181cbSPaul Moore 22830c6181cbSPaul Moore return sid; 22840c6181cbSPaul Moore } 22850c6181cbSPaul Moore 22867b0d0b40SStephen Smalley static int check_nnp_nosuid(const struct linux_binprm *bprm, 22877b0d0b40SStephen Smalley const struct task_security_struct *old_tsec, 22887b0d0b40SStephen Smalley const struct task_security_struct *new_tsec) 22897b0d0b40SStephen Smalley { 22907b0d0b40SStephen Smalley int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS); 2291380cf5baSAndy Lutomirski int nosuid = !mnt_may_suid(bprm->file->f_path.mnt); 22927b0d0b40SStephen Smalley int rc; 22937b0d0b40SStephen Smalley 22947b0d0b40SStephen Smalley if (!nnp && !nosuid) 22957b0d0b40SStephen Smalley return 0; /* neither NNP nor nosuid */ 22967b0d0b40SStephen Smalley 22977b0d0b40SStephen Smalley if (new_tsec->sid == old_tsec->sid) 22987b0d0b40SStephen Smalley return 0; /* No change in credentials */ 22997b0d0b40SStephen Smalley 23007b0d0b40SStephen Smalley /* 23017b0d0b40SStephen Smalley * The only transitions we permit under NNP or nosuid 23027b0d0b40SStephen Smalley * are transitions to bounded SIDs, i.e. SIDs that are 23037b0d0b40SStephen Smalley * guaranteed to only be allowed a subset of the permissions 23047b0d0b40SStephen Smalley * of the current SID. 23057b0d0b40SStephen Smalley */ 23067b0d0b40SStephen Smalley rc = security_bounded_transition(old_tsec->sid, new_tsec->sid); 23077b0d0b40SStephen Smalley if (rc) { 23087b0d0b40SStephen Smalley /* 23097b0d0b40SStephen Smalley * On failure, preserve the errno values for NNP vs nosuid. 23107b0d0b40SStephen Smalley * NNP: Operation not permitted for caller. 23117b0d0b40SStephen Smalley * nosuid: Permission denied to file. 23127b0d0b40SStephen Smalley */ 23137b0d0b40SStephen Smalley if (nnp) 23147b0d0b40SStephen Smalley return -EPERM; 23157b0d0b40SStephen Smalley else 23167b0d0b40SStephen Smalley return -EACCES; 23177b0d0b40SStephen Smalley } 23187b0d0b40SStephen Smalley return 0; 23197b0d0b40SStephen Smalley } 23207b0d0b40SStephen Smalley 2321a6f76f23SDavid Howells static int selinux_bprm_set_creds(struct linux_binprm *bprm) 23221da177e4SLinus Torvalds { 2323a6f76f23SDavid Howells const struct task_security_struct *old_tsec; 2324a6f76f23SDavid Howells struct task_security_struct *new_tsec; 23251da177e4SLinus Torvalds struct inode_security_struct *isec; 23262bf49690SThomas Liu struct common_audit_data ad; 2327496ad9aaSAl Viro struct inode *inode = file_inode(bprm->file); 23281da177e4SLinus Torvalds int rc; 23291da177e4SLinus Torvalds 2330a6f76f23SDavid Howells /* SELinux context only depends on initial program or script and not 2331a6f76f23SDavid Howells * the script interpreter */ 2332a6f76f23SDavid Howells if (bprm->cred_prepared) 23331da177e4SLinus Torvalds return 0; 23341da177e4SLinus Torvalds 2335a6f76f23SDavid Howells old_tsec = current_security(); 2336a6f76f23SDavid Howells new_tsec = bprm->cred->security; 233783da53c5SAndreas Gruenbacher isec = inode_security(inode); 23381da177e4SLinus Torvalds 23391da177e4SLinus Torvalds /* Default to the current task SID. */ 2340a6f76f23SDavid Howells new_tsec->sid = old_tsec->sid; 2341a6f76f23SDavid Howells new_tsec->osid = old_tsec->sid; 23421da177e4SLinus Torvalds 234328eba5bfSMichael LeMay /* Reset fs, key, and sock SIDs on execve. */ 2344a6f76f23SDavid Howells new_tsec->create_sid = 0; 2345a6f76f23SDavid Howells new_tsec->keycreate_sid = 0; 2346a6f76f23SDavid Howells new_tsec->sockcreate_sid = 0; 23471da177e4SLinus Torvalds 2348a6f76f23SDavid Howells if (old_tsec->exec_sid) { 2349a6f76f23SDavid Howells new_tsec->sid = old_tsec->exec_sid; 23501da177e4SLinus Torvalds /* Reset exec SID on execve. */ 2351a6f76f23SDavid Howells new_tsec->exec_sid = 0; 2352259e5e6cSAndy Lutomirski 23537b0d0b40SStephen Smalley /* Fail on NNP or nosuid if not an allowed transition. */ 23547b0d0b40SStephen Smalley rc = check_nnp_nosuid(bprm, old_tsec, new_tsec); 23557b0d0b40SStephen Smalley if (rc) 23567b0d0b40SStephen Smalley return rc; 23571da177e4SLinus Torvalds } else { 23581da177e4SLinus Torvalds /* Check for a default transition on this program. */ 2359a6f76f23SDavid Howells rc = security_transition_sid(old_tsec->sid, isec->sid, 2360652bb9b0SEric Paris SECCLASS_PROCESS, NULL, 2361652bb9b0SEric Paris &new_tsec->sid); 23621da177e4SLinus Torvalds if (rc) 23631da177e4SLinus Torvalds return rc; 23647b0d0b40SStephen Smalley 23657b0d0b40SStephen Smalley /* 23667b0d0b40SStephen Smalley * Fallback to old SID on NNP or nosuid if not an allowed 23677b0d0b40SStephen Smalley * transition. 23687b0d0b40SStephen Smalley */ 23697b0d0b40SStephen Smalley rc = check_nnp_nosuid(bprm, old_tsec, new_tsec); 23707b0d0b40SStephen Smalley if (rc) 23717b0d0b40SStephen Smalley new_tsec->sid = old_tsec->sid; 23721da177e4SLinus Torvalds } 23731da177e4SLinus Torvalds 237443af5de7SVivek Goyal ad.type = LSM_AUDIT_DATA_FILE; 237543af5de7SVivek Goyal ad.u.file = bprm->file; 23761da177e4SLinus Torvalds 2377a6f76f23SDavid Howells if (new_tsec->sid == old_tsec->sid) { 2378a6f76f23SDavid Howells rc = avc_has_perm(old_tsec->sid, isec->sid, 23791da177e4SLinus Torvalds SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad); 23801da177e4SLinus Torvalds if (rc) 23811da177e4SLinus Torvalds return rc; 23821da177e4SLinus Torvalds } else { 23831da177e4SLinus Torvalds /* Check permissions for the transition. */ 2384a6f76f23SDavid Howells rc = avc_has_perm(old_tsec->sid, new_tsec->sid, 23851da177e4SLinus Torvalds SECCLASS_PROCESS, PROCESS__TRANSITION, &ad); 23861da177e4SLinus Torvalds if (rc) 23871da177e4SLinus Torvalds return rc; 23881da177e4SLinus Torvalds 2389a6f76f23SDavid Howells rc = avc_has_perm(new_tsec->sid, isec->sid, 23901da177e4SLinus Torvalds SECCLASS_FILE, FILE__ENTRYPOINT, &ad); 23911da177e4SLinus Torvalds if (rc) 23921da177e4SLinus Torvalds return rc; 23931da177e4SLinus Torvalds 2394a6f76f23SDavid Howells /* Check for shared state */ 2395a6f76f23SDavid Howells if (bprm->unsafe & LSM_UNSAFE_SHARE) { 2396a6f76f23SDavid Howells rc = avc_has_perm(old_tsec->sid, new_tsec->sid, 2397a6f76f23SDavid Howells SECCLASS_PROCESS, PROCESS__SHARE, 2398a6f76f23SDavid Howells NULL); 2399a6f76f23SDavid Howells if (rc) 2400a6f76f23SDavid Howells return -EPERM; 24011da177e4SLinus Torvalds } 24021da177e4SLinus Torvalds 2403a6f76f23SDavid Howells /* Make sure that anyone attempting to ptrace over a task that 2404a6f76f23SDavid Howells * changes its SID has the appropriate permit */ 24059227dd2aSEric W. Biederman if (bprm->unsafe & LSM_UNSAFE_PTRACE) { 2406be0554c9SStephen Smalley u32 ptsid = ptrace_parent_sid(); 2407a6f76f23SDavid Howells if (ptsid != 0) { 2408a6f76f23SDavid Howells rc = avc_has_perm(ptsid, new_tsec->sid, 2409a6f76f23SDavid Howells SECCLASS_PROCESS, 2410a6f76f23SDavid Howells PROCESS__PTRACE, NULL); 2411a6f76f23SDavid Howells if (rc) 2412a6f76f23SDavid Howells return -EPERM; 2413a6f76f23SDavid Howells } 2414a6f76f23SDavid Howells } 2415a6f76f23SDavid Howells 2416a6f76f23SDavid Howells /* Clear any possibly unsafe personality bits on exec: */ 2417a6f76f23SDavid Howells bprm->per_clear |= PER_CLEAR_ON_SETID; 2418a6f76f23SDavid Howells } 2419a6f76f23SDavid Howells 24201da177e4SLinus Torvalds return 0; 24211da177e4SLinus Torvalds } 24221da177e4SLinus Torvalds 24231da177e4SLinus Torvalds static int selinux_bprm_secureexec(struct linux_binprm *bprm) 24241da177e4SLinus Torvalds { 24255fb49870SPaul Moore const struct task_security_struct *tsec = current_security(); 2426275bb41eSDavid Howells u32 sid, osid; 24271da177e4SLinus Torvalds int atsecure = 0; 24281da177e4SLinus Torvalds 2429275bb41eSDavid Howells sid = tsec->sid; 2430275bb41eSDavid Howells osid = tsec->osid; 2431275bb41eSDavid Howells 2432275bb41eSDavid Howells if (osid != sid) { 24331da177e4SLinus Torvalds /* Enable secure mode for SIDs transitions unless 24341da177e4SLinus Torvalds the noatsecure permission is granted between 24351da177e4SLinus Torvalds the two SIDs, i.e. ahp returns 0. */ 2436275bb41eSDavid Howells atsecure = avc_has_perm(osid, sid, 24371da177e4SLinus Torvalds SECCLASS_PROCESS, 24381da177e4SLinus Torvalds PROCESS__NOATSECURE, NULL); 24391da177e4SLinus Torvalds } 24401da177e4SLinus Torvalds 2441b1d9e6b0SCasey Schaufler return !!atsecure; 24421da177e4SLinus Torvalds } 24431da177e4SLinus Torvalds 2444c3c073f8SAl Viro static int match_file(const void *p, struct file *file, unsigned fd) 2445c3c073f8SAl Viro { 2446c3c073f8SAl Viro return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0; 2447c3c073f8SAl Viro } 2448c3c073f8SAl Viro 24491da177e4SLinus Torvalds /* Derived from fs/exec.c:flush_old_files. */ 2450745ca247SDavid Howells static inline void flush_unauthorized_files(const struct cred *cred, 2451745ca247SDavid Howells struct files_struct *files) 24521da177e4SLinus Torvalds { 24531da177e4SLinus Torvalds struct file *file, *devnull = NULL; 2454b20c8122SStephen Smalley struct tty_struct *tty; 245524ec839cSPeter Zijlstra int drop_tty = 0; 2456c3c073f8SAl Viro unsigned n; 24571da177e4SLinus Torvalds 245824ec839cSPeter Zijlstra tty = get_current_tty(); 24591da177e4SLinus Torvalds if (tty) { 24604a510969SPeter Hurley spin_lock(&tty->files_lock); 246137dd0bd0SEric Paris if (!list_empty(&tty->tty_files)) { 2462d996b62aSNick Piggin struct tty_file_private *file_priv; 246337dd0bd0SEric Paris 24641da177e4SLinus Torvalds /* Revalidate access to controlling tty. 246513f8e981SDavid Howells Use file_path_has_perm on the tty path directly 246613f8e981SDavid Howells rather than using file_has_perm, as this particular 246713f8e981SDavid Howells open file may belong to another process and we are 246813f8e981SDavid Howells only interested in the inode-based check here. */ 2469d996b62aSNick Piggin file_priv = list_first_entry(&tty->tty_files, 2470d996b62aSNick Piggin struct tty_file_private, list); 2471d996b62aSNick Piggin file = file_priv->file; 247213f8e981SDavid Howells if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE)) 247324ec839cSPeter Zijlstra drop_tty = 1; 24741da177e4SLinus Torvalds } 24754a510969SPeter Hurley spin_unlock(&tty->files_lock); 2476452a00d2SAlan Cox tty_kref_put(tty); 24771da177e4SLinus Torvalds } 247898a27ba4SEric W. Biederman /* Reset controlling tty. */ 247998a27ba4SEric W. Biederman if (drop_tty) 248098a27ba4SEric W. Biederman no_tty(); 24811da177e4SLinus Torvalds 24821da177e4SLinus Torvalds /* Revalidate access to inherited open files. */ 2483c3c073f8SAl Viro n = iterate_fd(files, 0, match_file, cred); 2484c3c073f8SAl Viro if (!n) /* none found? */ 2485c3c073f8SAl Viro return; 24861da177e4SLinus Torvalds 2487c3c073f8SAl Viro devnull = dentry_open(&selinux_null, O_RDWR, cred); 248845525b26SAl Viro if (IS_ERR(devnull)) 248945525b26SAl Viro devnull = NULL; 2490c3c073f8SAl Viro /* replace all the matching ones with this */ 2491c3c073f8SAl Viro do { 249245525b26SAl Viro replace_fd(n - 1, devnull, 0); 2493c3c073f8SAl Viro } while ((n = iterate_fd(files, n, match_file, cred)) != 0); 249445525b26SAl Viro if (devnull) 2495c3c073f8SAl Viro fput(devnull); 24961da177e4SLinus Torvalds } 24971da177e4SLinus Torvalds 24981da177e4SLinus Torvalds /* 2499a6f76f23SDavid Howells * Prepare a process for imminent new credential changes due to exec 25001da177e4SLinus Torvalds */ 2501a6f76f23SDavid Howells static void selinux_bprm_committing_creds(struct linux_binprm *bprm) 25021da177e4SLinus Torvalds { 2503a6f76f23SDavid Howells struct task_security_struct *new_tsec; 25041da177e4SLinus Torvalds struct rlimit *rlim, *initrlim; 25051da177e4SLinus Torvalds int rc, i; 25061da177e4SLinus Torvalds 2507a6f76f23SDavid Howells new_tsec = bprm->cred->security; 2508a6f76f23SDavid Howells if (new_tsec->sid == new_tsec->osid) 25091da177e4SLinus Torvalds return; 25101da177e4SLinus Torvalds 25111da177e4SLinus Torvalds /* Close files for which the new task SID is not authorized. */ 2512a6f76f23SDavid Howells flush_unauthorized_files(bprm->cred, current->files); 25131da177e4SLinus Torvalds 2514a6f76f23SDavid Howells /* Always clear parent death signal on SID transitions. */ 2515a6f76f23SDavid Howells current->pdeath_signal = 0; 2516a6f76f23SDavid Howells 2517a6f76f23SDavid Howells /* Check whether the new SID can inherit resource limits from the old 2518a6f76f23SDavid Howells * SID. If not, reset all soft limits to the lower of the current 2519a6f76f23SDavid Howells * task's hard limit and the init task's soft limit. 2520a6f76f23SDavid Howells * 2521a6f76f23SDavid Howells * Note that the setting of hard limits (even to lower them) can be 2522a6f76f23SDavid Howells * controlled by the setrlimit check. The inclusion of the init task's 2523a6f76f23SDavid Howells * soft limit into the computation is to avoid resetting soft limits 2524a6f76f23SDavid Howells * higher than the default soft limit for cases where the default is 2525a6f76f23SDavid Howells * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK. 2526a6f76f23SDavid Howells */ 2527a6f76f23SDavid Howells rc = avc_has_perm(new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS, 2528a6f76f23SDavid Howells PROCESS__RLIMITINH, NULL); 2529a6f76f23SDavid Howells if (rc) { 2530eb2d55a3SOleg Nesterov /* protect against do_prlimit() */ 2531eb2d55a3SOleg Nesterov task_lock(current); 2532a6f76f23SDavid Howells for (i = 0; i < RLIM_NLIMITS; i++) { 2533a6f76f23SDavid Howells rlim = current->signal->rlim + i; 2534a6f76f23SDavid Howells initrlim = init_task.signal->rlim + i; 2535a6f76f23SDavid Howells rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur); 2536a6f76f23SDavid Howells } 2537eb2d55a3SOleg Nesterov task_unlock(current); 2538baa73d9eSNicolas Pitre if (IS_ENABLED(CONFIG_POSIX_TIMERS)) 2539eb2d55a3SOleg Nesterov update_rlimit_cpu(current, rlimit(RLIMIT_CPU)); 2540a6f76f23SDavid Howells } 2541a6f76f23SDavid Howells } 2542a6f76f23SDavid Howells 2543a6f76f23SDavid Howells /* 2544a6f76f23SDavid Howells * Clean up the process immediately after the installation of new credentials 2545a6f76f23SDavid Howells * due to exec 2546a6f76f23SDavid Howells */ 2547a6f76f23SDavid Howells static void selinux_bprm_committed_creds(struct linux_binprm *bprm) 2548a6f76f23SDavid Howells { 2549a6f76f23SDavid Howells const struct task_security_struct *tsec = current_security(); 2550a6f76f23SDavid Howells struct itimerval itimer; 2551a6f76f23SDavid Howells u32 osid, sid; 2552a6f76f23SDavid Howells int rc, i; 2553a6f76f23SDavid Howells 2554a6f76f23SDavid Howells osid = tsec->osid; 2555a6f76f23SDavid Howells sid = tsec->sid; 2556a6f76f23SDavid Howells 2557a6f76f23SDavid Howells if (sid == osid) 2558a6f76f23SDavid Howells return; 2559a6f76f23SDavid Howells 2560a6f76f23SDavid Howells /* Check whether the new SID can inherit signal state from the old SID. 2561a6f76f23SDavid Howells * If not, clear itimers to avoid subsequent signal generation and 2562a6f76f23SDavid Howells * flush and unblock signals. 2563a6f76f23SDavid Howells * 2564a6f76f23SDavid Howells * This must occur _after_ the task SID has been updated so that any 2565a6f76f23SDavid Howells * kill done after the flush will be checked against the new SID. 2566a6f76f23SDavid Howells */ 2567a6f76f23SDavid Howells rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL); 25681da177e4SLinus Torvalds if (rc) { 2569baa73d9eSNicolas Pitre if (IS_ENABLED(CONFIG_POSIX_TIMERS)) { 25701da177e4SLinus Torvalds memset(&itimer, 0, sizeof itimer); 25711da177e4SLinus Torvalds for (i = 0; i < 3; i++) 25721da177e4SLinus Torvalds do_setitimer(i, &itimer, NULL); 2573baa73d9eSNicolas Pitre } 25741da177e4SLinus Torvalds spin_lock_irq(¤t->sighand->siglock); 25759e7c8f8cSOleg Nesterov if (!fatal_signal_pending(current)) { 25769e7c8f8cSOleg Nesterov flush_sigqueue(¤t->pending); 25779e7c8f8cSOleg Nesterov flush_sigqueue(¤t->signal->shared_pending); 25781da177e4SLinus Torvalds flush_signal_handlers(current, 1); 25791da177e4SLinus Torvalds sigemptyset(¤t->blocked); 25809e7c8f8cSOleg Nesterov recalc_sigpending(); 25813bcac026SDavid Howells } 25821da177e4SLinus Torvalds spin_unlock_irq(¤t->sighand->siglock); 25831da177e4SLinus Torvalds } 25841da177e4SLinus Torvalds 2585a6f76f23SDavid Howells /* Wake up the parent if it is waiting so that it can recheck 2586a6f76f23SDavid Howells * wait permission to the new task SID. */ 2587ecd6de3cSOleg Nesterov read_lock(&tasklist_lock); 25880b7570e7SOleg Nesterov __wake_up_parent(current, current->real_parent); 2589ecd6de3cSOleg Nesterov read_unlock(&tasklist_lock); 25901da177e4SLinus Torvalds } 25911da177e4SLinus Torvalds 25921da177e4SLinus Torvalds /* superblock security operations */ 25931da177e4SLinus Torvalds 25941da177e4SLinus Torvalds static int selinux_sb_alloc_security(struct super_block *sb) 25951da177e4SLinus Torvalds { 25961da177e4SLinus Torvalds return superblock_alloc_security(sb); 25971da177e4SLinus Torvalds } 25981da177e4SLinus Torvalds 25991da177e4SLinus Torvalds static void selinux_sb_free_security(struct super_block *sb) 26001da177e4SLinus Torvalds { 26011da177e4SLinus Torvalds superblock_free_security(sb); 26021da177e4SLinus Torvalds } 26031da177e4SLinus Torvalds 26041da177e4SLinus Torvalds static inline int match_prefix(char *prefix, int plen, char *option, int olen) 26051da177e4SLinus Torvalds { 26061da177e4SLinus Torvalds if (plen > olen) 26071da177e4SLinus Torvalds return 0; 26081da177e4SLinus Torvalds 26091da177e4SLinus Torvalds return !memcmp(prefix, option, plen); 26101da177e4SLinus Torvalds } 26111da177e4SLinus Torvalds 26121da177e4SLinus Torvalds static inline int selinux_option(char *option, int len) 26131da177e4SLinus Torvalds { 2614832cbd9aSEric Paris return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) || 2615832cbd9aSEric Paris match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) || 2616832cbd9aSEric Paris match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) || 261711689d47SDavid P. Quigley match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) || 261811689d47SDavid P. Quigley match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len)); 26191da177e4SLinus Torvalds } 26201da177e4SLinus Torvalds 26211da177e4SLinus Torvalds static inline void take_option(char **to, char *from, int *first, int len) 26221da177e4SLinus Torvalds { 26231da177e4SLinus Torvalds if (!*first) { 26241da177e4SLinus Torvalds **to = ','; 26251da177e4SLinus Torvalds *to += 1; 26263528a953SCory Olmo } else 26271da177e4SLinus Torvalds *first = 0; 26281da177e4SLinus Torvalds memcpy(*to, from, len); 26291da177e4SLinus Torvalds *to += len; 26301da177e4SLinus Torvalds } 26311da177e4SLinus Torvalds 26323528a953SCory Olmo static inline void take_selinux_option(char **to, char *from, int *first, 26333528a953SCory Olmo int len) 26343528a953SCory Olmo { 26353528a953SCory Olmo int current_size = 0; 26363528a953SCory Olmo 26373528a953SCory Olmo if (!*first) { 26383528a953SCory Olmo **to = '|'; 26393528a953SCory Olmo *to += 1; 2640828dfe1dSEric Paris } else 26413528a953SCory Olmo *first = 0; 26423528a953SCory Olmo 26433528a953SCory Olmo while (current_size < len) { 26443528a953SCory Olmo if (*from != '"') { 26453528a953SCory Olmo **to = *from; 26463528a953SCory Olmo *to += 1; 26473528a953SCory Olmo } 26483528a953SCory Olmo from += 1; 26493528a953SCory Olmo current_size += 1; 26503528a953SCory Olmo } 26513528a953SCory Olmo } 26523528a953SCory Olmo 2653e0007529SEric Paris static int selinux_sb_copy_data(char *orig, char *copy) 26541da177e4SLinus Torvalds { 26551da177e4SLinus Torvalds int fnosec, fsec, rc = 0; 26561da177e4SLinus Torvalds char *in_save, *in_curr, *in_end; 26571da177e4SLinus Torvalds char *sec_curr, *nosec_save, *nosec; 26583528a953SCory Olmo int open_quote = 0; 26591da177e4SLinus Torvalds 26601da177e4SLinus Torvalds in_curr = orig; 26611da177e4SLinus Torvalds sec_curr = copy; 26621da177e4SLinus Torvalds 26631da177e4SLinus Torvalds nosec = (char *)get_zeroed_page(GFP_KERNEL); 26641da177e4SLinus Torvalds if (!nosec) { 26651da177e4SLinus Torvalds rc = -ENOMEM; 26661da177e4SLinus Torvalds goto out; 26671da177e4SLinus Torvalds } 26681da177e4SLinus Torvalds 26691da177e4SLinus Torvalds nosec_save = nosec; 26701da177e4SLinus Torvalds fnosec = fsec = 1; 26711da177e4SLinus Torvalds in_save = in_end = orig; 26721da177e4SLinus Torvalds 26731da177e4SLinus Torvalds do { 26743528a953SCory Olmo if (*in_end == '"') 26753528a953SCory Olmo open_quote = !open_quote; 26763528a953SCory Olmo if ((*in_end == ',' && open_quote == 0) || 26773528a953SCory Olmo *in_end == '\0') { 26781da177e4SLinus Torvalds int len = in_end - in_curr; 26791da177e4SLinus Torvalds 26801da177e4SLinus Torvalds if (selinux_option(in_curr, len)) 26813528a953SCory Olmo take_selinux_option(&sec_curr, in_curr, &fsec, len); 26821da177e4SLinus Torvalds else 26831da177e4SLinus Torvalds take_option(&nosec, in_curr, &fnosec, len); 26841da177e4SLinus Torvalds 26851da177e4SLinus Torvalds in_curr = in_end + 1; 26861da177e4SLinus Torvalds } 26871da177e4SLinus Torvalds } while (*in_end++); 26881da177e4SLinus Torvalds 26896931dfc9SEric Paris strcpy(in_save, nosec_save); 2690da3caa20SGerald Schaefer free_page((unsigned long)nosec_save); 26911da177e4SLinus Torvalds out: 26921da177e4SLinus Torvalds return rc; 26931da177e4SLinus Torvalds } 26941da177e4SLinus Torvalds 2695026eb167SEric Paris static int selinux_sb_remount(struct super_block *sb, void *data) 2696026eb167SEric Paris { 2697026eb167SEric Paris int rc, i, *flags; 2698026eb167SEric Paris struct security_mnt_opts opts; 2699026eb167SEric Paris char *secdata, **mount_options; 2700026eb167SEric Paris struct superblock_security_struct *sbsec = sb->s_security; 2701026eb167SEric Paris 2702026eb167SEric Paris if (!(sbsec->flags & SE_SBINITIALIZED)) 2703026eb167SEric Paris return 0; 2704026eb167SEric Paris 2705026eb167SEric Paris if (!data) 2706026eb167SEric Paris return 0; 2707026eb167SEric Paris 2708026eb167SEric Paris if (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA) 2709026eb167SEric Paris return 0; 2710026eb167SEric Paris 2711026eb167SEric Paris security_init_mnt_opts(&opts); 2712026eb167SEric Paris secdata = alloc_secdata(); 2713026eb167SEric Paris if (!secdata) 2714026eb167SEric Paris return -ENOMEM; 2715026eb167SEric Paris rc = selinux_sb_copy_data(data, secdata); 2716026eb167SEric Paris if (rc) 2717026eb167SEric Paris goto out_free_secdata; 2718026eb167SEric Paris 2719026eb167SEric Paris rc = selinux_parse_opts_str(secdata, &opts); 2720026eb167SEric Paris if (rc) 2721026eb167SEric Paris goto out_free_secdata; 2722026eb167SEric Paris 2723026eb167SEric Paris mount_options = opts.mnt_opts; 2724026eb167SEric Paris flags = opts.mnt_opts_flags; 2725026eb167SEric Paris 2726026eb167SEric Paris for (i = 0; i < opts.num_mnt_opts; i++) { 2727026eb167SEric Paris u32 sid; 2728026eb167SEric Paris 272912f348b9SEric Paris if (flags[i] == SBLABEL_MNT) 2730026eb167SEric Paris continue; 273144be2f65SRasmus Villemoes rc = security_context_str_to_sid(mount_options[i], &sid, GFP_KERNEL); 2732026eb167SEric Paris if (rc) { 273344be2f65SRasmus Villemoes printk(KERN_WARNING "SELinux: security_context_str_to_sid" 273429b1deb2SLinus Torvalds "(%s) failed for (dev %s, type %s) errno=%d\n", 273529b1deb2SLinus Torvalds mount_options[i], sb->s_id, sb->s_type->name, rc); 2736026eb167SEric Paris goto out_free_opts; 2737026eb167SEric Paris } 2738026eb167SEric Paris rc = -EINVAL; 2739026eb167SEric Paris switch (flags[i]) { 2740026eb167SEric Paris case FSCONTEXT_MNT: 2741026eb167SEric Paris if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid)) 2742026eb167SEric Paris goto out_bad_option; 2743026eb167SEric Paris break; 2744026eb167SEric Paris case CONTEXT_MNT: 2745026eb167SEric Paris if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid)) 2746026eb167SEric Paris goto out_bad_option; 2747026eb167SEric Paris break; 2748026eb167SEric Paris case ROOTCONTEXT_MNT: { 2749026eb167SEric Paris struct inode_security_struct *root_isec; 275083da53c5SAndreas Gruenbacher root_isec = backing_inode_security(sb->s_root); 2751026eb167SEric Paris 2752026eb167SEric Paris if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid)) 2753026eb167SEric Paris goto out_bad_option; 2754026eb167SEric Paris break; 2755026eb167SEric Paris } 2756026eb167SEric Paris case DEFCONTEXT_MNT: 2757026eb167SEric Paris if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid)) 2758026eb167SEric Paris goto out_bad_option; 2759026eb167SEric Paris break; 2760026eb167SEric Paris default: 2761026eb167SEric Paris goto out_free_opts; 2762026eb167SEric Paris } 2763026eb167SEric Paris } 2764026eb167SEric Paris 2765026eb167SEric Paris rc = 0; 2766026eb167SEric Paris out_free_opts: 2767026eb167SEric Paris security_free_mnt_opts(&opts); 2768026eb167SEric Paris out_free_secdata: 2769026eb167SEric Paris free_secdata(secdata); 2770026eb167SEric Paris return rc; 2771026eb167SEric Paris out_bad_option: 2772026eb167SEric Paris printk(KERN_WARNING "SELinux: unable to change security options " 277329b1deb2SLinus Torvalds "during remount (dev %s, type=%s)\n", sb->s_id, 277429b1deb2SLinus Torvalds sb->s_type->name); 2775026eb167SEric Paris goto out_free_opts; 2776026eb167SEric Paris } 2777026eb167SEric Paris 277812204e24SJames Morris static int selinux_sb_kern_mount(struct super_block *sb, int flags, void *data) 27791da177e4SLinus Torvalds { 278088e67f3bSDavid Howells const struct cred *cred = current_cred(); 27812bf49690SThomas Liu struct common_audit_data ad; 27821da177e4SLinus Torvalds int rc; 27831da177e4SLinus Torvalds 27841da177e4SLinus Torvalds rc = superblock_doinit(sb, data); 27851da177e4SLinus Torvalds if (rc) 27861da177e4SLinus Torvalds return rc; 27871da177e4SLinus Torvalds 278874192246SJames Morris /* Allow all mounts performed by the kernel */ 278974192246SJames Morris if (flags & MS_KERNMOUNT) 279074192246SJames Morris return 0; 279174192246SJames Morris 279250c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 2793a269434dSEric Paris ad.u.dentry = sb->s_root; 279488e67f3bSDavid Howells return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad); 27951da177e4SLinus Torvalds } 27961da177e4SLinus Torvalds 2797726c3342SDavid Howells static int selinux_sb_statfs(struct dentry *dentry) 27981da177e4SLinus Torvalds { 279988e67f3bSDavid Howells const struct cred *cred = current_cred(); 28002bf49690SThomas Liu struct common_audit_data ad; 28011da177e4SLinus Torvalds 280250c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 2803a269434dSEric Paris ad.u.dentry = dentry->d_sb->s_root; 280488e67f3bSDavid Howells return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad); 28051da177e4SLinus Torvalds } 28061da177e4SLinus Torvalds 2807808d4e3cSAl Viro static int selinux_mount(const char *dev_name, 28088a04c43bSAl Viro const struct path *path, 2809808d4e3cSAl Viro const char *type, 28101da177e4SLinus Torvalds unsigned long flags, 28111da177e4SLinus Torvalds void *data) 28121da177e4SLinus Torvalds { 281388e67f3bSDavid Howells const struct cred *cred = current_cred(); 28141da177e4SLinus Torvalds 28151da177e4SLinus Torvalds if (flags & MS_REMOUNT) 2816d8c9584eSAl Viro return superblock_has_perm(cred, path->dentry->d_sb, 28171da177e4SLinus Torvalds FILESYSTEM__REMOUNT, NULL); 28181da177e4SLinus Torvalds else 28192875fa00SEric Paris return path_has_perm(cred, path, FILE__MOUNTON); 28201da177e4SLinus Torvalds } 28211da177e4SLinus Torvalds 28221da177e4SLinus Torvalds static int selinux_umount(struct vfsmount *mnt, int flags) 28231da177e4SLinus Torvalds { 282488e67f3bSDavid Howells const struct cred *cred = current_cred(); 28251da177e4SLinus Torvalds 282688e67f3bSDavid Howells return superblock_has_perm(cred, mnt->mnt_sb, 28271da177e4SLinus Torvalds FILESYSTEM__UNMOUNT, NULL); 28281da177e4SLinus Torvalds } 28291da177e4SLinus Torvalds 28301da177e4SLinus Torvalds /* inode security operations */ 28311da177e4SLinus Torvalds 28321da177e4SLinus Torvalds static int selinux_inode_alloc_security(struct inode *inode) 28331da177e4SLinus Torvalds { 28341da177e4SLinus Torvalds return inode_alloc_security(inode); 28351da177e4SLinus Torvalds } 28361da177e4SLinus Torvalds 28371da177e4SLinus Torvalds static void selinux_inode_free_security(struct inode *inode) 28381da177e4SLinus Torvalds { 28391da177e4SLinus Torvalds inode_free_security(inode); 28401da177e4SLinus Torvalds } 28411da177e4SLinus Torvalds 2842d47be3dfSDavid Quigley static int selinux_dentry_init_security(struct dentry *dentry, int mode, 28434f3ccd76SAl Viro const struct qstr *name, void **ctx, 2844d47be3dfSDavid Quigley u32 *ctxlen) 2845d47be3dfSDavid Quigley { 2846d47be3dfSDavid Quigley u32 newsid; 2847d47be3dfSDavid Quigley int rc; 2848d47be3dfSDavid Quigley 2849c957f6dfSVivek Goyal rc = selinux_determine_inode_label(current_security(), 2850c957f6dfSVivek Goyal d_inode(dentry->d_parent), name, 2851d47be3dfSDavid Quigley inode_mode_to_security_class(mode), 2852d47be3dfSDavid Quigley &newsid); 2853c3c188b2SDavid Howells if (rc) 2854d47be3dfSDavid Quigley return rc; 2855d47be3dfSDavid Quigley 2856d47be3dfSDavid Quigley return security_sid_to_context(newsid, (char **)ctx, ctxlen); 2857d47be3dfSDavid Quigley } 2858d47be3dfSDavid Quigley 2859a518b0a5SVivek Goyal static int selinux_dentry_create_files_as(struct dentry *dentry, int mode, 2860a518b0a5SVivek Goyal struct qstr *name, 2861a518b0a5SVivek Goyal const struct cred *old, 2862a518b0a5SVivek Goyal struct cred *new) 2863a518b0a5SVivek Goyal { 2864a518b0a5SVivek Goyal u32 newsid; 2865a518b0a5SVivek Goyal int rc; 2866a518b0a5SVivek Goyal struct task_security_struct *tsec; 2867a518b0a5SVivek Goyal 2868a518b0a5SVivek Goyal rc = selinux_determine_inode_label(old->security, 2869a518b0a5SVivek Goyal d_inode(dentry->d_parent), name, 2870a518b0a5SVivek Goyal inode_mode_to_security_class(mode), 2871a518b0a5SVivek Goyal &newsid); 2872a518b0a5SVivek Goyal if (rc) 2873a518b0a5SVivek Goyal return rc; 2874a518b0a5SVivek Goyal 2875a518b0a5SVivek Goyal tsec = new->security; 2876a518b0a5SVivek Goyal tsec->create_sid = newsid; 2877a518b0a5SVivek Goyal return 0; 2878a518b0a5SVivek Goyal } 2879a518b0a5SVivek Goyal 28805e41ff9eSStephen Smalley static int selinux_inode_init_security(struct inode *inode, struct inode *dir, 28819548906bSTetsuo Handa const struct qstr *qstr, 28829548906bSTetsuo Handa const char **name, 28832a7dba39SEric Paris void **value, size_t *len) 28845e41ff9eSStephen Smalley { 28855fb49870SPaul Moore const struct task_security_struct *tsec = current_security(); 28865e41ff9eSStephen Smalley struct superblock_security_struct *sbsec; 2887275bb41eSDavid Howells u32 sid, newsid, clen; 28885e41ff9eSStephen Smalley int rc; 28899548906bSTetsuo Handa char *context; 28905e41ff9eSStephen Smalley 28915e41ff9eSStephen Smalley sbsec = dir->i_sb->s_security; 28925e41ff9eSStephen Smalley 2893275bb41eSDavid Howells sid = tsec->sid; 28945e41ff9eSStephen Smalley newsid = tsec->create_sid; 2895275bb41eSDavid Howells 2896c957f6dfSVivek Goyal rc = selinux_determine_inode_label(current_security(), 2897c3c188b2SDavid Howells dir, qstr, 28985e41ff9eSStephen Smalley inode_mode_to_security_class(inode->i_mode), 2899c3c188b2SDavid Howells &newsid); 2900c3c188b2SDavid Howells if (rc) 29015e41ff9eSStephen Smalley return rc; 29025e41ff9eSStephen Smalley 2903296fddf7SEric Paris /* Possibly defer initialization to selinux_complete_init. */ 29040d90a7ecSDavid P. Quigley if (sbsec->flags & SE_SBINITIALIZED) { 2905296fddf7SEric Paris struct inode_security_struct *isec = inode->i_security; 2906296fddf7SEric Paris isec->sclass = inode_mode_to_security_class(inode->i_mode); 2907296fddf7SEric Paris isec->sid = newsid; 29086f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 2909296fddf7SEric Paris } 29105e41ff9eSStephen Smalley 291112f348b9SEric Paris if (!ss_initialized || !(sbsec->flags & SBLABEL_MNT)) 291225a74f3bSStephen Smalley return -EOPNOTSUPP; 291325a74f3bSStephen Smalley 29149548906bSTetsuo Handa if (name) 29159548906bSTetsuo Handa *name = XATTR_SELINUX_SUFFIX; 29165e41ff9eSStephen Smalley 2917570bc1c2SStephen Smalley if (value && len) { 291812b29f34SStephen Smalley rc = security_sid_to_context_force(newsid, &context, &clen); 29199548906bSTetsuo Handa if (rc) 29205e41ff9eSStephen Smalley return rc; 29215e41ff9eSStephen Smalley *value = context; 2922570bc1c2SStephen Smalley *len = clen; 2923570bc1c2SStephen Smalley } 29245e41ff9eSStephen Smalley 29255e41ff9eSStephen Smalley return 0; 29265e41ff9eSStephen Smalley } 29275e41ff9eSStephen Smalley 29284acdaf27SAl Viro static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode) 29291da177e4SLinus Torvalds { 29301da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_FILE); 29311da177e4SLinus Torvalds } 29321da177e4SLinus Torvalds 29331da177e4SLinus Torvalds static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 29341da177e4SLinus Torvalds { 29351da177e4SLinus Torvalds return may_link(dir, old_dentry, MAY_LINK); 29361da177e4SLinus Torvalds } 29371da177e4SLinus Torvalds 29381da177e4SLinus Torvalds static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry) 29391da177e4SLinus Torvalds { 29401da177e4SLinus Torvalds return may_link(dir, dentry, MAY_UNLINK); 29411da177e4SLinus Torvalds } 29421da177e4SLinus Torvalds 29431da177e4SLinus Torvalds static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name) 29441da177e4SLinus Torvalds { 29451da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_LNK_FILE); 29461da177e4SLinus Torvalds } 29471da177e4SLinus Torvalds 294818bb1db3SAl Viro static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask) 29491da177e4SLinus Torvalds { 29501da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_DIR); 29511da177e4SLinus Torvalds } 29521da177e4SLinus Torvalds 29531da177e4SLinus Torvalds static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry) 29541da177e4SLinus Torvalds { 29551da177e4SLinus Torvalds return may_link(dir, dentry, MAY_RMDIR); 29561da177e4SLinus Torvalds } 29571da177e4SLinus Torvalds 29581a67aafbSAl Viro static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) 29591da177e4SLinus Torvalds { 29601da177e4SLinus Torvalds return may_create(dir, dentry, inode_mode_to_security_class(mode)); 29611da177e4SLinus Torvalds } 29621da177e4SLinus Torvalds 29631da177e4SLinus Torvalds static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry, 29641da177e4SLinus Torvalds struct inode *new_inode, struct dentry *new_dentry) 29651da177e4SLinus Torvalds { 29661da177e4SLinus Torvalds return may_rename(old_inode, old_dentry, new_inode, new_dentry); 29671da177e4SLinus Torvalds } 29681da177e4SLinus Torvalds 29691da177e4SLinus Torvalds static int selinux_inode_readlink(struct dentry *dentry) 29701da177e4SLinus Torvalds { 297188e67f3bSDavid Howells const struct cred *cred = current_cred(); 297288e67f3bSDavid Howells 29732875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__READ); 29741da177e4SLinus Torvalds } 29751da177e4SLinus Torvalds 2976bda0be7aSNeilBrown static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode, 2977bda0be7aSNeilBrown bool rcu) 29781da177e4SLinus Torvalds { 297988e67f3bSDavid Howells const struct cred *cred = current_cred(); 2980bda0be7aSNeilBrown struct common_audit_data ad; 2981bda0be7aSNeilBrown struct inode_security_struct *isec; 2982bda0be7aSNeilBrown u32 sid; 29831da177e4SLinus Torvalds 2984bda0be7aSNeilBrown validate_creds(cred); 2985bda0be7aSNeilBrown 2986bda0be7aSNeilBrown ad.type = LSM_AUDIT_DATA_DENTRY; 2987bda0be7aSNeilBrown ad.u.dentry = dentry; 2988bda0be7aSNeilBrown sid = cred_sid(cred); 29895d226df4SAndreas Gruenbacher isec = inode_security_rcu(inode, rcu); 29905d226df4SAndreas Gruenbacher if (IS_ERR(isec)) 29915d226df4SAndreas Gruenbacher return PTR_ERR(isec); 2992bda0be7aSNeilBrown 2993bda0be7aSNeilBrown return avc_has_perm_flags(sid, isec->sid, isec->sclass, FILE__READ, &ad, 2994bda0be7aSNeilBrown rcu ? MAY_NOT_BLOCK : 0); 29951da177e4SLinus Torvalds } 29961da177e4SLinus Torvalds 2997d4cf970dSEric Paris static noinline int audit_inode_permission(struct inode *inode, 2998d4cf970dSEric Paris u32 perms, u32 audited, u32 denied, 2999626b9740SStephen Smalley int result, 3000d4cf970dSEric Paris unsigned flags) 3001d4cf970dSEric Paris { 3002d4cf970dSEric Paris struct common_audit_data ad; 3003d4cf970dSEric Paris struct inode_security_struct *isec = inode->i_security; 3004d4cf970dSEric Paris int rc; 3005d4cf970dSEric Paris 300650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_INODE; 3007d4cf970dSEric Paris ad.u.inode = inode; 3008d4cf970dSEric Paris 3009d4cf970dSEric Paris rc = slow_avc_audit(current_sid(), isec->sid, isec->sclass, perms, 3010626b9740SStephen Smalley audited, denied, result, &ad, flags); 3011d4cf970dSEric Paris if (rc) 3012d4cf970dSEric Paris return rc; 3013d4cf970dSEric Paris return 0; 3014d4cf970dSEric Paris } 3015d4cf970dSEric Paris 3016e74f71ebSAl Viro static int selinux_inode_permission(struct inode *inode, int mask) 30171da177e4SLinus Torvalds { 301888e67f3bSDavid Howells const struct cred *cred = current_cred(); 3019b782e0a6SEric Paris u32 perms; 3020b782e0a6SEric Paris bool from_access; 3021cf1dd1daSAl Viro unsigned flags = mask & MAY_NOT_BLOCK; 30222e334057SEric Paris struct inode_security_struct *isec; 30232e334057SEric Paris u32 sid; 30242e334057SEric Paris struct av_decision avd; 30252e334057SEric Paris int rc, rc2; 30262e334057SEric Paris u32 audited, denied; 30271da177e4SLinus Torvalds 3028b782e0a6SEric Paris from_access = mask & MAY_ACCESS; 3029d09ca739SEric Paris mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND); 3030d09ca739SEric Paris 30311da177e4SLinus Torvalds /* No permission to check. Existence test. */ 3032b782e0a6SEric Paris if (!mask) 30331da177e4SLinus Torvalds return 0; 30341da177e4SLinus Torvalds 30352e334057SEric Paris validate_creds(cred); 3036b782e0a6SEric Paris 30372e334057SEric Paris if (unlikely(IS_PRIVATE(inode))) 30382e334057SEric Paris return 0; 3039b782e0a6SEric Paris 3040b782e0a6SEric Paris perms = file_mask_to_av(inode->i_mode, mask); 3041b782e0a6SEric Paris 30422e334057SEric Paris sid = cred_sid(cred); 30435d226df4SAndreas Gruenbacher isec = inode_security_rcu(inode, flags & MAY_NOT_BLOCK); 30445d226df4SAndreas Gruenbacher if (IS_ERR(isec)) 30455d226df4SAndreas Gruenbacher return PTR_ERR(isec); 30462e334057SEric Paris 30472e334057SEric Paris rc = avc_has_perm_noaudit(sid, isec->sid, isec->sclass, perms, 0, &avd); 30482e334057SEric Paris audited = avc_audit_required(perms, &avd, rc, 30492e334057SEric Paris from_access ? FILE__AUDIT_ACCESS : 0, 30502e334057SEric Paris &denied); 30512e334057SEric Paris if (likely(!audited)) 30522e334057SEric Paris return rc; 30532e334057SEric Paris 3054626b9740SStephen Smalley rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags); 30552e334057SEric Paris if (rc2) 30562e334057SEric Paris return rc2; 30572e334057SEric Paris return rc; 30581da177e4SLinus Torvalds } 30591da177e4SLinus Torvalds 30601da177e4SLinus Torvalds static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr) 30611da177e4SLinus Torvalds { 306288e67f3bSDavid Howells const struct cred *cred = current_cred(); 3063*ccb54478SStephen Smalley struct inode *inode = d_backing_inode(dentry); 3064bc6a6008SAmerigo Wang unsigned int ia_valid = iattr->ia_valid; 306595dbf739SEric Paris __u32 av = FILE__WRITE; 30661da177e4SLinus Torvalds 3067bc6a6008SAmerigo Wang /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */ 3068bc6a6008SAmerigo Wang if (ia_valid & ATTR_FORCE) { 3069bc6a6008SAmerigo Wang ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE | 3070bc6a6008SAmerigo Wang ATTR_FORCE); 3071bc6a6008SAmerigo Wang if (!ia_valid) 30721da177e4SLinus Torvalds return 0; 3073bc6a6008SAmerigo Wang } 30741da177e4SLinus Torvalds 3075bc6a6008SAmerigo Wang if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | 3076bc6a6008SAmerigo Wang ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET)) 30772875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__SETATTR); 30781da177e4SLinus Torvalds 3079*ccb54478SStephen Smalley if (selinux_policycap_openperm && 3080*ccb54478SStephen Smalley inode->i_sb->s_magic != SOCKFS_MAGIC && 3081*ccb54478SStephen Smalley (ia_valid & ATTR_SIZE) && 3082*ccb54478SStephen Smalley !(ia_valid & ATTR_FILE)) 308395dbf739SEric Paris av |= FILE__OPEN; 308495dbf739SEric Paris 308595dbf739SEric Paris return dentry_has_perm(cred, dentry, av); 30861da177e4SLinus Torvalds } 30871da177e4SLinus Torvalds 30883f7036a0SAl Viro static int selinux_inode_getattr(const struct path *path) 30891da177e4SLinus Torvalds { 30903f7036a0SAl Viro return path_has_perm(current_cred(), path, FILE__GETATTR); 30911da177e4SLinus Torvalds } 30921da177e4SLinus Torvalds 30938f0cfa52SDavid Howells static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name) 3094b5376771SSerge E. Hallyn { 309588e67f3bSDavid Howells const struct cred *cred = current_cred(); 309688e67f3bSDavid Howells 3097b5376771SSerge E. Hallyn if (!strncmp(name, XATTR_SECURITY_PREFIX, 3098b5376771SSerge E. Hallyn sizeof XATTR_SECURITY_PREFIX - 1)) { 3099b5376771SSerge E. Hallyn if (!strcmp(name, XATTR_NAME_CAPS)) { 3100b5376771SSerge E. Hallyn if (!capable(CAP_SETFCAP)) 3101b5376771SSerge E. Hallyn return -EPERM; 3102b5376771SSerge E. Hallyn } else if (!capable(CAP_SYS_ADMIN)) { 3103b5376771SSerge E. Hallyn /* A different attribute in the security namespace. 3104b5376771SSerge E. Hallyn Restrict to administrator. */ 3105b5376771SSerge E. Hallyn return -EPERM; 3106b5376771SSerge E. Hallyn } 3107b5376771SSerge E. Hallyn } 3108b5376771SSerge E. Hallyn 3109b5376771SSerge E. Hallyn /* Not an attribute we recognize, so just check the 3110b5376771SSerge E. Hallyn ordinary setattr permission. */ 31112875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__SETATTR); 3112b5376771SSerge E. Hallyn } 3113b5376771SSerge E. Hallyn 3114db59000aSStephen Smalley static bool has_cap_mac_admin(bool audit) 3115db59000aSStephen Smalley { 3116db59000aSStephen Smalley const struct cred *cred = current_cred(); 3117db59000aSStephen Smalley int cap_audit = audit ? SECURITY_CAP_AUDIT : SECURITY_CAP_NOAUDIT; 3118db59000aSStephen Smalley 3119db59000aSStephen Smalley if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, cap_audit)) 3120db59000aSStephen Smalley return false; 3121db59000aSStephen Smalley if (cred_has_capability(cred, CAP_MAC_ADMIN, cap_audit, true)) 3122db59000aSStephen Smalley return false; 3123db59000aSStephen Smalley return true; 3124db59000aSStephen Smalley } 3125db59000aSStephen Smalley 31268f0cfa52SDavid Howells static int selinux_inode_setxattr(struct dentry *dentry, const char *name, 31278f0cfa52SDavid Howells const void *value, size_t size, int flags) 31281da177e4SLinus Torvalds { 3129c6f493d6SDavid Howells struct inode *inode = d_backing_inode(dentry); 313020cdef8dSPaul Moore struct inode_security_struct *isec; 31311da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 31322bf49690SThomas Liu struct common_audit_data ad; 3133275bb41eSDavid Howells u32 newsid, sid = current_sid(); 31341da177e4SLinus Torvalds int rc = 0; 31351da177e4SLinus Torvalds 3136b5376771SSerge E. Hallyn if (strcmp(name, XATTR_NAME_SELINUX)) 3137b5376771SSerge E. Hallyn return selinux_inode_setotherxattr(dentry, name); 31381da177e4SLinus Torvalds 31391da177e4SLinus Torvalds sbsec = inode->i_sb->s_security; 314012f348b9SEric Paris if (!(sbsec->flags & SBLABEL_MNT)) 31411da177e4SLinus Torvalds return -EOPNOTSUPP; 31421da177e4SLinus Torvalds 31432e149670SSerge E. Hallyn if (!inode_owner_or_capable(inode)) 31441da177e4SLinus Torvalds return -EPERM; 31451da177e4SLinus Torvalds 314650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 3147a269434dSEric Paris ad.u.dentry = dentry; 31481da177e4SLinus Torvalds 314920cdef8dSPaul Moore isec = backing_inode_security(dentry); 3150275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, isec->sclass, 31511da177e4SLinus Torvalds FILE__RELABELFROM, &ad); 31521da177e4SLinus Torvalds if (rc) 31531da177e4SLinus Torvalds return rc; 31541da177e4SLinus Torvalds 315552a4c640SNikolay Aleksandrov rc = security_context_to_sid(value, size, &newsid, GFP_KERNEL); 315612b29f34SStephen Smalley if (rc == -EINVAL) { 3157db59000aSStephen Smalley if (!has_cap_mac_admin(true)) { 3158d6ea83ecSEric Paris struct audit_buffer *ab; 3159d6ea83ecSEric Paris size_t audit_size; 3160d6ea83ecSEric Paris const char *str; 3161d6ea83ecSEric Paris 3162d6ea83ecSEric Paris /* We strip a nul only if it is at the end, otherwise the 3163d6ea83ecSEric Paris * context contains a nul and we should audit that */ 3164e3fea3f7SAl Viro if (value) { 3165d6ea83ecSEric Paris str = value; 3166d6ea83ecSEric Paris if (str[size - 1] == '\0') 3167d6ea83ecSEric Paris audit_size = size - 1; 3168d6ea83ecSEric Paris else 3169d6ea83ecSEric Paris audit_size = size; 3170e3fea3f7SAl Viro } else { 3171e3fea3f7SAl Viro str = ""; 3172e3fea3f7SAl Viro audit_size = 0; 3173e3fea3f7SAl Viro } 3174d6ea83ecSEric Paris ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR); 3175d6ea83ecSEric Paris audit_log_format(ab, "op=setxattr invalid_context="); 3176d6ea83ecSEric Paris audit_log_n_untrustedstring(ab, value, audit_size); 3177d6ea83ecSEric Paris audit_log_end(ab); 3178d6ea83ecSEric Paris 317912b29f34SStephen Smalley return rc; 3180d6ea83ecSEric Paris } 318112b29f34SStephen Smalley rc = security_context_to_sid_force(value, size, &newsid); 318212b29f34SStephen Smalley } 31831da177e4SLinus Torvalds if (rc) 31841da177e4SLinus Torvalds return rc; 31851da177e4SLinus Torvalds 3186275bb41eSDavid Howells rc = avc_has_perm(sid, newsid, isec->sclass, 31871da177e4SLinus Torvalds FILE__RELABELTO, &ad); 31881da177e4SLinus Torvalds if (rc) 31891da177e4SLinus Torvalds return rc; 31901da177e4SLinus Torvalds 3191275bb41eSDavid Howells rc = security_validate_transition(isec->sid, newsid, sid, 31921da177e4SLinus Torvalds isec->sclass); 31931da177e4SLinus Torvalds if (rc) 31941da177e4SLinus Torvalds return rc; 31951da177e4SLinus Torvalds 31961da177e4SLinus Torvalds return avc_has_perm(newsid, 31971da177e4SLinus Torvalds sbsec->sid, 31981da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 31991da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, 32001da177e4SLinus Torvalds &ad); 32011da177e4SLinus Torvalds } 32021da177e4SLinus Torvalds 32038f0cfa52SDavid Howells static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name, 32048f0cfa52SDavid Howells const void *value, size_t size, 32058f0cfa52SDavid Howells int flags) 32061da177e4SLinus Torvalds { 3207c6f493d6SDavid Howells struct inode *inode = d_backing_inode(dentry); 320820cdef8dSPaul Moore struct inode_security_struct *isec; 32091da177e4SLinus Torvalds u32 newsid; 32101da177e4SLinus Torvalds int rc; 32111da177e4SLinus Torvalds 32121da177e4SLinus Torvalds if (strcmp(name, XATTR_NAME_SELINUX)) { 32131da177e4SLinus Torvalds /* Not an attribute we recognize, so nothing to do. */ 32141da177e4SLinus Torvalds return; 32151da177e4SLinus Torvalds } 32161da177e4SLinus Torvalds 321712b29f34SStephen Smalley rc = security_context_to_sid_force(value, size, &newsid); 32181da177e4SLinus Torvalds if (rc) { 321912b29f34SStephen Smalley printk(KERN_ERR "SELinux: unable to map context to SID" 322012b29f34SStephen Smalley "for (%s, %lu), rc=%d\n", 322112b29f34SStephen Smalley inode->i_sb->s_id, inode->i_ino, -rc); 32221da177e4SLinus Torvalds return; 32231da177e4SLinus Torvalds } 32241da177e4SLinus Torvalds 322520cdef8dSPaul Moore isec = backing_inode_security(dentry); 32269287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 3227aa9c2669SDavid Quigley isec->sclass = inode_mode_to_security_class(inode->i_mode); 32281da177e4SLinus Torvalds isec->sid = newsid; 32296f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 32309287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 3231aa9c2669SDavid Quigley 32321da177e4SLinus Torvalds return; 32331da177e4SLinus Torvalds } 32341da177e4SLinus Torvalds 32358f0cfa52SDavid Howells static int selinux_inode_getxattr(struct dentry *dentry, const char *name) 32361da177e4SLinus Torvalds { 323788e67f3bSDavid Howells const struct cred *cred = current_cred(); 323888e67f3bSDavid Howells 32392875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__GETATTR); 32401da177e4SLinus Torvalds } 32411da177e4SLinus Torvalds 32421da177e4SLinus Torvalds static int selinux_inode_listxattr(struct dentry *dentry) 32431da177e4SLinus Torvalds { 324488e67f3bSDavid Howells const struct cred *cred = current_cred(); 324588e67f3bSDavid Howells 32462875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__GETATTR); 32471da177e4SLinus Torvalds } 32481da177e4SLinus Torvalds 32498f0cfa52SDavid Howells static int selinux_inode_removexattr(struct dentry *dentry, const char *name) 32501da177e4SLinus Torvalds { 3251b5376771SSerge E. Hallyn if (strcmp(name, XATTR_NAME_SELINUX)) 3252b5376771SSerge E. Hallyn return selinux_inode_setotherxattr(dentry, name); 32531da177e4SLinus Torvalds 32541da177e4SLinus Torvalds /* No one is allowed to remove a SELinux security label. 32551da177e4SLinus Torvalds You can change the label, but all data must be labeled. */ 32561da177e4SLinus Torvalds return -EACCES; 32571da177e4SLinus Torvalds } 32581da177e4SLinus Torvalds 3259d381d8a9SJames Morris /* 3260abc69bb6SStephen Smalley * Copy the inode security context value to the user. 3261d381d8a9SJames Morris * 3262d381d8a9SJames Morris * Permission check is handled by selinux_inode_getxattr hook. 3263d381d8a9SJames Morris */ 3264ea861dfdSAndreas Gruenbacher static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc) 32651da177e4SLinus Torvalds { 326642492594SDavid P. Quigley u32 size; 326742492594SDavid P. Quigley int error; 326842492594SDavid P. Quigley char *context = NULL; 326920cdef8dSPaul Moore struct inode_security_struct *isec; 32701da177e4SLinus Torvalds 32718c8570fbSDustin Kirkland if (strcmp(name, XATTR_SELINUX_SUFFIX)) 32728c8570fbSDustin Kirkland return -EOPNOTSUPP; 32731da177e4SLinus Torvalds 3274abc69bb6SStephen Smalley /* 3275abc69bb6SStephen Smalley * If the caller has CAP_MAC_ADMIN, then get the raw context 3276abc69bb6SStephen Smalley * value even if it is not defined by current policy; otherwise, 3277abc69bb6SStephen Smalley * use the in-core value under current policy. 3278abc69bb6SStephen Smalley * Use the non-auditing forms of the permission checks since 3279abc69bb6SStephen Smalley * getxattr may be called by unprivileged processes commonly 3280abc69bb6SStephen Smalley * and lack of permission just means that we fall back to the 3281abc69bb6SStephen Smalley * in-core context value, not a denial. 3282abc69bb6SStephen Smalley */ 328320cdef8dSPaul Moore isec = inode_security(inode); 3284db59000aSStephen Smalley if (has_cap_mac_admin(false)) 3285abc69bb6SStephen Smalley error = security_sid_to_context_force(isec->sid, &context, 3286abc69bb6SStephen Smalley &size); 3287abc69bb6SStephen Smalley else 328842492594SDavid P. Quigley error = security_sid_to_context(isec->sid, &context, &size); 328942492594SDavid P. Quigley if (error) 329042492594SDavid P. Quigley return error; 329142492594SDavid P. Quigley error = size; 329242492594SDavid P. Quigley if (alloc) { 329342492594SDavid P. Quigley *buffer = context; 329442492594SDavid P. Quigley goto out_nofree; 329542492594SDavid P. Quigley } 329642492594SDavid P. Quigley kfree(context); 329742492594SDavid P. Quigley out_nofree: 329842492594SDavid P. Quigley return error; 32991da177e4SLinus Torvalds } 33001da177e4SLinus Torvalds 33011da177e4SLinus Torvalds static int selinux_inode_setsecurity(struct inode *inode, const char *name, 33021da177e4SLinus Torvalds const void *value, size_t size, int flags) 33031da177e4SLinus Torvalds { 33042c97165bSPaul Moore struct inode_security_struct *isec = inode_security_novalidate(inode); 33051da177e4SLinus Torvalds u32 newsid; 33061da177e4SLinus Torvalds int rc; 33071da177e4SLinus Torvalds 33081da177e4SLinus Torvalds if (strcmp(name, XATTR_SELINUX_SUFFIX)) 33091da177e4SLinus Torvalds return -EOPNOTSUPP; 33101da177e4SLinus Torvalds 33111da177e4SLinus Torvalds if (!value || !size) 33121da177e4SLinus Torvalds return -EACCES; 33131da177e4SLinus Torvalds 331420ba96aeSRasmus Villemoes rc = security_context_to_sid(value, size, &newsid, GFP_KERNEL); 33151da177e4SLinus Torvalds if (rc) 33161da177e4SLinus Torvalds return rc; 33171da177e4SLinus Torvalds 33189287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 3319aa9c2669SDavid Quigley isec->sclass = inode_mode_to_security_class(inode->i_mode); 33201da177e4SLinus Torvalds isec->sid = newsid; 33216f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 33229287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 33231da177e4SLinus Torvalds return 0; 33241da177e4SLinus Torvalds } 33251da177e4SLinus Torvalds 33261da177e4SLinus Torvalds static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size) 33271da177e4SLinus Torvalds { 33281da177e4SLinus Torvalds const int len = sizeof(XATTR_NAME_SELINUX); 33291da177e4SLinus Torvalds if (buffer && len <= buffer_size) 33301da177e4SLinus Torvalds memcpy(buffer, XATTR_NAME_SELINUX, len); 33311da177e4SLinus Torvalds return len; 33321da177e4SLinus Torvalds } 33331da177e4SLinus Torvalds 3334d6335d77SAndreas Gruenbacher static void selinux_inode_getsecid(struct inode *inode, u32 *secid) 3335713a04aeSAhmed S. Darwish { 3336e817c2f3SAndreas Gruenbacher struct inode_security_struct *isec = inode_security_novalidate(inode); 3337713a04aeSAhmed S. Darwish *secid = isec->sid; 3338713a04aeSAhmed S. Darwish } 3339713a04aeSAhmed S. Darwish 334056909eb3SVivek Goyal static int selinux_inode_copy_up(struct dentry *src, struct cred **new) 334156909eb3SVivek Goyal { 334256909eb3SVivek Goyal u32 sid; 334356909eb3SVivek Goyal struct task_security_struct *tsec; 334456909eb3SVivek Goyal struct cred *new_creds = *new; 334556909eb3SVivek Goyal 334656909eb3SVivek Goyal if (new_creds == NULL) { 334756909eb3SVivek Goyal new_creds = prepare_creds(); 334856909eb3SVivek Goyal if (!new_creds) 334956909eb3SVivek Goyal return -ENOMEM; 335056909eb3SVivek Goyal } 335156909eb3SVivek Goyal 335256909eb3SVivek Goyal tsec = new_creds->security; 335356909eb3SVivek Goyal /* Get label from overlay inode and set it in create_sid */ 335456909eb3SVivek Goyal selinux_inode_getsecid(d_inode(src), &sid); 335556909eb3SVivek Goyal tsec->create_sid = sid; 335656909eb3SVivek Goyal *new = new_creds; 335756909eb3SVivek Goyal return 0; 335856909eb3SVivek Goyal } 335956909eb3SVivek Goyal 336019472b69SVivek Goyal static int selinux_inode_copy_up_xattr(const char *name) 336119472b69SVivek Goyal { 336219472b69SVivek Goyal /* The copy_up hook above sets the initial context on an inode, but we 336319472b69SVivek Goyal * don't then want to overwrite it by blindly copying all the lower 336419472b69SVivek Goyal * xattrs up. Instead, we have to filter out SELinux-related xattrs. 336519472b69SVivek Goyal */ 336619472b69SVivek Goyal if (strcmp(name, XATTR_NAME_SELINUX) == 0) 336719472b69SVivek Goyal return 1; /* Discard */ 336819472b69SVivek Goyal /* 336919472b69SVivek Goyal * Any other attribute apart from SELINUX is not claimed, supported 337019472b69SVivek Goyal * by selinux. 337119472b69SVivek Goyal */ 337219472b69SVivek Goyal return -EOPNOTSUPP; 337319472b69SVivek Goyal } 337419472b69SVivek Goyal 33751da177e4SLinus Torvalds /* file security operations */ 33761da177e4SLinus Torvalds 3377788e7dd4SYuichi Nakamura static int selinux_revalidate_file_permission(struct file *file, int mask) 33781da177e4SLinus Torvalds { 337988e67f3bSDavid Howells const struct cred *cred = current_cred(); 3380496ad9aaSAl Viro struct inode *inode = file_inode(file); 33811da177e4SLinus Torvalds 33821da177e4SLinus Torvalds /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */ 33831da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE)) 33841da177e4SLinus Torvalds mask |= MAY_APPEND; 33851da177e4SLinus Torvalds 3386389fb800SPaul Moore return file_has_perm(cred, file, 33871da177e4SLinus Torvalds file_mask_to_av(inode->i_mode, mask)); 33881da177e4SLinus Torvalds } 33891da177e4SLinus Torvalds 3390788e7dd4SYuichi Nakamura static int selinux_file_permission(struct file *file, int mask) 3391788e7dd4SYuichi Nakamura { 3392496ad9aaSAl Viro struct inode *inode = file_inode(file); 339320dda18bSStephen Smalley struct file_security_struct *fsec = file->f_security; 3394b197367eSAndreas Gruenbacher struct inode_security_struct *isec; 339520dda18bSStephen Smalley u32 sid = current_sid(); 339620dda18bSStephen Smalley 3397389fb800SPaul Moore if (!mask) 3398788e7dd4SYuichi Nakamura /* No permission to check. Existence test. */ 3399788e7dd4SYuichi Nakamura return 0; 3400788e7dd4SYuichi Nakamura 3401b197367eSAndreas Gruenbacher isec = inode_security(inode); 340220dda18bSStephen Smalley if (sid == fsec->sid && fsec->isid == isec->sid && 340320dda18bSStephen Smalley fsec->pseqno == avc_policy_seqno()) 340483d49856SEric Paris /* No change since file_open check. */ 340520dda18bSStephen Smalley return 0; 340620dda18bSStephen Smalley 3407788e7dd4SYuichi Nakamura return selinux_revalidate_file_permission(file, mask); 3408788e7dd4SYuichi Nakamura } 3409788e7dd4SYuichi Nakamura 34101da177e4SLinus Torvalds static int selinux_file_alloc_security(struct file *file) 34111da177e4SLinus Torvalds { 34121da177e4SLinus Torvalds return file_alloc_security(file); 34131da177e4SLinus Torvalds } 34141da177e4SLinus Torvalds 34151da177e4SLinus Torvalds static void selinux_file_free_security(struct file *file) 34161da177e4SLinus Torvalds { 34171da177e4SLinus Torvalds file_free_security(file); 34181da177e4SLinus Torvalds } 34191da177e4SLinus Torvalds 3420fa1aa143SJeff Vander Stoep /* 3421fa1aa143SJeff Vander Stoep * Check whether a task has the ioctl permission and cmd 3422fa1aa143SJeff Vander Stoep * operation to an inode. 3423fa1aa143SJeff Vander Stoep */ 34241d2a168aSGeliang Tang static int ioctl_has_perm(const struct cred *cred, struct file *file, 3425fa1aa143SJeff Vander Stoep u32 requested, u16 cmd) 3426fa1aa143SJeff Vander Stoep { 3427fa1aa143SJeff Vander Stoep struct common_audit_data ad; 3428fa1aa143SJeff Vander Stoep struct file_security_struct *fsec = file->f_security; 3429fa1aa143SJeff Vander Stoep struct inode *inode = file_inode(file); 343020cdef8dSPaul Moore struct inode_security_struct *isec; 3431fa1aa143SJeff Vander Stoep struct lsm_ioctlop_audit ioctl; 3432fa1aa143SJeff Vander Stoep u32 ssid = cred_sid(cred); 3433fa1aa143SJeff Vander Stoep int rc; 3434fa1aa143SJeff Vander Stoep u8 driver = cmd >> 8; 3435fa1aa143SJeff Vander Stoep u8 xperm = cmd & 0xff; 3436fa1aa143SJeff Vander Stoep 3437fa1aa143SJeff Vander Stoep ad.type = LSM_AUDIT_DATA_IOCTL_OP; 3438fa1aa143SJeff Vander Stoep ad.u.op = &ioctl; 3439fa1aa143SJeff Vander Stoep ad.u.op->cmd = cmd; 3440fa1aa143SJeff Vander Stoep ad.u.op->path = file->f_path; 3441fa1aa143SJeff Vander Stoep 3442fa1aa143SJeff Vander Stoep if (ssid != fsec->sid) { 3443fa1aa143SJeff Vander Stoep rc = avc_has_perm(ssid, fsec->sid, 3444fa1aa143SJeff Vander Stoep SECCLASS_FD, 3445fa1aa143SJeff Vander Stoep FD__USE, 3446fa1aa143SJeff Vander Stoep &ad); 3447fa1aa143SJeff Vander Stoep if (rc) 3448fa1aa143SJeff Vander Stoep goto out; 3449fa1aa143SJeff Vander Stoep } 3450fa1aa143SJeff Vander Stoep 3451fa1aa143SJeff Vander Stoep if (unlikely(IS_PRIVATE(inode))) 3452fa1aa143SJeff Vander Stoep return 0; 3453fa1aa143SJeff Vander Stoep 345420cdef8dSPaul Moore isec = inode_security(inode); 3455fa1aa143SJeff Vander Stoep rc = avc_has_extended_perms(ssid, isec->sid, isec->sclass, 3456fa1aa143SJeff Vander Stoep requested, driver, xperm, &ad); 3457fa1aa143SJeff Vander Stoep out: 3458fa1aa143SJeff Vander Stoep return rc; 3459fa1aa143SJeff Vander Stoep } 3460fa1aa143SJeff Vander Stoep 34611da177e4SLinus Torvalds static int selinux_file_ioctl(struct file *file, unsigned int cmd, 34621da177e4SLinus Torvalds unsigned long arg) 34631da177e4SLinus Torvalds { 346488e67f3bSDavid Howells const struct cred *cred = current_cred(); 34650b24dcb7SEric Paris int error = 0; 34661da177e4SLinus Torvalds 34670b24dcb7SEric Paris switch (cmd) { 34680b24dcb7SEric Paris case FIONREAD: 34690b24dcb7SEric Paris /* fall through */ 34700b24dcb7SEric Paris case FIBMAP: 34710b24dcb7SEric Paris /* fall through */ 34720b24dcb7SEric Paris case FIGETBSZ: 34730b24dcb7SEric Paris /* fall through */ 34742f99c369SAl Viro case FS_IOC_GETFLAGS: 34750b24dcb7SEric Paris /* fall through */ 34762f99c369SAl Viro case FS_IOC_GETVERSION: 34770b24dcb7SEric Paris error = file_has_perm(cred, file, FILE__GETATTR); 34780b24dcb7SEric Paris break; 34791da177e4SLinus Torvalds 34802f99c369SAl Viro case FS_IOC_SETFLAGS: 34810b24dcb7SEric Paris /* fall through */ 34822f99c369SAl Viro case FS_IOC_SETVERSION: 34830b24dcb7SEric Paris error = file_has_perm(cred, file, FILE__SETATTR); 34840b24dcb7SEric Paris break; 34850b24dcb7SEric Paris 34860b24dcb7SEric Paris /* sys_ioctl() checks */ 34870b24dcb7SEric Paris case FIONBIO: 34880b24dcb7SEric Paris /* fall through */ 34890b24dcb7SEric Paris case FIOASYNC: 34900b24dcb7SEric Paris error = file_has_perm(cred, file, 0); 34910b24dcb7SEric Paris break; 34920b24dcb7SEric Paris 34930b24dcb7SEric Paris case KDSKBENT: 34940b24dcb7SEric Paris case KDSKBSENT: 34956a9de491SEric Paris error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG, 34968e4ff6f2SStephen Smalley SECURITY_CAP_AUDIT, true); 34970b24dcb7SEric Paris break; 34980b24dcb7SEric Paris 34990b24dcb7SEric Paris /* default case assumes that the command will go 35000b24dcb7SEric Paris * to the file's ioctl() function. 35010b24dcb7SEric Paris */ 35020b24dcb7SEric Paris default: 3503fa1aa143SJeff Vander Stoep error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd); 35040b24dcb7SEric Paris } 35050b24dcb7SEric Paris return error; 35061da177e4SLinus Torvalds } 35071da177e4SLinus Torvalds 3508fcaaade1SStephen Smalley static int default_noexec; 3509fcaaade1SStephen Smalley 35101da177e4SLinus Torvalds static int file_map_prot_check(struct file *file, unsigned long prot, int shared) 35111da177e4SLinus Torvalds { 351288e67f3bSDavid Howells const struct cred *cred = current_cred(); 3513be0554c9SStephen Smalley u32 sid = cred_sid(cred); 3514d84f4f99SDavid Howells int rc = 0; 351588e67f3bSDavid Howells 3516fcaaade1SStephen Smalley if (default_noexec && 3517892e8cacSStephen Smalley (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) || 3518892e8cacSStephen Smalley (!shared && (prot & PROT_WRITE)))) { 35191da177e4SLinus Torvalds /* 35201da177e4SLinus Torvalds * We are making executable an anonymous mapping or a 35211da177e4SLinus Torvalds * private file mapping that will also be writable. 35221da177e4SLinus Torvalds * This has an additional check. 35231da177e4SLinus Torvalds */ 3524be0554c9SStephen Smalley rc = avc_has_perm(sid, sid, SECCLASS_PROCESS, 3525be0554c9SStephen Smalley PROCESS__EXECMEM, NULL); 35261da177e4SLinus Torvalds if (rc) 3527d84f4f99SDavid Howells goto error; 35281da177e4SLinus Torvalds } 35291da177e4SLinus Torvalds 35301da177e4SLinus Torvalds if (file) { 35311da177e4SLinus Torvalds /* read access is always possible with a mapping */ 35321da177e4SLinus Torvalds u32 av = FILE__READ; 35331da177e4SLinus Torvalds 35341da177e4SLinus Torvalds /* write access only matters if the mapping is shared */ 35351da177e4SLinus Torvalds if (shared && (prot & PROT_WRITE)) 35361da177e4SLinus Torvalds av |= FILE__WRITE; 35371da177e4SLinus Torvalds 35381da177e4SLinus Torvalds if (prot & PROT_EXEC) 35391da177e4SLinus Torvalds av |= FILE__EXECUTE; 35401da177e4SLinus Torvalds 354188e67f3bSDavid Howells return file_has_perm(cred, file, av); 35421da177e4SLinus Torvalds } 3543d84f4f99SDavid Howells 3544d84f4f99SDavid Howells error: 3545d84f4f99SDavid Howells return rc; 35461da177e4SLinus Torvalds } 35471da177e4SLinus Torvalds 3548e5467859SAl Viro static int selinux_mmap_addr(unsigned long addr) 35491da177e4SLinus Torvalds { 3550b1d9e6b0SCasey Schaufler int rc = 0; 355198883bfdSPaul Moore 355298883bfdSPaul Moore if (addr < CONFIG_LSM_MMAP_MIN_ADDR) { 355398883bfdSPaul Moore u32 sid = current_sid(); 355498883bfdSPaul Moore rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT, 355598883bfdSPaul Moore MEMPROTECT__MMAP_ZERO, NULL); 355698883bfdSPaul Moore } 355798883bfdSPaul Moore 355898883bfdSPaul Moore return rc; 3559e5467859SAl Viro } 35601da177e4SLinus Torvalds 3561e5467859SAl Viro static int selinux_mmap_file(struct file *file, unsigned long reqprot, 3562e5467859SAl Viro unsigned long prot, unsigned long flags) 3563e5467859SAl Viro { 35643ba4bf5fSStephen Smalley struct common_audit_data ad; 35653ba4bf5fSStephen Smalley int rc; 35663ba4bf5fSStephen Smalley 35673ba4bf5fSStephen Smalley if (file) { 35683ba4bf5fSStephen Smalley ad.type = LSM_AUDIT_DATA_FILE; 35693ba4bf5fSStephen Smalley ad.u.file = file; 35703ba4bf5fSStephen Smalley rc = inode_has_perm(current_cred(), file_inode(file), 35713ba4bf5fSStephen Smalley FILE__MAP, &ad); 35723ba4bf5fSStephen Smalley if (rc) 35733ba4bf5fSStephen Smalley return rc; 35743ba4bf5fSStephen Smalley } 35753ba4bf5fSStephen Smalley 35761da177e4SLinus Torvalds if (selinux_checkreqprot) 35771da177e4SLinus Torvalds prot = reqprot; 35781da177e4SLinus Torvalds 35791da177e4SLinus Torvalds return file_map_prot_check(file, prot, 35801da177e4SLinus Torvalds (flags & MAP_TYPE) == MAP_SHARED); 35811da177e4SLinus Torvalds } 35821da177e4SLinus Torvalds 35831da177e4SLinus Torvalds static int selinux_file_mprotect(struct vm_area_struct *vma, 35841da177e4SLinus Torvalds unsigned long reqprot, 35851da177e4SLinus Torvalds unsigned long prot) 35861da177e4SLinus Torvalds { 358788e67f3bSDavid Howells const struct cred *cred = current_cred(); 3588be0554c9SStephen Smalley u32 sid = cred_sid(cred); 35891da177e4SLinus Torvalds 35901da177e4SLinus Torvalds if (selinux_checkreqprot) 35911da177e4SLinus Torvalds prot = reqprot; 35921da177e4SLinus Torvalds 3593fcaaade1SStephen Smalley if (default_noexec && 3594fcaaade1SStephen Smalley (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) { 3595d541bbeeSJames Morris int rc = 0; 3596db4c9641SStephen Smalley if (vma->vm_start >= vma->vm_mm->start_brk && 3597db4c9641SStephen Smalley vma->vm_end <= vma->vm_mm->brk) { 3598be0554c9SStephen Smalley rc = avc_has_perm(sid, sid, SECCLASS_PROCESS, 3599be0554c9SStephen Smalley PROCESS__EXECHEAP, NULL); 3600db4c9641SStephen Smalley } else if (!vma->vm_file && 3601c2316dbfSStephen Smalley ((vma->vm_start <= vma->vm_mm->start_stack && 3602c2316dbfSStephen Smalley vma->vm_end >= vma->vm_mm->start_stack) || 3603d17af505SAndy Lutomirski vma_is_stack_for_current(vma))) { 3604be0554c9SStephen Smalley rc = avc_has_perm(sid, sid, SECCLASS_PROCESS, 3605be0554c9SStephen Smalley PROCESS__EXECSTACK, NULL); 3606db4c9641SStephen Smalley } else if (vma->vm_file && vma->anon_vma) { 3607db4c9641SStephen Smalley /* 3608db4c9641SStephen Smalley * We are making executable a file mapping that has 3609db4c9641SStephen Smalley * had some COW done. Since pages might have been 3610db4c9641SStephen Smalley * written, check ability to execute the possibly 3611db4c9641SStephen Smalley * modified content. This typically should only 3612db4c9641SStephen Smalley * occur for text relocations. 3613db4c9641SStephen Smalley */ 3614d84f4f99SDavid Howells rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD); 3615db4c9641SStephen Smalley } 36166b992197SLorenzo Hernandez García-Hierro if (rc) 36176b992197SLorenzo Hernandez García-Hierro return rc; 36186b992197SLorenzo Hernandez García-Hierro } 36191da177e4SLinus Torvalds 36201da177e4SLinus Torvalds return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED); 36211da177e4SLinus Torvalds } 36221da177e4SLinus Torvalds 36231da177e4SLinus Torvalds static int selinux_file_lock(struct file *file, unsigned int cmd) 36241da177e4SLinus Torvalds { 362588e67f3bSDavid Howells const struct cred *cred = current_cred(); 362688e67f3bSDavid Howells 362788e67f3bSDavid Howells return file_has_perm(cred, file, FILE__LOCK); 36281da177e4SLinus Torvalds } 36291da177e4SLinus Torvalds 36301da177e4SLinus Torvalds static int selinux_file_fcntl(struct file *file, unsigned int cmd, 36311da177e4SLinus Torvalds unsigned long arg) 36321da177e4SLinus Torvalds { 363388e67f3bSDavid Howells const struct cred *cred = current_cred(); 36341da177e4SLinus Torvalds int err = 0; 36351da177e4SLinus Torvalds 36361da177e4SLinus Torvalds switch (cmd) { 36371da177e4SLinus Torvalds case F_SETFL: 36381da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) { 363988e67f3bSDavid Howells err = file_has_perm(cred, file, FILE__WRITE); 36401da177e4SLinus Torvalds break; 36411da177e4SLinus Torvalds } 36421da177e4SLinus Torvalds /* fall through */ 36431da177e4SLinus Torvalds case F_SETOWN: 36441da177e4SLinus Torvalds case F_SETSIG: 36451da177e4SLinus Torvalds case F_GETFL: 36461da177e4SLinus Torvalds case F_GETOWN: 36471da177e4SLinus Torvalds case F_GETSIG: 36481d151c33SCyrill Gorcunov case F_GETOWNER_UIDS: 36491da177e4SLinus Torvalds /* Just check FD__USE permission */ 365088e67f3bSDavid Howells err = file_has_perm(cred, file, 0); 36511da177e4SLinus Torvalds break; 36521da177e4SLinus Torvalds case F_GETLK: 36531da177e4SLinus Torvalds case F_SETLK: 36541da177e4SLinus Torvalds case F_SETLKW: 36550d3f7a2dSJeff Layton case F_OFD_GETLK: 36560d3f7a2dSJeff Layton case F_OFD_SETLK: 36570d3f7a2dSJeff Layton case F_OFD_SETLKW: 36581da177e4SLinus Torvalds #if BITS_PER_LONG == 32 36591da177e4SLinus Torvalds case F_GETLK64: 36601da177e4SLinus Torvalds case F_SETLK64: 36611da177e4SLinus Torvalds case F_SETLKW64: 36621da177e4SLinus Torvalds #endif 366388e67f3bSDavid Howells err = file_has_perm(cred, file, FILE__LOCK); 36641da177e4SLinus Torvalds break; 36651da177e4SLinus Torvalds } 36661da177e4SLinus Torvalds 36671da177e4SLinus Torvalds return err; 36681da177e4SLinus Torvalds } 36691da177e4SLinus Torvalds 3670e0b93eddSJeff Layton static void selinux_file_set_fowner(struct file *file) 36711da177e4SLinus Torvalds { 36721da177e4SLinus Torvalds struct file_security_struct *fsec; 36731da177e4SLinus Torvalds 36741da177e4SLinus Torvalds fsec = file->f_security; 3675275bb41eSDavid Howells fsec->fown_sid = current_sid(); 36761da177e4SLinus Torvalds } 36771da177e4SLinus Torvalds 36781da177e4SLinus Torvalds static int selinux_file_send_sigiotask(struct task_struct *tsk, 36791da177e4SLinus Torvalds struct fown_struct *fown, int signum) 36801da177e4SLinus Torvalds { 36811da177e4SLinus Torvalds struct file *file; 368265c90bcaSStephen Smalley u32 sid = task_sid(tsk); 36831da177e4SLinus Torvalds u32 perm; 36841da177e4SLinus Torvalds struct file_security_struct *fsec; 36851da177e4SLinus Torvalds 36861da177e4SLinus Torvalds /* struct fown_struct is never outside the context of a struct file */ 3687b385a144SRobert P. J. Day file = container_of(fown, struct file, f_owner); 36881da177e4SLinus Torvalds 36891da177e4SLinus Torvalds fsec = file->f_security; 36901da177e4SLinus Torvalds 36911da177e4SLinus Torvalds if (!signum) 36921da177e4SLinus Torvalds perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */ 36931da177e4SLinus Torvalds else 36941da177e4SLinus Torvalds perm = signal_to_av(signum); 36951da177e4SLinus Torvalds 3696275bb41eSDavid Howells return avc_has_perm(fsec->fown_sid, sid, 36971da177e4SLinus Torvalds SECCLASS_PROCESS, perm, NULL); 36981da177e4SLinus Torvalds } 36991da177e4SLinus Torvalds 37001da177e4SLinus Torvalds static int selinux_file_receive(struct file *file) 37011da177e4SLinus Torvalds { 370288e67f3bSDavid Howells const struct cred *cred = current_cred(); 370388e67f3bSDavid Howells 370488e67f3bSDavid Howells return file_has_perm(cred, file, file_to_av(file)); 37051da177e4SLinus Torvalds } 37061da177e4SLinus Torvalds 370783d49856SEric Paris static int selinux_file_open(struct file *file, const struct cred *cred) 3708788e7dd4SYuichi Nakamura { 3709788e7dd4SYuichi Nakamura struct file_security_struct *fsec; 3710788e7dd4SYuichi Nakamura struct inode_security_struct *isec; 3711d84f4f99SDavid Howells 3712788e7dd4SYuichi Nakamura fsec = file->f_security; 371383da53c5SAndreas Gruenbacher isec = inode_security(file_inode(file)); 3714788e7dd4SYuichi Nakamura /* 3715788e7dd4SYuichi Nakamura * Save inode label and policy sequence number 3716788e7dd4SYuichi Nakamura * at open-time so that selinux_file_permission 3717788e7dd4SYuichi Nakamura * can determine whether revalidation is necessary. 3718788e7dd4SYuichi Nakamura * Task label is already saved in the file security 3719788e7dd4SYuichi Nakamura * struct as its SID. 3720788e7dd4SYuichi Nakamura */ 3721788e7dd4SYuichi Nakamura fsec->isid = isec->sid; 3722788e7dd4SYuichi Nakamura fsec->pseqno = avc_policy_seqno(); 3723788e7dd4SYuichi Nakamura /* 3724788e7dd4SYuichi Nakamura * Since the inode label or policy seqno may have changed 3725788e7dd4SYuichi Nakamura * between the selinux_inode_permission check and the saving 3726788e7dd4SYuichi Nakamura * of state above, recheck that access is still permitted. 3727788e7dd4SYuichi Nakamura * Otherwise, access might never be revalidated against the 3728788e7dd4SYuichi Nakamura * new inode label or new policy. 3729788e7dd4SYuichi Nakamura * This check is not redundant - do not remove. 3730788e7dd4SYuichi Nakamura */ 373113f8e981SDavid Howells return file_path_has_perm(cred, file, open_file_to_av(file)); 3732788e7dd4SYuichi Nakamura } 3733788e7dd4SYuichi Nakamura 37341da177e4SLinus Torvalds /* task security operations */ 37351da177e4SLinus Torvalds 3736a79be238STetsuo Handa static int selinux_task_alloc(struct task_struct *task, 3737a79be238STetsuo Handa unsigned long clone_flags) 37381da177e4SLinus Torvalds { 3739be0554c9SStephen Smalley u32 sid = current_sid(); 3740be0554c9SStephen Smalley 3741be0554c9SStephen Smalley return avc_has_perm(sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL); 37421da177e4SLinus Torvalds } 37431da177e4SLinus Torvalds 3744f1752eecSDavid Howells /* 3745ee18d64cSDavid Howells * allocate the SELinux part of blank credentials 3746ee18d64cSDavid Howells */ 3747ee18d64cSDavid Howells static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp) 3748ee18d64cSDavid Howells { 3749ee18d64cSDavid Howells struct task_security_struct *tsec; 3750ee18d64cSDavid Howells 3751ee18d64cSDavid Howells tsec = kzalloc(sizeof(struct task_security_struct), gfp); 3752ee18d64cSDavid Howells if (!tsec) 3753ee18d64cSDavid Howells return -ENOMEM; 3754ee18d64cSDavid Howells 3755ee18d64cSDavid Howells cred->security = tsec; 3756ee18d64cSDavid Howells return 0; 3757ee18d64cSDavid Howells } 3758ee18d64cSDavid Howells 3759ee18d64cSDavid Howells /* 3760f1752eecSDavid Howells * detach and free the LSM part of a set of credentials 3761f1752eecSDavid Howells */ 3762f1752eecSDavid Howells static void selinux_cred_free(struct cred *cred) 37631da177e4SLinus Torvalds { 3764f1752eecSDavid Howells struct task_security_struct *tsec = cred->security; 3765e0e81739SDavid Howells 37662edeaa34STetsuo Handa /* 37672edeaa34STetsuo Handa * cred->security == NULL if security_cred_alloc_blank() or 37682edeaa34STetsuo Handa * security_prepare_creds() returned an error. 37692edeaa34STetsuo Handa */ 37702edeaa34STetsuo Handa BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE); 3771e0e81739SDavid Howells cred->security = (void *) 0x7UL; 3772f1752eecSDavid Howells kfree(tsec); 37731da177e4SLinus Torvalds } 37741da177e4SLinus Torvalds 3775d84f4f99SDavid Howells /* 3776d84f4f99SDavid Howells * prepare a new set of credentials for modification 3777d84f4f99SDavid Howells */ 3778d84f4f99SDavid Howells static int selinux_cred_prepare(struct cred *new, const struct cred *old, 3779d84f4f99SDavid Howells gfp_t gfp) 3780d84f4f99SDavid Howells { 3781d84f4f99SDavid Howells const struct task_security_struct *old_tsec; 3782d84f4f99SDavid Howells struct task_security_struct *tsec; 3783d84f4f99SDavid Howells 3784d84f4f99SDavid Howells old_tsec = old->security; 3785d84f4f99SDavid Howells 3786d84f4f99SDavid Howells tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp); 3787d84f4f99SDavid Howells if (!tsec) 3788d84f4f99SDavid Howells return -ENOMEM; 3789d84f4f99SDavid Howells 3790d84f4f99SDavid Howells new->security = tsec; 3791d84f4f99SDavid Howells return 0; 3792d84f4f99SDavid Howells } 3793d84f4f99SDavid Howells 3794d84f4f99SDavid Howells /* 3795ee18d64cSDavid Howells * transfer the SELinux data to a blank set of creds 3796ee18d64cSDavid Howells */ 3797ee18d64cSDavid Howells static void selinux_cred_transfer(struct cred *new, const struct cred *old) 3798ee18d64cSDavid Howells { 3799ee18d64cSDavid Howells const struct task_security_struct *old_tsec = old->security; 3800ee18d64cSDavid Howells struct task_security_struct *tsec = new->security; 3801ee18d64cSDavid Howells 3802ee18d64cSDavid Howells *tsec = *old_tsec; 3803ee18d64cSDavid Howells } 3804ee18d64cSDavid Howells 3805ee18d64cSDavid Howells /* 38063a3b7ce9SDavid Howells * set the security data for a kernel service 38073a3b7ce9SDavid Howells * - all the creation contexts are set to unlabelled 38083a3b7ce9SDavid Howells */ 38093a3b7ce9SDavid Howells static int selinux_kernel_act_as(struct cred *new, u32 secid) 38103a3b7ce9SDavid Howells { 38113a3b7ce9SDavid Howells struct task_security_struct *tsec = new->security; 38123a3b7ce9SDavid Howells u32 sid = current_sid(); 38133a3b7ce9SDavid Howells int ret; 38143a3b7ce9SDavid Howells 38153a3b7ce9SDavid Howells ret = avc_has_perm(sid, secid, 38163a3b7ce9SDavid Howells SECCLASS_KERNEL_SERVICE, 38173a3b7ce9SDavid Howells KERNEL_SERVICE__USE_AS_OVERRIDE, 38183a3b7ce9SDavid Howells NULL); 38193a3b7ce9SDavid Howells if (ret == 0) { 38203a3b7ce9SDavid Howells tsec->sid = secid; 38213a3b7ce9SDavid Howells tsec->create_sid = 0; 38223a3b7ce9SDavid Howells tsec->keycreate_sid = 0; 38233a3b7ce9SDavid Howells tsec->sockcreate_sid = 0; 38243a3b7ce9SDavid Howells } 38253a3b7ce9SDavid Howells return ret; 38263a3b7ce9SDavid Howells } 38273a3b7ce9SDavid Howells 38283a3b7ce9SDavid Howells /* 38293a3b7ce9SDavid Howells * set the file creation context in a security record to the same as the 38303a3b7ce9SDavid Howells * objective context of the specified inode 38313a3b7ce9SDavid Howells */ 38323a3b7ce9SDavid Howells static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode) 38333a3b7ce9SDavid Howells { 383483da53c5SAndreas Gruenbacher struct inode_security_struct *isec = inode_security(inode); 38353a3b7ce9SDavid Howells struct task_security_struct *tsec = new->security; 38363a3b7ce9SDavid Howells u32 sid = current_sid(); 38373a3b7ce9SDavid Howells int ret; 38383a3b7ce9SDavid Howells 38393a3b7ce9SDavid Howells ret = avc_has_perm(sid, isec->sid, 38403a3b7ce9SDavid Howells SECCLASS_KERNEL_SERVICE, 38413a3b7ce9SDavid Howells KERNEL_SERVICE__CREATE_FILES_AS, 38423a3b7ce9SDavid Howells NULL); 38433a3b7ce9SDavid Howells 38443a3b7ce9SDavid Howells if (ret == 0) 38453a3b7ce9SDavid Howells tsec->create_sid = isec->sid; 3846ef57471aSDavid Howells return ret; 38473a3b7ce9SDavid Howells } 38483a3b7ce9SDavid Howells 3849dd8dbf2eSEric Paris static int selinux_kernel_module_request(char *kmod_name) 385025354c4fSEric Paris { 3851dd8dbf2eSEric Paris struct common_audit_data ad; 3852dd8dbf2eSEric Paris 385350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_KMOD; 3854dd8dbf2eSEric Paris ad.u.kmod_name = kmod_name; 3855dd8dbf2eSEric Paris 3856be0554c9SStephen Smalley return avc_has_perm(current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM, 3857dd8dbf2eSEric Paris SYSTEM__MODULE_REQUEST, &ad); 385825354c4fSEric Paris } 385925354c4fSEric Paris 386061d612eaSJeff Vander Stoep static int selinux_kernel_module_from_file(struct file *file) 386161d612eaSJeff Vander Stoep { 386261d612eaSJeff Vander Stoep struct common_audit_data ad; 386361d612eaSJeff Vander Stoep struct inode_security_struct *isec; 386461d612eaSJeff Vander Stoep struct file_security_struct *fsec; 386561d612eaSJeff Vander Stoep u32 sid = current_sid(); 386661d612eaSJeff Vander Stoep int rc; 386761d612eaSJeff Vander Stoep 386861d612eaSJeff Vander Stoep /* init_module */ 386961d612eaSJeff Vander Stoep if (file == NULL) 387061d612eaSJeff Vander Stoep return avc_has_perm(sid, sid, SECCLASS_SYSTEM, 387161d612eaSJeff Vander Stoep SYSTEM__MODULE_LOAD, NULL); 387261d612eaSJeff Vander Stoep 387361d612eaSJeff Vander Stoep /* finit_module */ 387420cdef8dSPaul Moore 387543af5de7SVivek Goyal ad.type = LSM_AUDIT_DATA_FILE; 387643af5de7SVivek Goyal ad.u.file = file; 387761d612eaSJeff Vander Stoep 387861d612eaSJeff Vander Stoep fsec = file->f_security; 387961d612eaSJeff Vander Stoep if (sid != fsec->sid) { 388061d612eaSJeff Vander Stoep rc = avc_has_perm(sid, fsec->sid, SECCLASS_FD, FD__USE, &ad); 388161d612eaSJeff Vander Stoep if (rc) 388261d612eaSJeff Vander Stoep return rc; 388361d612eaSJeff Vander Stoep } 388461d612eaSJeff Vander Stoep 388520cdef8dSPaul Moore isec = inode_security(file_inode(file)); 388661d612eaSJeff Vander Stoep return avc_has_perm(sid, isec->sid, SECCLASS_SYSTEM, 388761d612eaSJeff Vander Stoep SYSTEM__MODULE_LOAD, &ad); 388861d612eaSJeff Vander Stoep } 388961d612eaSJeff Vander Stoep 389061d612eaSJeff Vander Stoep static int selinux_kernel_read_file(struct file *file, 389161d612eaSJeff Vander Stoep enum kernel_read_file_id id) 389261d612eaSJeff Vander Stoep { 389361d612eaSJeff Vander Stoep int rc = 0; 389461d612eaSJeff Vander Stoep 389561d612eaSJeff Vander Stoep switch (id) { 389661d612eaSJeff Vander Stoep case READING_MODULE: 389761d612eaSJeff Vander Stoep rc = selinux_kernel_module_from_file(file); 389861d612eaSJeff Vander Stoep break; 389961d612eaSJeff Vander Stoep default: 390061d612eaSJeff Vander Stoep break; 390161d612eaSJeff Vander Stoep } 390261d612eaSJeff Vander Stoep 390361d612eaSJeff Vander Stoep return rc; 390461d612eaSJeff Vander Stoep } 390561d612eaSJeff Vander Stoep 39061da177e4SLinus Torvalds static int selinux_task_setpgid(struct task_struct *p, pid_t pgid) 39071da177e4SLinus Torvalds { 3908be0554c9SStephen Smalley return avc_has_perm(current_sid(), task_sid(p), SECCLASS_PROCESS, 3909be0554c9SStephen Smalley PROCESS__SETPGID, NULL); 39101da177e4SLinus Torvalds } 39111da177e4SLinus Torvalds 39121da177e4SLinus Torvalds static int selinux_task_getpgid(struct task_struct *p) 39131da177e4SLinus Torvalds { 3914be0554c9SStephen Smalley return avc_has_perm(current_sid(), task_sid(p), SECCLASS_PROCESS, 3915be0554c9SStephen Smalley PROCESS__GETPGID, NULL); 39161da177e4SLinus Torvalds } 39171da177e4SLinus Torvalds 39181da177e4SLinus Torvalds static int selinux_task_getsid(struct task_struct *p) 39191da177e4SLinus Torvalds { 3920be0554c9SStephen Smalley return avc_has_perm(current_sid(), task_sid(p), SECCLASS_PROCESS, 3921be0554c9SStephen Smalley PROCESS__GETSESSION, NULL); 39221da177e4SLinus Torvalds } 39231da177e4SLinus Torvalds 3924f9008e4cSDavid Quigley static void selinux_task_getsecid(struct task_struct *p, u32 *secid) 3925f9008e4cSDavid Quigley { 3926275bb41eSDavid Howells *secid = task_sid(p); 3927f9008e4cSDavid Quigley } 3928f9008e4cSDavid Quigley 39291da177e4SLinus Torvalds static int selinux_task_setnice(struct task_struct *p, int nice) 39301da177e4SLinus Torvalds { 3931be0554c9SStephen Smalley return avc_has_perm(current_sid(), task_sid(p), SECCLASS_PROCESS, 3932be0554c9SStephen Smalley PROCESS__SETSCHED, NULL); 39331da177e4SLinus Torvalds } 39341da177e4SLinus Torvalds 393503e68060SJames Morris static int selinux_task_setioprio(struct task_struct *p, int ioprio) 393603e68060SJames Morris { 3937be0554c9SStephen Smalley return avc_has_perm(current_sid(), task_sid(p), SECCLASS_PROCESS, 3938be0554c9SStephen Smalley PROCESS__SETSCHED, NULL); 393903e68060SJames Morris } 394003e68060SJames Morris 3941a1836a42SDavid Quigley static int selinux_task_getioprio(struct task_struct *p) 3942a1836a42SDavid Quigley { 3943be0554c9SStephen Smalley return avc_has_perm(current_sid(), task_sid(p), SECCLASS_PROCESS, 3944be0554c9SStephen Smalley PROCESS__GETSCHED, NULL); 3945a1836a42SDavid Quigley } 3946a1836a42SDavid Quigley 3947791ec491SStephen Smalley int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred, 3948791ec491SStephen Smalley unsigned int flags) 3949791ec491SStephen Smalley { 3950791ec491SStephen Smalley u32 av = 0; 3951791ec491SStephen Smalley 395284e6885eSStephen Smalley if (!flags) 395384e6885eSStephen Smalley return 0; 3954791ec491SStephen Smalley if (flags & LSM_PRLIMIT_WRITE) 3955791ec491SStephen Smalley av |= PROCESS__SETRLIMIT; 3956791ec491SStephen Smalley if (flags & LSM_PRLIMIT_READ) 3957791ec491SStephen Smalley av |= PROCESS__GETRLIMIT; 3958791ec491SStephen Smalley return avc_has_perm(cred_sid(cred), cred_sid(tcred), 3959791ec491SStephen Smalley SECCLASS_PROCESS, av, NULL); 3960791ec491SStephen Smalley } 3961791ec491SStephen Smalley 39628fd00b4dSJiri Slaby static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource, 39638fd00b4dSJiri Slaby struct rlimit *new_rlim) 39641da177e4SLinus Torvalds { 39658fd00b4dSJiri Slaby struct rlimit *old_rlim = p->signal->rlim + resource; 39661da177e4SLinus Torvalds 39671da177e4SLinus Torvalds /* Control the ability to change the hard limit (whether 39681da177e4SLinus Torvalds lowering or raising it), so that the hard limit can 39691da177e4SLinus Torvalds later be used as a safe reset point for the soft limit 3970d84f4f99SDavid Howells upon context transitions. See selinux_bprm_committing_creds. */ 39711da177e4SLinus Torvalds if (old_rlim->rlim_max != new_rlim->rlim_max) 3972be0554c9SStephen Smalley return avc_has_perm(current_sid(), task_sid(p), 3973be0554c9SStephen Smalley SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL); 39741da177e4SLinus Torvalds 39751da177e4SLinus Torvalds return 0; 39761da177e4SLinus Torvalds } 39771da177e4SLinus Torvalds 3978b0ae1981SKOSAKI Motohiro static int selinux_task_setscheduler(struct task_struct *p) 39791da177e4SLinus Torvalds { 3980be0554c9SStephen Smalley return avc_has_perm(current_sid(), task_sid(p), SECCLASS_PROCESS, 3981be0554c9SStephen Smalley PROCESS__SETSCHED, NULL); 39821da177e4SLinus Torvalds } 39831da177e4SLinus Torvalds 39841da177e4SLinus Torvalds static int selinux_task_getscheduler(struct task_struct *p) 39851da177e4SLinus Torvalds { 3986be0554c9SStephen Smalley return avc_has_perm(current_sid(), task_sid(p), SECCLASS_PROCESS, 3987be0554c9SStephen Smalley PROCESS__GETSCHED, NULL); 39881da177e4SLinus Torvalds } 39891da177e4SLinus Torvalds 399035601547SDavid Quigley static int selinux_task_movememory(struct task_struct *p) 399135601547SDavid Quigley { 3992be0554c9SStephen Smalley return avc_has_perm(current_sid(), task_sid(p), SECCLASS_PROCESS, 3993be0554c9SStephen Smalley PROCESS__SETSCHED, NULL); 399435601547SDavid Quigley } 399535601547SDavid Quigley 3996f9008e4cSDavid Quigley static int selinux_task_kill(struct task_struct *p, struct siginfo *info, 3997f9008e4cSDavid Quigley int sig, u32 secid) 39981da177e4SLinus Torvalds { 39991da177e4SLinus Torvalds u32 perm; 40001da177e4SLinus Torvalds 40011da177e4SLinus Torvalds if (!sig) 40021da177e4SLinus Torvalds perm = PROCESS__SIGNULL; /* null signal; existence test */ 40031da177e4SLinus Torvalds else 40041da177e4SLinus Torvalds perm = signal_to_av(sig); 4005be0554c9SStephen Smalley if (!secid) 4006be0554c9SStephen Smalley secid = current_sid(); 4007be0554c9SStephen Smalley return avc_has_perm(secid, task_sid(p), SECCLASS_PROCESS, perm, NULL); 40081da177e4SLinus Torvalds } 40091da177e4SLinus Torvalds 40101da177e4SLinus Torvalds static void selinux_task_to_inode(struct task_struct *p, 40111da177e4SLinus Torvalds struct inode *inode) 40121da177e4SLinus Torvalds { 40131da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 4014275bb41eSDavid Howells u32 sid = task_sid(p); 40151da177e4SLinus Torvalds 40169287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 4017db978da8SAndreas Gruenbacher isec->sclass = inode_mode_to_security_class(inode->i_mode); 4018275bb41eSDavid Howells isec->sid = sid; 40196f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 40209287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 40211da177e4SLinus Torvalds } 40221da177e4SLinus Torvalds 40231da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 402467f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv4(struct sk_buff *skb, 40252bf49690SThomas Liu struct common_audit_data *ad, u8 *proto) 40261da177e4SLinus Torvalds { 40271da177e4SLinus Torvalds int offset, ihlen, ret = -EINVAL; 40281da177e4SLinus Torvalds struct iphdr _iph, *ih; 40291da177e4SLinus Torvalds 4030bbe735e4SArnaldo Carvalho de Melo offset = skb_network_offset(skb); 40311da177e4SLinus Torvalds ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph); 40321da177e4SLinus Torvalds if (ih == NULL) 40331da177e4SLinus Torvalds goto out; 40341da177e4SLinus Torvalds 40351da177e4SLinus Torvalds ihlen = ih->ihl * 4; 40361da177e4SLinus Torvalds if (ihlen < sizeof(_iph)) 40371da177e4SLinus Torvalds goto out; 40381da177e4SLinus Torvalds 403948c62af6SEric Paris ad->u.net->v4info.saddr = ih->saddr; 404048c62af6SEric Paris ad->u.net->v4info.daddr = ih->daddr; 40411da177e4SLinus Torvalds ret = 0; 40421da177e4SLinus Torvalds 404367f83cbfSVenkat Yekkirala if (proto) 404467f83cbfSVenkat Yekkirala *proto = ih->protocol; 404567f83cbfSVenkat Yekkirala 40461da177e4SLinus Torvalds switch (ih->protocol) { 40471da177e4SLinus Torvalds case IPPROTO_TCP: { 40481da177e4SLinus Torvalds struct tcphdr _tcph, *th; 40491da177e4SLinus Torvalds 40501da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 40511da177e4SLinus Torvalds break; 40521da177e4SLinus Torvalds 40531da177e4SLinus Torvalds offset += ihlen; 40541da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 40551da177e4SLinus Torvalds if (th == NULL) 40561da177e4SLinus Torvalds break; 40571da177e4SLinus Torvalds 405848c62af6SEric Paris ad->u.net->sport = th->source; 405948c62af6SEric Paris ad->u.net->dport = th->dest; 40601da177e4SLinus Torvalds break; 40611da177e4SLinus Torvalds } 40621da177e4SLinus Torvalds 40631da177e4SLinus Torvalds case IPPROTO_UDP: { 40641da177e4SLinus Torvalds struct udphdr _udph, *uh; 40651da177e4SLinus Torvalds 40661da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 40671da177e4SLinus Torvalds break; 40681da177e4SLinus Torvalds 40691da177e4SLinus Torvalds offset += ihlen; 40701da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 40711da177e4SLinus Torvalds if (uh == NULL) 40721da177e4SLinus Torvalds break; 40731da177e4SLinus Torvalds 407448c62af6SEric Paris ad->u.net->sport = uh->source; 407548c62af6SEric Paris ad->u.net->dport = uh->dest; 40761da177e4SLinus Torvalds break; 40771da177e4SLinus Torvalds } 40781da177e4SLinus Torvalds 40792ee92d46SJames Morris case IPPROTO_DCCP: { 40802ee92d46SJames Morris struct dccp_hdr _dccph, *dh; 40812ee92d46SJames Morris 40822ee92d46SJames Morris if (ntohs(ih->frag_off) & IP_OFFSET) 40832ee92d46SJames Morris break; 40842ee92d46SJames Morris 40852ee92d46SJames Morris offset += ihlen; 40862ee92d46SJames Morris dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); 40872ee92d46SJames Morris if (dh == NULL) 40882ee92d46SJames Morris break; 40892ee92d46SJames Morris 409048c62af6SEric Paris ad->u.net->sport = dh->dccph_sport; 409148c62af6SEric Paris ad->u.net->dport = dh->dccph_dport; 40922ee92d46SJames Morris break; 40932ee92d46SJames Morris } 40942ee92d46SJames Morris 40951da177e4SLinus Torvalds default: 40961da177e4SLinus Torvalds break; 40971da177e4SLinus Torvalds } 40981da177e4SLinus Torvalds out: 40991da177e4SLinus Torvalds return ret; 41001da177e4SLinus Torvalds } 41011da177e4SLinus Torvalds 41021a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 41031da177e4SLinus Torvalds 41041da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 410567f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv6(struct sk_buff *skb, 41062bf49690SThomas Liu struct common_audit_data *ad, u8 *proto) 41071da177e4SLinus Torvalds { 41081da177e4SLinus Torvalds u8 nexthdr; 41091da177e4SLinus Torvalds int ret = -EINVAL, offset; 41101da177e4SLinus Torvalds struct ipv6hdr _ipv6h, *ip6; 411175f2811cSJesse Gross __be16 frag_off; 41121da177e4SLinus Torvalds 4113bbe735e4SArnaldo Carvalho de Melo offset = skb_network_offset(skb); 41141da177e4SLinus Torvalds ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h); 41151da177e4SLinus Torvalds if (ip6 == NULL) 41161da177e4SLinus Torvalds goto out; 41171da177e4SLinus Torvalds 411848c62af6SEric Paris ad->u.net->v6info.saddr = ip6->saddr; 411948c62af6SEric Paris ad->u.net->v6info.daddr = ip6->daddr; 41201da177e4SLinus Torvalds ret = 0; 41211da177e4SLinus Torvalds 41221da177e4SLinus Torvalds nexthdr = ip6->nexthdr; 41231da177e4SLinus Torvalds offset += sizeof(_ipv6h); 412475f2811cSJesse Gross offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off); 41251da177e4SLinus Torvalds if (offset < 0) 41261da177e4SLinus Torvalds goto out; 41271da177e4SLinus Torvalds 412867f83cbfSVenkat Yekkirala if (proto) 412967f83cbfSVenkat Yekkirala *proto = nexthdr; 413067f83cbfSVenkat Yekkirala 41311da177e4SLinus Torvalds switch (nexthdr) { 41321da177e4SLinus Torvalds case IPPROTO_TCP: { 41331da177e4SLinus Torvalds struct tcphdr _tcph, *th; 41341da177e4SLinus Torvalds 41351da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 41361da177e4SLinus Torvalds if (th == NULL) 41371da177e4SLinus Torvalds break; 41381da177e4SLinus Torvalds 413948c62af6SEric Paris ad->u.net->sport = th->source; 414048c62af6SEric Paris ad->u.net->dport = th->dest; 41411da177e4SLinus Torvalds break; 41421da177e4SLinus Torvalds } 41431da177e4SLinus Torvalds 41441da177e4SLinus Torvalds case IPPROTO_UDP: { 41451da177e4SLinus Torvalds struct udphdr _udph, *uh; 41461da177e4SLinus Torvalds 41471da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 41481da177e4SLinus Torvalds if (uh == NULL) 41491da177e4SLinus Torvalds break; 41501da177e4SLinus Torvalds 415148c62af6SEric Paris ad->u.net->sport = uh->source; 415248c62af6SEric Paris ad->u.net->dport = uh->dest; 41531da177e4SLinus Torvalds break; 41541da177e4SLinus Torvalds } 41551da177e4SLinus Torvalds 41562ee92d46SJames Morris case IPPROTO_DCCP: { 41572ee92d46SJames Morris struct dccp_hdr _dccph, *dh; 41582ee92d46SJames Morris 41592ee92d46SJames Morris dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); 41602ee92d46SJames Morris if (dh == NULL) 41612ee92d46SJames Morris break; 41622ee92d46SJames Morris 416348c62af6SEric Paris ad->u.net->sport = dh->dccph_sport; 416448c62af6SEric Paris ad->u.net->dport = dh->dccph_dport; 41652ee92d46SJames Morris break; 41662ee92d46SJames Morris } 41672ee92d46SJames Morris 41681da177e4SLinus Torvalds /* includes fragments */ 41691da177e4SLinus Torvalds default: 41701da177e4SLinus Torvalds break; 41711da177e4SLinus Torvalds } 41721da177e4SLinus Torvalds out: 41731da177e4SLinus Torvalds return ret; 41741da177e4SLinus Torvalds } 41751da177e4SLinus Torvalds 41761da177e4SLinus Torvalds #endif /* IPV6 */ 41771da177e4SLinus Torvalds 41782bf49690SThomas Liu static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad, 4179cf9481e2SDavid Howells char **_addrp, int src, u8 *proto) 41801da177e4SLinus Torvalds { 4181cf9481e2SDavid Howells char *addrp; 4182cf9481e2SDavid Howells int ret; 41831da177e4SLinus Torvalds 418448c62af6SEric Paris switch (ad->u.net->family) { 41851da177e4SLinus Torvalds case PF_INET: 418667f83cbfSVenkat Yekkirala ret = selinux_parse_skb_ipv4(skb, ad, proto); 4187cf9481e2SDavid Howells if (ret) 4188cf9481e2SDavid Howells goto parse_error; 418948c62af6SEric Paris addrp = (char *)(src ? &ad->u.net->v4info.saddr : 419048c62af6SEric Paris &ad->u.net->v4info.daddr); 4191cf9481e2SDavid Howells goto okay; 41921da177e4SLinus Torvalds 41931a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 41941da177e4SLinus Torvalds case PF_INET6: 419567f83cbfSVenkat Yekkirala ret = selinux_parse_skb_ipv6(skb, ad, proto); 4196cf9481e2SDavid Howells if (ret) 4197cf9481e2SDavid Howells goto parse_error; 419848c62af6SEric Paris addrp = (char *)(src ? &ad->u.net->v6info.saddr : 419948c62af6SEric Paris &ad->u.net->v6info.daddr); 4200cf9481e2SDavid Howells goto okay; 42011da177e4SLinus Torvalds #endif /* IPV6 */ 42021da177e4SLinus Torvalds default: 4203cf9481e2SDavid Howells addrp = NULL; 4204cf9481e2SDavid Howells goto okay; 42051da177e4SLinus Torvalds } 42061da177e4SLinus Torvalds 4207cf9481e2SDavid Howells parse_error: 420871f1cb05SPaul Moore printk(KERN_WARNING 420971f1cb05SPaul Moore "SELinux: failure in selinux_parse_skb()," 421071f1cb05SPaul Moore " unable to parse packet\n"); 42111da177e4SLinus Torvalds return ret; 4212cf9481e2SDavid Howells 4213cf9481e2SDavid Howells okay: 4214cf9481e2SDavid Howells if (_addrp) 4215cf9481e2SDavid Howells *_addrp = addrp; 4216cf9481e2SDavid Howells return 0; 42171da177e4SLinus Torvalds } 42181da177e4SLinus Torvalds 42194f6a993fSPaul Moore /** 4220220deb96SPaul Moore * selinux_skb_peerlbl_sid - Determine the peer label of a packet 42214f6a993fSPaul Moore * @skb: the packet 422275e22910SPaul Moore * @family: protocol family 4223220deb96SPaul Moore * @sid: the packet's peer label SID 42244f6a993fSPaul Moore * 42254f6a993fSPaul Moore * Description: 4226220deb96SPaul Moore * Check the various different forms of network peer labeling and determine 4227220deb96SPaul Moore * the peer label/SID for the packet; most of the magic actually occurs in 4228220deb96SPaul Moore * the security server function security_net_peersid_cmp(). The function 4229220deb96SPaul Moore * returns zero if the value in @sid is valid (although it may be SECSID_NULL) 4230220deb96SPaul Moore * or -EACCES if @sid is invalid due to inconsistencies with the different 4231220deb96SPaul Moore * peer labels. 42324f6a993fSPaul Moore * 42334f6a993fSPaul Moore */ 4234220deb96SPaul Moore static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid) 42354f6a993fSPaul Moore { 423671f1cb05SPaul Moore int err; 42374f6a993fSPaul Moore u32 xfrm_sid; 42384f6a993fSPaul Moore u32 nlbl_sid; 4239220deb96SPaul Moore u32 nlbl_type; 42404f6a993fSPaul Moore 4241817eff71SPaul Moore err = selinux_xfrm_skb_sid(skb, &xfrm_sid); 4242bed4d7efSPaul Moore if (unlikely(err)) 4243bed4d7efSPaul Moore return -EACCES; 4244bed4d7efSPaul Moore err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid); 4245bed4d7efSPaul Moore if (unlikely(err)) 4246bed4d7efSPaul Moore return -EACCES; 4247220deb96SPaul Moore 424871f1cb05SPaul Moore err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid); 424971f1cb05SPaul Moore if (unlikely(err)) { 425071f1cb05SPaul Moore printk(KERN_WARNING 425171f1cb05SPaul Moore "SELinux: failure in selinux_skb_peerlbl_sid()," 425271f1cb05SPaul Moore " unable to determine packet's peer label\n"); 4253220deb96SPaul Moore return -EACCES; 425471f1cb05SPaul Moore } 4255220deb96SPaul Moore 4256220deb96SPaul Moore return 0; 42574f6a993fSPaul Moore } 42584f6a993fSPaul Moore 4259446b8024SPaul Moore /** 4260446b8024SPaul Moore * selinux_conn_sid - Determine the child socket label for a connection 4261446b8024SPaul Moore * @sk_sid: the parent socket's SID 4262446b8024SPaul Moore * @skb_sid: the packet's SID 4263446b8024SPaul Moore * @conn_sid: the resulting connection SID 4264446b8024SPaul Moore * 4265446b8024SPaul Moore * If @skb_sid is valid then the user:role:type information from @sk_sid is 4266446b8024SPaul Moore * combined with the MLS information from @skb_sid in order to create 4267446b8024SPaul Moore * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy 4268446b8024SPaul Moore * of @sk_sid. Returns zero on success, negative values on failure. 4269446b8024SPaul Moore * 4270446b8024SPaul Moore */ 4271446b8024SPaul Moore static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid) 4272446b8024SPaul Moore { 4273446b8024SPaul Moore int err = 0; 4274446b8024SPaul Moore 4275446b8024SPaul Moore if (skb_sid != SECSID_NULL) 4276446b8024SPaul Moore err = security_sid_mls_copy(sk_sid, skb_sid, conn_sid); 4277446b8024SPaul Moore else 4278446b8024SPaul Moore *conn_sid = sk_sid; 4279446b8024SPaul Moore 4280446b8024SPaul Moore return err; 4281446b8024SPaul Moore } 4282446b8024SPaul Moore 42831da177e4SLinus Torvalds /* socket security operations */ 4284d4f2d978SPaul Moore 42852ad18bdfSHarry Ciao static int socket_sockcreate_sid(const struct task_security_struct *tsec, 42862ad18bdfSHarry Ciao u16 secclass, u32 *socksid) 4287d4f2d978SPaul Moore { 42882ad18bdfSHarry Ciao if (tsec->sockcreate_sid > SECSID_NULL) { 42892ad18bdfSHarry Ciao *socksid = tsec->sockcreate_sid; 42902ad18bdfSHarry Ciao return 0; 42912ad18bdfSHarry Ciao } 42922ad18bdfSHarry Ciao 42932ad18bdfSHarry Ciao return security_transition_sid(tsec->sid, tsec->sid, secclass, NULL, 42942ad18bdfSHarry Ciao socksid); 4295d4f2d978SPaul Moore } 4296d4f2d978SPaul Moore 4297be0554c9SStephen Smalley static int sock_has_perm(struct sock *sk, u32 perms) 42981da177e4SLinus Torvalds { 4299253bfae6SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 43002bf49690SThomas Liu struct common_audit_data ad; 430148c62af6SEric Paris struct lsm_network_audit net = {0,}; 43021da177e4SLinus Torvalds 4303253bfae6SPaul Moore if (sksec->sid == SECINITSID_KERNEL) 4304253bfae6SPaul Moore return 0; 43051da177e4SLinus Torvalds 430650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 430748c62af6SEric Paris ad.u.net = &net; 430848c62af6SEric Paris ad.u.net->sk = sk; 43091da177e4SLinus Torvalds 4310be0554c9SStephen Smalley return avc_has_perm(current_sid(), sksec->sid, sksec->sclass, perms, 4311be0554c9SStephen Smalley &ad); 43121da177e4SLinus Torvalds } 43131da177e4SLinus Torvalds 43141da177e4SLinus Torvalds static int selinux_socket_create(int family, int type, 43151da177e4SLinus Torvalds int protocol, int kern) 43161da177e4SLinus Torvalds { 43175fb49870SPaul Moore const struct task_security_struct *tsec = current_security(); 4318d4f2d978SPaul Moore u32 newsid; 4319275bb41eSDavid Howells u16 secclass; 43202ad18bdfSHarry Ciao int rc; 43211da177e4SLinus Torvalds 43221da177e4SLinus Torvalds if (kern) 4323d4f2d978SPaul Moore return 0; 43241da177e4SLinus Torvalds 4325275bb41eSDavid Howells secclass = socket_type_to_security_class(family, type, protocol); 43262ad18bdfSHarry Ciao rc = socket_sockcreate_sid(tsec, secclass, &newsid); 43272ad18bdfSHarry Ciao if (rc) 43282ad18bdfSHarry Ciao return rc; 43292ad18bdfSHarry Ciao 4330d4f2d978SPaul Moore return avc_has_perm(tsec->sid, newsid, secclass, SOCKET__CREATE, NULL); 43311da177e4SLinus Torvalds } 43321da177e4SLinus Torvalds 43337420ed23SVenkat Yekkirala static int selinux_socket_post_create(struct socket *sock, int family, 43341da177e4SLinus Torvalds int type, int protocol, int kern) 43351da177e4SLinus Torvalds { 43365fb49870SPaul Moore const struct task_security_struct *tsec = current_security(); 43375d226df4SAndreas Gruenbacher struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock)); 4338892c141eSVenkat Yekkirala struct sk_security_struct *sksec; 43399287aed2SAndreas Gruenbacher u16 sclass = socket_type_to_security_class(family, type, protocol); 43409287aed2SAndreas Gruenbacher u32 sid = SECINITSID_KERNEL; 4341275bb41eSDavid Howells int err = 0; 4342275bb41eSDavid Howells 43439287aed2SAndreas Gruenbacher if (!kern) { 43449287aed2SAndreas Gruenbacher err = socket_sockcreate_sid(tsec, sclass, &sid); 43452ad18bdfSHarry Ciao if (err) 43462ad18bdfSHarry Ciao return err; 43472ad18bdfSHarry Ciao } 4348275bb41eSDavid Howells 43499287aed2SAndreas Gruenbacher isec->sclass = sclass; 43509287aed2SAndreas Gruenbacher isec->sid = sid; 43516f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 43521da177e4SLinus Torvalds 4353892c141eSVenkat Yekkirala if (sock->sk) { 4354892c141eSVenkat Yekkirala sksec = sock->sk->sk_security; 43559287aed2SAndreas Gruenbacher sksec->sclass = sclass; 43569287aed2SAndreas Gruenbacher sksec->sid = sid; 4357389fb800SPaul Moore err = selinux_netlbl_socket_post_create(sock->sk, family); 4358892c141eSVenkat Yekkirala } 4359892c141eSVenkat Yekkirala 43607420ed23SVenkat Yekkirala return err; 43611da177e4SLinus Torvalds } 43621da177e4SLinus Torvalds 43631da177e4SLinus Torvalds /* Range of port numbers used to automatically bind. 43641da177e4SLinus Torvalds Need to determine whether we should perform a name_bind 43651da177e4SLinus Torvalds permission check between the socket and the port number. */ 43661da177e4SLinus Torvalds 43671da177e4SLinus Torvalds static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen) 43681da177e4SLinus Torvalds { 4369253bfae6SPaul Moore struct sock *sk = sock->sk; 43701da177e4SLinus Torvalds u16 family; 43711da177e4SLinus Torvalds int err; 43721da177e4SLinus Torvalds 4373be0554c9SStephen Smalley err = sock_has_perm(sk, SOCKET__BIND); 43741da177e4SLinus Torvalds if (err) 43751da177e4SLinus Torvalds goto out; 43761da177e4SLinus Torvalds 43771da177e4SLinus Torvalds /* 43781da177e4SLinus Torvalds * If PF_INET or PF_INET6, check name_bind permission for the port. 437913402580SJames Morris * Multiple address binding for SCTP is not supported yet: we just 438013402580SJames Morris * check the first address now. 43811da177e4SLinus Torvalds */ 4382253bfae6SPaul Moore family = sk->sk_family; 43831da177e4SLinus Torvalds if (family == PF_INET || family == PF_INET6) { 43841da177e4SLinus Torvalds char *addrp; 4385253bfae6SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 43862bf49690SThomas Liu struct common_audit_data ad; 438748c62af6SEric Paris struct lsm_network_audit net = {0,}; 43881da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 43891da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 43901da177e4SLinus Torvalds unsigned short snum; 4391e399f982SJames Morris u32 sid, node_perm; 43921da177e4SLinus Torvalds 43931da177e4SLinus Torvalds if (family == PF_INET) { 4394e2f586bdSAlexander Potapenko if (addrlen < sizeof(struct sockaddr_in)) { 4395e2f586bdSAlexander Potapenko err = -EINVAL; 4396e2f586bdSAlexander Potapenko goto out; 4397e2f586bdSAlexander Potapenko } 43981da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 43991da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 44001da177e4SLinus Torvalds addrp = (char *)&addr4->sin_addr.s_addr; 44011da177e4SLinus Torvalds } else { 4402e2f586bdSAlexander Potapenko if (addrlen < SIN6_LEN_RFC2133) { 4403e2f586bdSAlexander Potapenko err = -EINVAL; 4404e2f586bdSAlexander Potapenko goto out; 4405e2f586bdSAlexander Potapenko } 44061da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 44071da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 44081da177e4SLinus Torvalds addrp = (char *)&addr6->sin6_addr.s6_addr; 44091da177e4SLinus Torvalds } 44101da177e4SLinus Torvalds 4411227b60f5SStephen Hemminger if (snum) { 4412227b60f5SStephen Hemminger int low, high; 4413227b60f5SStephen Hemminger 44140bbf87d8SEric W. Biederman inet_get_local_port_range(sock_net(sk), &low, &high); 4415227b60f5SStephen Hemminger 44164548b683SKrister Johansen if (snum < max(inet_prot_sock(sock_net(sk)), low) || 44174548b683SKrister Johansen snum > high) { 44183e112172SPaul Moore err = sel_netport_sid(sk->sk_protocol, 44193e112172SPaul Moore snum, &sid); 44201da177e4SLinus Torvalds if (err) 44211da177e4SLinus Torvalds goto out; 442250c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 442348c62af6SEric Paris ad.u.net = &net; 442448c62af6SEric Paris ad.u.net->sport = htons(snum); 442548c62af6SEric Paris ad.u.net->family = family; 4426253bfae6SPaul Moore err = avc_has_perm(sksec->sid, sid, 4427253bfae6SPaul Moore sksec->sclass, 44281da177e4SLinus Torvalds SOCKET__NAME_BIND, &ad); 44291da177e4SLinus Torvalds if (err) 44301da177e4SLinus Torvalds goto out; 44311da177e4SLinus Torvalds } 4432227b60f5SStephen Hemminger } 44331da177e4SLinus Torvalds 4434253bfae6SPaul Moore switch (sksec->sclass) { 443513402580SJames Morris case SECCLASS_TCP_SOCKET: 44361da177e4SLinus Torvalds node_perm = TCP_SOCKET__NODE_BIND; 44371da177e4SLinus Torvalds break; 44381da177e4SLinus Torvalds 443913402580SJames Morris case SECCLASS_UDP_SOCKET: 44401da177e4SLinus Torvalds node_perm = UDP_SOCKET__NODE_BIND; 44411da177e4SLinus Torvalds break; 44421da177e4SLinus Torvalds 44432ee92d46SJames Morris case SECCLASS_DCCP_SOCKET: 44442ee92d46SJames Morris node_perm = DCCP_SOCKET__NODE_BIND; 44452ee92d46SJames Morris break; 44462ee92d46SJames Morris 44471da177e4SLinus Torvalds default: 44481da177e4SLinus Torvalds node_perm = RAWIP_SOCKET__NODE_BIND; 44491da177e4SLinus Torvalds break; 44501da177e4SLinus Torvalds } 44511da177e4SLinus Torvalds 4452224dfbd8SPaul Moore err = sel_netnode_sid(addrp, family, &sid); 44531da177e4SLinus Torvalds if (err) 44541da177e4SLinus Torvalds goto out; 44551da177e4SLinus Torvalds 445650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 445748c62af6SEric Paris ad.u.net = &net; 445848c62af6SEric Paris ad.u.net->sport = htons(snum); 445948c62af6SEric Paris ad.u.net->family = family; 44601da177e4SLinus Torvalds 44611da177e4SLinus Torvalds if (family == PF_INET) 446248c62af6SEric Paris ad.u.net->v4info.saddr = addr4->sin_addr.s_addr; 44631da177e4SLinus Torvalds else 446448c62af6SEric Paris ad.u.net->v6info.saddr = addr6->sin6_addr; 44651da177e4SLinus Torvalds 4466253bfae6SPaul Moore err = avc_has_perm(sksec->sid, sid, 4467253bfae6SPaul Moore sksec->sclass, node_perm, &ad); 44681da177e4SLinus Torvalds if (err) 44691da177e4SLinus Torvalds goto out; 44701da177e4SLinus Torvalds } 44711da177e4SLinus Torvalds out: 44721da177e4SLinus Torvalds return err; 44731da177e4SLinus Torvalds } 44741da177e4SLinus Torvalds 44751da177e4SLinus Torvalds static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen) 44761da177e4SLinus Torvalds { 4477014ab19aSPaul Moore struct sock *sk = sock->sk; 4478253bfae6SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 44791da177e4SLinus Torvalds int err; 44801da177e4SLinus Torvalds 4481be0554c9SStephen Smalley err = sock_has_perm(sk, SOCKET__CONNECT); 44821da177e4SLinus Torvalds if (err) 44831da177e4SLinus Torvalds return err; 44841da177e4SLinus Torvalds 44851da177e4SLinus Torvalds /* 44862ee92d46SJames Morris * If a TCP or DCCP socket, check name_connect permission for the port. 44871da177e4SLinus Torvalds */ 4488253bfae6SPaul Moore if (sksec->sclass == SECCLASS_TCP_SOCKET || 4489253bfae6SPaul Moore sksec->sclass == SECCLASS_DCCP_SOCKET) { 44902bf49690SThomas Liu struct common_audit_data ad; 449148c62af6SEric Paris struct lsm_network_audit net = {0,}; 44921da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 44931da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 44941da177e4SLinus Torvalds unsigned short snum; 44952ee92d46SJames Morris u32 sid, perm; 44961da177e4SLinus Torvalds 44971da177e4SLinus Torvalds if (sk->sk_family == PF_INET) { 44981da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 4499911656f8SStephen Smalley if (addrlen < sizeof(struct sockaddr_in)) 45001da177e4SLinus Torvalds return -EINVAL; 45011da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 45021da177e4SLinus Torvalds } else { 45031da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 4504911656f8SStephen Smalley if (addrlen < SIN6_LEN_RFC2133) 45051da177e4SLinus Torvalds return -EINVAL; 45061da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 45071da177e4SLinus Torvalds } 45081da177e4SLinus Torvalds 45093e112172SPaul Moore err = sel_netport_sid(sk->sk_protocol, snum, &sid); 45101da177e4SLinus Torvalds if (err) 45111da177e4SLinus Torvalds goto out; 45121da177e4SLinus Torvalds 4513253bfae6SPaul Moore perm = (sksec->sclass == SECCLASS_TCP_SOCKET) ? 45142ee92d46SJames Morris TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT; 45152ee92d46SJames Morris 451650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 451748c62af6SEric Paris ad.u.net = &net; 451848c62af6SEric Paris ad.u.net->dport = htons(snum); 451948c62af6SEric Paris ad.u.net->family = sk->sk_family; 4520253bfae6SPaul Moore err = avc_has_perm(sksec->sid, sid, sksec->sclass, perm, &ad); 45211da177e4SLinus Torvalds if (err) 45221da177e4SLinus Torvalds goto out; 45231da177e4SLinus Torvalds } 45241da177e4SLinus Torvalds 4525014ab19aSPaul Moore err = selinux_netlbl_socket_connect(sk, address); 4526014ab19aSPaul Moore 45271da177e4SLinus Torvalds out: 45281da177e4SLinus Torvalds return err; 45291da177e4SLinus Torvalds } 45301da177e4SLinus Torvalds 45311da177e4SLinus Torvalds static int selinux_socket_listen(struct socket *sock, int backlog) 45321da177e4SLinus Torvalds { 4533be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__LISTEN); 45341da177e4SLinus Torvalds } 45351da177e4SLinus Torvalds 45361da177e4SLinus Torvalds static int selinux_socket_accept(struct socket *sock, struct socket *newsock) 45371da177e4SLinus Torvalds { 45381da177e4SLinus Torvalds int err; 45391da177e4SLinus Torvalds struct inode_security_struct *isec; 45401da177e4SLinus Torvalds struct inode_security_struct *newisec; 45419287aed2SAndreas Gruenbacher u16 sclass; 45429287aed2SAndreas Gruenbacher u32 sid; 45431da177e4SLinus Torvalds 4544be0554c9SStephen Smalley err = sock_has_perm(sock->sk, SOCKET__ACCEPT); 45451da177e4SLinus Torvalds if (err) 45461da177e4SLinus Torvalds return err; 45471da177e4SLinus Torvalds 45485d226df4SAndreas Gruenbacher isec = inode_security_novalidate(SOCK_INODE(sock)); 45499287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 45509287aed2SAndreas Gruenbacher sclass = isec->sclass; 45519287aed2SAndreas Gruenbacher sid = isec->sid; 45529287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 45539287aed2SAndreas Gruenbacher 45549287aed2SAndreas Gruenbacher newisec = inode_security_novalidate(SOCK_INODE(newsock)); 45559287aed2SAndreas Gruenbacher newisec->sclass = sclass; 45569287aed2SAndreas Gruenbacher newisec->sid = sid; 45576f3be9f5SAndreas Gruenbacher newisec->initialized = LABEL_INITIALIZED; 45581da177e4SLinus Torvalds 45591da177e4SLinus Torvalds return 0; 45601da177e4SLinus Torvalds } 45611da177e4SLinus Torvalds 45621da177e4SLinus Torvalds static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg, 45631da177e4SLinus Torvalds int size) 45641da177e4SLinus Torvalds { 4565be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__WRITE); 45661da177e4SLinus Torvalds } 45671da177e4SLinus Torvalds 45681da177e4SLinus Torvalds static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg, 45691da177e4SLinus Torvalds int size, int flags) 45701da177e4SLinus Torvalds { 4571be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__READ); 45721da177e4SLinus Torvalds } 45731da177e4SLinus Torvalds 45741da177e4SLinus Torvalds static int selinux_socket_getsockname(struct socket *sock) 45751da177e4SLinus Torvalds { 4576be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__GETATTR); 45771da177e4SLinus Torvalds } 45781da177e4SLinus Torvalds 45791da177e4SLinus Torvalds static int selinux_socket_getpeername(struct socket *sock) 45801da177e4SLinus Torvalds { 4581be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__GETATTR); 45821da177e4SLinus Torvalds } 45831da177e4SLinus Torvalds 45841da177e4SLinus Torvalds static int selinux_socket_setsockopt(struct socket *sock, int level, int optname) 45851da177e4SLinus Torvalds { 4586f8687afeSPaul Moore int err; 4587f8687afeSPaul Moore 4588be0554c9SStephen Smalley err = sock_has_perm(sock->sk, SOCKET__SETOPT); 4589f8687afeSPaul Moore if (err) 4590f8687afeSPaul Moore return err; 4591f8687afeSPaul Moore 4592f8687afeSPaul Moore return selinux_netlbl_socket_setsockopt(sock, level, optname); 45931da177e4SLinus Torvalds } 45941da177e4SLinus Torvalds 45951da177e4SLinus Torvalds static int selinux_socket_getsockopt(struct socket *sock, int level, 45961da177e4SLinus Torvalds int optname) 45971da177e4SLinus Torvalds { 4598be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__GETOPT); 45991da177e4SLinus Torvalds } 46001da177e4SLinus Torvalds 46011da177e4SLinus Torvalds static int selinux_socket_shutdown(struct socket *sock, int how) 46021da177e4SLinus Torvalds { 4603be0554c9SStephen Smalley return sock_has_perm(sock->sk, SOCKET__SHUTDOWN); 46041da177e4SLinus Torvalds } 46051da177e4SLinus Torvalds 46063610cda5SDavid S. Miller static int selinux_socket_unix_stream_connect(struct sock *sock, 46073610cda5SDavid S. Miller struct sock *other, 46081da177e4SLinus Torvalds struct sock *newsk) 46091da177e4SLinus Torvalds { 46103610cda5SDavid S. Miller struct sk_security_struct *sksec_sock = sock->sk_security; 46113610cda5SDavid S. Miller struct sk_security_struct *sksec_other = other->sk_security; 46124d1e2451SPaul Moore struct sk_security_struct *sksec_new = newsk->sk_security; 46132bf49690SThomas Liu struct common_audit_data ad; 461448c62af6SEric Paris struct lsm_network_audit net = {0,}; 46151da177e4SLinus Torvalds int err; 46161da177e4SLinus Torvalds 461750c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 461848c62af6SEric Paris ad.u.net = &net; 461948c62af6SEric Paris ad.u.net->sk = other; 46201da177e4SLinus Torvalds 46214d1e2451SPaul Moore err = avc_has_perm(sksec_sock->sid, sksec_other->sid, 46224d1e2451SPaul Moore sksec_other->sclass, 46231da177e4SLinus Torvalds UNIX_STREAM_SOCKET__CONNECTTO, &ad); 46241da177e4SLinus Torvalds if (err) 46251da177e4SLinus Torvalds return err; 46261da177e4SLinus Torvalds 46271da177e4SLinus Torvalds /* server child socket */ 46284d1e2451SPaul Moore sksec_new->peer_sid = sksec_sock->sid; 46294d1e2451SPaul Moore err = security_sid_mls_copy(sksec_other->sid, sksec_sock->sid, 46304d1e2451SPaul Moore &sksec_new->sid); 46314d1e2451SPaul Moore if (err) 46324237c75cSVenkat Yekkirala return err; 46334d1e2451SPaul Moore 46344d1e2451SPaul Moore /* connecting socket */ 46354d1e2451SPaul Moore sksec_sock->peer_sid = sksec_new->sid; 46364d1e2451SPaul Moore 46374d1e2451SPaul Moore return 0; 46381da177e4SLinus Torvalds } 46391da177e4SLinus Torvalds 46401da177e4SLinus Torvalds static int selinux_socket_unix_may_send(struct socket *sock, 46411da177e4SLinus Torvalds struct socket *other) 46421da177e4SLinus Torvalds { 4643253bfae6SPaul Moore struct sk_security_struct *ssec = sock->sk->sk_security; 4644253bfae6SPaul Moore struct sk_security_struct *osec = other->sk->sk_security; 46452bf49690SThomas Liu struct common_audit_data ad; 464648c62af6SEric Paris struct lsm_network_audit net = {0,}; 46471da177e4SLinus Torvalds 464850c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 464948c62af6SEric Paris ad.u.net = &net; 465048c62af6SEric Paris ad.u.net->sk = other->sk; 46511da177e4SLinus Torvalds 4652253bfae6SPaul Moore return avc_has_perm(ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO, 4653253bfae6SPaul Moore &ad); 46541da177e4SLinus Torvalds } 46551da177e4SLinus Torvalds 4656cbe0d6e8SPaul Moore static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex, 4657cbe0d6e8SPaul Moore char *addrp, u16 family, u32 peer_sid, 46582bf49690SThomas Liu struct common_audit_data *ad) 4659effad8dfSPaul Moore { 4660effad8dfSPaul Moore int err; 4661effad8dfSPaul Moore u32 if_sid; 4662effad8dfSPaul Moore u32 node_sid; 4663effad8dfSPaul Moore 4664cbe0d6e8SPaul Moore err = sel_netif_sid(ns, ifindex, &if_sid); 4665effad8dfSPaul Moore if (err) 4666effad8dfSPaul Moore return err; 4667effad8dfSPaul Moore err = avc_has_perm(peer_sid, if_sid, 4668effad8dfSPaul Moore SECCLASS_NETIF, NETIF__INGRESS, ad); 4669effad8dfSPaul Moore if (err) 4670effad8dfSPaul Moore return err; 4671effad8dfSPaul Moore 4672effad8dfSPaul Moore err = sel_netnode_sid(addrp, family, &node_sid); 4673effad8dfSPaul Moore if (err) 4674effad8dfSPaul Moore return err; 4675effad8dfSPaul Moore return avc_has_perm(peer_sid, node_sid, 4676effad8dfSPaul Moore SECCLASS_NODE, NODE__RECVFROM, ad); 4677effad8dfSPaul Moore } 4678effad8dfSPaul Moore 4679220deb96SPaul Moore static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb, 4680d8395c87SPaul Moore u16 family) 4681220deb96SPaul Moore { 4682277d342fSPaul Moore int err = 0; 4683220deb96SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 4684220deb96SPaul Moore u32 sk_sid = sksec->sid; 46852bf49690SThomas Liu struct common_audit_data ad; 468648c62af6SEric Paris struct lsm_network_audit net = {0,}; 4687d8395c87SPaul Moore char *addrp; 4688d8395c87SPaul Moore 468950c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 469048c62af6SEric Paris ad.u.net = &net; 469148c62af6SEric Paris ad.u.net->netif = skb->skb_iif; 469248c62af6SEric Paris ad.u.net->family = family; 4693d8395c87SPaul Moore err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); 4694d8395c87SPaul Moore if (err) 4695d8395c87SPaul Moore return err; 4696220deb96SPaul Moore 469758bfbb51SPaul Moore if (selinux_secmark_enabled()) { 4698220deb96SPaul Moore err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET, 4699d8395c87SPaul Moore PACKET__RECV, &ad); 4700220deb96SPaul Moore if (err) 4701220deb96SPaul Moore return err; 470258bfbb51SPaul Moore } 4703220deb96SPaul Moore 4704d8395c87SPaul Moore err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad); 4705220deb96SPaul Moore if (err) 4706220deb96SPaul Moore return err; 4707d8395c87SPaul Moore err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad); 4708220deb96SPaul Moore 47094e5ab4cbSJames Morris return err; 47104e5ab4cbSJames Morris } 4711d28d1e08STrent Jaeger 47124e5ab4cbSJames Morris static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) 47134e5ab4cbSJames Morris { 4714220deb96SPaul Moore int err; 47154237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 4716220deb96SPaul Moore u16 family = sk->sk_family; 4717220deb96SPaul Moore u32 sk_sid = sksec->sid; 47182bf49690SThomas Liu struct common_audit_data ad; 471948c62af6SEric Paris struct lsm_network_audit net = {0,}; 4720220deb96SPaul Moore char *addrp; 4721d8395c87SPaul Moore u8 secmark_active; 4722d8395c87SPaul Moore u8 peerlbl_active; 47234e5ab4cbSJames Morris 47244e5ab4cbSJames Morris if (family != PF_INET && family != PF_INET6) 4725220deb96SPaul Moore return 0; 47264e5ab4cbSJames Morris 47274e5ab4cbSJames Morris /* Handle mapped IPv4 packets arriving via IPv6 sockets */ 472887fcd70dSAl Viro if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 47294e5ab4cbSJames Morris family = PF_INET; 47304e5ab4cbSJames Morris 4731d8395c87SPaul Moore /* If any sort of compatibility mode is enabled then handoff processing 4732d8395c87SPaul Moore * to the selinux_sock_rcv_skb_compat() function to deal with the 4733d8395c87SPaul Moore * special handling. We do this in an attempt to keep this function 4734d8395c87SPaul Moore * as fast and as clean as possible. */ 473558bfbb51SPaul Moore if (!selinux_policycap_netpeer) 4736d8395c87SPaul Moore return selinux_sock_rcv_skb_compat(sk, skb, family); 4737d8395c87SPaul Moore 4738d8395c87SPaul Moore secmark_active = selinux_secmark_enabled(); 47392be4d74fSChris PeBenito peerlbl_active = selinux_peerlbl_enabled(); 4740d8395c87SPaul Moore if (!secmark_active && !peerlbl_active) 4741d8395c87SPaul Moore return 0; 4742d8395c87SPaul Moore 474350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 474448c62af6SEric Paris ad.u.net = &net; 474548c62af6SEric Paris ad.u.net->netif = skb->skb_iif; 474648c62af6SEric Paris ad.u.net->family = family; 4747224dfbd8SPaul Moore err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); 47484e5ab4cbSJames Morris if (err) 4749220deb96SPaul Moore return err; 47504e5ab4cbSJames Morris 4751d8395c87SPaul Moore if (peerlbl_active) { 4752d621d35eSPaul Moore u32 peer_sid; 4753220deb96SPaul Moore 4754220deb96SPaul Moore err = selinux_skb_peerlbl_sid(skb, family, &peer_sid); 4755220deb96SPaul Moore if (err) 4756220deb96SPaul Moore return err; 4757cbe0d6e8SPaul Moore err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif, 4758cbe0d6e8SPaul Moore addrp, family, peer_sid, &ad); 4759dfaebe98SPaul Moore if (err) { 4760a04e71f6SHuw Davies selinux_netlbl_err(skb, family, err, 0); 4761effad8dfSPaul Moore return err; 4762dfaebe98SPaul Moore } 4763d621d35eSPaul Moore err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER, 4764d621d35eSPaul Moore PEER__RECV, &ad); 476546d01d63SChad Hanson if (err) { 4766a04e71f6SHuw Davies selinux_netlbl_err(skb, family, err, 0); 476746d01d63SChad Hanson return err; 476846d01d63SChad Hanson } 4769d621d35eSPaul Moore } 4770d621d35eSPaul Moore 4771d8395c87SPaul Moore if (secmark_active) { 4772effad8dfSPaul Moore err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET, 4773effad8dfSPaul Moore PACKET__RECV, &ad); 4774effad8dfSPaul Moore if (err) 4775effad8dfSPaul Moore return err; 4776effad8dfSPaul Moore } 4777effad8dfSPaul Moore 4778d621d35eSPaul Moore return err; 47791da177e4SLinus Torvalds } 47801da177e4SLinus Torvalds 47812c7946a7SCatherine Zhang static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval, 47821da177e4SLinus Torvalds int __user *optlen, unsigned len) 47831da177e4SLinus Torvalds { 47841da177e4SLinus Torvalds int err = 0; 47851da177e4SLinus Torvalds char *scontext; 47861da177e4SLinus Torvalds u32 scontext_len; 4787253bfae6SPaul Moore struct sk_security_struct *sksec = sock->sk->sk_security; 47883de4bab5SPaul Moore u32 peer_sid = SECSID_NULL; 47891da177e4SLinus Torvalds 4790253bfae6SPaul Moore if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET || 4791253bfae6SPaul Moore sksec->sclass == SECCLASS_TCP_SOCKET) 4792dd3e7836SEric Paris peer_sid = sksec->peer_sid; 4793253bfae6SPaul Moore if (peer_sid == SECSID_NULL) 4794253bfae6SPaul Moore return -ENOPROTOOPT; 47951da177e4SLinus Torvalds 47962c7946a7SCatherine Zhang err = security_sid_to_context(peer_sid, &scontext, &scontext_len); 47971da177e4SLinus Torvalds if (err) 4798253bfae6SPaul Moore return err; 47991da177e4SLinus Torvalds 48001da177e4SLinus Torvalds if (scontext_len > len) { 48011da177e4SLinus Torvalds err = -ERANGE; 48021da177e4SLinus Torvalds goto out_len; 48031da177e4SLinus Torvalds } 48041da177e4SLinus Torvalds 48051da177e4SLinus Torvalds if (copy_to_user(optval, scontext, scontext_len)) 48061da177e4SLinus Torvalds err = -EFAULT; 48071da177e4SLinus Torvalds 48081da177e4SLinus Torvalds out_len: 48091da177e4SLinus Torvalds if (put_user(scontext_len, optlen)) 48101da177e4SLinus Torvalds err = -EFAULT; 48111da177e4SLinus Torvalds kfree(scontext); 48121da177e4SLinus Torvalds return err; 48131da177e4SLinus Torvalds } 48141da177e4SLinus Torvalds 4815dc49c1f9SCatherine Zhang static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid) 48162c7946a7SCatherine Zhang { 4817dc49c1f9SCatherine Zhang u32 peer_secid = SECSID_NULL; 481875e22910SPaul Moore u16 family; 4819899134f2SPaul Moore struct inode_security_struct *isec; 4820877ce7c1SCatherine Zhang 4821aa862900SPaul Moore if (skb && skb->protocol == htons(ETH_P_IP)) 4822aa862900SPaul Moore family = PF_INET; 4823aa862900SPaul Moore else if (skb && skb->protocol == htons(ETH_P_IPV6)) 4824aa862900SPaul Moore family = PF_INET6; 4825aa862900SPaul Moore else if (sock) 482675e22910SPaul Moore family = sock->sk->sk_family; 482775e22910SPaul Moore else 482875e22910SPaul Moore goto out; 482975e22910SPaul Moore 4830899134f2SPaul Moore if (sock && family == PF_UNIX) { 4831899134f2SPaul Moore isec = inode_security_novalidate(SOCK_INODE(sock)); 4832899134f2SPaul Moore peer_secid = isec->sid; 4833899134f2SPaul Moore } else if (skb) 4834220deb96SPaul Moore selinux_skb_peerlbl_sid(skb, family, &peer_secid); 48352c7946a7SCatherine Zhang 483675e22910SPaul Moore out: 4837dc49c1f9SCatherine Zhang *secid = peer_secid; 483875e22910SPaul Moore if (peer_secid == SECSID_NULL) 483975e22910SPaul Moore return -EINVAL; 484075e22910SPaul Moore return 0; 48412c7946a7SCatherine Zhang } 48422c7946a7SCatherine Zhang 48437d877f3bSAl Viro static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority) 48441da177e4SLinus Torvalds { 484584914b7eSPaul Moore struct sk_security_struct *sksec; 484684914b7eSPaul Moore 484784914b7eSPaul Moore sksec = kzalloc(sizeof(*sksec), priority); 484884914b7eSPaul Moore if (!sksec) 484984914b7eSPaul Moore return -ENOMEM; 485084914b7eSPaul Moore 485184914b7eSPaul Moore sksec->peer_sid = SECINITSID_UNLABELED; 485284914b7eSPaul Moore sksec->sid = SECINITSID_UNLABELED; 48535dee25d0SStephen Smalley sksec->sclass = SECCLASS_SOCKET; 485484914b7eSPaul Moore selinux_netlbl_sk_security_reset(sksec); 485584914b7eSPaul Moore sk->sk_security = sksec; 485684914b7eSPaul Moore 485784914b7eSPaul Moore return 0; 48581da177e4SLinus Torvalds } 48591da177e4SLinus Torvalds 48601da177e4SLinus Torvalds static void selinux_sk_free_security(struct sock *sk) 48611da177e4SLinus Torvalds { 486284914b7eSPaul Moore struct sk_security_struct *sksec = sk->sk_security; 486384914b7eSPaul Moore 486484914b7eSPaul Moore sk->sk_security = NULL; 486584914b7eSPaul Moore selinux_netlbl_sk_security_free(sksec); 486684914b7eSPaul Moore kfree(sksec); 48671da177e4SLinus Torvalds } 48681da177e4SLinus Torvalds 4869892c141eSVenkat Yekkirala static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk) 4870892c141eSVenkat Yekkirala { 4871dd3e7836SEric Paris struct sk_security_struct *sksec = sk->sk_security; 4872dd3e7836SEric Paris struct sk_security_struct *newsksec = newsk->sk_security; 4873892c141eSVenkat Yekkirala 4874dd3e7836SEric Paris newsksec->sid = sksec->sid; 4875dd3e7836SEric Paris newsksec->peer_sid = sksec->peer_sid; 4876dd3e7836SEric Paris newsksec->sclass = sksec->sclass; 487799f59ed0SPaul Moore 4878dd3e7836SEric Paris selinux_netlbl_sk_security_reset(newsksec); 4879892c141eSVenkat Yekkirala } 4880892c141eSVenkat Yekkirala 4881beb8d13bSVenkat Yekkirala static void selinux_sk_getsecid(struct sock *sk, u32 *secid) 4882d28d1e08STrent Jaeger { 4883d28d1e08STrent Jaeger if (!sk) 4884beb8d13bSVenkat Yekkirala *secid = SECINITSID_ANY_SOCKET; 4885892c141eSVenkat Yekkirala else { 4886892c141eSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 4887d28d1e08STrent Jaeger 4888beb8d13bSVenkat Yekkirala *secid = sksec->sid; 4889892c141eSVenkat Yekkirala } 4890d28d1e08STrent Jaeger } 4891d28d1e08STrent Jaeger 48929a673e56SAdrian Bunk static void selinux_sock_graft(struct sock *sk, struct socket *parent) 48934237c75cSVenkat Yekkirala { 48945d226df4SAndreas Gruenbacher struct inode_security_struct *isec = 48955d226df4SAndreas Gruenbacher inode_security_novalidate(SOCK_INODE(parent)); 48964237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 48974237c75cSVenkat Yekkirala 48982873ead7SPaul Moore if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 || 48992873ead7SPaul Moore sk->sk_family == PF_UNIX) 49004237c75cSVenkat Yekkirala isec->sid = sksec->sid; 4901220deb96SPaul Moore sksec->sclass = isec->sclass; 49024237c75cSVenkat Yekkirala } 49034237c75cSVenkat Yekkirala 49049a673e56SAdrian Bunk static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb, 49054237c75cSVenkat Yekkirala struct request_sock *req) 49064237c75cSVenkat Yekkirala { 49074237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 49084237c75cSVenkat Yekkirala int err; 49090b1f24e6SPaul Moore u16 family = req->rsk_ops->family; 4910446b8024SPaul Moore u32 connsid; 49114237c75cSVenkat Yekkirala u32 peersid; 49124237c75cSVenkat Yekkirala 4913aa862900SPaul Moore err = selinux_skb_peerlbl_sid(skb, family, &peersid); 4914220deb96SPaul Moore if (err) 4915220deb96SPaul Moore return err; 4916446b8024SPaul Moore err = selinux_conn_sid(sksec->sid, peersid, &connsid); 49174237c75cSVenkat Yekkirala if (err) 49184237c75cSVenkat Yekkirala return err; 4919446b8024SPaul Moore req->secid = connsid; 49206b877699SVenkat Yekkirala req->peer_secid = peersid; 4921389fb800SPaul Moore 4922389fb800SPaul Moore return selinux_netlbl_inet_conn_request(req, family); 49234237c75cSVenkat Yekkirala } 49244237c75cSVenkat Yekkirala 49259a673e56SAdrian Bunk static void selinux_inet_csk_clone(struct sock *newsk, 49269a673e56SAdrian Bunk const struct request_sock *req) 49274237c75cSVenkat Yekkirala { 49284237c75cSVenkat Yekkirala struct sk_security_struct *newsksec = newsk->sk_security; 49294237c75cSVenkat Yekkirala 49304237c75cSVenkat Yekkirala newsksec->sid = req->secid; 49316b877699SVenkat Yekkirala newsksec->peer_sid = req->peer_secid; 49324237c75cSVenkat Yekkirala /* NOTE: Ideally, we should also get the isec->sid for the 49334237c75cSVenkat Yekkirala new socket in sync, but we don't have the isec available yet. 49344237c75cSVenkat Yekkirala So we will wait until sock_graft to do it, by which 49354237c75cSVenkat Yekkirala time it will have been created and available. */ 493699f59ed0SPaul Moore 49379f2ad665SPaul Moore /* We don't need to take any sort of lock here as we are the only 49389f2ad665SPaul Moore * thread with access to newsksec */ 4939389fb800SPaul Moore selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family); 49404237c75cSVenkat Yekkirala } 49414237c75cSVenkat Yekkirala 4942014ab19aSPaul Moore static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb) 49436b877699SVenkat Yekkirala { 4944aa862900SPaul Moore u16 family = sk->sk_family; 49456b877699SVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 49466b877699SVenkat Yekkirala 4947aa862900SPaul Moore /* handle mapped IPv4 packets arriving via IPv6 sockets */ 4948aa862900SPaul Moore if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 4949aa862900SPaul Moore family = PF_INET; 4950aa862900SPaul Moore 4951aa862900SPaul Moore selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid); 49526b877699SVenkat Yekkirala } 49536b877699SVenkat Yekkirala 49542606fd1fSEric Paris static int selinux_secmark_relabel_packet(u32 sid) 49552606fd1fSEric Paris { 49562606fd1fSEric Paris const struct task_security_struct *__tsec; 49572606fd1fSEric Paris u32 tsid; 49582606fd1fSEric Paris 49592606fd1fSEric Paris __tsec = current_security(); 49602606fd1fSEric Paris tsid = __tsec->sid; 49612606fd1fSEric Paris 49622606fd1fSEric Paris return avc_has_perm(tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, NULL); 49632606fd1fSEric Paris } 49642606fd1fSEric Paris 49652606fd1fSEric Paris static void selinux_secmark_refcount_inc(void) 49662606fd1fSEric Paris { 49672606fd1fSEric Paris atomic_inc(&selinux_secmark_refcount); 49682606fd1fSEric Paris } 49692606fd1fSEric Paris 49702606fd1fSEric Paris static void selinux_secmark_refcount_dec(void) 49712606fd1fSEric Paris { 49722606fd1fSEric Paris atomic_dec(&selinux_secmark_refcount); 49732606fd1fSEric Paris } 49742606fd1fSEric Paris 49759a673e56SAdrian Bunk static void selinux_req_classify_flow(const struct request_sock *req, 49769a673e56SAdrian Bunk struct flowi *fl) 49774237c75cSVenkat Yekkirala { 49781d28f42cSDavid S. Miller fl->flowi_secid = req->secid; 49794237c75cSVenkat Yekkirala } 49804237c75cSVenkat Yekkirala 49815dbbaf2dSPaul Moore static int selinux_tun_dev_alloc_security(void **security) 49825dbbaf2dSPaul Moore { 49835dbbaf2dSPaul Moore struct tun_security_struct *tunsec; 49845dbbaf2dSPaul Moore 49855dbbaf2dSPaul Moore tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL); 49865dbbaf2dSPaul Moore if (!tunsec) 49875dbbaf2dSPaul Moore return -ENOMEM; 49885dbbaf2dSPaul Moore tunsec->sid = current_sid(); 49895dbbaf2dSPaul Moore 49905dbbaf2dSPaul Moore *security = tunsec; 49915dbbaf2dSPaul Moore return 0; 49925dbbaf2dSPaul Moore } 49935dbbaf2dSPaul Moore 49945dbbaf2dSPaul Moore static void selinux_tun_dev_free_security(void *security) 49955dbbaf2dSPaul Moore { 49965dbbaf2dSPaul Moore kfree(security); 49975dbbaf2dSPaul Moore } 49985dbbaf2dSPaul Moore 4999ed6d76e4SPaul Moore static int selinux_tun_dev_create(void) 5000ed6d76e4SPaul Moore { 5001ed6d76e4SPaul Moore u32 sid = current_sid(); 5002ed6d76e4SPaul Moore 5003ed6d76e4SPaul Moore /* we aren't taking into account the "sockcreate" SID since the socket 5004ed6d76e4SPaul Moore * that is being created here is not a socket in the traditional sense, 5005ed6d76e4SPaul Moore * instead it is a private sock, accessible only to the kernel, and 5006ed6d76e4SPaul Moore * representing a wide range of network traffic spanning multiple 5007ed6d76e4SPaul Moore * connections unlike traditional sockets - check the TUN driver to 5008ed6d76e4SPaul Moore * get a better understanding of why this socket is special */ 5009ed6d76e4SPaul Moore 5010ed6d76e4SPaul Moore return avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE, 5011ed6d76e4SPaul Moore NULL); 5012ed6d76e4SPaul Moore } 5013ed6d76e4SPaul Moore 50145dbbaf2dSPaul Moore static int selinux_tun_dev_attach_queue(void *security) 5015ed6d76e4SPaul Moore { 50165dbbaf2dSPaul Moore struct tun_security_struct *tunsec = security; 50175dbbaf2dSPaul Moore 50185dbbaf2dSPaul Moore return avc_has_perm(current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET, 50195dbbaf2dSPaul Moore TUN_SOCKET__ATTACH_QUEUE, NULL); 50205dbbaf2dSPaul Moore } 50215dbbaf2dSPaul Moore 50225dbbaf2dSPaul Moore static int selinux_tun_dev_attach(struct sock *sk, void *security) 50235dbbaf2dSPaul Moore { 50245dbbaf2dSPaul Moore struct tun_security_struct *tunsec = security; 5025ed6d76e4SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 5026ed6d76e4SPaul Moore 5027ed6d76e4SPaul Moore /* we don't currently perform any NetLabel based labeling here and it 5028ed6d76e4SPaul Moore * isn't clear that we would want to do so anyway; while we could apply 5029ed6d76e4SPaul Moore * labeling without the support of the TUN user the resulting labeled 5030ed6d76e4SPaul Moore * traffic from the other end of the connection would almost certainly 5031ed6d76e4SPaul Moore * cause confusion to the TUN user that had no idea network labeling 5032ed6d76e4SPaul Moore * protocols were being used */ 5033ed6d76e4SPaul Moore 50345dbbaf2dSPaul Moore sksec->sid = tunsec->sid; 5035ed6d76e4SPaul Moore sksec->sclass = SECCLASS_TUN_SOCKET; 50365dbbaf2dSPaul Moore 50375dbbaf2dSPaul Moore return 0; 5038ed6d76e4SPaul Moore } 5039ed6d76e4SPaul Moore 50405dbbaf2dSPaul Moore static int selinux_tun_dev_open(void *security) 5041ed6d76e4SPaul Moore { 50425dbbaf2dSPaul Moore struct tun_security_struct *tunsec = security; 5043ed6d76e4SPaul Moore u32 sid = current_sid(); 5044ed6d76e4SPaul Moore int err; 5045ed6d76e4SPaul Moore 50465dbbaf2dSPaul Moore err = avc_has_perm(sid, tunsec->sid, SECCLASS_TUN_SOCKET, 5047ed6d76e4SPaul Moore TUN_SOCKET__RELABELFROM, NULL); 5048ed6d76e4SPaul Moore if (err) 5049ed6d76e4SPaul Moore return err; 5050ed6d76e4SPaul Moore err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, 5051ed6d76e4SPaul Moore TUN_SOCKET__RELABELTO, NULL); 5052ed6d76e4SPaul Moore if (err) 5053ed6d76e4SPaul Moore return err; 50545dbbaf2dSPaul Moore tunsec->sid = sid; 5055ed6d76e4SPaul Moore 5056ed6d76e4SPaul Moore return 0; 5057ed6d76e4SPaul Moore } 5058ed6d76e4SPaul Moore 50591da177e4SLinus Torvalds static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb) 50601da177e4SLinus Torvalds { 50611da177e4SLinus Torvalds int err = 0; 50621da177e4SLinus Torvalds u32 perm; 50631da177e4SLinus Torvalds struct nlmsghdr *nlh; 5064253bfae6SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 50651da177e4SLinus Torvalds 506677954983SHong zhi guo if (skb->len < NLMSG_HDRLEN) { 50671da177e4SLinus Torvalds err = -EINVAL; 50681da177e4SLinus Torvalds goto out; 50691da177e4SLinus Torvalds } 5070b529ccf2SArnaldo Carvalho de Melo nlh = nlmsg_hdr(skb); 50711da177e4SLinus Torvalds 5072253bfae6SPaul Moore err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm); 50731da177e4SLinus Torvalds if (err) { 50741da177e4SLinus Torvalds if (err == -EINVAL) { 507576319946SVladis Dronov pr_warn_ratelimited("SELinux: unrecognized netlink" 507676319946SVladis Dronov " message: protocol=%hu nlmsg_type=%hu sclass=%s" 507776319946SVladis Dronov " pig=%d comm=%s\n", 5078cded3fffSMarek Milkovic sk->sk_protocol, nlh->nlmsg_type, 507976319946SVladis Dronov secclass_map[sksec->sclass - 1].name, 508076319946SVladis Dronov task_pid_nr(current), current->comm); 508139c9aedeSEric Paris if (!selinux_enforcing || security_get_allow_unknown()) 50821da177e4SLinus Torvalds err = 0; 50831da177e4SLinus Torvalds } 50841da177e4SLinus Torvalds 50851da177e4SLinus Torvalds /* Ignore */ 50861da177e4SLinus Torvalds if (err == -ENOENT) 50871da177e4SLinus Torvalds err = 0; 50881da177e4SLinus Torvalds goto out; 50891da177e4SLinus Torvalds } 50901da177e4SLinus Torvalds 5091be0554c9SStephen Smalley err = sock_has_perm(sk, perm); 50921da177e4SLinus Torvalds out: 50931da177e4SLinus Torvalds return err; 50941da177e4SLinus Torvalds } 50951da177e4SLinus Torvalds 50961da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER 50971da177e4SLinus Torvalds 5098cbe0d6e8SPaul Moore static unsigned int selinux_ip_forward(struct sk_buff *skb, 5099cbe0d6e8SPaul Moore const struct net_device *indev, 5100effad8dfSPaul Moore u16 family) 51011da177e4SLinus Torvalds { 5102dfaebe98SPaul Moore int err; 5103effad8dfSPaul Moore char *addrp; 5104effad8dfSPaul Moore u32 peer_sid; 51052bf49690SThomas Liu struct common_audit_data ad; 510648c62af6SEric Paris struct lsm_network_audit net = {0,}; 5107effad8dfSPaul Moore u8 secmark_active; 5108948bf85cSPaul Moore u8 netlbl_active; 5109effad8dfSPaul Moore u8 peerlbl_active; 51104237c75cSVenkat Yekkirala 5111effad8dfSPaul Moore if (!selinux_policycap_netpeer) 5112effad8dfSPaul Moore return NF_ACCEPT; 51134237c75cSVenkat Yekkirala 5114effad8dfSPaul Moore secmark_active = selinux_secmark_enabled(); 5115948bf85cSPaul Moore netlbl_active = netlbl_enabled(); 51162be4d74fSChris PeBenito peerlbl_active = selinux_peerlbl_enabled(); 5117effad8dfSPaul Moore if (!secmark_active && !peerlbl_active) 5118effad8dfSPaul Moore return NF_ACCEPT; 51194237c75cSVenkat Yekkirala 5120d8395c87SPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0) 5121d8395c87SPaul Moore return NF_DROP; 5122d8395c87SPaul Moore 512350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 512448c62af6SEric Paris ad.u.net = &net; 5125cbe0d6e8SPaul Moore ad.u.net->netif = indev->ifindex; 512648c62af6SEric Paris ad.u.net->family = family; 5127effad8dfSPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0) 5128effad8dfSPaul Moore return NF_DROP; 51291da177e4SLinus Torvalds 5130dfaebe98SPaul Moore if (peerlbl_active) { 5131cbe0d6e8SPaul Moore err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex, 5132cbe0d6e8SPaul Moore addrp, family, peer_sid, &ad); 5133dfaebe98SPaul Moore if (err) { 5134a04e71f6SHuw Davies selinux_netlbl_err(skb, family, err, 1); 5135effad8dfSPaul Moore return NF_DROP; 5136dfaebe98SPaul Moore } 5137dfaebe98SPaul Moore } 5138effad8dfSPaul Moore 5139effad8dfSPaul Moore if (secmark_active) 5140effad8dfSPaul Moore if (avc_has_perm(peer_sid, skb->secmark, 5141effad8dfSPaul Moore SECCLASS_PACKET, PACKET__FORWARD_IN, &ad)) 5142effad8dfSPaul Moore return NF_DROP; 5143effad8dfSPaul Moore 5144948bf85cSPaul Moore if (netlbl_active) 5145948bf85cSPaul Moore /* we do this in the FORWARD path and not the POST_ROUTING 5146948bf85cSPaul Moore * path because we want to make sure we apply the necessary 5147948bf85cSPaul Moore * labeling before IPsec is applied so we can leverage AH 5148948bf85cSPaul Moore * protection */ 5149948bf85cSPaul Moore if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0) 5150948bf85cSPaul Moore return NF_DROP; 5151948bf85cSPaul Moore 5152effad8dfSPaul Moore return NF_ACCEPT; 5153effad8dfSPaul Moore } 5154effad8dfSPaul Moore 515506198b34SEric W. Biederman static unsigned int selinux_ipv4_forward(void *priv, 5156effad8dfSPaul Moore struct sk_buff *skb, 5157238e54c9SDavid S. Miller const struct nf_hook_state *state) 5158effad8dfSPaul Moore { 5159238e54c9SDavid S. Miller return selinux_ip_forward(skb, state->in, PF_INET); 5160effad8dfSPaul Moore } 5161effad8dfSPaul Moore 51621a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 516306198b34SEric W. Biederman static unsigned int selinux_ipv6_forward(void *priv, 5164effad8dfSPaul Moore struct sk_buff *skb, 5165238e54c9SDavid S. Miller const struct nf_hook_state *state) 5166effad8dfSPaul Moore { 5167238e54c9SDavid S. Miller return selinux_ip_forward(skb, state->in, PF_INET6); 5168effad8dfSPaul Moore } 5169effad8dfSPaul Moore #endif /* IPV6 */ 5170effad8dfSPaul Moore 5171948bf85cSPaul Moore static unsigned int selinux_ip_output(struct sk_buff *skb, 5172948bf85cSPaul Moore u16 family) 5173948bf85cSPaul Moore { 517447180068SPaul Moore struct sock *sk; 5175948bf85cSPaul Moore u32 sid; 5176948bf85cSPaul Moore 5177948bf85cSPaul Moore if (!netlbl_enabled()) 5178948bf85cSPaul Moore return NF_ACCEPT; 5179948bf85cSPaul Moore 5180948bf85cSPaul Moore /* we do this in the LOCAL_OUT path and not the POST_ROUTING path 5181948bf85cSPaul Moore * because we want to make sure we apply the necessary labeling 5182948bf85cSPaul Moore * before IPsec is applied so we can leverage AH protection */ 518347180068SPaul Moore sk = skb->sk; 518447180068SPaul Moore if (sk) { 518547180068SPaul Moore struct sk_security_struct *sksec; 518647180068SPaul Moore 5187e446f9dfSEric Dumazet if (sk_listener(sk)) 518847180068SPaul Moore /* if the socket is the listening state then this 518947180068SPaul Moore * packet is a SYN-ACK packet which means it needs to 519047180068SPaul Moore * be labeled based on the connection/request_sock and 519147180068SPaul Moore * not the parent socket. unfortunately, we can't 519247180068SPaul Moore * lookup the request_sock yet as it isn't queued on 519347180068SPaul Moore * the parent socket until after the SYN-ACK is sent. 519447180068SPaul Moore * the "solution" is to simply pass the packet as-is 519547180068SPaul Moore * as any IP option based labeling should be copied 519647180068SPaul Moore * from the initial connection request (in the IP 519747180068SPaul Moore * layer). it is far from ideal, but until we get a 519847180068SPaul Moore * security label in the packet itself this is the 519947180068SPaul Moore * best we can do. */ 520047180068SPaul Moore return NF_ACCEPT; 520147180068SPaul Moore 520247180068SPaul Moore /* standard practice, label using the parent socket */ 520347180068SPaul Moore sksec = sk->sk_security; 5204948bf85cSPaul Moore sid = sksec->sid; 5205948bf85cSPaul Moore } else 5206948bf85cSPaul Moore sid = SECINITSID_KERNEL; 5207948bf85cSPaul Moore if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0) 5208948bf85cSPaul Moore return NF_DROP; 5209948bf85cSPaul Moore 5210948bf85cSPaul Moore return NF_ACCEPT; 5211948bf85cSPaul Moore } 5212948bf85cSPaul Moore 521306198b34SEric W. Biederman static unsigned int selinux_ipv4_output(void *priv, 5214948bf85cSPaul Moore struct sk_buff *skb, 5215238e54c9SDavid S. Miller const struct nf_hook_state *state) 5216948bf85cSPaul Moore { 5217948bf85cSPaul Moore return selinux_ip_output(skb, PF_INET); 5218948bf85cSPaul Moore } 5219948bf85cSPaul Moore 52201a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 52212917f57bSHuw Davies static unsigned int selinux_ipv6_output(void *priv, 52222917f57bSHuw Davies struct sk_buff *skb, 52232917f57bSHuw Davies const struct nf_hook_state *state) 52242917f57bSHuw Davies { 52252917f57bSHuw Davies return selinux_ip_output(skb, PF_INET6); 52262917f57bSHuw Davies } 52272917f57bSHuw Davies #endif /* IPV6 */ 52282917f57bSHuw Davies 5229effad8dfSPaul Moore static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb, 5230effad8dfSPaul Moore int ifindex, 5231d8395c87SPaul Moore u16 family) 52324e5ab4cbSJames Morris { 523354abc686SEric Dumazet struct sock *sk = skb_to_full_sk(skb); 52344237c75cSVenkat Yekkirala struct sk_security_struct *sksec; 52352bf49690SThomas Liu struct common_audit_data ad; 523648c62af6SEric Paris struct lsm_network_audit net = {0,}; 5237d8395c87SPaul Moore char *addrp; 5238d8395c87SPaul Moore u8 proto; 52394e5ab4cbSJames Morris 5240effad8dfSPaul Moore if (sk == NULL) 5241effad8dfSPaul Moore return NF_ACCEPT; 52424237c75cSVenkat Yekkirala sksec = sk->sk_security; 52434e5ab4cbSJames Morris 524450c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 524548c62af6SEric Paris ad.u.net = &net; 524648c62af6SEric Paris ad.u.net->netif = ifindex; 524748c62af6SEric Paris ad.u.net->family = family; 5248d8395c87SPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto)) 5249d8395c87SPaul Moore return NF_DROP; 5250d8395c87SPaul Moore 525158bfbb51SPaul Moore if (selinux_secmark_enabled()) 5252effad8dfSPaul Moore if (avc_has_perm(sksec->sid, skb->secmark, 5253d8395c87SPaul Moore SECCLASS_PACKET, PACKET__SEND, &ad)) 52542fe66ec2SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 52551da177e4SLinus Torvalds 5256d8395c87SPaul Moore if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto)) 52572fe66ec2SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 5258effad8dfSPaul Moore 5259effad8dfSPaul Moore return NF_ACCEPT; 5260effad8dfSPaul Moore } 5261effad8dfSPaul Moore 5262cbe0d6e8SPaul Moore static unsigned int selinux_ip_postroute(struct sk_buff *skb, 5263cbe0d6e8SPaul Moore const struct net_device *outdev, 5264effad8dfSPaul Moore u16 family) 5265effad8dfSPaul Moore { 5266effad8dfSPaul Moore u32 secmark_perm; 5267effad8dfSPaul Moore u32 peer_sid; 5268cbe0d6e8SPaul Moore int ifindex = outdev->ifindex; 5269effad8dfSPaul Moore struct sock *sk; 52702bf49690SThomas Liu struct common_audit_data ad; 527148c62af6SEric Paris struct lsm_network_audit net = {0,}; 5272effad8dfSPaul Moore char *addrp; 5273effad8dfSPaul Moore u8 secmark_active; 5274effad8dfSPaul Moore u8 peerlbl_active; 5275effad8dfSPaul Moore 5276effad8dfSPaul Moore /* If any sort of compatibility mode is enabled then handoff processing 5277effad8dfSPaul Moore * to the selinux_ip_postroute_compat() function to deal with the 5278effad8dfSPaul Moore * special handling. We do this in an attempt to keep this function 5279effad8dfSPaul Moore * as fast and as clean as possible. */ 528058bfbb51SPaul Moore if (!selinux_policycap_netpeer) 5281d8395c87SPaul Moore return selinux_ip_postroute_compat(skb, ifindex, family); 5282c0828e50SPaul Moore 5283effad8dfSPaul Moore secmark_active = selinux_secmark_enabled(); 52842be4d74fSChris PeBenito peerlbl_active = selinux_peerlbl_enabled(); 5285effad8dfSPaul Moore if (!secmark_active && !peerlbl_active) 5286effad8dfSPaul Moore return NF_ACCEPT; 5287effad8dfSPaul Moore 528854abc686SEric Dumazet sk = skb_to_full_sk(skb); 5289c0828e50SPaul Moore 5290effad8dfSPaul Moore #ifdef CONFIG_XFRM 5291effad8dfSPaul Moore /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec 5292effad8dfSPaul Moore * packet transformation so allow the packet to pass without any checks 5293effad8dfSPaul Moore * since we'll have another chance to perform access control checks 5294effad8dfSPaul Moore * when the packet is on it's final way out. 5295effad8dfSPaul Moore * NOTE: there appear to be some IPv6 multicast cases where skb->dst 5296c0828e50SPaul Moore * is NULL, in this case go ahead and apply access control. 5297c0828e50SPaul Moore * NOTE: if this is a local socket (skb->sk != NULL) that is in the 5298c0828e50SPaul Moore * TCP listening state we cannot wait until the XFRM processing 5299c0828e50SPaul Moore * is done as we will miss out on the SA label if we do; 5300c0828e50SPaul Moore * unfortunately, this means more work, but it is only once per 5301c0828e50SPaul Moore * connection. */ 5302c0828e50SPaul Moore if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL && 5303e446f9dfSEric Dumazet !(sk && sk_listener(sk))) 5304effad8dfSPaul Moore return NF_ACCEPT; 5305effad8dfSPaul Moore #endif 5306effad8dfSPaul Moore 5307d8395c87SPaul Moore if (sk == NULL) { 5308446b8024SPaul Moore /* Without an associated socket the packet is either coming 5309446b8024SPaul Moore * from the kernel or it is being forwarded; check the packet 5310446b8024SPaul Moore * to determine which and if the packet is being forwarded 5311446b8024SPaul Moore * query the packet directly to determine the security label. */ 53124a7ab3dcSSteffen Klassert if (skb->skb_iif) { 5313d8395c87SPaul Moore secmark_perm = PACKET__FORWARD_OUT; 5314d8395c87SPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &peer_sid)) 531504f6d70fSEric Paris return NF_DROP; 53164a7ab3dcSSteffen Klassert } else { 53174a7ab3dcSSteffen Klassert secmark_perm = PACKET__SEND; 5318d8395c87SPaul Moore peer_sid = SECINITSID_KERNEL; 53194a7ab3dcSSteffen Klassert } 5320e446f9dfSEric Dumazet } else if (sk_listener(sk)) { 5321446b8024SPaul Moore /* Locally generated packet but the associated socket is in the 5322446b8024SPaul Moore * listening state which means this is a SYN-ACK packet. In 5323446b8024SPaul Moore * this particular case the correct security label is assigned 5324446b8024SPaul Moore * to the connection/request_sock but unfortunately we can't 5325446b8024SPaul Moore * query the request_sock as it isn't queued on the parent 5326446b8024SPaul Moore * socket until after the SYN-ACK packet is sent; the only 5327446b8024SPaul Moore * viable choice is to regenerate the label like we do in 5328446b8024SPaul Moore * selinux_inet_conn_request(). See also selinux_ip_output() 5329446b8024SPaul Moore * for similar problems. */ 5330446b8024SPaul Moore u32 skb_sid; 5331e446f9dfSEric Dumazet struct sk_security_struct *sksec; 5332e446f9dfSEric Dumazet 5333e446f9dfSEric Dumazet sksec = sk->sk_security; 5334446b8024SPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &skb_sid)) 5335446b8024SPaul Moore return NF_DROP; 5336c0828e50SPaul Moore /* At this point, if the returned skb peerlbl is SECSID_NULL 5337c0828e50SPaul Moore * and the packet has been through at least one XFRM 5338c0828e50SPaul Moore * transformation then we must be dealing with the "final" 5339c0828e50SPaul Moore * form of labeled IPsec packet; since we've already applied 5340c0828e50SPaul Moore * all of our access controls on this packet we can safely 5341c0828e50SPaul Moore * pass the packet. */ 5342c0828e50SPaul Moore if (skb_sid == SECSID_NULL) { 5343c0828e50SPaul Moore switch (family) { 5344c0828e50SPaul Moore case PF_INET: 5345c0828e50SPaul Moore if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) 5346c0828e50SPaul Moore return NF_ACCEPT; 5347c0828e50SPaul Moore break; 5348c0828e50SPaul Moore case PF_INET6: 5349c0828e50SPaul Moore if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) 5350c0828e50SPaul Moore return NF_ACCEPT; 5351a7a91a19SPaul Moore break; 5352c0828e50SPaul Moore default: 5353c0828e50SPaul Moore return NF_DROP_ERR(-ECONNREFUSED); 5354c0828e50SPaul Moore } 5355c0828e50SPaul Moore } 5356446b8024SPaul Moore if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid)) 5357446b8024SPaul Moore return NF_DROP; 5358446b8024SPaul Moore secmark_perm = PACKET__SEND; 5359d8395c87SPaul Moore } else { 5360446b8024SPaul Moore /* Locally generated packet, fetch the security label from the 5361446b8024SPaul Moore * associated socket. */ 5362effad8dfSPaul Moore struct sk_security_struct *sksec = sk->sk_security; 5363effad8dfSPaul Moore peer_sid = sksec->sid; 5364effad8dfSPaul Moore secmark_perm = PACKET__SEND; 5365effad8dfSPaul Moore } 5366effad8dfSPaul Moore 536750c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 536848c62af6SEric Paris ad.u.net = &net; 536948c62af6SEric Paris ad.u.net->netif = ifindex; 537048c62af6SEric Paris ad.u.net->family = family; 5371d8395c87SPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL)) 537204f6d70fSEric Paris return NF_DROP; 5373d8395c87SPaul Moore 5374effad8dfSPaul Moore if (secmark_active) 5375effad8dfSPaul Moore if (avc_has_perm(peer_sid, skb->secmark, 5376effad8dfSPaul Moore SECCLASS_PACKET, secmark_perm, &ad)) 53771f1aaf82SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 5378effad8dfSPaul Moore 5379effad8dfSPaul Moore if (peerlbl_active) { 5380effad8dfSPaul Moore u32 if_sid; 5381effad8dfSPaul Moore u32 node_sid; 5382effad8dfSPaul Moore 5383cbe0d6e8SPaul Moore if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid)) 538404f6d70fSEric Paris return NF_DROP; 5385effad8dfSPaul Moore if (avc_has_perm(peer_sid, if_sid, 5386effad8dfSPaul Moore SECCLASS_NETIF, NETIF__EGRESS, &ad)) 53871f1aaf82SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 5388effad8dfSPaul Moore 5389effad8dfSPaul Moore if (sel_netnode_sid(addrp, family, &node_sid)) 539004f6d70fSEric Paris return NF_DROP; 5391effad8dfSPaul Moore if (avc_has_perm(peer_sid, node_sid, 5392effad8dfSPaul Moore SECCLASS_NODE, NODE__SENDTO, &ad)) 53931f1aaf82SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 5394effad8dfSPaul Moore } 5395effad8dfSPaul Moore 5396effad8dfSPaul Moore return NF_ACCEPT; 5397effad8dfSPaul Moore } 5398effad8dfSPaul Moore 539906198b34SEric W. Biederman static unsigned int selinux_ipv4_postroute(void *priv, 5400a224be76SDavid S. Miller struct sk_buff *skb, 5401238e54c9SDavid S. Miller const struct nf_hook_state *state) 54021da177e4SLinus Torvalds { 5403238e54c9SDavid S. Miller return selinux_ip_postroute(skb, state->out, PF_INET); 54041da177e4SLinus Torvalds } 54051da177e4SLinus Torvalds 54061a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 540706198b34SEric W. Biederman static unsigned int selinux_ipv6_postroute(void *priv, 5408a224be76SDavid S. Miller struct sk_buff *skb, 5409238e54c9SDavid S. Miller const struct nf_hook_state *state) 54101da177e4SLinus Torvalds { 5411238e54c9SDavid S. Miller return selinux_ip_postroute(skb, state->out, PF_INET6); 54121da177e4SLinus Torvalds } 54131da177e4SLinus Torvalds #endif /* IPV6 */ 54141da177e4SLinus Torvalds 54151da177e4SLinus Torvalds #endif /* CONFIG_NETFILTER */ 54161da177e4SLinus Torvalds 54171da177e4SLinus Torvalds static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb) 54181da177e4SLinus Torvalds { 5419941fc5b2SStephen Smalley return selinux_nlmsg_perm(sk, skb); 54201da177e4SLinus Torvalds } 54211da177e4SLinus Torvalds 5422be0554c9SStephen Smalley static int ipc_alloc_security(struct kern_ipc_perm *perm, 54231da177e4SLinus Torvalds u16 sclass) 54241da177e4SLinus Torvalds { 54251da177e4SLinus Torvalds struct ipc_security_struct *isec; 54261da177e4SLinus Torvalds 542789d155efSJames Morris isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL); 54281da177e4SLinus Torvalds if (!isec) 54291da177e4SLinus Torvalds return -ENOMEM; 54301da177e4SLinus Torvalds 54311da177e4SLinus Torvalds isec->sclass = sclass; 5432be0554c9SStephen Smalley isec->sid = current_sid(); 54331da177e4SLinus Torvalds perm->security = isec; 54341da177e4SLinus Torvalds 54351da177e4SLinus Torvalds return 0; 54361da177e4SLinus Torvalds } 54371da177e4SLinus Torvalds 54381da177e4SLinus Torvalds static void ipc_free_security(struct kern_ipc_perm *perm) 54391da177e4SLinus Torvalds { 54401da177e4SLinus Torvalds struct ipc_security_struct *isec = perm->security; 54411da177e4SLinus Torvalds perm->security = NULL; 54421da177e4SLinus Torvalds kfree(isec); 54431da177e4SLinus Torvalds } 54441da177e4SLinus Torvalds 54451da177e4SLinus Torvalds static int msg_msg_alloc_security(struct msg_msg *msg) 54461da177e4SLinus Torvalds { 54471da177e4SLinus Torvalds struct msg_security_struct *msec; 54481da177e4SLinus Torvalds 544989d155efSJames Morris msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL); 54501da177e4SLinus Torvalds if (!msec) 54511da177e4SLinus Torvalds return -ENOMEM; 54521da177e4SLinus Torvalds 54531da177e4SLinus Torvalds msec->sid = SECINITSID_UNLABELED; 54541da177e4SLinus Torvalds msg->security = msec; 54551da177e4SLinus Torvalds 54561da177e4SLinus Torvalds return 0; 54571da177e4SLinus Torvalds } 54581da177e4SLinus Torvalds 54591da177e4SLinus Torvalds static void msg_msg_free_security(struct msg_msg *msg) 54601da177e4SLinus Torvalds { 54611da177e4SLinus Torvalds struct msg_security_struct *msec = msg->security; 54621da177e4SLinus Torvalds 54631da177e4SLinus Torvalds msg->security = NULL; 54641da177e4SLinus Torvalds kfree(msec); 54651da177e4SLinus Torvalds } 54661da177e4SLinus Torvalds 54671da177e4SLinus Torvalds static int ipc_has_perm(struct kern_ipc_perm *ipc_perms, 54686af963f1SStephen Smalley u32 perms) 54691da177e4SLinus Torvalds { 54701da177e4SLinus Torvalds struct ipc_security_struct *isec; 54712bf49690SThomas Liu struct common_audit_data ad; 5472275bb41eSDavid Howells u32 sid = current_sid(); 54731da177e4SLinus Torvalds 54741da177e4SLinus Torvalds isec = ipc_perms->security; 54751da177e4SLinus Torvalds 547650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 54771da177e4SLinus Torvalds ad.u.ipc_id = ipc_perms->key; 54781da177e4SLinus Torvalds 5479275bb41eSDavid Howells return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad); 54801da177e4SLinus Torvalds } 54811da177e4SLinus Torvalds 54821da177e4SLinus Torvalds static int selinux_msg_msg_alloc_security(struct msg_msg *msg) 54831da177e4SLinus Torvalds { 54841da177e4SLinus Torvalds return msg_msg_alloc_security(msg); 54851da177e4SLinus Torvalds } 54861da177e4SLinus Torvalds 54871da177e4SLinus Torvalds static void selinux_msg_msg_free_security(struct msg_msg *msg) 54881da177e4SLinus Torvalds { 54891da177e4SLinus Torvalds msg_msg_free_security(msg); 54901da177e4SLinus Torvalds } 54911da177e4SLinus Torvalds 54921da177e4SLinus Torvalds /* message queue security operations */ 54931da177e4SLinus Torvalds static int selinux_msg_queue_alloc_security(struct msg_queue *msq) 54941da177e4SLinus Torvalds { 54951da177e4SLinus Torvalds struct ipc_security_struct *isec; 54962bf49690SThomas Liu struct common_audit_data ad; 5497275bb41eSDavid Howells u32 sid = current_sid(); 54981da177e4SLinus Torvalds int rc; 54991da177e4SLinus Torvalds 5500be0554c9SStephen Smalley rc = ipc_alloc_security(&msq->q_perm, SECCLASS_MSGQ); 55011da177e4SLinus Torvalds if (rc) 55021da177e4SLinus Torvalds return rc; 55031da177e4SLinus Torvalds 55041da177e4SLinus Torvalds isec = msq->q_perm.security; 55051da177e4SLinus Torvalds 550650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 55071da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 55081da177e4SLinus Torvalds 5509275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ, 55101da177e4SLinus Torvalds MSGQ__CREATE, &ad); 55111da177e4SLinus Torvalds if (rc) { 55121da177e4SLinus Torvalds ipc_free_security(&msq->q_perm); 55131da177e4SLinus Torvalds return rc; 55141da177e4SLinus Torvalds } 55151da177e4SLinus Torvalds return 0; 55161da177e4SLinus Torvalds } 55171da177e4SLinus Torvalds 55181da177e4SLinus Torvalds static void selinux_msg_queue_free_security(struct msg_queue *msq) 55191da177e4SLinus Torvalds { 55201da177e4SLinus Torvalds ipc_free_security(&msq->q_perm); 55211da177e4SLinus Torvalds } 55221da177e4SLinus Torvalds 55231da177e4SLinus Torvalds static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg) 55241da177e4SLinus Torvalds { 55251da177e4SLinus Torvalds struct ipc_security_struct *isec; 55262bf49690SThomas Liu struct common_audit_data ad; 5527275bb41eSDavid Howells u32 sid = current_sid(); 55281da177e4SLinus Torvalds 55291da177e4SLinus Torvalds isec = msq->q_perm.security; 55301da177e4SLinus Torvalds 553150c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 55321da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 55331da177e4SLinus Torvalds 5534275bb41eSDavid Howells return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ, 55351da177e4SLinus Torvalds MSGQ__ASSOCIATE, &ad); 55361da177e4SLinus Torvalds } 55371da177e4SLinus Torvalds 55381da177e4SLinus Torvalds static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd) 55391da177e4SLinus Torvalds { 55401da177e4SLinus Torvalds int err; 55411da177e4SLinus Torvalds int perms; 55421da177e4SLinus Torvalds 55431da177e4SLinus Torvalds switch (cmd) { 55441da177e4SLinus Torvalds case IPC_INFO: 55451da177e4SLinus Torvalds case MSG_INFO: 55461da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 5547be0554c9SStephen Smalley return avc_has_perm(current_sid(), SECINITSID_KERNEL, 5548be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); 55491da177e4SLinus Torvalds case IPC_STAT: 55501da177e4SLinus Torvalds case MSG_STAT: 55511da177e4SLinus Torvalds perms = MSGQ__GETATTR | MSGQ__ASSOCIATE; 55521da177e4SLinus Torvalds break; 55531da177e4SLinus Torvalds case IPC_SET: 55541da177e4SLinus Torvalds perms = MSGQ__SETATTR; 55551da177e4SLinus Torvalds break; 55561da177e4SLinus Torvalds case IPC_RMID: 55571da177e4SLinus Torvalds perms = MSGQ__DESTROY; 55581da177e4SLinus Torvalds break; 55591da177e4SLinus Torvalds default: 55601da177e4SLinus Torvalds return 0; 55611da177e4SLinus Torvalds } 55621da177e4SLinus Torvalds 55636af963f1SStephen Smalley err = ipc_has_perm(&msq->q_perm, perms); 55641da177e4SLinus Torvalds return err; 55651da177e4SLinus Torvalds } 55661da177e4SLinus Torvalds 55671da177e4SLinus Torvalds static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg) 55681da177e4SLinus Torvalds { 55691da177e4SLinus Torvalds struct ipc_security_struct *isec; 55701da177e4SLinus Torvalds struct msg_security_struct *msec; 55712bf49690SThomas Liu struct common_audit_data ad; 5572275bb41eSDavid Howells u32 sid = current_sid(); 55731da177e4SLinus Torvalds int rc; 55741da177e4SLinus Torvalds 55751da177e4SLinus Torvalds isec = msq->q_perm.security; 55761da177e4SLinus Torvalds msec = msg->security; 55771da177e4SLinus Torvalds 55781da177e4SLinus Torvalds /* 55791da177e4SLinus Torvalds * First time through, need to assign label to the message 55801da177e4SLinus Torvalds */ 55811da177e4SLinus Torvalds if (msec->sid == SECINITSID_UNLABELED) { 55821da177e4SLinus Torvalds /* 55831da177e4SLinus Torvalds * Compute new sid based on current process and 55841da177e4SLinus Torvalds * message queue this message will be stored in 55851da177e4SLinus Torvalds */ 5586275bb41eSDavid Howells rc = security_transition_sid(sid, isec->sid, SECCLASS_MSG, 5587652bb9b0SEric Paris NULL, &msec->sid); 55881da177e4SLinus Torvalds if (rc) 55891da177e4SLinus Torvalds return rc; 55901da177e4SLinus Torvalds } 55911da177e4SLinus Torvalds 559250c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 55931da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 55941da177e4SLinus Torvalds 55951da177e4SLinus Torvalds /* Can this process write to the queue? */ 5596275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ, 55971da177e4SLinus Torvalds MSGQ__WRITE, &ad); 55981da177e4SLinus Torvalds if (!rc) 55991da177e4SLinus Torvalds /* Can this process send the message */ 5600275bb41eSDavid Howells rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG, 5601275bb41eSDavid Howells MSG__SEND, &ad); 56021da177e4SLinus Torvalds if (!rc) 56031da177e4SLinus Torvalds /* Can the message be put in the queue? */ 5604275bb41eSDavid Howells rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ, 5605275bb41eSDavid Howells MSGQ__ENQUEUE, &ad); 56061da177e4SLinus Torvalds 56071da177e4SLinus Torvalds return rc; 56081da177e4SLinus Torvalds } 56091da177e4SLinus Torvalds 56101da177e4SLinus Torvalds static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg, 56111da177e4SLinus Torvalds struct task_struct *target, 56121da177e4SLinus Torvalds long type, int mode) 56131da177e4SLinus Torvalds { 56141da177e4SLinus Torvalds struct ipc_security_struct *isec; 56151da177e4SLinus Torvalds struct msg_security_struct *msec; 56162bf49690SThomas Liu struct common_audit_data ad; 5617275bb41eSDavid Howells u32 sid = task_sid(target); 56181da177e4SLinus Torvalds int rc; 56191da177e4SLinus Torvalds 56201da177e4SLinus Torvalds isec = msq->q_perm.security; 56211da177e4SLinus Torvalds msec = msg->security; 56221da177e4SLinus Torvalds 562350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 56241da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 56251da177e4SLinus Torvalds 5626275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, 56271da177e4SLinus Torvalds SECCLASS_MSGQ, MSGQ__READ, &ad); 56281da177e4SLinus Torvalds if (!rc) 5629275bb41eSDavid Howells rc = avc_has_perm(sid, msec->sid, 56301da177e4SLinus Torvalds SECCLASS_MSG, MSG__RECEIVE, &ad); 56311da177e4SLinus Torvalds return rc; 56321da177e4SLinus Torvalds } 56331da177e4SLinus Torvalds 56341da177e4SLinus Torvalds /* Shared Memory security operations */ 56351da177e4SLinus Torvalds static int selinux_shm_alloc_security(struct shmid_kernel *shp) 56361da177e4SLinus Torvalds { 56371da177e4SLinus Torvalds struct ipc_security_struct *isec; 56382bf49690SThomas Liu struct common_audit_data ad; 5639275bb41eSDavid Howells u32 sid = current_sid(); 56401da177e4SLinus Torvalds int rc; 56411da177e4SLinus Torvalds 5642be0554c9SStephen Smalley rc = ipc_alloc_security(&shp->shm_perm, SECCLASS_SHM); 56431da177e4SLinus Torvalds if (rc) 56441da177e4SLinus Torvalds return rc; 56451da177e4SLinus Torvalds 56461da177e4SLinus Torvalds isec = shp->shm_perm.security; 56471da177e4SLinus Torvalds 564850c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 56491da177e4SLinus Torvalds ad.u.ipc_id = shp->shm_perm.key; 56501da177e4SLinus Torvalds 5651275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, SECCLASS_SHM, 56521da177e4SLinus Torvalds SHM__CREATE, &ad); 56531da177e4SLinus Torvalds if (rc) { 56541da177e4SLinus Torvalds ipc_free_security(&shp->shm_perm); 56551da177e4SLinus Torvalds return rc; 56561da177e4SLinus Torvalds } 56571da177e4SLinus Torvalds return 0; 56581da177e4SLinus Torvalds } 56591da177e4SLinus Torvalds 56601da177e4SLinus Torvalds static void selinux_shm_free_security(struct shmid_kernel *shp) 56611da177e4SLinus Torvalds { 56621da177e4SLinus Torvalds ipc_free_security(&shp->shm_perm); 56631da177e4SLinus Torvalds } 56641da177e4SLinus Torvalds 56651da177e4SLinus Torvalds static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg) 56661da177e4SLinus Torvalds { 56671da177e4SLinus Torvalds struct ipc_security_struct *isec; 56682bf49690SThomas Liu struct common_audit_data ad; 5669275bb41eSDavid Howells u32 sid = current_sid(); 56701da177e4SLinus Torvalds 56711da177e4SLinus Torvalds isec = shp->shm_perm.security; 56721da177e4SLinus Torvalds 567350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 56741da177e4SLinus Torvalds ad.u.ipc_id = shp->shm_perm.key; 56751da177e4SLinus Torvalds 5676275bb41eSDavid Howells return avc_has_perm(sid, isec->sid, SECCLASS_SHM, 56771da177e4SLinus Torvalds SHM__ASSOCIATE, &ad); 56781da177e4SLinus Torvalds } 56791da177e4SLinus Torvalds 56801da177e4SLinus Torvalds /* Note, at this point, shp is locked down */ 56811da177e4SLinus Torvalds static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd) 56821da177e4SLinus Torvalds { 56831da177e4SLinus Torvalds int perms; 56841da177e4SLinus Torvalds int err; 56851da177e4SLinus Torvalds 56861da177e4SLinus Torvalds switch (cmd) { 56871da177e4SLinus Torvalds case IPC_INFO: 56881da177e4SLinus Torvalds case SHM_INFO: 56891da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 5690be0554c9SStephen Smalley return avc_has_perm(current_sid(), SECINITSID_KERNEL, 5691be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); 56921da177e4SLinus Torvalds case IPC_STAT: 56931da177e4SLinus Torvalds case SHM_STAT: 56941da177e4SLinus Torvalds perms = SHM__GETATTR | SHM__ASSOCIATE; 56951da177e4SLinus Torvalds break; 56961da177e4SLinus Torvalds case IPC_SET: 56971da177e4SLinus Torvalds perms = SHM__SETATTR; 56981da177e4SLinus Torvalds break; 56991da177e4SLinus Torvalds case SHM_LOCK: 57001da177e4SLinus Torvalds case SHM_UNLOCK: 57011da177e4SLinus Torvalds perms = SHM__LOCK; 57021da177e4SLinus Torvalds break; 57031da177e4SLinus Torvalds case IPC_RMID: 57041da177e4SLinus Torvalds perms = SHM__DESTROY; 57051da177e4SLinus Torvalds break; 57061da177e4SLinus Torvalds default: 57071da177e4SLinus Torvalds return 0; 57081da177e4SLinus Torvalds } 57091da177e4SLinus Torvalds 57106af963f1SStephen Smalley err = ipc_has_perm(&shp->shm_perm, perms); 57111da177e4SLinus Torvalds return err; 57121da177e4SLinus Torvalds } 57131da177e4SLinus Torvalds 57141da177e4SLinus Torvalds static int selinux_shm_shmat(struct shmid_kernel *shp, 57151da177e4SLinus Torvalds char __user *shmaddr, int shmflg) 57161da177e4SLinus Torvalds { 57171da177e4SLinus Torvalds u32 perms; 57181da177e4SLinus Torvalds 57191da177e4SLinus Torvalds if (shmflg & SHM_RDONLY) 57201da177e4SLinus Torvalds perms = SHM__READ; 57211da177e4SLinus Torvalds else 57221da177e4SLinus Torvalds perms = SHM__READ | SHM__WRITE; 57231da177e4SLinus Torvalds 57246af963f1SStephen Smalley return ipc_has_perm(&shp->shm_perm, perms); 57251da177e4SLinus Torvalds } 57261da177e4SLinus Torvalds 57271da177e4SLinus Torvalds /* Semaphore security operations */ 57281da177e4SLinus Torvalds static int selinux_sem_alloc_security(struct sem_array *sma) 57291da177e4SLinus Torvalds { 57301da177e4SLinus Torvalds struct ipc_security_struct *isec; 57312bf49690SThomas Liu struct common_audit_data ad; 5732275bb41eSDavid Howells u32 sid = current_sid(); 57331da177e4SLinus Torvalds int rc; 57341da177e4SLinus Torvalds 5735be0554c9SStephen Smalley rc = ipc_alloc_security(&sma->sem_perm, SECCLASS_SEM); 57361da177e4SLinus Torvalds if (rc) 57371da177e4SLinus Torvalds return rc; 57381da177e4SLinus Torvalds 57391da177e4SLinus Torvalds isec = sma->sem_perm.security; 57401da177e4SLinus Torvalds 574150c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 57421da177e4SLinus Torvalds ad.u.ipc_id = sma->sem_perm.key; 57431da177e4SLinus Torvalds 5744275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, SECCLASS_SEM, 57451da177e4SLinus Torvalds SEM__CREATE, &ad); 57461da177e4SLinus Torvalds if (rc) { 57471da177e4SLinus Torvalds ipc_free_security(&sma->sem_perm); 57481da177e4SLinus Torvalds return rc; 57491da177e4SLinus Torvalds } 57501da177e4SLinus Torvalds return 0; 57511da177e4SLinus Torvalds } 57521da177e4SLinus Torvalds 57531da177e4SLinus Torvalds static void selinux_sem_free_security(struct sem_array *sma) 57541da177e4SLinus Torvalds { 57551da177e4SLinus Torvalds ipc_free_security(&sma->sem_perm); 57561da177e4SLinus Torvalds } 57571da177e4SLinus Torvalds 57581da177e4SLinus Torvalds static int selinux_sem_associate(struct sem_array *sma, int semflg) 57591da177e4SLinus Torvalds { 57601da177e4SLinus Torvalds struct ipc_security_struct *isec; 57612bf49690SThomas Liu struct common_audit_data ad; 5762275bb41eSDavid Howells u32 sid = current_sid(); 57631da177e4SLinus Torvalds 57641da177e4SLinus Torvalds isec = sma->sem_perm.security; 57651da177e4SLinus Torvalds 576650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 57671da177e4SLinus Torvalds ad.u.ipc_id = sma->sem_perm.key; 57681da177e4SLinus Torvalds 5769275bb41eSDavid Howells return avc_has_perm(sid, isec->sid, SECCLASS_SEM, 57701da177e4SLinus Torvalds SEM__ASSOCIATE, &ad); 57711da177e4SLinus Torvalds } 57721da177e4SLinus Torvalds 57731da177e4SLinus Torvalds /* Note, at this point, sma is locked down */ 57741da177e4SLinus Torvalds static int selinux_sem_semctl(struct sem_array *sma, int cmd) 57751da177e4SLinus Torvalds { 57761da177e4SLinus Torvalds int err; 57771da177e4SLinus Torvalds u32 perms; 57781da177e4SLinus Torvalds 57791da177e4SLinus Torvalds switch (cmd) { 57801da177e4SLinus Torvalds case IPC_INFO: 57811da177e4SLinus Torvalds case SEM_INFO: 57821da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 5783be0554c9SStephen Smalley return avc_has_perm(current_sid(), SECINITSID_KERNEL, 5784be0554c9SStephen Smalley SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL); 57851da177e4SLinus Torvalds case GETPID: 57861da177e4SLinus Torvalds case GETNCNT: 57871da177e4SLinus Torvalds case GETZCNT: 57881da177e4SLinus Torvalds perms = SEM__GETATTR; 57891da177e4SLinus Torvalds break; 57901da177e4SLinus Torvalds case GETVAL: 57911da177e4SLinus Torvalds case GETALL: 57921da177e4SLinus Torvalds perms = SEM__READ; 57931da177e4SLinus Torvalds break; 57941da177e4SLinus Torvalds case SETVAL: 57951da177e4SLinus Torvalds case SETALL: 57961da177e4SLinus Torvalds perms = SEM__WRITE; 57971da177e4SLinus Torvalds break; 57981da177e4SLinus Torvalds case IPC_RMID: 57991da177e4SLinus Torvalds perms = SEM__DESTROY; 58001da177e4SLinus Torvalds break; 58011da177e4SLinus Torvalds case IPC_SET: 58021da177e4SLinus Torvalds perms = SEM__SETATTR; 58031da177e4SLinus Torvalds break; 58041da177e4SLinus Torvalds case IPC_STAT: 58051da177e4SLinus Torvalds case SEM_STAT: 58061da177e4SLinus Torvalds perms = SEM__GETATTR | SEM__ASSOCIATE; 58071da177e4SLinus Torvalds break; 58081da177e4SLinus Torvalds default: 58091da177e4SLinus Torvalds return 0; 58101da177e4SLinus Torvalds } 58111da177e4SLinus Torvalds 58126af963f1SStephen Smalley err = ipc_has_perm(&sma->sem_perm, perms); 58131da177e4SLinus Torvalds return err; 58141da177e4SLinus Torvalds } 58151da177e4SLinus Torvalds 58161da177e4SLinus Torvalds static int selinux_sem_semop(struct sem_array *sma, 58171da177e4SLinus Torvalds struct sembuf *sops, unsigned nsops, int alter) 58181da177e4SLinus Torvalds { 58191da177e4SLinus Torvalds u32 perms; 58201da177e4SLinus Torvalds 58211da177e4SLinus Torvalds if (alter) 58221da177e4SLinus Torvalds perms = SEM__READ | SEM__WRITE; 58231da177e4SLinus Torvalds else 58241da177e4SLinus Torvalds perms = SEM__READ; 58251da177e4SLinus Torvalds 58266af963f1SStephen Smalley return ipc_has_perm(&sma->sem_perm, perms); 58271da177e4SLinus Torvalds } 58281da177e4SLinus Torvalds 58291da177e4SLinus Torvalds static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag) 58301da177e4SLinus Torvalds { 58311da177e4SLinus Torvalds u32 av = 0; 58321da177e4SLinus Torvalds 58331da177e4SLinus Torvalds av = 0; 58341da177e4SLinus Torvalds if (flag & S_IRUGO) 58351da177e4SLinus Torvalds av |= IPC__UNIX_READ; 58361da177e4SLinus Torvalds if (flag & S_IWUGO) 58371da177e4SLinus Torvalds av |= IPC__UNIX_WRITE; 58381da177e4SLinus Torvalds 58391da177e4SLinus Torvalds if (av == 0) 58401da177e4SLinus Torvalds return 0; 58411da177e4SLinus Torvalds 58426af963f1SStephen Smalley return ipc_has_perm(ipcp, av); 58431da177e4SLinus Torvalds } 58441da177e4SLinus Torvalds 5845713a04aeSAhmed S. Darwish static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid) 5846713a04aeSAhmed S. Darwish { 5847713a04aeSAhmed S. Darwish struct ipc_security_struct *isec = ipcp->security; 5848713a04aeSAhmed S. Darwish *secid = isec->sid; 5849713a04aeSAhmed S. Darwish } 5850713a04aeSAhmed S. Darwish 58511da177e4SLinus Torvalds static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode) 58521da177e4SLinus Torvalds { 58531da177e4SLinus Torvalds if (inode) 58541da177e4SLinus Torvalds inode_doinit_with_dentry(inode, dentry); 58551da177e4SLinus Torvalds } 58561da177e4SLinus Torvalds 58571da177e4SLinus Torvalds static int selinux_getprocattr(struct task_struct *p, 585804ff9708SAl Viro char *name, char **value) 58591da177e4SLinus Torvalds { 5860275bb41eSDavid Howells const struct task_security_struct *__tsec; 58618c8570fbSDustin Kirkland u32 sid; 58621da177e4SLinus Torvalds int error; 586304ff9708SAl Viro unsigned len; 58641da177e4SLinus Torvalds 5865275bb41eSDavid Howells rcu_read_lock(); 5866275bb41eSDavid Howells __tsec = __task_cred(p)->security; 58671da177e4SLinus Torvalds 5868be0554c9SStephen Smalley if (current != p) { 5869be0554c9SStephen Smalley error = avc_has_perm(current_sid(), __tsec->sid, 5870be0554c9SStephen Smalley SECCLASS_PROCESS, PROCESS__GETATTR, NULL); 5871be0554c9SStephen Smalley if (error) 5872be0554c9SStephen Smalley goto bad; 5873be0554c9SStephen Smalley } 5874be0554c9SStephen Smalley 58751da177e4SLinus Torvalds if (!strcmp(name, "current")) 5876275bb41eSDavid Howells sid = __tsec->sid; 58771da177e4SLinus Torvalds else if (!strcmp(name, "prev")) 5878275bb41eSDavid Howells sid = __tsec->osid; 58791da177e4SLinus Torvalds else if (!strcmp(name, "exec")) 5880275bb41eSDavid Howells sid = __tsec->exec_sid; 58811da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 5882275bb41eSDavid Howells sid = __tsec->create_sid; 58834eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) 5884275bb41eSDavid Howells sid = __tsec->keycreate_sid; 588542c3e03eSEric Paris else if (!strcmp(name, "sockcreate")) 5886275bb41eSDavid Howells sid = __tsec->sockcreate_sid; 5887be0554c9SStephen Smalley else { 5888be0554c9SStephen Smalley error = -EINVAL; 5889be0554c9SStephen Smalley goto bad; 5890be0554c9SStephen Smalley } 5891275bb41eSDavid Howells rcu_read_unlock(); 58921da177e4SLinus Torvalds 58931da177e4SLinus Torvalds if (!sid) 58941da177e4SLinus Torvalds return 0; 58951da177e4SLinus Torvalds 589604ff9708SAl Viro error = security_sid_to_context(sid, value, &len); 589704ff9708SAl Viro if (error) 589804ff9708SAl Viro return error; 589904ff9708SAl Viro return len; 5900275bb41eSDavid Howells 5901be0554c9SStephen Smalley bad: 5902275bb41eSDavid Howells rcu_read_unlock(); 5903be0554c9SStephen Smalley return error; 59041da177e4SLinus Torvalds } 59051da177e4SLinus Torvalds 5906b21507e2SStephen Smalley static int selinux_setprocattr(const char *name, void *value, size_t size) 59071da177e4SLinus Torvalds { 59081da177e4SLinus Torvalds struct task_security_struct *tsec; 5909d84f4f99SDavid Howells struct cred *new; 5910be0554c9SStephen Smalley u32 mysid = current_sid(), sid = 0, ptsid; 59111da177e4SLinus Torvalds int error; 59121da177e4SLinus Torvalds char *str = value; 59131da177e4SLinus Torvalds 59141da177e4SLinus Torvalds /* 59151da177e4SLinus Torvalds * Basic control over ability to set these attributes at all. 59161da177e4SLinus Torvalds */ 59171da177e4SLinus Torvalds if (!strcmp(name, "exec")) 5918be0554c9SStephen Smalley error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS, 5919be0554c9SStephen Smalley PROCESS__SETEXEC, NULL); 59201da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 5921be0554c9SStephen Smalley error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS, 5922be0554c9SStephen Smalley PROCESS__SETFSCREATE, NULL); 59234eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) 5924be0554c9SStephen Smalley error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS, 5925be0554c9SStephen Smalley PROCESS__SETKEYCREATE, NULL); 592642c3e03eSEric Paris else if (!strcmp(name, "sockcreate")) 5927be0554c9SStephen Smalley error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS, 5928be0554c9SStephen Smalley PROCESS__SETSOCKCREATE, NULL); 59291da177e4SLinus Torvalds else if (!strcmp(name, "current")) 5930be0554c9SStephen Smalley error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS, 5931be0554c9SStephen Smalley PROCESS__SETCURRENT, NULL); 59321da177e4SLinus Torvalds else 59331da177e4SLinus Torvalds error = -EINVAL; 59341da177e4SLinus Torvalds if (error) 59351da177e4SLinus Torvalds return error; 59361da177e4SLinus Torvalds 59371da177e4SLinus Torvalds /* Obtain a SID for the context, if one was specified. */ 5938a050a570SStephen Smalley if (size && str[0] && str[0] != '\n') { 59391da177e4SLinus Torvalds if (str[size-1] == '\n') { 59401da177e4SLinus Torvalds str[size-1] = 0; 59411da177e4SLinus Torvalds size--; 59421da177e4SLinus Torvalds } 594352a4c640SNikolay Aleksandrov error = security_context_to_sid(value, size, &sid, GFP_KERNEL); 594412b29f34SStephen Smalley if (error == -EINVAL && !strcmp(name, "fscreate")) { 5945db59000aSStephen Smalley if (!has_cap_mac_admin(true)) { 5946d6ea83ecSEric Paris struct audit_buffer *ab; 5947d6ea83ecSEric Paris size_t audit_size; 5948d6ea83ecSEric Paris 5949d6ea83ecSEric Paris /* We strip a nul only if it is at the end, otherwise the 5950d6ea83ecSEric Paris * context contains a nul and we should audit that */ 5951d6ea83ecSEric Paris if (str[size - 1] == '\0') 5952d6ea83ecSEric Paris audit_size = size - 1; 5953d6ea83ecSEric Paris else 5954d6ea83ecSEric Paris audit_size = size; 5955d6ea83ecSEric Paris ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR); 5956d6ea83ecSEric Paris audit_log_format(ab, "op=fscreate invalid_context="); 5957d6ea83ecSEric Paris audit_log_n_untrustedstring(ab, value, audit_size); 5958d6ea83ecSEric Paris audit_log_end(ab); 5959d6ea83ecSEric Paris 596012b29f34SStephen Smalley return error; 5961d6ea83ecSEric Paris } 596212b29f34SStephen Smalley error = security_context_to_sid_force(value, size, 596312b29f34SStephen Smalley &sid); 596412b29f34SStephen Smalley } 59651da177e4SLinus Torvalds if (error) 59661da177e4SLinus Torvalds return error; 59671da177e4SLinus Torvalds } 59681da177e4SLinus Torvalds 5969d84f4f99SDavid Howells new = prepare_creds(); 5970d84f4f99SDavid Howells if (!new) 5971d84f4f99SDavid Howells return -ENOMEM; 5972d84f4f99SDavid Howells 59731da177e4SLinus Torvalds /* Permission checking based on the specified context is 59741da177e4SLinus Torvalds performed during the actual operation (execve, 59751da177e4SLinus Torvalds open/mkdir/...), when we know the full context of the 5976d84f4f99SDavid Howells operation. See selinux_bprm_set_creds for the execve 59771da177e4SLinus Torvalds checks and may_create for the file creation checks. The 59781da177e4SLinus Torvalds operation will then fail if the context is not permitted. */ 5979d84f4f99SDavid Howells tsec = new->security; 5980d84f4f99SDavid Howells if (!strcmp(name, "exec")) { 59811da177e4SLinus Torvalds tsec->exec_sid = sid; 5982d84f4f99SDavid Howells } else if (!strcmp(name, "fscreate")) { 59831da177e4SLinus Torvalds tsec->create_sid = sid; 5984d84f4f99SDavid Howells } else if (!strcmp(name, "keycreate")) { 5985be0554c9SStephen Smalley error = avc_has_perm(mysid, sid, SECCLASS_KEY, KEY__CREATE, 5986be0554c9SStephen Smalley NULL); 59874eb582cfSMichael LeMay if (error) 5988d84f4f99SDavid Howells goto abort_change; 59894eb582cfSMichael LeMay tsec->keycreate_sid = sid; 5990d84f4f99SDavid Howells } else if (!strcmp(name, "sockcreate")) { 599142c3e03eSEric Paris tsec->sockcreate_sid = sid; 5992d84f4f99SDavid Howells } else if (!strcmp(name, "current")) { 5993d84f4f99SDavid Howells error = -EINVAL; 59941da177e4SLinus Torvalds if (sid == 0) 5995d84f4f99SDavid Howells goto abort_change; 5996d9250deaSKaiGai Kohei 5997d84f4f99SDavid Howells /* Only allow single threaded processes to change context */ 5998d84f4f99SDavid Howells error = -EPERM; 59995bb459bbSOleg Nesterov if (!current_is_single_threaded()) { 6000d84f4f99SDavid Howells error = security_bounded_transition(tsec->sid, sid); 6001d84f4f99SDavid Howells if (error) 6002d84f4f99SDavid Howells goto abort_change; 60031da177e4SLinus Torvalds } 60041da177e4SLinus Torvalds 60051da177e4SLinus Torvalds /* Check permissions for the transition. */ 60061da177e4SLinus Torvalds error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS, 60071da177e4SLinus Torvalds PROCESS__DYNTRANSITION, NULL); 60081da177e4SLinus Torvalds if (error) 6009d84f4f99SDavid Howells goto abort_change; 60101da177e4SLinus Torvalds 60111da177e4SLinus Torvalds /* Check for ptracing, and update the task SID if ok. 60121da177e4SLinus Torvalds Otherwise, leave SID unchanged and fail. */ 6013be0554c9SStephen Smalley ptsid = ptrace_parent_sid(); 60140c6181cbSPaul Moore if (ptsid != 0) { 6015d84f4f99SDavid Howells error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS, 6016d84f4f99SDavid Howells PROCESS__PTRACE, NULL); 6017d84f4f99SDavid Howells if (error) 6018d84f4f99SDavid Howells goto abort_change; 6019d84f4f99SDavid Howells } 6020d84f4f99SDavid Howells 6021d84f4f99SDavid Howells tsec->sid = sid; 6022d84f4f99SDavid Howells } else { 6023d84f4f99SDavid Howells error = -EINVAL; 6024d84f4f99SDavid Howells goto abort_change; 6025d84f4f99SDavid Howells } 6026d84f4f99SDavid Howells 6027d84f4f99SDavid Howells commit_creds(new); 60281da177e4SLinus Torvalds return size; 6029d84f4f99SDavid Howells 6030d84f4f99SDavid Howells abort_change: 6031d84f4f99SDavid Howells abort_creds(new); 6032d84f4f99SDavid Howells return error; 60331da177e4SLinus Torvalds } 60341da177e4SLinus Torvalds 6035746df9b5SDavid Quigley static int selinux_ismaclabel(const char *name) 6036746df9b5SDavid Quigley { 6037746df9b5SDavid Quigley return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0); 6038746df9b5SDavid Quigley } 6039746df9b5SDavid Quigley 6040dc49c1f9SCatherine Zhang static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) 6041dc49c1f9SCatherine Zhang { 6042dc49c1f9SCatherine Zhang return security_sid_to_context(secid, secdata, seclen); 6043dc49c1f9SCatherine Zhang } 6044dc49c1f9SCatherine Zhang 60457bf570dcSDavid Howells static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid) 604663cb3449SDavid Howells { 604752a4c640SNikolay Aleksandrov return security_context_to_sid(secdata, seclen, secid, GFP_KERNEL); 604863cb3449SDavid Howells } 604963cb3449SDavid Howells 6050dc49c1f9SCatherine Zhang static void selinux_release_secctx(char *secdata, u32 seclen) 6051dc49c1f9SCatherine Zhang { 6052dc49c1f9SCatherine Zhang kfree(secdata); 6053dc49c1f9SCatherine Zhang } 6054dc49c1f9SCatherine Zhang 60556f3be9f5SAndreas Gruenbacher static void selinux_inode_invalidate_secctx(struct inode *inode) 60566f3be9f5SAndreas Gruenbacher { 60576f3be9f5SAndreas Gruenbacher struct inode_security_struct *isec = inode->i_security; 60586f3be9f5SAndreas Gruenbacher 60599287aed2SAndreas Gruenbacher spin_lock(&isec->lock); 60606f3be9f5SAndreas Gruenbacher isec->initialized = LABEL_INVALID; 60619287aed2SAndreas Gruenbacher spin_unlock(&isec->lock); 60626f3be9f5SAndreas Gruenbacher } 60636f3be9f5SAndreas Gruenbacher 60641ee65e37SDavid P. Quigley /* 60651ee65e37SDavid P. Quigley * called with inode->i_mutex locked 60661ee65e37SDavid P. Quigley */ 60671ee65e37SDavid P. Quigley static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen) 60681ee65e37SDavid P. Quigley { 60691ee65e37SDavid P. Quigley return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0); 60701ee65e37SDavid P. Quigley } 60711ee65e37SDavid P. Quigley 60721ee65e37SDavid P. Quigley /* 60731ee65e37SDavid P. Quigley * called with inode->i_mutex locked 60741ee65e37SDavid P. Quigley */ 60751ee65e37SDavid P. Quigley static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen) 60761ee65e37SDavid P. Quigley { 60771ee65e37SDavid P. Quigley return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0); 60781ee65e37SDavid P. Quigley } 60791ee65e37SDavid P. Quigley 60801ee65e37SDavid P. Quigley static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen) 60811ee65e37SDavid P. Quigley { 60821ee65e37SDavid P. Quigley int len = 0; 60831ee65e37SDavid P. Quigley len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX, 60841ee65e37SDavid P. Quigley ctx, true); 60851ee65e37SDavid P. Quigley if (len < 0) 60861ee65e37SDavid P. Quigley return len; 60871ee65e37SDavid P. Quigley *ctxlen = len; 60881ee65e37SDavid P. Quigley return 0; 60891ee65e37SDavid P. Quigley } 6090d720024eSMichael LeMay #ifdef CONFIG_KEYS 6091d720024eSMichael LeMay 6092d84f4f99SDavid Howells static int selinux_key_alloc(struct key *k, const struct cred *cred, 60937e047ef5SDavid Howells unsigned long flags) 6094d720024eSMichael LeMay { 6095d84f4f99SDavid Howells const struct task_security_struct *tsec; 6096d720024eSMichael LeMay struct key_security_struct *ksec; 6097d720024eSMichael LeMay 6098d720024eSMichael LeMay ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL); 6099d720024eSMichael LeMay if (!ksec) 6100d720024eSMichael LeMay return -ENOMEM; 6101d720024eSMichael LeMay 6102d84f4f99SDavid Howells tsec = cred->security; 6103d84f4f99SDavid Howells if (tsec->keycreate_sid) 6104d84f4f99SDavid Howells ksec->sid = tsec->keycreate_sid; 61054eb582cfSMichael LeMay else 6106d84f4f99SDavid Howells ksec->sid = tsec->sid; 6107d720024eSMichael LeMay 6108275bb41eSDavid Howells k->security = ksec; 6109d720024eSMichael LeMay return 0; 6110d720024eSMichael LeMay } 6111d720024eSMichael LeMay 6112d720024eSMichael LeMay static void selinux_key_free(struct key *k) 6113d720024eSMichael LeMay { 6114d720024eSMichael LeMay struct key_security_struct *ksec = k->security; 6115d720024eSMichael LeMay 6116d720024eSMichael LeMay k->security = NULL; 6117d720024eSMichael LeMay kfree(ksec); 6118d720024eSMichael LeMay } 6119d720024eSMichael LeMay 6120d720024eSMichael LeMay static int selinux_key_permission(key_ref_t key_ref, 6121d84f4f99SDavid Howells const struct cred *cred, 6122f5895943SDavid Howells unsigned perm) 6123d720024eSMichael LeMay { 6124d720024eSMichael LeMay struct key *key; 6125d720024eSMichael LeMay struct key_security_struct *ksec; 6126275bb41eSDavid Howells u32 sid; 6127d720024eSMichael LeMay 6128d720024eSMichael LeMay /* if no specific permissions are requested, we skip the 6129d720024eSMichael LeMay permission check. No serious, additional covert channels 6130d720024eSMichael LeMay appear to be created. */ 6131d720024eSMichael LeMay if (perm == 0) 6132d720024eSMichael LeMay return 0; 6133d720024eSMichael LeMay 6134d84f4f99SDavid Howells sid = cred_sid(cred); 6135275bb41eSDavid Howells 6136275bb41eSDavid Howells key = key_ref_to_ptr(key_ref); 6137275bb41eSDavid Howells ksec = key->security; 6138275bb41eSDavid Howells 6139275bb41eSDavid Howells return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL); 6140d720024eSMichael LeMay } 6141d720024eSMichael LeMay 614270a5bb72SDavid Howells static int selinux_key_getsecurity(struct key *key, char **_buffer) 614370a5bb72SDavid Howells { 614470a5bb72SDavid Howells struct key_security_struct *ksec = key->security; 614570a5bb72SDavid Howells char *context = NULL; 614670a5bb72SDavid Howells unsigned len; 614770a5bb72SDavid Howells int rc; 614870a5bb72SDavid Howells 614970a5bb72SDavid Howells rc = security_sid_to_context(ksec->sid, &context, &len); 615070a5bb72SDavid Howells if (!rc) 615170a5bb72SDavid Howells rc = len; 615270a5bb72SDavid Howells *_buffer = context; 615370a5bb72SDavid Howells return rc; 615470a5bb72SDavid Howells } 615570a5bb72SDavid Howells 6156d720024eSMichael LeMay #endif 6157d720024eSMichael LeMay 6158ca97d939SJames Morris static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = { 6159e20b043aSCasey Schaufler LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr), 6160e20b043aSCasey Schaufler LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction), 6161e20b043aSCasey Schaufler LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder), 6162e20b043aSCasey Schaufler LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file), 6163076c54c5SAhmed S. Darwish 6164e20b043aSCasey Schaufler LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check), 6165e20b043aSCasey Schaufler LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme), 6166e20b043aSCasey Schaufler LSM_HOOK_INIT(capget, selinux_capget), 6167e20b043aSCasey Schaufler LSM_HOOK_INIT(capset, selinux_capset), 6168e20b043aSCasey Schaufler LSM_HOOK_INIT(capable, selinux_capable), 6169e20b043aSCasey Schaufler LSM_HOOK_INIT(quotactl, selinux_quotactl), 6170e20b043aSCasey Schaufler LSM_HOOK_INIT(quota_on, selinux_quota_on), 6171e20b043aSCasey Schaufler LSM_HOOK_INIT(syslog, selinux_syslog), 6172e20b043aSCasey Schaufler LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory), 617379af7307SStephen Smalley 6174e20b043aSCasey Schaufler LSM_HOOK_INIT(netlink_send, selinux_netlink_send), 61751da177e4SLinus Torvalds 6176e20b043aSCasey Schaufler LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds), 6177e20b043aSCasey Schaufler LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds), 6178e20b043aSCasey Schaufler LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds), 6179e20b043aSCasey Schaufler LSM_HOOK_INIT(bprm_secureexec, selinux_bprm_secureexec), 61801da177e4SLinus Torvalds 6181e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security), 6182e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security), 6183e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_copy_data, selinux_sb_copy_data), 6184e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_remount, selinux_sb_remount), 6185e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount), 6186e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options), 6187e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs), 6188e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_mount, selinux_mount), 6189e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_umount, selinux_umount), 6190e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts), 6191e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts), 6192e20b043aSCasey Schaufler LSM_HOOK_INIT(sb_parse_opts_str, selinux_parse_opts_str), 61931da177e4SLinus Torvalds 6194e20b043aSCasey Schaufler LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security), 6195a518b0a5SVivek Goyal LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as), 6196e0007529SEric Paris 6197e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security), 6198e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security), 6199e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security), 6200e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_create, selinux_inode_create), 6201e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_link, selinux_inode_link), 6202e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink), 6203e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink), 6204e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir), 6205e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir), 6206e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod), 6207e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_rename, selinux_inode_rename), 6208e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink), 6209e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link), 6210e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_permission, selinux_inode_permission), 6211e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr), 6212e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr), 6213e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr), 6214e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr), 6215e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr), 6216e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr), 6217e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr), 6218e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity), 6219e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity), 6220e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity), 6221e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid), 622256909eb3SVivek Goyal LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up), 622319472b69SVivek Goyal LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr), 62241da177e4SLinus Torvalds 6225e20b043aSCasey Schaufler LSM_HOOK_INIT(file_permission, selinux_file_permission), 6226e20b043aSCasey Schaufler LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security), 6227e20b043aSCasey Schaufler LSM_HOOK_INIT(file_free_security, selinux_file_free_security), 6228e20b043aSCasey Schaufler LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl), 6229e20b043aSCasey Schaufler LSM_HOOK_INIT(mmap_file, selinux_mmap_file), 6230e20b043aSCasey Schaufler LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr), 6231e20b043aSCasey Schaufler LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect), 6232e20b043aSCasey Schaufler LSM_HOOK_INIT(file_lock, selinux_file_lock), 6233e20b043aSCasey Schaufler LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl), 6234e20b043aSCasey Schaufler LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner), 6235e20b043aSCasey Schaufler LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask), 6236e20b043aSCasey Schaufler LSM_HOOK_INIT(file_receive, selinux_file_receive), 62371da177e4SLinus Torvalds 6238e20b043aSCasey Schaufler LSM_HOOK_INIT(file_open, selinux_file_open), 62391da177e4SLinus Torvalds 6240a79be238STetsuo Handa LSM_HOOK_INIT(task_alloc, selinux_task_alloc), 6241e20b043aSCasey Schaufler LSM_HOOK_INIT(cred_alloc_blank, selinux_cred_alloc_blank), 6242e20b043aSCasey Schaufler LSM_HOOK_INIT(cred_free, selinux_cred_free), 6243e20b043aSCasey Schaufler LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare), 6244e20b043aSCasey Schaufler LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer), 6245e20b043aSCasey Schaufler LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as), 6246e20b043aSCasey Schaufler LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as), 6247e20b043aSCasey Schaufler LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request), 624861d612eaSJeff Vander Stoep LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file), 6249e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid), 6250e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid), 6251e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getsid, selinux_task_getsid), 6252e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid), 6253e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setnice, selinux_task_setnice), 6254e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio), 6255e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio), 6256791ec491SStephen Smalley LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit), 6257e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit), 6258e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler), 6259e20b043aSCasey Schaufler LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler), 6260e20b043aSCasey Schaufler LSM_HOOK_INIT(task_movememory, selinux_task_movememory), 6261e20b043aSCasey Schaufler LSM_HOOK_INIT(task_kill, selinux_task_kill), 6262e20b043aSCasey Schaufler LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode), 6263788e7dd4SYuichi Nakamura 6264e20b043aSCasey Schaufler LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission), 6265e20b043aSCasey Schaufler LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid), 62661da177e4SLinus Torvalds 6267e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security), 6268e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_msg_free_security, selinux_msg_msg_free_security), 62691da177e4SLinus Torvalds 6270e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_alloc_security, 6271e20b043aSCasey Schaufler selinux_msg_queue_alloc_security), 6272e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_free_security, selinux_msg_queue_free_security), 6273e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate), 6274e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl), 6275e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd), 6276e20b043aSCasey Schaufler LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv), 62771da177e4SLinus Torvalds 6278e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security), 6279e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_free_security, selinux_shm_free_security), 6280e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_associate, selinux_shm_associate), 6281e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl), 6282e20b043aSCasey Schaufler LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat), 62831da177e4SLinus Torvalds 6284e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security), 6285e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_free_security, selinux_sem_free_security), 6286e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_associate, selinux_sem_associate), 6287e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl), 6288e20b043aSCasey Schaufler LSM_HOOK_INIT(sem_semop, selinux_sem_semop), 62891da177e4SLinus Torvalds 6290e20b043aSCasey Schaufler LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate), 62911da177e4SLinus Torvalds 6292e20b043aSCasey Schaufler LSM_HOOK_INIT(getprocattr, selinux_getprocattr), 6293e20b043aSCasey Schaufler LSM_HOOK_INIT(setprocattr, selinux_setprocattr), 62941da177e4SLinus Torvalds 6295e20b043aSCasey Schaufler LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel), 6296e20b043aSCasey Schaufler LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx), 6297e20b043aSCasey Schaufler LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid), 6298e20b043aSCasey Schaufler LSM_HOOK_INIT(release_secctx, selinux_release_secctx), 62996f3be9f5SAndreas Gruenbacher LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx), 6300e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx), 6301e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx), 6302e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx), 63031da177e4SLinus Torvalds 6304e20b043aSCasey Schaufler LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect), 6305e20b043aSCasey Schaufler LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send), 6306dc49c1f9SCatherine Zhang 6307e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_create, selinux_socket_create), 6308e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create), 6309e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_bind, selinux_socket_bind), 6310e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_connect, selinux_socket_connect), 6311e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_listen, selinux_socket_listen), 6312e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_accept, selinux_socket_accept), 6313e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg), 6314e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg), 6315e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname), 6316e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername), 6317e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt), 6318e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt), 6319e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown), 6320e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb), 6321e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getpeersec_stream, 6322e20b043aSCasey Schaufler selinux_socket_getpeersec_stream), 6323e20b043aSCasey Schaufler LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram), 6324e20b043aSCasey Schaufler LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security), 6325e20b043aSCasey Schaufler LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security), 6326e20b043aSCasey Schaufler LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security), 6327e20b043aSCasey Schaufler LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid), 6328e20b043aSCasey Schaufler LSM_HOOK_INIT(sock_graft, selinux_sock_graft), 6329e20b043aSCasey Schaufler LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request), 6330e20b043aSCasey Schaufler LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone), 6331e20b043aSCasey Schaufler LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established), 6332e20b043aSCasey Schaufler LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet), 6333e20b043aSCasey Schaufler LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc), 6334e20b043aSCasey Schaufler LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec), 6335e20b043aSCasey Schaufler LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow), 6336e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security), 6337e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security), 6338e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create), 6339e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue), 6340e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach), 6341e20b043aSCasey Schaufler LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open), 6342d28d1e08STrent Jaeger 6343d28d1e08STrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM 6344e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc), 6345e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone), 6346e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free), 6347e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete), 6348e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc), 6349e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_alloc_acquire, 6350e20b043aSCasey Schaufler selinux_xfrm_state_alloc_acquire), 6351e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free), 6352e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete), 6353e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup), 6354e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_state_pol_flow_match, 6355e20b043aSCasey Schaufler selinux_xfrm_state_pol_flow_match), 6356e20b043aSCasey Schaufler LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session), 63571da177e4SLinus Torvalds #endif 6358d720024eSMichael LeMay 6359d720024eSMichael LeMay #ifdef CONFIG_KEYS 6360e20b043aSCasey Schaufler LSM_HOOK_INIT(key_alloc, selinux_key_alloc), 6361e20b043aSCasey Schaufler LSM_HOOK_INIT(key_free, selinux_key_free), 6362e20b043aSCasey Schaufler LSM_HOOK_INIT(key_permission, selinux_key_permission), 6363e20b043aSCasey Schaufler LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity), 6364d720024eSMichael LeMay #endif 63659d57a7f9SAhmed S. Darwish 63669d57a7f9SAhmed S. Darwish #ifdef CONFIG_AUDIT 6367e20b043aSCasey Schaufler LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init), 6368e20b043aSCasey Schaufler LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known), 6369e20b043aSCasey Schaufler LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match), 6370e20b043aSCasey Schaufler LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free), 63719d57a7f9SAhmed S. Darwish #endif 63721da177e4SLinus Torvalds }; 63731da177e4SLinus Torvalds 63741da177e4SLinus Torvalds static __init int selinux_init(void) 63751da177e4SLinus Torvalds { 6376b1d9e6b0SCasey Schaufler if (!security_module_enable("selinux")) { 6377076c54c5SAhmed S. Darwish selinux_enabled = 0; 6378076c54c5SAhmed S. Darwish return 0; 6379076c54c5SAhmed S. Darwish } 6380076c54c5SAhmed S. Darwish 63811da177e4SLinus Torvalds if (!selinux_enabled) { 63821da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Disabled at boot.\n"); 63831da177e4SLinus Torvalds return 0; 63841da177e4SLinus Torvalds } 63851da177e4SLinus Torvalds 63861da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Initializing.\n"); 63871da177e4SLinus Torvalds 63881da177e4SLinus Torvalds /* Set the security state for the initial task. */ 6389d84f4f99SDavid Howells cred_init_security(); 63901da177e4SLinus Torvalds 6391fcaaade1SStephen Smalley default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC); 6392fcaaade1SStephen Smalley 63937cae7e26SJames Morris sel_inode_cache = kmem_cache_create("selinux_inode_security", 63947cae7e26SJames Morris sizeof(struct inode_security_struct), 639520c2df83SPaul Mundt 0, SLAB_PANIC, NULL); 639663205654SSangwoo file_security_cache = kmem_cache_create("selinux_file_security", 639763205654SSangwoo sizeof(struct file_security_struct), 639863205654SSangwoo 0, SLAB_PANIC, NULL); 63991da177e4SLinus Torvalds avc_init(); 64001da177e4SLinus Torvalds 6401d69dece5SCasey Schaufler security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux"); 64021da177e4SLinus Torvalds 6403615e51fdSPaul Moore if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET)) 6404615e51fdSPaul Moore panic("SELinux: Unable to register AVC netcache callback\n"); 6405615e51fdSPaul Moore 6406828dfe1dSEric Paris if (selinux_enforcing) 6407fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Starting in enforcing mode\n"); 6408828dfe1dSEric Paris else 6409fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Starting in permissive mode\n"); 6410d720024eSMichael LeMay 64111da177e4SLinus Torvalds return 0; 64121da177e4SLinus Torvalds } 64131da177e4SLinus Torvalds 6414e8c26255SAl Viro static void delayed_superblock_init(struct super_block *sb, void *unused) 6415e8c26255SAl Viro { 6416e8c26255SAl Viro superblock_doinit(sb, NULL); 6417e8c26255SAl Viro } 6418e8c26255SAl Viro 64191da177e4SLinus Torvalds void selinux_complete_init(void) 64201da177e4SLinus Torvalds { 6421fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Completing initialization.\n"); 64221da177e4SLinus Torvalds 64231da177e4SLinus Torvalds /* Set up any superblocks initialized prior to the policy load. */ 6424fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Setting up existing superblocks.\n"); 6425e8c26255SAl Viro iterate_supers(delayed_superblock_init, NULL); 64261da177e4SLinus Torvalds } 64271da177e4SLinus Torvalds 64281da177e4SLinus Torvalds /* SELinux requires early initialization in order to label 64291da177e4SLinus Torvalds all processes and objects when they are created. */ 64301da177e4SLinus Torvalds security_initcall(selinux_init); 64311da177e4SLinus Torvalds 6432c2b507fdSStephen Smalley #if defined(CONFIG_NETFILTER) 64331da177e4SLinus Torvalds 643425db6beaSJiri Pirko static struct nf_hook_ops selinux_nf_ops[] = { 6435effad8dfSPaul Moore { 6436effad8dfSPaul Moore .hook = selinux_ipv4_postroute, 64372597a834SAlban Crequy .pf = NFPROTO_IPV4, 64386e23ae2aSPatrick McHardy .hooknum = NF_INET_POST_ROUTING, 64391da177e4SLinus Torvalds .priority = NF_IP_PRI_SELINUX_LAST, 6440effad8dfSPaul Moore }, 6441effad8dfSPaul Moore { 6442effad8dfSPaul Moore .hook = selinux_ipv4_forward, 64432597a834SAlban Crequy .pf = NFPROTO_IPV4, 6444effad8dfSPaul Moore .hooknum = NF_INET_FORWARD, 6445effad8dfSPaul Moore .priority = NF_IP_PRI_SELINUX_FIRST, 6446948bf85cSPaul Moore }, 6447948bf85cSPaul Moore { 6448948bf85cSPaul Moore .hook = selinux_ipv4_output, 64492597a834SAlban Crequy .pf = NFPROTO_IPV4, 6450948bf85cSPaul Moore .hooknum = NF_INET_LOCAL_OUT, 6451948bf85cSPaul Moore .priority = NF_IP_PRI_SELINUX_FIRST, 645225db6beaSJiri Pirko }, 64531a93a6eaSJavier Martinez Canillas #if IS_ENABLED(CONFIG_IPV6) 6454effad8dfSPaul Moore { 6455effad8dfSPaul Moore .hook = selinux_ipv6_postroute, 64562597a834SAlban Crequy .pf = NFPROTO_IPV6, 64576e23ae2aSPatrick McHardy .hooknum = NF_INET_POST_ROUTING, 64581da177e4SLinus Torvalds .priority = NF_IP6_PRI_SELINUX_LAST, 6459effad8dfSPaul Moore }, 6460effad8dfSPaul Moore { 6461effad8dfSPaul Moore .hook = selinux_ipv6_forward, 64622597a834SAlban Crequy .pf = NFPROTO_IPV6, 6463effad8dfSPaul Moore .hooknum = NF_INET_FORWARD, 6464effad8dfSPaul Moore .priority = NF_IP6_PRI_SELINUX_FIRST, 646525db6beaSJiri Pirko }, 64662917f57bSHuw Davies { 64672917f57bSHuw Davies .hook = selinux_ipv6_output, 64682917f57bSHuw Davies .pf = NFPROTO_IPV6, 64692917f57bSHuw Davies .hooknum = NF_INET_LOCAL_OUT, 64702917f57bSHuw Davies .priority = NF_IP6_PRI_SELINUX_FIRST, 64712917f57bSHuw Davies }, 64721da177e4SLinus Torvalds #endif /* IPV6 */ 647325db6beaSJiri Pirko }; 64741da177e4SLinus Torvalds 64751da177e4SLinus Torvalds static int __init selinux_nf_ip_init(void) 64761da177e4SLinus Torvalds { 647725db6beaSJiri Pirko int err; 64781da177e4SLinus Torvalds 64791da177e4SLinus Torvalds if (!selinux_enabled) 648025db6beaSJiri Pirko return 0; 64811da177e4SLinus Torvalds 6482fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Registering netfilter hooks\n"); 64831da177e4SLinus Torvalds 648425db6beaSJiri Pirko err = nf_register_hooks(selinux_nf_ops, ARRAY_SIZE(selinux_nf_ops)); 64851da177e4SLinus Torvalds if (err) 648625db6beaSJiri Pirko panic("SELinux: nf_register_hooks: error %d\n", err); 64871da177e4SLinus Torvalds 648825db6beaSJiri Pirko return 0; 64891da177e4SLinus Torvalds } 64901da177e4SLinus Torvalds 64911da177e4SLinus Torvalds __initcall(selinux_nf_ip_init); 64921da177e4SLinus Torvalds 64931da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 64941da177e4SLinus Torvalds static void selinux_nf_ip_exit(void) 64951da177e4SLinus Torvalds { 6496fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Unregistering netfilter hooks\n"); 64971da177e4SLinus Torvalds 649825db6beaSJiri Pirko nf_unregister_hooks(selinux_nf_ops, ARRAY_SIZE(selinux_nf_ops)); 64991da177e4SLinus Torvalds } 65001da177e4SLinus Torvalds #endif 65011da177e4SLinus Torvalds 6502c2b507fdSStephen Smalley #else /* CONFIG_NETFILTER */ 65031da177e4SLinus Torvalds 65041da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 65051da177e4SLinus Torvalds #define selinux_nf_ip_exit() 65061da177e4SLinus Torvalds #endif 65071da177e4SLinus Torvalds 6508c2b507fdSStephen Smalley #endif /* CONFIG_NETFILTER */ 65091da177e4SLinus Torvalds 65101da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 6511828dfe1dSEric Paris static int selinux_disabled; 6512828dfe1dSEric Paris 65131da177e4SLinus Torvalds int selinux_disable(void) 65141da177e4SLinus Torvalds { 65151da177e4SLinus Torvalds if (ss_initialized) { 65161da177e4SLinus Torvalds /* Not permitted after initial policy load. */ 65171da177e4SLinus Torvalds return -EINVAL; 65181da177e4SLinus Torvalds } 65191da177e4SLinus Torvalds 65201da177e4SLinus Torvalds if (selinux_disabled) { 65211da177e4SLinus Torvalds /* Only do this once. */ 65221da177e4SLinus Torvalds return -EINVAL; 65231da177e4SLinus Torvalds } 65241da177e4SLinus Torvalds 65251da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Disabled at runtime.\n"); 65261da177e4SLinus Torvalds 65271da177e4SLinus Torvalds selinux_disabled = 1; 652830d55280SStephen Smalley selinux_enabled = 0; 65291da177e4SLinus Torvalds 6530b1d9e6b0SCasey Schaufler security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks)); 65311da177e4SLinus Torvalds 6532af8ff049SEric Paris /* Try to destroy the avc node cache */ 6533af8ff049SEric Paris avc_disable(); 6534af8ff049SEric Paris 65351da177e4SLinus Torvalds /* Unregister netfilter hooks. */ 65361da177e4SLinus Torvalds selinux_nf_ip_exit(); 65371da177e4SLinus Torvalds 65381da177e4SLinus Torvalds /* Unregister selinuxfs. */ 65391da177e4SLinus Torvalds exit_sel_fs(); 65401da177e4SLinus Torvalds 65411da177e4SLinus Torvalds return 0; 65421da177e4SLinus Torvalds } 65431da177e4SLinus Torvalds #endif 6544