11da177e4SLinus Torvalds /* Updated: Karl MacMillan <kmacmillan@tresys.com> 21da177e4SLinus Torvalds * 31da177e4SLinus Torvalds * Added conditional policy language extensions 41da177e4SLinus Torvalds * 53bb56b25SPaul Moore * Updated: Hewlett-Packard <paul.moore@hp.com> 63bb56b25SPaul Moore * 73bb56b25SPaul Moore * Added support for the policy capability bitmap 83bb56b25SPaul Moore * 93bb56b25SPaul Moore * Copyright (C) 2007 Hewlett-Packard Development Company, L.P. 101da177e4SLinus Torvalds * Copyright (C) 2003 - 2004 Tresys Technology, LLC 111da177e4SLinus Torvalds * Copyright (C) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com> 121da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 131da177e4SLinus Torvalds * it under the terms of the GNU General Public License as published by 141da177e4SLinus Torvalds * the Free Software Foundation, version 2. 151da177e4SLinus Torvalds */ 161da177e4SLinus Torvalds 171da177e4SLinus Torvalds #include <linux/kernel.h> 181da177e4SLinus Torvalds #include <linux/pagemap.h> 191da177e4SLinus Torvalds #include <linux/slab.h> 201da177e4SLinus Torvalds #include <linux/vmalloc.h> 211da177e4SLinus Torvalds #include <linux/fs.h> 22bb003079SIngo Molnar #include <linux/mutex.h> 231da177e4SLinus Torvalds #include <linux/init.h> 241da177e4SLinus Torvalds #include <linux/string.h> 251da177e4SLinus Torvalds #include <linux/security.h> 261da177e4SLinus Torvalds #include <linux/major.h> 271da177e4SLinus Torvalds #include <linux/seq_file.h> 281da177e4SLinus Torvalds #include <linux/percpu.h> 29af601e46SSteve Grubb #include <linux/audit.h> 301da177e4SLinus Torvalds #include <asm/uaccess.h> 311da177e4SLinus Torvalds #include <asm/semaphore.h> 321da177e4SLinus Torvalds 331da177e4SLinus Torvalds /* selinuxfs pseudo filesystem for exporting the security policy API. 341da177e4SLinus Torvalds Based on the proc code and the fs/nfsd/nfsctl.c code. */ 351da177e4SLinus Torvalds 361da177e4SLinus Torvalds #include "flask.h" 371da177e4SLinus Torvalds #include "avc.h" 381da177e4SLinus Torvalds #include "avc_ss.h" 391da177e4SLinus Torvalds #include "security.h" 401da177e4SLinus Torvalds #include "objsec.h" 411da177e4SLinus Torvalds #include "conditional.h" 421da177e4SLinus Torvalds 433bb56b25SPaul Moore /* Policy capability filenames */ 443bb56b25SPaul Moore static char *policycap_names[] = { 453bb56b25SPaul Moore "network_peer_controls" 463bb56b25SPaul Moore }; 473bb56b25SPaul Moore 481da177e4SLinus Torvalds unsigned int selinux_checkreqprot = CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE; 491da177e4SLinus Torvalds 504e5ab4cbSJames Morris #ifdef CONFIG_SECURITY_SELINUX_ENABLE_SECMARK_DEFAULT 514e5ab4cbSJames Morris #define SELINUX_COMPAT_NET_VALUE 0 524e5ab4cbSJames Morris #else 534e5ab4cbSJames Morris #define SELINUX_COMPAT_NET_VALUE 1 544e5ab4cbSJames Morris #endif 554e5ab4cbSJames Morris 564e5ab4cbSJames Morris int selinux_compat_net = SELINUX_COMPAT_NET_VALUE; 574e5ab4cbSJames Morris 581da177e4SLinus Torvalds static int __init checkreqprot_setup(char *str) 591da177e4SLinus Torvalds { 601da177e4SLinus Torvalds selinux_checkreqprot = simple_strtoul(str,NULL,0) ? 1 : 0; 611da177e4SLinus Torvalds return 1; 621da177e4SLinus Torvalds } 631da177e4SLinus Torvalds __setup("checkreqprot=", checkreqprot_setup); 641da177e4SLinus Torvalds 654e5ab4cbSJames Morris static int __init selinux_compat_net_setup(char *str) 664e5ab4cbSJames Morris { 674e5ab4cbSJames Morris selinux_compat_net = simple_strtoul(str,NULL,0) ? 1 : 0; 684e5ab4cbSJames Morris return 1; 694e5ab4cbSJames Morris } 704e5ab4cbSJames Morris __setup("selinux_compat_net=", selinux_compat_net_setup); 714e5ab4cbSJames Morris 721da177e4SLinus Torvalds 73bb003079SIngo Molnar static DEFINE_MUTEX(sel_mutex); 741da177e4SLinus Torvalds 751da177e4SLinus Torvalds /* global data for booleans */ 761da177e4SLinus Torvalds static struct dentry *bool_dir = NULL; 771da177e4SLinus Torvalds static int bool_num = 0; 78d313f948SStephen Smalley static char **bool_pending_names; 791da177e4SLinus Torvalds static int *bool_pending_values = NULL; 801da177e4SLinus Torvalds 81e47c8fc5SChristopher J. PeBenito /* global data for classes */ 82e47c8fc5SChristopher J. PeBenito static struct dentry *class_dir = NULL; 83e47c8fc5SChristopher J. PeBenito static unsigned long last_class_ino; 84e47c8fc5SChristopher J. PeBenito 853bb56b25SPaul Moore /* global data for policy capabilities */ 863bb56b25SPaul Moore static struct dentry *policycap_dir = NULL; 873bb56b25SPaul Moore 881da177e4SLinus Torvalds extern void selnl_notify_setenforce(int val); 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds /* Check whether a task is allowed to use a security operation. */ 911da177e4SLinus Torvalds static int task_has_security(struct task_struct *tsk, 921da177e4SLinus Torvalds u32 perms) 931da177e4SLinus Torvalds { 941da177e4SLinus Torvalds struct task_security_struct *tsec; 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds tsec = tsk->security; 971da177e4SLinus Torvalds if (!tsec) 981da177e4SLinus Torvalds return -EACCES; 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds return avc_has_perm(tsec->sid, SECINITSID_SECURITY, 1011da177e4SLinus Torvalds SECCLASS_SECURITY, perms, NULL); 1021da177e4SLinus Torvalds } 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds enum sel_inos { 1051da177e4SLinus Torvalds SEL_ROOT_INO = 2, 1061da177e4SLinus Torvalds SEL_LOAD, /* load policy */ 1071da177e4SLinus Torvalds SEL_ENFORCE, /* get or set enforcing status */ 1081da177e4SLinus Torvalds SEL_CONTEXT, /* validate context */ 1091da177e4SLinus Torvalds SEL_ACCESS, /* compute access decision */ 1101da177e4SLinus Torvalds SEL_CREATE, /* compute create labeling decision */ 1111da177e4SLinus Torvalds SEL_RELABEL, /* compute relabeling decision */ 1121da177e4SLinus Torvalds SEL_USER, /* compute reachable user contexts */ 1131da177e4SLinus Torvalds SEL_POLICYVERS, /* return policy version for this kernel */ 1141da177e4SLinus Torvalds SEL_COMMIT_BOOLS, /* commit new boolean values */ 1151da177e4SLinus Torvalds SEL_MLS, /* return if MLS policy is enabled */ 1161da177e4SLinus Torvalds SEL_DISABLE, /* disable SELinux until next reboot */ 1171da177e4SLinus Torvalds SEL_MEMBER, /* compute polyinstantiation membership decision */ 1181da177e4SLinus Torvalds SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */ 1194e5ab4cbSJames Morris SEL_COMPAT_NET, /* whether to use old compat network packet controls */ 1203f12070eSEric Paris SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */ 1213f12070eSEric Paris SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */ 1226174eafcSJames Carter SEL_INO_NEXT, /* The next inode number to use */ 1231da177e4SLinus Torvalds }; 1241da177e4SLinus Torvalds 1256174eafcSJames Carter static unsigned long sel_last_ino = SEL_INO_NEXT - 1; 1266174eafcSJames Carter 127f0ee2e46SJames Carter #define SEL_INITCON_INO_OFFSET 0x01000000 128bce34bc0SJames Carter #define SEL_BOOL_INO_OFFSET 0x02000000 129e47c8fc5SChristopher J. PeBenito #define SEL_CLASS_INO_OFFSET 0x04000000 1303bb56b25SPaul Moore #define SEL_POLICYCAP_INO_OFFSET 0x08000000 131f0ee2e46SJames Carter #define SEL_INO_MASK 0x00ffffff 132f0ee2e46SJames Carter 1331da177e4SLinus Torvalds #define TMPBUFLEN 12 1341da177e4SLinus Torvalds static ssize_t sel_read_enforce(struct file *filp, char __user *buf, 1351da177e4SLinus Torvalds size_t count, loff_t *ppos) 1361da177e4SLinus Torvalds { 1371da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 1381da177e4SLinus Torvalds ssize_t length; 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing); 1411da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 1421da177e4SLinus Torvalds } 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP 1451da177e4SLinus Torvalds static ssize_t sel_write_enforce(struct file * file, const char __user * buf, 1461da177e4SLinus Torvalds size_t count, loff_t *ppos) 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds { 1491da177e4SLinus Torvalds char *page; 1501da177e4SLinus Torvalds ssize_t length; 1511da177e4SLinus Torvalds int new_value; 1521da177e4SLinus Torvalds 153bfd51626SDavi Arnaut if (count >= PAGE_SIZE) 1541da177e4SLinus Torvalds return -ENOMEM; 1551da177e4SLinus Torvalds if (*ppos != 0) { 1561da177e4SLinus Torvalds /* No partial writes. */ 1571da177e4SLinus Torvalds return -EINVAL; 1581da177e4SLinus Torvalds } 1591da177e4SLinus Torvalds page = (char*)get_zeroed_page(GFP_KERNEL); 1601da177e4SLinus Torvalds if (!page) 1611da177e4SLinus Torvalds return -ENOMEM; 1621da177e4SLinus Torvalds length = -EFAULT; 1631da177e4SLinus Torvalds if (copy_from_user(page, buf, count)) 1641da177e4SLinus Torvalds goto out; 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds length = -EINVAL; 1671da177e4SLinus Torvalds if (sscanf(page, "%d", &new_value) != 1) 1681da177e4SLinus Torvalds goto out; 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds if (new_value != selinux_enforcing) { 1711da177e4SLinus Torvalds length = task_has_security(current, SECURITY__SETENFORCE); 1721da177e4SLinus Torvalds if (length) 1731da177e4SLinus Torvalds goto out; 174af601e46SSteve Grubb audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS, 175af601e46SSteve Grubb "enforcing=%d old_enforcing=%d auid=%u", new_value, 176af601e46SSteve Grubb selinux_enforcing, 177af601e46SSteve Grubb audit_get_loginuid(current->audit_context)); 1781da177e4SLinus Torvalds selinux_enforcing = new_value; 1791da177e4SLinus Torvalds if (selinux_enforcing) 1801da177e4SLinus Torvalds avc_ss_reset(0); 1811da177e4SLinus Torvalds selnl_notify_setenforce(selinux_enforcing); 1821da177e4SLinus Torvalds } 1831da177e4SLinus Torvalds length = count; 1841da177e4SLinus Torvalds out: 1851da177e4SLinus Torvalds free_page((unsigned long) page); 1861da177e4SLinus Torvalds return length; 1871da177e4SLinus Torvalds } 1881da177e4SLinus Torvalds #else 1891da177e4SLinus Torvalds #define sel_write_enforce NULL 1901da177e4SLinus Torvalds #endif 1911da177e4SLinus Torvalds 1929c2e08c5SArjan van de Ven static const struct file_operations sel_enforce_ops = { 1931da177e4SLinus Torvalds .read = sel_read_enforce, 1941da177e4SLinus Torvalds .write = sel_write_enforce, 1951da177e4SLinus Torvalds }; 1961da177e4SLinus Torvalds 1973f12070eSEric Paris static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf, 1983f12070eSEric Paris size_t count, loff_t *ppos) 1993f12070eSEric Paris { 2003f12070eSEric Paris char tmpbuf[TMPBUFLEN]; 2013f12070eSEric Paris ssize_t length; 2023f12070eSEric Paris ino_t ino = filp->f_path.dentry->d_inode->i_ino; 2033f12070eSEric Paris int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ? 2043f12070eSEric Paris security_get_reject_unknown() : !security_get_allow_unknown(); 2053f12070eSEric Paris 2063f12070eSEric Paris length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown); 2073f12070eSEric Paris return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 2083f12070eSEric Paris } 2093f12070eSEric Paris 2103f12070eSEric Paris static const struct file_operations sel_handle_unknown_ops = { 2113f12070eSEric Paris .read = sel_read_handle_unknown, 2123f12070eSEric Paris }; 2133f12070eSEric Paris 2141da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 2151da177e4SLinus Torvalds static ssize_t sel_write_disable(struct file * file, const char __user * buf, 2161da177e4SLinus Torvalds size_t count, loff_t *ppos) 2171da177e4SLinus Torvalds 2181da177e4SLinus Torvalds { 2191da177e4SLinus Torvalds char *page; 2201da177e4SLinus Torvalds ssize_t length; 2211da177e4SLinus Torvalds int new_value; 2221da177e4SLinus Torvalds extern int selinux_disable(void); 2231da177e4SLinus Torvalds 224bfd51626SDavi Arnaut if (count >= PAGE_SIZE) 2251da177e4SLinus Torvalds return -ENOMEM; 2261da177e4SLinus Torvalds if (*ppos != 0) { 2271da177e4SLinus Torvalds /* No partial writes. */ 2281da177e4SLinus Torvalds return -EINVAL; 2291da177e4SLinus Torvalds } 2301da177e4SLinus Torvalds page = (char*)get_zeroed_page(GFP_KERNEL); 2311da177e4SLinus Torvalds if (!page) 2321da177e4SLinus Torvalds return -ENOMEM; 2331da177e4SLinus Torvalds length = -EFAULT; 2341da177e4SLinus Torvalds if (copy_from_user(page, buf, count)) 2351da177e4SLinus Torvalds goto out; 2361da177e4SLinus Torvalds 2371da177e4SLinus Torvalds length = -EINVAL; 2381da177e4SLinus Torvalds if (sscanf(page, "%d", &new_value) != 1) 2391da177e4SLinus Torvalds goto out; 2401da177e4SLinus Torvalds 2411da177e4SLinus Torvalds if (new_value) { 2421da177e4SLinus Torvalds length = selinux_disable(); 2431da177e4SLinus Torvalds if (length < 0) 2441da177e4SLinus Torvalds goto out; 245af601e46SSteve Grubb audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS, 246af601e46SSteve Grubb "selinux=0 auid=%u", 247af601e46SSteve Grubb audit_get_loginuid(current->audit_context)); 2481da177e4SLinus Torvalds } 2491da177e4SLinus Torvalds 2501da177e4SLinus Torvalds length = count; 2511da177e4SLinus Torvalds out: 2521da177e4SLinus Torvalds free_page((unsigned long) page); 2531da177e4SLinus Torvalds return length; 2541da177e4SLinus Torvalds } 2551da177e4SLinus Torvalds #else 2561da177e4SLinus Torvalds #define sel_write_disable NULL 2571da177e4SLinus Torvalds #endif 2581da177e4SLinus Torvalds 2599c2e08c5SArjan van de Ven static const struct file_operations sel_disable_ops = { 2601da177e4SLinus Torvalds .write = sel_write_disable, 2611da177e4SLinus Torvalds }; 2621da177e4SLinus Torvalds 2631da177e4SLinus Torvalds static ssize_t sel_read_policyvers(struct file *filp, char __user *buf, 2641da177e4SLinus Torvalds size_t count, loff_t *ppos) 2651da177e4SLinus Torvalds { 2661da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 2671da177e4SLinus Torvalds ssize_t length; 2681da177e4SLinus Torvalds 2691da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX); 2701da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 2711da177e4SLinus Torvalds } 2721da177e4SLinus Torvalds 2739c2e08c5SArjan van de Ven static const struct file_operations sel_policyvers_ops = { 2741da177e4SLinus Torvalds .read = sel_read_policyvers, 2751da177e4SLinus Torvalds }; 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds /* declaration for sel_write_load */ 2781da177e4SLinus Torvalds static int sel_make_bools(void); 279e47c8fc5SChristopher J. PeBenito static int sel_make_classes(void); 2803bb56b25SPaul Moore static int sel_make_policycap(void); 281e47c8fc5SChristopher J. PeBenito 282e47c8fc5SChristopher J. PeBenito /* declaration for sel_make_class_dirs */ 283e47c8fc5SChristopher J. PeBenito static int sel_make_dir(struct inode *dir, struct dentry *dentry, 284e47c8fc5SChristopher J. PeBenito unsigned long *ino); 2851da177e4SLinus Torvalds 2861da177e4SLinus Torvalds static ssize_t sel_read_mls(struct file *filp, char __user *buf, 2871da177e4SLinus Torvalds size_t count, loff_t *ppos) 2881da177e4SLinus Torvalds { 2891da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 2901da177e4SLinus Torvalds ssize_t length; 2911da177e4SLinus Torvalds 2921da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_mls_enabled); 2931da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 2941da177e4SLinus Torvalds } 2951da177e4SLinus Torvalds 2969c2e08c5SArjan van de Ven static const struct file_operations sel_mls_ops = { 2971da177e4SLinus Torvalds .read = sel_read_mls, 2981da177e4SLinus Torvalds }; 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds static ssize_t sel_write_load(struct file * file, const char __user * buf, 3011da177e4SLinus Torvalds size_t count, loff_t *ppos) 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds { 3041da177e4SLinus Torvalds int ret; 3051da177e4SLinus Torvalds ssize_t length; 3061da177e4SLinus Torvalds void *data = NULL; 3071da177e4SLinus Torvalds 308bb003079SIngo Molnar mutex_lock(&sel_mutex); 3091da177e4SLinus Torvalds 3101da177e4SLinus Torvalds length = task_has_security(current, SECURITY__LOAD_POLICY); 3111da177e4SLinus Torvalds if (length) 3121da177e4SLinus Torvalds goto out; 3131da177e4SLinus Torvalds 3141da177e4SLinus Torvalds if (*ppos != 0) { 3151da177e4SLinus Torvalds /* No partial writes. */ 3161da177e4SLinus Torvalds length = -EINVAL; 3171da177e4SLinus Torvalds goto out; 3181da177e4SLinus Torvalds } 3191da177e4SLinus Torvalds 320bfd51626SDavi Arnaut if ((count > 64 * 1024 * 1024) 3211da177e4SLinus Torvalds || (data = vmalloc(count)) == NULL) { 3221da177e4SLinus Torvalds length = -ENOMEM; 3231da177e4SLinus Torvalds goto out; 3241da177e4SLinus Torvalds } 3251da177e4SLinus Torvalds 3261da177e4SLinus Torvalds length = -EFAULT; 3271da177e4SLinus Torvalds if (copy_from_user(data, buf, count) != 0) 3281da177e4SLinus Torvalds goto out; 3291da177e4SLinus Torvalds 3301da177e4SLinus Torvalds length = security_load_policy(data, count); 3311da177e4SLinus Torvalds if (length) 3321da177e4SLinus Torvalds goto out; 3331da177e4SLinus Torvalds 3341da177e4SLinus Torvalds ret = sel_make_bools(); 335e47c8fc5SChristopher J. PeBenito if (ret) { 336e47c8fc5SChristopher J. PeBenito length = ret; 337e47c8fc5SChristopher J. PeBenito goto out1; 338e47c8fc5SChristopher J. PeBenito } 339e47c8fc5SChristopher J. PeBenito 340e47c8fc5SChristopher J. PeBenito ret = sel_make_classes(); 3413bb56b25SPaul Moore if (ret) { 3423bb56b25SPaul Moore length = ret; 3433bb56b25SPaul Moore goto out1; 3443bb56b25SPaul Moore } 3453bb56b25SPaul Moore 3463bb56b25SPaul Moore ret = sel_make_policycap(); 3471da177e4SLinus Torvalds if (ret) 3481da177e4SLinus Torvalds length = ret; 3491da177e4SLinus Torvalds else 3501da177e4SLinus Torvalds length = count; 351e47c8fc5SChristopher J. PeBenito 352e47c8fc5SChristopher J. PeBenito out1: 3533f12070eSEric Paris 3543f12070eSEric Paris printk(KERN_INFO "SELinux: policy loaded with handle_unknown=%s\n", 3553f12070eSEric Paris (security_get_reject_unknown() ? "reject" : 3563f12070eSEric Paris (security_get_allow_unknown() ? "allow" : "deny"))); 3573f12070eSEric Paris 358af601e46SSteve Grubb audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD, 359af601e46SSteve Grubb "policy loaded auid=%u", 360af601e46SSteve Grubb audit_get_loginuid(current->audit_context)); 3611da177e4SLinus Torvalds out: 362bb003079SIngo Molnar mutex_unlock(&sel_mutex); 3631da177e4SLinus Torvalds vfree(data); 3641da177e4SLinus Torvalds return length; 3651da177e4SLinus Torvalds } 3661da177e4SLinus Torvalds 3679c2e08c5SArjan van de Ven static const struct file_operations sel_load_ops = { 3681da177e4SLinus Torvalds .write = sel_write_load, 3691da177e4SLinus Torvalds }; 3701da177e4SLinus Torvalds 371ce9982d0SStephen Smalley static ssize_t sel_write_context(struct file * file, char *buf, size_t size) 3721da177e4SLinus Torvalds { 373ce9982d0SStephen Smalley char *canon; 374ce9982d0SStephen Smalley u32 sid, len; 3751da177e4SLinus Torvalds ssize_t length; 3761da177e4SLinus Torvalds 3771da177e4SLinus Torvalds length = task_has_security(current, SECURITY__CHECK_CONTEXT); 3781da177e4SLinus Torvalds if (length) 3791da177e4SLinus Torvalds return length; 3801da177e4SLinus Torvalds 381ce9982d0SStephen Smalley length = security_context_to_sid(buf, size, &sid); 3821da177e4SLinus Torvalds if (length < 0) 383ce9982d0SStephen Smalley return length; 3841da177e4SLinus Torvalds 385ce9982d0SStephen Smalley length = security_sid_to_context(sid, &canon, &len); 386ce9982d0SStephen Smalley if (length < 0) 387ce9982d0SStephen Smalley return length; 388ce9982d0SStephen Smalley 389ce9982d0SStephen Smalley if (len > SIMPLE_TRANSACTION_LIMIT) { 390ce9982d0SStephen Smalley printk(KERN_ERR "%s: context size (%u) exceeds payload " 391ce9982d0SStephen Smalley "max\n", __FUNCTION__, len); 392ce9982d0SStephen Smalley length = -ERANGE; 393ce9982d0SStephen Smalley goto out; 394ce9982d0SStephen Smalley } 395ce9982d0SStephen Smalley 396ce9982d0SStephen Smalley memcpy(buf, canon, len); 397ce9982d0SStephen Smalley length = len; 3981da177e4SLinus Torvalds out: 399ce9982d0SStephen Smalley kfree(canon); 4001da177e4SLinus Torvalds return length; 4011da177e4SLinus Torvalds } 4021da177e4SLinus Torvalds 4031da177e4SLinus Torvalds static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf, 4041da177e4SLinus Torvalds size_t count, loff_t *ppos) 4051da177e4SLinus Torvalds { 4061da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 4071da177e4SLinus Torvalds ssize_t length; 4081da177e4SLinus Torvalds 4091da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot); 4101da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 4111da177e4SLinus Torvalds } 4121da177e4SLinus Torvalds 4131da177e4SLinus Torvalds static ssize_t sel_write_checkreqprot(struct file * file, const char __user * buf, 4141da177e4SLinus Torvalds size_t count, loff_t *ppos) 4151da177e4SLinus Torvalds { 4161da177e4SLinus Torvalds char *page; 4171da177e4SLinus Torvalds ssize_t length; 4181da177e4SLinus Torvalds unsigned int new_value; 4191da177e4SLinus Torvalds 4201da177e4SLinus Torvalds length = task_has_security(current, SECURITY__SETCHECKREQPROT); 4211da177e4SLinus Torvalds if (length) 4221da177e4SLinus Torvalds return length; 4231da177e4SLinus Torvalds 424bfd51626SDavi Arnaut if (count >= PAGE_SIZE) 4251da177e4SLinus Torvalds return -ENOMEM; 4261da177e4SLinus Torvalds if (*ppos != 0) { 4271da177e4SLinus Torvalds /* No partial writes. */ 4281da177e4SLinus Torvalds return -EINVAL; 4291da177e4SLinus Torvalds } 4301da177e4SLinus Torvalds page = (char*)get_zeroed_page(GFP_KERNEL); 4311da177e4SLinus Torvalds if (!page) 4321da177e4SLinus Torvalds return -ENOMEM; 4331da177e4SLinus Torvalds length = -EFAULT; 4341da177e4SLinus Torvalds if (copy_from_user(page, buf, count)) 4351da177e4SLinus Torvalds goto out; 4361da177e4SLinus Torvalds 4371da177e4SLinus Torvalds length = -EINVAL; 4381da177e4SLinus Torvalds if (sscanf(page, "%u", &new_value) != 1) 4391da177e4SLinus Torvalds goto out; 4401da177e4SLinus Torvalds 4411da177e4SLinus Torvalds selinux_checkreqprot = new_value ? 1 : 0; 4421da177e4SLinus Torvalds length = count; 4431da177e4SLinus Torvalds out: 4441da177e4SLinus Torvalds free_page((unsigned long) page); 4451da177e4SLinus Torvalds return length; 4461da177e4SLinus Torvalds } 4479c2e08c5SArjan van de Ven static const struct file_operations sel_checkreqprot_ops = { 4481da177e4SLinus Torvalds .read = sel_read_checkreqprot, 4491da177e4SLinus Torvalds .write = sel_write_checkreqprot, 4501da177e4SLinus Torvalds }; 4511da177e4SLinus Torvalds 4524e5ab4cbSJames Morris static ssize_t sel_read_compat_net(struct file *filp, char __user *buf, 4534e5ab4cbSJames Morris size_t count, loff_t *ppos) 4544e5ab4cbSJames Morris { 4554e5ab4cbSJames Morris char tmpbuf[TMPBUFLEN]; 4564e5ab4cbSJames Morris ssize_t length; 4574e5ab4cbSJames Morris 4584e5ab4cbSJames Morris length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_compat_net); 4594e5ab4cbSJames Morris return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 4604e5ab4cbSJames Morris } 4614e5ab4cbSJames Morris 4624e5ab4cbSJames Morris static ssize_t sel_write_compat_net(struct file * file, const char __user * buf, 4634e5ab4cbSJames Morris size_t count, loff_t *ppos) 4644e5ab4cbSJames Morris { 4654e5ab4cbSJames Morris char *page; 4664e5ab4cbSJames Morris ssize_t length; 4674e5ab4cbSJames Morris int new_value; 4684e5ab4cbSJames Morris 4694e5ab4cbSJames Morris length = task_has_security(current, SECURITY__LOAD_POLICY); 4704e5ab4cbSJames Morris if (length) 4714e5ab4cbSJames Morris return length; 4724e5ab4cbSJames Morris 4734e5ab4cbSJames Morris if (count >= PAGE_SIZE) 4744e5ab4cbSJames Morris return -ENOMEM; 4754e5ab4cbSJames Morris if (*ppos != 0) { 4764e5ab4cbSJames Morris /* No partial writes. */ 4774e5ab4cbSJames Morris return -EINVAL; 4784e5ab4cbSJames Morris } 4794e5ab4cbSJames Morris page = (char*)get_zeroed_page(GFP_KERNEL); 4804e5ab4cbSJames Morris if (!page) 4814e5ab4cbSJames Morris return -ENOMEM; 4824e5ab4cbSJames Morris length = -EFAULT; 4834e5ab4cbSJames Morris if (copy_from_user(page, buf, count)) 4844e5ab4cbSJames Morris goto out; 4854e5ab4cbSJames Morris 4864e5ab4cbSJames Morris length = -EINVAL; 4874e5ab4cbSJames Morris if (sscanf(page, "%d", &new_value) != 1) 4884e5ab4cbSJames Morris goto out; 4894e5ab4cbSJames Morris 4904e5ab4cbSJames Morris selinux_compat_net = new_value ? 1 : 0; 4914e5ab4cbSJames Morris length = count; 4924e5ab4cbSJames Morris out: 4934e5ab4cbSJames Morris free_page((unsigned long) page); 4944e5ab4cbSJames Morris return length; 4954e5ab4cbSJames Morris } 4969c2e08c5SArjan van de Ven static const struct file_operations sel_compat_net_ops = { 4974e5ab4cbSJames Morris .read = sel_read_compat_net, 4984e5ab4cbSJames Morris .write = sel_write_compat_net, 4994e5ab4cbSJames Morris }; 5004e5ab4cbSJames Morris 5011da177e4SLinus Torvalds /* 5021da177e4SLinus Torvalds * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c 5031da177e4SLinus Torvalds */ 5041da177e4SLinus Torvalds static ssize_t sel_write_access(struct file * file, char *buf, size_t size); 5051da177e4SLinus Torvalds static ssize_t sel_write_create(struct file * file, char *buf, size_t size); 5061da177e4SLinus Torvalds static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size); 5071da177e4SLinus Torvalds static ssize_t sel_write_user(struct file * file, char *buf, size_t size); 5081da177e4SLinus Torvalds static ssize_t sel_write_member(struct file * file, char *buf, size_t size); 5091da177e4SLinus Torvalds 5101da177e4SLinus Torvalds static ssize_t (*write_op[])(struct file *, char *, size_t) = { 5111da177e4SLinus Torvalds [SEL_ACCESS] = sel_write_access, 5121da177e4SLinus Torvalds [SEL_CREATE] = sel_write_create, 5131da177e4SLinus Torvalds [SEL_RELABEL] = sel_write_relabel, 5141da177e4SLinus Torvalds [SEL_USER] = sel_write_user, 5151da177e4SLinus Torvalds [SEL_MEMBER] = sel_write_member, 516ce9982d0SStephen Smalley [SEL_CONTEXT] = sel_write_context, 5171da177e4SLinus Torvalds }; 5181da177e4SLinus Torvalds 5191da177e4SLinus Torvalds static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos) 5201da177e4SLinus Torvalds { 5213d5ff529SJosef Sipek ino_t ino = file->f_path.dentry->d_inode->i_ino; 5221da177e4SLinus Torvalds char *data; 5231da177e4SLinus Torvalds ssize_t rv; 5241da177e4SLinus Torvalds 5256e20a64aSNicolas Kaiser if (ino >= ARRAY_SIZE(write_op) || !write_op[ino]) 5261da177e4SLinus Torvalds return -EINVAL; 5271da177e4SLinus Torvalds 5281da177e4SLinus Torvalds data = simple_transaction_get(file, buf, size); 5291da177e4SLinus Torvalds if (IS_ERR(data)) 5301da177e4SLinus Torvalds return PTR_ERR(data); 5311da177e4SLinus Torvalds 5321da177e4SLinus Torvalds rv = write_op[ino](file, data, size); 5331da177e4SLinus Torvalds if (rv>0) { 5341da177e4SLinus Torvalds simple_transaction_set(file, rv); 5351da177e4SLinus Torvalds rv = size; 5361da177e4SLinus Torvalds } 5371da177e4SLinus Torvalds return rv; 5381da177e4SLinus Torvalds } 5391da177e4SLinus Torvalds 5409c2e08c5SArjan van de Ven static const struct file_operations transaction_ops = { 5411da177e4SLinus Torvalds .write = selinux_transaction_write, 5421da177e4SLinus Torvalds .read = simple_transaction_read, 5431da177e4SLinus Torvalds .release = simple_transaction_release, 5441da177e4SLinus Torvalds }; 5451da177e4SLinus Torvalds 5461da177e4SLinus Torvalds /* 5471da177e4SLinus Torvalds * payload - write methods 5481da177e4SLinus Torvalds * If the method has a response, the response should be put in buf, 5491da177e4SLinus Torvalds * and the length returned. Otherwise return 0 or and -error. 5501da177e4SLinus Torvalds */ 5511da177e4SLinus Torvalds 5521da177e4SLinus Torvalds static ssize_t sel_write_access(struct file * file, char *buf, size_t size) 5531da177e4SLinus Torvalds { 5541da177e4SLinus Torvalds char *scon, *tcon; 5551da177e4SLinus Torvalds u32 ssid, tsid; 5561da177e4SLinus Torvalds u16 tclass; 5571da177e4SLinus Torvalds u32 req; 5581da177e4SLinus Torvalds struct av_decision avd; 5591da177e4SLinus Torvalds ssize_t length; 5601da177e4SLinus Torvalds 5611da177e4SLinus Torvalds length = task_has_security(current, SECURITY__COMPUTE_AV); 5621da177e4SLinus Torvalds if (length) 5631da177e4SLinus Torvalds return length; 5641da177e4SLinus Torvalds 5651da177e4SLinus Torvalds length = -ENOMEM; 56689d155efSJames Morris scon = kzalloc(size+1, GFP_KERNEL); 5671da177e4SLinus Torvalds if (!scon) 5681da177e4SLinus Torvalds return length; 5691da177e4SLinus Torvalds 57089d155efSJames Morris tcon = kzalloc(size+1, GFP_KERNEL); 5711da177e4SLinus Torvalds if (!tcon) 5721da177e4SLinus Torvalds goto out; 5731da177e4SLinus Torvalds 5741da177e4SLinus Torvalds length = -EINVAL; 5751da177e4SLinus Torvalds if (sscanf(buf, "%s %s %hu %x", scon, tcon, &tclass, &req) != 4) 5761da177e4SLinus Torvalds goto out2; 5771da177e4SLinus Torvalds 5781da177e4SLinus Torvalds length = security_context_to_sid(scon, strlen(scon)+1, &ssid); 5791da177e4SLinus Torvalds if (length < 0) 5801da177e4SLinus Torvalds goto out2; 5811da177e4SLinus Torvalds length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid); 5821da177e4SLinus Torvalds if (length < 0) 5831da177e4SLinus Torvalds goto out2; 5841da177e4SLinus Torvalds 5851da177e4SLinus Torvalds length = security_compute_av(ssid, tsid, tclass, req, &avd); 5861da177e4SLinus Torvalds if (length < 0) 5871da177e4SLinus Torvalds goto out2; 5881da177e4SLinus Torvalds 5891da177e4SLinus Torvalds length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, 5901da177e4SLinus Torvalds "%x %x %x %x %u", 5911da177e4SLinus Torvalds avd.allowed, avd.decided, 5921da177e4SLinus Torvalds avd.auditallow, avd.auditdeny, 5931da177e4SLinus Torvalds avd.seqno); 5941da177e4SLinus Torvalds out2: 5951da177e4SLinus Torvalds kfree(tcon); 5961da177e4SLinus Torvalds out: 5971da177e4SLinus Torvalds kfree(scon); 5981da177e4SLinus Torvalds return length; 5991da177e4SLinus Torvalds } 6001da177e4SLinus Torvalds 6011da177e4SLinus Torvalds static ssize_t sel_write_create(struct file * file, char *buf, size_t size) 6021da177e4SLinus Torvalds { 6031da177e4SLinus Torvalds char *scon, *tcon; 6041da177e4SLinus Torvalds u32 ssid, tsid, newsid; 6051da177e4SLinus Torvalds u16 tclass; 6061da177e4SLinus Torvalds ssize_t length; 6071da177e4SLinus Torvalds char *newcon; 6081da177e4SLinus Torvalds u32 len; 6091da177e4SLinus Torvalds 6101da177e4SLinus Torvalds length = task_has_security(current, SECURITY__COMPUTE_CREATE); 6111da177e4SLinus Torvalds if (length) 6121da177e4SLinus Torvalds return length; 6131da177e4SLinus Torvalds 6141da177e4SLinus Torvalds length = -ENOMEM; 61589d155efSJames Morris scon = kzalloc(size+1, GFP_KERNEL); 6161da177e4SLinus Torvalds if (!scon) 6171da177e4SLinus Torvalds return length; 6181da177e4SLinus Torvalds 61989d155efSJames Morris tcon = kzalloc(size+1, GFP_KERNEL); 6201da177e4SLinus Torvalds if (!tcon) 6211da177e4SLinus Torvalds goto out; 6221da177e4SLinus Torvalds 6231da177e4SLinus Torvalds length = -EINVAL; 6241da177e4SLinus Torvalds if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) 6251da177e4SLinus Torvalds goto out2; 6261da177e4SLinus Torvalds 6271da177e4SLinus Torvalds length = security_context_to_sid(scon, strlen(scon)+1, &ssid); 6281da177e4SLinus Torvalds if (length < 0) 6291da177e4SLinus Torvalds goto out2; 6301da177e4SLinus Torvalds length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid); 6311da177e4SLinus Torvalds if (length < 0) 6321da177e4SLinus Torvalds goto out2; 6331da177e4SLinus Torvalds 6341da177e4SLinus Torvalds length = security_transition_sid(ssid, tsid, tclass, &newsid); 6351da177e4SLinus Torvalds if (length < 0) 6361da177e4SLinus Torvalds goto out2; 6371da177e4SLinus Torvalds 6381da177e4SLinus Torvalds length = security_sid_to_context(newsid, &newcon, &len); 6391da177e4SLinus Torvalds if (length < 0) 6401da177e4SLinus Torvalds goto out2; 6411da177e4SLinus Torvalds 6421da177e4SLinus Torvalds if (len > SIMPLE_TRANSACTION_LIMIT) { 6431da177e4SLinus Torvalds printk(KERN_ERR "%s: context size (%u) exceeds payload " 6441da177e4SLinus Torvalds "max\n", __FUNCTION__, len); 6451da177e4SLinus Torvalds length = -ERANGE; 6461da177e4SLinus Torvalds goto out3; 6471da177e4SLinus Torvalds } 6481da177e4SLinus Torvalds 6491da177e4SLinus Torvalds memcpy(buf, newcon, len); 6501da177e4SLinus Torvalds length = len; 6511da177e4SLinus Torvalds out3: 6521da177e4SLinus Torvalds kfree(newcon); 6531da177e4SLinus Torvalds out2: 6541da177e4SLinus Torvalds kfree(tcon); 6551da177e4SLinus Torvalds out: 6561da177e4SLinus Torvalds kfree(scon); 6571da177e4SLinus Torvalds return length; 6581da177e4SLinus Torvalds } 6591da177e4SLinus Torvalds 6601da177e4SLinus Torvalds static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size) 6611da177e4SLinus Torvalds { 6621da177e4SLinus Torvalds char *scon, *tcon; 6631da177e4SLinus Torvalds u32 ssid, tsid, newsid; 6641da177e4SLinus Torvalds u16 tclass; 6651da177e4SLinus Torvalds ssize_t length; 6661da177e4SLinus Torvalds char *newcon; 6671da177e4SLinus Torvalds u32 len; 6681da177e4SLinus Torvalds 6691da177e4SLinus Torvalds length = task_has_security(current, SECURITY__COMPUTE_RELABEL); 6701da177e4SLinus Torvalds if (length) 6711da177e4SLinus Torvalds return length; 6721da177e4SLinus Torvalds 6731da177e4SLinus Torvalds length = -ENOMEM; 67489d155efSJames Morris scon = kzalloc(size+1, GFP_KERNEL); 6751da177e4SLinus Torvalds if (!scon) 6761da177e4SLinus Torvalds return length; 6771da177e4SLinus Torvalds 67889d155efSJames Morris tcon = kzalloc(size+1, GFP_KERNEL); 6791da177e4SLinus Torvalds if (!tcon) 6801da177e4SLinus Torvalds goto out; 6811da177e4SLinus Torvalds 6821da177e4SLinus Torvalds length = -EINVAL; 6831da177e4SLinus Torvalds if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) 6841da177e4SLinus Torvalds goto out2; 6851da177e4SLinus Torvalds 6861da177e4SLinus Torvalds length = security_context_to_sid(scon, strlen(scon)+1, &ssid); 6871da177e4SLinus Torvalds if (length < 0) 6881da177e4SLinus Torvalds goto out2; 6891da177e4SLinus Torvalds length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid); 6901da177e4SLinus Torvalds if (length < 0) 6911da177e4SLinus Torvalds goto out2; 6921da177e4SLinus Torvalds 6931da177e4SLinus Torvalds length = security_change_sid(ssid, tsid, tclass, &newsid); 6941da177e4SLinus Torvalds if (length < 0) 6951da177e4SLinus Torvalds goto out2; 6961da177e4SLinus Torvalds 6971da177e4SLinus Torvalds length = security_sid_to_context(newsid, &newcon, &len); 6981da177e4SLinus Torvalds if (length < 0) 6991da177e4SLinus Torvalds goto out2; 7001da177e4SLinus Torvalds 7011da177e4SLinus Torvalds if (len > SIMPLE_TRANSACTION_LIMIT) { 7021da177e4SLinus Torvalds length = -ERANGE; 7031da177e4SLinus Torvalds goto out3; 7041da177e4SLinus Torvalds } 7051da177e4SLinus Torvalds 7061da177e4SLinus Torvalds memcpy(buf, newcon, len); 7071da177e4SLinus Torvalds length = len; 7081da177e4SLinus Torvalds out3: 7091da177e4SLinus Torvalds kfree(newcon); 7101da177e4SLinus Torvalds out2: 7111da177e4SLinus Torvalds kfree(tcon); 7121da177e4SLinus Torvalds out: 7131da177e4SLinus Torvalds kfree(scon); 7141da177e4SLinus Torvalds return length; 7151da177e4SLinus Torvalds } 7161da177e4SLinus Torvalds 7171da177e4SLinus Torvalds static ssize_t sel_write_user(struct file * file, char *buf, size_t size) 7181da177e4SLinus Torvalds { 7191da177e4SLinus Torvalds char *con, *user, *ptr; 7201da177e4SLinus Torvalds u32 sid, *sids; 7211da177e4SLinus Torvalds ssize_t length; 7221da177e4SLinus Torvalds char *newcon; 7231da177e4SLinus Torvalds int i, rc; 7241da177e4SLinus Torvalds u32 len, nsids; 7251da177e4SLinus Torvalds 7261da177e4SLinus Torvalds length = task_has_security(current, SECURITY__COMPUTE_USER); 7271da177e4SLinus Torvalds if (length) 7281da177e4SLinus Torvalds return length; 7291da177e4SLinus Torvalds 7301da177e4SLinus Torvalds length = -ENOMEM; 73189d155efSJames Morris con = kzalloc(size+1, GFP_KERNEL); 7321da177e4SLinus Torvalds if (!con) 7331da177e4SLinus Torvalds return length; 7341da177e4SLinus Torvalds 73589d155efSJames Morris user = kzalloc(size+1, GFP_KERNEL); 7361da177e4SLinus Torvalds if (!user) 7371da177e4SLinus Torvalds goto out; 7381da177e4SLinus Torvalds 7391da177e4SLinus Torvalds length = -EINVAL; 7401da177e4SLinus Torvalds if (sscanf(buf, "%s %s", con, user) != 2) 7411da177e4SLinus Torvalds goto out2; 7421da177e4SLinus Torvalds 7431da177e4SLinus Torvalds length = security_context_to_sid(con, strlen(con)+1, &sid); 7441da177e4SLinus Torvalds if (length < 0) 7451da177e4SLinus Torvalds goto out2; 7461da177e4SLinus Torvalds 7471da177e4SLinus Torvalds length = security_get_user_sids(sid, user, &sids, &nsids); 7481da177e4SLinus Torvalds if (length < 0) 7491da177e4SLinus Torvalds goto out2; 7501da177e4SLinus Torvalds 7511da177e4SLinus Torvalds length = sprintf(buf, "%u", nsids) + 1; 7521da177e4SLinus Torvalds ptr = buf + length; 7531da177e4SLinus Torvalds for (i = 0; i < nsids; i++) { 7541da177e4SLinus Torvalds rc = security_sid_to_context(sids[i], &newcon, &len); 7551da177e4SLinus Torvalds if (rc) { 7561da177e4SLinus Torvalds length = rc; 7571da177e4SLinus Torvalds goto out3; 7581da177e4SLinus Torvalds } 7591da177e4SLinus Torvalds if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) { 7601da177e4SLinus Torvalds kfree(newcon); 7611da177e4SLinus Torvalds length = -ERANGE; 7621da177e4SLinus Torvalds goto out3; 7631da177e4SLinus Torvalds } 7641da177e4SLinus Torvalds memcpy(ptr, newcon, len); 7651da177e4SLinus Torvalds kfree(newcon); 7661da177e4SLinus Torvalds ptr += len; 7671da177e4SLinus Torvalds length += len; 7681da177e4SLinus Torvalds } 7691da177e4SLinus Torvalds out3: 7701da177e4SLinus Torvalds kfree(sids); 7711da177e4SLinus Torvalds out2: 7721da177e4SLinus Torvalds kfree(user); 7731da177e4SLinus Torvalds out: 7741da177e4SLinus Torvalds kfree(con); 7751da177e4SLinus Torvalds return length; 7761da177e4SLinus Torvalds } 7771da177e4SLinus Torvalds 7781da177e4SLinus Torvalds static ssize_t sel_write_member(struct file * file, char *buf, size_t size) 7791da177e4SLinus Torvalds { 7801da177e4SLinus Torvalds char *scon, *tcon; 7811da177e4SLinus Torvalds u32 ssid, tsid, newsid; 7821da177e4SLinus Torvalds u16 tclass; 7831da177e4SLinus Torvalds ssize_t length; 7841da177e4SLinus Torvalds char *newcon; 7851da177e4SLinus Torvalds u32 len; 7861da177e4SLinus Torvalds 7871da177e4SLinus Torvalds length = task_has_security(current, SECURITY__COMPUTE_MEMBER); 7881da177e4SLinus Torvalds if (length) 7891da177e4SLinus Torvalds return length; 7901da177e4SLinus Torvalds 7911da177e4SLinus Torvalds length = -ENOMEM; 79289d155efSJames Morris scon = kzalloc(size+1, GFP_KERNEL); 7931da177e4SLinus Torvalds if (!scon) 7941da177e4SLinus Torvalds return length; 7951da177e4SLinus Torvalds 79689d155efSJames Morris tcon = kzalloc(size+1, GFP_KERNEL); 7971da177e4SLinus Torvalds if (!tcon) 7981da177e4SLinus Torvalds goto out; 7991da177e4SLinus Torvalds 8001da177e4SLinus Torvalds length = -EINVAL; 8011da177e4SLinus Torvalds if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) 8021da177e4SLinus Torvalds goto out2; 8031da177e4SLinus Torvalds 8041da177e4SLinus Torvalds length = security_context_to_sid(scon, strlen(scon)+1, &ssid); 8051da177e4SLinus Torvalds if (length < 0) 8061da177e4SLinus Torvalds goto out2; 8071da177e4SLinus Torvalds length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid); 8081da177e4SLinus Torvalds if (length < 0) 8091da177e4SLinus Torvalds goto out2; 8101da177e4SLinus Torvalds 8111da177e4SLinus Torvalds length = security_member_sid(ssid, tsid, tclass, &newsid); 8121da177e4SLinus Torvalds if (length < 0) 8131da177e4SLinus Torvalds goto out2; 8141da177e4SLinus Torvalds 8151da177e4SLinus Torvalds length = security_sid_to_context(newsid, &newcon, &len); 8161da177e4SLinus Torvalds if (length < 0) 8171da177e4SLinus Torvalds goto out2; 8181da177e4SLinus Torvalds 8191da177e4SLinus Torvalds if (len > SIMPLE_TRANSACTION_LIMIT) { 8201da177e4SLinus Torvalds printk(KERN_ERR "%s: context size (%u) exceeds payload " 8211da177e4SLinus Torvalds "max\n", __FUNCTION__, len); 8221da177e4SLinus Torvalds length = -ERANGE; 8231da177e4SLinus Torvalds goto out3; 8241da177e4SLinus Torvalds } 8251da177e4SLinus Torvalds 8261da177e4SLinus Torvalds memcpy(buf, newcon, len); 8271da177e4SLinus Torvalds length = len; 8281da177e4SLinus Torvalds out3: 8291da177e4SLinus Torvalds kfree(newcon); 8301da177e4SLinus Torvalds out2: 8311da177e4SLinus Torvalds kfree(tcon); 8321da177e4SLinus Torvalds out: 8331da177e4SLinus Torvalds kfree(scon); 8341da177e4SLinus Torvalds return length; 8351da177e4SLinus Torvalds } 8361da177e4SLinus Torvalds 8371da177e4SLinus Torvalds static struct inode *sel_make_inode(struct super_block *sb, int mode) 8381da177e4SLinus Torvalds { 8391da177e4SLinus Torvalds struct inode *ret = new_inode(sb); 8401da177e4SLinus Torvalds 8411da177e4SLinus Torvalds if (ret) { 8421da177e4SLinus Torvalds ret->i_mode = mode; 8431da177e4SLinus Torvalds ret->i_uid = ret->i_gid = 0; 8441da177e4SLinus Torvalds ret->i_blocks = 0; 8451da177e4SLinus Torvalds ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME; 8461da177e4SLinus Torvalds } 8471da177e4SLinus Torvalds return ret; 8481da177e4SLinus Torvalds } 8491da177e4SLinus Torvalds 8501da177e4SLinus Torvalds static ssize_t sel_read_bool(struct file *filep, char __user *buf, 8511da177e4SLinus Torvalds size_t count, loff_t *ppos) 8521da177e4SLinus Torvalds { 8531da177e4SLinus Torvalds char *page = NULL; 8541da177e4SLinus Torvalds ssize_t length; 8551da177e4SLinus Torvalds ssize_t ret; 8561da177e4SLinus Torvalds int cur_enforcing; 857d313f948SStephen Smalley struct inode *inode = filep->f_path.dentry->d_inode; 858d313f948SStephen Smalley unsigned index = inode->i_ino & SEL_INO_MASK; 859d313f948SStephen Smalley const char *name = filep->f_path.dentry->d_name.name; 8601da177e4SLinus Torvalds 861bb003079SIngo Molnar mutex_lock(&sel_mutex); 8621da177e4SLinus Torvalds 863d313f948SStephen Smalley if (index >= bool_num || strcmp(name, bool_pending_names[index])) { 864d313f948SStephen Smalley ret = -EINVAL; 865d313f948SStephen Smalley goto out; 866d313f948SStephen Smalley } 8671da177e4SLinus Torvalds 868bfd51626SDavi Arnaut if (count > PAGE_SIZE) { 8691da177e4SLinus Torvalds ret = -EINVAL; 8701da177e4SLinus Torvalds goto out; 8711da177e4SLinus Torvalds } 8721da177e4SLinus Torvalds if (!(page = (char*)get_zeroed_page(GFP_KERNEL))) { 8731da177e4SLinus Torvalds ret = -ENOMEM; 8741da177e4SLinus Torvalds goto out; 8751da177e4SLinus Torvalds } 8761da177e4SLinus Torvalds 877d313f948SStephen Smalley cur_enforcing = security_get_bool_value(index); 8781da177e4SLinus Torvalds if (cur_enforcing < 0) { 8791da177e4SLinus Torvalds ret = cur_enforcing; 8801da177e4SLinus Torvalds goto out; 8811da177e4SLinus Torvalds } 8821da177e4SLinus Torvalds length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing, 883d313f948SStephen Smalley bool_pending_values[index]); 88468bdcf28SStephen Smalley ret = simple_read_from_buffer(buf, count, ppos, page, length); 8851da177e4SLinus Torvalds out: 886bb003079SIngo Molnar mutex_unlock(&sel_mutex); 8871da177e4SLinus Torvalds if (page) 8881da177e4SLinus Torvalds free_page((unsigned long)page); 8891da177e4SLinus Torvalds return ret; 8901da177e4SLinus Torvalds } 8911da177e4SLinus Torvalds 8921da177e4SLinus Torvalds static ssize_t sel_write_bool(struct file *filep, const char __user *buf, 8931da177e4SLinus Torvalds size_t count, loff_t *ppos) 8941da177e4SLinus Torvalds { 8951da177e4SLinus Torvalds char *page = NULL; 896d313f948SStephen Smalley ssize_t length; 8971da177e4SLinus Torvalds int new_value; 898d313f948SStephen Smalley struct inode *inode = filep->f_path.dentry->d_inode; 899d313f948SStephen Smalley unsigned index = inode->i_ino & SEL_INO_MASK; 900d313f948SStephen Smalley const char *name = filep->f_path.dentry->d_name.name; 9011da177e4SLinus Torvalds 902bb003079SIngo Molnar mutex_lock(&sel_mutex); 9031da177e4SLinus Torvalds 9041da177e4SLinus Torvalds length = task_has_security(current, SECURITY__SETBOOL); 9051da177e4SLinus Torvalds if (length) 9061da177e4SLinus Torvalds goto out; 9071da177e4SLinus Torvalds 908d313f948SStephen Smalley if (index >= bool_num || strcmp(name, bool_pending_names[index])) { 909d313f948SStephen Smalley length = -EINVAL; 910d313f948SStephen Smalley goto out; 911d313f948SStephen Smalley } 912d313f948SStephen Smalley 913bfd51626SDavi Arnaut if (count >= PAGE_SIZE) { 9141da177e4SLinus Torvalds length = -ENOMEM; 9151da177e4SLinus Torvalds goto out; 9161da177e4SLinus Torvalds } 917d313f948SStephen Smalley 9181da177e4SLinus Torvalds if (*ppos != 0) { 9191da177e4SLinus Torvalds /* No partial writes. */ 920d313f948SStephen Smalley length = -EINVAL; 9211da177e4SLinus Torvalds goto out; 9221da177e4SLinus Torvalds } 9231da177e4SLinus Torvalds page = (char*)get_zeroed_page(GFP_KERNEL); 9241da177e4SLinus Torvalds if (!page) { 9251da177e4SLinus Torvalds length = -ENOMEM; 9261da177e4SLinus Torvalds goto out; 9271da177e4SLinus Torvalds } 9281da177e4SLinus Torvalds 929d313f948SStephen Smalley length = -EFAULT; 9301da177e4SLinus Torvalds if (copy_from_user(page, buf, count)) 9311da177e4SLinus Torvalds goto out; 9321da177e4SLinus Torvalds 9331da177e4SLinus Torvalds length = -EINVAL; 9341da177e4SLinus Torvalds if (sscanf(page, "%d", &new_value) != 1) 9351da177e4SLinus Torvalds goto out; 9361da177e4SLinus Torvalds 9371da177e4SLinus Torvalds if (new_value) 9381da177e4SLinus Torvalds new_value = 1; 9391da177e4SLinus Torvalds 940d313f948SStephen Smalley bool_pending_values[index] = new_value; 9411da177e4SLinus Torvalds length = count; 9421da177e4SLinus Torvalds 9431da177e4SLinus Torvalds out: 944bb003079SIngo Molnar mutex_unlock(&sel_mutex); 9451da177e4SLinus Torvalds if (page) 9461da177e4SLinus Torvalds free_page((unsigned long) page); 9471da177e4SLinus Torvalds return length; 9481da177e4SLinus Torvalds } 9491da177e4SLinus Torvalds 9509c2e08c5SArjan van de Ven static const struct file_operations sel_bool_ops = { 9511da177e4SLinus Torvalds .read = sel_read_bool, 9521da177e4SLinus Torvalds .write = sel_write_bool, 9531da177e4SLinus Torvalds }; 9541da177e4SLinus Torvalds 9551da177e4SLinus Torvalds static ssize_t sel_commit_bools_write(struct file *filep, 9561da177e4SLinus Torvalds const char __user *buf, 9571da177e4SLinus Torvalds size_t count, loff_t *ppos) 9581da177e4SLinus Torvalds { 9591da177e4SLinus Torvalds char *page = NULL; 960d313f948SStephen Smalley ssize_t length; 9611da177e4SLinus Torvalds int new_value; 9621da177e4SLinus Torvalds 963bb003079SIngo Molnar mutex_lock(&sel_mutex); 9641da177e4SLinus Torvalds 9651da177e4SLinus Torvalds length = task_has_security(current, SECURITY__SETBOOL); 9661da177e4SLinus Torvalds if (length) 9671da177e4SLinus Torvalds goto out; 9681da177e4SLinus Torvalds 969bfd51626SDavi Arnaut if (count >= PAGE_SIZE) { 9701da177e4SLinus Torvalds length = -ENOMEM; 9711da177e4SLinus Torvalds goto out; 9721da177e4SLinus Torvalds } 9731da177e4SLinus Torvalds if (*ppos != 0) { 9741da177e4SLinus Torvalds /* No partial writes. */ 9751da177e4SLinus Torvalds goto out; 9761da177e4SLinus Torvalds } 9771da177e4SLinus Torvalds page = (char*)get_zeroed_page(GFP_KERNEL); 9781da177e4SLinus Torvalds if (!page) { 9791da177e4SLinus Torvalds length = -ENOMEM; 9801da177e4SLinus Torvalds goto out; 9811da177e4SLinus Torvalds } 9821da177e4SLinus Torvalds 983d313f948SStephen Smalley length = -EFAULT; 9841da177e4SLinus Torvalds if (copy_from_user(page, buf, count)) 9851da177e4SLinus Torvalds goto out; 9861da177e4SLinus Torvalds 9871da177e4SLinus Torvalds length = -EINVAL; 9881da177e4SLinus Torvalds if (sscanf(page, "%d", &new_value) != 1) 9891da177e4SLinus Torvalds goto out; 9901da177e4SLinus Torvalds 99120c19e41SDavi Arnaut if (new_value && bool_pending_values) { 9921da177e4SLinus Torvalds security_set_bools(bool_num, bool_pending_values); 9931da177e4SLinus Torvalds } 9941da177e4SLinus Torvalds 9951da177e4SLinus Torvalds length = count; 9961da177e4SLinus Torvalds 9971da177e4SLinus Torvalds out: 998bb003079SIngo Molnar mutex_unlock(&sel_mutex); 9991da177e4SLinus Torvalds if (page) 10001da177e4SLinus Torvalds free_page((unsigned long) page); 10011da177e4SLinus Torvalds return length; 10021da177e4SLinus Torvalds } 10031da177e4SLinus Torvalds 10049c2e08c5SArjan van de Ven static const struct file_operations sel_commit_bools_ops = { 10051da177e4SLinus Torvalds .write = sel_commit_bools_write, 10061da177e4SLinus Torvalds }; 10071da177e4SLinus Torvalds 10080c92d7c7SChristopher J. PeBenito static void sel_remove_entries(struct dentry *de) 10091da177e4SLinus Torvalds { 10100955dc03SStephen Smalley struct list_head *node; 10111da177e4SLinus Torvalds 10121da177e4SLinus Torvalds spin_lock(&dcache_lock); 10131da177e4SLinus Torvalds node = de->d_subdirs.next; 10141da177e4SLinus Torvalds while (node != &de->d_subdirs) { 10155160ee6fSEric Dumazet struct dentry *d = list_entry(node, struct dentry, d_u.d_child); 10161da177e4SLinus Torvalds list_del_init(node); 10171da177e4SLinus Torvalds 10181da177e4SLinus Torvalds if (d->d_inode) { 10191da177e4SLinus Torvalds d = dget_locked(d); 10201da177e4SLinus Torvalds spin_unlock(&dcache_lock); 10211da177e4SLinus Torvalds d_delete(d); 10221da177e4SLinus Torvalds simple_unlink(de->d_inode, d); 10231da177e4SLinus Torvalds dput(d); 10241da177e4SLinus Torvalds spin_lock(&dcache_lock); 10251da177e4SLinus Torvalds } 10261da177e4SLinus Torvalds node = de->d_subdirs.next; 10271da177e4SLinus Torvalds } 10281da177e4SLinus Torvalds 10291da177e4SLinus Torvalds spin_unlock(&dcache_lock); 10301da177e4SLinus Torvalds } 10311da177e4SLinus Torvalds 10321da177e4SLinus Torvalds #define BOOL_DIR_NAME "booleans" 10331da177e4SLinus Torvalds 10341da177e4SLinus Torvalds static int sel_make_bools(void) 10351da177e4SLinus Torvalds { 10361da177e4SLinus Torvalds int i, ret = 0; 10371da177e4SLinus Torvalds ssize_t len; 10381da177e4SLinus Torvalds struct dentry *dentry = NULL; 10391da177e4SLinus Torvalds struct dentry *dir = bool_dir; 10401da177e4SLinus Torvalds struct inode *inode = NULL; 10411da177e4SLinus Torvalds struct inode_security_struct *isec; 10421da177e4SLinus Torvalds char **names = NULL, *page; 10431da177e4SLinus Torvalds int num; 10441da177e4SLinus Torvalds int *values = NULL; 10451da177e4SLinus Torvalds u32 sid; 10461da177e4SLinus Torvalds 10471da177e4SLinus Torvalds /* remove any existing files */ 1048d313f948SStephen Smalley kfree(bool_pending_names); 10491da177e4SLinus Torvalds kfree(bool_pending_values); 1050d313f948SStephen Smalley bool_pending_names = NULL; 105120c19e41SDavi Arnaut bool_pending_values = NULL; 10521da177e4SLinus Torvalds 10530c92d7c7SChristopher J. PeBenito sel_remove_entries(dir); 10541da177e4SLinus Torvalds 10551da177e4SLinus Torvalds if (!(page = (char*)get_zeroed_page(GFP_KERNEL))) 10561da177e4SLinus Torvalds return -ENOMEM; 10571da177e4SLinus Torvalds 10581da177e4SLinus Torvalds ret = security_get_bools(&num, &names, &values); 10591da177e4SLinus Torvalds if (ret != 0) 10601da177e4SLinus Torvalds goto out; 10611da177e4SLinus Torvalds 10621da177e4SLinus Torvalds for (i = 0; i < num; i++) { 10631da177e4SLinus Torvalds dentry = d_alloc_name(dir, names[i]); 10641da177e4SLinus Torvalds if (!dentry) { 10651da177e4SLinus Torvalds ret = -ENOMEM; 10661da177e4SLinus Torvalds goto err; 10671da177e4SLinus Torvalds } 10681da177e4SLinus Torvalds inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR); 10691da177e4SLinus Torvalds if (!inode) { 10701da177e4SLinus Torvalds ret = -ENOMEM; 10711da177e4SLinus Torvalds goto err; 10721da177e4SLinus Torvalds } 10731da177e4SLinus Torvalds 10741da177e4SLinus Torvalds len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]); 10751da177e4SLinus Torvalds if (len < 0) { 10761da177e4SLinus Torvalds ret = -EINVAL; 10771da177e4SLinus Torvalds goto err; 10781da177e4SLinus Torvalds } else if (len >= PAGE_SIZE) { 10791da177e4SLinus Torvalds ret = -ENAMETOOLONG; 10801da177e4SLinus Torvalds goto err; 10811da177e4SLinus Torvalds } 10821da177e4SLinus Torvalds isec = (struct inode_security_struct*)inode->i_security; 10831da177e4SLinus Torvalds if ((ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid))) 10841da177e4SLinus Torvalds goto err; 10851da177e4SLinus Torvalds isec->sid = sid; 10861da177e4SLinus Torvalds isec->initialized = 1; 10871da177e4SLinus Torvalds inode->i_fop = &sel_bool_ops; 1088bce34bc0SJames Carter inode->i_ino = i|SEL_BOOL_INO_OFFSET; 10891da177e4SLinus Torvalds d_add(dentry, inode); 10901da177e4SLinus Torvalds } 10911da177e4SLinus Torvalds bool_num = num; 1092d313f948SStephen Smalley bool_pending_names = names; 10931da177e4SLinus Torvalds bool_pending_values = values; 10941da177e4SLinus Torvalds out: 10951da177e4SLinus Torvalds free_page((unsigned long)page); 1096d313f948SStephen Smalley return ret; 1097d313f948SStephen Smalley err: 10981da177e4SLinus Torvalds if (names) { 10999a5f04bfSJesper Juhl for (i = 0; i < num; i++) 11001da177e4SLinus Torvalds kfree(names[i]); 11011da177e4SLinus Torvalds kfree(names); 11021da177e4SLinus Torvalds } 110320c19e41SDavi Arnaut kfree(values); 11040c92d7c7SChristopher J. PeBenito sel_remove_entries(dir); 11051da177e4SLinus Torvalds ret = -ENOMEM; 11061da177e4SLinus Torvalds goto out; 11071da177e4SLinus Torvalds } 11081da177e4SLinus Torvalds 11091da177e4SLinus Torvalds #define NULL_FILE_NAME "null" 11101da177e4SLinus Torvalds 11111da177e4SLinus Torvalds struct dentry *selinux_null = NULL; 11121da177e4SLinus Torvalds 11131da177e4SLinus Torvalds static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf, 11141da177e4SLinus Torvalds size_t count, loff_t *ppos) 11151da177e4SLinus Torvalds { 11161da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 11171da177e4SLinus Torvalds ssize_t length; 11181da177e4SLinus Torvalds 11191da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold); 11201da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 11211da177e4SLinus Torvalds } 11221da177e4SLinus Torvalds 11231da177e4SLinus Torvalds static ssize_t sel_write_avc_cache_threshold(struct file * file, 11241da177e4SLinus Torvalds const char __user * buf, 11251da177e4SLinus Torvalds size_t count, loff_t *ppos) 11261da177e4SLinus Torvalds 11271da177e4SLinus Torvalds { 11281da177e4SLinus Torvalds char *page; 11291da177e4SLinus Torvalds ssize_t ret; 11301da177e4SLinus Torvalds int new_value; 11311da177e4SLinus Torvalds 1132bfd51626SDavi Arnaut if (count >= PAGE_SIZE) { 11331da177e4SLinus Torvalds ret = -ENOMEM; 11341da177e4SLinus Torvalds goto out; 11351da177e4SLinus Torvalds } 11361da177e4SLinus Torvalds 11371da177e4SLinus Torvalds if (*ppos != 0) { 11381da177e4SLinus Torvalds /* No partial writes. */ 11391da177e4SLinus Torvalds ret = -EINVAL; 11401da177e4SLinus Torvalds goto out; 11411da177e4SLinus Torvalds } 11421da177e4SLinus Torvalds 11431da177e4SLinus Torvalds page = (char*)get_zeroed_page(GFP_KERNEL); 11441da177e4SLinus Torvalds if (!page) { 11451da177e4SLinus Torvalds ret = -ENOMEM; 11461da177e4SLinus Torvalds goto out; 11471da177e4SLinus Torvalds } 11481da177e4SLinus Torvalds 11491da177e4SLinus Torvalds if (copy_from_user(page, buf, count)) { 11501da177e4SLinus Torvalds ret = -EFAULT; 11511da177e4SLinus Torvalds goto out_free; 11521da177e4SLinus Torvalds } 11531da177e4SLinus Torvalds 11541da177e4SLinus Torvalds if (sscanf(page, "%u", &new_value) != 1) { 11551da177e4SLinus Torvalds ret = -EINVAL; 11561da177e4SLinus Torvalds goto out; 11571da177e4SLinus Torvalds } 11581da177e4SLinus Torvalds 11591da177e4SLinus Torvalds if (new_value != avc_cache_threshold) { 11601da177e4SLinus Torvalds ret = task_has_security(current, SECURITY__SETSECPARAM); 11611da177e4SLinus Torvalds if (ret) 11621da177e4SLinus Torvalds goto out_free; 11631da177e4SLinus Torvalds avc_cache_threshold = new_value; 11641da177e4SLinus Torvalds } 11651da177e4SLinus Torvalds ret = count; 11661da177e4SLinus Torvalds out_free: 11671da177e4SLinus Torvalds free_page((unsigned long)page); 11681da177e4SLinus Torvalds out: 11691da177e4SLinus Torvalds return ret; 11701da177e4SLinus Torvalds } 11711da177e4SLinus Torvalds 11721da177e4SLinus Torvalds static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf, 11731da177e4SLinus Torvalds size_t count, loff_t *ppos) 11741da177e4SLinus Torvalds { 11751da177e4SLinus Torvalds char *page; 11761da177e4SLinus Torvalds ssize_t ret = 0; 11771da177e4SLinus Torvalds 11781da177e4SLinus Torvalds page = (char *)__get_free_page(GFP_KERNEL); 11791da177e4SLinus Torvalds if (!page) { 11801da177e4SLinus Torvalds ret = -ENOMEM; 11811da177e4SLinus Torvalds goto out; 11821da177e4SLinus Torvalds } 11831da177e4SLinus Torvalds ret = avc_get_hash_stats(page); 11841da177e4SLinus Torvalds if (ret >= 0) 11851da177e4SLinus Torvalds ret = simple_read_from_buffer(buf, count, ppos, page, ret); 11861da177e4SLinus Torvalds free_page((unsigned long)page); 11871da177e4SLinus Torvalds out: 11881da177e4SLinus Torvalds return ret; 11891da177e4SLinus Torvalds } 11901da177e4SLinus Torvalds 11919c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_threshold_ops = { 11921da177e4SLinus Torvalds .read = sel_read_avc_cache_threshold, 11931da177e4SLinus Torvalds .write = sel_write_avc_cache_threshold, 11941da177e4SLinus Torvalds }; 11951da177e4SLinus Torvalds 11969c2e08c5SArjan van de Ven static const struct file_operations sel_avc_hash_stats_ops = { 11971da177e4SLinus Torvalds .read = sel_read_avc_hash_stats, 11981da177e4SLinus Torvalds }; 11991da177e4SLinus Torvalds 12001da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS 12011da177e4SLinus Torvalds static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx) 12021da177e4SLinus Torvalds { 12031da177e4SLinus Torvalds int cpu; 12041da177e4SLinus Torvalds 12051da177e4SLinus Torvalds for (cpu = *idx; cpu < NR_CPUS; ++cpu) { 12061da177e4SLinus Torvalds if (!cpu_possible(cpu)) 12071da177e4SLinus Torvalds continue; 12081da177e4SLinus Torvalds *idx = cpu + 1; 12091da177e4SLinus Torvalds return &per_cpu(avc_cache_stats, cpu); 12101da177e4SLinus Torvalds } 12111da177e4SLinus Torvalds return NULL; 12121da177e4SLinus Torvalds } 12131da177e4SLinus Torvalds 12141da177e4SLinus Torvalds static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos) 12151da177e4SLinus Torvalds { 12161da177e4SLinus Torvalds loff_t n = *pos - 1; 12171da177e4SLinus Torvalds 12181da177e4SLinus Torvalds if (*pos == 0) 12191da177e4SLinus Torvalds return SEQ_START_TOKEN; 12201da177e4SLinus Torvalds 12211da177e4SLinus Torvalds return sel_avc_get_stat_idx(&n); 12221da177e4SLinus Torvalds } 12231da177e4SLinus Torvalds 12241da177e4SLinus Torvalds static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos) 12251da177e4SLinus Torvalds { 12261da177e4SLinus Torvalds return sel_avc_get_stat_idx(pos); 12271da177e4SLinus Torvalds } 12281da177e4SLinus Torvalds 12291da177e4SLinus Torvalds static int sel_avc_stats_seq_show(struct seq_file *seq, void *v) 12301da177e4SLinus Torvalds { 12311da177e4SLinus Torvalds struct avc_cache_stats *st = v; 12321da177e4SLinus Torvalds 12331da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 12341da177e4SLinus Torvalds seq_printf(seq, "lookups hits misses allocations reclaims " 12351da177e4SLinus Torvalds "frees\n"); 12361da177e4SLinus Torvalds else 12371da177e4SLinus Torvalds seq_printf(seq, "%u %u %u %u %u %u\n", st->lookups, 12381da177e4SLinus Torvalds st->hits, st->misses, st->allocations, 12391da177e4SLinus Torvalds st->reclaims, st->frees); 12401da177e4SLinus Torvalds return 0; 12411da177e4SLinus Torvalds } 12421da177e4SLinus Torvalds 12431da177e4SLinus Torvalds static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v) 12441da177e4SLinus Torvalds { } 12451da177e4SLinus Torvalds 12461996a109SJan Engelhardt static const struct seq_operations sel_avc_cache_stats_seq_ops = { 12471da177e4SLinus Torvalds .start = sel_avc_stats_seq_start, 12481da177e4SLinus Torvalds .next = sel_avc_stats_seq_next, 12491da177e4SLinus Torvalds .show = sel_avc_stats_seq_show, 12501da177e4SLinus Torvalds .stop = sel_avc_stats_seq_stop, 12511da177e4SLinus Torvalds }; 12521da177e4SLinus Torvalds 12531da177e4SLinus Torvalds static int sel_open_avc_cache_stats(struct inode *inode, struct file *file) 12541da177e4SLinus Torvalds { 12551da177e4SLinus Torvalds return seq_open(file, &sel_avc_cache_stats_seq_ops); 12561da177e4SLinus Torvalds } 12571da177e4SLinus Torvalds 12589c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_stats_ops = { 12591da177e4SLinus Torvalds .open = sel_open_avc_cache_stats, 12601da177e4SLinus Torvalds .read = seq_read, 12611da177e4SLinus Torvalds .llseek = seq_lseek, 12621da177e4SLinus Torvalds .release = seq_release, 12631da177e4SLinus Torvalds }; 12641da177e4SLinus Torvalds #endif 12651da177e4SLinus Torvalds 12661da177e4SLinus Torvalds static int sel_make_avc_files(struct dentry *dir) 12671da177e4SLinus Torvalds { 12681da177e4SLinus Torvalds int i, ret = 0; 12691da177e4SLinus Torvalds static struct tree_descr files[] = { 12701da177e4SLinus Torvalds { "cache_threshold", 12711da177e4SLinus Torvalds &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR }, 12721da177e4SLinus Torvalds { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO }, 12731da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS 12741da177e4SLinus Torvalds { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO }, 12751da177e4SLinus Torvalds #endif 12761da177e4SLinus Torvalds }; 12771da177e4SLinus Torvalds 12786e20a64aSNicolas Kaiser for (i = 0; i < ARRAY_SIZE(files); i++) { 12791da177e4SLinus Torvalds struct inode *inode; 12801da177e4SLinus Torvalds struct dentry *dentry; 12811da177e4SLinus Torvalds 12821da177e4SLinus Torvalds dentry = d_alloc_name(dir, files[i].name); 12831da177e4SLinus Torvalds if (!dentry) { 12841da177e4SLinus Torvalds ret = -ENOMEM; 1285d6aafa65SJames Morris goto out; 12861da177e4SLinus Torvalds } 12871da177e4SLinus Torvalds 12881da177e4SLinus Torvalds inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode); 12891da177e4SLinus Torvalds if (!inode) { 12901da177e4SLinus Torvalds ret = -ENOMEM; 1291d6aafa65SJames Morris goto out; 12921da177e4SLinus Torvalds } 12931da177e4SLinus Torvalds inode->i_fop = files[i].ops; 12946174eafcSJames Carter inode->i_ino = ++sel_last_ino; 12951da177e4SLinus Torvalds d_add(dentry, inode); 12961da177e4SLinus Torvalds } 12971da177e4SLinus Torvalds out: 12981da177e4SLinus Torvalds return ret; 12991da177e4SLinus Torvalds } 13001da177e4SLinus Torvalds 1301f0ee2e46SJames Carter static ssize_t sel_read_initcon(struct file * file, char __user *buf, 1302f0ee2e46SJames Carter size_t count, loff_t *ppos) 1303f0ee2e46SJames Carter { 1304f0ee2e46SJames Carter struct inode *inode; 1305f0ee2e46SJames Carter char *con; 1306f0ee2e46SJames Carter u32 sid, len; 1307f0ee2e46SJames Carter ssize_t ret; 1308f0ee2e46SJames Carter 1309f0ee2e46SJames Carter inode = file->f_path.dentry->d_inode; 1310f0ee2e46SJames Carter sid = inode->i_ino&SEL_INO_MASK; 1311f0ee2e46SJames Carter ret = security_sid_to_context(sid, &con, &len); 1312f0ee2e46SJames Carter if (ret < 0) 1313f0ee2e46SJames Carter return ret; 1314f0ee2e46SJames Carter 1315f0ee2e46SJames Carter ret = simple_read_from_buffer(buf, count, ppos, con, len); 1316f0ee2e46SJames Carter kfree(con); 1317f0ee2e46SJames Carter return ret; 1318f0ee2e46SJames Carter } 1319f0ee2e46SJames Carter 1320f0ee2e46SJames Carter static const struct file_operations sel_initcon_ops = { 1321f0ee2e46SJames Carter .read = sel_read_initcon, 1322f0ee2e46SJames Carter }; 1323f0ee2e46SJames Carter 1324f0ee2e46SJames Carter static int sel_make_initcon_files(struct dentry *dir) 1325f0ee2e46SJames Carter { 1326f0ee2e46SJames Carter int i, ret = 0; 1327f0ee2e46SJames Carter 1328f0ee2e46SJames Carter for (i = 1; i <= SECINITSID_NUM; i++) { 1329f0ee2e46SJames Carter struct inode *inode; 1330f0ee2e46SJames Carter struct dentry *dentry; 1331f0ee2e46SJames Carter dentry = d_alloc_name(dir, security_get_initial_sid_context(i)); 1332f0ee2e46SJames Carter if (!dentry) { 1333f0ee2e46SJames Carter ret = -ENOMEM; 1334f0ee2e46SJames Carter goto out; 1335f0ee2e46SJames Carter } 1336f0ee2e46SJames Carter 1337f0ee2e46SJames Carter inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); 1338f0ee2e46SJames Carter if (!inode) { 1339f0ee2e46SJames Carter ret = -ENOMEM; 1340f0ee2e46SJames Carter goto out; 1341f0ee2e46SJames Carter } 1342f0ee2e46SJames Carter inode->i_fop = &sel_initcon_ops; 1343f0ee2e46SJames Carter inode->i_ino = i|SEL_INITCON_INO_OFFSET; 1344f0ee2e46SJames Carter d_add(dentry, inode); 1345f0ee2e46SJames Carter } 1346f0ee2e46SJames Carter out: 1347f0ee2e46SJames Carter return ret; 1348f0ee2e46SJames Carter } 1349f0ee2e46SJames Carter 1350e47c8fc5SChristopher J. PeBenito static inline unsigned int sel_div(unsigned long a, unsigned long b) 1351e47c8fc5SChristopher J. PeBenito { 1352e47c8fc5SChristopher J. PeBenito return a / b - (a % b < 0); 1353e47c8fc5SChristopher J. PeBenito } 1354e47c8fc5SChristopher J. PeBenito 1355e47c8fc5SChristopher J. PeBenito static inline unsigned long sel_class_to_ino(u16 class) 1356e47c8fc5SChristopher J. PeBenito { 1357e47c8fc5SChristopher J. PeBenito return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET; 1358e47c8fc5SChristopher J. PeBenito } 1359e47c8fc5SChristopher J. PeBenito 1360e47c8fc5SChristopher J. PeBenito static inline u16 sel_ino_to_class(unsigned long ino) 1361e47c8fc5SChristopher J. PeBenito { 1362e47c8fc5SChristopher J. PeBenito return sel_div(ino & SEL_INO_MASK, SEL_VEC_MAX + 1); 1363e47c8fc5SChristopher J. PeBenito } 1364e47c8fc5SChristopher J. PeBenito 1365e47c8fc5SChristopher J. PeBenito static inline unsigned long sel_perm_to_ino(u16 class, u32 perm) 1366e47c8fc5SChristopher J. PeBenito { 1367e47c8fc5SChristopher J. PeBenito return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET; 1368e47c8fc5SChristopher J. PeBenito } 1369e47c8fc5SChristopher J. PeBenito 1370e47c8fc5SChristopher J. PeBenito static inline u32 sel_ino_to_perm(unsigned long ino) 1371e47c8fc5SChristopher J. PeBenito { 1372e47c8fc5SChristopher J. PeBenito return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1); 1373e47c8fc5SChristopher J. PeBenito } 1374e47c8fc5SChristopher J. PeBenito 1375e47c8fc5SChristopher J. PeBenito static ssize_t sel_read_class(struct file * file, char __user *buf, 1376e47c8fc5SChristopher J. PeBenito size_t count, loff_t *ppos) 1377e47c8fc5SChristopher J. PeBenito { 1378e47c8fc5SChristopher J. PeBenito ssize_t rc, len; 1379e47c8fc5SChristopher J. PeBenito char *page; 1380e47c8fc5SChristopher J. PeBenito unsigned long ino = file->f_path.dentry->d_inode->i_ino; 1381e47c8fc5SChristopher J. PeBenito 1382e47c8fc5SChristopher J. PeBenito page = (char *)__get_free_page(GFP_KERNEL); 1383e47c8fc5SChristopher J. PeBenito if (!page) { 1384e47c8fc5SChristopher J. PeBenito rc = -ENOMEM; 1385e47c8fc5SChristopher J. PeBenito goto out; 1386e47c8fc5SChristopher J. PeBenito } 1387e47c8fc5SChristopher J. PeBenito 1388e47c8fc5SChristopher J. PeBenito len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_class(ino)); 1389e47c8fc5SChristopher J. PeBenito rc = simple_read_from_buffer(buf, count, ppos, page, len); 1390e47c8fc5SChristopher J. PeBenito free_page((unsigned long)page); 1391e47c8fc5SChristopher J. PeBenito out: 1392e47c8fc5SChristopher J. PeBenito return rc; 1393e47c8fc5SChristopher J. PeBenito } 1394e47c8fc5SChristopher J. PeBenito 1395e47c8fc5SChristopher J. PeBenito static const struct file_operations sel_class_ops = { 1396e47c8fc5SChristopher J. PeBenito .read = sel_read_class, 1397e47c8fc5SChristopher J. PeBenito }; 1398e47c8fc5SChristopher J. PeBenito 1399e47c8fc5SChristopher J. PeBenito static ssize_t sel_read_perm(struct file * file, char __user *buf, 1400e47c8fc5SChristopher J. PeBenito size_t count, loff_t *ppos) 1401e47c8fc5SChristopher J. PeBenito { 1402e47c8fc5SChristopher J. PeBenito ssize_t rc, len; 1403e47c8fc5SChristopher J. PeBenito char *page; 1404e47c8fc5SChristopher J. PeBenito unsigned long ino = file->f_path.dentry->d_inode->i_ino; 1405e47c8fc5SChristopher J. PeBenito 1406e47c8fc5SChristopher J. PeBenito page = (char *)__get_free_page(GFP_KERNEL); 1407e47c8fc5SChristopher J. PeBenito if (!page) { 1408e47c8fc5SChristopher J. PeBenito rc = -ENOMEM; 1409e47c8fc5SChristopher J. PeBenito goto out; 1410e47c8fc5SChristopher J. PeBenito } 1411e47c8fc5SChristopher J. PeBenito 1412e47c8fc5SChristopher J. PeBenito len = snprintf(page, PAGE_SIZE,"%d", sel_ino_to_perm(ino)); 1413e47c8fc5SChristopher J. PeBenito rc = simple_read_from_buffer(buf, count, ppos, page, len); 1414e47c8fc5SChristopher J. PeBenito free_page((unsigned long)page); 1415e47c8fc5SChristopher J. PeBenito out: 1416e47c8fc5SChristopher J. PeBenito return rc; 1417e47c8fc5SChristopher J. PeBenito } 1418e47c8fc5SChristopher J. PeBenito 1419e47c8fc5SChristopher J. PeBenito static const struct file_operations sel_perm_ops = { 1420e47c8fc5SChristopher J. PeBenito .read = sel_read_perm, 1421e47c8fc5SChristopher J. PeBenito }; 1422e47c8fc5SChristopher J. PeBenito 14233bb56b25SPaul Moore static ssize_t sel_read_policycap(struct file *file, char __user *buf, 14243bb56b25SPaul Moore size_t count, loff_t *ppos) 14253bb56b25SPaul Moore { 14263bb56b25SPaul Moore int value; 14273bb56b25SPaul Moore char tmpbuf[TMPBUFLEN]; 14283bb56b25SPaul Moore ssize_t length; 14293bb56b25SPaul Moore unsigned long i_ino = file->f_path.dentry->d_inode->i_ino; 14303bb56b25SPaul Moore 14313bb56b25SPaul Moore value = security_policycap_supported(i_ino & SEL_INO_MASK); 14323bb56b25SPaul Moore length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value); 14333bb56b25SPaul Moore 14343bb56b25SPaul Moore return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 14353bb56b25SPaul Moore } 14363bb56b25SPaul Moore 14373bb56b25SPaul Moore static const struct file_operations sel_policycap_ops = { 14383bb56b25SPaul Moore .read = sel_read_policycap, 14393bb56b25SPaul Moore }; 14403bb56b25SPaul Moore 1441e47c8fc5SChristopher J. PeBenito static int sel_make_perm_files(char *objclass, int classvalue, 1442e47c8fc5SChristopher J. PeBenito struct dentry *dir) 1443e47c8fc5SChristopher J. PeBenito { 1444e47c8fc5SChristopher J. PeBenito int i, rc = 0, nperms; 1445e47c8fc5SChristopher J. PeBenito char **perms; 1446e47c8fc5SChristopher J. PeBenito 1447e47c8fc5SChristopher J. PeBenito rc = security_get_permissions(objclass, &perms, &nperms); 1448e47c8fc5SChristopher J. PeBenito if (rc) 1449e47c8fc5SChristopher J. PeBenito goto out; 1450e47c8fc5SChristopher J. PeBenito 1451e47c8fc5SChristopher J. PeBenito for (i = 0; i < nperms; i++) { 1452e47c8fc5SChristopher J. PeBenito struct inode *inode; 1453e47c8fc5SChristopher J. PeBenito struct dentry *dentry; 1454e47c8fc5SChristopher J. PeBenito 1455e47c8fc5SChristopher J. PeBenito dentry = d_alloc_name(dir, perms[i]); 1456e47c8fc5SChristopher J. PeBenito if (!dentry) { 1457e47c8fc5SChristopher J. PeBenito rc = -ENOMEM; 1458e47c8fc5SChristopher J. PeBenito goto out1; 1459e47c8fc5SChristopher J. PeBenito } 1460e47c8fc5SChristopher J. PeBenito 1461e47c8fc5SChristopher J. PeBenito inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); 1462e47c8fc5SChristopher J. PeBenito if (!inode) { 1463e47c8fc5SChristopher J. PeBenito rc = -ENOMEM; 1464e47c8fc5SChristopher J. PeBenito goto out1; 1465e47c8fc5SChristopher J. PeBenito } 1466e47c8fc5SChristopher J. PeBenito inode->i_fop = &sel_perm_ops; 1467e47c8fc5SChristopher J. PeBenito /* i+1 since perm values are 1-indexed */ 1468e47c8fc5SChristopher J. PeBenito inode->i_ino = sel_perm_to_ino(classvalue, i+1); 1469e47c8fc5SChristopher J. PeBenito d_add(dentry, inode); 1470e47c8fc5SChristopher J. PeBenito } 1471e47c8fc5SChristopher J. PeBenito 1472e47c8fc5SChristopher J. PeBenito out1: 1473e47c8fc5SChristopher J. PeBenito for (i = 0; i < nperms; i++) 1474e47c8fc5SChristopher J. PeBenito kfree(perms[i]); 1475e47c8fc5SChristopher J. PeBenito kfree(perms); 1476e47c8fc5SChristopher J. PeBenito out: 1477e47c8fc5SChristopher J. PeBenito return rc; 1478e47c8fc5SChristopher J. PeBenito } 1479e47c8fc5SChristopher J. PeBenito 1480e47c8fc5SChristopher J. PeBenito static int sel_make_class_dir_entries(char *classname, int index, 1481e47c8fc5SChristopher J. PeBenito struct dentry *dir) 1482e47c8fc5SChristopher J. PeBenito { 1483e47c8fc5SChristopher J. PeBenito struct dentry *dentry = NULL; 1484e47c8fc5SChristopher J. PeBenito struct inode *inode = NULL; 1485e47c8fc5SChristopher J. PeBenito int rc; 1486e47c8fc5SChristopher J. PeBenito 1487e47c8fc5SChristopher J. PeBenito dentry = d_alloc_name(dir, "index"); 1488e47c8fc5SChristopher J. PeBenito if (!dentry) { 1489e47c8fc5SChristopher J. PeBenito rc = -ENOMEM; 1490e47c8fc5SChristopher J. PeBenito goto out; 1491e47c8fc5SChristopher J. PeBenito } 1492e47c8fc5SChristopher J. PeBenito 1493e47c8fc5SChristopher J. PeBenito inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); 1494e47c8fc5SChristopher J. PeBenito if (!inode) { 1495e47c8fc5SChristopher J. PeBenito rc = -ENOMEM; 1496e47c8fc5SChristopher J. PeBenito goto out; 1497e47c8fc5SChristopher J. PeBenito } 1498e47c8fc5SChristopher J. PeBenito 1499e47c8fc5SChristopher J. PeBenito inode->i_fop = &sel_class_ops; 1500e47c8fc5SChristopher J. PeBenito inode->i_ino = sel_class_to_ino(index); 1501e47c8fc5SChristopher J. PeBenito d_add(dentry, inode); 1502e47c8fc5SChristopher J. PeBenito 1503e47c8fc5SChristopher J. PeBenito dentry = d_alloc_name(dir, "perms"); 1504e47c8fc5SChristopher J. PeBenito if (!dentry) { 1505e47c8fc5SChristopher J. PeBenito rc = -ENOMEM; 1506e47c8fc5SChristopher J. PeBenito goto out; 1507e47c8fc5SChristopher J. PeBenito } 1508e47c8fc5SChristopher J. PeBenito 1509e47c8fc5SChristopher J. PeBenito rc = sel_make_dir(dir->d_inode, dentry, &last_class_ino); 1510e47c8fc5SChristopher J. PeBenito if (rc) 1511e47c8fc5SChristopher J. PeBenito goto out; 1512e47c8fc5SChristopher J. PeBenito 1513e47c8fc5SChristopher J. PeBenito rc = sel_make_perm_files(classname, index, dentry); 1514e47c8fc5SChristopher J. PeBenito 1515e47c8fc5SChristopher J. PeBenito out: 1516e47c8fc5SChristopher J. PeBenito return rc; 1517e47c8fc5SChristopher J. PeBenito } 1518e47c8fc5SChristopher J. PeBenito 1519e47c8fc5SChristopher J. PeBenito static void sel_remove_classes(void) 1520e47c8fc5SChristopher J. PeBenito { 1521e47c8fc5SChristopher J. PeBenito struct list_head *class_node; 1522e47c8fc5SChristopher J. PeBenito 1523e47c8fc5SChristopher J. PeBenito list_for_each(class_node, &class_dir->d_subdirs) { 1524e47c8fc5SChristopher J. PeBenito struct dentry *class_subdir = list_entry(class_node, 1525e47c8fc5SChristopher J. PeBenito struct dentry, d_u.d_child); 1526e47c8fc5SChristopher J. PeBenito struct list_head *class_subdir_node; 1527e47c8fc5SChristopher J. PeBenito 1528e47c8fc5SChristopher J. PeBenito list_for_each(class_subdir_node, &class_subdir->d_subdirs) { 1529e47c8fc5SChristopher J. PeBenito struct dentry *d = list_entry(class_subdir_node, 1530e47c8fc5SChristopher J. PeBenito struct dentry, d_u.d_child); 1531e47c8fc5SChristopher J. PeBenito 1532e47c8fc5SChristopher J. PeBenito if (d->d_inode) 1533e47c8fc5SChristopher J. PeBenito if (d->d_inode->i_mode & S_IFDIR) 1534e47c8fc5SChristopher J. PeBenito sel_remove_entries(d); 1535e47c8fc5SChristopher J. PeBenito } 1536e47c8fc5SChristopher J. PeBenito 1537e47c8fc5SChristopher J. PeBenito sel_remove_entries(class_subdir); 1538e47c8fc5SChristopher J. PeBenito } 1539e47c8fc5SChristopher J. PeBenito 1540e47c8fc5SChristopher J. PeBenito sel_remove_entries(class_dir); 1541e47c8fc5SChristopher J. PeBenito } 1542e47c8fc5SChristopher J. PeBenito 1543e47c8fc5SChristopher J. PeBenito static int sel_make_classes(void) 1544e47c8fc5SChristopher J. PeBenito { 1545e47c8fc5SChristopher J. PeBenito int rc = 0, nclasses, i; 1546e47c8fc5SChristopher J. PeBenito char **classes; 1547e47c8fc5SChristopher J. PeBenito 1548e47c8fc5SChristopher J. PeBenito /* delete any existing entries */ 1549e47c8fc5SChristopher J. PeBenito sel_remove_classes(); 1550e47c8fc5SChristopher J. PeBenito 1551e47c8fc5SChristopher J. PeBenito rc = security_get_classes(&classes, &nclasses); 1552e47c8fc5SChristopher J. PeBenito if (rc < 0) 1553e47c8fc5SChristopher J. PeBenito goto out; 1554e47c8fc5SChristopher J. PeBenito 1555e47c8fc5SChristopher J. PeBenito /* +2 since classes are 1-indexed */ 1556e47c8fc5SChristopher J. PeBenito last_class_ino = sel_class_to_ino(nclasses+2); 1557e47c8fc5SChristopher J. PeBenito 1558e47c8fc5SChristopher J. PeBenito for (i = 0; i < nclasses; i++) { 1559e47c8fc5SChristopher J. PeBenito struct dentry *class_name_dir; 1560e47c8fc5SChristopher J. PeBenito 1561e47c8fc5SChristopher J. PeBenito class_name_dir = d_alloc_name(class_dir, classes[i]); 1562e47c8fc5SChristopher J. PeBenito if (!class_name_dir) { 1563e47c8fc5SChristopher J. PeBenito rc = -ENOMEM; 1564e47c8fc5SChristopher J. PeBenito goto out1; 1565e47c8fc5SChristopher J. PeBenito } 1566e47c8fc5SChristopher J. PeBenito 1567e47c8fc5SChristopher J. PeBenito rc = sel_make_dir(class_dir->d_inode, class_name_dir, 1568e47c8fc5SChristopher J. PeBenito &last_class_ino); 1569e47c8fc5SChristopher J. PeBenito if (rc) 1570e47c8fc5SChristopher J. PeBenito goto out1; 1571e47c8fc5SChristopher J. PeBenito 1572e47c8fc5SChristopher J. PeBenito /* i+1 since class values are 1-indexed */ 1573e47c8fc5SChristopher J. PeBenito rc = sel_make_class_dir_entries(classes[i], i+1, 1574e47c8fc5SChristopher J. PeBenito class_name_dir); 1575e47c8fc5SChristopher J. PeBenito if (rc) 1576e47c8fc5SChristopher J. PeBenito goto out1; 1577e47c8fc5SChristopher J. PeBenito } 1578e47c8fc5SChristopher J. PeBenito 1579e47c8fc5SChristopher J. PeBenito out1: 1580e47c8fc5SChristopher J. PeBenito for (i = 0; i < nclasses; i++) 1581e47c8fc5SChristopher J. PeBenito kfree(classes[i]); 1582e47c8fc5SChristopher J. PeBenito kfree(classes); 1583e47c8fc5SChristopher J. PeBenito out: 1584e47c8fc5SChristopher J. PeBenito return rc; 1585e47c8fc5SChristopher J. PeBenito } 1586e47c8fc5SChristopher J. PeBenito 15873bb56b25SPaul Moore static int sel_make_policycap(void) 15883bb56b25SPaul Moore { 15893bb56b25SPaul Moore unsigned int iter; 15903bb56b25SPaul Moore struct dentry *dentry = NULL; 15913bb56b25SPaul Moore struct inode *inode = NULL; 15923bb56b25SPaul Moore 15933bb56b25SPaul Moore sel_remove_entries(policycap_dir); 15943bb56b25SPaul Moore 15953bb56b25SPaul Moore for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) { 15963bb56b25SPaul Moore if (iter < ARRAY_SIZE(policycap_names)) 15973bb56b25SPaul Moore dentry = d_alloc_name(policycap_dir, 15983bb56b25SPaul Moore policycap_names[iter]); 15993bb56b25SPaul Moore else 16003bb56b25SPaul Moore dentry = d_alloc_name(policycap_dir, "unknown"); 16013bb56b25SPaul Moore 16023bb56b25SPaul Moore if (dentry == NULL) 16033bb56b25SPaul Moore return -ENOMEM; 16043bb56b25SPaul Moore 16053bb56b25SPaul Moore inode = sel_make_inode(policycap_dir->d_sb, S_IFREG | S_IRUGO); 16063bb56b25SPaul Moore if (inode == NULL) 16073bb56b25SPaul Moore return -ENOMEM; 16083bb56b25SPaul Moore 16093bb56b25SPaul Moore inode->i_fop = &sel_policycap_ops; 16103bb56b25SPaul Moore inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET; 16113bb56b25SPaul Moore d_add(dentry, inode); 16123bb56b25SPaul Moore } 16133bb56b25SPaul Moore 16143bb56b25SPaul Moore return 0; 16153bb56b25SPaul Moore } 16163bb56b25SPaul Moore 16170dd4ae51SChristopher J. PeBenito static int sel_make_dir(struct inode *dir, struct dentry *dentry, 16180dd4ae51SChristopher J. PeBenito unsigned long *ino) 16191da177e4SLinus Torvalds { 16201da177e4SLinus Torvalds int ret = 0; 16211da177e4SLinus Torvalds struct inode *inode; 16221da177e4SLinus Torvalds 1623edb20fb5SJames Morris inode = sel_make_inode(dir->i_sb, S_IFDIR | S_IRUGO | S_IXUGO); 16241da177e4SLinus Torvalds if (!inode) { 16251da177e4SLinus Torvalds ret = -ENOMEM; 16261da177e4SLinus Torvalds goto out; 16271da177e4SLinus Torvalds } 16281da177e4SLinus Torvalds inode->i_op = &simple_dir_inode_operations; 16291da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations; 16300dd4ae51SChristopher J. PeBenito inode->i_ino = ++(*ino); 163140e906f8SJames Morris /* directory inodes start off with i_nlink == 2 (for "." entry) */ 1632d8c76e6fSDave Hansen inc_nlink(inode); 16331da177e4SLinus Torvalds d_add(dentry, inode); 1634edb20fb5SJames Morris /* bump link count on parent directory, too */ 1635d8c76e6fSDave Hansen inc_nlink(dir); 16361da177e4SLinus Torvalds out: 16371da177e4SLinus Torvalds return ret; 16381da177e4SLinus Torvalds } 16391da177e4SLinus Torvalds 16401da177e4SLinus Torvalds static int sel_fill_super(struct super_block * sb, void * data, int silent) 16411da177e4SLinus Torvalds { 16421da177e4SLinus Torvalds int ret; 16431da177e4SLinus Torvalds struct dentry *dentry; 1644edb20fb5SJames Morris struct inode *inode, *root_inode; 16451da177e4SLinus Torvalds struct inode_security_struct *isec; 16461da177e4SLinus Torvalds 16471da177e4SLinus Torvalds static struct tree_descr selinux_files[] = { 16481da177e4SLinus Torvalds [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR}, 16491da177e4SLinus Torvalds [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR}, 1650ce9982d0SStephen Smalley [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO}, 16511da177e4SLinus Torvalds [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO}, 16521da177e4SLinus Torvalds [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO}, 16531da177e4SLinus Torvalds [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO}, 16541da177e4SLinus Torvalds [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO}, 16551da177e4SLinus Torvalds [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO}, 16561da177e4SLinus Torvalds [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR}, 16571da177e4SLinus Torvalds [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO}, 16581da177e4SLinus Torvalds [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR}, 16591da177e4SLinus Torvalds [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO}, 16601da177e4SLinus Torvalds [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR}, 16614e5ab4cbSJames Morris [SEL_COMPAT_NET] = {"compat_net", &sel_compat_net_ops, S_IRUGO|S_IWUSR}, 16623f12070eSEric Paris [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO}, 16633f12070eSEric Paris [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO}, 16641da177e4SLinus Torvalds /* last one */ {""} 16651da177e4SLinus Torvalds }; 16661da177e4SLinus Torvalds ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files); 16671da177e4SLinus Torvalds if (ret) 1668161ce45aSJames Morris goto err; 16691da177e4SLinus Torvalds 1670edb20fb5SJames Morris root_inode = sb->s_root->d_inode; 1671edb20fb5SJames Morris 16721da177e4SLinus Torvalds dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME); 1673161ce45aSJames Morris if (!dentry) { 1674161ce45aSJames Morris ret = -ENOMEM; 1675161ce45aSJames Morris goto err; 1676161ce45aSJames Morris } 16771da177e4SLinus Torvalds 16780dd4ae51SChristopher J. PeBenito ret = sel_make_dir(root_inode, dentry, &sel_last_ino); 1679cde174a8SJames Morris if (ret) 1680161ce45aSJames Morris goto err; 1681cde174a8SJames Morris 16821da177e4SLinus Torvalds bool_dir = dentry; 16831da177e4SLinus Torvalds 16841da177e4SLinus Torvalds dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME); 1685161ce45aSJames Morris if (!dentry) { 1686161ce45aSJames Morris ret = -ENOMEM; 1687161ce45aSJames Morris goto err; 1688161ce45aSJames Morris } 16891da177e4SLinus Torvalds 16901da177e4SLinus Torvalds inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO); 1691161ce45aSJames Morris if (!inode) { 1692161ce45aSJames Morris ret = -ENOMEM; 1693161ce45aSJames Morris goto err; 1694161ce45aSJames Morris } 16956174eafcSJames Carter inode->i_ino = ++sel_last_ino; 16961da177e4SLinus Torvalds isec = (struct inode_security_struct*)inode->i_security; 16971da177e4SLinus Torvalds isec->sid = SECINITSID_DEVNULL; 16981da177e4SLinus Torvalds isec->sclass = SECCLASS_CHR_FILE; 16991da177e4SLinus Torvalds isec->initialized = 1; 17001da177e4SLinus Torvalds 17011da177e4SLinus Torvalds init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3)); 17021da177e4SLinus Torvalds d_add(dentry, inode); 17031da177e4SLinus Torvalds selinux_null = dentry; 17041da177e4SLinus Torvalds 17051da177e4SLinus Torvalds dentry = d_alloc_name(sb->s_root, "avc"); 1706161ce45aSJames Morris if (!dentry) { 1707161ce45aSJames Morris ret = -ENOMEM; 1708161ce45aSJames Morris goto err; 1709161ce45aSJames Morris } 17101da177e4SLinus Torvalds 17110dd4ae51SChristopher J. PeBenito ret = sel_make_dir(root_inode, dentry, &sel_last_ino); 17121da177e4SLinus Torvalds if (ret) 1713161ce45aSJames Morris goto err; 17141da177e4SLinus Torvalds 17151da177e4SLinus Torvalds ret = sel_make_avc_files(dentry); 17161da177e4SLinus Torvalds if (ret) 1717161ce45aSJames Morris goto err; 1718f0ee2e46SJames Carter 1719f0ee2e46SJames Carter dentry = d_alloc_name(sb->s_root, "initial_contexts"); 1720f0ee2e46SJames Carter if (!dentry) { 1721f0ee2e46SJames Carter ret = -ENOMEM; 1722f0ee2e46SJames Carter goto err; 1723f0ee2e46SJames Carter } 1724f0ee2e46SJames Carter 17250dd4ae51SChristopher J. PeBenito ret = sel_make_dir(root_inode, dentry, &sel_last_ino); 1726f0ee2e46SJames Carter if (ret) 1727f0ee2e46SJames Carter goto err; 1728f0ee2e46SJames Carter 1729f0ee2e46SJames Carter ret = sel_make_initcon_files(dentry); 1730f0ee2e46SJames Carter if (ret) 1731f0ee2e46SJames Carter goto err; 1732f0ee2e46SJames Carter 1733e47c8fc5SChristopher J. PeBenito dentry = d_alloc_name(sb->s_root, "class"); 1734e47c8fc5SChristopher J. PeBenito if (!dentry) { 1735e47c8fc5SChristopher J. PeBenito ret = -ENOMEM; 1736e47c8fc5SChristopher J. PeBenito goto err; 1737e47c8fc5SChristopher J. PeBenito } 1738e47c8fc5SChristopher J. PeBenito 1739e47c8fc5SChristopher J. PeBenito ret = sel_make_dir(root_inode, dentry, &sel_last_ino); 1740e47c8fc5SChristopher J. PeBenito if (ret) 1741e47c8fc5SChristopher J. PeBenito goto err; 1742e47c8fc5SChristopher J. PeBenito 1743e47c8fc5SChristopher J. PeBenito class_dir = dentry; 1744e47c8fc5SChristopher J. PeBenito 17453bb56b25SPaul Moore dentry = d_alloc_name(sb->s_root, "policy_capabilities"); 17463bb56b25SPaul Moore if (!dentry) { 17473bb56b25SPaul Moore ret = -ENOMEM; 17483bb56b25SPaul Moore goto err; 17493bb56b25SPaul Moore } 17503bb56b25SPaul Moore 17513bb56b25SPaul Moore ret = sel_make_dir(root_inode, dentry, &sel_last_ino); 17523bb56b25SPaul Moore if (ret) 17533bb56b25SPaul Moore goto err; 17543bb56b25SPaul Moore 17553bb56b25SPaul Moore policycap_dir = dentry; 17563bb56b25SPaul Moore 17571da177e4SLinus Torvalds out: 1758161ce45aSJames Morris return ret; 1759161ce45aSJames Morris err: 17601da177e4SLinus Torvalds printk(KERN_ERR "%s: failed while creating inodes\n", __FUNCTION__); 1761161ce45aSJames Morris goto out; 17621da177e4SLinus Torvalds } 17631da177e4SLinus Torvalds 1764454e2398SDavid Howells static int sel_get_sb(struct file_system_type *fs_type, 1765454e2398SDavid Howells int flags, const char *dev_name, void *data, 1766454e2398SDavid Howells struct vfsmount *mnt) 17671da177e4SLinus Torvalds { 1768454e2398SDavid Howells return get_sb_single(fs_type, flags, data, sel_fill_super, mnt); 17691da177e4SLinus Torvalds } 17701da177e4SLinus Torvalds 17711da177e4SLinus Torvalds static struct file_system_type sel_fs_type = { 17721da177e4SLinus Torvalds .name = "selinuxfs", 17731da177e4SLinus Torvalds .get_sb = sel_get_sb, 17741da177e4SLinus Torvalds .kill_sb = kill_litter_super, 17751da177e4SLinus Torvalds }; 17761da177e4SLinus Torvalds 17771da177e4SLinus Torvalds struct vfsmount *selinuxfs_mount; 17781da177e4SLinus Torvalds 17791da177e4SLinus Torvalds static int __init init_sel_fs(void) 17801da177e4SLinus Torvalds { 17811da177e4SLinus Torvalds int err; 17821da177e4SLinus Torvalds 17831da177e4SLinus Torvalds if (!selinux_enabled) 17841da177e4SLinus Torvalds return 0; 17851da177e4SLinus Torvalds err = register_filesystem(&sel_fs_type); 17861da177e4SLinus Torvalds if (!err) { 17871da177e4SLinus Torvalds selinuxfs_mount = kern_mount(&sel_fs_type); 17881da177e4SLinus Torvalds if (IS_ERR(selinuxfs_mount)) { 17891da177e4SLinus Torvalds printk(KERN_ERR "selinuxfs: could not mount!\n"); 17901da177e4SLinus Torvalds err = PTR_ERR(selinuxfs_mount); 17911da177e4SLinus Torvalds selinuxfs_mount = NULL; 17921da177e4SLinus Torvalds } 17931da177e4SLinus Torvalds } 17941da177e4SLinus Torvalds return err; 17951da177e4SLinus Torvalds } 17961da177e4SLinus Torvalds 17971da177e4SLinus Torvalds __initcall(init_sel_fs); 17981da177e4SLinus Torvalds 17991da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 18001da177e4SLinus Torvalds void exit_sel_fs(void) 18011da177e4SLinus Torvalds { 18021da177e4SLinus Torvalds unregister_filesystem(&sel_fs_type); 18031da177e4SLinus Torvalds } 18041da177e4SLinus Torvalds #endif 1805