11da177e4SLinus Torvalds /* Updated: Karl MacMillan <kmacmillan@tresys.com> 21da177e4SLinus Torvalds * 31da177e4SLinus Torvalds * Added conditional policy language extensions 41da177e4SLinus Torvalds * 582c21bfaSPaul Moore * Updated: Hewlett-Packard <paul@paul-moore.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> 30f5269710SEric Paris #include <linux/uaccess.h> 317a627e3bSGreg Kroah-Hartman #include <linux/kobject.h> 320f7e4c33SKohei Kaigai #include <linux/ctype.h> 331da177e4SLinus Torvalds 341da177e4SLinus Torvalds /* selinuxfs pseudo filesystem for exporting the security policy API. 351da177e4SLinus Torvalds Based on the proc code and the fs/nfsd/nfsctl.c code. */ 361da177e4SLinus Torvalds 371da177e4SLinus Torvalds #include "flask.h" 381da177e4SLinus Torvalds #include "avc.h" 391da177e4SLinus Torvalds #include "avc_ss.h" 401da177e4SLinus Torvalds #include "security.h" 411da177e4SLinus Torvalds #include "objsec.h" 421da177e4SLinus Torvalds #include "conditional.h" 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds unsigned int selinux_checkreqprot = CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE; 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds static int __init checkreqprot_setup(char *str) 471da177e4SLinus Torvalds { 48f5269710SEric Paris unsigned long checkreqprot; 4929707b20SJingoo Han if (!kstrtoul(str, 0, &checkreqprot)) 50f5269710SEric Paris selinux_checkreqprot = checkreqprot ? 1 : 0; 511da177e4SLinus Torvalds return 1; 521da177e4SLinus Torvalds } 531da177e4SLinus Torvalds __setup("checkreqprot=", checkreqprot_setup); 541da177e4SLinus Torvalds 55bb003079SIngo Molnar static DEFINE_MUTEX(sel_mutex); 561da177e4SLinus Torvalds 571da177e4SLinus Torvalds /* global data for booleans */ 581872981bSEric Paris static struct dentry *bool_dir; 591872981bSEric Paris static int bool_num; 60d313f948SStephen Smalley static char **bool_pending_names; 611872981bSEric Paris static int *bool_pending_values; 621da177e4SLinus Torvalds 63e47c8fc5SChristopher J. PeBenito /* global data for classes */ 641872981bSEric Paris static struct dentry *class_dir; 65e47c8fc5SChristopher J. PeBenito static unsigned long last_class_ino; 66e47c8fc5SChristopher J. PeBenito 67cee74f47SEric Paris static char policy_opened; 68cee74f47SEric Paris 693bb56b25SPaul Moore /* global data for policy capabilities */ 701872981bSEric Paris static struct dentry *policycap_dir; 713bb56b25SPaul Moore 721da177e4SLinus Torvalds enum sel_inos { 731da177e4SLinus Torvalds SEL_ROOT_INO = 2, 741da177e4SLinus Torvalds SEL_LOAD, /* load policy */ 751da177e4SLinus Torvalds SEL_ENFORCE, /* get or set enforcing status */ 761da177e4SLinus Torvalds SEL_CONTEXT, /* validate context */ 771da177e4SLinus Torvalds SEL_ACCESS, /* compute access decision */ 781da177e4SLinus Torvalds SEL_CREATE, /* compute create labeling decision */ 791da177e4SLinus Torvalds SEL_RELABEL, /* compute relabeling decision */ 801da177e4SLinus Torvalds SEL_USER, /* compute reachable user contexts */ 811da177e4SLinus Torvalds SEL_POLICYVERS, /* return policy version for this kernel */ 821da177e4SLinus Torvalds SEL_COMMIT_BOOLS, /* commit new boolean values */ 831da177e4SLinus Torvalds SEL_MLS, /* return if MLS policy is enabled */ 841da177e4SLinus Torvalds SEL_DISABLE, /* disable SELinux until next reboot */ 851da177e4SLinus Torvalds SEL_MEMBER, /* compute polyinstantiation membership decision */ 861da177e4SLinus Torvalds SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */ 874e5ab4cbSJames Morris SEL_COMPAT_NET, /* whether to use old compat network packet controls */ 883f12070eSEric Paris SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */ 893f12070eSEric Paris SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */ 9011904167SKaiGai Kohei SEL_STATUS, /* export current status using mmap() */ 91cee74f47SEric Paris SEL_POLICY, /* allow userspace to read the in kernel policy */ 92f9df6458SAndrew Perepechko SEL_VALIDATE_TRANS, /* compute validatetrans decision */ 936174eafcSJames Carter SEL_INO_NEXT, /* The next inode number to use */ 941da177e4SLinus Torvalds }; 951da177e4SLinus Torvalds 966174eafcSJames Carter static unsigned long sel_last_ino = SEL_INO_NEXT - 1; 976174eafcSJames Carter 98f0ee2e46SJames Carter #define SEL_INITCON_INO_OFFSET 0x01000000 99bce34bc0SJames Carter #define SEL_BOOL_INO_OFFSET 0x02000000 100e47c8fc5SChristopher J. PeBenito #define SEL_CLASS_INO_OFFSET 0x04000000 1013bb56b25SPaul Moore #define SEL_POLICYCAP_INO_OFFSET 0x08000000 102f0ee2e46SJames Carter #define SEL_INO_MASK 0x00ffffff 103f0ee2e46SJames Carter 1041da177e4SLinus Torvalds #define TMPBUFLEN 12 1051da177e4SLinus Torvalds static ssize_t sel_read_enforce(struct file *filp, char __user *buf, 1061da177e4SLinus Torvalds size_t count, loff_t *ppos) 1071da177e4SLinus Torvalds { 1081da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 1091da177e4SLinus Torvalds ssize_t length; 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing); 1121da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 1131da177e4SLinus Torvalds } 1141da177e4SLinus Torvalds 1151da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP 1161da177e4SLinus Torvalds static ssize_t sel_write_enforce(struct file *file, const char __user *buf, 1171da177e4SLinus Torvalds size_t count, loff_t *ppos) 1181da177e4SLinus Torvalds 1191da177e4SLinus Torvalds { 120b77a493bSEric Paris char *page = NULL; 1211da177e4SLinus Torvalds ssize_t length; 1221da177e4SLinus Torvalds int new_value; 1231da177e4SLinus Torvalds 124bfd51626SDavi Arnaut if (count >= PAGE_SIZE) 1258365a719SAl Viro return -ENOMEM; 126b77a493bSEric Paris 1271da177e4SLinus Torvalds /* No partial writes. */ 128b77a493bSEric Paris if (*ppos != 0) 1298365a719SAl Viro return -EINVAL; 130b77a493bSEric Paris 1318365a719SAl Viro page = memdup_user_nul(buf, count); 1328365a719SAl Viro if (IS_ERR(page)) 1338365a719SAl Viro return PTR_ERR(page); 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds length = -EINVAL; 1361da177e4SLinus Torvalds if (sscanf(page, "%d", &new_value) != 1) 1371da177e4SLinus Torvalds goto out; 1381da177e4SLinus Torvalds 139ea49d10eSStephen Smalley new_value = !!new_value; 140ea49d10eSStephen Smalley 1411da177e4SLinus Torvalds if (new_value != selinux_enforcing) { 142be0554c9SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 143be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETENFORCE, 144be0554c9SStephen Smalley NULL); 1451da177e4SLinus Torvalds if (length) 1461da177e4SLinus Torvalds goto out; 147af601e46SSteve Grubb audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS, 1484746ec5bSEric Paris "enforcing=%d old_enforcing=%d auid=%u ses=%u", 1494746ec5bSEric Paris new_value, selinux_enforcing, 150581abc09SEric W. Biederman from_kuid(&init_user_ns, audit_get_loginuid(current)), 1514746ec5bSEric Paris audit_get_sessionid(current)); 1521da177e4SLinus Torvalds selinux_enforcing = new_value; 1531da177e4SLinus Torvalds if (selinux_enforcing) 1541da177e4SLinus Torvalds avc_ss_reset(0); 1551da177e4SLinus Torvalds selnl_notify_setenforce(selinux_enforcing); 15611904167SKaiGai Kohei selinux_status_update_setenforce(selinux_enforcing); 1578f408ab6SDaniel Jurgens if (!selinux_enforcing) 1588f408ab6SDaniel Jurgens call_lsm_notifier(LSM_POLICY_CHANGE, NULL); 1591da177e4SLinus Torvalds } 1601da177e4SLinus Torvalds length = count; 1611da177e4SLinus Torvalds out: 1628365a719SAl Viro kfree(page); 1631da177e4SLinus Torvalds return length; 1641da177e4SLinus Torvalds } 1651da177e4SLinus Torvalds #else 1661da177e4SLinus Torvalds #define sel_write_enforce NULL 1671da177e4SLinus Torvalds #endif 1681da177e4SLinus Torvalds 1699c2e08c5SArjan van de Ven static const struct file_operations sel_enforce_ops = { 1701da177e4SLinus Torvalds .read = sel_read_enforce, 1711da177e4SLinus Torvalds .write = sel_write_enforce, 17257a62c23SArnd Bergmann .llseek = generic_file_llseek, 1731da177e4SLinus Torvalds }; 1741da177e4SLinus Torvalds 1753f12070eSEric Paris static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf, 1763f12070eSEric Paris size_t count, loff_t *ppos) 1773f12070eSEric Paris { 1783f12070eSEric Paris char tmpbuf[TMPBUFLEN]; 1793f12070eSEric Paris ssize_t length; 180496ad9aaSAl Viro ino_t ino = file_inode(filp)->i_ino; 1813f12070eSEric Paris int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ? 1823f12070eSEric Paris security_get_reject_unknown() : !security_get_allow_unknown(); 1833f12070eSEric Paris 1843f12070eSEric Paris length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown); 1853f12070eSEric Paris return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 1863f12070eSEric Paris } 1873f12070eSEric Paris 1883f12070eSEric Paris static const struct file_operations sel_handle_unknown_ops = { 1893f12070eSEric Paris .read = sel_read_handle_unknown, 19057a62c23SArnd Bergmann .llseek = generic_file_llseek, 1913f12070eSEric Paris }; 1923f12070eSEric Paris 19311904167SKaiGai Kohei static int sel_open_handle_status(struct inode *inode, struct file *filp) 19411904167SKaiGai Kohei { 19511904167SKaiGai Kohei struct page *status = selinux_kernel_status_page(); 19611904167SKaiGai Kohei 19711904167SKaiGai Kohei if (!status) 19811904167SKaiGai Kohei return -ENOMEM; 19911904167SKaiGai Kohei 20011904167SKaiGai Kohei filp->private_data = status; 20111904167SKaiGai Kohei 20211904167SKaiGai Kohei return 0; 20311904167SKaiGai Kohei } 20411904167SKaiGai Kohei 20511904167SKaiGai Kohei static ssize_t sel_read_handle_status(struct file *filp, char __user *buf, 20611904167SKaiGai Kohei size_t count, loff_t *ppos) 20711904167SKaiGai Kohei { 20811904167SKaiGai Kohei struct page *status = filp->private_data; 20911904167SKaiGai Kohei 21011904167SKaiGai Kohei BUG_ON(!status); 21111904167SKaiGai Kohei 21211904167SKaiGai Kohei return simple_read_from_buffer(buf, count, ppos, 21311904167SKaiGai Kohei page_address(status), 21411904167SKaiGai Kohei sizeof(struct selinux_kernel_status)); 21511904167SKaiGai Kohei } 21611904167SKaiGai Kohei 21711904167SKaiGai Kohei static int sel_mmap_handle_status(struct file *filp, 21811904167SKaiGai Kohei struct vm_area_struct *vma) 21911904167SKaiGai Kohei { 22011904167SKaiGai Kohei struct page *status = filp->private_data; 22111904167SKaiGai Kohei unsigned long size = vma->vm_end - vma->vm_start; 22211904167SKaiGai Kohei 22311904167SKaiGai Kohei BUG_ON(!status); 22411904167SKaiGai Kohei 22511904167SKaiGai Kohei /* only allows one page from the head */ 22611904167SKaiGai Kohei if (vma->vm_pgoff > 0 || size != PAGE_SIZE) 22711904167SKaiGai Kohei return -EIO; 22811904167SKaiGai Kohei /* disallow writable mapping */ 22911904167SKaiGai Kohei if (vma->vm_flags & VM_WRITE) 23011904167SKaiGai Kohei return -EPERM; 23111904167SKaiGai Kohei /* disallow mprotect() turns it into writable */ 23211904167SKaiGai Kohei vma->vm_flags &= ~VM_MAYWRITE; 23311904167SKaiGai Kohei 23411904167SKaiGai Kohei return remap_pfn_range(vma, vma->vm_start, 23511904167SKaiGai Kohei page_to_pfn(status), 23611904167SKaiGai Kohei size, vma->vm_page_prot); 23711904167SKaiGai Kohei } 23811904167SKaiGai Kohei 23911904167SKaiGai Kohei static const struct file_operations sel_handle_status_ops = { 24011904167SKaiGai Kohei .open = sel_open_handle_status, 24111904167SKaiGai Kohei .read = sel_read_handle_status, 24211904167SKaiGai Kohei .mmap = sel_mmap_handle_status, 24311904167SKaiGai Kohei .llseek = generic_file_llseek, 24411904167SKaiGai Kohei }; 24511904167SKaiGai Kohei 2461da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 2471da177e4SLinus Torvalds static ssize_t sel_write_disable(struct file *file, const char __user *buf, 2481da177e4SLinus Torvalds size_t count, loff_t *ppos) 2491da177e4SLinus Torvalds 2501da177e4SLinus Torvalds { 2518365a719SAl Viro char *page; 2521da177e4SLinus Torvalds ssize_t length; 2531da177e4SLinus Torvalds int new_value; 2541da177e4SLinus Torvalds 255bfd51626SDavi Arnaut if (count >= PAGE_SIZE) 2568365a719SAl Viro return -ENOMEM; 257b77a493bSEric Paris 2581da177e4SLinus Torvalds /* No partial writes. */ 259b77a493bSEric Paris if (*ppos != 0) 2608365a719SAl Viro return -EINVAL; 261b77a493bSEric Paris 2628365a719SAl Viro page = memdup_user_nul(buf, count); 2638365a719SAl Viro if (IS_ERR(page)) 2648365a719SAl Viro return PTR_ERR(page); 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds length = -EINVAL; 2671da177e4SLinus Torvalds if (sscanf(page, "%d", &new_value) != 1) 2681da177e4SLinus Torvalds goto out; 2691da177e4SLinus Torvalds 2701da177e4SLinus Torvalds if (new_value) { 2711da177e4SLinus Torvalds length = selinux_disable(); 272b77a493bSEric Paris if (length) 2731da177e4SLinus Torvalds goto out; 274af601e46SSteve Grubb audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS, 2754746ec5bSEric Paris "selinux=0 auid=%u ses=%u", 276581abc09SEric W. Biederman from_kuid(&init_user_ns, audit_get_loginuid(current)), 2774746ec5bSEric Paris audit_get_sessionid(current)); 2781da177e4SLinus Torvalds } 2791da177e4SLinus Torvalds 2801da177e4SLinus Torvalds length = count; 2811da177e4SLinus Torvalds out: 2828365a719SAl Viro kfree(page); 2831da177e4SLinus Torvalds return length; 2841da177e4SLinus Torvalds } 2851da177e4SLinus Torvalds #else 2861da177e4SLinus Torvalds #define sel_write_disable NULL 2871da177e4SLinus Torvalds #endif 2881da177e4SLinus Torvalds 2899c2e08c5SArjan van de Ven static const struct file_operations sel_disable_ops = { 2901da177e4SLinus Torvalds .write = sel_write_disable, 29157a62c23SArnd Bergmann .llseek = generic_file_llseek, 2921da177e4SLinus Torvalds }; 2931da177e4SLinus Torvalds 2941da177e4SLinus Torvalds static ssize_t sel_read_policyvers(struct file *filp, char __user *buf, 2951da177e4SLinus Torvalds size_t count, loff_t *ppos) 2961da177e4SLinus Torvalds { 2971da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 2981da177e4SLinus Torvalds ssize_t length; 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX); 3011da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 3021da177e4SLinus Torvalds } 3031da177e4SLinus Torvalds 3049c2e08c5SArjan van de Ven static const struct file_operations sel_policyvers_ops = { 3051da177e4SLinus Torvalds .read = sel_read_policyvers, 30657a62c23SArnd Bergmann .llseek = generic_file_llseek, 3071da177e4SLinus Torvalds }; 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds /* declaration for sel_write_load */ 3101da177e4SLinus Torvalds static int sel_make_bools(void); 311e47c8fc5SChristopher J. PeBenito static int sel_make_classes(void); 3123bb56b25SPaul Moore static int sel_make_policycap(void); 313e47c8fc5SChristopher J. PeBenito 314e47c8fc5SChristopher J. PeBenito /* declaration for sel_make_class_dirs */ 315a1c2aa1eSAl Viro static struct dentry *sel_make_dir(struct dentry *dir, const char *name, 316e47c8fc5SChristopher J. PeBenito unsigned long *ino); 3171da177e4SLinus Torvalds 3181da177e4SLinus Torvalds static ssize_t sel_read_mls(struct file *filp, char __user *buf, 3191da177e4SLinus Torvalds size_t count, loff_t *ppos) 3201da177e4SLinus Torvalds { 3211da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 3221da177e4SLinus Torvalds ssize_t length; 3231da177e4SLinus Torvalds 3240719aaf5SGuido Trentalancia length = scnprintf(tmpbuf, TMPBUFLEN, "%d", 3250719aaf5SGuido Trentalancia security_mls_enabled()); 3261da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 3271da177e4SLinus Torvalds } 3281da177e4SLinus Torvalds 3299c2e08c5SArjan van de Ven static const struct file_operations sel_mls_ops = { 3301da177e4SLinus Torvalds .read = sel_read_mls, 33157a62c23SArnd Bergmann .llseek = generic_file_llseek, 3321da177e4SLinus Torvalds }; 3331da177e4SLinus Torvalds 334cee74f47SEric Paris struct policy_load_memory { 335cee74f47SEric Paris size_t len; 336cee74f47SEric Paris void *data; 337cee74f47SEric Paris }; 338cee74f47SEric Paris 339cee74f47SEric Paris static int sel_open_policy(struct inode *inode, struct file *filp) 340cee74f47SEric Paris { 341cee74f47SEric Paris struct policy_load_memory *plm = NULL; 342cee74f47SEric Paris int rc; 343cee74f47SEric Paris 344cee74f47SEric Paris BUG_ON(filp->private_data); 345cee74f47SEric Paris 346cee74f47SEric Paris mutex_lock(&sel_mutex); 347cee74f47SEric Paris 348be0554c9SStephen Smalley rc = avc_has_perm(current_sid(), SECINITSID_SECURITY, 349be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL); 350cee74f47SEric Paris if (rc) 351cee74f47SEric Paris goto err; 352cee74f47SEric Paris 353cee74f47SEric Paris rc = -EBUSY; 354cee74f47SEric Paris if (policy_opened) 355cee74f47SEric Paris goto err; 356cee74f47SEric Paris 357cee74f47SEric Paris rc = -ENOMEM; 358cee74f47SEric Paris plm = kzalloc(sizeof(*plm), GFP_KERNEL); 359cee74f47SEric Paris if (!plm) 360cee74f47SEric Paris goto err; 361cee74f47SEric Paris 362cee74f47SEric Paris if (i_size_read(inode) != security_policydb_len()) { 3635955102cSAl Viro inode_lock(inode); 364cee74f47SEric Paris i_size_write(inode, security_policydb_len()); 3655955102cSAl Viro inode_unlock(inode); 366cee74f47SEric Paris } 367cee74f47SEric Paris 368cee74f47SEric Paris rc = security_read_policy(&plm->data, &plm->len); 369cee74f47SEric Paris if (rc) 370cee74f47SEric Paris goto err; 371cee74f47SEric Paris 372cee74f47SEric Paris policy_opened = 1; 373cee74f47SEric Paris 374cee74f47SEric Paris filp->private_data = plm; 375cee74f47SEric Paris 376cee74f47SEric Paris mutex_unlock(&sel_mutex); 377cee74f47SEric Paris 378cee74f47SEric Paris return 0; 379cee74f47SEric Paris err: 380cee74f47SEric Paris mutex_unlock(&sel_mutex); 381cee74f47SEric Paris 382cee74f47SEric Paris if (plm) 383cee74f47SEric Paris vfree(plm->data); 384cee74f47SEric Paris kfree(plm); 385cee74f47SEric Paris return rc; 386cee74f47SEric Paris } 387cee74f47SEric Paris 388cee74f47SEric Paris static int sel_release_policy(struct inode *inode, struct file *filp) 389cee74f47SEric Paris { 390cee74f47SEric Paris struct policy_load_memory *plm = filp->private_data; 391cee74f47SEric Paris 392cee74f47SEric Paris BUG_ON(!plm); 393cee74f47SEric Paris 394cee74f47SEric Paris policy_opened = 0; 395cee74f47SEric Paris 396cee74f47SEric Paris vfree(plm->data); 397cee74f47SEric Paris kfree(plm); 398cee74f47SEric Paris 399cee74f47SEric Paris return 0; 400cee74f47SEric Paris } 401cee74f47SEric Paris 402cee74f47SEric Paris static ssize_t sel_read_policy(struct file *filp, char __user *buf, 403cee74f47SEric Paris size_t count, loff_t *ppos) 404cee74f47SEric Paris { 405cee74f47SEric Paris struct policy_load_memory *plm = filp->private_data; 406cee74f47SEric Paris int ret; 407cee74f47SEric Paris 408cee74f47SEric Paris mutex_lock(&sel_mutex); 409cee74f47SEric Paris 410be0554c9SStephen Smalley ret = avc_has_perm(current_sid(), SECINITSID_SECURITY, 411be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL); 412cee74f47SEric Paris if (ret) 413cee74f47SEric Paris goto out; 414cee74f47SEric Paris 415cee74f47SEric Paris ret = simple_read_from_buffer(buf, count, ppos, plm->data, plm->len); 416cee74f47SEric Paris out: 417cee74f47SEric Paris mutex_unlock(&sel_mutex); 418cee74f47SEric Paris return ret; 419cee74f47SEric Paris } 420cee74f47SEric Paris 42111bac800SDave Jiang static int sel_mmap_policy_fault(struct vm_fault *vmf) 422845ca30fSEric Paris { 42311bac800SDave Jiang struct policy_load_memory *plm = vmf->vma->vm_file->private_data; 424845ca30fSEric Paris unsigned long offset; 425845ca30fSEric Paris struct page *page; 426845ca30fSEric Paris 427845ca30fSEric Paris if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE)) 428845ca30fSEric Paris return VM_FAULT_SIGBUS; 429845ca30fSEric Paris 430845ca30fSEric Paris offset = vmf->pgoff << PAGE_SHIFT; 431845ca30fSEric Paris if (offset >= roundup(plm->len, PAGE_SIZE)) 432845ca30fSEric Paris return VM_FAULT_SIGBUS; 433845ca30fSEric Paris 434845ca30fSEric Paris page = vmalloc_to_page(plm->data + offset); 435845ca30fSEric Paris get_page(page); 436845ca30fSEric Paris 437845ca30fSEric Paris vmf->page = page; 438845ca30fSEric Paris 439845ca30fSEric Paris return 0; 440845ca30fSEric Paris } 441845ca30fSEric Paris 4427cbea8dcSKirill A. Shutemov static const struct vm_operations_struct sel_mmap_policy_ops = { 443845ca30fSEric Paris .fault = sel_mmap_policy_fault, 444845ca30fSEric Paris .page_mkwrite = sel_mmap_policy_fault, 445845ca30fSEric Paris }; 446845ca30fSEric Paris 447ad3fa08cSJames Morris static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma) 448845ca30fSEric Paris { 449845ca30fSEric Paris if (vma->vm_flags & VM_SHARED) { 450845ca30fSEric Paris /* do not allow mprotect to make mapping writable */ 451845ca30fSEric Paris vma->vm_flags &= ~VM_MAYWRITE; 452845ca30fSEric Paris 453845ca30fSEric Paris if (vma->vm_flags & VM_WRITE) 454845ca30fSEric Paris return -EACCES; 455845ca30fSEric Paris } 456845ca30fSEric Paris 457314e51b9SKonstantin Khlebnikov vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; 458845ca30fSEric Paris vma->vm_ops = &sel_mmap_policy_ops; 459845ca30fSEric Paris 460845ca30fSEric Paris return 0; 461845ca30fSEric Paris } 462845ca30fSEric Paris 463cee74f47SEric Paris static const struct file_operations sel_policy_ops = { 464cee74f47SEric Paris .open = sel_open_policy, 465cee74f47SEric Paris .read = sel_read_policy, 466845ca30fSEric Paris .mmap = sel_mmap_policy, 467cee74f47SEric Paris .release = sel_release_policy, 46847a93a5bSEric Paris .llseek = generic_file_llseek, 469cee74f47SEric Paris }; 470cee74f47SEric Paris 4711da177e4SLinus Torvalds static ssize_t sel_write_load(struct file *file, const char __user *buf, 4721da177e4SLinus Torvalds size_t count, loff_t *ppos) 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds { 4751da177e4SLinus Torvalds ssize_t length; 4761da177e4SLinus Torvalds void *data = NULL; 4771da177e4SLinus Torvalds 478bb003079SIngo Molnar mutex_lock(&sel_mutex); 4791da177e4SLinus Torvalds 480be0554c9SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 481be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL); 4821da177e4SLinus Torvalds if (length) 4831da177e4SLinus Torvalds goto out; 4841da177e4SLinus Torvalds 4851da177e4SLinus Torvalds /* No partial writes. */ 4861da177e4SLinus Torvalds length = -EINVAL; 487b77a493bSEric Paris if (*ppos != 0) 4881da177e4SLinus Torvalds goto out; 4891da177e4SLinus Torvalds 490b77a493bSEric Paris length = -EFBIG; 491b77a493bSEric Paris if (count > 64 * 1024 * 1024) 4921da177e4SLinus Torvalds goto out; 493b77a493bSEric Paris 494b77a493bSEric Paris length = -ENOMEM; 495b77a493bSEric Paris data = vmalloc(count); 496b77a493bSEric Paris if (!data) 497b77a493bSEric Paris goto out; 4981da177e4SLinus Torvalds 4991da177e4SLinus Torvalds length = -EFAULT; 5001da177e4SLinus Torvalds if (copy_from_user(data, buf, count) != 0) 5011da177e4SLinus Torvalds goto out; 5021da177e4SLinus Torvalds 5031da177e4SLinus Torvalds length = security_load_policy(data, count); 5044262fb51SGary Tierney if (length) { 5054262fb51SGary Tierney pr_warn_ratelimited("SELinux: failed to load policy\n"); 5061da177e4SLinus Torvalds goto out; 5074262fb51SGary Tierney } 5081da177e4SLinus Torvalds 509b77a493bSEric Paris length = sel_make_bools(); 5104262fb51SGary Tierney if (length) { 5114262fb51SGary Tierney pr_err("SELinux: failed to load policy booleans\n"); 512e47c8fc5SChristopher J. PeBenito goto out1; 5134262fb51SGary Tierney } 514e47c8fc5SChristopher J. PeBenito 515b77a493bSEric Paris length = sel_make_classes(); 5164262fb51SGary Tierney if (length) { 5174262fb51SGary Tierney pr_err("SELinux: failed to load policy classes\n"); 5183bb56b25SPaul Moore goto out1; 5194262fb51SGary Tierney } 5203bb56b25SPaul Moore 521b77a493bSEric Paris length = sel_make_policycap(); 5224262fb51SGary Tierney if (length) { 5234262fb51SGary Tierney pr_err("SELinux: failed to load policy capabilities\n"); 524b77a493bSEric Paris goto out1; 5254262fb51SGary Tierney } 526b77a493bSEric Paris 5271da177e4SLinus Torvalds length = count; 528e47c8fc5SChristopher J. PeBenito 529e47c8fc5SChristopher J. PeBenito out1: 530af601e46SSteve Grubb audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD, 5314746ec5bSEric Paris "policy loaded auid=%u ses=%u", 532581abc09SEric W. Biederman from_kuid(&init_user_ns, audit_get_loginuid(current)), 5334746ec5bSEric Paris audit_get_sessionid(current)); 5341da177e4SLinus Torvalds out: 535bb003079SIngo Molnar mutex_unlock(&sel_mutex); 5361da177e4SLinus Torvalds vfree(data); 5371da177e4SLinus Torvalds return length; 5381da177e4SLinus Torvalds } 5391da177e4SLinus Torvalds 5409c2e08c5SArjan van de Ven static const struct file_operations sel_load_ops = { 5411da177e4SLinus Torvalds .write = sel_write_load, 54257a62c23SArnd Bergmann .llseek = generic_file_llseek, 5431da177e4SLinus Torvalds }; 5441da177e4SLinus Torvalds 545ce9982d0SStephen Smalley static ssize_t sel_write_context(struct file *file, char *buf, size_t size) 5461da177e4SLinus Torvalds { 547b77a493bSEric Paris char *canon = NULL; 548ce9982d0SStephen Smalley u32 sid, len; 5491da177e4SLinus Torvalds ssize_t length; 5501da177e4SLinus Torvalds 551be0554c9SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 552be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__CHECK_CONTEXT, NULL); 5531da177e4SLinus Torvalds if (length) 554b77a493bSEric Paris goto out; 5551da177e4SLinus Torvalds 55652a4c640SNikolay Aleksandrov length = security_context_to_sid(buf, size, &sid, GFP_KERNEL); 557b77a493bSEric Paris if (length) 558b77a493bSEric Paris goto out; 5591da177e4SLinus Torvalds 560ce9982d0SStephen Smalley length = security_sid_to_context(sid, &canon, &len); 561b77a493bSEric Paris if (length) 562b77a493bSEric Paris goto out; 563ce9982d0SStephen Smalley 564b77a493bSEric Paris length = -ERANGE; 565ce9982d0SStephen Smalley if (len > SIMPLE_TRANSACTION_LIMIT) { 566744ba35eSEric Paris printk(KERN_ERR "SELinux: %s: context size (%u) exceeds " 567744ba35eSEric Paris "payload max\n", __func__, len); 568ce9982d0SStephen Smalley goto out; 569ce9982d0SStephen Smalley } 570ce9982d0SStephen Smalley 571ce9982d0SStephen Smalley memcpy(buf, canon, len); 572ce9982d0SStephen Smalley length = len; 5731da177e4SLinus Torvalds out: 574ce9982d0SStephen Smalley kfree(canon); 5751da177e4SLinus Torvalds return length; 5761da177e4SLinus Torvalds } 5771da177e4SLinus Torvalds 5781da177e4SLinus Torvalds static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf, 5791da177e4SLinus Torvalds size_t count, loff_t *ppos) 5801da177e4SLinus Torvalds { 5811da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 5821da177e4SLinus Torvalds ssize_t length; 5831da177e4SLinus Torvalds 5841da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot); 5851da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 5861da177e4SLinus Torvalds } 5871da177e4SLinus Torvalds 5881da177e4SLinus Torvalds static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf, 5891da177e4SLinus Torvalds size_t count, loff_t *ppos) 5901da177e4SLinus Torvalds { 5918365a719SAl Viro char *page; 5921da177e4SLinus Torvalds ssize_t length; 5931da177e4SLinus Torvalds unsigned int new_value; 5941da177e4SLinus Torvalds 595be0554c9SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 596be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETCHECKREQPROT, 597be0554c9SStephen Smalley NULL); 5981da177e4SLinus Torvalds if (length) 5998365a719SAl Viro return length; 6001da177e4SLinus Torvalds 601bfd51626SDavi Arnaut if (count >= PAGE_SIZE) 6028365a719SAl Viro return -ENOMEM; 603b77a493bSEric Paris 6041da177e4SLinus Torvalds /* No partial writes. */ 605b77a493bSEric Paris if (*ppos != 0) 6068365a719SAl Viro return -EINVAL; 607b77a493bSEric Paris 6088365a719SAl Viro page = memdup_user_nul(buf, count); 6098365a719SAl Viro if (IS_ERR(page)) 6108365a719SAl Viro return PTR_ERR(page); 6111da177e4SLinus Torvalds 6121da177e4SLinus Torvalds length = -EINVAL; 6131da177e4SLinus Torvalds if (sscanf(page, "%u", &new_value) != 1) 6141da177e4SLinus Torvalds goto out; 6151da177e4SLinus Torvalds 6161da177e4SLinus Torvalds selinux_checkreqprot = new_value ? 1 : 0; 6171da177e4SLinus Torvalds length = count; 6181da177e4SLinus Torvalds out: 6198365a719SAl Viro kfree(page); 6201da177e4SLinus Torvalds return length; 6211da177e4SLinus Torvalds } 6229c2e08c5SArjan van de Ven static const struct file_operations sel_checkreqprot_ops = { 6231da177e4SLinus Torvalds .read = sel_read_checkreqprot, 6241da177e4SLinus Torvalds .write = sel_write_checkreqprot, 62557a62c23SArnd Bergmann .llseek = generic_file_llseek, 6261da177e4SLinus Torvalds }; 6271da177e4SLinus Torvalds 628f9df6458SAndrew Perepechko static ssize_t sel_write_validatetrans(struct file *file, 629f9df6458SAndrew Perepechko const char __user *buf, 630f9df6458SAndrew Perepechko size_t count, loff_t *ppos) 631f9df6458SAndrew Perepechko { 632f9df6458SAndrew Perepechko char *oldcon = NULL, *newcon = NULL, *taskcon = NULL; 633f9df6458SAndrew Perepechko char *req = NULL; 634f9df6458SAndrew Perepechko u32 osid, nsid, tsid; 635f9df6458SAndrew Perepechko u16 tclass; 636f9df6458SAndrew Perepechko int rc; 637f9df6458SAndrew Perepechko 638be0554c9SStephen Smalley rc = avc_has_perm(current_sid(), SECINITSID_SECURITY, 639be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__VALIDATE_TRANS, NULL); 640f9df6458SAndrew Perepechko if (rc) 641f9df6458SAndrew Perepechko goto out; 642f9df6458SAndrew Perepechko 643f9df6458SAndrew Perepechko rc = -ENOMEM; 644f9df6458SAndrew Perepechko if (count >= PAGE_SIZE) 645f9df6458SAndrew Perepechko goto out; 646f9df6458SAndrew Perepechko 647f9df6458SAndrew Perepechko /* No partial writes. */ 648f9df6458SAndrew Perepechko rc = -EINVAL; 649f9df6458SAndrew Perepechko if (*ppos != 0) 650f9df6458SAndrew Perepechko goto out; 651f9df6458SAndrew Perepechko 652f9df6458SAndrew Perepechko rc = -ENOMEM; 653f9df6458SAndrew Perepechko req = kzalloc(count + 1, GFP_KERNEL); 654f9df6458SAndrew Perepechko if (!req) 655f9df6458SAndrew Perepechko goto out; 656f9df6458SAndrew Perepechko 657f9df6458SAndrew Perepechko rc = -EFAULT; 658f9df6458SAndrew Perepechko if (copy_from_user(req, buf, count)) 659f9df6458SAndrew Perepechko goto out; 660f9df6458SAndrew Perepechko 661f9df6458SAndrew Perepechko rc = -ENOMEM; 662f9df6458SAndrew Perepechko oldcon = kzalloc(count + 1, GFP_KERNEL); 663f9df6458SAndrew Perepechko if (!oldcon) 664f9df6458SAndrew Perepechko goto out; 665f9df6458SAndrew Perepechko 666f9df6458SAndrew Perepechko newcon = kzalloc(count + 1, GFP_KERNEL); 667f9df6458SAndrew Perepechko if (!newcon) 668f9df6458SAndrew Perepechko goto out; 669f9df6458SAndrew Perepechko 670f9df6458SAndrew Perepechko taskcon = kzalloc(count + 1, GFP_KERNEL); 671f9df6458SAndrew Perepechko if (!taskcon) 672f9df6458SAndrew Perepechko goto out; 673f9df6458SAndrew Perepechko 674f9df6458SAndrew Perepechko rc = -EINVAL; 675f9df6458SAndrew Perepechko if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4) 676f9df6458SAndrew Perepechko goto out; 677f9df6458SAndrew Perepechko 678f9df6458SAndrew Perepechko rc = security_context_str_to_sid(oldcon, &osid, GFP_KERNEL); 679f9df6458SAndrew Perepechko if (rc) 680f9df6458SAndrew Perepechko goto out; 681f9df6458SAndrew Perepechko 682f9df6458SAndrew Perepechko rc = security_context_str_to_sid(newcon, &nsid, GFP_KERNEL); 683f9df6458SAndrew Perepechko if (rc) 684f9df6458SAndrew Perepechko goto out; 685f9df6458SAndrew Perepechko 686f9df6458SAndrew Perepechko rc = security_context_str_to_sid(taskcon, &tsid, GFP_KERNEL); 687f9df6458SAndrew Perepechko if (rc) 688f9df6458SAndrew Perepechko goto out; 689f9df6458SAndrew Perepechko 690f9df6458SAndrew Perepechko rc = security_validate_transition_user(osid, nsid, tsid, tclass); 691f9df6458SAndrew Perepechko if (!rc) 692f9df6458SAndrew Perepechko rc = count; 693f9df6458SAndrew Perepechko out: 694f9df6458SAndrew Perepechko kfree(req); 695f9df6458SAndrew Perepechko kfree(oldcon); 696f9df6458SAndrew Perepechko kfree(newcon); 697f9df6458SAndrew Perepechko kfree(taskcon); 698f9df6458SAndrew Perepechko return rc; 699f9df6458SAndrew Perepechko } 700f9df6458SAndrew Perepechko 701f9df6458SAndrew Perepechko static const struct file_operations sel_transition_ops = { 702f9df6458SAndrew Perepechko .write = sel_write_validatetrans, 703f9df6458SAndrew Perepechko .llseek = generic_file_llseek, 704f9df6458SAndrew Perepechko }; 705f9df6458SAndrew Perepechko 7061da177e4SLinus Torvalds /* 7071da177e4SLinus Torvalds * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c 7081da177e4SLinus Torvalds */ 7091da177e4SLinus Torvalds static ssize_t sel_write_access(struct file *file, char *buf, size_t size); 7101da177e4SLinus Torvalds static ssize_t sel_write_create(struct file *file, char *buf, size_t size); 7111da177e4SLinus Torvalds static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size); 7121da177e4SLinus Torvalds static ssize_t sel_write_user(struct file *file, char *buf, size_t size); 7131da177e4SLinus Torvalds static ssize_t sel_write_member(struct file *file, char *buf, size_t size); 7141da177e4SLinus Torvalds 7151da177e4SLinus Torvalds static ssize_t (*write_op[])(struct file *, char *, size_t) = { 7161da177e4SLinus Torvalds [SEL_ACCESS] = sel_write_access, 7171da177e4SLinus Torvalds [SEL_CREATE] = sel_write_create, 7181da177e4SLinus Torvalds [SEL_RELABEL] = sel_write_relabel, 7191da177e4SLinus Torvalds [SEL_USER] = sel_write_user, 7201da177e4SLinus Torvalds [SEL_MEMBER] = sel_write_member, 721ce9982d0SStephen Smalley [SEL_CONTEXT] = sel_write_context, 7221da177e4SLinus Torvalds }; 7231da177e4SLinus Torvalds 7241da177e4SLinus Torvalds static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos) 7251da177e4SLinus Torvalds { 726496ad9aaSAl Viro ino_t ino = file_inode(file)->i_ino; 7271da177e4SLinus Torvalds char *data; 7281da177e4SLinus Torvalds ssize_t rv; 7291da177e4SLinus Torvalds 7306e20a64aSNicolas Kaiser if (ino >= ARRAY_SIZE(write_op) || !write_op[ino]) 7311da177e4SLinus Torvalds return -EINVAL; 7321da177e4SLinus Torvalds 7331da177e4SLinus Torvalds data = simple_transaction_get(file, buf, size); 7341da177e4SLinus Torvalds if (IS_ERR(data)) 7351da177e4SLinus Torvalds return PTR_ERR(data); 7361da177e4SLinus Torvalds 7371da177e4SLinus Torvalds rv = write_op[ino](file, data, size); 7381da177e4SLinus Torvalds if (rv > 0) { 7391da177e4SLinus Torvalds simple_transaction_set(file, rv); 7401da177e4SLinus Torvalds rv = size; 7411da177e4SLinus Torvalds } 7421da177e4SLinus Torvalds return rv; 7431da177e4SLinus Torvalds } 7441da177e4SLinus Torvalds 7459c2e08c5SArjan van de Ven static const struct file_operations transaction_ops = { 7461da177e4SLinus Torvalds .write = selinux_transaction_write, 7471da177e4SLinus Torvalds .read = simple_transaction_read, 7481da177e4SLinus Torvalds .release = simple_transaction_release, 74957a62c23SArnd Bergmann .llseek = generic_file_llseek, 7501da177e4SLinus Torvalds }; 7511da177e4SLinus Torvalds 7521da177e4SLinus Torvalds /* 7531da177e4SLinus Torvalds * payload - write methods 7541da177e4SLinus Torvalds * If the method has a response, the response should be put in buf, 7551da177e4SLinus Torvalds * and the length returned. Otherwise return 0 or and -error. 7561da177e4SLinus Torvalds */ 7571da177e4SLinus Torvalds 7581da177e4SLinus Torvalds static ssize_t sel_write_access(struct file *file, char *buf, size_t size) 7591da177e4SLinus Torvalds { 760b77a493bSEric Paris char *scon = NULL, *tcon = NULL; 7611da177e4SLinus Torvalds u32 ssid, tsid; 7621da177e4SLinus Torvalds u16 tclass; 7631da177e4SLinus Torvalds struct av_decision avd; 7641da177e4SLinus Torvalds ssize_t length; 7651da177e4SLinus Torvalds 766be0554c9SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 767be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_AV, NULL); 7681da177e4SLinus Torvalds if (length) 769b77a493bSEric Paris goto out; 7701da177e4SLinus Torvalds 7711da177e4SLinus Torvalds length = -ENOMEM; 77289d155efSJames Morris scon = kzalloc(size + 1, GFP_KERNEL); 7731da177e4SLinus Torvalds if (!scon) 774b77a493bSEric Paris goto out; 7751da177e4SLinus Torvalds 776b77a493bSEric Paris length = -ENOMEM; 77789d155efSJames Morris tcon = kzalloc(size + 1, GFP_KERNEL); 7781da177e4SLinus Torvalds if (!tcon) 7791da177e4SLinus Torvalds goto out; 7801da177e4SLinus Torvalds 7811da177e4SLinus Torvalds length = -EINVAL; 78219439d05SStephen Smalley if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) 783b77a493bSEric Paris goto out; 7841da177e4SLinus Torvalds 78544be2f65SRasmus Villemoes length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL); 786b77a493bSEric Paris if (length) 787b77a493bSEric Paris goto out; 788b77a493bSEric Paris 78944be2f65SRasmus Villemoes length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL); 790b77a493bSEric Paris if (length) 791b77a493bSEric Paris goto out; 7921da177e4SLinus Torvalds 79319439d05SStephen Smalley security_compute_av_user(ssid, tsid, tclass, &avd); 7941da177e4SLinus Torvalds 7951da177e4SLinus Torvalds length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, 7968a6f83afSKaiGai Kohei "%x %x %x %x %u %x", 797f1c6381aSEric Paris avd.allowed, 0xffffffff, 7981da177e4SLinus Torvalds avd.auditallow, avd.auditdeny, 7998a6f83afSKaiGai Kohei avd.seqno, avd.flags); 8001da177e4SLinus Torvalds out: 801b77a493bSEric Paris kfree(tcon); 8021da177e4SLinus Torvalds kfree(scon); 8031da177e4SLinus Torvalds return length; 8041da177e4SLinus Torvalds } 8051da177e4SLinus Torvalds 8061da177e4SLinus Torvalds static ssize_t sel_write_create(struct file *file, char *buf, size_t size) 8071da177e4SLinus Torvalds { 808b77a493bSEric Paris char *scon = NULL, *tcon = NULL; 809f50a3ec9SKohei Kaigai char *namebuf = NULL, *objname = NULL; 8101da177e4SLinus Torvalds u32 ssid, tsid, newsid; 8111da177e4SLinus Torvalds u16 tclass; 8121da177e4SLinus Torvalds ssize_t length; 813b77a493bSEric Paris char *newcon = NULL; 8141da177e4SLinus Torvalds u32 len; 815f50a3ec9SKohei Kaigai int nargs; 8161da177e4SLinus Torvalds 817be0554c9SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 818be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_CREATE, 819be0554c9SStephen Smalley NULL); 8201da177e4SLinus Torvalds if (length) 821b77a493bSEric Paris goto out; 8221da177e4SLinus Torvalds 8231da177e4SLinus Torvalds length = -ENOMEM; 82489d155efSJames Morris scon = kzalloc(size + 1, GFP_KERNEL); 8251da177e4SLinus Torvalds if (!scon) 826b77a493bSEric Paris goto out; 8271da177e4SLinus Torvalds 828b77a493bSEric Paris length = -ENOMEM; 82989d155efSJames Morris tcon = kzalloc(size + 1, GFP_KERNEL); 8301da177e4SLinus Torvalds if (!tcon) 8311da177e4SLinus Torvalds goto out; 8321da177e4SLinus Torvalds 833f50a3ec9SKohei Kaigai length = -ENOMEM; 834f50a3ec9SKohei Kaigai namebuf = kzalloc(size + 1, GFP_KERNEL); 835f50a3ec9SKohei Kaigai if (!namebuf) 836b77a493bSEric Paris goto out; 8371da177e4SLinus Torvalds 838f50a3ec9SKohei Kaigai length = -EINVAL; 839f50a3ec9SKohei Kaigai nargs = sscanf(buf, "%s %s %hu %s", scon, tcon, &tclass, namebuf); 840f50a3ec9SKohei Kaigai if (nargs < 3 || nargs > 4) 841f50a3ec9SKohei Kaigai goto out; 8420f7e4c33SKohei Kaigai if (nargs == 4) { 8430f7e4c33SKohei Kaigai /* 8440f7e4c33SKohei Kaigai * If and when the name of new object to be queried contains 8450f7e4c33SKohei Kaigai * either whitespace or multibyte characters, they shall be 8460f7e4c33SKohei Kaigai * encoded based on the percentage-encoding rule. 8470f7e4c33SKohei Kaigai * If not encoded, the sscanf logic picks up only left-half 8480f7e4c33SKohei Kaigai * of the supplied name; splitted by a whitespace unexpectedly. 8490f7e4c33SKohei Kaigai */ 8500f7e4c33SKohei Kaigai char *r, *w; 8510f7e4c33SKohei Kaigai int c1, c2; 8520f7e4c33SKohei Kaigai 8530f7e4c33SKohei Kaigai r = w = namebuf; 8540f7e4c33SKohei Kaigai do { 8550f7e4c33SKohei Kaigai c1 = *r++; 8560f7e4c33SKohei Kaigai if (c1 == '+') 8570f7e4c33SKohei Kaigai c1 = ' '; 8580f7e4c33SKohei Kaigai else if (c1 == '%') { 859af7ff2c2SAndy Shevchenko c1 = hex_to_bin(*r++); 860af7ff2c2SAndy Shevchenko if (c1 < 0) 8610f7e4c33SKohei Kaigai goto out; 862af7ff2c2SAndy Shevchenko c2 = hex_to_bin(*r++); 863af7ff2c2SAndy Shevchenko if (c2 < 0) 8640f7e4c33SKohei Kaigai goto out; 8650f7e4c33SKohei Kaigai c1 = (c1 << 4) | c2; 8660f7e4c33SKohei Kaigai } 8670f7e4c33SKohei Kaigai *w++ = c1; 8680f7e4c33SKohei Kaigai } while (c1 != '\0'); 8690f7e4c33SKohei Kaigai 870f50a3ec9SKohei Kaigai objname = namebuf; 8710f7e4c33SKohei Kaigai } 872f50a3ec9SKohei Kaigai 87344be2f65SRasmus Villemoes length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL); 874b77a493bSEric Paris if (length) 875b77a493bSEric Paris goto out; 876b77a493bSEric Paris 87744be2f65SRasmus Villemoes length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL); 878b77a493bSEric Paris if (length) 879b77a493bSEric Paris goto out; 8801da177e4SLinus Torvalds 881f50a3ec9SKohei Kaigai length = security_transition_sid_user(ssid, tsid, tclass, 882f50a3ec9SKohei Kaigai objname, &newsid); 883b77a493bSEric Paris if (length) 884b77a493bSEric Paris goto out; 8851da177e4SLinus Torvalds 8861da177e4SLinus Torvalds length = security_sid_to_context(newsid, &newcon, &len); 887b77a493bSEric Paris if (length) 888b77a493bSEric Paris goto out; 8891da177e4SLinus Torvalds 890b77a493bSEric Paris length = -ERANGE; 8911da177e4SLinus Torvalds if (len > SIMPLE_TRANSACTION_LIMIT) { 892744ba35eSEric Paris printk(KERN_ERR "SELinux: %s: context size (%u) exceeds " 893744ba35eSEric Paris "payload max\n", __func__, len); 894b77a493bSEric Paris goto out; 8951da177e4SLinus Torvalds } 8961da177e4SLinus Torvalds 8971da177e4SLinus Torvalds memcpy(buf, newcon, len); 8981da177e4SLinus Torvalds length = len; 8991da177e4SLinus Torvalds out: 900b77a493bSEric Paris kfree(newcon); 901f50a3ec9SKohei Kaigai kfree(namebuf); 902b77a493bSEric Paris kfree(tcon); 9031da177e4SLinus Torvalds kfree(scon); 9041da177e4SLinus Torvalds return length; 9051da177e4SLinus Torvalds } 9061da177e4SLinus Torvalds 9071da177e4SLinus Torvalds static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size) 9081da177e4SLinus Torvalds { 909b77a493bSEric Paris char *scon = NULL, *tcon = NULL; 9101da177e4SLinus Torvalds u32 ssid, tsid, newsid; 9111da177e4SLinus Torvalds u16 tclass; 9121da177e4SLinus Torvalds ssize_t length; 913b77a493bSEric Paris char *newcon = NULL; 9141da177e4SLinus Torvalds u32 len; 9151da177e4SLinus Torvalds 916be0554c9SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 917be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_RELABEL, 918be0554c9SStephen Smalley NULL); 9191da177e4SLinus Torvalds if (length) 920b77a493bSEric Paris goto out; 9211da177e4SLinus Torvalds 9221da177e4SLinus Torvalds length = -ENOMEM; 92389d155efSJames Morris scon = kzalloc(size + 1, GFP_KERNEL); 9241da177e4SLinus Torvalds if (!scon) 925b77a493bSEric Paris goto out; 9261da177e4SLinus Torvalds 927b77a493bSEric Paris length = -ENOMEM; 92889d155efSJames Morris tcon = kzalloc(size + 1, GFP_KERNEL); 9291da177e4SLinus Torvalds if (!tcon) 9301da177e4SLinus Torvalds goto out; 9311da177e4SLinus Torvalds 9321da177e4SLinus Torvalds length = -EINVAL; 9331da177e4SLinus Torvalds if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) 934b77a493bSEric Paris goto out; 9351da177e4SLinus Torvalds 93644be2f65SRasmus Villemoes length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL); 937b77a493bSEric Paris if (length) 938b77a493bSEric Paris goto out; 939b77a493bSEric Paris 94044be2f65SRasmus Villemoes length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL); 941b77a493bSEric Paris if (length) 942b77a493bSEric Paris goto out; 9431da177e4SLinus Torvalds 9441da177e4SLinus Torvalds length = security_change_sid(ssid, tsid, tclass, &newsid); 945b77a493bSEric Paris if (length) 946b77a493bSEric Paris goto out; 9471da177e4SLinus Torvalds 9481da177e4SLinus Torvalds length = security_sid_to_context(newsid, &newcon, &len); 949b77a493bSEric Paris if (length) 950b77a493bSEric Paris goto out; 9511da177e4SLinus Torvalds 9521da177e4SLinus Torvalds length = -ERANGE; 953b77a493bSEric Paris if (len > SIMPLE_TRANSACTION_LIMIT) 954b77a493bSEric Paris goto out; 9551da177e4SLinus Torvalds 9561da177e4SLinus Torvalds memcpy(buf, newcon, len); 9571da177e4SLinus Torvalds length = len; 9581da177e4SLinus Torvalds out: 959b77a493bSEric Paris kfree(newcon); 960b77a493bSEric Paris kfree(tcon); 9611da177e4SLinus Torvalds kfree(scon); 9621da177e4SLinus Torvalds return length; 9631da177e4SLinus Torvalds } 9641da177e4SLinus Torvalds 9651da177e4SLinus Torvalds static ssize_t sel_write_user(struct file *file, char *buf, size_t size) 9661da177e4SLinus Torvalds { 967b77a493bSEric Paris char *con = NULL, *user = NULL, *ptr; 968b77a493bSEric Paris u32 sid, *sids = NULL; 9691da177e4SLinus Torvalds ssize_t length; 9701da177e4SLinus Torvalds char *newcon; 9711da177e4SLinus Torvalds int i, rc; 9721da177e4SLinus Torvalds u32 len, nsids; 9731da177e4SLinus Torvalds 974be0554c9SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 975be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_USER, 976be0554c9SStephen Smalley NULL); 9771da177e4SLinus Torvalds if (length) 9786eab04a8SJustin P. Mattock goto out; 9791da177e4SLinus Torvalds 9801da177e4SLinus Torvalds length = -ENOMEM; 98189d155efSJames Morris con = kzalloc(size + 1, GFP_KERNEL); 9821da177e4SLinus Torvalds if (!con) 9836eab04a8SJustin P. Mattock goto out; 9841da177e4SLinus Torvalds 985b77a493bSEric Paris length = -ENOMEM; 98689d155efSJames Morris user = kzalloc(size + 1, GFP_KERNEL); 9871da177e4SLinus Torvalds if (!user) 9881da177e4SLinus Torvalds goto out; 9891da177e4SLinus Torvalds 9901da177e4SLinus Torvalds length = -EINVAL; 9911da177e4SLinus Torvalds if (sscanf(buf, "%s %s", con, user) != 2) 992b77a493bSEric Paris goto out; 9931da177e4SLinus Torvalds 99444be2f65SRasmus Villemoes length = security_context_str_to_sid(con, &sid, GFP_KERNEL); 995b77a493bSEric Paris if (length) 996b77a493bSEric Paris goto out; 9971da177e4SLinus Torvalds 9981da177e4SLinus Torvalds length = security_get_user_sids(sid, user, &sids, &nsids); 999b77a493bSEric Paris if (length) 1000b77a493bSEric Paris goto out; 10011da177e4SLinus Torvalds 10021da177e4SLinus Torvalds length = sprintf(buf, "%u", nsids) + 1; 10031da177e4SLinus Torvalds ptr = buf + length; 10041da177e4SLinus Torvalds for (i = 0; i < nsids; i++) { 10051da177e4SLinus Torvalds rc = security_sid_to_context(sids[i], &newcon, &len); 10061da177e4SLinus Torvalds if (rc) { 10071da177e4SLinus Torvalds length = rc; 1008b77a493bSEric Paris goto out; 10091da177e4SLinus Torvalds } 10101da177e4SLinus Torvalds if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) { 10111da177e4SLinus Torvalds kfree(newcon); 10121da177e4SLinus Torvalds length = -ERANGE; 1013b77a493bSEric Paris goto out; 10141da177e4SLinus Torvalds } 10151da177e4SLinus Torvalds memcpy(ptr, newcon, len); 10161da177e4SLinus Torvalds kfree(newcon); 10171da177e4SLinus Torvalds ptr += len; 10181da177e4SLinus Torvalds length += len; 10191da177e4SLinus Torvalds } 10201da177e4SLinus Torvalds out: 1021b77a493bSEric Paris kfree(sids); 1022b77a493bSEric Paris kfree(user); 10231da177e4SLinus Torvalds kfree(con); 10241da177e4SLinus Torvalds return length; 10251da177e4SLinus Torvalds } 10261da177e4SLinus Torvalds 10271da177e4SLinus Torvalds static ssize_t sel_write_member(struct file *file, char *buf, size_t size) 10281da177e4SLinus Torvalds { 1029b77a493bSEric Paris char *scon = NULL, *tcon = NULL; 10301da177e4SLinus Torvalds u32 ssid, tsid, newsid; 10311da177e4SLinus Torvalds u16 tclass; 10321da177e4SLinus Torvalds ssize_t length; 1033b77a493bSEric Paris char *newcon = NULL; 10341da177e4SLinus Torvalds u32 len; 10351da177e4SLinus Torvalds 1036be0554c9SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 1037be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_MEMBER, 1038be0554c9SStephen Smalley NULL); 10391da177e4SLinus Torvalds if (length) 1040b77a493bSEric Paris goto out; 10411da177e4SLinus Torvalds 10421da177e4SLinus Torvalds length = -ENOMEM; 104389d155efSJames Morris scon = kzalloc(size + 1, GFP_KERNEL); 10441da177e4SLinus Torvalds if (!scon) 10456eab04a8SJustin P. Mattock goto out; 10461da177e4SLinus Torvalds 1047b77a493bSEric Paris length = -ENOMEM; 104889d155efSJames Morris tcon = kzalloc(size + 1, GFP_KERNEL); 10491da177e4SLinus Torvalds if (!tcon) 10501da177e4SLinus Torvalds goto out; 10511da177e4SLinus Torvalds 10521da177e4SLinus Torvalds length = -EINVAL; 10531da177e4SLinus Torvalds if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) 1054b77a493bSEric Paris goto out; 10551da177e4SLinus Torvalds 105644be2f65SRasmus Villemoes length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL); 1057b77a493bSEric Paris if (length) 1058b77a493bSEric Paris goto out; 1059b77a493bSEric Paris 106044be2f65SRasmus Villemoes length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL); 1061b77a493bSEric Paris if (length) 1062b77a493bSEric Paris goto out; 10631da177e4SLinus Torvalds 10641da177e4SLinus Torvalds length = security_member_sid(ssid, tsid, tclass, &newsid); 1065b77a493bSEric Paris if (length) 1066b77a493bSEric Paris goto out; 10671da177e4SLinus Torvalds 10681da177e4SLinus Torvalds length = security_sid_to_context(newsid, &newcon, &len); 1069b77a493bSEric Paris if (length) 1070b77a493bSEric Paris goto out; 10711da177e4SLinus Torvalds 1072b77a493bSEric Paris length = -ERANGE; 10731da177e4SLinus Torvalds if (len > SIMPLE_TRANSACTION_LIMIT) { 1074744ba35eSEric Paris printk(KERN_ERR "SELinux: %s: context size (%u) exceeds " 1075744ba35eSEric Paris "payload max\n", __func__, len); 1076b77a493bSEric Paris goto out; 10771da177e4SLinus Torvalds } 10781da177e4SLinus Torvalds 10791da177e4SLinus Torvalds memcpy(buf, newcon, len); 10801da177e4SLinus Torvalds length = len; 10811da177e4SLinus Torvalds out: 1082b77a493bSEric Paris kfree(newcon); 1083b77a493bSEric Paris kfree(tcon); 10841da177e4SLinus Torvalds kfree(scon); 10851da177e4SLinus Torvalds return length; 10861da177e4SLinus Torvalds } 10871da177e4SLinus Torvalds 10881da177e4SLinus Torvalds static struct inode *sel_make_inode(struct super_block *sb, int mode) 10891da177e4SLinus Torvalds { 10901da177e4SLinus Torvalds struct inode *ret = new_inode(sb); 10911da177e4SLinus Torvalds 10921da177e4SLinus Torvalds if (ret) { 10931da177e4SLinus Torvalds ret->i_mode = mode; 1094078cd827SDeepa Dinamani ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret); 10951da177e4SLinus Torvalds } 10961da177e4SLinus Torvalds return ret; 10971da177e4SLinus Torvalds } 10981da177e4SLinus Torvalds 10991da177e4SLinus Torvalds static ssize_t sel_read_bool(struct file *filep, char __user *buf, 11001da177e4SLinus Torvalds size_t count, loff_t *ppos) 11011da177e4SLinus Torvalds { 11021da177e4SLinus Torvalds char *page = NULL; 11031da177e4SLinus Torvalds ssize_t length; 11041da177e4SLinus Torvalds ssize_t ret; 11051da177e4SLinus Torvalds int cur_enforcing; 1106496ad9aaSAl Viro unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK; 1107d313f948SStephen Smalley const char *name = filep->f_path.dentry->d_name.name; 11081da177e4SLinus Torvalds 1109bb003079SIngo Molnar mutex_lock(&sel_mutex); 11101da177e4SLinus Torvalds 1111d313f948SStephen Smalley ret = -EINVAL; 1112b77a493bSEric Paris if (index >= bool_num || strcmp(name, bool_pending_names[index])) 1113d313f948SStephen Smalley goto out; 11141da177e4SLinus Torvalds 11151da177e4SLinus Torvalds ret = -ENOMEM; 1116b77a493bSEric Paris page = (char *)get_zeroed_page(GFP_KERNEL); 1117b77a493bSEric Paris if (!page) 11181da177e4SLinus Torvalds goto out; 11191da177e4SLinus Torvalds 1120d313f948SStephen Smalley cur_enforcing = security_get_bool_value(index); 11211da177e4SLinus Torvalds if (cur_enforcing < 0) { 11221da177e4SLinus Torvalds ret = cur_enforcing; 11231da177e4SLinus Torvalds goto out; 11241da177e4SLinus Torvalds } 11251da177e4SLinus Torvalds length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing, 1126d313f948SStephen Smalley bool_pending_values[index]); 112768bdcf28SStephen Smalley ret = simple_read_from_buffer(buf, count, ppos, page, length); 11281da177e4SLinus Torvalds out: 1129bb003079SIngo Molnar mutex_unlock(&sel_mutex); 11301da177e4SLinus Torvalds free_page((unsigned long)page); 11311da177e4SLinus Torvalds return ret; 11321da177e4SLinus Torvalds } 11331da177e4SLinus Torvalds 11341da177e4SLinus Torvalds static ssize_t sel_write_bool(struct file *filep, const char __user *buf, 11351da177e4SLinus Torvalds size_t count, loff_t *ppos) 11361da177e4SLinus Torvalds { 11371da177e4SLinus Torvalds char *page = NULL; 1138d313f948SStephen Smalley ssize_t length; 11391da177e4SLinus Torvalds int new_value; 1140496ad9aaSAl Viro unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK; 1141d313f948SStephen Smalley const char *name = filep->f_path.dentry->d_name.name; 11421da177e4SLinus Torvalds 1143bb003079SIngo Molnar mutex_lock(&sel_mutex); 11441da177e4SLinus Torvalds 1145be0554c9SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 1146be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETBOOL, 1147be0554c9SStephen Smalley NULL); 11481da177e4SLinus Torvalds if (length) 11491da177e4SLinus Torvalds goto out; 11501da177e4SLinus Torvalds 1151d313f948SStephen Smalley length = -EINVAL; 1152b77a493bSEric Paris if (index >= bool_num || strcmp(name, bool_pending_names[index])) 1153d313f948SStephen Smalley goto out; 1154d313f948SStephen Smalley 11551da177e4SLinus Torvalds length = -ENOMEM; 1156b77a493bSEric Paris if (count >= PAGE_SIZE) 11571da177e4SLinus Torvalds goto out; 1158d313f948SStephen Smalley 11591da177e4SLinus Torvalds /* No partial writes. */ 1160d313f948SStephen Smalley length = -EINVAL; 1161b77a493bSEric Paris if (*ppos != 0) 11621da177e4SLinus Torvalds goto out; 1163b77a493bSEric Paris 11648365a719SAl Viro page = memdup_user_nul(buf, count); 11658365a719SAl Viro if (IS_ERR(page)) { 11668365a719SAl Viro length = PTR_ERR(page); 11678365a719SAl Viro page = NULL; 11681da177e4SLinus Torvalds goto out; 11698365a719SAl Viro } 11701da177e4SLinus Torvalds 11711da177e4SLinus Torvalds length = -EINVAL; 11721da177e4SLinus Torvalds if (sscanf(page, "%d", &new_value) != 1) 11731da177e4SLinus Torvalds goto out; 11741da177e4SLinus Torvalds 11751da177e4SLinus Torvalds if (new_value) 11761da177e4SLinus Torvalds new_value = 1; 11771da177e4SLinus Torvalds 1178d313f948SStephen Smalley bool_pending_values[index] = new_value; 11791da177e4SLinus Torvalds length = count; 11801da177e4SLinus Torvalds 11811da177e4SLinus Torvalds out: 1182bb003079SIngo Molnar mutex_unlock(&sel_mutex); 11838365a719SAl Viro kfree(page); 11841da177e4SLinus Torvalds return length; 11851da177e4SLinus Torvalds } 11861da177e4SLinus Torvalds 11879c2e08c5SArjan van de Ven static const struct file_operations sel_bool_ops = { 11881da177e4SLinus Torvalds .read = sel_read_bool, 11891da177e4SLinus Torvalds .write = sel_write_bool, 119057a62c23SArnd Bergmann .llseek = generic_file_llseek, 11911da177e4SLinus Torvalds }; 11921da177e4SLinus Torvalds 11931da177e4SLinus Torvalds static ssize_t sel_commit_bools_write(struct file *filep, 11941da177e4SLinus Torvalds const char __user *buf, 11951da177e4SLinus Torvalds size_t count, loff_t *ppos) 11961da177e4SLinus Torvalds { 11971da177e4SLinus Torvalds char *page = NULL; 1198d313f948SStephen Smalley ssize_t length; 11991da177e4SLinus Torvalds int new_value; 12001da177e4SLinus Torvalds 1201bb003079SIngo Molnar mutex_lock(&sel_mutex); 12021da177e4SLinus Torvalds 1203be0554c9SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 1204be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETBOOL, 1205be0554c9SStephen Smalley NULL); 12061da177e4SLinus Torvalds if (length) 12071da177e4SLinus Torvalds goto out; 12081da177e4SLinus Torvalds 12091da177e4SLinus Torvalds length = -ENOMEM; 1210b77a493bSEric Paris if (count >= PAGE_SIZE) 12111da177e4SLinus Torvalds goto out; 1212b77a493bSEric Paris 12131da177e4SLinus Torvalds /* No partial writes. */ 1214b77a493bSEric Paris length = -EINVAL; 1215b77a493bSEric Paris if (*ppos != 0) 12161da177e4SLinus Torvalds goto out; 1217b77a493bSEric Paris 12188365a719SAl Viro page = memdup_user_nul(buf, count); 12198365a719SAl Viro if (IS_ERR(page)) { 12208365a719SAl Viro length = PTR_ERR(page); 12218365a719SAl Viro page = NULL; 12221da177e4SLinus Torvalds goto out; 12238365a719SAl Viro } 12241da177e4SLinus Torvalds 12251da177e4SLinus Torvalds length = -EINVAL; 12261da177e4SLinus Torvalds if (sscanf(page, "%d", &new_value) != 1) 12271da177e4SLinus Torvalds goto out; 12281da177e4SLinus Torvalds 1229b77a493bSEric Paris length = 0; 12301872981bSEric Paris if (new_value && bool_pending_values) 1231b77a493bSEric Paris length = security_set_bools(bool_num, bool_pending_values); 12321da177e4SLinus Torvalds 1233b77a493bSEric Paris if (!length) 12341da177e4SLinus Torvalds length = count; 12351da177e4SLinus Torvalds 12361da177e4SLinus Torvalds out: 1237bb003079SIngo Molnar mutex_unlock(&sel_mutex); 12388365a719SAl Viro kfree(page); 12391da177e4SLinus Torvalds return length; 12401da177e4SLinus Torvalds } 12411da177e4SLinus Torvalds 12429c2e08c5SArjan van de Ven static const struct file_operations sel_commit_bools_ops = { 12431da177e4SLinus Torvalds .write = sel_commit_bools_write, 124457a62c23SArnd Bergmann .llseek = generic_file_llseek, 12451da177e4SLinus Torvalds }; 12461da177e4SLinus Torvalds 12470c92d7c7SChristopher J. PeBenito static void sel_remove_entries(struct dentry *de) 12481da177e4SLinus Torvalds { 1249ad52184bSAl Viro d_genocide(de); 1250ad52184bSAl Viro shrink_dcache_parent(de); 12511da177e4SLinus Torvalds } 12521da177e4SLinus Torvalds 12531da177e4SLinus Torvalds #define BOOL_DIR_NAME "booleans" 12541da177e4SLinus Torvalds 12551da177e4SLinus Torvalds static int sel_make_bools(void) 12561da177e4SLinus Torvalds { 1257b77a493bSEric Paris int i, ret; 12581da177e4SLinus Torvalds ssize_t len; 12591da177e4SLinus Torvalds struct dentry *dentry = NULL; 12601da177e4SLinus Torvalds struct dentry *dir = bool_dir; 12611da177e4SLinus Torvalds struct inode *inode = NULL; 12621da177e4SLinus Torvalds struct inode_security_struct *isec; 12631da177e4SLinus Torvalds char **names = NULL, *page; 12641da177e4SLinus Torvalds int num; 12651da177e4SLinus Torvalds int *values = NULL; 12661da177e4SLinus Torvalds u32 sid; 12671da177e4SLinus Torvalds 12681da177e4SLinus Torvalds /* remove any existing files */ 12698007f102SXiaotian Feng for (i = 0; i < bool_num; i++) 12708007f102SXiaotian Feng kfree(bool_pending_names[i]); 1271d313f948SStephen Smalley kfree(bool_pending_names); 12721da177e4SLinus Torvalds kfree(bool_pending_values); 1273154c50caSEric Paris bool_num = 0; 1274d313f948SStephen Smalley bool_pending_names = NULL; 127520c19e41SDavi Arnaut bool_pending_values = NULL; 12761da177e4SLinus Torvalds 12770c92d7c7SChristopher J. PeBenito sel_remove_entries(dir); 12781da177e4SLinus Torvalds 1279b77a493bSEric Paris ret = -ENOMEM; 12801872981bSEric Paris page = (char *)get_zeroed_page(GFP_KERNEL); 12811872981bSEric Paris if (!page) 1282b77a493bSEric Paris goto out; 12831da177e4SLinus Torvalds 12841da177e4SLinus Torvalds ret = security_get_bools(&num, &names, &values); 1285b77a493bSEric Paris if (ret) 12861da177e4SLinus Torvalds goto out; 12871da177e4SLinus Torvalds 12881da177e4SLinus Torvalds for (i = 0; i < num; i++) { 1289b77a493bSEric Paris ret = -ENOMEM; 12901da177e4SLinus Torvalds dentry = d_alloc_name(dir, names[i]); 1291b77a493bSEric Paris if (!dentry) 1292b77a493bSEric Paris goto out; 12931da177e4SLinus Torvalds 1294b77a493bSEric Paris ret = -ENOMEM; 1295b77a493bSEric Paris inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR); 1296b77a493bSEric Paris if (!inode) 1297b77a493bSEric Paris goto out; 1298b77a493bSEric Paris 12991da177e4SLinus Torvalds ret = -ENAMETOOLONG; 1300cc1dad71SAl Viro len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]); 1301b77a493bSEric Paris if (len >= PAGE_SIZE) 1302b77a493bSEric Paris goto out; 1303b77a493bSEric Paris 13041da177e4SLinus Torvalds isec = (struct inode_security_struct *)inode->i_security; 13051872981bSEric Paris ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid); 13064262fb51SGary Tierney if (ret) { 1307900fde06SGary Tierney pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n", 1308900fde06SGary Tierney page); 1309900fde06SGary Tierney sid = SECINITSID_SECURITY; 13104262fb51SGary Tierney } 13114262fb51SGary Tierney 13121da177e4SLinus Torvalds isec->sid = sid; 131342059112SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 13141da177e4SLinus Torvalds inode->i_fop = &sel_bool_ops; 1315bce34bc0SJames Carter inode->i_ino = i|SEL_BOOL_INO_OFFSET; 13161da177e4SLinus Torvalds d_add(dentry, inode); 13171da177e4SLinus Torvalds } 13181da177e4SLinus Torvalds bool_num = num; 1319d313f948SStephen Smalley bool_pending_names = names; 13201da177e4SLinus Torvalds bool_pending_values = values; 1321b77a493bSEric Paris 1322b77a493bSEric Paris free_page((unsigned long)page); 1323b77a493bSEric Paris return 0; 13241da177e4SLinus Torvalds out: 13251da177e4SLinus Torvalds free_page((unsigned long)page); 1326b77a493bSEric Paris 13271da177e4SLinus Torvalds if (names) { 13289a5f04bfSJesper Juhl for (i = 0; i < num; i++) 13291da177e4SLinus Torvalds kfree(names[i]); 13301da177e4SLinus Torvalds kfree(names); 13311da177e4SLinus Torvalds } 133220c19e41SDavi Arnaut kfree(values); 13330c92d7c7SChristopher J. PeBenito sel_remove_entries(dir); 1334b77a493bSEric Paris 1335b77a493bSEric Paris return ret; 13361da177e4SLinus Torvalds } 13371da177e4SLinus Torvalds 13381da177e4SLinus Torvalds #define NULL_FILE_NAME "null" 13391da177e4SLinus Torvalds 1340765927b2SAl Viro struct path selinux_null; 13411da177e4SLinus Torvalds 13421da177e4SLinus Torvalds static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf, 13431da177e4SLinus Torvalds size_t count, loff_t *ppos) 13441da177e4SLinus Torvalds { 13451da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 13461da177e4SLinus Torvalds ssize_t length; 13471da177e4SLinus Torvalds 13481da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold); 13491da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 13501da177e4SLinus Torvalds } 13511da177e4SLinus Torvalds 13521da177e4SLinus Torvalds static ssize_t sel_write_avc_cache_threshold(struct file *file, 13531da177e4SLinus Torvalds const char __user *buf, 13541da177e4SLinus Torvalds size_t count, loff_t *ppos) 13551da177e4SLinus Torvalds 13561da177e4SLinus Torvalds { 13578365a719SAl Viro char *page; 13581da177e4SLinus Torvalds ssize_t ret; 1359309c5fadSHeinrich Schuchardt unsigned int new_value; 13601da177e4SLinus Torvalds 1361be0554c9SStephen Smalley ret = avc_has_perm(current_sid(), SECINITSID_SECURITY, 1362be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETSECPARAM, 1363be0554c9SStephen Smalley NULL); 13641da177e4SLinus Torvalds if (ret) 13658365a719SAl Viro return ret; 1366b77a493bSEric Paris 1367b77a493bSEric Paris if (count >= PAGE_SIZE) 13688365a719SAl Viro return -ENOMEM; 1369b77a493bSEric Paris 1370b77a493bSEric Paris /* No partial writes. */ 1371b77a493bSEric Paris if (*ppos != 0) 13728365a719SAl Viro return -EINVAL; 1373b77a493bSEric Paris 13748365a719SAl Viro page = memdup_user_nul(buf, count); 13758365a719SAl Viro if (IS_ERR(page)) 13768365a719SAl Viro return PTR_ERR(page); 1377b77a493bSEric Paris 1378b77a493bSEric Paris ret = -EINVAL; 1379b77a493bSEric Paris if (sscanf(page, "%u", &new_value) != 1) 1380b77a493bSEric Paris goto out; 1381b77a493bSEric Paris 13821da177e4SLinus Torvalds avc_cache_threshold = new_value; 1383b77a493bSEric Paris 13841da177e4SLinus Torvalds ret = count; 13851da177e4SLinus Torvalds out: 13868365a719SAl Viro kfree(page); 13871da177e4SLinus Torvalds return ret; 13881da177e4SLinus Torvalds } 13891da177e4SLinus Torvalds 13901da177e4SLinus Torvalds static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf, 13911da177e4SLinus Torvalds size_t count, loff_t *ppos) 13921da177e4SLinus Torvalds { 13931da177e4SLinus Torvalds char *page; 1394b77a493bSEric Paris ssize_t length; 13951da177e4SLinus Torvalds 13961da177e4SLinus Torvalds page = (char *)__get_free_page(GFP_KERNEL); 1397b77a493bSEric Paris if (!page) 1398b77a493bSEric Paris return -ENOMEM; 1399b77a493bSEric Paris 1400b77a493bSEric Paris length = avc_get_hash_stats(page); 1401b77a493bSEric Paris if (length >= 0) 1402b77a493bSEric Paris length = simple_read_from_buffer(buf, count, ppos, page, length); 14031da177e4SLinus Torvalds free_page((unsigned long)page); 1404b77a493bSEric Paris 1405b77a493bSEric Paris return length; 14061da177e4SLinus Torvalds } 14071da177e4SLinus Torvalds 14089c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_threshold_ops = { 14091da177e4SLinus Torvalds .read = sel_read_avc_cache_threshold, 14101da177e4SLinus Torvalds .write = sel_write_avc_cache_threshold, 141157a62c23SArnd Bergmann .llseek = generic_file_llseek, 14121da177e4SLinus Torvalds }; 14131da177e4SLinus Torvalds 14149c2e08c5SArjan van de Ven static const struct file_operations sel_avc_hash_stats_ops = { 14151da177e4SLinus Torvalds .read = sel_read_avc_hash_stats, 141657a62c23SArnd Bergmann .llseek = generic_file_llseek, 14171da177e4SLinus Torvalds }; 14181da177e4SLinus Torvalds 14191da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS 14201da177e4SLinus Torvalds static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx) 14211da177e4SLinus Torvalds { 14221da177e4SLinus Torvalds int cpu; 14231da177e4SLinus Torvalds 14244f4b6c1aSRusty Russell for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) { 14251da177e4SLinus Torvalds if (!cpu_possible(cpu)) 14261da177e4SLinus Torvalds continue; 14271da177e4SLinus Torvalds *idx = cpu + 1; 14281da177e4SLinus Torvalds return &per_cpu(avc_cache_stats, cpu); 14291da177e4SLinus Torvalds } 14301da177e4SLinus Torvalds return NULL; 14311da177e4SLinus Torvalds } 14321da177e4SLinus Torvalds 14331da177e4SLinus Torvalds static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos) 14341da177e4SLinus Torvalds { 14351da177e4SLinus Torvalds loff_t n = *pos - 1; 14361da177e4SLinus Torvalds 14371da177e4SLinus Torvalds if (*pos == 0) 14381da177e4SLinus Torvalds return SEQ_START_TOKEN; 14391da177e4SLinus Torvalds 14401da177e4SLinus Torvalds return sel_avc_get_stat_idx(&n); 14411da177e4SLinus Torvalds } 14421da177e4SLinus Torvalds 14431da177e4SLinus Torvalds static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos) 14441da177e4SLinus Torvalds { 14451da177e4SLinus Torvalds return sel_avc_get_stat_idx(pos); 14461da177e4SLinus Torvalds } 14471da177e4SLinus Torvalds 14481da177e4SLinus Torvalds static int sel_avc_stats_seq_show(struct seq_file *seq, void *v) 14491da177e4SLinus Torvalds { 14501da177e4SLinus Torvalds struct avc_cache_stats *st = v; 14511da177e4SLinus Torvalds 1452710a0647SMarkus Elfring if (v == SEQ_START_TOKEN) { 1453710a0647SMarkus Elfring seq_puts(seq, 1454710a0647SMarkus Elfring "lookups hits misses allocations reclaims frees\n"); 1455710a0647SMarkus Elfring } else { 1456257313b2SLinus Torvalds unsigned int lookups = st->lookups; 1457257313b2SLinus Torvalds unsigned int misses = st->misses; 1458257313b2SLinus Torvalds unsigned int hits = lookups - misses; 1459257313b2SLinus Torvalds seq_printf(seq, "%u %u %u %u %u %u\n", lookups, 1460257313b2SLinus Torvalds hits, misses, st->allocations, 14611da177e4SLinus Torvalds st->reclaims, st->frees); 1462257313b2SLinus Torvalds } 14631da177e4SLinus Torvalds return 0; 14641da177e4SLinus Torvalds } 14651da177e4SLinus Torvalds 14661da177e4SLinus Torvalds static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v) 14671da177e4SLinus Torvalds { } 14681da177e4SLinus Torvalds 14691996a109SJan Engelhardt static const struct seq_operations sel_avc_cache_stats_seq_ops = { 14701da177e4SLinus Torvalds .start = sel_avc_stats_seq_start, 14711da177e4SLinus Torvalds .next = sel_avc_stats_seq_next, 14721da177e4SLinus Torvalds .show = sel_avc_stats_seq_show, 14731da177e4SLinus Torvalds .stop = sel_avc_stats_seq_stop, 14741da177e4SLinus Torvalds }; 14751da177e4SLinus Torvalds 14761da177e4SLinus Torvalds static int sel_open_avc_cache_stats(struct inode *inode, struct file *file) 14771da177e4SLinus Torvalds { 14781da177e4SLinus Torvalds return seq_open(file, &sel_avc_cache_stats_seq_ops); 14791da177e4SLinus Torvalds } 14801da177e4SLinus Torvalds 14819c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_stats_ops = { 14821da177e4SLinus Torvalds .open = sel_open_avc_cache_stats, 14831da177e4SLinus Torvalds .read = seq_read, 14841da177e4SLinus Torvalds .llseek = seq_lseek, 14851da177e4SLinus Torvalds .release = seq_release, 14861da177e4SLinus Torvalds }; 14871da177e4SLinus Torvalds #endif 14881da177e4SLinus Torvalds 14891da177e4SLinus Torvalds static int sel_make_avc_files(struct dentry *dir) 14901da177e4SLinus Torvalds { 1491b77a493bSEric Paris int i; 1492cda37124SEric Biggers static const struct tree_descr files[] = { 14931da177e4SLinus Torvalds { "cache_threshold", 14941da177e4SLinus Torvalds &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR }, 14951da177e4SLinus Torvalds { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO }, 14961da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS 14971da177e4SLinus Torvalds { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO }, 14981da177e4SLinus Torvalds #endif 14991da177e4SLinus Torvalds }; 15001da177e4SLinus Torvalds 15016e20a64aSNicolas Kaiser for (i = 0; i < ARRAY_SIZE(files); i++) { 15021da177e4SLinus Torvalds struct inode *inode; 15031da177e4SLinus Torvalds struct dentry *dentry; 15041da177e4SLinus Torvalds 15051da177e4SLinus Torvalds dentry = d_alloc_name(dir, files[i].name); 1506b77a493bSEric Paris if (!dentry) 1507b77a493bSEric Paris return -ENOMEM; 15081da177e4SLinus Torvalds 15091da177e4SLinus Torvalds inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode); 1510b77a493bSEric Paris if (!inode) 1511b77a493bSEric Paris return -ENOMEM; 1512b77a493bSEric Paris 15131da177e4SLinus Torvalds inode->i_fop = files[i].ops; 15146174eafcSJames Carter inode->i_ino = ++sel_last_ino; 15151da177e4SLinus Torvalds d_add(dentry, inode); 15161da177e4SLinus Torvalds } 1517b77a493bSEric Paris 1518b77a493bSEric Paris return 0; 15191da177e4SLinus Torvalds } 15201da177e4SLinus Torvalds 1521f0ee2e46SJames Carter static ssize_t sel_read_initcon(struct file *file, char __user *buf, 1522f0ee2e46SJames Carter size_t count, loff_t *ppos) 1523f0ee2e46SJames Carter { 1524f0ee2e46SJames Carter char *con; 1525f0ee2e46SJames Carter u32 sid, len; 1526f0ee2e46SJames Carter ssize_t ret; 1527f0ee2e46SJames Carter 1528496ad9aaSAl Viro sid = file_inode(file)->i_ino&SEL_INO_MASK; 1529f0ee2e46SJames Carter ret = security_sid_to_context(sid, &con, &len); 1530b77a493bSEric Paris if (ret) 1531f0ee2e46SJames Carter return ret; 1532f0ee2e46SJames Carter 1533f0ee2e46SJames Carter ret = simple_read_from_buffer(buf, count, ppos, con, len); 1534f0ee2e46SJames Carter kfree(con); 1535f0ee2e46SJames Carter return ret; 1536f0ee2e46SJames Carter } 1537f0ee2e46SJames Carter 1538f0ee2e46SJames Carter static const struct file_operations sel_initcon_ops = { 1539f0ee2e46SJames Carter .read = sel_read_initcon, 154057a62c23SArnd Bergmann .llseek = generic_file_llseek, 1541f0ee2e46SJames Carter }; 1542f0ee2e46SJames Carter 1543f0ee2e46SJames Carter static int sel_make_initcon_files(struct dentry *dir) 1544f0ee2e46SJames Carter { 1545b77a493bSEric Paris int i; 1546f0ee2e46SJames Carter 1547f0ee2e46SJames Carter for (i = 1; i <= SECINITSID_NUM; i++) { 1548f0ee2e46SJames Carter struct inode *inode; 1549f0ee2e46SJames Carter struct dentry *dentry; 1550f0ee2e46SJames Carter dentry = d_alloc_name(dir, security_get_initial_sid_context(i)); 1551b77a493bSEric Paris if (!dentry) 1552b77a493bSEric Paris return -ENOMEM; 1553f0ee2e46SJames Carter 1554f0ee2e46SJames Carter inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); 1555b77a493bSEric Paris if (!inode) 1556b77a493bSEric Paris return -ENOMEM; 1557b77a493bSEric Paris 1558f0ee2e46SJames Carter inode->i_fop = &sel_initcon_ops; 1559f0ee2e46SJames Carter inode->i_ino = i|SEL_INITCON_INO_OFFSET; 1560f0ee2e46SJames Carter d_add(dentry, inode); 1561f0ee2e46SJames Carter } 1562b77a493bSEric Paris 1563b77a493bSEric Paris return 0; 1564f0ee2e46SJames Carter } 1565f0ee2e46SJames Carter 1566e47c8fc5SChristopher J. PeBenito static inline unsigned long sel_class_to_ino(u16 class) 1567e47c8fc5SChristopher J. PeBenito { 1568e47c8fc5SChristopher J. PeBenito return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET; 1569e47c8fc5SChristopher J. PeBenito } 1570e47c8fc5SChristopher J. PeBenito 1571e47c8fc5SChristopher J. PeBenito static inline u16 sel_ino_to_class(unsigned long ino) 1572e47c8fc5SChristopher J. PeBenito { 157392ae9e82SEric Paris return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1); 1574e47c8fc5SChristopher J. PeBenito } 1575e47c8fc5SChristopher J. PeBenito 1576e47c8fc5SChristopher J. PeBenito static inline unsigned long sel_perm_to_ino(u16 class, u32 perm) 1577e47c8fc5SChristopher J. PeBenito { 1578e47c8fc5SChristopher J. PeBenito return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET; 1579e47c8fc5SChristopher J. PeBenito } 1580e47c8fc5SChristopher J. PeBenito 1581e47c8fc5SChristopher J. PeBenito static inline u32 sel_ino_to_perm(unsigned long ino) 1582e47c8fc5SChristopher J. PeBenito { 1583e47c8fc5SChristopher J. PeBenito return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1); 1584e47c8fc5SChristopher J. PeBenito } 1585e47c8fc5SChristopher J. PeBenito 1586e47c8fc5SChristopher J. PeBenito static ssize_t sel_read_class(struct file *file, char __user *buf, 1587e47c8fc5SChristopher J. PeBenito size_t count, loff_t *ppos) 1588e47c8fc5SChristopher J. PeBenito { 1589496ad9aaSAl Viro unsigned long ino = file_inode(file)->i_ino; 1590cc1dad71SAl Viro char res[TMPBUFLEN]; 1591cc1dad71SAl Viro ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_class(ino)); 1592cc1dad71SAl Viro return simple_read_from_buffer(buf, count, ppos, res, len); 1593e47c8fc5SChristopher J. PeBenito } 1594e47c8fc5SChristopher J. PeBenito 1595e47c8fc5SChristopher J. PeBenito static const struct file_operations sel_class_ops = { 1596e47c8fc5SChristopher J. PeBenito .read = sel_read_class, 159757a62c23SArnd Bergmann .llseek = generic_file_llseek, 1598e47c8fc5SChristopher J. PeBenito }; 1599e47c8fc5SChristopher J. PeBenito 1600e47c8fc5SChristopher J. PeBenito static ssize_t sel_read_perm(struct file *file, char __user *buf, 1601e47c8fc5SChristopher J. PeBenito size_t count, loff_t *ppos) 1602e47c8fc5SChristopher J. PeBenito { 1603496ad9aaSAl Viro unsigned long ino = file_inode(file)->i_ino; 1604cc1dad71SAl Viro char res[TMPBUFLEN]; 1605cc1dad71SAl Viro ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino)); 1606cc1dad71SAl Viro return simple_read_from_buffer(buf, count, ppos, res, len); 1607e47c8fc5SChristopher J. PeBenito } 1608e47c8fc5SChristopher J. PeBenito 1609e47c8fc5SChristopher J. PeBenito static const struct file_operations sel_perm_ops = { 1610e47c8fc5SChristopher J. PeBenito .read = sel_read_perm, 161157a62c23SArnd Bergmann .llseek = generic_file_llseek, 1612e47c8fc5SChristopher J. PeBenito }; 1613e47c8fc5SChristopher J. PeBenito 16143bb56b25SPaul Moore static ssize_t sel_read_policycap(struct file *file, char __user *buf, 16153bb56b25SPaul Moore size_t count, loff_t *ppos) 16163bb56b25SPaul Moore { 16173bb56b25SPaul Moore int value; 16183bb56b25SPaul Moore char tmpbuf[TMPBUFLEN]; 16193bb56b25SPaul Moore ssize_t length; 1620496ad9aaSAl Viro unsigned long i_ino = file_inode(file)->i_ino; 16213bb56b25SPaul Moore 16223bb56b25SPaul Moore value = security_policycap_supported(i_ino & SEL_INO_MASK); 16233bb56b25SPaul Moore length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value); 16243bb56b25SPaul Moore 16253bb56b25SPaul Moore return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 16263bb56b25SPaul Moore } 16273bb56b25SPaul Moore 16283bb56b25SPaul Moore static const struct file_operations sel_policycap_ops = { 16293bb56b25SPaul Moore .read = sel_read_policycap, 163057a62c23SArnd Bergmann .llseek = generic_file_llseek, 16313bb56b25SPaul Moore }; 16323bb56b25SPaul Moore 1633e47c8fc5SChristopher J. PeBenito static int sel_make_perm_files(char *objclass, int classvalue, 1634e47c8fc5SChristopher J. PeBenito struct dentry *dir) 1635e47c8fc5SChristopher J. PeBenito { 1636b77a493bSEric Paris int i, rc, nperms; 1637e47c8fc5SChristopher J. PeBenito char **perms; 1638e47c8fc5SChristopher J. PeBenito 1639e47c8fc5SChristopher J. PeBenito rc = security_get_permissions(objclass, &perms, &nperms); 1640e47c8fc5SChristopher J. PeBenito if (rc) 1641b77a493bSEric Paris return rc; 1642e47c8fc5SChristopher J. PeBenito 1643e47c8fc5SChristopher J. PeBenito for (i = 0; i < nperms; i++) { 1644e47c8fc5SChristopher J. PeBenito struct inode *inode; 1645e47c8fc5SChristopher J. PeBenito struct dentry *dentry; 1646e47c8fc5SChristopher J. PeBenito 1647b77a493bSEric Paris rc = -ENOMEM; 1648e47c8fc5SChristopher J. PeBenito dentry = d_alloc_name(dir, perms[i]); 1649b77a493bSEric Paris if (!dentry) 1650b77a493bSEric Paris goto out; 1651e47c8fc5SChristopher J. PeBenito 1652e47c8fc5SChristopher J. PeBenito rc = -ENOMEM; 1653b77a493bSEric Paris inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); 1654b77a493bSEric Paris if (!inode) 1655b77a493bSEric Paris goto out; 1656b77a493bSEric Paris 1657e47c8fc5SChristopher J. PeBenito inode->i_fop = &sel_perm_ops; 1658e47c8fc5SChristopher J. PeBenito /* i+1 since perm values are 1-indexed */ 1659e47c8fc5SChristopher J. PeBenito inode->i_ino = sel_perm_to_ino(classvalue, i + 1); 1660e47c8fc5SChristopher J. PeBenito d_add(dentry, inode); 1661e47c8fc5SChristopher J. PeBenito } 1662b77a493bSEric Paris rc = 0; 1663b77a493bSEric Paris out: 1664e47c8fc5SChristopher J. PeBenito for (i = 0; i < nperms; i++) 1665e47c8fc5SChristopher J. PeBenito kfree(perms[i]); 1666e47c8fc5SChristopher J. PeBenito kfree(perms); 1667e47c8fc5SChristopher J. PeBenito return rc; 1668e47c8fc5SChristopher J. PeBenito } 1669e47c8fc5SChristopher J. PeBenito 1670e47c8fc5SChristopher J. PeBenito static int sel_make_class_dir_entries(char *classname, int index, 1671e47c8fc5SChristopher J. PeBenito struct dentry *dir) 1672e47c8fc5SChristopher J. PeBenito { 1673e47c8fc5SChristopher J. PeBenito struct dentry *dentry = NULL; 1674e47c8fc5SChristopher J. PeBenito struct inode *inode = NULL; 1675e47c8fc5SChristopher J. PeBenito int rc; 1676e47c8fc5SChristopher J. PeBenito 1677e47c8fc5SChristopher J. PeBenito dentry = d_alloc_name(dir, "index"); 1678b77a493bSEric Paris if (!dentry) 1679b77a493bSEric Paris return -ENOMEM; 1680e47c8fc5SChristopher J. PeBenito 1681e47c8fc5SChristopher J. PeBenito inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); 1682b77a493bSEric Paris if (!inode) 1683b77a493bSEric Paris return -ENOMEM; 1684e47c8fc5SChristopher J. PeBenito 1685e47c8fc5SChristopher J. PeBenito inode->i_fop = &sel_class_ops; 1686e47c8fc5SChristopher J. PeBenito inode->i_ino = sel_class_to_ino(index); 1687e47c8fc5SChristopher J. PeBenito d_add(dentry, inode); 1688e47c8fc5SChristopher J. PeBenito 1689a1c2aa1eSAl Viro dentry = sel_make_dir(dir, "perms", &last_class_ino); 1690a1c2aa1eSAl Viro if (IS_ERR(dentry)) 1691a1c2aa1eSAl Viro return PTR_ERR(dentry); 1692e47c8fc5SChristopher J. PeBenito 1693e47c8fc5SChristopher J. PeBenito rc = sel_make_perm_files(classname, index, dentry); 1694e47c8fc5SChristopher J. PeBenito 1695e47c8fc5SChristopher J. PeBenito return rc; 1696e47c8fc5SChristopher J. PeBenito } 1697e47c8fc5SChristopher J. PeBenito 1698e47c8fc5SChristopher J. PeBenito static int sel_make_classes(void) 1699e47c8fc5SChristopher J. PeBenito { 1700b77a493bSEric Paris int rc, nclasses, i; 1701e47c8fc5SChristopher J. PeBenito char **classes; 1702e47c8fc5SChristopher J. PeBenito 1703e47c8fc5SChristopher J. PeBenito /* delete any existing entries */ 1704ad52184bSAl Viro sel_remove_entries(class_dir); 1705e47c8fc5SChristopher J. PeBenito 1706e47c8fc5SChristopher J. PeBenito rc = security_get_classes(&classes, &nclasses); 1707b77a493bSEric Paris if (rc) 1708b77a493bSEric Paris return rc; 1709e47c8fc5SChristopher J. PeBenito 1710e47c8fc5SChristopher J. PeBenito /* +2 since classes are 1-indexed */ 1711e47c8fc5SChristopher J. PeBenito last_class_ino = sel_class_to_ino(nclasses + 2); 1712e47c8fc5SChristopher J. PeBenito 1713e47c8fc5SChristopher J. PeBenito for (i = 0; i < nclasses; i++) { 1714e47c8fc5SChristopher J. PeBenito struct dentry *class_name_dir; 1715e47c8fc5SChristopher J. PeBenito 1716a1c2aa1eSAl Viro class_name_dir = sel_make_dir(class_dir, classes[i], 1717e47c8fc5SChristopher J. PeBenito &last_class_ino); 1718a1c2aa1eSAl Viro if (IS_ERR(class_name_dir)) { 1719a1c2aa1eSAl Viro rc = PTR_ERR(class_name_dir); 1720b77a493bSEric Paris goto out; 1721a1c2aa1eSAl Viro } 1722e47c8fc5SChristopher J. PeBenito 1723e47c8fc5SChristopher J. PeBenito /* i+1 since class values are 1-indexed */ 1724e47c8fc5SChristopher J. PeBenito rc = sel_make_class_dir_entries(classes[i], i + 1, 1725e47c8fc5SChristopher J. PeBenito class_name_dir); 1726e47c8fc5SChristopher J. PeBenito if (rc) 1727b77a493bSEric Paris goto out; 1728e47c8fc5SChristopher J. PeBenito } 1729b77a493bSEric Paris rc = 0; 1730b77a493bSEric Paris out: 1731e47c8fc5SChristopher J. PeBenito for (i = 0; i < nclasses; i++) 1732e47c8fc5SChristopher J. PeBenito kfree(classes[i]); 1733e47c8fc5SChristopher J. PeBenito kfree(classes); 1734e47c8fc5SChristopher J. PeBenito return rc; 1735e47c8fc5SChristopher J. PeBenito } 1736e47c8fc5SChristopher J. PeBenito 17373bb56b25SPaul Moore static int sel_make_policycap(void) 17383bb56b25SPaul Moore { 17393bb56b25SPaul Moore unsigned int iter; 17403bb56b25SPaul Moore struct dentry *dentry = NULL; 17413bb56b25SPaul Moore struct inode *inode = NULL; 17423bb56b25SPaul Moore 17433bb56b25SPaul Moore sel_remove_entries(policycap_dir); 17443bb56b25SPaul Moore 17453bb56b25SPaul Moore for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) { 17464dc2fce3SStephen Smalley if (iter < ARRAY_SIZE(selinux_policycap_names)) 17473bb56b25SPaul Moore dentry = d_alloc_name(policycap_dir, 17484dc2fce3SStephen Smalley selinux_policycap_names[iter]); 17493bb56b25SPaul Moore else 17503bb56b25SPaul Moore dentry = d_alloc_name(policycap_dir, "unknown"); 17513bb56b25SPaul Moore 17523bb56b25SPaul Moore if (dentry == NULL) 17533bb56b25SPaul Moore return -ENOMEM; 17543bb56b25SPaul Moore 17553bb56b25SPaul Moore inode = sel_make_inode(policycap_dir->d_sb, S_IFREG | S_IRUGO); 17563bb56b25SPaul Moore if (inode == NULL) 17573bb56b25SPaul Moore return -ENOMEM; 17583bb56b25SPaul Moore 17593bb56b25SPaul Moore inode->i_fop = &sel_policycap_ops; 17603bb56b25SPaul Moore inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET; 17613bb56b25SPaul Moore d_add(dentry, inode); 17623bb56b25SPaul Moore } 17633bb56b25SPaul Moore 17643bb56b25SPaul Moore return 0; 17653bb56b25SPaul Moore } 17663bb56b25SPaul Moore 1767a1c2aa1eSAl Viro static struct dentry *sel_make_dir(struct dentry *dir, const char *name, 17680dd4ae51SChristopher J. PeBenito unsigned long *ino) 17691da177e4SLinus Torvalds { 1770a1c2aa1eSAl Viro struct dentry *dentry = d_alloc_name(dir, name); 17711da177e4SLinus Torvalds struct inode *inode; 17721da177e4SLinus Torvalds 1773a1c2aa1eSAl Viro if (!dentry) 1774a1c2aa1eSAl Viro return ERR_PTR(-ENOMEM); 1775a1c2aa1eSAl Viro 1776a1c2aa1eSAl Viro inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO); 1777a1c2aa1eSAl Viro if (!inode) { 1778a1c2aa1eSAl Viro dput(dentry); 1779a1c2aa1eSAl Viro return ERR_PTR(-ENOMEM); 1780a1c2aa1eSAl Viro } 1781b77a493bSEric Paris 17821da177e4SLinus Torvalds inode->i_op = &simple_dir_inode_operations; 17831da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations; 17840dd4ae51SChristopher J. PeBenito inode->i_ino = ++(*ino); 178540e906f8SJames Morris /* directory inodes start off with i_nlink == 2 (for "." entry) */ 1786d8c76e6fSDave Hansen inc_nlink(inode); 17871da177e4SLinus Torvalds d_add(dentry, inode); 1788edb20fb5SJames Morris /* bump link count on parent directory, too */ 1789ce0b16ddSDavid Howells inc_nlink(d_inode(dir)); 1790b77a493bSEric Paris 1791a1c2aa1eSAl Viro return dentry; 17921da177e4SLinus Torvalds } 17931da177e4SLinus Torvalds 17941da177e4SLinus Torvalds static int sel_fill_super(struct super_block *sb, void *data, int silent) 17951da177e4SLinus Torvalds { 17961da177e4SLinus Torvalds int ret; 17971da177e4SLinus Torvalds struct dentry *dentry; 1798a1c2aa1eSAl Viro struct inode *inode; 17991da177e4SLinus Torvalds struct inode_security_struct *isec; 18001da177e4SLinus Torvalds 1801cda37124SEric Biggers static const struct tree_descr selinux_files[] = { 18021da177e4SLinus Torvalds [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR}, 18031da177e4SLinus Torvalds [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR}, 1804ce9982d0SStephen Smalley [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO}, 18051da177e4SLinus Torvalds [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO}, 18061da177e4SLinus Torvalds [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO}, 18071da177e4SLinus Torvalds [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO}, 18081da177e4SLinus Torvalds [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO}, 18091da177e4SLinus Torvalds [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO}, 18101da177e4SLinus Torvalds [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR}, 18111da177e4SLinus Torvalds [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO}, 18121da177e4SLinus Torvalds [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR}, 18131da177e4SLinus Torvalds [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO}, 18141da177e4SLinus Torvalds [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR}, 18153f12070eSEric Paris [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO}, 18163f12070eSEric Paris [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO}, 181711904167SKaiGai Kohei [SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO}, 181872e8c859SEric Paris [SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO}, 1819f9df6458SAndrew Perepechko [SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops, 1820f9df6458SAndrew Perepechko S_IWUGO}, 18211da177e4SLinus Torvalds /* last one */ {""} 18221da177e4SLinus Torvalds }; 18231da177e4SLinus Torvalds ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files); 18241da177e4SLinus Torvalds if (ret) 1825161ce45aSJames Morris goto err; 18261da177e4SLinus Torvalds 1827a1c2aa1eSAl Viro bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &sel_last_ino); 1828a1c2aa1eSAl Viro if (IS_ERR(bool_dir)) { 1829a1c2aa1eSAl Viro ret = PTR_ERR(bool_dir); 1830a1c2aa1eSAl Viro bool_dir = NULL; 1831161ce45aSJames Morris goto err; 1832a1c2aa1eSAl Viro } 18331da177e4SLinus Torvalds 1834b77a493bSEric Paris ret = -ENOMEM; 18351da177e4SLinus Torvalds dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME); 1836b77a493bSEric Paris if (!dentry) 1837161ce45aSJames Morris goto err; 18381da177e4SLinus Torvalds 1839161ce45aSJames Morris ret = -ENOMEM; 1840b77a493bSEric Paris inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO); 1841b77a493bSEric Paris if (!inode) 1842161ce45aSJames Morris goto err; 1843b77a493bSEric Paris 18446174eafcSJames Carter inode->i_ino = ++sel_last_ino; 18451da177e4SLinus Torvalds isec = (struct inode_security_struct *)inode->i_security; 18461da177e4SLinus Torvalds isec->sid = SECINITSID_DEVNULL; 18471da177e4SLinus Torvalds isec->sclass = SECCLASS_CHR_FILE; 184842059112SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 18491da177e4SLinus Torvalds 18501da177e4SLinus Torvalds init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3)); 18511da177e4SLinus Torvalds d_add(dentry, inode); 1852765927b2SAl Viro selinux_null.dentry = dentry; 18531da177e4SLinus Torvalds 1854a1c2aa1eSAl Viro dentry = sel_make_dir(sb->s_root, "avc", &sel_last_ino); 1855a1c2aa1eSAl Viro if (IS_ERR(dentry)) { 1856a1c2aa1eSAl Viro ret = PTR_ERR(dentry); 1857161ce45aSJames Morris goto err; 1858a1c2aa1eSAl Viro } 18591da177e4SLinus Torvalds 18601da177e4SLinus Torvalds ret = sel_make_avc_files(dentry); 18611da177e4SLinus Torvalds if (ret) 1862161ce45aSJames Morris goto err; 1863f0ee2e46SJames Carter 1864a1c2aa1eSAl Viro dentry = sel_make_dir(sb->s_root, "initial_contexts", &sel_last_ino); 1865a1c2aa1eSAl Viro if (IS_ERR(dentry)) { 1866a1c2aa1eSAl Viro ret = PTR_ERR(dentry); 1867f0ee2e46SJames Carter goto err; 1868a1c2aa1eSAl Viro } 1869f0ee2e46SJames Carter 1870f0ee2e46SJames Carter ret = sel_make_initcon_files(dentry); 1871f0ee2e46SJames Carter if (ret) 1872f0ee2e46SJames Carter goto err; 1873f0ee2e46SJames Carter 1874a1c2aa1eSAl Viro class_dir = sel_make_dir(sb->s_root, "class", &sel_last_ino); 1875a1c2aa1eSAl Viro if (IS_ERR(class_dir)) { 1876a1c2aa1eSAl Viro ret = PTR_ERR(class_dir); 1877a1c2aa1eSAl Viro class_dir = NULL; 1878e47c8fc5SChristopher J. PeBenito goto err; 1879a1c2aa1eSAl Viro } 1880e47c8fc5SChristopher J. PeBenito 1881a1c2aa1eSAl Viro policycap_dir = sel_make_dir(sb->s_root, "policy_capabilities", &sel_last_ino); 1882a1c2aa1eSAl Viro if (IS_ERR(policycap_dir)) { 1883a1c2aa1eSAl Viro ret = PTR_ERR(policycap_dir); 1884a1c2aa1eSAl Viro policycap_dir = NULL; 1885e47c8fc5SChristopher J. PeBenito goto err; 1886a1c2aa1eSAl Viro } 1887b77a493bSEric Paris return 0; 1888161ce45aSJames Morris err: 1889744ba35eSEric Paris printk(KERN_ERR "SELinux: %s: failed while creating inodes\n", 1890744ba35eSEric Paris __func__); 1891b77a493bSEric Paris return ret; 18921da177e4SLinus Torvalds } 18931da177e4SLinus Torvalds 1894fc14f2feSAl Viro static struct dentry *sel_mount(struct file_system_type *fs_type, 1895fc14f2feSAl Viro int flags, const char *dev_name, void *data) 18961da177e4SLinus Torvalds { 1897fc14f2feSAl Viro return mount_single(fs_type, flags, data, sel_fill_super); 18981da177e4SLinus Torvalds } 18991da177e4SLinus Torvalds 19001da177e4SLinus Torvalds static struct file_system_type sel_fs_type = { 19011da177e4SLinus Torvalds .name = "selinuxfs", 1902fc14f2feSAl Viro .mount = sel_mount, 19031da177e4SLinus Torvalds .kill_sb = kill_litter_super, 19041da177e4SLinus Torvalds }; 19051da177e4SLinus Torvalds 19061da177e4SLinus Torvalds struct vfsmount *selinuxfs_mount; 19071da177e4SLinus Torvalds 19081da177e4SLinus Torvalds static int __init init_sel_fs(void) 19091da177e4SLinus Torvalds { 19101da177e4SLinus Torvalds int err; 19111da177e4SLinus Torvalds 19121da177e4SLinus Torvalds if (!selinux_enabled) 19131da177e4SLinus Torvalds return 0; 19147a627e3bSGreg Kroah-Hartman 1915f9bb4882SEric W. Biederman err = sysfs_create_mount_point(fs_kobj, "selinux"); 1916f9bb4882SEric W. Biederman if (err) 1917f9bb4882SEric W. Biederman return err; 19187a627e3bSGreg Kroah-Hartman 19191da177e4SLinus Torvalds err = register_filesystem(&sel_fs_type); 19207a627e3bSGreg Kroah-Hartman if (err) { 1921f9bb4882SEric W. Biederman sysfs_remove_mount_point(fs_kobj, "selinux"); 1922b77a493bSEric Paris return err; 19237a627e3bSGreg Kroah-Hartman } 1924b77a493bSEric Paris 1925765927b2SAl Viro selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type); 19261da177e4SLinus Torvalds if (IS_ERR(selinuxfs_mount)) { 19271da177e4SLinus Torvalds printk(KERN_ERR "selinuxfs: could not mount!\n"); 19281da177e4SLinus Torvalds err = PTR_ERR(selinuxfs_mount); 19291da177e4SLinus Torvalds selinuxfs_mount = NULL; 19301da177e4SLinus Torvalds } 1931b77a493bSEric Paris 19321da177e4SLinus Torvalds return err; 19331da177e4SLinus Torvalds } 19341da177e4SLinus Torvalds 19351da177e4SLinus Torvalds __initcall(init_sel_fs); 19361da177e4SLinus Torvalds 19371da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 19381da177e4SLinus Torvalds void exit_sel_fs(void) 19391da177e4SLinus Torvalds { 1940f9bb4882SEric W. Biederman sysfs_remove_mount_point(fs_kobj, "selinux"); 1941423e0ab0STim Chen kern_unmount(selinuxfs_mount); 19421da177e4SLinus Torvalds unregister_filesystem(&sel_fs_type); 19431da177e4SLinus Torvalds } 19441da177e4SLinus Torvalds #endif 1945