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 183a02fe132SJosef 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; 1146*869ab514SStephen Smalley context = kmalloc(len, GFP_NOFS); 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; 1164*869ab514SStephen Smalley context = kmalloc(len, GFP_NOFS); 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, 1188*869ab514SStephen Smalley sbsec->def_sid, 1189*869ab514SStephen Smalley GFP_NOFS); 11901da177e4SLinus Torvalds if (rc) { 11911da177e4SLinus Torvalds printk(KERN_WARNING "%s: context_to_sid(%s) " 11921da177e4SLinus Torvalds "returned %d for dev=%s ino=%ld\n", 11931da177e4SLinus Torvalds __FUNCTION__, context, -rc, 11941da177e4SLinus Torvalds inode->i_sb->s_id, inode->i_ino); 11951da177e4SLinus Torvalds kfree(context); 11961da177e4SLinus Torvalds /* Leave with the unlabeled SID */ 11971da177e4SLinus Torvalds rc = 0; 11981da177e4SLinus Torvalds break; 11991da177e4SLinus Torvalds } 12001da177e4SLinus Torvalds } 12011da177e4SLinus Torvalds kfree(context); 12021da177e4SLinus Torvalds isec->sid = sid; 12031da177e4SLinus Torvalds break; 12041da177e4SLinus Torvalds case SECURITY_FS_USE_TASK: 12051da177e4SLinus Torvalds isec->sid = isec->task_sid; 12061da177e4SLinus Torvalds break; 12071da177e4SLinus Torvalds case SECURITY_FS_USE_TRANS: 12081da177e4SLinus Torvalds /* Default to the fs SID. */ 12091da177e4SLinus Torvalds isec->sid = sbsec->sid; 12101da177e4SLinus Torvalds 12111da177e4SLinus Torvalds /* Try to obtain a transition SID. */ 12121da177e4SLinus Torvalds isec->sclass = inode_mode_to_security_class(inode->i_mode); 12131da177e4SLinus Torvalds rc = security_transition_sid(isec->task_sid, 12141da177e4SLinus Torvalds sbsec->sid, 12151da177e4SLinus Torvalds isec->sclass, 12161da177e4SLinus Torvalds &sid); 12171da177e4SLinus Torvalds if (rc) 121823970741SEric Paris goto out_unlock; 12191da177e4SLinus Torvalds isec->sid = sid; 12201da177e4SLinus Torvalds break; 1221c312feb2SEric Paris case SECURITY_FS_USE_MNTPOINT: 1222c312feb2SEric Paris isec->sid = sbsec->mntpoint_sid; 1223c312feb2SEric Paris break; 12241da177e4SLinus Torvalds default: 1225c312feb2SEric Paris /* Default to the fs superblock SID. */ 12261da177e4SLinus Torvalds isec->sid = sbsec->sid; 12271da177e4SLinus Torvalds 12281da177e4SLinus Torvalds if (sbsec->proc) { 12291da177e4SLinus Torvalds struct proc_inode *proci = PROC_I(inode); 12301da177e4SLinus Torvalds if (proci->pde) { 12311da177e4SLinus Torvalds isec->sclass = inode_mode_to_security_class(inode->i_mode); 12321da177e4SLinus Torvalds rc = selinux_proc_get_sid(proci->pde, 12331da177e4SLinus Torvalds isec->sclass, 12341da177e4SLinus Torvalds &sid); 12351da177e4SLinus Torvalds if (rc) 123623970741SEric Paris goto out_unlock; 12371da177e4SLinus Torvalds isec->sid = sid; 12381da177e4SLinus Torvalds } 12391da177e4SLinus Torvalds } 12401da177e4SLinus Torvalds break; 12411da177e4SLinus Torvalds } 12421da177e4SLinus Torvalds 12431da177e4SLinus Torvalds isec->initialized = 1; 12441da177e4SLinus Torvalds 124523970741SEric Paris out_unlock: 124623970741SEric Paris mutex_unlock(&isec->lock); 12471da177e4SLinus Torvalds out: 12481da177e4SLinus Torvalds if (isec->sclass == SECCLASS_FILE) 12491da177e4SLinus Torvalds isec->sclass = inode_mode_to_security_class(inode->i_mode); 12501da177e4SLinus Torvalds return rc; 12511da177e4SLinus Torvalds } 12521da177e4SLinus Torvalds 12531da177e4SLinus Torvalds /* Convert a Linux signal to an access vector. */ 12541da177e4SLinus Torvalds static inline u32 signal_to_av(int sig) 12551da177e4SLinus Torvalds { 12561da177e4SLinus Torvalds u32 perm = 0; 12571da177e4SLinus Torvalds 12581da177e4SLinus Torvalds switch (sig) { 12591da177e4SLinus Torvalds case SIGCHLD: 12601da177e4SLinus Torvalds /* Commonly granted from child to parent. */ 12611da177e4SLinus Torvalds perm = PROCESS__SIGCHLD; 12621da177e4SLinus Torvalds break; 12631da177e4SLinus Torvalds case SIGKILL: 12641da177e4SLinus Torvalds /* Cannot be caught or ignored */ 12651da177e4SLinus Torvalds perm = PROCESS__SIGKILL; 12661da177e4SLinus Torvalds break; 12671da177e4SLinus Torvalds case SIGSTOP: 12681da177e4SLinus Torvalds /* Cannot be caught or ignored */ 12691da177e4SLinus Torvalds perm = PROCESS__SIGSTOP; 12701da177e4SLinus Torvalds break; 12711da177e4SLinus Torvalds default: 12721da177e4SLinus Torvalds /* All other signals. */ 12731da177e4SLinus Torvalds perm = PROCESS__SIGNAL; 12741da177e4SLinus Torvalds break; 12751da177e4SLinus Torvalds } 12761da177e4SLinus Torvalds 12771da177e4SLinus Torvalds return perm; 12781da177e4SLinus Torvalds } 12791da177e4SLinus Torvalds 12801da177e4SLinus Torvalds /* Check permission betweeen a pair of tasks, e.g. signal checks, 12811da177e4SLinus Torvalds fork check, ptrace check, etc. */ 12821da177e4SLinus Torvalds static int task_has_perm(struct task_struct *tsk1, 12831da177e4SLinus Torvalds struct task_struct *tsk2, 12841da177e4SLinus Torvalds u32 perms) 12851da177e4SLinus Torvalds { 12861da177e4SLinus Torvalds struct task_security_struct *tsec1, *tsec2; 12871da177e4SLinus Torvalds 12881da177e4SLinus Torvalds tsec1 = tsk1->security; 12891da177e4SLinus Torvalds tsec2 = tsk2->security; 12901da177e4SLinus Torvalds return avc_has_perm(tsec1->sid, tsec2->sid, 12911da177e4SLinus Torvalds SECCLASS_PROCESS, perms, NULL); 12921da177e4SLinus Torvalds } 12931da177e4SLinus Torvalds 1294b68e418cSStephen Smalley #if CAP_LAST_CAP > 63 1295b68e418cSStephen Smalley #error Fix SELinux to handle capabilities > 63. 1296b68e418cSStephen Smalley #endif 1297b68e418cSStephen Smalley 12981da177e4SLinus Torvalds /* Check whether a task is allowed to use a capability. */ 12991da177e4SLinus Torvalds static int task_has_capability(struct task_struct *tsk, 13001da177e4SLinus Torvalds int cap) 13011da177e4SLinus Torvalds { 13021da177e4SLinus Torvalds struct task_security_struct *tsec; 13031da177e4SLinus Torvalds struct avc_audit_data ad; 1304b68e418cSStephen Smalley u16 sclass; 1305b68e418cSStephen Smalley u32 av = CAP_TO_MASK(cap); 13061da177e4SLinus Torvalds 13071da177e4SLinus Torvalds tsec = tsk->security; 13081da177e4SLinus Torvalds 13091da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,CAP); 13101da177e4SLinus Torvalds ad.tsk = tsk; 13111da177e4SLinus Torvalds ad.u.cap = cap; 13121da177e4SLinus Torvalds 1313b68e418cSStephen Smalley switch (CAP_TO_INDEX(cap)) { 1314b68e418cSStephen Smalley case 0: 1315b68e418cSStephen Smalley sclass = SECCLASS_CAPABILITY; 1316b68e418cSStephen Smalley break; 1317b68e418cSStephen Smalley case 1: 1318b68e418cSStephen Smalley sclass = SECCLASS_CAPABILITY2; 1319b68e418cSStephen Smalley break; 1320b68e418cSStephen Smalley default: 1321b68e418cSStephen Smalley printk(KERN_ERR 1322b68e418cSStephen Smalley "SELinux: out of range capability %d\n", cap); 1323b68e418cSStephen Smalley BUG(); 1324b68e418cSStephen Smalley } 1325b68e418cSStephen Smalley return avc_has_perm(tsec->sid, tsec->sid, sclass, av, &ad); 13261da177e4SLinus Torvalds } 13271da177e4SLinus Torvalds 13281da177e4SLinus Torvalds /* Check whether a task is allowed to use a system operation. */ 13291da177e4SLinus Torvalds static int task_has_system(struct task_struct *tsk, 13301da177e4SLinus Torvalds u32 perms) 13311da177e4SLinus Torvalds { 13321da177e4SLinus Torvalds struct task_security_struct *tsec; 13331da177e4SLinus Torvalds 13341da177e4SLinus Torvalds tsec = tsk->security; 13351da177e4SLinus Torvalds 13361da177e4SLinus Torvalds return avc_has_perm(tsec->sid, SECINITSID_KERNEL, 13371da177e4SLinus Torvalds SECCLASS_SYSTEM, perms, NULL); 13381da177e4SLinus Torvalds } 13391da177e4SLinus Torvalds 13401da177e4SLinus Torvalds /* Check whether a task has a particular permission to an inode. 13411da177e4SLinus Torvalds The 'adp' parameter is optional and allows other audit 13421da177e4SLinus Torvalds data to be passed (e.g. the dentry). */ 13431da177e4SLinus Torvalds static int inode_has_perm(struct task_struct *tsk, 13441da177e4SLinus Torvalds struct inode *inode, 13451da177e4SLinus Torvalds u32 perms, 13461da177e4SLinus Torvalds struct avc_audit_data *adp) 13471da177e4SLinus Torvalds { 13481da177e4SLinus Torvalds struct task_security_struct *tsec; 13491da177e4SLinus Torvalds struct inode_security_struct *isec; 13501da177e4SLinus Torvalds struct avc_audit_data ad; 13511da177e4SLinus Torvalds 1352bbaca6c2SStephen Smalley if (unlikely (IS_PRIVATE (inode))) 1353bbaca6c2SStephen Smalley return 0; 1354bbaca6c2SStephen Smalley 13551da177e4SLinus Torvalds tsec = tsk->security; 13561da177e4SLinus Torvalds isec = inode->i_security; 13571da177e4SLinus Torvalds 13581da177e4SLinus Torvalds if (!adp) { 13591da177e4SLinus Torvalds adp = &ad; 13601da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 13611da177e4SLinus Torvalds ad.u.fs.inode = inode; 13621da177e4SLinus Torvalds } 13631da177e4SLinus Torvalds 13641da177e4SLinus Torvalds return avc_has_perm(tsec->sid, isec->sid, isec->sclass, perms, adp); 13651da177e4SLinus Torvalds } 13661da177e4SLinus Torvalds 13671da177e4SLinus Torvalds /* Same as inode_has_perm, but pass explicit audit data containing 13681da177e4SLinus Torvalds the dentry to help the auditing code to more easily generate the 13691da177e4SLinus Torvalds pathname if needed. */ 13701da177e4SLinus Torvalds static inline int dentry_has_perm(struct task_struct *tsk, 13711da177e4SLinus Torvalds struct vfsmount *mnt, 13721da177e4SLinus Torvalds struct dentry *dentry, 13731da177e4SLinus Torvalds u32 av) 13741da177e4SLinus Torvalds { 13751da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 13761da177e4SLinus Torvalds struct avc_audit_data ad; 13771da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,FS); 137844707fdfSJan Blunck ad.u.fs.path.mnt = mnt; 137944707fdfSJan Blunck ad.u.fs.path.dentry = dentry; 13801da177e4SLinus Torvalds return inode_has_perm(tsk, inode, av, &ad); 13811da177e4SLinus Torvalds } 13821da177e4SLinus Torvalds 13831da177e4SLinus Torvalds /* Check whether a task can use an open file descriptor to 13841da177e4SLinus Torvalds access an inode in a given way. Check access to the 13851da177e4SLinus Torvalds descriptor itself, and then use dentry_has_perm to 13861da177e4SLinus Torvalds check a particular permission to the file. 13871da177e4SLinus Torvalds Access to the descriptor is implicitly granted if it 13881da177e4SLinus Torvalds has the same SID as the process. If av is zero, then 13891da177e4SLinus Torvalds access to the file is not checked, e.g. for cases 13901da177e4SLinus Torvalds where only the descriptor is affected like seek. */ 1391858119e1SArjan van de Ven static int file_has_perm(struct task_struct *tsk, 13921da177e4SLinus Torvalds struct file *file, 13931da177e4SLinus Torvalds u32 av) 13941da177e4SLinus Torvalds { 13951da177e4SLinus Torvalds struct task_security_struct *tsec = tsk->security; 13961da177e4SLinus Torvalds struct file_security_struct *fsec = file->f_security; 139744707fdfSJan Blunck struct inode *inode = file->f_path.dentry->d_inode; 13981da177e4SLinus Torvalds struct avc_audit_data ad; 13991da177e4SLinus Torvalds int rc; 14001da177e4SLinus Torvalds 14011da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 140244707fdfSJan Blunck ad.u.fs.path = file->f_path; 14031da177e4SLinus Torvalds 14041da177e4SLinus Torvalds if (tsec->sid != fsec->sid) { 14051da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, fsec->sid, 14061da177e4SLinus Torvalds SECCLASS_FD, 14071da177e4SLinus Torvalds FD__USE, 14081da177e4SLinus Torvalds &ad); 14091da177e4SLinus Torvalds if (rc) 14101da177e4SLinus Torvalds return rc; 14111da177e4SLinus Torvalds } 14121da177e4SLinus Torvalds 14131da177e4SLinus Torvalds /* av is zero if only checking access to the descriptor. */ 14141da177e4SLinus Torvalds if (av) 14151da177e4SLinus Torvalds return inode_has_perm(tsk, inode, av, &ad); 14161da177e4SLinus Torvalds 14171da177e4SLinus Torvalds return 0; 14181da177e4SLinus Torvalds } 14191da177e4SLinus Torvalds 14201da177e4SLinus Torvalds /* Check whether a task can create a file. */ 14211da177e4SLinus Torvalds static int may_create(struct inode *dir, 14221da177e4SLinus Torvalds struct dentry *dentry, 14231da177e4SLinus Torvalds u16 tclass) 14241da177e4SLinus Torvalds { 14251da177e4SLinus Torvalds struct task_security_struct *tsec; 14261da177e4SLinus Torvalds struct inode_security_struct *dsec; 14271da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 14281da177e4SLinus Torvalds u32 newsid; 14291da177e4SLinus Torvalds struct avc_audit_data ad; 14301da177e4SLinus Torvalds int rc; 14311da177e4SLinus Torvalds 14321da177e4SLinus Torvalds tsec = current->security; 14331da177e4SLinus Torvalds dsec = dir->i_security; 14341da177e4SLinus Torvalds sbsec = dir->i_sb->s_security; 14351da177e4SLinus Torvalds 14361da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 143744707fdfSJan Blunck ad.u.fs.path.dentry = dentry; 14381da177e4SLinus Torvalds 14391da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, dsec->sid, SECCLASS_DIR, 14401da177e4SLinus Torvalds DIR__ADD_NAME | DIR__SEARCH, 14411da177e4SLinus Torvalds &ad); 14421da177e4SLinus Torvalds if (rc) 14431da177e4SLinus Torvalds return rc; 14441da177e4SLinus Torvalds 14451da177e4SLinus Torvalds if (tsec->create_sid && sbsec->behavior != SECURITY_FS_USE_MNTPOINT) { 14461da177e4SLinus Torvalds newsid = tsec->create_sid; 14471da177e4SLinus Torvalds } else { 14481da177e4SLinus Torvalds rc = security_transition_sid(tsec->sid, dsec->sid, tclass, 14491da177e4SLinus Torvalds &newsid); 14501da177e4SLinus Torvalds if (rc) 14511da177e4SLinus Torvalds return rc; 14521da177e4SLinus Torvalds } 14531da177e4SLinus Torvalds 14541da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, newsid, tclass, FILE__CREATE, &ad); 14551da177e4SLinus Torvalds if (rc) 14561da177e4SLinus Torvalds return rc; 14571da177e4SLinus Torvalds 14581da177e4SLinus Torvalds return avc_has_perm(newsid, sbsec->sid, 14591da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 14601da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, &ad); 14611da177e4SLinus Torvalds } 14621da177e4SLinus Torvalds 14634eb582cfSMichael LeMay /* Check whether a task can create a key. */ 14644eb582cfSMichael LeMay static int may_create_key(u32 ksid, 14654eb582cfSMichael LeMay struct task_struct *ctx) 14664eb582cfSMichael LeMay { 14674eb582cfSMichael LeMay struct task_security_struct *tsec; 14684eb582cfSMichael LeMay 14694eb582cfSMichael LeMay tsec = ctx->security; 14704eb582cfSMichael LeMay 14714eb582cfSMichael LeMay return avc_has_perm(tsec->sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL); 14724eb582cfSMichael LeMay } 14734eb582cfSMichael LeMay 14741da177e4SLinus Torvalds #define MAY_LINK 0 14751da177e4SLinus Torvalds #define MAY_UNLINK 1 14761da177e4SLinus Torvalds #define MAY_RMDIR 2 14771da177e4SLinus Torvalds 14781da177e4SLinus Torvalds /* Check whether a task can link, unlink, or rmdir a file/directory. */ 14791da177e4SLinus Torvalds static int may_link(struct inode *dir, 14801da177e4SLinus Torvalds struct dentry *dentry, 14811da177e4SLinus Torvalds int kind) 14821da177e4SLinus Torvalds 14831da177e4SLinus Torvalds { 14841da177e4SLinus Torvalds struct task_security_struct *tsec; 14851da177e4SLinus Torvalds struct inode_security_struct *dsec, *isec; 14861da177e4SLinus Torvalds struct avc_audit_data ad; 14871da177e4SLinus Torvalds u32 av; 14881da177e4SLinus Torvalds int rc; 14891da177e4SLinus Torvalds 14901da177e4SLinus Torvalds tsec = current->security; 14911da177e4SLinus Torvalds dsec = dir->i_security; 14921da177e4SLinus Torvalds isec = dentry->d_inode->i_security; 14931da177e4SLinus Torvalds 14941da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 149544707fdfSJan Blunck ad.u.fs.path.dentry = dentry; 14961da177e4SLinus Torvalds 14971da177e4SLinus Torvalds av = DIR__SEARCH; 14981da177e4SLinus Torvalds av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME); 14991da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, dsec->sid, SECCLASS_DIR, av, &ad); 15001da177e4SLinus Torvalds if (rc) 15011da177e4SLinus Torvalds return rc; 15021da177e4SLinus Torvalds 15031da177e4SLinus Torvalds switch (kind) { 15041da177e4SLinus Torvalds case MAY_LINK: 15051da177e4SLinus Torvalds av = FILE__LINK; 15061da177e4SLinus Torvalds break; 15071da177e4SLinus Torvalds case MAY_UNLINK: 15081da177e4SLinus Torvalds av = FILE__UNLINK; 15091da177e4SLinus Torvalds break; 15101da177e4SLinus Torvalds case MAY_RMDIR: 15111da177e4SLinus Torvalds av = DIR__RMDIR; 15121da177e4SLinus Torvalds break; 15131da177e4SLinus Torvalds default: 15141da177e4SLinus Torvalds printk(KERN_WARNING "may_link: unrecognized kind %d\n", kind); 15151da177e4SLinus Torvalds return 0; 15161da177e4SLinus Torvalds } 15171da177e4SLinus Torvalds 15181da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, isec->sclass, av, &ad); 15191da177e4SLinus Torvalds return rc; 15201da177e4SLinus Torvalds } 15211da177e4SLinus Torvalds 15221da177e4SLinus Torvalds static inline int may_rename(struct inode *old_dir, 15231da177e4SLinus Torvalds struct dentry *old_dentry, 15241da177e4SLinus Torvalds struct inode *new_dir, 15251da177e4SLinus Torvalds struct dentry *new_dentry) 15261da177e4SLinus Torvalds { 15271da177e4SLinus Torvalds struct task_security_struct *tsec; 15281da177e4SLinus Torvalds struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec; 15291da177e4SLinus Torvalds struct avc_audit_data ad; 15301da177e4SLinus Torvalds u32 av; 15311da177e4SLinus Torvalds int old_is_dir, new_is_dir; 15321da177e4SLinus Torvalds int rc; 15331da177e4SLinus Torvalds 15341da177e4SLinus Torvalds tsec = current->security; 15351da177e4SLinus Torvalds old_dsec = old_dir->i_security; 15361da177e4SLinus Torvalds old_isec = old_dentry->d_inode->i_security; 15371da177e4SLinus Torvalds old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 15381da177e4SLinus Torvalds new_dsec = new_dir->i_security; 15391da177e4SLinus Torvalds 15401da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 15411da177e4SLinus Torvalds 154244707fdfSJan Blunck ad.u.fs.path.dentry = old_dentry; 15431da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, old_dsec->sid, SECCLASS_DIR, 15441da177e4SLinus Torvalds DIR__REMOVE_NAME | DIR__SEARCH, &ad); 15451da177e4SLinus Torvalds if (rc) 15461da177e4SLinus Torvalds return rc; 15471da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, old_isec->sid, 15481da177e4SLinus Torvalds old_isec->sclass, FILE__RENAME, &ad); 15491da177e4SLinus Torvalds if (rc) 15501da177e4SLinus Torvalds return rc; 15511da177e4SLinus Torvalds if (old_is_dir && new_dir != old_dir) { 15521da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, old_isec->sid, 15531da177e4SLinus Torvalds old_isec->sclass, DIR__REPARENT, &ad); 15541da177e4SLinus Torvalds if (rc) 15551da177e4SLinus Torvalds return rc; 15561da177e4SLinus Torvalds } 15571da177e4SLinus Torvalds 155844707fdfSJan Blunck ad.u.fs.path.dentry = new_dentry; 15591da177e4SLinus Torvalds av = DIR__ADD_NAME | DIR__SEARCH; 15601da177e4SLinus Torvalds if (new_dentry->d_inode) 15611da177e4SLinus Torvalds av |= DIR__REMOVE_NAME; 15621da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, new_dsec->sid, SECCLASS_DIR, av, &ad); 15631da177e4SLinus Torvalds if (rc) 15641da177e4SLinus Torvalds return rc; 15651da177e4SLinus Torvalds if (new_dentry->d_inode) { 15661da177e4SLinus Torvalds new_isec = new_dentry->d_inode->i_security; 15671da177e4SLinus Torvalds new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode); 15681da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, new_isec->sid, 15691da177e4SLinus Torvalds new_isec->sclass, 15701da177e4SLinus Torvalds (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad); 15711da177e4SLinus Torvalds if (rc) 15721da177e4SLinus Torvalds return rc; 15731da177e4SLinus Torvalds } 15741da177e4SLinus Torvalds 15751da177e4SLinus Torvalds return 0; 15761da177e4SLinus Torvalds } 15771da177e4SLinus Torvalds 15781da177e4SLinus Torvalds /* Check whether a task can perform a filesystem operation. */ 15791da177e4SLinus Torvalds static int superblock_has_perm(struct task_struct *tsk, 15801da177e4SLinus Torvalds struct super_block *sb, 15811da177e4SLinus Torvalds u32 perms, 15821da177e4SLinus Torvalds struct avc_audit_data *ad) 15831da177e4SLinus Torvalds { 15841da177e4SLinus Torvalds struct task_security_struct *tsec; 15851da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 15861da177e4SLinus Torvalds 15871da177e4SLinus Torvalds tsec = tsk->security; 15881da177e4SLinus Torvalds sbsec = sb->s_security; 15891da177e4SLinus Torvalds return avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 15901da177e4SLinus Torvalds perms, ad); 15911da177e4SLinus Torvalds } 15921da177e4SLinus Torvalds 15931da177e4SLinus Torvalds /* Convert a Linux mode and permission mask to an access vector. */ 15941da177e4SLinus Torvalds static inline u32 file_mask_to_av(int mode, int mask) 15951da177e4SLinus Torvalds { 15961da177e4SLinus Torvalds u32 av = 0; 15971da177e4SLinus Torvalds 15981da177e4SLinus Torvalds if ((mode & S_IFMT) != S_IFDIR) { 15991da177e4SLinus Torvalds if (mask & MAY_EXEC) 16001da177e4SLinus Torvalds av |= FILE__EXECUTE; 16011da177e4SLinus Torvalds if (mask & MAY_READ) 16021da177e4SLinus Torvalds av |= FILE__READ; 16031da177e4SLinus Torvalds 16041da177e4SLinus Torvalds if (mask & MAY_APPEND) 16051da177e4SLinus Torvalds av |= FILE__APPEND; 16061da177e4SLinus Torvalds else if (mask & MAY_WRITE) 16071da177e4SLinus Torvalds av |= FILE__WRITE; 16081da177e4SLinus Torvalds 16091da177e4SLinus Torvalds } else { 16101da177e4SLinus Torvalds if (mask & MAY_EXEC) 16111da177e4SLinus Torvalds av |= DIR__SEARCH; 16121da177e4SLinus Torvalds if (mask & MAY_WRITE) 16131da177e4SLinus Torvalds av |= DIR__WRITE; 16141da177e4SLinus Torvalds if (mask & MAY_READ) 16151da177e4SLinus Torvalds av |= DIR__READ; 16161da177e4SLinus Torvalds } 16171da177e4SLinus Torvalds 16181da177e4SLinus Torvalds return av; 16191da177e4SLinus Torvalds } 16201da177e4SLinus Torvalds 16211da177e4SLinus Torvalds /* Convert a Linux file to an access vector. */ 16221da177e4SLinus Torvalds static inline u32 file_to_av(struct file *file) 16231da177e4SLinus Torvalds { 16241da177e4SLinus Torvalds u32 av = 0; 16251da177e4SLinus Torvalds 16261da177e4SLinus Torvalds if (file->f_mode & FMODE_READ) 16271da177e4SLinus Torvalds av |= FILE__READ; 16281da177e4SLinus Torvalds if (file->f_mode & FMODE_WRITE) { 16291da177e4SLinus Torvalds if (file->f_flags & O_APPEND) 16301da177e4SLinus Torvalds av |= FILE__APPEND; 16311da177e4SLinus Torvalds else 16321da177e4SLinus Torvalds av |= FILE__WRITE; 16331da177e4SLinus Torvalds } 16340794c66dSStephen Smalley if (!av) { 16350794c66dSStephen Smalley /* 16360794c66dSStephen Smalley * Special file opened with flags 3 for ioctl-only use. 16370794c66dSStephen Smalley */ 16380794c66dSStephen Smalley av = FILE__IOCTL; 16390794c66dSStephen Smalley } 16401da177e4SLinus Torvalds 16411da177e4SLinus Torvalds return av; 16421da177e4SLinus Torvalds } 16431da177e4SLinus Torvalds 16441da177e4SLinus Torvalds /* Hook functions begin here. */ 16451da177e4SLinus Torvalds 16461da177e4SLinus Torvalds static int selinux_ptrace(struct task_struct *parent, struct task_struct *child) 16471da177e4SLinus Torvalds { 16481da177e4SLinus Torvalds struct task_security_struct *psec = parent->security; 16491da177e4SLinus Torvalds struct task_security_struct *csec = child->security; 16501da177e4SLinus Torvalds int rc; 16511da177e4SLinus Torvalds 16521da177e4SLinus Torvalds rc = secondary_ops->ptrace(parent,child); 16531da177e4SLinus Torvalds if (rc) 16541da177e4SLinus Torvalds return rc; 16551da177e4SLinus Torvalds 16561da177e4SLinus Torvalds rc = task_has_perm(parent, child, PROCESS__PTRACE); 16571da177e4SLinus Torvalds /* Save the SID of the tracing process for later use in apply_creds. */ 1658341c2d80SStephen Smalley if (!(child->ptrace & PT_PTRACED) && !rc) 16591da177e4SLinus Torvalds csec->ptrace_sid = psec->sid; 16601da177e4SLinus Torvalds return rc; 16611da177e4SLinus Torvalds } 16621da177e4SLinus Torvalds 16631da177e4SLinus Torvalds static int selinux_capget(struct task_struct *target, kernel_cap_t *effective, 16641da177e4SLinus Torvalds kernel_cap_t *inheritable, kernel_cap_t *permitted) 16651da177e4SLinus Torvalds { 16661da177e4SLinus Torvalds int error; 16671da177e4SLinus Torvalds 16681da177e4SLinus Torvalds error = task_has_perm(current, target, PROCESS__GETCAP); 16691da177e4SLinus Torvalds if (error) 16701da177e4SLinus Torvalds return error; 16711da177e4SLinus Torvalds 16721da177e4SLinus Torvalds return secondary_ops->capget(target, effective, inheritable, permitted); 16731da177e4SLinus Torvalds } 16741da177e4SLinus Torvalds 16751da177e4SLinus Torvalds static int selinux_capset_check(struct task_struct *target, kernel_cap_t *effective, 16761da177e4SLinus Torvalds kernel_cap_t *inheritable, kernel_cap_t *permitted) 16771da177e4SLinus Torvalds { 16781da177e4SLinus Torvalds int error; 16791da177e4SLinus Torvalds 16801da177e4SLinus Torvalds error = secondary_ops->capset_check(target, effective, inheritable, permitted); 16811da177e4SLinus Torvalds if (error) 16821da177e4SLinus Torvalds return error; 16831da177e4SLinus Torvalds 16841da177e4SLinus Torvalds return task_has_perm(current, target, PROCESS__SETCAP); 16851da177e4SLinus Torvalds } 16861da177e4SLinus Torvalds 16871da177e4SLinus Torvalds static void selinux_capset_set(struct task_struct *target, kernel_cap_t *effective, 16881da177e4SLinus Torvalds kernel_cap_t *inheritable, kernel_cap_t *permitted) 16891da177e4SLinus Torvalds { 16901da177e4SLinus Torvalds secondary_ops->capset_set(target, effective, inheritable, permitted); 16911da177e4SLinus Torvalds } 16921da177e4SLinus Torvalds 16931da177e4SLinus Torvalds static int selinux_capable(struct task_struct *tsk, int cap) 16941da177e4SLinus Torvalds { 16951da177e4SLinus Torvalds int rc; 16961da177e4SLinus Torvalds 16971da177e4SLinus Torvalds rc = secondary_ops->capable(tsk, cap); 16981da177e4SLinus Torvalds if (rc) 16991da177e4SLinus Torvalds return rc; 17001da177e4SLinus Torvalds 17011da177e4SLinus Torvalds return task_has_capability(tsk,cap); 17021da177e4SLinus Torvalds } 17031da177e4SLinus Torvalds 17043fbfa981SEric W. Biederman static int selinux_sysctl_get_sid(ctl_table *table, u16 tclass, u32 *sid) 17053fbfa981SEric W. Biederman { 17063fbfa981SEric W. Biederman int buflen, rc; 17073fbfa981SEric W. Biederman char *buffer, *path, *end; 17083fbfa981SEric W. Biederman 17093fbfa981SEric W. Biederman rc = -ENOMEM; 17103fbfa981SEric W. Biederman buffer = (char*)__get_free_page(GFP_KERNEL); 17113fbfa981SEric W. Biederman if (!buffer) 17123fbfa981SEric W. Biederman goto out; 17133fbfa981SEric W. Biederman 17143fbfa981SEric W. Biederman buflen = PAGE_SIZE; 17153fbfa981SEric W. Biederman end = buffer+buflen; 17163fbfa981SEric W. Biederman *--end = '\0'; 17173fbfa981SEric W. Biederman buflen--; 17183fbfa981SEric W. Biederman path = end-1; 17193fbfa981SEric W. Biederman *path = '/'; 17203fbfa981SEric W. Biederman while (table) { 17213fbfa981SEric W. Biederman const char *name = table->procname; 17223fbfa981SEric W. Biederman size_t namelen = strlen(name); 17233fbfa981SEric W. Biederman buflen -= namelen + 1; 17243fbfa981SEric W. Biederman if (buflen < 0) 17253fbfa981SEric W. Biederman goto out_free; 17263fbfa981SEric W. Biederman end -= namelen; 17273fbfa981SEric W. Biederman memcpy(end, name, namelen); 17283fbfa981SEric W. Biederman *--end = '/'; 17293fbfa981SEric W. Biederman path = end; 17303fbfa981SEric W. Biederman table = table->parent; 17313fbfa981SEric W. Biederman } 1732b599fdfdSEric W. Biederman buflen -= 4; 1733b599fdfdSEric W. Biederman if (buflen < 0) 1734b599fdfdSEric W. Biederman goto out_free; 1735b599fdfdSEric W. Biederman end -= 4; 1736b599fdfdSEric W. Biederman memcpy(end, "/sys", 4); 1737b599fdfdSEric W. Biederman path = end; 17383fbfa981SEric W. Biederman rc = security_genfs_sid("proc", path, tclass, sid); 17393fbfa981SEric W. Biederman out_free: 17403fbfa981SEric W. Biederman free_page((unsigned long)buffer); 17413fbfa981SEric W. Biederman out: 17423fbfa981SEric W. Biederman return rc; 17433fbfa981SEric W. Biederman } 17443fbfa981SEric W. Biederman 17451da177e4SLinus Torvalds static int selinux_sysctl(ctl_table *table, int op) 17461da177e4SLinus Torvalds { 17471da177e4SLinus Torvalds int error = 0; 17481da177e4SLinus Torvalds u32 av; 17491da177e4SLinus Torvalds struct task_security_struct *tsec; 17501da177e4SLinus Torvalds u32 tsid; 17511da177e4SLinus Torvalds int rc; 17521da177e4SLinus Torvalds 17531da177e4SLinus Torvalds rc = secondary_ops->sysctl(table, op); 17541da177e4SLinus Torvalds if (rc) 17551da177e4SLinus Torvalds return rc; 17561da177e4SLinus Torvalds 17571da177e4SLinus Torvalds tsec = current->security; 17581da177e4SLinus Torvalds 17593fbfa981SEric W. Biederman rc = selinux_sysctl_get_sid(table, (op == 0001) ? 17601da177e4SLinus Torvalds SECCLASS_DIR : SECCLASS_FILE, &tsid); 17611da177e4SLinus Torvalds if (rc) { 17621da177e4SLinus Torvalds /* Default to the well-defined sysctl SID. */ 17631da177e4SLinus Torvalds tsid = SECINITSID_SYSCTL; 17641da177e4SLinus Torvalds } 17651da177e4SLinus Torvalds 17661da177e4SLinus Torvalds /* The op values are "defined" in sysctl.c, thereby creating 17671da177e4SLinus Torvalds * a bad coupling between this module and sysctl.c */ 17681da177e4SLinus Torvalds if(op == 001) { 17691da177e4SLinus Torvalds error = avc_has_perm(tsec->sid, tsid, 17701da177e4SLinus Torvalds SECCLASS_DIR, DIR__SEARCH, NULL); 17711da177e4SLinus Torvalds } else { 17721da177e4SLinus Torvalds av = 0; 17731da177e4SLinus Torvalds if (op & 004) 17741da177e4SLinus Torvalds av |= FILE__READ; 17751da177e4SLinus Torvalds if (op & 002) 17761da177e4SLinus Torvalds av |= FILE__WRITE; 17771da177e4SLinus Torvalds if (av) 17781da177e4SLinus Torvalds error = avc_has_perm(tsec->sid, tsid, 17791da177e4SLinus Torvalds SECCLASS_FILE, av, NULL); 17801da177e4SLinus Torvalds } 17811da177e4SLinus Torvalds 17821da177e4SLinus Torvalds return error; 17831da177e4SLinus Torvalds } 17841da177e4SLinus Torvalds 17851da177e4SLinus Torvalds static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb) 17861da177e4SLinus Torvalds { 17871da177e4SLinus Torvalds int rc = 0; 17881da177e4SLinus Torvalds 17891da177e4SLinus Torvalds if (!sb) 17901da177e4SLinus Torvalds return 0; 17911da177e4SLinus Torvalds 17921da177e4SLinus Torvalds switch (cmds) { 17931da177e4SLinus Torvalds case Q_SYNC: 17941da177e4SLinus Torvalds case Q_QUOTAON: 17951da177e4SLinus Torvalds case Q_QUOTAOFF: 17961da177e4SLinus Torvalds case Q_SETINFO: 17971da177e4SLinus Torvalds case Q_SETQUOTA: 17981da177e4SLinus Torvalds rc = superblock_has_perm(current, 17991da177e4SLinus Torvalds sb, 18001da177e4SLinus Torvalds FILESYSTEM__QUOTAMOD, NULL); 18011da177e4SLinus Torvalds break; 18021da177e4SLinus Torvalds case Q_GETFMT: 18031da177e4SLinus Torvalds case Q_GETINFO: 18041da177e4SLinus Torvalds case Q_GETQUOTA: 18051da177e4SLinus Torvalds rc = superblock_has_perm(current, 18061da177e4SLinus Torvalds sb, 18071da177e4SLinus Torvalds FILESYSTEM__QUOTAGET, NULL); 18081da177e4SLinus Torvalds break; 18091da177e4SLinus Torvalds default: 18101da177e4SLinus Torvalds rc = 0; /* let the kernel handle invalid cmds */ 18111da177e4SLinus Torvalds break; 18121da177e4SLinus Torvalds } 18131da177e4SLinus Torvalds return rc; 18141da177e4SLinus Torvalds } 18151da177e4SLinus Torvalds 18161da177e4SLinus Torvalds static int selinux_quota_on(struct dentry *dentry) 18171da177e4SLinus Torvalds { 18181da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__QUOTAON); 18191da177e4SLinus Torvalds } 18201da177e4SLinus Torvalds 18211da177e4SLinus Torvalds static int selinux_syslog(int type) 18221da177e4SLinus Torvalds { 18231da177e4SLinus Torvalds int rc; 18241da177e4SLinus Torvalds 18251da177e4SLinus Torvalds rc = secondary_ops->syslog(type); 18261da177e4SLinus Torvalds if (rc) 18271da177e4SLinus Torvalds return rc; 18281da177e4SLinus Torvalds 18291da177e4SLinus Torvalds switch (type) { 18301da177e4SLinus Torvalds case 3: /* Read last kernel messages */ 18311da177e4SLinus Torvalds case 10: /* Return size of the log buffer */ 18321da177e4SLinus Torvalds rc = task_has_system(current, SYSTEM__SYSLOG_READ); 18331da177e4SLinus Torvalds break; 18341da177e4SLinus Torvalds case 6: /* Disable logging to console */ 18351da177e4SLinus Torvalds case 7: /* Enable logging to console */ 18361da177e4SLinus Torvalds case 8: /* Set level of messages printed to console */ 18371da177e4SLinus Torvalds rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE); 18381da177e4SLinus Torvalds break; 18391da177e4SLinus Torvalds case 0: /* Close log */ 18401da177e4SLinus Torvalds case 1: /* Open log */ 18411da177e4SLinus Torvalds case 2: /* Read from log */ 18421da177e4SLinus Torvalds case 4: /* Read/clear last kernel messages */ 18431da177e4SLinus Torvalds case 5: /* Clear ring buffer */ 18441da177e4SLinus Torvalds default: 18451da177e4SLinus Torvalds rc = task_has_system(current, SYSTEM__SYSLOG_MOD); 18461da177e4SLinus Torvalds break; 18471da177e4SLinus Torvalds } 18481da177e4SLinus Torvalds return rc; 18491da177e4SLinus Torvalds } 18501da177e4SLinus Torvalds 18511da177e4SLinus Torvalds /* 18521da177e4SLinus Torvalds * Check that a process has enough memory to allocate a new virtual 18531da177e4SLinus Torvalds * mapping. 0 means there is enough memory for the allocation to 18541da177e4SLinus Torvalds * succeed and -ENOMEM implies there is not. 18551da177e4SLinus Torvalds * 18561da177e4SLinus Torvalds * Note that secondary_ops->capable and task_has_perm_noaudit return 0 18571da177e4SLinus Torvalds * if the capability is granted, but __vm_enough_memory requires 1 if 18581da177e4SLinus Torvalds * the capability is granted. 18591da177e4SLinus Torvalds * 18601da177e4SLinus Torvalds * Do not audit the selinux permission check, as this is applied to all 18611da177e4SLinus Torvalds * processes that allocate mappings. 18621da177e4SLinus Torvalds */ 186334b4e4aaSAlan Cox static int selinux_vm_enough_memory(struct mm_struct *mm, long pages) 18641da177e4SLinus Torvalds { 18651da177e4SLinus Torvalds int rc, cap_sys_admin = 0; 18661da177e4SLinus Torvalds struct task_security_struct *tsec = current->security; 18671da177e4SLinus Torvalds 18681da177e4SLinus Torvalds rc = secondary_ops->capable(current, CAP_SYS_ADMIN); 18691da177e4SLinus Torvalds if (rc == 0) 18701da177e4SLinus Torvalds rc = avc_has_perm_noaudit(tsec->sid, tsec->sid, 18711da177e4SLinus Torvalds SECCLASS_CAPABILITY, 18721da177e4SLinus Torvalds CAP_TO_MASK(CAP_SYS_ADMIN), 18732c3c05dbSStephen Smalley 0, 18741da177e4SLinus Torvalds NULL); 18751da177e4SLinus Torvalds 18761da177e4SLinus Torvalds if (rc == 0) 18771da177e4SLinus Torvalds cap_sys_admin = 1; 18781da177e4SLinus Torvalds 187934b4e4aaSAlan Cox return __vm_enough_memory(mm, pages, cap_sys_admin); 18801da177e4SLinus Torvalds } 18811da177e4SLinus Torvalds 18821da177e4SLinus Torvalds /* binprm security operations */ 18831da177e4SLinus Torvalds 18841da177e4SLinus Torvalds static int selinux_bprm_alloc_security(struct linux_binprm *bprm) 18851da177e4SLinus Torvalds { 18861da177e4SLinus Torvalds struct bprm_security_struct *bsec; 18871da177e4SLinus Torvalds 188889d155efSJames Morris bsec = kzalloc(sizeof(struct bprm_security_struct), GFP_KERNEL); 18891da177e4SLinus Torvalds if (!bsec) 18901da177e4SLinus Torvalds return -ENOMEM; 18911da177e4SLinus Torvalds 18921da177e4SLinus Torvalds bsec->bprm = bprm; 18931da177e4SLinus Torvalds bsec->sid = SECINITSID_UNLABELED; 18941da177e4SLinus Torvalds bsec->set = 0; 18951da177e4SLinus Torvalds 18961da177e4SLinus Torvalds bprm->security = bsec; 18971da177e4SLinus Torvalds return 0; 18981da177e4SLinus Torvalds } 18991da177e4SLinus Torvalds 19001da177e4SLinus Torvalds static int selinux_bprm_set_security(struct linux_binprm *bprm) 19011da177e4SLinus Torvalds { 19021da177e4SLinus Torvalds struct task_security_struct *tsec; 19033d5ff529SJosef Sipek struct inode *inode = bprm->file->f_path.dentry->d_inode; 19041da177e4SLinus Torvalds struct inode_security_struct *isec; 19051da177e4SLinus Torvalds struct bprm_security_struct *bsec; 19061da177e4SLinus Torvalds u32 newsid; 19071da177e4SLinus Torvalds struct avc_audit_data ad; 19081da177e4SLinus Torvalds int rc; 19091da177e4SLinus Torvalds 19101da177e4SLinus Torvalds rc = secondary_ops->bprm_set_security(bprm); 19111da177e4SLinus Torvalds if (rc) 19121da177e4SLinus Torvalds return rc; 19131da177e4SLinus Torvalds 19141da177e4SLinus Torvalds bsec = bprm->security; 19151da177e4SLinus Torvalds 19161da177e4SLinus Torvalds if (bsec->set) 19171da177e4SLinus Torvalds return 0; 19181da177e4SLinus Torvalds 19191da177e4SLinus Torvalds tsec = current->security; 19201da177e4SLinus Torvalds isec = inode->i_security; 19211da177e4SLinus Torvalds 19221da177e4SLinus Torvalds /* Default to the current task SID. */ 19231da177e4SLinus Torvalds bsec->sid = tsec->sid; 19241da177e4SLinus Torvalds 192528eba5bfSMichael LeMay /* Reset fs, key, and sock SIDs on execve. */ 19261da177e4SLinus Torvalds tsec->create_sid = 0; 192728eba5bfSMichael LeMay tsec->keycreate_sid = 0; 192842c3e03eSEric Paris tsec->sockcreate_sid = 0; 19291da177e4SLinus Torvalds 19301da177e4SLinus Torvalds if (tsec->exec_sid) { 19311da177e4SLinus Torvalds newsid = tsec->exec_sid; 19321da177e4SLinus Torvalds /* Reset exec SID on execve. */ 19331da177e4SLinus Torvalds tsec->exec_sid = 0; 19341da177e4SLinus Torvalds } else { 19351da177e4SLinus Torvalds /* Check for a default transition on this program. */ 19361da177e4SLinus Torvalds rc = security_transition_sid(tsec->sid, isec->sid, 19371da177e4SLinus Torvalds SECCLASS_PROCESS, &newsid); 19381da177e4SLinus Torvalds if (rc) 19391da177e4SLinus Torvalds return rc; 19401da177e4SLinus Torvalds } 19411da177e4SLinus Torvalds 19421da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, FS); 194344707fdfSJan Blunck ad.u.fs.path = bprm->file->f_path; 19441da177e4SLinus Torvalds 19453d5ff529SJosef Sipek if (bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) 19461da177e4SLinus Torvalds newsid = tsec->sid; 19471da177e4SLinus Torvalds 19481da177e4SLinus Torvalds if (tsec->sid == newsid) { 19491da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, 19501da177e4SLinus Torvalds SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad); 19511da177e4SLinus Torvalds if (rc) 19521da177e4SLinus Torvalds return rc; 19531da177e4SLinus Torvalds } else { 19541da177e4SLinus Torvalds /* Check permissions for the transition. */ 19551da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, newsid, 19561da177e4SLinus Torvalds SECCLASS_PROCESS, PROCESS__TRANSITION, &ad); 19571da177e4SLinus Torvalds if (rc) 19581da177e4SLinus Torvalds return rc; 19591da177e4SLinus Torvalds 19601da177e4SLinus Torvalds rc = avc_has_perm(newsid, isec->sid, 19611da177e4SLinus Torvalds SECCLASS_FILE, FILE__ENTRYPOINT, &ad); 19621da177e4SLinus Torvalds if (rc) 19631da177e4SLinus Torvalds return rc; 19641da177e4SLinus Torvalds 19651da177e4SLinus Torvalds /* Clear any possibly unsafe personality bits on exec: */ 19661da177e4SLinus Torvalds current->personality &= ~PER_CLEAR_ON_SETID; 19671da177e4SLinus Torvalds 19681da177e4SLinus Torvalds /* Set the security field to the new SID. */ 19691da177e4SLinus Torvalds bsec->sid = newsid; 19701da177e4SLinus Torvalds } 19711da177e4SLinus Torvalds 19721da177e4SLinus Torvalds bsec->set = 1; 19731da177e4SLinus Torvalds return 0; 19741da177e4SLinus Torvalds } 19751da177e4SLinus Torvalds 19761da177e4SLinus Torvalds static int selinux_bprm_check_security (struct linux_binprm *bprm) 19771da177e4SLinus Torvalds { 19781da177e4SLinus Torvalds return secondary_ops->bprm_check_security(bprm); 19791da177e4SLinus Torvalds } 19801da177e4SLinus Torvalds 19811da177e4SLinus Torvalds 19821da177e4SLinus Torvalds static int selinux_bprm_secureexec (struct linux_binprm *bprm) 19831da177e4SLinus Torvalds { 19841da177e4SLinus Torvalds struct task_security_struct *tsec = current->security; 19851da177e4SLinus Torvalds int atsecure = 0; 19861da177e4SLinus Torvalds 19871da177e4SLinus Torvalds if (tsec->osid != tsec->sid) { 19881da177e4SLinus Torvalds /* Enable secure mode for SIDs transitions unless 19891da177e4SLinus Torvalds the noatsecure permission is granted between 19901da177e4SLinus Torvalds the two SIDs, i.e. ahp returns 0. */ 19911da177e4SLinus Torvalds atsecure = avc_has_perm(tsec->osid, tsec->sid, 19921da177e4SLinus Torvalds SECCLASS_PROCESS, 19931da177e4SLinus Torvalds PROCESS__NOATSECURE, NULL); 19941da177e4SLinus Torvalds } 19951da177e4SLinus Torvalds 19961da177e4SLinus Torvalds return (atsecure || secondary_ops->bprm_secureexec(bprm)); 19971da177e4SLinus Torvalds } 19981da177e4SLinus Torvalds 19991da177e4SLinus Torvalds static void selinux_bprm_free_security(struct linux_binprm *bprm) 20001da177e4SLinus Torvalds { 20019a5f04bfSJesper Juhl kfree(bprm->security); 20021da177e4SLinus Torvalds bprm->security = NULL; 20031da177e4SLinus Torvalds } 20041da177e4SLinus Torvalds 20051da177e4SLinus Torvalds extern struct vfsmount *selinuxfs_mount; 20061da177e4SLinus Torvalds extern struct dentry *selinux_null; 20071da177e4SLinus Torvalds 20081da177e4SLinus Torvalds /* Derived from fs/exec.c:flush_old_files. */ 20091da177e4SLinus Torvalds static inline void flush_unauthorized_files(struct files_struct * files) 20101da177e4SLinus Torvalds { 20111da177e4SLinus Torvalds struct avc_audit_data ad; 20121da177e4SLinus Torvalds struct file *file, *devnull = NULL; 2013b20c8122SStephen Smalley struct tty_struct *tty; 2014badf1662SDipankar Sarma struct fdtable *fdt; 20151da177e4SLinus Torvalds long j = -1; 201624ec839cSPeter Zijlstra int drop_tty = 0; 20171da177e4SLinus Torvalds 2018b20c8122SStephen Smalley mutex_lock(&tty_mutex); 201924ec839cSPeter Zijlstra tty = get_current_tty(); 20201da177e4SLinus Torvalds if (tty) { 20211da177e4SLinus Torvalds file_list_lock(); 20222f512016SEric Dumazet file = list_entry(tty->tty_files.next, typeof(*file), f_u.fu_list); 20231da177e4SLinus Torvalds if (file) { 20241da177e4SLinus Torvalds /* Revalidate access to controlling tty. 20251da177e4SLinus Torvalds Use inode_has_perm on the tty inode directly rather 20261da177e4SLinus Torvalds than using file_has_perm, as this particular open 20271da177e4SLinus Torvalds file may belong to another process and we are only 20281da177e4SLinus Torvalds interested in the inode-based check here. */ 20293d5ff529SJosef Sipek struct inode *inode = file->f_path.dentry->d_inode; 20301da177e4SLinus Torvalds if (inode_has_perm(current, inode, 20311da177e4SLinus Torvalds FILE__READ | FILE__WRITE, NULL)) { 203224ec839cSPeter Zijlstra drop_tty = 1; 20331da177e4SLinus Torvalds } 20341da177e4SLinus Torvalds } 20351da177e4SLinus Torvalds file_list_unlock(); 20361da177e4SLinus Torvalds } 2037b20c8122SStephen Smalley mutex_unlock(&tty_mutex); 203898a27ba4SEric W. Biederman /* Reset controlling tty. */ 203998a27ba4SEric W. Biederman if (drop_tty) 204098a27ba4SEric W. Biederman no_tty(); 20411da177e4SLinus Torvalds 20421da177e4SLinus Torvalds /* Revalidate access to inherited open files. */ 20431da177e4SLinus Torvalds 20441da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,FS); 20451da177e4SLinus Torvalds 20461da177e4SLinus Torvalds spin_lock(&files->file_lock); 20471da177e4SLinus Torvalds for (;;) { 20481da177e4SLinus Torvalds unsigned long set, i; 20491da177e4SLinus Torvalds int fd; 20501da177e4SLinus Torvalds 20511da177e4SLinus Torvalds j++; 20521da177e4SLinus Torvalds i = j * __NFDBITS; 2053badf1662SDipankar Sarma fdt = files_fdtable(files); 2054bbea9f69SVadim Lobanov if (i >= fdt->max_fds) 20551da177e4SLinus Torvalds break; 2056badf1662SDipankar Sarma set = fdt->open_fds->fds_bits[j]; 20571da177e4SLinus Torvalds if (!set) 20581da177e4SLinus Torvalds continue; 20591da177e4SLinus Torvalds spin_unlock(&files->file_lock); 20601da177e4SLinus Torvalds for ( ; set ; i++,set >>= 1) { 20611da177e4SLinus Torvalds if (set & 1) { 20621da177e4SLinus Torvalds file = fget(i); 20631da177e4SLinus Torvalds if (!file) 20641da177e4SLinus Torvalds continue; 20651da177e4SLinus Torvalds if (file_has_perm(current, 20661da177e4SLinus Torvalds file, 20671da177e4SLinus Torvalds file_to_av(file))) { 20681da177e4SLinus Torvalds sys_close(i); 20691da177e4SLinus Torvalds fd = get_unused_fd(); 20701da177e4SLinus Torvalds if (fd != i) { 20711da177e4SLinus Torvalds if (fd >= 0) 20721da177e4SLinus Torvalds put_unused_fd(fd); 20731da177e4SLinus Torvalds fput(file); 20741da177e4SLinus Torvalds continue; 20751da177e4SLinus Torvalds } 20761da177e4SLinus Torvalds if (devnull) { 2077095975daSNick Piggin get_file(devnull); 20781da177e4SLinus Torvalds } else { 20791da177e4SLinus Torvalds devnull = dentry_open(dget(selinux_null), mntget(selinuxfs_mount), O_RDWR); 2080fc5d81e6SAkinobu Mita if (IS_ERR(devnull)) { 2081fc5d81e6SAkinobu Mita devnull = NULL; 20821da177e4SLinus Torvalds put_unused_fd(fd); 20831da177e4SLinus Torvalds fput(file); 20841da177e4SLinus Torvalds continue; 20851da177e4SLinus Torvalds } 20861da177e4SLinus Torvalds } 20871da177e4SLinus Torvalds fd_install(fd, devnull); 20881da177e4SLinus Torvalds } 20891da177e4SLinus Torvalds fput(file); 20901da177e4SLinus Torvalds } 20911da177e4SLinus Torvalds } 20921da177e4SLinus Torvalds spin_lock(&files->file_lock); 20931da177e4SLinus Torvalds 20941da177e4SLinus Torvalds } 20951da177e4SLinus Torvalds spin_unlock(&files->file_lock); 20961da177e4SLinus Torvalds } 20971da177e4SLinus Torvalds 20981da177e4SLinus Torvalds static void selinux_bprm_apply_creds(struct linux_binprm *bprm, int unsafe) 20991da177e4SLinus Torvalds { 21001da177e4SLinus Torvalds struct task_security_struct *tsec; 21011da177e4SLinus Torvalds struct bprm_security_struct *bsec; 21021da177e4SLinus Torvalds u32 sid; 21031da177e4SLinus Torvalds int rc; 21041da177e4SLinus Torvalds 21051da177e4SLinus Torvalds secondary_ops->bprm_apply_creds(bprm, unsafe); 21061da177e4SLinus Torvalds 21071da177e4SLinus Torvalds tsec = current->security; 21081da177e4SLinus Torvalds 21091da177e4SLinus Torvalds bsec = bprm->security; 21101da177e4SLinus Torvalds sid = bsec->sid; 21111da177e4SLinus Torvalds 21121da177e4SLinus Torvalds tsec->osid = tsec->sid; 21131da177e4SLinus Torvalds bsec->unsafe = 0; 21141da177e4SLinus Torvalds if (tsec->sid != sid) { 21151da177e4SLinus Torvalds /* Check for shared state. If not ok, leave SID 21161da177e4SLinus Torvalds unchanged and kill. */ 21171da177e4SLinus Torvalds if (unsafe & LSM_UNSAFE_SHARE) { 21181da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS, 21191da177e4SLinus Torvalds PROCESS__SHARE, NULL); 21201da177e4SLinus Torvalds if (rc) { 21211da177e4SLinus Torvalds bsec->unsafe = 1; 21221da177e4SLinus Torvalds return; 21231da177e4SLinus Torvalds } 21241da177e4SLinus Torvalds } 21251da177e4SLinus Torvalds 21261da177e4SLinus Torvalds /* Check for ptracing, and update the task SID if ok. 21271da177e4SLinus Torvalds Otherwise, leave SID unchanged and kill. */ 21281da177e4SLinus Torvalds if (unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) { 21291da177e4SLinus Torvalds rc = avc_has_perm(tsec->ptrace_sid, sid, 21301da177e4SLinus Torvalds SECCLASS_PROCESS, PROCESS__PTRACE, 21311da177e4SLinus Torvalds NULL); 21321da177e4SLinus Torvalds if (rc) { 21331da177e4SLinus Torvalds bsec->unsafe = 1; 21341da177e4SLinus Torvalds return; 21351da177e4SLinus Torvalds } 21361da177e4SLinus Torvalds } 21371da177e4SLinus Torvalds tsec->sid = sid; 21381da177e4SLinus Torvalds } 21391da177e4SLinus Torvalds } 21401da177e4SLinus Torvalds 21411da177e4SLinus Torvalds /* 21421da177e4SLinus Torvalds * called after apply_creds without the task lock held 21431da177e4SLinus Torvalds */ 21441da177e4SLinus Torvalds static void selinux_bprm_post_apply_creds(struct linux_binprm *bprm) 21451da177e4SLinus Torvalds { 21461da177e4SLinus Torvalds struct task_security_struct *tsec; 21471da177e4SLinus Torvalds struct rlimit *rlim, *initrlim; 21481da177e4SLinus Torvalds struct itimerval itimer; 21491da177e4SLinus Torvalds struct bprm_security_struct *bsec; 21501da177e4SLinus Torvalds int rc, i; 21511da177e4SLinus Torvalds 21521da177e4SLinus Torvalds tsec = current->security; 21531da177e4SLinus Torvalds bsec = bprm->security; 21541da177e4SLinus Torvalds 21551da177e4SLinus Torvalds if (bsec->unsafe) { 21561da177e4SLinus Torvalds force_sig_specific(SIGKILL, current); 21571da177e4SLinus Torvalds return; 21581da177e4SLinus Torvalds } 21591da177e4SLinus Torvalds if (tsec->osid == tsec->sid) 21601da177e4SLinus Torvalds return; 21611da177e4SLinus Torvalds 21621da177e4SLinus Torvalds /* Close files for which the new task SID is not authorized. */ 21631da177e4SLinus Torvalds flush_unauthorized_files(current->files); 21641da177e4SLinus Torvalds 21651da177e4SLinus Torvalds /* Check whether the new SID can inherit signal state 21661da177e4SLinus Torvalds from the old SID. If not, clear itimers to avoid 21671da177e4SLinus Torvalds subsequent signal generation and flush and unblock 21681da177e4SLinus Torvalds signals. This must occur _after_ the task SID has 21691da177e4SLinus Torvalds been updated so that any kill done after the flush 21701da177e4SLinus Torvalds will be checked against the new SID. */ 21711da177e4SLinus Torvalds rc = avc_has_perm(tsec->osid, tsec->sid, SECCLASS_PROCESS, 21721da177e4SLinus Torvalds PROCESS__SIGINH, NULL); 21731da177e4SLinus Torvalds if (rc) { 21741da177e4SLinus Torvalds memset(&itimer, 0, sizeof itimer); 21751da177e4SLinus Torvalds for (i = 0; i < 3; i++) 21761da177e4SLinus Torvalds do_setitimer(i, &itimer, NULL); 21771da177e4SLinus Torvalds flush_signals(current); 21781da177e4SLinus Torvalds spin_lock_irq(¤t->sighand->siglock); 21791da177e4SLinus Torvalds flush_signal_handlers(current, 1); 21801da177e4SLinus Torvalds sigemptyset(¤t->blocked); 21811da177e4SLinus Torvalds recalc_sigpending(); 21821da177e4SLinus Torvalds spin_unlock_irq(¤t->sighand->siglock); 21831da177e4SLinus Torvalds } 21841da177e4SLinus Torvalds 21854ac212adSStephen Smalley /* Always clear parent death signal on SID transitions. */ 21864ac212adSStephen Smalley current->pdeath_signal = 0; 21874ac212adSStephen Smalley 21881da177e4SLinus Torvalds /* Check whether the new SID can inherit resource limits 21891da177e4SLinus Torvalds from the old SID. If not, reset all soft limits to 21901da177e4SLinus Torvalds the lower of the current task's hard limit and the init 21911da177e4SLinus Torvalds task's soft limit. Note that the setting of hard limits 21921da177e4SLinus Torvalds (even to lower them) can be controlled by the setrlimit 21931da177e4SLinus Torvalds check. The inclusion of the init task's soft limit into 21941da177e4SLinus Torvalds the computation is to avoid resetting soft limits higher 21951da177e4SLinus Torvalds than the default soft limit for cases where the default 21961da177e4SLinus Torvalds is lower than the hard limit, e.g. RLIMIT_CORE or 21971da177e4SLinus Torvalds RLIMIT_STACK.*/ 21981da177e4SLinus Torvalds rc = avc_has_perm(tsec->osid, tsec->sid, SECCLASS_PROCESS, 21991da177e4SLinus Torvalds PROCESS__RLIMITINH, NULL); 22001da177e4SLinus Torvalds if (rc) { 22011da177e4SLinus Torvalds for (i = 0; i < RLIM_NLIMITS; i++) { 22021da177e4SLinus Torvalds rlim = current->signal->rlim + i; 22031da177e4SLinus Torvalds initrlim = init_task.signal->rlim+i; 22041da177e4SLinus Torvalds rlim->rlim_cur = min(rlim->rlim_max,initrlim->rlim_cur); 22051da177e4SLinus Torvalds } 22061da177e4SLinus Torvalds if (current->signal->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) { 22071da177e4SLinus Torvalds /* 22081da177e4SLinus Torvalds * This will cause RLIMIT_CPU calculations 22091da177e4SLinus Torvalds * to be refigured. 22101da177e4SLinus Torvalds */ 22111da177e4SLinus Torvalds current->it_prof_expires = jiffies_to_cputime(1); 22121da177e4SLinus Torvalds } 22131da177e4SLinus Torvalds } 22141da177e4SLinus Torvalds 22151da177e4SLinus Torvalds /* Wake up the parent if it is waiting so that it can 22161da177e4SLinus Torvalds recheck wait permission to the new task SID. */ 22171da177e4SLinus Torvalds wake_up_interruptible(¤t->parent->signal->wait_chldexit); 22181da177e4SLinus Torvalds } 22191da177e4SLinus Torvalds 22201da177e4SLinus Torvalds /* superblock security operations */ 22211da177e4SLinus Torvalds 22221da177e4SLinus Torvalds static int selinux_sb_alloc_security(struct super_block *sb) 22231da177e4SLinus Torvalds { 22241da177e4SLinus Torvalds return superblock_alloc_security(sb); 22251da177e4SLinus Torvalds } 22261da177e4SLinus Torvalds 22271da177e4SLinus Torvalds static void selinux_sb_free_security(struct super_block *sb) 22281da177e4SLinus Torvalds { 22291da177e4SLinus Torvalds superblock_free_security(sb); 22301da177e4SLinus Torvalds } 22311da177e4SLinus Torvalds 22321da177e4SLinus Torvalds static inline int match_prefix(char *prefix, int plen, char *option, int olen) 22331da177e4SLinus Torvalds { 22341da177e4SLinus Torvalds if (plen > olen) 22351da177e4SLinus Torvalds return 0; 22361da177e4SLinus Torvalds 22371da177e4SLinus Torvalds return !memcmp(prefix, option, plen); 22381da177e4SLinus Torvalds } 22391da177e4SLinus Torvalds 22401da177e4SLinus Torvalds static inline int selinux_option(char *option, int len) 22411da177e4SLinus Torvalds { 22421da177e4SLinus Torvalds return (match_prefix("context=", sizeof("context=")-1, option, len) || 22431da177e4SLinus Torvalds match_prefix("fscontext=", sizeof("fscontext=")-1, option, len) || 22440808925eSEric Paris match_prefix("defcontext=", sizeof("defcontext=")-1, option, len) || 22450808925eSEric Paris match_prefix("rootcontext=", sizeof("rootcontext=")-1, option, len)); 22461da177e4SLinus Torvalds } 22471da177e4SLinus Torvalds 22481da177e4SLinus Torvalds static inline void take_option(char **to, char *from, int *first, int len) 22491da177e4SLinus Torvalds { 22501da177e4SLinus Torvalds if (!*first) { 22511da177e4SLinus Torvalds **to = ','; 22521da177e4SLinus Torvalds *to += 1; 22533528a953SCory Olmo } else 22541da177e4SLinus Torvalds *first = 0; 22551da177e4SLinus Torvalds memcpy(*to, from, len); 22561da177e4SLinus Torvalds *to += len; 22571da177e4SLinus Torvalds } 22581da177e4SLinus Torvalds 22593528a953SCory Olmo static inline void take_selinux_option(char **to, char *from, int *first, 22603528a953SCory Olmo int len) 22613528a953SCory Olmo { 22623528a953SCory Olmo int current_size = 0; 22633528a953SCory Olmo 22643528a953SCory Olmo if (!*first) { 22653528a953SCory Olmo **to = '|'; 22663528a953SCory Olmo *to += 1; 22673528a953SCory Olmo } 22683528a953SCory Olmo else 22693528a953SCory Olmo *first = 0; 22703528a953SCory Olmo 22713528a953SCory Olmo while (current_size < len) { 22723528a953SCory Olmo if (*from != '"') { 22733528a953SCory Olmo **to = *from; 22743528a953SCory Olmo *to += 1; 22753528a953SCory Olmo } 22763528a953SCory Olmo from += 1; 22773528a953SCory Olmo current_size += 1; 22783528a953SCory Olmo } 22793528a953SCory Olmo } 22803528a953SCory Olmo 2281e0007529SEric Paris static int selinux_sb_copy_data(char *orig, char *copy) 22821da177e4SLinus Torvalds { 22831da177e4SLinus Torvalds int fnosec, fsec, rc = 0; 22841da177e4SLinus Torvalds char *in_save, *in_curr, *in_end; 22851da177e4SLinus Torvalds char *sec_curr, *nosec_save, *nosec; 22863528a953SCory Olmo int open_quote = 0; 22871da177e4SLinus Torvalds 22881da177e4SLinus Torvalds in_curr = orig; 22891da177e4SLinus Torvalds sec_curr = copy; 22901da177e4SLinus Torvalds 22911da177e4SLinus Torvalds nosec = (char *)get_zeroed_page(GFP_KERNEL); 22921da177e4SLinus Torvalds if (!nosec) { 22931da177e4SLinus Torvalds rc = -ENOMEM; 22941da177e4SLinus Torvalds goto out; 22951da177e4SLinus Torvalds } 22961da177e4SLinus Torvalds 22971da177e4SLinus Torvalds nosec_save = nosec; 22981da177e4SLinus Torvalds fnosec = fsec = 1; 22991da177e4SLinus Torvalds in_save = in_end = orig; 23001da177e4SLinus Torvalds 23011da177e4SLinus Torvalds do { 23023528a953SCory Olmo if (*in_end == '"') 23033528a953SCory Olmo open_quote = !open_quote; 23043528a953SCory Olmo if ((*in_end == ',' && open_quote == 0) || 23053528a953SCory Olmo *in_end == '\0') { 23061da177e4SLinus Torvalds int len = in_end - in_curr; 23071da177e4SLinus Torvalds 23081da177e4SLinus Torvalds if (selinux_option(in_curr, len)) 23093528a953SCory Olmo take_selinux_option(&sec_curr, in_curr, &fsec, len); 23101da177e4SLinus Torvalds else 23111da177e4SLinus Torvalds take_option(&nosec, in_curr, &fnosec, len); 23121da177e4SLinus Torvalds 23131da177e4SLinus Torvalds in_curr = in_end + 1; 23141da177e4SLinus Torvalds } 23151da177e4SLinus Torvalds } while (*in_end++); 23161da177e4SLinus Torvalds 23176931dfc9SEric Paris strcpy(in_save, nosec_save); 2318da3caa20SGerald Schaefer free_page((unsigned long)nosec_save); 23191da177e4SLinus Torvalds out: 23201da177e4SLinus Torvalds return rc; 23211da177e4SLinus Torvalds } 23221da177e4SLinus Torvalds 23231da177e4SLinus Torvalds static int selinux_sb_kern_mount(struct super_block *sb, void *data) 23241da177e4SLinus Torvalds { 23251da177e4SLinus Torvalds struct avc_audit_data ad; 23261da177e4SLinus Torvalds int rc; 23271da177e4SLinus Torvalds 23281da177e4SLinus Torvalds rc = superblock_doinit(sb, data); 23291da177e4SLinus Torvalds if (rc) 23301da177e4SLinus Torvalds return rc; 23311da177e4SLinus Torvalds 23321da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,FS); 233344707fdfSJan Blunck ad.u.fs.path.dentry = sb->s_root; 23341da177e4SLinus Torvalds return superblock_has_perm(current, sb, FILESYSTEM__MOUNT, &ad); 23351da177e4SLinus Torvalds } 23361da177e4SLinus Torvalds 2337726c3342SDavid Howells static int selinux_sb_statfs(struct dentry *dentry) 23381da177e4SLinus Torvalds { 23391da177e4SLinus Torvalds struct avc_audit_data ad; 23401da177e4SLinus Torvalds 23411da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,FS); 234244707fdfSJan Blunck ad.u.fs.path.dentry = dentry->d_sb->s_root; 2343726c3342SDavid Howells return superblock_has_perm(current, dentry->d_sb, FILESYSTEM__GETATTR, &ad); 23441da177e4SLinus Torvalds } 23451da177e4SLinus Torvalds 23461da177e4SLinus Torvalds static int selinux_mount(char * dev_name, 23471da177e4SLinus Torvalds struct nameidata *nd, 23481da177e4SLinus Torvalds char * type, 23491da177e4SLinus Torvalds unsigned long flags, 23501da177e4SLinus Torvalds void * data) 23511da177e4SLinus Torvalds { 23521da177e4SLinus Torvalds int rc; 23531da177e4SLinus Torvalds 23541da177e4SLinus Torvalds rc = secondary_ops->sb_mount(dev_name, nd, type, flags, data); 23551da177e4SLinus Torvalds if (rc) 23561da177e4SLinus Torvalds return rc; 23571da177e4SLinus Torvalds 23581da177e4SLinus Torvalds if (flags & MS_REMOUNT) 23594ac91378SJan Blunck return superblock_has_perm(current, nd->path.mnt->mnt_sb, 23601da177e4SLinus Torvalds FILESYSTEM__REMOUNT, NULL); 23611da177e4SLinus Torvalds else 23624ac91378SJan Blunck return dentry_has_perm(current, nd->path.mnt, nd->path.dentry, 23631da177e4SLinus Torvalds FILE__MOUNTON); 23641da177e4SLinus Torvalds } 23651da177e4SLinus Torvalds 23661da177e4SLinus Torvalds static int selinux_umount(struct vfsmount *mnt, int flags) 23671da177e4SLinus Torvalds { 23681da177e4SLinus Torvalds int rc; 23691da177e4SLinus Torvalds 23701da177e4SLinus Torvalds rc = secondary_ops->sb_umount(mnt, flags); 23711da177e4SLinus Torvalds if (rc) 23721da177e4SLinus Torvalds return rc; 23731da177e4SLinus Torvalds 23741da177e4SLinus Torvalds return superblock_has_perm(current,mnt->mnt_sb, 23751da177e4SLinus Torvalds FILESYSTEM__UNMOUNT,NULL); 23761da177e4SLinus Torvalds } 23771da177e4SLinus Torvalds 23781da177e4SLinus Torvalds /* inode security operations */ 23791da177e4SLinus Torvalds 23801da177e4SLinus Torvalds static int selinux_inode_alloc_security(struct inode *inode) 23811da177e4SLinus Torvalds { 23821da177e4SLinus Torvalds return inode_alloc_security(inode); 23831da177e4SLinus Torvalds } 23841da177e4SLinus Torvalds 23851da177e4SLinus Torvalds static void selinux_inode_free_security(struct inode *inode) 23861da177e4SLinus Torvalds { 23871da177e4SLinus Torvalds inode_free_security(inode); 23881da177e4SLinus Torvalds } 23891da177e4SLinus Torvalds 23905e41ff9eSStephen Smalley static int selinux_inode_init_security(struct inode *inode, struct inode *dir, 23915e41ff9eSStephen Smalley char **name, void **value, 23925e41ff9eSStephen Smalley size_t *len) 23935e41ff9eSStephen Smalley { 23945e41ff9eSStephen Smalley struct task_security_struct *tsec; 23955e41ff9eSStephen Smalley struct inode_security_struct *dsec; 23965e41ff9eSStephen Smalley struct superblock_security_struct *sbsec; 2397570bc1c2SStephen Smalley u32 newsid, clen; 23985e41ff9eSStephen Smalley int rc; 2399570bc1c2SStephen Smalley char *namep = NULL, *context; 24005e41ff9eSStephen Smalley 24015e41ff9eSStephen Smalley tsec = current->security; 24025e41ff9eSStephen Smalley dsec = dir->i_security; 24035e41ff9eSStephen Smalley sbsec = dir->i_sb->s_security; 24045e41ff9eSStephen Smalley 24055e41ff9eSStephen Smalley if (tsec->create_sid && sbsec->behavior != SECURITY_FS_USE_MNTPOINT) { 24065e41ff9eSStephen Smalley newsid = tsec->create_sid; 24075e41ff9eSStephen Smalley } else { 24085e41ff9eSStephen Smalley rc = security_transition_sid(tsec->sid, dsec->sid, 24095e41ff9eSStephen Smalley inode_mode_to_security_class(inode->i_mode), 24105e41ff9eSStephen Smalley &newsid); 24115e41ff9eSStephen Smalley if (rc) { 24125e41ff9eSStephen Smalley printk(KERN_WARNING "%s: " 24135e41ff9eSStephen Smalley "security_transition_sid failed, rc=%d (dev=%s " 24145e41ff9eSStephen Smalley "ino=%ld)\n", 24155e41ff9eSStephen Smalley __FUNCTION__, 24165e41ff9eSStephen Smalley -rc, inode->i_sb->s_id, inode->i_ino); 24175e41ff9eSStephen Smalley return rc; 24185e41ff9eSStephen Smalley } 24195e41ff9eSStephen Smalley } 24205e41ff9eSStephen Smalley 2421296fddf7SEric Paris /* Possibly defer initialization to selinux_complete_init. */ 2422296fddf7SEric Paris if (sbsec->initialized) { 2423296fddf7SEric Paris struct inode_security_struct *isec = inode->i_security; 2424296fddf7SEric Paris isec->sclass = inode_mode_to_security_class(inode->i_mode); 2425296fddf7SEric Paris isec->sid = newsid; 2426296fddf7SEric Paris isec->initialized = 1; 2427296fddf7SEric Paris } 24285e41ff9eSStephen Smalley 24298aad3875SStephen Smalley if (!ss_initialized || sbsec->behavior == SECURITY_FS_USE_MNTPOINT) 243025a74f3bSStephen Smalley return -EOPNOTSUPP; 243125a74f3bSStephen Smalley 2432570bc1c2SStephen Smalley if (name) { 2433a02fe132SJosef Bacik namep = kstrdup(XATTR_SELINUX_SUFFIX, GFP_NOFS); 24345e41ff9eSStephen Smalley if (!namep) 24355e41ff9eSStephen Smalley return -ENOMEM; 24365e41ff9eSStephen Smalley *name = namep; 2437570bc1c2SStephen Smalley } 24385e41ff9eSStephen Smalley 2439570bc1c2SStephen Smalley if (value && len) { 2440570bc1c2SStephen Smalley rc = security_sid_to_context(newsid, &context, &clen); 24415e41ff9eSStephen Smalley if (rc) { 24425e41ff9eSStephen Smalley kfree(namep); 24435e41ff9eSStephen Smalley return rc; 24445e41ff9eSStephen Smalley } 24455e41ff9eSStephen Smalley *value = context; 2446570bc1c2SStephen Smalley *len = clen; 2447570bc1c2SStephen Smalley } 24485e41ff9eSStephen Smalley 24495e41ff9eSStephen Smalley return 0; 24505e41ff9eSStephen Smalley } 24515e41ff9eSStephen Smalley 24521da177e4SLinus Torvalds static int selinux_inode_create(struct inode *dir, struct dentry *dentry, int mask) 24531da177e4SLinus Torvalds { 24541da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_FILE); 24551da177e4SLinus Torvalds } 24561da177e4SLinus Torvalds 24571da177e4SLinus Torvalds static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 24581da177e4SLinus Torvalds { 24591da177e4SLinus Torvalds int rc; 24601da177e4SLinus Torvalds 24611da177e4SLinus Torvalds rc = secondary_ops->inode_link(old_dentry,dir,new_dentry); 24621da177e4SLinus Torvalds if (rc) 24631da177e4SLinus Torvalds return rc; 24641da177e4SLinus Torvalds return may_link(dir, old_dentry, MAY_LINK); 24651da177e4SLinus Torvalds } 24661da177e4SLinus Torvalds 24671da177e4SLinus Torvalds static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry) 24681da177e4SLinus Torvalds { 24691da177e4SLinus Torvalds int rc; 24701da177e4SLinus Torvalds 24711da177e4SLinus Torvalds rc = secondary_ops->inode_unlink(dir, dentry); 24721da177e4SLinus Torvalds if (rc) 24731da177e4SLinus Torvalds return rc; 24741da177e4SLinus Torvalds return may_link(dir, dentry, MAY_UNLINK); 24751da177e4SLinus Torvalds } 24761da177e4SLinus Torvalds 24771da177e4SLinus Torvalds static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name) 24781da177e4SLinus Torvalds { 24791da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_LNK_FILE); 24801da177e4SLinus Torvalds } 24811da177e4SLinus Torvalds 24821da177e4SLinus Torvalds static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, int mask) 24831da177e4SLinus Torvalds { 24841da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_DIR); 24851da177e4SLinus Torvalds } 24861da177e4SLinus Torvalds 24871da177e4SLinus Torvalds static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry) 24881da177e4SLinus Torvalds { 24891da177e4SLinus Torvalds return may_link(dir, dentry, MAY_RMDIR); 24901da177e4SLinus Torvalds } 24911da177e4SLinus Torvalds 24921da177e4SLinus Torvalds static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 24931da177e4SLinus Torvalds { 24941da177e4SLinus Torvalds int rc; 24951da177e4SLinus Torvalds 24961da177e4SLinus Torvalds rc = secondary_ops->inode_mknod(dir, dentry, mode, dev); 24971da177e4SLinus Torvalds if (rc) 24981da177e4SLinus Torvalds return rc; 24991da177e4SLinus Torvalds 25001da177e4SLinus Torvalds return may_create(dir, dentry, inode_mode_to_security_class(mode)); 25011da177e4SLinus Torvalds } 25021da177e4SLinus Torvalds 25031da177e4SLinus Torvalds static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry, 25041da177e4SLinus Torvalds struct inode *new_inode, struct dentry *new_dentry) 25051da177e4SLinus Torvalds { 25061da177e4SLinus Torvalds return may_rename(old_inode, old_dentry, new_inode, new_dentry); 25071da177e4SLinus Torvalds } 25081da177e4SLinus Torvalds 25091da177e4SLinus Torvalds static int selinux_inode_readlink(struct dentry *dentry) 25101da177e4SLinus Torvalds { 25111da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__READ); 25121da177e4SLinus Torvalds } 25131da177e4SLinus Torvalds 25141da177e4SLinus Torvalds static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata) 25151da177e4SLinus Torvalds { 25161da177e4SLinus Torvalds int rc; 25171da177e4SLinus Torvalds 25181da177e4SLinus Torvalds rc = secondary_ops->inode_follow_link(dentry,nameidata); 25191da177e4SLinus Torvalds if (rc) 25201da177e4SLinus Torvalds return rc; 25211da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__READ); 25221da177e4SLinus Torvalds } 25231da177e4SLinus Torvalds 25241da177e4SLinus Torvalds static int selinux_inode_permission(struct inode *inode, int mask, 25251da177e4SLinus Torvalds struct nameidata *nd) 25261da177e4SLinus Torvalds { 25271da177e4SLinus Torvalds int rc; 25281da177e4SLinus Torvalds 25291da177e4SLinus Torvalds rc = secondary_ops->inode_permission(inode, mask, nd); 25301da177e4SLinus Torvalds if (rc) 25311da177e4SLinus Torvalds return rc; 25321da177e4SLinus Torvalds 25331da177e4SLinus Torvalds if (!mask) { 25341da177e4SLinus Torvalds /* No permission to check. Existence test. */ 25351da177e4SLinus Torvalds return 0; 25361da177e4SLinus Torvalds } 25371da177e4SLinus Torvalds 25381da177e4SLinus Torvalds return inode_has_perm(current, inode, 25391da177e4SLinus Torvalds file_mask_to_av(inode->i_mode, mask), NULL); 25401da177e4SLinus Torvalds } 25411da177e4SLinus Torvalds 25421da177e4SLinus Torvalds static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr) 25431da177e4SLinus Torvalds { 25441da177e4SLinus Torvalds int rc; 25451da177e4SLinus Torvalds 25461da177e4SLinus Torvalds rc = secondary_ops->inode_setattr(dentry, iattr); 25471da177e4SLinus Torvalds if (rc) 25481da177e4SLinus Torvalds return rc; 25491da177e4SLinus Torvalds 25501da177e4SLinus Torvalds if (iattr->ia_valid & ATTR_FORCE) 25511da177e4SLinus Torvalds return 0; 25521da177e4SLinus Torvalds 25531da177e4SLinus Torvalds if (iattr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | 25541da177e4SLinus Torvalds ATTR_ATIME_SET | ATTR_MTIME_SET)) 25551da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__SETATTR); 25561da177e4SLinus Torvalds 25571da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__WRITE); 25581da177e4SLinus Torvalds } 25591da177e4SLinus Torvalds 25601da177e4SLinus Torvalds static int selinux_inode_getattr(struct vfsmount *mnt, struct dentry *dentry) 25611da177e4SLinus Torvalds { 25621da177e4SLinus Torvalds return dentry_has_perm(current, mnt, dentry, FILE__GETATTR); 25631da177e4SLinus Torvalds } 25641da177e4SLinus Torvalds 2565b5376771SSerge E. Hallyn static int selinux_inode_setotherxattr(struct dentry *dentry, char *name) 2566b5376771SSerge E. Hallyn { 2567b5376771SSerge E. Hallyn if (!strncmp(name, XATTR_SECURITY_PREFIX, 2568b5376771SSerge E. Hallyn sizeof XATTR_SECURITY_PREFIX - 1)) { 2569b5376771SSerge E. Hallyn if (!strcmp(name, XATTR_NAME_CAPS)) { 2570b5376771SSerge E. Hallyn if (!capable(CAP_SETFCAP)) 2571b5376771SSerge E. Hallyn return -EPERM; 2572b5376771SSerge E. Hallyn } else if (!capable(CAP_SYS_ADMIN)) { 2573b5376771SSerge E. Hallyn /* A different attribute in the security namespace. 2574b5376771SSerge E. Hallyn Restrict to administrator. */ 2575b5376771SSerge E. Hallyn return -EPERM; 2576b5376771SSerge E. Hallyn } 2577b5376771SSerge E. Hallyn } 2578b5376771SSerge E. Hallyn 2579b5376771SSerge E. Hallyn /* Not an attribute we recognize, so just check the 2580b5376771SSerge E. Hallyn ordinary setattr permission. */ 2581b5376771SSerge E. Hallyn return dentry_has_perm(current, NULL, dentry, FILE__SETATTR); 2582b5376771SSerge E. Hallyn } 2583b5376771SSerge E. Hallyn 25841da177e4SLinus Torvalds static int selinux_inode_setxattr(struct dentry *dentry, char *name, void *value, size_t size, int flags) 25851da177e4SLinus Torvalds { 25861da177e4SLinus Torvalds struct task_security_struct *tsec = current->security; 25871da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 25881da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 25891da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 25901da177e4SLinus Torvalds struct avc_audit_data ad; 25911da177e4SLinus Torvalds u32 newsid; 25921da177e4SLinus Torvalds int rc = 0; 25931da177e4SLinus Torvalds 2594b5376771SSerge E. Hallyn if (strcmp(name, XATTR_NAME_SELINUX)) 2595b5376771SSerge E. Hallyn return selinux_inode_setotherxattr(dentry, name); 25961da177e4SLinus Torvalds 25971da177e4SLinus Torvalds sbsec = inode->i_sb->s_security; 25981da177e4SLinus Torvalds if (sbsec->behavior == SECURITY_FS_USE_MNTPOINT) 25991da177e4SLinus Torvalds return -EOPNOTSUPP; 26001da177e4SLinus Torvalds 26013bd858abSSatyam Sharma if (!is_owner_or_cap(inode)) 26021da177e4SLinus Torvalds return -EPERM; 26031da177e4SLinus Torvalds 26041da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,FS); 260544707fdfSJan Blunck ad.u.fs.path.dentry = dentry; 26061da177e4SLinus Torvalds 26071da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, isec->sclass, 26081da177e4SLinus Torvalds FILE__RELABELFROM, &ad); 26091da177e4SLinus Torvalds if (rc) 26101da177e4SLinus Torvalds return rc; 26111da177e4SLinus Torvalds 26121da177e4SLinus Torvalds rc = security_context_to_sid(value, size, &newsid); 26131da177e4SLinus Torvalds if (rc) 26141da177e4SLinus Torvalds return rc; 26151da177e4SLinus Torvalds 26161da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, newsid, isec->sclass, 26171da177e4SLinus Torvalds FILE__RELABELTO, &ad); 26181da177e4SLinus Torvalds if (rc) 26191da177e4SLinus Torvalds return rc; 26201da177e4SLinus Torvalds 26211da177e4SLinus Torvalds rc = security_validate_transition(isec->sid, newsid, tsec->sid, 26221da177e4SLinus Torvalds isec->sclass); 26231da177e4SLinus Torvalds if (rc) 26241da177e4SLinus Torvalds return rc; 26251da177e4SLinus Torvalds 26261da177e4SLinus Torvalds return avc_has_perm(newsid, 26271da177e4SLinus Torvalds sbsec->sid, 26281da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 26291da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, 26301da177e4SLinus Torvalds &ad); 26311da177e4SLinus Torvalds } 26321da177e4SLinus Torvalds 26331da177e4SLinus Torvalds static void selinux_inode_post_setxattr(struct dentry *dentry, char *name, 26341da177e4SLinus Torvalds void *value, size_t size, int flags) 26351da177e4SLinus Torvalds { 26361da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 26371da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 26381da177e4SLinus Torvalds u32 newsid; 26391da177e4SLinus Torvalds int rc; 26401da177e4SLinus Torvalds 26411da177e4SLinus Torvalds if (strcmp(name, XATTR_NAME_SELINUX)) { 26421da177e4SLinus Torvalds /* Not an attribute we recognize, so nothing to do. */ 26431da177e4SLinus Torvalds return; 26441da177e4SLinus Torvalds } 26451da177e4SLinus Torvalds 26461da177e4SLinus Torvalds rc = security_context_to_sid(value, size, &newsid); 26471da177e4SLinus Torvalds if (rc) { 26481da177e4SLinus Torvalds printk(KERN_WARNING "%s: unable to obtain SID for context " 26491da177e4SLinus Torvalds "%s, rc=%d\n", __FUNCTION__, (char*)value, -rc); 26501da177e4SLinus Torvalds return; 26511da177e4SLinus Torvalds } 26521da177e4SLinus Torvalds 26531da177e4SLinus Torvalds isec->sid = newsid; 26541da177e4SLinus Torvalds return; 26551da177e4SLinus Torvalds } 26561da177e4SLinus Torvalds 26571da177e4SLinus Torvalds static int selinux_inode_getxattr (struct dentry *dentry, char *name) 26581da177e4SLinus Torvalds { 26591da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__GETATTR); 26601da177e4SLinus Torvalds } 26611da177e4SLinus Torvalds 26621da177e4SLinus Torvalds static int selinux_inode_listxattr (struct dentry *dentry) 26631da177e4SLinus Torvalds { 26641da177e4SLinus Torvalds return dentry_has_perm(current, NULL, dentry, FILE__GETATTR); 26651da177e4SLinus Torvalds } 26661da177e4SLinus Torvalds 26671da177e4SLinus Torvalds static int selinux_inode_removexattr (struct dentry *dentry, char *name) 26681da177e4SLinus Torvalds { 2669b5376771SSerge E. Hallyn if (strcmp(name, XATTR_NAME_SELINUX)) 2670b5376771SSerge E. Hallyn return selinux_inode_setotherxattr(dentry, name); 26711da177e4SLinus Torvalds 26721da177e4SLinus Torvalds /* No one is allowed to remove a SELinux security label. 26731da177e4SLinus Torvalds You can change the label, but all data must be labeled. */ 26741da177e4SLinus Torvalds return -EACCES; 26751da177e4SLinus Torvalds } 26761da177e4SLinus Torvalds 2677d381d8a9SJames Morris /* 2678d381d8a9SJames Morris * Copy the in-core inode security context value to the user. If the 2679d381d8a9SJames Morris * getxattr() prior to this succeeded, check to see if we need to 2680d381d8a9SJames Morris * canonicalize the value to be finally returned to the user. 2681d381d8a9SJames Morris * 2682d381d8a9SJames Morris * Permission check is handled by selinux_inode_getxattr hook. 2683d381d8a9SJames Morris */ 268442492594SDavid P. Quigley static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void **buffer, bool alloc) 26851da177e4SLinus Torvalds { 268642492594SDavid P. Quigley u32 size; 268742492594SDavid P. Quigley int error; 268842492594SDavid P. Quigley char *context = NULL; 26891da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 26901da177e4SLinus Torvalds 26918c8570fbSDustin Kirkland if (strcmp(name, XATTR_SELINUX_SUFFIX)) 26928c8570fbSDustin Kirkland return -EOPNOTSUPP; 26931da177e4SLinus Torvalds 269442492594SDavid P. Quigley error = security_sid_to_context(isec->sid, &context, &size); 269542492594SDavid P. Quigley if (error) 269642492594SDavid P. Quigley return error; 269742492594SDavid P. Quigley error = size; 269842492594SDavid P. Quigley if (alloc) { 269942492594SDavid P. Quigley *buffer = context; 270042492594SDavid P. Quigley goto out_nofree; 270142492594SDavid P. Quigley } 270242492594SDavid P. Quigley kfree(context); 270342492594SDavid P. Quigley out_nofree: 270442492594SDavid P. Quigley return error; 27051da177e4SLinus Torvalds } 27061da177e4SLinus Torvalds 27071da177e4SLinus Torvalds static int selinux_inode_setsecurity(struct inode *inode, const char *name, 27081da177e4SLinus Torvalds const void *value, size_t size, int flags) 27091da177e4SLinus Torvalds { 27101da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 27111da177e4SLinus Torvalds u32 newsid; 27121da177e4SLinus Torvalds int rc; 27131da177e4SLinus Torvalds 27141da177e4SLinus Torvalds if (strcmp(name, XATTR_SELINUX_SUFFIX)) 27151da177e4SLinus Torvalds return -EOPNOTSUPP; 27161da177e4SLinus Torvalds 27171da177e4SLinus Torvalds if (!value || !size) 27181da177e4SLinus Torvalds return -EACCES; 27191da177e4SLinus Torvalds 27201da177e4SLinus Torvalds rc = security_context_to_sid((void*)value, size, &newsid); 27211da177e4SLinus Torvalds if (rc) 27221da177e4SLinus Torvalds return rc; 27231da177e4SLinus Torvalds 27241da177e4SLinus Torvalds isec->sid = newsid; 27251da177e4SLinus Torvalds return 0; 27261da177e4SLinus Torvalds } 27271da177e4SLinus Torvalds 27281da177e4SLinus Torvalds static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size) 27291da177e4SLinus Torvalds { 27301da177e4SLinus Torvalds const int len = sizeof(XATTR_NAME_SELINUX); 27311da177e4SLinus Torvalds if (buffer && len <= buffer_size) 27321da177e4SLinus Torvalds memcpy(buffer, XATTR_NAME_SELINUX, len); 27331da177e4SLinus Torvalds return len; 27341da177e4SLinus Torvalds } 27351da177e4SLinus Torvalds 2736b5376771SSerge E. Hallyn static int selinux_inode_need_killpriv(struct dentry *dentry) 2737b5376771SSerge E. Hallyn { 2738b5376771SSerge E. Hallyn return secondary_ops->inode_need_killpriv(dentry); 2739b5376771SSerge E. Hallyn } 2740b5376771SSerge E. Hallyn 2741b5376771SSerge E. Hallyn static int selinux_inode_killpriv(struct dentry *dentry) 2742b5376771SSerge E. Hallyn { 2743b5376771SSerge E. Hallyn return secondary_ops->inode_killpriv(dentry); 2744b5376771SSerge E. Hallyn } 2745b5376771SSerge E. Hallyn 27461da177e4SLinus Torvalds /* file security operations */ 27471da177e4SLinus Torvalds 2748788e7dd4SYuichi Nakamura static int selinux_revalidate_file_permission(struct file *file, int mask) 27491da177e4SLinus Torvalds { 27507420ed23SVenkat Yekkirala int rc; 27513d5ff529SJosef Sipek struct inode *inode = file->f_path.dentry->d_inode; 27521da177e4SLinus Torvalds 27531da177e4SLinus Torvalds if (!mask) { 27541da177e4SLinus Torvalds /* No permission to check. Existence test. */ 27551da177e4SLinus Torvalds return 0; 27561da177e4SLinus Torvalds } 27571da177e4SLinus Torvalds 27581da177e4SLinus Torvalds /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */ 27591da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE)) 27601da177e4SLinus Torvalds mask |= MAY_APPEND; 27611da177e4SLinus Torvalds 27627420ed23SVenkat Yekkirala rc = file_has_perm(current, file, 27631da177e4SLinus Torvalds file_mask_to_av(inode->i_mode, mask)); 27647420ed23SVenkat Yekkirala if (rc) 27657420ed23SVenkat Yekkirala return rc; 27667420ed23SVenkat Yekkirala 27677420ed23SVenkat Yekkirala return selinux_netlbl_inode_permission(inode, mask); 27681da177e4SLinus Torvalds } 27691da177e4SLinus Torvalds 2770788e7dd4SYuichi Nakamura static int selinux_file_permission(struct file *file, int mask) 2771788e7dd4SYuichi Nakamura { 2772788e7dd4SYuichi Nakamura struct inode *inode = file->f_path.dentry->d_inode; 2773788e7dd4SYuichi Nakamura struct task_security_struct *tsec = current->security; 2774788e7dd4SYuichi Nakamura struct file_security_struct *fsec = file->f_security; 2775788e7dd4SYuichi Nakamura struct inode_security_struct *isec = inode->i_security; 2776788e7dd4SYuichi Nakamura 2777788e7dd4SYuichi Nakamura if (!mask) { 2778788e7dd4SYuichi Nakamura /* No permission to check. Existence test. */ 2779788e7dd4SYuichi Nakamura return 0; 2780788e7dd4SYuichi Nakamura } 2781788e7dd4SYuichi Nakamura 2782788e7dd4SYuichi Nakamura if (tsec->sid == fsec->sid && fsec->isid == isec->sid 2783788e7dd4SYuichi Nakamura && fsec->pseqno == avc_policy_seqno()) 2784788e7dd4SYuichi Nakamura return selinux_netlbl_inode_permission(inode, mask); 2785788e7dd4SYuichi Nakamura 2786788e7dd4SYuichi Nakamura return selinux_revalidate_file_permission(file, mask); 2787788e7dd4SYuichi Nakamura } 2788788e7dd4SYuichi Nakamura 27891da177e4SLinus Torvalds static int selinux_file_alloc_security(struct file *file) 27901da177e4SLinus Torvalds { 27911da177e4SLinus Torvalds return file_alloc_security(file); 27921da177e4SLinus Torvalds } 27931da177e4SLinus Torvalds 27941da177e4SLinus Torvalds static void selinux_file_free_security(struct file *file) 27951da177e4SLinus Torvalds { 27961da177e4SLinus Torvalds file_free_security(file); 27971da177e4SLinus Torvalds } 27981da177e4SLinus Torvalds 27991da177e4SLinus Torvalds static int selinux_file_ioctl(struct file *file, unsigned int cmd, 28001da177e4SLinus Torvalds unsigned long arg) 28011da177e4SLinus Torvalds { 28021da177e4SLinus Torvalds int error = 0; 28031da177e4SLinus Torvalds 28041da177e4SLinus Torvalds switch (cmd) { 28051da177e4SLinus Torvalds case FIONREAD: 28061da177e4SLinus Torvalds /* fall through */ 28071da177e4SLinus Torvalds case FIBMAP: 28081da177e4SLinus Torvalds /* fall through */ 28091da177e4SLinus Torvalds case FIGETBSZ: 28101da177e4SLinus Torvalds /* fall through */ 28111da177e4SLinus Torvalds case EXT2_IOC_GETFLAGS: 28121da177e4SLinus Torvalds /* fall through */ 28131da177e4SLinus Torvalds case EXT2_IOC_GETVERSION: 28141da177e4SLinus Torvalds error = file_has_perm(current, file, FILE__GETATTR); 28151da177e4SLinus Torvalds break; 28161da177e4SLinus Torvalds 28171da177e4SLinus Torvalds case EXT2_IOC_SETFLAGS: 28181da177e4SLinus Torvalds /* fall through */ 28191da177e4SLinus Torvalds case EXT2_IOC_SETVERSION: 28201da177e4SLinus Torvalds error = file_has_perm(current, file, FILE__SETATTR); 28211da177e4SLinus Torvalds break; 28221da177e4SLinus Torvalds 28231da177e4SLinus Torvalds /* sys_ioctl() checks */ 28241da177e4SLinus Torvalds case FIONBIO: 28251da177e4SLinus Torvalds /* fall through */ 28261da177e4SLinus Torvalds case FIOASYNC: 28271da177e4SLinus Torvalds error = file_has_perm(current, file, 0); 28281da177e4SLinus Torvalds break; 28291da177e4SLinus Torvalds 28301da177e4SLinus Torvalds case KDSKBENT: 28311da177e4SLinus Torvalds case KDSKBSENT: 28321da177e4SLinus Torvalds error = task_has_capability(current,CAP_SYS_TTY_CONFIG); 28331da177e4SLinus Torvalds break; 28341da177e4SLinus Torvalds 28351da177e4SLinus Torvalds /* default case assumes that the command will go 28361da177e4SLinus Torvalds * to the file's ioctl() function. 28371da177e4SLinus Torvalds */ 28381da177e4SLinus Torvalds default: 28391da177e4SLinus Torvalds error = file_has_perm(current, file, FILE__IOCTL); 28401da177e4SLinus Torvalds 28411da177e4SLinus Torvalds } 28421da177e4SLinus Torvalds return error; 28431da177e4SLinus Torvalds } 28441da177e4SLinus Torvalds 28451da177e4SLinus Torvalds static int file_map_prot_check(struct file *file, unsigned long prot, int shared) 28461da177e4SLinus Torvalds { 28471da177e4SLinus Torvalds #ifndef CONFIG_PPC32 28481da177e4SLinus Torvalds if ((prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) { 28491da177e4SLinus Torvalds /* 28501da177e4SLinus Torvalds * We are making executable an anonymous mapping or a 28511da177e4SLinus Torvalds * private file mapping that will also be writable. 28521da177e4SLinus Torvalds * This has an additional check. 28531da177e4SLinus Torvalds */ 28541da177e4SLinus Torvalds int rc = task_has_perm(current, current, PROCESS__EXECMEM); 28551da177e4SLinus Torvalds if (rc) 28561da177e4SLinus Torvalds return rc; 28571da177e4SLinus Torvalds } 28581da177e4SLinus Torvalds #endif 28591da177e4SLinus Torvalds 28601da177e4SLinus Torvalds if (file) { 28611da177e4SLinus Torvalds /* read access is always possible with a mapping */ 28621da177e4SLinus Torvalds u32 av = FILE__READ; 28631da177e4SLinus Torvalds 28641da177e4SLinus Torvalds /* write access only matters if the mapping is shared */ 28651da177e4SLinus Torvalds if (shared && (prot & PROT_WRITE)) 28661da177e4SLinus Torvalds av |= FILE__WRITE; 28671da177e4SLinus Torvalds 28681da177e4SLinus Torvalds if (prot & PROT_EXEC) 28691da177e4SLinus Torvalds av |= FILE__EXECUTE; 28701da177e4SLinus Torvalds 28711da177e4SLinus Torvalds return file_has_perm(current, file, av); 28721da177e4SLinus Torvalds } 28731da177e4SLinus Torvalds return 0; 28741da177e4SLinus Torvalds } 28751da177e4SLinus Torvalds 28761da177e4SLinus Torvalds static int selinux_file_mmap(struct file *file, unsigned long reqprot, 2877ed032189SEric Paris unsigned long prot, unsigned long flags, 2878ed032189SEric Paris unsigned long addr, unsigned long addr_only) 28791da177e4SLinus Torvalds { 2880ed032189SEric Paris int rc = 0; 2881ed032189SEric Paris u32 sid = ((struct task_security_struct*)(current->security))->sid; 28821da177e4SLinus Torvalds 2883ed032189SEric Paris if (addr < mmap_min_addr) 2884ed032189SEric Paris rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT, 2885ed032189SEric Paris MEMPROTECT__MMAP_ZERO, NULL); 2886ed032189SEric Paris if (rc || addr_only) 28871da177e4SLinus Torvalds return rc; 28881da177e4SLinus Torvalds 28891da177e4SLinus Torvalds if (selinux_checkreqprot) 28901da177e4SLinus Torvalds prot = reqprot; 28911da177e4SLinus Torvalds 28921da177e4SLinus Torvalds return file_map_prot_check(file, prot, 28931da177e4SLinus Torvalds (flags & MAP_TYPE) == MAP_SHARED); 28941da177e4SLinus Torvalds } 28951da177e4SLinus Torvalds 28961da177e4SLinus Torvalds static int selinux_file_mprotect(struct vm_area_struct *vma, 28971da177e4SLinus Torvalds unsigned long reqprot, 28981da177e4SLinus Torvalds unsigned long prot) 28991da177e4SLinus Torvalds { 29001da177e4SLinus Torvalds int rc; 29011da177e4SLinus Torvalds 29021da177e4SLinus Torvalds rc = secondary_ops->file_mprotect(vma, reqprot, prot); 29031da177e4SLinus Torvalds if (rc) 29041da177e4SLinus Torvalds return rc; 29051da177e4SLinus Torvalds 29061da177e4SLinus Torvalds if (selinux_checkreqprot) 29071da177e4SLinus Torvalds prot = reqprot; 29081da177e4SLinus Torvalds 29091da177e4SLinus Torvalds #ifndef CONFIG_PPC32 2910db4c9641SStephen Smalley if ((prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) { 2911db4c9641SStephen Smalley rc = 0; 2912db4c9641SStephen Smalley if (vma->vm_start >= vma->vm_mm->start_brk && 2913db4c9641SStephen Smalley vma->vm_end <= vma->vm_mm->brk) { 2914db4c9641SStephen Smalley rc = task_has_perm(current, current, 2915db4c9641SStephen Smalley PROCESS__EXECHEAP); 2916db4c9641SStephen Smalley } else if (!vma->vm_file && 29176b992197SLorenzo Hernandez García-Hierro vma->vm_start <= vma->vm_mm->start_stack && 29186b992197SLorenzo Hernandez García-Hierro vma->vm_end >= vma->vm_mm->start_stack) { 29196b992197SLorenzo Hernandez García-Hierro rc = task_has_perm(current, current, PROCESS__EXECSTACK); 2920db4c9641SStephen Smalley } else if (vma->vm_file && vma->anon_vma) { 2921db4c9641SStephen Smalley /* 2922db4c9641SStephen Smalley * We are making executable a file mapping that has 2923db4c9641SStephen Smalley * had some COW done. Since pages might have been 2924db4c9641SStephen Smalley * written, check ability to execute the possibly 2925db4c9641SStephen Smalley * modified content. This typically should only 2926db4c9641SStephen Smalley * occur for text relocations. 2927db4c9641SStephen Smalley */ 2928db4c9641SStephen Smalley rc = file_has_perm(current, vma->vm_file, 2929db4c9641SStephen Smalley FILE__EXECMOD); 2930db4c9641SStephen Smalley } 29316b992197SLorenzo Hernandez García-Hierro if (rc) 29326b992197SLorenzo Hernandez García-Hierro return rc; 29336b992197SLorenzo Hernandez García-Hierro } 29341da177e4SLinus Torvalds #endif 29351da177e4SLinus Torvalds 29361da177e4SLinus Torvalds return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED); 29371da177e4SLinus Torvalds } 29381da177e4SLinus Torvalds 29391da177e4SLinus Torvalds static int selinux_file_lock(struct file *file, unsigned int cmd) 29401da177e4SLinus Torvalds { 29411da177e4SLinus Torvalds return file_has_perm(current, file, FILE__LOCK); 29421da177e4SLinus Torvalds } 29431da177e4SLinus Torvalds 29441da177e4SLinus Torvalds static int selinux_file_fcntl(struct file *file, unsigned int cmd, 29451da177e4SLinus Torvalds unsigned long arg) 29461da177e4SLinus Torvalds { 29471da177e4SLinus Torvalds int err = 0; 29481da177e4SLinus Torvalds 29491da177e4SLinus Torvalds switch (cmd) { 29501da177e4SLinus Torvalds case F_SETFL: 29513d5ff529SJosef Sipek if (!file->f_path.dentry || !file->f_path.dentry->d_inode) { 29521da177e4SLinus Torvalds err = -EINVAL; 29531da177e4SLinus Torvalds break; 29541da177e4SLinus Torvalds } 29551da177e4SLinus Torvalds 29561da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) { 29571da177e4SLinus Torvalds err = file_has_perm(current, file,FILE__WRITE); 29581da177e4SLinus Torvalds break; 29591da177e4SLinus Torvalds } 29601da177e4SLinus Torvalds /* fall through */ 29611da177e4SLinus Torvalds case F_SETOWN: 29621da177e4SLinus Torvalds case F_SETSIG: 29631da177e4SLinus Torvalds case F_GETFL: 29641da177e4SLinus Torvalds case F_GETOWN: 29651da177e4SLinus Torvalds case F_GETSIG: 29661da177e4SLinus Torvalds /* Just check FD__USE permission */ 29671da177e4SLinus Torvalds err = file_has_perm(current, file, 0); 29681da177e4SLinus Torvalds break; 29691da177e4SLinus Torvalds case F_GETLK: 29701da177e4SLinus Torvalds case F_SETLK: 29711da177e4SLinus Torvalds case F_SETLKW: 29721da177e4SLinus Torvalds #if BITS_PER_LONG == 32 29731da177e4SLinus Torvalds case F_GETLK64: 29741da177e4SLinus Torvalds case F_SETLK64: 29751da177e4SLinus Torvalds case F_SETLKW64: 29761da177e4SLinus Torvalds #endif 29773d5ff529SJosef Sipek if (!file->f_path.dentry || !file->f_path.dentry->d_inode) { 29781da177e4SLinus Torvalds err = -EINVAL; 29791da177e4SLinus Torvalds break; 29801da177e4SLinus Torvalds } 29811da177e4SLinus Torvalds err = file_has_perm(current, file, FILE__LOCK); 29821da177e4SLinus Torvalds break; 29831da177e4SLinus Torvalds } 29841da177e4SLinus Torvalds 29851da177e4SLinus Torvalds return err; 29861da177e4SLinus Torvalds } 29871da177e4SLinus Torvalds 29881da177e4SLinus Torvalds static int selinux_file_set_fowner(struct file *file) 29891da177e4SLinus Torvalds { 29901da177e4SLinus Torvalds struct task_security_struct *tsec; 29911da177e4SLinus Torvalds struct file_security_struct *fsec; 29921da177e4SLinus Torvalds 29931da177e4SLinus Torvalds tsec = current->security; 29941da177e4SLinus Torvalds fsec = file->f_security; 29951da177e4SLinus Torvalds fsec->fown_sid = tsec->sid; 29961da177e4SLinus Torvalds 29971da177e4SLinus Torvalds return 0; 29981da177e4SLinus Torvalds } 29991da177e4SLinus Torvalds 30001da177e4SLinus Torvalds static int selinux_file_send_sigiotask(struct task_struct *tsk, 30011da177e4SLinus Torvalds struct fown_struct *fown, int signum) 30021da177e4SLinus Torvalds { 30031da177e4SLinus Torvalds struct file *file; 30041da177e4SLinus Torvalds u32 perm; 30051da177e4SLinus Torvalds struct task_security_struct *tsec; 30061da177e4SLinus Torvalds struct file_security_struct *fsec; 30071da177e4SLinus Torvalds 30081da177e4SLinus Torvalds /* struct fown_struct is never outside the context of a struct file */ 3009b385a144SRobert P. J. Day file = container_of(fown, struct file, f_owner); 30101da177e4SLinus Torvalds 30111da177e4SLinus Torvalds tsec = tsk->security; 30121da177e4SLinus Torvalds fsec = file->f_security; 30131da177e4SLinus Torvalds 30141da177e4SLinus Torvalds if (!signum) 30151da177e4SLinus Torvalds perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */ 30161da177e4SLinus Torvalds else 30171da177e4SLinus Torvalds perm = signal_to_av(signum); 30181da177e4SLinus Torvalds 30191da177e4SLinus Torvalds return avc_has_perm(fsec->fown_sid, tsec->sid, 30201da177e4SLinus Torvalds SECCLASS_PROCESS, perm, NULL); 30211da177e4SLinus Torvalds } 30221da177e4SLinus Torvalds 30231da177e4SLinus Torvalds static int selinux_file_receive(struct file *file) 30241da177e4SLinus Torvalds { 30251da177e4SLinus Torvalds return file_has_perm(current, file, file_to_av(file)); 30261da177e4SLinus Torvalds } 30271da177e4SLinus Torvalds 3028788e7dd4SYuichi Nakamura static int selinux_dentry_open(struct file *file) 3029788e7dd4SYuichi Nakamura { 3030788e7dd4SYuichi Nakamura struct file_security_struct *fsec; 3031788e7dd4SYuichi Nakamura struct inode *inode; 3032788e7dd4SYuichi Nakamura struct inode_security_struct *isec; 3033788e7dd4SYuichi Nakamura inode = file->f_path.dentry->d_inode; 3034788e7dd4SYuichi Nakamura fsec = file->f_security; 3035788e7dd4SYuichi Nakamura isec = inode->i_security; 3036788e7dd4SYuichi Nakamura /* 3037788e7dd4SYuichi Nakamura * Save inode label and policy sequence number 3038788e7dd4SYuichi Nakamura * at open-time so that selinux_file_permission 3039788e7dd4SYuichi Nakamura * can determine whether revalidation is necessary. 3040788e7dd4SYuichi Nakamura * Task label is already saved in the file security 3041788e7dd4SYuichi Nakamura * struct as its SID. 3042788e7dd4SYuichi Nakamura */ 3043788e7dd4SYuichi Nakamura fsec->isid = isec->sid; 3044788e7dd4SYuichi Nakamura fsec->pseqno = avc_policy_seqno(); 3045788e7dd4SYuichi Nakamura /* 3046788e7dd4SYuichi Nakamura * Since the inode label or policy seqno may have changed 3047788e7dd4SYuichi Nakamura * between the selinux_inode_permission check and the saving 3048788e7dd4SYuichi Nakamura * of state above, recheck that access is still permitted. 3049788e7dd4SYuichi Nakamura * Otherwise, access might never be revalidated against the 3050788e7dd4SYuichi Nakamura * new inode label or new policy. 3051788e7dd4SYuichi Nakamura * This check is not redundant - do not remove. 3052788e7dd4SYuichi Nakamura */ 3053788e7dd4SYuichi Nakamura return inode_has_perm(current, inode, file_to_av(file), NULL); 3054788e7dd4SYuichi Nakamura } 3055788e7dd4SYuichi Nakamura 30561da177e4SLinus Torvalds /* task security operations */ 30571da177e4SLinus Torvalds 30581da177e4SLinus Torvalds static int selinux_task_create(unsigned long clone_flags) 30591da177e4SLinus Torvalds { 30601da177e4SLinus Torvalds int rc; 30611da177e4SLinus Torvalds 30621da177e4SLinus Torvalds rc = secondary_ops->task_create(clone_flags); 30631da177e4SLinus Torvalds if (rc) 30641da177e4SLinus Torvalds return rc; 30651da177e4SLinus Torvalds 30661da177e4SLinus Torvalds return task_has_perm(current, current, PROCESS__FORK); 30671da177e4SLinus Torvalds } 30681da177e4SLinus Torvalds 30691da177e4SLinus Torvalds static int selinux_task_alloc_security(struct task_struct *tsk) 30701da177e4SLinus Torvalds { 30711da177e4SLinus Torvalds struct task_security_struct *tsec1, *tsec2; 30721da177e4SLinus Torvalds int rc; 30731da177e4SLinus Torvalds 30741da177e4SLinus Torvalds tsec1 = current->security; 30751da177e4SLinus Torvalds 30761da177e4SLinus Torvalds rc = task_alloc_security(tsk); 30771da177e4SLinus Torvalds if (rc) 30781da177e4SLinus Torvalds return rc; 30791da177e4SLinus Torvalds tsec2 = tsk->security; 30801da177e4SLinus Torvalds 30811da177e4SLinus Torvalds tsec2->osid = tsec1->osid; 30821da177e4SLinus Torvalds tsec2->sid = tsec1->sid; 30831da177e4SLinus Torvalds 308428eba5bfSMichael LeMay /* Retain the exec, fs, key, and sock SIDs across fork */ 30851da177e4SLinus Torvalds tsec2->exec_sid = tsec1->exec_sid; 30861da177e4SLinus Torvalds tsec2->create_sid = tsec1->create_sid; 308728eba5bfSMichael LeMay tsec2->keycreate_sid = tsec1->keycreate_sid; 308842c3e03eSEric Paris tsec2->sockcreate_sid = tsec1->sockcreate_sid; 30891da177e4SLinus Torvalds 30901da177e4SLinus Torvalds /* Retain ptracer SID across fork, if any. 30911da177e4SLinus Torvalds This will be reset by the ptrace hook upon any 30921da177e4SLinus Torvalds subsequent ptrace_attach operations. */ 30931da177e4SLinus Torvalds tsec2->ptrace_sid = tsec1->ptrace_sid; 30941da177e4SLinus Torvalds 30951da177e4SLinus Torvalds return 0; 30961da177e4SLinus Torvalds } 30971da177e4SLinus Torvalds 30981da177e4SLinus Torvalds static void selinux_task_free_security(struct task_struct *tsk) 30991da177e4SLinus Torvalds { 31001da177e4SLinus Torvalds task_free_security(tsk); 31011da177e4SLinus Torvalds } 31021da177e4SLinus Torvalds 31031da177e4SLinus Torvalds static int selinux_task_setuid(uid_t id0, uid_t id1, uid_t id2, int flags) 31041da177e4SLinus Torvalds { 31051da177e4SLinus Torvalds /* Since setuid only affects the current process, and 31061da177e4SLinus Torvalds since the SELinux controls are not based on the Linux 31071da177e4SLinus Torvalds identity attributes, SELinux does not need to control 31081da177e4SLinus Torvalds this operation. However, SELinux does control the use 31091da177e4SLinus Torvalds of the CAP_SETUID and CAP_SETGID capabilities using the 31101da177e4SLinus Torvalds capable hook. */ 31111da177e4SLinus Torvalds return 0; 31121da177e4SLinus Torvalds } 31131da177e4SLinus Torvalds 31141da177e4SLinus Torvalds static int selinux_task_post_setuid(uid_t id0, uid_t id1, uid_t id2, int flags) 31151da177e4SLinus Torvalds { 31161da177e4SLinus Torvalds return secondary_ops->task_post_setuid(id0,id1,id2,flags); 31171da177e4SLinus Torvalds } 31181da177e4SLinus Torvalds 31191da177e4SLinus Torvalds static int selinux_task_setgid(gid_t id0, gid_t id1, gid_t id2, int flags) 31201da177e4SLinus Torvalds { 31211da177e4SLinus Torvalds /* See the comment for setuid above. */ 31221da177e4SLinus Torvalds return 0; 31231da177e4SLinus Torvalds } 31241da177e4SLinus Torvalds 31251da177e4SLinus Torvalds static int selinux_task_setpgid(struct task_struct *p, pid_t pgid) 31261da177e4SLinus Torvalds { 31271da177e4SLinus Torvalds return task_has_perm(current, p, PROCESS__SETPGID); 31281da177e4SLinus Torvalds } 31291da177e4SLinus Torvalds 31301da177e4SLinus Torvalds static int selinux_task_getpgid(struct task_struct *p) 31311da177e4SLinus Torvalds { 31321da177e4SLinus Torvalds return task_has_perm(current, p, PROCESS__GETPGID); 31331da177e4SLinus Torvalds } 31341da177e4SLinus Torvalds 31351da177e4SLinus Torvalds static int selinux_task_getsid(struct task_struct *p) 31361da177e4SLinus Torvalds { 31371da177e4SLinus Torvalds return task_has_perm(current, p, PROCESS__GETSESSION); 31381da177e4SLinus Torvalds } 31391da177e4SLinus Torvalds 3140f9008e4cSDavid Quigley static void selinux_task_getsecid(struct task_struct *p, u32 *secid) 3141f9008e4cSDavid Quigley { 3142f9008e4cSDavid Quigley selinux_get_task_sid(p, secid); 3143f9008e4cSDavid Quigley } 3144f9008e4cSDavid Quigley 31451da177e4SLinus Torvalds static int selinux_task_setgroups(struct group_info *group_info) 31461da177e4SLinus Torvalds { 31471da177e4SLinus Torvalds /* See the comment for setuid above. */ 31481da177e4SLinus Torvalds return 0; 31491da177e4SLinus Torvalds } 31501da177e4SLinus Torvalds 31511da177e4SLinus Torvalds static int selinux_task_setnice(struct task_struct *p, int nice) 31521da177e4SLinus Torvalds { 31531da177e4SLinus Torvalds int rc; 31541da177e4SLinus Torvalds 31551da177e4SLinus Torvalds rc = secondary_ops->task_setnice(p, nice); 31561da177e4SLinus Torvalds if (rc) 31571da177e4SLinus Torvalds return rc; 31581da177e4SLinus Torvalds 31591da177e4SLinus Torvalds return task_has_perm(current,p, PROCESS__SETSCHED); 31601da177e4SLinus Torvalds } 31611da177e4SLinus Torvalds 316203e68060SJames Morris static int selinux_task_setioprio(struct task_struct *p, int ioprio) 316303e68060SJames Morris { 3164b5376771SSerge E. Hallyn int rc; 3165b5376771SSerge E. Hallyn 3166b5376771SSerge E. Hallyn rc = secondary_ops->task_setioprio(p, ioprio); 3167b5376771SSerge E. Hallyn if (rc) 3168b5376771SSerge E. Hallyn return rc; 3169b5376771SSerge E. Hallyn 317003e68060SJames Morris return task_has_perm(current, p, PROCESS__SETSCHED); 317103e68060SJames Morris } 317203e68060SJames Morris 3173a1836a42SDavid Quigley static int selinux_task_getioprio(struct task_struct *p) 3174a1836a42SDavid Quigley { 3175a1836a42SDavid Quigley return task_has_perm(current, p, PROCESS__GETSCHED); 3176a1836a42SDavid Quigley } 3177a1836a42SDavid Quigley 31781da177e4SLinus Torvalds static int selinux_task_setrlimit(unsigned int resource, struct rlimit *new_rlim) 31791da177e4SLinus Torvalds { 31801da177e4SLinus Torvalds struct rlimit *old_rlim = current->signal->rlim + resource; 31811da177e4SLinus Torvalds int rc; 31821da177e4SLinus Torvalds 31831da177e4SLinus Torvalds rc = secondary_ops->task_setrlimit(resource, new_rlim); 31841da177e4SLinus Torvalds if (rc) 31851da177e4SLinus Torvalds return rc; 31861da177e4SLinus Torvalds 31871da177e4SLinus Torvalds /* Control the ability to change the hard limit (whether 31881da177e4SLinus Torvalds lowering or raising it), so that the hard limit can 31891da177e4SLinus Torvalds later be used as a safe reset point for the soft limit 31901da177e4SLinus Torvalds upon context transitions. See selinux_bprm_apply_creds. */ 31911da177e4SLinus Torvalds if (old_rlim->rlim_max != new_rlim->rlim_max) 31921da177e4SLinus Torvalds return task_has_perm(current, current, PROCESS__SETRLIMIT); 31931da177e4SLinus Torvalds 31941da177e4SLinus Torvalds return 0; 31951da177e4SLinus Torvalds } 31961da177e4SLinus Torvalds 31971da177e4SLinus Torvalds static int selinux_task_setscheduler(struct task_struct *p, int policy, struct sched_param *lp) 31981da177e4SLinus Torvalds { 3199b5376771SSerge E. Hallyn int rc; 3200b5376771SSerge E. Hallyn 3201b5376771SSerge E. Hallyn rc = secondary_ops->task_setscheduler(p, policy, lp); 3202b5376771SSerge E. Hallyn if (rc) 3203b5376771SSerge E. Hallyn return rc; 3204b5376771SSerge E. Hallyn 32051da177e4SLinus Torvalds return task_has_perm(current, p, PROCESS__SETSCHED); 32061da177e4SLinus Torvalds } 32071da177e4SLinus Torvalds 32081da177e4SLinus Torvalds static int selinux_task_getscheduler(struct task_struct *p) 32091da177e4SLinus Torvalds { 32101da177e4SLinus Torvalds return task_has_perm(current, p, PROCESS__GETSCHED); 32111da177e4SLinus Torvalds } 32121da177e4SLinus Torvalds 321335601547SDavid Quigley static int selinux_task_movememory(struct task_struct *p) 321435601547SDavid Quigley { 321535601547SDavid Quigley return task_has_perm(current, p, PROCESS__SETSCHED); 321635601547SDavid Quigley } 321735601547SDavid Quigley 3218f9008e4cSDavid Quigley static int selinux_task_kill(struct task_struct *p, struct siginfo *info, 3219f9008e4cSDavid Quigley int sig, u32 secid) 32201da177e4SLinus Torvalds { 32211da177e4SLinus Torvalds u32 perm; 32221da177e4SLinus Torvalds int rc; 3223f9008e4cSDavid Quigley struct task_security_struct *tsec; 32241da177e4SLinus Torvalds 3225f9008e4cSDavid Quigley rc = secondary_ops->task_kill(p, info, sig, secid); 32261da177e4SLinus Torvalds if (rc) 32271da177e4SLinus Torvalds return rc; 32281da177e4SLinus Torvalds 3229621d3121SOleg Nesterov if (info != SEND_SIG_NOINFO && (is_si_special(info) || SI_FROMKERNEL(info))) 32301da177e4SLinus Torvalds return 0; 32311da177e4SLinus Torvalds 32321da177e4SLinus Torvalds if (!sig) 32331da177e4SLinus Torvalds perm = PROCESS__SIGNULL; /* null signal; existence test */ 32341da177e4SLinus Torvalds else 32351da177e4SLinus Torvalds perm = signal_to_av(sig); 3236f9008e4cSDavid Quigley tsec = p->security; 3237f9008e4cSDavid Quigley if (secid) 3238f9008e4cSDavid Quigley rc = avc_has_perm(secid, tsec->sid, SECCLASS_PROCESS, perm, NULL); 3239f9008e4cSDavid Quigley else 3240f9008e4cSDavid Quigley rc = task_has_perm(current, p, perm); 3241f9008e4cSDavid Quigley return rc; 32421da177e4SLinus Torvalds } 32431da177e4SLinus Torvalds 32441da177e4SLinus Torvalds static int selinux_task_prctl(int option, 32451da177e4SLinus Torvalds unsigned long arg2, 32461da177e4SLinus Torvalds unsigned long arg3, 32471da177e4SLinus Torvalds unsigned long arg4, 32481da177e4SLinus Torvalds unsigned long arg5) 32491da177e4SLinus Torvalds { 32501da177e4SLinus Torvalds /* The current prctl operations do not appear to require 32511da177e4SLinus Torvalds any SELinux controls since they merely observe or modify 32521da177e4SLinus Torvalds the state of the current process. */ 32531da177e4SLinus Torvalds return 0; 32541da177e4SLinus Torvalds } 32551da177e4SLinus Torvalds 32561da177e4SLinus Torvalds static int selinux_task_wait(struct task_struct *p) 32571da177e4SLinus Torvalds { 32588a535140SEric Paris return task_has_perm(p, current, PROCESS__SIGCHLD); 32591da177e4SLinus Torvalds } 32601da177e4SLinus Torvalds 32611da177e4SLinus Torvalds static void selinux_task_reparent_to_init(struct task_struct *p) 32621da177e4SLinus Torvalds { 32631da177e4SLinus Torvalds struct task_security_struct *tsec; 32641da177e4SLinus Torvalds 32651da177e4SLinus Torvalds secondary_ops->task_reparent_to_init(p); 32661da177e4SLinus Torvalds 32671da177e4SLinus Torvalds tsec = p->security; 32681da177e4SLinus Torvalds tsec->osid = tsec->sid; 32691da177e4SLinus Torvalds tsec->sid = SECINITSID_KERNEL; 32701da177e4SLinus Torvalds return; 32711da177e4SLinus Torvalds } 32721da177e4SLinus Torvalds 32731da177e4SLinus Torvalds static void selinux_task_to_inode(struct task_struct *p, 32741da177e4SLinus Torvalds struct inode *inode) 32751da177e4SLinus Torvalds { 32761da177e4SLinus Torvalds struct task_security_struct *tsec = p->security; 32771da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 32781da177e4SLinus Torvalds 32791da177e4SLinus Torvalds isec->sid = tsec->sid; 32801da177e4SLinus Torvalds isec->initialized = 1; 32811da177e4SLinus Torvalds return; 32821da177e4SLinus Torvalds } 32831da177e4SLinus Torvalds 32841da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 328567f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv4(struct sk_buff *skb, 328667f83cbfSVenkat Yekkirala struct avc_audit_data *ad, u8 *proto) 32871da177e4SLinus Torvalds { 32881da177e4SLinus Torvalds int offset, ihlen, ret = -EINVAL; 32891da177e4SLinus Torvalds struct iphdr _iph, *ih; 32901da177e4SLinus Torvalds 3291bbe735e4SArnaldo Carvalho de Melo offset = skb_network_offset(skb); 32921da177e4SLinus Torvalds ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph); 32931da177e4SLinus Torvalds if (ih == NULL) 32941da177e4SLinus Torvalds goto out; 32951da177e4SLinus Torvalds 32961da177e4SLinus Torvalds ihlen = ih->ihl * 4; 32971da177e4SLinus Torvalds if (ihlen < sizeof(_iph)) 32981da177e4SLinus Torvalds goto out; 32991da177e4SLinus Torvalds 33001da177e4SLinus Torvalds ad->u.net.v4info.saddr = ih->saddr; 33011da177e4SLinus Torvalds ad->u.net.v4info.daddr = ih->daddr; 33021da177e4SLinus Torvalds ret = 0; 33031da177e4SLinus Torvalds 330467f83cbfSVenkat Yekkirala if (proto) 330567f83cbfSVenkat Yekkirala *proto = ih->protocol; 330667f83cbfSVenkat Yekkirala 33071da177e4SLinus Torvalds switch (ih->protocol) { 33081da177e4SLinus Torvalds case IPPROTO_TCP: { 33091da177e4SLinus Torvalds struct tcphdr _tcph, *th; 33101da177e4SLinus Torvalds 33111da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 33121da177e4SLinus Torvalds break; 33131da177e4SLinus Torvalds 33141da177e4SLinus Torvalds offset += ihlen; 33151da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 33161da177e4SLinus Torvalds if (th == NULL) 33171da177e4SLinus Torvalds break; 33181da177e4SLinus Torvalds 33191da177e4SLinus Torvalds ad->u.net.sport = th->source; 33201da177e4SLinus Torvalds ad->u.net.dport = th->dest; 33211da177e4SLinus Torvalds break; 33221da177e4SLinus Torvalds } 33231da177e4SLinus Torvalds 33241da177e4SLinus Torvalds case IPPROTO_UDP: { 33251da177e4SLinus Torvalds struct udphdr _udph, *uh; 33261da177e4SLinus Torvalds 33271da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 33281da177e4SLinus Torvalds break; 33291da177e4SLinus Torvalds 33301da177e4SLinus Torvalds offset += ihlen; 33311da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 33321da177e4SLinus Torvalds if (uh == NULL) 33331da177e4SLinus Torvalds break; 33341da177e4SLinus Torvalds 33351da177e4SLinus Torvalds ad->u.net.sport = uh->source; 33361da177e4SLinus Torvalds ad->u.net.dport = uh->dest; 33371da177e4SLinus Torvalds break; 33381da177e4SLinus Torvalds } 33391da177e4SLinus Torvalds 33402ee92d46SJames Morris case IPPROTO_DCCP: { 33412ee92d46SJames Morris struct dccp_hdr _dccph, *dh; 33422ee92d46SJames Morris 33432ee92d46SJames Morris if (ntohs(ih->frag_off) & IP_OFFSET) 33442ee92d46SJames Morris break; 33452ee92d46SJames Morris 33462ee92d46SJames Morris offset += ihlen; 33472ee92d46SJames Morris dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); 33482ee92d46SJames Morris if (dh == NULL) 33492ee92d46SJames Morris break; 33502ee92d46SJames Morris 33512ee92d46SJames Morris ad->u.net.sport = dh->dccph_sport; 33522ee92d46SJames Morris ad->u.net.dport = dh->dccph_dport; 33532ee92d46SJames Morris break; 33542ee92d46SJames Morris } 33552ee92d46SJames Morris 33561da177e4SLinus Torvalds default: 33571da177e4SLinus Torvalds break; 33581da177e4SLinus Torvalds } 33591da177e4SLinus Torvalds out: 33601da177e4SLinus Torvalds return ret; 33611da177e4SLinus Torvalds } 33621da177e4SLinus Torvalds 33631da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 33641da177e4SLinus Torvalds 33651da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 336667f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv6(struct sk_buff *skb, 336767f83cbfSVenkat Yekkirala struct avc_audit_data *ad, u8 *proto) 33681da177e4SLinus Torvalds { 33691da177e4SLinus Torvalds u8 nexthdr; 33701da177e4SLinus Torvalds int ret = -EINVAL, offset; 33711da177e4SLinus Torvalds struct ipv6hdr _ipv6h, *ip6; 33721da177e4SLinus Torvalds 3373bbe735e4SArnaldo Carvalho de Melo offset = skb_network_offset(skb); 33741da177e4SLinus Torvalds ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h); 33751da177e4SLinus Torvalds if (ip6 == NULL) 33761da177e4SLinus Torvalds goto out; 33771da177e4SLinus Torvalds 33781da177e4SLinus Torvalds ipv6_addr_copy(&ad->u.net.v6info.saddr, &ip6->saddr); 33791da177e4SLinus Torvalds ipv6_addr_copy(&ad->u.net.v6info.daddr, &ip6->daddr); 33801da177e4SLinus Torvalds ret = 0; 33811da177e4SLinus Torvalds 33821da177e4SLinus Torvalds nexthdr = ip6->nexthdr; 33831da177e4SLinus Torvalds offset += sizeof(_ipv6h); 33840d3d077cSHerbert Xu offset = ipv6_skip_exthdr(skb, offset, &nexthdr); 33851da177e4SLinus Torvalds if (offset < 0) 33861da177e4SLinus Torvalds goto out; 33871da177e4SLinus Torvalds 338867f83cbfSVenkat Yekkirala if (proto) 338967f83cbfSVenkat Yekkirala *proto = nexthdr; 339067f83cbfSVenkat Yekkirala 33911da177e4SLinus Torvalds switch (nexthdr) { 33921da177e4SLinus Torvalds case IPPROTO_TCP: { 33931da177e4SLinus Torvalds struct tcphdr _tcph, *th; 33941da177e4SLinus Torvalds 33951da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 33961da177e4SLinus Torvalds if (th == NULL) 33971da177e4SLinus Torvalds break; 33981da177e4SLinus Torvalds 33991da177e4SLinus Torvalds ad->u.net.sport = th->source; 34001da177e4SLinus Torvalds ad->u.net.dport = th->dest; 34011da177e4SLinus Torvalds break; 34021da177e4SLinus Torvalds } 34031da177e4SLinus Torvalds 34041da177e4SLinus Torvalds case IPPROTO_UDP: { 34051da177e4SLinus Torvalds struct udphdr _udph, *uh; 34061da177e4SLinus Torvalds 34071da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 34081da177e4SLinus Torvalds if (uh == NULL) 34091da177e4SLinus Torvalds break; 34101da177e4SLinus Torvalds 34111da177e4SLinus Torvalds ad->u.net.sport = uh->source; 34121da177e4SLinus Torvalds ad->u.net.dport = uh->dest; 34131da177e4SLinus Torvalds break; 34141da177e4SLinus Torvalds } 34151da177e4SLinus Torvalds 34162ee92d46SJames Morris case IPPROTO_DCCP: { 34172ee92d46SJames Morris struct dccp_hdr _dccph, *dh; 34182ee92d46SJames Morris 34192ee92d46SJames Morris dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); 34202ee92d46SJames Morris if (dh == NULL) 34212ee92d46SJames Morris break; 34222ee92d46SJames Morris 34232ee92d46SJames Morris ad->u.net.sport = dh->dccph_sport; 34242ee92d46SJames Morris ad->u.net.dport = dh->dccph_dport; 34252ee92d46SJames Morris break; 34262ee92d46SJames Morris } 34272ee92d46SJames Morris 34281da177e4SLinus Torvalds /* includes fragments */ 34291da177e4SLinus Torvalds default: 34301da177e4SLinus Torvalds break; 34311da177e4SLinus Torvalds } 34321da177e4SLinus Torvalds out: 34331da177e4SLinus Torvalds return ret; 34341da177e4SLinus Torvalds } 34351da177e4SLinus Torvalds 34361da177e4SLinus Torvalds #endif /* IPV6 */ 34371da177e4SLinus Torvalds 34381da177e4SLinus Torvalds static int selinux_parse_skb(struct sk_buff *skb, struct avc_audit_data *ad, 3439224dfbd8SPaul Moore char **addrp, int src, u8 *proto) 34401da177e4SLinus Torvalds { 34411da177e4SLinus Torvalds int ret = 0; 34421da177e4SLinus Torvalds 34431da177e4SLinus Torvalds switch (ad->u.net.family) { 34441da177e4SLinus Torvalds case PF_INET: 344567f83cbfSVenkat Yekkirala ret = selinux_parse_skb_ipv4(skb, ad, proto); 34461da177e4SLinus Torvalds if (ret || !addrp) 34471da177e4SLinus Torvalds break; 34481da177e4SLinus Torvalds *addrp = (char *)(src ? &ad->u.net.v4info.saddr : 34491da177e4SLinus Torvalds &ad->u.net.v4info.daddr); 34501da177e4SLinus Torvalds break; 34511da177e4SLinus Torvalds 34521da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 34531da177e4SLinus Torvalds case PF_INET6: 345467f83cbfSVenkat Yekkirala ret = selinux_parse_skb_ipv6(skb, ad, proto); 34551da177e4SLinus Torvalds if (ret || !addrp) 34561da177e4SLinus Torvalds break; 34571da177e4SLinus Torvalds *addrp = (char *)(src ? &ad->u.net.v6info.saddr : 34581da177e4SLinus Torvalds &ad->u.net.v6info.daddr); 34591da177e4SLinus Torvalds break; 34601da177e4SLinus Torvalds #endif /* IPV6 */ 34611da177e4SLinus Torvalds default: 34621da177e4SLinus Torvalds break; 34631da177e4SLinus Torvalds } 34641da177e4SLinus Torvalds 346571f1cb05SPaul Moore if (unlikely(ret)) 346671f1cb05SPaul Moore printk(KERN_WARNING 346771f1cb05SPaul Moore "SELinux: failure in selinux_parse_skb()," 346871f1cb05SPaul Moore " unable to parse packet\n"); 346971f1cb05SPaul Moore 34701da177e4SLinus Torvalds return ret; 34711da177e4SLinus Torvalds } 34721da177e4SLinus Torvalds 34734f6a993fSPaul Moore /** 3474220deb96SPaul Moore * selinux_skb_peerlbl_sid - Determine the peer label of a packet 34754f6a993fSPaul Moore * @skb: the packet 347675e22910SPaul Moore * @family: protocol family 3477220deb96SPaul Moore * @sid: the packet's peer label SID 34784f6a993fSPaul Moore * 34794f6a993fSPaul Moore * Description: 3480220deb96SPaul Moore * Check the various different forms of network peer labeling and determine 3481220deb96SPaul Moore * the peer label/SID for the packet; most of the magic actually occurs in 3482220deb96SPaul Moore * the security server function security_net_peersid_cmp(). The function 3483220deb96SPaul Moore * returns zero if the value in @sid is valid (although it may be SECSID_NULL) 3484220deb96SPaul Moore * or -EACCES if @sid is invalid due to inconsistencies with the different 3485220deb96SPaul Moore * peer labels. 34864f6a993fSPaul Moore * 34874f6a993fSPaul Moore */ 3488220deb96SPaul Moore static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid) 34894f6a993fSPaul Moore { 349071f1cb05SPaul Moore int err; 34914f6a993fSPaul Moore u32 xfrm_sid; 34924f6a993fSPaul Moore u32 nlbl_sid; 3493220deb96SPaul Moore u32 nlbl_type; 34944f6a993fSPaul Moore 34954f6a993fSPaul Moore selinux_skb_xfrm_sid(skb, &xfrm_sid); 34965dbe1eb0SPaul Moore selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid); 3497220deb96SPaul Moore 349871f1cb05SPaul Moore err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid); 349971f1cb05SPaul Moore if (unlikely(err)) { 350071f1cb05SPaul Moore printk(KERN_WARNING 350171f1cb05SPaul Moore "SELinux: failure in selinux_skb_peerlbl_sid()," 350271f1cb05SPaul Moore " unable to determine packet's peer label\n"); 3503220deb96SPaul Moore return -EACCES; 350471f1cb05SPaul Moore } 3505220deb96SPaul Moore 3506220deb96SPaul Moore return 0; 35074f6a993fSPaul Moore } 35084f6a993fSPaul Moore 35091da177e4SLinus Torvalds /* socket security operations */ 35101da177e4SLinus Torvalds static int socket_has_perm(struct task_struct *task, struct socket *sock, 35111da177e4SLinus Torvalds u32 perms) 35121da177e4SLinus Torvalds { 35131da177e4SLinus Torvalds struct inode_security_struct *isec; 35141da177e4SLinus Torvalds struct task_security_struct *tsec; 35151da177e4SLinus Torvalds struct avc_audit_data ad; 35161da177e4SLinus Torvalds int err = 0; 35171da177e4SLinus Torvalds 35181da177e4SLinus Torvalds tsec = task->security; 35191da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 35201da177e4SLinus Torvalds 35211da177e4SLinus Torvalds if (isec->sid == SECINITSID_KERNEL) 35221da177e4SLinus Torvalds goto out; 35231da177e4SLinus Torvalds 35241da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,NET); 35251da177e4SLinus Torvalds ad.u.net.sk = sock->sk; 35261da177e4SLinus Torvalds err = avc_has_perm(tsec->sid, isec->sid, isec->sclass, perms, &ad); 35271da177e4SLinus Torvalds 35281da177e4SLinus Torvalds out: 35291da177e4SLinus Torvalds return err; 35301da177e4SLinus Torvalds } 35311da177e4SLinus Torvalds 35321da177e4SLinus Torvalds static int selinux_socket_create(int family, int type, 35331da177e4SLinus Torvalds int protocol, int kern) 35341da177e4SLinus Torvalds { 35351da177e4SLinus Torvalds int err = 0; 35361da177e4SLinus Torvalds struct task_security_struct *tsec; 353742c3e03eSEric Paris u32 newsid; 35381da177e4SLinus Torvalds 35391da177e4SLinus Torvalds if (kern) 35401da177e4SLinus Torvalds goto out; 35411da177e4SLinus Torvalds 35421da177e4SLinus Torvalds tsec = current->security; 354342c3e03eSEric Paris newsid = tsec->sockcreate_sid ? : tsec->sid; 354442c3e03eSEric Paris err = avc_has_perm(tsec->sid, newsid, 35451da177e4SLinus Torvalds socket_type_to_security_class(family, type, 35461da177e4SLinus Torvalds protocol), SOCKET__CREATE, NULL); 35471da177e4SLinus Torvalds 35481da177e4SLinus Torvalds out: 35491da177e4SLinus Torvalds return err; 35501da177e4SLinus Torvalds } 35511da177e4SLinus Torvalds 35527420ed23SVenkat Yekkirala static int selinux_socket_post_create(struct socket *sock, int family, 35531da177e4SLinus Torvalds int type, int protocol, int kern) 35541da177e4SLinus Torvalds { 35557420ed23SVenkat Yekkirala int err = 0; 35561da177e4SLinus Torvalds struct inode_security_struct *isec; 35571da177e4SLinus Torvalds struct task_security_struct *tsec; 3558892c141eSVenkat Yekkirala struct sk_security_struct *sksec; 355942c3e03eSEric Paris u32 newsid; 35601da177e4SLinus Torvalds 35611da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 35621da177e4SLinus Torvalds 35631da177e4SLinus Torvalds tsec = current->security; 356442c3e03eSEric Paris newsid = tsec->sockcreate_sid ? : tsec->sid; 35651da177e4SLinus Torvalds isec->sclass = socket_type_to_security_class(family, type, protocol); 356642c3e03eSEric Paris isec->sid = kern ? SECINITSID_KERNEL : newsid; 35671da177e4SLinus Torvalds isec->initialized = 1; 35681da177e4SLinus Torvalds 3569892c141eSVenkat Yekkirala if (sock->sk) { 3570892c141eSVenkat Yekkirala sksec = sock->sk->sk_security; 3571892c141eSVenkat Yekkirala sksec->sid = isec->sid; 3572220deb96SPaul Moore sksec->sclass = isec->sclass; 35739f2ad665SPaul Moore err = selinux_netlbl_socket_post_create(sock); 3574892c141eSVenkat Yekkirala } 3575892c141eSVenkat Yekkirala 35767420ed23SVenkat Yekkirala return err; 35771da177e4SLinus Torvalds } 35781da177e4SLinus Torvalds 35791da177e4SLinus Torvalds /* Range of port numbers used to automatically bind. 35801da177e4SLinus Torvalds Need to determine whether we should perform a name_bind 35811da177e4SLinus Torvalds permission check between the socket and the port number. */ 35821da177e4SLinus Torvalds 35831da177e4SLinus Torvalds static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen) 35841da177e4SLinus Torvalds { 35851da177e4SLinus Torvalds u16 family; 35861da177e4SLinus Torvalds int err; 35871da177e4SLinus Torvalds 35881da177e4SLinus Torvalds err = socket_has_perm(current, sock, SOCKET__BIND); 35891da177e4SLinus Torvalds if (err) 35901da177e4SLinus Torvalds goto out; 35911da177e4SLinus Torvalds 35921da177e4SLinus Torvalds /* 35931da177e4SLinus Torvalds * If PF_INET or PF_INET6, check name_bind permission for the port. 359413402580SJames Morris * Multiple address binding for SCTP is not supported yet: we just 359513402580SJames Morris * check the first address now. 35961da177e4SLinus Torvalds */ 35971da177e4SLinus Torvalds family = sock->sk->sk_family; 35981da177e4SLinus Torvalds if (family == PF_INET || family == PF_INET6) { 35991da177e4SLinus Torvalds char *addrp; 36001da177e4SLinus Torvalds struct inode_security_struct *isec; 36011da177e4SLinus Torvalds struct task_security_struct *tsec; 36021da177e4SLinus Torvalds struct avc_audit_data ad; 36031da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 36041da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 36051da177e4SLinus Torvalds unsigned short snum; 36061da177e4SLinus Torvalds struct sock *sk = sock->sk; 36071da177e4SLinus Torvalds u32 sid, node_perm, addrlen; 36081da177e4SLinus Torvalds 36091da177e4SLinus Torvalds tsec = current->security; 36101da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 36111da177e4SLinus Torvalds 36121da177e4SLinus Torvalds if (family == PF_INET) { 36131da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 36141da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 36151da177e4SLinus Torvalds addrlen = sizeof(addr4->sin_addr.s_addr); 36161da177e4SLinus Torvalds addrp = (char *)&addr4->sin_addr.s_addr; 36171da177e4SLinus Torvalds } else { 36181da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 36191da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 36201da177e4SLinus Torvalds addrlen = sizeof(addr6->sin6_addr.s6_addr); 36211da177e4SLinus Torvalds addrp = (char *)&addr6->sin6_addr.s6_addr; 36221da177e4SLinus Torvalds } 36231da177e4SLinus Torvalds 3624227b60f5SStephen Hemminger if (snum) { 3625227b60f5SStephen Hemminger int low, high; 3626227b60f5SStephen Hemminger 3627227b60f5SStephen Hemminger inet_get_local_port_range(&low, &high); 3628227b60f5SStephen Hemminger 3629227b60f5SStephen Hemminger if (snum < max(PROT_SOCK, low) || snum > high) { 3630227b60f5SStephen Hemminger err = security_port_sid(sk->sk_family, 3631227b60f5SStephen Hemminger sk->sk_type, 3632227b60f5SStephen Hemminger sk->sk_protocol, snum, 3633227b60f5SStephen Hemminger &sid); 36341da177e4SLinus Torvalds if (err) 36351da177e4SLinus Torvalds goto out; 36361da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,NET); 36371da177e4SLinus Torvalds ad.u.net.sport = htons(snum); 36381da177e4SLinus Torvalds ad.u.net.family = family; 36391da177e4SLinus Torvalds err = avc_has_perm(isec->sid, sid, 36401da177e4SLinus Torvalds isec->sclass, 36411da177e4SLinus Torvalds SOCKET__NAME_BIND, &ad); 36421da177e4SLinus Torvalds if (err) 36431da177e4SLinus Torvalds goto out; 36441da177e4SLinus Torvalds } 3645227b60f5SStephen Hemminger } 36461da177e4SLinus Torvalds 364713402580SJames Morris switch(isec->sclass) { 364813402580SJames Morris case SECCLASS_TCP_SOCKET: 36491da177e4SLinus Torvalds node_perm = TCP_SOCKET__NODE_BIND; 36501da177e4SLinus Torvalds break; 36511da177e4SLinus Torvalds 365213402580SJames Morris case SECCLASS_UDP_SOCKET: 36531da177e4SLinus Torvalds node_perm = UDP_SOCKET__NODE_BIND; 36541da177e4SLinus Torvalds break; 36551da177e4SLinus Torvalds 36562ee92d46SJames Morris case SECCLASS_DCCP_SOCKET: 36572ee92d46SJames Morris node_perm = DCCP_SOCKET__NODE_BIND; 36582ee92d46SJames Morris break; 36592ee92d46SJames Morris 36601da177e4SLinus Torvalds default: 36611da177e4SLinus Torvalds node_perm = RAWIP_SOCKET__NODE_BIND; 36621da177e4SLinus Torvalds break; 36631da177e4SLinus Torvalds } 36641da177e4SLinus Torvalds 3665224dfbd8SPaul Moore err = sel_netnode_sid(addrp, family, &sid); 36661da177e4SLinus Torvalds if (err) 36671da177e4SLinus Torvalds goto out; 36681da177e4SLinus Torvalds 36691da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,NET); 36701da177e4SLinus Torvalds ad.u.net.sport = htons(snum); 36711da177e4SLinus Torvalds ad.u.net.family = family; 36721da177e4SLinus Torvalds 36731da177e4SLinus Torvalds if (family == PF_INET) 36741da177e4SLinus Torvalds ad.u.net.v4info.saddr = addr4->sin_addr.s_addr; 36751da177e4SLinus Torvalds else 36761da177e4SLinus Torvalds ipv6_addr_copy(&ad.u.net.v6info.saddr, &addr6->sin6_addr); 36771da177e4SLinus Torvalds 36781da177e4SLinus Torvalds err = avc_has_perm(isec->sid, sid, 36791da177e4SLinus Torvalds isec->sclass, node_perm, &ad); 36801da177e4SLinus Torvalds if (err) 36811da177e4SLinus Torvalds goto out; 36821da177e4SLinus Torvalds } 36831da177e4SLinus Torvalds out: 36841da177e4SLinus Torvalds return err; 36851da177e4SLinus Torvalds } 36861da177e4SLinus Torvalds 36871da177e4SLinus Torvalds static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen) 36881da177e4SLinus Torvalds { 36891da177e4SLinus Torvalds struct inode_security_struct *isec; 36901da177e4SLinus Torvalds int err; 36911da177e4SLinus Torvalds 36921da177e4SLinus Torvalds err = socket_has_perm(current, sock, SOCKET__CONNECT); 36931da177e4SLinus Torvalds if (err) 36941da177e4SLinus Torvalds return err; 36951da177e4SLinus Torvalds 36961da177e4SLinus Torvalds /* 36972ee92d46SJames Morris * If a TCP or DCCP socket, check name_connect permission for the port. 36981da177e4SLinus Torvalds */ 36991da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 37002ee92d46SJames Morris if (isec->sclass == SECCLASS_TCP_SOCKET || 37012ee92d46SJames Morris isec->sclass == SECCLASS_DCCP_SOCKET) { 37021da177e4SLinus Torvalds struct sock *sk = sock->sk; 37031da177e4SLinus Torvalds struct avc_audit_data ad; 37041da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 37051da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 37061da177e4SLinus Torvalds unsigned short snum; 37072ee92d46SJames Morris u32 sid, perm; 37081da177e4SLinus Torvalds 37091da177e4SLinus Torvalds if (sk->sk_family == PF_INET) { 37101da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 3711911656f8SStephen Smalley if (addrlen < sizeof(struct sockaddr_in)) 37121da177e4SLinus Torvalds return -EINVAL; 37131da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 37141da177e4SLinus Torvalds } else { 37151da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 3716911656f8SStephen Smalley if (addrlen < SIN6_LEN_RFC2133) 37171da177e4SLinus Torvalds return -EINVAL; 37181da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 37191da177e4SLinus Torvalds } 37201da177e4SLinus Torvalds 37211da177e4SLinus Torvalds err = security_port_sid(sk->sk_family, sk->sk_type, 37221da177e4SLinus Torvalds sk->sk_protocol, snum, &sid); 37231da177e4SLinus Torvalds if (err) 37241da177e4SLinus Torvalds goto out; 37251da177e4SLinus Torvalds 37262ee92d46SJames Morris perm = (isec->sclass == SECCLASS_TCP_SOCKET) ? 37272ee92d46SJames Morris TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT; 37282ee92d46SJames Morris 37291da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,NET); 37301da177e4SLinus Torvalds ad.u.net.dport = htons(snum); 37311da177e4SLinus Torvalds ad.u.net.family = sk->sk_family; 37322ee92d46SJames Morris err = avc_has_perm(isec->sid, sid, isec->sclass, perm, &ad); 37331da177e4SLinus Torvalds if (err) 37341da177e4SLinus Torvalds goto out; 37351da177e4SLinus Torvalds } 37361da177e4SLinus Torvalds 37371da177e4SLinus Torvalds out: 37381da177e4SLinus Torvalds return err; 37391da177e4SLinus Torvalds } 37401da177e4SLinus Torvalds 37411da177e4SLinus Torvalds static int selinux_socket_listen(struct socket *sock, int backlog) 37421da177e4SLinus Torvalds { 37431da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__LISTEN); 37441da177e4SLinus Torvalds } 37451da177e4SLinus Torvalds 37461da177e4SLinus Torvalds static int selinux_socket_accept(struct socket *sock, struct socket *newsock) 37471da177e4SLinus Torvalds { 37481da177e4SLinus Torvalds int err; 37491da177e4SLinus Torvalds struct inode_security_struct *isec; 37501da177e4SLinus Torvalds struct inode_security_struct *newisec; 37511da177e4SLinus Torvalds 37521da177e4SLinus Torvalds err = socket_has_perm(current, sock, SOCKET__ACCEPT); 37531da177e4SLinus Torvalds if (err) 37541da177e4SLinus Torvalds return err; 37551da177e4SLinus Torvalds 37561da177e4SLinus Torvalds newisec = SOCK_INODE(newsock)->i_security; 37571da177e4SLinus Torvalds 37581da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 37591da177e4SLinus Torvalds newisec->sclass = isec->sclass; 37601da177e4SLinus Torvalds newisec->sid = isec->sid; 37611da177e4SLinus Torvalds newisec->initialized = 1; 37621da177e4SLinus Torvalds 37631da177e4SLinus Torvalds return 0; 37641da177e4SLinus Torvalds } 37651da177e4SLinus Torvalds 37661da177e4SLinus Torvalds static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg, 37671da177e4SLinus Torvalds int size) 37681da177e4SLinus Torvalds { 37697420ed23SVenkat Yekkirala int rc; 37707420ed23SVenkat Yekkirala 37717420ed23SVenkat Yekkirala rc = socket_has_perm(current, sock, SOCKET__WRITE); 37727420ed23SVenkat Yekkirala if (rc) 37737420ed23SVenkat Yekkirala return rc; 37747420ed23SVenkat Yekkirala 37757420ed23SVenkat Yekkirala return selinux_netlbl_inode_permission(SOCK_INODE(sock), MAY_WRITE); 37761da177e4SLinus Torvalds } 37771da177e4SLinus Torvalds 37781da177e4SLinus Torvalds static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg, 37791da177e4SLinus Torvalds int size, int flags) 37801da177e4SLinus Torvalds { 37811da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__READ); 37821da177e4SLinus Torvalds } 37831da177e4SLinus Torvalds 37841da177e4SLinus Torvalds static int selinux_socket_getsockname(struct socket *sock) 37851da177e4SLinus Torvalds { 37861da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__GETATTR); 37871da177e4SLinus Torvalds } 37881da177e4SLinus Torvalds 37891da177e4SLinus Torvalds static int selinux_socket_getpeername(struct socket *sock) 37901da177e4SLinus Torvalds { 37911da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__GETATTR); 37921da177e4SLinus Torvalds } 37931da177e4SLinus Torvalds 37941da177e4SLinus Torvalds static int selinux_socket_setsockopt(struct socket *sock,int level,int optname) 37951da177e4SLinus Torvalds { 3796f8687afeSPaul Moore int err; 3797f8687afeSPaul Moore 3798f8687afeSPaul Moore err = socket_has_perm(current, sock, SOCKET__SETOPT); 3799f8687afeSPaul Moore if (err) 3800f8687afeSPaul Moore return err; 3801f8687afeSPaul Moore 3802f8687afeSPaul Moore return selinux_netlbl_socket_setsockopt(sock, level, optname); 38031da177e4SLinus Torvalds } 38041da177e4SLinus Torvalds 38051da177e4SLinus Torvalds static int selinux_socket_getsockopt(struct socket *sock, int level, 38061da177e4SLinus Torvalds int optname) 38071da177e4SLinus Torvalds { 38081da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__GETOPT); 38091da177e4SLinus Torvalds } 38101da177e4SLinus Torvalds 38111da177e4SLinus Torvalds static int selinux_socket_shutdown(struct socket *sock, int how) 38121da177e4SLinus Torvalds { 38131da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__SHUTDOWN); 38141da177e4SLinus Torvalds } 38151da177e4SLinus Torvalds 38161da177e4SLinus Torvalds static int selinux_socket_unix_stream_connect(struct socket *sock, 38171da177e4SLinus Torvalds struct socket *other, 38181da177e4SLinus Torvalds struct sock *newsk) 38191da177e4SLinus Torvalds { 38201da177e4SLinus Torvalds struct sk_security_struct *ssec; 38211da177e4SLinus Torvalds struct inode_security_struct *isec; 38221da177e4SLinus Torvalds struct inode_security_struct *other_isec; 38231da177e4SLinus Torvalds struct avc_audit_data ad; 38241da177e4SLinus Torvalds int err; 38251da177e4SLinus Torvalds 38261da177e4SLinus Torvalds err = secondary_ops->unix_stream_connect(sock, other, newsk); 38271da177e4SLinus Torvalds if (err) 38281da177e4SLinus Torvalds return err; 38291da177e4SLinus Torvalds 38301da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 38311da177e4SLinus Torvalds other_isec = SOCK_INODE(other)->i_security; 38321da177e4SLinus Torvalds 38331da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,NET); 38341da177e4SLinus Torvalds ad.u.net.sk = other->sk; 38351da177e4SLinus Torvalds 38361da177e4SLinus Torvalds err = avc_has_perm(isec->sid, other_isec->sid, 38371da177e4SLinus Torvalds isec->sclass, 38381da177e4SLinus Torvalds UNIX_STREAM_SOCKET__CONNECTTO, &ad); 38391da177e4SLinus Torvalds if (err) 38401da177e4SLinus Torvalds return err; 38411da177e4SLinus Torvalds 38421da177e4SLinus Torvalds /* connecting socket */ 38431da177e4SLinus Torvalds ssec = sock->sk->sk_security; 38441da177e4SLinus Torvalds ssec->peer_sid = other_isec->sid; 38451da177e4SLinus Torvalds 38461da177e4SLinus Torvalds /* server child socket */ 38471da177e4SLinus Torvalds ssec = newsk->sk_security; 38481da177e4SLinus Torvalds ssec->peer_sid = isec->sid; 38494237c75cSVenkat Yekkirala err = security_sid_mls_copy(other_isec->sid, ssec->peer_sid, &ssec->sid); 38501da177e4SLinus Torvalds 38514237c75cSVenkat Yekkirala return err; 38521da177e4SLinus Torvalds } 38531da177e4SLinus Torvalds 38541da177e4SLinus Torvalds static int selinux_socket_unix_may_send(struct socket *sock, 38551da177e4SLinus Torvalds struct socket *other) 38561da177e4SLinus Torvalds { 38571da177e4SLinus Torvalds struct inode_security_struct *isec; 38581da177e4SLinus Torvalds struct inode_security_struct *other_isec; 38591da177e4SLinus Torvalds struct avc_audit_data ad; 38601da177e4SLinus Torvalds int err; 38611da177e4SLinus Torvalds 38621da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 38631da177e4SLinus Torvalds other_isec = SOCK_INODE(other)->i_security; 38641da177e4SLinus Torvalds 38651da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad,NET); 38661da177e4SLinus Torvalds ad.u.net.sk = other->sk; 38671da177e4SLinus Torvalds 38681da177e4SLinus Torvalds err = avc_has_perm(isec->sid, other_isec->sid, 38691da177e4SLinus Torvalds isec->sclass, SOCKET__SENDTO, &ad); 38701da177e4SLinus Torvalds if (err) 38711da177e4SLinus Torvalds return err; 38721da177e4SLinus Torvalds 38731da177e4SLinus Torvalds return 0; 38741da177e4SLinus Torvalds } 38751da177e4SLinus Torvalds 3876effad8dfSPaul Moore static int selinux_inet_sys_rcv_skb(int ifindex, char *addrp, u16 family, 3877effad8dfSPaul Moore u32 peer_sid, 3878effad8dfSPaul Moore struct avc_audit_data *ad) 3879effad8dfSPaul Moore { 3880effad8dfSPaul Moore int err; 3881effad8dfSPaul Moore u32 if_sid; 3882effad8dfSPaul Moore u32 node_sid; 3883effad8dfSPaul Moore 3884effad8dfSPaul Moore err = sel_netif_sid(ifindex, &if_sid); 3885effad8dfSPaul Moore if (err) 3886effad8dfSPaul Moore return err; 3887effad8dfSPaul Moore err = avc_has_perm(peer_sid, if_sid, 3888effad8dfSPaul Moore SECCLASS_NETIF, NETIF__INGRESS, ad); 3889effad8dfSPaul Moore if (err) 3890effad8dfSPaul Moore return err; 3891effad8dfSPaul Moore 3892effad8dfSPaul Moore err = sel_netnode_sid(addrp, family, &node_sid); 3893effad8dfSPaul Moore if (err) 3894effad8dfSPaul Moore return err; 3895effad8dfSPaul Moore return avc_has_perm(peer_sid, node_sid, 3896effad8dfSPaul Moore SECCLASS_NODE, NODE__RECVFROM, ad); 3897effad8dfSPaul Moore } 3898effad8dfSPaul Moore 3899220deb96SPaul Moore static int selinux_sock_rcv_skb_iptables_compat(struct sock *sk, 3900220deb96SPaul Moore struct sk_buff *skb, 3901224dfbd8SPaul Moore struct avc_audit_data *ad, 3902220deb96SPaul Moore u16 family, 3903220deb96SPaul Moore char *addrp) 39041da177e4SLinus Torvalds { 3905220deb96SPaul Moore int err; 3906220deb96SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 3907220deb96SPaul Moore u16 sk_class; 3908220deb96SPaul Moore u32 netif_perm, node_perm, recv_perm; 3909220deb96SPaul Moore u32 port_sid, node_sid, if_sid, sk_sid; 39104237c75cSVenkat Yekkirala 3911220deb96SPaul Moore sk_sid = sksec->sid; 3912220deb96SPaul Moore sk_class = sksec->sclass; 39131da177e4SLinus Torvalds 3914220deb96SPaul Moore switch (sk_class) { 39151da177e4SLinus Torvalds case SECCLASS_UDP_SOCKET: 39161da177e4SLinus Torvalds netif_perm = NETIF__UDP_RECV; 39171da177e4SLinus Torvalds node_perm = NODE__UDP_RECV; 39181da177e4SLinus Torvalds recv_perm = UDP_SOCKET__RECV_MSG; 39191da177e4SLinus Torvalds break; 39201da177e4SLinus Torvalds case SECCLASS_TCP_SOCKET: 39211da177e4SLinus Torvalds netif_perm = NETIF__TCP_RECV; 39221da177e4SLinus Torvalds node_perm = NODE__TCP_RECV; 39231da177e4SLinus Torvalds recv_perm = TCP_SOCKET__RECV_MSG; 39241da177e4SLinus Torvalds break; 39252ee92d46SJames Morris case SECCLASS_DCCP_SOCKET: 39262ee92d46SJames Morris netif_perm = NETIF__DCCP_RECV; 39272ee92d46SJames Morris node_perm = NODE__DCCP_RECV; 39282ee92d46SJames Morris recv_perm = DCCP_SOCKET__RECV_MSG; 39292ee92d46SJames Morris break; 39301da177e4SLinus Torvalds default: 39311da177e4SLinus Torvalds netif_perm = NETIF__RAWIP_RECV; 39321da177e4SLinus Torvalds node_perm = NODE__RAWIP_RECV; 3933220deb96SPaul Moore recv_perm = 0; 39341da177e4SLinus Torvalds break; 39351da177e4SLinus Torvalds } 39361da177e4SLinus Torvalds 3937220deb96SPaul Moore err = sel_netif_sid(skb->iif, &if_sid); 39381da177e4SLinus Torvalds if (err) 3939220deb96SPaul Moore return err; 3940220deb96SPaul Moore err = avc_has_perm(sk_sid, if_sid, SECCLASS_NETIF, netif_perm, ad); 3941220deb96SPaul Moore if (err) 3942220deb96SPaul Moore return err; 39431da177e4SLinus Torvalds 3944224dfbd8SPaul Moore err = sel_netnode_sid(addrp, family, &node_sid); 39451da177e4SLinus Torvalds if (err) 3946220deb96SPaul Moore return err; 3947220deb96SPaul Moore err = avc_has_perm(sk_sid, node_sid, SECCLASS_NODE, node_perm, ad); 39481da177e4SLinus Torvalds if (err) 3949220deb96SPaul Moore return err; 39501da177e4SLinus Torvalds 3951220deb96SPaul Moore if (!recv_perm) 3952220deb96SPaul Moore return 0; 39531da177e4SLinus Torvalds err = security_port_sid(sk->sk_family, sk->sk_type, 39544e5ab4cbSJames Morris sk->sk_protocol, ntohs(ad->u.net.sport), 39551da177e4SLinus Torvalds &port_sid); 395671f1cb05SPaul Moore if (unlikely(err)) { 395771f1cb05SPaul Moore printk(KERN_WARNING 395871f1cb05SPaul Moore "SELinux: failure in" 395971f1cb05SPaul Moore " selinux_sock_rcv_skb_iptables_compat()," 396071f1cb05SPaul Moore " network port label not found\n"); 3961220deb96SPaul Moore return err; 396271f1cb05SPaul Moore } 3963220deb96SPaul Moore return avc_has_perm(sk_sid, port_sid, sk_class, recv_perm, ad); 39641da177e4SLinus Torvalds } 3965d28d1e08STrent Jaeger 3966220deb96SPaul Moore static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb, 3967220deb96SPaul Moore struct avc_audit_data *ad, 3968220deb96SPaul Moore u16 family, char *addrp) 3969220deb96SPaul Moore { 3970220deb96SPaul Moore int err; 3971220deb96SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 3972220deb96SPaul Moore u32 peer_sid; 3973220deb96SPaul Moore u32 sk_sid = sksec->sid; 3974220deb96SPaul Moore 3975220deb96SPaul Moore if (selinux_compat_net) 3976220deb96SPaul Moore err = selinux_sock_rcv_skb_iptables_compat(sk, skb, ad, 3977220deb96SPaul Moore family, addrp); 3978220deb96SPaul Moore else 3979220deb96SPaul Moore err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET, 3980220deb96SPaul Moore PACKET__RECV, ad); 3981220deb96SPaul Moore if (err) 3982220deb96SPaul Moore return err; 3983220deb96SPaul Moore 3984220deb96SPaul Moore if (selinux_policycap_netpeer) { 3985220deb96SPaul Moore err = selinux_skb_peerlbl_sid(skb, family, &peer_sid); 3986220deb96SPaul Moore if (err) 3987220deb96SPaul Moore return err; 3988220deb96SPaul Moore err = avc_has_perm(sk_sid, peer_sid, 3989220deb96SPaul Moore SECCLASS_PEER, PEER__RECV, ad); 3990220deb96SPaul Moore } else { 3991220deb96SPaul Moore err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, ad); 3992220deb96SPaul Moore if (err) 3993220deb96SPaul Moore return err; 3994220deb96SPaul Moore err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, ad); 3995220deb96SPaul Moore } 3996220deb96SPaul Moore 39974e5ab4cbSJames Morris return err; 39984e5ab4cbSJames Morris } 3999d28d1e08STrent Jaeger 40004e5ab4cbSJames Morris static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) 40014e5ab4cbSJames Morris { 4002220deb96SPaul Moore int err; 40034237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 4004220deb96SPaul Moore u16 family = sk->sk_family; 4005220deb96SPaul Moore u32 sk_sid = sksec->sid; 4006220deb96SPaul Moore struct avc_audit_data ad; 4007220deb96SPaul Moore char *addrp; 40084e5ab4cbSJames Morris 40094e5ab4cbSJames Morris if (family != PF_INET && family != PF_INET6) 4010220deb96SPaul Moore return 0; 40114e5ab4cbSJames Morris 40124e5ab4cbSJames Morris /* Handle mapped IPv4 packets arriving via IPv6 sockets */ 401387fcd70dSAl Viro if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 40144e5ab4cbSJames Morris family = PF_INET; 40154e5ab4cbSJames Morris 40164e5ab4cbSJames Morris AVC_AUDIT_DATA_INIT(&ad, NET); 4017da5645a2SPaul Moore ad.u.net.netif = skb->iif; 40184e5ab4cbSJames Morris ad.u.net.family = family; 4019224dfbd8SPaul Moore err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); 40204e5ab4cbSJames Morris if (err) 4021220deb96SPaul Moore return err; 40224e5ab4cbSJames Morris 4023220deb96SPaul Moore /* If any sort of compatibility mode is enabled then handoff processing 4024220deb96SPaul Moore * to the selinux_sock_rcv_skb_compat() function to deal with the 4025220deb96SPaul Moore * special handling. We do this in an attempt to keep this function 4026220deb96SPaul Moore * as fast and as clean as possible. */ 4027220deb96SPaul Moore if (selinux_compat_net || !selinux_policycap_netpeer) 4028220deb96SPaul Moore return selinux_sock_rcv_skb_compat(sk, skb, &ad, 4029220deb96SPaul Moore family, addrp); 4030220deb96SPaul Moore 4031d621d35eSPaul Moore if (netlbl_enabled() || selinux_xfrm_enabled()) { 4032d621d35eSPaul Moore u32 peer_sid; 4033220deb96SPaul Moore 4034220deb96SPaul Moore err = selinux_skb_peerlbl_sid(skb, family, &peer_sid); 4035220deb96SPaul Moore if (err) 4036220deb96SPaul Moore return err; 4037effad8dfSPaul Moore err = selinux_inet_sys_rcv_skb(skb->iif, addrp, family, 4038effad8dfSPaul Moore peer_sid, &ad); 4039effad8dfSPaul Moore if (err) 4040effad8dfSPaul Moore return err; 4041d621d35eSPaul Moore err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER, 4042d621d35eSPaul Moore PEER__RECV, &ad); 4043d621d35eSPaul Moore } 4044d621d35eSPaul Moore 4045effad8dfSPaul Moore if (selinux_secmark_enabled()) { 4046effad8dfSPaul Moore err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET, 4047effad8dfSPaul Moore PACKET__RECV, &ad); 4048effad8dfSPaul Moore if (err) 4049effad8dfSPaul Moore return err; 4050effad8dfSPaul Moore } 4051effad8dfSPaul Moore 4052d621d35eSPaul Moore return err; 40531da177e4SLinus Torvalds } 40541da177e4SLinus Torvalds 40552c7946a7SCatherine Zhang static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval, 40561da177e4SLinus Torvalds int __user *optlen, unsigned len) 40571da177e4SLinus Torvalds { 40581da177e4SLinus Torvalds int err = 0; 40591da177e4SLinus Torvalds char *scontext; 40601da177e4SLinus Torvalds u32 scontext_len; 40611da177e4SLinus Torvalds struct sk_security_struct *ssec; 40621da177e4SLinus Torvalds struct inode_security_struct *isec; 40633de4bab5SPaul Moore u32 peer_sid = SECSID_NULL; 40641da177e4SLinus Torvalds 40651da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 40662c7946a7SCatherine Zhang 40673de4bab5SPaul Moore if (isec->sclass == SECCLASS_UNIX_STREAM_SOCKET || 40683de4bab5SPaul Moore isec->sclass == SECCLASS_TCP_SOCKET) { 40696b877699SVenkat Yekkirala ssec = sock->sk->sk_security; 40706b877699SVenkat Yekkirala peer_sid = ssec->peer_sid; 40716b877699SVenkat Yekkirala } 40722c7946a7SCatherine Zhang if (peer_sid == SECSID_NULL) { 40732c7946a7SCatherine Zhang err = -ENOPROTOOPT; 40742c7946a7SCatherine Zhang goto out; 40752c7946a7SCatherine Zhang } 40761da177e4SLinus Torvalds 40772c7946a7SCatherine Zhang err = security_sid_to_context(peer_sid, &scontext, &scontext_len); 40781da177e4SLinus Torvalds 40791da177e4SLinus Torvalds if (err) 40801da177e4SLinus Torvalds goto out; 40811da177e4SLinus Torvalds 40821da177e4SLinus Torvalds if (scontext_len > len) { 40831da177e4SLinus Torvalds err = -ERANGE; 40841da177e4SLinus Torvalds goto out_len; 40851da177e4SLinus Torvalds } 40861da177e4SLinus Torvalds 40871da177e4SLinus Torvalds if (copy_to_user(optval, scontext, scontext_len)) 40881da177e4SLinus Torvalds err = -EFAULT; 40891da177e4SLinus Torvalds 40901da177e4SLinus Torvalds out_len: 40911da177e4SLinus Torvalds if (put_user(scontext_len, optlen)) 40921da177e4SLinus Torvalds err = -EFAULT; 40931da177e4SLinus Torvalds 40941da177e4SLinus Torvalds kfree(scontext); 40951da177e4SLinus Torvalds out: 40961da177e4SLinus Torvalds return err; 40971da177e4SLinus Torvalds } 40981da177e4SLinus Torvalds 4099dc49c1f9SCatherine Zhang static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid) 41002c7946a7SCatherine Zhang { 4101dc49c1f9SCatherine Zhang u32 peer_secid = SECSID_NULL; 410275e22910SPaul Moore u16 family; 4103877ce7c1SCatherine Zhang 410475e22910SPaul Moore if (sock) 410575e22910SPaul Moore family = sock->sk->sk_family; 410675e22910SPaul Moore else if (skb && skb->sk) 410775e22910SPaul Moore family = skb->sk->sk_family; 410875e22910SPaul Moore else 410975e22910SPaul Moore goto out; 411075e22910SPaul Moore 411175e22910SPaul Moore if (sock && family == PF_UNIX) 4112dc49c1f9SCatherine Zhang selinux_get_inode_sid(SOCK_INODE(sock), &peer_secid); 41133de4bab5SPaul Moore else if (skb) 4114220deb96SPaul Moore selinux_skb_peerlbl_sid(skb, family, &peer_secid); 41152c7946a7SCatherine Zhang 411675e22910SPaul Moore out: 4117dc49c1f9SCatherine Zhang *secid = peer_secid; 411875e22910SPaul Moore if (peer_secid == SECSID_NULL) 411975e22910SPaul Moore return -EINVAL; 412075e22910SPaul Moore return 0; 41212c7946a7SCatherine Zhang } 41222c7946a7SCatherine Zhang 41237d877f3bSAl Viro static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority) 41241da177e4SLinus Torvalds { 41251da177e4SLinus Torvalds return sk_alloc_security(sk, family, priority); 41261da177e4SLinus Torvalds } 41271da177e4SLinus Torvalds 41281da177e4SLinus Torvalds static void selinux_sk_free_security(struct sock *sk) 41291da177e4SLinus Torvalds { 41301da177e4SLinus Torvalds sk_free_security(sk); 41311da177e4SLinus Torvalds } 41321da177e4SLinus Torvalds 4133892c141eSVenkat Yekkirala static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk) 4134892c141eSVenkat Yekkirala { 4135892c141eSVenkat Yekkirala struct sk_security_struct *ssec = sk->sk_security; 4136892c141eSVenkat Yekkirala struct sk_security_struct *newssec = newsk->sk_security; 4137892c141eSVenkat Yekkirala 4138892c141eSVenkat Yekkirala newssec->sid = ssec->sid; 4139892c141eSVenkat Yekkirala newssec->peer_sid = ssec->peer_sid; 4140220deb96SPaul Moore newssec->sclass = ssec->sclass; 414199f59ed0SPaul Moore 41429f2ad665SPaul Moore selinux_netlbl_sk_security_clone(ssec, newssec); 4143892c141eSVenkat Yekkirala } 4144892c141eSVenkat Yekkirala 4145beb8d13bSVenkat Yekkirala static void selinux_sk_getsecid(struct sock *sk, u32 *secid) 4146d28d1e08STrent Jaeger { 4147d28d1e08STrent Jaeger if (!sk) 4148beb8d13bSVenkat Yekkirala *secid = SECINITSID_ANY_SOCKET; 4149892c141eSVenkat Yekkirala else { 4150892c141eSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 4151d28d1e08STrent Jaeger 4152beb8d13bSVenkat Yekkirala *secid = sksec->sid; 4153892c141eSVenkat Yekkirala } 4154d28d1e08STrent Jaeger } 4155d28d1e08STrent Jaeger 41569a673e56SAdrian Bunk static void selinux_sock_graft(struct sock* sk, struct socket *parent) 41574237c75cSVenkat Yekkirala { 41584237c75cSVenkat Yekkirala struct inode_security_struct *isec = SOCK_INODE(parent)->i_security; 41594237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 41604237c75cSVenkat Yekkirala 41612148ccc4SDavid Woodhouse if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 || 41622148ccc4SDavid Woodhouse sk->sk_family == PF_UNIX) 41634237c75cSVenkat Yekkirala isec->sid = sksec->sid; 4164220deb96SPaul Moore sksec->sclass = isec->sclass; 41657420ed23SVenkat Yekkirala 41667420ed23SVenkat Yekkirala selinux_netlbl_sock_graft(sk, parent); 41674237c75cSVenkat Yekkirala } 41684237c75cSVenkat Yekkirala 41699a673e56SAdrian Bunk static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb, 41704237c75cSVenkat Yekkirala struct request_sock *req) 41714237c75cSVenkat Yekkirala { 41724237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 41734237c75cSVenkat Yekkirala int err; 41747420ed23SVenkat Yekkirala u32 newsid; 41754237c75cSVenkat Yekkirala u32 peersid; 41764237c75cSVenkat Yekkirala 4177220deb96SPaul Moore err = selinux_skb_peerlbl_sid(skb, sk->sk_family, &peersid); 4178220deb96SPaul Moore if (err) 4179220deb96SPaul Moore return err; 4180a51c64f1SVenkat Yekkirala if (peersid == SECSID_NULL) { 4181a51c64f1SVenkat Yekkirala req->secid = sksec->sid; 41823de4bab5SPaul Moore req->peer_secid = SECSID_NULL; 4183a51c64f1SVenkat Yekkirala return 0; 4184a51c64f1SVenkat Yekkirala } 4185a51c64f1SVenkat Yekkirala 41864237c75cSVenkat Yekkirala err = security_sid_mls_copy(sksec->sid, peersid, &newsid); 41874237c75cSVenkat Yekkirala if (err) 41884237c75cSVenkat Yekkirala return err; 41894237c75cSVenkat Yekkirala 41904237c75cSVenkat Yekkirala req->secid = newsid; 41916b877699SVenkat Yekkirala req->peer_secid = peersid; 41924237c75cSVenkat Yekkirala return 0; 41934237c75cSVenkat Yekkirala } 41944237c75cSVenkat Yekkirala 41959a673e56SAdrian Bunk static void selinux_inet_csk_clone(struct sock *newsk, 41969a673e56SAdrian Bunk const struct request_sock *req) 41974237c75cSVenkat Yekkirala { 41984237c75cSVenkat Yekkirala struct sk_security_struct *newsksec = newsk->sk_security; 41994237c75cSVenkat Yekkirala 42004237c75cSVenkat Yekkirala newsksec->sid = req->secid; 42016b877699SVenkat Yekkirala newsksec->peer_sid = req->peer_secid; 42024237c75cSVenkat Yekkirala /* NOTE: Ideally, we should also get the isec->sid for the 42034237c75cSVenkat Yekkirala new socket in sync, but we don't have the isec available yet. 42044237c75cSVenkat Yekkirala So we will wait until sock_graft to do it, by which 42054237c75cSVenkat Yekkirala time it will have been created and available. */ 420699f59ed0SPaul Moore 42079f2ad665SPaul Moore /* We don't need to take any sort of lock here as we are the only 42089f2ad665SPaul Moore * thread with access to newsksec */ 42099f2ad665SPaul Moore selinux_netlbl_sk_security_reset(newsksec, req->rsk_ops->family); 42104237c75cSVenkat Yekkirala } 42114237c75cSVenkat Yekkirala 42126b877699SVenkat Yekkirala static void selinux_inet_conn_established(struct sock *sk, 42136b877699SVenkat Yekkirala struct sk_buff *skb) 42146b877699SVenkat Yekkirala { 42156b877699SVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 42166b877699SVenkat Yekkirala 4217220deb96SPaul Moore selinux_skb_peerlbl_sid(skb, sk->sk_family, &sksec->peer_sid); 42186b877699SVenkat Yekkirala } 42196b877699SVenkat Yekkirala 42209a673e56SAdrian Bunk static void selinux_req_classify_flow(const struct request_sock *req, 42219a673e56SAdrian Bunk struct flowi *fl) 42224237c75cSVenkat Yekkirala { 42234237c75cSVenkat Yekkirala fl->secid = req->secid; 42244237c75cSVenkat Yekkirala } 42254237c75cSVenkat Yekkirala 42261da177e4SLinus Torvalds static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb) 42271da177e4SLinus Torvalds { 42281da177e4SLinus Torvalds int err = 0; 42291da177e4SLinus Torvalds u32 perm; 42301da177e4SLinus Torvalds struct nlmsghdr *nlh; 42311da177e4SLinus Torvalds struct socket *sock = sk->sk_socket; 42321da177e4SLinus Torvalds struct inode_security_struct *isec = SOCK_INODE(sock)->i_security; 42331da177e4SLinus Torvalds 42341da177e4SLinus Torvalds if (skb->len < NLMSG_SPACE(0)) { 42351da177e4SLinus Torvalds err = -EINVAL; 42361da177e4SLinus Torvalds goto out; 42371da177e4SLinus Torvalds } 4238b529ccf2SArnaldo Carvalho de Melo nlh = nlmsg_hdr(skb); 42391da177e4SLinus Torvalds 42401da177e4SLinus Torvalds err = selinux_nlmsg_lookup(isec->sclass, nlh->nlmsg_type, &perm); 42411da177e4SLinus Torvalds if (err) { 42421da177e4SLinus Torvalds if (err == -EINVAL) { 42439ad9ad38SDavid Woodhouse audit_log(current->audit_context, GFP_KERNEL, AUDIT_SELINUX_ERR, 42441da177e4SLinus Torvalds "SELinux: unrecognized netlink message" 42451da177e4SLinus Torvalds " type=%hu for sclass=%hu\n", 42461da177e4SLinus Torvalds nlh->nlmsg_type, isec->sclass); 42471da177e4SLinus Torvalds if (!selinux_enforcing) 42481da177e4SLinus Torvalds err = 0; 42491da177e4SLinus Torvalds } 42501da177e4SLinus Torvalds 42511da177e4SLinus Torvalds /* Ignore */ 42521da177e4SLinus Torvalds if (err == -ENOENT) 42531da177e4SLinus Torvalds err = 0; 42541da177e4SLinus Torvalds goto out; 42551da177e4SLinus Torvalds } 42561da177e4SLinus Torvalds 42571da177e4SLinus Torvalds err = socket_has_perm(current, sock, perm); 42581da177e4SLinus Torvalds out: 42591da177e4SLinus Torvalds return err; 42601da177e4SLinus Torvalds } 42611da177e4SLinus Torvalds 42621da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER 42631da177e4SLinus Torvalds 4264effad8dfSPaul Moore static unsigned int selinux_ip_forward(struct sk_buff *skb, int ifindex, 4265effad8dfSPaul Moore u16 family) 42661da177e4SLinus Torvalds { 4267effad8dfSPaul Moore char *addrp; 4268effad8dfSPaul Moore u32 peer_sid; 4269effad8dfSPaul Moore struct avc_audit_data ad; 4270effad8dfSPaul Moore u8 secmark_active; 4271effad8dfSPaul Moore u8 peerlbl_active; 42724237c75cSVenkat Yekkirala 4273effad8dfSPaul Moore if (!selinux_policycap_netpeer) 4274effad8dfSPaul Moore return NF_ACCEPT; 42754237c75cSVenkat Yekkirala 4276effad8dfSPaul Moore secmark_active = selinux_secmark_enabled(); 4277effad8dfSPaul Moore peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled(); 4278effad8dfSPaul Moore if (!secmark_active && !peerlbl_active) 4279effad8dfSPaul Moore return NF_ACCEPT; 42804237c75cSVenkat Yekkirala 4281effad8dfSPaul Moore AVC_AUDIT_DATA_INIT(&ad, NET); 4282effad8dfSPaul Moore ad.u.net.netif = ifindex; 4283effad8dfSPaul Moore ad.u.net.family = family; 4284effad8dfSPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0) 4285effad8dfSPaul Moore return NF_DROP; 42861da177e4SLinus Torvalds 4287effad8dfSPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0) 4288effad8dfSPaul Moore return NF_DROP; 42891da177e4SLinus Torvalds 4290effad8dfSPaul Moore if (peerlbl_active) 4291effad8dfSPaul Moore if (selinux_inet_sys_rcv_skb(ifindex, addrp, family, 4292effad8dfSPaul Moore peer_sid, &ad) != 0) 4293effad8dfSPaul Moore return NF_DROP; 4294effad8dfSPaul Moore 4295effad8dfSPaul Moore if (secmark_active) 4296effad8dfSPaul Moore if (avc_has_perm(peer_sid, skb->secmark, 4297effad8dfSPaul Moore SECCLASS_PACKET, PACKET__FORWARD_IN, &ad)) 4298effad8dfSPaul Moore return NF_DROP; 4299effad8dfSPaul Moore 4300effad8dfSPaul Moore return NF_ACCEPT; 4301effad8dfSPaul Moore } 4302effad8dfSPaul Moore 4303effad8dfSPaul Moore static unsigned int selinux_ipv4_forward(unsigned int hooknum, 4304effad8dfSPaul Moore struct sk_buff *skb, 4305effad8dfSPaul Moore const struct net_device *in, 4306effad8dfSPaul Moore const struct net_device *out, 4307effad8dfSPaul Moore int (*okfn)(struct sk_buff *)) 4308effad8dfSPaul Moore { 4309effad8dfSPaul Moore return selinux_ip_forward(skb, in->ifindex, PF_INET); 4310effad8dfSPaul Moore } 4311effad8dfSPaul Moore 4312effad8dfSPaul Moore #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 4313effad8dfSPaul Moore static unsigned int selinux_ipv6_forward(unsigned int hooknum, 4314effad8dfSPaul Moore struct sk_buff *skb, 4315effad8dfSPaul Moore const struct net_device *in, 4316effad8dfSPaul Moore const struct net_device *out, 4317effad8dfSPaul Moore int (*okfn)(struct sk_buff *)) 4318effad8dfSPaul Moore { 4319effad8dfSPaul Moore return selinux_ip_forward(skb, in->ifindex, PF_INET6); 4320effad8dfSPaul Moore } 4321effad8dfSPaul Moore #endif /* IPV6 */ 4322effad8dfSPaul Moore 4323effad8dfSPaul Moore static int selinux_ip_postroute_iptables_compat(struct sock *sk, 4324effad8dfSPaul Moore int ifindex, 4325effad8dfSPaul Moore struct avc_audit_data *ad, 4326effad8dfSPaul Moore u16 family, char *addrp) 4327effad8dfSPaul Moore { 4328effad8dfSPaul Moore int err; 4329effad8dfSPaul Moore struct sk_security_struct *sksec = sk->sk_security; 4330effad8dfSPaul Moore u16 sk_class; 4331effad8dfSPaul Moore u32 netif_perm, node_perm, send_perm; 4332effad8dfSPaul Moore u32 port_sid, node_sid, if_sid, sk_sid; 4333effad8dfSPaul Moore 4334effad8dfSPaul Moore sk_sid = sksec->sid; 4335effad8dfSPaul Moore sk_class = sksec->sclass; 4336effad8dfSPaul Moore 4337effad8dfSPaul Moore switch (sk_class) { 43381da177e4SLinus Torvalds case SECCLASS_UDP_SOCKET: 43391da177e4SLinus Torvalds netif_perm = NETIF__UDP_SEND; 43401da177e4SLinus Torvalds node_perm = NODE__UDP_SEND; 43411da177e4SLinus Torvalds send_perm = UDP_SOCKET__SEND_MSG; 43421da177e4SLinus Torvalds break; 43431da177e4SLinus Torvalds case SECCLASS_TCP_SOCKET: 43441da177e4SLinus Torvalds netif_perm = NETIF__TCP_SEND; 43451da177e4SLinus Torvalds node_perm = NODE__TCP_SEND; 43461da177e4SLinus Torvalds send_perm = TCP_SOCKET__SEND_MSG; 43471da177e4SLinus Torvalds break; 43482ee92d46SJames Morris case SECCLASS_DCCP_SOCKET: 43492ee92d46SJames Morris netif_perm = NETIF__DCCP_SEND; 43502ee92d46SJames Morris node_perm = NODE__DCCP_SEND; 43512ee92d46SJames Morris send_perm = DCCP_SOCKET__SEND_MSG; 43522ee92d46SJames Morris break; 43531da177e4SLinus Torvalds default: 43541da177e4SLinus Torvalds netif_perm = NETIF__RAWIP_SEND; 43551da177e4SLinus Torvalds node_perm = NODE__RAWIP_SEND; 4356effad8dfSPaul Moore send_perm = 0; 43571da177e4SLinus Torvalds break; 43581da177e4SLinus Torvalds } 43591da177e4SLinus Torvalds 4360effad8dfSPaul Moore err = sel_netif_sid(ifindex, &if_sid); 43614e5ab4cbSJames Morris if (err) 4362effad8dfSPaul Moore return err; 4363effad8dfSPaul Moore err = avc_has_perm(sk_sid, if_sid, SECCLASS_NETIF, netif_perm, ad); 4364effad8dfSPaul Moore return err; 43651da177e4SLinus Torvalds 4366224dfbd8SPaul Moore err = sel_netnode_sid(addrp, family, &node_sid); 43674e5ab4cbSJames Morris if (err) 4368effad8dfSPaul Moore return err; 4369effad8dfSPaul Moore err = avc_has_perm(sk_sid, node_sid, SECCLASS_NODE, node_perm, ad); 43704e5ab4cbSJames Morris if (err) 4371effad8dfSPaul Moore return err; 43721da177e4SLinus Torvalds 4373effad8dfSPaul Moore if (send_perm != 0) 4374effad8dfSPaul Moore return 0; 43751da177e4SLinus Torvalds 4376effad8dfSPaul Moore err = security_port_sid(sk->sk_family, sk->sk_type, 4377effad8dfSPaul Moore sk->sk_protocol, ntohs(ad->u.net.dport), 43784e5ab4cbSJames Morris &port_sid); 437971f1cb05SPaul Moore if (unlikely(err)) { 438071f1cb05SPaul Moore printk(KERN_WARNING 438171f1cb05SPaul Moore "SELinux: failure in" 438271f1cb05SPaul Moore " selinux_ip_postroute_iptables_compat()," 438371f1cb05SPaul Moore " network port label not found\n"); 43844e5ab4cbSJames Morris return err; 438571f1cb05SPaul Moore } 4386effad8dfSPaul Moore return avc_has_perm(sk_sid, port_sid, sk_class, send_perm, ad); 43871da177e4SLinus Torvalds } 43881da177e4SLinus Torvalds 4389effad8dfSPaul Moore static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb, 4390effad8dfSPaul Moore int ifindex, 4391effad8dfSPaul Moore struct avc_audit_data *ad, 4392effad8dfSPaul Moore u16 family, 4393effad8dfSPaul Moore char *addrp, 4394effad8dfSPaul Moore u8 proto) 43954e5ab4cbSJames Morris { 4396effad8dfSPaul Moore struct sock *sk = skb->sk; 43974237c75cSVenkat Yekkirala struct sk_security_struct *sksec; 43984e5ab4cbSJames Morris 4399effad8dfSPaul Moore if (sk == NULL) 4400effad8dfSPaul Moore return NF_ACCEPT; 44014237c75cSVenkat Yekkirala sksec = sk->sk_security; 44024e5ab4cbSJames Morris 4403effad8dfSPaul Moore if (selinux_compat_net) { 4404effad8dfSPaul Moore if (selinux_ip_postroute_iptables_compat(skb->sk, ifindex, 4405effad8dfSPaul Moore ad, family, addrp)) 4406effad8dfSPaul Moore return NF_DROP; 4407effad8dfSPaul Moore } else { 4408effad8dfSPaul Moore if (avc_has_perm(sksec->sid, skb->secmark, 4409effad8dfSPaul Moore SECCLASS_PACKET, PACKET__SEND, ad)) 4410effad8dfSPaul Moore return NF_DROP; 44111da177e4SLinus Torvalds } 44121da177e4SLinus Torvalds 4413effad8dfSPaul Moore if (selinux_policycap_netpeer) 4414effad8dfSPaul Moore if (selinux_xfrm_postroute_last(sksec->sid, skb, ad, proto)) 4415effad8dfSPaul Moore return NF_DROP; 4416effad8dfSPaul Moore 4417effad8dfSPaul Moore return NF_ACCEPT; 4418effad8dfSPaul Moore } 4419effad8dfSPaul Moore 4420effad8dfSPaul Moore static unsigned int selinux_ip_postroute(struct sk_buff *skb, int ifindex, 4421effad8dfSPaul Moore u16 family) 4422effad8dfSPaul Moore { 4423effad8dfSPaul Moore u32 secmark_perm; 4424effad8dfSPaul Moore u32 peer_sid; 4425effad8dfSPaul Moore struct sock *sk; 4426effad8dfSPaul Moore struct avc_audit_data ad; 4427effad8dfSPaul Moore char *addrp; 4428effad8dfSPaul Moore u8 proto; 4429effad8dfSPaul Moore u8 secmark_active; 4430effad8dfSPaul Moore u8 peerlbl_active; 4431effad8dfSPaul Moore 4432effad8dfSPaul Moore AVC_AUDIT_DATA_INIT(&ad, NET); 4433effad8dfSPaul Moore ad.u.net.netif = ifindex; 4434effad8dfSPaul Moore ad.u.net.family = family; 4435effad8dfSPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto)) 4436effad8dfSPaul Moore return NF_DROP; 4437effad8dfSPaul Moore 4438effad8dfSPaul Moore /* If any sort of compatibility mode is enabled then handoff processing 4439effad8dfSPaul Moore * to the selinux_ip_postroute_compat() function to deal with the 4440effad8dfSPaul Moore * special handling. We do this in an attempt to keep this function 4441effad8dfSPaul Moore * as fast and as clean as possible. */ 4442effad8dfSPaul Moore if (selinux_compat_net || !selinux_policycap_netpeer) 4443effad8dfSPaul Moore return selinux_ip_postroute_compat(skb, ifindex, &ad, 4444effad8dfSPaul Moore family, addrp, proto); 4445effad8dfSPaul Moore 4446effad8dfSPaul Moore /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec 4447effad8dfSPaul Moore * packet transformation so allow the packet to pass without any checks 4448effad8dfSPaul Moore * since we'll have another chance to perform access control checks 4449effad8dfSPaul Moore * when the packet is on it's final way out. 4450effad8dfSPaul Moore * NOTE: there appear to be some IPv6 multicast cases where skb->dst 4451effad8dfSPaul Moore * is NULL, in this case go ahead and apply access control. */ 4452effad8dfSPaul Moore if (skb->dst != NULL && skb->dst->xfrm != NULL) 4453effad8dfSPaul Moore return NF_ACCEPT; 4454effad8dfSPaul Moore 4455effad8dfSPaul Moore secmark_active = selinux_secmark_enabled(); 4456effad8dfSPaul Moore peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled(); 4457effad8dfSPaul Moore if (!secmark_active && !peerlbl_active) 4458effad8dfSPaul Moore return NF_ACCEPT; 4459effad8dfSPaul Moore 4460effad8dfSPaul Moore /* if the packet is locally generated (skb->sk != NULL) then use the 4461effad8dfSPaul Moore * socket's label as the peer label, otherwise the packet is being 4462effad8dfSPaul Moore * forwarded through this system and we need to fetch the peer label 4463effad8dfSPaul Moore * directly from the packet */ 4464effad8dfSPaul Moore sk = skb->sk; 4465effad8dfSPaul Moore if (sk) { 4466effad8dfSPaul Moore struct sk_security_struct *sksec = sk->sk_security; 4467effad8dfSPaul Moore peer_sid = sksec->sid; 4468effad8dfSPaul Moore secmark_perm = PACKET__SEND; 4469effad8dfSPaul Moore } else { 4470effad8dfSPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &peer_sid)) 4471effad8dfSPaul Moore return NF_DROP; 4472effad8dfSPaul Moore secmark_perm = PACKET__FORWARD_OUT; 4473effad8dfSPaul Moore } 4474effad8dfSPaul Moore 4475effad8dfSPaul Moore if (secmark_active) 4476effad8dfSPaul Moore if (avc_has_perm(peer_sid, skb->secmark, 4477effad8dfSPaul Moore SECCLASS_PACKET, secmark_perm, &ad)) 4478effad8dfSPaul Moore return NF_DROP; 4479effad8dfSPaul Moore 4480effad8dfSPaul Moore if (peerlbl_active) { 4481effad8dfSPaul Moore u32 if_sid; 4482effad8dfSPaul Moore u32 node_sid; 4483effad8dfSPaul Moore 4484effad8dfSPaul Moore if (sel_netif_sid(ifindex, &if_sid)) 4485effad8dfSPaul Moore return NF_DROP; 4486effad8dfSPaul Moore if (avc_has_perm(peer_sid, if_sid, 4487effad8dfSPaul Moore SECCLASS_NETIF, NETIF__EGRESS, &ad)) 4488effad8dfSPaul Moore return NF_DROP; 4489effad8dfSPaul Moore 4490effad8dfSPaul Moore if (sel_netnode_sid(addrp, family, &node_sid)) 4491effad8dfSPaul Moore return NF_DROP; 4492effad8dfSPaul Moore if (avc_has_perm(peer_sid, node_sid, 4493effad8dfSPaul Moore SECCLASS_NODE, NODE__SENDTO, &ad)) 4494effad8dfSPaul Moore return NF_DROP; 4495effad8dfSPaul Moore } 4496effad8dfSPaul Moore 4497effad8dfSPaul Moore return NF_ACCEPT; 4498effad8dfSPaul Moore } 4499effad8dfSPaul Moore 4500effad8dfSPaul Moore static unsigned int selinux_ipv4_postroute(unsigned int hooknum, 4501a224be76SDavid S. Miller struct sk_buff *skb, 45021da177e4SLinus Torvalds const struct net_device *in, 45031da177e4SLinus Torvalds const struct net_device *out, 45041da177e4SLinus Torvalds int (*okfn)(struct sk_buff *)) 45051da177e4SLinus Torvalds { 4506effad8dfSPaul Moore return selinux_ip_postroute(skb, out->ifindex, PF_INET); 45071da177e4SLinus Torvalds } 45081da177e4SLinus Torvalds 45091da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 4510effad8dfSPaul Moore static unsigned int selinux_ipv6_postroute(unsigned int hooknum, 4511a224be76SDavid S. Miller struct sk_buff *skb, 45121da177e4SLinus Torvalds const struct net_device *in, 45131da177e4SLinus Torvalds const struct net_device *out, 45141da177e4SLinus Torvalds int (*okfn)(struct sk_buff *)) 45151da177e4SLinus Torvalds { 4516effad8dfSPaul Moore return selinux_ip_postroute(skb, out->ifindex, PF_INET6); 45171da177e4SLinus Torvalds } 45181da177e4SLinus Torvalds #endif /* IPV6 */ 45191da177e4SLinus Torvalds 45201da177e4SLinus Torvalds #endif /* CONFIG_NETFILTER */ 45211da177e4SLinus Torvalds 45221da177e4SLinus Torvalds static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb) 45231da177e4SLinus Torvalds { 45241da177e4SLinus Torvalds int err; 45251da177e4SLinus Torvalds 45261da177e4SLinus Torvalds err = secondary_ops->netlink_send(sk, skb); 45271da177e4SLinus Torvalds if (err) 45281da177e4SLinus Torvalds return err; 45291da177e4SLinus Torvalds 45301da177e4SLinus Torvalds if (policydb_loaded_version >= POLICYDB_VERSION_NLCLASS) 45311da177e4SLinus Torvalds err = selinux_nlmsg_perm(sk, skb); 45321da177e4SLinus Torvalds 45331da177e4SLinus Torvalds return err; 45341da177e4SLinus Torvalds } 45351da177e4SLinus Torvalds 4536c7bdb545SDarrel Goeddel static int selinux_netlink_recv(struct sk_buff *skb, int capability) 45371da177e4SLinus Torvalds { 4538c7bdb545SDarrel Goeddel int err; 4539c7bdb545SDarrel Goeddel struct avc_audit_data ad; 4540c7bdb545SDarrel Goeddel 4541c7bdb545SDarrel Goeddel err = secondary_ops->netlink_recv(skb, capability); 4542c7bdb545SDarrel Goeddel if (err) 4543c7bdb545SDarrel Goeddel return err; 4544c7bdb545SDarrel Goeddel 4545c7bdb545SDarrel Goeddel AVC_AUDIT_DATA_INIT(&ad, CAP); 4546c7bdb545SDarrel Goeddel ad.u.cap = capability; 4547c7bdb545SDarrel Goeddel 4548c7bdb545SDarrel Goeddel return avc_has_perm(NETLINK_CB(skb).sid, NETLINK_CB(skb).sid, 4549c7bdb545SDarrel Goeddel SECCLASS_CAPABILITY, CAP_TO_MASK(capability), &ad); 45501da177e4SLinus Torvalds } 45511da177e4SLinus Torvalds 45521da177e4SLinus Torvalds static int ipc_alloc_security(struct task_struct *task, 45531da177e4SLinus Torvalds struct kern_ipc_perm *perm, 45541da177e4SLinus Torvalds u16 sclass) 45551da177e4SLinus Torvalds { 45561da177e4SLinus Torvalds struct task_security_struct *tsec = task->security; 45571da177e4SLinus Torvalds struct ipc_security_struct *isec; 45581da177e4SLinus Torvalds 455989d155efSJames Morris isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL); 45601da177e4SLinus Torvalds if (!isec) 45611da177e4SLinus Torvalds return -ENOMEM; 45621da177e4SLinus Torvalds 45631da177e4SLinus Torvalds isec->sclass = sclass; 45641da177e4SLinus Torvalds isec->ipc_perm = perm; 45651da177e4SLinus Torvalds isec->sid = tsec->sid; 45661da177e4SLinus Torvalds perm->security = isec; 45671da177e4SLinus Torvalds 45681da177e4SLinus Torvalds return 0; 45691da177e4SLinus Torvalds } 45701da177e4SLinus Torvalds 45711da177e4SLinus Torvalds static void ipc_free_security(struct kern_ipc_perm *perm) 45721da177e4SLinus Torvalds { 45731da177e4SLinus Torvalds struct ipc_security_struct *isec = perm->security; 45741da177e4SLinus Torvalds perm->security = NULL; 45751da177e4SLinus Torvalds kfree(isec); 45761da177e4SLinus Torvalds } 45771da177e4SLinus Torvalds 45781da177e4SLinus Torvalds static int msg_msg_alloc_security(struct msg_msg *msg) 45791da177e4SLinus Torvalds { 45801da177e4SLinus Torvalds struct msg_security_struct *msec; 45811da177e4SLinus Torvalds 458289d155efSJames Morris msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL); 45831da177e4SLinus Torvalds if (!msec) 45841da177e4SLinus Torvalds return -ENOMEM; 45851da177e4SLinus Torvalds 45861da177e4SLinus Torvalds msec->msg = msg; 45871da177e4SLinus Torvalds msec->sid = SECINITSID_UNLABELED; 45881da177e4SLinus Torvalds msg->security = msec; 45891da177e4SLinus Torvalds 45901da177e4SLinus Torvalds return 0; 45911da177e4SLinus Torvalds } 45921da177e4SLinus Torvalds 45931da177e4SLinus Torvalds static void msg_msg_free_security(struct msg_msg *msg) 45941da177e4SLinus Torvalds { 45951da177e4SLinus Torvalds struct msg_security_struct *msec = msg->security; 45961da177e4SLinus Torvalds 45971da177e4SLinus Torvalds msg->security = NULL; 45981da177e4SLinus Torvalds kfree(msec); 45991da177e4SLinus Torvalds } 46001da177e4SLinus Torvalds 46011da177e4SLinus Torvalds static int ipc_has_perm(struct kern_ipc_perm *ipc_perms, 46026af963f1SStephen Smalley u32 perms) 46031da177e4SLinus Torvalds { 46041da177e4SLinus Torvalds struct task_security_struct *tsec; 46051da177e4SLinus Torvalds struct ipc_security_struct *isec; 46061da177e4SLinus Torvalds struct avc_audit_data ad; 46071da177e4SLinus Torvalds 46081da177e4SLinus Torvalds tsec = current->security; 46091da177e4SLinus Torvalds isec = ipc_perms->security; 46101da177e4SLinus Torvalds 46111da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 46121da177e4SLinus Torvalds ad.u.ipc_id = ipc_perms->key; 46131da177e4SLinus Torvalds 46146af963f1SStephen Smalley return avc_has_perm(tsec->sid, isec->sid, isec->sclass, perms, &ad); 46151da177e4SLinus Torvalds } 46161da177e4SLinus Torvalds 46171da177e4SLinus Torvalds static int selinux_msg_msg_alloc_security(struct msg_msg *msg) 46181da177e4SLinus Torvalds { 46191da177e4SLinus Torvalds return msg_msg_alloc_security(msg); 46201da177e4SLinus Torvalds } 46211da177e4SLinus Torvalds 46221da177e4SLinus Torvalds static void selinux_msg_msg_free_security(struct msg_msg *msg) 46231da177e4SLinus Torvalds { 46241da177e4SLinus Torvalds msg_msg_free_security(msg); 46251da177e4SLinus Torvalds } 46261da177e4SLinus Torvalds 46271da177e4SLinus Torvalds /* message queue security operations */ 46281da177e4SLinus Torvalds static int selinux_msg_queue_alloc_security(struct msg_queue *msq) 46291da177e4SLinus Torvalds { 46301da177e4SLinus Torvalds struct task_security_struct *tsec; 46311da177e4SLinus Torvalds struct ipc_security_struct *isec; 46321da177e4SLinus Torvalds struct avc_audit_data ad; 46331da177e4SLinus Torvalds int rc; 46341da177e4SLinus Torvalds 46351da177e4SLinus Torvalds rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ); 46361da177e4SLinus Torvalds if (rc) 46371da177e4SLinus Torvalds return rc; 46381da177e4SLinus Torvalds 46391da177e4SLinus Torvalds tsec = current->security; 46401da177e4SLinus Torvalds isec = msq->q_perm.security; 46411da177e4SLinus Torvalds 46421da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 46431da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 46441da177e4SLinus Torvalds 46451da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_MSGQ, 46461da177e4SLinus Torvalds MSGQ__CREATE, &ad); 46471da177e4SLinus Torvalds if (rc) { 46481da177e4SLinus Torvalds ipc_free_security(&msq->q_perm); 46491da177e4SLinus Torvalds return rc; 46501da177e4SLinus Torvalds } 46511da177e4SLinus Torvalds return 0; 46521da177e4SLinus Torvalds } 46531da177e4SLinus Torvalds 46541da177e4SLinus Torvalds static void selinux_msg_queue_free_security(struct msg_queue *msq) 46551da177e4SLinus Torvalds { 46561da177e4SLinus Torvalds ipc_free_security(&msq->q_perm); 46571da177e4SLinus Torvalds } 46581da177e4SLinus Torvalds 46591da177e4SLinus Torvalds static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg) 46601da177e4SLinus Torvalds { 46611da177e4SLinus Torvalds struct task_security_struct *tsec; 46621da177e4SLinus Torvalds struct ipc_security_struct *isec; 46631da177e4SLinus Torvalds struct avc_audit_data ad; 46641da177e4SLinus Torvalds 46651da177e4SLinus Torvalds tsec = current->security; 46661da177e4SLinus Torvalds isec = msq->q_perm.security; 46671da177e4SLinus Torvalds 46681da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 46691da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 46701da177e4SLinus Torvalds 46711da177e4SLinus Torvalds return avc_has_perm(tsec->sid, isec->sid, SECCLASS_MSGQ, 46721da177e4SLinus Torvalds MSGQ__ASSOCIATE, &ad); 46731da177e4SLinus Torvalds } 46741da177e4SLinus Torvalds 46751da177e4SLinus Torvalds static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd) 46761da177e4SLinus Torvalds { 46771da177e4SLinus Torvalds int err; 46781da177e4SLinus Torvalds int perms; 46791da177e4SLinus Torvalds 46801da177e4SLinus Torvalds switch(cmd) { 46811da177e4SLinus Torvalds case IPC_INFO: 46821da177e4SLinus Torvalds case MSG_INFO: 46831da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 46841da177e4SLinus Torvalds return task_has_system(current, SYSTEM__IPC_INFO); 46851da177e4SLinus Torvalds case IPC_STAT: 46861da177e4SLinus Torvalds case MSG_STAT: 46871da177e4SLinus Torvalds perms = MSGQ__GETATTR | MSGQ__ASSOCIATE; 46881da177e4SLinus Torvalds break; 46891da177e4SLinus Torvalds case IPC_SET: 46901da177e4SLinus Torvalds perms = MSGQ__SETATTR; 46911da177e4SLinus Torvalds break; 46921da177e4SLinus Torvalds case IPC_RMID: 46931da177e4SLinus Torvalds perms = MSGQ__DESTROY; 46941da177e4SLinus Torvalds break; 46951da177e4SLinus Torvalds default: 46961da177e4SLinus Torvalds return 0; 46971da177e4SLinus Torvalds } 46981da177e4SLinus Torvalds 46996af963f1SStephen Smalley err = ipc_has_perm(&msq->q_perm, perms); 47001da177e4SLinus Torvalds return err; 47011da177e4SLinus Torvalds } 47021da177e4SLinus Torvalds 47031da177e4SLinus Torvalds static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg) 47041da177e4SLinus Torvalds { 47051da177e4SLinus Torvalds struct task_security_struct *tsec; 47061da177e4SLinus Torvalds struct ipc_security_struct *isec; 47071da177e4SLinus Torvalds struct msg_security_struct *msec; 47081da177e4SLinus Torvalds struct avc_audit_data ad; 47091da177e4SLinus Torvalds int rc; 47101da177e4SLinus Torvalds 47111da177e4SLinus Torvalds tsec = current->security; 47121da177e4SLinus Torvalds isec = msq->q_perm.security; 47131da177e4SLinus Torvalds msec = msg->security; 47141da177e4SLinus Torvalds 47151da177e4SLinus Torvalds /* 47161da177e4SLinus Torvalds * First time through, need to assign label to the message 47171da177e4SLinus Torvalds */ 47181da177e4SLinus Torvalds if (msec->sid == SECINITSID_UNLABELED) { 47191da177e4SLinus Torvalds /* 47201da177e4SLinus Torvalds * Compute new sid based on current process and 47211da177e4SLinus Torvalds * message queue this message will be stored in 47221da177e4SLinus Torvalds */ 47231da177e4SLinus Torvalds rc = security_transition_sid(tsec->sid, 47241da177e4SLinus Torvalds isec->sid, 47251da177e4SLinus Torvalds SECCLASS_MSG, 47261da177e4SLinus Torvalds &msec->sid); 47271da177e4SLinus Torvalds if (rc) 47281da177e4SLinus Torvalds return rc; 47291da177e4SLinus Torvalds } 47301da177e4SLinus Torvalds 47311da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 47321da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 47331da177e4SLinus Torvalds 47341da177e4SLinus Torvalds /* Can this process write to the queue? */ 47351da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_MSGQ, 47361da177e4SLinus Torvalds MSGQ__WRITE, &ad); 47371da177e4SLinus Torvalds if (!rc) 47381da177e4SLinus Torvalds /* Can this process send the message */ 47391da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, msec->sid, 47401da177e4SLinus Torvalds SECCLASS_MSG, MSG__SEND, &ad); 47411da177e4SLinus Torvalds if (!rc) 47421da177e4SLinus Torvalds /* Can the message be put in the queue? */ 47431da177e4SLinus Torvalds rc = avc_has_perm(msec->sid, isec->sid, 47441da177e4SLinus Torvalds SECCLASS_MSGQ, MSGQ__ENQUEUE, &ad); 47451da177e4SLinus Torvalds 47461da177e4SLinus Torvalds return rc; 47471da177e4SLinus Torvalds } 47481da177e4SLinus Torvalds 47491da177e4SLinus Torvalds static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg, 47501da177e4SLinus Torvalds struct task_struct *target, 47511da177e4SLinus Torvalds long type, int mode) 47521da177e4SLinus Torvalds { 47531da177e4SLinus Torvalds struct task_security_struct *tsec; 47541da177e4SLinus Torvalds struct ipc_security_struct *isec; 47551da177e4SLinus Torvalds struct msg_security_struct *msec; 47561da177e4SLinus Torvalds struct avc_audit_data ad; 47571da177e4SLinus Torvalds int rc; 47581da177e4SLinus Torvalds 47591da177e4SLinus Torvalds tsec = target->security; 47601da177e4SLinus Torvalds isec = msq->q_perm.security; 47611da177e4SLinus Torvalds msec = msg->security; 47621da177e4SLinus Torvalds 47631da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 47641da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 47651da177e4SLinus Torvalds 47661da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, 47671da177e4SLinus Torvalds SECCLASS_MSGQ, MSGQ__READ, &ad); 47681da177e4SLinus Torvalds if (!rc) 47691da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, msec->sid, 47701da177e4SLinus Torvalds SECCLASS_MSG, MSG__RECEIVE, &ad); 47711da177e4SLinus Torvalds return rc; 47721da177e4SLinus Torvalds } 47731da177e4SLinus Torvalds 47741da177e4SLinus Torvalds /* Shared Memory security operations */ 47751da177e4SLinus Torvalds static int selinux_shm_alloc_security(struct shmid_kernel *shp) 47761da177e4SLinus Torvalds { 47771da177e4SLinus Torvalds struct task_security_struct *tsec; 47781da177e4SLinus Torvalds struct ipc_security_struct *isec; 47791da177e4SLinus Torvalds struct avc_audit_data ad; 47801da177e4SLinus Torvalds int rc; 47811da177e4SLinus Torvalds 47821da177e4SLinus Torvalds rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM); 47831da177e4SLinus Torvalds if (rc) 47841da177e4SLinus Torvalds return rc; 47851da177e4SLinus Torvalds 47861da177e4SLinus Torvalds tsec = current->security; 47871da177e4SLinus Torvalds isec = shp->shm_perm.security; 47881da177e4SLinus Torvalds 47891da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 47901da177e4SLinus Torvalds ad.u.ipc_id = shp->shm_perm.key; 47911da177e4SLinus Torvalds 47921da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_SHM, 47931da177e4SLinus Torvalds SHM__CREATE, &ad); 47941da177e4SLinus Torvalds if (rc) { 47951da177e4SLinus Torvalds ipc_free_security(&shp->shm_perm); 47961da177e4SLinus Torvalds return rc; 47971da177e4SLinus Torvalds } 47981da177e4SLinus Torvalds return 0; 47991da177e4SLinus Torvalds } 48001da177e4SLinus Torvalds 48011da177e4SLinus Torvalds static void selinux_shm_free_security(struct shmid_kernel *shp) 48021da177e4SLinus Torvalds { 48031da177e4SLinus Torvalds ipc_free_security(&shp->shm_perm); 48041da177e4SLinus Torvalds } 48051da177e4SLinus Torvalds 48061da177e4SLinus Torvalds static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg) 48071da177e4SLinus Torvalds { 48081da177e4SLinus Torvalds struct task_security_struct *tsec; 48091da177e4SLinus Torvalds struct ipc_security_struct *isec; 48101da177e4SLinus Torvalds struct avc_audit_data ad; 48111da177e4SLinus Torvalds 48121da177e4SLinus Torvalds tsec = current->security; 48131da177e4SLinus Torvalds isec = shp->shm_perm.security; 48141da177e4SLinus Torvalds 48151da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 48161da177e4SLinus Torvalds ad.u.ipc_id = shp->shm_perm.key; 48171da177e4SLinus Torvalds 48181da177e4SLinus Torvalds return avc_has_perm(tsec->sid, isec->sid, SECCLASS_SHM, 48191da177e4SLinus Torvalds SHM__ASSOCIATE, &ad); 48201da177e4SLinus Torvalds } 48211da177e4SLinus Torvalds 48221da177e4SLinus Torvalds /* Note, at this point, shp is locked down */ 48231da177e4SLinus Torvalds static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd) 48241da177e4SLinus Torvalds { 48251da177e4SLinus Torvalds int perms; 48261da177e4SLinus Torvalds int err; 48271da177e4SLinus Torvalds 48281da177e4SLinus Torvalds switch(cmd) { 48291da177e4SLinus Torvalds case IPC_INFO: 48301da177e4SLinus Torvalds case SHM_INFO: 48311da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 48321da177e4SLinus Torvalds return task_has_system(current, SYSTEM__IPC_INFO); 48331da177e4SLinus Torvalds case IPC_STAT: 48341da177e4SLinus Torvalds case SHM_STAT: 48351da177e4SLinus Torvalds perms = SHM__GETATTR | SHM__ASSOCIATE; 48361da177e4SLinus Torvalds break; 48371da177e4SLinus Torvalds case IPC_SET: 48381da177e4SLinus Torvalds perms = SHM__SETATTR; 48391da177e4SLinus Torvalds break; 48401da177e4SLinus Torvalds case SHM_LOCK: 48411da177e4SLinus Torvalds case SHM_UNLOCK: 48421da177e4SLinus Torvalds perms = SHM__LOCK; 48431da177e4SLinus Torvalds break; 48441da177e4SLinus Torvalds case IPC_RMID: 48451da177e4SLinus Torvalds perms = SHM__DESTROY; 48461da177e4SLinus Torvalds break; 48471da177e4SLinus Torvalds default: 48481da177e4SLinus Torvalds return 0; 48491da177e4SLinus Torvalds } 48501da177e4SLinus Torvalds 48516af963f1SStephen Smalley err = ipc_has_perm(&shp->shm_perm, perms); 48521da177e4SLinus Torvalds return err; 48531da177e4SLinus Torvalds } 48541da177e4SLinus Torvalds 48551da177e4SLinus Torvalds static int selinux_shm_shmat(struct shmid_kernel *shp, 48561da177e4SLinus Torvalds char __user *shmaddr, int shmflg) 48571da177e4SLinus Torvalds { 48581da177e4SLinus Torvalds u32 perms; 48591da177e4SLinus Torvalds int rc; 48601da177e4SLinus Torvalds 48611da177e4SLinus Torvalds rc = secondary_ops->shm_shmat(shp, shmaddr, shmflg); 48621da177e4SLinus Torvalds if (rc) 48631da177e4SLinus Torvalds return rc; 48641da177e4SLinus Torvalds 48651da177e4SLinus Torvalds if (shmflg & SHM_RDONLY) 48661da177e4SLinus Torvalds perms = SHM__READ; 48671da177e4SLinus Torvalds else 48681da177e4SLinus Torvalds perms = SHM__READ | SHM__WRITE; 48691da177e4SLinus Torvalds 48706af963f1SStephen Smalley return ipc_has_perm(&shp->shm_perm, perms); 48711da177e4SLinus Torvalds } 48721da177e4SLinus Torvalds 48731da177e4SLinus Torvalds /* Semaphore security operations */ 48741da177e4SLinus Torvalds static int selinux_sem_alloc_security(struct sem_array *sma) 48751da177e4SLinus Torvalds { 48761da177e4SLinus Torvalds struct task_security_struct *tsec; 48771da177e4SLinus Torvalds struct ipc_security_struct *isec; 48781da177e4SLinus Torvalds struct avc_audit_data ad; 48791da177e4SLinus Torvalds int rc; 48801da177e4SLinus Torvalds 48811da177e4SLinus Torvalds rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM); 48821da177e4SLinus Torvalds if (rc) 48831da177e4SLinus Torvalds return rc; 48841da177e4SLinus Torvalds 48851da177e4SLinus Torvalds tsec = current->security; 48861da177e4SLinus Torvalds isec = sma->sem_perm.security; 48871da177e4SLinus Torvalds 48881da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 48891da177e4SLinus Torvalds ad.u.ipc_id = sma->sem_perm.key; 48901da177e4SLinus Torvalds 48911da177e4SLinus Torvalds rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_SEM, 48921da177e4SLinus Torvalds SEM__CREATE, &ad); 48931da177e4SLinus Torvalds if (rc) { 48941da177e4SLinus Torvalds ipc_free_security(&sma->sem_perm); 48951da177e4SLinus Torvalds return rc; 48961da177e4SLinus Torvalds } 48971da177e4SLinus Torvalds return 0; 48981da177e4SLinus Torvalds } 48991da177e4SLinus Torvalds 49001da177e4SLinus Torvalds static void selinux_sem_free_security(struct sem_array *sma) 49011da177e4SLinus Torvalds { 49021da177e4SLinus Torvalds ipc_free_security(&sma->sem_perm); 49031da177e4SLinus Torvalds } 49041da177e4SLinus Torvalds 49051da177e4SLinus Torvalds static int selinux_sem_associate(struct sem_array *sma, int semflg) 49061da177e4SLinus Torvalds { 49071da177e4SLinus Torvalds struct task_security_struct *tsec; 49081da177e4SLinus Torvalds struct ipc_security_struct *isec; 49091da177e4SLinus Torvalds struct avc_audit_data ad; 49101da177e4SLinus Torvalds 49111da177e4SLinus Torvalds tsec = current->security; 49121da177e4SLinus Torvalds isec = sma->sem_perm.security; 49131da177e4SLinus Torvalds 49141da177e4SLinus Torvalds AVC_AUDIT_DATA_INIT(&ad, IPC); 49151da177e4SLinus Torvalds ad.u.ipc_id = sma->sem_perm.key; 49161da177e4SLinus Torvalds 49171da177e4SLinus Torvalds return avc_has_perm(tsec->sid, isec->sid, SECCLASS_SEM, 49181da177e4SLinus Torvalds SEM__ASSOCIATE, &ad); 49191da177e4SLinus Torvalds } 49201da177e4SLinus Torvalds 49211da177e4SLinus Torvalds /* Note, at this point, sma is locked down */ 49221da177e4SLinus Torvalds static int selinux_sem_semctl(struct sem_array *sma, int cmd) 49231da177e4SLinus Torvalds { 49241da177e4SLinus Torvalds int err; 49251da177e4SLinus Torvalds u32 perms; 49261da177e4SLinus Torvalds 49271da177e4SLinus Torvalds switch(cmd) { 49281da177e4SLinus Torvalds case IPC_INFO: 49291da177e4SLinus Torvalds case SEM_INFO: 49301da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 49311da177e4SLinus Torvalds return task_has_system(current, SYSTEM__IPC_INFO); 49321da177e4SLinus Torvalds case GETPID: 49331da177e4SLinus Torvalds case GETNCNT: 49341da177e4SLinus Torvalds case GETZCNT: 49351da177e4SLinus Torvalds perms = SEM__GETATTR; 49361da177e4SLinus Torvalds break; 49371da177e4SLinus Torvalds case GETVAL: 49381da177e4SLinus Torvalds case GETALL: 49391da177e4SLinus Torvalds perms = SEM__READ; 49401da177e4SLinus Torvalds break; 49411da177e4SLinus Torvalds case SETVAL: 49421da177e4SLinus Torvalds case SETALL: 49431da177e4SLinus Torvalds perms = SEM__WRITE; 49441da177e4SLinus Torvalds break; 49451da177e4SLinus Torvalds case IPC_RMID: 49461da177e4SLinus Torvalds perms = SEM__DESTROY; 49471da177e4SLinus Torvalds break; 49481da177e4SLinus Torvalds case IPC_SET: 49491da177e4SLinus Torvalds perms = SEM__SETATTR; 49501da177e4SLinus Torvalds break; 49511da177e4SLinus Torvalds case IPC_STAT: 49521da177e4SLinus Torvalds case SEM_STAT: 49531da177e4SLinus Torvalds perms = SEM__GETATTR | SEM__ASSOCIATE; 49541da177e4SLinus Torvalds break; 49551da177e4SLinus Torvalds default: 49561da177e4SLinus Torvalds return 0; 49571da177e4SLinus Torvalds } 49581da177e4SLinus Torvalds 49596af963f1SStephen Smalley err = ipc_has_perm(&sma->sem_perm, perms); 49601da177e4SLinus Torvalds return err; 49611da177e4SLinus Torvalds } 49621da177e4SLinus Torvalds 49631da177e4SLinus Torvalds static int selinux_sem_semop(struct sem_array *sma, 49641da177e4SLinus Torvalds struct sembuf *sops, unsigned nsops, int alter) 49651da177e4SLinus Torvalds { 49661da177e4SLinus Torvalds u32 perms; 49671da177e4SLinus Torvalds 49681da177e4SLinus Torvalds if (alter) 49691da177e4SLinus Torvalds perms = SEM__READ | SEM__WRITE; 49701da177e4SLinus Torvalds else 49711da177e4SLinus Torvalds perms = SEM__READ; 49721da177e4SLinus Torvalds 49736af963f1SStephen Smalley return ipc_has_perm(&sma->sem_perm, perms); 49741da177e4SLinus Torvalds } 49751da177e4SLinus Torvalds 49761da177e4SLinus Torvalds static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag) 49771da177e4SLinus Torvalds { 49781da177e4SLinus Torvalds u32 av = 0; 49791da177e4SLinus Torvalds 49801da177e4SLinus Torvalds av = 0; 49811da177e4SLinus Torvalds if (flag & S_IRUGO) 49821da177e4SLinus Torvalds av |= IPC__UNIX_READ; 49831da177e4SLinus Torvalds if (flag & S_IWUGO) 49841da177e4SLinus Torvalds av |= IPC__UNIX_WRITE; 49851da177e4SLinus Torvalds 49861da177e4SLinus Torvalds if (av == 0) 49871da177e4SLinus Torvalds return 0; 49881da177e4SLinus Torvalds 49896af963f1SStephen Smalley return ipc_has_perm(ipcp, av); 49901da177e4SLinus Torvalds } 49911da177e4SLinus Torvalds 49921da177e4SLinus Torvalds /* module stacking operations */ 49931da177e4SLinus Torvalds static int selinux_register_security (const char *name, struct security_operations *ops) 49941da177e4SLinus Torvalds { 49951da177e4SLinus Torvalds if (secondary_ops != original_ops) { 4996fadcdb45SEric Paris printk(KERN_ERR "%s: There is already a secondary security " 49971da177e4SLinus Torvalds "module registered.\n", __FUNCTION__); 49981da177e4SLinus Torvalds return -EINVAL; 49991da177e4SLinus Torvalds } 50001da177e4SLinus Torvalds 50011da177e4SLinus Torvalds secondary_ops = ops; 50021da177e4SLinus Torvalds 50031da177e4SLinus Torvalds printk(KERN_INFO "%s: Registering secondary module %s\n", 50041da177e4SLinus Torvalds __FUNCTION__, 50051da177e4SLinus Torvalds name); 50061da177e4SLinus Torvalds 50071da177e4SLinus Torvalds return 0; 50081da177e4SLinus Torvalds } 50091da177e4SLinus Torvalds 50101da177e4SLinus Torvalds static void selinux_d_instantiate (struct dentry *dentry, struct inode *inode) 50111da177e4SLinus Torvalds { 50121da177e4SLinus Torvalds if (inode) 50131da177e4SLinus Torvalds inode_doinit_with_dentry(inode, dentry); 50141da177e4SLinus Torvalds } 50151da177e4SLinus Torvalds 50161da177e4SLinus Torvalds static int selinux_getprocattr(struct task_struct *p, 501704ff9708SAl Viro char *name, char **value) 50181da177e4SLinus Torvalds { 50191da177e4SLinus Torvalds struct task_security_struct *tsec; 50208c8570fbSDustin Kirkland u32 sid; 50211da177e4SLinus Torvalds int error; 502204ff9708SAl Viro unsigned len; 50231da177e4SLinus Torvalds 50241da177e4SLinus Torvalds if (current != p) { 50251da177e4SLinus Torvalds error = task_has_perm(current, p, PROCESS__GETATTR); 50261da177e4SLinus Torvalds if (error) 50271da177e4SLinus Torvalds return error; 50281da177e4SLinus Torvalds } 50291da177e4SLinus Torvalds 50301da177e4SLinus Torvalds tsec = p->security; 50311da177e4SLinus Torvalds 50321da177e4SLinus Torvalds if (!strcmp(name, "current")) 50331da177e4SLinus Torvalds sid = tsec->sid; 50341da177e4SLinus Torvalds else if (!strcmp(name, "prev")) 50351da177e4SLinus Torvalds sid = tsec->osid; 50361da177e4SLinus Torvalds else if (!strcmp(name, "exec")) 50371da177e4SLinus Torvalds sid = tsec->exec_sid; 50381da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 50391da177e4SLinus Torvalds sid = tsec->create_sid; 50404eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) 50414eb582cfSMichael LeMay sid = tsec->keycreate_sid; 504242c3e03eSEric Paris else if (!strcmp(name, "sockcreate")) 504342c3e03eSEric Paris sid = tsec->sockcreate_sid; 50441da177e4SLinus Torvalds else 50451da177e4SLinus Torvalds return -EINVAL; 50461da177e4SLinus Torvalds 50471da177e4SLinus Torvalds if (!sid) 50481da177e4SLinus Torvalds return 0; 50491da177e4SLinus Torvalds 505004ff9708SAl Viro error = security_sid_to_context(sid, value, &len); 505104ff9708SAl Viro if (error) 505204ff9708SAl Viro return error; 505304ff9708SAl Viro return len; 50541da177e4SLinus Torvalds } 50551da177e4SLinus Torvalds 50561da177e4SLinus Torvalds static int selinux_setprocattr(struct task_struct *p, 50571da177e4SLinus Torvalds char *name, void *value, size_t size) 50581da177e4SLinus Torvalds { 50591da177e4SLinus Torvalds struct task_security_struct *tsec; 50601da177e4SLinus Torvalds u32 sid = 0; 50611da177e4SLinus Torvalds int error; 50621da177e4SLinus Torvalds char *str = value; 50631da177e4SLinus Torvalds 50641da177e4SLinus Torvalds if (current != p) { 50651da177e4SLinus Torvalds /* SELinux only allows a process to change its own 50661da177e4SLinus Torvalds security attributes. */ 50671da177e4SLinus Torvalds return -EACCES; 50681da177e4SLinus Torvalds } 50691da177e4SLinus Torvalds 50701da177e4SLinus Torvalds /* 50711da177e4SLinus Torvalds * Basic control over ability to set these attributes at all. 50721da177e4SLinus Torvalds * current == p, but we'll pass them separately in case the 50731da177e4SLinus Torvalds * above restriction is ever removed. 50741da177e4SLinus Torvalds */ 50751da177e4SLinus Torvalds if (!strcmp(name, "exec")) 50761da177e4SLinus Torvalds error = task_has_perm(current, p, PROCESS__SETEXEC); 50771da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 50781da177e4SLinus Torvalds error = task_has_perm(current, p, PROCESS__SETFSCREATE); 50794eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) 50804eb582cfSMichael LeMay error = task_has_perm(current, p, PROCESS__SETKEYCREATE); 508142c3e03eSEric Paris else if (!strcmp(name, "sockcreate")) 508242c3e03eSEric Paris error = task_has_perm(current, p, PROCESS__SETSOCKCREATE); 50831da177e4SLinus Torvalds else if (!strcmp(name, "current")) 50841da177e4SLinus Torvalds error = task_has_perm(current, p, PROCESS__SETCURRENT); 50851da177e4SLinus Torvalds else 50861da177e4SLinus Torvalds error = -EINVAL; 50871da177e4SLinus Torvalds if (error) 50881da177e4SLinus Torvalds return error; 50891da177e4SLinus Torvalds 50901da177e4SLinus Torvalds /* Obtain a SID for the context, if one was specified. */ 50911da177e4SLinus Torvalds if (size && str[1] && str[1] != '\n') { 50921da177e4SLinus Torvalds if (str[size-1] == '\n') { 50931da177e4SLinus Torvalds str[size-1] = 0; 50941da177e4SLinus Torvalds size--; 50951da177e4SLinus Torvalds } 50961da177e4SLinus Torvalds error = security_context_to_sid(value, size, &sid); 50971da177e4SLinus Torvalds if (error) 50981da177e4SLinus Torvalds return error; 50991da177e4SLinus Torvalds } 51001da177e4SLinus Torvalds 51011da177e4SLinus Torvalds /* Permission checking based on the specified context is 51021da177e4SLinus Torvalds performed during the actual operation (execve, 51031da177e4SLinus Torvalds open/mkdir/...), when we know the full context of the 51041da177e4SLinus Torvalds operation. See selinux_bprm_set_security for the execve 51051da177e4SLinus Torvalds checks and may_create for the file creation checks. The 51061da177e4SLinus Torvalds operation will then fail if the context is not permitted. */ 51071da177e4SLinus Torvalds tsec = p->security; 51081da177e4SLinus Torvalds if (!strcmp(name, "exec")) 51091da177e4SLinus Torvalds tsec->exec_sid = sid; 51101da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 51111da177e4SLinus Torvalds tsec->create_sid = sid; 51124eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) { 51134eb582cfSMichael LeMay error = may_create_key(sid, p); 51144eb582cfSMichael LeMay if (error) 51154eb582cfSMichael LeMay return error; 51164eb582cfSMichael LeMay tsec->keycreate_sid = sid; 511742c3e03eSEric Paris } else if (!strcmp(name, "sockcreate")) 511842c3e03eSEric Paris tsec->sockcreate_sid = sid; 511942c3e03eSEric Paris else if (!strcmp(name, "current")) { 51201da177e4SLinus Torvalds struct av_decision avd; 51211da177e4SLinus Torvalds 51221da177e4SLinus Torvalds if (sid == 0) 51231da177e4SLinus Torvalds return -EINVAL; 51241da177e4SLinus Torvalds 51251da177e4SLinus Torvalds /* Only allow single threaded processes to change context */ 51261da177e4SLinus Torvalds if (atomic_read(&p->mm->mm_users) != 1) { 51271da177e4SLinus Torvalds struct task_struct *g, *t; 51281da177e4SLinus Torvalds struct mm_struct *mm = p->mm; 51291da177e4SLinus Torvalds read_lock(&tasklist_lock); 51301da177e4SLinus Torvalds do_each_thread(g, t) 51311da177e4SLinus Torvalds if (t->mm == mm && t != p) { 51321da177e4SLinus Torvalds read_unlock(&tasklist_lock); 51331da177e4SLinus Torvalds return -EPERM; 51341da177e4SLinus Torvalds } 51351da177e4SLinus Torvalds while_each_thread(g, t); 51361da177e4SLinus Torvalds read_unlock(&tasklist_lock); 51371da177e4SLinus Torvalds } 51381da177e4SLinus Torvalds 51391da177e4SLinus Torvalds /* Check permissions for the transition. */ 51401da177e4SLinus Torvalds error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS, 51411da177e4SLinus Torvalds PROCESS__DYNTRANSITION, NULL); 51421da177e4SLinus Torvalds if (error) 51431da177e4SLinus Torvalds return error; 51441da177e4SLinus Torvalds 51451da177e4SLinus Torvalds /* Check for ptracing, and update the task SID if ok. 51461da177e4SLinus Torvalds Otherwise, leave SID unchanged and fail. */ 51471da177e4SLinus Torvalds task_lock(p); 51481da177e4SLinus Torvalds if (p->ptrace & PT_PTRACED) { 51491da177e4SLinus Torvalds error = avc_has_perm_noaudit(tsec->ptrace_sid, sid, 51501da177e4SLinus Torvalds SECCLASS_PROCESS, 51512c3c05dbSStephen Smalley PROCESS__PTRACE, 0, &avd); 51521da177e4SLinus Torvalds if (!error) 51531da177e4SLinus Torvalds tsec->sid = sid; 51541da177e4SLinus Torvalds task_unlock(p); 51551da177e4SLinus Torvalds avc_audit(tsec->ptrace_sid, sid, SECCLASS_PROCESS, 51561da177e4SLinus Torvalds PROCESS__PTRACE, &avd, error, NULL); 51571da177e4SLinus Torvalds if (error) 51581da177e4SLinus Torvalds return error; 51591da177e4SLinus Torvalds } else { 51601da177e4SLinus Torvalds tsec->sid = sid; 51611da177e4SLinus Torvalds task_unlock(p); 51621da177e4SLinus Torvalds } 51631da177e4SLinus Torvalds } 51641da177e4SLinus Torvalds else 51651da177e4SLinus Torvalds return -EINVAL; 51661da177e4SLinus Torvalds 51671da177e4SLinus Torvalds return size; 51681da177e4SLinus Torvalds } 51691da177e4SLinus Torvalds 5170dc49c1f9SCatherine Zhang static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) 5171dc49c1f9SCatherine Zhang { 5172dc49c1f9SCatherine Zhang return security_sid_to_context(secid, secdata, seclen); 5173dc49c1f9SCatherine Zhang } 5174dc49c1f9SCatherine Zhang 517563cb3449SDavid Howells static int selinux_secctx_to_secid(char *secdata, u32 seclen, u32 *secid) 517663cb3449SDavid Howells { 517763cb3449SDavid Howells return security_context_to_sid(secdata, seclen, secid); 517863cb3449SDavid Howells } 517963cb3449SDavid Howells 5180dc49c1f9SCatherine Zhang static void selinux_release_secctx(char *secdata, u32 seclen) 5181dc49c1f9SCatherine Zhang { 5182dc49c1f9SCatherine Zhang kfree(secdata); 5183dc49c1f9SCatherine Zhang } 5184dc49c1f9SCatherine Zhang 5185d720024eSMichael LeMay #ifdef CONFIG_KEYS 5186d720024eSMichael LeMay 51877e047ef5SDavid Howells static int selinux_key_alloc(struct key *k, struct task_struct *tsk, 51887e047ef5SDavid Howells unsigned long flags) 5189d720024eSMichael LeMay { 5190d720024eSMichael LeMay struct task_security_struct *tsec = tsk->security; 5191d720024eSMichael LeMay struct key_security_struct *ksec; 5192d720024eSMichael LeMay 5193d720024eSMichael LeMay ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL); 5194d720024eSMichael LeMay if (!ksec) 5195d720024eSMichael LeMay return -ENOMEM; 5196d720024eSMichael LeMay 5197d720024eSMichael LeMay ksec->obj = k; 51984eb582cfSMichael LeMay if (tsec->keycreate_sid) 51994eb582cfSMichael LeMay ksec->sid = tsec->keycreate_sid; 52004eb582cfSMichael LeMay else 5201d720024eSMichael LeMay ksec->sid = tsec->sid; 5202d720024eSMichael LeMay k->security = ksec; 5203d720024eSMichael LeMay 5204d720024eSMichael LeMay return 0; 5205d720024eSMichael LeMay } 5206d720024eSMichael LeMay 5207d720024eSMichael LeMay static void selinux_key_free(struct key *k) 5208d720024eSMichael LeMay { 5209d720024eSMichael LeMay struct key_security_struct *ksec = k->security; 5210d720024eSMichael LeMay 5211d720024eSMichael LeMay k->security = NULL; 5212d720024eSMichael LeMay kfree(ksec); 5213d720024eSMichael LeMay } 5214d720024eSMichael LeMay 5215d720024eSMichael LeMay static int selinux_key_permission(key_ref_t key_ref, 5216d720024eSMichael LeMay struct task_struct *ctx, 5217d720024eSMichael LeMay key_perm_t perm) 5218d720024eSMichael LeMay { 5219d720024eSMichael LeMay struct key *key; 5220d720024eSMichael LeMay struct task_security_struct *tsec; 5221d720024eSMichael LeMay struct key_security_struct *ksec; 5222d720024eSMichael LeMay 5223d720024eSMichael LeMay key = key_ref_to_ptr(key_ref); 5224d720024eSMichael LeMay 5225d720024eSMichael LeMay tsec = ctx->security; 5226d720024eSMichael LeMay ksec = key->security; 5227d720024eSMichael LeMay 5228d720024eSMichael LeMay /* if no specific permissions are requested, we skip the 5229d720024eSMichael LeMay permission check. No serious, additional covert channels 5230d720024eSMichael LeMay appear to be created. */ 5231d720024eSMichael LeMay if (perm == 0) 5232d720024eSMichael LeMay return 0; 5233d720024eSMichael LeMay 5234d720024eSMichael LeMay return avc_has_perm(tsec->sid, ksec->sid, 5235d720024eSMichael LeMay SECCLASS_KEY, perm, NULL); 5236d720024eSMichael LeMay } 5237d720024eSMichael LeMay 5238d720024eSMichael LeMay #endif 5239d720024eSMichael LeMay 52401da177e4SLinus Torvalds static struct security_operations selinux_ops = { 52411da177e4SLinus Torvalds .ptrace = selinux_ptrace, 52421da177e4SLinus Torvalds .capget = selinux_capget, 52431da177e4SLinus Torvalds .capset_check = selinux_capset_check, 52441da177e4SLinus Torvalds .capset_set = selinux_capset_set, 52451da177e4SLinus Torvalds .sysctl = selinux_sysctl, 52461da177e4SLinus Torvalds .capable = selinux_capable, 52471da177e4SLinus Torvalds .quotactl = selinux_quotactl, 52481da177e4SLinus Torvalds .quota_on = selinux_quota_on, 52491da177e4SLinus Torvalds .syslog = selinux_syslog, 52501da177e4SLinus Torvalds .vm_enough_memory = selinux_vm_enough_memory, 52511da177e4SLinus Torvalds 52521da177e4SLinus Torvalds .netlink_send = selinux_netlink_send, 52531da177e4SLinus Torvalds .netlink_recv = selinux_netlink_recv, 52541da177e4SLinus Torvalds 52551da177e4SLinus Torvalds .bprm_alloc_security = selinux_bprm_alloc_security, 52561da177e4SLinus Torvalds .bprm_free_security = selinux_bprm_free_security, 52571da177e4SLinus Torvalds .bprm_apply_creds = selinux_bprm_apply_creds, 52581da177e4SLinus Torvalds .bprm_post_apply_creds = selinux_bprm_post_apply_creds, 52591da177e4SLinus Torvalds .bprm_set_security = selinux_bprm_set_security, 52601da177e4SLinus Torvalds .bprm_check_security = selinux_bprm_check_security, 52611da177e4SLinus Torvalds .bprm_secureexec = selinux_bprm_secureexec, 52621da177e4SLinus Torvalds 52631da177e4SLinus Torvalds .sb_alloc_security = selinux_sb_alloc_security, 52641da177e4SLinus Torvalds .sb_free_security = selinux_sb_free_security, 52651da177e4SLinus Torvalds .sb_copy_data = selinux_sb_copy_data, 52661da177e4SLinus Torvalds .sb_kern_mount = selinux_sb_kern_mount, 52671da177e4SLinus Torvalds .sb_statfs = selinux_sb_statfs, 52681da177e4SLinus Torvalds .sb_mount = selinux_mount, 52691da177e4SLinus Torvalds .sb_umount = selinux_umount, 5270c9180a57SEric Paris .sb_get_mnt_opts = selinux_get_mnt_opts, 5271c9180a57SEric Paris .sb_set_mnt_opts = selinux_set_mnt_opts, 5272c9180a57SEric Paris .sb_clone_mnt_opts = selinux_sb_clone_mnt_opts, 5273e0007529SEric Paris .sb_parse_opts_str = selinux_parse_opts_str, 5274e0007529SEric Paris 52751da177e4SLinus Torvalds 52761da177e4SLinus Torvalds .inode_alloc_security = selinux_inode_alloc_security, 52771da177e4SLinus Torvalds .inode_free_security = selinux_inode_free_security, 52785e41ff9eSStephen Smalley .inode_init_security = selinux_inode_init_security, 52791da177e4SLinus Torvalds .inode_create = selinux_inode_create, 52801da177e4SLinus Torvalds .inode_link = selinux_inode_link, 52811da177e4SLinus Torvalds .inode_unlink = selinux_inode_unlink, 52821da177e4SLinus Torvalds .inode_symlink = selinux_inode_symlink, 52831da177e4SLinus Torvalds .inode_mkdir = selinux_inode_mkdir, 52841da177e4SLinus Torvalds .inode_rmdir = selinux_inode_rmdir, 52851da177e4SLinus Torvalds .inode_mknod = selinux_inode_mknod, 52861da177e4SLinus Torvalds .inode_rename = selinux_inode_rename, 52871da177e4SLinus Torvalds .inode_readlink = selinux_inode_readlink, 52881da177e4SLinus Torvalds .inode_follow_link = selinux_inode_follow_link, 52891da177e4SLinus Torvalds .inode_permission = selinux_inode_permission, 52901da177e4SLinus Torvalds .inode_setattr = selinux_inode_setattr, 52911da177e4SLinus Torvalds .inode_getattr = selinux_inode_getattr, 52921da177e4SLinus Torvalds .inode_setxattr = selinux_inode_setxattr, 52931da177e4SLinus Torvalds .inode_post_setxattr = selinux_inode_post_setxattr, 52941da177e4SLinus Torvalds .inode_getxattr = selinux_inode_getxattr, 52951da177e4SLinus Torvalds .inode_listxattr = selinux_inode_listxattr, 52961da177e4SLinus Torvalds .inode_removexattr = selinux_inode_removexattr, 52971da177e4SLinus Torvalds .inode_getsecurity = selinux_inode_getsecurity, 52981da177e4SLinus Torvalds .inode_setsecurity = selinux_inode_setsecurity, 52991da177e4SLinus Torvalds .inode_listsecurity = selinux_inode_listsecurity, 5300b5376771SSerge E. Hallyn .inode_need_killpriv = selinux_inode_need_killpriv, 5301b5376771SSerge E. Hallyn .inode_killpriv = selinux_inode_killpriv, 53021da177e4SLinus Torvalds 53031da177e4SLinus Torvalds .file_permission = selinux_file_permission, 53041da177e4SLinus Torvalds .file_alloc_security = selinux_file_alloc_security, 53051da177e4SLinus Torvalds .file_free_security = selinux_file_free_security, 53061da177e4SLinus Torvalds .file_ioctl = selinux_file_ioctl, 53071da177e4SLinus Torvalds .file_mmap = selinux_file_mmap, 53081da177e4SLinus Torvalds .file_mprotect = selinux_file_mprotect, 53091da177e4SLinus Torvalds .file_lock = selinux_file_lock, 53101da177e4SLinus Torvalds .file_fcntl = selinux_file_fcntl, 53111da177e4SLinus Torvalds .file_set_fowner = selinux_file_set_fowner, 53121da177e4SLinus Torvalds .file_send_sigiotask = selinux_file_send_sigiotask, 53131da177e4SLinus Torvalds .file_receive = selinux_file_receive, 53141da177e4SLinus Torvalds 5315788e7dd4SYuichi Nakamura .dentry_open = selinux_dentry_open, 5316788e7dd4SYuichi Nakamura 53171da177e4SLinus Torvalds .task_create = selinux_task_create, 53181da177e4SLinus Torvalds .task_alloc_security = selinux_task_alloc_security, 53191da177e4SLinus Torvalds .task_free_security = selinux_task_free_security, 53201da177e4SLinus Torvalds .task_setuid = selinux_task_setuid, 53211da177e4SLinus Torvalds .task_post_setuid = selinux_task_post_setuid, 53221da177e4SLinus Torvalds .task_setgid = selinux_task_setgid, 53231da177e4SLinus Torvalds .task_setpgid = selinux_task_setpgid, 53241da177e4SLinus Torvalds .task_getpgid = selinux_task_getpgid, 53251da177e4SLinus Torvalds .task_getsid = selinux_task_getsid, 5326f9008e4cSDavid Quigley .task_getsecid = selinux_task_getsecid, 53271da177e4SLinus Torvalds .task_setgroups = selinux_task_setgroups, 53281da177e4SLinus Torvalds .task_setnice = selinux_task_setnice, 532903e68060SJames Morris .task_setioprio = selinux_task_setioprio, 5330a1836a42SDavid Quigley .task_getioprio = selinux_task_getioprio, 53311da177e4SLinus Torvalds .task_setrlimit = selinux_task_setrlimit, 53321da177e4SLinus Torvalds .task_setscheduler = selinux_task_setscheduler, 53331da177e4SLinus Torvalds .task_getscheduler = selinux_task_getscheduler, 533435601547SDavid Quigley .task_movememory = selinux_task_movememory, 53351da177e4SLinus Torvalds .task_kill = selinux_task_kill, 53361da177e4SLinus Torvalds .task_wait = selinux_task_wait, 53371da177e4SLinus Torvalds .task_prctl = selinux_task_prctl, 53381da177e4SLinus Torvalds .task_reparent_to_init = selinux_task_reparent_to_init, 53391da177e4SLinus Torvalds .task_to_inode = selinux_task_to_inode, 53401da177e4SLinus Torvalds 53411da177e4SLinus Torvalds .ipc_permission = selinux_ipc_permission, 53421da177e4SLinus Torvalds 53431da177e4SLinus Torvalds .msg_msg_alloc_security = selinux_msg_msg_alloc_security, 53441da177e4SLinus Torvalds .msg_msg_free_security = selinux_msg_msg_free_security, 53451da177e4SLinus Torvalds 53461da177e4SLinus Torvalds .msg_queue_alloc_security = selinux_msg_queue_alloc_security, 53471da177e4SLinus Torvalds .msg_queue_free_security = selinux_msg_queue_free_security, 53481da177e4SLinus Torvalds .msg_queue_associate = selinux_msg_queue_associate, 53491da177e4SLinus Torvalds .msg_queue_msgctl = selinux_msg_queue_msgctl, 53501da177e4SLinus Torvalds .msg_queue_msgsnd = selinux_msg_queue_msgsnd, 53511da177e4SLinus Torvalds .msg_queue_msgrcv = selinux_msg_queue_msgrcv, 53521da177e4SLinus Torvalds 53531da177e4SLinus Torvalds .shm_alloc_security = selinux_shm_alloc_security, 53541da177e4SLinus Torvalds .shm_free_security = selinux_shm_free_security, 53551da177e4SLinus Torvalds .shm_associate = selinux_shm_associate, 53561da177e4SLinus Torvalds .shm_shmctl = selinux_shm_shmctl, 53571da177e4SLinus Torvalds .shm_shmat = selinux_shm_shmat, 53581da177e4SLinus Torvalds 53591da177e4SLinus Torvalds .sem_alloc_security = selinux_sem_alloc_security, 53601da177e4SLinus Torvalds .sem_free_security = selinux_sem_free_security, 53611da177e4SLinus Torvalds .sem_associate = selinux_sem_associate, 53621da177e4SLinus Torvalds .sem_semctl = selinux_sem_semctl, 53631da177e4SLinus Torvalds .sem_semop = selinux_sem_semop, 53641da177e4SLinus Torvalds 53651da177e4SLinus Torvalds .register_security = selinux_register_security, 53661da177e4SLinus Torvalds 53671da177e4SLinus Torvalds .d_instantiate = selinux_d_instantiate, 53681da177e4SLinus Torvalds 53691da177e4SLinus Torvalds .getprocattr = selinux_getprocattr, 53701da177e4SLinus Torvalds .setprocattr = selinux_setprocattr, 53711da177e4SLinus Torvalds 5372dc49c1f9SCatherine Zhang .secid_to_secctx = selinux_secid_to_secctx, 537363cb3449SDavid Howells .secctx_to_secid = selinux_secctx_to_secid, 5374dc49c1f9SCatherine Zhang .release_secctx = selinux_release_secctx, 5375dc49c1f9SCatherine Zhang 53761da177e4SLinus Torvalds .unix_stream_connect = selinux_socket_unix_stream_connect, 53771da177e4SLinus Torvalds .unix_may_send = selinux_socket_unix_may_send, 53781da177e4SLinus Torvalds 53791da177e4SLinus Torvalds .socket_create = selinux_socket_create, 53801da177e4SLinus Torvalds .socket_post_create = selinux_socket_post_create, 53811da177e4SLinus Torvalds .socket_bind = selinux_socket_bind, 53821da177e4SLinus Torvalds .socket_connect = selinux_socket_connect, 53831da177e4SLinus Torvalds .socket_listen = selinux_socket_listen, 53841da177e4SLinus Torvalds .socket_accept = selinux_socket_accept, 53851da177e4SLinus Torvalds .socket_sendmsg = selinux_socket_sendmsg, 53861da177e4SLinus Torvalds .socket_recvmsg = selinux_socket_recvmsg, 53871da177e4SLinus Torvalds .socket_getsockname = selinux_socket_getsockname, 53881da177e4SLinus Torvalds .socket_getpeername = selinux_socket_getpeername, 53891da177e4SLinus Torvalds .socket_getsockopt = selinux_socket_getsockopt, 53901da177e4SLinus Torvalds .socket_setsockopt = selinux_socket_setsockopt, 53911da177e4SLinus Torvalds .socket_shutdown = selinux_socket_shutdown, 53921da177e4SLinus Torvalds .socket_sock_rcv_skb = selinux_socket_sock_rcv_skb, 53932c7946a7SCatherine Zhang .socket_getpeersec_stream = selinux_socket_getpeersec_stream, 53942c7946a7SCatherine Zhang .socket_getpeersec_dgram = selinux_socket_getpeersec_dgram, 53951da177e4SLinus Torvalds .sk_alloc_security = selinux_sk_alloc_security, 53961da177e4SLinus Torvalds .sk_free_security = selinux_sk_free_security, 5397892c141eSVenkat Yekkirala .sk_clone_security = selinux_sk_clone_security, 5398beb8d13bSVenkat Yekkirala .sk_getsecid = selinux_sk_getsecid, 53994237c75cSVenkat Yekkirala .sock_graft = selinux_sock_graft, 54004237c75cSVenkat Yekkirala .inet_conn_request = selinux_inet_conn_request, 54014237c75cSVenkat Yekkirala .inet_csk_clone = selinux_inet_csk_clone, 54026b877699SVenkat Yekkirala .inet_conn_established = selinux_inet_conn_established, 54034237c75cSVenkat Yekkirala .req_classify_flow = selinux_req_classify_flow, 5404d28d1e08STrent Jaeger 5405d28d1e08STrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM 5406d28d1e08STrent Jaeger .xfrm_policy_alloc_security = selinux_xfrm_policy_alloc, 5407d28d1e08STrent Jaeger .xfrm_policy_clone_security = selinux_xfrm_policy_clone, 5408d28d1e08STrent Jaeger .xfrm_policy_free_security = selinux_xfrm_policy_free, 5409c8c05a8eSCatherine Zhang .xfrm_policy_delete_security = selinux_xfrm_policy_delete, 5410d28d1e08STrent Jaeger .xfrm_state_alloc_security = selinux_xfrm_state_alloc, 5411d28d1e08STrent Jaeger .xfrm_state_free_security = selinux_xfrm_state_free, 5412c8c05a8eSCatherine Zhang .xfrm_state_delete_security = selinux_xfrm_state_delete, 5413d28d1e08STrent Jaeger .xfrm_policy_lookup = selinux_xfrm_policy_lookup, 5414e0d1caa7SVenkat Yekkirala .xfrm_state_pol_flow_match = selinux_xfrm_state_pol_flow_match, 5415e0d1caa7SVenkat Yekkirala .xfrm_decode_session = selinux_xfrm_decode_session, 54161da177e4SLinus Torvalds #endif 5417d720024eSMichael LeMay 5418d720024eSMichael LeMay #ifdef CONFIG_KEYS 5419d720024eSMichael LeMay .key_alloc = selinux_key_alloc, 5420d720024eSMichael LeMay .key_free = selinux_key_free, 5421d720024eSMichael LeMay .key_permission = selinux_key_permission, 5422d720024eSMichael LeMay #endif 54231da177e4SLinus Torvalds }; 54241da177e4SLinus Torvalds 54251da177e4SLinus Torvalds static __init int selinux_init(void) 54261da177e4SLinus Torvalds { 54271da177e4SLinus Torvalds struct task_security_struct *tsec; 54281da177e4SLinus Torvalds 54291da177e4SLinus Torvalds if (!selinux_enabled) { 54301da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Disabled at boot.\n"); 54311da177e4SLinus Torvalds return 0; 54321da177e4SLinus Torvalds } 54331da177e4SLinus Torvalds 54341da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Initializing.\n"); 54351da177e4SLinus Torvalds 54361da177e4SLinus Torvalds /* Set the security state for the initial task. */ 54371da177e4SLinus Torvalds if (task_alloc_security(current)) 54381da177e4SLinus Torvalds panic("SELinux: Failed to initialize initial task.\n"); 54391da177e4SLinus Torvalds tsec = current->security; 54401da177e4SLinus Torvalds tsec->osid = tsec->sid = SECINITSID_KERNEL; 54411da177e4SLinus Torvalds 54427cae7e26SJames Morris sel_inode_cache = kmem_cache_create("selinux_inode_security", 54437cae7e26SJames Morris sizeof(struct inode_security_struct), 544420c2df83SPaul Mundt 0, SLAB_PANIC, NULL); 54451da177e4SLinus Torvalds avc_init(); 54461da177e4SLinus Torvalds 54471da177e4SLinus Torvalds original_ops = secondary_ops = security_ops; 54481da177e4SLinus Torvalds if (!secondary_ops) 54491da177e4SLinus Torvalds panic ("SELinux: No initial security operations\n"); 54501da177e4SLinus Torvalds if (register_security (&selinux_ops)) 54511da177e4SLinus Torvalds panic("SELinux: Unable to register with kernel.\n"); 54521da177e4SLinus Torvalds 54531da177e4SLinus Torvalds if (selinux_enforcing) { 5454fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Starting in enforcing mode\n"); 54551da177e4SLinus Torvalds } else { 5456fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Starting in permissive mode\n"); 54571da177e4SLinus Torvalds } 5458d720024eSMichael LeMay 5459d720024eSMichael LeMay #ifdef CONFIG_KEYS 5460d720024eSMichael LeMay /* Add security information to initial keyrings */ 54614eb582cfSMichael LeMay selinux_key_alloc(&root_user_keyring, current, 54627e047ef5SDavid Howells KEY_ALLOC_NOT_IN_QUOTA); 54634eb582cfSMichael LeMay selinux_key_alloc(&root_session_keyring, current, 54647e047ef5SDavid Howells KEY_ALLOC_NOT_IN_QUOTA); 5465d720024eSMichael LeMay #endif 5466d720024eSMichael LeMay 54671da177e4SLinus Torvalds return 0; 54681da177e4SLinus Torvalds } 54691da177e4SLinus Torvalds 54701da177e4SLinus Torvalds void selinux_complete_init(void) 54711da177e4SLinus Torvalds { 5472fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Completing initialization.\n"); 54731da177e4SLinus Torvalds 54741da177e4SLinus Torvalds /* Set up any superblocks initialized prior to the policy load. */ 5475fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Setting up existing superblocks.\n"); 5476ba0c19edSStephen Smalley spin_lock(&sb_lock); 54771da177e4SLinus Torvalds spin_lock(&sb_security_lock); 54781da177e4SLinus Torvalds next_sb: 54791da177e4SLinus Torvalds if (!list_empty(&superblock_security_head)) { 54801da177e4SLinus Torvalds struct superblock_security_struct *sbsec = 54811da177e4SLinus Torvalds list_entry(superblock_security_head.next, 54821da177e4SLinus Torvalds struct superblock_security_struct, 54831da177e4SLinus Torvalds list); 54841da177e4SLinus Torvalds struct super_block *sb = sbsec->sb; 54851da177e4SLinus Torvalds sb->s_count++; 54861da177e4SLinus Torvalds spin_unlock(&sb_security_lock); 5487ba0c19edSStephen Smalley spin_unlock(&sb_lock); 54881da177e4SLinus Torvalds down_read(&sb->s_umount); 54891da177e4SLinus Torvalds if (sb->s_root) 54901da177e4SLinus Torvalds superblock_doinit(sb, NULL); 54911da177e4SLinus Torvalds drop_super(sb); 5492ba0c19edSStephen Smalley spin_lock(&sb_lock); 54931da177e4SLinus Torvalds spin_lock(&sb_security_lock); 54941da177e4SLinus Torvalds list_del_init(&sbsec->list); 54951da177e4SLinus Torvalds goto next_sb; 54961da177e4SLinus Torvalds } 54971da177e4SLinus Torvalds spin_unlock(&sb_security_lock); 5498ba0c19edSStephen Smalley spin_unlock(&sb_lock); 54991da177e4SLinus Torvalds } 55001da177e4SLinus Torvalds 55011da177e4SLinus Torvalds /* SELinux requires early initialization in order to label 55021da177e4SLinus Torvalds all processes and objects when they are created. */ 55031da177e4SLinus Torvalds security_initcall(selinux_init); 55041da177e4SLinus Torvalds 5505c2b507fdSStephen Smalley #if defined(CONFIG_NETFILTER) 55061da177e4SLinus Torvalds 5507effad8dfSPaul Moore static struct nf_hook_ops selinux_ipv4_ops[] = { 5508effad8dfSPaul Moore { 5509effad8dfSPaul Moore .hook = selinux_ipv4_postroute, 55101da177e4SLinus Torvalds .owner = THIS_MODULE, 55111da177e4SLinus Torvalds .pf = PF_INET, 55126e23ae2aSPatrick McHardy .hooknum = NF_INET_POST_ROUTING, 55131da177e4SLinus Torvalds .priority = NF_IP_PRI_SELINUX_LAST, 5514effad8dfSPaul Moore }, 5515effad8dfSPaul Moore { 5516effad8dfSPaul Moore .hook = selinux_ipv4_forward, 5517effad8dfSPaul Moore .owner = THIS_MODULE, 5518effad8dfSPaul Moore .pf = PF_INET, 5519effad8dfSPaul Moore .hooknum = NF_INET_FORWARD, 5520effad8dfSPaul Moore .priority = NF_IP_PRI_SELINUX_FIRST, 5521effad8dfSPaul Moore } 55221da177e4SLinus Torvalds }; 55231da177e4SLinus Torvalds 55241da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 55251da177e4SLinus Torvalds 5526effad8dfSPaul Moore static struct nf_hook_ops selinux_ipv6_ops[] = { 5527effad8dfSPaul Moore { 5528effad8dfSPaul Moore .hook = selinux_ipv6_postroute, 55291da177e4SLinus Torvalds .owner = THIS_MODULE, 55301da177e4SLinus Torvalds .pf = PF_INET6, 55316e23ae2aSPatrick McHardy .hooknum = NF_INET_POST_ROUTING, 55321da177e4SLinus Torvalds .priority = NF_IP6_PRI_SELINUX_LAST, 5533effad8dfSPaul Moore }, 5534effad8dfSPaul Moore { 5535effad8dfSPaul Moore .hook = selinux_ipv6_forward, 5536effad8dfSPaul Moore .owner = THIS_MODULE, 5537effad8dfSPaul Moore .pf = PF_INET6, 5538effad8dfSPaul Moore .hooknum = NF_INET_FORWARD, 5539effad8dfSPaul Moore .priority = NF_IP6_PRI_SELINUX_FIRST, 5540effad8dfSPaul Moore } 55411da177e4SLinus Torvalds }; 55421da177e4SLinus Torvalds 55431da177e4SLinus Torvalds #endif /* IPV6 */ 55441da177e4SLinus Torvalds 55451da177e4SLinus Torvalds static int __init selinux_nf_ip_init(void) 55461da177e4SLinus Torvalds { 55471da177e4SLinus Torvalds int err = 0; 5548effad8dfSPaul Moore u32 iter; 55491da177e4SLinus Torvalds 55501da177e4SLinus Torvalds if (!selinux_enabled) 55511da177e4SLinus Torvalds goto out; 55521da177e4SLinus Torvalds 5553fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Registering netfilter hooks\n"); 55541da177e4SLinus Torvalds 5555effad8dfSPaul Moore for (iter = 0; iter < ARRAY_SIZE(selinux_ipv4_ops); iter++) { 5556effad8dfSPaul Moore err = nf_register_hook(&selinux_ipv4_ops[iter]); 55571da177e4SLinus Torvalds if (err) 5558effad8dfSPaul Moore panic("SELinux: nf_register_hook for IPv4: error %d\n", 5559effad8dfSPaul Moore err); 5560effad8dfSPaul Moore } 55611da177e4SLinus Torvalds 55621da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 5563effad8dfSPaul Moore for (iter = 0; iter < ARRAY_SIZE(selinux_ipv6_ops); iter++) { 5564effad8dfSPaul Moore err = nf_register_hook(&selinux_ipv6_ops[iter]); 55651da177e4SLinus Torvalds if (err) 5566effad8dfSPaul Moore panic("SELinux: nf_register_hook for IPv6: error %d\n", 5567effad8dfSPaul Moore err); 5568effad8dfSPaul Moore } 55691da177e4SLinus Torvalds #endif /* IPV6 */ 5570d28d1e08STrent Jaeger 55711da177e4SLinus Torvalds out: 55721da177e4SLinus Torvalds return err; 55731da177e4SLinus Torvalds } 55741da177e4SLinus Torvalds 55751da177e4SLinus Torvalds __initcall(selinux_nf_ip_init); 55761da177e4SLinus Torvalds 55771da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 55781da177e4SLinus Torvalds static void selinux_nf_ip_exit(void) 55791da177e4SLinus Torvalds { 5580effad8dfSPaul Moore u32 iter; 5581effad8dfSPaul Moore 5582fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Unregistering netfilter hooks\n"); 55831da177e4SLinus Torvalds 5584effad8dfSPaul Moore for (iter = 0; iter < ARRAY_SIZE(selinux_ipv4_ops); iter++) 5585effad8dfSPaul Moore nf_unregister_hook(&selinux_ipv4_ops[iter]); 55861da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 5587effad8dfSPaul Moore for (iter = 0; iter < ARRAY_SIZE(selinux_ipv6_ops); iter++) 5588effad8dfSPaul Moore nf_unregister_hook(&selinux_ipv6_ops[iter]); 55891da177e4SLinus Torvalds #endif /* IPV6 */ 55901da177e4SLinus Torvalds } 55911da177e4SLinus Torvalds #endif 55921da177e4SLinus Torvalds 5593c2b507fdSStephen Smalley #else /* CONFIG_NETFILTER */ 55941da177e4SLinus Torvalds 55951da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 55961da177e4SLinus Torvalds #define selinux_nf_ip_exit() 55971da177e4SLinus Torvalds #endif 55981da177e4SLinus Torvalds 5599c2b507fdSStephen Smalley #endif /* CONFIG_NETFILTER */ 56001da177e4SLinus Torvalds 56011da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 56021da177e4SLinus Torvalds int selinux_disable(void) 56031da177e4SLinus Torvalds { 56041da177e4SLinus Torvalds extern void exit_sel_fs(void); 56051da177e4SLinus Torvalds static int selinux_disabled = 0; 56061da177e4SLinus Torvalds 56071da177e4SLinus Torvalds if (ss_initialized) { 56081da177e4SLinus Torvalds /* Not permitted after initial policy load. */ 56091da177e4SLinus Torvalds return -EINVAL; 56101da177e4SLinus Torvalds } 56111da177e4SLinus Torvalds 56121da177e4SLinus Torvalds if (selinux_disabled) { 56131da177e4SLinus Torvalds /* Only do this once. */ 56141da177e4SLinus Torvalds return -EINVAL; 56151da177e4SLinus Torvalds } 56161da177e4SLinus Torvalds 56171da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Disabled at runtime.\n"); 56181da177e4SLinus Torvalds 56191da177e4SLinus Torvalds selinux_disabled = 1; 562030d55280SStephen Smalley selinux_enabled = 0; 56211da177e4SLinus Torvalds 56221da177e4SLinus Torvalds /* Reset security_ops to the secondary module, dummy or capability. */ 56231da177e4SLinus Torvalds security_ops = secondary_ops; 56241da177e4SLinus Torvalds 56251da177e4SLinus Torvalds /* Unregister netfilter hooks. */ 56261da177e4SLinus Torvalds selinux_nf_ip_exit(); 56271da177e4SLinus Torvalds 56281da177e4SLinus Torvalds /* Unregister selinuxfs. */ 56291da177e4SLinus Torvalds exit_sel_fs(); 56301da177e4SLinus Torvalds 56311da177e4SLinus Torvalds return 0; 56321da177e4SLinus Torvalds } 56331da177e4SLinus Torvalds #endif 56341da177e4SLinus Torvalds 56351da177e4SLinus Torvalds 5636