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 selinux_state *state; 810619f0f5SStephen Smalley struct super_block *sb; 820619f0f5SStephen Smalley }; 830619f0f5SStephen Smalley 840619f0f5SStephen Smalley static int selinux_fs_info_create(struct super_block *sb) 850619f0f5SStephen Smalley { 860619f0f5SStephen Smalley struct selinux_fs_info *fsi; 870619f0f5SStephen Smalley 880619f0f5SStephen Smalley fsi = kzalloc(sizeof(*fsi), GFP_KERNEL); 890619f0f5SStephen Smalley if (!fsi) 900619f0f5SStephen Smalley return -ENOMEM; 910619f0f5SStephen Smalley 920619f0f5SStephen Smalley fsi->last_ino = SEL_INO_NEXT - 1; 930619f0f5SStephen Smalley fsi->state = &selinux_state; 940619f0f5SStephen Smalley fsi->sb = sb; 950619f0f5SStephen Smalley sb->s_fs_info = fsi; 960619f0f5SStephen Smalley return 0; 970619f0f5SStephen Smalley } 980619f0f5SStephen Smalley 990619f0f5SStephen Smalley static void selinux_fs_info_free(struct super_block *sb) 1000619f0f5SStephen Smalley { 1010619f0f5SStephen Smalley struct selinux_fs_info *fsi = sb->s_fs_info; 1020619f0f5SStephen Smalley int i; 1030619f0f5SStephen Smalley 1040619f0f5SStephen Smalley if (fsi) { 1050619f0f5SStephen Smalley for (i = 0; i < fsi->bool_num; i++) 1060619f0f5SStephen Smalley kfree(fsi->bool_pending_names[i]); 1070619f0f5SStephen Smalley kfree(fsi->bool_pending_names); 1080619f0f5SStephen Smalley kfree(fsi->bool_pending_values); 1090619f0f5SStephen Smalley } 1100619f0f5SStephen Smalley kfree(sb->s_fs_info); 1110619f0f5SStephen Smalley sb->s_fs_info = NULL; 1120619f0f5SStephen Smalley } 1136174eafcSJames Carter 114f0ee2e46SJames Carter #define SEL_INITCON_INO_OFFSET 0x01000000 115bce34bc0SJames Carter #define SEL_BOOL_INO_OFFSET 0x02000000 116e47c8fc5SChristopher J. PeBenito #define SEL_CLASS_INO_OFFSET 0x04000000 1173bb56b25SPaul Moore #define SEL_POLICYCAP_INO_OFFSET 0x08000000 118f0ee2e46SJames Carter #define SEL_INO_MASK 0x00ffffff 119f0ee2e46SJames Carter 120613ba187SDaniel Burgener #define BOOL_DIR_NAME "booleans" 121613ba187SDaniel Burgener #define CLASS_DIR_NAME "class" 122613ba187SDaniel Burgener #define POLICYCAP_DIR_NAME "policy_capabilities" 123613ba187SDaniel Burgener 1241da177e4SLinus Torvalds #define TMPBUFLEN 12 1251da177e4SLinus Torvalds static ssize_t sel_read_enforce(struct file *filp, char __user *buf, 1261da177e4SLinus Torvalds size_t count, loff_t *ppos) 1271da177e4SLinus Torvalds { 1280619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info; 1291da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 1301da177e4SLinus Torvalds ssize_t length; 1311da177e4SLinus Torvalds 132aa8e712cSStephen Smalley length = scnprintf(tmpbuf, TMPBUFLEN, "%d", 1330619f0f5SStephen Smalley enforcing_enabled(fsi->state)); 1341da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 1351da177e4SLinus Torvalds } 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP 1381da177e4SLinus Torvalds static ssize_t sel_write_enforce(struct file *file, const char __user *buf, 1391da177e4SLinus Torvalds size_t count, loff_t *ppos) 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds { 1420619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 1430619f0f5SStephen Smalley struct selinux_state *state = fsi->state; 144b77a493bSEric Paris char *page = NULL; 1451da177e4SLinus Torvalds ssize_t length; 146aa8e712cSStephen Smalley int old_value, new_value; 1471da177e4SLinus Torvalds 148bfd51626SDavi Arnaut if (count >= PAGE_SIZE) 1498365a719SAl Viro return -ENOMEM; 150b77a493bSEric Paris 1511da177e4SLinus Torvalds /* No partial writes. */ 152b77a493bSEric Paris if (*ppos != 0) 1538365a719SAl Viro return -EINVAL; 154b77a493bSEric Paris 1558365a719SAl Viro page = memdup_user_nul(buf, count); 1568365a719SAl Viro if (IS_ERR(page)) 1578365a719SAl Viro return PTR_ERR(page); 1581da177e4SLinus Torvalds 1591da177e4SLinus Torvalds length = -EINVAL; 1601da177e4SLinus Torvalds if (sscanf(page, "%d", &new_value) != 1) 1611da177e4SLinus Torvalds goto out; 1621da177e4SLinus Torvalds 163ea49d10eSStephen Smalley new_value = !!new_value; 164ea49d10eSStephen Smalley 1650619f0f5SStephen Smalley old_value = enforcing_enabled(state); 166aa8e712cSStephen Smalley if (new_value != old_value) { 1676b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 1686b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 169be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETENFORCE, 170be0554c9SStephen Smalley NULL); 1711da177e4SLinus Torvalds if (length) 1721da177e4SLinus Torvalds goto out; 173cdfb6b34SRichard Guy Briggs audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS, 1744195ed42SRichard Guy Briggs "enforcing=%d old_enforcing=%d auid=%u ses=%u" 1756c5a682eSStephen Smalley " enabled=1 old-enabled=1 lsm=selinux res=1", 176aa8e712cSStephen Smalley new_value, old_value, 177581abc09SEric W. Biederman from_kuid(&init_user_ns, audit_get_loginuid(current)), 1786c5a682eSStephen Smalley audit_get_sessionid(current)); 1790619f0f5SStephen Smalley enforcing_set(state, new_value); 180aa8e712cSStephen Smalley if (new_value) 1816b6bc620SStephen Smalley avc_ss_reset(state->avc, 0); 182aa8e712cSStephen Smalley selnl_notify_setenforce(new_value); 1830619f0f5SStephen Smalley selinux_status_update_setenforce(state, new_value); 184aa8e712cSStephen Smalley if (!new_value) 18542df744cSJanne Karhunen call_blocking_lsm_notifier(LSM_POLICY_CHANGE, NULL); 1862554a48fSLakshmi Ramasubramanian 1872554a48fSLakshmi Ramasubramanian selinux_ima_measure_state(state); 1881da177e4SLinus Torvalds } 1891da177e4SLinus Torvalds length = count; 1901da177e4SLinus Torvalds out: 1918365a719SAl Viro kfree(page); 1921da177e4SLinus Torvalds return length; 1931da177e4SLinus Torvalds } 1941da177e4SLinus Torvalds #else 1951da177e4SLinus Torvalds #define sel_write_enforce NULL 1961da177e4SLinus Torvalds #endif 1971da177e4SLinus Torvalds 1989c2e08c5SArjan van de Ven static const struct file_operations sel_enforce_ops = { 1991da177e4SLinus Torvalds .read = sel_read_enforce, 2001da177e4SLinus Torvalds .write = sel_write_enforce, 20157a62c23SArnd Bergmann .llseek = generic_file_llseek, 2021da177e4SLinus Torvalds }; 2031da177e4SLinus Torvalds 2043f12070eSEric Paris static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf, 2053f12070eSEric Paris size_t count, loff_t *ppos) 2063f12070eSEric Paris { 2070619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info; 2080619f0f5SStephen Smalley struct selinux_state *state = fsi->state; 2093f12070eSEric Paris char tmpbuf[TMPBUFLEN]; 2103f12070eSEric Paris ssize_t length; 211496ad9aaSAl Viro ino_t ino = file_inode(filp)->i_ino; 2123f12070eSEric Paris int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ? 2130619f0f5SStephen Smalley security_get_reject_unknown(state) : 2140619f0f5SStephen Smalley !security_get_allow_unknown(state); 2153f12070eSEric Paris 2163f12070eSEric Paris length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown); 2173f12070eSEric Paris return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 2183f12070eSEric Paris } 2193f12070eSEric Paris 2203f12070eSEric Paris static const struct file_operations sel_handle_unknown_ops = { 2213f12070eSEric Paris .read = sel_read_handle_unknown, 22257a62c23SArnd Bergmann .llseek = generic_file_llseek, 2233f12070eSEric Paris }; 2243f12070eSEric Paris 22511904167SKaiGai Kohei static int sel_open_handle_status(struct inode *inode, struct file *filp) 22611904167SKaiGai Kohei { 2270619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info; 2280619f0f5SStephen Smalley struct page *status = selinux_kernel_status_page(fsi->state); 22911904167SKaiGai Kohei 23011904167SKaiGai Kohei if (!status) 23111904167SKaiGai Kohei return -ENOMEM; 23211904167SKaiGai Kohei 23311904167SKaiGai Kohei filp->private_data = status; 23411904167SKaiGai Kohei 23511904167SKaiGai Kohei return 0; 23611904167SKaiGai Kohei } 23711904167SKaiGai Kohei 23811904167SKaiGai Kohei static ssize_t sel_read_handle_status(struct file *filp, char __user *buf, 23911904167SKaiGai Kohei size_t count, loff_t *ppos) 24011904167SKaiGai Kohei { 24111904167SKaiGai Kohei struct page *status = filp->private_data; 24211904167SKaiGai Kohei 24311904167SKaiGai Kohei BUG_ON(!status); 24411904167SKaiGai Kohei 24511904167SKaiGai Kohei return simple_read_from_buffer(buf, count, ppos, 24611904167SKaiGai Kohei page_address(status), 24711904167SKaiGai Kohei sizeof(struct selinux_kernel_status)); 24811904167SKaiGai Kohei } 24911904167SKaiGai Kohei 25011904167SKaiGai Kohei static int sel_mmap_handle_status(struct file *filp, 25111904167SKaiGai Kohei struct vm_area_struct *vma) 25211904167SKaiGai Kohei { 25311904167SKaiGai Kohei struct page *status = filp->private_data; 25411904167SKaiGai Kohei unsigned long size = vma->vm_end - vma->vm_start; 25511904167SKaiGai Kohei 25611904167SKaiGai Kohei BUG_ON(!status); 25711904167SKaiGai Kohei 25811904167SKaiGai Kohei /* only allows one page from the head */ 25911904167SKaiGai Kohei if (vma->vm_pgoff > 0 || size != PAGE_SIZE) 26011904167SKaiGai Kohei return -EIO; 26111904167SKaiGai Kohei /* disallow writable mapping */ 26211904167SKaiGai Kohei if (vma->vm_flags & VM_WRITE) 26311904167SKaiGai Kohei return -EPERM; 26411904167SKaiGai Kohei /* disallow mprotect() turns it into writable */ 26511904167SKaiGai Kohei vma->vm_flags &= ~VM_MAYWRITE; 26611904167SKaiGai Kohei 26711904167SKaiGai Kohei return remap_pfn_range(vma, vma->vm_start, 26811904167SKaiGai Kohei page_to_pfn(status), 26911904167SKaiGai Kohei size, vma->vm_page_prot); 27011904167SKaiGai Kohei } 27111904167SKaiGai Kohei 27211904167SKaiGai Kohei static const struct file_operations sel_handle_status_ops = { 27311904167SKaiGai Kohei .open = sel_open_handle_status, 27411904167SKaiGai Kohei .read = sel_read_handle_status, 27511904167SKaiGai Kohei .mmap = sel_mmap_handle_status, 27611904167SKaiGai Kohei .llseek = generic_file_llseek, 27711904167SKaiGai Kohei }; 27811904167SKaiGai Kohei 2791da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 2801da177e4SLinus Torvalds static ssize_t sel_write_disable(struct file *file, const char __user *buf, 2811da177e4SLinus Torvalds size_t count, loff_t *ppos) 2821da177e4SLinus Torvalds 2831da177e4SLinus Torvalds { 2840619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 2858365a719SAl Viro char *page; 2861da177e4SLinus Torvalds ssize_t length; 2871da177e4SLinus Torvalds int new_value; 2884195ed42SRichard Guy Briggs int enforcing; 2891da177e4SLinus Torvalds 29089b223bfSPaul Moore /* NOTE: we are now officially considering runtime disable as 29189b223bfSPaul Moore * deprecated, and using it will become increasingly painful 29289b223bfSPaul Moore * (e.g. sleeping/blocking) as we progress through future 29389b223bfSPaul Moore * kernel releases until eventually it is removed 29489b223bfSPaul Moore */ 29589b223bfSPaul Moore pr_err("SELinux: Runtime disable is deprecated, use selinux=0 on the kernel cmdline.\n"); 29643b66662SPaul Moore pr_err("SELinux: https://github.com/SELinuxProject/selinux-kernel/wiki/DEPRECATE-runtime-disable\n"); 297*e0d82593SPaul Moore ssleep(15); 29889b223bfSPaul Moore 299bfd51626SDavi Arnaut if (count >= PAGE_SIZE) 3008365a719SAl Viro return -ENOMEM; 301b77a493bSEric Paris 3021da177e4SLinus Torvalds /* No partial writes. */ 303b77a493bSEric Paris if (*ppos != 0) 3048365a719SAl Viro return -EINVAL; 305b77a493bSEric Paris 3068365a719SAl Viro page = memdup_user_nul(buf, count); 3078365a719SAl Viro if (IS_ERR(page)) 3088365a719SAl Viro return PTR_ERR(page); 3091da177e4SLinus Torvalds 3101da177e4SLinus Torvalds length = -EINVAL; 3111da177e4SLinus Torvalds if (sscanf(page, "%d", &new_value) != 1) 3121da177e4SLinus Torvalds goto out; 3131da177e4SLinus Torvalds 3141da177e4SLinus Torvalds if (new_value) { 3154195ed42SRichard Guy Briggs enforcing = enforcing_enabled(fsi->state); 3160619f0f5SStephen Smalley length = selinux_disable(fsi->state); 317b77a493bSEric Paris if (length) 3181da177e4SLinus Torvalds goto out; 319cdfb6b34SRichard Guy Briggs audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS, 3204195ed42SRichard Guy Briggs "enforcing=%d old_enforcing=%d auid=%u ses=%u" 3216c5a682eSStephen Smalley " enabled=0 old-enabled=1 lsm=selinux res=1", 3224195ed42SRichard Guy Briggs enforcing, enforcing, 323581abc09SEric W. Biederman from_kuid(&init_user_ns, audit_get_loginuid(current)), 3246c5a682eSStephen Smalley audit_get_sessionid(current)); 3251da177e4SLinus Torvalds } 3261da177e4SLinus Torvalds 3271da177e4SLinus Torvalds length = count; 3281da177e4SLinus Torvalds out: 3298365a719SAl Viro kfree(page); 3301da177e4SLinus Torvalds return length; 3311da177e4SLinus Torvalds } 3321da177e4SLinus Torvalds #else 3331da177e4SLinus Torvalds #define sel_write_disable NULL 3341da177e4SLinus Torvalds #endif 3351da177e4SLinus Torvalds 3369c2e08c5SArjan van de Ven static const struct file_operations sel_disable_ops = { 3371da177e4SLinus Torvalds .write = sel_write_disable, 33857a62c23SArnd Bergmann .llseek = generic_file_llseek, 3391da177e4SLinus Torvalds }; 3401da177e4SLinus Torvalds 3411da177e4SLinus Torvalds static ssize_t sel_read_policyvers(struct file *filp, char __user *buf, 3421da177e4SLinus Torvalds size_t count, loff_t *ppos) 3431da177e4SLinus Torvalds { 3441da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 3451da177e4SLinus Torvalds ssize_t length; 3461da177e4SLinus Torvalds 3471da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX); 3481da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 3491da177e4SLinus Torvalds } 3501da177e4SLinus Torvalds 3519c2e08c5SArjan van de Ven static const struct file_operations sel_policyvers_ops = { 3521da177e4SLinus Torvalds .read = sel_read_policyvers, 35357a62c23SArnd Bergmann .llseek = generic_file_llseek, 3541da177e4SLinus Torvalds }; 3551da177e4SLinus Torvalds 3561da177e4SLinus Torvalds /* declaration for sel_write_load */ 35766ec384aSDaniel Burgener static int sel_make_bools(struct selinux_policy *newpolicy, struct dentry *bool_dir, 35866ec384aSDaniel Burgener unsigned int *bool_num, char ***bool_pending_names, 359c3fae2b2SChristian Göttsche int **bool_pending_values); 36066ec384aSDaniel Burgener static int sel_make_classes(struct selinux_policy *newpolicy, 36166ec384aSDaniel Burgener struct dentry *class_dir, 36266ec384aSDaniel Burgener unsigned long *last_class_ino); 363e47c8fc5SChristopher J. PeBenito 364e47c8fc5SChristopher J. PeBenito /* declaration for sel_make_class_dirs */ 365a1c2aa1eSAl Viro static struct dentry *sel_make_dir(struct dentry *dir, const char *name, 366e47c8fc5SChristopher J. PeBenito unsigned long *ino); 3671da177e4SLinus Torvalds 3680eea6091SDaniel Burgener /* declaration for sel_make_policy_nodes */ 3690eea6091SDaniel Burgener static struct dentry *sel_make_disconnected_dir(struct super_block *sb, 3700eea6091SDaniel Burgener unsigned long *ino); 3710eea6091SDaniel Burgener 3720eea6091SDaniel Burgener /* declaration for sel_make_policy_nodes */ 373aeecf4a3SDaniel Burgener static void sel_remove_entries(struct dentry *de); 374aeecf4a3SDaniel Burgener 3751da177e4SLinus Torvalds static ssize_t sel_read_mls(struct file *filp, char __user *buf, 3761da177e4SLinus Torvalds size_t count, loff_t *ppos) 3771da177e4SLinus Torvalds { 3780619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info; 3791da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 3801da177e4SLinus Torvalds ssize_t length; 3811da177e4SLinus Torvalds 3820719aaf5SGuido Trentalancia length = scnprintf(tmpbuf, TMPBUFLEN, "%d", 3830619f0f5SStephen Smalley security_mls_enabled(fsi->state)); 3841da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 3851da177e4SLinus Torvalds } 3861da177e4SLinus Torvalds 3879c2e08c5SArjan van de Ven static const struct file_operations sel_mls_ops = { 3881da177e4SLinus Torvalds .read = sel_read_mls, 38957a62c23SArnd Bergmann .llseek = generic_file_llseek, 3901da177e4SLinus Torvalds }; 3911da177e4SLinus Torvalds 392cee74f47SEric Paris struct policy_load_memory { 393cee74f47SEric Paris size_t len; 394cee74f47SEric Paris void *data; 395cee74f47SEric Paris }; 396cee74f47SEric Paris 397cee74f47SEric Paris static int sel_open_policy(struct inode *inode, struct file *filp) 398cee74f47SEric Paris { 3990619f0f5SStephen Smalley struct selinux_fs_info *fsi = inode->i_sb->s_fs_info; 4000619f0f5SStephen Smalley struct selinux_state *state = fsi->state; 401cee74f47SEric Paris struct policy_load_memory *plm = NULL; 402cee74f47SEric Paris int rc; 403cee74f47SEric Paris 404cee74f47SEric Paris BUG_ON(filp->private_data); 405cee74f47SEric Paris 4069ff9abc4SStephen Smalley mutex_lock(&fsi->state->policy_mutex); 407cee74f47SEric Paris 4086b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 4096b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 410be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL); 411cee74f47SEric Paris if (rc) 412cee74f47SEric Paris goto err; 413cee74f47SEric Paris 414cee74f47SEric Paris rc = -EBUSY; 4150619f0f5SStephen Smalley if (fsi->policy_opened) 416cee74f47SEric Paris goto err; 417cee74f47SEric Paris 418cee74f47SEric Paris rc = -ENOMEM; 419cee74f47SEric Paris plm = kzalloc(sizeof(*plm), GFP_KERNEL); 420cee74f47SEric Paris if (!plm) 421cee74f47SEric Paris goto err; 422cee74f47SEric Paris 4230619f0f5SStephen Smalley rc = security_read_policy(state, &plm->data, &plm->len); 424cee74f47SEric Paris if (rc) 425cee74f47SEric Paris goto err; 426cee74f47SEric Paris 42766ccd256SOndrej Mosnacek if ((size_t)i_size_read(inode) != plm->len) { 42866ccd256SOndrej Mosnacek inode_lock(inode); 42966ccd256SOndrej Mosnacek i_size_write(inode, plm->len); 43066ccd256SOndrej Mosnacek inode_unlock(inode); 43166ccd256SOndrej Mosnacek } 43266ccd256SOndrej Mosnacek 4330619f0f5SStephen Smalley fsi->policy_opened = 1; 434cee74f47SEric Paris 435cee74f47SEric Paris filp->private_data = plm; 436cee74f47SEric Paris 4379ff9abc4SStephen Smalley mutex_unlock(&fsi->state->policy_mutex); 438cee74f47SEric Paris 439cee74f47SEric Paris return 0; 440cee74f47SEric Paris err: 4419ff9abc4SStephen Smalley mutex_unlock(&fsi->state->policy_mutex); 442cee74f47SEric Paris 443cee74f47SEric Paris if (plm) 444cee74f47SEric Paris vfree(plm->data); 445cee74f47SEric Paris kfree(plm); 446cee74f47SEric Paris return rc; 447cee74f47SEric Paris } 448cee74f47SEric Paris 449cee74f47SEric Paris static int sel_release_policy(struct inode *inode, struct file *filp) 450cee74f47SEric Paris { 4510619f0f5SStephen Smalley struct selinux_fs_info *fsi = inode->i_sb->s_fs_info; 452cee74f47SEric Paris struct policy_load_memory *plm = filp->private_data; 453cee74f47SEric Paris 454cee74f47SEric Paris BUG_ON(!plm); 455cee74f47SEric Paris 4560619f0f5SStephen Smalley fsi->policy_opened = 0; 457cee74f47SEric Paris 458cee74f47SEric Paris vfree(plm->data); 459cee74f47SEric Paris kfree(plm); 460cee74f47SEric Paris 461cee74f47SEric Paris return 0; 462cee74f47SEric Paris } 463cee74f47SEric Paris 464cee74f47SEric Paris static ssize_t sel_read_policy(struct file *filp, char __user *buf, 465cee74f47SEric Paris size_t count, loff_t *ppos) 466cee74f47SEric Paris { 467cee74f47SEric Paris struct policy_load_memory *plm = filp->private_data; 468cee74f47SEric Paris int ret; 469cee74f47SEric Paris 4706b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 4716b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 472be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL); 473cee74f47SEric Paris if (ret) 474cee74f47SEric Paris return ret; 4750da74120SJann Horn 4760da74120SJann Horn return simple_read_from_buffer(buf, count, ppos, plm->data, plm->len); 477cee74f47SEric Paris } 478cee74f47SEric Paris 479ac9a1f6dSSouptick Joarder static vm_fault_t sel_mmap_policy_fault(struct vm_fault *vmf) 480845ca30fSEric Paris { 48111bac800SDave Jiang struct policy_load_memory *plm = vmf->vma->vm_file->private_data; 482845ca30fSEric Paris unsigned long offset; 483845ca30fSEric Paris struct page *page; 484845ca30fSEric Paris 485845ca30fSEric Paris if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE)) 486845ca30fSEric Paris return VM_FAULT_SIGBUS; 487845ca30fSEric Paris 488845ca30fSEric Paris offset = vmf->pgoff << PAGE_SHIFT; 489845ca30fSEric Paris if (offset >= roundup(plm->len, PAGE_SIZE)) 490845ca30fSEric Paris return VM_FAULT_SIGBUS; 491845ca30fSEric Paris 492845ca30fSEric Paris page = vmalloc_to_page(plm->data + offset); 493845ca30fSEric Paris get_page(page); 494845ca30fSEric Paris 495845ca30fSEric Paris vmf->page = page; 496845ca30fSEric Paris 497845ca30fSEric Paris return 0; 498845ca30fSEric Paris } 499845ca30fSEric Paris 5007cbea8dcSKirill A. Shutemov static const struct vm_operations_struct sel_mmap_policy_ops = { 501845ca30fSEric Paris .fault = sel_mmap_policy_fault, 502845ca30fSEric Paris .page_mkwrite = sel_mmap_policy_fault, 503845ca30fSEric Paris }; 504845ca30fSEric Paris 505ad3fa08cSJames Morris static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma) 506845ca30fSEric Paris { 507845ca30fSEric Paris if (vma->vm_flags & VM_SHARED) { 508845ca30fSEric Paris /* do not allow mprotect to make mapping writable */ 509845ca30fSEric Paris vma->vm_flags &= ~VM_MAYWRITE; 510845ca30fSEric Paris 511845ca30fSEric Paris if (vma->vm_flags & VM_WRITE) 512845ca30fSEric Paris return -EACCES; 513845ca30fSEric Paris } 514845ca30fSEric Paris 515314e51b9SKonstantin Khlebnikov vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; 516845ca30fSEric Paris vma->vm_ops = &sel_mmap_policy_ops; 517845ca30fSEric Paris 518845ca30fSEric Paris return 0; 519845ca30fSEric Paris } 520845ca30fSEric Paris 521cee74f47SEric Paris static const struct file_operations sel_policy_ops = { 522cee74f47SEric Paris .open = sel_open_policy, 523cee74f47SEric Paris .read = sel_read_policy, 524845ca30fSEric Paris .mmap = sel_mmap_policy, 525cee74f47SEric Paris .release = sel_release_policy, 52647a93a5bSEric Paris .llseek = generic_file_llseek, 527cee74f47SEric Paris }; 528cee74f47SEric Paris 5290eea6091SDaniel Burgener static void sel_remove_old_bool_data(unsigned int bool_num, char **bool_names, 530c3fae2b2SChristian Göttsche int *bool_values) 531aeecf4a3SDaniel Burgener { 532aeecf4a3SDaniel Burgener u32 i; 533aeecf4a3SDaniel Burgener 534aeecf4a3SDaniel Burgener /* bool_dir cleanup */ 5350eea6091SDaniel Burgener for (i = 0; i < bool_num; i++) 5360eea6091SDaniel Burgener kfree(bool_names[i]); 5370eea6091SDaniel Burgener kfree(bool_names); 5380eea6091SDaniel Burgener kfree(bool_values); 539aeecf4a3SDaniel Burgener } 540aeecf4a3SDaniel Burgener 54102a52c5cSStephen Smalley static int sel_make_policy_nodes(struct selinux_fs_info *fsi, 54202a52c5cSStephen Smalley struct selinux_policy *newpolicy) 5430619f0f5SStephen Smalley { 5440eea6091SDaniel Burgener int ret = 0; 5450eea6091SDaniel Burgener struct dentry *tmp_parent, *tmp_bool_dir, *tmp_class_dir, *old_dentry; 5460eea6091SDaniel Burgener unsigned int tmp_bool_num, old_bool_num; 5470eea6091SDaniel Burgener char **tmp_bool_names, **old_bool_names; 548c3fae2b2SChristian Göttsche int *tmp_bool_values, *old_bool_values; 5490eea6091SDaniel Burgener unsigned long tmp_ino = fsi->last_ino; /* Don't increment last_ino in this function */ 5500619f0f5SStephen Smalley 5510eea6091SDaniel Burgener tmp_parent = sel_make_disconnected_dir(fsi->sb, &tmp_ino); 5520eea6091SDaniel Burgener if (IS_ERR(tmp_parent)) 5530eea6091SDaniel Burgener return PTR_ERR(tmp_parent); 554aeecf4a3SDaniel Burgener 5550eea6091SDaniel Burgener tmp_ino = fsi->bool_dir->d_inode->i_ino - 1; /* sel_make_dir will increment and set */ 5560eea6091SDaniel Burgener tmp_bool_dir = sel_make_dir(tmp_parent, BOOL_DIR_NAME, &tmp_ino); 5570eea6091SDaniel Burgener if (IS_ERR(tmp_bool_dir)) { 5580eea6091SDaniel Burgener ret = PTR_ERR(tmp_bool_dir); 5590eea6091SDaniel Burgener goto out; 5600619f0f5SStephen Smalley } 5610619f0f5SStephen Smalley 5620eea6091SDaniel Burgener tmp_ino = fsi->class_dir->d_inode->i_ino - 1; /* sel_make_dir will increment and set */ 5630eea6091SDaniel Burgener tmp_class_dir = sel_make_dir(tmp_parent, CLASS_DIR_NAME, &tmp_ino); 5640eea6091SDaniel Burgener if (IS_ERR(tmp_class_dir)) { 5650eea6091SDaniel Burgener ret = PTR_ERR(tmp_class_dir); 5660eea6091SDaniel Burgener goto out; 5670eea6091SDaniel Burgener } 5680eea6091SDaniel Burgener 5690eea6091SDaniel Burgener ret = sel_make_bools(newpolicy, tmp_bool_dir, &tmp_bool_num, 5700eea6091SDaniel Burgener &tmp_bool_names, &tmp_bool_values); 571ee5de60aSOndrej Mosnacek if (ret) 5720eea6091SDaniel Burgener goto out; 5730eea6091SDaniel Burgener 5740eea6091SDaniel Burgener ret = sel_make_classes(newpolicy, tmp_class_dir, 57566ec384aSDaniel Burgener &fsi->last_class_ino); 576ee5de60aSOndrej Mosnacek if (ret) 5770eea6091SDaniel Burgener goto out; 5780619f0f5SStephen Smalley 5790eea6091SDaniel Burgener /* booleans */ 5800eea6091SDaniel Burgener old_dentry = fsi->bool_dir; 5810eea6091SDaniel Burgener lock_rename(tmp_bool_dir, old_dentry); 5820eea6091SDaniel Burgener d_exchange(tmp_bool_dir, fsi->bool_dir); 5830eea6091SDaniel Burgener 5840eea6091SDaniel Burgener old_bool_num = fsi->bool_num; 5850eea6091SDaniel Burgener old_bool_names = fsi->bool_pending_names; 5860eea6091SDaniel Burgener old_bool_values = fsi->bool_pending_values; 5870eea6091SDaniel Burgener 5880eea6091SDaniel Burgener fsi->bool_num = tmp_bool_num; 5890eea6091SDaniel Burgener fsi->bool_pending_names = tmp_bool_names; 5900eea6091SDaniel Burgener fsi->bool_pending_values = tmp_bool_values; 5910eea6091SDaniel Burgener 5920eea6091SDaniel Burgener sel_remove_old_bool_data(old_bool_num, old_bool_names, old_bool_values); 5930eea6091SDaniel Burgener 5940eea6091SDaniel Burgener fsi->bool_dir = tmp_bool_dir; 5950eea6091SDaniel Burgener unlock_rename(tmp_bool_dir, old_dentry); 5960eea6091SDaniel Burgener 5970eea6091SDaniel Burgener /* classes */ 5980eea6091SDaniel Burgener old_dentry = fsi->class_dir; 5990eea6091SDaniel Burgener lock_rename(tmp_class_dir, old_dentry); 6000eea6091SDaniel Burgener d_exchange(tmp_class_dir, fsi->class_dir); 6010eea6091SDaniel Burgener fsi->class_dir = tmp_class_dir; 6020eea6091SDaniel Burgener unlock_rename(tmp_class_dir, old_dentry); 6030eea6091SDaniel Burgener 6040eea6091SDaniel Burgener out: 6050eea6091SDaniel Burgener /* Since the other temporary dirs are children of tmp_parent 6060eea6091SDaniel Burgener * this will handle all the cleanup in the case of a failure before 6070eea6091SDaniel Burgener * the swapover 6080eea6091SDaniel Burgener */ 6090eea6091SDaniel Burgener sel_remove_entries(tmp_parent); 6100eea6091SDaniel Burgener dput(tmp_parent); /* d_genocide() only handles the children */ 6110eea6091SDaniel Burgener 6120eea6091SDaniel Burgener return ret; 6130619f0f5SStephen Smalley } 6140619f0f5SStephen Smalley 6151da177e4SLinus Torvalds static ssize_t sel_write_load(struct file *file, const char __user *buf, 6161da177e4SLinus Torvalds size_t count, loff_t *ppos) 6171da177e4SLinus Torvalds 6181da177e4SLinus Torvalds { 6190619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 6206406887aSOndrej Mosnacek struct selinux_load_state load_state; 6211da177e4SLinus Torvalds ssize_t length; 6221da177e4SLinus Torvalds void *data = NULL; 6231da177e4SLinus Torvalds 6249ff9abc4SStephen Smalley mutex_lock(&fsi->state->policy_mutex); 6251da177e4SLinus Torvalds 6266b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 6276b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 628be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL); 6291da177e4SLinus Torvalds if (length) 6301da177e4SLinus Torvalds goto out; 6311da177e4SLinus Torvalds 6321da177e4SLinus Torvalds /* No partial writes. */ 6331da177e4SLinus Torvalds length = -EINVAL; 634b77a493bSEric Paris if (*ppos != 0) 6351da177e4SLinus Torvalds goto out; 6361da177e4SLinus Torvalds 637b77a493bSEric Paris length = -ENOMEM; 638b77a493bSEric Paris data = vmalloc(count); 639b77a493bSEric Paris if (!data) 640b77a493bSEric Paris goto out; 6411da177e4SLinus Torvalds 6421da177e4SLinus Torvalds length = -EFAULT; 6431da177e4SLinus Torvalds if (copy_from_user(data, buf, count) != 0) 6441da177e4SLinus Torvalds goto out; 6451da177e4SLinus Torvalds 6466406887aSOndrej Mosnacek length = security_load_policy(fsi->state, data, count, &load_state); 6474262fb51SGary Tierney if (length) { 6484262fb51SGary Tierney pr_warn_ratelimited("SELinux: failed to load policy\n"); 6491da177e4SLinus Torvalds goto out; 6504262fb51SGary Tierney } 6511da177e4SLinus Torvalds 6526406887aSOndrej Mosnacek length = sel_make_policy_nodes(fsi, load_state.policy); 65302a52c5cSStephen Smalley if (length) { 654ee5de60aSOndrej Mosnacek pr_warn_ratelimited("SELinux: failed to initialize selinuxfs\n"); 6556406887aSOndrej Mosnacek selinux_policy_cancel(fsi->state, &load_state); 656519dad3bSOndrej Mosnacek goto out; 65702a52c5cSStephen Smalley } 65802a52c5cSStephen Smalley 6596406887aSOndrej Mosnacek selinux_policy_commit(fsi->state, &load_state); 660b77a493bSEric Paris 6611da177e4SLinus Torvalds length = count; 662e47c8fc5SChristopher J. PeBenito 663cdfb6b34SRichard Guy Briggs audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_POLICY_LOAD, 664d141136fSRichard Guy Briggs "auid=%u ses=%u lsm=selinux res=1", 665581abc09SEric W. Biederman from_kuid(&init_user_ns, audit_get_loginuid(current)), 6664746ec5bSEric Paris audit_get_sessionid(current)); 6671da177e4SLinus Torvalds out: 6689ff9abc4SStephen Smalley mutex_unlock(&fsi->state->policy_mutex); 6691da177e4SLinus Torvalds vfree(data); 6701da177e4SLinus Torvalds return length; 6711da177e4SLinus Torvalds } 6721da177e4SLinus Torvalds 6739c2e08c5SArjan van de Ven static const struct file_operations sel_load_ops = { 6741da177e4SLinus Torvalds .write = sel_write_load, 67557a62c23SArnd Bergmann .llseek = generic_file_llseek, 6761da177e4SLinus Torvalds }; 6771da177e4SLinus Torvalds 678ce9982d0SStephen Smalley static ssize_t sel_write_context(struct file *file, char *buf, size_t size) 6791da177e4SLinus Torvalds { 6800619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 6810619f0f5SStephen Smalley struct selinux_state *state = fsi->state; 682b77a493bSEric Paris char *canon = NULL; 683ce9982d0SStephen Smalley u32 sid, len; 6841da177e4SLinus Torvalds ssize_t length; 6851da177e4SLinus Torvalds 6866b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 6876b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 688be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__CHECK_CONTEXT, NULL); 6891da177e4SLinus Torvalds if (length) 690b77a493bSEric Paris goto out; 6911da177e4SLinus Torvalds 6920619f0f5SStephen Smalley length = security_context_to_sid(state, buf, size, &sid, GFP_KERNEL); 693b77a493bSEric Paris if (length) 694b77a493bSEric Paris goto out; 6951da177e4SLinus Torvalds 6960619f0f5SStephen Smalley length = security_sid_to_context(state, sid, &canon, &len); 697b77a493bSEric Paris if (length) 698b77a493bSEric Paris goto out; 699ce9982d0SStephen Smalley 700b77a493bSEric Paris length = -ERANGE; 701ce9982d0SStephen Smalley if (len > SIMPLE_TRANSACTION_LIMIT) { 702f8b69a5fSpeter enderborg pr_err("SELinux: %s: context size (%u) exceeds " 703744ba35eSEric Paris "payload max\n", __func__, len); 704ce9982d0SStephen Smalley goto out; 705ce9982d0SStephen Smalley } 706ce9982d0SStephen Smalley 707ce9982d0SStephen Smalley memcpy(buf, canon, len); 708ce9982d0SStephen Smalley length = len; 7091da177e4SLinus Torvalds out: 710ce9982d0SStephen Smalley kfree(canon); 7111da177e4SLinus Torvalds return length; 7121da177e4SLinus Torvalds } 7131da177e4SLinus Torvalds 7141da177e4SLinus Torvalds static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf, 7151da177e4SLinus Torvalds size_t count, loff_t *ppos) 7161da177e4SLinus Torvalds { 7170619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info; 7181da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 7191da177e4SLinus Torvalds ssize_t length; 7201da177e4SLinus Torvalds 7218861d0afSLakshmi Ramasubramanian length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 7228861d0afSLakshmi Ramasubramanian checkreqprot_get(fsi->state)); 7231da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 7241da177e4SLinus Torvalds } 7251da177e4SLinus Torvalds 7261da177e4SLinus Torvalds static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf, 7271da177e4SLinus Torvalds size_t count, loff_t *ppos) 7281da177e4SLinus Torvalds { 7290619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 7308365a719SAl Viro char *page; 7311da177e4SLinus Torvalds ssize_t length; 7321da177e4SLinus Torvalds unsigned int new_value; 7331da177e4SLinus Torvalds 7346b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 7356b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 736be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETCHECKREQPROT, 737be0554c9SStephen Smalley NULL); 7381da177e4SLinus Torvalds if (length) 7398365a719SAl Viro return length; 7401da177e4SLinus Torvalds 741bfd51626SDavi Arnaut if (count >= PAGE_SIZE) 7428365a719SAl Viro return -ENOMEM; 743b77a493bSEric Paris 7441da177e4SLinus Torvalds /* No partial writes. */ 745b77a493bSEric Paris if (*ppos != 0) 7468365a719SAl Viro return -EINVAL; 747b77a493bSEric Paris 7488365a719SAl Viro page = memdup_user_nul(buf, count); 7498365a719SAl Viro if (IS_ERR(page)) 7508365a719SAl Viro return PTR_ERR(page); 7511da177e4SLinus Torvalds 7521da177e4SLinus Torvalds length = -EINVAL; 7531da177e4SLinus Torvalds if (sscanf(page, "%u", &new_value) != 1) 7541da177e4SLinus Torvalds goto out; 7551da177e4SLinus Torvalds 756e9c38f9fSStephen Smalley if (new_value) { 757e9c38f9fSStephen Smalley char comm[sizeof(current->comm)]; 758e9c38f9fSStephen Smalley 759e9c38f9fSStephen Smalley memcpy(comm, current->comm, sizeof(comm)); 76081200b02SPaul Moore pr_err("SELinux: %s (%d) set checkreqprot to 1. This is deprecated and will be rejected in a future kernel release.\n", 761e9c38f9fSStephen Smalley comm, current->pid); 762e9c38f9fSStephen Smalley } 763e9c38f9fSStephen Smalley 7648861d0afSLakshmi Ramasubramanian checkreqprot_set(fsi->state, (new_value ? 1 : 0)); 7656a9e261cSPaul Moore if (new_value) 766*e0d82593SPaul Moore ssleep(15); 7671da177e4SLinus Torvalds length = count; 7682554a48fSLakshmi Ramasubramanian 7692554a48fSLakshmi Ramasubramanian selinux_ima_measure_state(fsi->state); 7702554a48fSLakshmi Ramasubramanian 7711da177e4SLinus Torvalds out: 7728365a719SAl Viro kfree(page); 7731da177e4SLinus Torvalds return length; 7741da177e4SLinus Torvalds } 7759c2e08c5SArjan van de Ven static const struct file_operations sel_checkreqprot_ops = { 7761da177e4SLinus Torvalds .read = sel_read_checkreqprot, 7771da177e4SLinus Torvalds .write = sel_write_checkreqprot, 77857a62c23SArnd Bergmann .llseek = generic_file_llseek, 7791da177e4SLinus Torvalds }; 7801da177e4SLinus Torvalds 781f9df6458SAndrew Perepechko static ssize_t sel_write_validatetrans(struct file *file, 782f9df6458SAndrew Perepechko const char __user *buf, 783f9df6458SAndrew Perepechko size_t count, loff_t *ppos) 784f9df6458SAndrew Perepechko { 7850619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 7860619f0f5SStephen Smalley struct selinux_state *state = fsi->state; 787f9df6458SAndrew Perepechko char *oldcon = NULL, *newcon = NULL, *taskcon = NULL; 788f9df6458SAndrew Perepechko char *req = NULL; 789f9df6458SAndrew Perepechko u32 osid, nsid, tsid; 790f9df6458SAndrew Perepechko u16 tclass; 791f9df6458SAndrew Perepechko int rc; 792f9df6458SAndrew Perepechko 7936b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 7946b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 795be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__VALIDATE_TRANS, NULL); 796f9df6458SAndrew Perepechko if (rc) 797f9df6458SAndrew Perepechko goto out; 798f9df6458SAndrew Perepechko 799f9df6458SAndrew Perepechko rc = -ENOMEM; 800f9df6458SAndrew Perepechko if (count >= PAGE_SIZE) 801f9df6458SAndrew Perepechko goto out; 802f9df6458SAndrew Perepechko 803f9df6458SAndrew Perepechko /* No partial writes. */ 804f9df6458SAndrew Perepechko rc = -EINVAL; 805f9df6458SAndrew Perepechko if (*ppos != 0) 806f9df6458SAndrew Perepechko goto out; 807f9df6458SAndrew Perepechko 8080b884d25SAl Viro req = memdup_user_nul(buf, count); 8090b884d25SAl Viro if (IS_ERR(req)) { 8100b884d25SAl Viro rc = PTR_ERR(req); 8110b884d25SAl Viro req = NULL; 812f9df6458SAndrew Perepechko goto out; 8130b884d25SAl Viro } 814f9df6458SAndrew Perepechko 815f9df6458SAndrew Perepechko rc = -ENOMEM; 816f9df6458SAndrew Perepechko oldcon = kzalloc(count + 1, GFP_KERNEL); 817f9df6458SAndrew Perepechko if (!oldcon) 818f9df6458SAndrew Perepechko goto out; 819f9df6458SAndrew Perepechko 820f9df6458SAndrew Perepechko newcon = kzalloc(count + 1, GFP_KERNEL); 821f9df6458SAndrew Perepechko if (!newcon) 822f9df6458SAndrew Perepechko goto out; 823f9df6458SAndrew Perepechko 824f9df6458SAndrew Perepechko taskcon = kzalloc(count + 1, GFP_KERNEL); 825f9df6458SAndrew Perepechko if (!taskcon) 826f9df6458SAndrew Perepechko goto out; 827f9df6458SAndrew Perepechko 828f9df6458SAndrew Perepechko rc = -EINVAL; 829f9df6458SAndrew Perepechko if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4) 830f9df6458SAndrew Perepechko goto out; 831f9df6458SAndrew Perepechko 8320619f0f5SStephen Smalley rc = security_context_str_to_sid(state, oldcon, &osid, GFP_KERNEL); 833f9df6458SAndrew Perepechko if (rc) 834f9df6458SAndrew Perepechko goto out; 835f9df6458SAndrew Perepechko 8360619f0f5SStephen Smalley rc = security_context_str_to_sid(state, newcon, &nsid, GFP_KERNEL); 837f9df6458SAndrew Perepechko if (rc) 838f9df6458SAndrew Perepechko goto out; 839f9df6458SAndrew Perepechko 8400619f0f5SStephen Smalley rc = security_context_str_to_sid(state, taskcon, &tsid, GFP_KERNEL); 841f9df6458SAndrew Perepechko if (rc) 842f9df6458SAndrew Perepechko goto out; 843f9df6458SAndrew Perepechko 8440619f0f5SStephen Smalley rc = security_validate_transition_user(state, osid, nsid, tsid, tclass); 845f9df6458SAndrew Perepechko if (!rc) 846f9df6458SAndrew Perepechko rc = count; 847f9df6458SAndrew Perepechko out: 848f9df6458SAndrew Perepechko kfree(req); 849f9df6458SAndrew Perepechko kfree(oldcon); 850f9df6458SAndrew Perepechko kfree(newcon); 851f9df6458SAndrew Perepechko kfree(taskcon); 852f9df6458SAndrew Perepechko return rc; 853f9df6458SAndrew Perepechko } 854f9df6458SAndrew Perepechko 855f9df6458SAndrew Perepechko static const struct file_operations sel_transition_ops = { 856f9df6458SAndrew Perepechko .write = sel_write_validatetrans, 857f9df6458SAndrew Perepechko .llseek = generic_file_llseek, 858f9df6458SAndrew Perepechko }; 859f9df6458SAndrew Perepechko 8601da177e4SLinus Torvalds /* 8611da177e4SLinus Torvalds * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c 8621da177e4SLinus Torvalds */ 8631da177e4SLinus Torvalds static ssize_t sel_write_access(struct file *file, char *buf, size_t size); 8641da177e4SLinus Torvalds static ssize_t sel_write_create(struct file *file, char *buf, size_t size); 8651da177e4SLinus Torvalds static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size); 8661da177e4SLinus Torvalds static ssize_t sel_write_user(struct file *file, char *buf, size_t size); 8671da177e4SLinus Torvalds static ssize_t sel_write_member(struct file *file, char *buf, size_t size); 8681da177e4SLinus Torvalds 869631d2b49SEric Biggers static ssize_t (*const write_op[])(struct file *, char *, size_t) = { 8701da177e4SLinus Torvalds [SEL_ACCESS] = sel_write_access, 8711da177e4SLinus Torvalds [SEL_CREATE] = sel_write_create, 8721da177e4SLinus Torvalds [SEL_RELABEL] = sel_write_relabel, 8731da177e4SLinus Torvalds [SEL_USER] = sel_write_user, 8741da177e4SLinus Torvalds [SEL_MEMBER] = sel_write_member, 875ce9982d0SStephen Smalley [SEL_CONTEXT] = sel_write_context, 8761da177e4SLinus Torvalds }; 8771da177e4SLinus Torvalds 8781da177e4SLinus Torvalds static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos) 8791da177e4SLinus Torvalds { 880496ad9aaSAl Viro ino_t ino = file_inode(file)->i_ino; 8811da177e4SLinus Torvalds char *data; 8821da177e4SLinus Torvalds ssize_t rv; 8831da177e4SLinus Torvalds 8846e20a64aSNicolas Kaiser if (ino >= ARRAY_SIZE(write_op) || !write_op[ino]) 8851da177e4SLinus Torvalds return -EINVAL; 8861da177e4SLinus Torvalds 8871da177e4SLinus Torvalds data = simple_transaction_get(file, buf, size); 8881da177e4SLinus Torvalds if (IS_ERR(data)) 8891da177e4SLinus Torvalds return PTR_ERR(data); 8901da177e4SLinus Torvalds 8911da177e4SLinus Torvalds rv = write_op[ino](file, data, size); 8921da177e4SLinus Torvalds if (rv > 0) { 8931da177e4SLinus Torvalds simple_transaction_set(file, rv); 8941da177e4SLinus Torvalds rv = size; 8951da177e4SLinus Torvalds } 8961da177e4SLinus Torvalds return rv; 8971da177e4SLinus Torvalds } 8981da177e4SLinus Torvalds 8999c2e08c5SArjan van de Ven static const struct file_operations transaction_ops = { 9001da177e4SLinus Torvalds .write = selinux_transaction_write, 9011da177e4SLinus Torvalds .read = simple_transaction_read, 9021da177e4SLinus Torvalds .release = simple_transaction_release, 90357a62c23SArnd Bergmann .llseek = generic_file_llseek, 9041da177e4SLinus Torvalds }; 9051da177e4SLinus Torvalds 9061da177e4SLinus Torvalds /* 9071da177e4SLinus Torvalds * payload - write methods 9081da177e4SLinus Torvalds * If the method has a response, the response should be put in buf, 9091da177e4SLinus Torvalds * and the length returned. Otherwise return 0 or and -error. 9101da177e4SLinus Torvalds */ 9111da177e4SLinus Torvalds 9121da177e4SLinus Torvalds static ssize_t sel_write_access(struct file *file, char *buf, size_t size) 9131da177e4SLinus Torvalds { 9140619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 9150619f0f5SStephen Smalley struct selinux_state *state = fsi->state; 916b77a493bSEric Paris char *scon = NULL, *tcon = NULL; 9171da177e4SLinus Torvalds u32 ssid, tsid; 9181da177e4SLinus Torvalds u16 tclass; 9191da177e4SLinus Torvalds struct av_decision avd; 9201da177e4SLinus Torvalds ssize_t length; 9211da177e4SLinus Torvalds 9226b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 9236b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 924be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_AV, NULL); 9251da177e4SLinus Torvalds if (length) 926b77a493bSEric Paris goto out; 9271da177e4SLinus Torvalds 9281da177e4SLinus Torvalds length = -ENOMEM; 92989d155efSJames Morris scon = kzalloc(size + 1, GFP_KERNEL); 9301da177e4SLinus Torvalds if (!scon) 931b77a493bSEric Paris goto out; 9321da177e4SLinus Torvalds 933b77a493bSEric Paris length = -ENOMEM; 93489d155efSJames Morris tcon = kzalloc(size + 1, GFP_KERNEL); 9351da177e4SLinus Torvalds if (!tcon) 9361da177e4SLinus Torvalds goto out; 9371da177e4SLinus Torvalds 9381da177e4SLinus Torvalds length = -EINVAL; 93919439d05SStephen Smalley if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) 940b77a493bSEric Paris goto out; 9411da177e4SLinus Torvalds 9420619f0f5SStephen Smalley length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL); 943b77a493bSEric Paris if (length) 944b77a493bSEric Paris goto out; 945b77a493bSEric Paris 9460619f0f5SStephen Smalley length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL); 947b77a493bSEric Paris if (length) 948b77a493bSEric Paris goto out; 9491da177e4SLinus Torvalds 9500619f0f5SStephen Smalley security_compute_av_user(state, ssid, tsid, tclass, &avd); 9511da177e4SLinus Torvalds 9521da177e4SLinus Torvalds length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, 9538a6f83afSKaiGai Kohei "%x %x %x %x %u %x", 954f1c6381aSEric Paris avd.allowed, 0xffffffff, 9551da177e4SLinus Torvalds avd.auditallow, avd.auditdeny, 9568a6f83afSKaiGai Kohei avd.seqno, avd.flags); 9571da177e4SLinus Torvalds out: 958b77a493bSEric Paris kfree(tcon); 9591da177e4SLinus Torvalds kfree(scon); 9601da177e4SLinus Torvalds return length; 9611da177e4SLinus Torvalds } 9621da177e4SLinus Torvalds 9631da177e4SLinus Torvalds static ssize_t sel_write_create(struct file *file, char *buf, size_t size) 9641da177e4SLinus Torvalds { 9650619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 9660619f0f5SStephen Smalley struct selinux_state *state = fsi->state; 967b77a493bSEric Paris char *scon = NULL, *tcon = NULL; 968f50a3ec9SKohei Kaigai char *namebuf = NULL, *objname = NULL; 9691da177e4SLinus Torvalds u32 ssid, tsid, newsid; 9701da177e4SLinus Torvalds u16 tclass; 9711da177e4SLinus Torvalds ssize_t length; 972b77a493bSEric Paris char *newcon = NULL; 9731da177e4SLinus Torvalds u32 len; 974f50a3ec9SKohei Kaigai int nargs; 9751da177e4SLinus Torvalds 9766b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 9776b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 978be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_CREATE, 979be0554c9SStephen Smalley NULL); 9801da177e4SLinus Torvalds if (length) 981b77a493bSEric Paris goto out; 9821da177e4SLinus Torvalds 9831da177e4SLinus Torvalds length = -ENOMEM; 98489d155efSJames Morris scon = kzalloc(size + 1, GFP_KERNEL); 9851da177e4SLinus Torvalds if (!scon) 986b77a493bSEric Paris goto out; 9871da177e4SLinus Torvalds 988b77a493bSEric Paris length = -ENOMEM; 98989d155efSJames Morris tcon = kzalloc(size + 1, GFP_KERNEL); 9901da177e4SLinus Torvalds if (!tcon) 9911da177e4SLinus Torvalds goto out; 9921da177e4SLinus Torvalds 993f50a3ec9SKohei Kaigai length = -ENOMEM; 994f50a3ec9SKohei Kaigai namebuf = kzalloc(size + 1, GFP_KERNEL); 995f50a3ec9SKohei Kaigai if (!namebuf) 996b77a493bSEric Paris goto out; 9971da177e4SLinus Torvalds 998f50a3ec9SKohei Kaigai length = -EINVAL; 999f50a3ec9SKohei Kaigai nargs = sscanf(buf, "%s %s %hu %s", scon, tcon, &tclass, namebuf); 1000f50a3ec9SKohei Kaigai if (nargs < 3 || nargs > 4) 1001f50a3ec9SKohei Kaigai goto out; 10020f7e4c33SKohei Kaigai if (nargs == 4) { 10030f7e4c33SKohei Kaigai /* 10040f7e4c33SKohei Kaigai * If and when the name of new object to be queried contains 10050f7e4c33SKohei Kaigai * either whitespace or multibyte characters, they shall be 10060f7e4c33SKohei Kaigai * encoded based on the percentage-encoding rule. 10070f7e4c33SKohei Kaigai * If not encoded, the sscanf logic picks up only left-half 10080f7e4c33SKohei Kaigai * of the supplied name; splitted by a whitespace unexpectedly. 10090f7e4c33SKohei Kaigai */ 10100f7e4c33SKohei Kaigai char *r, *w; 10110f7e4c33SKohei Kaigai int c1, c2; 10120f7e4c33SKohei Kaigai 10130f7e4c33SKohei Kaigai r = w = namebuf; 10140f7e4c33SKohei Kaigai do { 10150f7e4c33SKohei Kaigai c1 = *r++; 10160f7e4c33SKohei Kaigai if (c1 == '+') 10170f7e4c33SKohei Kaigai c1 = ' '; 10180f7e4c33SKohei Kaigai else if (c1 == '%') { 1019af7ff2c2SAndy Shevchenko c1 = hex_to_bin(*r++); 1020af7ff2c2SAndy Shevchenko if (c1 < 0) 10210f7e4c33SKohei Kaigai goto out; 1022af7ff2c2SAndy Shevchenko c2 = hex_to_bin(*r++); 1023af7ff2c2SAndy Shevchenko if (c2 < 0) 10240f7e4c33SKohei Kaigai goto out; 10250f7e4c33SKohei Kaigai c1 = (c1 << 4) | c2; 10260f7e4c33SKohei Kaigai } 10270f7e4c33SKohei Kaigai *w++ = c1; 10280f7e4c33SKohei Kaigai } while (c1 != '\0'); 10290f7e4c33SKohei Kaigai 1030f50a3ec9SKohei Kaigai objname = namebuf; 10310f7e4c33SKohei Kaigai } 1032f50a3ec9SKohei Kaigai 10330619f0f5SStephen Smalley length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL); 1034b77a493bSEric Paris if (length) 1035b77a493bSEric Paris goto out; 1036b77a493bSEric Paris 10370619f0f5SStephen Smalley length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL); 1038b77a493bSEric Paris if (length) 1039b77a493bSEric Paris goto out; 10401da177e4SLinus Torvalds 10410619f0f5SStephen Smalley length = security_transition_sid_user(state, ssid, tsid, tclass, 10420619f0f5SStephen Smalley objname, &newsid); 1043b77a493bSEric Paris if (length) 1044b77a493bSEric Paris goto out; 10451da177e4SLinus Torvalds 10460619f0f5SStephen Smalley length = security_sid_to_context(state, newsid, &newcon, &len); 1047b77a493bSEric Paris if (length) 1048b77a493bSEric Paris goto out; 10491da177e4SLinus Torvalds 1050b77a493bSEric Paris length = -ERANGE; 10511da177e4SLinus Torvalds if (len > SIMPLE_TRANSACTION_LIMIT) { 1052f8b69a5fSpeter enderborg pr_err("SELinux: %s: context size (%u) exceeds " 1053744ba35eSEric Paris "payload max\n", __func__, len); 1054b77a493bSEric Paris goto out; 10551da177e4SLinus Torvalds } 10561da177e4SLinus Torvalds 10571da177e4SLinus Torvalds memcpy(buf, newcon, len); 10581da177e4SLinus Torvalds length = len; 10591da177e4SLinus Torvalds out: 1060b77a493bSEric Paris kfree(newcon); 1061f50a3ec9SKohei Kaigai kfree(namebuf); 1062b77a493bSEric Paris kfree(tcon); 10631da177e4SLinus Torvalds kfree(scon); 10641da177e4SLinus Torvalds return length; 10651da177e4SLinus Torvalds } 10661da177e4SLinus Torvalds 10671da177e4SLinus Torvalds static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size) 10681da177e4SLinus Torvalds { 10690619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 10700619f0f5SStephen Smalley struct selinux_state *state = fsi->state; 1071b77a493bSEric Paris char *scon = NULL, *tcon = NULL; 10721da177e4SLinus Torvalds u32 ssid, tsid, newsid; 10731da177e4SLinus Torvalds u16 tclass; 10741da177e4SLinus Torvalds ssize_t length; 1075b77a493bSEric Paris char *newcon = NULL; 10761da177e4SLinus Torvalds u32 len; 10771da177e4SLinus Torvalds 10786b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 10796b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 1080be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_RELABEL, 1081be0554c9SStephen Smalley NULL); 10821da177e4SLinus Torvalds if (length) 1083b77a493bSEric Paris goto out; 10841da177e4SLinus Torvalds 10851da177e4SLinus Torvalds length = -ENOMEM; 108689d155efSJames Morris scon = kzalloc(size + 1, GFP_KERNEL); 10871da177e4SLinus Torvalds if (!scon) 1088b77a493bSEric Paris goto out; 10891da177e4SLinus Torvalds 1090b77a493bSEric Paris length = -ENOMEM; 109189d155efSJames Morris tcon = kzalloc(size + 1, GFP_KERNEL); 10921da177e4SLinus Torvalds if (!tcon) 10931da177e4SLinus Torvalds goto out; 10941da177e4SLinus Torvalds 10951da177e4SLinus Torvalds length = -EINVAL; 10961da177e4SLinus Torvalds if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) 1097b77a493bSEric Paris goto out; 10981da177e4SLinus Torvalds 10990619f0f5SStephen Smalley length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL); 1100b77a493bSEric Paris if (length) 1101b77a493bSEric Paris goto out; 1102b77a493bSEric Paris 11030619f0f5SStephen Smalley length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL); 1104b77a493bSEric Paris if (length) 1105b77a493bSEric Paris goto out; 11061da177e4SLinus Torvalds 11070619f0f5SStephen Smalley length = security_change_sid(state, ssid, tsid, tclass, &newsid); 1108b77a493bSEric Paris if (length) 1109b77a493bSEric Paris goto out; 11101da177e4SLinus Torvalds 11110619f0f5SStephen Smalley length = security_sid_to_context(state, newsid, &newcon, &len); 1112b77a493bSEric Paris if (length) 1113b77a493bSEric Paris goto out; 11141da177e4SLinus Torvalds 11151da177e4SLinus Torvalds length = -ERANGE; 1116b77a493bSEric Paris if (len > SIMPLE_TRANSACTION_LIMIT) 1117b77a493bSEric Paris goto out; 11181da177e4SLinus Torvalds 11191da177e4SLinus Torvalds memcpy(buf, newcon, len); 11201da177e4SLinus Torvalds length = len; 11211da177e4SLinus Torvalds out: 1122b77a493bSEric Paris kfree(newcon); 1123b77a493bSEric Paris kfree(tcon); 11241da177e4SLinus Torvalds kfree(scon); 11251da177e4SLinus Torvalds return length; 11261da177e4SLinus Torvalds } 11271da177e4SLinus Torvalds 11281da177e4SLinus Torvalds static ssize_t sel_write_user(struct file *file, char *buf, size_t size) 11291da177e4SLinus Torvalds { 11300619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 11310619f0f5SStephen Smalley struct selinux_state *state = fsi->state; 1132b77a493bSEric Paris char *con = NULL, *user = NULL, *ptr; 1133b77a493bSEric Paris u32 sid, *sids = NULL; 11341da177e4SLinus Torvalds ssize_t length; 11351da177e4SLinus Torvalds char *newcon; 11361da177e4SLinus Torvalds int i, rc; 11371da177e4SLinus Torvalds u32 len, nsids; 11381da177e4SLinus Torvalds 11396b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 11406b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 1141be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_USER, 1142be0554c9SStephen Smalley NULL); 11431da177e4SLinus Torvalds if (length) 11446eab04a8SJustin P. Mattock goto out; 11451da177e4SLinus Torvalds 11461da177e4SLinus Torvalds length = -ENOMEM; 114789d155efSJames Morris con = kzalloc(size + 1, GFP_KERNEL); 11481da177e4SLinus Torvalds if (!con) 11496eab04a8SJustin P. Mattock goto out; 11501da177e4SLinus Torvalds 1151b77a493bSEric Paris length = -ENOMEM; 115289d155efSJames Morris user = kzalloc(size + 1, GFP_KERNEL); 11531da177e4SLinus Torvalds if (!user) 11541da177e4SLinus Torvalds goto out; 11551da177e4SLinus Torvalds 11561da177e4SLinus Torvalds length = -EINVAL; 11571da177e4SLinus Torvalds if (sscanf(buf, "%s %s", con, user) != 2) 1158b77a493bSEric Paris goto out; 11591da177e4SLinus Torvalds 11600619f0f5SStephen Smalley length = security_context_str_to_sid(state, con, &sid, GFP_KERNEL); 1161b77a493bSEric Paris if (length) 1162b77a493bSEric Paris goto out; 11631da177e4SLinus Torvalds 11640619f0f5SStephen Smalley length = security_get_user_sids(state, sid, user, &sids, &nsids); 1165b77a493bSEric Paris if (length) 1166b77a493bSEric Paris goto out; 11671da177e4SLinus Torvalds 11681da177e4SLinus Torvalds length = sprintf(buf, "%u", nsids) + 1; 11691da177e4SLinus Torvalds ptr = buf + length; 11701da177e4SLinus Torvalds for (i = 0; i < nsids; i++) { 11710619f0f5SStephen Smalley rc = security_sid_to_context(state, sids[i], &newcon, &len); 11721da177e4SLinus Torvalds if (rc) { 11731da177e4SLinus Torvalds length = rc; 1174b77a493bSEric Paris goto out; 11751da177e4SLinus Torvalds } 11761da177e4SLinus Torvalds if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) { 11771da177e4SLinus Torvalds kfree(newcon); 11781da177e4SLinus Torvalds length = -ERANGE; 1179b77a493bSEric Paris goto out; 11801da177e4SLinus Torvalds } 11811da177e4SLinus Torvalds memcpy(ptr, newcon, len); 11821da177e4SLinus Torvalds kfree(newcon); 11831da177e4SLinus Torvalds ptr += len; 11841da177e4SLinus Torvalds length += len; 11851da177e4SLinus Torvalds } 11861da177e4SLinus Torvalds out: 1187b77a493bSEric Paris kfree(sids); 1188b77a493bSEric Paris kfree(user); 11891da177e4SLinus Torvalds kfree(con); 11901da177e4SLinus Torvalds return length; 11911da177e4SLinus Torvalds } 11921da177e4SLinus Torvalds 11931da177e4SLinus Torvalds static ssize_t sel_write_member(struct file *file, char *buf, size_t size) 11941da177e4SLinus Torvalds { 11950619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 11960619f0f5SStephen Smalley struct selinux_state *state = fsi->state; 1197b77a493bSEric Paris char *scon = NULL, *tcon = NULL; 11981da177e4SLinus Torvalds u32 ssid, tsid, newsid; 11991da177e4SLinus Torvalds u16 tclass; 12001da177e4SLinus Torvalds ssize_t length; 1201b77a493bSEric Paris char *newcon = NULL; 12021da177e4SLinus Torvalds u32 len; 12031da177e4SLinus Torvalds 12046b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 12056b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 1206be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_MEMBER, 1207be0554c9SStephen Smalley NULL); 12081da177e4SLinus Torvalds if (length) 1209b77a493bSEric Paris goto out; 12101da177e4SLinus Torvalds 12111da177e4SLinus Torvalds length = -ENOMEM; 121289d155efSJames Morris scon = kzalloc(size + 1, GFP_KERNEL); 12131da177e4SLinus Torvalds if (!scon) 12146eab04a8SJustin P. Mattock goto out; 12151da177e4SLinus Torvalds 1216b77a493bSEric Paris length = -ENOMEM; 121789d155efSJames Morris tcon = kzalloc(size + 1, GFP_KERNEL); 12181da177e4SLinus Torvalds if (!tcon) 12191da177e4SLinus Torvalds goto out; 12201da177e4SLinus Torvalds 12211da177e4SLinus Torvalds length = -EINVAL; 12221da177e4SLinus Torvalds if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) 1223b77a493bSEric Paris goto out; 12241da177e4SLinus Torvalds 12250619f0f5SStephen Smalley length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL); 1226b77a493bSEric Paris if (length) 1227b77a493bSEric Paris goto out; 1228b77a493bSEric Paris 12290619f0f5SStephen Smalley length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL); 1230b77a493bSEric Paris if (length) 1231b77a493bSEric Paris goto out; 12321da177e4SLinus Torvalds 12330619f0f5SStephen Smalley length = security_member_sid(state, ssid, tsid, tclass, &newsid); 1234b77a493bSEric Paris if (length) 1235b77a493bSEric Paris goto out; 12361da177e4SLinus Torvalds 12370619f0f5SStephen Smalley length = security_sid_to_context(state, newsid, &newcon, &len); 1238b77a493bSEric Paris if (length) 1239b77a493bSEric Paris goto out; 12401da177e4SLinus Torvalds 1241b77a493bSEric Paris length = -ERANGE; 12421da177e4SLinus Torvalds if (len > SIMPLE_TRANSACTION_LIMIT) { 1243f8b69a5fSpeter enderborg pr_err("SELinux: %s: context size (%u) exceeds " 1244744ba35eSEric Paris "payload max\n", __func__, len); 1245b77a493bSEric Paris goto out; 12461da177e4SLinus Torvalds } 12471da177e4SLinus Torvalds 12481da177e4SLinus Torvalds memcpy(buf, newcon, len); 12491da177e4SLinus Torvalds length = len; 12501da177e4SLinus Torvalds out: 1251b77a493bSEric Paris kfree(newcon); 1252b77a493bSEric Paris kfree(tcon); 12531da177e4SLinus Torvalds kfree(scon); 12541da177e4SLinus Torvalds return length; 12551da177e4SLinus Torvalds } 12561da177e4SLinus Torvalds 12571da177e4SLinus Torvalds static struct inode *sel_make_inode(struct super_block *sb, int mode) 12581da177e4SLinus Torvalds { 12591da177e4SLinus Torvalds struct inode *ret = new_inode(sb); 12601da177e4SLinus Torvalds 12611da177e4SLinus Torvalds if (ret) { 12621da177e4SLinus Torvalds ret->i_mode = mode; 1263078cd827SDeepa Dinamani ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret); 12641da177e4SLinus Torvalds } 12651da177e4SLinus Torvalds return ret; 12661da177e4SLinus Torvalds } 12671da177e4SLinus Torvalds 12681da177e4SLinus Torvalds static ssize_t sel_read_bool(struct file *filep, char __user *buf, 12691da177e4SLinus Torvalds size_t count, loff_t *ppos) 12701da177e4SLinus Torvalds { 12710619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info; 12721da177e4SLinus Torvalds char *page = NULL; 12731da177e4SLinus Torvalds ssize_t length; 12741da177e4SLinus Torvalds ssize_t ret; 12751da177e4SLinus Torvalds int cur_enforcing; 1276496ad9aaSAl Viro unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK; 1277d313f948SStephen Smalley const char *name = filep->f_path.dentry->d_name.name; 12781da177e4SLinus Torvalds 12799ff9abc4SStephen Smalley mutex_lock(&fsi->state->policy_mutex); 12801da177e4SLinus Torvalds 1281d313f948SStephen Smalley ret = -EINVAL; 12820619f0f5SStephen Smalley if (index >= fsi->bool_num || strcmp(name, 12830619f0f5SStephen Smalley fsi->bool_pending_names[index])) 12840da74120SJann Horn goto out_unlock; 12851da177e4SLinus Torvalds 12861da177e4SLinus Torvalds ret = -ENOMEM; 1287b77a493bSEric Paris page = (char *)get_zeroed_page(GFP_KERNEL); 1288b77a493bSEric Paris if (!page) 12890da74120SJann Horn goto out_unlock; 12901da177e4SLinus Torvalds 12910619f0f5SStephen Smalley cur_enforcing = security_get_bool_value(fsi->state, index); 12921da177e4SLinus Torvalds if (cur_enforcing < 0) { 12931da177e4SLinus Torvalds ret = cur_enforcing; 12940da74120SJann Horn goto out_unlock; 12951da177e4SLinus Torvalds } 12961da177e4SLinus Torvalds length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing, 12970619f0f5SStephen Smalley fsi->bool_pending_values[index]); 12989ff9abc4SStephen Smalley mutex_unlock(&fsi->state->policy_mutex); 12990da74120SJann Horn ret = simple_read_from_buffer(buf, count, ppos, page, length); 13000da74120SJann Horn out_free: 13011da177e4SLinus Torvalds free_page((unsigned long)page); 13021da177e4SLinus Torvalds return ret; 13030da74120SJann Horn 13040da74120SJann Horn out_unlock: 13059ff9abc4SStephen Smalley mutex_unlock(&fsi->state->policy_mutex); 13060da74120SJann Horn goto out_free; 13071da177e4SLinus Torvalds } 13081da177e4SLinus Torvalds 13091da177e4SLinus Torvalds static ssize_t sel_write_bool(struct file *filep, const char __user *buf, 13101da177e4SLinus Torvalds size_t count, loff_t *ppos) 13111da177e4SLinus Torvalds { 13120619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info; 13131da177e4SLinus Torvalds char *page = NULL; 1314d313f948SStephen Smalley ssize_t length; 13151da177e4SLinus Torvalds int new_value; 1316496ad9aaSAl Viro unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK; 1317d313f948SStephen Smalley const char *name = filep->f_path.dentry->d_name.name; 13181da177e4SLinus Torvalds 13190da74120SJann Horn if (count >= PAGE_SIZE) 13200da74120SJann Horn return -ENOMEM; 13210da74120SJann Horn 13220da74120SJann Horn /* No partial writes. */ 13230da74120SJann Horn if (*ppos != 0) 13240da74120SJann Horn return -EINVAL; 13250da74120SJann Horn 13260da74120SJann Horn page = memdup_user_nul(buf, count); 13270da74120SJann Horn if (IS_ERR(page)) 13280da74120SJann Horn return PTR_ERR(page); 13290da74120SJann Horn 13309ff9abc4SStephen Smalley mutex_lock(&fsi->state->policy_mutex); 13311da177e4SLinus Torvalds 13326b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 13336b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 1334be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETBOOL, 1335be0554c9SStephen Smalley NULL); 13361da177e4SLinus Torvalds if (length) 13371da177e4SLinus Torvalds goto out; 13381da177e4SLinus Torvalds 1339d313f948SStephen Smalley length = -EINVAL; 13400619f0f5SStephen Smalley if (index >= fsi->bool_num || strcmp(name, 13410619f0f5SStephen Smalley fsi->bool_pending_names[index])) 1342d313f948SStephen Smalley goto out; 1343d313f948SStephen Smalley 13441da177e4SLinus Torvalds length = -EINVAL; 13451da177e4SLinus Torvalds if (sscanf(page, "%d", &new_value) != 1) 13461da177e4SLinus Torvalds goto out; 13471da177e4SLinus Torvalds 13481da177e4SLinus Torvalds if (new_value) 13491da177e4SLinus Torvalds new_value = 1; 13501da177e4SLinus Torvalds 13510619f0f5SStephen Smalley fsi->bool_pending_values[index] = new_value; 13521da177e4SLinus Torvalds length = count; 13531da177e4SLinus Torvalds 13541da177e4SLinus Torvalds out: 13559ff9abc4SStephen Smalley mutex_unlock(&fsi->state->policy_mutex); 13568365a719SAl Viro kfree(page); 13571da177e4SLinus Torvalds return length; 13581da177e4SLinus Torvalds } 13591da177e4SLinus Torvalds 13609c2e08c5SArjan van de Ven static const struct file_operations sel_bool_ops = { 13611da177e4SLinus Torvalds .read = sel_read_bool, 13621da177e4SLinus Torvalds .write = sel_write_bool, 136357a62c23SArnd Bergmann .llseek = generic_file_llseek, 13641da177e4SLinus Torvalds }; 13651da177e4SLinus Torvalds 13661da177e4SLinus Torvalds static ssize_t sel_commit_bools_write(struct file *filep, 13671da177e4SLinus Torvalds const char __user *buf, 13681da177e4SLinus Torvalds size_t count, loff_t *ppos) 13691da177e4SLinus Torvalds { 13700619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info; 13711da177e4SLinus Torvalds char *page = NULL; 1372d313f948SStephen Smalley ssize_t length; 13731da177e4SLinus Torvalds int new_value; 13741da177e4SLinus Torvalds 13750da74120SJann Horn if (count >= PAGE_SIZE) 13760da74120SJann Horn return -ENOMEM; 13770da74120SJann Horn 13780da74120SJann Horn /* No partial writes. */ 13790da74120SJann Horn if (*ppos != 0) 13800da74120SJann Horn return -EINVAL; 13810da74120SJann Horn 13820da74120SJann Horn page = memdup_user_nul(buf, count); 13830da74120SJann Horn if (IS_ERR(page)) 13840da74120SJann Horn return PTR_ERR(page); 13850da74120SJann Horn 13869ff9abc4SStephen Smalley mutex_lock(&fsi->state->policy_mutex); 13871da177e4SLinus Torvalds 13886b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 13896b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 1390be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETBOOL, 1391be0554c9SStephen Smalley NULL); 13921da177e4SLinus Torvalds if (length) 13931da177e4SLinus Torvalds goto out; 13941da177e4SLinus Torvalds 13951da177e4SLinus Torvalds length = -EINVAL; 13961da177e4SLinus Torvalds if (sscanf(page, "%d", &new_value) != 1) 13971da177e4SLinus Torvalds goto out; 13981da177e4SLinus Torvalds 1399b77a493bSEric Paris length = 0; 14000619f0f5SStephen Smalley if (new_value && fsi->bool_pending_values) 14010619f0f5SStephen Smalley length = security_set_bools(fsi->state, fsi->bool_num, 14020619f0f5SStephen Smalley fsi->bool_pending_values); 14031da177e4SLinus Torvalds 1404b77a493bSEric Paris if (!length) 14051da177e4SLinus Torvalds length = count; 14061da177e4SLinus Torvalds 14071da177e4SLinus Torvalds out: 14089ff9abc4SStephen Smalley mutex_unlock(&fsi->state->policy_mutex); 14098365a719SAl Viro kfree(page); 14101da177e4SLinus Torvalds return length; 14111da177e4SLinus Torvalds } 14121da177e4SLinus Torvalds 14139c2e08c5SArjan van de Ven static const struct file_operations sel_commit_bools_ops = { 14141da177e4SLinus Torvalds .write = sel_commit_bools_write, 141557a62c23SArnd Bergmann .llseek = generic_file_llseek, 14161da177e4SLinus Torvalds }; 14171da177e4SLinus Torvalds 14180c92d7c7SChristopher J. PeBenito static void sel_remove_entries(struct dentry *de) 14191da177e4SLinus Torvalds { 1420ad52184bSAl Viro d_genocide(de); 1421ad52184bSAl Viro shrink_dcache_parent(de); 14221da177e4SLinus Torvalds } 14231da177e4SLinus Torvalds 142466ec384aSDaniel Burgener static int sel_make_bools(struct selinux_policy *newpolicy, struct dentry *bool_dir, 142566ec384aSDaniel Burgener unsigned int *bool_num, char ***bool_pending_names, 1426c3fae2b2SChristian Göttsche int **bool_pending_values) 14271da177e4SLinus Torvalds { 142860abd318SOndrej Mosnacek int ret; 14291da177e4SLinus Torvalds ssize_t len; 14301da177e4SLinus Torvalds struct dentry *dentry = NULL; 14311da177e4SLinus Torvalds struct inode *inode = NULL; 14321da177e4SLinus Torvalds struct inode_security_struct *isec; 14331da177e4SLinus Torvalds char **names = NULL, *page; 143460abd318SOndrej Mosnacek u32 i, num; 14351da177e4SLinus Torvalds int *values = NULL; 14361da177e4SLinus Torvalds u32 sid; 14371da177e4SLinus Torvalds 1438b77a493bSEric Paris ret = -ENOMEM; 14391872981bSEric Paris page = (char *)get_zeroed_page(GFP_KERNEL); 14401872981bSEric Paris if (!page) 1441b77a493bSEric Paris goto out; 14421da177e4SLinus Torvalds 144302a52c5cSStephen Smalley ret = security_get_bools(newpolicy, &num, &names, &values); 1444b77a493bSEric Paris if (ret) 14451da177e4SLinus Torvalds goto out; 14461da177e4SLinus Torvalds 14471da177e4SLinus Torvalds for (i = 0; i < num; i++) { 1448b77a493bSEric Paris ret = -ENOMEM; 144966ec384aSDaniel Burgener dentry = d_alloc_name(bool_dir, names[i]); 1450b77a493bSEric Paris if (!dentry) 1451b77a493bSEric Paris goto out; 14521da177e4SLinus Torvalds 1453b77a493bSEric Paris ret = -ENOMEM; 145466ec384aSDaniel Burgener inode = sel_make_inode(bool_dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR); 14557e4237faSnixiaoming if (!inode) { 14567e4237faSnixiaoming dput(dentry); 1457b77a493bSEric Paris goto out; 14587e4237faSnixiaoming } 1459b77a493bSEric Paris 14601da177e4SLinus Torvalds ret = -ENAMETOOLONG; 1461cc1dad71SAl Viro len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]); 14627e4237faSnixiaoming if (len >= PAGE_SIZE) { 14637e4237faSnixiaoming dput(dentry); 14647e4237faSnixiaoming iput(inode); 1465b77a493bSEric Paris goto out; 14667e4237faSnixiaoming } 1467b77a493bSEric Paris 146880788c22SCasey Schaufler isec = selinux_inode(inode); 146902a52c5cSStephen Smalley ret = selinux_policy_genfs_sid(newpolicy, "selinuxfs", page, 1470aa8e712cSStephen Smalley SECCLASS_FILE, &sid); 14714262fb51SGary Tierney if (ret) { 1472900fde06SGary Tierney pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n", 1473900fde06SGary Tierney page); 1474900fde06SGary Tierney sid = SECINITSID_SECURITY; 14754262fb51SGary Tierney } 14764262fb51SGary Tierney 14771da177e4SLinus Torvalds isec->sid = sid; 147842059112SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 14791da177e4SLinus Torvalds inode->i_fop = &sel_bool_ops; 1480bce34bc0SJames Carter inode->i_ino = i|SEL_BOOL_INO_OFFSET; 14811da177e4SLinus Torvalds d_add(dentry, inode); 14821da177e4SLinus Torvalds } 148366ec384aSDaniel Burgener *bool_num = num; 148466ec384aSDaniel Burgener *bool_pending_names = names; 148566ec384aSDaniel Burgener *bool_pending_values = values; 1486b77a493bSEric Paris 1487b77a493bSEric Paris free_page((unsigned long)page); 1488b77a493bSEric Paris return 0; 14891da177e4SLinus Torvalds out: 14901da177e4SLinus Torvalds free_page((unsigned long)page); 1491b77a493bSEric Paris 14921da177e4SLinus Torvalds if (names) { 14939a5f04bfSJesper Juhl for (i = 0; i < num; i++) 14941da177e4SLinus Torvalds kfree(names[i]); 14951da177e4SLinus Torvalds kfree(names); 14961da177e4SLinus Torvalds } 149720c19e41SDavi Arnaut kfree(values); 149866ec384aSDaniel Burgener sel_remove_entries(bool_dir); 1499b77a493bSEric Paris 1500b77a493bSEric Paris return ret; 15011da177e4SLinus Torvalds } 15021da177e4SLinus Torvalds 15031da177e4SLinus Torvalds static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf, 15041da177e4SLinus Torvalds size_t count, loff_t *ppos) 15051da177e4SLinus Torvalds { 15066b6bc620SStephen Smalley struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info; 15076b6bc620SStephen Smalley struct selinux_state *state = fsi->state; 15081da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 15091da177e4SLinus Torvalds ssize_t length; 15101da177e4SLinus Torvalds 15116b6bc620SStephen Smalley length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 15126b6bc620SStephen Smalley avc_get_cache_threshold(state->avc)); 15131da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 15141da177e4SLinus Torvalds } 15151da177e4SLinus Torvalds 15161da177e4SLinus Torvalds static ssize_t sel_write_avc_cache_threshold(struct file *file, 15171da177e4SLinus Torvalds const char __user *buf, 15181da177e4SLinus Torvalds size_t count, loff_t *ppos) 15191da177e4SLinus Torvalds 15201da177e4SLinus Torvalds { 15216b6bc620SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 15226b6bc620SStephen Smalley struct selinux_state *state = fsi->state; 15238365a719SAl Viro char *page; 15241da177e4SLinus Torvalds ssize_t ret; 1525309c5fadSHeinrich Schuchardt unsigned int new_value; 15261da177e4SLinus Torvalds 15276b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 15286b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 1529be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETSECPARAM, 1530be0554c9SStephen Smalley NULL); 15311da177e4SLinus Torvalds if (ret) 15328365a719SAl Viro return ret; 1533b77a493bSEric Paris 1534b77a493bSEric Paris if (count >= PAGE_SIZE) 15358365a719SAl Viro return -ENOMEM; 1536b77a493bSEric Paris 1537b77a493bSEric Paris /* No partial writes. */ 1538b77a493bSEric Paris if (*ppos != 0) 15398365a719SAl Viro return -EINVAL; 1540b77a493bSEric Paris 15418365a719SAl Viro page = memdup_user_nul(buf, count); 15428365a719SAl Viro if (IS_ERR(page)) 15438365a719SAl Viro return PTR_ERR(page); 1544b77a493bSEric Paris 1545b77a493bSEric Paris ret = -EINVAL; 1546b77a493bSEric Paris if (sscanf(page, "%u", &new_value) != 1) 1547b77a493bSEric Paris goto out; 1548b77a493bSEric Paris 15496b6bc620SStephen Smalley avc_set_cache_threshold(state->avc, new_value); 1550b77a493bSEric Paris 15511da177e4SLinus Torvalds ret = count; 15521da177e4SLinus Torvalds out: 15538365a719SAl Viro kfree(page); 15541da177e4SLinus Torvalds return ret; 15551da177e4SLinus Torvalds } 15561da177e4SLinus Torvalds 15571da177e4SLinus Torvalds static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf, 15581da177e4SLinus Torvalds size_t count, loff_t *ppos) 15591da177e4SLinus Torvalds { 15606b6bc620SStephen Smalley struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info; 15616b6bc620SStephen Smalley struct selinux_state *state = fsi->state; 15621da177e4SLinus Torvalds char *page; 1563b77a493bSEric Paris ssize_t length; 15641da177e4SLinus Torvalds 15651da177e4SLinus Torvalds page = (char *)__get_free_page(GFP_KERNEL); 1566b77a493bSEric Paris if (!page) 1567b77a493bSEric Paris return -ENOMEM; 1568b77a493bSEric Paris 15696b6bc620SStephen Smalley length = avc_get_hash_stats(state->avc, page); 1570b77a493bSEric Paris if (length >= 0) 1571b77a493bSEric Paris length = simple_read_from_buffer(buf, count, ppos, page, length); 15721da177e4SLinus Torvalds free_page((unsigned long)page); 1573b77a493bSEric Paris 1574b77a493bSEric Paris return length; 15751da177e4SLinus Torvalds } 15761da177e4SLinus Torvalds 157766f8e2f0SJeff Vander Stoep static ssize_t sel_read_sidtab_hash_stats(struct file *filp, char __user *buf, 157866f8e2f0SJeff Vander Stoep size_t count, loff_t *ppos) 157966f8e2f0SJeff Vander Stoep { 158066f8e2f0SJeff Vander Stoep struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info; 158166f8e2f0SJeff Vander Stoep struct selinux_state *state = fsi->state; 158266f8e2f0SJeff Vander Stoep char *page; 158366f8e2f0SJeff Vander Stoep ssize_t length; 158466f8e2f0SJeff Vander Stoep 158566f8e2f0SJeff Vander Stoep page = (char *)__get_free_page(GFP_KERNEL); 158666f8e2f0SJeff Vander Stoep if (!page) 158766f8e2f0SJeff Vander Stoep return -ENOMEM; 158866f8e2f0SJeff Vander Stoep 158966f8e2f0SJeff Vander Stoep length = security_sidtab_hash_stats(state, page); 159066f8e2f0SJeff Vander Stoep if (length >= 0) 159166f8e2f0SJeff Vander Stoep length = simple_read_from_buffer(buf, count, ppos, page, 159266f8e2f0SJeff Vander Stoep length); 159366f8e2f0SJeff Vander Stoep free_page((unsigned long)page); 159466f8e2f0SJeff Vander Stoep 159566f8e2f0SJeff Vander Stoep return length; 159666f8e2f0SJeff Vander Stoep } 159766f8e2f0SJeff Vander Stoep 159866f8e2f0SJeff Vander Stoep static const struct file_operations sel_sidtab_hash_stats_ops = { 159966f8e2f0SJeff Vander Stoep .read = sel_read_sidtab_hash_stats, 160066f8e2f0SJeff Vander Stoep .llseek = generic_file_llseek, 160166f8e2f0SJeff Vander Stoep }; 160266f8e2f0SJeff Vander Stoep 16039c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_threshold_ops = { 16041da177e4SLinus Torvalds .read = sel_read_avc_cache_threshold, 16051da177e4SLinus Torvalds .write = sel_write_avc_cache_threshold, 160657a62c23SArnd Bergmann .llseek = generic_file_llseek, 16071da177e4SLinus Torvalds }; 16081da177e4SLinus Torvalds 16099c2e08c5SArjan van de Ven static const struct file_operations sel_avc_hash_stats_ops = { 16101da177e4SLinus Torvalds .read = sel_read_avc_hash_stats, 161157a62c23SArnd Bergmann .llseek = generic_file_llseek, 16121da177e4SLinus Torvalds }; 16131da177e4SLinus Torvalds 16141da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS 16151da177e4SLinus Torvalds static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx) 16161da177e4SLinus Torvalds { 16171da177e4SLinus Torvalds int cpu; 16181da177e4SLinus Torvalds 16194f4b6c1aSRusty Russell for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) { 16201da177e4SLinus Torvalds if (!cpu_possible(cpu)) 16211da177e4SLinus Torvalds continue; 16221da177e4SLinus Torvalds *idx = cpu + 1; 16231da177e4SLinus Torvalds return &per_cpu(avc_cache_stats, cpu); 16241da177e4SLinus Torvalds } 16258d269a8eSVasily Averin (*idx)++; 16261da177e4SLinus Torvalds return NULL; 16271da177e4SLinus Torvalds } 16281da177e4SLinus Torvalds 16291da177e4SLinus Torvalds static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos) 16301da177e4SLinus Torvalds { 16311da177e4SLinus Torvalds loff_t n = *pos - 1; 16321da177e4SLinus Torvalds 16331da177e4SLinus Torvalds if (*pos == 0) 16341da177e4SLinus Torvalds return SEQ_START_TOKEN; 16351da177e4SLinus Torvalds 16361da177e4SLinus Torvalds return sel_avc_get_stat_idx(&n); 16371da177e4SLinus Torvalds } 16381da177e4SLinus Torvalds 16391da177e4SLinus Torvalds static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos) 16401da177e4SLinus Torvalds { 16411da177e4SLinus Torvalds return sel_avc_get_stat_idx(pos); 16421da177e4SLinus Torvalds } 16431da177e4SLinus Torvalds 16441da177e4SLinus Torvalds static int sel_avc_stats_seq_show(struct seq_file *seq, void *v) 16451da177e4SLinus Torvalds { 16461da177e4SLinus Torvalds struct avc_cache_stats *st = v; 16471da177e4SLinus Torvalds 1648710a0647SMarkus Elfring if (v == SEQ_START_TOKEN) { 1649710a0647SMarkus Elfring seq_puts(seq, 1650710a0647SMarkus Elfring "lookups hits misses allocations reclaims frees\n"); 1651710a0647SMarkus Elfring } else { 1652257313b2SLinus Torvalds unsigned int lookups = st->lookups; 1653257313b2SLinus Torvalds unsigned int misses = st->misses; 1654257313b2SLinus Torvalds unsigned int hits = lookups - misses; 1655257313b2SLinus Torvalds seq_printf(seq, "%u %u %u %u %u %u\n", lookups, 1656257313b2SLinus Torvalds hits, misses, st->allocations, 16571da177e4SLinus Torvalds st->reclaims, st->frees); 1658257313b2SLinus Torvalds } 16591da177e4SLinus Torvalds return 0; 16601da177e4SLinus Torvalds } 16611da177e4SLinus Torvalds 16621da177e4SLinus Torvalds static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v) 16631da177e4SLinus Torvalds { } 16641da177e4SLinus Torvalds 16651996a109SJan Engelhardt static const struct seq_operations sel_avc_cache_stats_seq_ops = { 16661da177e4SLinus Torvalds .start = sel_avc_stats_seq_start, 16671da177e4SLinus Torvalds .next = sel_avc_stats_seq_next, 16681da177e4SLinus Torvalds .show = sel_avc_stats_seq_show, 16691da177e4SLinus Torvalds .stop = sel_avc_stats_seq_stop, 16701da177e4SLinus Torvalds }; 16711da177e4SLinus Torvalds 16721da177e4SLinus Torvalds static int sel_open_avc_cache_stats(struct inode *inode, struct file *file) 16731da177e4SLinus Torvalds { 16741da177e4SLinus Torvalds return seq_open(file, &sel_avc_cache_stats_seq_ops); 16751da177e4SLinus Torvalds } 16761da177e4SLinus Torvalds 16779c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_stats_ops = { 16781da177e4SLinus Torvalds .open = sel_open_avc_cache_stats, 16791da177e4SLinus Torvalds .read = seq_read, 16801da177e4SLinus Torvalds .llseek = seq_lseek, 16811da177e4SLinus Torvalds .release = seq_release, 16821da177e4SLinus Torvalds }; 16831da177e4SLinus Torvalds #endif 16841da177e4SLinus Torvalds 16851da177e4SLinus Torvalds static int sel_make_avc_files(struct dentry *dir) 16861da177e4SLinus Torvalds { 16870619f0f5SStephen Smalley struct super_block *sb = dir->d_sb; 16880619f0f5SStephen Smalley struct selinux_fs_info *fsi = sb->s_fs_info; 1689b77a493bSEric Paris int i; 1690cda37124SEric Biggers static const struct tree_descr files[] = { 16911da177e4SLinus Torvalds { "cache_threshold", 16921da177e4SLinus Torvalds &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR }, 16931da177e4SLinus Torvalds { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO }, 16941da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS 16951da177e4SLinus Torvalds { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO }, 16961da177e4SLinus Torvalds #endif 16971da177e4SLinus Torvalds }; 16981da177e4SLinus Torvalds 16996e20a64aSNicolas Kaiser for (i = 0; i < ARRAY_SIZE(files); i++) { 17001da177e4SLinus Torvalds struct inode *inode; 17011da177e4SLinus Torvalds struct dentry *dentry; 17021da177e4SLinus Torvalds 17031da177e4SLinus Torvalds dentry = d_alloc_name(dir, files[i].name); 1704b77a493bSEric Paris if (!dentry) 1705b77a493bSEric Paris return -ENOMEM; 17061da177e4SLinus Torvalds 17071da177e4SLinus Torvalds inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode); 17087e4237faSnixiaoming if (!inode) { 17097e4237faSnixiaoming dput(dentry); 1710b77a493bSEric Paris return -ENOMEM; 17117e4237faSnixiaoming } 1712b77a493bSEric Paris 17131da177e4SLinus Torvalds inode->i_fop = files[i].ops; 17140619f0f5SStephen Smalley inode->i_ino = ++fsi->last_ino; 17151da177e4SLinus Torvalds d_add(dentry, inode); 17161da177e4SLinus Torvalds } 1717b77a493bSEric Paris 1718b77a493bSEric Paris return 0; 17191da177e4SLinus Torvalds } 17201da177e4SLinus Torvalds 172166f8e2f0SJeff Vander Stoep static int sel_make_ss_files(struct dentry *dir) 172266f8e2f0SJeff Vander Stoep { 172366f8e2f0SJeff Vander Stoep struct super_block *sb = dir->d_sb; 172466f8e2f0SJeff Vander Stoep struct selinux_fs_info *fsi = sb->s_fs_info; 172566f8e2f0SJeff Vander Stoep int i; 172666f8e2f0SJeff Vander Stoep static struct tree_descr files[] = { 172766f8e2f0SJeff Vander Stoep { "sidtab_hash_stats", &sel_sidtab_hash_stats_ops, S_IRUGO }, 172866f8e2f0SJeff Vander Stoep }; 172966f8e2f0SJeff Vander Stoep 173066f8e2f0SJeff Vander Stoep for (i = 0; i < ARRAY_SIZE(files); i++) { 173166f8e2f0SJeff Vander Stoep struct inode *inode; 173266f8e2f0SJeff Vander Stoep struct dentry *dentry; 173366f8e2f0SJeff Vander Stoep 173466f8e2f0SJeff Vander Stoep dentry = d_alloc_name(dir, files[i].name); 173566f8e2f0SJeff Vander Stoep if (!dentry) 173666f8e2f0SJeff Vander Stoep return -ENOMEM; 173766f8e2f0SJeff Vander Stoep 173866f8e2f0SJeff Vander Stoep inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode); 173966f8e2f0SJeff Vander Stoep if (!inode) { 174066f8e2f0SJeff Vander Stoep dput(dentry); 174166f8e2f0SJeff Vander Stoep return -ENOMEM; 174266f8e2f0SJeff Vander Stoep } 174366f8e2f0SJeff Vander Stoep 174466f8e2f0SJeff Vander Stoep inode->i_fop = files[i].ops; 174566f8e2f0SJeff Vander Stoep inode->i_ino = ++fsi->last_ino; 174666f8e2f0SJeff Vander Stoep d_add(dentry, inode); 174766f8e2f0SJeff Vander Stoep } 174866f8e2f0SJeff Vander Stoep 174966f8e2f0SJeff Vander Stoep return 0; 175066f8e2f0SJeff Vander Stoep } 175166f8e2f0SJeff Vander Stoep 1752f0ee2e46SJames Carter static ssize_t sel_read_initcon(struct file *file, char __user *buf, 1753f0ee2e46SJames Carter size_t count, loff_t *ppos) 1754f0ee2e46SJames Carter { 17550619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 1756f0ee2e46SJames Carter char *con; 1757f0ee2e46SJames Carter u32 sid, len; 1758f0ee2e46SJames Carter ssize_t ret; 1759f0ee2e46SJames Carter 1760496ad9aaSAl Viro sid = file_inode(file)->i_ino&SEL_INO_MASK; 17610619f0f5SStephen Smalley ret = security_sid_to_context(fsi->state, sid, &con, &len); 1762b77a493bSEric Paris if (ret) 1763f0ee2e46SJames Carter return ret; 1764f0ee2e46SJames Carter 1765f0ee2e46SJames Carter ret = simple_read_from_buffer(buf, count, ppos, con, len); 1766f0ee2e46SJames Carter kfree(con); 1767f0ee2e46SJames Carter return ret; 1768f0ee2e46SJames Carter } 1769f0ee2e46SJames Carter 1770f0ee2e46SJames Carter static const struct file_operations sel_initcon_ops = { 1771f0ee2e46SJames Carter .read = sel_read_initcon, 177257a62c23SArnd Bergmann .llseek = generic_file_llseek, 1773f0ee2e46SJames Carter }; 1774f0ee2e46SJames Carter 1775f0ee2e46SJames Carter static int sel_make_initcon_files(struct dentry *dir) 1776f0ee2e46SJames Carter { 1777b77a493bSEric Paris int i; 1778f0ee2e46SJames Carter 1779f0ee2e46SJames Carter for (i = 1; i <= SECINITSID_NUM; i++) { 1780f0ee2e46SJames Carter struct inode *inode; 1781f0ee2e46SJames Carter struct dentry *dentry; 1782e3e0b582SStephen Smalley const char *s = security_get_initial_sid_context(i); 1783e3e0b582SStephen Smalley 1784e3e0b582SStephen Smalley if (!s) 1785e3e0b582SStephen Smalley continue; 1786e3e0b582SStephen Smalley dentry = d_alloc_name(dir, s); 1787b77a493bSEric Paris if (!dentry) 1788b77a493bSEric Paris return -ENOMEM; 1789f0ee2e46SJames Carter 1790f0ee2e46SJames Carter inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); 17917e4237faSnixiaoming if (!inode) { 17927e4237faSnixiaoming dput(dentry); 1793b77a493bSEric Paris return -ENOMEM; 17947e4237faSnixiaoming } 1795b77a493bSEric Paris 1796f0ee2e46SJames Carter inode->i_fop = &sel_initcon_ops; 1797f0ee2e46SJames Carter inode->i_ino = i|SEL_INITCON_INO_OFFSET; 1798f0ee2e46SJames Carter d_add(dentry, inode); 1799f0ee2e46SJames Carter } 1800b77a493bSEric Paris 1801b77a493bSEric Paris return 0; 1802f0ee2e46SJames Carter } 1803f0ee2e46SJames Carter 1804e47c8fc5SChristopher J. PeBenito static inline unsigned long sel_class_to_ino(u16 class) 1805e47c8fc5SChristopher J. PeBenito { 1806e47c8fc5SChristopher J. PeBenito return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET; 1807e47c8fc5SChristopher J. PeBenito } 1808e47c8fc5SChristopher J. PeBenito 1809e47c8fc5SChristopher J. PeBenito static inline u16 sel_ino_to_class(unsigned long ino) 1810e47c8fc5SChristopher J. PeBenito { 181192ae9e82SEric Paris return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1); 1812e47c8fc5SChristopher J. PeBenito } 1813e47c8fc5SChristopher J. PeBenito 1814e47c8fc5SChristopher J. PeBenito static inline unsigned long sel_perm_to_ino(u16 class, u32 perm) 1815e47c8fc5SChristopher J. PeBenito { 1816e47c8fc5SChristopher J. PeBenito return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET; 1817e47c8fc5SChristopher J. PeBenito } 1818e47c8fc5SChristopher J. PeBenito 1819e47c8fc5SChristopher J. PeBenito static inline u32 sel_ino_to_perm(unsigned long ino) 1820e47c8fc5SChristopher J. PeBenito { 1821e47c8fc5SChristopher J. PeBenito return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1); 1822e47c8fc5SChristopher J. PeBenito } 1823e47c8fc5SChristopher J. PeBenito 1824e47c8fc5SChristopher J. PeBenito static ssize_t sel_read_class(struct file *file, char __user *buf, 1825e47c8fc5SChristopher J. PeBenito size_t count, loff_t *ppos) 1826e47c8fc5SChristopher J. PeBenito { 1827496ad9aaSAl Viro unsigned long ino = file_inode(file)->i_ino; 1828cc1dad71SAl Viro char res[TMPBUFLEN]; 18297e78c875Sliuyang34 ssize_t len = scnprintf(res, sizeof(res), "%d", sel_ino_to_class(ino)); 1830cc1dad71SAl Viro return simple_read_from_buffer(buf, count, ppos, res, len); 1831e47c8fc5SChristopher J. PeBenito } 1832e47c8fc5SChristopher J. PeBenito 1833e47c8fc5SChristopher J. PeBenito static const struct file_operations sel_class_ops = { 1834e47c8fc5SChristopher J. PeBenito .read = sel_read_class, 183557a62c23SArnd Bergmann .llseek = generic_file_llseek, 1836e47c8fc5SChristopher J. PeBenito }; 1837e47c8fc5SChristopher J. PeBenito 1838e47c8fc5SChristopher J. PeBenito static ssize_t sel_read_perm(struct file *file, char __user *buf, 1839e47c8fc5SChristopher J. PeBenito size_t count, loff_t *ppos) 1840e47c8fc5SChristopher J. PeBenito { 1841496ad9aaSAl Viro unsigned long ino = file_inode(file)->i_ino; 1842cc1dad71SAl Viro char res[TMPBUFLEN]; 18437e78c875Sliuyang34 ssize_t len = scnprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino)); 1844cc1dad71SAl Viro return simple_read_from_buffer(buf, count, ppos, res, len); 1845e47c8fc5SChristopher J. PeBenito } 1846e47c8fc5SChristopher J. PeBenito 1847e47c8fc5SChristopher J. PeBenito static const struct file_operations sel_perm_ops = { 1848e47c8fc5SChristopher J. PeBenito .read = sel_read_perm, 184957a62c23SArnd Bergmann .llseek = generic_file_llseek, 1850e47c8fc5SChristopher J. PeBenito }; 1851e47c8fc5SChristopher J. PeBenito 18523bb56b25SPaul Moore static ssize_t sel_read_policycap(struct file *file, char __user *buf, 18533bb56b25SPaul Moore size_t count, loff_t *ppos) 18543bb56b25SPaul Moore { 18550619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 18563bb56b25SPaul Moore int value; 18573bb56b25SPaul Moore char tmpbuf[TMPBUFLEN]; 18583bb56b25SPaul Moore ssize_t length; 1859496ad9aaSAl Viro unsigned long i_ino = file_inode(file)->i_ino; 18603bb56b25SPaul Moore 18610619f0f5SStephen Smalley value = security_policycap_supported(fsi->state, i_ino & SEL_INO_MASK); 18623bb56b25SPaul Moore length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value); 18633bb56b25SPaul Moore 18643bb56b25SPaul Moore return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 18653bb56b25SPaul Moore } 18663bb56b25SPaul Moore 18673bb56b25SPaul Moore static const struct file_operations sel_policycap_ops = { 18683bb56b25SPaul Moore .read = sel_read_policycap, 186957a62c23SArnd Bergmann .llseek = generic_file_llseek, 18703bb56b25SPaul Moore }; 18713bb56b25SPaul Moore 187202a52c5cSStephen Smalley static int sel_make_perm_files(struct selinux_policy *newpolicy, 187302a52c5cSStephen Smalley char *objclass, int classvalue, 1874e47c8fc5SChristopher J. PeBenito struct dentry *dir) 1875e47c8fc5SChristopher J. PeBenito { 1876b77a493bSEric Paris int i, rc, nperms; 1877e47c8fc5SChristopher J. PeBenito char **perms; 1878e47c8fc5SChristopher J. PeBenito 187902a52c5cSStephen Smalley rc = security_get_permissions(newpolicy, objclass, &perms, &nperms); 1880e47c8fc5SChristopher J. PeBenito if (rc) 1881b77a493bSEric Paris return rc; 1882e47c8fc5SChristopher J. PeBenito 1883e47c8fc5SChristopher J. PeBenito for (i = 0; i < nperms; i++) { 1884e47c8fc5SChristopher J. PeBenito struct inode *inode; 1885e47c8fc5SChristopher J. PeBenito struct dentry *dentry; 1886e47c8fc5SChristopher J. PeBenito 1887b77a493bSEric Paris rc = -ENOMEM; 1888e47c8fc5SChristopher J. PeBenito dentry = d_alloc_name(dir, perms[i]); 1889b77a493bSEric Paris if (!dentry) 1890b77a493bSEric Paris goto out; 1891e47c8fc5SChristopher J. PeBenito 1892e47c8fc5SChristopher J. PeBenito rc = -ENOMEM; 1893b77a493bSEric Paris inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); 18947e4237faSnixiaoming if (!inode) { 18957e4237faSnixiaoming dput(dentry); 1896b77a493bSEric Paris goto out; 18977e4237faSnixiaoming } 1898b77a493bSEric Paris 1899e47c8fc5SChristopher J. PeBenito inode->i_fop = &sel_perm_ops; 1900e47c8fc5SChristopher J. PeBenito /* i+1 since perm values are 1-indexed */ 1901e47c8fc5SChristopher J. PeBenito inode->i_ino = sel_perm_to_ino(classvalue, i + 1); 1902e47c8fc5SChristopher J. PeBenito d_add(dentry, inode); 1903e47c8fc5SChristopher J. PeBenito } 1904b77a493bSEric Paris rc = 0; 1905b77a493bSEric Paris out: 1906e47c8fc5SChristopher J. PeBenito for (i = 0; i < nperms; i++) 1907e47c8fc5SChristopher J. PeBenito kfree(perms[i]); 1908e47c8fc5SChristopher J. PeBenito kfree(perms); 1909e47c8fc5SChristopher J. PeBenito return rc; 1910e47c8fc5SChristopher J. PeBenito } 1911e47c8fc5SChristopher J. PeBenito 191202a52c5cSStephen Smalley static int sel_make_class_dir_entries(struct selinux_policy *newpolicy, 191302a52c5cSStephen Smalley char *classname, int index, 1914e47c8fc5SChristopher J. PeBenito struct dentry *dir) 1915e47c8fc5SChristopher J. PeBenito { 19160619f0f5SStephen Smalley struct super_block *sb = dir->d_sb; 19170619f0f5SStephen Smalley struct selinux_fs_info *fsi = sb->s_fs_info; 1918e47c8fc5SChristopher J. PeBenito struct dentry *dentry = NULL; 1919e47c8fc5SChristopher J. PeBenito struct inode *inode = NULL; 1920e47c8fc5SChristopher J. PeBenito 1921e47c8fc5SChristopher J. PeBenito dentry = d_alloc_name(dir, "index"); 1922b77a493bSEric Paris if (!dentry) 1923b77a493bSEric Paris return -ENOMEM; 1924e47c8fc5SChristopher J. PeBenito 1925e47c8fc5SChristopher J. PeBenito inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); 19267e4237faSnixiaoming if (!inode) { 19277e4237faSnixiaoming dput(dentry); 1928b77a493bSEric Paris return -ENOMEM; 19297e4237faSnixiaoming } 1930e47c8fc5SChristopher J. PeBenito 1931e47c8fc5SChristopher J. PeBenito inode->i_fop = &sel_class_ops; 1932e47c8fc5SChristopher J. PeBenito inode->i_ino = sel_class_to_ino(index); 1933e47c8fc5SChristopher J. PeBenito d_add(dentry, inode); 1934e47c8fc5SChristopher J. PeBenito 19350619f0f5SStephen Smalley dentry = sel_make_dir(dir, "perms", &fsi->last_class_ino); 1936a1c2aa1eSAl Viro if (IS_ERR(dentry)) 1937a1c2aa1eSAl Viro return PTR_ERR(dentry); 1938e47c8fc5SChristopher J. PeBenito 19395698f081Sye xingchen return sel_make_perm_files(newpolicy, classname, index, dentry); 1940e47c8fc5SChristopher J. PeBenito } 1941e47c8fc5SChristopher J. PeBenito 194266ec384aSDaniel Burgener static int sel_make_classes(struct selinux_policy *newpolicy, 194366ec384aSDaniel Burgener struct dentry *class_dir, 194466ec384aSDaniel Burgener unsigned long *last_class_ino) 1945e47c8fc5SChristopher J. PeBenito { 19460619f0f5SStephen Smalley 1947b77a493bSEric Paris int rc, nclasses, i; 1948e47c8fc5SChristopher J. PeBenito char **classes; 1949e47c8fc5SChristopher J. PeBenito 195002a52c5cSStephen Smalley rc = security_get_classes(newpolicy, &classes, &nclasses); 1951b77a493bSEric Paris if (rc) 1952b77a493bSEric Paris return rc; 1953e47c8fc5SChristopher J. PeBenito 1954e47c8fc5SChristopher J. PeBenito /* +2 since classes are 1-indexed */ 195566ec384aSDaniel Burgener *last_class_ino = sel_class_to_ino(nclasses + 2); 1956e47c8fc5SChristopher J. PeBenito 1957e47c8fc5SChristopher J. PeBenito for (i = 0; i < nclasses; i++) { 1958e47c8fc5SChristopher J. PeBenito struct dentry *class_name_dir; 1959e47c8fc5SChristopher J. PeBenito 196066ec384aSDaniel Burgener class_name_dir = sel_make_dir(class_dir, classes[i], 196166ec384aSDaniel Burgener last_class_ino); 1962a1c2aa1eSAl Viro if (IS_ERR(class_name_dir)) { 1963a1c2aa1eSAl Viro rc = PTR_ERR(class_name_dir); 1964b77a493bSEric Paris goto out; 1965a1c2aa1eSAl Viro } 1966e47c8fc5SChristopher J. PeBenito 1967e47c8fc5SChristopher J. PeBenito /* i+1 since class values are 1-indexed */ 196802a52c5cSStephen Smalley rc = sel_make_class_dir_entries(newpolicy, classes[i], i + 1, 1969e47c8fc5SChristopher J. PeBenito class_name_dir); 1970e47c8fc5SChristopher J. PeBenito if (rc) 1971b77a493bSEric Paris goto out; 1972e47c8fc5SChristopher J. PeBenito } 1973b77a493bSEric Paris rc = 0; 1974b77a493bSEric Paris out: 1975e47c8fc5SChristopher J. PeBenito for (i = 0; i < nclasses; i++) 1976e47c8fc5SChristopher J. PeBenito kfree(classes[i]); 1977e47c8fc5SChristopher J. PeBenito kfree(classes); 1978e47c8fc5SChristopher J. PeBenito return rc; 1979e47c8fc5SChristopher J. PeBenito } 1980e47c8fc5SChristopher J. PeBenito 19810619f0f5SStephen Smalley static int sel_make_policycap(struct selinux_fs_info *fsi) 19823bb56b25SPaul Moore { 19833bb56b25SPaul Moore unsigned int iter; 19843bb56b25SPaul Moore struct dentry *dentry = NULL; 19853bb56b25SPaul Moore struct inode *inode = NULL; 19863bb56b25SPaul Moore 1987cdbec3edSPaul Moore for (iter = 0; iter <= POLICYDB_CAP_MAX; iter++) { 19884dc2fce3SStephen Smalley if (iter < ARRAY_SIZE(selinux_policycap_names)) 19890619f0f5SStephen Smalley dentry = d_alloc_name(fsi->policycap_dir, 19904dc2fce3SStephen Smalley selinux_policycap_names[iter]); 19913bb56b25SPaul Moore else 19920619f0f5SStephen Smalley dentry = d_alloc_name(fsi->policycap_dir, "unknown"); 19933bb56b25SPaul Moore 19943bb56b25SPaul Moore if (dentry == NULL) 19953bb56b25SPaul Moore return -ENOMEM; 19963bb56b25SPaul Moore 19970619f0f5SStephen Smalley inode = sel_make_inode(fsi->sb, S_IFREG | 0444); 19987e4237faSnixiaoming if (inode == NULL) { 19997e4237faSnixiaoming dput(dentry); 20003bb56b25SPaul Moore return -ENOMEM; 20017e4237faSnixiaoming } 20023bb56b25SPaul Moore 20033bb56b25SPaul Moore inode->i_fop = &sel_policycap_ops; 20043bb56b25SPaul Moore inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET; 20053bb56b25SPaul Moore d_add(dentry, inode); 20063bb56b25SPaul Moore } 20073bb56b25SPaul Moore 20083bb56b25SPaul Moore return 0; 20093bb56b25SPaul Moore } 20103bb56b25SPaul Moore 2011a1c2aa1eSAl Viro static struct dentry *sel_make_dir(struct dentry *dir, const char *name, 20120dd4ae51SChristopher J. PeBenito unsigned long *ino) 20131da177e4SLinus Torvalds { 2014a1c2aa1eSAl Viro struct dentry *dentry = d_alloc_name(dir, name); 20151da177e4SLinus Torvalds struct inode *inode; 20161da177e4SLinus Torvalds 2017a1c2aa1eSAl Viro if (!dentry) 2018a1c2aa1eSAl Viro return ERR_PTR(-ENOMEM); 2019a1c2aa1eSAl Viro 2020a1c2aa1eSAl Viro inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO); 2021a1c2aa1eSAl Viro if (!inode) { 2022a1c2aa1eSAl Viro dput(dentry); 2023a1c2aa1eSAl Viro return ERR_PTR(-ENOMEM); 2024a1c2aa1eSAl Viro } 2025b77a493bSEric Paris 20261da177e4SLinus Torvalds inode->i_op = &simple_dir_inode_operations; 20271da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations; 20280dd4ae51SChristopher J. PeBenito inode->i_ino = ++(*ino); 202940e906f8SJames Morris /* directory inodes start off with i_nlink == 2 (for "." entry) */ 2030d8c76e6fSDave Hansen inc_nlink(inode); 20311da177e4SLinus Torvalds d_add(dentry, inode); 2032edb20fb5SJames Morris /* bump link count on parent directory, too */ 2033ce0b16ddSDavid Howells inc_nlink(d_inode(dir)); 2034b77a493bSEric Paris 2035a1c2aa1eSAl Viro return dentry; 20361da177e4SLinus Torvalds } 20371da177e4SLinus Torvalds 20380eea6091SDaniel Burgener static struct dentry *sel_make_disconnected_dir(struct super_block *sb, 20390eea6091SDaniel Burgener unsigned long *ino) 20400eea6091SDaniel Burgener { 20410eea6091SDaniel Burgener struct inode *inode = sel_make_inode(sb, S_IFDIR | S_IRUGO | S_IXUGO); 20420eea6091SDaniel Burgener 20430eea6091SDaniel Burgener if (!inode) 20440eea6091SDaniel Burgener return ERR_PTR(-ENOMEM); 20450eea6091SDaniel Burgener 20460eea6091SDaniel Burgener inode->i_op = &simple_dir_inode_operations; 20470eea6091SDaniel Burgener inode->i_fop = &simple_dir_operations; 20480eea6091SDaniel Burgener inode->i_ino = ++(*ino); 20490eea6091SDaniel Burgener /* directory inodes start off with i_nlink == 2 (for "." entry) */ 20500eea6091SDaniel Burgener inc_nlink(inode); 20510eea6091SDaniel Burgener return d_obtain_alias(inode); 20520eea6091SDaniel Burgener } 20530eea6091SDaniel Burgener 20540619f0f5SStephen Smalley #define NULL_FILE_NAME "null" 20550619f0f5SStephen Smalley 2056920f50b2SDavid Howells static int sel_fill_super(struct super_block *sb, struct fs_context *fc) 20571da177e4SLinus Torvalds { 20580619f0f5SStephen Smalley struct selinux_fs_info *fsi; 20591da177e4SLinus Torvalds int ret; 20601da177e4SLinus Torvalds struct dentry *dentry; 2061a1c2aa1eSAl Viro struct inode *inode; 20621da177e4SLinus Torvalds struct inode_security_struct *isec; 20631da177e4SLinus Torvalds 2064cda37124SEric Biggers static const struct tree_descr selinux_files[] = { 20651da177e4SLinus Torvalds [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR}, 20661da177e4SLinus Torvalds [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR}, 2067ce9982d0SStephen Smalley [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO}, 20681da177e4SLinus Torvalds [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO}, 20691da177e4SLinus Torvalds [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO}, 20701da177e4SLinus Torvalds [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO}, 20711da177e4SLinus Torvalds [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO}, 20721da177e4SLinus Torvalds [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO}, 20731da177e4SLinus Torvalds [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR}, 20741da177e4SLinus Torvalds [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO}, 20751da177e4SLinus Torvalds [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR}, 20761da177e4SLinus Torvalds [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO}, 20771da177e4SLinus Torvalds [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR}, 20783f12070eSEric Paris [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO}, 20793f12070eSEric Paris [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO}, 208011904167SKaiGai Kohei [SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO}, 208172e8c859SEric Paris [SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO}, 2082f9df6458SAndrew Perepechko [SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops, 2083f9df6458SAndrew Perepechko S_IWUGO}, 20841da177e4SLinus Torvalds /* last one */ {""} 20851da177e4SLinus Torvalds }; 20860619f0f5SStephen Smalley 20870619f0f5SStephen Smalley ret = selinux_fs_info_create(sb); 20880619f0f5SStephen Smalley if (ret) 20890619f0f5SStephen Smalley goto err; 20900619f0f5SStephen Smalley 20911da177e4SLinus Torvalds ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files); 20921da177e4SLinus Torvalds if (ret) 2093161ce45aSJames Morris goto err; 20941da177e4SLinus Torvalds 20950619f0f5SStephen Smalley fsi = sb->s_fs_info; 20960619f0f5SStephen Smalley fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino); 20970619f0f5SStephen Smalley if (IS_ERR(fsi->bool_dir)) { 20980619f0f5SStephen Smalley ret = PTR_ERR(fsi->bool_dir); 20990619f0f5SStephen Smalley fsi->bool_dir = NULL; 2100161ce45aSJames Morris goto err; 2101a1c2aa1eSAl Viro } 21021da177e4SLinus Torvalds 2103b77a493bSEric Paris ret = -ENOMEM; 21041da177e4SLinus Torvalds dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME); 2105b77a493bSEric Paris if (!dentry) 2106161ce45aSJames Morris goto err; 21071da177e4SLinus Torvalds 2108161ce45aSJames Morris ret = -ENOMEM; 2109b77a493bSEric Paris inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO); 21107e4237faSnixiaoming if (!inode) { 21117e4237faSnixiaoming dput(dentry); 2112161ce45aSJames Morris goto err; 21137e4237faSnixiaoming } 2114b77a493bSEric Paris 21150619f0f5SStephen Smalley inode->i_ino = ++fsi->last_ino; 211680788c22SCasey Schaufler isec = selinux_inode(inode); 21171da177e4SLinus Torvalds isec->sid = SECINITSID_DEVNULL; 21181da177e4SLinus Torvalds isec->sclass = SECCLASS_CHR_FILE; 211942059112SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 21201da177e4SLinus Torvalds 21211da177e4SLinus Torvalds init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3)); 21221da177e4SLinus Torvalds d_add(dentry, inode); 21231da177e4SLinus Torvalds 21240619f0f5SStephen Smalley dentry = sel_make_dir(sb->s_root, "avc", &fsi->last_ino); 2125a1c2aa1eSAl Viro if (IS_ERR(dentry)) { 2126a1c2aa1eSAl Viro ret = PTR_ERR(dentry); 2127161ce45aSJames Morris goto err; 2128a1c2aa1eSAl Viro } 21291da177e4SLinus Torvalds 21301da177e4SLinus Torvalds ret = sel_make_avc_files(dentry); 2131bcb62828SChristian Göttsche if (ret) 2132bcb62828SChristian Göttsche goto err; 213366f8e2f0SJeff Vander Stoep 213466f8e2f0SJeff Vander Stoep dentry = sel_make_dir(sb->s_root, "ss", &fsi->last_ino); 213566f8e2f0SJeff Vander Stoep if (IS_ERR(dentry)) { 213666f8e2f0SJeff Vander Stoep ret = PTR_ERR(dentry); 213766f8e2f0SJeff Vander Stoep goto err; 213866f8e2f0SJeff Vander Stoep } 213966f8e2f0SJeff Vander Stoep 214066f8e2f0SJeff Vander Stoep ret = sel_make_ss_files(dentry); 21411da177e4SLinus Torvalds if (ret) 2142161ce45aSJames Morris goto err; 2143f0ee2e46SJames Carter 21440619f0f5SStephen Smalley dentry = sel_make_dir(sb->s_root, "initial_contexts", &fsi->last_ino); 2145a1c2aa1eSAl Viro if (IS_ERR(dentry)) { 2146a1c2aa1eSAl Viro ret = PTR_ERR(dentry); 2147f0ee2e46SJames Carter goto err; 2148a1c2aa1eSAl Viro } 2149f0ee2e46SJames Carter 2150f0ee2e46SJames Carter ret = sel_make_initcon_files(dentry); 2151f0ee2e46SJames Carter if (ret) 2152f0ee2e46SJames Carter goto err; 2153f0ee2e46SJames Carter 2154613ba187SDaniel Burgener fsi->class_dir = sel_make_dir(sb->s_root, CLASS_DIR_NAME, &fsi->last_ino); 21550619f0f5SStephen Smalley if (IS_ERR(fsi->class_dir)) { 21560619f0f5SStephen Smalley ret = PTR_ERR(fsi->class_dir); 21570619f0f5SStephen Smalley fsi->class_dir = NULL; 2158e47c8fc5SChristopher J. PeBenito goto err; 2159a1c2aa1eSAl Viro } 2160e47c8fc5SChristopher J. PeBenito 2161613ba187SDaniel Burgener fsi->policycap_dir = sel_make_dir(sb->s_root, POLICYCAP_DIR_NAME, 21620619f0f5SStephen Smalley &fsi->last_ino); 21630619f0f5SStephen Smalley if (IS_ERR(fsi->policycap_dir)) { 21640619f0f5SStephen Smalley ret = PTR_ERR(fsi->policycap_dir); 21650619f0f5SStephen Smalley fsi->policycap_dir = NULL; 2166e47c8fc5SChristopher J. PeBenito goto err; 2167a1c2aa1eSAl Viro } 21680619f0f5SStephen Smalley 216902a52c5cSStephen Smalley ret = sel_make_policycap(fsi); 217002a52c5cSStephen Smalley if (ret) { 217102a52c5cSStephen Smalley pr_err("SELinux: failed to load policy capabilities\n"); 21720619f0f5SStephen Smalley goto err; 217302a52c5cSStephen Smalley } 217402a52c5cSStephen Smalley 2175b77a493bSEric Paris return 0; 2176161ce45aSJames Morris err: 2177f8b69a5fSpeter enderborg pr_err("SELinux: %s: failed while creating inodes\n", 2178744ba35eSEric Paris __func__); 21790619f0f5SStephen Smalley 21800619f0f5SStephen Smalley selinux_fs_info_free(sb); 21810619f0f5SStephen Smalley 2182b77a493bSEric Paris return ret; 21831da177e4SLinus Torvalds } 21841da177e4SLinus Torvalds 2185920f50b2SDavid Howells static int sel_get_tree(struct fs_context *fc) 21861da177e4SLinus Torvalds { 2187920f50b2SDavid Howells return get_tree_single(fc, sel_fill_super); 2188920f50b2SDavid Howells } 2189920f50b2SDavid Howells 2190920f50b2SDavid Howells static const struct fs_context_operations sel_context_ops = { 2191920f50b2SDavid Howells .get_tree = sel_get_tree, 2192920f50b2SDavid Howells }; 2193920f50b2SDavid Howells 2194920f50b2SDavid Howells static int sel_init_fs_context(struct fs_context *fc) 2195920f50b2SDavid Howells { 2196920f50b2SDavid Howells fc->ops = &sel_context_ops; 2197920f50b2SDavid Howells return 0; 21981da177e4SLinus Torvalds } 21991da177e4SLinus Torvalds 22000619f0f5SStephen Smalley static void sel_kill_sb(struct super_block *sb) 22010619f0f5SStephen Smalley { 22020619f0f5SStephen Smalley selinux_fs_info_free(sb); 22030619f0f5SStephen Smalley kill_litter_super(sb); 22040619f0f5SStephen Smalley } 22050619f0f5SStephen Smalley 22061da177e4SLinus Torvalds static struct file_system_type sel_fs_type = { 22071da177e4SLinus Torvalds .name = "selinuxfs", 2208920f50b2SDavid Howells .init_fs_context = sel_init_fs_context, 22090619f0f5SStephen Smalley .kill_sb = sel_kill_sb, 22101da177e4SLinus Torvalds }; 22111da177e4SLinus Torvalds 2212cd2bb4cbSOndrej Mosnacek static struct vfsmount *selinuxfs_mount __ro_after_init; 2213cd2bb4cbSOndrej Mosnacek struct path selinux_null __ro_after_init; 22141da177e4SLinus Torvalds 22151da177e4SLinus Torvalds static int __init init_sel_fs(void) 22161da177e4SLinus Torvalds { 22170619f0f5SStephen Smalley struct qstr null_name = QSTR_INIT(NULL_FILE_NAME, 22180619f0f5SStephen Smalley sizeof(NULL_FILE_NAME)-1); 22191da177e4SLinus Torvalds int err; 22201da177e4SLinus Torvalds 22216c5a682eSStephen Smalley if (!selinux_enabled_boot) 22221da177e4SLinus Torvalds return 0; 22237a627e3bSGreg Kroah-Hartman 2224f9bb4882SEric W. Biederman err = sysfs_create_mount_point(fs_kobj, "selinux"); 2225f9bb4882SEric W. Biederman if (err) 2226f9bb4882SEric W. Biederman return err; 22277a627e3bSGreg Kroah-Hartman 22281da177e4SLinus Torvalds err = register_filesystem(&sel_fs_type); 22297a627e3bSGreg Kroah-Hartman if (err) { 2230f9bb4882SEric W. Biederman sysfs_remove_mount_point(fs_kobj, "selinux"); 2231b77a493bSEric Paris return err; 22327a627e3bSGreg Kroah-Hartman } 2233b77a493bSEric Paris 2234765927b2SAl Viro selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type); 22351da177e4SLinus Torvalds if (IS_ERR(selinuxfs_mount)) { 2236f8b69a5fSpeter enderborg pr_err("selinuxfs: could not mount!\n"); 22371da177e4SLinus Torvalds err = PTR_ERR(selinuxfs_mount); 22381da177e4SLinus Torvalds selinuxfs_mount = NULL; 22391da177e4SLinus Torvalds } 22400619f0f5SStephen Smalley selinux_null.dentry = d_hash_and_lookup(selinux_null.mnt->mnt_root, 22410619f0f5SStephen Smalley &null_name); 22420619f0f5SStephen Smalley if (IS_ERR(selinux_null.dentry)) { 22430619f0f5SStephen Smalley pr_err("selinuxfs: could not lookup null!\n"); 22440619f0f5SStephen Smalley err = PTR_ERR(selinux_null.dentry); 22450619f0f5SStephen Smalley selinux_null.dentry = NULL; 22460619f0f5SStephen Smalley } 2247b77a493bSEric Paris 22481da177e4SLinus Torvalds return err; 22491da177e4SLinus Torvalds } 22501da177e4SLinus Torvalds 22511da177e4SLinus Torvalds __initcall(init_sel_fs); 22521da177e4SLinus Torvalds 22531da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 22541da177e4SLinus Torvalds void exit_sel_fs(void) 22551da177e4SLinus Torvalds { 2256f9bb4882SEric W. Biederman sysfs_remove_mount_point(fs_kobj, "selinux"); 2257fd40ffc7SStephen Smalley dput(selinux_null.dentry); 2258423e0ab0STim Chen kern_unmount(selinuxfs_mount); 22591da177e4SLinus Torvalds unregister_filesystem(&sel_fs_type); 22601da177e4SLinus Torvalds } 22611da177e4SLinus Torvalds #endif 2262