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; 141*c867248cSChristian Göttsche int scan_value; 142*c867248cSChristian Göttsche bool old_value, new_value; 1431da177e4SLinus Torvalds 144bfd51626SDavi Arnaut if (count >= PAGE_SIZE) 1458365a719SAl Viro return -ENOMEM; 146b77a493bSEric Paris 1471da177e4SLinus Torvalds /* No partial writes. */ 148b77a493bSEric Paris if (*ppos != 0) 1498365a719SAl Viro return -EINVAL; 150b77a493bSEric Paris 1518365a719SAl Viro page = memdup_user_nul(buf, count); 1528365a719SAl Viro if (IS_ERR(page)) 1538365a719SAl Viro return PTR_ERR(page); 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds length = -EINVAL; 156*c867248cSChristian Göttsche if (sscanf(page, "%d", &scan_value) != 1) 1571da177e4SLinus Torvalds goto out; 1581da177e4SLinus Torvalds 159*c867248cSChristian Göttsche new_value = !!scan_value; 160ea49d10eSStephen Smalley 161e67b7985SStephen Smalley old_value = enforcing_enabled(); 162aa8e712cSStephen Smalley if (new_value != old_value) { 163e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 164be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETENFORCE, 165be0554c9SStephen Smalley NULL); 1661da177e4SLinus Torvalds if (length) 1671da177e4SLinus Torvalds goto out; 168cdfb6b34SRichard Guy Briggs audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS, 1694195ed42SRichard Guy Briggs "enforcing=%d old_enforcing=%d auid=%u ses=%u" 1706c5a682eSStephen Smalley " enabled=1 old-enabled=1 lsm=selinux res=1", 171aa8e712cSStephen Smalley new_value, old_value, 172581abc09SEric W. Biederman from_kuid(&init_user_ns, audit_get_loginuid(current)), 1736c5a682eSStephen Smalley audit_get_sessionid(current)); 174e67b7985SStephen Smalley enforcing_set(new_value); 175aa8e712cSStephen Smalley if (new_value) 176e67b7985SStephen Smalley avc_ss_reset(0); 177aa8e712cSStephen Smalley selnl_notify_setenforce(new_value); 178e67b7985SStephen Smalley selinux_status_update_setenforce(new_value); 179aa8e712cSStephen Smalley if (!new_value) 18042df744cSJanne Karhunen call_blocking_lsm_notifier(LSM_POLICY_CHANGE, NULL); 1812554a48fSLakshmi Ramasubramanian 182e67b7985SStephen Smalley selinux_ima_measure_state(); 1831da177e4SLinus Torvalds } 1841da177e4SLinus Torvalds length = count; 1851da177e4SLinus Torvalds out: 1868365a719SAl Viro kfree(page); 1871da177e4SLinus Torvalds return length; 1881da177e4SLinus Torvalds } 1891da177e4SLinus Torvalds #else 1901da177e4SLinus Torvalds #define sel_write_enforce NULL 1911da177e4SLinus Torvalds #endif 1921da177e4SLinus Torvalds 1939c2e08c5SArjan van de Ven static const struct file_operations sel_enforce_ops = { 1941da177e4SLinus Torvalds .read = sel_read_enforce, 1951da177e4SLinus Torvalds .write = sel_write_enforce, 19657a62c23SArnd Bergmann .llseek = generic_file_llseek, 1971da177e4SLinus Torvalds }; 1981da177e4SLinus Torvalds 1993f12070eSEric Paris static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf, 2003f12070eSEric Paris size_t count, loff_t *ppos) 2013f12070eSEric Paris { 2023f12070eSEric Paris char tmpbuf[TMPBUFLEN]; 2033f12070eSEric Paris ssize_t length; 204496ad9aaSAl Viro ino_t ino = file_inode(filp)->i_ino; 2053f12070eSEric Paris int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ? 206e67b7985SStephen Smalley security_get_reject_unknown() : 207e67b7985SStephen Smalley !security_get_allow_unknown(); 2083f12070eSEric Paris 2093f12070eSEric Paris length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown); 2103f12070eSEric Paris return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 2113f12070eSEric Paris } 2123f12070eSEric Paris 2133f12070eSEric Paris static const struct file_operations sel_handle_unknown_ops = { 2143f12070eSEric Paris .read = sel_read_handle_unknown, 21557a62c23SArnd Bergmann .llseek = generic_file_llseek, 2163f12070eSEric Paris }; 2173f12070eSEric Paris 21811904167SKaiGai Kohei static int sel_open_handle_status(struct inode *inode, struct file *filp) 21911904167SKaiGai Kohei { 220e67b7985SStephen Smalley struct page *status = selinux_kernel_status_page(); 22111904167SKaiGai Kohei 22211904167SKaiGai Kohei if (!status) 22311904167SKaiGai Kohei return -ENOMEM; 22411904167SKaiGai Kohei 22511904167SKaiGai Kohei filp->private_data = status; 22611904167SKaiGai Kohei 22711904167SKaiGai Kohei return 0; 22811904167SKaiGai Kohei } 22911904167SKaiGai Kohei 23011904167SKaiGai Kohei static ssize_t sel_read_handle_status(struct file *filp, char __user *buf, 23111904167SKaiGai Kohei size_t count, loff_t *ppos) 23211904167SKaiGai Kohei { 23311904167SKaiGai Kohei struct page *status = filp->private_data; 23411904167SKaiGai Kohei 23511904167SKaiGai Kohei BUG_ON(!status); 23611904167SKaiGai Kohei 23711904167SKaiGai Kohei return simple_read_from_buffer(buf, count, ppos, 23811904167SKaiGai Kohei page_address(status), 23911904167SKaiGai Kohei sizeof(struct selinux_kernel_status)); 24011904167SKaiGai Kohei } 24111904167SKaiGai Kohei 24211904167SKaiGai Kohei static int sel_mmap_handle_status(struct file *filp, 24311904167SKaiGai Kohei struct vm_area_struct *vma) 24411904167SKaiGai Kohei { 24511904167SKaiGai Kohei struct page *status = filp->private_data; 24611904167SKaiGai Kohei unsigned long size = vma->vm_end - vma->vm_start; 24711904167SKaiGai Kohei 24811904167SKaiGai Kohei BUG_ON(!status); 24911904167SKaiGai Kohei 25011904167SKaiGai Kohei /* only allows one page from the head */ 25111904167SKaiGai Kohei if (vma->vm_pgoff > 0 || size != PAGE_SIZE) 25211904167SKaiGai Kohei return -EIO; 25311904167SKaiGai Kohei /* disallow writable mapping */ 25411904167SKaiGai Kohei if (vma->vm_flags & VM_WRITE) 25511904167SKaiGai Kohei return -EPERM; 25611904167SKaiGai Kohei /* disallow mprotect() turns it into writable */ 2571c71222eSSuren Baghdasaryan vm_flags_clear(vma, VM_MAYWRITE); 25811904167SKaiGai Kohei 25911904167SKaiGai Kohei return remap_pfn_range(vma, vma->vm_start, 26011904167SKaiGai Kohei page_to_pfn(status), 26111904167SKaiGai Kohei size, vma->vm_page_prot); 26211904167SKaiGai Kohei } 26311904167SKaiGai Kohei 26411904167SKaiGai Kohei static const struct file_operations sel_handle_status_ops = { 26511904167SKaiGai Kohei .open = sel_open_handle_status, 26611904167SKaiGai Kohei .read = sel_read_handle_status, 26711904167SKaiGai Kohei .mmap = sel_mmap_handle_status, 26811904167SKaiGai Kohei .llseek = generic_file_llseek, 26911904167SKaiGai Kohei }; 27011904167SKaiGai Kohei 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; 27889b223bfSPaul Moore 279bfd51626SDavi Arnaut if (count >= PAGE_SIZE) 2808365a719SAl Viro return -ENOMEM; 281b77a493bSEric Paris 2821da177e4SLinus Torvalds /* No partial writes. */ 283b77a493bSEric Paris if (*ppos != 0) 2848365a719SAl Viro return -EINVAL; 285b77a493bSEric Paris 2868365a719SAl Viro page = memdup_user_nul(buf, count); 2878365a719SAl Viro if (IS_ERR(page)) 2888365a719SAl Viro return PTR_ERR(page); 2891da177e4SLinus Torvalds 290f22f9aafSPaul Moore if (sscanf(page, "%d", &new_value) != 1) { 2911da177e4SLinus Torvalds length = -EINVAL; 2921da177e4SLinus Torvalds goto out; 293f22f9aafSPaul Moore } 294f22f9aafSPaul Moore length = count; 2951da177e4SLinus Torvalds 2961da177e4SLinus Torvalds if (new_value) { 297f22f9aafSPaul Moore pr_err("SELinux: https://github.com/SELinuxProject/selinux-kernel/wiki/DEPRECATE-runtime-disable\n"); 298f22f9aafSPaul Moore pr_err("SELinux: Runtime disable is not supported, use selinux=0 on the kernel cmdline.\n"); 2991da177e4SLinus Torvalds } 3001da177e4SLinus Torvalds 3011da177e4SLinus Torvalds out: 3028365a719SAl Viro kfree(page); 3031da177e4SLinus Torvalds return length; 3041da177e4SLinus Torvalds } 3051da177e4SLinus Torvalds 3069c2e08c5SArjan van de Ven static const struct file_operations sel_disable_ops = { 3071da177e4SLinus Torvalds .write = sel_write_disable, 30857a62c23SArnd Bergmann .llseek = generic_file_llseek, 3091da177e4SLinus Torvalds }; 3101da177e4SLinus Torvalds 3111da177e4SLinus Torvalds static ssize_t sel_read_policyvers(struct file *filp, char __user *buf, 3121da177e4SLinus Torvalds size_t count, loff_t *ppos) 3131da177e4SLinus Torvalds { 3141da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 3151da177e4SLinus Torvalds ssize_t length; 3161da177e4SLinus Torvalds 3171da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX); 3181da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 3191da177e4SLinus Torvalds } 3201da177e4SLinus Torvalds 3219c2e08c5SArjan van de Ven static const struct file_operations sel_policyvers_ops = { 3221da177e4SLinus Torvalds .read = sel_read_policyvers, 32357a62c23SArnd Bergmann .llseek = generic_file_llseek, 3241da177e4SLinus Torvalds }; 3251da177e4SLinus Torvalds 3261da177e4SLinus Torvalds /* declaration for sel_write_load */ 32766ec384aSDaniel Burgener static int sel_make_bools(struct selinux_policy *newpolicy, struct dentry *bool_dir, 32866ec384aSDaniel Burgener unsigned int *bool_num, char ***bool_pending_names, 329c3fae2b2SChristian Göttsche int **bool_pending_values); 33066ec384aSDaniel Burgener static int sel_make_classes(struct selinux_policy *newpolicy, 33166ec384aSDaniel Burgener struct dentry *class_dir, 33266ec384aSDaniel Burgener unsigned long *last_class_ino); 333e47c8fc5SChristopher J. PeBenito 334e47c8fc5SChristopher J. PeBenito /* declaration for sel_make_class_dirs */ 335a1c2aa1eSAl Viro static struct dentry *sel_make_dir(struct dentry *dir, const char *name, 336e47c8fc5SChristopher J. PeBenito unsigned long *ino); 3371da177e4SLinus Torvalds 3380eea6091SDaniel Burgener /* declaration for sel_make_policy_nodes */ 3390eea6091SDaniel Burgener static struct dentry *sel_make_disconnected_dir(struct super_block *sb, 3400eea6091SDaniel Burgener unsigned long *ino); 3410eea6091SDaniel Burgener 3420eea6091SDaniel Burgener /* declaration for sel_make_policy_nodes */ 343aeecf4a3SDaniel Burgener static void sel_remove_entries(struct dentry *de); 344aeecf4a3SDaniel Burgener 3451da177e4SLinus Torvalds static ssize_t sel_read_mls(struct file *filp, char __user *buf, 3461da177e4SLinus Torvalds size_t count, loff_t *ppos) 3471da177e4SLinus Torvalds { 3481da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 3491da177e4SLinus Torvalds ssize_t length; 3501da177e4SLinus Torvalds 3510719aaf5SGuido Trentalancia length = scnprintf(tmpbuf, TMPBUFLEN, "%d", 352e67b7985SStephen Smalley security_mls_enabled()); 3531da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 3541da177e4SLinus Torvalds } 3551da177e4SLinus Torvalds 3569c2e08c5SArjan van de Ven static const struct file_operations sel_mls_ops = { 3571da177e4SLinus Torvalds .read = sel_read_mls, 35857a62c23SArnd Bergmann .llseek = generic_file_llseek, 3591da177e4SLinus Torvalds }; 3601da177e4SLinus Torvalds 361cee74f47SEric Paris struct policy_load_memory { 362cee74f47SEric Paris size_t len; 363cee74f47SEric Paris void *data; 364cee74f47SEric Paris }; 365cee74f47SEric Paris 366cee74f47SEric Paris static int sel_open_policy(struct inode *inode, struct file *filp) 367cee74f47SEric Paris { 3680619f0f5SStephen Smalley struct selinux_fs_info *fsi = inode->i_sb->s_fs_info; 369cee74f47SEric Paris struct policy_load_memory *plm = NULL; 370cee74f47SEric Paris int rc; 371cee74f47SEric Paris 372cee74f47SEric Paris BUG_ON(filp->private_data); 373cee74f47SEric Paris 374e67b7985SStephen Smalley mutex_lock(&selinux_state.policy_mutex); 375cee74f47SEric Paris 376e67b7985SStephen Smalley rc = avc_has_perm(current_sid(), SECINITSID_SECURITY, 377be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL); 378cee74f47SEric Paris if (rc) 379cee74f47SEric Paris goto err; 380cee74f47SEric Paris 381cee74f47SEric Paris rc = -EBUSY; 3820619f0f5SStephen Smalley if (fsi->policy_opened) 383cee74f47SEric Paris goto err; 384cee74f47SEric Paris 385cee74f47SEric Paris rc = -ENOMEM; 386cee74f47SEric Paris plm = kzalloc(sizeof(*plm), GFP_KERNEL); 387cee74f47SEric Paris if (!plm) 388cee74f47SEric Paris goto err; 389cee74f47SEric Paris 390e67b7985SStephen Smalley rc = security_read_policy(&plm->data, &plm->len); 391cee74f47SEric Paris if (rc) 392cee74f47SEric Paris goto err; 393cee74f47SEric Paris 39466ccd256SOndrej Mosnacek if ((size_t)i_size_read(inode) != plm->len) { 39566ccd256SOndrej Mosnacek inode_lock(inode); 39666ccd256SOndrej Mosnacek i_size_write(inode, plm->len); 39766ccd256SOndrej Mosnacek inode_unlock(inode); 39866ccd256SOndrej Mosnacek } 39966ccd256SOndrej Mosnacek 4000619f0f5SStephen Smalley fsi->policy_opened = 1; 401cee74f47SEric Paris 402cee74f47SEric Paris filp->private_data = plm; 403cee74f47SEric Paris 404e67b7985SStephen Smalley mutex_unlock(&selinux_state.policy_mutex); 405cee74f47SEric Paris 406cee74f47SEric Paris return 0; 407cee74f47SEric Paris err: 408e67b7985SStephen Smalley mutex_unlock(&selinux_state.policy_mutex); 409cee74f47SEric Paris 410cee74f47SEric Paris if (plm) 411cee74f47SEric Paris vfree(plm->data); 412cee74f47SEric Paris kfree(plm); 413cee74f47SEric Paris return rc; 414cee74f47SEric Paris } 415cee74f47SEric Paris 416cee74f47SEric Paris static int sel_release_policy(struct inode *inode, struct file *filp) 417cee74f47SEric Paris { 4180619f0f5SStephen Smalley struct selinux_fs_info *fsi = inode->i_sb->s_fs_info; 419cee74f47SEric Paris struct policy_load_memory *plm = filp->private_data; 420cee74f47SEric Paris 421cee74f47SEric Paris BUG_ON(!plm); 422cee74f47SEric Paris 4230619f0f5SStephen Smalley fsi->policy_opened = 0; 424cee74f47SEric Paris 425cee74f47SEric Paris vfree(plm->data); 426cee74f47SEric Paris kfree(plm); 427cee74f47SEric Paris 428cee74f47SEric Paris return 0; 429cee74f47SEric Paris } 430cee74f47SEric Paris 431cee74f47SEric Paris static ssize_t sel_read_policy(struct file *filp, char __user *buf, 432cee74f47SEric Paris size_t count, loff_t *ppos) 433cee74f47SEric Paris { 434cee74f47SEric Paris struct policy_load_memory *plm = filp->private_data; 435cee74f47SEric Paris int ret; 436cee74f47SEric Paris 437e67b7985SStephen Smalley ret = avc_has_perm(current_sid(), SECINITSID_SECURITY, 438be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL); 439cee74f47SEric Paris if (ret) 440cee74f47SEric Paris return ret; 4410da74120SJann Horn 4420da74120SJann Horn return simple_read_from_buffer(buf, count, ppos, plm->data, plm->len); 443cee74f47SEric Paris } 444cee74f47SEric Paris 445ac9a1f6dSSouptick Joarder static vm_fault_t sel_mmap_policy_fault(struct vm_fault *vmf) 446845ca30fSEric Paris { 44711bac800SDave Jiang struct policy_load_memory *plm = vmf->vma->vm_file->private_data; 448845ca30fSEric Paris unsigned long offset; 449845ca30fSEric Paris struct page *page; 450845ca30fSEric Paris 451845ca30fSEric Paris if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE)) 452845ca30fSEric Paris return VM_FAULT_SIGBUS; 453845ca30fSEric Paris 454845ca30fSEric Paris offset = vmf->pgoff << PAGE_SHIFT; 455845ca30fSEric Paris if (offset >= roundup(plm->len, PAGE_SIZE)) 456845ca30fSEric Paris return VM_FAULT_SIGBUS; 457845ca30fSEric Paris 458845ca30fSEric Paris page = vmalloc_to_page(plm->data + offset); 459845ca30fSEric Paris get_page(page); 460845ca30fSEric Paris 461845ca30fSEric Paris vmf->page = page; 462845ca30fSEric Paris 463845ca30fSEric Paris return 0; 464845ca30fSEric Paris } 465845ca30fSEric Paris 4667cbea8dcSKirill A. Shutemov static const struct vm_operations_struct sel_mmap_policy_ops = { 467845ca30fSEric Paris .fault = sel_mmap_policy_fault, 468845ca30fSEric Paris .page_mkwrite = sel_mmap_policy_fault, 469845ca30fSEric Paris }; 470845ca30fSEric Paris 471ad3fa08cSJames Morris static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma) 472845ca30fSEric Paris { 473845ca30fSEric Paris if (vma->vm_flags & VM_SHARED) { 474845ca30fSEric Paris /* do not allow mprotect to make mapping writable */ 4751c71222eSSuren Baghdasaryan vm_flags_clear(vma, VM_MAYWRITE); 476845ca30fSEric Paris 477845ca30fSEric Paris if (vma->vm_flags & VM_WRITE) 478845ca30fSEric Paris return -EACCES; 479845ca30fSEric Paris } 480845ca30fSEric Paris 4811c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_DONTEXPAND | VM_DONTDUMP); 482845ca30fSEric Paris vma->vm_ops = &sel_mmap_policy_ops; 483845ca30fSEric Paris 484845ca30fSEric Paris return 0; 485845ca30fSEric Paris } 486845ca30fSEric Paris 487cee74f47SEric Paris static const struct file_operations sel_policy_ops = { 488cee74f47SEric Paris .open = sel_open_policy, 489cee74f47SEric Paris .read = sel_read_policy, 490845ca30fSEric Paris .mmap = sel_mmap_policy, 491cee74f47SEric Paris .release = sel_release_policy, 49247a93a5bSEric Paris .llseek = generic_file_llseek, 493cee74f47SEric Paris }; 494cee74f47SEric Paris 4950eea6091SDaniel Burgener static void sel_remove_old_bool_data(unsigned int bool_num, char **bool_names, 496c3fae2b2SChristian Göttsche int *bool_values) 497aeecf4a3SDaniel Burgener { 498aeecf4a3SDaniel Burgener u32 i; 499aeecf4a3SDaniel Burgener 500aeecf4a3SDaniel Burgener /* bool_dir cleanup */ 5010eea6091SDaniel Burgener for (i = 0; i < bool_num; i++) 5020eea6091SDaniel Burgener kfree(bool_names[i]); 5030eea6091SDaniel Burgener kfree(bool_names); 5040eea6091SDaniel Burgener kfree(bool_values); 505aeecf4a3SDaniel Burgener } 506aeecf4a3SDaniel Burgener 50702a52c5cSStephen Smalley static int sel_make_policy_nodes(struct selinux_fs_info *fsi, 50802a52c5cSStephen Smalley struct selinux_policy *newpolicy) 5090619f0f5SStephen Smalley { 5100eea6091SDaniel Burgener int ret = 0; 5110eea6091SDaniel Burgener struct dentry *tmp_parent, *tmp_bool_dir, *tmp_class_dir, *old_dentry; 5120eea6091SDaniel Burgener unsigned int tmp_bool_num, old_bool_num; 5130eea6091SDaniel Burgener char **tmp_bool_names, **old_bool_names; 514c3fae2b2SChristian Göttsche int *tmp_bool_values, *old_bool_values; 5150eea6091SDaniel Burgener unsigned long tmp_ino = fsi->last_ino; /* Don't increment last_ino in this function */ 5160619f0f5SStephen Smalley 5170eea6091SDaniel Burgener tmp_parent = sel_make_disconnected_dir(fsi->sb, &tmp_ino); 5180eea6091SDaniel Burgener if (IS_ERR(tmp_parent)) 5190eea6091SDaniel Burgener return PTR_ERR(tmp_parent); 520aeecf4a3SDaniel Burgener 5210eea6091SDaniel Burgener tmp_ino = fsi->bool_dir->d_inode->i_ino - 1; /* sel_make_dir will increment and set */ 5220eea6091SDaniel Burgener tmp_bool_dir = sel_make_dir(tmp_parent, BOOL_DIR_NAME, &tmp_ino); 5230eea6091SDaniel Burgener if (IS_ERR(tmp_bool_dir)) { 5240eea6091SDaniel Burgener ret = PTR_ERR(tmp_bool_dir); 5250eea6091SDaniel Burgener goto out; 5260619f0f5SStephen Smalley } 5270619f0f5SStephen Smalley 5280eea6091SDaniel Burgener tmp_ino = fsi->class_dir->d_inode->i_ino - 1; /* sel_make_dir will increment and set */ 5290eea6091SDaniel Burgener tmp_class_dir = sel_make_dir(tmp_parent, CLASS_DIR_NAME, &tmp_ino); 5300eea6091SDaniel Burgener if (IS_ERR(tmp_class_dir)) { 5310eea6091SDaniel Burgener ret = PTR_ERR(tmp_class_dir); 5320eea6091SDaniel Burgener goto out; 5330eea6091SDaniel Burgener } 5340eea6091SDaniel Burgener 5350eea6091SDaniel Burgener ret = sel_make_bools(newpolicy, tmp_bool_dir, &tmp_bool_num, 5360eea6091SDaniel Burgener &tmp_bool_names, &tmp_bool_values); 537ee5de60aSOndrej Mosnacek if (ret) 5380eea6091SDaniel Burgener goto out; 5390eea6091SDaniel Burgener 5400eea6091SDaniel Burgener ret = sel_make_classes(newpolicy, tmp_class_dir, 54166ec384aSDaniel Burgener &fsi->last_class_ino); 542ee5de60aSOndrej Mosnacek if (ret) 5430eea6091SDaniel Burgener goto out; 5440619f0f5SStephen Smalley 5450eea6091SDaniel Burgener /* booleans */ 5460eea6091SDaniel Burgener old_dentry = fsi->bool_dir; 5470eea6091SDaniel Burgener lock_rename(tmp_bool_dir, old_dentry); 5480eea6091SDaniel Burgener d_exchange(tmp_bool_dir, fsi->bool_dir); 5490eea6091SDaniel Burgener 5500eea6091SDaniel Burgener old_bool_num = fsi->bool_num; 5510eea6091SDaniel Burgener old_bool_names = fsi->bool_pending_names; 5520eea6091SDaniel Burgener old_bool_values = fsi->bool_pending_values; 5530eea6091SDaniel Burgener 5540eea6091SDaniel Burgener fsi->bool_num = tmp_bool_num; 5550eea6091SDaniel Burgener fsi->bool_pending_names = tmp_bool_names; 5560eea6091SDaniel Burgener fsi->bool_pending_values = tmp_bool_values; 5570eea6091SDaniel Burgener 5580eea6091SDaniel Burgener sel_remove_old_bool_data(old_bool_num, old_bool_names, old_bool_values); 5590eea6091SDaniel Burgener 5600eea6091SDaniel Burgener fsi->bool_dir = tmp_bool_dir; 5610eea6091SDaniel Burgener unlock_rename(tmp_bool_dir, old_dentry); 5620eea6091SDaniel Burgener 5630eea6091SDaniel Burgener /* classes */ 5640eea6091SDaniel Burgener old_dentry = fsi->class_dir; 5650eea6091SDaniel Burgener lock_rename(tmp_class_dir, old_dentry); 5660eea6091SDaniel Burgener d_exchange(tmp_class_dir, fsi->class_dir); 5670eea6091SDaniel Burgener fsi->class_dir = tmp_class_dir; 5680eea6091SDaniel Burgener unlock_rename(tmp_class_dir, old_dentry); 5690eea6091SDaniel Burgener 5700eea6091SDaniel Burgener out: 5710eea6091SDaniel Burgener /* Since the other temporary dirs are children of tmp_parent 5720eea6091SDaniel Burgener * this will handle all the cleanup in the case of a failure before 5730eea6091SDaniel Burgener * the swapover 5740eea6091SDaniel Burgener */ 5750eea6091SDaniel Burgener sel_remove_entries(tmp_parent); 5760eea6091SDaniel Burgener dput(tmp_parent); /* d_genocide() only handles the children */ 5770eea6091SDaniel Burgener 5780eea6091SDaniel Burgener return ret; 5790619f0f5SStephen Smalley } 5800619f0f5SStephen Smalley 5811da177e4SLinus Torvalds static ssize_t sel_write_load(struct file *file, const char __user *buf, 5821da177e4SLinus Torvalds size_t count, loff_t *ppos) 5831da177e4SLinus Torvalds 5841da177e4SLinus Torvalds { 5850619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 5866406887aSOndrej Mosnacek struct selinux_load_state load_state; 5871da177e4SLinus Torvalds ssize_t length; 5881da177e4SLinus Torvalds void *data = NULL; 5891da177e4SLinus Torvalds 590e67b7985SStephen Smalley mutex_lock(&selinux_state.policy_mutex); 5911da177e4SLinus Torvalds 592e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 593be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL); 5941da177e4SLinus Torvalds if (length) 5951da177e4SLinus Torvalds goto out; 5961da177e4SLinus Torvalds 5971da177e4SLinus Torvalds /* No partial writes. */ 5981da177e4SLinus Torvalds length = -EINVAL; 599b77a493bSEric Paris if (*ppos != 0) 6001da177e4SLinus Torvalds goto out; 6011da177e4SLinus Torvalds 602b77a493bSEric Paris length = -ENOMEM; 603b77a493bSEric Paris data = vmalloc(count); 604b77a493bSEric Paris if (!data) 605b77a493bSEric Paris goto out; 6061da177e4SLinus Torvalds 6071da177e4SLinus Torvalds length = -EFAULT; 6081da177e4SLinus Torvalds if (copy_from_user(data, buf, count) != 0) 6091da177e4SLinus Torvalds goto out; 6101da177e4SLinus Torvalds 611e67b7985SStephen Smalley length = security_load_policy(data, count, &load_state); 6124262fb51SGary Tierney if (length) { 6134262fb51SGary Tierney pr_warn_ratelimited("SELinux: failed to load policy\n"); 6141da177e4SLinus Torvalds goto out; 6154262fb51SGary Tierney } 6161da177e4SLinus Torvalds 6176406887aSOndrej Mosnacek length = sel_make_policy_nodes(fsi, load_state.policy); 61802a52c5cSStephen Smalley if (length) { 619ee5de60aSOndrej Mosnacek pr_warn_ratelimited("SELinux: failed to initialize selinuxfs\n"); 620e67b7985SStephen Smalley selinux_policy_cancel(&load_state); 621519dad3bSOndrej Mosnacek goto out; 62202a52c5cSStephen Smalley } 62302a52c5cSStephen Smalley 624e67b7985SStephen Smalley selinux_policy_commit(&load_state); 625b77a493bSEric Paris 6261da177e4SLinus Torvalds length = count; 627e47c8fc5SChristopher J. PeBenito 628cdfb6b34SRichard Guy Briggs audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_POLICY_LOAD, 629d141136fSRichard Guy Briggs "auid=%u ses=%u lsm=selinux res=1", 630581abc09SEric W. Biederman from_kuid(&init_user_ns, audit_get_loginuid(current)), 6314746ec5bSEric Paris audit_get_sessionid(current)); 6321da177e4SLinus Torvalds out: 633e67b7985SStephen Smalley mutex_unlock(&selinux_state.policy_mutex); 6341da177e4SLinus Torvalds vfree(data); 6351da177e4SLinus Torvalds return length; 6361da177e4SLinus Torvalds } 6371da177e4SLinus Torvalds 6389c2e08c5SArjan van de Ven static const struct file_operations sel_load_ops = { 6391da177e4SLinus Torvalds .write = sel_write_load, 64057a62c23SArnd Bergmann .llseek = generic_file_llseek, 6411da177e4SLinus Torvalds }; 6421da177e4SLinus Torvalds 643ce9982d0SStephen Smalley static ssize_t sel_write_context(struct file *file, char *buf, size_t size) 6441da177e4SLinus Torvalds { 645b77a493bSEric Paris char *canon = NULL; 646ce9982d0SStephen Smalley u32 sid, len; 6471da177e4SLinus Torvalds ssize_t length; 6481da177e4SLinus Torvalds 649e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 650be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__CHECK_CONTEXT, NULL); 6511da177e4SLinus Torvalds if (length) 652b77a493bSEric Paris goto out; 6531da177e4SLinus Torvalds 654e67b7985SStephen Smalley length = security_context_to_sid(buf, size, &sid, GFP_KERNEL); 655b77a493bSEric Paris if (length) 656b77a493bSEric Paris goto out; 6571da177e4SLinus Torvalds 658e67b7985SStephen Smalley length = security_sid_to_context(sid, &canon, &len); 659b77a493bSEric Paris if (length) 660b77a493bSEric Paris goto out; 661ce9982d0SStephen Smalley 662b77a493bSEric Paris length = -ERANGE; 663ce9982d0SStephen Smalley if (len > SIMPLE_TRANSACTION_LIMIT) { 664f8b69a5fSpeter enderborg pr_err("SELinux: %s: context size (%u) exceeds " 665744ba35eSEric Paris "payload max\n", __func__, len); 666ce9982d0SStephen Smalley goto out; 667ce9982d0SStephen Smalley } 668ce9982d0SStephen Smalley 669ce9982d0SStephen Smalley memcpy(buf, canon, len); 670ce9982d0SStephen Smalley length = len; 6711da177e4SLinus Torvalds out: 672ce9982d0SStephen Smalley kfree(canon); 6731da177e4SLinus Torvalds return length; 6741da177e4SLinus Torvalds } 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf, 6771da177e4SLinus Torvalds size_t count, loff_t *ppos) 6781da177e4SLinus Torvalds { 6791da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 6801da177e4SLinus Torvalds ssize_t length; 6811da177e4SLinus Torvalds 6828861d0afSLakshmi Ramasubramanian length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 683e67b7985SStephen Smalley checkreqprot_get()); 6841da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 6851da177e4SLinus Torvalds } 6861da177e4SLinus Torvalds 6871da177e4SLinus Torvalds static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf, 6881da177e4SLinus Torvalds size_t count, loff_t *ppos) 6891da177e4SLinus Torvalds { 6908365a719SAl Viro char *page; 6911da177e4SLinus Torvalds ssize_t length; 6921da177e4SLinus Torvalds unsigned int new_value; 6931da177e4SLinus Torvalds 694e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 695be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETCHECKREQPROT, 696be0554c9SStephen Smalley NULL); 6971da177e4SLinus Torvalds if (length) 6988365a719SAl Viro return length; 6991da177e4SLinus Torvalds 700bfd51626SDavi Arnaut if (count >= PAGE_SIZE) 7018365a719SAl Viro return -ENOMEM; 702b77a493bSEric Paris 7031da177e4SLinus Torvalds /* No partial writes. */ 704b77a493bSEric Paris if (*ppos != 0) 7058365a719SAl Viro return -EINVAL; 706b77a493bSEric Paris 7078365a719SAl Viro page = memdup_user_nul(buf, count); 7088365a719SAl Viro if (IS_ERR(page)) 7098365a719SAl Viro return PTR_ERR(page); 7101da177e4SLinus Torvalds 711a7e4676eSPaul Moore if (sscanf(page, "%u", &new_value) != 1) { 7121da177e4SLinus Torvalds length = -EINVAL; 7131da177e4SLinus Torvalds goto out; 714a7e4676eSPaul Moore } 715a7e4676eSPaul Moore length = count; 7161da177e4SLinus Torvalds 717e9c38f9fSStephen Smalley if (new_value) { 718e9c38f9fSStephen Smalley char comm[sizeof(current->comm)]; 719e9c38f9fSStephen Smalley 720e9c38f9fSStephen Smalley memcpy(comm, current->comm, sizeof(comm)); 721a7e4676eSPaul Moore pr_err("SELinux: %s (%d) set checkreqprot to 1. This is no longer supported.\n", 722e9c38f9fSStephen Smalley comm, current->pid); 723e9c38f9fSStephen Smalley } 724e9c38f9fSStephen Smalley 725e67b7985SStephen Smalley selinux_ima_measure_state(); 7262554a48fSLakshmi Ramasubramanian 7271da177e4SLinus Torvalds out: 7288365a719SAl Viro kfree(page); 7291da177e4SLinus Torvalds return length; 7301da177e4SLinus Torvalds } 7319c2e08c5SArjan van de Ven static const struct file_operations sel_checkreqprot_ops = { 7321da177e4SLinus Torvalds .read = sel_read_checkreqprot, 7331da177e4SLinus Torvalds .write = sel_write_checkreqprot, 73457a62c23SArnd Bergmann .llseek = generic_file_llseek, 7351da177e4SLinus Torvalds }; 7361da177e4SLinus Torvalds 737f9df6458SAndrew Perepechko static ssize_t sel_write_validatetrans(struct file *file, 738f9df6458SAndrew Perepechko const char __user *buf, 739f9df6458SAndrew Perepechko size_t count, loff_t *ppos) 740f9df6458SAndrew Perepechko { 741f9df6458SAndrew Perepechko char *oldcon = NULL, *newcon = NULL, *taskcon = NULL; 742f9df6458SAndrew Perepechko char *req = NULL; 743f9df6458SAndrew Perepechko u32 osid, nsid, tsid; 744f9df6458SAndrew Perepechko u16 tclass; 745f9df6458SAndrew Perepechko int rc; 746f9df6458SAndrew Perepechko 747e67b7985SStephen Smalley rc = avc_has_perm(current_sid(), SECINITSID_SECURITY, 748be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__VALIDATE_TRANS, NULL); 749f9df6458SAndrew Perepechko if (rc) 750f9df6458SAndrew Perepechko goto out; 751f9df6458SAndrew Perepechko 752f9df6458SAndrew Perepechko rc = -ENOMEM; 753f9df6458SAndrew Perepechko if (count >= PAGE_SIZE) 754f9df6458SAndrew Perepechko goto out; 755f9df6458SAndrew Perepechko 756f9df6458SAndrew Perepechko /* No partial writes. */ 757f9df6458SAndrew Perepechko rc = -EINVAL; 758f9df6458SAndrew Perepechko if (*ppos != 0) 759f9df6458SAndrew Perepechko goto out; 760f9df6458SAndrew Perepechko 7610b884d25SAl Viro req = memdup_user_nul(buf, count); 7620b884d25SAl Viro if (IS_ERR(req)) { 7630b884d25SAl Viro rc = PTR_ERR(req); 7640b884d25SAl Viro req = NULL; 765f9df6458SAndrew Perepechko goto out; 7660b884d25SAl Viro } 767f9df6458SAndrew Perepechko 768f9df6458SAndrew Perepechko rc = -ENOMEM; 769f9df6458SAndrew Perepechko oldcon = kzalloc(count + 1, GFP_KERNEL); 770f9df6458SAndrew Perepechko if (!oldcon) 771f9df6458SAndrew Perepechko goto out; 772f9df6458SAndrew Perepechko 773f9df6458SAndrew Perepechko newcon = kzalloc(count + 1, GFP_KERNEL); 774f9df6458SAndrew Perepechko if (!newcon) 775f9df6458SAndrew Perepechko goto out; 776f9df6458SAndrew Perepechko 777f9df6458SAndrew Perepechko taskcon = kzalloc(count + 1, GFP_KERNEL); 778f9df6458SAndrew Perepechko if (!taskcon) 779f9df6458SAndrew Perepechko goto out; 780f9df6458SAndrew Perepechko 781f9df6458SAndrew Perepechko rc = -EINVAL; 782f9df6458SAndrew Perepechko if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4) 783f9df6458SAndrew Perepechko goto out; 784f9df6458SAndrew Perepechko 785e67b7985SStephen Smalley rc = security_context_str_to_sid(oldcon, &osid, GFP_KERNEL); 786f9df6458SAndrew Perepechko if (rc) 787f9df6458SAndrew Perepechko goto out; 788f9df6458SAndrew Perepechko 789e67b7985SStephen Smalley rc = security_context_str_to_sid(newcon, &nsid, GFP_KERNEL); 790f9df6458SAndrew Perepechko if (rc) 791f9df6458SAndrew Perepechko goto out; 792f9df6458SAndrew Perepechko 793e67b7985SStephen Smalley rc = security_context_str_to_sid(taskcon, &tsid, GFP_KERNEL); 794f9df6458SAndrew Perepechko if (rc) 795f9df6458SAndrew Perepechko goto out; 796f9df6458SAndrew Perepechko 797e67b7985SStephen Smalley rc = security_validate_transition_user(osid, nsid, tsid, tclass); 798f9df6458SAndrew Perepechko if (!rc) 799f9df6458SAndrew Perepechko rc = count; 800f9df6458SAndrew Perepechko out: 801f9df6458SAndrew Perepechko kfree(req); 802f9df6458SAndrew Perepechko kfree(oldcon); 803f9df6458SAndrew Perepechko kfree(newcon); 804f9df6458SAndrew Perepechko kfree(taskcon); 805f9df6458SAndrew Perepechko return rc; 806f9df6458SAndrew Perepechko } 807f9df6458SAndrew Perepechko 808f9df6458SAndrew Perepechko static const struct file_operations sel_transition_ops = { 809f9df6458SAndrew Perepechko .write = sel_write_validatetrans, 810f9df6458SAndrew Perepechko .llseek = generic_file_llseek, 811f9df6458SAndrew Perepechko }; 812f9df6458SAndrew Perepechko 8131da177e4SLinus Torvalds /* 8141da177e4SLinus Torvalds * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c 8151da177e4SLinus Torvalds */ 8161da177e4SLinus Torvalds static ssize_t sel_write_access(struct file *file, char *buf, size_t size); 8171da177e4SLinus Torvalds static ssize_t sel_write_create(struct file *file, char *buf, size_t size); 8181da177e4SLinus Torvalds static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size); 8191da177e4SLinus Torvalds static ssize_t sel_write_user(struct file *file, char *buf, size_t size); 8201da177e4SLinus Torvalds static ssize_t sel_write_member(struct file *file, char *buf, size_t size); 8211da177e4SLinus Torvalds 822631d2b49SEric Biggers static ssize_t (*const write_op[])(struct file *, char *, size_t) = { 8231da177e4SLinus Torvalds [SEL_ACCESS] = sel_write_access, 8241da177e4SLinus Torvalds [SEL_CREATE] = sel_write_create, 8251da177e4SLinus Torvalds [SEL_RELABEL] = sel_write_relabel, 8261da177e4SLinus Torvalds [SEL_USER] = sel_write_user, 8271da177e4SLinus Torvalds [SEL_MEMBER] = sel_write_member, 828ce9982d0SStephen Smalley [SEL_CONTEXT] = sel_write_context, 8291da177e4SLinus Torvalds }; 8301da177e4SLinus Torvalds 8311da177e4SLinus Torvalds static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos) 8321da177e4SLinus Torvalds { 833496ad9aaSAl Viro ino_t ino = file_inode(file)->i_ino; 8341da177e4SLinus Torvalds char *data; 8351da177e4SLinus Torvalds ssize_t rv; 8361da177e4SLinus Torvalds 8376e20a64aSNicolas Kaiser if (ino >= ARRAY_SIZE(write_op) || !write_op[ino]) 8381da177e4SLinus Torvalds return -EINVAL; 8391da177e4SLinus Torvalds 8401da177e4SLinus Torvalds data = simple_transaction_get(file, buf, size); 8411da177e4SLinus Torvalds if (IS_ERR(data)) 8421da177e4SLinus Torvalds return PTR_ERR(data); 8431da177e4SLinus Torvalds 8441da177e4SLinus Torvalds rv = write_op[ino](file, data, size); 8451da177e4SLinus Torvalds if (rv > 0) { 8461da177e4SLinus Torvalds simple_transaction_set(file, rv); 8471da177e4SLinus Torvalds rv = size; 8481da177e4SLinus Torvalds } 8491da177e4SLinus Torvalds return rv; 8501da177e4SLinus Torvalds } 8511da177e4SLinus Torvalds 8529c2e08c5SArjan van de Ven static const struct file_operations transaction_ops = { 8531da177e4SLinus Torvalds .write = selinux_transaction_write, 8541da177e4SLinus Torvalds .read = simple_transaction_read, 8551da177e4SLinus Torvalds .release = simple_transaction_release, 85657a62c23SArnd Bergmann .llseek = generic_file_llseek, 8571da177e4SLinus Torvalds }; 8581da177e4SLinus Torvalds 8591da177e4SLinus Torvalds /* 8601da177e4SLinus Torvalds * payload - write methods 8611da177e4SLinus Torvalds * If the method has a response, the response should be put in buf, 8621da177e4SLinus Torvalds * and the length returned. Otherwise return 0 or and -error. 8631da177e4SLinus Torvalds */ 8641da177e4SLinus Torvalds 8651da177e4SLinus Torvalds static ssize_t sel_write_access(struct file *file, char *buf, size_t size) 8661da177e4SLinus Torvalds { 867b77a493bSEric Paris char *scon = NULL, *tcon = NULL; 8681da177e4SLinus Torvalds u32 ssid, tsid; 8691da177e4SLinus Torvalds u16 tclass; 8701da177e4SLinus Torvalds struct av_decision avd; 8711da177e4SLinus Torvalds ssize_t length; 8721da177e4SLinus Torvalds 873e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 874be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_AV, NULL); 8751da177e4SLinus Torvalds if (length) 876b77a493bSEric Paris goto out; 8771da177e4SLinus Torvalds 8781da177e4SLinus Torvalds length = -ENOMEM; 87989d155efSJames Morris scon = kzalloc(size + 1, GFP_KERNEL); 8801da177e4SLinus Torvalds if (!scon) 881b77a493bSEric Paris goto out; 8821da177e4SLinus Torvalds 883b77a493bSEric Paris length = -ENOMEM; 88489d155efSJames Morris tcon = kzalloc(size + 1, GFP_KERNEL); 8851da177e4SLinus Torvalds if (!tcon) 8861da177e4SLinus Torvalds goto out; 8871da177e4SLinus Torvalds 8881da177e4SLinus Torvalds length = -EINVAL; 88919439d05SStephen Smalley if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) 890b77a493bSEric Paris goto out; 8911da177e4SLinus Torvalds 892e67b7985SStephen Smalley length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL); 893b77a493bSEric Paris if (length) 894b77a493bSEric Paris goto out; 895b77a493bSEric Paris 896e67b7985SStephen Smalley length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL); 897b77a493bSEric Paris if (length) 898b77a493bSEric Paris goto out; 8991da177e4SLinus Torvalds 900e67b7985SStephen Smalley security_compute_av_user(ssid, tsid, tclass, &avd); 9011da177e4SLinus Torvalds 9021da177e4SLinus Torvalds length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, 9038a6f83afSKaiGai Kohei "%x %x %x %x %u %x", 904f1c6381aSEric Paris avd.allowed, 0xffffffff, 9051da177e4SLinus Torvalds avd.auditallow, avd.auditdeny, 9068a6f83afSKaiGai Kohei avd.seqno, avd.flags); 9071da177e4SLinus Torvalds out: 908b77a493bSEric Paris kfree(tcon); 9091da177e4SLinus Torvalds kfree(scon); 9101da177e4SLinus Torvalds return length; 9111da177e4SLinus Torvalds } 9121da177e4SLinus Torvalds 9131da177e4SLinus Torvalds static ssize_t sel_write_create(struct file *file, char *buf, size_t size) 9141da177e4SLinus Torvalds { 915b77a493bSEric Paris char *scon = NULL, *tcon = NULL; 916f50a3ec9SKohei Kaigai char *namebuf = NULL, *objname = NULL; 9171da177e4SLinus Torvalds u32 ssid, tsid, newsid; 9181da177e4SLinus Torvalds u16 tclass; 9191da177e4SLinus Torvalds ssize_t length; 920b77a493bSEric Paris char *newcon = NULL; 9211da177e4SLinus Torvalds u32 len; 922f50a3ec9SKohei Kaigai int nargs; 9231da177e4SLinus Torvalds 924e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 925be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_CREATE, 926be0554c9SStephen Smalley NULL); 9271da177e4SLinus Torvalds if (length) 928b77a493bSEric Paris goto out; 9291da177e4SLinus Torvalds 9301da177e4SLinus Torvalds length = -ENOMEM; 93189d155efSJames Morris scon = kzalloc(size + 1, GFP_KERNEL); 9321da177e4SLinus Torvalds if (!scon) 933b77a493bSEric Paris goto out; 9341da177e4SLinus Torvalds 935b77a493bSEric Paris length = -ENOMEM; 93689d155efSJames Morris tcon = kzalloc(size + 1, GFP_KERNEL); 9371da177e4SLinus Torvalds if (!tcon) 9381da177e4SLinus Torvalds goto out; 9391da177e4SLinus Torvalds 940f50a3ec9SKohei Kaigai length = -ENOMEM; 941f50a3ec9SKohei Kaigai namebuf = kzalloc(size + 1, GFP_KERNEL); 942f50a3ec9SKohei Kaigai if (!namebuf) 943b77a493bSEric Paris goto out; 9441da177e4SLinus Torvalds 945f50a3ec9SKohei Kaigai length = -EINVAL; 946f50a3ec9SKohei Kaigai nargs = sscanf(buf, "%s %s %hu %s", scon, tcon, &tclass, namebuf); 947f50a3ec9SKohei Kaigai if (nargs < 3 || nargs > 4) 948f50a3ec9SKohei Kaigai goto out; 9490f7e4c33SKohei Kaigai if (nargs == 4) { 9500f7e4c33SKohei Kaigai /* 9510f7e4c33SKohei Kaigai * If and when the name of new object to be queried contains 9520f7e4c33SKohei Kaigai * either whitespace or multibyte characters, they shall be 9530f7e4c33SKohei Kaigai * encoded based on the percentage-encoding rule. 9540f7e4c33SKohei Kaigai * If not encoded, the sscanf logic picks up only left-half 9553d9047a0SChristian Göttsche * of the supplied name; split by a whitespace unexpectedly. 9560f7e4c33SKohei Kaigai */ 9570f7e4c33SKohei Kaigai char *r, *w; 9580f7e4c33SKohei Kaigai int c1, c2; 9590f7e4c33SKohei Kaigai 9600f7e4c33SKohei Kaigai r = w = namebuf; 9610f7e4c33SKohei Kaigai do { 9620f7e4c33SKohei Kaigai c1 = *r++; 9630f7e4c33SKohei Kaigai if (c1 == '+') 9640f7e4c33SKohei Kaigai c1 = ' '; 9650f7e4c33SKohei Kaigai else if (c1 == '%') { 966af7ff2c2SAndy Shevchenko c1 = hex_to_bin(*r++); 967af7ff2c2SAndy Shevchenko if (c1 < 0) 9680f7e4c33SKohei Kaigai goto out; 969af7ff2c2SAndy Shevchenko c2 = hex_to_bin(*r++); 970af7ff2c2SAndy Shevchenko if (c2 < 0) 9710f7e4c33SKohei Kaigai goto out; 9720f7e4c33SKohei Kaigai c1 = (c1 << 4) | c2; 9730f7e4c33SKohei Kaigai } 9740f7e4c33SKohei Kaigai *w++ = c1; 9750f7e4c33SKohei Kaigai } while (c1 != '\0'); 9760f7e4c33SKohei Kaigai 977f50a3ec9SKohei Kaigai objname = namebuf; 9780f7e4c33SKohei Kaigai } 979f50a3ec9SKohei Kaigai 980e67b7985SStephen Smalley length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL); 981b77a493bSEric Paris if (length) 982b77a493bSEric Paris goto out; 983b77a493bSEric Paris 984e67b7985SStephen Smalley length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL); 985b77a493bSEric Paris if (length) 986b77a493bSEric Paris goto out; 9871da177e4SLinus Torvalds 988e67b7985SStephen Smalley length = security_transition_sid_user(ssid, tsid, tclass, 9890619f0f5SStephen Smalley objname, &newsid); 990b77a493bSEric Paris if (length) 991b77a493bSEric Paris goto out; 9921da177e4SLinus Torvalds 993e67b7985SStephen Smalley length = security_sid_to_context(newsid, &newcon, &len); 994b77a493bSEric Paris if (length) 995b77a493bSEric Paris goto out; 9961da177e4SLinus Torvalds 997b77a493bSEric Paris length = -ERANGE; 9981da177e4SLinus Torvalds if (len > SIMPLE_TRANSACTION_LIMIT) { 999f8b69a5fSpeter enderborg pr_err("SELinux: %s: context size (%u) exceeds " 1000744ba35eSEric Paris "payload max\n", __func__, len); 1001b77a493bSEric Paris goto out; 10021da177e4SLinus Torvalds } 10031da177e4SLinus Torvalds 10041da177e4SLinus Torvalds memcpy(buf, newcon, len); 10051da177e4SLinus Torvalds length = len; 10061da177e4SLinus Torvalds out: 1007b77a493bSEric Paris kfree(newcon); 1008f50a3ec9SKohei Kaigai kfree(namebuf); 1009b77a493bSEric Paris kfree(tcon); 10101da177e4SLinus Torvalds kfree(scon); 10111da177e4SLinus Torvalds return length; 10121da177e4SLinus Torvalds } 10131da177e4SLinus Torvalds 10141da177e4SLinus Torvalds static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size) 10151da177e4SLinus Torvalds { 1016b77a493bSEric Paris char *scon = NULL, *tcon = NULL; 10171da177e4SLinus Torvalds u32 ssid, tsid, newsid; 10181da177e4SLinus Torvalds u16 tclass; 10191da177e4SLinus Torvalds ssize_t length; 1020b77a493bSEric Paris char *newcon = NULL; 10211da177e4SLinus Torvalds u32 len; 10221da177e4SLinus Torvalds 1023e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 1024be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_RELABEL, 1025be0554c9SStephen Smalley NULL); 10261da177e4SLinus Torvalds if (length) 1027b77a493bSEric Paris goto out; 10281da177e4SLinus Torvalds 10291da177e4SLinus Torvalds length = -ENOMEM; 103089d155efSJames Morris scon = kzalloc(size + 1, GFP_KERNEL); 10311da177e4SLinus Torvalds if (!scon) 1032b77a493bSEric Paris goto out; 10331da177e4SLinus Torvalds 1034b77a493bSEric Paris length = -ENOMEM; 103589d155efSJames Morris tcon = kzalloc(size + 1, GFP_KERNEL); 10361da177e4SLinus Torvalds if (!tcon) 10371da177e4SLinus Torvalds goto out; 10381da177e4SLinus Torvalds 10391da177e4SLinus Torvalds length = -EINVAL; 10401da177e4SLinus Torvalds if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) 1041b77a493bSEric Paris goto out; 10421da177e4SLinus Torvalds 1043e67b7985SStephen Smalley length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL); 1044b77a493bSEric Paris if (length) 1045b77a493bSEric Paris goto out; 1046b77a493bSEric Paris 1047e67b7985SStephen Smalley length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL); 1048b77a493bSEric Paris if (length) 1049b77a493bSEric Paris goto out; 10501da177e4SLinus Torvalds 1051e67b7985SStephen Smalley length = security_change_sid(ssid, tsid, tclass, &newsid); 1052b77a493bSEric Paris if (length) 1053b77a493bSEric Paris goto out; 10541da177e4SLinus Torvalds 1055e67b7985SStephen Smalley length = security_sid_to_context(newsid, &newcon, &len); 1056b77a493bSEric Paris if (length) 1057b77a493bSEric Paris goto out; 10581da177e4SLinus Torvalds 10591da177e4SLinus Torvalds length = -ERANGE; 1060b77a493bSEric Paris if (len > SIMPLE_TRANSACTION_LIMIT) 1061b77a493bSEric Paris goto out; 10621da177e4SLinus Torvalds 10631da177e4SLinus Torvalds memcpy(buf, newcon, len); 10641da177e4SLinus Torvalds length = len; 10651da177e4SLinus Torvalds out: 1066b77a493bSEric Paris kfree(newcon); 1067b77a493bSEric Paris kfree(tcon); 10681da177e4SLinus Torvalds kfree(scon); 10691da177e4SLinus Torvalds return length; 10701da177e4SLinus Torvalds } 10711da177e4SLinus Torvalds 10721da177e4SLinus Torvalds static ssize_t sel_write_user(struct file *file, char *buf, size_t size) 10731da177e4SLinus Torvalds { 1074b77a493bSEric Paris char *con = NULL, *user = NULL, *ptr; 1075b77a493bSEric Paris u32 sid, *sids = NULL; 10761da177e4SLinus Torvalds ssize_t length; 10771da177e4SLinus Torvalds char *newcon; 10781da177e4SLinus Torvalds int i, rc; 10791da177e4SLinus Torvalds u32 len, nsids; 10801da177e4SLinus Torvalds 1081e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 1082be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_USER, 1083be0554c9SStephen Smalley NULL); 10841da177e4SLinus Torvalds if (length) 10856eab04a8SJustin P. Mattock goto out; 10861da177e4SLinus Torvalds 10871da177e4SLinus Torvalds length = -ENOMEM; 108889d155efSJames Morris con = kzalloc(size + 1, GFP_KERNEL); 10891da177e4SLinus Torvalds if (!con) 10906eab04a8SJustin P. Mattock goto out; 10911da177e4SLinus Torvalds 1092b77a493bSEric Paris length = -ENOMEM; 109389d155efSJames Morris user = kzalloc(size + 1, GFP_KERNEL); 10941da177e4SLinus Torvalds if (!user) 10951da177e4SLinus Torvalds goto out; 10961da177e4SLinus Torvalds 10971da177e4SLinus Torvalds length = -EINVAL; 10981da177e4SLinus Torvalds if (sscanf(buf, "%s %s", con, user) != 2) 1099b77a493bSEric Paris goto out; 11001da177e4SLinus Torvalds 1101e67b7985SStephen Smalley length = security_context_str_to_sid(con, &sid, GFP_KERNEL); 1102b77a493bSEric Paris if (length) 1103b77a493bSEric Paris goto out; 11041da177e4SLinus Torvalds 1105e67b7985SStephen Smalley length = security_get_user_sids(sid, user, &sids, &nsids); 1106b77a493bSEric Paris if (length) 1107b77a493bSEric Paris goto out; 11081da177e4SLinus Torvalds 11091da177e4SLinus Torvalds length = sprintf(buf, "%u", nsids) + 1; 11101da177e4SLinus Torvalds ptr = buf + length; 11111da177e4SLinus Torvalds for (i = 0; i < nsids; i++) { 1112e67b7985SStephen Smalley rc = security_sid_to_context(sids[i], &newcon, &len); 11131da177e4SLinus Torvalds if (rc) { 11141da177e4SLinus Torvalds length = rc; 1115b77a493bSEric Paris goto out; 11161da177e4SLinus Torvalds } 11171da177e4SLinus Torvalds if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) { 11181da177e4SLinus Torvalds kfree(newcon); 11191da177e4SLinus Torvalds length = -ERANGE; 1120b77a493bSEric Paris goto out; 11211da177e4SLinus Torvalds } 11221da177e4SLinus Torvalds memcpy(ptr, newcon, len); 11231da177e4SLinus Torvalds kfree(newcon); 11241da177e4SLinus Torvalds ptr += len; 11251da177e4SLinus Torvalds length += len; 11261da177e4SLinus Torvalds } 11271da177e4SLinus Torvalds out: 1128b77a493bSEric Paris kfree(sids); 1129b77a493bSEric Paris kfree(user); 11301da177e4SLinus Torvalds kfree(con); 11311da177e4SLinus Torvalds return length; 11321da177e4SLinus Torvalds } 11331da177e4SLinus Torvalds 11341da177e4SLinus Torvalds static ssize_t sel_write_member(struct file *file, char *buf, size_t size) 11351da177e4SLinus Torvalds { 1136b77a493bSEric Paris char *scon = NULL, *tcon = NULL; 11371da177e4SLinus Torvalds u32 ssid, tsid, newsid; 11381da177e4SLinus Torvalds u16 tclass; 11391da177e4SLinus Torvalds ssize_t length; 1140b77a493bSEric Paris char *newcon = NULL; 11411da177e4SLinus Torvalds u32 len; 11421da177e4SLinus Torvalds 1143e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 1144be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_MEMBER, 1145be0554c9SStephen Smalley NULL); 11461da177e4SLinus Torvalds if (length) 1147b77a493bSEric Paris goto out; 11481da177e4SLinus Torvalds 11491da177e4SLinus Torvalds length = -ENOMEM; 115089d155efSJames Morris scon = kzalloc(size + 1, GFP_KERNEL); 11511da177e4SLinus Torvalds if (!scon) 11526eab04a8SJustin P. Mattock goto out; 11531da177e4SLinus Torvalds 1154b77a493bSEric Paris length = -ENOMEM; 115589d155efSJames Morris tcon = kzalloc(size + 1, GFP_KERNEL); 11561da177e4SLinus Torvalds if (!tcon) 11571da177e4SLinus Torvalds goto out; 11581da177e4SLinus Torvalds 11591da177e4SLinus Torvalds length = -EINVAL; 11601da177e4SLinus Torvalds if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) 1161b77a493bSEric Paris goto out; 11621da177e4SLinus Torvalds 1163e67b7985SStephen Smalley length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL); 1164b77a493bSEric Paris if (length) 1165b77a493bSEric Paris goto out; 1166b77a493bSEric Paris 1167e67b7985SStephen Smalley length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL); 1168b77a493bSEric Paris if (length) 1169b77a493bSEric Paris goto out; 11701da177e4SLinus Torvalds 1171e67b7985SStephen Smalley length = security_member_sid(ssid, tsid, tclass, &newsid); 1172b77a493bSEric Paris if (length) 1173b77a493bSEric Paris goto out; 11741da177e4SLinus Torvalds 1175e67b7985SStephen Smalley length = security_sid_to_context(newsid, &newcon, &len); 1176b77a493bSEric Paris if (length) 1177b77a493bSEric Paris goto out; 11781da177e4SLinus Torvalds 1179b77a493bSEric Paris length = -ERANGE; 11801da177e4SLinus Torvalds if (len > SIMPLE_TRANSACTION_LIMIT) { 1181f8b69a5fSpeter enderborg pr_err("SELinux: %s: context size (%u) exceeds " 1182744ba35eSEric Paris "payload max\n", __func__, len); 1183b77a493bSEric Paris goto out; 11841da177e4SLinus Torvalds } 11851da177e4SLinus Torvalds 11861da177e4SLinus Torvalds memcpy(buf, newcon, len); 11871da177e4SLinus Torvalds length = len; 11881da177e4SLinus Torvalds out: 1189b77a493bSEric Paris kfree(newcon); 1190b77a493bSEric Paris kfree(tcon); 11911da177e4SLinus Torvalds kfree(scon); 11921da177e4SLinus Torvalds return length; 11931da177e4SLinus Torvalds } 11941da177e4SLinus Torvalds 11951da177e4SLinus Torvalds static struct inode *sel_make_inode(struct super_block *sb, int mode) 11961da177e4SLinus Torvalds { 11971da177e4SLinus Torvalds struct inode *ret = new_inode(sb); 11981da177e4SLinus Torvalds 11991da177e4SLinus Torvalds if (ret) { 12001da177e4SLinus Torvalds ret->i_mode = mode; 1201078cd827SDeepa Dinamani ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret); 12021da177e4SLinus Torvalds } 12031da177e4SLinus Torvalds return ret; 12041da177e4SLinus Torvalds } 12051da177e4SLinus Torvalds 12061da177e4SLinus Torvalds static ssize_t sel_read_bool(struct file *filep, char __user *buf, 12071da177e4SLinus Torvalds size_t count, loff_t *ppos) 12081da177e4SLinus Torvalds { 12090619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info; 12101da177e4SLinus Torvalds char *page = NULL; 12111da177e4SLinus Torvalds ssize_t length; 12121da177e4SLinus Torvalds ssize_t ret; 12131da177e4SLinus Torvalds int cur_enforcing; 1214496ad9aaSAl Viro unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK; 1215d313f948SStephen Smalley const char *name = filep->f_path.dentry->d_name.name; 12161da177e4SLinus Torvalds 1217e67b7985SStephen Smalley mutex_lock(&selinux_state.policy_mutex); 12181da177e4SLinus Torvalds 1219d313f948SStephen Smalley ret = -EINVAL; 12200619f0f5SStephen Smalley if (index >= fsi->bool_num || strcmp(name, 12210619f0f5SStephen Smalley fsi->bool_pending_names[index])) 12220da74120SJann Horn goto out_unlock; 12231da177e4SLinus Torvalds 12241da177e4SLinus Torvalds ret = -ENOMEM; 1225b77a493bSEric Paris page = (char *)get_zeroed_page(GFP_KERNEL); 1226b77a493bSEric Paris if (!page) 12270da74120SJann Horn goto out_unlock; 12281da177e4SLinus Torvalds 1229e67b7985SStephen Smalley cur_enforcing = security_get_bool_value(index); 12301da177e4SLinus Torvalds if (cur_enforcing < 0) { 12311da177e4SLinus Torvalds ret = cur_enforcing; 12320da74120SJann Horn goto out_unlock; 12331da177e4SLinus Torvalds } 12341da177e4SLinus Torvalds length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing, 12350619f0f5SStephen Smalley fsi->bool_pending_values[index]); 1236e67b7985SStephen Smalley mutex_unlock(&selinux_state.policy_mutex); 12370da74120SJann Horn ret = simple_read_from_buffer(buf, count, ppos, page, length); 12380da74120SJann Horn out_free: 12391da177e4SLinus Torvalds free_page((unsigned long)page); 12401da177e4SLinus Torvalds return ret; 12410da74120SJann Horn 12420da74120SJann Horn out_unlock: 1243e67b7985SStephen Smalley mutex_unlock(&selinux_state.policy_mutex); 12440da74120SJann Horn goto out_free; 12451da177e4SLinus Torvalds } 12461da177e4SLinus Torvalds 12471da177e4SLinus Torvalds static ssize_t sel_write_bool(struct file *filep, const char __user *buf, 12481da177e4SLinus Torvalds size_t count, loff_t *ppos) 12491da177e4SLinus Torvalds { 12500619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info; 12511da177e4SLinus Torvalds char *page = NULL; 1252d313f948SStephen Smalley ssize_t length; 12531da177e4SLinus Torvalds int new_value; 1254496ad9aaSAl Viro unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK; 1255d313f948SStephen Smalley const char *name = filep->f_path.dentry->d_name.name; 12561da177e4SLinus Torvalds 12570da74120SJann Horn if (count >= PAGE_SIZE) 12580da74120SJann Horn return -ENOMEM; 12590da74120SJann Horn 12600da74120SJann Horn /* No partial writes. */ 12610da74120SJann Horn if (*ppos != 0) 12620da74120SJann Horn return -EINVAL; 12630da74120SJann Horn 12640da74120SJann Horn page = memdup_user_nul(buf, count); 12650da74120SJann Horn if (IS_ERR(page)) 12660da74120SJann Horn return PTR_ERR(page); 12670da74120SJann Horn 1268e67b7985SStephen Smalley mutex_lock(&selinux_state.policy_mutex); 12691da177e4SLinus Torvalds 1270e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 1271be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETBOOL, 1272be0554c9SStephen Smalley NULL); 12731da177e4SLinus Torvalds if (length) 12741da177e4SLinus Torvalds goto out; 12751da177e4SLinus Torvalds 1276d313f948SStephen Smalley length = -EINVAL; 12770619f0f5SStephen Smalley if (index >= fsi->bool_num || strcmp(name, 12780619f0f5SStephen Smalley fsi->bool_pending_names[index])) 1279d313f948SStephen Smalley goto out; 1280d313f948SStephen Smalley 12811da177e4SLinus Torvalds length = -EINVAL; 12821da177e4SLinus Torvalds if (sscanf(page, "%d", &new_value) != 1) 12831da177e4SLinus Torvalds goto out; 12841da177e4SLinus Torvalds 12851da177e4SLinus Torvalds if (new_value) 12861da177e4SLinus Torvalds new_value = 1; 12871da177e4SLinus Torvalds 12880619f0f5SStephen Smalley fsi->bool_pending_values[index] = new_value; 12891da177e4SLinus Torvalds length = count; 12901da177e4SLinus Torvalds 12911da177e4SLinus Torvalds out: 1292e67b7985SStephen Smalley mutex_unlock(&selinux_state.policy_mutex); 12938365a719SAl Viro kfree(page); 12941da177e4SLinus Torvalds return length; 12951da177e4SLinus Torvalds } 12961da177e4SLinus Torvalds 12979c2e08c5SArjan van de Ven static const struct file_operations sel_bool_ops = { 12981da177e4SLinus Torvalds .read = sel_read_bool, 12991da177e4SLinus Torvalds .write = sel_write_bool, 130057a62c23SArnd Bergmann .llseek = generic_file_llseek, 13011da177e4SLinus Torvalds }; 13021da177e4SLinus Torvalds 13031da177e4SLinus Torvalds static ssize_t sel_commit_bools_write(struct file *filep, 13041da177e4SLinus Torvalds const char __user *buf, 13051da177e4SLinus Torvalds size_t count, loff_t *ppos) 13061da177e4SLinus Torvalds { 13070619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info; 13081da177e4SLinus Torvalds char *page = NULL; 1309d313f948SStephen Smalley ssize_t length; 13101da177e4SLinus Torvalds int new_value; 13111da177e4SLinus Torvalds 13120da74120SJann Horn if (count >= PAGE_SIZE) 13130da74120SJann Horn return -ENOMEM; 13140da74120SJann Horn 13150da74120SJann Horn /* No partial writes. */ 13160da74120SJann Horn if (*ppos != 0) 13170da74120SJann Horn return -EINVAL; 13180da74120SJann Horn 13190da74120SJann Horn page = memdup_user_nul(buf, count); 13200da74120SJann Horn if (IS_ERR(page)) 13210da74120SJann Horn return PTR_ERR(page); 13220da74120SJann Horn 1323e67b7985SStephen Smalley mutex_lock(&selinux_state.policy_mutex); 13241da177e4SLinus Torvalds 1325e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 1326be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETBOOL, 1327be0554c9SStephen Smalley NULL); 13281da177e4SLinus Torvalds if (length) 13291da177e4SLinus Torvalds goto out; 13301da177e4SLinus Torvalds 13311da177e4SLinus Torvalds length = -EINVAL; 13321da177e4SLinus Torvalds if (sscanf(page, "%d", &new_value) != 1) 13331da177e4SLinus Torvalds goto out; 13341da177e4SLinus Torvalds 1335b77a493bSEric Paris length = 0; 13360619f0f5SStephen Smalley if (new_value && fsi->bool_pending_values) 1337e67b7985SStephen Smalley length = security_set_bools(fsi->bool_num, 13380619f0f5SStephen Smalley fsi->bool_pending_values); 13391da177e4SLinus Torvalds 1340b77a493bSEric Paris if (!length) 13411da177e4SLinus Torvalds length = count; 13421da177e4SLinus Torvalds 13431da177e4SLinus Torvalds out: 1344e67b7985SStephen Smalley mutex_unlock(&selinux_state.policy_mutex); 13458365a719SAl Viro kfree(page); 13461da177e4SLinus Torvalds return length; 13471da177e4SLinus Torvalds } 13481da177e4SLinus Torvalds 13499c2e08c5SArjan van de Ven static const struct file_operations sel_commit_bools_ops = { 13501da177e4SLinus Torvalds .write = sel_commit_bools_write, 135157a62c23SArnd Bergmann .llseek = generic_file_llseek, 13521da177e4SLinus Torvalds }; 13531da177e4SLinus Torvalds 13540c92d7c7SChristopher J. PeBenito static void sel_remove_entries(struct dentry *de) 13551da177e4SLinus Torvalds { 1356ad52184bSAl Viro d_genocide(de); 1357ad52184bSAl Viro shrink_dcache_parent(de); 13581da177e4SLinus Torvalds } 13591da177e4SLinus Torvalds 136066ec384aSDaniel Burgener static int sel_make_bools(struct selinux_policy *newpolicy, struct dentry *bool_dir, 136166ec384aSDaniel Burgener unsigned int *bool_num, char ***bool_pending_names, 1362c3fae2b2SChristian Göttsche int **bool_pending_values) 13631da177e4SLinus Torvalds { 136460abd318SOndrej Mosnacek int ret; 13651da177e4SLinus Torvalds ssize_t len; 13661da177e4SLinus Torvalds struct dentry *dentry = NULL; 13671da177e4SLinus Torvalds struct inode *inode = NULL; 13681da177e4SLinus Torvalds struct inode_security_struct *isec; 13691da177e4SLinus Torvalds char **names = NULL, *page; 137060abd318SOndrej Mosnacek u32 i, num; 13711da177e4SLinus Torvalds int *values = NULL; 13721da177e4SLinus Torvalds u32 sid; 13731da177e4SLinus Torvalds 1374b77a493bSEric Paris ret = -ENOMEM; 13751872981bSEric Paris page = (char *)get_zeroed_page(GFP_KERNEL); 13761872981bSEric Paris if (!page) 1377b77a493bSEric Paris goto out; 13781da177e4SLinus Torvalds 137902a52c5cSStephen Smalley ret = security_get_bools(newpolicy, &num, &names, &values); 1380b77a493bSEric Paris if (ret) 13811da177e4SLinus Torvalds goto out; 13821da177e4SLinus Torvalds 13831da177e4SLinus Torvalds for (i = 0; i < num; i++) { 1384b77a493bSEric Paris ret = -ENOMEM; 138566ec384aSDaniel Burgener dentry = d_alloc_name(bool_dir, names[i]); 1386b77a493bSEric Paris if (!dentry) 1387b77a493bSEric Paris goto out; 13881da177e4SLinus Torvalds 1389b77a493bSEric Paris ret = -ENOMEM; 139066ec384aSDaniel Burgener inode = sel_make_inode(bool_dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR); 13917e4237faSnixiaoming if (!inode) { 13927e4237faSnixiaoming dput(dentry); 1393b77a493bSEric Paris goto out; 13947e4237faSnixiaoming } 1395b77a493bSEric Paris 13961da177e4SLinus Torvalds ret = -ENAMETOOLONG; 1397cc1dad71SAl Viro len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]); 13987e4237faSnixiaoming if (len >= PAGE_SIZE) { 13997e4237faSnixiaoming dput(dentry); 14007e4237faSnixiaoming iput(inode); 1401b77a493bSEric Paris goto out; 14027e4237faSnixiaoming } 1403b77a493bSEric Paris 140480788c22SCasey Schaufler isec = selinux_inode(inode); 140502a52c5cSStephen Smalley ret = selinux_policy_genfs_sid(newpolicy, "selinuxfs", page, 1406aa8e712cSStephen Smalley SECCLASS_FILE, &sid); 14074262fb51SGary Tierney if (ret) { 1408900fde06SGary Tierney pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n", 1409900fde06SGary Tierney page); 1410900fde06SGary Tierney sid = SECINITSID_SECURITY; 14114262fb51SGary Tierney } 14124262fb51SGary Tierney 14131da177e4SLinus Torvalds isec->sid = sid; 141442059112SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 14151da177e4SLinus Torvalds inode->i_fop = &sel_bool_ops; 1416bce34bc0SJames Carter inode->i_ino = i|SEL_BOOL_INO_OFFSET; 14171da177e4SLinus Torvalds d_add(dentry, inode); 14181da177e4SLinus Torvalds } 141966ec384aSDaniel Burgener *bool_num = num; 142066ec384aSDaniel Burgener *bool_pending_names = names; 142166ec384aSDaniel Burgener *bool_pending_values = values; 1422b77a493bSEric Paris 1423b77a493bSEric Paris free_page((unsigned long)page); 1424b77a493bSEric Paris return 0; 14251da177e4SLinus Torvalds out: 14261da177e4SLinus Torvalds free_page((unsigned long)page); 1427b77a493bSEric Paris 14281da177e4SLinus Torvalds if (names) { 14299a5f04bfSJesper Juhl for (i = 0; i < num; i++) 14301da177e4SLinus Torvalds kfree(names[i]); 14311da177e4SLinus Torvalds kfree(names); 14321da177e4SLinus Torvalds } 143320c19e41SDavi Arnaut kfree(values); 143466ec384aSDaniel Burgener sel_remove_entries(bool_dir); 1435b77a493bSEric Paris 1436b77a493bSEric Paris return ret; 14371da177e4SLinus Torvalds } 14381da177e4SLinus Torvalds 14391da177e4SLinus Torvalds static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf, 14401da177e4SLinus Torvalds size_t count, loff_t *ppos) 14411da177e4SLinus Torvalds { 14421da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 14431da177e4SLinus Torvalds ssize_t length; 14441da177e4SLinus Torvalds 14456b6bc620SStephen Smalley length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 1446e67b7985SStephen Smalley avc_get_cache_threshold()); 14471da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 14481da177e4SLinus Torvalds } 14491da177e4SLinus Torvalds 14501da177e4SLinus Torvalds static ssize_t sel_write_avc_cache_threshold(struct file *file, 14511da177e4SLinus Torvalds const char __user *buf, 14521da177e4SLinus Torvalds size_t count, loff_t *ppos) 14531da177e4SLinus Torvalds 14541da177e4SLinus Torvalds { 14558365a719SAl Viro char *page; 14561da177e4SLinus Torvalds ssize_t ret; 1457309c5fadSHeinrich Schuchardt unsigned int new_value; 14581da177e4SLinus Torvalds 1459e67b7985SStephen Smalley ret = avc_has_perm(current_sid(), SECINITSID_SECURITY, 1460be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETSECPARAM, 1461be0554c9SStephen Smalley NULL); 14621da177e4SLinus Torvalds if (ret) 14638365a719SAl Viro return ret; 1464b77a493bSEric Paris 1465b77a493bSEric Paris if (count >= PAGE_SIZE) 14668365a719SAl Viro return -ENOMEM; 1467b77a493bSEric Paris 1468b77a493bSEric Paris /* No partial writes. */ 1469b77a493bSEric Paris if (*ppos != 0) 14708365a719SAl Viro return -EINVAL; 1471b77a493bSEric Paris 14728365a719SAl Viro page = memdup_user_nul(buf, count); 14738365a719SAl Viro if (IS_ERR(page)) 14748365a719SAl Viro return PTR_ERR(page); 1475b77a493bSEric Paris 1476b77a493bSEric Paris ret = -EINVAL; 1477b77a493bSEric Paris if (sscanf(page, "%u", &new_value) != 1) 1478b77a493bSEric Paris goto out; 1479b77a493bSEric Paris 1480e67b7985SStephen Smalley avc_set_cache_threshold(new_value); 1481b77a493bSEric Paris 14821da177e4SLinus Torvalds ret = count; 14831da177e4SLinus Torvalds out: 14848365a719SAl Viro kfree(page); 14851da177e4SLinus Torvalds return ret; 14861da177e4SLinus Torvalds } 14871da177e4SLinus Torvalds 14881da177e4SLinus Torvalds static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf, 14891da177e4SLinus Torvalds size_t count, loff_t *ppos) 14901da177e4SLinus Torvalds { 14911da177e4SLinus Torvalds char *page; 1492b77a493bSEric Paris ssize_t length; 14931da177e4SLinus Torvalds 14941da177e4SLinus Torvalds page = (char *)__get_free_page(GFP_KERNEL); 1495b77a493bSEric Paris if (!page) 1496b77a493bSEric Paris return -ENOMEM; 1497b77a493bSEric Paris 1498e67b7985SStephen Smalley length = avc_get_hash_stats(page); 1499b77a493bSEric Paris if (length >= 0) 1500b77a493bSEric Paris length = simple_read_from_buffer(buf, count, ppos, page, length); 15011da177e4SLinus Torvalds free_page((unsigned long)page); 1502b77a493bSEric Paris 1503b77a493bSEric Paris return length; 15041da177e4SLinus Torvalds } 15051da177e4SLinus Torvalds 150666f8e2f0SJeff Vander Stoep static ssize_t sel_read_sidtab_hash_stats(struct file *filp, char __user *buf, 150766f8e2f0SJeff Vander Stoep size_t count, loff_t *ppos) 150866f8e2f0SJeff Vander Stoep { 150966f8e2f0SJeff Vander Stoep char *page; 151066f8e2f0SJeff Vander Stoep ssize_t length; 151166f8e2f0SJeff Vander Stoep 151266f8e2f0SJeff Vander Stoep page = (char *)__get_free_page(GFP_KERNEL); 151366f8e2f0SJeff Vander Stoep if (!page) 151466f8e2f0SJeff Vander Stoep return -ENOMEM; 151566f8e2f0SJeff Vander Stoep 1516e67b7985SStephen Smalley length = security_sidtab_hash_stats(page); 151766f8e2f0SJeff Vander Stoep if (length >= 0) 151866f8e2f0SJeff Vander Stoep length = simple_read_from_buffer(buf, count, ppos, page, 151966f8e2f0SJeff Vander Stoep length); 152066f8e2f0SJeff Vander Stoep free_page((unsigned long)page); 152166f8e2f0SJeff Vander Stoep 152266f8e2f0SJeff Vander Stoep return length; 152366f8e2f0SJeff Vander Stoep } 152466f8e2f0SJeff Vander Stoep 152566f8e2f0SJeff Vander Stoep static const struct file_operations sel_sidtab_hash_stats_ops = { 152666f8e2f0SJeff Vander Stoep .read = sel_read_sidtab_hash_stats, 152766f8e2f0SJeff Vander Stoep .llseek = generic_file_llseek, 152866f8e2f0SJeff Vander Stoep }; 152966f8e2f0SJeff Vander Stoep 15309c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_threshold_ops = { 15311da177e4SLinus Torvalds .read = sel_read_avc_cache_threshold, 15321da177e4SLinus Torvalds .write = sel_write_avc_cache_threshold, 153357a62c23SArnd Bergmann .llseek = generic_file_llseek, 15341da177e4SLinus Torvalds }; 15351da177e4SLinus Torvalds 15369c2e08c5SArjan van de Ven static const struct file_operations sel_avc_hash_stats_ops = { 15371da177e4SLinus Torvalds .read = sel_read_avc_hash_stats, 153857a62c23SArnd Bergmann .llseek = generic_file_llseek, 15391da177e4SLinus Torvalds }; 15401da177e4SLinus Torvalds 15411da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS 15421da177e4SLinus Torvalds static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx) 15431da177e4SLinus Torvalds { 15441da177e4SLinus Torvalds int cpu; 15451da177e4SLinus Torvalds 15464f4b6c1aSRusty Russell for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) { 15471da177e4SLinus Torvalds if (!cpu_possible(cpu)) 15481da177e4SLinus Torvalds continue; 15491da177e4SLinus Torvalds *idx = cpu + 1; 15501da177e4SLinus Torvalds return &per_cpu(avc_cache_stats, cpu); 15511da177e4SLinus Torvalds } 15528d269a8eSVasily Averin (*idx)++; 15531da177e4SLinus Torvalds return NULL; 15541da177e4SLinus Torvalds } 15551da177e4SLinus Torvalds 15561da177e4SLinus Torvalds static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos) 15571da177e4SLinus Torvalds { 15581da177e4SLinus Torvalds loff_t n = *pos - 1; 15591da177e4SLinus Torvalds 15601da177e4SLinus Torvalds if (*pos == 0) 15611da177e4SLinus Torvalds return SEQ_START_TOKEN; 15621da177e4SLinus Torvalds 15631da177e4SLinus Torvalds return sel_avc_get_stat_idx(&n); 15641da177e4SLinus Torvalds } 15651da177e4SLinus Torvalds 15661da177e4SLinus Torvalds static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos) 15671da177e4SLinus Torvalds { 15681da177e4SLinus Torvalds return sel_avc_get_stat_idx(pos); 15691da177e4SLinus Torvalds } 15701da177e4SLinus Torvalds 15711da177e4SLinus Torvalds static int sel_avc_stats_seq_show(struct seq_file *seq, void *v) 15721da177e4SLinus Torvalds { 15731da177e4SLinus Torvalds struct avc_cache_stats *st = v; 15741da177e4SLinus Torvalds 1575710a0647SMarkus Elfring if (v == SEQ_START_TOKEN) { 1576710a0647SMarkus Elfring seq_puts(seq, 1577710a0647SMarkus Elfring "lookups hits misses allocations reclaims frees\n"); 1578710a0647SMarkus Elfring } else { 1579257313b2SLinus Torvalds unsigned int lookups = st->lookups; 1580257313b2SLinus Torvalds unsigned int misses = st->misses; 1581257313b2SLinus Torvalds unsigned int hits = lookups - misses; 1582257313b2SLinus Torvalds seq_printf(seq, "%u %u %u %u %u %u\n", lookups, 1583257313b2SLinus Torvalds hits, misses, st->allocations, 15841da177e4SLinus Torvalds st->reclaims, st->frees); 1585257313b2SLinus Torvalds } 15861da177e4SLinus Torvalds return 0; 15871da177e4SLinus Torvalds } 15881da177e4SLinus Torvalds 15891da177e4SLinus Torvalds static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v) 15901da177e4SLinus Torvalds { } 15911da177e4SLinus Torvalds 15921996a109SJan Engelhardt static const struct seq_operations sel_avc_cache_stats_seq_ops = { 15931da177e4SLinus Torvalds .start = sel_avc_stats_seq_start, 15941da177e4SLinus Torvalds .next = sel_avc_stats_seq_next, 15951da177e4SLinus Torvalds .show = sel_avc_stats_seq_show, 15961da177e4SLinus Torvalds .stop = sel_avc_stats_seq_stop, 15971da177e4SLinus Torvalds }; 15981da177e4SLinus Torvalds 15991da177e4SLinus Torvalds static int sel_open_avc_cache_stats(struct inode *inode, struct file *file) 16001da177e4SLinus Torvalds { 16011da177e4SLinus Torvalds return seq_open(file, &sel_avc_cache_stats_seq_ops); 16021da177e4SLinus Torvalds } 16031da177e4SLinus Torvalds 16049c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_stats_ops = { 16051da177e4SLinus Torvalds .open = sel_open_avc_cache_stats, 16061da177e4SLinus Torvalds .read = seq_read, 16071da177e4SLinus Torvalds .llseek = seq_lseek, 16081da177e4SLinus Torvalds .release = seq_release, 16091da177e4SLinus Torvalds }; 16101da177e4SLinus Torvalds #endif 16111da177e4SLinus Torvalds 16121da177e4SLinus Torvalds static int sel_make_avc_files(struct dentry *dir) 16131da177e4SLinus Torvalds { 16140619f0f5SStephen Smalley struct super_block *sb = dir->d_sb; 16150619f0f5SStephen Smalley struct selinux_fs_info *fsi = sb->s_fs_info; 1616b77a493bSEric Paris int i; 1617cda37124SEric Biggers static const struct tree_descr files[] = { 16181da177e4SLinus Torvalds { "cache_threshold", 16191da177e4SLinus Torvalds &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR }, 16201da177e4SLinus Torvalds { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO }, 16211da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS 16221da177e4SLinus Torvalds { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO }, 16231da177e4SLinus Torvalds #endif 16241da177e4SLinus Torvalds }; 16251da177e4SLinus Torvalds 16266e20a64aSNicolas Kaiser for (i = 0; i < ARRAY_SIZE(files); i++) { 16271da177e4SLinus Torvalds struct inode *inode; 16281da177e4SLinus Torvalds struct dentry *dentry; 16291da177e4SLinus Torvalds 16301da177e4SLinus Torvalds dentry = d_alloc_name(dir, files[i].name); 1631b77a493bSEric Paris if (!dentry) 1632b77a493bSEric Paris return -ENOMEM; 16331da177e4SLinus Torvalds 16341da177e4SLinus Torvalds inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode); 16357e4237faSnixiaoming if (!inode) { 16367e4237faSnixiaoming dput(dentry); 1637b77a493bSEric Paris return -ENOMEM; 16387e4237faSnixiaoming } 1639b77a493bSEric Paris 16401da177e4SLinus Torvalds inode->i_fop = files[i].ops; 16410619f0f5SStephen Smalley inode->i_ino = ++fsi->last_ino; 16421da177e4SLinus Torvalds d_add(dentry, inode); 16431da177e4SLinus Torvalds } 1644b77a493bSEric Paris 1645b77a493bSEric Paris return 0; 16461da177e4SLinus Torvalds } 16471da177e4SLinus Torvalds 164866f8e2f0SJeff Vander Stoep static int sel_make_ss_files(struct dentry *dir) 164966f8e2f0SJeff Vander Stoep { 165066f8e2f0SJeff Vander Stoep struct super_block *sb = dir->d_sb; 165166f8e2f0SJeff Vander Stoep struct selinux_fs_info *fsi = sb->s_fs_info; 165266f8e2f0SJeff Vander Stoep int i; 16534158cb60SChristian Göttsche static const struct tree_descr files[] = { 165466f8e2f0SJeff Vander Stoep { "sidtab_hash_stats", &sel_sidtab_hash_stats_ops, S_IRUGO }, 165566f8e2f0SJeff Vander Stoep }; 165666f8e2f0SJeff Vander Stoep 165766f8e2f0SJeff Vander Stoep for (i = 0; i < ARRAY_SIZE(files); i++) { 165866f8e2f0SJeff Vander Stoep struct inode *inode; 165966f8e2f0SJeff Vander Stoep struct dentry *dentry; 166066f8e2f0SJeff Vander Stoep 166166f8e2f0SJeff Vander Stoep dentry = d_alloc_name(dir, files[i].name); 166266f8e2f0SJeff Vander Stoep if (!dentry) 166366f8e2f0SJeff Vander Stoep return -ENOMEM; 166466f8e2f0SJeff Vander Stoep 166566f8e2f0SJeff Vander Stoep inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode); 166666f8e2f0SJeff Vander Stoep if (!inode) { 166766f8e2f0SJeff Vander Stoep dput(dentry); 166866f8e2f0SJeff Vander Stoep return -ENOMEM; 166966f8e2f0SJeff Vander Stoep } 167066f8e2f0SJeff Vander Stoep 167166f8e2f0SJeff Vander Stoep inode->i_fop = files[i].ops; 167266f8e2f0SJeff Vander Stoep inode->i_ino = ++fsi->last_ino; 167366f8e2f0SJeff Vander Stoep d_add(dentry, inode); 167466f8e2f0SJeff Vander Stoep } 167566f8e2f0SJeff Vander Stoep 167666f8e2f0SJeff Vander Stoep return 0; 167766f8e2f0SJeff Vander Stoep } 167866f8e2f0SJeff Vander Stoep 1679f0ee2e46SJames Carter static ssize_t sel_read_initcon(struct file *file, char __user *buf, 1680f0ee2e46SJames Carter size_t count, loff_t *ppos) 1681f0ee2e46SJames Carter { 1682f0ee2e46SJames Carter char *con; 1683f0ee2e46SJames Carter u32 sid, len; 1684f0ee2e46SJames Carter ssize_t ret; 1685f0ee2e46SJames Carter 1686496ad9aaSAl Viro sid = file_inode(file)->i_ino&SEL_INO_MASK; 1687e67b7985SStephen Smalley ret = security_sid_to_context(sid, &con, &len); 1688b77a493bSEric Paris if (ret) 1689f0ee2e46SJames Carter return ret; 1690f0ee2e46SJames Carter 1691f0ee2e46SJames Carter ret = simple_read_from_buffer(buf, count, ppos, con, len); 1692f0ee2e46SJames Carter kfree(con); 1693f0ee2e46SJames Carter return ret; 1694f0ee2e46SJames Carter } 1695f0ee2e46SJames Carter 1696f0ee2e46SJames Carter static const struct file_operations sel_initcon_ops = { 1697f0ee2e46SJames Carter .read = sel_read_initcon, 169857a62c23SArnd Bergmann .llseek = generic_file_llseek, 1699f0ee2e46SJames Carter }; 1700f0ee2e46SJames Carter 1701f0ee2e46SJames Carter static int sel_make_initcon_files(struct dentry *dir) 1702f0ee2e46SJames Carter { 1703b77a493bSEric Paris int i; 1704f0ee2e46SJames Carter 1705f0ee2e46SJames Carter for (i = 1; i <= SECINITSID_NUM; i++) { 1706f0ee2e46SJames Carter struct inode *inode; 1707f0ee2e46SJames Carter struct dentry *dentry; 1708e3e0b582SStephen Smalley const char *s = security_get_initial_sid_context(i); 1709e3e0b582SStephen Smalley 1710e3e0b582SStephen Smalley if (!s) 1711e3e0b582SStephen Smalley continue; 1712e3e0b582SStephen Smalley dentry = d_alloc_name(dir, s); 1713b77a493bSEric Paris if (!dentry) 1714b77a493bSEric Paris return -ENOMEM; 1715f0ee2e46SJames Carter 1716f0ee2e46SJames Carter inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); 17177e4237faSnixiaoming if (!inode) { 17187e4237faSnixiaoming dput(dentry); 1719b77a493bSEric Paris return -ENOMEM; 17207e4237faSnixiaoming } 1721b77a493bSEric Paris 1722f0ee2e46SJames Carter inode->i_fop = &sel_initcon_ops; 1723f0ee2e46SJames Carter inode->i_ino = i|SEL_INITCON_INO_OFFSET; 1724f0ee2e46SJames Carter d_add(dentry, inode); 1725f0ee2e46SJames Carter } 1726b77a493bSEric Paris 1727b77a493bSEric Paris return 0; 1728f0ee2e46SJames Carter } 1729f0ee2e46SJames Carter 1730e47c8fc5SChristopher J. PeBenito static inline unsigned long sel_class_to_ino(u16 class) 1731e47c8fc5SChristopher J. PeBenito { 1732e47c8fc5SChristopher J. PeBenito return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET; 1733e47c8fc5SChristopher J. PeBenito } 1734e47c8fc5SChristopher J. PeBenito 1735e47c8fc5SChristopher J. PeBenito static inline u16 sel_ino_to_class(unsigned long ino) 1736e47c8fc5SChristopher J. PeBenito { 173792ae9e82SEric Paris return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1); 1738e47c8fc5SChristopher J. PeBenito } 1739e47c8fc5SChristopher J. PeBenito 1740e47c8fc5SChristopher J. PeBenito static inline unsigned long sel_perm_to_ino(u16 class, u32 perm) 1741e47c8fc5SChristopher J. PeBenito { 1742e47c8fc5SChristopher J. PeBenito return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET; 1743e47c8fc5SChristopher J. PeBenito } 1744e47c8fc5SChristopher J. PeBenito 1745e47c8fc5SChristopher J. PeBenito static inline u32 sel_ino_to_perm(unsigned long ino) 1746e47c8fc5SChristopher J. PeBenito { 1747e47c8fc5SChristopher J. PeBenito return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1); 1748e47c8fc5SChristopher J. PeBenito } 1749e47c8fc5SChristopher J. PeBenito 1750e47c8fc5SChristopher J. PeBenito static ssize_t sel_read_class(struct file *file, char __user *buf, 1751e47c8fc5SChristopher J. PeBenito size_t count, loff_t *ppos) 1752e47c8fc5SChristopher J. PeBenito { 1753496ad9aaSAl Viro unsigned long ino = file_inode(file)->i_ino; 1754cc1dad71SAl Viro char res[TMPBUFLEN]; 17557e78c875Sliuyang34 ssize_t len = scnprintf(res, sizeof(res), "%d", sel_ino_to_class(ino)); 1756cc1dad71SAl Viro return simple_read_from_buffer(buf, count, ppos, res, len); 1757e47c8fc5SChristopher J. PeBenito } 1758e47c8fc5SChristopher J. PeBenito 1759e47c8fc5SChristopher J. PeBenito static const struct file_operations sel_class_ops = { 1760e47c8fc5SChristopher J. PeBenito .read = sel_read_class, 176157a62c23SArnd Bergmann .llseek = generic_file_llseek, 1762e47c8fc5SChristopher J. PeBenito }; 1763e47c8fc5SChristopher J. PeBenito 1764e47c8fc5SChristopher J. PeBenito static ssize_t sel_read_perm(struct file *file, char __user *buf, 1765e47c8fc5SChristopher J. PeBenito size_t count, loff_t *ppos) 1766e47c8fc5SChristopher J. PeBenito { 1767496ad9aaSAl Viro unsigned long ino = file_inode(file)->i_ino; 1768cc1dad71SAl Viro char res[TMPBUFLEN]; 17697e78c875Sliuyang34 ssize_t len = scnprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino)); 1770cc1dad71SAl Viro return simple_read_from_buffer(buf, count, ppos, res, len); 1771e47c8fc5SChristopher J. PeBenito } 1772e47c8fc5SChristopher J. PeBenito 1773e47c8fc5SChristopher J. PeBenito static const struct file_operations sel_perm_ops = { 1774e47c8fc5SChristopher J. PeBenito .read = sel_read_perm, 177557a62c23SArnd Bergmann .llseek = generic_file_llseek, 1776e47c8fc5SChristopher J. PeBenito }; 1777e47c8fc5SChristopher J. PeBenito 17783bb56b25SPaul Moore static ssize_t sel_read_policycap(struct file *file, char __user *buf, 17793bb56b25SPaul Moore size_t count, loff_t *ppos) 17803bb56b25SPaul Moore { 17813bb56b25SPaul Moore int value; 17823bb56b25SPaul Moore char tmpbuf[TMPBUFLEN]; 17833bb56b25SPaul Moore ssize_t length; 1784496ad9aaSAl Viro unsigned long i_ino = file_inode(file)->i_ino; 17853bb56b25SPaul Moore 1786e67b7985SStephen Smalley value = security_policycap_supported(i_ino & SEL_INO_MASK); 17873bb56b25SPaul Moore length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value); 17883bb56b25SPaul Moore 17893bb56b25SPaul Moore return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 17903bb56b25SPaul Moore } 17913bb56b25SPaul Moore 17923bb56b25SPaul Moore static const struct file_operations sel_policycap_ops = { 17933bb56b25SPaul Moore .read = sel_read_policycap, 179457a62c23SArnd Bergmann .llseek = generic_file_llseek, 17953bb56b25SPaul Moore }; 17963bb56b25SPaul Moore 179702a52c5cSStephen Smalley static int sel_make_perm_files(struct selinux_policy *newpolicy, 179802a52c5cSStephen Smalley char *objclass, int classvalue, 1799e47c8fc5SChristopher J. PeBenito struct dentry *dir) 1800e47c8fc5SChristopher J. PeBenito { 1801b77a493bSEric Paris int i, rc, nperms; 1802e47c8fc5SChristopher J. PeBenito char **perms; 1803e47c8fc5SChristopher J. PeBenito 180402a52c5cSStephen Smalley rc = security_get_permissions(newpolicy, objclass, &perms, &nperms); 1805e47c8fc5SChristopher J. PeBenito if (rc) 1806b77a493bSEric Paris return rc; 1807e47c8fc5SChristopher J. PeBenito 1808e47c8fc5SChristopher J. PeBenito for (i = 0; i < nperms; i++) { 1809e47c8fc5SChristopher J. PeBenito struct inode *inode; 1810e47c8fc5SChristopher J. PeBenito struct dentry *dentry; 1811e47c8fc5SChristopher J. PeBenito 1812b77a493bSEric Paris rc = -ENOMEM; 1813e47c8fc5SChristopher J. PeBenito dentry = d_alloc_name(dir, perms[i]); 1814b77a493bSEric Paris if (!dentry) 1815b77a493bSEric Paris goto out; 1816e47c8fc5SChristopher J. PeBenito 1817e47c8fc5SChristopher J. PeBenito rc = -ENOMEM; 1818b77a493bSEric Paris inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); 18197e4237faSnixiaoming if (!inode) { 18207e4237faSnixiaoming dput(dentry); 1821b77a493bSEric Paris goto out; 18227e4237faSnixiaoming } 1823b77a493bSEric Paris 1824e47c8fc5SChristopher J. PeBenito inode->i_fop = &sel_perm_ops; 1825e47c8fc5SChristopher J. PeBenito /* i+1 since perm values are 1-indexed */ 1826e47c8fc5SChristopher J. PeBenito inode->i_ino = sel_perm_to_ino(classvalue, i + 1); 1827e47c8fc5SChristopher J. PeBenito d_add(dentry, inode); 1828e47c8fc5SChristopher J. PeBenito } 1829b77a493bSEric Paris rc = 0; 1830b77a493bSEric Paris out: 1831e47c8fc5SChristopher J. PeBenito for (i = 0; i < nperms; i++) 1832e47c8fc5SChristopher J. PeBenito kfree(perms[i]); 1833e47c8fc5SChristopher J. PeBenito kfree(perms); 1834e47c8fc5SChristopher J. PeBenito return rc; 1835e47c8fc5SChristopher J. PeBenito } 1836e47c8fc5SChristopher J. PeBenito 183702a52c5cSStephen Smalley static int sel_make_class_dir_entries(struct selinux_policy *newpolicy, 183802a52c5cSStephen Smalley char *classname, int index, 1839e47c8fc5SChristopher J. PeBenito struct dentry *dir) 1840e47c8fc5SChristopher J. PeBenito { 18410619f0f5SStephen Smalley struct super_block *sb = dir->d_sb; 18420619f0f5SStephen Smalley struct selinux_fs_info *fsi = sb->s_fs_info; 1843e47c8fc5SChristopher J. PeBenito struct dentry *dentry = NULL; 1844e47c8fc5SChristopher J. PeBenito struct inode *inode = NULL; 1845e47c8fc5SChristopher J. PeBenito 1846e47c8fc5SChristopher J. PeBenito dentry = d_alloc_name(dir, "index"); 1847b77a493bSEric Paris if (!dentry) 1848b77a493bSEric Paris return -ENOMEM; 1849e47c8fc5SChristopher J. PeBenito 1850e47c8fc5SChristopher J. PeBenito inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); 18517e4237faSnixiaoming if (!inode) { 18527e4237faSnixiaoming dput(dentry); 1853b77a493bSEric Paris return -ENOMEM; 18547e4237faSnixiaoming } 1855e47c8fc5SChristopher J. PeBenito 1856e47c8fc5SChristopher J. PeBenito inode->i_fop = &sel_class_ops; 1857e47c8fc5SChristopher J. PeBenito inode->i_ino = sel_class_to_ino(index); 1858e47c8fc5SChristopher J. PeBenito d_add(dentry, inode); 1859e47c8fc5SChristopher J. PeBenito 18600619f0f5SStephen Smalley dentry = sel_make_dir(dir, "perms", &fsi->last_class_ino); 1861a1c2aa1eSAl Viro if (IS_ERR(dentry)) 1862a1c2aa1eSAl Viro return PTR_ERR(dentry); 1863e47c8fc5SChristopher J. PeBenito 18645698f081Sye xingchen return sel_make_perm_files(newpolicy, classname, index, dentry); 1865e47c8fc5SChristopher J. PeBenito } 1866e47c8fc5SChristopher J. PeBenito 186766ec384aSDaniel Burgener static int sel_make_classes(struct selinux_policy *newpolicy, 186866ec384aSDaniel Burgener struct dentry *class_dir, 186966ec384aSDaniel Burgener unsigned long *last_class_ino) 1870e47c8fc5SChristopher J. PeBenito { 18710619f0f5SStephen Smalley 1872b77a493bSEric Paris int rc, nclasses, i; 1873e47c8fc5SChristopher J. PeBenito char **classes; 1874e47c8fc5SChristopher J. PeBenito 187502a52c5cSStephen Smalley rc = security_get_classes(newpolicy, &classes, &nclasses); 1876b77a493bSEric Paris if (rc) 1877b77a493bSEric Paris return rc; 1878e47c8fc5SChristopher J. PeBenito 1879e47c8fc5SChristopher J. PeBenito /* +2 since classes are 1-indexed */ 188066ec384aSDaniel Burgener *last_class_ino = sel_class_to_ino(nclasses + 2); 1881e47c8fc5SChristopher J. PeBenito 1882e47c8fc5SChristopher J. PeBenito for (i = 0; i < nclasses; i++) { 1883e47c8fc5SChristopher J. PeBenito struct dentry *class_name_dir; 1884e47c8fc5SChristopher J. PeBenito 188566ec384aSDaniel Burgener class_name_dir = sel_make_dir(class_dir, classes[i], 188666ec384aSDaniel Burgener last_class_ino); 1887a1c2aa1eSAl Viro if (IS_ERR(class_name_dir)) { 1888a1c2aa1eSAl Viro rc = PTR_ERR(class_name_dir); 1889b77a493bSEric Paris goto out; 1890a1c2aa1eSAl Viro } 1891e47c8fc5SChristopher J. PeBenito 1892e47c8fc5SChristopher J. PeBenito /* i+1 since class values are 1-indexed */ 189302a52c5cSStephen Smalley rc = sel_make_class_dir_entries(newpolicy, classes[i], i + 1, 1894e47c8fc5SChristopher J. PeBenito class_name_dir); 1895e47c8fc5SChristopher J. PeBenito if (rc) 1896b77a493bSEric Paris goto out; 1897e47c8fc5SChristopher J. PeBenito } 1898b77a493bSEric Paris rc = 0; 1899b77a493bSEric Paris out: 1900e47c8fc5SChristopher J. PeBenito for (i = 0; i < nclasses; i++) 1901e47c8fc5SChristopher J. PeBenito kfree(classes[i]); 1902e47c8fc5SChristopher J. PeBenito kfree(classes); 1903e47c8fc5SChristopher J. PeBenito return rc; 1904e47c8fc5SChristopher J. PeBenito } 1905e47c8fc5SChristopher J. PeBenito 19060619f0f5SStephen Smalley static int sel_make_policycap(struct selinux_fs_info *fsi) 19073bb56b25SPaul Moore { 19083bb56b25SPaul Moore unsigned int iter; 19093bb56b25SPaul Moore struct dentry *dentry = NULL; 19103bb56b25SPaul Moore struct inode *inode = NULL; 19113bb56b25SPaul Moore 1912cdbec3edSPaul Moore for (iter = 0; iter <= POLICYDB_CAP_MAX; iter++) { 19134dc2fce3SStephen Smalley if (iter < ARRAY_SIZE(selinux_policycap_names)) 19140619f0f5SStephen Smalley dentry = d_alloc_name(fsi->policycap_dir, 19154dc2fce3SStephen Smalley selinux_policycap_names[iter]); 19163bb56b25SPaul Moore else 19170619f0f5SStephen Smalley dentry = d_alloc_name(fsi->policycap_dir, "unknown"); 19183bb56b25SPaul Moore 19193bb56b25SPaul Moore if (dentry == NULL) 19203bb56b25SPaul Moore return -ENOMEM; 19213bb56b25SPaul Moore 19220619f0f5SStephen Smalley inode = sel_make_inode(fsi->sb, S_IFREG | 0444); 19237e4237faSnixiaoming if (inode == NULL) { 19247e4237faSnixiaoming dput(dentry); 19253bb56b25SPaul Moore return -ENOMEM; 19267e4237faSnixiaoming } 19273bb56b25SPaul Moore 19283bb56b25SPaul Moore inode->i_fop = &sel_policycap_ops; 19293bb56b25SPaul Moore inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET; 19303bb56b25SPaul Moore d_add(dentry, inode); 19313bb56b25SPaul Moore } 19323bb56b25SPaul Moore 19333bb56b25SPaul Moore return 0; 19343bb56b25SPaul Moore } 19353bb56b25SPaul Moore 1936a1c2aa1eSAl Viro static struct dentry *sel_make_dir(struct dentry *dir, const char *name, 19370dd4ae51SChristopher J. PeBenito unsigned long *ino) 19381da177e4SLinus Torvalds { 1939a1c2aa1eSAl Viro struct dentry *dentry = d_alloc_name(dir, name); 19401da177e4SLinus Torvalds struct inode *inode; 19411da177e4SLinus Torvalds 1942a1c2aa1eSAl Viro if (!dentry) 1943a1c2aa1eSAl Viro return ERR_PTR(-ENOMEM); 1944a1c2aa1eSAl Viro 1945a1c2aa1eSAl Viro inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO); 1946a1c2aa1eSAl Viro if (!inode) { 1947a1c2aa1eSAl Viro dput(dentry); 1948a1c2aa1eSAl Viro return ERR_PTR(-ENOMEM); 1949a1c2aa1eSAl Viro } 1950b77a493bSEric Paris 19511da177e4SLinus Torvalds inode->i_op = &simple_dir_inode_operations; 19521da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations; 19530dd4ae51SChristopher J. PeBenito inode->i_ino = ++(*ino); 195440e906f8SJames Morris /* directory inodes start off with i_nlink == 2 (for "." entry) */ 1955d8c76e6fSDave Hansen inc_nlink(inode); 19561da177e4SLinus Torvalds d_add(dentry, inode); 1957edb20fb5SJames Morris /* bump link count on parent directory, too */ 1958ce0b16ddSDavid Howells inc_nlink(d_inode(dir)); 1959b77a493bSEric Paris 1960a1c2aa1eSAl Viro return dentry; 19611da177e4SLinus Torvalds } 19621da177e4SLinus Torvalds 19630eea6091SDaniel Burgener static struct dentry *sel_make_disconnected_dir(struct super_block *sb, 19640eea6091SDaniel Burgener unsigned long *ino) 19650eea6091SDaniel Burgener { 19660eea6091SDaniel Burgener struct inode *inode = sel_make_inode(sb, S_IFDIR | S_IRUGO | S_IXUGO); 19670eea6091SDaniel Burgener 19680eea6091SDaniel Burgener if (!inode) 19690eea6091SDaniel Burgener return ERR_PTR(-ENOMEM); 19700eea6091SDaniel Burgener 19710eea6091SDaniel Burgener inode->i_op = &simple_dir_inode_operations; 19720eea6091SDaniel Burgener inode->i_fop = &simple_dir_operations; 19730eea6091SDaniel Burgener inode->i_ino = ++(*ino); 19740eea6091SDaniel Burgener /* directory inodes start off with i_nlink == 2 (for "." entry) */ 19750eea6091SDaniel Burgener inc_nlink(inode); 19760eea6091SDaniel Burgener return d_obtain_alias(inode); 19770eea6091SDaniel Burgener } 19780eea6091SDaniel Burgener 19790619f0f5SStephen Smalley #define NULL_FILE_NAME "null" 19800619f0f5SStephen Smalley 1981920f50b2SDavid Howells static int sel_fill_super(struct super_block *sb, struct fs_context *fc) 19821da177e4SLinus Torvalds { 19830619f0f5SStephen Smalley struct selinux_fs_info *fsi; 19841da177e4SLinus Torvalds int ret; 19851da177e4SLinus Torvalds struct dentry *dentry; 1986a1c2aa1eSAl Viro struct inode *inode; 19871da177e4SLinus Torvalds struct inode_security_struct *isec; 19881da177e4SLinus Torvalds 1989cda37124SEric Biggers static const struct tree_descr selinux_files[] = { 19901da177e4SLinus Torvalds [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR}, 19911da177e4SLinus Torvalds [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR}, 1992ce9982d0SStephen Smalley [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO}, 19931da177e4SLinus Torvalds [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO}, 19941da177e4SLinus Torvalds [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO}, 19951da177e4SLinus Torvalds [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO}, 19961da177e4SLinus Torvalds [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO}, 19971da177e4SLinus Torvalds [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO}, 19981da177e4SLinus Torvalds [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR}, 19991da177e4SLinus Torvalds [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO}, 20001da177e4SLinus Torvalds [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR}, 20011da177e4SLinus Torvalds [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO}, 20021da177e4SLinus Torvalds [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR}, 20033f12070eSEric Paris [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO}, 20043f12070eSEric Paris [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO}, 200511904167SKaiGai Kohei [SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO}, 200672e8c859SEric Paris [SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO}, 2007f9df6458SAndrew Perepechko [SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops, 2008f9df6458SAndrew Perepechko S_IWUGO}, 20091da177e4SLinus Torvalds /* last one */ {""} 20101da177e4SLinus Torvalds }; 20110619f0f5SStephen Smalley 20120619f0f5SStephen Smalley ret = selinux_fs_info_create(sb); 20130619f0f5SStephen Smalley if (ret) 20140619f0f5SStephen Smalley goto err; 20150619f0f5SStephen Smalley 20161da177e4SLinus Torvalds ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files); 20171da177e4SLinus Torvalds if (ret) 2018161ce45aSJames Morris goto err; 20191da177e4SLinus Torvalds 20200619f0f5SStephen Smalley fsi = sb->s_fs_info; 20210619f0f5SStephen Smalley fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino); 20220619f0f5SStephen Smalley if (IS_ERR(fsi->bool_dir)) { 20230619f0f5SStephen Smalley ret = PTR_ERR(fsi->bool_dir); 20240619f0f5SStephen Smalley fsi->bool_dir = NULL; 2025161ce45aSJames Morris goto err; 2026a1c2aa1eSAl Viro } 20271da177e4SLinus Torvalds 2028b77a493bSEric Paris ret = -ENOMEM; 20291da177e4SLinus Torvalds dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME); 2030b77a493bSEric Paris if (!dentry) 2031161ce45aSJames Morris goto err; 20321da177e4SLinus Torvalds 2033161ce45aSJames Morris ret = -ENOMEM; 2034b77a493bSEric Paris inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO); 20357e4237faSnixiaoming if (!inode) { 20367e4237faSnixiaoming dput(dentry); 2037161ce45aSJames Morris goto err; 20387e4237faSnixiaoming } 2039b77a493bSEric Paris 20400619f0f5SStephen Smalley inode->i_ino = ++fsi->last_ino; 204180788c22SCasey Schaufler isec = selinux_inode(inode); 20421da177e4SLinus Torvalds isec->sid = SECINITSID_DEVNULL; 20431da177e4SLinus Torvalds isec->sclass = SECCLASS_CHR_FILE; 204442059112SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 20451da177e4SLinus Torvalds 20461da177e4SLinus Torvalds init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3)); 20471da177e4SLinus Torvalds d_add(dentry, inode); 20481da177e4SLinus Torvalds 20490619f0f5SStephen Smalley dentry = sel_make_dir(sb->s_root, "avc", &fsi->last_ino); 2050a1c2aa1eSAl Viro if (IS_ERR(dentry)) { 2051a1c2aa1eSAl Viro ret = PTR_ERR(dentry); 2052161ce45aSJames Morris goto err; 2053a1c2aa1eSAl Viro } 20541da177e4SLinus Torvalds 20551da177e4SLinus Torvalds ret = sel_make_avc_files(dentry); 2056bcb62828SChristian Göttsche if (ret) 2057bcb62828SChristian Göttsche goto err; 205866f8e2f0SJeff Vander Stoep 205966f8e2f0SJeff Vander Stoep dentry = sel_make_dir(sb->s_root, "ss", &fsi->last_ino); 206066f8e2f0SJeff Vander Stoep if (IS_ERR(dentry)) { 206166f8e2f0SJeff Vander Stoep ret = PTR_ERR(dentry); 206266f8e2f0SJeff Vander Stoep goto err; 206366f8e2f0SJeff Vander Stoep } 206466f8e2f0SJeff Vander Stoep 206566f8e2f0SJeff Vander Stoep ret = sel_make_ss_files(dentry); 20661da177e4SLinus Torvalds if (ret) 2067161ce45aSJames Morris goto err; 2068f0ee2e46SJames Carter 20690619f0f5SStephen Smalley dentry = sel_make_dir(sb->s_root, "initial_contexts", &fsi->last_ino); 2070a1c2aa1eSAl Viro if (IS_ERR(dentry)) { 2071a1c2aa1eSAl Viro ret = PTR_ERR(dentry); 2072f0ee2e46SJames Carter goto err; 2073a1c2aa1eSAl Viro } 2074f0ee2e46SJames Carter 2075f0ee2e46SJames Carter ret = sel_make_initcon_files(dentry); 2076f0ee2e46SJames Carter if (ret) 2077f0ee2e46SJames Carter goto err; 2078f0ee2e46SJames Carter 2079613ba187SDaniel Burgener fsi->class_dir = sel_make_dir(sb->s_root, CLASS_DIR_NAME, &fsi->last_ino); 20800619f0f5SStephen Smalley if (IS_ERR(fsi->class_dir)) { 20810619f0f5SStephen Smalley ret = PTR_ERR(fsi->class_dir); 20820619f0f5SStephen Smalley fsi->class_dir = NULL; 2083e47c8fc5SChristopher J. PeBenito goto err; 2084a1c2aa1eSAl Viro } 2085e47c8fc5SChristopher J. PeBenito 2086613ba187SDaniel Burgener fsi->policycap_dir = sel_make_dir(sb->s_root, POLICYCAP_DIR_NAME, 20870619f0f5SStephen Smalley &fsi->last_ino); 20880619f0f5SStephen Smalley if (IS_ERR(fsi->policycap_dir)) { 20890619f0f5SStephen Smalley ret = PTR_ERR(fsi->policycap_dir); 20900619f0f5SStephen Smalley fsi->policycap_dir = NULL; 2091e47c8fc5SChristopher J. PeBenito goto err; 2092a1c2aa1eSAl Viro } 20930619f0f5SStephen Smalley 209402a52c5cSStephen Smalley ret = sel_make_policycap(fsi); 209502a52c5cSStephen Smalley if (ret) { 209602a52c5cSStephen Smalley pr_err("SELinux: failed to load policy capabilities\n"); 20970619f0f5SStephen Smalley goto err; 209802a52c5cSStephen Smalley } 209902a52c5cSStephen Smalley 2100b77a493bSEric Paris return 0; 2101161ce45aSJames Morris err: 2102f8b69a5fSpeter enderborg pr_err("SELinux: %s: failed while creating inodes\n", 2103744ba35eSEric Paris __func__); 21040619f0f5SStephen Smalley 21050619f0f5SStephen Smalley selinux_fs_info_free(sb); 21060619f0f5SStephen Smalley 2107b77a493bSEric Paris return ret; 21081da177e4SLinus Torvalds } 21091da177e4SLinus Torvalds 2110920f50b2SDavid Howells static int sel_get_tree(struct fs_context *fc) 21111da177e4SLinus Torvalds { 2112920f50b2SDavid Howells return get_tree_single(fc, sel_fill_super); 2113920f50b2SDavid Howells } 2114920f50b2SDavid Howells 2115920f50b2SDavid Howells static const struct fs_context_operations sel_context_ops = { 2116920f50b2SDavid Howells .get_tree = sel_get_tree, 2117920f50b2SDavid Howells }; 2118920f50b2SDavid Howells 2119920f50b2SDavid Howells static int sel_init_fs_context(struct fs_context *fc) 2120920f50b2SDavid Howells { 2121920f50b2SDavid Howells fc->ops = &sel_context_ops; 2122920f50b2SDavid Howells return 0; 21231da177e4SLinus Torvalds } 21241da177e4SLinus Torvalds 21250619f0f5SStephen Smalley static void sel_kill_sb(struct super_block *sb) 21260619f0f5SStephen Smalley { 21270619f0f5SStephen Smalley selinux_fs_info_free(sb); 21280619f0f5SStephen Smalley kill_litter_super(sb); 21290619f0f5SStephen Smalley } 21300619f0f5SStephen Smalley 21311da177e4SLinus Torvalds static struct file_system_type sel_fs_type = { 21321da177e4SLinus Torvalds .name = "selinuxfs", 2133920f50b2SDavid Howells .init_fs_context = sel_init_fs_context, 21340619f0f5SStephen Smalley .kill_sb = sel_kill_sb, 21351da177e4SLinus Torvalds }; 21361da177e4SLinus Torvalds 2137cd2bb4cbSOndrej Mosnacek static struct vfsmount *selinuxfs_mount __ro_after_init; 2138cd2bb4cbSOndrej Mosnacek struct path selinux_null __ro_after_init; 21391da177e4SLinus Torvalds 21401da177e4SLinus Torvalds static int __init init_sel_fs(void) 21411da177e4SLinus Torvalds { 21420619f0f5SStephen Smalley struct qstr null_name = QSTR_INIT(NULL_FILE_NAME, 21430619f0f5SStephen Smalley sizeof(NULL_FILE_NAME)-1); 21441da177e4SLinus Torvalds int err; 21451da177e4SLinus Torvalds 21466c5a682eSStephen Smalley if (!selinux_enabled_boot) 21471da177e4SLinus Torvalds return 0; 21487a627e3bSGreg Kroah-Hartman 2149f9bb4882SEric W. Biederman err = sysfs_create_mount_point(fs_kobj, "selinux"); 2150f9bb4882SEric W. Biederman if (err) 2151f9bb4882SEric W. Biederman return err; 21527a627e3bSGreg Kroah-Hartman 21531da177e4SLinus Torvalds err = register_filesystem(&sel_fs_type); 21547a627e3bSGreg Kroah-Hartman if (err) { 2155f9bb4882SEric W. Biederman sysfs_remove_mount_point(fs_kobj, "selinux"); 2156b77a493bSEric Paris return err; 21577a627e3bSGreg Kroah-Hartman } 2158b77a493bSEric Paris 2159765927b2SAl Viro selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type); 21601da177e4SLinus Torvalds if (IS_ERR(selinuxfs_mount)) { 2161f8b69a5fSpeter enderborg pr_err("selinuxfs: could not mount!\n"); 21621da177e4SLinus Torvalds err = PTR_ERR(selinuxfs_mount); 21631da177e4SLinus Torvalds selinuxfs_mount = NULL; 21641da177e4SLinus Torvalds } 21650619f0f5SStephen Smalley selinux_null.dentry = d_hash_and_lookup(selinux_null.mnt->mnt_root, 21660619f0f5SStephen Smalley &null_name); 21670619f0f5SStephen Smalley if (IS_ERR(selinux_null.dentry)) { 21680619f0f5SStephen Smalley pr_err("selinuxfs: could not lookup null!\n"); 21690619f0f5SStephen Smalley err = PTR_ERR(selinux_null.dentry); 21700619f0f5SStephen Smalley selinux_null.dentry = NULL; 21710619f0f5SStephen Smalley } 2172b77a493bSEric Paris 21731da177e4SLinus Torvalds return err; 21741da177e4SLinus Torvalds } 21751da177e4SLinus Torvalds 21761da177e4SLinus Torvalds __initcall(init_sel_fs); 2177