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 443bb56b25SPaul Moore /* Policy capability filenames */ 453bb56b25SPaul Moore static char *policycap_names[] = { 46b0c636b9SEric Paris "network_peer_controls", 472be4d74fSChris PeBenito "open_perms", 48da69a530SStephen Smalley "extended_socket_class", 492be4d74fSChris PeBenito "always_check_network" 503bb56b25SPaul Moore }; 513bb56b25SPaul Moore 521da177e4SLinus Torvalds unsigned int selinux_checkreqprot = CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE; 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds static int __init checkreqprot_setup(char *str) 551da177e4SLinus Torvalds { 56f5269710SEric Paris unsigned long checkreqprot; 5729707b20SJingoo Han if (!kstrtoul(str, 0, &checkreqprot)) 58f5269710SEric Paris selinux_checkreqprot = checkreqprot ? 1 : 0; 591da177e4SLinus Torvalds return 1; 601da177e4SLinus Torvalds } 611da177e4SLinus Torvalds __setup("checkreqprot=", checkreqprot_setup); 621da177e4SLinus Torvalds 63bb003079SIngo Molnar static DEFINE_MUTEX(sel_mutex); 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds /* global data for booleans */ 661872981bSEric Paris static struct dentry *bool_dir; 671872981bSEric Paris static int bool_num; 68d313f948SStephen Smalley static char **bool_pending_names; 691872981bSEric Paris static int *bool_pending_values; 701da177e4SLinus Torvalds 71e47c8fc5SChristopher J. PeBenito /* global data for classes */ 721872981bSEric Paris static struct dentry *class_dir; 73e47c8fc5SChristopher J. PeBenito static unsigned long last_class_ino; 74e47c8fc5SChristopher J. PeBenito 75cee74f47SEric Paris static char policy_opened; 76cee74f47SEric Paris 773bb56b25SPaul Moore /* global data for policy capabilities */ 781872981bSEric Paris static struct dentry *policycap_dir; 793bb56b25SPaul Moore 801da177e4SLinus Torvalds enum sel_inos { 811da177e4SLinus Torvalds SEL_ROOT_INO = 2, 821da177e4SLinus Torvalds SEL_LOAD, /* load policy */ 831da177e4SLinus Torvalds SEL_ENFORCE, /* get or set enforcing status */ 841da177e4SLinus Torvalds SEL_CONTEXT, /* validate context */ 851da177e4SLinus Torvalds SEL_ACCESS, /* compute access decision */ 861da177e4SLinus Torvalds SEL_CREATE, /* compute create labeling decision */ 871da177e4SLinus Torvalds SEL_RELABEL, /* compute relabeling decision */ 881da177e4SLinus Torvalds SEL_USER, /* compute reachable user contexts */ 891da177e4SLinus Torvalds SEL_POLICYVERS, /* return policy version for this kernel */ 901da177e4SLinus Torvalds SEL_COMMIT_BOOLS, /* commit new boolean values */ 911da177e4SLinus Torvalds SEL_MLS, /* return if MLS policy is enabled */ 921da177e4SLinus Torvalds SEL_DISABLE, /* disable SELinux until next reboot */ 931da177e4SLinus Torvalds SEL_MEMBER, /* compute polyinstantiation membership decision */ 941da177e4SLinus Torvalds SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */ 954e5ab4cbSJames Morris SEL_COMPAT_NET, /* whether to use old compat network packet controls */ 963f12070eSEric Paris SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */ 973f12070eSEric Paris SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */ 9811904167SKaiGai Kohei SEL_STATUS, /* export current status using mmap() */ 99cee74f47SEric Paris SEL_POLICY, /* allow userspace to read the in kernel policy */ 100f9df6458SAndrew Perepechko SEL_VALIDATE_TRANS, /* compute validatetrans decision */ 1016174eafcSJames Carter SEL_INO_NEXT, /* The next inode number to use */ 1021da177e4SLinus Torvalds }; 1031da177e4SLinus Torvalds 1046174eafcSJames Carter static unsigned long sel_last_ino = SEL_INO_NEXT - 1; 1056174eafcSJames Carter 106f0ee2e46SJames Carter #define SEL_INITCON_INO_OFFSET 0x01000000 107bce34bc0SJames Carter #define SEL_BOOL_INO_OFFSET 0x02000000 108e47c8fc5SChristopher J. PeBenito #define SEL_CLASS_INO_OFFSET 0x04000000 1093bb56b25SPaul Moore #define SEL_POLICYCAP_INO_OFFSET 0x08000000 110f0ee2e46SJames Carter #define SEL_INO_MASK 0x00ffffff 111f0ee2e46SJames Carter 1121da177e4SLinus Torvalds #define TMPBUFLEN 12 1131da177e4SLinus Torvalds static ssize_t sel_read_enforce(struct file *filp, char __user *buf, 1141da177e4SLinus Torvalds size_t count, loff_t *ppos) 1151da177e4SLinus Torvalds { 1161da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 1171da177e4SLinus Torvalds ssize_t length; 1181da177e4SLinus Torvalds 1191da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing); 1201da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 1211da177e4SLinus Torvalds } 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP 1241da177e4SLinus Torvalds static ssize_t sel_write_enforce(struct file *file, const char __user *buf, 1251da177e4SLinus Torvalds size_t count, loff_t *ppos) 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds { 128b77a493bSEric Paris char *page = NULL; 1291da177e4SLinus Torvalds ssize_t length; 1301da177e4SLinus Torvalds int new_value; 1311da177e4SLinus Torvalds 132bfd51626SDavi Arnaut if (count >= PAGE_SIZE) 1338365a719SAl Viro return -ENOMEM; 134b77a493bSEric Paris 1351da177e4SLinus Torvalds /* No partial writes. */ 136b77a493bSEric Paris if (*ppos != 0) 1378365a719SAl Viro return -EINVAL; 138b77a493bSEric Paris 1398365a719SAl Viro page = memdup_user_nul(buf, count); 1408365a719SAl Viro if (IS_ERR(page)) 1418365a719SAl Viro return PTR_ERR(page); 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds length = -EINVAL; 1441da177e4SLinus Torvalds if (sscanf(page, "%d", &new_value) != 1) 1451da177e4SLinus Torvalds goto out; 1461da177e4SLinus Torvalds 147ea49d10eSStephen Smalley new_value = !!new_value; 148ea49d10eSStephen Smalley 1491da177e4SLinus Torvalds if (new_value != selinux_enforcing) { 150be0554c9SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 151be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETENFORCE, 152be0554c9SStephen Smalley NULL); 1531da177e4SLinus Torvalds if (length) 1541da177e4SLinus Torvalds goto out; 155af601e46SSteve Grubb audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS, 1564746ec5bSEric Paris "enforcing=%d old_enforcing=%d auid=%u ses=%u", 1574746ec5bSEric Paris new_value, selinux_enforcing, 158581abc09SEric W. Biederman from_kuid(&init_user_ns, audit_get_loginuid(current)), 1594746ec5bSEric Paris audit_get_sessionid(current)); 1601da177e4SLinus Torvalds selinux_enforcing = new_value; 1611da177e4SLinus Torvalds if (selinux_enforcing) 1621da177e4SLinus Torvalds avc_ss_reset(0); 1631da177e4SLinus Torvalds selnl_notify_setenforce(selinux_enforcing); 16411904167SKaiGai Kohei selinux_status_update_setenforce(selinux_enforcing); 1651da177e4SLinus Torvalds } 1661da177e4SLinus Torvalds length = count; 1671da177e4SLinus Torvalds out: 1688365a719SAl Viro kfree(page); 1691da177e4SLinus Torvalds return length; 1701da177e4SLinus Torvalds } 1711da177e4SLinus Torvalds #else 1721da177e4SLinus Torvalds #define sel_write_enforce NULL 1731da177e4SLinus Torvalds #endif 1741da177e4SLinus Torvalds 1759c2e08c5SArjan van de Ven static const struct file_operations sel_enforce_ops = { 1761da177e4SLinus Torvalds .read = sel_read_enforce, 1771da177e4SLinus Torvalds .write = sel_write_enforce, 17857a62c23SArnd Bergmann .llseek = generic_file_llseek, 1791da177e4SLinus Torvalds }; 1801da177e4SLinus Torvalds 1813f12070eSEric Paris static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf, 1823f12070eSEric Paris size_t count, loff_t *ppos) 1833f12070eSEric Paris { 1843f12070eSEric Paris char tmpbuf[TMPBUFLEN]; 1853f12070eSEric Paris ssize_t length; 186496ad9aaSAl Viro ino_t ino = file_inode(filp)->i_ino; 1873f12070eSEric Paris int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ? 1883f12070eSEric Paris security_get_reject_unknown() : !security_get_allow_unknown(); 1893f12070eSEric Paris 1903f12070eSEric Paris length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown); 1913f12070eSEric Paris return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 1923f12070eSEric Paris } 1933f12070eSEric Paris 1943f12070eSEric Paris static const struct file_operations sel_handle_unknown_ops = { 1953f12070eSEric Paris .read = sel_read_handle_unknown, 19657a62c23SArnd Bergmann .llseek = generic_file_llseek, 1973f12070eSEric Paris }; 1983f12070eSEric Paris 19911904167SKaiGai Kohei static int sel_open_handle_status(struct inode *inode, struct file *filp) 20011904167SKaiGai Kohei { 20111904167SKaiGai Kohei struct page *status = selinux_kernel_status_page(); 20211904167SKaiGai Kohei 20311904167SKaiGai Kohei if (!status) 20411904167SKaiGai Kohei return -ENOMEM; 20511904167SKaiGai Kohei 20611904167SKaiGai Kohei filp->private_data = status; 20711904167SKaiGai Kohei 20811904167SKaiGai Kohei return 0; 20911904167SKaiGai Kohei } 21011904167SKaiGai Kohei 21111904167SKaiGai Kohei static ssize_t sel_read_handle_status(struct file *filp, char __user *buf, 21211904167SKaiGai Kohei size_t count, loff_t *ppos) 21311904167SKaiGai Kohei { 21411904167SKaiGai Kohei struct page *status = filp->private_data; 21511904167SKaiGai Kohei 21611904167SKaiGai Kohei BUG_ON(!status); 21711904167SKaiGai Kohei 21811904167SKaiGai Kohei return simple_read_from_buffer(buf, count, ppos, 21911904167SKaiGai Kohei page_address(status), 22011904167SKaiGai Kohei sizeof(struct selinux_kernel_status)); 22111904167SKaiGai Kohei } 22211904167SKaiGai Kohei 22311904167SKaiGai Kohei static int sel_mmap_handle_status(struct file *filp, 22411904167SKaiGai Kohei struct vm_area_struct *vma) 22511904167SKaiGai Kohei { 22611904167SKaiGai Kohei struct page *status = filp->private_data; 22711904167SKaiGai Kohei unsigned long size = vma->vm_end - vma->vm_start; 22811904167SKaiGai Kohei 22911904167SKaiGai Kohei BUG_ON(!status); 23011904167SKaiGai Kohei 23111904167SKaiGai Kohei /* only allows one page from the head */ 23211904167SKaiGai Kohei if (vma->vm_pgoff > 0 || size != PAGE_SIZE) 23311904167SKaiGai Kohei return -EIO; 23411904167SKaiGai Kohei /* disallow writable mapping */ 23511904167SKaiGai Kohei if (vma->vm_flags & VM_WRITE) 23611904167SKaiGai Kohei return -EPERM; 23711904167SKaiGai Kohei /* disallow mprotect() turns it into writable */ 23811904167SKaiGai Kohei vma->vm_flags &= ~VM_MAYWRITE; 23911904167SKaiGai Kohei 24011904167SKaiGai Kohei return remap_pfn_range(vma, vma->vm_start, 24111904167SKaiGai Kohei page_to_pfn(status), 24211904167SKaiGai Kohei size, vma->vm_page_prot); 24311904167SKaiGai Kohei } 24411904167SKaiGai Kohei 24511904167SKaiGai Kohei static const struct file_operations sel_handle_status_ops = { 24611904167SKaiGai Kohei .open = sel_open_handle_status, 24711904167SKaiGai Kohei .read = sel_read_handle_status, 24811904167SKaiGai Kohei .mmap = sel_mmap_handle_status, 24911904167SKaiGai Kohei .llseek = generic_file_llseek, 25011904167SKaiGai Kohei }; 25111904167SKaiGai Kohei 2521da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 2531da177e4SLinus Torvalds static ssize_t sel_write_disable(struct file *file, const char __user *buf, 2541da177e4SLinus Torvalds size_t count, loff_t *ppos) 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds { 2578365a719SAl Viro char *page; 2581da177e4SLinus Torvalds ssize_t length; 2591da177e4SLinus Torvalds int new_value; 2601da177e4SLinus Torvalds 261bfd51626SDavi Arnaut if (count >= PAGE_SIZE) 2628365a719SAl Viro return -ENOMEM; 263b77a493bSEric Paris 2641da177e4SLinus Torvalds /* No partial writes. */ 265b77a493bSEric Paris if (*ppos != 0) 2668365a719SAl Viro return -EINVAL; 267b77a493bSEric Paris 2688365a719SAl Viro page = memdup_user_nul(buf, count); 2698365a719SAl Viro if (IS_ERR(page)) 2708365a719SAl Viro return PTR_ERR(page); 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds length = -EINVAL; 2731da177e4SLinus Torvalds if (sscanf(page, "%d", &new_value) != 1) 2741da177e4SLinus Torvalds goto out; 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds if (new_value) { 2771da177e4SLinus Torvalds length = selinux_disable(); 278b77a493bSEric Paris if (length) 2791da177e4SLinus Torvalds goto out; 280af601e46SSteve Grubb audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS, 2814746ec5bSEric Paris "selinux=0 auid=%u ses=%u", 282581abc09SEric W. Biederman from_kuid(&init_user_ns, audit_get_loginuid(current)), 2834746ec5bSEric Paris audit_get_sessionid(current)); 2841da177e4SLinus Torvalds } 2851da177e4SLinus Torvalds 2861da177e4SLinus Torvalds length = count; 2871da177e4SLinus Torvalds out: 2888365a719SAl Viro kfree(page); 2891da177e4SLinus Torvalds return length; 2901da177e4SLinus Torvalds } 2911da177e4SLinus Torvalds #else 2921da177e4SLinus Torvalds #define sel_write_disable NULL 2931da177e4SLinus Torvalds #endif 2941da177e4SLinus Torvalds 2959c2e08c5SArjan van de Ven static const struct file_operations sel_disable_ops = { 2961da177e4SLinus Torvalds .write = sel_write_disable, 29757a62c23SArnd Bergmann .llseek = generic_file_llseek, 2981da177e4SLinus Torvalds }; 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds static ssize_t sel_read_policyvers(struct file *filp, char __user *buf, 3011da177e4SLinus Torvalds size_t count, loff_t *ppos) 3021da177e4SLinus Torvalds { 3031da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 3041da177e4SLinus Torvalds ssize_t length; 3051da177e4SLinus Torvalds 3061da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX); 3071da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 3081da177e4SLinus Torvalds } 3091da177e4SLinus Torvalds 3109c2e08c5SArjan van de Ven static const struct file_operations sel_policyvers_ops = { 3111da177e4SLinus Torvalds .read = sel_read_policyvers, 31257a62c23SArnd Bergmann .llseek = generic_file_llseek, 3131da177e4SLinus Torvalds }; 3141da177e4SLinus Torvalds 3151da177e4SLinus Torvalds /* declaration for sel_write_load */ 3161da177e4SLinus Torvalds static int sel_make_bools(void); 317e47c8fc5SChristopher J. PeBenito static int sel_make_classes(void); 3183bb56b25SPaul Moore static int sel_make_policycap(void); 319e47c8fc5SChristopher J. PeBenito 320e47c8fc5SChristopher J. PeBenito /* declaration for sel_make_class_dirs */ 321a1c2aa1eSAl Viro static struct dentry *sel_make_dir(struct dentry *dir, const char *name, 322e47c8fc5SChristopher J. PeBenito unsigned long *ino); 3231da177e4SLinus Torvalds 3241da177e4SLinus Torvalds static ssize_t sel_read_mls(struct file *filp, char __user *buf, 3251da177e4SLinus Torvalds size_t count, loff_t *ppos) 3261da177e4SLinus Torvalds { 3271da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 3281da177e4SLinus Torvalds ssize_t length; 3291da177e4SLinus Torvalds 3300719aaf5SGuido Trentalancia length = scnprintf(tmpbuf, TMPBUFLEN, "%d", 3310719aaf5SGuido Trentalancia security_mls_enabled()); 3321da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 3331da177e4SLinus Torvalds } 3341da177e4SLinus Torvalds 3359c2e08c5SArjan van de Ven static const struct file_operations sel_mls_ops = { 3361da177e4SLinus Torvalds .read = sel_read_mls, 33757a62c23SArnd Bergmann .llseek = generic_file_llseek, 3381da177e4SLinus Torvalds }; 3391da177e4SLinus Torvalds 340cee74f47SEric Paris struct policy_load_memory { 341cee74f47SEric Paris size_t len; 342cee74f47SEric Paris void *data; 343cee74f47SEric Paris }; 344cee74f47SEric Paris 345cee74f47SEric Paris static int sel_open_policy(struct inode *inode, struct file *filp) 346cee74f47SEric Paris { 347cee74f47SEric Paris struct policy_load_memory *plm = NULL; 348cee74f47SEric Paris int rc; 349cee74f47SEric Paris 350cee74f47SEric Paris BUG_ON(filp->private_data); 351cee74f47SEric Paris 352cee74f47SEric Paris mutex_lock(&sel_mutex); 353cee74f47SEric Paris 354be0554c9SStephen Smalley rc = avc_has_perm(current_sid(), SECINITSID_SECURITY, 355be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL); 356cee74f47SEric Paris if (rc) 357cee74f47SEric Paris goto err; 358cee74f47SEric Paris 359cee74f47SEric Paris rc = -EBUSY; 360cee74f47SEric Paris if (policy_opened) 361cee74f47SEric Paris goto err; 362cee74f47SEric Paris 363cee74f47SEric Paris rc = -ENOMEM; 364cee74f47SEric Paris plm = kzalloc(sizeof(*plm), GFP_KERNEL); 365cee74f47SEric Paris if (!plm) 366cee74f47SEric Paris goto err; 367cee74f47SEric Paris 368cee74f47SEric Paris if (i_size_read(inode) != security_policydb_len()) { 3695955102cSAl Viro inode_lock(inode); 370cee74f47SEric Paris i_size_write(inode, security_policydb_len()); 3715955102cSAl Viro inode_unlock(inode); 372cee74f47SEric Paris } 373cee74f47SEric Paris 374cee74f47SEric Paris rc = security_read_policy(&plm->data, &plm->len); 375cee74f47SEric Paris if (rc) 376cee74f47SEric Paris goto err; 377cee74f47SEric Paris 378cee74f47SEric Paris policy_opened = 1; 379cee74f47SEric Paris 380cee74f47SEric Paris filp->private_data = plm; 381cee74f47SEric Paris 382cee74f47SEric Paris mutex_unlock(&sel_mutex); 383cee74f47SEric Paris 384cee74f47SEric Paris return 0; 385cee74f47SEric Paris err: 386cee74f47SEric Paris mutex_unlock(&sel_mutex); 387cee74f47SEric Paris 388cee74f47SEric Paris if (plm) 389cee74f47SEric Paris vfree(plm->data); 390cee74f47SEric Paris kfree(plm); 391cee74f47SEric Paris return rc; 392cee74f47SEric Paris } 393cee74f47SEric Paris 394cee74f47SEric Paris static int sel_release_policy(struct inode *inode, struct file *filp) 395cee74f47SEric Paris { 396cee74f47SEric Paris struct policy_load_memory *plm = filp->private_data; 397cee74f47SEric Paris 398cee74f47SEric Paris BUG_ON(!plm); 399cee74f47SEric Paris 400cee74f47SEric Paris policy_opened = 0; 401cee74f47SEric Paris 402cee74f47SEric Paris vfree(plm->data); 403cee74f47SEric Paris kfree(plm); 404cee74f47SEric Paris 405cee74f47SEric Paris return 0; 406cee74f47SEric Paris } 407cee74f47SEric Paris 408cee74f47SEric Paris static ssize_t sel_read_policy(struct file *filp, char __user *buf, 409cee74f47SEric Paris size_t count, loff_t *ppos) 410cee74f47SEric Paris { 411cee74f47SEric Paris struct policy_load_memory *plm = filp->private_data; 412cee74f47SEric Paris int ret; 413cee74f47SEric Paris 414cee74f47SEric Paris mutex_lock(&sel_mutex); 415cee74f47SEric Paris 416be0554c9SStephen Smalley ret = avc_has_perm(current_sid(), SECINITSID_SECURITY, 417be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL); 418cee74f47SEric Paris if (ret) 419cee74f47SEric Paris goto out; 420cee74f47SEric Paris 421cee74f47SEric Paris ret = simple_read_from_buffer(buf, count, ppos, plm->data, plm->len); 422cee74f47SEric Paris out: 423cee74f47SEric Paris mutex_unlock(&sel_mutex); 424cee74f47SEric Paris return ret; 425cee74f47SEric Paris } 426cee74f47SEric Paris 427845ca30fSEric Paris static int sel_mmap_policy_fault(struct vm_area_struct *vma, 428845ca30fSEric Paris struct vm_fault *vmf) 429845ca30fSEric Paris { 430845ca30fSEric Paris struct policy_load_memory *plm = vma->vm_file->private_data; 431845ca30fSEric Paris unsigned long offset; 432845ca30fSEric Paris struct page *page; 433845ca30fSEric Paris 434845ca30fSEric Paris if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE)) 435845ca30fSEric Paris return VM_FAULT_SIGBUS; 436845ca30fSEric Paris 437845ca30fSEric Paris offset = vmf->pgoff << PAGE_SHIFT; 438845ca30fSEric Paris if (offset >= roundup(plm->len, PAGE_SIZE)) 439845ca30fSEric Paris return VM_FAULT_SIGBUS; 440845ca30fSEric Paris 441845ca30fSEric Paris page = vmalloc_to_page(plm->data + offset); 442845ca30fSEric Paris get_page(page); 443845ca30fSEric Paris 444845ca30fSEric Paris vmf->page = page; 445845ca30fSEric Paris 446845ca30fSEric Paris return 0; 447845ca30fSEric Paris } 448845ca30fSEric Paris 4497cbea8dcSKirill A. Shutemov static const struct vm_operations_struct sel_mmap_policy_ops = { 450845ca30fSEric Paris .fault = sel_mmap_policy_fault, 451845ca30fSEric Paris .page_mkwrite = sel_mmap_policy_fault, 452845ca30fSEric Paris }; 453845ca30fSEric Paris 454ad3fa08cSJames Morris static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma) 455845ca30fSEric Paris { 456845ca30fSEric Paris if (vma->vm_flags & VM_SHARED) { 457845ca30fSEric Paris /* do not allow mprotect to make mapping writable */ 458845ca30fSEric Paris vma->vm_flags &= ~VM_MAYWRITE; 459845ca30fSEric Paris 460845ca30fSEric Paris if (vma->vm_flags & VM_WRITE) 461845ca30fSEric Paris return -EACCES; 462845ca30fSEric Paris } 463845ca30fSEric Paris 464314e51b9SKonstantin Khlebnikov vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; 465845ca30fSEric Paris vma->vm_ops = &sel_mmap_policy_ops; 466845ca30fSEric Paris 467845ca30fSEric Paris return 0; 468845ca30fSEric Paris } 469845ca30fSEric Paris 470cee74f47SEric Paris static const struct file_operations sel_policy_ops = { 471cee74f47SEric Paris .open = sel_open_policy, 472cee74f47SEric Paris .read = sel_read_policy, 473845ca30fSEric Paris .mmap = sel_mmap_policy, 474cee74f47SEric Paris .release = sel_release_policy, 47547a93a5bSEric Paris .llseek = generic_file_llseek, 476cee74f47SEric Paris }; 477cee74f47SEric Paris 4781da177e4SLinus Torvalds static ssize_t sel_write_load(struct file *file, const char __user *buf, 4791da177e4SLinus Torvalds size_t count, loff_t *ppos) 4801da177e4SLinus Torvalds 4811da177e4SLinus Torvalds { 4821da177e4SLinus Torvalds ssize_t length; 4831da177e4SLinus Torvalds void *data = NULL; 4841da177e4SLinus Torvalds 485bb003079SIngo Molnar mutex_lock(&sel_mutex); 4861da177e4SLinus Torvalds 487be0554c9SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 488be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL); 4891da177e4SLinus Torvalds if (length) 4901da177e4SLinus Torvalds goto out; 4911da177e4SLinus Torvalds 4921da177e4SLinus Torvalds /* No partial writes. */ 4931da177e4SLinus Torvalds length = -EINVAL; 494b77a493bSEric Paris if (*ppos != 0) 4951da177e4SLinus Torvalds goto out; 4961da177e4SLinus Torvalds 497b77a493bSEric Paris length = -EFBIG; 498b77a493bSEric Paris if (count > 64 * 1024 * 1024) 4991da177e4SLinus Torvalds goto out; 500b77a493bSEric Paris 501b77a493bSEric Paris length = -ENOMEM; 502b77a493bSEric Paris data = vmalloc(count); 503b77a493bSEric Paris if (!data) 504b77a493bSEric Paris goto out; 5051da177e4SLinus Torvalds 5061da177e4SLinus Torvalds length = -EFAULT; 5071da177e4SLinus Torvalds if (copy_from_user(data, buf, count) != 0) 5081da177e4SLinus Torvalds goto out; 5091da177e4SLinus Torvalds 5101da177e4SLinus Torvalds length = security_load_policy(data, count); 5114262fb51SGary Tierney if (length) { 5124262fb51SGary Tierney pr_warn_ratelimited("SELinux: failed to load policy\n"); 5131da177e4SLinus Torvalds goto out; 5144262fb51SGary Tierney } 5151da177e4SLinus Torvalds 516b77a493bSEric Paris length = sel_make_bools(); 5174262fb51SGary Tierney if (length) { 5184262fb51SGary Tierney pr_err("SELinux: failed to load policy booleans\n"); 519e47c8fc5SChristopher J. PeBenito goto out1; 5204262fb51SGary Tierney } 521e47c8fc5SChristopher J. PeBenito 522b77a493bSEric Paris length = sel_make_classes(); 5234262fb51SGary Tierney if (length) { 5244262fb51SGary Tierney pr_err("SELinux: failed to load policy classes\n"); 5253bb56b25SPaul Moore goto out1; 5264262fb51SGary Tierney } 5273bb56b25SPaul Moore 528b77a493bSEric Paris length = sel_make_policycap(); 5294262fb51SGary Tierney if (length) { 5304262fb51SGary Tierney pr_err("SELinux: failed to load policy capabilities\n"); 531b77a493bSEric Paris goto out1; 5324262fb51SGary Tierney } 533b77a493bSEric Paris 5341da177e4SLinus Torvalds length = count; 535e47c8fc5SChristopher J. PeBenito 536e47c8fc5SChristopher J. PeBenito out1: 537af601e46SSteve Grubb audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD, 5384746ec5bSEric Paris "policy loaded auid=%u ses=%u", 539581abc09SEric W. Biederman from_kuid(&init_user_ns, audit_get_loginuid(current)), 5404746ec5bSEric Paris audit_get_sessionid(current)); 5411da177e4SLinus Torvalds out: 542bb003079SIngo Molnar mutex_unlock(&sel_mutex); 5431da177e4SLinus Torvalds vfree(data); 5441da177e4SLinus Torvalds return length; 5451da177e4SLinus Torvalds } 5461da177e4SLinus Torvalds 5479c2e08c5SArjan van de Ven static const struct file_operations sel_load_ops = { 5481da177e4SLinus Torvalds .write = sel_write_load, 54957a62c23SArnd Bergmann .llseek = generic_file_llseek, 5501da177e4SLinus Torvalds }; 5511da177e4SLinus Torvalds 552ce9982d0SStephen Smalley static ssize_t sel_write_context(struct file *file, char *buf, size_t size) 5531da177e4SLinus Torvalds { 554b77a493bSEric Paris char *canon = NULL; 555ce9982d0SStephen Smalley u32 sid, len; 5561da177e4SLinus Torvalds ssize_t length; 5571da177e4SLinus Torvalds 558be0554c9SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 559be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__CHECK_CONTEXT, NULL); 5601da177e4SLinus Torvalds if (length) 561b77a493bSEric Paris goto out; 5621da177e4SLinus Torvalds 56352a4c640SNikolay Aleksandrov length = security_context_to_sid(buf, size, &sid, GFP_KERNEL); 564b77a493bSEric Paris if (length) 565b77a493bSEric Paris goto out; 5661da177e4SLinus Torvalds 567ce9982d0SStephen Smalley length = security_sid_to_context(sid, &canon, &len); 568b77a493bSEric Paris if (length) 569b77a493bSEric Paris goto out; 570ce9982d0SStephen Smalley 571b77a493bSEric Paris length = -ERANGE; 572ce9982d0SStephen Smalley if (len > SIMPLE_TRANSACTION_LIMIT) { 573744ba35eSEric Paris printk(KERN_ERR "SELinux: %s: context size (%u) exceeds " 574744ba35eSEric Paris "payload max\n", __func__, len); 575ce9982d0SStephen Smalley goto out; 576ce9982d0SStephen Smalley } 577ce9982d0SStephen Smalley 578ce9982d0SStephen Smalley memcpy(buf, canon, len); 579ce9982d0SStephen Smalley length = len; 5801da177e4SLinus Torvalds out: 581ce9982d0SStephen Smalley kfree(canon); 5821da177e4SLinus Torvalds return length; 5831da177e4SLinus Torvalds } 5841da177e4SLinus Torvalds 5851da177e4SLinus Torvalds static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf, 5861da177e4SLinus Torvalds size_t count, loff_t *ppos) 5871da177e4SLinus Torvalds { 5881da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 5891da177e4SLinus Torvalds ssize_t length; 5901da177e4SLinus Torvalds 5911da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot); 5921da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 5931da177e4SLinus Torvalds } 5941da177e4SLinus Torvalds 5951da177e4SLinus Torvalds static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf, 5961da177e4SLinus Torvalds size_t count, loff_t *ppos) 5971da177e4SLinus Torvalds { 5988365a719SAl Viro char *page; 5991da177e4SLinus Torvalds ssize_t length; 6001da177e4SLinus Torvalds unsigned int new_value; 6011da177e4SLinus Torvalds 602be0554c9SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 603be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETCHECKREQPROT, 604be0554c9SStephen Smalley NULL); 6051da177e4SLinus Torvalds if (length) 6068365a719SAl Viro return length; 6071da177e4SLinus Torvalds 608bfd51626SDavi Arnaut if (count >= PAGE_SIZE) 6098365a719SAl Viro return -ENOMEM; 610b77a493bSEric Paris 6111da177e4SLinus Torvalds /* No partial writes. */ 612b77a493bSEric Paris if (*ppos != 0) 6138365a719SAl Viro return -EINVAL; 614b77a493bSEric Paris 6158365a719SAl Viro page = memdup_user_nul(buf, count); 6168365a719SAl Viro if (IS_ERR(page)) 6178365a719SAl Viro return PTR_ERR(page); 6181da177e4SLinus Torvalds 6191da177e4SLinus Torvalds length = -EINVAL; 6201da177e4SLinus Torvalds if (sscanf(page, "%u", &new_value) != 1) 6211da177e4SLinus Torvalds goto out; 6221da177e4SLinus Torvalds 6231da177e4SLinus Torvalds selinux_checkreqprot = new_value ? 1 : 0; 6241da177e4SLinus Torvalds length = count; 6251da177e4SLinus Torvalds out: 6268365a719SAl Viro kfree(page); 6271da177e4SLinus Torvalds return length; 6281da177e4SLinus Torvalds } 6299c2e08c5SArjan van de Ven static const struct file_operations sel_checkreqprot_ops = { 6301da177e4SLinus Torvalds .read = sel_read_checkreqprot, 6311da177e4SLinus Torvalds .write = sel_write_checkreqprot, 63257a62c23SArnd Bergmann .llseek = generic_file_llseek, 6331da177e4SLinus Torvalds }; 6341da177e4SLinus Torvalds 635f9df6458SAndrew Perepechko static ssize_t sel_write_validatetrans(struct file *file, 636f9df6458SAndrew Perepechko const char __user *buf, 637f9df6458SAndrew Perepechko size_t count, loff_t *ppos) 638f9df6458SAndrew Perepechko { 639f9df6458SAndrew Perepechko char *oldcon = NULL, *newcon = NULL, *taskcon = NULL; 640f9df6458SAndrew Perepechko char *req = NULL; 641f9df6458SAndrew Perepechko u32 osid, nsid, tsid; 642f9df6458SAndrew Perepechko u16 tclass; 643f9df6458SAndrew Perepechko int rc; 644f9df6458SAndrew Perepechko 645be0554c9SStephen Smalley rc = avc_has_perm(current_sid(), SECINITSID_SECURITY, 646be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__VALIDATE_TRANS, NULL); 647f9df6458SAndrew Perepechko if (rc) 648f9df6458SAndrew Perepechko goto out; 649f9df6458SAndrew Perepechko 650f9df6458SAndrew Perepechko rc = -ENOMEM; 651f9df6458SAndrew Perepechko if (count >= PAGE_SIZE) 652f9df6458SAndrew Perepechko goto out; 653f9df6458SAndrew Perepechko 654f9df6458SAndrew Perepechko /* No partial writes. */ 655f9df6458SAndrew Perepechko rc = -EINVAL; 656f9df6458SAndrew Perepechko if (*ppos != 0) 657f9df6458SAndrew Perepechko goto out; 658f9df6458SAndrew Perepechko 659f9df6458SAndrew Perepechko rc = -ENOMEM; 660f9df6458SAndrew Perepechko req = kzalloc(count + 1, GFP_KERNEL); 661f9df6458SAndrew Perepechko if (!req) 662f9df6458SAndrew Perepechko goto out; 663f9df6458SAndrew Perepechko 664f9df6458SAndrew Perepechko rc = -EFAULT; 665f9df6458SAndrew Perepechko if (copy_from_user(req, buf, count)) 666f9df6458SAndrew Perepechko goto out; 667f9df6458SAndrew Perepechko 668f9df6458SAndrew Perepechko rc = -ENOMEM; 669f9df6458SAndrew Perepechko oldcon = kzalloc(count + 1, GFP_KERNEL); 670f9df6458SAndrew Perepechko if (!oldcon) 671f9df6458SAndrew Perepechko goto out; 672f9df6458SAndrew Perepechko 673f9df6458SAndrew Perepechko newcon = kzalloc(count + 1, GFP_KERNEL); 674f9df6458SAndrew Perepechko if (!newcon) 675f9df6458SAndrew Perepechko goto out; 676f9df6458SAndrew Perepechko 677f9df6458SAndrew Perepechko taskcon = kzalloc(count + 1, GFP_KERNEL); 678f9df6458SAndrew Perepechko if (!taskcon) 679f9df6458SAndrew Perepechko goto out; 680f9df6458SAndrew Perepechko 681f9df6458SAndrew Perepechko rc = -EINVAL; 682f9df6458SAndrew Perepechko if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4) 683f9df6458SAndrew Perepechko goto out; 684f9df6458SAndrew Perepechko 685f9df6458SAndrew Perepechko rc = security_context_str_to_sid(oldcon, &osid, GFP_KERNEL); 686f9df6458SAndrew Perepechko if (rc) 687f9df6458SAndrew Perepechko goto out; 688f9df6458SAndrew Perepechko 689f9df6458SAndrew Perepechko rc = security_context_str_to_sid(newcon, &nsid, GFP_KERNEL); 690f9df6458SAndrew Perepechko if (rc) 691f9df6458SAndrew Perepechko goto out; 692f9df6458SAndrew Perepechko 693f9df6458SAndrew Perepechko rc = security_context_str_to_sid(taskcon, &tsid, GFP_KERNEL); 694f9df6458SAndrew Perepechko if (rc) 695f9df6458SAndrew Perepechko goto out; 696f9df6458SAndrew Perepechko 697f9df6458SAndrew Perepechko rc = security_validate_transition_user(osid, nsid, tsid, tclass); 698f9df6458SAndrew Perepechko if (!rc) 699f9df6458SAndrew Perepechko rc = count; 700f9df6458SAndrew Perepechko out: 701f9df6458SAndrew Perepechko kfree(req); 702f9df6458SAndrew Perepechko kfree(oldcon); 703f9df6458SAndrew Perepechko kfree(newcon); 704f9df6458SAndrew Perepechko kfree(taskcon); 705f9df6458SAndrew Perepechko return rc; 706f9df6458SAndrew Perepechko } 707f9df6458SAndrew Perepechko 708f9df6458SAndrew Perepechko static const struct file_operations sel_transition_ops = { 709f9df6458SAndrew Perepechko .write = sel_write_validatetrans, 710f9df6458SAndrew Perepechko .llseek = generic_file_llseek, 711f9df6458SAndrew Perepechko }; 712f9df6458SAndrew Perepechko 7131da177e4SLinus Torvalds /* 7141da177e4SLinus Torvalds * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c 7151da177e4SLinus Torvalds */ 7161da177e4SLinus Torvalds static ssize_t sel_write_access(struct file *file, char *buf, size_t size); 7171da177e4SLinus Torvalds static ssize_t sel_write_create(struct file *file, char *buf, size_t size); 7181da177e4SLinus Torvalds static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size); 7191da177e4SLinus Torvalds static ssize_t sel_write_user(struct file *file, char *buf, size_t size); 7201da177e4SLinus Torvalds static ssize_t sel_write_member(struct file *file, char *buf, size_t size); 7211da177e4SLinus Torvalds 7221da177e4SLinus Torvalds static ssize_t (*write_op[])(struct file *, char *, size_t) = { 7231da177e4SLinus Torvalds [SEL_ACCESS] = sel_write_access, 7241da177e4SLinus Torvalds [SEL_CREATE] = sel_write_create, 7251da177e4SLinus Torvalds [SEL_RELABEL] = sel_write_relabel, 7261da177e4SLinus Torvalds [SEL_USER] = sel_write_user, 7271da177e4SLinus Torvalds [SEL_MEMBER] = sel_write_member, 728ce9982d0SStephen Smalley [SEL_CONTEXT] = sel_write_context, 7291da177e4SLinus Torvalds }; 7301da177e4SLinus Torvalds 7311da177e4SLinus Torvalds static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos) 7321da177e4SLinus Torvalds { 733496ad9aaSAl Viro ino_t ino = file_inode(file)->i_ino; 7341da177e4SLinus Torvalds char *data; 7351da177e4SLinus Torvalds ssize_t rv; 7361da177e4SLinus Torvalds 7376e20a64aSNicolas Kaiser if (ino >= ARRAY_SIZE(write_op) || !write_op[ino]) 7381da177e4SLinus Torvalds return -EINVAL; 7391da177e4SLinus Torvalds 7401da177e4SLinus Torvalds data = simple_transaction_get(file, buf, size); 7411da177e4SLinus Torvalds if (IS_ERR(data)) 7421da177e4SLinus Torvalds return PTR_ERR(data); 7431da177e4SLinus Torvalds 7441da177e4SLinus Torvalds rv = write_op[ino](file, data, size); 7451da177e4SLinus Torvalds if (rv > 0) { 7461da177e4SLinus Torvalds simple_transaction_set(file, rv); 7471da177e4SLinus Torvalds rv = size; 7481da177e4SLinus Torvalds } 7491da177e4SLinus Torvalds return rv; 7501da177e4SLinus Torvalds } 7511da177e4SLinus Torvalds 7529c2e08c5SArjan van de Ven static const struct file_operations transaction_ops = { 7531da177e4SLinus Torvalds .write = selinux_transaction_write, 7541da177e4SLinus Torvalds .read = simple_transaction_read, 7551da177e4SLinus Torvalds .release = simple_transaction_release, 75657a62c23SArnd Bergmann .llseek = generic_file_llseek, 7571da177e4SLinus Torvalds }; 7581da177e4SLinus Torvalds 7591da177e4SLinus Torvalds /* 7601da177e4SLinus Torvalds * payload - write methods 7611da177e4SLinus Torvalds * If the method has a response, the response should be put in buf, 7621da177e4SLinus Torvalds * and the length returned. Otherwise return 0 or and -error. 7631da177e4SLinus Torvalds */ 7641da177e4SLinus Torvalds 7651da177e4SLinus Torvalds static ssize_t sel_write_access(struct file *file, char *buf, size_t size) 7661da177e4SLinus Torvalds { 767b77a493bSEric Paris char *scon = NULL, *tcon = NULL; 7681da177e4SLinus Torvalds u32 ssid, tsid; 7691da177e4SLinus Torvalds u16 tclass; 7701da177e4SLinus Torvalds struct av_decision avd; 7711da177e4SLinus Torvalds ssize_t length; 7721da177e4SLinus Torvalds 773be0554c9SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 774be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_AV, NULL); 7751da177e4SLinus Torvalds if (length) 776b77a493bSEric Paris goto out; 7771da177e4SLinus Torvalds 7781da177e4SLinus Torvalds length = -ENOMEM; 77989d155efSJames Morris scon = kzalloc(size + 1, GFP_KERNEL); 7801da177e4SLinus Torvalds if (!scon) 781b77a493bSEric Paris goto out; 7821da177e4SLinus Torvalds 783b77a493bSEric Paris length = -ENOMEM; 78489d155efSJames Morris tcon = kzalloc(size + 1, GFP_KERNEL); 7851da177e4SLinus Torvalds if (!tcon) 7861da177e4SLinus Torvalds goto out; 7871da177e4SLinus Torvalds 7881da177e4SLinus Torvalds length = -EINVAL; 78919439d05SStephen Smalley if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) 790b77a493bSEric Paris goto out; 7911da177e4SLinus Torvalds 79244be2f65SRasmus Villemoes length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL); 793b77a493bSEric Paris if (length) 794b77a493bSEric Paris goto out; 795b77a493bSEric Paris 79644be2f65SRasmus Villemoes length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL); 797b77a493bSEric Paris if (length) 798b77a493bSEric Paris goto out; 7991da177e4SLinus Torvalds 80019439d05SStephen Smalley security_compute_av_user(ssid, tsid, tclass, &avd); 8011da177e4SLinus Torvalds 8021da177e4SLinus Torvalds length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, 8038a6f83afSKaiGai Kohei "%x %x %x %x %u %x", 804f1c6381aSEric Paris avd.allowed, 0xffffffff, 8051da177e4SLinus Torvalds avd.auditallow, avd.auditdeny, 8068a6f83afSKaiGai Kohei avd.seqno, avd.flags); 8071da177e4SLinus Torvalds out: 808b77a493bSEric Paris kfree(tcon); 8091da177e4SLinus Torvalds kfree(scon); 8101da177e4SLinus Torvalds return length; 8111da177e4SLinus Torvalds } 8121da177e4SLinus Torvalds 8131da177e4SLinus Torvalds static ssize_t sel_write_create(struct file *file, char *buf, size_t size) 8141da177e4SLinus Torvalds { 815b77a493bSEric Paris char *scon = NULL, *tcon = NULL; 816f50a3ec9SKohei Kaigai char *namebuf = NULL, *objname = NULL; 8171da177e4SLinus Torvalds u32 ssid, tsid, newsid; 8181da177e4SLinus Torvalds u16 tclass; 8191da177e4SLinus Torvalds ssize_t length; 820b77a493bSEric Paris char *newcon = NULL; 8211da177e4SLinus Torvalds u32 len; 822f50a3ec9SKohei Kaigai int nargs; 8231da177e4SLinus Torvalds 824be0554c9SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 825be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_CREATE, 826be0554c9SStephen Smalley NULL); 8271da177e4SLinus Torvalds if (length) 828b77a493bSEric Paris goto out; 8291da177e4SLinus Torvalds 8301da177e4SLinus Torvalds length = -ENOMEM; 83189d155efSJames Morris scon = kzalloc(size + 1, GFP_KERNEL); 8321da177e4SLinus Torvalds if (!scon) 833b77a493bSEric Paris goto out; 8341da177e4SLinus Torvalds 835b77a493bSEric Paris length = -ENOMEM; 83689d155efSJames Morris tcon = kzalloc(size + 1, GFP_KERNEL); 8371da177e4SLinus Torvalds if (!tcon) 8381da177e4SLinus Torvalds goto out; 8391da177e4SLinus Torvalds 840f50a3ec9SKohei Kaigai length = -ENOMEM; 841f50a3ec9SKohei Kaigai namebuf = kzalloc(size + 1, GFP_KERNEL); 842f50a3ec9SKohei Kaigai if (!namebuf) 843b77a493bSEric Paris goto out; 8441da177e4SLinus Torvalds 845f50a3ec9SKohei Kaigai length = -EINVAL; 846f50a3ec9SKohei Kaigai nargs = sscanf(buf, "%s %s %hu %s", scon, tcon, &tclass, namebuf); 847f50a3ec9SKohei Kaigai if (nargs < 3 || nargs > 4) 848f50a3ec9SKohei Kaigai goto out; 8490f7e4c33SKohei Kaigai if (nargs == 4) { 8500f7e4c33SKohei Kaigai /* 8510f7e4c33SKohei Kaigai * If and when the name of new object to be queried contains 8520f7e4c33SKohei Kaigai * either whitespace or multibyte characters, they shall be 8530f7e4c33SKohei Kaigai * encoded based on the percentage-encoding rule. 8540f7e4c33SKohei Kaigai * If not encoded, the sscanf logic picks up only left-half 8550f7e4c33SKohei Kaigai * of the supplied name; splitted by a whitespace unexpectedly. 8560f7e4c33SKohei Kaigai */ 8570f7e4c33SKohei Kaigai char *r, *w; 8580f7e4c33SKohei Kaigai int c1, c2; 8590f7e4c33SKohei Kaigai 8600f7e4c33SKohei Kaigai r = w = namebuf; 8610f7e4c33SKohei Kaigai do { 8620f7e4c33SKohei Kaigai c1 = *r++; 8630f7e4c33SKohei Kaigai if (c1 == '+') 8640f7e4c33SKohei Kaigai c1 = ' '; 8650f7e4c33SKohei Kaigai else if (c1 == '%') { 866af7ff2c2SAndy Shevchenko c1 = hex_to_bin(*r++); 867af7ff2c2SAndy Shevchenko if (c1 < 0) 8680f7e4c33SKohei Kaigai goto out; 869af7ff2c2SAndy Shevchenko c2 = hex_to_bin(*r++); 870af7ff2c2SAndy Shevchenko if (c2 < 0) 8710f7e4c33SKohei Kaigai goto out; 8720f7e4c33SKohei Kaigai c1 = (c1 << 4) | c2; 8730f7e4c33SKohei Kaigai } 8740f7e4c33SKohei Kaigai *w++ = c1; 8750f7e4c33SKohei Kaigai } while (c1 != '\0'); 8760f7e4c33SKohei Kaigai 877f50a3ec9SKohei Kaigai objname = namebuf; 8780f7e4c33SKohei Kaigai } 879f50a3ec9SKohei Kaigai 88044be2f65SRasmus Villemoes length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL); 881b77a493bSEric Paris if (length) 882b77a493bSEric Paris goto out; 883b77a493bSEric Paris 88444be2f65SRasmus Villemoes length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL); 885b77a493bSEric Paris if (length) 886b77a493bSEric Paris goto out; 8871da177e4SLinus Torvalds 888f50a3ec9SKohei Kaigai length = security_transition_sid_user(ssid, tsid, tclass, 889f50a3ec9SKohei Kaigai objname, &newsid); 890b77a493bSEric Paris if (length) 891b77a493bSEric Paris goto out; 8921da177e4SLinus Torvalds 8931da177e4SLinus Torvalds length = security_sid_to_context(newsid, &newcon, &len); 894b77a493bSEric Paris if (length) 895b77a493bSEric Paris goto out; 8961da177e4SLinus Torvalds 897b77a493bSEric Paris length = -ERANGE; 8981da177e4SLinus Torvalds if (len > SIMPLE_TRANSACTION_LIMIT) { 899744ba35eSEric Paris printk(KERN_ERR "SELinux: %s: context size (%u) exceeds " 900744ba35eSEric Paris "payload max\n", __func__, len); 901b77a493bSEric Paris goto out; 9021da177e4SLinus Torvalds } 9031da177e4SLinus Torvalds 9041da177e4SLinus Torvalds memcpy(buf, newcon, len); 9051da177e4SLinus Torvalds length = len; 9061da177e4SLinus Torvalds out: 907b77a493bSEric Paris kfree(newcon); 908f50a3ec9SKohei Kaigai kfree(namebuf); 909b77a493bSEric Paris kfree(tcon); 9101da177e4SLinus Torvalds kfree(scon); 9111da177e4SLinus Torvalds return length; 9121da177e4SLinus Torvalds } 9131da177e4SLinus Torvalds 9141da177e4SLinus Torvalds static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size) 9151da177e4SLinus Torvalds { 916b77a493bSEric Paris char *scon = NULL, *tcon = NULL; 9171da177e4SLinus Torvalds u32 ssid, tsid, newsid; 9181da177e4SLinus Torvalds u16 tclass; 9191da177e4SLinus Torvalds ssize_t length; 920b77a493bSEric Paris char *newcon = NULL; 9211da177e4SLinus Torvalds u32 len; 9221da177e4SLinus Torvalds 923be0554c9SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 924be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_RELABEL, 925be0554c9SStephen Smalley NULL); 9261da177e4SLinus Torvalds if (length) 927b77a493bSEric Paris goto out; 9281da177e4SLinus Torvalds 9291da177e4SLinus Torvalds length = -ENOMEM; 93089d155efSJames Morris scon = kzalloc(size + 1, GFP_KERNEL); 9311da177e4SLinus Torvalds if (!scon) 932b77a493bSEric Paris goto out; 9331da177e4SLinus Torvalds 934b77a493bSEric Paris length = -ENOMEM; 93589d155efSJames Morris tcon = kzalloc(size + 1, GFP_KERNEL); 9361da177e4SLinus Torvalds if (!tcon) 9371da177e4SLinus Torvalds goto out; 9381da177e4SLinus Torvalds 9391da177e4SLinus Torvalds length = -EINVAL; 9401da177e4SLinus Torvalds if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) 941b77a493bSEric Paris goto out; 9421da177e4SLinus Torvalds 94344be2f65SRasmus Villemoes length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL); 944b77a493bSEric Paris if (length) 945b77a493bSEric Paris goto out; 946b77a493bSEric Paris 94744be2f65SRasmus Villemoes length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL); 948b77a493bSEric Paris if (length) 949b77a493bSEric Paris goto out; 9501da177e4SLinus Torvalds 9511da177e4SLinus Torvalds length = security_change_sid(ssid, tsid, tclass, &newsid); 952b77a493bSEric Paris if (length) 953b77a493bSEric Paris goto out; 9541da177e4SLinus Torvalds 9551da177e4SLinus Torvalds length = security_sid_to_context(newsid, &newcon, &len); 956b77a493bSEric Paris if (length) 957b77a493bSEric Paris goto out; 9581da177e4SLinus Torvalds 9591da177e4SLinus Torvalds length = -ERANGE; 960b77a493bSEric Paris if (len > SIMPLE_TRANSACTION_LIMIT) 961b77a493bSEric Paris goto out; 9621da177e4SLinus Torvalds 9631da177e4SLinus Torvalds memcpy(buf, newcon, len); 9641da177e4SLinus Torvalds length = len; 9651da177e4SLinus Torvalds out: 966b77a493bSEric Paris kfree(newcon); 967b77a493bSEric Paris kfree(tcon); 9681da177e4SLinus Torvalds kfree(scon); 9691da177e4SLinus Torvalds return length; 9701da177e4SLinus Torvalds } 9711da177e4SLinus Torvalds 9721da177e4SLinus Torvalds static ssize_t sel_write_user(struct file *file, char *buf, size_t size) 9731da177e4SLinus Torvalds { 974b77a493bSEric Paris char *con = NULL, *user = NULL, *ptr; 975b77a493bSEric Paris u32 sid, *sids = NULL; 9761da177e4SLinus Torvalds ssize_t length; 9771da177e4SLinus Torvalds char *newcon; 9781da177e4SLinus Torvalds int i, rc; 9791da177e4SLinus Torvalds u32 len, nsids; 9801da177e4SLinus Torvalds 981be0554c9SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 982be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_USER, 983be0554c9SStephen Smalley NULL); 9841da177e4SLinus Torvalds if (length) 9856eab04a8SJustin P. Mattock goto out; 9861da177e4SLinus Torvalds 9871da177e4SLinus Torvalds length = -ENOMEM; 98889d155efSJames Morris con = kzalloc(size + 1, GFP_KERNEL); 9891da177e4SLinus Torvalds if (!con) 9906eab04a8SJustin P. Mattock goto out; 9911da177e4SLinus Torvalds 992b77a493bSEric Paris length = -ENOMEM; 99389d155efSJames Morris user = kzalloc(size + 1, GFP_KERNEL); 9941da177e4SLinus Torvalds if (!user) 9951da177e4SLinus Torvalds goto out; 9961da177e4SLinus Torvalds 9971da177e4SLinus Torvalds length = -EINVAL; 9981da177e4SLinus Torvalds if (sscanf(buf, "%s %s", con, user) != 2) 999b77a493bSEric Paris goto out; 10001da177e4SLinus Torvalds 100144be2f65SRasmus Villemoes length = security_context_str_to_sid(con, &sid, GFP_KERNEL); 1002b77a493bSEric Paris if (length) 1003b77a493bSEric Paris goto out; 10041da177e4SLinus Torvalds 10051da177e4SLinus Torvalds length = security_get_user_sids(sid, user, &sids, &nsids); 1006b77a493bSEric Paris if (length) 1007b77a493bSEric Paris goto out; 10081da177e4SLinus Torvalds 10091da177e4SLinus Torvalds length = sprintf(buf, "%u", nsids) + 1; 10101da177e4SLinus Torvalds ptr = buf + length; 10111da177e4SLinus Torvalds for (i = 0; i < nsids; i++) { 10121da177e4SLinus Torvalds rc = security_sid_to_context(sids[i], &newcon, &len); 10131da177e4SLinus Torvalds if (rc) { 10141da177e4SLinus Torvalds length = rc; 1015b77a493bSEric Paris goto out; 10161da177e4SLinus Torvalds } 10171da177e4SLinus Torvalds if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) { 10181da177e4SLinus Torvalds kfree(newcon); 10191da177e4SLinus Torvalds length = -ERANGE; 1020b77a493bSEric Paris goto out; 10211da177e4SLinus Torvalds } 10221da177e4SLinus Torvalds memcpy(ptr, newcon, len); 10231da177e4SLinus Torvalds kfree(newcon); 10241da177e4SLinus Torvalds ptr += len; 10251da177e4SLinus Torvalds length += len; 10261da177e4SLinus Torvalds } 10271da177e4SLinus Torvalds out: 1028b77a493bSEric Paris kfree(sids); 1029b77a493bSEric Paris kfree(user); 10301da177e4SLinus Torvalds kfree(con); 10311da177e4SLinus Torvalds return length; 10321da177e4SLinus Torvalds } 10331da177e4SLinus Torvalds 10341da177e4SLinus Torvalds static ssize_t sel_write_member(struct file *file, char *buf, size_t size) 10351da177e4SLinus Torvalds { 1036b77a493bSEric Paris char *scon = NULL, *tcon = NULL; 10371da177e4SLinus Torvalds u32 ssid, tsid, newsid; 10381da177e4SLinus Torvalds u16 tclass; 10391da177e4SLinus Torvalds ssize_t length; 1040b77a493bSEric Paris char *newcon = NULL; 10411da177e4SLinus Torvalds u32 len; 10421da177e4SLinus Torvalds 1043be0554c9SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 1044be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_MEMBER, 1045be0554c9SStephen Smalley NULL); 10461da177e4SLinus Torvalds if (length) 1047b77a493bSEric Paris goto out; 10481da177e4SLinus Torvalds 10491da177e4SLinus Torvalds length = -ENOMEM; 105089d155efSJames Morris scon = kzalloc(size + 1, GFP_KERNEL); 10511da177e4SLinus Torvalds if (!scon) 10526eab04a8SJustin P. Mattock goto out; 10531da177e4SLinus Torvalds 1054b77a493bSEric Paris length = -ENOMEM; 105589d155efSJames Morris tcon = kzalloc(size + 1, GFP_KERNEL); 10561da177e4SLinus Torvalds if (!tcon) 10571da177e4SLinus Torvalds goto out; 10581da177e4SLinus Torvalds 10591da177e4SLinus Torvalds length = -EINVAL; 10601da177e4SLinus Torvalds if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) 1061b77a493bSEric Paris goto out; 10621da177e4SLinus Torvalds 106344be2f65SRasmus Villemoes length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL); 1064b77a493bSEric Paris if (length) 1065b77a493bSEric Paris goto out; 1066b77a493bSEric Paris 106744be2f65SRasmus Villemoes length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL); 1068b77a493bSEric Paris if (length) 1069b77a493bSEric Paris goto out; 10701da177e4SLinus Torvalds 10711da177e4SLinus Torvalds length = security_member_sid(ssid, tsid, tclass, &newsid); 1072b77a493bSEric Paris if (length) 1073b77a493bSEric Paris goto out; 10741da177e4SLinus Torvalds 10751da177e4SLinus Torvalds length = security_sid_to_context(newsid, &newcon, &len); 1076b77a493bSEric Paris if (length) 1077b77a493bSEric Paris goto out; 10781da177e4SLinus Torvalds 1079b77a493bSEric Paris length = -ERANGE; 10801da177e4SLinus Torvalds if (len > SIMPLE_TRANSACTION_LIMIT) { 1081744ba35eSEric Paris printk(KERN_ERR "SELinux: %s: context size (%u) exceeds " 1082744ba35eSEric Paris "payload max\n", __func__, len); 1083b77a493bSEric Paris goto out; 10841da177e4SLinus Torvalds } 10851da177e4SLinus Torvalds 10861da177e4SLinus Torvalds memcpy(buf, newcon, len); 10871da177e4SLinus Torvalds length = len; 10881da177e4SLinus Torvalds out: 1089b77a493bSEric Paris kfree(newcon); 1090b77a493bSEric Paris kfree(tcon); 10911da177e4SLinus Torvalds kfree(scon); 10921da177e4SLinus Torvalds return length; 10931da177e4SLinus Torvalds } 10941da177e4SLinus Torvalds 10951da177e4SLinus Torvalds static struct inode *sel_make_inode(struct super_block *sb, int mode) 10961da177e4SLinus Torvalds { 10971da177e4SLinus Torvalds struct inode *ret = new_inode(sb); 10981da177e4SLinus Torvalds 10991da177e4SLinus Torvalds if (ret) { 11001da177e4SLinus Torvalds ret->i_mode = mode; 1101078cd827SDeepa Dinamani ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret); 11021da177e4SLinus Torvalds } 11031da177e4SLinus Torvalds return ret; 11041da177e4SLinus Torvalds } 11051da177e4SLinus Torvalds 11061da177e4SLinus Torvalds static ssize_t sel_read_bool(struct file *filep, char __user *buf, 11071da177e4SLinus Torvalds size_t count, loff_t *ppos) 11081da177e4SLinus Torvalds { 11091da177e4SLinus Torvalds char *page = NULL; 11101da177e4SLinus Torvalds ssize_t length; 11111da177e4SLinus Torvalds ssize_t ret; 11121da177e4SLinus Torvalds int cur_enforcing; 1113496ad9aaSAl Viro unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK; 1114d313f948SStephen Smalley const char *name = filep->f_path.dentry->d_name.name; 11151da177e4SLinus Torvalds 1116bb003079SIngo Molnar mutex_lock(&sel_mutex); 11171da177e4SLinus Torvalds 1118d313f948SStephen Smalley ret = -EINVAL; 1119b77a493bSEric Paris if (index >= bool_num || strcmp(name, bool_pending_names[index])) 1120d313f948SStephen Smalley goto out; 11211da177e4SLinus Torvalds 11221da177e4SLinus Torvalds ret = -ENOMEM; 1123b77a493bSEric Paris page = (char *)get_zeroed_page(GFP_KERNEL); 1124b77a493bSEric Paris if (!page) 11251da177e4SLinus Torvalds goto out; 11261da177e4SLinus Torvalds 1127d313f948SStephen Smalley cur_enforcing = security_get_bool_value(index); 11281da177e4SLinus Torvalds if (cur_enforcing < 0) { 11291da177e4SLinus Torvalds ret = cur_enforcing; 11301da177e4SLinus Torvalds goto out; 11311da177e4SLinus Torvalds } 11321da177e4SLinus Torvalds length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing, 1133d313f948SStephen Smalley bool_pending_values[index]); 113468bdcf28SStephen Smalley ret = simple_read_from_buffer(buf, count, ppos, page, length); 11351da177e4SLinus Torvalds out: 1136bb003079SIngo Molnar mutex_unlock(&sel_mutex); 11371da177e4SLinus Torvalds free_page((unsigned long)page); 11381da177e4SLinus Torvalds return ret; 11391da177e4SLinus Torvalds } 11401da177e4SLinus Torvalds 11411da177e4SLinus Torvalds static ssize_t sel_write_bool(struct file *filep, const char __user *buf, 11421da177e4SLinus Torvalds size_t count, loff_t *ppos) 11431da177e4SLinus Torvalds { 11441da177e4SLinus Torvalds char *page = NULL; 1145d313f948SStephen Smalley ssize_t length; 11461da177e4SLinus Torvalds int new_value; 1147496ad9aaSAl Viro unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK; 1148d313f948SStephen Smalley const char *name = filep->f_path.dentry->d_name.name; 11491da177e4SLinus Torvalds 1150bb003079SIngo Molnar mutex_lock(&sel_mutex); 11511da177e4SLinus Torvalds 1152be0554c9SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 1153be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETBOOL, 1154be0554c9SStephen Smalley NULL); 11551da177e4SLinus Torvalds if (length) 11561da177e4SLinus Torvalds goto out; 11571da177e4SLinus Torvalds 1158d313f948SStephen Smalley length = -EINVAL; 1159b77a493bSEric Paris if (index >= bool_num || strcmp(name, bool_pending_names[index])) 1160d313f948SStephen Smalley goto out; 1161d313f948SStephen Smalley 11621da177e4SLinus Torvalds length = -ENOMEM; 1163b77a493bSEric Paris if (count >= PAGE_SIZE) 11641da177e4SLinus Torvalds goto out; 1165d313f948SStephen Smalley 11661da177e4SLinus Torvalds /* No partial writes. */ 1167d313f948SStephen Smalley length = -EINVAL; 1168b77a493bSEric Paris if (*ppos != 0) 11691da177e4SLinus Torvalds goto out; 1170b77a493bSEric Paris 11718365a719SAl Viro page = memdup_user_nul(buf, count); 11728365a719SAl Viro if (IS_ERR(page)) { 11738365a719SAl Viro length = PTR_ERR(page); 11748365a719SAl Viro page = NULL; 11751da177e4SLinus Torvalds goto out; 11768365a719SAl Viro } 11771da177e4SLinus Torvalds 11781da177e4SLinus Torvalds length = -EINVAL; 11791da177e4SLinus Torvalds if (sscanf(page, "%d", &new_value) != 1) 11801da177e4SLinus Torvalds goto out; 11811da177e4SLinus Torvalds 11821da177e4SLinus Torvalds if (new_value) 11831da177e4SLinus Torvalds new_value = 1; 11841da177e4SLinus Torvalds 1185d313f948SStephen Smalley bool_pending_values[index] = new_value; 11861da177e4SLinus Torvalds length = count; 11871da177e4SLinus Torvalds 11881da177e4SLinus Torvalds out: 1189bb003079SIngo Molnar mutex_unlock(&sel_mutex); 11908365a719SAl Viro kfree(page); 11911da177e4SLinus Torvalds return length; 11921da177e4SLinus Torvalds } 11931da177e4SLinus Torvalds 11949c2e08c5SArjan van de Ven static const struct file_operations sel_bool_ops = { 11951da177e4SLinus Torvalds .read = sel_read_bool, 11961da177e4SLinus Torvalds .write = sel_write_bool, 119757a62c23SArnd Bergmann .llseek = generic_file_llseek, 11981da177e4SLinus Torvalds }; 11991da177e4SLinus Torvalds 12001da177e4SLinus Torvalds static ssize_t sel_commit_bools_write(struct file *filep, 12011da177e4SLinus Torvalds const char __user *buf, 12021da177e4SLinus Torvalds size_t count, loff_t *ppos) 12031da177e4SLinus Torvalds { 12041da177e4SLinus Torvalds char *page = NULL; 1205d313f948SStephen Smalley ssize_t length; 12061da177e4SLinus Torvalds int new_value; 12071da177e4SLinus Torvalds 1208bb003079SIngo Molnar mutex_lock(&sel_mutex); 12091da177e4SLinus Torvalds 1210be0554c9SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 1211be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETBOOL, 1212be0554c9SStephen Smalley NULL); 12131da177e4SLinus Torvalds if (length) 12141da177e4SLinus Torvalds goto out; 12151da177e4SLinus Torvalds 12161da177e4SLinus Torvalds length = -ENOMEM; 1217b77a493bSEric Paris if (count >= PAGE_SIZE) 12181da177e4SLinus Torvalds goto out; 1219b77a493bSEric Paris 12201da177e4SLinus Torvalds /* No partial writes. */ 1221b77a493bSEric Paris length = -EINVAL; 1222b77a493bSEric Paris if (*ppos != 0) 12231da177e4SLinus Torvalds goto out; 1224b77a493bSEric Paris 12258365a719SAl Viro page = memdup_user_nul(buf, count); 12268365a719SAl Viro if (IS_ERR(page)) { 12278365a719SAl Viro length = PTR_ERR(page); 12288365a719SAl Viro page = NULL; 12291da177e4SLinus Torvalds goto out; 12308365a719SAl Viro } 12311da177e4SLinus Torvalds 12321da177e4SLinus Torvalds length = -EINVAL; 12331da177e4SLinus Torvalds if (sscanf(page, "%d", &new_value) != 1) 12341da177e4SLinus Torvalds goto out; 12351da177e4SLinus Torvalds 1236b77a493bSEric Paris length = 0; 12371872981bSEric Paris if (new_value && bool_pending_values) 1238b77a493bSEric Paris length = security_set_bools(bool_num, bool_pending_values); 12391da177e4SLinus Torvalds 1240b77a493bSEric Paris if (!length) 12411da177e4SLinus Torvalds length = count; 12421da177e4SLinus Torvalds 12431da177e4SLinus Torvalds out: 1244bb003079SIngo Molnar mutex_unlock(&sel_mutex); 12458365a719SAl Viro kfree(page); 12461da177e4SLinus Torvalds return length; 12471da177e4SLinus Torvalds } 12481da177e4SLinus Torvalds 12499c2e08c5SArjan van de Ven static const struct file_operations sel_commit_bools_ops = { 12501da177e4SLinus Torvalds .write = sel_commit_bools_write, 125157a62c23SArnd Bergmann .llseek = generic_file_llseek, 12521da177e4SLinus Torvalds }; 12531da177e4SLinus Torvalds 12540c92d7c7SChristopher J. PeBenito static void sel_remove_entries(struct dentry *de) 12551da177e4SLinus Torvalds { 1256ad52184bSAl Viro d_genocide(de); 1257ad52184bSAl Viro shrink_dcache_parent(de); 12581da177e4SLinus Torvalds } 12591da177e4SLinus Torvalds 12601da177e4SLinus Torvalds #define BOOL_DIR_NAME "booleans" 12611da177e4SLinus Torvalds 12621da177e4SLinus Torvalds static int sel_make_bools(void) 12631da177e4SLinus Torvalds { 1264b77a493bSEric Paris int i, ret; 12651da177e4SLinus Torvalds ssize_t len; 12661da177e4SLinus Torvalds struct dentry *dentry = NULL; 12671da177e4SLinus Torvalds struct dentry *dir = bool_dir; 12681da177e4SLinus Torvalds struct inode *inode = NULL; 12691da177e4SLinus Torvalds struct inode_security_struct *isec; 12701da177e4SLinus Torvalds char **names = NULL, *page; 12711da177e4SLinus Torvalds int num; 12721da177e4SLinus Torvalds int *values = NULL; 12731da177e4SLinus Torvalds u32 sid; 12741da177e4SLinus Torvalds 12751da177e4SLinus Torvalds /* remove any existing files */ 12768007f102SXiaotian Feng for (i = 0; i < bool_num; i++) 12778007f102SXiaotian Feng kfree(bool_pending_names[i]); 1278d313f948SStephen Smalley kfree(bool_pending_names); 12791da177e4SLinus Torvalds kfree(bool_pending_values); 1280154c50caSEric Paris bool_num = 0; 1281d313f948SStephen Smalley bool_pending_names = NULL; 128220c19e41SDavi Arnaut bool_pending_values = NULL; 12831da177e4SLinus Torvalds 12840c92d7c7SChristopher J. PeBenito sel_remove_entries(dir); 12851da177e4SLinus Torvalds 1286b77a493bSEric Paris ret = -ENOMEM; 12871872981bSEric Paris page = (char *)get_zeroed_page(GFP_KERNEL); 12881872981bSEric Paris if (!page) 1289b77a493bSEric Paris goto out; 12901da177e4SLinus Torvalds 12911da177e4SLinus Torvalds ret = security_get_bools(&num, &names, &values); 1292b77a493bSEric Paris if (ret) 12931da177e4SLinus Torvalds goto out; 12941da177e4SLinus Torvalds 12951da177e4SLinus Torvalds for (i = 0; i < num; i++) { 1296b77a493bSEric Paris ret = -ENOMEM; 12971da177e4SLinus Torvalds dentry = d_alloc_name(dir, names[i]); 1298b77a493bSEric Paris if (!dentry) 1299b77a493bSEric Paris goto out; 13001da177e4SLinus Torvalds 1301b77a493bSEric Paris ret = -ENOMEM; 1302b77a493bSEric Paris inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR); 1303b77a493bSEric Paris if (!inode) 1304b77a493bSEric Paris goto out; 1305b77a493bSEric Paris 13061da177e4SLinus Torvalds ret = -ENAMETOOLONG; 1307cc1dad71SAl Viro len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]); 1308b77a493bSEric Paris if (len >= PAGE_SIZE) 1309b77a493bSEric Paris goto out; 1310b77a493bSEric Paris 13111da177e4SLinus Torvalds isec = (struct inode_security_struct *)inode->i_security; 13121872981bSEric Paris ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid); 13134262fb51SGary Tierney if (ret) { 1314900fde06SGary Tierney pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n", 1315900fde06SGary Tierney page); 1316900fde06SGary Tierney sid = SECINITSID_SECURITY; 13174262fb51SGary Tierney } 13184262fb51SGary Tierney 13191da177e4SLinus Torvalds isec->sid = sid; 132042059112SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 13211da177e4SLinus Torvalds inode->i_fop = &sel_bool_ops; 1322bce34bc0SJames Carter inode->i_ino = i|SEL_BOOL_INO_OFFSET; 13231da177e4SLinus Torvalds d_add(dentry, inode); 13241da177e4SLinus Torvalds } 13251da177e4SLinus Torvalds bool_num = num; 1326d313f948SStephen Smalley bool_pending_names = names; 13271da177e4SLinus Torvalds bool_pending_values = values; 1328b77a493bSEric Paris 1329b77a493bSEric Paris free_page((unsigned long)page); 1330b77a493bSEric Paris return 0; 13311da177e4SLinus Torvalds out: 13321da177e4SLinus Torvalds free_page((unsigned long)page); 1333b77a493bSEric Paris 13341da177e4SLinus Torvalds if (names) { 13359a5f04bfSJesper Juhl for (i = 0; i < num; i++) 13361da177e4SLinus Torvalds kfree(names[i]); 13371da177e4SLinus Torvalds kfree(names); 13381da177e4SLinus Torvalds } 133920c19e41SDavi Arnaut kfree(values); 13400c92d7c7SChristopher J. PeBenito sel_remove_entries(dir); 1341b77a493bSEric Paris 1342b77a493bSEric Paris return ret; 13431da177e4SLinus Torvalds } 13441da177e4SLinus Torvalds 13451da177e4SLinus Torvalds #define NULL_FILE_NAME "null" 13461da177e4SLinus Torvalds 1347765927b2SAl Viro struct path selinux_null; 13481da177e4SLinus Torvalds 13491da177e4SLinus Torvalds static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf, 13501da177e4SLinus Torvalds size_t count, loff_t *ppos) 13511da177e4SLinus Torvalds { 13521da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 13531da177e4SLinus Torvalds ssize_t length; 13541da177e4SLinus Torvalds 13551da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold); 13561da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 13571da177e4SLinus Torvalds } 13581da177e4SLinus Torvalds 13591da177e4SLinus Torvalds static ssize_t sel_write_avc_cache_threshold(struct file *file, 13601da177e4SLinus Torvalds const char __user *buf, 13611da177e4SLinus Torvalds size_t count, loff_t *ppos) 13621da177e4SLinus Torvalds 13631da177e4SLinus Torvalds { 13648365a719SAl Viro char *page; 13651da177e4SLinus Torvalds ssize_t ret; 1366309c5fadSHeinrich Schuchardt unsigned int new_value; 13671da177e4SLinus Torvalds 1368be0554c9SStephen Smalley ret = avc_has_perm(current_sid(), SECINITSID_SECURITY, 1369be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETSECPARAM, 1370be0554c9SStephen Smalley NULL); 13711da177e4SLinus Torvalds if (ret) 13728365a719SAl Viro return ret; 1373b77a493bSEric Paris 1374b77a493bSEric Paris if (count >= PAGE_SIZE) 13758365a719SAl Viro return -ENOMEM; 1376b77a493bSEric Paris 1377b77a493bSEric Paris /* No partial writes. */ 1378b77a493bSEric Paris if (*ppos != 0) 13798365a719SAl Viro return -EINVAL; 1380b77a493bSEric Paris 13818365a719SAl Viro page = memdup_user_nul(buf, count); 13828365a719SAl Viro if (IS_ERR(page)) 13838365a719SAl Viro return PTR_ERR(page); 1384b77a493bSEric Paris 1385b77a493bSEric Paris ret = -EINVAL; 1386b77a493bSEric Paris if (sscanf(page, "%u", &new_value) != 1) 1387b77a493bSEric Paris goto out; 1388b77a493bSEric Paris 13891da177e4SLinus Torvalds avc_cache_threshold = new_value; 1390b77a493bSEric Paris 13911da177e4SLinus Torvalds ret = count; 13921da177e4SLinus Torvalds out: 13938365a719SAl Viro kfree(page); 13941da177e4SLinus Torvalds return ret; 13951da177e4SLinus Torvalds } 13961da177e4SLinus Torvalds 13971da177e4SLinus Torvalds static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf, 13981da177e4SLinus Torvalds size_t count, loff_t *ppos) 13991da177e4SLinus Torvalds { 14001da177e4SLinus Torvalds char *page; 1401b77a493bSEric Paris ssize_t length; 14021da177e4SLinus Torvalds 14031da177e4SLinus Torvalds page = (char *)__get_free_page(GFP_KERNEL); 1404b77a493bSEric Paris if (!page) 1405b77a493bSEric Paris return -ENOMEM; 1406b77a493bSEric Paris 1407b77a493bSEric Paris length = avc_get_hash_stats(page); 1408b77a493bSEric Paris if (length >= 0) 1409b77a493bSEric Paris length = simple_read_from_buffer(buf, count, ppos, page, length); 14101da177e4SLinus Torvalds free_page((unsigned long)page); 1411b77a493bSEric Paris 1412b77a493bSEric Paris return length; 14131da177e4SLinus Torvalds } 14141da177e4SLinus Torvalds 14159c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_threshold_ops = { 14161da177e4SLinus Torvalds .read = sel_read_avc_cache_threshold, 14171da177e4SLinus Torvalds .write = sel_write_avc_cache_threshold, 141857a62c23SArnd Bergmann .llseek = generic_file_llseek, 14191da177e4SLinus Torvalds }; 14201da177e4SLinus Torvalds 14219c2e08c5SArjan van de Ven static const struct file_operations sel_avc_hash_stats_ops = { 14221da177e4SLinus Torvalds .read = sel_read_avc_hash_stats, 142357a62c23SArnd Bergmann .llseek = generic_file_llseek, 14241da177e4SLinus Torvalds }; 14251da177e4SLinus Torvalds 14261da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS 14271da177e4SLinus Torvalds static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx) 14281da177e4SLinus Torvalds { 14291da177e4SLinus Torvalds int cpu; 14301da177e4SLinus Torvalds 14314f4b6c1aSRusty Russell for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) { 14321da177e4SLinus Torvalds if (!cpu_possible(cpu)) 14331da177e4SLinus Torvalds continue; 14341da177e4SLinus Torvalds *idx = cpu + 1; 14351da177e4SLinus Torvalds return &per_cpu(avc_cache_stats, cpu); 14361da177e4SLinus Torvalds } 14371da177e4SLinus Torvalds return NULL; 14381da177e4SLinus Torvalds } 14391da177e4SLinus Torvalds 14401da177e4SLinus Torvalds static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos) 14411da177e4SLinus Torvalds { 14421da177e4SLinus Torvalds loff_t n = *pos - 1; 14431da177e4SLinus Torvalds 14441da177e4SLinus Torvalds if (*pos == 0) 14451da177e4SLinus Torvalds return SEQ_START_TOKEN; 14461da177e4SLinus Torvalds 14471da177e4SLinus Torvalds return sel_avc_get_stat_idx(&n); 14481da177e4SLinus Torvalds } 14491da177e4SLinus Torvalds 14501da177e4SLinus Torvalds static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos) 14511da177e4SLinus Torvalds { 14521da177e4SLinus Torvalds return sel_avc_get_stat_idx(pos); 14531da177e4SLinus Torvalds } 14541da177e4SLinus Torvalds 14551da177e4SLinus Torvalds static int sel_avc_stats_seq_show(struct seq_file *seq, void *v) 14561da177e4SLinus Torvalds { 14571da177e4SLinus Torvalds struct avc_cache_stats *st = v; 14581da177e4SLinus Torvalds 14591da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) 14601da177e4SLinus Torvalds seq_printf(seq, "lookups hits misses allocations reclaims " 14611da177e4SLinus Torvalds "frees\n"); 1462257313b2SLinus Torvalds else { 1463257313b2SLinus Torvalds unsigned int lookups = st->lookups; 1464257313b2SLinus Torvalds unsigned int misses = st->misses; 1465257313b2SLinus Torvalds unsigned int hits = lookups - misses; 1466257313b2SLinus Torvalds seq_printf(seq, "%u %u %u %u %u %u\n", lookups, 1467257313b2SLinus Torvalds hits, misses, st->allocations, 14681da177e4SLinus Torvalds st->reclaims, st->frees); 1469257313b2SLinus Torvalds } 14701da177e4SLinus Torvalds return 0; 14711da177e4SLinus Torvalds } 14721da177e4SLinus Torvalds 14731da177e4SLinus Torvalds static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v) 14741da177e4SLinus Torvalds { } 14751da177e4SLinus Torvalds 14761996a109SJan Engelhardt static const struct seq_operations sel_avc_cache_stats_seq_ops = { 14771da177e4SLinus Torvalds .start = sel_avc_stats_seq_start, 14781da177e4SLinus Torvalds .next = sel_avc_stats_seq_next, 14791da177e4SLinus Torvalds .show = sel_avc_stats_seq_show, 14801da177e4SLinus Torvalds .stop = sel_avc_stats_seq_stop, 14811da177e4SLinus Torvalds }; 14821da177e4SLinus Torvalds 14831da177e4SLinus Torvalds static int sel_open_avc_cache_stats(struct inode *inode, struct file *file) 14841da177e4SLinus Torvalds { 14851da177e4SLinus Torvalds return seq_open(file, &sel_avc_cache_stats_seq_ops); 14861da177e4SLinus Torvalds } 14871da177e4SLinus Torvalds 14889c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_stats_ops = { 14891da177e4SLinus Torvalds .open = sel_open_avc_cache_stats, 14901da177e4SLinus Torvalds .read = seq_read, 14911da177e4SLinus Torvalds .llseek = seq_lseek, 14921da177e4SLinus Torvalds .release = seq_release, 14931da177e4SLinus Torvalds }; 14941da177e4SLinus Torvalds #endif 14951da177e4SLinus Torvalds 14961da177e4SLinus Torvalds static int sel_make_avc_files(struct dentry *dir) 14971da177e4SLinus Torvalds { 1498b77a493bSEric Paris int i; 14991da177e4SLinus Torvalds static struct tree_descr files[] = { 15001da177e4SLinus Torvalds { "cache_threshold", 15011da177e4SLinus Torvalds &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR }, 15021da177e4SLinus Torvalds { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO }, 15031da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS 15041da177e4SLinus Torvalds { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO }, 15051da177e4SLinus Torvalds #endif 15061da177e4SLinus Torvalds }; 15071da177e4SLinus Torvalds 15086e20a64aSNicolas Kaiser for (i = 0; i < ARRAY_SIZE(files); i++) { 15091da177e4SLinus Torvalds struct inode *inode; 15101da177e4SLinus Torvalds struct dentry *dentry; 15111da177e4SLinus Torvalds 15121da177e4SLinus Torvalds dentry = d_alloc_name(dir, files[i].name); 1513b77a493bSEric Paris if (!dentry) 1514b77a493bSEric Paris return -ENOMEM; 15151da177e4SLinus Torvalds 15161da177e4SLinus Torvalds inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode); 1517b77a493bSEric Paris if (!inode) 1518b77a493bSEric Paris return -ENOMEM; 1519b77a493bSEric Paris 15201da177e4SLinus Torvalds inode->i_fop = files[i].ops; 15216174eafcSJames Carter inode->i_ino = ++sel_last_ino; 15221da177e4SLinus Torvalds d_add(dentry, inode); 15231da177e4SLinus Torvalds } 1524b77a493bSEric Paris 1525b77a493bSEric Paris return 0; 15261da177e4SLinus Torvalds } 15271da177e4SLinus Torvalds 1528f0ee2e46SJames Carter static ssize_t sel_read_initcon(struct file *file, char __user *buf, 1529f0ee2e46SJames Carter size_t count, loff_t *ppos) 1530f0ee2e46SJames Carter { 1531f0ee2e46SJames Carter char *con; 1532f0ee2e46SJames Carter u32 sid, len; 1533f0ee2e46SJames Carter ssize_t ret; 1534f0ee2e46SJames Carter 1535496ad9aaSAl Viro sid = file_inode(file)->i_ino&SEL_INO_MASK; 1536f0ee2e46SJames Carter ret = security_sid_to_context(sid, &con, &len); 1537b77a493bSEric Paris if (ret) 1538f0ee2e46SJames Carter return ret; 1539f0ee2e46SJames Carter 1540f0ee2e46SJames Carter ret = simple_read_from_buffer(buf, count, ppos, con, len); 1541f0ee2e46SJames Carter kfree(con); 1542f0ee2e46SJames Carter return ret; 1543f0ee2e46SJames Carter } 1544f0ee2e46SJames Carter 1545f0ee2e46SJames Carter static const struct file_operations sel_initcon_ops = { 1546f0ee2e46SJames Carter .read = sel_read_initcon, 154757a62c23SArnd Bergmann .llseek = generic_file_llseek, 1548f0ee2e46SJames Carter }; 1549f0ee2e46SJames Carter 1550f0ee2e46SJames Carter static int sel_make_initcon_files(struct dentry *dir) 1551f0ee2e46SJames Carter { 1552b77a493bSEric Paris int i; 1553f0ee2e46SJames Carter 1554f0ee2e46SJames Carter for (i = 1; i <= SECINITSID_NUM; i++) { 1555f0ee2e46SJames Carter struct inode *inode; 1556f0ee2e46SJames Carter struct dentry *dentry; 1557f0ee2e46SJames Carter dentry = d_alloc_name(dir, security_get_initial_sid_context(i)); 1558b77a493bSEric Paris if (!dentry) 1559b77a493bSEric Paris return -ENOMEM; 1560f0ee2e46SJames Carter 1561f0ee2e46SJames Carter inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); 1562b77a493bSEric Paris if (!inode) 1563b77a493bSEric Paris return -ENOMEM; 1564b77a493bSEric Paris 1565f0ee2e46SJames Carter inode->i_fop = &sel_initcon_ops; 1566f0ee2e46SJames Carter inode->i_ino = i|SEL_INITCON_INO_OFFSET; 1567f0ee2e46SJames Carter d_add(dentry, inode); 1568f0ee2e46SJames Carter } 1569b77a493bSEric Paris 1570b77a493bSEric Paris return 0; 1571f0ee2e46SJames Carter } 1572f0ee2e46SJames Carter 1573e47c8fc5SChristopher J. PeBenito static inline unsigned long sel_class_to_ino(u16 class) 1574e47c8fc5SChristopher J. PeBenito { 1575e47c8fc5SChristopher J. PeBenito return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET; 1576e47c8fc5SChristopher J. PeBenito } 1577e47c8fc5SChristopher J. PeBenito 1578e47c8fc5SChristopher J. PeBenito static inline u16 sel_ino_to_class(unsigned long ino) 1579e47c8fc5SChristopher J. PeBenito { 158092ae9e82SEric Paris return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1); 1581e47c8fc5SChristopher J. PeBenito } 1582e47c8fc5SChristopher J. PeBenito 1583e47c8fc5SChristopher J. PeBenito static inline unsigned long sel_perm_to_ino(u16 class, u32 perm) 1584e47c8fc5SChristopher J. PeBenito { 1585e47c8fc5SChristopher J. PeBenito return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET; 1586e47c8fc5SChristopher J. PeBenito } 1587e47c8fc5SChristopher J. PeBenito 1588e47c8fc5SChristopher J. PeBenito static inline u32 sel_ino_to_perm(unsigned long ino) 1589e47c8fc5SChristopher J. PeBenito { 1590e47c8fc5SChristopher J. PeBenito return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1); 1591e47c8fc5SChristopher J. PeBenito } 1592e47c8fc5SChristopher J. PeBenito 1593e47c8fc5SChristopher J. PeBenito static ssize_t sel_read_class(struct file *file, char __user *buf, 1594e47c8fc5SChristopher J. PeBenito size_t count, loff_t *ppos) 1595e47c8fc5SChristopher J. PeBenito { 1596496ad9aaSAl Viro unsigned long ino = file_inode(file)->i_ino; 1597cc1dad71SAl Viro char res[TMPBUFLEN]; 1598cc1dad71SAl Viro ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_class(ino)); 1599cc1dad71SAl Viro return simple_read_from_buffer(buf, count, ppos, res, len); 1600e47c8fc5SChristopher J. PeBenito } 1601e47c8fc5SChristopher J. PeBenito 1602e47c8fc5SChristopher J. PeBenito static const struct file_operations sel_class_ops = { 1603e47c8fc5SChristopher J. PeBenito .read = sel_read_class, 160457a62c23SArnd Bergmann .llseek = generic_file_llseek, 1605e47c8fc5SChristopher J. PeBenito }; 1606e47c8fc5SChristopher J. PeBenito 1607e47c8fc5SChristopher J. PeBenito static ssize_t sel_read_perm(struct file *file, char __user *buf, 1608e47c8fc5SChristopher J. PeBenito size_t count, loff_t *ppos) 1609e47c8fc5SChristopher J. PeBenito { 1610496ad9aaSAl Viro unsigned long ino = file_inode(file)->i_ino; 1611cc1dad71SAl Viro char res[TMPBUFLEN]; 1612cc1dad71SAl Viro ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino)); 1613cc1dad71SAl Viro return simple_read_from_buffer(buf, count, ppos, res, len); 1614e47c8fc5SChristopher J. PeBenito } 1615e47c8fc5SChristopher J. PeBenito 1616e47c8fc5SChristopher J. PeBenito static const struct file_operations sel_perm_ops = { 1617e47c8fc5SChristopher J. PeBenito .read = sel_read_perm, 161857a62c23SArnd Bergmann .llseek = generic_file_llseek, 1619e47c8fc5SChristopher J. PeBenito }; 1620e47c8fc5SChristopher J. PeBenito 16213bb56b25SPaul Moore static ssize_t sel_read_policycap(struct file *file, char __user *buf, 16223bb56b25SPaul Moore size_t count, loff_t *ppos) 16233bb56b25SPaul Moore { 16243bb56b25SPaul Moore int value; 16253bb56b25SPaul Moore char tmpbuf[TMPBUFLEN]; 16263bb56b25SPaul Moore ssize_t length; 1627496ad9aaSAl Viro unsigned long i_ino = file_inode(file)->i_ino; 16283bb56b25SPaul Moore 16293bb56b25SPaul Moore value = security_policycap_supported(i_ino & SEL_INO_MASK); 16303bb56b25SPaul Moore length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value); 16313bb56b25SPaul Moore 16323bb56b25SPaul Moore return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 16333bb56b25SPaul Moore } 16343bb56b25SPaul Moore 16353bb56b25SPaul Moore static const struct file_operations sel_policycap_ops = { 16363bb56b25SPaul Moore .read = sel_read_policycap, 163757a62c23SArnd Bergmann .llseek = generic_file_llseek, 16383bb56b25SPaul Moore }; 16393bb56b25SPaul Moore 1640e47c8fc5SChristopher J. PeBenito static int sel_make_perm_files(char *objclass, int classvalue, 1641e47c8fc5SChristopher J. PeBenito struct dentry *dir) 1642e47c8fc5SChristopher J. PeBenito { 1643b77a493bSEric Paris int i, rc, nperms; 1644e47c8fc5SChristopher J. PeBenito char **perms; 1645e47c8fc5SChristopher J. PeBenito 1646e47c8fc5SChristopher J. PeBenito rc = security_get_permissions(objclass, &perms, &nperms); 1647e47c8fc5SChristopher J. PeBenito if (rc) 1648b77a493bSEric Paris return rc; 1649e47c8fc5SChristopher J. PeBenito 1650e47c8fc5SChristopher J. PeBenito for (i = 0; i < nperms; i++) { 1651e47c8fc5SChristopher J. PeBenito struct inode *inode; 1652e47c8fc5SChristopher J. PeBenito struct dentry *dentry; 1653e47c8fc5SChristopher J. PeBenito 1654b77a493bSEric Paris rc = -ENOMEM; 1655e47c8fc5SChristopher J. PeBenito dentry = d_alloc_name(dir, perms[i]); 1656b77a493bSEric Paris if (!dentry) 1657b77a493bSEric Paris goto out; 1658e47c8fc5SChristopher J. PeBenito 1659e47c8fc5SChristopher J. PeBenito rc = -ENOMEM; 1660b77a493bSEric Paris inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); 1661b77a493bSEric Paris if (!inode) 1662b77a493bSEric Paris goto out; 1663b77a493bSEric Paris 1664e47c8fc5SChristopher J. PeBenito inode->i_fop = &sel_perm_ops; 1665e47c8fc5SChristopher J. PeBenito /* i+1 since perm values are 1-indexed */ 1666e47c8fc5SChristopher J. PeBenito inode->i_ino = sel_perm_to_ino(classvalue, i + 1); 1667e47c8fc5SChristopher J. PeBenito d_add(dentry, inode); 1668e47c8fc5SChristopher J. PeBenito } 1669b77a493bSEric Paris rc = 0; 1670b77a493bSEric Paris out: 1671e47c8fc5SChristopher J. PeBenito for (i = 0; i < nperms; i++) 1672e47c8fc5SChristopher J. PeBenito kfree(perms[i]); 1673e47c8fc5SChristopher J. PeBenito kfree(perms); 1674e47c8fc5SChristopher J. PeBenito return rc; 1675e47c8fc5SChristopher J. PeBenito } 1676e47c8fc5SChristopher J. PeBenito 1677e47c8fc5SChristopher J. PeBenito static int sel_make_class_dir_entries(char *classname, int index, 1678e47c8fc5SChristopher J. PeBenito struct dentry *dir) 1679e47c8fc5SChristopher J. PeBenito { 1680e47c8fc5SChristopher J. PeBenito struct dentry *dentry = NULL; 1681e47c8fc5SChristopher J. PeBenito struct inode *inode = NULL; 1682e47c8fc5SChristopher J. PeBenito int rc; 1683e47c8fc5SChristopher J. PeBenito 1684e47c8fc5SChristopher J. PeBenito dentry = d_alloc_name(dir, "index"); 1685b77a493bSEric Paris if (!dentry) 1686b77a493bSEric Paris return -ENOMEM; 1687e47c8fc5SChristopher J. PeBenito 1688e47c8fc5SChristopher J. PeBenito inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); 1689b77a493bSEric Paris if (!inode) 1690b77a493bSEric Paris return -ENOMEM; 1691e47c8fc5SChristopher J. PeBenito 1692e47c8fc5SChristopher J. PeBenito inode->i_fop = &sel_class_ops; 1693e47c8fc5SChristopher J. PeBenito inode->i_ino = sel_class_to_ino(index); 1694e47c8fc5SChristopher J. PeBenito d_add(dentry, inode); 1695e47c8fc5SChristopher J. PeBenito 1696a1c2aa1eSAl Viro dentry = sel_make_dir(dir, "perms", &last_class_ino); 1697a1c2aa1eSAl Viro if (IS_ERR(dentry)) 1698a1c2aa1eSAl Viro return PTR_ERR(dentry); 1699e47c8fc5SChristopher J. PeBenito 1700e47c8fc5SChristopher J. PeBenito rc = sel_make_perm_files(classname, index, dentry); 1701e47c8fc5SChristopher J. PeBenito 1702e47c8fc5SChristopher J. PeBenito return rc; 1703e47c8fc5SChristopher J. PeBenito } 1704e47c8fc5SChristopher J. PeBenito 1705e47c8fc5SChristopher J. PeBenito static int sel_make_classes(void) 1706e47c8fc5SChristopher J. PeBenito { 1707b77a493bSEric Paris int rc, nclasses, i; 1708e47c8fc5SChristopher J. PeBenito char **classes; 1709e47c8fc5SChristopher J. PeBenito 1710e47c8fc5SChristopher J. PeBenito /* delete any existing entries */ 1711ad52184bSAl Viro sel_remove_entries(class_dir); 1712e47c8fc5SChristopher J. PeBenito 1713e47c8fc5SChristopher J. PeBenito rc = security_get_classes(&classes, &nclasses); 1714b77a493bSEric Paris if (rc) 1715b77a493bSEric Paris return rc; 1716e47c8fc5SChristopher J. PeBenito 1717e47c8fc5SChristopher J. PeBenito /* +2 since classes are 1-indexed */ 1718e47c8fc5SChristopher J. PeBenito last_class_ino = sel_class_to_ino(nclasses + 2); 1719e47c8fc5SChristopher J. PeBenito 1720e47c8fc5SChristopher J. PeBenito for (i = 0; i < nclasses; i++) { 1721e47c8fc5SChristopher J. PeBenito struct dentry *class_name_dir; 1722e47c8fc5SChristopher J. PeBenito 1723a1c2aa1eSAl Viro class_name_dir = sel_make_dir(class_dir, classes[i], 1724e47c8fc5SChristopher J. PeBenito &last_class_ino); 1725a1c2aa1eSAl Viro if (IS_ERR(class_name_dir)) { 1726a1c2aa1eSAl Viro rc = PTR_ERR(class_name_dir); 1727b77a493bSEric Paris goto out; 1728a1c2aa1eSAl Viro } 1729e47c8fc5SChristopher J. PeBenito 1730e47c8fc5SChristopher J. PeBenito /* i+1 since class values are 1-indexed */ 1731e47c8fc5SChristopher J. PeBenito rc = sel_make_class_dir_entries(classes[i], i + 1, 1732e47c8fc5SChristopher J. PeBenito class_name_dir); 1733e47c8fc5SChristopher J. PeBenito if (rc) 1734b77a493bSEric Paris goto out; 1735e47c8fc5SChristopher J. PeBenito } 1736b77a493bSEric Paris rc = 0; 1737b77a493bSEric Paris out: 1738e47c8fc5SChristopher J. PeBenito for (i = 0; i < nclasses; i++) 1739e47c8fc5SChristopher J. PeBenito kfree(classes[i]); 1740e47c8fc5SChristopher J. PeBenito kfree(classes); 1741e47c8fc5SChristopher J. PeBenito return rc; 1742e47c8fc5SChristopher J. PeBenito } 1743e47c8fc5SChristopher J. PeBenito 17443bb56b25SPaul Moore static int sel_make_policycap(void) 17453bb56b25SPaul Moore { 17463bb56b25SPaul Moore unsigned int iter; 17473bb56b25SPaul Moore struct dentry *dentry = NULL; 17483bb56b25SPaul Moore struct inode *inode = NULL; 17493bb56b25SPaul Moore 17503bb56b25SPaul Moore sel_remove_entries(policycap_dir); 17513bb56b25SPaul Moore 17523bb56b25SPaul Moore for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) { 17533bb56b25SPaul Moore if (iter < ARRAY_SIZE(policycap_names)) 17543bb56b25SPaul Moore dentry = d_alloc_name(policycap_dir, 17553bb56b25SPaul Moore policycap_names[iter]); 17563bb56b25SPaul Moore else 17573bb56b25SPaul Moore dentry = d_alloc_name(policycap_dir, "unknown"); 17583bb56b25SPaul Moore 17593bb56b25SPaul Moore if (dentry == NULL) 17603bb56b25SPaul Moore return -ENOMEM; 17613bb56b25SPaul Moore 17623bb56b25SPaul Moore inode = sel_make_inode(policycap_dir->d_sb, S_IFREG | S_IRUGO); 17633bb56b25SPaul Moore if (inode == NULL) 17643bb56b25SPaul Moore return -ENOMEM; 17653bb56b25SPaul Moore 17663bb56b25SPaul Moore inode->i_fop = &sel_policycap_ops; 17673bb56b25SPaul Moore inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET; 17683bb56b25SPaul Moore d_add(dentry, inode); 17693bb56b25SPaul Moore } 17703bb56b25SPaul Moore 17713bb56b25SPaul Moore return 0; 17723bb56b25SPaul Moore } 17733bb56b25SPaul Moore 1774a1c2aa1eSAl Viro static struct dentry *sel_make_dir(struct dentry *dir, const char *name, 17750dd4ae51SChristopher J. PeBenito unsigned long *ino) 17761da177e4SLinus Torvalds { 1777a1c2aa1eSAl Viro struct dentry *dentry = d_alloc_name(dir, name); 17781da177e4SLinus Torvalds struct inode *inode; 17791da177e4SLinus Torvalds 1780a1c2aa1eSAl Viro if (!dentry) 1781a1c2aa1eSAl Viro return ERR_PTR(-ENOMEM); 1782a1c2aa1eSAl Viro 1783a1c2aa1eSAl Viro inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO); 1784a1c2aa1eSAl Viro if (!inode) { 1785a1c2aa1eSAl Viro dput(dentry); 1786a1c2aa1eSAl Viro return ERR_PTR(-ENOMEM); 1787a1c2aa1eSAl Viro } 1788b77a493bSEric Paris 17891da177e4SLinus Torvalds inode->i_op = &simple_dir_inode_operations; 17901da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations; 17910dd4ae51SChristopher J. PeBenito inode->i_ino = ++(*ino); 179240e906f8SJames Morris /* directory inodes start off with i_nlink == 2 (for "." entry) */ 1793d8c76e6fSDave Hansen inc_nlink(inode); 17941da177e4SLinus Torvalds d_add(dentry, inode); 1795edb20fb5SJames Morris /* bump link count on parent directory, too */ 1796ce0b16ddSDavid Howells inc_nlink(d_inode(dir)); 1797b77a493bSEric Paris 1798a1c2aa1eSAl Viro return dentry; 17991da177e4SLinus Torvalds } 18001da177e4SLinus Torvalds 18011da177e4SLinus Torvalds static int sel_fill_super(struct super_block *sb, void *data, int silent) 18021da177e4SLinus Torvalds { 18031da177e4SLinus Torvalds int ret; 18041da177e4SLinus Torvalds struct dentry *dentry; 1805a1c2aa1eSAl Viro struct inode *inode; 18061da177e4SLinus Torvalds struct inode_security_struct *isec; 18071da177e4SLinus Torvalds 18081da177e4SLinus Torvalds static struct tree_descr selinux_files[] = { 18091da177e4SLinus Torvalds [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR}, 18101da177e4SLinus Torvalds [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR}, 1811ce9982d0SStephen Smalley [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO}, 18121da177e4SLinus Torvalds [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO}, 18131da177e4SLinus Torvalds [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO}, 18141da177e4SLinus Torvalds [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO}, 18151da177e4SLinus Torvalds [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO}, 18161da177e4SLinus Torvalds [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO}, 18171da177e4SLinus Torvalds [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR}, 18181da177e4SLinus Torvalds [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO}, 18191da177e4SLinus Torvalds [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR}, 18201da177e4SLinus Torvalds [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO}, 18211da177e4SLinus Torvalds [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR}, 18223f12070eSEric Paris [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO}, 18233f12070eSEric Paris [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO}, 182411904167SKaiGai Kohei [SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO}, 182572e8c859SEric Paris [SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO}, 1826f9df6458SAndrew Perepechko [SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops, 1827f9df6458SAndrew Perepechko S_IWUGO}, 18281da177e4SLinus Torvalds /* last one */ {""} 18291da177e4SLinus Torvalds }; 18301da177e4SLinus Torvalds ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files); 18311da177e4SLinus Torvalds if (ret) 1832161ce45aSJames Morris goto err; 18331da177e4SLinus Torvalds 1834a1c2aa1eSAl Viro bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &sel_last_ino); 1835a1c2aa1eSAl Viro if (IS_ERR(bool_dir)) { 1836a1c2aa1eSAl Viro ret = PTR_ERR(bool_dir); 1837a1c2aa1eSAl Viro bool_dir = NULL; 1838161ce45aSJames Morris goto err; 1839a1c2aa1eSAl Viro } 18401da177e4SLinus Torvalds 1841b77a493bSEric Paris ret = -ENOMEM; 18421da177e4SLinus Torvalds dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME); 1843b77a493bSEric Paris if (!dentry) 1844161ce45aSJames Morris goto err; 18451da177e4SLinus Torvalds 1846161ce45aSJames Morris ret = -ENOMEM; 1847b77a493bSEric Paris inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO); 1848b77a493bSEric Paris if (!inode) 1849161ce45aSJames Morris goto err; 1850b77a493bSEric Paris 18516174eafcSJames Carter inode->i_ino = ++sel_last_ino; 18521da177e4SLinus Torvalds isec = (struct inode_security_struct *)inode->i_security; 18531da177e4SLinus Torvalds isec->sid = SECINITSID_DEVNULL; 18541da177e4SLinus Torvalds isec->sclass = SECCLASS_CHR_FILE; 185542059112SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 18561da177e4SLinus Torvalds 18571da177e4SLinus Torvalds init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3)); 18581da177e4SLinus Torvalds d_add(dentry, inode); 1859765927b2SAl Viro selinux_null.dentry = dentry; 18601da177e4SLinus Torvalds 1861a1c2aa1eSAl Viro dentry = sel_make_dir(sb->s_root, "avc", &sel_last_ino); 1862a1c2aa1eSAl Viro if (IS_ERR(dentry)) { 1863a1c2aa1eSAl Viro ret = PTR_ERR(dentry); 1864161ce45aSJames Morris goto err; 1865a1c2aa1eSAl Viro } 18661da177e4SLinus Torvalds 18671da177e4SLinus Torvalds ret = sel_make_avc_files(dentry); 18681da177e4SLinus Torvalds if (ret) 1869161ce45aSJames Morris goto err; 1870f0ee2e46SJames Carter 1871a1c2aa1eSAl Viro dentry = sel_make_dir(sb->s_root, "initial_contexts", &sel_last_ino); 1872a1c2aa1eSAl Viro if (IS_ERR(dentry)) { 1873a1c2aa1eSAl Viro ret = PTR_ERR(dentry); 1874f0ee2e46SJames Carter goto err; 1875a1c2aa1eSAl Viro } 1876f0ee2e46SJames Carter 1877f0ee2e46SJames Carter ret = sel_make_initcon_files(dentry); 1878f0ee2e46SJames Carter if (ret) 1879f0ee2e46SJames Carter goto err; 1880f0ee2e46SJames Carter 1881a1c2aa1eSAl Viro class_dir = sel_make_dir(sb->s_root, "class", &sel_last_ino); 1882a1c2aa1eSAl Viro if (IS_ERR(class_dir)) { 1883a1c2aa1eSAl Viro ret = PTR_ERR(class_dir); 1884a1c2aa1eSAl Viro class_dir = NULL; 1885e47c8fc5SChristopher J. PeBenito goto err; 1886a1c2aa1eSAl Viro } 1887e47c8fc5SChristopher J. PeBenito 1888a1c2aa1eSAl Viro policycap_dir = sel_make_dir(sb->s_root, "policy_capabilities", &sel_last_ino); 1889a1c2aa1eSAl Viro if (IS_ERR(policycap_dir)) { 1890a1c2aa1eSAl Viro ret = PTR_ERR(policycap_dir); 1891a1c2aa1eSAl Viro policycap_dir = NULL; 1892e47c8fc5SChristopher J. PeBenito goto err; 1893a1c2aa1eSAl Viro } 1894b77a493bSEric Paris return 0; 1895161ce45aSJames Morris err: 1896744ba35eSEric Paris printk(KERN_ERR "SELinux: %s: failed while creating inodes\n", 1897744ba35eSEric Paris __func__); 1898b77a493bSEric Paris return ret; 18991da177e4SLinus Torvalds } 19001da177e4SLinus Torvalds 1901fc14f2feSAl Viro static struct dentry *sel_mount(struct file_system_type *fs_type, 1902fc14f2feSAl Viro int flags, const char *dev_name, void *data) 19031da177e4SLinus Torvalds { 1904fc14f2feSAl Viro return mount_single(fs_type, flags, data, sel_fill_super); 19051da177e4SLinus Torvalds } 19061da177e4SLinus Torvalds 19071da177e4SLinus Torvalds static struct file_system_type sel_fs_type = { 19081da177e4SLinus Torvalds .name = "selinuxfs", 1909fc14f2feSAl Viro .mount = sel_mount, 19101da177e4SLinus Torvalds .kill_sb = kill_litter_super, 19111da177e4SLinus Torvalds }; 19121da177e4SLinus Torvalds 19131da177e4SLinus Torvalds struct vfsmount *selinuxfs_mount; 19141da177e4SLinus Torvalds 19151da177e4SLinus Torvalds static int __init init_sel_fs(void) 19161da177e4SLinus Torvalds { 19171da177e4SLinus Torvalds int err; 19181da177e4SLinus Torvalds 19191da177e4SLinus Torvalds if (!selinux_enabled) 19201da177e4SLinus Torvalds return 0; 19217a627e3bSGreg Kroah-Hartman 1922f9bb4882SEric W. Biederman err = sysfs_create_mount_point(fs_kobj, "selinux"); 1923f9bb4882SEric W. Biederman if (err) 1924f9bb4882SEric W. Biederman return err; 19257a627e3bSGreg Kroah-Hartman 19261da177e4SLinus Torvalds err = register_filesystem(&sel_fs_type); 19277a627e3bSGreg Kroah-Hartman if (err) { 1928f9bb4882SEric W. Biederman sysfs_remove_mount_point(fs_kobj, "selinux"); 1929b77a493bSEric Paris return err; 19307a627e3bSGreg Kroah-Hartman } 1931b77a493bSEric Paris 1932765927b2SAl Viro selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type); 19331da177e4SLinus Torvalds if (IS_ERR(selinuxfs_mount)) { 19341da177e4SLinus Torvalds printk(KERN_ERR "selinuxfs: could not mount!\n"); 19351da177e4SLinus Torvalds err = PTR_ERR(selinuxfs_mount); 19361da177e4SLinus Torvalds selinuxfs_mount = NULL; 19371da177e4SLinus Torvalds } 1938b77a493bSEric Paris 19391da177e4SLinus Torvalds return err; 19401da177e4SLinus Torvalds } 19411da177e4SLinus Torvalds 19421da177e4SLinus Torvalds __initcall(init_sel_fs); 19431da177e4SLinus Torvalds 19441da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 19451da177e4SLinus Torvalds void exit_sel_fs(void) 19461da177e4SLinus Torvalds { 1947f9bb4882SEric W. Biederman sysfs_remove_mount_point(fs_kobj, "selinux"); 1948423e0ab0STim Chen kern_unmount(selinuxfs_mount); 19491da177e4SLinus Torvalds unregister_filesystem(&sel_fs_type); 19501da177e4SLinus Torvalds } 19511da177e4SLinus Torvalds #endif 1952