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" 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds enum sel_inos { 461da177e4SLinus Torvalds SEL_ROOT_INO = 2, 471da177e4SLinus Torvalds SEL_LOAD, /* load policy */ 481da177e4SLinus Torvalds SEL_ENFORCE, /* get or set enforcing status */ 491da177e4SLinus Torvalds SEL_CONTEXT, /* validate context */ 501da177e4SLinus Torvalds SEL_ACCESS, /* compute access decision */ 511da177e4SLinus Torvalds SEL_CREATE, /* compute create labeling decision */ 521da177e4SLinus Torvalds SEL_RELABEL, /* compute relabeling decision */ 531da177e4SLinus Torvalds SEL_USER, /* compute reachable user contexts */ 541da177e4SLinus Torvalds SEL_POLICYVERS, /* return policy version for this kernel */ 551da177e4SLinus Torvalds SEL_COMMIT_BOOLS, /* commit new boolean values */ 561da177e4SLinus Torvalds SEL_MLS, /* return if MLS policy is enabled */ 571da177e4SLinus Torvalds SEL_DISABLE, /* disable SELinux until next reboot */ 581da177e4SLinus Torvalds SEL_MEMBER, /* compute polyinstantiation membership decision */ 591da177e4SLinus Torvalds SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */ 604e5ab4cbSJames Morris SEL_COMPAT_NET, /* whether to use old compat network packet controls */ 613f12070eSEric Paris SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */ 623f12070eSEric Paris SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */ 6311904167SKaiGai Kohei SEL_STATUS, /* export current status using mmap() */ 64cee74f47SEric Paris SEL_POLICY, /* allow userspace to read the in kernel policy */ 65f9df6458SAndrew Perepechko SEL_VALIDATE_TRANS, /* compute validatetrans decision */ 666174eafcSJames Carter SEL_INO_NEXT, /* The next inode number to use */ 671da177e4SLinus Torvalds }; 681da177e4SLinus Torvalds 690619f0f5SStephen Smalley struct selinux_fs_info { 700619f0f5SStephen Smalley struct dentry *bool_dir; 710619f0f5SStephen Smalley unsigned int bool_num; 720619f0f5SStephen Smalley char **bool_pending_names; 730619f0f5SStephen Smalley unsigned int *bool_pending_values; 740619f0f5SStephen Smalley struct dentry *class_dir; 750619f0f5SStephen Smalley unsigned long last_class_ino; 760619f0f5SStephen Smalley bool policy_opened; 770619f0f5SStephen Smalley struct dentry *policycap_dir; 780619f0f5SStephen Smalley unsigned long last_ino; 790619f0f5SStephen Smalley struct selinux_state *state; 800619f0f5SStephen Smalley struct super_block *sb; 810619f0f5SStephen Smalley }; 820619f0f5SStephen Smalley 830619f0f5SStephen Smalley static int selinux_fs_info_create(struct super_block *sb) 840619f0f5SStephen Smalley { 850619f0f5SStephen Smalley struct selinux_fs_info *fsi; 860619f0f5SStephen Smalley 870619f0f5SStephen Smalley fsi = kzalloc(sizeof(*fsi), GFP_KERNEL); 880619f0f5SStephen Smalley if (!fsi) 890619f0f5SStephen Smalley return -ENOMEM; 900619f0f5SStephen Smalley 910619f0f5SStephen Smalley fsi->last_ino = SEL_INO_NEXT - 1; 920619f0f5SStephen Smalley fsi->state = &selinux_state; 930619f0f5SStephen Smalley fsi->sb = sb; 940619f0f5SStephen Smalley sb->s_fs_info = fsi; 950619f0f5SStephen Smalley return 0; 960619f0f5SStephen Smalley } 970619f0f5SStephen Smalley 980619f0f5SStephen Smalley static void selinux_fs_info_free(struct super_block *sb) 990619f0f5SStephen Smalley { 1000619f0f5SStephen Smalley struct selinux_fs_info *fsi = sb->s_fs_info; 1010619f0f5SStephen Smalley int i; 1020619f0f5SStephen Smalley 1030619f0f5SStephen Smalley if (fsi) { 1040619f0f5SStephen Smalley for (i = 0; i < fsi->bool_num; i++) 1050619f0f5SStephen Smalley kfree(fsi->bool_pending_names[i]); 1060619f0f5SStephen Smalley kfree(fsi->bool_pending_names); 1070619f0f5SStephen Smalley kfree(fsi->bool_pending_values); 1080619f0f5SStephen Smalley } 1090619f0f5SStephen Smalley kfree(sb->s_fs_info); 1100619f0f5SStephen Smalley sb->s_fs_info = NULL; 1110619f0f5SStephen Smalley } 1126174eafcSJames Carter 113f0ee2e46SJames Carter #define SEL_INITCON_INO_OFFSET 0x01000000 114bce34bc0SJames Carter #define SEL_BOOL_INO_OFFSET 0x02000000 115e47c8fc5SChristopher J. PeBenito #define SEL_CLASS_INO_OFFSET 0x04000000 1163bb56b25SPaul Moore #define SEL_POLICYCAP_INO_OFFSET 0x08000000 117f0ee2e46SJames Carter #define SEL_INO_MASK 0x00ffffff 118f0ee2e46SJames Carter 119613ba187SDaniel Burgener #define BOOL_DIR_NAME "booleans" 120613ba187SDaniel Burgener #define CLASS_DIR_NAME "class" 121613ba187SDaniel Burgener #define POLICYCAP_DIR_NAME "policy_capabilities" 122613ba187SDaniel Burgener 1231da177e4SLinus Torvalds #define TMPBUFLEN 12 1241da177e4SLinus Torvalds static ssize_t sel_read_enforce(struct file *filp, char __user *buf, 1251da177e4SLinus Torvalds size_t count, loff_t *ppos) 1261da177e4SLinus Torvalds { 1270619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info; 1281da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 1291da177e4SLinus Torvalds ssize_t length; 1301da177e4SLinus Torvalds 131aa8e712cSStephen Smalley length = scnprintf(tmpbuf, TMPBUFLEN, "%d", 1320619f0f5SStephen Smalley enforcing_enabled(fsi->state)); 1331da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 1341da177e4SLinus Torvalds } 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP 1371da177e4SLinus Torvalds static ssize_t sel_write_enforce(struct file *file, const char __user *buf, 1381da177e4SLinus Torvalds size_t count, loff_t *ppos) 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds { 1410619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 1420619f0f5SStephen Smalley struct selinux_state *state = fsi->state; 143b77a493bSEric Paris char *page = NULL; 1441da177e4SLinus Torvalds ssize_t length; 145aa8e712cSStephen Smalley int old_value, new_value; 1461da177e4SLinus Torvalds 147bfd51626SDavi Arnaut if (count >= PAGE_SIZE) 1488365a719SAl Viro return -ENOMEM; 149b77a493bSEric Paris 1501da177e4SLinus Torvalds /* No partial writes. */ 151b77a493bSEric Paris if (*ppos != 0) 1528365a719SAl Viro return -EINVAL; 153b77a493bSEric Paris 1548365a719SAl Viro page = memdup_user_nul(buf, count); 1558365a719SAl Viro if (IS_ERR(page)) 1568365a719SAl Viro return PTR_ERR(page); 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds length = -EINVAL; 1591da177e4SLinus Torvalds if (sscanf(page, "%d", &new_value) != 1) 1601da177e4SLinus Torvalds goto out; 1611da177e4SLinus Torvalds 162ea49d10eSStephen Smalley new_value = !!new_value; 163ea49d10eSStephen Smalley 1640619f0f5SStephen Smalley old_value = enforcing_enabled(state); 165aa8e712cSStephen Smalley if (new_value != old_value) { 1666b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 1676b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 168be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETENFORCE, 169be0554c9SStephen Smalley NULL); 1701da177e4SLinus Torvalds if (length) 1711da177e4SLinus Torvalds goto out; 172cdfb6b34SRichard Guy Briggs audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS, 1734195ed42SRichard Guy Briggs "enforcing=%d old_enforcing=%d auid=%u ses=%u" 1746c5a682eSStephen Smalley " enabled=1 old-enabled=1 lsm=selinux res=1", 175aa8e712cSStephen Smalley new_value, old_value, 176581abc09SEric W. Biederman from_kuid(&init_user_ns, audit_get_loginuid(current)), 1776c5a682eSStephen Smalley audit_get_sessionid(current)); 1780619f0f5SStephen Smalley enforcing_set(state, new_value); 179aa8e712cSStephen Smalley if (new_value) 1806b6bc620SStephen Smalley avc_ss_reset(state->avc, 0); 181aa8e712cSStephen Smalley selnl_notify_setenforce(new_value); 1820619f0f5SStephen Smalley selinux_status_update_setenforce(state, new_value); 183aa8e712cSStephen Smalley if (!new_value) 18442df744cSJanne Karhunen call_blocking_lsm_notifier(LSM_POLICY_CHANGE, NULL); 1851da177e4SLinus Torvalds } 1861da177e4SLinus Torvalds length = count; 1871da177e4SLinus Torvalds out: 1888365a719SAl Viro kfree(page); 1891da177e4SLinus Torvalds return length; 1901da177e4SLinus Torvalds } 1911da177e4SLinus Torvalds #else 1921da177e4SLinus Torvalds #define sel_write_enforce NULL 1931da177e4SLinus Torvalds #endif 1941da177e4SLinus Torvalds 1959c2e08c5SArjan van de Ven static const struct file_operations sel_enforce_ops = { 1961da177e4SLinus Torvalds .read = sel_read_enforce, 1971da177e4SLinus Torvalds .write = sel_write_enforce, 19857a62c23SArnd Bergmann .llseek = generic_file_llseek, 1991da177e4SLinus Torvalds }; 2001da177e4SLinus Torvalds 2013f12070eSEric Paris static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf, 2023f12070eSEric Paris size_t count, loff_t *ppos) 2033f12070eSEric Paris { 2040619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info; 2050619f0f5SStephen Smalley struct selinux_state *state = fsi->state; 2063f12070eSEric Paris char tmpbuf[TMPBUFLEN]; 2073f12070eSEric Paris ssize_t length; 208496ad9aaSAl Viro ino_t ino = file_inode(filp)->i_ino; 2093f12070eSEric Paris int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ? 2100619f0f5SStephen Smalley security_get_reject_unknown(state) : 2110619f0f5SStephen Smalley !security_get_allow_unknown(state); 2123f12070eSEric Paris 2133f12070eSEric Paris length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown); 2143f12070eSEric Paris return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 2153f12070eSEric Paris } 2163f12070eSEric Paris 2173f12070eSEric Paris static const struct file_operations sel_handle_unknown_ops = { 2183f12070eSEric Paris .read = sel_read_handle_unknown, 21957a62c23SArnd Bergmann .llseek = generic_file_llseek, 2203f12070eSEric Paris }; 2213f12070eSEric Paris 22211904167SKaiGai Kohei static int sel_open_handle_status(struct inode *inode, struct file *filp) 22311904167SKaiGai Kohei { 2240619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info; 2250619f0f5SStephen Smalley struct page *status = selinux_kernel_status_page(fsi->state); 22611904167SKaiGai Kohei 22711904167SKaiGai Kohei if (!status) 22811904167SKaiGai Kohei return -ENOMEM; 22911904167SKaiGai Kohei 23011904167SKaiGai Kohei filp->private_data = status; 23111904167SKaiGai Kohei 23211904167SKaiGai Kohei return 0; 23311904167SKaiGai Kohei } 23411904167SKaiGai Kohei 23511904167SKaiGai Kohei static ssize_t sel_read_handle_status(struct file *filp, char __user *buf, 23611904167SKaiGai Kohei size_t count, loff_t *ppos) 23711904167SKaiGai Kohei { 23811904167SKaiGai Kohei struct page *status = filp->private_data; 23911904167SKaiGai Kohei 24011904167SKaiGai Kohei BUG_ON(!status); 24111904167SKaiGai Kohei 24211904167SKaiGai Kohei return simple_read_from_buffer(buf, count, ppos, 24311904167SKaiGai Kohei page_address(status), 24411904167SKaiGai Kohei sizeof(struct selinux_kernel_status)); 24511904167SKaiGai Kohei } 24611904167SKaiGai Kohei 24711904167SKaiGai Kohei static int sel_mmap_handle_status(struct file *filp, 24811904167SKaiGai Kohei struct vm_area_struct *vma) 24911904167SKaiGai Kohei { 25011904167SKaiGai Kohei struct page *status = filp->private_data; 25111904167SKaiGai Kohei unsigned long size = vma->vm_end - vma->vm_start; 25211904167SKaiGai Kohei 25311904167SKaiGai Kohei BUG_ON(!status); 25411904167SKaiGai Kohei 25511904167SKaiGai Kohei /* only allows one page from the head */ 25611904167SKaiGai Kohei if (vma->vm_pgoff > 0 || size != PAGE_SIZE) 25711904167SKaiGai Kohei return -EIO; 25811904167SKaiGai Kohei /* disallow writable mapping */ 25911904167SKaiGai Kohei if (vma->vm_flags & VM_WRITE) 26011904167SKaiGai Kohei return -EPERM; 26111904167SKaiGai Kohei /* disallow mprotect() turns it into writable */ 26211904167SKaiGai Kohei vma->vm_flags &= ~VM_MAYWRITE; 26311904167SKaiGai Kohei 26411904167SKaiGai Kohei return remap_pfn_range(vma, vma->vm_start, 26511904167SKaiGai Kohei page_to_pfn(status), 26611904167SKaiGai Kohei size, vma->vm_page_prot); 26711904167SKaiGai Kohei } 26811904167SKaiGai Kohei 26911904167SKaiGai Kohei static const struct file_operations sel_handle_status_ops = { 27011904167SKaiGai Kohei .open = sel_open_handle_status, 27111904167SKaiGai Kohei .read = sel_read_handle_status, 27211904167SKaiGai Kohei .mmap = sel_mmap_handle_status, 27311904167SKaiGai Kohei .llseek = generic_file_llseek, 27411904167SKaiGai Kohei }; 27511904167SKaiGai Kohei 2761da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 2771da177e4SLinus Torvalds static ssize_t sel_write_disable(struct file *file, const char __user *buf, 2781da177e4SLinus Torvalds size_t count, loff_t *ppos) 2791da177e4SLinus Torvalds 2801da177e4SLinus Torvalds { 2810619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 2828365a719SAl Viro char *page; 2831da177e4SLinus Torvalds ssize_t length; 2841da177e4SLinus Torvalds int new_value; 2854195ed42SRichard Guy Briggs int enforcing; 2861da177e4SLinus Torvalds 28789b223bfSPaul Moore /* NOTE: we are now officially considering runtime disable as 28889b223bfSPaul Moore * deprecated, and using it will become increasingly painful 28989b223bfSPaul Moore * (e.g. sleeping/blocking) as we progress through future 29089b223bfSPaul Moore * kernel releases until eventually it is removed 29189b223bfSPaul Moore */ 29289b223bfSPaul Moore pr_err("SELinux: Runtime disable is deprecated, use selinux=0 on the kernel cmdline.\n"); 29389b223bfSPaul Moore 294bfd51626SDavi Arnaut if (count >= PAGE_SIZE) 2958365a719SAl Viro return -ENOMEM; 296b77a493bSEric Paris 2971da177e4SLinus Torvalds /* No partial writes. */ 298b77a493bSEric Paris if (*ppos != 0) 2998365a719SAl Viro return -EINVAL; 300b77a493bSEric Paris 3018365a719SAl Viro page = memdup_user_nul(buf, count); 3028365a719SAl Viro if (IS_ERR(page)) 3038365a719SAl Viro return PTR_ERR(page); 3041da177e4SLinus Torvalds 3051da177e4SLinus Torvalds length = -EINVAL; 3061da177e4SLinus Torvalds if (sscanf(page, "%d", &new_value) != 1) 3071da177e4SLinus Torvalds goto out; 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds if (new_value) { 3104195ed42SRichard Guy Briggs enforcing = enforcing_enabled(fsi->state); 3110619f0f5SStephen Smalley length = selinux_disable(fsi->state); 312b77a493bSEric Paris if (length) 3131da177e4SLinus Torvalds goto out; 314cdfb6b34SRichard Guy Briggs audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS, 3154195ed42SRichard Guy Briggs "enforcing=%d old_enforcing=%d auid=%u ses=%u" 3166c5a682eSStephen Smalley " enabled=0 old-enabled=1 lsm=selinux res=1", 3174195ed42SRichard Guy Briggs enforcing, enforcing, 318581abc09SEric W. Biederman from_kuid(&init_user_ns, audit_get_loginuid(current)), 3196c5a682eSStephen Smalley audit_get_sessionid(current)); 3201da177e4SLinus Torvalds } 3211da177e4SLinus Torvalds 3221da177e4SLinus Torvalds length = count; 3231da177e4SLinus Torvalds out: 3248365a719SAl Viro kfree(page); 3251da177e4SLinus Torvalds return length; 3261da177e4SLinus Torvalds } 3271da177e4SLinus Torvalds #else 3281da177e4SLinus Torvalds #define sel_write_disable NULL 3291da177e4SLinus Torvalds #endif 3301da177e4SLinus Torvalds 3319c2e08c5SArjan van de Ven static const struct file_operations sel_disable_ops = { 3321da177e4SLinus Torvalds .write = sel_write_disable, 33357a62c23SArnd Bergmann .llseek = generic_file_llseek, 3341da177e4SLinus Torvalds }; 3351da177e4SLinus Torvalds 3361da177e4SLinus Torvalds static ssize_t sel_read_policyvers(struct file *filp, char __user *buf, 3371da177e4SLinus Torvalds size_t count, loff_t *ppos) 3381da177e4SLinus Torvalds { 3391da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 3401da177e4SLinus Torvalds ssize_t length; 3411da177e4SLinus Torvalds 3421da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX); 3431da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 3441da177e4SLinus Torvalds } 3451da177e4SLinus Torvalds 3469c2e08c5SArjan van de Ven static const struct file_operations sel_policyvers_ops = { 3471da177e4SLinus Torvalds .read = sel_read_policyvers, 34857a62c23SArnd Bergmann .llseek = generic_file_llseek, 3491da177e4SLinus Torvalds }; 3501da177e4SLinus Torvalds 3511da177e4SLinus Torvalds /* declaration for sel_write_load */ 35266ec384aSDaniel Burgener static int sel_make_bools(struct selinux_policy *newpolicy, struct dentry *bool_dir, 35366ec384aSDaniel Burgener unsigned int *bool_num, char ***bool_pending_names, 35466ec384aSDaniel Burgener unsigned int **bool_pending_values); 35566ec384aSDaniel Burgener static int sel_make_classes(struct selinux_policy *newpolicy, 35666ec384aSDaniel Burgener struct dentry *class_dir, 35766ec384aSDaniel Burgener unsigned long *last_class_ino); 358e47c8fc5SChristopher J. PeBenito 359e47c8fc5SChristopher J. PeBenito /* declaration for sel_make_class_dirs */ 360a1c2aa1eSAl Viro static struct dentry *sel_make_dir(struct dentry *dir, const char *name, 361e47c8fc5SChristopher J. PeBenito unsigned long *ino); 3621da177e4SLinus Torvalds 3630eea6091SDaniel Burgener /* declaration for sel_make_policy_nodes */ 3640eea6091SDaniel Burgener static struct dentry *sel_make_disconnected_dir(struct super_block *sb, 3650eea6091SDaniel Burgener unsigned long *ino); 3660eea6091SDaniel Burgener 3670eea6091SDaniel Burgener /* declaration for sel_make_policy_nodes */ 368aeecf4a3SDaniel Burgener static void sel_remove_entries(struct dentry *de); 369aeecf4a3SDaniel Burgener 3701da177e4SLinus Torvalds static ssize_t sel_read_mls(struct file *filp, char __user *buf, 3711da177e4SLinus Torvalds size_t count, loff_t *ppos) 3721da177e4SLinus Torvalds { 3730619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info; 3741da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 3751da177e4SLinus Torvalds ssize_t length; 3761da177e4SLinus Torvalds 3770719aaf5SGuido Trentalancia length = scnprintf(tmpbuf, TMPBUFLEN, "%d", 3780619f0f5SStephen Smalley security_mls_enabled(fsi->state)); 3791da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 3801da177e4SLinus Torvalds } 3811da177e4SLinus Torvalds 3829c2e08c5SArjan van de Ven static const struct file_operations sel_mls_ops = { 3831da177e4SLinus Torvalds .read = sel_read_mls, 38457a62c23SArnd Bergmann .llseek = generic_file_llseek, 3851da177e4SLinus Torvalds }; 3861da177e4SLinus Torvalds 387cee74f47SEric Paris struct policy_load_memory { 388cee74f47SEric Paris size_t len; 389cee74f47SEric Paris void *data; 390cee74f47SEric Paris }; 391cee74f47SEric Paris 392cee74f47SEric Paris static int sel_open_policy(struct inode *inode, struct file *filp) 393cee74f47SEric Paris { 3940619f0f5SStephen Smalley struct selinux_fs_info *fsi = inode->i_sb->s_fs_info; 3950619f0f5SStephen Smalley struct selinux_state *state = fsi->state; 396cee74f47SEric Paris struct policy_load_memory *plm = NULL; 397cee74f47SEric Paris int rc; 398cee74f47SEric Paris 399cee74f47SEric Paris BUG_ON(filp->private_data); 400cee74f47SEric Paris 4019ff9abc4SStephen Smalley mutex_lock(&fsi->state->policy_mutex); 402cee74f47SEric Paris 4036b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 4046b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 405be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL); 406cee74f47SEric Paris if (rc) 407cee74f47SEric Paris goto err; 408cee74f47SEric Paris 409cee74f47SEric Paris rc = -EBUSY; 4100619f0f5SStephen Smalley if (fsi->policy_opened) 411cee74f47SEric Paris goto err; 412cee74f47SEric Paris 413cee74f47SEric Paris rc = -ENOMEM; 414cee74f47SEric Paris plm = kzalloc(sizeof(*plm), GFP_KERNEL); 415cee74f47SEric Paris if (!plm) 416cee74f47SEric Paris goto err; 417cee74f47SEric Paris 4180619f0f5SStephen Smalley rc = security_read_policy(state, &plm->data, &plm->len); 419cee74f47SEric Paris if (rc) 420cee74f47SEric Paris goto err; 421cee74f47SEric Paris 42266ccd256SOndrej Mosnacek if ((size_t)i_size_read(inode) != plm->len) { 42366ccd256SOndrej Mosnacek inode_lock(inode); 42466ccd256SOndrej Mosnacek i_size_write(inode, plm->len); 42566ccd256SOndrej Mosnacek inode_unlock(inode); 42666ccd256SOndrej Mosnacek } 42766ccd256SOndrej Mosnacek 4280619f0f5SStephen Smalley fsi->policy_opened = 1; 429cee74f47SEric Paris 430cee74f47SEric Paris filp->private_data = plm; 431cee74f47SEric Paris 4329ff9abc4SStephen Smalley mutex_unlock(&fsi->state->policy_mutex); 433cee74f47SEric Paris 434cee74f47SEric Paris return 0; 435cee74f47SEric Paris err: 4369ff9abc4SStephen Smalley mutex_unlock(&fsi->state->policy_mutex); 437cee74f47SEric Paris 438cee74f47SEric Paris if (plm) 439cee74f47SEric Paris vfree(plm->data); 440cee74f47SEric Paris kfree(plm); 441cee74f47SEric Paris return rc; 442cee74f47SEric Paris } 443cee74f47SEric Paris 444cee74f47SEric Paris static int sel_release_policy(struct inode *inode, struct file *filp) 445cee74f47SEric Paris { 4460619f0f5SStephen Smalley struct selinux_fs_info *fsi = inode->i_sb->s_fs_info; 447cee74f47SEric Paris struct policy_load_memory *plm = filp->private_data; 448cee74f47SEric Paris 449cee74f47SEric Paris BUG_ON(!plm); 450cee74f47SEric Paris 4510619f0f5SStephen Smalley fsi->policy_opened = 0; 452cee74f47SEric Paris 453cee74f47SEric Paris vfree(plm->data); 454cee74f47SEric Paris kfree(plm); 455cee74f47SEric Paris 456cee74f47SEric Paris return 0; 457cee74f47SEric Paris } 458cee74f47SEric Paris 459cee74f47SEric Paris static ssize_t sel_read_policy(struct file *filp, char __user *buf, 460cee74f47SEric Paris size_t count, loff_t *ppos) 461cee74f47SEric Paris { 462cee74f47SEric Paris struct policy_load_memory *plm = filp->private_data; 463cee74f47SEric Paris int ret; 464cee74f47SEric Paris 4656b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 4666b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 467be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL); 468cee74f47SEric Paris if (ret) 469cee74f47SEric Paris return ret; 4700da74120SJann Horn 4710da74120SJann Horn return simple_read_from_buffer(buf, count, ppos, plm->data, plm->len); 472cee74f47SEric Paris } 473cee74f47SEric Paris 474ac9a1f6dSSouptick Joarder static vm_fault_t sel_mmap_policy_fault(struct vm_fault *vmf) 475845ca30fSEric Paris { 47611bac800SDave Jiang struct policy_load_memory *plm = vmf->vma->vm_file->private_data; 477845ca30fSEric Paris unsigned long offset; 478845ca30fSEric Paris struct page *page; 479845ca30fSEric Paris 480845ca30fSEric Paris if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE)) 481845ca30fSEric Paris return VM_FAULT_SIGBUS; 482845ca30fSEric Paris 483845ca30fSEric Paris offset = vmf->pgoff << PAGE_SHIFT; 484845ca30fSEric Paris if (offset >= roundup(plm->len, PAGE_SIZE)) 485845ca30fSEric Paris return VM_FAULT_SIGBUS; 486845ca30fSEric Paris 487845ca30fSEric Paris page = vmalloc_to_page(plm->data + offset); 488845ca30fSEric Paris get_page(page); 489845ca30fSEric Paris 490845ca30fSEric Paris vmf->page = page; 491845ca30fSEric Paris 492845ca30fSEric Paris return 0; 493845ca30fSEric Paris } 494845ca30fSEric Paris 4957cbea8dcSKirill A. Shutemov static const struct vm_operations_struct sel_mmap_policy_ops = { 496845ca30fSEric Paris .fault = sel_mmap_policy_fault, 497845ca30fSEric Paris .page_mkwrite = sel_mmap_policy_fault, 498845ca30fSEric Paris }; 499845ca30fSEric Paris 500ad3fa08cSJames Morris static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma) 501845ca30fSEric Paris { 502845ca30fSEric Paris if (vma->vm_flags & VM_SHARED) { 503845ca30fSEric Paris /* do not allow mprotect to make mapping writable */ 504845ca30fSEric Paris vma->vm_flags &= ~VM_MAYWRITE; 505845ca30fSEric Paris 506845ca30fSEric Paris if (vma->vm_flags & VM_WRITE) 507845ca30fSEric Paris return -EACCES; 508845ca30fSEric Paris } 509845ca30fSEric Paris 510314e51b9SKonstantin Khlebnikov vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; 511845ca30fSEric Paris vma->vm_ops = &sel_mmap_policy_ops; 512845ca30fSEric Paris 513845ca30fSEric Paris return 0; 514845ca30fSEric Paris } 515845ca30fSEric Paris 516cee74f47SEric Paris static const struct file_operations sel_policy_ops = { 517cee74f47SEric Paris .open = sel_open_policy, 518cee74f47SEric Paris .read = sel_read_policy, 519845ca30fSEric Paris .mmap = sel_mmap_policy, 520cee74f47SEric Paris .release = sel_release_policy, 52147a93a5bSEric Paris .llseek = generic_file_llseek, 522cee74f47SEric Paris }; 523cee74f47SEric Paris 5240eea6091SDaniel Burgener static void sel_remove_old_bool_data(unsigned int bool_num, char **bool_names, 5250eea6091SDaniel Burgener unsigned int *bool_values) 526aeecf4a3SDaniel Burgener { 527aeecf4a3SDaniel Burgener u32 i; 528aeecf4a3SDaniel Burgener 529aeecf4a3SDaniel Burgener /* bool_dir cleanup */ 5300eea6091SDaniel Burgener for (i = 0; i < bool_num; i++) 5310eea6091SDaniel Burgener kfree(bool_names[i]); 5320eea6091SDaniel Burgener kfree(bool_names); 5330eea6091SDaniel Burgener kfree(bool_values); 534aeecf4a3SDaniel Burgener } 535aeecf4a3SDaniel Burgener 53602a52c5cSStephen Smalley static int sel_make_policy_nodes(struct selinux_fs_info *fsi, 53702a52c5cSStephen Smalley struct selinux_policy *newpolicy) 5380619f0f5SStephen Smalley { 5390eea6091SDaniel Burgener int ret = 0; 5400eea6091SDaniel Burgener struct dentry *tmp_parent, *tmp_bool_dir, *tmp_class_dir, *old_dentry; 5410eea6091SDaniel Burgener unsigned int tmp_bool_num, old_bool_num; 5420eea6091SDaniel Burgener char **tmp_bool_names, **old_bool_names; 5430eea6091SDaniel Burgener unsigned int *tmp_bool_values, *old_bool_values; 5440eea6091SDaniel Burgener unsigned long tmp_ino = fsi->last_ino; /* Don't increment last_ino in this function */ 5450619f0f5SStephen Smalley 5460eea6091SDaniel Burgener tmp_parent = sel_make_disconnected_dir(fsi->sb, &tmp_ino); 5470eea6091SDaniel Burgener if (IS_ERR(tmp_parent)) 5480eea6091SDaniel Burgener return PTR_ERR(tmp_parent); 549aeecf4a3SDaniel Burgener 5500eea6091SDaniel Burgener tmp_ino = fsi->bool_dir->d_inode->i_ino - 1; /* sel_make_dir will increment and set */ 5510eea6091SDaniel Burgener tmp_bool_dir = sel_make_dir(tmp_parent, BOOL_DIR_NAME, &tmp_ino); 5520eea6091SDaniel Burgener if (IS_ERR(tmp_bool_dir)) { 5530eea6091SDaniel Burgener ret = PTR_ERR(tmp_bool_dir); 5540eea6091SDaniel Burgener goto out; 5550619f0f5SStephen Smalley } 5560619f0f5SStephen Smalley 5570eea6091SDaniel Burgener tmp_ino = fsi->class_dir->d_inode->i_ino - 1; /* sel_make_dir will increment and set */ 5580eea6091SDaniel Burgener tmp_class_dir = sel_make_dir(tmp_parent, CLASS_DIR_NAME, &tmp_ino); 5590eea6091SDaniel Burgener if (IS_ERR(tmp_class_dir)) { 5600eea6091SDaniel Burgener ret = PTR_ERR(tmp_class_dir); 5610eea6091SDaniel Burgener goto out; 5620eea6091SDaniel Burgener } 5630eea6091SDaniel Burgener 5640eea6091SDaniel Burgener ret = sel_make_bools(newpolicy, tmp_bool_dir, &tmp_bool_num, 5650eea6091SDaniel Burgener &tmp_bool_names, &tmp_bool_values); 5660eea6091SDaniel Burgener if (ret) { 5670eea6091SDaniel Burgener pr_err("SELinux: failed to load policy booleans\n"); 5680eea6091SDaniel Burgener goto out; 5690eea6091SDaniel Burgener } 5700eea6091SDaniel Burgener 5710eea6091SDaniel Burgener ret = sel_make_classes(newpolicy, tmp_class_dir, 57266ec384aSDaniel Burgener &fsi->last_class_ino); 5730619f0f5SStephen Smalley if (ret) { 5740619f0f5SStephen Smalley pr_err("SELinux: failed to load policy classes\n"); 5750eea6091SDaniel Burgener goto out; 5760619f0f5SStephen Smalley } 5770619f0f5SStephen Smalley 5780eea6091SDaniel Burgener /* booleans */ 5790eea6091SDaniel Burgener old_dentry = fsi->bool_dir; 5800eea6091SDaniel Burgener lock_rename(tmp_bool_dir, old_dentry); 5810eea6091SDaniel Burgener d_exchange(tmp_bool_dir, fsi->bool_dir); 5820eea6091SDaniel Burgener 5830eea6091SDaniel Burgener old_bool_num = fsi->bool_num; 5840eea6091SDaniel Burgener old_bool_names = fsi->bool_pending_names; 5850eea6091SDaniel Burgener old_bool_values = fsi->bool_pending_values; 5860eea6091SDaniel Burgener 5870eea6091SDaniel Burgener fsi->bool_num = tmp_bool_num; 5880eea6091SDaniel Burgener fsi->bool_pending_names = tmp_bool_names; 5890eea6091SDaniel Burgener fsi->bool_pending_values = tmp_bool_values; 5900eea6091SDaniel Burgener 5910eea6091SDaniel Burgener sel_remove_old_bool_data(old_bool_num, old_bool_names, old_bool_values); 5920eea6091SDaniel Burgener 5930eea6091SDaniel Burgener fsi->bool_dir = tmp_bool_dir; 5940eea6091SDaniel Burgener unlock_rename(tmp_bool_dir, old_dentry); 5950eea6091SDaniel Burgener 5960eea6091SDaniel Burgener /* classes */ 5970eea6091SDaniel Burgener old_dentry = fsi->class_dir; 5980eea6091SDaniel Burgener lock_rename(tmp_class_dir, old_dentry); 5990eea6091SDaniel Burgener d_exchange(tmp_class_dir, fsi->class_dir); 6000eea6091SDaniel Burgener fsi->class_dir = tmp_class_dir; 6010eea6091SDaniel Burgener unlock_rename(tmp_class_dir, old_dentry); 6020eea6091SDaniel Burgener 6030eea6091SDaniel Burgener out: 6040eea6091SDaniel Burgener /* Since the other temporary dirs are children of tmp_parent 6050eea6091SDaniel Burgener * this will handle all the cleanup in the case of a failure before 6060eea6091SDaniel Burgener * the swapover 6070eea6091SDaniel Burgener */ 6080eea6091SDaniel Burgener sel_remove_entries(tmp_parent); 6090eea6091SDaniel Burgener dput(tmp_parent); /* d_genocide() only handles the children */ 6100eea6091SDaniel Burgener 6110eea6091SDaniel Burgener return ret; 6120619f0f5SStephen Smalley } 6130619f0f5SStephen Smalley 6141da177e4SLinus Torvalds static ssize_t sel_write_load(struct file *file, const char __user *buf, 6151da177e4SLinus Torvalds size_t count, loff_t *ppos) 6161da177e4SLinus Torvalds 6171da177e4SLinus Torvalds { 6180619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 61902a52c5cSStephen Smalley struct selinux_policy *newpolicy; 6201da177e4SLinus Torvalds ssize_t length; 6211da177e4SLinus Torvalds void *data = NULL; 6221da177e4SLinus Torvalds 6239ff9abc4SStephen Smalley mutex_lock(&fsi->state->policy_mutex); 6241da177e4SLinus Torvalds 6256b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 6266b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 627be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL); 6281da177e4SLinus Torvalds if (length) 6291da177e4SLinus Torvalds goto out; 6301da177e4SLinus Torvalds 6311da177e4SLinus Torvalds /* No partial writes. */ 6321da177e4SLinus Torvalds length = -EINVAL; 633b77a493bSEric Paris if (*ppos != 0) 6341da177e4SLinus Torvalds goto out; 6351da177e4SLinus Torvalds 636b77a493bSEric Paris length = -ENOMEM; 637b77a493bSEric Paris data = vmalloc(count); 638b77a493bSEric Paris if (!data) 639b77a493bSEric Paris goto out; 6401da177e4SLinus Torvalds 6411da177e4SLinus Torvalds length = -EFAULT; 6421da177e4SLinus Torvalds if (copy_from_user(data, buf, count) != 0) 6431da177e4SLinus Torvalds goto out; 6441da177e4SLinus Torvalds 64502a52c5cSStephen Smalley length = security_load_policy(fsi->state, data, count, &newpolicy); 6464262fb51SGary Tierney if (length) { 6474262fb51SGary Tierney pr_warn_ratelimited("SELinux: failed to load policy\n"); 6481da177e4SLinus Torvalds goto out; 6494262fb51SGary Tierney } 6501da177e4SLinus Torvalds 65102a52c5cSStephen Smalley length = sel_make_policy_nodes(fsi, newpolicy); 65202a52c5cSStephen Smalley if (length) { 65302a52c5cSStephen Smalley selinux_policy_cancel(fsi->state, newpolicy); 654*519dad3bSOndrej Mosnacek goto out; 65502a52c5cSStephen Smalley } 65602a52c5cSStephen Smalley 65702a52c5cSStephen Smalley selinux_policy_commit(fsi->state, newpolicy); 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; 7641da177e4SLinus Torvalds out: 7658365a719SAl Viro kfree(page); 7661da177e4SLinus Torvalds return length; 7671da177e4SLinus Torvalds } 7689c2e08c5SArjan van de Ven static const struct file_operations sel_checkreqprot_ops = { 7691da177e4SLinus Torvalds .read = sel_read_checkreqprot, 7701da177e4SLinus Torvalds .write = sel_write_checkreqprot, 77157a62c23SArnd Bergmann .llseek = generic_file_llseek, 7721da177e4SLinus Torvalds }; 7731da177e4SLinus Torvalds 774f9df6458SAndrew Perepechko static ssize_t sel_write_validatetrans(struct file *file, 775f9df6458SAndrew Perepechko const char __user *buf, 776f9df6458SAndrew Perepechko size_t count, loff_t *ppos) 777f9df6458SAndrew Perepechko { 7780619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 7790619f0f5SStephen Smalley struct selinux_state *state = fsi->state; 780f9df6458SAndrew Perepechko char *oldcon = NULL, *newcon = NULL, *taskcon = NULL; 781f9df6458SAndrew Perepechko char *req = NULL; 782f9df6458SAndrew Perepechko u32 osid, nsid, tsid; 783f9df6458SAndrew Perepechko u16 tclass; 784f9df6458SAndrew Perepechko int rc; 785f9df6458SAndrew Perepechko 7866b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 7876b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 788be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__VALIDATE_TRANS, NULL); 789f9df6458SAndrew Perepechko if (rc) 790f9df6458SAndrew Perepechko goto out; 791f9df6458SAndrew Perepechko 792f9df6458SAndrew Perepechko rc = -ENOMEM; 793f9df6458SAndrew Perepechko if (count >= PAGE_SIZE) 794f9df6458SAndrew Perepechko goto out; 795f9df6458SAndrew Perepechko 796f9df6458SAndrew Perepechko /* No partial writes. */ 797f9df6458SAndrew Perepechko rc = -EINVAL; 798f9df6458SAndrew Perepechko if (*ppos != 0) 799f9df6458SAndrew Perepechko goto out; 800f9df6458SAndrew Perepechko 8010b884d25SAl Viro req = memdup_user_nul(buf, count); 8020b884d25SAl Viro if (IS_ERR(req)) { 8030b884d25SAl Viro rc = PTR_ERR(req); 8040b884d25SAl Viro req = NULL; 805f9df6458SAndrew Perepechko goto out; 8060b884d25SAl Viro } 807f9df6458SAndrew Perepechko 808f9df6458SAndrew Perepechko rc = -ENOMEM; 809f9df6458SAndrew Perepechko oldcon = kzalloc(count + 1, GFP_KERNEL); 810f9df6458SAndrew Perepechko if (!oldcon) 811f9df6458SAndrew Perepechko goto out; 812f9df6458SAndrew Perepechko 813f9df6458SAndrew Perepechko newcon = kzalloc(count + 1, GFP_KERNEL); 814f9df6458SAndrew Perepechko if (!newcon) 815f9df6458SAndrew Perepechko goto out; 816f9df6458SAndrew Perepechko 817f9df6458SAndrew Perepechko taskcon = kzalloc(count + 1, GFP_KERNEL); 818f9df6458SAndrew Perepechko if (!taskcon) 819f9df6458SAndrew Perepechko goto out; 820f9df6458SAndrew Perepechko 821f9df6458SAndrew Perepechko rc = -EINVAL; 822f9df6458SAndrew Perepechko if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4) 823f9df6458SAndrew Perepechko goto out; 824f9df6458SAndrew Perepechko 8250619f0f5SStephen Smalley rc = security_context_str_to_sid(state, oldcon, &osid, GFP_KERNEL); 826f9df6458SAndrew Perepechko if (rc) 827f9df6458SAndrew Perepechko goto out; 828f9df6458SAndrew Perepechko 8290619f0f5SStephen Smalley rc = security_context_str_to_sid(state, newcon, &nsid, GFP_KERNEL); 830f9df6458SAndrew Perepechko if (rc) 831f9df6458SAndrew Perepechko goto out; 832f9df6458SAndrew Perepechko 8330619f0f5SStephen Smalley rc = security_context_str_to_sid(state, taskcon, &tsid, GFP_KERNEL); 834f9df6458SAndrew Perepechko if (rc) 835f9df6458SAndrew Perepechko goto out; 836f9df6458SAndrew Perepechko 8370619f0f5SStephen Smalley rc = security_validate_transition_user(state, osid, nsid, tsid, tclass); 838f9df6458SAndrew Perepechko if (!rc) 839f9df6458SAndrew Perepechko rc = count; 840f9df6458SAndrew Perepechko out: 841f9df6458SAndrew Perepechko kfree(req); 842f9df6458SAndrew Perepechko kfree(oldcon); 843f9df6458SAndrew Perepechko kfree(newcon); 844f9df6458SAndrew Perepechko kfree(taskcon); 845f9df6458SAndrew Perepechko return rc; 846f9df6458SAndrew Perepechko } 847f9df6458SAndrew Perepechko 848f9df6458SAndrew Perepechko static const struct file_operations sel_transition_ops = { 849f9df6458SAndrew Perepechko .write = sel_write_validatetrans, 850f9df6458SAndrew Perepechko .llseek = generic_file_llseek, 851f9df6458SAndrew Perepechko }; 852f9df6458SAndrew Perepechko 8531da177e4SLinus Torvalds /* 8541da177e4SLinus Torvalds * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c 8551da177e4SLinus Torvalds */ 8561da177e4SLinus Torvalds static ssize_t sel_write_access(struct file *file, char *buf, size_t size); 8571da177e4SLinus Torvalds static ssize_t sel_write_create(struct file *file, char *buf, size_t size); 8581da177e4SLinus Torvalds static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size); 8591da177e4SLinus Torvalds static ssize_t sel_write_user(struct file *file, char *buf, size_t size); 8601da177e4SLinus Torvalds static ssize_t sel_write_member(struct file *file, char *buf, size_t size); 8611da177e4SLinus Torvalds 862631d2b49SEric Biggers static ssize_t (*const write_op[])(struct file *, char *, size_t) = { 8631da177e4SLinus Torvalds [SEL_ACCESS] = sel_write_access, 8641da177e4SLinus Torvalds [SEL_CREATE] = sel_write_create, 8651da177e4SLinus Torvalds [SEL_RELABEL] = sel_write_relabel, 8661da177e4SLinus Torvalds [SEL_USER] = sel_write_user, 8671da177e4SLinus Torvalds [SEL_MEMBER] = sel_write_member, 868ce9982d0SStephen Smalley [SEL_CONTEXT] = sel_write_context, 8691da177e4SLinus Torvalds }; 8701da177e4SLinus Torvalds 8711da177e4SLinus Torvalds static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos) 8721da177e4SLinus Torvalds { 873496ad9aaSAl Viro ino_t ino = file_inode(file)->i_ino; 8741da177e4SLinus Torvalds char *data; 8751da177e4SLinus Torvalds ssize_t rv; 8761da177e4SLinus Torvalds 8776e20a64aSNicolas Kaiser if (ino >= ARRAY_SIZE(write_op) || !write_op[ino]) 8781da177e4SLinus Torvalds return -EINVAL; 8791da177e4SLinus Torvalds 8801da177e4SLinus Torvalds data = simple_transaction_get(file, buf, size); 8811da177e4SLinus Torvalds if (IS_ERR(data)) 8821da177e4SLinus Torvalds return PTR_ERR(data); 8831da177e4SLinus Torvalds 8841da177e4SLinus Torvalds rv = write_op[ino](file, data, size); 8851da177e4SLinus Torvalds if (rv > 0) { 8861da177e4SLinus Torvalds simple_transaction_set(file, rv); 8871da177e4SLinus Torvalds rv = size; 8881da177e4SLinus Torvalds } 8891da177e4SLinus Torvalds return rv; 8901da177e4SLinus Torvalds } 8911da177e4SLinus Torvalds 8929c2e08c5SArjan van de Ven static const struct file_operations transaction_ops = { 8931da177e4SLinus Torvalds .write = selinux_transaction_write, 8941da177e4SLinus Torvalds .read = simple_transaction_read, 8951da177e4SLinus Torvalds .release = simple_transaction_release, 89657a62c23SArnd Bergmann .llseek = generic_file_llseek, 8971da177e4SLinus Torvalds }; 8981da177e4SLinus Torvalds 8991da177e4SLinus Torvalds /* 9001da177e4SLinus Torvalds * payload - write methods 9011da177e4SLinus Torvalds * If the method has a response, the response should be put in buf, 9021da177e4SLinus Torvalds * and the length returned. Otherwise return 0 or and -error. 9031da177e4SLinus Torvalds */ 9041da177e4SLinus Torvalds 9051da177e4SLinus Torvalds static ssize_t sel_write_access(struct file *file, char *buf, size_t size) 9061da177e4SLinus Torvalds { 9070619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 9080619f0f5SStephen Smalley struct selinux_state *state = fsi->state; 909b77a493bSEric Paris char *scon = NULL, *tcon = NULL; 9101da177e4SLinus Torvalds u32 ssid, tsid; 9111da177e4SLinus Torvalds u16 tclass; 9121da177e4SLinus Torvalds struct av_decision avd; 9131da177e4SLinus Torvalds ssize_t length; 9141da177e4SLinus Torvalds 9156b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 9166b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 917be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_AV, NULL); 9181da177e4SLinus Torvalds if (length) 919b77a493bSEric Paris goto out; 9201da177e4SLinus Torvalds 9211da177e4SLinus Torvalds length = -ENOMEM; 92289d155efSJames Morris scon = kzalloc(size + 1, GFP_KERNEL); 9231da177e4SLinus Torvalds if (!scon) 924b77a493bSEric Paris goto out; 9251da177e4SLinus Torvalds 926b77a493bSEric Paris length = -ENOMEM; 92789d155efSJames Morris tcon = kzalloc(size + 1, GFP_KERNEL); 9281da177e4SLinus Torvalds if (!tcon) 9291da177e4SLinus Torvalds goto out; 9301da177e4SLinus Torvalds 9311da177e4SLinus Torvalds length = -EINVAL; 93219439d05SStephen Smalley if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) 933b77a493bSEric Paris goto out; 9341da177e4SLinus Torvalds 9350619f0f5SStephen Smalley length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL); 936b77a493bSEric Paris if (length) 937b77a493bSEric Paris goto out; 938b77a493bSEric Paris 9390619f0f5SStephen Smalley length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL); 940b77a493bSEric Paris if (length) 941b77a493bSEric Paris goto out; 9421da177e4SLinus Torvalds 9430619f0f5SStephen Smalley security_compute_av_user(state, ssid, tsid, tclass, &avd); 9441da177e4SLinus Torvalds 9451da177e4SLinus Torvalds length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, 9468a6f83afSKaiGai Kohei "%x %x %x %x %u %x", 947f1c6381aSEric Paris avd.allowed, 0xffffffff, 9481da177e4SLinus Torvalds avd.auditallow, avd.auditdeny, 9498a6f83afSKaiGai Kohei avd.seqno, avd.flags); 9501da177e4SLinus Torvalds out: 951b77a493bSEric Paris kfree(tcon); 9521da177e4SLinus Torvalds kfree(scon); 9531da177e4SLinus Torvalds return length; 9541da177e4SLinus Torvalds } 9551da177e4SLinus Torvalds 9561da177e4SLinus Torvalds static ssize_t sel_write_create(struct file *file, char *buf, size_t size) 9571da177e4SLinus Torvalds { 9580619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 9590619f0f5SStephen Smalley struct selinux_state *state = fsi->state; 960b77a493bSEric Paris char *scon = NULL, *tcon = NULL; 961f50a3ec9SKohei Kaigai char *namebuf = NULL, *objname = NULL; 9621da177e4SLinus Torvalds u32 ssid, tsid, newsid; 9631da177e4SLinus Torvalds u16 tclass; 9641da177e4SLinus Torvalds ssize_t length; 965b77a493bSEric Paris char *newcon = NULL; 9661da177e4SLinus Torvalds u32 len; 967f50a3ec9SKohei Kaigai int nargs; 9681da177e4SLinus Torvalds 9696b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 9706b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 971be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_CREATE, 972be0554c9SStephen Smalley NULL); 9731da177e4SLinus Torvalds if (length) 974b77a493bSEric Paris goto out; 9751da177e4SLinus Torvalds 9761da177e4SLinus Torvalds length = -ENOMEM; 97789d155efSJames Morris scon = kzalloc(size + 1, GFP_KERNEL); 9781da177e4SLinus Torvalds if (!scon) 979b77a493bSEric Paris goto out; 9801da177e4SLinus Torvalds 981b77a493bSEric Paris length = -ENOMEM; 98289d155efSJames Morris tcon = kzalloc(size + 1, GFP_KERNEL); 9831da177e4SLinus Torvalds if (!tcon) 9841da177e4SLinus Torvalds goto out; 9851da177e4SLinus Torvalds 986f50a3ec9SKohei Kaigai length = -ENOMEM; 987f50a3ec9SKohei Kaigai namebuf = kzalloc(size + 1, GFP_KERNEL); 988f50a3ec9SKohei Kaigai if (!namebuf) 989b77a493bSEric Paris goto out; 9901da177e4SLinus Torvalds 991f50a3ec9SKohei Kaigai length = -EINVAL; 992f50a3ec9SKohei Kaigai nargs = sscanf(buf, "%s %s %hu %s", scon, tcon, &tclass, namebuf); 993f50a3ec9SKohei Kaigai if (nargs < 3 || nargs > 4) 994f50a3ec9SKohei Kaigai goto out; 9950f7e4c33SKohei Kaigai if (nargs == 4) { 9960f7e4c33SKohei Kaigai /* 9970f7e4c33SKohei Kaigai * If and when the name of new object to be queried contains 9980f7e4c33SKohei Kaigai * either whitespace or multibyte characters, they shall be 9990f7e4c33SKohei Kaigai * encoded based on the percentage-encoding rule. 10000f7e4c33SKohei Kaigai * If not encoded, the sscanf logic picks up only left-half 10010f7e4c33SKohei Kaigai * of the supplied name; splitted by a whitespace unexpectedly. 10020f7e4c33SKohei Kaigai */ 10030f7e4c33SKohei Kaigai char *r, *w; 10040f7e4c33SKohei Kaigai int c1, c2; 10050f7e4c33SKohei Kaigai 10060f7e4c33SKohei Kaigai r = w = namebuf; 10070f7e4c33SKohei Kaigai do { 10080f7e4c33SKohei Kaigai c1 = *r++; 10090f7e4c33SKohei Kaigai if (c1 == '+') 10100f7e4c33SKohei Kaigai c1 = ' '; 10110f7e4c33SKohei Kaigai else if (c1 == '%') { 1012af7ff2c2SAndy Shevchenko c1 = hex_to_bin(*r++); 1013af7ff2c2SAndy Shevchenko if (c1 < 0) 10140f7e4c33SKohei Kaigai goto out; 1015af7ff2c2SAndy Shevchenko c2 = hex_to_bin(*r++); 1016af7ff2c2SAndy Shevchenko if (c2 < 0) 10170f7e4c33SKohei Kaigai goto out; 10180f7e4c33SKohei Kaigai c1 = (c1 << 4) | c2; 10190f7e4c33SKohei Kaigai } 10200f7e4c33SKohei Kaigai *w++ = c1; 10210f7e4c33SKohei Kaigai } while (c1 != '\0'); 10220f7e4c33SKohei Kaigai 1023f50a3ec9SKohei Kaigai objname = namebuf; 10240f7e4c33SKohei Kaigai } 1025f50a3ec9SKohei Kaigai 10260619f0f5SStephen Smalley length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL); 1027b77a493bSEric Paris if (length) 1028b77a493bSEric Paris goto out; 1029b77a493bSEric Paris 10300619f0f5SStephen Smalley length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL); 1031b77a493bSEric Paris if (length) 1032b77a493bSEric Paris goto out; 10331da177e4SLinus Torvalds 10340619f0f5SStephen Smalley length = security_transition_sid_user(state, ssid, tsid, tclass, 10350619f0f5SStephen Smalley objname, &newsid); 1036b77a493bSEric Paris if (length) 1037b77a493bSEric Paris goto out; 10381da177e4SLinus Torvalds 10390619f0f5SStephen Smalley length = security_sid_to_context(state, newsid, &newcon, &len); 1040b77a493bSEric Paris if (length) 1041b77a493bSEric Paris goto out; 10421da177e4SLinus Torvalds 1043b77a493bSEric Paris length = -ERANGE; 10441da177e4SLinus Torvalds if (len > SIMPLE_TRANSACTION_LIMIT) { 1045f8b69a5fSpeter enderborg pr_err("SELinux: %s: context size (%u) exceeds " 1046744ba35eSEric Paris "payload max\n", __func__, len); 1047b77a493bSEric Paris goto out; 10481da177e4SLinus Torvalds } 10491da177e4SLinus Torvalds 10501da177e4SLinus Torvalds memcpy(buf, newcon, len); 10511da177e4SLinus Torvalds length = len; 10521da177e4SLinus Torvalds out: 1053b77a493bSEric Paris kfree(newcon); 1054f50a3ec9SKohei Kaigai kfree(namebuf); 1055b77a493bSEric Paris kfree(tcon); 10561da177e4SLinus Torvalds kfree(scon); 10571da177e4SLinus Torvalds return length; 10581da177e4SLinus Torvalds } 10591da177e4SLinus Torvalds 10601da177e4SLinus Torvalds static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size) 10611da177e4SLinus Torvalds { 10620619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 10630619f0f5SStephen Smalley struct selinux_state *state = fsi->state; 1064b77a493bSEric Paris char *scon = NULL, *tcon = NULL; 10651da177e4SLinus Torvalds u32 ssid, tsid, newsid; 10661da177e4SLinus Torvalds u16 tclass; 10671da177e4SLinus Torvalds ssize_t length; 1068b77a493bSEric Paris char *newcon = NULL; 10691da177e4SLinus Torvalds u32 len; 10701da177e4SLinus Torvalds 10716b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 10726b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 1073be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_RELABEL, 1074be0554c9SStephen Smalley NULL); 10751da177e4SLinus Torvalds if (length) 1076b77a493bSEric Paris goto out; 10771da177e4SLinus Torvalds 10781da177e4SLinus Torvalds length = -ENOMEM; 107989d155efSJames Morris scon = kzalloc(size + 1, GFP_KERNEL); 10801da177e4SLinus Torvalds if (!scon) 1081b77a493bSEric Paris goto out; 10821da177e4SLinus Torvalds 1083b77a493bSEric Paris length = -ENOMEM; 108489d155efSJames Morris tcon = kzalloc(size + 1, GFP_KERNEL); 10851da177e4SLinus Torvalds if (!tcon) 10861da177e4SLinus Torvalds goto out; 10871da177e4SLinus Torvalds 10881da177e4SLinus Torvalds length = -EINVAL; 10891da177e4SLinus Torvalds if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) 1090b77a493bSEric Paris goto out; 10911da177e4SLinus Torvalds 10920619f0f5SStephen Smalley length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL); 1093b77a493bSEric Paris if (length) 1094b77a493bSEric Paris goto out; 1095b77a493bSEric Paris 10960619f0f5SStephen Smalley length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL); 1097b77a493bSEric Paris if (length) 1098b77a493bSEric Paris goto out; 10991da177e4SLinus Torvalds 11000619f0f5SStephen Smalley length = security_change_sid(state, ssid, tsid, tclass, &newsid); 1101b77a493bSEric Paris if (length) 1102b77a493bSEric Paris goto out; 11031da177e4SLinus Torvalds 11040619f0f5SStephen Smalley length = security_sid_to_context(state, newsid, &newcon, &len); 1105b77a493bSEric Paris if (length) 1106b77a493bSEric Paris goto out; 11071da177e4SLinus Torvalds 11081da177e4SLinus Torvalds length = -ERANGE; 1109b77a493bSEric Paris if (len > SIMPLE_TRANSACTION_LIMIT) 1110b77a493bSEric Paris goto out; 11111da177e4SLinus Torvalds 11121da177e4SLinus Torvalds memcpy(buf, newcon, len); 11131da177e4SLinus Torvalds length = len; 11141da177e4SLinus Torvalds out: 1115b77a493bSEric Paris kfree(newcon); 1116b77a493bSEric Paris kfree(tcon); 11171da177e4SLinus Torvalds kfree(scon); 11181da177e4SLinus Torvalds return length; 11191da177e4SLinus Torvalds } 11201da177e4SLinus Torvalds 11211da177e4SLinus Torvalds static ssize_t sel_write_user(struct file *file, char *buf, size_t size) 11221da177e4SLinus Torvalds { 11230619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 11240619f0f5SStephen Smalley struct selinux_state *state = fsi->state; 1125b77a493bSEric Paris char *con = NULL, *user = NULL, *ptr; 1126b77a493bSEric Paris u32 sid, *sids = NULL; 11271da177e4SLinus Torvalds ssize_t length; 11281da177e4SLinus Torvalds char *newcon; 11291da177e4SLinus Torvalds int i, rc; 11301da177e4SLinus Torvalds u32 len, nsids; 11311da177e4SLinus Torvalds 11326b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 11336b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 1134be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_USER, 1135be0554c9SStephen Smalley NULL); 11361da177e4SLinus Torvalds if (length) 11376eab04a8SJustin P. Mattock goto out; 11381da177e4SLinus Torvalds 11391da177e4SLinus Torvalds length = -ENOMEM; 114089d155efSJames Morris con = kzalloc(size + 1, GFP_KERNEL); 11411da177e4SLinus Torvalds if (!con) 11426eab04a8SJustin P. Mattock goto out; 11431da177e4SLinus Torvalds 1144b77a493bSEric Paris length = -ENOMEM; 114589d155efSJames Morris user = kzalloc(size + 1, GFP_KERNEL); 11461da177e4SLinus Torvalds if (!user) 11471da177e4SLinus Torvalds goto out; 11481da177e4SLinus Torvalds 11491da177e4SLinus Torvalds length = -EINVAL; 11501da177e4SLinus Torvalds if (sscanf(buf, "%s %s", con, user) != 2) 1151b77a493bSEric Paris goto out; 11521da177e4SLinus Torvalds 11530619f0f5SStephen Smalley length = security_context_str_to_sid(state, con, &sid, GFP_KERNEL); 1154b77a493bSEric Paris if (length) 1155b77a493bSEric Paris goto out; 11561da177e4SLinus Torvalds 11570619f0f5SStephen Smalley length = security_get_user_sids(state, sid, user, &sids, &nsids); 1158b77a493bSEric Paris if (length) 1159b77a493bSEric Paris goto out; 11601da177e4SLinus Torvalds 11611da177e4SLinus Torvalds length = sprintf(buf, "%u", nsids) + 1; 11621da177e4SLinus Torvalds ptr = buf + length; 11631da177e4SLinus Torvalds for (i = 0; i < nsids; i++) { 11640619f0f5SStephen Smalley rc = security_sid_to_context(state, sids[i], &newcon, &len); 11651da177e4SLinus Torvalds if (rc) { 11661da177e4SLinus Torvalds length = rc; 1167b77a493bSEric Paris goto out; 11681da177e4SLinus Torvalds } 11691da177e4SLinus Torvalds if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) { 11701da177e4SLinus Torvalds kfree(newcon); 11711da177e4SLinus Torvalds length = -ERANGE; 1172b77a493bSEric Paris goto out; 11731da177e4SLinus Torvalds } 11741da177e4SLinus Torvalds memcpy(ptr, newcon, len); 11751da177e4SLinus Torvalds kfree(newcon); 11761da177e4SLinus Torvalds ptr += len; 11771da177e4SLinus Torvalds length += len; 11781da177e4SLinus Torvalds } 11791da177e4SLinus Torvalds out: 1180b77a493bSEric Paris kfree(sids); 1181b77a493bSEric Paris kfree(user); 11821da177e4SLinus Torvalds kfree(con); 11831da177e4SLinus Torvalds return length; 11841da177e4SLinus Torvalds } 11851da177e4SLinus Torvalds 11861da177e4SLinus Torvalds static ssize_t sel_write_member(struct file *file, char *buf, size_t size) 11871da177e4SLinus Torvalds { 11880619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 11890619f0f5SStephen Smalley struct selinux_state *state = fsi->state; 1190b77a493bSEric Paris char *scon = NULL, *tcon = NULL; 11911da177e4SLinus Torvalds u32 ssid, tsid, newsid; 11921da177e4SLinus Torvalds u16 tclass; 11931da177e4SLinus Torvalds ssize_t length; 1194b77a493bSEric Paris char *newcon = NULL; 11951da177e4SLinus Torvalds u32 len; 11961da177e4SLinus Torvalds 11976b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 11986b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 1199be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_MEMBER, 1200be0554c9SStephen Smalley NULL); 12011da177e4SLinus Torvalds if (length) 1202b77a493bSEric Paris goto out; 12031da177e4SLinus Torvalds 12041da177e4SLinus Torvalds length = -ENOMEM; 120589d155efSJames Morris scon = kzalloc(size + 1, GFP_KERNEL); 12061da177e4SLinus Torvalds if (!scon) 12076eab04a8SJustin P. Mattock goto out; 12081da177e4SLinus Torvalds 1209b77a493bSEric Paris length = -ENOMEM; 121089d155efSJames Morris tcon = kzalloc(size + 1, GFP_KERNEL); 12111da177e4SLinus Torvalds if (!tcon) 12121da177e4SLinus Torvalds goto out; 12131da177e4SLinus Torvalds 12141da177e4SLinus Torvalds length = -EINVAL; 12151da177e4SLinus Torvalds if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) 1216b77a493bSEric Paris goto out; 12171da177e4SLinus Torvalds 12180619f0f5SStephen Smalley length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL); 1219b77a493bSEric Paris if (length) 1220b77a493bSEric Paris goto out; 1221b77a493bSEric Paris 12220619f0f5SStephen Smalley length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL); 1223b77a493bSEric Paris if (length) 1224b77a493bSEric Paris goto out; 12251da177e4SLinus Torvalds 12260619f0f5SStephen Smalley length = security_member_sid(state, ssid, tsid, tclass, &newsid); 1227b77a493bSEric Paris if (length) 1228b77a493bSEric Paris goto out; 12291da177e4SLinus Torvalds 12300619f0f5SStephen Smalley length = security_sid_to_context(state, newsid, &newcon, &len); 1231b77a493bSEric Paris if (length) 1232b77a493bSEric Paris goto out; 12331da177e4SLinus Torvalds 1234b77a493bSEric Paris length = -ERANGE; 12351da177e4SLinus Torvalds if (len > SIMPLE_TRANSACTION_LIMIT) { 1236f8b69a5fSpeter enderborg pr_err("SELinux: %s: context size (%u) exceeds " 1237744ba35eSEric Paris "payload max\n", __func__, len); 1238b77a493bSEric Paris goto out; 12391da177e4SLinus Torvalds } 12401da177e4SLinus Torvalds 12411da177e4SLinus Torvalds memcpy(buf, newcon, len); 12421da177e4SLinus Torvalds length = len; 12431da177e4SLinus Torvalds out: 1244b77a493bSEric Paris kfree(newcon); 1245b77a493bSEric Paris kfree(tcon); 12461da177e4SLinus Torvalds kfree(scon); 12471da177e4SLinus Torvalds return length; 12481da177e4SLinus Torvalds } 12491da177e4SLinus Torvalds 12501da177e4SLinus Torvalds static struct inode *sel_make_inode(struct super_block *sb, int mode) 12511da177e4SLinus Torvalds { 12521da177e4SLinus Torvalds struct inode *ret = new_inode(sb); 12531da177e4SLinus Torvalds 12541da177e4SLinus Torvalds if (ret) { 12551da177e4SLinus Torvalds ret->i_mode = mode; 1256078cd827SDeepa Dinamani ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret); 12571da177e4SLinus Torvalds } 12581da177e4SLinus Torvalds return ret; 12591da177e4SLinus Torvalds } 12601da177e4SLinus Torvalds 12611da177e4SLinus Torvalds static ssize_t sel_read_bool(struct file *filep, char __user *buf, 12621da177e4SLinus Torvalds size_t count, loff_t *ppos) 12631da177e4SLinus Torvalds { 12640619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info; 12651da177e4SLinus Torvalds char *page = NULL; 12661da177e4SLinus Torvalds ssize_t length; 12671da177e4SLinus Torvalds ssize_t ret; 12681da177e4SLinus Torvalds int cur_enforcing; 1269496ad9aaSAl Viro unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK; 1270d313f948SStephen Smalley const char *name = filep->f_path.dentry->d_name.name; 12711da177e4SLinus Torvalds 12729ff9abc4SStephen Smalley mutex_lock(&fsi->state->policy_mutex); 12731da177e4SLinus Torvalds 1274d313f948SStephen Smalley ret = -EINVAL; 12750619f0f5SStephen Smalley if (index >= fsi->bool_num || strcmp(name, 12760619f0f5SStephen Smalley fsi->bool_pending_names[index])) 12770da74120SJann Horn goto out_unlock; 12781da177e4SLinus Torvalds 12791da177e4SLinus Torvalds ret = -ENOMEM; 1280b77a493bSEric Paris page = (char *)get_zeroed_page(GFP_KERNEL); 1281b77a493bSEric Paris if (!page) 12820da74120SJann Horn goto out_unlock; 12831da177e4SLinus Torvalds 12840619f0f5SStephen Smalley cur_enforcing = security_get_bool_value(fsi->state, index); 12851da177e4SLinus Torvalds if (cur_enforcing < 0) { 12861da177e4SLinus Torvalds ret = cur_enforcing; 12870da74120SJann Horn goto out_unlock; 12881da177e4SLinus Torvalds } 12891da177e4SLinus Torvalds length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing, 12900619f0f5SStephen Smalley fsi->bool_pending_values[index]); 12919ff9abc4SStephen Smalley mutex_unlock(&fsi->state->policy_mutex); 12920da74120SJann Horn ret = simple_read_from_buffer(buf, count, ppos, page, length); 12930da74120SJann Horn out_free: 12941da177e4SLinus Torvalds free_page((unsigned long)page); 12951da177e4SLinus Torvalds return ret; 12960da74120SJann Horn 12970da74120SJann Horn out_unlock: 12989ff9abc4SStephen Smalley mutex_unlock(&fsi->state->policy_mutex); 12990da74120SJann Horn goto out_free; 13001da177e4SLinus Torvalds } 13011da177e4SLinus Torvalds 13021da177e4SLinus Torvalds static ssize_t sel_write_bool(struct file *filep, const char __user *buf, 13031da177e4SLinus Torvalds size_t count, loff_t *ppos) 13041da177e4SLinus Torvalds { 13050619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info; 13061da177e4SLinus Torvalds char *page = NULL; 1307d313f948SStephen Smalley ssize_t length; 13081da177e4SLinus Torvalds int new_value; 1309496ad9aaSAl Viro unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK; 1310d313f948SStephen Smalley const char *name = filep->f_path.dentry->d_name.name; 13111da177e4SLinus Torvalds 13120da74120SJann Horn if (count >= PAGE_SIZE) 13130da74120SJann Horn return -ENOMEM; 13140da74120SJann Horn 13150da74120SJann Horn /* No partial writes. */ 13160da74120SJann Horn if (*ppos != 0) 13170da74120SJann Horn return -EINVAL; 13180da74120SJann Horn 13190da74120SJann Horn page = memdup_user_nul(buf, count); 13200da74120SJann Horn if (IS_ERR(page)) 13210da74120SJann Horn return PTR_ERR(page); 13220da74120SJann Horn 13239ff9abc4SStephen Smalley mutex_lock(&fsi->state->policy_mutex); 13241da177e4SLinus Torvalds 13256b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 13266b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 1327be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETBOOL, 1328be0554c9SStephen Smalley NULL); 13291da177e4SLinus Torvalds if (length) 13301da177e4SLinus Torvalds goto out; 13311da177e4SLinus Torvalds 1332d313f948SStephen Smalley length = -EINVAL; 13330619f0f5SStephen Smalley if (index >= fsi->bool_num || strcmp(name, 13340619f0f5SStephen Smalley fsi->bool_pending_names[index])) 1335d313f948SStephen Smalley goto out; 1336d313f948SStephen Smalley 13371da177e4SLinus Torvalds length = -EINVAL; 13381da177e4SLinus Torvalds if (sscanf(page, "%d", &new_value) != 1) 13391da177e4SLinus Torvalds goto out; 13401da177e4SLinus Torvalds 13411da177e4SLinus Torvalds if (new_value) 13421da177e4SLinus Torvalds new_value = 1; 13431da177e4SLinus Torvalds 13440619f0f5SStephen Smalley fsi->bool_pending_values[index] = new_value; 13451da177e4SLinus Torvalds length = count; 13461da177e4SLinus Torvalds 13471da177e4SLinus Torvalds out: 13489ff9abc4SStephen Smalley mutex_unlock(&fsi->state->policy_mutex); 13498365a719SAl Viro kfree(page); 13501da177e4SLinus Torvalds return length; 13511da177e4SLinus Torvalds } 13521da177e4SLinus Torvalds 13539c2e08c5SArjan van de Ven static const struct file_operations sel_bool_ops = { 13541da177e4SLinus Torvalds .read = sel_read_bool, 13551da177e4SLinus Torvalds .write = sel_write_bool, 135657a62c23SArnd Bergmann .llseek = generic_file_llseek, 13571da177e4SLinus Torvalds }; 13581da177e4SLinus Torvalds 13591da177e4SLinus Torvalds static ssize_t sel_commit_bools_write(struct file *filep, 13601da177e4SLinus Torvalds const char __user *buf, 13611da177e4SLinus Torvalds size_t count, loff_t *ppos) 13621da177e4SLinus Torvalds { 13630619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info; 13641da177e4SLinus Torvalds char *page = NULL; 1365d313f948SStephen Smalley ssize_t length; 13661da177e4SLinus Torvalds int new_value; 13671da177e4SLinus Torvalds 13680da74120SJann Horn if (count >= PAGE_SIZE) 13690da74120SJann Horn return -ENOMEM; 13700da74120SJann Horn 13710da74120SJann Horn /* No partial writes. */ 13720da74120SJann Horn if (*ppos != 0) 13730da74120SJann Horn return -EINVAL; 13740da74120SJann Horn 13750da74120SJann Horn page = memdup_user_nul(buf, count); 13760da74120SJann Horn if (IS_ERR(page)) 13770da74120SJann Horn return PTR_ERR(page); 13780da74120SJann Horn 13799ff9abc4SStephen Smalley mutex_lock(&fsi->state->policy_mutex); 13801da177e4SLinus Torvalds 13816b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 13826b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 1383be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETBOOL, 1384be0554c9SStephen Smalley NULL); 13851da177e4SLinus Torvalds if (length) 13861da177e4SLinus Torvalds goto out; 13871da177e4SLinus Torvalds 13881da177e4SLinus Torvalds length = -EINVAL; 13891da177e4SLinus Torvalds if (sscanf(page, "%d", &new_value) != 1) 13901da177e4SLinus Torvalds goto out; 13911da177e4SLinus Torvalds 1392b77a493bSEric Paris length = 0; 13930619f0f5SStephen Smalley if (new_value && fsi->bool_pending_values) 13940619f0f5SStephen Smalley length = security_set_bools(fsi->state, fsi->bool_num, 13950619f0f5SStephen Smalley fsi->bool_pending_values); 13961da177e4SLinus Torvalds 1397b77a493bSEric Paris if (!length) 13981da177e4SLinus Torvalds length = count; 13991da177e4SLinus Torvalds 14001da177e4SLinus Torvalds out: 14019ff9abc4SStephen Smalley mutex_unlock(&fsi->state->policy_mutex); 14028365a719SAl Viro kfree(page); 14031da177e4SLinus Torvalds return length; 14041da177e4SLinus Torvalds } 14051da177e4SLinus Torvalds 14069c2e08c5SArjan van de Ven static const struct file_operations sel_commit_bools_ops = { 14071da177e4SLinus Torvalds .write = sel_commit_bools_write, 140857a62c23SArnd Bergmann .llseek = generic_file_llseek, 14091da177e4SLinus Torvalds }; 14101da177e4SLinus Torvalds 14110c92d7c7SChristopher J. PeBenito static void sel_remove_entries(struct dentry *de) 14121da177e4SLinus Torvalds { 1413ad52184bSAl Viro d_genocide(de); 1414ad52184bSAl Viro shrink_dcache_parent(de); 14151da177e4SLinus Torvalds } 14161da177e4SLinus Torvalds 141766ec384aSDaniel Burgener static int sel_make_bools(struct selinux_policy *newpolicy, struct dentry *bool_dir, 141866ec384aSDaniel Burgener unsigned int *bool_num, char ***bool_pending_names, 141966ec384aSDaniel Burgener unsigned int **bool_pending_values) 14201da177e4SLinus Torvalds { 142160abd318SOndrej Mosnacek int ret; 14221da177e4SLinus Torvalds ssize_t len; 14231da177e4SLinus Torvalds struct dentry *dentry = NULL; 14241da177e4SLinus Torvalds struct inode *inode = NULL; 14251da177e4SLinus Torvalds struct inode_security_struct *isec; 14261da177e4SLinus Torvalds char **names = NULL, *page; 142760abd318SOndrej Mosnacek u32 i, num; 14281da177e4SLinus Torvalds int *values = NULL; 14291da177e4SLinus Torvalds u32 sid; 14301da177e4SLinus Torvalds 1431b77a493bSEric Paris ret = -ENOMEM; 14321872981bSEric Paris page = (char *)get_zeroed_page(GFP_KERNEL); 14331872981bSEric Paris if (!page) 1434b77a493bSEric Paris goto out; 14351da177e4SLinus Torvalds 143602a52c5cSStephen Smalley ret = security_get_bools(newpolicy, &num, &names, &values); 1437b77a493bSEric Paris if (ret) 14381da177e4SLinus Torvalds goto out; 14391da177e4SLinus Torvalds 14401da177e4SLinus Torvalds for (i = 0; i < num; i++) { 1441b77a493bSEric Paris ret = -ENOMEM; 144266ec384aSDaniel Burgener dentry = d_alloc_name(bool_dir, names[i]); 1443b77a493bSEric Paris if (!dentry) 1444b77a493bSEric Paris goto out; 14451da177e4SLinus Torvalds 1446b77a493bSEric Paris ret = -ENOMEM; 144766ec384aSDaniel Burgener inode = sel_make_inode(bool_dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR); 14487e4237faSnixiaoming if (!inode) { 14497e4237faSnixiaoming dput(dentry); 1450b77a493bSEric Paris goto out; 14517e4237faSnixiaoming } 1452b77a493bSEric Paris 14531da177e4SLinus Torvalds ret = -ENAMETOOLONG; 1454cc1dad71SAl Viro len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]); 14557e4237faSnixiaoming if (len >= PAGE_SIZE) { 14567e4237faSnixiaoming dput(dentry); 14577e4237faSnixiaoming iput(inode); 1458b77a493bSEric Paris goto out; 14597e4237faSnixiaoming } 1460b77a493bSEric Paris 146180788c22SCasey Schaufler isec = selinux_inode(inode); 146202a52c5cSStephen Smalley ret = selinux_policy_genfs_sid(newpolicy, "selinuxfs", page, 1463aa8e712cSStephen Smalley SECCLASS_FILE, &sid); 14644262fb51SGary Tierney if (ret) { 1465900fde06SGary Tierney pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n", 1466900fde06SGary Tierney page); 1467900fde06SGary Tierney sid = SECINITSID_SECURITY; 14684262fb51SGary Tierney } 14694262fb51SGary Tierney 14701da177e4SLinus Torvalds isec->sid = sid; 147142059112SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 14721da177e4SLinus Torvalds inode->i_fop = &sel_bool_ops; 1473bce34bc0SJames Carter inode->i_ino = i|SEL_BOOL_INO_OFFSET; 14741da177e4SLinus Torvalds d_add(dentry, inode); 14751da177e4SLinus Torvalds } 147666ec384aSDaniel Burgener *bool_num = num; 147766ec384aSDaniel Burgener *bool_pending_names = names; 147866ec384aSDaniel Burgener *bool_pending_values = values; 1479b77a493bSEric Paris 1480b77a493bSEric Paris free_page((unsigned long)page); 1481b77a493bSEric Paris return 0; 14821da177e4SLinus Torvalds out: 14831da177e4SLinus Torvalds free_page((unsigned long)page); 1484b77a493bSEric Paris 14851da177e4SLinus Torvalds if (names) { 14869a5f04bfSJesper Juhl for (i = 0; i < num; i++) 14871da177e4SLinus Torvalds kfree(names[i]); 14881da177e4SLinus Torvalds kfree(names); 14891da177e4SLinus Torvalds } 149020c19e41SDavi Arnaut kfree(values); 149166ec384aSDaniel Burgener sel_remove_entries(bool_dir); 1492b77a493bSEric Paris 1493b77a493bSEric Paris return ret; 14941da177e4SLinus Torvalds } 14951da177e4SLinus Torvalds 14961da177e4SLinus Torvalds static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf, 14971da177e4SLinus Torvalds size_t count, loff_t *ppos) 14981da177e4SLinus Torvalds { 14996b6bc620SStephen Smalley struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info; 15006b6bc620SStephen Smalley struct selinux_state *state = fsi->state; 15011da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 15021da177e4SLinus Torvalds ssize_t length; 15031da177e4SLinus Torvalds 15046b6bc620SStephen Smalley length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 15056b6bc620SStephen Smalley avc_get_cache_threshold(state->avc)); 15061da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 15071da177e4SLinus Torvalds } 15081da177e4SLinus Torvalds 15091da177e4SLinus Torvalds static ssize_t sel_write_avc_cache_threshold(struct file *file, 15101da177e4SLinus Torvalds const char __user *buf, 15111da177e4SLinus Torvalds size_t count, loff_t *ppos) 15121da177e4SLinus Torvalds 15131da177e4SLinus Torvalds { 15146b6bc620SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 15156b6bc620SStephen Smalley struct selinux_state *state = fsi->state; 15168365a719SAl Viro char *page; 15171da177e4SLinus Torvalds ssize_t ret; 1518309c5fadSHeinrich Schuchardt unsigned int new_value; 15191da177e4SLinus Torvalds 15206b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 15216b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 1522be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETSECPARAM, 1523be0554c9SStephen Smalley NULL); 15241da177e4SLinus Torvalds if (ret) 15258365a719SAl Viro return ret; 1526b77a493bSEric Paris 1527b77a493bSEric Paris if (count >= PAGE_SIZE) 15288365a719SAl Viro return -ENOMEM; 1529b77a493bSEric Paris 1530b77a493bSEric Paris /* No partial writes. */ 1531b77a493bSEric Paris if (*ppos != 0) 15328365a719SAl Viro return -EINVAL; 1533b77a493bSEric Paris 15348365a719SAl Viro page = memdup_user_nul(buf, count); 15358365a719SAl Viro if (IS_ERR(page)) 15368365a719SAl Viro return PTR_ERR(page); 1537b77a493bSEric Paris 1538b77a493bSEric Paris ret = -EINVAL; 1539b77a493bSEric Paris if (sscanf(page, "%u", &new_value) != 1) 1540b77a493bSEric Paris goto out; 1541b77a493bSEric Paris 15426b6bc620SStephen Smalley avc_set_cache_threshold(state->avc, new_value); 1543b77a493bSEric Paris 15441da177e4SLinus Torvalds ret = count; 15451da177e4SLinus Torvalds out: 15468365a719SAl Viro kfree(page); 15471da177e4SLinus Torvalds return ret; 15481da177e4SLinus Torvalds } 15491da177e4SLinus Torvalds 15501da177e4SLinus Torvalds static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf, 15511da177e4SLinus Torvalds size_t count, loff_t *ppos) 15521da177e4SLinus Torvalds { 15536b6bc620SStephen Smalley struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info; 15546b6bc620SStephen Smalley struct selinux_state *state = fsi->state; 15551da177e4SLinus Torvalds char *page; 1556b77a493bSEric Paris ssize_t length; 15571da177e4SLinus Torvalds 15581da177e4SLinus Torvalds page = (char *)__get_free_page(GFP_KERNEL); 1559b77a493bSEric Paris if (!page) 1560b77a493bSEric Paris return -ENOMEM; 1561b77a493bSEric Paris 15626b6bc620SStephen Smalley length = avc_get_hash_stats(state->avc, page); 1563b77a493bSEric Paris if (length >= 0) 1564b77a493bSEric Paris length = simple_read_from_buffer(buf, count, ppos, page, length); 15651da177e4SLinus Torvalds free_page((unsigned long)page); 1566b77a493bSEric Paris 1567b77a493bSEric Paris return length; 15681da177e4SLinus Torvalds } 15691da177e4SLinus Torvalds 157066f8e2f0SJeff Vander Stoep static ssize_t sel_read_sidtab_hash_stats(struct file *filp, char __user *buf, 157166f8e2f0SJeff Vander Stoep size_t count, loff_t *ppos) 157266f8e2f0SJeff Vander Stoep { 157366f8e2f0SJeff Vander Stoep struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info; 157466f8e2f0SJeff Vander Stoep struct selinux_state *state = fsi->state; 157566f8e2f0SJeff Vander Stoep char *page; 157666f8e2f0SJeff Vander Stoep ssize_t length; 157766f8e2f0SJeff Vander Stoep 157866f8e2f0SJeff Vander Stoep page = (char *)__get_free_page(GFP_KERNEL); 157966f8e2f0SJeff Vander Stoep if (!page) 158066f8e2f0SJeff Vander Stoep return -ENOMEM; 158166f8e2f0SJeff Vander Stoep 158266f8e2f0SJeff Vander Stoep length = security_sidtab_hash_stats(state, page); 158366f8e2f0SJeff Vander Stoep if (length >= 0) 158466f8e2f0SJeff Vander Stoep length = simple_read_from_buffer(buf, count, ppos, page, 158566f8e2f0SJeff Vander Stoep length); 158666f8e2f0SJeff Vander Stoep free_page((unsigned long)page); 158766f8e2f0SJeff Vander Stoep 158866f8e2f0SJeff Vander Stoep return length; 158966f8e2f0SJeff Vander Stoep } 159066f8e2f0SJeff Vander Stoep 159166f8e2f0SJeff Vander Stoep static const struct file_operations sel_sidtab_hash_stats_ops = { 159266f8e2f0SJeff Vander Stoep .read = sel_read_sidtab_hash_stats, 159366f8e2f0SJeff Vander Stoep .llseek = generic_file_llseek, 159466f8e2f0SJeff Vander Stoep }; 159566f8e2f0SJeff Vander Stoep 15969c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_threshold_ops = { 15971da177e4SLinus Torvalds .read = sel_read_avc_cache_threshold, 15981da177e4SLinus Torvalds .write = sel_write_avc_cache_threshold, 159957a62c23SArnd Bergmann .llseek = generic_file_llseek, 16001da177e4SLinus Torvalds }; 16011da177e4SLinus Torvalds 16029c2e08c5SArjan van de Ven static const struct file_operations sel_avc_hash_stats_ops = { 16031da177e4SLinus Torvalds .read = sel_read_avc_hash_stats, 160457a62c23SArnd Bergmann .llseek = generic_file_llseek, 16051da177e4SLinus Torvalds }; 16061da177e4SLinus Torvalds 16071da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS 16081da177e4SLinus Torvalds static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx) 16091da177e4SLinus Torvalds { 16101da177e4SLinus Torvalds int cpu; 16111da177e4SLinus Torvalds 16124f4b6c1aSRusty Russell for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) { 16131da177e4SLinus Torvalds if (!cpu_possible(cpu)) 16141da177e4SLinus Torvalds continue; 16151da177e4SLinus Torvalds *idx = cpu + 1; 16161da177e4SLinus Torvalds return &per_cpu(avc_cache_stats, cpu); 16171da177e4SLinus Torvalds } 16188d269a8eSVasily Averin (*idx)++; 16191da177e4SLinus Torvalds return NULL; 16201da177e4SLinus Torvalds } 16211da177e4SLinus Torvalds 16221da177e4SLinus Torvalds static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos) 16231da177e4SLinus Torvalds { 16241da177e4SLinus Torvalds loff_t n = *pos - 1; 16251da177e4SLinus Torvalds 16261da177e4SLinus Torvalds if (*pos == 0) 16271da177e4SLinus Torvalds return SEQ_START_TOKEN; 16281da177e4SLinus Torvalds 16291da177e4SLinus Torvalds return sel_avc_get_stat_idx(&n); 16301da177e4SLinus Torvalds } 16311da177e4SLinus Torvalds 16321da177e4SLinus Torvalds static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos) 16331da177e4SLinus Torvalds { 16341da177e4SLinus Torvalds return sel_avc_get_stat_idx(pos); 16351da177e4SLinus Torvalds } 16361da177e4SLinus Torvalds 16371da177e4SLinus Torvalds static int sel_avc_stats_seq_show(struct seq_file *seq, void *v) 16381da177e4SLinus Torvalds { 16391da177e4SLinus Torvalds struct avc_cache_stats *st = v; 16401da177e4SLinus Torvalds 1641710a0647SMarkus Elfring if (v == SEQ_START_TOKEN) { 1642710a0647SMarkus Elfring seq_puts(seq, 1643710a0647SMarkus Elfring "lookups hits misses allocations reclaims frees\n"); 1644710a0647SMarkus Elfring } else { 1645257313b2SLinus Torvalds unsigned int lookups = st->lookups; 1646257313b2SLinus Torvalds unsigned int misses = st->misses; 1647257313b2SLinus Torvalds unsigned int hits = lookups - misses; 1648257313b2SLinus Torvalds seq_printf(seq, "%u %u %u %u %u %u\n", lookups, 1649257313b2SLinus Torvalds hits, misses, st->allocations, 16501da177e4SLinus Torvalds st->reclaims, st->frees); 1651257313b2SLinus Torvalds } 16521da177e4SLinus Torvalds return 0; 16531da177e4SLinus Torvalds } 16541da177e4SLinus Torvalds 16551da177e4SLinus Torvalds static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v) 16561da177e4SLinus Torvalds { } 16571da177e4SLinus Torvalds 16581996a109SJan Engelhardt static const struct seq_operations sel_avc_cache_stats_seq_ops = { 16591da177e4SLinus Torvalds .start = sel_avc_stats_seq_start, 16601da177e4SLinus Torvalds .next = sel_avc_stats_seq_next, 16611da177e4SLinus Torvalds .show = sel_avc_stats_seq_show, 16621da177e4SLinus Torvalds .stop = sel_avc_stats_seq_stop, 16631da177e4SLinus Torvalds }; 16641da177e4SLinus Torvalds 16651da177e4SLinus Torvalds static int sel_open_avc_cache_stats(struct inode *inode, struct file *file) 16661da177e4SLinus Torvalds { 16671da177e4SLinus Torvalds return seq_open(file, &sel_avc_cache_stats_seq_ops); 16681da177e4SLinus Torvalds } 16691da177e4SLinus Torvalds 16709c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_stats_ops = { 16711da177e4SLinus Torvalds .open = sel_open_avc_cache_stats, 16721da177e4SLinus Torvalds .read = seq_read, 16731da177e4SLinus Torvalds .llseek = seq_lseek, 16741da177e4SLinus Torvalds .release = seq_release, 16751da177e4SLinus Torvalds }; 16761da177e4SLinus Torvalds #endif 16771da177e4SLinus Torvalds 16781da177e4SLinus Torvalds static int sel_make_avc_files(struct dentry *dir) 16791da177e4SLinus Torvalds { 16800619f0f5SStephen Smalley struct super_block *sb = dir->d_sb; 16810619f0f5SStephen Smalley struct selinux_fs_info *fsi = sb->s_fs_info; 1682b77a493bSEric Paris int i; 1683cda37124SEric Biggers static const struct tree_descr files[] = { 16841da177e4SLinus Torvalds { "cache_threshold", 16851da177e4SLinus Torvalds &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR }, 16861da177e4SLinus Torvalds { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO }, 16871da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS 16881da177e4SLinus Torvalds { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO }, 16891da177e4SLinus Torvalds #endif 16901da177e4SLinus Torvalds }; 16911da177e4SLinus Torvalds 16926e20a64aSNicolas Kaiser for (i = 0; i < ARRAY_SIZE(files); i++) { 16931da177e4SLinus Torvalds struct inode *inode; 16941da177e4SLinus Torvalds struct dentry *dentry; 16951da177e4SLinus Torvalds 16961da177e4SLinus Torvalds dentry = d_alloc_name(dir, files[i].name); 1697b77a493bSEric Paris if (!dentry) 1698b77a493bSEric Paris return -ENOMEM; 16991da177e4SLinus Torvalds 17001da177e4SLinus Torvalds inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode); 17017e4237faSnixiaoming if (!inode) { 17027e4237faSnixiaoming dput(dentry); 1703b77a493bSEric Paris return -ENOMEM; 17047e4237faSnixiaoming } 1705b77a493bSEric Paris 17061da177e4SLinus Torvalds inode->i_fop = files[i].ops; 17070619f0f5SStephen Smalley inode->i_ino = ++fsi->last_ino; 17081da177e4SLinus Torvalds d_add(dentry, inode); 17091da177e4SLinus Torvalds } 1710b77a493bSEric Paris 1711b77a493bSEric Paris return 0; 17121da177e4SLinus Torvalds } 17131da177e4SLinus Torvalds 171466f8e2f0SJeff Vander Stoep static int sel_make_ss_files(struct dentry *dir) 171566f8e2f0SJeff Vander Stoep { 171666f8e2f0SJeff Vander Stoep struct super_block *sb = dir->d_sb; 171766f8e2f0SJeff Vander Stoep struct selinux_fs_info *fsi = sb->s_fs_info; 171866f8e2f0SJeff Vander Stoep int i; 171966f8e2f0SJeff Vander Stoep static struct tree_descr files[] = { 172066f8e2f0SJeff Vander Stoep { "sidtab_hash_stats", &sel_sidtab_hash_stats_ops, S_IRUGO }, 172166f8e2f0SJeff Vander Stoep }; 172266f8e2f0SJeff Vander Stoep 172366f8e2f0SJeff Vander Stoep for (i = 0; i < ARRAY_SIZE(files); i++) { 172466f8e2f0SJeff Vander Stoep struct inode *inode; 172566f8e2f0SJeff Vander Stoep struct dentry *dentry; 172666f8e2f0SJeff Vander Stoep 172766f8e2f0SJeff Vander Stoep dentry = d_alloc_name(dir, files[i].name); 172866f8e2f0SJeff Vander Stoep if (!dentry) 172966f8e2f0SJeff Vander Stoep return -ENOMEM; 173066f8e2f0SJeff Vander Stoep 173166f8e2f0SJeff Vander Stoep inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode); 173266f8e2f0SJeff Vander Stoep if (!inode) { 173366f8e2f0SJeff Vander Stoep dput(dentry); 173466f8e2f0SJeff Vander Stoep return -ENOMEM; 173566f8e2f0SJeff Vander Stoep } 173666f8e2f0SJeff Vander Stoep 173766f8e2f0SJeff Vander Stoep inode->i_fop = files[i].ops; 173866f8e2f0SJeff Vander Stoep inode->i_ino = ++fsi->last_ino; 173966f8e2f0SJeff Vander Stoep d_add(dentry, inode); 174066f8e2f0SJeff Vander Stoep } 174166f8e2f0SJeff Vander Stoep 174266f8e2f0SJeff Vander Stoep return 0; 174366f8e2f0SJeff Vander Stoep } 174466f8e2f0SJeff Vander Stoep 1745f0ee2e46SJames Carter static ssize_t sel_read_initcon(struct file *file, char __user *buf, 1746f0ee2e46SJames Carter size_t count, loff_t *ppos) 1747f0ee2e46SJames Carter { 17480619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 1749f0ee2e46SJames Carter char *con; 1750f0ee2e46SJames Carter u32 sid, len; 1751f0ee2e46SJames Carter ssize_t ret; 1752f0ee2e46SJames Carter 1753496ad9aaSAl Viro sid = file_inode(file)->i_ino&SEL_INO_MASK; 17540619f0f5SStephen Smalley ret = security_sid_to_context(fsi->state, sid, &con, &len); 1755b77a493bSEric Paris if (ret) 1756f0ee2e46SJames Carter return ret; 1757f0ee2e46SJames Carter 1758f0ee2e46SJames Carter ret = simple_read_from_buffer(buf, count, ppos, con, len); 1759f0ee2e46SJames Carter kfree(con); 1760f0ee2e46SJames Carter return ret; 1761f0ee2e46SJames Carter } 1762f0ee2e46SJames Carter 1763f0ee2e46SJames Carter static const struct file_operations sel_initcon_ops = { 1764f0ee2e46SJames Carter .read = sel_read_initcon, 176557a62c23SArnd Bergmann .llseek = generic_file_llseek, 1766f0ee2e46SJames Carter }; 1767f0ee2e46SJames Carter 1768f0ee2e46SJames Carter static int sel_make_initcon_files(struct dentry *dir) 1769f0ee2e46SJames Carter { 1770b77a493bSEric Paris int i; 1771f0ee2e46SJames Carter 1772f0ee2e46SJames Carter for (i = 1; i <= SECINITSID_NUM; i++) { 1773f0ee2e46SJames Carter struct inode *inode; 1774f0ee2e46SJames Carter struct dentry *dentry; 1775e3e0b582SStephen Smalley const char *s = security_get_initial_sid_context(i); 1776e3e0b582SStephen Smalley 1777e3e0b582SStephen Smalley if (!s) 1778e3e0b582SStephen Smalley continue; 1779e3e0b582SStephen Smalley dentry = d_alloc_name(dir, s); 1780b77a493bSEric Paris if (!dentry) 1781b77a493bSEric Paris return -ENOMEM; 1782f0ee2e46SJames Carter 1783f0ee2e46SJames Carter inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); 17847e4237faSnixiaoming if (!inode) { 17857e4237faSnixiaoming dput(dentry); 1786b77a493bSEric Paris return -ENOMEM; 17877e4237faSnixiaoming } 1788b77a493bSEric Paris 1789f0ee2e46SJames Carter inode->i_fop = &sel_initcon_ops; 1790f0ee2e46SJames Carter inode->i_ino = i|SEL_INITCON_INO_OFFSET; 1791f0ee2e46SJames Carter d_add(dentry, inode); 1792f0ee2e46SJames Carter } 1793b77a493bSEric Paris 1794b77a493bSEric Paris return 0; 1795f0ee2e46SJames Carter } 1796f0ee2e46SJames Carter 1797e47c8fc5SChristopher J. PeBenito static inline unsigned long sel_class_to_ino(u16 class) 1798e47c8fc5SChristopher J. PeBenito { 1799e47c8fc5SChristopher J. PeBenito return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET; 1800e47c8fc5SChristopher J. PeBenito } 1801e47c8fc5SChristopher J. PeBenito 1802e47c8fc5SChristopher J. PeBenito static inline u16 sel_ino_to_class(unsigned long ino) 1803e47c8fc5SChristopher J. PeBenito { 180492ae9e82SEric Paris return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1); 1805e47c8fc5SChristopher J. PeBenito } 1806e47c8fc5SChristopher J. PeBenito 1807e47c8fc5SChristopher J. PeBenito static inline unsigned long sel_perm_to_ino(u16 class, u32 perm) 1808e47c8fc5SChristopher J. PeBenito { 1809e47c8fc5SChristopher J. PeBenito return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET; 1810e47c8fc5SChristopher J. PeBenito } 1811e47c8fc5SChristopher J. PeBenito 1812e47c8fc5SChristopher J. PeBenito static inline u32 sel_ino_to_perm(unsigned long ino) 1813e47c8fc5SChristopher J. PeBenito { 1814e47c8fc5SChristopher J. PeBenito return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1); 1815e47c8fc5SChristopher J. PeBenito } 1816e47c8fc5SChristopher J. PeBenito 1817e47c8fc5SChristopher J. PeBenito static ssize_t sel_read_class(struct file *file, char __user *buf, 1818e47c8fc5SChristopher J. PeBenito size_t count, loff_t *ppos) 1819e47c8fc5SChristopher J. PeBenito { 1820496ad9aaSAl Viro unsigned long ino = file_inode(file)->i_ino; 1821cc1dad71SAl Viro char res[TMPBUFLEN]; 18227e78c875Sliuyang34 ssize_t len = scnprintf(res, sizeof(res), "%d", sel_ino_to_class(ino)); 1823cc1dad71SAl Viro return simple_read_from_buffer(buf, count, ppos, res, len); 1824e47c8fc5SChristopher J. PeBenito } 1825e47c8fc5SChristopher J. PeBenito 1826e47c8fc5SChristopher J. PeBenito static const struct file_operations sel_class_ops = { 1827e47c8fc5SChristopher J. PeBenito .read = sel_read_class, 182857a62c23SArnd Bergmann .llseek = generic_file_llseek, 1829e47c8fc5SChristopher J. PeBenito }; 1830e47c8fc5SChristopher J. PeBenito 1831e47c8fc5SChristopher J. PeBenito static ssize_t sel_read_perm(struct file *file, char __user *buf, 1832e47c8fc5SChristopher J. PeBenito size_t count, loff_t *ppos) 1833e47c8fc5SChristopher J. PeBenito { 1834496ad9aaSAl Viro unsigned long ino = file_inode(file)->i_ino; 1835cc1dad71SAl Viro char res[TMPBUFLEN]; 18367e78c875Sliuyang34 ssize_t len = scnprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino)); 1837cc1dad71SAl Viro return simple_read_from_buffer(buf, count, ppos, res, len); 1838e47c8fc5SChristopher J. PeBenito } 1839e47c8fc5SChristopher J. PeBenito 1840e47c8fc5SChristopher J. PeBenito static const struct file_operations sel_perm_ops = { 1841e47c8fc5SChristopher J. PeBenito .read = sel_read_perm, 184257a62c23SArnd Bergmann .llseek = generic_file_llseek, 1843e47c8fc5SChristopher J. PeBenito }; 1844e47c8fc5SChristopher J. PeBenito 18453bb56b25SPaul Moore static ssize_t sel_read_policycap(struct file *file, char __user *buf, 18463bb56b25SPaul Moore size_t count, loff_t *ppos) 18473bb56b25SPaul Moore { 18480619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 18493bb56b25SPaul Moore int value; 18503bb56b25SPaul Moore char tmpbuf[TMPBUFLEN]; 18513bb56b25SPaul Moore ssize_t length; 1852496ad9aaSAl Viro unsigned long i_ino = file_inode(file)->i_ino; 18533bb56b25SPaul Moore 18540619f0f5SStephen Smalley value = security_policycap_supported(fsi->state, i_ino & SEL_INO_MASK); 18553bb56b25SPaul Moore length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value); 18563bb56b25SPaul Moore 18573bb56b25SPaul Moore return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 18583bb56b25SPaul Moore } 18593bb56b25SPaul Moore 18603bb56b25SPaul Moore static const struct file_operations sel_policycap_ops = { 18613bb56b25SPaul Moore .read = sel_read_policycap, 186257a62c23SArnd Bergmann .llseek = generic_file_llseek, 18633bb56b25SPaul Moore }; 18643bb56b25SPaul Moore 186502a52c5cSStephen Smalley static int sel_make_perm_files(struct selinux_policy *newpolicy, 186602a52c5cSStephen Smalley char *objclass, int classvalue, 1867e47c8fc5SChristopher J. PeBenito struct dentry *dir) 1868e47c8fc5SChristopher J. PeBenito { 1869b77a493bSEric Paris int i, rc, nperms; 1870e47c8fc5SChristopher J. PeBenito char **perms; 1871e47c8fc5SChristopher J. PeBenito 187202a52c5cSStephen Smalley rc = security_get_permissions(newpolicy, objclass, &perms, &nperms); 1873e47c8fc5SChristopher J. PeBenito if (rc) 1874b77a493bSEric Paris return rc; 1875e47c8fc5SChristopher J. PeBenito 1876e47c8fc5SChristopher J. PeBenito for (i = 0; i < nperms; i++) { 1877e47c8fc5SChristopher J. PeBenito struct inode *inode; 1878e47c8fc5SChristopher J. PeBenito struct dentry *dentry; 1879e47c8fc5SChristopher J. PeBenito 1880b77a493bSEric Paris rc = -ENOMEM; 1881e47c8fc5SChristopher J. PeBenito dentry = d_alloc_name(dir, perms[i]); 1882b77a493bSEric Paris if (!dentry) 1883b77a493bSEric Paris goto out; 1884e47c8fc5SChristopher J. PeBenito 1885e47c8fc5SChristopher J. PeBenito rc = -ENOMEM; 1886b77a493bSEric Paris inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); 18877e4237faSnixiaoming if (!inode) { 18887e4237faSnixiaoming dput(dentry); 1889b77a493bSEric Paris goto out; 18907e4237faSnixiaoming } 1891b77a493bSEric Paris 1892e47c8fc5SChristopher J. PeBenito inode->i_fop = &sel_perm_ops; 1893e47c8fc5SChristopher J. PeBenito /* i+1 since perm values are 1-indexed */ 1894e47c8fc5SChristopher J. PeBenito inode->i_ino = sel_perm_to_ino(classvalue, i + 1); 1895e47c8fc5SChristopher J. PeBenito d_add(dentry, inode); 1896e47c8fc5SChristopher J. PeBenito } 1897b77a493bSEric Paris rc = 0; 1898b77a493bSEric Paris out: 1899e47c8fc5SChristopher J. PeBenito for (i = 0; i < nperms; i++) 1900e47c8fc5SChristopher J. PeBenito kfree(perms[i]); 1901e47c8fc5SChristopher J. PeBenito kfree(perms); 1902e47c8fc5SChristopher J. PeBenito return rc; 1903e47c8fc5SChristopher J. PeBenito } 1904e47c8fc5SChristopher J. PeBenito 190502a52c5cSStephen Smalley static int sel_make_class_dir_entries(struct selinux_policy *newpolicy, 190602a52c5cSStephen Smalley char *classname, int index, 1907e47c8fc5SChristopher J. PeBenito struct dentry *dir) 1908e47c8fc5SChristopher J. PeBenito { 19090619f0f5SStephen Smalley struct super_block *sb = dir->d_sb; 19100619f0f5SStephen Smalley struct selinux_fs_info *fsi = sb->s_fs_info; 1911e47c8fc5SChristopher J. PeBenito struct dentry *dentry = NULL; 1912e47c8fc5SChristopher J. PeBenito struct inode *inode = NULL; 1913e47c8fc5SChristopher J. PeBenito int rc; 1914e47c8fc5SChristopher J. PeBenito 1915e47c8fc5SChristopher J. PeBenito dentry = d_alloc_name(dir, "index"); 1916b77a493bSEric Paris if (!dentry) 1917b77a493bSEric Paris return -ENOMEM; 1918e47c8fc5SChristopher J. PeBenito 1919e47c8fc5SChristopher J. PeBenito inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); 19207e4237faSnixiaoming if (!inode) { 19217e4237faSnixiaoming dput(dentry); 1922b77a493bSEric Paris return -ENOMEM; 19237e4237faSnixiaoming } 1924e47c8fc5SChristopher J. PeBenito 1925e47c8fc5SChristopher J. PeBenito inode->i_fop = &sel_class_ops; 1926e47c8fc5SChristopher J. PeBenito inode->i_ino = sel_class_to_ino(index); 1927e47c8fc5SChristopher J. PeBenito d_add(dentry, inode); 1928e47c8fc5SChristopher J. PeBenito 19290619f0f5SStephen Smalley dentry = sel_make_dir(dir, "perms", &fsi->last_class_ino); 1930a1c2aa1eSAl Viro if (IS_ERR(dentry)) 1931a1c2aa1eSAl Viro return PTR_ERR(dentry); 1932e47c8fc5SChristopher J. PeBenito 193302a52c5cSStephen Smalley rc = sel_make_perm_files(newpolicy, classname, index, dentry); 1934e47c8fc5SChristopher J. PeBenito 1935e47c8fc5SChristopher J. PeBenito return rc; 1936e47c8fc5SChristopher J. PeBenito } 1937e47c8fc5SChristopher J. PeBenito 193866ec384aSDaniel Burgener static int sel_make_classes(struct selinux_policy *newpolicy, 193966ec384aSDaniel Burgener struct dentry *class_dir, 194066ec384aSDaniel Burgener unsigned long *last_class_ino) 1941e47c8fc5SChristopher J. PeBenito { 19420619f0f5SStephen Smalley 1943b77a493bSEric Paris int rc, nclasses, i; 1944e47c8fc5SChristopher J. PeBenito char **classes; 1945e47c8fc5SChristopher J. PeBenito 194602a52c5cSStephen Smalley rc = security_get_classes(newpolicy, &classes, &nclasses); 1947b77a493bSEric Paris if (rc) 1948b77a493bSEric Paris return rc; 1949e47c8fc5SChristopher J. PeBenito 1950e47c8fc5SChristopher J. PeBenito /* +2 since classes are 1-indexed */ 195166ec384aSDaniel Burgener *last_class_ino = sel_class_to_ino(nclasses + 2); 1952e47c8fc5SChristopher J. PeBenito 1953e47c8fc5SChristopher J. PeBenito for (i = 0; i < nclasses; i++) { 1954e47c8fc5SChristopher J. PeBenito struct dentry *class_name_dir; 1955e47c8fc5SChristopher J. PeBenito 195666ec384aSDaniel Burgener class_name_dir = sel_make_dir(class_dir, classes[i], 195766ec384aSDaniel Burgener last_class_ino); 1958a1c2aa1eSAl Viro if (IS_ERR(class_name_dir)) { 1959a1c2aa1eSAl Viro rc = PTR_ERR(class_name_dir); 1960b77a493bSEric Paris goto out; 1961a1c2aa1eSAl Viro } 1962e47c8fc5SChristopher J. PeBenito 1963e47c8fc5SChristopher J. PeBenito /* i+1 since class values are 1-indexed */ 196402a52c5cSStephen Smalley rc = sel_make_class_dir_entries(newpolicy, classes[i], i + 1, 1965e47c8fc5SChristopher J. PeBenito class_name_dir); 1966e47c8fc5SChristopher J. PeBenito if (rc) 1967b77a493bSEric Paris goto out; 1968e47c8fc5SChristopher J. PeBenito } 1969b77a493bSEric Paris rc = 0; 1970b77a493bSEric Paris out: 1971e47c8fc5SChristopher J. PeBenito for (i = 0; i < nclasses; i++) 1972e47c8fc5SChristopher J. PeBenito kfree(classes[i]); 1973e47c8fc5SChristopher J. PeBenito kfree(classes); 1974e47c8fc5SChristopher J. PeBenito return rc; 1975e47c8fc5SChristopher J. PeBenito } 1976e47c8fc5SChristopher J. PeBenito 19770619f0f5SStephen Smalley static int sel_make_policycap(struct selinux_fs_info *fsi) 19783bb56b25SPaul Moore { 19793bb56b25SPaul Moore unsigned int iter; 19803bb56b25SPaul Moore struct dentry *dentry = NULL; 19813bb56b25SPaul Moore struct inode *inode = NULL; 19823bb56b25SPaul Moore 19833bb56b25SPaul Moore for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) { 19844dc2fce3SStephen Smalley if (iter < ARRAY_SIZE(selinux_policycap_names)) 19850619f0f5SStephen Smalley dentry = d_alloc_name(fsi->policycap_dir, 19864dc2fce3SStephen Smalley selinux_policycap_names[iter]); 19873bb56b25SPaul Moore else 19880619f0f5SStephen Smalley dentry = d_alloc_name(fsi->policycap_dir, "unknown"); 19893bb56b25SPaul Moore 19903bb56b25SPaul Moore if (dentry == NULL) 19913bb56b25SPaul Moore return -ENOMEM; 19923bb56b25SPaul Moore 19930619f0f5SStephen Smalley inode = sel_make_inode(fsi->sb, S_IFREG | 0444); 19947e4237faSnixiaoming if (inode == NULL) { 19957e4237faSnixiaoming dput(dentry); 19963bb56b25SPaul Moore return -ENOMEM; 19977e4237faSnixiaoming } 19983bb56b25SPaul Moore 19993bb56b25SPaul Moore inode->i_fop = &sel_policycap_ops; 20003bb56b25SPaul Moore inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET; 20013bb56b25SPaul Moore d_add(dentry, inode); 20023bb56b25SPaul Moore } 20033bb56b25SPaul Moore 20043bb56b25SPaul Moore return 0; 20053bb56b25SPaul Moore } 20063bb56b25SPaul Moore 2007a1c2aa1eSAl Viro static struct dentry *sel_make_dir(struct dentry *dir, const char *name, 20080dd4ae51SChristopher J. PeBenito unsigned long *ino) 20091da177e4SLinus Torvalds { 2010a1c2aa1eSAl Viro struct dentry *dentry = d_alloc_name(dir, name); 20111da177e4SLinus Torvalds struct inode *inode; 20121da177e4SLinus Torvalds 2013a1c2aa1eSAl Viro if (!dentry) 2014a1c2aa1eSAl Viro return ERR_PTR(-ENOMEM); 2015a1c2aa1eSAl Viro 2016a1c2aa1eSAl Viro inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO); 2017a1c2aa1eSAl Viro if (!inode) { 2018a1c2aa1eSAl Viro dput(dentry); 2019a1c2aa1eSAl Viro return ERR_PTR(-ENOMEM); 2020a1c2aa1eSAl Viro } 2021b77a493bSEric Paris 20221da177e4SLinus Torvalds inode->i_op = &simple_dir_inode_operations; 20231da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations; 20240dd4ae51SChristopher J. PeBenito inode->i_ino = ++(*ino); 202540e906f8SJames Morris /* directory inodes start off with i_nlink == 2 (for "." entry) */ 2026d8c76e6fSDave Hansen inc_nlink(inode); 20271da177e4SLinus Torvalds d_add(dentry, inode); 2028edb20fb5SJames Morris /* bump link count on parent directory, too */ 2029ce0b16ddSDavid Howells inc_nlink(d_inode(dir)); 2030b77a493bSEric Paris 2031a1c2aa1eSAl Viro return dentry; 20321da177e4SLinus Torvalds } 20331da177e4SLinus Torvalds 20340eea6091SDaniel Burgener static struct dentry *sel_make_disconnected_dir(struct super_block *sb, 20350eea6091SDaniel Burgener unsigned long *ino) 20360eea6091SDaniel Burgener { 20370eea6091SDaniel Burgener struct inode *inode = sel_make_inode(sb, S_IFDIR | S_IRUGO | S_IXUGO); 20380eea6091SDaniel Burgener 20390eea6091SDaniel Burgener if (!inode) 20400eea6091SDaniel Burgener return ERR_PTR(-ENOMEM); 20410eea6091SDaniel Burgener 20420eea6091SDaniel Burgener inode->i_op = &simple_dir_inode_operations; 20430eea6091SDaniel Burgener inode->i_fop = &simple_dir_operations; 20440eea6091SDaniel Burgener inode->i_ino = ++(*ino); 20450eea6091SDaniel Burgener /* directory inodes start off with i_nlink == 2 (for "." entry) */ 20460eea6091SDaniel Burgener inc_nlink(inode); 20470eea6091SDaniel Burgener return d_obtain_alias(inode); 20480eea6091SDaniel Burgener } 20490eea6091SDaniel Burgener 20500619f0f5SStephen Smalley #define NULL_FILE_NAME "null" 20510619f0f5SStephen Smalley 2052920f50b2SDavid Howells static int sel_fill_super(struct super_block *sb, struct fs_context *fc) 20531da177e4SLinus Torvalds { 20540619f0f5SStephen Smalley struct selinux_fs_info *fsi; 20551da177e4SLinus Torvalds int ret; 20561da177e4SLinus Torvalds struct dentry *dentry; 2057a1c2aa1eSAl Viro struct inode *inode; 20581da177e4SLinus Torvalds struct inode_security_struct *isec; 20591da177e4SLinus Torvalds 2060cda37124SEric Biggers static const struct tree_descr selinux_files[] = { 20611da177e4SLinus Torvalds [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR}, 20621da177e4SLinus Torvalds [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR}, 2063ce9982d0SStephen Smalley [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO}, 20641da177e4SLinus Torvalds [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO}, 20651da177e4SLinus Torvalds [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO}, 20661da177e4SLinus Torvalds [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO}, 20671da177e4SLinus Torvalds [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO}, 20681da177e4SLinus Torvalds [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO}, 20691da177e4SLinus Torvalds [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR}, 20701da177e4SLinus Torvalds [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO}, 20711da177e4SLinus Torvalds [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR}, 20721da177e4SLinus Torvalds [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO}, 20731da177e4SLinus Torvalds [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR}, 20743f12070eSEric Paris [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO}, 20753f12070eSEric Paris [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO}, 207611904167SKaiGai Kohei [SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO}, 207772e8c859SEric Paris [SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO}, 2078f9df6458SAndrew Perepechko [SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops, 2079f9df6458SAndrew Perepechko S_IWUGO}, 20801da177e4SLinus Torvalds /* last one */ {""} 20811da177e4SLinus Torvalds }; 20820619f0f5SStephen Smalley 20830619f0f5SStephen Smalley ret = selinux_fs_info_create(sb); 20840619f0f5SStephen Smalley if (ret) 20850619f0f5SStephen Smalley goto err; 20860619f0f5SStephen Smalley 20871da177e4SLinus Torvalds ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files); 20881da177e4SLinus Torvalds if (ret) 2089161ce45aSJames Morris goto err; 20901da177e4SLinus Torvalds 20910619f0f5SStephen Smalley fsi = sb->s_fs_info; 20920619f0f5SStephen Smalley fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino); 20930619f0f5SStephen Smalley if (IS_ERR(fsi->bool_dir)) { 20940619f0f5SStephen Smalley ret = PTR_ERR(fsi->bool_dir); 20950619f0f5SStephen Smalley fsi->bool_dir = NULL; 2096161ce45aSJames Morris goto err; 2097a1c2aa1eSAl Viro } 20981da177e4SLinus Torvalds 2099b77a493bSEric Paris ret = -ENOMEM; 21001da177e4SLinus Torvalds dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME); 2101b77a493bSEric Paris if (!dentry) 2102161ce45aSJames Morris goto err; 21031da177e4SLinus Torvalds 2104161ce45aSJames Morris ret = -ENOMEM; 2105b77a493bSEric Paris inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO); 21067e4237faSnixiaoming if (!inode) { 21077e4237faSnixiaoming dput(dentry); 2108161ce45aSJames Morris goto err; 21097e4237faSnixiaoming } 2110b77a493bSEric Paris 21110619f0f5SStephen Smalley inode->i_ino = ++fsi->last_ino; 211280788c22SCasey Schaufler isec = selinux_inode(inode); 21131da177e4SLinus Torvalds isec->sid = SECINITSID_DEVNULL; 21141da177e4SLinus Torvalds isec->sclass = SECCLASS_CHR_FILE; 211542059112SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 21161da177e4SLinus Torvalds 21171da177e4SLinus Torvalds init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3)); 21181da177e4SLinus Torvalds d_add(dentry, inode); 21191da177e4SLinus Torvalds 21200619f0f5SStephen Smalley dentry = sel_make_dir(sb->s_root, "avc", &fsi->last_ino); 2121a1c2aa1eSAl Viro if (IS_ERR(dentry)) { 2122a1c2aa1eSAl Viro ret = PTR_ERR(dentry); 2123161ce45aSJames Morris goto err; 2124a1c2aa1eSAl Viro } 21251da177e4SLinus Torvalds 21261da177e4SLinus Torvalds ret = sel_make_avc_files(dentry); 212766f8e2f0SJeff Vander Stoep 212866f8e2f0SJeff Vander Stoep dentry = sel_make_dir(sb->s_root, "ss", &fsi->last_ino); 212966f8e2f0SJeff Vander Stoep if (IS_ERR(dentry)) { 213066f8e2f0SJeff Vander Stoep ret = PTR_ERR(dentry); 213166f8e2f0SJeff Vander Stoep goto err; 213266f8e2f0SJeff Vander Stoep } 213366f8e2f0SJeff Vander Stoep 213466f8e2f0SJeff Vander Stoep ret = sel_make_ss_files(dentry); 21351da177e4SLinus Torvalds if (ret) 2136161ce45aSJames Morris goto err; 2137f0ee2e46SJames Carter 21380619f0f5SStephen Smalley dentry = sel_make_dir(sb->s_root, "initial_contexts", &fsi->last_ino); 2139a1c2aa1eSAl Viro if (IS_ERR(dentry)) { 2140a1c2aa1eSAl Viro ret = PTR_ERR(dentry); 2141f0ee2e46SJames Carter goto err; 2142a1c2aa1eSAl Viro } 2143f0ee2e46SJames Carter 2144f0ee2e46SJames Carter ret = sel_make_initcon_files(dentry); 2145f0ee2e46SJames Carter if (ret) 2146f0ee2e46SJames Carter goto err; 2147f0ee2e46SJames Carter 2148613ba187SDaniel Burgener fsi->class_dir = sel_make_dir(sb->s_root, CLASS_DIR_NAME, &fsi->last_ino); 21490619f0f5SStephen Smalley if (IS_ERR(fsi->class_dir)) { 21500619f0f5SStephen Smalley ret = PTR_ERR(fsi->class_dir); 21510619f0f5SStephen Smalley fsi->class_dir = NULL; 2152e47c8fc5SChristopher J. PeBenito goto err; 2153a1c2aa1eSAl Viro } 2154e47c8fc5SChristopher J. PeBenito 2155613ba187SDaniel Burgener fsi->policycap_dir = sel_make_dir(sb->s_root, POLICYCAP_DIR_NAME, 21560619f0f5SStephen Smalley &fsi->last_ino); 21570619f0f5SStephen Smalley if (IS_ERR(fsi->policycap_dir)) { 21580619f0f5SStephen Smalley ret = PTR_ERR(fsi->policycap_dir); 21590619f0f5SStephen Smalley fsi->policycap_dir = NULL; 2160e47c8fc5SChristopher J. PeBenito goto err; 2161a1c2aa1eSAl Viro } 21620619f0f5SStephen Smalley 216302a52c5cSStephen Smalley ret = sel_make_policycap(fsi); 216402a52c5cSStephen Smalley if (ret) { 216502a52c5cSStephen Smalley pr_err("SELinux: failed to load policy capabilities\n"); 21660619f0f5SStephen Smalley goto err; 216702a52c5cSStephen Smalley } 216802a52c5cSStephen Smalley 2169b77a493bSEric Paris return 0; 2170161ce45aSJames Morris err: 2171f8b69a5fSpeter enderborg pr_err("SELinux: %s: failed while creating inodes\n", 2172744ba35eSEric Paris __func__); 21730619f0f5SStephen Smalley 21740619f0f5SStephen Smalley selinux_fs_info_free(sb); 21750619f0f5SStephen Smalley 2176b77a493bSEric Paris return ret; 21771da177e4SLinus Torvalds } 21781da177e4SLinus Torvalds 2179920f50b2SDavid Howells static int sel_get_tree(struct fs_context *fc) 21801da177e4SLinus Torvalds { 2181920f50b2SDavid Howells return get_tree_single(fc, sel_fill_super); 2182920f50b2SDavid Howells } 2183920f50b2SDavid Howells 2184920f50b2SDavid Howells static const struct fs_context_operations sel_context_ops = { 2185920f50b2SDavid Howells .get_tree = sel_get_tree, 2186920f50b2SDavid Howells }; 2187920f50b2SDavid Howells 2188920f50b2SDavid Howells static int sel_init_fs_context(struct fs_context *fc) 2189920f50b2SDavid Howells { 2190920f50b2SDavid Howells fc->ops = &sel_context_ops; 2191920f50b2SDavid Howells return 0; 21921da177e4SLinus Torvalds } 21931da177e4SLinus Torvalds 21940619f0f5SStephen Smalley static void sel_kill_sb(struct super_block *sb) 21950619f0f5SStephen Smalley { 21960619f0f5SStephen Smalley selinux_fs_info_free(sb); 21970619f0f5SStephen Smalley kill_litter_super(sb); 21980619f0f5SStephen Smalley } 21990619f0f5SStephen Smalley 22001da177e4SLinus Torvalds static struct file_system_type sel_fs_type = { 22011da177e4SLinus Torvalds .name = "selinuxfs", 2202920f50b2SDavid Howells .init_fs_context = sel_init_fs_context, 22030619f0f5SStephen Smalley .kill_sb = sel_kill_sb, 22041da177e4SLinus Torvalds }; 22051da177e4SLinus Torvalds 2206cd2bb4cbSOndrej Mosnacek static struct vfsmount *selinuxfs_mount __ro_after_init; 2207cd2bb4cbSOndrej Mosnacek struct path selinux_null __ro_after_init; 22081da177e4SLinus Torvalds 22091da177e4SLinus Torvalds static int __init init_sel_fs(void) 22101da177e4SLinus Torvalds { 22110619f0f5SStephen Smalley struct qstr null_name = QSTR_INIT(NULL_FILE_NAME, 22120619f0f5SStephen Smalley sizeof(NULL_FILE_NAME)-1); 22131da177e4SLinus Torvalds int err; 22141da177e4SLinus Torvalds 22156c5a682eSStephen Smalley if (!selinux_enabled_boot) 22161da177e4SLinus Torvalds return 0; 22177a627e3bSGreg Kroah-Hartman 2218f9bb4882SEric W. Biederman err = sysfs_create_mount_point(fs_kobj, "selinux"); 2219f9bb4882SEric W. Biederman if (err) 2220f9bb4882SEric W. Biederman return err; 22217a627e3bSGreg Kroah-Hartman 22221da177e4SLinus Torvalds err = register_filesystem(&sel_fs_type); 22237a627e3bSGreg Kroah-Hartman if (err) { 2224f9bb4882SEric W. Biederman sysfs_remove_mount_point(fs_kobj, "selinux"); 2225b77a493bSEric Paris return err; 22267a627e3bSGreg Kroah-Hartman } 2227b77a493bSEric Paris 2228765927b2SAl Viro selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type); 22291da177e4SLinus Torvalds if (IS_ERR(selinuxfs_mount)) { 2230f8b69a5fSpeter enderborg pr_err("selinuxfs: could not mount!\n"); 22311da177e4SLinus Torvalds err = PTR_ERR(selinuxfs_mount); 22321da177e4SLinus Torvalds selinuxfs_mount = NULL; 22331da177e4SLinus Torvalds } 22340619f0f5SStephen Smalley selinux_null.dentry = d_hash_and_lookup(selinux_null.mnt->mnt_root, 22350619f0f5SStephen Smalley &null_name); 22360619f0f5SStephen Smalley if (IS_ERR(selinux_null.dentry)) { 22370619f0f5SStephen Smalley pr_err("selinuxfs: could not lookup null!\n"); 22380619f0f5SStephen Smalley err = PTR_ERR(selinux_null.dentry); 22390619f0f5SStephen Smalley selinux_null.dentry = NULL; 22400619f0f5SStephen Smalley } 2241b77a493bSEric Paris 22421da177e4SLinus Torvalds return err; 22431da177e4SLinus Torvalds } 22441da177e4SLinus Torvalds 22451da177e4SLinus Torvalds __initcall(init_sel_fs); 22461da177e4SLinus Torvalds 22471da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 22481da177e4SLinus Torvalds void exit_sel_fs(void) 22491da177e4SLinus Torvalds { 2250f9bb4882SEric W. Biederman sysfs_remove_mount_point(fs_kobj, "selinux"); 2251fd40ffc7SStephen Smalley dput(selinux_null.dentry); 2252423e0ab0STim Chen kern_unmount(selinuxfs_mount); 22531da177e4SLinus Torvalds unregister_filesystem(&sel_fs_type); 22541da177e4SLinus Torvalds } 22551da177e4SLinus Torvalds #endif 2256