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. 121da177e4SLinus Torvalds * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com> 131da177e4SLinus Torvalds * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc. 141da177e4SLinus Torvalds * <dgoeddel@trustedcs.com> 15effad8dfSPaul Moore * Copyright (C) 2006, 2007 Hewlett-Packard Development Company, L.P. 16effad8dfSPaul Moore * Paul Moore <paul.moore@hp.com> 17788e7dd4SYuichi Nakamura * Copyright (C) 2007 Hitachi Software Engineering Co., Ltd. 18788e7dd4SYuichi Nakamura * Yuichi Nakamura <ynakam@hitachisoft.jp> 191da177e4SLinus Torvalds * 201da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 211da177e4SLinus Torvalds * it under the terms of the GNU General Public License version 2, 221da177e4SLinus Torvalds * as published by the Free Software Foundation. 231da177e4SLinus Torvalds */ 241da177e4SLinus Torvalds 251da177e4SLinus Torvalds #include <linux/init.h> 261da177e4SLinus Torvalds #include <linux/kernel.h> 271da177e4SLinus Torvalds #include <linux/ptrace.h> 281da177e4SLinus Torvalds #include <linux/errno.h> 291da177e4SLinus Torvalds #include <linux/sched.h> 301da177e4SLinus Torvalds #include <linux/security.h> 311da177e4SLinus Torvalds #include <linux/xattr.h> 321da177e4SLinus Torvalds #include <linux/capability.h> 331da177e4SLinus Torvalds #include <linux/unistd.h> 341da177e4SLinus Torvalds #include <linux/mm.h> 351da177e4SLinus Torvalds #include <linux/mman.h> 361da177e4SLinus Torvalds #include <linux/slab.h> 371da177e4SLinus Torvalds #include <linux/pagemap.h> 381da177e4SLinus Torvalds #include <linux/swap.h> 391da177e4SLinus Torvalds #include <linux/spinlock.h> 401da177e4SLinus Torvalds #include <linux/syscalls.h> 411da177e4SLinus Torvalds #include <linux/file.h> 421da177e4SLinus Torvalds #include <linux/namei.h> 431da177e4SLinus Torvalds #include <linux/mount.h> 441da177e4SLinus Torvalds #include <linux/ext2_fs.h> 451da177e4SLinus Torvalds #include <linux/proc_fs.h> 461da177e4SLinus Torvalds #include <linux/kd.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> 551da177e4SLinus Torvalds #include <asm/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" 83d28d1e08STrent Jaeger #include "xfrm.h" 84c60475bfSPaul Moore #include "netlabel.h" 851da177e4SLinus Torvalds 861da177e4SLinus Torvalds #define XATTR_SELINUX_SUFFIX "selinux" 871da177e4SLinus Torvalds #define XATTR_NAME_SELINUX XATTR_SECURITY_PREFIX XATTR_SELINUX_SUFFIX 881da177e4SLinus Torvalds 89c9180a57SEric Paris #define NUM_SEL_MNT_OPTS 4 90c9180a57SEric Paris 911da177e4SLinus Torvalds extern unsigned int policydb_loaded_version; 921da177e4SLinus Torvalds extern int selinux_nlmsg_lookup(u16 sclass, u16 nlmsg_type, u32 *perm); 934e5ab4cbSJames Morris extern int selinux_compat_net; 9420510f2fSJames Morris extern struct security_operations *security_ops; 951da177e4SLinus Torvalds 96d621d35eSPaul Moore /* SECMARK reference count */ 97d621d35eSPaul Moore atomic_t selinux_secmark_refcount = ATOMIC_INIT(0); 98d621d35eSPaul Moore 991da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP 1001da177e4SLinus Torvalds int selinux_enforcing = 0; 1011da177e4SLinus Torvalds 1021da177e4SLinus Torvalds static int __init enforcing_setup(char *str) 1031da177e4SLinus Torvalds { 1041da177e4SLinus Torvalds selinux_enforcing = simple_strtol(str,NULL,0); 1051da177e4SLinus Torvalds return 1; 1061da177e4SLinus Torvalds } 1071da177e4SLinus Torvalds __setup("enforcing=", enforcing_setup); 1081da177e4SLinus Torvalds #endif 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM 1111da177e4SLinus Torvalds int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE; 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds static int __init selinux_enabled_setup(char *str) 1141da177e4SLinus Torvalds { 1151da177e4SLinus Torvalds selinux_enabled = simple_strtol(str, NULL, 0); 1161da177e4SLinus Torvalds return 1; 1171da177e4SLinus Torvalds } 1181da177e4SLinus Torvalds __setup("selinux=", selinux_enabled_setup); 11930d55280SStephen Smalley #else 12030d55280SStephen Smalley int selinux_enabled = 1; 1211da177e4SLinus Torvalds #endif 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds /* Original (dummy) security module. */ 1241da177e4SLinus Torvalds static struct security_operations *original_ops = NULL; 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds /* Minimal support for a secondary security module, 1271da177e4SLinus Torvalds just to allow the use of the dummy or capability modules. 1281da177e4SLinus Torvalds The owlsm module can alternatively be used as a secondary 1291da177e4SLinus Torvalds module as long as CONFIG_OWLSM_FD is not enabled. */ 1301da177e4SLinus Torvalds static struct security_operations *secondary_ops = NULL; 1311da177e4SLinus Torvalds 1321da177e4SLinus Torvalds /* Lists of inode and superblock security structures initialized 1331da177e4SLinus Torvalds before the policy was loaded. */ 1341da177e4SLinus Torvalds static LIST_HEAD(superblock_security_head); 1351da177e4SLinus Torvalds static DEFINE_SPINLOCK(sb_security_lock); 1361da177e4SLinus Torvalds 137e18b890bSChristoph Lameter static struct kmem_cache *sel_inode_cache; 1387cae7e26SJames Morris 139d621d35eSPaul Moore /** 140d621d35eSPaul Moore * selinux_secmark_enabled - Check to see if SECMARK is currently enabled 141d621d35eSPaul Moore * 142d621d35eSPaul Moore * Description: 143d621d35eSPaul Moore * This function checks the SECMARK reference counter to see if any SECMARK 144d621d35eSPaul Moore * targets are currently configured, if the reference counter is greater than 145d621d35eSPaul Moore * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is 146d621d35eSPaul Moore * enabled, false (0) if SECMARK is disabled. 147d621d35eSPaul Moore * 148d621d35eSPaul Moore */ 149d621d35eSPaul Moore static int selinux_secmark_enabled(void) 150d621d35eSPaul Moore { 151d621d35eSPaul Moore return (atomic_read(&selinux_secmark_refcount) > 0); 152d621d35eSPaul Moore } 153d621d35eSPaul Moore 1541da177e4SLinus Torvalds /* Allocate and free functions for each kind of security blob. */ 1551da177e4SLinus Torvalds 1561da177e4SLinus Torvalds static int task_alloc_security(struct task_struct *task) 1571da177e4SLinus Torvalds { 1581da177e4SLinus Torvalds struct task_security_struct *tsec; 1591da177e4SLinus Torvalds 16089d155efSJames Morris tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL); 1611da177e4SLinus Torvalds if (!tsec) 1621da177e4SLinus Torvalds return -ENOMEM; 1631da177e4SLinus Torvalds 1641da177e4SLinus Torvalds tsec->task = task; 1651da177e4SLinus Torvalds tsec->osid = tsec->sid = tsec->ptrace_sid = SECINITSID_UNLABELED; 1661da177e4SLinus Torvalds task->security = tsec; 1671da177e4SLinus Torvalds 1681da177e4SLinus Torvalds return 0; 1691da177e4SLinus Torvalds } 1701da177e4SLinus Torvalds 1711da177e4SLinus Torvalds static void task_free_security(struct task_struct *task) 1721da177e4SLinus Torvalds { 1731da177e4SLinus Torvalds struct task_security_struct *tsec = task->security; 1741da177e4SLinus Torvalds task->security = NULL; 1751da177e4SLinus Torvalds kfree(tsec); 1761da177e4SLinus Torvalds } 1771da177e4SLinus Torvalds 1781da177e4SLinus Torvalds static int inode_alloc_security(struct inode *inode) 1791da177e4SLinus Torvalds { 1801da177e4SLinus Torvalds struct task_security_struct *tsec = current->security; 1811da177e4SLinus Torvalds struct inode_security_struct *isec; 1821da177e4SLinus Torvalds 183*a02fe132SJosef Bacik isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS); 1841da177e4SLinus Torvalds if (!isec) 1851da177e4SLinus Torvalds return -ENOMEM; 1861da177e4SLinus Torvalds 18723970741SEric Paris mutex_init(&isec->lock); 1881da177e4SLinus Torvalds INIT_LIST_HEAD(&isec->list); 1891da177e4SLinus Torvalds isec->inode = inode; 1901da177e4SLinus Torvalds isec->sid = SECINITSID_UNLABELED; 1911da177e4SLinus Torvalds isec->sclass = SECCLASS_FILE; 1921da177e4SLinus Torvalds isec->task_sid = tsec->sid; 1931da177e4SLinus Torvalds inode->i_security = isec; 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds return 0; 1961da177e4SLinus Torvalds } 1971da177e4SLinus Torvalds 1981da177e4SLinus Torvalds static void inode_free_security(struct inode *inode) 1991da177e4SLinus Torvalds { 2001da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 2011da177e4SLinus Torvalds struct superblock_security_struct *sbsec = inode->i_sb->s_security; 2021da177e4SLinus Torvalds 2031da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 2041da177e4SLinus Torvalds if (!list_empty(&isec->list)) 2051da177e4SLinus Torvalds list_del_init(&isec->list); 2061da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 2071da177e4SLinus Torvalds 2081da177e4SLinus Torvalds inode->i_security = NULL; 2097cae7e26SJames Morris kmem_cache_free(sel_inode_cache, isec); 2101da177e4SLinus Torvalds } 2111da177e4SLinus Torvalds 2121da177e4SLinus Torvalds static int file_alloc_security(struct file *file) 2131da177e4SLinus Torvalds { 2141da177e4SLinus Torvalds struct task_security_struct *tsec = current->security; 2151da177e4SLinus Torvalds struct file_security_struct *fsec; 2161da177e4SLinus Torvalds 21726d2a4beSStephen Smalley fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL); 2181da177e4SLinus Torvalds if (!fsec) 2191da177e4SLinus Torvalds return -ENOMEM; 2201da177e4SLinus Torvalds 2211da177e4SLinus Torvalds fsec->file = file; 2221da177e4SLinus Torvalds fsec->sid = tsec->sid; 2231da177e4SLinus Torvalds fsec->fown_sid = tsec->sid; 2241da177e4SLinus Torvalds file->f_security = fsec; 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds return 0; 2271da177e4SLinus Torvalds } 2281da177e4SLinus Torvalds 2291da177e4SLinus Torvalds static void file_free_security(struct file *file) 2301da177e4SLinus Torvalds { 2311da177e4SLinus Torvalds struct file_security_struct *fsec = file->f_security; 2321da177e4SLinus Torvalds file->f_security = NULL; 2331da177e4SLinus Torvalds kfree(fsec); 2341da177e4SLinus Torvalds } 2351da177e4SLinus Torvalds 2361da177e4SLinus Torvalds static int superblock_alloc_security(struct super_block *sb) 2371da177e4SLinus Torvalds { 2381da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 2391da177e4SLinus Torvalds 24089d155efSJames Morris sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL); 2411da177e4SLinus Torvalds if (!sbsec) 2421da177e4SLinus Torvalds return -ENOMEM; 2431da177e4SLinus Torvalds 244bc7e982bSEric Paris mutex_init(&sbsec->lock); 2451da177e4SLinus Torvalds INIT_LIST_HEAD(&sbsec->list); 2461da177e4SLinus Torvalds INIT_LIST_HEAD(&sbsec->isec_head); 2471da177e4SLinus Torvalds spin_lock_init(&sbsec->isec_lock); 2481da177e4SLinus Torvalds sbsec->sb = sb; 2491da177e4SLinus Torvalds sbsec->sid = SECINITSID_UNLABELED; 2501da177e4SLinus Torvalds sbsec->def_sid = SECINITSID_FILE; 251c312feb2SEric Paris sbsec->mntpoint_sid = SECINITSID_UNLABELED; 2521da177e4SLinus Torvalds sb->s_security = sbsec; 2531da177e4SLinus Torvalds 2541da177e4SLinus Torvalds return 0; 2551da177e4SLinus Torvalds } 2561da177e4SLinus Torvalds 2571da177e4SLinus Torvalds static void superblock_free_security(struct super_block *sb) 2581da177e4SLinus Torvalds { 2591da177e4SLinus Torvalds struct superblock_security_struct *sbsec = sb->s_security; 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds spin_lock(&sb_security_lock); 2621da177e4SLinus Torvalds if (!list_empty(&sbsec->list)) 2631da177e4SLinus Torvalds list_del_init(&sbsec->list); 2641da177e4SLinus Torvalds spin_unlock(&sb_security_lock); 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds sb->s_security = NULL; 2671da177e4SLinus Torvalds kfree(sbsec); 2681da177e4SLinus Torvalds } 2691da177e4SLinus Torvalds 2707d877f3bSAl Viro static int sk_alloc_security(struct sock *sk, int family, gfp_t priority) 2711da177e4SLinus Torvalds { 2721da177e4SLinus Torvalds struct sk_security_struct *ssec; 2731da177e4SLinus Torvalds 27489d155efSJames Morris ssec = kzalloc(sizeof(*ssec), priority); 2751da177e4SLinus Torvalds if (!ssec) 2761da177e4SLinus Torvalds return -ENOMEM; 2771da177e4SLinus Torvalds 2781da177e4SLinus Torvalds ssec->sk = sk; 2791da177e4SLinus Torvalds ssec->peer_sid = SECINITSID_UNLABELED; 280892c141eSVenkat Yekkirala ssec->sid = SECINITSID_UNLABELED; 2811da177e4SLinus Torvalds sk->sk_security = ssec; 2821da177e4SLinus Torvalds 28399f59ed0SPaul Moore selinux_netlbl_sk_security_init(ssec, family); 28499f59ed0SPaul Moore 2851da177e4SLinus Torvalds return 0; 2861da177e4SLinus Torvalds } 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds static void sk_free_security(struct sock *sk) 2891da177e4SLinus Torvalds { 2901da177e4SLinus Torvalds struct sk_security_struct *ssec = sk->sk_security; 2911da177e4SLinus Torvalds 2921da177e4SLinus Torvalds sk->sk_security = NULL; 2931da177e4SLinus Torvalds kfree(ssec); 2941da177e4SLinus Torvalds } 2951da177e4SLinus Torvalds 2961da177e4SLinus Torvalds /* The security server must be initialized before 2971da177e4SLinus Torvalds any labeling or access decisions can be provided. */ 2981da177e4SLinus Torvalds extern int ss_initialized; 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds /* The file system's label must be initialized prior to use. */ 3011da177e4SLinus Torvalds 3021da177e4SLinus Torvalds static char *labeling_behaviors[6] = { 3031da177e4SLinus Torvalds "uses xattr", 3041da177e4SLinus Torvalds "uses transition SIDs", 3051da177e4SLinus Torvalds "uses task SIDs", 3061da177e4SLinus Torvalds "uses genfs_contexts", 3071da177e4SLinus Torvalds "not configured for labeling", 3081da177e4SLinus Torvalds "uses mountpoint labeling", 3091da177e4SLinus Torvalds }; 3101da177e4SLinus Torvalds 3111da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry); 3121da177e4SLinus Torvalds 3131da177e4SLinus Torvalds static inline int inode_doinit(struct inode *inode) 3141da177e4SLinus Torvalds { 3151da177e4SLinus Torvalds return inode_doinit_with_dentry(inode, NULL); 3161da177e4SLinus Torvalds } 3171da177e4SLinus Torvalds 3181da177e4SLinus Torvalds enum { 31931e87930SEric Paris Opt_error = -1, 3201da177e4SLinus Torvalds Opt_context = 1, 3211da177e4SLinus Torvalds Opt_fscontext = 2, 322c9180a57SEric Paris Opt_defcontext = 3, 323c9180a57SEric Paris Opt_rootcontext = 4, 3241da177e4SLinus Torvalds }; 3251da177e4SLinus Torvalds 3261da177e4SLinus Torvalds static match_table_t tokens = { 3271da177e4SLinus Torvalds {Opt_context, "context=%s"}, 3281da177e4SLinus Torvalds {Opt_fscontext, "fscontext=%s"}, 3291da177e4SLinus Torvalds {Opt_defcontext, "defcontext=%s"}, 3300808925eSEric Paris {Opt_rootcontext, "rootcontext=%s"}, 33131e87930SEric Paris {Opt_error, NULL}, 3321da177e4SLinus Torvalds }; 3331da177e4SLinus Torvalds 3341da177e4SLinus Torvalds #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n" 3351da177e4SLinus Torvalds 336c312feb2SEric Paris static int may_context_mount_sb_relabel(u32 sid, 337c312feb2SEric Paris struct superblock_security_struct *sbsec, 338c312feb2SEric Paris struct task_security_struct *tsec) 339c312feb2SEric Paris { 340c312feb2SEric Paris int rc; 341c312feb2SEric Paris 342c312feb2SEric Paris rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 343c312feb2SEric Paris FILESYSTEM__RELABELFROM, NULL); 344c312feb2SEric Paris if (rc) 345c312feb2SEric Paris return rc; 346c312feb2SEric Paris 347c312feb2SEric Paris rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM, 348c312feb2SEric Paris FILESYSTEM__RELABELTO, NULL); 349c312feb2SEric Paris return rc; 350c312feb2SEric Paris } 351c312feb2SEric Paris 3520808925eSEric Paris static int may_context_mount_inode_relabel(u32 sid, 3530808925eSEric Paris struct superblock_security_struct *sbsec, 3540808925eSEric Paris struct task_security_struct *tsec) 3550808925eSEric Paris { 3560808925eSEric Paris int rc; 3570808925eSEric Paris rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 3580808925eSEric Paris FILESYSTEM__RELABELFROM, NULL); 3590808925eSEric Paris if (rc) 3600808925eSEric Paris return rc; 3610808925eSEric Paris 3620808925eSEric Paris rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, 3630808925eSEric Paris FILESYSTEM__ASSOCIATE, NULL); 3640808925eSEric Paris return rc; 3650808925eSEric Paris } 3660808925eSEric Paris 367c9180a57SEric Paris static int sb_finish_set_opts(struct super_block *sb) 3681da177e4SLinus Torvalds { 3691da177e4SLinus Torvalds struct superblock_security_struct *sbsec = sb->s_security; 3701da177e4SLinus Torvalds struct dentry *root = sb->s_root; 371c9180a57SEric Paris struct inode *root_inode = root->d_inode; 3721da177e4SLinus Torvalds int rc = 0; 3731da177e4SLinus Torvalds 3741da177e4SLinus Torvalds if (sbsec->behavior == SECURITY_FS_USE_XATTR) { 3751da177e4SLinus Torvalds /* Make sure that the xattr handler exists and that no 3761da177e4SLinus Torvalds error other than -ENODATA is returned by getxattr on 3771da177e4SLinus Torvalds the root directory. -ENODATA is ok, as this may be 3781da177e4SLinus Torvalds the first boot of the SELinux kernel before we have 3791da177e4SLinus Torvalds assigned xattr values to the filesystem. */ 380c9180a57SEric Paris if (!root_inode->i_op->getxattr) { 3811da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: (dev %s, type %s) has no " 3821da177e4SLinus Torvalds "xattr support\n", sb->s_id, sb->s_type->name); 3831da177e4SLinus Torvalds rc = -EOPNOTSUPP; 3841da177e4SLinus Torvalds goto out; 3851da177e4SLinus Torvalds } 386c9180a57SEric Paris rc = root_inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0); 3871da177e4SLinus Torvalds if (rc < 0 && rc != -ENODATA) { 3881da177e4SLinus Torvalds if (rc == -EOPNOTSUPP) 3891da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: (dev %s, type " 3901da177e4SLinus Torvalds "%s) has no security xattr handler\n", 3911da177e4SLinus Torvalds sb->s_id, sb->s_type->name); 3921da177e4SLinus Torvalds else 3931da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: (dev %s, type " 3941da177e4SLinus Torvalds "%s) getxattr errno %d\n", sb->s_id, 3951da177e4SLinus Torvalds sb->s_type->name, -rc); 3961da177e4SLinus Torvalds goto out; 3971da177e4SLinus Torvalds } 3981da177e4SLinus Torvalds } 3991da177e4SLinus Torvalds 4001da177e4SLinus Torvalds sbsec->initialized = 1; 4011da177e4SLinus Torvalds 402c9180a57SEric Paris if (sbsec->behavior > ARRAY_SIZE(labeling_behaviors)) 403fadcdb45SEric Paris printk(KERN_ERR "SELinux: initialized (dev %s, type %s), unknown behavior\n", 4041da177e4SLinus Torvalds sb->s_id, sb->s_type->name); 405c9180a57SEric Paris else 406fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: initialized (dev %s, type %s), %s\n", 4071da177e4SLinus Torvalds sb->s_id, sb->s_type->name, 4081da177e4SLinus Torvalds labeling_behaviors[sbsec->behavior-1]); 4091da177e4SLinus Torvalds 4101da177e4SLinus Torvalds /* Initialize the root inode. */ 411c9180a57SEric Paris rc = inode_doinit_with_dentry(root_inode, root); 4121da177e4SLinus Torvalds 4131da177e4SLinus Torvalds /* Initialize any other inodes associated with the superblock, e.g. 4141da177e4SLinus Torvalds inodes created prior to initial policy load or inodes created 4151da177e4SLinus Torvalds during get_sb by a pseudo filesystem that directly 4161da177e4SLinus Torvalds populates itself. */ 4171da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 4181da177e4SLinus Torvalds next_inode: 4191da177e4SLinus Torvalds if (!list_empty(&sbsec->isec_head)) { 4201da177e4SLinus Torvalds struct inode_security_struct *isec = 4211da177e4SLinus Torvalds list_entry(sbsec->isec_head.next, 4221da177e4SLinus Torvalds struct inode_security_struct, list); 4231da177e4SLinus Torvalds struct inode *inode = isec->inode; 4241da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 4251da177e4SLinus Torvalds inode = igrab(inode); 4261da177e4SLinus Torvalds if (inode) { 4271da177e4SLinus Torvalds if (!IS_PRIVATE(inode)) 4281da177e4SLinus Torvalds inode_doinit(inode); 4291da177e4SLinus Torvalds iput(inode); 4301da177e4SLinus Torvalds } 4311da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 4321da177e4SLinus Torvalds list_del_init(&isec->list); 4331da177e4SLinus Torvalds goto next_inode; 4341da177e4SLinus Torvalds } 4351da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 4361da177e4SLinus Torvalds out: 437c9180a57SEric Paris return rc; 438c9180a57SEric Paris } 439c9180a57SEric Paris 440c9180a57SEric Paris /* 441c9180a57SEric Paris * This function should allow an FS to ask what it's mount security 442c9180a57SEric Paris * options were so it can use those later for submounts, displaying 443c9180a57SEric Paris * mount options, or whatever. 444c9180a57SEric Paris */ 445c9180a57SEric Paris static int selinux_get_mnt_opts(const struct super_block *sb, 446e0007529SEric Paris struct security_mnt_opts *opts) 447c9180a57SEric Paris { 448c9180a57SEric Paris int rc = 0, i; 449c9180a57SEric Paris struct superblock_security_struct *sbsec = sb->s_security; 450c9180a57SEric Paris char *context = NULL; 451c9180a57SEric Paris u32 len; 452c9180a57SEric Paris char tmp; 453c9180a57SEric Paris 454e0007529SEric Paris security_init_mnt_opts(opts); 455c9180a57SEric Paris 456c9180a57SEric Paris if (!sbsec->initialized) 457c9180a57SEric Paris return -EINVAL; 458c9180a57SEric Paris 459c9180a57SEric Paris if (!ss_initialized) 460c9180a57SEric Paris return -EINVAL; 461c9180a57SEric Paris 462c9180a57SEric Paris /* 463c9180a57SEric Paris * if we ever use sbsec flags for anything other than tracking mount 464c9180a57SEric Paris * settings this is going to need a mask 465c9180a57SEric Paris */ 466c9180a57SEric Paris tmp = sbsec->flags; 467c9180a57SEric Paris /* count the number of mount options for this sb */ 468c9180a57SEric Paris for (i = 0; i < 8; i++) { 469c9180a57SEric Paris if (tmp & 0x01) 470e0007529SEric Paris opts->num_mnt_opts++; 471c9180a57SEric Paris tmp >>= 1; 472c9180a57SEric Paris } 473c9180a57SEric Paris 474e0007529SEric Paris opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC); 475e0007529SEric Paris if (!opts->mnt_opts) { 476c9180a57SEric Paris rc = -ENOMEM; 477c9180a57SEric Paris goto out_free; 478c9180a57SEric Paris } 479c9180a57SEric Paris 480e0007529SEric Paris opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC); 481e0007529SEric Paris if (!opts->mnt_opts_flags) { 482c9180a57SEric Paris rc = -ENOMEM; 483c9180a57SEric Paris goto out_free; 484c9180a57SEric Paris } 485c9180a57SEric Paris 486c9180a57SEric Paris i = 0; 487c9180a57SEric Paris if (sbsec->flags & FSCONTEXT_MNT) { 488c9180a57SEric Paris rc = security_sid_to_context(sbsec->sid, &context, &len); 489c9180a57SEric Paris if (rc) 490c9180a57SEric Paris goto out_free; 491e0007529SEric Paris opts->mnt_opts[i] = context; 492e0007529SEric Paris opts->mnt_opts_flags[i++] = FSCONTEXT_MNT; 493c9180a57SEric Paris } 494c9180a57SEric Paris if (sbsec->flags & CONTEXT_MNT) { 495c9180a57SEric Paris rc = security_sid_to_context(sbsec->mntpoint_sid, &context, &len); 496c9180a57SEric Paris if (rc) 497c9180a57SEric Paris goto out_free; 498e0007529SEric Paris opts->mnt_opts[i] = context; 499e0007529SEric Paris opts->mnt_opts_flags[i++] = CONTEXT_MNT; 500c9180a57SEric Paris } 501c9180a57SEric Paris if (sbsec->flags & DEFCONTEXT_MNT) { 502c9180a57SEric Paris rc = security_sid_to_context(sbsec->def_sid, &context, &len); 503c9180a57SEric Paris if (rc) 504c9180a57SEric Paris goto out_free; 505e0007529SEric Paris opts->mnt_opts[i] = context; 506e0007529SEric Paris opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT; 507c9180a57SEric Paris } 508c9180a57SEric Paris if (sbsec->flags & ROOTCONTEXT_MNT) { 509c9180a57SEric Paris struct inode *root = sbsec->sb->s_root->d_inode; 510c9180a57SEric Paris struct inode_security_struct *isec = root->i_security; 511c9180a57SEric Paris 512c9180a57SEric Paris rc = security_sid_to_context(isec->sid, &context, &len); 513c9180a57SEric Paris if (rc) 514c9180a57SEric Paris goto out_free; 515e0007529SEric Paris opts->mnt_opts[i] = context; 516e0007529SEric Paris opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT; 517c9180a57SEric Paris } 518c9180a57SEric Paris 519e0007529SEric Paris BUG_ON(i != opts->num_mnt_opts); 520c9180a57SEric Paris 521c9180a57SEric Paris return 0; 522c9180a57SEric Paris 523c9180a57SEric Paris out_free: 524e0007529SEric Paris security_free_mnt_opts(opts); 525c9180a57SEric Paris return rc; 526c9180a57SEric Paris } 527c9180a57SEric Paris 528c9180a57SEric Paris static int bad_option(struct superblock_security_struct *sbsec, char flag, 529c9180a57SEric Paris u32 old_sid, u32 new_sid) 530c9180a57SEric Paris { 531c9180a57SEric Paris /* check if the old mount command had the same options */ 532c9180a57SEric Paris if (sbsec->initialized) 533c9180a57SEric Paris if (!(sbsec->flags & flag) || 534c9180a57SEric Paris (old_sid != new_sid)) 535c9180a57SEric Paris return 1; 536c9180a57SEric Paris 537c9180a57SEric Paris /* check if we were passed the same options twice, 538c9180a57SEric Paris * aka someone passed context=a,context=b 539c9180a57SEric Paris */ 540c9180a57SEric Paris if (!sbsec->initialized) 541c9180a57SEric Paris if (sbsec->flags & flag) 542c9180a57SEric Paris return 1; 543c9180a57SEric Paris return 0; 544c9180a57SEric Paris } 545e0007529SEric Paris 546c9180a57SEric Paris /* 547c9180a57SEric Paris * Allow filesystems with binary mount data to explicitly set mount point 548c9180a57SEric Paris * labeling information. 549c9180a57SEric Paris */ 550e0007529SEric Paris static int selinux_set_mnt_opts(struct super_block *sb, 551e0007529SEric Paris struct security_mnt_opts *opts) 552c9180a57SEric Paris { 553c9180a57SEric Paris int rc = 0, i; 554c9180a57SEric Paris struct task_security_struct *tsec = current->security; 555c9180a57SEric Paris struct superblock_security_struct *sbsec = sb->s_security; 556c9180a57SEric Paris const char *name = sb->s_type->name; 557c9180a57SEric Paris struct inode *inode = sbsec->sb->s_root->d_inode; 558c9180a57SEric Paris struct inode_security_struct *root_isec = inode->i_security; 559c9180a57SEric Paris u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0; 560c9180a57SEric Paris u32 defcontext_sid = 0; 561e0007529SEric Paris char **mount_options = opts->mnt_opts; 562e0007529SEric Paris int *flags = opts->mnt_opts_flags; 563e0007529SEric Paris int num_opts = opts->num_mnt_opts; 564c9180a57SEric Paris 565c9180a57SEric Paris mutex_lock(&sbsec->lock); 566c9180a57SEric Paris 567c9180a57SEric Paris if (!ss_initialized) { 568c9180a57SEric Paris if (!num_opts) { 569c9180a57SEric Paris /* Defer initialization until selinux_complete_init, 570c9180a57SEric Paris after the initial policy is loaded and the security 571c9180a57SEric Paris server is ready to handle calls. */ 572c9180a57SEric Paris spin_lock(&sb_security_lock); 573c9180a57SEric Paris if (list_empty(&sbsec->list)) 574c9180a57SEric Paris list_add(&sbsec->list, &superblock_security_head); 575c9180a57SEric Paris spin_unlock(&sb_security_lock); 576c9180a57SEric Paris goto out; 577c9180a57SEric Paris } 578c9180a57SEric Paris rc = -EINVAL; 579c9180a57SEric Paris printk(KERN_WARNING "Unable to set superblock options before " 580c9180a57SEric Paris "the security server is initialized\n"); 581c9180a57SEric Paris goto out; 582c9180a57SEric Paris } 583c9180a57SEric Paris 584c9180a57SEric Paris /* 585e0007529SEric Paris * Binary mount data FS will come through this function twice. Once 586e0007529SEric Paris * from an explicit call and once from the generic calls from the vfs. 587e0007529SEric Paris * Since the generic VFS calls will not contain any security mount data 588e0007529SEric Paris * we need to skip the double mount verification. 589e0007529SEric Paris * 590e0007529SEric Paris * This does open a hole in which we will not notice if the first 591e0007529SEric Paris * mount using this sb set explict options and a second mount using 592e0007529SEric Paris * this sb does not set any security options. (The first options 593e0007529SEric Paris * will be used for both mounts) 594e0007529SEric Paris */ 595e0007529SEric Paris if (sbsec->initialized && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA) 596e0007529SEric Paris && (num_opts == 0)) 597e0007529SEric Paris goto out; 598e0007529SEric Paris 599e0007529SEric Paris /* 600c9180a57SEric Paris * parse the mount options, check if they are valid sids. 601c9180a57SEric Paris * also check if someone is trying to mount the same sb more 602c9180a57SEric Paris * than once with different security options. 603c9180a57SEric Paris */ 604c9180a57SEric Paris for (i = 0; i < num_opts; i++) { 605c9180a57SEric Paris u32 sid; 606c9180a57SEric Paris rc = security_context_to_sid(mount_options[i], 607c9180a57SEric Paris strlen(mount_options[i]), &sid); 608c9180a57SEric Paris if (rc) { 609c9180a57SEric Paris printk(KERN_WARNING "SELinux: security_context_to_sid" 610c9180a57SEric Paris "(%s) failed for (dev %s, type %s) errno=%d\n", 611c9180a57SEric Paris mount_options[i], sb->s_id, name, rc); 612c9180a57SEric Paris goto out; 613c9180a57SEric Paris } 614c9180a57SEric Paris switch (flags[i]) { 615c9180a57SEric Paris case FSCONTEXT_MNT: 616c9180a57SEric Paris fscontext_sid = sid; 617c9180a57SEric Paris 618c9180a57SEric Paris if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, 619c9180a57SEric Paris fscontext_sid)) 620c9180a57SEric Paris goto out_double_mount; 621c9180a57SEric Paris 622c9180a57SEric Paris sbsec->flags |= FSCONTEXT_MNT; 623c9180a57SEric Paris break; 624c9180a57SEric Paris case CONTEXT_MNT: 625c9180a57SEric Paris context_sid = sid; 626c9180a57SEric Paris 627c9180a57SEric Paris if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, 628c9180a57SEric Paris context_sid)) 629c9180a57SEric Paris goto out_double_mount; 630c9180a57SEric Paris 631c9180a57SEric Paris sbsec->flags |= CONTEXT_MNT; 632c9180a57SEric Paris break; 633c9180a57SEric Paris case ROOTCONTEXT_MNT: 634c9180a57SEric Paris rootcontext_sid = sid; 635c9180a57SEric Paris 636c9180a57SEric Paris if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, 637c9180a57SEric Paris rootcontext_sid)) 638c9180a57SEric Paris goto out_double_mount; 639c9180a57SEric Paris 640c9180a57SEric Paris sbsec->flags |= ROOTCONTEXT_MNT; 641c9180a57SEric Paris 642c9180a57SEric Paris break; 643c9180a57SEric Paris case DEFCONTEXT_MNT: 644c9180a57SEric Paris defcontext_sid = sid; 645c9180a57SEric Paris 646c9180a57SEric Paris if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, 647c9180a57SEric Paris defcontext_sid)) 648c9180a57SEric Paris goto out_double_mount; 649c9180a57SEric Paris 650c9180a57SEric Paris sbsec->flags |= DEFCONTEXT_MNT; 651c9180a57SEric Paris 652c9180a57SEric Paris break; 653c9180a57SEric Paris default: 654c9180a57SEric Paris rc = -EINVAL; 655c9180a57SEric Paris goto out; 656c9180a57SEric Paris } 657c9180a57SEric Paris } 658c9180a57SEric Paris 659c9180a57SEric Paris if (sbsec->initialized) { 660c9180a57SEric Paris /* previously mounted with options, but not on this attempt? */ 661c9180a57SEric Paris if (sbsec->flags && !num_opts) 662c9180a57SEric Paris goto out_double_mount; 663c9180a57SEric Paris rc = 0; 664c9180a57SEric Paris goto out; 665c9180a57SEric Paris } 666c9180a57SEric Paris 667c9180a57SEric Paris if (strcmp(sb->s_type->name, "proc") == 0) 668c9180a57SEric Paris sbsec->proc = 1; 669c9180a57SEric Paris 670c9180a57SEric Paris /* Determine the labeling behavior to use for this filesystem type. */ 671c9180a57SEric Paris rc = security_fs_use(sb->s_type->name, &sbsec->behavior, &sbsec->sid); 672c9180a57SEric Paris if (rc) { 673c9180a57SEric Paris printk(KERN_WARNING "%s: security_fs_use(%s) returned %d\n", 674c9180a57SEric Paris __FUNCTION__, sb->s_type->name, rc); 675c9180a57SEric Paris goto out; 676c9180a57SEric Paris } 677c9180a57SEric Paris 678c9180a57SEric Paris /* sets the context of the superblock for the fs being mounted. */ 679c9180a57SEric Paris if (fscontext_sid) { 680c9180a57SEric Paris 681c9180a57SEric Paris rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, tsec); 682c9180a57SEric Paris if (rc) 683c9180a57SEric Paris goto out; 684c9180a57SEric Paris 685c9180a57SEric Paris sbsec->sid = fscontext_sid; 686c9180a57SEric Paris } 687c9180a57SEric Paris 688c9180a57SEric Paris /* 689c9180a57SEric Paris * Switch to using mount point labeling behavior. 690c9180a57SEric Paris * sets the label used on all file below the mountpoint, and will set 691c9180a57SEric Paris * the superblock context if not already set. 692c9180a57SEric Paris */ 693c9180a57SEric Paris if (context_sid) { 694c9180a57SEric Paris if (!fscontext_sid) { 695c9180a57SEric Paris rc = may_context_mount_sb_relabel(context_sid, sbsec, tsec); 696c9180a57SEric Paris if (rc) 697c9180a57SEric Paris goto out; 698c9180a57SEric Paris sbsec->sid = context_sid; 699c9180a57SEric Paris } else { 700c9180a57SEric Paris rc = may_context_mount_inode_relabel(context_sid, sbsec, tsec); 701c9180a57SEric Paris if (rc) 702c9180a57SEric Paris goto out; 703c9180a57SEric Paris } 704c9180a57SEric Paris if (!rootcontext_sid) 705c9180a57SEric Paris rootcontext_sid = context_sid; 706c9180a57SEric Paris 707c9180a57SEric Paris sbsec->mntpoint_sid = context_sid; 708c9180a57SEric Paris sbsec->behavior = SECURITY_FS_USE_MNTPOINT; 709c9180a57SEric Paris } 710c9180a57SEric Paris 711c9180a57SEric Paris if (rootcontext_sid) { 712c9180a57SEric Paris rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec, tsec); 713c9180a57SEric Paris if (rc) 714c9180a57SEric Paris goto out; 715c9180a57SEric Paris 716c9180a57SEric Paris root_isec->sid = rootcontext_sid; 717c9180a57SEric Paris root_isec->initialized = 1; 718c9180a57SEric Paris } 719c9180a57SEric Paris 720c9180a57SEric Paris if (defcontext_sid) { 721c9180a57SEric Paris if (sbsec->behavior != SECURITY_FS_USE_XATTR) { 722c9180a57SEric Paris rc = -EINVAL; 723c9180a57SEric Paris printk(KERN_WARNING "SELinux: defcontext option is " 724c9180a57SEric Paris "invalid for this filesystem type\n"); 725c9180a57SEric Paris goto out; 726c9180a57SEric Paris } 727c9180a57SEric Paris 728c9180a57SEric Paris if (defcontext_sid != sbsec->def_sid) { 729c9180a57SEric Paris rc = may_context_mount_inode_relabel(defcontext_sid, 730c9180a57SEric Paris sbsec, tsec); 731c9180a57SEric Paris if (rc) 732c9180a57SEric Paris goto out; 733c9180a57SEric Paris } 734c9180a57SEric Paris 735c9180a57SEric Paris sbsec->def_sid = defcontext_sid; 736c9180a57SEric Paris } 737c9180a57SEric Paris 738c9180a57SEric Paris rc = sb_finish_set_opts(sb); 739c9180a57SEric Paris out: 740bc7e982bSEric Paris mutex_unlock(&sbsec->lock); 7411da177e4SLinus Torvalds return rc; 742c9180a57SEric Paris out_double_mount: 743c9180a57SEric Paris rc = -EINVAL; 744c9180a57SEric Paris printk(KERN_WARNING "SELinux: mount invalid. Same superblock, different " 745c9180a57SEric Paris "security settings for (dev %s, type %s)\n", sb->s_id, name); 746c9180a57SEric Paris goto out; 747c9180a57SEric Paris } 748c9180a57SEric Paris 749c9180a57SEric Paris static void selinux_sb_clone_mnt_opts(const struct super_block *oldsb, 750c9180a57SEric Paris struct super_block *newsb) 751c9180a57SEric Paris { 752c9180a57SEric Paris const struct superblock_security_struct *oldsbsec = oldsb->s_security; 753c9180a57SEric Paris struct superblock_security_struct *newsbsec = newsb->s_security; 754c9180a57SEric Paris 755c9180a57SEric Paris int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT); 756c9180a57SEric Paris int set_context = (oldsbsec->flags & CONTEXT_MNT); 757c9180a57SEric Paris int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT); 758c9180a57SEric Paris 759c9180a57SEric Paris /* we can't error, we can't save the info, this shouldn't get called 760c9180a57SEric Paris * this early in the boot process. */ 761c9180a57SEric Paris BUG_ON(!ss_initialized); 762c9180a57SEric Paris 763c9180a57SEric Paris /* this might go away sometime down the line if there is a new user 764c9180a57SEric Paris * of clone, but for now, nfs better not get here... */ 765c9180a57SEric Paris BUG_ON(newsbsec->initialized); 766c9180a57SEric Paris 767c9180a57SEric Paris /* how can we clone if the old one wasn't set up?? */ 768c9180a57SEric Paris BUG_ON(!oldsbsec->initialized); 769c9180a57SEric Paris 770c9180a57SEric Paris mutex_lock(&newsbsec->lock); 771c9180a57SEric Paris 772c9180a57SEric Paris newsbsec->flags = oldsbsec->flags; 773c9180a57SEric Paris 774c9180a57SEric Paris newsbsec->sid = oldsbsec->sid; 775c9180a57SEric Paris newsbsec->def_sid = oldsbsec->def_sid; 776c9180a57SEric Paris newsbsec->behavior = oldsbsec->behavior; 777c9180a57SEric Paris 778c9180a57SEric Paris if (set_context) { 779c9180a57SEric Paris u32 sid = oldsbsec->mntpoint_sid; 780c9180a57SEric Paris 781c9180a57SEric Paris if (!set_fscontext) 782c9180a57SEric Paris newsbsec->sid = sid; 783c9180a57SEric Paris if (!set_rootcontext) { 784c9180a57SEric Paris struct inode *newinode = newsb->s_root->d_inode; 785c9180a57SEric Paris struct inode_security_struct *newisec = newinode->i_security; 786c9180a57SEric Paris newisec->sid = sid; 787c9180a57SEric Paris } 788c9180a57SEric Paris newsbsec->mntpoint_sid = sid; 789c9180a57SEric Paris } 790c9180a57SEric Paris if (set_rootcontext) { 791c9180a57SEric Paris const struct inode *oldinode = oldsb->s_root->d_inode; 792c9180a57SEric Paris const struct inode_security_struct *oldisec = oldinode->i_security; 793c9180a57SEric Paris struct inode *newinode = newsb->s_root->d_inode; 794c9180a57SEric Paris struct inode_security_struct *newisec = newinode->i_security; 795c9180a57SEric Paris 796c9180a57SEric Paris newisec->sid = oldisec->sid; 797c9180a57SEric Paris } 798c9180a57SEric Paris 799c9180a57SEric Paris sb_finish_set_opts(newsb); 800c9180a57SEric Paris mutex_unlock(&newsbsec->lock); 801c9180a57SEric Paris } 802c9180a57SEric Paris 8032e1479d9SAdrian Bunk static int selinux_parse_opts_str(char *options, 8042e1479d9SAdrian Bunk struct security_mnt_opts *opts) 805c9180a57SEric Paris { 806e0007529SEric Paris char *p; 807c9180a57SEric Paris char *context = NULL, *defcontext = NULL; 808c9180a57SEric Paris char *fscontext = NULL, *rootcontext = NULL; 809e0007529SEric Paris int rc, num_mnt_opts = 0; 810c9180a57SEric Paris 811e0007529SEric Paris opts->num_mnt_opts = 0; 812c9180a57SEric Paris 813c9180a57SEric Paris /* Standard string-based options. */ 814c9180a57SEric Paris while ((p = strsep(&options, "|")) != NULL) { 815c9180a57SEric Paris int token; 816c9180a57SEric Paris substring_t args[MAX_OPT_ARGS]; 817c9180a57SEric Paris 818c9180a57SEric Paris if (!*p) 819c9180a57SEric Paris continue; 820c9180a57SEric Paris 821c9180a57SEric Paris token = match_token(p, tokens, args); 822c9180a57SEric Paris 823c9180a57SEric Paris switch (token) { 824c9180a57SEric Paris case Opt_context: 825c9180a57SEric Paris if (context || defcontext) { 826c9180a57SEric Paris rc = -EINVAL; 827c9180a57SEric Paris printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 828c9180a57SEric Paris goto out_err; 829c9180a57SEric Paris } 830c9180a57SEric Paris context = match_strdup(&args[0]); 831c9180a57SEric Paris if (!context) { 832c9180a57SEric Paris rc = -ENOMEM; 833c9180a57SEric Paris goto out_err; 834c9180a57SEric Paris } 835c9180a57SEric Paris break; 836c9180a57SEric Paris 837c9180a57SEric Paris case Opt_fscontext: 838c9180a57SEric Paris if (fscontext) { 839c9180a57SEric Paris rc = -EINVAL; 840c9180a57SEric Paris printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 841c9180a57SEric Paris goto out_err; 842c9180a57SEric Paris } 843c9180a57SEric Paris fscontext = match_strdup(&args[0]); 844c9180a57SEric Paris if (!fscontext) { 845c9180a57SEric Paris rc = -ENOMEM; 846c9180a57SEric Paris goto out_err; 847c9180a57SEric Paris } 848c9180a57SEric Paris break; 849c9180a57SEric Paris 850c9180a57SEric Paris case Opt_rootcontext: 851c9180a57SEric Paris if (rootcontext) { 852c9180a57SEric Paris rc = -EINVAL; 853c9180a57SEric Paris printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 854c9180a57SEric Paris goto out_err; 855c9180a57SEric Paris } 856c9180a57SEric Paris rootcontext = match_strdup(&args[0]); 857c9180a57SEric Paris if (!rootcontext) { 858c9180a57SEric Paris rc = -ENOMEM; 859c9180a57SEric Paris goto out_err; 860c9180a57SEric Paris } 861c9180a57SEric Paris break; 862c9180a57SEric Paris 863c9180a57SEric Paris case Opt_defcontext: 864c9180a57SEric Paris if (context || defcontext) { 865c9180a57SEric Paris rc = -EINVAL; 866c9180a57SEric Paris printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 867c9180a57SEric Paris goto out_err; 868c9180a57SEric Paris } 869c9180a57SEric Paris defcontext = match_strdup(&args[0]); 870c9180a57SEric Paris if (!defcontext) { 871c9180a57SEric Paris rc = -ENOMEM; 872c9180a57SEric Paris goto out_err; 873c9180a57SEric Paris } 874c9180a57SEric Paris break; 875c9180a57SEric Paris 876c9180a57SEric Paris default: 877c9180a57SEric Paris rc = -EINVAL; 878c9180a57SEric Paris printk(KERN_WARNING "SELinux: unknown mount option\n"); 879c9180a57SEric Paris goto out_err; 880c9180a57SEric Paris 881c9180a57SEric Paris } 882c9180a57SEric Paris } 883c9180a57SEric Paris 884e0007529SEric Paris rc = -ENOMEM; 885e0007529SEric Paris opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_ATOMIC); 886e0007529SEric Paris if (!opts->mnt_opts) 887e0007529SEric Paris goto out_err; 888e0007529SEric Paris 889e0007529SEric Paris opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), GFP_ATOMIC); 890e0007529SEric Paris if (!opts->mnt_opts_flags) { 891e0007529SEric Paris kfree(opts->mnt_opts); 892e0007529SEric Paris goto out_err; 893c9180a57SEric Paris } 894c9180a57SEric Paris 895e0007529SEric Paris if (fscontext) { 896e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = fscontext; 897e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT; 898e0007529SEric Paris } 899e0007529SEric Paris if (context) { 900e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = context; 901e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT; 902e0007529SEric Paris } 903e0007529SEric Paris if (rootcontext) { 904e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = rootcontext; 905e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT; 906e0007529SEric Paris } 907e0007529SEric Paris if (defcontext) { 908e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = defcontext; 909e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT; 910e0007529SEric Paris } 911e0007529SEric Paris 912e0007529SEric Paris opts->num_mnt_opts = num_mnt_opts; 913e0007529SEric Paris return 0; 914e0007529SEric Paris 915c9180a57SEric Paris out_err: 916c9180a57SEric Paris kfree(context); 917c9180a57SEric Paris kfree(defcontext); 918c9180a57SEric Paris kfree(fscontext); 919c9180a57SEric Paris kfree(rootcontext); 920c9180a57SEric Paris return rc; 9211da177e4SLinus Torvalds } 922e0007529SEric Paris /* 923e0007529SEric Paris * string mount options parsing and call set the sbsec 924e0007529SEric Paris */ 925e0007529SEric Paris static int superblock_doinit(struct super_block *sb, void *data) 926e0007529SEric Paris { 927e0007529SEric Paris int rc = 0; 928e0007529SEric Paris char *options = data; 929e0007529SEric Paris struct security_mnt_opts opts; 930e0007529SEric Paris 931e0007529SEric Paris security_init_mnt_opts(&opts); 932e0007529SEric Paris 933e0007529SEric Paris if (!data) 934e0007529SEric Paris goto out; 935e0007529SEric Paris 936e0007529SEric Paris BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA); 937e0007529SEric Paris 938e0007529SEric Paris rc = selinux_parse_opts_str(options, &opts); 939e0007529SEric Paris if (rc) 940e0007529SEric Paris goto out_err; 941e0007529SEric Paris 942e0007529SEric Paris out: 943e0007529SEric Paris rc = selinux_set_mnt_opts(sb, &opts); 944e0007529SEric Paris 945e0007529SEric Paris out_err: 946e0007529SEric Paris security_free_mnt_opts(&opts); 947e0007529SEric Paris return rc; 948e0007529SEric Paris } 9491da177e4SLinus Torvalds 9501da177e4SLinus Torvalds static inline u16 inode_mode_to_security_class(umode_t mode) 9511da177e4SLinus Torvalds { 9521da177e4SLinus Torvalds switch (mode & S_IFMT) { 9531da177e4SLinus Torvalds case S_IFSOCK: 9541da177e4SLinus Torvalds return SECCLASS_SOCK_FILE; 9551da177e4SLinus Torvalds case S_IFLNK: 9561da177e4SLinus Torvalds return SECCLASS_LNK_FILE; 9571da177e4SLinus Torvalds case S_IFREG: 9581da177e4SLinus Torvalds return SECCLASS_FILE; 9591da177e4SLinus Torvalds case S_IFBLK: 9601da177e4SLinus Torvalds return SECCLASS_BLK_FILE; 9611da177e4SLinus Torvalds case S_IFDIR: 9621da177e4SLinus Torvalds return SECCLASS_DIR; 9631da177e4SLinus Torvalds case S_IFCHR: 9641da177e4SLinus Torvalds return SECCLASS_CHR_FILE; 9651da177e4SLinus Torvalds case S_IFIFO: 9661da177e4SLinus Torvalds return SECCLASS_FIFO_FILE; 9671da177e4SLinus Torvalds 9681da177e4SLinus Torvalds } 9691da177e4SLinus Torvalds 9701da177e4SLinus Torvalds return SECCLASS_FILE; 9711da177e4SLinus Torvalds } 9721da177e4SLinus Torvalds 97313402580SJames Morris static inline int default_protocol_stream(int protocol) 97413402580SJames Morris { 97513402580SJames Morris return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP); 97613402580SJames Morris } 97713402580SJames Morris 97813402580SJames Morris static inline int default_protocol_dgram(int protocol) 97913402580SJames Morris { 98013402580SJames Morris return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP); 98113402580SJames Morris } 98213402580SJames Morris 9831da177e4SLinus Torvalds static inline u16 socket_type_to_security_class(int family, int type, int protocol) 9841da177e4SLinus Torvalds { 9851da177e4SLinus Torvalds switch (family) { 9861da177e4SLinus Torvalds case PF_UNIX: 9871da177e4SLinus Torvalds switch (type) { 9881da177e4SLinus Torvalds case SOCK_STREAM: 9891da177e4SLinus Torvalds case SOCK_SEQPACKET: 9901da177e4SLinus Torvalds return SECCLASS_UNIX_STREAM_SOCKET; 9911da177e4SLinus Torvalds case SOCK_DGRAM: 9921da177e4SLinus Torvalds return SECCLASS_UNIX_DGRAM_SOCKET; 9931da177e4SLinus Torvalds } 9941da177e4SLinus Torvalds break; 9951da177e4SLinus Torvalds case PF_INET: 9961da177e4SLinus Torvalds case PF_INET6: 9971da177e4SLinus Torvalds switch (type) { 9981da177e4SLinus Torvalds case SOCK_STREAM: 99913402580SJames Morris if (default_protocol_stream(protocol)) 10001da177e4SLinus Torvalds return SECCLASS_TCP_SOCKET; 100113402580SJames Morris else 100213402580SJames Morris return SECCLASS_RAWIP_SOCKET; 10031da177e4SLinus Torvalds case SOCK_DGRAM: 100413402580SJames Morris if (default_protocol_dgram(protocol)) 10051da177e4SLinus Torvalds return SECCLASS_UDP_SOCKET; 100613402580SJames Morris else 100713402580SJames Morris return SECCLASS_RAWIP_SOCKET; 10082ee92d46SJames Morris case SOCK_DCCP: 10092ee92d46SJames Morris return SECCLASS_DCCP_SOCKET; 101013402580SJames Morris default: 10111da177e4SLinus Torvalds return SECCLASS_RAWIP_SOCKET; 10121da177e4SLinus Torvalds } 10131da177e4SLinus Torvalds break; 10141da177e4SLinus Torvalds case PF_NETLINK: 10151da177e4SLinus Torvalds switch (protocol) { 10161da177e4SLinus Torvalds case NETLINK_ROUTE: 10171da177e4SLinus Torvalds return SECCLASS_NETLINK_ROUTE_SOCKET; 10181da177e4SLinus Torvalds case NETLINK_FIREWALL: 10191da177e4SLinus Torvalds return SECCLASS_NETLINK_FIREWALL_SOCKET; 1020216efaaaSJames Morris case NETLINK_INET_DIAG: 10211da177e4SLinus Torvalds return SECCLASS_NETLINK_TCPDIAG_SOCKET; 10221da177e4SLinus Torvalds case NETLINK_NFLOG: 10231da177e4SLinus Torvalds return SECCLASS_NETLINK_NFLOG_SOCKET; 10241da177e4SLinus Torvalds case NETLINK_XFRM: 10251da177e4SLinus Torvalds return SECCLASS_NETLINK_XFRM_SOCKET; 10261da177e4SLinus Torvalds case NETLINK_SELINUX: 10271da177e4SLinus Torvalds return SECCLASS_NETLINK_SELINUX_SOCKET; 10281da177e4SLinus Torvalds case NETLINK_AUDIT: 10291da177e4SLinus Torvalds return SECCLASS_NETLINK_AUDIT_SOCKET; 10301da177e4SLinus Torvalds case NETLINK_IP6_FW: 10311da177e4SLinus Torvalds return SECCLASS_NETLINK_IP6FW_SOCKET; 10321da177e4SLinus Torvalds case NETLINK_DNRTMSG: 10331da177e4SLinus Torvalds return SECCLASS_NETLINK_DNRT_SOCKET; 10340c9b7942SJames Morris case NETLINK_KOBJECT_UEVENT: 10350c9b7942SJames Morris return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET; 10361da177e4SLinus Torvalds default: 10371da177e4SLinus Torvalds return SECCLASS_NETLINK_SOCKET; 10381da177e4SLinus Torvalds } 10391da177e4SLinus Torvalds case PF_PACKET: 10401da177e4SLinus Torvalds return SECCLASS_PACKET_SOCKET; 10411da177e4SLinus Torvalds case PF_KEY: 10421da177e4SLinus Torvalds return SECCLASS_KEY_SOCKET; 10433e3ff15eSChristopher J. PeBenito case PF_APPLETALK: 10443e3ff15eSChristopher J. PeBenito return SECCLASS_APPLETALK_SOCKET; 10451da177e4SLinus Torvalds } 10461da177e4SLinus Torvalds 10471da177e4SLinus Torvalds return SECCLASS_SOCKET; 10481da177e4SLinus Torvalds } 10491da177e4SLinus Torvalds 10501da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 10511da177e4SLinus Torvalds static int selinux_proc_get_sid(struct proc_dir_entry *de, 10521da177e4SLinus Torvalds u16 tclass, 10531da177e4SLinus Torvalds u32 *sid) 10541da177e4SLinus Torvalds { 10551da177e4SLinus Torvalds int buflen, rc; 10561da177e4SLinus Torvalds char *buffer, *path, *end; 10571da177e4SLinus Torvalds 10581da177e4SLinus Torvalds buffer = (char*)__get_free_page(GFP_KERNEL); 10591da177e4SLinus Torvalds if (!buffer) 10601da177e4SLinus Torvalds return -ENOMEM; 10611da177e4SLinus Torvalds 10621da177e4SLinus Torvalds buflen = PAGE_SIZE; 10631da177e4SLinus Torvalds end = buffer+buflen; 10641da177e4SLinus Torvalds *--end = '\0'; 10651da177e4SLinus Torvalds buflen--; 10661da177e4SLinus Torvalds path = end-1; 10671da177e4SLinus Torvalds *path = '/'; 10681da177e4SLinus Torvalds while (de && de != de->parent) { 10691da177e4SLinus Torvalds buflen -= de->namelen + 1; 10701da177e4SLinus Torvalds if (buflen < 0) 10711da177e4SLinus Torvalds break; 10721da177e4SLinus Torvalds end -= de->namelen; 10731da177e4SLinus Torvalds memcpy(end, de->name, de->namelen); 10741da177e4SLinus Torvalds *--end = '/'; 10751da177e4SLinus Torvalds path = end; 10761da177e4SLinus Torvalds de = de->parent; 10771da177e4SLinus Torvalds } 10781da177e4SLinus Torvalds rc = security_genfs_sid("proc", path, tclass, sid); 10791da177e4SLinus Torvalds free_page((unsigned long)buffer); 10801da177e4SLinus Torvalds return rc; 10811da177e4SLinus Torvalds } 10821da177e4SLinus Torvalds #else 10831da177e4SLinus Torvalds static int selinux_proc_get_sid(struct proc_dir_entry *de, 10841da177e4SLinus Torvalds u16 tclass, 10851da177e4SLinus Torvalds u32 *sid) 10861da177e4SLinus Torvalds { 10871da177e4SLinus Torvalds return -EINVAL; 10881da177e4SLinus Torvalds } 10891da177e4SLinus Torvalds #endif 10901da177e4SLinus Torvalds 10911da177e4SLinus Torvalds /* The inode's security attributes must be initialized before first use. */ 10921da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry) 10931da177e4SLinus Torvalds { 10941da177e4SLinus Torvalds struct superblock_security_struct *sbsec = NULL; 10951da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 10961da177e4SLinus Torvalds u32 sid; 10971da177e4SLinus Torvalds struct dentry *dentry; 10981da177e4SLinus Torvalds #define INITCONTEXTLEN 255 10991da177e4SLinus Torvalds char *context = NULL; 11001da177e4SLinus Torvalds unsigned len = 0; 11011da177e4SLinus Torvalds int rc = 0; 11021da177e4SLinus Torvalds 11031da177e4SLinus Torvalds if (isec->initialized) 11041da177e4SLinus Torvalds goto out; 11051da177e4SLinus Torvalds 110623970741SEric Paris mutex_lock(&isec->lock); 11071da177e4SLinus Torvalds if (isec->initialized) 110823970741SEric Paris goto out_unlock; 11091da177e4SLinus Torvalds 11101da177e4SLinus Torvalds sbsec = inode->i_sb->s_security; 11111da177e4SLinus Torvalds if (!sbsec->initialized) { 11121da177e4SLinus Torvalds /* Defer initialization until selinux_complete_init, 11131da177e4SLinus Torvalds after the initial policy is loaded and the security 11141da177e4SLinus Torvalds server is ready to handle calls. */ 11151da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 11161da177e4SLinus Torvalds if (list_empty(&isec->list)) 11171da177e4SLinus Torvalds list_add(&isec->list, &sbsec->isec_head); 11181da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 111923970741SEric Paris goto out_unlock; 11201da177e4SLinus Torvalds } 11211da177e4SLinus Torvalds 11221da177e4SLinus Torvalds switch (sbsec->behavior) { 11231da177e4SLinus Torvalds case SECURITY_FS_USE_XATTR: 11241da177e4SLinus Torvalds if (!inode->i_op->getxattr) { 11251da177e4SLinus Torvalds isec->sid = sbsec->def_sid; 11261da177e4SLinus Torvalds break; 11271da177e4SLinus Torvalds } 11281da177e4SLinus Torvalds 11291da177e4SLinus Torvalds /* Need a dentry, since the xattr API requires one. 11301da177e4SLinus Torvalds Life would be simpler if we could just pass the inode. */ 11311da177e4SLinus Torvalds if (opt_dentry) { 11321da177e4SLinus Torvalds /* Called from d_instantiate or d_splice_alias. */ 11331da177e4SLinus Torvalds dentry = dget(opt_dentry); 11341da177e4SLinus Torvalds } else { 11351da177e4SLinus Torvalds /* Called from selinux_complete_init, try to find a dentry. */ 11361da177e4SLinus Torvalds dentry = d_find_alias(inode); 11371da177e4SLinus Torvalds } 11381da177e4SLinus Torvalds if (!dentry) { 11391da177e4SLinus Torvalds printk(KERN_WARNING "%s: no dentry for dev=%s " 11401da177e4SLinus Torvalds "ino=%ld\n", __FUNCTION__, inode->i_sb->s_id, 11411da177e4SLinus Torvalds inode->i_ino); 114223970741SEric Paris goto out_unlock; 11431da177e4SLinus Torvalds } 11441da177e4SLinus Torvalds 11451da177e4SLinus Torvalds len = INITCONTEXTLEN; 11461da177e4SLinus Torvalds context = kmalloc(len, GFP_KERNEL); 11471da177e4SLinus Torvalds if (!context) { 11481da177e4SLinus Torvalds rc = -ENOMEM; 11491da177e4SLinus Torvalds dput(dentry); 115023970741SEric Paris goto out_unlock; 11511da177e4SLinus Torvalds } 11521da177e4SLinus Torvalds rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX, 11531da177e4SLinus Torvalds context, len); 11541da177e4SLinus Torvalds if (rc == -ERANGE) { 11551da177e4SLinus Torvalds /* Need a larger buffer. Query for the right size. */ 11561da177e4SLinus Torvalds rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX, 11571da177e4SLinus Torvalds NULL, 0); 11581da177e4SLinus Torvalds if (rc < 0) { 11591da177e4SLinus Torvalds dput(dentry); 116023970741SEric Paris goto out_unlock; 11611da177e4SLinus Torvalds } 11621da177e4SLinus Torvalds kfree(context); 11631da177e4SLinus Torvalds len = rc; 11641da177e4SLinus Torvalds context = kmalloc(len, GFP_KERNEL); 11651da177e4SLinus Torvalds if (!context) { 11661da177e4SLinus Torvalds rc = -ENOMEM; 11671da177e4SLinus Torvalds dput(dentry); 116823970741SEric Paris goto out_unlock; 11691da177e4SLinus Torvalds } 11701da177e4SLinus Torvalds rc = inode->i_op->getxattr(dentry, 11711da177e4SLinus Torvalds XATTR_NAME_SELINUX, 11721da177e4SLinus Torvalds context, len); 11731da177e4SLinus Torvalds } 11741da177e4SLinus Torvalds dput(dentry); 11751da177e4SLinus Torvalds if (rc < 0) { 11761da177e4SLinus Torvalds if (rc != -ENODATA) { 11771da177e4SLinus Torvalds printk(KERN_WARNING "%s: getxattr returned " 11781da177e4SLinus Torvalds "%d for dev=%s ino=%ld\n", __FUNCTION__, 11791da177e4SLinus Torvalds -rc, inode->i_sb->s_id, inode->i_ino); 11801da177e4SLinus Torvalds kfree(context); 118123970741SEric Paris goto out_unlock; 11821da177e4SLinus Torvalds } 11831da177e4SLinus Torvalds /* Map ENODATA to the default file SID */ 11841da177e4SLinus Torvalds sid = sbsec->def_sid; 11851da177e4SLinus Torvalds rc = 0; 11861da177e4SLinus Torvalds } else { 1187f5c1d5b2SJames Morris rc = security_context_to_sid_default(context, rc, &sid, 1188f5c1d5b2SJames Morris sbsec->def_sid); 11891da177e4SLinus Torvalds if (rc) { 11901da177e4SLinus Torvalds printk(KERN_WARNING "%s: context_to_sid(%s) " 11911da177e4SLinus Torvalds "returned %d for dev=%s ino=%ld\n", 11921da177e4SLinus Torvalds __FUNCTION__, context, -rc, 11931da177e4SLinus Torvalds inode->i_sb->s_id, inode->i_ino); 11941da177e4SLinus Torvalds kfree(context); 11951da177e4SLinus Torvalds /* Leave with the unlabeled SID */ 11961da177e4SLinus Torvalds rc = 0; 11971da177e4SLinus Torvalds break; 11981da177e4SLinus Torvalds } 11991da177e4SLinus Torvalds } 12001da177e4SLinus Torvalds kfree(context); 12011da177e4SLinus Torvalds isec->sid = sid; 12021da177e4SLinus Torvalds break; 12031da177e4SLinus Torvalds case SECURITY_FS_USE_TASK: 12041da177e4SLinus Torvalds isec->sid = isec->task_sid; 12051da177e4SLinus Torvalds break; 12061da177e4SLinus Torvalds case SECURITY_FS_USE_TRANS: 12071da177e4SLinus Torvalds /* Default to the fs SID. */ 12081da177e4SLinus Torvalds isec->sid = sbsec->sid; 12091da177e4SLinus Torvalds 12101da177e4SLinus Torvalds /* Try to obtain a transition SID. */ 12111da177e4SLinus Torvalds isec->sclass = inode_mode_to_security_class(inode->i_mode); 12121da177e4SLinus Torvalds rc = security_transition_sid(isec->task_sid, 12131da177e4SLinus Torvalds sbsec->sid, 12141da177e4SLinus Torvalds isec->sclass, 12151da177e4SLinus Torvalds &sid); 12161da177e4SLinus Torvalds if (rc) 121723970741SEric Paris goto out_unlock; 12181da177e4SLinus Torvalds isec->sid = sid; 12191da177e4SLinus Torvalds break; 1220c312feb2SEric Paris case SECURITY_FS_USE_MNTPOINT: 1221c312feb2SEric Paris isec->sid = sbsec->mntpoint_sid; 1222c312feb2SEric Paris break; 12231da177e4SLinus Torvalds default: 1224c312feb2SEric Paris /* Default to the fs superblock SID. */ 12251da177e4SLinus Torvalds isec->sid = sbsec->sid; 12261da177e4SLinus Torvalds 12271da177e4SLinus Torvalds if (sbsec->proc) { 12281da177e4SLinus Torvalds struct proc_inode *proci = PROC_I(inode); 12291da177e4SLinus Torvalds if (proci->pde) { 12301da177e4SLinus Torvalds isec->sclass = inode_mode_to_security_class(inode->i_mode); 12311da177e4SLinus Torvalds rc = selinux_proc_get_sid(proci->pde, 12321da177e4SLinus Torvalds isec->sclass, 12331da177e4SLinus Torvalds &sid); 12341da177e4SLinus Torvalds if (rc) 123523970741SEric Paris goto out_unlock; 12361da177e4SLinus Torvalds isec->sid = sid; 12371da177e4SLinus Torvalds } 12381da177e4SLinus Torvalds } 12391da177e4SLinus Torvalds break; 12401da177e4SLinus Torvalds } 12411da177e4SLinus Torvalds 12421da177e4SLinus Torvalds isec->initialized = 1; 12431da177e4SLinus Torvalds 124423970741SEric Paris out_unlock: 124523970741SEric Paris mutex_unlock(&isec->lock); 12461da177e4SLinus Torvalds out: 12471da177e4SLinus Torvalds if (isec->sclass == SECCLASS_FILE) 12481da177e4SLinus Torvalds isec->sclass = inode_mode_to_security_class(inode->i_mode); 12491da177e4SLinus Torvalds return rc; 12501da177e4SLinus Torvalds } 12511da177e4SLinus Torvalds 12521da177e4SLinus Torvalds /* Convert a Linux signal to an access vector. */ 12531da177e4SLinus Torvalds static inline u32 signal_to_av(int sig) 12541da177e4SLinus Torvalds { 12551da177e4SLinus Torvalds u32 perm = 0; 12561da177e4SLinus Torvalds 12571da177e4SLinus Torvalds switch (sig) { 12581da177e4SLinus Torvalds case SIGCHLD: 12591da177e4SLinus Torvalds /* Commonly granted from child to parent. */ 12601da177e4SLinus Torvalds perm = PROCESS__SIGCHLD; 12611da177e4SLinus Torvalds break; 12621da177e4SLinus Torvalds case SIGKILL: 12631da177e4SLinus Torvalds /* Cannot be caught or ignored */ 12641da177e4SLinus Torvalds perm = PROCESS__SIGKILL; 12651da177e4SLinus Torvalds break; 12661da177e4SLinus Torvalds case SIGSTOP: 12671da177e4SLinus Torvalds /* Cannot be caught or ignored */ 12681da177e4SLinus Torvalds perm = PROCESS__SIGSTOP; 12691da177e4SLinus Torvalds break; 12701da177e4SLinus Torvalds default: 12711da177e4SLinus Torvalds /* All other signals. */ 12721da177e4SLinus Torvalds perm = PROCESS__SIGNAL; 12731da177e4SLinus Torvalds break; 12741da177e4SLinus Torvalds } 12751da177e4SLinus Torvalds 12761da177e4SLinus Torvalds return perm; 12771da177e4SLinus Torvalds } 12781da177e4SLinus Torvalds 12791da177e4SLinus Torvalds /* Check permission betweeen a pair of tasks, e.g. signal checks, 12801da177e4SLinus Torvalds fork check, ptrace check, etc. */ 12811da177e4SLinus Torvalds static int task_has_perm(struct task_struct *tsk1, 12821da177e4SLinus Torvalds struct task_struct *tsk2, 12831da177e4SLinus Torvalds u32 perms) 12841da177e4SLinus Torvalds { 12851da177e4SLinus Torvalds struct task_security_struct *tsec1, *tsec2; 12861da177e4SLinus Torvalds 12871da177e4SLinus Torvalds tsec1 = tsk1->security; 12881da177e4SLinus Torvalds tsec2 = tsk2->security; 12891da177e4SLinus Torvalds return avc_has_perm(tsec1->sid, tsec2->sid, 12901da177e4SLinus Torvalds SECCLASS_PROCESS, perms, NULL); 12911da177e4SLinus Torvalds } 12921da177e4SLinus Torvalds 1293b68e418cSStephen Smalley #if CAP_LAST_CAP > 63 1294b68e418cSStephen Smalley #error Fix SELinux to handle capabilities > 63. 1295b68e418cSStephen Smalley #endif 1296b68e418cSStephen Smalley 12971da177e4SLinus Torvalds /* Check whether a task is allowed to use a capability. */ 12981da177e4SLinus Torvalds static int task_has_capability(struct task_struct *tsk, 12991da177e4SLinus Torvalds int cap) 13001da177e4SLinus Torvalds { 13011da177e4SLinus Torvalds struct task_security_struct *tsec; 13021da177e4SLinus Torvalds struct avc_audit_data ad; 1303b68e418cSStephen Smalley u16 sclass; 1304b68e418cSStephen Smalley u32 av = CAP_TO_MASK(cap); 13051da177e4SLinus Torvalds 13061da177e4SLinus Torvalds tsec = tsk->security; 13071da177e4SLinus Torvalds 13081da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,CAP); 13091da177e4SLinus Torvalds ad.tsk = tsk; 13101da177e4SLinus Torvalds ad.u.cap = cap; 13111da177e4SLinus Torvalds 1312b68e418cSStephen Smalley switch (CAP_TO_INDEX(cap)) { 1313b68e418cSStephen Smalley case 0: 1314b68e418cSStephen Smalley sclass = SECCLASS_CAPABILITY; 1315b68e418cSStephen Smalley break; 1316b68e418cSStephen Smalley case 1: 1317b68e418cSStephen Smalley sclass = SECCLASS_CAPABILITY2; 1318b68e418cSStephen Smalley break; 1319b68e418cSStephen Smalley default: 1320b68e418cSStephen Smalley printk(KERN_ERR 1321b68e418cSStephen Smalley "SELinux: out of range capability %d\n", cap); 1322b68e418cSStephen Smalley BUG(); 1323b68e418cSStephen Smalley } 1324b68e418cSStephen Smalley return avc_has_perm(tsec->sid, tsec->sid, sclass, av, &ad); 13251da177e4SLinus Torvalds } 13261da177e4SLinus Torvalds 13271da177e4SLinus Torvalds /* Check whether a task is allowed to use a system operation. */ 13281da177e4SLinus Torvalds static int task_has_system(struct task_struct *tsk, 13291da177e4SLinus Torvalds u32 perms) 13301da177e4SLinus Torvalds { 13311da177e4SLinus Torvalds struct task_security_struct *tsec; 13321da177e4SLinus Torvalds 13331da177e4SLinus Torvalds tsec = tsk->security; 13341da177e4SLinus Torvalds 13351da177e4SLinus Torvalds return avc_has_perm(tsec->sid, SECINITSID_KERNEL, 13361da177e4SLinus Torvalds SECCLASS_SYSTEM, perms, NULL); 13371da177e4SLinus Torvalds } 13381da177e4SLinus Torvalds 13391da177e4SLinus Torvalds /* Check whether a task has a particular permission to an inode. 13401da177e4SLinus Torvalds The 'adp' parameter is optional and allows other audit 13411da177e4SLinus Torvalds data to be passed (e.g. the dentry). */ 13421da177e4SLinus Torvalds static int inode_has_perm(struct task_struct *tsk, 13431da177e4SLinus Torvalds struct inode *inode, 13441da177e4SLinus Torvalds u32 perms, 13451da177e4SLinus Torvalds struct avc_audit_data *adp) 13461da177e4SLinus Torvalds { 13471da177e4SLinus Torvalds struct task_security_struct *tsec; 13481da177e4SLinus Torvalds struct inode_security_struct *isec; 13491da177e4SLinus Torvalds struct avc_audit_data ad; 13501da177e4SLinus Torvalds 1351bbaca6c2SStephen Smalley if (unlikely (IS_PRIVATE (inode))) 1352bbaca6c2SStephen Smalley return 0; 1353bbaca6c2SStephen Smalley 13541da177e4SLinus Torvalds tsec = tsk->security; 13551da177e4SLinus Torvalds isec = inode->i_security; 13561da177e4SLinus Torvalds 13571da177e4SLinus Torvalds if (!adp) { 13581da177e4SLinus Torvalds adp = &ad; 13591da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 13601da177e4SLinus Torvalds ad.u.fs.inode = inode; 13611da177e4SLinus Torvalds } 13621da177e4SLinus Torvalds 13631da177e4SLinus Torvalds return avc_has_perm(tsec->sid, isec->sid, isec->sclass, perms, adp); 13641da177e4SLinus Torvalds } 13651da177e4SLinus Torvalds 13661da177e4SLinus Torvalds /* Same as inode_has_perm, but pass explicit audit data containing 13671da177e4SLinus Torvalds the dentry to help the auditing code to more easily generate the 13681da177e4SLinus Torvalds pathname if needed. */ 13691da177e4SLinus Torvalds static inline int dentry_has_perm(struct task_struct *tsk, 13701da177e4SLinus Torvalds struct vfsmount *mnt, 13711da177e4SLinus Torvalds struct dentry *dentry, 13721da177e4SLinus Torvalds u32 av) 13731da177e4SLinus Torvalds { 13741da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 13751da177e4SLinus Torvalds struct avc_audit_data ad; 13761da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,FS); 137744707fdfSJan Blunck ad.u.fs.path.mnt = mnt; 137844707fdfSJan Blunck ad.u.fs.path.dentry = dentry; 13791da177e4SLinus Torvalds return inode_has_perm(tsk, inode, av, &ad); 13801da177e4SLinus Torvalds } 13811da177e4SLinus Torvalds 13821da177e4SLinus Torvalds /* Check whether a task can use an open file descriptor to 13831da177e4SLinus Torvalds access an inode in a given way. Check access to the 13841da177e4SLinus Torvalds descriptor itself, and then use dentry_has_perm to 13851da177e4SLinus Torvalds check a particular permission to the file. 13861da177e4SLinus Torvalds Access to the descriptor is implicitly granted if it 13871da177e4SLinus Torvalds has the same SID as the process. If av is zero, then 13881da177e4SLinus Torvalds access to the file is not checked, e.g. for cases 13891da177e4SLinus Torvalds where only the descriptor is affected like seek. */ 1390858119e1SArjan van de Ven static int file_has_perm(struct task_struct *tsk, 13911da177e4SLinus Torvalds struct file *file, 13921da177e4SLinus Torvalds u32 av) 13931da177e4SLinus Torvalds { 13941da177e4SLinus Torvalds struct task_security_struct *tsec = tsk->security; 13951da177e4SLinus Torvalds struct file_security_struct *fsec = file->f_security; 139644707fdfSJan Blunck struct inode *inode = file->f_path.dentry->d_inode; 13971da177e4SLinus Torvalds struct avc_audit_data ad; 13981da177e4SLinus Torvalds int rc; 13991da177e4SLinus Torvalds 14001da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 140144707fdfSJan Blunck ad.u.fs.path = file->f_path; 14021da177e4SLinus Torvalds 14031da177e4SLinus Torvalds if (tsec->sid != fsec->sid) { 14041da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, fsec->sid, 14051da177e4SLinus Torvalds SECCLASS_FD, 14061da177e4SLinus Torvalds FD__USE, 14071da177e4SLinus Torvalds &ad); 14081da177e4SLinus Torvalds if (rc) 14091da177e4SLinus Torvalds return rc; 14101da177e4SLinus Torvalds } 14111da177e4SLinus Torvalds 14121da177e4SLinus Torvalds /* av is zero if only checking access to the descriptor. */ 14131da177e4SLinus Torvalds if (av) 14141da177e4SLinus Torvalds return inode_has_perm(tsk, inode, av, &ad); 14151da177e4SLinus Torvalds 14161da177e4SLinus Torvalds return 0; 14171da177e4SLinus Torvalds } 14181da177e4SLinus Torvalds 14191da177e4SLinus Torvalds /* Check whether a task can create a file. */ 14201da177e4SLinus Torvalds static int may_create(struct inode *dir, 14211da177e4SLinus Torvalds struct dentry *dentry, 14221da177e4SLinus Torvalds u16 tclass) 14231da177e4SLinus Torvalds { 14241da177e4SLinus Torvalds struct task_security_struct *tsec; 14251da177e4SLinus Torvalds struct inode_security_struct *dsec; 14261da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 14271da177e4SLinus Torvalds u32 newsid; 14281da177e4SLinus Torvalds struct avc_audit_data ad; 14291da177e4SLinus Torvalds int rc; 14301da177e4SLinus Torvalds 14311da177e4SLinus Torvalds tsec = current->security; 14321da177e4SLinus Torvalds dsec = dir->i_security; 14331da177e4SLinus Torvalds sbsec = dir->i_sb->s_security; 14341da177e4SLinus Torvalds 14351da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 143644707fdfSJan Blunck ad.u.fs.path.dentry = dentry; 14371da177e4SLinus Torvalds 14381da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, dsec->sid, SECCLASS_DIR, 14391da177e4SLinus Torvalds DIR__ADD_NAME | DIR__SEARCH, 14401da177e4SLinus Torvalds &ad); 14411da177e4SLinus Torvalds if (rc) 14421da177e4SLinus Torvalds return rc; 14431da177e4SLinus Torvalds 14441da177e4SLinus Torvalds if (tsec->create_sid && sbsec->behavior != SECURITY_FS_USE_MNTPOINT) { 14451da177e4SLinus Torvalds newsid = tsec->create_sid; 14461da177e4SLinus Torvalds } else { 14471da177e4SLinus Torvalds rc = security_transition_sid(tsec->sid, dsec->sid, tclass, 14481da177e4SLinus Torvalds &newsid); 14491da177e4SLinus Torvalds if (rc) 14501da177e4SLinus Torvalds return rc; 14511da177e4SLinus Torvalds } 14521da177e4SLinus Torvalds 14531da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, newsid, tclass, FILE__CREATE, &ad); 14541da177e4SLinus Torvalds if (rc) 14551da177e4SLinus Torvalds return rc; 14561da177e4SLinus Torvalds 14571da177e4SLinus Torvalds return avc_has_perm(newsid, sbsec->sid, 14581da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 14591da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, &ad); 14601da177e4SLinus Torvalds } 14611da177e4SLinus Torvalds 14624eb582cfSMichael LeMay /* Check whether a task can create a key. */ 14634eb582cfSMichael LeMay static int may_create_key(u32 ksid, 14644eb582cfSMichael LeMay struct task_struct *ctx) 14654eb582cfSMichael LeMay { 14664eb582cfSMichael LeMay struct task_security_struct *tsec; 14674eb582cfSMichael LeMay 14684eb582cfSMichael LeMay tsec = ctx->security; 14694eb582cfSMichael LeMay 14704eb582cfSMichael LeMay return avc_has_perm(tsec->sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL); 14714eb582cfSMichael LeMay } 14724eb582cfSMichael LeMay 14731da177e4SLinus Torvalds #define MAY_LINK 0 14741da177e4SLinus Torvalds #define MAY_UNLINK 1 14751da177e4SLinus Torvalds #define MAY_RMDIR 2 14761da177e4SLinus Torvalds 14771da177e4SLinus Torvalds /* Check whether a task can link, unlink, or rmdir a file/directory. */ 14781da177e4SLinus Torvalds static int may_link(struct inode *dir, 14791da177e4SLinus Torvalds struct dentry *dentry, 14801da177e4SLinus Torvalds int kind) 14811da177e4SLinus Torvalds 14821da177e4SLinus Torvalds { 14831da177e4SLinus Torvalds struct task_security_struct *tsec; 14841da177e4SLinus Torvalds struct inode_security_struct *dsec, *isec; 14851da177e4SLinus Torvalds struct avc_audit_data ad; 14861da177e4SLinus Torvalds u32 av; 14871da177e4SLinus Torvalds int rc; 14881da177e4SLinus Torvalds 14891da177e4SLinus Torvalds tsec = current->security; 14901da177e4SLinus Torvalds dsec = dir->i_security; 14911da177e4SLinus Torvalds isec = dentry->d_inode->i_security; 14921da177e4SLinus Torvalds 14931da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 149444707fdfSJan Blunck ad.u.fs.path.dentry = dentry; 14951da177e4SLinus Torvalds 14961da177e4SLinus Torvalds av = DIR__SEARCH; 14971da177e4SLinus Torvalds av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME); 14981da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, dsec->sid, SECCLASS_DIR, av, &ad); 14991da177e4SLinus Torvalds if (rc) 15001da177e4SLinus Torvalds return rc; 15011da177e4SLinus Torvalds 15021da177e4SLinus Torvalds switch (kind) { 15031da177e4SLinus Torvalds case MAY_LINK: 15041da177e4SLinus Torvalds av = FILE__LINK; 15051da177e4SLinus Torvalds break; 15061da177e4SLinus Torvalds case MAY_UNLINK: 15071da177e4SLinus Torvalds av = FILE__UNLINK; 15081da177e4SLinus Torvalds break; 15091da177e4SLinus Torvalds case MAY_RMDIR: 15101da177e4SLinus Torvalds av = DIR__RMDIR; 15111da177e4SLinus Torvalds break; 15121da177e4SLinus Torvalds default: 15131da177e4SLinus Torvalds printk(KERN_WARNING "may_link: unrecognized kind %d\n", kind); 15141da177e4SLinus Torvalds return 0; 15151da177e4SLinus Torvalds } 15161da177e4SLinus Torvalds 15171da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, isec->sclass, av, &ad); 15181da177e4SLinus Torvalds return rc; 15191da177e4SLinus Torvalds } 15201da177e4SLinus Torvalds 15211da177e4SLinus Torvalds static inline int may_rename(struct inode *old_dir, 15221da177e4SLinus Torvalds struct dentry *old_dentry, 15231da177e4SLinus Torvalds struct inode *new_dir, 15241da177e4SLinus Torvalds struct dentry *new_dentry) 15251da177e4SLinus Torvalds { 15261da177e4SLinus Torvalds struct task_security_struct *tsec; 15271da177e4SLinus Torvalds struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec; 15281da177e4SLinus Torvalds struct avc_audit_data ad; 15291da177e4SLinus Torvalds u32 av; 15301da177e4SLinus Torvalds int old_is_dir, new_is_dir; 15311da177e4SLinus Torvalds int rc; 15321da177e4SLinus Torvalds 15331da177e4SLinus Torvalds tsec = current->security; 15341da177e4SLinus Torvalds old_dsec = old_dir->i_security; 15351da177e4SLinus Torvalds old_isec = old_dentry->d_inode->i_security; 15361da177e4SLinus Torvalds old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 15371da177e4SLinus Torvalds new_dsec = new_dir->i_security; 15381da177e4SLinus Torvalds 15391da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 15401da177e4SLinus Torvalds 154144707fdfSJan Blunck ad.u.fs.path.dentry = old_dentry; 15421da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, old_dsec->sid, SECCLASS_DIR, 15431da177e4SLinus Torvalds DIR__REMOVE_NAME | DIR__SEARCH, &ad); 15441da177e4SLinus Torvalds if (rc) 15451da177e4SLinus Torvalds return rc; 15461da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, old_isec->sid, 15471da177e4SLinus Torvalds old_isec->sclass, FILE__RENAME, &ad); 15481da177e4SLinus Torvalds if (rc) 15491da177e4SLinus Torvalds return rc; 15501da177e4SLinus Torvalds if (old_is_dir && new_dir != old_dir) { 15511da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, old_isec->sid, 15521da177e4SLinus Torvalds old_isec->sclass, DIR__REPARENT, &ad); 15531da177e4SLinus Torvalds if (rc) 15541da177e4SLinus Torvalds return rc; 15551da177e4SLinus Torvalds } 15561da177e4SLinus Torvalds 155744707fdfSJan Blunck ad.u.fs.path.dentry = new_dentry; 15581da177e4SLinus Torvalds av = DIR__ADD_NAME | DIR__SEARCH; 15591da177e4SLinus Torvalds if (new_dentry->d_inode) 15601da177e4SLinus Torvalds av |= DIR__REMOVE_NAME; 15611da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, new_dsec->sid, SECCLASS_DIR, av, &ad); 15621da177e4SLinus Torvalds if (rc) 15631da177e4SLinus Torvalds return rc; 15641da177e4SLinus Torvalds if (new_dentry->d_inode) { 15651da177e4SLinus Torvalds new_isec = new_dentry->d_inode->i_security; 15661da177e4SLinus Torvalds new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode); 15671da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, new_isec->sid, 15681da177e4SLinus Torvalds new_isec->sclass, 15691da177e4SLinus Torvalds (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad); 15701da177e4SLinus Torvalds if (rc) 15711da177e4SLinus Torvalds return rc; 15721da177e4SLinus Torvalds } 15731da177e4SLinus Torvalds 15741da177e4SLinus Torvalds return 0; 15751da177e4SLinus Torvalds } 15761da177e4SLinus Torvalds 15771da177e4SLinus Torvalds /* Check whether a task can perform a filesystem operation. */ 15781da177e4SLinus Torvalds static int superblock_has_perm(struct task_struct *tsk, 15791da177e4SLinus Torvalds struct super_block *sb, 15801da177e4SLinus Torvalds u32 perms, 15811da177e4SLinus Torvalds struct avc_audit_data *ad) 15821da177e4SLinus Torvalds { 15831da177e4SLinus Torvalds struct task_security_struct *tsec; 15841da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 15851da177e4SLinus Torvalds 15861da177e4SLinus Torvalds tsec = tsk->security; 15871da177e4SLinus Torvalds sbsec = sb->s_security; 15881da177e4SLinus Torvalds return avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 15891da177e4SLinus Torvalds perms, ad); 15901da177e4SLinus Torvalds } 15911da177e4SLinus Torvalds 15921da177e4SLinus Torvalds /* Convert a Linux mode and permission mask to an access vector. */ 15931da177e4SLinus Torvalds static inline u32 file_mask_to_av(int mode, int mask) 15941da177e4SLinus Torvalds { 15951da177e4SLinus Torvalds u32 av = 0; 15961da177e4SLinus Torvalds 15971da177e4SLinus Torvalds if ((mode & S_IFMT) != S_IFDIR) { 15981da177e4SLinus Torvalds if (mask & MAY_EXEC) 15991da177e4SLinus Torvalds av |= FILE__EXECUTE; 16001da177e4SLinus Torvalds if (mask & MAY_READ) 16011da177e4SLinus Torvalds av |= FILE__READ; 16021da177e4SLinus Torvalds 16031da177e4SLinus Torvalds if (mask & MAY_APPEND) 16041da177e4SLinus Torvalds av |= FILE__APPEND; 16051da177e4SLinus Torvalds else if (mask & MAY_WRITE) 16061da177e4SLinus Torvalds av |= FILE__WRITE; 16071da177e4SLinus Torvalds 16081da177e4SLinus Torvalds } else { 16091da177e4SLinus Torvalds if (mask & MAY_EXEC) 16101da177e4SLinus Torvalds av |= DIR__SEARCH; 16111da177e4SLinus Torvalds if (mask & MAY_WRITE) 16121da177e4SLinus Torvalds av |= DIR__WRITE; 16131da177e4SLinus Torvalds if (mask & MAY_READ) 16141da177e4SLinus Torvalds av |= DIR__READ; 16151da177e4SLinus Torvalds } 16161da177e4SLinus Torvalds 16171da177e4SLinus Torvalds return av; 16181da177e4SLinus Torvalds } 16191da177e4SLinus Torvalds 16201da177e4SLinus Torvalds /* Convert a Linux file to an access vector. */ 16211da177e4SLinus Torvalds static inline u32 file_to_av(struct file *file) 16221da177e4SLinus Torvalds { 16231da177e4SLinus Torvalds u32 av = 0; 16241da177e4SLinus Torvalds 16251da177e4SLinus Torvalds if (file->f_mode & FMODE_READ) 16261da177e4SLinus Torvalds av |= FILE__READ; 16271da177e4SLinus Torvalds if (file->f_mode & FMODE_WRITE) { 16281da177e4SLinus Torvalds if (file->f_flags & O_APPEND) 16291da177e4SLinus Torvalds av |= FILE__APPEND; 16301da177e4SLinus Torvalds else 16311da177e4SLinus Torvalds av |= FILE__WRITE; 16321da177e4SLinus Torvalds } 16330794c66dSStephen Smalley if (!av) { 16340794c66dSStephen Smalley /* 16350794c66dSStephen Smalley * Special file opened with flags 3 for ioctl-only use. 16360794c66dSStephen Smalley */ 16370794c66dSStephen Smalley av = FILE__IOCTL; 16380794c66dSStephen Smalley } 16391da177e4SLinus Torvalds 16401da177e4SLinus Torvalds return av; 16411da177e4SLinus Torvalds } 16421da177e4SLinus Torvalds 16431da177e4SLinus Torvalds /* Hook functions begin here. */ 16441da177e4SLinus Torvalds 16451da177e4SLinus Torvalds static int selinux_ptrace(struct task_struct *parent, struct task_struct *child) 16461da177e4SLinus Torvalds { 16471da177e4SLinus Torvalds struct task_security_struct *psec = parent->security; 16481da177e4SLinus Torvalds struct task_security_struct *csec = child->security; 16491da177e4SLinus Torvalds int rc; 16501da177e4SLinus Torvalds 16511da177e4SLinus Torvalds rc = secondary_ops->ptrace(parent,child); 16521da177e4SLinus Torvalds if (rc) 16531da177e4SLinus Torvalds return rc; 16541da177e4SLinus Torvalds 16551da177e4SLinus Torvalds rc = task_has_perm(parent, child, PROCESS__PTRACE); 16561da177e4SLinus Torvalds /* Save the SID of the tracing process for later use in apply_creds. */ 1657341c2d80SStephen Smalley if (!(child->ptrace & PT_PTRACED) && !rc) 16581da177e4SLinus Torvalds csec->ptrace_sid = psec->sid; 16591da177e4SLinus Torvalds return rc; 16601da177e4SLinus Torvalds } 16611da177e4SLinus Torvalds 16621da177e4SLinus Torvalds static int selinux_capget(struct task_struct *target, kernel_cap_t *effective, 16631da177e4SLinus Torvalds kernel_cap_t *inheritable, kernel_cap_t *permitted) 16641da177e4SLinus Torvalds { 16651da177e4SLinus Torvalds int error; 16661da177e4SLinus Torvalds 16671da177e4SLinus Torvalds error = task_has_perm(current, target, PROCESS__GETCAP); 16681da177e4SLinus Torvalds if (error) 16691da177e4SLinus Torvalds return error; 16701da177e4SLinus Torvalds 16711da177e4SLinus Torvalds return secondary_ops->capget(target, effective, inheritable, permitted); 16721da177e4SLinus Torvalds } 16731da177e4SLinus Torvalds 16741da177e4SLinus Torvalds static int selinux_capset_check(struct task_struct *target, kernel_cap_t *effective, 16751da177e4SLinus Torvalds kernel_cap_t *inheritable, kernel_cap_t *permitted) 16761da177e4SLinus Torvalds { 16771da177e4SLinus Torvalds int error; 16781da177e4SLinus Torvalds 16791da177e4SLinus Torvalds error = secondary_ops->capset_check(target, effective, inheritable, permitted); 16801da177e4SLinus Torvalds if (error) 16811da177e4SLinus Torvalds return error; 16821da177e4SLinus Torvalds 16831da177e4SLinus Torvalds return task_has_perm(current, target, PROCESS__SETCAP); 16841da177e4SLinus Torvalds } 16851da177e4SLinus Torvalds 16861da177e4SLinus Torvalds static void selinux_capset_set(struct task_struct *target, kernel_cap_t *effective, 16871da177e4SLinus Torvalds kernel_cap_t *inheritable, kernel_cap_t *permitted) 16881da177e4SLinus Torvalds { 16891da177e4SLinus Torvalds secondary_ops->capset_set(target, effective, inheritable, permitted); 16901da177e4SLinus Torvalds } 16911da177e4SLinus Torvalds 16921da177e4SLinus Torvalds static int selinux_capable(struct task_struct *tsk, int cap) 16931da177e4SLinus Torvalds { 16941da177e4SLinus Torvalds int rc; 16951da177e4SLinus Torvalds 16961da177e4SLinus Torvalds rc = secondary_ops->capable(tsk, cap); 16971da177e4SLinus Torvalds if (rc) 16981da177e4SLinus Torvalds return rc; 16991da177e4SLinus Torvalds 17001da177e4SLinus Torvalds return task_has_capability(tsk,cap); 17011da177e4SLinus Torvalds } 17021da177e4SLinus Torvalds 17033fbfa981SEric W. Biederman static int selinux_sysctl_get_sid(ctl_table *table, u16 tclass, u32 *sid) 17043fbfa981SEric W. Biederman { 17053fbfa981SEric W. Biederman int buflen, rc; 17063fbfa981SEric W. Biederman char *buffer, *path, *end; 17073fbfa981SEric W. Biederman 17083fbfa981SEric W. Biederman rc = -ENOMEM; 17093fbfa981SEric W. Biederman buffer = (char*)__get_free_page(GFP_KERNEL); 17103fbfa981SEric W. Biederman if (!buffer) 17113fbfa981SEric W. Biederman goto out; 17123fbfa981SEric W. Biederman 17133fbfa981SEric W. Biederman buflen = PAGE_SIZE; 17143fbfa981SEric W. Biederman end = buffer+buflen; 17153fbfa981SEric W. Biederman *--end = '\0'; 17163fbfa981SEric W. Biederman buflen--; 17173fbfa981SEric W. Biederman path = end-1; 17183fbfa981SEric W. Biederman *path = '/'; 17193fbfa981SEric W. Biederman while (table) { 17203fbfa981SEric W. Biederman const char *name = table->procname; 17213fbfa981SEric W. Biederman size_t namelen = strlen(name); 17223fbfa981SEric W. Biederman buflen -= namelen + 1; 17233fbfa981SEric W. Biederman if (buflen < 0) 17243fbfa981SEric W. Biederman goto out_free; 17253fbfa981SEric W. Biederman end -= namelen; 17263fbfa981SEric W. Biederman memcpy(end, name, namelen); 17273fbfa981SEric W. Biederman *--end = '/'; 17283fbfa981SEric W. Biederman path = end; 17293fbfa981SEric W. Biederman table = table->parent; 17303fbfa981SEric W. Biederman } 1731b599fdfdSEric W. Biederman buflen -= 4; 1732b599fdfdSEric W. Biederman if (buflen < 0) 1733b599fdfdSEric W. Biederman goto out_free; 1734b599fdfdSEric W. Biederman end -= 4; 1735b599fdfdSEric W. Biederman memcpy(end, "/sys", 4); 1736b599fdfdSEric W. Biederman path = end; 17373fbfa981SEric W. Biederman rc = security_genfs_sid("proc", path, tclass, sid); 17383fbfa981SEric W. Biederman out_free: 17393fbfa981SEric W. Biederman free_page((unsigned long)buffer); 17403fbfa981SEric W. Biederman out: 17413fbfa981SEric W. Biederman return rc; 17423fbfa981SEric W. Biederman } 17433fbfa981SEric W. Biederman 17441da177e4SLinus Torvalds static int selinux_sysctl(ctl_table *table, int op) 17451da177e4SLinus Torvalds { 17461da177e4SLinus Torvalds int error = 0; 17471da177e4SLinus Torvalds u32 av; 17481da177e4SLinus Torvalds struct task_security_struct *tsec; 17491da177e4SLinus Torvalds u32 tsid; 17501da177e4SLinus Torvalds int rc; 17511da177e4SLinus Torvalds 17521da177e4SLinus Torvalds rc = secondary_ops->sysctl(table, op); 17531da177e4SLinus Torvalds if (rc) 17541da177e4SLinus Torvalds return rc; 17551da177e4SLinus Torvalds 17561da177e4SLinus Torvalds tsec = current->security; 17571da177e4SLinus Torvalds 17583fbfa981SEric W. Biederman rc = selinux_sysctl_get_sid(table, (op == 0001) ? 17591da177e4SLinus Torvalds SECCLASS_DIR : SECCLASS_FILE, &tsid); 17601da177e4SLinus Torvalds if (rc) { 17611da177e4SLinus Torvalds /* Default to the well-defined sysctl SID. */ 17621da177e4SLinus Torvalds tsid = SECINITSID_SYSCTL; 17631da177e4SLinus Torvalds } 17641da177e4SLinus Torvalds 17651da177e4SLinus Torvalds /* The op values are "defined" in sysctl.c, thereby creating 17661da177e4SLinus Torvalds * a bad coupling between this module and sysctl.c */ 17671da177e4SLinus Torvalds if(op == 001) { 17681da177e4SLinus Torvalds error = avc_has_perm(tsec->sid, tsid, 17691da177e4SLinus Torvalds SECCLASS_DIR, DIR__SEARCH, NULL); 17701da177e4SLinus Torvalds } else { 17711da177e4SLinus Torvalds av = 0; 17721da177e4SLinus Torvalds if (op & 004) 17731da177e4SLinus Torvalds av |= FILE__READ; 17741da177e4SLinus Torvalds if (op & 002) 17751da177e4SLinus Torvalds av |= FILE__WRITE; 17761da177e4SLinus Torvalds if (av) 17771da177e4SLinus Torvalds error = avc_has_perm(tsec->sid, tsid, 17781da177e4SLinus Torvalds SECCLASS_FILE, av, NULL); 17791da177e4SLinus Torvalds } 17801da177e4SLinus Torvalds 17811da177e4SLinus Torvalds return error; 17821da177e4SLinus Torvalds } 17831da177e4SLinus Torvalds 17841da177e4SLinus Torvalds static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb) 17851da177e4SLinus Torvalds { 17861da177e4SLinus Torvalds int rc = 0; 17871da177e4SLinus Torvalds 17881da177e4SLinus Torvalds if (!sb) 17891da177e4SLinus Torvalds return 0; 17901da177e4SLinus Torvalds 17911da177e4SLinus Torvalds switch (cmds) { 17921da177e4SLinus Torvalds case Q_SYNC: 17931da177e4SLinus Torvalds case Q_QUOTAON: 17941da177e4SLinus Torvalds case Q_QUOTAOFF: 17951da177e4SLinus Torvalds case Q_SETINFO: 17961da177e4SLinus Torvalds case Q_SETQUOTA: 17971da177e4SLinus Torvalds rc = superblock_has_perm(current, 17981da177e4SLinus Torvalds sb, 17991da177e4SLinus Torvalds FILESYSTEM__QUOTAMOD, NULL); 18001da177e4SLinus Torvalds break; 18011da177e4SLinus Torvalds case Q_GETFMT: 18021da177e4SLinus Torvalds case Q_GETINFO: 18031da177e4SLinus Torvalds case Q_GETQUOTA: 18041da177e4SLinus Torvalds rc = superblock_has_perm(current, 18051da177e4SLinus Torvalds sb, 18061da177e4SLinus Torvalds FILESYSTEM__QUOTAGET, NULL); 18071da177e4SLinus Torvalds break; 18081da177e4SLinus Torvalds default: 18091da177e4SLinus Torvalds rc = 0; /* let the kernel handle invalid cmds */ 18101da177e4SLinus Torvalds break; 18111da177e4SLinus Torvalds } 18121da177e4SLinus Torvalds return rc; 18131da177e4SLinus Torvalds } 18141da177e4SLinus Torvalds 18151da177e4SLinus Torvalds static int selinux_quota_on(struct dentry *dentry) 18161da177e4SLinus Torvalds { 18171da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__QUOTAON); 18181da177e4SLinus Torvalds } 18191da177e4SLinus Torvalds 18201da177e4SLinus Torvalds static int selinux_syslog(int type) 18211da177e4SLinus Torvalds { 18221da177e4SLinus Torvalds int rc; 18231da177e4SLinus Torvalds 18241da177e4SLinus Torvalds rc = secondary_ops->syslog(type); 18251da177e4SLinus Torvalds if (rc) 18261da177e4SLinus Torvalds return rc; 18271da177e4SLinus Torvalds 18281da177e4SLinus Torvalds switch (type) { 18291da177e4SLinus Torvalds case 3: /* Read last kernel messages */ 18301da177e4SLinus Torvalds case 10: /* Return size of the log buffer */ 18311da177e4SLinus Torvalds rc = task_has_system(current, SYSTEM__SYSLOG_READ); 18321da177e4SLinus Torvalds break; 18331da177e4SLinus Torvalds case 6: /* Disable logging to console */ 18341da177e4SLinus Torvalds case 7: /* Enable logging to console */ 18351da177e4SLinus Torvalds case 8: /* Set level of messages printed to console */ 18361da177e4SLinus Torvalds rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE); 18371da177e4SLinus Torvalds break; 18381da177e4SLinus Torvalds case 0: /* Close log */ 18391da177e4SLinus Torvalds case 1: /* Open log */ 18401da177e4SLinus Torvalds case 2: /* Read from log */ 18411da177e4SLinus Torvalds case 4: /* Read/clear last kernel messages */ 18421da177e4SLinus Torvalds case 5: /* Clear ring buffer */ 18431da177e4SLinus Torvalds default: 18441da177e4SLinus Torvalds rc = task_has_system(current, SYSTEM__SYSLOG_MOD); 18451da177e4SLinus Torvalds break; 18461da177e4SLinus Torvalds } 18471da177e4SLinus Torvalds return rc; 18481da177e4SLinus Torvalds } 18491da177e4SLinus Torvalds 18501da177e4SLinus Torvalds /* 18511da177e4SLinus Torvalds * Check that a process has enough memory to allocate a new virtual 18521da177e4SLinus Torvalds * mapping. 0 means there is enough memory for the allocation to 18531da177e4SLinus Torvalds * succeed and -ENOMEM implies there is not. 18541da177e4SLinus Torvalds * 18551da177e4SLinus Torvalds * Note that secondary_ops->capable and task_has_perm_noaudit return 0 18561da177e4SLinus Torvalds * if the capability is granted, but __vm_enough_memory requires 1 if 18571da177e4SLinus Torvalds * the capability is granted. 18581da177e4SLinus Torvalds * 18591da177e4SLinus Torvalds * Do not audit the selinux permission check, as this is applied to all 18601da177e4SLinus Torvalds * processes that allocate mappings. 18611da177e4SLinus Torvalds */ 186234b4e4aaSAlan Cox static int selinux_vm_enough_memory(struct mm_struct *mm, long pages) 18631da177e4SLinus Torvalds { 18641da177e4SLinus Torvalds int rc, cap_sys_admin = 0; 18651da177e4SLinus Torvalds struct task_security_struct *tsec = current->security; 18661da177e4SLinus Torvalds 18671da177e4SLinus Torvalds rc = secondary_ops->capable(current, CAP_SYS_ADMIN); 18681da177e4SLinus Torvalds if (rc == 0) 18691da177e4SLinus Torvalds rc = avc_has_perm_noaudit(tsec->sid, tsec->sid, 18701da177e4SLinus Torvalds SECCLASS_CAPABILITY, 18711da177e4SLinus Torvalds CAP_TO_MASK(CAP_SYS_ADMIN), 18722c3c05dbSStephen Smalley 0, 18731da177e4SLinus Torvalds NULL); 18741da177e4SLinus Torvalds 18751da177e4SLinus Torvalds if (rc == 0) 18761da177e4SLinus Torvalds cap_sys_admin = 1; 18771da177e4SLinus Torvalds 187834b4e4aaSAlan Cox return __vm_enough_memory(mm, pages, cap_sys_admin); 18791da177e4SLinus Torvalds } 18801da177e4SLinus Torvalds 18811da177e4SLinus Torvalds /* binprm security operations */ 18821da177e4SLinus Torvalds 18831da177e4SLinus Torvalds static int selinux_bprm_alloc_security(struct linux_binprm *bprm) 18841da177e4SLinus Torvalds { 18851da177e4SLinus Torvalds struct bprm_security_struct *bsec; 18861da177e4SLinus Torvalds 188789d155efSJames Morris bsec = kzalloc(sizeof(struct bprm_security_struct), GFP_KERNEL); 18881da177e4SLinus Torvalds if (!bsec) 18891da177e4SLinus Torvalds return -ENOMEM; 18901da177e4SLinus Torvalds 18911da177e4SLinus Torvalds bsec->bprm = bprm; 18921da177e4SLinus Torvalds bsec->sid = SECINITSID_UNLABELED; 18931da177e4SLinus Torvalds bsec->set = 0; 18941da177e4SLinus Torvalds 18951da177e4SLinus Torvalds bprm->security = bsec; 18961da177e4SLinus Torvalds return 0; 18971da177e4SLinus Torvalds } 18981da177e4SLinus Torvalds 18991da177e4SLinus Torvalds static int selinux_bprm_set_security(struct linux_binprm *bprm) 19001da177e4SLinus Torvalds { 19011da177e4SLinus Torvalds struct task_security_struct *tsec; 19023d5ff529SJosef Sipek struct inode *inode = bprm->file->f_path.dentry->d_inode; 19031da177e4SLinus Torvalds struct inode_security_struct *isec; 19041da177e4SLinus Torvalds struct bprm_security_struct *bsec; 19051da177e4SLinus Torvalds u32 newsid; 19061da177e4SLinus Torvalds struct avc_audit_data ad; 19071da177e4SLinus Torvalds int rc; 19081da177e4SLinus Torvalds 19091da177e4SLinus Torvalds rc = secondary_ops->bprm_set_security(bprm); 19101da177e4SLinus Torvalds if (rc) 19111da177e4SLinus Torvalds return rc; 19121da177e4SLinus Torvalds 19131da177e4SLinus Torvalds bsec = bprm->security; 19141da177e4SLinus Torvalds 19151da177e4SLinus Torvalds if (bsec->set) 19161da177e4SLinus Torvalds return 0; 19171da177e4SLinus Torvalds 19181da177e4SLinus Torvalds tsec = current->security; 19191da177e4SLinus Torvalds isec = inode->i_security; 19201da177e4SLinus Torvalds 19211da177e4SLinus Torvalds /* Default to the current task SID. */ 19221da177e4SLinus Torvalds bsec->sid = tsec->sid; 19231da177e4SLinus Torvalds 192428eba5bfSMichael LeMay /* Reset fs, key, and sock SIDs on execve. */ 19251da177e4SLinus Torvalds tsec->create_sid = 0; 192628eba5bfSMichael LeMay tsec->keycreate_sid = 0; 192742c3e03eSEric Paris tsec->sockcreate_sid = 0; 19281da177e4SLinus Torvalds 19291da177e4SLinus Torvalds if (tsec->exec_sid) { 19301da177e4SLinus Torvalds newsid = tsec->exec_sid; 19311da177e4SLinus Torvalds /* Reset exec SID on execve. */ 19321da177e4SLinus Torvalds tsec->exec_sid = 0; 19331da177e4SLinus Torvalds } else { 19341da177e4SLinus Torvalds /* Check for a default transition on this program. */ 19351da177e4SLinus Torvalds rc = security_transition_sid(tsec->sid, isec->sid, 19361da177e4SLinus Torvalds SECCLASS_PROCESS, &newsid); 19371da177e4SLinus Torvalds if (rc) 19381da177e4SLinus Torvalds return rc; 19391da177e4SLinus Torvalds } 19401da177e4SLinus Torvalds 19411da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 194244707fdfSJan Blunck ad.u.fs.path = bprm->file->f_path; 19431da177e4SLinus Torvalds 19443d5ff529SJosef Sipek if (bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) 19451da177e4SLinus Torvalds newsid = tsec->sid; 19461da177e4SLinus Torvalds 19471da177e4SLinus Torvalds if (tsec->sid == newsid) { 19481da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, 19491da177e4SLinus Torvalds SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad); 19501da177e4SLinus Torvalds if (rc) 19511da177e4SLinus Torvalds return rc; 19521da177e4SLinus Torvalds } else { 19531da177e4SLinus Torvalds /* Check permissions for the transition. */ 19541da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, newsid, 19551da177e4SLinus Torvalds SECCLASS_PROCESS, PROCESS__TRANSITION, &ad); 19561da177e4SLinus Torvalds if (rc) 19571da177e4SLinus Torvalds return rc; 19581da177e4SLinus Torvalds 19591da177e4SLinus Torvalds rc = avc_has_perm(newsid, isec->sid, 19601da177e4SLinus Torvalds SECCLASS_FILE, FILE__ENTRYPOINT, &ad); 19611da177e4SLinus Torvalds if (rc) 19621da177e4SLinus Torvalds return rc; 19631da177e4SLinus Torvalds 19641da177e4SLinus Torvalds /* Clear any possibly unsafe personality bits on exec: */ 19651da177e4SLinus Torvalds current->personality &= ~PER_CLEAR_ON_SETID; 19661da177e4SLinus Torvalds 19671da177e4SLinus Torvalds /* Set the security field to the new SID. */ 19681da177e4SLinus Torvalds bsec->sid = newsid; 19691da177e4SLinus Torvalds } 19701da177e4SLinus Torvalds 19711da177e4SLinus Torvalds bsec->set = 1; 19721da177e4SLinus Torvalds return 0; 19731da177e4SLinus Torvalds } 19741da177e4SLinus Torvalds 19751da177e4SLinus Torvalds static int selinux_bprm_check_security (struct linux_binprm *bprm) 19761da177e4SLinus Torvalds { 19771da177e4SLinus Torvalds return secondary_ops->bprm_check_security(bprm); 19781da177e4SLinus Torvalds } 19791da177e4SLinus Torvalds 19801da177e4SLinus Torvalds 19811da177e4SLinus Torvalds static int selinux_bprm_secureexec (struct linux_binprm *bprm) 19821da177e4SLinus Torvalds { 19831da177e4SLinus Torvalds struct task_security_struct *tsec = current->security; 19841da177e4SLinus Torvalds int atsecure = 0; 19851da177e4SLinus Torvalds 19861da177e4SLinus Torvalds if (tsec->osid != tsec->sid) { 19871da177e4SLinus Torvalds /* Enable secure mode for SIDs transitions unless 19881da177e4SLinus Torvalds the noatsecure permission is granted between 19891da177e4SLinus Torvalds the two SIDs, i.e. ahp returns 0. */ 19901da177e4SLinus Torvalds atsecure = avc_has_perm(tsec->osid, tsec->sid, 19911da177e4SLinus Torvalds SECCLASS_PROCESS, 19921da177e4SLinus Torvalds PROCESS__NOATSECURE, NULL); 19931da177e4SLinus Torvalds } 19941da177e4SLinus Torvalds 19951da177e4SLinus Torvalds return (atsecure || secondary_ops->bprm_secureexec(bprm)); 19961da177e4SLinus Torvalds } 19971da177e4SLinus Torvalds 19981da177e4SLinus Torvalds static void selinux_bprm_free_security(struct linux_binprm *bprm) 19991da177e4SLinus Torvalds { 20009a5f04bfSJesper Juhl kfree(bprm->security); 20011da177e4SLinus Torvalds bprm->security = NULL; 20021da177e4SLinus Torvalds } 20031da177e4SLinus Torvalds 20041da177e4SLinus Torvalds extern struct vfsmount *selinuxfs_mount; 20051da177e4SLinus Torvalds extern struct dentry *selinux_null; 20061da177e4SLinus Torvalds 20071da177e4SLinus Torvalds /* Derived from fs/exec.c:flush_old_files. */ 20081da177e4SLinus Torvalds static inline void flush_unauthorized_files(struct files_struct * files) 20091da177e4SLinus Torvalds { 20101da177e4SLinus Torvalds struct avc_audit_data ad; 20111da177e4SLinus Torvalds struct file *file, *devnull = NULL; 2012b20c8122SStephen Smalley struct tty_struct *tty; 2013badf1662SDipankar Sarma struct fdtable *fdt; 20141da177e4SLinus Torvalds long j = -1; 201524ec839cSPeter Zijlstra int drop_tty = 0; 20161da177e4SLinus Torvalds 2017b20c8122SStephen Smalley mutex_lock(&tty_mutex); 201824ec839cSPeter Zijlstra tty = get_current_tty(); 20191da177e4SLinus Torvalds if (tty) { 20201da177e4SLinus Torvalds file_list_lock(); 20212f512016SEric Dumazet file = list_entry(tty->tty_files.next, typeof(*file), f_u.fu_list); 20221da177e4SLinus Torvalds if (file) { 20231da177e4SLinus Torvalds /* Revalidate access to controlling tty. 20241da177e4SLinus Torvalds Use inode_has_perm on the tty inode directly rather 20251da177e4SLinus Torvalds than using file_has_perm, as this particular open 20261da177e4SLinus Torvalds file may belong to another process and we are only 20271da177e4SLinus Torvalds interested in the inode-based check here. */ 20283d5ff529SJosef Sipek struct inode *inode = file->f_path.dentry->d_inode; 20291da177e4SLinus Torvalds if (inode_has_perm(current, inode, 20301da177e4SLinus Torvalds FILE__READ | FILE__WRITE, NULL)) { 203124ec839cSPeter Zijlstra drop_tty = 1; 20321da177e4SLinus Torvalds } 20331da177e4SLinus Torvalds } 20341da177e4SLinus Torvalds file_list_unlock(); 20351da177e4SLinus Torvalds } 2036b20c8122SStephen Smalley mutex_unlock(&tty_mutex); 203798a27ba4SEric W. Biederman /* Reset controlling tty. */ 203898a27ba4SEric W. Biederman if (drop_tty) 203998a27ba4SEric W. Biederman no_tty(); 20401da177e4SLinus Torvalds 20411da177e4SLinus Torvalds /* Revalidate access to inherited open files. */ 20421da177e4SLinus Torvalds 20431da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,FS); 20441da177e4SLinus Torvalds 20451da177e4SLinus Torvalds spin_lock(&files->file_lock); 20461da177e4SLinus Torvalds for (;;) { 20471da177e4SLinus Torvalds unsigned long set, i; 20481da177e4SLinus Torvalds int fd; 20491da177e4SLinus Torvalds 20501da177e4SLinus Torvalds j++; 20511da177e4SLinus Torvalds i = j * __NFDBITS; 2052badf1662SDipankar Sarma fdt = files_fdtable(files); 2053bbea9f69SVadim Lobanov if (i >= fdt->max_fds) 20541da177e4SLinus Torvalds break; 2055badf1662SDipankar Sarma set = fdt->open_fds->fds_bits[j]; 20561da177e4SLinus Torvalds if (!set) 20571da177e4SLinus Torvalds continue; 20581da177e4SLinus Torvalds spin_unlock(&files->file_lock); 20591da177e4SLinus Torvalds for ( ; set ; i++,set >>= 1) { 20601da177e4SLinus Torvalds if (set & 1) { 20611da177e4SLinus Torvalds file = fget(i); 20621da177e4SLinus Torvalds if (!file) 20631da177e4SLinus Torvalds continue; 20641da177e4SLinus Torvalds if (file_has_perm(current, 20651da177e4SLinus Torvalds file, 20661da177e4SLinus Torvalds file_to_av(file))) { 20671da177e4SLinus Torvalds sys_close(i); 20681da177e4SLinus Torvalds fd = get_unused_fd(); 20691da177e4SLinus Torvalds if (fd != i) { 20701da177e4SLinus Torvalds if (fd >= 0) 20711da177e4SLinus Torvalds put_unused_fd(fd); 20721da177e4SLinus Torvalds fput(file); 20731da177e4SLinus Torvalds continue; 20741da177e4SLinus Torvalds } 20751da177e4SLinus Torvalds if (devnull) { 2076095975daSNick Piggin get_file(devnull); 20771da177e4SLinus Torvalds } else { 20781da177e4SLinus Torvalds devnull = dentry_open(dget(selinux_null), mntget(selinuxfs_mount), O_RDWR); 2079fc5d81e6SAkinobu Mita if (IS_ERR(devnull)) { 2080fc5d81e6SAkinobu Mita devnull = NULL; 20811da177e4SLinus Torvalds put_unused_fd(fd); 20821da177e4SLinus Torvalds fput(file); 20831da177e4SLinus Torvalds continue; 20841da177e4SLinus Torvalds } 20851da177e4SLinus Torvalds } 20861da177e4SLinus Torvalds fd_install(fd, devnull); 20871da177e4SLinus Torvalds } 20881da177e4SLinus Torvalds fput(file); 20891da177e4SLinus Torvalds } 20901da177e4SLinus Torvalds } 20911da177e4SLinus Torvalds spin_lock(&files->file_lock); 20921da177e4SLinus Torvalds 20931da177e4SLinus Torvalds } 20941da177e4SLinus Torvalds spin_unlock(&files->file_lock); 20951da177e4SLinus Torvalds } 20961da177e4SLinus Torvalds 20971da177e4SLinus Torvalds static void selinux_bprm_apply_creds(struct linux_binprm *bprm, int unsafe) 20981da177e4SLinus Torvalds { 20991da177e4SLinus Torvalds struct task_security_struct *tsec; 21001da177e4SLinus Torvalds struct bprm_security_struct *bsec; 21011da177e4SLinus Torvalds u32 sid; 21021da177e4SLinus Torvalds int rc; 21031da177e4SLinus Torvalds 21041da177e4SLinus Torvalds secondary_ops->bprm_apply_creds(bprm, unsafe); 21051da177e4SLinus Torvalds 21061da177e4SLinus Torvalds tsec = current->security; 21071da177e4SLinus Torvalds 21081da177e4SLinus Torvalds bsec = bprm->security; 21091da177e4SLinus Torvalds sid = bsec->sid; 21101da177e4SLinus Torvalds 21111da177e4SLinus Torvalds tsec->osid = tsec->sid; 21121da177e4SLinus Torvalds bsec->unsafe = 0; 21131da177e4SLinus Torvalds if (tsec->sid != sid) { 21141da177e4SLinus Torvalds /* Check for shared state. If not ok, leave SID 21151da177e4SLinus Torvalds unchanged and kill. */ 21161da177e4SLinus Torvalds if (unsafe & LSM_UNSAFE_SHARE) { 21171da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS, 21181da177e4SLinus Torvalds PROCESS__SHARE, NULL); 21191da177e4SLinus Torvalds if (rc) { 21201da177e4SLinus Torvalds bsec->unsafe = 1; 21211da177e4SLinus Torvalds return; 21221da177e4SLinus Torvalds } 21231da177e4SLinus Torvalds } 21241da177e4SLinus Torvalds 21251da177e4SLinus Torvalds /* Check for ptracing, and update the task SID if ok. 21261da177e4SLinus Torvalds Otherwise, leave SID unchanged and kill. */ 21271da177e4SLinus Torvalds if (unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) { 21281da177e4SLinus Torvalds rc = avc_has_perm(tsec->ptrace_sid, sid, 21291da177e4SLinus Torvalds SECCLASS_PROCESS, PROCESS__PTRACE, 21301da177e4SLinus Torvalds NULL); 21311da177e4SLinus Torvalds if (rc) { 21321da177e4SLinus Torvalds bsec->unsafe = 1; 21331da177e4SLinus Torvalds return; 21341da177e4SLinus Torvalds } 21351da177e4SLinus Torvalds } 21361da177e4SLinus Torvalds tsec->sid = sid; 21371da177e4SLinus Torvalds } 21381da177e4SLinus Torvalds } 21391da177e4SLinus Torvalds 21401da177e4SLinus Torvalds /* 21411da177e4SLinus Torvalds * called after apply_creds without the task lock held 21421da177e4SLinus Torvalds */ 21431da177e4SLinus Torvalds static void selinux_bprm_post_apply_creds(struct linux_binprm *bprm) 21441da177e4SLinus Torvalds { 21451da177e4SLinus Torvalds struct task_security_struct *tsec; 21461da177e4SLinus Torvalds struct rlimit *rlim, *initrlim; 21471da177e4SLinus Torvalds struct itimerval itimer; 21481da177e4SLinus Torvalds struct bprm_security_struct *bsec; 21491da177e4SLinus Torvalds int rc, i; 21501da177e4SLinus Torvalds 21511da177e4SLinus Torvalds tsec = current->security; 21521da177e4SLinus Torvalds bsec = bprm->security; 21531da177e4SLinus Torvalds 21541da177e4SLinus Torvalds if (bsec->unsafe) { 21551da177e4SLinus Torvalds force_sig_specific(SIGKILL, current); 21561da177e4SLinus Torvalds return; 21571da177e4SLinus Torvalds } 21581da177e4SLinus Torvalds if (tsec->osid == tsec->sid) 21591da177e4SLinus Torvalds return; 21601da177e4SLinus Torvalds 21611da177e4SLinus Torvalds /* Close files for which the new task SID is not authorized. */ 21621da177e4SLinus Torvalds flush_unauthorized_files(current->files); 21631da177e4SLinus Torvalds 21641da177e4SLinus Torvalds /* Check whether the new SID can inherit signal state 21651da177e4SLinus Torvalds from the old SID. If not, clear itimers to avoid 21661da177e4SLinus Torvalds subsequent signal generation and flush and unblock 21671da177e4SLinus Torvalds signals. This must occur _after_ the task SID has 21681da177e4SLinus Torvalds been updated so that any kill done after the flush 21691da177e4SLinus Torvalds will be checked against the new SID. */ 21701da177e4SLinus Torvalds rc = avc_has_perm(tsec->osid, tsec->sid, SECCLASS_PROCESS, 21711da177e4SLinus Torvalds PROCESS__SIGINH, NULL); 21721da177e4SLinus Torvalds if (rc) { 21731da177e4SLinus Torvalds memset(&itimer, 0, sizeof itimer); 21741da177e4SLinus Torvalds for (i = 0; i < 3; i++) 21751da177e4SLinus Torvalds do_setitimer(i, &itimer, NULL); 21761da177e4SLinus Torvalds flush_signals(current); 21771da177e4SLinus Torvalds spin_lock_irq(¤t->sighand->siglock); 21781da177e4SLinus Torvalds flush_signal_handlers(current, 1); 21791da177e4SLinus Torvalds sigemptyset(¤t->blocked); 21801da177e4SLinus Torvalds recalc_sigpending(); 21811da177e4SLinus Torvalds spin_unlock_irq(¤t->sighand->siglock); 21821da177e4SLinus Torvalds } 21831da177e4SLinus Torvalds 21844ac212adSStephen Smalley /* Always clear parent death signal on SID transitions. */ 21854ac212adSStephen Smalley current->pdeath_signal = 0; 21864ac212adSStephen Smalley 21871da177e4SLinus Torvalds /* Check whether the new SID can inherit resource limits 21881da177e4SLinus Torvalds from the old SID. If not, reset all soft limits to 21891da177e4SLinus Torvalds the lower of the current task's hard limit and the init 21901da177e4SLinus Torvalds task's soft limit. Note that the setting of hard limits 21911da177e4SLinus Torvalds (even to lower them) can be controlled by the setrlimit 21921da177e4SLinus Torvalds check. The inclusion of the init task's soft limit into 21931da177e4SLinus Torvalds the computation is to avoid resetting soft limits higher 21941da177e4SLinus Torvalds than the default soft limit for cases where the default 21951da177e4SLinus Torvalds is lower than the hard limit, e.g. RLIMIT_CORE or 21961da177e4SLinus Torvalds RLIMIT_STACK.*/ 21971da177e4SLinus Torvalds rc = avc_has_perm(tsec->osid, tsec->sid, SECCLASS_PROCESS, 21981da177e4SLinus Torvalds PROCESS__RLIMITINH, NULL); 21991da177e4SLinus Torvalds if (rc) { 22001da177e4SLinus Torvalds for (i = 0; i < RLIM_NLIMITS; i++) { 22011da177e4SLinus Torvalds rlim = current->signal->rlim + i; 22021da177e4SLinus Torvalds initrlim = init_task.signal->rlim+i; 22031da177e4SLinus Torvalds rlim->rlim_cur = min(rlim->rlim_max,initrlim->rlim_cur); 22041da177e4SLinus Torvalds } 22051da177e4SLinus Torvalds if (current->signal->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) { 22061da177e4SLinus Torvalds /* 22071da177e4SLinus Torvalds * This will cause RLIMIT_CPU calculations 22081da177e4SLinus Torvalds * to be refigured. 22091da177e4SLinus Torvalds */ 22101da177e4SLinus Torvalds current->it_prof_expires = jiffies_to_cputime(1); 22111da177e4SLinus Torvalds } 22121da177e4SLinus Torvalds } 22131da177e4SLinus Torvalds 22141da177e4SLinus Torvalds /* Wake up the parent if it is waiting so that it can 22151da177e4SLinus Torvalds recheck wait permission to the new task SID. */ 22161da177e4SLinus Torvalds wake_up_interruptible(¤t->parent->signal->wait_chldexit); 22171da177e4SLinus Torvalds } 22181da177e4SLinus Torvalds 22191da177e4SLinus Torvalds /* superblock security operations */ 22201da177e4SLinus Torvalds 22211da177e4SLinus Torvalds static int selinux_sb_alloc_security(struct super_block *sb) 22221da177e4SLinus Torvalds { 22231da177e4SLinus Torvalds return superblock_alloc_security(sb); 22241da177e4SLinus Torvalds } 22251da177e4SLinus Torvalds 22261da177e4SLinus Torvalds static void selinux_sb_free_security(struct super_block *sb) 22271da177e4SLinus Torvalds { 22281da177e4SLinus Torvalds superblock_free_security(sb); 22291da177e4SLinus Torvalds } 22301da177e4SLinus Torvalds 22311da177e4SLinus Torvalds static inline int match_prefix(char *prefix, int plen, char *option, int olen) 22321da177e4SLinus Torvalds { 22331da177e4SLinus Torvalds if (plen > olen) 22341da177e4SLinus Torvalds return 0; 22351da177e4SLinus Torvalds 22361da177e4SLinus Torvalds return !memcmp(prefix, option, plen); 22371da177e4SLinus Torvalds } 22381da177e4SLinus Torvalds 22391da177e4SLinus Torvalds static inline int selinux_option(char *option, int len) 22401da177e4SLinus Torvalds { 22411da177e4SLinus Torvalds return (match_prefix("context=", sizeof("context=")-1, option, len) || 22421da177e4SLinus Torvalds match_prefix("fscontext=", sizeof("fscontext=")-1, option, len) || 22430808925eSEric Paris match_prefix("defcontext=", sizeof("defcontext=")-1, option, len) || 22440808925eSEric Paris match_prefix("rootcontext=", sizeof("rootcontext=")-1, option, len)); 22451da177e4SLinus Torvalds } 22461da177e4SLinus Torvalds 22471da177e4SLinus Torvalds static inline void take_option(char **to, char *from, int *first, int len) 22481da177e4SLinus Torvalds { 22491da177e4SLinus Torvalds if (!*first) { 22501da177e4SLinus Torvalds **to = ','; 22511da177e4SLinus Torvalds *to += 1; 22523528a953SCory Olmo } else 22531da177e4SLinus Torvalds *first = 0; 22541da177e4SLinus Torvalds memcpy(*to, from, len); 22551da177e4SLinus Torvalds *to += len; 22561da177e4SLinus Torvalds } 22571da177e4SLinus Torvalds 22583528a953SCory Olmo static inline void take_selinux_option(char **to, char *from, int *first, 22593528a953SCory Olmo int len) 22603528a953SCory Olmo { 22613528a953SCory Olmo int current_size = 0; 22623528a953SCory Olmo 22633528a953SCory Olmo if (!*first) { 22643528a953SCory Olmo **to = '|'; 22653528a953SCory Olmo *to += 1; 22663528a953SCory Olmo } 22673528a953SCory Olmo else 22683528a953SCory Olmo *first = 0; 22693528a953SCory Olmo 22703528a953SCory Olmo while (current_size < len) { 22713528a953SCory Olmo if (*from != '"') { 22723528a953SCory Olmo **to = *from; 22733528a953SCory Olmo *to += 1; 22743528a953SCory Olmo } 22753528a953SCory Olmo from += 1; 22763528a953SCory Olmo current_size += 1; 22773528a953SCory Olmo } 22783528a953SCory Olmo } 22793528a953SCory Olmo 2280e0007529SEric Paris static int selinux_sb_copy_data(char *orig, char *copy) 22811da177e4SLinus Torvalds { 22821da177e4SLinus Torvalds int fnosec, fsec, rc = 0; 22831da177e4SLinus Torvalds char *in_save, *in_curr, *in_end; 22841da177e4SLinus Torvalds char *sec_curr, *nosec_save, *nosec; 22853528a953SCory Olmo int open_quote = 0; 22861da177e4SLinus Torvalds 22871da177e4SLinus Torvalds in_curr = orig; 22881da177e4SLinus Torvalds sec_curr = copy; 22891da177e4SLinus Torvalds 22901da177e4SLinus Torvalds nosec = (char *)get_zeroed_page(GFP_KERNEL); 22911da177e4SLinus Torvalds if (!nosec) { 22921da177e4SLinus Torvalds rc = -ENOMEM; 22931da177e4SLinus Torvalds goto out; 22941da177e4SLinus Torvalds } 22951da177e4SLinus Torvalds 22961da177e4SLinus Torvalds nosec_save = nosec; 22971da177e4SLinus Torvalds fnosec = fsec = 1; 22981da177e4SLinus Torvalds in_save = in_end = orig; 22991da177e4SLinus Torvalds 23001da177e4SLinus Torvalds do { 23013528a953SCory Olmo if (*in_end == '"') 23023528a953SCory Olmo open_quote = !open_quote; 23033528a953SCory Olmo if ((*in_end == ',' && open_quote == 0) || 23043528a953SCory Olmo *in_end == '\0') { 23051da177e4SLinus Torvalds int len = in_end - in_curr; 23061da177e4SLinus Torvalds 23071da177e4SLinus Torvalds if (selinux_option(in_curr, len)) 23083528a953SCory Olmo take_selinux_option(&sec_curr, in_curr, &fsec, len); 23091da177e4SLinus Torvalds else 23101da177e4SLinus Torvalds take_option(&nosec, in_curr, &fnosec, len); 23111da177e4SLinus Torvalds 23121da177e4SLinus Torvalds in_curr = in_end + 1; 23131da177e4SLinus Torvalds } 23141da177e4SLinus Torvalds } while (*in_end++); 23151da177e4SLinus Torvalds 23166931dfc9SEric Paris strcpy(in_save, nosec_save); 2317da3caa20SGerald Schaefer free_page((unsigned long)nosec_save); 23181da177e4SLinus Torvalds out: 23191da177e4SLinus Torvalds return rc; 23201da177e4SLinus Torvalds } 23211da177e4SLinus Torvalds 23221da177e4SLinus Torvalds static int selinux_sb_kern_mount(struct super_block *sb, void *data) 23231da177e4SLinus Torvalds { 23241da177e4SLinus Torvalds struct avc_audit_data ad; 23251da177e4SLinus Torvalds int rc; 23261da177e4SLinus Torvalds 23271da177e4SLinus Torvalds rc = superblock_doinit(sb, data); 23281da177e4SLinus Torvalds if (rc) 23291da177e4SLinus Torvalds return rc; 23301da177e4SLinus Torvalds 23311da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,FS); 233244707fdfSJan Blunck ad.u.fs.path.dentry = sb->s_root; 23331da177e4SLinus Torvalds return superblock_has_perm(current, sb, FILESYSTEM__MOUNT, &ad); 23341da177e4SLinus Torvalds } 23351da177e4SLinus Torvalds 2336726c3342SDavid Howells static int selinux_sb_statfs(struct dentry *dentry) 23371da177e4SLinus Torvalds { 23381da177e4SLinus Torvalds struct avc_audit_data ad; 23391da177e4SLinus Torvalds 23401da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,FS); 234144707fdfSJan Blunck ad.u.fs.path.dentry = dentry->d_sb->s_root; 2342726c3342SDavid Howells return superblock_has_perm(current, dentry->d_sb, FILESYSTEM__GETATTR, &ad); 23431da177e4SLinus Torvalds } 23441da177e4SLinus Torvalds 23451da177e4SLinus Torvalds static int selinux_mount(char * dev_name, 23461da177e4SLinus Torvalds struct nameidata *nd, 23471da177e4SLinus Torvalds char * type, 23481da177e4SLinus Torvalds unsigned long flags, 23491da177e4SLinus Torvalds void * data) 23501da177e4SLinus Torvalds { 23511da177e4SLinus Torvalds int rc; 23521da177e4SLinus Torvalds 23531da177e4SLinus Torvalds rc = secondary_ops->sb_mount(dev_name, nd, type, flags, data); 23541da177e4SLinus Torvalds if (rc) 23551da177e4SLinus Torvalds return rc; 23561da177e4SLinus Torvalds 23571da177e4SLinus Torvalds if (flags & MS_REMOUNT) 23584ac91378SJan Blunck return superblock_has_perm(current, nd->path.mnt->mnt_sb, 23591da177e4SLinus Torvalds FILESYSTEM__REMOUNT, NULL); 23601da177e4SLinus Torvalds else 23614ac91378SJan Blunck return dentry_has_perm(current, nd->path.mnt, nd->path.dentry, 23621da177e4SLinus Torvalds FILE__MOUNTON); 23631da177e4SLinus Torvalds } 23641da177e4SLinus Torvalds 23651da177e4SLinus Torvalds static int selinux_umount(struct vfsmount *mnt, int flags) 23661da177e4SLinus Torvalds { 23671da177e4SLinus Torvalds int rc; 23681da177e4SLinus Torvalds 23691da177e4SLinus Torvalds rc = secondary_ops->sb_umount(mnt, flags); 23701da177e4SLinus Torvalds if (rc) 23711da177e4SLinus Torvalds return rc; 23721da177e4SLinus Torvalds 23731da177e4SLinus Torvalds return superblock_has_perm(current,mnt->mnt_sb, 23741da177e4SLinus Torvalds FILESYSTEM__UNMOUNT,NULL); 23751da177e4SLinus Torvalds } 23761da177e4SLinus Torvalds 23771da177e4SLinus Torvalds /* inode security operations */ 23781da177e4SLinus Torvalds 23791da177e4SLinus Torvalds static int selinux_inode_alloc_security(struct inode *inode) 23801da177e4SLinus Torvalds { 23811da177e4SLinus Torvalds return inode_alloc_security(inode); 23821da177e4SLinus Torvalds } 23831da177e4SLinus Torvalds 23841da177e4SLinus Torvalds static void selinux_inode_free_security(struct inode *inode) 23851da177e4SLinus Torvalds { 23861da177e4SLinus Torvalds inode_free_security(inode); 23871da177e4SLinus Torvalds } 23881da177e4SLinus Torvalds 23895e41ff9eSStephen Smalley static int selinux_inode_init_security(struct inode *inode, struct inode *dir, 23905e41ff9eSStephen Smalley char **name, void **value, 23915e41ff9eSStephen Smalley size_t *len) 23925e41ff9eSStephen Smalley { 23935e41ff9eSStephen Smalley struct task_security_struct *tsec; 23945e41ff9eSStephen Smalley struct inode_security_struct *dsec; 23955e41ff9eSStephen Smalley struct superblock_security_struct *sbsec; 2396570bc1c2SStephen Smalley u32 newsid, clen; 23975e41ff9eSStephen Smalley int rc; 2398570bc1c2SStephen Smalley char *namep = NULL, *context; 23995e41ff9eSStephen Smalley 24005e41ff9eSStephen Smalley tsec = current->security; 24015e41ff9eSStephen Smalley dsec = dir->i_security; 24025e41ff9eSStephen Smalley sbsec = dir->i_sb->s_security; 24035e41ff9eSStephen Smalley 24045e41ff9eSStephen Smalley if (tsec->create_sid && sbsec->behavior != SECURITY_FS_USE_MNTPOINT) { 24055e41ff9eSStephen Smalley newsid = tsec->create_sid; 24065e41ff9eSStephen Smalley } else { 24075e41ff9eSStephen Smalley rc = security_transition_sid(tsec->sid, dsec->sid, 24085e41ff9eSStephen Smalley inode_mode_to_security_class(inode->i_mode), 24095e41ff9eSStephen Smalley &newsid); 24105e41ff9eSStephen Smalley if (rc) { 24115e41ff9eSStephen Smalley printk(KERN_WARNING "%s: " 24125e41ff9eSStephen Smalley "security_transition_sid failed, rc=%d (dev=%s " 24135e41ff9eSStephen Smalley "ino=%ld)\n", 24145e41ff9eSStephen Smalley __FUNCTION__, 24155e41ff9eSStephen Smalley -rc, inode->i_sb->s_id, inode->i_ino); 24165e41ff9eSStephen Smalley return rc; 24175e41ff9eSStephen Smalley } 24185e41ff9eSStephen Smalley } 24195e41ff9eSStephen Smalley 2420296fddf7SEric Paris /* Possibly defer initialization to selinux_complete_init. */ 2421296fddf7SEric Paris if (sbsec->initialized) { 2422296fddf7SEric Paris struct inode_security_struct *isec = inode->i_security; 2423296fddf7SEric Paris isec->sclass = inode_mode_to_security_class(inode->i_mode); 2424296fddf7SEric Paris isec->sid = newsid; 2425296fddf7SEric Paris isec->initialized = 1; 2426296fddf7SEric Paris } 24275e41ff9eSStephen Smalley 24288aad3875SStephen Smalley if (!ss_initialized || sbsec->behavior == SECURITY_FS_USE_MNTPOINT) 242925a74f3bSStephen Smalley return -EOPNOTSUPP; 243025a74f3bSStephen Smalley 2431570bc1c2SStephen Smalley if (name) { 2432*a02fe132SJosef Bacik namep = kstrdup(XATTR_SELINUX_SUFFIX, GFP_NOFS); 24335e41ff9eSStephen Smalley if (!namep) 24345e41ff9eSStephen Smalley return -ENOMEM; 24355e41ff9eSStephen Smalley *name = namep; 2436570bc1c2SStephen Smalley } 24375e41ff9eSStephen Smalley 2438570bc1c2SStephen Smalley if (value && len) { 2439570bc1c2SStephen Smalley rc = security_sid_to_context(newsid, &context, &clen); 24405e41ff9eSStephen Smalley if (rc) { 24415e41ff9eSStephen Smalley kfree(namep); 24425e41ff9eSStephen Smalley return rc; 24435e41ff9eSStephen Smalley } 24445e41ff9eSStephen Smalley *value = context; 2445570bc1c2SStephen Smalley *len = clen; 2446570bc1c2SStephen Smalley } 24475e41ff9eSStephen Smalley 24485e41ff9eSStephen Smalley return 0; 24495e41ff9eSStephen Smalley } 24505e41ff9eSStephen Smalley 24511da177e4SLinus Torvalds static int selinux_inode_create(struct inode *dir, struct dentry *dentry, int mask) 24521da177e4SLinus Torvalds { 24531da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_FILE); 24541da177e4SLinus Torvalds } 24551da177e4SLinus Torvalds 24561da177e4SLinus Torvalds static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 24571da177e4SLinus Torvalds { 24581da177e4SLinus Torvalds int rc; 24591da177e4SLinus Torvalds 24601da177e4SLinus Torvalds rc = secondary_ops->inode_link(old_dentry,dir,new_dentry); 24611da177e4SLinus Torvalds if (rc) 24621da177e4SLinus Torvalds return rc; 24631da177e4SLinus Torvalds return may_link(dir, old_dentry, MAY_LINK); 24641da177e4SLinus Torvalds } 24651da177e4SLinus Torvalds 24661da177e4SLinus Torvalds static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry) 24671da177e4SLinus Torvalds { 24681da177e4SLinus Torvalds int rc; 24691da177e4SLinus Torvalds 24701da177e4SLinus Torvalds rc = secondary_ops->inode_unlink(dir, dentry); 24711da177e4SLinus Torvalds if (rc) 24721da177e4SLinus Torvalds return rc; 24731da177e4SLinus Torvalds return may_link(dir, dentry, MAY_UNLINK); 24741da177e4SLinus Torvalds } 24751da177e4SLinus Torvalds 24761da177e4SLinus Torvalds static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name) 24771da177e4SLinus Torvalds { 24781da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_LNK_FILE); 24791da177e4SLinus Torvalds } 24801da177e4SLinus Torvalds 24811da177e4SLinus Torvalds static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, int mask) 24821da177e4SLinus Torvalds { 24831da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_DIR); 24841da177e4SLinus Torvalds } 24851da177e4SLinus Torvalds 24861da177e4SLinus Torvalds static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry) 24871da177e4SLinus Torvalds { 24881da177e4SLinus Torvalds return may_link(dir, dentry, MAY_RMDIR); 24891da177e4SLinus Torvalds } 24901da177e4SLinus Torvalds 24911da177e4SLinus Torvalds static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 24921da177e4SLinus Torvalds { 24931da177e4SLinus Torvalds int rc; 24941da177e4SLinus Torvalds 24951da177e4SLinus Torvalds rc = secondary_ops->inode_mknod(dir, dentry, mode, dev); 24961da177e4SLinus Torvalds if (rc) 24971da177e4SLinus Torvalds return rc; 24981da177e4SLinus Torvalds 24991da177e4SLinus Torvalds return may_create(dir, dentry, inode_mode_to_security_class(mode)); 25001da177e4SLinus Torvalds } 25011da177e4SLinus Torvalds 25021da177e4SLinus Torvalds static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry, 25031da177e4SLinus Torvalds struct inode *new_inode, struct dentry *new_dentry) 25041da177e4SLinus Torvalds { 25051da177e4SLinus Torvalds return may_rename(old_inode, old_dentry, new_inode, new_dentry); 25061da177e4SLinus Torvalds } 25071da177e4SLinus Torvalds 25081da177e4SLinus Torvalds static int selinux_inode_readlink(struct dentry *dentry) 25091da177e4SLinus Torvalds { 25101da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__READ); 25111da177e4SLinus Torvalds } 25121da177e4SLinus Torvalds 25131da177e4SLinus Torvalds static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata) 25141da177e4SLinus Torvalds { 25151da177e4SLinus Torvalds int rc; 25161da177e4SLinus Torvalds 25171da177e4SLinus Torvalds rc = secondary_ops->inode_follow_link(dentry,nameidata); 25181da177e4SLinus Torvalds if (rc) 25191da177e4SLinus Torvalds return rc; 25201da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__READ); 25211da177e4SLinus Torvalds } 25221da177e4SLinus Torvalds 25231da177e4SLinus Torvalds static int selinux_inode_permission(struct inode *inode, int mask, 25241da177e4SLinus Torvalds struct nameidata *nd) 25251da177e4SLinus Torvalds { 25261da177e4SLinus Torvalds int rc; 25271da177e4SLinus Torvalds 25281da177e4SLinus Torvalds rc = secondary_ops->inode_permission(inode, mask, nd); 25291da177e4SLinus Torvalds if (rc) 25301da177e4SLinus Torvalds return rc; 25311da177e4SLinus Torvalds 25321da177e4SLinus Torvalds if (!mask) { 25331da177e4SLinus Torvalds /* No permission to check. Existence test. */ 25341da177e4SLinus Torvalds return 0; 25351da177e4SLinus Torvalds } 25361da177e4SLinus Torvalds 25371da177e4SLinus Torvalds return inode_has_perm(current, inode, 25381da177e4SLinus Torvalds file_mask_to_av(inode->i_mode, mask), NULL); 25391da177e4SLinus Torvalds } 25401da177e4SLinus Torvalds 25411da177e4SLinus Torvalds static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr) 25421da177e4SLinus Torvalds { 25431da177e4SLinus Torvalds int rc; 25441da177e4SLinus Torvalds 25451da177e4SLinus Torvalds rc = secondary_ops->inode_setattr(dentry, iattr); 25461da177e4SLinus Torvalds if (rc) 25471da177e4SLinus Torvalds return rc; 25481da177e4SLinus Torvalds 25491da177e4SLinus Torvalds if (iattr->ia_valid & ATTR_FORCE) 25501da177e4SLinus Torvalds return 0; 25511da177e4SLinus Torvalds 25521da177e4SLinus Torvalds if (iattr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | 25531da177e4SLinus Torvalds ATTR_ATIME_SET | ATTR_MTIME_SET)) 25541da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__SETATTR); 25551da177e4SLinus Torvalds 25561da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__WRITE); 25571da177e4SLinus Torvalds } 25581da177e4SLinus Torvalds 25591da177e4SLinus Torvalds static int selinux_inode_getattr(struct vfsmount *mnt, struct dentry *dentry) 25601da177e4SLinus Torvalds { 25611da177e4SLinus Torvalds return dentry_has_perm(current, mnt, dentry, FILE__GETATTR); 25621da177e4SLinus Torvalds } 25631da177e4SLinus Torvalds 2564b5376771SSerge E. Hallyn static int selinux_inode_setotherxattr(struct dentry *dentry, char *name) 2565b5376771SSerge E. Hallyn { 2566b5376771SSerge E. Hallyn if (!strncmp(name, XATTR_SECURITY_PREFIX, 2567b5376771SSerge E. Hallyn sizeof XATTR_SECURITY_PREFIX - 1)) { 2568b5376771SSerge E. Hallyn if (!strcmp(name, XATTR_NAME_CAPS)) { 2569b5376771SSerge E. Hallyn if (!capable(CAP_SETFCAP)) 2570b5376771SSerge E. Hallyn return -EPERM; 2571b5376771SSerge E. Hallyn } else if (!capable(CAP_SYS_ADMIN)) { 2572b5376771SSerge E. Hallyn /* A different attribute in the security namespace. 2573b5376771SSerge E. Hallyn Restrict to administrator. */ 2574b5376771SSerge E. Hallyn return -EPERM; 2575b5376771SSerge E. Hallyn } 2576b5376771SSerge E. Hallyn } 2577b5376771SSerge E. Hallyn 2578b5376771SSerge E. Hallyn /* Not an attribute we recognize, so just check the 2579b5376771SSerge E. Hallyn ordinary setattr permission. */ 2580b5376771SSerge E. Hallyn return dentry_has_perm(current, NULL, dentry, FILE__SETATTR); 2581b5376771SSerge E. Hallyn } 2582b5376771SSerge E. Hallyn 25831da177e4SLinus Torvalds static int selinux_inode_setxattr(struct dentry *dentry, char *name, void *value, size_t size, int flags) 25841da177e4SLinus Torvalds { 25851da177e4SLinus Torvalds struct task_security_struct *tsec = current->security; 25861da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 25871da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 25881da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 25891da177e4SLinus Torvalds struct avc_audit_data ad; 25901da177e4SLinus Torvalds u32 newsid; 25911da177e4SLinus Torvalds int rc = 0; 25921da177e4SLinus Torvalds 2593b5376771SSerge E. Hallyn if (strcmp(name, XATTR_NAME_SELINUX)) 2594b5376771SSerge E. Hallyn return selinux_inode_setotherxattr(dentry, name); 25951da177e4SLinus Torvalds 25961da177e4SLinus Torvalds sbsec = inode->i_sb->s_security; 25971da177e4SLinus Torvalds if (sbsec->behavior == SECURITY_FS_USE_MNTPOINT) 25981da177e4SLinus Torvalds return -EOPNOTSUPP; 25991da177e4SLinus Torvalds 26003bd858abSSatyam Sharma if (!is_owner_or_cap(inode)) 26011da177e4SLinus Torvalds return -EPERM; 26021da177e4SLinus Torvalds 26031da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,FS); 260444707fdfSJan Blunck ad.u.fs.path.dentry = dentry; 26051da177e4SLinus Torvalds 26061da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, isec->sclass, 26071da177e4SLinus Torvalds FILE__RELABELFROM, &ad); 26081da177e4SLinus Torvalds if (rc) 26091da177e4SLinus Torvalds return rc; 26101da177e4SLinus Torvalds 26111da177e4SLinus Torvalds rc = security_context_to_sid(value, size, &newsid); 26121da177e4SLinus Torvalds if (rc) 26131da177e4SLinus Torvalds return rc; 26141da177e4SLinus Torvalds 26151da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, newsid, isec->sclass, 26161da177e4SLinus Torvalds FILE__RELABELTO, &ad); 26171da177e4SLinus Torvalds if (rc) 26181da177e4SLinus Torvalds return rc; 26191da177e4SLinus Torvalds 26201da177e4SLinus Torvalds rc = security_validate_transition(isec->sid, newsid, tsec->sid, 26211da177e4SLinus Torvalds isec->sclass); 26221da177e4SLinus Torvalds if (rc) 26231da177e4SLinus Torvalds return rc; 26241da177e4SLinus Torvalds 26251da177e4SLinus Torvalds return avc_has_perm(newsid, 26261da177e4SLinus Torvalds sbsec->sid, 26271da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 26281da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, 26291da177e4SLinus Torvalds &ad); 26301da177e4SLinus Torvalds } 26311da177e4SLinus Torvalds 26321da177e4SLinus Torvalds static void selinux_inode_post_setxattr(struct dentry *dentry, char *name, 26331da177e4SLinus Torvalds void *value, size_t size, int flags) 26341da177e4SLinus Torvalds { 26351da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 26361da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 26371da177e4SLinus Torvalds u32 newsid; 26381da177e4SLinus Torvalds int rc; 26391da177e4SLinus Torvalds 26401da177e4SLinus Torvalds if (strcmp(name, XATTR_NAME_SELINUX)) { 26411da177e4SLinus Torvalds /* Not an attribute we recognize, so nothing to do. */ 26421da177e4SLinus Torvalds return; 26431da177e4SLinus Torvalds } 26441da177e4SLinus Torvalds 26451da177e4SLinus Torvalds rc = security_context_to_sid(value, size, &newsid); 26461da177e4SLinus Torvalds if (rc) { 26471da177e4SLinus Torvalds printk(KERN_WARNING "%s: unable to obtain SID for context " 26481da177e4SLinus Torvalds "%s, rc=%d\n", __FUNCTION__, (char*)value, -rc); 26491da177e4SLinus Torvalds return; 26501da177e4SLinus Torvalds } 26511da177e4SLinus Torvalds 26521da177e4SLinus Torvalds isec->sid = newsid; 26531da177e4SLinus Torvalds return; 26541da177e4SLinus Torvalds } 26551da177e4SLinus Torvalds 26561da177e4SLinus Torvalds static int selinux_inode_getxattr (struct dentry *dentry, char *name) 26571da177e4SLinus Torvalds { 26581da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__GETATTR); 26591da177e4SLinus Torvalds } 26601da177e4SLinus Torvalds 26611da177e4SLinus Torvalds static int selinux_inode_listxattr (struct dentry *dentry) 26621da177e4SLinus Torvalds { 26631da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__GETATTR); 26641da177e4SLinus Torvalds } 26651da177e4SLinus Torvalds 26661da177e4SLinus Torvalds static int selinux_inode_removexattr (struct dentry *dentry, char *name) 26671da177e4SLinus Torvalds { 2668b5376771SSerge E. Hallyn if (strcmp(name, XATTR_NAME_SELINUX)) 2669b5376771SSerge E. Hallyn return selinux_inode_setotherxattr(dentry, name); 26701da177e4SLinus Torvalds 26711da177e4SLinus Torvalds /* No one is allowed to remove a SELinux security label. 26721da177e4SLinus Torvalds You can change the label, but all data must be labeled. */ 26731da177e4SLinus Torvalds return -EACCES; 26741da177e4SLinus Torvalds } 26751da177e4SLinus Torvalds 2676d381d8a9SJames Morris /* 2677d381d8a9SJames Morris * Copy the in-core inode security context value to the user. If the 2678d381d8a9SJames Morris * getxattr() prior to this succeeded, check to see if we need to 2679d381d8a9SJames Morris * canonicalize the value to be finally returned to the user. 2680d381d8a9SJames Morris * 2681d381d8a9SJames Morris * Permission check is handled by selinux_inode_getxattr hook. 2682d381d8a9SJames Morris */ 268342492594SDavid P. Quigley static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void **buffer, bool alloc) 26841da177e4SLinus Torvalds { 268542492594SDavid P. Quigley u32 size; 268642492594SDavid P. Quigley int error; 268742492594SDavid P. Quigley char *context = NULL; 26881da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 26891da177e4SLinus Torvalds 26908c8570fbSDustin Kirkland if (strcmp(name, XATTR_SELINUX_SUFFIX)) 26918c8570fbSDustin Kirkland return -EOPNOTSUPP; 26921da177e4SLinus Torvalds 269342492594SDavid P. Quigley error = security_sid_to_context(isec->sid, &context, &size); 269442492594SDavid P. Quigley if (error) 269542492594SDavid P. Quigley return error; 269642492594SDavid P. Quigley error = size; 269742492594SDavid P. Quigley if (alloc) { 269842492594SDavid P. Quigley *buffer = context; 269942492594SDavid P. Quigley goto out_nofree; 270042492594SDavid P. Quigley } 270142492594SDavid P. Quigley kfree(context); 270242492594SDavid P. Quigley out_nofree: 270342492594SDavid P. Quigley return error; 27041da177e4SLinus Torvalds } 27051da177e4SLinus Torvalds 27061da177e4SLinus Torvalds static int selinux_inode_setsecurity(struct inode *inode, const char *name, 27071da177e4SLinus Torvalds const void *value, size_t size, int flags) 27081da177e4SLinus Torvalds { 27091da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 27101da177e4SLinus Torvalds u32 newsid; 27111da177e4SLinus Torvalds int rc; 27121da177e4SLinus Torvalds 27131da177e4SLinus Torvalds if (strcmp(name, XATTR_SELINUX_SUFFIX)) 27141da177e4SLinus Torvalds return -EOPNOTSUPP; 27151da177e4SLinus Torvalds 27161da177e4SLinus Torvalds if (!value || !size) 27171da177e4SLinus Torvalds return -EACCES; 27181da177e4SLinus Torvalds 27191da177e4SLinus Torvalds rc = security_context_to_sid((void*)value, size, &newsid); 27201da177e4SLinus Torvalds if (rc) 27211da177e4SLinus Torvalds return rc; 27221da177e4SLinus Torvalds 27231da177e4SLinus Torvalds isec->sid = newsid; 27241da177e4SLinus Torvalds return 0; 27251da177e4SLinus Torvalds } 27261da177e4SLinus Torvalds 27271da177e4SLinus Torvalds static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size) 27281da177e4SLinus Torvalds { 27291da177e4SLinus Torvalds const int len = sizeof(XATTR_NAME_SELINUX); 27301da177e4SLinus Torvalds if (buffer && len <= buffer_size) 27311da177e4SLinus Torvalds memcpy(buffer, XATTR_NAME_SELINUX, len); 27321da177e4SLinus Torvalds return len; 27331da177e4SLinus Torvalds } 27341da177e4SLinus Torvalds 2735b5376771SSerge E. Hallyn static int selinux_inode_need_killpriv(struct dentry *dentry) 2736b5376771SSerge E. Hallyn { 2737b5376771SSerge E. Hallyn return secondary_ops->inode_need_killpriv(dentry); 2738b5376771SSerge E. Hallyn } 2739b5376771SSerge E. Hallyn 2740b5376771SSerge E. Hallyn static int selinux_inode_killpriv(struct dentry *dentry) 2741b5376771SSerge E. Hallyn { 2742b5376771SSerge E. Hallyn return secondary_ops->inode_killpriv(dentry); 2743b5376771SSerge E. Hallyn } 2744b5376771SSerge E. Hallyn 27451da177e4SLinus Torvalds /* file security operations */ 27461da177e4SLinus Torvalds 2747788e7dd4SYuichi Nakamura static int selinux_revalidate_file_permission(struct file *file, int mask) 27481da177e4SLinus Torvalds { 27497420ed23SVenkat Yekkirala int rc; 27503d5ff529SJosef Sipek struct inode *inode = file->f_path.dentry->d_inode; 27511da177e4SLinus Torvalds 27521da177e4SLinus Torvalds if (!mask) { 27531da177e4SLinus Torvalds /* No permission to check. Existence test. */ 27541da177e4SLinus Torvalds return 0; 27551da177e4SLinus Torvalds } 27561da177e4SLinus Torvalds 27571da177e4SLinus Torvalds /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */ 27581da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE)) 27591da177e4SLinus Torvalds mask |= MAY_APPEND; 27601da177e4SLinus Torvalds 27617420ed23SVenkat Yekkirala rc = file_has_perm(current, file, 27621da177e4SLinus Torvalds file_mask_to_av(inode->i_mode, mask)); 27637420ed23SVenkat Yekkirala if (rc) 27647420ed23SVenkat Yekkirala return rc; 27657420ed23SVenkat Yekkirala 27667420ed23SVenkat Yekkirala return selinux_netlbl_inode_permission(inode, mask); 27671da177e4SLinus Torvalds } 27681da177e4SLinus Torvalds 2769788e7dd4SYuichi Nakamura static int selinux_file_permission(struct file *file, int mask) 2770788e7dd4SYuichi Nakamura { 2771788e7dd4SYuichi Nakamura struct inode *inode = file->f_path.dentry->d_inode; 2772788e7dd4SYuichi Nakamura struct task_security_struct *tsec = current->security; 2773788e7dd4SYuichi Nakamura struct file_security_struct *fsec = file->f_security; 2774788e7dd4SYuichi Nakamura struct inode_security_struct *isec = inode->i_security; 2775788e7dd4SYuichi Nakamura 2776788e7dd4SYuichi Nakamura if (!mask) { 2777788e7dd4SYuichi Nakamura /* No permission to check. Existence test. */ 2778788e7dd4SYuichi Nakamura return 0; 2779788e7dd4SYuichi Nakamura } 2780788e7dd4SYuichi Nakamura 2781788e7dd4SYuichi Nakamura if (tsec->sid == fsec->sid && fsec->isid == isec->sid 2782788e7dd4SYuichi Nakamura && fsec->pseqno == avc_policy_seqno()) 2783788e7dd4SYuichi Nakamura return selinux_netlbl_inode_permission(inode, mask); 2784788e7dd4SYuichi Nakamura 2785788e7dd4SYuichi Nakamura return selinux_revalidate_file_permission(file, mask); 2786788e7dd4SYuichi Nakamura } 2787788e7dd4SYuichi Nakamura 27881da177e4SLinus Torvalds static int selinux_file_alloc_security(struct file *file) 27891da177e4SLinus Torvalds { 27901da177e4SLinus Torvalds return file_alloc_security(file); 27911da177e4SLinus Torvalds } 27921da177e4SLinus Torvalds 27931da177e4SLinus Torvalds static void selinux_file_free_security(struct file *file) 27941da177e4SLinus Torvalds { 27951da177e4SLinus Torvalds file_free_security(file); 27961da177e4SLinus Torvalds } 27971da177e4SLinus Torvalds 27981da177e4SLinus Torvalds static int selinux_file_ioctl(struct file *file, unsigned int cmd, 27991da177e4SLinus Torvalds unsigned long arg) 28001da177e4SLinus Torvalds { 28011da177e4SLinus Torvalds int error = 0; 28021da177e4SLinus Torvalds 28031da177e4SLinus Torvalds switch (cmd) { 28041da177e4SLinus Torvalds case FIONREAD: 28051da177e4SLinus Torvalds /* fall through */ 28061da177e4SLinus Torvalds case FIBMAP: 28071da177e4SLinus Torvalds /* fall through */ 28081da177e4SLinus Torvalds case FIGETBSZ: 28091da177e4SLinus Torvalds /* fall through */ 28101da177e4SLinus Torvalds case EXT2_IOC_GETFLAGS: 28111da177e4SLinus Torvalds /* fall through */ 28121da177e4SLinus Torvalds case EXT2_IOC_GETVERSION: 28131da177e4SLinus Torvalds error = file_has_perm(current, file, FILE__GETATTR); 28141da177e4SLinus Torvalds break; 28151da177e4SLinus Torvalds 28161da177e4SLinus Torvalds case EXT2_IOC_SETFLAGS: 28171da177e4SLinus Torvalds /* fall through */ 28181da177e4SLinus Torvalds case EXT2_IOC_SETVERSION: 28191da177e4SLinus Torvalds error = file_has_perm(current, file, FILE__SETATTR); 28201da177e4SLinus Torvalds break; 28211da177e4SLinus Torvalds 28221da177e4SLinus Torvalds /* sys_ioctl() checks */ 28231da177e4SLinus Torvalds case FIONBIO: 28241da177e4SLinus Torvalds /* fall through */ 28251da177e4SLinus Torvalds case FIOASYNC: 28261da177e4SLinus Torvalds error = file_has_perm(current, file, 0); 28271da177e4SLinus Torvalds break; 28281da177e4SLinus Torvalds 28291da177e4SLinus Torvalds case KDSKBENT: 28301da177e4SLinus Torvalds case KDSKBSENT: 28311da177e4SLinus Torvalds error = task_has_capability(current,CAP_SYS_TTY_CONFIG); 28321da177e4SLinus Torvalds break; 28331da177e4SLinus Torvalds 28341da177e4SLinus Torvalds /* default case assumes that the command will go 28351da177e4SLinus Torvalds * to the file's ioctl() function. 28361da177e4SLinus Torvalds */ 28371da177e4SLinus Torvalds default: 28381da177e4SLinus Torvalds error = file_has_perm(current, file, FILE__IOCTL); 28391da177e4SLinus Torvalds 28401da177e4SLinus Torvalds } 28411da177e4SLinus Torvalds return error; 28421da177e4SLinus Torvalds } 28431da177e4SLinus Torvalds 28441da177e4SLinus Torvalds static int file_map_prot_check(struct file *file, unsigned long prot, int shared) 28451da177e4SLinus Torvalds { 28461da177e4SLinus Torvalds #ifndef CONFIG_PPC32 28471da177e4SLinus Torvalds if ((prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) { 28481da177e4SLinus Torvalds /* 28491da177e4SLinus Torvalds * We are making executable an anonymous mapping or a 28501da177e4SLinus Torvalds * private file mapping that will also be writable. 28511da177e4SLinus Torvalds * This has an additional check. 28521da177e4SLinus Torvalds */ 28531da177e4SLinus Torvalds int rc = task_has_perm(current, current, PROCESS__EXECMEM); 28541da177e4SLinus Torvalds if (rc) 28551da177e4SLinus Torvalds return rc; 28561da177e4SLinus Torvalds } 28571da177e4SLinus Torvalds #endif 28581da177e4SLinus Torvalds 28591da177e4SLinus Torvalds if (file) { 28601da177e4SLinus Torvalds /* read access is always possible with a mapping */ 28611da177e4SLinus Torvalds u32 av = FILE__READ; 28621da177e4SLinus Torvalds 28631da177e4SLinus Torvalds /* write access only matters if the mapping is shared */ 28641da177e4SLinus Torvalds if (shared && (prot & PROT_WRITE)) 28651da177e4SLinus Torvalds av |= FILE__WRITE; 28661da177e4SLinus Torvalds 28671da177e4SLinus Torvalds if (prot & PROT_EXEC) 28681da177e4SLinus Torvalds av |= FILE__EXECUTE; 28691da177e4SLinus Torvalds 28701da177e4SLinus Torvalds return file_has_perm(current, file, av); 28711da177e4SLinus Torvalds } 28721da177e4SLinus Torvalds return 0; 28731da177e4SLinus Torvalds } 28741da177e4SLinus Torvalds 28751da177e4SLinus Torvalds static int selinux_file_mmap(struct file *file, unsigned long reqprot, 2876ed032189SEric Paris unsigned long prot, unsigned long flags, 2877ed032189SEric Paris unsigned long addr, unsigned long addr_only) 28781da177e4SLinus Torvalds { 2879ed032189SEric Paris int rc = 0; 2880ed032189SEric Paris u32 sid = ((struct task_security_struct*)(current->security))->sid; 28811da177e4SLinus Torvalds 2882ed032189SEric Paris if (addr < mmap_min_addr) 2883ed032189SEric Paris rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT, 2884ed032189SEric Paris MEMPROTECT__MMAP_ZERO, NULL); 2885ed032189SEric Paris if (rc || addr_only) 28861da177e4SLinus Torvalds return rc; 28871da177e4SLinus Torvalds 28881da177e4SLinus Torvalds if (selinux_checkreqprot) 28891da177e4SLinus Torvalds prot = reqprot; 28901da177e4SLinus Torvalds 28911da177e4SLinus Torvalds return file_map_prot_check(file, prot, 28921da177e4SLinus Torvalds (flags & MAP_TYPE) == MAP_SHARED); 28931da177e4SLinus Torvalds } 28941da177e4SLinus Torvalds 28951da177e4SLinus Torvalds static int selinux_file_mprotect(struct vm_area_struct *vma, 28961da177e4SLinus Torvalds unsigned long reqprot, 28971da177e4SLinus Torvalds unsigned long prot) 28981da177e4SLinus Torvalds { 28991da177e4SLinus Torvalds int rc; 29001da177e4SLinus Torvalds 29011da177e4SLinus Torvalds rc = secondary_ops->file_mprotect(vma, reqprot, prot); 29021da177e4SLinus Torvalds if (rc) 29031da177e4SLinus Torvalds return rc; 29041da177e4SLinus Torvalds 29051da177e4SLinus Torvalds if (selinux_checkreqprot) 29061da177e4SLinus Torvalds prot = reqprot; 29071da177e4SLinus Torvalds 29081da177e4SLinus Torvalds #ifndef CONFIG_PPC32 2909db4c9641SStephen Smalley if ((prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) { 2910db4c9641SStephen Smalley rc = 0; 2911db4c9641SStephen Smalley if (vma->vm_start >= vma->vm_mm->start_brk && 2912db4c9641SStephen Smalley vma->vm_end <= vma->vm_mm->brk) { 2913db4c9641SStephen Smalley rc = task_has_perm(current, current, 2914db4c9641SStephen Smalley PROCESS__EXECHEAP); 2915db4c9641SStephen Smalley } else if (!vma->vm_file && 29166b992197SLorenzo Hernandez García-Hierro vma->vm_start <= vma->vm_mm->start_stack && 29176b992197SLorenzo Hernandez García-Hierro vma->vm_end >= vma->vm_mm->start_stack) { 29186b992197SLorenzo Hernandez García-Hierro rc = task_has_perm(current, current, PROCESS__EXECSTACK); 2919db4c9641SStephen Smalley } else if (vma->vm_file && vma->anon_vma) { 2920db4c9641SStephen Smalley /* 2921db4c9641SStephen Smalley * We are making executable a file mapping that has 2922db4c9641SStephen Smalley * had some COW done. Since pages might have been 2923db4c9641SStephen Smalley * written, check ability to execute the possibly 2924db4c9641SStephen Smalley * modified content. This typically should only 2925db4c9641SStephen Smalley * occur for text relocations. 2926db4c9641SStephen Smalley */ 2927db4c9641SStephen Smalley rc = file_has_perm(current, vma->vm_file, 2928db4c9641SStephen Smalley FILE__EXECMOD); 2929db4c9641SStephen Smalley } 29306b992197SLorenzo Hernandez García-Hierro if (rc) 29316b992197SLorenzo Hernandez García-Hierro return rc; 29326b992197SLorenzo Hernandez García-Hierro } 29331da177e4SLinus Torvalds #endif 29341da177e4SLinus Torvalds 29351da177e4SLinus Torvalds return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED); 29361da177e4SLinus Torvalds } 29371da177e4SLinus Torvalds 29381da177e4SLinus Torvalds static int selinux_file_lock(struct file *file, unsigned int cmd) 29391da177e4SLinus Torvalds { 29401da177e4SLinus Torvalds return file_has_perm(current, file, FILE__LOCK); 29411da177e4SLinus Torvalds } 29421da177e4SLinus Torvalds 29431da177e4SLinus Torvalds static int selinux_file_fcntl(struct file *file, unsigned int cmd, 29441da177e4SLinus Torvalds unsigned long arg) 29451da177e4SLinus Torvalds { 29461da177e4SLinus Torvalds int err = 0; 29471da177e4SLinus Torvalds 29481da177e4SLinus Torvalds switch (cmd) { 29491da177e4SLinus Torvalds case F_SETFL: 29503d5ff529SJosef Sipek if (!file->f_path.dentry || !file->f_path.dentry->d_inode) { 29511da177e4SLinus Torvalds err = -EINVAL; 29521da177e4SLinus Torvalds break; 29531da177e4SLinus Torvalds } 29541da177e4SLinus Torvalds 29551da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) { 29561da177e4SLinus Torvalds err = file_has_perm(current, file,FILE__WRITE); 29571da177e4SLinus Torvalds break; 29581da177e4SLinus Torvalds } 29591da177e4SLinus Torvalds /* fall through */ 29601da177e4SLinus Torvalds case F_SETOWN: 29611da177e4SLinus Torvalds case F_SETSIG: 29621da177e4SLinus Torvalds case F_GETFL: 29631da177e4SLinus Torvalds case F_GETOWN: 29641da177e4SLinus Torvalds case F_GETSIG: 29651da177e4SLinus Torvalds /* Just check FD__USE permission */ 29661da177e4SLinus Torvalds err = file_has_perm(current, file, 0); 29671da177e4SLinus Torvalds break; 29681da177e4SLinus Torvalds case F_GETLK: 29691da177e4SLinus Torvalds case F_SETLK: 29701da177e4SLinus Torvalds case F_SETLKW: 29711da177e4SLinus Torvalds #if BITS_PER_LONG == 32 29721da177e4SLinus Torvalds case F_GETLK64: 29731da177e4SLinus Torvalds case F_SETLK64: 29741da177e4SLinus Torvalds case F_SETLKW64: 29751da177e4SLinus Torvalds #endif 29763d5ff529SJosef Sipek if (!file->f_path.dentry || !file->f_path.dentry->d_inode) { 29771da177e4SLinus Torvalds err = -EINVAL; 29781da177e4SLinus Torvalds break; 29791da177e4SLinus Torvalds } 29801da177e4SLinus Torvalds err = file_has_perm(current, file, FILE__LOCK); 29811da177e4SLinus Torvalds break; 29821da177e4SLinus Torvalds } 29831da177e4SLinus Torvalds 29841da177e4SLinus Torvalds return err; 29851da177e4SLinus Torvalds } 29861da177e4SLinus Torvalds 29871da177e4SLinus Torvalds static int selinux_file_set_fowner(struct file *file) 29881da177e4SLinus Torvalds { 29891da177e4SLinus Torvalds struct task_security_struct *tsec; 29901da177e4SLinus Torvalds struct file_security_struct *fsec; 29911da177e4SLinus Torvalds 29921da177e4SLinus Torvalds tsec = current->security; 29931da177e4SLinus Torvalds fsec = file->f_security; 29941da177e4SLinus Torvalds fsec->fown_sid = tsec->sid; 29951da177e4SLinus Torvalds 29961da177e4SLinus Torvalds return 0; 29971da177e4SLinus Torvalds } 29981da177e4SLinus Torvalds 29991da177e4SLinus Torvalds static int selinux_file_send_sigiotask(struct task_struct *tsk, 30001da177e4SLinus Torvalds struct fown_struct *fown, int signum) 30011da177e4SLinus Torvalds { 30021da177e4SLinus Torvalds struct file *file; 30031da177e4SLinus Torvalds u32 perm; 30041da177e4SLinus Torvalds struct task_security_struct *tsec; 30051da177e4SLinus Torvalds struct file_security_struct *fsec; 30061da177e4SLinus Torvalds 30071da177e4SLinus Torvalds /* struct fown_struct is never outside the context of a struct file */ 3008b385a144SRobert P. J. Day file = container_of(fown, struct file, f_owner); 30091da177e4SLinus Torvalds 30101da177e4SLinus Torvalds tsec = tsk->security; 30111da177e4SLinus Torvalds fsec = file->f_security; 30121da177e4SLinus Torvalds 30131da177e4SLinus Torvalds if (!signum) 30141da177e4SLinus Torvalds perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */ 30151da177e4SLinus Torvalds else 30161da177e4SLinus Torvalds perm = signal_to_av(signum); 30171da177e4SLinus Torvalds 30181da177e4SLinus Torvalds return avc_has_perm(fsec->fown_sid, tsec->sid, 30191da177e4SLinus Torvalds SECCLASS_PROCESS, perm, NULL); 30201da177e4SLinus Torvalds } 30211da177e4SLinus Torvalds 30221da177e4SLinus Torvalds static int selinux_file_receive(struct file *file) 30231da177e4SLinus Torvalds { 30241da177e4SLinus Torvalds return file_has_perm(current, file, file_to_av(file)); 30251da177e4SLinus Torvalds } 30261da177e4SLinus Torvalds 3027788e7dd4SYuichi Nakamura static int selinux_dentry_open(struct file *file) 3028788e7dd4SYuichi Nakamura { 3029788e7dd4SYuichi Nakamura struct file_security_struct *fsec; 3030788e7dd4SYuichi Nakamura struct inode *inode; 3031788e7dd4SYuichi Nakamura struct inode_security_struct *isec; 3032788e7dd4SYuichi Nakamura inode = file->f_path.dentry->d_inode; 3033788e7dd4SYuichi Nakamura fsec = file->f_security; 3034788e7dd4SYuichi Nakamura isec = inode->i_security; 3035788e7dd4SYuichi Nakamura /* 3036788e7dd4SYuichi Nakamura * Save inode label and policy sequence number 3037788e7dd4SYuichi Nakamura * at open-time so that selinux_file_permission 3038788e7dd4SYuichi Nakamura * can determine whether revalidation is necessary. 3039788e7dd4SYuichi Nakamura * Task label is already saved in the file security 3040788e7dd4SYuichi Nakamura * struct as its SID. 3041788e7dd4SYuichi Nakamura */ 3042788e7dd4SYuichi Nakamura fsec->isid = isec->sid; 3043788e7dd4SYuichi Nakamura fsec->pseqno = avc_policy_seqno(); 3044788e7dd4SYuichi Nakamura /* 3045788e7dd4SYuichi Nakamura * Since the inode label or policy seqno may have changed 3046788e7dd4SYuichi Nakamura * between the selinux_inode_permission check and the saving 3047788e7dd4SYuichi Nakamura * of state above, recheck that access is still permitted. 3048788e7dd4SYuichi Nakamura * Otherwise, access might never be revalidated against the 3049788e7dd4SYuichi Nakamura * new inode label or new policy. 3050788e7dd4SYuichi Nakamura * This check is not redundant - do not remove. 3051788e7dd4SYuichi Nakamura */ 3052788e7dd4SYuichi Nakamura return inode_has_perm(current, inode, file_to_av(file), NULL); 3053788e7dd4SYuichi Nakamura } 3054788e7dd4SYuichi Nakamura 30551da177e4SLinus Torvalds /* task security operations */ 30561da177e4SLinus Torvalds 30571da177e4SLinus Torvalds static int selinux_task_create(unsigned long clone_flags) 30581da177e4SLinus Torvalds { 30591da177e4SLinus Torvalds int rc; 30601da177e4SLinus Torvalds 30611da177e4SLinus Torvalds rc = secondary_ops->task_create(clone_flags); 30621da177e4SLinus Torvalds if (rc) 30631da177e4SLinus Torvalds return rc; 30641da177e4SLinus Torvalds 30651da177e4SLinus Torvalds return task_has_perm(current, current, PROCESS__FORK); 30661da177e4SLinus Torvalds } 30671da177e4SLinus Torvalds 30681da177e4SLinus Torvalds static int selinux_task_alloc_security(struct task_struct *tsk) 30691da177e4SLinus Torvalds { 30701da177e4SLinus Torvalds struct task_security_struct *tsec1, *tsec2; 30711da177e4SLinus Torvalds int rc; 30721da177e4SLinus Torvalds 30731da177e4SLinus Torvalds tsec1 = current->security; 30741da177e4SLinus Torvalds 30751da177e4SLinus Torvalds rc = task_alloc_security(tsk); 30761da177e4SLinus Torvalds if (rc) 30771da177e4SLinus Torvalds return rc; 30781da177e4SLinus Torvalds tsec2 = tsk->security; 30791da177e4SLinus Torvalds 30801da177e4SLinus Torvalds tsec2->osid = tsec1->osid; 30811da177e4SLinus Torvalds tsec2->sid = tsec1->sid; 30821da177e4SLinus Torvalds 308328eba5bfSMichael LeMay /* Retain the exec, fs, key, and sock SIDs across fork */ 30841da177e4SLinus Torvalds tsec2->exec_sid = tsec1->exec_sid; 30851da177e4SLinus Torvalds tsec2->create_sid = tsec1->create_sid; 308628eba5bfSMichael LeMay tsec2->keycreate_sid = tsec1->keycreate_sid; 308742c3e03eSEric Paris tsec2->sockcreate_sid = tsec1->sockcreate_sid; 30881da177e4SLinus Torvalds 30891da177e4SLinus Torvalds /* Retain ptracer SID across fork, if any. 30901da177e4SLinus Torvalds This will be reset by the ptrace hook upon any 30911da177e4SLinus Torvalds subsequent ptrace_attach operations. */ 30921da177e4SLinus Torvalds tsec2->ptrace_sid = tsec1->ptrace_sid; 30931da177e4SLinus Torvalds 30941da177e4SLinus Torvalds return 0; 30951da177e4SLinus Torvalds } 30961da177e4SLinus Torvalds 30971da177e4SLinus Torvalds static void selinux_task_free_security(struct task_struct *tsk) 30981da177e4SLinus Torvalds { 30991da177e4SLinus Torvalds task_free_security(tsk); 31001da177e4SLinus Torvalds } 31011da177e4SLinus Torvalds 31021da177e4SLinus Torvalds static int selinux_task_setuid(uid_t id0, uid_t id1, uid_t id2, int flags) 31031da177e4SLinus Torvalds { 31041da177e4SLinus Torvalds /* Since setuid only affects the current process, and 31051da177e4SLinus Torvalds since the SELinux controls are not based on the Linux 31061da177e4SLinus Torvalds identity attributes, SELinux does not need to control 31071da177e4SLinus Torvalds this operation. However, SELinux does control the use 31081da177e4SLinus Torvalds of the CAP_SETUID and CAP_SETGID capabilities using the 31091da177e4SLinus Torvalds capable hook. */ 31101da177e4SLinus Torvalds return 0; 31111da177e4SLinus Torvalds } 31121da177e4SLinus Torvalds 31131da177e4SLinus Torvalds static int selinux_task_post_setuid(uid_t id0, uid_t id1, uid_t id2, int flags) 31141da177e4SLinus Torvalds { 31151da177e4SLinus Torvalds return secondary_ops->task_post_setuid(id0,id1,id2,flags); 31161da177e4SLinus Torvalds } 31171da177e4SLinus Torvalds 31181da177e4SLinus Torvalds static int selinux_task_setgid(gid_t id0, gid_t id1, gid_t id2, int flags) 31191da177e4SLinus Torvalds { 31201da177e4SLinus Torvalds /* See the comment for setuid above. */ 31211da177e4SLinus Torvalds return 0; 31221da177e4SLinus Torvalds } 31231da177e4SLinus Torvalds 31241da177e4SLinus Torvalds static int selinux_task_setpgid(struct task_struct *p, pid_t pgid) 31251da177e4SLinus Torvalds { 31261da177e4SLinus Torvalds return task_has_perm(current, p, PROCESS__SETPGID); 31271da177e4SLinus Torvalds } 31281da177e4SLinus Torvalds 31291da177e4SLinus Torvalds static int selinux_task_getpgid(struct task_struct *p) 31301da177e4SLinus Torvalds { 31311da177e4SLinus Torvalds return task_has_perm(current, p, PROCESS__GETPGID); 31321da177e4SLinus Torvalds } 31331da177e4SLinus Torvalds 31341da177e4SLinus Torvalds static int selinux_task_getsid(struct task_struct *p) 31351da177e4SLinus Torvalds { 31361da177e4SLinus Torvalds return task_has_perm(current, p, PROCESS__GETSESSION); 31371da177e4SLinus Torvalds } 31381da177e4SLinus Torvalds 3139f9008e4cSDavid Quigley static void selinux_task_getsecid(struct task_struct *p, u32 *secid) 3140f9008e4cSDavid Quigley { 3141f9008e4cSDavid Quigley selinux_get_task_sid(p, secid); 3142f9008e4cSDavid Quigley } 3143f9008e4cSDavid Quigley 31441da177e4SLinus Torvalds static int selinux_task_setgroups(struct group_info *group_info) 31451da177e4SLinus Torvalds { 31461da177e4SLinus Torvalds /* See the comment for setuid above. */ 31471da177e4SLinus Torvalds return 0; 31481da177e4SLinus Torvalds } 31491da177e4SLinus Torvalds 31501da177e4SLinus Torvalds static int selinux_task_setnice(struct task_struct *p, int nice) 31511da177e4SLinus Torvalds { 31521da177e4SLinus Torvalds int rc; 31531da177e4SLinus Torvalds 31541da177e4SLinus Torvalds rc = secondary_ops->task_setnice(p, nice); 31551da177e4SLinus Torvalds if (rc) 31561da177e4SLinus Torvalds return rc; 31571da177e4SLinus Torvalds 31581da177e4SLinus Torvalds return task_has_perm(current,p, PROCESS__SETSCHED); 31591da177e4SLinus Torvalds } 31601da177e4SLinus Torvalds 316103e68060SJames Morris static int selinux_task_setioprio(struct task_struct *p, int ioprio) 316203e68060SJames Morris { 3163b5376771SSerge E. Hallyn int rc; 3164b5376771SSerge E. Hallyn 3165b5376771SSerge E. Hallyn rc = secondary_ops->task_setioprio(p, ioprio); 3166b5376771SSerge E. Hallyn if (rc) 3167b5376771SSerge E. Hallyn return rc; 3168b5376771SSerge E. Hallyn 316903e68060SJames Morris return task_has_perm(current, p, PROCESS__SETSCHED); 317003e68060SJames Morris } 317103e68060SJames Morris 3172a1836a42SDavid Quigley static int selinux_task_getioprio(struct task_struct *p) 3173a1836a42SDavid Quigley { 3174a1836a42SDavid Quigley return task_has_perm(current, p, PROCESS__GETSCHED); 3175a1836a42SDavid Quigley } 3176a1836a42SDavid Quigley 31771da177e4SLinus Torvalds static int selinux_task_setrlimit(unsigned int resource, struct rlimit *new_rlim) 31781da177e4SLinus Torvalds { 31791da177e4SLinus Torvalds struct rlimit *old_rlim = current->signal->rlim + resource; 31801da177e4SLinus Torvalds int rc; 31811da177e4SLinus Torvalds 31821da177e4SLinus Torvalds rc = secondary_ops->task_setrlimit(resource, new_rlim); 31831da177e4SLinus Torvalds if (rc) 31841da177e4SLinus Torvalds return rc; 31851da177e4SLinus Torvalds 31861da177e4SLinus Torvalds /* Control the ability to change the hard limit (whether 31871da177e4SLinus Torvalds lowering or raising it), so that the hard limit can 31881da177e4SLinus Torvalds later be used as a safe reset point for the soft limit 31891da177e4SLinus Torvalds upon context transitions. See selinux_bprm_apply_creds. */ 31901da177e4SLinus Torvalds if (old_rlim->rlim_max != new_rlim->rlim_max) 31911da177e4SLinus Torvalds return task_has_perm(current, current, PROCESS__SETRLIMIT); 31921da177e4SLinus Torvalds 31931da177e4SLinus Torvalds return 0; 31941da177e4SLinus Torvalds } 31951da177e4SLinus Torvalds 31961da177e4SLinus Torvalds static int selinux_task_setscheduler(struct task_struct *p, int policy, struct sched_param *lp) 31971da177e4SLinus Torvalds { 3198b5376771SSerge E. Hallyn int rc; 3199b5376771SSerge E. Hallyn 3200b5376771SSerge E. Hallyn rc = secondary_ops->task_setscheduler(p, policy, lp); 3201b5376771SSerge E. Hallyn if (rc) 3202b5376771SSerge E. Hallyn return rc; 3203b5376771SSerge E. Hallyn 32041da177e4SLinus Torvalds return task_has_perm(current, p, PROCESS__SETSCHED); 32051da177e4SLinus Torvalds } 32061da177e4SLinus Torvalds 32071da177e4SLinus Torvalds static int selinux_task_getscheduler(struct task_struct *p) 32081da177e4SLinus Torvalds { 32091da177e4SLinus Torvalds return task_has_perm(current, p, PROCESS__GETSCHED); 32101da177e4SLinus Torvalds } 32111da177e4SLinus Torvalds 321235601547SDavid Quigley static int selinux_task_movememory(struct task_struct *p) 321335601547SDavid Quigley { 321435601547SDavid Quigley return task_has_perm(current, p, PROCESS__SETSCHED); 321535601547SDavid Quigley } 321635601547SDavid Quigley 3217f9008e4cSDavid Quigley static int selinux_task_kill(struct task_struct *p, struct siginfo *info, 3218f9008e4cSDavid Quigley int sig, u32 secid) 32191da177e4SLinus Torvalds { 32201da177e4SLinus Torvalds u32 perm; 32211da177e4SLinus Torvalds int rc; 3222f9008e4cSDavid Quigley struct task_security_struct *tsec; 32231da177e4SLinus Torvalds 3224f9008e4cSDavid Quigley rc = secondary_ops->task_kill(p, info, sig, secid); 32251da177e4SLinus Torvalds if (rc) 32261da177e4SLinus Torvalds return rc; 32271da177e4SLinus Torvalds 3228621d3121SOleg Nesterov if (info != SEND_SIG_NOINFO && (is_si_special(info) || SI_FROMKERNEL(info))) 32291da177e4SLinus Torvalds return 0; 32301da177e4SLinus Torvalds 32311da177e4SLinus Torvalds if (!sig) 32321da177e4SLinus Torvalds perm = PROCESS__SIGNULL; /* null signal; existence test */ 32331da177e4SLinus Torvalds else 32341da177e4SLinus Torvalds perm = signal_to_av(sig); 3235f9008e4cSDavid Quigley tsec = p->security; 3236f9008e4cSDavid Quigley if (secid) 3237f9008e4cSDavid Quigley rc = avc_has_perm(secid, tsec->sid, SECCLASS_PROCESS, perm, NULL); 3238f9008e4cSDavid Quigley else 3239f9008e4cSDavid Quigley rc = task_has_perm(current, p, perm); 3240f9008e4cSDavid Quigley return rc; 32411da177e4SLinus Torvalds } 32421da177e4SLinus Torvalds 32431da177e4SLinus Torvalds static int selinux_task_prctl(int option, 32441da177e4SLinus Torvalds unsigned long arg2, 32451da177e4SLinus Torvalds unsigned long arg3, 32461da177e4SLinus Torvalds unsigned long arg4, 32471da177e4SLinus Torvalds unsigned long arg5) 32481da177e4SLinus Torvalds { 32491da177e4SLinus Torvalds /* The current prctl operations do not appear to require 32501da177e4SLinus Torvalds any SELinux controls since they merely observe or modify 32511da177e4SLinus Torvalds the state of the current process. */ 32521da177e4SLinus Torvalds return 0; 32531da177e4SLinus Torvalds } 32541da177e4SLinus Torvalds 32551da177e4SLinus Torvalds static int selinux_task_wait(struct task_struct *p) 32561da177e4SLinus Torvalds { 32578a535140SEric Paris return task_has_perm(p, current, PROCESS__SIGCHLD); 32581da177e4SLinus Torvalds } 32591da177e4SLinus Torvalds 32601da177e4SLinus Torvalds static void selinux_task_reparent_to_init(struct task_struct *p) 32611da177e4SLinus Torvalds { 32621da177e4SLinus Torvalds struct task_security_struct *tsec; 32631da177e4SLinus Torvalds 32641da177e4SLinus Torvalds secondary_ops->task_reparent_to_init(p); 32651da177e4SLinus Torvalds 32661da177e4SLinus Torvalds tsec = p->security; 32671da177e4SLinus Torvalds tsec->osid = tsec->sid; 32681da177e4SLinus Torvalds tsec->sid = SECINITSID_KERNEL; 32691da177e4SLinus Torvalds return; 32701da177e4SLinus Torvalds } 32711da177e4SLinus Torvalds 32721da177e4SLinus Torvalds static void selinux_task_to_inode(struct task_struct *p, 32731da177e4SLinus Torvalds struct inode *inode) 32741da177e4SLinus Torvalds { 32751da177e4SLinus Torvalds struct task_security_struct *tsec = p->security; 32761da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 32771da177e4SLinus Torvalds 32781da177e4SLinus Torvalds isec->sid = tsec->sid; 32791da177e4SLinus Torvalds isec->initialized = 1; 32801da177e4SLinus Torvalds return; 32811da177e4SLinus Torvalds } 32821da177e4SLinus Torvalds 32831da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 328467f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv4(struct sk_buff *skb, 328567f83cbfSVenkat Yekkirala struct avc_audit_data *ad, u8 *proto) 32861da177e4SLinus Torvalds { 32871da177e4SLinus Torvalds int offset, ihlen, ret = -EINVAL; 32881da177e4SLinus Torvalds struct iphdr _iph, *ih; 32891da177e4SLinus Torvalds 3290bbe735e4SArnaldo Carvalho de Melo offset = skb_network_offset(skb); 32911da177e4SLinus Torvalds ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph); 32921da177e4SLinus Torvalds if (ih == NULL) 32931da177e4SLinus Torvalds goto out; 32941da177e4SLinus Torvalds 32951da177e4SLinus Torvalds ihlen = ih->ihl * 4; 32961da177e4SLinus Torvalds if (ihlen < sizeof(_iph)) 32971da177e4SLinus Torvalds goto out; 32981da177e4SLinus Torvalds 32991da177e4SLinus Torvalds ad->u.net.v4info.saddr = ih->saddr; 33001da177e4SLinus Torvalds ad->u.net.v4info.daddr = ih->daddr; 33011da177e4SLinus Torvalds ret = 0; 33021da177e4SLinus Torvalds 330367f83cbfSVenkat Yekkirala if (proto) 330467f83cbfSVenkat Yekkirala *proto = ih->protocol; 330567f83cbfSVenkat Yekkirala 33061da177e4SLinus Torvalds switch (ih->protocol) { 33071da177e4SLinus Torvalds case IPPROTO_TCP: { 33081da177e4SLinus Torvalds struct tcphdr _tcph, *th; 33091da177e4SLinus Torvalds 33101da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 33111da177e4SLinus Torvalds break; 33121da177e4SLinus Torvalds 33131da177e4SLinus Torvalds offset += ihlen; 33141da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 33151da177e4SLinus Torvalds if (th == NULL) 33161da177e4SLinus Torvalds break; 33171da177e4SLinus Torvalds 33181da177e4SLinus Torvalds ad->u.net.sport = th->source; 33191da177e4SLinus Torvalds ad->u.net.dport = th->dest; 33201da177e4SLinus Torvalds break; 33211da177e4SLinus Torvalds } 33221da177e4SLinus Torvalds 33231da177e4SLinus Torvalds case IPPROTO_UDP: { 33241da177e4SLinus Torvalds struct udphdr _udph, *uh; 33251da177e4SLinus Torvalds 33261da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 33271da177e4SLinus Torvalds break; 33281da177e4SLinus Torvalds 33291da177e4SLinus Torvalds offset += ihlen; 33301da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 33311da177e4SLinus Torvalds if (uh == NULL) 33321da177e4SLinus Torvalds break; 33331da177e4SLinus Torvalds 33341da177e4SLinus Torvalds ad->u.net.sport = uh->source; 33351da177e4SLinus Torvalds ad->u.net.dport = uh->dest; 33361da177e4SLinus Torvalds break; 33371da177e4SLinus Torvalds } 33381da177e4SLinus Torvalds 33392ee92d46SJames Morris case IPPROTO_DCCP: { 33402ee92d46SJames Morris struct dccp_hdr _dccph, *dh; 33412ee92d46SJames Morris 33422ee92d46SJames Morris if (ntohs(ih->frag_off) & IP_OFFSET) 33432ee92d46SJames Morris break; 33442ee92d46SJames Morris 33452ee92d46SJames Morris offset += ihlen; 33462ee92d46SJames Morris dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); 33472ee92d46SJames Morris if (dh == NULL) 33482ee92d46SJames Morris break; 33492ee92d46SJames Morris 33502ee92d46SJames Morris ad->u.net.sport = dh->dccph_sport; 33512ee92d46SJames Morris ad->u.net.dport = dh->dccph_dport; 33522ee92d46SJames Morris break; 33532ee92d46SJames Morris } 33542ee92d46SJames Morris 33551da177e4SLinus Torvalds default: 33561da177e4SLinus Torvalds break; 33571da177e4SLinus Torvalds } 33581da177e4SLinus Torvalds out: 33591da177e4SLinus Torvalds return ret; 33601da177e4SLinus Torvalds } 33611da177e4SLinus Torvalds 33621da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 33631da177e4SLinus Torvalds 33641da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 336567f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv6(struct sk_buff *skb, 336667f83cbfSVenkat Yekkirala struct avc_audit_data *ad, u8 *proto) 33671da177e4SLinus Torvalds { 33681da177e4SLinus Torvalds u8 nexthdr; 33691da177e4SLinus Torvalds int ret = -EINVAL, offset; 33701da177e4SLinus Torvalds struct ipv6hdr _ipv6h, *ip6; 33711da177e4SLinus Torvalds 3372bbe735e4SArnaldo Carvalho de Melo offset = skb_network_offset(skb); 33731da177e4SLinus Torvalds ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h); 33741da177e4SLinus Torvalds if (ip6 == NULL) 33751da177e4SLinus Torvalds goto out; 33761da177e4SLinus Torvalds 33771da177e4SLinus Torvalds ipv6_addr_copy(&ad->u.net.v6info.saddr, &ip6->saddr); 33781da177e4SLinus Torvalds ipv6_addr_copy(&ad->u.net.v6info.daddr, &ip6->daddr); 33791da177e4SLinus Torvalds ret = 0; 33801da177e4SLinus Torvalds 33811da177e4SLinus Torvalds nexthdr = ip6->nexthdr; 33821da177e4SLinus Torvalds offset += sizeof(_ipv6h); 33830d3d077cSHerbert Xu offset = ipv6_skip_exthdr(skb, offset, &nexthdr); 33841da177e4SLinus Torvalds if (offset < 0) 33851da177e4SLinus Torvalds goto out; 33861da177e4SLinus Torvalds 338767f83cbfSVenkat Yekkirala if (proto) 338867f83cbfSVenkat Yekkirala *proto = nexthdr; 338967f83cbfSVenkat Yekkirala 33901da177e4SLinus Torvalds switch (nexthdr) { 33911da177e4SLinus Torvalds case IPPROTO_TCP: { 33921da177e4SLinus Torvalds struct tcphdr _tcph, *th; 33931da177e4SLinus Torvalds 33941da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 33951da177e4SLinus Torvalds if (th == NULL) 33961da177e4SLinus Torvalds break; 33971da177e4SLinus Torvalds 33981da177e4SLinus Torvalds ad->u.net.sport = th->source; 33991da177e4SLinus Torvalds ad->u.net.dport = th->dest; 34001da177e4SLinus Torvalds break; 34011da177e4SLinus Torvalds } 34021da177e4SLinus Torvalds 34031da177e4SLinus Torvalds case IPPROTO_UDP: { 34041da177e4SLinus Torvalds struct udphdr _udph, *uh; 34051da177e4SLinus Torvalds 34061da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 34071da177e4SLinus Torvalds if (uh == NULL) 34081da177e4SLinus Torvalds break; 34091da177e4SLinus Torvalds 34101da177e4SLinus Torvalds ad->u.net.sport = uh->source; 34111da177e4SLinus Torvalds ad->u.net.dport = uh->dest; 34121da177e4SLinus Torvalds break; 34131da177e4SLinus Torvalds } 34141da177e4SLinus Torvalds 34152ee92d46SJames Morris case IPPROTO_DCCP: { 34162ee92d46SJames Morris struct dccp_hdr _dccph, *dh; 34172ee92d46SJames Morris 34182ee92d46SJames Morris dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); 34192ee92d46SJames Morris if (dh == NULL) 34202ee92d46SJames Morris break; 34212ee92d46SJames Morris 34222ee92d46SJames Morris ad->u.net.sport = dh->dccph_sport; 34232ee92d46SJames Morris ad->u.net.dport = dh->dccph_dport; 34242ee92d46SJames Morris break; 34252ee92d46SJames Morris } 34262ee92d46SJames Morris 34271da177e4SLinus Torvalds /* includes fragments */ 34281da177e4SLinus Torvalds default: 34291da177e4SLinus Torvalds break; 34301da177e4SLinus Torvalds } 34311da177e4SLinus Torvalds out: 34321da177e4SLinus Torvalds return ret; 34331da177e4SLinus Torvalds } 34341da177e4SLinus Torvalds 34351da177e4SLinus Torvalds #endif /* IPV6 */ 34361da177e4SLinus Torvalds 34371da177e4SLinus Torvalds static int selinux_parse_skb(struct sk_buff *skb, struct avc_audit_data *ad, 3438224dfbd8SPaul Moore char **addrp, int src, u8 *proto) 34391da177e4SLinus Torvalds { 34401da177e4SLinus Torvalds int ret = 0; 34411da177e4SLinus Torvalds 34421da177e4SLinus Torvalds switch (ad->u.net.family) { 34431da177e4SLinus Torvalds case PF_INET: 344467f83cbfSVenkat Yekkirala ret = selinux_parse_skb_ipv4(skb, ad, proto); 34451da177e4SLinus Torvalds if (ret || !addrp) 34461da177e4SLinus Torvalds break; 34471da177e4SLinus Torvalds *addrp = (char *)(src ? &ad->u.net.v4info.saddr : 34481da177e4SLinus Torvalds &ad->u.net.v4info.daddr); 34491da177e4SLinus Torvalds break; 34501da177e4SLinus Torvalds 34511da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 34521da177e4SLinus Torvalds case PF_INET6: 345367f83cbfSVenkat Yekkirala ret = selinux_parse_skb_ipv6(skb, ad, proto); 34541da177e4SLinus Torvalds if (ret || !addrp) 34551da177e4SLinus Torvalds break; 34561da177e4SLinus Torvalds *addrp = (char *)(src ? &ad->u.net.v6info.saddr : 34571da177e4SLinus Torvalds &ad->u.net.v6info.daddr); 34581da177e4SLinus Torvalds break; 34591da177e4SLinus Torvalds #endif /* IPV6 */ 34601da177e4SLinus Torvalds default: 34611da177e4SLinus Torvalds break; 34621da177e4SLinus Torvalds } 34631da177e4SLinus Torvalds 346471f1cb05SPaul Moore if (unlikely(ret)) 346571f1cb05SPaul Moore printk(KERN_WARNING 346671f1cb05SPaul Moore "SELinux: failure in selinux_parse_skb()," 346771f1cb05SPaul Moore " unable to parse packet\n"); 346871f1cb05SPaul Moore 34691da177e4SLinus Torvalds return ret; 34701da177e4SLinus Torvalds } 34711da177e4SLinus Torvalds 34724f6a993fSPaul Moore /** 3473220deb96SPaul Moore * selinux_skb_peerlbl_sid - Determine the peer label of a packet 34744f6a993fSPaul Moore * @skb: the packet 347575e22910SPaul Moore * @family: protocol family 3476220deb96SPaul Moore * @sid: the packet's peer label SID 34774f6a993fSPaul Moore * 34784f6a993fSPaul Moore * Description: 3479220deb96SPaul Moore * Check the various different forms of network peer labeling and determine 3480220deb96SPaul Moore * the peer label/SID for the packet; most of the magic actually occurs in 3481220deb96SPaul Moore * the security server function security_net_peersid_cmp(). The function 3482220deb96SPaul Moore * returns zero if the value in @sid is valid (although it may be SECSID_NULL) 3483220deb96SPaul Moore * or -EACCES if @sid is invalid due to inconsistencies with the different 3484220deb96SPaul Moore * peer labels. 34854f6a993fSPaul Moore * 34864f6a993fSPaul Moore */ 3487220deb96SPaul Moore static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid) 34884f6a993fSPaul Moore { 348971f1cb05SPaul Moore int err; 34904f6a993fSPaul Moore u32 xfrm_sid; 34914f6a993fSPaul Moore u32 nlbl_sid; 3492220deb96SPaul Moore u32 nlbl_type; 34934f6a993fSPaul Moore 34944f6a993fSPaul Moore selinux_skb_xfrm_sid(skb, &xfrm_sid); 34955dbe1eb0SPaul Moore selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid); 3496220deb96SPaul Moore 349771f1cb05SPaul Moore err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid); 349871f1cb05SPaul Moore if (unlikely(err)) { 349971f1cb05SPaul Moore printk(KERN_WARNING 350071f1cb05SPaul Moore "SELinux: failure in selinux_skb_peerlbl_sid()," 350171f1cb05SPaul Moore " unable to determine packet's peer label\n"); 3502220deb96SPaul Moore return -EACCES; 350371f1cb05SPaul Moore } 3504220deb96SPaul Moore 3505220deb96SPaul Moore return 0; 35064f6a993fSPaul Moore } 35074f6a993fSPaul Moore 35081da177e4SLinus Torvalds /* socket security operations */ 35091da177e4SLinus Torvalds static int socket_has_perm(struct task_struct *task, struct socket *sock, 35101da177e4SLinus Torvalds u32 perms) 35111da177e4SLinus Torvalds { 35121da177e4SLinus Torvalds struct inode_security_struct *isec; 35131da177e4SLinus Torvalds struct task_security_struct *tsec; 35141da177e4SLinus Torvalds struct avc_audit_data ad; 35151da177e4SLinus Torvalds int err = 0; 35161da177e4SLinus Torvalds 35171da177e4SLinus Torvalds tsec = task->security; 35181da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 35191da177e4SLinus Torvalds 35201da177e4SLinus Torvalds if (isec->sid == SECINITSID_KERNEL) 35211da177e4SLinus Torvalds goto out; 35221da177e4SLinus Torvalds 35231da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,NET); 35241da177e4SLinus Torvalds ad.u.net.sk = sock->sk; 35251da177e4SLinus Torvalds err = avc_has_perm(tsec->sid, isec->sid, isec->sclass, perms, &ad); 35261da177e4SLinus Torvalds 35271da177e4SLinus Torvalds out: 35281da177e4SLinus Torvalds return err; 35291da177e4SLinus Torvalds } 35301da177e4SLinus Torvalds 35311da177e4SLinus Torvalds static int selinux_socket_create(int family, int type, 35321da177e4SLinus Torvalds int protocol, int kern) 35331da177e4SLinus Torvalds { 35341da177e4SLinus Torvalds int err = 0; 35351da177e4SLinus Torvalds struct task_security_struct *tsec; 353642c3e03eSEric Paris u32 newsid; 35371da177e4SLinus Torvalds 35381da177e4SLinus Torvalds if (kern) 35391da177e4SLinus Torvalds goto out; 35401da177e4SLinus Torvalds 35411da177e4SLinus Torvalds tsec = current->security; 354242c3e03eSEric Paris newsid = tsec->sockcreate_sid ? : tsec->sid; 354342c3e03eSEric Paris err = avc_has_perm(tsec->sid, newsid, 35441da177e4SLinus Torvalds socket_type_to_security_class(family, type, 35451da177e4SLinus Torvalds protocol), SOCKET__CREATE, NULL); 35461da177e4SLinus Torvalds 35471da177e4SLinus Torvalds out: 35481da177e4SLinus Torvalds return err; 35491da177e4SLinus Torvalds } 35501da177e4SLinus Torvalds 35517420ed23SVenkat Yekkirala static int selinux_socket_post_create(struct socket *sock, int family, 35521da177e4SLinus Torvalds int type, int protocol, int kern) 35531da177e4SLinus Torvalds { 35547420ed23SVenkat Yekkirala int err = 0; 35551da177e4SLinus Torvalds struct inode_security_struct *isec; 35561da177e4SLinus Torvalds struct task_security_struct *tsec; 3557892c141eSVenkat Yekkirala struct sk_security_struct *sksec; 355842c3e03eSEric Paris u32 newsid; 35591da177e4SLinus Torvalds 35601da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 35611da177e4SLinus Torvalds 35621da177e4SLinus Torvalds tsec = current->security; 356342c3e03eSEric Paris newsid = tsec->sockcreate_sid ? : tsec->sid; 35641da177e4SLinus Torvalds isec->sclass = socket_type_to_security_class(family, type, protocol); 356542c3e03eSEric Paris isec->sid = kern ? SECINITSID_KERNEL : newsid; 35661da177e4SLinus Torvalds isec->initialized = 1; 35671da177e4SLinus Torvalds 3568892c141eSVenkat Yekkirala if (sock->sk) { 3569892c141eSVenkat Yekkirala sksec = sock->sk->sk_security; 3570892c141eSVenkat Yekkirala sksec->sid = isec->sid; 3571220deb96SPaul Moore sksec->sclass = isec->sclass; 35729f2ad665SPaul Moore err = selinux_netlbl_socket_post_create(sock); 3573892c141eSVenkat Yekkirala } 3574892c141eSVenkat Yekkirala 35757420ed23SVenkat Yekkirala return err; 35761da177e4SLinus Torvalds } 35771da177e4SLinus Torvalds 35781da177e4SLinus Torvalds /* Range of port numbers used to automatically bind. 35791da177e4SLinus Torvalds Need to determine whether we should perform a name_bind 35801da177e4SLinus Torvalds permission check between the socket and the port number. */ 35811da177e4SLinus Torvalds 35821da177e4SLinus Torvalds static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen) 35831da177e4SLinus Torvalds { 35841da177e4SLinus Torvalds u16 family; 35851da177e4SLinus Torvalds int err; 35861da177e4SLinus Torvalds 35871da177e4SLinus Torvalds err = socket_has_perm(current, sock, SOCKET__BIND); 35881da177e4SLinus Torvalds if (err) 35891da177e4SLinus Torvalds goto out; 35901da177e4SLinus Torvalds 35911da177e4SLinus Torvalds /* 35921da177e4SLinus Torvalds * If PF_INET or PF_INET6, check name_bind permission for the port. 359313402580SJames Morris * Multiple address binding for SCTP is not supported yet: we just 359413402580SJames Morris * check the first address now. 35951da177e4SLinus Torvalds */ 35961da177e4SLinus Torvalds family = sock->sk->sk_family; 35971da177e4SLinus Torvalds if (family == PF_INET || family == PF_INET6) { 35981da177e4SLinus Torvalds char *addrp; 35991da177e4SLinus Torvalds struct inode_security_struct *isec; 36001da177e4SLinus Torvalds struct task_security_struct *tsec; 36011da177e4SLinus Torvalds struct avc_audit_data ad; 36021da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 36031da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 36041da177e4SLinus Torvalds unsigned short snum; 36051da177e4SLinus Torvalds struct sock *sk = sock->sk; 36061da177e4SLinus Torvalds u32 sid, node_perm, addrlen; 36071da177e4SLinus Torvalds 36081da177e4SLinus Torvalds tsec = current->security; 36091da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 36101da177e4SLinus Torvalds 36111da177e4SLinus Torvalds if (family == PF_INET) { 36121da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 36131da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 36141da177e4SLinus Torvalds addrlen = sizeof(addr4->sin_addr.s_addr); 36151da177e4SLinus Torvalds addrp = (char *)&addr4->sin_addr.s_addr; 36161da177e4SLinus Torvalds } else { 36171da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 36181da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 36191da177e4SLinus Torvalds addrlen = sizeof(addr6->sin6_addr.s6_addr); 36201da177e4SLinus Torvalds addrp = (char *)&addr6->sin6_addr.s6_addr; 36211da177e4SLinus Torvalds } 36221da177e4SLinus Torvalds 3623227b60f5SStephen Hemminger if (snum) { 3624227b60f5SStephen Hemminger int low, high; 3625227b60f5SStephen Hemminger 3626227b60f5SStephen Hemminger inet_get_local_port_range(&low, &high); 3627227b60f5SStephen Hemminger 3628227b60f5SStephen Hemminger if (snum < max(PROT_SOCK, low) || snum > high) { 3629227b60f5SStephen Hemminger err = security_port_sid(sk->sk_family, 3630227b60f5SStephen Hemminger sk->sk_type, 3631227b60f5SStephen Hemminger sk->sk_protocol, snum, 3632227b60f5SStephen Hemminger &sid); 36331da177e4SLinus Torvalds if (err) 36341da177e4SLinus Torvalds goto out; 36351da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,NET); 36361da177e4SLinus Torvalds ad.u.net.sport = htons(snum); 36371da177e4SLinus Torvalds ad.u.net.family = family; 36381da177e4SLinus Torvalds err = avc_has_perm(isec->sid, sid, 36391da177e4SLinus Torvalds isec->sclass, 36401da177e4SLinus Torvalds SOCKET__NAME_BIND, &ad); 36411da177e4SLinus Torvalds if (err) 36421da177e4SLinus Torvalds goto out; 36431da177e4SLinus Torvalds } 3644227b60f5SStephen Hemminger } 36451da177e4SLinus Torvalds 364613402580SJames Morris switch(isec->sclass) { 364713402580SJames Morris case SECCLASS_TCP_SOCKET: 36481da177e4SLinus Torvalds node_perm = TCP_SOCKET__NODE_BIND; 36491da177e4SLinus Torvalds break; 36501da177e4SLinus Torvalds 365113402580SJames Morris case SECCLASS_UDP_SOCKET: 36521da177e4SLinus Torvalds node_perm = UDP_SOCKET__NODE_BIND; 36531da177e4SLinus Torvalds break; 36541da177e4SLinus Torvalds 36552ee92d46SJames Morris case SECCLASS_DCCP_SOCKET: 36562ee92d46SJames Morris node_perm = DCCP_SOCKET__NODE_BIND; 36572ee92d46SJames Morris break; 36582ee92d46SJames Morris 36591da177e4SLinus Torvalds default: 36601da177e4SLinus Torvalds node_perm = RAWIP_SOCKET__NODE_BIND; 36611da177e4SLinus Torvalds break; 36621da177e4SLinus Torvalds } 36631da177e4SLinus Torvalds 3664224dfbd8SPaul Moore err = sel_netnode_sid(addrp, family, &sid); 36651da177e4SLinus Torvalds if (err) 36661da177e4SLinus Torvalds goto out; 36671da177e4SLinus Torvalds 36681da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,NET); 36691da177e4SLinus Torvalds ad.u.net.sport = htons(snum); 36701da177e4SLinus Torvalds ad.u.net.family = family; 36711da177e4SLinus Torvalds 36721da177e4SLinus Torvalds if (family == PF_INET) 36731da177e4SLinus Torvalds ad.u.net.v4info.saddr = addr4->sin_addr.s_addr; 36741da177e4SLinus Torvalds else 36751da177e4SLinus Torvalds ipv6_addr_copy(&ad.u.net.v6info.saddr, &addr6->sin6_addr); 36761da177e4SLinus Torvalds 36771da177e4SLinus Torvalds err = avc_has_perm(isec->sid, sid, 36781da177e4SLinus Torvalds isec->sclass, node_perm, &ad); 36791da177e4SLinus Torvalds if (err) 36801da177e4SLinus Torvalds goto out; 36811da177e4SLinus Torvalds } 36821da177e4SLinus Torvalds out: 36831da177e4SLinus Torvalds return err; 36841da177e4SLinus Torvalds } 36851da177e4SLinus Torvalds 36861da177e4SLinus Torvalds static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen) 36871da177e4SLinus Torvalds { 36881da177e4SLinus Torvalds struct inode_security_struct *isec; 36891da177e4SLinus Torvalds int err; 36901da177e4SLinus Torvalds 36911da177e4SLinus Torvalds err = socket_has_perm(current, sock, SOCKET__CONNECT); 36921da177e4SLinus Torvalds if (err) 36931da177e4SLinus Torvalds return err; 36941da177e4SLinus Torvalds 36951da177e4SLinus Torvalds /* 36962ee92d46SJames Morris * If a TCP or DCCP socket, check name_connect permission for the port. 36971da177e4SLinus Torvalds */ 36981da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 36992ee92d46SJames Morris if (isec->sclass == SECCLASS_TCP_SOCKET || 37002ee92d46SJames Morris isec->sclass == SECCLASS_DCCP_SOCKET) { 37011da177e4SLinus Torvalds struct sock *sk = sock->sk; 37021da177e4SLinus Torvalds struct avc_audit_data ad; 37031da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 37041da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 37051da177e4SLinus Torvalds unsigned short snum; 37062ee92d46SJames Morris u32 sid, perm; 37071da177e4SLinus Torvalds 37081da177e4SLinus Torvalds if (sk->sk_family == PF_INET) { 37091da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 3710911656f8SStephen Smalley if (addrlen < sizeof(struct sockaddr_in)) 37111da177e4SLinus Torvalds return -EINVAL; 37121da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 37131da177e4SLinus Torvalds } else { 37141da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 3715911656f8SStephen Smalley if (addrlen < SIN6_LEN_RFC2133) 37161da177e4SLinus Torvalds return -EINVAL; 37171da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 37181da177e4SLinus Torvalds } 37191da177e4SLinus Torvalds 37201da177e4SLinus Torvalds err = security_port_sid(sk->sk_family, sk->sk_type, 37211da177e4SLinus Torvalds sk->sk_protocol, snum, &sid); 37221da177e4SLinus Torvalds if (err) 37231da177e4SLinus Torvalds goto out; 37241da177e4SLinus Torvalds 37252ee92d46SJames Morris perm = (isec->sclass == SECCLASS_TCP_SOCKET) ? 37262ee92d46SJames Morris TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT; 37272ee92d46SJames Morris 37281da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,NET); 37291da177e4SLinus Torvalds ad.u.net.dport = htons(snum); 37301da177e4SLinus Torvalds ad.u.net.family = sk->sk_family; 37312ee92d46SJames Morris err = avc_has_perm(isec->sid, sid, isec->sclass, perm, &ad); 37321da177e4SLinus Torvalds if (err) 37331da177e4SLinus Torvalds goto out; 37341da177e4SLinus Torvalds } 37351da177e4SLinus Torvalds 37361da177e4SLinus Torvalds out: 37371da177e4SLinus Torvalds return err; 37381da177e4SLinus Torvalds } 37391da177e4SLinus Torvalds 37401da177e4SLinus Torvalds static int selinux_socket_listen(struct socket *sock, int backlog) 37411da177e4SLinus Torvalds { 37421da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__LISTEN); 37431da177e4SLinus Torvalds } 37441da177e4SLinus Torvalds 37451da177e4SLinus Torvalds static int selinux_socket_accept(struct socket *sock, struct socket *newsock) 37461da177e4SLinus Torvalds { 37471da177e4SLinus Torvalds int err; 37481da177e4SLinus Torvalds struct inode_security_struct *isec; 37491da177e4SLinus Torvalds struct inode_security_struct *newisec; 37501da177e4SLinus Torvalds 37511da177e4SLinus Torvalds err = socket_has_perm(current, sock, SOCKET__ACCEPT); 37521da177e4SLinus Torvalds if (err) 37531da177e4SLinus Torvalds return err; 37541da177e4SLinus Torvalds 37551da177e4SLinus Torvalds newisec = SOCK_INODE(newsock)->i_security; 37561da177e4SLinus Torvalds 37571da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 37581da177e4SLinus Torvalds newisec->sclass = isec->sclass; 37591da177e4SLinus Torvalds newisec->sid = isec->sid; 37601da177e4SLinus Torvalds newisec->initialized = 1; 37611da177e4SLinus Torvalds 37621da177e4SLinus Torvalds return 0; 37631da177e4SLinus Torvalds } 37641da177e4SLinus Torvalds 37651da177e4SLinus Torvalds static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg, 37661da177e4SLinus Torvalds int size) 37671da177e4SLinus Torvalds { 37687420ed23SVenkat Yekkirala int rc; 37697420ed23SVenkat Yekkirala 37707420ed23SVenkat Yekkirala rc = socket_has_perm(current, sock, SOCKET__WRITE); 37717420ed23SVenkat Yekkirala if (rc) 37727420ed23SVenkat Yekkirala return rc; 37737420ed23SVenkat Yekkirala 37747420ed23SVenkat Yekkirala return selinux_netlbl_inode_permission(SOCK_INODE(sock), MAY_WRITE); 37751da177e4SLinus Torvalds } 37761da177e4SLinus Torvalds 37771da177e4SLinus Torvalds static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg, 37781da177e4SLinus Torvalds int size, int flags) 37791da177e4SLinus Torvalds { 37801da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__READ); 37811da177e4SLinus Torvalds } 37821da177e4SLinus Torvalds 37831da177e4SLinus Torvalds static int selinux_socket_getsockname(struct socket *sock) 37841da177e4SLinus Torvalds { 37851da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__GETATTR); 37861da177e4SLinus Torvalds } 37871da177e4SLinus Torvalds 37881da177e4SLinus Torvalds static int selinux_socket_getpeername(struct socket *sock) 37891da177e4SLinus Torvalds { 37901da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__GETATTR); 37911da177e4SLinus Torvalds } 37921da177e4SLinus Torvalds 37931da177e4SLinus Torvalds static int selinux_socket_setsockopt(struct socket *sock,int level,int optname) 37941da177e4SLinus Torvalds { 3795f8687afeSPaul Moore int err; 3796f8687afeSPaul Moore 3797f8687afeSPaul Moore err = socket_has_perm(current, sock, SOCKET__SETOPT); 3798f8687afeSPaul Moore if (err) 3799f8687afeSPaul Moore return err; 3800f8687afeSPaul Moore 3801f8687afeSPaul Moore return selinux_netlbl_socket_setsockopt(sock, level, optname); 38021da177e4SLinus Torvalds } 38031da177e4SLinus Torvalds 38041da177e4SLinus Torvalds static int selinux_socket_getsockopt(struct socket *sock, int level, 38051da177e4SLinus Torvalds int optname) 38061da177e4SLinus Torvalds { 38071da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__GETOPT); 38081da177e4SLinus Torvalds } 38091da177e4SLinus Torvalds 38101da177e4SLinus Torvalds static int selinux_socket_shutdown(struct socket *sock, int how) 38111da177e4SLinus Torvalds { 38121da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__SHUTDOWN); 38131da177e4SLinus Torvalds } 38141da177e4SLinus Torvalds 38151da177e4SLinus Torvalds static int selinux_socket_unix_stream_connect(struct socket *sock, 38161da177e4SLinus Torvalds struct socket *other, 38171da177e4SLinus Torvalds struct sock *newsk) 38181da177e4SLinus Torvalds { 38191da177e4SLinus Torvalds struct sk_security_struct *ssec; 38201da177e4SLinus Torvalds struct inode_security_struct *isec; 38211da177e4SLinus Torvalds struct inode_security_struct *other_isec; 38221da177e4SLinus Torvalds struct avc_audit_data ad; 38231da177e4SLinus Torvalds int err; 38241da177e4SLinus Torvalds 38251da177e4SLinus Torvalds err = secondary_ops->unix_stream_connect(sock, other, newsk); 38261da177e4SLinus Torvalds if (err) 38271da177e4SLinus Torvalds return err; 38281da177e4SLinus Torvalds 38291da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 38301da177e4SLinus Torvalds other_isec = SOCK_INODE(other)->i_security; 38311da177e4SLinus Torvalds 38321da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,NET); 38331da177e4SLinus Torvalds ad.u.net.sk = other->sk; 38341da177e4SLinus Torvalds 38351da177e4SLinus Torvalds err = avc_has_perm(isec->sid, other_isec->sid, 38361da177e4SLinus Torvalds isec->sclass, 38371da177e4SLinus Torvalds UNIX_STREAM_SOCKET__CONNECTTO, &ad); 38381da177e4SLinus Torvalds if (err) 38391da177e4SLinus Torvalds return err; 38401da177e4SLinus Torvalds 38411da177e4SLinus Torvalds /* connecting socket */ 38421da177e4SLinus Torvalds ssec = sock->sk->sk_security; 38431da177e4SLinus Torvalds ssec->peer_sid = other_isec->sid; 38441da177e4SLinus Torvalds 38451da177e4SLinus Torvalds /* server child socket */ 38461da177e4SLinus Torvalds ssec = newsk->sk_security; 38471da177e4SLinus Torvalds ssec->peer_sid = isec->sid; 38484237c75cSVenkat Yekkirala err = security_sid_mls_copy(other_isec->sid, ssec->peer_sid, &ssec->sid); 38491da177e4SLinus Torvalds 38504237c75cSVenkat Yekkirala return err; 38511da177e4SLinus Torvalds } 38521da177e4SLinus Torvalds 38531da177e4SLinus Torvalds static int selinux_socket_unix_may_send(struct socket *sock, 38541da177e4SLinus Torvalds struct socket *other) 38551da177e4SLinus Torvalds { 38561da177e4SLinus Torvalds struct inode_security_struct *isec; 38571da177e4SLinus Torvalds struct inode_security_struct *other_isec; 38581da177e4SLinus Torvalds struct avc_audit_data ad; 38591da177e4SLinus Torvalds int err; 38601da177e4SLinus Torvalds 38611da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 38621da177e4SLinus Torvalds other_isec = SOCK_INODE(other)->i_security; 38631da177e4SLinus Torvalds 38641da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,NET); 38651da177e4SLinus Torvalds ad.u.net.sk = other->sk; 38661da177e4SLinus Torvalds 38671da177e4SLinus Torvalds err = avc_has_perm(isec->sid, other_isec->sid, 38681da177e4SLinus Torvalds isec->sclass, SOCKET__SENDTO, &ad); 38691da177e4SLinus Torvalds if (err) 38701da177e4SLinus Torvalds return err; 38711da177e4SLinus Torvalds 38721da177e4SLinus Torvalds return 0; 38731da177e4SLinus Torvalds } 38741da177e4SLinus Torvalds 3875effad8dfSPaul Moore static int selinux_inet_sys_rcv_skb(int ifindex, char *addrp, u16 family, 3876effad8dfSPaul Moore u32 peer_sid, 3877effad8dfSPaul Moore struct avc_audit_data *ad) 3878effad8dfSPaul Moore { 3879effad8dfSPaul Moore int err; 3880effad8dfSPaul Moore u32 if_sid; 3881effad8dfSPaul Moore u32 node_sid; 3882effad8dfSPaul Moore 3883effad8dfSPaul Moore err = sel_netif_sid(ifindex, &if_sid); 3884effad8dfSPaul Moore if (err) 3885effad8dfSPaul Moore return err; 3886effad8dfSPaul Moore err = avc_has_perm(peer_sid, if_sid, 3887effad8dfSPaul Moore SECCLASS_NETIF, NETIF__INGRESS, ad); 3888effad8dfSPaul Moore if (err) 3889effad8dfSPaul Moore return err; 3890effad8dfSPaul Moore 3891effad8dfSPaul Moore err = sel_netnode_sid(addrp, family, &node_sid); 3892effad8dfSPaul Moore if (err) 3893effad8dfSPaul Moore return err; 3894effad8dfSPaul Moore return avc_has_perm(peer_sid, node_sid, 3895effad8dfSPaul Moore SECCLASS_NODE, NODE__RECVFROM, ad); 3896effad8dfSPaul Moore } 3897effad8dfSPaul Moore 3898220deb96SPaul Moore static int selinux_sock_rcv_skb_iptables_compat(struct sock *sk, 3899220deb96SPaul Moore struct sk_buff *skb, 3900224dfbd8SPaul Moore struct avc_audit_data *ad, 3901220deb96SPaul Moore u16 family, 3902220deb96SPaul Moore char *addrp) 39031da177e4SLinus Torvalds { 3904220deb96SPaul Moore int err; 3905220deb96SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 3906220deb96SPaul Moore u16 sk_class; 3907220deb96SPaul Moore u32 netif_perm, node_perm, recv_perm; 3908220deb96SPaul Moore u32 port_sid, node_sid, if_sid, sk_sid; 39094237c75cSVenkat Yekkirala 3910220deb96SPaul Moore sk_sid = sksec->sid; 3911220deb96SPaul Moore sk_class = sksec->sclass; 39121da177e4SLinus Torvalds 3913220deb96SPaul Moore switch (sk_class) { 39141da177e4SLinus Torvalds case SECCLASS_UDP_SOCKET: 39151da177e4SLinus Torvalds netif_perm = NETIF__UDP_RECV; 39161da177e4SLinus Torvalds node_perm = NODE__UDP_RECV; 39171da177e4SLinus Torvalds recv_perm = UDP_SOCKET__RECV_MSG; 39181da177e4SLinus Torvalds break; 39191da177e4SLinus Torvalds case SECCLASS_TCP_SOCKET: 39201da177e4SLinus Torvalds netif_perm = NETIF__TCP_RECV; 39211da177e4SLinus Torvalds node_perm = NODE__TCP_RECV; 39221da177e4SLinus Torvalds recv_perm = TCP_SOCKET__RECV_MSG; 39231da177e4SLinus Torvalds break; 39242ee92d46SJames Morris case SECCLASS_DCCP_SOCKET: 39252ee92d46SJames Morris netif_perm = NETIF__DCCP_RECV; 39262ee92d46SJames Morris node_perm = NODE__DCCP_RECV; 39272ee92d46SJames Morris recv_perm = DCCP_SOCKET__RECV_MSG; 39282ee92d46SJames Morris break; 39291da177e4SLinus Torvalds default: 39301da177e4SLinus Torvalds netif_perm = NETIF__RAWIP_RECV; 39311da177e4SLinus Torvalds node_perm = NODE__RAWIP_RECV; 3932220deb96SPaul Moore recv_perm = 0; 39331da177e4SLinus Torvalds break; 39341da177e4SLinus Torvalds } 39351da177e4SLinus Torvalds 3936220deb96SPaul Moore err = sel_netif_sid(skb->iif, &if_sid); 39371da177e4SLinus Torvalds if (err) 3938220deb96SPaul Moore return err; 3939220deb96SPaul Moore err = avc_has_perm(sk_sid, if_sid, SECCLASS_NETIF, netif_perm, ad); 3940220deb96SPaul Moore if (err) 3941220deb96SPaul Moore return err; 39421da177e4SLinus Torvalds 3943224dfbd8SPaul Moore err = sel_netnode_sid(addrp, family, &node_sid); 39441da177e4SLinus Torvalds if (err) 3945220deb96SPaul Moore return err; 3946220deb96SPaul Moore err = avc_has_perm(sk_sid, node_sid, SECCLASS_NODE, node_perm, ad); 39471da177e4SLinus Torvalds if (err) 3948220deb96SPaul Moore return err; 39491da177e4SLinus Torvalds 3950220deb96SPaul Moore if (!recv_perm) 3951220deb96SPaul Moore return 0; 39521da177e4SLinus Torvalds err = security_port_sid(sk->sk_family, sk->sk_type, 39534e5ab4cbSJames Morris sk->sk_protocol, ntohs(ad->u.net.sport), 39541da177e4SLinus Torvalds &port_sid); 395571f1cb05SPaul Moore if (unlikely(err)) { 395671f1cb05SPaul Moore printk(KERN_WARNING 395771f1cb05SPaul Moore "SELinux: failure in" 395871f1cb05SPaul Moore " selinux_sock_rcv_skb_iptables_compat()," 395971f1cb05SPaul Moore " network port label not found\n"); 3960220deb96SPaul Moore return err; 396171f1cb05SPaul Moore } 3962220deb96SPaul Moore return avc_has_perm(sk_sid, port_sid, sk_class, recv_perm, ad); 39631da177e4SLinus Torvalds } 3964d28d1e08STrent Jaeger 3965220deb96SPaul Moore static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb, 3966220deb96SPaul Moore struct avc_audit_data *ad, 3967220deb96SPaul Moore u16 family, char *addrp) 3968220deb96SPaul Moore { 3969220deb96SPaul Moore int err; 3970220deb96SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 3971220deb96SPaul Moore u32 peer_sid; 3972220deb96SPaul Moore u32 sk_sid = sksec->sid; 3973220deb96SPaul Moore 3974220deb96SPaul Moore if (selinux_compat_net) 3975220deb96SPaul Moore err = selinux_sock_rcv_skb_iptables_compat(sk, skb, ad, 3976220deb96SPaul Moore family, addrp); 3977220deb96SPaul Moore else 3978220deb96SPaul Moore err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET, 3979220deb96SPaul Moore PACKET__RECV, ad); 3980220deb96SPaul Moore if (err) 3981220deb96SPaul Moore return err; 3982220deb96SPaul Moore 3983220deb96SPaul Moore if (selinux_policycap_netpeer) { 3984220deb96SPaul Moore err = selinux_skb_peerlbl_sid(skb, family, &peer_sid); 3985220deb96SPaul Moore if (err) 3986220deb96SPaul Moore return err; 3987220deb96SPaul Moore err = avc_has_perm(sk_sid, peer_sid, 3988220deb96SPaul Moore SECCLASS_PEER, PEER__RECV, ad); 3989220deb96SPaul Moore } else { 3990220deb96SPaul Moore err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, ad); 3991220deb96SPaul Moore if (err) 3992220deb96SPaul Moore return err; 3993220deb96SPaul Moore err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, ad); 3994220deb96SPaul Moore } 3995220deb96SPaul Moore 39964e5ab4cbSJames Morris return err; 39974e5ab4cbSJames Morris } 3998d28d1e08STrent Jaeger 39994e5ab4cbSJames Morris static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) 40004e5ab4cbSJames Morris { 4001220deb96SPaul Moore int err; 40024237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 4003220deb96SPaul Moore u16 family = sk->sk_family; 4004220deb96SPaul Moore u32 sk_sid = sksec->sid; 4005220deb96SPaul Moore struct avc_audit_data ad; 4006220deb96SPaul Moore char *addrp; 40074e5ab4cbSJames Morris 40084e5ab4cbSJames Morris if (family != PF_INET && family != PF_INET6) 4009220deb96SPaul Moore return 0; 40104e5ab4cbSJames Morris 40114e5ab4cbSJames Morris /* Handle mapped IPv4 packets arriving via IPv6 sockets */ 401287fcd70dSAl Viro if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 40134e5ab4cbSJames Morris family = PF_INET; 40144e5ab4cbSJames Morris 40154e5ab4cbSJames Morris AVC_AUDIT_DATA_INIT(&ad, NET); 4016da5645a2SPaul Moore ad.u.net.netif = skb->iif; 40174e5ab4cbSJames Morris ad.u.net.family = family; 4018224dfbd8SPaul Moore err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); 40194e5ab4cbSJames Morris if (err) 4020220deb96SPaul Moore return err; 40214e5ab4cbSJames Morris 4022220deb96SPaul Moore /* If any sort of compatibility mode is enabled then handoff processing 4023220deb96SPaul Moore * to the selinux_sock_rcv_skb_compat() function to deal with the 4024220deb96SPaul Moore * special handling. We do this in an attempt to keep this function 4025220deb96SPaul Moore * as fast and as clean as possible. */ 4026220deb96SPaul Moore if (selinux_compat_net || !selinux_policycap_netpeer) 4027220deb96SPaul Moore return selinux_sock_rcv_skb_compat(sk, skb, &ad, 4028220deb96SPaul Moore family, addrp); 4029220deb96SPaul Moore 4030d621d35eSPaul Moore if (netlbl_enabled() || selinux_xfrm_enabled()) { 4031d621d35eSPaul Moore u32 peer_sid; 4032220deb96SPaul Moore 4033220deb96SPaul Moore err = selinux_skb_peerlbl_sid(skb, family, &peer_sid); 4034220deb96SPaul Moore if (err) 4035220deb96SPaul Moore return err; 4036effad8dfSPaul Moore err = selinux_inet_sys_rcv_skb(skb->iif, addrp, family, 4037effad8dfSPaul Moore peer_sid, &ad); 4038effad8dfSPaul Moore if (err) 4039effad8dfSPaul Moore return err; 4040d621d35eSPaul Moore err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER, 4041d621d35eSPaul Moore PEER__RECV, &ad); 4042d621d35eSPaul Moore } 4043d621d35eSPaul Moore 4044effad8dfSPaul Moore if (selinux_secmark_enabled()) { 4045effad8dfSPaul Moore err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET, 4046effad8dfSPaul Moore PACKET__RECV, &ad); 4047effad8dfSPaul Moore if (err) 4048effad8dfSPaul Moore return err; 4049effad8dfSPaul Moore } 4050effad8dfSPaul Moore 4051d621d35eSPaul Moore return err; 40521da177e4SLinus Torvalds } 40531da177e4SLinus Torvalds 40542c7946a7SCatherine Zhang static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval, 40551da177e4SLinus Torvalds int __user *optlen, unsigned len) 40561da177e4SLinus Torvalds { 40571da177e4SLinus Torvalds int err = 0; 40581da177e4SLinus Torvalds char *scontext; 40591da177e4SLinus Torvalds u32 scontext_len; 40601da177e4SLinus Torvalds struct sk_security_struct *ssec; 40611da177e4SLinus Torvalds struct inode_security_struct *isec; 40623de4bab5SPaul Moore u32 peer_sid = SECSID_NULL; 40631da177e4SLinus Torvalds 40641da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 40652c7946a7SCatherine Zhang 40663de4bab5SPaul Moore if (isec->sclass == SECCLASS_UNIX_STREAM_SOCKET || 40673de4bab5SPaul Moore isec->sclass == SECCLASS_TCP_SOCKET) { 40686b877699SVenkat Yekkirala ssec = sock->sk->sk_security; 40696b877699SVenkat Yekkirala peer_sid = ssec->peer_sid; 40706b877699SVenkat Yekkirala } 40712c7946a7SCatherine Zhang if (peer_sid == SECSID_NULL) { 40722c7946a7SCatherine Zhang err = -ENOPROTOOPT; 40732c7946a7SCatherine Zhang goto out; 40742c7946a7SCatherine Zhang } 40751da177e4SLinus Torvalds 40762c7946a7SCatherine Zhang err = security_sid_to_context(peer_sid, &scontext, &scontext_len); 40771da177e4SLinus Torvalds 40781da177e4SLinus Torvalds if (err) 40791da177e4SLinus Torvalds goto out; 40801da177e4SLinus Torvalds 40811da177e4SLinus Torvalds if (scontext_len > len) { 40821da177e4SLinus Torvalds err = -ERANGE; 40831da177e4SLinus Torvalds goto out_len; 40841da177e4SLinus Torvalds } 40851da177e4SLinus Torvalds 40861da177e4SLinus Torvalds if (copy_to_user(optval, scontext, scontext_len)) 40871da177e4SLinus Torvalds err = -EFAULT; 40881da177e4SLinus Torvalds 40891da177e4SLinus Torvalds out_len: 40901da177e4SLinus Torvalds if (put_user(scontext_len, optlen)) 40911da177e4SLinus Torvalds err = -EFAULT; 40921da177e4SLinus Torvalds 40931da177e4SLinus Torvalds kfree(scontext); 40941da177e4SLinus Torvalds out: 40951da177e4SLinus Torvalds return err; 40961da177e4SLinus Torvalds } 40971da177e4SLinus Torvalds 4098dc49c1f9SCatherine Zhang static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid) 40992c7946a7SCatherine Zhang { 4100dc49c1f9SCatherine Zhang u32 peer_secid = SECSID_NULL; 410175e22910SPaul Moore u16 family; 4102877ce7c1SCatherine Zhang 410375e22910SPaul Moore if (sock) 410475e22910SPaul Moore family = sock->sk->sk_family; 410575e22910SPaul Moore else if (skb && skb->sk) 410675e22910SPaul Moore family = skb->sk->sk_family; 410775e22910SPaul Moore else 410875e22910SPaul Moore goto out; 410975e22910SPaul Moore 411075e22910SPaul Moore if (sock && family == PF_UNIX) 4111dc49c1f9SCatherine Zhang selinux_get_inode_sid(SOCK_INODE(sock), &peer_secid); 41123de4bab5SPaul Moore else if (skb) 4113220deb96SPaul Moore selinux_skb_peerlbl_sid(skb, family, &peer_secid); 41142c7946a7SCatherine Zhang 411575e22910SPaul Moore out: 4116dc49c1f9SCatherine Zhang *secid = peer_secid; 411775e22910SPaul Moore if (peer_secid == SECSID_NULL) 411875e22910SPaul Moore return -EINVAL; 411975e22910SPaul Moore return 0; 41202c7946a7SCatherine Zhang } 41212c7946a7SCatherine Zhang 41227d877f3bSAl Viro static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority) 41231da177e4SLinus Torvalds { 41241da177e4SLinus Torvalds return sk_alloc_security(sk, family, priority); 41251da177e4SLinus Torvalds } 41261da177e4SLinus Torvalds 41271da177e4SLinus Torvalds static void selinux_sk_free_security(struct sock *sk) 41281da177e4SLinus Torvalds { 41291da177e4SLinus Torvalds sk_free_security(sk); 41301da177e4SLinus Torvalds } 41311da177e4SLinus Torvalds 4132892c141eSVenkat Yekkirala static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk) 4133892c141eSVenkat Yekkirala { 4134892c141eSVenkat Yekkirala struct sk_security_struct *ssec = sk->sk_security; 4135892c141eSVenkat Yekkirala struct sk_security_struct *newssec = newsk->sk_security; 4136892c141eSVenkat Yekkirala 4137892c141eSVenkat Yekkirala newssec->sid = ssec->sid; 4138892c141eSVenkat Yekkirala newssec->peer_sid = ssec->peer_sid; 4139220deb96SPaul Moore newssec->sclass = ssec->sclass; 414099f59ed0SPaul Moore 41419f2ad665SPaul Moore selinux_netlbl_sk_security_clone(ssec, newssec); 4142892c141eSVenkat Yekkirala } 4143892c141eSVenkat Yekkirala 4144beb8d13bSVenkat Yekkirala static void selinux_sk_getsecid(struct sock *sk, u32 *secid) 4145d28d1e08STrent Jaeger { 4146d28d1e08STrent Jaeger if (!sk) 4147beb8d13bSVenkat Yekkirala *secid = SECINITSID_ANY_SOCKET; 4148892c141eSVenkat Yekkirala else { 4149892c141eSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 4150d28d1e08STrent Jaeger 4151beb8d13bSVenkat Yekkirala *secid = sksec->sid; 4152892c141eSVenkat Yekkirala } 4153d28d1e08STrent Jaeger } 4154d28d1e08STrent Jaeger 41559a673e56SAdrian Bunk static void selinux_sock_graft(struct sock* sk, struct socket *parent) 41564237c75cSVenkat Yekkirala { 41574237c75cSVenkat Yekkirala struct inode_security_struct *isec = SOCK_INODE(parent)->i_security; 41584237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 41594237c75cSVenkat Yekkirala 41602148ccc4SDavid Woodhouse if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 || 41612148ccc4SDavid Woodhouse sk->sk_family == PF_UNIX) 41624237c75cSVenkat Yekkirala isec->sid = sksec->sid; 4163220deb96SPaul Moore sksec->sclass = isec->sclass; 41647420ed23SVenkat Yekkirala 41657420ed23SVenkat Yekkirala selinux_netlbl_sock_graft(sk, parent); 41664237c75cSVenkat Yekkirala } 41674237c75cSVenkat Yekkirala 41689a673e56SAdrian Bunk static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb, 41694237c75cSVenkat Yekkirala struct request_sock *req) 41704237c75cSVenkat Yekkirala { 41714237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 41724237c75cSVenkat Yekkirala int err; 41737420ed23SVenkat Yekkirala u32 newsid; 41744237c75cSVenkat Yekkirala u32 peersid; 41754237c75cSVenkat Yekkirala 4176220deb96SPaul Moore err = selinux_skb_peerlbl_sid(skb, sk->sk_family, &peersid); 4177220deb96SPaul Moore if (err) 4178220deb96SPaul Moore return err; 4179a51c64f1SVenkat Yekkirala if (peersid == SECSID_NULL) { 4180a51c64f1SVenkat Yekkirala req->secid = sksec->sid; 41813de4bab5SPaul Moore req->peer_secid = SECSID_NULL; 4182a51c64f1SVenkat Yekkirala return 0; 4183a51c64f1SVenkat Yekkirala } 4184a51c64f1SVenkat Yekkirala 41854237c75cSVenkat Yekkirala err = security_sid_mls_copy(sksec->sid, peersid, &newsid); 41864237c75cSVenkat Yekkirala if (err) 41874237c75cSVenkat Yekkirala return err; 41884237c75cSVenkat Yekkirala 41894237c75cSVenkat Yekkirala req->secid = newsid; 41906b877699SVenkat Yekkirala req->peer_secid = peersid; 41914237c75cSVenkat Yekkirala return 0; 41924237c75cSVenkat Yekkirala } 41934237c75cSVenkat Yekkirala 41949a673e56SAdrian Bunk static void selinux_inet_csk_clone(struct sock *newsk, 41959a673e56SAdrian Bunk const struct request_sock *req) 41964237c75cSVenkat Yekkirala { 41974237c75cSVenkat Yekkirala struct sk_security_struct *newsksec = newsk->sk_security; 41984237c75cSVenkat Yekkirala 41994237c75cSVenkat Yekkirala newsksec->sid = req->secid; 42006b877699SVenkat Yekkirala newsksec->peer_sid = req->peer_secid; 42014237c75cSVenkat Yekkirala /* NOTE: Ideally, we should also get the isec->sid for the 42024237c75cSVenkat Yekkirala new socket in sync, but we don't have the isec available yet. 42034237c75cSVenkat Yekkirala So we will wait until sock_graft to do it, by which 42044237c75cSVenkat Yekkirala time it will have been created and available. */ 420599f59ed0SPaul Moore 42069f2ad665SPaul Moore /* We don't need to take any sort of lock here as we are the only 42079f2ad665SPaul Moore * thread with access to newsksec */ 42089f2ad665SPaul Moore selinux_netlbl_sk_security_reset(newsksec, req->rsk_ops->family); 42094237c75cSVenkat Yekkirala } 42104237c75cSVenkat Yekkirala 42116b877699SVenkat Yekkirala static void selinux_inet_conn_established(struct sock *sk, 42126b877699SVenkat Yekkirala struct sk_buff *skb) 42136b877699SVenkat Yekkirala { 42146b877699SVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 42156b877699SVenkat Yekkirala 4216220deb96SPaul Moore selinux_skb_peerlbl_sid(skb, sk->sk_family, &sksec->peer_sid); 42176b877699SVenkat Yekkirala } 42186b877699SVenkat Yekkirala 42199a673e56SAdrian Bunk static void selinux_req_classify_flow(const struct request_sock *req, 42209a673e56SAdrian Bunk struct flowi *fl) 42214237c75cSVenkat Yekkirala { 42224237c75cSVenkat Yekkirala fl->secid = req->secid; 42234237c75cSVenkat Yekkirala } 42244237c75cSVenkat Yekkirala 42251da177e4SLinus Torvalds static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb) 42261da177e4SLinus Torvalds { 42271da177e4SLinus Torvalds int err = 0; 42281da177e4SLinus Torvalds u32 perm; 42291da177e4SLinus Torvalds struct nlmsghdr *nlh; 42301da177e4SLinus Torvalds struct socket *sock = sk->sk_socket; 42311da177e4SLinus Torvalds struct inode_security_struct *isec = SOCK_INODE(sock)->i_security; 42321da177e4SLinus Torvalds 42331da177e4SLinus Torvalds if (skb->len < NLMSG_SPACE(0)) { 42341da177e4SLinus Torvalds err = -EINVAL; 42351da177e4SLinus Torvalds goto out; 42361da177e4SLinus Torvalds } 4237b529ccf2SArnaldo Carvalho de Melo nlh = nlmsg_hdr(skb); 42381da177e4SLinus Torvalds 42391da177e4SLinus Torvalds err = selinux_nlmsg_lookup(isec->sclass, nlh->nlmsg_type, &perm); 42401da177e4SLinus Torvalds if (err) { 42411da177e4SLinus Torvalds if (err == -EINVAL) { 42429ad9ad38SDavid Woodhouse audit_log(current->audit_context, GFP_KERNEL, AUDIT_SELINUX_ERR, 42431da177e4SLinus Torvalds "SELinux: unrecognized netlink message" 42441da177e4SLinus Torvalds " type=%hu for sclass=%hu\n", 42451da177e4SLinus Torvalds nlh->nlmsg_type, isec->sclass); 42461da177e4SLinus Torvalds if (!selinux_enforcing) 42471da177e4SLinus Torvalds err = 0; 42481da177e4SLinus Torvalds } 42491da177e4SLinus Torvalds 42501da177e4SLinus Torvalds /* Ignore */ 42511da177e4SLinus Torvalds if (err == -ENOENT) 42521da177e4SLinus Torvalds err = 0; 42531da177e4SLinus Torvalds goto out; 42541da177e4SLinus Torvalds } 42551da177e4SLinus Torvalds 42561da177e4SLinus Torvalds err = socket_has_perm(current, sock, perm); 42571da177e4SLinus Torvalds out: 42581da177e4SLinus Torvalds return err; 42591da177e4SLinus Torvalds } 42601da177e4SLinus Torvalds 42611da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER 42621da177e4SLinus Torvalds 4263effad8dfSPaul Moore static unsigned int selinux_ip_forward(struct sk_buff *skb, int ifindex, 4264effad8dfSPaul Moore u16 family) 42651da177e4SLinus Torvalds { 4266effad8dfSPaul Moore char *addrp; 4267effad8dfSPaul Moore u32 peer_sid; 4268effad8dfSPaul Moore struct avc_audit_data ad; 4269effad8dfSPaul Moore u8 secmark_active; 4270effad8dfSPaul Moore u8 peerlbl_active; 42714237c75cSVenkat Yekkirala 4272effad8dfSPaul Moore if (!selinux_policycap_netpeer) 4273effad8dfSPaul Moore return NF_ACCEPT; 42744237c75cSVenkat Yekkirala 4275effad8dfSPaul Moore secmark_active = selinux_secmark_enabled(); 4276effad8dfSPaul Moore peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled(); 4277effad8dfSPaul Moore if (!secmark_active && !peerlbl_active) 4278effad8dfSPaul Moore return NF_ACCEPT; 42794237c75cSVenkat Yekkirala 4280effad8dfSPaul Moore AVC_AUDIT_DATA_INIT(&ad, NET); 4281effad8dfSPaul Moore ad.u.net.netif = ifindex; 4282effad8dfSPaul Moore ad.u.net.family = family; 4283effad8dfSPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0) 4284effad8dfSPaul Moore return NF_DROP; 42851da177e4SLinus Torvalds 4286effad8dfSPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0) 4287effad8dfSPaul Moore return NF_DROP; 42881da177e4SLinus Torvalds 4289effad8dfSPaul Moore if (peerlbl_active) 4290effad8dfSPaul Moore if (selinux_inet_sys_rcv_skb(ifindex, addrp, family, 4291effad8dfSPaul Moore peer_sid, &ad) != 0) 4292effad8dfSPaul Moore return NF_DROP; 4293effad8dfSPaul Moore 4294effad8dfSPaul Moore if (secmark_active) 4295effad8dfSPaul Moore if (avc_has_perm(peer_sid, skb->secmark, 4296effad8dfSPaul Moore SECCLASS_PACKET, PACKET__FORWARD_IN, &ad)) 4297effad8dfSPaul Moore return NF_DROP; 4298effad8dfSPaul Moore 4299effad8dfSPaul Moore return NF_ACCEPT; 4300effad8dfSPaul Moore } 4301effad8dfSPaul Moore 4302effad8dfSPaul Moore static unsigned int selinux_ipv4_forward(unsigned int hooknum, 4303effad8dfSPaul Moore struct sk_buff *skb, 4304effad8dfSPaul Moore const struct net_device *in, 4305effad8dfSPaul Moore const struct net_device *out, 4306effad8dfSPaul Moore int (*okfn)(struct sk_buff *)) 4307effad8dfSPaul Moore { 4308effad8dfSPaul Moore return selinux_ip_forward(skb, in->ifindex, PF_INET); 4309effad8dfSPaul Moore } 4310effad8dfSPaul Moore 4311effad8dfSPaul Moore #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 4312effad8dfSPaul Moore static unsigned int selinux_ipv6_forward(unsigned int hooknum, 4313effad8dfSPaul Moore struct sk_buff *skb, 4314effad8dfSPaul Moore const struct net_device *in, 4315effad8dfSPaul Moore const struct net_device *out, 4316effad8dfSPaul Moore int (*okfn)(struct sk_buff *)) 4317effad8dfSPaul Moore { 4318effad8dfSPaul Moore return selinux_ip_forward(skb, in->ifindex, PF_INET6); 4319effad8dfSPaul Moore } 4320effad8dfSPaul Moore #endif /* IPV6 */ 4321effad8dfSPaul Moore 4322effad8dfSPaul Moore static int selinux_ip_postroute_iptables_compat(struct sock *sk, 4323effad8dfSPaul Moore int ifindex, 4324effad8dfSPaul Moore struct avc_audit_data *ad, 4325effad8dfSPaul Moore u16 family, char *addrp) 4326effad8dfSPaul Moore { 4327effad8dfSPaul Moore int err; 4328effad8dfSPaul Moore struct sk_security_struct *sksec = sk->sk_security; 4329effad8dfSPaul Moore u16 sk_class; 4330effad8dfSPaul Moore u32 netif_perm, node_perm, send_perm; 4331effad8dfSPaul Moore u32 port_sid, node_sid, if_sid, sk_sid; 4332effad8dfSPaul Moore 4333effad8dfSPaul Moore sk_sid = sksec->sid; 4334effad8dfSPaul Moore sk_class = sksec->sclass; 4335effad8dfSPaul Moore 4336effad8dfSPaul Moore switch (sk_class) { 43371da177e4SLinus Torvalds case SECCLASS_UDP_SOCKET: 43381da177e4SLinus Torvalds netif_perm = NETIF__UDP_SEND; 43391da177e4SLinus Torvalds node_perm = NODE__UDP_SEND; 43401da177e4SLinus Torvalds send_perm = UDP_SOCKET__SEND_MSG; 43411da177e4SLinus Torvalds break; 43421da177e4SLinus Torvalds case SECCLASS_TCP_SOCKET: 43431da177e4SLinus Torvalds netif_perm = NETIF__TCP_SEND; 43441da177e4SLinus Torvalds node_perm = NODE__TCP_SEND; 43451da177e4SLinus Torvalds send_perm = TCP_SOCKET__SEND_MSG; 43461da177e4SLinus Torvalds break; 43472ee92d46SJames Morris case SECCLASS_DCCP_SOCKET: 43482ee92d46SJames Morris netif_perm = NETIF__DCCP_SEND; 43492ee92d46SJames Morris node_perm = NODE__DCCP_SEND; 43502ee92d46SJames Morris send_perm = DCCP_SOCKET__SEND_MSG; 43512ee92d46SJames Morris break; 43521da177e4SLinus Torvalds default: 43531da177e4SLinus Torvalds netif_perm = NETIF__RAWIP_SEND; 43541da177e4SLinus Torvalds node_perm = NODE__RAWIP_SEND; 4355effad8dfSPaul Moore send_perm = 0; 43561da177e4SLinus Torvalds break; 43571da177e4SLinus Torvalds } 43581da177e4SLinus Torvalds 4359effad8dfSPaul Moore err = sel_netif_sid(ifindex, &if_sid); 43604e5ab4cbSJames Morris if (err) 4361effad8dfSPaul Moore return err; 4362effad8dfSPaul Moore err = avc_has_perm(sk_sid, if_sid, SECCLASS_NETIF, netif_perm, ad); 4363effad8dfSPaul Moore return err; 43641da177e4SLinus Torvalds 4365224dfbd8SPaul Moore err = sel_netnode_sid(addrp, family, &node_sid); 43664e5ab4cbSJames Morris if (err) 4367effad8dfSPaul Moore return err; 4368effad8dfSPaul Moore err = avc_has_perm(sk_sid, node_sid, SECCLASS_NODE, node_perm, ad); 43694e5ab4cbSJames Morris if (err) 4370effad8dfSPaul Moore return err; 43711da177e4SLinus Torvalds 4372effad8dfSPaul Moore if (send_perm != 0) 4373effad8dfSPaul Moore return 0; 43741da177e4SLinus Torvalds 4375effad8dfSPaul Moore err = security_port_sid(sk->sk_family, sk->sk_type, 4376effad8dfSPaul Moore sk->sk_protocol, ntohs(ad->u.net.dport), 43774e5ab4cbSJames Morris &port_sid); 437871f1cb05SPaul Moore if (unlikely(err)) { 437971f1cb05SPaul Moore printk(KERN_WARNING 438071f1cb05SPaul Moore "SELinux: failure in" 438171f1cb05SPaul Moore " selinux_ip_postroute_iptables_compat()," 438271f1cb05SPaul Moore " network port label not found\n"); 43834e5ab4cbSJames Morris return err; 438471f1cb05SPaul Moore } 4385effad8dfSPaul Moore return avc_has_perm(sk_sid, port_sid, sk_class, send_perm, ad); 43861da177e4SLinus Torvalds } 43871da177e4SLinus Torvalds 4388effad8dfSPaul Moore static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb, 4389effad8dfSPaul Moore int ifindex, 4390effad8dfSPaul Moore struct avc_audit_data *ad, 4391effad8dfSPaul Moore u16 family, 4392effad8dfSPaul Moore char *addrp, 4393effad8dfSPaul Moore u8 proto) 43944e5ab4cbSJames Morris { 4395effad8dfSPaul Moore struct sock *sk = skb->sk; 43964237c75cSVenkat Yekkirala struct sk_security_struct *sksec; 43974e5ab4cbSJames Morris 4398effad8dfSPaul Moore if (sk == NULL) 4399effad8dfSPaul Moore return NF_ACCEPT; 44004237c75cSVenkat Yekkirala sksec = sk->sk_security; 44014e5ab4cbSJames Morris 4402effad8dfSPaul Moore if (selinux_compat_net) { 4403effad8dfSPaul Moore if (selinux_ip_postroute_iptables_compat(skb->sk, ifindex, 4404effad8dfSPaul Moore ad, family, addrp)) 4405effad8dfSPaul Moore return NF_DROP; 4406effad8dfSPaul Moore } else { 4407effad8dfSPaul Moore if (avc_has_perm(sksec->sid, skb->secmark, 4408effad8dfSPaul Moore SECCLASS_PACKET, PACKET__SEND, ad)) 4409effad8dfSPaul Moore return NF_DROP; 44101da177e4SLinus Torvalds } 44111da177e4SLinus Torvalds 4412effad8dfSPaul Moore if (selinux_policycap_netpeer) 4413effad8dfSPaul Moore if (selinux_xfrm_postroute_last(sksec->sid, skb, ad, proto)) 4414effad8dfSPaul Moore return NF_DROP; 4415effad8dfSPaul Moore 4416effad8dfSPaul Moore return NF_ACCEPT; 4417effad8dfSPaul Moore } 4418effad8dfSPaul Moore 4419effad8dfSPaul Moore static unsigned int selinux_ip_postroute(struct sk_buff *skb, int ifindex, 4420effad8dfSPaul Moore u16 family) 4421effad8dfSPaul Moore { 4422effad8dfSPaul Moore u32 secmark_perm; 4423effad8dfSPaul Moore u32 peer_sid; 4424effad8dfSPaul Moore struct sock *sk; 4425effad8dfSPaul Moore struct avc_audit_data ad; 4426effad8dfSPaul Moore char *addrp; 4427effad8dfSPaul Moore u8 proto; 4428effad8dfSPaul Moore u8 secmark_active; 4429effad8dfSPaul Moore u8 peerlbl_active; 4430effad8dfSPaul Moore 4431effad8dfSPaul Moore AVC_AUDIT_DATA_INIT(&ad, NET); 4432effad8dfSPaul Moore ad.u.net.netif = ifindex; 4433effad8dfSPaul Moore ad.u.net.family = family; 4434effad8dfSPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto)) 4435effad8dfSPaul Moore return NF_DROP; 4436effad8dfSPaul Moore 4437effad8dfSPaul Moore /* If any sort of compatibility mode is enabled then handoff processing 4438effad8dfSPaul Moore * to the selinux_ip_postroute_compat() function to deal with the 4439effad8dfSPaul Moore * special handling. We do this in an attempt to keep this function 4440effad8dfSPaul Moore * as fast and as clean as possible. */ 4441effad8dfSPaul Moore if (selinux_compat_net || !selinux_policycap_netpeer) 4442effad8dfSPaul Moore return selinux_ip_postroute_compat(skb, ifindex, &ad, 4443effad8dfSPaul Moore family, addrp, proto); 4444effad8dfSPaul Moore 4445effad8dfSPaul Moore /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec 4446effad8dfSPaul Moore * packet transformation so allow the packet to pass without any checks 4447effad8dfSPaul Moore * since we'll have another chance to perform access control checks 4448effad8dfSPaul Moore * when the packet is on it's final way out. 4449effad8dfSPaul Moore * NOTE: there appear to be some IPv6 multicast cases where skb->dst 4450effad8dfSPaul Moore * is NULL, in this case go ahead and apply access control. */ 4451effad8dfSPaul Moore if (skb->dst != NULL && skb->dst->xfrm != NULL) 4452effad8dfSPaul Moore return NF_ACCEPT; 4453effad8dfSPaul Moore 4454effad8dfSPaul Moore secmark_active = selinux_secmark_enabled(); 4455effad8dfSPaul Moore peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled(); 4456effad8dfSPaul Moore if (!secmark_active && !peerlbl_active) 4457effad8dfSPaul Moore return NF_ACCEPT; 4458effad8dfSPaul Moore 4459effad8dfSPaul Moore /* if the packet is locally generated (skb->sk != NULL) then use the 4460effad8dfSPaul Moore * socket's label as the peer label, otherwise the packet is being 4461effad8dfSPaul Moore * forwarded through this system and we need to fetch the peer label 4462effad8dfSPaul Moore * directly from the packet */ 4463effad8dfSPaul Moore sk = skb->sk; 4464effad8dfSPaul Moore if (sk) { 4465effad8dfSPaul Moore struct sk_security_struct *sksec = sk->sk_security; 4466effad8dfSPaul Moore peer_sid = sksec->sid; 4467effad8dfSPaul Moore secmark_perm = PACKET__SEND; 4468effad8dfSPaul Moore } else { 4469effad8dfSPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &peer_sid)) 4470effad8dfSPaul Moore return NF_DROP; 4471effad8dfSPaul Moore secmark_perm = PACKET__FORWARD_OUT; 4472effad8dfSPaul Moore } 4473effad8dfSPaul Moore 4474effad8dfSPaul Moore if (secmark_active) 4475effad8dfSPaul Moore if (avc_has_perm(peer_sid, skb->secmark, 4476effad8dfSPaul Moore SECCLASS_PACKET, secmark_perm, &ad)) 4477effad8dfSPaul Moore return NF_DROP; 4478effad8dfSPaul Moore 4479effad8dfSPaul Moore if (peerlbl_active) { 4480effad8dfSPaul Moore u32 if_sid; 4481effad8dfSPaul Moore u32 node_sid; 4482effad8dfSPaul Moore 4483effad8dfSPaul Moore if (sel_netif_sid(ifindex, &if_sid)) 4484effad8dfSPaul Moore return NF_DROP; 4485effad8dfSPaul Moore if (avc_has_perm(peer_sid, if_sid, 4486effad8dfSPaul Moore SECCLASS_NETIF, NETIF__EGRESS, &ad)) 4487effad8dfSPaul Moore return NF_DROP; 4488effad8dfSPaul Moore 4489effad8dfSPaul Moore if (sel_netnode_sid(addrp, family, &node_sid)) 4490effad8dfSPaul Moore return NF_DROP; 4491effad8dfSPaul Moore if (avc_has_perm(peer_sid, node_sid, 4492effad8dfSPaul Moore SECCLASS_NODE, NODE__SENDTO, &ad)) 4493effad8dfSPaul Moore return NF_DROP; 4494effad8dfSPaul Moore } 4495effad8dfSPaul Moore 4496effad8dfSPaul Moore return NF_ACCEPT; 4497effad8dfSPaul Moore } 4498effad8dfSPaul Moore 4499effad8dfSPaul Moore static unsigned int selinux_ipv4_postroute(unsigned int hooknum, 4500a224be76SDavid S. Miller struct sk_buff *skb, 45011da177e4SLinus Torvalds const struct net_device *in, 45021da177e4SLinus Torvalds const struct net_device *out, 45031da177e4SLinus Torvalds int (*okfn)(struct sk_buff *)) 45041da177e4SLinus Torvalds { 4505effad8dfSPaul Moore return selinux_ip_postroute(skb, out->ifindex, PF_INET); 45061da177e4SLinus Torvalds } 45071da177e4SLinus Torvalds 45081da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 4509effad8dfSPaul Moore static unsigned int selinux_ipv6_postroute(unsigned int hooknum, 4510a224be76SDavid S. Miller struct sk_buff *skb, 45111da177e4SLinus Torvalds const struct net_device *in, 45121da177e4SLinus Torvalds const struct net_device *out, 45131da177e4SLinus Torvalds int (*okfn)(struct sk_buff *)) 45141da177e4SLinus Torvalds { 4515effad8dfSPaul Moore return selinux_ip_postroute(skb, out->ifindex, PF_INET6); 45161da177e4SLinus Torvalds } 45171da177e4SLinus Torvalds #endif /* IPV6 */ 45181da177e4SLinus Torvalds 45191da177e4SLinus Torvalds #endif /* CONFIG_NETFILTER */ 45201da177e4SLinus Torvalds 45211da177e4SLinus Torvalds static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb) 45221da177e4SLinus Torvalds { 45231da177e4SLinus Torvalds int err; 45241da177e4SLinus Torvalds 45251da177e4SLinus Torvalds err = secondary_ops->netlink_send(sk, skb); 45261da177e4SLinus Torvalds if (err) 45271da177e4SLinus Torvalds return err; 45281da177e4SLinus Torvalds 45291da177e4SLinus Torvalds if (policydb_loaded_version >= POLICYDB_VERSION_NLCLASS) 45301da177e4SLinus Torvalds err = selinux_nlmsg_perm(sk, skb); 45311da177e4SLinus Torvalds 45321da177e4SLinus Torvalds return err; 45331da177e4SLinus Torvalds } 45341da177e4SLinus Torvalds 4535c7bdb545SDarrel Goeddel static int selinux_netlink_recv(struct sk_buff *skb, int capability) 45361da177e4SLinus Torvalds { 4537c7bdb545SDarrel Goeddel int err; 4538c7bdb545SDarrel Goeddel struct avc_audit_data ad; 4539c7bdb545SDarrel Goeddel 4540c7bdb545SDarrel Goeddel err = secondary_ops->netlink_recv(skb, capability); 4541c7bdb545SDarrel Goeddel if (err) 4542c7bdb545SDarrel Goeddel return err; 4543c7bdb545SDarrel Goeddel 4544c7bdb545SDarrel Goeddel AVC_AUDIT_DATA_INIT(&ad, CAP); 4545c7bdb545SDarrel Goeddel ad.u.cap = capability; 4546c7bdb545SDarrel Goeddel 4547c7bdb545SDarrel Goeddel return avc_has_perm(NETLINK_CB(skb).sid, NETLINK_CB(skb).sid, 4548c7bdb545SDarrel Goeddel SECCLASS_CAPABILITY, CAP_TO_MASK(capability), &ad); 45491da177e4SLinus Torvalds } 45501da177e4SLinus Torvalds 45511da177e4SLinus Torvalds static int ipc_alloc_security(struct task_struct *task, 45521da177e4SLinus Torvalds struct kern_ipc_perm *perm, 45531da177e4SLinus Torvalds u16 sclass) 45541da177e4SLinus Torvalds { 45551da177e4SLinus Torvalds struct task_security_struct *tsec = task->security; 45561da177e4SLinus Torvalds struct ipc_security_struct *isec; 45571da177e4SLinus Torvalds 455889d155efSJames Morris isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL); 45591da177e4SLinus Torvalds if (!isec) 45601da177e4SLinus Torvalds return -ENOMEM; 45611da177e4SLinus Torvalds 45621da177e4SLinus Torvalds isec->sclass = sclass; 45631da177e4SLinus Torvalds isec->ipc_perm = perm; 45641da177e4SLinus Torvalds isec->sid = tsec->sid; 45651da177e4SLinus Torvalds perm->security = isec; 45661da177e4SLinus Torvalds 45671da177e4SLinus Torvalds return 0; 45681da177e4SLinus Torvalds } 45691da177e4SLinus Torvalds 45701da177e4SLinus Torvalds static void ipc_free_security(struct kern_ipc_perm *perm) 45711da177e4SLinus Torvalds { 45721da177e4SLinus Torvalds struct ipc_security_struct *isec = perm->security; 45731da177e4SLinus Torvalds perm->security = NULL; 45741da177e4SLinus Torvalds kfree(isec); 45751da177e4SLinus Torvalds } 45761da177e4SLinus Torvalds 45771da177e4SLinus Torvalds static int msg_msg_alloc_security(struct msg_msg *msg) 45781da177e4SLinus Torvalds { 45791da177e4SLinus Torvalds struct msg_security_struct *msec; 45801da177e4SLinus Torvalds 458189d155efSJames Morris msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL); 45821da177e4SLinus Torvalds if (!msec) 45831da177e4SLinus Torvalds return -ENOMEM; 45841da177e4SLinus Torvalds 45851da177e4SLinus Torvalds msec->msg = msg; 45861da177e4SLinus Torvalds msec->sid = SECINITSID_UNLABELED; 45871da177e4SLinus Torvalds msg->security = msec; 45881da177e4SLinus Torvalds 45891da177e4SLinus Torvalds return 0; 45901da177e4SLinus Torvalds } 45911da177e4SLinus Torvalds 45921da177e4SLinus Torvalds static void msg_msg_free_security(struct msg_msg *msg) 45931da177e4SLinus Torvalds { 45941da177e4SLinus Torvalds struct msg_security_struct *msec = msg->security; 45951da177e4SLinus Torvalds 45961da177e4SLinus Torvalds msg->security = NULL; 45971da177e4SLinus Torvalds kfree(msec); 45981da177e4SLinus Torvalds } 45991da177e4SLinus Torvalds 46001da177e4SLinus Torvalds static int ipc_has_perm(struct kern_ipc_perm *ipc_perms, 46016af963f1SStephen Smalley u32 perms) 46021da177e4SLinus Torvalds { 46031da177e4SLinus Torvalds struct task_security_struct *tsec; 46041da177e4SLinus Torvalds struct ipc_security_struct *isec; 46051da177e4SLinus Torvalds struct avc_audit_data ad; 46061da177e4SLinus Torvalds 46071da177e4SLinus Torvalds tsec = current->security; 46081da177e4SLinus Torvalds isec = ipc_perms->security; 46091da177e4SLinus Torvalds 46101da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 46111da177e4SLinus Torvalds ad.u.ipc_id = ipc_perms->key; 46121da177e4SLinus Torvalds 46136af963f1SStephen Smalley return avc_has_perm(tsec->sid, isec->sid, isec->sclass, perms, &ad); 46141da177e4SLinus Torvalds } 46151da177e4SLinus Torvalds 46161da177e4SLinus Torvalds static int selinux_msg_msg_alloc_security(struct msg_msg *msg) 46171da177e4SLinus Torvalds { 46181da177e4SLinus Torvalds return msg_msg_alloc_security(msg); 46191da177e4SLinus Torvalds } 46201da177e4SLinus Torvalds 46211da177e4SLinus Torvalds static void selinux_msg_msg_free_security(struct msg_msg *msg) 46221da177e4SLinus Torvalds { 46231da177e4SLinus Torvalds msg_msg_free_security(msg); 46241da177e4SLinus Torvalds } 46251da177e4SLinus Torvalds 46261da177e4SLinus Torvalds /* message queue security operations */ 46271da177e4SLinus Torvalds static int selinux_msg_queue_alloc_security(struct msg_queue *msq) 46281da177e4SLinus Torvalds { 46291da177e4SLinus Torvalds struct task_security_struct *tsec; 46301da177e4SLinus Torvalds struct ipc_security_struct *isec; 46311da177e4SLinus Torvalds struct avc_audit_data ad; 46321da177e4SLinus Torvalds int rc; 46331da177e4SLinus Torvalds 46341da177e4SLinus Torvalds rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ); 46351da177e4SLinus Torvalds if (rc) 46361da177e4SLinus Torvalds return rc; 46371da177e4SLinus Torvalds 46381da177e4SLinus Torvalds tsec = current->security; 46391da177e4SLinus Torvalds isec = msq->q_perm.security; 46401da177e4SLinus Torvalds 46411da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 46421da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 46431da177e4SLinus Torvalds 46441da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_MSGQ, 46451da177e4SLinus Torvalds MSGQ__CREATE, &ad); 46461da177e4SLinus Torvalds if (rc) { 46471da177e4SLinus Torvalds ipc_free_security(&msq->q_perm); 46481da177e4SLinus Torvalds return rc; 46491da177e4SLinus Torvalds } 46501da177e4SLinus Torvalds return 0; 46511da177e4SLinus Torvalds } 46521da177e4SLinus Torvalds 46531da177e4SLinus Torvalds static void selinux_msg_queue_free_security(struct msg_queue *msq) 46541da177e4SLinus Torvalds { 46551da177e4SLinus Torvalds ipc_free_security(&msq->q_perm); 46561da177e4SLinus Torvalds } 46571da177e4SLinus Torvalds 46581da177e4SLinus Torvalds static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg) 46591da177e4SLinus Torvalds { 46601da177e4SLinus Torvalds struct task_security_struct *tsec; 46611da177e4SLinus Torvalds struct ipc_security_struct *isec; 46621da177e4SLinus Torvalds struct avc_audit_data ad; 46631da177e4SLinus Torvalds 46641da177e4SLinus Torvalds tsec = current->security; 46651da177e4SLinus Torvalds isec = msq->q_perm.security; 46661da177e4SLinus Torvalds 46671da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 46681da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 46691da177e4SLinus Torvalds 46701da177e4SLinus Torvalds return avc_has_perm(tsec->sid, isec->sid, SECCLASS_MSGQ, 46711da177e4SLinus Torvalds MSGQ__ASSOCIATE, &ad); 46721da177e4SLinus Torvalds } 46731da177e4SLinus Torvalds 46741da177e4SLinus Torvalds static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd) 46751da177e4SLinus Torvalds { 46761da177e4SLinus Torvalds int err; 46771da177e4SLinus Torvalds int perms; 46781da177e4SLinus Torvalds 46791da177e4SLinus Torvalds switch(cmd) { 46801da177e4SLinus Torvalds case IPC_INFO: 46811da177e4SLinus Torvalds case MSG_INFO: 46821da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 46831da177e4SLinus Torvalds return task_has_system(current, SYSTEM__IPC_INFO); 46841da177e4SLinus Torvalds case IPC_STAT: 46851da177e4SLinus Torvalds case MSG_STAT: 46861da177e4SLinus Torvalds perms = MSGQ__GETATTR | MSGQ__ASSOCIATE; 46871da177e4SLinus Torvalds break; 46881da177e4SLinus Torvalds case IPC_SET: 46891da177e4SLinus Torvalds perms = MSGQ__SETATTR; 46901da177e4SLinus Torvalds break; 46911da177e4SLinus Torvalds case IPC_RMID: 46921da177e4SLinus Torvalds perms = MSGQ__DESTROY; 46931da177e4SLinus Torvalds break; 46941da177e4SLinus Torvalds default: 46951da177e4SLinus Torvalds return 0; 46961da177e4SLinus Torvalds } 46971da177e4SLinus Torvalds 46986af963f1SStephen Smalley err = ipc_has_perm(&msq->q_perm, perms); 46991da177e4SLinus Torvalds return err; 47001da177e4SLinus Torvalds } 47011da177e4SLinus Torvalds 47021da177e4SLinus Torvalds static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg) 47031da177e4SLinus Torvalds { 47041da177e4SLinus Torvalds struct task_security_struct *tsec; 47051da177e4SLinus Torvalds struct ipc_security_struct *isec; 47061da177e4SLinus Torvalds struct msg_security_struct *msec; 47071da177e4SLinus Torvalds struct avc_audit_data ad; 47081da177e4SLinus Torvalds int rc; 47091da177e4SLinus Torvalds 47101da177e4SLinus Torvalds tsec = current->security; 47111da177e4SLinus Torvalds isec = msq->q_perm.security; 47121da177e4SLinus Torvalds msec = msg->security; 47131da177e4SLinus Torvalds 47141da177e4SLinus Torvalds /* 47151da177e4SLinus Torvalds * First time through, need to assign label to the message 47161da177e4SLinus Torvalds */ 47171da177e4SLinus Torvalds if (msec->sid == SECINITSID_UNLABELED) { 47181da177e4SLinus Torvalds /* 47191da177e4SLinus Torvalds * Compute new sid based on current process and 47201da177e4SLinus Torvalds * message queue this message will be stored in 47211da177e4SLinus Torvalds */ 47221da177e4SLinus Torvalds rc = security_transition_sid(tsec->sid, 47231da177e4SLinus Torvalds isec->sid, 47241da177e4SLinus Torvalds SECCLASS_MSG, 47251da177e4SLinus Torvalds &msec->sid); 47261da177e4SLinus Torvalds if (rc) 47271da177e4SLinus Torvalds return rc; 47281da177e4SLinus Torvalds } 47291da177e4SLinus Torvalds 47301da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 47311da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 47321da177e4SLinus Torvalds 47331da177e4SLinus Torvalds /* Can this process write to the queue? */ 47341da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_MSGQ, 47351da177e4SLinus Torvalds MSGQ__WRITE, &ad); 47361da177e4SLinus Torvalds if (!rc) 47371da177e4SLinus Torvalds /* Can this process send the message */ 47381da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, msec->sid, 47391da177e4SLinus Torvalds SECCLASS_MSG, MSG__SEND, &ad); 47401da177e4SLinus Torvalds if (!rc) 47411da177e4SLinus Torvalds /* Can the message be put in the queue? */ 47421da177e4SLinus Torvalds rc = avc_has_perm(msec->sid, isec->sid, 47431da177e4SLinus Torvalds SECCLASS_MSGQ, MSGQ__ENQUEUE, &ad); 47441da177e4SLinus Torvalds 47451da177e4SLinus Torvalds return rc; 47461da177e4SLinus Torvalds } 47471da177e4SLinus Torvalds 47481da177e4SLinus Torvalds static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg, 47491da177e4SLinus Torvalds struct task_struct *target, 47501da177e4SLinus Torvalds long type, int mode) 47511da177e4SLinus Torvalds { 47521da177e4SLinus Torvalds struct task_security_struct *tsec; 47531da177e4SLinus Torvalds struct ipc_security_struct *isec; 47541da177e4SLinus Torvalds struct msg_security_struct *msec; 47551da177e4SLinus Torvalds struct avc_audit_data ad; 47561da177e4SLinus Torvalds int rc; 47571da177e4SLinus Torvalds 47581da177e4SLinus Torvalds tsec = target->security; 47591da177e4SLinus Torvalds isec = msq->q_perm.security; 47601da177e4SLinus Torvalds msec = msg->security; 47611da177e4SLinus Torvalds 47621da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 47631da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 47641da177e4SLinus Torvalds 47651da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, 47661da177e4SLinus Torvalds SECCLASS_MSGQ, MSGQ__READ, &ad); 47671da177e4SLinus Torvalds if (!rc) 47681da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, msec->sid, 47691da177e4SLinus Torvalds SECCLASS_MSG, MSG__RECEIVE, &ad); 47701da177e4SLinus Torvalds return rc; 47711da177e4SLinus Torvalds } 47721da177e4SLinus Torvalds 47731da177e4SLinus Torvalds /* Shared Memory security operations */ 47741da177e4SLinus Torvalds static int selinux_shm_alloc_security(struct shmid_kernel *shp) 47751da177e4SLinus Torvalds { 47761da177e4SLinus Torvalds struct task_security_struct *tsec; 47771da177e4SLinus Torvalds struct ipc_security_struct *isec; 47781da177e4SLinus Torvalds struct avc_audit_data ad; 47791da177e4SLinus Torvalds int rc; 47801da177e4SLinus Torvalds 47811da177e4SLinus Torvalds rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM); 47821da177e4SLinus Torvalds if (rc) 47831da177e4SLinus Torvalds return rc; 47841da177e4SLinus Torvalds 47851da177e4SLinus Torvalds tsec = current->security; 47861da177e4SLinus Torvalds isec = shp->shm_perm.security; 47871da177e4SLinus Torvalds 47881da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 47891da177e4SLinus Torvalds ad.u.ipc_id = shp->shm_perm.key; 47901da177e4SLinus Torvalds 47911da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_SHM, 47921da177e4SLinus Torvalds SHM__CREATE, &ad); 47931da177e4SLinus Torvalds if (rc) { 47941da177e4SLinus Torvalds ipc_free_security(&shp->shm_perm); 47951da177e4SLinus Torvalds return rc; 47961da177e4SLinus Torvalds } 47971da177e4SLinus Torvalds return 0; 47981da177e4SLinus Torvalds } 47991da177e4SLinus Torvalds 48001da177e4SLinus Torvalds static void selinux_shm_free_security(struct shmid_kernel *shp) 48011da177e4SLinus Torvalds { 48021da177e4SLinus Torvalds ipc_free_security(&shp->shm_perm); 48031da177e4SLinus Torvalds } 48041da177e4SLinus Torvalds 48051da177e4SLinus Torvalds static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg) 48061da177e4SLinus Torvalds { 48071da177e4SLinus Torvalds struct task_security_struct *tsec; 48081da177e4SLinus Torvalds struct ipc_security_struct *isec; 48091da177e4SLinus Torvalds struct avc_audit_data ad; 48101da177e4SLinus Torvalds 48111da177e4SLinus Torvalds tsec = current->security; 48121da177e4SLinus Torvalds isec = shp->shm_perm.security; 48131da177e4SLinus Torvalds 48141da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 48151da177e4SLinus Torvalds ad.u.ipc_id = shp->shm_perm.key; 48161da177e4SLinus Torvalds 48171da177e4SLinus Torvalds return avc_has_perm(tsec->sid, isec->sid, SECCLASS_SHM, 48181da177e4SLinus Torvalds SHM__ASSOCIATE, &ad); 48191da177e4SLinus Torvalds } 48201da177e4SLinus Torvalds 48211da177e4SLinus Torvalds /* Note, at this point, shp is locked down */ 48221da177e4SLinus Torvalds static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd) 48231da177e4SLinus Torvalds { 48241da177e4SLinus Torvalds int perms; 48251da177e4SLinus Torvalds int err; 48261da177e4SLinus Torvalds 48271da177e4SLinus Torvalds switch(cmd) { 48281da177e4SLinus Torvalds case IPC_INFO: 48291da177e4SLinus Torvalds case SHM_INFO: 48301da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 48311da177e4SLinus Torvalds return task_has_system(current, SYSTEM__IPC_INFO); 48321da177e4SLinus Torvalds case IPC_STAT: 48331da177e4SLinus Torvalds case SHM_STAT: 48341da177e4SLinus Torvalds perms = SHM__GETATTR | SHM__ASSOCIATE; 48351da177e4SLinus Torvalds break; 48361da177e4SLinus Torvalds case IPC_SET: 48371da177e4SLinus Torvalds perms = SHM__SETATTR; 48381da177e4SLinus Torvalds break; 48391da177e4SLinus Torvalds case SHM_LOCK: 48401da177e4SLinus Torvalds case SHM_UNLOCK: 48411da177e4SLinus Torvalds perms = SHM__LOCK; 48421da177e4SLinus Torvalds break; 48431da177e4SLinus Torvalds case IPC_RMID: 48441da177e4SLinus Torvalds perms = SHM__DESTROY; 48451da177e4SLinus Torvalds break; 48461da177e4SLinus Torvalds default: 48471da177e4SLinus Torvalds return 0; 48481da177e4SLinus Torvalds } 48491da177e4SLinus Torvalds 48506af963f1SStephen Smalley err = ipc_has_perm(&shp->shm_perm, perms); 48511da177e4SLinus Torvalds return err; 48521da177e4SLinus Torvalds } 48531da177e4SLinus Torvalds 48541da177e4SLinus Torvalds static int selinux_shm_shmat(struct shmid_kernel *shp, 48551da177e4SLinus Torvalds char __user *shmaddr, int shmflg) 48561da177e4SLinus Torvalds { 48571da177e4SLinus Torvalds u32 perms; 48581da177e4SLinus Torvalds int rc; 48591da177e4SLinus Torvalds 48601da177e4SLinus Torvalds rc = secondary_ops->shm_shmat(shp, shmaddr, shmflg); 48611da177e4SLinus Torvalds if (rc) 48621da177e4SLinus Torvalds return rc; 48631da177e4SLinus Torvalds 48641da177e4SLinus Torvalds if (shmflg & SHM_RDONLY) 48651da177e4SLinus Torvalds perms = SHM__READ; 48661da177e4SLinus Torvalds else 48671da177e4SLinus Torvalds perms = SHM__READ | SHM__WRITE; 48681da177e4SLinus Torvalds 48696af963f1SStephen Smalley return ipc_has_perm(&shp->shm_perm, perms); 48701da177e4SLinus Torvalds } 48711da177e4SLinus Torvalds 48721da177e4SLinus Torvalds /* Semaphore security operations */ 48731da177e4SLinus Torvalds static int selinux_sem_alloc_security(struct sem_array *sma) 48741da177e4SLinus Torvalds { 48751da177e4SLinus Torvalds struct task_security_struct *tsec; 48761da177e4SLinus Torvalds struct ipc_security_struct *isec; 48771da177e4SLinus Torvalds struct avc_audit_data ad; 48781da177e4SLinus Torvalds int rc; 48791da177e4SLinus Torvalds 48801da177e4SLinus Torvalds rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM); 48811da177e4SLinus Torvalds if (rc) 48821da177e4SLinus Torvalds return rc; 48831da177e4SLinus Torvalds 48841da177e4SLinus Torvalds tsec = current->security; 48851da177e4SLinus Torvalds isec = sma->sem_perm.security; 48861da177e4SLinus Torvalds 48871da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 48881da177e4SLinus Torvalds ad.u.ipc_id = sma->sem_perm.key; 48891da177e4SLinus Torvalds 48901da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_SEM, 48911da177e4SLinus Torvalds SEM__CREATE, &ad); 48921da177e4SLinus Torvalds if (rc) { 48931da177e4SLinus Torvalds ipc_free_security(&sma->sem_perm); 48941da177e4SLinus Torvalds return rc; 48951da177e4SLinus Torvalds } 48961da177e4SLinus Torvalds return 0; 48971da177e4SLinus Torvalds } 48981da177e4SLinus Torvalds 48991da177e4SLinus Torvalds static void selinux_sem_free_security(struct sem_array *sma) 49001da177e4SLinus Torvalds { 49011da177e4SLinus Torvalds ipc_free_security(&sma->sem_perm); 49021da177e4SLinus Torvalds } 49031da177e4SLinus Torvalds 49041da177e4SLinus Torvalds static int selinux_sem_associate(struct sem_array *sma, int semflg) 49051da177e4SLinus Torvalds { 49061da177e4SLinus Torvalds struct task_security_struct *tsec; 49071da177e4SLinus Torvalds struct ipc_security_struct *isec; 49081da177e4SLinus Torvalds struct avc_audit_data ad; 49091da177e4SLinus Torvalds 49101da177e4SLinus Torvalds tsec = current->security; 49111da177e4SLinus Torvalds isec = sma->sem_perm.security; 49121da177e4SLinus Torvalds 49131da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 49141da177e4SLinus Torvalds ad.u.ipc_id = sma->sem_perm.key; 49151da177e4SLinus Torvalds 49161da177e4SLinus Torvalds return avc_has_perm(tsec->sid, isec->sid, SECCLASS_SEM, 49171da177e4SLinus Torvalds SEM__ASSOCIATE, &ad); 49181da177e4SLinus Torvalds } 49191da177e4SLinus Torvalds 49201da177e4SLinus Torvalds /* Note, at this point, sma is locked down */ 49211da177e4SLinus Torvalds static int selinux_sem_semctl(struct sem_array *sma, int cmd) 49221da177e4SLinus Torvalds { 49231da177e4SLinus Torvalds int err; 49241da177e4SLinus Torvalds u32 perms; 49251da177e4SLinus Torvalds 49261da177e4SLinus Torvalds switch(cmd) { 49271da177e4SLinus Torvalds case IPC_INFO: 49281da177e4SLinus Torvalds case SEM_INFO: 49291da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 49301da177e4SLinus Torvalds return task_has_system(current, SYSTEM__IPC_INFO); 49311da177e4SLinus Torvalds case GETPID: 49321da177e4SLinus Torvalds case GETNCNT: 49331da177e4SLinus Torvalds case GETZCNT: 49341da177e4SLinus Torvalds perms = SEM__GETATTR; 49351da177e4SLinus Torvalds break; 49361da177e4SLinus Torvalds case GETVAL: 49371da177e4SLinus Torvalds case GETALL: 49381da177e4SLinus Torvalds perms = SEM__READ; 49391da177e4SLinus Torvalds break; 49401da177e4SLinus Torvalds case SETVAL: 49411da177e4SLinus Torvalds case SETALL: 49421da177e4SLinus Torvalds perms = SEM__WRITE; 49431da177e4SLinus Torvalds break; 49441da177e4SLinus Torvalds case IPC_RMID: 49451da177e4SLinus Torvalds perms = SEM__DESTROY; 49461da177e4SLinus Torvalds break; 49471da177e4SLinus Torvalds case IPC_SET: 49481da177e4SLinus Torvalds perms = SEM__SETATTR; 49491da177e4SLinus Torvalds break; 49501da177e4SLinus Torvalds case IPC_STAT: 49511da177e4SLinus Torvalds case SEM_STAT: 49521da177e4SLinus Torvalds perms = SEM__GETATTR | SEM__ASSOCIATE; 49531da177e4SLinus Torvalds break; 49541da177e4SLinus Torvalds default: 49551da177e4SLinus Torvalds return 0; 49561da177e4SLinus Torvalds } 49571da177e4SLinus Torvalds 49586af963f1SStephen Smalley err = ipc_has_perm(&sma->sem_perm, perms); 49591da177e4SLinus Torvalds return err; 49601da177e4SLinus Torvalds } 49611da177e4SLinus Torvalds 49621da177e4SLinus Torvalds static int selinux_sem_semop(struct sem_array *sma, 49631da177e4SLinus Torvalds struct sembuf *sops, unsigned nsops, int alter) 49641da177e4SLinus Torvalds { 49651da177e4SLinus Torvalds u32 perms; 49661da177e4SLinus Torvalds 49671da177e4SLinus Torvalds if (alter) 49681da177e4SLinus Torvalds perms = SEM__READ | SEM__WRITE; 49691da177e4SLinus Torvalds else 49701da177e4SLinus Torvalds perms = SEM__READ; 49711da177e4SLinus Torvalds 49726af963f1SStephen Smalley return ipc_has_perm(&sma->sem_perm, perms); 49731da177e4SLinus Torvalds } 49741da177e4SLinus Torvalds 49751da177e4SLinus Torvalds static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag) 49761da177e4SLinus Torvalds { 49771da177e4SLinus Torvalds u32 av = 0; 49781da177e4SLinus Torvalds 49791da177e4SLinus Torvalds av = 0; 49801da177e4SLinus Torvalds if (flag & S_IRUGO) 49811da177e4SLinus Torvalds av |= IPC__UNIX_READ; 49821da177e4SLinus Torvalds if (flag & S_IWUGO) 49831da177e4SLinus Torvalds av |= IPC__UNIX_WRITE; 49841da177e4SLinus Torvalds 49851da177e4SLinus Torvalds if (av == 0) 49861da177e4SLinus Torvalds return 0; 49871da177e4SLinus Torvalds 49886af963f1SStephen Smalley return ipc_has_perm(ipcp, av); 49891da177e4SLinus Torvalds } 49901da177e4SLinus Torvalds 49911da177e4SLinus Torvalds /* module stacking operations */ 49921da177e4SLinus Torvalds static int selinux_register_security (const char *name, struct security_operations *ops) 49931da177e4SLinus Torvalds { 49941da177e4SLinus Torvalds if (secondary_ops != original_ops) { 4995fadcdb45SEric Paris printk(KERN_ERR "%s: There is already a secondary security " 49961da177e4SLinus Torvalds "module registered.\n", __FUNCTION__); 49971da177e4SLinus Torvalds return -EINVAL; 49981da177e4SLinus Torvalds } 49991da177e4SLinus Torvalds 50001da177e4SLinus Torvalds secondary_ops = ops; 50011da177e4SLinus Torvalds 50021da177e4SLinus Torvalds printk(KERN_INFO "%s: Registering secondary module %s\n", 50031da177e4SLinus Torvalds __FUNCTION__, 50041da177e4SLinus Torvalds name); 50051da177e4SLinus Torvalds 50061da177e4SLinus Torvalds return 0; 50071da177e4SLinus Torvalds } 50081da177e4SLinus Torvalds 50091da177e4SLinus Torvalds static void selinux_d_instantiate (struct dentry *dentry, struct inode *inode) 50101da177e4SLinus Torvalds { 50111da177e4SLinus Torvalds if (inode) 50121da177e4SLinus Torvalds inode_doinit_with_dentry(inode, dentry); 50131da177e4SLinus Torvalds } 50141da177e4SLinus Torvalds 50151da177e4SLinus Torvalds static int selinux_getprocattr(struct task_struct *p, 501604ff9708SAl Viro char *name, char **value) 50171da177e4SLinus Torvalds { 50181da177e4SLinus Torvalds struct task_security_struct *tsec; 50198c8570fbSDustin Kirkland u32 sid; 50201da177e4SLinus Torvalds int error; 502104ff9708SAl Viro unsigned len; 50221da177e4SLinus Torvalds 50231da177e4SLinus Torvalds if (current != p) { 50241da177e4SLinus Torvalds error = task_has_perm(current, p, PROCESS__GETATTR); 50251da177e4SLinus Torvalds if (error) 50261da177e4SLinus Torvalds return error; 50271da177e4SLinus Torvalds } 50281da177e4SLinus Torvalds 50291da177e4SLinus Torvalds tsec = p->security; 50301da177e4SLinus Torvalds 50311da177e4SLinus Torvalds if (!strcmp(name, "current")) 50321da177e4SLinus Torvalds sid = tsec->sid; 50331da177e4SLinus Torvalds else if (!strcmp(name, "prev")) 50341da177e4SLinus Torvalds sid = tsec->osid; 50351da177e4SLinus Torvalds else if (!strcmp(name, "exec")) 50361da177e4SLinus Torvalds sid = tsec->exec_sid; 50371da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 50381da177e4SLinus Torvalds sid = tsec->create_sid; 50394eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) 50404eb582cfSMichael LeMay sid = tsec->keycreate_sid; 504142c3e03eSEric Paris else if (!strcmp(name, "sockcreate")) 504242c3e03eSEric Paris sid = tsec->sockcreate_sid; 50431da177e4SLinus Torvalds else 50441da177e4SLinus Torvalds return -EINVAL; 50451da177e4SLinus Torvalds 50461da177e4SLinus Torvalds if (!sid) 50471da177e4SLinus Torvalds return 0; 50481da177e4SLinus Torvalds 504904ff9708SAl Viro error = security_sid_to_context(sid, value, &len); 505004ff9708SAl Viro if (error) 505104ff9708SAl Viro return error; 505204ff9708SAl Viro return len; 50531da177e4SLinus Torvalds } 50541da177e4SLinus Torvalds 50551da177e4SLinus Torvalds static int selinux_setprocattr(struct task_struct *p, 50561da177e4SLinus Torvalds char *name, void *value, size_t size) 50571da177e4SLinus Torvalds { 50581da177e4SLinus Torvalds struct task_security_struct *tsec; 50591da177e4SLinus Torvalds u32 sid = 0; 50601da177e4SLinus Torvalds int error; 50611da177e4SLinus Torvalds char *str = value; 50621da177e4SLinus Torvalds 50631da177e4SLinus Torvalds if (current != p) { 50641da177e4SLinus Torvalds /* SELinux only allows a process to change its own 50651da177e4SLinus Torvalds security attributes. */ 50661da177e4SLinus Torvalds return -EACCES; 50671da177e4SLinus Torvalds } 50681da177e4SLinus Torvalds 50691da177e4SLinus Torvalds /* 50701da177e4SLinus Torvalds * Basic control over ability to set these attributes at all. 50711da177e4SLinus Torvalds * current == p, but we'll pass them separately in case the 50721da177e4SLinus Torvalds * above restriction is ever removed. 50731da177e4SLinus Torvalds */ 50741da177e4SLinus Torvalds if (!strcmp(name, "exec")) 50751da177e4SLinus Torvalds error = task_has_perm(current, p, PROCESS__SETEXEC); 50761da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 50771da177e4SLinus Torvalds error = task_has_perm(current, p, PROCESS__SETFSCREATE); 50784eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) 50794eb582cfSMichael LeMay error = task_has_perm(current, p, PROCESS__SETKEYCREATE); 508042c3e03eSEric Paris else if (!strcmp(name, "sockcreate")) 508142c3e03eSEric Paris error = task_has_perm(current, p, PROCESS__SETSOCKCREATE); 50821da177e4SLinus Torvalds else if (!strcmp(name, "current")) 50831da177e4SLinus Torvalds error = task_has_perm(current, p, PROCESS__SETCURRENT); 50841da177e4SLinus Torvalds else 50851da177e4SLinus Torvalds error = -EINVAL; 50861da177e4SLinus Torvalds if (error) 50871da177e4SLinus Torvalds return error; 50881da177e4SLinus Torvalds 50891da177e4SLinus Torvalds /* Obtain a SID for the context, if one was specified. */ 50901da177e4SLinus Torvalds if (size && str[1] && str[1] != '\n') { 50911da177e4SLinus Torvalds if (str[size-1] == '\n') { 50921da177e4SLinus Torvalds str[size-1] = 0; 50931da177e4SLinus Torvalds size--; 50941da177e4SLinus Torvalds } 50951da177e4SLinus Torvalds error = security_context_to_sid(value, size, &sid); 50961da177e4SLinus Torvalds if (error) 50971da177e4SLinus Torvalds return error; 50981da177e4SLinus Torvalds } 50991da177e4SLinus Torvalds 51001da177e4SLinus Torvalds /* Permission checking based on the specified context is 51011da177e4SLinus Torvalds performed during the actual operation (execve, 51021da177e4SLinus Torvalds open/mkdir/...), when we know the full context of the 51031da177e4SLinus Torvalds operation. See selinux_bprm_set_security for the execve 51041da177e4SLinus Torvalds checks and may_create for the file creation checks. The 51051da177e4SLinus Torvalds operation will then fail if the context is not permitted. */ 51061da177e4SLinus Torvalds tsec = p->security; 51071da177e4SLinus Torvalds if (!strcmp(name, "exec")) 51081da177e4SLinus Torvalds tsec->exec_sid = sid; 51091da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 51101da177e4SLinus Torvalds tsec->create_sid = sid; 51114eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) { 51124eb582cfSMichael LeMay error = may_create_key(sid, p); 51134eb582cfSMichael LeMay if (error) 51144eb582cfSMichael LeMay return error; 51154eb582cfSMichael LeMay tsec->keycreate_sid = sid; 511642c3e03eSEric Paris } else if (!strcmp(name, "sockcreate")) 511742c3e03eSEric Paris tsec->sockcreate_sid = sid; 511842c3e03eSEric Paris else if (!strcmp(name, "current")) { 51191da177e4SLinus Torvalds struct av_decision avd; 51201da177e4SLinus Torvalds 51211da177e4SLinus Torvalds if (sid == 0) 51221da177e4SLinus Torvalds return -EINVAL; 51231da177e4SLinus Torvalds 51241da177e4SLinus Torvalds /* Only allow single threaded processes to change context */ 51251da177e4SLinus Torvalds if (atomic_read(&p->mm->mm_users) != 1) { 51261da177e4SLinus Torvalds struct task_struct *g, *t; 51271da177e4SLinus Torvalds struct mm_struct *mm = p->mm; 51281da177e4SLinus Torvalds read_lock(&tasklist_lock); 51291da177e4SLinus Torvalds do_each_thread(g, t) 51301da177e4SLinus Torvalds if (t->mm == mm && t != p) { 51311da177e4SLinus Torvalds read_unlock(&tasklist_lock); 51321da177e4SLinus Torvalds return -EPERM; 51331da177e4SLinus Torvalds } 51341da177e4SLinus Torvalds while_each_thread(g, t); 51351da177e4SLinus Torvalds read_unlock(&tasklist_lock); 51361da177e4SLinus Torvalds } 51371da177e4SLinus Torvalds 51381da177e4SLinus Torvalds /* Check permissions for the transition. */ 51391da177e4SLinus Torvalds error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS, 51401da177e4SLinus Torvalds PROCESS__DYNTRANSITION, NULL); 51411da177e4SLinus Torvalds if (error) 51421da177e4SLinus Torvalds return error; 51431da177e4SLinus Torvalds 51441da177e4SLinus Torvalds /* Check for ptracing, and update the task SID if ok. 51451da177e4SLinus Torvalds Otherwise, leave SID unchanged and fail. */ 51461da177e4SLinus Torvalds task_lock(p); 51471da177e4SLinus Torvalds if (p->ptrace & PT_PTRACED) { 51481da177e4SLinus Torvalds error = avc_has_perm_noaudit(tsec->ptrace_sid, sid, 51491da177e4SLinus Torvalds SECCLASS_PROCESS, 51502c3c05dbSStephen Smalley PROCESS__PTRACE, 0, &avd); 51511da177e4SLinus Torvalds if (!error) 51521da177e4SLinus Torvalds tsec->sid = sid; 51531da177e4SLinus Torvalds task_unlock(p); 51541da177e4SLinus Torvalds avc_audit(tsec->ptrace_sid, sid, SECCLASS_PROCESS, 51551da177e4SLinus Torvalds PROCESS__PTRACE, &avd, error, NULL); 51561da177e4SLinus Torvalds if (error) 51571da177e4SLinus Torvalds return error; 51581da177e4SLinus Torvalds } else { 51591da177e4SLinus Torvalds tsec->sid = sid; 51601da177e4SLinus Torvalds task_unlock(p); 51611da177e4SLinus Torvalds } 51621da177e4SLinus Torvalds } 51631da177e4SLinus Torvalds else 51641da177e4SLinus Torvalds return -EINVAL; 51651da177e4SLinus Torvalds 51661da177e4SLinus Torvalds return size; 51671da177e4SLinus Torvalds } 51681da177e4SLinus Torvalds 5169dc49c1f9SCatherine Zhang static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) 5170dc49c1f9SCatherine Zhang { 5171dc49c1f9SCatherine Zhang return security_sid_to_context(secid, secdata, seclen); 5172dc49c1f9SCatherine Zhang } 5173dc49c1f9SCatherine Zhang 517463cb3449SDavid Howells static int selinux_secctx_to_secid(char *secdata, u32 seclen, u32 *secid) 517563cb3449SDavid Howells { 517663cb3449SDavid Howells return security_context_to_sid(secdata, seclen, secid); 517763cb3449SDavid Howells } 517863cb3449SDavid Howells 5179dc49c1f9SCatherine Zhang static void selinux_release_secctx(char *secdata, u32 seclen) 5180dc49c1f9SCatherine Zhang { 5181dc49c1f9SCatherine Zhang kfree(secdata); 5182dc49c1f9SCatherine Zhang } 5183dc49c1f9SCatherine Zhang 5184d720024eSMichael LeMay #ifdef CONFIG_KEYS 5185d720024eSMichael LeMay 51867e047ef5SDavid Howells static int selinux_key_alloc(struct key *k, struct task_struct *tsk, 51877e047ef5SDavid Howells unsigned long flags) 5188d720024eSMichael LeMay { 5189d720024eSMichael LeMay struct task_security_struct *tsec = tsk->security; 5190d720024eSMichael LeMay struct key_security_struct *ksec; 5191d720024eSMichael LeMay 5192d720024eSMichael LeMay ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL); 5193d720024eSMichael LeMay if (!ksec) 5194d720024eSMichael LeMay return -ENOMEM; 5195d720024eSMichael LeMay 5196d720024eSMichael LeMay ksec->obj = k; 51974eb582cfSMichael LeMay if (tsec->keycreate_sid) 51984eb582cfSMichael LeMay ksec->sid = tsec->keycreate_sid; 51994eb582cfSMichael LeMay else 5200d720024eSMichael LeMay ksec->sid = tsec->sid; 5201d720024eSMichael LeMay k->security = ksec; 5202d720024eSMichael LeMay 5203d720024eSMichael LeMay return 0; 5204d720024eSMichael LeMay } 5205d720024eSMichael LeMay 5206d720024eSMichael LeMay static void selinux_key_free(struct key *k) 5207d720024eSMichael LeMay { 5208d720024eSMichael LeMay struct key_security_struct *ksec = k->security; 5209d720024eSMichael LeMay 5210d720024eSMichael LeMay k->security = NULL; 5211d720024eSMichael LeMay kfree(ksec); 5212d720024eSMichael LeMay } 5213d720024eSMichael LeMay 5214d720024eSMichael LeMay static int selinux_key_permission(key_ref_t key_ref, 5215d720024eSMichael LeMay struct task_struct *ctx, 5216d720024eSMichael LeMay key_perm_t perm) 5217d720024eSMichael LeMay { 5218d720024eSMichael LeMay struct key *key; 5219d720024eSMichael LeMay struct task_security_struct *tsec; 5220d720024eSMichael LeMay struct key_security_struct *ksec; 5221d720024eSMichael LeMay 5222d720024eSMichael LeMay key = key_ref_to_ptr(key_ref); 5223d720024eSMichael LeMay 5224d720024eSMichael LeMay tsec = ctx->security; 5225d720024eSMichael LeMay ksec = key->security; 5226d720024eSMichael LeMay 5227d720024eSMichael LeMay /* if no specific permissions are requested, we skip the 5228d720024eSMichael LeMay permission check. No serious, additional covert channels 5229d720024eSMichael LeMay appear to be created. */ 5230d720024eSMichael LeMay if (perm == 0) 5231d720024eSMichael LeMay return 0; 5232d720024eSMichael LeMay 5233d720024eSMichael LeMay return avc_has_perm(tsec->sid, ksec->sid, 5234d720024eSMichael LeMay SECCLASS_KEY, perm, NULL); 5235d720024eSMichael LeMay } 5236d720024eSMichael LeMay 5237d720024eSMichael LeMay #endif 5238d720024eSMichael LeMay 52391da177e4SLinus Torvalds static struct security_operations selinux_ops = { 52401da177e4SLinus Torvalds .ptrace = selinux_ptrace, 52411da177e4SLinus Torvalds .capget = selinux_capget, 52421da177e4SLinus Torvalds .capset_check = selinux_capset_check, 52431da177e4SLinus Torvalds .capset_set = selinux_capset_set, 52441da177e4SLinus Torvalds .sysctl = selinux_sysctl, 52451da177e4SLinus Torvalds .capable = selinux_capable, 52461da177e4SLinus Torvalds .quotactl = selinux_quotactl, 52471da177e4SLinus Torvalds .quota_on = selinux_quota_on, 52481da177e4SLinus Torvalds .syslog = selinux_syslog, 52491da177e4SLinus Torvalds .vm_enough_memory = selinux_vm_enough_memory, 52501da177e4SLinus Torvalds 52511da177e4SLinus Torvalds .netlink_send = selinux_netlink_send, 52521da177e4SLinus Torvalds .netlink_recv = selinux_netlink_recv, 52531da177e4SLinus Torvalds 52541da177e4SLinus Torvalds .bprm_alloc_security = selinux_bprm_alloc_security, 52551da177e4SLinus Torvalds .bprm_free_security = selinux_bprm_free_security, 52561da177e4SLinus Torvalds .bprm_apply_creds = selinux_bprm_apply_creds, 52571da177e4SLinus Torvalds .bprm_post_apply_creds = selinux_bprm_post_apply_creds, 52581da177e4SLinus Torvalds .bprm_set_security = selinux_bprm_set_security, 52591da177e4SLinus Torvalds .bprm_check_security = selinux_bprm_check_security, 52601da177e4SLinus Torvalds .bprm_secureexec = selinux_bprm_secureexec, 52611da177e4SLinus Torvalds 52621da177e4SLinus Torvalds .sb_alloc_security = selinux_sb_alloc_security, 52631da177e4SLinus Torvalds .sb_free_security = selinux_sb_free_security, 52641da177e4SLinus Torvalds .sb_copy_data = selinux_sb_copy_data, 52651da177e4SLinus Torvalds .sb_kern_mount = selinux_sb_kern_mount, 52661da177e4SLinus Torvalds .sb_statfs = selinux_sb_statfs, 52671da177e4SLinus Torvalds .sb_mount = selinux_mount, 52681da177e4SLinus Torvalds .sb_umount = selinux_umount, 5269c9180a57SEric Paris .sb_get_mnt_opts = selinux_get_mnt_opts, 5270c9180a57SEric Paris .sb_set_mnt_opts = selinux_set_mnt_opts, 5271c9180a57SEric Paris .sb_clone_mnt_opts = selinux_sb_clone_mnt_opts, 5272e0007529SEric Paris .sb_parse_opts_str = selinux_parse_opts_str, 5273e0007529SEric Paris 52741da177e4SLinus Torvalds 52751da177e4SLinus Torvalds .inode_alloc_security = selinux_inode_alloc_security, 52761da177e4SLinus Torvalds .inode_free_security = selinux_inode_free_security, 52775e41ff9eSStephen Smalley .inode_init_security = selinux_inode_init_security, 52781da177e4SLinus Torvalds .inode_create = selinux_inode_create, 52791da177e4SLinus Torvalds .inode_link = selinux_inode_link, 52801da177e4SLinus Torvalds .inode_unlink = selinux_inode_unlink, 52811da177e4SLinus Torvalds .inode_symlink = selinux_inode_symlink, 52821da177e4SLinus Torvalds .inode_mkdir = selinux_inode_mkdir, 52831da177e4SLinus Torvalds .inode_rmdir = selinux_inode_rmdir, 52841da177e4SLinus Torvalds .inode_mknod = selinux_inode_mknod, 52851da177e4SLinus Torvalds .inode_rename = selinux_inode_rename, 52861da177e4SLinus Torvalds .inode_readlink = selinux_inode_readlink, 52871da177e4SLinus Torvalds .inode_follow_link = selinux_inode_follow_link, 52881da177e4SLinus Torvalds .inode_permission = selinux_inode_permission, 52891da177e4SLinus Torvalds .inode_setattr = selinux_inode_setattr, 52901da177e4SLinus Torvalds .inode_getattr = selinux_inode_getattr, 52911da177e4SLinus Torvalds .inode_setxattr = selinux_inode_setxattr, 52921da177e4SLinus Torvalds .inode_post_setxattr = selinux_inode_post_setxattr, 52931da177e4SLinus Torvalds .inode_getxattr = selinux_inode_getxattr, 52941da177e4SLinus Torvalds .inode_listxattr = selinux_inode_listxattr, 52951da177e4SLinus Torvalds .inode_removexattr = selinux_inode_removexattr, 52961da177e4SLinus Torvalds .inode_getsecurity = selinux_inode_getsecurity, 52971da177e4SLinus Torvalds .inode_setsecurity = selinux_inode_setsecurity, 52981da177e4SLinus Torvalds .inode_listsecurity = selinux_inode_listsecurity, 5299b5376771SSerge E. Hallyn .inode_need_killpriv = selinux_inode_need_killpriv, 5300b5376771SSerge E. Hallyn .inode_killpriv = selinux_inode_killpriv, 53011da177e4SLinus Torvalds 53021da177e4SLinus Torvalds .file_permission = selinux_file_permission, 53031da177e4SLinus Torvalds .file_alloc_security = selinux_file_alloc_security, 53041da177e4SLinus Torvalds .file_free_security = selinux_file_free_security, 53051da177e4SLinus Torvalds .file_ioctl = selinux_file_ioctl, 53061da177e4SLinus Torvalds .file_mmap = selinux_file_mmap, 53071da177e4SLinus Torvalds .file_mprotect = selinux_file_mprotect, 53081da177e4SLinus Torvalds .file_lock = selinux_file_lock, 53091da177e4SLinus Torvalds .file_fcntl = selinux_file_fcntl, 53101da177e4SLinus Torvalds .file_set_fowner = selinux_file_set_fowner, 53111da177e4SLinus Torvalds .file_send_sigiotask = selinux_file_send_sigiotask, 53121da177e4SLinus Torvalds .file_receive = selinux_file_receive, 53131da177e4SLinus Torvalds 5314788e7dd4SYuichi Nakamura .dentry_open = selinux_dentry_open, 5315788e7dd4SYuichi Nakamura 53161da177e4SLinus Torvalds .task_create = selinux_task_create, 53171da177e4SLinus Torvalds .task_alloc_security = selinux_task_alloc_security, 53181da177e4SLinus Torvalds .task_free_security = selinux_task_free_security, 53191da177e4SLinus Torvalds .task_setuid = selinux_task_setuid, 53201da177e4SLinus Torvalds .task_post_setuid = selinux_task_post_setuid, 53211da177e4SLinus Torvalds .task_setgid = selinux_task_setgid, 53221da177e4SLinus Torvalds .task_setpgid = selinux_task_setpgid, 53231da177e4SLinus Torvalds .task_getpgid = selinux_task_getpgid, 53241da177e4SLinus Torvalds .task_getsid = selinux_task_getsid, 5325f9008e4cSDavid Quigley .task_getsecid = selinux_task_getsecid, 53261da177e4SLinus Torvalds .task_setgroups = selinux_task_setgroups, 53271da177e4SLinus Torvalds .task_setnice = selinux_task_setnice, 532803e68060SJames Morris .task_setioprio = selinux_task_setioprio, 5329a1836a42SDavid Quigley .task_getioprio = selinux_task_getioprio, 53301da177e4SLinus Torvalds .task_setrlimit = selinux_task_setrlimit, 53311da177e4SLinus Torvalds .task_setscheduler = selinux_task_setscheduler, 53321da177e4SLinus Torvalds .task_getscheduler = selinux_task_getscheduler, 533335601547SDavid Quigley .task_movememory = selinux_task_movememory, 53341da177e4SLinus Torvalds .task_kill = selinux_task_kill, 53351da177e4SLinus Torvalds .task_wait = selinux_task_wait, 53361da177e4SLinus Torvalds .task_prctl = selinux_task_prctl, 53371da177e4SLinus Torvalds .task_reparent_to_init = selinux_task_reparent_to_init, 53381da177e4SLinus Torvalds .task_to_inode = selinux_task_to_inode, 53391da177e4SLinus Torvalds 53401da177e4SLinus Torvalds .ipc_permission = selinux_ipc_permission, 53411da177e4SLinus Torvalds 53421da177e4SLinus Torvalds .msg_msg_alloc_security = selinux_msg_msg_alloc_security, 53431da177e4SLinus Torvalds .msg_msg_free_security = selinux_msg_msg_free_security, 53441da177e4SLinus Torvalds 53451da177e4SLinus Torvalds .msg_queue_alloc_security = selinux_msg_queue_alloc_security, 53461da177e4SLinus Torvalds .msg_queue_free_security = selinux_msg_queue_free_security, 53471da177e4SLinus Torvalds .msg_queue_associate = selinux_msg_queue_associate, 53481da177e4SLinus Torvalds .msg_queue_msgctl = selinux_msg_queue_msgctl, 53491da177e4SLinus Torvalds .msg_queue_msgsnd = selinux_msg_queue_msgsnd, 53501da177e4SLinus Torvalds .msg_queue_msgrcv = selinux_msg_queue_msgrcv, 53511da177e4SLinus Torvalds 53521da177e4SLinus Torvalds .shm_alloc_security = selinux_shm_alloc_security, 53531da177e4SLinus Torvalds .shm_free_security = selinux_shm_free_security, 53541da177e4SLinus Torvalds .shm_associate = selinux_shm_associate, 53551da177e4SLinus Torvalds .shm_shmctl = selinux_shm_shmctl, 53561da177e4SLinus Torvalds .shm_shmat = selinux_shm_shmat, 53571da177e4SLinus Torvalds 53581da177e4SLinus Torvalds .sem_alloc_security = selinux_sem_alloc_security, 53591da177e4SLinus Torvalds .sem_free_security = selinux_sem_free_security, 53601da177e4SLinus Torvalds .sem_associate = selinux_sem_associate, 53611da177e4SLinus Torvalds .sem_semctl = selinux_sem_semctl, 53621da177e4SLinus Torvalds .sem_semop = selinux_sem_semop, 53631da177e4SLinus Torvalds 53641da177e4SLinus Torvalds .register_security = selinux_register_security, 53651da177e4SLinus Torvalds 53661da177e4SLinus Torvalds .d_instantiate = selinux_d_instantiate, 53671da177e4SLinus Torvalds 53681da177e4SLinus Torvalds .getprocattr = selinux_getprocattr, 53691da177e4SLinus Torvalds .setprocattr = selinux_setprocattr, 53701da177e4SLinus Torvalds 5371dc49c1f9SCatherine Zhang .secid_to_secctx = selinux_secid_to_secctx, 537263cb3449SDavid Howells .secctx_to_secid = selinux_secctx_to_secid, 5373dc49c1f9SCatherine Zhang .release_secctx = selinux_release_secctx, 5374dc49c1f9SCatherine Zhang 53751da177e4SLinus Torvalds .unix_stream_connect = selinux_socket_unix_stream_connect, 53761da177e4SLinus Torvalds .unix_may_send = selinux_socket_unix_may_send, 53771da177e4SLinus Torvalds 53781da177e4SLinus Torvalds .socket_create = selinux_socket_create, 53791da177e4SLinus Torvalds .socket_post_create = selinux_socket_post_create, 53801da177e4SLinus Torvalds .socket_bind = selinux_socket_bind, 53811da177e4SLinus Torvalds .socket_connect = selinux_socket_connect, 53821da177e4SLinus Torvalds .socket_listen = selinux_socket_listen, 53831da177e4SLinus Torvalds .socket_accept = selinux_socket_accept, 53841da177e4SLinus Torvalds .socket_sendmsg = selinux_socket_sendmsg, 53851da177e4SLinus Torvalds .socket_recvmsg = selinux_socket_recvmsg, 53861da177e4SLinus Torvalds .socket_getsockname = selinux_socket_getsockname, 53871da177e4SLinus Torvalds .socket_getpeername = selinux_socket_getpeername, 53881da177e4SLinus Torvalds .socket_getsockopt = selinux_socket_getsockopt, 53891da177e4SLinus Torvalds .socket_setsockopt = selinux_socket_setsockopt, 53901da177e4SLinus Torvalds .socket_shutdown = selinux_socket_shutdown, 53911da177e4SLinus Torvalds .socket_sock_rcv_skb = selinux_socket_sock_rcv_skb, 53922c7946a7SCatherine Zhang .socket_getpeersec_stream = selinux_socket_getpeersec_stream, 53932c7946a7SCatherine Zhang .socket_getpeersec_dgram = selinux_socket_getpeersec_dgram, 53941da177e4SLinus Torvalds .sk_alloc_security = selinux_sk_alloc_security, 53951da177e4SLinus Torvalds .sk_free_security = selinux_sk_free_security, 5396892c141eSVenkat Yekkirala .sk_clone_security = selinux_sk_clone_security, 5397beb8d13bSVenkat Yekkirala .sk_getsecid = selinux_sk_getsecid, 53984237c75cSVenkat Yekkirala .sock_graft = selinux_sock_graft, 53994237c75cSVenkat Yekkirala .inet_conn_request = selinux_inet_conn_request, 54004237c75cSVenkat Yekkirala .inet_csk_clone = selinux_inet_csk_clone, 54016b877699SVenkat Yekkirala .inet_conn_established = selinux_inet_conn_established, 54024237c75cSVenkat Yekkirala .req_classify_flow = selinux_req_classify_flow, 5403d28d1e08STrent Jaeger 5404d28d1e08STrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM 5405d28d1e08STrent Jaeger .xfrm_policy_alloc_security = selinux_xfrm_policy_alloc, 5406d28d1e08STrent Jaeger .xfrm_policy_clone_security = selinux_xfrm_policy_clone, 5407d28d1e08STrent Jaeger .xfrm_policy_free_security = selinux_xfrm_policy_free, 5408c8c05a8eSCatherine Zhang .xfrm_policy_delete_security = selinux_xfrm_policy_delete, 5409d28d1e08STrent Jaeger .xfrm_state_alloc_security = selinux_xfrm_state_alloc, 5410d28d1e08STrent Jaeger .xfrm_state_free_security = selinux_xfrm_state_free, 5411c8c05a8eSCatherine Zhang .xfrm_state_delete_security = selinux_xfrm_state_delete, 5412d28d1e08STrent Jaeger .xfrm_policy_lookup = selinux_xfrm_policy_lookup, 5413e0d1caa7SVenkat Yekkirala .xfrm_state_pol_flow_match = selinux_xfrm_state_pol_flow_match, 5414e0d1caa7SVenkat Yekkirala .xfrm_decode_session = selinux_xfrm_decode_session, 54151da177e4SLinus Torvalds #endif 5416d720024eSMichael LeMay 5417d720024eSMichael LeMay #ifdef CONFIG_KEYS 5418d720024eSMichael LeMay .key_alloc = selinux_key_alloc, 5419d720024eSMichael LeMay .key_free = selinux_key_free, 5420d720024eSMichael LeMay .key_permission = selinux_key_permission, 5421d720024eSMichael LeMay #endif 54221da177e4SLinus Torvalds }; 54231da177e4SLinus Torvalds 54241da177e4SLinus Torvalds static __init int selinux_init(void) 54251da177e4SLinus Torvalds { 54261da177e4SLinus Torvalds struct task_security_struct *tsec; 54271da177e4SLinus Torvalds 54281da177e4SLinus Torvalds if (!selinux_enabled) { 54291da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Disabled at boot.\n"); 54301da177e4SLinus Torvalds return 0; 54311da177e4SLinus Torvalds } 54321da177e4SLinus Torvalds 54331da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Initializing.\n"); 54341da177e4SLinus Torvalds 54351da177e4SLinus Torvalds /* Set the security state for the initial task. */ 54361da177e4SLinus Torvalds if (task_alloc_security(current)) 54371da177e4SLinus Torvalds panic("SELinux: Failed to initialize initial task.\n"); 54381da177e4SLinus Torvalds tsec = current->security; 54391da177e4SLinus Torvalds tsec->osid = tsec->sid = SECINITSID_KERNEL; 54401da177e4SLinus Torvalds 54417cae7e26SJames Morris sel_inode_cache = kmem_cache_create("selinux_inode_security", 54427cae7e26SJames Morris sizeof(struct inode_security_struct), 544320c2df83SPaul Mundt 0, SLAB_PANIC, NULL); 54441da177e4SLinus Torvalds avc_init(); 54451da177e4SLinus Torvalds 54461da177e4SLinus Torvalds original_ops = secondary_ops = security_ops; 54471da177e4SLinus Torvalds if (!secondary_ops) 54481da177e4SLinus Torvalds panic ("SELinux: No initial security operations\n"); 54491da177e4SLinus Torvalds if (register_security (&selinux_ops)) 54501da177e4SLinus Torvalds panic("SELinux: Unable to register with kernel.\n"); 54511da177e4SLinus Torvalds 54521da177e4SLinus Torvalds if (selinux_enforcing) { 5453fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Starting in enforcing mode\n"); 54541da177e4SLinus Torvalds } else { 5455fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Starting in permissive mode\n"); 54561da177e4SLinus Torvalds } 5457d720024eSMichael LeMay 5458d720024eSMichael LeMay #ifdef CONFIG_KEYS 5459d720024eSMichael LeMay /* Add security information to initial keyrings */ 54604eb582cfSMichael LeMay selinux_key_alloc(&root_user_keyring, current, 54617e047ef5SDavid Howells KEY_ALLOC_NOT_IN_QUOTA); 54624eb582cfSMichael LeMay selinux_key_alloc(&root_session_keyring, current, 54637e047ef5SDavid Howells KEY_ALLOC_NOT_IN_QUOTA); 5464d720024eSMichael LeMay #endif 5465d720024eSMichael LeMay 54661da177e4SLinus Torvalds return 0; 54671da177e4SLinus Torvalds } 54681da177e4SLinus Torvalds 54691da177e4SLinus Torvalds void selinux_complete_init(void) 54701da177e4SLinus Torvalds { 5471fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Completing initialization.\n"); 54721da177e4SLinus Torvalds 54731da177e4SLinus Torvalds /* Set up any superblocks initialized prior to the policy load. */ 5474fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Setting up existing superblocks.\n"); 5475ba0c19edSStephen Smalley spin_lock(&sb_lock); 54761da177e4SLinus Torvalds spin_lock(&sb_security_lock); 54771da177e4SLinus Torvalds next_sb: 54781da177e4SLinus Torvalds if (!list_empty(&superblock_security_head)) { 54791da177e4SLinus Torvalds struct superblock_security_struct *sbsec = 54801da177e4SLinus Torvalds list_entry(superblock_security_head.next, 54811da177e4SLinus Torvalds struct superblock_security_struct, 54821da177e4SLinus Torvalds list); 54831da177e4SLinus Torvalds struct super_block *sb = sbsec->sb; 54841da177e4SLinus Torvalds sb->s_count++; 54851da177e4SLinus Torvalds spin_unlock(&sb_security_lock); 5486ba0c19edSStephen Smalley spin_unlock(&sb_lock); 54871da177e4SLinus Torvalds down_read(&sb->s_umount); 54881da177e4SLinus Torvalds if (sb->s_root) 54891da177e4SLinus Torvalds superblock_doinit(sb, NULL); 54901da177e4SLinus Torvalds drop_super(sb); 5491ba0c19edSStephen Smalley spin_lock(&sb_lock); 54921da177e4SLinus Torvalds spin_lock(&sb_security_lock); 54931da177e4SLinus Torvalds list_del_init(&sbsec->list); 54941da177e4SLinus Torvalds goto next_sb; 54951da177e4SLinus Torvalds } 54961da177e4SLinus Torvalds spin_unlock(&sb_security_lock); 5497ba0c19edSStephen Smalley spin_unlock(&sb_lock); 54981da177e4SLinus Torvalds } 54991da177e4SLinus Torvalds 55001da177e4SLinus Torvalds /* SELinux requires early initialization in order to label 55011da177e4SLinus Torvalds all processes and objects when they are created. */ 55021da177e4SLinus Torvalds security_initcall(selinux_init); 55031da177e4SLinus Torvalds 5504c2b507fdSStephen Smalley #if defined(CONFIG_NETFILTER) 55051da177e4SLinus Torvalds 5506effad8dfSPaul Moore static struct nf_hook_ops selinux_ipv4_ops[] = { 5507effad8dfSPaul Moore { 5508effad8dfSPaul Moore .hook = selinux_ipv4_postroute, 55091da177e4SLinus Torvalds .owner = THIS_MODULE, 55101da177e4SLinus Torvalds .pf = PF_INET, 55116e23ae2aSPatrick McHardy .hooknum = NF_INET_POST_ROUTING, 55121da177e4SLinus Torvalds .priority = NF_IP_PRI_SELINUX_LAST, 5513effad8dfSPaul Moore }, 5514effad8dfSPaul Moore { 5515effad8dfSPaul Moore .hook = selinux_ipv4_forward, 5516effad8dfSPaul Moore .owner = THIS_MODULE, 5517effad8dfSPaul Moore .pf = PF_INET, 5518effad8dfSPaul Moore .hooknum = NF_INET_FORWARD, 5519effad8dfSPaul Moore .priority = NF_IP_PRI_SELINUX_FIRST, 5520effad8dfSPaul Moore } 55211da177e4SLinus Torvalds }; 55221da177e4SLinus Torvalds 55231da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 55241da177e4SLinus Torvalds 5525effad8dfSPaul Moore static struct nf_hook_ops selinux_ipv6_ops[] = { 5526effad8dfSPaul Moore { 5527effad8dfSPaul Moore .hook = selinux_ipv6_postroute, 55281da177e4SLinus Torvalds .owner = THIS_MODULE, 55291da177e4SLinus Torvalds .pf = PF_INET6, 55306e23ae2aSPatrick McHardy .hooknum = NF_INET_POST_ROUTING, 55311da177e4SLinus Torvalds .priority = NF_IP6_PRI_SELINUX_LAST, 5532effad8dfSPaul Moore }, 5533effad8dfSPaul Moore { 5534effad8dfSPaul Moore .hook = selinux_ipv6_forward, 5535effad8dfSPaul Moore .owner = THIS_MODULE, 5536effad8dfSPaul Moore .pf = PF_INET6, 5537effad8dfSPaul Moore .hooknum = NF_INET_FORWARD, 5538effad8dfSPaul Moore .priority = NF_IP6_PRI_SELINUX_FIRST, 5539effad8dfSPaul Moore } 55401da177e4SLinus Torvalds }; 55411da177e4SLinus Torvalds 55421da177e4SLinus Torvalds #endif /* IPV6 */ 55431da177e4SLinus Torvalds 55441da177e4SLinus Torvalds static int __init selinux_nf_ip_init(void) 55451da177e4SLinus Torvalds { 55461da177e4SLinus Torvalds int err = 0; 5547effad8dfSPaul Moore u32 iter; 55481da177e4SLinus Torvalds 55491da177e4SLinus Torvalds if (!selinux_enabled) 55501da177e4SLinus Torvalds goto out; 55511da177e4SLinus Torvalds 5552fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Registering netfilter hooks\n"); 55531da177e4SLinus Torvalds 5554effad8dfSPaul Moore for (iter = 0; iter < ARRAY_SIZE(selinux_ipv4_ops); iter++) { 5555effad8dfSPaul Moore err = nf_register_hook(&selinux_ipv4_ops[iter]); 55561da177e4SLinus Torvalds if (err) 5557effad8dfSPaul Moore panic("SELinux: nf_register_hook for IPv4: error %d\n", 5558effad8dfSPaul Moore err); 5559effad8dfSPaul Moore } 55601da177e4SLinus Torvalds 55611da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 5562effad8dfSPaul Moore for (iter = 0; iter < ARRAY_SIZE(selinux_ipv6_ops); iter++) { 5563effad8dfSPaul Moore err = nf_register_hook(&selinux_ipv6_ops[iter]); 55641da177e4SLinus Torvalds if (err) 5565effad8dfSPaul Moore panic("SELinux: nf_register_hook for IPv6: error %d\n", 5566effad8dfSPaul Moore err); 5567effad8dfSPaul Moore } 55681da177e4SLinus Torvalds #endif /* IPV6 */ 5569d28d1e08STrent Jaeger 55701da177e4SLinus Torvalds out: 55711da177e4SLinus Torvalds return err; 55721da177e4SLinus Torvalds } 55731da177e4SLinus Torvalds 55741da177e4SLinus Torvalds __initcall(selinux_nf_ip_init); 55751da177e4SLinus Torvalds 55761da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 55771da177e4SLinus Torvalds static void selinux_nf_ip_exit(void) 55781da177e4SLinus Torvalds { 5579effad8dfSPaul Moore u32 iter; 5580effad8dfSPaul Moore 5581fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Unregistering netfilter hooks\n"); 55821da177e4SLinus Torvalds 5583effad8dfSPaul Moore for (iter = 0; iter < ARRAY_SIZE(selinux_ipv4_ops); iter++) 5584effad8dfSPaul Moore nf_unregister_hook(&selinux_ipv4_ops[iter]); 55851da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 5586effad8dfSPaul Moore for (iter = 0; iter < ARRAY_SIZE(selinux_ipv6_ops); iter++) 5587effad8dfSPaul Moore nf_unregister_hook(&selinux_ipv6_ops[iter]); 55881da177e4SLinus Torvalds #endif /* IPV6 */ 55891da177e4SLinus Torvalds } 55901da177e4SLinus Torvalds #endif 55911da177e4SLinus Torvalds 5592c2b507fdSStephen Smalley #else /* CONFIG_NETFILTER */ 55931da177e4SLinus Torvalds 55941da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 55951da177e4SLinus Torvalds #define selinux_nf_ip_exit() 55961da177e4SLinus Torvalds #endif 55971da177e4SLinus Torvalds 5598c2b507fdSStephen Smalley #endif /* CONFIG_NETFILTER */ 55991da177e4SLinus Torvalds 56001da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 56011da177e4SLinus Torvalds int selinux_disable(void) 56021da177e4SLinus Torvalds { 56031da177e4SLinus Torvalds extern void exit_sel_fs(void); 56041da177e4SLinus Torvalds static int selinux_disabled = 0; 56051da177e4SLinus Torvalds 56061da177e4SLinus Torvalds if (ss_initialized) { 56071da177e4SLinus Torvalds /* Not permitted after initial policy load. */ 56081da177e4SLinus Torvalds return -EINVAL; 56091da177e4SLinus Torvalds } 56101da177e4SLinus Torvalds 56111da177e4SLinus Torvalds if (selinux_disabled) { 56121da177e4SLinus Torvalds /* Only do this once. */ 56131da177e4SLinus Torvalds return -EINVAL; 56141da177e4SLinus Torvalds } 56151da177e4SLinus Torvalds 56161da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Disabled at runtime.\n"); 56171da177e4SLinus Torvalds 56181da177e4SLinus Torvalds selinux_disabled = 1; 561930d55280SStephen Smalley selinux_enabled = 0; 56201da177e4SLinus Torvalds 56211da177e4SLinus Torvalds /* Reset security_ops to the secondary module, dummy or capability. */ 56221da177e4SLinus Torvalds security_ops = secondary_ops; 56231da177e4SLinus Torvalds 56241da177e4SLinus Torvalds /* Unregister netfilter hooks. */ 56251da177e4SLinus Torvalds selinux_nf_ip_exit(); 56261da177e4SLinus Torvalds 56271da177e4SLinus Torvalds /* Unregister selinuxfs. */ 56281da177e4SLinus Torvalds exit_sel_fs(); 56291da177e4SLinus Torvalds 56301da177e4SLinus Torvalds return 0; 56311da177e4SLinus Torvalds } 56321da177e4SLinus Torvalds #endif 56331da177e4SLinus Torvalds 56341da177e4SLinus Torvalds 5635