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. 17effad8dfSPaul Moore * Paul Moore <paul.moore@hp.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> 271da177e4SLinus Torvalds #include <linux/kernel.h> 280d094efeSRoland McGrath #include <linux/tracehook.h> 291da177e4SLinus Torvalds #include <linux/errno.h> 301da177e4SLinus Torvalds #include <linux/sched.h> 311da177e4SLinus Torvalds #include <linux/security.h> 321da177e4SLinus Torvalds #include <linux/xattr.h> 331da177e4SLinus Torvalds #include <linux/capability.h> 341da177e4SLinus Torvalds #include <linux/unistd.h> 351da177e4SLinus Torvalds #include <linux/mm.h> 361da177e4SLinus Torvalds #include <linux/mman.h> 371da177e4SLinus Torvalds #include <linux/slab.h> 381da177e4SLinus Torvalds #include <linux/pagemap.h> 391da177e4SLinus Torvalds #include <linux/swap.h> 401da177e4SLinus Torvalds #include <linux/spinlock.h> 411da177e4SLinus Torvalds #include <linux/syscalls.h> 421da177e4SLinus Torvalds #include <linux/file.h> 439f3acc31SAl Viro #include <linux/fdtable.h> 441da177e4SLinus Torvalds #include <linux/namei.h> 451da177e4SLinus Torvalds #include <linux/mount.h> 461da177e4SLinus Torvalds #include <linux/proc_fs.h> 471da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h> 481da177e4SLinus Torvalds #include <linux/netfilter_ipv6.h> 491da177e4SLinus Torvalds #include <linux/tty.h> 501da177e4SLinus Torvalds #include <net/icmp.h> 51227b60f5SStephen Hemminger #include <net/ip.h> /* for local_port_range[] */ 521da177e4SLinus Torvalds #include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */ 53220deb96SPaul Moore #include <net/net_namespace.h> 54d621d35eSPaul Moore #include <net/netlabel.h> 55f5269710SEric Paris #include <linux/uaccess.h> 561da177e4SLinus Torvalds #include <asm/ioctls.h> 57d621d35eSPaul Moore #include <asm/atomic.h> 581da177e4SLinus Torvalds #include <linux/bitops.h> 591da177e4SLinus Torvalds #include <linux/interrupt.h> 601da177e4SLinus Torvalds #include <linux/netdevice.h> /* for network interface checks */ 611da177e4SLinus Torvalds #include <linux/netlink.h> 621da177e4SLinus Torvalds #include <linux/tcp.h> 631da177e4SLinus Torvalds #include <linux/udp.h> 642ee92d46SJames Morris #include <linux/dccp.h> 651da177e4SLinus Torvalds #include <linux/quota.h> 661da177e4SLinus Torvalds #include <linux/un.h> /* for Unix socket types */ 671da177e4SLinus Torvalds #include <net/af_unix.h> /* for Unix socket types */ 681da177e4SLinus Torvalds #include <linux/parser.h> 691da177e4SLinus Torvalds #include <linux/nfs_mount.h> 701da177e4SLinus Torvalds #include <net/ipv6.h> 711da177e4SLinus Torvalds #include <linux/hugetlb.h> 721da177e4SLinus Torvalds #include <linux/personality.h> 731da177e4SLinus Torvalds #include <linux/sysctl.h> 741da177e4SLinus Torvalds #include <linux/audit.h> 756931dfc9SEric Paris #include <linux/string.h> 76877ce7c1SCatherine Zhang #include <linux/selinux.h> 7723970741SEric Paris #include <linux/mutex.h> 78f06febc9SFrank Mayhar #include <linux/posix-timers.h> 7900234592SKees Cook #include <linux/syslog.h> 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds #include "avc.h" 821da177e4SLinus Torvalds #include "objsec.h" 831da177e4SLinus Torvalds #include "netif.h" 84224dfbd8SPaul Moore #include "netnode.h" 853e112172SPaul Moore #include "netport.h" 86d28d1e08STrent Jaeger #include "xfrm.h" 87c60475bfSPaul Moore #include "netlabel.h" 889d57a7f9SAhmed S. Darwish #include "audit.h" 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds #define XATTR_SELINUX_SUFFIX "selinux" 911da177e4SLinus Torvalds #define XATTR_NAME_SELINUX XATTR_SECURITY_PREFIX XATTR_SELINUX_SUFFIX 921da177e4SLinus Torvalds 9311689d47SDavid P. Quigley #define NUM_SEL_MNT_OPTS 5 94c9180a57SEric Paris 951da177e4SLinus Torvalds extern int selinux_nlmsg_lookup(u16 sclass, u16 nlmsg_type, u32 *perm); 9620510f2fSJames Morris extern struct security_operations *security_ops; 971da177e4SLinus Torvalds 98d621d35eSPaul Moore /* SECMARK reference count */ 99d621d35eSPaul Moore atomic_t selinux_secmark_refcount = ATOMIC_INIT(0); 100d621d35eSPaul Moore 1011da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP 102828dfe1dSEric Paris int selinux_enforcing; 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds static int __init enforcing_setup(char *str) 1051da177e4SLinus Torvalds { 106f5269710SEric Paris unsigned long enforcing; 107f5269710SEric Paris if (!strict_strtoul(str, 0, &enforcing)) 108f5269710SEric Paris selinux_enforcing = enforcing ? 1 : 0; 1091da177e4SLinus Torvalds return 1; 1101da177e4SLinus Torvalds } 1111da177e4SLinus Torvalds __setup("enforcing=", enforcing_setup); 1121da177e4SLinus Torvalds #endif 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM 1151da177e4SLinus Torvalds int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE; 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds static int __init selinux_enabled_setup(char *str) 1181da177e4SLinus Torvalds { 119f5269710SEric Paris unsigned long enabled; 120f5269710SEric Paris if (!strict_strtoul(str, 0, &enabled)) 121f5269710SEric Paris selinux_enabled = enabled ? 1 : 0; 1221da177e4SLinus Torvalds return 1; 1231da177e4SLinus Torvalds } 1241da177e4SLinus Torvalds __setup("selinux=", selinux_enabled_setup); 12530d55280SStephen Smalley #else 12630d55280SStephen Smalley int selinux_enabled = 1; 1271da177e4SLinus Torvalds #endif 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds /* Lists of inode and superblock security structures initialized 1301da177e4SLinus Torvalds before the policy was loaded. */ 1311da177e4SLinus Torvalds static LIST_HEAD(superblock_security_head); 1321da177e4SLinus Torvalds static DEFINE_SPINLOCK(sb_security_lock); 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 143d621d35eSPaul Moore * enabled, false (0) if SECMARK is disabled. 144d621d35eSPaul Moore * 145d621d35eSPaul Moore */ 146d621d35eSPaul Moore static int selinux_secmark_enabled(void) 147d621d35eSPaul Moore { 148d621d35eSPaul Moore return (atomic_read(&selinux_secmark_refcount) > 0); 149d621d35eSPaul Moore } 150d621d35eSPaul Moore 151d84f4f99SDavid Howells /* 152d84f4f99SDavid Howells * initialise the security for the init task 153d84f4f99SDavid Howells */ 154d84f4f99SDavid Howells static void cred_init_security(void) 1551da177e4SLinus Torvalds { 1563b11a1deSDavid Howells struct cred *cred = (struct cred *) current->real_cred; 1571da177e4SLinus Torvalds struct task_security_struct *tsec; 1581da177e4SLinus Torvalds 15989d155efSJames Morris tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL); 1601da177e4SLinus Torvalds if (!tsec) 161d84f4f99SDavid Howells panic("SELinux: Failed to initialize initial task.\n"); 1621da177e4SLinus Torvalds 163d84f4f99SDavid Howells tsec->osid = tsec->sid = SECINITSID_KERNEL; 164f1752eecSDavid Howells cred->security = tsec; 1651da177e4SLinus Torvalds } 1661da177e4SLinus Torvalds 167275bb41eSDavid Howells /* 16888e67f3bSDavid Howells * get the security ID of a set of credentials 16988e67f3bSDavid Howells */ 17088e67f3bSDavid Howells static inline u32 cred_sid(const struct cred *cred) 17188e67f3bSDavid Howells { 17288e67f3bSDavid Howells const struct task_security_struct *tsec; 17388e67f3bSDavid Howells 17488e67f3bSDavid Howells tsec = cred->security; 17588e67f3bSDavid Howells return tsec->sid; 17688e67f3bSDavid Howells } 17788e67f3bSDavid Howells 17888e67f3bSDavid Howells /* 1793b11a1deSDavid Howells * get the objective security ID of a task 180275bb41eSDavid Howells */ 181275bb41eSDavid Howells static inline u32 task_sid(const struct task_struct *task) 182275bb41eSDavid Howells { 183275bb41eSDavid Howells u32 sid; 184275bb41eSDavid Howells 185275bb41eSDavid Howells rcu_read_lock(); 18688e67f3bSDavid Howells sid = cred_sid(__task_cred(task)); 187275bb41eSDavid Howells rcu_read_unlock(); 188275bb41eSDavid Howells return sid; 189275bb41eSDavid Howells } 190275bb41eSDavid Howells 191275bb41eSDavid Howells /* 1923b11a1deSDavid Howells * get the subjective security ID of the current task 193275bb41eSDavid Howells */ 194275bb41eSDavid Howells static inline u32 current_sid(void) 195275bb41eSDavid Howells { 196275bb41eSDavid Howells const struct task_security_struct *tsec = current_cred()->security; 197275bb41eSDavid Howells 198275bb41eSDavid Howells return tsec->sid; 199275bb41eSDavid Howells } 200275bb41eSDavid Howells 20188e67f3bSDavid Howells /* Allocate and free functions for each kind of security blob. */ 20288e67f3bSDavid Howells 2031da177e4SLinus Torvalds static int inode_alloc_security(struct inode *inode) 2041da177e4SLinus Torvalds { 2051da177e4SLinus Torvalds struct inode_security_struct *isec; 206275bb41eSDavid Howells u32 sid = current_sid(); 2071da177e4SLinus Torvalds 208a02fe132SJosef Bacik isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS); 2091da177e4SLinus Torvalds if (!isec) 2101da177e4SLinus Torvalds return -ENOMEM; 2111da177e4SLinus Torvalds 21223970741SEric Paris mutex_init(&isec->lock); 2131da177e4SLinus Torvalds INIT_LIST_HEAD(&isec->list); 2141da177e4SLinus Torvalds isec->inode = inode; 2151da177e4SLinus Torvalds isec->sid = SECINITSID_UNLABELED; 2161da177e4SLinus Torvalds isec->sclass = SECCLASS_FILE; 217275bb41eSDavid Howells isec->task_sid = sid; 2181da177e4SLinus Torvalds inode->i_security = isec; 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds return 0; 2211da177e4SLinus Torvalds } 2221da177e4SLinus Torvalds 2231da177e4SLinus Torvalds static void inode_free_security(struct inode *inode) 2241da177e4SLinus Torvalds { 2251da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 2261da177e4SLinus Torvalds struct superblock_security_struct *sbsec = inode->i_sb->s_security; 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 2291da177e4SLinus Torvalds if (!list_empty(&isec->list)) 2301da177e4SLinus Torvalds list_del_init(&isec->list); 2311da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 2321da177e4SLinus Torvalds 2331da177e4SLinus Torvalds inode->i_security = NULL; 2347cae7e26SJames Morris kmem_cache_free(sel_inode_cache, isec); 2351da177e4SLinus Torvalds } 2361da177e4SLinus Torvalds 2371da177e4SLinus Torvalds static int file_alloc_security(struct file *file) 2381da177e4SLinus Torvalds { 2391da177e4SLinus Torvalds struct file_security_struct *fsec; 240275bb41eSDavid Howells u32 sid = current_sid(); 2411da177e4SLinus Torvalds 24226d2a4beSStephen Smalley fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL); 2431da177e4SLinus Torvalds if (!fsec) 2441da177e4SLinus Torvalds return -ENOMEM; 2451da177e4SLinus Torvalds 246275bb41eSDavid Howells fsec->sid = sid; 247275bb41eSDavid Howells fsec->fown_sid = sid; 2481da177e4SLinus Torvalds file->f_security = fsec; 2491da177e4SLinus Torvalds 2501da177e4SLinus Torvalds return 0; 2511da177e4SLinus Torvalds } 2521da177e4SLinus Torvalds 2531da177e4SLinus Torvalds static void file_free_security(struct file *file) 2541da177e4SLinus Torvalds { 2551da177e4SLinus Torvalds struct file_security_struct *fsec = file->f_security; 2561da177e4SLinus Torvalds file->f_security = NULL; 2571da177e4SLinus Torvalds kfree(fsec); 2581da177e4SLinus Torvalds } 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds static int superblock_alloc_security(struct super_block *sb) 2611da177e4SLinus Torvalds { 2621da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 2631da177e4SLinus Torvalds 26489d155efSJames Morris sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL); 2651da177e4SLinus Torvalds if (!sbsec) 2661da177e4SLinus Torvalds return -ENOMEM; 2671da177e4SLinus Torvalds 268bc7e982bSEric Paris mutex_init(&sbsec->lock); 2691da177e4SLinus Torvalds INIT_LIST_HEAD(&sbsec->list); 2701da177e4SLinus Torvalds INIT_LIST_HEAD(&sbsec->isec_head); 2711da177e4SLinus Torvalds spin_lock_init(&sbsec->isec_lock); 2721da177e4SLinus Torvalds sbsec->sb = sb; 2731da177e4SLinus Torvalds sbsec->sid = SECINITSID_UNLABELED; 2741da177e4SLinus Torvalds sbsec->def_sid = SECINITSID_FILE; 275c312feb2SEric Paris sbsec->mntpoint_sid = SECINITSID_UNLABELED; 2761da177e4SLinus Torvalds sb->s_security = sbsec; 2771da177e4SLinus Torvalds 2781da177e4SLinus Torvalds return 0; 2791da177e4SLinus Torvalds } 2801da177e4SLinus Torvalds 2811da177e4SLinus Torvalds static void superblock_free_security(struct super_block *sb) 2821da177e4SLinus Torvalds { 2831da177e4SLinus Torvalds struct superblock_security_struct *sbsec = sb->s_security; 2841da177e4SLinus Torvalds 2851da177e4SLinus Torvalds spin_lock(&sb_security_lock); 2861da177e4SLinus Torvalds if (!list_empty(&sbsec->list)) 2871da177e4SLinus Torvalds list_del_init(&sbsec->list); 2881da177e4SLinus Torvalds spin_unlock(&sb_security_lock); 2891da177e4SLinus Torvalds 2901da177e4SLinus Torvalds sb->s_security = NULL; 2911da177e4SLinus Torvalds kfree(sbsec); 2921da177e4SLinus Torvalds } 2931da177e4SLinus Torvalds 2947d877f3bSAl Viro static int sk_alloc_security(struct sock *sk, int family, gfp_t priority) 2951da177e4SLinus Torvalds { 2961da177e4SLinus Torvalds struct sk_security_struct *ssec; 2971da177e4SLinus Torvalds 29889d155efSJames Morris ssec = kzalloc(sizeof(*ssec), priority); 2991da177e4SLinus Torvalds if (!ssec) 3001da177e4SLinus Torvalds return -ENOMEM; 3011da177e4SLinus Torvalds 3021da177e4SLinus Torvalds ssec->peer_sid = SECINITSID_UNLABELED; 303892c141eSVenkat Yekkirala ssec->sid = SECINITSID_UNLABELED; 3041da177e4SLinus Torvalds sk->sk_security = ssec; 3051da177e4SLinus Torvalds 306389fb800SPaul Moore selinux_netlbl_sk_security_reset(ssec); 30799f59ed0SPaul Moore 3081da177e4SLinus Torvalds return 0; 3091da177e4SLinus Torvalds } 3101da177e4SLinus Torvalds 3111da177e4SLinus Torvalds static void sk_free_security(struct sock *sk) 3121da177e4SLinus Torvalds { 3131da177e4SLinus Torvalds struct sk_security_struct *ssec = sk->sk_security; 3141da177e4SLinus Torvalds 3151da177e4SLinus Torvalds sk->sk_security = NULL; 3166c5b3fc0SPaul Moore selinux_netlbl_sk_security_free(ssec); 3171da177e4SLinus Torvalds kfree(ssec); 3181da177e4SLinus Torvalds } 3191da177e4SLinus Torvalds 3201da177e4SLinus Torvalds /* The security server must be initialized before 3211da177e4SLinus Torvalds any labeling or access decisions can be provided. */ 3221da177e4SLinus Torvalds extern int ss_initialized; 3231da177e4SLinus Torvalds 3241da177e4SLinus Torvalds /* The file system's label must be initialized prior to use. */ 3251da177e4SLinus Torvalds 3261da177e4SLinus Torvalds static char *labeling_behaviors[6] = { 3271da177e4SLinus Torvalds "uses xattr", 3281da177e4SLinus Torvalds "uses transition SIDs", 3291da177e4SLinus Torvalds "uses task SIDs", 3301da177e4SLinus Torvalds "uses genfs_contexts", 3311da177e4SLinus Torvalds "not configured for labeling", 3321da177e4SLinus Torvalds "uses mountpoint labeling", 3331da177e4SLinus Torvalds }; 3341da177e4SLinus Torvalds 3351da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry); 3361da177e4SLinus Torvalds 3371da177e4SLinus Torvalds static inline int inode_doinit(struct inode *inode) 3381da177e4SLinus Torvalds { 3391da177e4SLinus Torvalds return inode_doinit_with_dentry(inode, NULL); 3401da177e4SLinus Torvalds } 3411da177e4SLinus Torvalds 3421da177e4SLinus Torvalds enum { 34331e87930SEric Paris Opt_error = -1, 3441da177e4SLinus Torvalds Opt_context = 1, 3451da177e4SLinus Torvalds Opt_fscontext = 2, 346c9180a57SEric Paris Opt_defcontext = 3, 347c9180a57SEric Paris Opt_rootcontext = 4, 34811689d47SDavid P. Quigley Opt_labelsupport = 5, 3491da177e4SLinus Torvalds }; 3501da177e4SLinus Torvalds 351a447c093SSteven Whitehouse static const match_table_t tokens = { 352832cbd9aSEric Paris {Opt_context, CONTEXT_STR "%s"}, 353832cbd9aSEric Paris {Opt_fscontext, FSCONTEXT_STR "%s"}, 354832cbd9aSEric Paris {Opt_defcontext, DEFCONTEXT_STR "%s"}, 355832cbd9aSEric Paris {Opt_rootcontext, ROOTCONTEXT_STR "%s"}, 35611689d47SDavid P. Quigley {Opt_labelsupport, LABELSUPP_STR}, 35731e87930SEric Paris {Opt_error, NULL}, 3581da177e4SLinus Torvalds }; 3591da177e4SLinus Torvalds 3601da177e4SLinus Torvalds #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n" 3611da177e4SLinus Torvalds 362c312feb2SEric Paris static int may_context_mount_sb_relabel(u32 sid, 363c312feb2SEric Paris struct superblock_security_struct *sbsec, 364275bb41eSDavid Howells const struct cred *cred) 365c312feb2SEric Paris { 366275bb41eSDavid Howells const struct task_security_struct *tsec = cred->security; 367c312feb2SEric Paris int rc; 368c312feb2SEric Paris 369c312feb2SEric Paris rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 370c312feb2SEric Paris FILESYSTEM__RELABELFROM, NULL); 371c312feb2SEric Paris if (rc) 372c312feb2SEric Paris return rc; 373c312feb2SEric Paris 374c312feb2SEric Paris rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM, 375c312feb2SEric Paris FILESYSTEM__RELABELTO, NULL); 376c312feb2SEric Paris return rc; 377c312feb2SEric Paris } 378c312feb2SEric Paris 3790808925eSEric Paris static int may_context_mount_inode_relabel(u32 sid, 3800808925eSEric Paris struct superblock_security_struct *sbsec, 381275bb41eSDavid Howells const struct cred *cred) 3820808925eSEric Paris { 383275bb41eSDavid Howells const struct task_security_struct *tsec = cred->security; 3840808925eSEric Paris int rc; 3850808925eSEric Paris rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 3860808925eSEric Paris FILESYSTEM__RELABELFROM, NULL); 3870808925eSEric Paris if (rc) 3880808925eSEric Paris return rc; 3890808925eSEric Paris 3900808925eSEric Paris rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, 3910808925eSEric Paris FILESYSTEM__ASSOCIATE, NULL); 3920808925eSEric Paris return rc; 3930808925eSEric Paris } 3940808925eSEric Paris 395c9180a57SEric Paris static int sb_finish_set_opts(struct super_block *sb) 3961da177e4SLinus Torvalds { 3971da177e4SLinus Torvalds struct superblock_security_struct *sbsec = sb->s_security; 3981da177e4SLinus Torvalds struct dentry *root = sb->s_root; 399c9180a57SEric Paris struct inode *root_inode = root->d_inode; 4001da177e4SLinus Torvalds int rc = 0; 4011da177e4SLinus Torvalds 4021da177e4SLinus Torvalds if (sbsec->behavior == SECURITY_FS_USE_XATTR) { 4031da177e4SLinus Torvalds /* Make sure that the xattr handler exists and that no 4041da177e4SLinus Torvalds error other than -ENODATA is returned by getxattr on 4051da177e4SLinus Torvalds the root directory. -ENODATA is ok, as this may be 4061da177e4SLinus Torvalds the first boot of the SELinux kernel before we have 4071da177e4SLinus Torvalds assigned xattr values to the filesystem. */ 408c9180a57SEric Paris if (!root_inode->i_op->getxattr) { 4091da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: (dev %s, type %s) has no " 4101da177e4SLinus Torvalds "xattr support\n", sb->s_id, sb->s_type->name); 4111da177e4SLinus Torvalds rc = -EOPNOTSUPP; 4121da177e4SLinus Torvalds goto out; 4131da177e4SLinus Torvalds } 414c9180a57SEric Paris rc = root_inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0); 4151da177e4SLinus Torvalds if (rc < 0 && rc != -ENODATA) { 4161da177e4SLinus Torvalds if (rc == -EOPNOTSUPP) 4171da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: (dev %s, type " 4181da177e4SLinus Torvalds "%s) has no security xattr handler\n", 4191da177e4SLinus Torvalds sb->s_id, sb->s_type->name); 4201da177e4SLinus Torvalds else 4211da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: (dev %s, type " 4221da177e4SLinus Torvalds "%s) getxattr errno %d\n", sb->s_id, 4231da177e4SLinus Torvalds sb->s_type->name, -rc); 4241da177e4SLinus Torvalds goto out; 4251da177e4SLinus Torvalds } 4261da177e4SLinus Torvalds } 4271da177e4SLinus Torvalds 42811689d47SDavid P. Quigley sbsec->flags |= (SE_SBINITIALIZED | SE_SBLABELSUPP); 4291da177e4SLinus Torvalds 430c9180a57SEric Paris if (sbsec->behavior > ARRAY_SIZE(labeling_behaviors)) 431fadcdb45SEric Paris printk(KERN_ERR "SELinux: initialized (dev %s, type %s), unknown behavior\n", 4321da177e4SLinus Torvalds sb->s_id, sb->s_type->name); 433c9180a57SEric Paris else 434fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: initialized (dev %s, type %s), %s\n", 4351da177e4SLinus Torvalds sb->s_id, sb->s_type->name, 4361da177e4SLinus Torvalds labeling_behaviors[sbsec->behavior-1]); 4371da177e4SLinus Torvalds 43811689d47SDavid P. Quigley if (sbsec->behavior == SECURITY_FS_USE_GENFS || 43911689d47SDavid P. Quigley sbsec->behavior == SECURITY_FS_USE_MNTPOINT || 44011689d47SDavid P. Quigley sbsec->behavior == SECURITY_FS_USE_NONE || 44111689d47SDavid P. Quigley sbsec->behavior > ARRAY_SIZE(labeling_behaviors)) 44211689d47SDavid P. Quigley sbsec->flags &= ~SE_SBLABELSUPP; 44311689d47SDavid P. Quigley 444ddd29ec6SDavid P. Quigley /* Special handling for sysfs. Is genfs but also has setxattr handler*/ 445ddd29ec6SDavid P. Quigley if (strncmp(sb->s_type->name, "sysfs", sizeof("sysfs")) == 0) 446ddd29ec6SDavid P. Quigley sbsec->flags |= SE_SBLABELSUPP; 447ddd29ec6SDavid P. Quigley 4481da177e4SLinus Torvalds /* Initialize the root inode. */ 449c9180a57SEric Paris rc = inode_doinit_with_dentry(root_inode, root); 4501da177e4SLinus Torvalds 4511da177e4SLinus Torvalds /* Initialize any other inodes associated with the superblock, e.g. 4521da177e4SLinus Torvalds inodes created prior to initial policy load or inodes created 4531da177e4SLinus Torvalds during get_sb by a pseudo filesystem that directly 4541da177e4SLinus Torvalds populates itself. */ 4551da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 4561da177e4SLinus Torvalds next_inode: 4571da177e4SLinus Torvalds if (!list_empty(&sbsec->isec_head)) { 4581da177e4SLinus Torvalds struct inode_security_struct *isec = 4591da177e4SLinus Torvalds list_entry(sbsec->isec_head.next, 4601da177e4SLinus Torvalds struct inode_security_struct, list); 4611da177e4SLinus Torvalds struct inode *inode = isec->inode; 4621da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 4631da177e4SLinus Torvalds inode = igrab(inode); 4641da177e4SLinus Torvalds if (inode) { 4651da177e4SLinus Torvalds if (!IS_PRIVATE(inode)) 4661da177e4SLinus Torvalds inode_doinit(inode); 4671da177e4SLinus Torvalds iput(inode); 4681da177e4SLinus Torvalds } 4691da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 4701da177e4SLinus Torvalds list_del_init(&isec->list); 4711da177e4SLinus Torvalds goto next_inode; 4721da177e4SLinus Torvalds } 4731da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 4741da177e4SLinus Torvalds out: 475c9180a57SEric Paris return rc; 476c9180a57SEric Paris } 477c9180a57SEric Paris 478c9180a57SEric Paris /* 479c9180a57SEric Paris * This function should allow an FS to ask what it's mount security 480c9180a57SEric Paris * options were so it can use those later for submounts, displaying 481c9180a57SEric Paris * mount options, or whatever. 482c9180a57SEric Paris */ 483c9180a57SEric Paris static int selinux_get_mnt_opts(const struct super_block *sb, 484e0007529SEric Paris struct security_mnt_opts *opts) 485c9180a57SEric Paris { 486c9180a57SEric Paris int rc = 0, i; 487c9180a57SEric Paris struct superblock_security_struct *sbsec = sb->s_security; 488c9180a57SEric Paris char *context = NULL; 489c9180a57SEric Paris u32 len; 490c9180a57SEric Paris char tmp; 491c9180a57SEric Paris 492e0007529SEric Paris security_init_mnt_opts(opts); 493c9180a57SEric Paris 4940d90a7ecSDavid P. Quigley if (!(sbsec->flags & SE_SBINITIALIZED)) 495c9180a57SEric Paris return -EINVAL; 496c9180a57SEric Paris 497c9180a57SEric Paris if (!ss_initialized) 498c9180a57SEric Paris return -EINVAL; 499c9180a57SEric Paris 5000d90a7ecSDavid P. Quigley tmp = sbsec->flags & SE_MNTMASK; 501c9180a57SEric Paris /* count the number of mount options for this sb */ 502c9180a57SEric Paris for (i = 0; i < 8; i++) { 503c9180a57SEric Paris if (tmp & 0x01) 504e0007529SEric Paris opts->num_mnt_opts++; 505c9180a57SEric Paris tmp >>= 1; 506c9180a57SEric Paris } 50711689d47SDavid P. Quigley /* Check if the Label support flag is set */ 50811689d47SDavid P. Quigley if (sbsec->flags & SE_SBLABELSUPP) 50911689d47SDavid P. Quigley 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 } 55511689d47SDavid P. Quigley if (sbsec->flags & SE_SBLABELSUPP) { 55611689d47SDavid P. Quigley opts->mnt_opts[i] = NULL; 55711689d47SDavid P. Quigley opts->mnt_opts_flags[i++] = SE_SBLABELSUPP; 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; 599c9180a57SEric Paris const char *name = sb->s_type->name; 600089be43eSJames Morris struct inode *inode = sbsec->sb->s_root->d_inode; 601089be43eSJames Morris struct inode_security_struct *root_isec = inode->i_security; 602c9180a57SEric Paris u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0; 603c9180a57SEric Paris u32 defcontext_sid = 0; 604e0007529SEric Paris char **mount_options = opts->mnt_opts; 605e0007529SEric Paris int *flags = opts->mnt_opts_flags; 606e0007529SEric Paris int num_opts = opts->num_mnt_opts; 607c9180a57SEric Paris 608c9180a57SEric Paris mutex_lock(&sbsec->lock); 609c9180a57SEric Paris 610c9180a57SEric Paris if (!ss_initialized) { 611c9180a57SEric Paris if (!num_opts) { 612c9180a57SEric Paris /* Defer initialization until selinux_complete_init, 613c9180a57SEric Paris after the initial policy is loaded and the security 614c9180a57SEric Paris server is ready to handle calls. */ 615c9180a57SEric Paris spin_lock(&sb_security_lock); 616c9180a57SEric Paris if (list_empty(&sbsec->list)) 617c9180a57SEric Paris list_add(&sbsec->list, &superblock_security_head); 618c9180a57SEric Paris spin_unlock(&sb_security_lock); 619c9180a57SEric Paris goto out; 620c9180a57SEric Paris } 621c9180a57SEric Paris rc = -EINVAL; 622744ba35eSEric Paris printk(KERN_WARNING "SELinux: Unable to set superblock options " 623744ba35eSEric Paris "before the security server is initialized\n"); 624c9180a57SEric Paris goto out; 625c9180a57SEric Paris } 626c9180a57SEric Paris 627c9180a57SEric Paris /* 628e0007529SEric Paris * Binary mount data FS will come through this function twice. Once 629e0007529SEric Paris * from an explicit call and once from the generic calls from the vfs. 630e0007529SEric Paris * Since the generic VFS calls will not contain any security mount data 631e0007529SEric Paris * we need to skip the double mount verification. 632e0007529SEric Paris * 633e0007529SEric Paris * This does open a hole in which we will not notice if the first 634e0007529SEric Paris * mount using this sb set explict options and a second mount using 635e0007529SEric Paris * this sb does not set any security options. (The first options 636e0007529SEric Paris * will be used for both mounts) 637e0007529SEric Paris */ 6380d90a7ecSDavid P. Quigley if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA) 639e0007529SEric Paris && (num_opts == 0)) 640e0007529SEric Paris goto out; 641e0007529SEric Paris 642e0007529SEric Paris /* 643c9180a57SEric Paris * parse the mount options, check if they are valid sids. 644c9180a57SEric Paris * also check if someone is trying to mount the same sb more 645c9180a57SEric Paris * than once with different security options. 646c9180a57SEric Paris */ 647c9180a57SEric Paris for (i = 0; i < num_opts; i++) { 648c9180a57SEric Paris u32 sid; 64911689d47SDavid P. Quigley 65011689d47SDavid P. Quigley if (flags[i] == SE_SBLABELSUPP) 65111689d47SDavid P. Quigley continue; 652c9180a57SEric Paris rc = security_context_to_sid(mount_options[i], 653c9180a57SEric Paris strlen(mount_options[i]), &sid); 654c9180a57SEric Paris if (rc) { 655c9180a57SEric Paris printk(KERN_WARNING "SELinux: security_context_to_sid" 656c9180a57SEric Paris "(%s) failed for (dev %s, type %s) errno=%d\n", 657c9180a57SEric Paris mount_options[i], sb->s_id, name, rc); 658c9180a57SEric Paris goto out; 659c9180a57SEric Paris } 660c9180a57SEric Paris switch (flags[i]) { 661c9180a57SEric Paris case FSCONTEXT_MNT: 662c9180a57SEric Paris fscontext_sid = sid; 663c9180a57SEric Paris 664c9180a57SEric Paris if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, 665c9180a57SEric Paris fscontext_sid)) 666c9180a57SEric Paris goto out_double_mount; 667c9180a57SEric Paris 668c9180a57SEric Paris sbsec->flags |= FSCONTEXT_MNT; 669c9180a57SEric Paris break; 670c9180a57SEric Paris case CONTEXT_MNT: 671c9180a57SEric Paris context_sid = sid; 672c9180a57SEric Paris 673c9180a57SEric Paris if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, 674c9180a57SEric Paris context_sid)) 675c9180a57SEric Paris goto out_double_mount; 676c9180a57SEric Paris 677c9180a57SEric Paris sbsec->flags |= CONTEXT_MNT; 678c9180a57SEric Paris break; 679c9180a57SEric Paris case ROOTCONTEXT_MNT: 680c9180a57SEric Paris rootcontext_sid = sid; 681c9180a57SEric Paris 682c9180a57SEric Paris if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, 683c9180a57SEric Paris rootcontext_sid)) 684c9180a57SEric Paris goto out_double_mount; 685c9180a57SEric Paris 686c9180a57SEric Paris sbsec->flags |= ROOTCONTEXT_MNT; 687c9180a57SEric Paris 688c9180a57SEric Paris break; 689c9180a57SEric Paris case DEFCONTEXT_MNT: 690c9180a57SEric Paris defcontext_sid = sid; 691c9180a57SEric Paris 692c9180a57SEric Paris if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, 693c9180a57SEric Paris defcontext_sid)) 694c9180a57SEric Paris goto out_double_mount; 695c9180a57SEric Paris 696c9180a57SEric Paris sbsec->flags |= DEFCONTEXT_MNT; 697c9180a57SEric Paris 698c9180a57SEric Paris break; 699c9180a57SEric Paris default: 700c9180a57SEric Paris rc = -EINVAL; 701c9180a57SEric Paris goto out; 702c9180a57SEric Paris } 703c9180a57SEric Paris } 704c9180a57SEric Paris 7050d90a7ecSDavid P. Quigley if (sbsec->flags & SE_SBINITIALIZED) { 706c9180a57SEric Paris /* previously mounted with options, but not on this attempt? */ 7070d90a7ecSDavid P. Quigley if ((sbsec->flags & SE_MNTMASK) && !num_opts) 708c9180a57SEric Paris goto out_double_mount; 709c9180a57SEric Paris rc = 0; 710c9180a57SEric Paris goto out; 711c9180a57SEric Paris } 712c9180a57SEric Paris 713089be43eSJames Morris if (strcmp(sb->s_type->name, "proc") == 0) 7140d90a7ecSDavid P. Quigley sbsec->flags |= SE_SBPROC; 715c9180a57SEric Paris 716c9180a57SEric Paris /* Determine the labeling behavior to use for this filesystem type. */ 7170d90a7ecSDavid P. Quigley rc = security_fs_use((sbsec->flags & SE_SBPROC) ? "proc" : sb->s_type->name, &sbsec->behavior, &sbsec->sid); 718c9180a57SEric Paris if (rc) { 719c9180a57SEric Paris printk(KERN_WARNING "%s: security_fs_use(%s) returned %d\n", 720089be43eSJames Morris __func__, sb->s_type->name, rc); 721c9180a57SEric Paris goto out; 722c9180a57SEric Paris } 723c9180a57SEric Paris 724c9180a57SEric Paris /* sets the context of the superblock for the fs being mounted. */ 725c9180a57SEric Paris if (fscontext_sid) { 726275bb41eSDavid Howells rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred); 727c9180a57SEric Paris if (rc) 728c9180a57SEric Paris goto out; 729c9180a57SEric Paris 730c9180a57SEric Paris sbsec->sid = fscontext_sid; 731c9180a57SEric Paris } 732c9180a57SEric Paris 733c9180a57SEric Paris /* 734c9180a57SEric Paris * Switch to using mount point labeling behavior. 735c9180a57SEric Paris * sets the label used on all file below the mountpoint, and will set 736c9180a57SEric Paris * the superblock context if not already set. 737c9180a57SEric Paris */ 738c9180a57SEric Paris if (context_sid) { 739c9180a57SEric Paris if (!fscontext_sid) { 740275bb41eSDavid Howells rc = may_context_mount_sb_relabel(context_sid, sbsec, 741275bb41eSDavid Howells cred); 742c9180a57SEric Paris if (rc) 743c9180a57SEric Paris goto out; 744c9180a57SEric Paris sbsec->sid = context_sid; 745c9180a57SEric Paris } else { 746275bb41eSDavid Howells rc = may_context_mount_inode_relabel(context_sid, sbsec, 747275bb41eSDavid Howells cred); 748c9180a57SEric Paris if (rc) 749c9180a57SEric Paris goto out; 750c9180a57SEric Paris } 751c9180a57SEric Paris if (!rootcontext_sid) 752c9180a57SEric Paris rootcontext_sid = context_sid; 753c9180a57SEric Paris 754c9180a57SEric Paris sbsec->mntpoint_sid = context_sid; 755c9180a57SEric Paris sbsec->behavior = SECURITY_FS_USE_MNTPOINT; 756c9180a57SEric Paris } 757c9180a57SEric Paris 758c9180a57SEric Paris if (rootcontext_sid) { 759275bb41eSDavid Howells rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec, 760275bb41eSDavid Howells cred); 761c9180a57SEric Paris if (rc) 762c9180a57SEric Paris goto out; 763c9180a57SEric Paris 764c9180a57SEric Paris root_isec->sid = rootcontext_sid; 765c9180a57SEric Paris root_isec->initialized = 1; 766c9180a57SEric Paris } 767c9180a57SEric Paris 768c9180a57SEric Paris if (defcontext_sid) { 769c9180a57SEric Paris if (sbsec->behavior != SECURITY_FS_USE_XATTR) { 770c9180a57SEric Paris rc = -EINVAL; 771c9180a57SEric Paris printk(KERN_WARNING "SELinux: defcontext option is " 772c9180a57SEric Paris "invalid for this filesystem type\n"); 773c9180a57SEric Paris goto out; 774c9180a57SEric Paris } 775c9180a57SEric Paris 776c9180a57SEric Paris if (defcontext_sid != sbsec->def_sid) { 777c9180a57SEric Paris rc = may_context_mount_inode_relabel(defcontext_sid, 778275bb41eSDavid Howells sbsec, cred); 779c9180a57SEric Paris if (rc) 780c9180a57SEric Paris goto out; 781c9180a57SEric Paris } 782c9180a57SEric Paris 783c9180a57SEric Paris sbsec->def_sid = defcontext_sid; 784c9180a57SEric Paris } 785c9180a57SEric Paris 786c9180a57SEric Paris rc = sb_finish_set_opts(sb); 787c9180a57SEric Paris out: 788bc7e982bSEric Paris mutex_unlock(&sbsec->lock); 7891da177e4SLinus Torvalds return rc; 790c9180a57SEric Paris out_double_mount: 791c9180a57SEric Paris rc = -EINVAL; 792c9180a57SEric Paris printk(KERN_WARNING "SELinux: mount invalid. Same superblock, different " 793c9180a57SEric Paris "security settings for (dev %s, type %s)\n", sb->s_id, name); 794c9180a57SEric Paris goto out; 795c9180a57SEric Paris } 796c9180a57SEric Paris 797c9180a57SEric Paris static void selinux_sb_clone_mnt_opts(const struct super_block *oldsb, 798c9180a57SEric Paris struct super_block *newsb) 799c9180a57SEric Paris { 800c9180a57SEric Paris const struct superblock_security_struct *oldsbsec = oldsb->s_security; 801c9180a57SEric Paris struct superblock_security_struct *newsbsec = newsb->s_security; 802c9180a57SEric Paris 803c9180a57SEric Paris int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT); 804c9180a57SEric Paris int set_context = (oldsbsec->flags & CONTEXT_MNT); 805c9180a57SEric Paris int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT); 806c9180a57SEric Paris 8070f5e6420SEric Paris /* 8080f5e6420SEric Paris * if the parent was able to be mounted it clearly had no special lsm 8090f5e6420SEric Paris * mount options. thus we can safely put this sb on the list and deal 8100f5e6420SEric Paris * with it later 8110f5e6420SEric Paris */ 8120f5e6420SEric Paris if (!ss_initialized) { 8130f5e6420SEric Paris spin_lock(&sb_security_lock); 8140f5e6420SEric Paris if (list_empty(&newsbsec->list)) 8150f5e6420SEric Paris list_add(&newsbsec->list, &superblock_security_head); 8160f5e6420SEric Paris spin_unlock(&sb_security_lock); 8170f5e6420SEric Paris return; 8180f5e6420SEric Paris } 819c9180a57SEric Paris 820c9180a57SEric Paris /* how can we clone if the old one wasn't set up?? */ 8210d90a7ecSDavid P. Quigley BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED)); 822c9180a57SEric Paris 8235a552617SEric Paris /* if fs is reusing a sb, just let its options stand... */ 8240d90a7ecSDavid P. Quigley if (newsbsec->flags & SE_SBINITIALIZED) 8255a552617SEric Paris return; 8265a552617SEric Paris 827c9180a57SEric Paris mutex_lock(&newsbsec->lock); 828c9180a57SEric Paris 829c9180a57SEric Paris newsbsec->flags = oldsbsec->flags; 830c9180a57SEric Paris 831c9180a57SEric Paris newsbsec->sid = oldsbsec->sid; 832c9180a57SEric Paris newsbsec->def_sid = oldsbsec->def_sid; 833c9180a57SEric Paris newsbsec->behavior = oldsbsec->behavior; 834c9180a57SEric Paris 835c9180a57SEric Paris if (set_context) { 836c9180a57SEric Paris u32 sid = oldsbsec->mntpoint_sid; 837c9180a57SEric Paris 838c9180a57SEric Paris if (!set_fscontext) 839c9180a57SEric Paris newsbsec->sid = sid; 840c9180a57SEric Paris if (!set_rootcontext) { 841c9180a57SEric Paris struct inode *newinode = newsb->s_root->d_inode; 842c9180a57SEric Paris struct inode_security_struct *newisec = newinode->i_security; 843c9180a57SEric Paris newisec->sid = sid; 844c9180a57SEric Paris } 845c9180a57SEric Paris newsbsec->mntpoint_sid = sid; 846c9180a57SEric Paris } 847c9180a57SEric Paris if (set_rootcontext) { 848c9180a57SEric Paris const struct inode *oldinode = oldsb->s_root->d_inode; 849c9180a57SEric Paris const struct inode_security_struct *oldisec = oldinode->i_security; 850c9180a57SEric Paris struct inode *newinode = newsb->s_root->d_inode; 851c9180a57SEric Paris struct inode_security_struct *newisec = newinode->i_security; 852c9180a57SEric Paris 853c9180a57SEric Paris newisec->sid = oldisec->sid; 854c9180a57SEric Paris } 855c9180a57SEric Paris 856c9180a57SEric Paris sb_finish_set_opts(newsb); 857c9180a57SEric Paris mutex_unlock(&newsbsec->lock); 858c9180a57SEric Paris } 859c9180a57SEric Paris 8602e1479d9SAdrian Bunk static int selinux_parse_opts_str(char *options, 8612e1479d9SAdrian Bunk struct security_mnt_opts *opts) 862c9180a57SEric Paris { 863e0007529SEric Paris char *p; 864c9180a57SEric Paris char *context = NULL, *defcontext = NULL; 865c9180a57SEric Paris char *fscontext = NULL, *rootcontext = NULL; 866e0007529SEric Paris int rc, num_mnt_opts = 0; 867c9180a57SEric Paris 868e0007529SEric Paris opts->num_mnt_opts = 0; 869c9180a57SEric Paris 870c9180a57SEric Paris /* Standard string-based options. */ 871c9180a57SEric Paris while ((p = strsep(&options, "|")) != NULL) { 872c9180a57SEric Paris int token; 873c9180a57SEric Paris substring_t args[MAX_OPT_ARGS]; 874c9180a57SEric Paris 875c9180a57SEric Paris if (!*p) 876c9180a57SEric Paris continue; 877c9180a57SEric Paris 878c9180a57SEric Paris token = match_token(p, tokens, args); 879c9180a57SEric Paris 880c9180a57SEric Paris switch (token) { 881c9180a57SEric Paris case Opt_context: 882c9180a57SEric Paris if (context || defcontext) { 883c9180a57SEric Paris rc = -EINVAL; 884c9180a57SEric Paris printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 885c9180a57SEric Paris goto out_err; 886c9180a57SEric Paris } 887c9180a57SEric Paris context = match_strdup(&args[0]); 888c9180a57SEric Paris if (!context) { 889c9180a57SEric Paris rc = -ENOMEM; 890c9180a57SEric Paris goto out_err; 891c9180a57SEric Paris } 892c9180a57SEric Paris break; 893c9180a57SEric Paris 894c9180a57SEric Paris case Opt_fscontext: 895c9180a57SEric Paris if (fscontext) { 896c9180a57SEric Paris rc = -EINVAL; 897c9180a57SEric Paris printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 898c9180a57SEric Paris goto out_err; 899c9180a57SEric Paris } 900c9180a57SEric Paris fscontext = match_strdup(&args[0]); 901c9180a57SEric Paris if (!fscontext) { 902c9180a57SEric Paris rc = -ENOMEM; 903c9180a57SEric Paris goto out_err; 904c9180a57SEric Paris } 905c9180a57SEric Paris break; 906c9180a57SEric Paris 907c9180a57SEric Paris case Opt_rootcontext: 908c9180a57SEric Paris if (rootcontext) { 909c9180a57SEric Paris rc = -EINVAL; 910c9180a57SEric Paris printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 911c9180a57SEric Paris goto out_err; 912c9180a57SEric Paris } 913c9180a57SEric Paris rootcontext = match_strdup(&args[0]); 914c9180a57SEric Paris if (!rootcontext) { 915c9180a57SEric Paris rc = -ENOMEM; 916c9180a57SEric Paris goto out_err; 917c9180a57SEric Paris } 918c9180a57SEric Paris break; 919c9180a57SEric Paris 920c9180a57SEric Paris case Opt_defcontext: 921c9180a57SEric Paris if (context || defcontext) { 922c9180a57SEric Paris rc = -EINVAL; 923c9180a57SEric Paris printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 924c9180a57SEric Paris goto out_err; 925c9180a57SEric Paris } 926c9180a57SEric Paris defcontext = match_strdup(&args[0]); 927c9180a57SEric Paris if (!defcontext) { 928c9180a57SEric Paris rc = -ENOMEM; 929c9180a57SEric Paris goto out_err; 930c9180a57SEric Paris } 931c9180a57SEric Paris break; 93211689d47SDavid P. Quigley case Opt_labelsupport: 93311689d47SDavid P. Quigley break; 934c9180a57SEric Paris default: 935c9180a57SEric Paris rc = -EINVAL; 936c9180a57SEric Paris printk(KERN_WARNING "SELinux: unknown mount option\n"); 937c9180a57SEric Paris goto out_err; 938c9180a57SEric Paris 939c9180a57SEric Paris } 940c9180a57SEric Paris } 941c9180a57SEric Paris 942e0007529SEric Paris rc = -ENOMEM; 943e0007529SEric Paris opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_ATOMIC); 944e0007529SEric Paris if (!opts->mnt_opts) 945e0007529SEric Paris goto out_err; 946e0007529SEric Paris 947e0007529SEric Paris opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), GFP_ATOMIC); 948e0007529SEric Paris if (!opts->mnt_opts_flags) { 949e0007529SEric Paris kfree(opts->mnt_opts); 950e0007529SEric Paris goto out_err; 951c9180a57SEric Paris } 952c9180a57SEric Paris 953e0007529SEric Paris if (fscontext) { 954e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = fscontext; 955e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT; 956e0007529SEric Paris } 957e0007529SEric Paris if (context) { 958e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = context; 959e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT; 960e0007529SEric Paris } 961e0007529SEric Paris if (rootcontext) { 962e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = rootcontext; 963e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT; 964e0007529SEric Paris } 965e0007529SEric Paris if (defcontext) { 966e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = defcontext; 967e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT; 968e0007529SEric Paris } 969e0007529SEric Paris 970e0007529SEric Paris opts->num_mnt_opts = num_mnt_opts; 971e0007529SEric Paris return 0; 972e0007529SEric Paris 973c9180a57SEric Paris out_err: 974c9180a57SEric Paris kfree(context); 975c9180a57SEric Paris kfree(defcontext); 976c9180a57SEric Paris kfree(fscontext); 977c9180a57SEric Paris kfree(rootcontext); 978c9180a57SEric Paris return rc; 9791da177e4SLinus Torvalds } 980e0007529SEric Paris /* 981e0007529SEric Paris * string mount options parsing and call set the sbsec 982e0007529SEric Paris */ 983e0007529SEric Paris static int superblock_doinit(struct super_block *sb, void *data) 984e0007529SEric Paris { 985e0007529SEric Paris int rc = 0; 986e0007529SEric Paris char *options = data; 987e0007529SEric Paris struct security_mnt_opts opts; 988e0007529SEric Paris 989e0007529SEric Paris security_init_mnt_opts(&opts); 990e0007529SEric Paris 991e0007529SEric Paris if (!data) 992e0007529SEric Paris goto out; 993e0007529SEric Paris 994e0007529SEric Paris BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA); 995e0007529SEric Paris 996e0007529SEric Paris rc = selinux_parse_opts_str(options, &opts); 997e0007529SEric Paris if (rc) 998e0007529SEric Paris goto out_err; 999e0007529SEric Paris 1000e0007529SEric Paris out: 1001e0007529SEric Paris rc = selinux_set_mnt_opts(sb, &opts); 1002e0007529SEric Paris 1003e0007529SEric Paris out_err: 1004e0007529SEric Paris security_free_mnt_opts(&opts); 1005e0007529SEric Paris return rc; 1006e0007529SEric Paris } 10071da177e4SLinus Torvalds 10083583a711SAdrian Bunk static void selinux_write_opts(struct seq_file *m, 10093583a711SAdrian Bunk struct security_mnt_opts *opts) 10102069f457SEric Paris { 10112069f457SEric Paris int i; 10122069f457SEric Paris char *prefix; 10132069f457SEric Paris 10142069f457SEric Paris for (i = 0; i < opts->num_mnt_opts; i++) { 101511689d47SDavid P. Quigley char *has_comma; 101611689d47SDavid P. Quigley 101711689d47SDavid P. Quigley if (opts->mnt_opts[i]) 101811689d47SDavid P. Quigley has_comma = strchr(opts->mnt_opts[i], ','); 101911689d47SDavid P. Quigley else 102011689d47SDavid P. Quigley has_comma = NULL; 10212069f457SEric Paris 10222069f457SEric Paris switch (opts->mnt_opts_flags[i]) { 10232069f457SEric Paris case CONTEXT_MNT: 10242069f457SEric Paris prefix = CONTEXT_STR; 10252069f457SEric Paris break; 10262069f457SEric Paris case FSCONTEXT_MNT: 10272069f457SEric Paris prefix = FSCONTEXT_STR; 10282069f457SEric Paris break; 10292069f457SEric Paris case ROOTCONTEXT_MNT: 10302069f457SEric Paris prefix = ROOTCONTEXT_STR; 10312069f457SEric Paris break; 10322069f457SEric Paris case DEFCONTEXT_MNT: 10332069f457SEric Paris prefix = DEFCONTEXT_STR; 10342069f457SEric Paris break; 103511689d47SDavid P. Quigley case SE_SBLABELSUPP: 103611689d47SDavid P. Quigley seq_putc(m, ','); 103711689d47SDavid P. Quigley seq_puts(m, LABELSUPP_STR); 103811689d47SDavid P. Quigley continue; 10392069f457SEric Paris default: 10402069f457SEric Paris BUG(); 10412069f457SEric Paris }; 10422069f457SEric Paris /* we need a comma before each option */ 10432069f457SEric Paris seq_putc(m, ','); 10442069f457SEric Paris seq_puts(m, prefix); 10452069f457SEric Paris if (has_comma) 10462069f457SEric Paris seq_putc(m, '\"'); 10472069f457SEric Paris seq_puts(m, opts->mnt_opts[i]); 10482069f457SEric Paris if (has_comma) 10492069f457SEric Paris seq_putc(m, '\"'); 10502069f457SEric Paris } 10512069f457SEric Paris } 10522069f457SEric Paris 10532069f457SEric Paris static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb) 10542069f457SEric Paris { 10552069f457SEric Paris struct security_mnt_opts opts; 10562069f457SEric Paris int rc; 10572069f457SEric Paris 10582069f457SEric Paris rc = selinux_get_mnt_opts(sb, &opts); 1059383795c2SEric Paris if (rc) { 1060383795c2SEric Paris /* before policy load we may get EINVAL, don't show anything */ 1061383795c2SEric Paris if (rc == -EINVAL) 1062383795c2SEric Paris rc = 0; 10632069f457SEric Paris return rc; 1064383795c2SEric Paris } 10652069f457SEric Paris 10662069f457SEric Paris selinux_write_opts(m, &opts); 10672069f457SEric Paris 10682069f457SEric Paris security_free_mnt_opts(&opts); 10692069f457SEric Paris 10702069f457SEric Paris return rc; 10712069f457SEric Paris } 10722069f457SEric Paris 10731da177e4SLinus Torvalds static inline u16 inode_mode_to_security_class(umode_t mode) 10741da177e4SLinus Torvalds { 10751da177e4SLinus Torvalds switch (mode & S_IFMT) { 10761da177e4SLinus Torvalds case S_IFSOCK: 10771da177e4SLinus Torvalds return SECCLASS_SOCK_FILE; 10781da177e4SLinus Torvalds case S_IFLNK: 10791da177e4SLinus Torvalds return SECCLASS_LNK_FILE; 10801da177e4SLinus Torvalds case S_IFREG: 10811da177e4SLinus Torvalds return SECCLASS_FILE; 10821da177e4SLinus Torvalds case S_IFBLK: 10831da177e4SLinus Torvalds return SECCLASS_BLK_FILE; 10841da177e4SLinus Torvalds case S_IFDIR: 10851da177e4SLinus Torvalds return SECCLASS_DIR; 10861da177e4SLinus Torvalds case S_IFCHR: 10871da177e4SLinus Torvalds return SECCLASS_CHR_FILE; 10881da177e4SLinus Torvalds case S_IFIFO: 10891da177e4SLinus Torvalds return SECCLASS_FIFO_FILE; 10901da177e4SLinus Torvalds 10911da177e4SLinus Torvalds } 10921da177e4SLinus Torvalds 10931da177e4SLinus Torvalds return SECCLASS_FILE; 10941da177e4SLinus Torvalds } 10951da177e4SLinus Torvalds 109613402580SJames Morris static inline int default_protocol_stream(int protocol) 109713402580SJames Morris { 109813402580SJames Morris return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP); 109913402580SJames Morris } 110013402580SJames Morris 110113402580SJames Morris static inline int default_protocol_dgram(int protocol) 110213402580SJames Morris { 110313402580SJames Morris return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP); 110413402580SJames Morris } 110513402580SJames Morris 11061da177e4SLinus Torvalds static inline u16 socket_type_to_security_class(int family, int type, int protocol) 11071da177e4SLinus Torvalds { 11081da177e4SLinus Torvalds switch (family) { 11091da177e4SLinus Torvalds case PF_UNIX: 11101da177e4SLinus Torvalds switch (type) { 11111da177e4SLinus Torvalds case SOCK_STREAM: 11121da177e4SLinus Torvalds case SOCK_SEQPACKET: 11131da177e4SLinus Torvalds return SECCLASS_UNIX_STREAM_SOCKET; 11141da177e4SLinus Torvalds case SOCK_DGRAM: 11151da177e4SLinus Torvalds return SECCLASS_UNIX_DGRAM_SOCKET; 11161da177e4SLinus Torvalds } 11171da177e4SLinus Torvalds break; 11181da177e4SLinus Torvalds case PF_INET: 11191da177e4SLinus Torvalds case PF_INET6: 11201da177e4SLinus Torvalds switch (type) { 11211da177e4SLinus Torvalds case SOCK_STREAM: 112213402580SJames Morris if (default_protocol_stream(protocol)) 11231da177e4SLinus Torvalds return SECCLASS_TCP_SOCKET; 112413402580SJames Morris else 112513402580SJames Morris return SECCLASS_RAWIP_SOCKET; 11261da177e4SLinus Torvalds case SOCK_DGRAM: 112713402580SJames Morris if (default_protocol_dgram(protocol)) 11281da177e4SLinus Torvalds return SECCLASS_UDP_SOCKET; 112913402580SJames Morris else 113013402580SJames Morris return SECCLASS_RAWIP_SOCKET; 11312ee92d46SJames Morris case SOCK_DCCP: 11322ee92d46SJames Morris return SECCLASS_DCCP_SOCKET; 113313402580SJames Morris default: 11341da177e4SLinus Torvalds return SECCLASS_RAWIP_SOCKET; 11351da177e4SLinus Torvalds } 11361da177e4SLinus Torvalds break; 11371da177e4SLinus Torvalds case PF_NETLINK: 11381da177e4SLinus Torvalds switch (protocol) { 11391da177e4SLinus Torvalds case NETLINK_ROUTE: 11401da177e4SLinus Torvalds return SECCLASS_NETLINK_ROUTE_SOCKET; 11411da177e4SLinus Torvalds case NETLINK_FIREWALL: 11421da177e4SLinus Torvalds return SECCLASS_NETLINK_FIREWALL_SOCKET; 1143216efaaaSJames Morris case NETLINK_INET_DIAG: 11441da177e4SLinus Torvalds return SECCLASS_NETLINK_TCPDIAG_SOCKET; 11451da177e4SLinus Torvalds case NETLINK_NFLOG: 11461da177e4SLinus Torvalds return SECCLASS_NETLINK_NFLOG_SOCKET; 11471da177e4SLinus Torvalds case NETLINK_XFRM: 11481da177e4SLinus Torvalds return SECCLASS_NETLINK_XFRM_SOCKET; 11491da177e4SLinus Torvalds case NETLINK_SELINUX: 11501da177e4SLinus Torvalds return SECCLASS_NETLINK_SELINUX_SOCKET; 11511da177e4SLinus Torvalds case NETLINK_AUDIT: 11521da177e4SLinus Torvalds return SECCLASS_NETLINK_AUDIT_SOCKET; 11531da177e4SLinus Torvalds case NETLINK_IP6_FW: 11541da177e4SLinus Torvalds return SECCLASS_NETLINK_IP6FW_SOCKET; 11551da177e4SLinus Torvalds case NETLINK_DNRTMSG: 11561da177e4SLinus Torvalds return SECCLASS_NETLINK_DNRT_SOCKET; 11570c9b7942SJames Morris case NETLINK_KOBJECT_UEVENT: 11580c9b7942SJames Morris return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET; 11591da177e4SLinus Torvalds default: 11601da177e4SLinus Torvalds return SECCLASS_NETLINK_SOCKET; 11611da177e4SLinus Torvalds } 11621da177e4SLinus Torvalds case PF_PACKET: 11631da177e4SLinus Torvalds return SECCLASS_PACKET_SOCKET; 11641da177e4SLinus Torvalds case PF_KEY: 11651da177e4SLinus Torvalds return SECCLASS_KEY_SOCKET; 11663e3ff15eSChristopher J. PeBenito case PF_APPLETALK: 11673e3ff15eSChristopher J. PeBenito return SECCLASS_APPLETALK_SOCKET; 11681da177e4SLinus Torvalds } 11691da177e4SLinus Torvalds 11701da177e4SLinus Torvalds return SECCLASS_SOCKET; 11711da177e4SLinus Torvalds } 11721da177e4SLinus Torvalds 11731da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 11741da177e4SLinus Torvalds static int selinux_proc_get_sid(struct proc_dir_entry *de, 11751da177e4SLinus Torvalds u16 tclass, 11761da177e4SLinus Torvalds u32 *sid) 11771da177e4SLinus Torvalds { 11781da177e4SLinus Torvalds int buflen, rc; 11791da177e4SLinus Torvalds char *buffer, *path, *end; 11801da177e4SLinus Torvalds 11811da177e4SLinus Torvalds buffer = (char *)__get_free_page(GFP_KERNEL); 11821da177e4SLinus Torvalds if (!buffer) 11831da177e4SLinus Torvalds return -ENOMEM; 11841da177e4SLinus Torvalds 11851da177e4SLinus Torvalds buflen = PAGE_SIZE; 11861da177e4SLinus Torvalds end = buffer+buflen; 11871da177e4SLinus Torvalds *--end = '\0'; 11881da177e4SLinus Torvalds buflen--; 11891da177e4SLinus Torvalds path = end-1; 11901da177e4SLinus Torvalds *path = '/'; 11911da177e4SLinus Torvalds while (de && de != de->parent) { 11921da177e4SLinus Torvalds buflen -= de->namelen + 1; 11931da177e4SLinus Torvalds if (buflen < 0) 11941da177e4SLinus Torvalds break; 11951da177e4SLinus Torvalds end -= de->namelen; 11961da177e4SLinus Torvalds memcpy(end, de->name, de->namelen); 11971da177e4SLinus Torvalds *--end = '/'; 11981da177e4SLinus Torvalds path = end; 11991da177e4SLinus Torvalds de = de->parent; 12001da177e4SLinus Torvalds } 12011da177e4SLinus Torvalds rc = security_genfs_sid("proc", path, tclass, sid); 12021da177e4SLinus Torvalds free_page((unsigned long)buffer); 12031da177e4SLinus Torvalds return rc; 12041da177e4SLinus Torvalds } 12051da177e4SLinus Torvalds #else 12061da177e4SLinus Torvalds static int selinux_proc_get_sid(struct proc_dir_entry *de, 12071da177e4SLinus Torvalds u16 tclass, 12081da177e4SLinus Torvalds u32 *sid) 12091da177e4SLinus Torvalds { 12101da177e4SLinus Torvalds return -EINVAL; 12111da177e4SLinus Torvalds } 12121da177e4SLinus Torvalds #endif 12131da177e4SLinus Torvalds 12141da177e4SLinus Torvalds /* The inode's security attributes must be initialized before first use. */ 12151da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry) 12161da177e4SLinus Torvalds { 12171da177e4SLinus Torvalds struct superblock_security_struct *sbsec = NULL; 12181da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 12191da177e4SLinus Torvalds u32 sid; 12201da177e4SLinus Torvalds struct dentry *dentry; 12211da177e4SLinus Torvalds #define INITCONTEXTLEN 255 12221da177e4SLinus Torvalds char *context = NULL; 12231da177e4SLinus Torvalds unsigned len = 0; 12241da177e4SLinus Torvalds int rc = 0; 12251da177e4SLinus Torvalds 12261da177e4SLinus Torvalds if (isec->initialized) 12271da177e4SLinus Torvalds goto out; 12281da177e4SLinus Torvalds 122923970741SEric Paris mutex_lock(&isec->lock); 12301da177e4SLinus Torvalds if (isec->initialized) 123123970741SEric Paris goto out_unlock; 12321da177e4SLinus Torvalds 12331da177e4SLinus Torvalds sbsec = inode->i_sb->s_security; 12340d90a7ecSDavid P. Quigley if (!(sbsec->flags & SE_SBINITIALIZED)) { 12351da177e4SLinus Torvalds /* Defer initialization until selinux_complete_init, 12361da177e4SLinus Torvalds after the initial policy is loaded and the security 12371da177e4SLinus Torvalds server is ready to handle calls. */ 12381da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 12391da177e4SLinus Torvalds if (list_empty(&isec->list)) 12401da177e4SLinus Torvalds list_add(&isec->list, &sbsec->isec_head); 12411da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 124223970741SEric Paris goto out_unlock; 12431da177e4SLinus Torvalds } 12441da177e4SLinus Torvalds 12451da177e4SLinus Torvalds switch (sbsec->behavior) { 12461da177e4SLinus Torvalds case SECURITY_FS_USE_XATTR: 12471da177e4SLinus Torvalds if (!inode->i_op->getxattr) { 12481da177e4SLinus Torvalds isec->sid = sbsec->def_sid; 12491da177e4SLinus Torvalds break; 12501da177e4SLinus Torvalds } 12511da177e4SLinus Torvalds 12521da177e4SLinus Torvalds /* Need a dentry, since the xattr API requires one. 12531da177e4SLinus Torvalds Life would be simpler if we could just pass the inode. */ 12541da177e4SLinus Torvalds if (opt_dentry) { 12551da177e4SLinus Torvalds /* Called from d_instantiate or d_splice_alias. */ 12561da177e4SLinus Torvalds dentry = dget(opt_dentry); 12571da177e4SLinus Torvalds } else { 12581da177e4SLinus Torvalds /* Called from selinux_complete_init, try to find a dentry. */ 12591da177e4SLinus Torvalds dentry = d_find_alias(inode); 12601da177e4SLinus Torvalds } 12611da177e4SLinus Torvalds if (!dentry) { 1262df7f54c0SEric Paris /* 1263df7f54c0SEric Paris * this is can be hit on boot when a file is accessed 1264df7f54c0SEric Paris * before the policy is loaded. When we load policy we 1265df7f54c0SEric Paris * may find inodes that have no dentry on the 1266df7f54c0SEric Paris * sbsec->isec_head list. No reason to complain as these 1267df7f54c0SEric Paris * will get fixed up the next time we go through 1268df7f54c0SEric Paris * inode_doinit with a dentry, before these inodes could 1269df7f54c0SEric Paris * be used again by userspace. 1270df7f54c0SEric Paris */ 127123970741SEric Paris goto out_unlock; 12721da177e4SLinus Torvalds } 12731da177e4SLinus Torvalds 12741da177e4SLinus Torvalds len = INITCONTEXTLEN; 12754cb912f1SEric Paris context = kmalloc(len+1, GFP_NOFS); 12761da177e4SLinus Torvalds if (!context) { 12771da177e4SLinus Torvalds rc = -ENOMEM; 12781da177e4SLinus Torvalds dput(dentry); 127923970741SEric Paris goto out_unlock; 12801da177e4SLinus Torvalds } 12814cb912f1SEric Paris context[len] = '\0'; 12821da177e4SLinus Torvalds rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX, 12831da177e4SLinus Torvalds context, len); 12841da177e4SLinus Torvalds if (rc == -ERANGE) { 1285314dabb8SJames Morris kfree(context); 1286314dabb8SJames Morris 12871da177e4SLinus Torvalds /* Need a larger buffer. Query for the right size. */ 12881da177e4SLinus Torvalds rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX, 12891da177e4SLinus Torvalds NULL, 0); 12901da177e4SLinus Torvalds if (rc < 0) { 12911da177e4SLinus Torvalds dput(dentry); 129223970741SEric Paris goto out_unlock; 12931da177e4SLinus Torvalds } 12941da177e4SLinus Torvalds len = rc; 12954cb912f1SEric Paris context = kmalloc(len+1, GFP_NOFS); 12961da177e4SLinus Torvalds if (!context) { 12971da177e4SLinus Torvalds rc = -ENOMEM; 12981da177e4SLinus Torvalds dput(dentry); 129923970741SEric Paris goto out_unlock; 13001da177e4SLinus Torvalds } 13014cb912f1SEric Paris context[len] = '\0'; 13021da177e4SLinus Torvalds rc = inode->i_op->getxattr(dentry, 13031da177e4SLinus Torvalds XATTR_NAME_SELINUX, 13041da177e4SLinus Torvalds context, len); 13051da177e4SLinus Torvalds } 13061da177e4SLinus Torvalds dput(dentry); 13071da177e4SLinus Torvalds if (rc < 0) { 13081da177e4SLinus Torvalds if (rc != -ENODATA) { 1309744ba35eSEric Paris printk(KERN_WARNING "SELinux: %s: getxattr returned " 1310dd6f953aSHarvey Harrison "%d for dev=%s ino=%ld\n", __func__, 13111da177e4SLinus Torvalds -rc, inode->i_sb->s_id, inode->i_ino); 13121da177e4SLinus Torvalds kfree(context); 131323970741SEric Paris goto out_unlock; 13141da177e4SLinus Torvalds } 13151da177e4SLinus Torvalds /* Map ENODATA to the default file SID */ 13161da177e4SLinus Torvalds sid = sbsec->def_sid; 13171da177e4SLinus Torvalds rc = 0; 13181da177e4SLinus Torvalds } else { 1319f5c1d5b2SJames Morris rc = security_context_to_sid_default(context, rc, &sid, 1320869ab514SStephen Smalley sbsec->def_sid, 1321869ab514SStephen Smalley GFP_NOFS); 13221da177e4SLinus Torvalds if (rc) { 13234ba0a8adSEric Paris char *dev = inode->i_sb->s_id; 13244ba0a8adSEric Paris unsigned long ino = inode->i_ino; 13254ba0a8adSEric Paris 13264ba0a8adSEric Paris if (rc == -EINVAL) { 13274ba0a8adSEric Paris if (printk_ratelimit()) 13284ba0a8adSEric Paris printk(KERN_NOTICE "SELinux: inode=%lu on dev=%s was found to have an invalid " 13294ba0a8adSEric Paris "context=%s. This indicates you may need to relabel the inode or the " 13304ba0a8adSEric Paris "filesystem in question.\n", ino, dev, context); 13314ba0a8adSEric Paris } else { 1332744ba35eSEric Paris printk(KERN_WARNING "SELinux: %s: context_to_sid(%s) " 13331da177e4SLinus Torvalds "returned %d for dev=%s ino=%ld\n", 13344ba0a8adSEric Paris __func__, context, -rc, dev, ino); 13354ba0a8adSEric Paris } 13361da177e4SLinus Torvalds kfree(context); 13371da177e4SLinus Torvalds /* Leave with the unlabeled SID */ 13381da177e4SLinus Torvalds rc = 0; 13391da177e4SLinus Torvalds break; 13401da177e4SLinus Torvalds } 13411da177e4SLinus Torvalds } 13421da177e4SLinus Torvalds kfree(context); 13431da177e4SLinus Torvalds isec->sid = sid; 13441da177e4SLinus Torvalds break; 13451da177e4SLinus Torvalds case SECURITY_FS_USE_TASK: 13461da177e4SLinus Torvalds isec->sid = isec->task_sid; 13471da177e4SLinus Torvalds break; 13481da177e4SLinus Torvalds case SECURITY_FS_USE_TRANS: 13491da177e4SLinus Torvalds /* Default to the fs SID. */ 13501da177e4SLinus Torvalds isec->sid = sbsec->sid; 13511da177e4SLinus Torvalds 13521da177e4SLinus Torvalds /* Try to obtain a transition SID. */ 13531da177e4SLinus Torvalds isec->sclass = inode_mode_to_security_class(inode->i_mode); 13541da177e4SLinus Torvalds rc = security_transition_sid(isec->task_sid, 13551da177e4SLinus Torvalds sbsec->sid, 13561da177e4SLinus Torvalds isec->sclass, 13571da177e4SLinus Torvalds &sid); 13581da177e4SLinus Torvalds if (rc) 135923970741SEric Paris goto out_unlock; 13601da177e4SLinus Torvalds isec->sid = sid; 13611da177e4SLinus Torvalds break; 1362c312feb2SEric Paris case SECURITY_FS_USE_MNTPOINT: 1363c312feb2SEric Paris isec->sid = sbsec->mntpoint_sid; 1364c312feb2SEric Paris break; 13651da177e4SLinus Torvalds default: 1366c312feb2SEric Paris /* Default to the fs superblock SID. */ 13671da177e4SLinus Torvalds isec->sid = sbsec->sid; 13681da177e4SLinus Torvalds 13690d90a7ecSDavid P. Quigley if ((sbsec->flags & SE_SBPROC) && !S_ISLNK(inode->i_mode)) { 13701da177e4SLinus Torvalds struct proc_inode *proci = PROC_I(inode); 13711da177e4SLinus Torvalds if (proci->pde) { 13721da177e4SLinus Torvalds isec->sclass = inode_mode_to_security_class(inode->i_mode); 13731da177e4SLinus Torvalds rc = selinux_proc_get_sid(proci->pde, 13741da177e4SLinus Torvalds isec->sclass, 13751da177e4SLinus Torvalds &sid); 13761da177e4SLinus Torvalds if (rc) 137723970741SEric Paris goto out_unlock; 13781da177e4SLinus Torvalds isec->sid = sid; 13791da177e4SLinus Torvalds } 13801da177e4SLinus Torvalds } 13811da177e4SLinus Torvalds break; 13821da177e4SLinus Torvalds } 13831da177e4SLinus Torvalds 13841da177e4SLinus Torvalds isec->initialized = 1; 13851da177e4SLinus Torvalds 138623970741SEric Paris out_unlock: 138723970741SEric Paris mutex_unlock(&isec->lock); 13881da177e4SLinus Torvalds out: 13891da177e4SLinus Torvalds if (isec->sclass == SECCLASS_FILE) 13901da177e4SLinus Torvalds isec->sclass = inode_mode_to_security_class(inode->i_mode); 13911da177e4SLinus Torvalds return rc; 13921da177e4SLinus Torvalds } 13931da177e4SLinus Torvalds 13941da177e4SLinus Torvalds /* Convert a Linux signal to an access vector. */ 13951da177e4SLinus Torvalds static inline u32 signal_to_av(int sig) 13961da177e4SLinus Torvalds { 13971da177e4SLinus Torvalds u32 perm = 0; 13981da177e4SLinus Torvalds 13991da177e4SLinus Torvalds switch (sig) { 14001da177e4SLinus Torvalds case SIGCHLD: 14011da177e4SLinus Torvalds /* Commonly granted from child to parent. */ 14021da177e4SLinus Torvalds perm = PROCESS__SIGCHLD; 14031da177e4SLinus Torvalds break; 14041da177e4SLinus Torvalds case SIGKILL: 14051da177e4SLinus Torvalds /* Cannot be caught or ignored */ 14061da177e4SLinus Torvalds perm = PROCESS__SIGKILL; 14071da177e4SLinus Torvalds break; 14081da177e4SLinus Torvalds case SIGSTOP: 14091da177e4SLinus Torvalds /* Cannot be caught or ignored */ 14101da177e4SLinus Torvalds perm = PROCESS__SIGSTOP; 14111da177e4SLinus Torvalds break; 14121da177e4SLinus Torvalds default: 14131da177e4SLinus Torvalds /* All other signals. */ 14141da177e4SLinus Torvalds perm = PROCESS__SIGNAL; 14151da177e4SLinus Torvalds break; 14161da177e4SLinus Torvalds } 14171da177e4SLinus Torvalds 14181da177e4SLinus Torvalds return perm; 14191da177e4SLinus Torvalds } 14201da177e4SLinus Torvalds 1421275bb41eSDavid Howells /* 1422d84f4f99SDavid Howells * Check permission between a pair of credentials 1423d84f4f99SDavid Howells * fork check, ptrace check, etc. 1424d84f4f99SDavid Howells */ 1425d84f4f99SDavid Howells static int cred_has_perm(const struct cred *actor, 1426d84f4f99SDavid Howells const struct cred *target, 1427d84f4f99SDavid Howells u32 perms) 1428d84f4f99SDavid Howells { 1429d84f4f99SDavid Howells u32 asid = cred_sid(actor), tsid = cred_sid(target); 1430d84f4f99SDavid Howells 1431d84f4f99SDavid Howells return avc_has_perm(asid, tsid, SECCLASS_PROCESS, perms, NULL); 1432d84f4f99SDavid Howells } 1433d84f4f99SDavid Howells 1434d84f4f99SDavid Howells /* 143588e67f3bSDavid Howells * Check permission between a pair of tasks, e.g. signal checks, 1436275bb41eSDavid Howells * fork check, ptrace check, etc. 1437275bb41eSDavid Howells * tsk1 is the actor and tsk2 is the target 14383b11a1deSDavid Howells * - this uses the default subjective creds of tsk1 1439275bb41eSDavid Howells */ 1440275bb41eSDavid Howells static int task_has_perm(const struct task_struct *tsk1, 1441275bb41eSDavid Howells const struct task_struct *tsk2, 14421da177e4SLinus Torvalds u32 perms) 14431da177e4SLinus Torvalds { 1444275bb41eSDavid Howells const struct task_security_struct *__tsec1, *__tsec2; 1445275bb41eSDavid Howells u32 sid1, sid2; 14461da177e4SLinus Torvalds 1447275bb41eSDavid Howells rcu_read_lock(); 1448275bb41eSDavid Howells __tsec1 = __task_cred(tsk1)->security; sid1 = __tsec1->sid; 1449275bb41eSDavid Howells __tsec2 = __task_cred(tsk2)->security; sid2 = __tsec2->sid; 1450275bb41eSDavid Howells rcu_read_unlock(); 1451275bb41eSDavid Howells return avc_has_perm(sid1, sid2, SECCLASS_PROCESS, perms, NULL); 14521da177e4SLinus Torvalds } 14531da177e4SLinus Torvalds 14543b11a1deSDavid Howells /* 14553b11a1deSDavid Howells * Check permission between current and another task, e.g. signal checks, 14563b11a1deSDavid Howells * fork check, ptrace check, etc. 14573b11a1deSDavid Howells * current is the actor and tsk2 is the target 14583b11a1deSDavid Howells * - this uses current's subjective creds 14593b11a1deSDavid Howells */ 14603b11a1deSDavid Howells static int current_has_perm(const struct task_struct *tsk, 14613b11a1deSDavid Howells u32 perms) 14623b11a1deSDavid Howells { 14633b11a1deSDavid Howells u32 sid, tsid; 14643b11a1deSDavid Howells 14653b11a1deSDavid Howells sid = current_sid(); 14663b11a1deSDavid Howells tsid = task_sid(tsk); 14673b11a1deSDavid Howells return avc_has_perm(sid, tsid, SECCLASS_PROCESS, perms, NULL); 14683b11a1deSDavid Howells } 14693b11a1deSDavid Howells 1470b68e418cSStephen Smalley #if CAP_LAST_CAP > 63 1471b68e418cSStephen Smalley #error Fix SELinux to handle capabilities > 63. 1472b68e418cSStephen Smalley #endif 1473b68e418cSStephen Smalley 14741da177e4SLinus Torvalds /* Check whether a task is allowed to use a capability. */ 14751da177e4SLinus Torvalds static int task_has_capability(struct task_struct *tsk, 14763699c53cSDavid Howells const struct cred *cred, 147706112163SEric Paris int cap, int audit) 14781da177e4SLinus Torvalds { 14792bf49690SThomas Liu struct common_audit_data ad; 148006112163SEric Paris struct av_decision avd; 1481b68e418cSStephen Smalley u16 sclass; 14823699c53cSDavid Howells u32 sid = cred_sid(cred); 1483b68e418cSStephen Smalley u32 av = CAP_TO_MASK(cap); 148406112163SEric Paris int rc; 14851da177e4SLinus Torvalds 14862bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, CAP); 14871da177e4SLinus Torvalds ad.tsk = tsk; 14881da177e4SLinus Torvalds ad.u.cap = cap; 14891da177e4SLinus Torvalds 1490b68e418cSStephen Smalley switch (CAP_TO_INDEX(cap)) { 1491b68e418cSStephen Smalley case 0: 1492b68e418cSStephen Smalley sclass = SECCLASS_CAPABILITY; 1493b68e418cSStephen Smalley break; 1494b68e418cSStephen Smalley case 1: 1495b68e418cSStephen Smalley sclass = SECCLASS_CAPABILITY2; 1496b68e418cSStephen Smalley break; 1497b68e418cSStephen Smalley default: 1498b68e418cSStephen Smalley printk(KERN_ERR 1499b68e418cSStephen Smalley "SELinux: out of range capability %d\n", cap); 1500b68e418cSStephen Smalley BUG(); 1501b68e418cSStephen Smalley } 150206112163SEric Paris 1503275bb41eSDavid Howells rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd); 150406112163SEric Paris if (audit == SECURITY_CAP_AUDIT) 1505275bb41eSDavid Howells avc_audit(sid, sid, sclass, av, &avd, rc, &ad); 150606112163SEric Paris return rc; 15071da177e4SLinus Torvalds } 15081da177e4SLinus Torvalds 15091da177e4SLinus Torvalds /* Check whether a task is allowed to use a system operation. */ 15101da177e4SLinus Torvalds static int task_has_system(struct task_struct *tsk, 15111da177e4SLinus Torvalds u32 perms) 15121da177e4SLinus Torvalds { 1513275bb41eSDavid Howells u32 sid = task_sid(tsk); 15141da177e4SLinus Torvalds 1515275bb41eSDavid Howells return avc_has_perm(sid, SECINITSID_KERNEL, 15161da177e4SLinus Torvalds SECCLASS_SYSTEM, perms, NULL); 15171da177e4SLinus Torvalds } 15181da177e4SLinus Torvalds 15191da177e4SLinus Torvalds /* Check whether a task has a particular permission to an inode. 15201da177e4SLinus Torvalds The 'adp' parameter is optional and allows other audit 15211da177e4SLinus Torvalds data to be passed (e.g. the dentry). */ 152288e67f3bSDavid Howells static int inode_has_perm(const struct cred *cred, 15231da177e4SLinus Torvalds struct inode *inode, 15241da177e4SLinus Torvalds u32 perms, 15252bf49690SThomas Liu struct common_audit_data *adp) 15261da177e4SLinus Torvalds { 15271da177e4SLinus Torvalds struct inode_security_struct *isec; 15282bf49690SThomas Liu struct common_audit_data ad; 1529275bb41eSDavid Howells u32 sid; 15301da177e4SLinus Torvalds 1531e0e81739SDavid Howells validate_creds(cred); 1532e0e81739SDavid Howells 1533bbaca6c2SStephen Smalley if (unlikely(IS_PRIVATE(inode))) 1534bbaca6c2SStephen Smalley return 0; 1535bbaca6c2SStephen Smalley 153688e67f3bSDavid Howells sid = cred_sid(cred); 15371da177e4SLinus Torvalds isec = inode->i_security; 15381da177e4SLinus Torvalds 15391da177e4SLinus Torvalds if (!adp) { 15401da177e4SLinus Torvalds adp = &ad; 15412bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, FS); 15421da177e4SLinus Torvalds ad.u.fs.inode = inode; 15431da177e4SLinus Torvalds } 15441da177e4SLinus Torvalds 1545275bb41eSDavid Howells return avc_has_perm(sid, isec->sid, isec->sclass, perms, adp); 15461da177e4SLinus Torvalds } 15471da177e4SLinus Torvalds 15481da177e4SLinus Torvalds /* Same as inode_has_perm, but pass explicit audit data containing 15491da177e4SLinus Torvalds the dentry to help the auditing code to more easily generate the 15501da177e4SLinus Torvalds pathname if needed. */ 155188e67f3bSDavid Howells static inline int dentry_has_perm(const struct cred *cred, 15521da177e4SLinus Torvalds struct vfsmount *mnt, 15531da177e4SLinus Torvalds struct dentry *dentry, 15541da177e4SLinus Torvalds u32 av) 15551da177e4SLinus Torvalds { 15561da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 15572bf49690SThomas Liu struct common_audit_data ad; 155888e67f3bSDavid Howells 15592bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, FS); 156044707fdfSJan Blunck ad.u.fs.path.mnt = mnt; 156144707fdfSJan Blunck ad.u.fs.path.dentry = dentry; 156288e67f3bSDavid Howells return inode_has_perm(cred, inode, av, &ad); 15631da177e4SLinus Torvalds } 15641da177e4SLinus Torvalds 15651da177e4SLinus Torvalds /* Check whether a task can use an open file descriptor to 15661da177e4SLinus Torvalds access an inode in a given way. Check access to the 15671da177e4SLinus Torvalds descriptor itself, and then use dentry_has_perm to 15681da177e4SLinus Torvalds check a particular permission to the file. 15691da177e4SLinus Torvalds Access to the descriptor is implicitly granted if it 15701da177e4SLinus Torvalds has the same SID as the process. If av is zero, then 15711da177e4SLinus Torvalds access to the file is not checked, e.g. for cases 15721da177e4SLinus Torvalds where only the descriptor is affected like seek. */ 157388e67f3bSDavid Howells static int file_has_perm(const struct cred *cred, 15741da177e4SLinus Torvalds struct file *file, 15751da177e4SLinus Torvalds u32 av) 15761da177e4SLinus Torvalds { 15771da177e4SLinus Torvalds struct file_security_struct *fsec = file->f_security; 157844707fdfSJan Blunck struct inode *inode = file->f_path.dentry->d_inode; 15792bf49690SThomas Liu struct common_audit_data ad; 158088e67f3bSDavid Howells u32 sid = cred_sid(cred); 15811da177e4SLinus Torvalds int rc; 15821da177e4SLinus Torvalds 15832bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, FS); 158444707fdfSJan Blunck ad.u.fs.path = file->f_path; 15851da177e4SLinus Torvalds 1586275bb41eSDavid Howells if (sid != fsec->sid) { 1587275bb41eSDavid Howells rc = avc_has_perm(sid, fsec->sid, 15881da177e4SLinus Torvalds SECCLASS_FD, 15891da177e4SLinus Torvalds FD__USE, 15901da177e4SLinus Torvalds &ad); 15911da177e4SLinus Torvalds if (rc) 159288e67f3bSDavid Howells goto out; 15931da177e4SLinus Torvalds } 15941da177e4SLinus Torvalds 15951da177e4SLinus Torvalds /* av is zero if only checking access to the descriptor. */ 159688e67f3bSDavid Howells rc = 0; 15971da177e4SLinus Torvalds if (av) 159888e67f3bSDavid Howells rc = inode_has_perm(cred, inode, av, &ad); 15991da177e4SLinus Torvalds 160088e67f3bSDavid Howells out: 160188e67f3bSDavid Howells return rc; 16021da177e4SLinus Torvalds } 16031da177e4SLinus Torvalds 16041da177e4SLinus Torvalds /* Check whether a task can create a file. */ 16051da177e4SLinus Torvalds static int may_create(struct inode *dir, 16061da177e4SLinus Torvalds struct dentry *dentry, 16071da177e4SLinus Torvalds u16 tclass) 16081da177e4SLinus Torvalds { 1609275bb41eSDavid Howells const struct cred *cred = current_cred(); 1610275bb41eSDavid Howells const struct task_security_struct *tsec = cred->security; 16111da177e4SLinus Torvalds struct inode_security_struct *dsec; 16121da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 1613275bb41eSDavid Howells u32 sid, newsid; 16142bf49690SThomas Liu struct common_audit_data ad; 16151da177e4SLinus Torvalds int rc; 16161da177e4SLinus Torvalds 16171da177e4SLinus Torvalds dsec = dir->i_security; 16181da177e4SLinus Torvalds sbsec = dir->i_sb->s_security; 16191da177e4SLinus Torvalds 1620275bb41eSDavid Howells sid = tsec->sid; 1621275bb41eSDavid Howells newsid = tsec->create_sid; 1622275bb41eSDavid Howells 16232bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, FS); 162444707fdfSJan Blunck ad.u.fs.path.dentry = dentry; 16251da177e4SLinus Torvalds 1626275bb41eSDavid Howells rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, 16271da177e4SLinus Torvalds DIR__ADD_NAME | DIR__SEARCH, 16281da177e4SLinus Torvalds &ad); 16291da177e4SLinus Torvalds if (rc) 16301da177e4SLinus Torvalds return rc; 16311da177e4SLinus Torvalds 1632cd89596fSDavid P. Quigley if (!newsid || !(sbsec->flags & SE_SBLABELSUPP)) { 1633275bb41eSDavid Howells rc = security_transition_sid(sid, dsec->sid, tclass, &newsid); 16341da177e4SLinus Torvalds if (rc) 16351da177e4SLinus Torvalds return rc; 16361da177e4SLinus Torvalds } 16371da177e4SLinus Torvalds 1638275bb41eSDavid Howells rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad); 16391da177e4SLinus Torvalds if (rc) 16401da177e4SLinus Torvalds return rc; 16411da177e4SLinus Torvalds 16421da177e4SLinus Torvalds return avc_has_perm(newsid, sbsec->sid, 16431da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 16441da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, &ad); 16451da177e4SLinus Torvalds } 16461da177e4SLinus Torvalds 16474eb582cfSMichael LeMay /* Check whether a task can create a key. */ 16484eb582cfSMichael LeMay static int may_create_key(u32 ksid, 16494eb582cfSMichael LeMay struct task_struct *ctx) 16504eb582cfSMichael LeMay { 1651275bb41eSDavid Howells u32 sid = task_sid(ctx); 16524eb582cfSMichael LeMay 1653275bb41eSDavid Howells return avc_has_perm(sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL); 16544eb582cfSMichael LeMay } 16554eb582cfSMichael LeMay 16561da177e4SLinus Torvalds #define MAY_LINK 0 16571da177e4SLinus Torvalds #define MAY_UNLINK 1 16581da177e4SLinus Torvalds #define MAY_RMDIR 2 16591da177e4SLinus Torvalds 16601da177e4SLinus Torvalds /* Check whether a task can link, unlink, or rmdir a file/directory. */ 16611da177e4SLinus Torvalds static int may_link(struct inode *dir, 16621da177e4SLinus Torvalds struct dentry *dentry, 16631da177e4SLinus Torvalds int kind) 16641da177e4SLinus Torvalds 16651da177e4SLinus Torvalds { 16661da177e4SLinus Torvalds struct inode_security_struct *dsec, *isec; 16672bf49690SThomas Liu struct common_audit_data ad; 1668275bb41eSDavid Howells u32 sid = current_sid(); 16691da177e4SLinus Torvalds u32 av; 16701da177e4SLinus Torvalds int rc; 16711da177e4SLinus Torvalds 16721da177e4SLinus Torvalds dsec = dir->i_security; 16731da177e4SLinus Torvalds isec = dentry->d_inode->i_security; 16741da177e4SLinus Torvalds 16752bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, FS); 167644707fdfSJan Blunck ad.u.fs.path.dentry = dentry; 16771da177e4SLinus Torvalds 16781da177e4SLinus Torvalds av = DIR__SEARCH; 16791da177e4SLinus Torvalds av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME); 1680275bb41eSDavid Howells rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad); 16811da177e4SLinus Torvalds if (rc) 16821da177e4SLinus Torvalds return rc; 16831da177e4SLinus Torvalds 16841da177e4SLinus Torvalds switch (kind) { 16851da177e4SLinus Torvalds case MAY_LINK: 16861da177e4SLinus Torvalds av = FILE__LINK; 16871da177e4SLinus Torvalds break; 16881da177e4SLinus Torvalds case MAY_UNLINK: 16891da177e4SLinus Torvalds av = FILE__UNLINK; 16901da177e4SLinus Torvalds break; 16911da177e4SLinus Torvalds case MAY_RMDIR: 16921da177e4SLinus Torvalds av = DIR__RMDIR; 16931da177e4SLinus Torvalds break; 16941da177e4SLinus Torvalds default: 1695744ba35eSEric Paris printk(KERN_WARNING "SELinux: %s: unrecognized kind %d\n", 1696744ba35eSEric Paris __func__, kind); 16971da177e4SLinus Torvalds return 0; 16981da177e4SLinus Torvalds } 16991da177e4SLinus Torvalds 1700275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad); 17011da177e4SLinus Torvalds return rc; 17021da177e4SLinus Torvalds } 17031da177e4SLinus Torvalds 17041da177e4SLinus Torvalds static inline int may_rename(struct inode *old_dir, 17051da177e4SLinus Torvalds struct dentry *old_dentry, 17061da177e4SLinus Torvalds struct inode *new_dir, 17071da177e4SLinus Torvalds struct dentry *new_dentry) 17081da177e4SLinus Torvalds { 17091da177e4SLinus Torvalds struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec; 17102bf49690SThomas Liu struct common_audit_data ad; 1711275bb41eSDavid Howells u32 sid = current_sid(); 17121da177e4SLinus Torvalds u32 av; 17131da177e4SLinus Torvalds int old_is_dir, new_is_dir; 17141da177e4SLinus Torvalds int rc; 17151da177e4SLinus Torvalds 17161da177e4SLinus Torvalds old_dsec = old_dir->i_security; 17171da177e4SLinus Torvalds old_isec = old_dentry->d_inode->i_security; 17181da177e4SLinus Torvalds old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 17191da177e4SLinus Torvalds new_dsec = new_dir->i_security; 17201da177e4SLinus Torvalds 17212bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, FS); 17221da177e4SLinus Torvalds 172344707fdfSJan Blunck ad.u.fs.path.dentry = old_dentry; 1724275bb41eSDavid Howells rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR, 17251da177e4SLinus Torvalds DIR__REMOVE_NAME | DIR__SEARCH, &ad); 17261da177e4SLinus Torvalds if (rc) 17271da177e4SLinus Torvalds return rc; 1728275bb41eSDavid Howells rc = avc_has_perm(sid, old_isec->sid, 17291da177e4SLinus Torvalds old_isec->sclass, FILE__RENAME, &ad); 17301da177e4SLinus Torvalds if (rc) 17311da177e4SLinus Torvalds return rc; 17321da177e4SLinus Torvalds if (old_is_dir && new_dir != old_dir) { 1733275bb41eSDavid Howells rc = avc_has_perm(sid, old_isec->sid, 17341da177e4SLinus Torvalds old_isec->sclass, DIR__REPARENT, &ad); 17351da177e4SLinus Torvalds if (rc) 17361da177e4SLinus Torvalds return rc; 17371da177e4SLinus Torvalds } 17381da177e4SLinus Torvalds 173944707fdfSJan Blunck ad.u.fs.path.dentry = new_dentry; 17401da177e4SLinus Torvalds av = DIR__ADD_NAME | DIR__SEARCH; 17411da177e4SLinus Torvalds if (new_dentry->d_inode) 17421da177e4SLinus Torvalds av |= DIR__REMOVE_NAME; 1743275bb41eSDavid Howells rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad); 17441da177e4SLinus Torvalds if (rc) 17451da177e4SLinus Torvalds return rc; 17461da177e4SLinus Torvalds if (new_dentry->d_inode) { 17471da177e4SLinus Torvalds new_isec = new_dentry->d_inode->i_security; 17481da177e4SLinus Torvalds new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode); 1749275bb41eSDavid Howells rc = avc_has_perm(sid, new_isec->sid, 17501da177e4SLinus Torvalds new_isec->sclass, 17511da177e4SLinus Torvalds (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad); 17521da177e4SLinus Torvalds if (rc) 17531da177e4SLinus Torvalds return rc; 17541da177e4SLinus Torvalds } 17551da177e4SLinus Torvalds 17561da177e4SLinus Torvalds return 0; 17571da177e4SLinus Torvalds } 17581da177e4SLinus Torvalds 17591da177e4SLinus Torvalds /* Check whether a task can perform a filesystem operation. */ 176088e67f3bSDavid Howells static int superblock_has_perm(const struct cred *cred, 17611da177e4SLinus Torvalds struct super_block *sb, 17621da177e4SLinus Torvalds u32 perms, 17632bf49690SThomas Liu struct common_audit_data *ad) 17641da177e4SLinus Torvalds { 17651da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 176688e67f3bSDavid Howells u32 sid = cred_sid(cred); 17671da177e4SLinus Torvalds 17681da177e4SLinus Torvalds sbsec = sb->s_security; 1769275bb41eSDavid Howells return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad); 17701da177e4SLinus Torvalds } 17711da177e4SLinus Torvalds 17721da177e4SLinus Torvalds /* Convert a Linux mode and permission mask to an access vector. */ 17731da177e4SLinus Torvalds static inline u32 file_mask_to_av(int mode, int mask) 17741da177e4SLinus Torvalds { 17751da177e4SLinus Torvalds u32 av = 0; 17761da177e4SLinus Torvalds 17771da177e4SLinus Torvalds if ((mode & S_IFMT) != S_IFDIR) { 17781da177e4SLinus Torvalds if (mask & MAY_EXEC) 17791da177e4SLinus Torvalds av |= FILE__EXECUTE; 17801da177e4SLinus Torvalds if (mask & MAY_READ) 17811da177e4SLinus Torvalds av |= FILE__READ; 17821da177e4SLinus Torvalds 17831da177e4SLinus Torvalds if (mask & MAY_APPEND) 17841da177e4SLinus Torvalds av |= FILE__APPEND; 17851da177e4SLinus Torvalds else if (mask & MAY_WRITE) 17861da177e4SLinus Torvalds av |= FILE__WRITE; 17871da177e4SLinus Torvalds 17881da177e4SLinus Torvalds } else { 17891da177e4SLinus Torvalds if (mask & MAY_EXEC) 17901da177e4SLinus Torvalds av |= DIR__SEARCH; 17911da177e4SLinus Torvalds if (mask & MAY_WRITE) 17921da177e4SLinus Torvalds av |= DIR__WRITE; 17931da177e4SLinus Torvalds if (mask & MAY_READ) 17941da177e4SLinus Torvalds av |= DIR__READ; 17951da177e4SLinus Torvalds } 17961da177e4SLinus Torvalds 17971da177e4SLinus Torvalds return av; 17981da177e4SLinus Torvalds } 17991da177e4SLinus Torvalds 18001da177e4SLinus Torvalds /* Convert a Linux file to an access vector. */ 18011da177e4SLinus Torvalds static inline u32 file_to_av(struct file *file) 18021da177e4SLinus Torvalds { 18031da177e4SLinus Torvalds u32 av = 0; 18041da177e4SLinus Torvalds 18051da177e4SLinus Torvalds if (file->f_mode & FMODE_READ) 18061da177e4SLinus Torvalds av |= FILE__READ; 18071da177e4SLinus Torvalds if (file->f_mode & FMODE_WRITE) { 18081da177e4SLinus Torvalds if (file->f_flags & O_APPEND) 18091da177e4SLinus Torvalds av |= FILE__APPEND; 18101da177e4SLinus Torvalds else 18111da177e4SLinus Torvalds av |= FILE__WRITE; 18121da177e4SLinus Torvalds } 18130794c66dSStephen Smalley if (!av) { 18140794c66dSStephen Smalley /* 18150794c66dSStephen Smalley * Special file opened with flags 3 for ioctl-only use. 18160794c66dSStephen Smalley */ 18170794c66dSStephen Smalley av = FILE__IOCTL; 18180794c66dSStephen Smalley } 18191da177e4SLinus Torvalds 18201da177e4SLinus Torvalds return av; 18211da177e4SLinus Torvalds } 18221da177e4SLinus Torvalds 18238b6a5a37SEric Paris /* 18248b6a5a37SEric Paris * Convert a file to an access vector and include the correct open 18258b6a5a37SEric Paris * open permission. 18268b6a5a37SEric Paris */ 18278b6a5a37SEric Paris static inline u32 open_file_to_av(struct file *file) 18288b6a5a37SEric Paris { 18298b6a5a37SEric Paris u32 av = file_to_av(file); 18308b6a5a37SEric Paris 18318b6a5a37SEric Paris if (selinux_policycap_openperm) { 18328b6a5a37SEric Paris mode_t mode = file->f_path.dentry->d_inode->i_mode; 18338b6a5a37SEric Paris /* 18348b6a5a37SEric Paris * lnk files and socks do not really have an 'open' 18358b6a5a37SEric Paris */ 18368b6a5a37SEric Paris if (S_ISREG(mode)) 18378b6a5a37SEric Paris av |= FILE__OPEN; 18388b6a5a37SEric Paris else if (S_ISCHR(mode)) 18398b6a5a37SEric Paris av |= CHR_FILE__OPEN; 18408b6a5a37SEric Paris else if (S_ISBLK(mode)) 18418b6a5a37SEric Paris av |= BLK_FILE__OPEN; 18428b6a5a37SEric Paris else if (S_ISFIFO(mode)) 18438b6a5a37SEric Paris av |= FIFO_FILE__OPEN; 18448b6a5a37SEric Paris else if (S_ISDIR(mode)) 18458b6a5a37SEric Paris av |= DIR__OPEN; 18466a25b27dSEric Paris else if (S_ISSOCK(mode)) 18476a25b27dSEric Paris av |= SOCK_FILE__OPEN; 18488b6a5a37SEric Paris else 18498b6a5a37SEric Paris printk(KERN_ERR "SELinux: WARNING: inside %s with " 18508b6a5a37SEric Paris "unknown mode:%o\n", __func__, mode); 18518b6a5a37SEric Paris } 18528b6a5a37SEric Paris return av; 18538b6a5a37SEric Paris } 18548b6a5a37SEric Paris 18551da177e4SLinus Torvalds /* Hook functions begin here. */ 18561da177e4SLinus Torvalds 18579e48858fSIngo Molnar static int selinux_ptrace_access_check(struct task_struct *child, 1858006ebb40SStephen Smalley unsigned int mode) 18591da177e4SLinus Torvalds { 18601da177e4SLinus Torvalds int rc; 18611da177e4SLinus Torvalds 18629e48858fSIngo Molnar rc = cap_ptrace_access_check(child, mode); 18631da177e4SLinus Torvalds if (rc) 18641da177e4SLinus Torvalds return rc; 18651da177e4SLinus Torvalds 1866006ebb40SStephen Smalley if (mode == PTRACE_MODE_READ) { 1867275bb41eSDavid Howells u32 sid = current_sid(); 1868275bb41eSDavid Howells u32 csid = task_sid(child); 1869275bb41eSDavid Howells return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, NULL); 1870006ebb40SStephen Smalley } 1871006ebb40SStephen Smalley 18723b11a1deSDavid Howells return current_has_perm(child, PROCESS__PTRACE); 18735cd9c58fSDavid Howells } 18745cd9c58fSDavid Howells 18755cd9c58fSDavid Howells static int selinux_ptrace_traceme(struct task_struct *parent) 18765cd9c58fSDavid Howells { 18775cd9c58fSDavid Howells int rc; 18785cd9c58fSDavid Howells 1879200ac532SEric Paris rc = cap_ptrace_traceme(parent); 18805cd9c58fSDavid Howells if (rc) 18815cd9c58fSDavid Howells return rc; 18825cd9c58fSDavid Howells 18835cd9c58fSDavid Howells return task_has_perm(parent, current, PROCESS__PTRACE); 18841da177e4SLinus Torvalds } 18851da177e4SLinus Torvalds 18861da177e4SLinus Torvalds static int selinux_capget(struct task_struct *target, kernel_cap_t *effective, 18871da177e4SLinus Torvalds kernel_cap_t *inheritable, kernel_cap_t *permitted) 18881da177e4SLinus Torvalds { 18891da177e4SLinus Torvalds int error; 18901da177e4SLinus Torvalds 18913b11a1deSDavid Howells error = current_has_perm(target, PROCESS__GETCAP); 18921da177e4SLinus Torvalds if (error) 18931da177e4SLinus Torvalds return error; 18941da177e4SLinus Torvalds 1895200ac532SEric Paris return cap_capget(target, effective, inheritable, permitted); 18961da177e4SLinus Torvalds } 18971da177e4SLinus Torvalds 1898d84f4f99SDavid Howells static int selinux_capset(struct cred *new, const struct cred *old, 1899d84f4f99SDavid Howells const kernel_cap_t *effective, 190015a2460eSDavid Howells const kernel_cap_t *inheritable, 190115a2460eSDavid Howells const kernel_cap_t *permitted) 19021da177e4SLinus Torvalds { 19031da177e4SLinus Torvalds int error; 19041da177e4SLinus Torvalds 1905200ac532SEric Paris error = cap_capset(new, old, 1906d84f4f99SDavid Howells effective, inheritable, permitted); 19071da177e4SLinus Torvalds if (error) 19081da177e4SLinus Torvalds return error; 19091da177e4SLinus Torvalds 1910d84f4f99SDavid Howells return cred_has_perm(old, new, PROCESS__SETCAP); 19111da177e4SLinus Torvalds } 19121da177e4SLinus Torvalds 19135626d3e8SJames Morris /* 19145626d3e8SJames Morris * (This comment used to live with the selinux_task_setuid hook, 19155626d3e8SJames Morris * which was removed). 19165626d3e8SJames Morris * 19175626d3e8SJames Morris * Since setuid only affects the current process, and since the SELinux 19185626d3e8SJames Morris * controls are not based on the Linux identity attributes, SELinux does not 19195626d3e8SJames Morris * need to control this operation. However, SELinux does control the use of 19205626d3e8SJames Morris * the CAP_SETUID and CAP_SETGID capabilities using the capable hook. 19215626d3e8SJames Morris */ 19225626d3e8SJames Morris 19233699c53cSDavid Howells static int selinux_capable(struct task_struct *tsk, const struct cred *cred, 19243699c53cSDavid Howells int cap, int audit) 19251da177e4SLinus Torvalds { 19261da177e4SLinus Torvalds int rc; 19271da177e4SLinus Torvalds 1928200ac532SEric Paris rc = cap_capable(tsk, cred, cap, audit); 19291da177e4SLinus Torvalds if (rc) 19301da177e4SLinus Torvalds return rc; 19311da177e4SLinus Torvalds 19323699c53cSDavid Howells return task_has_capability(tsk, cred, cap, audit); 19331da177e4SLinus Torvalds } 19341da177e4SLinus Torvalds 19353fbfa981SEric W. Biederman static int selinux_sysctl_get_sid(ctl_table *table, u16 tclass, u32 *sid) 19363fbfa981SEric W. Biederman { 19373fbfa981SEric W. Biederman int buflen, rc; 19383fbfa981SEric W. Biederman char *buffer, *path, *end; 19393fbfa981SEric W. Biederman 19403fbfa981SEric W. Biederman rc = -ENOMEM; 19413fbfa981SEric W. Biederman buffer = (char *)__get_free_page(GFP_KERNEL); 19423fbfa981SEric W. Biederman if (!buffer) 19433fbfa981SEric W. Biederman goto out; 19443fbfa981SEric W. Biederman 19453fbfa981SEric W. Biederman buflen = PAGE_SIZE; 19463fbfa981SEric W. Biederman end = buffer+buflen; 19473fbfa981SEric W. Biederman *--end = '\0'; 19483fbfa981SEric W. Biederman buflen--; 19493fbfa981SEric W. Biederman path = end-1; 19503fbfa981SEric W. Biederman *path = '/'; 19513fbfa981SEric W. Biederman while (table) { 19523fbfa981SEric W. Biederman const char *name = table->procname; 19533fbfa981SEric W. Biederman size_t namelen = strlen(name); 19543fbfa981SEric W. Biederman buflen -= namelen + 1; 19553fbfa981SEric W. Biederman if (buflen < 0) 19563fbfa981SEric W. Biederman goto out_free; 19573fbfa981SEric W. Biederman end -= namelen; 19583fbfa981SEric W. Biederman memcpy(end, name, namelen); 19593fbfa981SEric W. Biederman *--end = '/'; 19603fbfa981SEric W. Biederman path = end; 19613fbfa981SEric W. Biederman table = table->parent; 19623fbfa981SEric W. Biederman } 1963b599fdfdSEric W. Biederman buflen -= 4; 1964b599fdfdSEric W. Biederman if (buflen < 0) 1965b599fdfdSEric W. Biederman goto out_free; 1966b599fdfdSEric W. Biederman end -= 4; 1967b599fdfdSEric W. Biederman memcpy(end, "/sys", 4); 1968b599fdfdSEric W. Biederman path = end; 19693fbfa981SEric W. Biederman rc = security_genfs_sid("proc", path, tclass, sid); 19703fbfa981SEric W. Biederman out_free: 19713fbfa981SEric W. Biederman free_page((unsigned long)buffer); 19723fbfa981SEric W. Biederman out: 19733fbfa981SEric W. Biederman return rc; 19743fbfa981SEric W. Biederman } 19753fbfa981SEric W. Biederman 19761da177e4SLinus Torvalds static int selinux_sysctl(ctl_table *table, int op) 19771da177e4SLinus Torvalds { 19781da177e4SLinus Torvalds int error = 0; 19791da177e4SLinus Torvalds u32 av; 1980275bb41eSDavid Howells u32 tsid, sid; 19811da177e4SLinus Torvalds int rc; 19821da177e4SLinus Torvalds 1983275bb41eSDavid Howells sid = current_sid(); 19841da177e4SLinus Torvalds 19853fbfa981SEric W. Biederman rc = selinux_sysctl_get_sid(table, (op == 0001) ? 19861da177e4SLinus Torvalds SECCLASS_DIR : SECCLASS_FILE, &tsid); 19871da177e4SLinus Torvalds if (rc) { 19881da177e4SLinus Torvalds /* Default to the well-defined sysctl SID. */ 19891da177e4SLinus Torvalds tsid = SECINITSID_SYSCTL; 19901da177e4SLinus Torvalds } 19911da177e4SLinus Torvalds 19921da177e4SLinus Torvalds /* The op values are "defined" in sysctl.c, thereby creating 19931da177e4SLinus Torvalds * a bad coupling between this module and sysctl.c */ 19941da177e4SLinus Torvalds if (op == 001) { 1995275bb41eSDavid Howells error = avc_has_perm(sid, tsid, 19961da177e4SLinus Torvalds SECCLASS_DIR, DIR__SEARCH, NULL); 19971da177e4SLinus Torvalds } else { 19981da177e4SLinus Torvalds av = 0; 19991da177e4SLinus Torvalds if (op & 004) 20001da177e4SLinus Torvalds av |= FILE__READ; 20011da177e4SLinus Torvalds if (op & 002) 20021da177e4SLinus Torvalds av |= FILE__WRITE; 20031da177e4SLinus Torvalds if (av) 2004275bb41eSDavid Howells error = avc_has_perm(sid, tsid, 20051da177e4SLinus Torvalds SECCLASS_FILE, av, NULL); 20061da177e4SLinus Torvalds } 20071da177e4SLinus Torvalds 20081da177e4SLinus Torvalds return error; 20091da177e4SLinus Torvalds } 20101da177e4SLinus Torvalds 20111da177e4SLinus Torvalds static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb) 20121da177e4SLinus Torvalds { 201388e67f3bSDavid Howells const struct cred *cred = current_cred(); 20141da177e4SLinus Torvalds int rc = 0; 20151da177e4SLinus Torvalds 20161da177e4SLinus Torvalds if (!sb) 20171da177e4SLinus Torvalds return 0; 20181da177e4SLinus Torvalds 20191da177e4SLinus Torvalds switch (cmds) { 20201da177e4SLinus Torvalds case Q_SYNC: 20211da177e4SLinus Torvalds case Q_QUOTAON: 20221da177e4SLinus Torvalds case Q_QUOTAOFF: 20231da177e4SLinus Torvalds case Q_SETINFO: 20241da177e4SLinus Torvalds case Q_SETQUOTA: 202588e67f3bSDavid Howells rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL); 20261da177e4SLinus Torvalds break; 20271da177e4SLinus Torvalds case Q_GETFMT: 20281da177e4SLinus Torvalds case Q_GETINFO: 20291da177e4SLinus Torvalds case Q_GETQUOTA: 203088e67f3bSDavid Howells rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL); 20311da177e4SLinus Torvalds break; 20321da177e4SLinus Torvalds default: 20331da177e4SLinus Torvalds rc = 0; /* let the kernel handle invalid cmds */ 20341da177e4SLinus Torvalds break; 20351da177e4SLinus Torvalds } 20361da177e4SLinus Torvalds return rc; 20371da177e4SLinus Torvalds } 20381da177e4SLinus Torvalds 20391da177e4SLinus Torvalds static int selinux_quota_on(struct dentry *dentry) 20401da177e4SLinus Torvalds { 204188e67f3bSDavid Howells const struct cred *cred = current_cred(); 204288e67f3bSDavid Howells 204388e67f3bSDavid Howells return dentry_has_perm(cred, NULL, dentry, FILE__QUOTAON); 20441da177e4SLinus Torvalds } 20451da177e4SLinus Torvalds 204600234592SKees Cook static int selinux_syslog(int type, bool from_file) 20471da177e4SLinus Torvalds { 20481da177e4SLinus Torvalds int rc; 20491da177e4SLinus Torvalds 205000234592SKees Cook rc = cap_syslog(type, from_file); 20511da177e4SLinus Torvalds if (rc) 20521da177e4SLinus Torvalds return rc; 20531da177e4SLinus Torvalds 20541da177e4SLinus Torvalds switch (type) { 2055d78ca3cdSKees Cook case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */ 2056d78ca3cdSKees Cook case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */ 20571da177e4SLinus Torvalds rc = task_has_system(current, SYSTEM__SYSLOG_READ); 20581da177e4SLinus Torvalds break; 2059d78ca3cdSKees Cook case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */ 2060d78ca3cdSKees Cook case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */ 2061d78ca3cdSKees Cook /* Set level of messages printed to console */ 2062d78ca3cdSKees Cook case SYSLOG_ACTION_CONSOLE_LEVEL: 20631da177e4SLinus Torvalds rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE); 20641da177e4SLinus Torvalds break; 2065d78ca3cdSKees Cook case SYSLOG_ACTION_CLOSE: /* Close log */ 2066d78ca3cdSKees Cook case SYSLOG_ACTION_OPEN: /* Open log */ 2067d78ca3cdSKees Cook case SYSLOG_ACTION_READ: /* Read from log */ 2068d78ca3cdSKees Cook case SYSLOG_ACTION_READ_CLEAR: /* Read/clear last kernel messages */ 2069d78ca3cdSKees Cook case SYSLOG_ACTION_CLEAR: /* Clear ring buffer */ 20701da177e4SLinus Torvalds default: 20711da177e4SLinus Torvalds rc = task_has_system(current, SYSTEM__SYSLOG_MOD); 20721da177e4SLinus Torvalds break; 20731da177e4SLinus Torvalds } 20741da177e4SLinus Torvalds return rc; 20751da177e4SLinus Torvalds } 20761da177e4SLinus Torvalds 20771da177e4SLinus Torvalds /* 20781da177e4SLinus Torvalds * Check that a process has enough memory to allocate a new virtual 20791da177e4SLinus Torvalds * mapping. 0 means there is enough memory for the allocation to 20801da177e4SLinus Torvalds * succeed and -ENOMEM implies there is not. 20811da177e4SLinus Torvalds * 20821da177e4SLinus Torvalds * Do not audit the selinux permission check, as this is applied to all 20831da177e4SLinus Torvalds * processes that allocate mappings. 20841da177e4SLinus Torvalds */ 208534b4e4aaSAlan Cox static int selinux_vm_enough_memory(struct mm_struct *mm, long pages) 20861da177e4SLinus Torvalds { 20871da177e4SLinus Torvalds int rc, cap_sys_admin = 0; 20881da177e4SLinus Torvalds 20893699c53cSDavid Howells rc = selinux_capable(current, current_cred(), CAP_SYS_ADMIN, 20903699c53cSDavid Howells SECURITY_CAP_NOAUDIT); 20911da177e4SLinus Torvalds if (rc == 0) 20921da177e4SLinus Torvalds cap_sys_admin = 1; 20931da177e4SLinus Torvalds 209434b4e4aaSAlan Cox return __vm_enough_memory(mm, pages, cap_sys_admin); 20951da177e4SLinus Torvalds } 20961da177e4SLinus Torvalds 20971da177e4SLinus Torvalds /* binprm security operations */ 20981da177e4SLinus Torvalds 2099a6f76f23SDavid Howells static int selinux_bprm_set_creds(struct linux_binprm *bprm) 21001da177e4SLinus Torvalds { 2101a6f76f23SDavid Howells const struct task_security_struct *old_tsec; 2102a6f76f23SDavid Howells struct task_security_struct *new_tsec; 21031da177e4SLinus Torvalds struct inode_security_struct *isec; 21042bf49690SThomas Liu struct common_audit_data ad; 2105a6f76f23SDavid Howells struct inode *inode = bprm->file->f_path.dentry->d_inode; 21061da177e4SLinus Torvalds int rc; 21071da177e4SLinus Torvalds 2108200ac532SEric Paris rc = cap_bprm_set_creds(bprm); 21091da177e4SLinus Torvalds if (rc) 21101da177e4SLinus Torvalds return rc; 21111da177e4SLinus Torvalds 2112a6f76f23SDavid Howells /* SELinux context only depends on initial program or script and not 2113a6f76f23SDavid Howells * the script interpreter */ 2114a6f76f23SDavid Howells if (bprm->cred_prepared) 21151da177e4SLinus Torvalds return 0; 21161da177e4SLinus Torvalds 2117a6f76f23SDavid Howells old_tsec = current_security(); 2118a6f76f23SDavid Howells new_tsec = bprm->cred->security; 21191da177e4SLinus Torvalds isec = inode->i_security; 21201da177e4SLinus Torvalds 21211da177e4SLinus Torvalds /* Default to the current task SID. */ 2122a6f76f23SDavid Howells new_tsec->sid = old_tsec->sid; 2123a6f76f23SDavid Howells new_tsec->osid = old_tsec->sid; 21241da177e4SLinus Torvalds 212528eba5bfSMichael LeMay /* Reset fs, key, and sock SIDs on execve. */ 2126a6f76f23SDavid Howells new_tsec->create_sid = 0; 2127a6f76f23SDavid Howells new_tsec->keycreate_sid = 0; 2128a6f76f23SDavid Howells new_tsec->sockcreate_sid = 0; 21291da177e4SLinus Torvalds 2130a6f76f23SDavid Howells if (old_tsec->exec_sid) { 2131a6f76f23SDavid Howells new_tsec->sid = old_tsec->exec_sid; 21321da177e4SLinus Torvalds /* Reset exec SID on execve. */ 2133a6f76f23SDavid Howells new_tsec->exec_sid = 0; 21341da177e4SLinus Torvalds } else { 21351da177e4SLinus Torvalds /* Check for a default transition on this program. */ 2136a6f76f23SDavid Howells rc = security_transition_sid(old_tsec->sid, isec->sid, 2137a6f76f23SDavid Howells SECCLASS_PROCESS, &new_tsec->sid); 21381da177e4SLinus Torvalds if (rc) 21391da177e4SLinus Torvalds return rc; 21401da177e4SLinus Torvalds } 21411da177e4SLinus Torvalds 21422bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, FS); 214344707fdfSJan Blunck ad.u.fs.path = bprm->file->f_path; 21441da177e4SLinus Torvalds 21453d5ff529SJosef Sipek if (bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) 2146a6f76f23SDavid Howells new_tsec->sid = old_tsec->sid; 21471da177e4SLinus Torvalds 2148a6f76f23SDavid Howells if (new_tsec->sid == old_tsec->sid) { 2149a6f76f23SDavid Howells rc = avc_has_perm(old_tsec->sid, isec->sid, 21501da177e4SLinus Torvalds SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad); 21511da177e4SLinus Torvalds if (rc) 21521da177e4SLinus Torvalds return rc; 21531da177e4SLinus Torvalds } else { 21541da177e4SLinus Torvalds /* Check permissions for the transition. */ 2155a6f76f23SDavid Howells rc = avc_has_perm(old_tsec->sid, new_tsec->sid, 21561da177e4SLinus Torvalds SECCLASS_PROCESS, PROCESS__TRANSITION, &ad); 21571da177e4SLinus Torvalds if (rc) 21581da177e4SLinus Torvalds return rc; 21591da177e4SLinus Torvalds 2160a6f76f23SDavid Howells rc = avc_has_perm(new_tsec->sid, isec->sid, 21611da177e4SLinus Torvalds SECCLASS_FILE, FILE__ENTRYPOINT, &ad); 21621da177e4SLinus Torvalds if (rc) 21631da177e4SLinus Torvalds return rc; 21641da177e4SLinus Torvalds 2165a6f76f23SDavid Howells /* Check for shared state */ 2166a6f76f23SDavid Howells if (bprm->unsafe & LSM_UNSAFE_SHARE) { 2167a6f76f23SDavid Howells rc = avc_has_perm(old_tsec->sid, new_tsec->sid, 2168a6f76f23SDavid Howells SECCLASS_PROCESS, PROCESS__SHARE, 2169a6f76f23SDavid Howells NULL); 2170a6f76f23SDavid Howells if (rc) 2171a6f76f23SDavid Howells return -EPERM; 21721da177e4SLinus Torvalds } 21731da177e4SLinus Torvalds 2174a6f76f23SDavid Howells /* Make sure that anyone attempting to ptrace over a task that 2175a6f76f23SDavid Howells * changes its SID has the appropriate permit */ 2176a6f76f23SDavid Howells if (bprm->unsafe & 2177a6f76f23SDavid Howells (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) { 2178a6f76f23SDavid Howells struct task_struct *tracer; 2179a6f76f23SDavid Howells struct task_security_struct *sec; 2180a6f76f23SDavid Howells u32 ptsid = 0; 2181a6f76f23SDavid Howells 2182a6f76f23SDavid Howells rcu_read_lock(); 2183a6f76f23SDavid Howells tracer = tracehook_tracer_task(current); 2184a6f76f23SDavid Howells if (likely(tracer != NULL)) { 2185a6f76f23SDavid Howells sec = __task_cred(tracer)->security; 2186a6f76f23SDavid Howells ptsid = sec->sid; 2187a6f76f23SDavid Howells } 2188a6f76f23SDavid Howells rcu_read_unlock(); 2189a6f76f23SDavid Howells 2190a6f76f23SDavid Howells if (ptsid != 0) { 2191a6f76f23SDavid Howells rc = avc_has_perm(ptsid, new_tsec->sid, 2192a6f76f23SDavid Howells SECCLASS_PROCESS, 2193a6f76f23SDavid Howells PROCESS__PTRACE, NULL); 2194a6f76f23SDavid Howells if (rc) 2195a6f76f23SDavid Howells return -EPERM; 2196a6f76f23SDavid Howells } 2197a6f76f23SDavid Howells } 2198a6f76f23SDavid Howells 2199a6f76f23SDavid Howells /* Clear any possibly unsafe personality bits on exec: */ 2200a6f76f23SDavid Howells bprm->per_clear |= PER_CLEAR_ON_SETID; 2201a6f76f23SDavid Howells } 2202a6f76f23SDavid Howells 22031da177e4SLinus Torvalds return 0; 22041da177e4SLinus Torvalds } 22051da177e4SLinus Torvalds 22061da177e4SLinus Torvalds static int selinux_bprm_secureexec(struct linux_binprm *bprm) 22071da177e4SLinus Torvalds { 2208275bb41eSDavid Howells const struct cred *cred = current_cred(); 2209275bb41eSDavid Howells const struct task_security_struct *tsec = cred->security; 2210275bb41eSDavid Howells u32 sid, osid; 22111da177e4SLinus Torvalds int atsecure = 0; 22121da177e4SLinus Torvalds 2213275bb41eSDavid Howells sid = tsec->sid; 2214275bb41eSDavid Howells osid = tsec->osid; 2215275bb41eSDavid Howells 2216275bb41eSDavid Howells if (osid != sid) { 22171da177e4SLinus Torvalds /* Enable secure mode for SIDs transitions unless 22181da177e4SLinus Torvalds the noatsecure permission is granted between 22191da177e4SLinus Torvalds the two SIDs, i.e. ahp returns 0. */ 2220275bb41eSDavid Howells atsecure = avc_has_perm(osid, sid, 22211da177e4SLinus Torvalds SECCLASS_PROCESS, 22221da177e4SLinus Torvalds PROCESS__NOATSECURE, NULL); 22231da177e4SLinus Torvalds } 22241da177e4SLinus Torvalds 2225200ac532SEric Paris return (atsecure || cap_bprm_secureexec(bprm)); 22261da177e4SLinus Torvalds } 22271da177e4SLinus Torvalds 22281da177e4SLinus Torvalds extern struct vfsmount *selinuxfs_mount; 22291da177e4SLinus Torvalds extern struct dentry *selinux_null; 22301da177e4SLinus Torvalds 22311da177e4SLinus Torvalds /* Derived from fs/exec.c:flush_old_files. */ 2232745ca247SDavid Howells static inline void flush_unauthorized_files(const struct cred *cred, 2233745ca247SDavid Howells struct files_struct *files) 22341da177e4SLinus Torvalds { 22352bf49690SThomas Liu struct common_audit_data ad; 22361da177e4SLinus Torvalds struct file *file, *devnull = NULL; 2237b20c8122SStephen Smalley struct tty_struct *tty; 2238badf1662SDipankar Sarma struct fdtable *fdt; 22391da177e4SLinus Torvalds long j = -1; 224024ec839cSPeter Zijlstra int drop_tty = 0; 22411da177e4SLinus Torvalds 224224ec839cSPeter Zijlstra tty = get_current_tty(); 22431da177e4SLinus Torvalds if (tty) { 22441da177e4SLinus Torvalds file_list_lock(); 224537dd0bd0SEric Paris if (!list_empty(&tty->tty_files)) { 224637dd0bd0SEric Paris struct inode *inode; 224737dd0bd0SEric Paris 22481da177e4SLinus Torvalds /* Revalidate access to controlling tty. 22491da177e4SLinus Torvalds Use inode_has_perm on the tty inode directly rather 22501da177e4SLinus Torvalds than using file_has_perm, as this particular open 22511da177e4SLinus Torvalds file may belong to another process and we are only 22521da177e4SLinus Torvalds interested in the inode-based check here. */ 225337dd0bd0SEric Paris file = list_first_entry(&tty->tty_files, struct file, f_u.fu_list); 225437dd0bd0SEric Paris inode = file->f_path.dentry->d_inode; 225588e67f3bSDavid Howells if (inode_has_perm(cred, inode, 22561da177e4SLinus Torvalds FILE__READ | FILE__WRITE, NULL)) { 225724ec839cSPeter Zijlstra drop_tty = 1; 22581da177e4SLinus Torvalds } 22591da177e4SLinus Torvalds } 22601da177e4SLinus Torvalds file_list_unlock(); 2261452a00d2SAlan Cox tty_kref_put(tty); 22621da177e4SLinus Torvalds } 226398a27ba4SEric W. Biederman /* Reset controlling tty. */ 226498a27ba4SEric W. Biederman if (drop_tty) 226598a27ba4SEric W. Biederman no_tty(); 22661da177e4SLinus Torvalds 22671da177e4SLinus Torvalds /* Revalidate access to inherited open files. */ 22681da177e4SLinus Torvalds 22692bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, FS); 22701da177e4SLinus Torvalds 22711da177e4SLinus Torvalds spin_lock(&files->file_lock); 22721da177e4SLinus Torvalds for (;;) { 22731da177e4SLinus Torvalds unsigned long set, i; 22741da177e4SLinus Torvalds int fd; 22751da177e4SLinus Torvalds 22761da177e4SLinus Torvalds j++; 22771da177e4SLinus Torvalds i = j * __NFDBITS; 2278badf1662SDipankar Sarma fdt = files_fdtable(files); 2279bbea9f69SVadim Lobanov if (i >= fdt->max_fds) 22801da177e4SLinus Torvalds break; 2281badf1662SDipankar Sarma set = fdt->open_fds->fds_bits[j]; 22821da177e4SLinus Torvalds if (!set) 22831da177e4SLinus Torvalds continue; 22841da177e4SLinus Torvalds spin_unlock(&files->file_lock); 22851da177e4SLinus Torvalds for ( ; set ; i++, set >>= 1) { 22861da177e4SLinus Torvalds if (set & 1) { 22871da177e4SLinus Torvalds file = fget(i); 22881da177e4SLinus Torvalds if (!file) 22891da177e4SLinus Torvalds continue; 229088e67f3bSDavid Howells if (file_has_perm(cred, 22911da177e4SLinus Torvalds file, 22921da177e4SLinus Torvalds file_to_av(file))) { 22931da177e4SLinus Torvalds sys_close(i); 22941da177e4SLinus Torvalds fd = get_unused_fd(); 22951da177e4SLinus Torvalds if (fd != i) { 22961da177e4SLinus Torvalds if (fd >= 0) 22971da177e4SLinus Torvalds put_unused_fd(fd); 22981da177e4SLinus Torvalds fput(file); 22991da177e4SLinus Torvalds continue; 23001da177e4SLinus Torvalds } 23011da177e4SLinus Torvalds if (devnull) { 2302095975daSNick Piggin get_file(devnull); 23031da177e4SLinus Torvalds } else { 2304745ca247SDavid Howells devnull = dentry_open( 2305745ca247SDavid Howells dget(selinux_null), 2306745ca247SDavid Howells mntget(selinuxfs_mount), 2307745ca247SDavid Howells O_RDWR, cred); 2308fc5d81e6SAkinobu Mita if (IS_ERR(devnull)) { 2309fc5d81e6SAkinobu Mita devnull = NULL; 23101da177e4SLinus Torvalds put_unused_fd(fd); 23111da177e4SLinus Torvalds fput(file); 23121da177e4SLinus Torvalds continue; 23131da177e4SLinus Torvalds } 23141da177e4SLinus Torvalds } 23151da177e4SLinus Torvalds fd_install(fd, devnull); 23161da177e4SLinus Torvalds } 23171da177e4SLinus Torvalds fput(file); 23181da177e4SLinus Torvalds } 23191da177e4SLinus Torvalds } 23201da177e4SLinus Torvalds spin_lock(&files->file_lock); 23211da177e4SLinus Torvalds 23221da177e4SLinus Torvalds } 23231da177e4SLinus Torvalds spin_unlock(&files->file_lock); 23241da177e4SLinus Torvalds } 23251da177e4SLinus Torvalds 23261da177e4SLinus Torvalds /* 2327a6f76f23SDavid Howells * Prepare a process for imminent new credential changes due to exec 23281da177e4SLinus Torvalds */ 2329a6f76f23SDavid Howells static void selinux_bprm_committing_creds(struct linux_binprm *bprm) 23301da177e4SLinus Torvalds { 2331a6f76f23SDavid Howells struct task_security_struct *new_tsec; 23321da177e4SLinus Torvalds struct rlimit *rlim, *initrlim; 23331da177e4SLinus Torvalds int rc, i; 23341da177e4SLinus Torvalds 2335a6f76f23SDavid Howells new_tsec = bprm->cred->security; 2336a6f76f23SDavid Howells if (new_tsec->sid == new_tsec->osid) 23371da177e4SLinus Torvalds return; 23381da177e4SLinus Torvalds 23391da177e4SLinus Torvalds /* Close files for which the new task SID is not authorized. */ 2340a6f76f23SDavid Howells flush_unauthorized_files(bprm->cred, current->files); 23411da177e4SLinus Torvalds 2342a6f76f23SDavid Howells /* Always clear parent death signal on SID transitions. */ 2343a6f76f23SDavid Howells current->pdeath_signal = 0; 2344a6f76f23SDavid Howells 2345a6f76f23SDavid Howells /* Check whether the new SID can inherit resource limits from the old 2346a6f76f23SDavid Howells * SID. If not, reset all soft limits to the lower of the current 2347a6f76f23SDavid Howells * task's hard limit and the init task's soft limit. 2348a6f76f23SDavid Howells * 2349a6f76f23SDavid Howells * Note that the setting of hard limits (even to lower them) can be 2350a6f76f23SDavid Howells * controlled by the setrlimit check. The inclusion of the init task's 2351a6f76f23SDavid Howells * soft limit into the computation is to avoid resetting soft limits 2352a6f76f23SDavid Howells * higher than the default soft limit for cases where the default is 2353a6f76f23SDavid Howells * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK. 2354a6f76f23SDavid Howells */ 2355a6f76f23SDavid Howells rc = avc_has_perm(new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS, 2356a6f76f23SDavid Howells PROCESS__RLIMITINH, NULL); 2357a6f76f23SDavid Howells if (rc) { 2358a6f76f23SDavid Howells for (i = 0; i < RLIM_NLIMITS; i++) { 2359a6f76f23SDavid Howells rlim = current->signal->rlim + i; 2360a6f76f23SDavid Howells initrlim = init_task.signal->rlim + i; 2361a6f76f23SDavid Howells rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur); 2362a6f76f23SDavid Howells } 236317740d89SJiri Slaby update_rlimit_cpu(current->signal->rlim[RLIMIT_CPU].rlim_cur); 2364a6f76f23SDavid Howells } 2365a6f76f23SDavid Howells } 2366a6f76f23SDavid Howells 2367a6f76f23SDavid Howells /* 2368a6f76f23SDavid Howells * Clean up the process immediately after the installation of new credentials 2369a6f76f23SDavid Howells * due to exec 2370a6f76f23SDavid Howells */ 2371a6f76f23SDavid Howells static void selinux_bprm_committed_creds(struct linux_binprm *bprm) 2372a6f76f23SDavid Howells { 2373a6f76f23SDavid Howells const struct task_security_struct *tsec = current_security(); 2374a6f76f23SDavid Howells struct itimerval itimer; 2375a6f76f23SDavid Howells u32 osid, sid; 2376a6f76f23SDavid Howells int rc, i; 2377a6f76f23SDavid Howells 2378a6f76f23SDavid Howells osid = tsec->osid; 2379a6f76f23SDavid Howells sid = tsec->sid; 2380a6f76f23SDavid Howells 2381a6f76f23SDavid Howells if (sid == osid) 2382a6f76f23SDavid Howells return; 2383a6f76f23SDavid Howells 2384a6f76f23SDavid Howells /* Check whether the new SID can inherit signal state from the old SID. 2385a6f76f23SDavid Howells * If not, clear itimers to avoid subsequent signal generation and 2386a6f76f23SDavid Howells * flush and unblock signals. 2387a6f76f23SDavid Howells * 2388a6f76f23SDavid Howells * This must occur _after_ the task SID has been updated so that any 2389a6f76f23SDavid Howells * kill done after the flush will be checked against the new SID. 2390a6f76f23SDavid Howells */ 2391a6f76f23SDavid Howells rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL); 23921da177e4SLinus Torvalds if (rc) { 23931da177e4SLinus Torvalds memset(&itimer, 0, sizeof itimer); 23941da177e4SLinus Torvalds for (i = 0; i < 3; i++) 23951da177e4SLinus Torvalds do_setitimer(i, &itimer, NULL); 23961da177e4SLinus Torvalds spin_lock_irq(¤t->sighand->siglock); 23973bcac026SDavid Howells if (!(current->signal->flags & SIGNAL_GROUP_EXIT)) { 23983bcac026SDavid Howells __flush_signals(current); 23991da177e4SLinus Torvalds flush_signal_handlers(current, 1); 24001da177e4SLinus Torvalds sigemptyset(¤t->blocked); 24013bcac026SDavid Howells } 24021da177e4SLinus Torvalds spin_unlock_irq(¤t->sighand->siglock); 24031da177e4SLinus Torvalds } 24041da177e4SLinus Torvalds 2405a6f76f23SDavid Howells /* Wake up the parent if it is waiting so that it can recheck 2406a6f76f23SDavid Howells * wait permission to the new task SID. */ 2407ecd6de3cSOleg Nesterov read_lock(&tasklist_lock); 24080b7570e7SOleg Nesterov __wake_up_parent(current, current->real_parent); 2409ecd6de3cSOleg Nesterov read_unlock(&tasklist_lock); 24101da177e4SLinus Torvalds } 24111da177e4SLinus Torvalds 24121da177e4SLinus Torvalds /* superblock security operations */ 24131da177e4SLinus Torvalds 24141da177e4SLinus Torvalds static int selinux_sb_alloc_security(struct super_block *sb) 24151da177e4SLinus Torvalds { 24161da177e4SLinus Torvalds return superblock_alloc_security(sb); 24171da177e4SLinus Torvalds } 24181da177e4SLinus Torvalds 24191da177e4SLinus Torvalds static void selinux_sb_free_security(struct super_block *sb) 24201da177e4SLinus Torvalds { 24211da177e4SLinus Torvalds superblock_free_security(sb); 24221da177e4SLinus Torvalds } 24231da177e4SLinus Torvalds 24241da177e4SLinus Torvalds static inline int match_prefix(char *prefix, int plen, char *option, int olen) 24251da177e4SLinus Torvalds { 24261da177e4SLinus Torvalds if (plen > olen) 24271da177e4SLinus Torvalds return 0; 24281da177e4SLinus Torvalds 24291da177e4SLinus Torvalds return !memcmp(prefix, option, plen); 24301da177e4SLinus Torvalds } 24311da177e4SLinus Torvalds 24321da177e4SLinus Torvalds static inline int selinux_option(char *option, int len) 24331da177e4SLinus Torvalds { 2434832cbd9aSEric Paris return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) || 2435832cbd9aSEric Paris match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) || 2436832cbd9aSEric Paris match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) || 243711689d47SDavid P. Quigley match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) || 243811689d47SDavid P. Quigley match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len)); 24391da177e4SLinus Torvalds } 24401da177e4SLinus Torvalds 24411da177e4SLinus Torvalds static inline void take_option(char **to, char *from, int *first, int len) 24421da177e4SLinus Torvalds { 24431da177e4SLinus Torvalds if (!*first) { 24441da177e4SLinus Torvalds **to = ','; 24451da177e4SLinus Torvalds *to += 1; 24463528a953SCory Olmo } else 24471da177e4SLinus Torvalds *first = 0; 24481da177e4SLinus Torvalds memcpy(*to, from, len); 24491da177e4SLinus Torvalds *to += len; 24501da177e4SLinus Torvalds } 24511da177e4SLinus Torvalds 24523528a953SCory Olmo static inline void take_selinux_option(char **to, char *from, int *first, 24533528a953SCory Olmo int len) 24543528a953SCory Olmo { 24553528a953SCory Olmo int current_size = 0; 24563528a953SCory Olmo 24573528a953SCory Olmo if (!*first) { 24583528a953SCory Olmo **to = '|'; 24593528a953SCory Olmo *to += 1; 2460828dfe1dSEric Paris } else 24613528a953SCory Olmo *first = 0; 24623528a953SCory Olmo 24633528a953SCory Olmo while (current_size < len) { 24643528a953SCory Olmo if (*from != '"') { 24653528a953SCory Olmo **to = *from; 24663528a953SCory Olmo *to += 1; 24673528a953SCory Olmo } 24683528a953SCory Olmo from += 1; 24693528a953SCory Olmo current_size += 1; 24703528a953SCory Olmo } 24713528a953SCory Olmo } 24723528a953SCory Olmo 2473e0007529SEric Paris static int selinux_sb_copy_data(char *orig, char *copy) 24741da177e4SLinus Torvalds { 24751da177e4SLinus Torvalds int fnosec, fsec, rc = 0; 24761da177e4SLinus Torvalds char *in_save, *in_curr, *in_end; 24771da177e4SLinus Torvalds char *sec_curr, *nosec_save, *nosec; 24783528a953SCory Olmo int open_quote = 0; 24791da177e4SLinus Torvalds 24801da177e4SLinus Torvalds in_curr = orig; 24811da177e4SLinus Torvalds sec_curr = copy; 24821da177e4SLinus Torvalds 24831da177e4SLinus Torvalds nosec = (char *)get_zeroed_page(GFP_KERNEL); 24841da177e4SLinus Torvalds if (!nosec) { 24851da177e4SLinus Torvalds rc = -ENOMEM; 24861da177e4SLinus Torvalds goto out; 24871da177e4SLinus Torvalds } 24881da177e4SLinus Torvalds 24891da177e4SLinus Torvalds nosec_save = nosec; 24901da177e4SLinus Torvalds fnosec = fsec = 1; 24911da177e4SLinus Torvalds in_save = in_end = orig; 24921da177e4SLinus Torvalds 24931da177e4SLinus Torvalds do { 24943528a953SCory Olmo if (*in_end == '"') 24953528a953SCory Olmo open_quote = !open_quote; 24963528a953SCory Olmo if ((*in_end == ',' && open_quote == 0) || 24973528a953SCory Olmo *in_end == '\0') { 24981da177e4SLinus Torvalds int len = in_end - in_curr; 24991da177e4SLinus Torvalds 25001da177e4SLinus Torvalds if (selinux_option(in_curr, len)) 25013528a953SCory Olmo take_selinux_option(&sec_curr, in_curr, &fsec, len); 25021da177e4SLinus Torvalds else 25031da177e4SLinus Torvalds take_option(&nosec, in_curr, &fnosec, len); 25041da177e4SLinus Torvalds 25051da177e4SLinus Torvalds in_curr = in_end + 1; 25061da177e4SLinus Torvalds } 25071da177e4SLinus Torvalds } while (*in_end++); 25081da177e4SLinus Torvalds 25096931dfc9SEric Paris strcpy(in_save, nosec_save); 2510da3caa20SGerald Schaefer free_page((unsigned long)nosec_save); 25111da177e4SLinus Torvalds out: 25121da177e4SLinus Torvalds return rc; 25131da177e4SLinus Torvalds } 25141da177e4SLinus Torvalds 251512204e24SJames Morris static int selinux_sb_kern_mount(struct super_block *sb, int flags, void *data) 25161da177e4SLinus Torvalds { 251788e67f3bSDavid Howells const struct cred *cred = current_cred(); 25182bf49690SThomas Liu struct common_audit_data ad; 25191da177e4SLinus Torvalds int rc; 25201da177e4SLinus Torvalds 25211da177e4SLinus Torvalds rc = superblock_doinit(sb, data); 25221da177e4SLinus Torvalds if (rc) 25231da177e4SLinus Torvalds return rc; 25241da177e4SLinus Torvalds 252574192246SJames Morris /* Allow all mounts performed by the kernel */ 252674192246SJames Morris if (flags & MS_KERNMOUNT) 252774192246SJames Morris return 0; 252874192246SJames Morris 25292bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, FS); 253044707fdfSJan Blunck ad.u.fs.path.dentry = sb->s_root; 253188e67f3bSDavid Howells return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad); 25321da177e4SLinus Torvalds } 25331da177e4SLinus Torvalds 2534726c3342SDavid Howells static int selinux_sb_statfs(struct dentry *dentry) 25351da177e4SLinus Torvalds { 253688e67f3bSDavid Howells const struct cred *cred = current_cred(); 25372bf49690SThomas Liu struct common_audit_data ad; 25381da177e4SLinus Torvalds 25392bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, FS); 254044707fdfSJan Blunck ad.u.fs.path.dentry = dentry->d_sb->s_root; 254188e67f3bSDavid Howells return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad); 25421da177e4SLinus Torvalds } 25431da177e4SLinus Torvalds 25441da177e4SLinus Torvalds static int selinux_mount(char *dev_name, 2545b5266eb4SAl Viro struct path *path, 25461da177e4SLinus Torvalds char *type, 25471da177e4SLinus Torvalds unsigned long flags, 25481da177e4SLinus Torvalds void *data) 25491da177e4SLinus Torvalds { 255088e67f3bSDavid Howells const struct cred *cred = current_cred(); 25511da177e4SLinus Torvalds 25521da177e4SLinus Torvalds if (flags & MS_REMOUNT) 255388e67f3bSDavid Howells return superblock_has_perm(cred, path->mnt->mnt_sb, 25541da177e4SLinus Torvalds FILESYSTEM__REMOUNT, NULL); 25551da177e4SLinus Torvalds else 255688e67f3bSDavid Howells return dentry_has_perm(cred, path->mnt, path->dentry, 25571da177e4SLinus Torvalds FILE__MOUNTON); 25581da177e4SLinus Torvalds } 25591da177e4SLinus Torvalds 25601da177e4SLinus Torvalds static int selinux_umount(struct vfsmount *mnt, int flags) 25611da177e4SLinus Torvalds { 256288e67f3bSDavid Howells const struct cred *cred = current_cred(); 25631da177e4SLinus Torvalds 256488e67f3bSDavid Howells return superblock_has_perm(cred, mnt->mnt_sb, 25651da177e4SLinus Torvalds FILESYSTEM__UNMOUNT, NULL); 25661da177e4SLinus Torvalds } 25671da177e4SLinus Torvalds 25681da177e4SLinus Torvalds /* inode security operations */ 25691da177e4SLinus Torvalds 25701da177e4SLinus Torvalds static int selinux_inode_alloc_security(struct inode *inode) 25711da177e4SLinus Torvalds { 25721da177e4SLinus Torvalds return inode_alloc_security(inode); 25731da177e4SLinus Torvalds } 25741da177e4SLinus Torvalds 25751da177e4SLinus Torvalds static void selinux_inode_free_security(struct inode *inode) 25761da177e4SLinus Torvalds { 25771da177e4SLinus Torvalds inode_free_security(inode); 25781da177e4SLinus Torvalds } 25791da177e4SLinus Torvalds 25805e41ff9eSStephen Smalley static int selinux_inode_init_security(struct inode *inode, struct inode *dir, 25815e41ff9eSStephen Smalley char **name, void **value, 25825e41ff9eSStephen Smalley size_t *len) 25835e41ff9eSStephen Smalley { 2584275bb41eSDavid Howells const struct cred *cred = current_cred(); 2585275bb41eSDavid Howells const struct task_security_struct *tsec = cred->security; 25865e41ff9eSStephen Smalley struct inode_security_struct *dsec; 25875e41ff9eSStephen Smalley struct superblock_security_struct *sbsec; 2588275bb41eSDavid Howells u32 sid, newsid, clen; 25895e41ff9eSStephen Smalley int rc; 2590570bc1c2SStephen Smalley char *namep = NULL, *context; 25915e41ff9eSStephen Smalley 25925e41ff9eSStephen Smalley dsec = dir->i_security; 25935e41ff9eSStephen Smalley sbsec = dir->i_sb->s_security; 25945e41ff9eSStephen Smalley 2595275bb41eSDavid Howells sid = tsec->sid; 25965e41ff9eSStephen Smalley newsid = tsec->create_sid; 2597275bb41eSDavid Howells 2598cd89596fSDavid P. Quigley if (!newsid || !(sbsec->flags & SE_SBLABELSUPP)) { 2599275bb41eSDavid Howells rc = security_transition_sid(sid, dsec->sid, 26005e41ff9eSStephen Smalley inode_mode_to_security_class(inode->i_mode), 26015e41ff9eSStephen Smalley &newsid); 26025e41ff9eSStephen Smalley if (rc) { 26035e41ff9eSStephen Smalley printk(KERN_WARNING "%s: " 26045e41ff9eSStephen Smalley "security_transition_sid failed, rc=%d (dev=%s " 26055e41ff9eSStephen Smalley "ino=%ld)\n", 2606dd6f953aSHarvey Harrison __func__, 26075e41ff9eSStephen Smalley -rc, inode->i_sb->s_id, inode->i_ino); 26085e41ff9eSStephen Smalley return rc; 26095e41ff9eSStephen Smalley } 26105e41ff9eSStephen Smalley } 26115e41ff9eSStephen Smalley 2612296fddf7SEric Paris /* Possibly defer initialization to selinux_complete_init. */ 26130d90a7ecSDavid P. Quigley if (sbsec->flags & SE_SBINITIALIZED) { 2614296fddf7SEric Paris struct inode_security_struct *isec = inode->i_security; 2615296fddf7SEric Paris isec->sclass = inode_mode_to_security_class(inode->i_mode); 2616296fddf7SEric Paris isec->sid = newsid; 2617296fddf7SEric Paris isec->initialized = 1; 2618296fddf7SEric Paris } 26195e41ff9eSStephen Smalley 2620cd89596fSDavid P. Quigley if (!ss_initialized || !(sbsec->flags & SE_SBLABELSUPP)) 262125a74f3bSStephen Smalley return -EOPNOTSUPP; 262225a74f3bSStephen Smalley 2623570bc1c2SStephen Smalley if (name) { 2624a02fe132SJosef Bacik namep = kstrdup(XATTR_SELINUX_SUFFIX, GFP_NOFS); 26255e41ff9eSStephen Smalley if (!namep) 26265e41ff9eSStephen Smalley return -ENOMEM; 26275e41ff9eSStephen Smalley *name = namep; 2628570bc1c2SStephen Smalley } 26295e41ff9eSStephen Smalley 2630570bc1c2SStephen Smalley if (value && len) { 263112b29f34SStephen Smalley rc = security_sid_to_context_force(newsid, &context, &clen); 26325e41ff9eSStephen Smalley if (rc) { 26335e41ff9eSStephen Smalley kfree(namep); 26345e41ff9eSStephen Smalley return rc; 26355e41ff9eSStephen Smalley } 26365e41ff9eSStephen Smalley *value = context; 2637570bc1c2SStephen Smalley *len = clen; 2638570bc1c2SStephen Smalley } 26395e41ff9eSStephen Smalley 26405e41ff9eSStephen Smalley return 0; 26415e41ff9eSStephen Smalley } 26425e41ff9eSStephen Smalley 26431da177e4SLinus Torvalds static int selinux_inode_create(struct inode *dir, struct dentry *dentry, int mask) 26441da177e4SLinus Torvalds { 26451da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_FILE); 26461da177e4SLinus Torvalds } 26471da177e4SLinus Torvalds 26481da177e4SLinus Torvalds static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 26491da177e4SLinus Torvalds { 26501da177e4SLinus Torvalds return may_link(dir, old_dentry, MAY_LINK); 26511da177e4SLinus Torvalds } 26521da177e4SLinus Torvalds 26531da177e4SLinus Torvalds static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry) 26541da177e4SLinus Torvalds { 26551da177e4SLinus Torvalds return may_link(dir, dentry, MAY_UNLINK); 26561da177e4SLinus Torvalds } 26571da177e4SLinus Torvalds 26581da177e4SLinus Torvalds static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name) 26591da177e4SLinus Torvalds { 26601da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_LNK_FILE); 26611da177e4SLinus Torvalds } 26621da177e4SLinus Torvalds 26631da177e4SLinus Torvalds static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, int mask) 26641da177e4SLinus Torvalds { 26651da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_DIR); 26661da177e4SLinus Torvalds } 26671da177e4SLinus Torvalds 26681da177e4SLinus Torvalds static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry) 26691da177e4SLinus Torvalds { 26701da177e4SLinus Torvalds return may_link(dir, dentry, MAY_RMDIR); 26711da177e4SLinus Torvalds } 26721da177e4SLinus Torvalds 26731da177e4SLinus Torvalds static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 26741da177e4SLinus Torvalds { 26751da177e4SLinus Torvalds return may_create(dir, dentry, inode_mode_to_security_class(mode)); 26761da177e4SLinus Torvalds } 26771da177e4SLinus Torvalds 26781da177e4SLinus Torvalds static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry, 26791da177e4SLinus Torvalds struct inode *new_inode, struct dentry *new_dentry) 26801da177e4SLinus Torvalds { 26811da177e4SLinus Torvalds return may_rename(old_inode, old_dentry, new_inode, new_dentry); 26821da177e4SLinus Torvalds } 26831da177e4SLinus Torvalds 26841da177e4SLinus Torvalds static int selinux_inode_readlink(struct dentry *dentry) 26851da177e4SLinus Torvalds { 268688e67f3bSDavid Howells const struct cred *cred = current_cred(); 268788e67f3bSDavid Howells 268888e67f3bSDavid Howells return dentry_has_perm(cred, NULL, dentry, FILE__READ); 26891da177e4SLinus Torvalds } 26901da177e4SLinus Torvalds 26911da177e4SLinus Torvalds static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata) 26921da177e4SLinus Torvalds { 269388e67f3bSDavid Howells const struct cred *cred = current_cred(); 26941da177e4SLinus Torvalds 269588e67f3bSDavid Howells return dentry_has_perm(cred, NULL, dentry, FILE__READ); 26961da177e4SLinus Torvalds } 26971da177e4SLinus Torvalds 2698b77b0646SAl Viro static int selinux_inode_permission(struct inode *inode, int mask) 26991da177e4SLinus Torvalds { 270088e67f3bSDavid Howells const struct cred *cred = current_cred(); 27011da177e4SLinus Torvalds 27021da177e4SLinus Torvalds if (!mask) { 27031da177e4SLinus Torvalds /* No permission to check. Existence test. */ 27041da177e4SLinus Torvalds return 0; 27051da177e4SLinus Torvalds } 27061da177e4SLinus Torvalds 270788e67f3bSDavid Howells return inode_has_perm(cred, inode, 27088b6a5a37SEric Paris file_mask_to_av(inode->i_mode, mask), NULL); 27091da177e4SLinus Torvalds } 27101da177e4SLinus Torvalds 27111da177e4SLinus Torvalds static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr) 27121da177e4SLinus Torvalds { 271388e67f3bSDavid Howells const struct cred *cred = current_cred(); 2714bc6a6008SAmerigo Wang unsigned int ia_valid = iattr->ia_valid; 27151da177e4SLinus Torvalds 2716bc6a6008SAmerigo Wang /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */ 2717bc6a6008SAmerigo Wang if (ia_valid & ATTR_FORCE) { 2718bc6a6008SAmerigo Wang ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE | 2719bc6a6008SAmerigo Wang ATTR_FORCE); 2720bc6a6008SAmerigo Wang if (!ia_valid) 27211da177e4SLinus Torvalds return 0; 2722bc6a6008SAmerigo Wang } 27231da177e4SLinus Torvalds 2724bc6a6008SAmerigo Wang if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | 2725bc6a6008SAmerigo Wang ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET)) 272688e67f3bSDavid Howells return dentry_has_perm(cred, NULL, dentry, FILE__SETATTR); 27271da177e4SLinus Torvalds 272888e67f3bSDavid Howells return dentry_has_perm(cred, NULL, dentry, FILE__WRITE); 27291da177e4SLinus Torvalds } 27301da177e4SLinus Torvalds 27311da177e4SLinus Torvalds static int selinux_inode_getattr(struct vfsmount *mnt, struct dentry *dentry) 27321da177e4SLinus Torvalds { 273388e67f3bSDavid Howells const struct cred *cred = current_cred(); 273488e67f3bSDavid Howells 273588e67f3bSDavid Howells return dentry_has_perm(cred, mnt, dentry, FILE__GETATTR); 27361da177e4SLinus Torvalds } 27371da177e4SLinus Torvalds 27388f0cfa52SDavid Howells static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name) 2739b5376771SSerge E. Hallyn { 274088e67f3bSDavid Howells const struct cred *cred = current_cred(); 274188e67f3bSDavid Howells 2742b5376771SSerge E. Hallyn if (!strncmp(name, XATTR_SECURITY_PREFIX, 2743b5376771SSerge E. Hallyn sizeof XATTR_SECURITY_PREFIX - 1)) { 2744b5376771SSerge E. Hallyn if (!strcmp(name, XATTR_NAME_CAPS)) { 2745b5376771SSerge E. Hallyn if (!capable(CAP_SETFCAP)) 2746b5376771SSerge E. Hallyn return -EPERM; 2747b5376771SSerge E. Hallyn } else if (!capable(CAP_SYS_ADMIN)) { 2748b5376771SSerge E. Hallyn /* A different attribute in the security namespace. 2749b5376771SSerge E. Hallyn Restrict to administrator. */ 2750b5376771SSerge E. Hallyn return -EPERM; 2751b5376771SSerge E. Hallyn } 2752b5376771SSerge E. Hallyn } 2753b5376771SSerge E. Hallyn 2754b5376771SSerge E. Hallyn /* Not an attribute we recognize, so just check the 2755b5376771SSerge E. Hallyn ordinary setattr permission. */ 275688e67f3bSDavid Howells return dentry_has_perm(cred, NULL, dentry, FILE__SETATTR); 2757b5376771SSerge E. Hallyn } 2758b5376771SSerge E. Hallyn 27598f0cfa52SDavid Howells static int selinux_inode_setxattr(struct dentry *dentry, const char *name, 27608f0cfa52SDavid Howells const void *value, size_t size, int flags) 27611da177e4SLinus Torvalds { 27621da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 27631da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 27641da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 27652bf49690SThomas Liu struct common_audit_data ad; 2766275bb41eSDavid Howells u32 newsid, sid = current_sid(); 27671da177e4SLinus Torvalds int rc = 0; 27681da177e4SLinus Torvalds 2769b5376771SSerge E. Hallyn if (strcmp(name, XATTR_NAME_SELINUX)) 2770b5376771SSerge E. Hallyn return selinux_inode_setotherxattr(dentry, name); 27711da177e4SLinus Torvalds 27721da177e4SLinus Torvalds sbsec = inode->i_sb->s_security; 2773cd89596fSDavid P. Quigley if (!(sbsec->flags & SE_SBLABELSUPP)) 27741da177e4SLinus Torvalds return -EOPNOTSUPP; 27751da177e4SLinus Torvalds 27763bd858abSSatyam Sharma if (!is_owner_or_cap(inode)) 27771da177e4SLinus Torvalds return -EPERM; 27781da177e4SLinus Torvalds 27792bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, FS); 278044707fdfSJan Blunck ad.u.fs.path.dentry = dentry; 27811da177e4SLinus Torvalds 2782275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, isec->sclass, 27831da177e4SLinus Torvalds FILE__RELABELFROM, &ad); 27841da177e4SLinus Torvalds if (rc) 27851da177e4SLinus Torvalds return rc; 27861da177e4SLinus Torvalds 27871da177e4SLinus Torvalds rc = security_context_to_sid(value, size, &newsid); 278812b29f34SStephen Smalley if (rc == -EINVAL) { 278912b29f34SStephen Smalley if (!capable(CAP_MAC_ADMIN)) 279012b29f34SStephen Smalley return rc; 279112b29f34SStephen Smalley rc = security_context_to_sid_force(value, size, &newsid); 279212b29f34SStephen Smalley } 27931da177e4SLinus Torvalds if (rc) 27941da177e4SLinus Torvalds return rc; 27951da177e4SLinus Torvalds 2796275bb41eSDavid Howells rc = avc_has_perm(sid, newsid, isec->sclass, 27971da177e4SLinus Torvalds FILE__RELABELTO, &ad); 27981da177e4SLinus Torvalds if (rc) 27991da177e4SLinus Torvalds return rc; 28001da177e4SLinus Torvalds 2801275bb41eSDavid Howells rc = security_validate_transition(isec->sid, newsid, sid, 28021da177e4SLinus Torvalds isec->sclass); 28031da177e4SLinus Torvalds if (rc) 28041da177e4SLinus Torvalds return rc; 28051da177e4SLinus Torvalds 28061da177e4SLinus Torvalds return avc_has_perm(newsid, 28071da177e4SLinus Torvalds sbsec->sid, 28081da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 28091da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, 28101da177e4SLinus Torvalds &ad); 28111da177e4SLinus Torvalds } 28121da177e4SLinus Torvalds 28138f0cfa52SDavid Howells static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name, 28148f0cfa52SDavid Howells const void *value, size_t size, 28158f0cfa52SDavid Howells int flags) 28161da177e4SLinus Torvalds { 28171da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 28181da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 28191da177e4SLinus Torvalds u32 newsid; 28201da177e4SLinus Torvalds int rc; 28211da177e4SLinus Torvalds 28221da177e4SLinus Torvalds if (strcmp(name, XATTR_NAME_SELINUX)) { 28231da177e4SLinus Torvalds /* Not an attribute we recognize, so nothing to do. */ 28241da177e4SLinus Torvalds return; 28251da177e4SLinus Torvalds } 28261da177e4SLinus Torvalds 282712b29f34SStephen Smalley rc = security_context_to_sid_force(value, size, &newsid); 28281da177e4SLinus Torvalds if (rc) { 282912b29f34SStephen Smalley printk(KERN_ERR "SELinux: unable to map context to SID" 283012b29f34SStephen Smalley "for (%s, %lu), rc=%d\n", 283112b29f34SStephen Smalley inode->i_sb->s_id, inode->i_ino, -rc); 28321da177e4SLinus Torvalds return; 28331da177e4SLinus Torvalds } 28341da177e4SLinus Torvalds 28351da177e4SLinus Torvalds isec->sid = newsid; 28361da177e4SLinus Torvalds return; 28371da177e4SLinus Torvalds } 28381da177e4SLinus Torvalds 28398f0cfa52SDavid Howells static int selinux_inode_getxattr(struct dentry *dentry, const char *name) 28401da177e4SLinus Torvalds { 284188e67f3bSDavid Howells const struct cred *cred = current_cred(); 284288e67f3bSDavid Howells 284388e67f3bSDavid Howells return dentry_has_perm(cred, NULL, dentry, FILE__GETATTR); 28441da177e4SLinus Torvalds } 28451da177e4SLinus Torvalds 28461da177e4SLinus Torvalds static int selinux_inode_listxattr(struct dentry *dentry) 28471da177e4SLinus Torvalds { 284888e67f3bSDavid Howells const struct cred *cred = current_cred(); 284988e67f3bSDavid Howells 285088e67f3bSDavid Howells return dentry_has_perm(cred, NULL, dentry, FILE__GETATTR); 28511da177e4SLinus Torvalds } 28521da177e4SLinus Torvalds 28538f0cfa52SDavid Howells static int selinux_inode_removexattr(struct dentry *dentry, const char *name) 28541da177e4SLinus Torvalds { 2855b5376771SSerge E. Hallyn if (strcmp(name, XATTR_NAME_SELINUX)) 2856b5376771SSerge E. Hallyn return selinux_inode_setotherxattr(dentry, name); 28571da177e4SLinus Torvalds 28581da177e4SLinus Torvalds /* No one is allowed to remove a SELinux security label. 28591da177e4SLinus Torvalds You can change the label, but all data must be labeled. */ 28601da177e4SLinus Torvalds return -EACCES; 28611da177e4SLinus Torvalds } 28621da177e4SLinus Torvalds 2863d381d8a9SJames Morris /* 2864abc69bb6SStephen Smalley * Copy the inode security context value to the user. 2865d381d8a9SJames Morris * 2866d381d8a9SJames Morris * Permission check is handled by selinux_inode_getxattr hook. 2867d381d8a9SJames Morris */ 286842492594SDavid P. Quigley static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void **buffer, bool alloc) 28691da177e4SLinus Torvalds { 287042492594SDavid P. Quigley u32 size; 287142492594SDavid P. Quigley int error; 287242492594SDavid P. Quigley char *context = NULL; 28731da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 28741da177e4SLinus Torvalds 28758c8570fbSDustin Kirkland if (strcmp(name, XATTR_SELINUX_SUFFIX)) 28768c8570fbSDustin Kirkland return -EOPNOTSUPP; 28771da177e4SLinus Torvalds 2878abc69bb6SStephen Smalley /* 2879abc69bb6SStephen Smalley * If the caller has CAP_MAC_ADMIN, then get the raw context 2880abc69bb6SStephen Smalley * value even if it is not defined by current policy; otherwise, 2881abc69bb6SStephen Smalley * use the in-core value under current policy. 2882abc69bb6SStephen Smalley * Use the non-auditing forms of the permission checks since 2883abc69bb6SStephen Smalley * getxattr may be called by unprivileged processes commonly 2884abc69bb6SStephen Smalley * and lack of permission just means that we fall back to the 2885abc69bb6SStephen Smalley * in-core context value, not a denial. 2886abc69bb6SStephen Smalley */ 28873699c53cSDavid Howells error = selinux_capable(current, current_cred(), CAP_MAC_ADMIN, 28883699c53cSDavid Howells SECURITY_CAP_NOAUDIT); 2889abc69bb6SStephen Smalley if (!error) 2890abc69bb6SStephen Smalley error = security_sid_to_context_force(isec->sid, &context, 2891abc69bb6SStephen Smalley &size); 2892abc69bb6SStephen Smalley else 289342492594SDavid P. Quigley error = security_sid_to_context(isec->sid, &context, &size); 289442492594SDavid P. Quigley if (error) 289542492594SDavid P. Quigley return error; 289642492594SDavid P. Quigley error = size; 289742492594SDavid P. Quigley if (alloc) { 289842492594SDavid P. Quigley *buffer = context; 289942492594SDavid P. Quigley goto out_nofree; 290042492594SDavid P. Quigley } 290142492594SDavid P. Quigley kfree(context); 290242492594SDavid P. Quigley out_nofree: 290342492594SDavid P. Quigley return error; 29041da177e4SLinus Torvalds } 29051da177e4SLinus Torvalds 29061da177e4SLinus Torvalds static int selinux_inode_setsecurity(struct inode *inode, const char *name, 29071da177e4SLinus Torvalds const void *value, size_t size, int flags) 29081da177e4SLinus Torvalds { 29091da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 29101da177e4SLinus Torvalds u32 newsid; 29111da177e4SLinus Torvalds int rc; 29121da177e4SLinus Torvalds 29131da177e4SLinus Torvalds if (strcmp(name, XATTR_SELINUX_SUFFIX)) 29141da177e4SLinus Torvalds return -EOPNOTSUPP; 29151da177e4SLinus Torvalds 29161da177e4SLinus Torvalds if (!value || !size) 29171da177e4SLinus Torvalds return -EACCES; 29181da177e4SLinus Torvalds 29191da177e4SLinus Torvalds rc = security_context_to_sid((void *)value, size, &newsid); 29201da177e4SLinus Torvalds if (rc) 29211da177e4SLinus Torvalds return rc; 29221da177e4SLinus Torvalds 29231da177e4SLinus Torvalds isec->sid = newsid; 2924ddd29ec6SDavid P. Quigley isec->initialized = 1; 29251da177e4SLinus Torvalds return 0; 29261da177e4SLinus Torvalds } 29271da177e4SLinus Torvalds 29281da177e4SLinus Torvalds static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size) 29291da177e4SLinus Torvalds { 29301da177e4SLinus Torvalds const int len = sizeof(XATTR_NAME_SELINUX); 29311da177e4SLinus Torvalds if (buffer && len <= buffer_size) 29321da177e4SLinus Torvalds memcpy(buffer, XATTR_NAME_SELINUX, len); 29331da177e4SLinus Torvalds return len; 29341da177e4SLinus Torvalds } 29351da177e4SLinus Torvalds 2936713a04aeSAhmed S. Darwish static void selinux_inode_getsecid(const struct inode *inode, u32 *secid) 2937713a04aeSAhmed S. Darwish { 2938713a04aeSAhmed S. Darwish struct inode_security_struct *isec = inode->i_security; 2939713a04aeSAhmed S. Darwish *secid = isec->sid; 2940713a04aeSAhmed S. Darwish } 2941713a04aeSAhmed S. Darwish 29421da177e4SLinus Torvalds /* file security operations */ 29431da177e4SLinus Torvalds 2944788e7dd4SYuichi Nakamura static int selinux_revalidate_file_permission(struct file *file, int mask) 29451da177e4SLinus Torvalds { 294688e67f3bSDavid Howells const struct cred *cred = current_cred(); 29473d5ff529SJosef Sipek struct inode *inode = file->f_path.dentry->d_inode; 29481da177e4SLinus Torvalds 29491da177e4SLinus Torvalds /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */ 29501da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE)) 29511da177e4SLinus Torvalds mask |= MAY_APPEND; 29521da177e4SLinus Torvalds 2953389fb800SPaul Moore return file_has_perm(cred, file, 29541da177e4SLinus Torvalds file_mask_to_av(inode->i_mode, mask)); 29551da177e4SLinus Torvalds } 29561da177e4SLinus Torvalds 2957788e7dd4SYuichi Nakamura static int selinux_file_permission(struct file *file, int mask) 2958788e7dd4SYuichi Nakamura { 295920dda18bSStephen Smalley struct inode *inode = file->f_path.dentry->d_inode; 296020dda18bSStephen Smalley struct file_security_struct *fsec = file->f_security; 296120dda18bSStephen Smalley struct inode_security_struct *isec = inode->i_security; 296220dda18bSStephen Smalley u32 sid = current_sid(); 296320dda18bSStephen Smalley 2964389fb800SPaul Moore if (!mask) 2965788e7dd4SYuichi Nakamura /* No permission to check. Existence test. */ 2966788e7dd4SYuichi Nakamura return 0; 2967788e7dd4SYuichi Nakamura 296820dda18bSStephen Smalley if (sid == fsec->sid && fsec->isid == isec->sid && 296920dda18bSStephen Smalley fsec->pseqno == avc_policy_seqno()) 297020dda18bSStephen Smalley /* No change since dentry_open check. */ 297120dda18bSStephen Smalley return 0; 297220dda18bSStephen Smalley 2973788e7dd4SYuichi Nakamura return selinux_revalidate_file_permission(file, mask); 2974788e7dd4SYuichi Nakamura } 2975788e7dd4SYuichi Nakamura 29761da177e4SLinus Torvalds static int selinux_file_alloc_security(struct file *file) 29771da177e4SLinus Torvalds { 29781da177e4SLinus Torvalds return file_alloc_security(file); 29791da177e4SLinus Torvalds } 29801da177e4SLinus Torvalds 29811da177e4SLinus Torvalds static void selinux_file_free_security(struct file *file) 29821da177e4SLinus Torvalds { 29831da177e4SLinus Torvalds file_free_security(file); 29841da177e4SLinus Torvalds } 29851da177e4SLinus Torvalds 29861da177e4SLinus Torvalds static int selinux_file_ioctl(struct file *file, unsigned int cmd, 29871da177e4SLinus Torvalds unsigned long arg) 29881da177e4SLinus Torvalds { 298988e67f3bSDavid Howells const struct cred *cred = current_cred(); 2990242631c4SStephen Smalley u32 av = 0; 29911da177e4SLinus Torvalds 2992242631c4SStephen Smalley if (_IOC_DIR(cmd) & _IOC_WRITE) 2993242631c4SStephen Smalley av |= FILE__WRITE; 2994242631c4SStephen Smalley if (_IOC_DIR(cmd) & _IOC_READ) 2995242631c4SStephen Smalley av |= FILE__READ; 2996242631c4SStephen Smalley if (!av) 2997242631c4SStephen Smalley av = FILE__IOCTL; 29981da177e4SLinus Torvalds 299988e67f3bSDavid Howells return file_has_perm(cred, file, av); 30001da177e4SLinus Torvalds } 30011da177e4SLinus Torvalds 30021da177e4SLinus Torvalds static int file_map_prot_check(struct file *file, unsigned long prot, int shared) 30031da177e4SLinus Torvalds { 300488e67f3bSDavid Howells const struct cred *cred = current_cred(); 3005d84f4f99SDavid Howells int rc = 0; 300688e67f3bSDavid Howells 30071da177e4SLinus Torvalds #ifndef CONFIG_PPC32 30081da177e4SLinus Torvalds if ((prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) { 30091da177e4SLinus Torvalds /* 30101da177e4SLinus Torvalds * We are making executable an anonymous mapping or a 30111da177e4SLinus Torvalds * private file mapping that will also be writable. 30121da177e4SLinus Torvalds * This has an additional check. 30131da177e4SLinus Torvalds */ 3014d84f4f99SDavid Howells rc = cred_has_perm(cred, cred, PROCESS__EXECMEM); 30151da177e4SLinus Torvalds if (rc) 3016d84f4f99SDavid Howells goto error; 30171da177e4SLinus Torvalds } 30181da177e4SLinus Torvalds #endif 30191da177e4SLinus Torvalds 30201da177e4SLinus Torvalds if (file) { 30211da177e4SLinus Torvalds /* read access is always possible with a mapping */ 30221da177e4SLinus Torvalds u32 av = FILE__READ; 30231da177e4SLinus Torvalds 30241da177e4SLinus Torvalds /* write access only matters if the mapping is shared */ 30251da177e4SLinus Torvalds if (shared && (prot & PROT_WRITE)) 30261da177e4SLinus Torvalds av |= FILE__WRITE; 30271da177e4SLinus Torvalds 30281da177e4SLinus Torvalds if (prot & PROT_EXEC) 30291da177e4SLinus Torvalds av |= FILE__EXECUTE; 30301da177e4SLinus Torvalds 303188e67f3bSDavid Howells return file_has_perm(cred, file, av); 30321da177e4SLinus Torvalds } 3033d84f4f99SDavid Howells 3034d84f4f99SDavid Howells error: 3035d84f4f99SDavid Howells return rc; 30361da177e4SLinus Torvalds } 30371da177e4SLinus Torvalds 30381da177e4SLinus Torvalds static int selinux_file_mmap(struct file *file, unsigned long reqprot, 3039ed032189SEric Paris unsigned long prot, unsigned long flags, 3040ed032189SEric Paris unsigned long addr, unsigned long addr_only) 30411da177e4SLinus Torvalds { 3042ed032189SEric Paris int rc = 0; 3043275bb41eSDavid Howells u32 sid = current_sid(); 30441da177e4SLinus Torvalds 304584336d1aSEric Paris /* 304684336d1aSEric Paris * notice that we are intentionally putting the SELinux check before 304784336d1aSEric Paris * the secondary cap_file_mmap check. This is such a likely attempt 304884336d1aSEric Paris * at bad behaviour/exploit that we always want to get the AVC, even 304984336d1aSEric Paris * if DAC would have also denied the operation. 305084336d1aSEric Paris */ 3051a2551df7SEric Paris if (addr < CONFIG_LSM_MMAP_MIN_ADDR) { 3052ed032189SEric Paris rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT, 3053ed032189SEric Paris MEMPROTECT__MMAP_ZERO, NULL); 305484336d1aSEric Paris if (rc) 305584336d1aSEric Paris return rc; 305684336d1aSEric Paris } 305784336d1aSEric Paris 305884336d1aSEric Paris /* do DAC check on address space usage */ 305984336d1aSEric Paris rc = cap_file_mmap(file, reqprot, prot, flags, addr, addr_only); 3060ed032189SEric Paris if (rc || addr_only) 30611da177e4SLinus Torvalds return rc; 30621da177e4SLinus Torvalds 30631da177e4SLinus Torvalds if (selinux_checkreqprot) 30641da177e4SLinus Torvalds prot = reqprot; 30651da177e4SLinus Torvalds 30661da177e4SLinus Torvalds return file_map_prot_check(file, prot, 30671da177e4SLinus Torvalds (flags & MAP_TYPE) == MAP_SHARED); 30681da177e4SLinus Torvalds } 30691da177e4SLinus Torvalds 30701da177e4SLinus Torvalds static int selinux_file_mprotect(struct vm_area_struct *vma, 30711da177e4SLinus Torvalds unsigned long reqprot, 30721da177e4SLinus Torvalds unsigned long prot) 30731da177e4SLinus Torvalds { 307488e67f3bSDavid Howells const struct cred *cred = current_cred(); 30751da177e4SLinus Torvalds 30761da177e4SLinus Torvalds if (selinux_checkreqprot) 30771da177e4SLinus Torvalds prot = reqprot; 30781da177e4SLinus Torvalds 30791da177e4SLinus Torvalds #ifndef CONFIG_PPC32 3080db4c9641SStephen Smalley if ((prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) { 3081d541bbeeSJames Morris int rc = 0; 3082db4c9641SStephen Smalley if (vma->vm_start >= vma->vm_mm->start_brk && 3083db4c9641SStephen Smalley vma->vm_end <= vma->vm_mm->brk) { 3084d84f4f99SDavid Howells rc = cred_has_perm(cred, cred, PROCESS__EXECHEAP); 3085db4c9641SStephen Smalley } else if (!vma->vm_file && 30866b992197SLorenzo Hernandez García-Hierro vma->vm_start <= vma->vm_mm->start_stack && 30876b992197SLorenzo Hernandez García-Hierro vma->vm_end >= vma->vm_mm->start_stack) { 30883b11a1deSDavid Howells rc = current_has_perm(current, PROCESS__EXECSTACK); 3089db4c9641SStephen Smalley } else if (vma->vm_file && vma->anon_vma) { 3090db4c9641SStephen Smalley /* 3091db4c9641SStephen Smalley * We are making executable a file mapping that has 3092db4c9641SStephen Smalley * had some COW done. Since pages might have been 3093db4c9641SStephen Smalley * written, check ability to execute the possibly 3094db4c9641SStephen Smalley * modified content. This typically should only 3095db4c9641SStephen Smalley * occur for text relocations. 3096db4c9641SStephen Smalley */ 3097d84f4f99SDavid Howells rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD); 3098db4c9641SStephen Smalley } 30996b992197SLorenzo Hernandez García-Hierro if (rc) 31006b992197SLorenzo Hernandez García-Hierro return rc; 31016b992197SLorenzo Hernandez García-Hierro } 31021da177e4SLinus Torvalds #endif 31031da177e4SLinus Torvalds 31041da177e4SLinus Torvalds return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED); 31051da177e4SLinus Torvalds } 31061da177e4SLinus Torvalds 31071da177e4SLinus Torvalds static int selinux_file_lock(struct file *file, unsigned int cmd) 31081da177e4SLinus Torvalds { 310988e67f3bSDavid Howells const struct cred *cred = current_cred(); 311088e67f3bSDavid Howells 311188e67f3bSDavid Howells return file_has_perm(cred, file, FILE__LOCK); 31121da177e4SLinus Torvalds } 31131da177e4SLinus Torvalds 31141da177e4SLinus Torvalds static int selinux_file_fcntl(struct file *file, unsigned int cmd, 31151da177e4SLinus Torvalds unsigned long arg) 31161da177e4SLinus Torvalds { 311788e67f3bSDavid Howells const struct cred *cred = current_cred(); 31181da177e4SLinus Torvalds int err = 0; 31191da177e4SLinus Torvalds 31201da177e4SLinus Torvalds switch (cmd) { 31211da177e4SLinus Torvalds case F_SETFL: 31223d5ff529SJosef Sipek if (!file->f_path.dentry || !file->f_path.dentry->d_inode) { 31231da177e4SLinus Torvalds err = -EINVAL; 31241da177e4SLinus Torvalds break; 31251da177e4SLinus Torvalds } 31261da177e4SLinus Torvalds 31271da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) { 312888e67f3bSDavid Howells err = file_has_perm(cred, file, FILE__WRITE); 31291da177e4SLinus Torvalds break; 31301da177e4SLinus Torvalds } 31311da177e4SLinus Torvalds /* fall through */ 31321da177e4SLinus Torvalds case F_SETOWN: 31331da177e4SLinus Torvalds case F_SETSIG: 31341da177e4SLinus Torvalds case F_GETFL: 31351da177e4SLinus Torvalds case F_GETOWN: 31361da177e4SLinus Torvalds case F_GETSIG: 31371da177e4SLinus Torvalds /* Just check FD__USE permission */ 313888e67f3bSDavid Howells err = file_has_perm(cred, file, 0); 31391da177e4SLinus Torvalds break; 31401da177e4SLinus Torvalds case F_GETLK: 31411da177e4SLinus Torvalds case F_SETLK: 31421da177e4SLinus Torvalds case F_SETLKW: 31431da177e4SLinus Torvalds #if BITS_PER_LONG == 32 31441da177e4SLinus Torvalds case F_GETLK64: 31451da177e4SLinus Torvalds case F_SETLK64: 31461da177e4SLinus Torvalds case F_SETLKW64: 31471da177e4SLinus Torvalds #endif 31483d5ff529SJosef Sipek if (!file->f_path.dentry || !file->f_path.dentry->d_inode) { 31491da177e4SLinus Torvalds err = -EINVAL; 31501da177e4SLinus Torvalds break; 31511da177e4SLinus Torvalds } 315288e67f3bSDavid Howells err = file_has_perm(cred, file, FILE__LOCK); 31531da177e4SLinus Torvalds break; 31541da177e4SLinus Torvalds } 31551da177e4SLinus Torvalds 31561da177e4SLinus Torvalds return err; 31571da177e4SLinus Torvalds } 31581da177e4SLinus Torvalds 31591da177e4SLinus Torvalds static int selinux_file_set_fowner(struct file *file) 31601da177e4SLinus Torvalds { 31611da177e4SLinus Torvalds struct file_security_struct *fsec; 31621da177e4SLinus Torvalds 31631da177e4SLinus Torvalds fsec = file->f_security; 3164275bb41eSDavid Howells fsec->fown_sid = current_sid(); 31651da177e4SLinus Torvalds 31661da177e4SLinus Torvalds return 0; 31671da177e4SLinus Torvalds } 31681da177e4SLinus Torvalds 31691da177e4SLinus Torvalds static int selinux_file_send_sigiotask(struct task_struct *tsk, 31701da177e4SLinus Torvalds struct fown_struct *fown, int signum) 31711da177e4SLinus Torvalds { 31721da177e4SLinus Torvalds struct file *file; 317365c90bcaSStephen Smalley u32 sid = task_sid(tsk); 31741da177e4SLinus Torvalds u32 perm; 31751da177e4SLinus Torvalds struct file_security_struct *fsec; 31761da177e4SLinus Torvalds 31771da177e4SLinus Torvalds /* struct fown_struct is never outside the context of a struct file */ 3178b385a144SRobert P. J. Day file = container_of(fown, struct file, f_owner); 31791da177e4SLinus Torvalds 31801da177e4SLinus Torvalds fsec = file->f_security; 31811da177e4SLinus Torvalds 31821da177e4SLinus Torvalds if (!signum) 31831da177e4SLinus Torvalds perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */ 31841da177e4SLinus Torvalds else 31851da177e4SLinus Torvalds perm = signal_to_av(signum); 31861da177e4SLinus Torvalds 3187275bb41eSDavid Howells return avc_has_perm(fsec->fown_sid, sid, 31881da177e4SLinus Torvalds SECCLASS_PROCESS, perm, NULL); 31891da177e4SLinus Torvalds } 31901da177e4SLinus Torvalds 31911da177e4SLinus Torvalds static int selinux_file_receive(struct file *file) 31921da177e4SLinus Torvalds { 319388e67f3bSDavid Howells const struct cred *cred = current_cred(); 319488e67f3bSDavid Howells 319588e67f3bSDavid Howells return file_has_perm(cred, file, file_to_av(file)); 31961da177e4SLinus Torvalds } 31971da177e4SLinus Torvalds 3198745ca247SDavid Howells static int selinux_dentry_open(struct file *file, const struct cred *cred) 3199788e7dd4SYuichi Nakamura { 3200788e7dd4SYuichi Nakamura struct file_security_struct *fsec; 3201788e7dd4SYuichi Nakamura struct inode *inode; 3202788e7dd4SYuichi Nakamura struct inode_security_struct *isec; 3203d84f4f99SDavid Howells 3204788e7dd4SYuichi Nakamura inode = file->f_path.dentry->d_inode; 3205788e7dd4SYuichi Nakamura fsec = file->f_security; 3206788e7dd4SYuichi Nakamura isec = inode->i_security; 3207788e7dd4SYuichi Nakamura /* 3208788e7dd4SYuichi Nakamura * Save inode label and policy sequence number 3209788e7dd4SYuichi Nakamura * at open-time so that selinux_file_permission 3210788e7dd4SYuichi Nakamura * can determine whether revalidation is necessary. 3211788e7dd4SYuichi Nakamura * Task label is already saved in the file security 3212788e7dd4SYuichi Nakamura * struct as its SID. 3213788e7dd4SYuichi Nakamura */ 3214788e7dd4SYuichi Nakamura fsec->isid = isec->sid; 3215788e7dd4SYuichi Nakamura fsec->pseqno = avc_policy_seqno(); 3216788e7dd4SYuichi Nakamura /* 3217788e7dd4SYuichi Nakamura * Since the inode label or policy seqno may have changed 3218788e7dd4SYuichi Nakamura * between the selinux_inode_permission check and the saving 3219788e7dd4SYuichi Nakamura * of state above, recheck that access is still permitted. 3220788e7dd4SYuichi Nakamura * Otherwise, access might never be revalidated against the 3221788e7dd4SYuichi Nakamura * new inode label or new policy. 3222788e7dd4SYuichi Nakamura * This check is not redundant - do not remove. 3223788e7dd4SYuichi Nakamura */ 322488e67f3bSDavid Howells return inode_has_perm(cred, inode, open_file_to_av(file), NULL); 3225788e7dd4SYuichi Nakamura } 3226788e7dd4SYuichi Nakamura 32271da177e4SLinus Torvalds /* task security operations */ 32281da177e4SLinus Torvalds 32291da177e4SLinus Torvalds static int selinux_task_create(unsigned long clone_flags) 32301da177e4SLinus Torvalds { 32313b11a1deSDavid Howells return current_has_perm(current, PROCESS__FORK); 32321da177e4SLinus Torvalds } 32331da177e4SLinus Torvalds 3234f1752eecSDavid Howells /* 3235ee18d64cSDavid Howells * allocate the SELinux part of blank credentials 3236ee18d64cSDavid Howells */ 3237ee18d64cSDavid Howells static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp) 3238ee18d64cSDavid Howells { 3239ee18d64cSDavid Howells struct task_security_struct *tsec; 3240ee18d64cSDavid Howells 3241ee18d64cSDavid Howells tsec = kzalloc(sizeof(struct task_security_struct), gfp); 3242ee18d64cSDavid Howells if (!tsec) 3243ee18d64cSDavid Howells return -ENOMEM; 3244ee18d64cSDavid Howells 3245ee18d64cSDavid Howells cred->security = tsec; 3246ee18d64cSDavid Howells return 0; 3247ee18d64cSDavid Howells } 3248ee18d64cSDavid Howells 3249ee18d64cSDavid Howells /* 3250f1752eecSDavid Howells * detach and free the LSM part of a set of credentials 3251f1752eecSDavid Howells */ 3252f1752eecSDavid Howells static void selinux_cred_free(struct cred *cred) 32531da177e4SLinus Torvalds { 3254f1752eecSDavid Howells struct task_security_struct *tsec = cred->security; 3255e0e81739SDavid Howells 3256e0e81739SDavid Howells BUG_ON((unsigned long) cred->security < PAGE_SIZE); 3257e0e81739SDavid Howells cred->security = (void *) 0x7UL; 3258f1752eecSDavid Howells kfree(tsec); 32591da177e4SLinus Torvalds } 32601da177e4SLinus Torvalds 3261d84f4f99SDavid Howells /* 3262d84f4f99SDavid Howells * prepare a new set of credentials for modification 3263d84f4f99SDavid Howells */ 3264d84f4f99SDavid Howells static int selinux_cred_prepare(struct cred *new, const struct cred *old, 3265d84f4f99SDavid Howells gfp_t gfp) 3266d84f4f99SDavid Howells { 3267d84f4f99SDavid Howells const struct task_security_struct *old_tsec; 3268d84f4f99SDavid Howells struct task_security_struct *tsec; 3269d84f4f99SDavid Howells 3270d84f4f99SDavid Howells old_tsec = old->security; 3271d84f4f99SDavid Howells 3272d84f4f99SDavid Howells tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp); 3273d84f4f99SDavid Howells if (!tsec) 3274d84f4f99SDavid Howells return -ENOMEM; 3275d84f4f99SDavid Howells 3276d84f4f99SDavid Howells new->security = tsec; 3277d84f4f99SDavid Howells return 0; 3278d84f4f99SDavid Howells } 3279d84f4f99SDavid Howells 3280d84f4f99SDavid Howells /* 3281ee18d64cSDavid Howells * transfer the SELinux data to a blank set of creds 3282ee18d64cSDavid Howells */ 3283ee18d64cSDavid Howells static void selinux_cred_transfer(struct cred *new, const struct cred *old) 3284ee18d64cSDavid Howells { 3285ee18d64cSDavid Howells const struct task_security_struct *old_tsec = old->security; 3286ee18d64cSDavid Howells struct task_security_struct *tsec = new->security; 3287ee18d64cSDavid Howells 3288ee18d64cSDavid Howells *tsec = *old_tsec; 3289ee18d64cSDavid Howells } 3290ee18d64cSDavid Howells 3291ee18d64cSDavid Howells /* 32923a3b7ce9SDavid Howells * set the security data for a kernel service 32933a3b7ce9SDavid Howells * - all the creation contexts are set to unlabelled 32943a3b7ce9SDavid Howells */ 32953a3b7ce9SDavid Howells static int selinux_kernel_act_as(struct cred *new, u32 secid) 32963a3b7ce9SDavid Howells { 32973a3b7ce9SDavid Howells struct task_security_struct *tsec = new->security; 32983a3b7ce9SDavid Howells u32 sid = current_sid(); 32993a3b7ce9SDavid Howells int ret; 33003a3b7ce9SDavid Howells 33013a3b7ce9SDavid Howells ret = avc_has_perm(sid, secid, 33023a3b7ce9SDavid Howells SECCLASS_KERNEL_SERVICE, 33033a3b7ce9SDavid Howells KERNEL_SERVICE__USE_AS_OVERRIDE, 33043a3b7ce9SDavid Howells NULL); 33053a3b7ce9SDavid Howells if (ret == 0) { 33063a3b7ce9SDavid Howells tsec->sid = secid; 33073a3b7ce9SDavid Howells tsec->create_sid = 0; 33083a3b7ce9SDavid Howells tsec->keycreate_sid = 0; 33093a3b7ce9SDavid Howells tsec->sockcreate_sid = 0; 33103a3b7ce9SDavid Howells } 33113a3b7ce9SDavid Howells return ret; 33123a3b7ce9SDavid Howells } 33133a3b7ce9SDavid Howells 33143a3b7ce9SDavid Howells /* 33153a3b7ce9SDavid Howells * set the file creation context in a security record to the same as the 33163a3b7ce9SDavid Howells * objective context of the specified inode 33173a3b7ce9SDavid Howells */ 33183a3b7ce9SDavid Howells static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode) 33193a3b7ce9SDavid Howells { 33203a3b7ce9SDavid Howells struct inode_security_struct *isec = inode->i_security; 33213a3b7ce9SDavid Howells struct task_security_struct *tsec = new->security; 33223a3b7ce9SDavid Howells u32 sid = current_sid(); 33233a3b7ce9SDavid Howells int ret; 33243a3b7ce9SDavid Howells 33253a3b7ce9SDavid Howells ret = avc_has_perm(sid, isec->sid, 33263a3b7ce9SDavid Howells SECCLASS_KERNEL_SERVICE, 33273a3b7ce9SDavid Howells KERNEL_SERVICE__CREATE_FILES_AS, 33283a3b7ce9SDavid Howells NULL); 33293a3b7ce9SDavid Howells 33303a3b7ce9SDavid Howells if (ret == 0) 33313a3b7ce9SDavid Howells tsec->create_sid = isec->sid; 33323a3b7ce9SDavid Howells return 0; 33333a3b7ce9SDavid Howells } 33343a3b7ce9SDavid Howells 3335dd8dbf2eSEric Paris static int selinux_kernel_module_request(char *kmod_name) 333625354c4fSEric Paris { 3337dd8dbf2eSEric Paris u32 sid; 3338dd8dbf2eSEric Paris struct common_audit_data ad; 3339dd8dbf2eSEric Paris 3340dd8dbf2eSEric Paris sid = task_sid(current); 3341dd8dbf2eSEric Paris 3342dd8dbf2eSEric Paris COMMON_AUDIT_DATA_INIT(&ad, KMOD); 3343dd8dbf2eSEric Paris ad.u.kmod_name = kmod_name; 3344dd8dbf2eSEric Paris 3345dd8dbf2eSEric Paris return avc_has_perm(sid, SECINITSID_KERNEL, SECCLASS_SYSTEM, 3346dd8dbf2eSEric Paris SYSTEM__MODULE_REQUEST, &ad); 334725354c4fSEric Paris } 334825354c4fSEric Paris 33491da177e4SLinus Torvalds static int selinux_task_setpgid(struct task_struct *p, pid_t pgid) 33501da177e4SLinus Torvalds { 33513b11a1deSDavid Howells return current_has_perm(p, PROCESS__SETPGID); 33521da177e4SLinus Torvalds } 33531da177e4SLinus Torvalds 33541da177e4SLinus Torvalds static int selinux_task_getpgid(struct task_struct *p) 33551da177e4SLinus Torvalds { 33563b11a1deSDavid Howells return current_has_perm(p, PROCESS__GETPGID); 33571da177e4SLinus Torvalds } 33581da177e4SLinus Torvalds 33591da177e4SLinus Torvalds static int selinux_task_getsid(struct task_struct *p) 33601da177e4SLinus Torvalds { 33613b11a1deSDavid Howells return current_has_perm(p, PROCESS__GETSESSION); 33621da177e4SLinus Torvalds } 33631da177e4SLinus Torvalds 3364f9008e4cSDavid Quigley static void selinux_task_getsecid(struct task_struct *p, u32 *secid) 3365f9008e4cSDavid Quigley { 3366275bb41eSDavid Howells *secid = task_sid(p); 3367f9008e4cSDavid Quigley } 3368f9008e4cSDavid Quigley 33691da177e4SLinus Torvalds static int selinux_task_setnice(struct task_struct *p, int nice) 33701da177e4SLinus Torvalds { 33711da177e4SLinus Torvalds int rc; 33721da177e4SLinus Torvalds 3373200ac532SEric Paris rc = cap_task_setnice(p, nice); 33741da177e4SLinus Torvalds if (rc) 33751da177e4SLinus Torvalds return rc; 33761da177e4SLinus Torvalds 33773b11a1deSDavid Howells return current_has_perm(p, PROCESS__SETSCHED); 33781da177e4SLinus Torvalds } 33791da177e4SLinus Torvalds 338003e68060SJames Morris static int selinux_task_setioprio(struct task_struct *p, int ioprio) 338103e68060SJames Morris { 3382b5376771SSerge E. Hallyn int rc; 3383b5376771SSerge E. Hallyn 3384200ac532SEric Paris rc = cap_task_setioprio(p, ioprio); 3385b5376771SSerge E. Hallyn if (rc) 3386b5376771SSerge E. Hallyn return rc; 3387b5376771SSerge E. Hallyn 33883b11a1deSDavid Howells return current_has_perm(p, PROCESS__SETSCHED); 338903e68060SJames Morris } 339003e68060SJames Morris 3391a1836a42SDavid Quigley static int selinux_task_getioprio(struct task_struct *p) 3392a1836a42SDavid Quigley { 33933b11a1deSDavid Howells return current_has_perm(p, PROCESS__GETSCHED); 3394a1836a42SDavid Quigley } 3395a1836a42SDavid Quigley 33961da177e4SLinus Torvalds static int selinux_task_setrlimit(unsigned int resource, struct rlimit *new_rlim) 33971da177e4SLinus Torvalds { 33981da177e4SLinus Torvalds struct rlimit *old_rlim = current->signal->rlim + resource; 33991da177e4SLinus Torvalds 34001da177e4SLinus Torvalds /* Control the ability to change the hard limit (whether 34011da177e4SLinus Torvalds lowering or raising it), so that the hard limit can 34021da177e4SLinus Torvalds later be used as a safe reset point for the soft limit 3403d84f4f99SDavid Howells upon context transitions. See selinux_bprm_committing_creds. */ 34041da177e4SLinus Torvalds if (old_rlim->rlim_max != new_rlim->rlim_max) 34053b11a1deSDavid Howells return current_has_perm(current, PROCESS__SETRLIMIT); 34061da177e4SLinus Torvalds 34071da177e4SLinus Torvalds return 0; 34081da177e4SLinus Torvalds } 34091da177e4SLinus Torvalds 34101da177e4SLinus Torvalds static int selinux_task_setscheduler(struct task_struct *p, int policy, struct sched_param *lp) 34111da177e4SLinus Torvalds { 3412b5376771SSerge E. Hallyn int rc; 3413b5376771SSerge E. Hallyn 3414200ac532SEric Paris rc = cap_task_setscheduler(p, policy, lp); 3415b5376771SSerge E. Hallyn if (rc) 3416b5376771SSerge E. Hallyn return rc; 3417b5376771SSerge E. Hallyn 34183b11a1deSDavid Howells return current_has_perm(p, PROCESS__SETSCHED); 34191da177e4SLinus Torvalds } 34201da177e4SLinus Torvalds 34211da177e4SLinus Torvalds static int selinux_task_getscheduler(struct task_struct *p) 34221da177e4SLinus Torvalds { 34233b11a1deSDavid Howells return current_has_perm(p, PROCESS__GETSCHED); 34241da177e4SLinus Torvalds } 34251da177e4SLinus Torvalds 342635601547SDavid Quigley static int selinux_task_movememory(struct task_struct *p) 342735601547SDavid Quigley { 34283b11a1deSDavid Howells return current_has_perm(p, PROCESS__SETSCHED); 342935601547SDavid Quigley } 343035601547SDavid Quigley 3431f9008e4cSDavid Quigley static int selinux_task_kill(struct task_struct *p, struct siginfo *info, 3432f9008e4cSDavid Quigley int sig, u32 secid) 34331da177e4SLinus Torvalds { 34341da177e4SLinus Torvalds u32 perm; 34351da177e4SLinus Torvalds int rc; 34361da177e4SLinus Torvalds 34371da177e4SLinus Torvalds if (!sig) 34381da177e4SLinus Torvalds perm = PROCESS__SIGNULL; /* null signal; existence test */ 34391da177e4SLinus Torvalds else 34401da177e4SLinus Torvalds perm = signal_to_av(sig); 3441f9008e4cSDavid Quigley if (secid) 3442275bb41eSDavid Howells rc = avc_has_perm(secid, task_sid(p), 3443275bb41eSDavid Howells SECCLASS_PROCESS, perm, NULL); 3444f9008e4cSDavid Quigley else 34453b11a1deSDavid Howells rc = current_has_perm(p, perm); 3446f9008e4cSDavid Quigley return rc; 34471da177e4SLinus Torvalds } 34481da177e4SLinus Torvalds 34491da177e4SLinus Torvalds static int selinux_task_wait(struct task_struct *p) 34501da177e4SLinus Torvalds { 34518a535140SEric Paris return task_has_perm(p, current, PROCESS__SIGCHLD); 34521da177e4SLinus Torvalds } 34531da177e4SLinus Torvalds 34541da177e4SLinus Torvalds static void selinux_task_to_inode(struct task_struct *p, 34551da177e4SLinus Torvalds struct inode *inode) 34561da177e4SLinus Torvalds { 34571da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 3458275bb41eSDavid Howells u32 sid = task_sid(p); 34591da177e4SLinus Torvalds 3460275bb41eSDavid Howells isec->sid = sid; 34611da177e4SLinus Torvalds isec->initialized = 1; 34621da177e4SLinus Torvalds } 34631da177e4SLinus Torvalds 34641da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 346567f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv4(struct sk_buff *skb, 34662bf49690SThomas Liu struct common_audit_data *ad, u8 *proto) 34671da177e4SLinus Torvalds { 34681da177e4SLinus Torvalds int offset, ihlen, ret = -EINVAL; 34691da177e4SLinus Torvalds struct iphdr _iph, *ih; 34701da177e4SLinus Torvalds 3471bbe735e4SArnaldo Carvalho de Melo offset = skb_network_offset(skb); 34721da177e4SLinus Torvalds ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph); 34731da177e4SLinus Torvalds if (ih == NULL) 34741da177e4SLinus Torvalds goto out; 34751da177e4SLinus Torvalds 34761da177e4SLinus Torvalds ihlen = ih->ihl * 4; 34771da177e4SLinus Torvalds if (ihlen < sizeof(_iph)) 34781da177e4SLinus Torvalds goto out; 34791da177e4SLinus Torvalds 34801da177e4SLinus Torvalds ad->u.net.v4info.saddr = ih->saddr; 34811da177e4SLinus Torvalds ad->u.net.v4info.daddr = ih->daddr; 34821da177e4SLinus Torvalds ret = 0; 34831da177e4SLinus Torvalds 348467f83cbfSVenkat Yekkirala if (proto) 348567f83cbfSVenkat Yekkirala *proto = ih->protocol; 348667f83cbfSVenkat Yekkirala 34871da177e4SLinus Torvalds switch (ih->protocol) { 34881da177e4SLinus Torvalds case IPPROTO_TCP: { 34891da177e4SLinus Torvalds struct tcphdr _tcph, *th; 34901da177e4SLinus Torvalds 34911da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 34921da177e4SLinus Torvalds break; 34931da177e4SLinus Torvalds 34941da177e4SLinus Torvalds offset += ihlen; 34951da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 34961da177e4SLinus Torvalds if (th == NULL) 34971da177e4SLinus Torvalds break; 34981da177e4SLinus Torvalds 34991da177e4SLinus Torvalds ad->u.net.sport = th->source; 35001da177e4SLinus Torvalds ad->u.net.dport = th->dest; 35011da177e4SLinus Torvalds break; 35021da177e4SLinus Torvalds } 35031da177e4SLinus Torvalds 35041da177e4SLinus Torvalds case IPPROTO_UDP: { 35051da177e4SLinus Torvalds struct udphdr _udph, *uh; 35061da177e4SLinus Torvalds 35071da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 35081da177e4SLinus Torvalds break; 35091da177e4SLinus Torvalds 35101da177e4SLinus Torvalds offset += ihlen; 35111da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 35121da177e4SLinus Torvalds if (uh == NULL) 35131da177e4SLinus Torvalds break; 35141da177e4SLinus Torvalds 35151da177e4SLinus Torvalds ad->u.net.sport = uh->source; 35161da177e4SLinus Torvalds ad->u.net.dport = uh->dest; 35171da177e4SLinus Torvalds break; 35181da177e4SLinus Torvalds } 35191da177e4SLinus Torvalds 35202ee92d46SJames Morris case IPPROTO_DCCP: { 35212ee92d46SJames Morris struct dccp_hdr _dccph, *dh; 35222ee92d46SJames Morris 35232ee92d46SJames Morris if (ntohs(ih->frag_off) & IP_OFFSET) 35242ee92d46SJames Morris break; 35252ee92d46SJames Morris 35262ee92d46SJames Morris offset += ihlen; 35272ee92d46SJames Morris dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); 35282ee92d46SJames Morris if (dh == NULL) 35292ee92d46SJames Morris break; 35302ee92d46SJames Morris 35312ee92d46SJames Morris ad->u.net.sport = dh->dccph_sport; 35322ee92d46SJames Morris ad->u.net.dport = dh->dccph_dport; 35332ee92d46SJames Morris break; 35342ee92d46SJames Morris } 35352ee92d46SJames Morris 35361da177e4SLinus Torvalds default: 35371da177e4SLinus Torvalds break; 35381da177e4SLinus Torvalds } 35391da177e4SLinus Torvalds out: 35401da177e4SLinus Torvalds return ret; 35411da177e4SLinus Torvalds } 35421da177e4SLinus Torvalds 35431da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 35441da177e4SLinus Torvalds 35451da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 354667f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv6(struct sk_buff *skb, 35472bf49690SThomas Liu struct common_audit_data *ad, u8 *proto) 35481da177e4SLinus Torvalds { 35491da177e4SLinus Torvalds u8 nexthdr; 35501da177e4SLinus Torvalds int ret = -EINVAL, offset; 35511da177e4SLinus Torvalds struct ipv6hdr _ipv6h, *ip6; 35521da177e4SLinus Torvalds 3553bbe735e4SArnaldo Carvalho de Melo offset = skb_network_offset(skb); 35541da177e4SLinus Torvalds ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h); 35551da177e4SLinus Torvalds if (ip6 == NULL) 35561da177e4SLinus Torvalds goto out; 35571da177e4SLinus Torvalds 35581da177e4SLinus Torvalds ipv6_addr_copy(&ad->u.net.v6info.saddr, &ip6->saddr); 35591da177e4SLinus Torvalds ipv6_addr_copy(&ad->u.net.v6info.daddr, &ip6->daddr); 35601da177e4SLinus Torvalds ret = 0; 35611da177e4SLinus Torvalds 35621da177e4SLinus Torvalds nexthdr = ip6->nexthdr; 35631da177e4SLinus Torvalds offset += sizeof(_ipv6h); 35640d3d077cSHerbert Xu offset = ipv6_skip_exthdr(skb, offset, &nexthdr); 35651da177e4SLinus Torvalds if (offset < 0) 35661da177e4SLinus Torvalds goto out; 35671da177e4SLinus Torvalds 356867f83cbfSVenkat Yekkirala if (proto) 356967f83cbfSVenkat Yekkirala *proto = nexthdr; 357067f83cbfSVenkat Yekkirala 35711da177e4SLinus Torvalds switch (nexthdr) { 35721da177e4SLinus Torvalds case IPPROTO_TCP: { 35731da177e4SLinus Torvalds struct tcphdr _tcph, *th; 35741da177e4SLinus Torvalds 35751da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 35761da177e4SLinus Torvalds if (th == NULL) 35771da177e4SLinus Torvalds break; 35781da177e4SLinus Torvalds 35791da177e4SLinus Torvalds ad->u.net.sport = th->source; 35801da177e4SLinus Torvalds ad->u.net.dport = th->dest; 35811da177e4SLinus Torvalds break; 35821da177e4SLinus Torvalds } 35831da177e4SLinus Torvalds 35841da177e4SLinus Torvalds case IPPROTO_UDP: { 35851da177e4SLinus Torvalds struct udphdr _udph, *uh; 35861da177e4SLinus Torvalds 35871da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 35881da177e4SLinus Torvalds if (uh == NULL) 35891da177e4SLinus Torvalds break; 35901da177e4SLinus Torvalds 35911da177e4SLinus Torvalds ad->u.net.sport = uh->source; 35921da177e4SLinus Torvalds ad->u.net.dport = uh->dest; 35931da177e4SLinus Torvalds break; 35941da177e4SLinus Torvalds } 35951da177e4SLinus Torvalds 35962ee92d46SJames Morris case IPPROTO_DCCP: { 35972ee92d46SJames Morris struct dccp_hdr _dccph, *dh; 35982ee92d46SJames Morris 35992ee92d46SJames Morris dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); 36002ee92d46SJames Morris if (dh == NULL) 36012ee92d46SJames Morris break; 36022ee92d46SJames Morris 36032ee92d46SJames Morris ad->u.net.sport = dh->dccph_sport; 36042ee92d46SJames Morris ad->u.net.dport = dh->dccph_dport; 36052ee92d46SJames Morris break; 36062ee92d46SJames Morris } 36072ee92d46SJames Morris 36081da177e4SLinus Torvalds /* includes fragments */ 36091da177e4SLinus Torvalds default: 36101da177e4SLinus Torvalds break; 36111da177e4SLinus Torvalds } 36121da177e4SLinus Torvalds out: 36131da177e4SLinus Torvalds return ret; 36141da177e4SLinus Torvalds } 36151da177e4SLinus Torvalds 36161da177e4SLinus Torvalds #endif /* IPV6 */ 36171da177e4SLinus Torvalds 36182bf49690SThomas Liu static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad, 3619cf9481e2SDavid Howells char **_addrp, int src, u8 *proto) 36201da177e4SLinus Torvalds { 3621cf9481e2SDavid Howells char *addrp; 3622cf9481e2SDavid Howells int ret; 36231da177e4SLinus Torvalds 36241da177e4SLinus Torvalds switch (ad->u.net.family) { 36251da177e4SLinus Torvalds case PF_INET: 362667f83cbfSVenkat Yekkirala ret = selinux_parse_skb_ipv4(skb, ad, proto); 3627cf9481e2SDavid Howells if (ret) 3628cf9481e2SDavid Howells goto parse_error; 3629cf9481e2SDavid Howells addrp = (char *)(src ? &ad->u.net.v4info.saddr : 36301da177e4SLinus Torvalds &ad->u.net.v4info.daddr); 3631cf9481e2SDavid Howells goto okay; 36321da177e4SLinus Torvalds 36331da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 36341da177e4SLinus Torvalds case PF_INET6: 363567f83cbfSVenkat Yekkirala ret = selinux_parse_skb_ipv6(skb, ad, proto); 3636cf9481e2SDavid Howells if (ret) 3637cf9481e2SDavid Howells goto parse_error; 3638cf9481e2SDavid Howells addrp = (char *)(src ? &ad->u.net.v6info.saddr : 36391da177e4SLinus Torvalds &ad->u.net.v6info.daddr); 3640cf9481e2SDavid Howells goto okay; 36411da177e4SLinus Torvalds #endif /* IPV6 */ 36421da177e4SLinus Torvalds default: 3643cf9481e2SDavid Howells addrp = NULL; 3644cf9481e2SDavid Howells goto okay; 36451da177e4SLinus Torvalds } 36461da177e4SLinus Torvalds 3647cf9481e2SDavid Howells parse_error: 364871f1cb05SPaul Moore printk(KERN_WARNING 364971f1cb05SPaul Moore "SELinux: failure in selinux_parse_skb()," 365071f1cb05SPaul Moore " unable to parse packet\n"); 36511da177e4SLinus Torvalds return ret; 3652cf9481e2SDavid Howells 3653cf9481e2SDavid Howells okay: 3654cf9481e2SDavid Howells if (_addrp) 3655cf9481e2SDavid Howells *_addrp = addrp; 3656cf9481e2SDavid Howells return 0; 36571da177e4SLinus Torvalds } 36581da177e4SLinus Torvalds 36594f6a993fSPaul Moore /** 3660220deb96SPaul Moore * selinux_skb_peerlbl_sid - Determine the peer label of a packet 36614f6a993fSPaul Moore * @skb: the packet 366275e22910SPaul Moore * @family: protocol family 3663220deb96SPaul Moore * @sid: the packet's peer label SID 36644f6a993fSPaul Moore * 36654f6a993fSPaul Moore * Description: 3666220deb96SPaul Moore * Check the various different forms of network peer labeling and determine 3667220deb96SPaul Moore * the peer label/SID for the packet; most of the magic actually occurs in 3668220deb96SPaul Moore * the security server function security_net_peersid_cmp(). The function 3669220deb96SPaul Moore * returns zero if the value in @sid is valid (although it may be SECSID_NULL) 3670220deb96SPaul Moore * or -EACCES if @sid is invalid due to inconsistencies with the different 3671220deb96SPaul Moore * peer labels. 36724f6a993fSPaul Moore * 36734f6a993fSPaul Moore */ 3674220deb96SPaul Moore static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid) 36754f6a993fSPaul Moore { 367671f1cb05SPaul Moore int err; 36774f6a993fSPaul Moore u32 xfrm_sid; 36784f6a993fSPaul Moore u32 nlbl_sid; 3679220deb96SPaul Moore u32 nlbl_type; 36804f6a993fSPaul Moore 36814f6a993fSPaul Moore selinux_skb_xfrm_sid(skb, &xfrm_sid); 36825dbe1eb0SPaul Moore selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid); 3683220deb96SPaul Moore 368471f1cb05SPaul Moore err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid); 368571f1cb05SPaul Moore if (unlikely(err)) { 368671f1cb05SPaul Moore printk(KERN_WARNING 368771f1cb05SPaul Moore "SELinux: failure in selinux_skb_peerlbl_sid()," 368871f1cb05SPaul Moore " unable to determine packet's peer label\n"); 3689220deb96SPaul Moore return -EACCES; 369071f1cb05SPaul Moore } 3691220deb96SPaul Moore 3692220deb96SPaul Moore return 0; 36934f6a993fSPaul Moore } 36944f6a993fSPaul Moore 36951da177e4SLinus Torvalds /* socket security operations */ 36961da177e4SLinus Torvalds static int socket_has_perm(struct task_struct *task, struct socket *sock, 36971da177e4SLinus Torvalds u32 perms) 36981da177e4SLinus Torvalds { 36991da177e4SLinus Torvalds struct inode_security_struct *isec; 37002bf49690SThomas Liu struct common_audit_data ad; 3701275bb41eSDavid Howells u32 sid; 37021da177e4SLinus Torvalds int err = 0; 37031da177e4SLinus Torvalds 37041da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 37051da177e4SLinus Torvalds 37061da177e4SLinus Torvalds if (isec->sid == SECINITSID_KERNEL) 37071da177e4SLinus Torvalds goto out; 3708275bb41eSDavid Howells sid = task_sid(task); 37091da177e4SLinus Torvalds 37102bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, NET); 37111da177e4SLinus Torvalds ad.u.net.sk = sock->sk; 3712275bb41eSDavid Howells err = avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad); 37131da177e4SLinus Torvalds 37141da177e4SLinus Torvalds out: 37151da177e4SLinus Torvalds return err; 37161da177e4SLinus Torvalds } 37171da177e4SLinus Torvalds 37181da177e4SLinus Torvalds static int selinux_socket_create(int family, int type, 37191da177e4SLinus Torvalds int protocol, int kern) 37201da177e4SLinus Torvalds { 3721275bb41eSDavid Howells const struct cred *cred = current_cred(); 3722275bb41eSDavid Howells const struct task_security_struct *tsec = cred->security; 3723275bb41eSDavid Howells u32 sid, newsid; 3724275bb41eSDavid Howells u16 secclass; 37251da177e4SLinus Torvalds int err = 0; 37261da177e4SLinus Torvalds 37271da177e4SLinus Torvalds if (kern) 37281da177e4SLinus Torvalds goto out; 37291da177e4SLinus Torvalds 3730275bb41eSDavid Howells sid = tsec->sid; 3731275bb41eSDavid Howells newsid = tsec->sockcreate_sid ?: sid; 3732275bb41eSDavid Howells 3733275bb41eSDavid Howells secclass = socket_type_to_security_class(family, type, protocol); 3734275bb41eSDavid Howells err = avc_has_perm(sid, newsid, secclass, SOCKET__CREATE, NULL); 37351da177e4SLinus Torvalds 37361da177e4SLinus Torvalds out: 37371da177e4SLinus Torvalds return err; 37381da177e4SLinus Torvalds } 37391da177e4SLinus Torvalds 37407420ed23SVenkat Yekkirala static int selinux_socket_post_create(struct socket *sock, int family, 37411da177e4SLinus Torvalds int type, int protocol, int kern) 37421da177e4SLinus Torvalds { 3743275bb41eSDavid Howells const struct cred *cred = current_cred(); 3744275bb41eSDavid Howells const struct task_security_struct *tsec = cred->security; 37451da177e4SLinus Torvalds struct inode_security_struct *isec; 3746892c141eSVenkat Yekkirala struct sk_security_struct *sksec; 3747275bb41eSDavid Howells u32 sid, newsid; 3748275bb41eSDavid Howells int err = 0; 3749275bb41eSDavid Howells 3750275bb41eSDavid Howells sid = tsec->sid; 3751275bb41eSDavid Howells newsid = tsec->sockcreate_sid; 37521da177e4SLinus Torvalds 37531da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 37541da177e4SLinus Torvalds 3755275bb41eSDavid Howells if (kern) 3756275bb41eSDavid Howells isec->sid = SECINITSID_KERNEL; 3757275bb41eSDavid Howells else if (newsid) 3758275bb41eSDavid Howells isec->sid = newsid; 3759275bb41eSDavid Howells else 3760275bb41eSDavid Howells isec->sid = sid; 3761275bb41eSDavid Howells 37621da177e4SLinus Torvalds isec->sclass = socket_type_to_security_class(family, type, protocol); 37631da177e4SLinus Torvalds isec->initialized = 1; 37641da177e4SLinus Torvalds 3765892c141eSVenkat Yekkirala if (sock->sk) { 3766892c141eSVenkat Yekkirala sksec = sock->sk->sk_security; 3767892c141eSVenkat Yekkirala sksec->sid = isec->sid; 3768220deb96SPaul Moore sksec->sclass = isec->sclass; 3769389fb800SPaul Moore err = selinux_netlbl_socket_post_create(sock->sk, family); 3770892c141eSVenkat Yekkirala } 3771892c141eSVenkat Yekkirala 37727420ed23SVenkat Yekkirala return err; 37731da177e4SLinus Torvalds } 37741da177e4SLinus Torvalds 37751da177e4SLinus Torvalds /* Range of port numbers used to automatically bind. 37761da177e4SLinus Torvalds Need to determine whether we should perform a name_bind 37771da177e4SLinus Torvalds permission check between the socket and the port number. */ 37781da177e4SLinus Torvalds 37791da177e4SLinus Torvalds static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen) 37801da177e4SLinus Torvalds { 37811da177e4SLinus Torvalds u16 family; 37821da177e4SLinus Torvalds int err; 37831da177e4SLinus Torvalds 37841da177e4SLinus Torvalds err = socket_has_perm(current, sock, SOCKET__BIND); 37851da177e4SLinus Torvalds if (err) 37861da177e4SLinus Torvalds goto out; 37871da177e4SLinus Torvalds 37881da177e4SLinus Torvalds /* 37891da177e4SLinus Torvalds * If PF_INET or PF_INET6, check name_bind permission for the port. 379013402580SJames Morris * Multiple address binding for SCTP is not supported yet: we just 379113402580SJames Morris * check the first address now. 37921da177e4SLinus Torvalds */ 37931da177e4SLinus Torvalds family = sock->sk->sk_family; 37941da177e4SLinus Torvalds if (family == PF_INET || family == PF_INET6) { 37951da177e4SLinus Torvalds char *addrp; 37961da177e4SLinus Torvalds struct inode_security_struct *isec; 37972bf49690SThomas Liu struct common_audit_data ad; 37981da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 37991da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 38001da177e4SLinus Torvalds unsigned short snum; 38011da177e4SLinus Torvalds struct sock *sk = sock->sk; 3802e399f982SJames Morris u32 sid, node_perm; 38031da177e4SLinus Torvalds 38041da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 38051da177e4SLinus Torvalds 38061da177e4SLinus Torvalds if (family == PF_INET) { 38071da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 38081da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 38091da177e4SLinus Torvalds addrp = (char *)&addr4->sin_addr.s_addr; 38101da177e4SLinus Torvalds } else { 38111da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 38121da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 38131da177e4SLinus Torvalds addrp = (char *)&addr6->sin6_addr.s6_addr; 38141da177e4SLinus Torvalds } 38151da177e4SLinus Torvalds 3816227b60f5SStephen Hemminger if (snum) { 3817227b60f5SStephen Hemminger int low, high; 3818227b60f5SStephen Hemminger 3819227b60f5SStephen Hemminger inet_get_local_port_range(&low, &high); 3820227b60f5SStephen Hemminger 3821227b60f5SStephen Hemminger if (snum < max(PROT_SOCK, low) || snum > high) { 38223e112172SPaul Moore err = sel_netport_sid(sk->sk_protocol, 38233e112172SPaul Moore snum, &sid); 38241da177e4SLinus Torvalds if (err) 38251da177e4SLinus Torvalds goto out; 38262bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, NET); 38271da177e4SLinus Torvalds ad.u.net.sport = htons(snum); 38281da177e4SLinus Torvalds ad.u.net.family = family; 38291da177e4SLinus Torvalds err = avc_has_perm(isec->sid, sid, 38301da177e4SLinus Torvalds isec->sclass, 38311da177e4SLinus Torvalds SOCKET__NAME_BIND, &ad); 38321da177e4SLinus Torvalds if (err) 38331da177e4SLinus Torvalds goto out; 38341da177e4SLinus Torvalds } 3835227b60f5SStephen Hemminger } 38361da177e4SLinus Torvalds 383713402580SJames Morris switch (isec->sclass) { 383813402580SJames Morris case SECCLASS_TCP_SOCKET: 38391da177e4SLinus Torvalds node_perm = TCP_SOCKET__NODE_BIND; 38401da177e4SLinus Torvalds break; 38411da177e4SLinus Torvalds 384213402580SJames Morris case SECCLASS_UDP_SOCKET: 38431da177e4SLinus Torvalds node_perm = UDP_SOCKET__NODE_BIND; 38441da177e4SLinus Torvalds break; 38451da177e4SLinus Torvalds 38462ee92d46SJames Morris case SECCLASS_DCCP_SOCKET: 38472ee92d46SJames Morris node_perm = DCCP_SOCKET__NODE_BIND; 38482ee92d46SJames Morris break; 38492ee92d46SJames Morris 38501da177e4SLinus Torvalds default: 38511da177e4SLinus Torvalds node_perm = RAWIP_SOCKET__NODE_BIND; 38521da177e4SLinus Torvalds break; 38531da177e4SLinus Torvalds } 38541da177e4SLinus Torvalds 3855224dfbd8SPaul Moore err = sel_netnode_sid(addrp, family, &sid); 38561da177e4SLinus Torvalds if (err) 38571da177e4SLinus Torvalds goto out; 38581da177e4SLinus Torvalds 38592bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, NET); 38601da177e4SLinus Torvalds ad.u.net.sport = htons(snum); 38611da177e4SLinus Torvalds ad.u.net.family = family; 38621da177e4SLinus Torvalds 38631da177e4SLinus Torvalds if (family == PF_INET) 38641da177e4SLinus Torvalds ad.u.net.v4info.saddr = addr4->sin_addr.s_addr; 38651da177e4SLinus Torvalds else 38661da177e4SLinus Torvalds ipv6_addr_copy(&ad.u.net.v6info.saddr, &addr6->sin6_addr); 38671da177e4SLinus Torvalds 38681da177e4SLinus Torvalds err = avc_has_perm(isec->sid, sid, 38691da177e4SLinus Torvalds isec->sclass, node_perm, &ad); 38701da177e4SLinus Torvalds if (err) 38711da177e4SLinus Torvalds goto out; 38721da177e4SLinus Torvalds } 38731da177e4SLinus Torvalds out: 38741da177e4SLinus Torvalds return err; 38751da177e4SLinus Torvalds } 38761da177e4SLinus Torvalds 38771da177e4SLinus Torvalds static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen) 38781da177e4SLinus Torvalds { 3879014ab19aSPaul Moore struct sock *sk = sock->sk; 38801da177e4SLinus Torvalds struct inode_security_struct *isec; 38811da177e4SLinus Torvalds int err; 38821da177e4SLinus Torvalds 38831da177e4SLinus Torvalds err = socket_has_perm(current, sock, SOCKET__CONNECT); 38841da177e4SLinus Torvalds if (err) 38851da177e4SLinus Torvalds return err; 38861da177e4SLinus Torvalds 38871da177e4SLinus Torvalds /* 38882ee92d46SJames Morris * If a TCP or DCCP socket, check name_connect permission for the port. 38891da177e4SLinus Torvalds */ 38901da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 38912ee92d46SJames Morris if (isec->sclass == SECCLASS_TCP_SOCKET || 38922ee92d46SJames Morris isec->sclass == SECCLASS_DCCP_SOCKET) { 38932bf49690SThomas Liu struct common_audit_data ad; 38941da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 38951da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 38961da177e4SLinus Torvalds unsigned short snum; 38972ee92d46SJames Morris u32 sid, perm; 38981da177e4SLinus Torvalds 38991da177e4SLinus Torvalds if (sk->sk_family == PF_INET) { 39001da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 3901911656f8SStephen Smalley if (addrlen < sizeof(struct sockaddr_in)) 39021da177e4SLinus Torvalds return -EINVAL; 39031da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 39041da177e4SLinus Torvalds } else { 39051da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 3906911656f8SStephen Smalley if (addrlen < SIN6_LEN_RFC2133) 39071da177e4SLinus Torvalds return -EINVAL; 39081da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 39091da177e4SLinus Torvalds } 39101da177e4SLinus Torvalds 39113e112172SPaul Moore err = sel_netport_sid(sk->sk_protocol, snum, &sid); 39121da177e4SLinus Torvalds if (err) 39131da177e4SLinus Torvalds goto out; 39141da177e4SLinus Torvalds 39152ee92d46SJames Morris perm = (isec->sclass == SECCLASS_TCP_SOCKET) ? 39162ee92d46SJames Morris TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT; 39172ee92d46SJames Morris 39182bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, NET); 39191da177e4SLinus Torvalds ad.u.net.dport = htons(snum); 39201da177e4SLinus Torvalds ad.u.net.family = sk->sk_family; 39212ee92d46SJames Morris err = avc_has_perm(isec->sid, sid, isec->sclass, perm, &ad); 39221da177e4SLinus Torvalds if (err) 39231da177e4SLinus Torvalds goto out; 39241da177e4SLinus Torvalds } 39251da177e4SLinus Torvalds 3926014ab19aSPaul Moore err = selinux_netlbl_socket_connect(sk, address); 3927014ab19aSPaul Moore 39281da177e4SLinus Torvalds out: 39291da177e4SLinus Torvalds return err; 39301da177e4SLinus Torvalds } 39311da177e4SLinus Torvalds 39321da177e4SLinus Torvalds static int selinux_socket_listen(struct socket *sock, int backlog) 39331da177e4SLinus Torvalds { 39341da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__LISTEN); 39351da177e4SLinus Torvalds } 39361da177e4SLinus Torvalds 39371da177e4SLinus Torvalds static int selinux_socket_accept(struct socket *sock, struct socket *newsock) 39381da177e4SLinus Torvalds { 39391da177e4SLinus Torvalds int err; 39401da177e4SLinus Torvalds struct inode_security_struct *isec; 39411da177e4SLinus Torvalds struct inode_security_struct *newisec; 39421da177e4SLinus Torvalds 39431da177e4SLinus Torvalds err = socket_has_perm(current, sock, SOCKET__ACCEPT); 39441da177e4SLinus Torvalds if (err) 39451da177e4SLinus Torvalds return err; 39461da177e4SLinus Torvalds 39471da177e4SLinus Torvalds newisec = SOCK_INODE(newsock)->i_security; 39481da177e4SLinus Torvalds 39491da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 39501da177e4SLinus Torvalds newisec->sclass = isec->sclass; 39511da177e4SLinus Torvalds newisec->sid = isec->sid; 39521da177e4SLinus Torvalds newisec->initialized = 1; 39531da177e4SLinus Torvalds 39541da177e4SLinus Torvalds return 0; 39551da177e4SLinus Torvalds } 39561da177e4SLinus Torvalds 39571da177e4SLinus Torvalds static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg, 39581da177e4SLinus Torvalds int size) 39591da177e4SLinus Torvalds { 3960389fb800SPaul Moore return socket_has_perm(current, sock, SOCKET__WRITE); 39611da177e4SLinus Torvalds } 39621da177e4SLinus Torvalds 39631da177e4SLinus Torvalds static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg, 39641da177e4SLinus Torvalds int size, int flags) 39651da177e4SLinus Torvalds { 39661da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__READ); 39671da177e4SLinus Torvalds } 39681da177e4SLinus Torvalds 39691da177e4SLinus Torvalds static int selinux_socket_getsockname(struct socket *sock) 39701da177e4SLinus Torvalds { 39711da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__GETATTR); 39721da177e4SLinus Torvalds } 39731da177e4SLinus Torvalds 39741da177e4SLinus Torvalds static int selinux_socket_getpeername(struct socket *sock) 39751da177e4SLinus Torvalds { 39761da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__GETATTR); 39771da177e4SLinus Torvalds } 39781da177e4SLinus Torvalds 39791da177e4SLinus Torvalds static int selinux_socket_setsockopt(struct socket *sock, int level, int optname) 39801da177e4SLinus Torvalds { 3981f8687afeSPaul Moore int err; 3982f8687afeSPaul Moore 3983f8687afeSPaul Moore err = socket_has_perm(current, sock, SOCKET__SETOPT); 3984f8687afeSPaul Moore if (err) 3985f8687afeSPaul Moore return err; 3986f8687afeSPaul Moore 3987f8687afeSPaul Moore return selinux_netlbl_socket_setsockopt(sock, level, optname); 39881da177e4SLinus Torvalds } 39891da177e4SLinus Torvalds 39901da177e4SLinus Torvalds static int selinux_socket_getsockopt(struct socket *sock, int level, 39911da177e4SLinus Torvalds int optname) 39921da177e4SLinus Torvalds { 39931da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__GETOPT); 39941da177e4SLinus Torvalds } 39951da177e4SLinus Torvalds 39961da177e4SLinus Torvalds static int selinux_socket_shutdown(struct socket *sock, int how) 39971da177e4SLinus Torvalds { 39981da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__SHUTDOWN); 39991da177e4SLinus Torvalds } 40001da177e4SLinus Torvalds 40011da177e4SLinus Torvalds static int selinux_socket_unix_stream_connect(struct socket *sock, 40021da177e4SLinus Torvalds struct socket *other, 40031da177e4SLinus Torvalds struct sock *newsk) 40041da177e4SLinus Torvalds { 40051da177e4SLinus Torvalds struct sk_security_struct *ssec; 40061da177e4SLinus Torvalds struct inode_security_struct *isec; 40071da177e4SLinus Torvalds struct inode_security_struct *other_isec; 40082bf49690SThomas Liu struct common_audit_data ad; 40091da177e4SLinus Torvalds int err; 40101da177e4SLinus Torvalds 40111da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 40121da177e4SLinus Torvalds other_isec = SOCK_INODE(other)->i_security; 40131da177e4SLinus Torvalds 40142bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, NET); 40151da177e4SLinus Torvalds ad.u.net.sk = other->sk; 40161da177e4SLinus Torvalds 40171da177e4SLinus Torvalds err = avc_has_perm(isec->sid, other_isec->sid, 40181da177e4SLinus Torvalds isec->sclass, 40191da177e4SLinus Torvalds UNIX_STREAM_SOCKET__CONNECTTO, &ad); 40201da177e4SLinus Torvalds if (err) 40211da177e4SLinus Torvalds return err; 40221da177e4SLinus Torvalds 40231da177e4SLinus Torvalds /* connecting socket */ 40241da177e4SLinus Torvalds ssec = sock->sk->sk_security; 40251da177e4SLinus Torvalds ssec->peer_sid = other_isec->sid; 40261da177e4SLinus Torvalds 40271da177e4SLinus Torvalds /* server child socket */ 40281da177e4SLinus Torvalds ssec = newsk->sk_security; 40291da177e4SLinus Torvalds ssec->peer_sid = isec->sid; 40304237c75cSVenkat Yekkirala err = security_sid_mls_copy(other_isec->sid, ssec->peer_sid, &ssec->sid); 40311da177e4SLinus Torvalds 40324237c75cSVenkat Yekkirala return err; 40331da177e4SLinus Torvalds } 40341da177e4SLinus Torvalds 40351da177e4SLinus Torvalds static int selinux_socket_unix_may_send(struct socket *sock, 40361da177e4SLinus Torvalds struct socket *other) 40371da177e4SLinus Torvalds { 40381da177e4SLinus Torvalds struct inode_security_struct *isec; 40391da177e4SLinus Torvalds struct inode_security_struct *other_isec; 40402bf49690SThomas Liu struct common_audit_data ad; 40411da177e4SLinus Torvalds int err; 40421da177e4SLinus Torvalds 40431da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 40441da177e4SLinus Torvalds other_isec = SOCK_INODE(other)->i_security; 40451da177e4SLinus Torvalds 40462bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, NET); 40471da177e4SLinus Torvalds ad.u.net.sk = other->sk; 40481da177e4SLinus Torvalds 40491da177e4SLinus Torvalds err = avc_has_perm(isec->sid, other_isec->sid, 40501da177e4SLinus Torvalds isec->sclass, SOCKET__SENDTO, &ad); 40511da177e4SLinus Torvalds if (err) 40521da177e4SLinus Torvalds return err; 40531da177e4SLinus Torvalds 40541da177e4SLinus Torvalds return 0; 40551da177e4SLinus Torvalds } 40561da177e4SLinus Torvalds 4057effad8dfSPaul Moore static int selinux_inet_sys_rcv_skb(int ifindex, char *addrp, u16 family, 4058effad8dfSPaul Moore u32 peer_sid, 40592bf49690SThomas Liu struct common_audit_data *ad) 4060effad8dfSPaul Moore { 4061effad8dfSPaul Moore int err; 4062effad8dfSPaul Moore u32 if_sid; 4063effad8dfSPaul Moore u32 node_sid; 4064effad8dfSPaul Moore 4065effad8dfSPaul Moore err = sel_netif_sid(ifindex, &if_sid); 4066effad8dfSPaul Moore if (err) 4067effad8dfSPaul Moore return err; 4068effad8dfSPaul Moore err = avc_has_perm(peer_sid, if_sid, 4069effad8dfSPaul Moore SECCLASS_NETIF, NETIF__INGRESS, ad); 4070effad8dfSPaul Moore if (err) 4071effad8dfSPaul Moore return err; 4072effad8dfSPaul Moore 4073effad8dfSPaul Moore err = sel_netnode_sid(addrp, family, &node_sid); 4074effad8dfSPaul Moore if (err) 4075effad8dfSPaul Moore return err; 4076effad8dfSPaul Moore return avc_has_perm(peer_sid, node_sid, 4077effad8dfSPaul Moore SECCLASS_NODE, NODE__RECVFROM, ad); 4078effad8dfSPaul Moore } 4079effad8dfSPaul Moore 4080220deb96SPaul Moore static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb, 4081d8395c87SPaul Moore u16 family) 4082220deb96SPaul Moore { 4083277d342fSPaul Moore int err = 0; 4084220deb96SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 4085220deb96SPaul Moore u32 peer_sid; 4086220deb96SPaul Moore u32 sk_sid = sksec->sid; 40872bf49690SThomas Liu struct common_audit_data ad; 4088d8395c87SPaul Moore char *addrp; 4089d8395c87SPaul Moore 40902bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, NET); 40918964be4aSEric Dumazet ad.u.net.netif = skb->skb_iif; 4092d8395c87SPaul Moore ad.u.net.family = family; 4093d8395c87SPaul Moore err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); 4094d8395c87SPaul Moore if (err) 4095d8395c87SPaul Moore return err; 4096220deb96SPaul Moore 409758bfbb51SPaul Moore if (selinux_secmark_enabled()) { 4098220deb96SPaul Moore err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET, 4099d8395c87SPaul Moore PACKET__RECV, &ad); 4100220deb96SPaul Moore if (err) 4101220deb96SPaul Moore return err; 410258bfbb51SPaul Moore } 4103220deb96SPaul Moore 4104220deb96SPaul Moore if (selinux_policycap_netpeer) { 4105220deb96SPaul Moore err = selinux_skb_peerlbl_sid(skb, family, &peer_sid); 4106220deb96SPaul Moore if (err) 4107220deb96SPaul Moore return err; 4108220deb96SPaul Moore err = avc_has_perm(sk_sid, peer_sid, 4109d8395c87SPaul Moore SECCLASS_PEER, PEER__RECV, &ad); 4110dfaebe98SPaul Moore if (err) 4111dfaebe98SPaul Moore selinux_netlbl_err(skb, err, 0); 4112220deb96SPaul Moore } else { 4113d8395c87SPaul Moore err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad); 4114220deb96SPaul Moore if (err) 4115220deb96SPaul Moore return err; 4116d8395c87SPaul Moore err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad); 4117220deb96SPaul Moore } 4118220deb96SPaul Moore 41194e5ab4cbSJames Morris return err; 41204e5ab4cbSJames Morris } 4121d28d1e08STrent Jaeger 41224e5ab4cbSJames Morris static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) 41234e5ab4cbSJames Morris { 4124220deb96SPaul Moore int err; 41254237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 4126220deb96SPaul Moore u16 family = sk->sk_family; 4127220deb96SPaul Moore u32 sk_sid = sksec->sid; 41282bf49690SThomas Liu struct common_audit_data ad; 4129220deb96SPaul Moore char *addrp; 4130d8395c87SPaul Moore u8 secmark_active; 4131d8395c87SPaul Moore u8 peerlbl_active; 41324e5ab4cbSJames Morris 41334e5ab4cbSJames Morris if (family != PF_INET && family != PF_INET6) 4134220deb96SPaul Moore return 0; 41354e5ab4cbSJames Morris 41364e5ab4cbSJames Morris /* Handle mapped IPv4 packets arriving via IPv6 sockets */ 413787fcd70dSAl Viro if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 41384e5ab4cbSJames Morris family = PF_INET; 41394e5ab4cbSJames Morris 4140d8395c87SPaul Moore /* If any sort of compatibility mode is enabled then handoff processing 4141d8395c87SPaul Moore * to the selinux_sock_rcv_skb_compat() function to deal with the 4142d8395c87SPaul Moore * special handling. We do this in an attempt to keep this function 4143d8395c87SPaul Moore * as fast and as clean as possible. */ 414458bfbb51SPaul Moore if (!selinux_policycap_netpeer) 4145d8395c87SPaul Moore return selinux_sock_rcv_skb_compat(sk, skb, family); 4146d8395c87SPaul Moore 4147d8395c87SPaul Moore secmark_active = selinux_secmark_enabled(); 4148d8395c87SPaul Moore peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled(); 4149d8395c87SPaul Moore if (!secmark_active && !peerlbl_active) 4150d8395c87SPaul Moore return 0; 4151d8395c87SPaul Moore 41522bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, NET); 41538964be4aSEric Dumazet ad.u.net.netif = skb->skb_iif; 41544e5ab4cbSJames Morris ad.u.net.family = family; 4155224dfbd8SPaul Moore err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); 41564e5ab4cbSJames Morris if (err) 4157220deb96SPaul Moore return err; 41584e5ab4cbSJames Morris 4159d8395c87SPaul Moore if (peerlbl_active) { 4160d621d35eSPaul Moore u32 peer_sid; 4161220deb96SPaul Moore 4162220deb96SPaul Moore err = selinux_skb_peerlbl_sid(skb, family, &peer_sid); 4163220deb96SPaul Moore if (err) 4164220deb96SPaul Moore return err; 41658964be4aSEric Dumazet err = selinux_inet_sys_rcv_skb(skb->skb_iif, addrp, family, 4166effad8dfSPaul Moore peer_sid, &ad); 4167dfaebe98SPaul Moore if (err) { 4168dfaebe98SPaul Moore selinux_netlbl_err(skb, err, 0); 4169effad8dfSPaul Moore return err; 4170dfaebe98SPaul Moore } 4171d621d35eSPaul Moore err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER, 4172d621d35eSPaul Moore PEER__RECV, &ad); 4173dfaebe98SPaul Moore if (err) 4174dfaebe98SPaul Moore selinux_netlbl_err(skb, err, 0); 4175d621d35eSPaul Moore } 4176d621d35eSPaul Moore 4177d8395c87SPaul Moore if (secmark_active) { 4178effad8dfSPaul Moore err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET, 4179effad8dfSPaul Moore PACKET__RECV, &ad); 4180effad8dfSPaul Moore if (err) 4181effad8dfSPaul Moore return err; 4182effad8dfSPaul Moore } 4183effad8dfSPaul Moore 4184d621d35eSPaul Moore return err; 41851da177e4SLinus Torvalds } 41861da177e4SLinus Torvalds 41872c7946a7SCatherine Zhang static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval, 41881da177e4SLinus Torvalds int __user *optlen, unsigned len) 41891da177e4SLinus Torvalds { 41901da177e4SLinus Torvalds int err = 0; 41911da177e4SLinus Torvalds char *scontext; 41921da177e4SLinus Torvalds u32 scontext_len; 41931da177e4SLinus Torvalds struct sk_security_struct *ssec; 41941da177e4SLinus Torvalds struct inode_security_struct *isec; 41953de4bab5SPaul Moore u32 peer_sid = SECSID_NULL; 41961da177e4SLinus Torvalds 41971da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 41982c7946a7SCatherine Zhang 41993de4bab5SPaul Moore if (isec->sclass == SECCLASS_UNIX_STREAM_SOCKET || 42003de4bab5SPaul Moore isec->sclass == SECCLASS_TCP_SOCKET) { 42016b877699SVenkat Yekkirala ssec = sock->sk->sk_security; 42026b877699SVenkat Yekkirala peer_sid = ssec->peer_sid; 42036b877699SVenkat Yekkirala } 42042c7946a7SCatherine Zhang if (peer_sid == SECSID_NULL) { 42052c7946a7SCatherine Zhang err = -ENOPROTOOPT; 42062c7946a7SCatherine Zhang goto out; 42072c7946a7SCatherine Zhang } 42081da177e4SLinus Torvalds 42092c7946a7SCatherine Zhang err = security_sid_to_context(peer_sid, &scontext, &scontext_len); 42101da177e4SLinus Torvalds 42111da177e4SLinus Torvalds if (err) 42121da177e4SLinus Torvalds goto out; 42131da177e4SLinus Torvalds 42141da177e4SLinus Torvalds if (scontext_len > len) { 42151da177e4SLinus Torvalds err = -ERANGE; 42161da177e4SLinus Torvalds goto out_len; 42171da177e4SLinus Torvalds } 42181da177e4SLinus Torvalds 42191da177e4SLinus Torvalds if (copy_to_user(optval, scontext, scontext_len)) 42201da177e4SLinus Torvalds err = -EFAULT; 42211da177e4SLinus Torvalds 42221da177e4SLinus Torvalds out_len: 42231da177e4SLinus Torvalds if (put_user(scontext_len, optlen)) 42241da177e4SLinus Torvalds err = -EFAULT; 42251da177e4SLinus Torvalds 42261da177e4SLinus Torvalds kfree(scontext); 42271da177e4SLinus Torvalds out: 42281da177e4SLinus Torvalds return err; 42291da177e4SLinus Torvalds } 42301da177e4SLinus Torvalds 4231dc49c1f9SCatherine Zhang static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid) 42322c7946a7SCatherine Zhang { 4233dc49c1f9SCatherine Zhang u32 peer_secid = SECSID_NULL; 423475e22910SPaul Moore u16 family; 4235877ce7c1SCatherine Zhang 4236aa862900SPaul Moore if (skb && skb->protocol == htons(ETH_P_IP)) 4237aa862900SPaul Moore family = PF_INET; 4238aa862900SPaul Moore else if (skb && skb->protocol == htons(ETH_P_IPV6)) 4239aa862900SPaul Moore family = PF_INET6; 4240aa862900SPaul Moore else if (sock) 424175e22910SPaul Moore family = sock->sk->sk_family; 424275e22910SPaul Moore else 424375e22910SPaul Moore goto out; 424475e22910SPaul Moore 424575e22910SPaul Moore if (sock && family == PF_UNIX) 4246713a04aeSAhmed S. Darwish selinux_inode_getsecid(SOCK_INODE(sock), &peer_secid); 42473de4bab5SPaul Moore else if (skb) 4248220deb96SPaul Moore selinux_skb_peerlbl_sid(skb, family, &peer_secid); 42492c7946a7SCatherine Zhang 425075e22910SPaul Moore out: 4251dc49c1f9SCatherine Zhang *secid = peer_secid; 425275e22910SPaul Moore if (peer_secid == SECSID_NULL) 425375e22910SPaul Moore return -EINVAL; 425475e22910SPaul Moore return 0; 42552c7946a7SCatherine Zhang } 42562c7946a7SCatherine Zhang 42577d877f3bSAl Viro static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority) 42581da177e4SLinus Torvalds { 42591da177e4SLinus Torvalds return sk_alloc_security(sk, family, priority); 42601da177e4SLinus Torvalds } 42611da177e4SLinus Torvalds 42621da177e4SLinus Torvalds static void selinux_sk_free_security(struct sock *sk) 42631da177e4SLinus Torvalds { 42641da177e4SLinus Torvalds sk_free_security(sk); 42651da177e4SLinus Torvalds } 42661da177e4SLinus Torvalds 4267892c141eSVenkat Yekkirala static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk) 4268892c141eSVenkat Yekkirala { 4269892c141eSVenkat Yekkirala struct sk_security_struct *ssec = sk->sk_security; 4270892c141eSVenkat Yekkirala struct sk_security_struct *newssec = newsk->sk_security; 4271892c141eSVenkat Yekkirala 4272892c141eSVenkat Yekkirala newssec->sid = ssec->sid; 4273892c141eSVenkat Yekkirala newssec->peer_sid = ssec->peer_sid; 4274220deb96SPaul Moore newssec->sclass = ssec->sclass; 427599f59ed0SPaul Moore 4276389fb800SPaul Moore selinux_netlbl_sk_security_reset(newssec); 4277892c141eSVenkat Yekkirala } 4278892c141eSVenkat Yekkirala 4279beb8d13bSVenkat Yekkirala static void selinux_sk_getsecid(struct sock *sk, u32 *secid) 4280d28d1e08STrent Jaeger { 4281d28d1e08STrent Jaeger if (!sk) 4282beb8d13bSVenkat Yekkirala *secid = SECINITSID_ANY_SOCKET; 4283892c141eSVenkat Yekkirala else { 4284892c141eSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 4285d28d1e08STrent Jaeger 4286beb8d13bSVenkat Yekkirala *secid = sksec->sid; 4287892c141eSVenkat Yekkirala } 4288d28d1e08STrent Jaeger } 4289d28d1e08STrent Jaeger 42909a673e56SAdrian Bunk static void selinux_sock_graft(struct sock *sk, struct socket *parent) 42914237c75cSVenkat Yekkirala { 42924237c75cSVenkat Yekkirala struct inode_security_struct *isec = SOCK_INODE(parent)->i_security; 42934237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 42944237c75cSVenkat Yekkirala 42952148ccc4SDavid Woodhouse if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 || 42962148ccc4SDavid Woodhouse sk->sk_family == PF_UNIX) 42974237c75cSVenkat Yekkirala isec->sid = sksec->sid; 4298220deb96SPaul Moore sksec->sclass = isec->sclass; 42994237c75cSVenkat Yekkirala } 43004237c75cSVenkat Yekkirala 43019a673e56SAdrian Bunk static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb, 43024237c75cSVenkat Yekkirala struct request_sock *req) 43034237c75cSVenkat Yekkirala { 43044237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 43054237c75cSVenkat Yekkirala int err; 4306aa862900SPaul Moore u16 family = sk->sk_family; 43077420ed23SVenkat Yekkirala u32 newsid; 43084237c75cSVenkat Yekkirala u32 peersid; 43094237c75cSVenkat Yekkirala 4310aa862900SPaul Moore /* handle mapped IPv4 packets arriving via IPv6 sockets */ 4311aa862900SPaul Moore if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 4312aa862900SPaul Moore family = PF_INET; 4313aa862900SPaul Moore 4314aa862900SPaul Moore err = selinux_skb_peerlbl_sid(skb, family, &peersid); 4315220deb96SPaul Moore if (err) 4316220deb96SPaul Moore return err; 4317a51c64f1SVenkat Yekkirala if (peersid == SECSID_NULL) { 4318a51c64f1SVenkat Yekkirala req->secid = sksec->sid; 43193de4bab5SPaul Moore req->peer_secid = SECSID_NULL; 4320389fb800SPaul Moore } else { 43214237c75cSVenkat Yekkirala err = security_sid_mls_copy(sksec->sid, peersid, &newsid); 43224237c75cSVenkat Yekkirala if (err) 43234237c75cSVenkat Yekkirala return err; 43244237c75cSVenkat Yekkirala req->secid = newsid; 43256b877699SVenkat Yekkirala req->peer_secid = peersid; 4326389fb800SPaul Moore } 4327389fb800SPaul Moore 4328389fb800SPaul Moore return selinux_netlbl_inet_conn_request(req, family); 43294237c75cSVenkat Yekkirala } 43304237c75cSVenkat Yekkirala 43319a673e56SAdrian Bunk static void selinux_inet_csk_clone(struct sock *newsk, 43329a673e56SAdrian Bunk const struct request_sock *req) 43334237c75cSVenkat Yekkirala { 43344237c75cSVenkat Yekkirala struct sk_security_struct *newsksec = newsk->sk_security; 43354237c75cSVenkat Yekkirala 43364237c75cSVenkat Yekkirala newsksec->sid = req->secid; 43376b877699SVenkat Yekkirala newsksec->peer_sid = req->peer_secid; 43384237c75cSVenkat Yekkirala /* NOTE: Ideally, we should also get the isec->sid for the 43394237c75cSVenkat Yekkirala new socket in sync, but we don't have the isec available yet. 43404237c75cSVenkat Yekkirala So we will wait until sock_graft to do it, by which 43414237c75cSVenkat Yekkirala time it will have been created and available. */ 434299f59ed0SPaul Moore 43439f2ad665SPaul Moore /* We don't need to take any sort of lock here as we are the only 43449f2ad665SPaul Moore * thread with access to newsksec */ 4345389fb800SPaul Moore selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family); 43464237c75cSVenkat Yekkirala } 43474237c75cSVenkat Yekkirala 4348014ab19aSPaul Moore static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb) 43496b877699SVenkat Yekkirala { 4350aa862900SPaul Moore u16 family = sk->sk_family; 43516b877699SVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 43526b877699SVenkat Yekkirala 4353aa862900SPaul Moore /* handle mapped IPv4 packets arriving via IPv6 sockets */ 4354aa862900SPaul Moore if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 4355aa862900SPaul Moore family = PF_INET; 4356aa862900SPaul Moore 4357aa862900SPaul Moore selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid); 43586b877699SVenkat Yekkirala } 43596b877699SVenkat Yekkirala 43609a673e56SAdrian Bunk static void selinux_req_classify_flow(const struct request_sock *req, 43619a673e56SAdrian Bunk struct flowi *fl) 43624237c75cSVenkat Yekkirala { 43634237c75cSVenkat Yekkirala fl->secid = req->secid; 43644237c75cSVenkat Yekkirala } 43654237c75cSVenkat Yekkirala 4366ed6d76e4SPaul Moore static int selinux_tun_dev_create(void) 4367ed6d76e4SPaul Moore { 4368ed6d76e4SPaul Moore u32 sid = current_sid(); 4369ed6d76e4SPaul Moore 4370ed6d76e4SPaul Moore /* we aren't taking into account the "sockcreate" SID since the socket 4371ed6d76e4SPaul Moore * that is being created here is not a socket in the traditional sense, 4372ed6d76e4SPaul Moore * instead it is a private sock, accessible only to the kernel, and 4373ed6d76e4SPaul Moore * representing a wide range of network traffic spanning multiple 4374ed6d76e4SPaul Moore * connections unlike traditional sockets - check the TUN driver to 4375ed6d76e4SPaul Moore * get a better understanding of why this socket is special */ 4376ed6d76e4SPaul Moore 4377ed6d76e4SPaul Moore return avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE, 4378ed6d76e4SPaul Moore NULL); 4379ed6d76e4SPaul Moore } 4380ed6d76e4SPaul Moore 4381ed6d76e4SPaul Moore static void selinux_tun_dev_post_create(struct sock *sk) 4382ed6d76e4SPaul Moore { 4383ed6d76e4SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 4384ed6d76e4SPaul Moore 4385ed6d76e4SPaul Moore /* we don't currently perform any NetLabel based labeling here and it 4386ed6d76e4SPaul Moore * isn't clear that we would want to do so anyway; while we could apply 4387ed6d76e4SPaul Moore * labeling without the support of the TUN user the resulting labeled 4388ed6d76e4SPaul Moore * traffic from the other end of the connection would almost certainly 4389ed6d76e4SPaul Moore * cause confusion to the TUN user that had no idea network labeling 4390ed6d76e4SPaul Moore * protocols were being used */ 4391ed6d76e4SPaul Moore 4392ed6d76e4SPaul Moore /* see the comments in selinux_tun_dev_create() about why we don't use 4393ed6d76e4SPaul Moore * the sockcreate SID here */ 4394ed6d76e4SPaul Moore 4395ed6d76e4SPaul Moore sksec->sid = current_sid(); 4396ed6d76e4SPaul Moore sksec->sclass = SECCLASS_TUN_SOCKET; 4397ed6d76e4SPaul Moore } 4398ed6d76e4SPaul Moore 4399ed6d76e4SPaul Moore static int selinux_tun_dev_attach(struct sock *sk) 4400ed6d76e4SPaul Moore { 4401ed6d76e4SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 4402ed6d76e4SPaul Moore u32 sid = current_sid(); 4403ed6d76e4SPaul Moore int err; 4404ed6d76e4SPaul Moore 4405ed6d76e4SPaul Moore err = avc_has_perm(sid, sksec->sid, SECCLASS_TUN_SOCKET, 4406ed6d76e4SPaul Moore TUN_SOCKET__RELABELFROM, NULL); 4407ed6d76e4SPaul Moore if (err) 4408ed6d76e4SPaul Moore return err; 4409ed6d76e4SPaul Moore err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, 4410ed6d76e4SPaul Moore TUN_SOCKET__RELABELTO, NULL); 4411ed6d76e4SPaul Moore if (err) 4412ed6d76e4SPaul Moore return err; 4413ed6d76e4SPaul Moore 4414ed6d76e4SPaul Moore sksec->sid = sid; 4415ed6d76e4SPaul Moore 4416ed6d76e4SPaul Moore return 0; 4417ed6d76e4SPaul Moore } 4418ed6d76e4SPaul Moore 44191da177e4SLinus Torvalds static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb) 44201da177e4SLinus Torvalds { 44211da177e4SLinus Torvalds int err = 0; 44221da177e4SLinus Torvalds u32 perm; 44231da177e4SLinus Torvalds struct nlmsghdr *nlh; 44241da177e4SLinus Torvalds struct socket *sock = sk->sk_socket; 44251da177e4SLinus Torvalds struct inode_security_struct *isec = SOCK_INODE(sock)->i_security; 44261da177e4SLinus Torvalds 44271da177e4SLinus Torvalds if (skb->len < NLMSG_SPACE(0)) { 44281da177e4SLinus Torvalds err = -EINVAL; 44291da177e4SLinus Torvalds goto out; 44301da177e4SLinus Torvalds } 4431b529ccf2SArnaldo Carvalho de Melo nlh = nlmsg_hdr(skb); 44321da177e4SLinus Torvalds 44331da177e4SLinus Torvalds err = selinux_nlmsg_lookup(isec->sclass, nlh->nlmsg_type, &perm); 44341da177e4SLinus Torvalds if (err) { 44351da177e4SLinus Torvalds if (err == -EINVAL) { 44369ad9ad38SDavid Woodhouse audit_log(current->audit_context, GFP_KERNEL, AUDIT_SELINUX_ERR, 44371da177e4SLinus Torvalds "SELinux: unrecognized netlink message" 44381da177e4SLinus Torvalds " type=%hu for sclass=%hu\n", 44391da177e4SLinus Torvalds nlh->nlmsg_type, isec->sclass); 444039c9aedeSEric Paris if (!selinux_enforcing || security_get_allow_unknown()) 44411da177e4SLinus Torvalds err = 0; 44421da177e4SLinus Torvalds } 44431da177e4SLinus Torvalds 44441da177e4SLinus Torvalds /* Ignore */ 44451da177e4SLinus Torvalds if (err == -ENOENT) 44461da177e4SLinus Torvalds err = 0; 44471da177e4SLinus Torvalds goto out; 44481da177e4SLinus Torvalds } 44491da177e4SLinus Torvalds 44501da177e4SLinus Torvalds err = socket_has_perm(current, sock, perm); 44511da177e4SLinus Torvalds out: 44521da177e4SLinus Torvalds return err; 44531da177e4SLinus Torvalds } 44541da177e4SLinus Torvalds 44551da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER 44561da177e4SLinus Torvalds 4457effad8dfSPaul Moore static unsigned int selinux_ip_forward(struct sk_buff *skb, int ifindex, 4458effad8dfSPaul Moore u16 family) 44591da177e4SLinus Torvalds { 4460dfaebe98SPaul Moore int err; 4461effad8dfSPaul Moore char *addrp; 4462effad8dfSPaul Moore u32 peer_sid; 44632bf49690SThomas Liu struct common_audit_data ad; 4464effad8dfSPaul Moore u8 secmark_active; 4465948bf85cSPaul Moore u8 netlbl_active; 4466effad8dfSPaul Moore u8 peerlbl_active; 44674237c75cSVenkat Yekkirala 4468effad8dfSPaul Moore if (!selinux_policycap_netpeer) 4469effad8dfSPaul Moore return NF_ACCEPT; 44704237c75cSVenkat Yekkirala 4471effad8dfSPaul Moore secmark_active = selinux_secmark_enabled(); 4472948bf85cSPaul Moore netlbl_active = netlbl_enabled(); 4473948bf85cSPaul Moore peerlbl_active = netlbl_active || selinux_xfrm_enabled(); 4474effad8dfSPaul Moore if (!secmark_active && !peerlbl_active) 4475effad8dfSPaul Moore return NF_ACCEPT; 44764237c75cSVenkat Yekkirala 4477d8395c87SPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0) 4478d8395c87SPaul Moore return NF_DROP; 4479d8395c87SPaul Moore 44802bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, NET); 4481effad8dfSPaul Moore ad.u.net.netif = ifindex; 4482effad8dfSPaul Moore ad.u.net.family = family; 4483effad8dfSPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0) 4484effad8dfSPaul Moore return NF_DROP; 44851da177e4SLinus Torvalds 4486dfaebe98SPaul Moore if (peerlbl_active) { 4487dfaebe98SPaul Moore err = selinux_inet_sys_rcv_skb(ifindex, addrp, family, 4488dfaebe98SPaul Moore peer_sid, &ad); 4489dfaebe98SPaul Moore if (err) { 4490dfaebe98SPaul Moore selinux_netlbl_err(skb, err, 1); 4491effad8dfSPaul Moore return NF_DROP; 4492dfaebe98SPaul Moore } 4493dfaebe98SPaul Moore } 4494effad8dfSPaul Moore 4495effad8dfSPaul Moore if (secmark_active) 4496effad8dfSPaul Moore if (avc_has_perm(peer_sid, skb->secmark, 4497effad8dfSPaul Moore SECCLASS_PACKET, PACKET__FORWARD_IN, &ad)) 4498effad8dfSPaul Moore return NF_DROP; 4499effad8dfSPaul Moore 4500948bf85cSPaul Moore if (netlbl_active) 4501948bf85cSPaul Moore /* we do this in the FORWARD path and not the POST_ROUTING 4502948bf85cSPaul Moore * path because we want to make sure we apply the necessary 4503948bf85cSPaul Moore * labeling before IPsec is applied so we can leverage AH 4504948bf85cSPaul Moore * protection */ 4505948bf85cSPaul Moore if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0) 4506948bf85cSPaul Moore return NF_DROP; 4507948bf85cSPaul Moore 4508effad8dfSPaul Moore return NF_ACCEPT; 4509effad8dfSPaul Moore } 4510effad8dfSPaul Moore 4511effad8dfSPaul Moore static unsigned int selinux_ipv4_forward(unsigned int hooknum, 4512effad8dfSPaul Moore struct sk_buff *skb, 4513effad8dfSPaul Moore const struct net_device *in, 4514effad8dfSPaul Moore const struct net_device *out, 4515effad8dfSPaul Moore int (*okfn)(struct sk_buff *)) 4516effad8dfSPaul Moore { 4517effad8dfSPaul Moore return selinux_ip_forward(skb, in->ifindex, PF_INET); 4518effad8dfSPaul Moore } 4519effad8dfSPaul Moore 4520effad8dfSPaul Moore #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 4521effad8dfSPaul Moore static unsigned int selinux_ipv6_forward(unsigned int hooknum, 4522effad8dfSPaul Moore struct sk_buff *skb, 4523effad8dfSPaul Moore const struct net_device *in, 4524effad8dfSPaul Moore const struct net_device *out, 4525effad8dfSPaul Moore int (*okfn)(struct sk_buff *)) 4526effad8dfSPaul Moore { 4527effad8dfSPaul Moore return selinux_ip_forward(skb, in->ifindex, PF_INET6); 4528effad8dfSPaul Moore } 4529effad8dfSPaul Moore #endif /* IPV6 */ 4530effad8dfSPaul Moore 4531948bf85cSPaul Moore static unsigned int selinux_ip_output(struct sk_buff *skb, 4532948bf85cSPaul Moore u16 family) 4533948bf85cSPaul Moore { 4534948bf85cSPaul Moore u32 sid; 4535948bf85cSPaul Moore 4536948bf85cSPaul Moore if (!netlbl_enabled()) 4537948bf85cSPaul Moore return NF_ACCEPT; 4538948bf85cSPaul Moore 4539948bf85cSPaul Moore /* we do this in the LOCAL_OUT path and not the POST_ROUTING path 4540948bf85cSPaul Moore * because we want to make sure we apply the necessary labeling 4541948bf85cSPaul Moore * before IPsec is applied so we can leverage AH protection */ 4542948bf85cSPaul Moore if (skb->sk) { 4543948bf85cSPaul Moore struct sk_security_struct *sksec = skb->sk->sk_security; 4544948bf85cSPaul Moore sid = sksec->sid; 4545948bf85cSPaul Moore } else 4546948bf85cSPaul Moore sid = SECINITSID_KERNEL; 4547948bf85cSPaul Moore if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0) 4548948bf85cSPaul Moore return NF_DROP; 4549948bf85cSPaul Moore 4550948bf85cSPaul Moore return NF_ACCEPT; 4551948bf85cSPaul Moore } 4552948bf85cSPaul Moore 4553948bf85cSPaul Moore static unsigned int selinux_ipv4_output(unsigned int hooknum, 4554948bf85cSPaul Moore struct sk_buff *skb, 4555948bf85cSPaul Moore const struct net_device *in, 4556948bf85cSPaul Moore const struct net_device *out, 4557948bf85cSPaul Moore int (*okfn)(struct sk_buff *)) 4558948bf85cSPaul Moore { 4559948bf85cSPaul Moore return selinux_ip_output(skb, PF_INET); 4560948bf85cSPaul Moore } 4561948bf85cSPaul Moore 4562effad8dfSPaul Moore static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb, 4563effad8dfSPaul Moore int ifindex, 4564d8395c87SPaul Moore u16 family) 45654e5ab4cbSJames Morris { 4566effad8dfSPaul Moore struct sock *sk = skb->sk; 45674237c75cSVenkat Yekkirala struct sk_security_struct *sksec; 45682bf49690SThomas Liu struct common_audit_data ad; 4569d8395c87SPaul Moore char *addrp; 4570d8395c87SPaul Moore u8 proto; 45714e5ab4cbSJames Morris 4572effad8dfSPaul Moore if (sk == NULL) 4573effad8dfSPaul Moore return NF_ACCEPT; 45744237c75cSVenkat Yekkirala sksec = sk->sk_security; 45754e5ab4cbSJames Morris 45762bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, NET); 4577d8395c87SPaul Moore ad.u.net.netif = ifindex; 4578d8395c87SPaul Moore ad.u.net.family = family; 4579d8395c87SPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto)) 4580d8395c87SPaul Moore return NF_DROP; 4581d8395c87SPaul Moore 458258bfbb51SPaul Moore if (selinux_secmark_enabled()) 4583effad8dfSPaul Moore if (avc_has_perm(sksec->sid, skb->secmark, 4584d8395c87SPaul Moore SECCLASS_PACKET, PACKET__SEND, &ad)) 4585effad8dfSPaul Moore return NF_DROP; 45861da177e4SLinus Torvalds 4587effad8dfSPaul Moore if (selinux_policycap_netpeer) 4588d8395c87SPaul Moore if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto)) 4589effad8dfSPaul Moore return NF_DROP; 4590effad8dfSPaul Moore 4591effad8dfSPaul Moore return NF_ACCEPT; 4592effad8dfSPaul Moore } 4593effad8dfSPaul Moore 4594effad8dfSPaul Moore static unsigned int selinux_ip_postroute(struct sk_buff *skb, int ifindex, 4595effad8dfSPaul Moore u16 family) 4596effad8dfSPaul Moore { 4597effad8dfSPaul Moore u32 secmark_perm; 4598effad8dfSPaul Moore u32 peer_sid; 4599effad8dfSPaul Moore struct sock *sk; 46002bf49690SThomas Liu struct common_audit_data ad; 4601effad8dfSPaul Moore char *addrp; 4602effad8dfSPaul Moore u8 secmark_active; 4603effad8dfSPaul Moore u8 peerlbl_active; 4604effad8dfSPaul Moore 4605effad8dfSPaul Moore /* If any sort of compatibility mode is enabled then handoff processing 4606effad8dfSPaul Moore * to the selinux_ip_postroute_compat() function to deal with the 4607effad8dfSPaul Moore * special handling. We do this in an attempt to keep this function 4608effad8dfSPaul Moore * as fast and as clean as possible. */ 460958bfbb51SPaul Moore if (!selinux_policycap_netpeer) 4610d8395c87SPaul Moore return selinux_ip_postroute_compat(skb, ifindex, family); 4611def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM 4612effad8dfSPaul Moore /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec 4613effad8dfSPaul Moore * packet transformation so allow the packet to pass without any checks 4614effad8dfSPaul Moore * since we'll have another chance to perform access control checks 4615effad8dfSPaul Moore * when the packet is on it's final way out. 4616effad8dfSPaul Moore * NOTE: there appear to be some IPv6 multicast cases where skb->dst 4617effad8dfSPaul Moore * is NULL, in this case go ahead and apply access control. */ 4618adf30907SEric Dumazet if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL) 4619effad8dfSPaul Moore return NF_ACCEPT; 4620def8b4faSAlexey Dobriyan #endif 4621effad8dfSPaul Moore secmark_active = selinux_secmark_enabled(); 4622effad8dfSPaul Moore peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled(); 4623effad8dfSPaul Moore if (!secmark_active && !peerlbl_active) 4624effad8dfSPaul Moore return NF_ACCEPT; 4625effad8dfSPaul Moore 4626d8395c87SPaul Moore /* if the packet is being forwarded then get the peer label from the 4627d8395c87SPaul Moore * packet itself; otherwise check to see if it is from a local 4628d8395c87SPaul Moore * application or the kernel, if from an application get the peer label 4629d8395c87SPaul Moore * from the sending socket, otherwise use the kernel's sid */ 4630effad8dfSPaul Moore sk = skb->sk; 4631d8395c87SPaul Moore if (sk == NULL) { 4632d8395c87SPaul Moore switch (family) { 4633d8395c87SPaul Moore case PF_INET: 4634d8395c87SPaul Moore if (IPCB(skb)->flags & IPSKB_FORWARDED) 4635d8395c87SPaul Moore secmark_perm = PACKET__FORWARD_OUT; 4636d8395c87SPaul Moore else 4637d8395c87SPaul Moore secmark_perm = PACKET__SEND; 4638d8395c87SPaul Moore break; 4639d8395c87SPaul Moore case PF_INET6: 4640d8395c87SPaul Moore if (IP6CB(skb)->flags & IP6SKB_FORWARDED) 4641d8395c87SPaul Moore secmark_perm = PACKET__FORWARD_OUT; 4642d8395c87SPaul Moore else 4643d8395c87SPaul Moore secmark_perm = PACKET__SEND; 4644d8395c87SPaul Moore break; 4645d8395c87SPaul Moore default: 4646d8395c87SPaul Moore return NF_DROP; 4647d8395c87SPaul Moore } 4648d8395c87SPaul Moore if (secmark_perm == PACKET__FORWARD_OUT) { 4649d8395c87SPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &peer_sid)) 4650d8395c87SPaul Moore return NF_DROP; 4651d8395c87SPaul Moore } else 4652d8395c87SPaul Moore peer_sid = SECINITSID_KERNEL; 4653d8395c87SPaul Moore } else { 4654effad8dfSPaul Moore struct sk_security_struct *sksec = sk->sk_security; 4655effad8dfSPaul Moore peer_sid = sksec->sid; 4656effad8dfSPaul Moore secmark_perm = PACKET__SEND; 4657effad8dfSPaul Moore } 4658effad8dfSPaul Moore 46592bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, NET); 4660d8395c87SPaul Moore ad.u.net.netif = ifindex; 4661d8395c87SPaul Moore ad.u.net.family = family; 4662d8395c87SPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL)) 4663d8395c87SPaul Moore return NF_DROP; 4664d8395c87SPaul Moore 4665effad8dfSPaul Moore if (secmark_active) 4666effad8dfSPaul Moore if (avc_has_perm(peer_sid, skb->secmark, 4667effad8dfSPaul Moore SECCLASS_PACKET, secmark_perm, &ad)) 4668effad8dfSPaul Moore return NF_DROP; 4669effad8dfSPaul Moore 4670effad8dfSPaul Moore if (peerlbl_active) { 4671effad8dfSPaul Moore u32 if_sid; 4672effad8dfSPaul Moore u32 node_sid; 4673effad8dfSPaul Moore 4674effad8dfSPaul Moore if (sel_netif_sid(ifindex, &if_sid)) 4675effad8dfSPaul Moore return NF_DROP; 4676effad8dfSPaul Moore if (avc_has_perm(peer_sid, if_sid, 4677effad8dfSPaul Moore SECCLASS_NETIF, NETIF__EGRESS, &ad)) 4678effad8dfSPaul Moore return NF_DROP; 4679effad8dfSPaul Moore 4680effad8dfSPaul Moore if (sel_netnode_sid(addrp, family, &node_sid)) 4681effad8dfSPaul Moore return NF_DROP; 4682effad8dfSPaul Moore if (avc_has_perm(peer_sid, node_sid, 4683effad8dfSPaul Moore SECCLASS_NODE, NODE__SENDTO, &ad)) 4684effad8dfSPaul Moore return NF_DROP; 4685effad8dfSPaul Moore } 4686effad8dfSPaul Moore 4687effad8dfSPaul Moore return NF_ACCEPT; 4688effad8dfSPaul Moore } 4689effad8dfSPaul Moore 4690effad8dfSPaul Moore static unsigned int selinux_ipv4_postroute(unsigned int hooknum, 4691a224be76SDavid S. Miller struct sk_buff *skb, 46921da177e4SLinus Torvalds const struct net_device *in, 46931da177e4SLinus Torvalds const struct net_device *out, 46941da177e4SLinus Torvalds int (*okfn)(struct sk_buff *)) 46951da177e4SLinus Torvalds { 4696effad8dfSPaul Moore return selinux_ip_postroute(skb, out->ifindex, PF_INET); 46971da177e4SLinus Torvalds } 46981da177e4SLinus Torvalds 46991da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 4700effad8dfSPaul Moore static unsigned int selinux_ipv6_postroute(unsigned int hooknum, 4701a224be76SDavid S. Miller struct sk_buff *skb, 47021da177e4SLinus Torvalds const struct net_device *in, 47031da177e4SLinus Torvalds const struct net_device *out, 47041da177e4SLinus Torvalds int (*okfn)(struct sk_buff *)) 47051da177e4SLinus Torvalds { 4706effad8dfSPaul Moore return selinux_ip_postroute(skb, out->ifindex, PF_INET6); 47071da177e4SLinus Torvalds } 47081da177e4SLinus Torvalds #endif /* IPV6 */ 47091da177e4SLinus Torvalds 47101da177e4SLinus Torvalds #endif /* CONFIG_NETFILTER */ 47111da177e4SLinus Torvalds 47121da177e4SLinus Torvalds static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb) 47131da177e4SLinus Torvalds { 47141da177e4SLinus Torvalds int err; 47151da177e4SLinus Torvalds 4716200ac532SEric Paris err = cap_netlink_send(sk, skb); 47171da177e4SLinus Torvalds if (err) 47181da177e4SLinus Torvalds return err; 47191da177e4SLinus Torvalds 4720941fc5b2SStephen Smalley return selinux_nlmsg_perm(sk, skb); 47211da177e4SLinus Torvalds } 47221da177e4SLinus Torvalds 4723c7bdb545SDarrel Goeddel static int selinux_netlink_recv(struct sk_buff *skb, int capability) 47241da177e4SLinus Torvalds { 4725c7bdb545SDarrel Goeddel int err; 47262bf49690SThomas Liu struct common_audit_data ad; 4727c7bdb545SDarrel Goeddel 4728200ac532SEric Paris err = cap_netlink_recv(skb, capability); 4729c7bdb545SDarrel Goeddel if (err) 4730c7bdb545SDarrel Goeddel return err; 4731c7bdb545SDarrel Goeddel 47322bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, CAP); 4733c7bdb545SDarrel Goeddel ad.u.cap = capability; 4734c7bdb545SDarrel Goeddel 4735c7bdb545SDarrel Goeddel return avc_has_perm(NETLINK_CB(skb).sid, NETLINK_CB(skb).sid, 4736c7bdb545SDarrel Goeddel SECCLASS_CAPABILITY, CAP_TO_MASK(capability), &ad); 47371da177e4SLinus Torvalds } 47381da177e4SLinus Torvalds 47391da177e4SLinus Torvalds static int ipc_alloc_security(struct task_struct *task, 47401da177e4SLinus Torvalds struct kern_ipc_perm *perm, 47411da177e4SLinus Torvalds u16 sclass) 47421da177e4SLinus Torvalds { 47431da177e4SLinus Torvalds struct ipc_security_struct *isec; 4744275bb41eSDavid Howells u32 sid; 47451da177e4SLinus Torvalds 474689d155efSJames Morris isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL); 47471da177e4SLinus Torvalds if (!isec) 47481da177e4SLinus Torvalds return -ENOMEM; 47491da177e4SLinus Torvalds 4750275bb41eSDavid Howells sid = task_sid(task); 47511da177e4SLinus Torvalds isec->sclass = sclass; 4752275bb41eSDavid Howells isec->sid = sid; 47531da177e4SLinus Torvalds perm->security = isec; 47541da177e4SLinus Torvalds 47551da177e4SLinus Torvalds return 0; 47561da177e4SLinus Torvalds } 47571da177e4SLinus Torvalds 47581da177e4SLinus Torvalds static void ipc_free_security(struct kern_ipc_perm *perm) 47591da177e4SLinus Torvalds { 47601da177e4SLinus Torvalds struct ipc_security_struct *isec = perm->security; 47611da177e4SLinus Torvalds perm->security = NULL; 47621da177e4SLinus Torvalds kfree(isec); 47631da177e4SLinus Torvalds } 47641da177e4SLinus Torvalds 47651da177e4SLinus Torvalds static int msg_msg_alloc_security(struct msg_msg *msg) 47661da177e4SLinus Torvalds { 47671da177e4SLinus Torvalds struct msg_security_struct *msec; 47681da177e4SLinus Torvalds 476989d155efSJames Morris msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL); 47701da177e4SLinus Torvalds if (!msec) 47711da177e4SLinus Torvalds return -ENOMEM; 47721da177e4SLinus Torvalds 47731da177e4SLinus Torvalds msec->sid = SECINITSID_UNLABELED; 47741da177e4SLinus Torvalds msg->security = msec; 47751da177e4SLinus Torvalds 47761da177e4SLinus Torvalds return 0; 47771da177e4SLinus Torvalds } 47781da177e4SLinus Torvalds 47791da177e4SLinus Torvalds static void msg_msg_free_security(struct msg_msg *msg) 47801da177e4SLinus Torvalds { 47811da177e4SLinus Torvalds struct msg_security_struct *msec = msg->security; 47821da177e4SLinus Torvalds 47831da177e4SLinus Torvalds msg->security = NULL; 47841da177e4SLinus Torvalds kfree(msec); 47851da177e4SLinus Torvalds } 47861da177e4SLinus Torvalds 47871da177e4SLinus Torvalds static int ipc_has_perm(struct kern_ipc_perm *ipc_perms, 47886af963f1SStephen Smalley u32 perms) 47891da177e4SLinus Torvalds { 47901da177e4SLinus Torvalds struct ipc_security_struct *isec; 47912bf49690SThomas Liu struct common_audit_data ad; 4792275bb41eSDavid Howells u32 sid = current_sid(); 47931da177e4SLinus Torvalds 47941da177e4SLinus Torvalds isec = ipc_perms->security; 47951da177e4SLinus Torvalds 47962bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, IPC); 47971da177e4SLinus Torvalds ad.u.ipc_id = ipc_perms->key; 47981da177e4SLinus Torvalds 4799275bb41eSDavid Howells return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad); 48001da177e4SLinus Torvalds } 48011da177e4SLinus Torvalds 48021da177e4SLinus Torvalds static int selinux_msg_msg_alloc_security(struct msg_msg *msg) 48031da177e4SLinus Torvalds { 48041da177e4SLinus Torvalds return msg_msg_alloc_security(msg); 48051da177e4SLinus Torvalds } 48061da177e4SLinus Torvalds 48071da177e4SLinus Torvalds static void selinux_msg_msg_free_security(struct msg_msg *msg) 48081da177e4SLinus Torvalds { 48091da177e4SLinus Torvalds msg_msg_free_security(msg); 48101da177e4SLinus Torvalds } 48111da177e4SLinus Torvalds 48121da177e4SLinus Torvalds /* message queue security operations */ 48131da177e4SLinus Torvalds static int selinux_msg_queue_alloc_security(struct msg_queue *msq) 48141da177e4SLinus Torvalds { 48151da177e4SLinus Torvalds struct ipc_security_struct *isec; 48162bf49690SThomas Liu struct common_audit_data ad; 4817275bb41eSDavid Howells u32 sid = current_sid(); 48181da177e4SLinus Torvalds int rc; 48191da177e4SLinus Torvalds 48201da177e4SLinus Torvalds rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ); 48211da177e4SLinus Torvalds if (rc) 48221da177e4SLinus Torvalds return rc; 48231da177e4SLinus Torvalds 48241da177e4SLinus Torvalds isec = msq->q_perm.security; 48251da177e4SLinus Torvalds 48262bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, IPC); 48271da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 48281da177e4SLinus Torvalds 4829275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ, 48301da177e4SLinus Torvalds MSGQ__CREATE, &ad); 48311da177e4SLinus Torvalds if (rc) { 48321da177e4SLinus Torvalds ipc_free_security(&msq->q_perm); 48331da177e4SLinus Torvalds return rc; 48341da177e4SLinus Torvalds } 48351da177e4SLinus Torvalds return 0; 48361da177e4SLinus Torvalds } 48371da177e4SLinus Torvalds 48381da177e4SLinus Torvalds static void selinux_msg_queue_free_security(struct msg_queue *msq) 48391da177e4SLinus Torvalds { 48401da177e4SLinus Torvalds ipc_free_security(&msq->q_perm); 48411da177e4SLinus Torvalds } 48421da177e4SLinus Torvalds 48431da177e4SLinus Torvalds static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg) 48441da177e4SLinus Torvalds { 48451da177e4SLinus Torvalds struct ipc_security_struct *isec; 48462bf49690SThomas Liu struct common_audit_data ad; 4847275bb41eSDavid Howells u32 sid = current_sid(); 48481da177e4SLinus Torvalds 48491da177e4SLinus Torvalds isec = msq->q_perm.security; 48501da177e4SLinus Torvalds 48512bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, IPC); 48521da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 48531da177e4SLinus Torvalds 4854275bb41eSDavid Howells return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ, 48551da177e4SLinus Torvalds MSGQ__ASSOCIATE, &ad); 48561da177e4SLinus Torvalds } 48571da177e4SLinus Torvalds 48581da177e4SLinus Torvalds static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd) 48591da177e4SLinus Torvalds { 48601da177e4SLinus Torvalds int err; 48611da177e4SLinus Torvalds int perms; 48621da177e4SLinus Torvalds 48631da177e4SLinus Torvalds switch (cmd) { 48641da177e4SLinus Torvalds case IPC_INFO: 48651da177e4SLinus Torvalds case MSG_INFO: 48661da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 48671da177e4SLinus Torvalds return task_has_system(current, SYSTEM__IPC_INFO); 48681da177e4SLinus Torvalds case IPC_STAT: 48691da177e4SLinus Torvalds case MSG_STAT: 48701da177e4SLinus Torvalds perms = MSGQ__GETATTR | MSGQ__ASSOCIATE; 48711da177e4SLinus Torvalds break; 48721da177e4SLinus Torvalds case IPC_SET: 48731da177e4SLinus Torvalds perms = MSGQ__SETATTR; 48741da177e4SLinus Torvalds break; 48751da177e4SLinus Torvalds case IPC_RMID: 48761da177e4SLinus Torvalds perms = MSGQ__DESTROY; 48771da177e4SLinus Torvalds break; 48781da177e4SLinus Torvalds default: 48791da177e4SLinus Torvalds return 0; 48801da177e4SLinus Torvalds } 48811da177e4SLinus Torvalds 48826af963f1SStephen Smalley err = ipc_has_perm(&msq->q_perm, perms); 48831da177e4SLinus Torvalds return err; 48841da177e4SLinus Torvalds } 48851da177e4SLinus Torvalds 48861da177e4SLinus Torvalds static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg) 48871da177e4SLinus Torvalds { 48881da177e4SLinus Torvalds struct ipc_security_struct *isec; 48891da177e4SLinus Torvalds struct msg_security_struct *msec; 48902bf49690SThomas Liu struct common_audit_data ad; 4891275bb41eSDavid Howells u32 sid = current_sid(); 48921da177e4SLinus Torvalds int rc; 48931da177e4SLinus Torvalds 48941da177e4SLinus Torvalds isec = msq->q_perm.security; 48951da177e4SLinus Torvalds msec = msg->security; 48961da177e4SLinus Torvalds 48971da177e4SLinus Torvalds /* 48981da177e4SLinus Torvalds * First time through, need to assign label to the message 48991da177e4SLinus Torvalds */ 49001da177e4SLinus Torvalds if (msec->sid == SECINITSID_UNLABELED) { 49011da177e4SLinus Torvalds /* 49021da177e4SLinus Torvalds * Compute new sid based on current process and 49031da177e4SLinus Torvalds * message queue this message will be stored in 49041da177e4SLinus Torvalds */ 4905275bb41eSDavid Howells rc = security_transition_sid(sid, isec->sid, SECCLASS_MSG, 49061da177e4SLinus Torvalds &msec->sid); 49071da177e4SLinus Torvalds if (rc) 49081da177e4SLinus Torvalds return rc; 49091da177e4SLinus Torvalds } 49101da177e4SLinus Torvalds 49112bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, IPC); 49121da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 49131da177e4SLinus Torvalds 49141da177e4SLinus Torvalds /* Can this process write to the queue? */ 4915275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ, 49161da177e4SLinus Torvalds MSGQ__WRITE, &ad); 49171da177e4SLinus Torvalds if (!rc) 49181da177e4SLinus Torvalds /* Can this process send the message */ 4919275bb41eSDavid Howells rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG, 4920275bb41eSDavid Howells MSG__SEND, &ad); 49211da177e4SLinus Torvalds if (!rc) 49221da177e4SLinus Torvalds /* Can the message be put in the queue? */ 4923275bb41eSDavid Howells rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ, 4924275bb41eSDavid Howells MSGQ__ENQUEUE, &ad); 49251da177e4SLinus Torvalds 49261da177e4SLinus Torvalds return rc; 49271da177e4SLinus Torvalds } 49281da177e4SLinus Torvalds 49291da177e4SLinus Torvalds static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg, 49301da177e4SLinus Torvalds struct task_struct *target, 49311da177e4SLinus Torvalds long type, int mode) 49321da177e4SLinus Torvalds { 49331da177e4SLinus Torvalds struct ipc_security_struct *isec; 49341da177e4SLinus Torvalds struct msg_security_struct *msec; 49352bf49690SThomas Liu struct common_audit_data ad; 4936275bb41eSDavid Howells u32 sid = task_sid(target); 49371da177e4SLinus Torvalds int rc; 49381da177e4SLinus Torvalds 49391da177e4SLinus Torvalds isec = msq->q_perm.security; 49401da177e4SLinus Torvalds msec = msg->security; 49411da177e4SLinus Torvalds 49422bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, IPC); 49431da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 49441da177e4SLinus Torvalds 4945275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, 49461da177e4SLinus Torvalds SECCLASS_MSGQ, MSGQ__READ, &ad); 49471da177e4SLinus Torvalds if (!rc) 4948275bb41eSDavid Howells rc = avc_has_perm(sid, msec->sid, 49491da177e4SLinus Torvalds SECCLASS_MSG, MSG__RECEIVE, &ad); 49501da177e4SLinus Torvalds return rc; 49511da177e4SLinus Torvalds } 49521da177e4SLinus Torvalds 49531da177e4SLinus Torvalds /* Shared Memory security operations */ 49541da177e4SLinus Torvalds static int selinux_shm_alloc_security(struct shmid_kernel *shp) 49551da177e4SLinus Torvalds { 49561da177e4SLinus Torvalds struct ipc_security_struct *isec; 49572bf49690SThomas Liu struct common_audit_data ad; 4958275bb41eSDavid Howells u32 sid = current_sid(); 49591da177e4SLinus Torvalds int rc; 49601da177e4SLinus Torvalds 49611da177e4SLinus Torvalds rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM); 49621da177e4SLinus Torvalds if (rc) 49631da177e4SLinus Torvalds return rc; 49641da177e4SLinus Torvalds 49651da177e4SLinus Torvalds isec = shp->shm_perm.security; 49661da177e4SLinus Torvalds 49672bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, IPC); 49681da177e4SLinus Torvalds ad.u.ipc_id = shp->shm_perm.key; 49691da177e4SLinus Torvalds 4970275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, SECCLASS_SHM, 49711da177e4SLinus Torvalds SHM__CREATE, &ad); 49721da177e4SLinus Torvalds if (rc) { 49731da177e4SLinus Torvalds ipc_free_security(&shp->shm_perm); 49741da177e4SLinus Torvalds return rc; 49751da177e4SLinus Torvalds } 49761da177e4SLinus Torvalds return 0; 49771da177e4SLinus Torvalds } 49781da177e4SLinus Torvalds 49791da177e4SLinus Torvalds static void selinux_shm_free_security(struct shmid_kernel *shp) 49801da177e4SLinus Torvalds { 49811da177e4SLinus Torvalds ipc_free_security(&shp->shm_perm); 49821da177e4SLinus Torvalds } 49831da177e4SLinus Torvalds 49841da177e4SLinus Torvalds static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg) 49851da177e4SLinus Torvalds { 49861da177e4SLinus Torvalds struct ipc_security_struct *isec; 49872bf49690SThomas Liu struct common_audit_data ad; 4988275bb41eSDavid Howells u32 sid = current_sid(); 49891da177e4SLinus Torvalds 49901da177e4SLinus Torvalds isec = shp->shm_perm.security; 49911da177e4SLinus Torvalds 49922bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, IPC); 49931da177e4SLinus Torvalds ad.u.ipc_id = shp->shm_perm.key; 49941da177e4SLinus Torvalds 4995275bb41eSDavid Howells return avc_has_perm(sid, isec->sid, SECCLASS_SHM, 49961da177e4SLinus Torvalds SHM__ASSOCIATE, &ad); 49971da177e4SLinus Torvalds } 49981da177e4SLinus Torvalds 49991da177e4SLinus Torvalds /* Note, at this point, shp is locked down */ 50001da177e4SLinus Torvalds static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd) 50011da177e4SLinus Torvalds { 50021da177e4SLinus Torvalds int perms; 50031da177e4SLinus Torvalds int err; 50041da177e4SLinus Torvalds 50051da177e4SLinus Torvalds switch (cmd) { 50061da177e4SLinus Torvalds case IPC_INFO: 50071da177e4SLinus Torvalds case SHM_INFO: 50081da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 50091da177e4SLinus Torvalds return task_has_system(current, SYSTEM__IPC_INFO); 50101da177e4SLinus Torvalds case IPC_STAT: 50111da177e4SLinus Torvalds case SHM_STAT: 50121da177e4SLinus Torvalds perms = SHM__GETATTR | SHM__ASSOCIATE; 50131da177e4SLinus Torvalds break; 50141da177e4SLinus Torvalds case IPC_SET: 50151da177e4SLinus Torvalds perms = SHM__SETATTR; 50161da177e4SLinus Torvalds break; 50171da177e4SLinus Torvalds case SHM_LOCK: 50181da177e4SLinus Torvalds case SHM_UNLOCK: 50191da177e4SLinus Torvalds perms = SHM__LOCK; 50201da177e4SLinus Torvalds break; 50211da177e4SLinus Torvalds case IPC_RMID: 50221da177e4SLinus Torvalds perms = SHM__DESTROY; 50231da177e4SLinus Torvalds break; 50241da177e4SLinus Torvalds default: 50251da177e4SLinus Torvalds return 0; 50261da177e4SLinus Torvalds } 50271da177e4SLinus Torvalds 50286af963f1SStephen Smalley err = ipc_has_perm(&shp->shm_perm, perms); 50291da177e4SLinus Torvalds return err; 50301da177e4SLinus Torvalds } 50311da177e4SLinus Torvalds 50321da177e4SLinus Torvalds static int selinux_shm_shmat(struct shmid_kernel *shp, 50331da177e4SLinus Torvalds char __user *shmaddr, int shmflg) 50341da177e4SLinus Torvalds { 50351da177e4SLinus Torvalds u32 perms; 50361da177e4SLinus Torvalds 50371da177e4SLinus Torvalds if (shmflg & SHM_RDONLY) 50381da177e4SLinus Torvalds perms = SHM__READ; 50391da177e4SLinus Torvalds else 50401da177e4SLinus Torvalds perms = SHM__READ | SHM__WRITE; 50411da177e4SLinus Torvalds 50426af963f1SStephen Smalley return ipc_has_perm(&shp->shm_perm, perms); 50431da177e4SLinus Torvalds } 50441da177e4SLinus Torvalds 50451da177e4SLinus Torvalds /* Semaphore security operations */ 50461da177e4SLinus Torvalds static int selinux_sem_alloc_security(struct sem_array *sma) 50471da177e4SLinus Torvalds { 50481da177e4SLinus Torvalds struct ipc_security_struct *isec; 50492bf49690SThomas Liu struct common_audit_data ad; 5050275bb41eSDavid Howells u32 sid = current_sid(); 50511da177e4SLinus Torvalds int rc; 50521da177e4SLinus Torvalds 50531da177e4SLinus Torvalds rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM); 50541da177e4SLinus Torvalds if (rc) 50551da177e4SLinus Torvalds return rc; 50561da177e4SLinus Torvalds 50571da177e4SLinus Torvalds isec = sma->sem_perm.security; 50581da177e4SLinus Torvalds 50592bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, IPC); 50601da177e4SLinus Torvalds ad.u.ipc_id = sma->sem_perm.key; 50611da177e4SLinus Torvalds 5062275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, SECCLASS_SEM, 50631da177e4SLinus Torvalds SEM__CREATE, &ad); 50641da177e4SLinus Torvalds if (rc) { 50651da177e4SLinus Torvalds ipc_free_security(&sma->sem_perm); 50661da177e4SLinus Torvalds return rc; 50671da177e4SLinus Torvalds } 50681da177e4SLinus Torvalds return 0; 50691da177e4SLinus Torvalds } 50701da177e4SLinus Torvalds 50711da177e4SLinus Torvalds static void selinux_sem_free_security(struct sem_array *sma) 50721da177e4SLinus Torvalds { 50731da177e4SLinus Torvalds ipc_free_security(&sma->sem_perm); 50741da177e4SLinus Torvalds } 50751da177e4SLinus Torvalds 50761da177e4SLinus Torvalds static int selinux_sem_associate(struct sem_array *sma, int semflg) 50771da177e4SLinus Torvalds { 50781da177e4SLinus Torvalds struct ipc_security_struct *isec; 50792bf49690SThomas Liu struct common_audit_data ad; 5080275bb41eSDavid Howells u32 sid = current_sid(); 50811da177e4SLinus Torvalds 50821da177e4SLinus Torvalds isec = sma->sem_perm.security; 50831da177e4SLinus Torvalds 50842bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, IPC); 50851da177e4SLinus Torvalds ad.u.ipc_id = sma->sem_perm.key; 50861da177e4SLinus Torvalds 5087275bb41eSDavid Howells return avc_has_perm(sid, isec->sid, SECCLASS_SEM, 50881da177e4SLinus Torvalds SEM__ASSOCIATE, &ad); 50891da177e4SLinus Torvalds } 50901da177e4SLinus Torvalds 50911da177e4SLinus Torvalds /* Note, at this point, sma is locked down */ 50921da177e4SLinus Torvalds static int selinux_sem_semctl(struct sem_array *sma, int cmd) 50931da177e4SLinus Torvalds { 50941da177e4SLinus Torvalds int err; 50951da177e4SLinus Torvalds u32 perms; 50961da177e4SLinus Torvalds 50971da177e4SLinus Torvalds switch (cmd) { 50981da177e4SLinus Torvalds case IPC_INFO: 50991da177e4SLinus Torvalds case SEM_INFO: 51001da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 51011da177e4SLinus Torvalds return task_has_system(current, SYSTEM__IPC_INFO); 51021da177e4SLinus Torvalds case GETPID: 51031da177e4SLinus Torvalds case GETNCNT: 51041da177e4SLinus Torvalds case GETZCNT: 51051da177e4SLinus Torvalds perms = SEM__GETATTR; 51061da177e4SLinus Torvalds break; 51071da177e4SLinus Torvalds case GETVAL: 51081da177e4SLinus Torvalds case GETALL: 51091da177e4SLinus Torvalds perms = SEM__READ; 51101da177e4SLinus Torvalds break; 51111da177e4SLinus Torvalds case SETVAL: 51121da177e4SLinus Torvalds case SETALL: 51131da177e4SLinus Torvalds perms = SEM__WRITE; 51141da177e4SLinus Torvalds break; 51151da177e4SLinus Torvalds case IPC_RMID: 51161da177e4SLinus Torvalds perms = SEM__DESTROY; 51171da177e4SLinus Torvalds break; 51181da177e4SLinus Torvalds case IPC_SET: 51191da177e4SLinus Torvalds perms = SEM__SETATTR; 51201da177e4SLinus Torvalds break; 51211da177e4SLinus Torvalds case IPC_STAT: 51221da177e4SLinus Torvalds case SEM_STAT: 51231da177e4SLinus Torvalds perms = SEM__GETATTR | SEM__ASSOCIATE; 51241da177e4SLinus Torvalds break; 51251da177e4SLinus Torvalds default: 51261da177e4SLinus Torvalds return 0; 51271da177e4SLinus Torvalds } 51281da177e4SLinus Torvalds 51296af963f1SStephen Smalley err = ipc_has_perm(&sma->sem_perm, perms); 51301da177e4SLinus Torvalds return err; 51311da177e4SLinus Torvalds } 51321da177e4SLinus Torvalds 51331da177e4SLinus Torvalds static int selinux_sem_semop(struct sem_array *sma, 51341da177e4SLinus Torvalds struct sembuf *sops, unsigned nsops, int alter) 51351da177e4SLinus Torvalds { 51361da177e4SLinus Torvalds u32 perms; 51371da177e4SLinus Torvalds 51381da177e4SLinus Torvalds if (alter) 51391da177e4SLinus Torvalds perms = SEM__READ | SEM__WRITE; 51401da177e4SLinus Torvalds else 51411da177e4SLinus Torvalds perms = SEM__READ; 51421da177e4SLinus Torvalds 51436af963f1SStephen Smalley return ipc_has_perm(&sma->sem_perm, perms); 51441da177e4SLinus Torvalds } 51451da177e4SLinus Torvalds 51461da177e4SLinus Torvalds static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag) 51471da177e4SLinus Torvalds { 51481da177e4SLinus Torvalds u32 av = 0; 51491da177e4SLinus Torvalds 51501da177e4SLinus Torvalds av = 0; 51511da177e4SLinus Torvalds if (flag & S_IRUGO) 51521da177e4SLinus Torvalds av |= IPC__UNIX_READ; 51531da177e4SLinus Torvalds if (flag & S_IWUGO) 51541da177e4SLinus Torvalds av |= IPC__UNIX_WRITE; 51551da177e4SLinus Torvalds 51561da177e4SLinus Torvalds if (av == 0) 51571da177e4SLinus Torvalds return 0; 51581da177e4SLinus Torvalds 51596af963f1SStephen Smalley return ipc_has_perm(ipcp, av); 51601da177e4SLinus Torvalds } 51611da177e4SLinus Torvalds 5162713a04aeSAhmed S. Darwish static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid) 5163713a04aeSAhmed S. Darwish { 5164713a04aeSAhmed S. Darwish struct ipc_security_struct *isec = ipcp->security; 5165713a04aeSAhmed S. Darwish *secid = isec->sid; 5166713a04aeSAhmed S. Darwish } 5167713a04aeSAhmed S. Darwish 51681da177e4SLinus Torvalds static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode) 51691da177e4SLinus Torvalds { 51701da177e4SLinus Torvalds if (inode) 51711da177e4SLinus Torvalds inode_doinit_with_dentry(inode, dentry); 51721da177e4SLinus Torvalds } 51731da177e4SLinus Torvalds 51741da177e4SLinus Torvalds static int selinux_getprocattr(struct task_struct *p, 517504ff9708SAl Viro char *name, char **value) 51761da177e4SLinus Torvalds { 5177275bb41eSDavid Howells const struct task_security_struct *__tsec; 51788c8570fbSDustin Kirkland u32 sid; 51791da177e4SLinus Torvalds int error; 518004ff9708SAl Viro unsigned len; 51811da177e4SLinus Torvalds 51821da177e4SLinus Torvalds if (current != p) { 51833b11a1deSDavid Howells error = current_has_perm(p, PROCESS__GETATTR); 51841da177e4SLinus Torvalds if (error) 51851da177e4SLinus Torvalds return error; 51861da177e4SLinus Torvalds } 51871da177e4SLinus Torvalds 5188275bb41eSDavid Howells rcu_read_lock(); 5189275bb41eSDavid Howells __tsec = __task_cred(p)->security; 51901da177e4SLinus Torvalds 51911da177e4SLinus Torvalds if (!strcmp(name, "current")) 5192275bb41eSDavid Howells sid = __tsec->sid; 51931da177e4SLinus Torvalds else if (!strcmp(name, "prev")) 5194275bb41eSDavid Howells sid = __tsec->osid; 51951da177e4SLinus Torvalds else if (!strcmp(name, "exec")) 5196275bb41eSDavid Howells sid = __tsec->exec_sid; 51971da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 5198275bb41eSDavid Howells sid = __tsec->create_sid; 51994eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) 5200275bb41eSDavid Howells sid = __tsec->keycreate_sid; 520142c3e03eSEric Paris else if (!strcmp(name, "sockcreate")) 5202275bb41eSDavid Howells sid = __tsec->sockcreate_sid; 52031da177e4SLinus Torvalds else 5204275bb41eSDavid Howells goto invalid; 5205275bb41eSDavid Howells rcu_read_unlock(); 52061da177e4SLinus Torvalds 52071da177e4SLinus Torvalds if (!sid) 52081da177e4SLinus Torvalds return 0; 52091da177e4SLinus Torvalds 521004ff9708SAl Viro error = security_sid_to_context(sid, value, &len); 521104ff9708SAl Viro if (error) 521204ff9708SAl Viro return error; 521304ff9708SAl Viro return len; 5214275bb41eSDavid Howells 5215275bb41eSDavid Howells invalid: 5216275bb41eSDavid Howells rcu_read_unlock(); 5217275bb41eSDavid Howells return -EINVAL; 52181da177e4SLinus Torvalds } 52191da177e4SLinus Torvalds 52201da177e4SLinus Torvalds static int selinux_setprocattr(struct task_struct *p, 52211da177e4SLinus Torvalds char *name, void *value, size_t size) 52221da177e4SLinus Torvalds { 52231da177e4SLinus Torvalds struct task_security_struct *tsec; 52240356357cSRoland McGrath struct task_struct *tracer; 5225d84f4f99SDavid Howells struct cred *new; 5226d84f4f99SDavid Howells u32 sid = 0, ptsid; 52271da177e4SLinus Torvalds int error; 52281da177e4SLinus Torvalds char *str = value; 52291da177e4SLinus Torvalds 52301da177e4SLinus Torvalds if (current != p) { 52311da177e4SLinus Torvalds /* SELinux only allows a process to change its own 52321da177e4SLinus Torvalds security attributes. */ 52331da177e4SLinus Torvalds return -EACCES; 52341da177e4SLinus Torvalds } 52351da177e4SLinus Torvalds 52361da177e4SLinus Torvalds /* 52371da177e4SLinus Torvalds * Basic control over ability to set these attributes at all. 52381da177e4SLinus Torvalds * current == p, but we'll pass them separately in case the 52391da177e4SLinus Torvalds * above restriction is ever removed. 52401da177e4SLinus Torvalds */ 52411da177e4SLinus Torvalds if (!strcmp(name, "exec")) 52423b11a1deSDavid Howells error = current_has_perm(p, PROCESS__SETEXEC); 52431da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 52443b11a1deSDavid Howells error = current_has_perm(p, PROCESS__SETFSCREATE); 52454eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) 52463b11a1deSDavid Howells error = current_has_perm(p, PROCESS__SETKEYCREATE); 524742c3e03eSEric Paris else if (!strcmp(name, "sockcreate")) 52483b11a1deSDavid Howells error = current_has_perm(p, PROCESS__SETSOCKCREATE); 52491da177e4SLinus Torvalds else if (!strcmp(name, "current")) 52503b11a1deSDavid Howells error = current_has_perm(p, PROCESS__SETCURRENT); 52511da177e4SLinus Torvalds else 52521da177e4SLinus Torvalds error = -EINVAL; 52531da177e4SLinus Torvalds if (error) 52541da177e4SLinus Torvalds return error; 52551da177e4SLinus Torvalds 52561da177e4SLinus Torvalds /* Obtain a SID for the context, if one was specified. */ 52571da177e4SLinus Torvalds if (size && str[1] && str[1] != '\n') { 52581da177e4SLinus Torvalds if (str[size-1] == '\n') { 52591da177e4SLinus Torvalds str[size-1] = 0; 52601da177e4SLinus Torvalds size--; 52611da177e4SLinus Torvalds } 52621da177e4SLinus Torvalds error = security_context_to_sid(value, size, &sid); 526312b29f34SStephen Smalley if (error == -EINVAL && !strcmp(name, "fscreate")) { 526412b29f34SStephen Smalley if (!capable(CAP_MAC_ADMIN)) 526512b29f34SStephen Smalley return error; 526612b29f34SStephen Smalley error = security_context_to_sid_force(value, size, 526712b29f34SStephen Smalley &sid); 526812b29f34SStephen Smalley } 52691da177e4SLinus Torvalds if (error) 52701da177e4SLinus Torvalds return error; 52711da177e4SLinus Torvalds } 52721da177e4SLinus Torvalds 5273d84f4f99SDavid Howells new = prepare_creds(); 5274d84f4f99SDavid Howells if (!new) 5275d84f4f99SDavid Howells return -ENOMEM; 5276d84f4f99SDavid Howells 52771da177e4SLinus Torvalds /* Permission checking based on the specified context is 52781da177e4SLinus Torvalds performed during the actual operation (execve, 52791da177e4SLinus Torvalds open/mkdir/...), when we know the full context of the 5280d84f4f99SDavid Howells operation. See selinux_bprm_set_creds for the execve 52811da177e4SLinus Torvalds checks and may_create for the file creation checks. The 52821da177e4SLinus Torvalds operation will then fail if the context is not permitted. */ 5283d84f4f99SDavid Howells tsec = new->security; 5284d84f4f99SDavid Howells if (!strcmp(name, "exec")) { 52851da177e4SLinus Torvalds tsec->exec_sid = sid; 5286d84f4f99SDavid Howells } else if (!strcmp(name, "fscreate")) { 52871da177e4SLinus Torvalds tsec->create_sid = sid; 5288d84f4f99SDavid Howells } else if (!strcmp(name, "keycreate")) { 52894eb582cfSMichael LeMay error = may_create_key(sid, p); 52904eb582cfSMichael LeMay if (error) 5291d84f4f99SDavid Howells goto abort_change; 52924eb582cfSMichael LeMay tsec->keycreate_sid = sid; 5293d84f4f99SDavid Howells } else if (!strcmp(name, "sockcreate")) { 529442c3e03eSEric Paris tsec->sockcreate_sid = sid; 5295d84f4f99SDavid Howells } else if (!strcmp(name, "current")) { 5296d84f4f99SDavid Howells error = -EINVAL; 52971da177e4SLinus Torvalds if (sid == 0) 5298d84f4f99SDavid Howells goto abort_change; 5299d9250deaSKaiGai Kohei 5300d84f4f99SDavid Howells /* Only allow single threaded processes to change context */ 5301d84f4f99SDavid Howells error = -EPERM; 53025bb459bbSOleg Nesterov if (!current_is_single_threaded()) { 5303d84f4f99SDavid Howells error = security_bounded_transition(tsec->sid, sid); 5304d84f4f99SDavid Howells if (error) 5305d84f4f99SDavid Howells goto abort_change; 53061da177e4SLinus Torvalds } 53071da177e4SLinus Torvalds 53081da177e4SLinus Torvalds /* Check permissions for the transition. */ 53091da177e4SLinus Torvalds error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS, 53101da177e4SLinus Torvalds PROCESS__DYNTRANSITION, NULL); 53111da177e4SLinus Torvalds if (error) 5312d84f4f99SDavid Howells goto abort_change; 53131da177e4SLinus Torvalds 53141da177e4SLinus Torvalds /* Check for ptracing, and update the task SID if ok. 53151da177e4SLinus Torvalds Otherwise, leave SID unchanged and fail. */ 5316d84f4f99SDavid Howells ptsid = 0; 53171da177e4SLinus Torvalds task_lock(p); 53180d094efeSRoland McGrath tracer = tracehook_tracer_task(p); 5319d84f4f99SDavid Howells if (tracer) 5320d84f4f99SDavid Howells ptsid = task_sid(tracer); 53211da177e4SLinus Torvalds task_unlock(p); 53221da177e4SLinus Torvalds 5323d84f4f99SDavid Howells if (tracer) { 5324d84f4f99SDavid Howells error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS, 5325d84f4f99SDavid Howells PROCESS__PTRACE, NULL); 5326d84f4f99SDavid Howells if (error) 5327d84f4f99SDavid Howells goto abort_change; 5328d84f4f99SDavid Howells } 5329d84f4f99SDavid Howells 5330d84f4f99SDavid Howells tsec->sid = sid; 5331d84f4f99SDavid Howells } else { 5332d84f4f99SDavid Howells error = -EINVAL; 5333d84f4f99SDavid Howells goto abort_change; 5334d84f4f99SDavid Howells } 5335d84f4f99SDavid Howells 5336d84f4f99SDavid Howells commit_creds(new); 53371da177e4SLinus Torvalds return size; 5338d84f4f99SDavid Howells 5339d84f4f99SDavid Howells abort_change: 5340d84f4f99SDavid Howells abort_creds(new); 5341d84f4f99SDavid Howells return error; 53421da177e4SLinus Torvalds } 53431da177e4SLinus Torvalds 5344dc49c1f9SCatherine Zhang static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) 5345dc49c1f9SCatherine Zhang { 5346dc49c1f9SCatherine Zhang return security_sid_to_context(secid, secdata, seclen); 5347dc49c1f9SCatherine Zhang } 5348dc49c1f9SCatherine Zhang 53497bf570dcSDavid Howells static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid) 535063cb3449SDavid Howells { 535163cb3449SDavid Howells return security_context_to_sid(secdata, seclen, secid); 535263cb3449SDavid Howells } 535363cb3449SDavid Howells 5354dc49c1f9SCatherine Zhang static void selinux_release_secctx(char *secdata, u32 seclen) 5355dc49c1f9SCatherine Zhang { 5356dc49c1f9SCatherine Zhang kfree(secdata); 5357dc49c1f9SCatherine Zhang } 5358dc49c1f9SCatherine Zhang 53591ee65e37SDavid P. Quigley /* 53601ee65e37SDavid P. Quigley * called with inode->i_mutex locked 53611ee65e37SDavid P. Quigley */ 53621ee65e37SDavid P. Quigley static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen) 53631ee65e37SDavid P. Quigley { 53641ee65e37SDavid P. Quigley return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0); 53651ee65e37SDavid P. Quigley } 53661ee65e37SDavid P. Quigley 53671ee65e37SDavid P. Quigley /* 53681ee65e37SDavid P. Quigley * called with inode->i_mutex locked 53691ee65e37SDavid P. Quigley */ 53701ee65e37SDavid P. Quigley static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen) 53711ee65e37SDavid P. Quigley { 53721ee65e37SDavid P. Quigley return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0); 53731ee65e37SDavid P. Quigley } 53741ee65e37SDavid P. Quigley 53751ee65e37SDavid P. Quigley static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen) 53761ee65e37SDavid P. Quigley { 53771ee65e37SDavid P. Quigley int len = 0; 53781ee65e37SDavid P. Quigley len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX, 53791ee65e37SDavid P. Quigley ctx, true); 53801ee65e37SDavid P. Quigley if (len < 0) 53811ee65e37SDavid P. Quigley return len; 53821ee65e37SDavid P. Quigley *ctxlen = len; 53831ee65e37SDavid P. Quigley return 0; 53841ee65e37SDavid P. Quigley } 5385d720024eSMichael LeMay #ifdef CONFIG_KEYS 5386d720024eSMichael LeMay 5387d84f4f99SDavid Howells static int selinux_key_alloc(struct key *k, const struct cred *cred, 53887e047ef5SDavid Howells unsigned long flags) 5389d720024eSMichael LeMay { 5390d84f4f99SDavid Howells const struct task_security_struct *tsec; 5391d720024eSMichael LeMay struct key_security_struct *ksec; 5392d720024eSMichael LeMay 5393d720024eSMichael LeMay ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL); 5394d720024eSMichael LeMay if (!ksec) 5395d720024eSMichael LeMay return -ENOMEM; 5396d720024eSMichael LeMay 5397d84f4f99SDavid Howells tsec = cred->security; 5398d84f4f99SDavid Howells if (tsec->keycreate_sid) 5399d84f4f99SDavid Howells ksec->sid = tsec->keycreate_sid; 54004eb582cfSMichael LeMay else 5401d84f4f99SDavid Howells ksec->sid = tsec->sid; 5402d720024eSMichael LeMay 5403275bb41eSDavid Howells k->security = ksec; 5404d720024eSMichael LeMay return 0; 5405d720024eSMichael LeMay } 5406d720024eSMichael LeMay 5407d720024eSMichael LeMay static void selinux_key_free(struct key *k) 5408d720024eSMichael LeMay { 5409d720024eSMichael LeMay struct key_security_struct *ksec = k->security; 5410d720024eSMichael LeMay 5411d720024eSMichael LeMay k->security = NULL; 5412d720024eSMichael LeMay kfree(ksec); 5413d720024eSMichael LeMay } 5414d720024eSMichael LeMay 5415d720024eSMichael LeMay static int selinux_key_permission(key_ref_t key_ref, 5416d84f4f99SDavid Howells const struct cred *cred, 5417d720024eSMichael LeMay key_perm_t perm) 5418d720024eSMichael LeMay { 5419d720024eSMichael LeMay struct key *key; 5420d720024eSMichael LeMay struct key_security_struct *ksec; 5421275bb41eSDavid Howells u32 sid; 5422d720024eSMichael LeMay 5423d720024eSMichael LeMay /* if no specific permissions are requested, we skip the 5424d720024eSMichael LeMay permission check. No serious, additional covert channels 5425d720024eSMichael LeMay appear to be created. */ 5426d720024eSMichael LeMay if (perm == 0) 5427d720024eSMichael LeMay return 0; 5428d720024eSMichael LeMay 5429d84f4f99SDavid Howells sid = cred_sid(cred); 5430275bb41eSDavid Howells 5431275bb41eSDavid Howells key = key_ref_to_ptr(key_ref); 5432275bb41eSDavid Howells ksec = key->security; 5433275bb41eSDavid Howells 5434275bb41eSDavid Howells return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL); 5435d720024eSMichael LeMay } 5436d720024eSMichael LeMay 543770a5bb72SDavid Howells static int selinux_key_getsecurity(struct key *key, char **_buffer) 543870a5bb72SDavid Howells { 543970a5bb72SDavid Howells struct key_security_struct *ksec = key->security; 544070a5bb72SDavid Howells char *context = NULL; 544170a5bb72SDavid Howells unsigned len; 544270a5bb72SDavid Howells int rc; 544370a5bb72SDavid Howells 544470a5bb72SDavid Howells rc = security_sid_to_context(ksec->sid, &context, &len); 544570a5bb72SDavid Howells if (!rc) 544670a5bb72SDavid Howells rc = len; 544770a5bb72SDavid Howells *_buffer = context; 544870a5bb72SDavid Howells return rc; 544970a5bb72SDavid Howells } 545070a5bb72SDavid Howells 5451d720024eSMichael LeMay #endif 5452d720024eSMichael LeMay 54531da177e4SLinus Torvalds static struct security_operations selinux_ops = { 5454076c54c5SAhmed S. Darwish .name = "selinux", 5455076c54c5SAhmed S. Darwish 54569e48858fSIngo Molnar .ptrace_access_check = selinux_ptrace_access_check, 54575cd9c58fSDavid Howells .ptrace_traceme = selinux_ptrace_traceme, 54581da177e4SLinus Torvalds .capget = selinux_capget, 5459d84f4f99SDavid Howells .capset = selinux_capset, 54601da177e4SLinus Torvalds .sysctl = selinux_sysctl, 54611da177e4SLinus Torvalds .capable = selinux_capable, 54621da177e4SLinus Torvalds .quotactl = selinux_quotactl, 54631da177e4SLinus Torvalds .quota_on = selinux_quota_on, 54641da177e4SLinus Torvalds .syslog = selinux_syslog, 54651da177e4SLinus Torvalds .vm_enough_memory = selinux_vm_enough_memory, 54661da177e4SLinus Torvalds 54671da177e4SLinus Torvalds .netlink_send = selinux_netlink_send, 54681da177e4SLinus Torvalds .netlink_recv = selinux_netlink_recv, 54691da177e4SLinus Torvalds 5470a6f76f23SDavid Howells .bprm_set_creds = selinux_bprm_set_creds, 5471a6f76f23SDavid Howells .bprm_committing_creds = selinux_bprm_committing_creds, 5472a6f76f23SDavid Howells .bprm_committed_creds = selinux_bprm_committed_creds, 54731da177e4SLinus Torvalds .bprm_secureexec = selinux_bprm_secureexec, 54741da177e4SLinus Torvalds 54751da177e4SLinus Torvalds .sb_alloc_security = selinux_sb_alloc_security, 54761da177e4SLinus Torvalds .sb_free_security = selinux_sb_free_security, 54771da177e4SLinus Torvalds .sb_copy_data = selinux_sb_copy_data, 54781da177e4SLinus Torvalds .sb_kern_mount = selinux_sb_kern_mount, 54792069f457SEric Paris .sb_show_options = selinux_sb_show_options, 54801da177e4SLinus Torvalds .sb_statfs = selinux_sb_statfs, 54811da177e4SLinus Torvalds .sb_mount = selinux_mount, 54821da177e4SLinus Torvalds .sb_umount = selinux_umount, 5483c9180a57SEric Paris .sb_set_mnt_opts = selinux_set_mnt_opts, 5484c9180a57SEric Paris .sb_clone_mnt_opts = selinux_sb_clone_mnt_opts, 5485e0007529SEric Paris .sb_parse_opts_str = selinux_parse_opts_str, 5486e0007529SEric Paris 54871da177e4SLinus Torvalds 54881da177e4SLinus Torvalds .inode_alloc_security = selinux_inode_alloc_security, 54891da177e4SLinus Torvalds .inode_free_security = selinux_inode_free_security, 54905e41ff9eSStephen Smalley .inode_init_security = selinux_inode_init_security, 54911da177e4SLinus Torvalds .inode_create = selinux_inode_create, 54921da177e4SLinus Torvalds .inode_link = selinux_inode_link, 54931da177e4SLinus Torvalds .inode_unlink = selinux_inode_unlink, 54941da177e4SLinus Torvalds .inode_symlink = selinux_inode_symlink, 54951da177e4SLinus Torvalds .inode_mkdir = selinux_inode_mkdir, 54961da177e4SLinus Torvalds .inode_rmdir = selinux_inode_rmdir, 54971da177e4SLinus Torvalds .inode_mknod = selinux_inode_mknod, 54981da177e4SLinus Torvalds .inode_rename = selinux_inode_rename, 54991da177e4SLinus Torvalds .inode_readlink = selinux_inode_readlink, 55001da177e4SLinus Torvalds .inode_follow_link = selinux_inode_follow_link, 55011da177e4SLinus Torvalds .inode_permission = selinux_inode_permission, 55021da177e4SLinus Torvalds .inode_setattr = selinux_inode_setattr, 55031da177e4SLinus Torvalds .inode_getattr = selinux_inode_getattr, 55041da177e4SLinus Torvalds .inode_setxattr = selinux_inode_setxattr, 55051da177e4SLinus Torvalds .inode_post_setxattr = selinux_inode_post_setxattr, 55061da177e4SLinus Torvalds .inode_getxattr = selinux_inode_getxattr, 55071da177e4SLinus Torvalds .inode_listxattr = selinux_inode_listxattr, 55081da177e4SLinus Torvalds .inode_removexattr = selinux_inode_removexattr, 55091da177e4SLinus Torvalds .inode_getsecurity = selinux_inode_getsecurity, 55101da177e4SLinus Torvalds .inode_setsecurity = selinux_inode_setsecurity, 55111da177e4SLinus Torvalds .inode_listsecurity = selinux_inode_listsecurity, 5512713a04aeSAhmed S. Darwish .inode_getsecid = selinux_inode_getsecid, 55131da177e4SLinus Torvalds 55141da177e4SLinus Torvalds .file_permission = selinux_file_permission, 55151da177e4SLinus Torvalds .file_alloc_security = selinux_file_alloc_security, 55161da177e4SLinus Torvalds .file_free_security = selinux_file_free_security, 55171da177e4SLinus Torvalds .file_ioctl = selinux_file_ioctl, 55181da177e4SLinus Torvalds .file_mmap = selinux_file_mmap, 55191da177e4SLinus Torvalds .file_mprotect = selinux_file_mprotect, 55201da177e4SLinus Torvalds .file_lock = selinux_file_lock, 55211da177e4SLinus Torvalds .file_fcntl = selinux_file_fcntl, 55221da177e4SLinus Torvalds .file_set_fowner = selinux_file_set_fowner, 55231da177e4SLinus Torvalds .file_send_sigiotask = selinux_file_send_sigiotask, 55241da177e4SLinus Torvalds .file_receive = selinux_file_receive, 55251da177e4SLinus Torvalds 5526788e7dd4SYuichi Nakamura .dentry_open = selinux_dentry_open, 5527788e7dd4SYuichi Nakamura 55281da177e4SLinus Torvalds .task_create = selinux_task_create, 5529ee18d64cSDavid Howells .cred_alloc_blank = selinux_cred_alloc_blank, 5530f1752eecSDavid Howells .cred_free = selinux_cred_free, 5531d84f4f99SDavid Howells .cred_prepare = selinux_cred_prepare, 5532ee18d64cSDavid Howells .cred_transfer = selinux_cred_transfer, 55333a3b7ce9SDavid Howells .kernel_act_as = selinux_kernel_act_as, 55343a3b7ce9SDavid Howells .kernel_create_files_as = selinux_kernel_create_files_as, 553525354c4fSEric Paris .kernel_module_request = selinux_kernel_module_request, 55361da177e4SLinus Torvalds .task_setpgid = selinux_task_setpgid, 55371da177e4SLinus Torvalds .task_getpgid = selinux_task_getpgid, 55381da177e4SLinus Torvalds .task_getsid = selinux_task_getsid, 5539f9008e4cSDavid Quigley .task_getsecid = selinux_task_getsecid, 55401da177e4SLinus Torvalds .task_setnice = selinux_task_setnice, 554103e68060SJames Morris .task_setioprio = selinux_task_setioprio, 5542a1836a42SDavid Quigley .task_getioprio = selinux_task_getioprio, 55431da177e4SLinus Torvalds .task_setrlimit = selinux_task_setrlimit, 55441da177e4SLinus Torvalds .task_setscheduler = selinux_task_setscheduler, 55451da177e4SLinus Torvalds .task_getscheduler = selinux_task_getscheduler, 554635601547SDavid Quigley .task_movememory = selinux_task_movememory, 55471da177e4SLinus Torvalds .task_kill = selinux_task_kill, 55481da177e4SLinus Torvalds .task_wait = selinux_task_wait, 55491da177e4SLinus Torvalds .task_to_inode = selinux_task_to_inode, 55501da177e4SLinus Torvalds 55511da177e4SLinus Torvalds .ipc_permission = selinux_ipc_permission, 5552713a04aeSAhmed S. Darwish .ipc_getsecid = selinux_ipc_getsecid, 55531da177e4SLinus Torvalds 55541da177e4SLinus Torvalds .msg_msg_alloc_security = selinux_msg_msg_alloc_security, 55551da177e4SLinus Torvalds .msg_msg_free_security = selinux_msg_msg_free_security, 55561da177e4SLinus Torvalds 55571da177e4SLinus Torvalds .msg_queue_alloc_security = selinux_msg_queue_alloc_security, 55581da177e4SLinus Torvalds .msg_queue_free_security = selinux_msg_queue_free_security, 55591da177e4SLinus Torvalds .msg_queue_associate = selinux_msg_queue_associate, 55601da177e4SLinus Torvalds .msg_queue_msgctl = selinux_msg_queue_msgctl, 55611da177e4SLinus Torvalds .msg_queue_msgsnd = selinux_msg_queue_msgsnd, 55621da177e4SLinus Torvalds .msg_queue_msgrcv = selinux_msg_queue_msgrcv, 55631da177e4SLinus Torvalds 55641da177e4SLinus Torvalds .shm_alloc_security = selinux_shm_alloc_security, 55651da177e4SLinus Torvalds .shm_free_security = selinux_shm_free_security, 55661da177e4SLinus Torvalds .shm_associate = selinux_shm_associate, 55671da177e4SLinus Torvalds .shm_shmctl = selinux_shm_shmctl, 55681da177e4SLinus Torvalds .shm_shmat = selinux_shm_shmat, 55691da177e4SLinus Torvalds 55701da177e4SLinus Torvalds .sem_alloc_security = selinux_sem_alloc_security, 55711da177e4SLinus Torvalds .sem_free_security = selinux_sem_free_security, 55721da177e4SLinus Torvalds .sem_associate = selinux_sem_associate, 55731da177e4SLinus Torvalds .sem_semctl = selinux_sem_semctl, 55741da177e4SLinus Torvalds .sem_semop = selinux_sem_semop, 55751da177e4SLinus Torvalds 55761da177e4SLinus Torvalds .d_instantiate = selinux_d_instantiate, 55771da177e4SLinus Torvalds 55781da177e4SLinus Torvalds .getprocattr = selinux_getprocattr, 55791da177e4SLinus Torvalds .setprocattr = selinux_setprocattr, 55801da177e4SLinus Torvalds 5581dc49c1f9SCatherine Zhang .secid_to_secctx = selinux_secid_to_secctx, 558263cb3449SDavid Howells .secctx_to_secid = selinux_secctx_to_secid, 5583dc49c1f9SCatherine Zhang .release_secctx = selinux_release_secctx, 55841ee65e37SDavid P. Quigley .inode_notifysecctx = selinux_inode_notifysecctx, 55851ee65e37SDavid P. Quigley .inode_setsecctx = selinux_inode_setsecctx, 55861ee65e37SDavid P. Quigley .inode_getsecctx = selinux_inode_getsecctx, 5587dc49c1f9SCatherine Zhang 55881da177e4SLinus Torvalds .unix_stream_connect = selinux_socket_unix_stream_connect, 55891da177e4SLinus Torvalds .unix_may_send = selinux_socket_unix_may_send, 55901da177e4SLinus Torvalds 55911da177e4SLinus Torvalds .socket_create = selinux_socket_create, 55921da177e4SLinus Torvalds .socket_post_create = selinux_socket_post_create, 55931da177e4SLinus Torvalds .socket_bind = selinux_socket_bind, 55941da177e4SLinus Torvalds .socket_connect = selinux_socket_connect, 55951da177e4SLinus Torvalds .socket_listen = selinux_socket_listen, 55961da177e4SLinus Torvalds .socket_accept = selinux_socket_accept, 55971da177e4SLinus Torvalds .socket_sendmsg = selinux_socket_sendmsg, 55981da177e4SLinus Torvalds .socket_recvmsg = selinux_socket_recvmsg, 55991da177e4SLinus Torvalds .socket_getsockname = selinux_socket_getsockname, 56001da177e4SLinus Torvalds .socket_getpeername = selinux_socket_getpeername, 56011da177e4SLinus Torvalds .socket_getsockopt = selinux_socket_getsockopt, 56021da177e4SLinus Torvalds .socket_setsockopt = selinux_socket_setsockopt, 56031da177e4SLinus Torvalds .socket_shutdown = selinux_socket_shutdown, 56041da177e4SLinus Torvalds .socket_sock_rcv_skb = selinux_socket_sock_rcv_skb, 56052c7946a7SCatherine Zhang .socket_getpeersec_stream = selinux_socket_getpeersec_stream, 56062c7946a7SCatherine Zhang .socket_getpeersec_dgram = selinux_socket_getpeersec_dgram, 56071da177e4SLinus Torvalds .sk_alloc_security = selinux_sk_alloc_security, 56081da177e4SLinus Torvalds .sk_free_security = selinux_sk_free_security, 5609892c141eSVenkat Yekkirala .sk_clone_security = selinux_sk_clone_security, 5610beb8d13bSVenkat Yekkirala .sk_getsecid = selinux_sk_getsecid, 56114237c75cSVenkat Yekkirala .sock_graft = selinux_sock_graft, 56124237c75cSVenkat Yekkirala .inet_conn_request = selinux_inet_conn_request, 56134237c75cSVenkat Yekkirala .inet_csk_clone = selinux_inet_csk_clone, 56146b877699SVenkat Yekkirala .inet_conn_established = selinux_inet_conn_established, 56154237c75cSVenkat Yekkirala .req_classify_flow = selinux_req_classify_flow, 5616ed6d76e4SPaul Moore .tun_dev_create = selinux_tun_dev_create, 5617ed6d76e4SPaul Moore .tun_dev_post_create = selinux_tun_dev_post_create, 5618ed6d76e4SPaul Moore .tun_dev_attach = selinux_tun_dev_attach, 5619d28d1e08STrent Jaeger 5620d28d1e08STrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM 5621d28d1e08STrent Jaeger .xfrm_policy_alloc_security = selinux_xfrm_policy_alloc, 5622d28d1e08STrent Jaeger .xfrm_policy_clone_security = selinux_xfrm_policy_clone, 5623d28d1e08STrent Jaeger .xfrm_policy_free_security = selinux_xfrm_policy_free, 5624c8c05a8eSCatherine Zhang .xfrm_policy_delete_security = selinux_xfrm_policy_delete, 5625d28d1e08STrent Jaeger .xfrm_state_alloc_security = selinux_xfrm_state_alloc, 5626d28d1e08STrent Jaeger .xfrm_state_free_security = selinux_xfrm_state_free, 5627c8c05a8eSCatherine Zhang .xfrm_state_delete_security = selinux_xfrm_state_delete, 5628d28d1e08STrent Jaeger .xfrm_policy_lookup = selinux_xfrm_policy_lookup, 5629e0d1caa7SVenkat Yekkirala .xfrm_state_pol_flow_match = selinux_xfrm_state_pol_flow_match, 5630e0d1caa7SVenkat Yekkirala .xfrm_decode_session = selinux_xfrm_decode_session, 56311da177e4SLinus Torvalds #endif 5632d720024eSMichael LeMay 5633d720024eSMichael LeMay #ifdef CONFIG_KEYS 5634d720024eSMichael LeMay .key_alloc = selinux_key_alloc, 5635d720024eSMichael LeMay .key_free = selinux_key_free, 5636d720024eSMichael LeMay .key_permission = selinux_key_permission, 563770a5bb72SDavid Howells .key_getsecurity = selinux_key_getsecurity, 5638d720024eSMichael LeMay #endif 56399d57a7f9SAhmed S. Darwish 56409d57a7f9SAhmed S. Darwish #ifdef CONFIG_AUDIT 56419d57a7f9SAhmed S. Darwish .audit_rule_init = selinux_audit_rule_init, 56429d57a7f9SAhmed S. Darwish .audit_rule_known = selinux_audit_rule_known, 56439d57a7f9SAhmed S. Darwish .audit_rule_match = selinux_audit_rule_match, 56449d57a7f9SAhmed S. Darwish .audit_rule_free = selinux_audit_rule_free, 56459d57a7f9SAhmed S. Darwish #endif 56461da177e4SLinus Torvalds }; 56471da177e4SLinus Torvalds 56481da177e4SLinus Torvalds static __init int selinux_init(void) 56491da177e4SLinus Torvalds { 5650076c54c5SAhmed S. Darwish if (!security_module_enable(&selinux_ops)) { 5651076c54c5SAhmed S. Darwish selinux_enabled = 0; 5652076c54c5SAhmed S. Darwish return 0; 5653076c54c5SAhmed S. Darwish } 5654076c54c5SAhmed S. Darwish 56551da177e4SLinus Torvalds if (!selinux_enabled) { 56561da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Disabled at boot.\n"); 56571da177e4SLinus Torvalds return 0; 56581da177e4SLinus Torvalds } 56591da177e4SLinus Torvalds 56601da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Initializing.\n"); 56611da177e4SLinus Torvalds 56621da177e4SLinus Torvalds /* Set the security state for the initial task. */ 5663d84f4f99SDavid Howells cred_init_security(); 56641da177e4SLinus Torvalds 56657cae7e26SJames Morris sel_inode_cache = kmem_cache_create("selinux_inode_security", 56667cae7e26SJames Morris sizeof(struct inode_security_struct), 566720c2df83SPaul Mundt 0, SLAB_PANIC, NULL); 56681da177e4SLinus Torvalds avc_init(); 56691da177e4SLinus Torvalds 56701da177e4SLinus Torvalds if (register_security(&selinux_ops)) 56711da177e4SLinus Torvalds panic("SELinux: Unable to register with kernel.\n"); 56721da177e4SLinus Torvalds 5673828dfe1dSEric Paris if (selinux_enforcing) 5674fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Starting in enforcing mode\n"); 5675828dfe1dSEric Paris else 5676fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Starting in permissive mode\n"); 5677d720024eSMichael LeMay 56781da177e4SLinus Torvalds return 0; 56791da177e4SLinus Torvalds } 56801da177e4SLinus Torvalds 56811da177e4SLinus Torvalds void selinux_complete_init(void) 56821da177e4SLinus Torvalds { 5683fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Completing initialization.\n"); 56841da177e4SLinus Torvalds 56851da177e4SLinus Torvalds /* Set up any superblocks initialized prior to the policy load. */ 5686fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Setting up existing superblocks.\n"); 5687ba0c19edSStephen Smalley spin_lock(&sb_lock); 56881da177e4SLinus Torvalds spin_lock(&sb_security_lock); 56891da177e4SLinus Torvalds next_sb: 56901da177e4SLinus Torvalds if (!list_empty(&superblock_security_head)) { 56911da177e4SLinus Torvalds struct superblock_security_struct *sbsec = 56921da177e4SLinus Torvalds list_entry(superblock_security_head.next, 56931da177e4SLinus Torvalds struct superblock_security_struct, 56941da177e4SLinus Torvalds list); 56951da177e4SLinus Torvalds struct super_block *sb = sbsec->sb; 56961da177e4SLinus Torvalds sb->s_count++; 56971da177e4SLinus Torvalds spin_unlock(&sb_security_lock); 5698ba0c19edSStephen Smalley spin_unlock(&sb_lock); 56991da177e4SLinus Torvalds down_read(&sb->s_umount); 57001da177e4SLinus Torvalds if (sb->s_root) 57011da177e4SLinus Torvalds superblock_doinit(sb, NULL); 57021da177e4SLinus Torvalds drop_super(sb); 5703ba0c19edSStephen Smalley spin_lock(&sb_lock); 57041da177e4SLinus Torvalds spin_lock(&sb_security_lock); 57051da177e4SLinus Torvalds list_del_init(&sbsec->list); 57061da177e4SLinus Torvalds goto next_sb; 57071da177e4SLinus Torvalds } 57081da177e4SLinus Torvalds spin_unlock(&sb_security_lock); 5709ba0c19edSStephen Smalley spin_unlock(&sb_lock); 57101da177e4SLinus Torvalds } 57111da177e4SLinus Torvalds 57121da177e4SLinus Torvalds /* SELinux requires early initialization in order to label 57131da177e4SLinus Torvalds all processes and objects when they are created. */ 57141da177e4SLinus Torvalds security_initcall(selinux_init); 57151da177e4SLinus Torvalds 5716c2b507fdSStephen Smalley #if defined(CONFIG_NETFILTER) 57171da177e4SLinus Torvalds 5718effad8dfSPaul Moore static struct nf_hook_ops selinux_ipv4_ops[] = { 5719effad8dfSPaul Moore { 5720effad8dfSPaul Moore .hook = selinux_ipv4_postroute, 57211da177e4SLinus Torvalds .owner = THIS_MODULE, 57221da177e4SLinus Torvalds .pf = PF_INET, 57236e23ae2aSPatrick McHardy .hooknum = NF_INET_POST_ROUTING, 57241da177e4SLinus Torvalds .priority = NF_IP_PRI_SELINUX_LAST, 5725effad8dfSPaul Moore }, 5726effad8dfSPaul Moore { 5727effad8dfSPaul Moore .hook = selinux_ipv4_forward, 5728effad8dfSPaul Moore .owner = THIS_MODULE, 5729effad8dfSPaul Moore .pf = PF_INET, 5730effad8dfSPaul Moore .hooknum = NF_INET_FORWARD, 5731effad8dfSPaul Moore .priority = NF_IP_PRI_SELINUX_FIRST, 5732948bf85cSPaul Moore }, 5733948bf85cSPaul Moore { 5734948bf85cSPaul Moore .hook = selinux_ipv4_output, 5735948bf85cSPaul Moore .owner = THIS_MODULE, 5736948bf85cSPaul Moore .pf = PF_INET, 5737948bf85cSPaul Moore .hooknum = NF_INET_LOCAL_OUT, 5738948bf85cSPaul Moore .priority = NF_IP_PRI_SELINUX_FIRST, 5739effad8dfSPaul Moore } 57401da177e4SLinus Torvalds }; 57411da177e4SLinus Torvalds 57421da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 57431da177e4SLinus Torvalds 5744effad8dfSPaul Moore static struct nf_hook_ops selinux_ipv6_ops[] = { 5745effad8dfSPaul Moore { 5746effad8dfSPaul Moore .hook = selinux_ipv6_postroute, 57471da177e4SLinus Torvalds .owner = THIS_MODULE, 57481da177e4SLinus Torvalds .pf = PF_INET6, 57496e23ae2aSPatrick McHardy .hooknum = NF_INET_POST_ROUTING, 57501da177e4SLinus Torvalds .priority = NF_IP6_PRI_SELINUX_LAST, 5751effad8dfSPaul Moore }, 5752effad8dfSPaul Moore { 5753effad8dfSPaul Moore .hook = selinux_ipv6_forward, 5754effad8dfSPaul Moore .owner = THIS_MODULE, 5755effad8dfSPaul Moore .pf = PF_INET6, 5756effad8dfSPaul Moore .hooknum = NF_INET_FORWARD, 5757effad8dfSPaul Moore .priority = NF_IP6_PRI_SELINUX_FIRST, 5758effad8dfSPaul Moore } 57591da177e4SLinus Torvalds }; 57601da177e4SLinus Torvalds 57611da177e4SLinus Torvalds #endif /* IPV6 */ 57621da177e4SLinus Torvalds 57631da177e4SLinus Torvalds static int __init selinux_nf_ip_init(void) 57641da177e4SLinus Torvalds { 57651da177e4SLinus Torvalds int err = 0; 57661da177e4SLinus Torvalds 57671da177e4SLinus Torvalds if (!selinux_enabled) 57681da177e4SLinus Torvalds goto out; 57691da177e4SLinus Torvalds 5770fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Registering netfilter hooks\n"); 57711da177e4SLinus Torvalds 57726c5a9d2eSAlexey Dobriyan err = nf_register_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops)); 57731da177e4SLinus Torvalds if (err) 57746c5a9d2eSAlexey Dobriyan panic("SELinux: nf_register_hooks for IPv4: error %d\n", err); 57751da177e4SLinus Torvalds 57761da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 57776c5a9d2eSAlexey Dobriyan err = nf_register_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops)); 57781da177e4SLinus Torvalds if (err) 57796c5a9d2eSAlexey Dobriyan panic("SELinux: nf_register_hooks for IPv6: error %d\n", err); 57801da177e4SLinus Torvalds #endif /* IPV6 */ 5781d28d1e08STrent Jaeger 57821da177e4SLinus Torvalds out: 57831da177e4SLinus Torvalds return err; 57841da177e4SLinus Torvalds } 57851da177e4SLinus Torvalds 57861da177e4SLinus Torvalds __initcall(selinux_nf_ip_init); 57871da177e4SLinus Torvalds 57881da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 57891da177e4SLinus Torvalds static void selinux_nf_ip_exit(void) 57901da177e4SLinus Torvalds { 5791fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Unregistering netfilter hooks\n"); 57921da177e4SLinus Torvalds 57936c5a9d2eSAlexey Dobriyan nf_unregister_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops)); 57941da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 57956c5a9d2eSAlexey Dobriyan nf_unregister_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops)); 57961da177e4SLinus Torvalds #endif /* IPV6 */ 57971da177e4SLinus Torvalds } 57981da177e4SLinus Torvalds #endif 57991da177e4SLinus Torvalds 5800c2b507fdSStephen Smalley #else /* CONFIG_NETFILTER */ 58011da177e4SLinus Torvalds 58021da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 58031da177e4SLinus Torvalds #define selinux_nf_ip_exit() 58041da177e4SLinus Torvalds #endif 58051da177e4SLinus Torvalds 5806c2b507fdSStephen Smalley #endif /* CONFIG_NETFILTER */ 58071da177e4SLinus Torvalds 58081da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 5809828dfe1dSEric Paris static int selinux_disabled; 5810828dfe1dSEric Paris 58111da177e4SLinus Torvalds int selinux_disable(void) 58121da177e4SLinus Torvalds { 58131da177e4SLinus Torvalds extern void exit_sel_fs(void); 58141da177e4SLinus Torvalds 58151da177e4SLinus Torvalds if (ss_initialized) { 58161da177e4SLinus Torvalds /* Not permitted after initial policy load. */ 58171da177e4SLinus Torvalds return -EINVAL; 58181da177e4SLinus Torvalds } 58191da177e4SLinus Torvalds 58201da177e4SLinus Torvalds if (selinux_disabled) { 58211da177e4SLinus Torvalds /* Only do this once. */ 58221da177e4SLinus Torvalds return -EINVAL; 58231da177e4SLinus Torvalds } 58241da177e4SLinus Torvalds 58251da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Disabled at runtime.\n"); 58261da177e4SLinus Torvalds 58271da177e4SLinus Torvalds selinux_disabled = 1; 582830d55280SStephen Smalley selinux_enabled = 0; 58291da177e4SLinus Torvalds 5830*189b3b1cSwzt.wzt@gmail.com reset_security_ops(); 58311da177e4SLinus Torvalds 5832af8ff049SEric Paris /* Try to destroy the avc node cache */ 5833af8ff049SEric Paris avc_disable(); 5834af8ff049SEric Paris 58351da177e4SLinus Torvalds /* Unregister netfilter hooks. */ 58361da177e4SLinus Torvalds selinux_nf_ip_exit(); 58371da177e4SLinus Torvalds 58381da177e4SLinus Torvalds /* Unregister selinuxfs. */ 58391da177e4SLinus Torvalds exit_sel_fs(); 58401da177e4SLinus Torvalds 58411da177e4SLinus Torvalds return 0; 58421da177e4SLinus Torvalds } 58431da177e4SLinus Torvalds #endif 5844