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; 740619f0f5SStephen Smalley unsigned 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"); 29689b223bfSPaul Moore 297bfd51626SDavi Arnaut if (count >= PAGE_SIZE) 2988365a719SAl Viro return -ENOMEM; 299b77a493bSEric Paris 3001da177e4SLinus Torvalds /* No partial writes. */ 301b77a493bSEric Paris if (*ppos != 0) 3028365a719SAl Viro return -EINVAL; 303b77a493bSEric Paris 3048365a719SAl Viro page = memdup_user_nul(buf, count); 3058365a719SAl Viro if (IS_ERR(page)) 3068365a719SAl Viro return PTR_ERR(page); 3071da177e4SLinus Torvalds 3081da177e4SLinus Torvalds length = -EINVAL; 3091da177e4SLinus Torvalds if (sscanf(page, "%d", &new_value) != 1) 3101da177e4SLinus Torvalds goto out; 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds if (new_value) { 3134195ed42SRichard Guy Briggs enforcing = enforcing_enabled(fsi->state); 3140619f0f5SStephen Smalley length = selinux_disable(fsi->state); 315b77a493bSEric Paris if (length) 3161da177e4SLinus Torvalds goto out; 317cdfb6b34SRichard Guy Briggs audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS, 3184195ed42SRichard Guy Briggs "enforcing=%d old_enforcing=%d auid=%u ses=%u" 3196c5a682eSStephen Smalley " enabled=0 old-enabled=1 lsm=selinux res=1", 3204195ed42SRichard Guy Briggs enforcing, enforcing, 321581abc09SEric W. Biederman from_kuid(&init_user_ns, audit_get_loginuid(current)), 3226c5a682eSStephen Smalley audit_get_sessionid(current)); 3231da177e4SLinus Torvalds } 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds length = count; 3261da177e4SLinus Torvalds out: 3278365a719SAl Viro kfree(page); 3281da177e4SLinus Torvalds return length; 3291da177e4SLinus Torvalds } 3301da177e4SLinus Torvalds #else 3311da177e4SLinus Torvalds #define sel_write_disable NULL 3321da177e4SLinus Torvalds #endif 3331da177e4SLinus Torvalds 3349c2e08c5SArjan van de Ven static const struct file_operations sel_disable_ops = { 3351da177e4SLinus Torvalds .write = sel_write_disable, 33657a62c23SArnd Bergmann .llseek = generic_file_llseek, 3371da177e4SLinus Torvalds }; 3381da177e4SLinus Torvalds 3391da177e4SLinus Torvalds static ssize_t sel_read_policyvers(struct file *filp, char __user *buf, 3401da177e4SLinus Torvalds size_t count, loff_t *ppos) 3411da177e4SLinus Torvalds { 3421da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 3431da177e4SLinus Torvalds ssize_t length; 3441da177e4SLinus Torvalds 3451da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX); 3461da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 3471da177e4SLinus Torvalds } 3481da177e4SLinus Torvalds 3499c2e08c5SArjan van de Ven static const struct file_operations sel_policyvers_ops = { 3501da177e4SLinus Torvalds .read = sel_read_policyvers, 35157a62c23SArnd Bergmann .llseek = generic_file_llseek, 3521da177e4SLinus Torvalds }; 3531da177e4SLinus Torvalds 3541da177e4SLinus Torvalds /* declaration for sel_write_load */ 35566ec384aSDaniel Burgener static int sel_make_bools(struct selinux_policy *newpolicy, struct dentry *bool_dir, 35666ec384aSDaniel Burgener unsigned int *bool_num, char ***bool_pending_names, 35766ec384aSDaniel Burgener unsigned int **bool_pending_values); 35866ec384aSDaniel Burgener static int sel_make_classes(struct selinux_policy *newpolicy, 35966ec384aSDaniel Burgener struct dentry *class_dir, 36066ec384aSDaniel Burgener unsigned long *last_class_ino); 361e47c8fc5SChristopher J. PeBenito 362e47c8fc5SChristopher J. PeBenito /* declaration for sel_make_class_dirs */ 363a1c2aa1eSAl Viro static struct dentry *sel_make_dir(struct dentry *dir, const char *name, 364e47c8fc5SChristopher J. PeBenito unsigned long *ino); 3651da177e4SLinus Torvalds 3660eea6091SDaniel Burgener /* declaration for sel_make_policy_nodes */ 3670eea6091SDaniel Burgener static struct dentry *sel_make_disconnected_dir(struct super_block *sb, 3680eea6091SDaniel Burgener unsigned long *ino); 3690eea6091SDaniel Burgener 3700eea6091SDaniel Burgener /* declaration for sel_make_policy_nodes */ 371aeecf4a3SDaniel Burgener static void sel_remove_entries(struct dentry *de); 372aeecf4a3SDaniel Burgener 3731da177e4SLinus Torvalds static ssize_t sel_read_mls(struct file *filp, char __user *buf, 3741da177e4SLinus Torvalds size_t count, loff_t *ppos) 3751da177e4SLinus Torvalds { 3760619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info; 3771da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 3781da177e4SLinus Torvalds ssize_t length; 3791da177e4SLinus Torvalds 3800719aaf5SGuido Trentalancia length = scnprintf(tmpbuf, TMPBUFLEN, "%d", 3810619f0f5SStephen Smalley security_mls_enabled(fsi->state)); 3821da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 3831da177e4SLinus Torvalds } 3841da177e4SLinus Torvalds 3859c2e08c5SArjan van de Ven static const struct file_operations sel_mls_ops = { 3861da177e4SLinus Torvalds .read = sel_read_mls, 38757a62c23SArnd Bergmann .llseek = generic_file_llseek, 3881da177e4SLinus Torvalds }; 3891da177e4SLinus Torvalds 390cee74f47SEric Paris struct policy_load_memory { 391cee74f47SEric Paris size_t len; 392cee74f47SEric Paris void *data; 393cee74f47SEric Paris }; 394cee74f47SEric Paris 395cee74f47SEric Paris static int sel_open_policy(struct inode *inode, struct file *filp) 396cee74f47SEric Paris { 3970619f0f5SStephen Smalley struct selinux_fs_info *fsi = inode->i_sb->s_fs_info; 3980619f0f5SStephen Smalley struct selinux_state *state = fsi->state; 399cee74f47SEric Paris struct policy_load_memory *plm = NULL; 400cee74f47SEric Paris int rc; 401cee74f47SEric Paris 402cee74f47SEric Paris BUG_ON(filp->private_data); 403cee74f47SEric Paris 4049ff9abc4SStephen Smalley mutex_lock(&fsi->state->policy_mutex); 405cee74f47SEric Paris 4066b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 4076b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 408be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL); 409cee74f47SEric Paris if (rc) 410cee74f47SEric Paris goto err; 411cee74f47SEric Paris 412cee74f47SEric Paris rc = -EBUSY; 4130619f0f5SStephen Smalley if (fsi->policy_opened) 414cee74f47SEric Paris goto err; 415cee74f47SEric Paris 416cee74f47SEric Paris rc = -ENOMEM; 417cee74f47SEric Paris plm = kzalloc(sizeof(*plm), GFP_KERNEL); 418cee74f47SEric Paris if (!plm) 419cee74f47SEric Paris goto err; 420cee74f47SEric Paris 4210619f0f5SStephen Smalley rc = security_read_policy(state, &plm->data, &plm->len); 422cee74f47SEric Paris if (rc) 423cee74f47SEric Paris goto err; 424cee74f47SEric Paris 42566ccd256SOndrej Mosnacek if ((size_t)i_size_read(inode) != plm->len) { 42666ccd256SOndrej Mosnacek inode_lock(inode); 42766ccd256SOndrej Mosnacek i_size_write(inode, plm->len); 42866ccd256SOndrej Mosnacek inode_unlock(inode); 42966ccd256SOndrej Mosnacek } 43066ccd256SOndrej Mosnacek 4310619f0f5SStephen Smalley fsi->policy_opened = 1; 432cee74f47SEric Paris 433cee74f47SEric Paris filp->private_data = plm; 434cee74f47SEric Paris 4359ff9abc4SStephen Smalley mutex_unlock(&fsi->state->policy_mutex); 436cee74f47SEric Paris 437cee74f47SEric Paris return 0; 438cee74f47SEric Paris err: 4399ff9abc4SStephen Smalley mutex_unlock(&fsi->state->policy_mutex); 440cee74f47SEric Paris 441cee74f47SEric Paris if (plm) 442cee74f47SEric Paris vfree(plm->data); 443cee74f47SEric Paris kfree(plm); 444cee74f47SEric Paris return rc; 445cee74f47SEric Paris } 446cee74f47SEric Paris 447cee74f47SEric Paris static int sel_release_policy(struct inode *inode, struct file *filp) 448cee74f47SEric Paris { 4490619f0f5SStephen Smalley struct selinux_fs_info *fsi = inode->i_sb->s_fs_info; 450cee74f47SEric Paris struct policy_load_memory *plm = filp->private_data; 451cee74f47SEric Paris 452cee74f47SEric Paris BUG_ON(!plm); 453cee74f47SEric Paris 4540619f0f5SStephen Smalley fsi->policy_opened = 0; 455cee74f47SEric Paris 456cee74f47SEric Paris vfree(plm->data); 457cee74f47SEric Paris kfree(plm); 458cee74f47SEric Paris 459cee74f47SEric Paris return 0; 460cee74f47SEric Paris } 461cee74f47SEric Paris 462cee74f47SEric Paris static ssize_t sel_read_policy(struct file *filp, char __user *buf, 463cee74f47SEric Paris size_t count, loff_t *ppos) 464cee74f47SEric Paris { 465cee74f47SEric Paris struct policy_load_memory *plm = filp->private_data; 466cee74f47SEric Paris int ret; 467cee74f47SEric Paris 4686b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 4696b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 470be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL); 471cee74f47SEric Paris if (ret) 472cee74f47SEric Paris return ret; 4730da74120SJann Horn 4740da74120SJann Horn return simple_read_from_buffer(buf, count, ppos, plm->data, plm->len); 475cee74f47SEric Paris } 476cee74f47SEric Paris 477ac9a1f6dSSouptick Joarder static vm_fault_t sel_mmap_policy_fault(struct vm_fault *vmf) 478845ca30fSEric Paris { 47911bac800SDave Jiang struct policy_load_memory *plm = vmf->vma->vm_file->private_data; 480845ca30fSEric Paris unsigned long offset; 481845ca30fSEric Paris struct page *page; 482845ca30fSEric Paris 483845ca30fSEric Paris if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE)) 484845ca30fSEric Paris return VM_FAULT_SIGBUS; 485845ca30fSEric Paris 486845ca30fSEric Paris offset = vmf->pgoff << PAGE_SHIFT; 487845ca30fSEric Paris if (offset >= roundup(plm->len, PAGE_SIZE)) 488845ca30fSEric Paris return VM_FAULT_SIGBUS; 489845ca30fSEric Paris 490845ca30fSEric Paris page = vmalloc_to_page(plm->data + offset); 491845ca30fSEric Paris get_page(page); 492845ca30fSEric Paris 493845ca30fSEric Paris vmf->page = page; 494845ca30fSEric Paris 495845ca30fSEric Paris return 0; 496845ca30fSEric Paris } 497845ca30fSEric Paris 4987cbea8dcSKirill A. Shutemov static const struct vm_operations_struct sel_mmap_policy_ops = { 499845ca30fSEric Paris .fault = sel_mmap_policy_fault, 500845ca30fSEric Paris .page_mkwrite = sel_mmap_policy_fault, 501845ca30fSEric Paris }; 502845ca30fSEric Paris 503ad3fa08cSJames Morris static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma) 504845ca30fSEric Paris { 505845ca30fSEric Paris if (vma->vm_flags & VM_SHARED) { 506845ca30fSEric Paris /* do not allow mprotect to make mapping writable */ 507845ca30fSEric Paris vma->vm_flags &= ~VM_MAYWRITE; 508845ca30fSEric Paris 509845ca30fSEric Paris if (vma->vm_flags & VM_WRITE) 510845ca30fSEric Paris return -EACCES; 511845ca30fSEric Paris } 512845ca30fSEric Paris 513314e51b9SKonstantin Khlebnikov vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; 514845ca30fSEric Paris vma->vm_ops = &sel_mmap_policy_ops; 515845ca30fSEric Paris 516845ca30fSEric Paris return 0; 517845ca30fSEric Paris } 518845ca30fSEric Paris 519cee74f47SEric Paris static const struct file_operations sel_policy_ops = { 520cee74f47SEric Paris .open = sel_open_policy, 521cee74f47SEric Paris .read = sel_read_policy, 522845ca30fSEric Paris .mmap = sel_mmap_policy, 523cee74f47SEric Paris .release = sel_release_policy, 52447a93a5bSEric Paris .llseek = generic_file_llseek, 525cee74f47SEric Paris }; 526cee74f47SEric Paris 5270eea6091SDaniel Burgener static void sel_remove_old_bool_data(unsigned int bool_num, char **bool_names, 5280eea6091SDaniel Burgener unsigned int *bool_values) 529aeecf4a3SDaniel Burgener { 530aeecf4a3SDaniel Burgener u32 i; 531aeecf4a3SDaniel Burgener 532aeecf4a3SDaniel Burgener /* bool_dir cleanup */ 5330eea6091SDaniel Burgener for (i = 0; i < bool_num; i++) 5340eea6091SDaniel Burgener kfree(bool_names[i]); 5350eea6091SDaniel Burgener kfree(bool_names); 5360eea6091SDaniel Burgener kfree(bool_values); 537aeecf4a3SDaniel Burgener } 538aeecf4a3SDaniel Burgener 53902a52c5cSStephen Smalley static int sel_make_policy_nodes(struct selinux_fs_info *fsi, 54002a52c5cSStephen Smalley struct selinux_policy *newpolicy) 5410619f0f5SStephen Smalley { 5420eea6091SDaniel Burgener int ret = 0; 5430eea6091SDaniel Burgener struct dentry *tmp_parent, *tmp_bool_dir, *tmp_class_dir, *old_dentry; 5440eea6091SDaniel Burgener unsigned int tmp_bool_num, old_bool_num; 5450eea6091SDaniel Burgener char **tmp_bool_names, **old_bool_names; 5460eea6091SDaniel Burgener unsigned int *tmp_bool_values, *old_bool_values; 5470eea6091SDaniel Burgener unsigned long tmp_ino = fsi->last_ino; /* Don't increment last_ino in this function */ 5480619f0f5SStephen Smalley 5490eea6091SDaniel Burgener tmp_parent = sel_make_disconnected_dir(fsi->sb, &tmp_ino); 5500eea6091SDaniel Burgener if (IS_ERR(tmp_parent)) 5510eea6091SDaniel Burgener return PTR_ERR(tmp_parent); 552aeecf4a3SDaniel Burgener 5530eea6091SDaniel Burgener tmp_ino = fsi->bool_dir->d_inode->i_ino - 1; /* sel_make_dir will increment and set */ 5540eea6091SDaniel Burgener tmp_bool_dir = sel_make_dir(tmp_parent, BOOL_DIR_NAME, &tmp_ino); 5550eea6091SDaniel Burgener if (IS_ERR(tmp_bool_dir)) { 5560eea6091SDaniel Burgener ret = PTR_ERR(tmp_bool_dir); 5570eea6091SDaniel Burgener goto out; 5580619f0f5SStephen Smalley } 5590619f0f5SStephen Smalley 5600eea6091SDaniel Burgener tmp_ino = fsi->class_dir->d_inode->i_ino - 1; /* sel_make_dir will increment and set */ 5610eea6091SDaniel Burgener tmp_class_dir = sel_make_dir(tmp_parent, CLASS_DIR_NAME, &tmp_ino); 5620eea6091SDaniel Burgener if (IS_ERR(tmp_class_dir)) { 5630eea6091SDaniel Burgener ret = PTR_ERR(tmp_class_dir); 5640eea6091SDaniel Burgener goto out; 5650eea6091SDaniel Burgener } 5660eea6091SDaniel Burgener 5670eea6091SDaniel Burgener ret = sel_make_bools(newpolicy, tmp_bool_dir, &tmp_bool_num, 5680eea6091SDaniel Burgener &tmp_bool_names, &tmp_bool_values); 569ee5de60aSOndrej Mosnacek if (ret) 5700eea6091SDaniel Burgener goto out; 5710eea6091SDaniel Burgener 5720eea6091SDaniel Burgener ret = sel_make_classes(newpolicy, tmp_class_dir, 57366ec384aSDaniel Burgener &fsi->last_class_ino); 574ee5de60aSOndrej Mosnacek if (ret) 5750eea6091SDaniel Burgener goto out; 5760619f0f5SStephen Smalley 5770eea6091SDaniel Burgener /* booleans */ 5780eea6091SDaniel Burgener old_dentry = fsi->bool_dir; 5790eea6091SDaniel Burgener lock_rename(tmp_bool_dir, old_dentry); 5800eea6091SDaniel Burgener d_exchange(tmp_bool_dir, fsi->bool_dir); 5810eea6091SDaniel Burgener 5820eea6091SDaniel Burgener old_bool_num = fsi->bool_num; 5830eea6091SDaniel Burgener old_bool_names = fsi->bool_pending_names; 5840eea6091SDaniel Burgener old_bool_values = fsi->bool_pending_values; 5850eea6091SDaniel Burgener 5860eea6091SDaniel Burgener fsi->bool_num = tmp_bool_num; 5870eea6091SDaniel Burgener fsi->bool_pending_names = tmp_bool_names; 5880eea6091SDaniel Burgener fsi->bool_pending_values = tmp_bool_values; 5890eea6091SDaniel Burgener 5900eea6091SDaniel Burgener sel_remove_old_bool_data(old_bool_num, old_bool_names, old_bool_values); 5910eea6091SDaniel Burgener 5920eea6091SDaniel Burgener fsi->bool_dir = tmp_bool_dir; 5930eea6091SDaniel Burgener unlock_rename(tmp_bool_dir, old_dentry); 5940eea6091SDaniel Burgener 5950eea6091SDaniel Burgener /* classes */ 5960eea6091SDaniel Burgener old_dentry = fsi->class_dir; 5970eea6091SDaniel Burgener lock_rename(tmp_class_dir, old_dentry); 5980eea6091SDaniel Burgener d_exchange(tmp_class_dir, fsi->class_dir); 5990eea6091SDaniel Burgener fsi->class_dir = tmp_class_dir; 6000eea6091SDaniel Burgener unlock_rename(tmp_class_dir, old_dentry); 6010eea6091SDaniel Burgener 6020eea6091SDaniel Burgener out: 6030eea6091SDaniel Burgener /* Since the other temporary dirs are children of tmp_parent 6040eea6091SDaniel Burgener * this will handle all the cleanup in the case of a failure before 6050eea6091SDaniel Burgener * the swapover 6060eea6091SDaniel Burgener */ 6070eea6091SDaniel Burgener sel_remove_entries(tmp_parent); 6080eea6091SDaniel Burgener dput(tmp_parent); /* d_genocide() only handles the children */ 6090eea6091SDaniel Burgener 6100eea6091SDaniel Burgener return ret; 6110619f0f5SStephen Smalley } 6120619f0f5SStephen Smalley 6131da177e4SLinus Torvalds static ssize_t sel_write_load(struct file *file, const char __user *buf, 6141da177e4SLinus Torvalds size_t count, loff_t *ppos) 6151da177e4SLinus Torvalds 6161da177e4SLinus Torvalds { 6170619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 6186406887aSOndrej Mosnacek struct selinux_load_state load_state; 6191da177e4SLinus Torvalds ssize_t length; 6201da177e4SLinus Torvalds void *data = NULL; 6211da177e4SLinus Torvalds 6229ff9abc4SStephen Smalley mutex_lock(&fsi->state->policy_mutex); 6231da177e4SLinus Torvalds 6246b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 6256b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 626be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL); 6271da177e4SLinus Torvalds if (length) 6281da177e4SLinus Torvalds goto out; 6291da177e4SLinus Torvalds 6301da177e4SLinus Torvalds /* No partial writes. */ 6311da177e4SLinus Torvalds length = -EINVAL; 632b77a493bSEric Paris if (*ppos != 0) 6331da177e4SLinus Torvalds goto out; 6341da177e4SLinus Torvalds 635b77a493bSEric Paris length = -ENOMEM; 636b77a493bSEric Paris data = vmalloc(count); 637b77a493bSEric Paris if (!data) 638b77a493bSEric Paris goto out; 6391da177e4SLinus Torvalds 6401da177e4SLinus Torvalds length = -EFAULT; 6411da177e4SLinus Torvalds if (copy_from_user(data, buf, count) != 0) 6421da177e4SLinus Torvalds goto out; 6431da177e4SLinus Torvalds 6446406887aSOndrej Mosnacek length = security_load_policy(fsi->state, data, count, &load_state); 6454262fb51SGary Tierney if (length) { 6464262fb51SGary Tierney pr_warn_ratelimited("SELinux: failed to load policy\n"); 6471da177e4SLinus Torvalds goto out; 6484262fb51SGary Tierney } 6491da177e4SLinus Torvalds 6506406887aSOndrej Mosnacek length = sel_make_policy_nodes(fsi, load_state.policy); 65102a52c5cSStephen Smalley if (length) { 652ee5de60aSOndrej Mosnacek pr_warn_ratelimited("SELinux: failed to initialize selinuxfs\n"); 6536406887aSOndrej Mosnacek selinux_policy_cancel(fsi->state, &load_state); 654519dad3bSOndrej Mosnacek goto out; 65502a52c5cSStephen Smalley } 65602a52c5cSStephen Smalley 6576406887aSOndrej Mosnacek selinux_policy_commit(fsi->state, &load_state); 658b77a493bSEric Paris 6591da177e4SLinus Torvalds length = count; 660e47c8fc5SChristopher J. PeBenito 661cdfb6b34SRichard Guy Briggs audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_POLICY_LOAD, 662d141136fSRichard Guy Briggs "auid=%u ses=%u lsm=selinux res=1", 663581abc09SEric W. Biederman from_kuid(&init_user_ns, audit_get_loginuid(current)), 6644746ec5bSEric Paris audit_get_sessionid(current)); 6651da177e4SLinus Torvalds out: 6669ff9abc4SStephen Smalley mutex_unlock(&fsi->state->policy_mutex); 6671da177e4SLinus Torvalds vfree(data); 6681da177e4SLinus Torvalds return length; 6691da177e4SLinus Torvalds } 6701da177e4SLinus Torvalds 6719c2e08c5SArjan van de Ven static const struct file_operations sel_load_ops = { 6721da177e4SLinus Torvalds .write = sel_write_load, 67357a62c23SArnd Bergmann .llseek = generic_file_llseek, 6741da177e4SLinus Torvalds }; 6751da177e4SLinus Torvalds 676ce9982d0SStephen Smalley static ssize_t sel_write_context(struct file *file, char *buf, size_t size) 6771da177e4SLinus Torvalds { 6780619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 6790619f0f5SStephen Smalley struct selinux_state *state = fsi->state; 680b77a493bSEric Paris char *canon = NULL; 681ce9982d0SStephen Smalley u32 sid, len; 6821da177e4SLinus Torvalds ssize_t length; 6831da177e4SLinus Torvalds 6846b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 6856b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 686be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__CHECK_CONTEXT, NULL); 6871da177e4SLinus Torvalds if (length) 688b77a493bSEric Paris goto out; 6891da177e4SLinus Torvalds 6900619f0f5SStephen Smalley length = security_context_to_sid(state, buf, size, &sid, GFP_KERNEL); 691b77a493bSEric Paris if (length) 692b77a493bSEric Paris goto out; 6931da177e4SLinus Torvalds 6940619f0f5SStephen Smalley length = security_sid_to_context(state, sid, &canon, &len); 695b77a493bSEric Paris if (length) 696b77a493bSEric Paris goto out; 697ce9982d0SStephen Smalley 698b77a493bSEric Paris length = -ERANGE; 699ce9982d0SStephen Smalley if (len > SIMPLE_TRANSACTION_LIMIT) { 700f8b69a5fSpeter enderborg pr_err("SELinux: %s: context size (%u) exceeds " 701744ba35eSEric Paris "payload max\n", __func__, len); 702ce9982d0SStephen Smalley goto out; 703ce9982d0SStephen Smalley } 704ce9982d0SStephen Smalley 705ce9982d0SStephen Smalley memcpy(buf, canon, len); 706ce9982d0SStephen Smalley length = len; 7071da177e4SLinus Torvalds out: 708ce9982d0SStephen Smalley kfree(canon); 7091da177e4SLinus Torvalds return length; 7101da177e4SLinus Torvalds } 7111da177e4SLinus Torvalds 7121da177e4SLinus Torvalds static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf, 7131da177e4SLinus Torvalds size_t count, loff_t *ppos) 7141da177e4SLinus Torvalds { 7150619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info; 7161da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 7171da177e4SLinus Torvalds ssize_t length; 7181da177e4SLinus Torvalds 7198861d0afSLakshmi Ramasubramanian length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 7208861d0afSLakshmi Ramasubramanian checkreqprot_get(fsi->state)); 7211da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 7221da177e4SLinus Torvalds } 7231da177e4SLinus Torvalds 7241da177e4SLinus Torvalds static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf, 7251da177e4SLinus Torvalds size_t count, loff_t *ppos) 7261da177e4SLinus Torvalds { 7270619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 7288365a719SAl Viro char *page; 7291da177e4SLinus Torvalds ssize_t length; 7301da177e4SLinus Torvalds unsigned int new_value; 7311da177e4SLinus Torvalds 7326b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 7336b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 734be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETCHECKREQPROT, 735be0554c9SStephen Smalley NULL); 7361da177e4SLinus Torvalds if (length) 7378365a719SAl Viro return length; 7381da177e4SLinus Torvalds 739bfd51626SDavi Arnaut if (count >= PAGE_SIZE) 7408365a719SAl Viro return -ENOMEM; 741b77a493bSEric Paris 7421da177e4SLinus Torvalds /* No partial writes. */ 743b77a493bSEric Paris if (*ppos != 0) 7448365a719SAl Viro return -EINVAL; 745b77a493bSEric Paris 7468365a719SAl Viro page = memdup_user_nul(buf, count); 7478365a719SAl Viro if (IS_ERR(page)) 7488365a719SAl Viro return PTR_ERR(page); 7491da177e4SLinus Torvalds 7501da177e4SLinus Torvalds length = -EINVAL; 7511da177e4SLinus Torvalds if (sscanf(page, "%u", &new_value) != 1) 7521da177e4SLinus Torvalds goto out; 7531da177e4SLinus Torvalds 754e9c38f9fSStephen Smalley if (new_value) { 755e9c38f9fSStephen Smalley char comm[sizeof(current->comm)]; 756e9c38f9fSStephen Smalley 757e9c38f9fSStephen Smalley memcpy(comm, current->comm, sizeof(comm)); 758e9c38f9fSStephen Smalley pr_warn_once("SELinux: %s (%d) set checkreqprot to 1. This is deprecated and will be rejected in a future kernel release.\n", 759e9c38f9fSStephen Smalley comm, current->pid); 760e9c38f9fSStephen Smalley } 761e9c38f9fSStephen Smalley 7628861d0afSLakshmi Ramasubramanian checkreqprot_set(fsi->state, (new_value ? 1 : 0)); 7631da177e4SLinus Torvalds length = count; 7642554a48fSLakshmi Ramasubramanian 7652554a48fSLakshmi Ramasubramanian selinux_ima_measure_state(fsi->state); 7662554a48fSLakshmi Ramasubramanian 7671da177e4SLinus Torvalds out: 7688365a719SAl Viro kfree(page); 7691da177e4SLinus Torvalds return length; 7701da177e4SLinus Torvalds } 7719c2e08c5SArjan van de Ven static const struct file_operations sel_checkreqprot_ops = { 7721da177e4SLinus Torvalds .read = sel_read_checkreqprot, 7731da177e4SLinus Torvalds .write = sel_write_checkreqprot, 77457a62c23SArnd Bergmann .llseek = generic_file_llseek, 7751da177e4SLinus Torvalds }; 7761da177e4SLinus Torvalds 777f9df6458SAndrew Perepechko static ssize_t sel_write_validatetrans(struct file *file, 778f9df6458SAndrew Perepechko const char __user *buf, 779f9df6458SAndrew Perepechko size_t count, loff_t *ppos) 780f9df6458SAndrew Perepechko { 7810619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 7820619f0f5SStephen Smalley struct selinux_state *state = fsi->state; 783f9df6458SAndrew Perepechko char *oldcon = NULL, *newcon = NULL, *taskcon = NULL; 784f9df6458SAndrew Perepechko char *req = NULL; 785f9df6458SAndrew Perepechko u32 osid, nsid, tsid; 786f9df6458SAndrew Perepechko u16 tclass; 787f9df6458SAndrew Perepechko int rc; 788f9df6458SAndrew Perepechko 7896b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 7906b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 791be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__VALIDATE_TRANS, NULL); 792f9df6458SAndrew Perepechko if (rc) 793f9df6458SAndrew Perepechko goto out; 794f9df6458SAndrew Perepechko 795f9df6458SAndrew Perepechko rc = -ENOMEM; 796f9df6458SAndrew Perepechko if (count >= PAGE_SIZE) 797f9df6458SAndrew Perepechko goto out; 798f9df6458SAndrew Perepechko 799f9df6458SAndrew Perepechko /* No partial writes. */ 800f9df6458SAndrew Perepechko rc = -EINVAL; 801f9df6458SAndrew Perepechko if (*ppos != 0) 802f9df6458SAndrew Perepechko goto out; 803f9df6458SAndrew Perepechko 8040b884d25SAl Viro req = memdup_user_nul(buf, count); 8050b884d25SAl Viro if (IS_ERR(req)) { 8060b884d25SAl Viro rc = PTR_ERR(req); 8070b884d25SAl Viro req = NULL; 808f9df6458SAndrew Perepechko goto out; 8090b884d25SAl Viro } 810f9df6458SAndrew Perepechko 811f9df6458SAndrew Perepechko rc = -ENOMEM; 812f9df6458SAndrew Perepechko oldcon = kzalloc(count + 1, GFP_KERNEL); 813f9df6458SAndrew Perepechko if (!oldcon) 814f9df6458SAndrew Perepechko goto out; 815f9df6458SAndrew Perepechko 816f9df6458SAndrew Perepechko newcon = kzalloc(count + 1, GFP_KERNEL); 817f9df6458SAndrew Perepechko if (!newcon) 818f9df6458SAndrew Perepechko goto out; 819f9df6458SAndrew Perepechko 820f9df6458SAndrew Perepechko taskcon = kzalloc(count + 1, GFP_KERNEL); 821f9df6458SAndrew Perepechko if (!taskcon) 822f9df6458SAndrew Perepechko goto out; 823f9df6458SAndrew Perepechko 824f9df6458SAndrew Perepechko rc = -EINVAL; 825f9df6458SAndrew Perepechko if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4) 826f9df6458SAndrew Perepechko goto out; 827f9df6458SAndrew Perepechko 8280619f0f5SStephen Smalley rc = security_context_str_to_sid(state, oldcon, &osid, GFP_KERNEL); 829f9df6458SAndrew Perepechko if (rc) 830f9df6458SAndrew Perepechko goto out; 831f9df6458SAndrew Perepechko 8320619f0f5SStephen Smalley rc = security_context_str_to_sid(state, newcon, &nsid, GFP_KERNEL); 833f9df6458SAndrew Perepechko if (rc) 834f9df6458SAndrew Perepechko goto out; 835f9df6458SAndrew Perepechko 8360619f0f5SStephen Smalley rc = security_context_str_to_sid(state, taskcon, &tsid, GFP_KERNEL); 837f9df6458SAndrew Perepechko if (rc) 838f9df6458SAndrew Perepechko goto out; 839f9df6458SAndrew Perepechko 8400619f0f5SStephen Smalley rc = security_validate_transition_user(state, osid, nsid, tsid, tclass); 841f9df6458SAndrew Perepechko if (!rc) 842f9df6458SAndrew Perepechko rc = count; 843f9df6458SAndrew Perepechko out: 844f9df6458SAndrew Perepechko kfree(req); 845f9df6458SAndrew Perepechko kfree(oldcon); 846f9df6458SAndrew Perepechko kfree(newcon); 847f9df6458SAndrew Perepechko kfree(taskcon); 848f9df6458SAndrew Perepechko return rc; 849f9df6458SAndrew Perepechko } 850f9df6458SAndrew Perepechko 851f9df6458SAndrew Perepechko static const struct file_operations sel_transition_ops = { 852f9df6458SAndrew Perepechko .write = sel_write_validatetrans, 853f9df6458SAndrew Perepechko .llseek = generic_file_llseek, 854f9df6458SAndrew Perepechko }; 855f9df6458SAndrew Perepechko 8561da177e4SLinus Torvalds /* 8571da177e4SLinus Torvalds * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c 8581da177e4SLinus Torvalds */ 8591da177e4SLinus Torvalds static ssize_t sel_write_access(struct file *file, char *buf, size_t size); 8601da177e4SLinus Torvalds static ssize_t sel_write_create(struct file *file, char *buf, size_t size); 8611da177e4SLinus Torvalds static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size); 8621da177e4SLinus Torvalds static ssize_t sel_write_user(struct file *file, char *buf, size_t size); 8631da177e4SLinus Torvalds static ssize_t sel_write_member(struct file *file, char *buf, size_t size); 8641da177e4SLinus Torvalds 865631d2b49SEric Biggers static ssize_t (*const write_op[])(struct file *, char *, size_t) = { 8661da177e4SLinus Torvalds [SEL_ACCESS] = sel_write_access, 8671da177e4SLinus Torvalds [SEL_CREATE] = sel_write_create, 8681da177e4SLinus Torvalds [SEL_RELABEL] = sel_write_relabel, 8691da177e4SLinus Torvalds [SEL_USER] = sel_write_user, 8701da177e4SLinus Torvalds [SEL_MEMBER] = sel_write_member, 871ce9982d0SStephen Smalley [SEL_CONTEXT] = sel_write_context, 8721da177e4SLinus Torvalds }; 8731da177e4SLinus Torvalds 8741da177e4SLinus Torvalds static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos) 8751da177e4SLinus Torvalds { 876496ad9aaSAl Viro ino_t ino = file_inode(file)->i_ino; 8771da177e4SLinus Torvalds char *data; 8781da177e4SLinus Torvalds ssize_t rv; 8791da177e4SLinus Torvalds 8806e20a64aSNicolas Kaiser if (ino >= ARRAY_SIZE(write_op) || !write_op[ino]) 8811da177e4SLinus Torvalds return -EINVAL; 8821da177e4SLinus Torvalds 8831da177e4SLinus Torvalds data = simple_transaction_get(file, buf, size); 8841da177e4SLinus Torvalds if (IS_ERR(data)) 8851da177e4SLinus Torvalds return PTR_ERR(data); 8861da177e4SLinus Torvalds 8871da177e4SLinus Torvalds rv = write_op[ino](file, data, size); 8881da177e4SLinus Torvalds if (rv > 0) { 8891da177e4SLinus Torvalds simple_transaction_set(file, rv); 8901da177e4SLinus Torvalds rv = size; 8911da177e4SLinus Torvalds } 8921da177e4SLinus Torvalds return rv; 8931da177e4SLinus Torvalds } 8941da177e4SLinus Torvalds 8959c2e08c5SArjan van de Ven static const struct file_operations transaction_ops = { 8961da177e4SLinus Torvalds .write = selinux_transaction_write, 8971da177e4SLinus Torvalds .read = simple_transaction_read, 8981da177e4SLinus Torvalds .release = simple_transaction_release, 89957a62c23SArnd Bergmann .llseek = generic_file_llseek, 9001da177e4SLinus Torvalds }; 9011da177e4SLinus Torvalds 9021da177e4SLinus Torvalds /* 9031da177e4SLinus Torvalds * payload - write methods 9041da177e4SLinus Torvalds * If the method has a response, the response should be put in buf, 9051da177e4SLinus Torvalds * and the length returned. Otherwise return 0 or and -error. 9061da177e4SLinus Torvalds */ 9071da177e4SLinus Torvalds 9081da177e4SLinus Torvalds static ssize_t sel_write_access(struct file *file, char *buf, size_t size) 9091da177e4SLinus Torvalds { 9100619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 9110619f0f5SStephen Smalley struct selinux_state *state = fsi->state; 912b77a493bSEric Paris char *scon = NULL, *tcon = NULL; 9131da177e4SLinus Torvalds u32 ssid, tsid; 9141da177e4SLinus Torvalds u16 tclass; 9151da177e4SLinus Torvalds struct av_decision avd; 9161da177e4SLinus Torvalds ssize_t length; 9171da177e4SLinus Torvalds 9186b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 9196b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 920be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_AV, NULL); 9211da177e4SLinus Torvalds if (length) 922b77a493bSEric Paris goto out; 9231da177e4SLinus Torvalds 9241da177e4SLinus Torvalds length = -ENOMEM; 92589d155efSJames Morris scon = kzalloc(size + 1, GFP_KERNEL); 9261da177e4SLinus Torvalds if (!scon) 927b77a493bSEric Paris goto out; 9281da177e4SLinus Torvalds 929b77a493bSEric Paris length = -ENOMEM; 93089d155efSJames Morris tcon = kzalloc(size + 1, GFP_KERNEL); 9311da177e4SLinus Torvalds if (!tcon) 9321da177e4SLinus Torvalds goto out; 9331da177e4SLinus Torvalds 9341da177e4SLinus Torvalds length = -EINVAL; 93519439d05SStephen Smalley if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) 936b77a493bSEric Paris goto out; 9371da177e4SLinus Torvalds 9380619f0f5SStephen Smalley length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL); 939b77a493bSEric Paris if (length) 940b77a493bSEric Paris goto out; 941b77a493bSEric Paris 9420619f0f5SStephen Smalley length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL); 943b77a493bSEric Paris if (length) 944b77a493bSEric Paris goto out; 9451da177e4SLinus Torvalds 9460619f0f5SStephen Smalley security_compute_av_user(state, ssid, tsid, tclass, &avd); 9471da177e4SLinus Torvalds 9481da177e4SLinus Torvalds length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, 9498a6f83afSKaiGai Kohei "%x %x %x %x %u %x", 950f1c6381aSEric Paris avd.allowed, 0xffffffff, 9511da177e4SLinus Torvalds avd.auditallow, avd.auditdeny, 9528a6f83afSKaiGai Kohei avd.seqno, avd.flags); 9531da177e4SLinus Torvalds out: 954b77a493bSEric Paris kfree(tcon); 9551da177e4SLinus Torvalds kfree(scon); 9561da177e4SLinus Torvalds return length; 9571da177e4SLinus Torvalds } 9581da177e4SLinus Torvalds 9591da177e4SLinus Torvalds static ssize_t sel_write_create(struct file *file, char *buf, size_t size) 9601da177e4SLinus Torvalds { 9610619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 9620619f0f5SStephen Smalley struct selinux_state *state = fsi->state; 963b77a493bSEric Paris char *scon = NULL, *tcon = NULL; 964f50a3ec9SKohei Kaigai char *namebuf = NULL, *objname = NULL; 9651da177e4SLinus Torvalds u32 ssid, tsid, newsid; 9661da177e4SLinus Torvalds u16 tclass; 9671da177e4SLinus Torvalds ssize_t length; 968b77a493bSEric Paris char *newcon = NULL; 9691da177e4SLinus Torvalds u32 len; 970f50a3ec9SKohei Kaigai int nargs; 9711da177e4SLinus Torvalds 9726b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 9736b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 974be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_CREATE, 975be0554c9SStephen Smalley NULL); 9761da177e4SLinus Torvalds if (length) 977b77a493bSEric Paris goto out; 9781da177e4SLinus Torvalds 9791da177e4SLinus Torvalds length = -ENOMEM; 98089d155efSJames Morris scon = kzalloc(size + 1, GFP_KERNEL); 9811da177e4SLinus Torvalds if (!scon) 982b77a493bSEric Paris goto out; 9831da177e4SLinus Torvalds 984b77a493bSEric Paris length = -ENOMEM; 98589d155efSJames Morris tcon = kzalloc(size + 1, GFP_KERNEL); 9861da177e4SLinus Torvalds if (!tcon) 9871da177e4SLinus Torvalds goto out; 9881da177e4SLinus Torvalds 989f50a3ec9SKohei Kaigai length = -ENOMEM; 990f50a3ec9SKohei Kaigai namebuf = kzalloc(size + 1, GFP_KERNEL); 991f50a3ec9SKohei Kaigai if (!namebuf) 992b77a493bSEric Paris goto out; 9931da177e4SLinus Torvalds 994f50a3ec9SKohei Kaigai length = -EINVAL; 995f50a3ec9SKohei Kaigai nargs = sscanf(buf, "%s %s %hu %s", scon, tcon, &tclass, namebuf); 996f50a3ec9SKohei Kaigai if (nargs < 3 || nargs > 4) 997f50a3ec9SKohei Kaigai goto out; 9980f7e4c33SKohei Kaigai if (nargs == 4) { 9990f7e4c33SKohei Kaigai /* 10000f7e4c33SKohei Kaigai * If and when the name of new object to be queried contains 10010f7e4c33SKohei Kaigai * either whitespace or multibyte characters, they shall be 10020f7e4c33SKohei Kaigai * encoded based on the percentage-encoding rule. 10030f7e4c33SKohei Kaigai * If not encoded, the sscanf logic picks up only left-half 10040f7e4c33SKohei Kaigai * of the supplied name; splitted by a whitespace unexpectedly. 10050f7e4c33SKohei Kaigai */ 10060f7e4c33SKohei Kaigai char *r, *w; 10070f7e4c33SKohei Kaigai int c1, c2; 10080f7e4c33SKohei Kaigai 10090f7e4c33SKohei Kaigai r = w = namebuf; 10100f7e4c33SKohei Kaigai do { 10110f7e4c33SKohei Kaigai c1 = *r++; 10120f7e4c33SKohei Kaigai if (c1 == '+') 10130f7e4c33SKohei Kaigai c1 = ' '; 10140f7e4c33SKohei Kaigai else if (c1 == '%') { 1015af7ff2c2SAndy Shevchenko c1 = hex_to_bin(*r++); 1016af7ff2c2SAndy Shevchenko if (c1 < 0) 10170f7e4c33SKohei Kaigai goto out; 1018af7ff2c2SAndy Shevchenko c2 = hex_to_bin(*r++); 1019af7ff2c2SAndy Shevchenko if (c2 < 0) 10200f7e4c33SKohei Kaigai goto out; 10210f7e4c33SKohei Kaigai c1 = (c1 << 4) | c2; 10220f7e4c33SKohei Kaigai } 10230f7e4c33SKohei Kaigai *w++ = c1; 10240f7e4c33SKohei Kaigai } while (c1 != '\0'); 10250f7e4c33SKohei Kaigai 1026f50a3ec9SKohei Kaigai objname = namebuf; 10270f7e4c33SKohei Kaigai } 1028f50a3ec9SKohei Kaigai 10290619f0f5SStephen Smalley length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL); 1030b77a493bSEric Paris if (length) 1031b77a493bSEric Paris goto out; 1032b77a493bSEric Paris 10330619f0f5SStephen Smalley length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL); 1034b77a493bSEric Paris if (length) 1035b77a493bSEric Paris goto out; 10361da177e4SLinus Torvalds 10370619f0f5SStephen Smalley length = security_transition_sid_user(state, ssid, tsid, tclass, 10380619f0f5SStephen Smalley objname, &newsid); 1039b77a493bSEric Paris if (length) 1040b77a493bSEric Paris goto out; 10411da177e4SLinus Torvalds 10420619f0f5SStephen Smalley length = security_sid_to_context(state, newsid, &newcon, &len); 1043b77a493bSEric Paris if (length) 1044b77a493bSEric Paris goto out; 10451da177e4SLinus Torvalds 1046b77a493bSEric Paris length = -ERANGE; 10471da177e4SLinus Torvalds if (len > SIMPLE_TRANSACTION_LIMIT) { 1048f8b69a5fSpeter enderborg pr_err("SELinux: %s: context size (%u) exceeds " 1049744ba35eSEric Paris "payload max\n", __func__, len); 1050b77a493bSEric Paris goto out; 10511da177e4SLinus Torvalds } 10521da177e4SLinus Torvalds 10531da177e4SLinus Torvalds memcpy(buf, newcon, len); 10541da177e4SLinus Torvalds length = len; 10551da177e4SLinus Torvalds out: 1056b77a493bSEric Paris kfree(newcon); 1057f50a3ec9SKohei Kaigai kfree(namebuf); 1058b77a493bSEric Paris kfree(tcon); 10591da177e4SLinus Torvalds kfree(scon); 10601da177e4SLinus Torvalds return length; 10611da177e4SLinus Torvalds } 10621da177e4SLinus Torvalds 10631da177e4SLinus Torvalds static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size) 10641da177e4SLinus Torvalds { 10650619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 10660619f0f5SStephen Smalley struct selinux_state *state = fsi->state; 1067b77a493bSEric Paris char *scon = NULL, *tcon = NULL; 10681da177e4SLinus Torvalds u32 ssid, tsid, newsid; 10691da177e4SLinus Torvalds u16 tclass; 10701da177e4SLinus Torvalds ssize_t length; 1071b77a493bSEric Paris char *newcon = NULL; 10721da177e4SLinus Torvalds u32 len; 10731da177e4SLinus Torvalds 10746b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 10756b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 1076be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_RELABEL, 1077be0554c9SStephen Smalley NULL); 10781da177e4SLinus Torvalds if (length) 1079b77a493bSEric Paris goto out; 10801da177e4SLinus Torvalds 10811da177e4SLinus Torvalds length = -ENOMEM; 108289d155efSJames Morris scon = kzalloc(size + 1, GFP_KERNEL); 10831da177e4SLinus Torvalds if (!scon) 1084b77a493bSEric Paris goto out; 10851da177e4SLinus Torvalds 1086b77a493bSEric Paris length = -ENOMEM; 108789d155efSJames Morris tcon = kzalloc(size + 1, GFP_KERNEL); 10881da177e4SLinus Torvalds if (!tcon) 10891da177e4SLinus Torvalds goto out; 10901da177e4SLinus Torvalds 10911da177e4SLinus Torvalds length = -EINVAL; 10921da177e4SLinus Torvalds if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) 1093b77a493bSEric Paris goto out; 10941da177e4SLinus Torvalds 10950619f0f5SStephen Smalley length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL); 1096b77a493bSEric Paris if (length) 1097b77a493bSEric Paris goto out; 1098b77a493bSEric Paris 10990619f0f5SStephen Smalley length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL); 1100b77a493bSEric Paris if (length) 1101b77a493bSEric Paris goto out; 11021da177e4SLinus Torvalds 11030619f0f5SStephen Smalley length = security_change_sid(state, ssid, tsid, tclass, &newsid); 1104b77a493bSEric Paris if (length) 1105b77a493bSEric Paris goto out; 11061da177e4SLinus Torvalds 11070619f0f5SStephen Smalley length = security_sid_to_context(state, newsid, &newcon, &len); 1108b77a493bSEric Paris if (length) 1109b77a493bSEric Paris goto out; 11101da177e4SLinus Torvalds 11111da177e4SLinus Torvalds length = -ERANGE; 1112b77a493bSEric Paris if (len > SIMPLE_TRANSACTION_LIMIT) 1113b77a493bSEric Paris goto out; 11141da177e4SLinus Torvalds 11151da177e4SLinus Torvalds memcpy(buf, newcon, len); 11161da177e4SLinus Torvalds length = len; 11171da177e4SLinus Torvalds out: 1118b77a493bSEric Paris kfree(newcon); 1119b77a493bSEric Paris kfree(tcon); 11201da177e4SLinus Torvalds kfree(scon); 11211da177e4SLinus Torvalds return length; 11221da177e4SLinus Torvalds } 11231da177e4SLinus Torvalds 11241da177e4SLinus Torvalds static ssize_t sel_write_user(struct file *file, char *buf, size_t size) 11251da177e4SLinus Torvalds { 11260619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 11270619f0f5SStephen Smalley struct selinux_state *state = fsi->state; 1128b77a493bSEric Paris char *con = NULL, *user = NULL, *ptr; 1129b77a493bSEric Paris u32 sid, *sids = NULL; 11301da177e4SLinus Torvalds ssize_t length; 11311da177e4SLinus Torvalds char *newcon; 11321da177e4SLinus Torvalds int i, rc; 11331da177e4SLinus Torvalds u32 len, nsids; 11341da177e4SLinus Torvalds 11356b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 11366b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 1137be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_USER, 1138be0554c9SStephen Smalley NULL); 11391da177e4SLinus Torvalds if (length) 11406eab04a8SJustin P. Mattock goto out; 11411da177e4SLinus Torvalds 11421da177e4SLinus Torvalds length = -ENOMEM; 114389d155efSJames Morris con = kzalloc(size + 1, GFP_KERNEL); 11441da177e4SLinus Torvalds if (!con) 11456eab04a8SJustin P. Mattock goto out; 11461da177e4SLinus Torvalds 1147b77a493bSEric Paris length = -ENOMEM; 114889d155efSJames Morris user = kzalloc(size + 1, GFP_KERNEL); 11491da177e4SLinus Torvalds if (!user) 11501da177e4SLinus Torvalds goto out; 11511da177e4SLinus Torvalds 11521da177e4SLinus Torvalds length = -EINVAL; 11531da177e4SLinus Torvalds if (sscanf(buf, "%s %s", con, user) != 2) 1154b77a493bSEric Paris goto out; 11551da177e4SLinus Torvalds 11560619f0f5SStephen Smalley length = security_context_str_to_sid(state, con, &sid, GFP_KERNEL); 1157b77a493bSEric Paris if (length) 1158b77a493bSEric Paris goto out; 11591da177e4SLinus Torvalds 11600619f0f5SStephen Smalley length = security_get_user_sids(state, sid, user, &sids, &nsids); 1161b77a493bSEric Paris if (length) 1162b77a493bSEric Paris goto out; 11631da177e4SLinus Torvalds 11641da177e4SLinus Torvalds length = sprintf(buf, "%u", nsids) + 1; 11651da177e4SLinus Torvalds ptr = buf + length; 11661da177e4SLinus Torvalds for (i = 0; i < nsids; i++) { 11670619f0f5SStephen Smalley rc = security_sid_to_context(state, sids[i], &newcon, &len); 11681da177e4SLinus Torvalds if (rc) { 11691da177e4SLinus Torvalds length = rc; 1170b77a493bSEric Paris goto out; 11711da177e4SLinus Torvalds } 11721da177e4SLinus Torvalds if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) { 11731da177e4SLinus Torvalds kfree(newcon); 11741da177e4SLinus Torvalds length = -ERANGE; 1175b77a493bSEric Paris goto out; 11761da177e4SLinus Torvalds } 11771da177e4SLinus Torvalds memcpy(ptr, newcon, len); 11781da177e4SLinus Torvalds kfree(newcon); 11791da177e4SLinus Torvalds ptr += len; 11801da177e4SLinus Torvalds length += len; 11811da177e4SLinus Torvalds } 11821da177e4SLinus Torvalds out: 1183b77a493bSEric Paris kfree(sids); 1184b77a493bSEric Paris kfree(user); 11851da177e4SLinus Torvalds kfree(con); 11861da177e4SLinus Torvalds return length; 11871da177e4SLinus Torvalds } 11881da177e4SLinus Torvalds 11891da177e4SLinus Torvalds static ssize_t sel_write_member(struct file *file, char *buf, size_t size) 11901da177e4SLinus Torvalds { 11910619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 11920619f0f5SStephen Smalley struct selinux_state *state = fsi->state; 1193b77a493bSEric Paris char *scon = NULL, *tcon = NULL; 11941da177e4SLinus Torvalds u32 ssid, tsid, newsid; 11951da177e4SLinus Torvalds u16 tclass; 11961da177e4SLinus Torvalds ssize_t length; 1197b77a493bSEric Paris char *newcon = NULL; 11981da177e4SLinus Torvalds u32 len; 11991da177e4SLinus Torvalds 12006b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 12016b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 1202be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_MEMBER, 1203be0554c9SStephen Smalley NULL); 12041da177e4SLinus Torvalds if (length) 1205b77a493bSEric Paris goto out; 12061da177e4SLinus Torvalds 12071da177e4SLinus Torvalds length = -ENOMEM; 120889d155efSJames Morris scon = kzalloc(size + 1, GFP_KERNEL); 12091da177e4SLinus Torvalds if (!scon) 12106eab04a8SJustin P. Mattock goto out; 12111da177e4SLinus Torvalds 1212b77a493bSEric Paris length = -ENOMEM; 121389d155efSJames Morris tcon = kzalloc(size + 1, GFP_KERNEL); 12141da177e4SLinus Torvalds if (!tcon) 12151da177e4SLinus Torvalds goto out; 12161da177e4SLinus Torvalds 12171da177e4SLinus Torvalds length = -EINVAL; 12181da177e4SLinus Torvalds if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) 1219b77a493bSEric Paris goto out; 12201da177e4SLinus Torvalds 12210619f0f5SStephen Smalley length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL); 1222b77a493bSEric Paris if (length) 1223b77a493bSEric Paris goto out; 1224b77a493bSEric Paris 12250619f0f5SStephen Smalley length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL); 1226b77a493bSEric Paris if (length) 1227b77a493bSEric Paris goto out; 12281da177e4SLinus Torvalds 12290619f0f5SStephen Smalley length = security_member_sid(state, ssid, tsid, tclass, &newsid); 1230b77a493bSEric Paris if (length) 1231b77a493bSEric Paris goto out; 12321da177e4SLinus Torvalds 12330619f0f5SStephen Smalley length = security_sid_to_context(state, newsid, &newcon, &len); 1234b77a493bSEric Paris if (length) 1235b77a493bSEric Paris goto out; 12361da177e4SLinus Torvalds 1237b77a493bSEric Paris length = -ERANGE; 12381da177e4SLinus Torvalds if (len > SIMPLE_TRANSACTION_LIMIT) { 1239f8b69a5fSpeter enderborg pr_err("SELinux: %s: context size (%u) exceeds " 1240744ba35eSEric Paris "payload max\n", __func__, len); 1241b77a493bSEric Paris goto out; 12421da177e4SLinus Torvalds } 12431da177e4SLinus Torvalds 12441da177e4SLinus Torvalds memcpy(buf, newcon, len); 12451da177e4SLinus Torvalds length = len; 12461da177e4SLinus Torvalds out: 1247b77a493bSEric Paris kfree(newcon); 1248b77a493bSEric Paris kfree(tcon); 12491da177e4SLinus Torvalds kfree(scon); 12501da177e4SLinus Torvalds return length; 12511da177e4SLinus Torvalds } 12521da177e4SLinus Torvalds 12531da177e4SLinus Torvalds static struct inode *sel_make_inode(struct super_block *sb, int mode) 12541da177e4SLinus Torvalds { 12551da177e4SLinus Torvalds struct inode *ret = new_inode(sb); 12561da177e4SLinus Torvalds 12571da177e4SLinus Torvalds if (ret) { 12581da177e4SLinus Torvalds ret->i_mode = mode; 1259078cd827SDeepa Dinamani ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret); 12601da177e4SLinus Torvalds } 12611da177e4SLinus Torvalds return ret; 12621da177e4SLinus Torvalds } 12631da177e4SLinus Torvalds 12641da177e4SLinus Torvalds static ssize_t sel_read_bool(struct file *filep, char __user *buf, 12651da177e4SLinus Torvalds size_t count, loff_t *ppos) 12661da177e4SLinus Torvalds { 12670619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info; 12681da177e4SLinus Torvalds char *page = NULL; 12691da177e4SLinus Torvalds ssize_t length; 12701da177e4SLinus Torvalds ssize_t ret; 12711da177e4SLinus Torvalds int cur_enforcing; 1272496ad9aaSAl Viro unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK; 1273d313f948SStephen Smalley const char *name = filep->f_path.dentry->d_name.name; 12741da177e4SLinus Torvalds 12759ff9abc4SStephen Smalley mutex_lock(&fsi->state->policy_mutex); 12761da177e4SLinus Torvalds 1277d313f948SStephen Smalley ret = -EINVAL; 12780619f0f5SStephen Smalley if (index >= fsi->bool_num || strcmp(name, 12790619f0f5SStephen Smalley fsi->bool_pending_names[index])) 12800da74120SJann Horn goto out_unlock; 12811da177e4SLinus Torvalds 12821da177e4SLinus Torvalds ret = -ENOMEM; 1283b77a493bSEric Paris page = (char *)get_zeroed_page(GFP_KERNEL); 1284b77a493bSEric Paris if (!page) 12850da74120SJann Horn goto out_unlock; 12861da177e4SLinus Torvalds 12870619f0f5SStephen Smalley cur_enforcing = security_get_bool_value(fsi->state, index); 12881da177e4SLinus Torvalds if (cur_enforcing < 0) { 12891da177e4SLinus Torvalds ret = cur_enforcing; 12900da74120SJann Horn goto out_unlock; 12911da177e4SLinus Torvalds } 12921da177e4SLinus Torvalds length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing, 12930619f0f5SStephen Smalley fsi->bool_pending_values[index]); 12949ff9abc4SStephen Smalley mutex_unlock(&fsi->state->policy_mutex); 12950da74120SJann Horn ret = simple_read_from_buffer(buf, count, ppos, page, length); 12960da74120SJann Horn out_free: 12971da177e4SLinus Torvalds free_page((unsigned long)page); 12981da177e4SLinus Torvalds return ret; 12990da74120SJann Horn 13000da74120SJann Horn out_unlock: 13019ff9abc4SStephen Smalley mutex_unlock(&fsi->state->policy_mutex); 13020da74120SJann Horn goto out_free; 13031da177e4SLinus Torvalds } 13041da177e4SLinus Torvalds 13051da177e4SLinus Torvalds static ssize_t sel_write_bool(struct file *filep, const char __user *buf, 13061da177e4SLinus Torvalds size_t count, loff_t *ppos) 13071da177e4SLinus Torvalds { 13080619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info; 13091da177e4SLinus Torvalds char *page = NULL; 1310d313f948SStephen Smalley ssize_t length; 13111da177e4SLinus Torvalds int new_value; 1312496ad9aaSAl Viro unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK; 1313d313f948SStephen Smalley const char *name = filep->f_path.dentry->d_name.name; 13141da177e4SLinus Torvalds 13150da74120SJann Horn if (count >= PAGE_SIZE) 13160da74120SJann Horn return -ENOMEM; 13170da74120SJann Horn 13180da74120SJann Horn /* No partial writes. */ 13190da74120SJann Horn if (*ppos != 0) 13200da74120SJann Horn return -EINVAL; 13210da74120SJann Horn 13220da74120SJann Horn page = memdup_user_nul(buf, count); 13230da74120SJann Horn if (IS_ERR(page)) 13240da74120SJann Horn return PTR_ERR(page); 13250da74120SJann Horn 13269ff9abc4SStephen Smalley mutex_lock(&fsi->state->policy_mutex); 13271da177e4SLinus Torvalds 13286b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 13296b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 1330be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETBOOL, 1331be0554c9SStephen Smalley NULL); 13321da177e4SLinus Torvalds if (length) 13331da177e4SLinus Torvalds goto out; 13341da177e4SLinus Torvalds 1335d313f948SStephen Smalley length = -EINVAL; 13360619f0f5SStephen Smalley if (index >= fsi->bool_num || strcmp(name, 13370619f0f5SStephen Smalley fsi->bool_pending_names[index])) 1338d313f948SStephen Smalley goto out; 1339d313f948SStephen Smalley 13401da177e4SLinus Torvalds length = -EINVAL; 13411da177e4SLinus Torvalds if (sscanf(page, "%d", &new_value) != 1) 13421da177e4SLinus Torvalds goto out; 13431da177e4SLinus Torvalds 13441da177e4SLinus Torvalds if (new_value) 13451da177e4SLinus Torvalds new_value = 1; 13461da177e4SLinus Torvalds 13470619f0f5SStephen Smalley fsi->bool_pending_values[index] = new_value; 13481da177e4SLinus Torvalds length = count; 13491da177e4SLinus Torvalds 13501da177e4SLinus Torvalds out: 13519ff9abc4SStephen Smalley mutex_unlock(&fsi->state->policy_mutex); 13528365a719SAl Viro kfree(page); 13531da177e4SLinus Torvalds return length; 13541da177e4SLinus Torvalds } 13551da177e4SLinus Torvalds 13569c2e08c5SArjan van de Ven static const struct file_operations sel_bool_ops = { 13571da177e4SLinus Torvalds .read = sel_read_bool, 13581da177e4SLinus Torvalds .write = sel_write_bool, 135957a62c23SArnd Bergmann .llseek = generic_file_llseek, 13601da177e4SLinus Torvalds }; 13611da177e4SLinus Torvalds 13621da177e4SLinus Torvalds static ssize_t sel_commit_bools_write(struct file *filep, 13631da177e4SLinus Torvalds const char __user *buf, 13641da177e4SLinus Torvalds size_t count, loff_t *ppos) 13651da177e4SLinus Torvalds { 13660619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info; 13671da177e4SLinus Torvalds char *page = NULL; 1368d313f948SStephen Smalley ssize_t length; 13691da177e4SLinus Torvalds int new_value; 13701da177e4SLinus Torvalds 13710da74120SJann Horn if (count >= PAGE_SIZE) 13720da74120SJann Horn return -ENOMEM; 13730da74120SJann Horn 13740da74120SJann Horn /* No partial writes. */ 13750da74120SJann Horn if (*ppos != 0) 13760da74120SJann Horn return -EINVAL; 13770da74120SJann Horn 13780da74120SJann Horn page = memdup_user_nul(buf, count); 13790da74120SJann Horn if (IS_ERR(page)) 13800da74120SJann Horn return PTR_ERR(page); 13810da74120SJann Horn 13829ff9abc4SStephen Smalley mutex_lock(&fsi->state->policy_mutex); 13831da177e4SLinus Torvalds 13846b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 13856b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 1386be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETBOOL, 1387be0554c9SStephen Smalley NULL); 13881da177e4SLinus Torvalds if (length) 13891da177e4SLinus Torvalds goto out; 13901da177e4SLinus Torvalds 13911da177e4SLinus Torvalds length = -EINVAL; 13921da177e4SLinus Torvalds if (sscanf(page, "%d", &new_value) != 1) 13931da177e4SLinus Torvalds goto out; 13941da177e4SLinus Torvalds 1395b77a493bSEric Paris length = 0; 13960619f0f5SStephen Smalley if (new_value && fsi->bool_pending_values) 13970619f0f5SStephen Smalley length = security_set_bools(fsi->state, fsi->bool_num, 13980619f0f5SStephen Smalley fsi->bool_pending_values); 13991da177e4SLinus Torvalds 1400b77a493bSEric Paris if (!length) 14011da177e4SLinus Torvalds length = count; 14021da177e4SLinus Torvalds 14031da177e4SLinus Torvalds out: 14049ff9abc4SStephen Smalley mutex_unlock(&fsi->state->policy_mutex); 14058365a719SAl Viro kfree(page); 14061da177e4SLinus Torvalds return length; 14071da177e4SLinus Torvalds } 14081da177e4SLinus Torvalds 14099c2e08c5SArjan van de Ven static const struct file_operations sel_commit_bools_ops = { 14101da177e4SLinus Torvalds .write = sel_commit_bools_write, 141157a62c23SArnd Bergmann .llseek = generic_file_llseek, 14121da177e4SLinus Torvalds }; 14131da177e4SLinus Torvalds 14140c92d7c7SChristopher J. PeBenito static void sel_remove_entries(struct dentry *de) 14151da177e4SLinus Torvalds { 1416ad52184bSAl Viro d_genocide(de); 1417ad52184bSAl Viro shrink_dcache_parent(de); 14181da177e4SLinus Torvalds } 14191da177e4SLinus Torvalds 142066ec384aSDaniel Burgener static int sel_make_bools(struct selinux_policy *newpolicy, struct dentry *bool_dir, 142166ec384aSDaniel Burgener unsigned int *bool_num, char ***bool_pending_names, 142266ec384aSDaniel Burgener unsigned int **bool_pending_values) 14231da177e4SLinus Torvalds { 142460abd318SOndrej Mosnacek int ret; 14251da177e4SLinus Torvalds ssize_t len; 14261da177e4SLinus Torvalds struct dentry *dentry = NULL; 14271da177e4SLinus Torvalds struct inode *inode = NULL; 14281da177e4SLinus Torvalds struct inode_security_struct *isec; 14291da177e4SLinus Torvalds char **names = NULL, *page; 143060abd318SOndrej Mosnacek u32 i, num; 14311da177e4SLinus Torvalds int *values = NULL; 14321da177e4SLinus Torvalds u32 sid; 14331da177e4SLinus Torvalds 1434b77a493bSEric Paris ret = -ENOMEM; 14351872981bSEric Paris page = (char *)get_zeroed_page(GFP_KERNEL); 14361872981bSEric Paris if (!page) 1437b77a493bSEric Paris goto out; 14381da177e4SLinus Torvalds 143902a52c5cSStephen Smalley ret = security_get_bools(newpolicy, &num, &names, &values); 1440b77a493bSEric Paris if (ret) 14411da177e4SLinus Torvalds goto out; 14421da177e4SLinus Torvalds 14431da177e4SLinus Torvalds for (i = 0; i < num; i++) { 1444b77a493bSEric Paris ret = -ENOMEM; 144566ec384aSDaniel Burgener dentry = d_alloc_name(bool_dir, names[i]); 1446b77a493bSEric Paris if (!dentry) 1447b77a493bSEric Paris goto out; 14481da177e4SLinus Torvalds 1449b77a493bSEric Paris ret = -ENOMEM; 145066ec384aSDaniel Burgener inode = sel_make_inode(bool_dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR); 14517e4237faSnixiaoming if (!inode) { 14527e4237faSnixiaoming dput(dentry); 1453b77a493bSEric Paris goto out; 14547e4237faSnixiaoming } 1455b77a493bSEric Paris 14561da177e4SLinus Torvalds ret = -ENAMETOOLONG; 1457cc1dad71SAl Viro len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]); 14587e4237faSnixiaoming if (len >= PAGE_SIZE) { 14597e4237faSnixiaoming dput(dentry); 14607e4237faSnixiaoming iput(inode); 1461b77a493bSEric Paris goto out; 14627e4237faSnixiaoming } 1463b77a493bSEric Paris 146480788c22SCasey Schaufler isec = selinux_inode(inode); 146502a52c5cSStephen Smalley ret = selinux_policy_genfs_sid(newpolicy, "selinuxfs", page, 1466aa8e712cSStephen Smalley SECCLASS_FILE, &sid); 14674262fb51SGary Tierney if (ret) { 1468900fde06SGary Tierney pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n", 1469900fde06SGary Tierney page); 1470900fde06SGary Tierney sid = SECINITSID_SECURITY; 14714262fb51SGary Tierney } 14724262fb51SGary Tierney 14731da177e4SLinus Torvalds isec->sid = sid; 147442059112SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 14751da177e4SLinus Torvalds inode->i_fop = &sel_bool_ops; 1476bce34bc0SJames Carter inode->i_ino = i|SEL_BOOL_INO_OFFSET; 14771da177e4SLinus Torvalds d_add(dentry, inode); 14781da177e4SLinus Torvalds } 147966ec384aSDaniel Burgener *bool_num = num; 148066ec384aSDaniel Burgener *bool_pending_names = names; 148166ec384aSDaniel Burgener *bool_pending_values = values; 1482b77a493bSEric Paris 1483b77a493bSEric Paris free_page((unsigned long)page); 1484b77a493bSEric Paris return 0; 14851da177e4SLinus Torvalds out: 14861da177e4SLinus Torvalds free_page((unsigned long)page); 1487b77a493bSEric Paris 14881da177e4SLinus Torvalds if (names) { 14899a5f04bfSJesper Juhl for (i = 0; i < num; i++) 14901da177e4SLinus Torvalds kfree(names[i]); 14911da177e4SLinus Torvalds kfree(names); 14921da177e4SLinus Torvalds } 149320c19e41SDavi Arnaut kfree(values); 149466ec384aSDaniel Burgener sel_remove_entries(bool_dir); 1495b77a493bSEric Paris 1496b77a493bSEric Paris return ret; 14971da177e4SLinus Torvalds } 14981da177e4SLinus Torvalds 14991da177e4SLinus Torvalds static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf, 15001da177e4SLinus Torvalds size_t count, loff_t *ppos) 15011da177e4SLinus Torvalds { 15026b6bc620SStephen Smalley struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info; 15036b6bc620SStephen Smalley struct selinux_state *state = fsi->state; 15041da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 15051da177e4SLinus Torvalds ssize_t length; 15061da177e4SLinus Torvalds 15076b6bc620SStephen Smalley length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 15086b6bc620SStephen Smalley avc_get_cache_threshold(state->avc)); 15091da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 15101da177e4SLinus Torvalds } 15111da177e4SLinus Torvalds 15121da177e4SLinus Torvalds static ssize_t sel_write_avc_cache_threshold(struct file *file, 15131da177e4SLinus Torvalds const char __user *buf, 15141da177e4SLinus Torvalds size_t count, loff_t *ppos) 15151da177e4SLinus Torvalds 15161da177e4SLinus Torvalds { 15176b6bc620SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 15186b6bc620SStephen Smalley struct selinux_state *state = fsi->state; 15198365a719SAl Viro char *page; 15201da177e4SLinus Torvalds ssize_t ret; 1521309c5fadSHeinrich Schuchardt unsigned int new_value; 15221da177e4SLinus Torvalds 15236b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 15246b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 1525be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETSECPARAM, 1526be0554c9SStephen Smalley NULL); 15271da177e4SLinus Torvalds if (ret) 15288365a719SAl Viro return ret; 1529b77a493bSEric Paris 1530b77a493bSEric Paris if (count >= PAGE_SIZE) 15318365a719SAl Viro return -ENOMEM; 1532b77a493bSEric Paris 1533b77a493bSEric Paris /* No partial writes. */ 1534b77a493bSEric Paris if (*ppos != 0) 15358365a719SAl Viro return -EINVAL; 1536b77a493bSEric Paris 15378365a719SAl Viro page = memdup_user_nul(buf, count); 15388365a719SAl Viro if (IS_ERR(page)) 15398365a719SAl Viro return PTR_ERR(page); 1540b77a493bSEric Paris 1541b77a493bSEric Paris ret = -EINVAL; 1542b77a493bSEric Paris if (sscanf(page, "%u", &new_value) != 1) 1543b77a493bSEric Paris goto out; 1544b77a493bSEric Paris 15456b6bc620SStephen Smalley avc_set_cache_threshold(state->avc, new_value); 1546b77a493bSEric Paris 15471da177e4SLinus Torvalds ret = count; 15481da177e4SLinus Torvalds out: 15498365a719SAl Viro kfree(page); 15501da177e4SLinus Torvalds return ret; 15511da177e4SLinus Torvalds } 15521da177e4SLinus Torvalds 15531da177e4SLinus Torvalds static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf, 15541da177e4SLinus Torvalds size_t count, loff_t *ppos) 15551da177e4SLinus Torvalds { 15566b6bc620SStephen Smalley struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info; 15576b6bc620SStephen Smalley struct selinux_state *state = fsi->state; 15581da177e4SLinus Torvalds char *page; 1559b77a493bSEric Paris ssize_t length; 15601da177e4SLinus Torvalds 15611da177e4SLinus Torvalds page = (char *)__get_free_page(GFP_KERNEL); 1562b77a493bSEric Paris if (!page) 1563b77a493bSEric Paris return -ENOMEM; 1564b77a493bSEric Paris 15656b6bc620SStephen Smalley length = avc_get_hash_stats(state->avc, page); 1566b77a493bSEric Paris if (length >= 0) 1567b77a493bSEric Paris length = simple_read_from_buffer(buf, count, ppos, page, length); 15681da177e4SLinus Torvalds free_page((unsigned long)page); 1569b77a493bSEric Paris 1570b77a493bSEric Paris return length; 15711da177e4SLinus Torvalds } 15721da177e4SLinus Torvalds 157366f8e2f0SJeff Vander Stoep static ssize_t sel_read_sidtab_hash_stats(struct file *filp, char __user *buf, 157466f8e2f0SJeff Vander Stoep size_t count, loff_t *ppos) 157566f8e2f0SJeff Vander Stoep { 157666f8e2f0SJeff Vander Stoep struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info; 157766f8e2f0SJeff Vander Stoep struct selinux_state *state = fsi->state; 157866f8e2f0SJeff Vander Stoep char *page; 157966f8e2f0SJeff Vander Stoep ssize_t length; 158066f8e2f0SJeff Vander Stoep 158166f8e2f0SJeff Vander Stoep page = (char *)__get_free_page(GFP_KERNEL); 158266f8e2f0SJeff Vander Stoep if (!page) 158366f8e2f0SJeff Vander Stoep return -ENOMEM; 158466f8e2f0SJeff Vander Stoep 158566f8e2f0SJeff Vander Stoep length = security_sidtab_hash_stats(state, page); 158666f8e2f0SJeff Vander Stoep if (length >= 0) 158766f8e2f0SJeff Vander Stoep length = simple_read_from_buffer(buf, count, ppos, page, 158866f8e2f0SJeff Vander Stoep length); 158966f8e2f0SJeff Vander Stoep free_page((unsigned long)page); 159066f8e2f0SJeff Vander Stoep 159166f8e2f0SJeff Vander Stoep return length; 159266f8e2f0SJeff Vander Stoep } 159366f8e2f0SJeff Vander Stoep 159466f8e2f0SJeff Vander Stoep static const struct file_operations sel_sidtab_hash_stats_ops = { 159566f8e2f0SJeff Vander Stoep .read = sel_read_sidtab_hash_stats, 159666f8e2f0SJeff Vander Stoep .llseek = generic_file_llseek, 159766f8e2f0SJeff Vander Stoep }; 159866f8e2f0SJeff Vander Stoep 15999c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_threshold_ops = { 16001da177e4SLinus Torvalds .read = sel_read_avc_cache_threshold, 16011da177e4SLinus Torvalds .write = sel_write_avc_cache_threshold, 160257a62c23SArnd Bergmann .llseek = generic_file_llseek, 16031da177e4SLinus Torvalds }; 16041da177e4SLinus Torvalds 16059c2e08c5SArjan van de Ven static const struct file_operations sel_avc_hash_stats_ops = { 16061da177e4SLinus Torvalds .read = sel_read_avc_hash_stats, 160757a62c23SArnd Bergmann .llseek = generic_file_llseek, 16081da177e4SLinus Torvalds }; 16091da177e4SLinus Torvalds 16101da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS 16111da177e4SLinus Torvalds static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx) 16121da177e4SLinus Torvalds { 16131da177e4SLinus Torvalds int cpu; 16141da177e4SLinus Torvalds 16154f4b6c1aSRusty Russell for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) { 16161da177e4SLinus Torvalds if (!cpu_possible(cpu)) 16171da177e4SLinus Torvalds continue; 16181da177e4SLinus Torvalds *idx = cpu + 1; 16191da177e4SLinus Torvalds return &per_cpu(avc_cache_stats, cpu); 16201da177e4SLinus Torvalds } 16218d269a8eSVasily Averin (*idx)++; 16221da177e4SLinus Torvalds return NULL; 16231da177e4SLinus Torvalds } 16241da177e4SLinus Torvalds 16251da177e4SLinus Torvalds static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos) 16261da177e4SLinus Torvalds { 16271da177e4SLinus Torvalds loff_t n = *pos - 1; 16281da177e4SLinus Torvalds 16291da177e4SLinus Torvalds if (*pos == 0) 16301da177e4SLinus Torvalds return SEQ_START_TOKEN; 16311da177e4SLinus Torvalds 16321da177e4SLinus Torvalds return sel_avc_get_stat_idx(&n); 16331da177e4SLinus Torvalds } 16341da177e4SLinus Torvalds 16351da177e4SLinus Torvalds static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos) 16361da177e4SLinus Torvalds { 16371da177e4SLinus Torvalds return sel_avc_get_stat_idx(pos); 16381da177e4SLinus Torvalds } 16391da177e4SLinus Torvalds 16401da177e4SLinus Torvalds static int sel_avc_stats_seq_show(struct seq_file *seq, void *v) 16411da177e4SLinus Torvalds { 16421da177e4SLinus Torvalds struct avc_cache_stats *st = v; 16431da177e4SLinus Torvalds 1644710a0647SMarkus Elfring if (v == SEQ_START_TOKEN) { 1645710a0647SMarkus Elfring seq_puts(seq, 1646710a0647SMarkus Elfring "lookups hits misses allocations reclaims frees\n"); 1647710a0647SMarkus Elfring } else { 1648257313b2SLinus Torvalds unsigned int lookups = st->lookups; 1649257313b2SLinus Torvalds unsigned int misses = st->misses; 1650257313b2SLinus Torvalds unsigned int hits = lookups - misses; 1651257313b2SLinus Torvalds seq_printf(seq, "%u %u %u %u %u %u\n", lookups, 1652257313b2SLinus Torvalds hits, misses, st->allocations, 16531da177e4SLinus Torvalds st->reclaims, st->frees); 1654257313b2SLinus Torvalds } 16551da177e4SLinus Torvalds return 0; 16561da177e4SLinus Torvalds } 16571da177e4SLinus Torvalds 16581da177e4SLinus Torvalds static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v) 16591da177e4SLinus Torvalds { } 16601da177e4SLinus Torvalds 16611996a109SJan Engelhardt static const struct seq_operations sel_avc_cache_stats_seq_ops = { 16621da177e4SLinus Torvalds .start = sel_avc_stats_seq_start, 16631da177e4SLinus Torvalds .next = sel_avc_stats_seq_next, 16641da177e4SLinus Torvalds .show = sel_avc_stats_seq_show, 16651da177e4SLinus Torvalds .stop = sel_avc_stats_seq_stop, 16661da177e4SLinus Torvalds }; 16671da177e4SLinus Torvalds 16681da177e4SLinus Torvalds static int sel_open_avc_cache_stats(struct inode *inode, struct file *file) 16691da177e4SLinus Torvalds { 16701da177e4SLinus Torvalds return seq_open(file, &sel_avc_cache_stats_seq_ops); 16711da177e4SLinus Torvalds } 16721da177e4SLinus Torvalds 16739c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_stats_ops = { 16741da177e4SLinus Torvalds .open = sel_open_avc_cache_stats, 16751da177e4SLinus Torvalds .read = seq_read, 16761da177e4SLinus Torvalds .llseek = seq_lseek, 16771da177e4SLinus Torvalds .release = seq_release, 16781da177e4SLinus Torvalds }; 16791da177e4SLinus Torvalds #endif 16801da177e4SLinus Torvalds 16811da177e4SLinus Torvalds static int sel_make_avc_files(struct dentry *dir) 16821da177e4SLinus Torvalds { 16830619f0f5SStephen Smalley struct super_block *sb = dir->d_sb; 16840619f0f5SStephen Smalley struct selinux_fs_info *fsi = sb->s_fs_info; 1685b77a493bSEric Paris int i; 1686cda37124SEric Biggers static const struct tree_descr files[] = { 16871da177e4SLinus Torvalds { "cache_threshold", 16881da177e4SLinus Torvalds &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR }, 16891da177e4SLinus Torvalds { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO }, 16901da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS 16911da177e4SLinus Torvalds { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO }, 16921da177e4SLinus Torvalds #endif 16931da177e4SLinus Torvalds }; 16941da177e4SLinus Torvalds 16956e20a64aSNicolas Kaiser for (i = 0; i < ARRAY_SIZE(files); i++) { 16961da177e4SLinus Torvalds struct inode *inode; 16971da177e4SLinus Torvalds struct dentry *dentry; 16981da177e4SLinus Torvalds 16991da177e4SLinus Torvalds dentry = d_alloc_name(dir, files[i].name); 1700b77a493bSEric Paris if (!dentry) 1701b77a493bSEric Paris return -ENOMEM; 17021da177e4SLinus Torvalds 17031da177e4SLinus Torvalds inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode); 17047e4237faSnixiaoming if (!inode) { 17057e4237faSnixiaoming dput(dentry); 1706b77a493bSEric Paris return -ENOMEM; 17077e4237faSnixiaoming } 1708b77a493bSEric Paris 17091da177e4SLinus Torvalds inode->i_fop = files[i].ops; 17100619f0f5SStephen Smalley inode->i_ino = ++fsi->last_ino; 17111da177e4SLinus Torvalds d_add(dentry, inode); 17121da177e4SLinus Torvalds } 1713b77a493bSEric Paris 1714b77a493bSEric Paris return 0; 17151da177e4SLinus Torvalds } 17161da177e4SLinus Torvalds 171766f8e2f0SJeff Vander Stoep static int sel_make_ss_files(struct dentry *dir) 171866f8e2f0SJeff Vander Stoep { 171966f8e2f0SJeff Vander Stoep struct super_block *sb = dir->d_sb; 172066f8e2f0SJeff Vander Stoep struct selinux_fs_info *fsi = sb->s_fs_info; 172166f8e2f0SJeff Vander Stoep int i; 172266f8e2f0SJeff Vander Stoep static struct tree_descr files[] = { 172366f8e2f0SJeff Vander Stoep { "sidtab_hash_stats", &sel_sidtab_hash_stats_ops, S_IRUGO }, 172466f8e2f0SJeff Vander Stoep }; 172566f8e2f0SJeff Vander Stoep 172666f8e2f0SJeff Vander Stoep for (i = 0; i < ARRAY_SIZE(files); i++) { 172766f8e2f0SJeff Vander Stoep struct inode *inode; 172866f8e2f0SJeff Vander Stoep struct dentry *dentry; 172966f8e2f0SJeff Vander Stoep 173066f8e2f0SJeff Vander Stoep dentry = d_alloc_name(dir, files[i].name); 173166f8e2f0SJeff Vander Stoep if (!dentry) 173266f8e2f0SJeff Vander Stoep return -ENOMEM; 173366f8e2f0SJeff Vander Stoep 173466f8e2f0SJeff Vander Stoep inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode); 173566f8e2f0SJeff Vander Stoep if (!inode) { 173666f8e2f0SJeff Vander Stoep dput(dentry); 173766f8e2f0SJeff Vander Stoep return -ENOMEM; 173866f8e2f0SJeff Vander Stoep } 173966f8e2f0SJeff Vander Stoep 174066f8e2f0SJeff Vander Stoep inode->i_fop = files[i].ops; 174166f8e2f0SJeff Vander Stoep inode->i_ino = ++fsi->last_ino; 174266f8e2f0SJeff Vander Stoep d_add(dentry, inode); 174366f8e2f0SJeff Vander Stoep } 174466f8e2f0SJeff Vander Stoep 174566f8e2f0SJeff Vander Stoep return 0; 174666f8e2f0SJeff Vander Stoep } 174766f8e2f0SJeff Vander Stoep 1748f0ee2e46SJames Carter static ssize_t sel_read_initcon(struct file *file, char __user *buf, 1749f0ee2e46SJames Carter size_t count, loff_t *ppos) 1750f0ee2e46SJames Carter { 17510619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 1752f0ee2e46SJames Carter char *con; 1753f0ee2e46SJames Carter u32 sid, len; 1754f0ee2e46SJames Carter ssize_t ret; 1755f0ee2e46SJames Carter 1756496ad9aaSAl Viro sid = file_inode(file)->i_ino&SEL_INO_MASK; 17570619f0f5SStephen Smalley ret = security_sid_to_context(fsi->state, sid, &con, &len); 1758b77a493bSEric Paris if (ret) 1759f0ee2e46SJames Carter return ret; 1760f0ee2e46SJames Carter 1761f0ee2e46SJames Carter ret = simple_read_from_buffer(buf, count, ppos, con, len); 1762f0ee2e46SJames Carter kfree(con); 1763f0ee2e46SJames Carter return ret; 1764f0ee2e46SJames Carter } 1765f0ee2e46SJames Carter 1766f0ee2e46SJames Carter static const struct file_operations sel_initcon_ops = { 1767f0ee2e46SJames Carter .read = sel_read_initcon, 176857a62c23SArnd Bergmann .llseek = generic_file_llseek, 1769f0ee2e46SJames Carter }; 1770f0ee2e46SJames Carter 1771f0ee2e46SJames Carter static int sel_make_initcon_files(struct dentry *dir) 1772f0ee2e46SJames Carter { 1773b77a493bSEric Paris int i; 1774f0ee2e46SJames Carter 1775f0ee2e46SJames Carter for (i = 1; i <= SECINITSID_NUM; i++) { 1776f0ee2e46SJames Carter struct inode *inode; 1777f0ee2e46SJames Carter struct dentry *dentry; 1778e3e0b582SStephen Smalley const char *s = security_get_initial_sid_context(i); 1779e3e0b582SStephen Smalley 1780e3e0b582SStephen Smalley if (!s) 1781e3e0b582SStephen Smalley continue; 1782e3e0b582SStephen Smalley dentry = d_alloc_name(dir, s); 1783b77a493bSEric Paris if (!dentry) 1784b77a493bSEric Paris return -ENOMEM; 1785f0ee2e46SJames Carter 1786f0ee2e46SJames Carter inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); 17877e4237faSnixiaoming if (!inode) { 17887e4237faSnixiaoming dput(dentry); 1789b77a493bSEric Paris return -ENOMEM; 17907e4237faSnixiaoming } 1791b77a493bSEric Paris 1792f0ee2e46SJames Carter inode->i_fop = &sel_initcon_ops; 1793f0ee2e46SJames Carter inode->i_ino = i|SEL_INITCON_INO_OFFSET; 1794f0ee2e46SJames Carter d_add(dentry, inode); 1795f0ee2e46SJames Carter } 1796b77a493bSEric Paris 1797b77a493bSEric Paris return 0; 1798f0ee2e46SJames Carter } 1799f0ee2e46SJames Carter 1800e47c8fc5SChristopher J. PeBenito static inline unsigned long sel_class_to_ino(u16 class) 1801e47c8fc5SChristopher J. PeBenito { 1802e47c8fc5SChristopher J. PeBenito return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET; 1803e47c8fc5SChristopher J. PeBenito } 1804e47c8fc5SChristopher J. PeBenito 1805e47c8fc5SChristopher J. PeBenito static inline u16 sel_ino_to_class(unsigned long ino) 1806e47c8fc5SChristopher J. PeBenito { 180792ae9e82SEric Paris return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1); 1808e47c8fc5SChristopher J. PeBenito } 1809e47c8fc5SChristopher J. PeBenito 1810e47c8fc5SChristopher J. PeBenito static inline unsigned long sel_perm_to_ino(u16 class, u32 perm) 1811e47c8fc5SChristopher J. PeBenito { 1812e47c8fc5SChristopher J. PeBenito return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET; 1813e47c8fc5SChristopher J. PeBenito } 1814e47c8fc5SChristopher J. PeBenito 1815e47c8fc5SChristopher J. PeBenito static inline u32 sel_ino_to_perm(unsigned long ino) 1816e47c8fc5SChristopher J. PeBenito { 1817e47c8fc5SChristopher J. PeBenito return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1); 1818e47c8fc5SChristopher J. PeBenito } 1819e47c8fc5SChristopher J. PeBenito 1820e47c8fc5SChristopher J. PeBenito static ssize_t sel_read_class(struct file *file, char __user *buf, 1821e47c8fc5SChristopher J. PeBenito size_t count, loff_t *ppos) 1822e47c8fc5SChristopher J. PeBenito { 1823496ad9aaSAl Viro unsigned long ino = file_inode(file)->i_ino; 1824cc1dad71SAl Viro char res[TMPBUFLEN]; 18257e78c875Sliuyang34 ssize_t len = scnprintf(res, sizeof(res), "%d", sel_ino_to_class(ino)); 1826cc1dad71SAl Viro return simple_read_from_buffer(buf, count, ppos, res, len); 1827e47c8fc5SChristopher J. PeBenito } 1828e47c8fc5SChristopher J. PeBenito 1829e47c8fc5SChristopher J. PeBenito static const struct file_operations sel_class_ops = { 1830e47c8fc5SChristopher J. PeBenito .read = sel_read_class, 183157a62c23SArnd Bergmann .llseek = generic_file_llseek, 1832e47c8fc5SChristopher J. PeBenito }; 1833e47c8fc5SChristopher J. PeBenito 1834e47c8fc5SChristopher J. PeBenito static ssize_t sel_read_perm(struct file *file, char __user *buf, 1835e47c8fc5SChristopher J. PeBenito size_t count, loff_t *ppos) 1836e47c8fc5SChristopher J. PeBenito { 1837496ad9aaSAl Viro unsigned long ino = file_inode(file)->i_ino; 1838cc1dad71SAl Viro char res[TMPBUFLEN]; 18397e78c875Sliuyang34 ssize_t len = scnprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino)); 1840cc1dad71SAl Viro return simple_read_from_buffer(buf, count, ppos, res, len); 1841e47c8fc5SChristopher J. PeBenito } 1842e47c8fc5SChristopher J. PeBenito 1843e47c8fc5SChristopher J. PeBenito static const struct file_operations sel_perm_ops = { 1844e47c8fc5SChristopher J. PeBenito .read = sel_read_perm, 184557a62c23SArnd Bergmann .llseek = generic_file_llseek, 1846e47c8fc5SChristopher J. PeBenito }; 1847e47c8fc5SChristopher J. PeBenito 18483bb56b25SPaul Moore static ssize_t sel_read_policycap(struct file *file, char __user *buf, 18493bb56b25SPaul Moore size_t count, loff_t *ppos) 18503bb56b25SPaul Moore { 18510619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 18523bb56b25SPaul Moore int value; 18533bb56b25SPaul Moore char tmpbuf[TMPBUFLEN]; 18543bb56b25SPaul Moore ssize_t length; 1855496ad9aaSAl Viro unsigned long i_ino = file_inode(file)->i_ino; 18563bb56b25SPaul Moore 18570619f0f5SStephen Smalley value = security_policycap_supported(fsi->state, i_ino & SEL_INO_MASK); 18583bb56b25SPaul Moore length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value); 18593bb56b25SPaul Moore 18603bb56b25SPaul Moore return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 18613bb56b25SPaul Moore } 18623bb56b25SPaul Moore 18633bb56b25SPaul Moore static const struct file_operations sel_policycap_ops = { 18643bb56b25SPaul Moore .read = sel_read_policycap, 186557a62c23SArnd Bergmann .llseek = generic_file_llseek, 18663bb56b25SPaul Moore }; 18673bb56b25SPaul Moore 186802a52c5cSStephen Smalley static int sel_make_perm_files(struct selinux_policy *newpolicy, 186902a52c5cSStephen Smalley char *objclass, int classvalue, 1870e47c8fc5SChristopher J. PeBenito struct dentry *dir) 1871e47c8fc5SChristopher J. PeBenito { 1872b77a493bSEric Paris int i, rc, nperms; 1873e47c8fc5SChristopher J. PeBenito char **perms; 1874e47c8fc5SChristopher J. PeBenito 187502a52c5cSStephen Smalley rc = security_get_permissions(newpolicy, objclass, &perms, &nperms); 1876e47c8fc5SChristopher J. PeBenito if (rc) 1877b77a493bSEric Paris return rc; 1878e47c8fc5SChristopher J. PeBenito 1879e47c8fc5SChristopher J. PeBenito for (i = 0; i < nperms; i++) { 1880e47c8fc5SChristopher J. PeBenito struct inode *inode; 1881e47c8fc5SChristopher J. PeBenito struct dentry *dentry; 1882e47c8fc5SChristopher J. PeBenito 1883b77a493bSEric Paris rc = -ENOMEM; 1884e47c8fc5SChristopher J. PeBenito dentry = d_alloc_name(dir, perms[i]); 1885b77a493bSEric Paris if (!dentry) 1886b77a493bSEric Paris goto out; 1887e47c8fc5SChristopher J. PeBenito 1888e47c8fc5SChristopher J. PeBenito rc = -ENOMEM; 1889b77a493bSEric Paris inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); 18907e4237faSnixiaoming if (!inode) { 18917e4237faSnixiaoming dput(dentry); 1892b77a493bSEric Paris goto out; 18937e4237faSnixiaoming } 1894b77a493bSEric Paris 1895e47c8fc5SChristopher J. PeBenito inode->i_fop = &sel_perm_ops; 1896e47c8fc5SChristopher J. PeBenito /* i+1 since perm values are 1-indexed */ 1897e47c8fc5SChristopher J. PeBenito inode->i_ino = sel_perm_to_ino(classvalue, i + 1); 1898e47c8fc5SChristopher J. PeBenito d_add(dentry, inode); 1899e47c8fc5SChristopher J. PeBenito } 1900b77a493bSEric Paris rc = 0; 1901b77a493bSEric Paris out: 1902e47c8fc5SChristopher J. PeBenito for (i = 0; i < nperms; i++) 1903e47c8fc5SChristopher J. PeBenito kfree(perms[i]); 1904e47c8fc5SChristopher J. PeBenito kfree(perms); 1905e47c8fc5SChristopher J. PeBenito return rc; 1906e47c8fc5SChristopher J. PeBenito } 1907e47c8fc5SChristopher J. PeBenito 190802a52c5cSStephen Smalley static int sel_make_class_dir_entries(struct selinux_policy *newpolicy, 190902a52c5cSStephen Smalley char *classname, int index, 1910e47c8fc5SChristopher J. PeBenito struct dentry *dir) 1911e47c8fc5SChristopher J. PeBenito { 19120619f0f5SStephen Smalley struct super_block *sb = dir->d_sb; 19130619f0f5SStephen Smalley struct selinux_fs_info *fsi = sb->s_fs_info; 1914e47c8fc5SChristopher J. PeBenito struct dentry *dentry = NULL; 1915e47c8fc5SChristopher J. PeBenito struct inode *inode = NULL; 1916e47c8fc5SChristopher J. PeBenito int rc; 1917e47c8fc5SChristopher J. PeBenito 1918e47c8fc5SChristopher J. PeBenito dentry = d_alloc_name(dir, "index"); 1919b77a493bSEric Paris if (!dentry) 1920b77a493bSEric Paris return -ENOMEM; 1921e47c8fc5SChristopher J. PeBenito 1922e47c8fc5SChristopher J. PeBenito inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); 19237e4237faSnixiaoming if (!inode) { 19247e4237faSnixiaoming dput(dentry); 1925b77a493bSEric Paris return -ENOMEM; 19267e4237faSnixiaoming } 1927e47c8fc5SChristopher J. PeBenito 1928e47c8fc5SChristopher J. PeBenito inode->i_fop = &sel_class_ops; 1929e47c8fc5SChristopher J. PeBenito inode->i_ino = sel_class_to_ino(index); 1930e47c8fc5SChristopher J. PeBenito d_add(dentry, inode); 1931e47c8fc5SChristopher J. PeBenito 19320619f0f5SStephen Smalley dentry = sel_make_dir(dir, "perms", &fsi->last_class_ino); 1933a1c2aa1eSAl Viro if (IS_ERR(dentry)) 1934a1c2aa1eSAl Viro return PTR_ERR(dentry); 1935e47c8fc5SChristopher J. PeBenito 193602a52c5cSStephen Smalley rc = sel_make_perm_files(newpolicy, classname, index, dentry); 1937e47c8fc5SChristopher J. PeBenito 1938e47c8fc5SChristopher J. PeBenito return rc; 1939e47c8fc5SChristopher J. PeBenito } 1940e47c8fc5SChristopher J. PeBenito 194166ec384aSDaniel Burgener static int sel_make_classes(struct selinux_policy *newpolicy, 194266ec384aSDaniel Burgener struct dentry *class_dir, 194366ec384aSDaniel Burgener unsigned long *last_class_ino) 1944e47c8fc5SChristopher J. PeBenito { 19450619f0f5SStephen Smalley 1946b77a493bSEric Paris int rc, nclasses, i; 1947e47c8fc5SChristopher J. PeBenito char **classes; 1948e47c8fc5SChristopher J. PeBenito 194902a52c5cSStephen Smalley rc = security_get_classes(newpolicy, &classes, &nclasses); 1950b77a493bSEric Paris if (rc) 1951b77a493bSEric Paris return rc; 1952e47c8fc5SChristopher J. PeBenito 1953e47c8fc5SChristopher J. PeBenito /* +2 since classes are 1-indexed */ 195466ec384aSDaniel Burgener *last_class_ino = sel_class_to_ino(nclasses + 2); 1955e47c8fc5SChristopher J. PeBenito 1956e47c8fc5SChristopher J. PeBenito for (i = 0; i < nclasses; i++) { 1957e47c8fc5SChristopher J. PeBenito struct dentry *class_name_dir; 1958e47c8fc5SChristopher J. PeBenito 195966ec384aSDaniel Burgener class_name_dir = sel_make_dir(class_dir, classes[i], 196066ec384aSDaniel Burgener last_class_ino); 1961a1c2aa1eSAl Viro if (IS_ERR(class_name_dir)) { 1962a1c2aa1eSAl Viro rc = PTR_ERR(class_name_dir); 1963b77a493bSEric Paris goto out; 1964a1c2aa1eSAl Viro } 1965e47c8fc5SChristopher J. PeBenito 1966e47c8fc5SChristopher J. PeBenito /* i+1 since class values are 1-indexed */ 196702a52c5cSStephen Smalley rc = sel_make_class_dir_entries(newpolicy, classes[i], i + 1, 1968e47c8fc5SChristopher J. PeBenito class_name_dir); 1969e47c8fc5SChristopher J. PeBenito if (rc) 1970b77a493bSEric Paris goto out; 1971e47c8fc5SChristopher J. PeBenito } 1972b77a493bSEric Paris rc = 0; 1973b77a493bSEric Paris out: 1974e47c8fc5SChristopher J. PeBenito for (i = 0; i < nclasses; i++) 1975e47c8fc5SChristopher J. PeBenito kfree(classes[i]); 1976e47c8fc5SChristopher J. PeBenito kfree(classes); 1977e47c8fc5SChristopher J. PeBenito return rc; 1978e47c8fc5SChristopher J. PeBenito } 1979e47c8fc5SChristopher J. PeBenito 19800619f0f5SStephen Smalley static int sel_make_policycap(struct selinux_fs_info *fsi) 19813bb56b25SPaul Moore { 19823bb56b25SPaul Moore unsigned int iter; 19833bb56b25SPaul Moore struct dentry *dentry = NULL; 19843bb56b25SPaul Moore struct inode *inode = NULL; 19853bb56b25SPaul Moore 1986*cdbec3edSPaul Moore for (iter = 0; iter <= POLICYDB_CAP_MAX; iter++) { 19874dc2fce3SStephen Smalley if (iter < ARRAY_SIZE(selinux_policycap_names)) 19880619f0f5SStephen Smalley dentry = d_alloc_name(fsi->policycap_dir, 19894dc2fce3SStephen Smalley selinux_policycap_names[iter]); 19903bb56b25SPaul Moore else 19910619f0f5SStephen Smalley dentry = d_alloc_name(fsi->policycap_dir, "unknown"); 19923bb56b25SPaul Moore 19933bb56b25SPaul Moore if (dentry == NULL) 19943bb56b25SPaul Moore return -ENOMEM; 19953bb56b25SPaul Moore 19960619f0f5SStephen Smalley inode = sel_make_inode(fsi->sb, S_IFREG | 0444); 19977e4237faSnixiaoming if (inode == NULL) { 19987e4237faSnixiaoming dput(dentry); 19993bb56b25SPaul Moore return -ENOMEM; 20007e4237faSnixiaoming } 20013bb56b25SPaul Moore 20023bb56b25SPaul Moore inode->i_fop = &sel_policycap_ops; 20033bb56b25SPaul Moore inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET; 20043bb56b25SPaul Moore d_add(dentry, inode); 20053bb56b25SPaul Moore } 20063bb56b25SPaul Moore 20073bb56b25SPaul Moore return 0; 20083bb56b25SPaul Moore } 20093bb56b25SPaul Moore 2010a1c2aa1eSAl Viro static struct dentry *sel_make_dir(struct dentry *dir, const char *name, 20110dd4ae51SChristopher J. PeBenito unsigned long *ino) 20121da177e4SLinus Torvalds { 2013a1c2aa1eSAl Viro struct dentry *dentry = d_alloc_name(dir, name); 20141da177e4SLinus Torvalds struct inode *inode; 20151da177e4SLinus Torvalds 2016a1c2aa1eSAl Viro if (!dentry) 2017a1c2aa1eSAl Viro return ERR_PTR(-ENOMEM); 2018a1c2aa1eSAl Viro 2019a1c2aa1eSAl Viro inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO); 2020a1c2aa1eSAl Viro if (!inode) { 2021a1c2aa1eSAl Viro dput(dentry); 2022a1c2aa1eSAl Viro return ERR_PTR(-ENOMEM); 2023a1c2aa1eSAl Viro } 2024b77a493bSEric Paris 20251da177e4SLinus Torvalds inode->i_op = &simple_dir_inode_operations; 20261da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations; 20270dd4ae51SChristopher J. PeBenito inode->i_ino = ++(*ino); 202840e906f8SJames Morris /* directory inodes start off with i_nlink == 2 (for "." entry) */ 2029d8c76e6fSDave Hansen inc_nlink(inode); 20301da177e4SLinus Torvalds d_add(dentry, inode); 2031edb20fb5SJames Morris /* bump link count on parent directory, too */ 2032ce0b16ddSDavid Howells inc_nlink(d_inode(dir)); 2033b77a493bSEric Paris 2034a1c2aa1eSAl Viro return dentry; 20351da177e4SLinus Torvalds } 20361da177e4SLinus Torvalds 20370eea6091SDaniel Burgener static struct dentry *sel_make_disconnected_dir(struct super_block *sb, 20380eea6091SDaniel Burgener unsigned long *ino) 20390eea6091SDaniel Burgener { 20400eea6091SDaniel Burgener struct inode *inode = sel_make_inode(sb, S_IFDIR | S_IRUGO | S_IXUGO); 20410eea6091SDaniel Burgener 20420eea6091SDaniel Burgener if (!inode) 20430eea6091SDaniel Burgener return ERR_PTR(-ENOMEM); 20440eea6091SDaniel Burgener 20450eea6091SDaniel Burgener inode->i_op = &simple_dir_inode_operations; 20460eea6091SDaniel Burgener inode->i_fop = &simple_dir_operations; 20470eea6091SDaniel Burgener inode->i_ino = ++(*ino); 20480eea6091SDaniel Burgener /* directory inodes start off with i_nlink == 2 (for "." entry) */ 20490eea6091SDaniel Burgener inc_nlink(inode); 20500eea6091SDaniel Burgener return d_obtain_alias(inode); 20510eea6091SDaniel Burgener } 20520eea6091SDaniel Burgener 20530619f0f5SStephen Smalley #define NULL_FILE_NAME "null" 20540619f0f5SStephen Smalley 2055920f50b2SDavid Howells static int sel_fill_super(struct super_block *sb, struct fs_context *fc) 20561da177e4SLinus Torvalds { 20570619f0f5SStephen Smalley struct selinux_fs_info *fsi; 20581da177e4SLinus Torvalds int ret; 20591da177e4SLinus Torvalds struct dentry *dentry; 2060a1c2aa1eSAl Viro struct inode *inode; 20611da177e4SLinus Torvalds struct inode_security_struct *isec; 20621da177e4SLinus Torvalds 2063cda37124SEric Biggers static const struct tree_descr selinux_files[] = { 20641da177e4SLinus Torvalds [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR}, 20651da177e4SLinus Torvalds [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR}, 2066ce9982d0SStephen Smalley [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO}, 20671da177e4SLinus Torvalds [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO}, 20681da177e4SLinus Torvalds [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO}, 20691da177e4SLinus Torvalds [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO}, 20701da177e4SLinus Torvalds [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO}, 20711da177e4SLinus Torvalds [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO}, 20721da177e4SLinus Torvalds [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR}, 20731da177e4SLinus Torvalds [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO}, 20741da177e4SLinus Torvalds [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR}, 20751da177e4SLinus Torvalds [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO}, 20761da177e4SLinus Torvalds [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR}, 20773f12070eSEric Paris [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO}, 20783f12070eSEric Paris [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO}, 207911904167SKaiGai Kohei [SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO}, 208072e8c859SEric Paris [SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO}, 2081f9df6458SAndrew Perepechko [SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops, 2082f9df6458SAndrew Perepechko S_IWUGO}, 20831da177e4SLinus Torvalds /* last one */ {""} 20841da177e4SLinus Torvalds }; 20850619f0f5SStephen Smalley 20860619f0f5SStephen Smalley ret = selinux_fs_info_create(sb); 20870619f0f5SStephen Smalley if (ret) 20880619f0f5SStephen Smalley goto err; 20890619f0f5SStephen Smalley 20901da177e4SLinus Torvalds ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files); 20911da177e4SLinus Torvalds if (ret) 2092161ce45aSJames Morris goto err; 20931da177e4SLinus Torvalds 20940619f0f5SStephen Smalley fsi = sb->s_fs_info; 20950619f0f5SStephen Smalley fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino); 20960619f0f5SStephen Smalley if (IS_ERR(fsi->bool_dir)) { 20970619f0f5SStephen Smalley ret = PTR_ERR(fsi->bool_dir); 20980619f0f5SStephen Smalley fsi->bool_dir = NULL; 2099161ce45aSJames Morris goto err; 2100a1c2aa1eSAl Viro } 21011da177e4SLinus Torvalds 2102b77a493bSEric Paris ret = -ENOMEM; 21031da177e4SLinus Torvalds dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME); 2104b77a493bSEric Paris if (!dentry) 2105161ce45aSJames Morris goto err; 21061da177e4SLinus Torvalds 2107161ce45aSJames Morris ret = -ENOMEM; 2108b77a493bSEric Paris inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO); 21097e4237faSnixiaoming if (!inode) { 21107e4237faSnixiaoming dput(dentry); 2111161ce45aSJames Morris goto err; 21127e4237faSnixiaoming } 2113b77a493bSEric Paris 21140619f0f5SStephen Smalley inode->i_ino = ++fsi->last_ino; 211580788c22SCasey Schaufler isec = selinux_inode(inode); 21161da177e4SLinus Torvalds isec->sid = SECINITSID_DEVNULL; 21171da177e4SLinus Torvalds isec->sclass = SECCLASS_CHR_FILE; 211842059112SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 21191da177e4SLinus Torvalds 21201da177e4SLinus Torvalds init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3)); 21211da177e4SLinus Torvalds d_add(dentry, inode); 21221da177e4SLinus Torvalds 21230619f0f5SStephen Smalley dentry = sel_make_dir(sb->s_root, "avc", &fsi->last_ino); 2124a1c2aa1eSAl Viro if (IS_ERR(dentry)) { 2125a1c2aa1eSAl Viro ret = PTR_ERR(dentry); 2126161ce45aSJames Morris goto err; 2127a1c2aa1eSAl Viro } 21281da177e4SLinus Torvalds 21291da177e4SLinus Torvalds ret = sel_make_avc_files(dentry); 2130bcb62828SChristian Göttsche if (ret) 2131bcb62828SChristian Göttsche goto err; 213266f8e2f0SJeff Vander Stoep 213366f8e2f0SJeff Vander Stoep dentry = sel_make_dir(sb->s_root, "ss", &fsi->last_ino); 213466f8e2f0SJeff Vander Stoep if (IS_ERR(dentry)) { 213566f8e2f0SJeff Vander Stoep ret = PTR_ERR(dentry); 213666f8e2f0SJeff Vander Stoep goto err; 213766f8e2f0SJeff Vander Stoep } 213866f8e2f0SJeff Vander Stoep 213966f8e2f0SJeff Vander Stoep ret = sel_make_ss_files(dentry); 21401da177e4SLinus Torvalds if (ret) 2141161ce45aSJames Morris goto err; 2142f0ee2e46SJames Carter 21430619f0f5SStephen Smalley dentry = sel_make_dir(sb->s_root, "initial_contexts", &fsi->last_ino); 2144a1c2aa1eSAl Viro if (IS_ERR(dentry)) { 2145a1c2aa1eSAl Viro ret = PTR_ERR(dentry); 2146f0ee2e46SJames Carter goto err; 2147a1c2aa1eSAl Viro } 2148f0ee2e46SJames Carter 2149f0ee2e46SJames Carter ret = sel_make_initcon_files(dentry); 2150f0ee2e46SJames Carter if (ret) 2151f0ee2e46SJames Carter goto err; 2152f0ee2e46SJames Carter 2153613ba187SDaniel Burgener fsi->class_dir = sel_make_dir(sb->s_root, CLASS_DIR_NAME, &fsi->last_ino); 21540619f0f5SStephen Smalley if (IS_ERR(fsi->class_dir)) { 21550619f0f5SStephen Smalley ret = PTR_ERR(fsi->class_dir); 21560619f0f5SStephen Smalley fsi->class_dir = NULL; 2157e47c8fc5SChristopher J. PeBenito goto err; 2158a1c2aa1eSAl Viro } 2159e47c8fc5SChristopher J. PeBenito 2160613ba187SDaniel Burgener fsi->policycap_dir = sel_make_dir(sb->s_root, POLICYCAP_DIR_NAME, 21610619f0f5SStephen Smalley &fsi->last_ino); 21620619f0f5SStephen Smalley if (IS_ERR(fsi->policycap_dir)) { 21630619f0f5SStephen Smalley ret = PTR_ERR(fsi->policycap_dir); 21640619f0f5SStephen Smalley fsi->policycap_dir = NULL; 2165e47c8fc5SChristopher J. PeBenito goto err; 2166a1c2aa1eSAl Viro } 21670619f0f5SStephen Smalley 216802a52c5cSStephen Smalley ret = sel_make_policycap(fsi); 216902a52c5cSStephen Smalley if (ret) { 217002a52c5cSStephen Smalley pr_err("SELinux: failed to load policy capabilities\n"); 21710619f0f5SStephen Smalley goto err; 217202a52c5cSStephen Smalley } 217302a52c5cSStephen Smalley 2174b77a493bSEric Paris return 0; 2175161ce45aSJames Morris err: 2176f8b69a5fSpeter enderborg pr_err("SELinux: %s: failed while creating inodes\n", 2177744ba35eSEric Paris __func__); 21780619f0f5SStephen Smalley 21790619f0f5SStephen Smalley selinux_fs_info_free(sb); 21800619f0f5SStephen Smalley 2181b77a493bSEric Paris return ret; 21821da177e4SLinus Torvalds } 21831da177e4SLinus Torvalds 2184920f50b2SDavid Howells static int sel_get_tree(struct fs_context *fc) 21851da177e4SLinus Torvalds { 2186920f50b2SDavid Howells return get_tree_single(fc, sel_fill_super); 2187920f50b2SDavid Howells } 2188920f50b2SDavid Howells 2189920f50b2SDavid Howells static const struct fs_context_operations sel_context_ops = { 2190920f50b2SDavid Howells .get_tree = sel_get_tree, 2191920f50b2SDavid Howells }; 2192920f50b2SDavid Howells 2193920f50b2SDavid Howells static int sel_init_fs_context(struct fs_context *fc) 2194920f50b2SDavid Howells { 2195920f50b2SDavid Howells fc->ops = &sel_context_ops; 2196920f50b2SDavid Howells return 0; 21971da177e4SLinus Torvalds } 21981da177e4SLinus Torvalds 21990619f0f5SStephen Smalley static void sel_kill_sb(struct super_block *sb) 22000619f0f5SStephen Smalley { 22010619f0f5SStephen Smalley selinux_fs_info_free(sb); 22020619f0f5SStephen Smalley kill_litter_super(sb); 22030619f0f5SStephen Smalley } 22040619f0f5SStephen Smalley 22051da177e4SLinus Torvalds static struct file_system_type sel_fs_type = { 22061da177e4SLinus Torvalds .name = "selinuxfs", 2207920f50b2SDavid Howells .init_fs_context = sel_init_fs_context, 22080619f0f5SStephen Smalley .kill_sb = sel_kill_sb, 22091da177e4SLinus Torvalds }; 22101da177e4SLinus Torvalds 2211cd2bb4cbSOndrej Mosnacek static struct vfsmount *selinuxfs_mount __ro_after_init; 2212cd2bb4cbSOndrej Mosnacek struct path selinux_null __ro_after_init; 22131da177e4SLinus Torvalds 22141da177e4SLinus Torvalds static int __init init_sel_fs(void) 22151da177e4SLinus Torvalds { 22160619f0f5SStephen Smalley struct qstr null_name = QSTR_INIT(NULL_FILE_NAME, 22170619f0f5SStephen Smalley sizeof(NULL_FILE_NAME)-1); 22181da177e4SLinus Torvalds int err; 22191da177e4SLinus Torvalds 22206c5a682eSStephen Smalley if (!selinux_enabled_boot) 22211da177e4SLinus Torvalds return 0; 22227a627e3bSGreg Kroah-Hartman 2223f9bb4882SEric W. Biederman err = sysfs_create_mount_point(fs_kobj, "selinux"); 2224f9bb4882SEric W. Biederman if (err) 2225f9bb4882SEric W. Biederman return err; 22267a627e3bSGreg Kroah-Hartman 22271da177e4SLinus Torvalds err = register_filesystem(&sel_fs_type); 22287a627e3bSGreg Kroah-Hartman if (err) { 2229f9bb4882SEric W. Biederman sysfs_remove_mount_point(fs_kobj, "selinux"); 2230b77a493bSEric Paris return err; 22317a627e3bSGreg Kroah-Hartman } 2232b77a493bSEric Paris 2233765927b2SAl Viro selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type); 22341da177e4SLinus Torvalds if (IS_ERR(selinuxfs_mount)) { 2235f8b69a5fSpeter enderborg pr_err("selinuxfs: could not mount!\n"); 22361da177e4SLinus Torvalds err = PTR_ERR(selinuxfs_mount); 22371da177e4SLinus Torvalds selinuxfs_mount = NULL; 22381da177e4SLinus Torvalds } 22390619f0f5SStephen Smalley selinux_null.dentry = d_hash_and_lookup(selinux_null.mnt->mnt_root, 22400619f0f5SStephen Smalley &null_name); 22410619f0f5SStephen Smalley if (IS_ERR(selinux_null.dentry)) { 22420619f0f5SStephen Smalley pr_err("selinuxfs: could not lookup null!\n"); 22430619f0f5SStephen Smalley err = PTR_ERR(selinux_null.dentry); 22440619f0f5SStephen Smalley selinux_null.dentry = NULL; 22450619f0f5SStephen Smalley } 2246b77a493bSEric Paris 22471da177e4SLinus Torvalds return err; 22481da177e4SLinus Torvalds } 22491da177e4SLinus Torvalds 22501da177e4SLinus Torvalds __initcall(init_sel_fs); 22511da177e4SLinus Torvalds 22521da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 22531da177e4SLinus Torvalds void exit_sel_fs(void) 22541da177e4SLinus Torvalds { 2255f9bb4882SEric W. Biederman sysfs_remove_mount_point(fs_kobj, "selinux"); 2256fd40ffc7SStephen Smalley dput(selinux_null.dentry); 2257423e0ab0STim Chen kern_unmount(selinuxfs_mount); 22581da177e4SLinus Torvalds unregister_filesystem(&sel_fs_type); 22591da177e4SLinus Torvalds } 22601da177e4SLinus Torvalds #endif 2261