11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * NSA Security-Enhanced Linux (SELinux) security module 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * This file contains the SELinux hook function implementations. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Authors: Stephen Smalley, <sds@epoch.ncsc.mil> 71da177e4SLinus Torvalds * Chris Vance, <cvance@nai.com> 81da177e4SLinus Torvalds * Wayne Salamon, <wsalamon@nai.com> 91da177e4SLinus Torvalds * James Morris <jmorris@redhat.com> 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * Copyright (C) 2001,2002 Networks Associates Technology, Inc. 122069f457SEric Paris * Copyright (C) 2003-2008 Red Hat, Inc., James Morris <jmorris@redhat.com> 132069f457SEric Paris * Eric Paris <eparis@redhat.com> 141da177e4SLinus Torvalds * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc. 151da177e4SLinus Torvalds * <dgoeddel@trustedcs.com> 16effad8dfSPaul Moore * Copyright (C) 2006, 2007 Hewlett-Packard Development Company, L.P. 17effad8dfSPaul Moore * Paul Moore <paul.moore@hp.com> 18788e7dd4SYuichi Nakamura * Copyright (C) 2007 Hitachi Software Engineering Co., Ltd. 19788e7dd4SYuichi Nakamura * Yuichi Nakamura <ynakam@hitachisoft.jp> 201da177e4SLinus Torvalds * 211da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 221da177e4SLinus Torvalds * it under the terms of the GNU General Public License version 2, 231da177e4SLinus Torvalds * as published by the Free Software Foundation. 241da177e4SLinus Torvalds */ 251da177e4SLinus Torvalds 261da177e4SLinus Torvalds #include <linux/init.h> 271da177e4SLinus Torvalds #include <linux/kernel.h> 280d094efeSRoland McGrath #include <linux/tracehook.h> 291da177e4SLinus Torvalds #include <linux/errno.h> 301da177e4SLinus Torvalds #include <linux/sched.h> 311da177e4SLinus Torvalds #include <linux/security.h> 321da177e4SLinus Torvalds #include <linux/xattr.h> 331da177e4SLinus Torvalds #include <linux/capability.h> 341da177e4SLinus Torvalds #include <linux/unistd.h> 351da177e4SLinus Torvalds #include <linux/mm.h> 361da177e4SLinus Torvalds #include <linux/mman.h> 371da177e4SLinus Torvalds #include <linux/slab.h> 381da177e4SLinus Torvalds #include <linux/pagemap.h> 391da177e4SLinus Torvalds #include <linux/swap.h> 401da177e4SLinus Torvalds #include <linux/spinlock.h> 411da177e4SLinus Torvalds #include <linux/syscalls.h> 421da177e4SLinus Torvalds #include <linux/file.h> 439f3acc31SAl Viro #include <linux/fdtable.h> 441da177e4SLinus Torvalds #include <linux/namei.h> 451da177e4SLinus Torvalds #include <linux/mount.h> 461da177e4SLinus Torvalds #include <linux/proc_fs.h> 471da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h> 481da177e4SLinus Torvalds #include <linux/netfilter_ipv6.h> 491da177e4SLinus Torvalds #include <linux/tty.h> 501da177e4SLinus Torvalds #include <net/icmp.h> 51227b60f5SStephen Hemminger #include <net/ip.h> /* for local_port_range[] */ 521da177e4SLinus Torvalds #include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */ 53220deb96SPaul Moore #include <net/net_namespace.h> 54d621d35eSPaul Moore #include <net/netlabel.h> 55f5269710SEric Paris #include <linux/uaccess.h> 561da177e4SLinus Torvalds #include <asm/ioctls.h> 57d621d35eSPaul Moore #include <asm/atomic.h> 581da177e4SLinus Torvalds #include <linux/bitops.h> 591da177e4SLinus Torvalds #include <linux/interrupt.h> 601da177e4SLinus Torvalds #include <linux/netdevice.h> /* for network interface checks */ 611da177e4SLinus Torvalds #include <linux/netlink.h> 621da177e4SLinus Torvalds #include <linux/tcp.h> 631da177e4SLinus Torvalds #include <linux/udp.h> 642ee92d46SJames Morris #include <linux/dccp.h> 651da177e4SLinus Torvalds #include <linux/quota.h> 661da177e4SLinus Torvalds #include <linux/un.h> /* for Unix socket types */ 671da177e4SLinus Torvalds #include <net/af_unix.h> /* for Unix socket types */ 681da177e4SLinus Torvalds #include <linux/parser.h> 691da177e4SLinus Torvalds #include <linux/nfs_mount.h> 701da177e4SLinus Torvalds #include <net/ipv6.h> 711da177e4SLinus Torvalds #include <linux/hugetlb.h> 721da177e4SLinus Torvalds #include <linux/personality.h> 731da177e4SLinus Torvalds #include <linux/sysctl.h> 741da177e4SLinus Torvalds #include <linux/audit.h> 756931dfc9SEric Paris #include <linux/string.h> 76877ce7c1SCatherine Zhang #include <linux/selinux.h> 7723970741SEric Paris #include <linux/mutex.h> 78f06febc9SFrank Mayhar #include <linux/posix-timers.h> 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds #include "avc.h" 811da177e4SLinus Torvalds #include "objsec.h" 821da177e4SLinus Torvalds #include "netif.h" 83224dfbd8SPaul Moore #include "netnode.h" 843e112172SPaul Moore #include "netport.h" 85d28d1e08STrent Jaeger #include "xfrm.h" 86c60475bfSPaul Moore #include "netlabel.h" 879d57a7f9SAhmed S. Darwish #include "audit.h" 881da177e4SLinus Torvalds 891da177e4SLinus Torvalds #define XATTR_SELINUX_SUFFIX "selinux" 901da177e4SLinus Torvalds #define XATTR_NAME_SELINUX XATTR_SECURITY_PREFIX XATTR_SELINUX_SUFFIX 911da177e4SLinus Torvalds 9211689d47SDavid P. Quigley #define NUM_SEL_MNT_OPTS 5 93c9180a57SEric Paris 941da177e4SLinus Torvalds extern unsigned int policydb_loaded_version; 951da177e4SLinus Torvalds extern int selinux_nlmsg_lookup(u16 sclass, u16 nlmsg_type, u32 *perm); 9620510f2fSJames Morris extern struct security_operations *security_ops; 971da177e4SLinus Torvalds 98d621d35eSPaul Moore /* SECMARK reference count */ 99d621d35eSPaul Moore atomic_t selinux_secmark_refcount = ATOMIC_INIT(0); 100d621d35eSPaul Moore 1011da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP 102828dfe1dSEric Paris int selinux_enforcing; 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds static int __init enforcing_setup(char *str) 1051da177e4SLinus Torvalds { 106f5269710SEric Paris unsigned long enforcing; 107f5269710SEric Paris if (!strict_strtoul(str, 0, &enforcing)) 108f5269710SEric Paris selinux_enforcing = enforcing ? 1 : 0; 1091da177e4SLinus Torvalds return 1; 1101da177e4SLinus Torvalds } 1111da177e4SLinus Torvalds __setup("enforcing=", enforcing_setup); 1121da177e4SLinus Torvalds #endif 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM 1151da177e4SLinus Torvalds int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE; 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds static int __init selinux_enabled_setup(char *str) 1181da177e4SLinus Torvalds { 119f5269710SEric Paris unsigned long enabled; 120f5269710SEric Paris if (!strict_strtoul(str, 0, &enabled)) 121f5269710SEric Paris selinux_enabled = enabled ? 1 : 0; 1221da177e4SLinus Torvalds return 1; 1231da177e4SLinus Torvalds } 1241da177e4SLinus Torvalds __setup("selinux=", selinux_enabled_setup); 12530d55280SStephen Smalley #else 12630d55280SStephen Smalley int selinux_enabled = 1; 1271da177e4SLinus Torvalds #endif 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds 1306f0f0fd4SJames Morris /* 1316f0f0fd4SJames Morris * Minimal support for a secondary security module, 1326f0f0fd4SJames Morris * just to allow the use of the capability module. 1336f0f0fd4SJames Morris */ 134828dfe1dSEric Paris static struct security_operations *secondary_ops; 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds /* Lists of inode and superblock security structures initialized 1371da177e4SLinus Torvalds before the policy was loaded. */ 1381da177e4SLinus Torvalds static LIST_HEAD(superblock_security_head); 1391da177e4SLinus Torvalds static DEFINE_SPINLOCK(sb_security_lock); 1401da177e4SLinus Torvalds 141e18b890bSChristoph Lameter static struct kmem_cache *sel_inode_cache; 1427cae7e26SJames Morris 143d621d35eSPaul Moore /** 144d621d35eSPaul Moore * selinux_secmark_enabled - Check to see if SECMARK is currently enabled 145d621d35eSPaul Moore * 146d621d35eSPaul Moore * Description: 147d621d35eSPaul Moore * This function checks the SECMARK reference counter to see if any SECMARK 148d621d35eSPaul Moore * targets are currently configured, if the reference counter is greater than 149d621d35eSPaul Moore * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is 150d621d35eSPaul Moore * enabled, false (0) if SECMARK is disabled. 151d621d35eSPaul Moore * 152d621d35eSPaul Moore */ 153d621d35eSPaul Moore static int selinux_secmark_enabled(void) 154d621d35eSPaul Moore { 155d621d35eSPaul Moore return (atomic_read(&selinux_secmark_refcount) > 0); 156d621d35eSPaul Moore } 157d621d35eSPaul Moore 158d84f4f99SDavid Howells /* 159d84f4f99SDavid Howells * initialise the security for the init task 160d84f4f99SDavid Howells */ 161d84f4f99SDavid Howells static void cred_init_security(void) 1621da177e4SLinus Torvalds { 1633b11a1deSDavid Howells struct cred *cred = (struct cred *) current->real_cred; 1641da177e4SLinus Torvalds struct task_security_struct *tsec; 1651da177e4SLinus Torvalds 16689d155efSJames Morris tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL); 1671da177e4SLinus Torvalds if (!tsec) 168d84f4f99SDavid Howells panic("SELinux: Failed to initialize initial task.\n"); 1691da177e4SLinus Torvalds 170d84f4f99SDavid Howells tsec->osid = tsec->sid = SECINITSID_KERNEL; 171f1752eecSDavid Howells cred->security = tsec; 1721da177e4SLinus Torvalds } 1731da177e4SLinus Torvalds 174275bb41eSDavid Howells /* 17588e67f3bSDavid Howells * get the security ID of a set of credentials 17688e67f3bSDavid Howells */ 17788e67f3bSDavid Howells static inline u32 cred_sid(const struct cred *cred) 17888e67f3bSDavid Howells { 17988e67f3bSDavid Howells const struct task_security_struct *tsec; 18088e67f3bSDavid Howells 18188e67f3bSDavid Howells tsec = cred->security; 18288e67f3bSDavid Howells return tsec->sid; 18388e67f3bSDavid Howells } 18488e67f3bSDavid Howells 18588e67f3bSDavid Howells /* 1863b11a1deSDavid Howells * get the objective security ID of a task 187275bb41eSDavid Howells */ 188275bb41eSDavid Howells static inline u32 task_sid(const struct task_struct *task) 189275bb41eSDavid Howells { 190275bb41eSDavid Howells u32 sid; 191275bb41eSDavid Howells 192275bb41eSDavid Howells rcu_read_lock(); 19388e67f3bSDavid Howells sid = cred_sid(__task_cred(task)); 194275bb41eSDavid Howells rcu_read_unlock(); 195275bb41eSDavid Howells return sid; 196275bb41eSDavid Howells } 197275bb41eSDavid Howells 198275bb41eSDavid Howells /* 1993b11a1deSDavid Howells * get the subjective security ID of the current task 200275bb41eSDavid Howells */ 201275bb41eSDavid Howells static inline u32 current_sid(void) 202275bb41eSDavid Howells { 203275bb41eSDavid Howells const struct task_security_struct *tsec = current_cred()->security; 204275bb41eSDavid Howells 205275bb41eSDavid Howells return tsec->sid; 206275bb41eSDavid Howells } 207275bb41eSDavid Howells 20888e67f3bSDavid Howells /* Allocate and free functions for each kind of security blob. */ 20988e67f3bSDavid Howells 2101da177e4SLinus Torvalds static int inode_alloc_security(struct inode *inode) 2111da177e4SLinus Torvalds { 2121da177e4SLinus Torvalds struct inode_security_struct *isec; 213275bb41eSDavid Howells u32 sid = current_sid(); 2141da177e4SLinus Torvalds 215a02fe132SJosef Bacik isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS); 2161da177e4SLinus Torvalds if (!isec) 2171da177e4SLinus Torvalds return -ENOMEM; 2181da177e4SLinus Torvalds 21923970741SEric Paris mutex_init(&isec->lock); 2201da177e4SLinus Torvalds INIT_LIST_HEAD(&isec->list); 2211da177e4SLinus Torvalds isec->inode = inode; 2221da177e4SLinus Torvalds isec->sid = SECINITSID_UNLABELED; 2231da177e4SLinus Torvalds isec->sclass = SECCLASS_FILE; 224275bb41eSDavid Howells isec->task_sid = sid; 2251da177e4SLinus Torvalds inode->i_security = isec; 2261da177e4SLinus Torvalds 2271da177e4SLinus Torvalds return 0; 2281da177e4SLinus Torvalds } 2291da177e4SLinus Torvalds 2301da177e4SLinus Torvalds static void inode_free_security(struct inode *inode) 2311da177e4SLinus Torvalds { 2321da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 2331da177e4SLinus Torvalds struct superblock_security_struct *sbsec = inode->i_sb->s_security; 2341da177e4SLinus Torvalds 2351da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 2361da177e4SLinus Torvalds if (!list_empty(&isec->list)) 2371da177e4SLinus Torvalds list_del_init(&isec->list); 2381da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds inode->i_security = NULL; 2417cae7e26SJames Morris kmem_cache_free(sel_inode_cache, isec); 2421da177e4SLinus Torvalds } 2431da177e4SLinus Torvalds 2441da177e4SLinus Torvalds static int file_alloc_security(struct file *file) 2451da177e4SLinus Torvalds { 2461da177e4SLinus Torvalds struct file_security_struct *fsec; 247275bb41eSDavid Howells u32 sid = current_sid(); 2481da177e4SLinus Torvalds 24926d2a4beSStephen Smalley fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL); 2501da177e4SLinus Torvalds if (!fsec) 2511da177e4SLinus Torvalds return -ENOMEM; 2521da177e4SLinus Torvalds 253275bb41eSDavid Howells fsec->sid = sid; 254275bb41eSDavid Howells fsec->fown_sid = sid; 2551da177e4SLinus Torvalds file->f_security = fsec; 2561da177e4SLinus Torvalds 2571da177e4SLinus Torvalds return 0; 2581da177e4SLinus Torvalds } 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds static void file_free_security(struct file *file) 2611da177e4SLinus Torvalds { 2621da177e4SLinus Torvalds struct file_security_struct *fsec = file->f_security; 2631da177e4SLinus Torvalds file->f_security = NULL; 2641da177e4SLinus Torvalds kfree(fsec); 2651da177e4SLinus Torvalds } 2661da177e4SLinus Torvalds 2671da177e4SLinus Torvalds static int superblock_alloc_security(struct super_block *sb) 2681da177e4SLinus Torvalds { 2691da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 2701da177e4SLinus Torvalds 27189d155efSJames Morris sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL); 2721da177e4SLinus Torvalds if (!sbsec) 2731da177e4SLinus Torvalds return -ENOMEM; 2741da177e4SLinus Torvalds 275bc7e982bSEric Paris mutex_init(&sbsec->lock); 2761da177e4SLinus Torvalds INIT_LIST_HEAD(&sbsec->list); 2771da177e4SLinus Torvalds INIT_LIST_HEAD(&sbsec->isec_head); 2781da177e4SLinus Torvalds spin_lock_init(&sbsec->isec_lock); 2791da177e4SLinus Torvalds sbsec->sb = sb; 2801da177e4SLinus Torvalds sbsec->sid = SECINITSID_UNLABELED; 2811da177e4SLinus Torvalds sbsec->def_sid = SECINITSID_FILE; 282c312feb2SEric Paris sbsec->mntpoint_sid = SECINITSID_UNLABELED; 2831da177e4SLinus Torvalds sb->s_security = sbsec; 2841da177e4SLinus Torvalds 2851da177e4SLinus Torvalds return 0; 2861da177e4SLinus Torvalds } 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds static void superblock_free_security(struct super_block *sb) 2891da177e4SLinus Torvalds { 2901da177e4SLinus Torvalds struct superblock_security_struct *sbsec = sb->s_security; 2911da177e4SLinus Torvalds 2921da177e4SLinus Torvalds spin_lock(&sb_security_lock); 2931da177e4SLinus Torvalds if (!list_empty(&sbsec->list)) 2941da177e4SLinus Torvalds list_del_init(&sbsec->list); 2951da177e4SLinus Torvalds spin_unlock(&sb_security_lock); 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds sb->s_security = NULL; 2981da177e4SLinus Torvalds kfree(sbsec); 2991da177e4SLinus Torvalds } 3001da177e4SLinus Torvalds 3017d877f3bSAl Viro static int sk_alloc_security(struct sock *sk, int family, gfp_t priority) 3021da177e4SLinus Torvalds { 3031da177e4SLinus Torvalds struct sk_security_struct *ssec; 3041da177e4SLinus Torvalds 30589d155efSJames Morris ssec = kzalloc(sizeof(*ssec), priority); 3061da177e4SLinus Torvalds if (!ssec) 3071da177e4SLinus Torvalds return -ENOMEM; 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds ssec->peer_sid = SECINITSID_UNLABELED; 310892c141eSVenkat Yekkirala ssec->sid = SECINITSID_UNLABELED; 3111da177e4SLinus Torvalds sk->sk_security = ssec; 3121da177e4SLinus Torvalds 313389fb800SPaul Moore selinux_netlbl_sk_security_reset(ssec); 31499f59ed0SPaul Moore 3151da177e4SLinus Torvalds return 0; 3161da177e4SLinus Torvalds } 3171da177e4SLinus Torvalds 3181da177e4SLinus Torvalds static void sk_free_security(struct sock *sk) 3191da177e4SLinus Torvalds { 3201da177e4SLinus Torvalds struct sk_security_struct *ssec = sk->sk_security; 3211da177e4SLinus Torvalds 3221da177e4SLinus Torvalds sk->sk_security = NULL; 3236c5b3fc0SPaul Moore selinux_netlbl_sk_security_free(ssec); 3241da177e4SLinus Torvalds kfree(ssec); 3251da177e4SLinus Torvalds } 3261da177e4SLinus Torvalds 3271da177e4SLinus Torvalds /* The security server must be initialized before 3281da177e4SLinus Torvalds any labeling or access decisions can be provided. */ 3291da177e4SLinus Torvalds extern int ss_initialized; 3301da177e4SLinus Torvalds 3311da177e4SLinus Torvalds /* The file system's label must be initialized prior to use. */ 3321da177e4SLinus Torvalds 3331da177e4SLinus Torvalds static char *labeling_behaviors[6] = { 3341da177e4SLinus Torvalds "uses xattr", 3351da177e4SLinus Torvalds "uses transition SIDs", 3361da177e4SLinus Torvalds "uses task SIDs", 3371da177e4SLinus Torvalds "uses genfs_contexts", 3381da177e4SLinus Torvalds "not configured for labeling", 3391da177e4SLinus Torvalds "uses mountpoint labeling", 3401da177e4SLinus Torvalds }; 3411da177e4SLinus Torvalds 3421da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry); 3431da177e4SLinus Torvalds 3441da177e4SLinus Torvalds static inline int inode_doinit(struct inode *inode) 3451da177e4SLinus Torvalds { 3461da177e4SLinus Torvalds return inode_doinit_with_dentry(inode, NULL); 3471da177e4SLinus Torvalds } 3481da177e4SLinus Torvalds 3491da177e4SLinus Torvalds enum { 35031e87930SEric Paris Opt_error = -1, 3511da177e4SLinus Torvalds Opt_context = 1, 3521da177e4SLinus Torvalds Opt_fscontext = 2, 353c9180a57SEric Paris Opt_defcontext = 3, 354c9180a57SEric Paris Opt_rootcontext = 4, 35511689d47SDavid P. Quigley Opt_labelsupport = 5, 3561da177e4SLinus Torvalds }; 3571da177e4SLinus Torvalds 358a447c093SSteven Whitehouse static const match_table_t tokens = { 359832cbd9aSEric Paris {Opt_context, CONTEXT_STR "%s"}, 360832cbd9aSEric Paris {Opt_fscontext, FSCONTEXT_STR "%s"}, 361832cbd9aSEric Paris {Opt_defcontext, DEFCONTEXT_STR "%s"}, 362832cbd9aSEric Paris {Opt_rootcontext, ROOTCONTEXT_STR "%s"}, 36311689d47SDavid P. Quigley {Opt_labelsupport, LABELSUPP_STR}, 36431e87930SEric Paris {Opt_error, NULL}, 3651da177e4SLinus Torvalds }; 3661da177e4SLinus Torvalds 3671da177e4SLinus Torvalds #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n" 3681da177e4SLinus Torvalds 369c312feb2SEric Paris static int may_context_mount_sb_relabel(u32 sid, 370c312feb2SEric Paris struct superblock_security_struct *sbsec, 371275bb41eSDavid Howells const struct cred *cred) 372c312feb2SEric Paris { 373275bb41eSDavid Howells const struct task_security_struct *tsec = cred->security; 374c312feb2SEric Paris int rc; 375c312feb2SEric Paris 376c312feb2SEric Paris rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 377c312feb2SEric Paris FILESYSTEM__RELABELFROM, NULL); 378c312feb2SEric Paris if (rc) 379c312feb2SEric Paris return rc; 380c312feb2SEric Paris 381c312feb2SEric Paris rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM, 382c312feb2SEric Paris FILESYSTEM__RELABELTO, NULL); 383c312feb2SEric Paris return rc; 384c312feb2SEric Paris } 385c312feb2SEric Paris 3860808925eSEric Paris static int may_context_mount_inode_relabel(u32 sid, 3870808925eSEric Paris struct superblock_security_struct *sbsec, 388275bb41eSDavid Howells const struct cred *cred) 3890808925eSEric Paris { 390275bb41eSDavid Howells const struct task_security_struct *tsec = cred->security; 3910808925eSEric Paris int rc; 3920808925eSEric Paris rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, 3930808925eSEric Paris FILESYSTEM__RELABELFROM, NULL); 3940808925eSEric Paris if (rc) 3950808925eSEric Paris return rc; 3960808925eSEric Paris 3970808925eSEric Paris rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, 3980808925eSEric Paris FILESYSTEM__ASSOCIATE, NULL); 3990808925eSEric Paris return rc; 4000808925eSEric Paris } 4010808925eSEric Paris 402c9180a57SEric Paris static int sb_finish_set_opts(struct super_block *sb) 4031da177e4SLinus Torvalds { 4041da177e4SLinus Torvalds struct superblock_security_struct *sbsec = sb->s_security; 4051da177e4SLinus Torvalds struct dentry *root = sb->s_root; 406c9180a57SEric Paris struct inode *root_inode = root->d_inode; 4071da177e4SLinus Torvalds int rc = 0; 4081da177e4SLinus Torvalds 4091da177e4SLinus Torvalds if (sbsec->behavior == SECURITY_FS_USE_XATTR) { 4101da177e4SLinus Torvalds /* Make sure that the xattr handler exists and that no 4111da177e4SLinus Torvalds error other than -ENODATA is returned by getxattr on 4121da177e4SLinus Torvalds the root directory. -ENODATA is ok, as this may be 4131da177e4SLinus Torvalds the first boot of the SELinux kernel before we have 4141da177e4SLinus Torvalds assigned xattr values to the filesystem. */ 415c9180a57SEric Paris if (!root_inode->i_op->getxattr) { 4161da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: (dev %s, type %s) has no " 4171da177e4SLinus Torvalds "xattr support\n", sb->s_id, sb->s_type->name); 4181da177e4SLinus Torvalds rc = -EOPNOTSUPP; 4191da177e4SLinus Torvalds goto out; 4201da177e4SLinus Torvalds } 421c9180a57SEric Paris rc = root_inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0); 4221da177e4SLinus Torvalds if (rc < 0 && rc != -ENODATA) { 4231da177e4SLinus Torvalds if (rc == -EOPNOTSUPP) 4241da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: (dev %s, type " 4251da177e4SLinus Torvalds "%s) has no security xattr handler\n", 4261da177e4SLinus Torvalds sb->s_id, sb->s_type->name); 4271da177e4SLinus Torvalds else 4281da177e4SLinus Torvalds printk(KERN_WARNING "SELinux: (dev %s, type " 4291da177e4SLinus Torvalds "%s) getxattr errno %d\n", sb->s_id, 4301da177e4SLinus Torvalds sb->s_type->name, -rc); 4311da177e4SLinus Torvalds goto out; 4321da177e4SLinus Torvalds } 4331da177e4SLinus Torvalds } 4341da177e4SLinus Torvalds 43511689d47SDavid P. Quigley sbsec->flags |= (SE_SBINITIALIZED | SE_SBLABELSUPP); 4361da177e4SLinus Torvalds 437c9180a57SEric Paris if (sbsec->behavior > ARRAY_SIZE(labeling_behaviors)) 438fadcdb45SEric Paris printk(KERN_ERR "SELinux: initialized (dev %s, type %s), unknown behavior\n", 4391da177e4SLinus Torvalds sb->s_id, sb->s_type->name); 440c9180a57SEric Paris else 441fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: initialized (dev %s, type %s), %s\n", 4421da177e4SLinus Torvalds sb->s_id, sb->s_type->name, 4431da177e4SLinus Torvalds labeling_behaviors[sbsec->behavior-1]); 4441da177e4SLinus Torvalds 44511689d47SDavid P. Quigley if (sbsec->behavior == SECURITY_FS_USE_GENFS || 44611689d47SDavid P. Quigley sbsec->behavior == SECURITY_FS_USE_MNTPOINT || 44711689d47SDavid P. Quigley sbsec->behavior == SECURITY_FS_USE_NONE || 44811689d47SDavid P. Quigley sbsec->behavior > ARRAY_SIZE(labeling_behaviors)) 44911689d47SDavid P. Quigley sbsec->flags &= ~SE_SBLABELSUPP; 45011689d47SDavid P. Quigley 4511da177e4SLinus Torvalds /* Initialize the root inode. */ 452c9180a57SEric Paris rc = inode_doinit_with_dentry(root_inode, root); 4531da177e4SLinus Torvalds 4541da177e4SLinus Torvalds /* Initialize any other inodes associated with the superblock, e.g. 4551da177e4SLinus Torvalds inodes created prior to initial policy load or inodes created 4561da177e4SLinus Torvalds during get_sb by a pseudo filesystem that directly 4571da177e4SLinus Torvalds populates itself. */ 4581da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 4591da177e4SLinus Torvalds next_inode: 4601da177e4SLinus Torvalds if (!list_empty(&sbsec->isec_head)) { 4611da177e4SLinus Torvalds struct inode_security_struct *isec = 4621da177e4SLinus Torvalds list_entry(sbsec->isec_head.next, 4631da177e4SLinus Torvalds struct inode_security_struct, list); 4641da177e4SLinus Torvalds struct inode *inode = isec->inode; 4651da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 4661da177e4SLinus Torvalds inode = igrab(inode); 4671da177e4SLinus Torvalds if (inode) { 4681da177e4SLinus Torvalds if (!IS_PRIVATE(inode)) 4691da177e4SLinus Torvalds inode_doinit(inode); 4701da177e4SLinus Torvalds iput(inode); 4711da177e4SLinus Torvalds } 4721da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 4731da177e4SLinus Torvalds list_del_init(&isec->list); 4741da177e4SLinus Torvalds goto next_inode; 4751da177e4SLinus Torvalds } 4761da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 4771da177e4SLinus Torvalds out: 478c9180a57SEric Paris return rc; 479c9180a57SEric Paris } 480c9180a57SEric Paris 481c9180a57SEric Paris /* 482c9180a57SEric Paris * This function should allow an FS to ask what it's mount security 483c9180a57SEric Paris * options were so it can use those later for submounts, displaying 484c9180a57SEric Paris * mount options, or whatever. 485c9180a57SEric Paris */ 486c9180a57SEric Paris static int selinux_get_mnt_opts(const struct super_block *sb, 487e0007529SEric Paris struct security_mnt_opts *opts) 488c9180a57SEric Paris { 489c9180a57SEric Paris int rc = 0, i; 490c9180a57SEric Paris struct superblock_security_struct *sbsec = sb->s_security; 491c9180a57SEric Paris char *context = NULL; 492c9180a57SEric Paris u32 len; 493c9180a57SEric Paris char tmp; 494c9180a57SEric Paris 495e0007529SEric Paris security_init_mnt_opts(opts); 496c9180a57SEric Paris 4970d90a7ecSDavid P. Quigley if (!(sbsec->flags & SE_SBINITIALIZED)) 498c9180a57SEric Paris return -EINVAL; 499c9180a57SEric Paris 500c9180a57SEric Paris if (!ss_initialized) 501c9180a57SEric Paris return -EINVAL; 502c9180a57SEric Paris 5030d90a7ecSDavid P. Quigley tmp = sbsec->flags & SE_MNTMASK; 504c9180a57SEric Paris /* count the number of mount options for this sb */ 505c9180a57SEric Paris for (i = 0; i < 8; i++) { 506c9180a57SEric Paris if (tmp & 0x01) 507e0007529SEric Paris opts->num_mnt_opts++; 508c9180a57SEric Paris tmp >>= 1; 509c9180a57SEric Paris } 51011689d47SDavid P. Quigley /* Check if the Label support flag is set */ 51111689d47SDavid P. Quigley if (sbsec->flags & SE_SBLABELSUPP) 51211689d47SDavid P. Quigley opts->num_mnt_opts++; 513c9180a57SEric Paris 514e0007529SEric Paris opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC); 515e0007529SEric Paris if (!opts->mnt_opts) { 516c9180a57SEric Paris rc = -ENOMEM; 517c9180a57SEric Paris goto out_free; 518c9180a57SEric Paris } 519c9180a57SEric Paris 520e0007529SEric Paris opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC); 521e0007529SEric Paris if (!opts->mnt_opts_flags) { 522c9180a57SEric Paris rc = -ENOMEM; 523c9180a57SEric Paris goto out_free; 524c9180a57SEric Paris } 525c9180a57SEric Paris 526c9180a57SEric Paris i = 0; 527c9180a57SEric Paris if (sbsec->flags & FSCONTEXT_MNT) { 528c9180a57SEric Paris rc = security_sid_to_context(sbsec->sid, &context, &len); 529c9180a57SEric Paris if (rc) 530c9180a57SEric Paris goto out_free; 531e0007529SEric Paris opts->mnt_opts[i] = context; 532e0007529SEric Paris opts->mnt_opts_flags[i++] = FSCONTEXT_MNT; 533c9180a57SEric Paris } 534c9180a57SEric Paris if (sbsec->flags & CONTEXT_MNT) { 535c9180a57SEric Paris rc = security_sid_to_context(sbsec->mntpoint_sid, &context, &len); 536c9180a57SEric Paris if (rc) 537c9180a57SEric Paris goto out_free; 538e0007529SEric Paris opts->mnt_opts[i] = context; 539e0007529SEric Paris opts->mnt_opts_flags[i++] = CONTEXT_MNT; 540c9180a57SEric Paris } 541c9180a57SEric Paris if (sbsec->flags & DEFCONTEXT_MNT) { 542c9180a57SEric Paris rc = security_sid_to_context(sbsec->def_sid, &context, &len); 543c9180a57SEric Paris if (rc) 544c9180a57SEric Paris goto out_free; 545e0007529SEric Paris opts->mnt_opts[i] = context; 546e0007529SEric Paris opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT; 547c9180a57SEric Paris } 548c9180a57SEric Paris if (sbsec->flags & ROOTCONTEXT_MNT) { 549c9180a57SEric Paris struct inode *root = sbsec->sb->s_root->d_inode; 550c9180a57SEric Paris struct inode_security_struct *isec = root->i_security; 551c9180a57SEric Paris 552c9180a57SEric Paris rc = security_sid_to_context(isec->sid, &context, &len); 553c9180a57SEric Paris if (rc) 554c9180a57SEric Paris goto out_free; 555e0007529SEric Paris opts->mnt_opts[i] = context; 556e0007529SEric Paris opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT; 557c9180a57SEric Paris } 55811689d47SDavid P. Quigley if (sbsec->flags & SE_SBLABELSUPP) { 55911689d47SDavid P. Quigley opts->mnt_opts[i] = NULL; 56011689d47SDavid P. Quigley opts->mnt_opts_flags[i++] = SE_SBLABELSUPP; 56111689d47SDavid P. Quigley } 562c9180a57SEric Paris 563e0007529SEric Paris BUG_ON(i != opts->num_mnt_opts); 564c9180a57SEric Paris 565c9180a57SEric Paris return 0; 566c9180a57SEric Paris 567c9180a57SEric Paris out_free: 568e0007529SEric Paris security_free_mnt_opts(opts); 569c9180a57SEric Paris return rc; 570c9180a57SEric Paris } 571c9180a57SEric Paris 572c9180a57SEric Paris static int bad_option(struct superblock_security_struct *sbsec, char flag, 573c9180a57SEric Paris u32 old_sid, u32 new_sid) 574c9180a57SEric Paris { 5750d90a7ecSDavid P. Quigley char mnt_flags = sbsec->flags & SE_MNTMASK; 5760d90a7ecSDavid P. Quigley 577c9180a57SEric Paris /* check if the old mount command had the same options */ 5780d90a7ecSDavid P. Quigley if (sbsec->flags & SE_SBINITIALIZED) 579c9180a57SEric Paris if (!(sbsec->flags & flag) || 580c9180a57SEric Paris (old_sid != new_sid)) 581c9180a57SEric Paris return 1; 582c9180a57SEric Paris 583c9180a57SEric Paris /* check if we were passed the same options twice, 584c9180a57SEric Paris * aka someone passed context=a,context=b 585c9180a57SEric Paris */ 5860d90a7ecSDavid P. Quigley if (!(sbsec->flags & SE_SBINITIALIZED)) 5870d90a7ecSDavid P. Quigley if (mnt_flags & flag) 588c9180a57SEric Paris return 1; 589c9180a57SEric Paris return 0; 590c9180a57SEric Paris } 591e0007529SEric Paris 592c9180a57SEric Paris /* 593c9180a57SEric Paris * Allow filesystems with binary mount data to explicitly set mount point 594c9180a57SEric Paris * labeling information. 595c9180a57SEric Paris */ 596e0007529SEric Paris static int selinux_set_mnt_opts(struct super_block *sb, 597e0007529SEric Paris struct security_mnt_opts *opts) 598c9180a57SEric Paris { 599275bb41eSDavid Howells const struct cred *cred = current_cred(); 600c9180a57SEric Paris int rc = 0, i; 601c9180a57SEric Paris struct superblock_security_struct *sbsec = sb->s_security; 602c9180a57SEric Paris const char *name = sb->s_type->name; 603089be43eSJames Morris struct inode *inode = sbsec->sb->s_root->d_inode; 604089be43eSJames Morris struct inode_security_struct *root_isec = inode->i_security; 605c9180a57SEric Paris u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0; 606c9180a57SEric Paris u32 defcontext_sid = 0; 607e0007529SEric Paris char **mount_options = opts->mnt_opts; 608e0007529SEric Paris int *flags = opts->mnt_opts_flags; 609e0007529SEric Paris int num_opts = opts->num_mnt_opts; 610c9180a57SEric Paris 611c9180a57SEric Paris mutex_lock(&sbsec->lock); 612c9180a57SEric Paris 613c9180a57SEric Paris if (!ss_initialized) { 614c9180a57SEric Paris if (!num_opts) { 615c9180a57SEric Paris /* Defer initialization until selinux_complete_init, 616c9180a57SEric Paris after the initial policy is loaded and the security 617c9180a57SEric Paris server is ready to handle calls. */ 618c9180a57SEric Paris spin_lock(&sb_security_lock); 619c9180a57SEric Paris if (list_empty(&sbsec->list)) 620c9180a57SEric Paris list_add(&sbsec->list, &superblock_security_head); 621c9180a57SEric Paris spin_unlock(&sb_security_lock); 622c9180a57SEric Paris goto out; 623c9180a57SEric Paris } 624c9180a57SEric Paris rc = -EINVAL; 625744ba35eSEric Paris printk(KERN_WARNING "SELinux: Unable to set superblock options " 626744ba35eSEric Paris "before the security server is initialized\n"); 627c9180a57SEric Paris goto out; 628c9180a57SEric Paris } 629c9180a57SEric Paris 630c9180a57SEric Paris /* 631e0007529SEric Paris * Binary mount data FS will come through this function twice. Once 632e0007529SEric Paris * from an explicit call and once from the generic calls from the vfs. 633e0007529SEric Paris * Since the generic VFS calls will not contain any security mount data 634e0007529SEric Paris * we need to skip the double mount verification. 635e0007529SEric Paris * 636e0007529SEric Paris * This does open a hole in which we will not notice if the first 637e0007529SEric Paris * mount using this sb set explict options and a second mount using 638e0007529SEric Paris * this sb does not set any security options. (The first options 639e0007529SEric Paris * will be used for both mounts) 640e0007529SEric Paris */ 6410d90a7ecSDavid P. Quigley if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA) 642e0007529SEric Paris && (num_opts == 0)) 643e0007529SEric Paris goto out; 644e0007529SEric Paris 645e0007529SEric Paris /* 646c9180a57SEric Paris * parse the mount options, check if they are valid sids. 647c9180a57SEric Paris * also check if someone is trying to mount the same sb more 648c9180a57SEric Paris * than once with different security options. 649c9180a57SEric Paris */ 650c9180a57SEric Paris for (i = 0; i < num_opts; i++) { 651c9180a57SEric Paris u32 sid; 65211689d47SDavid P. Quigley 65311689d47SDavid P. Quigley if (flags[i] == SE_SBLABELSUPP) 65411689d47SDavid P. Quigley continue; 655c9180a57SEric Paris rc = security_context_to_sid(mount_options[i], 656c9180a57SEric Paris strlen(mount_options[i]), &sid); 657c9180a57SEric Paris if (rc) { 658c9180a57SEric Paris printk(KERN_WARNING "SELinux: security_context_to_sid" 659c9180a57SEric Paris "(%s) failed for (dev %s, type %s) errno=%d\n", 660c9180a57SEric Paris mount_options[i], sb->s_id, name, rc); 661c9180a57SEric Paris goto out; 662c9180a57SEric Paris } 663c9180a57SEric Paris switch (flags[i]) { 664c9180a57SEric Paris case FSCONTEXT_MNT: 665c9180a57SEric Paris fscontext_sid = sid; 666c9180a57SEric Paris 667c9180a57SEric Paris if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, 668c9180a57SEric Paris fscontext_sid)) 669c9180a57SEric Paris goto out_double_mount; 670c9180a57SEric Paris 671c9180a57SEric Paris sbsec->flags |= FSCONTEXT_MNT; 672c9180a57SEric Paris break; 673c9180a57SEric Paris case CONTEXT_MNT: 674c9180a57SEric Paris context_sid = sid; 675c9180a57SEric Paris 676c9180a57SEric Paris if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, 677c9180a57SEric Paris context_sid)) 678c9180a57SEric Paris goto out_double_mount; 679c9180a57SEric Paris 680c9180a57SEric Paris sbsec->flags |= CONTEXT_MNT; 681c9180a57SEric Paris break; 682c9180a57SEric Paris case ROOTCONTEXT_MNT: 683c9180a57SEric Paris rootcontext_sid = sid; 684c9180a57SEric Paris 685c9180a57SEric Paris if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, 686c9180a57SEric Paris rootcontext_sid)) 687c9180a57SEric Paris goto out_double_mount; 688c9180a57SEric Paris 689c9180a57SEric Paris sbsec->flags |= ROOTCONTEXT_MNT; 690c9180a57SEric Paris 691c9180a57SEric Paris break; 692c9180a57SEric Paris case DEFCONTEXT_MNT: 693c9180a57SEric Paris defcontext_sid = sid; 694c9180a57SEric Paris 695c9180a57SEric Paris if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, 696c9180a57SEric Paris defcontext_sid)) 697c9180a57SEric Paris goto out_double_mount; 698c9180a57SEric Paris 699c9180a57SEric Paris sbsec->flags |= DEFCONTEXT_MNT; 700c9180a57SEric Paris 701c9180a57SEric Paris break; 702c9180a57SEric Paris default: 703c9180a57SEric Paris rc = -EINVAL; 704c9180a57SEric Paris goto out; 705c9180a57SEric Paris } 706c9180a57SEric Paris } 707c9180a57SEric Paris 7080d90a7ecSDavid P. Quigley if (sbsec->flags & SE_SBINITIALIZED) { 709c9180a57SEric Paris /* previously mounted with options, but not on this attempt? */ 7100d90a7ecSDavid P. Quigley if ((sbsec->flags & SE_MNTMASK) && !num_opts) 711c9180a57SEric Paris goto out_double_mount; 712c9180a57SEric Paris rc = 0; 713c9180a57SEric Paris goto out; 714c9180a57SEric Paris } 715c9180a57SEric Paris 716089be43eSJames Morris if (strcmp(sb->s_type->name, "proc") == 0) 7170d90a7ecSDavid P. Quigley sbsec->flags |= SE_SBPROC; 718c9180a57SEric Paris 719c9180a57SEric Paris /* Determine the labeling behavior to use for this filesystem type. */ 7200d90a7ecSDavid P. Quigley rc = security_fs_use((sbsec->flags & SE_SBPROC) ? "proc" : sb->s_type->name, &sbsec->behavior, &sbsec->sid); 721c9180a57SEric Paris if (rc) { 722c9180a57SEric Paris printk(KERN_WARNING "%s: security_fs_use(%s) returned %d\n", 723089be43eSJames Morris __func__, sb->s_type->name, rc); 724c9180a57SEric Paris goto out; 725c9180a57SEric Paris } 726c9180a57SEric Paris 727c9180a57SEric Paris /* sets the context of the superblock for the fs being mounted. */ 728c9180a57SEric Paris if (fscontext_sid) { 729275bb41eSDavid Howells rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred); 730c9180a57SEric Paris if (rc) 731c9180a57SEric Paris goto out; 732c9180a57SEric Paris 733c9180a57SEric Paris sbsec->sid = fscontext_sid; 734c9180a57SEric Paris } 735c9180a57SEric Paris 736c9180a57SEric Paris /* 737c9180a57SEric Paris * Switch to using mount point labeling behavior. 738c9180a57SEric Paris * sets the label used on all file below the mountpoint, and will set 739c9180a57SEric Paris * the superblock context if not already set. 740c9180a57SEric Paris */ 741c9180a57SEric Paris if (context_sid) { 742c9180a57SEric Paris if (!fscontext_sid) { 743275bb41eSDavid Howells rc = may_context_mount_sb_relabel(context_sid, sbsec, 744275bb41eSDavid Howells cred); 745c9180a57SEric Paris if (rc) 746c9180a57SEric Paris goto out; 747c9180a57SEric Paris sbsec->sid = context_sid; 748c9180a57SEric Paris } else { 749275bb41eSDavid Howells rc = may_context_mount_inode_relabel(context_sid, sbsec, 750275bb41eSDavid Howells cred); 751c9180a57SEric Paris if (rc) 752c9180a57SEric Paris goto out; 753c9180a57SEric Paris } 754c9180a57SEric Paris if (!rootcontext_sid) 755c9180a57SEric Paris rootcontext_sid = context_sid; 756c9180a57SEric Paris 757c9180a57SEric Paris sbsec->mntpoint_sid = context_sid; 758c9180a57SEric Paris sbsec->behavior = SECURITY_FS_USE_MNTPOINT; 759c9180a57SEric Paris } 760c9180a57SEric Paris 761c9180a57SEric Paris if (rootcontext_sid) { 762275bb41eSDavid Howells rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec, 763275bb41eSDavid Howells cred); 764c9180a57SEric Paris if (rc) 765c9180a57SEric Paris goto out; 766c9180a57SEric Paris 767c9180a57SEric Paris root_isec->sid = rootcontext_sid; 768c9180a57SEric Paris root_isec->initialized = 1; 769c9180a57SEric Paris } 770c9180a57SEric Paris 771c9180a57SEric Paris if (defcontext_sid) { 772c9180a57SEric Paris if (sbsec->behavior != SECURITY_FS_USE_XATTR) { 773c9180a57SEric Paris rc = -EINVAL; 774c9180a57SEric Paris printk(KERN_WARNING "SELinux: defcontext option is " 775c9180a57SEric Paris "invalid for this filesystem type\n"); 776c9180a57SEric Paris goto out; 777c9180a57SEric Paris } 778c9180a57SEric Paris 779c9180a57SEric Paris if (defcontext_sid != sbsec->def_sid) { 780c9180a57SEric Paris rc = may_context_mount_inode_relabel(defcontext_sid, 781275bb41eSDavid Howells sbsec, cred); 782c9180a57SEric Paris if (rc) 783c9180a57SEric Paris goto out; 784c9180a57SEric Paris } 785c9180a57SEric Paris 786c9180a57SEric Paris sbsec->def_sid = defcontext_sid; 787c9180a57SEric Paris } 788c9180a57SEric Paris 789c9180a57SEric Paris rc = sb_finish_set_opts(sb); 790c9180a57SEric Paris out: 791bc7e982bSEric Paris mutex_unlock(&sbsec->lock); 7921da177e4SLinus Torvalds return rc; 793c9180a57SEric Paris out_double_mount: 794c9180a57SEric Paris rc = -EINVAL; 795c9180a57SEric Paris printk(KERN_WARNING "SELinux: mount invalid. Same superblock, different " 796c9180a57SEric Paris "security settings for (dev %s, type %s)\n", sb->s_id, name); 797c9180a57SEric Paris goto out; 798c9180a57SEric Paris } 799c9180a57SEric Paris 800c9180a57SEric Paris static void selinux_sb_clone_mnt_opts(const struct super_block *oldsb, 801c9180a57SEric Paris struct super_block *newsb) 802c9180a57SEric Paris { 803c9180a57SEric Paris const struct superblock_security_struct *oldsbsec = oldsb->s_security; 804c9180a57SEric Paris struct superblock_security_struct *newsbsec = newsb->s_security; 805c9180a57SEric Paris 806c9180a57SEric Paris int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT); 807c9180a57SEric Paris int set_context = (oldsbsec->flags & CONTEXT_MNT); 808c9180a57SEric Paris int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT); 809c9180a57SEric Paris 8100f5e6420SEric Paris /* 8110f5e6420SEric Paris * if the parent was able to be mounted it clearly had no special lsm 8120f5e6420SEric Paris * mount options. thus we can safely put this sb on the list and deal 8130f5e6420SEric Paris * with it later 8140f5e6420SEric Paris */ 8150f5e6420SEric Paris if (!ss_initialized) { 8160f5e6420SEric Paris spin_lock(&sb_security_lock); 8170f5e6420SEric Paris if (list_empty(&newsbsec->list)) 8180f5e6420SEric Paris list_add(&newsbsec->list, &superblock_security_head); 8190f5e6420SEric Paris spin_unlock(&sb_security_lock); 8200f5e6420SEric Paris return; 8210f5e6420SEric Paris } 822c9180a57SEric Paris 823c9180a57SEric Paris /* how can we clone if the old one wasn't set up?? */ 8240d90a7ecSDavid P. Quigley BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED)); 825c9180a57SEric Paris 8265a552617SEric Paris /* if fs is reusing a sb, just let its options stand... */ 8270d90a7ecSDavid P. Quigley if (newsbsec->flags & SE_SBINITIALIZED) 8285a552617SEric Paris return; 8295a552617SEric Paris 830c9180a57SEric Paris mutex_lock(&newsbsec->lock); 831c9180a57SEric Paris 832c9180a57SEric Paris newsbsec->flags = oldsbsec->flags; 833c9180a57SEric Paris 834c9180a57SEric Paris newsbsec->sid = oldsbsec->sid; 835c9180a57SEric Paris newsbsec->def_sid = oldsbsec->def_sid; 836c9180a57SEric Paris newsbsec->behavior = oldsbsec->behavior; 837c9180a57SEric Paris 838c9180a57SEric Paris if (set_context) { 839c9180a57SEric Paris u32 sid = oldsbsec->mntpoint_sid; 840c9180a57SEric Paris 841c9180a57SEric Paris if (!set_fscontext) 842c9180a57SEric Paris newsbsec->sid = sid; 843c9180a57SEric Paris if (!set_rootcontext) { 844c9180a57SEric Paris struct inode *newinode = newsb->s_root->d_inode; 845c9180a57SEric Paris struct inode_security_struct *newisec = newinode->i_security; 846c9180a57SEric Paris newisec->sid = sid; 847c9180a57SEric Paris } 848c9180a57SEric Paris newsbsec->mntpoint_sid = sid; 849c9180a57SEric Paris } 850c9180a57SEric Paris if (set_rootcontext) { 851c9180a57SEric Paris const struct inode *oldinode = oldsb->s_root->d_inode; 852c9180a57SEric Paris const struct inode_security_struct *oldisec = oldinode->i_security; 853c9180a57SEric Paris struct inode *newinode = newsb->s_root->d_inode; 854c9180a57SEric Paris struct inode_security_struct *newisec = newinode->i_security; 855c9180a57SEric Paris 856c9180a57SEric Paris newisec->sid = oldisec->sid; 857c9180a57SEric Paris } 858c9180a57SEric Paris 859c9180a57SEric Paris sb_finish_set_opts(newsb); 860c9180a57SEric Paris mutex_unlock(&newsbsec->lock); 861c9180a57SEric Paris } 862c9180a57SEric Paris 8632e1479d9SAdrian Bunk static int selinux_parse_opts_str(char *options, 8642e1479d9SAdrian Bunk struct security_mnt_opts *opts) 865c9180a57SEric Paris { 866e0007529SEric Paris char *p; 867c9180a57SEric Paris char *context = NULL, *defcontext = NULL; 868c9180a57SEric Paris char *fscontext = NULL, *rootcontext = NULL; 869e0007529SEric Paris int rc, num_mnt_opts = 0; 870c9180a57SEric Paris 871e0007529SEric Paris opts->num_mnt_opts = 0; 872c9180a57SEric Paris 873c9180a57SEric Paris /* Standard string-based options. */ 874c9180a57SEric Paris while ((p = strsep(&options, "|")) != NULL) { 875c9180a57SEric Paris int token; 876c9180a57SEric Paris substring_t args[MAX_OPT_ARGS]; 877c9180a57SEric Paris 878c9180a57SEric Paris if (!*p) 879c9180a57SEric Paris continue; 880c9180a57SEric Paris 881c9180a57SEric Paris token = match_token(p, tokens, args); 882c9180a57SEric Paris 883c9180a57SEric Paris switch (token) { 884c9180a57SEric Paris case Opt_context: 885c9180a57SEric Paris if (context || defcontext) { 886c9180a57SEric Paris rc = -EINVAL; 887c9180a57SEric Paris printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 888c9180a57SEric Paris goto out_err; 889c9180a57SEric Paris } 890c9180a57SEric Paris context = match_strdup(&args[0]); 891c9180a57SEric Paris if (!context) { 892c9180a57SEric Paris rc = -ENOMEM; 893c9180a57SEric Paris goto out_err; 894c9180a57SEric Paris } 895c9180a57SEric Paris break; 896c9180a57SEric Paris 897c9180a57SEric Paris case Opt_fscontext: 898c9180a57SEric Paris if (fscontext) { 899c9180a57SEric Paris rc = -EINVAL; 900c9180a57SEric Paris printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 901c9180a57SEric Paris goto out_err; 902c9180a57SEric Paris } 903c9180a57SEric Paris fscontext = match_strdup(&args[0]); 904c9180a57SEric Paris if (!fscontext) { 905c9180a57SEric Paris rc = -ENOMEM; 906c9180a57SEric Paris goto out_err; 907c9180a57SEric Paris } 908c9180a57SEric Paris break; 909c9180a57SEric Paris 910c9180a57SEric Paris case Opt_rootcontext: 911c9180a57SEric Paris if (rootcontext) { 912c9180a57SEric Paris rc = -EINVAL; 913c9180a57SEric Paris printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 914c9180a57SEric Paris goto out_err; 915c9180a57SEric Paris } 916c9180a57SEric Paris rootcontext = match_strdup(&args[0]); 917c9180a57SEric Paris if (!rootcontext) { 918c9180a57SEric Paris rc = -ENOMEM; 919c9180a57SEric Paris goto out_err; 920c9180a57SEric Paris } 921c9180a57SEric Paris break; 922c9180a57SEric Paris 923c9180a57SEric Paris case Opt_defcontext: 924c9180a57SEric Paris if (context || defcontext) { 925c9180a57SEric Paris rc = -EINVAL; 926c9180a57SEric Paris printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); 927c9180a57SEric Paris goto out_err; 928c9180a57SEric Paris } 929c9180a57SEric Paris defcontext = match_strdup(&args[0]); 930c9180a57SEric Paris if (!defcontext) { 931c9180a57SEric Paris rc = -ENOMEM; 932c9180a57SEric Paris goto out_err; 933c9180a57SEric Paris } 934c9180a57SEric Paris break; 93511689d47SDavid P. Quigley case Opt_labelsupport: 93611689d47SDavid P. Quigley break; 937c9180a57SEric Paris default: 938c9180a57SEric Paris rc = -EINVAL; 939c9180a57SEric Paris printk(KERN_WARNING "SELinux: unknown mount option\n"); 940c9180a57SEric Paris goto out_err; 941c9180a57SEric Paris 942c9180a57SEric Paris } 943c9180a57SEric Paris } 944c9180a57SEric Paris 945e0007529SEric Paris rc = -ENOMEM; 946e0007529SEric Paris opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_ATOMIC); 947e0007529SEric Paris if (!opts->mnt_opts) 948e0007529SEric Paris goto out_err; 949e0007529SEric Paris 950e0007529SEric Paris opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), GFP_ATOMIC); 951e0007529SEric Paris if (!opts->mnt_opts_flags) { 952e0007529SEric Paris kfree(opts->mnt_opts); 953e0007529SEric Paris goto out_err; 954c9180a57SEric Paris } 955c9180a57SEric Paris 956e0007529SEric Paris if (fscontext) { 957e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = fscontext; 958e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT; 959e0007529SEric Paris } 960e0007529SEric Paris if (context) { 961e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = context; 962e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT; 963e0007529SEric Paris } 964e0007529SEric Paris if (rootcontext) { 965e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = rootcontext; 966e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT; 967e0007529SEric Paris } 968e0007529SEric Paris if (defcontext) { 969e0007529SEric Paris opts->mnt_opts[num_mnt_opts] = defcontext; 970e0007529SEric Paris opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT; 971e0007529SEric Paris } 972e0007529SEric Paris 973e0007529SEric Paris opts->num_mnt_opts = num_mnt_opts; 974e0007529SEric Paris return 0; 975e0007529SEric Paris 976c9180a57SEric Paris out_err: 977c9180a57SEric Paris kfree(context); 978c9180a57SEric Paris kfree(defcontext); 979c9180a57SEric Paris kfree(fscontext); 980c9180a57SEric Paris kfree(rootcontext); 981c9180a57SEric Paris return rc; 9821da177e4SLinus Torvalds } 983e0007529SEric Paris /* 984e0007529SEric Paris * string mount options parsing and call set the sbsec 985e0007529SEric Paris */ 986e0007529SEric Paris static int superblock_doinit(struct super_block *sb, void *data) 987e0007529SEric Paris { 988e0007529SEric Paris int rc = 0; 989e0007529SEric Paris char *options = data; 990e0007529SEric Paris struct security_mnt_opts opts; 991e0007529SEric Paris 992e0007529SEric Paris security_init_mnt_opts(&opts); 993e0007529SEric Paris 994e0007529SEric Paris if (!data) 995e0007529SEric Paris goto out; 996e0007529SEric Paris 997e0007529SEric Paris BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA); 998e0007529SEric Paris 999e0007529SEric Paris rc = selinux_parse_opts_str(options, &opts); 1000e0007529SEric Paris if (rc) 1001e0007529SEric Paris goto out_err; 1002e0007529SEric Paris 1003e0007529SEric Paris out: 1004e0007529SEric Paris rc = selinux_set_mnt_opts(sb, &opts); 1005e0007529SEric Paris 1006e0007529SEric Paris out_err: 1007e0007529SEric Paris security_free_mnt_opts(&opts); 1008e0007529SEric Paris return rc; 1009e0007529SEric Paris } 10101da177e4SLinus Torvalds 10113583a711SAdrian Bunk static void selinux_write_opts(struct seq_file *m, 10123583a711SAdrian Bunk struct security_mnt_opts *opts) 10132069f457SEric Paris { 10142069f457SEric Paris int i; 10152069f457SEric Paris char *prefix; 10162069f457SEric Paris 10172069f457SEric Paris for (i = 0; i < opts->num_mnt_opts; i++) { 101811689d47SDavid P. Quigley char *has_comma; 101911689d47SDavid P. Quigley 102011689d47SDavid P. Quigley if (opts->mnt_opts[i]) 102111689d47SDavid P. Quigley has_comma = strchr(opts->mnt_opts[i], ','); 102211689d47SDavid P. Quigley else 102311689d47SDavid P. Quigley has_comma = NULL; 10242069f457SEric Paris 10252069f457SEric Paris switch (opts->mnt_opts_flags[i]) { 10262069f457SEric Paris case CONTEXT_MNT: 10272069f457SEric Paris prefix = CONTEXT_STR; 10282069f457SEric Paris break; 10292069f457SEric Paris case FSCONTEXT_MNT: 10302069f457SEric Paris prefix = FSCONTEXT_STR; 10312069f457SEric Paris break; 10322069f457SEric Paris case ROOTCONTEXT_MNT: 10332069f457SEric Paris prefix = ROOTCONTEXT_STR; 10342069f457SEric Paris break; 10352069f457SEric Paris case DEFCONTEXT_MNT: 10362069f457SEric Paris prefix = DEFCONTEXT_STR; 10372069f457SEric Paris break; 103811689d47SDavid P. Quigley case SE_SBLABELSUPP: 103911689d47SDavid P. Quigley seq_putc(m, ','); 104011689d47SDavid P. Quigley seq_puts(m, LABELSUPP_STR); 104111689d47SDavid P. Quigley continue; 10422069f457SEric Paris default: 10432069f457SEric Paris BUG(); 10442069f457SEric Paris }; 10452069f457SEric Paris /* we need a comma before each option */ 10462069f457SEric Paris seq_putc(m, ','); 10472069f457SEric Paris seq_puts(m, prefix); 10482069f457SEric Paris if (has_comma) 10492069f457SEric Paris seq_putc(m, '\"'); 10502069f457SEric Paris seq_puts(m, opts->mnt_opts[i]); 10512069f457SEric Paris if (has_comma) 10522069f457SEric Paris seq_putc(m, '\"'); 10532069f457SEric Paris } 10542069f457SEric Paris } 10552069f457SEric Paris 10562069f457SEric Paris static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb) 10572069f457SEric Paris { 10582069f457SEric Paris struct security_mnt_opts opts; 10592069f457SEric Paris int rc; 10602069f457SEric Paris 10612069f457SEric Paris rc = selinux_get_mnt_opts(sb, &opts); 1062383795c2SEric Paris if (rc) { 1063383795c2SEric Paris /* before policy load we may get EINVAL, don't show anything */ 1064383795c2SEric Paris if (rc == -EINVAL) 1065383795c2SEric Paris rc = 0; 10662069f457SEric Paris return rc; 1067383795c2SEric Paris } 10682069f457SEric Paris 10692069f457SEric Paris selinux_write_opts(m, &opts); 10702069f457SEric Paris 10712069f457SEric Paris security_free_mnt_opts(&opts); 10722069f457SEric Paris 10732069f457SEric Paris return rc; 10742069f457SEric Paris } 10752069f457SEric Paris 10761da177e4SLinus Torvalds static inline u16 inode_mode_to_security_class(umode_t mode) 10771da177e4SLinus Torvalds { 10781da177e4SLinus Torvalds switch (mode & S_IFMT) { 10791da177e4SLinus Torvalds case S_IFSOCK: 10801da177e4SLinus Torvalds return SECCLASS_SOCK_FILE; 10811da177e4SLinus Torvalds case S_IFLNK: 10821da177e4SLinus Torvalds return SECCLASS_LNK_FILE; 10831da177e4SLinus Torvalds case S_IFREG: 10841da177e4SLinus Torvalds return SECCLASS_FILE; 10851da177e4SLinus Torvalds case S_IFBLK: 10861da177e4SLinus Torvalds return SECCLASS_BLK_FILE; 10871da177e4SLinus Torvalds case S_IFDIR: 10881da177e4SLinus Torvalds return SECCLASS_DIR; 10891da177e4SLinus Torvalds case S_IFCHR: 10901da177e4SLinus Torvalds return SECCLASS_CHR_FILE; 10911da177e4SLinus Torvalds case S_IFIFO: 10921da177e4SLinus Torvalds return SECCLASS_FIFO_FILE; 10931da177e4SLinus Torvalds 10941da177e4SLinus Torvalds } 10951da177e4SLinus Torvalds 10961da177e4SLinus Torvalds return SECCLASS_FILE; 10971da177e4SLinus Torvalds } 10981da177e4SLinus Torvalds 109913402580SJames Morris static inline int default_protocol_stream(int protocol) 110013402580SJames Morris { 110113402580SJames Morris return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP); 110213402580SJames Morris } 110313402580SJames Morris 110413402580SJames Morris static inline int default_protocol_dgram(int protocol) 110513402580SJames Morris { 110613402580SJames Morris return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP); 110713402580SJames Morris } 110813402580SJames Morris 11091da177e4SLinus Torvalds static inline u16 socket_type_to_security_class(int family, int type, int protocol) 11101da177e4SLinus Torvalds { 11111da177e4SLinus Torvalds switch (family) { 11121da177e4SLinus Torvalds case PF_UNIX: 11131da177e4SLinus Torvalds switch (type) { 11141da177e4SLinus Torvalds case SOCK_STREAM: 11151da177e4SLinus Torvalds case SOCK_SEQPACKET: 11161da177e4SLinus Torvalds return SECCLASS_UNIX_STREAM_SOCKET; 11171da177e4SLinus Torvalds case SOCK_DGRAM: 11181da177e4SLinus Torvalds return SECCLASS_UNIX_DGRAM_SOCKET; 11191da177e4SLinus Torvalds } 11201da177e4SLinus Torvalds break; 11211da177e4SLinus Torvalds case PF_INET: 11221da177e4SLinus Torvalds case PF_INET6: 11231da177e4SLinus Torvalds switch (type) { 11241da177e4SLinus Torvalds case SOCK_STREAM: 112513402580SJames Morris if (default_protocol_stream(protocol)) 11261da177e4SLinus Torvalds return SECCLASS_TCP_SOCKET; 112713402580SJames Morris else 112813402580SJames Morris return SECCLASS_RAWIP_SOCKET; 11291da177e4SLinus Torvalds case SOCK_DGRAM: 113013402580SJames Morris if (default_protocol_dgram(protocol)) 11311da177e4SLinus Torvalds return SECCLASS_UDP_SOCKET; 113213402580SJames Morris else 113313402580SJames Morris return SECCLASS_RAWIP_SOCKET; 11342ee92d46SJames Morris case SOCK_DCCP: 11352ee92d46SJames Morris return SECCLASS_DCCP_SOCKET; 113613402580SJames Morris default: 11371da177e4SLinus Torvalds return SECCLASS_RAWIP_SOCKET; 11381da177e4SLinus Torvalds } 11391da177e4SLinus Torvalds break; 11401da177e4SLinus Torvalds case PF_NETLINK: 11411da177e4SLinus Torvalds switch (protocol) { 11421da177e4SLinus Torvalds case NETLINK_ROUTE: 11431da177e4SLinus Torvalds return SECCLASS_NETLINK_ROUTE_SOCKET; 11441da177e4SLinus Torvalds case NETLINK_FIREWALL: 11451da177e4SLinus Torvalds return SECCLASS_NETLINK_FIREWALL_SOCKET; 1146216efaaaSJames Morris case NETLINK_INET_DIAG: 11471da177e4SLinus Torvalds return SECCLASS_NETLINK_TCPDIAG_SOCKET; 11481da177e4SLinus Torvalds case NETLINK_NFLOG: 11491da177e4SLinus Torvalds return SECCLASS_NETLINK_NFLOG_SOCKET; 11501da177e4SLinus Torvalds case NETLINK_XFRM: 11511da177e4SLinus Torvalds return SECCLASS_NETLINK_XFRM_SOCKET; 11521da177e4SLinus Torvalds case NETLINK_SELINUX: 11531da177e4SLinus Torvalds return SECCLASS_NETLINK_SELINUX_SOCKET; 11541da177e4SLinus Torvalds case NETLINK_AUDIT: 11551da177e4SLinus Torvalds return SECCLASS_NETLINK_AUDIT_SOCKET; 11561da177e4SLinus Torvalds case NETLINK_IP6_FW: 11571da177e4SLinus Torvalds return SECCLASS_NETLINK_IP6FW_SOCKET; 11581da177e4SLinus Torvalds case NETLINK_DNRTMSG: 11591da177e4SLinus Torvalds return SECCLASS_NETLINK_DNRT_SOCKET; 11600c9b7942SJames Morris case NETLINK_KOBJECT_UEVENT: 11610c9b7942SJames Morris return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET; 11621da177e4SLinus Torvalds default: 11631da177e4SLinus Torvalds return SECCLASS_NETLINK_SOCKET; 11641da177e4SLinus Torvalds } 11651da177e4SLinus Torvalds case PF_PACKET: 11661da177e4SLinus Torvalds return SECCLASS_PACKET_SOCKET; 11671da177e4SLinus Torvalds case PF_KEY: 11681da177e4SLinus Torvalds return SECCLASS_KEY_SOCKET; 11693e3ff15eSChristopher J. PeBenito case PF_APPLETALK: 11703e3ff15eSChristopher J. PeBenito return SECCLASS_APPLETALK_SOCKET; 11711da177e4SLinus Torvalds } 11721da177e4SLinus Torvalds 11731da177e4SLinus Torvalds return SECCLASS_SOCKET; 11741da177e4SLinus Torvalds } 11751da177e4SLinus Torvalds 11761da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 11771da177e4SLinus Torvalds static int selinux_proc_get_sid(struct proc_dir_entry *de, 11781da177e4SLinus Torvalds u16 tclass, 11791da177e4SLinus Torvalds u32 *sid) 11801da177e4SLinus Torvalds { 11811da177e4SLinus Torvalds int buflen, rc; 11821da177e4SLinus Torvalds char *buffer, *path, *end; 11831da177e4SLinus Torvalds 11841da177e4SLinus Torvalds buffer = (char *)__get_free_page(GFP_KERNEL); 11851da177e4SLinus Torvalds if (!buffer) 11861da177e4SLinus Torvalds return -ENOMEM; 11871da177e4SLinus Torvalds 11881da177e4SLinus Torvalds buflen = PAGE_SIZE; 11891da177e4SLinus Torvalds end = buffer+buflen; 11901da177e4SLinus Torvalds *--end = '\0'; 11911da177e4SLinus Torvalds buflen--; 11921da177e4SLinus Torvalds path = end-1; 11931da177e4SLinus Torvalds *path = '/'; 11941da177e4SLinus Torvalds while (de && de != de->parent) { 11951da177e4SLinus Torvalds buflen -= de->namelen + 1; 11961da177e4SLinus Torvalds if (buflen < 0) 11971da177e4SLinus Torvalds break; 11981da177e4SLinus Torvalds end -= de->namelen; 11991da177e4SLinus Torvalds memcpy(end, de->name, de->namelen); 12001da177e4SLinus Torvalds *--end = '/'; 12011da177e4SLinus Torvalds path = end; 12021da177e4SLinus Torvalds de = de->parent; 12031da177e4SLinus Torvalds } 12041da177e4SLinus Torvalds rc = security_genfs_sid("proc", path, tclass, sid); 12051da177e4SLinus Torvalds free_page((unsigned long)buffer); 12061da177e4SLinus Torvalds return rc; 12071da177e4SLinus Torvalds } 12081da177e4SLinus Torvalds #else 12091da177e4SLinus Torvalds static int selinux_proc_get_sid(struct proc_dir_entry *de, 12101da177e4SLinus Torvalds u16 tclass, 12111da177e4SLinus Torvalds u32 *sid) 12121da177e4SLinus Torvalds { 12131da177e4SLinus Torvalds return -EINVAL; 12141da177e4SLinus Torvalds } 12151da177e4SLinus Torvalds #endif 12161da177e4SLinus Torvalds 12171da177e4SLinus Torvalds /* The inode's security attributes must be initialized before first use. */ 12181da177e4SLinus Torvalds static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry) 12191da177e4SLinus Torvalds { 12201da177e4SLinus Torvalds struct superblock_security_struct *sbsec = NULL; 12211da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 12221da177e4SLinus Torvalds u32 sid; 12231da177e4SLinus Torvalds struct dentry *dentry; 12241da177e4SLinus Torvalds #define INITCONTEXTLEN 255 12251da177e4SLinus Torvalds char *context = NULL; 12261da177e4SLinus Torvalds unsigned len = 0; 12271da177e4SLinus Torvalds int rc = 0; 12281da177e4SLinus Torvalds 12291da177e4SLinus Torvalds if (isec->initialized) 12301da177e4SLinus Torvalds goto out; 12311da177e4SLinus Torvalds 123223970741SEric Paris mutex_lock(&isec->lock); 12331da177e4SLinus Torvalds if (isec->initialized) 123423970741SEric Paris goto out_unlock; 12351da177e4SLinus Torvalds 12361da177e4SLinus Torvalds sbsec = inode->i_sb->s_security; 12370d90a7ecSDavid P. Quigley if (!(sbsec->flags & SE_SBINITIALIZED)) { 12381da177e4SLinus Torvalds /* Defer initialization until selinux_complete_init, 12391da177e4SLinus Torvalds after the initial policy is loaded and the security 12401da177e4SLinus Torvalds server is ready to handle calls. */ 12411da177e4SLinus Torvalds spin_lock(&sbsec->isec_lock); 12421da177e4SLinus Torvalds if (list_empty(&isec->list)) 12431da177e4SLinus Torvalds list_add(&isec->list, &sbsec->isec_head); 12441da177e4SLinus Torvalds spin_unlock(&sbsec->isec_lock); 124523970741SEric Paris goto out_unlock; 12461da177e4SLinus Torvalds } 12471da177e4SLinus Torvalds 12481da177e4SLinus Torvalds switch (sbsec->behavior) { 12491da177e4SLinus Torvalds case SECURITY_FS_USE_XATTR: 12501da177e4SLinus Torvalds if (!inode->i_op->getxattr) { 12511da177e4SLinus Torvalds isec->sid = sbsec->def_sid; 12521da177e4SLinus Torvalds break; 12531da177e4SLinus Torvalds } 12541da177e4SLinus Torvalds 12551da177e4SLinus Torvalds /* Need a dentry, since the xattr API requires one. 12561da177e4SLinus Torvalds Life would be simpler if we could just pass the inode. */ 12571da177e4SLinus Torvalds if (opt_dentry) { 12581da177e4SLinus Torvalds /* Called from d_instantiate or d_splice_alias. */ 12591da177e4SLinus Torvalds dentry = dget(opt_dentry); 12601da177e4SLinus Torvalds } else { 12611da177e4SLinus Torvalds /* Called from selinux_complete_init, try to find a dentry. */ 12621da177e4SLinus Torvalds dentry = d_find_alias(inode); 12631da177e4SLinus Torvalds } 12641da177e4SLinus Torvalds if (!dentry) { 1265df7f54c0SEric Paris /* 1266df7f54c0SEric Paris * this is can be hit on boot when a file is accessed 1267df7f54c0SEric Paris * before the policy is loaded. When we load policy we 1268df7f54c0SEric Paris * may find inodes that have no dentry on the 1269df7f54c0SEric Paris * sbsec->isec_head list. No reason to complain as these 1270df7f54c0SEric Paris * will get fixed up the next time we go through 1271df7f54c0SEric Paris * inode_doinit with a dentry, before these inodes could 1272df7f54c0SEric Paris * be used again by userspace. 1273df7f54c0SEric Paris */ 127423970741SEric Paris goto out_unlock; 12751da177e4SLinus Torvalds } 12761da177e4SLinus Torvalds 12771da177e4SLinus Torvalds len = INITCONTEXTLEN; 12784cb912f1SEric Paris context = kmalloc(len+1, GFP_NOFS); 12791da177e4SLinus Torvalds if (!context) { 12801da177e4SLinus Torvalds rc = -ENOMEM; 12811da177e4SLinus Torvalds dput(dentry); 128223970741SEric Paris goto out_unlock; 12831da177e4SLinus Torvalds } 12844cb912f1SEric Paris context[len] = '\0'; 12851da177e4SLinus Torvalds rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX, 12861da177e4SLinus Torvalds context, len); 12871da177e4SLinus Torvalds if (rc == -ERANGE) { 12881da177e4SLinus Torvalds /* Need a larger buffer. Query for the right size. */ 12891da177e4SLinus Torvalds rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX, 12901da177e4SLinus Torvalds NULL, 0); 12911da177e4SLinus Torvalds if (rc < 0) { 12921da177e4SLinus Torvalds dput(dentry); 129323970741SEric Paris goto out_unlock; 12941da177e4SLinus Torvalds } 12951da177e4SLinus Torvalds kfree(context); 12961da177e4SLinus Torvalds len = rc; 12974cb912f1SEric Paris context = kmalloc(len+1, GFP_NOFS); 12981da177e4SLinus Torvalds if (!context) { 12991da177e4SLinus Torvalds rc = -ENOMEM; 13001da177e4SLinus Torvalds dput(dentry); 130123970741SEric Paris goto out_unlock; 13021da177e4SLinus Torvalds } 13034cb912f1SEric Paris context[len] = '\0'; 13041da177e4SLinus Torvalds rc = inode->i_op->getxattr(dentry, 13051da177e4SLinus Torvalds XATTR_NAME_SELINUX, 13061da177e4SLinus Torvalds context, len); 13071da177e4SLinus Torvalds } 13081da177e4SLinus Torvalds dput(dentry); 13091da177e4SLinus Torvalds if (rc < 0) { 13101da177e4SLinus Torvalds if (rc != -ENODATA) { 1311744ba35eSEric Paris printk(KERN_WARNING "SELinux: %s: getxattr returned " 1312dd6f953aSHarvey Harrison "%d for dev=%s ino=%ld\n", __func__, 13131da177e4SLinus Torvalds -rc, inode->i_sb->s_id, inode->i_ino); 13141da177e4SLinus Torvalds kfree(context); 131523970741SEric Paris goto out_unlock; 13161da177e4SLinus Torvalds } 13171da177e4SLinus Torvalds /* Map ENODATA to the default file SID */ 13181da177e4SLinus Torvalds sid = sbsec->def_sid; 13191da177e4SLinus Torvalds rc = 0; 13201da177e4SLinus Torvalds } else { 1321f5c1d5b2SJames Morris rc = security_context_to_sid_default(context, rc, &sid, 1322869ab514SStephen Smalley sbsec->def_sid, 1323869ab514SStephen Smalley GFP_NOFS); 13241da177e4SLinus Torvalds if (rc) { 13254ba0a8adSEric Paris char *dev = inode->i_sb->s_id; 13264ba0a8adSEric Paris unsigned long ino = inode->i_ino; 13274ba0a8adSEric Paris 13284ba0a8adSEric Paris if (rc == -EINVAL) { 13294ba0a8adSEric Paris if (printk_ratelimit()) 13304ba0a8adSEric Paris printk(KERN_NOTICE "SELinux: inode=%lu on dev=%s was found to have an invalid " 13314ba0a8adSEric Paris "context=%s. This indicates you may need to relabel the inode or the " 13324ba0a8adSEric Paris "filesystem in question.\n", ino, dev, context); 13334ba0a8adSEric Paris } else { 1334744ba35eSEric Paris printk(KERN_WARNING "SELinux: %s: context_to_sid(%s) " 13351da177e4SLinus Torvalds "returned %d for dev=%s ino=%ld\n", 13364ba0a8adSEric Paris __func__, context, -rc, dev, ino); 13374ba0a8adSEric Paris } 13381da177e4SLinus Torvalds kfree(context); 13391da177e4SLinus Torvalds /* Leave with the unlabeled SID */ 13401da177e4SLinus Torvalds rc = 0; 13411da177e4SLinus Torvalds break; 13421da177e4SLinus Torvalds } 13431da177e4SLinus Torvalds } 13441da177e4SLinus Torvalds kfree(context); 13451da177e4SLinus Torvalds isec->sid = sid; 13461da177e4SLinus Torvalds break; 13471da177e4SLinus Torvalds case SECURITY_FS_USE_TASK: 13481da177e4SLinus Torvalds isec->sid = isec->task_sid; 13491da177e4SLinus Torvalds break; 13501da177e4SLinus Torvalds case SECURITY_FS_USE_TRANS: 13511da177e4SLinus Torvalds /* Default to the fs SID. */ 13521da177e4SLinus Torvalds isec->sid = sbsec->sid; 13531da177e4SLinus Torvalds 13541da177e4SLinus Torvalds /* Try to obtain a transition SID. */ 13551da177e4SLinus Torvalds isec->sclass = inode_mode_to_security_class(inode->i_mode); 13561da177e4SLinus Torvalds rc = security_transition_sid(isec->task_sid, 13571da177e4SLinus Torvalds sbsec->sid, 13581da177e4SLinus Torvalds isec->sclass, 13591da177e4SLinus Torvalds &sid); 13601da177e4SLinus Torvalds if (rc) 136123970741SEric Paris goto out_unlock; 13621da177e4SLinus Torvalds isec->sid = sid; 13631da177e4SLinus Torvalds break; 1364c312feb2SEric Paris case SECURITY_FS_USE_MNTPOINT: 1365c312feb2SEric Paris isec->sid = sbsec->mntpoint_sid; 1366c312feb2SEric Paris break; 13671da177e4SLinus Torvalds default: 1368c312feb2SEric Paris /* Default to the fs superblock SID. */ 13691da177e4SLinus Torvalds isec->sid = sbsec->sid; 13701da177e4SLinus Torvalds 13710d90a7ecSDavid P. Quigley if ((sbsec->flags & SE_SBPROC) && !S_ISLNK(inode->i_mode)) { 13721da177e4SLinus Torvalds struct proc_inode *proci = PROC_I(inode); 13731da177e4SLinus Torvalds if (proci->pde) { 13741da177e4SLinus Torvalds isec->sclass = inode_mode_to_security_class(inode->i_mode); 13751da177e4SLinus Torvalds rc = selinux_proc_get_sid(proci->pde, 13761da177e4SLinus Torvalds isec->sclass, 13771da177e4SLinus Torvalds &sid); 13781da177e4SLinus Torvalds if (rc) 137923970741SEric Paris goto out_unlock; 13801da177e4SLinus Torvalds isec->sid = sid; 13811da177e4SLinus Torvalds } 13821da177e4SLinus Torvalds } 13831da177e4SLinus Torvalds break; 13841da177e4SLinus Torvalds } 13851da177e4SLinus Torvalds 13861da177e4SLinus Torvalds isec->initialized = 1; 13871da177e4SLinus Torvalds 138823970741SEric Paris out_unlock: 138923970741SEric Paris mutex_unlock(&isec->lock); 13901da177e4SLinus Torvalds out: 13911da177e4SLinus Torvalds if (isec->sclass == SECCLASS_FILE) 13921da177e4SLinus Torvalds isec->sclass = inode_mode_to_security_class(inode->i_mode); 13931da177e4SLinus Torvalds return rc; 13941da177e4SLinus Torvalds } 13951da177e4SLinus Torvalds 13961da177e4SLinus Torvalds /* Convert a Linux signal to an access vector. */ 13971da177e4SLinus Torvalds static inline u32 signal_to_av(int sig) 13981da177e4SLinus Torvalds { 13991da177e4SLinus Torvalds u32 perm = 0; 14001da177e4SLinus Torvalds 14011da177e4SLinus Torvalds switch (sig) { 14021da177e4SLinus Torvalds case SIGCHLD: 14031da177e4SLinus Torvalds /* Commonly granted from child to parent. */ 14041da177e4SLinus Torvalds perm = PROCESS__SIGCHLD; 14051da177e4SLinus Torvalds break; 14061da177e4SLinus Torvalds case SIGKILL: 14071da177e4SLinus Torvalds /* Cannot be caught or ignored */ 14081da177e4SLinus Torvalds perm = PROCESS__SIGKILL; 14091da177e4SLinus Torvalds break; 14101da177e4SLinus Torvalds case SIGSTOP: 14111da177e4SLinus Torvalds /* Cannot be caught or ignored */ 14121da177e4SLinus Torvalds perm = PROCESS__SIGSTOP; 14131da177e4SLinus Torvalds break; 14141da177e4SLinus Torvalds default: 14151da177e4SLinus Torvalds /* All other signals. */ 14161da177e4SLinus Torvalds perm = PROCESS__SIGNAL; 14171da177e4SLinus Torvalds break; 14181da177e4SLinus Torvalds } 14191da177e4SLinus Torvalds 14201da177e4SLinus Torvalds return perm; 14211da177e4SLinus Torvalds } 14221da177e4SLinus Torvalds 1423275bb41eSDavid Howells /* 1424d84f4f99SDavid Howells * Check permission between a pair of credentials 1425d84f4f99SDavid Howells * fork check, ptrace check, etc. 1426d84f4f99SDavid Howells */ 1427d84f4f99SDavid Howells static int cred_has_perm(const struct cred *actor, 1428d84f4f99SDavid Howells const struct cred *target, 1429d84f4f99SDavid Howells u32 perms) 1430d84f4f99SDavid Howells { 1431d84f4f99SDavid Howells u32 asid = cred_sid(actor), tsid = cred_sid(target); 1432d84f4f99SDavid Howells 1433d84f4f99SDavid Howells return avc_has_perm(asid, tsid, SECCLASS_PROCESS, perms, NULL); 1434d84f4f99SDavid Howells } 1435d84f4f99SDavid Howells 1436d84f4f99SDavid Howells /* 143788e67f3bSDavid Howells * Check permission between a pair of tasks, e.g. signal checks, 1438275bb41eSDavid Howells * fork check, ptrace check, etc. 1439275bb41eSDavid Howells * tsk1 is the actor and tsk2 is the target 14403b11a1deSDavid Howells * - this uses the default subjective creds of tsk1 1441275bb41eSDavid Howells */ 1442275bb41eSDavid Howells static int task_has_perm(const struct task_struct *tsk1, 1443275bb41eSDavid Howells const struct task_struct *tsk2, 14441da177e4SLinus Torvalds u32 perms) 14451da177e4SLinus Torvalds { 1446275bb41eSDavid Howells const struct task_security_struct *__tsec1, *__tsec2; 1447275bb41eSDavid Howells u32 sid1, sid2; 14481da177e4SLinus Torvalds 1449275bb41eSDavid Howells rcu_read_lock(); 1450275bb41eSDavid Howells __tsec1 = __task_cred(tsk1)->security; sid1 = __tsec1->sid; 1451275bb41eSDavid Howells __tsec2 = __task_cred(tsk2)->security; sid2 = __tsec2->sid; 1452275bb41eSDavid Howells rcu_read_unlock(); 1453275bb41eSDavid Howells return avc_has_perm(sid1, sid2, SECCLASS_PROCESS, perms, NULL); 14541da177e4SLinus Torvalds } 14551da177e4SLinus Torvalds 14563b11a1deSDavid Howells /* 14573b11a1deSDavid Howells * Check permission between current and another task, e.g. signal checks, 14583b11a1deSDavid Howells * fork check, ptrace check, etc. 14593b11a1deSDavid Howells * current is the actor and tsk2 is the target 14603b11a1deSDavid Howells * - this uses current's subjective creds 14613b11a1deSDavid Howells */ 14623b11a1deSDavid Howells static int current_has_perm(const struct task_struct *tsk, 14633b11a1deSDavid Howells u32 perms) 14643b11a1deSDavid Howells { 14653b11a1deSDavid Howells u32 sid, tsid; 14663b11a1deSDavid Howells 14673b11a1deSDavid Howells sid = current_sid(); 14683b11a1deSDavid Howells tsid = task_sid(tsk); 14693b11a1deSDavid Howells return avc_has_perm(sid, tsid, SECCLASS_PROCESS, perms, NULL); 14703b11a1deSDavid Howells } 14713b11a1deSDavid Howells 1472b68e418cSStephen Smalley #if CAP_LAST_CAP > 63 1473b68e418cSStephen Smalley #error Fix SELinux to handle capabilities > 63. 1474b68e418cSStephen Smalley #endif 1475b68e418cSStephen Smalley 14761da177e4SLinus Torvalds /* Check whether a task is allowed to use a capability. */ 14771da177e4SLinus Torvalds static int task_has_capability(struct task_struct *tsk, 14783699c53cSDavid Howells const struct cred *cred, 147906112163SEric Paris int cap, int audit) 14801da177e4SLinus Torvalds { 1481*2bf49690SThomas Liu struct common_audit_data ad; 148206112163SEric Paris struct av_decision avd; 1483b68e418cSStephen Smalley u16 sclass; 14843699c53cSDavid Howells u32 sid = cred_sid(cred); 1485b68e418cSStephen Smalley u32 av = CAP_TO_MASK(cap); 148606112163SEric Paris int rc; 14871da177e4SLinus Torvalds 1488*2bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, CAP); 14891da177e4SLinus Torvalds ad.tsk = tsk; 14901da177e4SLinus Torvalds ad.u.cap = cap; 14911da177e4SLinus Torvalds 1492b68e418cSStephen Smalley switch (CAP_TO_INDEX(cap)) { 1493b68e418cSStephen Smalley case 0: 1494b68e418cSStephen Smalley sclass = SECCLASS_CAPABILITY; 1495b68e418cSStephen Smalley break; 1496b68e418cSStephen Smalley case 1: 1497b68e418cSStephen Smalley sclass = SECCLASS_CAPABILITY2; 1498b68e418cSStephen Smalley break; 1499b68e418cSStephen Smalley default: 1500b68e418cSStephen Smalley printk(KERN_ERR 1501b68e418cSStephen Smalley "SELinux: out of range capability %d\n", cap); 1502b68e418cSStephen Smalley BUG(); 1503b68e418cSStephen Smalley } 150406112163SEric Paris 1505275bb41eSDavid Howells rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd); 150606112163SEric Paris if (audit == SECURITY_CAP_AUDIT) 1507275bb41eSDavid Howells avc_audit(sid, sid, sclass, av, &avd, rc, &ad); 150806112163SEric Paris return rc; 15091da177e4SLinus Torvalds } 15101da177e4SLinus Torvalds 15111da177e4SLinus Torvalds /* Check whether a task is allowed to use a system operation. */ 15121da177e4SLinus Torvalds static int task_has_system(struct task_struct *tsk, 15131da177e4SLinus Torvalds u32 perms) 15141da177e4SLinus Torvalds { 1515275bb41eSDavid Howells u32 sid = task_sid(tsk); 15161da177e4SLinus Torvalds 1517275bb41eSDavid Howells return avc_has_perm(sid, SECINITSID_KERNEL, 15181da177e4SLinus Torvalds SECCLASS_SYSTEM, perms, NULL); 15191da177e4SLinus Torvalds } 15201da177e4SLinus Torvalds 15211da177e4SLinus Torvalds /* Check whether a task has a particular permission to an inode. 15221da177e4SLinus Torvalds The 'adp' parameter is optional and allows other audit 15231da177e4SLinus Torvalds data to be passed (e.g. the dentry). */ 152488e67f3bSDavid Howells static int inode_has_perm(const struct cred *cred, 15251da177e4SLinus Torvalds struct inode *inode, 15261da177e4SLinus Torvalds u32 perms, 1527*2bf49690SThomas Liu struct common_audit_data *adp) 15281da177e4SLinus Torvalds { 15291da177e4SLinus Torvalds struct inode_security_struct *isec; 1530*2bf49690SThomas Liu struct common_audit_data ad; 1531275bb41eSDavid Howells u32 sid; 15321da177e4SLinus Torvalds 1533bbaca6c2SStephen Smalley if (unlikely(IS_PRIVATE(inode))) 1534bbaca6c2SStephen Smalley return 0; 1535bbaca6c2SStephen Smalley 153688e67f3bSDavid Howells sid = cred_sid(cred); 15371da177e4SLinus Torvalds isec = inode->i_security; 15381da177e4SLinus Torvalds 15391da177e4SLinus Torvalds if (!adp) { 15401da177e4SLinus Torvalds adp = &ad; 1541*2bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, FS); 15421da177e4SLinus Torvalds ad.u.fs.inode = inode; 15431da177e4SLinus Torvalds } 15441da177e4SLinus Torvalds 1545275bb41eSDavid Howells return avc_has_perm(sid, isec->sid, isec->sclass, perms, adp); 15461da177e4SLinus Torvalds } 15471da177e4SLinus Torvalds 15481da177e4SLinus Torvalds /* Same as inode_has_perm, but pass explicit audit data containing 15491da177e4SLinus Torvalds the dentry to help the auditing code to more easily generate the 15501da177e4SLinus Torvalds pathname if needed. */ 155188e67f3bSDavid Howells static inline int dentry_has_perm(const struct cred *cred, 15521da177e4SLinus Torvalds struct vfsmount *mnt, 15531da177e4SLinus Torvalds struct dentry *dentry, 15541da177e4SLinus Torvalds u32 av) 15551da177e4SLinus Torvalds { 15561da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 1557*2bf49690SThomas Liu struct common_audit_data ad; 155888e67f3bSDavid Howells 1559*2bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, FS); 156044707fdfSJan Blunck ad.u.fs.path.mnt = mnt; 156144707fdfSJan Blunck ad.u.fs.path.dentry = dentry; 156288e67f3bSDavid Howells return inode_has_perm(cred, inode, av, &ad); 15631da177e4SLinus Torvalds } 15641da177e4SLinus Torvalds 15651da177e4SLinus Torvalds /* Check whether a task can use an open file descriptor to 15661da177e4SLinus Torvalds access an inode in a given way. Check access to the 15671da177e4SLinus Torvalds descriptor itself, and then use dentry_has_perm to 15681da177e4SLinus Torvalds check a particular permission to the file. 15691da177e4SLinus Torvalds Access to the descriptor is implicitly granted if it 15701da177e4SLinus Torvalds has the same SID as the process. If av is zero, then 15711da177e4SLinus Torvalds access to the file is not checked, e.g. for cases 15721da177e4SLinus Torvalds where only the descriptor is affected like seek. */ 157388e67f3bSDavid Howells static int file_has_perm(const struct cred *cred, 15741da177e4SLinus Torvalds struct file *file, 15751da177e4SLinus Torvalds u32 av) 15761da177e4SLinus Torvalds { 15771da177e4SLinus Torvalds struct file_security_struct *fsec = file->f_security; 157844707fdfSJan Blunck struct inode *inode = file->f_path.dentry->d_inode; 1579*2bf49690SThomas Liu struct common_audit_data ad; 158088e67f3bSDavid Howells u32 sid = cred_sid(cred); 15811da177e4SLinus Torvalds int rc; 15821da177e4SLinus Torvalds 1583*2bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, FS); 158444707fdfSJan Blunck ad.u.fs.path = file->f_path; 15851da177e4SLinus Torvalds 1586275bb41eSDavid Howells if (sid != fsec->sid) { 1587275bb41eSDavid Howells rc = avc_has_perm(sid, fsec->sid, 15881da177e4SLinus Torvalds SECCLASS_FD, 15891da177e4SLinus Torvalds FD__USE, 15901da177e4SLinus Torvalds &ad); 15911da177e4SLinus Torvalds if (rc) 159288e67f3bSDavid Howells goto out; 15931da177e4SLinus Torvalds } 15941da177e4SLinus Torvalds 15951da177e4SLinus Torvalds /* av is zero if only checking access to the descriptor. */ 159688e67f3bSDavid Howells rc = 0; 15971da177e4SLinus Torvalds if (av) 159888e67f3bSDavid Howells rc = inode_has_perm(cred, inode, av, &ad); 15991da177e4SLinus Torvalds 160088e67f3bSDavid Howells out: 160188e67f3bSDavid Howells return rc; 16021da177e4SLinus Torvalds } 16031da177e4SLinus Torvalds 16041da177e4SLinus Torvalds /* Check whether a task can create a file. */ 16051da177e4SLinus Torvalds static int may_create(struct inode *dir, 16061da177e4SLinus Torvalds struct dentry *dentry, 16071da177e4SLinus Torvalds u16 tclass) 16081da177e4SLinus Torvalds { 1609275bb41eSDavid Howells const struct cred *cred = current_cred(); 1610275bb41eSDavid Howells const struct task_security_struct *tsec = cred->security; 16111da177e4SLinus Torvalds struct inode_security_struct *dsec; 16121da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 1613275bb41eSDavid Howells u32 sid, newsid; 1614*2bf49690SThomas Liu struct common_audit_data ad; 16151da177e4SLinus Torvalds int rc; 16161da177e4SLinus Torvalds 16171da177e4SLinus Torvalds dsec = dir->i_security; 16181da177e4SLinus Torvalds sbsec = dir->i_sb->s_security; 16191da177e4SLinus Torvalds 1620275bb41eSDavid Howells sid = tsec->sid; 1621275bb41eSDavid Howells newsid = tsec->create_sid; 1622275bb41eSDavid Howells 1623*2bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, FS); 162444707fdfSJan Blunck ad.u.fs.path.dentry = dentry; 16251da177e4SLinus Torvalds 1626275bb41eSDavid Howells rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, 16271da177e4SLinus Torvalds DIR__ADD_NAME | DIR__SEARCH, 16281da177e4SLinus Torvalds &ad); 16291da177e4SLinus Torvalds if (rc) 16301da177e4SLinus Torvalds return rc; 16311da177e4SLinus Torvalds 1632cd89596fSDavid P. Quigley if (!newsid || !(sbsec->flags & SE_SBLABELSUPP)) { 1633275bb41eSDavid Howells rc = security_transition_sid(sid, dsec->sid, tclass, &newsid); 16341da177e4SLinus Torvalds if (rc) 16351da177e4SLinus Torvalds return rc; 16361da177e4SLinus Torvalds } 16371da177e4SLinus Torvalds 1638275bb41eSDavid Howells rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad); 16391da177e4SLinus Torvalds if (rc) 16401da177e4SLinus Torvalds return rc; 16411da177e4SLinus Torvalds 16421da177e4SLinus Torvalds return avc_has_perm(newsid, sbsec->sid, 16431da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 16441da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, &ad); 16451da177e4SLinus Torvalds } 16461da177e4SLinus Torvalds 16474eb582cfSMichael LeMay /* Check whether a task can create a key. */ 16484eb582cfSMichael LeMay static int may_create_key(u32 ksid, 16494eb582cfSMichael LeMay struct task_struct *ctx) 16504eb582cfSMichael LeMay { 1651275bb41eSDavid Howells u32 sid = task_sid(ctx); 16524eb582cfSMichael LeMay 1653275bb41eSDavid Howells return avc_has_perm(sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL); 16544eb582cfSMichael LeMay } 16554eb582cfSMichael LeMay 16561da177e4SLinus Torvalds #define MAY_LINK 0 16571da177e4SLinus Torvalds #define MAY_UNLINK 1 16581da177e4SLinus Torvalds #define MAY_RMDIR 2 16591da177e4SLinus Torvalds 16601da177e4SLinus Torvalds /* Check whether a task can link, unlink, or rmdir a file/directory. */ 16611da177e4SLinus Torvalds static int may_link(struct inode *dir, 16621da177e4SLinus Torvalds struct dentry *dentry, 16631da177e4SLinus Torvalds int kind) 16641da177e4SLinus Torvalds 16651da177e4SLinus Torvalds { 16661da177e4SLinus Torvalds struct inode_security_struct *dsec, *isec; 1667*2bf49690SThomas Liu struct common_audit_data ad; 1668275bb41eSDavid Howells u32 sid = current_sid(); 16691da177e4SLinus Torvalds u32 av; 16701da177e4SLinus Torvalds int rc; 16711da177e4SLinus Torvalds 16721da177e4SLinus Torvalds dsec = dir->i_security; 16731da177e4SLinus Torvalds isec = dentry->d_inode->i_security; 16741da177e4SLinus Torvalds 1675*2bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, FS); 167644707fdfSJan Blunck ad.u.fs.path.dentry = dentry; 16771da177e4SLinus Torvalds 16781da177e4SLinus Torvalds av = DIR__SEARCH; 16791da177e4SLinus Torvalds av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME); 1680275bb41eSDavid Howells rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad); 16811da177e4SLinus Torvalds if (rc) 16821da177e4SLinus Torvalds return rc; 16831da177e4SLinus Torvalds 16841da177e4SLinus Torvalds switch (kind) { 16851da177e4SLinus Torvalds case MAY_LINK: 16861da177e4SLinus Torvalds av = FILE__LINK; 16871da177e4SLinus Torvalds break; 16881da177e4SLinus Torvalds case MAY_UNLINK: 16891da177e4SLinus Torvalds av = FILE__UNLINK; 16901da177e4SLinus Torvalds break; 16911da177e4SLinus Torvalds case MAY_RMDIR: 16921da177e4SLinus Torvalds av = DIR__RMDIR; 16931da177e4SLinus Torvalds break; 16941da177e4SLinus Torvalds default: 1695744ba35eSEric Paris printk(KERN_WARNING "SELinux: %s: unrecognized kind %d\n", 1696744ba35eSEric Paris __func__, kind); 16971da177e4SLinus Torvalds return 0; 16981da177e4SLinus Torvalds } 16991da177e4SLinus Torvalds 1700275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad); 17011da177e4SLinus Torvalds return rc; 17021da177e4SLinus Torvalds } 17031da177e4SLinus Torvalds 17041da177e4SLinus Torvalds static inline int may_rename(struct inode *old_dir, 17051da177e4SLinus Torvalds struct dentry *old_dentry, 17061da177e4SLinus Torvalds struct inode *new_dir, 17071da177e4SLinus Torvalds struct dentry *new_dentry) 17081da177e4SLinus Torvalds { 17091da177e4SLinus Torvalds struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec; 1710*2bf49690SThomas Liu struct common_audit_data ad; 1711275bb41eSDavid Howells u32 sid = current_sid(); 17121da177e4SLinus Torvalds u32 av; 17131da177e4SLinus Torvalds int old_is_dir, new_is_dir; 17141da177e4SLinus Torvalds int rc; 17151da177e4SLinus Torvalds 17161da177e4SLinus Torvalds old_dsec = old_dir->i_security; 17171da177e4SLinus Torvalds old_isec = old_dentry->d_inode->i_security; 17181da177e4SLinus Torvalds old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 17191da177e4SLinus Torvalds new_dsec = new_dir->i_security; 17201da177e4SLinus Torvalds 1721*2bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, FS); 17221da177e4SLinus Torvalds 172344707fdfSJan Blunck ad.u.fs.path.dentry = old_dentry; 1724275bb41eSDavid Howells rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR, 17251da177e4SLinus Torvalds DIR__REMOVE_NAME | DIR__SEARCH, &ad); 17261da177e4SLinus Torvalds if (rc) 17271da177e4SLinus Torvalds return rc; 1728275bb41eSDavid Howells rc = avc_has_perm(sid, old_isec->sid, 17291da177e4SLinus Torvalds old_isec->sclass, FILE__RENAME, &ad); 17301da177e4SLinus Torvalds if (rc) 17311da177e4SLinus Torvalds return rc; 17321da177e4SLinus Torvalds if (old_is_dir && new_dir != old_dir) { 1733275bb41eSDavid Howells rc = avc_has_perm(sid, old_isec->sid, 17341da177e4SLinus Torvalds old_isec->sclass, DIR__REPARENT, &ad); 17351da177e4SLinus Torvalds if (rc) 17361da177e4SLinus Torvalds return rc; 17371da177e4SLinus Torvalds } 17381da177e4SLinus Torvalds 173944707fdfSJan Blunck ad.u.fs.path.dentry = new_dentry; 17401da177e4SLinus Torvalds av = DIR__ADD_NAME | DIR__SEARCH; 17411da177e4SLinus Torvalds if (new_dentry->d_inode) 17421da177e4SLinus Torvalds av |= DIR__REMOVE_NAME; 1743275bb41eSDavid Howells rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad); 17441da177e4SLinus Torvalds if (rc) 17451da177e4SLinus Torvalds return rc; 17461da177e4SLinus Torvalds if (new_dentry->d_inode) { 17471da177e4SLinus Torvalds new_isec = new_dentry->d_inode->i_security; 17481da177e4SLinus Torvalds new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode); 1749275bb41eSDavid Howells rc = avc_has_perm(sid, new_isec->sid, 17501da177e4SLinus Torvalds new_isec->sclass, 17511da177e4SLinus Torvalds (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad); 17521da177e4SLinus Torvalds if (rc) 17531da177e4SLinus Torvalds return rc; 17541da177e4SLinus Torvalds } 17551da177e4SLinus Torvalds 17561da177e4SLinus Torvalds return 0; 17571da177e4SLinus Torvalds } 17581da177e4SLinus Torvalds 17591da177e4SLinus Torvalds /* Check whether a task can perform a filesystem operation. */ 176088e67f3bSDavid Howells static int superblock_has_perm(const struct cred *cred, 17611da177e4SLinus Torvalds struct super_block *sb, 17621da177e4SLinus Torvalds u32 perms, 1763*2bf49690SThomas Liu struct common_audit_data *ad) 17641da177e4SLinus Torvalds { 17651da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 176688e67f3bSDavid Howells u32 sid = cred_sid(cred); 17671da177e4SLinus Torvalds 17681da177e4SLinus Torvalds sbsec = sb->s_security; 1769275bb41eSDavid Howells return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad); 17701da177e4SLinus Torvalds } 17711da177e4SLinus Torvalds 17721da177e4SLinus Torvalds /* Convert a Linux mode and permission mask to an access vector. */ 17731da177e4SLinus Torvalds static inline u32 file_mask_to_av(int mode, int mask) 17741da177e4SLinus Torvalds { 17751da177e4SLinus Torvalds u32 av = 0; 17761da177e4SLinus Torvalds 17771da177e4SLinus Torvalds if ((mode & S_IFMT) != S_IFDIR) { 17781da177e4SLinus Torvalds if (mask & MAY_EXEC) 17791da177e4SLinus Torvalds av |= FILE__EXECUTE; 17801da177e4SLinus Torvalds if (mask & MAY_READ) 17811da177e4SLinus Torvalds av |= FILE__READ; 17821da177e4SLinus Torvalds 17831da177e4SLinus Torvalds if (mask & MAY_APPEND) 17841da177e4SLinus Torvalds av |= FILE__APPEND; 17851da177e4SLinus Torvalds else if (mask & MAY_WRITE) 17861da177e4SLinus Torvalds av |= FILE__WRITE; 17871da177e4SLinus Torvalds 17881da177e4SLinus Torvalds } else { 17891da177e4SLinus Torvalds if (mask & MAY_EXEC) 17901da177e4SLinus Torvalds av |= DIR__SEARCH; 17911da177e4SLinus Torvalds if (mask & MAY_WRITE) 17921da177e4SLinus Torvalds av |= DIR__WRITE; 17931da177e4SLinus Torvalds if (mask & MAY_READ) 17941da177e4SLinus Torvalds av |= DIR__READ; 17951da177e4SLinus Torvalds } 17961da177e4SLinus Torvalds 17971da177e4SLinus Torvalds return av; 17981da177e4SLinus Torvalds } 17991da177e4SLinus Torvalds 18001da177e4SLinus Torvalds /* Convert a Linux file to an access vector. */ 18011da177e4SLinus Torvalds static inline u32 file_to_av(struct file *file) 18021da177e4SLinus Torvalds { 18031da177e4SLinus Torvalds u32 av = 0; 18041da177e4SLinus Torvalds 18051da177e4SLinus Torvalds if (file->f_mode & FMODE_READ) 18061da177e4SLinus Torvalds av |= FILE__READ; 18071da177e4SLinus Torvalds if (file->f_mode & FMODE_WRITE) { 18081da177e4SLinus Torvalds if (file->f_flags & O_APPEND) 18091da177e4SLinus Torvalds av |= FILE__APPEND; 18101da177e4SLinus Torvalds else 18111da177e4SLinus Torvalds av |= FILE__WRITE; 18121da177e4SLinus Torvalds } 18130794c66dSStephen Smalley if (!av) { 18140794c66dSStephen Smalley /* 18150794c66dSStephen Smalley * Special file opened with flags 3 for ioctl-only use. 18160794c66dSStephen Smalley */ 18170794c66dSStephen Smalley av = FILE__IOCTL; 18180794c66dSStephen Smalley } 18191da177e4SLinus Torvalds 18201da177e4SLinus Torvalds return av; 18211da177e4SLinus Torvalds } 18221da177e4SLinus Torvalds 18238b6a5a37SEric Paris /* 18248b6a5a37SEric Paris * Convert a file to an access vector and include the correct open 18258b6a5a37SEric Paris * open permission. 18268b6a5a37SEric Paris */ 18278b6a5a37SEric Paris static inline u32 open_file_to_av(struct file *file) 18288b6a5a37SEric Paris { 18298b6a5a37SEric Paris u32 av = file_to_av(file); 18308b6a5a37SEric Paris 18318b6a5a37SEric Paris if (selinux_policycap_openperm) { 18328b6a5a37SEric Paris mode_t mode = file->f_path.dentry->d_inode->i_mode; 18338b6a5a37SEric Paris /* 18348b6a5a37SEric Paris * lnk files and socks do not really have an 'open' 18358b6a5a37SEric Paris */ 18368b6a5a37SEric Paris if (S_ISREG(mode)) 18378b6a5a37SEric Paris av |= FILE__OPEN; 18388b6a5a37SEric Paris else if (S_ISCHR(mode)) 18398b6a5a37SEric Paris av |= CHR_FILE__OPEN; 18408b6a5a37SEric Paris else if (S_ISBLK(mode)) 18418b6a5a37SEric Paris av |= BLK_FILE__OPEN; 18428b6a5a37SEric Paris else if (S_ISFIFO(mode)) 18438b6a5a37SEric Paris av |= FIFO_FILE__OPEN; 18448b6a5a37SEric Paris else if (S_ISDIR(mode)) 18458b6a5a37SEric Paris av |= DIR__OPEN; 18466a25b27dSEric Paris else if (S_ISSOCK(mode)) 18476a25b27dSEric Paris av |= SOCK_FILE__OPEN; 18488b6a5a37SEric Paris else 18498b6a5a37SEric Paris printk(KERN_ERR "SELinux: WARNING: inside %s with " 18508b6a5a37SEric Paris "unknown mode:%o\n", __func__, mode); 18518b6a5a37SEric Paris } 18528b6a5a37SEric Paris return av; 18538b6a5a37SEric Paris } 18548b6a5a37SEric Paris 18551da177e4SLinus Torvalds /* Hook functions begin here. */ 18561da177e4SLinus Torvalds 18579e48858fSIngo Molnar static int selinux_ptrace_access_check(struct task_struct *child, 1858006ebb40SStephen Smalley unsigned int mode) 18591da177e4SLinus Torvalds { 18601da177e4SLinus Torvalds int rc; 18611da177e4SLinus Torvalds 18629e48858fSIngo Molnar rc = cap_ptrace_access_check(child, mode); 18631da177e4SLinus Torvalds if (rc) 18641da177e4SLinus Torvalds return rc; 18651da177e4SLinus Torvalds 1866006ebb40SStephen Smalley if (mode == PTRACE_MODE_READ) { 1867275bb41eSDavid Howells u32 sid = current_sid(); 1868275bb41eSDavid Howells u32 csid = task_sid(child); 1869275bb41eSDavid Howells return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, NULL); 1870006ebb40SStephen Smalley } 1871006ebb40SStephen Smalley 18723b11a1deSDavid Howells return current_has_perm(child, PROCESS__PTRACE); 18735cd9c58fSDavid Howells } 18745cd9c58fSDavid Howells 18755cd9c58fSDavid Howells static int selinux_ptrace_traceme(struct task_struct *parent) 18765cd9c58fSDavid Howells { 18775cd9c58fSDavid Howells int rc; 18785cd9c58fSDavid Howells 1879200ac532SEric Paris rc = cap_ptrace_traceme(parent); 18805cd9c58fSDavid Howells if (rc) 18815cd9c58fSDavid Howells return rc; 18825cd9c58fSDavid Howells 18835cd9c58fSDavid Howells return task_has_perm(parent, current, PROCESS__PTRACE); 18841da177e4SLinus Torvalds } 18851da177e4SLinus Torvalds 18861da177e4SLinus Torvalds static int selinux_capget(struct task_struct *target, kernel_cap_t *effective, 18871da177e4SLinus Torvalds kernel_cap_t *inheritable, kernel_cap_t *permitted) 18881da177e4SLinus Torvalds { 18891da177e4SLinus Torvalds int error; 18901da177e4SLinus Torvalds 18913b11a1deSDavid Howells error = current_has_perm(target, PROCESS__GETCAP); 18921da177e4SLinus Torvalds if (error) 18931da177e4SLinus Torvalds return error; 18941da177e4SLinus Torvalds 1895200ac532SEric Paris return cap_capget(target, effective, inheritable, permitted); 18961da177e4SLinus Torvalds } 18971da177e4SLinus Torvalds 1898d84f4f99SDavid Howells static int selinux_capset(struct cred *new, const struct cred *old, 1899d84f4f99SDavid Howells const kernel_cap_t *effective, 190015a2460eSDavid Howells const kernel_cap_t *inheritable, 190115a2460eSDavid Howells const kernel_cap_t *permitted) 19021da177e4SLinus Torvalds { 19031da177e4SLinus Torvalds int error; 19041da177e4SLinus Torvalds 1905200ac532SEric Paris error = cap_capset(new, old, 1906d84f4f99SDavid Howells effective, inheritable, permitted); 19071da177e4SLinus Torvalds if (error) 19081da177e4SLinus Torvalds return error; 19091da177e4SLinus Torvalds 1910d84f4f99SDavid Howells return cred_has_perm(old, new, PROCESS__SETCAP); 19111da177e4SLinus Torvalds } 19121da177e4SLinus Torvalds 19135626d3e8SJames Morris /* 19145626d3e8SJames Morris * (This comment used to live with the selinux_task_setuid hook, 19155626d3e8SJames Morris * which was removed). 19165626d3e8SJames Morris * 19175626d3e8SJames Morris * Since setuid only affects the current process, and since the SELinux 19185626d3e8SJames Morris * controls are not based on the Linux identity attributes, SELinux does not 19195626d3e8SJames Morris * need to control this operation. However, SELinux does control the use of 19205626d3e8SJames Morris * the CAP_SETUID and CAP_SETGID capabilities using the capable hook. 19215626d3e8SJames Morris */ 19225626d3e8SJames Morris 19233699c53cSDavid Howells static int selinux_capable(struct task_struct *tsk, const struct cred *cred, 19243699c53cSDavid Howells int cap, int audit) 19251da177e4SLinus Torvalds { 19261da177e4SLinus Torvalds int rc; 19271da177e4SLinus Torvalds 1928200ac532SEric Paris rc = cap_capable(tsk, cred, cap, audit); 19291da177e4SLinus Torvalds if (rc) 19301da177e4SLinus Torvalds return rc; 19311da177e4SLinus Torvalds 19323699c53cSDavid Howells return task_has_capability(tsk, cred, cap, audit); 19331da177e4SLinus Torvalds } 19341da177e4SLinus Torvalds 19353fbfa981SEric W. Biederman static int selinux_sysctl_get_sid(ctl_table *table, u16 tclass, u32 *sid) 19363fbfa981SEric W. Biederman { 19373fbfa981SEric W. Biederman int buflen, rc; 19383fbfa981SEric W. Biederman char *buffer, *path, *end; 19393fbfa981SEric W. Biederman 19403fbfa981SEric W. Biederman rc = -ENOMEM; 19413fbfa981SEric W. Biederman buffer = (char *)__get_free_page(GFP_KERNEL); 19423fbfa981SEric W. Biederman if (!buffer) 19433fbfa981SEric W. Biederman goto out; 19443fbfa981SEric W. Biederman 19453fbfa981SEric W. Biederman buflen = PAGE_SIZE; 19463fbfa981SEric W. Biederman end = buffer+buflen; 19473fbfa981SEric W. Biederman *--end = '\0'; 19483fbfa981SEric W. Biederman buflen--; 19493fbfa981SEric W. Biederman path = end-1; 19503fbfa981SEric W. Biederman *path = '/'; 19513fbfa981SEric W. Biederman while (table) { 19523fbfa981SEric W. Biederman const char *name = table->procname; 19533fbfa981SEric W. Biederman size_t namelen = strlen(name); 19543fbfa981SEric W. Biederman buflen -= namelen + 1; 19553fbfa981SEric W. Biederman if (buflen < 0) 19563fbfa981SEric W. Biederman goto out_free; 19573fbfa981SEric W. Biederman end -= namelen; 19583fbfa981SEric W. Biederman memcpy(end, name, namelen); 19593fbfa981SEric W. Biederman *--end = '/'; 19603fbfa981SEric W. Biederman path = end; 19613fbfa981SEric W. Biederman table = table->parent; 19623fbfa981SEric W. Biederman } 1963b599fdfdSEric W. Biederman buflen -= 4; 1964b599fdfdSEric W. Biederman if (buflen < 0) 1965b599fdfdSEric W. Biederman goto out_free; 1966b599fdfdSEric W. Biederman end -= 4; 1967b599fdfdSEric W. Biederman memcpy(end, "/sys", 4); 1968b599fdfdSEric W. Biederman path = end; 19693fbfa981SEric W. Biederman rc = security_genfs_sid("proc", path, tclass, sid); 19703fbfa981SEric W. Biederman out_free: 19713fbfa981SEric W. Biederman free_page((unsigned long)buffer); 19723fbfa981SEric W. Biederman out: 19733fbfa981SEric W. Biederman return rc; 19743fbfa981SEric W. Biederman } 19753fbfa981SEric W. Biederman 19761da177e4SLinus Torvalds static int selinux_sysctl(ctl_table *table, int op) 19771da177e4SLinus Torvalds { 19781da177e4SLinus Torvalds int error = 0; 19791da177e4SLinus Torvalds u32 av; 1980275bb41eSDavid Howells u32 tsid, sid; 19811da177e4SLinus Torvalds int rc; 19821da177e4SLinus Torvalds 1983275bb41eSDavid Howells sid = current_sid(); 19841da177e4SLinus Torvalds 19853fbfa981SEric W. Biederman rc = selinux_sysctl_get_sid(table, (op == 0001) ? 19861da177e4SLinus Torvalds SECCLASS_DIR : SECCLASS_FILE, &tsid); 19871da177e4SLinus Torvalds if (rc) { 19881da177e4SLinus Torvalds /* Default to the well-defined sysctl SID. */ 19891da177e4SLinus Torvalds tsid = SECINITSID_SYSCTL; 19901da177e4SLinus Torvalds } 19911da177e4SLinus Torvalds 19921da177e4SLinus Torvalds /* The op values are "defined" in sysctl.c, thereby creating 19931da177e4SLinus Torvalds * a bad coupling between this module and sysctl.c */ 19941da177e4SLinus Torvalds if (op == 001) { 1995275bb41eSDavid Howells error = avc_has_perm(sid, tsid, 19961da177e4SLinus Torvalds SECCLASS_DIR, DIR__SEARCH, NULL); 19971da177e4SLinus Torvalds } else { 19981da177e4SLinus Torvalds av = 0; 19991da177e4SLinus Torvalds if (op & 004) 20001da177e4SLinus Torvalds av |= FILE__READ; 20011da177e4SLinus Torvalds if (op & 002) 20021da177e4SLinus Torvalds av |= FILE__WRITE; 20031da177e4SLinus Torvalds if (av) 2004275bb41eSDavid Howells error = avc_has_perm(sid, tsid, 20051da177e4SLinus Torvalds SECCLASS_FILE, av, NULL); 20061da177e4SLinus Torvalds } 20071da177e4SLinus Torvalds 20081da177e4SLinus Torvalds return error; 20091da177e4SLinus Torvalds } 20101da177e4SLinus Torvalds 20111da177e4SLinus Torvalds static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb) 20121da177e4SLinus Torvalds { 201388e67f3bSDavid Howells const struct cred *cred = current_cred(); 20141da177e4SLinus Torvalds int rc = 0; 20151da177e4SLinus Torvalds 20161da177e4SLinus Torvalds if (!sb) 20171da177e4SLinus Torvalds return 0; 20181da177e4SLinus Torvalds 20191da177e4SLinus Torvalds switch (cmds) { 20201da177e4SLinus Torvalds case Q_SYNC: 20211da177e4SLinus Torvalds case Q_QUOTAON: 20221da177e4SLinus Torvalds case Q_QUOTAOFF: 20231da177e4SLinus Torvalds case Q_SETINFO: 20241da177e4SLinus Torvalds case Q_SETQUOTA: 202588e67f3bSDavid Howells rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL); 20261da177e4SLinus Torvalds break; 20271da177e4SLinus Torvalds case Q_GETFMT: 20281da177e4SLinus Torvalds case Q_GETINFO: 20291da177e4SLinus Torvalds case Q_GETQUOTA: 203088e67f3bSDavid Howells rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL); 20311da177e4SLinus Torvalds break; 20321da177e4SLinus Torvalds default: 20331da177e4SLinus Torvalds rc = 0; /* let the kernel handle invalid cmds */ 20341da177e4SLinus Torvalds break; 20351da177e4SLinus Torvalds } 20361da177e4SLinus Torvalds return rc; 20371da177e4SLinus Torvalds } 20381da177e4SLinus Torvalds 20391da177e4SLinus Torvalds static int selinux_quota_on(struct dentry *dentry) 20401da177e4SLinus Torvalds { 204188e67f3bSDavid Howells const struct cred *cred = current_cred(); 204288e67f3bSDavid Howells 204388e67f3bSDavid Howells return dentry_has_perm(cred, NULL, dentry, FILE__QUOTAON); 20441da177e4SLinus Torvalds } 20451da177e4SLinus Torvalds 20461da177e4SLinus Torvalds static int selinux_syslog(int type) 20471da177e4SLinus Torvalds { 20481da177e4SLinus Torvalds int rc; 20491da177e4SLinus Torvalds 2050200ac532SEric Paris rc = cap_syslog(type); 20511da177e4SLinus Torvalds if (rc) 20521da177e4SLinus Torvalds return rc; 20531da177e4SLinus Torvalds 20541da177e4SLinus Torvalds switch (type) { 20551da177e4SLinus Torvalds case 3: /* Read last kernel messages */ 20561da177e4SLinus Torvalds case 10: /* Return size of the log buffer */ 20571da177e4SLinus Torvalds rc = task_has_system(current, SYSTEM__SYSLOG_READ); 20581da177e4SLinus Torvalds break; 20591da177e4SLinus Torvalds case 6: /* Disable logging to console */ 20601da177e4SLinus Torvalds case 7: /* Enable logging to console */ 20611da177e4SLinus Torvalds case 8: /* Set level of messages printed to console */ 20621da177e4SLinus Torvalds rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE); 20631da177e4SLinus Torvalds break; 20641da177e4SLinus Torvalds case 0: /* Close log */ 20651da177e4SLinus Torvalds case 1: /* Open log */ 20661da177e4SLinus Torvalds case 2: /* Read from log */ 20671da177e4SLinus Torvalds case 4: /* Read/clear last kernel messages */ 20681da177e4SLinus Torvalds case 5: /* Clear ring buffer */ 20691da177e4SLinus Torvalds default: 20701da177e4SLinus Torvalds rc = task_has_system(current, SYSTEM__SYSLOG_MOD); 20711da177e4SLinus Torvalds break; 20721da177e4SLinus Torvalds } 20731da177e4SLinus Torvalds return rc; 20741da177e4SLinus Torvalds } 20751da177e4SLinus Torvalds 20761da177e4SLinus Torvalds /* 20771da177e4SLinus Torvalds * Check that a process has enough memory to allocate a new virtual 20781da177e4SLinus Torvalds * mapping. 0 means there is enough memory for the allocation to 20791da177e4SLinus Torvalds * succeed and -ENOMEM implies there is not. 20801da177e4SLinus Torvalds * 20811da177e4SLinus Torvalds * Do not audit the selinux permission check, as this is applied to all 20821da177e4SLinus Torvalds * processes that allocate mappings. 20831da177e4SLinus Torvalds */ 208434b4e4aaSAlan Cox static int selinux_vm_enough_memory(struct mm_struct *mm, long pages) 20851da177e4SLinus Torvalds { 20861da177e4SLinus Torvalds int rc, cap_sys_admin = 0; 20871da177e4SLinus Torvalds 20883699c53cSDavid Howells rc = selinux_capable(current, current_cred(), CAP_SYS_ADMIN, 20893699c53cSDavid Howells SECURITY_CAP_NOAUDIT); 20901da177e4SLinus Torvalds if (rc == 0) 20911da177e4SLinus Torvalds cap_sys_admin = 1; 20921da177e4SLinus Torvalds 209334b4e4aaSAlan Cox return __vm_enough_memory(mm, pages, cap_sys_admin); 20941da177e4SLinus Torvalds } 20951da177e4SLinus Torvalds 20961da177e4SLinus Torvalds /* binprm security operations */ 20971da177e4SLinus Torvalds 2098a6f76f23SDavid Howells static int selinux_bprm_set_creds(struct linux_binprm *bprm) 20991da177e4SLinus Torvalds { 2100a6f76f23SDavid Howells const struct task_security_struct *old_tsec; 2101a6f76f23SDavid Howells struct task_security_struct *new_tsec; 21021da177e4SLinus Torvalds struct inode_security_struct *isec; 2103*2bf49690SThomas Liu struct common_audit_data ad; 2104a6f76f23SDavid Howells struct inode *inode = bprm->file->f_path.dentry->d_inode; 21051da177e4SLinus Torvalds int rc; 21061da177e4SLinus Torvalds 2107200ac532SEric Paris rc = cap_bprm_set_creds(bprm); 21081da177e4SLinus Torvalds if (rc) 21091da177e4SLinus Torvalds return rc; 21101da177e4SLinus Torvalds 2111a6f76f23SDavid Howells /* SELinux context only depends on initial program or script and not 2112a6f76f23SDavid Howells * the script interpreter */ 2113a6f76f23SDavid Howells if (bprm->cred_prepared) 21141da177e4SLinus Torvalds return 0; 21151da177e4SLinus Torvalds 2116a6f76f23SDavid Howells old_tsec = current_security(); 2117a6f76f23SDavid Howells new_tsec = bprm->cred->security; 21181da177e4SLinus Torvalds isec = inode->i_security; 21191da177e4SLinus Torvalds 21201da177e4SLinus Torvalds /* Default to the current task SID. */ 2121a6f76f23SDavid Howells new_tsec->sid = old_tsec->sid; 2122a6f76f23SDavid Howells new_tsec->osid = old_tsec->sid; 21231da177e4SLinus Torvalds 212428eba5bfSMichael LeMay /* Reset fs, key, and sock SIDs on execve. */ 2125a6f76f23SDavid Howells new_tsec->create_sid = 0; 2126a6f76f23SDavid Howells new_tsec->keycreate_sid = 0; 2127a6f76f23SDavid Howells new_tsec->sockcreate_sid = 0; 21281da177e4SLinus Torvalds 2129a6f76f23SDavid Howells if (old_tsec->exec_sid) { 2130a6f76f23SDavid Howells new_tsec->sid = old_tsec->exec_sid; 21311da177e4SLinus Torvalds /* Reset exec SID on execve. */ 2132a6f76f23SDavid Howells new_tsec->exec_sid = 0; 21331da177e4SLinus Torvalds } else { 21341da177e4SLinus Torvalds /* Check for a default transition on this program. */ 2135a6f76f23SDavid Howells rc = security_transition_sid(old_tsec->sid, isec->sid, 2136a6f76f23SDavid Howells SECCLASS_PROCESS, &new_tsec->sid); 21371da177e4SLinus Torvalds if (rc) 21381da177e4SLinus Torvalds return rc; 21391da177e4SLinus Torvalds } 21401da177e4SLinus Torvalds 2141*2bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, FS); 214244707fdfSJan Blunck ad.u.fs.path = bprm->file->f_path; 21431da177e4SLinus Torvalds 21443d5ff529SJosef Sipek if (bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) 2145a6f76f23SDavid Howells new_tsec->sid = old_tsec->sid; 21461da177e4SLinus Torvalds 2147a6f76f23SDavid Howells if (new_tsec->sid == old_tsec->sid) { 2148a6f76f23SDavid Howells rc = avc_has_perm(old_tsec->sid, isec->sid, 21491da177e4SLinus Torvalds SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad); 21501da177e4SLinus Torvalds if (rc) 21511da177e4SLinus Torvalds return rc; 21521da177e4SLinus Torvalds } else { 21531da177e4SLinus Torvalds /* Check permissions for the transition. */ 2154a6f76f23SDavid Howells rc = avc_has_perm(old_tsec->sid, new_tsec->sid, 21551da177e4SLinus Torvalds SECCLASS_PROCESS, PROCESS__TRANSITION, &ad); 21561da177e4SLinus Torvalds if (rc) 21571da177e4SLinus Torvalds return rc; 21581da177e4SLinus Torvalds 2159a6f76f23SDavid Howells rc = avc_has_perm(new_tsec->sid, isec->sid, 21601da177e4SLinus Torvalds SECCLASS_FILE, FILE__ENTRYPOINT, &ad); 21611da177e4SLinus Torvalds if (rc) 21621da177e4SLinus Torvalds return rc; 21631da177e4SLinus Torvalds 2164a6f76f23SDavid Howells /* Check for shared state */ 2165a6f76f23SDavid Howells if (bprm->unsafe & LSM_UNSAFE_SHARE) { 2166a6f76f23SDavid Howells rc = avc_has_perm(old_tsec->sid, new_tsec->sid, 2167a6f76f23SDavid Howells SECCLASS_PROCESS, PROCESS__SHARE, 2168a6f76f23SDavid Howells NULL); 2169a6f76f23SDavid Howells if (rc) 2170a6f76f23SDavid Howells return -EPERM; 21711da177e4SLinus Torvalds } 21721da177e4SLinus Torvalds 2173a6f76f23SDavid Howells /* Make sure that anyone attempting to ptrace over a task that 2174a6f76f23SDavid Howells * changes its SID has the appropriate permit */ 2175a6f76f23SDavid Howells if (bprm->unsafe & 2176a6f76f23SDavid Howells (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) { 2177a6f76f23SDavid Howells struct task_struct *tracer; 2178a6f76f23SDavid Howells struct task_security_struct *sec; 2179a6f76f23SDavid Howells u32 ptsid = 0; 2180a6f76f23SDavid Howells 2181a6f76f23SDavid Howells rcu_read_lock(); 2182a6f76f23SDavid Howells tracer = tracehook_tracer_task(current); 2183a6f76f23SDavid Howells if (likely(tracer != NULL)) { 2184a6f76f23SDavid Howells sec = __task_cred(tracer)->security; 2185a6f76f23SDavid Howells ptsid = sec->sid; 2186a6f76f23SDavid Howells } 2187a6f76f23SDavid Howells rcu_read_unlock(); 2188a6f76f23SDavid Howells 2189a6f76f23SDavid Howells if (ptsid != 0) { 2190a6f76f23SDavid Howells rc = avc_has_perm(ptsid, new_tsec->sid, 2191a6f76f23SDavid Howells SECCLASS_PROCESS, 2192a6f76f23SDavid Howells PROCESS__PTRACE, NULL); 2193a6f76f23SDavid Howells if (rc) 2194a6f76f23SDavid Howells return -EPERM; 2195a6f76f23SDavid Howells } 2196a6f76f23SDavid Howells } 2197a6f76f23SDavid Howells 2198a6f76f23SDavid Howells /* Clear any possibly unsafe personality bits on exec: */ 2199a6f76f23SDavid Howells bprm->per_clear |= PER_CLEAR_ON_SETID; 2200a6f76f23SDavid Howells } 2201a6f76f23SDavid Howells 22021da177e4SLinus Torvalds return 0; 22031da177e4SLinus Torvalds } 22041da177e4SLinus Torvalds 22051da177e4SLinus Torvalds static int selinux_bprm_secureexec(struct linux_binprm *bprm) 22061da177e4SLinus Torvalds { 2207275bb41eSDavid Howells const struct cred *cred = current_cred(); 2208275bb41eSDavid Howells const struct task_security_struct *tsec = cred->security; 2209275bb41eSDavid Howells u32 sid, osid; 22101da177e4SLinus Torvalds int atsecure = 0; 22111da177e4SLinus Torvalds 2212275bb41eSDavid Howells sid = tsec->sid; 2213275bb41eSDavid Howells osid = tsec->osid; 2214275bb41eSDavid Howells 2215275bb41eSDavid Howells if (osid != sid) { 22161da177e4SLinus Torvalds /* Enable secure mode for SIDs transitions unless 22171da177e4SLinus Torvalds the noatsecure permission is granted between 22181da177e4SLinus Torvalds the two SIDs, i.e. ahp returns 0. */ 2219275bb41eSDavid Howells atsecure = avc_has_perm(osid, sid, 22201da177e4SLinus Torvalds SECCLASS_PROCESS, 22211da177e4SLinus Torvalds PROCESS__NOATSECURE, NULL); 22221da177e4SLinus Torvalds } 22231da177e4SLinus Torvalds 2224200ac532SEric Paris return (atsecure || cap_bprm_secureexec(bprm)); 22251da177e4SLinus Torvalds } 22261da177e4SLinus Torvalds 22271da177e4SLinus Torvalds extern struct vfsmount *selinuxfs_mount; 22281da177e4SLinus Torvalds extern struct dentry *selinux_null; 22291da177e4SLinus Torvalds 22301da177e4SLinus Torvalds /* Derived from fs/exec.c:flush_old_files. */ 2231745ca247SDavid Howells static inline void flush_unauthorized_files(const struct cred *cred, 2232745ca247SDavid Howells struct files_struct *files) 22331da177e4SLinus Torvalds { 2234*2bf49690SThomas Liu struct common_audit_data ad; 22351da177e4SLinus Torvalds struct file *file, *devnull = NULL; 2236b20c8122SStephen Smalley struct tty_struct *tty; 2237badf1662SDipankar Sarma struct fdtable *fdt; 22381da177e4SLinus Torvalds long j = -1; 223924ec839cSPeter Zijlstra int drop_tty = 0; 22401da177e4SLinus Torvalds 224124ec839cSPeter Zijlstra tty = get_current_tty(); 22421da177e4SLinus Torvalds if (tty) { 22431da177e4SLinus Torvalds file_list_lock(); 224437dd0bd0SEric Paris if (!list_empty(&tty->tty_files)) { 224537dd0bd0SEric Paris struct inode *inode; 224637dd0bd0SEric Paris 22471da177e4SLinus Torvalds /* Revalidate access to controlling tty. 22481da177e4SLinus Torvalds Use inode_has_perm on the tty inode directly rather 22491da177e4SLinus Torvalds than using file_has_perm, as this particular open 22501da177e4SLinus Torvalds file may belong to another process and we are only 22511da177e4SLinus Torvalds interested in the inode-based check here. */ 225237dd0bd0SEric Paris file = list_first_entry(&tty->tty_files, struct file, f_u.fu_list); 225337dd0bd0SEric Paris inode = file->f_path.dentry->d_inode; 225488e67f3bSDavid Howells if (inode_has_perm(cred, inode, 22551da177e4SLinus Torvalds FILE__READ | FILE__WRITE, NULL)) { 225624ec839cSPeter Zijlstra drop_tty = 1; 22571da177e4SLinus Torvalds } 22581da177e4SLinus Torvalds } 22591da177e4SLinus Torvalds file_list_unlock(); 2260452a00d2SAlan Cox tty_kref_put(tty); 22611da177e4SLinus Torvalds } 226298a27ba4SEric W. Biederman /* Reset controlling tty. */ 226398a27ba4SEric W. Biederman if (drop_tty) 226498a27ba4SEric W. Biederman no_tty(); 22651da177e4SLinus Torvalds 22661da177e4SLinus Torvalds /* Revalidate access to inherited open files. */ 22671da177e4SLinus Torvalds 2268*2bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, FS); 22691da177e4SLinus Torvalds 22701da177e4SLinus Torvalds spin_lock(&files->file_lock); 22711da177e4SLinus Torvalds for (;;) { 22721da177e4SLinus Torvalds unsigned long set, i; 22731da177e4SLinus Torvalds int fd; 22741da177e4SLinus Torvalds 22751da177e4SLinus Torvalds j++; 22761da177e4SLinus Torvalds i = j * __NFDBITS; 2277badf1662SDipankar Sarma fdt = files_fdtable(files); 2278bbea9f69SVadim Lobanov if (i >= fdt->max_fds) 22791da177e4SLinus Torvalds break; 2280badf1662SDipankar Sarma set = fdt->open_fds->fds_bits[j]; 22811da177e4SLinus Torvalds if (!set) 22821da177e4SLinus Torvalds continue; 22831da177e4SLinus Torvalds spin_unlock(&files->file_lock); 22841da177e4SLinus Torvalds for ( ; set ; i++, set >>= 1) { 22851da177e4SLinus Torvalds if (set & 1) { 22861da177e4SLinus Torvalds file = fget(i); 22871da177e4SLinus Torvalds if (!file) 22881da177e4SLinus Torvalds continue; 228988e67f3bSDavid Howells if (file_has_perm(cred, 22901da177e4SLinus Torvalds file, 22911da177e4SLinus Torvalds file_to_av(file))) { 22921da177e4SLinus Torvalds sys_close(i); 22931da177e4SLinus Torvalds fd = get_unused_fd(); 22941da177e4SLinus Torvalds if (fd != i) { 22951da177e4SLinus Torvalds if (fd >= 0) 22961da177e4SLinus Torvalds put_unused_fd(fd); 22971da177e4SLinus Torvalds fput(file); 22981da177e4SLinus Torvalds continue; 22991da177e4SLinus Torvalds } 23001da177e4SLinus Torvalds if (devnull) { 2301095975daSNick Piggin get_file(devnull); 23021da177e4SLinus Torvalds } else { 2303745ca247SDavid Howells devnull = dentry_open( 2304745ca247SDavid Howells dget(selinux_null), 2305745ca247SDavid Howells mntget(selinuxfs_mount), 2306745ca247SDavid Howells O_RDWR, cred); 2307fc5d81e6SAkinobu Mita if (IS_ERR(devnull)) { 2308fc5d81e6SAkinobu Mita devnull = NULL; 23091da177e4SLinus Torvalds put_unused_fd(fd); 23101da177e4SLinus Torvalds fput(file); 23111da177e4SLinus Torvalds continue; 23121da177e4SLinus Torvalds } 23131da177e4SLinus Torvalds } 23141da177e4SLinus Torvalds fd_install(fd, devnull); 23151da177e4SLinus Torvalds } 23161da177e4SLinus Torvalds fput(file); 23171da177e4SLinus Torvalds } 23181da177e4SLinus Torvalds } 23191da177e4SLinus Torvalds spin_lock(&files->file_lock); 23201da177e4SLinus Torvalds 23211da177e4SLinus Torvalds } 23221da177e4SLinus Torvalds spin_unlock(&files->file_lock); 23231da177e4SLinus Torvalds } 23241da177e4SLinus Torvalds 23251da177e4SLinus Torvalds /* 2326a6f76f23SDavid Howells * Prepare a process for imminent new credential changes due to exec 23271da177e4SLinus Torvalds */ 2328a6f76f23SDavid Howells static void selinux_bprm_committing_creds(struct linux_binprm *bprm) 23291da177e4SLinus Torvalds { 2330a6f76f23SDavid Howells struct task_security_struct *new_tsec; 23311da177e4SLinus Torvalds struct rlimit *rlim, *initrlim; 23321da177e4SLinus Torvalds int rc, i; 23331da177e4SLinus Torvalds 2334a6f76f23SDavid Howells new_tsec = bprm->cred->security; 2335a6f76f23SDavid Howells if (new_tsec->sid == new_tsec->osid) 23361da177e4SLinus Torvalds return; 23371da177e4SLinus Torvalds 23381da177e4SLinus Torvalds /* Close files for which the new task SID is not authorized. */ 2339a6f76f23SDavid Howells flush_unauthorized_files(bprm->cred, current->files); 23401da177e4SLinus Torvalds 2341a6f76f23SDavid Howells /* Always clear parent death signal on SID transitions. */ 2342a6f76f23SDavid Howells current->pdeath_signal = 0; 2343a6f76f23SDavid Howells 2344a6f76f23SDavid Howells /* Check whether the new SID can inherit resource limits from the old 2345a6f76f23SDavid Howells * SID. If not, reset all soft limits to the lower of the current 2346a6f76f23SDavid Howells * task's hard limit and the init task's soft limit. 2347a6f76f23SDavid Howells * 2348a6f76f23SDavid Howells * Note that the setting of hard limits (even to lower them) can be 2349a6f76f23SDavid Howells * controlled by the setrlimit check. The inclusion of the init task's 2350a6f76f23SDavid Howells * soft limit into the computation is to avoid resetting soft limits 2351a6f76f23SDavid Howells * higher than the default soft limit for cases where the default is 2352a6f76f23SDavid Howells * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK. 2353a6f76f23SDavid Howells */ 2354a6f76f23SDavid Howells rc = avc_has_perm(new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS, 2355a6f76f23SDavid Howells PROCESS__RLIMITINH, NULL); 2356a6f76f23SDavid Howells if (rc) { 2357a6f76f23SDavid Howells for (i = 0; i < RLIM_NLIMITS; i++) { 2358a6f76f23SDavid Howells rlim = current->signal->rlim + i; 2359a6f76f23SDavid Howells initrlim = init_task.signal->rlim + i; 2360a6f76f23SDavid Howells rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur); 2361a6f76f23SDavid Howells } 2362a6f76f23SDavid Howells update_rlimit_cpu(rlim->rlim_cur); 2363a6f76f23SDavid Howells } 2364a6f76f23SDavid Howells } 2365a6f76f23SDavid Howells 2366a6f76f23SDavid Howells /* 2367a6f76f23SDavid Howells * Clean up the process immediately after the installation of new credentials 2368a6f76f23SDavid Howells * due to exec 2369a6f76f23SDavid Howells */ 2370a6f76f23SDavid Howells static void selinux_bprm_committed_creds(struct linux_binprm *bprm) 2371a6f76f23SDavid Howells { 2372a6f76f23SDavid Howells const struct task_security_struct *tsec = current_security(); 2373a6f76f23SDavid Howells struct itimerval itimer; 2374a6f76f23SDavid Howells u32 osid, sid; 2375a6f76f23SDavid Howells int rc, i; 2376a6f76f23SDavid Howells 2377a6f76f23SDavid Howells osid = tsec->osid; 2378a6f76f23SDavid Howells sid = tsec->sid; 2379a6f76f23SDavid Howells 2380a6f76f23SDavid Howells if (sid == osid) 2381a6f76f23SDavid Howells return; 2382a6f76f23SDavid Howells 2383a6f76f23SDavid Howells /* Check whether the new SID can inherit signal state from the old SID. 2384a6f76f23SDavid Howells * If not, clear itimers to avoid subsequent signal generation and 2385a6f76f23SDavid Howells * flush and unblock signals. 2386a6f76f23SDavid Howells * 2387a6f76f23SDavid Howells * This must occur _after_ the task SID has been updated so that any 2388a6f76f23SDavid Howells * kill done after the flush will be checked against the new SID. 2389a6f76f23SDavid Howells */ 2390a6f76f23SDavid Howells rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL); 23911da177e4SLinus Torvalds if (rc) { 23921da177e4SLinus Torvalds memset(&itimer, 0, sizeof itimer); 23931da177e4SLinus Torvalds for (i = 0; i < 3; i++) 23941da177e4SLinus Torvalds do_setitimer(i, &itimer, NULL); 23951da177e4SLinus Torvalds spin_lock_irq(¤t->sighand->siglock); 23963bcac026SDavid Howells if (!(current->signal->flags & SIGNAL_GROUP_EXIT)) { 23973bcac026SDavid Howells __flush_signals(current); 23981da177e4SLinus Torvalds flush_signal_handlers(current, 1); 23991da177e4SLinus Torvalds sigemptyset(¤t->blocked); 24003bcac026SDavid Howells } 24011da177e4SLinus Torvalds spin_unlock_irq(¤t->sighand->siglock); 24021da177e4SLinus Torvalds } 24031da177e4SLinus Torvalds 2404a6f76f23SDavid Howells /* Wake up the parent if it is waiting so that it can recheck 2405a6f76f23SDavid Howells * wait permission to the new task SID. */ 2406ecd6de3cSOleg Nesterov read_lock(&tasklist_lock); 2407ecd6de3cSOleg Nesterov wake_up_interruptible(¤t->real_parent->signal->wait_chldexit); 2408ecd6de3cSOleg Nesterov read_unlock(&tasklist_lock); 24091da177e4SLinus Torvalds } 24101da177e4SLinus Torvalds 24111da177e4SLinus Torvalds /* superblock security operations */ 24121da177e4SLinus Torvalds 24131da177e4SLinus Torvalds static int selinux_sb_alloc_security(struct super_block *sb) 24141da177e4SLinus Torvalds { 24151da177e4SLinus Torvalds return superblock_alloc_security(sb); 24161da177e4SLinus Torvalds } 24171da177e4SLinus Torvalds 24181da177e4SLinus Torvalds static void selinux_sb_free_security(struct super_block *sb) 24191da177e4SLinus Torvalds { 24201da177e4SLinus Torvalds superblock_free_security(sb); 24211da177e4SLinus Torvalds } 24221da177e4SLinus Torvalds 24231da177e4SLinus Torvalds static inline int match_prefix(char *prefix, int plen, char *option, int olen) 24241da177e4SLinus Torvalds { 24251da177e4SLinus Torvalds if (plen > olen) 24261da177e4SLinus Torvalds return 0; 24271da177e4SLinus Torvalds 24281da177e4SLinus Torvalds return !memcmp(prefix, option, plen); 24291da177e4SLinus Torvalds } 24301da177e4SLinus Torvalds 24311da177e4SLinus Torvalds static inline int selinux_option(char *option, int len) 24321da177e4SLinus Torvalds { 2433832cbd9aSEric Paris return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) || 2434832cbd9aSEric Paris match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) || 2435832cbd9aSEric Paris match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) || 243611689d47SDavid P. Quigley match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) || 243711689d47SDavid P. Quigley match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len)); 24381da177e4SLinus Torvalds } 24391da177e4SLinus Torvalds 24401da177e4SLinus Torvalds static inline void take_option(char **to, char *from, int *first, int len) 24411da177e4SLinus Torvalds { 24421da177e4SLinus Torvalds if (!*first) { 24431da177e4SLinus Torvalds **to = ','; 24441da177e4SLinus Torvalds *to += 1; 24453528a953SCory Olmo } else 24461da177e4SLinus Torvalds *first = 0; 24471da177e4SLinus Torvalds memcpy(*to, from, len); 24481da177e4SLinus Torvalds *to += len; 24491da177e4SLinus Torvalds } 24501da177e4SLinus Torvalds 24513528a953SCory Olmo static inline void take_selinux_option(char **to, char *from, int *first, 24523528a953SCory Olmo int len) 24533528a953SCory Olmo { 24543528a953SCory Olmo int current_size = 0; 24553528a953SCory Olmo 24563528a953SCory Olmo if (!*first) { 24573528a953SCory Olmo **to = '|'; 24583528a953SCory Olmo *to += 1; 2459828dfe1dSEric Paris } else 24603528a953SCory Olmo *first = 0; 24613528a953SCory Olmo 24623528a953SCory Olmo while (current_size < len) { 24633528a953SCory Olmo if (*from != '"') { 24643528a953SCory Olmo **to = *from; 24653528a953SCory Olmo *to += 1; 24663528a953SCory Olmo } 24673528a953SCory Olmo from += 1; 24683528a953SCory Olmo current_size += 1; 24693528a953SCory Olmo } 24703528a953SCory Olmo } 24713528a953SCory Olmo 2472e0007529SEric Paris static int selinux_sb_copy_data(char *orig, char *copy) 24731da177e4SLinus Torvalds { 24741da177e4SLinus Torvalds int fnosec, fsec, rc = 0; 24751da177e4SLinus Torvalds char *in_save, *in_curr, *in_end; 24761da177e4SLinus Torvalds char *sec_curr, *nosec_save, *nosec; 24773528a953SCory Olmo int open_quote = 0; 24781da177e4SLinus Torvalds 24791da177e4SLinus Torvalds in_curr = orig; 24801da177e4SLinus Torvalds sec_curr = copy; 24811da177e4SLinus Torvalds 24821da177e4SLinus Torvalds nosec = (char *)get_zeroed_page(GFP_KERNEL); 24831da177e4SLinus Torvalds if (!nosec) { 24841da177e4SLinus Torvalds rc = -ENOMEM; 24851da177e4SLinus Torvalds goto out; 24861da177e4SLinus Torvalds } 24871da177e4SLinus Torvalds 24881da177e4SLinus Torvalds nosec_save = nosec; 24891da177e4SLinus Torvalds fnosec = fsec = 1; 24901da177e4SLinus Torvalds in_save = in_end = orig; 24911da177e4SLinus Torvalds 24921da177e4SLinus Torvalds do { 24933528a953SCory Olmo if (*in_end == '"') 24943528a953SCory Olmo open_quote = !open_quote; 24953528a953SCory Olmo if ((*in_end == ',' && open_quote == 0) || 24963528a953SCory Olmo *in_end == '\0') { 24971da177e4SLinus Torvalds int len = in_end - in_curr; 24981da177e4SLinus Torvalds 24991da177e4SLinus Torvalds if (selinux_option(in_curr, len)) 25003528a953SCory Olmo take_selinux_option(&sec_curr, in_curr, &fsec, len); 25011da177e4SLinus Torvalds else 25021da177e4SLinus Torvalds take_option(&nosec, in_curr, &fnosec, len); 25031da177e4SLinus Torvalds 25041da177e4SLinus Torvalds in_curr = in_end + 1; 25051da177e4SLinus Torvalds } 25061da177e4SLinus Torvalds } while (*in_end++); 25071da177e4SLinus Torvalds 25086931dfc9SEric Paris strcpy(in_save, nosec_save); 2509da3caa20SGerald Schaefer free_page((unsigned long)nosec_save); 25101da177e4SLinus Torvalds out: 25111da177e4SLinus Torvalds return rc; 25121da177e4SLinus Torvalds } 25131da177e4SLinus Torvalds 251412204e24SJames Morris static int selinux_sb_kern_mount(struct super_block *sb, int flags, void *data) 25151da177e4SLinus Torvalds { 251688e67f3bSDavid Howells const struct cred *cred = current_cred(); 2517*2bf49690SThomas Liu struct common_audit_data ad; 25181da177e4SLinus Torvalds int rc; 25191da177e4SLinus Torvalds 25201da177e4SLinus Torvalds rc = superblock_doinit(sb, data); 25211da177e4SLinus Torvalds if (rc) 25221da177e4SLinus Torvalds return rc; 25231da177e4SLinus Torvalds 252474192246SJames Morris /* Allow all mounts performed by the kernel */ 252574192246SJames Morris if (flags & MS_KERNMOUNT) 252674192246SJames Morris return 0; 252774192246SJames Morris 2528*2bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, FS); 252944707fdfSJan Blunck ad.u.fs.path.dentry = sb->s_root; 253088e67f3bSDavid Howells return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad); 25311da177e4SLinus Torvalds } 25321da177e4SLinus Torvalds 2533726c3342SDavid Howells static int selinux_sb_statfs(struct dentry *dentry) 25341da177e4SLinus Torvalds { 253588e67f3bSDavid Howells const struct cred *cred = current_cred(); 2536*2bf49690SThomas Liu struct common_audit_data ad; 25371da177e4SLinus Torvalds 2538*2bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, FS); 253944707fdfSJan Blunck ad.u.fs.path.dentry = dentry->d_sb->s_root; 254088e67f3bSDavid Howells return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad); 25411da177e4SLinus Torvalds } 25421da177e4SLinus Torvalds 25431da177e4SLinus Torvalds static int selinux_mount(char *dev_name, 2544b5266eb4SAl Viro struct path *path, 25451da177e4SLinus Torvalds char *type, 25461da177e4SLinus Torvalds unsigned long flags, 25471da177e4SLinus Torvalds void *data) 25481da177e4SLinus Torvalds { 254988e67f3bSDavid Howells const struct cred *cred = current_cred(); 25501da177e4SLinus Torvalds 25511da177e4SLinus Torvalds if (flags & MS_REMOUNT) 255288e67f3bSDavid Howells return superblock_has_perm(cred, path->mnt->mnt_sb, 25531da177e4SLinus Torvalds FILESYSTEM__REMOUNT, NULL); 25541da177e4SLinus Torvalds else 255588e67f3bSDavid Howells return dentry_has_perm(cred, path->mnt, path->dentry, 25561da177e4SLinus Torvalds FILE__MOUNTON); 25571da177e4SLinus Torvalds } 25581da177e4SLinus Torvalds 25591da177e4SLinus Torvalds static int selinux_umount(struct vfsmount *mnt, int flags) 25601da177e4SLinus Torvalds { 256188e67f3bSDavid Howells const struct cred *cred = current_cred(); 25621da177e4SLinus Torvalds 256388e67f3bSDavid Howells return superblock_has_perm(cred, mnt->mnt_sb, 25641da177e4SLinus Torvalds FILESYSTEM__UNMOUNT, NULL); 25651da177e4SLinus Torvalds } 25661da177e4SLinus Torvalds 25671da177e4SLinus Torvalds /* inode security operations */ 25681da177e4SLinus Torvalds 25691da177e4SLinus Torvalds static int selinux_inode_alloc_security(struct inode *inode) 25701da177e4SLinus Torvalds { 25711da177e4SLinus Torvalds return inode_alloc_security(inode); 25721da177e4SLinus Torvalds } 25731da177e4SLinus Torvalds 25741da177e4SLinus Torvalds static void selinux_inode_free_security(struct inode *inode) 25751da177e4SLinus Torvalds { 25761da177e4SLinus Torvalds inode_free_security(inode); 25771da177e4SLinus Torvalds } 25781da177e4SLinus Torvalds 25795e41ff9eSStephen Smalley static int selinux_inode_init_security(struct inode *inode, struct inode *dir, 25805e41ff9eSStephen Smalley char **name, void **value, 25815e41ff9eSStephen Smalley size_t *len) 25825e41ff9eSStephen Smalley { 2583275bb41eSDavid Howells const struct cred *cred = current_cred(); 2584275bb41eSDavid Howells const struct task_security_struct *tsec = cred->security; 25855e41ff9eSStephen Smalley struct inode_security_struct *dsec; 25865e41ff9eSStephen Smalley struct superblock_security_struct *sbsec; 2587275bb41eSDavid Howells u32 sid, newsid, clen; 25885e41ff9eSStephen Smalley int rc; 2589570bc1c2SStephen Smalley char *namep = NULL, *context; 25905e41ff9eSStephen Smalley 25915e41ff9eSStephen Smalley dsec = dir->i_security; 25925e41ff9eSStephen Smalley sbsec = dir->i_sb->s_security; 25935e41ff9eSStephen Smalley 2594275bb41eSDavid Howells sid = tsec->sid; 25955e41ff9eSStephen Smalley newsid = tsec->create_sid; 2596275bb41eSDavid Howells 2597cd89596fSDavid P. Quigley if (!newsid || !(sbsec->flags & SE_SBLABELSUPP)) { 2598275bb41eSDavid Howells rc = security_transition_sid(sid, dsec->sid, 25995e41ff9eSStephen Smalley inode_mode_to_security_class(inode->i_mode), 26005e41ff9eSStephen Smalley &newsid); 26015e41ff9eSStephen Smalley if (rc) { 26025e41ff9eSStephen Smalley printk(KERN_WARNING "%s: " 26035e41ff9eSStephen Smalley "security_transition_sid failed, rc=%d (dev=%s " 26045e41ff9eSStephen Smalley "ino=%ld)\n", 2605dd6f953aSHarvey Harrison __func__, 26065e41ff9eSStephen Smalley -rc, inode->i_sb->s_id, inode->i_ino); 26075e41ff9eSStephen Smalley return rc; 26085e41ff9eSStephen Smalley } 26095e41ff9eSStephen Smalley } 26105e41ff9eSStephen Smalley 2611296fddf7SEric Paris /* Possibly defer initialization to selinux_complete_init. */ 26120d90a7ecSDavid P. Quigley if (sbsec->flags & SE_SBINITIALIZED) { 2613296fddf7SEric Paris struct inode_security_struct *isec = inode->i_security; 2614296fddf7SEric Paris isec->sclass = inode_mode_to_security_class(inode->i_mode); 2615296fddf7SEric Paris isec->sid = newsid; 2616296fddf7SEric Paris isec->initialized = 1; 2617296fddf7SEric Paris } 26185e41ff9eSStephen Smalley 2619cd89596fSDavid P. Quigley if (!ss_initialized || !(sbsec->flags & SE_SBLABELSUPP)) 262025a74f3bSStephen Smalley return -EOPNOTSUPP; 262125a74f3bSStephen Smalley 2622570bc1c2SStephen Smalley if (name) { 2623a02fe132SJosef Bacik namep = kstrdup(XATTR_SELINUX_SUFFIX, GFP_NOFS); 26245e41ff9eSStephen Smalley if (!namep) 26255e41ff9eSStephen Smalley return -ENOMEM; 26265e41ff9eSStephen Smalley *name = namep; 2627570bc1c2SStephen Smalley } 26285e41ff9eSStephen Smalley 2629570bc1c2SStephen Smalley if (value && len) { 263012b29f34SStephen Smalley rc = security_sid_to_context_force(newsid, &context, &clen); 26315e41ff9eSStephen Smalley if (rc) { 26325e41ff9eSStephen Smalley kfree(namep); 26335e41ff9eSStephen Smalley return rc; 26345e41ff9eSStephen Smalley } 26355e41ff9eSStephen Smalley *value = context; 2636570bc1c2SStephen Smalley *len = clen; 2637570bc1c2SStephen Smalley } 26385e41ff9eSStephen Smalley 26395e41ff9eSStephen Smalley return 0; 26405e41ff9eSStephen Smalley } 26415e41ff9eSStephen Smalley 26421da177e4SLinus Torvalds static int selinux_inode_create(struct inode *dir, struct dentry *dentry, int mask) 26431da177e4SLinus Torvalds { 26441da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_FILE); 26451da177e4SLinus Torvalds } 26461da177e4SLinus Torvalds 26471da177e4SLinus Torvalds static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 26481da177e4SLinus Torvalds { 26491da177e4SLinus Torvalds return may_link(dir, old_dentry, MAY_LINK); 26501da177e4SLinus Torvalds } 26511da177e4SLinus Torvalds 26521da177e4SLinus Torvalds static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry) 26531da177e4SLinus Torvalds { 26541da177e4SLinus Torvalds return may_link(dir, dentry, MAY_UNLINK); 26551da177e4SLinus Torvalds } 26561da177e4SLinus Torvalds 26571da177e4SLinus Torvalds static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name) 26581da177e4SLinus Torvalds { 26591da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_LNK_FILE); 26601da177e4SLinus Torvalds } 26611da177e4SLinus Torvalds 26621da177e4SLinus Torvalds static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, int mask) 26631da177e4SLinus Torvalds { 26641da177e4SLinus Torvalds return may_create(dir, dentry, SECCLASS_DIR); 26651da177e4SLinus Torvalds } 26661da177e4SLinus Torvalds 26671da177e4SLinus Torvalds static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry) 26681da177e4SLinus Torvalds { 26691da177e4SLinus Torvalds return may_link(dir, dentry, MAY_RMDIR); 26701da177e4SLinus Torvalds } 26711da177e4SLinus Torvalds 26721da177e4SLinus Torvalds static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 26731da177e4SLinus Torvalds { 26741da177e4SLinus Torvalds return may_create(dir, dentry, inode_mode_to_security_class(mode)); 26751da177e4SLinus Torvalds } 26761da177e4SLinus Torvalds 26771da177e4SLinus Torvalds static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry, 26781da177e4SLinus Torvalds struct inode *new_inode, struct dentry *new_dentry) 26791da177e4SLinus Torvalds { 26801da177e4SLinus Torvalds return may_rename(old_inode, old_dentry, new_inode, new_dentry); 26811da177e4SLinus Torvalds } 26821da177e4SLinus Torvalds 26831da177e4SLinus Torvalds static int selinux_inode_readlink(struct dentry *dentry) 26841da177e4SLinus Torvalds { 268588e67f3bSDavid Howells const struct cred *cred = current_cred(); 268688e67f3bSDavid Howells 268788e67f3bSDavid Howells return dentry_has_perm(cred, NULL, dentry, FILE__READ); 26881da177e4SLinus Torvalds } 26891da177e4SLinus Torvalds 26901da177e4SLinus Torvalds static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata) 26911da177e4SLinus Torvalds { 269288e67f3bSDavid Howells const struct cred *cred = current_cred(); 26931da177e4SLinus Torvalds 269488e67f3bSDavid Howells return dentry_has_perm(cred, NULL, dentry, FILE__READ); 26951da177e4SLinus Torvalds } 26961da177e4SLinus Torvalds 2697b77b0646SAl Viro static int selinux_inode_permission(struct inode *inode, int mask) 26981da177e4SLinus Torvalds { 269988e67f3bSDavid Howells const struct cred *cred = current_cred(); 27001da177e4SLinus Torvalds 27011da177e4SLinus Torvalds if (!mask) { 27021da177e4SLinus Torvalds /* No permission to check. Existence test. */ 27031da177e4SLinus Torvalds return 0; 27041da177e4SLinus Torvalds } 27051da177e4SLinus Torvalds 270688e67f3bSDavid Howells return inode_has_perm(cred, inode, 27078b6a5a37SEric Paris file_mask_to_av(inode->i_mode, mask), NULL); 27081da177e4SLinus Torvalds } 27091da177e4SLinus Torvalds 27101da177e4SLinus Torvalds static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr) 27111da177e4SLinus Torvalds { 271288e67f3bSDavid Howells const struct cred *cred = current_cred(); 27131da177e4SLinus Torvalds 27141da177e4SLinus Torvalds if (iattr->ia_valid & ATTR_FORCE) 27151da177e4SLinus Torvalds return 0; 27161da177e4SLinus Torvalds 27171da177e4SLinus Torvalds if (iattr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | 27181da177e4SLinus Torvalds ATTR_ATIME_SET | ATTR_MTIME_SET)) 271988e67f3bSDavid Howells return dentry_has_perm(cred, NULL, dentry, FILE__SETATTR); 27201da177e4SLinus Torvalds 272188e67f3bSDavid Howells return dentry_has_perm(cred, NULL, dentry, FILE__WRITE); 27221da177e4SLinus Torvalds } 27231da177e4SLinus Torvalds 27241da177e4SLinus Torvalds static int selinux_inode_getattr(struct vfsmount *mnt, struct dentry *dentry) 27251da177e4SLinus Torvalds { 272688e67f3bSDavid Howells const struct cred *cred = current_cred(); 272788e67f3bSDavid Howells 272888e67f3bSDavid Howells return dentry_has_perm(cred, mnt, dentry, FILE__GETATTR); 27291da177e4SLinus Torvalds } 27301da177e4SLinus Torvalds 27318f0cfa52SDavid Howells static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name) 2732b5376771SSerge E. Hallyn { 273388e67f3bSDavid Howells const struct cred *cred = current_cred(); 273488e67f3bSDavid Howells 2735b5376771SSerge E. Hallyn if (!strncmp(name, XATTR_SECURITY_PREFIX, 2736b5376771SSerge E. Hallyn sizeof XATTR_SECURITY_PREFIX - 1)) { 2737b5376771SSerge E. Hallyn if (!strcmp(name, XATTR_NAME_CAPS)) { 2738b5376771SSerge E. Hallyn if (!capable(CAP_SETFCAP)) 2739b5376771SSerge E. Hallyn return -EPERM; 2740b5376771SSerge E. Hallyn } else if (!capable(CAP_SYS_ADMIN)) { 2741b5376771SSerge E. Hallyn /* A different attribute in the security namespace. 2742b5376771SSerge E. Hallyn Restrict to administrator. */ 2743b5376771SSerge E. Hallyn return -EPERM; 2744b5376771SSerge E. Hallyn } 2745b5376771SSerge E. Hallyn } 2746b5376771SSerge E. Hallyn 2747b5376771SSerge E. Hallyn /* Not an attribute we recognize, so just check the 2748b5376771SSerge E. Hallyn ordinary setattr permission. */ 274988e67f3bSDavid Howells return dentry_has_perm(cred, NULL, dentry, FILE__SETATTR); 2750b5376771SSerge E. Hallyn } 2751b5376771SSerge E. Hallyn 27528f0cfa52SDavid Howells static int selinux_inode_setxattr(struct dentry *dentry, const char *name, 27538f0cfa52SDavid Howells const void *value, size_t size, int flags) 27541da177e4SLinus Torvalds { 27551da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 27561da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 27571da177e4SLinus Torvalds struct superblock_security_struct *sbsec; 2758*2bf49690SThomas Liu struct common_audit_data ad; 2759275bb41eSDavid Howells u32 newsid, sid = current_sid(); 27601da177e4SLinus Torvalds int rc = 0; 27611da177e4SLinus Torvalds 2762b5376771SSerge E. Hallyn if (strcmp(name, XATTR_NAME_SELINUX)) 2763b5376771SSerge E. Hallyn return selinux_inode_setotherxattr(dentry, name); 27641da177e4SLinus Torvalds 27651da177e4SLinus Torvalds sbsec = inode->i_sb->s_security; 2766cd89596fSDavid P. Quigley if (!(sbsec->flags & SE_SBLABELSUPP)) 27671da177e4SLinus Torvalds return -EOPNOTSUPP; 27681da177e4SLinus Torvalds 27693bd858abSSatyam Sharma if (!is_owner_or_cap(inode)) 27701da177e4SLinus Torvalds return -EPERM; 27711da177e4SLinus Torvalds 2772*2bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, FS); 277344707fdfSJan Blunck ad.u.fs.path.dentry = dentry; 27741da177e4SLinus Torvalds 2775275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, isec->sclass, 27761da177e4SLinus Torvalds FILE__RELABELFROM, &ad); 27771da177e4SLinus Torvalds if (rc) 27781da177e4SLinus Torvalds return rc; 27791da177e4SLinus Torvalds 27801da177e4SLinus Torvalds rc = security_context_to_sid(value, size, &newsid); 278112b29f34SStephen Smalley if (rc == -EINVAL) { 278212b29f34SStephen Smalley if (!capable(CAP_MAC_ADMIN)) 278312b29f34SStephen Smalley return rc; 278412b29f34SStephen Smalley rc = security_context_to_sid_force(value, size, &newsid); 278512b29f34SStephen Smalley } 27861da177e4SLinus Torvalds if (rc) 27871da177e4SLinus Torvalds return rc; 27881da177e4SLinus Torvalds 2789275bb41eSDavid Howells rc = avc_has_perm(sid, newsid, isec->sclass, 27901da177e4SLinus Torvalds FILE__RELABELTO, &ad); 27911da177e4SLinus Torvalds if (rc) 27921da177e4SLinus Torvalds return rc; 27931da177e4SLinus Torvalds 2794275bb41eSDavid Howells rc = security_validate_transition(isec->sid, newsid, sid, 27951da177e4SLinus Torvalds isec->sclass); 27961da177e4SLinus Torvalds if (rc) 27971da177e4SLinus Torvalds return rc; 27981da177e4SLinus Torvalds 27991da177e4SLinus Torvalds return avc_has_perm(newsid, 28001da177e4SLinus Torvalds sbsec->sid, 28011da177e4SLinus Torvalds SECCLASS_FILESYSTEM, 28021da177e4SLinus Torvalds FILESYSTEM__ASSOCIATE, 28031da177e4SLinus Torvalds &ad); 28041da177e4SLinus Torvalds } 28051da177e4SLinus Torvalds 28068f0cfa52SDavid Howells static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name, 28078f0cfa52SDavid Howells const void *value, size_t size, 28088f0cfa52SDavid Howells int flags) 28091da177e4SLinus Torvalds { 28101da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 28111da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 28121da177e4SLinus Torvalds u32 newsid; 28131da177e4SLinus Torvalds int rc; 28141da177e4SLinus Torvalds 28151da177e4SLinus Torvalds if (strcmp(name, XATTR_NAME_SELINUX)) { 28161da177e4SLinus Torvalds /* Not an attribute we recognize, so nothing to do. */ 28171da177e4SLinus Torvalds return; 28181da177e4SLinus Torvalds } 28191da177e4SLinus Torvalds 282012b29f34SStephen Smalley rc = security_context_to_sid_force(value, size, &newsid); 28211da177e4SLinus Torvalds if (rc) { 282212b29f34SStephen Smalley printk(KERN_ERR "SELinux: unable to map context to SID" 282312b29f34SStephen Smalley "for (%s, %lu), rc=%d\n", 282412b29f34SStephen Smalley inode->i_sb->s_id, inode->i_ino, -rc); 28251da177e4SLinus Torvalds return; 28261da177e4SLinus Torvalds } 28271da177e4SLinus Torvalds 28281da177e4SLinus Torvalds isec->sid = newsid; 28291da177e4SLinus Torvalds return; 28301da177e4SLinus Torvalds } 28311da177e4SLinus Torvalds 28328f0cfa52SDavid Howells static int selinux_inode_getxattr(struct dentry *dentry, const char *name) 28331da177e4SLinus Torvalds { 283488e67f3bSDavid Howells const struct cred *cred = current_cred(); 283588e67f3bSDavid Howells 283688e67f3bSDavid Howells return dentry_has_perm(cred, NULL, dentry, FILE__GETATTR); 28371da177e4SLinus Torvalds } 28381da177e4SLinus Torvalds 28391da177e4SLinus Torvalds static int selinux_inode_listxattr(struct dentry *dentry) 28401da177e4SLinus Torvalds { 284188e67f3bSDavid Howells const struct cred *cred = current_cred(); 284288e67f3bSDavid Howells 284388e67f3bSDavid Howells return dentry_has_perm(cred, NULL, dentry, FILE__GETATTR); 28441da177e4SLinus Torvalds } 28451da177e4SLinus Torvalds 28468f0cfa52SDavid Howells static int selinux_inode_removexattr(struct dentry *dentry, const char *name) 28471da177e4SLinus Torvalds { 2848b5376771SSerge E. Hallyn if (strcmp(name, XATTR_NAME_SELINUX)) 2849b5376771SSerge E. Hallyn return selinux_inode_setotherxattr(dentry, name); 28501da177e4SLinus Torvalds 28511da177e4SLinus Torvalds /* No one is allowed to remove a SELinux security label. 28521da177e4SLinus Torvalds You can change the label, but all data must be labeled. */ 28531da177e4SLinus Torvalds return -EACCES; 28541da177e4SLinus Torvalds } 28551da177e4SLinus Torvalds 2856d381d8a9SJames Morris /* 2857abc69bb6SStephen Smalley * Copy the inode security context value to the user. 2858d381d8a9SJames Morris * 2859d381d8a9SJames Morris * Permission check is handled by selinux_inode_getxattr hook. 2860d381d8a9SJames Morris */ 286142492594SDavid P. Quigley static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void **buffer, bool alloc) 28621da177e4SLinus Torvalds { 286342492594SDavid P. Quigley u32 size; 286442492594SDavid P. Quigley int error; 286542492594SDavid P. Quigley char *context = NULL; 28661da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 28671da177e4SLinus Torvalds 28688c8570fbSDustin Kirkland if (strcmp(name, XATTR_SELINUX_SUFFIX)) 28698c8570fbSDustin Kirkland return -EOPNOTSUPP; 28701da177e4SLinus Torvalds 2871abc69bb6SStephen Smalley /* 2872abc69bb6SStephen Smalley * If the caller has CAP_MAC_ADMIN, then get the raw context 2873abc69bb6SStephen Smalley * value even if it is not defined by current policy; otherwise, 2874abc69bb6SStephen Smalley * use the in-core value under current policy. 2875abc69bb6SStephen Smalley * Use the non-auditing forms of the permission checks since 2876abc69bb6SStephen Smalley * getxattr may be called by unprivileged processes commonly 2877abc69bb6SStephen Smalley * and lack of permission just means that we fall back to the 2878abc69bb6SStephen Smalley * in-core context value, not a denial. 2879abc69bb6SStephen Smalley */ 28803699c53cSDavid Howells error = selinux_capable(current, current_cred(), CAP_MAC_ADMIN, 28813699c53cSDavid Howells SECURITY_CAP_NOAUDIT); 2882abc69bb6SStephen Smalley if (!error) 2883abc69bb6SStephen Smalley error = security_sid_to_context_force(isec->sid, &context, 2884abc69bb6SStephen Smalley &size); 2885abc69bb6SStephen Smalley else 288642492594SDavid P. Quigley error = security_sid_to_context(isec->sid, &context, &size); 288742492594SDavid P. Quigley if (error) 288842492594SDavid P. Quigley return error; 288942492594SDavid P. Quigley error = size; 289042492594SDavid P. Quigley if (alloc) { 289142492594SDavid P. Quigley *buffer = context; 289242492594SDavid P. Quigley goto out_nofree; 289342492594SDavid P. Quigley } 289442492594SDavid P. Quigley kfree(context); 289542492594SDavid P. Quigley out_nofree: 289642492594SDavid P. Quigley return error; 28971da177e4SLinus Torvalds } 28981da177e4SLinus Torvalds 28991da177e4SLinus Torvalds static int selinux_inode_setsecurity(struct inode *inode, const char *name, 29001da177e4SLinus Torvalds const void *value, size_t size, int flags) 29011da177e4SLinus Torvalds { 29021da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 29031da177e4SLinus Torvalds u32 newsid; 29041da177e4SLinus Torvalds int rc; 29051da177e4SLinus Torvalds 29061da177e4SLinus Torvalds if (strcmp(name, XATTR_SELINUX_SUFFIX)) 29071da177e4SLinus Torvalds return -EOPNOTSUPP; 29081da177e4SLinus Torvalds 29091da177e4SLinus Torvalds if (!value || !size) 29101da177e4SLinus Torvalds return -EACCES; 29111da177e4SLinus Torvalds 29121da177e4SLinus Torvalds rc = security_context_to_sid((void *)value, size, &newsid); 29131da177e4SLinus Torvalds if (rc) 29141da177e4SLinus Torvalds return rc; 29151da177e4SLinus Torvalds 29161da177e4SLinus Torvalds isec->sid = newsid; 29171da177e4SLinus Torvalds return 0; 29181da177e4SLinus Torvalds } 29191da177e4SLinus Torvalds 29201da177e4SLinus Torvalds static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size) 29211da177e4SLinus Torvalds { 29221da177e4SLinus Torvalds const int len = sizeof(XATTR_NAME_SELINUX); 29231da177e4SLinus Torvalds if (buffer && len <= buffer_size) 29241da177e4SLinus Torvalds memcpy(buffer, XATTR_NAME_SELINUX, len); 29251da177e4SLinus Torvalds return len; 29261da177e4SLinus Torvalds } 29271da177e4SLinus Torvalds 2928713a04aeSAhmed S. Darwish static void selinux_inode_getsecid(const struct inode *inode, u32 *secid) 2929713a04aeSAhmed S. Darwish { 2930713a04aeSAhmed S. Darwish struct inode_security_struct *isec = inode->i_security; 2931713a04aeSAhmed S. Darwish *secid = isec->sid; 2932713a04aeSAhmed S. Darwish } 2933713a04aeSAhmed S. Darwish 29341da177e4SLinus Torvalds /* file security operations */ 29351da177e4SLinus Torvalds 2936788e7dd4SYuichi Nakamura static int selinux_revalidate_file_permission(struct file *file, int mask) 29371da177e4SLinus Torvalds { 293888e67f3bSDavid Howells const struct cred *cred = current_cred(); 29393d5ff529SJosef Sipek struct inode *inode = file->f_path.dentry->d_inode; 29401da177e4SLinus Torvalds 29411da177e4SLinus Torvalds /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */ 29421da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE)) 29431da177e4SLinus Torvalds mask |= MAY_APPEND; 29441da177e4SLinus Torvalds 2945389fb800SPaul Moore return file_has_perm(cred, file, 29461da177e4SLinus Torvalds file_mask_to_av(inode->i_mode, mask)); 29471da177e4SLinus Torvalds } 29481da177e4SLinus Torvalds 2949788e7dd4SYuichi Nakamura static int selinux_file_permission(struct file *file, int mask) 2950788e7dd4SYuichi Nakamura { 295120dda18bSStephen Smalley struct inode *inode = file->f_path.dentry->d_inode; 295220dda18bSStephen Smalley struct file_security_struct *fsec = file->f_security; 295320dda18bSStephen Smalley struct inode_security_struct *isec = inode->i_security; 295420dda18bSStephen Smalley u32 sid = current_sid(); 295520dda18bSStephen Smalley 2956389fb800SPaul Moore if (!mask) 2957788e7dd4SYuichi Nakamura /* No permission to check. Existence test. */ 2958788e7dd4SYuichi Nakamura return 0; 2959788e7dd4SYuichi Nakamura 296020dda18bSStephen Smalley if (sid == fsec->sid && fsec->isid == isec->sid && 296120dda18bSStephen Smalley fsec->pseqno == avc_policy_seqno()) 296220dda18bSStephen Smalley /* No change since dentry_open check. */ 296320dda18bSStephen Smalley return 0; 296420dda18bSStephen Smalley 2965788e7dd4SYuichi Nakamura return selinux_revalidate_file_permission(file, mask); 2966788e7dd4SYuichi Nakamura } 2967788e7dd4SYuichi Nakamura 29681da177e4SLinus Torvalds static int selinux_file_alloc_security(struct file *file) 29691da177e4SLinus Torvalds { 29701da177e4SLinus Torvalds return file_alloc_security(file); 29711da177e4SLinus Torvalds } 29721da177e4SLinus Torvalds 29731da177e4SLinus Torvalds static void selinux_file_free_security(struct file *file) 29741da177e4SLinus Torvalds { 29751da177e4SLinus Torvalds file_free_security(file); 29761da177e4SLinus Torvalds } 29771da177e4SLinus Torvalds 29781da177e4SLinus Torvalds static int selinux_file_ioctl(struct file *file, unsigned int cmd, 29791da177e4SLinus Torvalds unsigned long arg) 29801da177e4SLinus Torvalds { 298188e67f3bSDavid Howells const struct cred *cred = current_cred(); 2982242631c4SStephen Smalley u32 av = 0; 29831da177e4SLinus Torvalds 2984242631c4SStephen Smalley if (_IOC_DIR(cmd) & _IOC_WRITE) 2985242631c4SStephen Smalley av |= FILE__WRITE; 2986242631c4SStephen Smalley if (_IOC_DIR(cmd) & _IOC_READ) 2987242631c4SStephen Smalley av |= FILE__READ; 2988242631c4SStephen Smalley if (!av) 2989242631c4SStephen Smalley av = FILE__IOCTL; 29901da177e4SLinus Torvalds 299188e67f3bSDavid Howells return file_has_perm(cred, file, av); 29921da177e4SLinus Torvalds } 29931da177e4SLinus Torvalds 29941da177e4SLinus Torvalds static int file_map_prot_check(struct file *file, unsigned long prot, int shared) 29951da177e4SLinus Torvalds { 299688e67f3bSDavid Howells const struct cred *cred = current_cred(); 2997d84f4f99SDavid Howells int rc = 0; 299888e67f3bSDavid Howells 29991da177e4SLinus Torvalds #ifndef CONFIG_PPC32 30001da177e4SLinus Torvalds if ((prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) { 30011da177e4SLinus Torvalds /* 30021da177e4SLinus Torvalds * We are making executable an anonymous mapping or a 30031da177e4SLinus Torvalds * private file mapping that will also be writable. 30041da177e4SLinus Torvalds * This has an additional check. 30051da177e4SLinus Torvalds */ 3006d84f4f99SDavid Howells rc = cred_has_perm(cred, cred, PROCESS__EXECMEM); 30071da177e4SLinus Torvalds if (rc) 3008d84f4f99SDavid Howells goto error; 30091da177e4SLinus Torvalds } 30101da177e4SLinus Torvalds #endif 30111da177e4SLinus Torvalds 30121da177e4SLinus Torvalds if (file) { 30131da177e4SLinus Torvalds /* read access is always possible with a mapping */ 30141da177e4SLinus Torvalds u32 av = FILE__READ; 30151da177e4SLinus Torvalds 30161da177e4SLinus Torvalds /* write access only matters if the mapping is shared */ 30171da177e4SLinus Torvalds if (shared && (prot & PROT_WRITE)) 30181da177e4SLinus Torvalds av |= FILE__WRITE; 30191da177e4SLinus Torvalds 30201da177e4SLinus Torvalds if (prot & PROT_EXEC) 30211da177e4SLinus Torvalds av |= FILE__EXECUTE; 30221da177e4SLinus Torvalds 302388e67f3bSDavid Howells return file_has_perm(cred, file, av); 30241da177e4SLinus Torvalds } 3025d84f4f99SDavid Howells 3026d84f4f99SDavid Howells error: 3027d84f4f99SDavid Howells return rc; 30281da177e4SLinus Torvalds } 30291da177e4SLinus Torvalds 30301da177e4SLinus Torvalds static int selinux_file_mmap(struct file *file, unsigned long reqprot, 3031ed032189SEric Paris unsigned long prot, unsigned long flags, 3032ed032189SEric Paris unsigned long addr, unsigned long addr_only) 30331da177e4SLinus Torvalds { 3034ed032189SEric Paris int rc = 0; 3035275bb41eSDavid Howells u32 sid = current_sid(); 30361da177e4SLinus Torvalds 303784336d1aSEric Paris /* 303884336d1aSEric Paris * notice that we are intentionally putting the SELinux check before 303984336d1aSEric Paris * the secondary cap_file_mmap check. This is such a likely attempt 304084336d1aSEric Paris * at bad behaviour/exploit that we always want to get the AVC, even 304184336d1aSEric Paris * if DAC would have also denied the operation. 304284336d1aSEric Paris */ 3043a2551df7SEric Paris if (addr < CONFIG_LSM_MMAP_MIN_ADDR) { 3044ed032189SEric Paris rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT, 3045ed032189SEric Paris MEMPROTECT__MMAP_ZERO, NULL); 304684336d1aSEric Paris if (rc) 304784336d1aSEric Paris return rc; 304884336d1aSEric Paris } 304984336d1aSEric Paris 305084336d1aSEric Paris /* do DAC check on address space usage */ 305184336d1aSEric Paris rc = cap_file_mmap(file, reqprot, prot, flags, addr, addr_only); 3052ed032189SEric Paris if (rc || addr_only) 30531da177e4SLinus Torvalds return rc; 30541da177e4SLinus Torvalds 30551da177e4SLinus Torvalds if (selinux_checkreqprot) 30561da177e4SLinus Torvalds prot = reqprot; 30571da177e4SLinus Torvalds 30581da177e4SLinus Torvalds return file_map_prot_check(file, prot, 30591da177e4SLinus Torvalds (flags & MAP_TYPE) == MAP_SHARED); 30601da177e4SLinus Torvalds } 30611da177e4SLinus Torvalds 30621da177e4SLinus Torvalds static int selinux_file_mprotect(struct vm_area_struct *vma, 30631da177e4SLinus Torvalds unsigned long reqprot, 30641da177e4SLinus Torvalds unsigned long prot) 30651da177e4SLinus Torvalds { 306688e67f3bSDavid Howells const struct cred *cred = current_cred(); 30671da177e4SLinus Torvalds 30681da177e4SLinus Torvalds if (selinux_checkreqprot) 30691da177e4SLinus Torvalds prot = reqprot; 30701da177e4SLinus Torvalds 30711da177e4SLinus Torvalds #ifndef CONFIG_PPC32 3072db4c9641SStephen Smalley if ((prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) { 3073d541bbeeSJames Morris int rc = 0; 3074db4c9641SStephen Smalley if (vma->vm_start >= vma->vm_mm->start_brk && 3075db4c9641SStephen Smalley vma->vm_end <= vma->vm_mm->brk) { 3076d84f4f99SDavid Howells rc = cred_has_perm(cred, cred, PROCESS__EXECHEAP); 3077db4c9641SStephen Smalley } else if (!vma->vm_file && 30786b992197SLorenzo Hernandez García-Hierro vma->vm_start <= vma->vm_mm->start_stack && 30796b992197SLorenzo Hernandez García-Hierro vma->vm_end >= vma->vm_mm->start_stack) { 30803b11a1deSDavid Howells rc = current_has_perm(current, PROCESS__EXECSTACK); 3081db4c9641SStephen Smalley } else if (vma->vm_file && vma->anon_vma) { 3082db4c9641SStephen Smalley /* 3083db4c9641SStephen Smalley * We are making executable a file mapping that has 3084db4c9641SStephen Smalley * had some COW done. Since pages might have been 3085db4c9641SStephen Smalley * written, check ability to execute the possibly 3086db4c9641SStephen Smalley * modified content. This typically should only 3087db4c9641SStephen Smalley * occur for text relocations. 3088db4c9641SStephen Smalley */ 3089d84f4f99SDavid Howells rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD); 3090db4c9641SStephen Smalley } 30916b992197SLorenzo Hernandez García-Hierro if (rc) 30926b992197SLorenzo Hernandez García-Hierro return rc; 30936b992197SLorenzo Hernandez García-Hierro } 30941da177e4SLinus Torvalds #endif 30951da177e4SLinus Torvalds 30961da177e4SLinus Torvalds return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED); 30971da177e4SLinus Torvalds } 30981da177e4SLinus Torvalds 30991da177e4SLinus Torvalds static int selinux_file_lock(struct file *file, unsigned int cmd) 31001da177e4SLinus Torvalds { 310188e67f3bSDavid Howells const struct cred *cred = current_cred(); 310288e67f3bSDavid Howells 310388e67f3bSDavid Howells return file_has_perm(cred, file, FILE__LOCK); 31041da177e4SLinus Torvalds } 31051da177e4SLinus Torvalds 31061da177e4SLinus Torvalds static int selinux_file_fcntl(struct file *file, unsigned int cmd, 31071da177e4SLinus Torvalds unsigned long arg) 31081da177e4SLinus Torvalds { 310988e67f3bSDavid Howells const struct cred *cred = current_cred(); 31101da177e4SLinus Torvalds int err = 0; 31111da177e4SLinus Torvalds 31121da177e4SLinus Torvalds switch (cmd) { 31131da177e4SLinus Torvalds case F_SETFL: 31143d5ff529SJosef Sipek if (!file->f_path.dentry || !file->f_path.dentry->d_inode) { 31151da177e4SLinus Torvalds err = -EINVAL; 31161da177e4SLinus Torvalds break; 31171da177e4SLinus Torvalds } 31181da177e4SLinus Torvalds 31191da177e4SLinus Torvalds if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) { 312088e67f3bSDavid Howells err = file_has_perm(cred, file, FILE__WRITE); 31211da177e4SLinus Torvalds break; 31221da177e4SLinus Torvalds } 31231da177e4SLinus Torvalds /* fall through */ 31241da177e4SLinus Torvalds case F_SETOWN: 31251da177e4SLinus Torvalds case F_SETSIG: 31261da177e4SLinus Torvalds case F_GETFL: 31271da177e4SLinus Torvalds case F_GETOWN: 31281da177e4SLinus Torvalds case F_GETSIG: 31291da177e4SLinus Torvalds /* Just check FD__USE permission */ 313088e67f3bSDavid Howells err = file_has_perm(cred, file, 0); 31311da177e4SLinus Torvalds break; 31321da177e4SLinus Torvalds case F_GETLK: 31331da177e4SLinus Torvalds case F_SETLK: 31341da177e4SLinus Torvalds case F_SETLKW: 31351da177e4SLinus Torvalds #if BITS_PER_LONG == 32 31361da177e4SLinus Torvalds case F_GETLK64: 31371da177e4SLinus Torvalds case F_SETLK64: 31381da177e4SLinus Torvalds case F_SETLKW64: 31391da177e4SLinus Torvalds #endif 31403d5ff529SJosef Sipek if (!file->f_path.dentry || !file->f_path.dentry->d_inode) { 31411da177e4SLinus Torvalds err = -EINVAL; 31421da177e4SLinus Torvalds break; 31431da177e4SLinus Torvalds } 314488e67f3bSDavid Howells err = file_has_perm(cred, file, FILE__LOCK); 31451da177e4SLinus Torvalds break; 31461da177e4SLinus Torvalds } 31471da177e4SLinus Torvalds 31481da177e4SLinus Torvalds return err; 31491da177e4SLinus Torvalds } 31501da177e4SLinus Torvalds 31511da177e4SLinus Torvalds static int selinux_file_set_fowner(struct file *file) 31521da177e4SLinus Torvalds { 31531da177e4SLinus Torvalds struct file_security_struct *fsec; 31541da177e4SLinus Torvalds 31551da177e4SLinus Torvalds fsec = file->f_security; 3156275bb41eSDavid Howells fsec->fown_sid = current_sid(); 31571da177e4SLinus Torvalds 31581da177e4SLinus Torvalds return 0; 31591da177e4SLinus Torvalds } 31601da177e4SLinus Torvalds 31611da177e4SLinus Torvalds static int selinux_file_send_sigiotask(struct task_struct *tsk, 31621da177e4SLinus Torvalds struct fown_struct *fown, int signum) 31631da177e4SLinus Torvalds { 31641da177e4SLinus Torvalds struct file *file; 316565c90bcaSStephen Smalley u32 sid = task_sid(tsk); 31661da177e4SLinus Torvalds u32 perm; 31671da177e4SLinus Torvalds struct file_security_struct *fsec; 31681da177e4SLinus Torvalds 31691da177e4SLinus Torvalds /* struct fown_struct is never outside the context of a struct file */ 3170b385a144SRobert P. J. Day file = container_of(fown, struct file, f_owner); 31711da177e4SLinus Torvalds 31721da177e4SLinus Torvalds fsec = file->f_security; 31731da177e4SLinus Torvalds 31741da177e4SLinus Torvalds if (!signum) 31751da177e4SLinus Torvalds perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */ 31761da177e4SLinus Torvalds else 31771da177e4SLinus Torvalds perm = signal_to_av(signum); 31781da177e4SLinus Torvalds 3179275bb41eSDavid Howells return avc_has_perm(fsec->fown_sid, sid, 31801da177e4SLinus Torvalds SECCLASS_PROCESS, perm, NULL); 31811da177e4SLinus Torvalds } 31821da177e4SLinus Torvalds 31831da177e4SLinus Torvalds static int selinux_file_receive(struct file *file) 31841da177e4SLinus Torvalds { 318588e67f3bSDavid Howells const struct cred *cred = current_cred(); 318688e67f3bSDavid Howells 318788e67f3bSDavid Howells return file_has_perm(cred, file, file_to_av(file)); 31881da177e4SLinus Torvalds } 31891da177e4SLinus Torvalds 3190745ca247SDavid Howells static int selinux_dentry_open(struct file *file, const struct cred *cred) 3191788e7dd4SYuichi Nakamura { 3192788e7dd4SYuichi Nakamura struct file_security_struct *fsec; 3193788e7dd4SYuichi Nakamura struct inode *inode; 3194788e7dd4SYuichi Nakamura struct inode_security_struct *isec; 3195d84f4f99SDavid Howells 3196788e7dd4SYuichi Nakamura inode = file->f_path.dentry->d_inode; 3197788e7dd4SYuichi Nakamura fsec = file->f_security; 3198788e7dd4SYuichi Nakamura isec = inode->i_security; 3199788e7dd4SYuichi Nakamura /* 3200788e7dd4SYuichi Nakamura * Save inode label and policy sequence number 3201788e7dd4SYuichi Nakamura * at open-time so that selinux_file_permission 3202788e7dd4SYuichi Nakamura * can determine whether revalidation is necessary. 3203788e7dd4SYuichi Nakamura * Task label is already saved in the file security 3204788e7dd4SYuichi Nakamura * struct as its SID. 3205788e7dd4SYuichi Nakamura */ 3206788e7dd4SYuichi Nakamura fsec->isid = isec->sid; 3207788e7dd4SYuichi Nakamura fsec->pseqno = avc_policy_seqno(); 3208788e7dd4SYuichi Nakamura /* 3209788e7dd4SYuichi Nakamura * Since the inode label or policy seqno may have changed 3210788e7dd4SYuichi Nakamura * between the selinux_inode_permission check and the saving 3211788e7dd4SYuichi Nakamura * of state above, recheck that access is still permitted. 3212788e7dd4SYuichi Nakamura * Otherwise, access might never be revalidated against the 3213788e7dd4SYuichi Nakamura * new inode label or new policy. 3214788e7dd4SYuichi Nakamura * This check is not redundant - do not remove. 3215788e7dd4SYuichi Nakamura */ 321688e67f3bSDavid Howells return inode_has_perm(cred, inode, open_file_to_av(file), NULL); 3217788e7dd4SYuichi Nakamura } 3218788e7dd4SYuichi Nakamura 32191da177e4SLinus Torvalds /* task security operations */ 32201da177e4SLinus Torvalds 32211da177e4SLinus Torvalds static int selinux_task_create(unsigned long clone_flags) 32221da177e4SLinus Torvalds { 32233b11a1deSDavid Howells return current_has_perm(current, PROCESS__FORK); 32241da177e4SLinus Torvalds } 32251da177e4SLinus Torvalds 3226f1752eecSDavid Howells /* 3227f1752eecSDavid Howells * detach and free the LSM part of a set of credentials 3228f1752eecSDavid Howells */ 3229f1752eecSDavid Howells static void selinux_cred_free(struct cred *cred) 32301da177e4SLinus Torvalds { 3231f1752eecSDavid Howells struct task_security_struct *tsec = cred->security; 3232f1752eecSDavid Howells cred->security = NULL; 3233f1752eecSDavid Howells kfree(tsec); 32341da177e4SLinus Torvalds } 32351da177e4SLinus Torvalds 3236d84f4f99SDavid Howells /* 3237d84f4f99SDavid Howells * prepare a new set of credentials for modification 3238d84f4f99SDavid Howells */ 3239d84f4f99SDavid Howells static int selinux_cred_prepare(struct cred *new, const struct cred *old, 3240d84f4f99SDavid Howells gfp_t gfp) 3241d84f4f99SDavid Howells { 3242d84f4f99SDavid Howells const struct task_security_struct *old_tsec; 3243d84f4f99SDavid Howells struct task_security_struct *tsec; 3244d84f4f99SDavid Howells 3245d84f4f99SDavid Howells old_tsec = old->security; 3246d84f4f99SDavid Howells 3247d84f4f99SDavid Howells tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp); 3248d84f4f99SDavid Howells if (!tsec) 3249d84f4f99SDavid Howells return -ENOMEM; 3250d84f4f99SDavid Howells 3251d84f4f99SDavid Howells new->security = tsec; 3252d84f4f99SDavid Howells return 0; 3253d84f4f99SDavid Howells } 3254d84f4f99SDavid Howells 3255d84f4f99SDavid Howells /* 32563a3b7ce9SDavid Howells * set the security data for a kernel service 32573a3b7ce9SDavid Howells * - all the creation contexts are set to unlabelled 32583a3b7ce9SDavid Howells */ 32593a3b7ce9SDavid Howells static int selinux_kernel_act_as(struct cred *new, u32 secid) 32603a3b7ce9SDavid Howells { 32613a3b7ce9SDavid Howells struct task_security_struct *tsec = new->security; 32623a3b7ce9SDavid Howells u32 sid = current_sid(); 32633a3b7ce9SDavid Howells int ret; 32643a3b7ce9SDavid Howells 32653a3b7ce9SDavid Howells ret = avc_has_perm(sid, secid, 32663a3b7ce9SDavid Howells SECCLASS_KERNEL_SERVICE, 32673a3b7ce9SDavid Howells KERNEL_SERVICE__USE_AS_OVERRIDE, 32683a3b7ce9SDavid Howells NULL); 32693a3b7ce9SDavid Howells if (ret == 0) { 32703a3b7ce9SDavid Howells tsec->sid = secid; 32713a3b7ce9SDavid Howells tsec->create_sid = 0; 32723a3b7ce9SDavid Howells tsec->keycreate_sid = 0; 32733a3b7ce9SDavid Howells tsec->sockcreate_sid = 0; 32743a3b7ce9SDavid Howells } 32753a3b7ce9SDavid Howells return ret; 32763a3b7ce9SDavid Howells } 32773a3b7ce9SDavid Howells 32783a3b7ce9SDavid Howells /* 32793a3b7ce9SDavid Howells * set the file creation context in a security record to the same as the 32803a3b7ce9SDavid Howells * objective context of the specified inode 32813a3b7ce9SDavid Howells */ 32823a3b7ce9SDavid Howells static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode) 32833a3b7ce9SDavid Howells { 32843a3b7ce9SDavid Howells struct inode_security_struct *isec = inode->i_security; 32853a3b7ce9SDavid Howells struct task_security_struct *tsec = new->security; 32863a3b7ce9SDavid Howells u32 sid = current_sid(); 32873a3b7ce9SDavid Howells int ret; 32883a3b7ce9SDavid Howells 32893a3b7ce9SDavid Howells ret = avc_has_perm(sid, isec->sid, 32903a3b7ce9SDavid Howells SECCLASS_KERNEL_SERVICE, 32913a3b7ce9SDavid Howells KERNEL_SERVICE__CREATE_FILES_AS, 32923a3b7ce9SDavid Howells NULL); 32933a3b7ce9SDavid Howells 32943a3b7ce9SDavid Howells if (ret == 0) 32953a3b7ce9SDavid Howells tsec->create_sid = isec->sid; 32963a3b7ce9SDavid Howells return 0; 32973a3b7ce9SDavid Howells } 32983a3b7ce9SDavid Howells 329925354c4fSEric Paris static int selinux_kernel_module_request(void) 330025354c4fSEric Paris { 330125354c4fSEric Paris return task_has_system(current, SYSTEM__MODULE_REQUEST); 330225354c4fSEric Paris } 330325354c4fSEric Paris 33041da177e4SLinus Torvalds static int selinux_task_setpgid(struct task_struct *p, pid_t pgid) 33051da177e4SLinus Torvalds { 33063b11a1deSDavid Howells return current_has_perm(p, PROCESS__SETPGID); 33071da177e4SLinus Torvalds } 33081da177e4SLinus Torvalds 33091da177e4SLinus Torvalds static int selinux_task_getpgid(struct task_struct *p) 33101da177e4SLinus Torvalds { 33113b11a1deSDavid Howells return current_has_perm(p, PROCESS__GETPGID); 33121da177e4SLinus Torvalds } 33131da177e4SLinus Torvalds 33141da177e4SLinus Torvalds static int selinux_task_getsid(struct task_struct *p) 33151da177e4SLinus Torvalds { 33163b11a1deSDavid Howells return current_has_perm(p, PROCESS__GETSESSION); 33171da177e4SLinus Torvalds } 33181da177e4SLinus Torvalds 3319f9008e4cSDavid Quigley static void selinux_task_getsecid(struct task_struct *p, u32 *secid) 3320f9008e4cSDavid Quigley { 3321275bb41eSDavid Howells *secid = task_sid(p); 3322f9008e4cSDavid Quigley } 3323f9008e4cSDavid Quigley 33241da177e4SLinus Torvalds static int selinux_task_setnice(struct task_struct *p, int nice) 33251da177e4SLinus Torvalds { 33261da177e4SLinus Torvalds int rc; 33271da177e4SLinus Torvalds 3328200ac532SEric Paris rc = cap_task_setnice(p, nice); 33291da177e4SLinus Torvalds if (rc) 33301da177e4SLinus Torvalds return rc; 33311da177e4SLinus Torvalds 33323b11a1deSDavid Howells return current_has_perm(p, PROCESS__SETSCHED); 33331da177e4SLinus Torvalds } 33341da177e4SLinus Torvalds 333503e68060SJames Morris static int selinux_task_setioprio(struct task_struct *p, int ioprio) 333603e68060SJames Morris { 3337b5376771SSerge E. Hallyn int rc; 3338b5376771SSerge E. Hallyn 3339200ac532SEric Paris rc = cap_task_setioprio(p, ioprio); 3340b5376771SSerge E. Hallyn if (rc) 3341b5376771SSerge E. Hallyn return rc; 3342b5376771SSerge E. Hallyn 33433b11a1deSDavid Howells return current_has_perm(p, PROCESS__SETSCHED); 334403e68060SJames Morris } 334503e68060SJames Morris 3346a1836a42SDavid Quigley static int selinux_task_getioprio(struct task_struct *p) 3347a1836a42SDavid Quigley { 33483b11a1deSDavid Howells return current_has_perm(p, PROCESS__GETSCHED); 3349a1836a42SDavid Quigley } 3350a1836a42SDavid Quigley 33511da177e4SLinus Torvalds static int selinux_task_setrlimit(unsigned int resource, struct rlimit *new_rlim) 33521da177e4SLinus Torvalds { 33531da177e4SLinus Torvalds struct rlimit *old_rlim = current->signal->rlim + resource; 33541da177e4SLinus Torvalds 33551da177e4SLinus Torvalds /* Control the ability to change the hard limit (whether 33561da177e4SLinus Torvalds lowering or raising it), so that the hard limit can 33571da177e4SLinus Torvalds later be used as a safe reset point for the soft limit 3358d84f4f99SDavid Howells upon context transitions. See selinux_bprm_committing_creds. */ 33591da177e4SLinus Torvalds if (old_rlim->rlim_max != new_rlim->rlim_max) 33603b11a1deSDavid Howells return current_has_perm(current, PROCESS__SETRLIMIT); 33611da177e4SLinus Torvalds 33621da177e4SLinus Torvalds return 0; 33631da177e4SLinus Torvalds } 33641da177e4SLinus Torvalds 33651da177e4SLinus Torvalds static int selinux_task_setscheduler(struct task_struct *p, int policy, struct sched_param *lp) 33661da177e4SLinus Torvalds { 3367b5376771SSerge E. Hallyn int rc; 3368b5376771SSerge E. Hallyn 3369200ac532SEric Paris rc = cap_task_setscheduler(p, policy, lp); 3370b5376771SSerge E. Hallyn if (rc) 3371b5376771SSerge E. Hallyn return rc; 3372b5376771SSerge E. Hallyn 33733b11a1deSDavid Howells return current_has_perm(p, PROCESS__SETSCHED); 33741da177e4SLinus Torvalds } 33751da177e4SLinus Torvalds 33761da177e4SLinus Torvalds static int selinux_task_getscheduler(struct task_struct *p) 33771da177e4SLinus Torvalds { 33783b11a1deSDavid Howells return current_has_perm(p, PROCESS__GETSCHED); 33791da177e4SLinus Torvalds } 33801da177e4SLinus Torvalds 338135601547SDavid Quigley static int selinux_task_movememory(struct task_struct *p) 338235601547SDavid Quigley { 33833b11a1deSDavid Howells return current_has_perm(p, PROCESS__SETSCHED); 338435601547SDavid Quigley } 338535601547SDavid Quigley 3386f9008e4cSDavid Quigley static int selinux_task_kill(struct task_struct *p, struct siginfo *info, 3387f9008e4cSDavid Quigley int sig, u32 secid) 33881da177e4SLinus Torvalds { 33891da177e4SLinus Torvalds u32 perm; 33901da177e4SLinus Torvalds int rc; 33911da177e4SLinus Torvalds 33921da177e4SLinus Torvalds if (!sig) 33931da177e4SLinus Torvalds perm = PROCESS__SIGNULL; /* null signal; existence test */ 33941da177e4SLinus Torvalds else 33951da177e4SLinus Torvalds perm = signal_to_av(sig); 3396f9008e4cSDavid Quigley if (secid) 3397275bb41eSDavid Howells rc = avc_has_perm(secid, task_sid(p), 3398275bb41eSDavid Howells SECCLASS_PROCESS, perm, NULL); 3399f9008e4cSDavid Quigley else 34003b11a1deSDavid Howells rc = current_has_perm(p, perm); 3401f9008e4cSDavid Quigley return rc; 34021da177e4SLinus Torvalds } 34031da177e4SLinus Torvalds 34041da177e4SLinus Torvalds static int selinux_task_wait(struct task_struct *p) 34051da177e4SLinus Torvalds { 34068a535140SEric Paris return task_has_perm(p, current, PROCESS__SIGCHLD); 34071da177e4SLinus Torvalds } 34081da177e4SLinus Torvalds 34091da177e4SLinus Torvalds static void selinux_task_to_inode(struct task_struct *p, 34101da177e4SLinus Torvalds struct inode *inode) 34111da177e4SLinus Torvalds { 34121da177e4SLinus Torvalds struct inode_security_struct *isec = inode->i_security; 3413275bb41eSDavid Howells u32 sid = task_sid(p); 34141da177e4SLinus Torvalds 3415275bb41eSDavid Howells isec->sid = sid; 34161da177e4SLinus Torvalds isec->initialized = 1; 34171da177e4SLinus Torvalds } 34181da177e4SLinus Torvalds 34191da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 342067f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv4(struct sk_buff *skb, 3421*2bf49690SThomas Liu struct common_audit_data *ad, u8 *proto) 34221da177e4SLinus Torvalds { 34231da177e4SLinus Torvalds int offset, ihlen, ret = -EINVAL; 34241da177e4SLinus Torvalds struct iphdr _iph, *ih; 34251da177e4SLinus Torvalds 3426bbe735e4SArnaldo Carvalho de Melo offset = skb_network_offset(skb); 34271da177e4SLinus Torvalds ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph); 34281da177e4SLinus Torvalds if (ih == NULL) 34291da177e4SLinus Torvalds goto out; 34301da177e4SLinus Torvalds 34311da177e4SLinus Torvalds ihlen = ih->ihl * 4; 34321da177e4SLinus Torvalds if (ihlen < sizeof(_iph)) 34331da177e4SLinus Torvalds goto out; 34341da177e4SLinus Torvalds 34351da177e4SLinus Torvalds ad->u.net.v4info.saddr = ih->saddr; 34361da177e4SLinus Torvalds ad->u.net.v4info.daddr = ih->daddr; 34371da177e4SLinus Torvalds ret = 0; 34381da177e4SLinus Torvalds 343967f83cbfSVenkat Yekkirala if (proto) 344067f83cbfSVenkat Yekkirala *proto = ih->protocol; 344167f83cbfSVenkat Yekkirala 34421da177e4SLinus Torvalds switch (ih->protocol) { 34431da177e4SLinus Torvalds case IPPROTO_TCP: { 34441da177e4SLinus Torvalds struct tcphdr _tcph, *th; 34451da177e4SLinus Torvalds 34461da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 34471da177e4SLinus Torvalds break; 34481da177e4SLinus Torvalds 34491da177e4SLinus Torvalds offset += ihlen; 34501da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 34511da177e4SLinus Torvalds if (th == NULL) 34521da177e4SLinus Torvalds break; 34531da177e4SLinus Torvalds 34541da177e4SLinus Torvalds ad->u.net.sport = th->source; 34551da177e4SLinus Torvalds ad->u.net.dport = th->dest; 34561da177e4SLinus Torvalds break; 34571da177e4SLinus Torvalds } 34581da177e4SLinus Torvalds 34591da177e4SLinus Torvalds case IPPROTO_UDP: { 34601da177e4SLinus Torvalds struct udphdr _udph, *uh; 34611da177e4SLinus Torvalds 34621da177e4SLinus Torvalds if (ntohs(ih->frag_off) & IP_OFFSET) 34631da177e4SLinus Torvalds break; 34641da177e4SLinus Torvalds 34651da177e4SLinus Torvalds offset += ihlen; 34661da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 34671da177e4SLinus Torvalds if (uh == NULL) 34681da177e4SLinus Torvalds break; 34691da177e4SLinus Torvalds 34701da177e4SLinus Torvalds ad->u.net.sport = uh->source; 34711da177e4SLinus Torvalds ad->u.net.dport = uh->dest; 34721da177e4SLinus Torvalds break; 34731da177e4SLinus Torvalds } 34741da177e4SLinus Torvalds 34752ee92d46SJames Morris case IPPROTO_DCCP: { 34762ee92d46SJames Morris struct dccp_hdr _dccph, *dh; 34772ee92d46SJames Morris 34782ee92d46SJames Morris if (ntohs(ih->frag_off) & IP_OFFSET) 34792ee92d46SJames Morris break; 34802ee92d46SJames Morris 34812ee92d46SJames Morris offset += ihlen; 34822ee92d46SJames Morris dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); 34832ee92d46SJames Morris if (dh == NULL) 34842ee92d46SJames Morris break; 34852ee92d46SJames Morris 34862ee92d46SJames Morris ad->u.net.sport = dh->dccph_sport; 34872ee92d46SJames Morris ad->u.net.dport = dh->dccph_dport; 34882ee92d46SJames Morris break; 34892ee92d46SJames Morris } 34902ee92d46SJames Morris 34911da177e4SLinus Torvalds default: 34921da177e4SLinus Torvalds break; 34931da177e4SLinus Torvalds } 34941da177e4SLinus Torvalds out: 34951da177e4SLinus Torvalds return ret; 34961da177e4SLinus Torvalds } 34971da177e4SLinus Torvalds 34981da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 34991da177e4SLinus Torvalds 35001da177e4SLinus Torvalds /* Returns error only if unable to parse addresses */ 350167f83cbfSVenkat Yekkirala static int selinux_parse_skb_ipv6(struct sk_buff *skb, 3502*2bf49690SThomas Liu struct common_audit_data *ad, u8 *proto) 35031da177e4SLinus Torvalds { 35041da177e4SLinus Torvalds u8 nexthdr; 35051da177e4SLinus Torvalds int ret = -EINVAL, offset; 35061da177e4SLinus Torvalds struct ipv6hdr _ipv6h, *ip6; 35071da177e4SLinus Torvalds 3508bbe735e4SArnaldo Carvalho de Melo offset = skb_network_offset(skb); 35091da177e4SLinus Torvalds ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h); 35101da177e4SLinus Torvalds if (ip6 == NULL) 35111da177e4SLinus Torvalds goto out; 35121da177e4SLinus Torvalds 35131da177e4SLinus Torvalds ipv6_addr_copy(&ad->u.net.v6info.saddr, &ip6->saddr); 35141da177e4SLinus Torvalds ipv6_addr_copy(&ad->u.net.v6info.daddr, &ip6->daddr); 35151da177e4SLinus Torvalds ret = 0; 35161da177e4SLinus Torvalds 35171da177e4SLinus Torvalds nexthdr = ip6->nexthdr; 35181da177e4SLinus Torvalds offset += sizeof(_ipv6h); 35190d3d077cSHerbert Xu offset = ipv6_skip_exthdr(skb, offset, &nexthdr); 35201da177e4SLinus Torvalds if (offset < 0) 35211da177e4SLinus Torvalds goto out; 35221da177e4SLinus Torvalds 352367f83cbfSVenkat Yekkirala if (proto) 352467f83cbfSVenkat Yekkirala *proto = nexthdr; 352567f83cbfSVenkat Yekkirala 35261da177e4SLinus Torvalds switch (nexthdr) { 35271da177e4SLinus Torvalds case IPPROTO_TCP: { 35281da177e4SLinus Torvalds struct tcphdr _tcph, *th; 35291da177e4SLinus Torvalds 35301da177e4SLinus Torvalds th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 35311da177e4SLinus Torvalds if (th == NULL) 35321da177e4SLinus Torvalds break; 35331da177e4SLinus Torvalds 35341da177e4SLinus Torvalds ad->u.net.sport = th->source; 35351da177e4SLinus Torvalds ad->u.net.dport = th->dest; 35361da177e4SLinus Torvalds break; 35371da177e4SLinus Torvalds } 35381da177e4SLinus Torvalds 35391da177e4SLinus Torvalds case IPPROTO_UDP: { 35401da177e4SLinus Torvalds struct udphdr _udph, *uh; 35411da177e4SLinus Torvalds 35421da177e4SLinus Torvalds uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 35431da177e4SLinus Torvalds if (uh == NULL) 35441da177e4SLinus Torvalds break; 35451da177e4SLinus Torvalds 35461da177e4SLinus Torvalds ad->u.net.sport = uh->source; 35471da177e4SLinus Torvalds ad->u.net.dport = uh->dest; 35481da177e4SLinus Torvalds break; 35491da177e4SLinus Torvalds } 35501da177e4SLinus Torvalds 35512ee92d46SJames Morris case IPPROTO_DCCP: { 35522ee92d46SJames Morris struct dccp_hdr _dccph, *dh; 35532ee92d46SJames Morris 35542ee92d46SJames Morris dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph); 35552ee92d46SJames Morris if (dh == NULL) 35562ee92d46SJames Morris break; 35572ee92d46SJames Morris 35582ee92d46SJames Morris ad->u.net.sport = dh->dccph_sport; 35592ee92d46SJames Morris ad->u.net.dport = dh->dccph_dport; 35602ee92d46SJames Morris break; 35612ee92d46SJames Morris } 35622ee92d46SJames Morris 35631da177e4SLinus Torvalds /* includes fragments */ 35641da177e4SLinus Torvalds default: 35651da177e4SLinus Torvalds break; 35661da177e4SLinus Torvalds } 35671da177e4SLinus Torvalds out: 35681da177e4SLinus Torvalds return ret; 35691da177e4SLinus Torvalds } 35701da177e4SLinus Torvalds 35711da177e4SLinus Torvalds #endif /* IPV6 */ 35721da177e4SLinus Torvalds 3573*2bf49690SThomas Liu static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad, 3574cf9481e2SDavid Howells char **_addrp, int src, u8 *proto) 35751da177e4SLinus Torvalds { 3576cf9481e2SDavid Howells char *addrp; 3577cf9481e2SDavid Howells int ret; 35781da177e4SLinus Torvalds 35791da177e4SLinus Torvalds switch (ad->u.net.family) { 35801da177e4SLinus Torvalds case PF_INET: 358167f83cbfSVenkat Yekkirala ret = selinux_parse_skb_ipv4(skb, ad, proto); 3582cf9481e2SDavid Howells if (ret) 3583cf9481e2SDavid Howells goto parse_error; 3584cf9481e2SDavid Howells addrp = (char *)(src ? &ad->u.net.v4info.saddr : 35851da177e4SLinus Torvalds &ad->u.net.v4info.daddr); 3586cf9481e2SDavid Howells goto okay; 35871da177e4SLinus Torvalds 35881da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 35891da177e4SLinus Torvalds case PF_INET6: 359067f83cbfSVenkat Yekkirala ret = selinux_parse_skb_ipv6(skb, ad, proto); 3591cf9481e2SDavid Howells if (ret) 3592cf9481e2SDavid Howells goto parse_error; 3593cf9481e2SDavid Howells addrp = (char *)(src ? &ad->u.net.v6info.saddr : 35941da177e4SLinus Torvalds &ad->u.net.v6info.daddr); 3595cf9481e2SDavid Howells goto okay; 35961da177e4SLinus Torvalds #endif /* IPV6 */ 35971da177e4SLinus Torvalds default: 3598cf9481e2SDavid Howells addrp = NULL; 3599cf9481e2SDavid Howells goto okay; 36001da177e4SLinus Torvalds } 36011da177e4SLinus Torvalds 3602cf9481e2SDavid Howells parse_error: 360371f1cb05SPaul Moore printk(KERN_WARNING 360471f1cb05SPaul Moore "SELinux: failure in selinux_parse_skb()," 360571f1cb05SPaul Moore " unable to parse packet\n"); 36061da177e4SLinus Torvalds return ret; 3607cf9481e2SDavid Howells 3608cf9481e2SDavid Howells okay: 3609cf9481e2SDavid Howells if (_addrp) 3610cf9481e2SDavid Howells *_addrp = addrp; 3611cf9481e2SDavid Howells return 0; 36121da177e4SLinus Torvalds } 36131da177e4SLinus Torvalds 36144f6a993fSPaul Moore /** 3615220deb96SPaul Moore * selinux_skb_peerlbl_sid - Determine the peer label of a packet 36164f6a993fSPaul Moore * @skb: the packet 361775e22910SPaul Moore * @family: protocol family 3618220deb96SPaul Moore * @sid: the packet's peer label SID 36194f6a993fSPaul Moore * 36204f6a993fSPaul Moore * Description: 3621220deb96SPaul Moore * Check the various different forms of network peer labeling and determine 3622220deb96SPaul Moore * the peer label/SID for the packet; most of the magic actually occurs in 3623220deb96SPaul Moore * the security server function security_net_peersid_cmp(). The function 3624220deb96SPaul Moore * returns zero if the value in @sid is valid (although it may be SECSID_NULL) 3625220deb96SPaul Moore * or -EACCES if @sid is invalid due to inconsistencies with the different 3626220deb96SPaul Moore * peer labels. 36274f6a993fSPaul Moore * 36284f6a993fSPaul Moore */ 3629220deb96SPaul Moore static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid) 36304f6a993fSPaul Moore { 363171f1cb05SPaul Moore int err; 36324f6a993fSPaul Moore u32 xfrm_sid; 36334f6a993fSPaul Moore u32 nlbl_sid; 3634220deb96SPaul Moore u32 nlbl_type; 36354f6a993fSPaul Moore 36364f6a993fSPaul Moore selinux_skb_xfrm_sid(skb, &xfrm_sid); 36375dbe1eb0SPaul Moore selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid); 3638220deb96SPaul Moore 363971f1cb05SPaul Moore err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid); 364071f1cb05SPaul Moore if (unlikely(err)) { 364171f1cb05SPaul Moore printk(KERN_WARNING 364271f1cb05SPaul Moore "SELinux: failure in selinux_skb_peerlbl_sid()," 364371f1cb05SPaul Moore " unable to determine packet's peer label\n"); 3644220deb96SPaul Moore return -EACCES; 364571f1cb05SPaul Moore } 3646220deb96SPaul Moore 3647220deb96SPaul Moore return 0; 36484f6a993fSPaul Moore } 36494f6a993fSPaul Moore 36501da177e4SLinus Torvalds /* socket security operations */ 36511da177e4SLinus Torvalds static int socket_has_perm(struct task_struct *task, struct socket *sock, 36521da177e4SLinus Torvalds u32 perms) 36531da177e4SLinus Torvalds { 36541da177e4SLinus Torvalds struct inode_security_struct *isec; 3655*2bf49690SThomas Liu struct common_audit_data ad; 3656275bb41eSDavid Howells u32 sid; 36571da177e4SLinus Torvalds int err = 0; 36581da177e4SLinus Torvalds 36591da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 36601da177e4SLinus Torvalds 36611da177e4SLinus Torvalds if (isec->sid == SECINITSID_KERNEL) 36621da177e4SLinus Torvalds goto out; 3663275bb41eSDavid Howells sid = task_sid(task); 36641da177e4SLinus Torvalds 3665*2bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, NET); 36661da177e4SLinus Torvalds ad.u.net.sk = sock->sk; 3667275bb41eSDavid Howells err = avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad); 36681da177e4SLinus Torvalds 36691da177e4SLinus Torvalds out: 36701da177e4SLinus Torvalds return err; 36711da177e4SLinus Torvalds } 36721da177e4SLinus Torvalds 36731da177e4SLinus Torvalds static int selinux_socket_create(int family, int type, 36741da177e4SLinus Torvalds int protocol, int kern) 36751da177e4SLinus Torvalds { 3676275bb41eSDavid Howells const struct cred *cred = current_cred(); 3677275bb41eSDavid Howells const struct task_security_struct *tsec = cred->security; 3678275bb41eSDavid Howells u32 sid, newsid; 3679275bb41eSDavid Howells u16 secclass; 36801da177e4SLinus Torvalds int err = 0; 36811da177e4SLinus Torvalds 36821da177e4SLinus Torvalds if (kern) 36831da177e4SLinus Torvalds goto out; 36841da177e4SLinus Torvalds 3685275bb41eSDavid Howells sid = tsec->sid; 3686275bb41eSDavid Howells newsid = tsec->sockcreate_sid ?: sid; 3687275bb41eSDavid Howells 3688275bb41eSDavid Howells secclass = socket_type_to_security_class(family, type, protocol); 3689275bb41eSDavid Howells err = avc_has_perm(sid, newsid, secclass, SOCKET__CREATE, NULL); 36901da177e4SLinus Torvalds 36911da177e4SLinus Torvalds out: 36921da177e4SLinus Torvalds return err; 36931da177e4SLinus Torvalds } 36941da177e4SLinus Torvalds 36957420ed23SVenkat Yekkirala static int selinux_socket_post_create(struct socket *sock, int family, 36961da177e4SLinus Torvalds int type, int protocol, int kern) 36971da177e4SLinus Torvalds { 3698275bb41eSDavid Howells const struct cred *cred = current_cred(); 3699275bb41eSDavid Howells const struct task_security_struct *tsec = cred->security; 37001da177e4SLinus Torvalds struct inode_security_struct *isec; 3701892c141eSVenkat Yekkirala struct sk_security_struct *sksec; 3702275bb41eSDavid Howells u32 sid, newsid; 3703275bb41eSDavid Howells int err = 0; 3704275bb41eSDavid Howells 3705275bb41eSDavid Howells sid = tsec->sid; 3706275bb41eSDavid Howells newsid = tsec->sockcreate_sid; 37071da177e4SLinus Torvalds 37081da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 37091da177e4SLinus Torvalds 3710275bb41eSDavid Howells if (kern) 3711275bb41eSDavid Howells isec->sid = SECINITSID_KERNEL; 3712275bb41eSDavid Howells else if (newsid) 3713275bb41eSDavid Howells isec->sid = newsid; 3714275bb41eSDavid Howells else 3715275bb41eSDavid Howells isec->sid = sid; 3716275bb41eSDavid Howells 37171da177e4SLinus Torvalds isec->sclass = socket_type_to_security_class(family, type, protocol); 37181da177e4SLinus Torvalds isec->initialized = 1; 37191da177e4SLinus Torvalds 3720892c141eSVenkat Yekkirala if (sock->sk) { 3721892c141eSVenkat Yekkirala sksec = sock->sk->sk_security; 3722892c141eSVenkat Yekkirala sksec->sid = isec->sid; 3723220deb96SPaul Moore sksec->sclass = isec->sclass; 3724389fb800SPaul Moore err = selinux_netlbl_socket_post_create(sock->sk, family); 3725892c141eSVenkat Yekkirala } 3726892c141eSVenkat Yekkirala 37277420ed23SVenkat Yekkirala return err; 37281da177e4SLinus Torvalds } 37291da177e4SLinus Torvalds 37301da177e4SLinus Torvalds /* Range of port numbers used to automatically bind. 37311da177e4SLinus Torvalds Need to determine whether we should perform a name_bind 37321da177e4SLinus Torvalds permission check between the socket and the port number. */ 37331da177e4SLinus Torvalds 37341da177e4SLinus Torvalds static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen) 37351da177e4SLinus Torvalds { 37361da177e4SLinus Torvalds u16 family; 37371da177e4SLinus Torvalds int err; 37381da177e4SLinus Torvalds 37391da177e4SLinus Torvalds err = socket_has_perm(current, sock, SOCKET__BIND); 37401da177e4SLinus Torvalds if (err) 37411da177e4SLinus Torvalds goto out; 37421da177e4SLinus Torvalds 37431da177e4SLinus Torvalds /* 37441da177e4SLinus Torvalds * If PF_INET or PF_INET6, check name_bind permission for the port. 374513402580SJames Morris * Multiple address binding for SCTP is not supported yet: we just 374613402580SJames Morris * check the first address now. 37471da177e4SLinus Torvalds */ 37481da177e4SLinus Torvalds family = sock->sk->sk_family; 37491da177e4SLinus Torvalds if (family == PF_INET || family == PF_INET6) { 37501da177e4SLinus Torvalds char *addrp; 37511da177e4SLinus Torvalds struct inode_security_struct *isec; 3752*2bf49690SThomas Liu struct common_audit_data ad; 37531da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 37541da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 37551da177e4SLinus Torvalds unsigned short snum; 37561da177e4SLinus Torvalds struct sock *sk = sock->sk; 3757e399f982SJames Morris u32 sid, node_perm; 37581da177e4SLinus Torvalds 37591da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 37601da177e4SLinus Torvalds 37611da177e4SLinus Torvalds if (family == PF_INET) { 37621da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 37631da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 37641da177e4SLinus Torvalds addrp = (char *)&addr4->sin_addr.s_addr; 37651da177e4SLinus Torvalds } else { 37661da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 37671da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 37681da177e4SLinus Torvalds addrp = (char *)&addr6->sin6_addr.s6_addr; 37691da177e4SLinus Torvalds } 37701da177e4SLinus Torvalds 3771227b60f5SStephen Hemminger if (snum) { 3772227b60f5SStephen Hemminger int low, high; 3773227b60f5SStephen Hemminger 3774227b60f5SStephen Hemminger inet_get_local_port_range(&low, &high); 3775227b60f5SStephen Hemminger 3776227b60f5SStephen Hemminger if (snum < max(PROT_SOCK, low) || snum > high) { 37773e112172SPaul Moore err = sel_netport_sid(sk->sk_protocol, 37783e112172SPaul Moore snum, &sid); 37791da177e4SLinus Torvalds if (err) 37801da177e4SLinus Torvalds goto out; 3781*2bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, NET); 37821da177e4SLinus Torvalds ad.u.net.sport = htons(snum); 37831da177e4SLinus Torvalds ad.u.net.family = family; 37841da177e4SLinus Torvalds err = avc_has_perm(isec->sid, sid, 37851da177e4SLinus Torvalds isec->sclass, 37861da177e4SLinus Torvalds SOCKET__NAME_BIND, &ad); 37871da177e4SLinus Torvalds if (err) 37881da177e4SLinus Torvalds goto out; 37891da177e4SLinus Torvalds } 3790227b60f5SStephen Hemminger } 37911da177e4SLinus Torvalds 379213402580SJames Morris switch (isec->sclass) { 379313402580SJames Morris case SECCLASS_TCP_SOCKET: 37941da177e4SLinus Torvalds node_perm = TCP_SOCKET__NODE_BIND; 37951da177e4SLinus Torvalds break; 37961da177e4SLinus Torvalds 379713402580SJames Morris case SECCLASS_UDP_SOCKET: 37981da177e4SLinus Torvalds node_perm = UDP_SOCKET__NODE_BIND; 37991da177e4SLinus Torvalds break; 38001da177e4SLinus Torvalds 38012ee92d46SJames Morris case SECCLASS_DCCP_SOCKET: 38022ee92d46SJames Morris node_perm = DCCP_SOCKET__NODE_BIND; 38032ee92d46SJames Morris break; 38042ee92d46SJames Morris 38051da177e4SLinus Torvalds default: 38061da177e4SLinus Torvalds node_perm = RAWIP_SOCKET__NODE_BIND; 38071da177e4SLinus Torvalds break; 38081da177e4SLinus Torvalds } 38091da177e4SLinus Torvalds 3810224dfbd8SPaul Moore err = sel_netnode_sid(addrp, family, &sid); 38111da177e4SLinus Torvalds if (err) 38121da177e4SLinus Torvalds goto out; 38131da177e4SLinus Torvalds 3814*2bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, NET); 38151da177e4SLinus Torvalds ad.u.net.sport = htons(snum); 38161da177e4SLinus Torvalds ad.u.net.family = family; 38171da177e4SLinus Torvalds 38181da177e4SLinus Torvalds if (family == PF_INET) 38191da177e4SLinus Torvalds ad.u.net.v4info.saddr = addr4->sin_addr.s_addr; 38201da177e4SLinus Torvalds else 38211da177e4SLinus Torvalds ipv6_addr_copy(&ad.u.net.v6info.saddr, &addr6->sin6_addr); 38221da177e4SLinus Torvalds 38231da177e4SLinus Torvalds err = avc_has_perm(isec->sid, sid, 38241da177e4SLinus Torvalds isec->sclass, node_perm, &ad); 38251da177e4SLinus Torvalds if (err) 38261da177e4SLinus Torvalds goto out; 38271da177e4SLinus Torvalds } 38281da177e4SLinus Torvalds out: 38291da177e4SLinus Torvalds return err; 38301da177e4SLinus Torvalds } 38311da177e4SLinus Torvalds 38321da177e4SLinus Torvalds static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen) 38331da177e4SLinus Torvalds { 3834014ab19aSPaul Moore struct sock *sk = sock->sk; 38351da177e4SLinus Torvalds struct inode_security_struct *isec; 38361da177e4SLinus Torvalds int err; 38371da177e4SLinus Torvalds 38381da177e4SLinus Torvalds err = socket_has_perm(current, sock, SOCKET__CONNECT); 38391da177e4SLinus Torvalds if (err) 38401da177e4SLinus Torvalds return err; 38411da177e4SLinus Torvalds 38421da177e4SLinus Torvalds /* 38432ee92d46SJames Morris * If a TCP or DCCP socket, check name_connect permission for the port. 38441da177e4SLinus Torvalds */ 38451da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 38462ee92d46SJames Morris if (isec->sclass == SECCLASS_TCP_SOCKET || 38472ee92d46SJames Morris isec->sclass == SECCLASS_DCCP_SOCKET) { 3848*2bf49690SThomas Liu struct common_audit_data ad; 38491da177e4SLinus Torvalds struct sockaddr_in *addr4 = NULL; 38501da177e4SLinus Torvalds struct sockaddr_in6 *addr6 = NULL; 38511da177e4SLinus Torvalds unsigned short snum; 38522ee92d46SJames Morris u32 sid, perm; 38531da177e4SLinus Torvalds 38541da177e4SLinus Torvalds if (sk->sk_family == PF_INET) { 38551da177e4SLinus Torvalds addr4 = (struct sockaddr_in *)address; 3856911656f8SStephen Smalley if (addrlen < sizeof(struct sockaddr_in)) 38571da177e4SLinus Torvalds return -EINVAL; 38581da177e4SLinus Torvalds snum = ntohs(addr4->sin_port); 38591da177e4SLinus Torvalds } else { 38601da177e4SLinus Torvalds addr6 = (struct sockaddr_in6 *)address; 3861911656f8SStephen Smalley if (addrlen < SIN6_LEN_RFC2133) 38621da177e4SLinus Torvalds return -EINVAL; 38631da177e4SLinus Torvalds snum = ntohs(addr6->sin6_port); 38641da177e4SLinus Torvalds } 38651da177e4SLinus Torvalds 38663e112172SPaul Moore err = sel_netport_sid(sk->sk_protocol, snum, &sid); 38671da177e4SLinus Torvalds if (err) 38681da177e4SLinus Torvalds goto out; 38691da177e4SLinus Torvalds 38702ee92d46SJames Morris perm = (isec->sclass == SECCLASS_TCP_SOCKET) ? 38712ee92d46SJames Morris TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT; 38722ee92d46SJames Morris 3873*2bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, NET); 38741da177e4SLinus Torvalds ad.u.net.dport = htons(snum); 38751da177e4SLinus Torvalds ad.u.net.family = sk->sk_family; 38762ee92d46SJames Morris err = avc_has_perm(isec->sid, sid, isec->sclass, perm, &ad); 38771da177e4SLinus Torvalds if (err) 38781da177e4SLinus Torvalds goto out; 38791da177e4SLinus Torvalds } 38801da177e4SLinus Torvalds 3881014ab19aSPaul Moore err = selinux_netlbl_socket_connect(sk, address); 3882014ab19aSPaul Moore 38831da177e4SLinus Torvalds out: 38841da177e4SLinus Torvalds return err; 38851da177e4SLinus Torvalds } 38861da177e4SLinus Torvalds 38871da177e4SLinus Torvalds static int selinux_socket_listen(struct socket *sock, int backlog) 38881da177e4SLinus Torvalds { 38891da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__LISTEN); 38901da177e4SLinus Torvalds } 38911da177e4SLinus Torvalds 38921da177e4SLinus Torvalds static int selinux_socket_accept(struct socket *sock, struct socket *newsock) 38931da177e4SLinus Torvalds { 38941da177e4SLinus Torvalds int err; 38951da177e4SLinus Torvalds struct inode_security_struct *isec; 38961da177e4SLinus Torvalds struct inode_security_struct *newisec; 38971da177e4SLinus Torvalds 38981da177e4SLinus Torvalds err = socket_has_perm(current, sock, SOCKET__ACCEPT); 38991da177e4SLinus Torvalds if (err) 39001da177e4SLinus Torvalds return err; 39011da177e4SLinus Torvalds 39021da177e4SLinus Torvalds newisec = SOCK_INODE(newsock)->i_security; 39031da177e4SLinus Torvalds 39041da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 39051da177e4SLinus Torvalds newisec->sclass = isec->sclass; 39061da177e4SLinus Torvalds newisec->sid = isec->sid; 39071da177e4SLinus Torvalds newisec->initialized = 1; 39081da177e4SLinus Torvalds 39091da177e4SLinus Torvalds return 0; 39101da177e4SLinus Torvalds } 39111da177e4SLinus Torvalds 39121da177e4SLinus Torvalds static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg, 39131da177e4SLinus Torvalds int size) 39141da177e4SLinus Torvalds { 3915389fb800SPaul Moore return socket_has_perm(current, sock, SOCKET__WRITE); 39161da177e4SLinus Torvalds } 39171da177e4SLinus Torvalds 39181da177e4SLinus Torvalds static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg, 39191da177e4SLinus Torvalds int size, int flags) 39201da177e4SLinus Torvalds { 39211da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__READ); 39221da177e4SLinus Torvalds } 39231da177e4SLinus Torvalds 39241da177e4SLinus Torvalds static int selinux_socket_getsockname(struct socket *sock) 39251da177e4SLinus Torvalds { 39261da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__GETATTR); 39271da177e4SLinus Torvalds } 39281da177e4SLinus Torvalds 39291da177e4SLinus Torvalds static int selinux_socket_getpeername(struct socket *sock) 39301da177e4SLinus Torvalds { 39311da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__GETATTR); 39321da177e4SLinus Torvalds } 39331da177e4SLinus Torvalds 39341da177e4SLinus Torvalds static int selinux_socket_setsockopt(struct socket *sock, int level, int optname) 39351da177e4SLinus Torvalds { 3936f8687afeSPaul Moore int err; 3937f8687afeSPaul Moore 3938f8687afeSPaul Moore err = socket_has_perm(current, sock, SOCKET__SETOPT); 3939f8687afeSPaul Moore if (err) 3940f8687afeSPaul Moore return err; 3941f8687afeSPaul Moore 3942f8687afeSPaul Moore return selinux_netlbl_socket_setsockopt(sock, level, optname); 39431da177e4SLinus Torvalds } 39441da177e4SLinus Torvalds 39451da177e4SLinus Torvalds static int selinux_socket_getsockopt(struct socket *sock, int level, 39461da177e4SLinus Torvalds int optname) 39471da177e4SLinus Torvalds { 39481da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__GETOPT); 39491da177e4SLinus Torvalds } 39501da177e4SLinus Torvalds 39511da177e4SLinus Torvalds static int selinux_socket_shutdown(struct socket *sock, int how) 39521da177e4SLinus Torvalds { 39531da177e4SLinus Torvalds return socket_has_perm(current, sock, SOCKET__SHUTDOWN); 39541da177e4SLinus Torvalds } 39551da177e4SLinus Torvalds 39561da177e4SLinus Torvalds static int selinux_socket_unix_stream_connect(struct socket *sock, 39571da177e4SLinus Torvalds struct socket *other, 39581da177e4SLinus Torvalds struct sock *newsk) 39591da177e4SLinus Torvalds { 39601da177e4SLinus Torvalds struct sk_security_struct *ssec; 39611da177e4SLinus Torvalds struct inode_security_struct *isec; 39621da177e4SLinus Torvalds struct inode_security_struct *other_isec; 3963*2bf49690SThomas Liu struct common_audit_data ad; 39641da177e4SLinus Torvalds int err; 39651da177e4SLinus Torvalds 39661da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 39671da177e4SLinus Torvalds other_isec = SOCK_INODE(other)->i_security; 39681da177e4SLinus Torvalds 3969*2bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, NET); 39701da177e4SLinus Torvalds ad.u.net.sk = other->sk; 39711da177e4SLinus Torvalds 39721da177e4SLinus Torvalds err = avc_has_perm(isec->sid, other_isec->sid, 39731da177e4SLinus Torvalds isec->sclass, 39741da177e4SLinus Torvalds UNIX_STREAM_SOCKET__CONNECTTO, &ad); 39751da177e4SLinus Torvalds if (err) 39761da177e4SLinus Torvalds return err; 39771da177e4SLinus Torvalds 39781da177e4SLinus Torvalds /* connecting socket */ 39791da177e4SLinus Torvalds ssec = sock->sk->sk_security; 39801da177e4SLinus Torvalds ssec->peer_sid = other_isec->sid; 39811da177e4SLinus Torvalds 39821da177e4SLinus Torvalds /* server child socket */ 39831da177e4SLinus Torvalds ssec = newsk->sk_security; 39841da177e4SLinus Torvalds ssec->peer_sid = isec->sid; 39854237c75cSVenkat Yekkirala err = security_sid_mls_copy(other_isec->sid, ssec->peer_sid, &ssec->sid); 39861da177e4SLinus Torvalds 39874237c75cSVenkat Yekkirala return err; 39881da177e4SLinus Torvalds } 39891da177e4SLinus Torvalds 39901da177e4SLinus Torvalds static int selinux_socket_unix_may_send(struct socket *sock, 39911da177e4SLinus Torvalds struct socket *other) 39921da177e4SLinus Torvalds { 39931da177e4SLinus Torvalds struct inode_security_struct *isec; 39941da177e4SLinus Torvalds struct inode_security_struct *other_isec; 3995*2bf49690SThomas Liu struct common_audit_data ad; 39961da177e4SLinus Torvalds int err; 39971da177e4SLinus Torvalds 39981da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 39991da177e4SLinus Torvalds other_isec = SOCK_INODE(other)->i_security; 40001da177e4SLinus Torvalds 4001*2bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, NET); 40021da177e4SLinus Torvalds ad.u.net.sk = other->sk; 40031da177e4SLinus Torvalds 40041da177e4SLinus Torvalds err = avc_has_perm(isec->sid, other_isec->sid, 40051da177e4SLinus Torvalds isec->sclass, SOCKET__SENDTO, &ad); 40061da177e4SLinus Torvalds if (err) 40071da177e4SLinus Torvalds return err; 40081da177e4SLinus Torvalds 40091da177e4SLinus Torvalds return 0; 40101da177e4SLinus Torvalds } 40111da177e4SLinus Torvalds 4012effad8dfSPaul Moore static int selinux_inet_sys_rcv_skb(int ifindex, char *addrp, u16 family, 4013effad8dfSPaul Moore u32 peer_sid, 4014*2bf49690SThomas Liu struct common_audit_data *ad) 4015effad8dfSPaul Moore { 4016effad8dfSPaul Moore int err; 4017effad8dfSPaul Moore u32 if_sid; 4018effad8dfSPaul Moore u32 node_sid; 4019effad8dfSPaul Moore 4020effad8dfSPaul Moore err = sel_netif_sid(ifindex, &if_sid); 4021effad8dfSPaul Moore if (err) 4022effad8dfSPaul Moore return err; 4023effad8dfSPaul Moore err = avc_has_perm(peer_sid, if_sid, 4024effad8dfSPaul Moore SECCLASS_NETIF, NETIF__INGRESS, ad); 4025effad8dfSPaul Moore if (err) 4026effad8dfSPaul Moore return err; 4027effad8dfSPaul Moore 4028effad8dfSPaul Moore err = sel_netnode_sid(addrp, family, &node_sid); 4029effad8dfSPaul Moore if (err) 4030effad8dfSPaul Moore return err; 4031effad8dfSPaul Moore return avc_has_perm(peer_sid, node_sid, 4032effad8dfSPaul Moore SECCLASS_NODE, NODE__RECVFROM, ad); 4033effad8dfSPaul Moore } 4034effad8dfSPaul Moore 4035220deb96SPaul Moore static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb, 4036d8395c87SPaul Moore u16 family) 4037220deb96SPaul Moore { 4038277d342fSPaul Moore int err = 0; 4039220deb96SPaul Moore struct sk_security_struct *sksec = sk->sk_security; 4040220deb96SPaul Moore u32 peer_sid; 4041220deb96SPaul Moore u32 sk_sid = sksec->sid; 4042*2bf49690SThomas Liu struct common_audit_data ad; 4043d8395c87SPaul Moore char *addrp; 4044d8395c87SPaul Moore 4045*2bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, NET); 4046d8395c87SPaul Moore ad.u.net.netif = skb->iif; 4047d8395c87SPaul Moore ad.u.net.family = family; 4048d8395c87SPaul Moore err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); 4049d8395c87SPaul Moore if (err) 4050d8395c87SPaul Moore return err; 4051220deb96SPaul Moore 405258bfbb51SPaul Moore if (selinux_secmark_enabled()) { 4053220deb96SPaul Moore err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET, 4054d8395c87SPaul Moore PACKET__RECV, &ad); 4055220deb96SPaul Moore if (err) 4056220deb96SPaul Moore return err; 405758bfbb51SPaul Moore } 4058220deb96SPaul Moore 4059220deb96SPaul Moore if (selinux_policycap_netpeer) { 4060220deb96SPaul Moore err = selinux_skb_peerlbl_sid(skb, family, &peer_sid); 4061220deb96SPaul Moore if (err) 4062220deb96SPaul Moore return err; 4063220deb96SPaul Moore err = avc_has_perm(sk_sid, peer_sid, 4064d8395c87SPaul Moore SECCLASS_PEER, PEER__RECV, &ad); 4065dfaebe98SPaul Moore if (err) 4066dfaebe98SPaul Moore selinux_netlbl_err(skb, err, 0); 4067220deb96SPaul Moore } else { 4068d8395c87SPaul Moore err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad); 4069220deb96SPaul Moore if (err) 4070220deb96SPaul Moore return err; 4071d8395c87SPaul Moore err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad); 4072220deb96SPaul Moore } 4073220deb96SPaul Moore 40744e5ab4cbSJames Morris return err; 40754e5ab4cbSJames Morris } 4076d28d1e08STrent Jaeger 40774e5ab4cbSJames Morris static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) 40784e5ab4cbSJames Morris { 4079220deb96SPaul Moore int err; 40804237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 4081220deb96SPaul Moore u16 family = sk->sk_family; 4082220deb96SPaul Moore u32 sk_sid = sksec->sid; 4083*2bf49690SThomas Liu struct common_audit_data ad; 4084220deb96SPaul Moore char *addrp; 4085d8395c87SPaul Moore u8 secmark_active; 4086d8395c87SPaul Moore u8 peerlbl_active; 40874e5ab4cbSJames Morris 40884e5ab4cbSJames Morris if (family != PF_INET && family != PF_INET6) 4089220deb96SPaul Moore return 0; 40904e5ab4cbSJames Morris 40914e5ab4cbSJames Morris /* Handle mapped IPv4 packets arriving via IPv6 sockets */ 409287fcd70dSAl Viro if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 40934e5ab4cbSJames Morris family = PF_INET; 40944e5ab4cbSJames Morris 4095d8395c87SPaul Moore /* If any sort of compatibility mode is enabled then handoff processing 4096d8395c87SPaul Moore * to the selinux_sock_rcv_skb_compat() function to deal with the 4097d8395c87SPaul Moore * special handling. We do this in an attempt to keep this function 4098d8395c87SPaul Moore * as fast and as clean as possible. */ 409958bfbb51SPaul Moore if (!selinux_policycap_netpeer) 4100d8395c87SPaul Moore return selinux_sock_rcv_skb_compat(sk, skb, family); 4101d8395c87SPaul Moore 4102d8395c87SPaul Moore secmark_active = selinux_secmark_enabled(); 4103d8395c87SPaul Moore peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled(); 4104d8395c87SPaul Moore if (!secmark_active && !peerlbl_active) 4105d8395c87SPaul Moore return 0; 4106d8395c87SPaul Moore 4107*2bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, NET); 4108da5645a2SPaul Moore ad.u.net.netif = skb->iif; 41094e5ab4cbSJames Morris ad.u.net.family = family; 4110224dfbd8SPaul Moore err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL); 41114e5ab4cbSJames Morris if (err) 4112220deb96SPaul Moore return err; 41134e5ab4cbSJames Morris 4114d8395c87SPaul Moore if (peerlbl_active) { 4115d621d35eSPaul Moore u32 peer_sid; 4116220deb96SPaul Moore 4117220deb96SPaul Moore err = selinux_skb_peerlbl_sid(skb, family, &peer_sid); 4118220deb96SPaul Moore if (err) 4119220deb96SPaul Moore return err; 4120effad8dfSPaul Moore err = selinux_inet_sys_rcv_skb(skb->iif, addrp, family, 4121effad8dfSPaul Moore peer_sid, &ad); 4122dfaebe98SPaul Moore if (err) { 4123dfaebe98SPaul Moore selinux_netlbl_err(skb, err, 0); 4124effad8dfSPaul Moore return err; 4125dfaebe98SPaul Moore } 4126d621d35eSPaul Moore err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER, 4127d621d35eSPaul Moore PEER__RECV, &ad); 4128dfaebe98SPaul Moore if (err) 4129dfaebe98SPaul Moore selinux_netlbl_err(skb, err, 0); 4130d621d35eSPaul Moore } 4131d621d35eSPaul Moore 4132d8395c87SPaul Moore if (secmark_active) { 4133effad8dfSPaul Moore err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET, 4134effad8dfSPaul Moore PACKET__RECV, &ad); 4135effad8dfSPaul Moore if (err) 4136effad8dfSPaul Moore return err; 4137effad8dfSPaul Moore } 4138effad8dfSPaul Moore 4139d621d35eSPaul Moore return err; 41401da177e4SLinus Torvalds } 41411da177e4SLinus Torvalds 41422c7946a7SCatherine Zhang static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval, 41431da177e4SLinus Torvalds int __user *optlen, unsigned len) 41441da177e4SLinus Torvalds { 41451da177e4SLinus Torvalds int err = 0; 41461da177e4SLinus Torvalds char *scontext; 41471da177e4SLinus Torvalds u32 scontext_len; 41481da177e4SLinus Torvalds struct sk_security_struct *ssec; 41491da177e4SLinus Torvalds struct inode_security_struct *isec; 41503de4bab5SPaul Moore u32 peer_sid = SECSID_NULL; 41511da177e4SLinus Torvalds 41521da177e4SLinus Torvalds isec = SOCK_INODE(sock)->i_security; 41532c7946a7SCatherine Zhang 41543de4bab5SPaul Moore if (isec->sclass == SECCLASS_UNIX_STREAM_SOCKET || 41553de4bab5SPaul Moore isec->sclass == SECCLASS_TCP_SOCKET) { 41566b877699SVenkat Yekkirala ssec = sock->sk->sk_security; 41576b877699SVenkat Yekkirala peer_sid = ssec->peer_sid; 41586b877699SVenkat Yekkirala } 41592c7946a7SCatherine Zhang if (peer_sid == SECSID_NULL) { 41602c7946a7SCatherine Zhang err = -ENOPROTOOPT; 41612c7946a7SCatherine Zhang goto out; 41622c7946a7SCatherine Zhang } 41631da177e4SLinus Torvalds 41642c7946a7SCatherine Zhang err = security_sid_to_context(peer_sid, &scontext, &scontext_len); 41651da177e4SLinus Torvalds 41661da177e4SLinus Torvalds if (err) 41671da177e4SLinus Torvalds goto out; 41681da177e4SLinus Torvalds 41691da177e4SLinus Torvalds if (scontext_len > len) { 41701da177e4SLinus Torvalds err = -ERANGE; 41711da177e4SLinus Torvalds goto out_len; 41721da177e4SLinus Torvalds } 41731da177e4SLinus Torvalds 41741da177e4SLinus Torvalds if (copy_to_user(optval, scontext, scontext_len)) 41751da177e4SLinus Torvalds err = -EFAULT; 41761da177e4SLinus Torvalds 41771da177e4SLinus Torvalds out_len: 41781da177e4SLinus Torvalds if (put_user(scontext_len, optlen)) 41791da177e4SLinus Torvalds err = -EFAULT; 41801da177e4SLinus Torvalds 41811da177e4SLinus Torvalds kfree(scontext); 41821da177e4SLinus Torvalds out: 41831da177e4SLinus Torvalds return err; 41841da177e4SLinus Torvalds } 41851da177e4SLinus Torvalds 4186dc49c1f9SCatherine Zhang static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid) 41872c7946a7SCatherine Zhang { 4188dc49c1f9SCatherine Zhang u32 peer_secid = SECSID_NULL; 418975e22910SPaul Moore u16 family; 4190877ce7c1SCatherine Zhang 4191aa862900SPaul Moore if (skb && skb->protocol == htons(ETH_P_IP)) 4192aa862900SPaul Moore family = PF_INET; 4193aa862900SPaul Moore else if (skb && skb->protocol == htons(ETH_P_IPV6)) 4194aa862900SPaul Moore family = PF_INET6; 4195aa862900SPaul Moore else if (sock) 419675e22910SPaul Moore family = sock->sk->sk_family; 419775e22910SPaul Moore else 419875e22910SPaul Moore goto out; 419975e22910SPaul Moore 420075e22910SPaul Moore if (sock && family == PF_UNIX) 4201713a04aeSAhmed S. Darwish selinux_inode_getsecid(SOCK_INODE(sock), &peer_secid); 42023de4bab5SPaul Moore else if (skb) 4203220deb96SPaul Moore selinux_skb_peerlbl_sid(skb, family, &peer_secid); 42042c7946a7SCatherine Zhang 420575e22910SPaul Moore out: 4206dc49c1f9SCatherine Zhang *secid = peer_secid; 420775e22910SPaul Moore if (peer_secid == SECSID_NULL) 420875e22910SPaul Moore return -EINVAL; 420975e22910SPaul Moore return 0; 42102c7946a7SCatherine Zhang } 42112c7946a7SCatherine Zhang 42127d877f3bSAl Viro static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority) 42131da177e4SLinus Torvalds { 42141da177e4SLinus Torvalds return sk_alloc_security(sk, family, priority); 42151da177e4SLinus Torvalds } 42161da177e4SLinus Torvalds 42171da177e4SLinus Torvalds static void selinux_sk_free_security(struct sock *sk) 42181da177e4SLinus Torvalds { 42191da177e4SLinus Torvalds sk_free_security(sk); 42201da177e4SLinus Torvalds } 42211da177e4SLinus Torvalds 4222892c141eSVenkat Yekkirala static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk) 4223892c141eSVenkat Yekkirala { 4224892c141eSVenkat Yekkirala struct sk_security_struct *ssec = sk->sk_security; 4225892c141eSVenkat Yekkirala struct sk_security_struct *newssec = newsk->sk_security; 4226892c141eSVenkat Yekkirala 4227892c141eSVenkat Yekkirala newssec->sid = ssec->sid; 4228892c141eSVenkat Yekkirala newssec->peer_sid = ssec->peer_sid; 4229220deb96SPaul Moore newssec->sclass = ssec->sclass; 423099f59ed0SPaul Moore 4231389fb800SPaul Moore selinux_netlbl_sk_security_reset(newssec); 4232892c141eSVenkat Yekkirala } 4233892c141eSVenkat Yekkirala 4234beb8d13bSVenkat Yekkirala static void selinux_sk_getsecid(struct sock *sk, u32 *secid) 4235d28d1e08STrent Jaeger { 4236d28d1e08STrent Jaeger if (!sk) 4237beb8d13bSVenkat Yekkirala *secid = SECINITSID_ANY_SOCKET; 4238892c141eSVenkat Yekkirala else { 4239892c141eSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 4240d28d1e08STrent Jaeger 4241beb8d13bSVenkat Yekkirala *secid = sksec->sid; 4242892c141eSVenkat Yekkirala } 4243d28d1e08STrent Jaeger } 4244d28d1e08STrent Jaeger 42459a673e56SAdrian Bunk static void selinux_sock_graft(struct sock *sk, struct socket *parent) 42464237c75cSVenkat Yekkirala { 42474237c75cSVenkat Yekkirala struct inode_security_struct *isec = SOCK_INODE(parent)->i_security; 42484237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 42494237c75cSVenkat Yekkirala 42502148ccc4SDavid Woodhouse if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 || 42512148ccc4SDavid Woodhouse sk->sk_family == PF_UNIX) 42524237c75cSVenkat Yekkirala isec->sid = sksec->sid; 4253220deb96SPaul Moore sksec->sclass = isec->sclass; 42544237c75cSVenkat Yekkirala } 42554237c75cSVenkat Yekkirala 42569a673e56SAdrian Bunk static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb, 42574237c75cSVenkat Yekkirala struct request_sock *req) 42584237c75cSVenkat Yekkirala { 42594237c75cSVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 42604237c75cSVenkat Yekkirala int err; 4261aa862900SPaul Moore u16 family = sk->sk_family; 42627420ed23SVenkat Yekkirala u32 newsid; 42634237c75cSVenkat Yekkirala u32 peersid; 42644237c75cSVenkat Yekkirala 4265aa862900SPaul Moore /* handle mapped IPv4 packets arriving via IPv6 sockets */ 4266aa862900SPaul Moore if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 4267aa862900SPaul Moore family = PF_INET; 4268aa862900SPaul Moore 4269aa862900SPaul Moore err = selinux_skb_peerlbl_sid(skb, family, &peersid); 4270220deb96SPaul Moore if (err) 4271220deb96SPaul Moore return err; 4272a51c64f1SVenkat Yekkirala if (peersid == SECSID_NULL) { 4273a51c64f1SVenkat Yekkirala req->secid = sksec->sid; 42743de4bab5SPaul Moore req->peer_secid = SECSID_NULL; 4275389fb800SPaul Moore } else { 42764237c75cSVenkat Yekkirala err = security_sid_mls_copy(sksec->sid, peersid, &newsid); 42774237c75cSVenkat Yekkirala if (err) 42784237c75cSVenkat Yekkirala return err; 42794237c75cSVenkat Yekkirala req->secid = newsid; 42806b877699SVenkat Yekkirala req->peer_secid = peersid; 4281389fb800SPaul Moore } 4282389fb800SPaul Moore 4283389fb800SPaul Moore return selinux_netlbl_inet_conn_request(req, family); 42844237c75cSVenkat Yekkirala } 42854237c75cSVenkat Yekkirala 42869a673e56SAdrian Bunk static void selinux_inet_csk_clone(struct sock *newsk, 42879a673e56SAdrian Bunk const struct request_sock *req) 42884237c75cSVenkat Yekkirala { 42894237c75cSVenkat Yekkirala struct sk_security_struct *newsksec = newsk->sk_security; 42904237c75cSVenkat Yekkirala 42914237c75cSVenkat Yekkirala newsksec->sid = req->secid; 42926b877699SVenkat Yekkirala newsksec->peer_sid = req->peer_secid; 42934237c75cSVenkat Yekkirala /* NOTE: Ideally, we should also get the isec->sid for the 42944237c75cSVenkat Yekkirala new socket in sync, but we don't have the isec available yet. 42954237c75cSVenkat Yekkirala So we will wait until sock_graft to do it, by which 42964237c75cSVenkat Yekkirala time it will have been created and available. */ 429799f59ed0SPaul Moore 42989f2ad665SPaul Moore /* We don't need to take any sort of lock here as we are the only 42999f2ad665SPaul Moore * thread with access to newsksec */ 4300389fb800SPaul Moore selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family); 43014237c75cSVenkat Yekkirala } 43024237c75cSVenkat Yekkirala 4303014ab19aSPaul Moore static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb) 43046b877699SVenkat Yekkirala { 4305aa862900SPaul Moore u16 family = sk->sk_family; 43066b877699SVenkat Yekkirala struct sk_security_struct *sksec = sk->sk_security; 43076b877699SVenkat Yekkirala 4308aa862900SPaul Moore /* handle mapped IPv4 packets arriving via IPv6 sockets */ 4309aa862900SPaul Moore if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP)) 4310aa862900SPaul Moore family = PF_INET; 4311aa862900SPaul Moore 4312aa862900SPaul Moore selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid); 43136b877699SVenkat Yekkirala } 43146b877699SVenkat Yekkirala 43159a673e56SAdrian Bunk static void selinux_req_classify_flow(const struct request_sock *req, 43169a673e56SAdrian Bunk struct flowi *fl) 43174237c75cSVenkat Yekkirala { 43184237c75cSVenkat Yekkirala fl->secid = req->secid; 43194237c75cSVenkat Yekkirala } 43204237c75cSVenkat Yekkirala 43211da177e4SLinus Torvalds static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb) 43221da177e4SLinus Torvalds { 43231da177e4SLinus Torvalds int err = 0; 43241da177e4SLinus Torvalds u32 perm; 43251da177e4SLinus Torvalds struct nlmsghdr *nlh; 43261da177e4SLinus Torvalds struct socket *sock = sk->sk_socket; 43271da177e4SLinus Torvalds struct inode_security_struct *isec = SOCK_INODE(sock)->i_security; 43281da177e4SLinus Torvalds 43291da177e4SLinus Torvalds if (skb->len < NLMSG_SPACE(0)) { 43301da177e4SLinus Torvalds err = -EINVAL; 43311da177e4SLinus Torvalds goto out; 43321da177e4SLinus Torvalds } 4333b529ccf2SArnaldo Carvalho de Melo nlh = nlmsg_hdr(skb); 43341da177e4SLinus Torvalds 43351da177e4SLinus Torvalds err = selinux_nlmsg_lookup(isec->sclass, nlh->nlmsg_type, &perm); 43361da177e4SLinus Torvalds if (err) { 43371da177e4SLinus Torvalds if (err == -EINVAL) { 43389ad9ad38SDavid Woodhouse audit_log(current->audit_context, GFP_KERNEL, AUDIT_SELINUX_ERR, 43391da177e4SLinus Torvalds "SELinux: unrecognized netlink message" 43401da177e4SLinus Torvalds " type=%hu for sclass=%hu\n", 43411da177e4SLinus Torvalds nlh->nlmsg_type, isec->sclass); 434239c9aedeSEric Paris if (!selinux_enforcing || security_get_allow_unknown()) 43431da177e4SLinus Torvalds err = 0; 43441da177e4SLinus Torvalds } 43451da177e4SLinus Torvalds 43461da177e4SLinus Torvalds /* Ignore */ 43471da177e4SLinus Torvalds if (err == -ENOENT) 43481da177e4SLinus Torvalds err = 0; 43491da177e4SLinus Torvalds goto out; 43501da177e4SLinus Torvalds } 43511da177e4SLinus Torvalds 43521da177e4SLinus Torvalds err = socket_has_perm(current, sock, perm); 43531da177e4SLinus Torvalds out: 43541da177e4SLinus Torvalds return err; 43551da177e4SLinus Torvalds } 43561da177e4SLinus Torvalds 43571da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER 43581da177e4SLinus Torvalds 4359effad8dfSPaul Moore static unsigned int selinux_ip_forward(struct sk_buff *skb, int ifindex, 4360effad8dfSPaul Moore u16 family) 43611da177e4SLinus Torvalds { 4362dfaebe98SPaul Moore int err; 4363effad8dfSPaul Moore char *addrp; 4364effad8dfSPaul Moore u32 peer_sid; 4365*2bf49690SThomas Liu struct common_audit_data ad; 4366effad8dfSPaul Moore u8 secmark_active; 4367948bf85cSPaul Moore u8 netlbl_active; 4368effad8dfSPaul Moore u8 peerlbl_active; 43694237c75cSVenkat Yekkirala 4370effad8dfSPaul Moore if (!selinux_policycap_netpeer) 4371effad8dfSPaul Moore return NF_ACCEPT; 43724237c75cSVenkat Yekkirala 4373effad8dfSPaul Moore secmark_active = selinux_secmark_enabled(); 4374948bf85cSPaul Moore netlbl_active = netlbl_enabled(); 4375948bf85cSPaul Moore peerlbl_active = netlbl_active || selinux_xfrm_enabled(); 4376effad8dfSPaul Moore if (!secmark_active && !peerlbl_active) 4377effad8dfSPaul Moore return NF_ACCEPT; 43784237c75cSVenkat Yekkirala 4379d8395c87SPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0) 4380d8395c87SPaul Moore return NF_DROP; 4381d8395c87SPaul Moore 4382*2bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, NET); 4383effad8dfSPaul Moore ad.u.net.netif = ifindex; 4384effad8dfSPaul Moore ad.u.net.family = family; 4385effad8dfSPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0) 4386effad8dfSPaul Moore return NF_DROP; 43871da177e4SLinus Torvalds 4388dfaebe98SPaul Moore if (peerlbl_active) { 4389dfaebe98SPaul Moore err = selinux_inet_sys_rcv_skb(ifindex, addrp, family, 4390dfaebe98SPaul Moore peer_sid, &ad); 4391dfaebe98SPaul Moore if (err) { 4392dfaebe98SPaul Moore selinux_netlbl_err(skb, err, 1); 4393effad8dfSPaul Moore return NF_DROP; 4394dfaebe98SPaul Moore } 4395dfaebe98SPaul Moore } 4396effad8dfSPaul Moore 4397effad8dfSPaul Moore if (secmark_active) 4398effad8dfSPaul Moore if (avc_has_perm(peer_sid, skb->secmark, 4399effad8dfSPaul Moore SECCLASS_PACKET, PACKET__FORWARD_IN, &ad)) 4400effad8dfSPaul Moore return NF_DROP; 4401effad8dfSPaul Moore 4402948bf85cSPaul Moore if (netlbl_active) 4403948bf85cSPaul Moore /* we do this in the FORWARD path and not the POST_ROUTING 4404948bf85cSPaul Moore * path because we want to make sure we apply the necessary 4405948bf85cSPaul Moore * labeling before IPsec is applied so we can leverage AH 4406948bf85cSPaul Moore * protection */ 4407948bf85cSPaul Moore if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0) 4408948bf85cSPaul Moore return NF_DROP; 4409948bf85cSPaul Moore 4410effad8dfSPaul Moore return NF_ACCEPT; 4411effad8dfSPaul Moore } 4412effad8dfSPaul Moore 4413effad8dfSPaul Moore static unsigned int selinux_ipv4_forward(unsigned int hooknum, 4414effad8dfSPaul Moore struct sk_buff *skb, 4415effad8dfSPaul Moore const struct net_device *in, 4416effad8dfSPaul Moore const struct net_device *out, 4417effad8dfSPaul Moore int (*okfn)(struct sk_buff *)) 4418effad8dfSPaul Moore { 4419effad8dfSPaul Moore return selinux_ip_forward(skb, in->ifindex, PF_INET); 4420effad8dfSPaul Moore } 4421effad8dfSPaul Moore 4422effad8dfSPaul Moore #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 4423effad8dfSPaul Moore static unsigned int selinux_ipv6_forward(unsigned int hooknum, 4424effad8dfSPaul Moore struct sk_buff *skb, 4425effad8dfSPaul Moore const struct net_device *in, 4426effad8dfSPaul Moore const struct net_device *out, 4427effad8dfSPaul Moore int (*okfn)(struct sk_buff *)) 4428effad8dfSPaul Moore { 4429effad8dfSPaul Moore return selinux_ip_forward(skb, in->ifindex, PF_INET6); 4430effad8dfSPaul Moore } 4431effad8dfSPaul Moore #endif /* IPV6 */ 4432effad8dfSPaul Moore 4433948bf85cSPaul Moore static unsigned int selinux_ip_output(struct sk_buff *skb, 4434948bf85cSPaul Moore u16 family) 4435948bf85cSPaul Moore { 4436948bf85cSPaul Moore u32 sid; 4437948bf85cSPaul Moore 4438948bf85cSPaul Moore if (!netlbl_enabled()) 4439948bf85cSPaul Moore return NF_ACCEPT; 4440948bf85cSPaul Moore 4441948bf85cSPaul Moore /* we do this in the LOCAL_OUT path and not the POST_ROUTING path 4442948bf85cSPaul Moore * because we want to make sure we apply the necessary labeling 4443948bf85cSPaul Moore * before IPsec is applied so we can leverage AH protection */ 4444948bf85cSPaul Moore if (skb->sk) { 4445948bf85cSPaul Moore struct sk_security_struct *sksec = skb->sk->sk_security; 4446948bf85cSPaul Moore sid = sksec->sid; 4447948bf85cSPaul Moore } else 4448948bf85cSPaul Moore sid = SECINITSID_KERNEL; 4449948bf85cSPaul Moore if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0) 4450948bf85cSPaul Moore return NF_DROP; 4451948bf85cSPaul Moore 4452948bf85cSPaul Moore return NF_ACCEPT; 4453948bf85cSPaul Moore } 4454948bf85cSPaul Moore 4455948bf85cSPaul Moore static unsigned int selinux_ipv4_output(unsigned int hooknum, 4456948bf85cSPaul Moore struct sk_buff *skb, 4457948bf85cSPaul Moore const struct net_device *in, 4458948bf85cSPaul Moore const struct net_device *out, 4459948bf85cSPaul Moore int (*okfn)(struct sk_buff *)) 4460948bf85cSPaul Moore { 4461948bf85cSPaul Moore return selinux_ip_output(skb, PF_INET); 4462948bf85cSPaul Moore } 4463948bf85cSPaul Moore 4464effad8dfSPaul Moore static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb, 4465effad8dfSPaul Moore int ifindex, 4466d8395c87SPaul Moore u16 family) 44674e5ab4cbSJames Morris { 4468effad8dfSPaul Moore struct sock *sk = skb->sk; 44694237c75cSVenkat Yekkirala struct sk_security_struct *sksec; 4470*2bf49690SThomas Liu struct common_audit_data ad; 4471d8395c87SPaul Moore char *addrp; 4472d8395c87SPaul Moore u8 proto; 44734e5ab4cbSJames Morris 4474effad8dfSPaul Moore if (sk == NULL) 4475effad8dfSPaul Moore return NF_ACCEPT; 44764237c75cSVenkat Yekkirala sksec = sk->sk_security; 44774e5ab4cbSJames Morris 4478*2bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, NET); 4479d8395c87SPaul Moore ad.u.net.netif = ifindex; 4480d8395c87SPaul Moore ad.u.net.family = family; 4481d8395c87SPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto)) 4482d8395c87SPaul Moore return NF_DROP; 4483d8395c87SPaul Moore 448458bfbb51SPaul Moore if (selinux_secmark_enabled()) 4485effad8dfSPaul Moore if (avc_has_perm(sksec->sid, skb->secmark, 4486d8395c87SPaul Moore SECCLASS_PACKET, PACKET__SEND, &ad)) 4487effad8dfSPaul Moore return NF_DROP; 44881da177e4SLinus Torvalds 4489effad8dfSPaul Moore if (selinux_policycap_netpeer) 4490d8395c87SPaul Moore if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto)) 4491effad8dfSPaul Moore return NF_DROP; 4492effad8dfSPaul Moore 4493effad8dfSPaul Moore return NF_ACCEPT; 4494effad8dfSPaul Moore } 4495effad8dfSPaul Moore 4496effad8dfSPaul Moore static unsigned int selinux_ip_postroute(struct sk_buff *skb, int ifindex, 4497effad8dfSPaul Moore u16 family) 4498effad8dfSPaul Moore { 4499effad8dfSPaul Moore u32 secmark_perm; 4500effad8dfSPaul Moore u32 peer_sid; 4501effad8dfSPaul Moore struct sock *sk; 4502*2bf49690SThomas Liu struct common_audit_data ad; 4503effad8dfSPaul Moore char *addrp; 4504effad8dfSPaul Moore u8 secmark_active; 4505effad8dfSPaul Moore u8 peerlbl_active; 4506effad8dfSPaul Moore 4507effad8dfSPaul Moore /* If any sort of compatibility mode is enabled then handoff processing 4508effad8dfSPaul Moore * to the selinux_ip_postroute_compat() function to deal with the 4509effad8dfSPaul Moore * special handling. We do this in an attempt to keep this function 4510effad8dfSPaul Moore * as fast and as clean as possible. */ 451158bfbb51SPaul Moore if (!selinux_policycap_netpeer) 4512d8395c87SPaul Moore return selinux_ip_postroute_compat(skb, ifindex, family); 4513def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM 4514effad8dfSPaul Moore /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec 4515effad8dfSPaul Moore * packet transformation so allow the packet to pass without any checks 4516effad8dfSPaul Moore * since we'll have another chance to perform access control checks 4517effad8dfSPaul Moore * when the packet is on it's final way out. 4518effad8dfSPaul Moore * NOTE: there appear to be some IPv6 multicast cases where skb->dst 4519effad8dfSPaul Moore * is NULL, in this case go ahead and apply access control. */ 4520adf30907SEric Dumazet if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL) 4521effad8dfSPaul Moore return NF_ACCEPT; 4522def8b4faSAlexey Dobriyan #endif 4523effad8dfSPaul Moore secmark_active = selinux_secmark_enabled(); 4524effad8dfSPaul Moore peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled(); 4525effad8dfSPaul Moore if (!secmark_active && !peerlbl_active) 4526effad8dfSPaul Moore return NF_ACCEPT; 4527effad8dfSPaul Moore 4528d8395c87SPaul Moore /* if the packet is being forwarded then get the peer label from the 4529d8395c87SPaul Moore * packet itself; otherwise check to see if it is from a local 4530d8395c87SPaul Moore * application or the kernel, if from an application get the peer label 4531d8395c87SPaul Moore * from the sending socket, otherwise use the kernel's sid */ 4532effad8dfSPaul Moore sk = skb->sk; 4533d8395c87SPaul Moore if (sk == NULL) { 4534d8395c87SPaul Moore switch (family) { 4535d8395c87SPaul Moore case PF_INET: 4536d8395c87SPaul Moore if (IPCB(skb)->flags & IPSKB_FORWARDED) 4537d8395c87SPaul Moore secmark_perm = PACKET__FORWARD_OUT; 4538d8395c87SPaul Moore else 4539d8395c87SPaul Moore secmark_perm = PACKET__SEND; 4540d8395c87SPaul Moore break; 4541d8395c87SPaul Moore case PF_INET6: 4542d8395c87SPaul Moore if (IP6CB(skb)->flags & IP6SKB_FORWARDED) 4543d8395c87SPaul Moore secmark_perm = PACKET__FORWARD_OUT; 4544d8395c87SPaul Moore else 4545d8395c87SPaul Moore secmark_perm = PACKET__SEND; 4546d8395c87SPaul Moore break; 4547d8395c87SPaul Moore default: 4548d8395c87SPaul Moore return NF_DROP; 4549d8395c87SPaul Moore } 4550d8395c87SPaul Moore if (secmark_perm == PACKET__FORWARD_OUT) { 4551d8395c87SPaul Moore if (selinux_skb_peerlbl_sid(skb, family, &peer_sid)) 4552d8395c87SPaul Moore return NF_DROP; 4553d8395c87SPaul Moore } else 4554d8395c87SPaul Moore peer_sid = SECINITSID_KERNEL; 4555d8395c87SPaul Moore } else { 4556effad8dfSPaul Moore struct sk_security_struct *sksec = sk->sk_security; 4557effad8dfSPaul Moore peer_sid = sksec->sid; 4558effad8dfSPaul Moore secmark_perm = PACKET__SEND; 4559effad8dfSPaul Moore } 4560effad8dfSPaul Moore 4561*2bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, NET); 4562d8395c87SPaul Moore ad.u.net.netif = ifindex; 4563d8395c87SPaul Moore ad.u.net.family = family; 4564d8395c87SPaul Moore if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL)) 4565d8395c87SPaul Moore return NF_DROP; 4566d8395c87SPaul Moore 4567effad8dfSPaul Moore if (secmark_active) 4568effad8dfSPaul Moore if (avc_has_perm(peer_sid, skb->secmark, 4569effad8dfSPaul Moore SECCLASS_PACKET, secmark_perm, &ad)) 4570effad8dfSPaul Moore return NF_DROP; 4571effad8dfSPaul Moore 4572effad8dfSPaul Moore if (peerlbl_active) { 4573effad8dfSPaul Moore u32 if_sid; 4574effad8dfSPaul Moore u32 node_sid; 4575effad8dfSPaul Moore 4576effad8dfSPaul Moore if (sel_netif_sid(ifindex, &if_sid)) 4577effad8dfSPaul Moore return NF_DROP; 4578effad8dfSPaul Moore if (avc_has_perm(peer_sid, if_sid, 4579effad8dfSPaul Moore SECCLASS_NETIF, NETIF__EGRESS, &ad)) 4580effad8dfSPaul Moore return NF_DROP; 4581effad8dfSPaul Moore 4582effad8dfSPaul Moore if (sel_netnode_sid(addrp, family, &node_sid)) 4583effad8dfSPaul Moore return NF_DROP; 4584effad8dfSPaul Moore if (avc_has_perm(peer_sid, node_sid, 4585effad8dfSPaul Moore SECCLASS_NODE, NODE__SENDTO, &ad)) 4586effad8dfSPaul Moore return NF_DROP; 4587effad8dfSPaul Moore } 4588effad8dfSPaul Moore 4589effad8dfSPaul Moore return NF_ACCEPT; 4590effad8dfSPaul Moore } 4591effad8dfSPaul Moore 4592effad8dfSPaul Moore static unsigned int selinux_ipv4_postroute(unsigned int hooknum, 4593a224be76SDavid S. Miller struct sk_buff *skb, 45941da177e4SLinus Torvalds const struct net_device *in, 45951da177e4SLinus Torvalds const struct net_device *out, 45961da177e4SLinus Torvalds int (*okfn)(struct sk_buff *)) 45971da177e4SLinus Torvalds { 4598effad8dfSPaul Moore return selinux_ip_postroute(skb, out->ifindex, PF_INET); 45991da177e4SLinus Torvalds } 46001da177e4SLinus Torvalds 46011da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 4602effad8dfSPaul Moore static unsigned int selinux_ipv6_postroute(unsigned int hooknum, 4603a224be76SDavid S. Miller struct sk_buff *skb, 46041da177e4SLinus Torvalds const struct net_device *in, 46051da177e4SLinus Torvalds const struct net_device *out, 46061da177e4SLinus Torvalds int (*okfn)(struct sk_buff *)) 46071da177e4SLinus Torvalds { 4608effad8dfSPaul Moore return selinux_ip_postroute(skb, out->ifindex, PF_INET6); 46091da177e4SLinus Torvalds } 46101da177e4SLinus Torvalds #endif /* IPV6 */ 46111da177e4SLinus Torvalds 46121da177e4SLinus Torvalds #endif /* CONFIG_NETFILTER */ 46131da177e4SLinus Torvalds 46141da177e4SLinus Torvalds static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb) 46151da177e4SLinus Torvalds { 46161da177e4SLinus Torvalds int err; 46171da177e4SLinus Torvalds 4618200ac532SEric Paris err = cap_netlink_send(sk, skb); 46191da177e4SLinus Torvalds if (err) 46201da177e4SLinus Torvalds return err; 46211da177e4SLinus Torvalds 46221da177e4SLinus Torvalds if (policydb_loaded_version >= POLICYDB_VERSION_NLCLASS) 46231da177e4SLinus Torvalds err = selinux_nlmsg_perm(sk, skb); 46241da177e4SLinus Torvalds 46251da177e4SLinus Torvalds return err; 46261da177e4SLinus Torvalds } 46271da177e4SLinus Torvalds 4628c7bdb545SDarrel Goeddel static int selinux_netlink_recv(struct sk_buff *skb, int capability) 46291da177e4SLinus Torvalds { 4630c7bdb545SDarrel Goeddel int err; 4631*2bf49690SThomas Liu struct common_audit_data ad; 4632c7bdb545SDarrel Goeddel 4633200ac532SEric Paris err = cap_netlink_recv(skb, capability); 4634c7bdb545SDarrel Goeddel if (err) 4635c7bdb545SDarrel Goeddel return err; 4636c7bdb545SDarrel Goeddel 4637*2bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, CAP); 4638c7bdb545SDarrel Goeddel ad.u.cap = capability; 4639c7bdb545SDarrel Goeddel 4640c7bdb545SDarrel Goeddel return avc_has_perm(NETLINK_CB(skb).sid, NETLINK_CB(skb).sid, 4641c7bdb545SDarrel Goeddel SECCLASS_CAPABILITY, CAP_TO_MASK(capability), &ad); 46421da177e4SLinus Torvalds } 46431da177e4SLinus Torvalds 46441da177e4SLinus Torvalds static int ipc_alloc_security(struct task_struct *task, 46451da177e4SLinus Torvalds struct kern_ipc_perm *perm, 46461da177e4SLinus Torvalds u16 sclass) 46471da177e4SLinus Torvalds { 46481da177e4SLinus Torvalds struct ipc_security_struct *isec; 4649275bb41eSDavid Howells u32 sid; 46501da177e4SLinus Torvalds 465189d155efSJames Morris isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL); 46521da177e4SLinus Torvalds if (!isec) 46531da177e4SLinus Torvalds return -ENOMEM; 46541da177e4SLinus Torvalds 4655275bb41eSDavid Howells sid = task_sid(task); 46561da177e4SLinus Torvalds isec->sclass = sclass; 4657275bb41eSDavid Howells isec->sid = sid; 46581da177e4SLinus Torvalds perm->security = isec; 46591da177e4SLinus Torvalds 46601da177e4SLinus Torvalds return 0; 46611da177e4SLinus Torvalds } 46621da177e4SLinus Torvalds 46631da177e4SLinus Torvalds static void ipc_free_security(struct kern_ipc_perm *perm) 46641da177e4SLinus Torvalds { 46651da177e4SLinus Torvalds struct ipc_security_struct *isec = perm->security; 46661da177e4SLinus Torvalds perm->security = NULL; 46671da177e4SLinus Torvalds kfree(isec); 46681da177e4SLinus Torvalds } 46691da177e4SLinus Torvalds 46701da177e4SLinus Torvalds static int msg_msg_alloc_security(struct msg_msg *msg) 46711da177e4SLinus Torvalds { 46721da177e4SLinus Torvalds struct msg_security_struct *msec; 46731da177e4SLinus Torvalds 467489d155efSJames Morris msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL); 46751da177e4SLinus Torvalds if (!msec) 46761da177e4SLinus Torvalds return -ENOMEM; 46771da177e4SLinus Torvalds 46781da177e4SLinus Torvalds msec->sid = SECINITSID_UNLABELED; 46791da177e4SLinus Torvalds msg->security = msec; 46801da177e4SLinus Torvalds 46811da177e4SLinus Torvalds return 0; 46821da177e4SLinus Torvalds } 46831da177e4SLinus Torvalds 46841da177e4SLinus Torvalds static void msg_msg_free_security(struct msg_msg *msg) 46851da177e4SLinus Torvalds { 46861da177e4SLinus Torvalds struct msg_security_struct *msec = msg->security; 46871da177e4SLinus Torvalds 46881da177e4SLinus Torvalds msg->security = NULL; 46891da177e4SLinus Torvalds kfree(msec); 46901da177e4SLinus Torvalds } 46911da177e4SLinus Torvalds 46921da177e4SLinus Torvalds static int ipc_has_perm(struct kern_ipc_perm *ipc_perms, 46936af963f1SStephen Smalley u32 perms) 46941da177e4SLinus Torvalds { 46951da177e4SLinus Torvalds struct ipc_security_struct *isec; 4696*2bf49690SThomas Liu struct common_audit_data ad; 4697275bb41eSDavid Howells u32 sid = current_sid(); 46981da177e4SLinus Torvalds 46991da177e4SLinus Torvalds isec = ipc_perms->security; 47001da177e4SLinus Torvalds 4701*2bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, IPC); 47021da177e4SLinus Torvalds ad.u.ipc_id = ipc_perms->key; 47031da177e4SLinus Torvalds 4704275bb41eSDavid Howells return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad); 47051da177e4SLinus Torvalds } 47061da177e4SLinus Torvalds 47071da177e4SLinus Torvalds static int selinux_msg_msg_alloc_security(struct msg_msg *msg) 47081da177e4SLinus Torvalds { 47091da177e4SLinus Torvalds return msg_msg_alloc_security(msg); 47101da177e4SLinus Torvalds } 47111da177e4SLinus Torvalds 47121da177e4SLinus Torvalds static void selinux_msg_msg_free_security(struct msg_msg *msg) 47131da177e4SLinus Torvalds { 47141da177e4SLinus Torvalds msg_msg_free_security(msg); 47151da177e4SLinus Torvalds } 47161da177e4SLinus Torvalds 47171da177e4SLinus Torvalds /* message queue security operations */ 47181da177e4SLinus Torvalds static int selinux_msg_queue_alloc_security(struct msg_queue *msq) 47191da177e4SLinus Torvalds { 47201da177e4SLinus Torvalds struct ipc_security_struct *isec; 4721*2bf49690SThomas Liu struct common_audit_data ad; 4722275bb41eSDavid Howells u32 sid = current_sid(); 47231da177e4SLinus Torvalds int rc; 47241da177e4SLinus Torvalds 47251da177e4SLinus Torvalds rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ); 47261da177e4SLinus Torvalds if (rc) 47271da177e4SLinus Torvalds return rc; 47281da177e4SLinus Torvalds 47291da177e4SLinus Torvalds isec = msq->q_perm.security; 47301da177e4SLinus Torvalds 4731*2bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, IPC); 47321da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 47331da177e4SLinus Torvalds 4734275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ, 47351da177e4SLinus Torvalds MSGQ__CREATE, &ad); 47361da177e4SLinus Torvalds if (rc) { 47371da177e4SLinus Torvalds ipc_free_security(&msq->q_perm); 47381da177e4SLinus Torvalds return rc; 47391da177e4SLinus Torvalds } 47401da177e4SLinus Torvalds return 0; 47411da177e4SLinus Torvalds } 47421da177e4SLinus Torvalds 47431da177e4SLinus Torvalds static void selinux_msg_queue_free_security(struct msg_queue *msq) 47441da177e4SLinus Torvalds { 47451da177e4SLinus Torvalds ipc_free_security(&msq->q_perm); 47461da177e4SLinus Torvalds } 47471da177e4SLinus Torvalds 47481da177e4SLinus Torvalds static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg) 47491da177e4SLinus Torvalds { 47501da177e4SLinus Torvalds struct ipc_security_struct *isec; 4751*2bf49690SThomas Liu struct common_audit_data ad; 4752275bb41eSDavid Howells u32 sid = current_sid(); 47531da177e4SLinus Torvalds 47541da177e4SLinus Torvalds isec = msq->q_perm.security; 47551da177e4SLinus Torvalds 4756*2bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, IPC); 47571da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 47581da177e4SLinus Torvalds 4759275bb41eSDavid Howells return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ, 47601da177e4SLinus Torvalds MSGQ__ASSOCIATE, &ad); 47611da177e4SLinus Torvalds } 47621da177e4SLinus Torvalds 47631da177e4SLinus Torvalds static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd) 47641da177e4SLinus Torvalds { 47651da177e4SLinus Torvalds int err; 47661da177e4SLinus Torvalds int perms; 47671da177e4SLinus Torvalds 47681da177e4SLinus Torvalds switch (cmd) { 47691da177e4SLinus Torvalds case IPC_INFO: 47701da177e4SLinus Torvalds case MSG_INFO: 47711da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 47721da177e4SLinus Torvalds return task_has_system(current, SYSTEM__IPC_INFO); 47731da177e4SLinus Torvalds case IPC_STAT: 47741da177e4SLinus Torvalds case MSG_STAT: 47751da177e4SLinus Torvalds perms = MSGQ__GETATTR | MSGQ__ASSOCIATE; 47761da177e4SLinus Torvalds break; 47771da177e4SLinus Torvalds case IPC_SET: 47781da177e4SLinus Torvalds perms = MSGQ__SETATTR; 47791da177e4SLinus Torvalds break; 47801da177e4SLinus Torvalds case IPC_RMID: 47811da177e4SLinus Torvalds perms = MSGQ__DESTROY; 47821da177e4SLinus Torvalds break; 47831da177e4SLinus Torvalds default: 47841da177e4SLinus Torvalds return 0; 47851da177e4SLinus Torvalds } 47861da177e4SLinus Torvalds 47876af963f1SStephen Smalley err = ipc_has_perm(&msq->q_perm, perms); 47881da177e4SLinus Torvalds return err; 47891da177e4SLinus Torvalds } 47901da177e4SLinus Torvalds 47911da177e4SLinus Torvalds static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg) 47921da177e4SLinus Torvalds { 47931da177e4SLinus Torvalds struct ipc_security_struct *isec; 47941da177e4SLinus Torvalds struct msg_security_struct *msec; 4795*2bf49690SThomas Liu struct common_audit_data ad; 4796275bb41eSDavid Howells u32 sid = current_sid(); 47971da177e4SLinus Torvalds int rc; 47981da177e4SLinus Torvalds 47991da177e4SLinus Torvalds isec = msq->q_perm.security; 48001da177e4SLinus Torvalds msec = msg->security; 48011da177e4SLinus Torvalds 48021da177e4SLinus Torvalds /* 48031da177e4SLinus Torvalds * First time through, need to assign label to the message 48041da177e4SLinus Torvalds */ 48051da177e4SLinus Torvalds if (msec->sid == SECINITSID_UNLABELED) { 48061da177e4SLinus Torvalds /* 48071da177e4SLinus Torvalds * Compute new sid based on current process and 48081da177e4SLinus Torvalds * message queue this message will be stored in 48091da177e4SLinus Torvalds */ 4810275bb41eSDavid Howells rc = security_transition_sid(sid, isec->sid, SECCLASS_MSG, 48111da177e4SLinus Torvalds &msec->sid); 48121da177e4SLinus Torvalds if (rc) 48131da177e4SLinus Torvalds return rc; 48141da177e4SLinus Torvalds } 48151da177e4SLinus Torvalds 4816*2bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, IPC); 48171da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 48181da177e4SLinus Torvalds 48191da177e4SLinus Torvalds /* Can this process write to the queue? */ 4820275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ, 48211da177e4SLinus Torvalds MSGQ__WRITE, &ad); 48221da177e4SLinus Torvalds if (!rc) 48231da177e4SLinus Torvalds /* Can this process send the message */ 4824275bb41eSDavid Howells rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG, 4825275bb41eSDavid Howells MSG__SEND, &ad); 48261da177e4SLinus Torvalds if (!rc) 48271da177e4SLinus Torvalds /* Can the message be put in the queue? */ 4828275bb41eSDavid Howells rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ, 4829275bb41eSDavid Howells MSGQ__ENQUEUE, &ad); 48301da177e4SLinus Torvalds 48311da177e4SLinus Torvalds return rc; 48321da177e4SLinus Torvalds } 48331da177e4SLinus Torvalds 48341da177e4SLinus Torvalds static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg, 48351da177e4SLinus Torvalds struct task_struct *target, 48361da177e4SLinus Torvalds long type, int mode) 48371da177e4SLinus Torvalds { 48381da177e4SLinus Torvalds struct ipc_security_struct *isec; 48391da177e4SLinus Torvalds struct msg_security_struct *msec; 4840*2bf49690SThomas Liu struct common_audit_data ad; 4841275bb41eSDavid Howells u32 sid = task_sid(target); 48421da177e4SLinus Torvalds int rc; 48431da177e4SLinus Torvalds 48441da177e4SLinus Torvalds isec = msq->q_perm.security; 48451da177e4SLinus Torvalds msec = msg->security; 48461da177e4SLinus Torvalds 4847*2bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, IPC); 48481da177e4SLinus Torvalds ad.u.ipc_id = msq->q_perm.key; 48491da177e4SLinus Torvalds 4850275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, 48511da177e4SLinus Torvalds SECCLASS_MSGQ, MSGQ__READ, &ad); 48521da177e4SLinus Torvalds if (!rc) 4853275bb41eSDavid Howells rc = avc_has_perm(sid, msec->sid, 48541da177e4SLinus Torvalds SECCLASS_MSG, MSG__RECEIVE, &ad); 48551da177e4SLinus Torvalds return rc; 48561da177e4SLinus Torvalds } 48571da177e4SLinus Torvalds 48581da177e4SLinus Torvalds /* Shared Memory security operations */ 48591da177e4SLinus Torvalds static int selinux_shm_alloc_security(struct shmid_kernel *shp) 48601da177e4SLinus Torvalds { 48611da177e4SLinus Torvalds struct ipc_security_struct *isec; 4862*2bf49690SThomas Liu struct common_audit_data ad; 4863275bb41eSDavid Howells u32 sid = current_sid(); 48641da177e4SLinus Torvalds int rc; 48651da177e4SLinus Torvalds 48661da177e4SLinus Torvalds rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM); 48671da177e4SLinus Torvalds if (rc) 48681da177e4SLinus Torvalds return rc; 48691da177e4SLinus Torvalds 48701da177e4SLinus Torvalds isec = shp->shm_perm.security; 48711da177e4SLinus Torvalds 4872*2bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, IPC); 48731da177e4SLinus Torvalds ad.u.ipc_id = shp->shm_perm.key; 48741da177e4SLinus Torvalds 4875275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, SECCLASS_SHM, 48761da177e4SLinus Torvalds SHM__CREATE, &ad); 48771da177e4SLinus Torvalds if (rc) { 48781da177e4SLinus Torvalds ipc_free_security(&shp->shm_perm); 48791da177e4SLinus Torvalds return rc; 48801da177e4SLinus Torvalds } 48811da177e4SLinus Torvalds return 0; 48821da177e4SLinus Torvalds } 48831da177e4SLinus Torvalds 48841da177e4SLinus Torvalds static void selinux_shm_free_security(struct shmid_kernel *shp) 48851da177e4SLinus Torvalds { 48861da177e4SLinus Torvalds ipc_free_security(&shp->shm_perm); 48871da177e4SLinus Torvalds } 48881da177e4SLinus Torvalds 48891da177e4SLinus Torvalds static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg) 48901da177e4SLinus Torvalds { 48911da177e4SLinus Torvalds struct ipc_security_struct *isec; 4892*2bf49690SThomas Liu struct common_audit_data ad; 4893275bb41eSDavid Howells u32 sid = current_sid(); 48941da177e4SLinus Torvalds 48951da177e4SLinus Torvalds isec = shp->shm_perm.security; 48961da177e4SLinus Torvalds 4897*2bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, IPC); 48981da177e4SLinus Torvalds ad.u.ipc_id = shp->shm_perm.key; 48991da177e4SLinus Torvalds 4900275bb41eSDavid Howells return avc_has_perm(sid, isec->sid, SECCLASS_SHM, 49011da177e4SLinus Torvalds SHM__ASSOCIATE, &ad); 49021da177e4SLinus Torvalds } 49031da177e4SLinus Torvalds 49041da177e4SLinus Torvalds /* Note, at this point, shp is locked down */ 49051da177e4SLinus Torvalds static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd) 49061da177e4SLinus Torvalds { 49071da177e4SLinus Torvalds int perms; 49081da177e4SLinus Torvalds int err; 49091da177e4SLinus Torvalds 49101da177e4SLinus Torvalds switch (cmd) { 49111da177e4SLinus Torvalds case IPC_INFO: 49121da177e4SLinus Torvalds case SHM_INFO: 49131da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 49141da177e4SLinus Torvalds return task_has_system(current, SYSTEM__IPC_INFO); 49151da177e4SLinus Torvalds case IPC_STAT: 49161da177e4SLinus Torvalds case SHM_STAT: 49171da177e4SLinus Torvalds perms = SHM__GETATTR | SHM__ASSOCIATE; 49181da177e4SLinus Torvalds break; 49191da177e4SLinus Torvalds case IPC_SET: 49201da177e4SLinus Torvalds perms = SHM__SETATTR; 49211da177e4SLinus Torvalds break; 49221da177e4SLinus Torvalds case SHM_LOCK: 49231da177e4SLinus Torvalds case SHM_UNLOCK: 49241da177e4SLinus Torvalds perms = SHM__LOCK; 49251da177e4SLinus Torvalds break; 49261da177e4SLinus Torvalds case IPC_RMID: 49271da177e4SLinus Torvalds perms = SHM__DESTROY; 49281da177e4SLinus Torvalds break; 49291da177e4SLinus Torvalds default: 49301da177e4SLinus Torvalds return 0; 49311da177e4SLinus Torvalds } 49321da177e4SLinus Torvalds 49336af963f1SStephen Smalley err = ipc_has_perm(&shp->shm_perm, perms); 49341da177e4SLinus Torvalds return err; 49351da177e4SLinus Torvalds } 49361da177e4SLinus Torvalds 49371da177e4SLinus Torvalds static int selinux_shm_shmat(struct shmid_kernel *shp, 49381da177e4SLinus Torvalds char __user *shmaddr, int shmflg) 49391da177e4SLinus Torvalds { 49401da177e4SLinus Torvalds u32 perms; 49411da177e4SLinus Torvalds 49421da177e4SLinus Torvalds if (shmflg & SHM_RDONLY) 49431da177e4SLinus Torvalds perms = SHM__READ; 49441da177e4SLinus Torvalds else 49451da177e4SLinus Torvalds perms = SHM__READ | SHM__WRITE; 49461da177e4SLinus Torvalds 49476af963f1SStephen Smalley return ipc_has_perm(&shp->shm_perm, perms); 49481da177e4SLinus Torvalds } 49491da177e4SLinus Torvalds 49501da177e4SLinus Torvalds /* Semaphore security operations */ 49511da177e4SLinus Torvalds static int selinux_sem_alloc_security(struct sem_array *sma) 49521da177e4SLinus Torvalds { 49531da177e4SLinus Torvalds struct ipc_security_struct *isec; 4954*2bf49690SThomas Liu struct common_audit_data ad; 4955275bb41eSDavid Howells u32 sid = current_sid(); 49561da177e4SLinus Torvalds int rc; 49571da177e4SLinus Torvalds 49581da177e4SLinus Torvalds rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM); 49591da177e4SLinus Torvalds if (rc) 49601da177e4SLinus Torvalds return rc; 49611da177e4SLinus Torvalds 49621da177e4SLinus Torvalds isec = sma->sem_perm.security; 49631da177e4SLinus Torvalds 4964*2bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, IPC); 49651da177e4SLinus Torvalds ad.u.ipc_id = sma->sem_perm.key; 49661da177e4SLinus Torvalds 4967275bb41eSDavid Howells rc = avc_has_perm(sid, isec->sid, SECCLASS_SEM, 49681da177e4SLinus Torvalds SEM__CREATE, &ad); 49691da177e4SLinus Torvalds if (rc) { 49701da177e4SLinus Torvalds ipc_free_security(&sma->sem_perm); 49711da177e4SLinus Torvalds return rc; 49721da177e4SLinus Torvalds } 49731da177e4SLinus Torvalds return 0; 49741da177e4SLinus Torvalds } 49751da177e4SLinus Torvalds 49761da177e4SLinus Torvalds static void selinux_sem_free_security(struct sem_array *sma) 49771da177e4SLinus Torvalds { 49781da177e4SLinus Torvalds ipc_free_security(&sma->sem_perm); 49791da177e4SLinus Torvalds } 49801da177e4SLinus Torvalds 49811da177e4SLinus Torvalds static int selinux_sem_associate(struct sem_array *sma, int semflg) 49821da177e4SLinus Torvalds { 49831da177e4SLinus Torvalds struct ipc_security_struct *isec; 4984*2bf49690SThomas Liu struct common_audit_data ad; 4985275bb41eSDavid Howells u32 sid = current_sid(); 49861da177e4SLinus Torvalds 49871da177e4SLinus Torvalds isec = sma->sem_perm.security; 49881da177e4SLinus Torvalds 4989*2bf49690SThomas Liu COMMON_AUDIT_DATA_INIT(&ad, IPC); 49901da177e4SLinus Torvalds ad.u.ipc_id = sma->sem_perm.key; 49911da177e4SLinus Torvalds 4992275bb41eSDavid Howells return avc_has_perm(sid, isec->sid, SECCLASS_SEM, 49931da177e4SLinus Torvalds SEM__ASSOCIATE, &ad); 49941da177e4SLinus Torvalds } 49951da177e4SLinus Torvalds 49961da177e4SLinus Torvalds /* Note, at this point, sma is locked down */ 49971da177e4SLinus Torvalds static int selinux_sem_semctl(struct sem_array *sma, int cmd) 49981da177e4SLinus Torvalds { 49991da177e4SLinus Torvalds int err; 50001da177e4SLinus Torvalds u32 perms; 50011da177e4SLinus Torvalds 50021da177e4SLinus Torvalds switch (cmd) { 50031da177e4SLinus Torvalds case IPC_INFO: 50041da177e4SLinus Torvalds case SEM_INFO: 50051da177e4SLinus Torvalds /* No specific object, just general system-wide information. */ 50061da177e4SLinus Torvalds return task_has_system(current, SYSTEM__IPC_INFO); 50071da177e4SLinus Torvalds case GETPID: 50081da177e4SLinus Torvalds case GETNCNT: 50091da177e4SLinus Torvalds case GETZCNT: 50101da177e4SLinus Torvalds perms = SEM__GETATTR; 50111da177e4SLinus Torvalds break; 50121da177e4SLinus Torvalds case GETVAL: 50131da177e4SLinus Torvalds case GETALL: 50141da177e4SLinus Torvalds perms = SEM__READ; 50151da177e4SLinus Torvalds break; 50161da177e4SLinus Torvalds case SETVAL: 50171da177e4SLinus Torvalds case SETALL: 50181da177e4SLinus Torvalds perms = SEM__WRITE; 50191da177e4SLinus Torvalds break; 50201da177e4SLinus Torvalds case IPC_RMID: 50211da177e4SLinus Torvalds perms = SEM__DESTROY; 50221da177e4SLinus Torvalds break; 50231da177e4SLinus Torvalds case IPC_SET: 50241da177e4SLinus Torvalds perms = SEM__SETATTR; 50251da177e4SLinus Torvalds break; 50261da177e4SLinus Torvalds case IPC_STAT: 50271da177e4SLinus Torvalds case SEM_STAT: 50281da177e4SLinus Torvalds perms = SEM__GETATTR | SEM__ASSOCIATE; 50291da177e4SLinus Torvalds break; 50301da177e4SLinus Torvalds default: 50311da177e4SLinus Torvalds return 0; 50321da177e4SLinus Torvalds } 50331da177e4SLinus Torvalds 50346af963f1SStephen Smalley err = ipc_has_perm(&sma->sem_perm, perms); 50351da177e4SLinus Torvalds return err; 50361da177e4SLinus Torvalds } 50371da177e4SLinus Torvalds 50381da177e4SLinus Torvalds static int selinux_sem_semop(struct sem_array *sma, 50391da177e4SLinus Torvalds struct sembuf *sops, unsigned nsops, int alter) 50401da177e4SLinus Torvalds { 50411da177e4SLinus Torvalds u32 perms; 50421da177e4SLinus Torvalds 50431da177e4SLinus Torvalds if (alter) 50441da177e4SLinus Torvalds perms = SEM__READ | SEM__WRITE; 50451da177e4SLinus Torvalds else 50461da177e4SLinus Torvalds perms = SEM__READ; 50471da177e4SLinus Torvalds 50486af963f1SStephen Smalley return ipc_has_perm(&sma->sem_perm, perms); 50491da177e4SLinus Torvalds } 50501da177e4SLinus Torvalds 50511da177e4SLinus Torvalds static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag) 50521da177e4SLinus Torvalds { 50531da177e4SLinus Torvalds u32 av = 0; 50541da177e4SLinus Torvalds 50551da177e4SLinus Torvalds av = 0; 50561da177e4SLinus Torvalds if (flag & S_IRUGO) 50571da177e4SLinus Torvalds av |= IPC__UNIX_READ; 50581da177e4SLinus Torvalds if (flag & S_IWUGO) 50591da177e4SLinus Torvalds av |= IPC__UNIX_WRITE; 50601da177e4SLinus Torvalds 50611da177e4SLinus Torvalds if (av == 0) 50621da177e4SLinus Torvalds return 0; 50631da177e4SLinus Torvalds 50646af963f1SStephen Smalley return ipc_has_perm(ipcp, av); 50651da177e4SLinus Torvalds } 50661da177e4SLinus Torvalds 5067713a04aeSAhmed S. Darwish static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid) 5068713a04aeSAhmed S. Darwish { 5069713a04aeSAhmed S. Darwish struct ipc_security_struct *isec = ipcp->security; 5070713a04aeSAhmed S. Darwish *secid = isec->sid; 5071713a04aeSAhmed S. Darwish } 5072713a04aeSAhmed S. Darwish 50731da177e4SLinus Torvalds static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode) 50741da177e4SLinus Torvalds { 50751da177e4SLinus Torvalds if (inode) 50761da177e4SLinus Torvalds inode_doinit_with_dentry(inode, dentry); 50771da177e4SLinus Torvalds } 50781da177e4SLinus Torvalds 50791da177e4SLinus Torvalds static int selinux_getprocattr(struct task_struct *p, 508004ff9708SAl Viro char *name, char **value) 50811da177e4SLinus Torvalds { 5082275bb41eSDavid Howells const struct task_security_struct *__tsec; 50838c8570fbSDustin Kirkland u32 sid; 50841da177e4SLinus Torvalds int error; 508504ff9708SAl Viro unsigned len; 50861da177e4SLinus Torvalds 50871da177e4SLinus Torvalds if (current != p) { 50883b11a1deSDavid Howells error = current_has_perm(p, PROCESS__GETATTR); 50891da177e4SLinus Torvalds if (error) 50901da177e4SLinus Torvalds return error; 50911da177e4SLinus Torvalds } 50921da177e4SLinus Torvalds 5093275bb41eSDavid Howells rcu_read_lock(); 5094275bb41eSDavid Howells __tsec = __task_cred(p)->security; 50951da177e4SLinus Torvalds 50961da177e4SLinus Torvalds if (!strcmp(name, "current")) 5097275bb41eSDavid Howells sid = __tsec->sid; 50981da177e4SLinus Torvalds else if (!strcmp(name, "prev")) 5099275bb41eSDavid Howells sid = __tsec->osid; 51001da177e4SLinus Torvalds else if (!strcmp(name, "exec")) 5101275bb41eSDavid Howells sid = __tsec->exec_sid; 51021da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 5103275bb41eSDavid Howells sid = __tsec->create_sid; 51044eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) 5105275bb41eSDavid Howells sid = __tsec->keycreate_sid; 510642c3e03eSEric Paris else if (!strcmp(name, "sockcreate")) 5107275bb41eSDavid Howells sid = __tsec->sockcreate_sid; 51081da177e4SLinus Torvalds else 5109275bb41eSDavid Howells goto invalid; 5110275bb41eSDavid Howells rcu_read_unlock(); 51111da177e4SLinus Torvalds 51121da177e4SLinus Torvalds if (!sid) 51131da177e4SLinus Torvalds return 0; 51141da177e4SLinus Torvalds 511504ff9708SAl Viro error = security_sid_to_context(sid, value, &len); 511604ff9708SAl Viro if (error) 511704ff9708SAl Viro return error; 511804ff9708SAl Viro return len; 5119275bb41eSDavid Howells 5120275bb41eSDavid Howells invalid: 5121275bb41eSDavid Howells rcu_read_unlock(); 5122275bb41eSDavid Howells return -EINVAL; 51231da177e4SLinus Torvalds } 51241da177e4SLinus Torvalds 51251da177e4SLinus Torvalds static int selinux_setprocattr(struct task_struct *p, 51261da177e4SLinus Torvalds char *name, void *value, size_t size) 51271da177e4SLinus Torvalds { 51281da177e4SLinus Torvalds struct task_security_struct *tsec; 51290356357cSRoland McGrath struct task_struct *tracer; 5130d84f4f99SDavid Howells struct cred *new; 5131d84f4f99SDavid Howells u32 sid = 0, ptsid; 51321da177e4SLinus Torvalds int error; 51331da177e4SLinus Torvalds char *str = value; 51341da177e4SLinus Torvalds 51351da177e4SLinus Torvalds if (current != p) { 51361da177e4SLinus Torvalds /* SELinux only allows a process to change its own 51371da177e4SLinus Torvalds security attributes. */ 51381da177e4SLinus Torvalds return -EACCES; 51391da177e4SLinus Torvalds } 51401da177e4SLinus Torvalds 51411da177e4SLinus Torvalds /* 51421da177e4SLinus Torvalds * Basic control over ability to set these attributes at all. 51431da177e4SLinus Torvalds * current == p, but we'll pass them separately in case the 51441da177e4SLinus Torvalds * above restriction is ever removed. 51451da177e4SLinus Torvalds */ 51461da177e4SLinus Torvalds if (!strcmp(name, "exec")) 51473b11a1deSDavid Howells error = current_has_perm(p, PROCESS__SETEXEC); 51481da177e4SLinus Torvalds else if (!strcmp(name, "fscreate")) 51493b11a1deSDavid Howells error = current_has_perm(p, PROCESS__SETFSCREATE); 51504eb582cfSMichael LeMay else if (!strcmp(name, "keycreate")) 51513b11a1deSDavid Howells error = current_has_perm(p, PROCESS__SETKEYCREATE); 515242c3e03eSEric Paris else if (!strcmp(name, "sockcreate")) 51533b11a1deSDavid Howells error = current_has_perm(p, PROCESS__SETSOCKCREATE); 51541da177e4SLinus Torvalds else if (!strcmp(name, "current")) 51553b11a1deSDavid Howells error = current_has_perm(p, PROCESS__SETCURRENT); 51561da177e4SLinus Torvalds else 51571da177e4SLinus Torvalds error = -EINVAL; 51581da177e4SLinus Torvalds if (error) 51591da177e4SLinus Torvalds return error; 51601da177e4SLinus Torvalds 51611da177e4SLinus Torvalds /* Obtain a SID for the context, if one was specified. */ 51621da177e4SLinus Torvalds if (size && str[1] && str[1] != '\n') { 51631da177e4SLinus Torvalds if (str[size-1] == '\n') { 51641da177e4SLinus Torvalds str[size-1] = 0; 51651da177e4SLinus Torvalds size--; 51661da177e4SLinus Torvalds } 51671da177e4SLinus Torvalds error = security_context_to_sid(value, size, &sid); 516812b29f34SStephen Smalley if (error == -EINVAL && !strcmp(name, "fscreate")) { 516912b29f34SStephen Smalley if (!capable(CAP_MAC_ADMIN)) 517012b29f34SStephen Smalley return error; 517112b29f34SStephen Smalley error = security_context_to_sid_force(value, size, 517212b29f34SStephen Smalley &sid); 517312b29f34SStephen Smalley } 51741da177e4SLinus Torvalds if (error) 51751da177e4SLinus Torvalds return error; 51761da177e4SLinus Torvalds } 51771da177e4SLinus Torvalds 5178d84f4f99SDavid Howells new = prepare_creds(); 5179d84f4f99SDavid Howells if (!new) 5180d84f4f99SDavid Howells return -ENOMEM; 5181d84f4f99SDavid Howells 51821da177e4SLinus Torvalds /* Permission checking based on the specified context is 51831da177e4SLinus Torvalds performed during the actual operation (execve, 51841da177e4SLinus Torvalds open/mkdir/...), when we know the full context of the 5185d84f4f99SDavid Howells operation. See selinux_bprm_set_creds for the execve 51861da177e4SLinus Torvalds checks and may_create for the file creation checks. The 51871da177e4SLinus Torvalds operation will then fail if the context is not permitted. */ 5188d84f4f99SDavid Howells tsec = new->security; 5189d84f4f99SDavid Howells if (!strcmp(name, "exec")) { 51901da177e4SLinus Torvalds tsec->exec_sid = sid; 5191d84f4f99SDavid Howells } else if (!strcmp(name, "fscreate")) { 51921da177e4SLinus Torvalds tsec->create_sid = sid; 5193d84f4f99SDavid Howells } else if (!strcmp(name, "keycreate")) { 51944eb582cfSMichael LeMay error = may_create_key(sid, p); 51954eb582cfSMichael LeMay if (error) 5196d84f4f99SDavid Howells goto abort_change; 51974eb582cfSMichael LeMay tsec->keycreate_sid = sid; 5198d84f4f99SDavid Howells } else if (!strcmp(name, "sockcreate")) { 519942c3e03eSEric Paris tsec->sockcreate_sid = sid; 5200d84f4f99SDavid Howells } else if (!strcmp(name, "current")) { 5201d84f4f99SDavid Howells error = -EINVAL; 52021da177e4SLinus Torvalds if (sid == 0) 5203d84f4f99SDavid Howells goto abort_change; 5204d9250deaSKaiGai Kohei 5205d84f4f99SDavid Howells /* Only allow single threaded processes to change context */ 5206d84f4f99SDavid Howells error = -EPERM; 52075bb459bbSOleg Nesterov if (!current_is_single_threaded()) { 5208d84f4f99SDavid Howells error = security_bounded_transition(tsec->sid, sid); 5209d84f4f99SDavid Howells if (error) 5210d84f4f99SDavid Howells goto abort_change; 52111da177e4SLinus Torvalds } 52121da177e4SLinus Torvalds 52131da177e4SLinus Torvalds /* Check permissions for the transition. */ 52141da177e4SLinus Torvalds error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS, 52151da177e4SLinus Torvalds PROCESS__DYNTRANSITION, NULL); 52161da177e4SLinus Torvalds if (error) 5217d84f4f99SDavid Howells goto abort_change; 52181da177e4SLinus Torvalds 52191da177e4SLinus Torvalds /* Check for ptracing, and update the task SID if ok. 52201da177e4SLinus Torvalds Otherwise, leave SID unchanged and fail. */ 5221d84f4f99SDavid Howells ptsid = 0; 52221da177e4SLinus Torvalds task_lock(p); 52230d094efeSRoland McGrath tracer = tracehook_tracer_task(p); 5224d84f4f99SDavid Howells if (tracer) 5225d84f4f99SDavid Howells ptsid = task_sid(tracer); 52261da177e4SLinus Torvalds task_unlock(p); 52271da177e4SLinus Torvalds 5228d84f4f99SDavid Howells if (tracer) { 5229d84f4f99SDavid Howells error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS, 5230d84f4f99SDavid Howells PROCESS__PTRACE, NULL); 5231d84f4f99SDavid Howells if (error) 5232d84f4f99SDavid Howells goto abort_change; 5233d84f4f99SDavid Howells } 5234d84f4f99SDavid Howells 5235d84f4f99SDavid Howells tsec->sid = sid; 5236d84f4f99SDavid Howells } else { 5237d84f4f99SDavid Howells error = -EINVAL; 5238d84f4f99SDavid Howells goto abort_change; 5239d84f4f99SDavid Howells } 5240d84f4f99SDavid Howells 5241d84f4f99SDavid Howells commit_creds(new); 52421da177e4SLinus Torvalds return size; 5243d84f4f99SDavid Howells 5244d84f4f99SDavid Howells abort_change: 5245d84f4f99SDavid Howells abort_creds(new); 5246d84f4f99SDavid Howells return error; 52471da177e4SLinus Torvalds } 52481da177e4SLinus Torvalds 5249dc49c1f9SCatherine Zhang static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen) 5250dc49c1f9SCatherine Zhang { 5251dc49c1f9SCatherine Zhang return security_sid_to_context(secid, secdata, seclen); 5252dc49c1f9SCatherine Zhang } 5253dc49c1f9SCatherine Zhang 52547bf570dcSDavid Howells static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid) 525563cb3449SDavid Howells { 525663cb3449SDavid Howells return security_context_to_sid(secdata, seclen, secid); 525763cb3449SDavid Howells } 525863cb3449SDavid Howells 5259dc49c1f9SCatherine Zhang static void selinux_release_secctx(char *secdata, u32 seclen) 5260dc49c1f9SCatherine Zhang { 5261dc49c1f9SCatherine Zhang kfree(secdata); 5262dc49c1f9SCatherine Zhang } 5263dc49c1f9SCatherine Zhang 5264d720024eSMichael LeMay #ifdef CONFIG_KEYS 5265d720024eSMichael LeMay 5266d84f4f99SDavid Howells static int selinux_key_alloc(struct key *k, const struct cred *cred, 52677e047ef5SDavid Howells unsigned long flags) 5268d720024eSMichael LeMay { 5269d84f4f99SDavid Howells const struct task_security_struct *tsec; 5270d720024eSMichael LeMay struct key_security_struct *ksec; 5271d720024eSMichael LeMay 5272d720024eSMichael LeMay ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL); 5273d720024eSMichael LeMay if (!ksec) 5274d720024eSMichael LeMay return -ENOMEM; 5275d720024eSMichael LeMay 5276d84f4f99SDavid Howells tsec = cred->security; 5277d84f4f99SDavid Howells if (tsec->keycreate_sid) 5278d84f4f99SDavid Howells ksec->sid = tsec->keycreate_sid; 52794eb582cfSMichael LeMay else 5280d84f4f99SDavid Howells ksec->sid = tsec->sid; 5281d720024eSMichael LeMay 5282275bb41eSDavid Howells k->security = ksec; 5283d720024eSMichael LeMay return 0; 5284d720024eSMichael LeMay } 5285d720024eSMichael LeMay 5286d720024eSMichael LeMay static void selinux_key_free(struct key *k) 5287d720024eSMichael LeMay { 5288d720024eSMichael LeMay struct key_security_struct *ksec = k->security; 5289d720024eSMichael LeMay 5290d720024eSMichael LeMay k->security = NULL; 5291d720024eSMichael LeMay kfree(ksec); 5292d720024eSMichael LeMay } 5293d720024eSMichael LeMay 5294d720024eSMichael LeMay static int selinux_key_permission(key_ref_t key_ref, 5295d84f4f99SDavid Howells const struct cred *cred, 5296d720024eSMichael LeMay key_perm_t perm) 5297d720024eSMichael LeMay { 5298d720024eSMichael LeMay struct key *key; 5299d720024eSMichael LeMay struct key_security_struct *ksec; 5300275bb41eSDavid Howells u32 sid; 5301d720024eSMichael LeMay 5302d720024eSMichael LeMay /* if no specific permissions are requested, we skip the 5303d720024eSMichael LeMay permission check. No serious, additional covert channels 5304d720024eSMichael LeMay appear to be created. */ 5305d720024eSMichael LeMay if (perm == 0) 5306d720024eSMichael LeMay return 0; 5307d720024eSMichael LeMay 5308d84f4f99SDavid Howells sid = cred_sid(cred); 5309275bb41eSDavid Howells 5310275bb41eSDavid Howells key = key_ref_to_ptr(key_ref); 5311275bb41eSDavid Howells ksec = key->security; 5312275bb41eSDavid Howells 5313275bb41eSDavid Howells return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL); 5314d720024eSMichael LeMay } 5315d720024eSMichael LeMay 531670a5bb72SDavid Howells static int selinux_key_getsecurity(struct key *key, char **_buffer) 531770a5bb72SDavid Howells { 531870a5bb72SDavid Howells struct key_security_struct *ksec = key->security; 531970a5bb72SDavid Howells char *context = NULL; 532070a5bb72SDavid Howells unsigned len; 532170a5bb72SDavid Howells int rc; 532270a5bb72SDavid Howells 532370a5bb72SDavid Howells rc = security_sid_to_context(ksec->sid, &context, &len); 532470a5bb72SDavid Howells if (!rc) 532570a5bb72SDavid Howells rc = len; 532670a5bb72SDavid Howells *_buffer = context; 532770a5bb72SDavid Howells return rc; 532870a5bb72SDavid Howells } 532970a5bb72SDavid Howells 5330d720024eSMichael LeMay #endif 5331d720024eSMichael LeMay 53321da177e4SLinus Torvalds static struct security_operations selinux_ops = { 5333076c54c5SAhmed S. Darwish .name = "selinux", 5334076c54c5SAhmed S. Darwish 53359e48858fSIngo Molnar .ptrace_access_check = selinux_ptrace_access_check, 53365cd9c58fSDavid Howells .ptrace_traceme = selinux_ptrace_traceme, 53371da177e4SLinus Torvalds .capget = selinux_capget, 5338d84f4f99SDavid Howells .capset = selinux_capset, 53391da177e4SLinus Torvalds .sysctl = selinux_sysctl, 53401da177e4SLinus Torvalds .capable = selinux_capable, 53411da177e4SLinus Torvalds .quotactl = selinux_quotactl, 53421da177e4SLinus Torvalds .quota_on = selinux_quota_on, 53431da177e4SLinus Torvalds .syslog = selinux_syslog, 53441da177e4SLinus Torvalds .vm_enough_memory = selinux_vm_enough_memory, 53451da177e4SLinus Torvalds 53461da177e4SLinus Torvalds .netlink_send = selinux_netlink_send, 53471da177e4SLinus Torvalds .netlink_recv = selinux_netlink_recv, 53481da177e4SLinus Torvalds 5349a6f76f23SDavid Howells .bprm_set_creds = selinux_bprm_set_creds, 5350a6f76f23SDavid Howells .bprm_committing_creds = selinux_bprm_committing_creds, 5351a6f76f23SDavid Howells .bprm_committed_creds = selinux_bprm_committed_creds, 53521da177e4SLinus Torvalds .bprm_secureexec = selinux_bprm_secureexec, 53531da177e4SLinus Torvalds 53541da177e4SLinus Torvalds .sb_alloc_security = selinux_sb_alloc_security, 53551da177e4SLinus Torvalds .sb_free_security = selinux_sb_free_security, 53561da177e4SLinus Torvalds .sb_copy_data = selinux_sb_copy_data, 53571da177e4SLinus Torvalds .sb_kern_mount = selinux_sb_kern_mount, 53582069f457SEric Paris .sb_show_options = selinux_sb_show_options, 53591da177e4SLinus Torvalds .sb_statfs = selinux_sb_statfs, 53601da177e4SLinus Torvalds .sb_mount = selinux_mount, 53611da177e4SLinus Torvalds .sb_umount = selinux_umount, 5362c9180a57SEric Paris .sb_set_mnt_opts = selinux_set_mnt_opts, 5363c9180a57SEric Paris .sb_clone_mnt_opts = selinux_sb_clone_mnt_opts, 5364e0007529SEric Paris .sb_parse_opts_str = selinux_parse_opts_str, 5365e0007529SEric Paris 53661da177e4SLinus Torvalds 53671da177e4SLinus Torvalds .inode_alloc_security = selinux_inode_alloc_security, 53681da177e4SLinus Torvalds .inode_free_security = selinux_inode_free_security, 53695e41ff9eSStephen Smalley .inode_init_security = selinux_inode_init_security, 53701da177e4SLinus Torvalds .inode_create = selinux_inode_create, 53711da177e4SLinus Torvalds .inode_link = selinux_inode_link, 53721da177e4SLinus Torvalds .inode_unlink = selinux_inode_unlink, 53731da177e4SLinus Torvalds .inode_symlink = selinux_inode_symlink, 53741da177e4SLinus Torvalds .inode_mkdir = selinux_inode_mkdir, 53751da177e4SLinus Torvalds .inode_rmdir = selinux_inode_rmdir, 53761da177e4SLinus Torvalds .inode_mknod = selinux_inode_mknod, 53771da177e4SLinus Torvalds .inode_rename = selinux_inode_rename, 53781da177e4SLinus Torvalds .inode_readlink = selinux_inode_readlink, 53791da177e4SLinus Torvalds .inode_follow_link = selinux_inode_follow_link, 53801da177e4SLinus Torvalds .inode_permission = selinux_inode_permission, 53811da177e4SLinus Torvalds .inode_setattr = selinux_inode_setattr, 53821da177e4SLinus Torvalds .inode_getattr = selinux_inode_getattr, 53831da177e4SLinus Torvalds .inode_setxattr = selinux_inode_setxattr, 53841da177e4SLinus Torvalds .inode_post_setxattr = selinux_inode_post_setxattr, 53851da177e4SLinus Torvalds .inode_getxattr = selinux_inode_getxattr, 53861da177e4SLinus Torvalds .inode_listxattr = selinux_inode_listxattr, 53871da177e4SLinus Torvalds .inode_removexattr = selinux_inode_removexattr, 53881da177e4SLinus Torvalds .inode_getsecurity = selinux_inode_getsecurity, 53891da177e4SLinus Torvalds .inode_setsecurity = selinux_inode_setsecurity, 53901da177e4SLinus Torvalds .inode_listsecurity = selinux_inode_listsecurity, 5391713a04aeSAhmed S. Darwish .inode_getsecid = selinux_inode_getsecid, 53921da177e4SLinus Torvalds 53931da177e4SLinus Torvalds .file_permission = selinux_file_permission, 53941da177e4SLinus Torvalds .file_alloc_security = selinux_file_alloc_security, 53951da177e4SLinus Torvalds .file_free_security = selinux_file_free_security, 53961da177e4SLinus Torvalds .file_ioctl = selinux_file_ioctl, 53971da177e4SLinus Torvalds .file_mmap = selinux_file_mmap, 53981da177e4SLinus Torvalds .file_mprotect = selinux_file_mprotect, 53991da177e4SLinus Torvalds .file_lock = selinux_file_lock, 54001da177e4SLinus Torvalds .file_fcntl = selinux_file_fcntl, 54011da177e4SLinus Torvalds .file_set_fowner = selinux_file_set_fowner, 54021da177e4SLinus Torvalds .file_send_sigiotask = selinux_file_send_sigiotask, 54031da177e4SLinus Torvalds .file_receive = selinux_file_receive, 54041da177e4SLinus Torvalds 5405788e7dd4SYuichi Nakamura .dentry_open = selinux_dentry_open, 5406788e7dd4SYuichi Nakamura 54071da177e4SLinus Torvalds .task_create = selinux_task_create, 5408f1752eecSDavid Howells .cred_free = selinux_cred_free, 5409d84f4f99SDavid Howells .cred_prepare = selinux_cred_prepare, 54103a3b7ce9SDavid Howells .kernel_act_as = selinux_kernel_act_as, 54113a3b7ce9SDavid Howells .kernel_create_files_as = selinux_kernel_create_files_as, 541225354c4fSEric Paris .kernel_module_request = selinux_kernel_module_request, 54131da177e4SLinus Torvalds .task_setpgid = selinux_task_setpgid, 54141da177e4SLinus Torvalds .task_getpgid = selinux_task_getpgid, 54151da177e4SLinus Torvalds .task_getsid = selinux_task_getsid, 5416f9008e4cSDavid Quigley .task_getsecid = selinux_task_getsecid, 54171da177e4SLinus Torvalds .task_setnice = selinux_task_setnice, 541803e68060SJames Morris .task_setioprio = selinux_task_setioprio, 5419a1836a42SDavid Quigley .task_getioprio = selinux_task_getioprio, 54201da177e4SLinus Torvalds .task_setrlimit = selinux_task_setrlimit, 54211da177e4SLinus Torvalds .task_setscheduler = selinux_task_setscheduler, 54221da177e4SLinus Torvalds .task_getscheduler = selinux_task_getscheduler, 542335601547SDavid Quigley .task_movememory = selinux_task_movememory, 54241da177e4SLinus Torvalds .task_kill = selinux_task_kill, 54251da177e4SLinus Torvalds .task_wait = selinux_task_wait, 54261da177e4SLinus Torvalds .task_to_inode = selinux_task_to_inode, 54271da177e4SLinus Torvalds 54281da177e4SLinus Torvalds .ipc_permission = selinux_ipc_permission, 5429713a04aeSAhmed S. Darwish .ipc_getsecid = selinux_ipc_getsecid, 54301da177e4SLinus Torvalds 54311da177e4SLinus Torvalds .msg_msg_alloc_security = selinux_msg_msg_alloc_security, 54321da177e4SLinus Torvalds .msg_msg_free_security = selinux_msg_msg_free_security, 54331da177e4SLinus Torvalds 54341da177e4SLinus Torvalds .msg_queue_alloc_security = selinux_msg_queue_alloc_security, 54351da177e4SLinus Torvalds .msg_queue_free_security = selinux_msg_queue_free_security, 54361da177e4SLinus Torvalds .msg_queue_associate = selinux_msg_queue_associate, 54371da177e4SLinus Torvalds .msg_queue_msgctl = selinux_msg_queue_msgctl, 54381da177e4SLinus Torvalds .msg_queue_msgsnd = selinux_msg_queue_msgsnd, 54391da177e4SLinus Torvalds .msg_queue_msgrcv = selinux_msg_queue_msgrcv, 54401da177e4SLinus Torvalds 54411da177e4SLinus Torvalds .shm_alloc_security = selinux_shm_alloc_security, 54421da177e4SLinus Torvalds .shm_free_security = selinux_shm_free_security, 54431da177e4SLinus Torvalds .shm_associate = selinux_shm_associate, 54441da177e4SLinus Torvalds .shm_shmctl = selinux_shm_shmctl, 54451da177e4SLinus Torvalds .shm_shmat = selinux_shm_shmat, 54461da177e4SLinus Torvalds 54471da177e4SLinus Torvalds .sem_alloc_security = selinux_sem_alloc_security, 54481da177e4SLinus Torvalds .sem_free_security = selinux_sem_free_security, 54491da177e4SLinus Torvalds .sem_associate = selinux_sem_associate, 54501da177e4SLinus Torvalds .sem_semctl = selinux_sem_semctl, 54511da177e4SLinus Torvalds .sem_semop = selinux_sem_semop, 54521da177e4SLinus Torvalds 54531da177e4SLinus Torvalds .d_instantiate = selinux_d_instantiate, 54541da177e4SLinus Torvalds 54551da177e4SLinus Torvalds .getprocattr = selinux_getprocattr, 54561da177e4SLinus Torvalds .setprocattr = selinux_setprocattr, 54571da177e4SLinus Torvalds 5458dc49c1f9SCatherine Zhang .secid_to_secctx = selinux_secid_to_secctx, 545963cb3449SDavid Howells .secctx_to_secid = selinux_secctx_to_secid, 5460dc49c1f9SCatherine Zhang .release_secctx = selinux_release_secctx, 5461dc49c1f9SCatherine Zhang 54621da177e4SLinus Torvalds .unix_stream_connect = selinux_socket_unix_stream_connect, 54631da177e4SLinus Torvalds .unix_may_send = selinux_socket_unix_may_send, 54641da177e4SLinus Torvalds 54651da177e4SLinus Torvalds .socket_create = selinux_socket_create, 54661da177e4SLinus Torvalds .socket_post_create = selinux_socket_post_create, 54671da177e4SLinus Torvalds .socket_bind = selinux_socket_bind, 54681da177e4SLinus Torvalds .socket_connect = selinux_socket_connect, 54691da177e4SLinus Torvalds .socket_listen = selinux_socket_listen, 54701da177e4SLinus Torvalds .socket_accept = selinux_socket_accept, 54711da177e4SLinus Torvalds .socket_sendmsg = selinux_socket_sendmsg, 54721da177e4SLinus Torvalds .socket_recvmsg = selinux_socket_recvmsg, 54731da177e4SLinus Torvalds .socket_getsockname = selinux_socket_getsockname, 54741da177e4SLinus Torvalds .socket_getpeername = selinux_socket_getpeername, 54751da177e4SLinus Torvalds .socket_getsockopt = selinux_socket_getsockopt, 54761da177e4SLinus Torvalds .socket_setsockopt = selinux_socket_setsockopt, 54771da177e4SLinus Torvalds .socket_shutdown = selinux_socket_shutdown, 54781da177e4SLinus Torvalds .socket_sock_rcv_skb = selinux_socket_sock_rcv_skb, 54792c7946a7SCatherine Zhang .socket_getpeersec_stream = selinux_socket_getpeersec_stream, 54802c7946a7SCatherine Zhang .socket_getpeersec_dgram = selinux_socket_getpeersec_dgram, 54811da177e4SLinus Torvalds .sk_alloc_security = selinux_sk_alloc_security, 54821da177e4SLinus Torvalds .sk_free_security = selinux_sk_free_security, 5483892c141eSVenkat Yekkirala .sk_clone_security = selinux_sk_clone_security, 5484beb8d13bSVenkat Yekkirala .sk_getsecid = selinux_sk_getsecid, 54854237c75cSVenkat Yekkirala .sock_graft = selinux_sock_graft, 54864237c75cSVenkat Yekkirala .inet_conn_request = selinux_inet_conn_request, 54874237c75cSVenkat Yekkirala .inet_csk_clone = selinux_inet_csk_clone, 54886b877699SVenkat Yekkirala .inet_conn_established = selinux_inet_conn_established, 54894237c75cSVenkat Yekkirala .req_classify_flow = selinux_req_classify_flow, 5490d28d1e08STrent Jaeger 5491d28d1e08STrent Jaeger #ifdef CONFIG_SECURITY_NETWORK_XFRM 5492d28d1e08STrent Jaeger .xfrm_policy_alloc_security = selinux_xfrm_policy_alloc, 5493d28d1e08STrent Jaeger .xfrm_policy_clone_security = selinux_xfrm_policy_clone, 5494d28d1e08STrent Jaeger .xfrm_policy_free_security = selinux_xfrm_policy_free, 5495c8c05a8eSCatherine Zhang .xfrm_policy_delete_security = selinux_xfrm_policy_delete, 5496d28d1e08STrent Jaeger .xfrm_state_alloc_security = selinux_xfrm_state_alloc, 5497d28d1e08STrent Jaeger .xfrm_state_free_security = selinux_xfrm_state_free, 5498c8c05a8eSCatherine Zhang .xfrm_state_delete_security = selinux_xfrm_state_delete, 5499d28d1e08STrent Jaeger .xfrm_policy_lookup = selinux_xfrm_policy_lookup, 5500e0d1caa7SVenkat Yekkirala .xfrm_state_pol_flow_match = selinux_xfrm_state_pol_flow_match, 5501e0d1caa7SVenkat Yekkirala .xfrm_decode_session = selinux_xfrm_decode_session, 55021da177e4SLinus Torvalds #endif 5503d720024eSMichael LeMay 5504d720024eSMichael LeMay #ifdef CONFIG_KEYS 5505d720024eSMichael LeMay .key_alloc = selinux_key_alloc, 5506d720024eSMichael LeMay .key_free = selinux_key_free, 5507d720024eSMichael LeMay .key_permission = selinux_key_permission, 550870a5bb72SDavid Howells .key_getsecurity = selinux_key_getsecurity, 5509d720024eSMichael LeMay #endif 55109d57a7f9SAhmed S. Darwish 55119d57a7f9SAhmed S. Darwish #ifdef CONFIG_AUDIT 55129d57a7f9SAhmed S. Darwish .audit_rule_init = selinux_audit_rule_init, 55139d57a7f9SAhmed S. Darwish .audit_rule_known = selinux_audit_rule_known, 55149d57a7f9SAhmed S. Darwish .audit_rule_match = selinux_audit_rule_match, 55159d57a7f9SAhmed S. Darwish .audit_rule_free = selinux_audit_rule_free, 55169d57a7f9SAhmed S. Darwish #endif 55171da177e4SLinus Torvalds }; 55181da177e4SLinus Torvalds 55191da177e4SLinus Torvalds static __init int selinux_init(void) 55201da177e4SLinus Torvalds { 5521076c54c5SAhmed S. Darwish if (!security_module_enable(&selinux_ops)) { 5522076c54c5SAhmed S. Darwish selinux_enabled = 0; 5523076c54c5SAhmed S. Darwish return 0; 5524076c54c5SAhmed S. Darwish } 5525076c54c5SAhmed S. Darwish 55261da177e4SLinus Torvalds if (!selinux_enabled) { 55271da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Disabled at boot.\n"); 55281da177e4SLinus Torvalds return 0; 55291da177e4SLinus Torvalds } 55301da177e4SLinus Torvalds 55311da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Initializing.\n"); 55321da177e4SLinus Torvalds 55331da177e4SLinus Torvalds /* Set the security state for the initial task. */ 5534d84f4f99SDavid Howells cred_init_security(); 55351da177e4SLinus Torvalds 55367cae7e26SJames Morris sel_inode_cache = kmem_cache_create("selinux_inode_security", 55377cae7e26SJames Morris sizeof(struct inode_security_struct), 553820c2df83SPaul Mundt 0, SLAB_PANIC, NULL); 55391da177e4SLinus Torvalds avc_init(); 55401da177e4SLinus Torvalds 55416f0f0fd4SJames Morris secondary_ops = security_ops; 55421da177e4SLinus Torvalds if (!secondary_ops) 55431da177e4SLinus Torvalds panic("SELinux: No initial security operations\n"); 55441da177e4SLinus Torvalds if (register_security(&selinux_ops)) 55451da177e4SLinus Torvalds panic("SELinux: Unable to register with kernel.\n"); 55461da177e4SLinus Torvalds 5547828dfe1dSEric Paris if (selinux_enforcing) 5548fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Starting in enforcing mode\n"); 5549828dfe1dSEric Paris else 5550fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Starting in permissive mode\n"); 5551d720024eSMichael LeMay 55521da177e4SLinus Torvalds return 0; 55531da177e4SLinus Torvalds } 55541da177e4SLinus Torvalds 55551da177e4SLinus Torvalds void selinux_complete_init(void) 55561da177e4SLinus Torvalds { 5557fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Completing initialization.\n"); 55581da177e4SLinus Torvalds 55591da177e4SLinus Torvalds /* Set up any superblocks initialized prior to the policy load. */ 5560fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Setting up existing superblocks.\n"); 5561ba0c19edSStephen Smalley spin_lock(&sb_lock); 55621da177e4SLinus Torvalds spin_lock(&sb_security_lock); 55631da177e4SLinus Torvalds next_sb: 55641da177e4SLinus Torvalds if (!list_empty(&superblock_security_head)) { 55651da177e4SLinus Torvalds struct superblock_security_struct *sbsec = 55661da177e4SLinus Torvalds list_entry(superblock_security_head.next, 55671da177e4SLinus Torvalds struct superblock_security_struct, 55681da177e4SLinus Torvalds list); 55691da177e4SLinus Torvalds struct super_block *sb = sbsec->sb; 55701da177e4SLinus Torvalds sb->s_count++; 55711da177e4SLinus Torvalds spin_unlock(&sb_security_lock); 5572ba0c19edSStephen Smalley spin_unlock(&sb_lock); 55731da177e4SLinus Torvalds down_read(&sb->s_umount); 55741da177e4SLinus Torvalds if (sb->s_root) 55751da177e4SLinus Torvalds superblock_doinit(sb, NULL); 55761da177e4SLinus Torvalds drop_super(sb); 5577ba0c19edSStephen Smalley spin_lock(&sb_lock); 55781da177e4SLinus Torvalds spin_lock(&sb_security_lock); 55791da177e4SLinus Torvalds list_del_init(&sbsec->list); 55801da177e4SLinus Torvalds goto next_sb; 55811da177e4SLinus Torvalds } 55821da177e4SLinus Torvalds spin_unlock(&sb_security_lock); 5583ba0c19edSStephen Smalley spin_unlock(&sb_lock); 55841da177e4SLinus Torvalds } 55851da177e4SLinus Torvalds 55861da177e4SLinus Torvalds /* SELinux requires early initialization in order to label 55871da177e4SLinus Torvalds all processes and objects when they are created. */ 55881da177e4SLinus Torvalds security_initcall(selinux_init); 55891da177e4SLinus Torvalds 5590c2b507fdSStephen Smalley #if defined(CONFIG_NETFILTER) 55911da177e4SLinus Torvalds 5592effad8dfSPaul Moore static struct nf_hook_ops selinux_ipv4_ops[] = { 5593effad8dfSPaul Moore { 5594effad8dfSPaul Moore .hook = selinux_ipv4_postroute, 55951da177e4SLinus Torvalds .owner = THIS_MODULE, 55961da177e4SLinus Torvalds .pf = PF_INET, 55976e23ae2aSPatrick McHardy .hooknum = NF_INET_POST_ROUTING, 55981da177e4SLinus Torvalds .priority = NF_IP_PRI_SELINUX_LAST, 5599effad8dfSPaul Moore }, 5600effad8dfSPaul Moore { 5601effad8dfSPaul Moore .hook = selinux_ipv4_forward, 5602effad8dfSPaul Moore .owner = THIS_MODULE, 5603effad8dfSPaul Moore .pf = PF_INET, 5604effad8dfSPaul Moore .hooknum = NF_INET_FORWARD, 5605effad8dfSPaul Moore .priority = NF_IP_PRI_SELINUX_FIRST, 5606948bf85cSPaul Moore }, 5607948bf85cSPaul Moore { 5608948bf85cSPaul Moore .hook = selinux_ipv4_output, 5609948bf85cSPaul Moore .owner = THIS_MODULE, 5610948bf85cSPaul Moore .pf = PF_INET, 5611948bf85cSPaul Moore .hooknum = NF_INET_LOCAL_OUT, 5612948bf85cSPaul Moore .priority = NF_IP_PRI_SELINUX_FIRST, 5613effad8dfSPaul Moore } 56141da177e4SLinus Torvalds }; 56151da177e4SLinus Torvalds 56161da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 56171da177e4SLinus Torvalds 5618effad8dfSPaul Moore static struct nf_hook_ops selinux_ipv6_ops[] = { 5619effad8dfSPaul Moore { 5620effad8dfSPaul Moore .hook = selinux_ipv6_postroute, 56211da177e4SLinus Torvalds .owner = THIS_MODULE, 56221da177e4SLinus Torvalds .pf = PF_INET6, 56236e23ae2aSPatrick McHardy .hooknum = NF_INET_POST_ROUTING, 56241da177e4SLinus Torvalds .priority = NF_IP6_PRI_SELINUX_LAST, 5625effad8dfSPaul Moore }, 5626effad8dfSPaul Moore { 5627effad8dfSPaul Moore .hook = selinux_ipv6_forward, 5628effad8dfSPaul Moore .owner = THIS_MODULE, 5629effad8dfSPaul Moore .pf = PF_INET6, 5630effad8dfSPaul Moore .hooknum = NF_INET_FORWARD, 5631effad8dfSPaul Moore .priority = NF_IP6_PRI_SELINUX_FIRST, 5632effad8dfSPaul Moore } 56331da177e4SLinus Torvalds }; 56341da177e4SLinus Torvalds 56351da177e4SLinus Torvalds #endif /* IPV6 */ 56361da177e4SLinus Torvalds 56371da177e4SLinus Torvalds static int __init selinux_nf_ip_init(void) 56381da177e4SLinus Torvalds { 56391da177e4SLinus Torvalds int err = 0; 56401da177e4SLinus Torvalds 56411da177e4SLinus Torvalds if (!selinux_enabled) 56421da177e4SLinus Torvalds goto out; 56431da177e4SLinus Torvalds 5644fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Registering netfilter hooks\n"); 56451da177e4SLinus Torvalds 56466c5a9d2eSAlexey Dobriyan err = nf_register_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops)); 56471da177e4SLinus Torvalds if (err) 56486c5a9d2eSAlexey Dobriyan panic("SELinux: nf_register_hooks for IPv4: error %d\n", err); 56491da177e4SLinus Torvalds 56501da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 56516c5a9d2eSAlexey Dobriyan err = nf_register_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops)); 56521da177e4SLinus Torvalds if (err) 56536c5a9d2eSAlexey Dobriyan panic("SELinux: nf_register_hooks for IPv6: error %d\n", err); 56541da177e4SLinus Torvalds #endif /* IPV6 */ 5655d28d1e08STrent Jaeger 56561da177e4SLinus Torvalds out: 56571da177e4SLinus Torvalds return err; 56581da177e4SLinus Torvalds } 56591da177e4SLinus Torvalds 56601da177e4SLinus Torvalds __initcall(selinux_nf_ip_init); 56611da177e4SLinus Torvalds 56621da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 56631da177e4SLinus Torvalds static void selinux_nf_ip_exit(void) 56641da177e4SLinus Torvalds { 5665fadcdb45SEric Paris printk(KERN_DEBUG "SELinux: Unregistering netfilter hooks\n"); 56661da177e4SLinus Torvalds 56676c5a9d2eSAlexey Dobriyan nf_unregister_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops)); 56681da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 56696c5a9d2eSAlexey Dobriyan nf_unregister_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops)); 56701da177e4SLinus Torvalds #endif /* IPV6 */ 56711da177e4SLinus Torvalds } 56721da177e4SLinus Torvalds #endif 56731da177e4SLinus Torvalds 5674c2b507fdSStephen Smalley #else /* CONFIG_NETFILTER */ 56751da177e4SLinus Torvalds 56761da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 56771da177e4SLinus Torvalds #define selinux_nf_ip_exit() 56781da177e4SLinus Torvalds #endif 56791da177e4SLinus Torvalds 5680c2b507fdSStephen Smalley #endif /* CONFIG_NETFILTER */ 56811da177e4SLinus Torvalds 56821da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 5683828dfe1dSEric Paris static int selinux_disabled; 5684828dfe1dSEric Paris 56851da177e4SLinus Torvalds int selinux_disable(void) 56861da177e4SLinus Torvalds { 56871da177e4SLinus Torvalds extern void exit_sel_fs(void); 56881da177e4SLinus Torvalds 56891da177e4SLinus Torvalds if (ss_initialized) { 56901da177e4SLinus Torvalds /* Not permitted after initial policy load. */ 56911da177e4SLinus Torvalds return -EINVAL; 56921da177e4SLinus Torvalds } 56931da177e4SLinus Torvalds 56941da177e4SLinus Torvalds if (selinux_disabled) { 56951da177e4SLinus Torvalds /* Only do this once. */ 56961da177e4SLinus Torvalds return -EINVAL; 56971da177e4SLinus Torvalds } 56981da177e4SLinus Torvalds 56991da177e4SLinus Torvalds printk(KERN_INFO "SELinux: Disabled at runtime.\n"); 57001da177e4SLinus Torvalds 57011da177e4SLinus Torvalds selinux_disabled = 1; 570230d55280SStephen Smalley selinux_enabled = 0; 57031da177e4SLinus Torvalds 570489c86576SThomas Liu /* Try to destroy the avc node cache */ 570589c86576SThomas Liu avc_disable(); 570689c86576SThomas Liu 57071da177e4SLinus Torvalds /* Reset security_ops to the secondary module, dummy or capability. */ 57081da177e4SLinus Torvalds security_ops = secondary_ops; 57091da177e4SLinus Torvalds 57101da177e4SLinus Torvalds /* Unregister netfilter hooks. */ 57111da177e4SLinus Torvalds selinux_nf_ip_exit(); 57121da177e4SLinus Torvalds 57131da177e4SLinus Torvalds /* Unregister selinuxfs. */ 57141da177e4SLinus Torvalds exit_sel_fs(); 57151da177e4SLinus Torvalds 57161da177e4SLinus Torvalds return 0; 57171da177e4SLinus Torvalds } 57181da177e4SLinus Torvalds #endif 5719