1a10e763bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* Updated: Karl MacMillan <kmacmillan@tresys.com> 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Added conditional policy language extensions 51da177e4SLinus Torvalds * 682c21bfaSPaul Moore * Updated: Hewlett-Packard <paul@paul-moore.com> 73bb56b25SPaul Moore * 83bb56b25SPaul Moore * Added support for the policy capability bitmap 93bb56b25SPaul Moore * 103bb56b25SPaul Moore * Copyright (C) 2007 Hewlett-Packard Development Company, L.P. 111da177e4SLinus Torvalds * Copyright (C) 2003 - 2004 Tresys Technology, LLC 121da177e4SLinus Torvalds * Copyright (C) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com> 131da177e4SLinus Torvalds */ 141da177e4SLinus Torvalds 151da177e4SLinus Torvalds #include <linux/kernel.h> 161da177e4SLinus Torvalds #include <linux/pagemap.h> 171da177e4SLinus Torvalds #include <linux/slab.h> 181da177e4SLinus Torvalds #include <linux/vmalloc.h> 191da177e4SLinus Torvalds #include <linux/fs.h> 20920f50b2SDavid Howells #include <linux/fs_context.h> 210619f0f5SStephen Smalley #include <linux/mount.h> 22bb003079SIngo Molnar #include <linux/mutex.h> 230eea6091SDaniel Burgener #include <linux/namei.h> 241da177e4SLinus Torvalds #include <linux/init.h> 251da177e4SLinus Torvalds #include <linux/string.h> 261da177e4SLinus Torvalds #include <linux/security.h> 271da177e4SLinus Torvalds #include <linux/major.h> 281da177e4SLinus Torvalds #include <linux/seq_file.h> 291da177e4SLinus Torvalds #include <linux/percpu.h> 30af601e46SSteve Grubb #include <linux/audit.h> 31f5269710SEric Paris #include <linux/uaccess.h> 327a627e3bSGreg Kroah-Hartman #include <linux/kobject.h> 330f7e4c33SKohei Kaigai #include <linux/ctype.h> 341da177e4SLinus Torvalds 351da177e4SLinus Torvalds /* selinuxfs pseudo filesystem for exporting the security policy API. 361da177e4SLinus Torvalds Based on the proc code and the fs/nfsd/nfsctl.c code. */ 371da177e4SLinus Torvalds 381da177e4SLinus Torvalds #include "flask.h" 391da177e4SLinus Torvalds #include "avc.h" 401da177e4SLinus Torvalds #include "avc_ss.h" 411da177e4SLinus Torvalds #include "security.h" 421da177e4SLinus Torvalds #include "objsec.h" 431da177e4SLinus Torvalds #include "conditional.h" 442554a48fSLakshmi Ramasubramanian #include "ima.h" 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds enum sel_inos { 471da177e4SLinus Torvalds SEL_ROOT_INO = 2, 481da177e4SLinus Torvalds SEL_LOAD, /* load policy */ 491da177e4SLinus Torvalds SEL_ENFORCE, /* get or set enforcing status */ 501da177e4SLinus Torvalds SEL_CONTEXT, /* validate context */ 511da177e4SLinus Torvalds SEL_ACCESS, /* compute access decision */ 521da177e4SLinus Torvalds SEL_CREATE, /* compute create labeling decision */ 531da177e4SLinus Torvalds SEL_RELABEL, /* compute relabeling decision */ 541da177e4SLinus Torvalds SEL_USER, /* compute reachable user contexts */ 551da177e4SLinus Torvalds SEL_POLICYVERS, /* return policy version for this kernel */ 561da177e4SLinus Torvalds SEL_COMMIT_BOOLS, /* commit new boolean values */ 571da177e4SLinus Torvalds SEL_MLS, /* return if MLS policy is enabled */ 581da177e4SLinus Torvalds SEL_DISABLE, /* disable SELinux until next reboot */ 591da177e4SLinus Torvalds SEL_MEMBER, /* compute polyinstantiation membership decision */ 601da177e4SLinus Torvalds SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */ 614e5ab4cbSJames Morris SEL_COMPAT_NET, /* whether to use old compat network packet controls */ 623f12070eSEric Paris SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */ 633f12070eSEric Paris SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */ 6411904167SKaiGai Kohei SEL_STATUS, /* export current status using mmap() */ 65cee74f47SEric Paris SEL_POLICY, /* allow userspace to read the in kernel policy */ 66f9df6458SAndrew Perepechko SEL_VALIDATE_TRANS, /* compute validatetrans decision */ 676174eafcSJames Carter SEL_INO_NEXT, /* The next inode number to use */ 681da177e4SLinus Torvalds }; 691da177e4SLinus Torvalds 700619f0f5SStephen Smalley struct selinux_fs_info { 710619f0f5SStephen Smalley struct dentry *bool_dir; 720619f0f5SStephen Smalley unsigned int bool_num; 730619f0f5SStephen Smalley char **bool_pending_names; 74c3fae2b2SChristian Göttsche int *bool_pending_values; 750619f0f5SStephen Smalley struct dentry *class_dir; 760619f0f5SStephen Smalley unsigned long last_class_ino; 770619f0f5SStephen Smalley bool policy_opened; 780619f0f5SStephen Smalley struct dentry *policycap_dir; 790619f0f5SStephen Smalley unsigned long last_ino; 800619f0f5SStephen Smalley struct super_block *sb; 810619f0f5SStephen Smalley }; 820619f0f5SStephen Smalley 830619f0f5SStephen Smalley static int selinux_fs_info_create(struct super_block *sb) 840619f0f5SStephen Smalley { 850619f0f5SStephen Smalley struct selinux_fs_info *fsi; 860619f0f5SStephen Smalley 870619f0f5SStephen Smalley fsi = kzalloc(sizeof(*fsi), GFP_KERNEL); 880619f0f5SStephen Smalley if (!fsi) 890619f0f5SStephen Smalley return -ENOMEM; 900619f0f5SStephen Smalley 910619f0f5SStephen Smalley fsi->last_ino = SEL_INO_NEXT - 1; 920619f0f5SStephen Smalley fsi->sb = sb; 930619f0f5SStephen Smalley sb->s_fs_info = fsi; 940619f0f5SStephen Smalley return 0; 950619f0f5SStephen Smalley } 960619f0f5SStephen Smalley 970619f0f5SStephen Smalley static void selinux_fs_info_free(struct super_block *sb) 980619f0f5SStephen Smalley { 990619f0f5SStephen Smalley struct selinux_fs_info *fsi = sb->s_fs_info; 1000619f0f5SStephen Smalley int i; 1010619f0f5SStephen Smalley 1020619f0f5SStephen Smalley if (fsi) { 1030619f0f5SStephen Smalley for (i = 0; i < fsi->bool_num; i++) 1040619f0f5SStephen Smalley kfree(fsi->bool_pending_names[i]); 1050619f0f5SStephen Smalley kfree(fsi->bool_pending_names); 1060619f0f5SStephen Smalley kfree(fsi->bool_pending_values); 1070619f0f5SStephen Smalley } 1080619f0f5SStephen Smalley kfree(sb->s_fs_info); 1090619f0f5SStephen Smalley sb->s_fs_info = NULL; 1100619f0f5SStephen Smalley } 1116174eafcSJames Carter 112f0ee2e46SJames Carter #define SEL_INITCON_INO_OFFSET 0x01000000 113bce34bc0SJames Carter #define SEL_BOOL_INO_OFFSET 0x02000000 114e47c8fc5SChristopher J. PeBenito #define SEL_CLASS_INO_OFFSET 0x04000000 1153bb56b25SPaul Moore #define SEL_POLICYCAP_INO_OFFSET 0x08000000 116f0ee2e46SJames Carter #define SEL_INO_MASK 0x00ffffff 117f0ee2e46SJames Carter 118613ba187SDaniel Burgener #define BOOL_DIR_NAME "booleans" 119613ba187SDaniel Burgener #define CLASS_DIR_NAME "class" 120613ba187SDaniel Burgener #define POLICYCAP_DIR_NAME "policy_capabilities" 121613ba187SDaniel Burgener 1221da177e4SLinus Torvalds #define TMPBUFLEN 12 1231da177e4SLinus Torvalds static ssize_t sel_read_enforce(struct file *filp, char __user *buf, 1241da177e4SLinus Torvalds size_t count, loff_t *ppos) 1251da177e4SLinus Torvalds { 1261da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 1271da177e4SLinus Torvalds ssize_t length; 1281da177e4SLinus Torvalds 129aa8e712cSStephen Smalley length = scnprintf(tmpbuf, TMPBUFLEN, "%d", 130e67b7985SStephen Smalley enforcing_enabled()); 1311da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 1321da177e4SLinus Torvalds } 1331da177e4SLinus Torvalds 1341da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP 1351da177e4SLinus Torvalds static ssize_t sel_write_enforce(struct file *file, const char __user *buf, 1361da177e4SLinus Torvalds size_t count, loff_t *ppos) 1371da177e4SLinus Torvalds 1381da177e4SLinus Torvalds { 139b77a493bSEric Paris char *page = NULL; 1401da177e4SLinus Torvalds ssize_t length; 141aa8e712cSStephen Smalley int old_value, new_value; 1421da177e4SLinus Torvalds 143bfd51626SDavi Arnaut if (count >= PAGE_SIZE) 1448365a719SAl Viro return -ENOMEM; 145b77a493bSEric Paris 1461da177e4SLinus Torvalds /* No partial writes. */ 147b77a493bSEric Paris if (*ppos != 0) 1488365a719SAl Viro return -EINVAL; 149b77a493bSEric Paris 1508365a719SAl Viro page = memdup_user_nul(buf, count); 1518365a719SAl Viro if (IS_ERR(page)) 1528365a719SAl Viro return PTR_ERR(page); 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds length = -EINVAL; 1551da177e4SLinus Torvalds if (sscanf(page, "%d", &new_value) != 1) 1561da177e4SLinus Torvalds goto out; 1571da177e4SLinus Torvalds 158ea49d10eSStephen Smalley new_value = !!new_value; 159ea49d10eSStephen Smalley 160e67b7985SStephen Smalley old_value = enforcing_enabled(); 161aa8e712cSStephen Smalley if (new_value != old_value) { 162e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 163be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETENFORCE, 164be0554c9SStephen Smalley NULL); 1651da177e4SLinus Torvalds if (length) 1661da177e4SLinus Torvalds goto out; 167cdfb6b34SRichard Guy Briggs audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS, 1684195ed42SRichard Guy Briggs "enforcing=%d old_enforcing=%d auid=%u ses=%u" 1696c5a682eSStephen Smalley " enabled=1 old-enabled=1 lsm=selinux res=1", 170aa8e712cSStephen Smalley new_value, old_value, 171581abc09SEric W. Biederman from_kuid(&init_user_ns, audit_get_loginuid(current)), 1726c5a682eSStephen Smalley audit_get_sessionid(current)); 173e67b7985SStephen Smalley enforcing_set(new_value); 174aa8e712cSStephen Smalley if (new_value) 175e67b7985SStephen Smalley avc_ss_reset(0); 176aa8e712cSStephen Smalley selnl_notify_setenforce(new_value); 177e67b7985SStephen Smalley selinux_status_update_setenforce(new_value); 178aa8e712cSStephen Smalley if (!new_value) 17942df744cSJanne Karhunen call_blocking_lsm_notifier(LSM_POLICY_CHANGE, NULL); 1802554a48fSLakshmi Ramasubramanian 181e67b7985SStephen Smalley selinux_ima_measure_state(); 1821da177e4SLinus Torvalds } 1831da177e4SLinus Torvalds length = count; 1841da177e4SLinus Torvalds out: 1858365a719SAl Viro kfree(page); 1861da177e4SLinus Torvalds return length; 1871da177e4SLinus Torvalds } 1881da177e4SLinus Torvalds #else 1891da177e4SLinus Torvalds #define sel_write_enforce NULL 1901da177e4SLinus Torvalds #endif 1911da177e4SLinus Torvalds 1929c2e08c5SArjan van de Ven static const struct file_operations sel_enforce_ops = { 1931da177e4SLinus Torvalds .read = sel_read_enforce, 1941da177e4SLinus Torvalds .write = sel_write_enforce, 19557a62c23SArnd Bergmann .llseek = generic_file_llseek, 1961da177e4SLinus Torvalds }; 1971da177e4SLinus Torvalds 1983f12070eSEric Paris static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf, 1993f12070eSEric Paris size_t count, loff_t *ppos) 2003f12070eSEric Paris { 2013f12070eSEric Paris char tmpbuf[TMPBUFLEN]; 2023f12070eSEric Paris ssize_t length; 203496ad9aaSAl Viro ino_t ino = file_inode(filp)->i_ino; 2043f12070eSEric Paris int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ? 205e67b7985SStephen Smalley security_get_reject_unknown() : 206e67b7985SStephen Smalley !security_get_allow_unknown(); 2073f12070eSEric Paris 2083f12070eSEric Paris length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown); 2093f12070eSEric Paris return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 2103f12070eSEric Paris } 2113f12070eSEric Paris 2123f12070eSEric Paris static const struct file_operations sel_handle_unknown_ops = { 2133f12070eSEric Paris .read = sel_read_handle_unknown, 21457a62c23SArnd Bergmann .llseek = generic_file_llseek, 2153f12070eSEric Paris }; 2163f12070eSEric Paris 21711904167SKaiGai Kohei static int sel_open_handle_status(struct inode *inode, struct file *filp) 21811904167SKaiGai Kohei { 219e67b7985SStephen Smalley struct page *status = selinux_kernel_status_page(); 22011904167SKaiGai Kohei 22111904167SKaiGai Kohei if (!status) 22211904167SKaiGai Kohei return -ENOMEM; 22311904167SKaiGai Kohei 22411904167SKaiGai Kohei filp->private_data = status; 22511904167SKaiGai Kohei 22611904167SKaiGai Kohei return 0; 22711904167SKaiGai Kohei } 22811904167SKaiGai Kohei 22911904167SKaiGai Kohei static ssize_t sel_read_handle_status(struct file *filp, char __user *buf, 23011904167SKaiGai Kohei size_t count, loff_t *ppos) 23111904167SKaiGai Kohei { 23211904167SKaiGai Kohei struct page *status = filp->private_data; 23311904167SKaiGai Kohei 23411904167SKaiGai Kohei BUG_ON(!status); 23511904167SKaiGai Kohei 23611904167SKaiGai Kohei return simple_read_from_buffer(buf, count, ppos, 23711904167SKaiGai Kohei page_address(status), 23811904167SKaiGai Kohei sizeof(struct selinux_kernel_status)); 23911904167SKaiGai Kohei } 24011904167SKaiGai Kohei 24111904167SKaiGai Kohei static int sel_mmap_handle_status(struct file *filp, 24211904167SKaiGai Kohei struct vm_area_struct *vma) 24311904167SKaiGai Kohei { 24411904167SKaiGai Kohei struct page *status = filp->private_data; 24511904167SKaiGai Kohei unsigned long size = vma->vm_end - vma->vm_start; 24611904167SKaiGai Kohei 24711904167SKaiGai Kohei BUG_ON(!status); 24811904167SKaiGai Kohei 24911904167SKaiGai Kohei /* only allows one page from the head */ 25011904167SKaiGai Kohei if (vma->vm_pgoff > 0 || size != PAGE_SIZE) 25111904167SKaiGai Kohei return -EIO; 25211904167SKaiGai Kohei /* disallow writable mapping */ 25311904167SKaiGai Kohei if (vma->vm_flags & VM_WRITE) 25411904167SKaiGai Kohei return -EPERM; 25511904167SKaiGai Kohei /* disallow mprotect() turns it into writable */ 2561c71222eSSuren Baghdasaryan vm_flags_clear(vma, VM_MAYWRITE); 25711904167SKaiGai Kohei 25811904167SKaiGai Kohei return remap_pfn_range(vma, vma->vm_start, 25911904167SKaiGai Kohei page_to_pfn(status), 26011904167SKaiGai Kohei size, vma->vm_page_prot); 26111904167SKaiGai Kohei } 26211904167SKaiGai Kohei 26311904167SKaiGai Kohei static const struct file_operations sel_handle_status_ops = { 26411904167SKaiGai Kohei .open = sel_open_handle_status, 26511904167SKaiGai Kohei .read = sel_read_handle_status, 26611904167SKaiGai Kohei .mmap = sel_mmap_handle_status, 26711904167SKaiGai Kohei .llseek = generic_file_llseek, 26811904167SKaiGai Kohei }; 26911904167SKaiGai Kohei 2701da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 2711da177e4SLinus Torvalds static ssize_t sel_write_disable(struct file *file, const char __user *buf, 2721da177e4SLinus Torvalds size_t count, loff_t *ppos) 2731da177e4SLinus Torvalds 2741da177e4SLinus Torvalds { 2758365a719SAl Viro char *page; 2761da177e4SLinus Torvalds ssize_t length; 2771da177e4SLinus Torvalds int new_value; 2784195ed42SRichard Guy Briggs int enforcing; 2791da177e4SLinus Torvalds 28089b223bfSPaul Moore /* NOTE: we are now officially considering runtime disable as 28189b223bfSPaul Moore * deprecated, and using it will become increasingly painful 28289b223bfSPaul Moore * (e.g. sleeping/blocking) as we progress through future 28389b223bfSPaul Moore * kernel releases until eventually it is removed 28489b223bfSPaul Moore */ 28589b223bfSPaul Moore pr_err("SELinux: Runtime disable is deprecated, use selinux=0 on the kernel cmdline.\n"); 28643b66662SPaul Moore pr_err("SELinux: https://github.com/SELinuxProject/selinux-kernel/wiki/DEPRECATE-runtime-disable\n"); 287e0d82593SPaul Moore ssleep(15); 28889b223bfSPaul Moore 289bfd51626SDavi Arnaut if (count >= PAGE_SIZE) 2908365a719SAl Viro return -ENOMEM; 291b77a493bSEric Paris 2921da177e4SLinus Torvalds /* No partial writes. */ 293b77a493bSEric Paris if (*ppos != 0) 2948365a719SAl Viro return -EINVAL; 295b77a493bSEric Paris 2968365a719SAl Viro page = memdup_user_nul(buf, count); 2978365a719SAl Viro if (IS_ERR(page)) 2988365a719SAl Viro return PTR_ERR(page); 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds length = -EINVAL; 3011da177e4SLinus Torvalds if (sscanf(page, "%d", &new_value) != 1) 3021da177e4SLinus Torvalds goto out; 3031da177e4SLinus Torvalds 3041da177e4SLinus Torvalds if (new_value) { 305e67b7985SStephen Smalley enforcing = enforcing_enabled(); 306e67b7985SStephen Smalley length = selinux_disable(); 307b77a493bSEric Paris if (length) 3081da177e4SLinus Torvalds goto out; 309cdfb6b34SRichard Guy Briggs audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS, 3104195ed42SRichard Guy Briggs "enforcing=%d old_enforcing=%d auid=%u ses=%u" 3116c5a682eSStephen Smalley " enabled=0 old-enabled=1 lsm=selinux res=1", 3124195ed42SRichard Guy Briggs enforcing, enforcing, 313581abc09SEric W. Biederman from_kuid(&init_user_ns, audit_get_loginuid(current)), 3146c5a682eSStephen Smalley audit_get_sessionid(current)); 3151da177e4SLinus Torvalds } 3161da177e4SLinus Torvalds 3171da177e4SLinus Torvalds length = count; 3181da177e4SLinus Torvalds out: 3198365a719SAl Viro kfree(page); 3201da177e4SLinus Torvalds return length; 3211da177e4SLinus Torvalds } 3221da177e4SLinus Torvalds #else 3231da177e4SLinus Torvalds #define sel_write_disable NULL 3241da177e4SLinus Torvalds #endif 3251da177e4SLinus Torvalds 3269c2e08c5SArjan van de Ven static const struct file_operations sel_disable_ops = { 3271da177e4SLinus Torvalds .write = sel_write_disable, 32857a62c23SArnd Bergmann .llseek = generic_file_llseek, 3291da177e4SLinus Torvalds }; 3301da177e4SLinus Torvalds 3311da177e4SLinus Torvalds static ssize_t sel_read_policyvers(struct file *filp, char __user *buf, 3321da177e4SLinus Torvalds size_t count, loff_t *ppos) 3331da177e4SLinus Torvalds { 3341da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 3351da177e4SLinus Torvalds ssize_t length; 3361da177e4SLinus Torvalds 3371da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX); 3381da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 3391da177e4SLinus Torvalds } 3401da177e4SLinus Torvalds 3419c2e08c5SArjan van de Ven static const struct file_operations sel_policyvers_ops = { 3421da177e4SLinus Torvalds .read = sel_read_policyvers, 34357a62c23SArnd Bergmann .llseek = generic_file_llseek, 3441da177e4SLinus Torvalds }; 3451da177e4SLinus Torvalds 3461da177e4SLinus Torvalds /* declaration for sel_write_load */ 34766ec384aSDaniel Burgener static int sel_make_bools(struct selinux_policy *newpolicy, struct dentry *bool_dir, 34866ec384aSDaniel Burgener unsigned int *bool_num, char ***bool_pending_names, 349c3fae2b2SChristian Göttsche int **bool_pending_values); 35066ec384aSDaniel Burgener static int sel_make_classes(struct selinux_policy *newpolicy, 35166ec384aSDaniel Burgener struct dentry *class_dir, 35266ec384aSDaniel Burgener unsigned long *last_class_ino); 353e47c8fc5SChristopher J. PeBenito 354e47c8fc5SChristopher J. PeBenito /* declaration for sel_make_class_dirs */ 355a1c2aa1eSAl Viro static struct dentry *sel_make_dir(struct dentry *dir, const char *name, 356e47c8fc5SChristopher J. PeBenito unsigned long *ino); 3571da177e4SLinus Torvalds 3580eea6091SDaniel Burgener /* declaration for sel_make_policy_nodes */ 3590eea6091SDaniel Burgener static struct dentry *sel_make_disconnected_dir(struct super_block *sb, 3600eea6091SDaniel Burgener unsigned long *ino); 3610eea6091SDaniel Burgener 3620eea6091SDaniel Burgener /* declaration for sel_make_policy_nodes */ 363aeecf4a3SDaniel Burgener static void sel_remove_entries(struct dentry *de); 364aeecf4a3SDaniel Burgener 3651da177e4SLinus Torvalds static ssize_t sel_read_mls(struct file *filp, char __user *buf, 3661da177e4SLinus Torvalds size_t count, loff_t *ppos) 3671da177e4SLinus Torvalds { 3681da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 3691da177e4SLinus Torvalds ssize_t length; 3701da177e4SLinus Torvalds 3710719aaf5SGuido Trentalancia length = scnprintf(tmpbuf, TMPBUFLEN, "%d", 372e67b7985SStephen Smalley security_mls_enabled()); 3731da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 3741da177e4SLinus Torvalds } 3751da177e4SLinus Torvalds 3769c2e08c5SArjan van de Ven static const struct file_operations sel_mls_ops = { 3771da177e4SLinus Torvalds .read = sel_read_mls, 37857a62c23SArnd Bergmann .llseek = generic_file_llseek, 3791da177e4SLinus Torvalds }; 3801da177e4SLinus Torvalds 381cee74f47SEric Paris struct policy_load_memory { 382cee74f47SEric Paris size_t len; 383cee74f47SEric Paris void *data; 384cee74f47SEric Paris }; 385cee74f47SEric Paris 386cee74f47SEric Paris static int sel_open_policy(struct inode *inode, struct file *filp) 387cee74f47SEric Paris { 3880619f0f5SStephen Smalley struct selinux_fs_info *fsi = inode->i_sb->s_fs_info; 389cee74f47SEric Paris struct policy_load_memory *plm = NULL; 390cee74f47SEric Paris int rc; 391cee74f47SEric Paris 392cee74f47SEric Paris BUG_ON(filp->private_data); 393cee74f47SEric Paris 394e67b7985SStephen Smalley mutex_lock(&selinux_state.policy_mutex); 395cee74f47SEric Paris 396e67b7985SStephen Smalley rc = avc_has_perm(current_sid(), SECINITSID_SECURITY, 397be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL); 398cee74f47SEric Paris if (rc) 399cee74f47SEric Paris goto err; 400cee74f47SEric Paris 401cee74f47SEric Paris rc = -EBUSY; 4020619f0f5SStephen Smalley if (fsi->policy_opened) 403cee74f47SEric Paris goto err; 404cee74f47SEric Paris 405cee74f47SEric Paris rc = -ENOMEM; 406cee74f47SEric Paris plm = kzalloc(sizeof(*plm), GFP_KERNEL); 407cee74f47SEric Paris if (!plm) 408cee74f47SEric Paris goto err; 409cee74f47SEric Paris 410e67b7985SStephen Smalley rc = security_read_policy(&plm->data, &plm->len); 411cee74f47SEric Paris if (rc) 412cee74f47SEric Paris goto err; 413cee74f47SEric Paris 41466ccd256SOndrej Mosnacek if ((size_t)i_size_read(inode) != plm->len) { 41566ccd256SOndrej Mosnacek inode_lock(inode); 41666ccd256SOndrej Mosnacek i_size_write(inode, plm->len); 41766ccd256SOndrej Mosnacek inode_unlock(inode); 41866ccd256SOndrej Mosnacek } 41966ccd256SOndrej Mosnacek 4200619f0f5SStephen Smalley fsi->policy_opened = 1; 421cee74f47SEric Paris 422cee74f47SEric Paris filp->private_data = plm; 423cee74f47SEric Paris 424e67b7985SStephen Smalley mutex_unlock(&selinux_state.policy_mutex); 425cee74f47SEric Paris 426cee74f47SEric Paris return 0; 427cee74f47SEric Paris err: 428e67b7985SStephen Smalley mutex_unlock(&selinux_state.policy_mutex); 429cee74f47SEric Paris 430cee74f47SEric Paris if (plm) 431cee74f47SEric Paris vfree(plm->data); 432cee74f47SEric Paris kfree(plm); 433cee74f47SEric Paris return rc; 434cee74f47SEric Paris } 435cee74f47SEric Paris 436cee74f47SEric Paris static int sel_release_policy(struct inode *inode, struct file *filp) 437cee74f47SEric Paris { 4380619f0f5SStephen Smalley struct selinux_fs_info *fsi = inode->i_sb->s_fs_info; 439cee74f47SEric Paris struct policy_load_memory *plm = filp->private_data; 440cee74f47SEric Paris 441cee74f47SEric Paris BUG_ON(!plm); 442cee74f47SEric Paris 4430619f0f5SStephen Smalley fsi->policy_opened = 0; 444cee74f47SEric Paris 445cee74f47SEric Paris vfree(plm->data); 446cee74f47SEric Paris kfree(plm); 447cee74f47SEric Paris 448cee74f47SEric Paris return 0; 449cee74f47SEric Paris } 450cee74f47SEric Paris 451cee74f47SEric Paris static ssize_t sel_read_policy(struct file *filp, char __user *buf, 452cee74f47SEric Paris size_t count, loff_t *ppos) 453cee74f47SEric Paris { 454cee74f47SEric Paris struct policy_load_memory *plm = filp->private_data; 455cee74f47SEric Paris int ret; 456cee74f47SEric Paris 457e67b7985SStephen Smalley ret = avc_has_perm(current_sid(), SECINITSID_SECURITY, 458be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL); 459cee74f47SEric Paris if (ret) 460cee74f47SEric Paris return ret; 4610da74120SJann Horn 4620da74120SJann Horn return simple_read_from_buffer(buf, count, ppos, plm->data, plm->len); 463cee74f47SEric Paris } 464cee74f47SEric Paris 465ac9a1f6dSSouptick Joarder static vm_fault_t sel_mmap_policy_fault(struct vm_fault *vmf) 466845ca30fSEric Paris { 46711bac800SDave Jiang struct policy_load_memory *plm = vmf->vma->vm_file->private_data; 468845ca30fSEric Paris unsigned long offset; 469845ca30fSEric Paris struct page *page; 470845ca30fSEric Paris 471845ca30fSEric Paris if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE)) 472845ca30fSEric Paris return VM_FAULT_SIGBUS; 473845ca30fSEric Paris 474845ca30fSEric Paris offset = vmf->pgoff << PAGE_SHIFT; 475845ca30fSEric Paris if (offset >= roundup(plm->len, PAGE_SIZE)) 476845ca30fSEric Paris return VM_FAULT_SIGBUS; 477845ca30fSEric Paris 478845ca30fSEric Paris page = vmalloc_to_page(plm->data + offset); 479845ca30fSEric Paris get_page(page); 480845ca30fSEric Paris 481845ca30fSEric Paris vmf->page = page; 482845ca30fSEric Paris 483845ca30fSEric Paris return 0; 484845ca30fSEric Paris } 485845ca30fSEric Paris 4867cbea8dcSKirill A. Shutemov static const struct vm_operations_struct sel_mmap_policy_ops = { 487845ca30fSEric Paris .fault = sel_mmap_policy_fault, 488845ca30fSEric Paris .page_mkwrite = sel_mmap_policy_fault, 489845ca30fSEric Paris }; 490845ca30fSEric Paris 491ad3fa08cSJames Morris static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma) 492845ca30fSEric Paris { 493845ca30fSEric Paris if (vma->vm_flags & VM_SHARED) { 494845ca30fSEric Paris /* do not allow mprotect to make mapping writable */ 4951c71222eSSuren Baghdasaryan vm_flags_clear(vma, VM_MAYWRITE); 496845ca30fSEric Paris 497845ca30fSEric Paris if (vma->vm_flags & VM_WRITE) 498845ca30fSEric Paris return -EACCES; 499845ca30fSEric Paris } 500845ca30fSEric Paris 5011c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_DONTEXPAND | VM_DONTDUMP); 502845ca30fSEric Paris vma->vm_ops = &sel_mmap_policy_ops; 503845ca30fSEric Paris 504845ca30fSEric Paris return 0; 505845ca30fSEric Paris } 506845ca30fSEric Paris 507cee74f47SEric Paris static const struct file_operations sel_policy_ops = { 508cee74f47SEric Paris .open = sel_open_policy, 509cee74f47SEric Paris .read = sel_read_policy, 510845ca30fSEric Paris .mmap = sel_mmap_policy, 511cee74f47SEric Paris .release = sel_release_policy, 51247a93a5bSEric Paris .llseek = generic_file_llseek, 513cee74f47SEric Paris }; 514cee74f47SEric Paris 5150eea6091SDaniel Burgener static void sel_remove_old_bool_data(unsigned int bool_num, char **bool_names, 516c3fae2b2SChristian Göttsche int *bool_values) 517aeecf4a3SDaniel Burgener { 518aeecf4a3SDaniel Burgener u32 i; 519aeecf4a3SDaniel Burgener 520aeecf4a3SDaniel Burgener /* bool_dir cleanup */ 5210eea6091SDaniel Burgener for (i = 0; i < bool_num; i++) 5220eea6091SDaniel Burgener kfree(bool_names[i]); 5230eea6091SDaniel Burgener kfree(bool_names); 5240eea6091SDaniel Burgener kfree(bool_values); 525aeecf4a3SDaniel Burgener } 526aeecf4a3SDaniel Burgener 52702a52c5cSStephen Smalley static int sel_make_policy_nodes(struct selinux_fs_info *fsi, 52802a52c5cSStephen Smalley struct selinux_policy *newpolicy) 5290619f0f5SStephen Smalley { 5300eea6091SDaniel Burgener int ret = 0; 5310eea6091SDaniel Burgener struct dentry *tmp_parent, *tmp_bool_dir, *tmp_class_dir, *old_dentry; 5320eea6091SDaniel Burgener unsigned int tmp_bool_num, old_bool_num; 5330eea6091SDaniel Burgener char **tmp_bool_names, **old_bool_names; 534c3fae2b2SChristian Göttsche int *tmp_bool_values, *old_bool_values; 5350eea6091SDaniel Burgener unsigned long tmp_ino = fsi->last_ino; /* Don't increment last_ino in this function */ 5360619f0f5SStephen Smalley 5370eea6091SDaniel Burgener tmp_parent = sel_make_disconnected_dir(fsi->sb, &tmp_ino); 5380eea6091SDaniel Burgener if (IS_ERR(tmp_parent)) 5390eea6091SDaniel Burgener return PTR_ERR(tmp_parent); 540aeecf4a3SDaniel Burgener 5410eea6091SDaniel Burgener tmp_ino = fsi->bool_dir->d_inode->i_ino - 1; /* sel_make_dir will increment and set */ 5420eea6091SDaniel Burgener tmp_bool_dir = sel_make_dir(tmp_parent, BOOL_DIR_NAME, &tmp_ino); 5430eea6091SDaniel Burgener if (IS_ERR(tmp_bool_dir)) { 5440eea6091SDaniel Burgener ret = PTR_ERR(tmp_bool_dir); 5450eea6091SDaniel Burgener goto out; 5460619f0f5SStephen Smalley } 5470619f0f5SStephen Smalley 5480eea6091SDaniel Burgener tmp_ino = fsi->class_dir->d_inode->i_ino - 1; /* sel_make_dir will increment and set */ 5490eea6091SDaniel Burgener tmp_class_dir = sel_make_dir(tmp_parent, CLASS_DIR_NAME, &tmp_ino); 5500eea6091SDaniel Burgener if (IS_ERR(tmp_class_dir)) { 5510eea6091SDaniel Burgener ret = PTR_ERR(tmp_class_dir); 5520eea6091SDaniel Burgener goto out; 5530eea6091SDaniel Burgener } 5540eea6091SDaniel Burgener 5550eea6091SDaniel Burgener ret = sel_make_bools(newpolicy, tmp_bool_dir, &tmp_bool_num, 5560eea6091SDaniel Burgener &tmp_bool_names, &tmp_bool_values); 557ee5de60aSOndrej Mosnacek if (ret) 5580eea6091SDaniel Burgener goto out; 5590eea6091SDaniel Burgener 5600eea6091SDaniel Burgener ret = sel_make_classes(newpolicy, tmp_class_dir, 56166ec384aSDaniel Burgener &fsi->last_class_ino); 562ee5de60aSOndrej Mosnacek if (ret) 5630eea6091SDaniel Burgener goto out; 5640619f0f5SStephen Smalley 5650eea6091SDaniel Burgener /* booleans */ 5660eea6091SDaniel Burgener old_dentry = fsi->bool_dir; 5670eea6091SDaniel Burgener lock_rename(tmp_bool_dir, old_dentry); 5680eea6091SDaniel Burgener d_exchange(tmp_bool_dir, fsi->bool_dir); 5690eea6091SDaniel Burgener 5700eea6091SDaniel Burgener old_bool_num = fsi->bool_num; 5710eea6091SDaniel Burgener old_bool_names = fsi->bool_pending_names; 5720eea6091SDaniel Burgener old_bool_values = fsi->bool_pending_values; 5730eea6091SDaniel Burgener 5740eea6091SDaniel Burgener fsi->bool_num = tmp_bool_num; 5750eea6091SDaniel Burgener fsi->bool_pending_names = tmp_bool_names; 5760eea6091SDaniel Burgener fsi->bool_pending_values = tmp_bool_values; 5770eea6091SDaniel Burgener 5780eea6091SDaniel Burgener sel_remove_old_bool_data(old_bool_num, old_bool_names, old_bool_values); 5790eea6091SDaniel Burgener 5800eea6091SDaniel Burgener fsi->bool_dir = tmp_bool_dir; 5810eea6091SDaniel Burgener unlock_rename(tmp_bool_dir, old_dentry); 5820eea6091SDaniel Burgener 5830eea6091SDaniel Burgener /* classes */ 5840eea6091SDaniel Burgener old_dentry = fsi->class_dir; 5850eea6091SDaniel Burgener lock_rename(tmp_class_dir, old_dentry); 5860eea6091SDaniel Burgener d_exchange(tmp_class_dir, fsi->class_dir); 5870eea6091SDaniel Burgener fsi->class_dir = tmp_class_dir; 5880eea6091SDaniel Burgener unlock_rename(tmp_class_dir, old_dentry); 5890eea6091SDaniel Burgener 5900eea6091SDaniel Burgener out: 5910eea6091SDaniel Burgener /* Since the other temporary dirs are children of tmp_parent 5920eea6091SDaniel Burgener * this will handle all the cleanup in the case of a failure before 5930eea6091SDaniel Burgener * the swapover 5940eea6091SDaniel Burgener */ 5950eea6091SDaniel Burgener sel_remove_entries(tmp_parent); 5960eea6091SDaniel Burgener dput(tmp_parent); /* d_genocide() only handles the children */ 5970eea6091SDaniel Burgener 5980eea6091SDaniel Burgener return ret; 5990619f0f5SStephen Smalley } 6000619f0f5SStephen Smalley 6011da177e4SLinus Torvalds static ssize_t sel_write_load(struct file *file, const char __user *buf, 6021da177e4SLinus Torvalds size_t count, loff_t *ppos) 6031da177e4SLinus Torvalds 6041da177e4SLinus Torvalds { 6050619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 6066406887aSOndrej Mosnacek struct selinux_load_state load_state; 6071da177e4SLinus Torvalds ssize_t length; 6081da177e4SLinus Torvalds void *data = NULL; 6091da177e4SLinus Torvalds 610e67b7985SStephen Smalley mutex_lock(&selinux_state.policy_mutex); 6111da177e4SLinus Torvalds 612e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 613be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL); 6141da177e4SLinus Torvalds if (length) 6151da177e4SLinus Torvalds goto out; 6161da177e4SLinus Torvalds 6171da177e4SLinus Torvalds /* No partial writes. */ 6181da177e4SLinus Torvalds length = -EINVAL; 619b77a493bSEric Paris if (*ppos != 0) 6201da177e4SLinus Torvalds goto out; 6211da177e4SLinus Torvalds 622b77a493bSEric Paris length = -ENOMEM; 623b77a493bSEric Paris data = vmalloc(count); 624b77a493bSEric Paris if (!data) 625b77a493bSEric Paris goto out; 6261da177e4SLinus Torvalds 6271da177e4SLinus Torvalds length = -EFAULT; 6281da177e4SLinus Torvalds if (copy_from_user(data, buf, count) != 0) 6291da177e4SLinus Torvalds goto out; 6301da177e4SLinus Torvalds 631e67b7985SStephen Smalley length = security_load_policy(data, count, &load_state); 6324262fb51SGary Tierney if (length) { 6334262fb51SGary Tierney pr_warn_ratelimited("SELinux: failed to load policy\n"); 6341da177e4SLinus Torvalds goto out; 6354262fb51SGary Tierney } 6361da177e4SLinus Torvalds 6376406887aSOndrej Mosnacek length = sel_make_policy_nodes(fsi, load_state.policy); 63802a52c5cSStephen Smalley if (length) { 639ee5de60aSOndrej Mosnacek pr_warn_ratelimited("SELinux: failed to initialize selinuxfs\n"); 640e67b7985SStephen Smalley selinux_policy_cancel(&load_state); 641519dad3bSOndrej Mosnacek goto out; 64202a52c5cSStephen Smalley } 64302a52c5cSStephen Smalley 644e67b7985SStephen Smalley selinux_policy_commit(&load_state); 645b77a493bSEric Paris 6461da177e4SLinus Torvalds length = count; 647e47c8fc5SChristopher J. PeBenito 648cdfb6b34SRichard Guy Briggs audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_POLICY_LOAD, 649d141136fSRichard Guy Briggs "auid=%u ses=%u lsm=selinux res=1", 650581abc09SEric W. Biederman from_kuid(&init_user_ns, audit_get_loginuid(current)), 6514746ec5bSEric Paris audit_get_sessionid(current)); 6521da177e4SLinus Torvalds out: 653e67b7985SStephen Smalley mutex_unlock(&selinux_state.policy_mutex); 6541da177e4SLinus Torvalds vfree(data); 6551da177e4SLinus Torvalds return length; 6561da177e4SLinus Torvalds } 6571da177e4SLinus Torvalds 6589c2e08c5SArjan van de Ven static const struct file_operations sel_load_ops = { 6591da177e4SLinus Torvalds .write = sel_write_load, 66057a62c23SArnd Bergmann .llseek = generic_file_llseek, 6611da177e4SLinus Torvalds }; 6621da177e4SLinus Torvalds 663ce9982d0SStephen Smalley static ssize_t sel_write_context(struct file *file, char *buf, size_t size) 6641da177e4SLinus Torvalds { 665b77a493bSEric Paris char *canon = NULL; 666ce9982d0SStephen Smalley u32 sid, len; 6671da177e4SLinus Torvalds ssize_t length; 6681da177e4SLinus Torvalds 669e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 670be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__CHECK_CONTEXT, NULL); 6711da177e4SLinus Torvalds if (length) 672b77a493bSEric Paris goto out; 6731da177e4SLinus Torvalds 674e67b7985SStephen Smalley length = security_context_to_sid(buf, size, &sid, GFP_KERNEL); 675b77a493bSEric Paris if (length) 676b77a493bSEric Paris goto out; 6771da177e4SLinus Torvalds 678e67b7985SStephen Smalley length = security_sid_to_context(sid, &canon, &len); 679b77a493bSEric Paris if (length) 680b77a493bSEric Paris goto out; 681ce9982d0SStephen Smalley 682b77a493bSEric Paris length = -ERANGE; 683ce9982d0SStephen Smalley if (len > SIMPLE_TRANSACTION_LIMIT) { 684f8b69a5fSpeter enderborg pr_err("SELinux: %s: context size (%u) exceeds " 685744ba35eSEric Paris "payload max\n", __func__, len); 686ce9982d0SStephen Smalley goto out; 687ce9982d0SStephen Smalley } 688ce9982d0SStephen Smalley 689ce9982d0SStephen Smalley memcpy(buf, canon, len); 690ce9982d0SStephen Smalley length = len; 6911da177e4SLinus Torvalds out: 692ce9982d0SStephen Smalley kfree(canon); 6931da177e4SLinus Torvalds return length; 6941da177e4SLinus Torvalds } 6951da177e4SLinus Torvalds 6961da177e4SLinus Torvalds static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf, 6971da177e4SLinus Torvalds size_t count, loff_t *ppos) 6981da177e4SLinus Torvalds { 6991da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 7001da177e4SLinus Torvalds ssize_t length; 7011da177e4SLinus Torvalds 7028861d0afSLakshmi Ramasubramanian length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 703e67b7985SStephen Smalley checkreqprot_get()); 7041da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 7051da177e4SLinus Torvalds } 7061da177e4SLinus Torvalds 7071da177e4SLinus Torvalds static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf, 7081da177e4SLinus Torvalds size_t count, loff_t *ppos) 7091da177e4SLinus Torvalds { 7108365a719SAl Viro char *page; 7111da177e4SLinus Torvalds ssize_t length; 7121da177e4SLinus Torvalds unsigned int new_value; 7131da177e4SLinus Torvalds 714e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 715be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETCHECKREQPROT, 716be0554c9SStephen Smalley NULL); 7171da177e4SLinus Torvalds if (length) 7188365a719SAl Viro return length; 7191da177e4SLinus Torvalds 720bfd51626SDavi Arnaut if (count >= PAGE_SIZE) 7218365a719SAl Viro return -ENOMEM; 722b77a493bSEric Paris 7231da177e4SLinus Torvalds /* No partial writes. */ 724b77a493bSEric Paris if (*ppos != 0) 7258365a719SAl Viro return -EINVAL; 726b77a493bSEric Paris 7278365a719SAl Viro page = memdup_user_nul(buf, count); 7288365a719SAl Viro if (IS_ERR(page)) 7298365a719SAl Viro return PTR_ERR(page); 7301da177e4SLinus Torvalds 731*a7e4676eSPaul Moore if (sscanf(page, "%u", &new_value) != 1) { 7321da177e4SLinus Torvalds length = -EINVAL; 7331da177e4SLinus Torvalds goto out; 734*a7e4676eSPaul Moore } 735*a7e4676eSPaul Moore length = count; 7361da177e4SLinus Torvalds 737e9c38f9fSStephen Smalley if (new_value) { 738e9c38f9fSStephen Smalley char comm[sizeof(current->comm)]; 739e9c38f9fSStephen Smalley 740e9c38f9fSStephen Smalley memcpy(comm, current->comm, sizeof(comm)); 741*a7e4676eSPaul Moore pr_err("SELinux: %s (%d) set checkreqprot to 1. This is no longer supported.\n", 742e9c38f9fSStephen Smalley comm, current->pid); 743e9c38f9fSStephen Smalley } 744e9c38f9fSStephen Smalley 745e67b7985SStephen Smalley selinux_ima_measure_state(); 7462554a48fSLakshmi Ramasubramanian 7471da177e4SLinus Torvalds out: 7488365a719SAl Viro kfree(page); 7491da177e4SLinus Torvalds return length; 7501da177e4SLinus Torvalds } 7519c2e08c5SArjan van de Ven static const struct file_operations sel_checkreqprot_ops = { 7521da177e4SLinus Torvalds .read = sel_read_checkreqprot, 7531da177e4SLinus Torvalds .write = sel_write_checkreqprot, 75457a62c23SArnd Bergmann .llseek = generic_file_llseek, 7551da177e4SLinus Torvalds }; 7561da177e4SLinus Torvalds 757f9df6458SAndrew Perepechko static ssize_t sel_write_validatetrans(struct file *file, 758f9df6458SAndrew Perepechko const char __user *buf, 759f9df6458SAndrew Perepechko size_t count, loff_t *ppos) 760f9df6458SAndrew Perepechko { 761f9df6458SAndrew Perepechko char *oldcon = NULL, *newcon = NULL, *taskcon = NULL; 762f9df6458SAndrew Perepechko char *req = NULL; 763f9df6458SAndrew Perepechko u32 osid, nsid, tsid; 764f9df6458SAndrew Perepechko u16 tclass; 765f9df6458SAndrew Perepechko int rc; 766f9df6458SAndrew Perepechko 767e67b7985SStephen Smalley rc = avc_has_perm(current_sid(), SECINITSID_SECURITY, 768be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__VALIDATE_TRANS, NULL); 769f9df6458SAndrew Perepechko if (rc) 770f9df6458SAndrew Perepechko goto out; 771f9df6458SAndrew Perepechko 772f9df6458SAndrew Perepechko rc = -ENOMEM; 773f9df6458SAndrew Perepechko if (count >= PAGE_SIZE) 774f9df6458SAndrew Perepechko goto out; 775f9df6458SAndrew Perepechko 776f9df6458SAndrew Perepechko /* No partial writes. */ 777f9df6458SAndrew Perepechko rc = -EINVAL; 778f9df6458SAndrew Perepechko if (*ppos != 0) 779f9df6458SAndrew Perepechko goto out; 780f9df6458SAndrew Perepechko 7810b884d25SAl Viro req = memdup_user_nul(buf, count); 7820b884d25SAl Viro if (IS_ERR(req)) { 7830b884d25SAl Viro rc = PTR_ERR(req); 7840b884d25SAl Viro req = NULL; 785f9df6458SAndrew Perepechko goto out; 7860b884d25SAl Viro } 787f9df6458SAndrew Perepechko 788f9df6458SAndrew Perepechko rc = -ENOMEM; 789f9df6458SAndrew Perepechko oldcon = kzalloc(count + 1, GFP_KERNEL); 790f9df6458SAndrew Perepechko if (!oldcon) 791f9df6458SAndrew Perepechko goto out; 792f9df6458SAndrew Perepechko 793f9df6458SAndrew Perepechko newcon = kzalloc(count + 1, GFP_KERNEL); 794f9df6458SAndrew Perepechko if (!newcon) 795f9df6458SAndrew Perepechko goto out; 796f9df6458SAndrew Perepechko 797f9df6458SAndrew Perepechko taskcon = kzalloc(count + 1, GFP_KERNEL); 798f9df6458SAndrew Perepechko if (!taskcon) 799f9df6458SAndrew Perepechko goto out; 800f9df6458SAndrew Perepechko 801f9df6458SAndrew Perepechko rc = -EINVAL; 802f9df6458SAndrew Perepechko if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4) 803f9df6458SAndrew Perepechko goto out; 804f9df6458SAndrew Perepechko 805e67b7985SStephen Smalley rc = security_context_str_to_sid(oldcon, &osid, GFP_KERNEL); 806f9df6458SAndrew Perepechko if (rc) 807f9df6458SAndrew Perepechko goto out; 808f9df6458SAndrew Perepechko 809e67b7985SStephen Smalley rc = security_context_str_to_sid(newcon, &nsid, GFP_KERNEL); 810f9df6458SAndrew Perepechko if (rc) 811f9df6458SAndrew Perepechko goto out; 812f9df6458SAndrew Perepechko 813e67b7985SStephen Smalley rc = security_context_str_to_sid(taskcon, &tsid, GFP_KERNEL); 814f9df6458SAndrew Perepechko if (rc) 815f9df6458SAndrew Perepechko goto out; 816f9df6458SAndrew Perepechko 817e67b7985SStephen Smalley rc = security_validate_transition_user(osid, nsid, tsid, tclass); 818f9df6458SAndrew Perepechko if (!rc) 819f9df6458SAndrew Perepechko rc = count; 820f9df6458SAndrew Perepechko out: 821f9df6458SAndrew Perepechko kfree(req); 822f9df6458SAndrew Perepechko kfree(oldcon); 823f9df6458SAndrew Perepechko kfree(newcon); 824f9df6458SAndrew Perepechko kfree(taskcon); 825f9df6458SAndrew Perepechko return rc; 826f9df6458SAndrew Perepechko } 827f9df6458SAndrew Perepechko 828f9df6458SAndrew Perepechko static const struct file_operations sel_transition_ops = { 829f9df6458SAndrew Perepechko .write = sel_write_validatetrans, 830f9df6458SAndrew Perepechko .llseek = generic_file_llseek, 831f9df6458SAndrew Perepechko }; 832f9df6458SAndrew Perepechko 8331da177e4SLinus Torvalds /* 8341da177e4SLinus Torvalds * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c 8351da177e4SLinus Torvalds */ 8361da177e4SLinus Torvalds static ssize_t sel_write_access(struct file *file, char *buf, size_t size); 8371da177e4SLinus Torvalds static ssize_t sel_write_create(struct file *file, char *buf, size_t size); 8381da177e4SLinus Torvalds static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size); 8391da177e4SLinus Torvalds static ssize_t sel_write_user(struct file *file, char *buf, size_t size); 8401da177e4SLinus Torvalds static ssize_t sel_write_member(struct file *file, char *buf, size_t size); 8411da177e4SLinus Torvalds 842631d2b49SEric Biggers static ssize_t (*const write_op[])(struct file *, char *, size_t) = { 8431da177e4SLinus Torvalds [SEL_ACCESS] = sel_write_access, 8441da177e4SLinus Torvalds [SEL_CREATE] = sel_write_create, 8451da177e4SLinus Torvalds [SEL_RELABEL] = sel_write_relabel, 8461da177e4SLinus Torvalds [SEL_USER] = sel_write_user, 8471da177e4SLinus Torvalds [SEL_MEMBER] = sel_write_member, 848ce9982d0SStephen Smalley [SEL_CONTEXT] = sel_write_context, 8491da177e4SLinus Torvalds }; 8501da177e4SLinus Torvalds 8511da177e4SLinus Torvalds static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos) 8521da177e4SLinus Torvalds { 853496ad9aaSAl Viro ino_t ino = file_inode(file)->i_ino; 8541da177e4SLinus Torvalds char *data; 8551da177e4SLinus Torvalds ssize_t rv; 8561da177e4SLinus Torvalds 8576e20a64aSNicolas Kaiser if (ino >= ARRAY_SIZE(write_op) || !write_op[ino]) 8581da177e4SLinus Torvalds return -EINVAL; 8591da177e4SLinus Torvalds 8601da177e4SLinus Torvalds data = simple_transaction_get(file, buf, size); 8611da177e4SLinus Torvalds if (IS_ERR(data)) 8621da177e4SLinus Torvalds return PTR_ERR(data); 8631da177e4SLinus Torvalds 8641da177e4SLinus Torvalds rv = write_op[ino](file, data, size); 8651da177e4SLinus Torvalds if (rv > 0) { 8661da177e4SLinus Torvalds simple_transaction_set(file, rv); 8671da177e4SLinus Torvalds rv = size; 8681da177e4SLinus Torvalds } 8691da177e4SLinus Torvalds return rv; 8701da177e4SLinus Torvalds } 8711da177e4SLinus Torvalds 8729c2e08c5SArjan van de Ven static const struct file_operations transaction_ops = { 8731da177e4SLinus Torvalds .write = selinux_transaction_write, 8741da177e4SLinus Torvalds .read = simple_transaction_read, 8751da177e4SLinus Torvalds .release = simple_transaction_release, 87657a62c23SArnd Bergmann .llseek = generic_file_llseek, 8771da177e4SLinus Torvalds }; 8781da177e4SLinus Torvalds 8791da177e4SLinus Torvalds /* 8801da177e4SLinus Torvalds * payload - write methods 8811da177e4SLinus Torvalds * If the method has a response, the response should be put in buf, 8821da177e4SLinus Torvalds * and the length returned. Otherwise return 0 or and -error. 8831da177e4SLinus Torvalds */ 8841da177e4SLinus Torvalds 8851da177e4SLinus Torvalds static ssize_t sel_write_access(struct file *file, char *buf, size_t size) 8861da177e4SLinus Torvalds { 887b77a493bSEric Paris char *scon = NULL, *tcon = NULL; 8881da177e4SLinus Torvalds u32 ssid, tsid; 8891da177e4SLinus Torvalds u16 tclass; 8901da177e4SLinus Torvalds struct av_decision avd; 8911da177e4SLinus Torvalds ssize_t length; 8921da177e4SLinus Torvalds 893e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 894be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_AV, NULL); 8951da177e4SLinus Torvalds if (length) 896b77a493bSEric Paris goto out; 8971da177e4SLinus Torvalds 8981da177e4SLinus Torvalds length = -ENOMEM; 89989d155efSJames Morris scon = kzalloc(size + 1, GFP_KERNEL); 9001da177e4SLinus Torvalds if (!scon) 901b77a493bSEric Paris goto out; 9021da177e4SLinus Torvalds 903b77a493bSEric Paris length = -ENOMEM; 90489d155efSJames Morris tcon = kzalloc(size + 1, GFP_KERNEL); 9051da177e4SLinus Torvalds if (!tcon) 9061da177e4SLinus Torvalds goto out; 9071da177e4SLinus Torvalds 9081da177e4SLinus Torvalds length = -EINVAL; 90919439d05SStephen Smalley if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) 910b77a493bSEric Paris goto out; 9111da177e4SLinus Torvalds 912e67b7985SStephen Smalley length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL); 913b77a493bSEric Paris if (length) 914b77a493bSEric Paris goto out; 915b77a493bSEric Paris 916e67b7985SStephen Smalley length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL); 917b77a493bSEric Paris if (length) 918b77a493bSEric Paris goto out; 9191da177e4SLinus Torvalds 920e67b7985SStephen Smalley security_compute_av_user(ssid, tsid, tclass, &avd); 9211da177e4SLinus Torvalds 9221da177e4SLinus Torvalds length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, 9238a6f83afSKaiGai Kohei "%x %x %x %x %u %x", 924f1c6381aSEric Paris avd.allowed, 0xffffffff, 9251da177e4SLinus Torvalds avd.auditallow, avd.auditdeny, 9268a6f83afSKaiGai Kohei avd.seqno, avd.flags); 9271da177e4SLinus Torvalds out: 928b77a493bSEric Paris kfree(tcon); 9291da177e4SLinus Torvalds kfree(scon); 9301da177e4SLinus Torvalds return length; 9311da177e4SLinus Torvalds } 9321da177e4SLinus Torvalds 9331da177e4SLinus Torvalds static ssize_t sel_write_create(struct file *file, char *buf, size_t size) 9341da177e4SLinus Torvalds { 935b77a493bSEric Paris char *scon = NULL, *tcon = NULL; 936f50a3ec9SKohei Kaigai char *namebuf = NULL, *objname = NULL; 9371da177e4SLinus Torvalds u32 ssid, tsid, newsid; 9381da177e4SLinus Torvalds u16 tclass; 9391da177e4SLinus Torvalds ssize_t length; 940b77a493bSEric Paris char *newcon = NULL; 9411da177e4SLinus Torvalds u32 len; 942f50a3ec9SKohei Kaigai int nargs; 9431da177e4SLinus Torvalds 944e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 945be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_CREATE, 946be0554c9SStephen Smalley NULL); 9471da177e4SLinus Torvalds if (length) 948b77a493bSEric Paris goto out; 9491da177e4SLinus Torvalds 9501da177e4SLinus Torvalds length = -ENOMEM; 95189d155efSJames Morris scon = kzalloc(size + 1, GFP_KERNEL); 9521da177e4SLinus Torvalds if (!scon) 953b77a493bSEric Paris goto out; 9541da177e4SLinus Torvalds 955b77a493bSEric Paris length = -ENOMEM; 95689d155efSJames Morris tcon = kzalloc(size + 1, GFP_KERNEL); 9571da177e4SLinus Torvalds if (!tcon) 9581da177e4SLinus Torvalds goto out; 9591da177e4SLinus Torvalds 960f50a3ec9SKohei Kaigai length = -ENOMEM; 961f50a3ec9SKohei Kaigai namebuf = kzalloc(size + 1, GFP_KERNEL); 962f50a3ec9SKohei Kaigai if (!namebuf) 963b77a493bSEric Paris goto out; 9641da177e4SLinus Torvalds 965f50a3ec9SKohei Kaigai length = -EINVAL; 966f50a3ec9SKohei Kaigai nargs = sscanf(buf, "%s %s %hu %s", scon, tcon, &tclass, namebuf); 967f50a3ec9SKohei Kaigai if (nargs < 3 || nargs > 4) 968f50a3ec9SKohei Kaigai goto out; 9690f7e4c33SKohei Kaigai if (nargs == 4) { 9700f7e4c33SKohei Kaigai /* 9710f7e4c33SKohei Kaigai * If and when the name of new object to be queried contains 9720f7e4c33SKohei Kaigai * either whitespace or multibyte characters, they shall be 9730f7e4c33SKohei Kaigai * encoded based on the percentage-encoding rule. 9740f7e4c33SKohei Kaigai * If not encoded, the sscanf logic picks up only left-half 9750f7e4c33SKohei Kaigai * of the supplied name; splitted by a whitespace unexpectedly. 9760f7e4c33SKohei Kaigai */ 9770f7e4c33SKohei Kaigai char *r, *w; 9780f7e4c33SKohei Kaigai int c1, c2; 9790f7e4c33SKohei Kaigai 9800f7e4c33SKohei Kaigai r = w = namebuf; 9810f7e4c33SKohei Kaigai do { 9820f7e4c33SKohei Kaigai c1 = *r++; 9830f7e4c33SKohei Kaigai if (c1 == '+') 9840f7e4c33SKohei Kaigai c1 = ' '; 9850f7e4c33SKohei Kaigai else if (c1 == '%') { 986af7ff2c2SAndy Shevchenko c1 = hex_to_bin(*r++); 987af7ff2c2SAndy Shevchenko if (c1 < 0) 9880f7e4c33SKohei Kaigai goto out; 989af7ff2c2SAndy Shevchenko c2 = hex_to_bin(*r++); 990af7ff2c2SAndy Shevchenko if (c2 < 0) 9910f7e4c33SKohei Kaigai goto out; 9920f7e4c33SKohei Kaigai c1 = (c1 << 4) | c2; 9930f7e4c33SKohei Kaigai } 9940f7e4c33SKohei Kaigai *w++ = c1; 9950f7e4c33SKohei Kaigai } while (c1 != '\0'); 9960f7e4c33SKohei Kaigai 997f50a3ec9SKohei Kaigai objname = namebuf; 9980f7e4c33SKohei Kaigai } 999f50a3ec9SKohei Kaigai 1000e67b7985SStephen Smalley length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL); 1001b77a493bSEric Paris if (length) 1002b77a493bSEric Paris goto out; 1003b77a493bSEric Paris 1004e67b7985SStephen Smalley length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL); 1005b77a493bSEric Paris if (length) 1006b77a493bSEric Paris goto out; 10071da177e4SLinus Torvalds 1008e67b7985SStephen Smalley length = security_transition_sid_user(ssid, tsid, tclass, 10090619f0f5SStephen Smalley objname, &newsid); 1010b77a493bSEric Paris if (length) 1011b77a493bSEric Paris goto out; 10121da177e4SLinus Torvalds 1013e67b7985SStephen Smalley length = security_sid_to_context(newsid, &newcon, &len); 1014b77a493bSEric Paris if (length) 1015b77a493bSEric Paris goto out; 10161da177e4SLinus Torvalds 1017b77a493bSEric Paris length = -ERANGE; 10181da177e4SLinus Torvalds if (len > SIMPLE_TRANSACTION_LIMIT) { 1019f8b69a5fSpeter enderborg pr_err("SELinux: %s: context size (%u) exceeds " 1020744ba35eSEric Paris "payload max\n", __func__, len); 1021b77a493bSEric Paris goto out; 10221da177e4SLinus Torvalds } 10231da177e4SLinus Torvalds 10241da177e4SLinus Torvalds memcpy(buf, newcon, len); 10251da177e4SLinus Torvalds length = len; 10261da177e4SLinus Torvalds out: 1027b77a493bSEric Paris kfree(newcon); 1028f50a3ec9SKohei Kaigai kfree(namebuf); 1029b77a493bSEric Paris kfree(tcon); 10301da177e4SLinus Torvalds kfree(scon); 10311da177e4SLinus Torvalds return length; 10321da177e4SLinus Torvalds } 10331da177e4SLinus Torvalds 10341da177e4SLinus Torvalds static ssize_t sel_write_relabel(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 1043e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 1044be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_RELABEL, 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) 1052b77a493bSEric Paris 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 1063e67b7985SStephen Smalley length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL); 1064b77a493bSEric Paris if (length) 1065b77a493bSEric Paris goto out; 1066b77a493bSEric Paris 1067e67b7985SStephen Smalley length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL); 1068b77a493bSEric Paris if (length) 1069b77a493bSEric Paris goto out; 10701da177e4SLinus Torvalds 1071e67b7985SStephen Smalley length = security_change_sid(ssid, tsid, tclass, &newsid); 1072b77a493bSEric Paris if (length) 1073b77a493bSEric Paris goto out; 10741da177e4SLinus Torvalds 1075e67b7985SStephen Smalley length = security_sid_to_context(newsid, &newcon, &len); 1076b77a493bSEric Paris if (length) 1077b77a493bSEric Paris goto out; 10781da177e4SLinus Torvalds 10791da177e4SLinus Torvalds length = -ERANGE; 1080b77a493bSEric Paris if (len > SIMPLE_TRANSACTION_LIMIT) 1081b77a493bSEric Paris goto out; 10821da177e4SLinus Torvalds 10831da177e4SLinus Torvalds memcpy(buf, newcon, len); 10841da177e4SLinus Torvalds length = len; 10851da177e4SLinus Torvalds out: 1086b77a493bSEric Paris kfree(newcon); 1087b77a493bSEric Paris kfree(tcon); 10881da177e4SLinus Torvalds kfree(scon); 10891da177e4SLinus Torvalds return length; 10901da177e4SLinus Torvalds } 10911da177e4SLinus Torvalds 10921da177e4SLinus Torvalds static ssize_t sel_write_user(struct file *file, char *buf, size_t size) 10931da177e4SLinus Torvalds { 1094b77a493bSEric Paris char *con = NULL, *user = NULL, *ptr; 1095b77a493bSEric Paris u32 sid, *sids = NULL; 10961da177e4SLinus Torvalds ssize_t length; 10971da177e4SLinus Torvalds char *newcon; 10981da177e4SLinus Torvalds int i, rc; 10991da177e4SLinus Torvalds u32 len, nsids; 11001da177e4SLinus Torvalds 1101e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 1102be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_USER, 1103be0554c9SStephen Smalley NULL); 11041da177e4SLinus Torvalds if (length) 11056eab04a8SJustin P. Mattock goto out; 11061da177e4SLinus Torvalds 11071da177e4SLinus Torvalds length = -ENOMEM; 110889d155efSJames Morris con = kzalloc(size + 1, GFP_KERNEL); 11091da177e4SLinus Torvalds if (!con) 11106eab04a8SJustin P. Mattock goto out; 11111da177e4SLinus Torvalds 1112b77a493bSEric Paris length = -ENOMEM; 111389d155efSJames Morris user = kzalloc(size + 1, GFP_KERNEL); 11141da177e4SLinus Torvalds if (!user) 11151da177e4SLinus Torvalds goto out; 11161da177e4SLinus Torvalds 11171da177e4SLinus Torvalds length = -EINVAL; 11181da177e4SLinus Torvalds if (sscanf(buf, "%s %s", con, user) != 2) 1119b77a493bSEric Paris goto out; 11201da177e4SLinus Torvalds 1121e67b7985SStephen Smalley length = security_context_str_to_sid(con, &sid, GFP_KERNEL); 1122b77a493bSEric Paris if (length) 1123b77a493bSEric Paris goto out; 11241da177e4SLinus Torvalds 1125e67b7985SStephen Smalley length = security_get_user_sids(sid, user, &sids, &nsids); 1126b77a493bSEric Paris if (length) 1127b77a493bSEric Paris goto out; 11281da177e4SLinus Torvalds 11291da177e4SLinus Torvalds length = sprintf(buf, "%u", nsids) + 1; 11301da177e4SLinus Torvalds ptr = buf + length; 11311da177e4SLinus Torvalds for (i = 0; i < nsids; i++) { 1132e67b7985SStephen Smalley rc = security_sid_to_context(sids[i], &newcon, &len); 11331da177e4SLinus Torvalds if (rc) { 11341da177e4SLinus Torvalds length = rc; 1135b77a493bSEric Paris goto out; 11361da177e4SLinus Torvalds } 11371da177e4SLinus Torvalds if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) { 11381da177e4SLinus Torvalds kfree(newcon); 11391da177e4SLinus Torvalds length = -ERANGE; 1140b77a493bSEric Paris goto out; 11411da177e4SLinus Torvalds } 11421da177e4SLinus Torvalds memcpy(ptr, newcon, len); 11431da177e4SLinus Torvalds kfree(newcon); 11441da177e4SLinus Torvalds ptr += len; 11451da177e4SLinus Torvalds length += len; 11461da177e4SLinus Torvalds } 11471da177e4SLinus Torvalds out: 1148b77a493bSEric Paris kfree(sids); 1149b77a493bSEric Paris kfree(user); 11501da177e4SLinus Torvalds kfree(con); 11511da177e4SLinus Torvalds return length; 11521da177e4SLinus Torvalds } 11531da177e4SLinus Torvalds 11541da177e4SLinus Torvalds static ssize_t sel_write_member(struct file *file, char *buf, size_t size) 11551da177e4SLinus Torvalds { 1156b77a493bSEric Paris char *scon = NULL, *tcon = NULL; 11571da177e4SLinus Torvalds u32 ssid, tsid, newsid; 11581da177e4SLinus Torvalds u16 tclass; 11591da177e4SLinus Torvalds ssize_t length; 1160b77a493bSEric Paris char *newcon = NULL; 11611da177e4SLinus Torvalds u32 len; 11621da177e4SLinus Torvalds 1163e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 1164be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_MEMBER, 1165be0554c9SStephen Smalley NULL); 11661da177e4SLinus Torvalds if (length) 1167b77a493bSEric Paris goto out; 11681da177e4SLinus Torvalds 11691da177e4SLinus Torvalds length = -ENOMEM; 117089d155efSJames Morris scon = kzalloc(size + 1, GFP_KERNEL); 11711da177e4SLinus Torvalds if (!scon) 11726eab04a8SJustin P. Mattock goto out; 11731da177e4SLinus Torvalds 1174b77a493bSEric Paris length = -ENOMEM; 117589d155efSJames Morris tcon = kzalloc(size + 1, GFP_KERNEL); 11761da177e4SLinus Torvalds if (!tcon) 11771da177e4SLinus Torvalds goto out; 11781da177e4SLinus Torvalds 11791da177e4SLinus Torvalds length = -EINVAL; 11801da177e4SLinus Torvalds if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) 1181b77a493bSEric Paris goto out; 11821da177e4SLinus Torvalds 1183e67b7985SStephen Smalley length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL); 1184b77a493bSEric Paris if (length) 1185b77a493bSEric Paris goto out; 1186b77a493bSEric Paris 1187e67b7985SStephen Smalley length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL); 1188b77a493bSEric Paris if (length) 1189b77a493bSEric Paris goto out; 11901da177e4SLinus Torvalds 1191e67b7985SStephen Smalley length = security_member_sid(ssid, tsid, tclass, &newsid); 1192b77a493bSEric Paris if (length) 1193b77a493bSEric Paris goto out; 11941da177e4SLinus Torvalds 1195e67b7985SStephen Smalley length = security_sid_to_context(newsid, &newcon, &len); 1196b77a493bSEric Paris if (length) 1197b77a493bSEric Paris goto out; 11981da177e4SLinus Torvalds 1199b77a493bSEric Paris length = -ERANGE; 12001da177e4SLinus Torvalds if (len > SIMPLE_TRANSACTION_LIMIT) { 1201f8b69a5fSpeter enderborg pr_err("SELinux: %s: context size (%u) exceeds " 1202744ba35eSEric Paris "payload max\n", __func__, len); 1203b77a493bSEric Paris goto out; 12041da177e4SLinus Torvalds } 12051da177e4SLinus Torvalds 12061da177e4SLinus Torvalds memcpy(buf, newcon, len); 12071da177e4SLinus Torvalds length = len; 12081da177e4SLinus Torvalds out: 1209b77a493bSEric Paris kfree(newcon); 1210b77a493bSEric Paris kfree(tcon); 12111da177e4SLinus Torvalds kfree(scon); 12121da177e4SLinus Torvalds return length; 12131da177e4SLinus Torvalds } 12141da177e4SLinus Torvalds 12151da177e4SLinus Torvalds static struct inode *sel_make_inode(struct super_block *sb, int mode) 12161da177e4SLinus Torvalds { 12171da177e4SLinus Torvalds struct inode *ret = new_inode(sb); 12181da177e4SLinus Torvalds 12191da177e4SLinus Torvalds if (ret) { 12201da177e4SLinus Torvalds ret->i_mode = mode; 1221078cd827SDeepa Dinamani ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret); 12221da177e4SLinus Torvalds } 12231da177e4SLinus Torvalds return ret; 12241da177e4SLinus Torvalds } 12251da177e4SLinus Torvalds 12261da177e4SLinus Torvalds static ssize_t sel_read_bool(struct file *filep, char __user *buf, 12271da177e4SLinus Torvalds size_t count, loff_t *ppos) 12281da177e4SLinus Torvalds { 12290619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info; 12301da177e4SLinus Torvalds char *page = NULL; 12311da177e4SLinus Torvalds ssize_t length; 12321da177e4SLinus Torvalds ssize_t ret; 12331da177e4SLinus Torvalds int cur_enforcing; 1234496ad9aaSAl Viro unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK; 1235d313f948SStephen Smalley const char *name = filep->f_path.dentry->d_name.name; 12361da177e4SLinus Torvalds 1237e67b7985SStephen Smalley mutex_lock(&selinux_state.policy_mutex); 12381da177e4SLinus Torvalds 1239d313f948SStephen Smalley ret = -EINVAL; 12400619f0f5SStephen Smalley if (index >= fsi->bool_num || strcmp(name, 12410619f0f5SStephen Smalley fsi->bool_pending_names[index])) 12420da74120SJann Horn goto out_unlock; 12431da177e4SLinus Torvalds 12441da177e4SLinus Torvalds ret = -ENOMEM; 1245b77a493bSEric Paris page = (char *)get_zeroed_page(GFP_KERNEL); 1246b77a493bSEric Paris if (!page) 12470da74120SJann Horn goto out_unlock; 12481da177e4SLinus Torvalds 1249e67b7985SStephen Smalley cur_enforcing = security_get_bool_value(index); 12501da177e4SLinus Torvalds if (cur_enforcing < 0) { 12511da177e4SLinus Torvalds ret = cur_enforcing; 12520da74120SJann Horn goto out_unlock; 12531da177e4SLinus Torvalds } 12541da177e4SLinus Torvalds length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing, 12550619f0f5SStephen Smalley fsi->bool_pending_values[index]); 1256e67b7985SStephen Smalley mutex_unlock(&selinux_state.policy_mutex); 12570da74120SJann Horn ret = simple_read_from_buffer(buf, count, ppos, page, length); 12580da74120SJann Horn out_free: 12591da177e4SLinus Torvalds free_page((unsigned long)page); 12601da177e4SLinus Torvalds return ret; 12610da74120SJann Horn 12620da74120SJann Horn out_unlock: 1263e67b7985SStephen Smalley mutex_unlock(&selinux_state.policy_mutex); 12640da74120SJann Horn goto out_free; 12651da177e4SLinus Torvalds } 12661da177e4SLinus Torvalds 12671da177e4SLinus Torvalds static ssize_t sel_write_bool(struct file *filep, const char __user *buf, 12681da177e4SLinus Torvalds size_t count, loff_t *ppos) 12691da177e4SLinus Torvalds { 12700619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info; 12711da177e4SLinus Torvalds char *page = NULL; 1272d313f948SStephen Smalley ssize_t length; 12731da177e4SLinus Torvalds int new_value; 1274496ad9aaSAl Viro unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK; 1275d313f948SStephen Smalley const char *name = filep->f_path.dentry->d_name.name; 12761da177e4SLinus Torvalds 12770da74120SJann Horn if (count >= PAGE_SIZE) 12780da74120SJann Horn return -ENOMEM; 12790da74120SJann Horn 12800da74120SJann Horn /* No partial writes. */ 12810da74120SJann Horn if (*ppos != 0) 12820da74120SJann Horn return -EINVAL; 12830da74120SJann Horn 12840da74120SJann Horn page = memdup_user_nul(buf, count); 12850da74120SJann Horn if (IS_ERR(page)) 12860da74120SJann Horn return PTR_ERR(page); 12870da74120SJann Horn 1288e67b7985SStephen Smalley mutex_lock(&selinux_state.policy_mutex); 12891da177e4SLinus Torvalds 1290e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 1291be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETBOOL, 1292be0554c9SStephen Smalley NULL); 12931da177e4SLinus Torvalds if (length) 12941da177e4SLinus Torvalds goto out; 12951da177e4SLinus Torvalds 1296d313f948SStephen Smalley length = -EINVAL; 12970619f0f5SStephen Smalley if (index >= fsi->bool_num || strcmp(name, 12980619f0f5SStephen Smalley fsi->bool_pending_names[index])) 1299d313f948SStephen Smalley goto out; 1300d313f948SStephen Smalley 13011da177e4SLinus Torvalds length = -EINVAL; 13021da177e4SLinus Torvalds if (sscanf(page, "%d", &new_value) != 1) 13031da177e4SLinus Torvalds goto out; 13041da177e4SLinus Torvalds 13051da177e4SLinus Torvalds if (new_value) 13061da177e4SLinus Torvalds new_value = 1; 13071da177e4SLinus Torvalds 13080619f0f5SStephen Smalley fsi->bool_pending_values[index] = new_value; 13091da177e4SLinus Torvalds length = count; 13101da177e4SLinus Torvalds 13111da177e4SLinus Torvalds out: 1312e67b7985SStephen Smalley mutex_unlock(&selinux_state.policy_mutex); 13138365a719SAl Viro kfree(page); 13141da177e4SLinus Torvalds return length; 13151da177e4SLinus Torvalds } 13161da177e4SLinus Torvalds 13179c2e08c5SArjan van de Ven static const struct file_operations sel_bool_ops = { 13181da177e4SLinus Torvalds .read = sel_read_bool, 13191da177e4SLinus Torvalds .write = sel_write_bool, 132057a62c23SArnd Bergmann .llseek = generic_file_llseek, 13211da177e4SLinus Torvalds }; 13221da177e4SLinus Torvalds 13231da177e4SLinus Torvalds static ssize_t sel_commit_bools_write(struct file *filep, 13241da177e4SLinus Torvalds const char __user *buf, 13251da177e4SLinus Torvalds size_t count, loff_t *ppos) 13261da177e4SLinus Torvalds { 13270619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info; 13281da177e4SLinus Torvalds char *page = NULL; 1329d313f948SStephen Smalley ssize_t length; 13301da177e4SLinus Torvalds int new_value; 13311da177e4SLinus Torvalds 13320da74120SJann Horn if (count >= PAGE_SIZE) 13330da74120SJann Horn return -ENOMEM; 13340da74120SJann Horn 13350da74120SJann Horn /* No partial writes. */ 13360da74120SJann Horn if (*ppos != 0) 13370da74120SJann Horn return -EINVAL; 13380da74120SJann Horn 13390da74120SJann Horn page = memdup_user_nul(buf, count); 13400da74120SJann Horn if (IS_ERR(page)) 13410da74120SJann Horn return PTR_ERR(page); 13420da74120SJann Horn 1343e67b7985SStephen Smalley mutex_lock(&selinux_state.policy_mutex); 13441da177e4SLinus Torvalds 1345e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 1346be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETBOOL, 1347be0554c9SStephen Smalley NULL); 13481da177e4SLinus Torvalds if (length) 13491da177e4SLinus Torvalds goto out; 13501da177e4SLinus Torvalds 13511da177e4SLinus Torvalds length = -EINVAL; 13521da177e4SLinus Torvalds if (sscanf(page, "%d", &new_value) != 1) 13531da177e4SLinus Torvalds goto out; 13541da177e4SLinus Torvalds 1355b77a493bSEric Paris length = 0; 13560619f0f5SStephen Smalley if (new_value && fsi->bool_pending_values) 1357e67b7985SStephen Smalley length = security_set_bools(fsi->bool_num, 13580619f0f5SStephen Smalley fsi->bool_pending_values); 13591da177e4SLinus Torvalds 1360b77a493bSEric Paris if (!length) 13611da177e4SLinus Torvalds length = count; 13621da177e4SLinus Torvalds 13631da177e4SLinus Torvalds out: 1364e67b7985SStephen Smalley mutex_unlock(&selinux_state.policy_mutex); 13658365a719SAl Viro kfree(page); 13661da177e4SLinus Torvalds return length; 13671da177e4SLinus Torvalds } 13681da177e4SLinus Torvalds 13699c2e08c5SArjan van de Ven static const struct file_operations sel_commit_bools_ops = { 13701da177e4SLinus Torvalds .write = sel_commit_bools_write, 137157a62c23SArnd Bergmann .llseek = generic_file_llseek, 13721da177e4SLinus Torvalds }; 13731da177e4SLinus Torvalds 13740c92d7c7SChristopher J. PeBenito static void sel_remove_entries(struct dentry *de) 13751da177e4SLinus Torvalds { 1376ad52184bSAl Viro d_genocide(de); 1377ad52184bSAl Viro shrink_dcache_parent(de); 13781da177e4SLinus Torvalds } 13791da177e4SLinus Torvalds 138066ec384aSDaniel Burgener static int sel_make_bools(struct selinux_policy *newpolicy, struct dentry *bool_dir, 138166ec384aSDaniel Burgener unsigned int *bool_num, char ***bool_pending_names, 1382c3fae2b2SChristian Göttsche int **bool_pending_values) 13831da177e4SLinus Torvalds { 138460abd318SOndrej Mosnacek int ret; 13851da177e4SLinus Torvalds ssize_t len; 13861da177e4SLinus Torvalds struct dentry *dentry = NULL; 13871da177e4SLinus Torvalds struct inode *inode = NULL; 13881da177e4SLinus Torvalds struct inode_security_struct *isec; 13891da177e4SLinus Torvalds char **names = NULL, *page; 139060abd318SOndrej Mosnacek u32 i, num; 13911da177e4SLinus Torvalds int *values = NULL; 13921da177e4SLinus Torvalds u32 sid; 13931da177e4SLinus Torvalds 1394b77a493bSEric Paris ret = -ENOMEM; 13951872981bSEric Paris page = (char *)get_zeroed_page(GFP_KERNEL); 13961872981bSEric Paris if (!page) 1397b77a493bSEric Paris goto out; 13981da177e4SLinus Torvalds 139902a52c5cSStephen Smalley ret = security_get_bools(newpolicy, &num, &names, &values); 1400b77a493bSEric Paris if (ret) 14011da177e4SLinus Torvalds goto out; 14021da177e4SLinus Torvalds 14031da177e4SLinus Torvalds for (i = 0; i < num; i++) { 1404b77a493bSEric Paris ret = -ENOMEM; 140566ec384aSDaniel Burgener dentry = d_alloc_name(bool_dir, names[i]); 1406b77a493bSEric Paris if (!dentry) 1407b77a493bSEric Paris goto out; 14081da177e4SLinus Torvalds 1409b77a493bSEric Paris ret = -ENOMEM; 141066ec384aSDaniel Burgener inode = sel_make_inode(bool_dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR); 14117e4237faSnixiaoming if (!inode) { 14127e4237faSnixiaoming dput(dentry); 1413b77a493bSEric Paris goto out; 14147e4237faSnixiaoming } 1415b77a493bSEric Paris 14161da177e4SLinus Torvalds ret = -ENAMETOOLONG; 1417cc1dad71SAl Viro len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]); 14187e4237faSnixiaoming if (len >= PAGE_SIZE) { 14197e4237faSnixiaoming dput(dentry); 14207e4237faSnixiaoming iput(inode); 1421b77a493bSEric Paris goto out; 14227e4237faSnixiaoming } 1423b77a493bSEric Paris 142480788c22SCasey Schaufler isec = selinux_inode(inode); 142502a52c5cSStephen Smalley ret = selinux_policy_genfs_sid(newpolicy, "selinuxfs", page, 1426aa8e712cSStephen Smalley SECCLASS_FILE, &sid); 14274262fb51SGary Tierney if (ret) { 1428900fde06SGary Tierney pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n", 1429900fde06SGary Tierney page); 1430900fde06SGary Tierney sid = SECINITSID_SECURITY; 14314262fb51SGary Tierney } 14324262fb51SGary Tierney 14331da177e4SLinus Torvalds isec->sid = sid; 143442059112SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 14351da177e4SLinus Torvalds inode->i_fop = &sel_bool_ops; 1436bce34bc0SJames Carter inode->i_ino = i|SEL_BOOL_INO_OFFSET; 14371da177e4SLinus Torvalds d_add(dentry, inode); 14381da177e4SLinus Torvalds } 143966ec384aSDaniel Burgener *bool_num = num; 144066ec384aSDaniel Burgener *bool_pending_names = names; 144166ec384aSDaniel Burgener *bool_pending_values = values; 1442b77a493bSEric Paris 1443b77a493bSEric Paris free_page((unsigned long)page); 1444b77a493bSEric Paris return 0; 14451da177e4SLinus Torvalds out: 14461da177e4SLinus Torvalds free_page((unsigned long)page); 1447b77a493bSEric Paris 14481da177e4SLinus Torvalds if (names) { 14499a5f04bfSJesper Juhl for (i = 0; i < num; i++) 14501da177e4SLinus Torvalds kfree(names[i]); 14511da177e4SLinus Torvalds kfree(names); 14521da177e4SLinus Torvalds } 145320c19e41SDavi Arnaut kfree(values); 145466ec384aSDaniel Burgener sel_remove_entries(bool_dir); 1455b77a493bSEric Paris 1456b77a493bSEric Paris return ret; 14571da177e4SLinus Torvalds } 14581da177e4SLinus Torvalds 14591da177e4SLinus Torvalds static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf, 14601da177e4SLinus Torvalds size_t count, loff_t *ppos) 14611da177e4SLinus Torvalds { 14621da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 14631da177e4SLinus Torvalds ssize_t length; 14641da177e4SLinus Torvalds 14656b6bc620SStephen Smalley length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 1466e67b7985SStephen Smalley avc_get_cache_threshold()); 14671da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 14681da177e4SLinus Torvalds } 14691da177e4SLinus Torvalds 14701da177e4SLinus Torvalds static ssize_t sel_write_avc_cache_threshold(struct file *file, 14711da177e4SLinus Torvalds const char __user *buf, 14721da177e4SLinus Torvalds size_t count, loff_t *ppos) 14731da177e4SLinus Torvalds 14741da177e4SLinus Torvalds { 14758365a719SAl Viro char *page; 14761da177e4SLinus Torvalds ssize_t ret; 1477309c5fadSHeinrich Schuchardt unsigned int new_value; 14781da177e4SLinus Torvalds 1479e67b7985SStephen Smalley ret = avc_has_perm(current_sid(), SECINITSID_SECURITY, 1480be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETSECPARAM, 1481be0554c9SStephen Smalley NULL); 14821da177e4SLinus Torvalds if (ret) 14838365a719SAl Viro return ret; 1484b77a493bSEric Paris 1485b77a493bSEric Paris if (count >= PAGE_SIZE) 14868365a719SAl Viro return -ENOMEM; 1487b77a493bSEric Paris 1488b77a493bSEric Paris /* No partial writes. */ 1489b77a493bSEric Paris if (*ppos != 0) 14908365a719SAl Viro return -EINVAL; 1491b77a493bSEric Paris 14928365a719SAl Viro page = memdup_user_nul(buf, count); 14938365a719SAl Viro if (IS_ERR(page)) 14948365a719SAl Viro return PTR_ERR(page); 1495b77a493bSEric Paris 1496b77a493bSEric Paris ret = -EINVAL; 1497b77a493bSEric Paris if (sscanf(page, "%u", &new_value) != 1) 1498b77a493bSEric Paris goto out; 1499b77a493bSEric Paris 1500e67b7985SStephen Smalley avc_set_cache_threshold(new_value); 1501b77a493bSEric Paris 15021da177e4SLinus Torvalds ret = count; 15031da177e4SLinus Torvalds out: 15048365a719SAl Viro kfree(page); 15051da177e4SLinus Torvalds return ret; 15061da177e4SLinus Torvalds } 15071da177e4SLinus Torvalds 15081da177e4SLinus Torvalds static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf, 15091da177e4SLinus Torvalds size_t count, loff_t *ppos) 15101da177e4SLinus Torvalds { 15111da177e4SLinus Torvalds char *page; 1512b77a493bSEric Paris ssize_t length; 15131da177e4SLinus Torvalds 15141da177e4SLinus Torvalds page = (char *)__get_free_page(GFP_KERNEL); 1515b77a493bSEric Paris if (!page) 1516b77a493bSEric Paris return -ENOMEM; 1517b77a493bSEric Paris 1518e67b7985SStephen Smalley length = avc_get_hash_stats(page); 1519b77a493bSEric Paris if (length >= 0) 1520b77a493bSEric Paris length = simple_read_from_buffer(buf, count, ppos, page, length); 15211da177e4SLinus Torvalds free_page((unsigned long)page); 1522b77a493bSEric Paris 1523b77a493bSEric Paris return length; 15241da177e4SLinus Torvalds } 15251da177e4SLinus Torvalds 152666f8e2f0SJeff Vander Stoep static ssize_t sel_read_sidtab_hash_stats(struct file *filp, char __user *buf, 152766f8e2f0SJeff Vander Stoep size_t count, loff_t *ppos) 152866f8e2f0SJeff Vander Stoep { 152966f8e2f0SJeff Vander Stoep char *page; 153066f8e2f0SJeff Vander Stoep ssize_t length; 153166f8e2f0SJeff Vander Stoep 153266f8e2f0SJeff Vander Stoep page = (char *)__get_free_page(GFP_KERNEL); 153366f8e2f0SJeff Vander Stoep if (!page) 153466f8e2f0SJeff Vander Stoep return -ENOMEM; 153566f8e2f0SJeff Vander Stoep 1536e67b7985SStephen Smalley length = security_sidtab_hash_stats(page); 153766f8e2f0SJeff Vander Stoep if (length >= 0) 153866f8e2f0SJeff Vander Stoep length = simple_read_from_buffer(buf, count, ppos, page, 153966f8e2f0SJeff Vander Stoep length); 154066f8e2f0SJeff Vander Stoep free_page((unsigned long)page); 154166f8e2f0SJeff Vander Stoep 154266f8e2f0SJeff Vander Stoep return length; 154366f8e2f0SJeff Vander Stoep } 154466f8e2f0SJeff Vander Stoep 154566f8e2f0SJeff Vander Stoep static const struct file_operations sel_sidtab_hash_stats_ops = { 154666f8e2f0SJeff Vander Stoep .read = sel_read_sidtab_hash_stats, 154766f8e2f0SJeff Vander Stoep .llseek = generic_file_llseek, 154866f8e2f0SJeff Vander Stoep }; 154966f8e2f0SJeff Vander Stoep 15509c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_threshold_ops = { 15511da177e4SLinus Torvalds .read = sel_read_avc_cache_threshold, 15521da177e4SLinus Torvalds .write = sel_write_avc_cache_threshold, 155357a62c23SArnd Bergmann .llseek = generic_file_llseek, 15541da177e4SLinus Torvalds }; 15551da177e4SLinus Torvalds 15569c2e08c5SArjan van de Ven static const struct file_operations sel_avc_hash_stats_ops = { 15571da177e4SLinus Torvalds .read = sel_read_avc_hash_stats, 155857a62c23SArnd Bergmann .llseek = generic_file_llseek, 15591da177e4SLinus Torvalds }; 15601da177e4SLinus Torvalds 15611da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS 15621da177e4SLinus Torvalds static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx) 15631da177e4SLinus Torvalds { 15641da177e4SLinus Torvalds int cpu; 15651da177e4SLinus Torvalds 15664f4b6c1aSRusty Russell for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) { 15671da177e4SLinus Torvalds if (!cpu_possible(cpu)) 15681da177e4SLinus Torvalds continue; 15691da177e4SLinus Torvalds *idx = cpu + 1; 15701da177e4SLinus Torvalds return &per_cpu(avc_cache_stats, cpu); 15711da177e4SLinus Torvalds } 15728d269a8eSVasily Averin (*idx)++; 15731da177e4SLinus Torvalds return NULL; 15741da177e4SLinus Torvalds } 15751da177e4SLinus Torvalds 15761da177e4SLinus Torvalds static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos) 15771da177e4SLinus Torvalds { 15781da177e4SLinus Torvalds loff_t n = *pos - 1; 15791da177e4SLinus Torvalds 15801da177e4SLinus Torvalds if (*pos == 0) 15811da177e4SLinus Torvalds return SEQ_START_TOKEN; 15821da177e4SLinus Torvalds 15831da177e4SLinus Torvalds return sel_avc_get_stat_idx(&n); 15841da177e4SLinus Torvalds } 15851da177e4SLinus Torvalds 15861da177e4SLinus Torvalds static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos) 15871da177e4SLinus Torvalds { 15881da177e4SLinus Torvalds return sel_avc_get_stat_idx(pos); 15891da177e4SLinus Torvalds } 15901da177e4SLinus Torvalds 15911da177e4SLinus Torvalds static int sel_avc_stats_seq_show(struct seq_file *seq, void *v) 15921da177e4SLinus Torvalds { 15931da177e4SLinus Torvalds struct avc_cache_stats *st = v; 15941da177e4SLinus Torvalds 1595710a0647SMarkus Elfring if (v == SEQ_START_TOKEN) { 1596710a0647SMarkus Elfring seq_puts(seq, 1597710a0647SMarkus Elfring "lookups hits misses allocations reclaims frees\n"); 1598710a0647SMarkus Elfring } else { 1599257313b2SLinus Torvalds unsigned int lookups = st->lookups; 1600257313b2SLinus Torvalds unsigned int misses = st->misses; 1601257313b2SLinus Torvalds unsigned int hits = lookups - misses; 1602257313b2SLinus Torvalds seq_printf(seq, "%u %u %u %u %u %u\n", lookups, 1603257313b2SLinus Torvalds hits, misses, st->allocations, 16041da177e4SLinus Torvalds st->reclaims, st->frees); 1605257313b2SLinus Torvalds } 16061da177e4SLinus Torvalds return 0; 16071da177e4SLinus Torvalds } 16081da177e4SLinus Torvalds 16091da177e4SLinus Torvalds static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v) 16101da177e4SLinus Torvalds { } 16111da177e4SLinus Torvalds 16121996a109SJan Engelhardt static const struct seq_operations sel_avc_cache_stats_seq_ops = { 16131da177e4SLinus Torvalds .start = sel_avc_stats_seq_start, 16141da177e4SLinus Torvalds .next = sel_avc_stats_seq_next, 16151da177e4SLinus Torvalds .show = sel_avc_stats_seq_show, 16161da177e4SLinus Torvalds .stop = sel_avc_stats_seq_stop, 16171da177e4SLinus Torvalds }; 16181da177e4SLinus Torvalds 16191da177e4SLinus Torvalds static int sel_open_avc_cache_stats(struct inode *inode, struct file *file) 16201da177e4SLinus Torvalds { 16211da177e4SLinus Torvalds return seq_open(file, &sel_avc_cache_stats_seq_ops); 16221da177e4SLinus Torvalds } 16231da177e4SLinus Torvalds 16249c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_stats_ops = { 16251da177e4SLinus Torvalds .open = sel_open_avc_cache_stats, 16261da177e4SLinus Torvalds .read = seq_read, 16271da177e4SLinus Torvalds .llseek = seq_lseek, 16281da177e4SLinus Torvalds .release = seq_release, 16291da177e4SLinus Torvalds }; 16301da177e4SLinus Torvalds #endif 16311da177e4SLinus Torvalds 16321da177e4SLinus Torvalds static int sel_make_avc_files(struct dentry *dir) 16331da177e4SLinus Torvalds { 16340619f0f5SStephen Smalley struct super_block *sb = dir->d_sb; 16350619f0f5SStephen Smalley struct selinux_fs_info *fsi = sb->s_fs_info; 1636b77a493bSEric Paris int i; 1637cda37124SEric Biggers static const struct tree_descr files[] = { 16381da177e4SLinus Torvalds { "cache_threshold", 16391da177e4SLinus Torvalds &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR }, 16401da177e4SLinus Torvalds { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO }, 16411da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS 16421da177e4SLinus Torvalds { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO }, 16431da177e4SLinus Torvalds #endif 16441da177e4SLinus Torvalds }; 16451da177e4SLinus Torvalds 16466e20a64aSNicolas Kaiser for (i = 0; i < ARRAY_SIZE(files); i++) { 16471da177e4SLinus Torvalds struct inode *inode; 16481da177e4SLinus Torvalds struct dentry *dentry; 16491da177e4SLinus Torvalds 16501da177e4SLinus Torvalds dentry = d_alloc_name(dir, files[i].name); 1651b77a493bSEric Paris if (!dentry) 1652b77a493bSEric Paris return -ENOMEM; 16531da177e4SLinus Torvalds 16541da177e4SLinus Torvalds inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode); 16557e4237faSnixiaoming if (!inode) { 16567e4237faSnixiaoming dput(dentry); 1657b77a493bSEric Paris return -ENOMEM; 16587e4237faSnixiaoming } 1659b77a493bSEric Paris 16601da177e4SLinus Torvalds inode->i_fop = files[i].ops; 16610619f0f5SStephen Smalley inode->i_ino = ++fsi->last_ino; 16621da177e4SLinus Torvalds d_add(dentry, inode); 16631da177e4SLinus Torvalds } 1664b77a493bSEric Paris 1665b77a493bSEric Paris return 0; 16661da177e4SLinus Torvalds } 16671da177e4SLinus Torvalds 166866f8e2f0SJeff Vander Stoep static int sel_make_ss_files(struct dentry *dir) 166966f8e2f0SJeff Vander Stoep { 167066f8e2f0SJeff Vander Stoep struct super_block *sb = dir->d_sb; 167166f8e2f0SJeff Vander Stoep struct selinux_fs_info *fsi = sb->s_fs_info; 167266f8e2f0SJeff Vander Stoep int i; 167366f8e2f0SJeff Vander Stoep static struct tree_descr files[] = { 167466f8e2f0SJeff Vander Stoep { "sidtab_hash_stats", &sel_sidtab_hash_stats_ops, S_IRUGO }, 167566f8e2f0SJeff Vander Stoep }; 167666f8e2f0SJeff Vander Stoep 167766f8e2f0SJeff Vander Stoep for (i = 0; i < ARRAY_SIZE(files); i++) { 167866f8e2f0SJeff Vander Stoep struct inode *inode; 167966f8e2f0SJeff Vander Stoep struct dentry *dentry; 168066f8e2f0SJeff Vander Stoep 168166f8e2f0SJeff Vander Stoep dentry = d_alloc_name(dir, files[i].name); 168266f8e2f0SJeff Vander Stoep if (!dentry) 168366f8e2f0SJeff Vander Stoep return -ENOMEM; 168466f8e2f0SJeff Vander Stoep 168566f8e2f0SJeff Vander Stoep inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode); 168666f8e2f0SJeff Vander Stoep if (!inode) { 168766f8e2f0SJeff Vander Stoep dput(dentry); 168866f8e2f0SJeff Vander Stoep return -ENOMEM; 168966f8e2f0SJeff Vander Stoep } 169066f8e2f0SJeff Vander Stoep 169166f8e2f0SJeff Vander Stoep inode->i_fop = files[i].ops; 169266f8e2f0SJeff Vander Stoep inode->i_ino = ++fsi->last_ino; 169366f8e2f0SJeff Vander Stoep d_add(dentry, inode); 169466f8e2f0SJeff Vander Stoep } 169566f8e2f0SJeff Vander Stoep 169666f8e2f0SJeff Vander Stoep return 0; 169766f8e2f0SJeff Vander Stoep } 169866f8e2f0SJeff Vander Stoep 1699f0ee2e46SJames Carter static ssize_t sel_read_initcon(struct file *file, char __user *buf, 1700f0ee2e46SJames Carter size_t count, loff_t *ppos) 1701f0ee2e46SJames Carter { 1702f0ee2e46SJames Carter char *con; 1703f0ee2e46SJames Carter u32 sid, len; 1704f0ee2e46SJames Carter ssize_t ret; 1705f0ee2e46SJames Carter 1706496ad9aaSAl Viro sid = file_inode(file)->i_ino&SEL_INO_MASK; 1707e67b7985SStephen Smalley ret = security_sid_to_context(sid, &con, &len); 1708b77a493bSEric Paris if (ret) 1709f0ee2e46SJames Carter return ret; 1710f0ee2e46SJames Carter 1711f0ee2e46SJames Carter ret = simple_read_from_buffer(buf, count, ppos, con, len); 1712f0ee2e46SJames Carter kfree(con); 1713f0ee2e46SJames Carter return ret; 1714f0ee2e46SJames Carter } 1715f0ee2e46SJames Carter 1716f0ee2e46SJames Carter static const struct file_operations sel_initcon_ops = { 1717f0ee2e46SJames Carter .read = sel_read_initcon, 171857a62c23SArnd Bergmann .llseek = generic_file_llseek, 1719f0ee2e46SJames Carter }; 1720f0ee2e46SJames Carter 1721f0ee2e46SJames Carter static int sel_make_initcon_files(struct dentry *dir) 1722f0ee2e46SJames Carter { 1723b77a493bSEric Paris int i; 1724f0ee2e46SJames Carter 1725f0ee2e46SJames Carter for (i = 1; i <= SECINITSID_NUM; i++) { 1726f0ee2e46SJames Carter struct inode *inode; 1727f0ee2e46SJames Carter struct dentry *dentry; 1728e3e0b582SStephen Smalley const char *s = security_get_initial_sid_context(i); 1729e3e0b582SStephen Smalley 1730e3e0b582SStephen Smalley if (!s) 1731e3e0b582SStephen Smalley continue; 1732e3e0b582SStephen Smalley dentry = d_alloc_name(dir, s); 1733b77a493bSEric Paris if (!dentry) 1734b77a493bSEric Paris return -ENOMEM; 1735f0ee2e46SJames Carter 1736f0ee2e46SJames Carter inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); 17377e4237faSnixiaoming if (!inode) { 17387e4237faSnixiaoming dput(dentry); 1739b77a493bSEric Paris return -ENOMEM; 17407e4237faSnixiaoming } 1741b77a493bSEric Paris 1742f0ee2e46SJames Carter inode->i_fop = &sel_initcon_ops; 1743f0ee2e46SJames Carter inode->i_ino = i|SEL_INITCON_INO_OFFSET; 1744f0ee2e46SJames Carter d_add(dentry, inode); 1745f0ee2e46SJames Carter } 1746b77a493bSEric Paris 1747b77a493bSEric Paris return 0; 1748f0ee2e46SJames Carter } 1749f0ee2e46SJames Carter 1750e47c8fc5SChristopher J. PeBenito static inline unsigned long sel_class_to_ino(u16 class) 1751e47c8fc5SChristopher J. PeBenito { 1752e47c8fc5SChristopher J. PeBenito return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET; 1753e47c8fc5SChristopher J. PeBenito } 1754e47c8fc5SChristopher J. PeBenito 1755e47c8fc5SChristopher J. PeBenito static inline u16 sel_ino_to_class(unsigned long ino) 1756e47c8fc5SChristopher J. PeBenito { 175792ae9e82SEric Paris return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1); 1758e47c8fc5SChristopher J. PeBenito } 1759e47c8fc5SChristopher J. PeBenito 1760e47c8fc5SChristopher J. PeBenito static inline unsigned long sel_perm_to_ino(u16 class, u32 perm) 1761e47c8fc5SChristopher J. PeBenito { 1762e47c8fc5SChristopher J. PeBenito return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET; 1763e47c8fc5SChristopher J. PeBenito } 1764e47c8fc5SChristopher J. PeBenito 1765e47c8fc5SChristopher J. PeBenito static inline u32 sel_ino_to_perm(unsigned long ino) 1766e47c8fc5SChristopher J. PeBenito { 1767e47c8fc5SChristopher J. PeBenito return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1); 1768e47c8fc5SChristopher J. PeBenito } 1769e47c8fc5SChristopher J. PeBenito 1770e47c8fc5SChristopher J. PeBenito static ssize_t sel_read_class(struct file *file, char __user *buf, 1771e47c8fc5SChristopher J. PeBenito size_t count, loff_t *ppos) 1772e47c8fc5SChristopher J. PeBenito { 1773496ad9aaSAl Viro unsigned long ino = file_inode(file)->i_ino; 1774cc1dad71SAl Viro char res[TMPBUFLEN]; 17757e78c875Sliuyang34 ssize_t len = scnprintf(res, sizeof(res), "%d", sel_ino_to_class(ino)); 1776cc1dad71SAl Viro return simple_read_from_buffer(buf, count, ppos, res, len); 1777e47c8fc5SChristopher J. PeBenito } 1778e47c8fc5SChristopher J. PeBenito 1779e47c8fc5SChristopher J. PeBenito static const struct file_operations sel_class_ops = { 1780e47c8fc5SChristopher J. PeBenito .read = sel_read_class, 178157a62c23SArnd Bergmann .llseek = generic_file_llseek, 1782e47c8fc5SChristopher J. PeBenito }; 1783e47c8fc5SChristopher J. PeBenito 1784e47c8fc5SChristopher J. PeBenito static ssize_t sel_read_perm(struct file *file, char __user *buf, 1785e47c8fc5SChristopher J. PeBenito size_t count, loff_t *ppos) 1786e47c8fc5SChristopher J. PeBenito { 1787496ad9aaSAl Viro unsigned long ino = file_inode(file)->i_ino; 1788cc1dad71SAl Viro char res[TMPBUFLEN]; 17897e78c875Sliuyang34 ssize_t len = scnprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino)); 1790cc1dad71SAl Viro return simple_read_from_buffer(buf, count, ppos, res, len); 1791e47c8fc5SChristopher J. PeBenito } 1792e47c8fc5SChristopher J. PeBenito 1793e47c8fc5SChristopher J. PeBenito static const struct file_operations sel_perm_ops = { 1794e47c8fc5SChristopher J. PeBenito .read = sel_read_perm, 179557a62c23SArnd Bergmann .llseek = generic_file_llseek, 1796e47c8fc5SChristopher J. PeBenito }; 1797e47c8fc5SChristopher J. PeBenito 17983bb56b25SPaul Moore static ssize_t sel_read_policycap(struct file *file, char __user *buf, 17993bb56b25SPaul Moore size_t count, loff_t *ppos) 18003bb56b25SPaul Moore { 18013bb56b25SPaul Moore int value; 18023bb56b25SPaul Moore char tmpbuf[TMPBUFLEN]; 18033bb56b25SPaul Moore ssize_t length; 1804496ad9aaSAl Viro unsigned long i_ino = file_inode(file)->i_ino; 18053bb56b25SPaul Moore 1806e67b7985SStephen Smalley value = security_policycap_supported(i_ino & SEL_INO_MASK); 18073bb56b25SPaul Moore length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value); 18083bb56b25SPaul Moore 18093bb56b25SPaul Moore return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 18103bb56b25SPaul Moore } 18113bb56b25SPaul Moore 18123bb56b25SPaul Moore static const struct file_operations sel_policycap_ops = { 18133bb56b25SPaul Moore .read = sel_read_policycap, 181457a62c23SArnd Bergmann .llseek = generic_file_llseek, 18153bb56b25SPaul Moore }; 18163bb56b25SPaul Moore 181702a52c5cSStephen Smalley static int sel_make_perm_files(struct selinux_policy *newpolicy, 181802a52c5cSStephen Smalley char *objclass, int classvalue, 1819e47c8fc5SChristopher J. PeBenito struct dentry *dir) 1820e47c8fc5SChristopher J. PeBenito { 1821b77a493bSEric Paris int i, rc, nperms; 1822e47c8fc5SChristopher J. PeBenito char **perms; 1823e47c8fc5SChristopher J. PeBenito 182402a52c5cSStephen Smalley rc = security_get_permissions(newpolicy, objclass, &perms, &nperms); 1825e47c8fc5SChristopher J. PeBenito if (rc) 1826b77a493bSEric Paris return rc; 1827e47c8fc5SChristopher J. PeBenito 1828e47c8fc5SChristopher J. PeBenito for (i = 0; i < nperms; i++) { 1829e47c8fc5SChristopher J. PeBenito struct inode *inode; 1830e47c8fc5SChristopher J. PeBenito struct dentry *dentry; 1831e47c8fc5SChristopher J. PeBenito 1832b77a493bSEric Paris rc = -ENOMEM; 1833e47c8fc5SChristopher J. PeBenito dentry = d_alloc_name(dir, perms[i]); 1834b77a493bSEric Paris if (!dentry) 1835b77a493bSEric Paris goto out; 1836e47c8fc5SChristopher J. PeBenito 1837e47c8fc5SChristopher J. PeBenito rc = -ENOMEM; 1838b77a493bSEric Paris inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); 18397e4237faSnixiaoming if (!inode) { 18407e4237faSnixiaoming dput(dentry); 1841b77a493bSEric Paris goto out; 18427e4237faSnixiaoming } 1843b77a493bSEric Paris 1844e47c8fc5SChristopher J. PeBenito inode->i_fop = &sel_perm_ops; 1845e47c8fc5SChristopher J. PeBenito /* i+1 since perm values are 1-indexed */ 1846e47c8fc5SChristopher J. PeBenito inode->i_ino = sel_perm_to_ino(classvalue, i + 1); 1847e47c8fc5SChristopher J. PeBenito d_add(dentry, inode); 1848e47c8fc5SChristopher J. PeBenito } 1849b77a493bSEric Paris rc = 0; 1850b77a493bSEric Paris out: 1851e47c8fc5SChristopher J. PeBenito for (i = 0; i < nperms; i++) 1852e47c8fc5SChristopher J. PeBenito kfree(perms[i]); 1853e47c8fc5SChristopher J. PeBenito kfree(perms); 1854e47c8fc5SChristopher J. PeBenito return rc; 1855e47c8fc5SChristopher J. PeBenito } 1856e47c8fc5SChristopher J. PeBenito 185702a52c5cSStephen Smalley static int sel_make_class_dir_entries(struct selinux_policy *newpolicy, 185802a52c5cSStephen Smalley char *classname, int index, 1859e47c8fc5SChristopher J. PeBenito struct dentry *dir) 1860e47c8fc5SChristopher J. PeBenito { 18610619f0f5SStephen Smalley struct super_block *sb = dir->d_sb; 18620619f0f5SStephen Smalley struct selinux_fs_info *fsi = sb->s_fs_info; 1863e47c8fc5SChristopher J. PeBenito struct dentry *dentry = NULL; 1864e47c8fc5SChristopher J. PeBenito struct inode *inode = NULL; 1865e47c8fc5SChristopher J. PeBenito 1866e47c8fc5SChristopher J. PeBenito dentry = d_alloc_name(dir, "index"); 1867b77a493bSEric Paris if (!dentry) 1868b77a493bSEric Paris return -ENOMEM; 1869e47c8fc5SChristopher J. PeBenito 1870e47c8fc5SChristopher J. PeBenito inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); 18717e4237faSnixiaoming if (!inode) { 18727e4237faSnixiaoming dput(dentry); 1873b77a493bSEric Paris return -ENOMEM; 18747e4237faSnixiaoming } 1875e47c8fc5SChristopher J. PeBenito 1876e47c8fc5SChristopher J. PeBenito inode->i_fop = &sel_class_ops; 1877e47c8fc5SChristopher J. PeBenito inode->i_ino = sel_class_to_ino(index); 1878e47c8fc5SChristopher J. PeBenito d_add(dentry, inode); 1879e47c8fc5SChristopher J. PeBenito 18800619f0f5SStephen Smalley dentry = sel_make_dir(dir, "perms", &fsi->last_class_ino); 1881a1c2aa1eSAl Viro if (IS_ERR(dentry)) 1882a1c2aa1eSAl Viro return PTR_ERR(dentry); 1883e47c8fc5SChristopher J. PeBenito 18845698f081Sye xingchen return sel_make_perm_files(newpolicy, classname, index, dentry); 1885e47c8fc5SChristopher J. PeBenito } 1886e47c8fc5SChristopher J. PeBenito 188766ec384aSDaniel Burgener static int sel_make_classes(struct selinux_policy *newpolicy, 188866ec384aSDaniel Burgener struct dentry *class_dir, 188966ec384aSDaniel Burgener unsigned long *last_class_ino) 1890e47c8fc5SChristopher J. PeBenito { 18910619f0f5SStephen Smalley 1892b77a493bSEric Paris int rc, nclasses, i; 1893e47c8fc5SChristopher J. PeBenito char **classes; 1894e47c8fc5SChristopher J. PeBenito 189502a52c5cSStephen Smalley rc = security_get_classes(newpolicy, &classes, &nclasses); 1896b77a493bSEric Paris if (rc) 1897b77a493bSEric Paris return rc; 1898e47c8fc5SChristopher J. PeBenito 1899e47c8fc5SChristopher J. PeBenito /* +2 since classes are 1-indexed */ 190066ec384aSDaniel Burgener *last_class_ino = sel_class_to_ino(nclasses + 2); 1901e47c8fc5SChristopher J. PeBenito 1902e47c8fc5SChristopher J. PeBenito for (i = 0; i < nclasses; i++) { 1903e47c8fc5SChristopher J. PeBenito struct dentry *class_name_dir; 1904e47c8fc5SChristopher J. PeBenito 190566ec384aSDaniel Burgener class_name_dir = sel_make_dir(class_dir, classes[i], 190666ec384aSDaniel Burgener last_class_ino); 1907a1c2aa1eSAl Viro if (IS_ERR(class_name_dir)) { 1908a1c2aa1eSAl Viro rc = PTR_ERR(class_name_dir); 1909b77a493bSEric Paris goto out; 1910a1c2aa1eSAl Viro } 1911e47c8fc5SChristopher J. PeBenito 1912e47c8fc5SChristopher J. PeBenito /* i+1 since class values are 1-indexed */ 191302a52c5cSStephen Smalley rc = sel_make_class_dir_entries(newpolicy, classes[i], i + 1, 1914e47c8fc5SChristopher J. PeBenito class_name_dir); 1915e47c8fc5SChristopher J. PeBenito if (rc) 1916b77a493bSEric Paris goto out; 1917e47c8fc5SChristopher J. PeBenito } 1918b77a493bSEric Paris rc = 0; 1919b77a493bSEric Paris out: 1920e47c8fc5SChristopher J. PeBenito for (i = 0; i < nclasses; i++) 1921e47c8fc5SChristopher J. PeBenito kfree(classes[i]); 1922e47c8fc5SChristopher J. PeBenito kfree(classes); 1923e47c8fc5SChristopher J. PeBenito return rc; 1924e47c8fc5SChristopher J. PeBenito } 1925e47c8fc5SChristopher J. PeBenito 19260619f0f5SStephen Smalley static int sel_make_policycap(struct selinux_fs_info *fsi) 19273bb56b25SPaul Moore { 19283bb56b25SPaul Moore unsigned int iter; 19293bb56b25SPaul Moore struct dentry *dentry = NULL; 19303bb56b25SPaul Moore struct inode *inode = NULL; 19313bb56b25SPaul Moore 1932cdbec3edSPaul Moore for (iter = 0; iter <= POLICYDB_CAP_MAX; iter++) { 19334dc2fce3SStephen Smalley if (iter < ARRAY_SIZE(selinux_policycap_names)) 19340619f0f5SStephen Smalley dentry = d_alloc_name(fsi->policycap_dir, 19354dc2fce3SStephen Smalley selinux_policycap_names[iter]); 19363bb56b25SPaul Moore else 19370619f0f5SStephen Smalley dentry = d_alloc_name(fsi->policycap_dir, "unknown"); 19383bb56b25SPaul Moore 19393bb56b25SPaul Moore if (dentry == NULL) 19403bb56b25SPaul Moore return -ENOMEM; 19413bb56b25SPaul Moore 19420619f0f5SStephen Smalley inode = sel_make_inode(fsi->sb, S_IFREG | 0444); 19437e4237faSnixiaoming if (inode == NULL) { 19447e4237faSnixiaoming dput(dentry); 19453bb56b25SPaul Moore return -ENOMEM; 19467e4237faSnixiaoming } 19473bb56b25SPaul Moore 19483bb56b25SPaul Moore inode->i_fop = &sel_policycap_ops; 19493bb56b25SPaul Moore inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET; 19503bb56b25SPaul Moore d_add(dentry, inode); 19513bb56b25SPaul Moore } 19523bb56b25SPaul Moore 19533bb56b25SPaul Moore return 0; 19543bb56b25SPaul Moore } 19553bb56b25SPaul Moore 1956a1c2aa1eSAl Viro static struct dentry *sel_make_dir(struct dentry *dir, const char *name, 19570dd4ae51SChristopher J. PeBenito unsigned long *ino) 19581da177e4SLinus Torvalds { 1959a1c2aa1eSAl Viro struct dentry *dentry = d_alloc_name(dir, name); 19601da177e4SLinus Torvalds struct inode *inode; 19611da177e4SLinus Torvalds 1962a1c2aa1eSAl Viro if (!dentry) 1963a1c2aa1eSAl Viro return ERR_PTR(-ENOMEM); 1964a1c2aa1eSAl Viro 1965a1c2aa1eSAl Viro inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO); 1966a1c2aa1eSAl Viro if (!inode) { 1967a1c2aa1eSAl Viro dput(dentry); 1968a1c2aa1eSAl Viro return ERR_PTR(-ENOMEM); 1969a1c2aa1eSAl Viro } 1970b77a493bSEric Paris 19711da177e4SLinus Torvalds inode->i_op = &simple_dir_inode_operations; 19721da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations; 19730dd4ae51SChristopher J. PeBenito inode->i_ino = ++(*ino); 197440e906f8SJames Morris /* directory inodes start off with i_nlink == 2 (for "." entry) */ 1975d8c76e6fSDave Hansen inc_nlink(inode); 19761da177e4SLinus Torvalds d_add(dentry, inode); 1977edb20fb5SJames Morris /* bump link count on parent directory, too */ 1978ce0b16ddSDavid Howells inc_nlink(d_inode(dir)); 1979b77a493bSEric Paris 1980a1c2aa1eSAl Viro return dentry; 19811da177e4SLinus Torvalds } 19821da177e4SLinus Torvalds 19830eea6091SDaniel Burgener static struct dentry *sel_make_disconnected_dir(struct super_block *sb, 19840eea6091SDaniel Burgener unsigned long *ino) 19850eea6091SDaniel Burgener { 19860eea6091SDaniel Burgener struct inode *inode = sel_make_inode(sb, S_IFDIR | S_IRUGO | S_IXUGO); 19870eea6091SDaniel Burgener 19880eea6091SDaniel Burgener if (!inode) 19890eea6091SDaniel Burgener return ERR_PTR(-ENOMEM); 19900eea6091SDaniel Burgener 19910eea6091SDaniel Burgener inode->i_op = &simple_dir_inode_operations; 19920eea6091SDaniel Burgener inode->i_fop = &simple_dir_operations; 19930eea6091SDaniel Burgener inode->i_ino = ++(*ino); 19940eea6091SDaniel Burgener /* directory inodes start off with i_nlink == 2 (for "." entry) */ 19950eea6091SDaniel Burgener inc_nlink(inode); 19960eea6091SDaniel Burgener return d_obtain_alias(inode); 19970eea6091SDaniel Burgener } 19980eea6091SDaniel Burgener 19990619f0f5SStephen Smalley #define NULL_FILE_NAME "null" 20000619f0f5SStephen Smalley 2001920f50b2SDavid Howells static int sel_fill_super(struct super_block *sb, struct fs_context *fc) 20021da177e4SLinus Torvalds { 20030619f0f5SStephen Smalley struct selinux_fs_info *fsi; 20041da177e4SLinus Torvalds int ret; 20051da177e4SLinus Torvalds struct dentry *dentry; 2006a1c2aa1eSAl Viro struct inode *inode; 20071da177e4SLinus Torvalds struct inode_security_struct *isec; 20081da177e4SLinus Torvalds 2009cda37124SEric Biggers static const struct tree_descr selinux_files[] = { 20101da177e4SLinus Torvalds [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR}, 20111da177e4SLinus Torvalds [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR}, 2012ce9982d0SStephen Smalley [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO}, 20131da177e4SLinus Torvalds [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO}, 20141da177e4SLinus Torvalds [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO}, 20151da177e4SLinus Torvalds [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO}, 20161da177e4SLinus Torvalds [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO}, 20171da177e4SLinus Torvalds [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO}, 20181da177e4SLinus Torvalds [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR}, 20191da177e4SLinus Torvalds [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO}, 20201da177e4SLinus Torvalds [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR}, 20211da177e4SLinus Torvalds [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO}, 20221da177e4SLinus Torvalds [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR}, 20233f12070eSEric Paris [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO}, 20243f12070eSEric Paris [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO}, 202511904167SKaiGai Kohei [SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO}, 202672e8c859SEric Paris [SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO}, 2027f9df6458SAndrew Perepechko [SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops, 2028f9df6458SAndrew Perepechko S_IWUGO}, 20291da177e4SLinus Torvalds /* last one */ {""} 20301da177e4SLinus Torvalds }; 20310619f0f5SStephen Smalley 20320619f0f5SStephen Smalley ret = selinux_fs_info_create(sb); 20330619f0f5SStephen Smalley if (ret) 20340619f0f5SStephen Smalley goto err; 20350619f0f5SStephen Smalley 20361da177e4SLinus Torvalds ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files); 20371da177e4SLinus Torvalds if (ret) 2038161ce45aSJames Morris goto err; 20391da177e4SLinus Torvalds 20400619f0f5SStephen Smalley fsi = sb->s_fs_info; 20410619f0f5SStephen Smalley fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino); 20420619f0f5SStephen Smalley if (IS_ERR(fsi->bool_dir)) { 20430619f0f5SStephen Smalley ret = PTR_ERR(fsi->bool_dir); 20440619f0f5SStephen Smalley fsi->bool_dir = NULL; 2045161ce45aSJames Morris goto err; 2046a1c2aa1eSAl Viro } 20471da177e4SLinus Torvalds 2048b77a493bSEric Paris ret = -ENOMEM; 20491da177e4SLinus Torvalds dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME); 2050b77a493bSEric Paris if (!dentry) 2051161ce45aSJames Morris goto err; 20521da177e4SLinus Torvalds 2053161ce45aSJames Morris ret = -ENOMEM; 2054b77a493bSEric Paris inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO); 20557e4237faSnixiaoming if (!inode) { 20567e4237faSnixiaoming dput(dentry); 2057161ce45aSJames Morris goto err; 20587e4237faSnixiaoming } 2059b77a493bSEric Paris 20600619f0f5SStephen Smalley inode->i_ino = ++fsi->last_ino; 206180788c22SCasey Schaufler isec = selinux_inode(inode); 20621da177e4SLinus Torvalds isec->sid = SECINITSID_DEVNULL; 20631da177e4SLinus Torvalds isec->sclass = SECCLASS_CHR_FILE; 206442059112SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 20651da177e4SLinus Torvalds 20661da177e4SLinus Torvalds init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3)); 20671da177e4SLinus Torvalds d_add(dentry, inode); 20681da177e4SLinus Torvalds 20690619f0f5SStephen Smalley dentry = sel_make_dir(sb->s_root, "avc", &fsi->last_ino); 2070a1c2aa1eSAl Viro if (IS_ERR(dentry)) { 2071a1c2aa1eSAl Viro ret = PTR_ERR(dentry); 2072161ce45aSJames Morris goto err; 2073a1c2aa1eSAl Viro } 20741da177e4SLinus Torvalds 20751da177e4SLinus Torvalds ret = sel_make_avc_files(dentry); 2076bcb62828SChristian Göttsche if (ret) 2077bcb62828SChristian Göttsche goto err; 207866f8e2f0SJeff Vander Stoep 207966f8e2f0SJeff Vander Stoep dentry = sel_make_dir(sb->s_root, "ss", &fsi->last_ino); 208066f8e2f0SJeff Vander Stoep if (IS_ERR(dentry)) { 208166f8e2f0SJeff Vander Stoep ret = PTR_ERR(dentry); 208266f8e2f0SJeff Vander Stoep goto err; 208366f8e2f0SJeff Vander Stoep } 208466f8e2f0SJeff Vander Stoep 208566f8e2f0SJeff Vander Stoep ret = sel_make_ss_files(dentry); 20861da177e4SLinus Torvalds if (ret) 2087161ce45aSJames Morris goto err; 2088f0ee2e46SJames Carter 20890619f0f5SStephen Smalley dentry = sel_make_dir(sb->s_root, "initial_contexts", &fsi->last_ino); 2090a1c2aa1eSAl Viro if (IS_ERR(dentry)) { 2091a1c2aa1eSAl Viro ret = PTR_ERR(dentry); 2092f0ee2e46SJames Carter goto err; 2093a1c2aa1eSAl Viro } 2094f0ee2e46SJames Carter 2095f0ee2e46SJames Carter ret = sel_make_initcon_files(dentry); 2096f0ee2e46SJames Carter if (ret) 2097f0ee2e46SJames Carter goto err; 2098f0ee2e46SJames Carter 2099613ba187SDaniel Burgener fsi->class_dir = sel_make_dir(sb->s_root, CLASS_DIR_NAME, &fsi->last_ino); 21000619f0f5SStephen Smalley if (IS_ERR(fsi->class_dir)) { 21010619f0f5SStephen Smalley ret = PTR_ERR(fsi->class_dir); 21020619f0f5SStephen Smalley fsi->class_dir = NULL; 2103e47c8fc5SChristopher J. PeBenito goto err; 2104a1c2aa1eSAl Viro } 2105e47c8fc5SChristopher J. PeBenito 2106613ba187SDaniel Burgener fsi->policycap_dir = sel_make_dir(sb->s_root, POLICYCAP_DIR_NAME, 21070619f0f5SStephen Smalley &fsi->last_ino); 21080619f0f5SStephen Smalley if (IS_ERR(fsi->policycap_dir)) { 21090619f0f5SStephen Smalley ret = PTR_ERR(fsi->policycap_dir); 21100619f0f5SStephen Smalley fsi->policycap_dir = NULL; 2111e47c8fc5SChristopher J. PeBenito goto err; 2112a1c2aa1eSAl Viro } 21130619f0f5SStephen Smalley 211402a52c5cSStephen Smalley ret = sel_make_policycap(fsi); 211502a52c5cSStephen Smalley if (ret) { 211602a52c5cSStephen Smalley pr_err("SELinux: failed to load policy capabilities\n"); 21170619f0f5SStephen Smalley goto err; 211802a52c5cSStephen Smalley } 211902a52c5cSStephen Smalley 2120b77a493bSEric Paris return 0; 2121161ce45aSJames Morris err: 2122f8b69a5fSpeter enderborg pr_err("SELinux: %s: failed while creating inodes\n", 2123744ba35eSEric Paris __func__); 21240619f0f5SStephen Smalley 21250619f0f5SStephen Smalley selinux_fs_info_free(sb); 21260619f0f5SStephen Smalley 2127b77a493bSEric Paris return ret; 21281da177e4SLinus Torvalds } 21291da177e4SLinus Torvalds 2130920f50b2SDavid Howells static int sel_get_tree(struct fs_context *fc) 21311da177e4SLinus Torvalds { 2132920f50b2SDavid Howells return get_tree_single(fc, sel_fill_super); 2133920f50b2SDavid Howells } 2134920f50b2SDavid Howells 2135920f50b2SDavid Howells static const struct fs_context_operations sel_context_ops = { 2136920f50b2SDavid Howells .get_tree = sel_get_tree, 2137920f50b2SDavid Howells }; 2138920f50b2SDavid Howells 2139920f50b2SDavid Howells static int sel_init_fs_context(struct fs_context *fc) 2140920f50b2SDavid Howells { 2141920f50b2SDavid Howells fc->ops = &sel_context_ops; 2142920f50b2SDavid Howells return 0; 21431da177e4SLinus Torvalds } 21441da177e4SLinus Torvalds 21450619f0f5SStephen Smalley static void sel_kill_sb(struct super_block *sb) 21460619f0f5SStephen Smalley { 21470619f0f5SStephen Smalley selinux_fs_info_free(sb); 21480619f0f5SStephen Smalley kill_litter_super(sb); 21490619f0f5SStephen Smalley } 21500619f0f5SStephen Smalley 21511da177e4SLinus Torvalds static struct file_system_type sel_fs_type = { 21521da177e4SLinus Torvalds .name = "selinuxfs", 2153920f50b2SDavid Howells .init_fs_context = sel_init_fs_context, 21540619f0f5SStephen Smalley .kill_sb = sel_kill_sb, 21551da177e4SLinus Torvalds }; 21561da177e4SLinus Torvalds 2157cd2bb4cbSOndrej Mosnacek static struct vfsmount *selinuxfs_mount __ro_after_init; 2158cd2bb4cbSOndrej Mosnacek struct path selinux_null __ro_after_init; 21591da177e4SLinus Torvalds 21601da177e4SLinus Torvalds static int __init init_sel_fs(void) 21611da177e4SLinus Torvalds { 21620619f0f5SStephen Smalley struct qstr null_name = QSTR_INIT(NULL_FILE_NAME, 21630619f0f5SStephen Smalley sizeof(NULL_FILE_NAME)-1); 21641da177e4SLinus Torvalds int err; 21651da177e4SLinus Torvalds 21666c5a682eSStephen Smalley if (!selinux_enabled_boot) 21671da177e4SLinus Torvalds return 0; 21687a627e3bSGreg Kroah-Hartman 2169f9bb4882SEric W. Biederman err = sysfs_create_mount_point(fs_kobj, "selinux"); 2170f9bb4882SEric W. Biederman if (err) 2171f9bb4882SEric W. Biederman return err; 21727a627e3bSGreg Kroah-Hartman 21731da177e4SLinus Torvalds err = register_filesystem(&sel_fs_type); 21747a627e3bSGreg Kroah-Hartman if (err) { 2175f9bb4882SEric W. Biederman sysfs_remove_mount_point(fs_kobj, "selinux"); 2176b77a493bSEric Paris return err; 21777a627e3bSGreg Kroah-Hartman } 2178b77a493bSEric Paris 2179765927b2SAl Viro selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type); 21801da177e4SLinus Torvalds if (IS_ERR(selinuxfs_mount)) { 2181f8b69a5fSpeter enderborg pr_err("selinuxfs: could not mount!\n"); 21821da177e4SLinus Torvalds err = PTR_ERR(selinuxfs_mount); 21831da177e4SLinus Torvalds selinuxfs_mount = NULL; 21841da177e4SLinus Torvalds } 21850619f0f5SStephen Smalley selinux_null.dentry = d_hash_and_lookup(selinux_null.mnt->mnt_root, 21860619f0f5SStephen Smalley &null_name); 21870619f0f5SStephen Smalley if (IS_ERR(selinux_null.dentry)) { 21880619f0f5SStephen Smalley pr_err("selinuxfs: could not lookup null!\n"); 21890619f0f5SStephen Smalley err = PTR_ERR(selinux_null.dentry); 21900619f0f5SStephen Smalley selinux_null.dentry = NULL; 21910619f0f5SStephen Smalley } 2192b77a493bSEric Paris 21931da177e4SLinus Torvalds return err; 21941da177e4SLinus Torvalds } 21951da177e4SLinus Torvalds 21961da177e4SLinus Torvalds __initcall(init_sel_fs); 21971da177e4SLinus Torvalds 21981da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 21991da177e4SLinus Torvalds void exit_sel_fs(void) 22001da177e4SLinus Torvalds { 2201f9bb4882SEric W. Biederman sysfs_remove_mount_point(fs_kobj, "selinux"); 2202fd40ffc7SStephen Smalley dput(selinux_null.dentry); 2203423e0ab0STim Chen kern_unmount(selinuxfs_mount); 22041da177e4SLinus Torvalds unregister_filesystem(&sel_fs_type); 22051da177e4SLinus Torvalds } 22061da177e4SLinus Torvalds #endif 2207