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> 16effad8dfSPaul Moore * Copyright (C) 2006, 2007 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> 781da177e4SLinus Torvalds 791da177e4SLinus Torvalds #include "avc.h" 801da177e4SLinus Torvalds #include "objsec.h" 811da177e4SLinus Torvalds #include "netif.h" 82224dfbd8SPaul Moore #include "netnode.h" 833e112172SPaul Moore #include "netport.h" 84d28d1e08STrent Jaeger #include "xfrm.h" 85c60475bfSPaul Moore #include "netlabel.h" 869d57a7f9SAhmed S. Darwish #include "audit.h" 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds #define XATTR_SELINUX_SUFFIX "selinux" 891da177e4SLinus Torvalds #define XATTR_NAME_SELINUX XATTR_SECURITY_PREFIX XATTR_SELINUX_SUFFIX 901da177e4SLinus Torvalds 91c9180a57SEric Paris #define NUM_SEL_MNT_OPTS 4 92c9180a57SEric Paris 931da177e4SLinus Torvalds extern unsigned int policydb_loaded_version; 941da177e4SLinus Torvalds extern int selinux_nlmsg_lookup(u16 sclass, u16 nlmsg_type, u32 *perm); 954e5ab4cbSJames Morris extern int selinux_compat_net; 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 1306f0f0fd4SJames Morris /* 1316f0f0fd4SJames Morris * Minimal support for a secondary security module, 1326f0f0fd4SJames Morris * just to allow the use of the capability module. 1336f0f0fd4SJames Morris */ 134828dfe1dSEric Paris static struct security_operations *secondary_ops; 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds /* Lists of inode and superblock security structures initialized 1371da177e4SLinus Torvalds before the policy was loaded. */ 1381da177e4SLinus Torvalds static LIST_HEAD(superblock_security_head); 1391da177e4SLinus Torvalds static DEFINE_SPINLOCK(sb_security_lock); 1401da177e4SLinus Torvalds 141e18b890bSChristoph Lameter static struct kmem_cache *sel_inode_cache; 1427cae7e26SJames Morris 143d621d35eSPaul Moore /** 144d621d35eSPaul Moore * selinux_secmark_enabled - Check to see if SECMARK is currently enabled 145d621d35eSPaul Moore * 146d621d35eSPaul Moore * Description: 147d621d35eSPaul Moore * This function checks the SECMARK reference counter to see if any SECMARK 148d621d35eSPaul Moore * targets are currently configured, if the reference counter is greater than 149d621d35eSPaul Moore * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is 150d621d35eSPaul Moore * enabled, false (0) if SECMARK is disabled. 151d621d35eSPaul Moore * 152d621d35eSPaul Moore */ 153d621d35eSPaul Moore static int selinux_secmark_enabled(void) 154d621d35eSPaul Moore { 155d621d35eSPaul Moore return (atomic_read(&selinux_secmark_refcount) > 0); 156d621d35eSPaul Moore } 157d621d35eSPaul Moore 1581da177e4SLinus Torvalds /* Allocate and free functions for each kind of security blob. */ 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds static int task_alloc_security(struct task_struct *task) 1611da177e4SLinus Torvalds { 1621da177e4SLinus Torvalds struct task_security_struct *tsec; 1631da177e4SLinus Torvalds 16489d155efSJames Morris tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL); 1651da177e4SLinus Torvalds if (!tsec) 1661da177e4SLinus Torvalds return -ENOMEM; 1671da177e4SLinus Torvalds 1680356357cSRoland McGrath tsec->osid = tsec->sid = SECINITSID_UNLABELED; 1691da177e4SLinus Torvalds task->security = tsec; 1701da177e4SLinus Torvalds 1711da177e4SLinus Torvalds return 0; 1721da177e4SLinus Torvalds } 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds static void task_free_security(struct task_struct *task) 1751da177e4SLinus Torvalds { 1761da177e4SLinus Torvalds struct task_security_struct *tsec = task->security; 1771da177e4SLinus Torvalds task->security = NULL; 1781da177e4SLinus Torvalds kfree(tsec); 1791da177e4SLinus Torvalds } 1801da177e4SLinus Torvalds 1811da177e4SLinus Torvalds static int inode_alloc_security(struct inode *inode) 1821da177e4SLinus Torvalds { 1831da177e4SLinus Torvalds struct task_security_struct *tsec = current->security; 1841da177e4SLinus Torvalds struct inode_security_struct *isec; 1851da177e4SLinus Torvalds 186a02fe132SJosef Bacik isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS); 1871da177e4SLinus Torvalds if (!isec) 1881da177e4SLinus Torvalds return -ENOMEM; 1891da177e4SLinus Torvalds 19023970741SEric Paris mutex_init(&isec->lock); 1911da177e4SLinus Torvalds INIT_LIST_HEAD(&isec->list); 1921da177e4SLinus Torvalds isec->inode = inode; 1931da177e4SLinus Torvalds isec->sid = SECINITSID_UNLABELED; 1941da177e4SLinus Torvalds isec->sclass = SECCLASS_FILE; 1951da177e4SLinus Torvalds isec->task_sid = tsec->sid; 1961da177e4SLinus Torvalds inode->i_security = isec; 1971da177e4SLinus Torvalds 1981da177e4SLinus Torvalds return 0; 1991da177e4SLinus Torvalds } 2001da177e4SLinus Torvalds 2011da177e4SLinus Torvalds static void inode_free_security(struct inode *inode) 2021da177e4SLinus Torvalds { 2031da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 2041da177e4SLinus Torvalds struct superblock_security_struct *sbsec = inode->i_sb->s_security; 2051da177e4SLinus Torvalds 2061da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 2071da177e4SLinus Torvalds if (!list_empty(&isec->list)) 2081da177e4SLinus Torvalds list_del_init(&isec->list); 2091da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 2101da177e4SLinus Torvalds 2111da177e4SLinus Torvalds inode->i_security = NULL; 2127cae7e26SJames Morris kmem_cache_free(sel_inode_cache, isec); 2131da177e4SLinus Torvalds } 2141da177e4SLinus Torvalds 2151da177e4SLinus Torvalds static int file_alloc_security(struct file *file) 2161da177e4SLinus Torvalds { 2171da177e4SLinus Torvalds struct task_security_struct *tsec = current->security; 2181da177e4SLinus Torvalds struct file_security_struct *fsec; 2191da177e4SLinus Torvalds 22026d2a4beSStephen Smalley fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL); 2211da177e4SLinus Torvalds if (!fsec) 2221da177e4SLinus Torvalds return -ENOMEM; 2231da177e4SLinus Torvalds 2241da177e4SLinus Torvalds fsec->sid = tsec->sid; 2251da177e4SLinus Torvalds fsec->fown_sid = tsec->sid; 2261da177e4SLinus Torvalds file->f_security = fsec; 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds return 0; 2291da177e4SLinus Torvalds } 2301da177e4SLinus Torvalds 2311da177e4SLinus Torvalds static void file_free_security(struct file *file) 2321da177e4SLinus Torvalds { 2331da177e4SLinus Torvalds struct file_security_struct *fsec = file->f_security; 2341da177e4SLinus Torvalds file->f_security = NULL; 2351da177e4SLinus Torvalds kfree(fsec); 2361da177e4SLinus Torvalds } 2371da177e4SLinus Torvalds 2381da177e4SLinus Torvalds static int superblock_alloc_security(struct super_block *sb) 2391da177e4SLinus Torvalds { 2401da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 2411da177e4SLinus Torvalds 24289d155efSJames Morris sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL); 2431da177e4SLinus Torvalds if (!sbsec) 2441da177e4SLinus Torvalds return -ENOMEM; 2451da177e4SLinus Torvalds 246bc7e982bSEric Paris mutex_init(&sbsec->lock); 2471da177e4SLinus Torvalds INIT_LIST_HEAD(&sbsec->list); 2481da177e4SLinus Torvalds INIT_LIST_HEAD(&sbsec->isec_head); 2491da177e4SLinus Torvalds spin_lock_init(&sbsec->isec_lock); 2501da177e4SLinus Torvalds sbsec->sb = sb; 2511da177e4SLinus Torvalds sbsec->sid = SECINITSID_UNLABELED; 2521da177e4SLinus Torvalds sbsec->def_sid = SECINITSID_FILE; 253c312feb2SEric Paris sbsec->mntpoint_sid = SECINITSID_UNLABELED; 2541da177e4SLinus Torvalds sb->s_security = sbsec; 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds return 0; 2571da177e4SLinus Torvalds } 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds static void superblock_free_security(struct super_block *sb) 2601da177e4SLinus Torvalds { 2611da177e4SLinus Torvalds struct superblock_security_struct *sbsec = sb->s_security; 2621da177e4SLinus Torvalds 2631da177e4SLinus Torvalds spin_lock(&sb_security_lock); 2641da177e4SLinus Torvalds if (!list_empty(&sbsec->list)) 2651da177e4SLinus Torvalds list_del_init(&sbsec->list); 2661da177e4SLinus Torvalds spin_unlock(&sb_security_lock); 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds sb->s_security = NULL; 2691da177e4SLinus Torvalds kfree(sbsec); 2701da177e4SLinus Torvalds } 2711da177e4SLinus Torvalds 2727d877f3bSAl Viro static int sk_alloc_security(struct sock *sk, int family, gfp_t priority) 2731da177e4SLinus Torvalds { 2741da177e4SLinus Torvalds struct sk_security_struct *ssec; 2751da177e4SLinus Torvalds 27689d155efSJames Morris ssec = kzalloc(sizeof(*ssec), priority); 2771da177e4SLinus Torvalds if (!ssec) 2781da177e4SLinus Torvalds return -ENOMEM; 2791da177e4SLinus Torvalds 2801da177e4SLinus Torvalds ssec->peer_sid = SECINITSID_UNLABELED; 281892c141eSVenkat Yekkirala ssec->sid = SECINITSID_UNLABELED; 2821da177e4SLinus Torvalds sk->sk_security = ssec; 2831da177e4SLinus Torvalds 284f74af6e8SPaul Moore selinux_netlbl_sk_security_reset(ssec, family); 28599f59ed0SPaul Moore 2861da177e4SLinus Torvalds return 0; 2871da177e4SLinus Torvalds } 2881da177e4SLinus Torvalds 2891da177e4SLinus Torvalds static void sk_free_security(struct sock *sk) 2901da177e4SLinus Torvalds { 2911da177e4SLinus Torvalds struct sk_security_struct *ssec = sk->sk_security; 2921da177e4SLinus Torvalds 2931da177e4SLinus Torvalds sk->sk_security = NULL; 2941da177e4SLinus Torvalds kfree(ssec); 2951da177e4SLinus Torvalds } 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds /* The security server must be initialized before 2981da177e4SLinus Torvalds any labeling or access decisions can be provided. */ 2991da177e4SLinus Torvalds extern int ss_initialized; 3001da177e4SLinus Torvalds 3011da177e4SLinus Torvalds /* The file system's label must be initialized prior to use. */ 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds static char *labeling_behaviors[6] = { 3041da177e4SLinus Torvalds "uses xattr", 3051da177e4SLinus Torvalds "uses transition SIDs", 3061da177e4SLinus Torvalds "uses task SIDs", 3071da177e4SLinus Torvalds "uses genfs_contexts", 3081da177e4SLinus Torvalds "not configured for labeling", 3091da177e4SLinus Torvalds "uses mountpoint labeling", 3101da177e4SLinus Torvalds }; 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry); 3131da177e4SLinus Torvalds 3141da177e4SLinus Torvalds static inline int inode_doinit(struct inode *inode) 3151da177e4SLinus Torvalds { 3161da177e4SLinus Torvalds return inode_doinit_with_dentry(inode, NULL); 3171da177e4SLinus Torvalds } 3181da177e4SLinus Torvalds 3191da177e4SLinus Torvalds enum { 32031e87930SEric Paris Opt_error = -1, 3211da177e4SLinus Torvalds Opt_context = 1, 3221da177e4SLinus Torvalds Opt_fscontext = 2, 323c9180a57SEric Paris Opt_defcontext = 3, 324c9180a57SEric Paris Opt_rootcontext = 4, 3251da177e4SLinus Torvalds }; 3261da177e4SLinus Torvalds 3271da177e4SLinus Torvalds static match_table_t tokens = { 328832cbd9aSEric Paris {Opt_context, CONTEXT_STR "%s"}, 329832cbd9aSEric Paris {Opt_fscontext, FSCONTEXT_STR "%s"}, 330832cbd9aSEric Paris {Opt_defcontext, DEFCONTEXT_STR "%s"}, 331832cbd9aSEric Paris {Opt_rootcontext, ROOTCONTEXT_STR "%s"}, 33231e87930SEric Paris {Opt_error, NULL}, 3331da177e4SLinus Torvalds }; 3341da177e4SLinus Torvalds 3351da177e4SLinus Torvalds #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n" 3361da177e4SLinus Torvalds 337c312feb2SEric Paris static int may_context_mount_sb_relabel(u32 sid, 338c312feb2SEric Paris struct superblock_security_struct *sbsec, 339c312feb2SEric Paris struct task_security_struct *tsec) 340c312feb2SEric Paris { 341c312feb2SEric Paris int rc; 342c312feb2SEric Paris 343c312feb2SEric Paris rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 344c312feb2SEric Paris FILESYSTEM__RELABELFROM, NULL); 345c312feb2SEric Paris if (rc) 346c312feb2SEric Paris return rc; 347c312feb2SEric Paris 348c312feb2SEric Paris rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM, 349c312feb2SEric Paris FILESYSTEM__RELABELTO, NULL); 350c312feb2SEric Paris return rc; 351c312feb2SEric Paris } 352c312feb2SEric Paris 3530808925eSEric Paris static int may_context_mount_inode_relabel(u32 sid, 3540808925eSEric Paris struct superblock_security_struct *sbsec, 3550808925eSEric Paris struct task_security_struct *tsec) 3560808925eSEric Paris { 3570808925eSEric Paris int rc; 3580808925eSEric Paris rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 3590808925eSEric Paris FILESYSTEM__RELABELFROM, NULL); 3600808925eSEric Paris if (rc) 3610808925eSEric Paris return rc; 3620808925eSEric Paris 3630808925eSEric Paris rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, 3640808925eSEric Paris FILESYSTEM__ASSOCIATE, NULL); 3650808925eSEric Paris return rc; 3660808925eSEric Paris } 3670808925eSEric Paris 368c9180a57SEric Paris static int sb_finish_set_opts(struct super_block *sb) 3691da177e4SLinus Torvalds { 3701da177e4SLinus Torvalds struct superblock_security_struct *sbsec = sb->s_security; 3711da177e4SLinus Torvalds struct dentry *root = sb->s_root; 372c9180a57SEric Paris struct inode *root_inode = root->d_inode; 3731da177e4SLinus Torvalds int rc = 0; 3741da177e4SLinus Torvalds 3751da177e4SLinus Torvalds if (sbsec->behavior == SECURITY_FS_USE_XATTR) { 3761da177e4SLinus Torvalds /* Make sure that the xattr handler exists and that no 3771da177e4SLinus Torvalds error other than -ENODATA is returned by getxattr on 3781da177e4SLinus Torvalds the root directory. -ENODATA is ok, as this may be 3791da177e4SLinus Torvalds the first boot of the SELinux kernel before we have 3801da177e4SLinus Torvalds assigned xattr values to the filesystem. */ 381c9180a57SEric Paris if (!root_inode->i_op->getxattr) { 3821da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: (dev %s, type %s) has no " 3831da177e4SLinus Torvalds "xattr support\n", sb->s_id, sb->s_type->name); 3841da177e4SLinus Torvalds rc = -EOPNOTSUPP; 3851da177e4SLinus Torvalds goto out; 3861da177e4SLinus Torvalds } 387c9180a57SEric Paris rc = root_inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0); 3881da177e4SLinus Torvalds if (rc < 0 && rc != -ENODATA) { 3891da177e4SLinus Torvalds if (rc == -EOPNOTSUPP) 3901da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: (dev %s, type " 3911da177e4SLinus Torvalds "%s) has no security xattr handler\n", 3921da177e4SLinus Torvalds sb->s_id, sb->s_type->name); 3931da177e4SLinus Torvalds else 3941da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: (dev %s, type " 3951da177e4SLinus Torvalds "%s) getxattr errno %d\n", sb->s_id, 3961da177e4SLinus Torvalds sb->s_type->name, -rc); 3971da177e4SLinus Torvalds goto out; 3981da177e4SLinus Torvalds } 3991da177e4SLinus Torvalds } 4001da177e4SLinus Torvalds 4011da177e4SLinus Torvalds sbsec->initialized = 1; 4021da177e4SLinus Torvalds 403c9180a57SEric Paris if (sbsec->behavior > ARRAY_SIZE(labeling_behaviors)) 404fadcdb45SEric Paris printk(KERN_ERR "SELinux: initialized (dev %s, type %s), unknown behavior\n", 4051da177e4SLinus Torvalds sb->s_id, sb->s_type->name); 406c9180a57SEric Paris else 407fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: initialized (dev %s, type %s), %s\n", 4081da177e4SLinus Torvalds sb->s_id, sb->s_type->name, 4091da177e4SLinus Torvalds labeling_behaviors[sbsec->behavior-1]); 4101da177e4SLinus Torvalds 4111da177e4SLinus Torvalds /* Initialize the root inode. */ 412c9180a57SEric Paris rc = inode_doinit_with_dentry(root_inode, root); 4131da177e4SLinus Torvalds 4141da177e4SLinus Torvalds /* Initialize any other inodes associated with the superblock, e.g. 4151da177e4SLinus Torvalds inodes created prior to initial policy load or inodes created 4161da177e4SLinus Torvalds during get_sb by a pseudo filesystem that directly 4171da177e4SLinus Torvalds populates itself. */ 4181da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 4191da177e4SLinus Torvalds next_inode: 4201da177e4SLinus Torvalds if (!list_empty(&sbsec->isec_head)) { 4211da177e4SLinus Torvalds struct inode_security_struct *isec = 4221da177e4SLinus Torvalds list_entry(sbsec->isec_head.next, 4231da177e4SLinus Torvalds struct inode_security_struct, list); 4241da177e4SLinus Torvalds struct inode *inode = isec->inode; 4251da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 4261da177e4SLinus Torvalds inode = igrab(inode); 4271da177e4SLinus Torvalds if (inode) { 4281da177e4SLinus Torvalds if (!IS_PRIVATE(inode)) 4291da177e4SLinus Torvalds inode_doinit(inode); 4301da177e4SLinus Torvalds iput(inode); 4311da177e4SLinus Torvalds } 4321da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 4331da177e4SLinus Torvalds list_del_init(&isec->list); 4341da177e4SLinus Torvalds goto next_inode; 4351da177e4SLinus Torvalds } 4361da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 4371da177e4SLinus Torvalds out: 438c9180a57SEric Paris return rc; 439c9180a57SEric Paris } 440c9180a57SEric Paris 441c9180a57SEric Paris /* 442c9180a57SEric Paris * This function should allow an FS to ask what it's mount security 443c9180a57SEric Paris * options were so it can use those later for submounts, displaying 444c9180a57SEric Paris * mount options, or whatever. 445c9180a57SEric Paris */ 446c9180a57SEric Paris static int selinux_get_mnt_opts(const struct super_block *sb, 447e0007529SEric Paris struct security_mnt_opts *opts) 448c9180a57SEric Paris { 449c9180a57SEric Paris int rc = 0, i; 450c9180a57SEric Paris struct superblock_security_struct *sbsec = sb->s_security; 451c9180a57SEric Paris char *context = NULL; 452c9180a57SEric Paris u32 len; 453c9180a57SEric Paris char tmp; 454c9180a57SEric Paris 455e0007529SEric Paris security_init_mnt_opts(opts); 456c9180a57SEric Paris 457c9180a57SEric Paris if (!sbsec->initialized) 458c9180a57SEric Paris return -EINVAL; 459c9180a57SEric Paris 460c9180a57SEric Paris if (!ss_initialized) 461c9180a57SEric Paris return -EINVAL; 462c9180a57SEric Paris 463c9180a57SEric Paris /* 464c9180a57SEric Paris * if we ever use sbsec flags for anything other than tracking mount 465c9180a57SEric Paris * settings this is going to need a mask 466c9180a57SEric Paris */ 467c9180a57SEric Paris tmp = sbsec->flags; 468c9180a57SEric Paris /* count the number of mount options for this sb */ 469c9180a57SEric Paris for (i = 0; i < 8; i++) { 470c9180a57SEric Paris if (tmp & 0x01) 471e0007529SEric Paris opts->num_mnt_opts++; 472c9180a57SEric Paris tmp >>= 1; 473c9180a57SEric Paris } 474c9180a57SEric Paris 475e0007529SEric Paris opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC); 476e0007529SEric Paris if (!opts->mnt_opts) { 477c9180a57SEric Paris rc = -ENOMEM; 478c9180a57SEric Paris goto out_free; 479c9180a57SEric Paris } 480c9180a57SEric Paris 481e0007529SEric Paris opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC); 482e0007529SEric Paris if (!opts->mnt_opts_flags) { 483c9180a57SEric Paris rc = -ENOMEM; 484c9180a57SEric Paris goto out_free; 485c9180a57SEric Paris } 486c9180a57SEric Paris 487c9180a57SEric Paris i = 0; 488c9180a57SEric Paris if (sbsec->flags & FSCONTEXT_MNT) { 489c9180a57SEric Paris rc = security_sid_to_context(sbsec->sid, &context, &len); 490c9180a57SEric Paris if (rc) 491c9180a57SEric Paris goto out_free; 492e0007529SEric Paris opts->mnt_opts[i] = context; 493e0007529SEric Paris opts->mnt_opts_flags[i++] = FSCONTEXT_MNT; 494c9180a57SEric Paris } 495c9180a57SEric Paris if (sbsec->flags & CONTEXT_MNT) { 496c9180a57SEric Paris rc = security_sid_to_context(sbsec->mntpoint_sid, &context, &len); 497c9180a57SEric Paris if (rc) 498c9180a57SEric Paris goto out_free; 499e0007529SEric Paris opts->mnt_opts[i] = context; 500e0007529SEric Paris opts->mnt_opts_flags[i++] = CONTEXT_MNT; 501c9180a57SEric Paris } 502c9180a57SEric Paris if (sbsec->flags & DEFCONTEXT_MNT) { 503c9180a57SEric Paris rc = security_sid_to_context(sbsec->def_sid, &context, &len); 504c9180a57SEric Paris if (rc) 505c9180a57SEric Paris goto out_free; 506e0007529SEric Paris opts->mnt_opts[i] = context; 507e0007529SEric Paris opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT; 508c9180a57SEric Paris } 509c9180a57SEric Paris if (sbsec->flags & ROOTCONTEXT_MNT) { 510c9180a57SEric Paris struct inode *root = sbsec->sb->s_root->d_inode; 511c9180a57SEric Paris struct inode_security_struct *isec = root->i_security; 512c9180a57SEric Paris 513c9180a57SEric Paris rc = security_sid_to_context(isec->sid, &context, &len); 514c9180a57SEric Paris if (rc) 515c9180a57SEric Paris goto out_free; 516e0007529SEric Paris opts->mnt_opts[i] = context; 517e0007529SEric Paris opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT; 518c9180a57SEric Paris } 519c9180a57SEric Paris 520e0007529SEric Paris BUG_ON(i != opts->num_mnt_opts); 521c9180a57SEric Paris 522c9180a57SEric Paris return 0; 523c9180a57SEric Paris 524c9180a57SEric Paris out_free: 525e0007529SEric Paris security_free_mnt_opts(opts); 526c9180a57SEric Paris return rc; 527c9180a57SEric Paris } 528c9180a57SEric Paris 529c9180a57SEric Paris static int bad_option(struct superblock_security_struct *sbsec, char flag, 530c9180a57SEric Paris u32 old_sid, u32 new_sid) 531c9180a57SEric Paris { 532c9180a57SEric Paris /* check if the old mount command had the same options */ 533c9180a57SEric Paris if (sbsec->initialized) 534c9180a57SEric Paris if (!(sbsec->flags & flag) || 535c9180a57SEric Paris (old_sid != new_sid)) 536c9180a57SEric Paris return 1; 537c9180a57SEric Paris 538c9180a57SEric Paris /* check if we were passed the same options twice, 539c9180a57SEric Paris * aka someone passed context=a,context=b 540c9180a57SEric Paris */ 541c9180a57SEric Paris if (!sbsec->initialized) 542c9180a57SEric Paris if (sbsec->flags & flag) 543c9180a57SEric Paris return 1; 544c9180a57SEric Paris return 0; 545c9180a57SEric Paris } 546e0007529SEric Paris 547c9180a57SEric Paris /* 548c9180a57SEric Paris * Allow filesystems with binary mount data to explicitly set mount point 549c9180a57SEric Paris * labeling information. 550c9180a57SEric Paris */ 551e0007529SEric Paris static int selinux_set_mnt_opts(struct super_block *sb, 552e0007529SEric Paris struct security_mnt_opts *opts) 553c9180a57SEric Paris { 554c9180a57SEric Paris int rc = 0, i; 555c9180a57SEric Paris struct task_security_struct *tsec = current->security; 556c9180a57SEric Paris struct superblock_security_struct *sbsec = sb->s_security; 557c9180a57SEric Paris const char *name = sb->s_type->name; 558089be43eSJames Morris struct inode *inode = sbsec->sb->s_root->d_inode; 559089be43eSJames Morris struct inode_security_struct *root_isec = inode->i_security; 560c9180a57SEric Paris u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0; 561c9180a57SEric Paris u32 defcontext_sid = 0; 562e0007529SEric Paris char **mount_options = opts->mnt_opts; 563e0007529SEric Paris int *flags = opts->mnt_opts_flags; 564e0007529SEric Paris int num_opts = opts->num_mnt_opts; 565c9180a57SEric Paris 566c9180a57SEric Paris mutex_lock(&sbsec->lock); 567c9180a57SEric Paris 568c9180a57SEric Paris if (!ss_initialized) { 569c9180a57SEric Paris if (!num_opts) { 570c9180a57SEric Paris /* Defer initialization until selinux_complete_init, 571c9180a57SEric Paris after the initial policy is loaded and the security 572c9180a57SEric Paris server is ready to handle calls. */ 573c9180a57SEric Paris spin_lock(&sb_security_lock); 574c9180a57SEric Paris if (list_empty(&sbsec->list)) 575c9180a57SEric Paris list_add(&sbsec->list, &superblock_security_head); 576c9180a57SEric Paris spin_unlock(&sb_security_lock); 577c9180a57SEric Paris goto out; 578c9180a57SEric Paris } 579c9180a57SEric Paris rc = -EINVAL; 580744ba35eSEric Paris printk(KERN_WARNING "SELinux: Unable to set superblock options " 581744ba35eSEric Paris "before the security server is initialized\n"); 582c9180a57SEric Paris goto out; 583c9180a57SEric Paris } 584c9180a57SEric Paris 585c9180a57SEric Paris /* 586e0007529SEric Paris * Binary mount data FS will come through this function twice. Once 587e0007529SEric Paris * from an explicit call and once from the generic calls from the vfs. 588e0007529SEric Paris * Since the generic VFS calls will not contain any security mount data 589e0007529SEric Paris * we need to skip the double mount verification. 590e0007529SEric Paris * 591e0007529SEric Paris * This does open a hole in which we will not notice if the first 592e0007529SEric Paris * mount using this sb set explict options and a second mount using 593e0007529SEric Paris * this sb does not set any security options. (The first options 594e0007529SEric Paris * will be used for both mounts) 595e0007529SEric Paris */ 596e0007529SEric Paris if (sbsec->initialized && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA) 597e0007529SEric Paris && (num_opts == 0)) 598e0007529SEric Paris goto out; 599e0007529SEric Paris 600e0007529SEric Paris /* 601c9180a57SEric Paris * parse the mount options, check if they are valid sids. 602c9180a57SEric Paris * also check if someone is trying to mount the same sb more 603c9180a57SEric Paris * than once with different security options. 604c9180a57SEric Paris */ 605c9180a57SEric Paris for (i = 0; i < num_opts; i++) { 606c9180a57SEric Paris u32 sid; 607c9180a57SEric Paris rc = security_context_to_sid(mount_options[i], 608c9180a57SEric Paris strlen(mount_options[i]), &sid); 609c9180a57SEric Paris if (rc) { 610c9180a57SEric Paris printk(KERN_WARNING "SELinux: security_context_to_sid" 611c9180a57SEric Paris "(%s) failed for (dev %s, type %s) errno=%d\n", 612c9180a57SEric Paris mount_options[i], sb->s_id, name, rc); 613c9180a57SEric Paris goto out; 614c9180a57SEric Paris } 615c9180a57SEric Paris switch (flags[i]) { 616c9180a57SEric Paris case FSCONTEXT_MNT: 617c9180a57SEric Paris fscontext_sid = sid; 618c9180a57SEric Paris 619c9180a57SEric Paris if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, 620c9180a57SEric Paris fscontext_sid)) 621c9180a57SEric Paris goto out_double_mount; 622c9180a57SEric Paris 623c9180a57SEric Paris sbsec->flags |= FSCONTEXT_MNT; 624c9180a57SEric Paris break; 625c9180a57SEric Paris case CONTEXT_MNT: 626c9180a57SEric Paris context_sid = sid; 627c9180a57SEric Paris 628c9180a57SEric Paris if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, 629c9180a57SEric Paris context_sid)) 630c9180a57SEric Paris goto out_double_mount; 631c9180a57SEric Paris 632c9180a57SEric Paris sbsec->flags |= CONTEXT_MNT; 633c9180a57SEric Paris break; 634c9180a57SEric Paris case ROOTCONTEXT_MNT: 635c9180a57SEric Paris rootcontext_sid = sid; 636c9180a57SEric Paris 637c9180a57SEric Paris if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, 638c9180a57SEric Paris rootcontext_sid)) 639c9180a57SEric Paris goto out_double_mount; 640c9180a57SEric Paris 641c9180a57SEric Paris sbsec->flags |= ROOTCONTEXT_MNT; 642c9180a57SEric Paris 643c9180a57SEric Paris break; 644c9180a57SEric Paris case DEFCONTEXT_MNT: 645c9180a57SEric Paris defcontext_sid = sid; 646c9180a57SEric Paris 647c9180a57SEric Paris if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, 648c9180a57SEric Paris defcontext_sid)) 649c9180a57SEric Paris goto out_double_mount; 650c9180a57SEric Paris 651c9180a57SEric Paris sbsec->flags |= DEFCONTEXT_MNT; 652c9180a57SEric Paris 653c9180a57SEric Paris break; 654c9180a57SEric Paris default: 655c9180a57SEric Paris rc = -EINVAL; 656c9180a57SEric Paris goto out; 657c9180a57SEric Paris } 658c9180a57SEric Paris } 659c9180a57SEric Paris 660c9180a57SEric Paris if (sbsec->initialized) { 661c9180a57SEric Paris /* previously mounted with options, but not on this attempt? */ 662c9180a57SEric Paris if (sbsec->flags && !num_opts) 663c9180a57SEric Paris goto out_double_mount; 664c9180a57SEric Paris rc = 0; 665c9180a57SEric Paris goto out; 666c9180a57SEric Paris } 667c9180a57SEric Paris 668089be43eSJames Morris if (strcmp(sb->s_type->name, "proc") == 0) 669c9180a57SEric Paris sbsec->proc = 1; 670c9180a57SEric Paris 671c9180a57SEric Paris /* Determine the labeling behavior to use for this filesystem type. */ 672089be43eSJames Morris rc = security_fs_use(sb->s_type->name, &sbsec->behavior, &sbsec->sid); 673c9180a57SEric Paris if (rc) { 674c9180a57SEric Paris printk(KERN_WARNING "%s: security_fs_use(%s) returned %d\n", 675089be43eSJames Morris __func__, sb->s_type->name, rc); 676c9180a57SEric Paris goto out; 677c9180a57SEric Paris } 678c9180a57SEric Paris 679c9180a57SEric Paris /* sets the context of the superblock for the fs being mounted. */ 680c9180a57SEric Paris if (fscontext_sid) { 681c9180a57SEric Paris 682c9180a57SEric Paris rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, tsec); 683c9180a57SEric Paris if (rc) 684c9180a57SEric Paris goto out; 685c9180a57SEric Paris 686c9180a57SEric Paris sbsec->sid = fscontext_sid; 687c9180a57SEric Paris } 688c9180a57SEric Paris 689c9180a57SEric Paris /* 690c9180a57SEric Paris * Switch to using mount point labeling behavior. 691c9180a57SEric Paris * sets the label used on all file below the mountpoint, and will set 692c9180a57SEric Paris * the superblock context if not already set. 693c9180a57SEric Paris */ 694c9180a57SEric Paris if (context_sid) { 695c9180a57SEric Paris if (!fscontext_sid) { 696c9180a57SEric Paris rc = may_context_mount_sb_relabel(context_sid, sbsec, tsec); 697c9180a57SEric Paris if (rc) 698c9180a57SEric Paris goto out; 699c9180a57SEric Paris sbsec->sid = context_sid; 700c9180a57SEric Paris } else { 701c9180a57SEric Paris rc = may_context_mount_inode_relabel(context_sid, sbsec, tsec); 702c9180a57SEric Paris if (rc) 703c9180a57SEric Paris goto out; 704c9180a57SEric Paris } 705c9180a57SEric Paris if (!rootcontext_sid) 706c9180a57SEric Paris rootcontext_sid = context_sid; 707c9180a57SEric Paris 708c9180a57SEric Paris sbsec->mntpoint_sid = context_sid; 709c9180a57SEric Paris sbsec->behavior = SECURITY_FS_USE_MNTPOINT; 710c9180a57SEric Paris } 711c9180a57SEric Paris 712c9180a57SEric Paris if (rootcontext_sid) { 713c9180a57SEric Paris rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec, tsec); 714c9180a57SEric Paris if (rc) 715c9180a57SEric Paris goto out; 716c9180a57SEric Paris 717c9180a57SEric Paris root_isec->sid = rootcontext_sid; 718c9180a57SEric Paris root_isec->initialized = 1; 719c9180a57SEric Paris } 720c9180a57SEric Paris 721c9180a57SEric Paris if (defcontext_sid) { 722c9180a57SEric Paris if (sbsec->behavior != SECURITY_FS_USE_XATTR) { 723c9180a57SEric Paris rc = -EINVAL; 724c9180a57SEric Paris printk(KERN_WARNING "SELinux: defcontext option is " 725c9180a57SEric Paris "invalid for this filesystem type\n"); 726c9180a57SEric Paris goto out; 727c9180a57SEric Paris } 728c9180a57SEric Paris 729c9180a57SEric Paris if (defcontext_sid != sbsec->def_sid) { 730c9180a57SEric Paris rc = may_context_mount_inode_relabel(defcontext_sid, 731c9180a57SEric Paris sbsec, tsec); 732c9180a57SEric Paris if (rc) 733c9180a57SEric Paris goto out; 734c9180a57SEric Paris } 735c9180a57SEric Paris 736c9180a57SEric Paris sbsec->def_sid = defcontext_sid; 737c9180a57SEric Paris } 738c9180a57SEric Paris 739c9180a57SEric Paris rc = sb_finish_set_opts(sb); 740c9180a57SEric Paris out: 741bc7e982bSEric Paris mutex_unlock(&sbsec->lock); 7421da177e4SLinus Torvalds return rc; 743c9180a57SEric Paris out_double_mount: 744c9180a57SEric Paris rc = -EINVAL; 745c9180a57SEric Paris printk(KERN_WARNING "SELinux: mount invalid. Same superblock, different " 746c9180a57SEric Paris "security settings for (dev %s, type %s)\n", sb->s_id, name); 747c9180a57SEric Paris goto out; 748c9180a57SEric Paris } 749c9180a57SEric Paris 750c9180a57SEric Paris static void selinux_sb_clone_mnt_opts(const struct super_block *oldsb, 751c9180a57SEric Paris struct super_block *newsb) 752c9180a57SEric Paris { 753c9180a57SEric Paris const struct superblock_security_struct *oldsbsec = oldsb->s_security; 754c9180a57SEric Paris struct superblock_security_struct *newsbsec = newsb->s_security; 755c9180a57SEric Paris 756c9180a57SEric Paris int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT); 757c9180a57SEric Paris int set_context = (oldsbsec->flags & CONTEXT_MNT); 758c9180a57SEric Paris int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT); 759c9180a57SEric Paris 7600f5e6420SEric Paris /* 7610f5e6420SEric Paris * if the parent was able to be mounted it clearly had no special lsm 7620f5e6420SEric Paris * mount options. thus we can safely put this sb on the list and deal 7630f5e6420SEric Paris * with it later 7640f5e6420SEric Paris */ 7650f5e6420SEric Paris if (!ss_initialized) { 7660f5e6420SEric Paris spin_lock(&sb_security_lock); 7670f5e6420SEric Paris if (list_empty(&newsbsec->list)) 7680f5e6420SEric Paris list_add(&newsbsec->list, &superblock_security_head); 7690f5e6420SEric Paris spin_unlock(&sb_security_lock); 7700f5e6420SEric Paris return; 7710f5e6420SEric Paris } 772c9180a57SEric Paris 773c9180a57SEric Paris /* how can we clone if the old one wasn't set up?? */ 774c9180a57SEric Paris BUG_ON(!oldsbsec->initialized); 775c9180a57SEric Paris 7765a552617SEric Paris /* if fs is reusing a sb, just let its options stand... */ 7775a552617SEric Paris if (newsbsec->initialized) 7785a552617SEric Paris return; 7795a552617SEric Paris 780c9180a57SEric Paris mutex_lock(&newsbsec->lock); 781c9180a57SEric Paris 782c9180a57SEric Paris newsbsec->flags = oldsbsec->flags; 783c9180a57SEric Paris 784c9180a57SEric Paris newsbsec->sid = oldsbsec->sid; 785c9180a57SEric Paris newsbsec->def_sid = oldsbsec->def_sid; 786c9180a57SEric Paris newsbsec->behavior = oldsbsec->behavior; 787c9180a57SEric Paris 788c9180a57SEric Paris if (set_context) { 789c9180a57SEric Paris u32 sid = oldsbsec->mntpoint_sid; 790c9180a57SEric Paris 791c9180a57SEric Paris if (!set_fscontext) 792c9180a57SEric Paris newsbsec->sid = sid; 793c9180a57SEric Paris if (!set_rootcontext) { 794c9180a57SEric Paris struct inode *newinode = newsb->s_root->d_inode; 795c9180a57SEric Paris struct inode_security_struct *newisec = newinode->i_security; 796c9180a57SEric Paris newisec->sid = sid; 797c9180a57SEric Paris } 798c9180a57SEric Paris newsbsec->mntpoint_sid = sid; 799c9180a57SEric Paris } 800c9180a57SEric Paris if (set_rootcontext) { 801c9180a57SEric Paris const struct inode *oldinode = oldsb->s_root->d_inode; 802c9180a57SEric Paris const struct inode_security_struct *oldisec = oldinode->i_security; 803c9180a57SEric Paris struct inode *newinode = newsb->s_root->d_inode; 804c9180a57SEric Paris struct inode_security_struct *newisec = newinode->i_security; 805c9180a57SEric Paris 806c9180a57SEric Paris newisec->sid = oldisec->sid; 807c9180a57SEric Paris } 808c9180a57SEric Paris 809c9180a57SEric Paris sb_finish_set_opts(newsb); 810c9180a57SEric Paris mutex_unlock(&newsbsec->lock); 811c9180a57SEric Paris } 812c9180a57SEric Paris 8132e1479d9SAdrian Bunk static int selinux_parse_opts_str(char *options, 8142e1479d9SAdrian Bunk struct security_mnt_opts *opts) 815c9180a57SEric Paris { 816e0007529SEric Paris char *p; 817c9180a57SEric Paris char *context = NULL, *defcontext = NULL; 818c9180a57SEric Paris char *fscontext = NULL, *rootcontext = NULL; 819e0007529SEric Paris int rc, num_mnt_opts = 0; 820c9180a57SEric Paris 821e0007529SEric Paris opts->num_mnt_opts = 0; 822c9180a57SEric Paris 823c9180a57SEric Paris /* Standard string-based options. */ 824c9180a57SEric Paris while ((p = strsep(&options, "|")) != NULL) { 825c9180a57SEric Paris int token; 826c9180a57SEric Paris substring_t args[MAX_OPT_ARGS]; 827c9180a57SEric Paris 828c9180a57SEric Paris if (!*p) 829c9180a57SEric Paris continue; 830c9180a57SEric Paris 831c9180a57SEric Paris token = match_token(p, tokens, args); 832c9180a57SEric Paris 833c9180a57SEric Paris switch (token) { 834c9180a57SEric Paris case Opt_context: 835c9180a57SEric Paris if (context || defcontext) { 836c9180a57SEric Paris rc = -EINVAL; 837c9180a57SEric Paris printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 838c9180a57SEric Paris goto out_err; 839c9180a57SEric Paris } 840c9180a57SEric Paris context = match_strdup(&args[0]); 841c9180a57SEric Paris if (!context) { 842c9180a57SEric Paris rc = -ENOMEM; 843c9180a57SEric Paris goto out_err; 844c9180a57SEric Paris } 845c9180a57SEric Paris break; 846c9180a57SEric Paris 847c9180a57SEric Paris case Opt_fscontext: 848c9180a57SEric Paris if (fscontext) { 849c9180a57SEric Paris rc = -EINVAL; 850c9180a57SEric Paris printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 851c9180a57SEric Paris goto out_err; 852c9180a57SEric Paris } 853c9180a57SEric Paris fscontext = match_strdup(&args[0]); 854c9180a57SEric Paris if (!fscontext) { 855c9180a57SEric Paris rc = -ENOMEM; 856c9180a57SEric Paris goto out_err; 857c9180a57SEric Paris } 858c9180a57SEric Paris break; 859c9180a57SEric Paris 860c9180a57SEric Paris case Opt_rootcontext: 861c9180a57SEric Paris if (rootcontext) { 862c9180a57SEric Paris rc = -EINVAL; 863c9180a57SEric Paris printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 864c9180a57SEric Paris goto out_err; 865c9180a57SEric Paris } 866c9180a57SEric Paris rootcontext = match_strdup(&args[0]); 867c9180a57SEric Paris if (!rootcontext) { 868c9180a57SEric Paris rc = -ENOMEM; 869c9180a57SEric Paris goto out_err; 870c9180a57SEric Paris } 871c9180a57SEric Paris break; 872c9180a57SEric Paris 873c9180a57SEric Paris case Opt_defcontext: 874c9180a57SEric Paris if (context || defcontext) { 875c9180a57SEric Paris rc = -EINVAL; 876c9180a57SEric Paris printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 877c9180a57SEric Paris goto out_err; 878c9180a57SEric Paris } 879c9180a57SEric Paris defcontext = match_strdup(&args[0]); 880c9180a57SEric Paris if (!defcontext) { 881c9180a57SEric Paris rc = -ENOMEM; 882c9180a57SEric Paris goto out_err; 883c9180a57SEric Paris } 884c9180a57SEric Paris break; 885c9180a57SEric Paris 886c9180a57SEric Paris default: 887c9180a57SEric Paris rc = -EINVAL; 888c9180a57SEric Paris printk(KERN_WARNING "SELinux: unknown mount option\n"); 889c9180a57SEric Paris goto out_err; 890c9180a57SEric Paris 891c9180a57SEric Paris } 892c9180a57SEric Paris } 893c9180a57SEric Paris 894e0007529SEric Paris rc = -ENOMEM; 895e0007529SEric Paris opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_ATOMIC); 896e0007529SEric Paris if (!opts->mnt_opts) 897e0007529SEric Paris goto out_err; 898e0007529SEric Paris 899e0007529SEric Paris opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), GFP_ATOMIC); 900e0007529SEric Paris if (!opts->mnt_opts_flags) { 901e0007529SEric Paris kfree(opts->mnt_opts); 902e0007529SEric Paris goto out_err; 903c9180a57SEric Paris } 904c9180a57SEric Paris 905e0007529SEric Paris if (fscontext) { 906e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = fscontext; 907e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT; 908e0007529SEric Paris } 909e0007529SEric Paris if (context) { 910e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = context; 911e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT; 912e0007529SEric Paris } 913e0007529SEric Paris if (rootcontext) { 914e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = rootcontext; 915e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT; 916e0007529SEric Paris } 917e0007529SEric Paris if (defcontext) { 918e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = defcontext; 919e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT; 920e0007529SEric Paris } 921e0007529SEric Paris 922e0007529SEric Paris opts->num_mnt_opts = num_mnt_opts; 923e0007529SEric Paris return 0; 924e0007529SEric Paris 925c9180a57SEric Paris out_err: 926c9180a57SEric Paris kfree(context); 927c9180a57SEric Paris kfree(defcontext); 928c9180a57SEric Paris kfree(fscontext); 929c9180a57SEric Paris kfree(rootcontext); 930c9180a57SEric Paris return rc; 9311da177e4SLinus Torvalds } 932e0007529SEric Paris /* 933e0007529SEric Paris * string mount options parsing and call set the sbsec 934e0007529SEric Paris */ 935e0007529SEric Paris static int superblock_doinit(struct super_block *sb, void *data) 936e0007529SEric Paris { 937e0007529SEric Paris int rc = 0; 938e0007529SEric Paris char *options = data; 939e0007529SEric Paris struct security_mnt_opts opts; 940e0007529SEric Paris 941e0007529SEric Paris security_init_mnt_opts(&opts); 942e0007529SEric Paris 943e0007529SEric Paris if (!data) 944e0007529SEric Paris goto out; 945e0007529SEric Paris 946e0007529SEric Paris BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA); 947e0007529SEric Paris 948e0007529SEric Paris rc = selinux_parse_opts_str(options, &opts); 949e0007529SEric Paris if (rc) 950e0007529SEric Paris goto out_err; 951e0007529SEric Paris 952e0007529SEric Paris out: 953e0007529SEric Paris rc = selinux_set_mnt_opts(sb, &opts); 954e0007529SEric Paris 955e0007529SEric Paris out_err: 956e0007529SEric Paris security_free_mnt_opts(&opts); 957e0007529SEric Paris return rc; 958e0007529SEric Paris } 9591da177e4SLinus Torvalds 9603583a711SAdrian Bunk static void selinux_write_opts(struct seq_file *m, 9613583a711SAdrian Bunk struct security_mnt_opts *opts) 9622069f457SEric Paris { 9632069f457SEric Paris int i; 9642069f457SEric Paris char *prefix; 9652069f457SEric Paris 9662069f457SEric Paris for (i = 0; i < opts->num_mnt_opts; i++) { 9672069f457SEric Paris char *has_comma = strchr(opts->mnt_opts[i], ','); 9682069f457SEric Paris 9692069f457SEric Paris switch (opts->mnt_opts_flags[i]) { 9702069f457SEric Paris case CONTEXT_MNT: 9712069f457SEric Paris prefix = CONTEXT_STR; 9722069f457SEric Paris break; 9732069f457SEric Paris case FSCONTEXT_MNT: 9742069f457SEric Paris prefix = FSCONTEXT_STR; 9752069f457SEric Paris break; 9762069f457SEric Paris case ROOTCONTEXT_MNT: 9772069f457SEric Paris prefix = ROOTCONTEXT_STR; 9782069f457SEric Paris break; 9792069f457SEric Paris case DEFCONTEXT_MNT: 9802069f457SEric Paris prefix = DEFCONTEXT_STR; 9812069f457SEric Paris break; 9822069f457SEric Paris default: 9832069f457SEric Paris BUG(); 9842069f457SEric Paris }; 9852069f457SEric Paris /* we need a comma before each option */ 9862069f457SEric Paris seq_putc(m, ','); 9872069f457SEric Paris seq_puts(m, prefix); 9882069f457SEric Paris if (has_comma) 9892069f457SEric Paris seq_putc(m, '\"'); 9902069f457SEric Paris seq_puts(m, opts->mnt_opts[i]); 9912069f457SEric Paris if (has_comma) 9922069f457SEric Paris seq_putc(m, '\"'); 9932069f457SEric Paris } 9942069f457SEric Paris } 9952069f457SEric Paris 9962069f457SEric Paris static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb) 9972069f457SEric Paris { 9982069f457SEric Paris struct security_mnt_opts opts; 9992069f457SEric Paris int rc; 10002069f457SEric Paris 10012069f457SEric Paris rc = selinux_get_mnt_opts(sb, &opts); 1002383795c2SEric Paris if (rc) { 1003383795c2SEric Paris /* before policy load we may get EINVAL, don't show anything */ 1004383795c2SEric Paris if (rc == -EINVAL) 1005383795c2SEric Paris rc = 0; 10062069f457SEric Paris return rc; 1007383795c2SEric Paris } 10082069f457SEric Paris 10092069f457SEric Paris selinux_write_opts(m, &opts); 10102069f457SEric Paris 10112069f457SEric Paris security_free_mnt_opts(&opts); 10122069f457SEric Paris 10132069f457SEric Paris return rc; 10142069f457SEric Paris } 10152069f457SEric Paris 10161da177e4SLinus Torvalds static inline u16 inode_mode_to_security_class(umode_t mode) 10171da177e4SLinus Torvalds { 10181da177e4SLinus Torvalds switch (mode & S_IFMT) { 10191da177e4SLinus Torvalds case S_IFSOCK: 10201da177e4SLinus Torvalds return SECCLASS_SOCK_FILE; 10211da177e4SLinus Torvalds case S_IFLNK: 10221da177e4SLinus Torvalds return SECCLASS_LNK_FILE; 10231da177e4SLinus Torvalds case S_IFREG: 10241da177e4SLinus Torvalds return SECCLASS_FILE; 10251da177e4SLinus Torvalds case S_IFBLK: 10261da177e4SLinus Torvalds return SECCLASS_BLK_FILE; 10271da177e4SLinus Torvalds case S_IFDIR: 10281da177e4SLinus Torvalds return SECCLASS_DIR; 10291da177e4SLinus Torvalds case S_IFCHR: 10301da177e4SLinus Torvalds return SECCLASS_CHR_FILE; 10311da177e4SLinus Torvalds case S_IFIFO: 10321da177e4SLinus Torvalds return SECCLASS_FIFO_FILE; 10331da177e4SLinus Torvalds 10341da177e4SLinus Torvalds } 10351da177e4SLinus Torvalds 10361da177e4SLinus Torvalds return SECCLASS_FILE; 10371da177e4SLinus Torvalds } 10381da177e4SLinus Torvalds 103913402580SJames Morris static inline int default_protocol_stream(int protocol) 104013402580SJames Morris { 104113402580SJames Morris return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP); 104213402580SJames Morris } 104313402580SJames Morris 104413402580SJames Morris static inline int default_protocol_dgram(int protocol) 104513402580SJames Morris { 104613402580SJames Morris return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP); 104713402580SJames Morris } 104813402580SJames Morris 10491da177e4SLinus Torvalds static inline u16 socket_type_to_security_class(int family, int type, int protocol) 10501da177e4SLinus Torvalds { 10511da177e4SLinus Torvalds switch (family) { 10521da177e4SLinus Torvalds case PF_UNIX: 10531da177e4SLinus Torvalds switch (type) { 10541da177e4SLinus Torvalds case SOCK_STREAM: 10551da177e4SLinus Torvalds case SOCK_SEQPACKET: 10561da177e4SLinus Torvalds return SECCLASS_UNIX_STREAM_SOCKET; 10571da177e4SLinus Torvalds case SOCK_DGRAM: 10581da177e4SLinus Torvalds return SECCLASS_UNIX_DGRAM_SOCKET; 10591da177e4SLinus Torvalds } 10601da177e4SLinus Torvalds break; 10611da177e4SLinus Torvalds case PF_INET: 10621da177e4SLinus Torvalds case PF_INET6: 10631da177e4SLinus Torvalds switch (type) { 10641da177e4SLinus Torvalds case SOCK_STREAM: 106513402580SJames Morris if (default_protocol_stream(protocol)) 10661da177e4SLinus Torvalds return SECCLASS_TCP_SOCKET; 106713402580SJames Morris else 106813402580SJames Morris return SECCLASS_RAWIP_SOCKET; 10691da177e4SLinus Torvalds case SOCK_DGRAM: 107013402580SJames Morris if (default_protocol_dgram(protocol)) 10711da177e4SLinus Torvalds return SECCLASS_UDP_SOCKET; 107213402580SJames Morris else 107313402580SJames Morris return SECCLASS_RAWIP_SOCKET; 10742ee92d46SJames Morris case SOCK_DCCP: 10752ee92d46SJames Morris return SECCLASS_DCCP_SOCKET; 107613402580SJames Morris default: 10771da177e4SLinus Torvalds return SECCLASS_RAWIP_SOCKET; 10781da177e4SLinus Torvalds } 10791da177e4SLinus Torvalds break; 10801da177e4SLinus Torvalds case PF_NETLINK: 10811da177e4SLinus Torvalds switch (protocol) { 10821da177e4SLinus Torvalds case NETLINK_ROUTE: 10831da177e4SLinus Torvalds return SECCLASS_NETLINK_ROUTE_SOCKET; 10841da177e4SLinus Torvalds case NETLINK_FIREWALL: 10851da177e4SLinus Torvalds return SECCLASS_NETLINK_FIREWALL_SOCKET; 1086216efaaaSJames Morris case NETLINK_INET_DIAG: 10871da177e4SLinus Torvalds return SECCLASS_NETLINK_TCPDIAG_SOCKET; 10881da177e4SLinus Torvalds case NETLINK_NFLOG: 10891da177e4SLinus Torvalds return SECCLASS_NETLINK_NFLOG_SOCKET; 10901da177e4SLinus Torvalds case NETLINK_XFRM: 10911da177e4SLinus Torvalds return SECCLASS_NETLINK_XFRM_SOCKET; 10921da177e4SLinus Torvalds case NETLINK_SELINUX: 10931da177e4SLinus Torvalds return SECCLASS_NETLINK_SELINUX_SOCKET; 10941da177e4SLinus Torvalds case NETLINK_AUDIT: 10951da177e4SLinus Torvalds return SECCLASS_NETLINK_AUDIT_SOCKET; 10961da177e4SLinus Torvalds case NETLINK_IP6_FW: 10971da177e4SLinus Torvalds return SECCLASS_NETLINK_IP6FW_SOCKET; 10981da177e4SLinus Torvalds case NETLINK_DNRTMSG: 10991da177e4SLinus Torvalds return SECCLASS_NETLINK_DNRT_SOCKET; 11000c9b7942SJames Morris case NETLINK_KOBJECT_UEVENT: 11010c9b7942SJames Morris return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET; 11021da177e4SLinus Torvalds default: 11031da177e4SLinus Torvalds return SECCLASS_NETLINK_SOCKET; 11041da177e4SLinus Torvalds } 11051da177e4SLinus Torvalds case PF_PACKET: 11061da177e4SLinus Torvalds return SECCLASS_PACKET_SOCKET; 11071da177e4SLinus Torvalds case PF_KEY: 11081da177e4SLinus Torvalds return SECCLASS_KEY_SOCKET; 11093e3ff15eSChristopher J. PeBenito case PF_APPLETALK: 11103e3ff15eSChristopher J. PeBenito return SECCLASS_APPLETALK_SOCKET; 11111da177e4SLinus Torvalds } 11121da177e4SLinus Torvalds 11131da177e4SLinus Torvalds return SECCLASS_SOCKET; 11141da177e4SLinus Torvalds } 11151da177e4SLinus Torvalds 11161da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 11171da177e4SLinus Torvalds static int selinux_proc_get_sid(struct proc_dir_entry *de, 11181da177e4SLinus Torvalds u16 tclass, 11191da177e4SLinus Torvalds u32 *sid) 11201da177e4SLinus Torvalds { 11211da177e4SLinus Torvalds int buflen, rc; 11221da177e4SLinus Torvalds char *buffer, *path, *end; 11231da177e4SLinus Torvalds 11241da177e4SLinus Torvalds buffer = (char *)__get_free_page(GFP_KERNEL); 11251da177e4SLinus Torvalds if (!buffer) 11261da177e4SLinus Torvalds return -ENOMEM; 11271da177e4SLinus Torvalds 11281da177e4SLinus Torvalds buflen = PAGE_SIZE; 11291da177e4SLinus Torvalds end = buffer+buflen; 11301da177e4SLinus Torvalds *--end = '\0'; 11311da177e4SLinus Torvalds buflen--; 11321da177e4SLinus Torvalds path = end-1; 11331da177e4SLinus Torvalds *path = '/'; 11341da177e4SLinus Torvalds while (de && de != de->parent) { 11351da177e4SLinus Torvalds buflen -= de->namelen + 1; 11361da177e4SLinus Torvalds if (buflen < 0) 11371da177e4SLinus Torvalds break; 11381da177e4SLinus Torvalds end -= de->namelen; 11391da177e4SLinus Torvalds memcpy(end, de->name, de->namelen); 11401da177e4SLinus Torvalds *--end = '/'; 11411da177e4SLinus Torvalds path = end; 11421da177e4SLinus Torvalds de = de->parent; 11431da177e4SLinus Torvalds } 11441da177e4SLinus Torvalds rc = security_genfs_sid("proc", path, tclass, sid); 11451da177e4SLinus Torvalds free_page((unsigned long)buffer); 11461da177e4SLinus Torvalds return rc; 11471da177e4SLinus Torvalds } 11481da177e4SLinus Torvalds #else 11491da177e4SLinus Torvalds static int selinux_proc_get_sid(struct proc_dir_entry *de, 11501da177e4SLinus Torvalds u16 tclass, 11511da177e4SLinus Torvalds u32 *sid) 11521da177e4SLinus Torvalds { 11531da177e4SLinus Torvalds return -EINVAL; 11541da177e4SLinus Torvalds } 11551da177e4SLinus Torvalds #endif 11561da177e4SLinus Torvalds 11571da177e4SLinus Torvalds /* The inode's security attributes must be initialized before first use. */ 11581da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry) 11591da177e4SLinus Torvalds { 11601da177e4SLinus Torvalds struct superblock_security_struct *sbsec = NULL; 11611da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 11621da177e4SLinus Torvalds u32 sid; 11631da177e4SLinus Torvalds struct dentry *dentry; 11641da177e4SLinus Torvalds #define INITCONTEXTLEN 255 11651da177e4SLinus Torvalds char *context = NULL; 11661da177e4SLinus Torvalds unsigned len = 0; 11671da177e4SLinus Torvalds int rc = 0; 11681da177e4SLinus Torvalds 11691da177e4SLinus Torvalds if (isec->initialized) 11701da177e4SLinus Torvalds goto out; 11711da177e4SLinus Torvalds 117223970741SEric Paris mutex_lock(&isec->lock); 11731da177e4SLinus Torvalds if (isec->initialized) 117423970741SEric Paris goto out_unlock; 11751da177e4SLinus Torvalds 11761da177e4SLinus Torvalds sbsec = inode->i_sb->s_security; 11771da177e4SLinus Torvalds if (!sbsec->initialized) { 11781da177e4SLinus Torvalds /* Defer initialization until selinux_complete_init, 11791da177e4SLinus Torvalds after the initial policy is loaded and the security 11801da177e4SLinus Torvalds server is ready to handle calls. */ 11811da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 11821da177e4SLinus Torvalds if (list_empty(&isec->list)) 11831da177e4SLinus Torvalds list_add(&isec->list, &sbsec->isec_head); 11841da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 118523970741SEric Paris goto out_unlock; 11861da177e4SLinus Torvalds } 11871da177e4SLinus Torvalds 11881da177e4SLinus Torvalds switch (sbsec->behavior) { 11891da177e4SLinus Torvalds case SECURITY_FS_USE_XATTR: 11901da177e4SLinus Torvalds if (!inode->i_op->getxattr) { 11911da177e4SLinus Torvalds isec->sid = sbsec->def_sid; 11921da177e4SLinus Torvalds break; 11931da177e4SLinus Torvalds } 11941da177e4SLinus Torvalds 11951da177e4SLinus Torvalds /* Need a dentry, since the xattr API requires one. 11961da177e4SLinus Torvalds Life would be simpler if we could just pass the inode. */ 11971da177e4SLinus Torvalds if (opt_dentry) { 11981da177e4SLinus Torvalds /* Called from d_instantiate or d_splice_alias. */ 11991da177e4SLinus Torvalds dentry = dget(opt_dentry); 12001da177e4SLinus Torvalds } else { 12011da177e4SLinus Torvalds /* Called from selinux_complete_init, try to find a dentry. */ 12021da177e4SLinus Torvalds dentry = d_find_alias(inode); 12031da177e4SLinus Torvalds } 12041da177e4SLinus Torvalds if (!dentry) { 1205744ba35eSEric Paris printk(KERN_WARNING "SELinux: %s: no dentry for dev=%s " 1206dd6f953aSHarvey Harrison "ino=%ld\n", __func__, inode->i_sb->s_id, 12071da177e4SLinus Torvalds inode->i_ino); 120823970741SEric Paris goto out_unlock; 12091da177e4SLinus Torvalds } 12101da177e4SLinus Torvalds 12111da177e4SLinus Torvalds len = INITCONTEXTLEN; 1212869ab514SStephen Smalley context = kmalloc(len, GFP_NOFS); 12131da177e4SLinus Torvalds if (!context) { 12141da177e4SLinus Torvalds rc = -ENOMEM; 12151da177e4SLinus Torvalds dput(dentry); 121623970741SEric Paris goto out_unlock; 12171da177e4SLinus Torvalds } 12181da177e4SLinus Torvalds rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX, 12191da177e4SLinus Torvalds context, len); 12201da177e4SLinus Torvalds if (rc == -ERANGE) { 12211da177e4SLinus Torvalds /* Need a larger buffer. Query for the right size. */ 12221da177e4SLinus Torvalds rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX, 12231da177e4SLinus Torvalds NULL, 0); 12241da177e4SLinus Torvalds if (rc < 0) { 12251da177e4SLinus Torvalds dput(dentry); 122623970741SEric Paris goto out_unlock; 12271da177e4SLinus Torvalds } 12281da177e4SLinus Torvalds kfree(context); 12291da177e4SLinus Torvalds len = rc; 1230869ab514SStephen Smalley context = kmalloc(len, GFP_NOFS); 12311da177e4SLinus Torvalds if (!context) { 12321da177e4SLinus Torvalds rc = -ENOMEM; 12331da177e4SLinus Torvalds dput(dentry); 123423970741SEric Paris goto out_unlock; 12351da177e4SLinus Torvalds } 12361da177e4SLinus Torvalds rc = inode->i_op->getxattr(dentry, 12371da177e4SLinus Torvalds XATTR_NAME_SELINUX, 12381da177e4SLinus Torvalds context, len); 12391da177e4SLinus Torvalds } 12401da177e4SLinus Torvalds dput(dentry); 12411da177e4SLinus Torvalds if (rc < 0) { 12421da177e4SLinus Torvalds if (rc != -ENODATA) { 1243744ba35eSEric Paris printk(KERN_WARNING "SELinux: %s: getxattr returned " 1244dd6f953aSHarvey Harrison "%d for dev=%s ino=%ld\n", __func__, 12451da177e4SLinus Torvalds -rc, inode->i_sb->s_id, inode->i_ino); 12461da177e4SLinus Torvalds kfree(context); 124723970741SEric Paris goto out_unlock; 12481da177e4SLinus Torvalds } 12491da177e4SLinus Torvalds /* Map ENODATA to the default file SID */ 12501da177e4SLinus Torvalds sid = sbsec->def_sid; 12511da177e4SLinus Torvalds rc = 0; 12521da177e4SLinus Torvalds } else { 1253f5c1d5b2SJames Morris rc = security_context_to_sid_default(context, rc, &sid, 1254869ab514SStephen Smalley sbsec->def_sid, 1255869ab514SStephen Smalley GFP_NOFS); 12561da177e4SLinus Torvalds if (rc) { 1257744ba35eSEric Paris printk(KERN_WARNING "SELinux: %s: context_to_sid(%s) " 12581da177e4SLinus Torvalds "returned %d for dev=%s ino=%ld\n", 1259dd6f953aSHarvey Harrison __func__, context, -rc, 12601da177e4SLinus Torvalds inode->i_sb->s_id, inode->i_ino); 12611da177e4SLinus Torvalds kfree(context); 12621da177e4SLinus Torvalds /* Leave with the unlabeled SID */ 12631da177e4SLinus Torvalds rc = 0; 12641da177e4SLinus Torvalds break; 12651da177e4SLinus Torvalds } 12661da177e4SLinus Torvalds } 12671da177e4SLinus Torvalds kfree(context); 12681da177e4SLinus Torvalds isec->sid = sid; 12691da177e4SLinus Torvalds break; 12701da177e4SLinus Torvalds case SECURITY_FS_USE_TASK: 12711da177e4SLinus Torvalds isec->sid = isec->task_sid; 12721da177e4SLinus Torvalds break; 12731da177e4SLinus Torvalds case SECURITY_FS_USE_TRANS: 12741da177e4SLinus Torvalds /* Default to the fs SID. */ 12751da177e4SLinus Torvalds isec->sid = sbsec->sid; 12761da177e4SLinus Torvalds 12771da177e4SLinus Torvalds /* Try to obtain a transition SID. */ 12781da177e4SLinus Torvalds isec->sclass = inode_mode_to_security_class(inode->i_mode); 12791da177e4SLinus Torvalds rc = security_transition_sid(isec->task_sid, 12801da177e4SLinus Torvalds sbsec->sid, 12811da177e4SLinus Torvalds isec->sclass, 12821da177e4SLinus Torvalds &sid); 12831da177e4SLinus Torvalds if (rc) 128423970741SEric Paris goto out_unlock; 12851da177e4SLinus Torvalds isec->sid = sid; 12861da177e4SLinus Torvalds break; 1287c312feb2SEric Paris case SECURITY_FS_USE_MNTPOINT: 1288c312feb2SEric Paris isec->sid = sbsec->mntpoint_sid; 1289c312feb2SEric Paris break; 12901da177e4SLinus Torvalds default: 1291c312feb2SEric Paris /* Default to the fs superblock SID. */ 12921da177e4SLinus Torvalds isec->sid = sbsec->sid; 12931da177e4SLinus Torvalds 12941da177e4SLinus Torvalds if (sbsec->proc) { 12951da177e4SLinus Torvalds struct proc_inode *proci = PROC_I(inode); 12961da177e4SLinus Torvalds if (proci->pde) { 12971da177e4SLinus Torvalds isec->sclass = inode_mode_to_security_class(inode->i_mode); 12981da177e4SLinus Torvalds rc = selinux_proc_get_sid(proci->pde, 12991da177e4SLinus Torvalds isec->sclass, 13001da177e4SLinus Torvalds &sid); 13011da177e4SLinus Torvalds if (rc) 130223970741SEric Paris goto out_unlock; 13031da177e4SLinus Torvalds isec->sid = sid; 13041da177e4SLinus Torvalds } 13051da177e4SLinus Torvalds } 13061da177e4SLinus Torvalds break; 13071da177e4SLinus Torvalds } 13081da177e4SLinus Torvalds 13091da177e4SLinus Torvalds isec->initialized = 1; 13101da177e4SLinus Torvalds 131123970741SEric Paris out_unlock: 131223970741SEric Paris mutex_unlock(&isec->lock); 13131da177e4SLinus Torvalds out: 13141da177e4SLinus Torvalds if (isec->sclass == SECCLASS_FILE) 13151da177e4SLinus Torvalds isec->sclass = inode_mode_to_security_class(inode->i_mode); 13161da177e4SLinus Torvalds return rc; 13171da177e4SLinus Torvalds } 13181da177e4SLinus Torvalds 13191da177e4SLinus Torvalds /* Convert a Linux signal to an access vector. */ 13201da177e4SLinus Torvalds static inline u32 signal_to_av(int sig) 13211da177e4SLinus Torvalds { 13221da177e4SLinus Torvalds u32 perm = 0; 13231da177e4SLinus Torvalds 13241da177e4SLinus Torvalds switch (sig) { 13251da177e4SLinus Torvalds case SIGCHLD: 13261da177e4SLinus Torvalds /* Commonly granted from child to parent. */ 13271da177e4SLinus Torvalds perm = PROCESS__SIGCHLD; 13281da177e4SLinus Torvalds break; 13291da177e4SLinus Torvalds case SIGKILL: 13301da177e4SLinus Torvalds /* Cannot be caught or ignored */ 13311da177e4SLinus Torvalds perm = PROCESS__SIGKILL; 13321da177e4SLinus Torvalds break; 13331da177e4SLinus Torvalds case SIGSTOP: 13341da177e4SLinus Torvalds /* Cannot be caught or ignored */ 13351da177e4SLinus Torvalds perm = PROCESS__SIGSTOP; 13361da177e4SLinus Torvalds break; 13371da177e4SLinus Torvalds default: 13381da177e4SLinus Torvalds /* All other signals. */ 13391da177e4SLinus Torvalds perm = PROCESS__SIGNAL; 13401da177e4SLinus Torvalds break; 13411da177e4SLinus Torvalds } 13421da177e4SLinus Torvalds 13431da177e4SLinus Torvalds return perm; 13441da177e4SLinus Torvalds } 13451da177e4SLinus Torvalds 13461da177e4SLinus Torvalds /* Check permission betweeen a pair of tasks, e.g. signal checks, 13471da177e4SLinus Torvalds fork check, ptrace check, etc. */ 13481da177e4SLinus Torvalds static int task_has_perm(struct task_struct *tsk1, 13491da177e4SLinus Torvalds struct task_struct *tsk2, 13501da177e4SLinus Torvalds u32 perms) 13511da177e4SLinus Torvalds { 13521da177e4SLinus Torvalds struct task_security_struct *tsec1, *tsec2; 13531da177e4SLinus Torvalds 13541da177e4SLinus Torvalds tsec1 = tsk1->security; 13551da177e4SLinus Torvalds tsec2 = tsk2->security; 13561da177e4SLinus Torvalds return avc_has_perm(tsec1->sid, tsec2->sid, 13571da177e4SLinus Torvalds SECCLASS_PROCESS, perms, NULL); 13581da177e4SLinus Torvalds } 13591da177e4SLinus Torvalds 1360b68e418cSStephen Smalley #if CAP_LAST_CAP > 63 1361b68e418cSStephen Smalley #error Fix SELinux to handle capabilities > 63. 1362b68e418cSStephen Smalley #endif 1363b68e418cSStephen Smalley 13641da177e4SLinus Torvalds /* Check whether a task is allowed to use a capability. */ 13651da177e4SLinus Torvalds static int task_has_capability(struct task_struct *tsk, 13661da177e4SLinus Torvalds int cap) 13671da177e4SLinus Torvalds { 13681da177e4SLinus Torvalds struct task_security_struct *tsec; 13691da177e4SLinus Torvalds struct avc_audit_data ad; 1370b68e418cSStephen Smalley u16 sclass; 1371b68e418cSStephen Smalley u32 av = CAP_TO_MASK(cap); 13721da177e4SLinus Torvalds 13731da177e4SLinus Torvalds tsec = tsk->security; 13741da177e4SLinus Torvalds 13751da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, CAP); 13761da177e4SLinus Torvalds ad.tsk = tsk; 13771da177e4SLinus Torvalds ad.u.cap = cap; 13781da177e4SLinus Torvalds 1379b68e418cSStephen Smalley switch (CAP_TO_INDEX(cap)) { 1380b68e418cSStephen Smalley case 0: 1381b68e418cSStephen Smalley sclass = SECCLASS_CAPABILITY; 1382b68e418cSStephen Smalley break; 1383b68e418cSStephen Smalley case 1: 1384b68e418cSStephen Smalley sclass = SECCLASS_CAPABILITY2; 1385b68e418cSStephen Smalley break; 1386b68e418cSStephen Smalley default: 1387b68e418cSStephen Smalley printk(KERN_ERR 1388b68e418cSStephen Smalley "SELinux: out of range capability %d\n", cap); 1389b68e418cSStephen Smalley BUG(); 1390b68e418cSStephen Smalley } 1391b68e418cSStephen Smalley return avc_has_perm(tsec->sid, tsec->sid, sclass, av, &ad); 13921da177e4SLinus Torvalds } 13931da177e4SLinus Torvalds 13941da177e4SLinus Torvalds /* Check whether a task is allowed to use a system operation. */ 13951da177e4SLinus Torvalds static int task_has_system(struct task_struct *tsk, 13961da177e4SLinus Torvalds u32 perms) 13971da177e4SLinus Torvalds { 13981da177e4SLinus Torvalds struct task_security_struct *tsec; 13991da177e4SLinus Torvalds 14001da177e4SLinus Torvalds tsec = tsk->security; 14011da177e4SLinus Torvalds 14021da177e4SLinus Torvalds return avc_has_perm(tsec->sid, SECINITSID_KERNEL, 14031da177e4SLinus Torvalds SECCLASS_SYSTEM, perms, NULL); 14041da177e4SLinus Torvalds } 14051da177e4SLinus Torvalds 14061da177e4SLinus Torvalds /* Check whether a task has a particular permission to an inode. 14071da177e4SLinus Torvalds The 'adp' parameter is optional and allows other audit 14081da177e4SLinus Torvalds data to be passed (e.g. the dentry). */ 14091da177e4SLinus Torvalds static int inode_has_perm(struct task_struct *tsk, 14101da177e4SLinus Torvalds struct inode *inode, 14111da177e4SLinus Torvalds u32 perms, 14121da177e4SLinus Torvalds struct avc_audit_data *adp) 14131da177e4SLinus Torvalds { 14141da177e4SLinus Torvalds struct task_security_struct *tsec; 14151da177e4SLinus Torvalds struct inode_security_struct *isec; 14161da177e4SLinus Torvalds struct avc_audit_data ad; 14171da177e4SLinus Torvalds 1418bbaca6c2SStephen Smalley if (unlikely(IS_PRIVATE(inode))) 1419bbaca6c2SStephen Smalley return 0; 1420bbaca6c2SStephen Smalley 14211da177e4SLinus Torvalds tsec = tsk->security; 14221da177e4SLinus Torvalds isec = inode->i_security; 14231da177e4SLinus Torvalds 14241da177e4SLinus Torvalds if (!adp) { 14251da177e4SLinus Torvalds adp = &ad; 14261da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 14271da177e4SLinus Torvalds ad.u.fs.inode = inode; 14281da177e4SLinus Torvalds } 14291da177e4SLinus Torvalds 14301da177e4SLinus Torvalds return avc_has_perm(tsec->sid, isec->sid, isec->sclass, perms, adp); 14311da177e4SLinus Torvalds } 14321da177e4SLinus Torvalds 14331da177e4SLinus Torvalds /* Same as inode_has_perm, but pass explicit audit data containing 14341da177e4SLinus Torvalds the dentry to help the auditing code to more easily generate the 14351da177e4SLinus Torvalds pathname if needed. */ 14361da177e4SLinus Torvalds static inline int dentry_has_perm(struct task_struct *tsk, 14371da177e4SLinus Torvalds struct vfsmount *mnt, 14381da177e4SLinus Torvalds struct dentry *dentry, 14391da177e4SLinus Torvalds u32 av) 14401da177e4SLinus Torvalds { 14411da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 14421da177e4SLinus Torvalds struct avc_audit_data ad; 14431da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 144444707fdfSJan Blunck ad.u.fs.path.mnt = mnt; 144544707fdfSJan Blunck ad.u.fs.path.dentry = dentry; 14461da177e4SLinus Torvalds return inode_has_perm(tsk, inode, av, &ad); 14471da177e4SLinus Torvalds } 14481da177e4SLinus Torvalds 14491da177e4SLinus Torvalds /* Check whether a task can use an open file descriptor to 14501da177e4SLinus Torvalds access an inode in a given way. Check access to the 14511da177e4SLinus Torvalds descriptor itself, and then use dentry_has_perm to 14521da177e4SLinus Torvalds check a particular permission to the file. 14531da177e4SLinus Torvalds Access to the descriptor is implicitly granted if it 14541da177e4SLinus Torvalds has the same SID as the process. If av is zero, then 14551da177e4SLinus Torvalds access to the file is not checked, e.g. for cases 14561da177e4SLinus Torvalds where only the descriptor is affected like seek. */ 1457858119e1SArjan van de Ven static int file_has_perm(struct task_struct *tsk, 14581da177e4SLinus Torvalds struct file *file, 14591da177e4SLinus Torvalds u32 av) 14601da177e4SLinus Torvalds { 14611da177e4SLinus Torvalds struct task_security_struct *tsec = tsk->security; 14621da177e4SLinus Torvalds struct file_security_struct *fsec = file->f_security; 146344707fdfSJan Blunck struct inode *inode = file->f_path.dentry->d_inode; 14641da177e4SLinus Torvalds struct avc_audit_data ad; 14651da177e4SLinus Torvalds int rc; 14661da177e4SLinus Torvalds 14671da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 146844707fdfSJan Blunck ad.u.fs.path = file->f_path; 14691da177e4SLinus Torvalds 14701da177e4SLinus Torvalds if (tsec->sid != fsec->sid) { 14711da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, fsec->sid, 14721da177e4SLinus Torvalds SECCLASS_FD, 14731da177e4SLinus Torvalds FD__USE, 14741da177e4SLinus Torvalds &ad); 14751da177e4SLinus Torvalds if (rc) 14761da177e4SLinus Torvalds return rc; 14771da177e4SLinus Torvalds } 14781da177e4SLinus Torvalds 14791da177e4SLinus Torvalds /* av is zero if only checking access to the descriptor. */ 14801da177e4SLinus Torvalds if (av) 14811da177e4SLinus Torvalds return inode_has_perm(tsk, inode, av, &ad); 14821da177e4SLinus Torvalds 14831da177e4SLinus Torvalds return 0; 14841da177e4SLinus Torvalds } 14851da177e4SLinus Torvalds 14861da177e4SLinus Torvalds /* Check whether a task can create a file. */ 14871da177e4SLinus Torvalds static int may_create(struct inode *dir, 14881da177e4SLinus Torvalds struct dentry *dentry, 14891da177e4SLinus Torvalds u16 tclass) 14901da177e4SLinus Torvalds { 14911da177e4SLinus Torvalds struct task_security_struct *tsec; 14921da177e4SLinus Torvalds struct inode_security_struct *dsec; 14931da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 14941da177e4SLinus Torvalds u32 newsid; 14951da177e4SLinus Torvalds struct avc_audit_data ad; 14961da177e4SLinus Torvalds int rc; 14971da177e4SLinus Torvalds 14981da177e4SLinus Torvalds tsec = current->security; 14991da177e4SLinus Torvalds dsec = dir->i_security; 15001da177e4SLinus Torvalds sbsec = dir->i_sb->s_security; 15011da177e4SLinus Torvalds 15021da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 150344707fdfSJan Blunck ad.u.fs.path.dentry = dentry; 15041da177e4SLinus Torvalds 15051da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, dsec->sid, SECCLASS_DIR, 15061da177e4SLinus Torvalds DIR__ADD_NAME | DIR__SEARCH, 15071da177e4SLinus Torvalds &ad); 15081da177e4SLinus Torvalds if (rc) 15091da177e4SLinus Torvalds return rc; 15101da177e4SLinus Torvalds 15111da177e4SLinus Torvalds if (tsec->create_sid && sbsec->behavior != SECURITY_FS_USE_MNTPOINT) { 15121da177e4SLinus Torvalds newsid = tsec->create_sid; 15131da177e4SLinus Torvalds } else { 15141da177e4SLinus Torvalds rc = security_transition_sid(tsec->sid, dsec->sid, tclass, 15151da177e4SLinus Torvalds &newsid); 15161da177e4SLinus Torvalds if (rc) 15171da177e4SLinus Torvalds return rc; 15181da177e4SLinus Torvalds } 15191da177e4SLinus Torvalds 15201da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, newsid, tclass, FILE__CREATE, &ad); 15211da177e4SLinus Torvalds if (rc) 15221da177e4SLinus Torvalds return rc; 15231da177e4SLinus Torvalds 15241da177e4SLinus Torvalds return avc_has_perm(newsid, sbsec->sid, 15251da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 15261da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, &ad); 15271da177e4SLinus Torvalds } 15281da177e4SLinus Torvalds 15294eb582cfSMichael LeMay /* Check whether a task can create a key. */ 15304eb582cfSMichael LeMay static int may_create_key(u32 ksid, 15314eb582cfSMichael LeMay struct task_struct *ctx) 15324eb582cfSMichael LeMay { 15334eb582cfSMichael LeMay struct task_security_struct *tsec; 15344eb582cfSMichael LeMay 15354eb582cfSMichael LeMay tsec = ctx->security; 15364eb582cfSMichael LeMay 15374eb582cfSMichael LeMay return avc_has_perm(tsec->sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL); 15384eb582cfSMichael LeMay } 15394eb582cfSMichael LeMay 15401da177e4SLinus Torvalds #define MAY_LINK 0 15411da177e4SLinus Torvalds #define MAY_UNLINK 1 15421da177e4SLinus Torvalds #define MAY_RMDIR 2 15431da177e4SLinus Torvalds 15441da177e4SLinus Torvalds /* Check whether a task can link, unlink, or rmdir a file/directory. */ 15451da177e4SLinus Torvalds static int may_link(struct inode *dir, 15461da177e4SLinus Torvalds struct dentry *dentry, 15471da177e4SLinus Torvalds int kind) 15481da177e4SLinus Torvalds 15491da177e4SLinus Torvalds { 15501da177e4SLinus Torvalds struct task_security_struct *tsec; 15511da177e4SLinus Torvalds struct inode_security_struct *dsec, *isec; 15521da177e4SLinus Torvalds struct avc_audit_data ad; 15531da177e4SLinus Torvalds u32 av; 15541da177e4SLinus Torvalds int rc; 15551da177e4SLinus Torvalds 15561da177e4SLinus Torvalds tsec = current->security; 15571da177e4SLinus Torvalds dsec = dir->i_security; 15581da177e4SLinus Torvalds isec = dentry->d_inode->i_security; 15591da177e4SLinus Torvalds 15601da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 156144707fdfSJan Blunck ad.u.fs.path.dentry = dentry; 15621da177e4SLinus Torvalds 15631da177e4SLinus Torvalds av = DIR__SEARCH; 15641da177e4SLinus Torvalds av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME); 15651da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, dsec->sid, SECCLASS_DIR, av, &ad); 15661da177e4SLinus Torvalds if (rc) 15671da177e4SLinus Torvalds return rc; 15681da177e4SLinus Torvalds 15691da177e4SLinus Torvalds switch (kind) { 15701da177e4SLinus Torvalds case MAY_LINK: 15711da177e4SLinus Torvalds av = FILE__LINK; 15721da177e4SLinus Torvalds break; 15731da177e4SLinus Torvalds case MAY_UNLINK: 15741da177e4SLinus Torvalds av = FILE__UNLINK; 15751da177e4SLinus Torvalds break; 15761da177e4SLinus Torvalds case MAY_RMDIR: 15771da177e4SLinus Torvalds av = DIR__RMDIR; 15781da177e4SLinus Torvalds break; 15791da177e4SLinus Torvalds default: 1580744ba35eSEric Paris printk(KERN_WARNING "SELinux: %s: unrecognized kind %d\n", 1581744ba35eSEric Paris __func__, kind); 15821da177e4SLinus Torvalds return 0; 15831da177e4SLinus Torvalds } 15841da177e4SLinus Torvalds 15851da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, isec->sclass, av, &ad); 15861da177e4SLinus Torvalds return rc; 15871da177e4SLinus Torvalds } 15881da177e4SLinus Torvalds 15891da177e4SLinus Torvalds static inline int may_rename(struct inode *old_dir, 15901da177e4SLinus Torvalds struct dentry *old_dentry, 15911da177e4SLinus Torvalds struct inode *new_dir, 15921da177e4SLinus Torvalds struct dentry *new_dentry) 15931da177e4SLinus Torvalds { 15941da177e4SLinus Torvalds struct task_security_struct *tsec; 15951da177e4SLinus Torvalds struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec; 15961da177e4SLinus Torvalds struct avc_audit_data ad; 15971da177e4SLinus Torvalds u32 av; 15981da177e4SLinus Torvalds int old_is_dir, new_is_dir; 15991da177e4SLinus Torvalds int rc; 16001da177e4SLinus Torvalds 16011da177e4SLinus Torvalds tsec = current->security; 16021da177e4SLinus Torvalds old_dsec = old_dir->i_security; 16031da177e4SLinus Torvalds old_isec = old_dentry->d_inode->i_security; 16041da177e4SLinus Torvalds old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 16051da177e4SLinus Torvalds new_dsec = new_dir->i_security; 16061da177e4SLinus Torvalds 16071da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 16081da177e4SLinus Torvalds 160944707fdfSJan Blunck ad.u.fs.path.dentry = old_dentry; 16101da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, old_dsec->sid, SECCLASS_DIR, 16111da177e4SLinus Torvalds DIR__REMOVE_NAME | DIR__SEARCH, &ad); 16121da177e4SLinus Torvalds if (rc) 16131da177e4SLinus Torvalds return rc; 16141da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, old_isec->sid, 16151da177e4SLinus Torvalds old_isec->sclass, FILE__RENAME, &ad); 16161da177e4SLinus Torvalds if (rc) 16171da177e4SLinus Torvalds return rc; 16181da177e4SLinus Torvalds if (old_is_dir && new_dir != old_dir) { 16191da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, old_isec->sid, 16201da177e4SLinus Torvalds old_isec->sclass, DIR__REPARENT, &ad); 16211da177e4SLinus Torvalds if (rc) 16221da177e4SLinus Torvalds return rc; 16231da177e4SLinus Torvalds } 16241da177e4SLinus Torvalds 162544707fdfSJan Blunck ad.u.fs.path.dentry = new_dentry; 16261da177e4SLinus Torvalds av = DIR__ADD_NAME | DIR__SEARCH; 16271da177e4SLinus Torvalds if (new_dentry->d_inode) 16281da177e4SLinus Torvalds av |= DIR__REMOVE_NAME; 16291da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, new_dsec->sid, SECCLASS_DIR, av, &ad); 16301da177e4SLinus Torvalds if (rc) 16311da177e4SLinus Torvalds return rc; 16321da177e4SLinus Torvalds if (new_dentry->d_inode) { 16331da177e4SLinus Torvalds new_isec = new_dentry->d_inode->i_security; 16341da177e4SLinus Torvalds new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode); 16351da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, new_isec->sid, 16361da177e4SLinus Torvalds new_isec->sclass, 16371da177e4SLinus Torvalds (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad); 16381da177e4SLinus Torvalds if (rc) 16391da177e4SLinus Torvalds return rc; 16401da177e4SLinus Torvalds } 16411da177e4SLinus Torvalds 16421da177e4SLinus Torvalds return 0; 16431da177e4SLinus Torvalds } 16441da177e4SLinus Torvalds 16451da177e4SLinus Torvalds /* Check whether a task can perform a filesystem operation. */ 16461da177e4SLinus Torvalds static int superblock_has_perm(struct task_struct *tsk, 16471da177e4SLinus Torvalds struct super_block *sb, 16481da177e4SLinus Torvalds u32 perms, 16491da177e4SLinus Torvalds struct avc_audit_data *ad) 16501da177e4SLinus Torvalds { 16511da177e4SLinus Torvalds struct task_security_struct *tsec; 16521da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 16531da177e4SLinus Torvalds 16541da177e4SLinus Torvalds tsec = tsk->security; 16551da177e4SLinus Torvalds sbsec = sb->s_security; 16561da177e4SLinus Torvalds return avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 16571da177e4SLinus Torvalds perms, ad); 16581da177e4SLinus Torvalds } 16591da177e4SLinus Torvalds 16601da177e4SLinus Torvalds /* Convert a Linux mode and permission mask to an access vector. */ 16611da177e4SLinus Torvalds static inline u32 file_mask_to_av(int mode, int mask) 16621da177e4SLinus Torvalds { 16631da177e4SLinus Torvalds u32 av = 0; 16641da177e4SLinus Torvalds 16651da177e4SLinus Torvalds if ((mode & S_IFMT) != S_IFDIR) { 16661da177e4SLinus Torvalds if (mask & MAY_EXEC) 16671da177e4SLinus Torvalds av |= FILE__EXECUTE; 16681da177e4SLinus Torvalds if (mask & MAY_READ) 16691da177e4SLinus Torvalds av |= FILE__READ; 16701da177e4SLinus Torvalds 16711da177e4SLinus Torvalds if (mask & MAY_APPEND) 16721da177e4SLinus Torvalds av |= FILE__APPEND; 16731da177e4SLinus Torvalds else if (mask & MAY_WRITE) 16741da177e4SLinus Torvalds av |= FILE__WRITE; 16751da177e4SLinus Torvalds 16761da177e4SLinus Torvalds } else { 16771da177e4SLinus Torvalds if (mask & MAY_EXEC) 16781da177e4SLinus Torvalds av |= DIR__SEARCH; 16791da177e4SLinus Torvalds if (mask & MAY_WRITE) 16801da177e4SLinus Torvalds av |= DIR__WRITE; 16811da177e4SLinus Torvalds if (mask & MAY_READ) 16821da177e4SLinus Torvalds av |= DIR__READ; 16831da177e4SLinus Torvalds } 16841da177e4SLinus Torvalds 16851da177e4SLinus Torvalds return av; 16861da177e4SLinus Torvalds } 16871da177e4SLinus Torvalds 1688b0c636b9SEric Paris /* 1689b0c636b9SEric Paris * Convert a file mask to an access vector and include the correct open 1690b0c636b9SEric Paris * open permission. 1691b0c636b9SEric Paris */ 1692b0c636b9SEric Paris static inline u32 open_file_mask_to_av(int mode, int mask) 1693b0c636b9SEric Paris { 1694b0c636b9SEric Paris u32 av = file_mask_to_av(mode, mask); 1695b0c636b9SEric Paris 1696b0c636b9SEric Paris if (selinux_policycap_openperm) { 1697b0c636b9SEric Paris /* 1698b0c636b9SEric Paris * lnk files and socks do not really have an 'open' 1699b0c636b9SEric Paris */ 1700b0c636b9SEric Paris if (S_ISREG(mode)) 1701b0c636b9SEric Paris av |= FILE__OPEN; 1702b0c636b9SEric Paris else if (S_ISCHR(mode)) 1703b0c636b9SEric Paris av |= CHR_FILE__OPEN; 1704b0c636b9SEric Paris else if (S_ISBLK(mode)) 1705b0c636b9SEric Paris av |= BLK_FILE__OPEN; 1706b0c636b9SEric Paris else if (S_ISFIFO(mode)) 1707b0c636b9SEric Paris av |= FIFO_FILE__OPEN; 1708b0c636b9SEric Paris else if (S_ISDIR(mode)) 1709b0c636b9SEric Paris av |= DIR__OPEN; 1710b0c636b9SEric Paris else 1711744ba35eSEric Paris printk(KERN_ERR "SELinux: WARNING: inside %s with " 1712744ba35eSEric Paris "unknown mode:%x\n", __func__, mode); 1713b0c636b9SEric Paris } 1714b0c636b9SEric Paris return av; 1715b0c636b9SEric Paris } 1716b0c636b9SEric Paris 17171da177e4SLinus Torvalds /* Convert a Linux file to an access vector. */ 17181da177e4SLinus Torvalds static inline u32 file_to_av(struct file *file) 17191da177e4SLinus Torvalds { 17201da177e4SLinus Torvalds u32 av = 0; 17211da177e4SLinus Torvalds 17221da177e4SLinus Torvalds if (file->f_mode & FMODE_READ) 17231da177e4SLinus Torvalds av |= FILE__READ; 17241da177e4SLinus Torvalds if (file->f_mode & FMODE_WRITE) { 17251da177e4SLinus Torvalds if (file->f_flags & O_APPEND) 17261da177e4SLinus Torvalds av |= FILE__APPEND; 17271da177e4SLinus Torvalds else 17281da177e4SLinus Torvalds av |= FILE__WRITE; 17291da177e4SLinus Torvalds } 17300794c66dSStephen Smalley if (!av) { 17310794c66dSStephen Smalley /* 17320794c66dSStephen Smalley * Special file opened with flags 3 for ioctl-only use. 17330794c66dSStephen Smalley */ 17340794c66dSStephen Smalley av = FILE__IOCTL; 17350794c66dSStephen Smalley } 17361da177e4SLinus Torvalds 17371da177e4SLinus Torvalds return av; 17381da177e4SLinus Torvalds } 17391da177e4SLinus Torvalds 17401da177e4SLinus Torvalds /* Hook functions begin here. */ 17411da177e4SLinus Torvalds 1742006ebb40SStephen Smalley static int selinux_ptrace(struct task_struct *parent, 1743006ebb40SStephen Smalley struct task_struct *child, 1744006ebb40SStephen Smalley unsigned int mode) 17451da177e4SLinus Torvalds { 17461da177e4SLinus Torvalds int rc; 17471da177e4SLinus Torvalds 1748006ebb40SStephen Smalley rc = secondary_ops->ptrace(parent, child, mode); 17491da177e4SLinus Torvalds if (rc) 17501da177e4SLinus Torvalds return rc; 17511da177e4SLinus Torvalds 1752006ebb40SStephen Smalley if (mode == PTRACE_MODE_READ) { 1753006ebb40SStephen Smalley struct task_security_struct *tsec = parent->security; 1754006ebb40SStephen Smalley struct task_security_struct *csec = child->security; 1755006ebb40SStephen Smalley return avc_has_perm(tsec->sid, csec->sid, 1756006ebb40SStephen Smalley SECCLASS_FILE, FILE__READ, NULL); 1757006ebb40SStephen Smalley } 1758006ebb40SStephen Smalley 17590356357cSRoland McGrath return task_has_perm(parent, child, PROCESS__PTRACE); 17601da177e4SLinus Torvalds } 17611da177e4SLinus Torvalds 17621da177e4SLinus Torvalds static int selinux_capget(struct task_struct *target, kernel_cap_t *effective, 17631da177e4SLinus Torvalds kernel_cap_t *inheritable, kernel_cap_t *permitted) 17641da177e4SLinus Torvalds { 17651da177e4SLinus Torvalds int error; 17661da177e4SLinus Torvalds 17671da177e4SLinus Torvalds error = task_has_perm(current, target, PROCESS__GETCAP); 17681da177e4SLinus Torvalds if (error) 17691da177e4SLinus Torvalds return error; 17701da177e4SLinus Torvalds 17711da177e4SLinus Torvalds return secondary_ops->capget(target, effective, inheritable, permitted); 17721da177e4SLinus Torvalds } 17731da177e4SLinus Torvalds 17741da177e4SLinus Torvalds static int selinux_capset_check(struct task_struct *target, kernel_cap_t *effective, 17751da177e4SLinus Torvalds kernel_cap_t *inheritable, kernel_cap_t *permitted) 17761da177e4SLinus Torvalds { 17771da177e4SLinus Torvalds int error; 17781da177e4SLinus Torvalds 17791da177e4SLinus Torvalds error = secondary_ops->capset_check(target, effective, inheritable, permitted); 17801da177e4SLinus Torvalds if (error) 17811da177e4SLinus Torvalds return error; 17821da177e4SLinus Torvalds 17831da177e4SLinus Torvalds return task_has_perm(current, target, PROCESS__SETCAP); 17841da177e4SLinus Torvalds } 17851da177e4SLinus Torvalds 17861da177e4SLinus Torvalds static void selinux_capset_set(struct task_struct *target, kernel_cap_t *effective, 17871da177e4SLinus Torvalds kernel_cap_t *inheritable, kernel_cap_t *permitted) 17881da177e4SLinus Torvalds { 17891da177e4SLinus Torvalds secondary_ops->capset_set(target, effective, inheritable, permitted); 17901da177e4SLinus Torvalds } 17911da177e4SLinus Torvalds 17921da177e4SLinus Torvalds static int selinux_capable(struct task_struct *tsk, int cap) 17931da177e4SLinus Torvalds { 17941da177e4SLinus Torvalds int rc; 17951da177e4SLinus Torvalds 17961da177e4SLinus Torvalds rc = secondary_ops->capable(tsk, cap); 17971da177e4SLinus Torvalds if (rc) 17981da177e4SLinus Torvalds return rc; 17991da177e4SLinus Torvalds 18001da177e4SLinus Torvalds return task_has_capability(tsk, cap); 18011da177e4SLinus Torvalds } 18021da177e4SLinus Torvalds 18033fbfa981SEric W. Biederman static int selinux_sysctl_get_sid(ctl_table *table, u16 tclass, u32 *sid) 18043fbfa981SEric W. Biederman { 18053fbfa981SEric W. Biederman int buflen, rc; 18063fbfa981SEric W. Biederman char *buffer, *path, *end; 18073fbfa981SEric W. Biederman 18083fbfa981SEric W. Biederman rc = -ENOMEM; 18093fbfa981SEric W. Biederman buffer = (char *)__get_free_page(GFP_KERNEL); 18103fbfa981SEric W. Biederman if (!buffer) 18113fbfa981SEric W. Biederman goto out; 18123fbfa981SEric W. Biederman 18133fbfa981SEric W. Biederman buflen = PAGE_SIZE; 18143fbfa981SEric W. Biederman end = buffer+buflen; 18153fbfa981SEric W. Biederman *--end = '\0'; 18163fbfa981SEric W. Biederman buflen--; 18173fbfa981SEric W. Biederman path = end-1; 18183fbfa981SEric W. Biederman *path = '/'; 18193fbfa981SEric W. Biederman while (table) { 18203fbfa981SEric W. Biederman const char *name = table->procname; 18213fbfa981SEric W. Biederman size_t namelen = strlen(name); 18223fbfa981SEric W. Biederman buflen -= namelen + 1; 18233fbfa981SEric W. Biederman if (buflen < 0) 18243fbfa981SEric W. Biederman goto out_free; 18253fbfa981SEric W. Biederman end -= namelen; 18263fbfa981SEric W. Biederman memcpy(end, name, namelen); 18273fbfa981SEric W. Biederman *--end = '/'; 18283fbfa981SEric W. Biederman path = end; 18293fbfa981SEric W. Biederman table = table->parent; 18303fbfa981SEric W. Biederman } 1831b599fdfdSEric W. Biederman buflen -= 4; 1832b599fdfdSEric W. Biederman if (buflen < 0) 1833b599fdfdSEric W. Biederman goto out_free; 1834b599fdfdSEric W. Biederman end -= 4; 1835b599fdfdSEric W. Biederman memcpy(end, "/sys", 4); 1836b599fdfdSEric W. Biederman path = end; 18373fbfa981SEric W. Biederman rc = security_genfs_sid("proc", path, tclass, sid); 18383fbfa981SEric W. Biederman out_free: 18393fbfa981SEric W. Biederman free_page((unsigned long)buffer); 18403fbfa981SEric W. Biederman out: 18413fbfa981SEric W. Biederman return rc; 18423fbfa981SEric W. Biederman } 18433fbfa981SEric W. Biederman 18441da177e4SLinus Torvalds static int selinux_sysctl(ctl_table *table, int op) 18451da177e4SLinus Torvalds { 18461da177e4SLinus Torvalds int error = 0; 18471da177e4SLinus Torvalds u32 av; 18481da177e4SLinus Torvalds struct task_security_struct *tsec; 18491da177e4SLinus Torvalds u32 tsid; 18501da177e4SLinus Torvalds int rc; 18511da177e4SLinus Torvalds 18521da177e4SLinus Torvalds rc = secondary_ops->sysctl(table, op); 18531da177e4SLinus Torvalds if (rc) 18541da177e4SLinus Torvalds return rc; 18551da177e4SLinus Torvalds 18561da177e4SLinus Torvalds tsec = current->security; 18571da177e4SLinus Torvalds 18583fbfa981SEric W. Biederman rc = selinux_sysctl_get_sid(table, (op == 0001) ? 18591da177e4SLinus Torvalds SECCLASS_DIR : SECCLASS_FILE, &tsid); 18601da177e4SLinus Torvalds if (rc) { 18611da177e4SLinus Torvalds /* Default to the well-defined sysctl SID. */ 18621da177e4SLinus Torvalds tsid = SECINITSID_SYSCTL; 18631da177e4SLinus Torvalds } 18641da177e4SLinus Torvalds 18651da177e4SLinus Torvalds /* The op values are "defined" in sysctl.c, thereby creating 18661da177e4SLinus Torvalds * a bad coupling between this module and sysctl.c */ 18671da177e4SLinus Torvalds if (op == 001) { 18681da177e4SLinus Torvalds error = avc_has_perm(tsec->sid, tsid, 18691da177e4SLinus Torvalds SECCLASS_DIR, DIR__SEARCH, NULL); 18701da177e4SLinus Torvalds } else { 18711da177e4SLinus Torvalds av = 0; 18721da177e4SLinus Torvalds if (op & 004) 18731da177e4SLinus Torvalds av |= FILE__READ; 18741da177e4SLinus Torvalds if (op & 002) 18751da177e4SLinus Torvalds av |= FILE__WRITE; 18761da177e4SLinus Torvalds if (av) 18771da177e4SLinus Torvalds error = avc_has_perm(tsec->sid, tsid, 18781da177e4SLinus Torvalds SECCLASS_FILE, av, NULL); 18791da177e4SLinus Torvalds } 18801da177e4SLinus Torvalds 18811da177e4SLinus Torvalds return error; 18821da177e4SLinus Torvalds } 18831da177e4SLinus Torvalds 18841da177e4SLinus Torvalds static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb) 18851da177e4SLinus Torvalds { 18861da177e4SLinus Torvalds int rc = 0; 18871da177e4SLinus Torvalds 18881da177e4SLinus Torvalds if (!sb) 18891da177e4SLinus Torvalds return 0; 18901da177e4SLinus Torvalds 18911da177e4SLinus Torvalds switch (cmds) { 18921da177e4SLinus Torvalds case Q_SYNC: 18931da177e4SLinus Torvalds case Q_QUOTAON: 18941da177e4SLinus Torvalds case Q_QUOTAOFF: 18951da177e4SLinus Torvalds case Q_SETINFO: 18961da177e4SLinus Torvalds case Q_SETQUOTA: 1897828dfe1dSEric Paris rc = superblock_has_perm(current, sb, FILESYSTEM__QUOTAMOD, 1898828dfe1dSEric Paris NULL); 18991da177e4SLinus Torvalds break; 19001da177e4SLinus Torvalds case Q_GETFMT: 19011da177e4SLinus Torvalds case Q_GETINFO: 19021da177e4SLinus Torvalds case Q_GETQUOTA: 1903828dfe1dSEric Paris rc = superblock_has_perm(current, sb, FILESYSTEM__QUOTAGET, 1904828dfe1dSEric Paris NULL); 19051da177e4SLinus Torvalds break; 19061da177e4SLinus Torvalds default: 19071da177e4SLinus Torvalds rc = 0; /* let the kernel handle invalid cmds */ 19081da177e4SLinus Torvalds break; 19091da177e4SLinus Torvalds } 19101da177e4SLinus Torvalds return rc; 19111da177e4SLinus Torvalds } 19121da177e4SLinus Torvalds 19131da177e4SLinus Torvalds static int selinux_quota_on(struct dentry *dentry) 19141da177e4SLinus Torvalds { 19151da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__QUOTAON); 19161da177e4SLinus Torvalds } 19171da177e4SLinus Torvalds 19181da177e4SLinus Torvalds static int selinux_syslog(int type) 19191da177e4SLinus Torvalds { 19201da177e4SLinus Torvalds int rc; 19211da177e4SLinus Torvalds 19221da177e4SLinus Torvalds rc = secondary_ops->syslog(type); 19231da177e4SLinus Torvalds if (rc) 19241da177e4SLinus Torvalds return rc; 19251da177e4SLinus Torvalds 19261da177e4SLinus Torvalds switch (type) { 19271da177e4SLinus Torvalds case 3: /* Read last kernel messages */ 19281da177e4SLinus Torvalds case 10: /* Return size of the log buffer */ 19291da177e4SLinus Torvalds rc = task_has_system(current, SYSTEM__SYSLOG_READ); 19301da177e4SLinus Torvalds break; 19311da177e4SLinus Torvalds case 6: /* Disable logging to console */ 19321da177e4SLinus Torvalds case 7: /* Enable logging to console */ 19331da177e4SLinus Torvalds case 8: /* Set level of messages printed to console */ 19341da177e4SLinus Torvalds rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE); 19351da177e4SLinus Torvalds break; 19361da177e4SLinus Torvalds case 0: /* Close log */ 19371da177e4SLinus Torvalds case 1: /* Open log */ 19381da177e4SLinus Torvalds case 2: /* Read from log */ 19391da177e4SLinus Torvalds case 4: /* Read/clear last kernel messages */ 19401da177e4SLinus Torvalds case 5: /* Clear ring buffer */ 19411da177e4SLinus Torvalds default: 19421da177e4SLinus Torvalds rc = task_has_system(current, SYSTEM__SYSLOG_MOD); 19431da177e4SLinus Torvalds break; 19441da177e4SLinus Torvalds } 19451da177e4SLinus Torvalds return rc; 19461da177e4SLinus Torvalds } 19471da177e4SLinus Torvalds 19481da177e4SLinus Torvalds /* 19491da177e4SLinus Torvalds * Check that a process has enough memory to allocate a new virtual 19501da177e4SLinus Torvalds * mapping. 0 means there is enough memory for the allocation to 19511da177e4SLinus Torvalds * succeed and -ENOMEM implies there is not. 19521da177e4SLinus Torvalds * 19531da177e4SLinus Torvalds * Note that secondary_ops->capable and task_has_perm_noaudit return 0 19541da177e4SLinus Torvalds * if the capability is granted, but __vm_enough_memory requires 1 if 19551da177e4SLinus Torvalds * the capability is granted. 19561da177e4SLinus Torvalds * 19571da177e4SLinus Torvalds * Do not audit the selinux permission check, as this is applied to all 19581da177e4SLinus Torvalds * processes that allocate mappings. 19591da177e4SLinus Torvalds */ 196034b4e4aaSAlan Cox static int selinux_vm_enough_memory(struct mm_struct *mm, long pages) 19611da177e4SLinus Torvalds { 19621da177e4SLinus Torvalds int rc, cap_sys_admin = 0; 19631da177e4SLinus Torvalds struct task_security_struct *tsec = current->security; 19641da177e4SLinus Torvalds 19651da177e4SLinus Torvalds rc = secondary_ops->capable(current, CAP_SYS_ADMIN); 19661da177e4SLinus Torvalds if (rc == 0) 19671da177e4SLinus Torvalds rc = avc_has_perm_noaudit(tsec->sid, tsec->sid, 19681da177e4SLinus Torvalds SECCLASS_CAPABILITY, 19691da177e4SLinus Torvalds CAP_TO_MASK(CAP_SYS_ADMIN), 19702c3c05dbSStephen Smalley 0, 19711da177e4SLinus Torvalds NULL); 19721da177e4SLinus Torvalds 19731da177e4SLinus Torvalds if (rc == 0) 19741da177e4SLinus Torvalds cap_sys_admin = 1; 19751da177e4SLinus Torvalds 197634b4e4aaSAlan Cox return __vm_enough_memory(mm, pages, cap_sys_admin); 19771da177e4SLinus Torvalds } 19781da177e4SLinus Torvalds 19791da177e4SLinus Torvalds /* binprm security operations */ 19801da177e4SLinus Torvalds 19811da177e4SLinus Torvalds static int selinux_bprm_alloc_security(struct linux_binprm *bprm) 19821da177e4SLinus Torvalds { 19831da177e4SLinus Torvalds struct bprm_security_struct *bsec; 19841da177e4SLinus Torvalds 198589d155efSJames Morris bsec = kzalloc(sizeof(struct bprm_security_struct), GFP_KERNEL); 19861da177e4SLinus Torvalds if (!bsec) 19871da177e4SLinus Torvalds return -ENOMEM; 19881da177e4SLinus Torvalds 19891da177e4SLinus Torvalds bsec->sid = SECINITSID_UNLABELED; 19901da177e4SLinus Torvalds bsec->set = 0; 19911da177e4SLinus Torvalds 19921da177e4SLinus Torvalds bprm->security = bsec; 19931da177e4SLinus Torvalds return 0; 19941da177e4SLinus Torvalds } 19951da177e4SLinus Torvalds 19961da177e4SLinus Torvalds static int selinux_bprm_set_security(struct linux_binprm *bprm) 19971da177e4SLinus Torvalds { 19981da177e4SLinus Torvalds struct task_security_struct *tsec; 19993d5ff529SJosef Sipek struct inode *inode = bprm->file->f_path.dentry->d_inode; 20001da177e4SLinus Torvalds struct inode_security_struct *isec; 20011da177e4SLinus Torvalds struct bprm_security_struct *bsec; 20021da177e4SLinus Torvalds u32 newsid; 20031da177e4SLinus Torvalds struct avc_audit_data ad; 20041da177e4SLinus Torvalds int rc; 20051da177e4SLinus Torvalds 20061da177e4SLinus Torvalds rc = secondary_ops->bprm_set_security(bprm); 20071da177e4SLinus Torvalds if (rc) 20081da177e4SLinus Torvalds return rc; 20091da177e4SLinus Torvalds 20101da177e4SLinus Torvalds bsec = bprm->security; 20111da177e4SLinus Torvalds 20121da177e4SLinus Torvalds if (bsec->set) 20131da177e4SLinus Torvalds return 0; 20141da177e4SLinus Torvalds 20151da177e4SLinus Torvalds tsec = current->security; 20161da177e4SLinus Torvalds isec = inode->i_security; 20171da177e4SLinus Torvalds 20181da177e4SLinus Torvalds /* Default to the current task SID. */ 20191da177e4SLinus Torvalds bsec->sid = tsec->sid; 20201da177e4SLinus Torvalds 202128eba5bfSMichael LeMay /* Reset fs, key, and sock SIDs on execve. */ 20221da177e4SLinus Torvalds tsec->create_sid = 0; 202328eba5bfSMichael LeMay tsec->keycreate_sid = 0; 202442c3e03eSEric Paris tsec->sockcreate_sid = 0; 20251da177e4SLinus Torvalds 20261da177e4SLinus Torvalds if (tsec->exec_sid) { 20271da177e4SLinus Torvalds newsid = tsec->exec_sid; 20281da177e4SLinus Torvalds /* Reset exec SID on execve. */ 20291da177e4SLinus Torvalds tsec->exec_sid = 0; 20301da177e4SLinus Torvalds } else { 20311da177e4SLinus Torvalds /* Check for a default transition on this program. */ 20321da177e4SLinus Torvalds rc = security_transition_sid(tsec->sid, isec->sid, 20331da177e4SLinus Torvalds SECCLASS_PROCESS, &newsid); 20341da177e4SLinus Torvalds if (rc) 20351da177e4SLinus Torvalds return rc; 20361da177e4SLinus Torvalds } 20371da177e4SLinus Torvalds 20381da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 203944707fdfSJan Blunck ad.u.fs.path = bprm->file->f_path; 20401da177e4SLinus Torvalds 20413d5ff529SJosef Sipek if (bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) 20421da177e4SLinus Torvalds newsid = tsec->sid; 20431da177e4SLinus Torvalds 20441da177e4SLinus Torvalds if (tsec->sid == newsid) { 20451da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, 20461da177e4SLinus Torvalds SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad); 20471da177e4SLinus Torvalds if (rc) 20481da177e4SLinus Torvalds return rc; 20491da177e4SLinus Torvalds } else { 20501da177e4SLinus Torvalds /* Check permissions for the transition. */ 20511da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, newsid, 20521da177e4SLinus Torvalds SECCLASS_PROCESS, PROCESS__TRANSITION, &ad); 20531da177e4SLinus Torvalds if (rc) 20541da177e4SLinus Torvalds return rc; 20551da177e4SLinus Torvalds 20561da177e4SLinus Torvalds rc = avc_has_perm(newsid, isec->sid, 20571da177e4SLinus Torvalds SECCLASS_FILE, FILE__ENTRYPOINT, &ad); 20581da177e4SLinus Torvalds if (rc) 20591da177e4SLinus Torvalds return rc; 20601da177e4SLinus Torvalds 20611da177e4SLinus Torvalds /* Clear any possibly unsafe personality bits on exec: */ 20621da177e4SLinus Torvalds current->personality &= ~PER_CLEAR_ON_SETID; 20631da177e4SLinus Torvalds 20641da177e4SLinus Torvalds /* Set the security field to the new SID. */ 20651da177e4SLinus Torvalds bsec->sid = newsid; 20661da177e4SLinus Torvalds } 20671da177e4SLinus Torvalds 20681da177e4SLinus Torvalds bsec->set = 1; 20691da177e4SLinus Torvalds return 0; 20701da177e4SLinus Torvalds } 20711da177e4SLinus Torvalds 20721da177e4SLinus Torvalds static int selinux_bprm_check_security(struct linux_binprm *bprm) 20731da177e4SLinus Torvalds { 20741da177e4SLinus Torvalds return secondary_ops->bprm_check_security(bprm); 20751da177e4SLinus Torvalds } 20761da177e4SLinus Torvalds 20771da177e4SLinus Torvalds 20781da177e4SLinus Torvalds static int selinux_bprm_secureexec(struct linux_binprm *bprm) 20791da177e4SLinus Torvalds { 20801da177e4SLinus Torvalds struct task_security_struct *tsec = current->security; 20811da177e4SLinus Torvalds int atsecure = 0; 20821da177e4SLinus Torvalds 20831da177e4SLinus Torvalds if (tsec->osid != tsec->sid) { 20841da177e4SLinus Torvalds /* Enable secure mode for SIDs transitions unless 20851da177e4SLinus Torvalds the noatsecure permission is granted between 20861da177e4SLinus Torvalds the two SIDs, i.e. ahp returns 0. */ 20871da177e4SLinus Torvalds atsecure = avc_has_perm(tsec->osid, tsec->sid, 20881da177e4SLinus Torvalds SECCLASS_PROCESS, 20891da177e4SLinus Torvalds PROCESS__NOATSECURE, NULL); 20901da177e4SLinus Torvalds } 20911da177e4SLinus Torvalds 20921da177e4SLinus Torvalds return (atsecure || secondary_ops->bprm_secureexec(bprm)); 20931da177e4SLinus Torvalds } 20941da177e4SLinus Torvalds 20951da177e4SLinus Torvalds static void selinux_bprm_free_security(struct linux_binprm *bprm) 20961da177e4SLinus Torvalds { 20979a5f04bfSJesper Juhl kfree(bprm->security); 20981da177e4SLinus Torvalds bprm->security = NULL; 20991da177e4SLinus Torvalds } 21001da177e4SLinus Torvalds 21011da177e4SLinus Torvalds extern struct vfsmount *selinuxfs_mount; 21021da177e4SLinus Torvalds extern struct dentry *selinux_null; 21031da177e4SLinus Torvalds 21041da177e4SLinus Torvalds /* Derived from fs/exec.c:flush_old_files. */ 21051da177e4SLinus Torvalds static inline void flush_unauthorized_files(struct files_struct *files) 21061da177e4SLinus Torvalds { 21071da177e4SLinus Torvalds struct avc_audit_data ad; 21081da177e4SLinus Torvalds struct file *file, *devnull = NULL; 2109b20c8122SStephen Smalley struct tty_struct *tty; 2110badf1662SDipankar Sarma struct fdtable *fdt; 21111da177e4SLinus Torvalds long j = -1; 211224ec839cSPeter Zijlstra int drop_tty = 0; 21131da177e4SLinus Torvalds 2114b20c8122SStephen Smalley mutex_lock(&tty_mutex); 211524ec839cSPeter Zijlstra tty = get_current_tty(); 21161da177e4SLinus Torvalds if (tty) { 21171da177e4SLinus Torvalds file_list_lock(); 21182f512016SEric Dumazet file = list_entry(tty->tty_files.next, typeof(*file), f_u.fu_list); 21191da177e4SLinus Torvalds if (file) { 21201da177e4SLinus Torvalds /* Revalidate access to controlling tty. 21211da177e4SLinus Torvalds Use inode_has_perm on the tty inode directly rather 21221da177e4SLinus Torvalds than using file_has_perm, as this particular open 21231da177e4SLinus Torvalds file may belong to another process and we are only 21241da177e4SLinus Torvalds interested in the inode-based check here. */ 21253d5ff529SJosef Sipek struct inode *inode = file->f_path.dentry->d_inode; 21261da177e4SLinus Torvalds if (inode_has_perm(current, inode, 21271da177e4SLinus Torvalds FILE__READ | FILE__WRITE, NULL)) { 212824ec839cSPeter Zijlstra drop_tty = 1; 21291da177e4SLinus Torvalds } 21301da177e4SLinus Torvalds } 21311da177e4SLinus Torvalds file_list_unlock(); 21321da177e4SLinus Torvalds } 2133b20c8122SStephen Smalley mutex_unlock(&tty_mutex); 213498a27ba4SEric W. Biederman /* Reset controlling tty. */ 213598a27ba4SEric W. Biederman if (drop_tty) 213698a27ba4SEric W. Biederman no_tty(); 21371da177e4SLinus Torvalds 21381da177e4SLinus Torvalds /* Revalidate access to inherited open files. */ 21391da177e4SLinus Torvalds 21401da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 21411da177e4SLinus Torvalds 21421da177e4SLinus Torvalds spin_lock(&files->file_lock); 21431da177e4SLinus Torvalds for (;;) { 21441da177e4SLinus Torvalds unsigned long set, i; 21451da177e4SLinus Torvalds int fd; 21461da177e4SLinus Torvalds 21471da177e4SLinus Torvalds j++; 21481da177e4SLinus Torvalds i = j * __NFDBITS; 2149badf1662SDipankar Sarma fdt = files_fdtable(files); 2150bbea9f69SVadim Lobanov if (i >= fdt->max_fds) 21511da177e4SLinus Torvalds break; 2152badf1662SDipankar Sarma set = fdt->open_fds->fds_bits[j]; 21531da177e4SLinus Torvalds if (!set) 21541da177e4SLinus Torvalds continue; 21551da177e4SLinus Torvalds spin_unlock(&files->file_lock); 21561da177e4SLinus Torvalds for ( ; set ; i++, set >>= 1) { 21571da177e4SLinus Torvalds if (set & 1) { 21581da177e4SLinus Torvalds file = fget(i); 21591da177e4SLinus Torvalds if (!file) 21601da177e4SLinus Torvalds continue; 21611da177e4SLinus Torvalds if (file_has_perm(current, 21621da177e4SLinus Torvalds file, 21631da177e4SLinus Torvalds file_to_av(file))) { 21641da177e4SLinus Torvalds sys_close(i); 21651da177e4SLinus Torvalds fd = get_unused_fd(); 21661da177e4SLinus Torvalds if (fd != i) { 21671da177e4SLinus Torvalds if (fd >= 0) 21681da177e4SLinus Torvalds put_unused_fd(fd); 21691da177e4SLinus Torvalds fput(file); 21701da177e4SLinus Torvalds continue; 21711da177e4SLinus Torvalds } 21721da177e4SLinus Torvalds if (devnull) { 2173095975daSNick Piggin get_file(devnull); 21741da177e4SLinus Torvalds } else { 21751da177e4SLinus Torvalds devnull = dentry_open(dget(selinux_null), mntget(selinuxfs_mount), O_RDWR); 2176fc5d81e6SAkinobu Mita if (IS_ERR(devnull)) { 2177fc5d81e6SAkinobu Mita devnull = NULL; 21781da177e4SLinus Torvalds put_unused_fd(fd); 21791da177e4SLinus Torvalds fput(file); 21801da177e4SLinus Torvalds continue; 21811da177e4SLinus Torvalds } 21821da177e4SLinus Torvalds } 21831da177e4SLinus Torvalds fd_install(fd, devnull); 21841da177e4SLinus Torvalds } 21851da177e4SLinus Torvalds fput(file); 21861da177e4SLinus Torvalds } 21871da177e4SLinus Torvalds } 21881da177e4SLinus Torvalds spin_lock(&files->file_lock); 21891da177e4SLinus Torvalds 21901da177e4SLinus Torvalds } 21911da177e4SLinus Torvalds spin_unlock(&files->file_lock); 21921da177e4SLinus Torvalds } 21931da177e4SLinus Torvalds 21941da177e4SLinus Torvalds static void selinux_bprm_apply_creds(struct linux_binprm *bprm, int unsafe) 21951da177e4SLinus Torvalds { 21961da177e4SLinus Torvalds struct task_security_struct *tsec; 21971da177e4SLinus Torvalds struct bprm_security_struct *bsec; 21981da177e4SLinus Torvalds u32 sid; 21991da177e4SLinus Torvalds int rc; 22001da177e4SLinus Torvalds 22011da177e4SLinus Torvalds secondary_ops->bprm_apply_creds(bprm, unsafe); 22021da177e4SLinus Torvalds 22031da177e4SLinus Torvalds tsec = current->security; 22041da177e4SLinus Torvalds 22051da177e4SLinus Torvalds bsec = bprm->security; 22061da177e4SLinus Torvalds sid = bsec->sid; 22071da177e4SLinus Torvalds 22081da177e4SLinus Torvalds tsec->osid = tsec->sid; 22091da177e4SLinus Torvalds bsec->unsafe = 0; 22101da177e4SLinus Torvalds if (tsec->sid != sid) { 22111da177e4SLinus Torvalds /* Check for shared state. If not ok, leave SID 22121da177e4SLinus Torvalds unchanged and kill. */ 22131da177e4SLinus Torvalds if (unsafe & LSM_UNSAFE_SHARE) { 22141da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS, 22151da177e4SLinus Torvalds PROCESS__SHARE, NULL); 22161da177e4SLinus Torvalds if (rc) { 22171da177e4SLinus Torvalds bsec->unsafe = 1; 22181da177e4SLinus Torvalds return; 22191da177e4SLinus Torvalds } 22201da177e4SLinus Torvalds } 22211da177e4SLinus Torvalds 22221da177e4SLinus Torvalds /* Check for ptracing, and update the task SID if ok. 22231da177e4SLinus Torvalds Otherwise, leave SID unchanged and kill. */ 22241da177e4SLinus Torvalds if (unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) { 22250356357cSRoland McGrath struct task_struct *tracer; 22260356357cSRoland McGrath struct task_security_struct *sec; 22270356357cSRoland McGrath u32 ptsid = 0; 22280356357cSRoland McGrath 22290356357cSRoland McGrath rcu_read_lock(); 22300d094efeSRoland McGrath tracer = tracehook_tracer_task(current); 22310356357cSRoland McGrath if (likely(tracer != NULL)) { 22320356357cSRoland McGrath sec = tracer->security; 22330356357cSRoland McGrath ptsid = sec->sid; 22340356357cSRoland McGrath } 22350356357cSRoland McGrath rcu_read_unlock(); 22360356357cSRoland McGrath 22370356357cSRoland McGrath if (ptsid != 0) { 22380356357cSRoland McGrath rc = avc_has_perm(ptsid, sid, SECCLASS_PROCESS, 22390356357cSRoland McGrath PROCESS__PTRACE, NULL); 22401da177e4SLinus Torvalds if (rc) { 22411da177e4SLinus Torvalds bsec->unsafe = 1; 22421da177e4SLinus Torvalds return; 22431da177e4SLinus Torvalds } 22441da177e4SLinus Torvalds } 22450356357cSRoland McGrath } 22461da177e4SLinus Torvalds tsec->sid = sid; 22471da177e4SLinus Torvalds } 22481da177e4SLinus Torvalds } 22491da177e4SLinus Torvalds 22501da177e4SLinus Torvalds /* 22511da177e4SLinus Torvalds * called after apply_creds without the task lock held 22521da177e4SLinus Torvalds */ 22531da177e4SLinus Torvalds static void selinux_bprm_post_apply_creds(struct linux_binprm *bprm) 22541da177e4SLinus Torvalds { 22551da177e4SLinus Torvalds struct task_security_struct *tsec; 22561da177e4SLinus Torvalds struct rlimit *rlim, *initrlim; 22571da177e4SLinus Torvalds struct itimerval itimer; 22581da177e4SLinus Torvalds struct bprm_security_struct *bsec; 22591da177e4SLinus Torvalds int rc, i; 22601da177e4SLinus Torvalds 22611da177e4SLinus Torvalds tsec = current->security; 22621da177e4SLinus Torvalds bsec = bprm->security; 22631da177e4SLinus Torvalds 22641da177e4SLinus Torvalds if (bsec->unsafe) { 22651da177e4SLinus Torvalds force_sig_specific(SIGKILL, current); 22661da177e4SLinus Torvalds return; 22671da177e4SLinus Torvalds } 22681da177e4SLinus Torvalds if (tsec->osid == tsec->sid) 22691da177e4SLinus Torvalds return; 22701da177e4SLinus Torvalds 22711da177e4SLinus Torvalds /* Close files for which the new task SID is not authorized. */ 22721da177e4SLinus Torvalds flush_unauthorized_files(current->files); 22731da177e4SLinus Torvalds 22741da177e4SLinus Torvalds /* Check whether the new SID can inherit signal state 22751da177e4SLinus Torvalds from the old SID. If not, clear itimers to avoid 22761da177e4SLinus Torvalds subsequent signal generation and flush and unblock 22771da177e4SLinus Torvalds signals. This must occur _after_ the task SID has 22781da177e4SLinus Torvalds been updated so that any kill done after the flush 22791da177e4SLinus Torvalds will be checked against the new SID. */ 22801da177e4SLinus Torvalds rc = avc_has_perm(tsec->osid, tsec->sid, SECCLASS_PROCESS, 22811da177e4SLinus Torvalds PROCESS__SIGINH, NULL); 22821da177e4SLinus Torvalds if (rc) { 22831da177e4SLinus Torvalds memset(&itimer, 0, sizeof itimer); 22841da177e4SLinus Torvalds for (i = 0; i < 3; i++) 22851da177e4SLinus Torvalds do_setitimer(i, &itimer, NULL); 22861da177e4SLinus Torvalds flush_signals(current); 22871da177e4SLinus Torvalds spin_lock_irq(¤t->sighand->siglock); 22881da177e4SLinus Torvalds flush_signal_handlers(current, 1); 22891da177e4SLinus Torvalds sigemptyset(¤t->blocked); 22901da177e4SLinus Torvalds recalc_sigpending(); 22911da177e4SLinus Torvalds spin_unlock_irq(¤t->sighand->siglock); 22921da177e4SLinus Torvalds } 22931da177e4SLinus Torvalds 22944ac212adSStephen Smalley /* Always clear parent death signal on SID transitions. */ 22954ac212adSStephen Smalley current->pdeath_signal = 0; 22964ac212adSStephen Smalley 22971da177e4SLinus Torvalds /* Check whether the new SID can inherit resource limits 22981da177e4SLinus Torvalds from the old SID. If not, reset all soft limits to 22991da177e4SLinus Torvalds the lower of the current task's hard limit and the init 23001da177e4SLinus Torvalds task's soft limit. Note that the setting of hard limits 23011da177e4SLinus Torvalds (even to lower them) can be controlled by the setrlimit 23021da177e4SLinus Torvalds check. The inclusion of the init task's soft limit into 23031da177e4SLinus Torvalds the computation is to avoid resetting soft limits higher 23041da177e4SLinus Torvalds than the default soft limit for cases where the default 23051da177e4SLinus Torvalds is lower than the hard limit, e.g. RLIMIT_CORE or 23061da177e4SLinus Torvalds RLIMIT_STACK.*/ 23071da177e4SLinus Torvalds rc = avc_has_perm(tsec->osid, tsec->sid, SECCLASS_PROCESS, 23081da177e4SLinus Torvalds PROCESS__RLIMITINH, NULL); 23091da177e4SLinus Torvalds if (rc) { 23101da177e4SLinus Torvalds for (i = 0; i < RLIM_NLIMITS; i++) { 23111da177e4SLinus Torvalds rlim = current->signal->rlim + i; 23121da177e4SLinus Torvalds initrlim = init_task.signal->rlim+i; 23131da177e4SLinus Torvalds rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur); 23141da177e4SLinus Torvalds } 23151da177e4SLinus Torvalds if (current->signal->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) { 23161da177e4SLinus Torvalds /* 23171da177e4SLinus Torvalds * This will cause RLIMIT_CPU calculations 23181da177e4SLinus Torvalds * to be refigured. 23191da177e4SLinus Torvalds */ 23201da177e4SLinus Torvalds current->it_prof_expires = jiffies_to_cputime(1); 23211da177e4SLinus Torvalds } 23221da177e4SLinus Torvalds } 23231da177e4SLinus Torvalds 23241da177e4SLinus Torvalds /* Wake up the parent if it is waiting so that it can 23251da177e4SLinus Torvalds recheck wait permission to the new task SID. */ 23261da177e4SLinus Torvalds wake_up_interruptible(¤t->parent->signal->wait_chldexit); 23271da177e4SLinus Torvalds } 23281da177e4SLinus Torvalds 23291da177e4SLinus Torvalds /* superblock security operations */ 23301da177e4SLinus Torvalds 23311da177e4SLinus Torvalds static int selinux_sb_alloc_security(struct super_block *sb) 23321da177e4SLinus Torvalds { 23331da177e4SLinus Torvalds return superblock_alloc_security(sb); 23341da177e4SLinus Torvalds } 23351da177e4SLinus Torvalds 23361da177e4SLinus Torvalds static void selinux_sb_free_security(struct super_block *sb) 23371da177e4SLinus Torvalds { 23381da177e4SLinus Torvalds superblock_free_security(sb); 23391da177e4SLinus Torvalds } 23401da177e4SLinus Torvalds 23411da177e4SLinus Torvalds static inline int match_prefix(char *prefix, int plen, char *option, int olen) 23421da177e4SLinus Torvalds { 23431da177e4SLinus Torvalds if (plen > olen) 23441da177e4SLinus Torvalds return 0; 23451da177e4SLinus Torvalds 23461da177e4SLinus Torvalds return !memcmp(prefix, option, plen); 23471da177e4SLinus Torvalds } 23481da177e4SLinus Torvalds 23491da177e4SLinus Torvalds static inline int selinux_option(char *option, int len) 23501da177e4SLinus Torvalds { 2351832cbd9aSEric Paris return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) || 2352832cbd9aSEric Paris match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) || 2353832cbd9aSEric Paris match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) || 2354832cbd9aSEric Paris match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len)); 23551da177e4SLinus Torvalds } 23561da177e4SLinus Torvalds 23571da177e4SLinus Torvalds static inline void take_option(char **to, char *from, int *first, int len) 23581da177e4SLinus Torvalds { 23591da177e4SLinus Torvalds if (!*first) { 23601da177e4SLinus Torvalds **to = ','; 23611da177e4SLinus Torvalds *to += 1; 23623528a953SCory Olmo } else 23631da177e4SLinus Torvalds *first = 0; 23641da177e4SLinus Torvalds memcpy(*to, from, len); 23651da177e4SLinus Torvalds *to += len; 23661da177e4SLinus Torvalds } 23671da177e4SLinus Torvalds 23683528a953SCory Olmo static inline void take_selinux_option(char **to, char *from, int *first, 23693528a953SCory Olmo int len) 23703528a953SCory Olmo { 23713528a953SCory Olmo int current_size = 0; 23723528a953SCory Olmo 23733528a953SCory Olmo if (!*first) { 23743528a953SCory Olmo **to = '|'; 23753528a953SCory Olmo *to += 1; 2376828dfe1dSEric Paris } else 23773528a953SCory Olmo *first = 0; 23783528a953SCory Olmo 23793528a953SCory Olmo while (current_size < len) { 23803528a953SCory Olmo if (*from != '"') { 23813528a953SCory Olmo **to = *from; 23823528a953SCory Olmo *to += 1; 23833528a953SCory Olmo } 23843528a953SCory Olmo from += 1; 23853528a953SCory Olmo current_size += 1; 23863528a953SCory Olmo } 23873528a953SCory Olmo } 23883528a953SCory Olmo 2389e0007529SEric Paris static int selinux_sb_copy_data(char *orig, char *copy) 23901da177e4SLinus Torvalds { 23911da177e4SLinus Torvalds int fnosec, fsec, rc = 0; 23921da177e4SLinus Torvalds char *in_save, *in_curr, *in_end; 23931da177e4SLinus Torvalds char *sec_curr, *nosec_save, *nosec; 23943528a953SCory Olmo int open_quote = 0; 23951da177e4SLinus Torvalds 23961da177e4SLinus Torvalds in_curr = orig; 23971da177e4SLinus Torvalds sec_curr = copy; 23981da177e4SLinus Torvalds 23991da177e4SLinus Torvalds nosec = (char *)get_zeroed_page(GFP_KERNEL); 24001da177e4SLinus Torvalds if (!nosec) { 24011da177e4SLinus Torvalds rc = -ENOMEM; 24021da177e4SLinus Torvalds goto out; 24031da177e4SLinus Torvalds } 24041da177e4SLinus Torvalds 24051da177e4SLinus Torvalds nosec_save = nosec; 24061da177e4SLinus Torvalds fnosec = fsec = 1; 24071da177e4SLinus Torvalds in_save = in_end = orig; 24081da177e4SLinus Torvalds 24091da177e4SLinus Torvalds do { 24103528a953SCory Olmo if (*in_end == '"') 24113528a953SCory Olmo open_quote = !open_quote; 24123528a953SCory Olmo if ((*in_end == ',' && open_quote == 0) || 24133528a953SCory Olmo *in_end == '\0') { 24141da177e4SLinus Torvalds int len = in_end - in_curr; 24151da177e4SLinus Torvalds 24161da177e4SLinus Torvalds if (selinux_option(in_curr, len)) 24173528a953SCory Olmo take_selinux_option(&sec_curr, in_curr, &fsec, len); 24181da177e4SLinus Torvalds else 24191da177e4SLinus Torvalds take_option(&nosec, in_curr, &fnosec, len); 24201da177e4SLinus Torvalds 24211da177e4SLinus Torvalds in_curr = in_end + 1; 24221da177e4SLinus Torvalds } 24231da177e4SLinus Torvalds } while (*in_end++); 24241da177e4SLinus Torvalds 24256931dfc9SEric Paris strcpy(in_save, nosec_save); 2426da3caa20SGerald Schaefer free_page((unsigned long)nosec_save); 24271da177e4SLinus Torvalds out: 24281da177e4SLinus Torvalds return rc; 24291da177e4SLinus Torvalds } 24301da177e4SLinus Torvalds 24311da177e4SLinus Torvalds static int selinux_sb_kern_mount(struct super_block *sb, void *data) 24321da177e4SLinus Torvalds { 24331da177e4SLinus Torvalds struct avc_audit_data ad; 24341da177e4SLinus Torvalds int rc; 24351da177e4SLinus Torvalds 24361da177e4SLinus Torvalds rc = superblock_doinit(sb, data); 24371da177e4SLinus Torvalds if (rc) 24381da177e4SLinus Torvalds return rc; 24391da177e4SLinus Torvalds 24401da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 244144707fdfSJan Blunck ad.u.fs.path.dentry = sb->s_root; 24421da177e4SLinus Torvalds return superblock_has_perm(current, sb, FILESYSTEM__MOUNT, &ad); 24431da177e4SLinus Torvalds } 24441da177e4SLinus Torvalds 2445726c3342SDavid Howells static int selinux_sb_statfs(struct dentry *dentry) 24461da177e4SLinus Torvalds { 24471da177e4SLinus Torvalds struct avc_audit_data ad; 24481da177e4SLinus Torvalds 24491da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 245044707fdfSJan Blunck ad.u.fs.path.dentry = dentry->d_sb->s_root; 2451726c3342SDavid Howells return superblock_has_perm(current, dentry->d_sb, FILESYSTEM__GETATTR, &ad); 24521da177e4SLinus Torvalds } 24531da177e4SLinus Torvalds 24541da177e4SLinus Torvalds static int selinux_mount(char *dev_name, 2455b5266eb4SAl Viro struct path *path, 24561da177e4SLinus Torvalds char *type, 24571da177e4SLinus Torvalds unsigned long flags, 24581da177e4SLinus Torvalds void *data) 24591da177e4SLinus Torvalds { 24601da177e4SLinus Torvalds int rc; 24611da177e4SLinus Torvalds 2462b5266eb4SAl Viro rc = secondary_ops->sb_mount(dev_name, path, type, flags, data); 24631da177e4SLinus Torvalds if (rc) 24641da177e4SLinus Torvalds return rc; 24651da177e4SLinus Torvalds 24661da177e4SLinus Torvalds if (flags & MS_REMOUNT) 2467b5266eb4SAl Viro return superblock_has_perm(current, path->mnt->mnt_sb, 24681da177e4SLinus Torvalds FILESYSTEM__REMOUNT, NULL); 24691da177e4SLinus Torvalds else 2470b5266eb4SAl Viro return dentry_has_perm(current, path->mnt, path->dentry, 24711da177e4SLinus Torvalds FILE__MOUNTON); 24721da177e4SLinus Torvalds } 24731da177e4SLinus Torvalds 24741da177e4SLinus Torvalds static int selinux_umount(struct vfsmount *mnt, int flags) 24751da177e4SLinus Torvalds { 24761da177e4SLinus Torvalds int rc; 24771da177e4SLinus Torvalds 24781da177e4SLinus Torvalds rc = secondary_ops->sb_umount(mnt, flags); 24791da177e4SLinus Torvalds if (rc) 24801da177e4SLinus Torvalds return rc; 24811da177e4SLinus Torvalds 24821da177e4SLinus Torvalds return superblock_has_perm(current, mnt->mnt_sb, 24831da177e4SLinus Torvalds FILESYSTEM__UNMOUNT, NULL); 24841da177e4SLinus Torvalds } 24851da177e4SLinus Torvalds 24861da177e4SLinus Torvalds /* inode security operations */ 24871da177e4SLinus Torvalds 24881da177e4SLinus Torvalds static int selinux_inode_alloc_security(struct inode *inode) 24891da177e4SLinus Torvalds { 24901da177e4SLinus Torvalds return inode_alloc_security(inode); 24911da177e4SLinus Torvalds } 24921da177e4SLinus Torvalds 24931da177e4SLinus Torvalds static void selinux_inode_free_security(struct inode *inode) 24941da177e4SLinus Torvalds { 24951da177e4SLinus Torvalds inode_free_security(inode); 24961da177e4SLinus Torvalds } 24971da177e4SLinus Torvalds 24985e41ff9eSStephen Smalley static int selinux_inode_init_security(struct inode *inode, struct inode *dir, 24995e41ff9eSStephen Smalley char **name, void **value, 25005e41ff9eSStephen Smalley size_t *len) 25015e41ff9eSStephen Smalley { 25025e41ff9eSStephen Smalley struct task_security_struct *tsec; 25035e41ff9eSStephen Smalley struct inode_security_struct *dsec; 25045e41ff9eSStephen Smalley struct superblock_security_struct *sbsec; 2505570bc1c2SStephen Smalley u32 newsid, clen; 25065e41ff9eSStephen Smalley int rc; 2507570bc1c2SStephen Smalley char *namep = NULL, *context; 25085e41ff9eSStephen Smalley 25095e41ff9eSStephen Smalley tsec = current->security; 25105e41ff9eSStephen Smalley dsec = dir->i_security; 25115e41ff9eSStephen Smalley sbsec = dir->i_sb->s_security; 25125e41ff9eSStephen Smalley 25135e41ff9eSStephen Smalley if (tsec->create_sid && sbsec->behavior != SECURITY_FS_USE_MNTPOINT) { 25145e41ff9eSStephen Smalley newsid = tsec->create_sid; 25155e41ff9eSStephen Smalley } else { 25165e41ff9eSStephen Smalley rc = security_transition_sid(tsec->sid, dsec->sid, 25175e41ff9eSStephen Smalley inode_mode_to_security_class(inode->i_mode), 25185e41ff9eSStephen Smalley &newsid); 25195e41ff9eSStephen Smalley if (rc) { 25205e41ff9eSStephen Smalley printk(KERN_WARNING "%s: " 25215e41ff9eSStephen Smalley "security_transition_sid failed, rc=%d (dev=%s " 25225e41ff9eSStephen Smalley "ino=%ld)\n", 2523dd6f953aSHarvey Harrison __func__, 25245e41ff9eSStephen Smalley -rc, inode->i_sb->s_id, inode->i_ino); 25255e41ff9eSStephen Smalley return rc; 25265e41ff9eSStephen Smalley } 25275e41ff9eSStephen Smalley } 25285e41ff9eSStephen Smalley 2529296fddf7SEric Paris /* Possibly defer initialization to selinux_complete_init. */ 2530296fddf7SEric Paris if (sbsec->initialized) { 2531296fddf7SEric Paris struct inode_security_struct *isec = inode->i_security; 2532296fddf7SEric Paris isec->sclass = inode_mode_to_security_class(inode->i_mode); 2533296fddf7SEric Paris isec->sid = newsid; 2534296fddf7SEric Paris isec->initialized = 1; 2535296fddf7SEric Paris } 25365e41ff9eSStephen Smalley 25378aad3875SStephen Smalley if (!ss_initialized || sbsec->behavior == SECURITY_FS_USE_MNTPOINT) 253825a74f3bSStephen Smalley return -EOPNOTSUPP; 253925a74f3bSStephen Smalley 2540570bc1c2SStephen Smalley if (name) { 2541a02fe132SJosef Bacik namep = kstrdup(XATTR_SELINUX_SUFFIX, GFP_NOFS); 25425e41ff9eSStephen Smalley if (!namep) 25435e41ff9eSStephen Smalley return -ENOMEM; 25445e41ff9eSStephen Smalley *name = namep; 2545570bc1c2SStephen Smalley } 25465e41ff9eSStephen Smalley 2547570bc1c2SStephen Smalley if (value && len) { 254812b29f34SStephen Smalley rc = security_sid_to_context_force(newsid, &context, &clen); 25495e41ff9eSStephen Smalley if (rc) { 25505e41ff9eSStephen Smalley kfree(namep); 25515e41ff9eSStephen Smalley return rc; 25525e41ff9eSStephen Smalley } 25535e41ff9eSStephen Smalley *value = context; 2554570bc1c2SStephen Smalley *len = clen; 2555570bc1c2SStephen Smalley } 25565e41ff9eSStephen Smalley 25575e41ff9eSStephen Smalley return 0; 25585e41ff9eSStephen Smalley } 25595e41ff9eSStephen Smalley 25601da177e4SLinus Torvalds static int selinux_inode_create(struct inode *dir, struct dentry *dentry, int mask) 25611da177e4SLinus Torvalds { 25621da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_FILE); 25631da177e4SLinus Torvalds } 25641da177e4SLinus Torvalds 25651da177e4SLinus Torvalds static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 25661da177e4SLinus Torvalds { 25671da177e4SLinus Torvalds int rc; 25681da177e4SLinus Torvalds 25691da177e4SLinus Torvalds rc = secondary_ops->inode_link(old_dentry, dir, new_dentry); 25701da177e4SLinus Torvalds if (rc) 25711da177e4SLinus Torvalds return rc; 25721da177e4SLinus Torvalds return may_link(dir, old_dentry, MAY_LINK); 25731da177e4SLinus Torvalds } 25741da177e4SLinus Torvalds 25751da177e4SLinus Torvalds static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry) 25761da177e4SLinus Torvalds { 25771da177e4SLinus Torvalds int rc; 25781da177e4SLinus Torvalds 25791da177e4SLinus Torvalds rc = secondary_ops->inode_unlink(dir, dentry); 25801da177e4SLinus Torvalds if (rc) 25811da177e4SLinus Torvalds return rc; 25821da177e4SLinus Torvalds return may_link(dir, dentry, MAY_UNLINK); 25831da177e4SLinus Torvalds } 25841da177e4SLinus Torvalds 25851da177e4SLinus Torvalds static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name) 25861da177e4SLinus Torvalds { 25871da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_LNK_FILE); 25881da177e4SLinus Torvalds } 25891da177e4SLinus Torvalds 25901da177e4SLinus Torvalds static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, int mask) 25911da177e4SLinus Torvalds { 25921da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_DIR); 25931da177e4SLinus Torvalds } 25941da177e4SLinus Torvalds 25951da177e4SLinus Torvalds static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry) 25961da177e4SLinus Torvalds { 25971da177e4SLinus Torvalds return may_link(dir, dentry, MAY_RMDIR); 25981da177e4SLinus Torvalds } 25991da177e4SLinus Torvalds 26001da177e4SLinus Torvalds static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 26011da177e4SLinus Torvalds { 26021da177e4SLinus Torvalds int rc; 26031da177e4SLinus Torvalds 26041da177e4SLinus Torvalds rc = secondary_ops->inode_mknod(dir, dentry, mode, dev); 26051da177e4SLinus Torvalds if (rc) 26061da177e4SLinus Torvalds return rc; 26071da177e4SLinus Torvalds 26081da177e4SLinus Torvalds return may_create(dir, dentry, inode_mode_to_security_class(mode)); 26091da177e4SLinus Torvalds } 26101da177e4SLinus Torvalds 26111da177e4SLinus Torvalds static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry, 26121da177e4SLinus Torvalds struct inode *new_inode, struct dentry *new_dentry) 26131da177e4SLinus Torvalds { 26141da177e4SLinus Torvalds return may_rename(old_inode, old_dentry, new_inode, new_dentry); 26151da177e4SLinus Torvalds } 26161da177e4SLinus Torvalds 26171da177e4SLinus Torvalds static int selinux_inode_readlink(struct dentry *dentry) 26181da177e4SLinus Torvalds { 26191da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__READ); 26201da177e4SLinus Torvalds } 26211da177e4SLinus Torvalds 26221da177e4SLinus Torvalds static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata) 26231da177e4SLinus Torvalds { 26241da177e4SLinus Torvalds int rc; 26251da177e4SLinus Torvalds 26261da177e4SLinus Torvalds rc = secondary_ops->inode_follow_link(dentry, nameidata); 26271da177e4SLinus Torvalds if (rc) 26281da177e4SLinus Torvalds return rc; 26291da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__READ); 26301da177e4SLinus Torvalds } 26311da177e4SLinus Torvalds 2632b77b0646SAl Viro static int selinux_inode_permission(struct inode *inode, int mask) 26331da177e4SLinus Torvalds { 26341da177e4SLinus Torvalds int rc; 26351da177e4SLinus Torvalds 2636b77b0646SAl Viro rc = secondary_ops->inode_permission(inode, mask); 26371da177e4SLinus Torvalds if (rc) 26381da177e4SLinus Torvalds return rc; 26391da177e4SLinus Torvalds 26401da177e4SLinus Torvalds if (!mask) { 26411da177e4SLinus Torvalds /* No permission to check. Existence test. */ 26421da177e4SLinus Torvalds return 0; 26431da177e4SLinus Torvalds } 26441da177e4SLinus Torvalds 26451da177e4SLinus Torvalds return inode_has_perm(current, inode, 2646b0c636b9SEric Paris open_file_mask_to_av(inode->i_mode, mask), NULL); 26471da177e4SLinus Torvalds } 26481da177e4SLinus Torvalds 26491da177e4SLinus Torvalds static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr) 26501da177e4SLinus Torvalds { 26511da177e4SLinus Torvalds int rc; 26521da177e4SLinus Torvalds 26531da177e4SLinus Torvalds rc = secondary_ops->inode_setattr(dentry, iattr); 26541da177e4SLinus Torvalds if (rc) 26551da177e4SLinus Torvalds return rc; 26561da177e4SLinus Torvalds 26571da177e4SLinus Torvalds if (iattr->ia_valid & ATTR_FORCE) 26581da177e4SLinus Torvalds return 0; 26591da177e4SLinus Torvalds 26601da177e4SLinus Torvalds if (iattr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | 26611da177e4SLinus Torvalds ATTR_ATIME_SET | ATTR_MTIME_SET)) 26621da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__SETATTR); 26631da177e4SLinus Torvalds 26641da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__WRITE); 26651da177e4SLinus Torvalds } 26661da177e4SLinus Torvalds 26671da177e4SLinus Torvalds static int selinux_inode_getattr(struct vfsmount *mnt, struct dentry *dentry) 26681da177e4SLinus Torvalds { 26691da177e4SLinus Torvalds return dentry_has_perm(current, mnt, dentry, FILE__GETATTR); 26701da177e4SLinus Torvalds } 26711da177e4SLinus Torvalds 26728f0cfa52SDavid Howells static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name) 2673b5376771SSerge E. Hallyn { 2674b5376771SSerge E. Hallyn if (!strncmp(name, XATTR_SECURITY_PREFIX, 2675b5376771SSerge E. Hallyn sizeof XATTR_SECURITY_PREFIX - 1)) { 2676b5376771SSerge E. Hallyn if (!strcmp(name, XATTR_NAME_CAPS)) { 2677b5376771SSerge E. Hallyn if (!capable(CAP_SETFCAP)) 2678b5376771SSerge E. Hallyn return -EPERM; 2679b5376771SSerge E. Hallyn } else if (!capable(CAP_SYS_ADMIN)) { 2680b5376771SSerge E. Hallyn /* A different attribute in the security namespace. 2681b5376771SSerge E. Hallyn Restrict to administrator. */ 2682b5376771SSerge E. Hallyn return -EPERM; 2683b5376771SSerge E. Hallyn } 2684b5376771SSerge E. Hallyn } 2685b5376771SSerge E. Hallyn 2686b5376771SSerge E. Hallyn /* Not an attribute we recognize, so just check the 2687b5376771SSerge E. Hallyn ordinary setattr permission. */ 2688b5376771SSerge E. Hallyn return dentry_has_perm(current, NULL, dentry, FILE__SETATTR); 2689b5376771SSerge E. Hallyn } 2690b5376771SSerge E. Hallyn 26918f0cfa52SDavid Howells static int selinux_inode_setxattr(struct dentry *dentry, const char *name, 26928f0cfa52SDavid Howells const void *value, size_t size, int flags) 26931da177e4SLinus Torvalds { 26941da177e4SLinus Torvalds struct task_security_struct *tsec = current->security; 26951da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 26961da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 26971da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 26981da177e4SLinus Torvalds struct avc_audit_data ad; 26991da177e4SLinus Torvalds u32 newsid; 27001da177e4SLinus Torvalds int rc = 0; 27011da177e4SLinus Torvalds 2702b5376771SSerge E. Hallyn if (strcmp(name, XATTR_NAME_SELINUX)) 2703b5376771SSerge E. Hallyn return selinux_inode_setotherxattr(dentry, name); 27041da177e4SLinus Torvalds 27051da177e4SLinus Torvalds sbsec = inode->i_sb->s_security; 27061da177e4SLinus Torvalds if (sbsec->behavior == SECURITY_FS_USE_MNTPOINT) 27071da177e4SLinus Torvalds return -EOPNOTSUPP; 27081da177e4SLinus Torvalds 27093bd858abSSatyam Sharma if (!is_owner_or_cap(inode)) 27101da177e4SLinus Torvalds return -EPERM; 27111da177e4SLinus Torvalds 27121da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 271344707fdfSJan Blunck ad.u.fs.path.dentry = dentry; 27141da177e4SLinus Torvalds 27151da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, isec->sclass, 27161da177e4SLinus Torvalds FILE__RELABELFROM, &ad); 27171da177e4SLinus Torvalds if (rc) 27181da177e4SLinus Torvalds return rc; 27191da177e4SLinus Torvalds 27201da177e4SLinus Torvalds rc = security_context_to_sid(value, size, &newsid); 272112b29f34SStephen Smalley if (rc == -EINVAL) { 272212b29f34SStephen Smalley if (!capable(CAP_MAC_ADMIN)) 272312b29f34SStephen Smalley return rc; 272412b29f34SStephen Smalley rc = security_context_to_sid_force(value, size, &newsid); 272512b29f34SStephen Smalley } 27261da177e4SLinus Torvalds if (rc) 27271da177e4SLinus Torvalds return rc; 27281da177e4SLinus Torvalds 27291da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, newsid, isec->sclass, 27301da177e4SLinus Torvalds FILE__RELABELTO, &ad); 27311da177e4SLinus Torvalds if (rc) 27321da177e4SLinus Torvalds return rc; 27331da177e4SLinus Torvalds 27341da177e4SLinus Torvalds rc = security_validate_transition(isec->sid, newsid, tsec->sid, 27351da177e4SLinus Torvalds isec->sclass); 27361da177e4SLinus Torvalds if (rc) 27371da177e4SLinus Torvalds return rc; 27381da177e4SLinus Torvalds 27391da177e4SLinus Torvalds return avc_has_perm(newsid, 27401da177e4SLinus Torvalds sbsec->sid, 27411da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 27421da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, 27431da177e4SLinus Torvalds &ad); 27441da177e4SLinus Torvalds } 27451da177e4SLinus Torvalds 27468f0cfa52SDavid Howells static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name, 27478f0cfa52SDavid Howells const void *value, size_t size, 27488f0cfa52SDavid Howells int flags) 27491da177e4SLinus Torvalds { 27501da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 27511da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 27521da177e4SLinus Torvalds u32 newsid; 27531da177e4SLinus Torvalds int rc; 27541da177e4SLinus Torvalds 27551da177e4SLinus Torvalds if (strcmp(name, XATTR_NAME_SELINUX)) { 27561da177e4SLinus Torvalds /* Not an attribute we recognize, so nothing to do. */ 27571da177e4SLinus Torvalds return; 27581da177e4SLinus Torvalds } 27591da177e4SLinus Torvalds 276012b29f34SStephen Smalley rc = security_context_to_sid_force(value, size, &newsid); 27611da177e4SLinus Torvalds if (rc) { 276212b29f34SStephen Smalley printk(KERN_ERR "SELinux: unable to map context to SID" 276312b29f34SStephen Smalley "for (%s, %lu), rc=%d\n", 276412b29f34SStephen Smalley inode->i_sb->s_id, inode->i_ino, -rc); 27651da177e4SLinus Torvalds return; 27661da177e4SLinus Torvalds } 27671da177e4SLinus Torvalds 27681da177e4SLinus Torvalds isec->sid = newsid; 27691da177e4SLinus Torvalds return; 27701da177e4SLinus Torvalds } 27711da177e4SLinus Torvalds 27728f0cfa52SDavid Howells static int selinux_inode_getxattr(struct dentry *dentry, const char *name) 27731da177e4SLinus Torvalds { 27741da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__GETATTR); 27751da177e4SLinus Torvalds } 27761da177e4SLinus Torvalds 27771da177e4SLinus Torvalds static int selinux_inode_listxattr(struct dentry *dentry) 27781da177e4SLinus Torvalds { 27791da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__GETATTR); 27801da177e4SLinus Torvalds } 27811da177e4SLinus Torvalds 27828f0cfa52SDavid Howells static int selinux_inode_removexattr(struct dentry *dentry, const char *name) 27831da177e4SLinus Torvalds { 2784b5376771SSerge E. Hallyn if (strcmp(name, XATTR_NAME_SELINUX)) 2785b5376771SSerge E. Hallyn return selinux_inode_setotherxattr(dentry, name); 27861da177e4SLinus Torvalds 27871da177e4SLinus Torvalds /* No one is allowed to remove a SELinux security label. 27881da177e4SLinus Torvalds You can change the label, but all data must be labeled. */ 27891da177e4SLinus Torvalds return -EACCES; 27901da177e4SLinus Torvalds } 27911da177e4SLinus Torvalds 2792d381d8a9SJames Morris /* 2793abc69bb6SStephen Smalley * Copy the inode security context value to the user. 2794d381d8a9SJames Morris * 2795d381d8a9SJames Morris * Permission check is handled by selinux_inode_getxattr hook. 2796d381d8a9SJames Morris */ 279742492594SDavid P. Quigley static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void **buffer, bool alloc) 27981da177e4SLinus Torvalds { 279942492594SDavid P. Quigley u32 size; 280042492594SDavid P. Quigley int error; 280142492594SDavid P. Quigley char *context = NULL; 2802abc69bb6SStephen Smalley struct task_security_struct *tsec = current->security; 28031da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 28041da177e4SLinus Torvalds 28058c8570fbSDustin Kirkland if (strcmp(name, XATTR_SELINUX_SUFFIX)) 28068c8570fbSDustin Kirkland return -EOPNOTSUPP; 28071da177e4SLinus Torvalds 2808abc69bb6SStephen Smalley /* 2809abc69bb6SStephen Smalley * If the caller has CAP_MAC_ADMIN, then get the raw context 2810abc69bb6SStephen Smalley * value even if it is not defined by current policy; otherwise, 2811abc69bb6SStephen Smalley * use the in-core value under current policy. 2812abc69bb6SStephen Smalley * Use the non-auditing forms of the permission checks since 2813abc69bb6SStephen Smalley * getxattr may be called by unprivileged processes commonly 2814abc69bb6SStephen Smalley * and lack of permission just means that we fall back to the 2815abc69bb6SStephen Smalley * in-core context value, not a denial. 2816abc69bb6SStephen Smalley */ 2817abc69bb6SStephen Smalley error = secondary_ops->capable(current, CAP_MAC_ADMIN); 2818abc69bb6SStephen Smalley if (!error) 2819abc69bb6SStephen Smalley error = avc_has_perm_noaudit(tsec->sid, tsec->sid, 2820abc69bb6SStephen Smalley SECCLASS_CAPABILITY2, 2821abc69bb6SStephen Smalley CAPABILITY2__MAC_ADMIN, 2822abc69bb6SStephen Smalley 0, 2823abc69bb6SStephen Smalley NULL); 2824abc69bb6SStephen Smalley if (!error) 2825abc69bb6SStephen Smalley error = security_sid_to_context_force(isec->sid, &context, 2826abc69bb6SStephen Smalley &size); 2827abc69bb6SStephen Smalley else 282842492594SDavid P. Quigley error = security_sid_to_context(isec->sid, &context, &size); 282942492594SDavid P. Quigley if (error) 283042492594SDavid P. Quigley return error; 283142492594SDavid P. Quigley error = size; 283242492594SDavid P. Quigley if (alloc) { 283342492594SDavid P. Quigley *buffer = context; 283442492594SDavid P. Quigley goto out_nofree; 283542492594SDavid P. Quigley } 283642492594SDavid P. Quigley kfree(context); 283742492594SDavid P. Quigley out_nofree: 283842492594SDavid P. Quigley return error; 28391da177e4SLinus Torvalds } 28401da177e4SLinus Torvalds 28411da177e4SLinus Torvalds static int selinux_inode_setsecurity(struct inode *inode, const char *name, 28421da177e4SLinus Torvalds const void *value, size_t size, int flags) 28431da177e4SLinus Torvalds { 28441da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 28451da177e4SLinus Torvalds u32 newsid; 28461da177e4SLinus Torvalds int rc; 28471da177e4SLinus Torvalds 28481da177e4SLinus Torvalds if (strcmp(name, XATTR_SELINUX_SUFFIX)) 28491da177e4SLinus Torvalds return -EOPNOTSUPP; 28501da177e4SLinus Torvalds 28511da177e4SLinus Torvalds if (!value || !size) 28521da177e4SLinus Torvalds return -EACCES; 28531da177e4SLinus Torvalds 28541da177e4SLinus Torvalds rc = security_context_to_sid((void *)value, size, &newsid); 28551da177e4SLinus Torvalds if (rc) 28561da177e4SLinus Torvalds return rc; 28571da177e4SLinus Torvalds 28581da177e4SLinus Torvalds isec->sid = newsid; 28591da177e4SLinus Torvalds return 0; 28601da177e4SLinus Torvalds } 28611da177e4SLinus Torvalds 28621da177e4SLinus Torvalds static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size) 28631da177e4SLinus Torvalds { 28641da177e4SLinus Torvalds const int len = sizeof(XATTR_NAME_SELINUX); 28651da177e4SLinus Torvalds if (buffer && len <= buffer_size) 28661da177e4SLinus Torvalds memcpy(buffer, XATTR_NAME_SELINUX, len); 28671da177e4SLinus Torvalds return len; 28681da177e4SLinus Torvalds } 28691da177e4SLinus Torvalds 2870b5376771SSerge E. Hallyn static int selinux_inode_need_killpriv(struct dentry *dentry) 2871b5376771SSerge E. Hallyn { 2872b5376771SSerge E. Hallyn return secondary_ops->inode_need_killpriv(dentry); 2873b5376771SSerge E. Hallyn } 2874b5376771SSerge E. Hallyn 2875b5376771SSerge E. Hallyn static int selinux_inode_killpriv(struct dentry *dentry) 2876b5376771SSerge E. Hallyn { 2877b5376771SSerge E. Hallyn return secondary_ops->inode_killpriv(dentry); 2878b5376771SSerge E. Hallyn } 2879b5376771SSerge E. Hallyn 2880713a04aeSAhmed S. Darwish static void selinux_inode_getsecid(const struct inode *inode, u32 *secid) 2881713a04aeSAhmed S. Darwish { 2882713a04aeSAhmed S. Darwish struct inode_security_struct *isec = inode->i_security; 2883713a04aeSAhmed S. Darwish *secid = isec->sid; 2884713a04aeSAhmed S. Darwish } 2885713a04aeSAhmed S. Darwish 28861da177e4SLinus Torvalds /* file security operations */ 28871da177e4SLinus Torvalds 2888788e7dd4SYuichi Nakamura static int selinux_revalidate_file_permission(struct file *file, int mask) 28891da177e4SLinus Torvalds { 28907420ed23SVenkat Yekkirala int rc; 28913d5ff529SJosef Sipek struct inode *inode = file->f_path.dentry->d_inode; 28921da177e4SLinus Torvalds 28931da177e4SLinus Torvalds if (!mask) { 28941da177e4SLinus Torvalds /* No permission to check. Existence test. */ 28951da177e4SLinus Torvalds return 0; 28961da177e4SLinus Torvalds } 28971da177e4SLinus Torvalds 28981da177e4SLinus Torvalds /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */ 28991da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE)) 29001da177e4SLinus Torvalds mask |= MAY_APPEND; 29011da177e4SLinus Torvalds 29027420ed23SVenkat Yekkirala rc = file_has_perm(current, file, 29031da177e4SLinus Torvalds file_mask_to_av(inode->i_mode, mask)); 29047420ed23SVenkat Yekkirala if (rc) 29057420ed23SVenkat Yekkirala return rc; 29067420ed23SVenkat Yekkirala 29077420ed23SVenkat Yekkirala return selinux_netlbl_inode_permission(inode, mask); 29081da177e4SLinus Torvalds } 29091da177e4SLinus Torvalds 2910788e7dd4SYuichi Nakamura static int selinux_file_permission(struct file *file, int mask) 2911788e7dd4SYuichi Nakamura { 2912788e7dd4SYuichi Nakamura struct inode *inode = file->f_path.dentry->d_inode; 2913788e7dd4SYuichi Nakamura struct task_security_struct *tsec = current->security; 2914788e7dd4SYuichi Nakamura struct file_security_struct *fsec = file->f_security; 2915788e7dd4SYuichi Nakamura struct inode_security_struct *isec = inode->i_security; 2916788e7dd4SYuichi Nakamura 2917788e7dd4SYuichi Nakamura if (!mask) { 2918788e7dd4SYuichi Nakamura /* No permission to check. Existence test. */ 2919788e7dd4SYuichi Nakamura return 0; 2920788e7dd4SYuichi Nakamura } 2921788e7dd4SYuichi Nakamura 2922788e7dd4SYuichi Nakamura if (tsec->sid == fsec->sid && fsec->isid == isec->sid 2923788e7dd4SYuichi Nakamura && fsec->pseqno == avc_policy_seqno()) 2924788e7dd4SYuichi Nakamura return selinux_netlbl_inode_permission(inode, mask); 2925788e7dd4SYuichi Nakamura 2926788e7dd4SYuichi Nakamura return selinux_revalidate_file_permission(file, mask); 2927788e7dd4SYuichi Nakamura } 2928788e7dd4SYuichi Nakamura 29291da177e4SLinus Torvalds static int selinux_file_alloc_security(struct file *file) 29301da177e4SLinus Torvalds { 29311da177e4SLinus Torvalds return file_alloc_security(file); 29321da177e4SLinus Torvalds } 29331da177e4SLinus Torvalds 29341da177e4SLinus Torvalds static void selinux_file_free_security(struct file *file) 29351da177e4SLinus Torvalds { 29361da177e4SLinus Torvalds file_free_security(file); 29371da177e4SLinus Torvalds } 29381da177e4SLinus Torvalds 29391da177e4SLinus Torvalds static int selinux_file_ioctl(struct file *file, unsigned int cmd, 29401da177e4SLinus Torvalds unsigned long arg) 29411da177e4SLinus Torvalds { 2942242631c4SStephen Smalley u32 av = 0; 29431da177e4SLinus Torvalds 2944242631c4SStephen Smalley if (_IOC_DIR(cmd) & _IOC_WRITE) 2945242631c4SStephen Smalley av |= FILE__WRITE; 2946242631c4SStephen Smalley if (_IOC_DIR(cmd) & _IOC_READ) 2947242631c4SStephen Smalley av |= FILE__READ; 2948242631c4SStephen Smalley if (!av) 2949242631c4SStephen Smalley av = FILE__IOCTL; 29501da177e4SLinus Torvalds 2951242631c4SStephen Smalley return file_has_perm(current, file, av); 29521da177e4SLinus Torvalds } 29531da177e4SLinus Torvalds 29541da177e4SLinus Torvalds static int file_map_prot_check(struct file *file, unsigned long prot, int shared) 29551da177e4SLinus Torvalds { 29561da177e4SLinus Torvalds #ifndef CONFIG_PPC32 29571da177e4SLinus Torvalds if ((prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) { 29581da177e4SLinus Torvalds /* 29591da177e4SLinus Torvalds * We are making executable an anonymous mapping or a 29601da177e4SLinus Torvalds * private file mapping that will also be writable. 29611da177e4SLinus Torvalds * This has an additional check. 29621da177e4SLinus Torvalds */ 29631da177e4SLinus Torvalds int rc = task_has_perm(current, current, PROCESS__EXECMEM); 29641da177e4SLinus Torvalds if (rc) 29651da177e4SLinus Torvalds return rc; 29661da177e4SLinus Torvalds } 29671da177e4SLinus Torvalds #endif 29681da177e4SLinus Torvalds 29691da177e4SLinus Torvalds if (file) { 29701da177e4SLinus Torvalds /* read access is always possible with a mapping */ 29711da177e4SLinus Torvalds u32 av = FILE__READ; 29721da177e4SLinus Torvalds 29731da177e4SLinus Torvalds /* write access only matters if the mapping is shared */ 29741da177e4SLinus Torvalds if (shared && (prot & PROT_WRITE)) 29751da177e4SLinus Torvalds av |= FILE__WRITE; 29761da177e4SLinus Torvalds 29771da177e4SLinus Torvalds if (prot & PROT_EXEC) 29781da177e4SLinus Torvalds av |= FILE__EXECUTE; 29791da177e4SLinus Torvalds 29801da177e4SLinus Torvalds return file_has_perm(current, file, av); 29811da177e4SLinus Torvalds } 29821da177e4SLinus Torvalds return 0; 29831da177e4SLinus Torvalds } 29841da177e4SLinus Torvalds 29851da177e4SLinus Torvalds static int selinux_file_mmap(struct file *file, unsigned long reqprot, 2986ed032189SEric Paris unsigned long prot, unsigned long flags, 2987ed032189SEric Paris unsigned long addr, unsigned long addr_only) 29881da177e4SLinus Torvalds { 2989ed032189SEric Paris int rc = 0; 2990ed032189SEric Paris u32 sid = ((struct task_security_struct *)(current->security))->sid; 29911da177e4SLinus Torvalds 2992ed032189SEric Paris if (addr < mmap_min_addr) 2993ed032189SEric Paris rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT, 2994ed032189SEric Paris MEMPROTECT__MMAP_ZERO, NULL); 2995ed032189SEric Paris if (rc || addr_only) 29961da177e4SLinus Torvalds return rc; 29971da177e4SLinus Torvalds 29981da177e4SLinus Torvalds if (selinux_checkreqprot) 29991da177e4SLinus Torvalds prot = reqprot; 30001da177e4SLinus Torvalds 30011da177e4SLinus Torvalds return file_map_prot_check(file, prot, 30021da177e4SLinus Torvalds (flags & MAP_TYPE) == MAP_SHARED); 30031da177e4SLinus Torvalds } 30041da177e4SLinus Torvalds 30051da177e4SLinus Torvalds static int selinux_file_mprotect(struct vm_area_struct *vma, 30061da177e4SLinus Torvalds unsigned long reqprot, 30071da177e4SLinus Torvalds unsigned long prot) 30081da177e4SLinus Torvalds { 30091da177e4SLinus Torvalds int rc; 30101da177e4SLinus Torvalds 30111da177e4SLinus Torvalds rc = secondary_ops->file_mprotect(vma, reqprot, prot); 30121da177e4SLinus Torvalds if (rc) 30131da177e4SLinus Torvalds return rc; 30141da177e4SLinus Torvalds 30151da177e4SLinus Torvalds if (selinux_checkreqprot) 30161da177e4SLinus Torvalds prot = reqprot; 30171da177e4SLinus Torvalds 30181da177e4SLinus Torvalds #ifndef CONFIG_PPC32 3019db4c9641SStephen Smalley if ((prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) { 3020db4c9641SStephen Smalley rc = 0; 3021db4c9641SStephen Smalley if (vma->vm_start >= vma->vm_mm->start_brk && 3022db4c9641SStephen Smalley vma->vm_end <= vma->vm_mm->brk) { 3023db4c9641SStephen Smalley rc = task_has_perm(current, current, 3024db4c9641SStephen Smalley PROCESS__EXECHEAP); 3025db4c9641SStephen Smalley } else if (!vma->vm_file && 30266b992197SLorenzo Hernandez García-Hierro vma->vm_start <= vma->vm_mm->start_stack && 30276b992197SLorenzo Hernandez García-Hierro vma->vm_end >= vma->vm_mm->start_stack) { 30286b992197SLorenzo Hernandez García-Hierro rc = task_has_perm(current, current, PROCESS__EXECSTACK); 3029db4c9641SStephen Smalley } else if (vma->vm_file && vma->anon_vma) { 3030db4c9641SStephen Smalley /* 3031db4c9641SStephen Smalley * We are making executable a file mapping that has 3032db4c9641SStephen Smalley * had some COW done. Since pages might have been 3033db4c9641SStephen Smalley * written, check ability to execute the possibly 3034db4c9641SStephen Smalley * modified content. This typically should only 3035db4c9641SStephen Smalley * occur for text relocations. 3036db4c9641SStephen Smalley */ 3037db4c9641SStephen Smalley rc = file_has_perm(current, vma->vm_file, 3038db4c9641SStephen Smalley FILE__EXECMOD); 3039db4c9641SStephen Smalley } 30406b992197SLorenzo Hernandez García-Hierro if (rc) 30416b992197SLorenzo Hernandez García-Hierro return rc; 30426b992197SLorenzo Hernandez García-Hierro } 30431da177e4SLinus Torvalds #endif 30441da177e4SLinus Torvalds 30451da177e4SLinus Torvalds return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED); 30461da177e4SLinus Torvalds } 30471da177e4SLinus Torvalds 30481da177e4SLinus Torvalds static int selinux_file_lock(struct file *file, unsigned int cmd) 30491da177e4SLinus Torvalds { 30501da177e4SLinus Torvalds return file_has_perm(current, file, FILE__LOCK); 30511da177e4SLinus Torvalds } 30521da177e4SLinus Torvalds 30531da177e4SLinus Torvalds static int selinux_file_fcntl(struct file *file, unsigned int cmd, 30541da177e4SLinus Torvalds unsigned long arg) 30551da177e4SLinus Torvalds { 30561da177e4SLinus Torvalds int err = 0; 30571da177e4SLinus Torvalds 30581da177e4SLinus Torvalds switch (cmd) { 30591da177e4SLinus Torvalds case F_SETFL: 30603d5ff529SJosef Sipek if (!file->f_path.dentry || !file->f_path.dentry->d_inode) { 30611da177e4SLinus Torvalds err = -EINVAL; 30621da177e4SLinus Torvalds break; 30631da177e4SLinus Torvalds } 30641da177e4SLinus Torvalds 30651da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) { 30661da177e4SLinus Torvalds err = file_has_perm(current, file, FILE__WRITE); 30671da177e4SLinus Torvalds break; 30681da177e4SLinus Torvalds } 30691da177e4SLinus Torvalds /* fall through */ 30701da177e4SLinus Torvalds case F_SETOWN: 30711da177e4SLinus Torvalds case F_SETSIG: 30721da177e4SLinus Torvalds case F_GETFL: 30731da177e4SLinus Torvalds case F_GETOWN: 30741da177e4SLinus Torvalds case F_GETSIG: 30751da177e4SLinus Torvalds /* Just check FD__USE permission */ 30761da177e4SLinus Torvalds err = file_has_perm(current, file, 0); 30771da177e4SLinus Torvalds break; 30781da177e4SLinus Torvalds case F_GETLK: 30791da177e4SLinus Torvalds case F_SETLK: 30801da177e4SLinus Torvalds case F_SETLKW: 30811da177e4SLinus Torvalds #if BITS_PER_LONG == 32 30821da177e4SLinus Torvalds case F_GETLK64: 30831da177e4SLinus Torvalds case F_SETLK64: 30841da177e4SLinus Torvalds case F_SETLKW64: 30851da177e4SLinus Torvalds #endif 30863d5ff529SJosef Sipek if (!file->f_path.dentry || !file->f_path.dentry->d_inode) { 30871da177e4SLinus Torvalds err = -EINVAL; 30881da177e4SLinus Torvalds break; 30891da177e4SLinus Torvalds } 30901da177e4SLinus Torvalds err = file_has_perm(current, file, FILE__LOCK); 30911da177e4SLinus Torvalds break; 30921da177e4SLinus Torvalds } 30931da177e4SLinus Torvalds 30941da177e4SLinus Torvalds return err; 30951da177e4SLinus Torvalds } 30961da177e4SLinus Torvalds 30971da177e4SLinus Torvalds static int selinux_file_set_fowner(struct file *file) 30981da177e4SLinus Torvalds { 30991da177e4SLinus Torvalds struct task_security_struct *tsec; 31001da177e4SLinus Torvalds struct file_security_struct *fsec; 31011da177e4SLinus Torvalds 31021da177e4SLinus Torvalds tsec = current->security; 31031da177e4SLinus Torvalds fsec = file->f_security; 31041da177e4SLinus Torvalds fsec->fown_sid = tsec->sid; 31051da177e4SLinus Torvalds 31061da177e4SLinus Torvalds return 0; 31071da177e4SLinus Torvalds } 31081da177e4SLinus Torvalds 31091da177e4SLinus Torvalds static int selinux_file_send_sigiotask(struct task_struct *tsk, 31101da177e4SLinus Torvalds struct fown_struct *fown, int signum) 31111da177e4SLinus Torvalds { 31121da177e4SLinus Torvalds struct file *file; 31131da177e4SLinus Torvalds u32 perm; 31141da177e4SLinus Torvalds struct task_security_struct *tsec; 31151da177e4SLinus Torvalds struct file_security_struct *fsec; 31161da177e4SLinus Torvalds 31171da177e4SLinus Torvalds /* struct fown_struct is never outside the context of a struct file */ 3118b385a144SRobert P. J. Day file = container_of(fown, struct file, f_owner); 31191da177e4SLinus Torvalds 31201da177e4SLinus Torvalds tsec = tsk->security; 31211da177e4SLinus Torvalds fsec = file->f_security; 31221da177e4SLinus Torvalds 31231da177e4SLinus Torvalds if (!signum) 31241da177e4SLinus Torvalds perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */ 31251da177e4SLinus Torvalds else 31261da177e4SLinus Torvalds perm = signal_to_av(signum); 31271da177e4SLinus Torvalds 31281da177e4SLinus Torvalds return avc_has_perm(fsec->fown_sid, tsec->sid, 31291da177e4SLinus Torvalds SECCLASS_PROCESS, perm, NULL); 31301da177e4SLinus Torvalds } 31311da177e4SLinus Torvalds 31321da177e4SLinus Torvalds static int selinux_file_receive(struct file *file) 31331da177e4SLinus Torvalds { 31341da177e4SLinus Torvalds return file_has_perm(current, file, file_to_av(file)); 31351da177e4SLinus Torvalds } 31361da177e4SLinus Torvalds 3137788e7dd4SYuichi Nakamura static int selinux_dentry_open(struct file *file) 3138788e7dd4SYuichi Nakamura { 3139788e7dd4SYuichi Nakamura struct file_security_struct *fsec; 3140788e7dd4SYuichi Nakamura struct inode *inode; 3141788e7dd4SYuichi Nakamura struct inode_security_struct *isec; 3142788e7dd4SYuichi Nakamura inode = file->f_path.dentry->d_inode; 3143788e7dd4SYuichi Nakamura fsec = file->f_security; 3144788e7dd4SYuichi Nakamura isec = inode->i_security; 3145788e7dd4SYuichi Nakamura /* 3146788e7dd4SYuichi Nakamura * Save inode label and policy sequence number 3147788e7dd4SYuichi Nakamura * at open-time so that selinux_file_permission 3148788e7dd4SYuichi Nakamura * can determine whether revalidation is necessary. 3149788e7dd4SYuichi Nakamura * Task label is already saved in the file security 3150788e7dd4SYuichi Nakamura * struct as its SID. 3151788e7dd4SYuichi Nakamura */ 3152788e7dd4SYuichi Nakamura fsec->isid = isec->sid; 3153788e7dd4SYuichi Nakamura fsec->pseqno = avc_policy_seqno(); 3154788e7dd4SYuichi Nakamura /* 3155788e7dd4SYuichi Nakamura * Since the inode label or policy seqno may have changed 3156788e7dd4SYuichi Nakamura * between the selinux_inode_permission check and the saving 3157788e7dd4SYuichi Nakamura * of state above, recheck that access is still permitted. 3158788e7dd4SYuichi Nakamura * Otherwise, access might never be revalidated against the 3159788e7dd4SYuichi Nakamura * new inode label or new policy. 3160788e7dd4SYuichi Nakamura * This check is not redundant - do not remove. 3161788e7dd4SYuichi Nakamura */ 3162788e7dd4SYuichi Nakamura return inode_has_perm(current, inode, file_to_av(file), NULL); 3163788e7dd4SYuichi Nakamura } 3164788e7dd4SYuichi Nakamura 31651da177e4SLinus Torvalds /* task security operations */ 31661da177e4SLinus Torvalds 31671da177e4SLinus Torvalds static int selinux_task_create(unsigned long clone_flags) 31681da177e4SLinus Torvalds { 31691da177e4SLinus Torvalds int rc; 31701da177e4SLinus Torvalds 31711da177e4SLinus Torvalds rc = secondary_ops->task_create(clone_flags); 31721da177e4SLinus Torvalds if (rc) 31731da177e4SLinus Torvalds return rc; 31741da177e4SLinus Torvalds 31751da177e4SLinus Torvalds return task_has_perm(current, current, PROCESS__FORK); 31761da177e4SLinus Torvalds } 31771da177e4SLinus Torvalds 31781da177e4SLinus Torvalds static int selinux_task_alloc_security(struct task_struct *tsk) 31791da177e4SLinus Torvalds { 31801da177e4SLinus Torvalds struct task_security_struct *tsec1, *tsec2; 31811da177e4SLinus Torvalds int rc; 31821da177e4SLinus Torvalds 31831da177e4SLinus Torvalds tsec1 = current->security; 31841da177e4SLinus Torvalds 31851da177e4SLinus Torvalds rc = task_alloc_security(tsk); 31861da177e4SLinus Torvalds if (rc) 31871da177e4SLinus Torvalds return rc; 31881da177e4SLinus Torvalds tsec2 = tsk->security; 31891da177e4SLinus Torvalds 31901da177e4SLinus Torvalds tsec2->osid = tsec1->osid; 31911da177e4SLinus Torvalds tsec2->sid = tsec1->sid; 31921da177e4SLinus Torvalds 319328eba5bfSMichael LeMay /* Retain the exec, fs, key, and sock SIDs across fork */ 31941da177e4SLinus Torvalds tsec2->exec_sid = tsec1->exec_sid; 31951da177e4SLinus Torvalds tsec2->create_sid = tsec1->create_sid; 319628eba5bfSMichael LeMay tsec2->keycreate_sid = tsec1->keycreate_sid; 319742c3e03eSEric Paris tsec2->sockcreate_sid = tsec1->sockcreate_sid; 31981da177e4SLinus Torvalds 31991da177e4SLinus Torvalds return 0; 32001da177e4SLinus Torvalds } 32011da177e4SLinus Torvalds 32021da177e4SLinus Torvalds static void selinux_task_free_security(struct task_struct *tsk) 32031da177e4SLinus Torvalds { 32041da177e4SLinus Torvalds task_free_security(tsk); 32051da177e4SLinus Torvalds } 32061da177e4SLinus Torvalds 32071da177e4SLinus Torvalds static int selinux_task_setuid(uid_t id0, uid_t id1, uid_t id2, int flags) 32081da177e4SLinus Torvalds { 32091da177e4SLinus Torvalds /* Since setuid only affects the current process, and 32101da177e4SLinus Torvalds since the SELinux controls are not based on the Linux 32111da177e4SLinus Torvalds identity attributes, SELinux does not need to control 32121da177e4SLinus Torvalds this operation. However, SELinux does control the use 32131da177e4SLinus Torvalds of the CAP_SETUID and CAP_SETGID capabilities using the 32141da177e4SLinus Torvalds capable hook. */ 32151da177e4SLinus Torvalds return 0; 32161da177e4SLinus Torvalds } 32171da177e4SLinus Torvalds 32181da177e4SLinus Torvalds static int selinux_task_post_setuid(uid_t id0, uid_t id1, uid_t id2, int flags) 32191da177e4SLinus Torvalds { 32201da177e4SLinus Torvalds return secondary_ops->task_post_setuid(id0, id1, id2, flags); 32211da177e4SLinus Torvalds } 32221da177e4SLinus Torvalds 32231da177e4SLinus Torvalds static int selinux_task_setgid(gid_t id0, gid_t id1, gid_t id2, int flags) 32241da177e4SLinus Torvalds { 32251da177e4SLinus Torvalds /* See the comment for setuid above. */ 32261da177e4SLinus Torvalds return 0; 32271da177e4SLinus Torvalds } 32281da177e4SLinus Torvalds 32291da177e4SLinus Torvalds static int selinux_task_setpgid(struct task_struct *p, pid_t pgid) 32301da177e4SLinus Torvalds { 32311da177e4SLinus Torvalds return task_has_perm(current, p, PROCESS__SETPGID); 32321da177e4SLinus Torvalds } 32331da177e4SLinus Torvalds 32341da177e4SLinus Torvalds static int selinux_task_getpgid(struct task_struct *p) 32351da177e4SLinus Torvalds { 32361da177e4SLinus Torvalds return task_has_perm(current, p, PROCESS__GETPGID); 32371da177e4SLinus Torvalds } 32381da177e4SLinus Torvalds 32391da177e4SLinus Torvalds static int selinux_task_getsid(struct task_struct *p) 32401da177e4SLinus Torvalds { 32411da177e4SLinus Torvalds return task_has_perm(current, p, PROCESS__GETSESSION); 32421da177e4SLinus Torvalds } 32431da177e4SLinus Torvalds 3244f9008e4cSDavid Quigley static void selinux_task_getsecid(struct task_struct *p, u32 *secid) 3245f9008e4cSDavid Quigley { 3246713a04aeSAhmed S. Darwish struct task_security_struct *tsec = p->security; 3247713a04aeSAhmed S. Darwish *secid = tsec->sid; 3248f9008e4cSDavid Quigley } 3249f9008e4cSDavid Quigley 32501da177e4SLinus Torvalds static int selinux_task_setgroups(struct group_info *group_info) 32511da177e4SLinus Torvalds { 32521da177e4SLinus Torvalds /* See the comment for setuid above. */ 32531da177e4SLinus Torvalds return 0; 32541da177e4SLinus Torvalds } 32551da177e4SLinus Torvalds 32561da177e4SLinus Torvalds static int selinux_task_setnice(struct task_struct *p, int nice) 32571da177e4SLinus Torvalds { 32581da177e4SLinus Torvalds int rc; 32591da177e4SLinus Torvalds 32601da177e4SLinus Torvalds rc = secondary_ops->task_setnice(p, nice); 32611da177e4SLinus Torvalds if (rc) 32621da177e4SLinus Torvalds return rc; 32631da177e4SLinus Torvalds 32641da177e4SLinus Torvalds return task_has_perm(current, p, PROCESS__SETSCHED); 32651da177e4SLinus Torvalds } 32661da177e4SLinus Torvalds 326703e68060SJames Morris static int selinux_task_setioprio(struct task_struct *p, int ioprio) 326803e68060SJames Morris { 3269b5376771SSerge E. Hallyn int rc; 3270b5376771SSerge E. Hallyn 3271b5376771SSerge E. Hallyn rc = secondary_ops->task_setioprio(p, ioprio); 3272b5376771SSerge E. Hallyn if (rc) 3273b5376771SSerge E. Hallyn return rc; 3274b5376771SSerge E. Hallyn 327503e68060SJames Morris return task_has_perm(current, p, PROCESS__SETSCHED); 327603e68060SJames Morris } 327703e68060SJames Morris 3278a1836a42SDavid Quigley static int selinux_task_getioprio(struct task_struct *p) 3279a1836a42SDavid Quigley { 3280a1836a42SDavid Quigley return task_has_perm(current, p, PROCESS__GETSCHED); 3281a1836a42SDavid Quigley } 3282a1836a42SDavid Quigley 32831da177e4SLinus Torvalds static int selinux_task_setrlimit(unsigned int resource, struct rlimit *new_rlim) 32841da177e4SLinus Torvalds { 32851da177e4SLinus Torvalds struct rlimit *old_rlim = current->signal->rlim + resource; 32861da177e4SLinus Torvalds int rc; 32871da177e4SLinus Torvalds 32881da177e4SLinus Torvalds rc = secondary_ops->task_setrlimit(resource, new_rlim); 32891da177e4SLinus Torvalds if (rc) 32901da177e4SLinus Torvalds return rc; 32911da177e4SLinus Torvalds 32921da177e4SLinus Torvalds /* Control the ability to change the hard limit (whether 32931da177e4SLinus Torvalds lowering or raising it), so that the hard limit can 32941da177e4SLinus Torvalds later be used as a safe reset point for the soft limit 32951da177e4SLinus Torvalds upon context transitions. See selinux_bprm_apply_creds. */ 32961da177e4SLinus Torvalds if (old_rlim->rlim_max != new_rlim->rlim_max) 32971da177e4SLinus Torvalds return task_has_perm(current, current, PROCESS__SETRLIMIT); 32981da177e4SLinus Torvalds 32991da177e4SLinus Torvalds return 0; 33001da177e4SLinus Torvalds } 33011da177e4SLinus Torvalds 33021da177e4SLinus Torvalds static int selinux_task_setscheduler(struct task_struct *p, int policy, struct sched_param *lp) 33031da177e4SLinus Torvalds { 3304b5376771SSerge E. Hallyn int rc; 3305b5376771SSerge E. Hallyn 3306b5376771SSerge E. Hallyn rc = secondary_ops->task_setscheduler(p, policy, lp); 3307b5376771SSerge E. Hallyn if (rc) 3308b5376771SSerge E. Hallyn return rc; 3309b5376771SSerge E. Hallyn 33101da177e4SLinus Torvalds return task_has_perm(current, p, PROCESS__SETSCHED); 33111da177e4SLinus Torvalds } 33121da177e4SLinus Torvalds 33131da177e4SLinus Torvalds static int selinux_task_getscheduler(struct task_struct *p) 33141da177e4SLinus Torvalds { 33151da177e4SLinus Torvalds return task_has_perm(current, p, PROCESS__GETSCHED); 33161da177e4SLinus Torvalds } 33171da177e4SLinus Torvalds 331835601547SDavid Quigley static int selinux_task_movememory(struct task_struct *p) 331935601547SDavid Quigley { 332035601547SDavid Quigley return task_has_perm(current, p, PROCESS__SETSCHED); 332135601547SDavid Quigley } 332235601547SDavid Quigley 3323f9008e4cSDavid Quigley static int selinux_task_kill(struct task_struct *p, struct siginfo *info, 3324f9008e4cSDavid Quigley int sig, u32 secid) 33251da177e4SLinus Torvalds { 33261da177e4SLinus Torvalds u32 perm; 33271da177e4SLinus Torvalds int rc; 3328f9008e4cSDavid Quigley struct task_security_struct *tsec; 33291da177e4SLinus Torvalds 3330f9008e4cSDavid Quigley rc = secondary_ops->task_kill(p, info, sig, secid); 33311da177e4SLinus Torvalds if (rc) 33321da177e4SLinus Torvalds return rc; 33331da177e4SLinus Torvalds 33341da177e4SLinus Torvalds if (!sig) 33351da177e4SLinus Torvalds perm = PROCESS__SIGNULL; /* null signal; existence test */ 33361da177e4SLinus Torvalds else 33371da177e4SLinus Torvalds perm = signal_to_av(sig); 3338f9008e4cSDavid Quigley tsec = p->security; 3339f9008e4cSDavid Quigley if (secid) 3340f9008e4cSDavid Quigley rc = avc_has_perm(secid, tsec->sid, SECCLASS_PROCESS, perm, NULL); 3341f9008e4cSDavid Quigley else 3342f9008e4cSDavid Quigley rc = task_has_perm(current, p, perm); 3343f9008e4cSDavid Quigley return rc; 33441da177e4SLinus Torvalds } 33451da177e4SLinus Torvalds 33461da177e4SLinus Torvalds static int selinux_task_prctl(int option, 33471da177e4SLinus Torvalds unsigned long arg2, 33481da177e4SLinus Torvalds unsigned long arg3, 33491da177e4SLinus Torvalds unsigned long arg4, 33503898b1b4SAndrew G. Morgan unsigned long arg5, 33513898b1b4SAndrew G. Morgan long *rc_p) 33521da177e4SLinus Torvalds { 33531da177e4SLinus Torvalds /* The current prctl operations do not appear to require 33541da177e4SLinus Torvalds any SELinux controls since they merely observe or modify 33551da177e4SLinus Torvalds the state of the current process. */ 33563898b1b4SAndrew G. Morgan return secondary_ops->task_prctl(option, arg2, arg3, arg4, arg5, rc_p); 33571da177e4SLinus Torvalds } 33581da177e4SLinus Torvalds 33591da177e4SLinus Torvalds static int selinux_task_wait(struct task_struct *p) 33601da177e4SLinus Torvalds { 33618a535140SEric Paris return task_has_perm(p, current, PROCESS__SIGCHLD); 33621da177e4SLinus Torvalds } 33631da177e4SLinus Torvalds 33641da177e4SLinus Torvalds static void selinux_task_reparent_to_init(struct task_struct *p) 33651da177e4SLinus Torvalds { 33661da177e4SLinus Torvalds struct task_security_struct *tsec; 33671da177e4SLinus Torvalds 33681da177e4SLinus Torvalds secondary_ops->task_reparent_to_init(p); 33691da177e4SLinus Torvalds 33701da177e4SLinus Torvalds tsec = p->security; 33711da177e4SLinus Torvalds tsec->osid = tsec->sid; 33721da177e4SLinus Torvalds tsec->sid = SECINITSID_KERNEL; 33731da177e4SLinus Torvalds return; 33741da177e4SLinus Torvalds } 33751da177e4SLinus Torvalds 33761da177e4SLinus Torvalds static void selinux_task_to_inode(struct task_struct *p, 33771da177e4SLinus Torvalds struct inode *inode) 33781da177e4SLinus Torvalds { 33791da177e4SLinus Torvalds struct task_security_struct *tsec = p->security; 33801da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 33811da177e4SLinus Torvalds 33821da177e4SLinus Torvalds isec->sid = tsec->sid; 33831da177e4SLinus Torvalds isec->initialized = 1; 33841da177e4SLinus Torvalds return; 33851da177e4SLinus Torvalds } 33861da177e4SLinus Torvalds 33871da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 338867f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv4(struct sk_buff *skb, 338967f83cbfSVenkat Yekkirala struct avc_audit_data *ad, u8 *proto) 33901da177e4SLinus Torvalds { 33911da177e4SLinus Torvalds int offset, ihlen, ret = -EINVAL; 33921da177e4SLinus Torvalds struct iphdr _iph, *ih; 33931da177e4SLinus Torvalds 3394bbe735e4SArnaldo Carvalho de Melo offset = skb_network_offset(skb); 33951da177e4SLinus Torvalds ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph); 33961da177e4SLinus Torvalds if (ih == NULL) 33971da177e4SLinus Torvalds goto out; 33981da177e4SLinus Torvalds 33991da177e4SLinus Torvalds ihlen = ih->ihl * 4; 34001da177e4SLinus Torvalds if (ihlen < sizeof(_iph)) 34011da177e4SLinus Torvalds goto out; 34021da177e4SLinus Torvalds 34031da177e4SLinus Torvalds ad->u.net.v4info.saddr = ih->saddr; 34041da177e4SLinus Torvalds ad->u.net.v4info.daddr = ih->daddr; 34051da177e4SLinus Torvalds ret = 0; 34061da177e4SLinus Torvalds 340767f83cbfSVenkat Yekkirala if (proto) 340867f83cbfSVenkat Yekkirala *proto = ih->protocol; 340967f83cbfSVenkat Yekkirala 34101da177e4SLinus Torvalds switch (ih->protocol) { 34111da177e4SLinus Torvalds case IPPROTO_TCP: { 34121da177e4SLinus Torvalds struct tcphdr _tcph, *th; 34131da177e4SLinus Torvalds 34141da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 34151da177e4SLinus Torvalds break; 34161da177e4SLinus Torvalds 34171da177e4SLinus Torvalds offset += ihlen; 34181da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 34191da177e4SLinus Torvalds if (th == NULL) 34201da177e4SLinus Torvalds break; 34211da177e4SLinus Torvalds 34221da177e4SLinus Torvalds ad->u.net.sport = th->source; 34231da177e4SLinus Torvalds ad->u.net.dport = th->dest; 34241da177e4SLinus Torvalds break; 34251da177e4SLinus Torvalds } 34261da177e4SLinus Torvalds 34271da177e4SLinus Torvalds case IPPROTO_UDP: { 34281da177e4SLinus Torvalds struct udphdr _udph, *uh; 34291da177e4SLinus Torvalds 34301da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 34311da177e4SLinus Torvalds break; 34321da177e4SLinus Torvalds 34331da177e4SLinus Torvalds offset += ihlen; 34341da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 34351da177e4SLinus Torvalds if (uh == NULL) 34361da177e4SLinus Torvalds break; 34371da177e4SLinus Torvalds 34381da177e4SLinus Torvalds ad->u.net.sport = uh->source; 34391da177e4SLinus Torvalds ad->u.net.dport = uh->dest; 34401da177e4SLinus Torvalds break; 34411da177e4SLinus Torvalds } 34421da177e4SLinus Torvalds 34432ee92d46SJames Morris case IPPROTO_DCCP: { 34442ee92d46SJames Morris struct dccp_hdr _dccph, *dh; 34452ee92d46SJames Morris 34462ee92d46SJames Morris if (ntohs(ih->frag_off) & IP_OFFSET) 34472ee92d46SJames Morris break; 34482ee92d46SJames Morris 34492ee92d46SJames Morris offset += ihlen; 34502ee92d46SJames Morris dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); 34512ee92d46SJames Morris if (dh == NULL) 34522ee92d46SJames Morris break; 34532ee92d46SJames Morris 34542ee92d46SJames Morris ad->u.net.sport = dh->dccph_sport; 34552ee92d46SJames Morris ad->u.net.dport = dh->dccph_dport; 34562ee92d46SJames Morris break; 34572ee92d46SJames Morris } 34582ee92d46SJames Morris 34591da177e4SLinus Torvalds default: 34601da177e4SLinus Torvalds break; 34611da177e4SLinus Torvalds } 34621da177e4SLinus Torvalds out: 34631da177e4SLinus Torvalds return ret; 34641da177e4SLinus Torvalds } 34651da177e4SLinus Torvalds 34661da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 34671da177e4SLinus Torvalds 34681da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 346967f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv6(struct sk_buff *skb, 347067f83cbfSVenkat Yekkirala struct avc_audit_data *ad, u8 *proto) 34711da177e4SLinus Torvalds { 34721da177e4SLinus Torvalds u8 nexthdr; 34731da177e4SLinus Torvalds int ret = -EINVAL, offset; 34741da177e4SLinus Torvalds struct ipv6hdr _ipv6h, *ip6; 34751da177e4SLinus Torvalds 3476bbe735e4SArnaldo Carvalho de Melo offset = skb_network_offset(skb); 34771da177e4SLinus Torvalds ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h); 34781da177e4SLinus Torvalds if (ip6 == NULL) 34791da177e4SLinus Torvalds goto out; 34801da177e4SLinus Torvalds 34811da177e4SLinus Torvalds ipv6_addr_copy(&ad->u.net.v6info.saddr, &ip6->saddr); 34821da177e4SLinus Torvalds ipv6_addr_copy(&ad->u.net.v6info.daddr, &ip6->daddr); 34831da177e4SLinus Torvalds ret = 0; 34841da177e4SLinus Torvalds 34851da177e4SLinus Torvalds nexthdr = ip6->nexthdr; 34861da177e4SLinus Torvalds offset += sizeof(_ipv6h); 34870d3d077cSHerbert Xu offset = ipv6_skip_exthdr(skb, offset, &nexthdr); 34881da177e4SLinus Torvalds if (offset < 0) 34891da177e4SLinus Torvalds goto out; 34901da177e4SLinus Torvalds 349167f83cbfSVenkat Yekkirala if (proto) 349267f83cbfSVenkat Yekkirala *proto = nexthdr; 349367f83cbfSVenkat Yekkirala 34941da177e4SLinus Torvalds switch (nexthdr) { 34951da177e4SLinus Torvalds case IPPROTO_TCP: { 34961da177e4SLinus Torvalds struct tcphdr _tcph, *th; 34971da177e4SLinus Torvalds 34981da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 34991da177e4SLinus Torvalds if (th == NULL) 35001da177e4SLinus Torvalds break; 35011da177e4SLinus Torvalds 35021da177e4SLinus Torvalds ad->u.net.sport = th->source; 35031da177e4SLinus Torvalds ad->u.net.dport = th->dest; 35041da177e4SLinus Torvalds break; 35051da177e4SLinus Torvalds } 35061da177e4SLinus Torvalds 35071da177e4SLinus Torvalds case IPPROTO_UDP: { 35081da177e4SLinus Torvalds struct udphdr _udph, *uh; 35091da177e4SLinus Torvalds 35101da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 35111da177e4SLinus Torvalds if (uh == NULL) 35121da177e4SLinus Torvalds break; 35131da177e4SLinus Torvalds 35141da177e4SLinus Torvalds ad->u.net.sport = uh->source; 35151da177e4SLinus Torvalds ad->u.net.dport = uh->dest; 35161da177e4SLinus Torvalds break; 35171da177e4SLinus Torvalds } 35181da177e4SLinus Torvalds 35192ee92d46SJames Morris case IPPROTO_DCCP: { 35202ee92d46SJames Morris struct dccp_hdr _dccph, *dh; 35212ee92d46SJames Morris 35222ee92d46SJames Morris dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); 35232ee92d46SJames Morris if (dh == NULL) 35242ee92d46SJames Morris break; 35252ee92d46SJames Morris 35262ee92d46SJames Morris ad->u.net.sport = dh->dccph_sport; 35272ee92d46SJames Morris ad->u.net.dport = dh->dccph_dport; 35282ee92d46SJames Morris break; 35292ee92d46SJames Morris } 35302ee92d46SJames Morris 35311da177e4SLinus Torvalds /* includes fragments */ 35321da177e4SLinus Torvalds default: 35331da177e4SLinus Torvalds break; 35341da177e4SLinus Torvalds } 35351da177e4SLinus Torvalds out: 35361da177e4SLinus Torvalds return ret; 35371da177e4SLinus Torvalds } 35381da177e4SLinus Torvalds 35391da177e4SLinus Torvalds #endif /* IPV6 */ 35401da177e4SLinus Torvalds 35411da177e4SLinus Torvalds static int selinux_parse_skb(struct sk_buff *skb, struct avc_audit_data *ad, 3542*cf9481e2SDavid Howells char **_addrp, int src, u8 *proto) 35431da177e4SLinus Torvalds { 3544*cf9481e2SDavid Howells char *addrp; 3545*cf9481e2SDavid Howells int ret; 35461da177e4SLinus Torvalds 35471da177e4SLinus Torvalds switch (ad->u.net.family) { 35481da177e4SLinus Torvalds case PF_INET: 354967f83cbfSVenkat Yekkirala ret = selinux_parse_skb_ipv4(skb, ad, proto); 3550*cf9481e2SDavid Howells if (ret) 3551*cf9481e2SDavid Howells goto parse_error; 3552*cf9481e2SDavid Howells addrp = (char *)(src ? &ad->u.net.v4info.saddr : 35531da177e4SLinus Torvalds &ad->u.net.v4info.daddr); 3554*cf9481e2SDavid Howells goto okay; 35551da177e4SLinus Torvalds 35561da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 35571da177e4SLinus Torvalds case PF_INET6: 355867f83cbfSVenkat Yekkirala ret = selinux_parse_skb_ipv6(skb, ad, proto); 3559*cf9481e2SDavid Howells if (ret) 3560*cf9481e2SDavid Howells goto parse_error; 3561*cf9481e2SDavid Howells addrp = (char *)(src ? &ad->u.net.v6info.saddr : 35621da177e4SLinus Torvalds &ad->u.net.v6info.daddr); 3563*cf9481e2SDavid Howells goto okay; 35641da177e4SLinus Torvalds #endif /* IPV6 */ 35651da177e4SLinus Torvalds default: 3566*cf9481e2SDavid Howells addrp = NULL; 3567*cf9481e2SDavid Howells goto okay; 35681da177e4SLinus Torvalds } 35691da177e4SLinus Torvalds 3570*cf9481e2SDavid Howells parse_error: 357171f1cb05SPaul Moore printk(KERN_WARNING 357271f1cb05SPaul Moore "SELinux: failure in selinux_parse_skb()," 357371f1cb05SPaul Moore " unable to parse packet\n"); 35741da177e4SLinus Torvalds return ret; 3575*cf9481e2SDavid Howells 3576*cf9481e2SDavid Howells okay: 3577*cf9481e2SDavid Howells if (_addrp) 3578*cf9481e2SDavid Howells *_addrp = addrp; 3579*cf9481e2SDavid Howells return 0; 35801da177e4SLinus Torvalds } 35811da177e4SLinus Torvalds 35824f6a993fSPaul Moore /** 3583220deb96SPaul Moore * selinux_skb_peerlbl_sid - Determine the peer label of a packet 35844f6a993fSPaul Moore * @skb: the packet 358575e22910SPaul Moore * @family: protocol family 3586220deb96SPaul Moore * @sid: the packet's peer label SID 35874f6a993fSPaul Moore * 35884f6a993fSPaul Moore * Description: 3589220deb96SPaul Moore * Check the various different forms of network peer labeling and determine 3590220deb96SPaul Moore * the peer label/SID for the packet; most of the magic actually occurs in 3591220deb96SPaul Moore * the security server function security_net_peersid_cmp(). The function 3592220deb96SPaul Moore * returns zero if the value in @sid is valid (although it may be SECSID_NULL) 3593220deb96SPaul Moore * or -EACCES if @sid is invalid due to inconsistencies with the different 3594220deb96SPaul Moore * peer labels. 35954f6a993fSPaul Moore * 35964f6a993fSPaul Moore */ 3597220deb96SPaul Moore static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid) 35984f6a993fSPaul Moore { 359971f1cb05SPaul Moore int err; 36004f6a993fSPaul Moore u32 xfrm_sid; 36014f6a993fSPaul Moore u32 nlbl_sid; 3602220deb96SPaul Moore u32 nlbl_type; 36034f6a993fSPaul Moore 36044f6a993fSPaul Moore selinux_skb_xfrm_sid(skb, &xfrm_sid); 36055dbe1eb0SPaul Moore selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid); 3606220deb96SPaul Moore 360771f1cb05SPaul Moore err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid); 360871f1cb05SPaul Moore if (unlikely(err)) { 360971f1cb05SPaul Moore printk(KERN_WARNING 361071f1cb05SPaul Moore "SELinux: failure in selinux_skb_peerlbl_sid()," 361171f1cb05SPaul Moore " unable to determine packet's peer label\n"); 3612220deb96SPaul Moore return -EACCES; 361371f1cb05SPaul Moore } 3614220deb96SPaul Moore 3615220deb96SPaul Moore return 0; 36164f6a993fSPaul Moore } 36174f6a993fSPaul Moore 36181da177e4SLinus Torvalds /* socket security operations */ 36191da177e4SLinus Torvalds static int socket_has_perm(struct task_struct *task, struct socket *sock, 36201da177e4SLinus Torvalds u32 perms) 36211da177e4SLinus Torvalds { 36221da177e4SLinus Torvalds struct inode_security_struct *isec; 36231da177e4SLinus Torvalds struct task_security_struct *tsec; 36241da177e4SLinus Torvalds struct avc_audit_data ad; 36251da177e4SLinus Torvalds int err = 0; 36261da177e4SLinus Torvalds 36271da177e4SLinus Torvalds tsec = task->security; 36281da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 36291da177e4SLinus Torvalds 36301da177e4SLinus Torvalds if (isec->sid == SECINITSID_KERNEL) 36311da177e4SLinus Torvalds goto out; 36321da177e4SLinus Torvalds 36331da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, NET); 36341da177e4SLinus Torvalds ad.u.net.sk = sock->sk; 36351da177e4SLinus Torvalds err = avc_has_perm(tsec->sid, isec->sid, isec->sclass, perms, &ad); 36361da177e4SLinus Torvalds 36371da177e4SLinus Torvalds out: 36381da177e4SLinus Torvalds return err; 36391da177e4SLinus Torvalds } 36401da177e4SLinus Torvalds 36411da177e4SLinus Torvalds static int selinux_socket_create(int family, int type, 36421da177e4SLinus Torvalds int protocol, int kern) 36431da177e4SLinus Torvalds { 36441da177e4SLinus Torvalds int err = 0; 36451da177e4SLinus Torvalds struct task_security_struct *tsec; 364642c3e03eSEric Paris u32 newsid; 36471da177e4SLinus Torvalds 36481da177e4SLinus Torvalds if (kern) 36491da177e4SLinus Torvalds goto out; 36501da177e4SLinus Torvalds 36511da177e4SLinus Torvalds tsec = current->security; 365242c3e03eSEric Paris newsid = tsec->sockcreate_sid ? : tsec->sid; 365342c3e03eSEric Paris err = avc_has_perm(tsec->sid, newsid, 36541da177e4SLinus Torvalds socket_type_to_security_class(family, type, 36551da177e4SLinus Torvalds protocol), SOCKET__CREATE, NULL); 36561da177e4SLinus Torvalds 36571da177e4SLinus Torvalds out: 36581da177e4SLinus Torvalds return err; 36591da177e4SLinus Torvalds } 36601da177e4SLinus Torvalds 36617420ed23SVenkat Yekkirala static int selinux_socket_post_create(struct socket *sock, int family, 36621da177e4SLinus Torvalds int type, int protocol, int kern) 36631da177e4SLinus Torvalds { 36647420ed23SVenkat Yekkirala int err = 0; 36651da177e4SLinus Torvalds struct inode_security_struct *isec; 36661da177e4SLinus Torvalds struct task_security_struct *tsec; 3667892c141eSVenkat Yekkirala struct sk_security_struct *sksec; 366842c3e03eSEric Paris u32 newsid; 36691da177e4SLinus Torvalds 36701da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 36711da177e4SLinus Torvalds 36721da177e4SLinus Torvalds tsec = current->security; 367342c3e03eSEric Paris newsid = tsec->sockcreate_sid ? : tsec->sid; 36741da177e4SLinus Torvalds isec->sclass = socket_type_to_security_class(family, type, protocol); 367542c3e03eSEric Paris isec->sid = kern ? SECINITSID_KERNEL : newsid; 36761da177e4SLinus Torvalds isec->initialized = 1; 36771da177e4SLinus Torvalds 3678892c141eSVenkat Yekkirala if (sock->sk) { 3679892c141eSVenkat Yekkirala sksec = sock->sk->sk_security; 3680892c141eSVenkat Yekkirala sksec->sid = isec->sid; 3681220deb96SPaul Moore sksec->sclass = isec->sclass; 36829f2ad665SPaul Moore err = selinux_netlbl_socket_post_create(sock); 3683892c141eSVenkat Yekkirala } 3684892c141eSVenkat Yekkirala 36857420ed23SVenkat Yekkirala return err; 36861da177e4SLinus Torvalds } 36871da177e4SLinus Torvalds 36881da177e4SLinus Torvalds /* Range of port numbers used to automatically bind. 36891da177e4SLinus Torvalds Need to determine whether we should perform a name_bind 36901da177e4SLinus Torvalds permission check between the socket and the port number. */ 36911da177e4SLinus Torvalds 36921da177e4SLinus Torvalds static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen) 36931da177e4SLinus Torvalds { 36941da177e4SLinus Torvalds u16 family; 36951da177e4SLinus Torvalds int err; 36961da177e4SLinus Torvalds 36971da177e4SLinus Torvalds err = socket_has_perm(current, sock, SOCKET__BIND); 36981da177e4SLinus Torvalds if (err) 36991da177e4SLinus Torvalds goto out; 37001da177e4SLinus Torvalds 37011da177e4SLinus Torvalds /* 37021da177e4SLinus Torvalds * If PF_INET or PF_INET6, check name_bind permission for the port. 370313402580SJames Morris * Multiple address binding for SCTP is not supported yet: we just 370413402580SJames Morris * check the first address now. 37051da177e4SLinus Torvalds */ 37061da177e4SLinus Torvalds family = sock->sk->sk_family; 37071da177e4SLinus Torvalds if (family == PF_INET || family == PF_INET6) { 37081da177e4SLinus Torvalds char *addrp; 37091da177e4SLinus Torvalds struct inode_security_struct *isec; 37101da177e4SLinus Torvalds struct task_security_struct *tsec; 37111da177e4SLinus Torvalds struct avc_audit_data ad; 37121da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 37131da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 37141da177e4SLinus Torvalds unsigned short snum; 37151da177e4SLinus Torvalds struct sock *sk = sock->sk; 3716e399f982SJames Morris u32 sid, node_perm; 37171da177e4SLinus Torvalds 37181da177e4SLinus Torvalds tsec = current->security; 37191da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 37201da177e4SLinus Torvalds 37211da177e4SLinus Torvalds if (family == PF_INET) { 37221da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 37231da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 37241da177e4SLinus Torvalds addrp = (char *)&addr4->sin_addr.s_addr; 37251da177e4SLinus Torvalds } else { 37261da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 37271da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 37281da177e4SLinus Torvalds addrp = (char *)&addr6->sin6_addr.s6_addr; 37291da177e4SLinus Torvalds } 37301da177e4SLinus Torvalds 3731227b60f5SStephen Hemminger if (snum) { 3732227b60f5SStephen Hemminger int low, high; 3733227b60f5SStephen Hemminger 3734227b60f5SStephen Hemminger inet_get_local_port_range(&low, &high); 3735227b60f5SStephen Hemminger 3736227b60f5SStephen Hemminger if (snum < max(PROT_SOCK, low) || snum > high) { 37373e112172SPaul Moore err = sel_netport_sid(sk->sk_protocol, 37383e112172SPaul Moore snum, &sid); 37391da177e4SLinus Torvalds if (err) 37401da177e4SLinus Torvalds goto out; 37411da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, NET); 37421da177e4SLinus Torvalds ad.u.net.sport = htons(snum); 37431da177e4SLinus Torvalds ad.u.net.family = family; 37441da177e4SLinus Torvalds err = avc_has_perm(isec->sid, sid, 37451da177e4SLinus Torvalds isec->sclass, 37461da177e4SLinus Torvalds SOCKET__NAME_BIND, &ad); 37471da177e4SLinus Torvalds if (err) 37481da177e4SLinus Torvalds goto out; 37491da177e4SLinus Torvalds } 3750227b60f5SStephen Hemminger } 37511da177e4SLinus Torvalds 375213402580SJames Morris switch (isec->sclass) { 375313402580SJames Morris case SECCLASS_TCP_SOCKET: 37541da177e4SLinus Torvalds node_perm = TCP_SOCKET__NODE_BIND; 37551da177e4SLinus Torvalds break; 37561da177e4SLinus Torvalds 375713402580SJames Morris case SECCLASS_UDP_SOCKET: 37581da177e4SLinus Torvalds node_perm = UDP_SOCKET__NODE_BIND; 37591da177e4SLinus Torvalds break; 37601da177e4SLinus Torvalds 37612ee92d46SJames Morris case SECCLASS_DCCP_SOCKET: 37622ee92d46SJames Morris node_perm = DCCP_SOCKET__NODE_BIND; 37632ee92d46SJames Morris break; 37642ee92d46SJames Morris 37651da177e4SLinus Torvalds default: 37661da177e4SLinus Torvalds node_perm = RAWIP_SOCKET__NODE_BIND; 37671da177e4SLinus Torvalds break; 37681da177e4SLinus Torvalds } 37691da177e4SLinus Torvalds 3770224dfbd8SPaul Moore err = sel_netnode_sid(addrp, family, &sid); 37711da177e4SLinus Torvalds if (err) 37721da177e4SLinus Torvalds goto out; 37731da177e4SLinus Torvalds 37741da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, NET); 37751da177e4SLinus Torvalds ad.u.net.sport = htons(snum); 37761da177e4SLinus Torvalds ad.u.net.family = family; 37771da177e4SLinus Torvalds 37781da177e4SLinus Torvalds if (family == PF_INET) 37791da177e4SLinus Torvalds ad.u.net.v4info.saddr = addr4->sin_addr.s_addr; 37801da177e4SLinus Torvalds else 37811da177e4SLinus Torvalds ipv6_addr_copy(&ad.u.net.v6info.saddr, &addr6->sin6_addr); 37821da177e4SLinus Torvalds 37831da177e4SLinus Torvalds err = avc_has_perm(isec->sid, sid, 37841da177e4SLinus Torvalds isec->sclass, node_perm, &ad); 37851da177e4SLinus Torvalds if (err) 37861da177e4SLinus Torvalds goto out; 37871da177e4SLinus Torvalds } 37881da177e4SLinus Torvalds out: 37891da177e4SLinus Torvalds return err; 37901da177e4SLinus Torvalds } 37911da177e4SLinus Torvalds 37921da177e4SLinus Torvalds static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen) 37931da177e4SLinus Torvalds { 37941da177e4SLinus Torvalds struct inode_security_struct *isec; 37951da177e4SLinus Torvalds int err; 37961da177e4SLinus Torvalds 37971da177e4SLinus Torvalds err = socket_has_perm(current, sock, SOCKET__CONNECT); 37981da177e4SLinus Torvalds if (err) 37991da177e4SLinus Torvalds return err; 38001da177e4SLinus Torvalds 38011da177e4SLinus Torvalds /* 38022ee92d46SJames Morris * If a TCP or DCCP socket, check name_connect permission for the port. 38031da177e4SLinus Torvalds */ 38041da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 38052ee92d46SJames Morris if (isec->sclass == SECCLASS_TCP_SOCKET || 38062ee92d46SJames Morris isec->sclass == SECCLASS_DCCP_SOCKET) { 38071da177e4SLinus Torvalds struct sock *sk = sock->sk; 38081da177e4SLinus Torvalds struct avc_audit_data ad; 38091da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 38101da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 38111da177e4SLinus Torvalds unsigned short snum; 38122ee92d46SJames Morris u32 sid, perm; 38131da177e4SLinus Torvalds 38141da177e4SLinus Torvalds if (sk->sk_family == PF_INET) { 38151da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 3816911656f8SStephen Smalley if (addrlen < sizeof(struct sockaddr_in)) 38171da177e4SLinus Torvalds return -EINVAL; 38181da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 38191da177e4SLinus Torvalds } else { 38201da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 3821911656f8SStephen Smalley if (addrlen < SIN6_LEN_RFC2133) 38221da177e4SLinus Torvalds return -EINVAL; 38231da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 38241da177e4SLinus Torvalds } 38251da177e4SLinus Torvalds 38263e112172SPaul Moore err = sel_netport_sid(sk->sk_protocol, snum, &sid); 38271da177e4SLinus Torvalds if (err) 38281da177e4SLinus Torvalds goto out; 38291da177e4SLinus Torvalds 38302ee92d46SJames Morris perm = (isec->sclass == SECCLASS_TCP_SOCKET) ? 38312ee92d46SJames Morris TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT; 38322ee92d46SJames Morris 38331da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, NET); 38341da177e4SLinus Torvalds ad.u.net.dport = htons(snum); 38351da177e4SLinus Torvalds ad.u.net.family = sk->sk_family; 38362ee92d46SJames Morris err = avc_has_perm(isec->sid, sid, isec->sclass, perm, &ad); 38371da177e4SLinus Torvalds if (err) 38381da177e4SLinus Torvalds goto out; 38391da177e4SLinus Torvalds } 38401da177e4SLinus Torvalds 38411da177e4SLinus Torvalds out: 38421da177e4SLinus Torvalds return err; 38431da177e4SLinus Torvalds } 38441da177e4SLinus Torvalds 38451da177e4SLinus Torvalds static int selinux_socket_listen(struct socket *sock, int backlog) 38461da177e4SLinus Torvalds { 38471da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__LISTEN); 38481da177e4SLinus Torvalds } 38491da177e4SLinus Torvalds 38501da177e4SLinus Torvalds static int selinux_socket_accept(struct socket *sock, struct socket *newsock) 38511da177e4SLinus Torvalds { 38521da177e4SLinus Torvalds int err; 38531da177e4SLinus Torvalds struct inode_security_struct *isec; 38541da177e4SLinus Torvalds struct inode_security_struct *newisec; 38551da177e4SLinus Torvalds 38561da177e4SLinus Torvalds err = socket_has_perm(current, sock, SOCKET__ACCEPT); 38571da177e4SLinus Torvalds if (err) 38581da177e4SLinus Torvalds return err; 38591da177e4SLinus Torvalds 38601da177e4SLinus Torvalds newisec = SOCK_INODE(newsock)->i_security; 38611da177e4SLinus Torvalds 38621da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 38631da177e4SLinus Torvalds newisec->sclass = isec->sclass; 38641da177e4SLinus Torvalds newisec->sid = isec->sid; 38651da177e4SLinus Torvalds newisec->initialized = 1; 38661da177e4SLinus Torvalds 38671da177e4SLinus Torvalds return 0; 38681da177e4SLinus Torvalds } 38691da177e4SLinus Torvalds 38701da177e4SLinus Torvalds static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg, 38711da177e4SLinus Torvalds int size) 38721da177e4SLinus Torvalds { 38737420ed23SVenkat Yekkirala int rc; 38747420ed23SVenkat Yekkirala 38757420ed23SVenkat Yekkirala rc = socket_has_perm(current, sock, SOCKET__WRITE); 38767420ed23SVenkat Yekkirala if (rc) 38777420ed23SVenkat Yekkirala return rc; 38787420ed23SVenkat Yekkirala 38797420ed23SVenkat Yekkirala return selinux_netlbl_inode_permission(SOCK_INODE(sock), MAY_WRITE); 38801da177e4SLinus Torvalds } 38811da177e4SLinus Torvalds 38821da177e4SLinus Torvalds static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg, 38831da177e4SLinus Torvalds int size, int flags) 38841da177e4SLinus Torvalds { 38851da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__READ); 38861da177e4SLinus Torvalds } 38871da177e4SLinus Torvalds 38881da177e4SLinus Torvalds static int selinux_socket_getsockname(struct socket *sock) 38891da177e4SLinus Torvalds { 38901da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__GETATTR); 38911da177e4SLinus Torvalds } 38921da177e4SLinus Torvalds 38931da177e4SLinus Torvalds static int selinux_socket_getpeername(struct socket *sock) 38941da177e4SLinus Torvalds { 38951da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__GETATTR); 38961da177e4SLinus Torvalds } 38971da177e4SLinus Torvalds 38981da177e4SLinus Torvalds static int selinux_socket_setsockopt(struct socket *sock, int level, int optname) 38991da177e4SLinus Torvalds { 3900f8687afeSPaul Moore int err; 3901f8687afeSPaul Moore 3902f8687afeSPaul Moore err = socket_has_perm(current, sock, SOCKET__SETOPT); 3903f8687afeSPaul Moore if (err) 3904f8687afeSPaul Moore return err; 3905f8687afeSPaul Moore 3906f8687afeSPaul Moore return selinux_netlbl_socket_setsockopt(sock, level, optname); 39071da177e4SLinus Torvalds } 39081da177e4SLinus Torvalds 39091da177e4SLinus Torvalds static int selinux_socket_getsockopt(struct socket *sock, int level, 39101da177e4SLinus Torvalds int optname) 39111da177e4SLinus Torvalds { 39121da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__GETOPT); 39131da177e4SLinus Torvalds } 39141da177e4SLinus Torvalds 39151da177e4SLinus Torvalds static int selinux_socket_shutdown(struct socket *sock, int how) 39161da177e4SLinus Torvalds { 39171da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__SHUTDOWN); 39181da177e4SLinus Torvalds } 39191da177e4SLinus Torvalds 39201da177e4SLinus Torvalds static int selinux_socket_unix_stream_connect(struct socket *sock, 39211da177e4SLinus Torvalds struct socket *other, 39221da177e4SLinus Torvalds struct sock *newsk) 39231da177e4SLinus Torvalds { 39241da177e4SLinus Torvalds struct sk_security_struct *ssec; 39251da177e4SLinus Torvalds struct inode_security_struct *isec; 39261da177e4SLinus Torvalds struct inode_security_struct *other_isec; 39271da177e4SLinus Torvalds struct avc_audit_data ad; 39281da177e4SLinus Torvalds int err; 39291da177e4SLinus Torvalds 39301da177e4SLinus Torvalds err = secondary_ops->unix_stream_connect(sock, other, newsk); 39311da177e4SLinus Torvalds if (err) 39321da177e4SLinus Torvalds return err; 39331da177e4SLinus Torvalds 39341da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 39351da177e4SLinus Torvalds other_isec = SOCK_INODE(other)->i_security; 39361da177e4SLinus Torvalds 39371da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, NET); 39381da177e4SLinus Torvalds ad.u.net.sk = other->sk; 39391da177e4SLinus Torvalds 39401da177e4SLinus Torvalds err = avc_has_perm(isec->sid, other_isec->sid, 39411da177e4SLinus Torvalds isec->sclass, 39421da177e4SLinus Torvalds UNIX_STREAM_SOCKET__CONNECTTO, &ad); 39431da177e4SLinus Torvalds if (err) 39441da177e4SLinus Torvalds return err; 39451da177e4SLinus Torvalds 39461da177e4SLinus Torvalds /* connecting socket */ 39471da177e4SLinus Torvalds ssec = sock->sk->sk_security; 39481da177e4SLinus Torvalds ssec->peer_sid = other_isec->sid; 39491da177e4SLinus Torvalds 39501da177e4SLinus Torvalds /* server child socket */ 39511da177e4SLinus Torvalds ssec = newsk->sk_security; 39521da177e4SLinus Torvalds ssec->peer_sid = isec->sid; 39534237c75cSVenkat Yekkirala err = security_sid_mls_copy(other_isec->sid, ssec->peer_sid, &ssec->sid); 39541da177e4SLinus Torvalds 39554237c75cSVenkat Yekkirala return err; 39561da177e4SLinus Torvalds } 39571da177e4SLinus Torvalds 39581da177e4SLinus Torvalds static int selinux_socket_unix_may_send(struct socket *sock, 39591da177e4SLinus Torvalds struct socket *other) 39601da177e4SLinus Torvalds { 39611da177e4SLinus Torvalds struct inode_security_struct *isec; 39621da177e4SLinus Torvalds struct inode_security_struct *other_isec; 39631da177e4SLinus Torvalds struct avc_audit_data ad; 39641da177e4SLinus Torvalds int err; 39651da177e4SLinus Torvalds 39661da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 39671da177e4SLinus Torvalds other_isec = SOCK_INODE(other)->i_security; 39681da177e4SLinus Torvalds 39691da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, NET); 39701da177e4SLinus Torvalds ad.u.net.sk = other->sk; 39711da177e4SLinus Torvalds 39721da177e4SLinus Torvalds err = avc_has_perm(isec->sid, other_isec->sid, 39731da177e4SLinus Torvalds isec->sclass, SOCKET__SENDTO, &ad); 39741da177e4SLinus Torvalds if (err) 39751da177e4SLinus Torvalds return err; 39761da177e4SLinus Torvalds 39771da177e4SLinus Torvalds return 0; 39781da177e4SLinus Torvalds } 39791da177e4SLinus Torvalds 3980effad8dfSPaul Moore static int selinux_inet_sys_rcv_skb(int ifindex, char *addrp, u16 family, 3981effad8dfSPaul Moore u32 peer_sid, 3982effad8dfSPaul Moore struct avc_audit_data *ad) 3983effad8dfSPaul Moore { 3984effad8dfSPaul Moore int err; 3985effad8dfSPaul Moore u32 if_sid; 3986effad8dfSPaul Moore u32 node_sid; 3987effad8dfSPaul Moore 3988effad8dfSPaul Moore err = sel_netif_sid(ifindex, &if_sid); 3989effad8dfSPaul Moore if (err) 3990effad8dfSPaul Moore return err; 3991effad8dfSPaul Moore err = avc_has_perm(peer_sid, if_sid, 3992effad8dfSPaul Moore SECCLASS_NETIF, NETIF__INGRESS, ad); 3993effad8dfSPaul Moore if (err) 3994effad8dfSPaul Moore return err; 3995effad8dfSPaul Moore 3996effad8dfSPaul Moore err = sel_netnode_sid(addrp, family, &node_sid); 3997effad8dfSPaul Moore if (err) 3998effad8dfSPaul Moore return err; 3999effad8dfSPaul Moore return avc_has_perm(peer_sid, node_sid, 4000effad8dfSPaul Moore SECCLASS_NODE, NODE__RECVFROM, ad); 4001effad8dfSPaul Moore } 4002effad8dfSPaul Moore 4003220deb96SPaul Moore static int selinux_sock_rcv_skb_iptables_compat(struct sock *sk, 4004220deb96SPaul Moore struct sk_buff *skb, 4005224dfbd8SPaul Moore struct avc_audit_data *ad, 4006220deb96SPaul Moore u16 family, 4007220deb96SPaul Moore char *addrp) 40081da177e4SLinus Torvalds { 4009220deb96SPaul Moore int err; 4010220deb96SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 4011220deb96SPaul Moore u16 sk_class; 4012220deb96SPaul Moore u32 netif_perm, node_perm, recv_perm; 4013220deb96SPaul Moore u32 port_sid, node_sid, if_sid, sk_sid; 40144237c75cSVenkat Yekkirala 4015220deb96SPaul Moore sk_sid = sksec->sid; 4016220deb96SPaul Moore sk_class = sksec->sclass; 40171da177e4SLinus Torvalds 4018220deb96SPaul Moore switch (sk_class) { 40191da177e4SLinus Torvalds case SECCLASS_UDP_SOCKET: 40201da177e4SLinus Torvalds netif_perm = NETIF__UDP_RECV; 40211da177e4SLinus Torvalds node_perm = NODE__UDP_RECV; 40221da177e4SLinus Torvalds recv_perm = UDP_SOCKET__RECV_MSG; 40231da177e4SLinus Torvalds break; 40241da177e4SLinus Torvalds case SECCLASS_TCP_SOCKET: 40251da177e4SLinus Torvalds netif_perm = NETIF__TCP_RECV; 40261da177e4SLinus Torvalds node_perm = NODE__TCP_RECV; 40271da177e4SLinus Torvalds recv_perm = TCP_SOCKET__RECV_MSG; 40281da177e4SLinus Torvalds break; 40292ee92d46SJames Morris case SECCLASS_DCCP_SOCKET: 40302ee92d46SJames Morris netif_perm = NETIF__DCCP_RECV; 40312ee92d46SJames Morris node_perm = NODE__DCCP_RECV; 40322ee92d46SJames Morris recv_perm = DCCP_SOCKET__RECV_MSG; 40332ee92d46SJames Morris break; 40341da177e4SLinus Torvalds default: 40351da177e4SLinus Torvalds netif_perm = NETIF__RAWIP_RECV; 40361da177e4SLinus Torvalds node_perm = NODE__RAWIP_RECV; 4037220deb96SPaul Moore recv_perm = 0; 40381da177e4SLinus Torvalds break; 40391da177e4SLinus Torvalds } 40401da177e4SLinus Torvalds 4041220deb96SPaul Moore err = sel_netif_sid(skb->iif, &if_sid); 40421da177e4SLinus Torvalds if (err) 4043220deb96SPaul Moore return err; 4044220deb96SPaul Moore err = avc_has_perm(sk_sid, if_sid, SECCLASS_NETIF, netif_perm, ad); 4045220deb96SPaul Moore if (err) 4046220deb96SPaul Moore return err; 40471da177e4SLinus Torvalds 4048224dfbd8SPaul Moore err = sel_netnode_sid(addrp, family, &node_sid); 40491da177e4SLinus Torvalds if (err) 4050220deb96SPaul Moore return err; 4051220deb96SPaul Moore err = avc_has_perm(sk_sid, node_sid, SECCLASS_NODE, node_perm, ad); 40521da177e4SLinus Torvalds if (err) 4053220deb96SPaul Moore return err; 40541da177e4SLinus Torvalds 4055220deb96SPaul Moore if (!recv_perm) 4056220deb96SPaul Moore return 0; 40573e112172SPaul Moore err = sel_netport_sid(sk->sk_protocol, 40583e112172SPaul Moore ntohs(ad->u.net.sport), &port_sid); 405971f1cb05SPaul Moore if (unlikely(err)) { 406071f1cb05SPaul Moore printk(KERN_WARNING 406171f1cb05SPaul Moore "SELinux: failure in" 406271f1cb05SPaul Moore " selinux_sock_rcv_skb_iptables_compat()," 406371f1cb05SPaul Moore " network port label not found\n"); 4064220deb96SPaul Moore return err; 406571f1cb05SPaul Moore } 4066220deb96SPaul Moore return avc_has_perm(sk_sid, port_sid, sk_class, recv_perm, ad); 40671da177e4SLinus Torvalds } 4068d28d1e08STrent Jaeger 4069220deb96SPaul Moore static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb, 4070220deb96SPaul Moore struct avc_audit_data *ad, 4071220deb96SPaul Moore u16 family, char *addrp) 4072220deb96SPaul Moore { 4073220deb96SPaul Moore int err; 4074220deb96SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 4075220deb96SPaul Moore u32 peer_sid; 4076220deb96SPaul Moore u32 sk_sid = sksec->sid; 4077220deb96SPaul Moore 4078220deb96SPaul Moore if (selinux_compat_net) 4079220deb96SPaul Moore err = selinux_sock_rcv_skb_iptables_compat(sk, skb, ad, 4080220deb96SPaul Moore family, addrp); 4081220deb96SPaul Moore else 4082220deb96SPaul Moore err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET, 4083220deb96SPaul Moore PACKET__RECV, ad); 4084220deb96SPaul Moore if (err) 4085220deb96SPaul Moore return err; 4086220deb96SPaul Moore 4087220deb96SPaul Moore if (selinux_policycap_netpeer) { 4088220deb96SPaul Moore err = selinux_skb_peerlbl_sid(skb, family, &peer_sid); 4089220deb96SPaul Moore if (err) 4090220deb96SPaul Moore return err; 4091220deb96SPaul Moore err = avc_has_perm(sk_sid, peer_sid, 4092220deb96SPaul Moore SECCLASS_PEER, PEER__RECV, ad); 4093220deb96SPaul Moore } else { 4094220deb96SPaul Moore err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, ad); 4095220deb96SPaul Moore if (err) 4096220deb96SPaul Moore return err; 4097220deb96SPaul Moore err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, ad); 4098220deb96SPaul Moore } 4099220deb96SPaul Moore 41004e5ab4cbSJames Morris return err; 41014e5ab4cbSJames Morris } 4102d28d1e08STrent Jaeger 41034e5ab4cbSJames Morris static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) 41044e5ab4cbSJames Morris { 4105220deb96SPaul Moore int err; 41064237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 4107220deb96SPaul Moore u16 family = sk->sk_family; 4108220deb96SPaul Moore u32 sk_sid = sksec->sid; 4109220deb96SPaul Moore struct avc_audit_data ad; 4110220deb96SPaul Moore char *addrp; 41114e5ab4cbSJames Morris 41124e5ab4cbSJames Morris if (family != PF_INET && family != PF_INET6) 4113220deb96SPaul Moore return 0; 41144e5ab4cbSJames Morris 41154e5ab4cbSJames Morris /* Handle mapped IPv4 packets arriving via IPv6 sockets */ 411687fcd70dSAl Viro if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 41174e5ab4cbSJames Morris family = PF_INET; 41184e5ab4cbSJames Morris 41194e5ab4cbSJames Morris AVC_AUDIT_DATA_INIT(&ad, NET); 4120da5645a2SPaul Moore ad.u.net.netif = skb->iif; 41214e5ab4cbSJames Morris ad.u.net.family = family; 4122224dfbd8SPaul Moore err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); 41234e5ab4cbSJames Morris if (err) 4124220deb96SPaul Moore return err; 41254e5ab4cbSJames Morris 4126220deb96SPaul Moore /* If any sort of compatibility mode is enabled then handoff processing 4127220deb96SPaul Moore * to the selinux_sock_rcv_skb_compat() function to deal with the 4128220deb96SPaul Moore * special handling. We do this in an attempt to keep this function 4129220deb96SPaul Moore * as fast and as clean as possible. */ 4130220deb96SPaul Moore if (selinux_compat_net || !selinux_policycap_netpeer) 4131220deb96SPaul Moore return selinux_sock_rcv_skb_compat(sk, skb, &ad, 4132220deb96SPaul Moore family, addrp); 4133220deb96SPaul Moore 4134d621d35eSPaul Moore if (netlbl_enabled() || selinux_xfrm_enabled()) { 4135d621d35eSPaul Moore u32 peer_sid; 4136220deb96SPaul Moore 4137220deb96SPaul Moore err = selinux_skb_peerlbl_sid(skb, family, &peer_sid); 4138220deb96SPaul Moore if (err) 4139220deb96SPaul Moore return err; 4140effad8dfSPaul Moore err = selinux_inet_sys_rcv_skb(skb->iif, addrp, family, 4141effad8dfSPaul Moore peer_sid, &ad); 4142effad8dfSPaul Moore if (err) 4143effad8dfSPaul Moore return err; 4144d621d35eSPaul Moore err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER, 4145d621d35eSPaul Moore PEER__RECV, &ad); 4146d621d35eSPaul Moore } 4147d621d35eSPaul Moore 4148effad8dfSPaul Moore if (selinux_secmark_enabled()) { 4149effad8dfSPaul Moore err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET, 4150effad8dfSPaul Moore PACKET__RECV, &ad); 4151effad8dfSPaul Moore if (err) 4152effad8dfSPaul Moore return err; 4153effad8dfSPaul Moore } 4154effad8dfSPaul Moore 4155d621d35eSPaul Moore return err; 41561da177e4SLinus Torvalds } 41571da177e4SLinus Torvalds 41582c7946a7SCatherine Zhang static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval, 41591da177e4SLinus Torvalds int __user *optlen, unsigned len) 41601da177e4SLinus Torvalds { 41611da177e4SLinus Torvalds int err = 0; 41621da177e4SLinus Torvalds char *scontext; 41631da177e4SLinus Torvalds u32 scontext_len; 41641da177e4SLinus Torvalds struct sk_security_struct *ssec; 41651da177e4SLinus Torvalds struct inode_security_struct *isec; 41663de4bab5SPaul Moore u32 peer_sid = SECSID_NULL; 41671da177e4SLinus Torvalds 41681da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 41692c7946a7SCatherine Zhang 41703de4bab5SPaul Moore if (isec->sclass == SECCLASS_UNIX_STREAM_SOCKET || 41713de4bab5SPaul Moore isec->sclass == SECCLASS_TCP_SOCKET) { 41726b877699SVenkat Yekkirala ssec = sock->sk->sk_security; 41736b877699SVenkat Yekkirala peer_sid = ssec->peer_sid; 41746b877699SVenkat Yekkirala } 41752c7946a7SCatherine Zhang if (peer_sid == SECSID_NULL) { 41762c7946a7SCatherine Zhang err = -ENOPROTOOPT; 41772c7946a7SCatherine Zhang goto out; 41782c7946a7SCatherine Zhang } 41791da177e4SLinus Torvalds 41802c7946a7SCatherine Zhang err = security_sid_to_context(peer_sid, &scontext, &scontext_len); 41811da177e4SLinus Torvalds 41821da177e4SLinus Torvalds if (err) 41831da177e4SLinus Torvalds goto out; 41841da177e4SLinus Torvalds 41851da177e4SLinus Torvalds if (scontext_len > len) { 41861da177e4SLinus Torvalds err = -ERANGE; 41871da177e4SLinus Torvalds goto out_len; 41881da177e4SLinus Torvalds } 41891da177e4SLinus Torvalds 41901da177e4SLinus Torvalds if (copy_to_user(optval, scontext, scontext_len)) 41911da177e4SLinus Torvalds err = -EFAULT; 41921da177e4SLinus Torvalds 41931da177e4SLinus Torvalds out_len: 41941da177e4SLinus Torvalds if (put_user(scontext_len, optlen)) 41951da177e4SLinus Torvalds err = -EFAULT; 41961da177e4SLinus Torvalds 41971da177e4SLinus Torvalds kfree(scontext); 41981da177e4SLinus Torvalds out: 41991da177e4SLinus Torvalds return err; 42001da177e4SLinus Torvalds } 42011da177e4SLinus Torvalds 4202dc49c1f9SCatherine Zhang static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid) 42032c7946a7SCatherine Zhang { 4204dc49c1f9SCatherine Zhang u32 peer_secid = SECSID_NULL; 420575e22910SPaul Moore u16 family; 4206877ce7c1SCatherine Zhang 420775e22910SPaul Moore if (sock) 420875e22910SPaul Moore family = sock->sk->sk_family; 420975e22910SPaul Moore else if (skb && skb->sk) 421075e22910SPaul Moore family = skb->sk->sk_family; 421175e22910SPaul Moore else 421275e22910SPaul Moore goto out; 421375e22910SPaul Moore 421475e22910SPaul Moore if (sock && family == PF_UNIX) 4215713a04aeSAhmed S. Darwish selinux_inode_getsecid(SOCK_INODE(sock), &peer_secid); 42163de4bab5SPaul Moore else if (skb) 4217220deb96SPaul Moore selinux_skb_peerlbl_sid(skb, family, &peer_secid); 42182c7946a7SCatherine Zhang 421975e22910SPaul Moore out: 4220dc49c1f9SCatherine Zhang *secid = peer_secid; 422175e22910SPaul Moore if (peer_secid == SECSID_NULL) 422275e22910SPaul Moore return -EINVAL; 422375e22910SPaul Moore return 0; 42242c7946a7SCatherine Zhang } 42252c7946a7SCatherine Zhang 42267d877f3bSAl Viro static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority) 42271da177e4SLinus Torvalds { 42281da177e4SLinus Torvalds return sk_alloc_security(sk, family, priority); 42291da177e4SLinus Torvalds } 42301da177e4SLinus Torvalds 42311da177e4SLinus Torvalds static void selinux_sk_free_security(struct sock *sk) 42321da177e4SLinus Torvalds { 42331da177e4SLinus Torvalds sk_free_security(sk); 42341da177e4SLinus Torvalds } 42351da177e4SLinus Torvalds 4236892c141eSVenkat Yekkirala static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk) 4237892c141eSVenkat Yekkirala { 4238892c141eSVenkat Yekkirala struct sk_security_struct *ssec = sk->sk_security; 4239892c141eSVenkat Yekkirala struct sk_security_struct *newssec = newsk->sk_security; 4240892c141eSVenkat Yekkirala 4241892c141eSVenkat Yekkirala newssec->sid = ssec->sid; 4242892c141eSVenkat Yekkirala newssec->peer_sid = ssec->peer_sid; 4243220deb96SPaul Moore newssec->sclass = ssec->sclass; 424499f59ed0SPaul Moore 4245f74af6e8SPaul Moore selinux_netlbl_sk_security_reset(newssec, newsk->sk_family); 4246892c141eSVenkat Yekkirala } 4247892c141eSVenkat Yekkirala 4248beb8d13bSVenkat Yekkirala static void selinux_sk_getsecid(struct sock *sk, u32 *secid) 4249d28d1e08STrent Jaeger { 4250d28d1e08STrent Jaeger if (!sk) 4251beb8d13bSVenkat Yekkirala *secid = SECINITSID_ANY_SOCKET; 4252892c141eSVenkat Yekkirala else { 4253892c141eSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 4254d28d1e08STrent Jaeger 4255beb8d13bSVenkat Yekkirala *secid = sksec->sid; 4256892c141eSVenkat Yekkirala } 4257d28d1e08STrent Jaeger } 4258d28d1e08STrent Jaeger 42599a673e56SAdrian Bunk static void selinux_sock_graft(struct sock *sk, struct socket *parent) 42604237c75cSVenkat Yekkirala { 42614237c75cSVenkat Yekkirala struct inode_security_struct *isec = SOCK_INODE(parent)->i_security; 42624237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 42634237c75cSVenkat Yekkirala 42642148ccc4SDavid Woodhouse if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 || 42652148ccc4SDavid Woodhouse sk->sk_family == PF_UNIX) 42664237c75cSVenkat Yekkirala isec->sid = sksec->sid; 4267220deb96SPaul Moore sksec->sclass = isec->sclass; 42687420ed23SVenkat Yekkirala 42697420ed23SVenkat Yekkirala selinux_netlbl_sock_graft(sk, parent); 42704237c75cSVenkat Yekkirala } 42714237c75cSVenkat Yekkirala 42729a673e56SAdrian Bunk static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb, 42734237c75cSVenkat Yekkirala struct request_sock *req) 42744237c75cSVenkat Yekkirala { 42754237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 42764237c75cSVenkat Yekkirala int err; 42777420ed23SVenkat Yekkirala u32 newsid; 42784237c75cSVenkat Yekkirala u32 peersid; 42794237c75cSVenkat Yekkirala 4280220deb96SPaul Moore err = selinux_skb_peerlbl_sid(skb, sk->sk_family, &peersid); 4281220deb96SPaul Moore if (err) 4282220deb96SPaul Moore return err; 4283a51c64f1SVenkat Yekkirala if (peersid == SECSID_NULL) { 4284a51c64f1SVenkat Yekkirala req->secid = sksec->sid; 42853de4bab5SPaul Moore req->peer_secid = SECSID_NULL; 4286a51c64f1SVenkat Yekkirala return 0; 4287a51c64f1SVenkat Yekkirala } 4288a51c64f1SVenkat Yekkirala 42894237c75cSVenkat Yekkirala err = security_sid_mls_copy(sksec->sid, peersid, &newsid); 42904237c75cSVenkat Yekkirala if (err) 42914237c75cSVenkat Yekkirala return err; 42924237c75cSVenkat Yekkirala 42934237c75cSVenkat Yekkirala req->secid = newsid; 42946b877699SVenkat Yekkirala req->peer_secid = peersid; 42954237c75cSVenkat Yekkirala return 0; 42964237c75cSVenkat Yekkirala } 42974237c75cSVenkat Yekkirala 42989a673e56SAdrian Bunk static void selinux_inet_csk_clone(struct sock *newsk, 42999a673e56SAdrian Bunk const struct request_sock *req) 43004237c75cSVenkat Yekkirala { 43014237c75cSVenkat Yekkirala struct sk_security_struct *newsksec = newsk->sk_security; 43024237c75cSVenkat Yekkirala 43034237c75cSVenkat Yekkirala newsksec->sid = req->secid; 43046b877699SVenkat Yekkirala newsksec->peer_sid = req->peer_secid; 43054237c75cSVenkat Yekkirala /* NOTE: Ideally, we should also get the isec->sid for the 43064237c75cSVenkat Yekkirala new socket in sync, but we don't have the isec available yet. 43074237c75cSVenkat Yekkirala So we will wait until sock_graft to do it, by which 43084237c75cSVenkat Yekkirala time it will have been created and available. */ 430999f59ed0SPaul Moore 43109f2ad665SPaul Moore /* We don't need to take any sort of lock here as we are the only 43119f2ad665SPaul Moore * thread with access to newsksec */ 43129f2ad665SPaul Moore selinux_netlbl_sk_security_reset(newsksec, req->rsk_ops->family); 43134237c75cSVenkat Yekkirala } 43144237c75cSVenkat Yekkirala 43156b877699SVenkat Yekkirala static void selinux_inet_conn_established(struct sock *sk, 43166b877699SVenkat Yekkirala struct sk_buff *skb) 43176b877699SVenkat Yekkirala { 43186b877699SVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 43196b877699SVenkat Yekkirala 4320220deb96SPaul Moore selinux_skb_peerlbl_sid(skb, sk->sk_family, &sksec->peer_sid); 43216b877699SVenkat Yekkirala } 43226b877699SVenkat Yekkirala 43239a673e56SAdrian Bunk static void selinux_req_classify_flow(const struct request_sock *req, 43249a673e56SAdrian Bunk struct flowi *fl) 43254237c75cSVenkat Yekkirala { 43264237c75cSVenkat Yekkirala fl->secid = req->secid; 43274237c75cSVenkat Yekkirala } 43284237c75cSVenkat Yekkirala 43291da177e4SLinus Torvalds static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb) 43301da177e4SLinus Torvalds { 43311da177e4SLinus Torvalds int err = 0; 43321da177e4SLinus Torvalds u32 perm; 43331da177e4SLinus Torvalds struct nlmsghdr *nlh; 43341da177e4SLinus Torvalds struct socket *sock = sk->sk_socket; 43351da177e4SLinus Torvalds struct inode_security_struct *isec = SOCK_INODE(sock)->i_security; 43361da177e4SLinus Torvalds 43371da177e4SLinus Torvalds if (skb->len < NLMSG_SPACE(0)) { 43381da177e4SLinus Torvalds err = -EINVAL; 43391da177e4SLinus Torvalds goto out; 43401da177e4SLinus Torvalds } 4341b529ccf2SArnaldo Carvalho de Melo nlh = nlmsg_hdr(skb); 43421da177e4SLinus Torvalds 43431da177e4SLinus Torvalds err = selinux_nlmsg_lookup(isec->sclass, nlh->nlmsg_type, &perm); 43441da177e4SLinus Torvalds if (err) { 43451da177e4SLinus Torvalds if (err == -EINVAL) { 43469ad9ad38SDavid Woodhouse audit_log(current->audit_context, GFP_KERNEL, AUDIT_SELINUX_ERR, 43471da177e4SLinus Torvalds "SELinux: unrecognized netlink message" 43481da177e4SLinus Torvalds " type=%hu for sclass=%hu\n", 43491da177e4SLinus Torvalds nlh->nlmsg_type, isec->sclass); 43501da177e4SLinus Torvalds if (!selinux_enforcing) 43511da177e4SLinus Torvalds err = 0; 43521da177e4SLinus Torvalds } 43531da177e4SLinus Torvalds 43541da177e4SLinus Torvalds /* Ignore */ 43551da177e4SLinus Torvalds if (err == -ENOENT) 43561da177e4SLinus Torvalds err = 0; 43571da177e4SLinus Torvalds goto out; 43581da177e4SLinus Torvalds } 43591da177e4SLinus Torvalds 43601da177e4SLinus Torvalds err = socket_has_perm(current, sock, perm); 43611da177e4SLinus Torvalds out: 43621da177e4SLinus Torvalds return err; 43631da177e4SLinus Torvalds } 43641da177e4SLinus Torvalds 43651da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER 43661da177e4SLinus Torvalds 4367effad8dfSPaul Moore static unsigned int selinux_ip_forward(struct sk_buff *skb, int ifindex, 4368effad8dfSPaul Moore u16 family) 43691da177e4SLinus Torvalds { 4370effad8dfSPaul Moore char *addrp; 4371effad8dfSPaul Moore u32 peer_sid; 4372effad8dfSPaul Moore struct avc_audit_data ad; 4373effad8dfSPaul Moore u8 secmark_active; 4374effad8dfSPaul Moore u8 peerlbl_active; 43754237c75cSVenkat Yekkirala 4376effad8dfSPaul Moore if (!selinux_policycap_netpeer) 4377effad8dfSPaul Moore return NF_ACCEPT; 43784237c75cSVenkat Yekkirala 4379effad8dfSPaul Moore secmark_active = selinux_secmark_enabled(); 4380effad8dfSPaul Moore peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled(); 4381effad8dfSPaul Moore if (!secmark_active && !peerlbl_active) 4382effad8dfSPaul Moore return NF_ACCEPT; 43834237c75cSVenkat Yekkirala 4384effad8dfSPaul Moore AVC_AUDIT_DATA_INIT(&ad, NET); 4385effad8dfSPaul Moore ad.u.net.netif = ifindex; 4386effad8dfSPaul Moore ad.u.net.family = family; 4387effad8dfSPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0) 4388effad8dfSPaul Moore return NF_DROP; 43891da177e4SLinus Torvalds 4390effad8dfSPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0) 4391effad8dfSPaul Moore return NF_DROP; 43921da177e4SLinus Torvalds 4393effad8dfSPaul Moore if (peerlbl_active) 4394effad8dfSPaul Moore if (selinux_inet_sys_rcv_skb(ifindex, addrp, family, 4395effad8dfSPaul Moore peer_sid, &ad) != 0) 4396effad8dfSPaul Moore return NF_DROP; 4397effad8dfSPaul Moore 4398effad8dfSPaul Moore if (secmark_active) 4399effad8dfSPaul Moore if (avc_has_perm(peer_sid, skb->secmark, 4400effad8dfSPaul Moore SECCLASS_PACKET, PACKET__FORWARD_IN, &ad)) 4401effad8dfSPaul Moore return NF_DROP; 4402effad8dfSPaul Moore 4403effad8dfSPaul Moore return NF_ACCEPT; 4404effad8dfSPaul Moore } 4405effad8dfSPaul Moore 4406effad8dfSPaul Moore static unsigned int selinux_ipv4_forward(unsigned int hooknum, 4407effad8dfSPaul Moore struct sk_buff *skb, 4408effad8dfSPaul Moore const struct net_device *in, 4409effad8dfSPaul Moore const struct net_device *out, 4410effad8dfSPaul Moore int (*okfn)(struct sk_buff *)) 4411effad8dfSPaul Moore { 4412effad8dfSPaul Moore return selinux_ip_forward(skb, in->ifindex, PF_INET); 4413effad8dfSPaul Moore } 4414effad8dfSPaul Moore 4415effad8dfSPaul Moore #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 4416effad8dfSPaul Moore static unsigned int selinux_ipv6_forward(unsigned int hooknum, 4417effad8dfSPaul Moore struct sk_buff *skb, 4418effad8dfSPaul Moore const struct net_device *in, 4419effad8dfSPaul Moore const struct net_device *out, 4420effad8dfSPaul Moore int (*okfn)(struct sk_buff *)) 4421effad8dfSPaul Moore { 4422effad8dfSPaul Moore return selinux_ip_forward(skb, in->ifindex, PF_INET6); 4423effad8dfSPaul Moore } 4424effad8dfSPaul Moore #endif /* IPV6 */ 4425effad8dfSPaul Moore 4426effad8dfSPaul Moore static int selinux_ip_postroute_iptables_compat(struct sock *sk, 4427effad8dfSPaul Moore int ifindex, 4428effad8dfSPaul Moore struct avc_audit_data *ad, 4429effad8dfSPaul Moore u16 family, char *addrp) 4430effad8dfSPaul Moore { 4431effad8dfSPaul Moore int err; 4432effad8dfSPaul Moore struct sk_security_struct *sksec = sk->sk_security; 4433effad8dfSPaul Moore u16 sk_class; 4434effad8dfSPaul Moore u32 netif_perm, node_perm, send_perm; 4435effad8dfSPaul Moore u32 port_sid, node_sid, if_sid, sk_sid; 4436effad8dfSPaul Moore 4437effad8dfSPaul Moore sk_sid = sksec->sid; 4438effad8dfSPaul Moore sk_class = sksec->sclass; 4439effad8dfSPaul Moore 4440effad8dfSPaul Moore switch (sk_class) { 44411da177e4SLinus Torvalds case SECCLASS_UDP_SOCKET: 44421da177e4SLinus Torvalds netif_perm = NETIF__UDP_SEND; 44431da177e4SLinus Torvalds node_perm = NODE__UDP_SEND; 44441da177e4SLinus Torvalds send_perm = UDP_SOCKET__SEND_MSG; 44451da177e4SLinus Torvalds break; 44461da177e4SLinus Torvalds case SECCLASS_TCP_SOCKET: 44471da177e4SLinus Torvalds netif_perm = NETIF__TCP_SEND; 44481da177e4SLinus Torvalds node_perm = NODE__TCP_SEND; 44491da177e4SLinus Torvalds send_perm = TCP_SOCKET__SEND_MSG; 44501da177e4SLinus Torvalds break; 44512ee92d46SJames Morris case SECCLASS_DCCP_SOCKET: 44522ee92d46SJames Morris netif_perm = NETIF__DCCP_SEND; 44532ee92d46SJames Morris node_perm = NODE__DCCP_SEND; 44542ee92d46SJames Morris send_perm = DCCP_SOCKET__SEND_MSG; 44552ee92d46SJames Morris break; 44561da177e4SLinus Torvalds default: 44571da177e4SLinus Torvalds netif_perm = NETIF__RAWIP_SEND; 44581da177e4SLinus Torvalds node_perm = NODE__RAWIP_SEND; 4459effad8dfSPaul Moore send_perm = 0; 44601da177e4SLinus Torvalds break; 44611da177e4SLinus Torvalds } 44621da177e4SLinus Torvalds 4463effad8dfSPaul Moore err = sel_netif_sid(ifindex, &if_sid); 44644e5ab4cbSJames Morris if (err) 4465effad8dfSPaul Moore return err; 4466effad8dfSPaul Moore err = avc_has_perm(sk_sid, if_sid, SECCLASS_NETIF, netif_perm, ad); 4467effad8dfSPaul Moore return err; 44681da177e4SLinus Torvalds 4469224dfbd8SPaul Moore err = sel_netnode_sid(addrp, family, &node_sid); 44704e5ab4cbSJames Morris if (err) 4471effad8dfSPaul Moore return err; 4472effad8dfSPaul Moore err = avc_has_perm(sk_sid, node_sid, SECCLASS_NODE, node_perm, ad); 44734e5ab4cbSJames Morris if (err) 4474effad8dfSPaul Moore return err; 44751da177e4SLinus Torvalds 4476effad8dfSPaul Moore if (send_perm != 0) 4477effad8dfSPaul Moore return 0; 44781da177e4SLinus Torvalds 44793e112172SPaul Moore err = sel_netport_sid(sk->sk_protocol, 44803e112172SPaul Moore ntohs(ad->u.net.dport), &port_sid); 448171f1cb05SPaul Moore if (unlikely(err)) { 448271f1cb05SPaul Moore printk(KERN_WARNING 448371f1cb05SPaul Moore "SELinux: failure in" 448471f1cb05SPaul Moore " selinux_ip_postroute_iptables_compat()," 448571f1cb05SPaul Moore " network port label not found\n"); 44864e5ab4cbSJames Morris return err; 448771f1cb05SPaul Moore } 4488effad8dfSPaul Moore return avc_has_perm(sk_sid, port_sid, sk_class, send_perm, ad); 44891da177e4SLinus Torvalds } 44901da177e4SLinus Torvalds 4491effad8dfSPaul Moore static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb, 4492effad8dfSPaul Moore int ifindex, 4493effad8dfSPaul Moore struct avc_audit_data *ad, 4494effad8dfSPaul Moore u16 family, 4495effad8dfSPaul Moore char *addrp, 4496effad8dfSPaul Moore u8 proto) 44974e5ab4cbSJames Morris { 4498effad8dfSPaul Moore struct sock *sk = skb->sk; 44994237c75cSVenkat Yekkirala struct sk_security_struct *sksec; 45004e5ab4cbSJames Morris 4501effad8dfSPaul Moore if (sk == NULL) 4502effad8dfSPaul Moore return NF_ACCEPT; 45034237c75cSVenkat Yekkirala sksec = sk->sk_security; 45044e5ab4cbSJames Morris 4505effad8dfSPaul Moore if (selinux_compat_net) { 4506effad8dfSPaul Moore if (selinux_ip_postroute_iptables_compat(skb->sk, ifindex, 4507effad8dfSPaul Moore ad, family, addrp)) 4508effad8dfSPaul Moore return NF_DROP; 4509effad8dfSPaul Moore } else { 4510effad8dfSPaul Moore if (avc_has_perm(sksec->sid, skb->secmark, 4511effad8dfSPaul Moore SECCLASS_PACKET, PACKET__SEND, ad)) 4512effad8dfSPaul Moore return NF_DROP; 45131da177e4SLinus Torvalds } 45141da177e4SLinus Torvalds 4515effad8dfSPaul Moore if (selinux_policycap_netpeer) 4516effad8dfSPaul Moore if (selinux_xfrm_postroute_last(sksec->sid, skb, ad, proto)) 4517effad8dfSPaul Moore return NF_DROP; 4518effad8dfSPaul Moore 4519effad8dfSPaul Moore return NF_ACCEPT; 4520effad8dfSPaul Moore } 4521effad8dfSPaul Moore 4522effad8dfSPaul Moore static unsigned int selinux_ip_postroute(struct sk_buff *skb, int ifindex, 4523effad8dfSPaul Moore u16 family) 4524effad8dfSPaul Moore { 4525effad8dfSPaul Moore u32 secmark_perm; 4526effad8dfSPaul Moore u32 peer_sid; 4527effad8dfSPaul Moore struct sock *sk; 4528effad8dfSPaul Moore struct avc_audit_data ad; 4529effad8dfSPaul Moore char *addrp; 4530effad8dfSPaul Moore u8 proto; 4531effad8dfSPaul Moore u8 secmark_active; 4532effad8dfSPaul Moore u8 peerlbl_active; 4533effad8dfSPaul Moore 4534effad8dfSPaul Moore AVC_AUDIT_DATA_INIT(&ad, NET); 4535effad8dfSPaul Moore ad.u.net.netif = ifindex; 4536effad8dfSPaul Moore ad.u.net.family = family; 4537effad8dfSPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto)) 4538effad8dfSPaul Moore return NF_DROP; 4539effad8dfSPaul Moore 4540effad8dfSPaul Moore /* If any sort of compatibility mode is enabled then handoff processing 4541effad8dfSPaul Moore * to the selinux_ip_postroute_compat() function to deal with the 4542effad8dfSPaul Moore * special handling. We do this in an attempt to keep this function 4543effad8dfSPaul Moore * as fast and as clean as possible. */ 4544effad8dfSPaul Moore if (selinux_compat_net || !selinux_policycap_netpeer) 4545effad8dfSPaul Moore return selinux_ip_postroute_compat(skb, ifindex, &ad, 4546effad8dfSPaul Moore family, addrp, proto); 4547effad8dfSPaul Moore 4548effad8dfSPaul Moore /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec 4549effad8dfSPaul Moore * packet transformation so allow the packet to pass without any checks 4550effad8dfSPaul Moore * since we'll have another chance to perform access control checks 4551effad8dfSPaul Moore * when the packet is on it's final way out. 4552effad8dfSPaul Moore * NOTE: there appear to be some IPv6 multicast cases where skb->dst 4553effad8dfSPaul Moore * is NULL, in this case go ahead and apply access control. */ 4554effad8dfSPaul Moore if (skb->dst != NULL && skb->dst->xfrm != NULL) 4555effad8dfSPaul Moore return NF_ACCEPT; 4556effad8dfSPaul Moore 4557effad8dfSPaul Moore secmark_active = selinux_secmark_enabled(); 4558effad8dfSPaul Moore peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled(); 4559effad8dfSPaul Moore if (!secmark_active && !peerlbl_active) 4560effad8dfSPaul Moore return NF_ACCEPT; 4561effad8dfSPaul Moore 4562effad8dfSPaul Moore /* if the packet is locally generated (skb->sk != NULL) then use the 4563effad8dfSPaul Moore * socket's label as the peer label, otherwise the packet is being 4564effad8dfSPaul Moore * forwarded through this system and we need to fetch the peer label 4565effad8dfSPaul Moore * directly from the packet */ 4566effad8dfSPaul Moore sk = skb->sk; 4567effad8dfSPaul Moore if (sk) { 4568effad8dfSPaul Moore struct sk_security_struct *sksec = sk->sk_security; 4569effad8dfSPaul Moore peer_sid = sksec->sid; 4570effad8dfSPaul Moore secmark_perm = PACKET__SEND; 4571effad8dfSPaul Moore } else { 4572effad8dfSPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &peer_sid)) 4573effad8dfSPaul Moore return NF_DROP; 4574effad8dfSPaul Moore secmark_perm = PACKET__FORWARD_OUT; 4575effad8dfSPaul Moore } 4576effad8dfSPaul Moore 4577effad8dfSPaul Moore if (secmark_active) 4578effad8dfSPaul Moore if (avc_has_perm(peer_sid, skb->secmark, 4579effad8dfSPaul Moore SECCLASS_PACKET, secmark_perm, &ad)) 4580effad8dfSPaul Moore return NF_DROP; 4581effad8dfSPaul Moore 4582effad8dfSPaul Moore if (peerlbl_active) { 4583effad8dfSPaul Moore u32 if_sid; 4584effad8dfSPaul Moore u32 node_sid; 4585effad8dfSPaul Moore 4586effad8dfSPaul Moore if (sel_netif_sid(ifindex, &if_sid)) 4587effad8dfSPaul Moore return NF_DROP; 4588effad8dfSPaul Moore if (avc_has_perm(peer_sid, if_sid, 4589effad8dfSPaul Moore SECCLASS_NETIF, NETIF__EGRESS, &ad)) 4590effad8dfSPaul Moore return NF_DROP; 4591effad8dfSPaul Moore 4592effad8dfSPaul Moore if (sel_netnode_sid(addrp, family, &node_sid)) 4593effad8dfSPaul Moore return NF_DROP; 4594effad8dfSPaul Moore if (avc_has_perm(peer_sid, node_sid, 4595effad8dfSPaul Moore SECCLASS_NODE, NODE__SENDTO, &ad)) 4596effad8dfSPaul Moore return NF_DROP; 4597effad8dfSPaul Moore } 4598effad8dfSPaul Moore 4599effad8dfSPaul Moore return NF_ACCEPT; 4600effad8dfSPaul Moore } 4601effad8dfSPaul Moore 4602effad8dfSPaul Moore static unsigned int selinux_ipv4_postroute(unsigned int hooknum, 4603a224be76SDavid S. Miller struct sk_buff *skb, 46041da177e4SLinus Torvalds const struct net_device *in, 46051da177e4SLinus Torvalds const struct net_device *out, 46061da177e4SLinus Torvalds int (*okfn)(struct sk_buff *)) 46071da177e4SLinus Torvalds { 4608effad8dfSPaul Moore return selinux_ip_postroute(skb, out->ifindex, PF_INET); 46091da177e4SLinus Torvalds } 46101da177e4SLinus Torvalds 46111da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 4612effad8dfSPaul Moore static unsigned int selinux_ipv6_postroute(unsigned int hooknum, 4613a224be76SDavid S. Miller struct sk_buff *skb, 46141da177e4SLinus Torvalds const struct net_device *in, 46151da177e4SLinus Torvalds const struct net_device *out, 46161da177e4SLinus Torvalds int (*okfn)(struct sk_buff *)) 46171da177e4SLinus Torvalds { 4618effad8dfSPaul Moore return selinux_ip_postroute(skb, out->ifindex, PF_INET6); 46191da177e4SLinus Torvalds } 46201da177e4SLinus Torvalds #endif /* IPV6 */ 46211da177e4SLinus Torvalds 46221da177e4SLinus Torvalds #endif /* CONFIG_NETFILTER */ 46231da177e4SLinus Torvalds 46241da177e4SLinus Torvalds static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb) 46251da177e4SLinus Torvalds { 46261da177e4SLinus Torvalds int err; 46271da177e4SLinus Torvalds 46281da177e4SLinus Torvalds err = secondary_ops->netlink_send(sk, skb); 46291da177e4SLinus Torvalds if (err) 46301da177e4SLinus Torvalds return err; 46311da177e4SLinus Torvalds 46321da177e4SLinus Torvalds if (policydb_loaded_version >= POLICYDB_VERSION_NLCLASS) 46331da177e4SLinus Torvalds err = selinux_nlmsg_perm(sk, skb); 46341da177e4SLinus Torvalds 46351da177e4SLinus Torvalds return err; 46361da177e4SLinus Torvalds } 46371da177e4SLinus Torvalds 4638c7bdb545SDarrel Goeddel static int selinux_netlink_recv(struct sk_buff *skb, int capability) 46391da177e4SLinus Torvalds { 4640c7bdb545SDarrel Goeddel int err; 4641c7bdb545SDarrel Goeddel struct avc_audit_data ad; 4642c7bdb545SDarrel Goeddel 4643c7bdb545SDarrel Goeddel err = secondary_ops->netlink_recv(skb, capability); 4644c7bdb545SDarrel Goeddel if (err) 4645c7bdb545SDarrel Goeddel return err; 4646c7bdb545SDarrel Goeddel 4647c7bdb545SDarrel Goeddel AVC_AUDIT_DATA_INIT(&ad, CAP); 4648c7bdb545SDarrel Goeddel ad.u.cap = capability; 4649c7bdb545SDarrel Goeddel 4650c7bdb545SDarrel Goeddel return avc_has_perm(NETLINK_CB(skb).sid, NETLINK_CB(skb).sid, 4651c7bdb545SDarrel Goeddel SECCLASS_CAPABILITY, CAP_TO_MASK(capability), &ad); 46521da177e4SLinus Torvalds } 46531da177e4SLinus Torvalds 46541da177e4SLinus Torvalds static int ipc_alloc_security(struct task_struct *task, 46551da177e4SLinus Torvalds struct kern_ipc_perm *perm, 46561da177e4SLinus Torvalds u16 sclass) 46571da177e4SLinus Torvalds { 46581da177e4SLinus Torvalds struct task_security_struct *tsec = task->security; 46591da177e4SLinus Torvalds struct ipc_security_struct *isec; 46601da177e4SLinus Torvalds 466189d155efSJames Morris isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL); 46621da177e4SLinus Torvalds if (!isec) 46631da177e4SLinus Torvalds return -ENOMEM; 46641da177e4SLinus Torvalds 46651da177e4SLinus Torvalds isec->sclass = sclass; 46661da177e4SLinus Torvalds isec->sid = tsec->sid; 46671da177e4SLinus Torvalds perm->security = isec; 46681da177e4SLinus Torvalds 46691da177e4SLinus Torvalds return 0; 46701da177e4SLinus Torvalds } 46711da177e4SLinus Torvalds 46721da177e4SLinus Torvalds static void ipc_free_security(struct kern_ipc_perm *perm) 46731da177e4SLinus Torvalds { 46741da177e4SLinus Torvalds struct ipc_security_struct *isec = perm->security; 46751da177e4SLinus Torvalds perm->security = NULL; 46761da177e4SLinus Torvalds kfree(isec); 46771da177e4SLinus Torvalds } 46781da177e4SLinus Torvalds 46791da177e4SLinus Torvalds static int msg_msg_alloc_security(struct msg_msg *msg) 46801da177e4SLinus Torvalds { 46811da177e4SLinus Torvalds struct msg_security_struct *msec; 46821da177e4SLinus Torvalds 468389d155efSJames Morris msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL); 46841da177e4SLinus Torvalds if (!msec) 46851da177e4SLinus Torvalds return -ENOMEM; 46861da177e4SLinus Torvalds 46871da177e4SLinus Torvalds msec->sid = SECINITSID_UNLABELED; 46881da177e4SLinus Torvalds msg->security = msec; 46891da177e4SLinus Torvalds 46901da177e4SLinus Torvalds return 0; 46911da177e4SLinus Torvalds } 46921da177e4SLinus Torvalds 46931da177e4SLinus Torvalds static void msg_msg_free_security(struct msg_msg *msg) 46941da177e4SLinus Torvalds { 46951da177e4SLinus Torvalds struct msg_security_struct *msec = msg->security; 46961da177e4SLinus Torvalds 46971da177e4SLinus Torvalds msg->security = NULL; 46981da177e4SLinus Torvalds kfree(msec); 46991da177e4SLinus Torvalds } 47001da177e4SLinus Torvalds 47011da177e4SLinus Torvalds static int ipc_has_perm(struct kern_ipc_perm *ipc_perms, 47026af963f1SStephen Smalley u32 perms) 47031da177e4SLinus Torvalds { 47041da177e4SLinus Torvalds struct task_security_struct *tsec; 47051da177e4SLinus Torvalds struct ipc_security_struct *isec; 47061da177e4SLinus Torvalds struct avc_audit_data ad; 47071da177e4SLinus Torvalds 47081da177e4SLinus Torvalds tsec = current->security; 47091da177e4SLinus Torvalds isec = ipc_perms->security; 47101da177e4SLinus Torvalds 47111da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 47121da177e4SLinus Torvalds ad.u.ipc_id = ipc_perms->key; 47131da177e4SLinus Torvalds 47146af963f1SStephen Smalley return avc_has_perm(tsec->sid, isec->sid, isec->sclass, perms, &ad); 47151da177e4SLinus Torvalds } 47161da177e4SLinus Torvalds 47171da177e4SLinus Torvalds static int selinux_msg_msg_alloc_security(struct msg_msg *msg) 47181da177e4SLinus Torvalds { 47191da177e4SLinus Torvalds return msg_msg_alloc_security(msg); 47201da177e4SLinus Torvalds } 47211da177e4SLinus Torvalds 47221da177e4SLinus Torvalds static void selinux_msg_msg_free_security(struct msg_msg *msg) 47231da177e4SLinus Torvalds { 47241da177e4SLinus Torvalds msg_msg_free_security(msg); 47251da177e4SLinus Torvalds } 47261da177e4SLinus Torvalds 47271da177e4SLinus Torvalds /* message queue security operations */ 47281da177e4SLinus Torvalds static int selinux_msg_queue_alloc_security(struct msg_queue *msq) 47291da177e4SLinus Torvalds { 47301da177e4SLinus Torvalds struct task_security_struct *tsec; 47311da177e4SLinus Torvalds struct ipc_security_struct *isec; 47321da177e4SLinus Torvalds struct avc_audit_data ad; 47331da177e4SLinus Torvalds int rc; 47341da177e4SLinus Torvalds 47351da177e4SLinus Torvalds rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ); 47361da177e4SLinus Torvalds if (rc) 47371da177e4SLinus Torvalds return rc; 47381da177e4SLinus Torvalds 47391da177e4SLinus Torvalds tsec = current->security; 47401da177e4SLinus Torvalds isec = msq->q_perm.security; 47411da177e4SLinus Torvalds 47421da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 47431da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 47441da177e4SLinus Torvalds 47451da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_MSGQ, 47461da177e4SLinus Torvalds MSGQ__CREATE, &ad); 47471da177e4SLinus Torvalds if (rc) { 47481da177e4SLinus Torvalds ipc_free_security(&msq->q_perm); 47491da177e4SLinus Torvalds return rc; 47501da177e4SLinus Torvalds } 47511da177e4SLinus Torvalds return 0; 47521da177e4SLinus Torvalds } 47531da177e4SLinus Torvalds 47541da177e4SLinus Torvalds static void selinux_msg_queue_free_security(struct msg_queue *msq) 47551da177e4SLinus Torvalds { 47561da177e4SLinus Torvalds ipc_free_security(&msq->q_perm); 47571da177e4SLinus Torvalds } 47581da177e4SLinus Torvalds 47591da177e4SLinus Torvalds static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg) 47601da177e4SLinus Torvalds { 47611da177e4SLinus Torvalds struct task_security_struct *tsec; 47621da177e4SLinus Torvalds struct ipc_security_struct *isec; 47631da177e4SLinus Torvalds struct avc_audit_data ad; 47641da177e4SLinus Torvalds 47651da177e4SLinus Torvalds tsec = current->security; 47661da177e4SLinus Torvalds isec = msq->q_perm.security; 47671da177e4SLinus Torvalds 47681da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 47691da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 47701da177e4SLinus Torvalds 47711da177e4SLinus Torvalds return avc_has_perm(tsec->sid, isec->sid, SECCLASS_MSGQ, 47721da177e4SLinus Torvalds MSGQ__ASSOCIATE, &ad); 47731da177e4SLinus Torvalds } 47741da177e4SLinus Torvalds 47751da177e4SLinus Torvalds static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd) 47761da177e4SLinus Torvalds { 47771da177e4SLinus Torvalds int err; 47781da177e4SLinus Torvalds int perms; 47791da177e4SLinus Torvalds 47801da177e4SLinus Torvalds switch (cmd) { 47811da177e4SLinus Torvalds case IPC_INFO: 47821da177e4SLinus Torvalds case MSG_INFO: 47831da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 47841da177e4SLinus Torvalds return task_has_system(current, SYSTEM__IPC_INFO); 47851da177e4SLinus Torvalds case IPC_STAT: 47861da177e4SLinus Torvalds case MSG_STAT: 47871da177e4SLinus Torvalds perms = MSGQ__GETATTR | MSGQ__ASSOCIATE; 47881da177e4SLinus Torvalds break; 47891da177e4SLinus Torvalds case IPC_SET: 47901da177e4SLinus Torvalds perms = MSGQ__SETATTR; 47911da177e4SLinus Torvalds break; 47921da177e4SLinus Torvalds case IPC_RMID: 47931da177e4SLinus Torvalds perms = MSGQ__DESTROY; 47941da177e4SLinus Torvalds break; 47951da177e4SLinus Torvalds default: 47961da177e4SLinus Torvalds return 0; 47971da177e4SLinus Torvalds } 47981da177e4SLinus Torvalds 47996af963f1SStephen Smalley err = ipc_has_perm(&msq->q_perm, perms); 48001da177e4SLinus Torvalds return err; 48011da177e4SLinus Torvalds } 48021da177e4SLinus Torvalds 48031da177e4SLinus Torvalds static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg) 48041da177e4SLinus Torvalds { 48051da177e4SLinus Torvalds struct task_security_struct *tsec; 48061da177e4SLinus Torvalds struct ipc_security_struct *isec; 48071da177e4SLinus Torvalds struct msg_security_struct *msec; 48081da177e4SLinus Torvalds struct avc_audit_data ad; 48091da177e4SLinus Torvalds int rc; 48101da177e4SLinus Torvalds 48111da177e4SLinus Torvalds tsec = current->security; 48121da177e4SLinus Torvalds isec = msq->q_perm.security; 48131da177e4SLinus Torvalds msec = msg->security; 48141da177e4SLinus Torvalds 48151da177e4SLinus Torvalds /* 48161da177e4SLinus Torvalds * First time through, need to assign label to the message 48171da177e4SLinus Torvalds */ 48181da177e4SLinus Torvalds if (msec->sid == SECINITSID_UNLABELED) { 48191da177e4SLinus Torvalds /* 48201da177e4SLinus Torvalds * Compute new sid based on current process and 48211da177e4SLinus Torvalds * message queue this message will be stored in 48221da177e4SLinus Torvalds */ 48231da177e4SLinus Torvalds rc = security_transition_sid(tsec->sid, 48241da177e4SLinus Torvalds isec->sid, 48251da177e4SLinus Torvalds SECCLASS_MSG, 48261da177e4SLinus Torvalds &msec->sid); 48271da177e4SLinus Torvalds if (rc) 48281da177e4SLinus Torvalds return rc; 48291da177e4SLinus Torvalds } 48301da177e4SLinus Torvalds 48311da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 48321da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 48331da177e4SLinus Torvalds 48341da177e4SLinus Torvalds /* Can this process write to the queue? */ 48351da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_MSGQ, 48361da177e4SLinus Torvalds MSGQ__WRITE, &ad); 48371da177e4SLinus Torvalds if (!rc) 48381da177e4SLinus Torvalds /* Can this process send the message */ 48391da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, msec->sid, 48401da177e4SLinus Torvalds SECCLASS_MSG, MSG__SEND, &ad); 48411da177e4SLinus Torvalds if (!rc) 48421da177e4SLinus Torvalds /* Can the message be put in the queue? */ 48431da177e4SLinus Torvalds rc = avc_has_perm(msec->sid, isec->sid, 48441da177e4SLinus Torvalds SECCLASS_MSGQ, MSGQ__ENQUEUE, &ad); 48451da177e4SLinus Torvalds 48461da177e4SLinus Torvalds return rc; 48471da177e4SLinus Torvalds } 48481da177e4SLinus Torvalds 48491da177e4SLinus Torvalds static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg, 48501da177e4SLinus Torvalds struct task_struct *target, 48511da177e4SLinus Torvalds long type, int mode) 48521da177e4SLinus Torvalds { 48531da177e4SLinus Torvalds struct task_security_struct *tsec; 48541da177e4SLinus Torvalds struct ipc_security_struct *isec; 48551da177e4SLinus Torvalds struct msg_security_struct *msec; 48561da177e4SLinus Torvalds struct avc_audit_data ad; 48571da177e4SLinus Torvalds int rc; 48581da177e4SLinus Torvalds 48591da177e4SLinus Torvalds tsec = target->security; 48601da177e4SLinus Torvalds isec = msq->q_perm.security; 48611da177e4SLinus Torvalds msec = msg->security; 48621da177e4SLinus Torvalds 48631da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 48641da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 48651da177e4SLinus Torvalds 48661da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, 48671da177e4SLinus Torvalds SECCLASS_MSGQ, MSGQ__READ, &ad); 48681da177e4SLinus Torvalds if (!rc) 48691da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, msec->sid, 48701da177e4SLinus Torvalds SECCLASS_MSG, MSG__RECEIVE, &ad); 48711da177e4SLinus Torvalds return rc; 48721da177e4SLinus Torvalds } 48731da177e4SLinus Torvalds 48741da177e4SLinus Torvalds /* Shared Memory security operations */ 48751da177e4SLinus Torvalds static int selinux_shm_alloc_security(struct shmid_kernel *shp) 48761da177e4SLinus Torvalds { 48771da177e4SLinus Torvalds struct task_security_struct *tsec; 48781da177e4SLinus Torvalds struct ipc_security_struct *isec; 48791da177e4SLinus Torvalds struct avc_audit_data ad; 48801da177e4SLinus Torvalds int rc; 48811da177e4SLinus Torvalds 48821da177e4SLinus Torvalds rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM); 48831da177e4SLinus Torvalds if (rc) 48841da177e4SLinus Torvalds return rc; 48851da177e4SLinus Torvalds 48861da177e4SLinus Torvalds tsec = current->security; 48871da177e4SLinus Torvalds isec = shp->shm_perm.security; 48881da177e4SLinus Torvalds 48891da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 48901da177e4SLinus Torvalds ad.u.ipc_id = shp->shm_perm.key; 48911da177e4SLinus Torvalds 48921da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_SHM, 48931da177e4SLinus Torvalds SHM__CREATE, &ad); 48941da177e4SLinus Torvalds if (rc) { 48951da177e4SLinus Torvalds ipc_free_security(&shp->shm_perm); 48961da177e4SLinus Torvalds return rc; 48971da177e4SLinus Torvalds } 48981da177e4SLinus Torvalds return 0; 48991da177e4SLinus Torvalds } 49001da177e4SLinus Torvalds 49011da177e4SLinus Torvalds static void selinux_shm_free_security(struct shmid_kernel *shp) 49021da177e4SLinus Torvalds { 49031da177e4SLinus Torvalds ipc_free_security(&shp->shm_perm); 49041da177e4SLinus Torvalds } 49051da177e4SLinus Torvalds 49061da177e4SLinus Torvalds static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg) 49071da177e4SLinus Torvalds { 49081da177e4SLinus Torvalds struct task_security_struct *tsec; 49091da177e4SLinus Torvalds struct ipc_security_struct *isec; 49101da177e4SLinus Torvalds struct avc_audit_data ad; 49111da177e4SLinus Torvalds 49121da177e4SLinus Torvalds tsec = current->security; 49131da177e4SLinus Torvalds isec = shp->shm_perm.security; 49141da177e4SLinus Torvalds 49151da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 49161da177e4SLinus Torvalds ad.u.ipc_id = shp->shm_perm.key; 49171da177e4SLinus Torvalds 49181da177e4SLinus Torvalds return avc_has_perm(tsec->sid, isec->sid, SECCLASS_SHM, 49191da177e4SLinus Torvalds SHM__ASSOCIATE, &ad); 49201da177e4SLinus Torvalds } 49211da177e4SLinus Torvalds 49221da177e4SLinus Torvalds /* Note, at this point, shp is locked down */ 49231da177e4SLinus Torvalds static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd) 49241da177e4SLinus Torvalds { 49251da177e4SLinus Torvalds int perms; 49261da177e4SLinus Torvalds int err; 49271da177e4SLinus Torvalds 49281da177e4SLinus Torvalds switch (cmd) { 49291da177e4SLinus Torvalds case IPC_INFO: 49301da177e4SLinus Torvalds case SHM_INFO: 49311da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 49321da177e4SLinus Torvalds return task_has_system(current, SYSTEM__IPC_INFO); 49331da177e4SLinus Torvalds case IPC_STAT: 49341da177e4SLinus Torvalds case SHM_STAT: 49351da177e4SLinus Torvalds perms = SHM__GETATTR | SHM__ASSOCIATE; 49361da177e4SLinus Torvalds break; 49371da177e4SLinus Torvalds case IPC_SET: 49381da177e4SLinus Torvalds perms = SHM__SETATTR; 49391da177e4SLinus Torvalds break; 49401da177e4SLinus Torvalds case SHM_LOCK: 49411da177e4SLinus Torvalds case SHM_UNLOCK: 49421da177e4SLinus Torvalds perms = SHM__LOCK; 49431da177e4SLinus Torvalds break; 49441da177e4SLinus Torvalds case IPC_RMID: 49451da177e4SLinus Torvalds perms = SHM__DESTROY; 49461da177e4SLinus Torvalds break; 49471da177e4SLinus Torvalds default: 49481da177e4SLinus Torvalds return 0; 49491da177e4SLinus Torvalds } 49501da177e4SLinus Torvalds 49516af963f1SStephen Smalley err = ipc_has_perm(&shp->shm_perm, perms); 49521da177e4SLinus Torvalds return err; 49531da177e4SLinus Torvalds } 49541da177e4SLinus Torvalds 49551da177e4SLinus Torvalds static int selinux_shm_shmat(struct shmid_kernel *shp, 49561da177e4SLinus Torvalds char __user *shmaddr, int shmflg) 49571da177e4SLinus Torvalds { 49581da177e4SLinus Torvalds u32 perms; 49591da177e4SLinus Torvalds int rc; 49601da177e4SLinus Torvalds 49611da177e4SLinus Torvalds rc = secondary_ops->shm_shmat(shp, shmaddr, shmflg); 49621da177e4SLinus Torvalds if (rc) 49631da177e4SLinus Torvalds return rc; 49641da177e4SLinus Torvalds 49651da177e4SLinus Torvalds if (shmflg & SHM_RDONLY) 49661da177e4SLinus Torvalds perms = SHM__READ; 49671da177e4SLinus Torvalds else 49681da177e4SLinus Torvalds perms = SHM__READ | SHM__WRITE; 49691da177e4SLinus Torvalds 49706af963f1SStephen Smalley return ipc_has_perm(&shp->shm_perm, perms); 49711da177e4SLinus Torvalds } 49721da177e4SLinus Torvalds 49731da177e4SLinus Torvalds /* Semaphore security operations */ 49741da177e4SLinus Torvalds static int selinux_sem_alloc_security(struct sem_array *sma) 49751da177e4SLinus Torvalds { 49761da177e4SLinus Torvalds struct task_security_struct *tsec; 49771da177e4SLinus Torvalds struct ipc_security_struct *isec; 49781da177e4SLinus Torvalds struct avc_audit_data ad; 49791da177e4SLinus Torvalds int rc; 49801da177e4SLinus Torvalds 49811da177e4SLinus Torvalds rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM); 49821da177e4SLinus Torvalds if (rc) 49831da177e4SLinus Torvalds return rc; 49841da177e4SLinus Torvalds 49851da177e4SLinus Torvalds tsec = current->security; 49861da177e4SLinus Torvalds isec = sma->sem_perm.security; 49871da177e4SLinus Torvalds 49881da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 49891da177e4SLinus Torvalds ad.u.ipc_id = sma->sem_perm.key; 49901da177e4SLinus Torvalds 49911da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_SEM, 49921da177e4SLinus Torvalds SEM__CREATE, &ad); 49931da177e4SLinus Torvalds if (rc) { 49941da177e4SLinus Torvalds ipc_free_security(&sma->sem_perm); 49951da177e4SLinus Torvalds return rc; 49961da177e4SLinus Torvalds } 49971da177e4SLinus Torvalds return 0; 49981da177e4SLinus Torvalds } 49991da177e4SLinus Torvalds 50001da177e4SLinus Torvalds static void selinux_sem_free_security(struct sem_array *sma) 50011da177e4SLinus Torvalds { 50021da177e4SLinus Torvalds ipc_free_security(&sma->sem_perm); 50031da177e4SLinus Torvalds } 50041da177e4SLinus Torvalds 50051da177e4SLinus Torvalds static int selinux_sem_associate(struct sem_array *sma, int semflg) 50061da177e4SLinus Torvalds { 50071da177e4SLinus Torvalds struct task_security_struct *tsec; 50081da177e4SLinus Torvalds struct ipc_security_struct *isec; 50091da177e4SLinus Torvalds struct avc_audit_data ad; 50101da177e4SLinus Torvalds 50111da177e4SLinus Torvalds tsec = current->security; 50121da177e4SLinus Torvalds isec = sma->sem_perm.security; 50131da177e4SLinus Torvalds 50141da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 50151da177e4SLinus Torvalds ad.u.ipc_id = sma->sem_perm.key; 50161da177e4SLinus Torvalds 50171da177e4SLinus Torvalds return avc_has_perm(tsec->sid, isec->sid, SECCLASS_SEM, 50181da177e4SLinus Torvalds SEM__ASSOCIATE, &ad); 50191da177e4SLinus Torvalds } 50201da177e4SLinus Torvalds 50211da177e4SLinus Torvalds /* Note, at this point, sma is locked down */ 50221da177e4SLinus Torvalds static int selinux_sem_semctl(struct sem_array *sma, int cmd) 50231da177e4SLinus Torvalds { 50241da177e4SLinus Torvalds int err; 50251da177e4SLinus Torvalds u32 perms; 50261da177e4SLinus Torvalds 50271da177e4SLinus Torvalds switch (cmd) { 50281da177e4SLinus Torvalds case IPC_INFO: 50291da177e4SLinus Torvalds case SEM_INFO: 50301da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 50311da177e4SLinus Torvalds return task_has_system(current, SYSTEM__IPC_INFO); 50321da177e4SLinus Torvalds case GETPID: 50331da177e4SLinus Torvalds case GETNCNT: 50341da177e4SLinus Torvalds case GETZCNT: 50351da177e4SLinus Torvalds perms = SEM__GETATTR; 50361da177e4SLinus Torvalds break; 50371da177e4SLinus Torvalds case GETVAL: 50381da177e4SLinus Torvalds case GETALL: 50391da177e4SLinus Torvalds perms = SEM__READ; 50401da177e4SLinus Torvalds break; 50411da177e4SLinus Torvalds case SETVAL: 50421da177e4SLinus Torvalds case SETALL: 50431da177e4SLinus Torvalds perms = SEM__WRITE; 50441da177e4SLinus Torvalds break; 50451da177e4SLinus Torvalds case IPC_RMID: 50461da177e4SLinus Torvalds perms = SEM__DESTROY; 50471da177e4SLinus Torvalds break; 50481da177e4SLinus Torvalds case IPC_SET: 50491da177e4SLinus Torvalds perms = SEM__SETATTR; 50501da177e4SLinus Torvalds break; 50511da177e4SLinus Torvalds case IPC_STAT: 50521da177e4SLinus Torvalds case SEM_STAT: 50531da177e4SLinus Torvalds perms = SEM__GETATTR | SEM__ASSOCIATE; 50541da177e4SLinus Torvalds break; 50551da177e4SLinus Torvalds default: 50561da177e4SLinus Torvalds return 0; 50571da177e4SLinus Torvalds } 50581da177e4SLinus Torvalds 50596af963f1SStephen Smalley err = ipc_has_perm(&sma->sem_perm, perms); 50601da177e4SLinus Torvalds return err; 50611da177e4SLinus Torvalds } 50621da177e4SLinus Torvalds 50631da177e4SLinus Torvalds static int selinux_sem_semop(struct sem_array *sma, 50641da177e4SLinus Torvalds struct sembuf *sops, unsigned nsops, int alter) 50651da177e4SLinus Torvalds { 50661da177e4SLinus Torvalds u32 perms; 50671da177e4SLinus Torvalds 50681da177e4SLinus Torvalds if (alter) 50691da177e4SLinus Torvalds perms = SEM__READ | SEM__WRITE; 50701da177e4SLinus Torvalds else 50711da177e4SLinus Torvalds perms = SEM__READ; 50721da177e4SLinus Torvalds 50736af963f1SStephen Smalley return ipc_has_perm(&sma->sem_perm, perms); 50741da177e4SLinus Torvalds } 50751da177e4SLinus Torvalds 50761da177e4SLinus Torvalds static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag) 50771da177e4SLinus Torvalds { 50781da177e4SLinus Torvalds u32 av = 0; 50791da177e4SLinus Torvalds 50801da177e4SLinus Torvalds av = 0; 50811da177e4SLinus Torvalds if (flag & S_IRUGO) 50821da177e4SLinus Torvalds av |= IPC__UNIX_READ; 50831da177e4SLinus Torvalds if (flag & S_IWUGO) 50841da177e4SLinus Torvalds av |= IPC__UNIX_WRITE; 50851da177e4SLinus Torvalds 50861da177e4SLinus Torvalds if (av == 0) 50871da177e4SLinus Torvalds return 0; 50881da177e4SLinus Torvalds 50896af963f1SStephen Smalley return ipc_has_perm(ipcp, av); 50901da177e4SLinus Torvalds } 50911da177e4SLinus Torvalds 5092713a04aeSAhmed S. Darwish static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid) 5093713a04aeSAhmed S. Darwish { 5094713a04aeSAhmed S. Darwish struct ipc_security_struct *isec = ipcp->security; 5095713a04aeSAhmed S. Darwish *secid = isec->sid; 5096713a04aeSAhmed S. Darwish } 5097713a04aeSAhmed S. Darwish 50981da177e4SLinus Torvalds static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode) 50991da177e4SLinus Torvalds { 51001da177e4SLinus Torvalds if (inode) 51011da177e4SLinus Torvalds inode_doinit_with_dentry(inode, dentry); 51021da177e4SLinus Torvalds } 51031da177e4SLinus Torvalds 51041da177e4SLinus Torvalds static int selinux_getprocattr(struct task_struct *p, 510504ff9708SAl Viro char *name, char **value) 51061da177e4SLinus Torvalds { 51071da177e4SLinus Torvalds struct task_security_struct *tsec; 51088c8570fbSDustin Kirkland u32 sid; 51091da177e4SLinus Torvalds int error; 511004ff9708SAl Viro unsigned len; 51111da177e4SLinus Torvalds 51121da177e4SLinus Torvalds if (current != p) { 51131da177e4SLinus Torvalds error = task_has_perm(current, p, PROCESS__GETATTR); 51141da177e4SLinus Torvalds if (error) 51151da177e4SLinus Torvalds return error; 51161da177e4SLinus Torvalds } 51171da177e4SLinus Torvalds 51181da177e4SLinus Torvalds tsec = p->security; 51191da177e4SLinus Torvalds 51201da177e4SLinus Torvalds if (!strcmp(name, "current")) 51211da177e4SLinus Torvalds sid = tsec->sid; 51221da177e4SLinus Torvalds else if (!strcmp(name, "prev")) 51231da177e4SLinus Torvalds sid = tsec->osid; 51241da177e4SLinus Torvalds else if (!strcmp(name, "exec")) 51251da177e4SLinus Torvalds sid = tsec->exec_sid; 51261da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 51271da177e4SLinus Torvalds sid = tsec->create_sid; 51284eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) 51294eb582cfSMichael LeMay sid = tsec->keycreate_sid; 513042c3e03eSEric Paris else if (!strcmp(name, "sockcreate")) 513142c3e03eSEric Paris sid = tsec->sockcreate_sid; 51321da177e4SLinus Torvalds else 51331da177e4SLinus Torvalds return -EINVAL; 51341da177e4SLinus Torvalds 51351da177e4SLinus Torvalds if (!sid) 51361da177e4SLinus Torvalds return 0; 51371da177e4SLinus Torvalds 513804ff9708SAl Viro error = security_sid_to_context(sid, value, &len); 513904ff9708SAl Viro if (error) 514004ff9708SAl Viro return error; 514104ff9708SAl Viro return len; 51421da177e4SLinus Torvalds } 51431da177e4SLinus Torvalds 51441da177e4SLinus Torvalds static int selinux_setprocattr(struct task_struct *p, 51451da177e4SLinus Torvalds char *name, void *value, size_t size) 51461da177e4SLinus Torvalds { 51471da177e4SLinus Torvalds struct task_security_struct *tsec; 51480356357cSRoland McGrath struct task_struct *tracer; 51491da177e4SLinus Torvalds u32 sid = 0; 51501da177e4SLinus Torvalds int error; 51511da177e4SLinus Torvalds char *str = value; 51521da177e4SLinus Torvalds 51531da177e4SLinus Torvalds if (current != p) { 51541da177e4SLinus Torvalds /* SELinux only allows a process to change its own 51551da177e4SLinus Torvalds security attributes. */ 51561da177e4SLinus Torvalds return -EACCES; 51571da177e4SLinus Torvalds } 51581da177e4SLinus Torvalds 51591da177e4SLinus Torvalds /* 51601da177e4SLinus Torvalds * Basic control over ability to set these attributes at all. 51611da177e4SLinus Torvalds * current == p, but we'll pass them separately in case the 51621da177e4SLinus Torvalds * above restriction is ever removed. 51631da177e4SLinus Torvalds */ 51641da177e4SLinus Torvalds if (!strcmp(name, "exec")) 51651da177e4SLinus Torvalds error = task_has_perm(current, p, PROCESS__SETEXEC); 51661da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 51671da177e4SLinus Torvalds error = task_has_perm(current, p, PROCESS__SETFSCREATE); 51684eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) 51694eb582cfSMichael LeMay error = task_has_perm(current, p, PROCESS__SETKEYCREATE); 517042c3e03eSEric Paris else if (!strcmp(name, "sockcreate")) 517142c3e03eSEric Paris error = task_has_perm(current, p, PROCESS__SETSOCKCREATE); 51721da177e4SLinus Torvalds else if (!strcmp(name, "current")) 51731da177e4SLinus Torvalds error = task_has_perm(current, p, PROCESS__SETCURRENT); 51741da177e4SLinus Torvalds else 51751da177e4SLinus Torvalds error = -EINVAL; 51761da177e4SLinus Torvalds if (error) 51771da177e4SLinus Torvalds return error; 51781da177e4SLinus Torvalds 51791da177e4SLinus Torvalds /* Obtain a SID for the context, if one was specified. */ 51801da177e4SLinus Torvalds if (size && str[1] && str[1] != '\n') { 51811da177e4SLinus Torvalds if (str[size-1] == '\n') { 51821da177e4SLinus Torvalds str[size-1] = 0; 51831da177e4SLinus Torvalds size--; 51841da177e4SLinus Torvalds } 51851da177e4SLinus Torvalds error = security_context_to_sid(value, size, &sid); 518612b29f34SStephen Smalley if (error == -EINVAL && !strcmp(name, "fscreate")) { 518712b29f34SStephen Smalley if (!capable(CAP_MAC_ADMIN)) 518812b29f34SStephen Smalley return error; 518912b29f34SStephen Smalley error = security_context_to_sid_force(value, size, 519012b29f34SStephen Smalley &sid); 519112b29f34SStephen Smalley } 51921da177e4SLinus Torvalds if (error) 51931da177e4SLinus Torvalds return error; 51941da177e4SLinus Torvalds } 51951da177e4SLinus Torvalds 51961da177e4SLinus Torvalds /* Permission checking based on the specified context is 51971da177e4SLinus Torvalds performed during the actual operation (execve, 51981da177e4SLinus Torvalds open/mkdir/...), when we know the full context of the 51991da177e4SLinus Torvalds operation. See selinux_bprm_set_security for the execve 52001da177e4SLinus Torvalds checks and may_create for the file creation checks. The 52011da177e4SLinus Torvalds operation will then fail if the context is not permitted. */ 52021da177e4SLinus Torvalds tsec = p->security; 52031da177e4SLinus Torvalds if (!strcmp(name, "exec")) 52041da177e4SLinus Torvalds tsec->exec_sid = sid; 52051da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 52061da177e4SLinus Torvalds tsec->create_sid = sid; 52074eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) { 52084eb582cfSMichael LeMay error = may_create_key(sid, p); 52094eb582cfSMichael LeMay if (error) 52104eb582cfSMichael LeMay return error; 52114eb582cfSMichael LeMay tsec->keycreate_sid = sid; 521242c3e03eSEric Paris } else if (!strcmp(name, "sockcreate")) 521342c3e03eSEric Paris tsec->sockcreate_sid = sid; 521442c3e03eSEric Paris else if (!strcmp(name, "current")) { 52151da177e4SLinus Torvalds struct av_decision avd; 52161da177e4SLinus Torvalds 52171da177e4SLinus Torvalds if (sid == 0) 52181da177e4SLinus Torvalds return -EINVAL; 52191da177e4SLinus Torvalds 52201da177e4SLinus Torvalds /* Only allow single threaded processes to change context */ 52211da177e4SLinus Torvalds if (atomic_read(&p->mm->mm_users) != 1) { 52221da177e4SLinus Torvalds struct task_struct *g, *t; 52231da177e4SLinus Torvalds struct mm_struct *mm = p->mm; 52241da177e4SLinus Torvalds read_lock(&tasklist_lock); 52252baf06dfSJames Morris do_each_thread(g, t) { 52261da177e4SLinus Torvalds if (t->mm == mm && t != p) { 52271da177e4SLinus Torvalds read_unlock(&tasklist_lock); 52281da177e4SLinus Torvalds return -EPERM; 52291da177e4SLinus Torvalds } 52302baf06dfSJames Morris } while_each_thread(g, t); 52311da177e4SLinus Torvalds read_unlock(&tasklist_lock); 52321da177e4SLinus Torvalds } 52331da177e4SLinus Torvalds 52341da177e4SLinus Torvalds /* Check permissions for the transition. */ 52351da177e4SLinus Torvalds error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS, 52361da177e4SLinus Torvalds PROCESS__DYNTRANSITION, NULL); 52371da177e4SLinus Torvalds if (error) 52381da177e4SLinus Torvalds return error; 52391da177e4SLinus Torvalds 52401da177e4SLinus Torvalds /* Check for ptracing, and update the task SID if ok. 52411da177e4SLinus Torvalds Otherwise, leave SID unchanged and fail. */ 52421da177e4SLinus Torvalds task_lock(p); 52430356357cSRoland McGrath rcu_read_lock(); 52440d094efeSRoland McGrath tracer = tracehook_tracer_task(p); 52450356357cSRoland McGrath if (tracer != NULL) { 52460356357cSRoland McGrath struct task_security_struct *ptsec = tracer->security; 52470356357cSRoland McGrath u32 ptsid = ptsec->sid; 52480356357cSRoland McGrath rcu_read_unlock(); 52490356357cSRoland McGrath error = avc_has_perm_noaudit(ptsid, sid, 52501da177e4SLinus Torvalds SECCLASS_PROCESS, 52512c3c05dbSStephen Smalley PROCESS__PTRACE, 0, &avd); 52521da177e4SLinus Torvalds if (!error) 52531da177e4SLinus Torvalds tsec->sid = sid; 52541da177e4SLinus Torvalds task_unlock(p); 52550356357cSRoland McGrath avc_audit(ptsid, sid, SECCLASS_PROCESS, 52561da177e4SLinus Torvalds PROCESS__PTRACE, &avd, error, NULL); 52571da177e4SLinus Torvalds if (error) 52581da177e4SLinus Torvalds return error; 52591da177e4SLinus Torvalds } else { 52600356357cSRoland McGrath rcu_read_unlock(); 52611da177e4SLinus Torvalds tsec->sid = sid; 52621da177e4SLinus Torvalds task_unlock(p); 52631da177e4SLinus Torvalds } 5264828dfe1dSEric Paris } else 52651da177e4SLinus Torvalds return -EINVAL; 52661da177e4SLinus Torvalds 52671da177e4SLinus Torvalds return size; 52681da177e4SLinus Torvalds } 52691da177e4SLinus Torvalds 5270dc49c1f9SCatherine Zhang static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) 5271dc49c1f9SCatherine Zhang { 5272dc49c1f9SCatherine Zhang return security_sid_to_context(secid, secdata, seclen); 5273dc49c1f9SCatherine Zhang } 5274dc49c1f9SCatherine Zhang 52757bf570dcSDavid Howells static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid) 527663cb3449SDavid Howells { 527763cb3449SDavid Howells return security_context_to_sid(secdata, seclen, secid); 527863cb3449SDavid Howells } 527963cb3449SDavid Howells 5280dc49c1f9SCatherine Zhang static void selinux_release_secctx(char *secdata, u32 seclen) 5281dc49c1f9SCatherine Zhang { 5282dc49c1f9SCatherine Zhang kfree(secdata); 5283dc49c1f9SCatherine Zhang } 5284dc49c1f9SCatherine Zhang 5285d720024eSMichael LeMay #ifdef CONFIG_KEYS 5286d720024eSMichael LeMay 52877e047ef5SDavid Howells static int selinux_key_alloc(struct key *k, struct task_struct *tsk, 52887e047ef5SDavid Howells unsigned long flags) 5289d720024eSMichael LeMay { 5290d720024eSMichael LeMay struct task_security_struct *tsec = tsk->security; 5291d720024eSMichael LeMay struct key_security_struct *ksec; 5292d720024eSMichael LeMay 5293d720024eSMichael LeMay ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL); 5294d720024eSMichael LeMay if (!ksec) 5295d720024eSMichael LeMay return -ENOMEM; 5296d720024eSMichael LeMay 52974eb582cfSMichael LeMay if (tsec->keycreate_sid) 52984eb582cfSMichael LeMay ksec->sid = tsec->keycreate_sid; 52994eb582cfSMichael LeMay else 5300d720024eSMichael LeMay ksec->sid = tsec->sid; 5301d720024eSMichael LeMay k->security = ksec; 5302d720024eSMichael LeMay 5303d720024eSMichael LeMay return 0; 5304d720024eSMichael LeMay } 5305d720024eSMichael LeMay 5306d720024eSMichael LeMay static void selinux_key_free(struct key *k) 5307d720024eSMichael LeMay { 5308d720024eSMichael LeMay struct key_security_struct *ksec = k->security; 5309d720024eSMichael LeMay 5310d720024eSMichael LeMay k->security = NULL; 5311d720024eSMichael LeMay kfree(ksec); 5312d720024eSMichael LeMay } 5313d720024eSMichael LeMay 5314d720024eSMichael LeMay static int selinux_key_permission(key_ref_t key_ref, 5315d720024eSMichael LeMay struct task_struct *ctx, 5316d720024eSMichael LeMay key_perm_t perm) 5317d720024eSMichael LeMay { 5318d720024eSMichael LeMay struct key *key; 5319d720024eSMichael LeMay struct task_security_struct *tsec; 5320d720024eSMichael LeMay struct key_security_struct *ksec; 5321d720024eSMichael LeMay 5322d720024eSMichael LeMay key = key_ref_to_ptr(key_ref); 5323d720024eSMichael LeMay 5324d720024eSMichael LeMay tsec = ctx->security; 5325d720024eSMichael LeMay ksec = key->security; 5326d720024eSMichael LeMay 5327d720024eSMichael LeMay /* if no specific permissions are requested, we skip the 5328d720024eSMichael LeMay permission check. No serious, additional covert channels 5329d720024eSMichael LeMay appear to be created. */ 5330d720024eSMichael LeMay if (perm == 0) 5331d720024eSMichael LeMay return 0; 5332d720024eSMichael LeMay 5333d720024eSMichael LeMay return avc_has_perm(tsec->sid, ksec->sid, 5334d720024eSMichael LeMay SECCLASS_KEY, perm, NULL); 5335d720024eSMichael LeMay } 5336d720024eSMichael LeMay 533770a5bb72SDavid Howells static int selinux_key_getsecurity(struct key *key, char **_buffer) 533870a5bb72SDavid Howells { 533970a5bb72SDavid Howells struct key_security_struct *ksec = key->security; 534070a5bb72SDavid Howells char *context = NULL; 534170a5bb72SDavid Howells unsigned len; 534270a5bb72SDavid Howells int rc; 534370a5bb72SDavid Howells 534470a5bb72SDavid Howells rc = security_sid_to_context(ksec->sid, &context, &len); 534570a5bb72SDavid Howells if (!rc) 534670a5bb72SDavid Howells rc = len; 534770a5bb72SDavid Howells *_buffer = context; 534870a5bb72SDavid Howells return rc; 534970a5bb72SDavid Howells } 535070a5bb72SDavid Howells 5351d720024eSMichael LeMay #endif 5352d720024eSMichael LeMay 53531da177e4SLinus Torvalds static struct security_operations selinux_ops = { 5354076c54c5SAhmed S. Darwish .name = "selinux", 5355076c54c5SAhmed S. Darwish 53561da177e4SLinus Torvalds .ptrace = selinux_ptrace, 53571da177e4SLinus Torvalds .capget = selinux_capget, 53581da177e4SLinus Torvalds .capset_check = selinux_capset_check, 53591da177e4SLinus Torvalds .capset_set = selinux_capset_set, 53601da177e4SLinus Torvalds .sysctl = selinux_sysctl, 53611da177e4SLinus Torvalds .capable = selinux_capable, 53621da177e4SLinus Torvalds .quotactl = selinux_quotactl, 53631da177e4SLinus Torvalds .quota_on = selinux_quota_on, 53641da177e4SLinus Torvalds .syslog = selinux_syslog, 53651da177e4SLinus Torvalds .vm_enough_memory = selinux_vm_enough_memory, 53661da177e4SLinus Torvalds 53671da177e4SLinus Torvalds .netlink_send = selinux_netlink_send, 53681da177e4SLinus Torvalds .netlink_recv = selinux_netlink_recv, 53691da177e4SLinus Torvalds 53701da177e4SLinus Torvalds .bprm_alloc_security = selinux_bprm_alloc_security, 53711da177e4SLinus Torvalds .bprm_free_security = selinux_bprm_free_security, 53721da177e4SLinus Torvalds .bprm_apply_creds = selinux_bprm_apply_creds, 53731da177e4SLinus Torvalds .bprm_post_apply_creds = selinux_bprm_post_apply_creds, 53741da177e4SLinus Torvalds .bprm_set_security = selinux_bprm_set_security, 53751da177e4SLinus Torvalds .bprm_check_security = selinux_bprm_check_security, 53761da177e4SLinus Torvalds .bprm_secureexec = selinux_bprm_secureexec, 53771da177e4SLinus Torvalds 53781da177e4SLinus Torvalds .sb_alloc_security = selinux_sb_alloc_security, 53791da177e4SLinus Torvalds .sb_free_security = selinux_sb_free_security, 53801da177e4SLinus Torvalds .sb_copy_data = selinux_sb_copy_data, 53811da177e4SLinus Torvalds .sb_kern_mount = selinux_sb_kern_mount, 53822069f457SEric Paris .sb_show_options = selinux_sb_show_options, 53831da177e4SLinus Torvalds .sb_statfs = selinux_sb_statfs, 53841da177e4SLinus Torvalds .sb_mount = selinux_mount, 53851da177e4SLinus Torvalds .sb_umount = selinux_umount, 5386c9180a57SEric Paris .sb_set_mnt_opts = selinux_set_mnt_opts, 5387c9180a57SEric Paris .sb_clone_mnt_opts = selinux_sb_clone_mnt_opts, 5388e0007529SEric Paris .sb_parse_opts_str = selinux_parse_opts_str, 5389e0007529SEric Paris 53901da177e4SLinus Torvalds 53911da177e4SLinus Torvalds .inode_alloc_security = selinux_inode_alloc_security, 53921da177e4SLinus Torvalds .inode_free_security = selinux_inode_free_security, 53935e41ff9eSStephen Smalley .inode_init_security = selinux_inode_init_security, 53941da177e4SLinus Torvalds .inode_create = selinux_inode_create, 53951da177e4SLinus Torvalds .inode_link = selinux_inode_link, 53961da177e4SLinus Torvalds .inode_unlink = selinux_inode_unlink, 53971da177e4SLinus Torvalds .inode_symlink = selinux_inode_symlink, 53981da177e4SLinus Torvalds .inode_mkdir = selinux_inode_mkdir, 53991da177e4SLinus Torvalds .inode_rmdir = selinux_inode_rmdir, 54001da177e4SLinus Torvalds .inode_mknod = selinux_inode_mknod, 54011da177e4SLinus Torvalds .inode_rename = selinux_inode_rename, 54021da177e4SLinus Torvalds .inode_readlink = selinux_inode_readlink, 54031da177e4SLinus Torvalds .inode_follow_link = selinux_inode_follow_link, 54041da177e4SLinus Torvalds .inode_permission = selinux_inode_permission, 54051da177e4SLinus Torvalds .inode_setattr = selinux_inode_setattr, 54061da177e4SLinus Torvalds .inode_getattr = selinux_inode_getattr, 54071da177e4SLinus Torvalds .inode_setxattr = selinux_inode_setxattr, 54081da177e4SLinus Torvalds .inode_post_setxattr = selinux_inode_post_setxattr, 54091da177e4SLinus Torvalds .inode_getxattr = selinux_inode_getxattr, 54101da177e4SLinus Torvalds .inode_listxattr = selinux_inode_listxattr, 54111da177e4SLinus Torvalds .inode_removexattr = selinux_inode_removexattr, 54121da177e4SLinus Torvalds .inode_getsecurity = selinux_inode_getsecurity, 54131da177e4SLinus Torvalds .inode_setsecurity = selinux_inode_setsecurity, 54141da177e4SLinus Torvalds .inode_listsecurity = selinux_inode_listsecurity, 5415b5376771SSerge E. Hallyn .inode_need_killpriv = selinux_inode_need_killpriv, 5416b5376771SSerge E. Hallyn .inode_killpriv = selinux_inode_killpriv, 5417713a04aeSAhmed S. Darwish .inode_getsecid = selinux_inode_getsecid, 54181da177e4SLinus Torvalds 54191da177e4SLinus Torvalds .file_permission = selinux_file_permission, 54201da177e4SLinus Torvalds .file_alloc_security = selinux_file_alloc_security, 54211da177e4SLinus Torvalds .file_free_security = selinux_file_free_security, 54221da177e4SLinus Torvalds .file_ioctl = selinux_file_ioctl, 54231da177e4SLinus Torvalds .file_mmap = selinux_file_mmap, 54241da177e4SLinus Torvalds .file_mprotect = selinux_file_mprotect, 54251da177e4SLinus Torvalds .file_lock = selinux_file_lock, 54261da177e4SLinus Torvalds .file_fcntl = selinux_file_fcntl, 54271da177e4SLinus Torvalds .file_set_fowner = selinux_file_set_fowner, 54281da177e4SLinus Torvalds .file_send_sigiotask = selinux_file_send_sigiotask, 54291da177e4SLinus Torvalds .file_receive = selinux_file_receive, 54301da177e4SLinus Torvalds 5431788e7dd4SYuichi Nakamura .dentry_open = selinux_dentry_open, 5432788e7dd4SYuichi Nakamura 54331da177e4SLinus Torvalds .task_create = selinux_task_create, 54341da177e4SLinus Torvalds .task_alloc_security = selinux_task_alloc_security, 54351da177e4SLinus Torvalds .task_free_security = selinux_task_free_security, 54361da177e4SLinus Torvalds .task_setuid = selinux_task_setuid, 54371da177e4SLinus Torvalds .task_post_setuid = selinux_task_post_setuid, 54381da177e4SLinus Torvalds .task_setgid = selinux_task_setgid, 54391da177e4SLinus Torvalds .task_setpgid = selinux_task_setpgid, 54401da177e4SLinus Torvalds .task_getpgid = selinux_task_getpgid, 54411da177e4SLinus Torvalds .task_getsid = selinux_task_getsid, 5442f9008e4cSDavid Quigley .task_getsecid = selinux_task_getsecid, 54431da177e4SLinus Torvalds .task_setgroups = selinux_task_setgroups, 54441da177e4SLinus Torvalds .task_setnice = selinux_task_setnice, 544503e68060SJames Morris .task_setioprio = selinux_task_setioprio, 5446a1836a42SDavid Quigley .task_getioprio = selinux_task_getioprio, 54471da177e4SLinus Torvalds .task_setrlimit = selinux_task_setrlimit, 54481da177e4SLinus Torvalds .task_setscheduler = selinux_task_setscheduler, 54491da177e4SLinus Torvalds .task_getscheduler = selinux_task_getscheduler, 545035601547SDavid Quigley .task_movememory = selinux_task_movememory, 54511da177e4SLinus Torvalds .task_kill = selinux_task_kill, 54521da177e4SLinus Torvalds .task_wait = selinux_task_wait, 54531da177e4SLinus Torvalds .task_prctl = selinux_task_prctl, 54541da177e4SLinus Torvalds .task_reparent_to_init = selinux_task_reparent_to_init, 54551da177e4SLinus Torvalds .task_to_inode = selinux_task_to_inode, 54561da177e4SLinus Torvalds 54571da177e4SLinus Torvalds .ipc_permission = selinux_ipc_permission, 5458713a04aeSAhmed S. Darwish .ipc_getsecid = selinux_ipc_getsecid, 54591da177e4SLinus Torvalds 54601da177e4SLinus Torvalds .msg_msg_alloc_security = selinux_msg_msg_alloc_security, 54611da177e4SLinus Torvalds .msg_msg_free_security = selinux_msg_msg_free_security, 54621da177e4SLinus Torvalds 54631da177e4SLinus Torvalds .msg_queue_alloc_security = selinux_msg_queue_alloc_security, 54641da177e4SLinus Torvalds .msg_queue_free_security = selinux_msg_queue_free_security, 54651da177e4SLinus Torvalds .msg_queue_associate = selinux_msg_queue_associate, 54661da177e4SLinus Torvalds .msg_queue_msgctl = selinux_msg_queue_msgctl, 54671da177e4SLinus Torvalds .msg_queue_msgsnd = selinux_msg_queue_msgsnd, 54681da177e4SLinus Torvalds .msg_queue_msgrcv = selinux_msg_queue_msgrcv, 54691da177e4SLinus Torvalds 54701da177e4SLinus Torvalds .shm_alloc_security = selinux_shm_alloc_security, 54711da177e4SLinus Torvalds .shm_free_security = selinux_shm_free_security, 54721da177e4SLinus Torvalds .shm_associate = selinux_shm_associate, 54731da177e4SLinus Torvalds .shm_shmctl = selinux_shm_shmctl, 54741da177e4SLinus Torvalds .shm_shmat = selinux_shm_shmat, 54751da177e4SLinus Torvalds 54761da177e4SLinus Torvalds .sem_alloc_security = selinux_sem_alloc_security, 54771da177e4SLinus Torvalds .sem_free_security = selinux_sem_free_security, 54781da177e4SLinus Torvalds .sem_associate = selinux_sem_associate, 54791da177e4SLinus Torvalds .sem_semctl = selinux_sem_semctl, 54801da177e4SLinus Torvalds .sem_semop = selinux_sem_semop, 54811da177e4SLinus Torvalds 54821da177e4SLinus Torvalds .d_instantiate = selinux_d_instantiate, 54831da177e4SLinus Torvalds 54841da177e4SLinus Torvalds .getprocattr = selinux_getprocattr, 54851da177e4SLinus Torvalds .setprocattr = selinux_setprocattr, 54861da177e4SLinus Torvalds 5487dc49c1f9SCatherine Zhang .secid_to_secctx = selinux_secid_to_secctx, 548863cb3449SDavid Howells .secctx_to_secid = selinux_secctx_to_secid, 5489dc49c1f9SCatherine Zhang .release_secctx = selinux_release_secctx, 5490dc49c1f9SCatherine Zhang 54911da177e4SLinus Torvalds .unix_stream_connect = selinux_socket_unix_stream_connect, 54921da177e4SLinus Torvalds .unix_may_send = selinux_socket_unix_may_send, 54931da177e4SLinus Torvalds 54941da177e4SLinus Torvalds .socket_create = selinux_socket_create, 54951da177e4SLinus Torvalds .socket_post_create = selinux_socket_post_create, 54961da177e4SLinus Torvalds .socket_bind = selinux_socket_bind, 54971da177e4SLinus Torvalds .socket_connect = selinux_socket_connect, 54981da177e4SLinus Torvalds .socket_listen = selinux_socket_listen, 54991da177e4SLinus Torvalds .socket_accept = selinux_socket_accept, 55001da177e4SLinus Torvalds .socket_sendmsg = selinux_socket_sendmsg, 55011da177e4SLinus Torvalds .socket_recvmsg = selinux_socket_recvmsg, 55021da177e4SLinus Torvalds .socket_getsockname = selinux_socket_getsockname, 55031da177e4SLinus Torvalds .socket_getpeername = selinux_socket_getpeername, 55041da177e4SLinus Torvalds .socket_getsockopt = selinux_socket_getsockopt, 55051da177e4SLinus Torvalds .socket_setsockopt = selinux_socket_setsockopt, 55061da177e4SLinus Torvalds .socket_shutdown = selinux_socket_shutdown, 55071da177e4SLinus Torvalds .socket_sock_rcv_skb = selinux_socket_sock_rcv_skb, 55082c7946a7SCatherine Zhang .socket_getpeersec_stream = selinux_socket_getpeersec_stream, 55092c7946a7SCatherine Zhang .socket_getpeersec_dgram = selinux_socket_getpeersec_dgram, 55101da177e4SLinus Torvalds .sk_alloc_security = selinux_sk_alloc_security, 55111da177e4SLinus Torvalds .sk_free_security = selinux_sk_free_security, 5512892c141eSVenkat Yekkirala .sk_clone_security = selinux_sk_clone_security, 5513beb8d13bSVenkat Yekkirala .sk_getsecid = selinux_sk_getsecid, 55144237c75cSVenkat Yekkirala .sock_graft = selinux_sock_graft, 55154237c75cSVenkat Yekkirala .inet_conn_request = selinux_inet_conn_request, 55164237c75cSVenkat Yekkirala .inet_csk_clone = selinux_inet_csk_clone, 55176b877699SVenkat Yekkirala .inet_conn_established = selinux_inet_conn_established, 55184237c75cSVenkat Yekkirala .req_classify_flow = selinux_req_classify_flow, 5519d28d1e08STrent Jaeger 5520d28d1e08STrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM 5521d28d1e08STrent Jaeger .xfrm_policy_alloc_security = selinux_xfrm_policy_alloc, 5522d28d1e08STrent Jaeger .xfrm_policy_clone_security = selinux_xfrm_policy_clone, 5523d28d1e08STrent Jaeger .xfrm_policy_free_security = selinux_xfrm_policy_free, 5524c8c05a8eSCatherine Zhang .xfrm_policy_delete_security = selinux_xfrm_policy_delete, 5525d28d1e08STrent Jaeger .xfrm_state_alloc_security = selinux_xfrm_state_alloc, 5526d28d1e08STrent Jaeger .xfrm_state_free_security = selinux_xfrm_state_free, 5527c8c05a8eSCatherine Zhang .xfrm_state_delete_security = selinux_xfrm_state_delete, 5528d28d1e08STrent Jaeger .xfrm_policy_lookup = selinux_xfrm_policy_lookup, 5529e0d1caa7SVenkat Yekkirala .xfrm_state_pol_flow_match = selinux_xfrm_state_pol_flow_match, 5530e0d1caa7SVenkat Yekkirala .xfrm_decode_session = selinux_xfrm_decode_session, 55311da177e4SLinus Torvalds #endif 5532d720024eSMichael LeMay 5533d720024eSMichael LeMay #ifdef CONFIG_KEYS 5534d720024eSMichael LeMay .key_alloc = selinux_key_alloc, 5535d720024eSMichael LeMay .key_free = selinux_key_free, 5536d720024eSMichael LeMay .key_permission = selinux_key_permission, 553770a5bb72SDavid Howells .key_getsecurity = selinux_key_getsecurity, 5538d720024eSMichael LeMay #endif 55399d57a7f9SAhmed S. Darwish 55409d57a7f9SAhmed S. Darwish #ifdef CONFIG_AUDIT 55419d57a7f9SAhmed S. Darwish .audit_rule_init = selinux_audit_rule_init, 55429d57a7f9SAhmed S. Darwish .audit_rule_known = selinux_audit_rule_known, 55439d57a7f9SAhmed S. Darwish .audit_rule_match = selinux_audit_rule_match, 55449d57a7f9SAhmed S. Darwish .audit_rule_free = selinux_audit_rule_free, 55459d57a7f9SAhmed S. Darwish #endif 55461da177e4SLinus Torvalds }; 55471da177e4SLinus Torvalds 55481da177e4SLinus Torvalds static __init int selinux_init(void) 55491da177e4SLinus Torvalds { 55501da177e4SLinus Torvalds struct task_security_struct *tsec; 55511da177e4SLinus Torvalds 5552076c54c5SAhmed S. Darwish if (!security_module_enable(&selinux_ops)) { 5553076c54c5SAhmed S. Darwish selinux_enabled = 0; 5554076c54c5SAhmed S. Darwish return 0; 5555076c54c5SAhmed S. Darwish } 5556076c54c5SAhmed S. Darwish 55571da177e4SLinus Torvalds if (!selinux_enabled) { 55581da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Disabled at boot.\n"); 55591da177e4SLinus Torvalds return 0; 55601da177e4SLinus Torvalds } 55611da177e4SLinus Torvalds 55621da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Initializing.\n"); 55631da177e4SLinus Torvalds 55641da177e4SLinus Torvalds /* Set the security state for the initial task. */ 55651da177e4SLinus Torvalds if (task_alloc_security(current)) 55661da177e4SLinus Torvalds panic("SELinux: Failed to initialize initial task.\n"); 55671da177e4SLinus Torvalds tsec = current->security; 55681da177e4SLinus Torvalds tsec->osid = tsec->sid = SECINITSID_KERNEL; 55691da177e4SLinus Torvalds 55707cae7e26SJames Morris sel_inode_cache = kmem_cache_create("selinux_inode_security", 55717cae7e26SJames Morris sizeof(struct inode_security_struct), 557220c2df83SPaul Mundt 0, SLAB_PANIC, NULL); 55731da177e4SLinus Torvalds avc_init(); 55741da177e4SLinus Torvalds 55756f0f0fd4SJames Morris secondary_ops = security_ops; 55761da177e4SLinus Torvalds if (!secondary_ops) 55771da177e4SLinus Torvalds panic("SELinux: No initial security operations\n"); 55781da177e4SLinus Torvalds if (register_security(&selinux_ops)) 55791da177e4SLinus Torvalds panic("SELinux: Unable to register with kernel.\n"); 55801da177e4SLinus Torvalds 5581828dfe1dSEric Paris if (selinux_enforcing) 5582fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Starting in enforcing mode\n"); 5583828dfe1dSEric Paris else 5584fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Starting in permissive mode\n"); 5585d720024eSMichael LeMay 55861da177e4SLinus Torvalds return 0; 55871da177e4SLinus Torvalds } 55881da177e4SLinus Torvalds 55891da177e4SLinus Torvalds void selinux_complete_init(void) 55901da177e4SLinus Torvalds { 5591fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Completing initialization.\n"); 55921da177e4SLinus Torvalds 55931da177e4SLinus Torvalds /* Set up any superblocks initialized prior to the policy load. */ 5594fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Setting up existing superblocks.\n"); 5595ba0c19edSStephen Smalley spin_lock(&sb_lock); 55961da177e4SLinus Torvalds spin_lock(&sb_security_lock); 55971da177e4SLinus Torvalds next_sb: 55981da177e4SLinus Torvalds if (!list_empty(&superblock_security_head)) { 55991da177e4SLinus Torvalds struct superblock_security_struct *sbsec = 56001da177e4SLinus Torvalds list_entry(superblock_security_head.next, 56011da177e4SLinus Torvalds struct superblock_security_struct, 56021da177e4SLinus Torvalds list); 56031da177e4SLinus Torvalds struct super_block *sb = sbsec->sb; 56041da177e4SLinus Torvalds sb->s_count++; 56051da177e4SLinus Torvalds spin_unlock(&sb_security_lock); 5606ba0c19edSStephen Smalley spin_unlock(&sb_lock); 56071da177e4SLinus Torvalds down_read(&sb->s_umount); 56081da177e4SLinus Torvalds if (sb->s_root) 56091da177e4SLinus Torvalds superblock_doinit(sb, NULL); 56101da177e4SLinus Torvalds drop_super(sb); 5611ba0c19edSStephen Smalley spin_lock(&sb_lock); 56121da177e4SLinus Torvalds spin_lock(&sb_security_lock); 56131da177e4SLinus Torvalds list_del_init(&sbsec->list); 56141da177e4SLinus Torvalds goto next_sb; 56151da177e4SLinus Torvalds } 56161da177e4SLinus Torvalds spin_unlock(&sb_security_lock); 5617ba0c19edSStephen Smalley spin_unlock(&sb_lock); 56181da177e4SLinus Torvalds } 56191da177e4SLinus Torvalds 56201da177e4SLinus Torvalds /* SELinux requires early initialization in order to label 56211da177e4SLinus Torvalds all processes and objects when they are created. */ 56221da177e4SLinus Torvalds security_initcall(selinux_init); 56231da177e4SLinus Torvalds 5624c2b507fdSStephen Smalley #if defined(CONFIG_NETFILTER) 56251da177e4SLinus Torvalds 5626effad8dfSPaul Moore static struct nf_hook_ops selinux_ipv4_ops[] = { 5627effad8dfSPaul Moore { 5628effad8dfSPaul Moore .hook = selinux_ipv4_postroute, 56291da177e4SLinus Torvalds .owner = THIS_MODULE, 56301da177e4SLinus Torvalds .pf = PF_INET, 56316e23ae2aSPatrick McHardy .hooknum = NF_INET_POST_ROUTING, 56321da177e4SLinus Torvalds .priority = NF_IP_PRI_SELINUX_LAST, 5633effad8dfSPaul Moore }, 5634effad8dfSPaul Moore { 5635effad8dfSPaul Moore .hook = selinux_ipv4_forward, 5636effad8dfSPaul Moore .owner = THIS_MODULE, 5637effad8dfSPaul Moore .pf = PF_INET, 5638effad8dfSPaul Moore .hooknum = NF_INET_FORWARD, 5639effad8dfSPaul Moore .priority = NF_IP_PRI_SELINUX_FIRST, 5640effad8dfSPaul Moore } 56411da177e4SLinus Torvalds }; 56421da177e4SLinus Torvalds 56431da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 56441da177e4SLinus Torvalds 5645effad8dfSPaul Moore static struct nf_hook_ops selinux_ipv6_ops[] = { 5646effad8dfSPaul Moore { 5647effad8dfSPaul Moore .hook = selinux_ipv6_postroute, 56481da177e4SLinus Torvalds .owner = THIS_MODULE, 56491da177e4SLinus Torvalds .pf = PF_INET6, 56506e23ae2aSPatrick McHardy .hooknum = NF_INET_POST_ROUTING, 56511da177e4SLinus Torvalds .priority = NF_IP6_PRI_SELINUX_LAST, 5652effad8dfSPaul Moore }, 5653effad8dfSPaul Moore { 5654effad8dfSPaul Moore .hook = selinux_ipv6_forward, 5655effad8dfSPaul Moore .owner = THIS_MODULE, 5656effad8dfSPaul Moore .pf = PF_INET6, 5657effad8dfSPaul Moore .hooknum = NF_INET_FORWARD, 5658effad8dfSPaul Moore .priority = NF_IP6_PRI_SELINUX_FIRST, 5659effad8dfSPaul Moore } 56601da177e4SLinus Torvalds }; 56611da177e4SLinus Torvalds 56621da177e4SLinus Torvalds #endif /* IPV6 */ 56631da177e4SLinus Torvalds 56641da177e4SLinus Torvalds static int __init selinux_nf_ip_init(void) 56651da177e4SLinus Torvalds { 56661da177e4SLinus Torvalds int err = 0; 56671da177e4SLinus Torvalds 56681da177e4SLinus Torvalds if (!selinux_enabled) 56691da177e4SLinus Torvalds goto out; 56701da177e4SLinus Torvalds 5671fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Registering netfilter hooks\n"); 56721da177e4SLinus Torvalds 56736c5a9d2eSAlexey Dobriyan err = nf_register_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops)); 56741da177e4SLinus Torvalds if (err) 56756c5a9d2eSAlexey Dobriyan panic("SELinux: nf_register_hooks for IPv4: error %d\n", err); 56761da177e4SLinus Torvalds 56771da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 56786c5a9d2eSAlexey Dobriyan err = nf_register_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops)); 56791da177e4SLinus Torvalds if (err) 56806c5a9d2eSAlexey Dobriyan panic("SELinux: nf_register_hooks for IPv6: error %d\n", err); 56811da177e4SLinus Torvalds #endif /* IPV6 */ 5682d28d1e08STrent Jaeger 56831da177e4SLinus Torvalds out: 56841da177e4SLinus Torvalds return err; 56851da177e4SLinus Torvalds } 56861da177e4SLinus Torvalds 56871da177e4SLinus Torvalds __initcall(selinux_nf_ip_init); 56881da177e4SLinus Torvalds 56891da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 56901da177e4SLinus Torvalds static void selinux_nf_ip_exit(void) 56911da177e4SLinus Torvalds { 5692fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Unregistering netfilter hooks\n"); 56931da177e4SLinus Torvalds 56946c5a9d2eSAlexey Dobriyan nf_unregister_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops)); 56951da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 56966c5a9d2eSAlexey Dobriyan nf_unregister_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops)); 56971da177e4SLinus Torvalds #endif /* IPV6 */ 56981da177e4SLinus Torvalds } 56991da177e4SLinus Torvalds #endif 57001da177e4SLinus Torvalds 5701c2b507fdSStephen Smalley #else /* CONFIG_NETFILTER */ 57021da177e4SLinus Torvalds 57031da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 57041da177e4SLinus Torvalds #define selinux_nf_ip_exit() 57051da177e4SLinus Torvalds #endif 57061da177e4SLinus Torvalds 5707c2b507fdSStephen Smalley #endif /* CONFIG_NETFILTER */ 57081da177e4SLinus Torvalds 57091da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 5710828dfe1dSEric Paris static int selinux_disabled; 5711828dfe1dSEric Paris 57121da177e4SLinus Torvalds int selinux_disable(void) 57131da177e4SLinus Torvalds { 57141da177e4SLinus Torvalds extern void exit_sel_fs(void); 57151da177e4SLinus Torvalds 57161da177e4SLinus Torvalds if (ss_initialized) { 57171da177e4SLinus Torvalds /* Not permitted after initial policy load. */ 57181da177e4SLinus Torvalds return -EINVAL; 57191da177e4SLinus Torvalds } 57201da177e4SLinus Torvalds 57211da177e4SLinus Torvalds if (selinux_disabled) { 57221da177e4SLinus Torvalds /* Only do this once. */ 57231da177e4SLinus Torvalds return -EINVAL; 57241da177e4SLinus Torvalds } 57251da177e4SLinus Torvalds 57261da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Disabled at runtime.\n"); 57271da177e4SLinus Torvalds 57281da177e4SLinus Torvalds selinux_disabled = 1; 572930d55280SStephen Smalley selinux_enabled = 0; 57301da177e4SLinus Torvalds 57311da177e4SLinus Torvalds /* Reset security_ops to the secondary module, dummy or capability. */ 57321da177e4SLinus Torvalds security_ops = secondary_ops; 57331da177e4SLinus Torvalds 57341da177e4SLinus Torvalds /* Unregister netfilter hooks. */ 57351da177e4SLinus Torvalds selinux_nf_ip_exit(); 57361da177e4SLinus Torvalds 57371da177e4SLinus Torvalds /* Unregister selinuxfs. */ 57381da177e4SLinus Torvalds exit_sel_fs(); 57391da177e4SLinus Torvalds 57401da177e4SLinus Torvalds return 0; 57411da177e4SLinus Torvalds } 57421da177e4SLinus Torvalds #endif 5743