1a10e763bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* Updated: Karl MacMillan <kmacmillan@tresys.com> 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Added conditional policy language extensions 51da177e4SLinus Torvalds * 682c21bfaSPaul Moore * Updated: Hewlett-Packard <paul@paul-moore.com> 73bb56b25SPaul Moore * 83bb56b25SPaul Moore * Added support for the policy capability bitmap 93bb56b25SPaul Moore * 103bb56b25SPaul Moore * Copyright (C) 2007 Hewlett-Packard Development Company, L.P. 111da177e4SLinus Torvalds * Copyright (C) 2003 - 2004 Tresys Technology, LLC 121da177e4SLinus Torvalds * Copyright (C) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com> 131da177e4SLinus Torvalds */ 141da177e4SLinus Torvalds 151da177e4SLinus Torvalds #include <linux/kernel.h> 161da177e4SLinus Torvalds #include <linux/pagemap.h> 171da177e4SLinus Torvalds #include <linux/slab.h> 181da177e4SLinus Torvalds #include <linux/vmalloc.h> 191da177e4SLinus Torvalds #include <linux/fs.h> 20920f50b2SDavid Howells #include <linux/fs_context.h> 210619f0f5SStephen Smalley #include <linux/mount.h> 22bb003079SIngo Molnar #include <linux/mutex.h> 230eea6091SDaniel Burgener #include <linux/namei.h> 241da177e4SLinus Torvalds #include <linux/init.h> 251da177e4SLinus Torvalds #include <linux/string.h> 261da177e4SLinus Torvalds #include <linux/security.h> 271da177e4SLinus Torvalds #include <linux/major.h> 281da177e4SLinus Torvalds #include <linux/seq_file.h> 291da177e4SLinus Torvalds #include <linux/percpu.h> 30af601e46SSteve Grubb #include <linux/audit.h> 31f5269710SEric Paris #include <linux/uaccess.h> 327a627e3bSGreg Kroah-Hartman #include <linux/kobject.h> 330f7e4c33SKohei Kaigai #include <linux/ctype.h> 341da177e4SLinus Torvalds 351da177e4SLinus Torvalds /* selinuxfs pseudo filesystem for exporting the security policy API. 361da177e4SLinus Torvalds Based on the proc code and the fs/nfsd/nfsctl.c code. */ 371da177e4SLinus Torvalds 381da177e4SLinus Torvalds #include "flask.h" 391da177e4SLinus Torvalds #include "avc.h" 401da177e4SLinus Torvalds #include "avc_ss.h" 411da177e4SLinus Torvalds #include "security.h" 421da177e4SLinus Torvalds #include "objsec.h" 431da177e4SLinus Torvalds #include "conditional.h" 442554a48fSLakshmi Ramasubramanian #include "ima.h" 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds enum sel_inos { 471da177e4SLinus Torvalds SEL_ROOT_INO = 2, 481da177e4SLinus Torvalds SEL_LOAD, /* load policy */ 491da177e4SLinus Torvalds SEL_ENFORCE, /* get or set enforcing status */ 501da177e4SLinus Torvalds SEL_CONTEXT, /* validate context */ 511da177e4SLinus Torvalds SEL_ACCESS, /* compute access decision */ 521da177e4SLinus Torvalds SEL_CREATE, /* compute create labeling decision */ 531da177e4SLinus Torvalds SEL_RELABEL, /* compute relabeling decision */ 541da177e4SLinus Torvalds SEL_USER, /* compute reachable user contexts */ 551da177e4SLinus Torvalds SEL_POLICYVERS, /* return policy version for this kernel */ 561da177e4SLinus Torvalds SEL_COMMIT_BOOLS, /* commit new boolean values */ 571da177e4SLinus Torvalds SEL_MLS, /* return if MLS policy is enabled */ 581da177e4SLinus Torvalds SEL_DISABLE, /* disable SELinux until next reboot */ 591da177e4SLinus Torvalds SEL_MEMBER, /* compute polyinstantiation membership decision */ 601da177e4SLinus Torvalds SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */ 614e5ab4cbSJames Morris SEL_COMPAT_NET, /* whether to use old compat network packet controls */ 623f12070eSEric Paris SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */ 633f12070eSEric Paris SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */ 6411904167SKaiGai Kohei SEL_STATUS, /* export current status using mmap() */ 65cee74f47SEric Paris SEL_POLICY, /* allow userspace to read the in kernel policy */ 66f9df6458SAndrew Perepechko SEL_VALIDATE_TRANS, /* compute validatetrans decision */ 676174eafcSJames Carter SEL_INO_NEXT, /* The next inode number to use */ 681da177e4SLinus Torvalds }; 691da177e4SLinus Torvalds 700619f0f5SStephen Smalley struct selinux_fs_info { 710619f0f5SStephen Smalley struct dentry *bool_dir; 720619f0f5SStephen Smalley unsigned int bool_num; 730619f0f5SStephen Smalley char **bool_pending_names; 74c3fae2b2SChristian Göttsche int *bool_pending_values; 750619f0f5SStephen Smalley struct dentry *class_dir; 760619f0f5SStephen Smalley unsigned long last_class_ino; 770619f0f5SStephen Smalley bool policy_opened; 780619f0f5SStephen Smalley struct dentry *policycap_dir; 790619f0f5SStephen Smalley unsigned long last_ino; 800619f0f5SStephen Smalley struct super_block *sb; 810619f0f5SStephen Smalley }; 820619f0f5SStephen Smalley 830619f0f5SStephen Smalley static int selinux_fs_info_create(struct super_block *sb) 840619f0f5SStephen Smalley { 850619f0f5SStephen Smalley struct selinux_fs_info *fsi; 860619f0f5SStephen Smalley 870619f0f5SStephen Smalley fsi = kzalloc(sizeof(*fsi), GFP_KERNEL); 880619f0f5SStephen Smalley if (!fsi) 890619f0f5SStephen Smalley return -ENOMEM; 900619f0f5SStephen Smalley 910619f0f5SStephen Smalley fsi->last_ino = SEL_INO_NEXT - 1; 920619f0f5SStephen Smalley fsi->sb = sb; 930619f0f5SStephen Smalley sb->s_fs_info = fsi; 940619f0f5SStephen Smalley return 0; 950619f0f5SStephen Smalley } 960619f0f5SStephen Smalley 970619f0f5SStephen Smalley static void selinux_fs_info_free(struct super_block *sb) 980619f0f5SStephen Smalley { 990619f0f5SStephen Smalley struct selinux_fs_info *fsi = sb->s_fs_info; 1000619f0f5SStephen Smalley int i; 1010619f0f5SStephen Smalley 1020619f0f5SStephen Smalley if (fsi) { 1030619f0f5SStephen Smalley for (i = 0; i < fsi->bool_num; i++) 1040619f0f5SStephen Smalley kfree(fsi->bool_pending_names[i]); 1050619f0f5SStephen Smalley kfree(fsi->bool_pending_names); 1060619f0f5SStephen Smalley kfree(fsi->bool_pending_values); 1070619f0f5SStephen Smalley } 1080619f0f5SStephen Smalley kfree(sb->s_fs_info); 1090619f0f5SStephen Smalley sb->s_fs_info = NULL; 1100619f0f5SStephen Smalley } 1116174eafcSJames Carter 112f0ee2e46SJames Carter #define SEL_INITCON_INO_OFFSET 0x01000000 113bce34bc0SJames Carter #define SEL_BOOL_INO_OFFSET 0x02000000 114e47c8fc5SChristopher J. PeBenito #define SEL_CLASS_INO_OFFSET 0x04000000 1153bb56b25SPaul Moore #define SEL_POLICYCAP_INO_OFFSET 0x08000000 116f0ee2e46SJames Carter #define SEL_INO_MASK 0x00ffffff 117f0ee2e46SJames Carter 118613ba187SDaniel Burgener #define BOOL_DIR_NAME "booleans" 119613ba187SDaniel Burgener #define CLASS_DIR_NAME "class" 120613ba187SDaniel Burgener #define POLICYCAP_DIR_NAME "policy_capabilities" 121613ba187SDaniel Burgener 1221da177e4SLinus Torvalds #define TMPBUFLEN 12 1231da177e4SLinus Torvalds static ssize_t sel_read_enforce(struct file *filp, char __user *buf, 1241da177e4SLinus Torvalds size_t count, loff_t *ppos) 1251da177e4SLinus Torvalds { 1261da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 1271da177e4SLinus Torvalds ssize_t length; 1281da177e4SLinus Torvalds 129aa8e712cSStephen Smalley length = scnprintf(tmpbuf, TMPBUFLEN, "%d", 130e67b7985SStephen Smalley enforcing_enabled()); 1311da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 1321da177e4SLinus Torvalds } 1331da177e4SLinus Torvalds 1341da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP 1351da177e4SLinus Torvalds static ssize_t sel_write_enforce(struct file *file, const char __user *buf, 1361da177e4SLinus Torvalds size_t count, loff_t *ppos) 1371da177e4SLinus Torvalds 1381da177e4SLinus Torvalds { 139b77a493bSEric Paris char *page = NULL; 1401da177e4SLinus Torvalds ssize_t length; 141aa8e712cSStephen Smalley int old_value, new_value; 1421da177e4SLinus Torvalds 143bfd51626SDavi Arnaut if (count >= PAGE_SIZE) 1448365a719SAl Viro return -ENOMEM; 145b77a493bSEric Paris 1461da177e4SLinus Torvalds /* No partial writes. */ 147b77a493bSEric Paris if (*ppos != 0) 1488365a719SAl Viro return -EINVAL; 149b77a493bSEric Paris 1508365a719SAl Viro page = memdup_user_nul(buf, count); 1518365a719SAl Viro if (IS_ERR(page)) 1528365a719SAl Viro return PTR_ERR(page); 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds length = -EINVAL; 1551da177e4SLinus Torvalds if (sscanf(page, "%d", &new_value) != 1) 1561da177e4SLinus Torvalds goto out; 1571da177e4SLinus Torvalds 158ea49d10eSStephen Smalley new_value = !!new_value; 159ea49d10eSStephen Smalley 160e67b7985SStephen Smalley old_value = enforcing_enabled(); 161aa8e712cSStephen Smalley if (new_value != old_value) { 162e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 163be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETENFORCE, 164be0554c9SStephen Smalley NULL); 1651da177e4SLinus Torvalds if (length) 1661da177e4SLinus Torvalds goto out; 167cdfb6b34SRichard Guy Briggs audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS, 1684195ed42SRichard Guy Briggs "enforcing=%d old_enforcing=%d auid=%u ses=%u" 1696c5a682eSStephen Smalley " enabled=1 old-enabled=1 lsm=selinux res=1", 170aa8e712cSStephen Smalley new_value, old_value, 171581abc09SEric W. Biederman from_kuid(&init_user_ns, audit_get_loginuid(current)), 1726c5a682eSStephen Smalley audit_get_sessionid(current)); 173e67b7985SStephen Smalley enforcing_set(new_value); 174aa8e712cSStephen Smalley if (new_value) 175e67b7985SStephen Smalley avc_ss_reset(0); 176aa8e712cSStephen Smalley selnl_notify_setenforce(new_value); 177e67b7985SStephen Smalley selinux_status_update_setenforce(new_value); 178aa8e712cSStephen Smalley if (!new_value) 17942df744cSJanne Karhunen call_blocking_lsm_notifier(LSM_POLICY_CHANGE, NULL); 1802554a48fSLakshmi Ramasubramanian 181e67b7985SStephen Smalley selinux_ima_measure_state(); 1821da177e4SLinus Torvalds } 1831da177e4SLinus Torvalds length = count; 1841da177e4SLinus Torvalds out: 1858365a719SAl Viro kfree(page); 1861da177e4SLinus Torvalds return length; 1871da177e4SLinus Torvalds } 1881da177e4SLinus Torvalds #else 1891da177e4SLinus Torvalds #define sel_write_enforce NULL 1901da177e4SLinus Torvalds #endif 1911da177e4SLinus Torvalds 1929c2e08c5SArjan van de Ven static const struct file_operations sel_enforce_ops = { 1931da177e4SLinus Torvalds .read = sel_read_enforce, 1941da177e4SLinus Torvalds .write = sel_write_enforce, 19557a62c23SArnd Bergmann .llseek = generic_file_llseek, 1961da177e4SLinus Torvalds }; 1971da177e4SLinus Torvalds 1983f12070eSEric Paris static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf, 1993f12070eSEric Paris size_t count, loff_t *ppos) 2003f12070eSEric Paris { 2013f12070eSEric Paris char tmpbuf[TMPBUFLEN]; 2023f12070eSEric Paris ssize_t length; 203496ad9aaSAl Viro ino_t ino = file_inode(filp)->i_ino; 2043f12070eSEric Paris int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ? 205e67b7985SStephen Smalley security_get_reject_unknown() : 206e67b7985SStephen Smalley !security_get_allow_unknown(); 2073f12070eSEric Paris 2083f12070eSEric Paris length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown); 2093f12070eSEric Paris return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 2103f12070eSEric Paris } 2113f12070eSEric Paris 2123f12070eSEric Paris static const struct file_operations sel_handle_unknown_ops = { 2133f12070eSEric Paris .read = sel_read_handle_unknown, 21457a62c23SArnd Bergmann .llseek = generic_file_llseek, 2153f12070eSEric Paris }; 2163f12070eSEric Paris 21711904167SKaiGai Kohei static int sel_open_handle_status(struct inode *inode, struct file *filp) 21811904167SKaiGai Kohei { 219e67b7985SStephen Smalley struct page *status = selinux_kernel_status_page(); 22011904167SKaiGai Kohei 22111904167SKaiGai Kohei if (!status) 22211904167SKaiGai Kohei return -ENOMEM; 22311904167SKaiGai Kohei 22411904167SKaiGai Kohei filp->private_data = status; 22511904167SKaiGai Kohei 22611904167SKaiGai Kohei return 0; 22711904167SKaiGai Kohei } 22811904167SKaiGai Kohei 22911904167SKaiGai Kohei static ssize_t sel_read_handle_status(struct file *filp, char __user *buf, 23011904167SKaiGai Kohei size_t count, loff_t *ppos) 23111904167SKaiGai Kohei { 23211904167SKaiGai Kohei struct page *status = filp->private_data; 23311904167SKaiGai Kohei 23411904167SKaiGai Kohei BUG_ON(!status); 23511904167SKaiGai Kohei 23611904167SKaiGai Kohei return simple_read_from_buffer(buf, count, ppos, 23711904167SKaiGai Kohei page_address(status), 23811904167SKaiGai Kohei sizeof(struct selinux_kernel_status)); 23911904167SKaiGai Kohei } 24011904167SKaiGai Kohei 24111904167SKaiGai Kohei static int sel_mmap_handle_status(struct file *filp, 24211904167SKaiGai Kohei struct vm_area_struct *vma) 24311904167SKaiGai Kohei { 24411904167SKaiGai Kohei struct page *status = filp->private_data; 24511904167SKaiGai Kohei unsigned long size = vma->vm_end - vma->vm_start; 24611904167SKaiGai Kohei 24711904167SKaiGai Kohei BUG_ON(!status); 24811904167SKaiGai Kohei 24911904167SKaiGai Kohei /* only allows one page from the head */ 25011904167SKaiGai Kohei if (vma->vm_pgoff > 0 || size != PAGE_SIZE) 25111904167SKaiGai Kohei return -EIO; 25211904167SKaiGai Kohei /* disallow writable mapping */ 25311904167SKaiGai Kohei if (vma->vm_flags & VM_WRITE) 25411904167SKaiGai Kohei return -EPERM; 25511904167SKaiGai Kohei /* disallow mprotect() turns it into writable */ 2561c71222eSSuren Baghdasaryan vm_flags_clear(vma, VM_MAYWRITE); 25711904167SKaiGai Kohei 25811904167SKaiGai Kohei return remap_pfn_range(vma, vma->vm_start, 25911904167SKaiGai Kohei page_to_pfn(status), 26011904167SKaiGai Kohei size, vma->vm_page_prot); 26111904167SKaiGai Kohei } 26211904167SKaiGai Kohei 26311904167SKaiGai Kohei static const struct file_operations sel_handle_status_ops = { 26411904167SKaiGai Kohei .open = sel_open_handle_status, 26511904167SKaiGai Kohei .read = sel_read_handle_status, 26611904167SKaiGai Kohei .mmap = sel_mmap_handle_status, 26711904167SKaiGai Kohei .llseek = generic_file_llseek, 26811904167SKaiGai Kohei }; 26911904167SKaiGai Kohei 2701da177e4SLinus Torvalds static ssize_t sel_write_disable(struct file *file, const char __user *buf, 2711da177e4SLinus Torvalds size_t count, loff_t *ppos) 2721da177e4SLinus Torvalds 2731da177e4SLinus Torvalds { 2748365a719SAl Viro char *page; 2751da177e4SLinus Torvalds ssize_t length; 2761da177e4SLinus Torvalds int new_value; 27789b223bfSPaul Moore 278bfd51626SDavi Arnaut if (count >= PAGE_SIZE) 2798365a719SAl Viro return -ENOMEM; 280b77a493bSEric Paris 2811da177e4SLinus Torvalds /* No partial writes. */ 282b77a493bSEric Paris if (*ppos != 0) 2838365a719SAl Viro return -EINVAL; 284b77a493bSEric Paris 2858365a719SAl Viro page = memdup_user_nul(buf, count); 2868365a719SAl Viro if (IS_ERR(page)) 2878365a719SAl Viro return PTR_ERR(page); 2881da177e4SLinus Torvalds 289f22f9aafSPaul Moore if (sscanf(page, "%d", &new_value) != 1) { 2901da177e4SLinus Torvalds length = -EINVAL; 2911da177e4SLinus Torvalds goto out; 292f22f9aafSPaul Moore } 293f22f9aafSPaul Moore length = count; 2941da177e4SLinus Torvalds 2951da177e4SLinus Torvalds if (new_value) { 296f22f9aafSPaul Moore pr_err("SELinux: https://github.com/SELinuxProject/selinux-kernel/wiki/DEPRECATE-runtime-disable\n"); 297f22f9aafSPaul Moore pr_err("SELinux: Runtime disable is not supported, use selinux=0 on the kernel cmdline.\n"); 2981da177e4SLinus Torvalds } 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds out: 3018365a719SAl Viro kfree(page); 3021da177e4SLinus Torvalds return length; 3031da177e4SLinus Torvalds } 3041da177e4SLinus Torvalds 3059c2e08c5SArjan van de Ven static const struct file_operations sel_disable_ops = { 3061da177e4SLinus Torvalds .write = sel_write_disable, 30757a62c23SArnd Bergmann .llseek = generic_file_llseek, 3081da177e4SLinus Torvalds }; 3091da177e4SLinus Torvalds 3101da177e4SLinus Torvalds static ssize_t sel_read_policyvers(struct file *filp, char __user *buf, 3111da177e4SLinus Torvalds size_t count, loff_t *ppos) 3121da177e4SLinus Torvalds { 3131da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 3141da177e4SLinus Torvalds ssize_t length; 3151da177e4SLinus Torvalds 3161da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX); 3171da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 3181da177e4SLinus Torvalds } 3191da177e4SLinus Torvalds 3209c2e08c5SArjan van de Ven static const struct file_operations sel_policyvers_ops = { 3211da177e4SLinus Torvalds .read = sel_read_policyvers, 32257a62c23SArnd Bergmann .llseek = generic_file_llseek, 3231da177e4SLinus Torvalds }; 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds /* declaration for sel_write_load */ 32666ec384aSDaniel Burgener static int sel_make_bools(struct selinux_policy *newpolicy, struct dentry *bool_dir, 32766ec384aSDaniel Burgener unsigned int *bool_num, char ***bool_pending_names, 328c3fae2b2SChristian Göttsche int **bool_pending_values); 32966ec384aSDaniel Burgener static int sel_make_classes(struct selinux_policy *newpolicy, 33066ec384aSDaniel Burgener struct dentry *class_dir, 33166ec384aSDaniel Burgener unsigned long *last_class_ino); 332e47c8fc5SChristopher J. PeBenito 333e47c8fc5SChristopher J. PeBenito /* declaration for sel_make_class_dirs */ 334a1c2aa1eSAl Viro static struct dentry *sel_make_dir(struct dentry *dir, const char *name, 335e47c8fc5SChristopher J. PeBenito unsigned long *ino); 3361da177e4SLinus Torvalds 3370eea6091SDaniel Burgener /* declaration for sel_make_policy_nodes */ 3380eea6091SDaniel Burgener static struct dentry *sel_make_disconnected_dir(struct super_block *sb, 3390eea6091SDaniel Burgener unsigned long *ino); 3400eea6091SDaniel Burgener 3410eea6091SDaniel Burgener /* declaration for sel_make_policy_nodes */ 342aeecf4a3SDaniel Burgener static void sel_remove_entries(struct dentry *de); 343aeecf4a3SDaniel Burgener 3441da177e4SLinus Torvalds static ssize_t sel_read_mls(struct file *filp, char __user *buf, 3451da177e4SLinus Torvalds size_t count, loff_t *ppos) 3461da177e4SLinus Torvalds { 3471da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 3481da177e4SLinus Torvalds ssize_t length; 3491da177e4SLinus Torvalds 3500719aaf5SGuido Trentalancia length = scnprintf(tmpbuf, TMPBUFLEN, "%d", 351e67b7985SStephen Smalley security_mls_enabled()); 3521da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 3531da177e4SLinus Torvalds } 3541da177e4SLinus Torvalds 3559c2e08c5SArjan van de Ven static const struct file_operations sel_mls_ops = { 3561da177e4SLinus Torvalds .read = sel_read_mls, 35757a62c23SArnd Bergmann .llseek = generic_file_llseek, 3581da177e4SLinus Torvalds }; 3591da177e4SLinus Torvalds 360cee74f47SEric Paris struct policy_load_memory { 361cee74f47SEric Paris size_t len; 362cee74f47SEric Paris void *data; 363cee74f47SEric Paris }; 364cee74f47SEric Paris 365cee74f47SEric Paris static int sel_open_policy(struct inode *inode, struct file *filp) 366cee74f47SEric Paris { 3670619f0f5SStephen Smalley struct selinux_fs_info *fsi = inode->i_sb->s_fs_info; 368cee74f47SEric Paris struct policy_load_memory *plm = NULL; 369cee74f47SEric Paris int rc; 370cee74f47SEric Paris 371cee74f47SEric Paris BUG_ON(filp->private_data); 372cee74f47SEric Paris 373e67b7985SStephen Smalley mutex_lock(&selinux_state.policy_mutex); 374cee74f47SEric Paris 375e67b7985SStephen Smalley rc = avc_has_perm(current_sid(), SECINITSID_SECURITY, 376be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL); 377cee74f47SEric Paris if (rc) 378cee74f47SEric Paris goto err; 379cee74f47SEric Paris 380cee74f47SEric Paris rc = -EBUSY; 3810619f0f5SStephen Smalley if (fsi->policy_opened) 382cee74f47SEric Paris goto err; 383cee74f47SEric Paris 384cee74f47SEric Paris rc = -ENOMEM; 385cee74f47SEric Paris plm = kzalloc(sizeof(*plm), GFP_KERNEL); 386cee74f47SEric Paris if (!plm) 387cee74f47SEric Paris goto err; 388cee74f47SEric Paris 389e67b7985SStephen Smalley rc = security_read_policy(&plm->data, &plm->len); 390cee74f47SEric Paris if (rc) 391cee74f47SEric Paris goto err; 392cee74f47SEric Paris 39366ccd256SOndrej Mosnacek if ((size_t)i_size_read(inode) != plm->len) { 39466ccd256SOndrej Mosnacek inode_lock(inode); 39566ccd256SOndrej Mosnacek i_size_write(inode, plm->len); 39666ccd256SOndrej Mosnacek inode_unlock(inode); 39766ccd256SOndrej Mosnacek } 39866ccd256SOndrej Mosnacek 3990619f0f5SStephen Smalley fsi->policy_opened = 1; 400cee74f47SEric Paris 401cee74f47SEric Paris filp->private_data = plm; 402cee74f47SEric Paris 403e67b7985SStephen Smalley mutex_unlock(&selinux_state.policy_mutex); 404cee74f47SEric Paris 405cee74f47SEric Paris return 0; 406cee74f47SEric Paris err: 407e67b7985SStephen Smalley mutex_unlock(&selinux_state.policy_mutex); 408cee74f47SEric Paris 409cee74f47SEric Paris if (plm) 410cee74f47SEric Paris vfree(plm->data); 411cee74f47SEric Paris kfree(plm); 412cee74f47SEric Paris return rc; 413cee74f47SEric Paris } 414cee74f47SEric Paris 415cee74f47SEric Paris static int sel_release_policy(struct inode *inode, struct file *filp) 416cee74f47SEric Paris { 4170619f0f5SStephen Smalley struct selinux_fs_info *fsi = inode->i_sb->s_fs_info; 418cee74f47SEric Paris struct policy_load_memory *plm = filp->private_data; 419cee74f47SEric Paris 420cee74f47SEric Paris BUG_ON(!plm); 421cee74f47SEric Paris 4220619f0f5SStephen Smalley fsi->policy_opened = 0; 423cee74f47SEric Paris 424cee74f47SEric Paris vfree(plm->data); 425cee74f47SEric Paris kfree(plm); 426cee74f47SEric Paris 427cee74f47SEric Paris return 0; 428cee74f47SEric Paris } 429cee74f47SEric Paris 430cee74f47SEric Paris static ssize_t sel_read_policy(struct file *filp, char __user *buf, 431cee74f47SEric Paris size_t count, loff_t *ppos) 432cee74f47SEric Paris { 433cee74f47SEric Paris struct policy_load_memory *plm = filp->private_data; 434cee74f47SEric Paris int ret; 435cee74f47SEric Paris 436e67b7985SStephen Smalley ret = avc_has_perm(current_sid(), SECINITSID_SECURITY, 437be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL); 438cee74f47SEric Paris if (ret) 439cee74f47SEric Paris return ret; 4400da74120SJann Horn 4410da74120SJann Horn return simple_read_from_buffer(buf, count, ppos, plm->data, plm->len); 442cee74f47SEric Paris } 443cee74f47SEric Paris 444ac9a1f6dSSouptick Joarder static vm_fault_t sel_mmap_policy_fault(struct vm_fault *vmf) 445845ca30fSEric Paris { 44611bac800SDave Jiang struct policy_load_memory *plm = vmf->vma->vm_file->private_data; 447845ca30fSEric Paris unsigned long offset; 448845ca30fSEric Paris struct page *page; 449845ca30fSEric Paris 450845ca30fSEric Paris if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE)) 451845ca30fSEric Paris return VM_FAULT_SIGBUS; 452845ca30fSEric Paris 453845ca30fSEric Paris offset = vmf->pgoff << PAGE_SHIFT; 454845ca30fSEric Paris if (offset >= roundup(plm->len, PAGE_SIZE)) 455845ca30fSEric Paris return VM_FAULT_SIGBUS; 456845ca30fSEric Paris 457845ca30fSEric Paris page = vmalloc_to_page(plm->data + offset); 458845ca30fSEric Paris get_page(page); 459845ca30fSEric Paris 460845ca30fSEric Paris vmf->page = page; 461845ca30fSEric Paris 462845ca30fSEric Paris return 0; 463845ca30fSEric Paris } 464845ca30fSEric Paris 4657cbea8dcSKirill A. Shutemov static const struct vm_operations_struct sel_mmap_policy_ops = { 466845ca30fSEric Paris .fault = sel_mmap_policy_fault, 467845ca30fSEric Paris .page_mkwrite = sel_mmap_policy_fault, 468845ca30fSEric Paris }; 469845ca30fSEric Paris 470ad3fa08cSJames Morris static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma) 471845ca30fSEric Paris { 472845ca30fSEric Paris if (vma->vm_flags & VM_SHARED) { 473845ca30fSEric Paris /* do not allow mprotect to make mapping writable */ 4741c71222eSSuren Baghdasaryan vm_flags_clear(vma, VM_MAYWRITE); 475845ca30fSEric Paris 476845ca30fSEric Paris if (vma->vm_flags & VM_WRITE) 477845ca30fSEric Paris return -EACCES; 478845ca30fSEric Paris } 479845ca30fSEric Paris 4801c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_DONTEXPAND | VM_DONTDUMP); 481845ca30fSEric Paris vma->vm_ops = &sel_mmap_policy_ops; 482845ca30fSEric Paris 483845ca30fSEric Paris return 0; 484845ca30fSEric Paris } 485845ca30fSEric Paris 486cee74f47SEric Paris static const struct file_operations sel_policy_ops = { 487cee74f47SEric Paris .open = sel_open_policy, 488cee74f47SEric Paris .read = sel_read_policy, 489845ca30fSEric Paris .mmap = sel_mmap_policy, 490cee74f47SEric Paris .release = sel_release_policy, 49147a93a5bSEric Paris .llseek = generic_file_llseek, 492cee74f47SEric Paris }; 493cee74f47SEric Paris 4940eea6091SDaniel Burgener static void sel_remove_old_bool_data(unsigned int bool_num, char **bool_names, 495c3fae2b2SChristian Göttsche int *bool_values) 496aeecf4a3SDaniel Burgener { 497aeecf4a3SDaniel Burgener u32 i; 498aeecf4a3SDaniel Burgener 499aeecf4a3SDaniel Burgener /* bool_dir cleanup */ 5000eea6091SDaniel Burgener for (i = 0; i < bool_num; i++) 5010eea6091SDaniel Burgener kfree(bool_names[i]); 5020eea6091SDaniel Burgener kfree(bool_names); 5030eea6091SDaniel Burgener kfree(bool_values); 504aeecf4a3SDaniel Burgener } 505aeecf4a3SDaniel Burgener 50602a52c5cSStephen Smalley static int sel_make_policy_nodes(struct selinux_fs_info *fsi, 50702a52c5cSStephen Smalley struct selinux_policy *newpolicy) 5080619f0f5SStephen Smalley { 5090eea6091SDaniel Burgener int ret = 0; 5100eea6091SDaniel Burgener struct dentry *tmp_parent, *tmp_bool_dir, *tmp_class_dir, *old_dentry; 5110eea6091SDaniel Burgener unsigned int tmp_bool_num, old_bool_num; 5120eea6091SDaniel Burgener char **tmp_bool_names, **old_bool_names; 513c3fae2b2SChristian Göttsche int *tmp_bool_values, *old_bool_values; 5140eea6091SDaniel Burgener unsigned long tmp_ino = fsi->last_ino; /* Don't increment last_ino in this function */ 5150619f0f5SStephen Smalley 5160eea6091SDaniel Burgener tmp_parent = sel_make_disconnected_dir(fsi->sb, &tmp_ino); 5170eea6091SDaniel Burgener if (IS_ERR(tmp_parent)) 5180eea6091SDaniel Burgener return PTR_ERR(tmp_parent); 519aeecf4a3SDaniel Burgener 5200eea6091SDaniel Burgener tmp_ino = fsi->bool_dir->d_inode->i_ino - 1; /* sel_make_dir will increment and set */ 5210eea6091SDaniel Burgener tmp_bool_dir = sel_make_dir(tmp_parent, BOOL_DIR_NAME, &tmp_ino); 5220eea6091SDaniel Burgener if (IS_ERR(tmp_bool_dir)) { 5230eea6091SDaniel Burgener ret = PTR_ERR(tmp_bool_dir); 5240eea6091SDaniel Burgener goto out; 5250619f0f5SStephen Smalley } 5260619f0f5SStephen Smalley 5270eea6091SDaniel Burgener tmp_ino = fsi->class_dir->d_inode->i_ino - 1; /* sel_make_dir will increment and set */ 5280eea6091SDaniel Burgener tmp_class_dir = sel_make_dir(tmp_parent, CLASS_DIR_NAME, &tmp_ino); 5290eea6091SDaniel Burgener if (IS_ERR(tmp_class_dir)) { 5300eea6091SDaniel Burgener ret = PTR_ERR(tmp_class_dir); 5310eea6091SDaniel Burgener goto out; 5320eea6091SDaniel Burgener } 5330eea6091SDaniel Burgener 5340eea6091SDaniel Burgener ret = sel_make_bools(newpolicy, tmp_bool_dir, &tmp_bool_num, 5350eea6091SDaniel Burgener &tmp_bool_names, &tmp_bool_values); 536ee5de60aSOndrej Mosnacek if (ret) 5370eea6091SDaniel Burgener goto out; 5380eea6091SDaniel Burgener 5390eea6091SDaniel Burgener ret = sel_make_classes(newpolicy, tmp_class_dir, 54066ec384aSDaniel Burgener &fsi->last_class_ino); 541ee5de60aSOndrej Mosnacek if (ret) 5420eea6091SDaniel Burgener goto out; 5430619f0f5SStephen Smalley 5440eea6091SDaniel Burgener /* booleans */ 5450eea6091SDaniel Burgener old_dentry = fsi->bool_dir; 5460eea6091SDaniel Burgener lock_rename(tmp_bool_dir, old_dentry); 5470eea6091SDaniel Burgener d_exchange(tmp_bool_dir, fsi->bool_dir); 5480eea6091SDaniel Burgener 5490eea6091SDaniel Burgener old_bool_num = fsi->bool_num; 5500eea6091SDaniel Burgener old_bool_names = fsi->bool_pending_names; 5510eea6091SDaniel Burgener old_bool_values = fsi->bool_pending_values; 5520eea6091SDaniel Burgener 5530eea6091SDaniel Burgener fsi->bool_num = tmp_bool_num; 5540eea6091SDaniel Burgener fsi->bool_pending_names = tmp_bool_names; 5550eea6091SDaniel Burgener fsi->bool_pending_values = tmp_bool_values; 5560eea6091SDaniel Burgener 5570eea6091SDaniel Burgener sel_remove_old_bool_data(old_bool_num, old_bool_names, old_bool_values); 5580eea6091SDaniel Burgener 5590eea6091SDaniel Burgener fsi->bool_dir = tmp_bool_dir; 5600eea6091SDaniel Burgener unlock_rename(tmp_bool_dir, old_dentry); 5610eea6091SDaniel Burgener 5620eea6091SDaniel Burgener /* classes */ 5630eea6091SDaniel Burgener old_dentry = fsi->class_dir; 5640eea6091SDaniel Burgener lock_rename(tmp_class_dir, old_dentry); 5650eea6091SDaniel Burgener d_exchange(tmp_class_dir, fsi->class_dir); 5660eea6091SDaniel Burgener fsi->class_dir = tmp_class_dir; 5670eea6091SDaniel Burgener unlock_rename(tmp_class_dir, old_dentry); 5680eea6091SDaniel Burgener 5690eea6091SDaniel Burgener out: 5700eea6091SDaniel Burgener /* Since the other temporary dirs are children of tmp_parent 5710eea6091SDaniel Burgener * this will handle all the cleanup in the case of a failure before 5720eea6091SDaniel Burgener * the swapover 5730eea6091SDaniel Burgener */ 5740eea6091SDaniel Burgener sel_remove_entries(tmp_parent); 5750eea6091SDaniel Burgener dput(tmp_parent); /* d_genocide() only handles the children */ 5760eea6091SDaniel Burgener 5770eea6091SDaniel Burgener return ret; 5780619f0f5SStephen Smalley } 5790619f0f5SStephen Smalley 5801da177e4SLinus Torvalds static ssize_t sel_write_load(struct file *file, const char __user *buf, 5811da177e4SLinus Torvalds size_t count, loff_t *ppos) 5821da177e4SLinus Torvalds 5831da177e4SLinus Torvalds { 5840619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 5856406887aSOndrej Mosnacek struct selinux_load_state load_state; 5861da177e4SLinus Torvalds ssize_t length; 5871da177e4SLinus Torvalds void *data = NULL; 5881da177e4SLinus Torvalds 589e67b7985SStephen Smalley mutex_lock(&selinux_state.policy_mutex); 5901da177e4SLinus Torvalds 591e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 592be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL); 5931da177e4SLinus Torvalds if (length) 5941da177e4SLinus Torvalds goto out; 5951da177e4SLinus Torvalds 5961da177e4SLinus Torvalds /* No partial writes. */ 5971da177e4SLinus Torvalds length = -EINVAL; 598b77a493bSEric Paris if (*ppos != 0) 5991da177e4SLinus Torvalds goto out; 6001da177e4SLinus Torvalds 601b77a493bSEric Paris length = -ENOMEM; 602b77a493bSEric Paris data = vmalloc(count); 603b77a493bSEric Paris if (!data) 604b77a493bSEric Paris goto out; 6051da177e4SLinus Torvalds 6061da177e4SLinus Torvalds length = -EFAULT; 6071da177e4SLinus Torvalds if (copy_from_user(data, buf, count) != 0) 6081da177e4SLinus Torvalds goto out; 6091da177e4SLinus Torvalds 610e67b7985SStephen Smalley length = security_load_policy(data, count, &load_state); 6114262fb51SGary Tierney if (length) { 6124262fb51SGary Tierney pr_warn_ratelimited("SELinux: failed to load policy\n"); 6131da177e4SLinus Torvalds goto out; 6144262fb51SGary Tierney } 6151da177e4SLinus Torvalds 6166406887aSOndrej Mosnacek length = sel_make_policy_nodes(fsi, load_state.policy); 61702a52c5cSStephen Smalley if (length) { 618ee5de60aSOndrej Mosnacek pr_warn_ratelimited("SELinux: failed to initialize selinuxfs\n"); 619e67b7985SStephen Smalley selinux_policy_cancel(&load_state); 620519dad3bSOndrej Mosnacek goto out; 62102a52c5cSStephen Smalley } 62202a52c5cSStephen Smalley 623e67b7985SStephen Smalley selinux_policy_commit(&load_state); 624b77a493bSEric Paris 6251da177e4SLinus Torvalds length = count; 626e47c8fc5SChristopher J. PeBenito 627cdfb6b34SRichard Guy Briggs audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_POLICY_LOAD, 628d141136fSRichard Guy Briggs "auid=%u ses=%u lsm=selinux res=1", 629581abc09SEric W. Biederman from_kuid(&init_user_ns, audit_get_loginuid(current)), 6304746ec5bSEric Paris audit_get_sessionid(current)); 6311da177e4SLinus Torvalds out: 632e67b7985SStephen Smalley mutex_unlock(&selinux_state.policy_mutex); 6331da177e4SLinus Torvalds vfree(data); 6341da177e4SLinus Torvalds return length; 6351da177e4SLinus Torvalds } 6361da177e4SLinus Torvalds 6379c2e08c5SArjan van de Ven static const struct file_operations sel_load_ops = { 6381da177e4SLinus Torvalds .write = sel_write_load, 63957a62c23SArnd Bergmann .llseek = generic_file_llseek, 6401da177e4SLinus Torvalds }; 6411da177e4SLinus Torvalds 642ce9982d0SStephen Smalley static ssize_t sel_write_context(struct file *file, char *buf, size_t size) 6431da177e4SLinus Torvalds { 644b77a493bSEric Paris char *canon = NULL; 645ce9982d0SStephen Smalley u32 sid, len; 6461da177e4SLinus Torvalds ssize_t length; 6471da177e4SLinus Torvalds 648e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 649be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__CHECK_CONTEXT, NULL); 6501da177e4SLinus Torvalds if (length) 651b77a493bSEric Paris goto out; 6521da177e4SLinus Torvalds 653e67b7985SStephen Smalley length = security_context_to_sid(buf, size, &sid, GFP_KERNEL); 654b77a493bSEric Paris if (length) 655b77a493bSEric Paris goto out; 6561da177e4SLinus Torvalds 657e67b7985SStephen Smalley length = security_sid_to_context(sid, &canon, &len); 658b77a493bSEric Paris if (length) 659b77a493bSEric Paris goto out; 660ce9982d0SStephen Smalley 661b77a493bSEric Paris length = -ERANGE; 662ce9982d0SStephen Smalley if (len > SIMPLE_TRANSACTION_LIMIT) { 663f8b69a5fSpeter enderborg pr_err("SELinux: %s: context size (%u) exceeds " 664744ba35eSEric Paris "payload max\n", __func__, len); 665ce9982d0SStephen Smalley goto out; 666ce9982d0SStephen Smalley } 667ce9982d0SStephen Smalley 668ce9982d0SStephen Smalley memcpy(buf, canon, len); 669ce9982d0SStephen Smalley length = len; 6701da177e4SLinus Torvalds out: 671ce9982d0SStephen Smalley kfree(canon); 6721da177e4SLinus Torvalds return length; 6731da177e4SLinus Torvalds } 6741da177e4SLinus Torvalds 6751da177e4SLinus Torvalds static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf, 6761da177e4SLinus Torvalds size_t count, loff_t *ppos) 6771da177e4SLinus Torvalds { 6781da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 6791da177e4SLinus Torvalds ssize_t length; 6801da177e4SLinus Torvalds 6818861d0afSLakshmi Ramasubramanian length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 682e67b7985SStephen Smalley checkreqprot_get()); 6831da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 6841da177e4SLinus Torvalds } 6851da177e4SLinus Torvalds 6861da177e4SLinus Torvalds static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf, 6871da177e4SLinus Torvalds size_t count, loff_t *ppos) 6881da177e4SLinus Torvalds { 6898365a719SAl Viro char *page; 6901da177e4SLinus Torvalds ssize_t length; 6911da177e4SLinus Torvalds unsigned int new_value; 6921da177e4SLinus Torvalds 693e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 694be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETCHECKREQPROT, 695be0554c9SStephen Smalley NULL); 6961da177e4SLinus Torvalds if (length) 6978365a719SAl Viro return length; 6981da177e4SLinus Torvalds 699bfd51626SDavi Arnaut if (count >= PAGE_SIZE) 7008365a719SAl Viro return -ENOMEM; 701b77a493bSEric Paris 7021da177e4SLinus Torvalds /* No partial writes. */ 703b77a493bSEric Paris if (*ppos != 0) 7048365a719SAl Viro return -EINVAL; 705b77a493bSEric Paris 7068365a719SAl Viro page = memdup_user_nul(buf, count); 7078365a719SAl Viro if (IS_ERR(page)) 7088365a719SAl Viro return PTR_ERR(page); 7091da177e4SLinus Torvalds 710a7e4676eSPaul Moore if (sscanf(page, "%u", &new_value) != 1) { 7111da177e4SLinus Torvalds length = -EINVAL; 7121da177e4SLinus Torvalds goto out; 713a7e4676eSPaul Moore } 714a7e4676eSPaul Moore length = count; 7151da177e4SLinus Torvalds 716e9c38f9fSStephen Smalley if (new_value) { 717e9c38f9fSStephen Smalley char comm[sizeof(current->comm)]; 718e9c38f9fSStephen Smalley 719e9c38f9fSStephen Smalley memcpy(comm, current->comm, sizeof(comm)); 720a7e4676eSPaul Moore pr_err("SELinux: %s (%d) set checkreqprot to 1. This is no longer supported.\n", 721e9c38f9fSStephen Smalley comm, current->pid); 722e9c38f9fSStephen Smalley } 723e9c38f9fSStephen Smalley 724e67b7985SStephen Smalley selinux_ima_measure_state(); 7252554a48fSLakshmi Ramasubramanian 7261da177e4SLinus Torvalds out: 7278365a719SAl Viro kfree(page); 7281da177e4SLinus Torvalds return length; 7291da177e4SLinus Torvalds } 7309c2e08c5SArjan van de Ven static const struct file_operations sel_checkreqprot_ops = { 7311da177e4SLinus Torvalds .read = sel_read_checkreqprot, 7321da177e4SLinus Torvalds .write = sel_write_checkreqprot, 73357a62c23SArnd Bergmann .llseek = generic_file_llseek, 7341da177e4SLinus Torvalds }; 7351da177e4SLinus Torvalds 736f9df6458SAndrew Perepechko static ssize_t sel_write_validatetrans(struct file *file, 737f9df6458SAndrew Perepechko const char __user *buf, 738f9df6458SAndrew Perepechko size_t count, loff_t *ppos) 739f9df6458SAndrew Perepechko { 740f9df6458SAndrew Perepechko char *oldcon = NULL, *newcon = NULL, *taskcon = NULL; 741f9df6458SAndrew Perepechko char *req = NULL; 742f9df6458SAndrew Perepechko u32 osid, nsid, tsid; 743f9df6458SAndrew Perepechko u16 tclass; 744f9df6458SAndrew Perepechko int rc; 745f9df6458SAndrew Perepechko 746e67b7985SStephen Smalley rc = avc_has_perm(current_sid(), SECINITSID_SECURITY, 747be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__VALIDATE_TRANS, NULL); 748f9df6458SAndrew Perepechko if (rc) 749f9df6458SAndrew Perepechko goto out; 750f9df6458SAndrew Perepechko 751f9df6458SAndrew Perepechko rc = -ENOMEM; 752f9df6458SAndrew Perepechko if (count >= PAGE_SIZE) 753f9df6458SAndrew Perepechko goto out; 754f9df6458SAndrew Perepechko 755f9df6458SAndrew Perepechko /* No partial writes. */ 756f9df6458SAndrew Perepechko rc = -EINVAL; 757f9df6458SAndrew Perepechko if (*ppos != 0) 758f9df6458SAndrew Perepechko goto out; 759f9df6458SAndrew Perepechko 7600b884d25SAl Viro req = memdup_user_nul(buf, count); 7610b884d25SAl Viro if (IS_ERR(req)) { 7620b884d25SAl Viro rc = PTR_ERR(req); 7630b884d25SAl Viro req = NULL; 764f9df6458SAndrew Perepechko goto out; 7650b884d25SAl Viro } 766f9df6458SAndrew Perepechko 767f9df6458SAndrew Perepechko rc = -ENOMEM; 768f9df6458SAndrew Perepechko oldcon = kzalloc(count + 1, GFP_KERNEL); 769f9df6458SAndrew Perepechko if (!oldcon) 770f9df6458SAndrew Perepechko goto out; 771f9df6458SAndrew Perepechko 772f9df6458SAndrew Perepechko newcon = kzalloc(count + 1, GFP_KERNEL); 773f9df6458SAndrew Perepechko if (!newcon) 774f9df6458SAndrew Perepechko goto out; 775f9df6458SAndrew Perepechko 776f9df6458SAndrew Perepechko taskcon = kzalloc(count + 1, GFP_KERNEL); 777f9df6458SAndrew Perepechko if (!taskcon) 778f9df6458SAndrew Perepechko goto out; 779f9df6458SAndrew Perepechko 780f9df6458SAndrew Perepechko rc = -EINVAL; 781f9df6458SAndrew Perepechko if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4) 782f9df6458SAndrew Perepechko goto out; 783f9df6458SAndrew Perepechko 784e67b7985SStephen Smalley rc = security_context_str_to_sid(oldcon, &osid, GFP_KERNEL); 785f9df6458SAndrew Perepechko if (rc) 786f9df6458SAndrew Perepechko goto out; 787f9df6458SAndrew Perepechko 788e67b7985SStephen Smalley rc = security_context_str_to_sid(newcon, &nsid, GFP_KERNEL); 789f9df6458SAndrew Perepechko if (rc) 790f9df6458SAndrew Perepechko goto out; 791f9df6458SAndrew Perepechko 792e67b7985SStephen Smalley rc = security_context_str_to_sid(taskcon, &tsid, GFP_KERNEL); 793f9df6458SAndrew Perepechko if (rc) 794f9df6458SAndrew Perepechko goto out; 795f9df6458SAndrew Perepechko 796e67b7985SStephen Smalley rc = security_validate_transition_user(osid, nsid, tsid, tclass); 797f9df6458SAndrew Perepechko if (!rc) 798f9df6458SAndrew Perepechko rc = count; 799f9df6458SAndrew Perepechko out: 800f9df6458SAndrew Perepechko kfree(req); 801f9df6458SAndrew Perepechko kfree(oldcon); 802f9df6458SAndrew Perepechko kfree(newcon); 803f9df6458SAndrew Perepechko kfree(taskcon); 804f9df6458SAndrew Perepechko return rc; 805f9df6458SAndrew Perepechko } 806f9df6458SAndrew Perepechko 807f9df6458SAndrew Perepechko static const struct file_operations sel_transition_ops = { 808f9df6458SAndrew Perepechko .write = sel_write_validatetrans, 809f9df6458SAndrew Perepechko .llseek = generic_file_llseek, 810f9df6458SAndrew Perepechko }; 811f9df6458SAndrew Perepechko 8121da177e4SLinus Torvalds /* 8131da177e4SLinus Torvalds * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c 8141da177e4SLinus Torvalds */ 8151da177e4SLinus Torvalds static ssize_t sel_write_access(struct file *file, char *buf, size_t size); 8161da177e4SLinus Torvalds static ssize_t sel_write_create(struct file *file, char *buf, size_t size); 8171da177e4SLinus Torvalds static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size); 8181da177e4SLinus Torvalds static ssize_t sel_write_user(struct file *file, char *buf, size_t size); 8191da177e4SLinus Torvalds static ssize_t sel_write_member(struct file *file, char *buf, size_t size); 8201da177e4SLinus Torvalds 821631d2b49SEric Biggers static ssize_t (*const write_op[])(struct file *, char *, size_t) = { 8221da177e4SLinus Torvalds [SEL_ACCESS] = sel_write_access, 8231da177e4SLinus Torvalds [SEL_CREATE] = sel_write_create, 8241da177e4SLinus Torvalds [SEL_RELABEL] = sel_write_relabel, 8251da177e4SLinus Torvalds [SEL_USER] = sel_write_user, 8261da177e4SLinus Torvalds [SEL_MEMBER] = sel_write_member, 827ce9982d0SStephen Smalley [SEL_CONTEXT] = sel_write_context, 8281da177e4SLinus Torvalds }; 8291da177e4SLinus Torvalds 8301da177e4SLinus Torvalds static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos) 8311da177e4SLinus Torvalds { 832496ad9aaSAl Viro ino_t ino = file_inode(file)->i_ino; 8331da177e4SLinus Torvalds char *data; 8341da177e4SLinus Torvalds ssize_t rv; 8351da177e4SLinus Torvalds 8366e20a64aSNicolas Kaiser if (ino >= ARRAY_SIZE(write_op) || !write_op[ino]) 8371da177e4SLinus Torvalds return -EINVAL; 8381da177e4SLinus Torvalds 8391da177e4SLinus Torvalds data = simple_transaction_get(file, buf, size); 8401da177e4SLinus Torvalds if (IS_ERR(data)) 8411da177e4SLinus Torvalds return PTR_ERR(data); 8421da177e4SLinus Torvalds 8431da177e4SLinus Torvalds rv = write_op[ino](file, data, size); 8441da177e4SLinus Torvalds if (rv > 0) { 8451da177e4SLinus Torvalds simple_transaction_set(file, rv); 8461da177e4SLinus Torvalds rv = size; 8471da177e4SLinus Torvalds } 8481da177e4SLinus Torvalds return rv; 8491da177e4SLinus Torvalds } 8501da177e4SLinus Torvalds 8519c2e08c5SArjan van de Ven static const struct file_operations transaction_ops = { 8521da177e4SLinus Torvalds .write = selinux_transaction_write, 8531da177e4SLinus Torvalds .read = simple_transaction_read, 8541da177e4SLinus Torvalds .release = simple_transaction_release, 85557a62c23SArnd Bergmann .llseek = generic_file_llseek, 8561da177e4SLinus Torvalds }; 8571da177e4SLinus Torvalds 8581da177e4SLinus Torvalds /* 8591da177e4SLinus Torvalds * payload - write methods 8601da177e4SLinus Torvalds * If the method has a response, the response should be put in buf, 8611da177e4SLinus Torvalds * and the length returned. Otherwise return 0 or and -error. 8621da177e4SLinus Torvalds */ 8631da177e4SLinus Torvalds 8641da177e4SLinus Torvalds static ssize_t sel_write_access(struct file *file, char *buf, size_t size) 8651da177e4SLinus Torvalds { 866b77a493bSEric Paris char *scon = NULL, *tcon = NULL; 8671da177e4SLinus Torvalds u32 ssid, tsid; 8681da177e4SLinus Torvalds u16 tclass; 8691da177e4SLinus Torvalds struct av_decision avd; 8701da177e4SLinus Torvalds ssize_t length; 8711da177e4SLinus Torvalds 872e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 873be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_AV, NULL); 8741da177e4SLinus Torvalds if (length) 875b77a493bSEric Paris goto out; 8761da177e4SLinus Torvalds 8771da177e4SLinus Torvalds length = -ENOMEM; 87889d155efSJames Morris scon = kzalloc(size + 1, GFP_KERNEL); 8791da177e4SLinus Torvalds if (!scon) 880b77a493bSEric Paris goto out; 8811da177e4SLinus Torvalds 882b77a493bSEric Paris length = -ENOMEM; 88389d155efSJames Morris tcon = kzalloc(size + 1, GFP_KERNEL); 8841da177e4SLinus Torvalds if (!tcon) 8851da177e4SLinus Torvalds goto out; 8861da177e4SLinus Torvalds 8871da177e4SLinus Torvalds length = -EINVAL; 88819439d05SStephen Smalley if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) 889b77a493bSEric Paris goto out; 8901da177e4SLinus Torvalds 891e67b7985SStephen Smalley length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL); 892b77a493bSEric Paris if (length) 893b77a493bSEric Paris goto out; 894b77a493bSEric Paris 895e67b7985SStephen Smalley length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL); 896b77a493bSEric Paris if (length) 897b77a493bSEric Paris goto out; 8981da177e4SLinus Torvalds 899e67b7985SStephen Smalley security_compute_av_user(ssid, tsid, tclass, &avd); 9001da177e4SLinus Torvalds 9011da177e4SLinus Torvalds length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, 9028a6f83afSKaiGai Kohei "%x %x %x %x %u %x", 903f1c6381aSEric Paris avd.allowed, 0xffffffff, 9041da177e4SLinus Torvalds avd.auditallow, avd.auditdeny, 9058a6f83afSKaiGai Kohei avd.seqno, avd.flags); 9061da177e4SLinus Torvalds out: 907b77a493bSEric Paris kfree(tcon); 9081da177e4SLinus Torvalds kfree(scon); 9091da177e4SLinus Torvalds return length; 9101da177e4SLinus Torvalds } 9111da177e4SLinus Torvalds 9121da177e4SLinus Torvalds static ssize_t sel_write_create(struct file *file, char *buf, size_t size) 9131da177e4SLinus Torvalds { 914b77a493bSEric Paris char *scon = NULL, *tcon = NULL; 915f50a3ec9SKohei Kaigai char *namebuf = NULL, *objname = NULL; 9161da177e4SLinus Torvalds u32 ssid, tsid, newsid; 9171da177e4SLinus Torvalds u16 tclass; 9181da177e4SLinus Torvalds ssize_t length; 919b77a493bSEric Paris char *newcon = NULL; 9201da177e4SLinus Torvalds u32 len; 921f50a3ec9SKohei Kaigai int nargs; 9221da177e4SLinus Torvalds 923e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 924be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_CREATE, 925be0554c9SStephen Smalley NULL); 9261da177e4SLinus Torvalds if (length) 927b77a493bSEric Paris goto out; 9281da177e4SLinus Torvalds 9291da177e4SLinus Torvalds length = -ENOMEM; 93089d155efSJames Morris scon = kzalloc(size + 1, GFP_KERNEL); 9311da177e4SLinus Torvalds if (!scon) 932b77a493bSEric Paris goto out; 9331da177e4SLinus Torvalds 934b77a493bSEric Paris length = -ENOMEM; 93589d155efSJames Morris tcon = kzalloc(size + 1, GFP_KERNEL); 9361da177e4SLinus Torvalds if (!tcon) 9371da177e4SLinus Torvalds goto out; 9381da177e4SLinus Torvalds 939f50a3ec9SKohei Kaigai length = -ENOMEM; 940f50a3ec9SKohei Kaigai namebuf = kzalloc(size + 1, GFP_KERNEL); 941f50a3ec9SKohei Kaigai if (!namebuf) 942b77a493bSEric Paris goto out; 9431da177e4SLinus Torvalds 944f50a3ec9SKohei Kaigai length = -EINVAL; 945f50a3ec9SKohei Kaigai nargs = sscanf(buf, "%s %s %hu %s", scon, tcon, &tclass, namebuf); 946f50a3ec9SKohei Kaigai if (nargs < 3 || nargs > 4) 947f50a3ec9SKohei Kaigai goto out; 9480f7e4c33SKohei Kaigai if (nargs == 4) { 9490f7e4c33SKohei Kaigai /* 9500f7e4c33SKohei Kaigai * If and when the name of new object to be queried contains 9510f7e4c33SKohei Kaigai * either whitespace or multibyte characters, they shall be 9520f7e4c33SKohei Kaigai * encoded based on the percentage-encoding rule. 9530f7e4c33SKohei Kaigai * If not encoded, the sscanf logic picks up only left-half 9543d9047a0SChristian Göttsche * of the supplied name; split by a whitespace unexpectedly. 9550f7e4c33SKohei Kaigai */ 9560f7e4c33SKohei Kaigai char *r, *w; 9570f7e4c33SKohei Kaigai int c1, c2; 9580f7e4c33SKohei Kaigai 9590f7e4c33SKohei Kaigai r = w = namebuf; 9600f7e4c33SKohei Kaigai do { 9610f7e4c33SKohei Kaigai c1 = *r++; 9620f7e4c33SKohei Kaigai if (c1 == '+') 9630f7e4c33SKohei Kaigai c1 = ' '; 9640f7e4c33SKohei Kaigai else if (c1 == '%') { 965af7ff2c2SAndy Shevchenko c1 = hex_to_bin(*r++); 966af7ff2c2SAndy Shevchenko if (c1 < 0) 9670f7e4c33SKohei Kaigai goto out; 968af7ff2c2SAndy Shevchenko c2 = hex_to_bin(*r++); 969af7ff2c2SAndy Shevchenko if (c2 < 0) 9700f7e4c33SKohei Kaigai goto out; 9710f7e4c33SKohei Kaigai c1 = (c1 << 4) | c2; 9720f7e4c33SKohei Kaigai } 9730f7e4c33SKohei Kaigai *w++ = c1; 9740f7e4c33SKohei Kaigai } while (c1 != '\0'); 9750f7e4c33SKohei Kaigai 976f50a3ec9SKohei Kaigai objname = namebuf; 9770f7e4c33SKohei Kaigai } 978f50a3ec9SKohei Kaigai 979e67b7985SStephen Smalley length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL); 980b77a493bSEric Paris if (length) 981b77a493bSEric Paris goto out; 982b77a493bSEric Paris 983e67b7985SStephen Smalley length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL); 984b77a493bSEric Paris if (length) 985b77a493bSEric Paris goto out; 9861da177e4SLinus Torvalds 987e67b7985SStephen Smalley length = security_transition_sid_user(ssid, tsid, tclass, 9880619f0f5SStephen Smalley objname, &newsid); 989b77a493bSEric Paris if (length) 990b77a493bSEric Paris goto out; 9911da177e4SLinus Torvalds 992e67b7985SStephen Smalley length = security_sid_to_context(newsid, &newcon, &len); 993b77a493bSEric Paris if (length) 994b77a493bSEric Paris goto out; 9951da177e4SLinus Torvalds 996b77a493bSEric Paris length = -ERANGE; 9971da177e4SLinus Torvalds if (len > SIMPLE_TRANSACTION_LIMIT) { 998f8b69a5fSpeter enderborg pr_err("SELinux: %s: context size (%u) exceeds " 999744ba35eSEric Paris "payload max\n", __func__, len); 1000b77a493bSEric Paris goto out; 10011da177e4SLinus Torvalds } 10021da177e4SLinus Torvalds 10031da177e4SLinus Torvalds memcpy(buf, newcon, len); 10041da177e4SLinus Torvalds length = len; 10051da177e4SLinus Torvalds out: 1006b77a493bSEric Paris kfree(newcon); 1007f50a3ec9SKohei Kaigai kfree(namebuf); 1008b77a493bSEric Paris kfree(tcon); 10091da177e4SLinus Torvalds kfree(scon); 10101da177e4SLinus Torvalds return length; 10111da177e4SLinus Torvalds } 10121da177e4SLinus Torvalds 10131da177e4SLinus Torvalds static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size) 10141da177e4SLinus Torvalds { 1015b77a493bSEric Paris char *scon = NULL, *tcon = NULL; 10161da177e4SLinus Torvalds u32 ssid, tsid, newsid; 10171da177e4SLinus Torvalds u16 tclass; 10181da177e4SLinus Torvalds ssize_t length; 1019b77a493bSEric Paris char *newcon = NULL; 10201da177e4SLinus Torvalds u32 len; 10211da177e4SLinus Torvalds 1022e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 1023be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_RELABEL, 1024be0554c9SStephen Smalley NULL); 10251da177e4SLinus Torvalds if (length) 1026b77a493bSEric Paris goto out; 10271da177e4SLinus Torvalds 10281da177e4SLinus Torvalds length = -ENOMEM; 102989d155efSJames Morris scon = kzalloc(size + 1, GFP_KERNEL); 10301da177e4SLinus Torvalds if (!scon) 1031b77a493bSEric Paris goto out; 10321da177e4SLinus Torvalds 1033b77a493bSEric Paris length = -ENOMEM; 103489d155efSJames Morris tcon = kzalloc(size + 1, GFP_KERNEL); 10351da177e4SLinus Torvalds if (!tcon) 10361da177e4SLinus Torvalds goto out; 10371da177e4SLinus Torvalds 10381da177e4SLinus Torvalds length = -EINVAL; 10391da177e4SLinus Torvalds if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) 1040b77a493bSEric Paris goto out; 10411da177e4SLinus Torvalds 1042e67b7985SStephen Smalley length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL); 1043b77a493bSEric Paris if (length) 1044b77a493bSEric Paris goto out; 1045b77a493bSEric Paris 1046e67b7985SStephen Smalley length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL); 1047b77a493bSEric Paris if (length) 1048b77a493bSEric Paris goto out; 10491da177e4SLinus Torvalds 1050e67b7985SStephen Smalley length = security_change_sid(ssid, tsid, tclass, &newsid); 1051b77a493bSEric Paris if (length) 1052b77a493bSEric Paris goto out; 10531da177e4SLinus Torvalds 1054e67b7985SStephen Smalley length = security_sid_to_context(newsid, &newcon, &len); 1055b77a493bSEric Paris if (length) 1056b77a493bSEric Paris goto out; 10571da177e4SLinus Torvalds 10581da177e4SLinus Torvalds length = -ERANGE; 1059b77a493bSEric Paris if (len > SIMPLE_TRANSACTION_LIMIT) 1060b77a493bSEric Paris goto out; 10611da177e4SLinus Torvalds 10621da177e4SLinus Torvalds memcpy(buf, newcon, len); 10631da177e4SLinus Torvalds length = len; 10641da177e4SLinus Torvalds out: 1065b77a493bSEric Paris kfree(newcon); 1066b77a493bSEric Paris kfree(tcon); 10671da177e4SLinus Torvalds kfree(scon); 10681da177e4SLinus Torvalds return length; 10691da177e4SLinus Torvalds } 10701da177e4SLinus Torvalds 10711da177e4SLinus Torvalds static ssize_t sel_write_user(struct file *file, char *buf, size_t size) 10721da177e4SLinus Torvalds { 1073b77a493bSEric Paris char *con = NULL, *user = NULL, *ptr; 1074b77a493bSEric Paris u32 sid, *sids = NULL; 10751da177e4SLinus Torvalds ssize_t length; 10761da177e4SLinus Torvalds char *newcon; 10771da177e4SLinus Torvalds int i, rc; 10781da177e4SLinus Torvalds u32 len, nsids; 10791da177e4SLinus Torvalds 1080e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 1081be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_USER, 1082be0554c9SStephen Smalley NULL); 10831da177e4SLinus Torvalds if (length) 10846eab04a8SJustin P. Mattock goto out; 10851da177e4SLinus Torvalds 10861da177e4SLinus Torvalds length = -ENOMEM; 108789d155efSJames Morris con = kzalloc(size + 1, GFP_KERNEL); 10881da177e4SLinus Torvalds if (!con) 10896eab04a8SJustin P. Mattock goto out; 10901da177e4SLinus Torvalds 1091b77a493bSEric Paris length = -ENOMEM; 109289d155efSJames Morris user = kzalloc(size + 1, GFP_KERNEL); 10931da177e4SLinus Torvalds if (!user) 10941da177e4SLinus Torvalds goto out; 10951da177e4SLinus Torvalds 10961da177e4SLinus Torvalds length = -EINVAL; 10971da177e4SLinus Torvalds if (sscanf(buf, "%s %s", con, user) != 2) 1098b77a493bSEric Paris goto out; 10991da177e4SLinus Torvalds 1100e67b7985SStephen Smalley length = security_context_str_to_sid(con, &sid, GFP_KERNEL); 1101b77a493bSEric Paris if (length) 1102b77a493bSEric Paris goto out; 11031da177e4SLinus Torvalds 1104e67b7985SStephen Smalley length = security_get_user_sids(sid, user, &sids, &nsids); 1105b77a493bSEric Paris if (length) 1106b77a493bSEric Paris goto out; 11071da177e4SLinus Torvalds 11081da177e4SLinus Torvalds length = sprintf(buf, "%u", nsids) + 1; 11091da177e4SLinus Torvalds ptr = buf + length; 11101da177e4SLinus Torvalds for (i = 0; i < nsids; i++) { 1111e67b7985SStephen Smalley rc = security_sid_to_context(sids[i], &newcon, &len); 11121da177e4SLinus Torvalds if (rc) { 11131da177e4SLinus Torvalds length = rc; 1114b77a493bSEric Paris goto out; 11151da177e4SLinus Torvalds } 11161da177e4SLinus Torvalds if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) { 11171da177e4SLinus Torvalds kfree(newcon); 11181da177e4SLinus Torvalds length = -ERANGE; 1119b77a493bSEric Paris goto out; 11201da177e4SLinus Torvalds } 11211da177e4SLinus Torvalds memcpy(ptr, newcon, len); 11221da177e4SLinus Torvalds kfree(newcon); 11231da177e4SLinus Torvalds ptr += len; 11241da177e4SLinus Torvalds length += len; 11251da177e4SLinus Torvalds } 11261da177e4SLinus Torvalds out: 1127b77a493bSEric Paris kfree(sids); 1128b77a493bSEric Paris kfree(user); 11291da177e4SLinus Torvalds kfree(con); 11301da177e4SLinus Torvalds return length; 11311da177e4SLinus Torvalds } 11321da177e4SLinus Torvalds 11331da177e4SLinus Torvalds static ssize_t sel_write_member(struct file *file, char *buf, size_t size) 11341da177e4SLinus Torvalds { 1135b77a493bSEric Paris char *scon = NULL, *tcon = NULL; 11361da177e4SLinus Torvalds u32 ssid, tsid, newsid; 11371da177e4SLinus Torvalds u16 tclass; 11381da177e4SLinus Torvalds ssize_t length; 1139b77a493bSEric Paris char *newcon = NULL; 11401da177e4SLinus Torvalds u32 len; 11411da177e4SLinus Torvalds 1142e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 1143be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_MEMBER, 1144be0554c9SStephen Smalley NULL); 11451da177e4SLinus Torvalds if (length) 1146b77a493bSEric Paris goto out; 11471da177e4SLinus Torvalds 11481da177e4SLinus Torvalds length = -ENOMEM; 114989d155efSJames Morris scon = kzalloc(size + 1, GFP_KERNEL); 11501da177e4SLinus Torvalds if (!scon) 11516eab04a8SJustin P. Mattock goto out; 11521da177e4SLinus Torvalds 1153b77a493bSEric Paris length = -ENOMEM; 115489d155efSJames Morris tcon = kzalloc(size + 1, GFP_KERNEL); 11551da177e4SLinus Torvalds if (!tcon) 11561da177e4SLinus Torvalds goto out; 11571da177e4SLinus Torvalds 11581da177e4SLinus Torvalds length = -EINVAL; 11591da177e4SLinus Torvalds if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) 1160b77a493bSEric Paris goto out; 11611da177e4SLinus Torvalds 1162e67b7985SStephen Smalley length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL); 1163b77a493bSEric Paris if (length) 1164b77a493bSEric Paris goto out; 1165b77a493bSEric Paris 1166e67b7985SStephen Smalley length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL); 1167b77a493bSEric Paris if (length) 1168b77a493bSEric Paris goto out; 11691da177e4SLinus Torvalds 1170e67b7985SStephen Smalley length = security_member_sid(ssid, tsid, tclass, &newsid); 1171b77a493bSEric Paris if (length) 1172b77a493bSEric Paris goto out; 11731da177e4SLinus Torvalds 1174e67b7985SStephen Smalley length = security_sid_to_context(newsid, &newcon, &len); 1175b77a493bSEric Paris if (length) 1176b77a493bSEric Paris goto out; 11771da177e4SLinus Torvalds 1178b77a493bSEric Paris length = -ERANGE; 11791da177e4SLinus Torvalds if (len > SIMPLE_TRANSACTION_LIMIT) { 1180f8b69a5fSpeter enderborg pr_err("SELinux: %s: context size (%u) exceeds " 1181744ba35eSEric Paris "payload max\n", __func__, len); 1182b77a493bSEric Paris goto out; 11831da177e4SLinus Torvalds } 11841da177e4SLinus Torvalds 11851da177e4SLinus Torvalds memcpy(buf, newcon, len); 11861da177e4SLinus Torvalds length = len; 11871da177e4SLinus Torvalds out: 1188b77a493bSEric Paris kfree(newcon); 1189b77a493bSEric Paris kfree(tcon); 11901da177e4SLinus Torvalds kfree(scon); 11911da177e4SLinus Torvalds return length; 11921da177e4SLinus Torvalds } 11931da177e4SLinus Torvalds 11941da177e4SLinus Torvalds static struct inode *sel_make_inode(struct super_block *sb, int mode) 11951da177e4SLinus Torvalds { 11961da177e4SLinus Torvalds struct inode *ret = new_inode(sb); 11971da177e4SLinus Torvalds 11981da177e4SLinus Torvalds if (ret) { 11991da177e4SLinus Torvalds ret->i_mode = mode; 1200078cd827SDeepa Dinamani ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret); 12011da177e4SLinus Torvalds } 12021da177e4SLinus Torvalds return ret; 12031da177e4SLinus Torvalds } 12041da177e4SLinus Torvalds 12051da177e4SLinus Torvalds static ssize_t sel_read_bool(struct file *filep, char __user *buf, 12061da177e4SLinus Torvalds size_t count, loff_t *ppos) 12071da177e4SLinus Torvalds { 12080619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info; 12091da177e4SLinus Torvalds char *page = NULL; 12101da177e4SLinus Torvalds ssize_t length; 12111da177e4SLinus Torvalds ssize_t ret; 12121da177e4SLinus Torvalds int cur_enforcing; 1213496ad9aaSAl Viro unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK; 1214d313f948SStephen Smalley const char *name = filep->f_path.dentry->d_name.name; 12151da177e4SLinus Torvalds 1216e67b7985SStephen Smalley mutex_lock(&selinux_state.policy_mutex); 12171da177e4SLinus Torvalds 1218d313f948SStephen Smalley ret = -EINVAL; 12190619f0f5SStephen Smalley if (index >= fsi->bool_num || strcmp(name, 12200619f0f5SStephen Smalley fsi->bool_pending_names[index])) 12210da74120SJann Horn goto out_unlock; 12221da177e4SLinus Torvalds 12231da177e4SLinus Torvalds ret = -ENOMEM; 1224b77a493bSEric Paris page = (char *)get_zeroed_page(GFP_KERNEL); 1225b77a493bSEric Paris if (!page) 12260da74120SJann Horn goto out_unlock; 12271da177e4SLinus Torvalds 1228e67b7985SStephen Smalley cur_enforcing = security_get_bool_value(index); 12291da177e4SLinus Torvalds if (cur_enforcing < 0) { 12301da177e4SLinus Torvalds ret = cur_enforcing; 12310da74120SJann Horn goto out_unlock; 12321da177e4SLinus Torvalds } 12331da177e4SLinus Torvalds length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing, 12340619f0f5SStephen Smalley fsi->bool_pending_values[index]); 1235e67b7985SStephen Smalley mutex_unlock(&selinux_state.policy_mutex); 12360da74120SJann Horn ret = simple_read_from_buffer(buf, count, ppos, page, length); 12370da74120SJann Horn out_free: 12381da177e4SLinus Torvalds free_page((unsigned long)page); 12391da177e4SLinus Torvalds return ret; 12400da74120SJann Horn 12410da74120SJann Horn out_unlock: 1242e67b7985SStephen Smalley mutex_unlock(&selinux_state.policy_mutex); 12430da74120SJann Horn goto out_free; 12441da177e4SLinus Torvalds } 12451da177e4SLinus Torvalds 12461da177e4SLinus Torvalds static ssize_t sel_write_bool(struct file *filep, const char __user *buf, 12471da177e4SLinus Torvalds size_t count, loff_t *ppos) 12481da177e4SLinus Torvalds { 12490619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info; 12501da177e4SLinus Torvalds char *page = NULL; 1251d313f948SStephen Smalley ssize_t length; 12521da177e4SLinus Torvalds int new_value; 1253496ad9aaSAl Viro unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK; 1254d313f948SStephen Smalley const char *name = filep->f_path.dentry->d_name.name; 12551da177e4SLinus Torvalds 12560da74120SJann Horn if (count >= PAGE_SIZE) 12570da74120SJann Horn return -ENOMEM; 12580da74120SJann Horn 12590da74120SJann Horn /* No partial writes. */ 12600da74120SJann Horn if (*ppos != 0) 12610da74120SJann Horn return -EINVAL; 12620da74120SJann Horn 12630da74120SJann Horn page = memdup_user_nul(buf, count); 12640da74120SJann Horn if (IS_ERR(page)) 12650da74120SJann Horn return PTR_ERR(page); 12660da74120SJann Horn 1267e67b7985SStephen Smalley mutex_lock(&selinux_state.policy_mutex); 12681da177e4SLinus Torvalds 1269e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 1270be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETBOOL, 1271be0554c9SStephen Smalley NULL); 12721da177e4SLinus Torvalds if (length) 12731da177e4SLinus Torvalds goto out; 12741da177e4SLinus Torvalds 1275d313f948SStephen Smalley length = -EINVAL; 12760619f0f5SStephen Smalley if (index >= fsi->bool_num || strcmp(name, 12770619f0f5SStephen Smalley fsi->bool_pending_names[index])) 1278d313f948SStephen Smalley goto out; 1279d313f948SStephen Smalley 12801da177e4SLinus Torvalds length = -EINVAL; 12811da177e4SLinus Torvalds if (sscanf(page, "%d", &new_value) != 1) 12821da177e4SLinus Torvalds goto out; 12831da177e4SLinus Torvalds 12841da177e4SLinus Torvalds if (new_value) 12851da177e4SLinus Torvalds new_value = 1; 12861da177e4SLinus Torvalds 12870619f0f5SStephen Smalley fsi->bool_pending_values[index] = new_value; 12881da177e4SLinus Torvalds length = count; 12891da177e4SLinus Torvalds 12901da177e4SLinus Torvalds out: 1291e67b7985SStephen Smalley mutex_unlock(&selinux_state.policy_mutex); 12928365a719SAl Viro kfree(page); 12931da177e4SLinus Torvalds return length; 12941da177e4SLinus Torvalds } 12951da177e4SLinus Torvalds 12969c2e08c5SArjan van de Ven static const struct file_operations sel_bool_ops = { 12971da177e4SLinus Torvalds .read = sel_read_bool, 12981da177e4SLinus Torvalds .write = sel_write_bool, 129957a62c23SArnd Bergmann .llseek = generic_file_llseek, 13001da177e4SLinus Torvalds }; 13011da177e4SLinus Torvalds 13021da177e4SLinus Torvalds static ssize_t sel_commit_bools_write(struct file *filep, 13031da177e4SLinus Torvalds const char __user *buf, 13041da177e4SLinus Torvalds size_t count, loff_t *ppos) 13051da177e4SLinus Torvalds { 13060619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info; 13071da177e4SLinus Torvalds char *page = NULL; 1308d313f948SStephen Smalley ssize_t length; 13091da177e4SLinus Torvalds int new_value; 13101da177e4SLinus Torvalds 13110da74120SJann Horn if (count >= PAGE_SIZE) 13120da74120SJann Horn return -ENOMEM; 13130da74120SJann Horn 13140da74120SJann Horn /* No partial writes. */ 13150da74120SJann Horn if (*ppos != 0) 13160da74120SJann Horn return -EINVAL; 13170da74120SJann Horn 13180da74120SJann Horn page = memdup_user_nul(buf, count); 13190da74120SJann Horn if (IS_ERR(page)) 13200da74120SJann Horn return PTR_ERR(page); 13210da74120SJann Horn 1322e67b7985SStephen Smalley mutex_lock(&selinux_state.policy_mutex); 13231da177e4SLinus Torvalds 1324e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY, 1325be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETBOOL, 1326be0554c9SStephen Smalley NULL); 13271da177e4SLinus Torvalds if (length) 13281da177e4SLinus Torvalds goto out; 13291da177e4SLinus Torvalds 13301da177e4SLinus Torvalds length = -EINVAL; 13311da177e4SLinus Torvalds if (sscanf(page, "%d", &new_value) != 1) 13321da177e4SLinus Torvalds goto out; 13331da177e4SLinus Torvalds 1334b77a493bSEric Paris length = 0; 13350619f0f5SStephen Smalley if (new_value && fsi->bool_pending_values) 1336e67b7985SStephen Smalley length = security_set_bools(fsi->bool_num, 13370619f0f5SStephen Smalley fsi->bool_pending_values); 13381da177e4SLinus Torvalds 1339b77a493bSEric Paris if (!length) 13401da177e4SLinus Torvalds length = count; 13411da177e4SLinus Torvalds 13421da177e4SLinus Torvalds out: 1343e67b7985SStephen Smalley mutex_unlock(&selinux_state.policy_mutex); 13448365a719SAl Viro kfree(page); 13451da177e4SLinus Torvalds return length; 13461da177e4SLinus Torvalds } 13471da177e4SLinus Torvalds 13489c2e08c5SArjan van de Ven static const struct file_operations sel_commit_bools_ops = { 13491da177e4SLinus Torvalds .write = sel_commit_bools_write, 135057a62c23SArnd Bergmann .llseek = generic_file_llseek, 13511da177e4SLinus Torvalds }; 13521da177e4SLinus Torvalds 13530c92d7c7SChristopher J. PeBenito static void sel_remove_entries(struct dentry *de) 13541da177e4SLinus Torvalds { 1355ad52184bSAl Viro d_genocide(de); 1356ad52184bSAl Viro shrink_dcache_parent(de); 13571da177e4SLinus Torvalds } 13581da177e4SLinus Torvalds 135966ec384aSDaniel Burgener static int sel_make_bools(struct selinux_policy *newpolicy, struct dentry *bool_dir, 136066ec384aSDaniel Burgener unsigned int *bool_num, char ***bool_pending_names, 1361c3fae2b2SChristian Göttsche int **bool_pending_values) 13621da177e4SLinus Torvalds { 136360abd318SOndrej Mosnacek int ret; 13641da177e4SLinus Torvalds ssize_t len; 13651da177e4SLinus Torvalds struct dentry *dentry = NULL; 13661da177e4SLinus Torvalds struct inode *inode = NULL; 13671da177e4SLinus Torvalds struct inode_security_struct *isec; 13681da177e4SLinus Torvalds char **names = NULL, *page; 136960abd318SOndrej Mosnacek u32 i, num; 13701da177e4SLinus Torvalds int *values = NULL; 13711da177e4SLinus Torvalds u32 sid; 13721da177e4SLinus Torvalds 1373b77a493bSEric Paris ret = -ENOMEM; 13741872981bSEric Paris page = (char *)get_zeroed_page(GFP_KERNEL); 13751872981bSEric Paris if (!page) 1376b77a493bSEric Paris goto out; 13771da177e4SLinus Torvalds 137802a52c5cSStephen Smalley ret = security_get_bools(newpolicy, &num, &names, &values); 1379b77a493bSEric Paris if (ret) 13801da177e4SLinus Torvalds goto out; 13811da177e4SLinus Torvalds 13821da177e4SLinus Torvalds for (i = 0; i < num; i++) { 1383b77a493bSEric Paris ret = -ENOMEM; 138466ec384aSDaniel Burgener dentry = d_alloc_name(bool_dir, names[i]); 1385b77a493bSEric Paris if (!dentry) 1386b77a493bSEric Paris goto out; 13871da177e4SLinus Torvalds 1388b77a493bSEric Paris ret = -ENOMEM; 138966ec384aSDaniel Burgener inode = sel_make_inode(bool_dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR); 13907e4237faSnixiaoming if (!inode) { 13917e4237faSnixiaoming dput(dentry); 1392b77a493bSEric Paris goto out; 13937e4237faSnixiaoming } 1394b77a493bSEric Paris 13951da177e4SLinus Torvalds ret = -ENAMETOOLONG; 1396cc1dad71SAl Viro len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]); 13977e4237faSnixiaoming if (len >= PAGE_SIZE) { 13987e4237faSnixiaoming dput(dentry); 13997e4237faSnixiaoming iput(inode); 1400b77a493bSEric Paris goto out; 14017e4237faSnixiaoming } 1402b77a493bSEric Paris 140380788c22SCasey Schaufler isec = selinux_inode(inode); 140402a52c5cSStephen Smalley ret = selinux_policy_genfs_sid(newpolicy, "selinuxfs", page, 1405aa8e712cSStephen Smalley SECCLASS_FILE, &sid); 14064262fb51SGary Tierney if (ret) { 1407900fde06SGary Tierney pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n", 1408900fde06SGary Tierney page); 1409900fde06SGary Tierney sid = SECINITSID_SECURITY; 14104262fb51SGary Tierney } 14114262fb51SGary Tierney 14121da177e4SLinus Torvalds isec->sid = sid; 141342059112SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 14141da177e4SLinus Torvalds inode->i_fop = &sel_bool_ops; 1415bce34bc0SJames Carter inode->i_ino = i|SEL_BOOL_INO_OFFSET; 14161da177e4SLinus Torvalds d_add(dentry, inode); 14171da177e4SLinus Torvalds } 141866ec384aSDaniel Burgener *bool_num = num; 141966ec384aSDaniel Burgener *bool_pending_names = names; 142066ec384aSDaniel Burgener *bool_pending_values = values; 1421b77a493bSEric Paris 1422b77a493bSEric Paris free_page((unsigned long)page); 1423b77a493bSEric Paris return 0; 14241da177e4SLinus Torvalds out: 14251da177e4SLinus Torvalds free_page((unsigned long)page); 1426b77a493bSEric Paris 14271da177e4SLinus Torvalds if (names) { 14289a5f04bfSJesper Juhl for (i = 0; i < num; i++) 14291da177e4SLinus Torvalds kfree(names[i]); 14301da177e4SLinus Torvalds kfree(names); 14311da177e4SLinus Torvalds } 143220c19e41SDavi Arnaut kfree(values); 143366ec384aSDaniel Burgener sel_remove_entries(bool_dir); 1434b77a493bSEric Paris 1435b77a493bSEric Paris return ret; 14361da177e4SLinus Torvalds } 14371da177e4SLinus Torvalds 14381da177e4SLinus Torvalds static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf, 14391da177e4SLinus Torvalds size_t count, loff_t *ppos) 14401da177e4SLinus Torvalds { 14411da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 14421da177e4SLinus Torvalds ssize_t length; 14431da177e4SLinus Torvalds 14446b6bc620SStephen Smalley length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 1445e67b7985SStephen Smalley avc_get_cache_threshold()); 14461da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 14471da177e4SLinus Torvalds } 14481da177e4SLinus Torvalds 14491da177e4SLinus Torvalds static ssize_t sel_write_avc_cache_threshold(struct file *file, 14501da177e4SLinus Torvalds const char __user *buf, 14511da177e4SLinus Torvalds size_t count, loff_t *ppos) 14521da177e4SLinus Torvalds 14531da177e4SLinus Torvalds { 14548365a719SAl Viro char *page; 14551da177e4SLinus Torvalds ssize_t ret; 1456309c5fadSHeinrich Schuchardt unsigned int new_value; 14571da177e4SLinus Torvalds 1458e67b7985SStephen Smalley ret = avc_has_perm(current_sid(), SECINITSID_SECURITY, 1459be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETSECPARAM, 1460be0554c9SStephen Smalley NULL); 14611da177e4SLinus Torvalds if (ret) 14628365a719SAl Viro return ret; 1463b77a493bSEric Paris 1464b77a493bSEric Paris if (count >= PAGE_SIZE) 14658365a719SAl Viro return -ENOMEM; 1466b77a493bSEric Paris 1467b77a493bSEric Paris /* No partial writes. */ 1468b77a493bSEric Paris if (*ppos != 0) 14698365a719SAl Viro return -EINVAL; 1470b77a493bSEric Paris 14718365a719SAl Viro page = memdup_user_nul(buf, count); 14728365a719SAl Viro if (IS_ERR(page)) 14738365a719SAl Viro return PTR_ERR(page); 1474b77a493bSEric Paris 1475b77a493bSEric Paris ret = -EINVAL; 1476b77a493bSEric Paris if (sscanf(page, "%u", &new_value) != 1) 1477b77a493bSEric Paris goto out; 1478b77a493bSEric Paris 1479e67b7985SStephen Smalley avc_set_cache_threshold(new_value); 1480b77a493bSEric Paris 14811da177e4SLinus Torvalds ret = count; 14821da177e4SLinus Torvalds out: 14838365a719SAl Viro kfree(page); 14841da177e4SLinus Torvalds return ret; 14851da177e4SLinus Torvalds } 14861da177e4SLinus Torvalds 14871da177e4SLinus Torvalds static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf, 14881da177e4SLinus Torvalds size_t count, loff_t *ppos) 14891da177e4SLinus Torvalds { 14901da177e4SLinus Torvalds char *page; 1491b77a493bSEric Paris ssize_t length; 14921da177e4SLinus Torvalds 14931da177e4SLinus Torvalds page = (char *)__get_free_page(GFP_KERNEL); 1494b77a493bSEric Paris if (!page) 1495b77a493bSEric Paris return -ENOMEM; 1496b77a493bSEric Paris 1497e67b7985SStephen Smalley length = avc_get_hash_stats(page); 1498b77a493bSEric Paris if (length >= 0) 1499b77a493bSEric Paris length = simple_read_from_buffer(buf, count, ppos, page, length); 15001da177e4SLinus Torvalds free_page((unsigned long)page); 1501b77a493bSEric Paris 1502b77a493bSEric Paris return length; 15031da177e4SLinus Torvalds } 15041da177e4SLinus Torvalds 150566f8e2f0SJeff Vander Stoep static ssize_t sel_read_sidtab_hash_stats(struct file *filp, char __user *buf, 150666f8e2f0SJeff Vander Stoep size_t count, loff_t *ppos) 150766f8e2f0SJeff Vander Stoep { 150866f8e2f0SJeff Vander Stoep char *page; 150966f8e2f0SJeff Vander Stoep ssize_t length; 151066f8e2f0SJeff Vander Stoep 151166f8e2f0SJeff Vander Stoep page = (char *)__get_free_page(GFP_KERNEL); 151266f8e2f0SJeff Vander Stoep if (!page) 151366f8e2f0SJeff Vander Stoep return -ENOMEM; 151466f8e2f0SJeff Vander Stoep 1515e67b7985SStephen Smalley length = security_sidtab_hash_stats(page); 151666f8e2f0SJeff Vander Stoep if (length >= 0) 151766f8e2f0SJeff Vander Stoep length = simple_read_from_buffer(buf, count, ppos, page, 151866f8e2f0SJeff Vander Stoep length); 151966f8e2f0SJeff Vander Stoep free_page((unsigned long)page); 152066f8e2f0SJeff Vander Stoep 152166f8e2f0SJeff Vander Stoep return length; 152266f8e2f0SJeff Vander Stoep } 152366f8e2f0SJeff Vander Stoep 152466f8e2f0SJeff Vander Stoep static const struct file_operations sel_sidtab_hash_stats_ops = { 152566f8e2f0SJeff Vander Stoep .read = sel_read_sidtab_hash_stats, 152666f8e2f0SJeff Vander Stoep .llseek = generic_file_llseek, 152766f8e2f0SJeff Vander Stoep }; 152866f8e2f0SJeff Vander Stoep 15299c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_threshold_ops = { 15301da177e4SLinus Torvalds .read = sel_read_avc_cache_threshold, 15311da177e4SLinus Torvalds .write = sel_write_avc_cache_threshold, 153257a62c23SArnd Bergmann .llseek = generic_file_llseek, 15331da177e4SLinus Torvalds }; 15341da177e4SLinus Torvalds 15359c2e08c5SArjan van de Ven static const struct file_operations sel_avc_hash_stats_ops = { 15361da177e4SLinus Torvalds .read = sel_read_avc_hash_stats, 153757a62c23SArnd Bergmann .llseek = generic_file_llseek, 15381da177e4SLinus Torvalds }; 15391da177e4SLinus Torvalds 15401da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS 15411da177e4SLinus Torvalds static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx) 15421da177e4SLinus Torvalds { 15431da177e4SLinus Torvalds int cpu; 15441da177e4SLinus Torvalds 15454f4b6c1aSRusty Russell for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) { 15461da177e4SLinus Torvalds if (!cpu_possible(cpu)) 15471da177e4SLinus Torvalds continue; 15481da177e4SLinus Torvalds *idx = cpu + 1; 15491da177e4SLinus Torvalds return &per_cpu(avc_cache_stats, cpu); 15501da177e4SLinus Torvalds } 15518d269a8eSVasily Averin (*idx)++; 15521da177e4SLinus Torvalds return NULL; 15531da177e4SLinus Torvalds } 15541da177e4SLinus Torvalds 15551da177e4SLinus Torvalds static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos) 15561da177e4SLinus Torvalds { 15571da177e4SLinus Torvalds loff_t n = *pos - 1; 15581da177e4SLinus Torvalds 15591da177e4SLinus Torvalds if (*pos == 0) 15601da177e4SLinus Torvalds return SEQ_START_TOKEN; 15611da177e4SLinus Torvalds 15621da177e4SLinus Torvalds return sel_avc_get_stat_idx(&n); 15631da177e4SLinus Torvalds } 15641da177e4SLinus Torvalds 15651da177e4SLinus Torvalds static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos) 15661da177e4SLinus Torvalds { 15671da177e4SLinus Torvalds return sel_avc_get_stat_idx(pos); 15681da177e4SLinus Torvalds } 15691da177e4SLinus Torvalds 15701da177e4SLinus Torvalds static int sel_avc_stats_seq_show(struct seq_file *seq, void *v) 15711da177e4SLinus Torvalds { 15721da177e4SLinus Torvalds struct avc_cache_stats *st = v; 15731da177e4SLinus Torvalds 1574710a0647SMarkus Elfring if (v == SEQ_START_TOKEN) { 1575710a0647SMarkus Elfring seq_puts(seq, 1576710a0647SMarkus Elfring "lookups hits misses allocations reclaims frees\n"); 1577710a0647SMarkus Elfring } else { 1578257313b2SLinus Torvalds unsigned int lookups = st->lookups; 1579257313b2SLinus Torvalds unsigned int misses = st->misses; 1580257313b2SLinus Torvalds unsigned int hits = lookups - misses; 1581257313b2SLinus Torvalds seq_printf(seq, "%u %u %u %u %u %u\n", lookups, 1582257313b2SLinus Torvalds hits, misses, st->allocations, 15831da177e4SLinus Torvalds st->reclaims, st->frees); 1584257313b2SLinus Torvalds } 15851da177e4SLinus Torvalds return 0; 15861da177e4SLinus Torvalds } 15871da177e4SLinus Torvalds 15881da177e4SLinus Torvalds static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v) 15891da177e4SLinus Torvalds { } 15901da177e4SLinus Torvalds 15911996a109SJan Engelhardt static const struct seq_operations sel_avc_cache_stats_seq_ops = { 15921da177e4SLinus Torvalds .start = sel_avc_stats_seq_start, 15931da177e4SLinus Torvalds .next = sel_avc_stats_seq_next, 15941da177e4SLinus Torvalds .show = sel_avc_stats_seq_show, 15951da177e4SLinus Torvalds .stop = sel_avc_stats_seq_stop, 15961da177e4SLinus Torvalds }; 15971da177e4SLinus Torvalds 15981da177e4SLinus Torvalds static int sel_open_avc_cache_stats(struct inode *inode, struct file *file) 15991da177e4SLinus Torvalds { 16001da177e4SLinus Torvalds return seq_open(file, &sel_avc_cache_stats_seq_ops); 16011da177e4SLinus Torvalds } 16021da177e4SLinus Torvalds 16039c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_stats_ops = { 16041da177e4SLinus Torvalds .open = sel_open_avc_cache_stats, 16051da177e4SLinus Torvalds .read = seq_read, 16061da177e4SLinus Torvalds .llseek = seq_lseek, 16071da177e4SLinus Torvalds .release = seq_release, 16081da177e4SLinus Torvalds }; 16091da177e4SLinus Torvalds #endif 16101da177e4SLinus Torvalds 16111da177e4SLinus Torvalds static int sel_make_avc_files(struct dentry *dir) 16121da177e4SLinus Torvalds { 16130619f0f5SStephen Smalley struct super_block *sb = dir->d_sb; 16140619f0f5SStephen Smalley struct selinux_fs_info *fsi = sb->s_fs_info; 1615b77a493bSEric Paris int i; 1616cda37124SEric Biggers static const struct tree_descr files[] = { 16171da177e4SLinus Torvalds { "cache_threshold", 16181da177e4SLinus Torvalds &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR }, 16191da177e4SLinus Torvalds { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO }, 16201da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS 16211da177e4SLinus Torvalds { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO }, 16221da177e4SLinus Torvalds #endif 16231da177e4SLinus Torvalds }; 16241da177e4SLinus Torvalds 16256e20a64aSNicolas Kaiser for (i = 0; i < ARRAY_SIZE(files); i++) { 16261da177e4SLinus Torvalds struct inode *inode; 16271da177e4SLinus Torvalds struct dentry *dentry; 16281da177e4SLinus Torvalds 16291da177e4SLinus Torvalds dentry = d_alloc_name(dir, files[i].name); 1630b77a493bSEric Paris if (!dentry) 1631b77a493bSEric Paris return -ENOMEM; 16321da177e4SLinus Torvalds 16331da177e4SLinus Torvalds inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode); 16347e4237faSnixiaoming if (!inode) { 16357e4237faSnixiaoming dput(dentry); 1636b77a493bSEric Paris return -ENOMEM; 16377e4237faSnixiaoming } 1638b77a493bSEric Paris 16391da177e4SLinus Torvalds inode->i_fop = files[i].ops; 16400619f0f5SStephen Smalley inode->i_ino = ++fsi->last_ino; 16411da177e4SLinus Torvalds d_add(dentry, inode); 16421da177e4SLinus Torvalds } 1643b77a493bSEric Paris 1644b77a493bSEric Paris return 0; 16451da177e4SLinus Torvalds } 16461da177e4SLinus Torvalds 164766f8e2f0SJeff Vander Stoep static int sel_make_ss_files(struct dentry *dir) 164866f8e2f0SJeff Vander Stoep { 164966f8e2f0SJeff Vander Stoep struct super_block *sb = dir->d_sb; 165066f8e2f0SJeff Vander Stoep struct selinux_fs_info *fsi = sb->s_fs_info; 165166f8e2f0SJeff Vander Stoep int i; 1652*4158cb60SChristian Göttsche static const struct tree_descr files[] = { 165366f8e2f0SJeff Vander Stoep { "sidtab_hash_stats", &sel_sidtab_hash_stats_ops, S_IRUGO }, 165466f8e2f0SJeff Vander Stoep }; 165566f8e2f0SJeff Vander Stoep 165666f8e2f0SJeff Vander Stoep for (i = 0; i < ARRAY_SIZE(files); i++) { 165766f8e2f0SJeff Vander Stoep struct inode *inode; 165866f8e2f0SJeff Vander Stoep struct dentry *dentry; 165966f8e2f0SJeff Vander Stoep 166066f8e2f0SJeff Vander Stoep dentry = d_alloc_name(dir, files[i].name); 166166f8e2f0SJeff Vander Stoep if (!dentry) 166266f8e2f0SJeff Vander Stoep return -ENOMEM; 166366f8e2f0SJeff Vander Stoep 166466f8e2f0SJeff Vander Stoep inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode); 166566f8e2f0SJeff Vander Stoep if (!inode) { 166666f8e2f0SJeff Vander Stoep dput(dentry); 166766f8e2f0SJeff Vander Stoep return -ENOMEM; 166866f8e2f0SJeff Vander Stoep } 166966f8e2f0SJeff Vander Stoep 167066f8e2f0SJeff Vander Stoep inode->i_fop = files[i].ops; 167166f8e2f0SJeff Vander Stoep inode->i_ino = ++fsi->last_ino; 167266f8e2f0SJeff Vander Stoep d_add(dentry, inode); 167366f8e2f0SJeff Vander Stoep } 167466f8e2f0SJeff Vander Stoep 167566f8e2f0SJeff Vander Stoep return 0; 167666f8e2f0SJeff Vander Stoep } 167766f8e2f0SJeff Vander Stoep 1678f0ee2e46SJames Carter static ssize_t sel_read_initcon(struct file *file, char __user *buf, 1679f0ee2e46SJames Carter size_t count, loff_t *ppos) 1680f0ee2e46SJames Carter { 1681f0ee2e46SJames Carter char *con; 1682f0ee2e46SJames Carter u32 sid, len; 1683f0ee2e46SJames Carter ssize_t ret; 1684f0ee2e46SJames Carter 1685496ad9aaSAl Viro sid = file_inode(file)->i_ino&SEL_INO_MASK; 1686e67b7985SStephen Smalley ret = security_sid_to_context(sid, &con, &len); 1687b77a493bSEric Paris if (ret) 1688f0ee2e46SJames Carter return ret; 1689f0ee2e46SJames Carter 1690f0ee2e46SJames Carter ret = simple_read_from_buffer(buf, count, ppos, con, len); 1691f0ee2e46SJames Carter kfree(con); 1692f0ee2e46SJames Carter return ret; 1693f0ee2e46SJames Carter } 1694f0ee2e46SJames Carter 1695f0ee2e46SJames Carter static const struct file_operations sel_initcon_ops = { 1696f0ee2e46SJames Carter .read = sel_read_initcon, 169757a62c23SArnd Bergmann .llseek = generic_file_llseek, 1698f0ee2e46SJames Carter }; 1699f0ee2e46SJames Carter 1700f0ee2e46SJames Carter static int sel_make_initcon_files(struct dentry *dir) 1701f0ee2e46SJames Carter { 1702b77a493bSEric Paris int i; 1703f0ee2e46SJames Carter 1704f0ee2e46SJames Carter for (i = 1; i <= SECINITSID_NUM; i++) { 1705f0ee2e46SJames Carter struct inode *inode; 1706f0ee2e46SJames Carter struct dentry *dentry; 1707e3e0b582SStephen Smalley const char *s = security_get_initial_sid_context(i); 1708e3e0b582SStephen Smalley 1709e3e0b582SStephen Smalley if (!s) 1710e3e0b582SStephen Smalley continue; 1711e3e0b582SStephen Smalley dentry = d_alloc_name(dir, s); 1712b77a493bSEric Paris if (!dentry) 1713b77a493bSEric Paris return -ENOMEM; 1714f0ee2e46SJames Carter 1715f0ee2e46SJames Carter inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); 17167e4237faSnixiaoming if (!inode) { 17177e4237faSnixiaoming dput(dentry); 1718b77a493bSEric Paris return -ENOMEM; 17197e4237faSnixiaoming } 1720b77a493bSEric Paris 1721f0ee2e46SJames Carter inode->i_fop = &sel_initcon_ops; 1722f0ee2e46SJames Carter inode->i_ino = i|SEL_INITCON_INO_OFFSET; 1723f0ee2e46SJames Carter d_add(dentry, inode); 1724f0ee2e46SJames Carter } 1725b77a493bSEric Paris 1726b77a493bSEric Paris return 0; 1727f0ee2e46SJames Carter } 1728f0ee2e46SJames Carter 1729e47c8fc5SChristopher J. PeBenito static inline unsigned long sel_class_to_ino(u16 class) 1730e47c8fc5SChristopher J. PeBenito { 1731e47c8fc5SChristopher J. PeBenito return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET; 1732e47c8fc5SChristopher J. PeBenito } 1733e47c8fc5SChristopher J. PeBenito 1734e47c8fc5SChristopher J. PeBenito static inline u16 sel_ino_to_class(unsigned long ino) 1735e47c8fc5SChristopher J. PeBenito { 173692ae9e82SEric Paris return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1); 1737e47c8fc5SChristopher J. PeBenito } 1738e47c8fc5SChristopher J. PeBenito 1739e47c8fc5SChristopher J. PeBenito static inline unsigned long sel_perm_to_ino(u16 class, u32 perm) 1740e47c8fc5SChristopher J. PeBenito { 1741e47c8fc5SChristopher J. PeBenito return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET; 1742e47c8fc5SChristopher J. PeBenito } 1743e47c8fc5SChristopher J. PeBenito 1744e47c8fc5SChristopher J. PeBenito static inline u32 sel_ino_to_perm(unsigned long ino) 1745e47c8fc5SChristopher J. PeBenito { 1746e47c8fc5SChristopher J. PeBenito return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1); 1747e47c8fc5SChristopher J. PeBenito } 1748e47c8fc5SChristopher J. PeBenito 1749e47c8fc5SChristopher J. PeBenito static ssize_t sel_read_class(struct file *file, char __user *buf, 1750e47c8fc5SChristopher J. PeBenito size_t count, loff_t *ppos) 1751e47c8fc5SChristopher J. PeBenito { 1752496ad9aaSAl Viro unsigned long ino = file_inode(file)->i_ino; 1753cc1dad71SAl Viro char res[TMPBUFLEN]; 17547e78c875Sliuyang34 ssize_t len = scnprintf(res, sizeof(res), "%d", sel_ino_to_class(ino)); 1755cc1dad71SAl Viro return simple_read_from_buffer(buf, count, ppos, res, len); 1756e47c8fc5SChristopher J. PeBenito } 1757e47c8fc5SChristopher J. PeBenito 1758e47c8fc5SChristopher J. PeBenito static const struct file_operations sel_class_ops = { 1759e47c8fc5SChristopher J. PeBenito .read = sel_read_class, 176057a62c23SArnd Bergmann .llseek = generic_file_llseek, 1761e47c8fc5SChristopher J. PeBenito }; 1762e47c8fc5SChristopher J. PeBenito 1763e47c8fc5SChristopher J. PeBenito static ssize_t sel_read_perm(struct file *file, char __user *buf, 1764e47c8fc5SChristopher J. PeBenito size_t count, loff_t *ppos) 1765e47c8fc5SChristopher J. PeBenito { 1766496ad9aaSAl Viro unsigned long ino = file_inode(file)->i_ino; 1767cc1dad71SAl Viro char res[TMPBUFLEN]; 17687e78c875Sliuyang34 ssize_t len = scnprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino)); 1769cc1dad71SAl Viro return simple_read_from_buffer(buf, count, ppos, res, len); 1770e47c8fc5SChristopher J. PeBenito } 1771e47c8fc5SChristopher J. PeBenito 1772e47c8fc5SChristopher J. PeBenito static const struct file_operations sel_perm_ops = { 1773e47c8fc5SChristopher J. PeBenito .read = sel_read_perm, 177457a62c23SArnd Bergmann .llseek = generic_file_llseek, 1775e47c8fc5SChristopher J. PeBenito }; 1776e47c8fc5SChristopher J. PeBenito 17773bb56b25SPaul Moore static ssize_t sel_read_policycap(struct file *file, char __user *buf, 17783bb56b25SPaul Moore size_t count, loff_t *ppos) 17793bb56b25SPaul Moore { 17803bb56b25SPaul Moore int value; 17813bb56b25SPaul Moore char tmpbuf[TMPBUFLEN]; 17823bb56b25SPaul Moore ssize_t length; 1783496ad9aaSAl Viro unsigned long i_ino = file_inode(file)->i_ino; 17843bb56b25SPaul Moore 1785e67b7985SStephen Smalley value = security_policycap_supported(i_ino & SEL_INO_MASK); 17863bb56b25SPaul Moore length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value); 17873bb56b25SPaul Moore 17883bb56b25SPaul Moore return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 17893bb56b25SPaul Moore } 17903bb56b25SPaul Moore 17913bb56b25SPaul Moore static const struct file_operations sel_policycap_ops = { 17923bb56b25SPaul Moore .read = sel_read_policycap, 179357a62c23SArnd Bergmann .llseek = generic_file_llseek, 17943bb56b25SPaul Moore }; 17953bb56b25SPaul Moore 179602a52c5cSStephen Smalley static int sel_make_perm_files(struct selinux_policy *newpolicy, 179702a52c5cSStephen Smalley char *objclass, int classvalue, 1798e47c8fc5SChristopher J. PeBenito struct dentry *dir) 1799e47c8fc5SChristopher J. PeBenito { 1800b77a493bSEric Paris int i, rc, nperms; 1801e47c8fc5SChristopher J. PeBenito char **perms; 1802e47c8fc5SChristopher J. PeBenito 180302a52c5cSStephen Smalley rc = security_get_permissions(newpolicy, objclass, &perms, &nperms); 1804e47c8fc5SChristopher J. PeBenito if (rc) 1805b77a493bSEric Paris return rc; 1806e47c8fc5SChristopher J. PeBenito 1807e47c8fc5SChristopher J. PeBenito for (i = 0; i < nperms; i++) { 1808e47c8fc5SChristopher J. PeBenito struct inode *inode; 1809e47c8fc5SChristopher J. PeBenito struct dentry *dentry; 1810e47c8fc5SChristopher J. PeBenito 1811b77a493bSEric Paris rc = -ENOMEM; 1812e47c8fc5SChristopher J. PeBenito dentry = d_alloc_name(dir, perms[i]); 1813b77a493bSEric Paris if (!dentry) 1814b77a493bSEric Paris goto out; 1815e47c8fc5SChristopher J. PeBenito 1816e47c8fc5SChristopher J. PeBenito rc = -ENOMEM; 1817b77a493bSEric Paris inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); 18187e4237faSnixiaoming if (!inode) { 18197e4237faSnixiaoming dput(dentry); 1820b77a493bSEric Paris goto out; 18217e4237faSnixiaoming } 1822b77a493bSEric Paris 1823e47c8fc5SChristopher J. PeBenito inode->i_fop = &sel_perm_ops; 1824e47c8fc5SChristopher J. PeBenito /* i+1 since perm values are 1-indexed */ 1825e47c8fc5SChristopher J. PeBenito inode->i_ino = sel_perm_to_ino(classvalue, i + 1); 1826e47c8fc5SChristopher J. PeBenito d_add(dentry, inode); 1827e47c8fc5SChristopher J. PeBenito } 1828b77a493bSEric Paris rc = 0; 1829b77a493bSEric Paris out: 1830e47c8fc5SChristopher J. PeBenito for (i = 0; i < nperms; i++) 1831e47c8fc5SChristopher J. PeBenito kfree(perms[i]); 1832e47c8fc5SChristopher J. PeBenito kfree(perms); 1833e47c8fc5SChristopher J. PeBenito return rc; 1834e47c8fc5SChristopher J. PeBenito } 1835e47c8fc5SChristopher J. PeBenito 183602a52c5cSStephen Smalley static int sel_make_class_dir_entries(struct selinux_policy *newpolicy, 183702a52c5cSStephen Smalley char *classname, int index, 1838e47c8fc5SChristopher J. PeBenito struct dentry *dir) 1839e47c8fc5SChristopher J. PeBenito { 18400619f0f5SStephen Smalley struct super_block *sb = dir->d_sb; 18410619f0f5SStephen Smalley struct selinux_fs_info *fsi = sb->s_fs_info; 1842e47c8fc5SChristopher J. PeBenito struct dentry *dentry = NULL; 1843e47c8fc5SChristopher J. PeBenito struct inode *inode = NULL; 1844e47c8fc5SChristopher J. PeBenito 1845e47c8fc5SChristopher J. PeBenito dentry = d_alloc_name(dir, "index"); 1846b77a493bSEric Paris if (!dentry) 1847b77a493bSEric Paris return -ENOMEM; 1848e47c8fc5SChristopher J. PeBenito 1849e47c8fc5SChristopher J. PeBenito inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); 18507e4237faSnixiaoming if (!inode) { 18517e4237faSnixiaoming dput(dentry); 1852b77a493bSEric Paris return -ENOMEM; 18537e4237faSnixiaoming } 1854e47c8fc5SChristopher J. PeBenito 1855e47c8fc5SChristopher J. PeBenito inode->i_fop = &sel_class_ops; 1856e47c8fc5SChristopher J. PeBenito inode->i_ino = sel_class_to_ino(index); 1857e47c8fc5SChristopher J. PeBenito d_add(dentry, inode); 1858e47c8fc5SChristopher J. PeBenito 18590619f0f5SStephen Smalley dentry = sel_make_dir(dir, "perms", &fsi->last_class_ino); 1860a1c2aa1eSAl Viro if (IS_ERR(dentry)) 1861a1c2aa1eSAl Viro return PTR_ERR(dentry); 1862e47c8fc5SChristopher J. PeBenito 18635698f081Sye xingchen return sel_make_perm_files(newpolicy, classname, index, dentry); 1864e47c8fc5SChristopher J. PeBenito } 1865e47c8fc5SChristopher J. PeBenito 186666ec384aSDaniel Burgener static int sel_make_classes(struct selinux_policy *newpolicy, 186766ec384aSDaniel Burgener struct dentry *class_dir, 186866ec384aSDaniel Burgener unsigned long *last_class_ino) 1869e47c8fc5SChristopher J. PeBenito { 18700619f0f5SStephen Smalley 1871b77a493bSEric Paris int rc, nclasses, i; 1872e47c8fc5SChristopher J. PeBenito char **classes; 1873e47c8fc5SChristopher J. PeBenito 187402a52c5cSStephen Smalley rc = security_get_classes(newpolicy, &classes, &nclasses); 1875b77a493bSEric Paris if (rc) 1876b77a493bSEric Paris return rc; 1877e47c8fc5SChristopher J. PeBenito 1878e47c8fc5SChristopher J. PeBenito /* +2 since classes are 1-indexed */ 187966ec384aSDaniel Burgener *last_class_ino = sel_class_to_ino(nclasses + 2); 1880e47c8fc5SChristopher J. PeBenito 1881e47c8fc5SChristopher J. PeBenito for (i = 0; i < nclasses; i++) { 1882e47c8fc5SChristopher J. PeBenito struct dentry *class_name_dir; 1883e47c8fc5SChristopher J. PeBenito 188466ec384aSDaniel Burgener class_name_dir = sel_make_dir(class_dir, classes[i], 188566ec384aSDaniel Burgener last_class_ino); 1886a1c2aa1eSAl Viro if (IS_ERR(class_name_dir)) { 1887a1c2aa1eSAl Viro rc = PTR_ERR(class_name_dir); 1888b77a493bSEric Paris goto out; 1889a1c2aa1eSAl Viro } 1890e47c8fc5SChristopher J. PeBenito 1891e47c8fc5SChristopher J. PeBenito /* i+1 since class values are 1-indexed */ 189202a52c5cSStephen Smalley rc = sel_make_class_dir_entries(newpolicy, classes[i], i + 1, 1893e47c8fc5SChristopher J. PeBenito class_name_dir); 1894e47c8fc5SChristopher J. PeBenito if (rc) 1895b77a493bSEric Paris goto out; 1896e47c8fc5SChristopher J. PeBenito } 1897b77a493bSEric Paris rc = 0; 1898b77a493bSEric Paris out: 1899e47c8fc5SChristopher J. PeBenito for (i = 0; i < nclasses; i++) 1900e47c8fc5SChristopher J. PeBenito kfree(classes[i]); 1901e47c8fc5SChristopher J. PeBenito kfree(classes); 1902e47c8fc5SChristopher J. PeBenito return rc; 1903e47c8fc5SChristopher J. PeBenito } 1904e47c8fc5SChristopher J. PeBenito 19050619f0f5SStephen Smalley static int sel_make_policycap(struct selinux_fs_info *fsi) 19063bb56b25SPaul Moore { 19073bb56b25SPaul Moore unsigned int iter; 19083bb56b25SPaul Moore struct dentry *dentry = NULL; 19093bb56b25SPaul Moore struct inode *inode = NULL; 19103bb56b25SPaul Moore 1911cdbec3edSPaul Moore for (iter = 0; iter <= POLICYDB_CAP_MAX; iter++) { 19124dc2fce3SStephen Smalley if (iter < ARRAY_SIZE(selinux_policycap_names)) 19130619f0f5SStephen Smalley dentry = d_alloc_name(fsi->policycap_dir, 19144dc2fce3SStephen Smalley selinux_policycap_names[iter]); 19153bb56b25SPaul Moore else 19160619f0f5SStephen Smalley dentry = d_alloc_name(fsi->policycap_dir, "unknown"); 19173bb56b25SPaul Moore 19183bb56b25SPaul Moore if (dentry == NULL) 19193bb56b25SPaul Moore return -ENOMEM; 19203bb56b25SPaul Moore 19210619f0f5SStephen Smalley inode = sel_make_inode(fsi->sb, S_IFREG | 0444); 19227e4237faSnixiaoming if (inode == NULL) { 19237e4237faSnixiaoming dput(dentry); 19243bb56b25SPaul Moore return -ENOMEM; 19257e4237faSnixiaoming } 19263bb56b25SPaul Moore 19273bb56b25SPaul Moore inode->i_fop = &sel_policycap_ops; 19283bb56b25SPaul Moore inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET; 19293bb56b25SPaul Moore d_add(dentry, inode); 19303bb56b25SPaul Moore } 19313bb56b25SPaul Moore 19323bb56b25SPaul Moore return 0; 19333bb56b25SPaul Moore } 19343bb56b25SPaul Moore 1935a1c2aa1eSAl Viro static struct dentry *sel_make_dir(struct dentry *dir, const char *name, 19360dd4ae51SChristopher J. PeBenito unsigned long *ino) 19371da177e4SLinus Torvalds { 1938a1c2aa1eSAl Viro struct dentry *dentry = d_alloc_name(dir, name); 19391da177e4SLinus Torvalds struct inode *inode; 19401da177e4SLinus Torvalds 1941a1c2aa1eSAl Viro if (!dentry) 1942a1c2aa1eSAl Viro return ERR_PTR(-ENOMEM); 1943a1c2aa1eSAl Viro 1944a1c2aa1eSAl Viro inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO); 1945a1c2aa1eSAl Viro if (!inode) { 1946a1c2aa1eSAl Viro dput(dentry); 1947a1c2aa1eSAl Viro return ERR_PTR(-ENOMEM); 1948a1c2aa1eSAl Viro } 1949b77a493bSEric Paris 19501da177e4SLinus Torvalds inode->i_op = &simple_dir_inode_operations; 19511da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations; 19520dd4ae51SChristopher J. PeBenito inode->i_ino = ++(*ino); 195340e906f8SJames Morris /* directory inodes start off with i_nlink == 2 (for "." entry) */ 1954d8c76e6fSDave Hansen inc_nlink(inode); 19551da177e4SLinus Torvalds d_add(dentry, inode); 1956edb20fb5SJames Morris /* bump link count on parent directory, too */ 1957ce0b16ddSDavid Howells inc_nlink(d_inode(dir)); 1958b77a493bSEric Paris 1959a1c2aa1eSAl Viro return dentry; 19601da177e4SLinus Torvalds } 19611da177e4SLinus Torvalds 19620eea6091SDaniel Burgener static struct dentry *sel_make_disconnected_dir(struct super_block *sb, 19630eea6091SDaniel Burgener unsigned long *ino) 19640eea6091SDaniel Burgener { 19650eea6091SDaniel Burgener struct inode *inode = sel_make_inode(sb, S_IFDIR | S_IRUGO | S_IXUGO); 19660eea6091SDaniel Burgener 19670eea6091SDaniel Burgener if (!inode) 19680eea6091SDaniel Burgener return ERR_PTR(-ENOMEM); 19690eea6091SDaniel Burgener 19700eea6091SDaniel Burgener inode->i_op = &simple_dir_inode_operations; 19710eea6091SDaniel Burgener inode->i_fop = &simple_dir_operations; 19720eea6091SDaniel Burgener inode->i_ino = ++(*ino); 19730eea6091SDaniel Burgener /* directory inodes start off with i_nlink == 2 (for "." entry) */ 19740eea6091SDaniel Burgener inc_nlink(inode); 19750eea6091SDaniel Burgener return d_obtain_alias(inode); 19760eea6091SDaniel Burgener } 19770eea6091SDaniel Burgener 19780619f0f5SStephen Smalley #define NULL_FILE_NAME "null" 19790619f0f5SStephen Smalley 1980920f50b2SDavid Howells static int sel_fill_super(struct super_block *sb, struct fs_context *fc) 19811da177e4SLinus Torvalds { 19820619f0f5SStephen Smalley struct selinux_fs_info *fsi; 19831da177e4SLinus Torvalds int ret; 19841da177e4SLinus Torvalds struct dentry *dentry; 1985a1c2aa1eSAl Viro struct inode *inode; 19861da177e4SLinus Torvalds struct inode_security_struct *isec; 19871da177e4SLinus Torvalds 1988cda37124SEric Biggers static const struct tree_descr selinux_files[] = { 19891da177e4SLinus Torvalds [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR}, 19901da177e4SLinus Torvalds [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR}, 1991ce9982d0SStephen Smalley [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO}, 19921da177e4SLinus Torvalds [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO}, 19931da177e4SLinus Torvalds [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO}, 19941da177e4SLinus Torvalds [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO}, 19951da177e4SLinus Torvalds [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO}, 19961da177e4SLinus Torvalds [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO}, 19971da177e4SLinus Torvalds [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR}, 19981da177e4SLinus Torvalds [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO}, 19991da177e4SLinus Torvalds [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR}, 20001da177e4SLinus Torvalds [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO}, 20011da177e4SLinus Torvalds [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR}, 20023f12070eSEric Paris [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO}, 20033f12070eSEric Paris [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO}, 200411904167SKaiGai Kohei [SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO}, 200572e8c859SEric Paris [SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO}, 2006f9df6458SAndrew Perepechko [SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops, 2007f9df6458SAndrew Perepechko S_IWUGO}, 20081da177e4SLinus Torvalds /* last one */ {""} 20091da177e4SLinus Torvalds }; 20100619f0f5SStephen Smalley 20110619f0f5SStephen Smalley ret = selinux_fs_info_create(sb); 20120619f0f5SStephen Smalley if (ret) 20130619f0f5SStephen Smalley goto err; 20140619f0f5SStephen Smalley 20151da177e4SLinus Torvalds ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files); 20161da177e4SLinus Torvalds if (ret) 2017161ce45aSJames Morris goto err; 20181da177e4SLinus Torvalds 20190619f0f5SStephen Smalley fsi = sb->s_fs_info; 20200619f0f5SStephen Smalley fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino); 20210619f0f5SStephen Smalley if (IS_ERR(fsi->bool_dir)) { 20220619f0f5SStephen Smalley ret = PTR_ERR(fsi->bool_dir); 20230619f0f5SStephen Smalley fsi->bool_dir = NULL; 2024161ce45aSJames Morris goto err; 2025a1c2aa1eSAl Viro } 20261da177e4SLinus Torvalds 2027b77a493bSEric Paris ret = -ENOMEM; 20281da177e4SLinus Torvalds dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME); 2029b77a493bSEric Paris if (!dentry) 2030161ce45aSJames Morris goto err; 20311da177e4SLinus Torvalds 2032161ce45aSJames Morris ret = -ENOMEM; 2033b77a493bSEric Paris inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO); 20347e4237faSnixiaoming if (!inode) { 20357e4237faSnixiaoming dput(dentry); 2036161ce45aSJames Morris goto err; 20377e4237faSnixiaoming } 2038b77a493bSEric Paris 20390619f0f5SStephen Smalley inode->i_ino = ++fsi->last_ino; 204080788c22SCasey Schaufler isec = selinux_inode(inode); 20411da177e4SLinus Torvalds isec->sid = SECINITSID_DEVNULL; 20421da177e4SLinus Torvalds isec->sclass = SECCLASS_CHR_FILE; 204342059112SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 20441da177e4SLinus Torvalds 20451da177e4SLinus Torvalds init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3)); 20461da177e4SLinus Torvalds d_add(dentry, inode); 20471da177e4SLinus Torvalds 20480619f0f5SStephen Smalley dentry = sel_make_dir(sb->s_root, "avc", &fsi->last_ino); 2049a1c2aa1eSAl Viro if (IS_ERR(dentry)) { 2050a1c2aa1eSAl Viro ret = PTR_ERR(dentry); 2051161ce45aSJames Morris goto err; 2052a1c2aa1eSAl Viro } 20531da177e4SLinus Torvalds 20541da177e4SLinus Torvalds ret = sel_make_avc_files(dentry); 2055bcb62828SChristian Göttsche if (ret) 2056bcb62828SChristian Göttsche goto err; 205766f8e2f0SJeff Vander Stoep 205866f8e2f0SJeff Vander Stoep dentry = sel_make_dir(sb->s_root, "ss", &fsi->last_ino); 205966f8e2f0SJeff Vander Stoep if (IS_ERR(dentry)) { 206066f8e2f0SJeff Vander Stoep ret = PTR_ERR(dentry); 206166f8e2f0SJeff Vander Stoep goto err; 206266f8e2f0SJeff Vander Stoep } 206366f8e2f0SJeff Vander Stoep 206466f8e2f0SJeff Vander Stoep ret = sel_make_ss_files(dentry); 20651da177e4SLinus Torvalds if (ret) 2066161ce45aSJames Morris goto err; 2067f0ee2e46SJames Carter 20680619f0f5SStephen Smalley dentry = sel_make_dir(sb->s_root, "initial_contexts", &fsi->last_ino); 2069a1c2aa1eSAl Viro if (IS_ERR(dentry)) { 2070a1c2aa1eSAl Viro ret = PTR_ERR(dentry); 2071f0ee2e46SJames Carter goto err; 2072a1c2aa1eSAl Viro } 2073f0ee2e46SJames Carter 2074f0ee2e46SJames Carter ret = sel_make_initcon_files(dentry); 2075f0ee2e46SJames Carter if (ret) 2076f0ee2e46SJames Carter goto err; 2077f0ee2e46SJames Carter 2078613ba187SDaniel Burgener fsi->class_dir = sel_make_dir(sb->s_root, CLASS_DIR_NAME, &fsi->last_ino); 20790619f0f5SStephen Smalley if (IS_ERR(fsi->class_dir)) { 20800619f0f5SStephen Smalley ret = PTR_ERR(fsi->class_dir); 20810619f0f5SStephen Smalley fsi->class_dir = NULL; 2082e47c8fc5SChristopher J. PeBenito goto err; 2083a1c2aa1eSAl Viro } 2084e47c8fc5SChristopher J. PeBenito 2085613ba187SDaniel Burgener fsi->policycap_dir = sel_make_dir(sb->s_root, POLICYCAP_DIR_NAME, 20860619f0f5SStephen Smalley &fsi->last_ino); 20870619f0f5SStephen Smalley if (IS_ERR(fsi->policycap_dir)) { 20880619f0f5SStephen Smalley ret = PTR_ERR(fsi->policycap_dir); 20890619f0f5SStephen Smalley fsi->policycap_dir = NULL; 2090e47c8fc5SChristopher J. PeBenito goto err; 2091a1c2aa1eSAl Viro } 20920619f0f5SStephen Smalley 209302a52c5cSStephen Smalley ret = sel_make_policycap(fsi); 209402a52c5cSStephen Smalley if (ret) { 209502a52c5cSStephen Smalley pr_err("SELinux: failed to load policy capabilities\n"); 20960619f0f5SStephen Smalley goto err; 209702a52c5cSStephen Smalley } 209802a52c5cSStephen Smalley 2099b77a493bSEric Paris return 0; 2100161ce45aSJames Morris err: 2101f8b69a5fSpeter enderborg pr_err("SELinux: %s: failed while creating inodes\n", 2102744ba35eSEric Paris __func__); 21030619f0f5SStephen Smalley 21040619f0f5SStephen Smalley selinux_fs_info_free(sb); 21050619f0f5SStephen Smalley 2106b77a493bSEric Paris return ret; 21071da177e4SLinus Torvalds } 21081da177e4SLinus Torvalds 2109920f50b2SDavid Howells static int sel_get_tree(struct fs_context *fc) 21101da177e4SLinus Torvalds { 2111920f50b2SDavid Howells return get_tree_single(fc, sel_fill_super); 2112920f50b2SDavid Howells } 2113920f50b2SDavid Howells 2114920f50b2SDavid Howells static const struct fs_context_operations sel_context_ops = { 2115920f50b2SDavid Howells .get_tree = sel_get_tree, 2116920f50b2SDavid Howells }; 2117920f50b2SDavid Howells 2118920f50b2SDavid Howells static int sel_init_fs_context(struct fs_context *fc) 2119920f50b2SDavid Howells { 2120920f50b2SDavid Howells fc->ops = &sel_context_ops; 2121920f50b2SDavid Howells return 0; 21221da177e4SLinus Torvalds } 21231da177e4SLinus Torvalds 21240619f0f5SStephen Smalley static void sel_kill_sb(struct super_block *sb) 21250619f0f5SStephen Smalley { 21260619f0f5SStephen Smalley selinux_fs_info_free(sb); 21270619f0f5SStephen Smalley kill_litter_super(sb); 21280619f0f5SStephen Smalley } 21290619f0f5SStephen Smalley 21301da177e4SLinus Torvalds static struct file_system_type sel_fs_type = { 21311da177e4SLinus Torvalds .name = "selinuxfs", 2132920f50b2SDavid Howells .init_fs_context = sel_init_fs_context, 21330619f0f5SStephen Smalley .kill_sb = sel_kill_sb, 21341da177e4SLinus Torvalds }; 21351da177e4SLinus Torvalds 2136cd2bb4cbSOndrej Mosnacek static struct vfsmount *selinuxfs_mount __ro_after_init; 2137cd2bb4cbSOndrej Mosnacek struct path selinux_null __ro_after_init; 21381da177e4SLinus Torvalds 21391da177e4SLinus Torvalds static int __init init_sel_fs(void) 21401da177e4SLinus Torvalds { 21410619f0f5SStephen Smalley struct qstr null_name = QSTR_INIT(NULL_FILE_NAME, 21420619f0f5SStephen Smalley sizeof(NULL_FILE_NAME)-1); 21431da177e4SLinus Torvalds int err; 21441da177e4SLinus Torvalds 21456c5a682eSStephen Smalley if (!selinux_enabled_boot) 21461da177e4SLinus Torvalds return 0; 21477a627e3bSGreg Kroah-Hartman 2148f9bb4882SEric W. Biederman err = sysfs_create_mount_point(fs_kobj, "selinux"); 2149f9bb4882SEric W. Biederman if (err) 2150f9bb4882SEric W. Biederman return err; 21517a627e3bSGreg Kroah-Hartman 21521da177e4SLinus Torvalds err = register_filesystem(&sel_fs_type); 21537a627e3bSGreg Kroah-Hartman if (err) { 2154f9bb4882SEric W. Biederman sysfs_remove_mount_point(fs_kobj, "selinux"); 2155b77a493bSEric Paris return err; 21567a627e3bSGreg Kroah-Hartman } 2157b77a493bSEric Paris 2158765927b2SAl Viro selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type); 21591da177e4SLinus Torvalds if (IS_ERR(selinuxfs_mount)) { 2160f8b69a5fSpeter enderborg pr_err("selinuxfs: could not mount!\n"); 21611da177e4SLinus Torvalds err = PTR_ERR(selinuxfs_mount); 21621da177e4SLinus Torvalds selinuxfs_mount = NULL; 21631da177e4SLinus Torvalds } 21640619f0f5SStephen Smalley selinux_null.dentry = d_hash_and_lookup(selinux_null.mnt->mnt_root, 21650619f0f5SStephen Smalley &null_name); 21660619f0f5SStephen Smalley if (IS_ERR(selinux_null.dentry)) { 21670619f0f5SStephen Smalley pr_err("selinuxfs: could not lookup null!\n"); 21680619f0f5SStephen Smalley err = PTR_ERR(selinux_null.dentry); 21690619f0f5SStephen Smalley selinux_null.dentry = NULL; 21700619f0f5SStephen Smalley } 2171b77a493bSEric Paris 21721da177e4SLinus Torvalds return err; 21731da177e4SLinus Torvalds } 21741da177e4SLinus Torvalds 21751da177e4SLinus Torvalds __initcall(init_sel_fs); 2176