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> 311da177e4SLinus Torvalds #include <linux/sched.h> 321da177e4SLinus Torvalds #include <linux/security.h> 331da177e4SLinus Torvalds #include <linux/xattr.h> 341da177e4SLinus Torvalds #include <linux/capability.h> 351da177e4SLinus Torvalds #include <linux/unistd.h> 361da177e4SLinus Torvalds #include <linux/mm.h> 371da177e4SLinus Torvalds #include <linux/mman.h> 381da177e4SLinus Torvalds #include <linux/slab.h> 391da177e4SLinus Torvalds #include <linux/pagemap.h> 400b24dcb7SEric Paris #include <linux/proc_fs.h> 411da177e4SLinus Torvalds #include <linux/swap.h> 421da177e4SLinus Torvalds #include <linux/spinlock.h> 431da177e4SLinus Torvalds #include <linux/syscalls.h> 442a7dba39SEric Paris #include <linux/dcache.h> 451da177e4SLinus Torvalds #include <linux/file.h> 469f3acc31SAl Viro #include <linux/fdtable.h> 471da177e4SLinus Torvalds #include <linux/namei.h> 481da177e4SLinus Torvalds #include <linux/mount.h> 491da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h> 501da177e4SLinus Torvalds #include <linux/netfilter_ipv6.h> 511da177e4SLinus Torvalds #include <linux/tty.h> 521da177e4SLinus Torvalds #include <net/icmp.h> 53227b60f5SStephen Hemminger #include <net/ip.h> /* for local_port_range[] */ 54ca10b9e9SEric Dumazet #include <net/sock.h> 551da177e4SLinus Torvalds #include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */ 56220deb96SPaul Moore #include <net/net_namespace.h> 57d621d35eSPaul Moore #include <net/netlabel.h> 58f5269710SEric Paris #include <linux/uaccess.h> 591da177e4SLinus Torvalds #include <asm/ioctls.h> 6060063497SArun Sharma #include <linux/atomic.h> 611da177e4SLinus Torvalds #include <linux/bitops.h> 621da177e4SLinus Torvalds #include <linux/interrupt.h> 631da177e4SLinus Torvalds #include <linux/netdevice.h> /* for network interface checks */ 6477954983SHong zhi guo #include <net/netlink.h> 651da177e4SLinus Torvalds #include <linux/tcp.h> 661da177e4SLinus Torvalds #include <linux/udp.h> 672ee92d46SJames Morris #include <linux/dccp.h> 681da177e4SLinus Torvalds #include <linux/quota.h> 691da177e4SLinus Torvalds #include <linux/un.h> /* for Unix socket types */ 701da177e4SLinus Torvalds #include <net/af_unix.h> /* for Unix socket types */ 711da177e4SLinus Torvalds #include <linux/parser.h> 721da177e4SLinus Torvalds #include <linux/nfs_mount.h> 731da177e4SLinus Torvalds #include <net/ipv6.h> 741da177e4SLinus Torvalds #include <linux/hugetlb.h> 751da177e4SLinus Torvalds #include <linux/personality.h> 761da177e4SLinus Torvalds #include <linux/audit.h> 776931dfc9SEric Paris #include <linux/string.h> 78877ce7c1SCatherine Zhang #include <linux/selinux.h> 7923970741SEric Paris #include <linux/mutex.h> 80f06febc9SFrank Mayhar #include <linux/posix-timers.h> 8100234592SKees Cook #include <linux/syslog.h> 823486740aSSerge E. Hallyn #include <linux/user_namespace.h> 8344fc7ea0SPaul Gortmaker #include <linux/export.h> 8440401530SAl Viro #include <linux/msg.h> 8540401530SAl Viro #include <linux/shm.h> 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds #include "avc.h" 881da177e4SLinus Torvalds #include "objsec.h" 891da177e4SLinus Torvalds #include "netif.h" 90224dfbd8SPaul Moore #include "netnode.h" 913e112172SPaul Moore #include "netport.h" 92d28d1e08STrent Jaeger #include "xfrm.h" 93c60475bfSPaul Moore #include "netlabel.h" 949d57a7f9SAhmed S. Darwish #include "audit.h" 957b98a585SJames Morris #include "avc_ss.h" 961da177e4SLinus Torvalds 97102aefddSAnand Avati #define SB_TYPE_FMT "%s%s%s" 98102aefddSAnand Avati #define SB_SUBTYPE(sb) (sb->s_subtype && sb->s_subtype[0]) 99102aefddSAnand Avati #define SB_TYPE_ARGS(sb) sb->s_type->name, SB_SUBTYPE(sb) ? "." : "", SB_SUBTYPE(sb) ? sb->s_subtype : "" 100102aefddSAnand Avati 10120510f2fSJames Morris extern struct security_operations *security_ops; 1021da177e4SLinus Torvalds 103d621d35eSPaul Moore /* SECMARK reference count */ 10456a4ca99SJames Morris static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0); 105d621d35eSPaul Moore 1061da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP 107828dfe1dSEric Paris int selinux_enforcing; 1081da177e4SLinus Torvalds 1091da177e4SLinus Torvalds static int __init enforcing_setup(char *str) 1101da177e4SLinus Torvalds { 111f5269710SEric Paris unsigned long enforcing; 112f5269710SEric Paris if (!strict_strtoul(str, 0, &enforcing)) 113f5269710SEric Paris selinux_enforcing = enforcing ? 1 : 0; 1141da177e4SLinus Torvalds return 1; 1151da177e4SLinus Torvalds } 1161da177e4SLinus Torvalds __setup("enforcing=", enforcing_setup); 1171da177e4SLinus Torvalds #endif 1181da177e4SLinus Torvalds 1191da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM 1201da177e4SLinus Torvalds int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE; 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds static int __init selinux_enabled_setup(char *str) 1231da177e4SLinus Torvalds { 124f5269710SEric Paris unsigned long enabled; 125f5269710SEric Paris if (!strict_strtoul(str, 0, &enabled)) 126f5269710SEric Paris selinux_enabled = enabled ? 1 : 0; 1271da177e4SLinus Torvalds return 1; 1281da177e4SLinus Torvalds } 1291da177e4SLinus Torvalds __setup("selinux=", selinux_enabled_setup); 13030d55280SStephen Smalley #else 13130d55280SStephen Smalley int selinux_enabled = 1; 1321da177e4SLinus Torvalds #endif 1331da177e4SLinus Torvalds 134e18b890bSChristoph Lameter static struct kmem_cache *sel_inode_cache; 1357cae7e26SJames Morris 136d621d35eSPaul Moore /** 137d621d35eSPaul Moore * selinux_secmark_enabled - Check to see if SECMARK is currently enabled 138d621d35eSPaul Moore * 139d621d35eSPaul Moore * Description: 140d621d35eSPaul Moore * This function checks the SECMARK reference counter to see if any SECMARK 141d621d35eSPaul Moore * targets are currently configured, if the reference counter is greater than 142d621d35eSPaul Moore * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is 1432be4d74fSChris PeBenito * enabled, false (0) if SECMARK is disabled. If the always_check_network 1442be4d74fSChris PeBenito * policy capability is enabled, SECMARK is always considered enabled. 145d621d35eSPaul Moore * 146d621d35eSPaul Moore */ 147d621d35eSPaul Moore static int selinux_secmark_enabled(void) 148d621d35eSPaul Moore { 1492be4d74fSChris PeBenito return (selinux_policycap_alwaysnetwork || atomic_read(&selinux_secmark_refcount)); 1502be4d74fSChris PeBenito } 1512be4d74fSChris PeBenito 1522be4d74fSChris PeBenito /** 1532be4d74fSChris PeBenito * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled 1542be4d74fSChris PeBenito * 1552be4d74fSChris PeBenito * Description: 1562be4d74fSChris PeBenito * This function checks if NetLabel or labeled IPSEC is enabled. Returns true 1572be4d74fSChris PeBenito * (1) if any are enabled or false (0) if neither are enabled. If the 1582be4d74fSChris PeBenito * always_check_network policy capability is enabled, peer labeling 1592be4d74fSChris PeBenito * is always considered enabled. 1602be4d74fSChris PeBenito * 1612be4d74fSChris PeBenito */ 1622be4d74fSChris PeBenito static int selinux_peerlbl_enabled(void) 1632be4d74fSChris PeBenito { 1642be4d74fSChris PeBenito return (selinux_policycap_alwaysnetwork || netlbl_enabled() || selinux_xfrm_enabled()); 165d621d35eSPaul Moore } 166d621d35eSPaul Moore 167d84f4f99SDavid Howells /* 168d84f4f99SDavid Howells * initialise the security for the init task 169d84f4f99SDavid Howells */ 170d84f4f99SDavid Howells static void cred_init_security(void) 1711da177e4SLinus Torvalds { 1723b11a1deSDavid Howells struct cred *cred = (struct cred *) current->real_cred; 1731da177e4SLinus Torvalds struct task_security_struct *tsec; 1741da177e4SLinus Torvalds 17589d155efSJames Morris tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL); 1761da177e4SLinus Torvalds if (!tsec) 177d84f4f99SDavid Howells panic("SELinux: Failed to initialize initial task.\n"); 1781da177e4SLinus Torvalds 179d84f4f99SDavid Howells tsec->osid = tsec->sid = SECINITSID_KERNEL; 180f1752eecSDavid Howells cred->security = tsec; 1811da177e4SLinus Torvalds } 1821da177e4SLinus Torvalds 183275bb41eSDavid Howells /* 18488e67f3bSDavid Howells * get the security ID of a set of credentials 18588e67f3bSDavid Howells */ 18688e67f3bSDavid Howells static inline u32 cred_sid(const struct cred *cred) 18788e67f3bSDavid Howells { 18888e67f3bSDavid Howells const struct task_security_struct *tsec; 18988e67f3bSDavid Howells 19088e67f3bSDavid Howells tsec = cred->security; 19188e67f3bSDavid Howells return tsec->sid; 19288e67f3bSDavid Howells } 19388e67f3bSDavid Howells 19488e67f3bSDavid Howells /* 1953b11a1deSDavid Howells * get the objective security ID of a task 196275bb41eSDavid Howells */ 197275bb41eSDavid Howells static inline u32 task_sid(const struct task_struct *task) 198275bb41eSDavid Howells { 199275bb41eSDavid Howells u32 sid; 200275bb41eSDavid Howells 201275bb41eSDavid Howells rcu_read_lock(); 20288e67f3bSDavid Howells sid = cred_sid(__task_cred(task)); 203275bb41eSDavid Howells rcu_read_unlock(); 204275bb41eSDavid Howells return sid; 205275bb41eSDavid Howells } 206275bb41eSDavid Howells 207275bb41eSDavid Howells /* 2083b11a1deSDavid Howells * get the subjective security ID of the current task 209275bb41eSDavid Howells */ 210275bb41eSDavid Howells static inline u32 current_sid(void) 211275bb41eSDavid Howells { 2125fb49870SPaul Moore const struct task_security_struct *tsec = current_security(); 213275bb41eSDavid Howells 214275bb41eSDavid Howells return tsec->sid; 215275bb41eSDavid Howells } 216275bb41eSDavid Howells 21788e67f3bSDavid Howells /* Allocate and free functions for each kind of security blob. */ 21888e67f3bSDavid Howells 2191da177e4SLinus Torvalds static int inode_alloc_security(struct inode *inode) 2201da177e4SLinus Torvalds { 2211da177e4SLinus Torvalds struct inode_security_struct *isec; 222275bb41eSDavid Howells u32 sid = current_sid(); 2231da177e4SLinus Torvalds 224a02fe132SJosef Bacik isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS); 2251da177e4SLinus Torvalds if (!isec) 2261da177e4SLinus Torvalds return -ENOMEM; 2271da177e4SLinus Torvalds 22823970741SEric Paris mutex_init(&isec->lock); 2291da177e4SLinus Torvalds INIT_LIST_HEAD(&isec->list); 2301da177e4SLinus Torvalds isec->inode = inode; 2311da177e4SLinus Torvalds isec->sid = SECINITSID_UNLABELED; 2321da177e4SLinus Torvalds isec->sclass = SECCLASS_FILE; 233275bb41eSDavid Howells isec->task_sid = sid; 2341da177e4SLinus Torvalds inode->i_security = isec; 2351da177e4SLinus Torvalds 2361da177e4SLinus Torvalds return 0; 2371da177e4SLinus Torvalds } 2381da177e4SLinus Torvalds 2391da177e4SLinus Torvalds static void inode_free_security(struct inode *inode) 2401da177e4SLinus Torvalds { 2411da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 2421da177e4SLinus Torvalds struct superblock_security_struct *sbsec = inode->i_sb->s_security; 2431da177e4SLinus Torvalds 2441da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 2451da177e4SLinus Torvalds if (!list_empty(&isec->list)) 2461da177e4SLinus Torvalds list_del_init(&isec->list); 2471da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 2481da177e4SLinus Torvalds 2491da177e4SLinus Torvalds inode->i_security = NULL; 2507cae7e26SJames Morris kmem_cache_free(sel_inode_cache, isec); 2511da177e4SLinus Torvalds } 2521da177e4SLinus Torvalds 2531da177e4SLinus Torvalds static int file_alloc_security(struct file *file) 2541da177e4SLinus Torvalds { 2551da177e4SLinus Torvalds struct file_security_struct *fsec; 256275bb41eSDavid Howells u32 sid = current_sid(); 2571da177e4SLinus Torvalds 25826d2a4beSStephen Smalley fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL); 2591da177e4SLinus Torvalds if (!fsec) 2601da177e4SLinus Torvalds return -ENOMEM; 2611da177e4SLinus Torvalds 262275bb41eSDavid Howells fsec->sid = sid; 263275bb41eSDavid Howells fsec->fown_sid = sid; 2641da177e4SLinus Torvalds file->f_security = fsec; 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds return 0; 2671da177e4SLinus Torvalds } 2681da177e4SLinus Torvalds 2691da177e4SLinus Torvalds static void file_free_security(struct file *file) 2701da177e4SLinus Torvalds { 2711da177e4SLinus Torvalds struct file_security_struct *fsec = file->f_security; 2721da177e4SLinus Torvalds file->f_security = NULL; 2731da177e4SLinus Torvalds kfree(fsec); 2741da177e4SLinus Torvalds } 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds static int superblock_alloc_security(struct super_block *sb) 2771da177e4SLinus Torvalds { 2781da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 2791da177e4SLinus Torvalds 28089d155efSJames Morris sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL); 2811da177e4SLinus Torvalds if (!sbsec) 2821da177e4SLinus Torvalds return -ENOMEM; 2831da177e4SLinus Torvalds 284bc7e982bSEric Paris mutex_init(&sbsec->lock); 2851da177e4SLinus Torvalds INIT_LIST_HEAD(&sbsec->isec_head); 2861da177e4SLinus Torvalds spin_lock_init(&sbsec->isec_lock); 2871da177e4SLinus Torvalds sbsec->sb = sb; 2881da177e4SLinus Torvalds sbsec->sid = SECINITSID_UNLABELED; 2891da177e4SLinus Torvalds sbsec->def_sid = SECINITSID_FILE; 290c312feb2SEric Paris sbsec->mntpoint_sid = SECINITSID_UNLABELED; 2911da177e4SLinus Torvalds sb->s_security = sbsec; 2921da177e4SLinus Torvalds 2931da177e4SLinus Torvalds return 0; 2941da177e4SLinus Torvalds } 2951da177e4SLinus Torvalds 2961da177e4SLinus Torvalds static void superblock_free_security(struct super_block *sb) 2971da177e4SLinus Torvalds { 2981da177e4SLinus Torvalds struct superblock_security_struct *sbsec = sb->s_security; 2991da177e4SLinus Torvalds sb->s_security = NULL; 3001da177e4SLinus Torvalds kfree(sbsec); 3011da177e4SLinus Torvalds } 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds /* The file system's label must be initialized prior to use. */ 3041da177e4SLinus Torvalds 305634a539eSStephen Hemminger static const char *labeling_behaviors[6] = { 3061da177e4SLinus Torvalds "uses xattr", 3071da177e4SLinus Torvalds "uses transition SIDs", 3081da177e4SLinus Torvalds "uses task SIDs", 3091da177e4SLinus Torvalds "uses genfs_contexts", 3101da177e4SLinus Torvalds "not configured for labeling", 3111da177e4SLinus Torvalds "uses mountpoint labeling", 3121da177e4SLinus Torvalds }; 3131da177e4SLinus Torvalds 3141da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry); 3151da177e4SLinus Torvalds 3161da177e4SLinus Torvalds static inline int inode_doinit(struct inode *inode) 3171da177e4SLinus Torvalds { 3181da177e4SLinus Torvalds return inode_doinit_with_dentry(inode, NULL); 3191da177e4SLinus Torvalds } 3201da177e4SLinus Torvalds 3211da177e4SLinus Torvalds enum { 32231e87930SEric Paris Opt_error = -1, 3231da177e4SLinus Torvalds Opt_context = 1, 3241da177e4SLinus Torvalds Opt_fscontext = 2, 325c9180a57SEric Paris Opt_defcontext = 3, 326c9180a57SEric Paris Opt_rootcontext = 4, 32711689d47SDavid P. Quigley Opt_labelsupport = 5, 328d355987fSEric Paris Opt_nextmntopt = 6, 3291da177e4SLinus Torvalds }; 3301da177e4SLinus Torvalds 331d355987fSEric Paris #define NUM_SEL_MNT_OPTS (Opt_nextmntopt - 1) 332d355987fSEric Paris 333a447c093SSteven Whitehouse static const match_table_t tokens = { 334832cbd9aSEric Paris {Opt_context, CONTEXT_STR "%s"}, 335832cbd9aSEric Paris {Opt_fscontext, FSCONTEXT_STR "%s"}, 336832cbd9aSEric Paris {Opt_defcontext, DEFCONTEXT_STR "%s"}, 337832cbd9aSEric Paris {Opt_rootcontext, ROOTCONTEXT_STR "%s"}, 33811689d47SDavid P. Quigley {Opt_labelsupport, LABELSUPP_STR}, 33931e87930SEric Paris {Opt_error, NULL}, 3401da177e4SLinus Torvalds }; 3411da177e4SLinus Torvalds 3421da177e4SLinus Torvalds #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n" 3431da177e4SLinus Torvalds 344c312feb2SEric Paris static int may_context_mount_sb_relabel(u32 sid, 345c312feb2SEric Paris struct superblock_security_struct *sbsec, 346275bb41eSDavid Howells const struct cred *cred) 347c312feb2SEric Paris { 348275bb41eSDavid Howells const struct task_security_struct *tsec = cred->security; 349c312feb2SEric Paris int rc; 350c312feb2SEric Paris 351c312feb2SEric Paris rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 352c312feb2SEric Paris FILESYSTEM__RELABELFROM, NULL); 353c312feb2SEric Paris if (rc) 354c312feb2SEric Paris return rc; 355c312feb2SEric Paris 356c312feb2SEric Paris rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM, 357c312feb2SEric Paris FILESYSTEM__RELABELTO, NULL); 358c312feb2SEric Paris return rc; 359c312feb2SEric Paris } 360c312feb2SEric Paris 3610808925eSEric Paris static int may_context_mount_inode_relabel(u32 sid, 3620808925eSEric Paris struct superblock_security_struct *sbsec, 363275bb41eSDavid Howells const struct cred *cred) 3640808925eSEric Paris { 365275bb41eSDavid Howells const struct task_security_struct *tsec = cred->security; 3660808925eSEric Paris int rc; 3670808925eSEric Paris rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 3680808925eSEric Paris FILESYSTEM__RELABELFROM, NULL); 3690808925eSEric Paris if (rc) 3700808925eSEric Paris return rc; 3710808925eSEric Paris 3720808925eSEric Paris rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, 3730808925eSEric Paris FILESYSTEM__ASSOCIATE, NULL); 3740808925eSEric Paris return rc; 3750808925eSEric Paris } 3760808925eSEric Paris 377b43e725dSEric Paris static int selinux_is_sblabel_mnt(struct super_block *sb) 378b43e725dSEric Paris { 379b43e725dSEric Paris struct superblock_security_struct *sbsec = sb->s_security; 380b43e725dSEric Paris 381b43e725dSEric Paris if (sbsec->behavior == SECURITY_FS_USE_XATTR || 382b43e725dSEric Paris sbsec->behavior == SECURITY_FS_USE_TRANS || 383b43e725dSEric Paris sbsec->behavior == SECURITY_FS_USE_TASK) 384b43e725dSEric Paris return 1; 385b43e725dSEric Paris 386b43e725dSEric Paris /* Special handling for sysfs. Is genfs but also has setxattr handler*/ 387b43e725dSEric Paris if (strncmp(sb->s_type->name, "sysfs", sizeof("sysfs")) == 0) 388b43e725dSEric Paris return 1; 389b43e725dSEric Paris 390b43e725dSEric Paris /* 391b43e725dSEric Paris * Special handling for rootfs. Is genfs but supports 392b43e725dSEric Paris * setting SELinux context on in-core inodes. 393b43e725dSEric Paris */ 394b43e725dSEric Paris if (strncmp(sb->s_type->name, "rootfs", sizeof("rootfs")) == 0) 395b43e725dSEric Paris return 1; 396b43e725dSEric Paris 397b43e725dSEric Paris return 0; 398b43e725dSEric Paris } 399b43e725dSEric Paris 400c9180a57SEric Paris static int sb_finish_set_opts(struct super_block *sb) 4011da177e4SLinus Torvalds { 4021da177e4SLinus Torvalds struct superblock_security_struct *sbsec = sb->s_security; 4031da177e4SLinus Torvalds struct dentry *root = sb->s_root; 404c9180a57SEric Paris struct inode *root_inode = root->d_inode; 4051da177e4SLinus Torvalds int rc = 0; 4061da177e4SLinus Torvalds 4071da177e4SLinus Torvalds if (sbsec->behavior == SECURITY_FS_USE_XATTR) { 4081da177e4SLinus Torvalds /* Make sure that the xattr handler exists and that no 4091da177e4SLinus Torvalds error other than -ENODATA is returned by getxattr on 4101da177e4SLinus Torvalds the root directory. -ENODATA is ok, as this may be 4111da177e4SLinus Torvalds the first boot of the SELinux kernel before we have 4121da177e4SLinus Torvalds assigned xattr values to the filesystem. */ 413c9180a57SEric Paris if (!root_inode->i_op->getxattr) { 414102aefddSAnand Avati printk(KERN_WARNING "SELinux: (dev %s, type "SB_TYPE_FMT") has no " 415102aefddSAnand Avati "xattr support\n", sb->s_id, SB_TYPE_ARGS(sb)); 4161da177e4SLinus Torvalds rc = -EOPNOTSUPP; 4171da177e4SLinus Torvalds goto out; 4181da177e4SLinus Torvalds } 419c9180a57SEric Paris rc = root_inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0); 4201da177e4SLinus Torvalds if (rc < 0 && rc != -ENODATA) { 4211da177e4SLinus Torvalds if (rc == -EOPNOTSUPP) 4221da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: (dev %s, type " 423102aefddSAnand Avati SB_TYPE_FMT") has no security xattr handler\n", 424102aefddSAnand Avati sb->s_id, SB_TYPE_ARGS(sb)); 4251da177e4SLinus Torvalds else 4261da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: (dev %s, type " 427102aefddSAnand Avati SB_TYPE_FMT") getxattr errno %d\n", sb->s_id, 428102aefddSAnand Avati SB_TYPE_ARGS(sb), -rc); 4291da177e4SLinus Torvalds goto out; 4301da177e4SLinus Torvalds } 4311da177e4SLinus Torvalds } 4321da177e4SLinus Torvalds 433c9180a57SEric Paris if (sbsec->behavior > ARRAY_SIZE(labeling_behaviors)) 434102aefddSAnand Avati printk(KERN_ERR "SELinux: initialized (dev %s, type "SB_TYPE_FMT"), unknown behavior\n", 435102aefddSAnand Avati sb->s_id, SB_TYPE_ARGS(sb)); 436c9180a57SEric Paris else 437102aefddSAnand Avati printk(KERN_DEBUG "SELinux: initialized (dev %s, type "SB_TYPE_FMT"), %s\n", 438102aefddSAnand Avati sb->s_id, SB_TYPE_ARGS(sb), 4391da177e4SLinus Torvalds labeling_behaviors[sbsec->behavior-1]); 4401da177e4SLinus Torvalds 441eadcabc6SEric Paris sbsec->flags |= SE_SBINITIALIZED; 442b43e725dSEric Paris if (selinux_is_sblabel_mnt(sb)) 44312f348b9SEric Paris sbsec->flags |= SBLABEL_MNT; 4445c73fcebSStephen Smalley 4451da177e4SLinus Torvalds /* Initialize the root inode. */ 446c9180a57SEric Paris rc = inode_doinit_with_dentry(root_inode, root); 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds /* Initialize any other inodes associated with the superblock, e.g. 4491da177e4SLinus Torvalds inodes created prior to initial policy load or inodes created 4501da177e4SLinus Torvalds during get_sb by a pseudo filesystem that directly 4511da177e4SLinus Torvalds populates itself. */ 4521da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 4531da177e4SLinus Torvalds next_inode: 4541da177e4SLinus Torvalds if (!list_empty(&sbsec->isec_head)) { 4551da177e4SLinus Torvalds struct inode_security_struct *isec = 4561da177e4SLinus Torvalds list_entry(sbsec->isec_head.next, 4571da177e4SLinus Torvalds struct inode_security_struct, list); 4581da177e4SLinus Torvalds struct inode *inode = isec->inode; 4591da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 4601da177e4SLinus Torvalds inode = igrab(inode); 4611da177e4SLinus Torvalds if (inode) { 4621da177e4SLinus Torvalds if (!IS_PRIVATE(inode)) 4631da177e4SLinus Torvalds inode_doinit(inode); 4641da177e4SLinus Torvalds iput(inode); 4651da177e4SLinus Torvalds } 4661da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 4671da177e4SLinus Torvalds list_del_init(&isec->list); 4681da177e4SLinus Torvalds goto next_inode; 4691da177e4SLinus Torvalds } 4701da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 4711da177e4SLinus Torvalds out: 472c9180a57SEric Paris return rc; 473c9180a57SEric Paris } 474c9180a57SEric Paris 475c9180a57SEric Paris /* 476c9180a57SEric Paris * This function should allow an FS to ask what it's mount security 477c9180a57SEric Paris * options were so it can use those later for submounts, displaying 478c9180a57SEric Paris * mount options, or whatever. 479c9180a57SEric Paris */ 480c9180a57SEric Paris static int selinux_get_mnt_opts(const struct super_block *sb, 481e0007529SEric Paris struct security_mnt_opts *opts) 482c9180a57SEric Paris { 483c9180a57SEric Paris int rc = 0, i; 484c9180a57SEric Paris struct superblock_security_struct *sbsec = sb->s_security; 485c9180a57SEric Paris char *context = NULL; 486c9180a57SEric Paris u32 len; 487c9180a57SEric Paris char tmp; 488c9180a57SEric Paris 489e0007529SEric Paris security_init_mnt_opts(opts); 490c9180a57SEric Paris 4910d90a7ecSDavid P. Quigley if (!(sbsec->flags & SE_SBINITIALIZED)) 492c9180a57SEric Paris return -EINVAL; 493c9180a57SEric Paris 494c9180a57SEric Paris if (!ss_initialized) 495c9180a57SEric Paris return -EINVAL; 496c9180a57SEric Paris 497af8e50ccSEric Paris /* make sure we always check enough bits to cover the mask */ 498af8e50ccSEric Paris BUILD_BUG_ON(SE_MNTMASK >= (1 << NUM_SEL_MNT_OPTS)); 499af8e50ccSEric Paris 5000d90a7ecSDavid P. Quigley tmp = sbsec->flags & SE_MNTMASK; 501c9180a57SEric Paris /* count the number of mount options for this sb */ 502af8e50ccSEric Paris for (i = 0; i < NUM_SEL_MNT_OPTS; i++) { 503c9180a57SEric Paris if (tmp & 0x01) 504e0007529SEric Paris opts->num_mnt_opts++; 505c9180a57SEric Paris tmp >>= 1; 506c9180a57SEric Paris } 507*0b4bdb35SEric Paris /* Check if the Label support flag is set */ 508*0b4bdb35SEric Paris if (sbsec->flags & SBLABEL_MNT) 509*0b4bdb35SEric Paris opts->num_mnt_opts++; 510c9180a57SEric Paris 511e0007529SEric Paris opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC); 512e0007529SEric Paris if (!opts->mnt_opts) { 513c9180a57SEric Paris rc = -ENOMEM; 514c9180a57SEric Paris goto out_free; 515c9180a57SEric Paris } 516c9180a57SEric Paris 517e0007529SEric Paris opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC); 518e0007529SEric Paris if (!opts->mnt_opts_flags) { 519c9180a57SEric Paris rc = -ENOMEM; 520c9180a57SEric Paris goto out_free; 521c9180a57SEric Paris } 522c9180a57SEric Paris 523c9180a57SEric Paris i = 0; 524c9180a57SEric Paris if (sbsec->flags & FSCONTEXT_MNT) { 525c9180a57SEric Paris rc = security_sid_to_context(sbsec->sid, &context, &len); 526c9180a57SEric Paris if (rc) 527c9180a57SEric Paris goto out_free; 528e0007529SEric Paris opts->mnt_opts[i] = context; 529e0007529SEric Paris opts->mnt_opts_flags[i++] = FSCONTEXT_MNT; 530c9180a57SEric Paris } 531c9180a57SEric Paris if (sbsec->flags & CONTEXT_MNT) { 532c9180a57SEric Paris rc = security_sid_to_context(sbsec->mntpoint_sid, &context, &len); 533c9180a57SEric Paris if (rc) 534c9180a57SEric Paris goto out_free; 535e0007529SEric Paris opts->mnt_opts[i] = context; 536e0007529SEric Paris opts->mnt_opts_flags[i++] = CONTEXT_MNT; 537c9180a57SEric Paris } 538c9180a57SEric Paris if (sbsec->flags & DEFCONTEXT_MNT) { 539c9180a57SEric Paris rc = security_sid_to_context(sbsec->def_sid, &context, &len); 540c9180a57SEric Paris if (rc) 541c9180a57SEric Paris goto out_free; 542e0007529SEric Paris opts->mnt_opts[i] = context; 543e0007529SEric Paris opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT; 544c9180a57SEric Paris } 545c9180a57SEric Paris if (sbsec->flags & ROOTCONTEXT_MNT) { 546c9180a57SEric Paris struct inode *root = sbsec->sb->s_root->d_inode; 547c9180a57SEric Paris struct inode_security_struct *isec = root->i_security; 548c9180a57SEric Paris 549c9180a57SEric Paris rc = security_sid_to_context(isec->sid, &context, &len); 550c9180a57SEric Paris if (rc) 551c9180a57SEric Paris goto out_free; 552e0007529SEric Paris opts->mnt_opts[i] = context; 553e0007529SEric Paris opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT; 554c9180a57SEric Paris } 55512f348b9SEric Paris if (sbsec->flags & SBLABEL_MNT) { 55611689d47SDavid P. Quigley opts->mnt_opts[i] = NULL; 55712f348b9SEric Paris opts->mnt_opts_flags[i++] = SBLABEL_MNT; 55811689d47SDavid P. Quigley } 559c9180a57SEric Paris 560e0007529SEric Paris BUG_ON(i != opts->num_mnt_opts); 561c9180a57SEric Paris 562c9180a57SEric Paris return 0; 563c9180a57SEric Paris 564c9180a57SEric Paris out_free: 565e0007529SEric Paris security_free_mnt_opts(opts); 566c9180a57SEric Paris return rc; 567c9180a57SEric Paris } 568c9180a57SEric Paris 569c9180a57SEric Paris static int bad_option(struct superblock_security_struct *sbsec, char flag, 570c9180a57SEric Paris u32 old_sid, u32 new_sid) 571c9180a57SEric Paris { 5720d90a7ecSDavid P. Quigley char mnt_flags = sbsec->flags & SE_MNTMASK; 5730d90a7ecSDavid P. Quigley 574c9180a57SEric Paris /* check if the old mount command had the same options */ 5750d90a7ecSDavid P. Quigley if (sbsec->flags & SE_SBINITIALIZED) 576c9180a57SEric Paris if (!(sbsec->flags & flag) || 577c9180a57SEric Paris (old_sid != new_sid)) 578c9180a57SEric Paris return 1; 579c9180a57SEric Paris 580c9180a57SEric Paris /* check if we were passed the same options twice, 581c9180a57SEric Paris * aka someone passed context=a,context=b 582c9180a57SEric Paris */ 5830d90a7ecSDavid P. Quigley if (!(sbsec->flags & SE_SBINITIALIZED)) 5840d90a7ecSDavid P. Quigley if (mnt_flags & flag) 585c9180a57SEric Paris return 1; 586c9180a57SEric Paris return 0; 587c9180a57SEric Paris } 588e0007529SEric Paris 589c9180a57SEric Paris /* 590c9180a57SEric Paris * Allow filesystems with binary mount data to explicitly set mount point 591c9180a57SEric Paris * labeling information. 592c9180a57SEric Paris */ 593e0007529SEric Paris static int selinux_set_mnt_opts(struct super_block *sb, 594e0007529SEric Paris struct security_mnt_opts *opts) 595c9180a57SEric Paris { 596275bb41eSDavid Howells const struct cred *cred = current_cred(); 597c9180a57SEric Paris int rc = 0, i; 598c9180a57SEric Paris struct superblock_security_struct *sbsec = sb->s_security; 599089be43eSJames Morris struct inode *inode = sbsec->sb->s_root->d_inode; 600089be43eSJames Morris struct inode_security_struct *root_isec = inode->i_security; 601c9180a57SEric Paris u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0; 602c9180a57SEric Paris u32 defcontext_sid = 0; 603e0007529SEric Paris char **mount_options = opts->mnt_opts; 604e0007529SEric Paris int *flags = opts->mnt_opts_flags; 605e0007529SEric Paris int num_opts = opts->num_mnt_opts; 606c9180a57SEric Paris 607c9180a57SEric Paris mutex_lock(&sbsec->lock); 608c9180a57SEric Paris 609c9180a57SEric Paris if (!ss_initialized) { 610c9180a57SEric Paris if (!num_opts) { 611c9180a57SEric Paris /* Defer initialization until selinux_complete_init, 612c9180a57SEric Paris after the initial policy is loaded and the security 613c9180a57SEric Paris server is ready to handle calls. */ 614c9180a57SEric Paris goto out; 615c9180a57SEric Paris } 616c9180a57SEric Paris rc = -EINVAL; 617744ba35eSEric Paris printk(KERN_WARNING "SELinux: Unable to set superblock options " 618744ba35eSEric Paris "before the security server is initialized\n"); 619c9180a57SEric Paris goto out; 620c9180a57SEric Paris } 621c9180a57SEric Paris 622c9180a57SEric Paris /* 623e0007529SEric Paris * Binary mount data FS will come through this function twice. Once 624e0007529SEric Paris * from an explicit call and once from the generic calls from the vfs. 625e0007529SEric Paris * Since the generic VFS calls will not contain any security mount data 626e0007529SEric Paris * we need to skip the double mount verification. 627e0007529SEric Paris * 628e0007529SEric Paris * This does open a hole in which we will not notice if the first 629e0007529SEric Paris * mount using this sb set explict options and a second mount using 630e0007529SEric Paris * this sb does not set any security options. (The first options 631e0007529SEric Paris * will be used for both mounts) 632e0007529SEric Paris */ 6330d90a7ecSDavid P. Quigley if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA) 634e0007529SEric Paris && (num_opts == 0)) 635e0007529SEric Paris goto out; 636e0007529SEric Paris 637e0007529SEric Paris /* 638c9180a57SEric Paris * parse the mount options, check if they are valid sids. 639c9180a57SEric Paris * also check if someone is trying to mount the same sb more 640c9180a57SEric Paris * than once with different security options. 641c9180a57SEric Paris */ 642c9180a57SEric Paris for (i = 0; i < num_opts; i++) { 643c9180a57SEric Paris u32 sid; 64411689d47SDavid P. Quigley 64512f348b9SEric Paris if (flags[i] == SBLABEL_MNT) 64611689d47SDavid P. Quigley continue; 647c9180a57SEric Paris rc = security_context_to_sid(mount_options[i], 648c9180a57SEric Paris strlen(mount_options[i]), &sid); 649c9180a57SEric Paris if (rc) { 650c9180a57SEric Paris printk(KERN_WARNING "SELinux: security_context_to_sid" 651102aefddSAnand Avati "(%s) failed for (dev %s, type "SB_TYPE_FMT") errno=%d\n", 652102aefddSAnand Avati mount_options[i], sb->s_id, SB_TYPE_ARGS(sb), rc); 653c9180a57SEric Paris goto out; 654c9180a57SEric Paris } 655c9180a57SEric Paris switch (flags[i]) { 656c9180a57SEric Paris case FSCONTEXT_MNT: 657c9180a57SEric Paris fscontext_sid = sid; 658c9180a57SEric Paris 659c9180a57SEric Paris if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, 660c9180a57SEric Paris fscontext_sid)) 661c9180a57SEric Paris goto out_double_mount; 662c9180a57SEric Paris 663c9180a57SEric Paris sbsec->flags |= FSCONTEXT_MNT; 664c9180a57SEric Paris break; 665c9180a57SEric Paris case CONTEXT_MNT: 666c9180a57SEric Paris context_sid = sid; 667c9180a57SEric Paris 668c9180a57SEric Paris if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, 669c9180a57SEric Paris context_sid)) 670c9180a57SEric Paris goto out_double_mount; 671c9180a57SEric Paris 672c9180a57SEric Paris sbsec->flags |= CONTEXT_MNT; 673c9180a57SEric Paris break; 674c9180a57SEric Paris case ROOTCONTEXT_MNT: 675c9180a57SEric Paris rootcontext_sid = sid; 676c9180a57SEric Paris 677c9180a57SEric Paris if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, 678c9180a57SEric Paris rootcontext_sid)) 679c9180a57SEric Paris goto out_double_mount; 680c9180a57SEric Paris 681c9180a57SEric Paris sbsec->flags |= ROOTCONTEXT_MNT; 682c9180a57SEric Paris 683c9180a57SEric Paris break; 684c9180a57SEric Paris case DEFCONTEXT_MNT: 685c9180a57SEric Paris defcontext_sid = sid; 686c9180a57SEric Paris 687c9180a57SEric Paris if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, 688c9180a57SEric Paris defcontext_sid)) 689c9180a57SEric Paris goto out_double_mount; 690c9180a57SEric Paris 691c9180a57SEric Paris sbsec->flags |= DEFCONTEXT_MNT; 692c9180a57SEric Paris 693c9180a57SEric Paris break; 694c9180a57SEric Paris default: 695c9180a57SEric Paris rc = -EINVAL; 696c9180a57SEric Paris goto out; 697c9180a57SEric Paris } 698c9180a57SEric Paris } 699c9180a57SEric Paris 7000d90a7ecSDavid P. Quigley if (sbsec->flags & SE_SBINITIALIZED) { 701c9180a57SEric Paris /* previously mounted with options, but not on this attempt? */ 7020d90a7ecSDavid P. Quigley if ((sbsec->flags & SE_MNTMASK) && !num_opts) 703c9180a57SEric Paris goto out_double_mount; 704c9180a57SEric Paris rc = 0; 705c9180a57SEric Paris goto out; 706c9180a57SEric Paris } 707c9180a57SEric Paris 708089be43eSJames Morris if (strcmp(sb->s_type->name, "proc") == 0) 7090d90a7ecSDavid P. Quigley sbsec->flags |= SE_SBPROC; 710c9180a57SEric Paris 711c9180a57SEric Paris /* Determine the labeling behavior to use for this filesystem type. */ 712a64c54cfSEric Paris rc = security_fs_use(sb); 713c9180a57SEric Paris if (rc) { 714c9180a57SEric Paris printk(KERN_WARNING "%s: security_fs_use(%s) returned %d\n", 715089be43eSJames Morris __func__, sb->s_type->name, rc); 716c9180a57SEric Paris goto out; 717c9180a57SEric Paris } 718c9180a57SEric Paris 719c9180a57SEric Paris /* sets the context of the superblock for the fs being mounted. */ 720c9180a57SEric Paris if (fscontext_sid) { 721275bb41eSDavid Howells rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred); 722c9180a57SEric Paris if (rc) 723c9180a57SEric Paris goto out; 724c9180a57SEric Paris 725c9180a57SEric Paris sbsec->sid = fscontext_sid; 726c9180a57SEric Paris } 727c9180a57SEric Paris 728c9180a57SEric Paris /* 729c9180a57SEric Paris * Switch to using mount point labeling behavior. 730c9180a57SEric Paris * sets the label used on all file below the mountpoint, and will set 731c9180a57SEric Paris * the superblock context if not already set. 732c9180a57SEric Paris */ 733c9180a57SEric Paris if (context_sid) { 734c9180a57SEric Paris if (!fscontext_sid) { 735275bb41eSDavid Howells rc = may_context_mount_sb_relabel(context_sid, sbsec, 736275bb41eSDavid Howells cred); 737c9180a57SEric Paris if (rc) 738c9180a57SEric Paris goto out; 739c9180a57SEric Paris sbsec->sid = context_sid; 740c9180a57SEric Paris } else { 741275bb41eSDavid Howells rc = may_context_mount_inode_relabel(context_sid, sbsec, 742275bb41eSDavid Howells cred); 743c9180a57SEric Paris if (rc) 744c9180a57SEric Paris goto out; 745c9180a57SEric Paris } 746c9180a57SEric Paris if (!rootcontext_sid) 747c9180a57SEric Paris rootcontext_sid = context_sid; 748c9180a57SEric Paris 749c9180a57SEric Paris sbsec->mntpoint_sid = context_sid; 750c9180a57SEric Paris sbsec->behavior = SECURITY_FS_USE_MNTPOINT; 751c9180a57SEric Paris } 752c9180a57SEric Paris 753c9180a57SEric Paris if (rootcontext_sid) { 754275bb41eSDavid Howells rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec, 755275bb41eSDavid Howells cred); 756c9180a57SEric Paris if (rc) 757c9180a57SEric Paris goto out; 758c9180a57SEric Paris 759c9180a57SEric Paris root_isec->sid = rootcontext_sid; 760c9180a57SEric Paris root_isec->initialized = 1; 761c9180a57SEric Paris } 762c9180a57SEric Paris 763c9180a57SEric Paris if (defcontext_sid) { 764c9180a57SEric Paris if (sbsec->behavior != SECURITY_FS_USE_XATTR) { 765c9180a57SEric Paris rc = -EINVAL; 766c9180a57SEric Paris printk(KERN_WARNING "SELinux: defcontext option is " 767c9180a57SEric Paris "invalid for this filesystem type\n"); 768c9180a57SEric Paris goto out; 769c9180a57SEric Paris } 770c9180a57SEric Paris 771c9180a57SEric Paris if (defcontext_sid != sbsec->def_sid) { 772c9180a57SEric Paris rc = may_context_mount_inode_relabel(defcontext_sid, 773275bb41eSDavid Howells sbsec, cred); 774c9180a57SEric Paris if (rc) 775c9180a57SEric Paris goto out; 776c9180a57SEric Paris } 777c9180a57SEric Paris 778c9180a57SEric Paris sbsec->def_sid = defcontext_sid; 779c9180a57SEric Paris } 780c9180a57SEric Paris 781c9180a57SEric Paris rc = sb_finish_set_opts(sb); 782c9180a57SEric Paris out: 783bc7e982bSEric Paris mutex_unlock(&sbsec->lock); 7841da177e4SLinus Torvalds return rc; 785c9180a57SEric Paris out_double_mount: 786c9180a57SEric Paris rc = -EINVAL; 787c9180a57SEric Paris printk(KERN_WARNING "SELinux: mount invalid. Same superblock, different " 788102aefddSAnand Avati "security settings for (dev %s, type "SB_TYPE_FMT")\n", sb->s_id, 789102aefddSAnand Avati SB_TYPE_ARGS(sb)); 790c9180a57SEric Paris goto out; 791c9180a57SEric Paris } 792c9180a57SEric Paris 793094f7b69SJeff Layton static int selinux_cmp_sb_context(const struct super_block *oldsb, 794094f7b69SJeff Layton const struct super_block *newsb) 795094f7b69SJeff Layton { 796094f7b69SJeff Layton struct superblock_security_struct *old = oldsb->s_security; 797094f7b69SJeff Layton struct superblock_security_struct *new = newsb->s_security; 798094f7b69SJeff Layton char oldflags = old->flags & SE_MNTMASK; 799094f7b69SJeff Layton char newflags = new->flags & SE_MNTMASK; 800094f7b69SJeff Layton 801094f7b69SJeff Layton if (oldflags != newflags) 802094f7b69SJeff Layton goto mismatch; 803094f7b69SJeff Layton if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid) 804094f7b69SJeff Layton goto mismatch; 805094f7b69SJeff Layton if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid) 806094f7b69SJeff Layton goto mismatch; 807094f7b69SJeff Layton if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid) 808094f7b69SJeff Layton goto mismatch; 809094f7b69SJeff Layton if (oldflags & ROOTCONTEXT_MNT) { 810094f7b69SJeff Layton struct inode_security_struct *oldroot = oldsb->s_root->d_inode->i_security; 811094f7b69SJeff Layton struct inode_security_struct *newroot = newsb->s_root->d_inode->i_security; 812094f7b69SJeff Layton if (oldroot->sid != newroot->sid) 813094f7b69SJeff Layton goto mismatch; 814094f7b69SJeff Layton } 815094f7b69SJeff Layton return 0; 816094f7b69SJeff Layton mismatch: 817094f7b69SJeff Layton printk(KERN_WARNING "SELinux: mount invalid. Same superblock, " 818094f7b69SJeff Layton "different security settings for (dev %s, " 819094f7b69SJeff Layton "type %s)\n", newsb->s_id, newsb->s_type->name); 820094f7b69SJeff Layton return -EBUSY; 821094f7b69SJeff Layton } 822094f7b69SJeff Layton 823094f7b69SJeff Layton static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb, 824c9180a57SEric Paris struct super_block *newsb) 825c9180a57SEric Paris { 826c9180a57SEric Paris const struct superblock_security_struct *oldsbsec = oldsb->s_security; 827c9180a57SEric Paris struct superblock_security_struct *newsbsec = newsb->s_security; 828c9180a57SEric Paris 829c9180a57SEric Paris int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT); 830c9180a57SEric Paris int set_context = (oldsbsec->flags & CONTEXT_MNT); 831c9180a57SEric Paris int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT); 832c9180a57SEric Paris 8330f5e6420SEric Paris /* 8340f5e6420SEric Paris * if the parent was able to be mounted it clearly had no special lsm 835e8c26255SAl Viro * mount options. thus we can safely deal with this superblock later 8360f5e6420SEric Paris */ 837e8c26255SAl Viro if (!ss_initialized) 838094f7b69SJeff Layton return 0; 839c9180a57SEric Paris 840c9180a57SEric Paris /* how can we clone if the old one wasn't set up?? */ 8410d90a7ecSDavid P. Quigley BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED)); 842c9180a57SEric Paris 843094f7b69SJeff Layton /* if fs is reusing a sb, make sure that the contexts match */ 8440d90a7ecSDavid P. Quigley if (newsbsec->flags & SE_SBINITIALIZED) 845094f7b69SJeff Layton return selinux_cmp_sb_context(oldsb, newsb); 8465a552617SEric Paris 847c9180a57SEric Paris mutex_lock(&newsbsec->lock); 848c9180a57SEric Paris 849c9180a57SEric Paris newsbsec->flags = oldsbsec->flags; 850c9180a57SEric Paris 851c9180a57SEric Paris newsbsec->sid = oldsbsec->sid; 852c9180a57SEric Paris newsbsec->def_sid = oldsbsec->def_sid; 853c9180a57SEric Paris newsbsec->behavior = oldsbsec->behavior; 854c9180a57SEric Paris 855c9180a57SEric Paris if (set_context) { 856c9180a57SEric Paris u32 sid = oldsbsec->mntpoint_sid; 857c9180a57SEric Paris 858c9180a57SEric Paris if (!set_fscontext) 859c9180a57SEric Paris newsbsec->sid = sid; 860c9180a57SEric Paris if (!set_rootcontext) { 861c9180a57SEric Paris struct inode *newinode = newsb->s_root->d_inode; 862c9180a57SEric Paris struct inode_security_struct *newisec = newinode->i_security; 863c9180a57SEric Paris newisec->sid = sid; 864c9180a57SEric Paris } 865c9180a57SEric Paris newsbsec->mntpoint_sid = sid; 866c9180a57SEric Paris } 867c9180a57SEric Paris if (set_rootcontext) { 868c9180a57SEric Paris const struct inode *oldinode = oldsb->s_root->d_inode; 869c9180a57SEric Paris const struct inode_security_struct *oldisec = oldinode->i_security; 870c9180a57SEric Paris struct inode *newinode = newsb->s_root->d_inode; 871c9180a57SEric Paris struct inode_security_struct *newisec = newinode->i_security; 872c9180a57SEric Paris 873c9180a57SEric Paris newisec->sid = oldisec->sid; 874c9180a57SEric Paris } 875c9180a57SEric Paris 876c9180a57SEric Paris sb_finish_set_opts(newsb); 877c9180a57SEric Paris mutex_unlock(&newsbsec->lock); 878094f7b69SJeff Layton return 0; 879c9180a57SEric Paris } 880c9180a57SEric Paris 8812e1479d9SAdrian Bunk static int selinux_parse_opts_str(char *options, 8822e1479d9SAdrian Bunk struct security_mnt_opts *opts) 883c9180a57SEric Paris { 884e0007529SEric Paris char *p; 885c9180a57SEric Paris char *context = NULL, *defcontext = NULL; 886c9180a57SEric Paris char *fscontext = NULL, *rootcontext = NULL; 887e0007529SEric Paris int rc, num_mnt_opts = 0; 888c9180a57SEric Paris 889e0007529SEric Paris opts->num_mnt_opts = 0; 890c9180a57SEric Paris 891c9180a57SEric Paris /* Standard string-based options. */ 892c9180a57SEric Paris while ((p = strsep(&options, "|")) != NULL) { 893c9180a57SEric Paris int token; 894c9180a57SEric Paris substring_t args[MAX_OPT_ARGS]; 895c9180a57SEric Paris 896c9180a57SEric Paris if (!*p) 897c9180a57SEric Paris continue; 898c9180a57SEric Paris 899c9180a57SEric Paris token = match_token(p, tokens, args); 900c9180a57SEric Paris 901c9180a57SEric Paris switch (token) { 902c9180a57SEric Paris case Opt_context: 903c9180a57SEric Paris if (context || defcontext) { 904c9180a57SEric Paris rc = -EINVAL; 905c9180a57SEric Paris printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 906c9180a57SEric Paris goto out_err; 907c9180a57SEric Paris } 908c9180a57SEric Paris context = match_strdup(&args[0]); 909c9180a57SEric Paris if (!context) { 910c9180a57SEric Paris rc = -ENOMEM; 911c9180a57SEric Paris goto out_err; 912c9180a57SEric Paris } 913c9180a57SEric Paris break; 914c9180a57SEric Paris 915c9180a57SEric Paris case Opt_fscontext: 916c9180a57SEric Paris if (fscontext) { 917c9180a57SEric Paris rc = -EINVAL; 918c9180a57SEric Paris printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 919c9180a57SEric Paris goto out_err; 920c9180a57SEric Paris } 921c9180a57SEric Paris fscontext = match_strdup(&args[0]); 922c9180a57SEric Paris if (!fscontext) { 923c9180a57SEric Paris rc = -ENOMEM; 924c9180a57SEric Paris goto out_err; 925c9180a57SEric Paris } 926c9180a57SEric Paris break; 927c9180a57SEric Paris 928c9180a57SEric Paris case Opt_rootcontext: 929c9180a57SEric Paris if (rootcontext) { 930c9180a57SEric Paris rc = -EINVAL; 931c9180a57SEric Paris printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 932c9180a57SEric Paris goto out_err; 933c9180a57SEric Paris } 934c9180a57SEric Paris rootcontext = match_strdup(&args[0]); 935c9180a57SEric Paris if (!rootcontext) { 936c9180a57SEric Paris rc = -ENOMEM; 937c9180a57SEric Paris goto out_err; 938c9180a57SEric Paris } 939c9180a57SEric Paris break; 940c9180a57SEric Paris 941c9180a57SEric Paris case Opt_defcontext: 942c9180a57SEric Paris if (context || defcontext) { 943c9180a57SEric Paris rc = -EINVAL; 944c9180a57SEric Paris printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 945c9180a57SEric Paris goto out_err; 946c9180a57SEric Paris } 947c9180a57SEric Paris defcontext = match_strdup(&args[0]); 948c9180a57SEric Paris if (!defcontext) { 949c9180a57SEric Paris rc = -ENOMEM; 950c9180a57SEric Paris goto out_err; 951c9180a57SEric Paris } 952c9180a57SEric Paris break; 95311689d47SDavid P. Quigley case Opt_labelsupport: 95411689d47SDavid P. Quigley break; 955c9180a57SEric Paris default: 956c9180a57SEric Paris rc = -EINVAL; 957c9180a57SEric Paris printk(KERN_WARNING "SELinux: unknown mount option\n"); 958c9180a57SEric Paris goto out_err; 959c9180a57SEric Paris 960c9180a57SEric Paris } 961c9180a57SEric Paris } 962c9180a57SEric Paris 963e0007529SEric Paris rc = -ENOMEM; 964e0007529SEric Paris opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_ATOMIC); 965e0007529SEric Paris if (!opts->mnt_opts) 966e0007529SEric Paris goto out_err; 967e0007529SEric Paris 968e0007529SEric Paris opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), GFP_ATOMIC); 969e0007529SEric Paris if (!opts->mnt_opts_flags) { 970e0007529SEric Paris kfree(opts->mnt_opts); 971e0007529SEric Paris goto out_err; 972c9180a57SEric Paris } 973c9180a57SEric Paris 974e0007529SEric Paris if (fscontext) { 975e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = fscontext; 976e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT; 977e0007529SEric Paris } 978e0007529SEric Paris if (context) { 979e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = context; 980e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT; 981e0007529SEric Paris } 982e0007529SEric Paris if (rootcontext) { 983e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = rootcontext; 984e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT; 985e0007529SEric Paris } 986e0007529SEric Paris if (defcontext) { 987e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = defcontext; 988e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT; 989e0007529SEric Paris } 990e0007529SEric Paris 991e0007529SEric Paris opts->num_mnt_opts = num_mnt_opts; 992e0007529SEric Paris return 0; 993e0007529SEric Paris 994c9180a57SEric Paris out_err: 995c9180a57SEric Paris kfree(context); 996c9180a57SEric Paris kfree(defcontext); 997c9180a57SEric Paris kfree(fscontext); 998c9180a57SEric Paris kfree(rootcontext); 999c9180a57SEric Paris return rc; 10001da177e4SLinus Torvalds } 1001e0007529SEric Paris /* 1002e0007529SEric Paris * string mount options parsing and call set the sbsec 1003e0007529SEric Paris */ 1004e0007529SEric Paris static int superblock_doinit(struct super_block *sb, void *data) 1005e0007529SEric Paris { 1006e0007529SEric Paris int rc = 0; 1007e0007529SEric Paris char *options = data; 1008e0007529SEric Paris struct security_mnt_opts opts; 1009e0007529SEric Paris 1010e0007529SEric Paris security_init_mnt_opts(&opts); 1011e0007529SEric Paris 1012e0007529SEric Paris if (!data) 1013e0007529SEric Paris goto out; 1014e0007529SEric Paris 1015e0007529SEric Paris BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA); 1016e0007529SEric Paris 1017e0007529SEric Paris rc = selinux_parse_opts_str(options, &opts); 1018e0007529SEric Paris if (rc) 1019e0007529SEric Paris goto out_err; 1020e0007529SEric Paris 1021e0007529SEric Paris out: 1022e0007529SEric Paris rc = selinux_set_mnt_opts(sb, &opts); 1023e0007529SEric Paris 1024e0007529SEric Paris out_err: 1025e0007529SEric Paris security_free_mnt_opts(&opts); 1026e0007529SEric Paris return rc; 1027e0007529SEric Paris } 10281da177e4SLinus Torvalds 10293583a711SAdrian Bunk static void selinux_write_opts(struct seq_file *m, 10303583a711SAdrian Bunk struct security_mnt_opts *opts) 10312069f457SEric Paris { 10322069f457SEric Paris int i; 10332069f457SEric Paris char *prefix; 10342069f457SEric Paris 10352069f457SEric Paris for (i = 0; i < opts->num_mnt_opts; i++) { 103611689d47SDavid P. Quigley char *has_comma; 103711689d47SDavid P. Quigley 103811689d47SDavid P. Quigley if (opts->mnt_opts[i]) 103911689d47SDavid P. Quigley has_comma = strchr(opts->mnt_opts[i], ','); 104011689d47SDavid P. Quigley else 104111689d47SDavid P. Quigley has_comma = NULL; 10422069f457SEric Paris 10432069f457SEric Paris switch (opts->mnt_opts_flags[i]) { 10442069f457SEric Paris case CONTEXT_MNT: 10452069f457SEric Paris prefix = CONTEXT_STR; 10462069f457SEric Paris break; 10472069f457SEric Paris case FSCONTEXT_MNT: 10482069f457SEric Paris prefix = FSCONTEXT_STR; 10492069f457SEric Paris break; 10502069f457SEric Paris case ROOTCONTEXT_MNT: 10512069f457SEric Paris prefix = ROOTCONTEXT_STR; 10522069f457SEric Paris break; 10532069f457SEric Paris case DEFCONTEXT_MNT: 10542069f457SEric Paris prefix = DEFCONTEXT_STR; 10552069f457SEric Paris break; 105612f348b9SEric Paris case SBLABEL_MNT: 105711689d47SDavid P. Quigley seq_putc(m, ','); 105811689d47SDavid P. Quigley seq_puts(m, LABELSUPP_STR); 105911689d47SDavid P. Quigley continue; 10602069f457SEric Paris default: 10612069f457SEric Paris BUG(); 1062a35c6c83SEric Paris return; 10632069f457SEric Paris }; 10642069f457SEric Paris /* we need a comma before each option */ 10652069f457SEric Paris seq_putc(m, ','); 10662069f457SEric Paris seq_puts(m, prefix); 10672069f457SEric Paris if (has_comma) 10682069f457SEric Paris seq_putc(m, '\"'); 10692069f457SEric Paris seq_puts(m, opts->mnt_opts[i]); 10702069f457SEric Paris if (has_comma) 10712069f457SEric Paris seq_putc(m, '\"'); 10722069f457SEric Paris } 10732069f457SEric Paris } 10742069f457SEric Paris 10752069f457SEric Paris static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb) 10762069f457SEric Paris { 10772069f457SEric Paris struct security_mnt_opts opts; 10782069f457SEric Paris int rc; 10792069f457SEric Paris 10802069f457SEric Paris rc = selinux_get_mnt_opts(sb, &opts); 1081383795c2SEric Paris if (rc) { 1082383795c2SEric Paris /* before policy load we may get EINVAL, don't show anything */ 1083383795c2SEric Paris if (rc == -EINVAL) 1084383795c2SEric Paris rc = 0; 10852069f457SEric Paris return rc; 1086383795c2SEric Paris } 10872069f457SEric Paris 10882069f457SEric Paris selinux_write_opts(m, &opts); 10892069f457SEric Paris 10902069f457SEric Paris security_free_mnt_opts(&opts); 10912069f457SEric Paris 10922069f457SEric Paris return rc; 10932069f457SEric Paris } 10942069f457SEric Paris 10951da177e4SLinus Torvalds static inline u16 inode_mode_to_security_class(umode_t mode) 10961da177e4SLinus Torvalds { 10971da177e4SLinus Torvalds switch (mode & S_IFMT) { 10981da177e4SLinus Torvalds case S_IFSOCK: 10991da177e4SLinus Torvalds return SECCLASS_SOCK_FILE; 11001da177e4SLinus Torvalds case S_IFLNK: 11011da177e4SLinus Torvalds return SECCLASS_LNK_FILE; 11021da177e4SLinus Torvalds case S_IFREG: 11031da177e4SLinus Torvalds return SECCLASS_FILE; 11041da177e4SLinus Torvalds case S_IFBLK: 11051da177e4SLinus Torvalds return SECCLASS_BLK_FILE; 11061da177e4SLinus Torvalds case S_IFDIR: 11071da177e4SLinus Torvalds return SECCLASS_DIR; 11081da177e4SLinus Torvalds case S_IFCHR: 11091da177e4SLinus Torvalds return SECCLASS_CHR_FILE; 11101da177e4SLinus Torvalds case S_IFIFO: 11111da177e4SLinus Torvalds return SECCLASS_FIFO_FILE; 11121da177e4SLinus Torvalds 11131da177e4SLinus Torvalds } 11141da177e4SLinus Torvalds 11151da177e4SLinus Torvalds return SECCLASS_FILE; 11161da177e4SLinus Torvalds } 11171da177e4SLinus Torvalds 111813402580SJames Morris static inline int default_protocol_stream(int protocol) 111913402580SJames Morris { 112013402580SJames Morris return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP); 112113402580SJames Morris } 112213402580SJames Morris 112313402580SJames Morris static inline int default_protocol_dgram(int protocol) 112413402580SJames Morris { 112513402580SJames Morris return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP); 112613402580SJames Morris } 112713402580SJames Morris 11281da177e4SLinus Torvalds static inline u16 socket_type_to_security_class(int family, int type, int protocol) 11291da177e4SLinus Torvalds { 11301da177e4SLinus Torvalds switch (family) { 11311da177e4SLinus Torvalds case PF_UNIX: 11321da177e4SLinus Torvalds switch (type) { 11331da177e4SLinus Torvalds case SOCK_STREAM: 11341da177e4SLinus Torvalds case SOCK_SEQPACKET: 11351da177e4SLinus Torvalds return SECCLASS_UNIX_STREAM_SOCKET; 11361da177e4SLinus Torvalds case SOCK_DGRAM: 11371da177e4SLinus Torvalds return SECCLASS_UNIX_DGRAM_SOCKET; 11381da177e4SLinus Torvalds } 11391da177e4SLinus Torvalds break; 11401da177e4SLinus Torvalds case PF_INET: 11411da177e4SLinus Torvalds case PF_INET6: 11421da177e4SLinus Torvalds switch (type) { 11431da177e4SLinus Torvalds case SOCK_STREAM: 114413402580SJames Morris if (default_protocol_stream(protocol)) 11451da177e4SLinus Torvalds return SECCLASS_TCP_SOCKET; 114613402580SJames Morris else 114713402580SJames Morris return SECCLASS_RAWIP_SOCKET; 11481da177e4SLinus Torvalds case SOCK_DGRAM: 114913402580SJames Morris if (default_protocol_dgram(protocol)) 11501da177e4SLinus Torvalds return SECCLASS_UDP_SOCKET; 115113402580SJames Morris else 115213402580SJames Morris return SECCLASS_RAWIP_SOCKET; 11532ee92d46SJames Morris case SOCK_DCCP: 11542ee92d46SJames Morris return SECCLASS_DCCP_SOCKET; 115513402580SJames Morris default: 11561da177e4SLinus Torvalds return SECCLASS_RAWIP_SOCKET; 11571da177e4SLinus Torvalds } 11581da177e4SLinus Torvalds break; 11591da177e4SLinus Torvalds case PF_NETLINK: 11601da177e4SLinus Torvalds switch (protocol) { 11611da177e4SLinus Torvalds case NETLINK_ROUTE: 11621da177e4SLinus Torvalds return SECCLASS_NETLINK_ROUTE_SOCKET; 11631da177e4SLinus Torvalds case NETLINK_FIREWALL: 11641da177e4SLinus Torvalds return SECCLASS_NETLINK_FIREWALL_SOCKET; 11657f1fb60cSPavel Emelyanov case NETLINK_SOCK_DIAG: 11661da177e4SLinus Torvalds return SECCLASS_NETLINK_TCPDIAG_SOCKET; 11671da177e4SLinus Torvalds case NETLINK_NFLOG: 11681da177e4SLinus Torvalds return SECCLASS_NETLINK_NFLOG_SOCKET; 11691da177e4SLinus Torvalds case NETLINK_XFRM: 11701da177e4SLinus Torvalds return SECCLASS_NETLINK_XFRM_SOCKET; 11711da177e4SLinus Torvalds case NETLINK_SELINUX: 11721da177e4SLinus Torvalds return SECCLASS_NETLINK_SELINUX_SOCKET; 11731da177e4SLinus Torvalds case NETLINK_AUDIT: 11741da177e4SLinus Torvalds return SECCLASS_NETLINK_AUDIT_SOCKET; 11751da177e4SLinus Torvalds case NETLINK_IP6_FW: 11761da177e4SLinus Torvalds return SECCLASS_NETLINK_IP6FW_SOCKET; 11771da177e4SLinus Torvalds case NETLINK_DNRTMSG: 11781da177e4SLinus Torvalds return SECCLASS_NETLINK_DNRT_SOCKET; 11790c9b7942SJames Morris case NETLINK_KOBJECT_UEVENT: 11800c9b7942SJames Morris return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET; 11811da177e4SLinus Torvalds default: 11821da177e4SLinus Torvalds return SECCLASS_NETLINK_SOCKET; 11831da177e4SLinus Torvalds } 11841da177e4SLinus Torvalds case PF_PACKET: 11851da177e4SLinus Torvalds return SECCLASS_PACKET_SOCKET; 11861da177e4SLinus Torvalds case PF_KEY: 11871da177e4SLinus Torvalds return SECCLASS_KEY_SOCKET; 11883e3ff15eSChristopher J. PeBenito case PF_APPLETALK: 11893e3ff15eSChristopher J. PeBenito return SECCLASS_APPLETALK_SOCKET; 11901da177e4SLinus Torvalds } 11911da177e4SLinus Torvalds 11921da177e4SLinus Torvalds return SECCLASS_SOCKET; 11931da177e4SLinus Torvalds } 11941da177e4SLinus Torvalds 11951da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 11968e6c9693SLucian Adrian Grijincu static int selinux_proc_get_sid(struct dentry *dentry, 11971da177e4SLinus Torvalds u16 tclass, 11981da177e4SLinus Torvalds u32 *sid) 11991da177e4SLinus Torvalds { 12008e6c9693SLucian Adrian Grijincu int rc; 12018e6c9693SLucian Adrian Grijincu char *buffer, *path; 12021da177e4SLinus Torvalds 12031da177e4SLinus Torvalds buffer = (char *)__get_free_page(GFP_KERNEL); 12041da177e4SLinus Torvalds if (!buffer) 12051da177e4SLinus Torvalds return -ENOMEM; 12061da177e4SLinus Torvalds 12078e6c9693SLucian Adrian Grijincu path = dentry_path_raw(dentry, buffer, PAGE_SIZE); 12088e6c9693SLucian Adrian Grijincu if (IS_ERR(path)) 12098e6c9693SLucian Adrian Grijincu rc = PTR_ERR(path); 12108e6c9693SLucian Adrian Grijincu else { 12118e6c9693SLucian Adrian Grijincu /* each process gets a /proc/PID/ entry. Strip off the 12128e6c9693SLucian Adrian Grijincu * PID part to get a valid selinux labeling. 12138e6c9693SLucian Adrian Grijincu * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */ 12148e6c9693SLucian Adrian Grijincu while (path[1] >= '0' && path[1] <= '9') { 12158e6c9693SLucian Adrian Grijincu path[1] = '/'; 12168e6c9693SLucian Adrian Grijincu path++; 12171da177e4SLinus Torvalds } 12181da177e4SLinus Torvalds rc = security_genfs_sid("proc", path, tclass, sid); 12198e6c9693SLucian Adrian Grijincu } 12201da177e4SLinus Torvalds free_page((unsigned long)buffer); 12211da177e4SLinus Torvalds return rc; 12221da177e4SLinus Torvalds } 12231da177e4SLinus Torvalds #else 12248e6c9693SLucian Adrian Grijincu static int selinux_proc_get_sid(struct dentry *dentry, 12251da177e4SLinus Torvalds u16 tclass, 12261da177e4SLinus Torvalds u32 *sid) 12271da177e4SLinus Torvalds { 12281da177e4SLinus Torvalds return -EINVAL; 12291da177e4SLinus Torvalds } 12301da177e4SLinus Torvalds #endif 12311da177e4SLinus Torvalds 12321da177e4SLinus Torvalds /* The inode's security attributes must be initialized before first use. */ 12331da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry) 12341da177e4SLinus Torvalds { 12351da177e4SLinus Torvalds struct superblock_security_struct *sbsec = NULL; 12361da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 12371da177e4SLinus Torvalds u32 sid; 12381da177e4SLinus Torvalds struct dentry *dentry; 12391da177e4SLinus Torvalds #define INITCONTEXTLEN 255 12401da177e4SLinus Torvalds char *context = NULL; 12411da177e4SLinus Torvalds unsigned len = 0; 12421da177e4SLinus Torvalds int rc = 0; 12431da177e4SLinus Torvalds 12441da177e4SLinus Torvalds if (isec->initialized) 12451da177e4SLinus Torvalds goto out; 12461da177e4SLinus Torvalds 124723970741SEric Paris mutex_lock(&isec->lock); 12481da177e4SLinus Torvalds if (isec->initialized) 124923970741SEric Paris goto out_unlock; 12501da177e4SLinus Torvalds 12511da177e4SLinus Torvalds sbsec = inode->i_sb->s_security; 12520d90a7ecSDavid P. Quigley if (!(sbsec->flags & SE_SBINITIALIZED)) { 12531da177e4SLinus Torvalds /* Defer initialization until selinux_complete_init, 12541da177e4SLinus Torvalds after the initial policy is loaded and the security 12551da177e4SLinus Torvalds server is ready to handle calls. */ 12561da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 12571da177e4SLinus Torvalds if (list_empty(&isec->list)) 12581da177e4SLinus Torvalds list_add(&isec->list, &sbsec->isec_head); 12591da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 126023970741SEric Paris goto out_unlock; 12611da177e4SLinus Torvalds } 12621da177e4SLinus Torvalds 12631da177e4SLinus Torvalds switch (sbsec->behavior) { 12641da177e4SLinus Torvalds case SECURITY_FS_USE_XATTR: 12651da177e4SLinus Torvalds if (!inode->i_op->getxattr) { 12661da177e4SLinus Torvalds isec->sid = sbsec->def_sid; 12671da177e4SLinus Torvalds break; 12681da177e4SLinus Torvalds } 12691da177e4SLinus Torvalds 12701da177e4SLinus Torvalds /* Need a dentry, since the xattr API requires one. 12711da177e4SLinus Torvalds Life would be simpler if we could just pass the inode. */ 12721da177e4SLinus Torvalds if (opt_dentry) { 12731da177e4SLinus Torvalds /* Called from d_instantiate or d_splice_alias. */ 12741da177e4SLinus Torvalds dentry = dget(opt_dentry); 12751da177e4SLinus Torvalds } else { 12761da177e4SLinus Torvalds /* Called from selinux_complete_init, try to find a dentry. */ 12771da177e4SLinus Torvalds dentry = d_find_alias(inode); 12781da177e4SLinus Torvalds } 12791da177e4SLinus Torvalds if (!dentry) { 1280df7f54c0SEric Paris /* 1281df7f54c0SEric Paris * this is can be hit on boot when a file is accessed 1282df7f54c0SEric Paris * before the policy is loaded. When we load policy we 1283df7f54c0SEric Paris * may find inodes that have no dentry on the 1284df7f54c0SEric Paris * sbsec->isec_head list. No reason to complain as these 1285df7f54c0SEric Paris * will get fixed up the next time we go through 1286df7f54c0SEric Paris * inode_doinit with a dentry, before these inodes could 1287df7f54c0SEric Paris * be used again by userspace. 1288df7f54c0SEric Paris */ 128923970741SEric Paris goto out_unlock; 12901da177e4SLinus Torvalds } 12911da177e4SLinus Torvalds 12921da177e4SLinus Torvalds len = INITCONTEXTLEN; 12934cb912f1SEric Paris context = kmalloc(len+1, GFP_NOFS); 12941da177e4SLinus Torvalds if (!context) { 12951da177e4SLinus Torvalds rc = -ENOMEM; 12961da177e4SLinus Torvalds dput(dentry); 129723970741SEric Paris goto out_unlock; 12981da177e4SLinus Torvalds } 12994cb912f1SEric Paris context[len] = '\0'; 13001da177e4SLinus Torvalds rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX, 13011da177e4SLinus Torvalds context, len); 13021da177e4SLinus Torvalds if (rc == -ERANGE) { 1303314dabb8SJames Morris kfree(context); 1304314dabb8SJames Morris 13051da177e4SLinus Torvalds /* Need a larger buffer. Query for the right size. */ 13061da177e4SLinus Torvalds rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX, 13071da177e4SLinus Torvalds NULL, 0); 13081da177e4SLinus Torvalds if (rc < 0) { 13091da177e4SLinus Torvalds dput(dentry); 131023970741SEric Paris goto out_unlock; 13111da177e4SLinus Torvalds } 13121da177e4SLinus Torvalds len = rc; 13134cb912f1SEric Paris context = kmalloc(len+1, GFP_NOFS); 13141da177e4SLinus Torvalds if (!context) { 13151da177e4SLinus Torvalds rc = -ENOMEM; 13161da177e4SLinus Torvalds dput(dentry); 131723970741SEric Paris goto out_unlock; 13181da177e4SLinus Torvalds } 13194cb912f1SEric Paris context[len] = '\0'; 13201da177e4SLinus Torvalds rc = inode->i_op->getxattr(dentry, 13211da177e4SLinus Torvalds XATTR_NAME_SELINUX, 13221da177e4SLinus Torvalds context, len); 13231da177e4SLinus Torvalds } 13241da177e4SLinus Torvalds dput(dentry); 13251da177e4SLinus Torvalds if (rc < 0) { 13261da177e4SLinus Torvalds if (rc != -ENODATA) { 1327744ba35eSEric Paris printk(KERN_WARNING "SELinux: %s: getxattr returned " 1328dd6f953aSHarvey Harrison "%d for dev=%s ino=%ld\n", __func__, 13291da177e4SLinus Torvalds -rc, inode->i_sb->s_id, inode->i_ino); 13301da177e4SLinus Torvalds kfree(context); 133123970741SEric Paris goto out_unlock; 13321da177e4SLinus Torvalds } 13331da177e4SLinus Torvalds /* Map ENODATA to the default file SID */ 13341da177e4SLinus Torvalds sid = sbsec->def_sid; 13351da177e4SLinus Torvalds rc = 0; 13361da177e4SLinus Torvalds } else { 1337f5c1d5b2SJames Morris rc = security_context_to_sid_default(context, rc, &sid, 1338869ab514SStephen Smalley sbsec->def_sid, 1339869ab514SStephen Smalley GFP_NOFS); 13401da177e4SLinus Torvalds if (rc) { 13414ba0a8adSEric Paris char *dev = inode->i_sb->s_id; 13424ba0a8adSEric Paris unsigned long ino = inode->i_ino; 13434ba0a8adSEric Paris 13444ba0a8adSEric Paris if (rc == -EINVAL) { 13454ba0a8adSEric Paris if (printk_ratelimit()) 13464ba0a8adSEric Paris printk(KERN_NOTICE "SELinux: inode=%lu on dev=%s was found to have an invalid " 13474ba0a8adSEric Paris "context=%s. This indicates you may need to relabel the inode or the " 13484ba0a8adSEric Paris "filesystem in question.\n", ino, dev, context); 13494ba0a8adSEric Paris } else { 1350744ba35eSEric Paris printk(KERN_WARNING "SELinux: %s: context_to_sid(%s) " 13511da177e4SLinus Torvalds "returned %d for dev=%s ino=%ld\n", 13524ba0a8adSEric Paris __func__, context, -rc, dev, ino); 13534ba0a8adSEric Paris } 13541da177e4SLinus Torvalds kfree(context); 13551da177e4SLinus Torvalds /* Leave with the unlabeled SID */ 13561da177e4SLinus Torvalds rc = 0; 13571da177e4SLinus Torvalds break; 13581da177e4SLinus Torvalds } 13591da177e4SLinus Torvalds } 13601da177e4SLinus Torvalds kfree(context); 13611da177e4SLinus Torvalds isec->sid = sid; 13621da177e4SLinus Torvalds break; 13631da177e4SLinus Torvalds case SECURITY_FS_USE_TASK: 13641da177e4SLinus Torvalds isec->sid = isec->task_sid; 13651da177e4SLinus Torvalds break; 13661da177e4SLinus Torvalds case SECURITY_FS_USE_TRANS: 13671da177e4SLinus Torvalds /* Default to the fs SID. */ 13681da177e4SLinus Torvalds isec->sid = sbsec->sid; 13691da177e4SLinus Torvalds 13701da177e4SLinus Torvalds /* Try to obtain a transition SID. */ 13711da177e4SLinus Torvalds isec->sclass = inode_mode_to_security_class(inode->i_mode); 1372652bb9b0SEric Paris rc = security_transition_sid(isec->task_sid, sbsec->sid, 1373652bb9b0SEric Paris isec->sclass, NULL, &sid); 13741da177e4SLinus Torvalds if (rc) 137523970741SEric Paris goto out_unlock; 13761da177e4SLinus Torvalds isec->sid = sid; 13771da177e4SLinus Torvalds break; 1378c312feb2SEric Paris case SECURITY_FS_USE_MNTPOINT: 1379c312feb2SEric Paris isec->sid = sbsec->mntpoint_sid; 1380c312feb2SEric Paris break; 13811da177e4SLinus Torvalds default: 1382c312feb2SEric Paris /* Default to the fs superblock SID. */ 13831da177e4SLinus Torvalds isec->sid = sbsec->sid; 13841da177e4SLinus Torvalds 13850d90a7ecSDavid P. Quigley if ((sbsec->flags & SE_SBPROC) && !S_ISLNK(inode->i_mode)) { 13868e6c9693SLucian Adrian Grijincu if (opt_dentry) { 13871da177e4SLinus Torvalds isec->sclass = inode_mode_to_security_class(inode->i_mode); 13888e6c9693SLucian Adrian Grijincu rc = selinux_proc_get_sid(opt_dentry, 13891da177e4SLinus Torvalds isec->sclass, 13901da177e4SLinus Torvalds &sid); 13911da177e4SLinus Torvalds if (rc) 139223970741SEric Paris goto out_unlock; 13931da177e4SLinus Torvalds isec->sid = sid; 13941da177e4SLinus Torvalds } 13951da177e4SLinus Torvalds } 13961da177e4SLinus Torvalds break; 13971da177e4SLinus Torvalds } 13981da177e4SLinus Torvalds 13991da177e4SLinus Torvalds isec->initialized = 1; 14001da177e4SLinus Torvalds 140123970741SEric Paris out_unlock: 140223970741SEric Paris mutex_unlock(&isec->lock); 14031da177e4SLinus Torvalds out: 14041da177e4SLinus Torvalds if (isec->sclass == SECCLASS_FILE) 14051da177e4SLinus Torvalds isec->sclass = inode_mode_to_security_class(inode->i_mode); 14061da177e4SLinus Torvalds return rc; 14071da177e4SLinus Torvalds } 14081da177e4SLinus Torvalds 14091da177e4SLinus Torvalds /* Convert a Linux signal to an access vector. */ 14101da177e4SLinus Torvalds static inline u32 signal_to_av(int sig) 14111da177e4SLinus Torvalds { 14121da177e4SLinus Torvalds u32 perm = 0; 14131da177e4SLinus Torvalds 14141da177e4SLinus Torvalds switch (sig) { 14151da177e4SLinus Torvalds case SIGCHLD: 14161da177e4SLinus Torvalds /* Commonly granted from child to parent. */ 14171da177e4SLinus Torvalds perm = PROCESS__SIGCHLD; 14181da177e4SLinus Torvalds break; 14191da177e4SLinus Torvalds case SIGKILL: 14201da177e4SLinus Torvalds /* Cannot be caught or ignored */ 14211da177e4SLinus Torvalds perm = PROCESS__SIGKILL; 14221da177e4SLinus Torvalds break; 14231da177e4SLinus Torvalds case SIGSTOP: 14241da177e4SLinus Torvalds /* Cannot be caught or ignored */ 14251da177e4SLinus Torvalds perm = PROCESS__SIGSTOP; 14261da177e4SLinus Torvalds break; 14271da177e4SLinus Torvalds default: 14281da177e4SLinus Torvalds /* All other signals. */ 14291da177e4SLinus Torvalds perm = PROCESS__SIGNAL; 14301da177e4SLinus Torvalds break; 14311da177e4SLinus Torvalds } 14321da177e4SLinus Torvalds 14331da177e4SLinus Torvalds return perm; 14341da177e4SLinus Torvalds } 14351da177e4SLinus Torvalds 1436275bb41eSDavid Howells /* 1437d84f4f99SDavid Howells * Check permission between a pair of credentials 1438d84f4f99SDavid Howells * fork check, ptrace check, etc. 1439d84f4f99SDavid Howells */ 1440d84f4f99SDavid Howells static int cred_has_perm(const struct cred *actor, 1441d84f4f99SDavid Howells const struct cred *target, 1442d84f4f99SDavid Howells u32 perms) 1443d84f4f99SDavid Howells { 1444d84f4f99SDavid Howells u32 asid = cred_sid(actor), tsid = cred_sid(target); 1445d84f4f99SDavid Howells 1446d84f4f99SDavid Howells return avc_has_perm(asid, tsid, SECCLASS_PROCESS, perms, NULL); 1447d84f4f99SDavid Howells } 1448d84f4f99SDavid Howells 1449d84f4f99SDavid Howells /* 145088e67f3bSDavid Howells * Check permission between a pair of tasks, e.g. signal checks, 1451275bb41eSDavid Howells * fork check, ptrace check, etc. 1452275bb41eSDavid Howells * tsk1 is the actor and tsk2 is the target 14533b11a1deSDavid Howells * - this uses the default subjective creds of tsk1 1454275bb41eSDavid Howells */ 1455275bb41eSDavid Howells static int task_has_perm(const struct task_struct *tsk1, 1456275bb41eSDavid Howells const struct task_struct *tsk2, 14571da177e4SLinus Torvalds u32 perms) 14581da177e4SLinus Torvalds { 1459275bb41eSDavid Howells const struct task_security_struct *__tsec1, *__tsec2; 1460275bb41eSDavid Howells u32 sid1, sid2; 14611da177e4SLinus Torvalds 1462275bb41eSDavid Howells rcu_read_lock(); 1463275bb41eSDavid Howells __tsec1 = __task_cred(tsk1)->security; sid1 = __tsec1->sid; 1464275bb41eSDavid Howells __tsec2 = __task_cred(tsk2)->security; sid2 = __tsec2->sid; 1465275bb41eSDavid Howells rcu_read_unlock(); 1466275bb41eSDavid Howells return avc_has_perm(sid1, sid2, SECCLASS_PROCESS, perms, NULL); 14671da177e4SLinus Torvalds } 14681da177e4SLinus Torvalds 14693b11a1deSDavid Howells /* 14703b11a1deSDavid Howells * Check permission between current and another task, e.g. signal checks, 14713b11a1deSDavid Howells * fork check, ptrace check, etc. 14723b11a1deSDavid Howells * current is the actor and tsk2 is the target 14733b11a1deSDavid Howells * - this uses current's subjective creds 14743b11a1deSDavid Howells */ 14753b11a1deSDavid Howells static int current_has_perm(const struct task_struct *tsk, 14763b11a1deSDavid Howells u32 perms) 14773b11a1deSDavid Howells { 14783b11a1deSDavid Howells u32 sid, tsid; 14793b11a1deSDavid Howells 14803b11a1deSDavid Howells sid = current_sid(); 14813b11a1deSDavid Howells tsid = task_sid(tsk); 14823b11a1deSDavid Howells return avc_has_perm(sid, tsid, SECCLASS_PROCESS, perms, NULL); 14833b11a1deSDavid Howells } 14843b11a1deSDavid Howells 1485b68e418cSStephen Smalley #if CAP_LAST_CAP > 63 1486b68e418cSStephen Smalley #error Fix SELinux to handle capabilities > 63. 1487b68e418cSStephen Smalley #endif 1488b68e418cSStephen Smalley 14891da177e4SLinus Torvalds /* Check whether a task is allowed to use a capability. */ 14906a9de491SEric Paris static int cred_has_capability(const struct cred *cred, 149106112163SEric Paris int cap, int audit) 14921da177e4SLinus Torvalds { 14932bf49690SThomas Liu struct common_audit_data ad; 149406112163SEric Paris struct av_decision avd; 1495b68e418cSStephen Smalley u16 sclass; 14963699c53cSDavid Howells u32 sid = cred_sid(cred); 1497b68e418cSStephen Smalley u32 av = CAP_TO_MASK(cap); 149806112163SEric Paris int rc; 14991da177e4SLinus Torvalds 150050c205f5SEric Paris ad.type = LSM_AUDIT_DATA_CAP; 15011da177e4SLinus Torvalds ad.u.cap = cap; 15021da177e4SLinus Torvalds 1503b68e418cSStephen Smalley switch (CAP_TO_INDEX(cap)) { 1504b68e418cSStephen Smalley case 0: 1505b68e418cSStephen Smalley sclass = SECCLASS_CAPABILITY; 1506b68e418cSStephen Smalley break; 1507b68e418cSStephen Smalley case 1: 1508b68e418cSStephen Smalley sclass = SECCLASS_CAPABILITY2; 1509b68e418cSStephen Smalley break; 1510b68e418cSStephen Smalley default: 1511b68e418cSStephen Smalley printk(KERN_ERR 1512b68e418cSStephen Smalley "SELinux: out of range capability %d\n", cap); 1513b68e418cSStephen Smalley BUG(); 1514a35c6c83SEric Paris return -EINVAL; 1515b68e418cSStephen Smalley } 151606112163SEric Paris 1517275bb41eSDavid Howells rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd); 15189ade0cf4SEric Paris if (audit == SECURITY_CAP_AUDIT) { 15199ade0cf4SEric Paris int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad, 0); 15209ade0cf4SEric Paris if (rc2) 15219ade0cf4SEric Paris return rc2; 15229ade0cf4SEric Paris } 152306112163SEric Paris return rc; 15241da177e4SLinus Torvalds } 15251da177e4SLinus Torvalds 15261da177e4SLinus Torvalds /* Check whether a task is allowed to use a system operation. */ 15271da177e4SLinus Torvalds static int task_has_system(struct task_struct *tsk, 15281da177e4SLinus Torvalds u32 perms) 15291da177e4SLinus Torvalds { 1530275bb41eSDavid Howells u32 sid = task_sid(tsk); 15311da177e4SLinus Torvalds 1532275bb41eSDavid Howells return avc_has_perm(sid, SECINITSID_KERNEL, 15331da177e4SLinus Torvalds SECCLASS_SYSTEM, perms, NULL); 15341da177e4SLinus Torvalds } 15351da177e4SLinus Torvalds 15361da177e4SLinus Torvalds /* Check whether a task has a particular permission to an inode. 15371da177e4SLinus Torvalds The 'adp' parameter is optional and allows other audit 15381da177e4SLinus Torvalds data to be passed (e.g. the dentry). */ 153988e67f3bSDavid Howells static int inode_has_perm(const struct cred *cred, 15401da177e4SLinus Torvalds struct inode *inode, 15411da177e4SLinus Torvalds u32 perms, 15429ade0cf4SEric Paris struct common_audit_data *adp, 15439ade0cf4SEric Paris unsigned flags) 15441da177e4SLinus Torvalds { 15451da177e4SLinus Torvalds struct inode_security_struct *isec; 1546275bb41eSDavid Howells u32 sid; 15471da177e4SLinus Torvalds 1548e0e81739SDavid Howells validate_creds(cred); 1549e0e81739SDavid Howells 1550bbaca6c2SStephen Smalley if (unlikely(IS_PRIVATE(inode))) 1551bbaca6c2SStephen Smalley return 0; 1552bbaca6c2SStephen Smalley 155388e67f3bSDavid Howells sid = cred_sid(cred); 15541da177e4SLinus Torvalds isec = inode->i_security; 15551da177e4SLinus Torvalds 155695f4efb2SLinus Torvalds return avc_has_perm_flags(sid, isec->sid, isec->sclass, perms, adp, flags); 15571da177e4SLinus Torvalds } 15581da177e4SLinus Torvalds 15591da177e4SLinus Torvalds /* Same as inode_has_perm, but pass explicit audit data containing 15601da177e4SLinus Torvalds the dentry to help the auditing code to more easily generate the 15611da177e4SLinus Torvalds pathname if needed. */ 156288e67f3bSDavid Howells static inline int dentry_has_perm(const struct cred *cred, 15631da177e4SLinus Torvalds struct dentry *dentry, 15641da177e4SLinus Torvalds u32 av) 15651da177e4SLinus Torvalds { 15661da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 15672bf49690SThomas Liu struct common_audit_data ad; 156888e67f3bSDavid Howells 156950c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 15702875fa00SEric Paris ad.u.dentry = dentry; 15712875fa00SEric Paris return inode_has_perm(cred, inode, av, &ad, 0); 15722875fa00SEric Paris } 15732875fa00SEric Paris 15742875fa00SEric Paris /* Same as inode_has_perm, but pass explicit audit data containing 15752875fa00SEric Paris the path to help the auditing code to more easily generate the 15762875fa00SEric Paris pathname if needed. */ 15772875fa00SEric Paris static inline int path_has_perm(const struct cred *cred, 15782875fa00SEric Paris struct path *path, 15792875fa00SEric Paris u32 av) 15802875fa00SEric Paris { 15812875fa00SEric Paris struct inode *inode = path->dentry->d_inode; 15822875fa00SEric Paris struct common_audit_data ad; 15832875fa00SEric Paris 158450c205f5SEric Paris ad.type = LSM_AUDIT_DATA_PATH; 15852875fa00SEric Paris ad.u.path = *path; 15869ade0cf4SEric Paris return inode_has_perm(cred, inode, av, &ad, 0); 15871da177e4SLinus Torvalds } 15881da177e4SLinus Torvalds 15891da177e4SLinus Torvalds /* Check whether a task can use an open file descriptor to 15901da177e4SLinus Torvalds access an inode in a given way. Check access to the 15911da177e4SLinus Torvalds descriptor itself, and then use dentry_has_perm to 15921da177e4SLinus Torvalds check a particular permission to the file. 15931da177e4SLinus Torvalds Access to the descriptor is implicitly granted if it 15941da177e4SLinus Torvalds has the same SID as the process. If av is zero, then 15951da177e4SLinus Torvalds access to the file is not checked, e.g. for cases 15961da177e4SLinus Torvalds where only the descriptor is affected like seek. */ 159788e67f3bSDavid Howells static int file_has_perm(const struct cred *cred, 15981da177e4SLinus Torvalds struct file *file, 15991da177e4SLinus Torvalds u32 av) 16001da177e4SLinus Torvalds { 16011da177e4SLinus Torvalds struct file_security_struct *fsec = file->f_security; 1602496ad9aaSAl Viro struct inode *inode = file_inode(file); 16032bf49690SThomas Liu struct common_audit_data ad; 160488e67f3bSDavid Howells u32 sid = cred_sid(cred); 16051da177e4SLinus Torvalds int rc; 16061da177e4SLinus Torvalds 160750c205f5SEric Paris ad.type = LSM_AUDIT_DATA_PATH; 1608f48b7399SEric Paris ad.u.path = file->f_path; 16091da177e4SLinus Torvalds 1610275bb41eSDavid Howells if (sid != fsec->sid) { 1611275bb41eSDavid Howells rc = avc_has_perm(sid, fsec->sid, 16121da177e4SLinus Torvalds SECCLASS_FD, 16131da177e4SLinus Torvalds FD__USE, 16141da177e4SLinus Torvalds &ad); 16151da177e4SLinus Torvalds if (rc) 161688e67f3bSDavid Howells goto out; 16171da177e4SLinus Torvalds } 16181da177e4SLinus Torvalds 16191da177e4SLinus Torvalds /* av is zero if only checking access to the descriptor. */ 162088e67f3bSDavid Howells rc = 0; 16211da177e4SLinus Torvalds if (av) 16229ade0cf4SEric Paris rc = inode_has_perm(cred, inode, av, &ad, 0); 16231da177e4SLinus Torvalds 162488e67f3bSDavid Howells out: 162588e67f3bSDavid Howells return rc; 16261da177e4SLinus Torvalds } 16271da177e4SLinus Torvalds 16281da177e4SLinus Torvalds /* Check whether a task can create a file. */ 16291da177e4SLinus Torvalds static int may_create(struct inode *dir, 16301da177e4SLinus Torvalds struct dentry *dentry, 16311da177e4SLinus Torvalds u16 tclass) 16321da177e4SLinus Torvalds { 16335fb49870SPaul Moore const struct task_security_struct *tsec = current_security(); 16341da177e4SLinus Torvalds struct inode_security_struct *dsec; 16351da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 1636275bb41eSDavid Howells u32 sid, newsid; 16372bf49690SThomas Liu struct common_audit_data ad; 16381da177e4SLinus Torvalds int rc; 16391da177e4SLinus Torvalds 16401da177e4SLinus Torvalds dsec = dir->i_security; 16411da177e4SLinus Torvalds sbsec = dir->i_sb->s_security; 16421da177e4SLinus Torvalds 1643275bb41eSDavid Howells sid = tsec->sid; 1644275bb41eSDavid Howells newsid = tsec->create_sid; 1645275bb41eSDavid Howells 164650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 1647a269434dSEric Paris ad.u.dentry = dentry; 16481da177e4SLinus Torvalds 1649275bb41eSDavid Howells rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, 16501da177e4SLinus Torvalds DIR__ADD_NAME | DIR__SEARCH, 16511da177e4SLinus Torvalds &ad); 16521da177e4SLinus Torvalds if (rc) 16531da177e4SLinus Torvalds return rc; 16541da177e4SLinus Torvalds 165512f348b9SEric Paris if (!newsid || !(sbsec->flags & SBLABEL_MNT)) { 1656cb1e922fSEric Paris rc = security_transition_sid(sid, dsec->sid, tclass, 1657cb1e922fSEric Paris &dentry->d_name, &newsid); 16581da177e4SLinus Torvalds if (rc) 16591da177e4SLinus Torvalds return rc; 16601da177e4SLinus Torvalds } 16611da177e4SLinus Torvalds 1662275bb41eSDavid Howells rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad); 16631da177e4SLinus Torvalds if (rc) 16641da177e4SLinus Torvalds return rc; 16651da177e4SLinus Torvalds 16661da177e4SLinus Torvalds return avc_has_perm(newsid, sbsec->sid, 16671da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 16681da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, &ad); 16691da177e4SLinus Torvalds } 16701da177e4SLinus Torvalds 16714eb582cfSMichael LeMay /* Check whether a task can create a key. */ 16724eb582cfSMichael LeMay static int may_create_key(u32 ksid, 16734eb582cfSMichael LeMay struct task_struct *ctx) 16744eb582cfSMichael LeMay { 1675275bb41eSDavid Howells u32 sid = task_sid(ctx); 16764eb582cfSMichael LeMay 1677275bb41eSDavid Howells return avc_has_perm(sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL); 16784eb582cfSMichael LeMay } 16794eb582cfSMichael LeMay 16801da177e4SLinus Torvalds #define MAY_LINK 0 16811da177e4SLinus Torvalds #define MAY_UNLINK 1 16821da177e4SLinus Torvalds #define MAY_RMDIR 2 16831da177e4SLinus Torvalds 16841da177e4SLinus Torvalds /* Check whether a task can link, unlink, or rmdir a file/directory. */ 16851da177e4SLinus Torvalds static int may_link(struct inode *dir, 16861da177e4SLinus Torvalds struct dentry *dentry, 16871da177e4SLinus Torvalds int kind) 16881da177e4SLinus Torvalds 16891da177e4SLinus Torvalds { 16901da177e4SLinus Torvalds struct inode_security_struct *dsec, *isec; 16912bf49690SThomas Liu struct common_audit_data ad; 1692275bb41eSDavid Howells u32 sid = current_sid(); 16931da177e4SLinus Torvalds u32 av; 16941da177e4SLinus Torvalds int rc; 16951da177e4SLinus Torvalds 16961da177e4SLinus Torvalds dsec = dir->i_security; 16971da177e4SLinus Torvalds isec = dentry->d_inode->i_security; 16981da177e4SLinus Torvalds 169950c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 1700a269434dSEric Paris ad.u.dentry = dentry; 17011da177e4SLinus Torvalds 17021da177e4SLinus Torvalds av = DIR__SEARCH; 17031da177e4SLinus Torvalds av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME); 1704275bb41eSDavid Howells rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad); 17051da177e4SLinus Torvalds if (rc) 17061da177e4SLinus Torvalds return rc; 17071da177e4SLinus Torvalds 17081da177e4SLinus Torvalds switch (kind) { 17091da177e4SLinus Torvalds case MAY_LINK: 17101da177e4SLinus Torvalds av = FILE__LINK; 17111da177e4SLinus Torvalds break; 17121da177e4SLinus Torvalds case MAY_UNLINK: 17131da177e4SLinus Torvalds av = FILE__UNLINK; 17141da177e4SLinus Torvalds break; 17151da177e4SLinus Torvalds case MAY_RMDIR: 17161da177e4SLinus Torvalds av = DIR__RMDIR; 17171da177e4SLinus Torvalds break; 17181da177e4SLinus Torvalds default: 1719744ba35eSEric Paris printk(KERN_WARNING "SELinux: %s: unrecognized kind %d\n", 1720744ba35eSEric Paris __func__, kind); 17211da177e4SLinus Torvalds return 0; 17221da177e4SLinus Torvalds } 17231da177e4SLinus Torvalds 1724275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad); 17251da177e4SLinus Torvalds return rc; 17261da177e4SLinus Torvalds } 17271da177e4SLinus Torvalds 17281da177e4SLinus Torvalds static inline int may_rename(struct inode *old_dir, 17291da177e4SLinus Torvalds struct dentry *old_dentry, 17301da177e4SLinus Torvalds struct inode *new_dir, 17311da177e4SLinus Torvalds struct dentry *new_dentry) 17321da177e4SLinus Torvalds { 17331da177e4SLinus Torvalds struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec; 17342bf49690SThomas Liu struct common_audit_data ad; 1735275bb41eSDavid Howells u32 sid = current_sid(); 17361da177e4SLinus Torvalds u32 av; 17371da177e4SLinus Torvalds int old_is_dir, new_is_dir; 17381da177e4SLinus Torvalds int rc; 17391da177e4SLinus Torvalds 17401da177e4SLinus Torvalds old_dsec = old_dir->i_security; 17411da177e4SLinus Torvalds old_isec = old_dentry->d_inode->i_security; 17421da177e4SLinus Torvalds old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 17431da177e4SLinus Torvalds new_dsec = new_dir->i_security; 17441da177e4SLinus Torvalds 174550c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 17461da177e4SLinus Torvalds 1747a269434dSEric Paris ad.u.dentry = old_dentry; 1748275bb41eSDavid Howells rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR, 17491da177e4SLinus Torvalds DIR__REMOVE_NAME | DIR__SEARCH, &ad); 17501da177e4SLinus Torvalds if (rc) 17511da177e4SLinus Torvalds return rc; 1752275bb41eSDavid Howells rc = avc_has_perm(sid, old_isec->sid, 17531da177e4SLinus Torvalds old_isec->sclass, FILE__RENAME, &ad); 17541da177e4SLinus Torvalds if (rc) 17551da177e4SLinus Torvalds return rc; 17561da177e4SLinus Torvalds if (old_is_dir && new_dir != old_dir) { 1757275bb41eSDavid Howells rc = avc_has_perm(sid, old_isec->sid, 17581da177e4SLinus Torvalds old_isec->sclass, DIR__REPARENT, &ad); 17591da177e4SLinus Torvalds if (rc) 17601da177e4SLinus Torvalds return rc; 17611da177e4SLinus Torvalds } 17621da177e4SLinus Torvalds 1763a269434dSEric Paris ad.u.dentry = new_dentry; 17641da177e4SLinus Torvalds av = DIR__ADD_NAME | DIR__SEARCH; 17651da177e4SLinus Torvalds if (new_dentry->d_inode) 17661da177e4SLinus Torvalds av |= DIR__REMOVE_NAME; 1767275bb41eSDavid Howells rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad); 17681da177e4SLinus Torvalds if (rc) 17691da177e4SLinus Torvalds return rc; 17701da177e4SLinus Torvalds if (new_dentry->d_inode) { 17711da177e4SLinus Torvalds new_isec = new_dentry->d_inode->i_security; 17721da177e4SLinus Torvalds new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode); 1773275bb41eSDavid Howells rc = avc_has_perm(sid, new_isec->sid, 17741da177e4SLinus Torvalds new_isec->sclass, 17751da177e4SLinus Torvalds (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad); 17761da177e4SLinus Torvalds if (rc) 17771da177e4SLinus Torvalds return rc; 17781da177e4SLinus Torvalds } 17791da177e4SLinus Torvalds 17801da177e4SLinus Torvalds return 0; 17811da177e4SLinus Torvalds } 17821da177e4SLinus Torvalds 17831da177e4SLinus Torvalds /* Check whether a task can perform a filesystem operation. */ 178488e67f3bSDavid Howells static int superblock_has_perm(const struct cred *cred, 17851da177e4SLinus Torvalds struct super_block *sb, 17861da177e4SLinus Torvalds u32 perms, 17872bf49690SThomas Liu struct common_audit_data *ad) 17881da177e4SLinus Torvalds { 17891da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 179088e67f3bSDavid Howells u32 sid = cred_sid(cred); 17911da177e4SLinus Torvalds 17921da177e4SLinus Torvalds sbsec = sb->s_security; 1793275bb41eSDavid Howells return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad); 17941da177e4SLinus Torvalds } 17951da177e4SLinus Torvalds 17961da177e4SLinus Torvalds /* Convert a Linux mode and permission mask to an access vector. */ 17971da177e4SLinus Torvalds static inline u32 file_mask_to_av(int mode, int mask) 17981da177e4SLinus Torvalds { 17991da177e4SLinus Torvalds u32 av = 0; 18001da177e4SLinus Torvalds 1801dba19c60SAl Viro if (!S_ISDIR(mode)) { 18021da177e4SLinus Torvalds if (mask & MAY_EXEC) 18031da177e4SLinus Torvalds av |= FILE__EXECUTE; 18041da177e4SLinus Torvalds if (mask & MAY_READ) 18051da177e4SLinus Torvalds av |= FILE__READ; 18061da177e4SLinus Torvalds 18071da177e4SLinus Torvalds if (mask & MAY_APPEND) 18081da177e4SLinus Torvalds av |= FILE__APPEND; 18091da177e4SLinus Torvalds else if (mask & MAY_WRITE) 18101da177e4SLinus Torvalds av |= FILE__WRITE; 18111da177e4SLinus Torvalds 18121da177e4SLinus Torvalds } else { 18131da177e4SLinus Torvalds if (mask & MAY_EXEC) 18141da177e4SLinus Torvalds av |= DIR__SEARCH; 18151da177e4SLinus Torvalds if (mask & MAY_WRITE) 18161da177e4SLinus Torvalds av |= DIR__WRITE; 18171da177e4SLinus Torvalds if (mask & MAY_READ) 18181da177e4SLinus Torvalds av |= DIR__READ; 18191da177e4SLinus Torvalds } 18201da177e4SLinus Torvalds 18211da177e4SLinus Torvalds return av; 18221da177e4SLinus Torvalds } 18231da177e4SLinus Torvalds 18241da177e4SLinus Torvalds /* Convert a Linux file to an access vector. */ 18251da177e4SLinus Torvalds static inline u32 file_to_av(struct file *file) 18261da177e4SLinus Torvalds { 18271da177e4SLinus Torvalds u32 av = 0; 18281da177e4SLinus Torvalds 18291da177e4SLinus Torvalds if (file->f_mode & FMODE_READ) 18301da177e4SLinus Torvalds av |= FILE__READ; 18311da177e4SLinus Torvalds if (file->f_mode & FMODE_WRITE) { 18321da177e4SLinus Torvalds if (file->f_flags & O_APPEND) 18331da177e4SLinus Torvalds av |= FILE__APPEND; 18341da177e4SLinus Torvalds else 18351da177e4SLinus Torvalds av |= FILE__WRITE; 18361da177e4SLinus Torvalds } 18370794c66dSStephen Smalley if (!av) { 18380794c66dSStephen Smalley /* 18390794c66dSStephen Smalley * Special file opened with flags 3 for ioctl-only use. 18400794c66dSStephen Smalley */ 18410794c66dSStephen Smalley av = FILE__IOCTL; 18420794c66dSStephen Smalley } 18431da177e4SLinus Torvalds 18441da177e4SLinus Torvalds return av; 18451da177e4SLinus Torvalds } 18461da177e4SLinus Torvalds 18478b6a5a37SEric Paris /* 18488b6a5a37SEric Paris * Convert a file to an access vector and include the correct open 18498b6a5a37SEric Paris * open permission. 18508b6a5a37SEric Paris */ 18518b6a5a37SEric Paris static inline u32 open_file_to_av(struct file *file) 18528b6a5a37SEric Paris { 18538b6a5a37SEric Paris u32 av = file_to_av(file); 18548b6a5a37SEric Paris 185549b7b8deSEric Paris if (selinux_policycap_openperm) 18568b6a5a37SEric Paris av |= FILE__OPEN; 185749b7b8deSEric Paris 18588b6a5a37SEric Paris return av; 18598b6a5a37SEric Paris } 18608b6a5a37SEric Paris 18611da177e4SLinus Torvalds /* Hook functions begin here. */ 18621da177e4SLinus Torvalds 18639e48858fSIngo Molnar static int selinux_ptrace_access_check(struct task_struct *child, 1864006ebb40SStephen Smalley unsigned int mode) 18651da177e4SLinus Torvalds { 18661da177e4SLinus Torvalds int rc; 18671da177e4SLinus Torvalds 18689e48858fSIngo Molnar rc = cap_ptrace_access_check(child, mode); 18691da177e4SLinus Torvalds if (rc) 18701da177e4SLinus Torvalds return rc; 18711da177e4SLinus Torvalds 187269f594a3SEric Paris if (mode & PTRACE_MODE_READ) { 1873275bb41eSDavid Howells u32 sid = current_sid(); 1874275bb41eSDavid Howells u32 csid = task_sid(child); 1875275bb41eSDavid Howells return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, NULL); 1876006ebb40SStephen Smalley } 1877006ebb40SStephen Smalley 18783b11a1deSDavid Howells return current_has_perm(child, PROCESS__PTRACE); 18795cd9c58fSDavid Howells } 18805cd9c58fSDavid Howells 18815cd9c58fSDavid Howells static int selinux_ptrace_traceme(struct task_struct *parent) 18825cd9c58fSDavid Howells { 18835cd9c58fSDavid Howells int rc; 18845cd9c58fSDavid Howells 1885200ac532SEric Paris rc = cap_ptrace_traceme(parent); 18865cd9c58fSDavid Howells if (rc) 18875cd9c58fSDavid Howells return rc; 18885cd9c58fSDavid Howells 18895cd9c58fSDavid Howells return task_has_perm(parent, current, PROCESS__PTRACE); 18901da177e4SLinus Torvalds } 18911da177e4SLinus Torvalds 18921da177e4SLinus Torvalds static int selinux_capget(struct task_struct *target, kernel_cap_t *effective, 18931da177e4SLinus Torvalds kernel_cap_t *inheritable, kernel_cap_t *permitted) 18941da177e4SLinus Torvalds { 18951da177e4SLinus Torvalds int error; 18961da177e4SLinus Torvalds 18973b11a1deSDavid Howells error = current_has_perm(target, PROCESS__GETCAP); 18981da177e4SLinus Torvalds if (error) 18991da177e4SLinus Torvalds return error; 19001da177e4SLinus Torvalds 1901200ac532SEric Paris return cap_capget(target, effective, inheritable, permitted); 19021da177e4SLinus Torvalds } 19031da177e4SLinus Torvalds 1904d84f4f99SDavid Howells static int selinux_capset(struct cred *new, const struct cred *old, 1905d84f4f99SDavid Howells const kernel_cap_t *effective, 190615a2460eSDavid Howells const kernel_cap_t *inheritable, 190715a2460eSDavid Howells const kernel_cap_t *permitted) 19081da177e4SLinus Torvalds { 19091da177e4SLinus Torvalds int error; 19101da177e4SLinus Torvalds 1911200ac532SEric Paris error = cap_capset(new, old, 1912d84f4f99SDavid Howells effective, inheritable, permitted); 19131da177e4SLinus Torvalds if (error) 19141da177e4SLinus Torvalds return error; 19151da177e4SLinus Torvalds 1916d84f4f99SDavid Howells return cred_has_perm(old, new, PROCESS__SETCAP); 19171da177e4SLinus Torvalds } 19181da177e4SLinus Torvalds 19195626d3e8SJames Morris /* 19205626d3e8SJames Morris * (This comment used to live with the selinux_task_setuid hook, 19215626d3e8SJames Morris * which was removed). 19225626d3e8SJames Morris * 19235626d3e8SJames Morris * Since setuid only affects the current process, and since the SELinux 19245626d3e8SJames Morris * controls are not based on the Linux identity attributes, SELinux does not 19255626d3e8SJames Morris * need to control this operation. However, SELinux does control the use of 19265626d3e8SJames Morris * the CAP_SETUID and CAP_SETGID capabilities using the capable hook. 19275626d3e8SJames Morris */ 19285626d3e8SJames Morris 19296a9de491SEric Paris static int selinux_capable(const struct cred *cred, struct user_namespace *ns, 19306a9de491SEric Paris int cap, int audit) 19311da177e4SLinus Torvalds { 19321da177e4SLinus Torvalds int rc; 19331da177e4SLinus Torvalds 19346a9de491SEric Paris rc = cap_capable(cred, ns, cap, audit); 19351da177e4SLinus Torvalds if (rc) 19361da177e4SLinus Torvalds return rc; 19371da177e4SLinus Torvalds 19386a9de491SEric Paris return cred_has_capability(cred, cap, audit); 19391da177e4SLinus Torvalds } 19401da177e4SLinus Torvalds 19411da177e4SLinus Torvalds static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb) 19421da177e4SLinus Torvalds { 194388e67f3bSDavid Howells const struct cred *cred = current_cred(); 19441da177e4SLinus Torvalds int rc = 0; 19451da177e4SLinus Torvalds 19461da177e4SLinus Torvalds if (!sb) 19471da177e4SLinus Torvalds return 0; 19481da177e4SLinus Torvalds 19491da177e4SLinus Torvalds switch (cmds) { 19501da177e4SLinus Torvalds case Q_SYNC: 19511da177e4SLinus Torvalds case Q_QUOTAON: 19521da177e4SLinus Torvalds case Q_QUOTAOFF: 19531da177e4SLinus Torvalds case Q_SETINFO: 19541da177e4SLinus Torvalds case Q_SETQUOTA: 195588e67f3bSDavid Howells rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL); 19561da177e4SLinus Torvalds break; 19571da177e4SLinus Torvalds case Q_GETFMT: 19581da177e4SLinus Torvalds case Q_GETINFO: 19591da177e4SLinus Torvalds case Q_GETQUOTA: 196088e67f3bSDavid Howells rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL); 19611da177e4SLinus Torvalds break; 19621da177e4SLinus Torvalds default: 19631da177e4SLinus Torvalds rc = 0; /* let the kernel handle invalid cmds */ 19641da177e4SLinus Torvalds break; 19651da177e4SLinus Torvalds } 19661da177e4SLinus Torvalds return rc; 19671da177e4SLinus Torvalds } 19681da177e4SLinus Torvalds 19691da177e4SLinus Torvalds static int selinux_quota_on(struct dentry *dentry) 19701da177e4SLinus Torvalds { 197188e67f3bSDavid Howells const struct cred *cred = current_cred(); 197288e67f3bSDavid Howells 19732875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__QUOTAON); 19741da177e4SLinus Torvalds } 19751da177e4SLinus Torvalds 197612b3052cSEric Paris static int selinux_syslog(int type) 19771da177e4SLinus Torvalds { 19781da177e4SLinus Torvalds int rc; 19791da177e4SLinus Torvalds 19801da177e4SLinus Torvalds switch (type) { 1981d78ca3cdSKees Cook case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */ 1982d78ca3cdSKees Cook case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */ 19831da177e4SLinus Torvalds rc = task_has_system(current, SYSTEM__SYSLOG_READ); 19841da177e4SLinus Torvalds break; 1985d78ca3cdSKees Cook case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */ 1986d78ca3cdSKees Cook case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */ 1987d78ca3cdSKees Cook /* Set level of messages printed to console */ 1988d78ca3cdSKees Cook case SYSLOG_ACTION_CONSOLE_LEVEL: 19891da177e4SLinus Torvalds rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE); 19901da177e4SLinus Torvalds break; 1991d78ca3cdSKees Cook case SYSLOG_ACTION_CLOSE: /* Close log */ 1992d78ca3cdSKees Cook case SYSLOG_ACTION_OPEN: /* Open log */ 1993d78ca3cdSKees Cook case SYSLOG_ACTION_READ: /* Read from log */ 1994d78ca3cdSKees Cook case SYSLOG_ACTION_READ_CLEAR: /* Read/clear last kernel messages */ 1995d78ca3cdSKees Cook case SYSLOG_ACTION_CLEAR: /* Clear ring buffer */ 19961da177e4SLinus Torvalds default: 19971da177e4SLinus Torvalds rc = task_has_system(current, SYSTEM__SYSLOG_MOD); 19981da177e4SLinus Torvalds break; 19991da177e4SLinus Torvalds } 20001da177e4SLinus Torvalds return rc; 20011da177e4SLinus Torvalds } 20021da177e4SLinus Torvalds 20031da177e4SLinus Torvalds /* 20041da177e4SLinus Torvalds * Check that a process has enough memory to allocate a new virtual 20051da177e4SLinus Torvalds * mapping. 0 means there is enough memory for the allocation to 20061da177e4SLinus Torvalds * succeed and -ENOMEM implies there is not. 20071da177e4SLinus Torvalds * 20081da177e4SLinus Torvalds * Do not audit the selinux permission check, as this is applied to all 20091da177e4SLinus Torvalds * processes that allocate mappings. 20101da177e4SLinus Torvalds */ 201134b4e4aaSAlan Cox static int selinux_vm_enough_memory(struct mm_struct *mm, long pages) 20121da177e4SLinus Torvalds { 20131da177e4SLinus Torvalds int rc, cap_sys_admin = 0; 20141da177e4SLinus Torvalds 20156a9de491SEric Paris rc = selinux_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN, 20163699c53cSDavid Howells SECURITY_CAP_NOAUDIT); 20171da177e4SLinus Torvalds if (rc == 0) 20181da177e4SLinus Torvalds cap_sys_admin = 1; 20191da177e4SLinus Torvalds 202034b4e4aaSAlan Cox return __vm_enough_memory(mm, pages, cap_sys_admin); 20211da177e4SLinus Torvalds } 20221da177e4SLinus Torvalds 20231da177e4SLinus Torvalds /* binprm security operations */ 20241da177e4SLinus Torvalds 2025a6f76f23SDavid Howells static int selinux_bprm_set_creds(struct linux_binprm *bprm) 20261da177e4SLinus Torvalds { 2027a6f76f23SDavid Howells const struct task_security_struct *old_tsec; 2028a6f76f23SDavid Howells struct task_security_struct *new_tsec; 20291da177e4SLinus Torvalds struct inode_security_struct *isec; 20302bf49690SThomas Liu struct common_audit_data ad; 2031496ad9aaSAl Viro struct inode *inode = file_inode(bprm->file); 20321da177e4SLinus Torvalds int rc; 20331da177e4SLinus Torvalds 2034200ac532SEric Paris rc = cap_bprm_set_creds(bprm); 20351da177e4SLinus Torvalds if (rc) 20361da177e4SLinus Torvalds return rc; 20371da177e4SLinus Torvalds 2038a6f76f23SDavid Howells /* SELinux context only depends on initial program or script and not 2039a6f76f23SDavid Howells * the script interpreter */ 2040a6f76f23SDavid Howells if (bprm->cred_prepared) 20411da177e4SLinus Torvalds return 0; 20421da177e4SLinus Torvalds 2043a6f76f23SDavid Howells old_tsec = current_security(); 2044a6f76f23SDavid Howells new_tsec = bprm->cred->security; 20451da177e4SLinus Torvalds isec = inode->i_security; 20461da177e4SLinus Torvalds 20471da177e4SLinus Torvalds /* Default to the current task SID. */ 2048a6f76f23SDavid Howells new_tsec->sid = old_tsec->sid; 2049a6f76f23SDavid Howells new_tsec->osid = old_tsec->sid; 20501da177e4SLinus Torvalds 205128eba5bfSMichael LeMay /* Reset fs, key, and sock SIDs on execve. */ 2052a6f76f23SDavid Howells new_tsec->create_sid = 0; 2053a6f76f23SDavid Howells new_tsec->keycreate_sid = 0; 2054a6f76f23SDavid Howells new_tsec->sockcreate_sid = 0; 20551da177e4SLinus Torvalds 2056a6f76f23SDavid Howells if (old_tsec->exec_sid) { 2057a6f76f23SDavid Howells new_tsec->sid = old_tsec->exec_sid; 20581da177e4SLinus Torvalds /* Reset exec SID on execve. */ 2059a6f76f23SDavid Howells new_tsec->exec_sid = 0; 2060259e5e6cSAndy Lutomirski 2061259e5e6cSAndy Lutomirski /* 2062259e5e6cSAndy Lutomirski * Minimize confusion: if no_new_privs and a transition is 2063259e5e6cSAndy Lutomirski * explicitly requested, then fail the exec. 2064259e5e6cSAndy Lutomirski */ 2065259e5e6cSAndy Lutomirski if (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS) 2066259e5e6cSAndy Lutomirski return -EPERM; 20671da177e4SLinus Torvalds } else { 20681da177e4SLinus Torvalds /* Check for a default transition on this program. */ 2069a6f76f23SDavid Howells rc = security_transition_sid(old_tsec->sid, isec->sid, 2070652bb9b0SEric Paris SECCLASS_PROCESS, NULL, 2071652bb9b0SEric Paris &new_tsec->sid); 20721da177e4SLinus Torvalds if (rc) 20731da177e4SLinus Torvalds return rc; 20741da177e4SLinus Torvalds } 20751da177e4SLinus Torvalds 207650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_PATH; 2077f48b7399SEric Paris ad.u.path = bprm->file->f_path; 20781da177e4SLinus Torvalds 2079259e5e6cSAndy Lutomirski if ((bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) || 2080259e5e6cSAndy Lutomirski (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS)) 2081a6f76f23SDavid Howells new_tsec->sid = old_tsec->sid; 20821da177e4SLinus Torvalds 2083a6f76f23SDavid Howells if (new_tsec->sid == old_tsec->sid) { 2084a6f76f23SDavid Howells rc = avc_has_perm(old_tsec->sid, isec->sid, 20851da177e4SLinus Torvalds SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad); 20861da177e4SLinus Torvalds if (rc) 20871da177e4SLinus Torvalds return rc; 20881da177e4SLinus Torvalds } else { 20891da177e4SLinus Torvalds /* Check permissions for the transition. */ 2090a6f76f23SDavid Howells rc = avc_has_perm(old_tsec->sid, new_tsec->sid, 20911da177e4SLinus Torvalds SECCLASS_PROCESS, PROCESS__TRANSITION, &ad); 20921da177e4SLinus Torvalds if (rc) 20931da177e4SLinus Torvalds return rc; 20941da177e4SLinus Torvalds 2095a6f76f23SDavid Howells rc = avc_has_perm(new_tsec->sid, isec->sid, 20961da177e4SLinus Torvalds SECCLASS_FILE, FILE__ENTRYPOINT, &ad); 20971da177e4SLinus Torvalds if (rc) 20981da177e4SLinus Torvalds return rc; 20991da177e4SLinus Torvalds 2100a6f76f23SDavid Howells /* Check for shared state */ 2101a6f76f23SDavid Howells if (bprm->unsafe & LSM_UNSAFE_SHARE) { 2102a6f76f23SDavid Howells rc = avc_has_perm(old_tsec->sid, new_tsec->sid, 2103a6f76f23SDavid Howells SECCLASS_PROCESS, PROCESS__SHARE, 2104a6f76f23SDavid Howells NULL); 2105a6f76f23SDavid Howells if (rc) 2106a6f76f23SDavid Howells return -EPERM; 21071da177e4SLinus Torvalds } 21081da177e4SLinus Torvalds 2109a6f76f23SDavid Howells /* Make sure that anyone attempting to ptrace over a task that 2110a6f76f23SDavid Howells * changes its SID has the appropriate permit */ 2111a6f76f23SDavid Howells if (bprm->unsafe & 2112a6f76f23SDavid Howells (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) { 2113a6f76f23SDavid Howells struct task_struct *tracer; 2114a6f76f23SDavid Howells struct task_security_struct *sec; 2115a6f76f23SDavid Howells u32 ptsid = 0; 2116a6f76f23SDavid Howells 2117a6f76f23SDavid Howells rcu_read_lock(); 211806d98473STejun Heo tracer = ptrace_parent(current); 2119a6f76f23SDavid Howells if (likely(tracer != NULL)) { 2120a6f76f23SDavid Howells sec = __task_cred(tracer)->security; 2121a6f76f23SDavid Howells ptsid = sec->sid; 2122a6f76f23SDavid Howells } 2123a6f76f23SDavid Howells rcu_read_unlock(); 2124a6f76f23SDavid Howells 2125a6f76f23SDavid Howells if (ptsid != 0) { 2126a6f76f23SDavid Howells rc = avc_has_perm(ptsid, new_tsec->sid, 2127a6f76f23SDavid Howells SECCLASS_PROCESS, 2128a6f76f23SDavid Howells PROCESS__PTRACE, NULL); 2129a6f76f23SDavid Howells if (rc) 2130a6f76f23SDavid Howells return -EPERM; 2131a6f76f23SDavid Howells } 2132a6f76f23SDavid Howells } 2133a6f76f23SDavid Howells 2134a6f76f23SDavid Howells /* Clear any possibly unsafe personality bits on exec: */ 2135a6f76f23SDavid Howells bprm->per_clear |= PER_CLEAR_ON_SETID; 2136a6f76f23SDavid Howells } 2137a6f76f23SDavid Howells 21381da177e4SLinus Torvalds return 0; 21391da177e4SLinus Torvalds } 21401da177e4SLinus Torvalds 21411da177e4SLinus Torvalds static int selinux_bprm_secureexec(struct linux_binprm *bprm) 21421da177e4SLinus Torvalds { 21435fb49870SPaul Moore const struct task_security_struct *tsec = current_security(); 2144275bb41eSDavid Howells u32 sid, osid; 21451da177e4SLinus Torvalds int atsecure = 0; 21461da177e4SLinus Torvalds 2147275bb41eSDavid Howells sid = tsec->sid; 2148275bb41eSDavid Howells osid = tsec->osid; 2149275bb41eSDavid Howells 2150275bb41eSDavid Howells if (osid != sid) { 21511da177e4SLinus Torvalds /* Enable secure mode for SIDs transitions unless 21521da177e4SLinus Torvalds the noatsecure permission is granted between 21531da177e4SLinus Torvalds the two SIDs, i.e. ahp returns 0. */ 2154275bb41eSDavid Howells atsecure = avc_has_perm(osid, sid, 21551da177e4SLinus Torvalds SECCLASS_PROCESS, 21561da177e4SLinus Torvalds PROCESS__NOATSECURE, NULL); 21571da177e4SLinus Torvalds } 21581da177e4SLinus Torvalds 2159200ac532SEric Paris return (atsecure || cap_bprm_secureexec(bprm)); 21601da177e4SLinus Torvalds } 21611da177e4SLinus Torvalds 2162c3c073f8SAl Viro static int match_file(const void *p, struct file *file, unsigned fd) 2163c3c073f8SAl Viro { 2164c3c073f8SAl Viro return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0; 2165c3c073f8SAl Viro } 2166c3c073f8SAl Viro 21671da177e4SLinus Torvalds /* Derived from fs/exec.c:flush_old_files. */ 2168745ca247SDavid Howells static inline void flush_unauthorized_files(const struct cred *cred, 2169745ca247SDavid Howells struct files_struct *files) 21701da177e4SLinus Torvalds { 21711da177e4SLinus Torvalds struct file *file, *devnull = NULL; 2172b20c8122SStephen Smalley struct tty_struct *tty; 217324ec839cSPeter Zijlstra int drop_tty = 0; 2174c3c073f8SAl Viro unsigned n; 21751da177e4SLinus Torvalds 217624ec839cSPeter Zijlstra tty = get_current_tty(); 21771da177e4SLinus Torvalds if (tty) { 2178ee2ffa0dSNick Piggin spin_lock(&tty_files_lock); 217937dd0bd0SEric Paris if (!list_empty(&tty->tty_files)) { 2180d996b62aSNick Piggin struct tty_file_private *file_priv; 218137dd0bd0SEric Paris 21821da177e4SLinus Torvalds /* Revalidate access to controlling tty. 2183602a8dd6SEric Paris Use path_has_perm on the tty path directly rather 21841da177e4SLinus Torvalds than using file_has_perm, as this particular open 21851da177e4SLinus Torvalds file may belong to another process and we are only 21861da177e4SLinus Torvalds interested in the inode-based check here. */ 2187d996b62aSNick Piggin file_priv = list_first_entry(&tty->tty_files, 2188d996b62aSNick Piggin struct tty_file_private, list); 2189d996b62aSNick Piggin file = file_priv->file; 2190602a8dd6SEric Paris if (path_has_perm(cred, &file->f_path, FILE__READ | FILE__WRITE)) 219124ec839cSPeter Zijlstra drop_tty = 1; 21921da177e4SLinus Torvalds } 2193ee2ffa0dSNick Piggin spin_unlock(&tty_files_lock); 2194452a00d2SAlan Cox tty_kref_put(tty); 21951da177e4SLinus Torvalds } 219698a27ba4SEric W. Biederman /* Reset controlling tty. */ 219798a27ba4SEric W. Biederman if (drop_tty) 219898a27ba4SEric W. Biederman no_tty(); 21991da177e4SLinus Torvalds 22001da177e4SLinus Torvalds /* Revalidate access to inherited open files. */ 2201c3c073f8SAl Viro n = iterate_fd(files, 0, match_file, cred); 2202c3c073f8SAl Viro if (!n) /* none found? */ 2203c3c073f8SAl Viro return; 22041da177e4SLinus Torvalds 2205c3c073f8SAl Viro devnull = dentry_open(&selinux_null, O_RDWR, cred); 220645525b26SAl Viro if (IS_ERR(devnull)) 220745525b26SAl Viro devnull = NULL; 2208c3c073f8SAl Viro /* replace all the matching ones with this */ 2209c3c073f8SAl Viro do { 221045525b26SAl Viro replace_fd(n - 1, devnull, 0); 2211c3c073f8SAl Viro } while ((n = iterate_fd(files, n, match_file, cred)) != 0); 221245525b26SAl Viro if (devnull) 2213c3c073f8SAl Viro fput(devnull); 22141da177e4SLinus Torvalds } 22151da177e4SLinus Torvalds 22161da177e4SLinus Torvalds /* 2217a6f76f23SDavid Howells * Prepare a process for imminent new credential changes due to exec 22181da177e4SLinus Torvalds */ 2219a6f76f23SDavid Howells static void selinux_bprm_committing_creds(struct linux_binprm *bprm) 22201da177e4SLinus Torvalds { 2221a6f76f23SDavid Howells struct task_security_struct *new_tsec; 22221da177e4SLinus Torvalds struct rlimit *rlim, *initrlim; 22231da177e4SLinus Torvalds int rc, i; 22241da177e4SLinus Torvalds 2225a6f76f23SDavid Howells new_tsec = bprm->cred->security; 2226a6f76f23SDavid Howells if (new_tsec->sid == new_tsec->osid) 22271da177e4SLinus Torvalds return; 22281da177e4SLinus Torvalds 22291da177e4SLinus Torvalds /* Close files for which the new task SID is not authorized. */ 2230a6f76f23SDavid Howells flush_unauthorized_files(bprm->cred, current->files); 22311da177e4SLinus Torvalds 2232a6f76f23SDavid Howells /* Always clear parent death signal on SID transitions. */ 2233a6f76f23SDavid Howells current->pdeath_signal = 0; 2234a6f76f23SDavid Howells 2235a6f76f23SDavid Howells /* Check whether the new SID can inherit resource limits from the old 2236a6f76f23SDavid Howells * SID. If not, reset all soft limits to the lower of the current 2237a6f76f23SDavid Howells * task's hard limit and the init task's soft limit. 2238a6f76f23SDavid Howells * 2239a6f76f23SDavid Howells * Note that the setting of hard limits (even to lower them) can be 2240a6f76f23SDavid Howells * controlled by the setrlimit check. The inclusion of the init task's 2241a6f76f23SDavid Howells * soft limit into the computation is to avoid resetting soft limits 2242a6f76f23SDavid Howells * higher than the default soft limit for cases where the default is 2243a6f76f23SDavid Howells * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK. 2244a6f76f23SDavid Howells */ 2245a6f76f23SDavid Howells rc = avc_has_perm(new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS, 2246a6f76f23SDavid Howells PROCESS__RLIMITINH, NULL); 2247a6f76f23SDavid Howells if (rc) { 2248eb2d55a3SOleg Nesterov /* protect against do_prlimit() */ 2249eb2d55a3SOleg Nesterov task_lock(current); 2250a6f76f23SDavid Howells for (i = 0; i < RLIM_NLIMITS; i++) { 2251a6f76f23SDavid Howells rlim = current->signal->rlim + i; 2252a6f76f23SDavid Howells initrlim = init_task.signal->rlim + i; 2253a6f76f23SDavid Howells rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur); 2254a6f76f23SDavid Howells } 2255eb2d55a3SOleg Nesterov task_unlock(current); 2256eb2d55a3SOleg Nesterov update_rlimit_cpu(current, rlimit(RLIMIT_CPU)); 2257a6f76f23SDavid Howells } 2258a6f76f23SDavid Howells } 2259a6f76f23SDavid Howells 2260a6f76f23SDavid Howells /* 2261a6f76f23SDavid Howells * Clean up the process immediately after the installation of new credentials 2262a6f76f23SDavid Howells * due to exec 2263a6f76f23SDavid Howells */ 2264a6f76f23SDavid Howells static void selinux_bprm_committed_creds(struct linux_binprm *bprm) 2265a6f76f23SDavid Howells { 2266a6f76f23SDavid Howells const struct task_security_struct *tsec = current_security(); 2267a6f76f23SDavid Howells struct itimerval itimer; 2268a6f76f23SDavid Howells u32 osid, sid; 2269a6f76f23SDavid Howells int rc, i; 2270a6f76f23SDavid Howells 2271a6f76f23SDavid Howells osid = tsec->osid; 2272a6f76f23SDavid Howells sid = tsec->sid; 2273a6f76f23SDavid Howells 2274a6f76f23SDavid Howells if (sid == osid) 2275a6f76f23SDavid Howells return; 2276a6f76f23SDavid Howells 2277a6f76f23SDavid Howells /* Check whether the new SID can inherit signal state from the old SID. 2278a6f76f23SDavid Howells * If not, clear itimers to avoid subsequent signal generation and 2279a6f76f23SDavid Howells * flush and unblock signals. 2280a6f76f23SDavid Howells * 2281a6f76f23SDavid Howells * This must occur _after_ the task SID has been updated so that any 2282a6f76f23SDavid Howells * kill done after the flush will be checked against the new SID. 2283a6f76f23SDavid Howells */ 2284a6f76f23SDavid Howells rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL); 22851da177e4SLinus Torvalds if (rc) { 22861da177e4SLinus Torvalds memset(&itimer, 0, sizeof itimer); 22871da177e4SLinus Torvalds for (i = 0; i < 3; i++) 22881da177e4SLinus Torvalds do_setitimer(i, &itimer, NULL); 22891da177e4SLinus Torvalds spin_lock_irq(¤t->sighand->siglock); 22903bcac026SDavid Howells if (!(current->signal->flags & SIGNAL_GROUP_EXIT)) { 22913bcac026SDavid Howells __flush_signals(current); 22921da177e4SLinus Torvalds flush_signal_handlers(current, 1); 22931da177e4SLinus Torvalds sigemptyset(¤t->blocked); 22943bcac026SDavid Howells } 22951da177e4SLinus Torvalds spin_unlock_irq(¤t->sighand->siglock); 22961da177e4SLinus Torvalds } 22971da177e4SLinus Torvalds 2298a6f76f23SDavid Howells /* Wake up the parent if it is waiting so that it can recheck 2299a6f76f23SDavid Howells * wait permission to the new task SID. */ 2300ecd6de3cSOleg Nesterov read_lock(&tasklist_lock); 23010b7570e7SOleg Nesterov __wake_up_parent(current, current->real_parent); 2302ecd6de3cSOleg Nesterov read_unlock(&tasklist_lock); 23031da177e4SLinus Torvalds } 23041da177e4SLinus Torvalds 23051da177e4SLinus Torvalds /* superblock security operations */ 23061da177e4SLinus Torvalds 23071da177e4SLinus Torvalds static int selinux_sb_alloc_security(struct super_block *sb) 23081da177e4SLinus Torvalds { 23091da177e4SLinus Torvalds return superblock_alloc_security(sb); 23101da177e4SLinus Torvalds } 23111da177e4SLinus Torvalds 23121da177e4SLinus Torvalds static void selinux_sb_free_security(struct super_block *sb) 23131da177e4SLinus Torvalds { 23141da177e4SLinus Torvalds superblock_free_security(sb); 23151da177e4SLinus Torvalds } 23161da177e4SLinus Torvalds 23171da177e4SLinus Torvalds static inline int match_prefix(char *prefix, int plen, char *option, int olen) 23181da177e4SLinus Torvalds { 23191da177e4SLinus Torvalds if (plen > olen) 23201da177e4SLinus Torvalds return 0; 23211da177e4SLinus Torvalds 23221da177e4SLinus Torvalds return !memcmp(prefix, option, plen); 23231da177e4SLinus Torvalds } 23241da177e4SLinus Torvalds 23251da177e4SLinus Torvalds static inline int selinux_option(char *option, int len) 23261da177e4SLinus Torvalds { 2327832cbd9aSEric Paris return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) || 2328832cbd9aSEric Paris match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) || 2329832cbd9aSEric Paris match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) || 233011689d47SDavid P. Quigley match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) || 233111689d47SDavid P. Quigley match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len)); 23321da177e4SLinus Torvalds } 23331da177e4SLinus Torvalds 23341da177e4SLinus Torvalds static inline void take_option(char **to, char *from, int *first, int len) 23351da177e4SLinus Torvalds { 23361da177e4SLinus Torvalds if (!*first) { 23371da177e4SLinus Torvalds **to = ','; 23381da177e4SLinus Torvalds *to += 1; 23393528a953SCory Olmo } else 23401da177e4SLinus Torvalds *first = 0; 23411da177e4SLinus Torvalds memcpy(*to, from, len); 23421da177e4SLinus Torvalds *to += len; 23431da177e4SLinus Torvalds } 23441da177e4SLinus Torvalds 23453528a953SCory Olmo static inline void take_selinux_option(char **to, char *from, int *first, 23463528a953SCory Olmo int len) 23473528a953SCory Olmo { 23483528a953SCory Olmo int current_size = 0; 23493528a953SCory Olmo 23503528a953SCory Olmo if (!*first) { 23513528a953SCory Olmo **to = '|'; 23523528a953SCory Olmo *to += 1; 2353828dfe1dSEric Paris } else 23543528a953SCory Olmo *first = 0; 23553528a953SCory Olmo 23563528a953SCory Olmo while (current_size < len) { 23573528a953SCory Olmo if (*from != '"') { 23583528a953SCory Olmo **to = *from; 23593528a953SCory Olmo *to += 1; 23603528a953SCory Olmo } 23613528a953SCory Olmo from += 1; 23623528a953SCory Olmo current_size += 1; 23633528a953SCory Olmo } 23643528a953SCory Olmo } 23653528a953SCory Olmo 2366e0007529SEric Paris static int selinux_sb_copy_data(char *orig, char *copy) 23671da177e4SLinus Torvalds { 23681da177e4SLinus Torvalds int fnosec, fsec, rc = 0; 23691da177e4SLinus Torvalds char *in_save, *in_curr, *in_end; 23701da177e4SLinus Torvalds char *sec_curr, *nosec_save, *nosec; 23713528a953SCory Olmo int open_quote = 0; 23721da177e4SLinus Torvalds 23731da177e4SLinus Torvalds in_curr = orig; 23741da177e4SLinus Torvalds sec_curr = copy; 23751da177e4SLinus Torvalds 23761da177e4SLinus Torvalds nosec = (char *)get_zeroed_page(GFP_KERNEL); 23771da177e4SLinus Torvalds if (!nosec) { 23781da177e4SLinus Torvalds rc = -ENOMEM; 23791da177e4SLinus Torvalds goto out; 23801da177e4SLinus Torvalds } 23811da177e4SLinus Torvalds 23821da177e4SLinus Torvalds nosec_save = nosec; 23831da177e4SLinus Torvalds fnosec = fsec = 1; 23841da177e4SLinus Torvalds in_save = in_end = orig; 23851da177e4SLinus Torvalds 23861da177e4SLinus Torvalds do { 23873528a953SCory Olmo if (*in_end == '"') 23883528a953SCory Olmo open_quote = !open_quote; 23893528a953SCory Olmo if ((*in_end == ',' && open_quote == 0) || 23903528a953SCory Olmo *in_end == '\0') { 23911da177e4SLinus Torvalds int len = in_end - in_curr; 23921da177e4SLinus Torvalds 23931da177e4SLinus Torvalds if (selinux_option(in_curr, len)) 23943528a953SCory Olmo take_selinux_option(&sec_curr, in_curr, &fsec, len); 23951da177e4SLinus Torvalds else 23961da177e4SLinus Torvalds take_option(&nosec, in_curr, &fnosec, len); 23971da177e4SLinus Torvalds 23981da177e4SLinus Torvalds in_curr = in_end + 1; 23991da177e4SLinus Torvalds } 24001da177e4SLinus Torvalds } while (*in_end++); 24011da177e4SLinus Torvalds 24026931dfc9SEric Paris strcpy(in_save, nosec_save); 2403da3caa20SGerald Schaefer free_page((unsigned long)nosec_save); 24041da177e4SLinus Torvalds out: 24051da177e4SLinus Torvalds return rc; 24061da177e4SLinus Torvalds } 24071da177e4SLinus Torvalds 2408026eb167SEric Paris static int selinux_sb_remount(struct super_block *sb, void *data) 2409026eb167SEric Paris { 2410026eb167SEric Paris int rc, i, *flags; 2411026eb167SEric Paris struct security_mnt_opts opts; 2412026eb167SEric Paris char *secdata, **mount_options; 2413026eb167SEric Paris struct superblock_security_struct *sbsec = sb->s_security; 2414026eb167SEric Paris 2415026eb167SEric Paris if (!(sbsec->flags & SE_SBINITIALIZED)) 2416026eb167SEric Paris return 0; 2417026eb167SEric Paris 2418026eb167SEric Paris if (!data) 2419026eb167SEric Paris return 0; 2420026eb167SEric Paris 2421026eb167SEric Paris if (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA) 2422026eb167SEric Paris return 0; 2423026eb167SEric Paris 2424026eb167SEric Paris security_init_mnt_opts(&opts); 2425026eb167SEric Paris secdata = alloc_secdata(); 2426026eb167SEric Paris if (!secdata) 2427026eb167SEric Paris return -ENOMEM; 2428026eb167SEric Paris rc = selinux_sb_copy_data(data, secdata); 2429026eb167SEric Paris if (rc) 2430026eb167SEric Paris goto out_free_secdata; 2431026eb167SEric Paris 2432026eb167SEric Paris rc = selinux_parse_opts_str(secdata, &opts); 2433026eb167SEric Paris if (rc) 2434026eb167SEric Paris goto out_free_secdata; 2435026eb167SEric Paris 2436026eb167SEric Paris mount_options = opts.mnt_opts; 2437026eb167SEric Paris flags = opts.mnt_opts_flags; 2438026eb167SEric Paris 2439026eb167SEric Paris for (i = 0; i < opts.num_mnt_opts; i++) { 2440026eb167SEric Paris u32 sid; 2441026eb167SEric Paris size_t len; 2442026eb167SEric Paris 244312f348b9SEric Paris if (flags[i] == SBLABEL_MNT) 2444026eb167SEric Paris continue; 2445026eb167SEric Paris len = strlen(mount_options[i]); 2446026eb167SEric Paris rc = security_context_to_sid(mount_options[i], len, &sid); 2447026eb167SEric Paris if (rc) { 2448026eb167SEric Paris printk(KERN_WARNING "SELinux: security_context_to_sid" 2449102aefddSAnand Avati "(%s) failed for (dev %s, type "SB_TYPE_FMT") errno=%d\n", 2450102aefddSAnand Avati mount_options[i], sb->s_id, SB_TYPE_ARGS(sb), rc); 2451026eb167SEric Paris goto out_free_opts; 2452026eb167SEric Paris } 2453026eb167SEric Paris rc = -EINVAL; 2454026eb167SEric Paris switch (flags[i]) { 2455026eb167SEric Paris case FSCONTEXT_MNT: 2456026eb167SEric Paris if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid)) 2457026eb167SEric Paris goto out_bad_option; 2458026eb167SEric Paris break; 2459026eb167SEric Paris case CONTEXT_MNT: 2460026eb167SEric Paris if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid)) 2461026eb167SEric Paris goto out_bad_option; 2462026eb167SEric Paris break; 2463026eb167SEric Paris case ROOTCONTEXT_MNT: { 2464026eb167SEric Paris struct inode_security_struct *root_isec; 2465026eb167SEric Paris root_isec = sb->s_root->d_inode->i_security; 2466026eb167SEric Paris 2467026eb167SEric Paris if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid)) 2468026eb167SEric Paris goto out_bad_option; 2469026eb167SEric Paris break; 2470026eb167SEric Paris } 2471026eb167SEric Paris case DEFCONTEXT_MNT: 2472026eb167SEric Paris if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid)) 2473026eb167SEric Paris goto out_bad_option; 2474026eb167SEric Paris break; 2475026eb167SEric Paris default: 2476026eb167SEric Paris goto out_free_opts; 2477026eb167SEric Paris } 2478026eb167SEric Paris } 2479026eb167SEric Paris 2480026eb167SEric Paris rc = 0; 2481026eb167SEric Paris out_free_opts: 2482026eb167SEric Paris security_free_mnt_opts(&opts); 2483026eb167SEric Paris out_free_secdata: 2484026eb167SEric Paris free_secdata(secdata); 2485026eb167SEric Paris return rc; 2486026eb167SEric Paris out_bad_option: 2487026eb167SEric Paris printk(KERN_WARNING "SELinux: unable to change security options " 2488102aefddSAnand Avati "during remount (dev %s, type "SB_TYPE_FMT")\n", sb->s_id, 2489102aefddSAnand Avati SB_TYPE_ARGS(sb)); 2490026eb167SEric Paris goto out_free_opts; 2491026eb167SEric Paris } 2492026eb167SEric Paris 249312204e24SJames Morris static int selinux_sb_kern_mount(struct super_block *sb, int flags, void *data) 24941da177e4SLinus Torvalds { 249588e67f3bSDavid Howells const struct cred *cred = current_cred(); 24962bf49690SThomas Liu struct common_audit_data ad; 24971da177e4SLinus Torvalds int rc; 24981da177e4SLinus Torvalds 24991da177e4SLinus Torvalds rc = superblock_doinit(sb, data); 25001da177e4SLinus Torvalds if (rc) 25011da177e4SLinus Torvalds return rc; 25021da177e4SLinus Torvalds 250374192246SJames Morris /* Allow all mounts performed by the kernel */ 250474192246SJames Morris if (flags & MS_KERNMOUNT) 250574192246SJames Morris return 0; 250674192246SJames Morris 250750c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 2508a269434dSEric Paris ad.u.dentry = sb->s_root; 250988e67f3bSDavid Howells return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad); 25101da177e4SLinus Torvalds } 25111da177e4SLinus Torvalds 2512726c3342SDavid Howells static int selinux_sb_statfs(struct dentry *dentry) 25131da177e4SLinus Torvalds { 251488e67f3bSDavid Howells const struct cred *cred = current_cred(); 25152bf49690SThomas Liu struct common_audit_data ad; 25161da177e4SLinus Torvalds 251750c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 2518a269434dSEric Paris ad.u.dentry = dentry->d_sb->s_root; 251988e67f3bSDavid Howells return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad); 25201da177e4SLinus Torvalds } 25211da177e4SLinus Torvalds 2522808d4e3cSAl Viro static int selinux_mount(const char *dev_name, 2523b5266eb4SAl Viro struct path *path, 2524808d4e3cSAl Viro const char *type, 25251da177e4SLinus Torvalds unsigned long flags, 25261da177e4SLinus Torvalds void *data) 25271da177e4SLinus Torvalds { 252888e67f3bSDavid Howells const struct cred *cred = current_cred(); 25291da177e4SLinus Torvalds 25301da177e4SLinus Torvalds if (flags & MS_REMOUNT) 2531d8c9584eSAl Viro return superblock_has_perm(cred, path->dentry->d_sb, 25321da177e4SLinus Torvalds FILESYSTEM__REMOUNT, NULL); 25331da177e4SLinus Torvalds else 25342875fa00SEric Paris return path_has_perm(cred, path, FILE__MOUNTON); 25351da177e4SLinus Torvalds } 25361da177e4SLinus Torvalds 25371da177e4SLinus Torvalds static int selinux_umount(struct vfsmount *mnt, int flags) 25381da177e4SLinus Torvalds { 253988e67f3bSDavid Howells const struct cred *cred = current_cred(); 25401da177e4SLinus Torvalds 254188e67f3bSDavid Howells return superblock_has_perm(cred, mnt->mnt_sb, 25421da177e4SLinus Torvalds FILESYSTEM__UNMOUNT, NULL); 25431da177e4SLinus Torvalds } 25441da177e4SLinus Torvalds 25451da177e4SLinus Torvalds /* inode security operations */ 25461da177e4SLinus Torvalds 25471da177e4SLinus Torvalds static int selinux_inode_alloc_security(struct inode *inode) 25481da177e4SLinus Torvalds { 25491da177e4SLinus Torvalds return inode_alloc_security(inode); 25501da177e4SLinus Torvalds } 25511da177e4SLinus Torvalds 25521da177e4SLinus Torvalds static void selinux_inode_free_security(struct inode *inode) 25531da177e4SLinus Torvalds { 25541da177e4SLinus Torvalds inode_free_security(inode); 25551da177e4SLinus Torvalds } 25561da177e4SLinus Torvalds 25575e41ff9eSStephen Smalley static int selinux_inode_init_security(struct inode *inode, struct inode *dir, 25582a7dba39SEric Paris const struct qstr *qstr, char **name, 25592a7dba39SEric Paris void **value, size_t *len) 25605e41ff9eSStephen Smalley { 25615fb49870SPaul Moore const struct task_security_struct *tsec = current_security(); 25625e41ff9eSStephen Smalley struct inode_security_struct *dsec; 25635e41ff9eSStephen Smalley struct superblock_security_struct *sbsec; 2564275bb41eSDavid Howells u32 sid, newsid, clen; 25655e41ff9eSStephen Smalley int rc; 2566570bc1c2SStephen Smalley char *namep = NULL, *context; 25675e41ff9eSStephen Smalley 25685e41ff9eSStephen Smalley dsec = dir->i_security; 25695e41ff9eSStephen Smalley sbsec = dir->i_sb->s_security; 25705e41ff9eSStephen Smalley 2571275bb41eSDavid Howells sid = tsec->sid; 25725e41ff9eSStephen Smalley newsid = tsec->create_sid; 2573275bb41eSDavid Howells 2574415103f9SEric Paris if ((sbsec->flags & SE_SBINITIALIZED) && 2575415103f9SEric Paris (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) 2576415103f9SEric Paris newsid = sbsec->mntpoint_sid; 257712f348b9SEric Paris else if (!newsid || !(sbsec->flags & SBLABEL_MNT)) { 2578275bb41eSDavid Howells rc = security_transition_sid(sid, dsec->sid, 25795e41ff9eSStephen Smalley inode_mode_to_security_class(inode->i_mode), 2580652bb9b0SEric Paris qstr, &newsid); 25815e41ff9eSStephen Smalley if (rc) { 25825e41ff9eSStephen Smalley printk(KERN_WARNING "%s: " 25835e41ff9eSStephen Smalley "security_transition_sid failed, rc=%d (dev=%s " 25845e41ff9eSStephen Smalley "ino=%ld)\n", 2585dd6f953aSHarvey Harrison __func__, 25865e41ff9eSStephen Smalley -rc, inode->i_sb->s_id, inode->i_ino); 25875e41ff9eSStephen Smalley return rc; 25885e41ff9eSStephen Smalley } 25895e41ff9eSStephen Smalley } 25905e41ff9eSStephen Smalley 2591296fddf7SEric Paris /* Possibly defer initialization to selinux_complete_init. */ 25920d90a7ecSDavid P. Quigley if (sbsec->flags & SE_SBINITIALIZED) { 2593296fddf7SEric Paris struct inode_security_struct *isec = inode->i_security; 2594296fddf7SEric Paris isec->sclass = inode_mode_to_security_class(inode->i_mode); 2595296fddf7SEric Paris isec->sid = newsid; 2596296fddf7SEric Paris isec->initialized = 1; 2597296fddf7SEric Paris } 25985e41ff9eSStephen Smalley 259912f348b9SEric Paris if (!ss_initialized || !(sbsec->flags & SBLABEL_MNT)) 260025a74f3bSStephen Smalley return -EOPNOTSUPP; 260125a74f3bSStephen Smalley 2602570bc1c2SStephen Smalley if (name) { 2603a02fe132SJosef Bacik namep = kstrdup(XATTR_SELINUX_SUFFIX, GFP_NOFS); 26045e41ff9eSStephen Smalley if (!namep) 26055e41ff9eSStephen Smalley return -ENOMEM; 26065e41ff9eSStephen Smalley *name = namep; 2607570bc1c2SStephen Smalley } 26085e41ff9eSStephen Smalley 2609570bc1c2SStephen Smalley if (value && len) { 261012b29f34SStephen Smalley rc = security_sid_to_context_force(newsid, &context, &clen); 26115e41ff9eSStephen Smalley if (rc) { 26125e41ff9eSStephen Smalley kfree(namep); 26135e41ff9eSStephen Smalley return rc; 26145e41ff9eSStephen Smalley } 26155e41ff9eSStephen Smalley *value = context; 2616570bc1c2SStephen Smalley *len = clen; 2617570bc1c2SStephen Smalley } 26185e41ff9eSStephen Smalley 26195e41ff9eSStephen Smalley return 0; 26205e41ff9eSStephen Smalley } 26215e41ff9eSStephen Smalley 26224acdaf27SAl Viro static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode) 26231da177e4SLinus Torvalds { 26241da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_FILE); 26251da177e4SLinus Torvalds } 26261da177e4SLinus Torvalds 26271da177e4SLinus Torvalds static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 26281da177e4SLinus Torvalds { 26291da177e4SLinus Torvalds return may_link(dir, old_dentry, MAY_LINK); 26301da177e4SLinus Torvalds } 26311da177e4SLinus Torvalds 26321da177e4SLinus Torvalds static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry) 26331da177e4SLinus Torvalds { 26341da177e4SLinus Torvalds return may_link(dir, dentry, MAY_UNLINK); 26351da177e4SLinus Torvalds } 26361da177e4SLinus Torvalds 26371da177e4SLinus Torvalds static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name) 26381da177e4SLinus Torvalds { 26391da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_LNK_FILE); 26401da177e4SLinus Torvalds } 26411da177e4SLinus Torvalds 264218bb1db3SAl Viro static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask) 26431da177e4SLinus Torvalds { 26441da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_DIR); 26451da177e4SLinus Torvalds } 26461da177e4SLinus Torvalds 26471da177e4SLinus Torvalds static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry) 26481da177e4SLinus Torvalds { 26491da177e4SLinus Torvalds return may_link(dir, dentry, MAY_RMDIR); 26501da177e4SLinus Torvalds } 26511da177e4SLinus Torvalds 26521a67aafbSAl Viro static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) 26531da177e4SLinus Torvalds { 26541da177e4SLinus Torvalds return may_create(dir, dentry, inode_mode_to_security_class(mode)); 26551da177e4SLinus Torvalds } 26561da177e4SLinus Torvalds 26571da177e4SLinus Torvalds static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry, 26581da177e4SLinus Torvalds struct inode *new_inode, struct dentry *new_dentry) 26591da177e4SLinus Torvalds { 26601da177e4SLinus Torvalds return may_rename(old_inode, old_dentry, new_inode, new_dentry); 26611da177e4SLinus Torvalds } 26621da177e4SLinus Torvalds 26631da177e4SLinus Torvalds static int selinux_inode_readlink(struct dentry *dentry) 26641da177e4SLinus Torvalds { 266588e67f3bSDavid Howells const struct cred *cred = current_cred(); 266688e67f3bSDavid Howells 26672875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__READ); 26681da177e4SLinus Torvalds } 26691da177e4SLinus Torvalds 26701da177e4SLinus Torvalds static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata) 26711da177e4SLinus Torvalds { 267288e67f3bSDavid Howells const struct cred *cred = current_cred(); 26731da177e4SLinus Torvalds 26742875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__READ); 26751da177e4SLinus Torvalds } 26761da177e4SLinus Torvalds 2677d4cf970dSEric Paris static noinline int audit_inode_permission(struct inode *inode, 2678d4cf970dSEric Paris u32 perms, u32 audited, u32 denied, 2679d4cf970dSEric Paris unsigned flags) 2680d4cf970dSEric Paris { 2681d4cf970dSEric Paris struct common_audit_data ad; 2682d4cf970dSEric Paris struct inode_security_struct *isec = inode->i_security; 2683d4cf970dSEric Paris int rc; 2684d4cf970dSEric Paris 268550c205f5SEric Paris ad.type = LSM_AUDIT_DATA_INODE; 2686d4cf970dSEric Paris ad.u.inode = inode; 2687d4cf970dSEric Paris 2688d4cf970dSEric Paris rc = slow_avc_audit(current_sid(), isec->sid, isec->sclass, perms, 2689d4cf970dSEric Paris audited, denied, &ad, flags); 2690d4cf970dSEric Paris if (rc) 2691d4cf970dSEric Paris return rc; 2692d4cf970dSEric Paris return 0; 2693d4cf970dSEric Paris } 2694d4cf970dSEric Paris 2695e74f71ebSAl Viro static int selinux_inode_permission(struct inode *inode, int mask) 26961da177e4SLinus Torvalds { 269788e67f3bSDavid Howells const struct cred *cred = current_cred(); 2698b782e0a6SEric Paris u32 perms; 2699b782e0a6SEric Paris bool from_access; 2700cf1dd1daSAl Viro unsigned flags = mask & MAY_NOT_BLOCK; 27012e334057SEric Paris struct inode_security_struct *isec; 27022e334057SEric Paris u32 sid; 27032e334057SEric Paris struct av_decision avd; 27042e334057SEric Paris int rc, rc2; 27052e334057SEric Paris u32 audited, denied; 27061da177e4SLinus Torvalds 2707b782e0a6SEric Paris from_access = mask & MAY_ACCESS; 2708d09ca739SEric Paris mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND); 2709d09ca739SEric Paris 27101da177e4SLinus Torvalds /* No permission to check. Existence test. */ 2711b782e0a6SEric Paris if (!mask) 27121da177e4SLinus Torvalds return 0; 27131da177e4SLinus Torvalds 27142e334057SEric Paris validate_creds(cred); 2715b782e0a6SEric Paris 27162e334057SEric Paris if (unlikely(IS_PRIVATE(inode))) 27172e334057SEric Paris return 0; 2718b782e0a6SEric Paris 2719b782e0a6SEric Paris perms = file_mask_to_av(inode->i_mode, mask); 2720b782e0a6SEric Paris 27212e334057SEric Paris sid = cred_sid(cred); 27222e334057SEric Paris isec = inode->i_security; 27232e334057SEric Paris 27242e334057SEric Paris rc = avc_has_perm_noaudit(sid, isec->sid, isec->sclass, perms, 0, &avd); 27252e334057SEric Paris audited = avc_audit_required(perms, &avd, rc, 27262e334057SEric Paris from_access ? FILE__AUDIT_ACCESS : 0, 27272e334057SEric Paris &denied); 27282e334057SEric Paris if (likely(!audited)) 27292e334057SEric Paris return rc; 27302e334057SEric Paris 2731d4cf970dSEric Paris rc2 = audit_inode_permission(inode, perms, audited, denied, flags); 27322e334057SEric Paris if (rc2) 27332e334057SEric Paris return rc2; 27342e334057SEric Paris return rc; 27351da177e4SLinus Torvalds } 27361da177e4SLinus Torvalds 27371da177e4SLinus Torvalds static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr) 27381da177e4SLinus Torvalds { 273988e67f3bSDavid Howells const struct cred *cred = current_cred(); 2740bc6a6008SAmerigo Wang unsigned int ia_valid = iattr->ia_valid; 274195dbf739SEric Paris __u32 av = FILE__WRITE; 27421da177e4SLinus Torvalds 2743bc6a6008SAmerigo Wang /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */ 2744bc6a6008SAmerigo Wang if (ia_valid & ATTR_FORCE) { 2745bc6a6008SAmerigo Wang ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE | 2746bc6a6008SAmerigo Wang ATTR_FORCE); 2747bc6a6008SAmerigo Wang if (!ia_valid) 27481da177e4SLinus Torvalds return 0; 2749bc6a6008SAmerigo Wang } 27501da177e4SLinus Torvalds 2751bc6a6008SAmerigo Wang if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | 2752bc6a6008SAmerigo Wang ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET)) 27532875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__SETATTR); 27541da177e4SLinus Torvalds 27553d2195c3SEric Paris if (selinux_policycap_openperm && (ia_valid & ATTR_SIZE)) 275695dbf739SEric Paris av |= FILE__OPEN; 275795dbf739SEric Paris 275895dbf739SEric Paris return dentry_has_perm(cred, dentry, av); 27591da177e4SLinus Torvalds } 27601da177e4SLinus Torvalds 27611da177e4SLinus Torvalds static int selinux_inode_getattr(struct vfsmount *mnt, struct dentry *dentry) 27621da177e4SLinus Torvalds { 276388e67f3bSDavid Howells const struct cred *cred = current_cred(); 27642875fa00SEric Paris struct path path; 276588e67f3bSDavid Howells 27662875fa00SEric Paris path.dentry = dentry; 27672875fa00SEric Paris path.mnt = mnt; 27682875fa00SEric Paris 27692875fa00SEric Paris return path_has_perm(cred, &path, FILE__GETATTR); 27701da177e4SLinus Torvalds } 27711da177e4SLinus Torvalds 27728f0cfa52SDavid Howells static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name) 2773b5376771SSerge E. Hallyn { 277488e67f3bSDavid Howells const struct cred *cred = current_cred(); 277588e67f3bSDavid Howells 2776b5376771SSerge E. Hallyn if (!strncmp(name, XATTR_SECURITY_PREFIX, 2777b5376771SSerge E. Hallyn sizeof XATTR_SECURITY_PREFIX - 1)) { 2778b5376771SSerge E. Hallyn if (!strcmp(name, XATTR_NAME_CAPS)) { 2779b5376771SSerge E. Hallyn if (!capable(CAP_SETFCAP)) 2780b5376771SSerge E. Hallyn return -EPERM; 2781b5376771SSerge E. Hallyn } else if (!capable(CAP_SYS_ADMIN)) { 2782b5376771SSerge E. Hallyn /* A different attribute in the security namespace. 2783b5376771SSerge E. Hallyn Restrict to administrator. */ 2784b5376771SSerge E. Hallyn return -EPERM; 2785b5376771SSerge E. Hallyn } 2786b5376771SSerge E. Hallyn } 2787b5376771SSerge E. Hallyn 2788b5376771SSerge E. Hallyn /* Not an attribute we recognize, so just check the 2789b5376771SSerge E. Hallyn ordinary setattr permission. */ 27902875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__SETATTR); 2791b5376771SSerge E. Hallyn } 2792b5376771SSerge E. Hallyn 27938f0cfa52SDavid Howells static int selinux_inode_setxattr(struct dentry *dentry, const char *name, 27948f0cfa52SDavid Howells const void *value, size_t size, int flags) 27951da177e4SLinus Torvalds { 27961da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 27971da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 27981da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 27992bf49690SThomas Liu struct common_audit_data ad; 2800275bb41eSDavid Howells u32 newsid, sid = current_sid(); 28011da177e4SLinus Torvalds int rc = 0; 28021da177e4SLinus Torvalds 2803b5376771SSerge E. Hallyn if (strcmp(name, XATTR_NAME_SELINUX)) 2804b5376771SSerge E. Hallyn return selinux_inode_setotherxattr(dentry, name); 28051da177e4SLinus Torvalds 28061da177e4SLinus Torvalds sbsec = inode->i_sb->s_security; 280712f348b9SEric Paris if (!(sbsec->flags & SBLABEL_MNT)) 28081da177e4SLinus Torvalds return -EOPNOTSUPP; 28091da177e4SLinus Torvalds 28102e149670SSerge E. Hallyn if (!inode_owner_or_capable(inode)) 28111da177e4SLinus Torvalds return -EPERM; 28121da177e4SLinus Torvalds 281350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_DENTRY; 2814a269434dSEric Paris ad.u.dentry = dentry; 28151da177e4SLinus Torvalds 2816275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, isec->sclass, 28171da177e4SLinus Torvalds FILE__RELABELFROM, &ad); 28181da177e4SLinus Torvalds if (rc) 28191da177e4SLinus Torvalds return rc; 28201da177e4SLinus Torvalds 28211da177e4SLinus Torvalds rc = security_context_to_sid(value, size, &newsid); 282212b29f34SStephen Smalley if (rc == -EINVAL) { 2823d6ea83ecSEric Paris if (!capable(CAP_MAC_ADMIN)) { 2824d6ea83ecSEric Paris struct audit_buffer *ab; 2825d6ea83ecSEric Paris size_t audit_size; 2826d6ea83ecSEric Paris const char *str; 2827d6ea83ecSEric Paris 2828d6ea83ecSEric Paris /* We strip a nul only if it is at the end, otherwise the 2829d6ea83ecSEric Paris * context contains a nul and we should audit that */ 2830e3fea3f7SAl Viro if (value) { 2831d6ea83ecSEric Paris str = value; 2832d6ea83ecSEric Paris if (str[size - 1] == '\0') 2833d6ea83ecSEric Paris audit_size = size - 1; 2834d6ea83ecSEric Paris else 2835d6ea83ecSEric Paris audit_size = size; 2836e3fea3f7SAl Viro } else { 2837e3fea3f7SAl Viro str = ""; 2838e3fea3f7SAl Viro audit_size = 0; 2839e3fea3f7SAl Viro } 2840d6ea83ecSEric Paris ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR); 2841d6ea83ecSEric Paris audit_log_format(ab, "op=setxattr invalid_context="); 2842d6ea83ecSEric Paris audit_log_n_untrustedstring(ab, value, audit_size); 2843d6ea83ecSEric Paris audit_log_end(ab); 2844d6ea83ecSEric Paris 284512b29f34SStephen Smalley return rc; 2846d6ea83ecSEric Paris } 284712b29f34SStephen Smalley rc = security_context_to_sid_force(value, size, &newsid); 284812b29f34SStephen Smalley } 28491da177e4SLinus Torvalds if (rc) 28501da177e4SLinus Torvalds return rc; 28511da177e4SLinus Torvalds 2852275bb41eSDavid Howells rc = avc_has_perm(sid, newsid, isec->sclass, 28531da177e4SLinus Torvalds FILE__RELABELTO, &ad); 28541da177e4SLinus Torvalds if (rc) 28551da177e4SLinus Torvalds return rc; 28561da177e4SLinus Torvalds 2857275bb41eSDavid Howells rc = security_validate_transition(isec->sid, newsid, sid, 28581da177e4SLinus Torvalds isec->sclass); 28591da177e4SLinus Torvalds if (rc) 28601da177e4SLinus Torvalds return rc; 28611da177e4SLinus Torvalds 28621da177e4SLinus Torvalds return avc_has_perm(newsid, 28631da177e4SLinus Torvalds sbsec->sid, 28641da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 28651da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, 28661da177e4SLinus Torvalds &ad); 28671da177e4SLinus Torvalds } 28681da177e4SLinus Torvalds 28698f0cfa52SDavid Howells static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name, 28708f0cfa52SDavid Howells const void *value, size_t size, 28718f0cfa52SDavid Howells int flags) 28721da177e4SLinus Torvalds { 28731da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 28741da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 28751da177e4SLinus Torvalds u32 newsid; 28761da177e4SLinus Torvalds int rc; 28771da177e4SLinus Torvalds 28781da177e4SLinus Torvalds if (strcmp(name, XATTR_NAME_SELINUX)) { 28791da177e4SLinus Torvalds /* Not an attribute we recognize, so nothing to do. */ 28801da177e4SLinus Torvalds return; 28811da177e4SLinus Torvalds } 28821da177e4SLinus Torvalds 288312b29f34SStephen Smalley rc = security_context_to_sid_force(value, size, &newsid); 28841da177e4SLinus Torvalds if (rc) { 288512b29f34SStephen Smalley printk(KERN_ERR "SELinux: unable to map context to SID" 288612b29f34SStephen Smalley "for (%s, %lu), rc=%d\n", 288712b29f34SStephen Smalley inode->i_sb->s_id, inode->i_ino, -rc); 28881da177e4SLinus Torvalds return; 28891da177e4SLinus Torvalds } 28901da177e4SLinus Torvalds 28911da177e4SLinus Torvalds isec->sid = newsid; 28921da177e4SLinus Torvalds return; 28931da177e4SLinus Torvalds } 28941da177e4SLinus Torvalds 28958f0cfa52SDavid Howells static int selinux_inode_getxattr(struct dentry *dentry, const char *name) 28961da177e4SLinus Torvalds { 289788e67f3bSDavid Howells const struct cred *cred = current_cred(); 289888e67f3bSDavid Howells 28992875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__GETATTR); 29001da177e4SLinus Torvalds } 29011da177e4SLinus Torvalds 29021da177e4SLinus Torvalds static int selinux_inode_listxattr(struct dentry *dentry) 29031da177e4SLinus Torvalds { 290488e67f3bSDavid Howells const struct cred *cred = current_cred(); 290588e67f3bSDavid Howells 29062875fa00SEric Paris return dentry_has_perm(cred, dentry, FILE__GETATTR); 29071da177e4SLinus Torvalds } 29081da177e4SLinus Torvalds 29098f0cfa52SDavid Howells static int selinux_inode_removexattr(struct dentry *dentry, const char *name) 29101da177e4SLinus Torvalds { 2911b5376771SSerge E. Hallyn if (strcmp(name, XATTR_NAME_SELINUX)) 2912b5376771SSerge E. Hallyn return selinux_inode_setotherxattr(dentry, name); 29131da177e4SLinus Torvalds 29141da177e4SLinus Torvalds /* No one is allowed to remove a SELinux security label. 29151da177e4SLinus Torvalds You can change the label, but all data must be labeled. */ 29161da177e4SLinus Torvalds return -EACCES; 29171da177e4SLinus Torvalds } 29181da177e4SLinus Torvalds 2919d381d8a9SJames Morris /* 2920abc69bb6SStephen Smalley * Copy the inode security context value to the user. 2921d381d8a9SJames Morris * 2922d381d8a9SJames Morris * Permission check is handled by selinux_inode_getxattr hook. 2923d381d8a9SJames Morris */ 292442492594SDavid P. Quigley static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void **buffer, bool alloc) 29251da177e4SLinus Torvalds { 292642492594SDavid P. Quigley u32 size; 292742492594SDavid P. Quigley int error; 292842492594SDavid P. Quigley char *context = NULL; 29291da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 29301da177e4SLinus Torvalds 29318c8570fbSDustin Kirkland if (strcmp(name, XATTR_SELINUX_SUFFIX)) 29328c8570fbSDustin Kirkland return -EOPNOTSUPP; 29331da177e4SLinus Torvalds 2934abc69bb6SStephen Smalley /* 2935abc69bb6SStephen Smalley * If the caller has CAP_MAC_ADMIN, then get the raw context 2936abc69bb6SStephen Smalley * value even if it is not defined by current policy; otherwise, 2937abc69bb6SStephen Smalley * use the in-core value under current policy. 2938abc69bb6SStephen Smalley * Use the non-auditing forms of the permission checks since 2939abc69bb6SStephen Smalley * getxattr may be called by unprivileged processes commonly 2940abc69bb6SStephen Smalley * and lack of permission just means that we fall back to the 2941abc69bb6SStephen Smalley * in-core context value, not a denial. 2942abc69bb6SStephen Smalley */ 29436a9de491SEric Paris error = selinux_capable(current_cred(), &init_user_ns, CAP_MAC_ADMIN, 29443699c53cSDavid Howells SECURITY_CAP_NOAUDIT); 2945abc69bb6SStephen Smalley if (!error) 2946abc69bb6SStephen Smalley error = security_sid_to_context_force(isec->sid, &context, 2947abc69bb6SStephen Smalley &size); 2948abc69bb6SStephen Smalley else 294942492594SDavid P. Quigley error = security_sid_to_context(isec->sid, &context, &size); 295042492594SDavid P. Quigley if (error) 295142492594SDavid P. Quigley return error; 295242492594SDavid P. Quigley error = size; 295342492594SDavid P. Quigley if (alloc) { 295442492594SDavid P. Quigley *buffer = context; 295542492594SDavid P. Quigley goto out_nofree; 295642492594SDavid P. Quigley } 295742492594SDavid P. Quigley kfree(context); 295842492594SDavid P. Quigley out_nofree: 295942492594SDavid P. Quigley return error; 29601da177e4SLinus Torvalds } 29611da177e4SLinus Torvalds 29621da177e4SLinus Torvalds static int selinux_inode_setsecurity(struct inode *inode, const char *name, 29631da177e4SLinus Torvalds const void *value, size_t size, int flags) 29641da177e4SLinus Torvalds { 29651da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 29661da177e4SLinus Torvalds u32 newsid; 29671da177e4SLinus Torvalds int rc; 29681da177e4SLinus Torvalds 29691da177e4SLinus Torvalds if (strcmp(name, XATTR_SELINUX_SUFFIX)) 29701da177e4SLinus Torvalds return -EOPNOTSUPP; 29711da177e4SLinus Torvalds 29721da177e4SLinus Torvalds if (!value || !size) 29731da177e4SLinus Torvalds return -EACCES; 29741da177e4SLinus Torvalds 29751da177e4SLinus Torvalds rc = security_context_to_sid((void *)value, size, &newsid); 29761da177e4SLinus Torvalds if (rc) 29771da177e4SLinus Torvalds return rc; 29781da177e4SLinus Torvalds 29791da177e4SLinus Torvalds isec->sid = newsid; 2980ddd29ec6SDavid P. Quigley isec->initialized = 1; 29811da177e4SLinus Torvalds return 0; 29821da177e4SLinus Torvalds } 29831da177e4SLinus Torvalds 29841da177e4SLinus Torvalds static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size) 29851da177e4SLinus Torvalds { 29861da177e4SLinus Torvalds const int len = sizeof(XATTR_NAME_SELINUX); 29871da177e4SLinus Torvalds if (buffer && len <= buffer_size) 29881da177e4SLinus Torvalds memcpy(buffer, XATTR_NAME_SELINUX, len); 29891da177e4SLinus Torvalds return len; 29901da177e4SLinus Torvalds } 29911da177e4SLinus Torvalds 2992713a04aeSAhmed S. Darwish static void selinux_inode_getsecid(const struct inode *inode, u32 *secid) 2993713a04aeSAhmed S. Darwish { 2994713a04aeSAhmed S. Darwish struct inode_security_struct *isec = inode->i_security; 2995713a04aeSAhmed S. Darwish *secid = isec->sid; 2996713a04aeSAhmed S. Darwish } 2997713a04aeSAhmed S. Darwish 29981da177e4SLinus Torvalds /* file security operations */ 29991da177e4SLinus Torvalds 3000788e7dd4SYuichi Nakamura static int selinux_revalidate_file_permission(struct file *file, int mask) 30011da177e4SLinus Torvalds { 300288e67f3bSDavid Howells const struct cred *cred = current_cred(); 3003496ad9aaSAl Viro struct inode *inode = file_inode(file); 30041da177e4SLinus Torvalds 30051da177e4SLinus Torvalds /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */ 30061da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE)) 30071da177e4SLinus Torvalds mask |= MAY_APPEND; 30081da177e4SLinus Torvalds 3009389fb800SPaul Moore return file_has_perm(cred, file, 30101da177e4SLinus Torvalds file_mask_to_av(inode->i_mode, mask)); 30111da177e4SLinus Torvalds } 30121da177e4SLinus Torvalds 3013788e7dd4SYuichi Nakamura static int selinux_file_permission(struct file *file, int mask) 3014788e7dd4SYuichi Nakamura { 3015496ad9aaSAl Viro struct inode *inode = file_inode(file); 301620dda18bSStephen Smalley struct file_security_struct *fsec = file->f_security; 301720dda18bSStephen Smalley struct inode_security_struct *isec = inode->i_security; 301820dda18bSStephen Smalley u32 sid = current_sid(); 301920dda18bSStephen Smalley 3020389fb800SPaul Moore if (!mask) 3021788e7dd4SYuichi Nakamura /* No permission to check. Existence test. */ 3022788e7dd4SYuichi Nakamura return 0; 3023788e7dd4SYuichi Nakamura 302420dda18bSStephen Smalley if (sid == fsec->sid && fsec->isid == isec->sid && 302520dda18bSStephen Smalley fsec->pseqno == avc_policy_seqno()) 302683d49856SEric Paris /* No change since file_open check. */ 302720dda18bSStephen Smalley return 0; 302820dda18bSStephen Smalley 3029788e7dd4SYuichi Nakamura return selinux_revalidate_file_permission(file, mask); 3030788e7dd4SYuichi Nakamura } 3031788e7dd4SYuichi Nakamura 30321da177e4SLinus Torvalds static int selinux_file_alloc_security(struct file *file) 30331da177e4SLinus Torvalds { 30341da177e4SLinus Torvalds return file_alloc_security(file); 30351da177e4SLinus Torvalds } 30361da177e4SLinus Torvalds 30371da177e4SLinus Torvalds static void selinux_file_free_security(struct file *file) 30381da177e4SLinus Torvalds { 30391da177e4SLinus Torvalds file_free_security(file); 30401da177e4SLinus Torvalds } 30411da177e4SLinus Torvalds 30421da177e4SLinus Torvalds static int selinux_file_ioctl(struct file *file, unsigned int cmd, 30431da177e4SLinus Torvalds unsigned long arg) 30441da177e4SLinus Torvalds { 304588e67f3bSDavid Howells const struct cred *cred = current_cred(); 30460b24dcb7SEric Paris int error = 0; 30471da177e4SLinus Torvalds 30480b24dcb7SEric Paris switch (cmd) { 30490b24dcb7SEric Paris case FIONREAD: 30500b24dcb7SEric Paris /* fall through */ 30510b24dcb7SEric Paris case FIBMAP: 30520b24dcb7SEric Paris /* fall through */ 30530b24dcb7SEric Paris case FIGETBSZ: 30540b24dcb7SEric Paris /* fall through */ 30552f99c369SAl Viro case FS_IOC_GETFLAGS: 30560b24dcb7SEric Paris /* fall through */ 30572f99c369SAl Viro case FS_IOC_GETVERSION: 30580b24dcb7SEric Paris error = file_has_perm(cred, file, FILE__GETATTR); 30590b24dcb7SEric Paris break; 30601da177e4SLinus Torvalds 30612f99c369SAl Viro case FS_IOC_SETFLAGS: 30620b24dcb7SEric Paris /* fall through */ 30632f99c369SAl Viro case FS_IOC_SETVERSION: 30640b24dcb7SEric Paris error = file_has_perm(cred, file, FILE__SETATTR); 30650b24dcb7SEric Paris break; 30660b24dcb7SEric Paris 30670b24dcb7SEric Paris /* sys_ioctl() checks */ 30680b24dcb7SEric Paris case FIONBIO: 30690b24dcb7SEric Paris /* fall through */ 30700b24dcb7SEric Paris case FIOASYNC: 30710b24dcb7SEric Paris error = file_has_perm(cred, file, 0); 30720b24dcb7SEric Paris break; 30730b24dcb7SEric Paris 30740b24dcb7SEric Paris case KDSKBENT: 30750b24dcb7SEric Paris case KDSKBSENT: 30766a9de491SEric Paris error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG, 30770b24dcb7SEric Paris SECURITY_CAP_AUDIT); 30780b24dcb7SEric Paris break; 30790b24dcb7SEric Paris 30800b24dcb7SEric Paris /* default case assumes that the command will go 30810b24dcb7SEric Paris * to the file's ioctl() function. 30820b24dcb7SEric Paris */ 30830b24dcb7SEric Paris default: 30840b24dcb7SEric Paris error = file_has_perm(cred, file, FILE__IOCTL); 30850b24dcb7SEric Paris } 30860b24dcb7SEric Paris return error; 30871da177e4SLinus Torvalds } 30881da177e4SLinus Torvalds 3089fcaaade1SStephen Smalley static int default_noexec; 3090fcaaade1SStephen Smalley 30911da177e4SLinus Torvalds static int file_map_prot_check(struct file *file, unsigned long prot, int shared) 30921da177e4SLinus Torvalds { 309388e67f3bSDavid Howells const struct cred *cred = current_cred(); 3094d84f4f99SDavid Howells int rc = 0; 309588e67f3bSDavid Howells 3096fcaaade1SStephen Smalley if (default_noexec && 3097fcaaade1SStephen Smalley (prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) { 30981da177e4SLinus Torvalds /* 30991da177e4SLinus Torvalds * We are making executable an anonymous mapping or a 31001da177e4SLinus Torvalds * private file mapping that will also be writable. 31011da177e4SLinus Torvalds * This has an additional check. 31021da177e4SLinus Torvalds */ 3103d84f4f99SDavid Howells rc = cred_has_perm(cred, cred, PROCESS__EXECMEM); 31041da177e4SLinus Torvalds if (rc) 3105d84f4f99SDavid Howells goto error; 31061da177e4SLinus Torvalds } 31071da177e4SLinus Torvalds 31081da177e4SLinus Torvalds if (file) { 31091da177e4SLinus Torvalds /* read access is always possible with a mapping */ 31101da177e4SLinus Torvalds u32 av = FILE__READ; 31111da177e4SLinus Torvalds 31121da177e4SLinus Torvalds /* write access only matters if the mapping is shared */ 31131da177e4SLinus Torvalds if (shared && (prot & PROT_WRITE)) 31141da177e4SLinus Torvalds av |= FILE__WRITE; 31151da177e4SLinus Torvalds 31161da177e4SLinus Torvalds if (prot & PROT_EXEC) 31171da177e4SLinus Torvalds av |= FILE__EXECUTE; 31181da177e4SLinus Torvalds 311988e67f3bSDavid Howells return file_has_perm(cred, file, av); 31201da177e4SLinus Torvalds } 3121d84f4f99SDavid Howells 3122d84f4f99SDavid Howells error: 3123d84f4f99SDavid Howells return rc; 31241da177e4SLinus Torvalds } 31251da177e4SLinus Torvalds 3126e5467859SAl Viro static int selinux_mmap_addr(unsigned long addr) 31271da177e4SLinus Torvalds { 3128ed032189SEric Paris int rc = 0; 3129275bb41eSDavid Howells u32 sid = current_sid(); 31301da177e4SLinus Torvalds 313184336d1aSEric Paris /* 313284336d1aSEric Paris * notice that we are intentionally putting the SELinux check before 313384336d1aSEric Paris * the secondary cap_file_mmap check. This is such a likely attempt 313484336d1aSEric Paris * at bad behaviour/exploit that we always want to get the AVC, even 313584336d1aSEric Paris * if DAC would have also denied the operation. 313684336d1aSEric Paris */ 3137a2551df7SEric Paris if (addr < CONFIG_LSM_MMAP_MIN_ADDR) { 3138ed032189SEric Paris rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT, 3139ed032189SEric Paris MEMPROTECT__MMAP_ZERO, NULL); 314084336d1aSEric Paris if (rc) 314184336d1aSEric Paris return rc; 314284336d1aSEric Paris } 314384336d1aSEric Paris 314484336d1aSEric Paris /* do DAC check on address space usage */ 3145e5467859SAl Viro return cap_mmap_addr(addr); 3146e5467859SAl Viro } 31471da177e4SLinus Torvalds 3148e5467859SAl Viro static int selinux_mmap_file(struct file *file, unsigned long reqprot, 3149e5467859SAl Viro unsigned long prot, unsigned long flags) 3150e5467859SAl Viro { 31511da177e4SLinus Torvalds if (selinux_checkreqprot) 31521da177e4SLinus Torvalds prot = reqprot; 31531da177e4SLinus Torvalds 31541da177e4SLinus Torvalds return file_map_prot_check(file, prot, 31551da177e4SLinus Torvalds (flags & MAP_TYPE) == MAP_SHARED); 31561da177e4SLinus Torvalds } 31571da177e4SLinus Torvalds 31581da177e4SLinus Torvalds static int selinux_file_mprotect(struct vm_area_struct *vma, 31591da177e4SLinus Torvalds unsigned long reqprot, 31601da177e4SLinus Torvalds unsigned long prot) 31611da177e4SLinus Torvalds { 316288e67f3bSDavid Howells const struct cred *cred = current_cred(); 31631da177e4SLinus Torvalds 31641da177e4SLinus Torvalds if (selinux_checkreqprot) 31651da177e4SLinus Torvalds prot = reqprot; 31661da177e4SLinus Torvalds 3167fcaaade1SStephen Smalley if (default_noexec && 3168fcaaade1SStephen Smalley (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) { 3169d541bbeeSJames Morris int rc = 0; 3170db4c9641SStephen Smalley if (vma->vm_start >= vma->vm_mm->start_brk && 3171db4c9641SStephen Smalley vma->vm_end <= vma->vm_mm->brk) { 3172d84f4f99SDavid Howells rc = cred_has_perm(cred, cred, PROCESS__EXECHEAP); 3173db4c9641SStephen Smalley } else if (!vma->vm_file && 31746b992197SLorenzo Hernandez García-Hierro vma->vm_start <= vma->vm_mm->start_stack && 31756b992197SLorenzo Hernandez García-Hierro vma->vm_end >= vma->vm_mm->start_stack) { 31763b11a1deSDavid Howells rc = current_has_perm(current, PROCESS__EXECSTACK); 3177db4c9641SStephen Smalley } else if (vma->vm_file && vma->anon_vma) { 3178db4c9641SStephen Smalley /* 3179db4c9641SStephen Smalley * We are making executable a file mapping that has 3180db4c9641SStephen Smalley * had some COW done. Since pages might have been 3181db4c9641SStephen Smalley * written, check ability to execute the possibly 3182db4c9641SStephen Smalley * modified content. This typically should only 3183db4c9641SStephen Smalley * occur for text relocations. 3184db4c9641SStephen Smalley */ 3185d84f4f99SDavid Howells rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD); 3186db4c9641SStephen Smalley } 31876b992197SLorenzo Hernandez García-Hierro if (rc) 31886b992197SLorenzo Hernandez García-Hierro return rc; 31896b992197SLorenzo Hernandez García-Hierro } 31901da177e4SLinus Torvalds 31911da177e4SLinus Torvalds return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED); 31921da177e4SLinus Torvalds } 31931da177e4SLinus Torvalds 31941da177e4SLinus Torvalds static int selinux_file_lock(struct file *file, unsigned int cmd) 31951da177e4SLinus Torvalds { 319688e67f3bSDavid Howells const struct cred *cred = current_cred(); 319788e67f3bSDavid Howells 319888e67f3bSDavid Howells return file_has_perm(cred, file, FILE__LOCK); 31991da177e4SLinus Torvalds } 32001da177e4SLinus Torvalds 32011da177e4SLinus Torvalds static int selinux_file_fcntl(struct file *file, unsigned int cmd, 32021da177e4SLinus Torvalds unsigned long arg) 32031da177e4SLinus Torvalds { 320488e67f3bSDavid Howells const struct cred *cred = current_cred(); 32051da177e4SLinus Torvalds int err = 0; 32061da177e4SLinus Torvalds 32071da177e4SLinus Torvalds switch (cmd) { 32081da177e4SLinus Torvalds case F_SETFL: 32091da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) { 321088e67f3bSDavid Howells err = file_has_perm(cred, file, FILE__WRITE); 32111da177e4SLinus Torvalds break; 32121da177e4SLinus Torvalds } 32131da177e4SLinus Torvalds /* fall through */ 32141da177e4SLinus Torvalds case F_SETOWN: 32151da177e4SLinus Torvalds case F_SETSIG: 32161da177e4SLinus Torvalds case F_GETFL: 32171da177e4SLinus Torvalds case F_GETOWN: 32181da177e4SLinus Torvalds case F_GETSIG: 32191d151c33SCyrill Gorcunov case F_GETOWNER_UIDS: 32201da177e4SLinus Torvalds /* Just check FD__USE permission */ 322188e67f3bSDavid Howells err = file_has_perm(cred, file, 0); 32221da177e4SLinus Torvalds break; 32231da177e4SLinus Torvalds case F_GETLK: 32241da177e4SLinus Torvalds case F_SETLK: 32251da177e4SLinus Torvalds case F_SETLKW: 32261da177e4SLinus Torvalds #if BITS_PER_LONG == 32 32271da177e4SLinus Torvalds case F_GETLK64: 32281da177e4SLinus Torvalds case F_SETLK64: 32291da177e4SLinus Torvalds case F_SETLKW64: 32301da177e4SLinus Torvalds #endif 323188e67f3bSDavid Howells err = file_has_perm(cred, file, FILE__LOCK); 32321da177e4SLinus Torvalds break; 32331da177e4SLinus Torvalds } 32341da177e4SLinus Torvalds 32351da177e4SLinus Torvalds return err; 32361da177e4SLinus Torvalds } 32371da177e4SLinus Torvalds 32381da177e4SLinus Torvalds static int selinux_file_set_fowner(struct file *file) 32391da177e4SLinus Torvalds { 32401da177e4SLinus Torvalds struct file_security_struct *fsec; 32411da177e4SLinus Torvalds 32421da177e4SLinus Torvalds fsec = file->f_security; 3243275bb41eSDavid Howells fsec->fown_sid = current_sid(); 32441da177e4SLinus Torvalds 32451da177e4SLinus Torvalds return 0; 32461da177e4SLinus Torvalds } 32471da177e4SLinus Torvalds 32481da177e4SLinus Torvalds static int selinux_file_send_sigiotask(struct task_struct *tsk, 32491da177e4SLinus Torvalds struct fown_struct *fown, int signum) 32501da177e4SLinus Torvalds { 32511da177e4SLinus Torvalds struct file *file; 325265c90bcaSStephen Smalley u32 sid = task_sid(tsk); 32531da177e4SLinus Torvalds u32 perm; 32541da177e4SLinus Torvalds struct file_security_struct *fsec; 32551da177e4SLinus Torvalds 32561da177e4SLinus Torvalds /* struct fown_struct is never outside the context of a struct file */ 3257b385a144SRobert P. J. Day file = container_of(fown, struct file, f_owner); 32581da177e4SLinus Torvalds 32591da177e4SLinus Torvalds fsec = file->f_security; 32601da177e4SLinus Torvalds 32611da177e4SLinus Torvalds if (!signum) 32621da177e4SLinus Torvalds perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */ 32631da177e4SLinus Torvalds else 32641da177e4SLinus Torvalds perm = signal_to_av(signum); 32651da177e4SLinus Torvalds 3266275bb41eSDavid Howells return avc_has_perm(fsec->fown_sid, sid, 32671da177e4SLinus Torvalds SECCLASS_PROCESS, perm, NULL); 32681da177e4SLinus Torvalds } 32691da177e4SLinus Torvalds 32701da177e4SLinus Torvalds static int selinux_file_receive(struct file *file) 32711da177e4SLinus Torvalds { 327288e67f3bSDavid Howells const struct cred *cred = current_cred(); 327388e67f3bSDavid Howells 327488e67f3bSDavid Howells return file_has_perm(cred, file, file_to_av(file)); 32751da177e4SLinus Torvalds } 32761da177e4SLinus Torvalds 327783d49856SEric Paris static int selinux_file_open(struct file *file, const struct cred *cred) 3278788e7dd4SYuichi Nakamura { 3279788e7dd4SYuichi Nakamura struct file_security_struct *fsec; 3280788e7dd4SYuichi Nakamura struct inode_security_struct *isec; 3281d84f4f99SDavid Howells 3282788e7dd4SYuichi Nakamura fsec = file->f_security; 3283496ad9aaSAl Viro isec = file_inode(file)->i_security; 3284788e7dd4SYuichi Nakamura /* 3285788e7dd4SYuichi Nakamura * Save inode label and policy sequence number 3286788e7dd4SYuichi Nakamura * at open-time so that selinux_file_permission 3287788e7dd4SYuichi Nakamura * can determine whether revalidation is necessary. 3288788e7dd4SYuichi Nakamura * Task label is already saved in the file security 3289788e7dd4SYuichi Nakamura * struct as its SID. 3290788e7dd4SYuichi Nakamura */ 3291788e7dd4SYuichi Nakamura fsec->isid = isec->sid; 3292788e7dd4SYuichi Nakamura fsec->pseqno = avc_policy_seqno(); 3293788e7dd4SYuichi Nakamura /* 3294788e7dd4SYuichi Nakamura * Since the inode label or policy seqno may have changed 3295788e7dd4SYuichi Nakamura * between the selinux_inode_permission check and the saving 3296788e7dd4SYuichi Nakamura * of state above, recheck that access is still permitted. 3297788e7dd4SYuichi Nakamura * Otherwise, access might never be revalidated against the 3298788e7dd4SYuichi Nakamura * new inode label or new policy. 3299788e7dd4SYuichi Nakamura * This check is not redundant - do not remove. 3300788e7dd4SYuichi Nakamura */ 3301602a8dd6SEric Paris return path_has_perm(cred, &file->f_path, open_file_to_av(file)); 3302788e7dd4SYuichi Nakamura } 3303788e7dd4SYuichi Nakamura 33041da177e4SLinus Torvalds /* task security operations */ 33051da177e4SLinus Torvalds 33061da177e4SLinus Torvalds static int selinux_task_create(unsigned long clone_flags) 33071da177e4SLinus Torvalds { 33083b11a1deSDavid Howells return current_has_perm(current, PROCESS__FORK); 33091da177e4SLinus Torvalds } 33101da177e4SLinus Torvalds 3311f1752eecSDavid Howells /* 3312ee18d64cSDavid Howells * allocate the SELinux part of blank credentials 3313ee18d64cSDavid Howells */ 3314ee18d64cSDavid Howells static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp) 3315ee18d64cSDavid Howells { 3316ee18d64cSDavid Howells struct task_security_struct *tsec; 3317ee18d64cSDavid Howells 3318ee18d64cSDavid Howells tsec = kzalloc(sizeof(struct task_security_struct), gfp); 3319ee18d64cSDavid Howells if (!tsec) 3320ee18d64cSDavid Howells return -ENOMEM; 3321ee18d64cSDavid Howells 3322ee18d64cSDavid Howells cred->security = tsec; 3323ee18d64cSDavid Howells return 0; 3324ee18d64cSDavid Howells } 3325ee18d64cSDavid Howells 3326ee18d64cSDavid Howells /* 3327f1752eecSDavid Howells * detach and free the LSM part of a set of credentials 3328f1752eecSDavid Howells */ 3329f1752eecSDavid Howells static void selinux_cred_free(struct cred *cred) 33301da177e4SLinus Torvalds { 3331f1752eecSDavid Howells struct task_security_struct *tsec = cred->security; 3332e0e81739SDavid Howells 33332edeaa34STetsuo Handa /* 33342edeaa34STetsuo Handa * cred->security == NULL if security_cred_alloc_blank() or 33352edeaa34STetsuo Handa * security_prepare_creds() returned an error. 33362edeaa34STetsuo Handa */ 33372edeaa34STetsuo Handa BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE); 3338e0e81739SDavid Howells cred->security = (void *) 0x7UL; 3339f1752eecSDavid Howells kfree(tsec); 33401da177e4SLinus Torvalds } 33411da177e4SLinus Torvalds 3342d84f4f99SDavid Howells /* 3343d84f4f99SDavid Howells * prepare a new set of credentials for modification 3344d84f4f99SDavid Howells */ 3345d84f4f99SDavid Howells static int selinux_cred_prepare(struct cred *new, const struct cred *old, 3346d84f4f99SDavid Howells gfp_t gfp) 3347d84f4f99SDavid Howells { 3348d84f4f99SDavid Howells const struct task_security_struct *old_tsec; 3349d84f4f99SDavid Howells struct task_security_struct *tsec; 3350d84f4f99SDavid Howells 3351d84f4f99SDavid Howells old_tsec = old->security; 3352d84f4f99SDavid Howells 3353d84f4f99SDavid Howells tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp); 3354d84f4f99SDavid Howells if (!tsec) 3355d84f4f99SDavid Howells return -ENOMEM; 3356d84f4f99SDavid Howells 3357d84f4f99SDavid Howells new->security = tsec; 3358d84f4f99SDavid Howells return 0; 3359d84f4f99SDavid Howells } 3360d84f4f99SDavid Howells 3361d84f4f99SDavid Howells /* 3362ee18d64cSDavid Howells * transfer the SELinux data to a blank set of creds 3363ee18d64cSDavid Howells */ 3364ee18d64cSDavid Howells static void selinux_cred_transfer(struct cred *new, const struct cred *old) 3365ee18d64cSDavid Howells { 3366ee18d64cSDavid Howells const struct task_security_struct *old_tsec = old->security; 3367ee18d64cSDavid Howells struct task_security_struct *tsec = new->security; 3368ee18d64cSDavid Howells 3369ee18d64cSDavid Howells *tsec = *old_tsec; 3370ee18d64cSDavid Howells } 3371ee18d64cSDavid Howells 3372ee18d64cSDavid Howells /* 33733a3b7ce9SDavid Howells * set the security data for a kernel service 33743a3b7ce9SDavid Howells * - all the creation contexts are set to unlabelled 33753a3b7ce9SDavid Howells */ 33763a3b7ce9SDavid Howells static int selinux_kernel_act_as(struct cred *new, u32 secid) 33773a3b7ce9SDavid Howells { 33783a3b7ce9SDavid Howells struct task_security_struct *tsec = new->security; 33793a3b7ce9SDavid Howells u32 sid = current_sid(); 33803a3b7ce9SDavid Howells int ret; 33813a3b7ce9SDavid Howells 33823a3b7ce9SDavid Howells ret = avc_has_perm(sid, secid, 33833a3b7ce9SDavid Howells SECCLASS_KERNEL_SERVICE, 33843a3b7ce9SDavid Howells KERNEL_SERVICE__USE_AS_OVERRIDE, 33853a3b7ce9SDavid Howells NULL); 33863a3b7ce9SDavid Howells if (ret == 0) { 33873a3b7ce9SDavid Howells tsec->sid = secid; 33883a3b7ce9SDavid Howells tsec->create_sid = 0; 33893a3b7ce9SDavid Howells tsec->keycreate_sid = 0; 33903a3b7ce9SDavid Howells tsec->sockcreate_sid = 0; 33913a3b7ce9SDavid Howells } 33923a3b7ce9SDavid Howells return ret; 33933a3b7ce9SDavid Howells } 33943a3b7ce9SDavid Howells 33953a3b7ce9SDavid Howells /* 33963a3b7ce9SDavid Howells * set the file creation context in a security record to the same as the 33973a3b7ce9SDavid Howells * objective context of the specified inode 33983a3b7ce9SDavid Howells */ 33993a3b7ce9SDavid Howells static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode) 34003a3b7ce9SDavid Howells { 34013a3b7ce9SDavid Howells struct inode_security_struct *isec = inode->i_security; 34023a3b7ce9SDavid Howells struct task_security_struct *tsec = new->security; 34033a3b7ce9SDavid Howells u32 sid = current_sid(); 34043a3b7ce9SDavid Howells int ret; 34053a3b7ce9SDavid Howells 34063a3b7ce9SDavid Howells ret = avc_has_perm(sid, isec->sid, 34073a3b7ce9SDavid Howells SECCLASS_KERNEL_SERVICE, 34083a3b7ce9SDavid Howells KERNEL_SERVICE__CREATE_FILES_AS, 34093a3b7ce9SDavid Howells NULL); 34103a3b7ce9SDavid Howells 34113a3b7ce9SDavid Howells if (ret == 0) 34123a3b7ce9SDavid Howells tsec->create_sid = isec->sid; 3413ef57471aSDavid Howells return ret; 34143a3b7ce9SDavid Howells } 34153a3b7ce9SDavid Howells 3416dd8dbf2eSEric Paris static int selinux_kernel_module_request(char *kmod_name) 341725354c4fSEric Paris { 3418dd8dbf2eSEric Paris u32 sid; 3419dd8dbf2eSEric Paris struct common_audit_data ad; 3420dd8dbf2eSEric Paris 3421dd8dbf2eSEric Paris sid = task_sid(current); 3422dd8dbf2eSEric Paris 342350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_KMOD; 3424dd8dbf2eSEric Paris ad.u.kmod_name = kmod_name; 3425dd8dbf2eSEric Paris 3426dd8dbf2eSEric Paris return avc_has_perm(sid, SECINITSID_KERNEL, SECCLASS_SYSTEM, 3427dd8dbf2eSEric Paris SYSTEM__MODULE_REQUEST, &ad); 342825354c4fSEric Paris } 342925354c4fSEric Paris 34301da177e4SLinus Torvalds static int selinux_task_setpgid(struct task_struct *p, pid_t pgid) 34311da177e4SLinus Torvalds { 34323b11a1deSDavid Howells return current_has_perm(p, PROCESS__SETPGID); 34331da177e4SLinus Torvalds } 34341da177e4SLinus Torvalds 34351da177e4SLinus Torvalds static int selinux_task_getpgid(struct task_struct *p) 34361da177e4SLinus Torvalds { 34373b11a1deSDavid Howells return current_has_perm(p, PROCESS__GETPGID); 34381da177e4SLinus Torvalds } 34391da177e4SLinus Torvalds 34401da177e4SLinus Torvalds static int selinux_task_getsid(struct task_struct *p) 34411da177e4SLinus Torvalds { 34423b11a1deSDavid Howells return current_has_perm(p, PROCESS__GETSESSION); 34431da177e4SLinus Torvalds } 34441da177e4SLinus Torvalds 3445f9008e4cSDavid Quigley static void selinux_task_getsecid(struct task_struct *p, u32 *secid) 3446f9008e4cSDavid Quigley { 3447275bb41eSDavid Howells *secid = task_sid(p); 3448f9008e4cSDavid Quigley } 3449f9008e4cSDavid Quigley 34501da177e4SLinus Torvalds static int selinux_task_setnice(struct task_struct *p, int nice) 34511da177e4SLinus Torvalds { 34521da177e4SLinus Torvalds int rc; 34531da177e4SLinus Torvalds 3454200ac532SEric Paris rc = cap_task_setnice(p, nice); 34551da177e4SLinus Torvalds if (rc) 34561da177e4SLinus Torvalds return rc; 34571da177e4SLinus Torvalds 34583b11a1deSDavid Howells return current_has_perm(p, PROCESS__SETSCHED); 34591da177e4SLinus Torvalds } 34601da177e4SLinus Torvalds 346103e68060SJames Morris static int selinux_task_setioprio(struct task_struct *p, int ioprio) 346203e68060SJames Morris { 3463b5376771SSerge E. Hallyn int rc; 3464b5376771SSerge E. Hallyn 3465200ac532SEric Paris rc = cap_task_setioprio(p, ioprio); 3466b5376771SSerge E. Hallyn if (rc) 3467b5376771SSerge E. Hallyn return rc; 3468b5376771SSerge E. Hallyn 34693b11a1deSDavid Howells return current_has_perm(p, PROCESS__SETSCHED); 347003e68060SJames Morris } 347103e68060SJames Morris 3472a1836a42SDavid Quigley static int selinux_task_getioprio(struct task_struct *p) 3473a1836a42SDavid Quigley { 34743b11a1deSDavid Howells return current_has_perm(p, PROCESS__GETSCHED); 3475a1836a42SDavid Quigley } 3476a1836a42SDavid Quigley 34778fd00b4dSJiri Slaby static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource, 34788fd00b4dSJiri Slaby struct rlimit *new_rlim) 34791da177e4SLinus Torvalds { 34808fd00b4dSJiri Slaby struct rlimit *old_rlim = p->signal->rlim + resource; 34811da177e4SLinus Torvalds 34821da177e4SLinus Torvalds /* Control the ability to change the hard limit (whether 34831da177e4SLinus Torvalds lowering or raising it), so that the hard limit can 34841da177e4SLinus Torvalds later be used as a safe reset point for the soft limit 3485d84f4f99SDavid Howells upon context transitions. See selinux_bprm_committing_creds. */ 34861da177e4SLinus Torvalds if (old_rlim->rlim_max != new_rlim->rlim_max) 34878fd00b4dSJiri Slaby return current_has_perm(p, PROCESS__SETRLIMIT); 34881da177e4SLinus Torvalds 34891da177e4SLinus Torvalds return 0; 34901da177e4SLinus Torvalds } 34911da177e4SLinus Torvalds 3492b0ae1981SKOSAKI Motohiro static int selinux_task_setscheduler(struct task_struct *p) 34931da177e4SLinus Torvalds { 3494b5376771SSerge E. Hallyn int rc; 3495b5376771SSerge E. Hallyn 3496b0ae1981SKOSAKI Motohiro rc = cap_task_setscheduler(p); 3497b5376771SSerge E. Hallyn if (rc) 3498b5376771SSerge E. Hallyn return rc; 3499b5376771SSerge E. Hallyn 35003b11a1deSDavid Howells return current_has_perm(p, PROCESS__SETSCHED); 35011da177e4SLinus Torvalds } 35021da177e4SLinus Torvalds 35031da177e4SLinus Torvalds static int selinux_task_getscheduler(struct task_struct *p) 35041da177e4SLinus Torvalds { 35053b11a1deSDavid Howells return current_has_perm(p, PROCESS__GETSCHED); 35061da177e4SLinus Torvalds } 35071da177e4SLinus Torvalds 350835601547SDavid Quigley static int selinux_task_movememory(struct task_struct *p) 350935601547SDavid Quigley { 35103b11a1deSDavid Howells return current_has_perm(p, PROCESS__SETSCHED); 351135601547SDavid Quigley } 351235601547SDavid Quigley 3513f9008e4cSDavid Quigley static int selinux_task_kill(struct task_struct *p, struct siginfo *info, 3514f9008e4cSDavid Quigley int sig, u32 secid) 35151da177e4SLinus Torvalds { 35161da177e4SLinus Torvalds u32 perm; 35171da177e4SLinus Torvalds int rc; 35181da177e4SLinus Torvalds 35191da177e4SLinus Torvalds if (!sig) 35201da177e4SLinus Torvalds perm = PROCESS__SIGNULL; /* null signal; existence test */ 35211da177e4SLinus Torvalds else 35221da177e4SLinus Torvalds perm = signal_to_av(sig); 3523f9008e4cSDavid Quigley if (secid) 3524275bb41eSDavid Howells rc = avc_has_perm(secid, task_sid(p), 3525275bb41eSDavid Howells SECCLASS_PROCESS, perm, NULL); 3526f9008e4cSDavid Quigley else 35273b11a1deSDavid Howells rc = current_has_perm(p, perm); 3528f9008e4cSDavid Quigley return rc; 35291da177e4SLinus Torvalds } 35301da177e4SLinus Torvalds 35311da177e4SLinus Torvalds static int selinux_task_wait(struct task_struct *p) 35321da177e4SLinus Torvalds { 35338a535140SEric Paris return task_has_perm(p, current, PROCESS__SIGCHLD); 35341da177e4SLinus Torvalds } 35351da177e4SLinus Torvalds 35361da177e4SLinus Torvalds static void selinux_task_to_inode(struct task_struct *p, 35371da177e4SLinus Torvalds struct inode *inode) 35381da177e4SLinus Torvalds { 35391da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 3540275bb41eSDavid Howells u32 sid = task_sid(p); 35411da177e4SLinus Torvalds 3542275bb41eSDavid Howells isec->sid = sid; 35431da177e4SLinus Torvalds isec->initialized = 1; 35441da177e4SLinus Torvalds } 35451da177e4SLinus Torvalds 35461da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 354767f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv4(struct sk_buff *skb, 35482bf49690SThomas Liu struct common_audit_data *ad, u8 *proto) 35491da177e4SLinus Torvalds { 35501da177e4SLinus Torvalds int offset, ihlen, ret = -EINVAL; 35511da177e4SLinus Torvalds struct iphdr _iph, *ih; 35521da177e4SLinus Torvalds 3553bbe735e4SArnaldo Carvalho de Melo offset = skb_network_offset(skb); 35541da177e4SLinus Torvalds ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph); 35551da177e4SLinus Torvalds if (ih == NULL) 35561da177e4SLinus Torvalds goto out; 35571da177e4SLinus Torvalds 35581da177e4SLinus Torvalds ihlen = ih->ihl * 4; 35591da177e4SLinus Torvalds if (ihlen < sizeof(_iph)) 35601da177e4SLinus Torvalds goto out; 35611da177e4SLinus Torvalds 356248c62af6SEric Paris ad->u.net->v4info.saddr = ih->saddr; 356348c62af6SEric Paris ad->u.net->v4info.daddr = ih->daddr; 35641da177e4SLinus Torvalds ret = 0; 35651da177e4SLinus Torvalds 356667f83cbfSVenkat Yekkirala if (proto) 356767f83cbfSVenkat Yekkirala *proto = ih->protocol; 356867f83cbfSVenkat Yekkirala 35691da177e4SLinus Torvalds switch (ih->protocol) { 35701da177e4SLinus Torvalds case IPPROTO_TCP: { 35711da177e4SLinus Torvalds struct tcphdr _tcph, *th; 35721da177e4SLinus Torvalds 35731da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 35741da177e4SLinus Torvalds break; 35751da177e4SLinus Torvalds 35761da177e4SLinus Torvalds offset += ihlen; 35771da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 35781da177e4SLinus Torvalds if (th == NULL) 35791da177e4SLinus Torvalds break; 35801da177e4SLinus Torvalds 358148c62af6SEric Paris ad->u.net->sport = th->source; 358248c62af6SEric Paris ad->u.net->dport = th->dest; 35831da177e4SLinus Torvalds break; 35841da177e4SLinus Torvalds } 35851da177e4SLinus Torvalds 35861da177e4SLinus Torvalds case IPPROTO_UDP: { 35871da177e4SLinus Torvalds struct udphdr _udph, *uh; 35881da177e4SLinus Torvalds 35891da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 35901da177e4SLinus Torvalds break; 35911da177e4SLinus Torvalds 35921da177e4SLinus Torvalds offset += ihlen; 35931da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 35941da177e4SLinus Torvalds if (uh == NULL) 35951da177e4SLinus Torvalds break; 35961da177e4SLinus Torvalds 359748c62af6SEric Paris ad->u.net->sport = uh->source; 359848c62af6SEric Paris ad->u.net->dport = uh->dest; 35991da177e4SLinus Torvalds break; 36001da177e4SLinus Torvalds } 36011da177e4SLinus Torvalds 36022ee92d46SJames Morris case IPPROTO_DCCP: { 36032ee92d46SJames Morris struct dccp_hdr _dccph, *dh; 36042ee92d46SJames Morris 36052ee92d46SJames Morris if (ntohs(ih->frag_off) & IP_OFFSET) 36062ee92d46SJames Morris break; 36072ee92d46SJames Morris 36082ee92d46SJames Morris offset += ihlen; 36092ee92d46SJames Morris dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); 36102ee92d46SJames Morris if (dh == NULL) 36112ee92d46SJames Morris break; 36122ee92d46SJames Morris 361348c62af6SEric Paris ad->u.net->sport = dh->dccph_sport; 361448c62af6SEric Paris ad->u.net->dport = dh->dccph_dport; 36152ee92d46SJames Morris break; 36162ee92d46SJames Morris } 36172ee92d46SJames Morris 36181da177e4SLinus Torvalds default: 36191da177e4SLinus Torvalds break; 36201da177e4SLinus Torvalds } 36211da177e4SLinus Torvalds out: 36221da177e4SLinus Torvalds return ret; 36231da177e4SLinus Torvalds } 36241da177e4SLinus Torvalds 36251da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 36261da177e4SLinus Torvalds 36271da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 362867f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv6(struct sk_buff *skb, 36292bf49690SThomas Liu struct common_audit_data *ad, u8 *proto) 36301da177e4SLinus Torvalds { 36311da177e4SLinus Torvalds u8 nexthdr; 36321da177e4SLinus Torvalds int ret = -EINVAL, offset; 36331da177e4SLinus Torvalds struct ipv6hdr _ipv6h, *ip6; 363475f2811cSJesse Gross __be16 frag_off; 36351da177e4SLinus Torvalds 3636bbe735e4SArnaldo Carvalho de Melo offset = skb_network_offset(skb); 36371da177e4SLinus Torvalds ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h); 36381da177e4SLinus Torvalds if (ip6 == NULL) 36391da177e4SLinus Torvalds goto out; 36401da177e4SLinus Torvalds 364148c62af6SEric Paris ad->u.net->v6info.saddr = ip6->saddr; 364248c62af6SEric Paris ad->u.net->v6info.daddr = ip6->daddr; 36431da177e4SLinus Torvalds ret = 0; 36441da177e4SLinus Torvalds 36451da177e4SLinus Torvalds nexthdr = ip6->nexthdr; 36461da177e4SLinus Torvalds offset += sizeof(_ipv6h); 364775f2811cSJesse Gross offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off); 36481da177e4SLinus Torvalds if (offset < 0) 36491da177e4SLinus Torvalds goto out; 36501da177e4SLinus Torvalds 365167f83cbfSVenkat Yekkirala if (proto) 365267f83cbfSVenkat Yekkirala *proto = nexthdr; 365367f83cbfSVenkat Yekkirala 36541da177e4SLinus Torvalds switch (nexthdr) { 36551da177e4SLinus Torvalds case IPPROTO_TCP: { 36561da177e4SLinus Torvalds struct tcphdr _tcph, *th; 36571da177e4SLinus Torvalds 36581da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 36591da177e4SLinus Torvalds if (th == NULL) 36601da177e4SLinus Torvalds break; 36611da177e4SLinus Torvalds 366248c62af6SEric Paris ad->u.net->sport = th->source; 366348c62af6SEric Paris ad->u.net->dport = th->dest; 36641da177e4SLinus Torvalds break; 36651da177e4SLinus Torvalds } 36661da177e4SLinus Torvalds 36671da177e4SLinus Torvalds case IPPROTO_UDP: { 36681da177e4SLinus Torvalds struct udphdr _udph, *uh; 36691da177e4SLinus Torvalds 36701da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 36711da177e4SLinus Torvalds if (uh == NULL) 36721da177e4SLinus Torvalds break; 36731da177e4SLinus Torvalds 367448c62af6SEric Paris ad->u.net->sport = uh->source; 367548c62af6SEric Paris ad->u.net->dport = uh->dest; 36761da177e4SLinus Torvalds break; 36771da177e4SLinus Torvalds } 36781da177e4SLinus Torvalds 36792ee92d46SJames Morris case IPPROTO_DCCP: { 36802ee92d46SJames Morris struct dccp_hdr _dccph, *dh; 36812ee92d46SJames Morris 36822ee92d46SJames Morris dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); 36832ee92d46SJames Morris if (dh == NULL) 36842ee92d46SJames Morris break; 36852ee92d46SJames Morris 368648c62af6SEric Paris ad->u.net->sport = dh->dccph_sport; 368748c62af6SEric Paris ad->u.net->dport = dh->dccph_dport; 36882ee92d46SJames Morris break; 36892ee92d46SJames Morris } 36902ee92d46SJames Morris 36911da177e4SLinus Torvalds /* includes fragments */ 36921da177e4SLinus Torvalds default: 36931da177e4SLinus Torvalds break; 36941da177e4SLinus Torvalds } 36951da177e4SLinus Torvalds out: 36961da177e4SLinus Torvalds return ret; 36971da177e4SLinus Torvalds } 36981da177e4SLinus Torvalds 36991da177e4SLinus Torvalds #endif /* IPV6 */ 37001da177e4SLinus Torvalds 37012bf49690SThomas Liu static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad, 3702cf9481e2SDavid Howells char **_addrp, int src, u8 *proto) 37031da177e4SLinus Torvalds { 3704cf9481e2SDavid Howells char *addrp; 3705cf9481e2SDavid Howells int ret; 37061da177e4SLinus Torvalds 370748c62af6SEric Paris switch (ad->u.net->family) { 37081da177e4SLinus Torvalds case PF_INET: 370967f83cbfSVenkat Yekkirala ret = selinux_parse_skb_ipv4(skb, ad, proto); 3710cf9481e2SDavid Howells if (ret) 3711cf9481e2SDavid Howells goto parse_error; 371248c62af6SEric Paris addrp = (char *)(src ? &ad->u.net->v4info.saddr : 371348c62af6SEric Paris &ad->u.net->v4info.daddr); 3714cf9481e2SDavid Howells goto okay; 37151da177e4SLinus Torvalds 37161da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 37171da177e4SLinus Torvalds case PF_INET6: 371867f83cbfSVenkat Yekkirala ret = selinux_parse_skb_ipv6(skb, ad, proto); 3719cf9481e2SDavid Howells if (ret) 3720cf9481e2SDavid Howells goto parse_error; 372148c62af6SEric Paris addrp = (char *)(src ? &ad->u.net->v6info.saddr : 372248c62af6SEric Paris &ad->u.net->v6info.daddr); 3723cf9481e2SDavid Howells goto okay; 37241da177e4SLinus Torvalds #endif /* IPV6 */ 37251da177e4SLinus Torvalds default: 3726cf9481e2SDavid Howells addrp = NULL; 3727cf9481e2SDavid Howells goto okay; 37281da177e4SLinus Torvalds } 37291da177e4SLinus Torvalds 3730cf9481e2SDavid Howells parse_error: 373171f1cb05SPaul Moore printk(KERN_WARNING 373271f1cb05SPaul Moore "SELinux: failure in selinux_parse_skb()," 373371f1cb05SPaul Moore " unable to parse packet\n"); 37341da177e4SLinus Torvalds return ret; 3735cf9481e2SDavid Howells 3736cf9481e2SDavid Howells okay: 3737cf9481e2SDavid Howells if (_addrp) 3738cf9481e2SDavid Howells *_addrp = addrp; 3739cf9481e2SDavid Howells return 0; 37401da177e4SLinus Torvalds } 37411da177e4SLinus Torvalds 37424f6a993fSPaul Moore /** 3743220deb96SPaul Moore * selinux_skb_peerlbl_sid - Determine the peer label of a packet 37444f6a993fSPaul Moore * @skb: the packet 374575e22910SPaul Moore * @family: protocol family 3746220deb96SPaul Moore * @sid: the packet's peer label SID 37474f6a993fSPaul Moore * 37484f6a993fSPaul Moore * Description: 3749220deb96SPaul Moore * Check the various different forms of network peer labeling and determine 3750220deb96SPaul Moore * the peer label/SID for the packet; most of the magic actually occurs in 3751220deb96SPaul Moore * the security server function security_net_peersid_cmp(). The function 3752220deb96SPaul Moore * returns zero if the value in @sid is valid (although it may be SECSID_NULL) 3753220deb96SPaul Moore * or -EACCES if @sid is invalid due to inconsistencies with the different 3754220deb96SPaul Moore * peer labels. 37554f6a993fSPaul Moore * 37564f6a993fSPaul Moore */ 3757220deb96SPaul Moore static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid) 37584f6a993fSPaul Moore { 375971f1cb05SPaul Moore int err; 37604f6a993fSPaul Moore u32 xfrm_sid; 37614f6a993fSPaul Moore u32 nlbl_sid; 3762220deb96SPaul Moore u32 nlbl_type; 37634f6a993fSPaul Moore 3764bed4d7efSPaul Moore err = selinux_skb_xfrm_sid(skb, &xfrm_sid); 3765bed4d7efSPaul Moore if (unlikely(err)) 3766bed4d7efSPaul Moore return -EACCES; 3767bed4d7efSPaul Moore err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid); 3768bed4d7efSPaul Moore if (unlikely(err)) 3769bed4d7efSPaul Moore return -EACCES; 3770220deb96SPaul Moore 377171f1cb05SPaul Moore err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid); 377271f1cb05SPaul Moore if (unlikely(err)) { 377371f1cb05SPaul Moore printk(KERN_WARNING 377471f1cb05SPaul Moore "SELinux: failure in selinux_skb_peerlbl_sid()," 377571f1cb05SPaul Moore " unable to determine packet's peer label\n"); 3776220deb96SPaul Moore return -EACCES; 377771f1cb05SPaul Moore } 3778220deb96SPaul Moore 3779220deb96SPaul Moore return 0; 37804f6a993fSPaul Moore } 37814f6a993fSPaul Moore 37821da177e4SLinus Torvalds /* socket security operations */ 3783d4f2d978SPaul Moore 37842ad18bdfSHarry Ciao static int socket_sockcreate_sid(const struct task_security_struct *tsec, 37852ad18bdfSHarry Ciao u16 secclass, u32 *socksid) 3786d4f2d978SPaul Moore { 37872ad18bdfSHarry Ciao if (tsec->sockcreate_sid > SECSID_NULL) { 37882ad18bdfSHarry Ciao *socksid = tsec->sockcreate_sid; 37892ad18bdfSHarry Ciao return 0; 37902ad18bdfSHarry Ciao } 37912ad18bdfSHarry Ciao 37922ad18bdfSHarry Ciao return security_transition_sid(tsec->sid, tsec->sid, secclass, NULL, 37932ad18bdfSHarry Ciao socksid); 3794d4f2d978SPaul Moore } 3795d4f2d978SPaul Moore 3796253bfae6SPaul Moore static int sock_has_perm(struct task_struct *task, struct sock *sk, u32 perms) 37971da177e4SLinus Torvalds { 3798253bfae6SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 37992bf49690SThomas Liu struct common_audit_data ad; 380048c62af6SEric Paris struct lsm_network_audit net = {0,}; 3801253bfae6SPaul Moore u32 tsid = task_sid(task); 38021da177e4SLinus Torvalds 3803253bfae6SPaul Moore if (sksec->sid == SECINITSID_KERNEL) 3804253bfae6SPaul Moore return 0; 38051da177e4SLinus Torvalds 380650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 380748c62af6SEric Paris ad.u.net = &net; 380848c62af6SEric Paris ad.u.net->sk = sk; 38091da177e4SLinus Torvalds 3810253bfae6SPaul Moore return avc_has_perm(tsid, sksec->sid, sksec->sclass, perms, &ad); 38111da177e4SLinus Torvalds } 38121da177e4SLinus Torvalds 38131da177e4SLinus Torvalds static int selinux_socket_create(int family, int type, 38141da177e4SLinus Torvalds int protocol, int kern) 38151da177e4SLinus Torvalds { 38165fb49870SPaul Moore const struct task_security_struct *tsec = current_security(); 3817d4f2d978SPaul Moore u32 newsid; 3818275bb41eSDavid Howells u16 secclass; 38192ad18bdfSHarry Ciao int rc; 38201da177e4SLinus Torvalds 38211da177e4SLinus Torvalds if (kern) 3822d4f2d978SPaul Moore return 0; 38231da177e4SLinus Torvalds 3824275bb41eSDavid Howells secclass = socket_type_to_security_class(family, type, protocol); 38252ad18bdfSHarry Ciao rc = socket_sockcreate_sid(tsec, secclass, &newsid); 38262ad18bdfSHarry Ciao if (rc) 38272ad18bdfSHarry Ciao return rc; 38282ad18bdfSHarry Ciao 3829d4f2d978SPaul Moore return avc_has_perm(tsec->sid, newsid, secclass, SOCKET__CREATE, NULL); 38301da177e4SLinus Torvalds } 38311da177e4SLinus Torvalds 38327420ed23SVenkat Yekkirala static int selinux_socket_post_create(struct socket *sock, int family, 38331da177e4SLinus Torvalds int type, int protocol, int kern) 38341da177e4SLinus Torvalds { 38355fb49870SPaul Moore const struct task_security_struct *tsec = current_security(); 3836d4f2d978SPaul Moore struct inode_security_struct *isec = SOCK_INODE(sock)->i_security; 3837892c141eSVenkat Yekkirala struct sk_security_struct *sksec; 3838275bb41eSDavid Howells int err = 0; 3839275bb41eSDavid Howells 38402ad18bdfSHarry Ciao isec->sclass = socket_type_to_security_class(family, type, protocol); 38412ad18bdfSHarry Ciao 3842275bb41eSDavid Howells if (kern) 3843275bb41eSDavid Howells isec->sid = SECINITSID_KERNEL; 38442ad18bdfSHarry Ciao else { 38452ad18bdfSHarry Ciao err = socket_sockcreate_sid(tsec, isec->sclass, &(isec->sid)); 38462ad18bdfSHarry Ciao if (err) 38472ad18bdfSHarry Ciao return err; 38482ad18bdfSHarry Ciao } 3849275bb41eSDavid Howells 38501da177e4SLinus Torvalds isec->initialized = 1; 38511da177e4SLinus Torvalds 3852892c141eSVenkat Yekkirala if (sock->sk) { 3853892c141eSVenkat Yekkirala sksec = sock->sk->sk_security; 3854892c141eSVenkat Yekkirala sksec->sid = isec->sid; 3855220deb96SPaul Moore sksec->sclass = isec->sclass; 3856389fb800SPaul Moore err = selinux_netlbl_socket_post_create(sock->sk, family); 3857892c141eSVenkat Yekkirala } 3858892c141eSVenkat Yekkirala 38597420ed23SVenkat Yekkirala return err; 38601da177e4SLinus Torvalds } 38611da177e4SLinus Torvalds 38621da177e4SLinus Torvalds /* Range of port numbers used to automatically bind. 38631da177e4SLinus Torvalds Need to determine whether we should perform a name_bind 38641da177e4SLinus Torvalds permission check between the socket and the port number. */ 38651da177e4SLinus Torvalds 38661da177e4SLinus Torvalds static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen) 38671da177e4SLinus Torvalds { 3868253bfae6SPaul Moore struct sock *sk = sock->sk; 38691da177e4SLinus Torvalds u16 family; 38701da177e4SLinus Torvalds int err; 38711da177e4SLinus Torvalds 3872253bfae6SPaul Moore err = sock_has_perm(current, sk, SOCKET__BIND); 38731da177e4SLinus Torvalds if (err) 38741da177e4SLinus Torvalds goto out; 38751da177e4SLinus Torvalds 38761da177e4SLinus Torvalds /* 38771da177e4SLinus Torvalds * If PF_INET or PF_INET6, check name_bind permission for the port. 387813402580SJames Morris * Multiple address binding for SCTP is not supported yet: we just 387913402580SJames Morris * check the first address now. 38801da177e4SLinus Torvalds */ 3881253bfae6SPaul Moore family = sk->sk_family; 38821da177e4SLinus Torvalds if (family == PF_INET || family == PF_INET6) { 38831da177e4SLinus Torvalds char *addrp; 3884253bfae6SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 38852bf49690SThomas Liu struct common_audit_data ad; 388648c62af6SEric Paris struct lsm_network_audit net = {0,}; 38871da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 38881da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 38891da177e4SLinus Torvalds unsigned short snum; 3890e399f982SJames Morris u32 sid, node_perm; 38911da177e4SLinus Torvalds 38921da177e4SLinus Torvalds if (family == PF_INET) { 38931da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 38941da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 38951da177e4SLinus Torvalds addrp = (char *)&addr4->sin_addr.s_addr; 38961da177e4SLinus Torvalds } else { 38971da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 38981da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 38991da177e4SLinus Torvalds addrp = (char *)&addr6->sin6_addr.s6_addr; 39001da177e4SLinus Torvalds } 39011da177e4SLinus Torvalds 3902227b60f5SStephen Hemminger if (snum) { 3903227b60f5SStephen Hemminger int low, high; 3904227b60f5SStephen Hemminger 3905227b60f5SStephen Hemminger inet_get_local_port_range(&low, &high); 3906227b60f5SStephen Hemminger 3907227b60f5SStephen Hemminger if (snum < max(PROT_SOCK, low) || snum > high) { 39083e112172SPaul Moore err = sel_netport_sid(sk->sk_protocol, 39093e112172SPaul Moore snum, &sid); 39101da177e4SLinus Torvalds if (err) 39111da177e4SLinus Torvalds goto out; 391250c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 391348c62af6SEric Paris ad.u.net = &net; 391448c62af6SEric Paris ad.u.net->sport = htons(snum); 391548c62af6SEric Paris ad.u.net->family = family; 3916253bfae6SPaul Moore err = avc_has_perm(sksec->sid, sid, 3917253bfae6SPaul Moore sksec->sclass, 39181da177e4SLinus Torvalds SOCKET__NAME_BIND, &ad); 39191da177e4SLinus Torvalds if (err) 39201da177e4SLinus Torvalds goto out; 39211da177e4SLinus Torvalds } 3922227b60f5SStephen Hemminger } 39231da177e4SLinus Torvalds 3924253bfae6SPaul Moore switch (sksec->sclass) { 392513402580SJames Morris case SECCLASS_TCP_SOCKET: 39261da177e4SLinus Torvalds node_perm = TCP_SOCKET__NODE_BIND; 39271da177e4SLinus Torvalds break; 39281da177e4SLinus Torvalds 392913402580SJames Morris case SECCLASS_UDP_SOCKET: 39301da177e4SLinus Torvalds node_perm = UDP_SOCKET__NODE_BIND; 39311da177e4SLinus Torvalds break; 39321da177e4SLinus Torvalds 39332ee92d46SJames Morris case SECCLASS_DCCP_SOCKET: 39342ee92d46SJames Morris node_perm = DCCP_SOCKET__NODE_BIND; 39352ee92d46SJames Morris break; 39362ee92d46SJames Morris 39371da177e4SLinus Torvalds default: 39381da177e4SLinus Torvalds node_perm = RAWIP_SOCKET__NODE_BIND; 39391da177e4SLinus Torvalds break; 39401da177e4SLinus Torvalds } 39411da177e4SLinus Torvalds 3942224dfbd8SPaul Moore err = sel_netnode_sid(addrp, family, &sid); 39431da177e4SLinus Torvalds if (err) 39441da177e4SLinus Torvalds goto out; 39451da177e4SLinus Torvalds 394650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 394748c62af6SEric Paris ad.u.net = &net; 394848c62af6SEric Paris ad.u.net->sport = htons(snum); 394948c62af6SEric Paris ad.u.net->family = family; 39501da177e4SLinus Torvalds 39511da177e4SLinus Torvalds if (family == PF_INET) 395248c62af6SEric Paris ad.u.net->v4info.saddr = addr4->sin_addr.s_addr; 39531da177e4SLinus Torvalds else 395448c62af6SEric Paris ad.u.net->v6info.saddr = addr6->sin6_addr; 39551da177e4SLinus Torvalds 3956253bfae6SPaul Moore err = avc_has_perm(sksec->sid, sid, 3957253bfae6SPaul Moore sksec->sclass, node_perm, &ad); 39581da177e4SLinus Torvalds if (err) 39591da177e4SLinus Torvalds goto out; 39601da177e4SLinus Torvalds } 39611da177e4SLinus Torvalds out: 39621da177e4SLinus Torvalds return err; 39631da177e4SLinus Torvalds } 39641da177e4SLinus Torvalds 39651da177e4SLinus Torvalds static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen) 39661da177e4SLinus Torvalds { 3967014ab19aSPaul Moore struct sock *sk = sock->sk; 3968253bfae6SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 39691da177e4SLinus Torvalds int err; 39701da177e4SLinus Torvalds 3971253bfae6SPaul Moore err = sock_has_perm(current, sk, SOCKET__CONNECT); 39721da177e4SLinus Torvalds if (err) 39731da177e4SLinus Torvalds return err; 39741da177e4SLinus Torvalds 39751da177e4SLinus Torvalds /* 39762ee92d46SJames Morris * If a TCP or DCCP socket, check name_connect permission for the port. 39771da177e4SLinus Torvalds */ 3978253bfae6SPaul Moore if (sksec->sclass == SECCLASS_TCP_SOCKET || 3979253bfae6SPaul Moore sksec->sclass == SECCLASS_DCCP_SOCKET) { 39802bf49690SThomas Liu struct common_audit_data ad; 398148c62af6SEric Paris struct lsm_network_audit net = {0,}; 39821da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 39831da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 39841da177e4SLinus Torvalds unsigned short snum; 39852ee92d46SJames Morris u32 sid, perm; 39861da177e4SLinus Torvalds 39871da177e4SLinus Torvalds if (sk->sk_family == PF_INET) { 39881da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 3989911656f8SStephen Smalley if (addrlen < sizeof(struct sockaddr_in)) 39901da177e4SLinus Torvalds return -EINVAL; 39911da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 39921da177e4SLinus Torvalds } else { 39931da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 3994911656f8SStephen Smalley if (addrlen < SIN6_LEN_RFC2133) 39951da177e4SLinus Torvalds return -EINVAL; 39961da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 39971da177e4SLinus Torvalds } 39981da177e4SLinus Torvalds 39993e112172SPaul Moore err = sel_netport_sid(sk->sk_protocol, snum, &sid); 40001da177e4SLinus Torvalds if (err) 40011da177e4SLinus Torvalds goto out; 40021da177e4SLinus Torvalds 4003253bfae6SPaul Moore perm = (sksec->sclass == SECCLASS_TCP_SOCKET) ? 40042ee92d46SJames Morris TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT; 40052ee92d46SJames Morris 400650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 400748c62af6SEric Paris ad.u.net = &net; 400848c62af6SEric Paris ad.u.net->dport = htons(snum); 400948c62af6SEric Paris ad.u.net->family = sk->sk_family; 4010253bfae6SPaul Moore err = avc_has_perm(sksec->sid, sid, sksec->sclass, perm, &ad); 40111da177e4SLinus Torvalds if (err) 40121da177e4SLinus Torvalds goto out; 40131da177e4SLinus Torvalds } 40141da177e4SLinus Torvalds 4015014ab19aSPaul Moore err = selinux_netlbl_socket_connect(sk, address); 4016014ab19aSPaul Moore 40171da177e4SLinus Torvalds out: 40181da177e4SLinus Torvalds return err; 40191da177e4SLinus Torvalds } 40201da177e4SLinus Torvalds 40211da177e4SLinus Torvalds static int selinux_socket_listen(struct socket *sock, int backlog) 40221da177e4SLinus Torvalds { 4023253bfae6SPaul Moore return sock_has_perm(current, sock->sk, SOCKET__LISTEN); 40241da177e4SLinus Torvalds } 40251da177e4SLinus Torvalds 40261da177e4SLinus Torvalds static int selinux_socket_accept(struct socket *sock, struct socket *newsock) 40271da177e4SLinus Torvalds { 40281da177e4SLinus Torvalds int err; 40291da177e4SLinus Torvalds struct inode_security_struct *isec; 40301da177e4SLinus Torvalds struct inode_security_struct *newisec; 40311da177e4SLinus Torvalds 4032253bfae6SPaul Moore err = sock_has_perm(current, sock->sk, SOCKET__ACCEPT); 40331da177e4SLinus Torvalds if (err) 40341da177e4SLinus Torvalds return err; 40351da177e4SLinus Torvalds 40361da177e4SLinus Torvalds newisec = SOCK_INODE(newsock)->i_security; 40371da177e4SLinus Torvalds 40381da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 40391da177e4SLinus Torvalds newisec->sclass = isec->sclass; 40401da177e4SLinus Torvalds newisec->sid = isec->sid; 40411da177e4SLinus Torvalds newisec->initialized = 1; 40421da177e4SLinus Torvalds 40431da177e4SLinus Torvalds return 0; 40441da177e4SLinus Torvalds } 40451da177e4SLinus Torvalds 40461da177e4SLinus Torvalds static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg, 40471da177e4SLinus Torvalds int size) 40481da177e4SLinus Torvalds { 4049253bfae6SPaul Moore return sock_has_perm(current, sock->sk, SOCKET__WRITE); 40501da177e4SLinus Torvalds } 40511da177e4SLinus Torvalds 40521da177e4SLinus Torvalds static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg, 40531da177e4SLinus Torvalds int size, int flags) 40541da177e4SLinus Torvalds { 4055253bfae6SPaul Moore return sock_has_perm(current, sock->sk, SOCKET__READ); 40561da177e4SLinus Torvalds } 40571da177e4SLinus Torvalds 40581da177e4SLinus Torvalds static int selinux_socket_getsockname(struct socket *sock) 40591da177e4SLinus Torvalds { 4060253bfae6SPaul Moore return sock_has_perm(current, sock->sk, SOCKET__GETATTR); 40611da177e4SLinus Torvalds } 40621da177e4SLinus Torvalds 40631da177e4SLinus Torvalds static int selinux_socket_getpeername(struct socket *sock) 40641da177e4SLinus Torvalds { 4065253bfae6SPaul Moore return sock_has_perm(current, sock->sk, SOCKET__GETATTR); 40661da177e4SLinus Torvalds } 40671da177e4SLinus Torvalds 40681da177e4SLinus Torvalds static int selinux_socket_setsockopt(struct socket *sock, int level, int optname) 40691da177e4SLinus Torvalds { 4070f8687afeSPaul Moore int err; 4071f8687afeSPaul Moore 4072253bfae6SPaul Moore err = sock_has_perm(current, sock->sk, SOCKET__SETOPT); 4073f8687afeSPaul Moore if (err) 4074f8687afeSPaul Moore return err; 4075f8687afeSPaul Moore 4076f8687afeSPaul Moore return selinux_netlbl_socket_setsockopt(sock, level, optname); 40771da177e4SLinus Torvalds } 40781da177e4SLinus Torvalds 40791da177e4SLinus Torvalds static int selinux_socket_getsockopt(struct socket *sock, int level, 40801da177e4SLinus Torvalds int optname) 40811da177e4SLinus Torvalds { 4082253bfae6SPaul Moore return sock_has_perm(current, sock->sk, SOCKET__GETOPT); 40831da177e4SLinus Torvalds } 40841da177e4SLinus Torvalds 40851da177e4SLinus Torvalds static int selinux_socket_shutdown(struct socket *sock, int how) 40861da177e4SLinus Torvalds { 4087253bfae6SPaul Moore return sock_has_perm(current, sock->sk, SOCKET__SHUTDOWN); 40881da177e4SLinus Torvalds } 40891da177e4SLinus Torvalds 40903610cda5SDavid S. Miller static int selinux_socket_unix_stream_connect(struct sock *sock, 40913610cda5SDavid S. Miller struct sock *other, 40921da177e4SLinus Torvalds struct sock *newsk) 40931da177e4SLinus Torvalds { 40943610cda5SDavid S. Miller struct sk_security_struct *sksec_sock = sock->sk_security; 40953610cda5SDavid S. Miller struct sk_security_struct *sksec_other = other->sk_security; 40964d1e2451SPaul Moore struct sk_security_struct *sksec_new = newsk->sk_security; 40972bf49690SThomas Liu struct common_audit_data ad; 409848c62af6SEric Paris struct lsm_network_audit net = {0,}; 40991da177e4SLinus Torvalds int err; 41001da177e4SLinus Torvalds 410150c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 410248c62af6SEric Paris ad.u.net = &net; 410348c62af6SEric Paris ad.u.net->sk = other; 41041da177e4SLinus Torvalds 41054d1e2451SPaul Moore err = avc_has_perm(sksec_sock->sid, sksec_other->sid, 41064d1e2451SPaul Moore sksec_other->sclass, 41071da177e4SLinus Torvalds UNIX_STREAM_SOCKET__CONNECTTO, &ad); 41081da177e4SLinus Torvalds if (err) 41091da177e4SLinus Torvalds return err; 41101da177e4SLinus Torvalds 41111da177e4SLinus Torvalds /* server child socket */ 41124d1e2451SPaul Moore sksec_new->peer_sid = sksec_sock->sid; 41134d1e2451SPaul Moore err = security_sid_mls_copy(sksec_other->sid, sksec_sock->sid, 41144d1e2451SPaul Moore &sksec_new->sid); 41154d1e2451SPaul Moore if (err) 41164237c75cSVenkat Yekkirala return err; 41174d1e2451SPaul Moore 41184d1e2451SPaul Moore /* connecting socket */ 41194d1e2451SPaul Moore sksec_sock->peer_sid = sksec_new->sid; 41204d1e2451SPaul Moore 41214d1e2451SPaul Moore return 0; 41221da177e4SLinus Torvalds } 41231da177e4SLinus Torvalds 41241da177e4SLinus Torvalds static int selinux_socket_unix_may_send(struct socket *sock, 41251da177e4SLinus Torvalds struct socket *other) 41261da177e4SLinus Torvalds { 4127253bfae6SPaul Moore struct sk_security_struct *ssec = sock->sk->sk_security; 4128253bfae6SPaul Moore struct sk_security_struct *osec = other->sk->sk_security; 41292bf49690SThomas Liu struct common_audit_data ad; 413048c62af6SEric Paris struct lsm_network_audit net = {0,}; 41311da177e4SLinus Torvalds 413250c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 413348c62af6SEric Paris ad.u.net = &net; 413448c62af6SEric Paris ad.u.net->sk = other->sk; 41351da177e4SLinus Torvalds 4136253bfae6SPaul Moore return avc_has_perm(ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO, 4137253bfae6SPaul Moore &ad); 41381da177e4SLinus Torvalds } 41391da177e4SLinus Torvalds 4140effad8dfSPaul Moore static int selinux_inet_sys_rcv_skb(int ifindex, char *addrp, u16 family, 4141effad8dfSPaul Moore u32 peer_sid, 41422bf49690SThomas Liu struct common_audit_data *ad) 4143effad8dfSPaul Moore { 4144effad8dfSPaul Moore int err; 4145effad8dfSPaul Moore u32 if_sid; 4146effad8dfSPaul Moore u32 node_sid; 4147effad8dfSPaul Moore 4148effad8dfSPaul Moore err = sel_netif_sid(ifindex, &if_sid); 4149effad8dfSPaul Moore if (err) 4150effad8dfSPaul Moore return err; 4151effad8dfSPaul Moore err = avc_has_perm(peer_sid, if_sid, 4152effad8dfSPaul Moore SECCLASS_NETIF, NETIF__INGRESS, ad); 4153effad8dfSPaul Moore if (err) 4154effad8dfSPaul Moore return err; 4155effad8dfSPaul Moore 4156effad8dfSPaul Moore err = sel_netnode_sid(addrp, family, &node_sid); 4157effad8dfSPaul Moore if (err) 4158effad8dfSPaul Moore return err; 4159effad8dfSPaul Moore return avc_has_perm(peer_sid, node_sid, 4160effad8dfSPaul Moore SECCLASS_NODE, NODE__RECVFROM, ad); 4161effad8dfSPaul Moore } 4162effad8dfSPaul Moore 4163220deb96SPaul Moore static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb, 4164d8395c87SPaul Moore u16 family) 4165220deb96SPaul Moore { 4166277d342fSPaul Moore int err = 0; 4167220deb96SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 4168220deb96SPaul Moore u32 sk_sid = sksec->sid; 41692bf49690SThomas Liu struct common_audit_data ad; 417048c62af6SEric Paris struct lsm_network_audit net = {0,}; 4171d8395c87SPaul Moore char *addrp; 4172d8395c87SPaul Moore 417350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 417448c62af6SEric Paris ad.u.net = &net; 417548c62af6SEric Paris ad.u.net->netif = skb->skb_iif; 417648c62af6SEric Paris ad.u.net->family = family; 4177d8395c87SPaul Moore err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); 4178d8395c87SPaul Moore if (err) 4179d8395c87SPaul Moore return err; 4180220deb96SPaul Moore 418158bfbb51SPaul Moore if (selinux_secmark_enabled()) { 4182220deb96SPaul Moore err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET, 4183d8395c87SPaul Moore PACKET__RECV, &ad); 4184220deb96SPaul Moore if (err) 4185220deb96SPaul Moore return err; 418658bfbb51SPaul Moore } 4187220deb96SPaul Moore 4188d8395c87SPaul Moore err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad); 4189220deb96SPaul Moore if (err) 4190220deb96SPaul Moore return err; 4191d8395c87SPaul Moore err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad); 4192220deb96SPaul Moore 41934e5ab4cbSJames Morris return err; 41944e5ab4cbSJames Morris } 4195d28d1e08STrent Jaeger 41964e5ab4cbSJames Morris static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) 41974e5ab4cbSJames Morris { 4198220deb96SPaul Moore int err; 41994237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 4200220deb96SPaul Moore u16 family = sk->sk_family; 4201220deb96SPaul Moore u32 sk_sid = sksec->sid; 42022bf49690SThomas Liu struct common_audit_data ad; 420348c62af6SEric Paris struct lsm_network_audit net = {0,}; 4204220deb96SPaul Moore char *addrp; 4205d8395c87SPaul Moore u8 secmark_active; 4206d8395c87SPaul Moore u8 peerlbl_active; 42074e5ab4cbSJames Morris 42084e5ab4cbSJames Morris if (family != PF_INET && family != PF_INET6) 4209220deb96SPaul Moore return 0; 42104e5ab4cbSJames Morris 42114e5ab4cbSJames Morris /* Handle mapped IPv4 packets arriving via IPv6 sockets */ 421287fcd70dSAl Viro if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 42134e5ab4cbSJames Morris family = PF_INET; 42144e5ab4cbSJames Morris 4215d8395c87SPaul Moore /* If any sort of compatibility mode is enabled then handoff processing 4216d8395c87SPaul Moore * to the selinux_sock_rcv_skb_compat() function to deal with the 4217d8395c87SPaul Moore * special handling. We do this in an attempt to keep this function 4218d8395c87SPaul Moore * as fast and as clean as possible. */ 421958bfbb51SPaul Moore if (!selinux_policycap_netpeer) 4220d8395c87SPaul Moore return selinux_sock_rcv_skb_compat(sk, skb, family); 4221d8395c87SPaul Moore 4222d8395c87SPaul Moore secmark_active = selinux_secmark_enabled(); 42232be4d74fSChris PeBenito peerlbl_active = selinux_peerlbl_enabled(); 4224d8395c87SPaul Moore if (!secmark_active && !peerlbl_active) 4225d8395c87SPaul Moore return 0; 4226d8395c87SPaul Moore 422750c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 422848c62af6SEric Paris ad.u.net = &net; 422948c62af6SEric Paris ad.u.net->netif = skb->skb_iif; 423048c62af6SEric Paris ad.u.net->family = family; 4231224dfbd8SPaul Moore err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); 42324e5ab4cbSJames Morris if (err) 4233220deb96SPaul Moore return err; 42344e5ab4cbSJames Morris 4235d8395c87SPaul Moore if (peerlbl_active) { 4236d621d35eSPaul Moore u32 peer_sid; 4237220deb96SPaul Moore 4238220deb96SPaul Moore err = selinux_skb_peerlbl_sid(skb, family, &peer_sid); 4239220deb96SPaul Moore if (err) 4240220deb96SPaul Moore return err; 42418964be4aSEric Dumazet err = selinux_inet_sys_rcv_skb(skb->skb_iif, addrp, family, 4242effad8dfSPaul Moore peer_sid, &ad); 4243dfaebe98SPaul Moore if (err) { 4244dfaebe98SPaul Moore selinux_netlbl_err(skb, err, 0); 4245effad8dfSPaul Moore return err; 4246dfaebe98SPaul Moore } 4247d621d35eSPaul Moore err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER, 4248d621d35eSPaul Moore PEER__RECV, &ad); 4249dfaebe98SPaul Moore if (err) 4250dfaebe98SPaul Moore selinux_netlbl_err(skb, err, 0); 4251d621d35eSPaul Moore } 4252d621d35eSPaul Moore 4253d8395c87SPaul Moore if (secmark_active) { 4254effad8dfSPaul Moore err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET, 4255effad8dfSPaul Moore PACKET__RECV, &ad); 4256effad8dfSPaul Moore if (err) 4257effad8dfSPaul Moore return err; 4258effad8dfSPaul Moore } 4259effad8dfSPaul Moore 4260d621d35eSPaul Moore return err; 42611da177e4SLinus Torvalds } 42621da177e4SLinus Torvalds 42632c7946a7SCatherine Zhang static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval, 42641da177e4SLinus Torvalds int __user *optlen, unsigned len) 42651da177e4SLinus Torvalds { 42661da177e4SLinus Torvalds int err = 0; 42671da177e4SLinus Torvalds char *scontext; 42681da177e4SLinus Torvalds u32 scontext_len; 4269253bfae6SPaul Moore struct sk_security_struct *sksec = sock->sk->sk_security; 42703de4bab5SPaul Moore u32 peer_sid = SECSID_NULL; 42711da177e4SLinus Torvalds 4272253bfae6SPaul Moore if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET || 4273253bfae6SPaul Moore sksec->sclass == SECCLASS_TCP_SOCKET) 4274dd3e7836SEric Paris peer_sid = sksec->peer_sid; 4275253bfae6SPaul Moore if (peer_sid == SECSID_NULL) 4276253bfae6SPaul Moore return -ENOPROTOOPT; 42771da177e4SLinus Torvalds 42782c7946a7SCatherine Zhang err = security_sid_to_context(peer_sid, &scontext, &scontext_len); 42791da177e4SLinus Torvalds if (err) 4280253bfae6SPaul Moore return err; 42811da177e4SLinus Torvalds 42821da177e4SLinus Torvalds if (scontext_len > len) { 42831da177e4SLinus Torvalds err = -ERANGE; 42841da177e4SLinus Torvalds goto out_len; 42851da177e4SLinus Torvalds } 42861da177e4SLinus Torvalds 42871da177e4SLinus Torvalds if (copy_to_user(optval, scontext, scontext_len)) 42881da177e4SLinus Torvalds err = -EFAULT; 42891da177e4SLinus Torvalds 42901da177e4SLinus Torvalds out_len: 42911da177e4SLinus Torvalds if (put_user(scontext_len, optlen)) 42921da177e4SLinus Torvalds err = -EFAULT; 42931da177e4SLinus Torvalds kfree(scontext); 42941da177e4SLinus Torvalds return err; 42951da177e4SLinus Torvalds } 42961da177e4SLinus Torvalds 4297dc49c1f9SCatherine Zhang static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid) 42982c7946a7SCatherine Zhang { 4299dc49c1f9SCatherine Zhang u32 peer_secid = SECSID_NULL; 430075e22910SPaul Moore u16 family; 4301877ce7c1SCatherine Zhang 4302aa862900SPaul Moore if (skb && skb->protocol == htons(ETH_P_IP)) 4303aa862900SPaul Moore family = PF_INET; 4304aa862900SPaul Moore else if (skb && skb->protocol == htons(ETH_P_IPV6)) 4305aa862900SPaul Moore family = PF_INET6; 4306aa862900SPaul Moore else if (sock) 430775e22910SPaul Moore family = sock->sk->sk_family; 430875e22910SPaul Moore else 430975e22910SPaul Moore goto out; 431075e22910SPaul Moore 431175e22910SPaul Moore if (sock && family == PF_UNIX) 4312713a04aeSAhmed S. Darwish selinux_inode_getsecid(SOCK_INODE(sock), &peer_secid); 43133de4bab5SPaul Moore else if (skb) 4314220deb96SPaul Moore selinux_skb_peerlbl_sid(skb, family, &peer_secid); 43152c7946a7SCatherine Zhang 431675e22910SPaul Moore out: 4317dc49c1f9SCatherine Zhang *secid = peer_secid; 431875e22910SPaul Moore if (peer_secid == SECSID_NULL) 431975e22910SPaul Moore return -EINVAL; 432075e22910SPaul Moore return 0; 43212c7946a7SCatherine Zhang } 43222c7946a7SCatherine Zhang 43237d877f3bSAl Viro static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority) 43241da177e4SLinus Torvalds { 432584914b7eSPaul Moore struct sk_security_struct *sksec; 432684914b7eSPaul Moore 432784914b7eSPaul Moore sksec = kzalloc(sizeof(*sksec), priority); 432884914b7eSPaul Moore if (!sksec) 432984914b7eSPaul Moore return -ENOMEM; 433084914b7eSPaul Moore 433184914b7eSPaul Moore sksec->peer_sid = SECINITSID_UNLABELED; 433284914b7eSPaul Moore sksec->sid = SECINITSID_UNLABELED; 433384914b7eSPaul Moore selinux_netlbl_sk_security_reset(sksec); 433484914b7eSPaul Moore sk->sk_security = sksec; 433584914b7eSPaul Moore 433684914b7eSPaul Moore return 0; 43371da177e4SLinus Torvalds } 43381da177e4SLinus Torvalds 43391da177e4SLinus Torvalds static void selinux_sk_free_security(struct sock *sk) 43401da177e4SLinus Torvalds { 434184914b7eSPaul Moore struct sk_security_struct *sksec = sk->sk_security; 434284914b7eSPaul Moore 434384914b7eSPaul Moore sk->sk_security = NULL; 434484914b7eSPaul Moore selinux_netlbl_sk_security_free(sksec); 434584914b7eSPaul Moore kfree(sksec); 43461da177e4SLinus Torvalds } 43471da177e4SLinus Torvalds 4348892c141eSVenkat Yekkirala static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk) 4349892c141eSVenkat Yekkirala { 4350dd3e7836SEric Paris struct sk_security_struct *sksec = sk->sk_security; 4351dd3e7836SEric Paris struct sk_security_struct *newsksec = newsk->sk_security; 4352892c141eSVenkat Yekkirala 4353dd3e7836SEric Paris newsksec->sid = sksec->sid; 4354dd3e7836SEric Paris newsksec->peer_sid = sksec->peer_sid; 4355dd3e7836SEric Paris newsksec->sclass = sksec->sclass; 435699f59ed0SPaul Moore 4357dd3e7836SEric Paris selinux_netlbl_sk_security_reset(newsksec); 4358892c141eSVenkat Yekkirala } 4359892c141eSVenkat Yekkirala 4360beb8d13bSVenkat Yekkirala static void selinux_sk_getsecid(struct sock *sk, u32 *secid) 4361d28d1e08STrent Jaeger { 4362d28d1e08STrent Jaeger if (!sk) 4363beb8d13bSVenkat Yekkirala *secid = SECINITSID_ANY_SOCKET; 4364892c141eSVenkat Yekkirala else { 4365892c141eSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 4366d28d1e08STrent Jaeger 4367beb8d13bSVenkat Yekkirala *secid = sksec->sid; 4368892c141eSVenkat Yekkirala } 4369d28d1e08STrent Jaeger } 4370d28d1e08STrent Jaeger 43719a673e56SAdrian Bunk static void selinux_sock_graft(struct sock *sk, struct socket *parent) 43724237c75cSVenkat Yekkirala { 43734237c75cSVenkat Yekkirala struct inode_security_struct *isec = SOCK_INODE(parent)->i_security; 43744237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 43754237c75cSVenkat Yekkirala 43762148ccc4SDavid Woodhouse if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 || 43772148ccc4SDavid Woodhouse sk->sk_family == PF_UNIX) 43784237c75cSVenkat Yekkirala isec->sid = sksec->sid; 4379220deb96SPaul Moore sksec->sclass = isec->sclass; 43804237c75cSVenkat Yekkirala } 43814237c75cSVenkat Yekkirala 43829a673e56SAdrian Bunk static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb, 43834237c75cSVenkat Yekkirala struct request_sock *req) 43844237c75cSVenkat Yekkirala { 43854237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 43864237c75cSVenkat Yekkirala int err; 4387aa862900SPaul Moore u16 family = sk->sk_family; 43887420ed23SVenkat Yekkirala u32 newsid; 43894237c75cSVenkat Yekkirala u32 peersid; 43904237c75cSVenkat Yekkirala 4391aa862900SPaul Moore /* handle mapped IPv4 packets arriving via IPv6 sockets */ 4392aa862900SPaul Moore if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 4393aa862900SPaul Moore family = PF_INET; 4394aa862900SPaul Moore 4395aa862900SPaul Moore err = selinux_skb_peerlbl_sid(skb, family, &peersid); 4396220deb96SPaul Moore if (err) 4397220deb96SPaul Moore return err; 4398a51c64f1SVenkat Yekkirala if (peersid == SECSID_NULL) { 4399a51c64f1SVenkat Yekkirala req->secid = sksec->sid; 44003de4bab5SPaul Moore req->peer_secid = SECSID_NULL; 4401389fb800SPaul Moore } else { 44024237c75cSVenkat Yekkirala err = security_sid_mls_copy(sksec->sid, peersid, &newsid); 44034237c75cSVenkat Yekkirala if (err) 44044237c75cSVenkat Yekkirala return err; 44054237c75cSVenkat Yekkirala req->secid = newsid; 44066b877699SVenkat Yekkirala req->peer_secid = peersid; 4407389fb800SPaul Moore } 4408389fb800SPaul Moore 4409389fb800SPaul Moore return selinux_netlbl_inet_conn_request(req, family); 44104237c75cSVenkat Yekkirala } 44114237c75cSVenkat Yekkirala 44129a673e56SAdrian Bunk static void selinux_inet_csk_clone(struct sock *newsk, 44139a673e56SAdrian Bunk const struct request_sock *req) 44144237c75cSVenkat Yekkirala { 44154237c75cSVenkat Yekkirala struct sk_security_struct *newsksec = newsk->sk_security; 44164237c75cSVenkat Yekkirala 44174237c75cSVenkat Yekkirala newsksec->sid = req->secid; 44186b877699SVenkat Yekkirala newsksec->peer_sid = req->peer_secid; 44194237c75cSVenkat Yekkirala /* NOTE: Ideally, we should also get the isec->sid for the 44204237c75cSVenkat Yekkirala new socket in sync, but we don't have the isec available yet. 44214237c75cSVenkat Yekkirala So we will wait until sock_graft to do it, by which 44224237c75cSVenkat Yekkirala time it will have been created and available. */ 442399f59ed0SPaul Moore 44249f2ad665SPaul Moore /* We don't need to take any sort of lock here as we are the only 44259f2ad665SPaul Moore * thread with access to newsksec */ 4426389fb800SPaul Moore selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family); 44274237c75cSVenkat Yekkirala } 44284237c75cSVenkat Yekkirala 4429014ab19aSPaul Moore static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb) 44306b877699SVenkat Yekkirala { 4431aa862900SPaul Moore u16 family = sk->sk_family; 44326b877699SVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 44336b877699SVenkat Yekkirala 4434aa862900SPaul Moore /* handle mapped IPv4 packets arriving via IPv6 sockets */ 4435aa862900SPaul Moore if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 4436aa862900SPaul Moore family = PF_INET; 4437aa862900SPaul Moore 4438aa862900SPaul Moore selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid); 44396b877699SVenkat Yekkirala } 44406b877699SVenkat Yekkirala 4441ca10b9e9SEric Dumazet static void selinux_skb_owned_by(struct sk_buff *skb, struct sock *sk) 4442ca10b9e9SEric Dumazet { 4443ca10b9e9SEric Dumazet skb_set_owner_w(skb, sk); 4444ca10b9e9SEric Dumazet } 4445ca10b9e9SEric Dumazet 44462606fd1fSEric Paris static int selinux_secmark_relabel_packet(u32 sid) 44472606fd1fSEric Paris { 44482606fd1fSEric Paris const struct task_security_struct *__tsec; 44492606fd1fSEric Paris u32 tsid; 44502606fd1fSEric Paris 44512606fd1fSEric Paris __tsec = current_security(); 44522606fd1fSEric Paris tsid = __tsec->sid; 44532606fd1fSEric Paris 44542606fd1fSEric Paris return avc_has_perm(tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, NULL); 44552606fd1fSEric Paris } 44562606fd1fSEric Paris 44572606fd1fSEric Paris static void selinux_secmark_refcount_inc(void) 44582606fd1fSEric Paris { 44592606fd1fSEric Paris atomic_inc(&selinux_secmark_refcount); 44602606fd1fSEric Paris } 44612606fd1fSEric Paris 44622606fd1fSEric Paris static void selinux_secmark_refcount_dec(void) 44632606fd1fSEric Paris { 44642606fd1fSEric Paris atomic_dec(&selinux_secmark_refcount); 44652606fd1fSEric Paris } 44662606fd1fSEric Paris 44679a673e56SAdrian Bunk static void selinux_req_classify_flow(const struct request_sock *req, 44689a673e56SAdrian Bunk struct flowi *fl) 44694237c75cSVenkat Yekkirala { 44701d28f42cSDavid S. Miller fl->flowi_secid = req->secid; 44714237c75cSVenkat Yekkirala } 44724237c75cSVenkat Yekkirala 44735dbbaf2dSPaul Moore static int selinux_tun_dev_alloc_security(void **security) 44745dbbaf2dSPaul Moore { 44755dbbaf2dSPaul Moore struct tun_security_struct *tunsec; 44765dbbaf2dSPaul Moore 44775dbbaf2dSPaul Moore tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL); 44785dbbaf2dSPaul Moore if (!tunsec) 44795dbbaf2dSPaul Moore return -ENOMEM; 44805dbbaf2dSPaul Moore tunsec->sid = current_sid(); 44815dbbaf2dSPaul Moore 44825dbbaf2dSPaul Moore *security = tunsec; 44835dbbaf2dSPaul Moore return 0; 44845dbbaf2dSPaul Moore } 44855dbbaf2dSPaul Moore 44865dbbaf2dSPaul Moore static void selinux_tun_dev_free_security(void *security) 44875dbbaf2dSPaul Moore { 44885dbbaf2dSPaul Moore kfree(security); 44895dbbaf2dSPaul Moore } 44905dbbaf2dSPaul Moore 4491ed6d76e4SPaul Moore static int selinux_tun_dev_create(void) 4492ed6d76e4SPaul Moore { 4493ed6d76e4SPaul Moore u32 sid = current_sid(); 4494ed6d76e4SPaul Moore 4495ed6d76e4SPaul Moore /* we aren't taking into account the "sockcreate" SID since the socket 4496ed6d76e4SPaul Moore * that is being created here is not a socket in the traditional sense, 4497ed6d76e4SPaul Moore * instead it is a private sock, accessible only to the kernel, and 4498ed6d76e4SPaul Moore * representing a wide range of network traffic spanning multiple 4499ed6d76e4SPaul Moore * connections unlike traditional sockets - check the TUN driver to 4500ed6d76e4SPaul Moore * get a better understanding of why this socket is special */ 4501ed6d76e4SPaul Moore 4502ed6d76e4SPaul Moore return avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE, 4503ed6d76e4SPaul Moore NULL); 4504ed6d76e4SPaul Moore } 4505ed6d76e4SPaul Moore 45065dbbaf2dSPaul Moore static int selinux_tun_dev_attach_queue(void *security) 4507ed6d76e4SPaul Moore { 45085dbbaf2dSPaul Moore struct tun_security_struct *tunsec = security; 45095dbbaf2dSPaul Moore 45105dbbaf2dSPaul Moore return avc_has_perm(current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET, 45115dbbaf2dSPaul Moore TUN_SOCKET__ATTACH_QUEUE, NULL); 45125dbbaf2dSPaul Moore } 45135dbbaf2dSPaul Moore 45145dbbaf2dSPaul Moore static int selinux_tun_dev_attach(struct sock *sk, void *security) 45155dbbaf2dSPaul Moore { 45165dbbaf2dSPaul Moore struct tun_security_struct *tunsec = security; 4517ed6d76e4SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 4518ed6d76e4SPaul Moore 4519ed6d76e4SPaul Moore /* we don't currently perform any NetLabel based labeling here and it 4520ed6d76e4SPaul Moore * isn't clear that we would want to do so anyway; while we could apply 4521ed6d76e4SPaul Moore * labeling without the support of the TUN user the resulting labeled 4522ed6d76e4SPaul Moore * traffic from the other end of the connection would almost certainly 4523ed6d76e4SPaul Moore * cause confusion to the TUN user that had no idea network labeling 4524ed6d76e4SPaul Moore * protocols were being used */ 4525ed6d76e4SPaul Moore 45265dbbaf2dSPaul Moore sksec->sid = tunsec->sid; 4527ed6d76e4SPaul Moore sksec->sclass = SECCLASS_TUN_SOCKET; 45285dbbaf2dSPaul Moore 45295dbbaf2dSPaul Moore return 0; 4530ed6d76e4SPaul Moore } 4531ed6d76e4SPaul Moore 45325dbbaf2dSPaul Moore static int selinux_tun_dev_open(void *security) 4533ed6d76e4SPaul Moore { 45345dbbaf2dSPaul Moore struct tun_security_struct *tunsec = security; 4535ed6d76e4SPaul Moore u32 sid = current_sid(); 4536ed6d76e4SPaul Moore int err; 4537ed6d76e4SPaul Moore 45385dbbaf2dSPaul Moore err = avc_has_perm(sid, tunsec->sid, SECCLASS_TUN_SOCKET, 4539ed6d76e4SPaul Moore TUN_SOCKET__RELABELFROM, NULL); 4540ed6d76e4SPaul Moore if (err) 4541ed6d76e4SPaul Moore return err; 4542ed6d76e4SPaul Moore err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, 4543ed6d76e4SPaul Moore TUN_SOCKET__RELABELTO, NULL); 4544ed6d76e4SPaul Moore if (err) 4545ed6d76e4SPaul Moore return err; 45465dbbaf2dSPaul Moore tunsec->sid = sid; 4547ed6d76e4SPaul Moore 4548ed6d76e4SPaul Moore return 0; 4549ed6d76e4SPaul Moore } 4550ed6d76e4SPaul Moore 45511da177e4SLinus Torvalds static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb) 45521da177e4SLinus Torvalds { 45531da177e4SLinus Torvalds int err = 0; 45541da177e4SLinus Torvalds u32 perm; 45551da177e4SLinus Torvalds struct nlmsghdr *nlh; 4556253bfae6SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 45571da177e4SLinus Torvalds 455877954983SHong zhi guo if (skb->len < NLMSG_HDRLEN) { 45591da177e4SLinus Torvalds err = -EINVAL; 45601da177e4SLinus Torvalds goto out; 45611da177e4SLinus Torvalds } 4562b529ccf2SArnaldo Carvalho de Melo nlh = nlmsg_hdr(skb); 45631da177e4SLinus Torvalds 4564253bfae6SPaul Moore err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm); 45651da177e4SLinus Torvalds if (err) { 45661da177e4SLinus Torvalds if (err == -EINVAL) { 45679ad9ad38SDavid Woodhouse audit_log(current->audit_context, GFP_KERNEL, AUDIT_SELINUX_ERR, 45681da177e4SLinus Torvalds "SELinux: unrecognized netlink message" 45691da177e4SLinus Torvalds " type=%hu for sclass=%hu\n", 4570253bfae6SPaul Moore nlh->nlmsg_type, sksec->sclass); 457139c9aedeSEric Paris if (!selinux_enforcing || security_get_allow_unknown()) 45721da177e4SLinus Torvalds err = 0; 45731da177e4SLinus Torvalds } 45741da177e4SLinus Torvalds 45751da177e4SLinus Torvalds /* Ignore */ 45761da177e4SLinus Torvalds if (err == -ENOENT) 45771da177e4SLinus Torvalds err = 0; 45781da177e4SLinus Torvalds goto out; 45791da177e4SLinus Torvalds } 45801da177e4SLinus Torvalds 4581253bfae6SPaul Moore err = sock_has_perm(current, sk, perm); 45821da177e4SLinus Torvalds out: 45831da177e4SLinus Torvalds return err; 45841da177e4SLinus Torvalds } 45851da177e4SLinus Torvalds 45861da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER 45871da177e4SLinus Torvalds 4588effad8dfSPaul Moore static unsigned int selinux_ip_forward(struct sk_buff *skb, int ifindex, 4589effad8dfSPaul Moore u16 family) 45901da177e4SLinus Torvalds { 4591dfaebe98SPaul Moore int err; 4592effad8dfSPaul Moore char *addrp; 4593effad8dfSPaul Moore u32 peer_sid; 45942bf49690SThomas Liu struct common_audit_data ad; 459548c62af6SEric Paris struct lsm_network_audit net = {0,}; 4596effad8dfSPaul Moore u8 secmark_active; 4597948bf85cSPaul Moore u8 netlbl_active; 4598effad8dfSPaul Moore u8 peerlbl_active; 45994237c75cSVenkat Yekkirala 4600effad8dfSPaul Moore if (!selinux_policycap_netpeer) 4601effad8dfSPaul Moore return NF_ACCEPT; 46024237c75cSVenkat Yekkirala 4603effad8dfSPaul Moore secmark_active = selinux_secmark_enabled(); 4604948bf85cSPaul Moore netlbl_active = netlbl_enabled(); 46052be4d74fSChris PeBenito peerlbl_active = selinux_peerlbl_enabled(); 4606effad8dfSPaul Moore if (!secmark_active && !peerlbl_active) 4607effad8dfSPaul Moore return NF_ACCEPT; 46084237c75cSVenkat Yekkirala 4609d8395c87SPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0) 4610d8395c87SPaul Moore return NF_DROP; 4611d8395c87SPaul Moore 461250c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 461348c62af6SEric Paris ad.u.net = &net; 461448c62af6SEric Paris ad.u.net->netif = ifindex; 461548c62af6SEric Paris ad.u.net->family = family; 4616effad8dfSPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0) 4617effad8dfSPaul Moore return NF_DROP; 46181da177e4SLinus Torvalds 4619dfaebe98SPaul Moore if (peerlbl_active) { 4620dfaebe98SPaul Moore err = selinux_inet_sys_rcv_skb(ifindex, addrp, family, 4621dfaebe98SPaul Moore peer_sid, &ad); 4622dfaebe98SPaul Moore if (err) { 4623dfaebe98SPaul Moore selinux_netlbl_err(skb, err, 1); 4624effad8dfSPaul Moore return NF_DROP; 4625dfaebe98SPaul Moore } 4626dfaebe98SPaul Moore } 4627effad8dfSPaul Moore 4628effad8dfSPaul Moore if (secmark_active) 4629effad8dfSPaul Moore if (avc_has_perm(peer_sid, skb->secmark, 4630effad8dfSPaul Moore SECCLASS_PACKET, PACKET__FORWARD_IN, &ad)) 4631effad8dfSPaul Moore return NF_DROP; 4632effad8dfSPaul Moore 4633948bf85cSPaul Moore if (netlbl_active) 4634948bf85cSPaul Moore /* we do this in the FORWARD path and not the POST_ROUTING 4635948bf85cSPaul Moore * path because we want to make sure we apply the necessary 4636948bf85cSPaul Moore * labeling before IPsec is applied so we can leverage AH 4637948bf85cSPaul Moore * protection */ 4638948bf85cSPaul Moore if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0) 4639948bf85cSPaul Moore return NF_DROP; 4640948bf85cSPaul Moore 4641effad8dfSPaul Moore return NF_ACCEPT; 4642effad8dfSPaul Moore } 4643effad8dfSPaul Moore 4644effad8dfSPaul Moore static unsigned int selinux_ipv4_forward(unsigned int hooknum, 4645effad8dfSPaul Moore struct sk_buff *skb, 4646effad8dfSPaul Moore const struct net_device *in, 4647effad8dfSPaul Moore const struct net_device *out, 4648effad8dfSPaul Moore int (*okfn)(struct sk_buff *)) 4649effad8dfSPaul Moore { 4650effad8dfSPaul Moore return selinux_ip_forward(skb, in->ifindex, PF_INET); 4651effad8dfSPaul Moore } 4652effad8dfSPaul Moore 4653effad8dfSPaul Moore #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 4654effad8dfSPaul Moore static unsigned int selinux_ipv6_forward(unsigned int hooknum, 4655effad8dfSPaul Moore struct sk_buff *skb, 4656effad8dfSPaul Moore const struct net_device *in, 4657effad8dfSPaul Moore const struct net_device *out, 4658effad8dfSPaul Moore int (*okfn)(struct sk_buff *)) 4659effad8dfSPaul Moore { 4660effad8dfSPaul Moore return selinux_ip_forward(skb, in->ifindex, PF_INET6); 4661effad8dfSPaul Moore } 4662effad8dfSPaul Moore #endif /* IPV6 */ 4663effad8dfSPaul Moore 4664948bf85cSPaul Moore static unsigned int selinux_ip_output(struct sk_buff *skb, 4665948bf85cSPaul Moore u16 family) 4666948bf85cSPaul Moore { 4667948bf85cSPaul Moore u32 sid; 4668948bf85cSPaul Moore 4669948bf85cSPaul Moore if (!netlbl_enabled()) 4670948bf85cSPaul Moore return NF_ACCEPT; 4671948bf85cSPaul Moore 4672948bf85cSPaul Moore /* we do this in the LOCAL_OUT path and not the POST_ROUTING path 4673948bf85cSPaul Moore * because we want to make sure we apply the necessary labeling 4674948bf85cSPaul Moore * before IPsec is applied so we can leverage AH protection */ 4675948bf85cSPaul Moore if (skb->sk) { 4676948bf85cSPaul Moore struct sk_security_struct *sksec = skb->sk->sk_security; 4677948bf85cSPaul Moore sid = sksec->sid; 4678948bf85cSPaul Moore } else 4679948bf85cSPaul Moore sid = SECINITSID_KERNEL; 4680948bf85cSPaul Moore if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0) 4681948bf85cSPaul Moore return NF_DROP; 4682948bf85cSPaul Moore 4683948bf85cSPaul Moore return NF_ACCEPT; 4684948bf85cSPaul Moore } 4685948bf85cSPaul Moore 4686948bf85cSPaul Moore static unsigned int selinux_ipv4_output(unsigned int hooknum, 4687948bf85cSPaul Moore struct sk_buff *skb, 4688948bf85cSPaul Moore const struct net_device *in, 4689948bf85cSPaul Moore const struct net_device *out, 4690948bf85cSPaul Moore int (*okfn)(struct sk_buff *)) 4691948bf85cSPaul Moore { 4692948bf85cSPaul Moore return selinux_ip_output(skb, PF_INET); 4693948bf85cSPaul Moore } 4694948bf85cSPaul Moore 4695effad8dfSPaul Moore static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb, 4696effad8dfSPaul Moore int ifindex, 4697d8395c87SPaul Moore u16 family) 46984e5ab4cbSJames Morris { 4699effad8dfSPaul Moore struct sock *sk = skb->sk; 47004237c75cSVenkat Yekkirala struct sk_security_struct *sksec; 47012bf49690SThomas Liu struct common_audit_data ad; 470248c62af6SEric Paris struct lsm_network_audit net = {0,}; 4703d8395c87SPaul Moore char *addrp; 4704d8395c87SPaul Moore u8 proto; 47054e5ab4cbSJames Morris 4706effad8dfSPaul Moore if (sk == NULL) 4707effad8dfSPaul Moore return NF_ACCEPT; 47084237c75cSVenkat Yekkirala sksec = sk->sk_security; 47094e5ab4cbSJames Morris 471050c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 471148c62af6SEric Paris ad.u.net = &net; 471248c62af6SEric Paris ad.u.net->netif = ifindex; 471348c62af6SEric Paris ad.u.net->family = family; 4714d8395c87SPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto)) 4715d8395c87SPaul Moore return NF_DROP; 4716d8395c87SPaul Moore 471758bfbb51SPaul Moore if (selinux_secmark_enabled()) 4718effad8dfSPaul Moore if (avc_has_perm(sksec->sid, skb->secmark, 4719d8395c87SPaul Moore SECCLASS_PACKET, PACKET__SEND, &ad)) 47202fe66ec2SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 47211da177e4SLinus Torvalds 4722d8395c87SPaul Moore if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto)) 47232fe66ec2SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 4724effad8dfSPaul Moore 4725effad8dfSPaul Moore return NF_ACCEPT; 4726effad8dfSPaul Moore } 4727effad8dfSPaul Moore 4728effad8dfSPaul Moore static unsigned int selinux_ip_postroute(struct sk_buff *skb, int ifindex, 4729effad8dfSPaul Moore u16 family) 4730effad8dfSPaul Moore { 4731effad8dfSPaul Moore u32 secmark_perm; 4732effad8dfSPaul Moore u32 peer_sid; 4733effad8dfSPaul Moore struct sock *sk; 47342bf49690SThomas Liu struct common_audit_data ad; 473548c62af6SEric Paris struct lsm_network_audit net = {0,}; 4736effad8dfSPaul Moore char *addrp; 4737effad8dfSPaul Moore u8 secmark_active; 4738effad8dfSPaul Moore u8 peerlbl_active; 4739effad8dfSPaul Moore 4740effad8dfSPaul Moore /* If any sort of compatibility mode is enabled then handoff processing 4741effad8dfSPaul Moore * to the selinux_ip_postroute_compat() function to deal with the 4742effad8dfSPaul Moore * special handling. We do this in an attempt to keep this function 4743effad8dfSPaul Moore * as fast and as clean as possible. */ 474458bfbb51SPaul Moore if (!selinux_policycap_netpeer) 4745d8395c87SPaul Moore return selinux_ip_postroute_compat(skb, ifindex, family); 4746def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM 4747effad8dfSPaul Moore /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec 4748effad8dfSPaul Moore * packet transformation so allow the packet to pass without any checks 4749effad8dfSPaul Moore * since we'll have another chance to perform access control checks 4750effad8dfSPaul Moore * when the packet is on it's final way out. 4751effad8dfSPaul Moore * NOTE: there appear to be some IPv6 multicast cases where skb->dst 4752effad8dfSPaul Moore * is NULL, in this case go ahead and apply access control. */ 4753adf30907SEric Dumazet if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL) 4754effad8dfSPaul Moore return NF_ACCEPT; 4755def8b4faSAlexey Dobriyan #endif 4756effad8dfSPaul Moore secmark_active = selinux_secmark_enabled(); 47572be4d74fSChris PeBenito peerlbl_active = selinux_peerlbl_enabled(); 4758effad8dfSPaul Moore if (!secmark_active && !peerlbl_active) 4759effad8dfSPaul Moore return NF_ACCEPT; 4760effad8dfSPaul Moore 4761d8395c87SPaul Moore /* if the packet is being forwarded then get the peer label from the 4762d8395c87SPaul Moore * packet itself; otherwise check to see if it is from a local 4763d8395c87SPaul Moore * application or the kernel, if from an application get the peer label 4764d8395c87SPaul Moore * from the sending socket, otherwise use the kernel's sid */ 4765effad8dfSPaul Moore sk = skb->sk; 4766d8395c87SPaul Moore if (sk == NULL) { 47674a7ab3dcSSteffen Klassert if (skb->skb_iif) { 4768d8395c87SPaul Moore secmark_perm = PACKET__FORWARD_OUT; 4769d8395c87SPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &peer_sid)) 477004f6d70fSEric Paris return NF_DROP; 47714a7ab3dcSSteffen Klassert } else { 47724a7ab3dcSSteffen Klassert secmark_perm = PACKET__SEND; 4773d8395c87SPaul Moore peer_sid = SECINITSID_KERNEL; 47744a7ab3dcSSteffen Klassert } 4775d8395c87SPaul Moore } else { 4776effad8dfSPaul Moore struct sk_security_struct *sksec = sk->sk_security; 4777effad8dfSPaul Moore peer_sid = sksec->sid; 4778effad8dfSPaul Moore secmark_perm = PACKET__SEND; 4779effad8dfSPaul Moore } 4780effad8dfSPaul Moore 478150c205f5SEric Paris ad.type = LSM_AUDIT_DATA_NET; 478248c62af6SEric Paris ad.u.net = &net; 478348c62af6SEric Paris ad.u.net->netif = ifindex; 478448c62af6SEric Paris ad.u.net->family = family; 4785d8395c87SPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL)) 478604f6d70fSEric Paris return NF_DROP; 4787d8395c87SPaul Moore 4788effad8dfSPaul Moore if (secmark_active) 4789effad8dfSPaul Moore if (avc_has_perm(peer_sid, skb->secmark, 4790effad8dfSPaul Moore SECCLASS_PACKET, secmark_perm, &ad)) 47911f1aaf82SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 4792effad8dfSPaul Moore 4793effad8dfSPaul Moore if (peerlbl_active) { 4794effad8dfSPaul Moore u32 if_sid; 4795effad8dfSPaul Moore u32 node_sid; 4796effad8dfSPaul Moore 4797effad8dfSPaul Moore if (sel_netif_sid(ifindex, &if_sid)) 479804f6d70fSEric Paris return NF_DROP; 4799effad8dfSPaul Moore if (avc_has_perm(peer_sid, if_sid, 4800effad8dfSPaul Moore SECCLASS_NETIF, NETIF__EGRESS, &ad)) 48011f1aaf82SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 4802effad8dfSPaul Moore 4803effad8dfSPaul Moore if (sel_netnode_sid(addrp, family, &node_sid)) 480404f6d70fSEric Paris return NF_DROP; 4805effad8dfSPaul Moore if (avc_has_perm(peer_sid, node_sid, 4806effad8dfSPaul Moore SECCLASS_NODE, NODE__SENDTO, &ad)) 48071f1aaf82SEric Paris return NF_DROP_ERR(-ECONNREFUSED); 4808effad8dfSPaul Moore } 4809effad8dfSPaul Moore 4810effad8dfSPaul Moore return NF_ACCEPT; 4811effad8dfSPaul Moore } 4812effad8dfSPaul Moore 4813effad8dfSPaul Moore static unsigned int selinux_ipv4_postroute(unsigned int hooknum, 4814a224be76SDavid S. Miller struct sk_buff *skb, 48151da177e4SLinus Torvalds const struct net_device *in, 48161da177e4SLinus Torvalds const struct net_device *out, 48171da177e4SLinus Torvalds int (*okfn)(struct sk_buff *)) 48181da177e4SLinus Torvalds { 4819effad8dfSPaul Moore return selinux_ip_postroute(skb, out->ifindex, PF_INET); 48201da177e4SLinus Torvalds } 48211da177e4SLinus Torvalds 48221da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 4823effad8dfSPaul Moore static unsigned int selinux_ipv6_postroute(unsigned int hooknum, 4824a224be76SDavid S. Miller struct sk_buff *skb, 48251da177e4SLinus Torvalds const struct net_device *in, 48261da177e4SLinus Torvalds const struct net_device *out, 48271da177e4SLinus Torvalds int (*okfn)(struct sk_buff *)) 48281da177e4SLinus Torvalds { 4829effad8dfSPaul Moore return selinux_ip_postroute(skb, out->ifindex, PF_INET6); 48301da177e4SLinus Torvalds } 48311da177e4SLinus Torvalds #endif /* IPV6 */ 48321da177e4SLinus Torvalds 48331da177e4SLinus Torvalds #endif /* CONFIG_NETFILTER */ 48341da177e4SLinus Torvalds 48351da177e4SLinus Torvalds static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb) 48361da177e4SLinus Torvalds { 48371da177e4SLinus Torvalds int err; 48381da177e4SLinus Torvalds 4839200ac532SEric Paris err = cap_netlink_send(sk, skb); 48401da177e4SLinus Torvalds if (err) 48411da177e4SLinus Torvalds return err; 48421da177e4SLinus Torvalds 4843941fc5b2SStephen Smalley return selinux_nlmsg_perm(sk, skb); 48441da177e4SLinus Torvalds } 48451da177e4SLinus Torvalds 48461da177e4SLinus Torvalds static int ipc_alloc_security(struct task_struct *task, 48471da177e4SLinus Torvalds struct kern_ipc_perm *perm, 48481da177e4SLinus Torvalds u16 sclass) 48491da177e4SLinus Torvalds { 48501da177e4SLinus Torvalds struct ipc_security_struct *isec; 4851275bb41eSDavid Howells u32 sid; 48521da177e4SLinus Torvalds 485389d155efSJames Morris isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL); 48541da177e4SLinus Torvalds if (!isec) 48551da177e4SLinus Torvalds return -ENOMEM; 48561da177e4SLinus Torvalds 4857275bb41eSDavid Howells sid = task_sid(task); 48581da177e4SLinus Torvalds isec->sclass = sclass; 4859275bb41eSDavid Howells isec->sid = sid; 48601da177e4SLinus Torvalds perm->security = isec; 48611da177e4SLinus Torvalds 48621da177e4SLinus Torvalds return 0; 48631da177e4SLinus Torvalds } 48641da177e4SLinus Torvalds 48651da177e4SLinus Torvalds static void ipc_free_security(struct kern_ipc_perm *perm) 48661da177e4SLinus Torvalds { 48671da177e4SLinus Torvalds struct ipc_security_struct *isec = perm->security; 48681da177e4SLinus Torvalds perm->security = NULL; 48691da177e4SLinus Torvalds kfree(isec); 48701da177e4SLinus Torvalds } 48711da177e4SLinus Torvalds 48721da177e4SLinus Torvalds static int msg_msg_alloc_security(struct msg_msg *msg) 48731da177e4SLinus Torvalds { 48741da177e4SLinus Torvalds struct msg_security_struct *msec; 48751da177e4SLinus Torvalds 487689d155efSJames Morris msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL); 48771da177e4SLinus Torvalds if (!msec) 48781da177e4SLinus Torvalds return -ENOMEM; 48791da177e4SLinus Torvalds 48801da177e4SLinus Torvalds msec->sid = SECINITSID_UNLABELED; 48811da177e4SLinus Torvalds msg->security = msec; 48821da177e4SLinus Torvalds 48831da177e4SLinus Torvalds return 0; 48841da177e4SLinus Torvalds } 48851da177e4SLinus Torvalds 48861da177e4SLinus Torvalds static void msg_msg_free_security(struct msg_msg *msg) 48871da177e4SLinus Torvalds { 48881da177e4SLinus Torvalds struct msg_security_struct *msec = msg->security; 48891da177e4SLinus Torvalds 48901da177e4SLinus Torvalds msg->security = NULL; 48911da177e4SLinus Torvalds kfree(msec); 48921da177e4SLinus Torvalds } 48931da177e4SLinus Torvalds 48941da177e4SLinus Torvalds static int ipc_has_perm(struct kern_ipc_perm *ipc_perms, 48956af963f1SStephen Smalley u32 perms) 48961da177e4SLinus Torvalds { 48971da177e4SLinus Torvalds struct ipc_security_struct *isec; 48982bf49690SThomas Liu struct common_audit_data ad; 4899275bb41eSDavid Howells u32 sid = current_sid(); 49001da177e4SLinus Torvalds 49011da177e4SLinus Torvalds isec = ipc_perms->security; 49021da177e4SLinus Torvalds 490350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 49041da177e4SLinus Torvalds ad.u.ipc_id = ipc_perms->key; 49051da177e4SLinus Torvalds 4906275bb41eSDavid Howells return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad); 49071da177e4SLinus Torvalds } 49081da177e4SLinus Torvalds 49091da177e4SLinus Torvalds static int selinux_msg_msg_alloc_security(struct msg_msg *msg) 49101da177e4SLinus Torvalds { 49111da177e4SLinus Torvalds return msg_msg_alloc_security(msg); 49121da177e4SLinus Torvalds } 49131da177e4SLinus Torvalds 49141da177e4SLinus Torvalds static void selinux_msg_msg_free_security(struct msg_msg *msg) 49151da177e4SLinus Torvalds { 49161da177e4SLinus Torvalds msg_msg_free_security(msg); 49171da177e4SLinus Torvalds } 49181da177e4SLinus Torvalds 49191da177e4SLinus Torvalds /* message queue security operations */ 49201da177e4SLinus Torvalds static int selinux_msg_queue_alloc_security(struct msg_queue *msq) 49211da177e4SLinus Torvalds { 49221da177e4SLinus Torvalds struct ipc_security_struct *isec; 49232bf49690SThomas Liu struct common_audit_data ad; 4924275bb41eSDavid Howells u32 sid = current_sid(); 49251da177e4SLinus Torvalds int rc; 49261da177e4SLinus Torvalds 49271da177e4SLinus Torvalds rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ); 49281da177e4SLinus Torvalds if (rc) 49291da177e4SLinus Torvalds return rc; 49301da177e4SLinus Torvalds 49311da177e4SLinus Torvalds isec = msq->q_perm.security; 49321da177e4SLinus Torvalds 493350c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 49341da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 49351da177e4SLinus Torvalds 4936275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ, 49371da177e4SLinus Torvalds MSGQ__CREATE, &ad); 49381da177e4SLinus Torvalds if (rc) { 49391da177e4SLinus Torvalds ipc_free_security(&msq->q_perm); 49401da177e4SLinus Torvalds return rc; 49411da177e4SLinus Torvalds } 49421da177e4SLinus Torvalds return 0; 49431da177e4SLinus Torvalds } 49441da177e4SLinus Torvalds 49451da177e4SLinus Torvalds static void selinux_msg_queue_free_security(struct msg_queue *msq) 49461da177e4SLinus Torvalds { 49471da177e4SLinus Torvalds ipc_free_security(&msq->q_perm); 49481da177e4SLinus Torvalds } 49491da177e4SLinus Torvalds 49501da177e4SLinus Torvalds static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg) 49511da177e4SLinus Torvalds { 49521da177e4SLinus Torvalds struct ipc_security_struct *isec; 49532bf49690SThomas Liu struct common_audit_data ad; 4954275bb41eSDavid Howells u32 sid = current_sid(); 49551da177e4SLinus Torvalds 49561da177e4SLinus Torvalds isec = msq->q_perm.security; 49571da177e4SLinus Torvalds 495850c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 49591da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 49601da177e4SLinus Torvalds 4961275bb41eSDavid Howells return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ, 49621da177e4SLinus Torvalds MSGQ__ASSOCIATE, &ad); 49631da177e4SLinus Torvalds } 49641da177e4SLinus Torvalds 49651da177e4SLinus Torvalds static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd) 49661da177e4SLinus Torvalds { 49671da177e4SLinus Torvalds int err; 49681da177e4SLinus Torvalds int perms; 49691da177e4SLinus Torvalds 49701da177e4SLinus Torvalds switch (cmd) { 49711da177e4SLinus Torvalds case IPC_INFO: 49721da177e4SLinus Torvalds case MSG_INFO: 49731da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 49741da177e4SLinus Torvalds return task_has_system(current, SYSTEM__IPC_INFO); 49751da177e4SLinus Torvalds case IPC_STAT: 49761da177e4SLinus Torvalds case MSG_STAT: 49771da177e4SLinus Torvalds perms = MSGQ__GETATTR | MSGQ__ASSOCIATE; 49781da177e4SLinus Torvalds break; 49791da177e4SLinus Torvalds case IPC_SET: 49801da177e4SLinus Torvalds perms = MSGQ__SETATTR; 49811da177e4SLinus Torvalds break; 49821da177e4SLinus Torvalds case IPC_RMID: 49831da177e4SLinus Torvalds perms = MSGQ__DESTROY; 49841da177e4SLinus Torvalds break; 49851da177e4SLinus Torvalds default: 49861da177e4SLinus Torvalds return 0; 49871da177e4SLinus Torvalds } 49881da177e4SLinus Torvalds 49896af963f1SStephen Smalley err = ipc_has_perm(&msq->q_perm, perms); 49901da177e4SLinus Torvalds return err; 49911da177e4SLinus Torvalds } 49921da177e4SLinus Torvalds 49931da177e4SLinus Torvalds static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg) 49941da177e4SLinus Torvalds { 49951da177e4SLinus Torvalds struct ipc_security_struct *isec; 49961da177e4SLinus Torvalds struct msg_security_struct *msec; 49972bf49690SThomas Liu struct common_audit_data ad; 4998275bb41eSDavid Howells u32 sid = current_sid(); 49991da177e4SLinus Torvalds int rc; 50001da177e4SLinus Torvalds 50011da177e4SLinus Torvalds isec = msq->q_perm.security; 50021da177e4SLinus Torvalds msec = msg->security; 50031da177e4SLinus Torvalds 50041da177e4SLinus Torvalds /* 50051da177e4SLinus Torvalds * First time through, need to assign label to the message 50061da177e4SLinus Torvalds */ 50071da177e4SLinus Torvalds if (msec->sid == SECINITSID_UNLABELED) { 50081da177e4SLinus Torvalds /* 50091da177e4SLinus Torvalds * Compute new sid based on current process and 50101da177e4SLinus Torvalds * message queue this message will be stored in 50111da177e4SLinus Torvalds */ 5012275bb41eSDavid Howells rc = security_transition_sid(sid, isec->sid, SECCLASS_MSG, 5013652bb9b0SEric Paris NULL, &msec->sid); 50141da177e4SLinus Torvalds if (rc) 50151da177e4SLinus Torvalds return rc; 50161da177e4SLinus Torvalds } 50171da177e4SLinus Torvalds 501850c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 50191da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 50201da177e4SLinus Torvalds 50211da177e4SLinus Torvalds /* Can this process write to the queue? */ 5022275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ, 50231da177e4SLinus Torvalds MSGQ__WRITE, &ad); 50241da177e4SLinus Torvalds if (!rc) 50251da177e4SLinus Torvalds /* Can this process send the message */ 5026275bb41eSDavid Howells rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG, 5027275bb41eSDavid Howells MSG__SEND, &ad); 50281da177e4SLinus Torvalds if (!rc) 50291da177e4SLinus Torvalds /* Can the message be put in the queue? */ 5030275bb41eSDavid Howells rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ, 5031275bb41eSDavid Howells MSGQ__ENQUEUE, &ad); 50321da177e4SLinus Torvalds 50331da177e4SLinus Torvalds return rc; 50341da177e4SLinus Torvalds } 50351da177e4SLinus Torvalds 50361da177e4SLinus Torvalds static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg, 50371da177e4SLinus Torvalds struct task_struct *target, 50381da177e4SLinus Torvalds long type, int mode) 50391da177e4SLinus Torvalds { 50401da177e4SLinus Torvalds struct ipc_security_struct *isec; 50411da177e4SLinus Torvalds struct msg_security_struct *msec; 50422bf49690SThomas Liu struct common_audit_data ad; 5043275bb41eSDavid Howells u32 sid = task_sid(target); 50441da177e4SLinus Torvalds int rc; 50451da177e4SLinus Torvalds 50461da177e4SLinus Torvalds isec = msq->q_perm.security; 50471da177e4SLinus Torvalds msec = msg->security; 50481da177e4SLinus Torvalds 504950c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 50501da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 50511da177e4SLinus Torvalds 5052275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, 50531da177e4SLinus Torvalds SECCLASS_MSGQ, MSGQ__READ, &ad); 50541da177e4SLinus Torvalds if (!rc) 5055275bb41eSDavid Howells rc = avc_has_perm(sid, msec->sid, 50561da177e4SLinus Torvalds SECCLASS_MSG, MSG__RECEIVE, &ad); 50571da177e4SLinus Torvalds return rc; 50581da177e4SLinus Torvalds } 50591da177e4SLinus Torvalds 50601da177e4SLinus Torvalds /* Shared Memory security operations */ 50611da177e4SLinus Torvalds static int selinux_shm_alloc_security(struct shmid_kernel *shp) 50621da177e4SLinus Torvalds { 50631da177e4SLinus Torvalds struct ipc_security_struct *isec; 50642bf49690SThomas Liu struct common_audit_data ad; 5065275bb41eSDavid Howells u32 sid = current_sid(); 50661da177e4SLinus Torvalds int rc; 50671da177e4SLinus Torvalds 50681da177e4SLinus Torvalds rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM); 50691da177e4SLinus Torvalds if (rc) 50701da177e4SLinus Torvalds return rc; 50711da177e4SLinus Torvalds 50721da177e4SLinus Torvalds isec = shp->shm_perm.security; 50731da177e4SLinus Torvalds 507450c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 50751da177e4SLinus Torvalds ad.u.ipc_id = shp->shm_perm.key; 50761da177e4SLinus Torvalds 5077275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, SECCLASS_SHM, 50781da177e4SLinus Torvalds SHM__CREATE, &ad); 50791da177e4SLinus Torvalds if (rc) { 50801da177e4SLinus Torvalds ipc_free_security(&shp->shm_perm); 50811da177e4SLinus Torvalds return rc; 50821da177e4SLinus Torvalds } 50831da177e4SLinus Torvalds return 0; 50841da177e4SLinus Torvalds } 50851da177e4SLinus Torvalds 50861da177e4SLinus Torvalds static void selinux_shm_free_security(struct shmid_kernel *shp) 50871da177e4SLinus Torvalds { 50881da177e4SLinus Torvalds ipc_free_security(&shp->shm_perm); 50891da177e4SLinus Torvalds } 50901da177e4SLinus Torvalds 50911da177e4SLinus Torvalds static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg) 50921da177e4SLinus Torvalds { 50931da177e4SLinus Torvalds struct ipc_security_struct *isec; 50942bf49690SThomas Liu struct common_audit_data ad; 5095275bb41eSDavid Howells u32 sid = current_sid(); 50961da177e4SLinus Torvalds 50971da177e4SLinus Torvalds isec = shp->shm_perm.security; 50981da177e4SLinus Torvalds 509950c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 51001da177e4SLinus Torvalds ad.u.ipc_id = shp->shm_perm.key; 51011da177e4SLinus Torvalds 5102275bb41eSDavid Howells return avc_has_perm(sid, isec->sid, SECCLASS_SHM, 51031da177e4SLinus Torvalds SHM__ASSOCIATE, &ad); 51041da177e4SLinus Torvalds } 51051da177e4SLinus Torvalds 51061da177e4SLinus Torvalds /* Note, at this point, shp is locked down */ 51071da177e4SLinus Torvalds static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd) 51081da177e4SLinus Torvalds { 51091da177e4SLinus Torvalds int perms; 51101da177e4SLinus Torvalds int err; 51111da177e4SLinus Torvalds 51121da177e4SLinus Torvalds switch (cmd) { 51131da177e4SLinus Torvalds case IPC_INFO: 51141da177e4SLinus Torvalds case SHM_INFO: 51151da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 51161da177e4SLinus Torvalds return task_has_system(current, SYSTEM__IPC_INFO); 51171da177e4SLinus Torvalds case IPC_STAT: 51181da177e4SLinus Torvalds case SHM_STAT: 51191da177e4SLinus Torvalds perms = SHM__GETATTR | SHM__ASSOCIATE; 51201da177e4SLinus Torvalds break; 51211da177e4SLinus Torvalds case IPC_SET: 51221da177e4SLinus Torvalds perms = SHM__SETATTR; 51231da177e4SLinus Torvalds break; 51241da177e4SLinus Torvalds case SHM_LOCK: 51251da177e4SLinus Torvalds case SHM_UNLOCK: 51261da177e4SLinus Torvalds perms = SHM__LOCK; 51271da177e4SLinus Torvalds break; 51281da177e4SLinus Torvalds case IPC_RMID: 51291da177e4SLinus Torvalds perms = SHM__DESTROY; 51301da177e4SLinus Torvalds break; 51311da177e4SLinus Torvalds default: 51321da177e4SLinus Torvalds return 0; 51331da177e4SLinus Torvalds } 51341da177e4SLinus Torvalds 51356af963f1SStephen Smalley err = ipc_has_perm(&shp->shm_perm, perms); 51361da177e4SLinus Torvalds return err; 51371da177e4SLinus Torvalds } 51381da177e4SLinus Torvalds 51391da177e4SLinus Torvalds static int selinux_shm_shmat(struct shmid_kernel *shp, 51401da177e4SLinus Torvalds char __user *shmaddr, int shmflg) 51411da177e4SLinus Torvalds { 51421da177e4SLinus Torvalds u32 perms; 51431da177e4SLinus Torvalds 51441da177e4SLinus Torvalds if (shmflg & SHM_RDONLY) 51451da177e4SLinus Torvalds perms = SHM__READ; 51461da177e4SLinus Torvalds else 51471da177e4SLinus Torvalds perms = SHM__READ | SHM__WRITE; 51481da177e4SLinus Torvalds 51496af963f1SStephen Smalley return ipc_has_perm(&shp->shm_perm, perms); 51501da177e4SLinus Torvalds } 51511da177e4SLinus Torvalds 51521da177e4SLinus Torvalds /* Semaphore security operations */ 51531da177e4SLinus Torvalds static int selinux_sem_alloc_security(struct sem_array *sma) 51541da177e4SLinus Torvalds { 51551da177e4SLinus Torvalds struct ipc_security_struct *isec; 51562bf49690SThomas Liu struct common_audit_data ad; 5157275bb41eSDavid Howells u32 sid = current_sid(); 51581da177e4SLinus Torvalds int rc; 51591da177e4SLinus Torvalds 51601da177e4SLinus Torvalds rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM); 51611da177e4SLinus Torvalds if (rc) 51621da177e4SLinus Torvalds return rc; 51631da177e4SLinus Torvalds 51641da177e4SLinus Torvalds isec = sma->sem_perm.security; 51651da177e4SLinus Torvalds 516650c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 51671da177e4SLinus Torvalds ad.u.ipc_id = sma->sem_perm.key; 51681da177e4SLinus Torvalds 5169275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, SECCLASS_SEM, 51701da177e4SLinus Torvalds SEM__CREATE, &ad); 51711da177e4SLinus Torvalds if (rc) { 51721da177e4SLinus Torvalds ipc_free_security(&sma->sem_perm); 51731da177e4SLinus Torvalds return rc; 51741da177e4SLinus Torvalds } 51751da177e4SLinus Torvalds return 0; 51761da177e4SLinus Torvalds } 51771da177e4SLinus Torvalds 51781da177e4SLinus Torvalds static void selinux_sem_free_security(struct sem_array *sma) 51791da177e4SLinus Torvalds { 51801da177e4SLinus Torvalds ipc_free_security(&sma->sem_perm); 51811da177e4SLinus Torvalds } 51821da177e4SLinus Torvalds 51831da177e4SLinus Torvalds static int selinux_sem_associate(struct sem_array *sma, int semflg) 51841da177e4SLinus Torvalds { 51851da177e4SLinus Torvalds struct ipc_security_struct *isec; 51862bf49690SThomas Liu struct common_audit_data ad; 5187275bb41eSDavid Howells u32 sid = current_sid(); 51881da177e4SLinus Torvalds 51891da177e4SLinus Torvalds isec = sma->sem_perm.security; 51901da177e4SLinus Torvalds 519150c205f5SEric Paris ad.type = LSM_AUDIT_DATA_IPC; 51921da177e4SLinus Torvalds ad.u.ipc_id = sma->sem_perm.key; 51931da177e4SLinus Torvalds 5194275bb41eSDavid Howells return avc_has_perm(sid, isec->sid, SECCLASS_SEM, 51951da177e4SLinus Torvalds SEM__ASSOCIATE, &ad); 51961da177e4SLinus Torvalds } 51971da177e4SLinus Torvalds 51981da177e4SLinus Torvalds /* Note, at this point, sma is locked down */ 51991da177e4SLinus Torvalds static int selinux_sem_semctl(struct sem_array *sma, int cmd) 52001da177e4SLinus Torvalds { 52011da177e4SLinus Torvalds int err; 52021da177e4SLinus Torvalds u32 perms; 52031da177e4SLinus Torvalds 52041da177e4SLinus Torvalds switch (cmd) { 52051da177e4SLinus Torvalds case IPC_INFO: 52061da177e4SLinus Torvalds case SEM_INFO: 52071da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 52081da177e4SLinus Torvalds return task_has_system(current, SYSTEM__IPC_INFO); 52091da177e4SLinus Torvalds case GETPID: 52101da177e4SLinus Torvalds case GETNCNT: 52111da177e4SLinus Torvalds case GETZCNT: 52121da177e4SLinus Torvalds perms = SEM__GETATTR; 52131da177e4SLinus Torvalds break; 52141da177e4SLinus Torvalds case GETVAL: 52151da177e4SLinus Torvalds case GETALL: 52161da177e4SLinus Torvalds perms = SEM__READ; 52171da177e4SLinus Torvalds break; 52181da177e4SLinus Torvalds case SETVAL: 52191da177e4SLinus Torvalds case SETALL: 52201da177e4SLinus Torvalds perms = SEM__WRITE; 52211da177e4SLinus Torvalds break; 52221da177e4SLinus Torvalds case IPC_RMID: 52231da177e4SLinus Torvalds perms = SEM__DESTROY; 52241da177e4SLinus Torvalds break; 52251da177e4SLinus Torvalds case IPC_SET: 52261da177e4SLinus Torvalds perms = SEM__SETATTR; 52271da177e4SLinus Torvalds break; 52281da177e4SLinus Torvalds case IPC_STAT: 52291da177e4SLinus Torvalds case SEM_STAT: 52301da177e4SLinus Torvalds perms = SEM__GETATTR | SEM__ASSOCIATE; 52311da177e4SLinus Torvalds break; 52321da177e4SLinus Torvalds default: 52331da177e4SLinus Torvalds return 0; 52341da177e4SLinus Torvalds } 52351da177e4SLinus Torvalds 52366af963f1SStephen Smalley err = ipc_has_perm(&sma->sem_perm, perms); 52371da177e4SLinus Torvalds return err; 52381da177e4SLinus Torvalds } 52391da177e4SLinus Torvalds 52401da177e4SLinus Torvalds static int selinux_sem_semop(struct sem_array *sma, 52411da177e4SLinus Torvalds struct sembuf *sops, unsigned nsops, int alter) 52421da177e4SLinus Torvalds { 52431da177e4SLinus Torvalds u32 perms; 52441da177e4SLinus Torvalds 52451da177e4SLinus Torvalds if (alter) 52461da177e4SLinus Torvalds perms = SEM__READ | SEM__WRITE; 52471da177e4SLinus Torvalds else 52481da177e4SLinus Torvalds perms = SEM__READ; 52491da177e4SLinus Torvalds 52506af963f1SStephen Smalley return ipc_has_perm(&sma->sem_perm, perms); 52511da177e4SLinus Torvalds } 52521da177e4SLinus Torvalds 52531da177e4SLinus Torvalds static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag) 52541da177e4SLinus Torvalds { 52551da177e4SLinus Torvalds u32 av = 0; 52561da177e4SLinus Torvalds 52571da177e4SLinus Torvalds av = 0; 52581da177e4SLinus Torvalds if (flag & S_IRUGO) 52591da177e4SLinus Torvalds av |= IPC__UNIX_READ; 52601da177e4SLinus Torvalds if (flag & S_IWUGO) 52611da177e4SLinus Torvalds av |= IPC__UNIX_WRITE; 52621da177e4SLinus Torvalds 52631da177e4SLinus Torvalds if (av == 0) 52641da177e4SLinus Torvalds return 0; 52651da177e4SLinus Torvalds 52666af963f1SStephen Smalley return ipc_has_perm(ipcp, av); 52671da177e4SLinus Torvalds } 52681da177e4SLinus Torvalds 5269713a04aeSAhmed S. Darwish static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid) 5270713a04aeSAhmed S. Darwish { 5271713a04aeSAhmed S. Darwish struct ipc_security_struct *isec = ipcp->security; 5272713a04aeSAhmed S. Darwish *secid = isec->sid; 5273713a04aeSAhmed S. Darwish } 5274713a04aeSAhmed S. Darwish 52751da177e4SLinus Torvalds static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode) 52761da177e4SLinus Torvalds { 52771da177e4SLinus Torvalds if (inode) 52781da177e4SLinus Torvalds inode_doinit_with_dentry(inode, dentry); 52791da177e4SLinus Torvalds } 52801da177e4SLinus Torvalds 52811da177e4SLinus Torvalds static int selinux_getprocattr(struct task_struct *p, 528204ff9708SAl Viro char *name, char **value) 52831da177e4SLinus Torvalds { 5284275bb41eSDavid Howells const struct task_security_struct *__tsec; 52858c8570fbSDustin Kirkland u32 sid; 52861da177e4SLinus Torvalds int error; 528704ff9708SAl Viro unsigned len; 52881da177e4SLinus Torvalds 52891da177e4SLinus Torvalds if (current != p) { 52903b11a1deSDavid Howells error = current_has_perm(p, PROCESS__GETATTR); 52911da177e4SLinus Torvalds if (error) 52921da177e4SLinus Torvalds return error; 52931da177e4SLinus Torvalds } 52941da177e4SLinus Torvalds 5295275bb41eSDavid Howells rcu_read_lock(); 5296275bb41eSDavid Howells __tsec = __task_cred(p)->security; 52971da177e4SLinus Torvalds 52981da177e4SLinus Torvalds if (!strcmp(name, "current")) 5299275bb41eSDavid Howells sid = __tsec->sid; 53001da177e4SLinus Torvalds else if (!strcmp(name, "prev")) 5301275bb41eSDavid Howells sid = __tsec->osid; 53021da177e4SLinus Torvalds else if (!strcmp(name, "exec")) 5303275bb41eSDavid Howells sid = __tsec->exec_sid; 53041da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 5305275bb41eSDavid Howells sid = __tsec->create_sid; 53064eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) 5307275bb41eSDavid Howells sid = __tsec->keycreate_sid; 530842c3e03eSEric Paris else if (!strcmp(name, "sockcreate")) 5309275bb41eSDavid Howells sid = __tsec->sockcreate_sid; 53101da177e4SLinus Torvalds else 5311275bb41eSDavid Howells goto invalid; 5312275bb41eSDavid Howells rcu_read_unlock(); 53131da177e4SLinus Torvalds 53141da177e4SLinus Torvalds if (!sid) 53151da177e4SLinus Torvalds return 0; 53161da177e4SLinus Torvalds 531704ff9708SAl Viro error = security_sid_to_context(sid, value, &len); 531804ff9708SAl Viro if (error) 531904ff9708SAl Viro return error; 532004ff9708SAl Viro return len; 5321275bb41eSDavid Howells 5322275bb41eSDavid Howells invalid: 5323275bb41eSDavid Howells rcu_read_unlock(); 5324275bb41eSDavid Howells return -EINVAL; 53251da177e4SLinus Torvalds } 53261da177e4SLinus Torvalds 53271da177e4SLinus Torvalds static int selinux_setprocattr(struct task_struct *p, 53281da177e4SLinus Torvalds char *name, void *value, size_t size) 53291da177e4SLinus Torvalds { 53301da177e4SLinus Torvalds struct task_security_struct *tsec; 53310356357cSRoland McGrath struct task_struct *tracer; 5332d84f4f99SDavid Howells struct cred *new; 5333d84f4f99SDavid Howells u32 sid = 0, ptsid; 53341da177e4SLinus Torvalds int error; 53351da177e4SLinus Torvalds char *str = value; 53361da177e4SLinus Torvalds 53371da177e4SLinus Torvalds if (current != p) { 53381da177e4SLinus Torvalds /* SELinux only allows a process to change its own 53391da177e4SLinus Torvalds security attributes. */ 53401da177e4SLinus Torvalds return -EACCES; 53411da177e4SLinus Torvalds } 53421da177e4SLinus Torvalds 53431da177e4SLinus Torvalds /* 53441da177e4SLinus Torvalds * Basic control over ability to set these attributes at all. 53451da177e4SLinus Torvalds * current == p, but we'll pass them separately in case the 53461da177e4SLinus Torvalds * above restriction is ever removed. 53471da177e4SLinus Torvalds */ 53481da177e4SLinus Torvalds if (!strcmp(name, "exec")) 53493b11a1deSDavid Howells error = current_has_perm(p, PROCESS__SETEXEC); 53501da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 53513b11a1deSDavid Howells error = current_has_perm(p, PROCESS__SETFSCREATE); 53524eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) 53533b11a1deSDavid Howells error = current_has_perm(p, PROCESS__SETKEYCREATE); 535442c3e03eSEric Paris else if (!strcmp(name, "sockcreate")) 53553b11a1deSDavid Howells error = current_has_perm(p, PROCESS__SETSOCKCREATE); 53561da177e4SLinus Torvalds else if (!strcmp(name, "current")) 53573b11a1deSDavid Howells error = current_has_perm(p, PROCESS__SETCURRENT); 53581da177e4SLinus Torvalds else 53591da177e4SLinus Torvalds error = -EINVAL; 53601da177e4SLinus Torvalds if (error) 53611da177e4SLinus Torvalds return error; 53621da177e4SLinus Torvalds 53631da177e4SLinus Torvalds /* Obtain a SID for the context, if one was specified. */ 53641da177e4SLinus Torvalds if (size && str[1] && str[1] != '\n') { 53651da177e4SLinus Torvalds if (str[size-1] == '\n') { 53661da177e4SLinus Torvalds str[size-1] = 0; 53671da177e4SLinus Torvalds size--; 53681da177e4SLinus Torvalds } 53691da177e4SLinus Torvalds error = security_context_to_sid(value, size, &sid); 537012b29f34SStephen Smalley if (error == -EINVAL && !strcmp(name, "fscreate")) { 5371d6ea83ecSEric Paris if (!capable(CAP_MAC_ADMIN)) { 5372d6ea83ecSEric Paris struct audit_buffer *ab; 5373d6ea83ecSEric Paris size_t audit_size; 5374d6ea83ecSEric Paris 5375d6ea83ecSEric Paris /* We strip a nul only if it is at the end, otherwise the 5376d6ea83ecSEric Paris * context contains a nul and we should audit that */ 5377d6ea83ecSEric Paris if (str[size - 1] == '\0') 5378d6ea83ecSEric Paris audit_size = size - 1; 5379d6ea83ecSEric Paris else 5380d6ea83ecSEric Paris audit_size = size; 5381d6ea83ecSEric Paris ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR); 5382d6ea83ecSEric Paris audit_log_format(ab, "op=fscreate invalid_context="); 5383d6ea83ecSEric Paris audit_log_n_untrustedstring(ab, value, audit_size); 5384d6ea83ecSEric Paris audit_log_end(ab); 5385d6ea83ecSEric Paris 538612b29f34SStephen Smalley return error; 5387d6ea83ecSEric Paris } 538812b29f34SStephen Smalley error = security_context_to_sid_force(value, size, 538912b29f34SStephen Smalley &sid); 539012b29f34SStephen Smalley } 53911da177e4SLinus Torvalds if (error) 53921da177e4SLinus Torvalds return error; 53931da177e4SLinus Torvalds } 53941da177e4SLinus Torvalds 5395d84f4f99SDavid Howells new = prepare_creds(); 5396d84f4f99SDavid Howells if (!new) 5397d84f4f99SDavid Howells return -ENOMEM; 5398d84f4f99SDavid Howells 53991da177e4SLinus Torvalds /* Permission checking based on the specified context is 54001da177e4SLinus Torvalds performed during the actual operation (execve, 54011da177e4SLinus Torvalds open/mkdir/...), when we know the full context of the 5402d84f4f99SDavid Howells operation. See selinux_bprm_set_creds for the execve 54031da177e4SLinus Torvalds checks and may_create for the file creation checks. The 54041da177e4SLinus Torvalds operation will then fail if the context is not permitted. */ 5405d84f4f99SDavid Howells tsec = new->security; 5406d84f4f99SDavid Howells if (!strcmp(name, "exec")) { 54071da177e4SLinus Torvalds tsec->exec_sid = sid; 5408d84f4f99SDavid Howells } else if (!strcmp(name, "fscreate")) { 54091da177e4SLinus Torvalds tsec->create_sid = sid; 5410d84f4f99SDavid Howells } else if (!strcmp(name, "keycreate")) { 54114eb582cfSMichael LeMay error = may_create_key(sid, p); 54124eb582cfSMichael LeMay if (error) 5413d84f4f99SDavid Howells goto abort_change; 54144eb582cfSMichael LeMay tsec->keycreate_sid = sid; 5415d84f4f99SDavid Howells } else if (!strcmp(name, "sockcreate")) { 541642c3e03eSEric Paris tsec->sockcreate_sid = sid; 5417d84f4f99SDavid Howells } else if (!strcmp(name, "current")) { 5418d84f4f99SDavid Howells error = -EINVAL; 54191da177e4SLinus Torvalds if (sid == 0) 5420d84f4f99SDavid Howells goto abort_change; 5421d9250deaSKaiGai Kohei 5422d84f4f99SDavid Howells /* Only allow single threaded processes to change context */ 5423d84f4f99SDavid Howells error = -EPERM; 54245bb459bbSOleg Nesterov if (!current_is_single_threaded()) { 5425d84f4f99SDavid Howells error = security_bounded_transition(tsec->sid, sid); 5426d84f4f99SDavid Howells if (error) 5427d84f4f99SDavid Howells goto abort_change; 54281da177e4SLinus Torvalds } 54291da177e4SLinus Torvalds 54301da177e4SLinus Torvalds /* Check permissions for the transition. */ 54311da177e4SLinus Torvalds error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS, 54321da177e4SLinus Torvalds PROCESS__DYNTRANSITION, NULL); 54331da177e4SLinus Torvalds if (error) 5434d84f4f99SDavid Howells goto abort_change; 54351da177e4SLinus Torvalds 54361da177e4SLinus Torvalds /* Check for ptracing, and update the task SID if ok. 54371da177e4SLinus Torvalds Otherwise, leave SID unchanged and fail. */ 5438d84f4f99SDavid Howells ptsid = 0; 54391da177e4SLinus Torvalds task_lock(p); 544006d98473STejun Heo tracer = ptrace_parent(p); 5441d84f4f99SDavid Howells if (tracer) 5442d84f4f99SDavid Howells ptsid = task_sid(tracer); 54431da177e4SLinus Torvalds task_unlock(p); 54441da177e4SLinus Torvalds 5445d84f4f99SDavid Howells if (tracer) { 5446d84f4f99SDavid Howells error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS, 5447d84f4f99SDavid Howells PROCESS__PTRACE, NULL); 5448d84f4f99SDavid Howells if (error) 5449d84f4f99SDavid Howells goto abort_change; 5450d84f4f99SDavid Howells } 5451d84f4f99SDavid Howells 5452d84f4f99SDavid Howells tsec->sid = sid; 5453d84f4f99SDavid Howells } else { 5454d84f4f99SDavid Howells error = -EINVAL; 5455d84f4f99SDavid Howells goto abort_change; 5456d84f4f99SDavid Howells } 5457d84f4f99SDavid Howells 5458d84f4f99SDavid Howells commit_creds(new); 54591da177e4SLinus Torvalds return size; 5460d84f4f99SDavid Howells 5461d84f4f99SDavid Howells abort_change: 5462d84f4f99SDavid Howells abort_creds(new); 5463d84f4f99SDavid Howells return error; 54641da177e4SLinus Torvalds } 54651da177e4SLinus Torvalds 5466dc49c1f9SCatherine Zhang static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) 5467dc49c1f9SCatherine Zhang { 5468dc49c1f9SCatherine Zhang return security_sid_to_context(secid, secdata, seclen); 5469dc49c1f9SCatherine Zhang } 5470dc49c1f9SCatherine Zhang 54717bf570dcSDavid Howells static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid) 547263cb3449SDavid Howells { 547363cb3449SDavid Howells return security_context_to_sid(secdata, seclen, secid); 547463cb3449SDavid Howells } 547563cb3449SDavid Howells 5476dc49c1f9SCatherine Zhang static void selinux_release_secctx(char *secdata, u32 seclen) 5477dc49c1f9SCatherine Zhang { 5478dc49c1f9SCatherine Zhang kfree(secdata); 5479dc49c1f9SCatherine Zhang } 5480dc49c1f9SCatherine Zhang 54811ee65e37SDavid P. Quigley /* 54821ee65e37SDavid P. Quigley * called with inode->i_mutex locked 54831ee65e37SDavid P. Quigley */ 54841ee65e37SDavid P. Quigley static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen) 54851ee65e37SDavid P. Quigley { 54861ee65e37SDavid P. Quigley return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0); 54871ee65e37SDavid P. Quigley } 54881ee65e37SDavid P. Quigley 54891ee65e37SDavid P. Quigley /* 54901ee65e37SDavid P. Quigley * called with inode->i_mutex locked 54911ee65e37SDavid P. Quigley */ 54921ee65e37SDavid P. Quigley static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen) 54931ee65e37SDavid P. Quigley { 54941ee65e37SDavid P. Quigley return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0); 54951ee65e37SDavid P. Quigley } 54961ee65e37SDavid P. Quigley 54971ee65e37SDavid P. Quigley static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen) 54981ee65e37SDavid P. Quigley { 54991ee65e37SDavid P. Quigley int len = 0; 55001ee65e37SDavid P. Quigley len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX, 55011ee65e37SDavid P. Quigley ctx, true); 55021ee65e37SDavid P. Quigley if (len < 0) 55031ee65e37SDavid P. Quigley return len; 55041ee65e37SDavid P. Quigley *ctxlen = len; 55051ee65e37SDavid P. Quigley return 0; 55061ee65e37SDavid P. Quigley } 5507d720024eSMichael LeMay #ifdef CONFIG_KEYS 5508d720024eSMichael LeMay 5509d84f4f99SDavid Howells static int selinux_key_alloc(struct key *k, const struct cred *cred, 55107e047ef5SDavid Howells unsigned long flags) 5511d720024eSMichael LeMay { 5512d84f4f99SDavid Howells const struct task_security_struct *tsec; 5513d720024eSMichael LeMay struct key_security_struct *ksec; 5514d720024eSMichael LeMay 5515d720024eSMichael LeMay ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL); 5516d720024eSMichael LeMay if (!ksec) 5517d720024eSMichael LeMay return -ENOMEM; 5518d720024eSMichael LeMay 5519d84f4f99SDavid Howells tsec = cred->security; 5520d84f4f99SDavid Howells if (tsec->keycreate_sid) 5521d84f4f99SDavid Howells ksec->sid = tsec->keycreate_sid; 55224eb582cfSMichael LeMay else 5523d84f4f99SDavid Howells ksec->sid = tsec->sid; 5524d720024eSMichael LeMay 5525275bb41eSDavid Howells k->security = ksec; 5526d720024eSMichael LeMay return 0; 5527d720024eSMichael LeMay } 5528d720024eSMichael LeMay 5529d720024eSMichael LeMay static void selinux_key_free(struct key *k) 5530d720024eSMichael LeMay { 5531d720024eSMichael LeMay struct key_security_struct *ksec = k->security; 5532d720024eSMichael LeMay 5533d720024eSMichael LeMay k->security = NULL; 5534d720024eSMichael LeMay kfree(ksec); 5535d720024eSMichael LeMay } 5536d720024eSMichael LeMay 5537d720024eSMichael LeMay static int selinux_key_permission(key_ref_t key_ref, 5538d84f4f99SDavid Howells const struct cred *cred, 5539d720024eSMichael LeMay key_perm_t perm) 5540d720024eSMichael LeMay { 5541d720024eSMichael LeMay struct key *key; 5542d720024eSMichael LeMay struct key_security_struct *ksec; 5543275bb41eSDavid Howells u32 sid; 5544d720024eSMichael LeMay 5545d720024eSMichael LeMay /* if no specific permissions are requested, we skip the 5546d720024eSMichael LeMay permission check. No serious, additional covert channels 5547d720024eSMichael LeMay appear to be created. */ 5548d720024eSMichael LeMay if (perm == 0) 5549d720024eSMichael LeMay return 0; 5550d720024eSMichael LeMay 5551d84f4f99SDavid Howells sid = cred_sid(cred); 5552275bb41eSDavid Howells 5553275bb41eSDavid Howells key = key_ref_to_ptr(key_ref); 5554275bb41eSDavid Howells ksec = key->security; 5555275bb41eSDavid Howells 5556275bb41eSDavid Howells return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL); 5557d720024eSMichael LeMay } 5558d720024eSMichael LeMay 555970a5bb72SDavid Howells static int selinux_key_getsecurity(struct key *key, char **_buffer) 556070a5bb72SDavid Howells { 556170a5bb72SDavid Howells struct key_security_struct *ksec = key->security; 556270a5bb72SDavid Howells char *context = NULL; 556370a5bb72SDavid Howells unsigned len; 556470a5bb72SDavid Howells int rc; 556570a5bb72SDavid Howells 556670a5bb72SDavid Howells rc = security_sid_to_context(ksec->sid, &context, &len); 556770a5bb72SDavid Howells if (!rc) 556870a5bb72SDavid Howells rc = len; 556970a5bb72SDavid Howells *_buffer = context; 557070a5bb72SDavid Howells return rc; 557170a5bb72SDavid Howells } 557270a5bb72SDavid Howells 5573d720024eSMichael LeMay #endif 5574d720024eSMichael LeMay 55751da177e4SLinus Torvalds static struct security_operations selinux_ops = { 5576076c54c5SAhmed S. Darwish .name = "selinux", 5577076c54c5SAhmed S. Darwish 55789e48858fSIngo Molnar .ptrace_access_check = selinux_ptrace_access_check, 55795cd9c58fSDavid Howells .ptrace_traceme = selinux_ptrace_traceme, 55801da177e4SLinus Torvalds .capget = selinux_capget, 5581d84f4f99SDavid Howells .capset = selinux_capset, 55821da177e4SLinus Torvalds .capable = selinux_capable, 55831da177e4SLinus Torvalds .quotactl = selinux_quotactl, 55841da177e4SLinus Torvalds .quota_on = selinux_quota_on, 55851da177e4SLinus Torvalds .syslog = selinux_syslog, 55861da177e4SLinus Torvalds .vm_enough_memory = selinux_vm_enough_memory, 55871da177e4SLinus Torvalds 55881da177e4SLinus Torvalds .netlink_send = selinux_netlink_send, 55891da177e4SLinus Torvalds 5590a6f76f23SDavid Howells .bprm_set_creds = selinux_bprm_set_creds, 5591a6f76f23SDavid Howells .bprm_committing_creds = selinux_bprm_committing_creds, 5592a6f76f23SDavid Howells .bprm_committed_creds = selinux_bprm_committed_creds, 55931da177e4SLinus Torvalds .bprm_secureexec = selinux_bprm_secureexec, 55941da177e4SLinus Torvalds 55951da177e4SLinus Torvalds .sb_alloc_security = selinux_sb_alloc_security, 55961da177e4SLinus Torvalds .sb_free_security = selinux_sb_free_security, 55971da177e4SLinus Torvalds .sb_copy_data = selinux_sb_copy_data, 5598026eb167SEric Paris .sb_remount = selinux_sb_remount, 55991da177e4SLinus Torvalds .sb_kern_mount = selinux_sb_kern_mount, 56002069f457SEric Paris .sb_show_options = selinux_sb_show_options, 56011da177e4SLinus Torvalds .sb_statfs = selinux_sb_statfs, 56021da177e4SLinus Torvalds .sb_mount = selinux_mount, 56031da177e4SLinus Torvalds .sb_umount = selinux_umount, 5604c9180a57SEric Paris .sb_set_mnt_opts = selinux_set_mnt_opts, 5605c9180a57SEric Paris .sb_clone_mnt_opts = selinux_sb_clone_mnt_opts, 5606e0007529SEric Paris .sb_parse_opts_str = selinux_parse_opts_str, 5607e0007529SEric Paris 56081da177e4SLinus Torvalds 56091da177e4SLinus Torvalds .inode_alloc_security = selinux_inode_alloc_security, 56101da177e4SLinus Torvalds .inode_free_security = selinux_inode_free_security, 56115e41ff9eSStephen Smalley .inode_init_security = selinux_inode_init_security, 56121da177e4SLinus Torvalds .inode_create = selinux_inode_create, 56131da177e4SLinus Torvalds .inode_link = selinux_inode_link, 56141da177e4SLinus Torvalds .inode_unlink = selinux_inode_unlink, 56151da177e4SLinus Torvalds .inode_symlink = selinux_inode_symlink, 56161da177e4SLinus Torvalds .inode_mkdir = selinux_inode_mkdir, 56171da177e4SLinus Torvalds .inode_rmdir = selinux_inode_rmdir, 56181da177e4SLinus Torvalds .inode_mknod = selinux_inode_mknod, 56191da177e4SLinus Torvalds .inode_rename = selinux_inode_rename, 56201da177e4SLinus Torvalds .inode_readlink = selinux_inode_readlink, 56211da177e4SLinus Torvalds .inode_follow_link = selinux_inode_follow_link, 56221da177e4SLinus Torvalds .inode_permission = selinux_inode_permission, 56231da177e4SLinus Torvalds .inode_setattr = selinux_inode_setattr, 56241da177e4SLinus Torvalds .inode_getattr = selinux_inode_getattr, 56251da177e4SLinus Torvalds .inode_setxattr = selinux_inode_setxattr, 56261da177e4SLinus Torvalds .inode_post_setxattr = selinux_inode_post_setxattr, 56271da177e4SLinus Torvalds .inode_getxattr = selinux_inode_getxattr, 56281da177e4SLinus Torvalds .inode_listxattr = selinux_inode_listxattr, 56291da177e4SLinus Torvalds .inode_removexattr = selinux_inode_removexattr, 56301da177e4SLinus Torvalds .inode_getsecurity = selinux_inode_getsecurity, 56311da177e4SLinus Torvalds .inode_setsecurity = selinux_inode_setsecurity, 56321da177e4SLinus Torvalds .inode_listsecurity = selinux_inode_listsecurity, 5633713a04aeSAhmed S. Darwish .inode_getsecid = selinux_inode_getsecid, 56341da177e4SLinus Torvalds 56351da177e4SLinus Torvalds .file_permission = selinux_file_permission, 56361da177e4SLinus Torvalds .file_alloc_security = selinux_file_alloc_security, 56371da177e4SLinus Torvalds .file_free_security = selinux_file_free_security, 56381da177e4SLinus Torvalds .file_ioctl = selinux_file_ioctl, 5639e5467859SAl Viro .mmap_file = selinux_mmap_file, 5640e5467859SAl Viro .mmap_addr = selinux_mmap_addr, 56411da177e4SLinus Torvalds .file_mprotect = selinux_file_mprotect, 56421da177e4SLinus Torvalds .file_lock = selinux_file_lock, 56431da177e4SLinus Torvalds .file_fcntl = selinux_file_fcntl, 56441da177e4SLinus Torvalds .file_set_fowner = selinux_file_set_fowner, 56451da177e4SLinus Torvalds .file_send_sigiotask = selinux_file_send_sigiotask, 56461da177e4SLinus Torvalds .file_receive = selinux_file_receive, 56471da177e4SLinus Torvalds 564883d49856SEric Paris .file_open = selinux_file_open, 5649788e7dd4SYuichi Nakamura 56501da177e4SLinus Torvalds .task_create = selinux_task_create, 5651ee18d64cSDavid Howells .cred_alloc_blank = selinux_cred_alloc_blank, 5652f1752eecSDavid Howells .cred_free = selinux_cred_free, 5653d84f4f99SDavid Howells .cred_prepare = selinux_cred_prepare, 5654ee18d64cSDavid Howells .cred_transfer = selinux_cred_transfer, 56553a3b7ce9SDavid Howells .kernel_act_as = selinux_kernel_act_as, 56563a3b7ce9SDavid Howells .kernel_create_files_as = selinux_kernel_create_files_as, 565725354c4fSEric Paris .kernel_module_request = selinux_kernel_module_request, 56581da177e4SLinus Torvalds .task_setpgid = selinux_task_setpgid, 56591da177e4SLinus Torvalds .task_getpgid = selinux_task_getpgid, 56601da177e4SLinus Torvalds .task_getsid = selinux_task_getsid, 5661f9008e4cSDavid Quigley .task_getsecid = selinux_task_getsecid, 56621da177e4SLinus Torvalds .task_setnice = selinux_task_setnice, 566303e68060SJames Morris .task_setioprio = selinux_task_setioprio, 5664a1836a42SDavid Quigley .task_getioprio = selinux_task_getioprio, 56651da177e4SLinus Torvalds .task_setrlimit = selinux_task_setrlimit, 56661da177e4SLinus Torvalds .task_setscheduler = selinux_task_setscheduler, 56671da177e4SLinus Torvalds .task_getscheduler = selinux_task_getscheduler, 566835601547SDavid Quigley .task_movememory = selinux_task_movememory, 56691da177e4SLinus Torvalds .task_kill = selinux_task_kill, 56701da177e4SLinus Torvalds .task_wait = selinux_task_wait, 56711da177e4SLinus Torvalds .task_to_inode = selinux_task_to_inode, 56721da177e4SLinus Torvalds 56731da177e4SLinus Torvalds .ipc_permission = selinux_ipc_permission, 5674713a04aeSAhmed S. Darwish .ipc_getsecid = selinux_ipc_getsecid, 56751da177e4SLinus Torvalds 56761da177e4SLinus Torvalds .msg_msg_alloc_security = selinux_msg_msg_alloc_security, 56771da177e4SLinus Torvalds .msg_msg_free_security = selinux_msg_msg_free_security, 56781da177e4SLinus Torvalds 56791da177e4SLinus Torvalds .msg_queue_alloc_security = selinux_msg_queue_alloc_security, 56801da177e4SLinus Torvalds .msg_queue_free_security = selinux_msg_queue_free_security, 56811da177e4SLinus Torvalds .msg_queue_associate = selinux_msg_queue_associate, 56821da177e4SLinus Torvalds .msg_queue_msgctl = selinux_msg_queue_msgctl, 56831da177e4SLinus Torvalds .msg_queue_msgsnd = selinux_msg_queue_msgsnd, 56841da177e4SLinus Torvalds .msg_queue_msgrcv = selinux_msg_queue_msgrcv, 56851da177e4SLinus Torvalds 56861da177e4SLinus Torvalds .shm_alloc_security = selinux_shm_alloc_security, 56871da177e4SLinus Torvalds .shm_free_security = selinux_shm_free_security, 56881da177e4SLinus Torvalds .shm_associate = selinux_shm_associate, 56891da177e4SLinus Torvalds .shm_shmctl = selinux_shm_shmctl, 56901da177e4SLinus Torvalds .shm_shmat = selinux_shm_shmat, 56911da177e4SLinus Torvalds 56921da177e4SLinus Torvalds .sem_alloc_security = selinux_sem_alloc_security, 56931da177e4SLinus Torvalds .sem_free_security = selinux_sem_free_security, 56941da177e4SLinus Torvalds .sem_associate = selinux_sem_associate, 56951da177e4SLinus Torvalds .sem_semctl = selinux_sem_semctl, 56961da177e4SLinus Torvalds .sem_semop = selinux_sem_semop, 56971da177e4SLinus Torvalds 56981da177e4SLinus Torvalds .d_instantiate = selinux_d_instantiate, 56991da177e4SLinus Torvalds 57001da177e4SLinus Torvalds .getprocattr = selinux_getprocattr, 57011da177e4SLinus Torvalds .setprocattr = selinux_setprocattr, 57021da177e4SLinus Torvalds 5703dc49c1f9SCatherine Zhang .secid_to_secctx = selinux_secid_to_secctx, 570463cb3449SDavid Howells .secctx_to_secid = selinux_secctx_to_secid, 5705dc49c1f9SCatherine Zhang .release_secctx = selinux_release_secctx, 57061ee65e37SDavid P. Quigley .inode_notifysecctx = selinux_inode_notifysecctx, 57071ee65e37SDavid P. Quigley .inode_setsecctx = selinux_inode_setsecctx, 57081ee65e37SDavid P. Quigley .inode_getsecctx = selinux_inode_getsecctx, 5709dc49c1f9SCatherine Zhang 57101da177e4SLinus Torvalds .unix_stream_connect = selinux_socket_unix_stream_connect, 57111da177e4SLinus Torvalds .unix_may_send = selinux_socket_unix_may_send, 57121da177e4SLinus Torvalds 57131da177e4SLinus Torvalds .socket_create = selinux_socket_create, 57141da177e4SLinus Torvalds .socket_post_create = selinux_socket_post_create, 57151da177e4SLinus Torvalds .socket_bind = selinux_socket_bind, 57161da177e4SLinus Torvalds .socket_connect = selinux_socket_connect, 57171da177e4SLinus Torvalds .socket_listen = selinux_socket_listen, 57181da177e4SLinus Torvalds .socket_accept = selinux_socket_accept, 57191da177e4SLinus Torvalds .socket_sendmsg = selinux_socket_sendmsg, 57201da177e4SLinus Torvalds .socket_recvmsg = selinux_socket_recvmsg, 57211da177e4SLinus Torvalds .socket_getsockname = selinux_socket_getsockname, 57221da177e4SLinus Torvalds .socket_getpeername = selinux_socket_getpeername, 57231da177e4SLinus Torvalds .socket_getsockopt = selinux_socket_getsockopt, 57241da177e4SLinus Torvalds .socket_setsockopt = selinux_socket_setsockopt, 57251da177e4SLinus Torvalds .socket_shutdown = selinux_socket_shutdown, 57261da177e4SLinus Torvalds .socket_sock_rcv_skb = selinux_socket_sock_rcv_skb, 57272c7946a7SCatherine Zhang .socket_getpeersec_stream = selinux_socket_getpeersec_stream, 57282c7946a7SCatherine Zhang .socket_getpeersec_dgram = selinux_socket_getpeersec_dgram, 57291da177e4SLinus Torvalds .sk_alloc_security = selinux_sk_alloc_security, 57301da177e4SLinus Torvalds .sk_free_security = selinux_sk_free_security, 5731892c141eSVenkat Yekkirala .sk_clone_security = selinux_sk_clone_security, 5732beb8d13bSVenkat Yekkirala .sk_getsecid = selinux_sk_getsecid, 57334237c75cSVenkat Yekkirala .sock_graft = selinux_sock_graft, 57344237c75cSVenkat Yekkirala .inet_conn_request = selinux_inet_conn_request, 57354237c75cSVenkat Yekkirala .inet_csk_clone = selinux_inet_csk_clone, 57366b877699SVenkat Yekkirala .inet_conn_established = selinux_inet_conn_established, 57372606fd1fSEric Paris .secmark_relabel_packet = selinux_secmark_relabel_packet, 57382606fd1fSEric Paris .secmark_refcount_inc = selinux_secmark_refcount_inc, 57392606fd1fSEric Paris .secmark_refcount_dec = selinux_secmark_refcount_dec, 57404237c75cSVenkat Yekkirala .req_classify_flow = selinux_req_classify_flow, 57415dbbaf2dSPaul Moore .tun_dev_alloc_security = selinux_tun_dev_alloc_security, 57425dbbaf2dSPaul Moore .tun_dev_free_security = selinux_tun_dev_free_security, 5743ed6d76e4SPaul Moore .tun_dev_create = selinux_tun_dev_create, 57445dbbaf2dSPaul Moore .tun_dev_attach_queue = selinux_tun_dev_attach_queue, 5745ed6d76e4SPaul Moore .tun_dev_attach = selinux_tun_dev_attach, 57465dbbaf2dSPaul Moore .tun_dev_open = selinux_tun_dev_open, 5747ca10b9e9SEric Dumazet .skb_owned_by = selinux_skb_owned_by, 5748d28d1e08STrent Jaeger 5749d28d1e08STrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM 5750d28d1e08STrent Jaeger .xfrm_policy_alloc_security = selinux_xfrm_policy_alloc, 5751d28d1e08STrent Jaeger .xfrm_policy_clone_security = selinux_xfrm_policy_clone, 5752d28d1e08STrent Jaeger .xfrm_policy_free_security = selinux_xfrm_policy_free, 5753c8c05a8eSCatherine Zhang .xfrm_policy_delete_security = selinux_xfrm_policy_delete, 57542e5aa866SPaul Moore .xfrm_state_alloc = selinux_xfrm_state_alloc, 57552e5aa866SPaul Moore .xfrm_state_alloc_acquire = selinux_xfrm_state_alloc_acquire, 5756d28d1e08STrent Jaeger .xfrm_state_free_security = selinux_xfrm_state_free, 5757c8c05a8eSCatherine Zhang .xfrm_state_delete_security = selinux_xfrm_state_delete, 5758d28d1e08STrent Jaeger .xfrm_policy_lookup = selinux_xfrm_policy_lookup, 5759e0d1caa7SVenkat Yekkirala .xfrm_state_pol_flow_match = selinux_xfrm_state_pol_flow_match, 5760e0d1caa7SVenkat Yekkirala .xfrm_decode_session = selinux_xfrm_decode_session, 57611da177e4SLinus Torvalds #endif 5762d720024eSMichael LeMay 5763d720024eSMichael LeMay #ifdef CONFIG_KEYS 5764d720024eSMichael LeMay .key_alloc = selinux_key_alloc, 5765d720024eSMichael LeMay .key_free = selinux_key_free, 5766d720024eSMichael LeMay .key_permission = selinux_key_permission, 576770a5bb72SDavid Howells .key_getsecurity = selinux_key_getsecurity, 5768d720024eSMichael LeMay #endif 57699d57a7f9SAhmed S. Darwish 57709d57a7f9SAhmed S. Darwish #ifdef CONFIG_AUDIT 57719d57a7f9SAhmed S. Darwish .audit_rule_init = selinux_audit_rule_init, 57729d57a7f9SAhmed S. Darwish .audit_rule_known = selinux_audit_rule_known, 57739d57a7f9SAhmed S. Darwish .audit_rule_match = selinux_audit_rule_match, 57749d57a7f9SAhmed S. Darwish .audit_rule_free = selinux_audit_rule_free, 57759d57a7f9SAhmed S. Darwish #endif 57761da177e4SLinus Torvalds }; 57771da177e4SLinus Torvalds 57781da177e4SLinus Torvalds static __init int selinux_init(void) 57791da177e4SLinus Torvalds { 5780076c54c5SAhmed S. Darwish if (!security_module_enable(&selinux_ops)) { 5781076c54c5SAhmed S. Darwish selinux_enabled = 0; 5782076c54c5SAhmed S. Darwish return 0; 5783076c54c5SAhmed S. Darwish } 5784076c54c5SAhmed S. Darwish 57851da177e4SLinus Torvalds if (!selinux_enabled) { 57861da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Disabled at boot.\n"); 57871da177e4SLinus Torvalds return 0; 57881da177e4SLinus Torvalds } 57891da177e4SLinus Torvalds 57901da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Initializing.\n"); 57911da177e4SLinus Torvalds 57921da177e4SLinus Torvalds /* Set the security state for the initial task. */ 5793d84f4f99SDavid Howells cred_init_security(); 57941da177e4SLinus Torvalds 5795fcaaade1SStephen Smalley default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC); 5796fcaaade1SStephen Smalley 57977cae7e26SJames Morris sel_inode_cache = kmem_cache_create("selinux_inode_security", 57987cae7e26SJames Morris sizeof(struct inode_security_struct), 579920c2df83SPaul Mundt 0, SLAB_PANIC, NULL); 58001da177e4SLinus Torvalds avc_init(); 58011da177e4SLinus Torvalds 58021da177e4SLinus Torvalds if (register_security(&selinux_ops)) 58031da177e4SLinus Torvalds panic("SELinux: Unable to register with kernel.\n"); 58041da177e4SLinus Torvalds 5805828dfe1dSEric Paris if (selinux_enforcing) 5806fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Starting in enforcing mode\n"); 5807828dfe1dSEric Paris else 5808fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Starting in permissive mode\n"); 5809d720024eSMichael LeMay 58101da177e4SLinus Torvalds return 0; 58111da177e4SLinus Torvalds } 58121da177e4SLinus Torvalds 5813e8c26255SAl Viro static void delayed_superblock_init(struct super_block *sb, void *unused) 5814e8c26255SAl Viro { 5815e8c26255SAl Viro superblock_doinit(sb, NULL); 5816e8c26255SAl Viro } 5817e8c26255SAl Viro 58181da177e4SLinus Torvalds void selinux_complete_init(void) 58191da177e4SLinus Torvalds { 5820fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Completing initialization.\n"); 58211da177e4SLinus Torvalds 58221da177e4SLinus Torvalds /* Set up any superblocks initialized prior to the policy load. */ 5823fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Setting up existing superblocks.\n"); 5824e8c26255SAl Viro iterate_supers(delayed_superblock_init, NULL); 58251da177e4SLinus Torvalds } 58261da177e4SLinus Torvalds 58271da177e4SLinus Torvalds /* SELinux requires early initialization in order to label 58281da177e4SLinus Torvalds all processes and objects when they are created. */ 58291da177e4SLinus Torvalds security_initcall(selinux_init); 58301da177e4SLinus Torvalds 5831c2b507fdSStephen Smalley #if defined(CONFIG_NETFILTER) 58321da177e4SLinus Torvalds 5833effad8dfSPaul Moore static struct nf_hook_ops selinux_ipv4_ops[] = { 5834effad8dfSPaul Moore { 5835effad8dfSPaul Moore .hook = selinux_ipv4_postroute, 58361da177e4SLinus Torvalds .owner = THIS_MODULE, 58372597a834SAlban Crequy .pf = NFPROTO_IPV4, 58386e23ae2aSPatrick McHardy .hooknum = NF_INET_POST_ROUTING, 58391da177e4SLinus Torvalds .priority = NF_IP_PRI_SELINUX_LAST, 5840effad8dfSPaul Moore }, 5841effad8dfSPaul Moore { 5842effad8dfSPaul Moore .hook = selinux_ipv4_forward, 5843effad8dfSPaul Moore .owner = THIS_MODULE, 58442597a834SAlban Crequy .pf = NFPROTO_IPV4, 5845effad8dfSPaul Moore .hooknum = NF_INET_FORWARD, 5846effad8dfSPaul Moore .priority = NF_IP_PRI_SELINUX_FIRST, 5847948bf85cSPaul Moore }, 5848948bf85cSPaul Moore { 5849948bf85cSPaul Moore .hook = selinux_ipv4_output, 5850948bf85cSPaul Moore .owner = THIS_MODULE, 58512597a834SAlban Crequy .pf = NFPROTO_IPV4, 5852948bf85cSPaul Moore .hooknum = NF_INET_LOCAL_OUT, 5853948bf85cSPaul Moore .priority = NF_IP_PRI_SELINUX_FIRST, 5854effad8dfSPaul Moore } 58551da177e4SLinus Torvalds }; 58561da177e4SLinus Torvalds 58571da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 58581da177e4SLinus Torvalds 5859effad8dfSPaul Moore static struct nf_hook_ops selinux_ipv6_ops[] = { 5860effad8dfSPaul Moore { 5861effad8dfSPaul Moore .hook = selinux_ipv6_postroute, 58621da177e4SLinus Torvalds .owner = THIS_MODULE, 58632597a834SAlban Crequy .pf = NFPROTO_IPV6, 58646e23ae2aSPatrick McHardy .hooknum = NF_INET_POST_ROUTING, 58651da177e4SLinus Torvalds .priority = NF_IP6_PRI_SELINUX_LAST, 5866effad8dfSPaul Moore }, 5867effad8dfSPaul Moore { 5868effad8dfSPaul Moore .hook = selinux_ipv6_forward, 5869effad8dfSPaul Moore .owner = THIS_MODULE, 58702597a834SAlban Crequy .pf = NFPROTO_IPV6, 5871effad8dfSPaul Moore .hooknum = NF_INET_FORWARD, 5872effad8dfSPaul Moore .priority = NF_IP6_PRI_SELINUX_FIRST, 5873effad8dfSPaul Moore } 58741da177e4SLinus Torvalds }; 58751da177e4SLinus Torvalds 58761da177e4SLinus Torvalds #endif /* IPV6 */ 58771da177e4SLinus Torvalds 58781da177e4SLinus Torvalds static int __init selinux_nf_ip_init(void) 58791da177e4SLinus Torvalds { 58801da177e4SLinus Torvalds int err = 0; 58811da177e4SLinus Torvalds 58821da177e4SLinus Torvalds if (!selinux_enabled) 58831da177e4SLinus Torvalds goto out; 58841da177e4SLinus Torvalds 5885fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Registering netfilter hooks\n"); 58861da177e4SLinus Torvalds 58876c5a9d2eSAlexey Dobriyan err = nf_register_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops)); 58881da177e4SLinus Torvalds if (err) 58896c5a9d2eSAlexey Dobriyan panic("SELinux: nf_register_hooks for IPv4: error %d\n", err); 58901da177e4SLinus Torvalds 58911da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 58926c5a9d2eSAlexey Dobriyan err = nf_register_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops)); 58931da177e4SLinus Torvalds if (err) 58946c5a9d2eSAlexey Dobriyan panic("SELinux: nf_register_hooks for IPv6: error %d\n", err); 58951da177e4SLinus Torvalds #endif /* IPV6 */ 5896d28d1e08STrent Jaeger 58971da177e4SLinus Torvalds out: 58981da177e4SLinus Torvalds return err; 58991da177e4SLinus Torvalds } 59001da177e4SLinus Torvalds 59011da177e4SLinus Torvalds __initcall(selinux_nf_ip_init); 59021da177e4SLinus Torvalds 59031da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 59041da177e4SLinus Torvalds static void selinux_nf_ip_exit(void) 59051da177e4SLinus Torvalds { 5906fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Unregistering netfilter hooks\n"); 59071da177e4SLinus Torvalds 59086c5a9d2eSAlexey Dobriyan nf_unregister_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops)); 59091da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 59106c5a9d2eSAlexey Dobriyan nf_unregister_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops)); 59111da177e4SLinus Torvalds #endif /* IPV6 */ 59121da177e4SLinus Torvalds } 59131da177e4SLinus Torvalds #endif 59141da177e4SLinus Torvalds 5915c2b507fdSStephen Smalley #else /* CONFIG_NETFILTER */ 59161da177e4SLinus Torvalds 59171da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 59181da177e4SLinus Torvalds #define selinux_nf_ip_exit() 59191da177e4SLinus Torvalds #endif 59201da177e4SLinus Torvalds 5921c2b507fdSStephen Smalley #endif /* CONFIG_NETFILTER */ 59221da177e4SLinus Torvalds 59231da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 5924828dfe1dSEric Paris static int selinux_disabled; 5925828dfe1dSEric Paris 59261da177e4SLinus Torvalds int selinux_disable(void) 59271da177e4SLinus Torvalds { 59281da177e4SLinus Torvalds if (ss_initialized) { 59291da177e4SLinus Torvalds /* Not permitted after initial policy load. */ 59301da177e4SLinus Torvalds return -EINVAL; 59311da177e4SLinus Torvalds } 59321da177e4SLinus Torvalds 59331da177e4SLinus Torvalds if (selinux_disabled) { 59341da177e4SLinus Torvalds /* Only do this once. */ 59351da177e4SLinus Torvalds return -EINVAL; 59361da177e4SLinus Torvalds } 59371da177e4SLinus Torvalds 59381da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Disabled at runtime.\n"); 59391da177e4SLinus Torvalds 59401da177e4SLinus Torvalds selinux_disabled = 1; 594130d55280SStephen Smalley selinux_enabled = 0; 59421da177e4SLinus Torvalds 5943189b3b1cSwzt.wzt@gmail.com reset_security_ops(); 59441da177e4SLinus Torvalds 5945af8ff049SEric Paris /* Try to destroy the avc node cache */ 5946af8ff049SEric Paris avc_disable(); 5947af8ff049SEric Paris 59481da177e4SLinus Torvalds /* Unregister netfilter hooks. */ 59491da177e4SLinus Torvalds selinux_nf_ip_exit(); 59501da177e4SLinus Torvalds 59511da177e4SLinus Torvalds /* Unregister selinuxfs. */ 59521da177e4SLinus Torvalds exit_sel_fs(); 59531da177e4SLinus Torvalds 59541da177e4SLinus Torvalds return 0; 59551da177e4SLinus Torvalds } 59561da177e4SLinus Torvalds #endif 5957