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); 566*ee5de60aSOndrej Mosnacek if (ret) 5670eea6091SDaniel Burgener goto out; 5680eea6091SDaniel Burgener 5690eea6091SDaniel Burgener ret = sel_make_classes(newpolicy, tmp_class_dir, 57066ec384aSDaniel Burgener &fsi->last_class_ino); 571*ee5de60aSOndrej Mosnacek if (ret) 5720eea6091SDaniel Burgener goto out; 5730619f0f5SStephen Smalley 5740eea6091SDaniel Burgener /* booleans */ 5750eea6091SDaniel Burgener old_dentry = fsi->bool_dir; 5760eea6091SDaniel Burgener lock_rename(tmp_bool_dir, old_dentry); 5770eea6091SDaniel Burgener d_exchange(tmp_bool_dir, fsi->bool_dir); 5780eea6091SDaniel Burgener 5790eea6091SDaniel Burgener old_bool_num = fsi->bool_num; 5800eea6091SDaniel Burgener old_bool_names = fsi->bool_pending_names; 5810eea6091SDaniel Burgener old_bool_values = fsi->bool_pending_values; 5820eea6091SDaniel Burgener 5830eea6091SDaniel Burgener fsi->bool_num = tmp_bool_num; 5840eea6091SDaniel Burgener fsi->bool_pending_names = tmp_bool_names; 5850eea6091SDaniel Burgener fsi->bool_pending_values = tmp_bool_values; 5860eea6091SDaniel Burgener 5870eea6091SDaniel Burgener sel_remove_old_bool_data(old_bool_num, old_bool_names, old_bool_values); 5880eea6091SDaniel Burgener 5890eea6091SDaniel Burgener fsi->bool_dir = tmp_bool_dir; 5900eea6091SDaniel Burgener unlock_rename(tmp_bool_dir, old_dentry); 5910eea6091SDaniel Burgener 5920eea6091SDaniel Burgener /* classes */ 5930eea6091SDaniel Burgener old_dentry = fsi->class_dir; 5940eea6091SDaniel Burgener lock_rename(tmp_class_dir, old_dentry); 5950eea6091SDaniel Burgener d_exchange(tmp_class_dir, fsi->class_dir); 5960eea6091SDaniel Burgener fsi->class_dir = tmp_class_dir; 5970eea6091SDaniel Burgener unlock_rename(tmp_class_dir, old_dentry); 5980eea6091SDaniel Burgener 5990eea6091SDaniel Burgener out: 6000eea6091SDaniel Burgener /* Since the other temporary dirs are children of tmp_parent 6010eea6091SDaniel Burgener * this will handle all the cleanup in the case of a failure before 6020eea6091SDaniel Burgener * the swapover 6030eea6091SDaniel Burgener */ 6040eea6091SDaniel Burgener sel_remove_entries(tmp_parent); 6050eea6091SDaniel Burgener dput(tmp_parent); /* d_genocide() only handles the children */ 6060eea6091SDaniel Burgener 6070eea6091SDaniel Burgener return ret; 6080619f0f5SStephen Smalley } 6090619f0f5SStephen Smalley 6101da177e4SLinus Torvalds static ssize_t sel_write_load(struct file *file, const char __user *buf, 6111da177e4SLinus Torvalds size_t count, loff_t *ppos) 6121da177e4SLinus Torvalds 6131da177e4SLinus Torvalds { 6140619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 6156406887aSOndrej Mosnacek struct selinux_load_state load_state; 6161da177e4SLinus Torvalds ssize_t length; 6171da177e4SLinus Torvalds void *data = NULL; 6181da177e4SLinus Torvalds 6199ff9abc4SStephen Smalley mutex_lock(&fsi->state->policy_mutex); 6201da177e4SLinus Torvalds 6216b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 6226b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 623be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL); 6241da177e4SLinus Torvalds if (length) 6251da177e4SLinus Torvalds goto out; 6261da177e4SLinus Torvalds 6271da177e4SLinus Torvalds /* No partial writes. */ 6281da177e4SLinus Torvalds length = -EINVAL; 629b77a493bSEric Paris if (*ppos != 0) 6301da177e4SLinus Torvalds goto out; 6311da177e4SLinus Torvalds 632b77a493bSEric Paris length = -ENOMEM; 633b77a493bSEric Paris data = vmalloc(count); 634b77a493bSEric Paris if (!data) 635b77a493bSEric Paris goto out; 6361da177e4SLinus Torvalds 6371da177e4SLinus Torvalds length = -EFAULT; 6381da177e4SLinus Torvalds if (copy_from_user(data, buf, count) != 0) 6391da177e4SLinus Torvalds goto out; 6401da177e4SLinus Torvalds 6416406887aSOndrej Mosnacek length = security_load_policy(fsi->state, data, count, &load_state); 6424262fb51SGary Tierney if (length) { 6434262fb51SGary Tierney pr_warn_ratelimited("SELinux: failed to load policy\n"); 6441da177e4SLinus Torvalds goto out; 6454262fb51SGary Tierney } 6461da177e4SLinus Torvalds 6476406887aSOndrej Mosnacek length = sel_make_policy_nodes(fsi, load_state.policy); 64802a52c5cSStephen Smalley if (length) { 649*ee5de60aSOndrej Mosnacek pr_warn_ratelimited("SELinux: failed to initialize selinuxfs\n"); 6506406887aSOndrej Mosnacek selinux_policy_cancel(fsi->state, &load_state); 651519dad3bSOndrej Mosnacek goto out; 65202a52c5cSStephen Smalley } 65302a52c5cSStephen Smalley 6546406887aSOndrej Mosnacek selinux_policy_commit(fsi->state, &load_state); 655b77a493bSEric Paris 6561da177e4SLinus Torvalds length = count; 657e47c8fc5SChristopher J. PeBenito 658cdfb6b34SRichard Guy Briggs audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_POLICY_LOAD, 659d141136fSRichard Guy Briggs "auid=%u ses=%u lsm=selinux res=1", 660581abc09SEric W. Biederman from_kuid(&init_user_ns, audit_get_loginuid(current)), 6614746ec5bSEric Paris audit_get_sessionid(current)); 6621da177e4SLinus Torvalds out: 6639ff9abc4SStephen Smalley mutex_unlock(&fsi->state->policy_mutex); 6641da177e4SLinus Torvalds vfree(data); 6651da177e4SLinus Torvalds return length; 6661da177e4SLinus Torvalds } 6671da177e4SLinus Torvalds 6689c2e08c5SArjan van de Ven static const struct file_operations sel_load_ops = { 6691da177e4SLinus Torvalds .write = sel_write_load, 67057a62c23SArnd Bergmann .llseek = generic_file_llseek, 6711da177e4SLinus Torvalds }; 6721da177e4SLinus Torvalds 673ce9982d0SStephen Smalley static ssize_t sel_write_context(struct file *file, char *buf, size_t size) 6741da177e4SLinus Torvalds { 6750619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 6760619f0f5SStephen Smalley struct selinux_state *state = fsi->state; 677b77a493bSEric Paris char *canon = NULL; 678ce9982d0SStephen Smalley u32 sid, len; 6791da177e4SLinus Torvalds ssize_t length; 6801da177e4SLinus Torvalds 6816b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 6826b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 683be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__CHECK_CONTEXT, NULL); 6841da177e4SLinus Torvalds if (length) 685b77a493bSEric Paris goto out; 6861da177e4SLinus Torvalds 6870619f0f5SStephen Smalley length = security_context_to_sid(state, buf, size, &sid, GFP_KERNEL); 688b77a493bSEric Paris if (length) 689b77a493bSEric Paris goto out; 6901da177e4SLinus Torvalds 6910619f0f5SStephen Smalley length = security_sid_to_context(state, sid, &canon, &len); 692b77a493bSEric Paris if (length) 693b77a493bSEric Paris goto out; 694ce9982d0SStephen Smalley 695b77a493bSEric Paris length = -ERANGE; 696ce9982d0SStephen Smalley if (len > SIMPLE_TRANSACTION_LIMIT) { 697f8b69a5fSpeter enderborg pr_err("SELinux: %s: context size (%u) exceeds " 698744ba35eSEric Paris "payload max\n", __func__, len); 699ce9982d0SStephen Smalley goto out; 700ce9982d0SStephen Smalley } 701ce9982d0SStephen Smalley 702ce9982d0SStephen Smalley memcpy(buf, canon, len); 703ce9982d0SStephen Smalley length = len; 7041da177e4SLinus Torvalds out: 705ce9982d0SStephen Smalley kfree(canon); 7061da177e4SLinus Torvalds return length; 7071da177e4SLinus Torvalds } 7081da177e4SLinus Torvalds 7091da177e4SLinus Torvalds static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf, 7101da177e4SLinus Torvalds size_t count, loff_t *ppos) 7111da177e4SLinus Torvalds { 7120619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info; 7131da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 7141da177e4SLinus Torvalds ssize_t length; 7151da177e4SLinus Torvalds 7168861d0afSLakshmi Ramasubramanian length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 7178861d0afSLakshmi Ramasubramanian checkreqprot_get(fsi->state)); 7181da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 7191da177e4SLinus Torvalds } 7201da177e4SLinus Torvalds 7211da177e4SLinus Torvalds static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf, 7221da177e4SLinus Torvalds size_t count, loff_t *ppos) 7231da177e4SLinus Torvalds { 7240619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 7258365a719SAl Viro char *page; 7261da177e4SLinus Torvalds ssize_t length; 7271da177e4SLinus Torvalds unsigned int new_value; 7281da177e4SLinus Torvalds 7296b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 7306b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 731be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETCHECKREQPROT, 732be0554c9SStephen Smalley NULL); 7331da177e4SLinus Torvalds if (length) 7348365a719SAl Viro return length; 7351da177e4SLinus Torvalds 736bfd51626SDavi Arnaut if (count >= PAGE_SIZE) 7378365a719SAl Viro return -ENOMEM; 738b77a493bSEric Paris 7391da177e4SLinus Torvalds /* No partial writes. */ 740b77a493bSEric Paris if (*ppos != 0) 7418365a719SAl Viro return -EINVAL; 742b77a493bSEric Paris 7438365a719SAl Viro page = memdup_user_nul(buf, count); 7448365a719SAl Viro if (IS_ERR(page)) 7458365a719SAl Viro return PTR_ERR(page); 7461da177e4SLinus Torvalds 7471da177e4SLinus Torvalds length = -EINVAL; 7481da177e4SLinus Torvalds if (sscanf(page, "%u", &new_value) != 1) 7491da177e4SLinus Torvalds goto out; 7501da177e4SLinus Torvalds 751e9c38f9fSStephen Smalley if (new_value) { 752e9c38f9fSStephen Smalley char comm[sizeof(current->comm)]; 753e9c38f9fSStephen Smalley 754e9c38f9fSStephen Smalley memcpy(comm, current->comm, sizeof(comm)); 755e9c38f9fSStephen Smalley pr_warn_once("SELinux: %s (%d) set checkreqprot to 1. This is deprecated and will be rejected in a future kernel release.\n", 756e9c38f9fSStephen Smalley comm, current->pid); 757e9c38f9fSStephen Smalley } 758e9c38f9fSStephen Smalley 7598861d0afSLakshmi Ramasubramanian checkreqprot_set(fsi->state, (new_value ? 1 : 0)); 7601da177e4SLinus Torvalds length = count; 7611da177e4SLinus Torvalds out: 7628365a719SAl Viro kfree(page); 7631da177e4SLinus Torvalds return length; 7641da177e4SLinus Torvalds } 7659c2e08c5SArjan van de Ven static const struct file_operations sel_checkreqprot_ops = { 7661da177e4SLinus Torvalds .read = sel_read_checkreqprot, 7671da177e4SLinus Torvalds .write = sel_write_checkreqprot, 76857a62c23SArnd Bergmann .llseek = generic_file_llseek, 7691da177e4SLinus Torvalds }; 7701da177e4SLinus Torvalds 771f9df6458SAndrew Perepechko static ssize_t sel_write_validatetrans(struct file *file, 772f9df6458SAndrew Perepechko const char __user *buf, 773f9df6458SAndrew Perepechko size_t count, loff_t *ppos) 774f9df6458SAndrew Perepechko { 7750619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 7760619f0f5SStephen Smalley struct selinux_state *state = fsi->state; 777f9df6458SAndrew Perepechko char *oldcon = NULL, *newcon = NULL, *taskcon = NULL; 778f9df6458SAndrew Perepechko char *req = NULL; 779f9df6458SAndrew Perepechko u32 osid, nsid, tsid; 780f9df6458SAndrew Perepechko u16 tclass; 781f9df6458SAndrew Perepechko int rc; 782f9df6458SAndrew Perepechko 7836b6bc620SStephen Smalley rc = avc_has_perm(&selinux_state, 7846b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 785be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__VALIDATE_TRANS, NULL); 786f9df6458SAndrew Perepechko if (rc) 787f9df6458SAndrew Perepechko goto out; 788f9df6458SAndrew Perepechko 789f9df6458SAndrew Perepechko rc = -ENOMEM; 790f9df6458SAndrew Perepechko if (count >= PAGE_SIZE) 791f9df6458SAndrew Perepechko goto out; 792f9df6458SAndrew Perepechko 793f9df6458SAndrew Perepechko /* No partial writes. */ 794f9df6458SAndrew Perepechko rc = -EINVAL; 795f9df6458SAndrew Perepechko if (*ppos != 0) 796f9df6458SAndrew Perepechko goto out; 797f9df6458SAndrew Perepechko 7980b884d25SAl Viro req = memdup_user_nul(buf, count); 7990b884d25SAl Viro if (IS_ERR(req)) { 8000b884d25SAl Viro rc = PTR_ERR(req); 8010b884d25SAl Viro req = NULL; 802f9df6458SAndrew Perepechko goto out; 8030b884d25SAl Viro } 804f9df6458SAndrew Perepechko 805f9df6458SAndrew Perepechko rc = -ENOMEM; 806f9df6458SAndrew Perepechko oldcon = kzalloc(count + 1, GFP_KERNEL); 807f9df6458SAndrew Perepechko if (!oldcon) 808f9df6458SAndrew Perepechko goto out; 809f9df6458SAndrew Perepechko 810f9df6458SAndrew Perepechko newcon = kzalloc(count + 1, GFP_KERNEL); 811f9df6458SAndrew Perepechko if (!newcon) 812f9df6458SAndrew Perepechko goto out; 813f9df6458SAndrew Perepechko 814f9df6458SAndrew Perepechko taskcon = kzalloc(count + 1, GFP_KERNEL); 815f9df6458SAndrew Perepechko if (!taskcon) 816f9df6458SAndrew Perepechko goto out; 817f9df6458SAndrew Perepechko 818f9df6458SAndrew Perepechko rc = -EINVAL; 819f9df6458SAndrew Perepechko if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4) 820f9df6458SAndrew Perepechko goto out; 821f9df6458SAndrew Perepechko 8220619f0f5SStephen Smalley rc = security_context_str_to_sid(state, oldcon, &osid, GFP_KERNEL); 823f9df6458SAndrew Perepechko if (rc) 824f9df6458SAndrew Perepechko goto out; 825f9df6458SAndrew Perepechko 8260619f0f5SStephen Smalley rc = security_context_str_to_sid(state, newcon, &nsid, GFP_KERNEL); 827f9df6458SAndrew Perepechko if (rc) 828f9df6458SAndrew Perepechko goto out; 829f9df6458SAndrew Perepechko 8300619f0f5SStephen Smalley rc = security_context_str_to_sid(state, taskcon, &tsid, GFP_KERNEL); 831f9df6458SAndrew Perepechko if (rc) 832f9df6458SAndrew Perepechko goto out; 833f9df6458SAndrew Perepechko 8340619f0f5SStephen Smalley rc = security_validate_transition_user(state, osid, nsid, tsid, tclass); 835f9df6458SAndrew Perepechko if (!rc) 836f9df6458SAndrew Perepechko rc = count; 837f9df6458SAndrew Perepechko out: 838f9df6458SAndrew Perepechko kfree(req); 839f9df6458SAndrew Perepechko kfree(oldcon); 840f9df6458SAndrew Perepechko kfree(newcon); 841f9df6458SAndrew Perepechko kfree(taskcon); 842f9df6458SAndrew Perepechko return rc; 843f9df6458SAndrew Perepechko } 844f9df6458SAndrew Perepechko 845f9df6458SAndrew Perepechko static const struct file_operations sel_transition_ops = { 846f9df6458SAndrew Perepechko .write = sel_write_validatetrans, 847f9df6458SAndrew Perepechko .llseek = generic_file_llseek, 848f9df6458SAndrew Perepechko }; 849f9df6458SAndrew Perepechko 8501da177e4SLinus Torvalds /* 8511da177e4SLinus Torvalds * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c 8521da177e4SLinus Torvalds */ 8531da177e4SLinus Torvalds static ssize_t sel_write_access(struct file *file, char *buf, size_t size); 8541da177e4SLinus Torvalds static ssize_t sel_write_create(struct file *file, char *buf, size_t size); 8551da177e4SLinus Torvalds static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size); 8561da177e4SLinus Torvalds static ssize_t sel_write_user(struct file *file, char *buf, size_t size); 8571da177e4SLinus Torvalds static ssize_t sel_write_member(struct file *file, char *buf, size_t size); 8581da177e4SLinus Torvalds 859631d2b49SEric Biggers static ssize_t (*const write_op[])(struct file *, char *, size_t) = { 8601da177e4SLinus Torvalds [SEL_ACCESS] = sel_write_access, 8611da177e4SLinus Torvalds [SEL_CREATE] = sel_write_create, 8621da177e4SLinus Torvalds [SEL_RELABEL] = sel_write_relabel, 8631da177e4SLinus Torvalds [SEL_USER] = sel_write_user, 8641da177e4SLinus Torvalds [SEL_MEMBER] = sel_write_member, 865ce9982d0SStephen Smalley [SEL_CONTEXT] = sel_write_context, 8661da177e4SLinus Torvalds }; 8671da177e4SLinus Torvalds 8681da177e4SLinus Torvalds static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos) 8691da177e4SLinus Torvalds { 870496ad9aaSAl Viro ino_t ino = file_inode(file)->i_ino; 8711da177e4SLinus Torvalds char *data; 8721da177e4SLinus Torvalds ssize_t rv; 8731da177e4SLinus Torvalds 8746e20a64aSNicolas Kaiser if (ino >= ARRAY_SIZE(write_op) || !write_op[ino]) 8751da177e4SLinus Torvalds return -EINVAL; 8761da177e4SLinus Torvalds 8771da177e4SLinus Torvalds data = simple_transaction_get(file, buf, size); 8781da177e4SLinus Torvalds if (IS_ERR(data)) 8791da177e4SLinus Torvalds return PTR_ERR(data); 8801da177e4SLinus Torvalds 8811da177e4SLinus Torvalds rv = write_op[ino](file, data, size); 8821da177e4SLinus Torvalds if (rv > 0) { 8831da177e4SLinus Torvalds simple_transaction_set(file, rv); 8841da177e4SLinus Torvalds rv = size; 8851da177e4SLinus Torvalds } 8861da177e4SLinus Torvalds return rv; 8871da177e4SLinus Torvalds } 8881da177e4SLinus Torvalds 8899c2e08c5SArjan van de Ven static const struct file_operations transaction_ops = { 8901da177e4SLinus Torvalds .write = selinux_transaction_write, 8911da177e4SLinus Torvalds .read = simple_transaction_read, 8921da177e4SLinus Torvalds .release = simple_transaction_release, 89357a62c23SArnd Bergmann .llseek = generic_file_llseek, 8941da177e4SLinus Torvalds }; 8951da177e4SLinus Torvalds 8961da177e4SLinus Torvalds /* 8971da177e4SLinus Torvalds * payload - write methods 8981da177e4SLinus Torvalds * If the method has a response, the response should be put in buf, 8991da177e4SLinus Torvalds * and the length returned. Otherwise return 0 or and -error. 9001da177e4SLinus Torvalds */ 9011da177e4SLinus Torvalds 9021da177e4SLinus Torvalds static ssize_t sel_write_access(struct file *file, char *buf, size_t size) 9031da177e4SLinus Torvalds { 9040619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 9050619f0f5SStephen Smalley struct selinux_state *state = fsi->state; 906b77a493bSEric Paris char *scon = NULL, *tcon = NULL; 9071da177e4SLinus Torvalds u32 ssid, tsid; 9081da177e4SLinus Torvalds u16 tclass; 9091da177e4SLinus Torvalds struct av_decision avd; 9101da177e4SLinus Torvalds ssize_t length; 9111da177e4SLinus Torvalds 9126b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 9136b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 914be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_AV, NULL); 9151da177e4SLinus Torvalds if (length) 916b77a493bSEric Paris goto out; 9171da177e4SLinus Torvalds 9181da177e4SLinus Torvalds length = -ENOMEM; 91989d155efSJames Morris scon = kzalloc(size + 1, GFP_KERNEL); 9201da177e4SLinus Torvalds if (!scon) 921b77a493bSEric Paris goto out; 9221da177e4SLinus Torvalds 923b77a493bSEric Paris length = -ENOMEM; 92489d155efSJames Morris tcon = kzalloc(size + 1, GFP_KERNEL); 9251da177e4SLinus Torvalds if (!tcon) 9261da177e4SLinus Torvalds goto out; 9271da177e4SLinus Torvalds 9281da177e4SLinus Torvalds length = -EINVAL; 92919439d05SStephen Smalley if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) 930b77a493bSEric Paris goto out; 9311da177e4SLinus Torvalds 9320619f0f5SStephen Smalley length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL); 933b77a493bSEric Paris if (length) 934b77a493bSEric Paris goto out; 935b77a493bSEric Paris 9360619f0f5SStephen Smalley length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL); 937b77a493bSEric Paris if (length) 938b77a493bSEric Paris goto out; 9391da177e4SLinus Torvalds 9400619f0f5SStephen Smalley security_compute_av_user(state, ssid, tsid, tclass, &avd); 9411da177e4SLinus Torvalds 9421da177e4SLinus Torvalds length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, 9438a6f83afSKaiGai Kohei "%x %x %x %x %u %x", 944f1c6381aSEric Paris avd.allowed, 0xffffffff, 9451da177e4SLinus Torvalds avd.auditallow, avd.auditdeny, 9468a6f83afSKaiGai Kohei avd.seqno, avd.flags); 9471da177e4SLinus Torvalds out: 948b77a493bSEric Paris kfree(tcon); 9491da177e4SLinus Torvalds kfree(scon); 9501da177e4SLinus Torvalds return length; 9511da177e4SLinus Torvalds } 9521da177e4SLinus Torvalds 9531da177e4SLinus Torvalds static ssize_t sel_write_create(struct file *file, char *buf, size_t size) 9541da177e4SLinus Torvalds { 9550619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 9560619f0f5SStephen Smalley struct selinux_state *state = fsi->state; 957b77a493bSEric Paris char *scon = NULL, *tcon = NULL; 958f50a3ec9SKohei Kaigai char *namebuf = NULL, *objname = NULL; 9591da177e4SLinus Torvalds u32 ssid, tsid, newsid; 9601da177e4SLinus Torvalds u16 tclass; 9611da177e4SLinus Torvalds ssize_t length; 962b77a493bSEric Paris char *newcon = NULL; 9631da177e4SLinus Torvalds u32 len; 964f50a3ec9SKohei Kaigai int nargs; 9651da177e4SLinus Torvalds 9666b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 9676b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 968be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_CREATE, 969be0554c9SStephen Smalley NULL); 9701da177e4SLinus Torvalds if (length) 971b77a493bSEric Paris goto out; 9721da177e4SLinus Torvalds 9731da177e4SLinus Torvalds length = -ENOMEM; 97489d155efSJames Morris scon = kzalloc(size + 1, GFP_KERNEL); 9751da177e4SLinus Torvalds if (!scon) 976b77a493bSEric Paris goto out; 9771da177e4SLinus Torvalds 978b77a493bSEric Paris length = -ENOMEM; 97989d155efSJames Morris tcon = kzalloc(size + 1, GFP_KERNEL); 9801da177e4SLinus Torvalds if (!tcon) 9811da177e4SLinus Torvalds goto out; 9821da177e4SLinus Torvalds 983f50a3ec9SKohei Kaigai length = -ENOMEM; 984f50a3ec9SKohei Kaigai namebuf = kzalloc(size + 1, GFP_KERNEL); 985f50a3ec9SKohei Kaigai if (!namebuf) 986b77a493bSEric Paris goto out; 9871da177e4SLinus Torvalds 988f50a3ec9SKohei Kaigai length = -EINVAL; 989f50a3ec9SKohei Kaigai nargs = sscanf(buf, "%s %s %hu %s", scon, tcon, &tclass, namebuf); 990f50a3ec9SKohei Kaigai if (nargs < 3 || nargs > 4) 991f50a3ec9SKohei Kaigai goto out; 9920f7e4c33SKohei Kaigai if (nargs == 4) { 9930f7e4c33SKohei Kaigai /* 9940f7e4c33SKohei Kaigai * If and when the name of new object to be queried contains 9950f7e4c33SKohei Kaigai * either whitespace or multibyte characters, they shall be 9960f7e4c33SKohei Kaigai * encoded based on the percentage-encoding rule. 9970f7e4c33SKohei Kaigai * If not encoded, the sscanf logic picks up only left-half 9980f7e4c33SKohei Kaigai * of the supplied name; splitted by a whitespace unexpectedly. 9990f7e4c33SKohei Kaigai */ 10000f7e4c33SKohei Kaigai char *r, *w; 10010f7e4c33SKohei Kaigai int c1, c2; 10020f7e4c33SKohei Kaigai 10030f7e4c33SKohei Kaigai r = w = namebuf; 10040f7e4c33SKohei Kaigai do { 10050f7e4c33SKohei Kaigai c1 = *r++; 10060f7e4c33SKohei Kaigai if (c1 == '+') 10070f7e4c33SKohei Kaigai c1 = ' '; 10080f7e4c33SKohei Kaigai else if (c1 == '%') { 1009af7ff2c2SAndy Shevchenko c1 = hex_to_bin(*r++); 1010af7ff2c2SAndy Shevchenko if (c1 < 0) 10110f7e4c33SKohei Kaigai goto out; 1012af7ff2c2SAndy Shevchenko c2 = hex_to_bin(*r++); 1013af7ff2c2SAndy Shevchenko if (c2 < 0) 10140f7e4c33SKohei Kaigai goto out; 10150f7e4c33SKohei Kaigai c1 = (c1 << 4) | c2; 10160f7e4c33SKohei Kaigai } 10170f7e4c33SKohei Kaigai *w++ = c1; 10180f7e4c33SKohei Kaigai } while (c1 != '\0'); 10190f7e4c33SKohei Kaigai 1020f50a3ec9SKohei Kaigai objname = namebuf; 10210f7e4c33SKohei Kaigai } 1022f50a3ec9SKohei Kaigai 10230619f0f5SStephen Smalley length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL); 1024b77a493bSEric Paris if (length) 1025b77a493bSEric Paris goto out; 1026b77a493bSEric Paris 10270619f0f5SStephen Smalley length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL); 1028b77a493bSEric Paris if (length) 1029b77a493bSEric Paris goto out; 10301da177e4SLinus Torvalds 10310619f0f5SStephen Smalley length = security_transition_sid_user(state, ssid, tsid, tclass, 10320619f0f5SStephen Smalley objname, &newsid); 1033b77a493bSEric Paris if (length) 1034b77a493bSEric Paris goto out; 10351da177e4SLinus Torvalds 10360619f0f5SStephen Smalley length = security_sid_to_context(state, newsid, &newcon, &len); 1037b77a493bSEric Paris if (length) 1038b77a493bSEric Paris goto out; 10391da177e4SLinus Torvalds 1040b77a493bSEric Paris length = -ERANGE; 10411da177e4SLinus Torvalds if (len > SIMPLE_TRANSACTION_LIMIT) { 1042f8b69a5fSpeter enderborg pr_err("SELinux: %s: context size (%u) exceeds " 1043744ba35eSEric Paris "payload max\n", __func__, len); 1044b77a493bSEric Paris goto out; 10451da177e4SLinus Torvalds } 10461da177e4SLinus Torvalds 10471da177e4SLinus Torvalds memcpy(buf, newcon, len); 10481da177e4SLinus Torvalds length = len; 10491da177e4SLinus Torvalds out: 1050b77a493bSEric Paris kfree(newcon); 1051f50a3ec9SKohei Kaigai kfree(namebuf); 1052b77a493bSEric Paris kfree(tcon); 10531da177e4SLinus Torvalds kfree(scon); 10541da177e4SLinus Torvalds return length; 10551da177e4SLinus Torvalds } 10561da177e4SLinus Torvalds 10571da177e4SLinus Torvalds static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size) 10581da177e4SLinus Torvalds { 10590619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 10600619f0f5SStephen Smalley struct selinux_state *state = fsi->state; 1061b77a493bSEric Paris char *scon = NULL, *tcon = NULL; 10621da177e4SLinus Torvalds u32 ssid, tsid, newsid; 10631da177e4SLinus Torvalds u16 tclass; 10641da177e4SLinus Torvalds ssize_t length; 1065b77a493bSEric Paris char *newcon = NULL; 10661da177e4SLinus Torvalds u32 len; 10671da177e4SLinus Torvalds 10686b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 10696b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 1070be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_RELABEL, 1071be0554c9SStephen Smalley NULL); 10721da177e4SLinus Torvalds if (length) 1073b77a493bSEric Paris goto out; 10741da177e4SLinus Torvalds 10751da177e4SLinus Torvalds length = -ENOMEM; 107689d155efSJames Morris scon = kzalloc(size + 1, GFP_KERNEL); 10771da177e4SLinus Torvalds if (!scon) 1078b77a493bSEric Paris goto out; 10791da177e4SLinus Torvalds 1080b77a493bSEric Paris length = -ENOMEM; 108189d155efSJames Morris tcon = kzalloc(size + 1, GFP_KERNEL); 10821da177e4SLinus Torvalds if (!tcon) 10831da177e4SLinus Torvalds goto out; 10841da177e4SLinus Torvalds 10851da177e4SLinus Torvalds length = -EINVAL; 10861da177e4SLinus Torvalds if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) 1087b77a493bSEric Paris goto out; 10881da177e4SLinus Torvalds 10890619f0f5SStephen Smalley length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL); 1090b77a493bSEric Paris if (length) 1091b77a493bSEric Paris goto out; 1092b77a493bSEric Paris 10930619f0f5SStephen Smalley length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL); 1094b77a493bSEric Paris if (length) 1095b77a493bSEric Paris goto out; 10961da177e4SLinus Torvalds 10970619f0f5SStephen Smalley length = security_change_sid(state, ssid, tsid, tclass, &newsid); 1098b77a493bSEric Paris if (length) 1099b77a493bSEric Paris goto out; 11001da177e4SLinus Torvalds 11010619f0f5SStephen Smalley length = security_sid_to_context(state, newsid, &newcon, &len); 1102b77a493bSEric Paris if (length) 1103b77a493bSEric Paris goto out; 11041da177e4SLinus Torvalds 11051da177e4SLinus Torvalds length = -ERANGE; 1106b77a493bSEric Paris if (len > SIMPLE_TRANSACTION_LIMIT) 1107b77a493bSEric Paris goto out; 11081da177e4SLinus Torvalds 11091da177e4SLinus Torvalds memcpy(buf, newcon, len); 11101da177e4SLinus Torvalds length = len; 11111da177e4SLinus Torvalds out: 1112b77a493bSEric Paris kfree(newcon); 1113b77a493bSEric Paris kfree(tcon); 11141da177e4SLinus Torvalds kfree(scon); 11151da177e4SLinus Torvalds return length; 11161da177e4SLinus Torvalds } 11171da177e4SLinus Torvalds 11181da177e4SLinus Torvalds static ssize_t sel_write_user(struct file *file, char *buf, size_t size) 11191da177e4SLinus Torvalds { 11200619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 11210619f0f5SStephen Smalley struct selinux_state *state = fsi->state; 1122b77a493bSEric Paris char *con = NULL, *user = NULL, *ptr; 1123b77a493bSEric Paris u32 sid, *sids = NULL; 11241da177e4SLinus Torvalds ssize_t length; 11251da177e4SLinus Torvalds char *newcon; 11261da177e4SLinus Torvalds int i, rc; 11271da177e4SLinus Torvalds u32 len, nsids; 11281da177e4SLinus Torvalds 11296b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 11306b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 1131be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_USER, 1132be0554c9SStephen Smalley NULL); 11331da177e4SLinus Torvalds if (length) 11346eab04a8SJustin P. Mattock goto out; 11351da177e4SLinus Torvalds 11361da177e4SLinus Torvalds length = -ENOMEM; 113789d155efSJames Morris con = kzalloc(size + 1, GFP_KERNEL); 11381da177e4SLinus Torvalds if (!con) 11396eab04a8SJustin P. Mattock goto out; 11401da177e4SLinus Torvalds 1141b77a493bSEric Paris length = -ENOMEM; 114289d155efSJames Morris user = kzalloc(size + 1, GFP_KERNEL); 11431da177e4SLinus Torvalds if (!user) 11441da177e4SLinus Torvalds goto out; 11451da177e4SLinus Torvalds 11461da177e4SLinus Torvalds length = -EINVAL; 11471da177e4SLinus Torvalds if (sscanf(buf, "%s %s", con, user) != 2) 1148b77a493bSEric Paris goto out; 11491da177e4SLinus Torvalds 11500619f0f5SStephen Smalley length = security_context_str_to_sid(state, con, &sid, GFP_KERNEL); 1151b77a493bSEric Paris if (length) 1152b77a493bSEric Paris goto out; 11531da177e4SLinus Torvalds 11540619f0f5SStephen Smalley length = security_get_user_sids(state, sid, user, &sids, &nsids); 1155b77a493bSEric Paris if (length) 1156b77a493bSEric Paris goto out; 11571da177e4SLinus Torvalds 11581da177e4SLinus Torvalds length = sprintf(buf, "%u", nsids) + 1; 11591da177e4SLinus Torvalds ptr = buf + length; 11601da177e4SLinus Torvalds for (i = 0; i < nsids; i++) { 11610619f0f5SStephen Smalley rc = security_sid_to_context(state, sids[i], &newcon, &len); 11621da177e4SLinus Torvalds if (rc) { 11631da177e4SLinus Torvalds length = rc; 1164b77a493bSEric Paris goto out; 11651da177e4SLinus Torvalds } 11661da177e4SLinus Torvalds if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) { 11671da177e4SLinus Torvalds kfree(newcon); 11681da177e4SLinus Torvalds length = -ERANGE; 1169b77a493bSEric Paris goto out; 11701da177e4SLinus Torvalds } 11711da177e4SLinus Torvalds memcpy(ptr, newcon, len); 11721da177e4SLinus Torvalds kfree(newcon); 11731da177e4SLinus Torvalds ptr += len; 11741da177e4SLinus Torvalds length += len; 11751da177e4SLinus Torvalds } 11761da177e4SLinus Torvalds out: 1177b77a493bSEric Paris kfree(sids); 1178b77a493bSEric Paris kfree(user); 11791da177e4SLinus Torvalds kfree(con); 11801da177e4SLinus Torvalds return length; 11811da177e4SLinus Torvalds } 11821da177e4SLinus Torvalds 11831da177e4SLinus Torvalds static ssize_t sel_write_member(struct file *file, char *buf, size_t size) 11841da177e4SLinus Torvalds { 11850619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 11860619f0f5SStephen Smalley struct selinux_state *state = fsi->state; 1187b77a493bSEric Paris char *scon = NULL, *tcon = NULL; 11881da177e4SLinus Torvalds u32 ssid, tsid, newsid; 11891da177e4SLinus Torvalds u16 tclass; 11901da177e4SLinus Torvalds ssize_t length; 1191b77a493bSEric Paris char *newcon = NULL; 11921da177e4SLinus Torvalds u32 len; 11931da177e4SLinus Torvalds 11946b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 11956b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 1196be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_MEMBER, 1197be0554c9SStephen Smalley NULL); 11981da177e4SLinus Torvalds if (length) 1199b77a493bSEric Paris goto out; 12001da177e4SLinus Torvalds 12011da177e4SLinus Torvalds length = -ENOMEM; 120289d155efSJames Morris scon = kzalloc(size + 1, GFP_KERNEL); 12031da177e4SLinus Torvalds if (!scon) 12046eab04a8SJustin P. Mattock goto out; 12051da177e4SLinus Torvalds 1206b77a493bSEric Paris length = -ENOMEM; 120789d155efSJames Morris tcon = kzalloc(size + 1, GFP_KERNEL); 12081da177e4SLinus Torvalds if (!tcon) 12091da177e4SLinus Torvalds goto out; 12101da177e4SLinus Torvalds 12111da177e4SLinus Torvalds length = -EINVAL; 12121da177e4SLinus Torvalds if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) 1213b77a493bSEric Paris goto out; 12141da177e4SLinus Torvalds 12150619f0f5SStephen Smalley length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL); 1216b77a493bSEric Paris if (length) 1217b77a493bSEric Paris goto out; 1218b77a493bSEric Paris 12190619f0f5SStephen Smalley length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL); 1220b77a493bSEric Paris if (length) 1221b77a493bSEric Paris goto out; 12221da177e4SLinus Torvalds 12230619f0f5SStephen Smalley length = security_member_sid(state, ssid, tsid, tclass, &newsid); 1224b77a493bSEric Paris if (length) 1225b77a493bSEric Paris goto out; 12261da177e4SLinus Torvalds 12270619f0f5SStephen Smalley length = security_sid_to_context(state, newsid, &newcon, &len); 1228b77a493bSEric Paris if (length) 1229b77a493bSEric Paris goto out; 12301da177e4SLinus Torvalds 1231b77a493bSEric Paris length = -ERANGE; 12321da177e4SLinus Torvalds if (len > SIMPLE_TRANSACTION_LIMIT) { 1233f8b69a5fSpeter enderborg pr_err("SELinux: %s: context size (%u) exceeds " 1234744ba35eSEric Paris "payload max\n", __func__, len); 1235b77a493bSEric Paris goto out; 12361da177e4SLinus Torvalds } 12371da177e4SLinus Torvalds 12381da177e4SLinus Torvalds memcpy(buf, newcon, len); 12391da177e4SLinus Torvalds length = len; 12401da177e4SLinus Torvalds out: 1241b77a493bSEric Paris kfree(newcon); 1242b77a493bSEric Paris kfree(tcon); 12431da177e4SLinus Torvalds kfree(scon); 12441da177e4SLinus Torvalds return length; 12451da177e4SLinus Torvalds } 12461da177e4SLinus Torvalds 12471da177e4SLinus Torvalds static struct inode *sel_make_inode(struct super_block *sb, int mode) 12481da177e4SLinus Torvalds { 12491da177e4SLinus Torvalds struct inode *ret = new_inode(sb); 12501da177e4SLinus Torvalds 12511da177e4SLinus Torvalds if (ret) { 12521da177e4SLinus Torvalds ret->i_mode = mode; 1253078cd827SDeepa Dinamani ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret); 12541da177e4SLinus Torvalds } 12551da177e4SLinus Torvalds return ret; 12561da177e4SLinus Torvalds } 12571da177e4SLinus Torvalds 12581da177e4SLinus Torvalds static ssize_t sel_read_bool(struct file *filep, char __user *buf, 12591da177e4SLinus Torvalds size_t count, loff_t *ppos) 12601da177e4SLinus Torvalds { 12610619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info; 12621da177e4SLinus Torvalds char *page = NULL; 12631da177e4SLinus Torvalds ssize_t length; 12641da177e4SLinus Torvalds ssize_t ret; 12651da177e4SLinus Torvalds int cur_enforcing; 1266496ad9aaSAl Viro unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK; 1267d313f948SStephen Smalley const char *name = filep->f_path.dentry->d_name.name; 12681da177e4SLinus Torvalds 12699ff9abc4SStephen Smalley mutex_lock(&fsi->state->policy_mutex); 12701da177e4SLinus Torvalds 1271d313f948SStephen Smalley ret = -EINVAL; 12720619f0f5SStephen Smalley if (index >= fsi->bool_num || strcmp(name, 12730619f0f5SStephen Smalley fsi->bool_pending_names[index])) 12740da74120SJann Horn goto out_unlock; 12751da177e4SLinus Torvalds 12761da177e4SLinus Torvalds ret = -ENOMEM; 1277b77a493bSEric Paris page = (char *)get_zeroed_page(GFP_KERNEL); 1278b77a493bSEric Paris if (!page) 12790da74120SJann Horn goto out_unlock; 12801da177e4SLinus Torvalds 12810619f0f5SStephen Smalley cur_enforcing = security_get_bool_value(fsi->state, index); 12821da177e4SLinus Torvalds if (cur_enforcing < 0) { 12831da177e4SLinus Torvalds ret = cur_enforcing; 12840da74120SJann Horn goto out_unlock; 12851da177e4SLinus Torvalds } 12861da177e4SLinus Torvalds length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing, 12870619f0f5SStephen Smalley fsi->bool_pending_values[index]); 12889ff9abc4SStephen Smalley mutex_unlock(&fsi->state->policy_mutex); 12890da74120SJann Horn ret = simple_read_from_buffer(buf, count, ppos, page, length); 12900da74120SJann Horn out_free: 12911da177e4SLinus Torvalds free_page((unsigned long)page); 12921da177e4SLinus Torvalds return ret; 12930da74120SJann Horn 12940da74120SJann Horn out_unlock: 12959ff9abc4SStephen Smalley mutex_unlock(&fsi->state->policy_mutex); 12960da74120SJann Horn goto out_free; 12971da177e4SLinus Torvalds } 12981da177e4SLinus Torvalds 12991da177e4SLinus Torvalds static ssize_t sel_write_bool(struct file *filep, const char __user *buf, 13001da177e4SLinus Torvalds size_t count, loff_t *ppos) 13011da177e4SLinus Torvalds { 13020619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info; 13031da177e4SLinus Torvalds char *page = NULL; 1304d313f948SStephen Smalley ssize_t length; 13051da177e4SLinus Torvalds int new_value; 1306496ad9aaSAl Viro unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK; 1307d313f948SStephen Smalley const char *name = filep->f_path.dentry->d_name.name; 13081da177e4SLinus Torvalds 13090da74120SJann Horn if (count >= PAGE_SIZE) 13100da74120SJann Horn return -ENOMEM; 13110da74120SJann Horn 13120da74120SJann Horn /* No partial writes. */ 13130da74120SJann Horn if (*ppos != 0) 13140da74120SJann Horn return -EINVAL; 13150da74120SJann Horn 13160da74120SJann Horn page = memdup_user_nul(buf, count); 13170da74120SJann Horn if (IS_ERR(page)) 13180da74120SJann Horn return PTR_ERR(page); 13190da74120SJann Horn 13209ff9abc4SStephen Smalley mutex_lock(&fsi->state->policy_mutex); 13211da177e4SLinus Torvalds 13226b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 13236b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 1324be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETBOOL, 1325be0554c9SStephen Smalley NULL); 13261da177e4SLinus Torvalds if (length) 13271da177e4SLinus Torvalds goto out; 13281da177e4SLinus Torvalds 1329d313f948SStephen Smalley length = -EINVAL; 13300619f0f5SStephen Smalley if (index >= fsi->bool_num || strcmp(name, 13310619f0f5SStephen Smalley fsi->bool_pending_names[index])) 1332d313f948SStephen Smalley goto out; 1333d313f948SStephen Smalley 13341da177e4SLinus Torvalds length = -EINVAL; 13351da177e4SLinus Torvalds if (sscanf(page, "%d", &new_value) != 1) 13361da177e4SLinus Torvalds goto out; 13371da177e4SLinus Torvalds 13381da177e4SLinus Torvalds if (new_value) 13391da177e4SLinus Torvalds new_value = 1; 13401da177e4SLinus Torvalds 13410619f0f5SStephen Smalley fsi->bool_pending_values[index] = new_value; 13421da177e4SLinus Torvalds length = count; 13431da177e4SLinus Torvalds 13441da177e4SLinus Torvalds out: 13459ff9abc4SStephen Smalley mutex_unlock(&fsi->state->policy_mutex); 13468365a719SAl Viro kfree(page); 13471da177e4SLinus Torvalds return length; 13481da177e4SLinus Torvalds } 13491da177e4SLinus Torvalds 13509c2e08c5SArjan van de Ven static const struct file_operations sel_bool_ops = { 13511da177e4SLinus Torvalds .read = sel_read_bool, 13521da177e4SLinus Torvalds .write = sel_write_bool, 135357a62c23SArnd Bergmann .llseek = generic_file_llseek, 13541da177e4SLinus Torvalds }; 13551da177e4SLinus Torvalds 13561da177e4SLinus Torvalds static ssize_t sel_commit_bools_write(struct file *filep, 13571da177e4SLinus Torvalds const char __user *buf, 13581da177e4SLinus Torvalds size_t count, loff_t *ppos) 13591da177e4SLinus Torvalds { 13600619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info; 13611da177e4SLinus Torvalds char *page = NULL; 1362d313f948SStephen Smalley ssize_t length; 13631da177e4SLinus Torvalds int new_value; 13641da177e4SLinus Torvalds 13650da74120SJann Horn if (count >= PAGE_SIZE) 13660da74120SJann Horn return -ENOMEM; 13670da74120SJann Horn 13680da74120SJann Horn /* No partial writes. */ 13690da74120SJann Horn if (*ppos != 0) 13700da74120SJann Horn return -EINVAL; 13710da74120SJann Horn 13720da74120SJann Horn page = memdup_user_nul(buf, count); 13730da74120SJann Horn if (IS_ERR(page)) 13740da74120SJann Horn return PTR_ERR(page); 13750da74120SJann Horn 13769ff9abc4SStephen Smalley mutex_lock(&fsi->state->policy_mutex); 13771da177e4SLinus Torvalds 13786b6bc620SStephen Smalley length = avc_has_perm(&selinux_state, 13796b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 1380be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETBOOL, 1381be0554c9SStephen Smalley NULL); 13821da177e4SLinus Torvalds if (length) 13831da177e4SLinus Torvalds goto out; 13841da177e4SLinus Torvalds 13851da177e4SLinus Torvalds length = -EINVAL; 13861da177e4SLinus Torvalds if (sscanf(page, "%d", &new_value) != 1) 13871da177e4SLinus Torvalds goto out; 13881da177e4SLinus Torvalds 1389b77a493bSEric Paris length = 0; 13900619f0f5SStephen Smalley if (new_value && fsi->bool_pending_values) 13910619f0f5SStephen Smalley length = security_set_bools(fsi->state, fsi->bool_num, 13920619f0f5SStephen Smalley fsi->bool_pending_values); 13931da177e4SLinus Torvalds 1394b77a493bSEric Paris if (!length) 13951da177e4SLinus Torvalds length = count; 13961da177e4SLinus Torvalds 13971da177e4SLinus Torvalds out: 13989ff9abc4SStephen Smalley mutex_unlock(&fsi->state->policy_mutex); 13998365a719SAl Viro kfree(page); 14001da177e4SLinus Torvalds return length; 14011da177e4SLinus Torvalds } 14021da177e4SLinus Torvalds 14039c2e08c5SArjan van de Ven static const struct file_operations sel_commit_bools_ops = { 14041da177e4SLinus Torvalds .write = sel_commit_bools_write, 140557a62c23SArnd Bergmann .llseek = generic_file_llseek, 14061da177e4SLinus Torvalds }; 14071da177e4SLinus Torvalds 14080c92d7c7SChristopher J. PeBenito static void sel_remove_entries(struct dentry *de) 14091da177e4SLinus Torvalds { 1410ad52184bSAl Viro d_genocide(de); 1411ad52184bSAl Viro shrink_dcache_parent(de); 14121da177e4SLinus Torvalds } 14131da177e4SLinus Torvalds 141466ec384aSDaniel Burgener static int sel_make_bools(struct selinux_policy *newpolicy, struct dentry *bool_dir, 141566ec384aSDaniel Burgener unsigned int *bool_num, char ***bool_pending_names, 141666ec384aSDaniel Burgener unsigned int **bool_pending_values) 14171da177e4SLinus Torvalds { 141860abd318SOndrej Mosnacek int ret; 14191da177e4SLinus Torvalds ssize_t len; 14201da177e4SLinus Torvalds struct dentry *dentry = NULL; 14211da177e4SLinus Torvalds struct inode *inode = NULL; 14221da177e4SLinus Torvalds struct inode_security_struct *isec; 14231da177e4SLinus Torvalds char **names = NULL, *page; 142460abd318SOndrej Mosnacek u32 i, num; 14251da177e4SLinus Torvalds int *values = NULL; 14261da177e4SLinus Torvalds u32 sid; 14271da177e4SLinus Torvalds 1428b77a493bSEric Paris ret = -ENOMEM; 14291872981bSEric Paris page = (char *)get_zeroed_page(GFP_KERNEL); 14301872981bSEric Paris if (!page) 1431b77a493bSEric Paris goto out; 14321da177e4SLinus Torvalds 143302a52c5cSStephen Smalley ret = security_get_bools(newpolicy, &num, &names, &values); 1434b77a493bSEric Paris if (ret) 14351da177e4SLinus Torvalds goto out; 14361da177e4SLinus Torvalds 14371da177e4SLinus Torvalds for (i = 0; i < num; i++) { 1438b77a493bSEric Paris ret = -ENOMEM; 143966ec384aSDaniel Burgener dentry = d_alloc_name(bool_dir, names[i]); 1440b77a493bSEric Paris if (!dentry) 1441b77a493bSEric Paris goto out; 14421da177e4SLinus Torvalds 1443b77a493bSEric Paris ret = -ENOMEM; 144466ec384aSDaniel Burgener inode = sel_make_inode(bool_dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR); 14457e4237faSnixiaoming if (!inode) { 14467e4237faSnixiaoming dput(dentry); 1447b77a493bSEric Paris goto out; 14487e4237faSnixiaoming } 1449b77a493bSEric Paris 14501da177e4SLinus Torvalds ret = -ENAMETOOLONG; 1451cc1dad71SAl Viro len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]); 14527e4237faSnixiaoming if (len >= PAGE_SIZE) { 14537e4237faSnixiaoming dput(dentry); 14547e4237faSnixiaoming iput(inode); 1455b77a493bSEric Paris goto out; 14567e4237faSnixiaoming } 1457b77a493bSEric Paris 145880788c22SCasey Schaufler isec = selinux_inode(inode); 145902a52c5cSStephen Smalley ret = selinux_policy_genfs_sid(newpolicy, "selinuxfs", page, 1460aa8e712cSStephen Smalley SECCLASS_FILE, &sid); 14614262fb51SGary Tierney if (ret) { 1462900fde06SGary Tierney pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n", 1463900fde06SGary Tierney page); 1464900fde06SGary Tierney sid = SECINITSID_SECURITY; 14654262fb51SGary Tierney } 14664262fb51SGary Tierney 14671da177e4SLinus Torvalds isec->sid = sid; 146842059112SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 14691da177e4SLinus Torvalds inode->i_fop = &sel_bool_ops; 1470bce34bc0SJames Carter inode->i_ino = i|SEL_BOOL_INO_OFFSET; 14711da177e4SLinus Torvalds d_add(dentry, inode); 14721da177e4SLinus Torvalds } 147366ec384aSDaniel Burgener *bool_num = num; 147466ec384aSDaniel Burgener *bool_pending_names = names; 147566ec384aSDaniel Burgener *bool_pending_values = values; 1476b77a493bSEric Paris 1477b77a493bSEric Paris free_page((unsigned long)page); 1478b77a493bSEric Paris return 0; 14791da177e4SLinus Torvalds out: 14801da177e4SLinus Torvalds free_page((unsigned long)page); 1481b77a493bSEric Paris 14821da177e4SLinus Torvalds if (names) { 14839a5f04bfSJesper Juhl for (i = 0; i < num; i++) 14841da177e4SLinus Torvalds kfree(names[i]); 14851da177e4SLinus Torvalds kfree(names); 14861da177e4SLinus Torvalds } 148720c19e41SDavi Arnaut kfree(values); 148866ec384aSDaniel Burgener sel_remove_entries(bool_dir); 1489b77a493bSEric Paris 1490b77a493bSEric Paris return ret; 14911da177e4SLinus Torvalds } 14921da177e4SLinus Torvalds 14931da177e4SLinus Torvalds static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf, 14941da177e4SLinus Torvalds size_t count, loff_t *ppos) 14951da177e4SLinus Torvalds { 14966b6bc620SStephen Smalley struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info; 14976b6bc620SStephen Smalley struct selinux_state *state = fsi->state; 14981da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 14991da177e4SLinus Torvalds ssize_t length; 15001da177e4SLinus Torvalds 15016b6bc620SStephen Smalley length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 15026b6bc620SStephen Smalley avc_get_cache_threshold(state->avc)); 15031da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 15041da177e4SLinus Torvalds } 15051da177e4SLinus Torvalds 15061da177e4SLinus Torvalds static ssize_t sel_write_avc_cache_threshold(struct file *file, 15071da177e4SLinus Torvalds const char __user *buf, 15081da177e4SLinus Torvalds size_t count, loff_t *ppos) 15091da177e4SLinus Torvalds 15101da177e4SLinus Torvalds { 15116b6bc620SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 15126b6bc620SStephen Smalley struct selinux_state *state = fsi->state; 15138365a719SAl Viro char *page; 15141da177e4SLinus Torvalds ssize_t ret; 1515309c5fadSHeinrich Schuchardt unsigned int new_value; 15161da177e4SLinus Torvalds 15176b6bc620SStephen Smalley ret = avc_has_perm(&selinux_state, 15186b6bc620SStephen Smalley current_sid(), SECINITSID_SECURITY, 1519be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETSECPARAM, 1520be0554c9SStephen Smalley NULL); 15211da177e4SLinus Torvalds if (ret) 15228365a719SAl Viro return ret; 1523b77a493bSEric Paris 1524b77a493bSEric Paris if (count >= PAGE_SIZE) 15258365a719SAl Viro return -ENOMEM; 1526b77a493bSEric Paris 1527b77a493bSEric Paris /* No partial writes. */ 1528b77a493bSEric Paris if (*ppos != 0) 15298365a719SAl Viro return -EINVAL; 1530b77a493bSEric Paris 15318365a719SAl Viro page = memdup_user_nul(buf, count); 15328365a719SAl Viro if (IS_ERR(page)) 15338365a719SAl Viro return PTR_ERR(page); 1534b77a493bSEric Paris 1535b77a493bSEric Paris ret = -EINVAL; 1536b77a493bSEric Paris if (sscanf(page, "%u", &new_value) != 1) 1537b77a493bSEric Paris goto out; 1538b77a493bSEric Paris 15396b6bc620SStephen Smalley avc_set_cache_threshold(state->avc, new_value); 1540b77a493bSEric Paris 15411da177e4SLinus Torvalds ret = count; 15421da177e4SLinus Torvalds out: 15438365a719SAl Viro kfree(page); 15441da177e4SLinus Torvalds return ret; 15451da177e4SLinus Torvalds } 15461da177e4SLinus Torvalds 15471da177e4SLinus Torvalds static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf, 15481da177e4SLinus Torvalds size_t count, loff_t *ppos) 15491da177e4SLinus Torvalds { 15506b6bc620SStephen Smalley struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info; 15516b6bc620SStephen Smalley struct selinux_state *state = fsi->state; 15521da177e4SLinus Torvalds char *page; 1553b77a493bSEric Paris ssize_t length; 15541da177e4SLinus Torvalds 15551da177e4SLinus Torvalds page = (char *)__get_free_page(GFP_KERNEL); 1556b77a493bSEric Paris if (!page) 1557b77a493bSEric Paris return -ENOMEM; 1558b77a493bSEric Paris 15596b6bc620SStephen Smalley length = avc_get_hash_stats(state->avc, page); 1560b77a493bSEric Paris if (length >= 0) 1561b77a493bSEric Paris length = simple_read_from_buffer(buf, count, ppos, page, length); 15621da177e4SLinus Torvalds free_page((unsigned long)page); 1563b77a493bSEric Paris 1564b77a493bSEric Paris return length; 15651da177e4SLinus Torvalds } 15661da177e4SLinus Torvalds 156766f8e2f0SJeff Vander Stoep static ssize_t sel_read_sidtab_hash_stats(struct file *filp, char __user *buf, 156866f8e2f0SJeff Vander Stoep size_t count, loff_t *ppos) 156966f8e2f0SJeff Vander Stoep { 157066f8e2f0SJeff Vander Stoep struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info; 157166f8e2f0SJeff Vander Stoep struct selinux_state *state = fsi->state; 157266f8e2f0SJeff Vander Stoep char *page; 157366f8e2f0SJeff Vander Stoep ssize_t length; 157466f8e2f0SJeff Vander Stoep 157566f8e2f0SJeff Vander Stoep page = (char *)__get_free_page(GFP_KERNEL); 157666f8e2f0SJeff Vander Stoep if (!page) 157766f8e2f0SJeff Vander Stoep return -ENOMEM; 157866f8e2f0SJeff Vander Stoep 157966f8e2f0SJeff Vander Stoep length = security_sidtab_hash_stats(state, page); 158066f8e2f0SJeff Vander Stoep if (length >= 0) 158166f8e2f0SJeff Vander Stoep length = simple_read_from_buffer(buf, count, ppos, page, 158266f8e2f0SJeff Vander Stoep length); 158366f8e2f0SJeff Vander Stoep free_page((unsigned long)page); 158466f8e2f0SJeff Vander Stoep 158566f8e2f0SJeff Vander Stoep return length; 158666f8e2f0SJeff Vander Stoep } 158766f8e2f0SJeff Vander Stoep 158866f8e2f0SJeff Vander Stoep static const struct file_operations sel_sidtab_hash_stats_ops = { 158966f8e2f0SJeff Vander Stoep .read = sel_read_sidtab_hash_stats, 159066f8e2f0SJeff Vander Stoep .llseek = generic_file_llseek, 159166f8e2f0SJeff Vander Stoep }; 159266f8e2f0SJeff Vander Stoep 15939c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_threshold_ops = { 15941da177e4SLinus Torvalds .read = sel_read_avc_cache_threshold, 15951da177e4SLinus Torvalds .write = sel_write_avc_cache_threshold, 159657a62c23SArnd Bergmann .llseek = generic_file_llseek, 15971da177e4SLinus Torvalds }; 15981da177e4SLinus Torvalds 15999c2e08c5SArjan van de Ven static const struct file_operations sel_avc_hash_stats_ops = { 16001da177e4SLinus Torvalds .read = sel_read_avc_hash_stats, 160157a62c23SArnd Bergmann .llseek = generic_file_llseek, 16021da177e4SLinus Torvalds }; 16031da177e4SLinus Torvalds 16041da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS 16051da177e4SLinus Torvalds static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx) 16061da177e4SLinus Torvalds { 16071da177e4SLinus Torvalds int cpu; 16081da177e4SLinus Torvalds 16094f4b6c1aSRusty Russell for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) { 16101da177e4SLinus Torvalds if (!cpu_possible(cpu)) 16111da177e4SLinus Torvalds continue; 16121da177e4SLinus Torvalds *idx = cpu + 1; 16131da177e4SLinus Torvalds return &per_cpu(avc_cache_stats, cpu); 16141da177e4SLinus Torvalds } 16158d269a8eSVasily Averin (*idx)++; 16161da177e4SLinus Torvalds return NULL; 16171da177e4SLinus Torvalds } 16181da177e4SLinus Torvalds 16191da177e4SLinus Torvalds static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos) 16201da177e4SLinus Torvalds { 16211da177e4SLinus Torvalds loff_t n = *pos - 1; 16221da177e4SLinus Torvalds 16231da177e4SLinus Torvalds if (*pos == 0) 16241da177e4SLinus Torvalds return SEQ_START_TOKEN; 16251da177e4SLinus Torvalds 16261da177e4SLinus Torvalds return sel_avc_get_stat_idx(&n); 16271da177e4SLinus Torvalds } 16281da177e4SLinus Torvalds 16291da177e4SLinus Torvalds static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos) 16301da177e4SLinus Torvalds { 16311da177e4SLinus Torvalds return sel_avc_get_stat_idx(pos); 16321da177e4SLinus Torvalds } 16331da177e4SLinus Torvalds 16341da177e4SLinus Torvalds static int sel_avc_stats_seq_show(struct seq_file *seq, void *v) 16351da177e4SLinus Torvalds { 16361da177e4SLinus Torvalds struct avc_cache_stats *st = v; 16371da177e4SLinus Torvalds 1638710a0647SMarkus Elfring if (v == SEQ_START_TOKEN) { 1639710a0647SMarkus Elfring seq_puts(seq, 1640710a0647SMarkus Elfring "lookups hits misses allocations reclaims frees\n"); 1641710a0647SMarkus Elfring } else { 1642257313b2SLinus Torvalds unsigned int lookups = st->lookups; 1643257313b2SLinus Torvalds unsigned int misses = st->misses; 1644257313b2SLinus Torvalds unsigned int hits = lookups - misses; 1645257313b2SLinus Torvalds seq_printf(seq, "%u %u %u %u %u %u\n", lookups, 1646257313b2SLinus Torvalds hits, misses, st->allocations, 16471da177e4SLinus Torvalds st->reclaims, st->frees); 1648257313b2SLinus Torvalds } 16491da177e4SLinus Torvalds return 0; 16501da177e4SLinus Torvalds } 16511da177e4SLinus Torvalds 16521da177e4SLinus Torvalds static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v) 16531da177e4SLinus Torvalds { } 16541da177e4SLinus Torvalds 16551996a109SJan Engelhardt static const struct seq_operations sel_avc_cache_stats_seq_ops = { 16561da177e4SLinus Torvalds .start = sel_avc_stats_seq_start, 16571da177e4SLinus Torvalds .next = sel_avc_stats_seq_next, 16581da177e4SLinus Torvalds .show = sel_avc_stats_seq_show, 16591da177e4SLinus Torvalds .stop = sel_avc_stats_seq_stop, 16601da177e4SLinus Torvalds }; 16611da177e4SLinus Torvalds 16621da177e4SLinus Torvalds static int sel_open_avc_cache_stats(struct inode *inode, struct file *file) 16631da177e4SLinus Torvalds { 16641da177e4SLinus Torvalds return seq_open(file, &sel_avc_cache_stats_seq_ops); 16651da177e4SLinus Torvalds } 16661da177e4SLinus Torvalds 16679c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_stats_ops = { 16681da177e4SLinus Torvalds .open = sel_open_avc_cache_stats, 16691da177e4SLinus Torvalds .read = seq_read, 16701da177e4SLinus Torvalds .llseek = seq_lseek, 16711da177e4SLinus Torvalds .release = seq_release, 16721da177e4SLinus Torvalds }; 16731da177e4SLinus Torvalds #endif 16741da177e4SLinus Torvalds 16751da177e4SLinus Torvalds static int sel_make_avc_files(struct dentry *dir) 16761da177e4SLinus Torvalds { 16770619f0f5SStephen Smalley struct super_block *sb = dir->d_sb; 16780619f0f5SStephen Smalley struct selinux_fs_info *fsi = sb->s_fs_info; 1679b77a493bSEric Paris int i; 1680cda37124SEric Biggers static const struct tree_descr files[] = { 16811da177e4SLinus Torvalds { "cache_threshold", 16821da177e4SLinus Torvalds &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR }, 16831da177e4SLinus Torvalds { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO }, 16841da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS 16851da177e4SLinus Torvalds { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO }, 16861da177e4SLinus Torvalds #endif 16871da177e4SLinus Torvalds }; 16881da177e4SLinus Torvalds 16896e20a64aSNicolas Kaiser for (i = 0; i < ARRAY_SIZE(files); i++) { 16901da177e4SLinus Torvalds struct inode *inode; 16911da177e4SLinus Torvalds struct dentry *dentry; 16921da177e4SLinus Torvalds 16931da177e4SLinus Torvalds dentry = d_alloc_name(dir, files[i].name); 1694b77a493bSEric Paris if (!dentry) 1695b77a493bSEric Paris return -ENOMEM; 16961da177e4SLinus Torvalds 16971da177e4SLinus Torvalds inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode); 16987e4237faSnixiaoming if (!inode) { 16997e4237faSnixiaoming dput(dentry); 1700b77a493bSEric Paris return -ENOMEM; 17017e4237faSnixiaoming } 1702b77a493bSEric Paris 17031da177e4SLinus Torvalds inode->i_fop = files[i].ops; 17040619f0f5SStephen Smalley inode->i_ino = ++fsi->last_ino; 17051da177e4SLinus Torvalds d_add(dentry, inode); 17061da177e4SLinus Torvalds } 1707b77a493bSEric Paris 1708b77a493bSEric Paris return 0; 17091da177e4SLinus Torvalds } 17101da177e4SLinus Torvalds 171166f8e2f0SJeff Vander Stoep static int sel_make_ss_files(struct dentry *dir) 171266f8e2f0SJeff Vander Stoep { 171366f8e2f0SJeff Vander Stoep struct super_block *sb = dir->d_sb; 171466f8e2f0SJeff Vander Stoep struct selinux_fs_info *fsi = sb->s_fs_info; 171566f8e2f0SJeff Vander Stoep int i; 171666f8e2f0SJeff Vander Stoep static struct tree_descr files[] = { 171766f8e2f0SJeff Vander Stoep { "sidtab_hash_stats", &sel_sidtab_hash_stats_ops, S_IRUGO }, 171866f8e2f0SJeff Vander Stoep }; 171966f8e2f0SJeff Vander Stoep 172066f8e2f0SJeff Vander Stoep for (i = 0; i < ARRAY_SIZE(files); i++) { 172166f8e2f0SJeff Vander Stoep struct inode *inode; 172266f8e2f0SJeff Vander Stoep struct dentry *dentry; 172366f8e2f0SJeff Vander Stoep 172466f8e2f0SJeff Vander Stoep dentry = d_alloc_name(dir, files[i].name); 172566f8e2f0SJeff Vander Stoep if (!dentry) 172666f8e2f0SJeff Vander Stoep return -ENOMEM; 172766f8e2f0SJeff Vander Stoep 172866f8e2f0SJeff Vander Stoep inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode); 172966f8e2f0SJeff Vander Stoep if (!inode) { 173066f8e2f0SJeff Vander Stoep dput(dentry); 173166f8e2f0SJeff Vander Stoep return -ENOMEM; 173266f8e2f0SJeff Vander Stoep } 173366f8e2f0SJeff Vander Stoep 173466f8e2f0SJeff Vander Stoep inode->i_fop = files[i].ops; 173566f8e2f0SJeff Vander Stoep inode->i_ino = ++fsi->last_ino; 173666f8e2f0SJeff Vander Stoep d_add(dentry, inode); 173766f8e2f0SJeff Vander Stoep } 173866f8e2f0SJeff Vander Stoep 173966f8e2f0SJeff Vander Stoep return 0; 174066f8e2f0SJeff Vander Stoep } 174166f8e2f0SJeff Vander Stoep 1742f0ee2e46SJames Carter static ssize_t sel_read_initcon(struct file *file, char __user *buf, 1743f0ee2e46SJames Carter size_t count, loff_t *ppos) 1744f0ee2e46SJames Carter { 17450619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 1746f0ee2e46SJames Carter char *con; 1747f0ee2e46SJames Carter u32 sid, len; 1748f0ee2e46SJames Carter ssize_t ret; 1749f0ee2e46SJames Carter 1750496ad9aaSAl Viro sid = file_inode(file)->i_ino&SEL_INO_MASK; 17510619f0f5SStephen Smalley ret = security_sid_to_context(fsi->state, sid, &con, &len); 1752b77a493bSEric Paris if (ret) 1753f0ee2e46SJames Carter return ret; 1754f0ee2e46SJames Carter 1755f0ee2e46SJames Carter ret = simple_read_from_buffer(buf, count, ppos, con, len); 1756f0ee2e46SJames Carter kfree(con); 1757f0ee2e46SJames Carter return ret; 1758f0ee2e46SJames Carter } 1759f0ee2e46SJames Carter 1760f0ee2e46SJames Carter static const struct file_operations sel_initcon_ops = { 1761f0ee2e46SJames Carter .read = sel_read_initcon, 176257a62c23SArnd Bergmann .llseek = generic_file_llseek, 1763f0ee2e46SJames Carter }; 1764f0ee2e46SJames Carter 1765f0ee2e46SJames Carter static int sel_make_initcon_files(struct dentry *dir) 1766f0ee2e46SJames Carter { 1767b77a493bSEric Paris int i; 1768f0ee2e46SJames Carter 1769f0ee2e46SJames Carter for (i = 1; i <= SECINITSID_NUM; i++) { 1770f0ee2e46SJames Carter struct inode *inode; 1771f0ee2e46SJames Carter struct dentry *dentry; 1772e3e0b582SStephen Smalley const char *s = security_get_initial_sid_context(i); 1773e3e0b582SStephen Smalley 1774e3e0b582SStephen Smalley if (!s) 1775e3e0b582SStephen Smalley continue; 1776e3e0b582SStephen Smalley dentry = d_alloc_name(dir, s); 1777b77a493bSEric Paris if (!dentry) 1778b77a493bSEric Paris return -ENOMEM; 1779f0ee2e46SJames Carter 1780f0ee2e46SJames Carter inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); 17817e4237faSnixiaoming if (!inode) { 17827e4237faSnixiaoming dput(dentry); 1783b77a493bSEric Paris return -ENOMEM; 17847e4237faSnixiaoming } 1785b77a493bSEric Paris 1786f0ee2e46SJames Carter inode->i_fop = &sel_initcon_ops; 1787f0ee2e46SJames Carter inode->i_ino = i|SEL_INITCON_INO_OFFSET; 1788f0ee2e46SJames Carter d_add(dentry, inode); 1789f0ee2e46SJames Carter } 1790b77a493bSEric Paris 1791b77a493bSEric Paris return 0; 1792f0ee2e46SJames Carter } 1793f0ee2e46SJames Carter 1794e47c8fc5SChristopher J. PeBenito static inline unsigned long sel_class_to_ino(u16 class) 1795e47c8fc5SChristopher J. PeBenito { 1796e47c8fc5SChristopher J. PeBenito return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET; 1797e47c8fc5SChristopher J. PeBenito } 1798e47c8fc5SChristopher J. PeBenito 1799e47c8fc5SChristopher J. PeBenito static inline u16 sel_ino_to_class(unsigned long ino) 1800e47c8fc5SChristopher J. PeBenito { 180192ae9e82SEric Paris return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1); 1802e47c8fc5SChristopher J. PeBenito } 1803e47c8fc5SChristopher J. PeBenito 1804e47c8fc5SChristopher J. PeBenito static inline unsigned long sel_perm_to_ino(u16 class, u32 perm) 1805e47c8fc5SChristopher J. PeBenito { 1806e47c8fc5SChristopher J. PeBenito return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET; 1807e47c8fc5SChristopher J. PeBenito } 1808e47c8fc5SChristopher J. PeBenito 1809e47c8fc5SChristopher J. PeBenito static inline u32 sel_ino_to_perm(unsigned long ino) 1810e47c8fc5SChristopher J. PeBenito { 1811e47c8fc5SChristopher J. PeBenito return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1); 1812e47c8fc5SChristopher J. PeBenito } 1813e47c8fc5SChristopher J. PeBenito 1814e47c8fc5SChristopher J. PeBenito static ssize_t sel_read_class(struct file *file, char __user *buf, 1815e47c8fc5SChristopher J. PeBenito size_t count, loff_t *ppos) 1816e47c8fc5SChristopher J. PeBenito { 1817496ad9aaSAl Viro unsigned long ino = file_inode(file)->i_ino; 1818cc1dad71SAl Viro char res[TMPBUFLEN]; 18197e78c875Sliuyang34 ssize_t len = scnprintf(res, sizeof(res), "%d", sel_ino_to_class(ino)); 1820cc1dad71SAl Viro return simple_read_from_buffer(buf, count, ppos, res, len); 1821e47c8fc5SChristopher J. PeBenito } 1822e47c8fc5SChristopher J. PeBenito 1823e47c8fc5SChristopher J. PeBenito static const struct file_operations sel_class_ops = { 1824e47c8fc5SChristopher J. PeBenito .read = sel_read_class, 182557a62c23SArnd Bergmann .llseek = generic_file_llseek, 1826e47c8fc5SChristopher J. PeBenito }; 1827e47c8fc5SChristopher J. PeBenito 1828e47c8fc5SChristopher J. PeBenito static ssize_t sel_read_perm(struct file *file, char __user *buf, 1829e47c8fc5SChristopher J. PeBenito size_t count, loff_t *ppos) 1830e47c8fc5SChristopher J. PeBenito { 1831496ad9aaSAl Viro unsigned long ino = file_inode(file)->i_ino; 1832cc1dad71SAl Viro char res[TMPBUFLEN]; 18337e78c875Sliuyang34 ssize_t len = scnprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino)); 1834cc1dad71SAl Viro return simple_read_from_buffer(buf, count, ppos, res, len); 1835e47c8fc5SChristopher J. PeBenito } 1836e47c8fc5SChristopher J. PeBenito 1837e47c8fc5SChristopher J. PeBenito static const struct file_operations sel_perm_ops = { 1838e47c8fc5SChristopher J. PeBenito .read = sel_read_perm, 183957a62c23SArnd Bergmann .llseek = generic_file_llseek, 1840e47c8fc5SChristopher J. PeBenito }; 1841e47c8fc5SChristopher J. PeBenito 18423bb56b25SPaul Moore static ssize_t sel_read_policycap(struct file *file, char __user *buf, 18433bb56b25SPaul Moore size_t count, loff_t *ppos) 18443bb56b25SPaul Moore { 18450619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info; 18463bb56b25SPaul Moore int value; 18473bb56b25SPaul Moore char tmpbuf[TMPBUFLEN]; 18483bb56b25SPaul Moore ssize_t length; 1849496ad9aaSAl Viro unsigned long i_ino = file_inode(file)->i_ino; 18503bb56b25SPaul Moore 18510619f0f5SStephen Smalley value = security_policycap_supported(fsi->state, i_ino & SEL_INO_MASK); 18523bb56b25SPaul Moore length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value); 18533bb56b25SPaul Moore 18543bb56b25SPaul Moore return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 18553bb56b25SPaul Moore } 18563bb56b25SPaul Moore 18573bb56b25SPaul Moore static const struct file_operations sel_policycap_ops = { 18583bb56b25SPaul Moore .read = sel_read_policycap, 185957a62c23SArnd Bergmann .llseek = generic_file_llseek, 18603bb56b25SPaul Moore }; 18613bb56b25SPaul Moore 186202a52c5cSStephen Smalley static int sel_make_perm_files(struct selinux_policy *newpolicy, 186302a52c5cSStephen Smalley char *objclass, int classvalue, 1864e47c8fc5SChristopher J. PeBenito struct dentry *dir) 1865e47c8fc5SChristopher J. PeBenito { 1866b77a493bSEric Paris int i, rc, nperms; 1867e47c8fc5SChristopher J. PeBenito char **perms; 1868e47c8fc5SChristopher J. PeBenito 186902a52c5cSStephen Smalley rc = security_get_permissions(newpolicy, objclass, &perms, &nperms); 1870e47c8fc5SChristopher J. PeBenito if (rc) 1871b77a493bSEric Paris return rc; 1872e47c8fc5SChristopher J. PeBenito 1873e47c8fc5SChristopher J. PeBenito for (i = 0; i < nperms; i++) { 1874e47c8fc5SChristopher J. PeBenito struct inode *inode; 1875e47c8fc5SChristopher J. PeBenito struct dentry *dentry; 1876e47c8fc5SChristopher J. PeBenito 1877b77a493bSEric Paris rc = -ENOMEM; 1878e47c8fc5SChristopher J. PeBenito dentry = d_alloc_name(dir, perms[i]); 1879b77a493bSEric Paris if (!dentry) 1880b77a493bSEric Paris goto out; 1881e47c8fc5SChristopher J. PeBenito 1882e47c8fc5SChristopher J. PeBenito rc = -ENOMEM; 1883b77a493bSEric Paris inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); 18847e4237faSnixiaoming if (!inode) { 18857e4237faSnixiaoming dput(dentry); 1886b77a493bSEric Paris goto out; 18877e4237faSnixiaoming } 1888b77a493bSEric Paris 1889e47c8fc5SChristopher J. PeBenito inode->i_fop = &sel_perm_ops; 1890e47c8fc5SChristopher J. PeBenito /* i+1 since perm values are 1-indexed */ 1891e47c8fc5SChristopher J. PeBenito inode->i_ino = sel_perm_to_ino(classvalue, i + 1); 1892e47c8fc5SChristopher J. PeBenito d_add(dentry, inode); 1893e47c8fc5SChristopher J. PeBenito } 1894b77a493bSEric Paris rc = 0; 1895b77a493bSEric Paris out: 1896e47c8fc5SChristopher J. PeBenito for (i = 0; i < nperms; i++) 1897e47c8fc5SChristopher J. PeBenito kfree(perms[i]); 1898e47c8fc5SChristopher J. PeBenito kfree(perms); 1899e47c8fc5SChristopher J. PeBenito return rc; 1900e47c8fc5SChristopher J. PeBenito } 1901e47c8fc5SChristopher J. PeBenito 190202a52c5cSStephen Smalley static int sel_make_class_dir_entries(struct selinux_policy *newpolicy, 190302a52c5cSStephen Smalley char *classname, int index, 1904e47c8fc5SChristopher J. PeBenito struct dentry *dir) 1905e47c8fc5SChristopher J. PeBenito { 19060619f0f5SStephen Smalley struct super_block *sb = dir->d_sb; 19070619f0f5SStephen Smalley struct selinux_fs_info *fsi = sb->s_fs_info; 1908e47c8fc5SChristopher J. PeBenito struct dentry *dentry = NULL; 1909e47c8fc5SChristopher J. PeBenito struct inode *inode = NULL; 1910e47c8fc5SChristopher J. PeBenito int rc; 1911e47c8fc5SChristopher J. PeBenito 1912e47c8fc5SChristopher J. PeBenito dentry = d_alloc_name(dir, "index"); 1913b77a493bSEric Paris if (!dentry) 1914b77a493bSEric Paris return -ENOMEM; 1915e47c8fc5SChristopher J. PeBenito 1916e47c8fc5SChristopher J. PeBenito inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO); 19177e4237faSnixiaoming if (!inode) { 19187e4237faSnixiaoming dput(dentry); 1919b77a493bSEric Paris return -ENOMEM; 19207e4237faSnixiaoming } 1921e47c8fc5SChristopher J. PeBenito 1922e47c8fc5SChristopher J. PeBenito inode->i_fop = &sel_class_ops; 1923e47c8fc5SChristopher J. PeBenito inode->i_ino = sel_class_to_ino(index); 1924e47c8fc5SChristopher J. PeBenito d_add(dentry, inode); 1925e47c8fc5SChristopher J. PeBenito 19260619f0f5SStephen Smalley dentry = sel_make_dir(dir, "perms", &fsi->last_class_ino); 1927a1c2aa1eSAl Viro if (IS_ERR(dentry)) 1928a1c2aa1eSAl Viro return PTR_ERR(dentry); 1929e47c8fc5SChristopher J. PeBenito 193002a52c5cSStephen Smalley rc = sel_make_perm_files(newpolicy, classname, index, dentry); 1931e47c8fc5SChristopher J. PeBenito 1932e47c8fc5SChristopher J. PeBenito return rc; 1933e47c8fc5SChristopher J. PeBenito } 1934e47c8fc5SChristopher J. PeBenito 193566ec384aSDaniel Burgener static int sel_make_classes(struct selinux_policy *newpolicy, 193666ec384aSDaniel Burgener struct dentry *class_dir, 193766ec384aSDaniel Burgener unsigned long *last_class_ino) 1938e47c8fc5SChristopher J. PeBenito { 19390619f0f5SStephen Smalley 1940b77a493bSEric Paris int rc, nclasses, i; 1941e47c8fc5SChristopher J. PeBenito char **classes; 1942e47c8fc5SChristopher J. PeBenito 194302a52c5cSStephen Smalley rc = security_get_classes(newpolicy, &classes, &nclasses); 1944b77a493bSEric Paris if (rc) 1945b77a493bSEric Paris return rc; 1946e47c8fc5SChristopher J. PeBenito 1947e47c8fc5SChristopher J. PeBenito /* +2 since classes are 1-indexed */ 194866ec384aSDaniel Burgener *last_class_ino = sel_class_to_ino(nclasses + 2); 1949e47c8fc5SChristopher J. PeBenito 1950e47c8fc5SChristopher J. PeBenito for (i = 0; i < nclasses; i++) { 1951e47c8fc5SChristopher J. PeBenito struct dentry *class_name_dir; 1952e47c8fc5SChristopher J. PeBenito 195366ec384aSDaniel Burgener class_name_dir = sel_make_dir(class_dir, classes[i], 195466ec384aSDaniel Burgener last_class_ino); 1955a1c2aa1eSAl Viro if (IS_ERR(class_name_dir)) { 1956a1c2aa1eSAl Viro rc = PTR_ERR(class_name_dir); 1957b77a493bSEric Paris goto out; 1958a1c2aa1eSAl Viro } 1959e47c8fc5SChristopher J. PeBenito 1960e47c8fc5SChristopher J. PeBenito /* i+1 since class values are 1-indexed */ 196102a52c5cSStephen Smalley rc = sel_make_class_dir_entries(newpolicy, classes[i], i + 1, 1962e47c8fc5SChristopher J. PeBenito class_name_dir); 1963e47c8fc5SChristopher J. PeBenito if (rc) 1964b77a493bSEric Paris goto out; 1965e47c8fc5SChristopher J. PeBenito } 1966b77a493bSEric Paris rc = 0; 1967b77a493bSEric Paris out: 1968e47c8fc5SChristopher J. PeBenito for (i = 0; i < nclasses; i++) 1969e47c8fc5SChristopher J. PeBenito kfree(classes[i]); 1970e47c8fc5SChristopher J. PeBenito kfree(classes); 1971e47c8fc5SChristopher J. PeBenito return rc; 1972e47c8fc5SChristopher J. PeBenito } 1973e47c8fc5SChristopher J. PeBenito 19740619f0f5SStephen Smalley static int sel_make_policycap(struct selinux_fs_info *fsi) 19753bb56b25SPaul Moore { 19763bb56b25SPaul Moore unsigned int iter; 19773bb56b25SPaul Moore struct dentry *dentry = NULL; 19783bb56b25SPaul Moore struct inode *inode = NULL; 19793bb56b25SPaul Moore 19803bb56b25SPaul Moore for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) { 19814dc2fce3SStephen Smalley if (iter < ARRAY_SIZE(selinux_policycap_names)) 19820619f0f5SStephen Smalley dentry = d_alloc_name(fsi->policycap_dir, 19834dc2fce3SStephen Smalley selinux_policycap_names[iter]); 19843bb56b25SPaul Moore else 19850619f0f5SStephen Smalley dentry = d_alloc_name(fsi->policycap_dir, "unknown"); 19863bb56b25SPaul Moore 19873bb56b25SPaul Moore if (dentry == NULL) 19883bb56b25SPaul Moore return -ENOMEM; 19893bb56b25SPaul Moore 19900619f0f5SStephen Smalley inode = sel_make_inode(fsi->sb, S_IFREG | 0444); 19917e4237faSnixiaoming if (inode == NULL) { 19927e4237faSnixiaoming dput(dentry); 19933bb56b25SPaul Moore return -ENOMEM; 19947e4237faSnixiaoming } 19953bb56b25SPaul Moore 19963bb56b25SPaul Moore inode->i_fop = &sel_policycap_ops; 19973bb56b25SPaul Moore inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET; 19983bb56b25SPaul Moore d_add(dentry, inode); 19993bb56b25SPaul Moore } 20003bb56b25SPaul Moore 20013bb56b25SPaul Moore return 0; 20023bb56b25SPaul Moore } 20033bb56b25SPaul Moore 2004a1c2aa1eSAl Viro static struct dentry *sel_make_dir(struct dentry *dir, const char *name, 20050dd4ae51SChristopher J. PeBenito unsigned long *ino) 20061da177e4SLinus Torvalds { 2007a1c2aa1eSAl Viro struct dentry *dentry = d_alloc_name(dir, name); 20081da177e4SLinus Torvalds struct inode *inode; 20091da177e4SLinus Torvalds 2010a1c2aa1eSAl Viro if (!dentry) 2011a1c2aa1eSAl Viro return ERR_PTR(-ENOMEM); 2012a1c2aa1eSAl Viro 2013a1c2aa1eSAl Viro inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO); 2014a1c2aa1eSAl Viro if (!inode) { 2015a1c2aa1eSAl Viro dput(dentry); 2016a1c2aa1eSAl Viro return ERR_PTR(-ENOMEM); 2017a1c2aa1eSAl Viro } 2018b77a493bSEric Paris 20191da177e4SLinus Torvalds inode->i_op = &simple_dir_inode_operations; 20201da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations; 20210dd4ae51SChristopher J. PeBenito inode->i_ino = ++(*ino); 202240e906f8SJames Morris /* directory inodes start off with i_nlink == 2 (for "." entry) */ 2023d8c76e6fSDave Hansen inc_nlink(inode); 20241da177e4SLinus Torvalds d_add(dentry, inode); 2025edb20fb5SJames Morris /* bump link count on parent directory, too */ 2026ce0b16ddSDavid Howells inc_nlink(d_inode(dir)); 2027b77a493bSEric Paris 2028a1c2aa1eSAl Viro return dentry; 20291da177e4SLinus Torvalds } 20301da177e4SLinus Torvalds 20310eea6091SDaniel Burgener static struct dentry *sel_make_disconnected_dir(struct super_block *sb, 20320eea6091SDaniel Burgener unsigned long *ino) 20330eea6091SDaniel Burgener { 20340eea6091SDaniel Burgener struct inode *inode = sel_make_inode(sb, S_IFDIR | S_IRUGO | S_IXUGO); 20350eea6091SDaniel Burgener 20360eea6091SDaniel Burgener if (!inode) 20370eea6091SDaniel Burgener return ERR_PTR(-ENOMEM); 20380eea6091SDaniel Burgener 20390eea6091SDaniel Burgener inode->i_op = &simple_dir_inode_operations; 20400eea6091SDaniel Burgener inode->i_fop = &simple_dir_operations; 20410eea6091SDaniel Burgener inode->i_ino = ++(*ino); 20420eea6091SDaniel Burgener /* directory inodes start off with i_nlink == 2 (for "." entry) */ 20430eea6091SDaniel Burgener inc_nlink(inode); 20440eea6091SDaniel Burgener return d_obtain_alias(inode); 20450eea6091SDaniel Burgener } 20460eea6091SDaniel Burgener 20470619f0f5SStephen Smalley #define NULL_FILE_NAME "null" 20480619f0f5SStephen Smalley 2049920f50b2SDavid Howells static int sel_fill_super(struct super_block *sb, struct fs_context *fc) 20501da177e4SLinus Torvalds { 20510619f0f5SStephen Smalley struct selinux_fs_info *fsi; 20521da177e4SLinus Torvalds int ret; 20531da177e4SLinus Torvalds struct dentry *dentry; 2054a1c2aa1eSAl Viro struct inode *inode; 20551da177e4SLinus Torvalds struct inode_security_struct *isec; 20561da177e4SLinus Torvalds 2057cda37124SEric Biggers static const struct tree_descr selinux_files[] = { 20581da177e4SLinus Torvalds [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR}, 20591da177e4SLinus Torvalds [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR}, 2060ce9982d0SStephen Smalley [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO}, 20611da177e4SLinus Torvalds [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO}, 20621da177e4SLinus Torvalds [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO}, 20631da177e4SLinus Torvalds [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO}, 20641da177e4SLinus Torvalds [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO}, 20651da177e4SLinus Torvalds [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO}, 20661da177e4SLinus Torvalds [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR}, 20671da177e4SLinus Torvalds [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO}, 20681da177e4SLinus Torvalds [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR}, 20691da177e4SLinus Torvalds [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO}, 20701da177e4SLinus Torvalds [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR}, 20713f12070eSEric Paris [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO}, 20723f12070eSEric Paris [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO}, 207311904167SKaiGai Kohei [SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO}, 207472e8c859SEric Paris [SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO}, 2075f9df6458SAndrew Perepechko [SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops, 2076f9df6458SAndrew Perepechko S_IWUGO}, 20771da177e4SLinus Torvalds /* last one */ {""} 20781da177e4SLinus Torvalds }; 20790619f0f5SStephen Smalley 20800619f0f5SStephen Smalley ret = selinux_fs_info_create(sb); 20810619f0f5SStephen Smalley if (ret) 20820619f0f5SStephen Smalley goto err; 20830619f0f5SStephen Smalley 20841da177e4SLinus Torvalds ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files); 20851da177e4SLinus Torvalds if (ret) 2086161ce45aSJames Morris goto err; 20871da177e4SLinus Torvalds 20880619f0f5SStephen Smalley fsi = sb->s_fs_info; 20890619f0f5SStephen Smalley fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino); 20900619f0f5SStephen Smalley if (IS_ERR(fsi->bool_dir)) { 20910619f0f5SStephen Smalley ret = PTR_ERR(fsi->bool_dir); 20920619f0f5SStephen Smalley fsi->bool_dir = NULL; 2093161ce45aSJames Morris goto err; 2094a1c2aa1eSAl Viro } 20951da177e4SLinus Torvalds 2096b77a493bSEric Paris ret = -ENOMEM; 20971da177e4SLinus Torvalds dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME); 2098b77a493bSEric Paris if (!dentry) 2099161ce45aSJames Morris goto err; 21001da177e4SLinus Torvalds 2101161ce45aSJames Morris ret = -ENOMEM; 2102b77a493bSEric Paris inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO); 21037e4237faSnixiaoming if (!inode) { 21047e4237faSnixiaoming dput(dentry); 2105161ce45aSJames Morris goto err; 21067e4237faSnixiaoming } 2107b77a493bSEric Paris 21080619f0f5SStephen Smalley inode->i_ino = ++fsi->last_ino; 210980788c22SCasey Schaufler isec = selinux_inode(inode); 21101da177e4SLinus Torvalds isec->sid = SECINITSID_DEVNULL; 21111da177e4SLinus Torvalds isec->sclass = SECCLASS_CHR_FILE; 211242059112SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED; 21131da177e4SLinus Torvalds 21141da177e4SLinus Torvalds init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3)); 21151da177e4SLinus Torvalds d_add(dentry, inode); 21161da177e4SLinus Torvalds 21170619f0f5SStephen Smalley dentry = sel_make_dir(sb->s_root, "avc", &fsi->last_ino); 2118a1c2aa1eSAl Viro if (IS_ERR(dentry)) { 2119a1c2aa1eSAl Viro ret = PTR_ERR(dentry); 2120161ce45aSJames Morris goto err; 2121a1c2aa1eSAl Viro } 21221da177e4SLinus Torvalds 21231da177e4SLinus Torvalds ret = sel_make_avc_files(dentry); 212466f8e2f0SJeff Vander Stoep 212566f8e2f0SJeff Vander Stoep dentry = sel_make_dir(sb->s_root, "ss", &fsi->last_ino); 212666f8e2f0SJeff Vander Stoep if (IS_ERR(dentry)) { 212766f8e2f0SJeff Vander Stoep ret = PTR_ERR(dentry); 212866f8e2f0SJeff Vander Stoep goto err; 212966f8e2f0SJeff Vander Stoep } 213066f8e2f0SJeff Vander Stoep 213166f8e2f0SJeff Vander Stoep ret = sel_make_ss_files(dentry); 21321da177e4SLinus Torvalds if (ret) 2133161ce45aSJames Morris goto err; 2134f0ee2e46SJames Carter 21350619f0f5SStephen Smalley dentry = sel_make_dir(sb->s_root, "initial_contexts", &fsi->last_ino); 2136a1c2aa1eSAl Viro if (IS_ERR(dentry)) { 2137a1c2aa1eSAl Viro ret = PTR_ERR(dentry); 2138f0ee2e46SJames Carter goto err; 2139a1c2aa1eSAl Viro } 2140f0ee2e46SJames Carter 2141f0ee2e46SJames Carter ret = sel_make_initcon_files(dentry); 2142f0ee2e46SJames Carter if (ret) 2143f0ee2e46SJames Carter goto err; 2144f0ee2e46SJames Carter 2145613ba187SDaniel Burgener fsi->class_dir = sel_make_dir(sb->s_root, CLASS_DIR_NAME, &fsi->last_ino); 21460619f0f5SStephen Smalley if (IS_ERR(fsi->class_dir)) { 21470619f0f5SStephen Smalley ret = PTR_ERR(fsi->class_dir); 21480619f0f5SStephen Smalley fsi->class_dir = NULL; 2149e47c8fc5SChristopher J. PeBenito goto err; 2150a1c2aa1eSAl Viro } 2151e47c8fc5SChristopher J. PeBenito 2152613ba187SDaniel Burgener fsi->policycap_dir = sel_make_dir(sb->s_root, POLICYCAP_DIR_NAME, 21530619f0f5SStephen Smalley &fsi->last_ino); 21540619f0f5SStephen Smalley if (IS_ERR(fsi->policycap_dir)) { 21550619f0f5SStephen Smalley ret = PTR_ERR(fsi->policycap_dir); 21560619f0f5SStephen Smalley fsi->policycap_dir = NULL; 2157e47c8fc5SChristopher J. PeBenito goto err; 2158a1c2aa1eSAl Viro } 21590619f0f5SStephen Smalley 216002a52c5cSStephen Smalley ret = sel_make_policycap(fsi); 216102a52c5cSStephen Smalley if (ret) { 216202a52c5cSStephen Smalley pr_err("SELinux: failed to load policy capabilities\n"); 21630619f0f5SStephen Smalley goto err; 216402a52c5cSStephen Smalley } 216502a52c5cSStephen Smalley 2166b77a493bSEric Paris return 0; 2167161ce45aSJames Morris err: 2168f8b69a5fSpeter enderborg pr_err("SELinux: %s: failed while creating inodes\n", 2169744ba35eSEric Paris __func__); 21700619f0f5SStephen Smalley 21710619f0f5SStephen Smalley selinux_fs_info_free(sb); 21720619f0f5SStephen Smalley 2173b77a493bSEric Paris return ret; 21741da177e4SLinus Torvalds } 21751da177e4SLinus Torvalds 2176920f50b2SDavid Howells static int sel_get_tree(struct fs_context *fc) 21771da177e4SLinus Torvalds { 2178920f50b2SDavid Howells return get_tree_single(fc, sel_fill_super); 2179920f50b2SDavid Howells } 2180920f50b2SDavid Howells 2181920f50b2SDavid Howells static const struct fs_context_operations sel_context_ops = { 2182920f50b2SDavid Howells .get_tree = sel_get_tree, 2183920f50b2SDavid Howells }; 2184920f50b2SDavid Howells 2185920f50b2SDavid Howells static int sel_init_fs_context(struct fs_context *fc) 2186920f50b2SDavid Howells { 2187920f50b2SDavid Howells fc->ops = &sel_context_ops; 2188920f50b2SDavid Howells return 0; 21891da177e4SLinus Torvalds } 21901da177e4SLinus Torvalds 21910619f0f5SStephen Smalley static void sel_kill_sb(struct super_block *sb) 21920619f0f5SStephen Smalley { 21930619f0f5SStephen Smalley selinux_fs_info_free(sb); 21940619f0f5SStephen Smalley kill_litter_super(sb); 21950619f0f5SStephen Smalley } 21960619f0f5SStephen Smalley 21971da177e4SLinus Torvalds static struct file_system_type sel_fs_type = { 21981da177e4SLinus Torvalds .name = "selinuxfs", 2199920f50b2SDavid Howells .init_fs_context = sel_init_fs_context, 22000619f0f5SStephen Smalley .kill_sb = sel_kill_sb, 22011da177e4SLinus Torvalds }; 22021da177e4SLinus Torvalds 2203cd2bb4cbSOndrej Mosnacek static struct vfsmount *selinuxfs_mount __ro_after_init; 2204cd2bb4cbSOndrej Mosnacek struct path selinux_null __ro_after_init; 22051da177e4SLinus Torvalds 22061da177e4SLinus Torvalds static int __init init_sel_fs(void) 22071da177e4SLinus Torvalds { 22080619f0f5SStephen Smalley struct qstr null_name = QSTR_INIT(NULL_FILE_NAME, 22090619f0f5SStephen Smalley sizeof(NULL_FILE_NAME)-1); 22101da177e4SLinus Torvalds int err; 22111da177e4SLinus Torvalds 22126c5a682eSStephen Smalley if (!selinux_enabled_boot) 22131da177e4SLinus Torvalds return 0; 22147a627e3bSGreg Kroah-Hartman 2215f9bb4882SEric W. Biederman err = sysfs_create_mount_point(fs_kobj, "selinux"); 2216f9bb4882SEric W. Biederman if (err) 2217f9bb4882SEric W. Biederman return err; 22187a627e3bSGreg Kroah-Hartman 22191da177e4SLinus Torvalds err = register_filesystem(&sel_fs_type); 22207a627e3bSGreg Kroah-Hartman if (err) { 2221f9bb4882SEric W. Biederman sysfs_remove_mount_point(fs_kobj, "selinux"); 2222b77a493bSEric Paris return err; 22237a627e3bSGreg Kroah-Hartman } 2224b77a493bSEric Paris 2225765927b2SAl Viro selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type); 22261da177e4SLinus Torvalds if (IS_ERR(selinuxfs_mount)) { 2227f8b69a5fSpeter enderborg pr_err("selinuxfs: could not mount!\n"); 22281da177e4SLinus Torvalds err = PTR_ERR(selinuxfs_mount); 22291da177e4SLinus Torvalds selinuxfs_mount = NULL; 22301da177e4SLinus Torvalds } 22310619f0f5SStephen Smalley selinux_null.dentry = d_hash_and_lookup(selinux_null.mnt->mnt_root, 22320619f0f5SStephen Smalley &null_name); 22330619f0f5SStephen Smalley if (IS_ERR(selinux_null.dentry)) { 22340619f0f5SStephen Smalley pr_err("selinuxfs: could not lookup null!\n"); 22350619f0f5SStephen Smalley err = PTR_ERR(selinux_null.dentry); 22360619f0f5SStephen Smalley selinux_null.dentry = NULL; 22370619f0f5SStephen Smalley } 2238b77a493bSEric Paris 22391da177e4SLinus Torvalds return err; 22401da177e4SLinus Torvalds } 22411da177e4SLinus Torvalds 22421da177e4SLinus Torvalds __initcall(init_sel_fs); 22431da177e4SLinus Torvalds 22441da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE 22451da177e4SLinus Torvalds void exit_sel_fs(void) 22461da177e4SLinus Torvalds { 2247f9bb4882SEric W. Biederman sysfs_remove_mount_point(fs_kobj, "selinux"); 2248fd40ffc7SStephen Smalley dput(selinux_null.dentry); 2249423e0ab0STim Chen kern_unmount(selinuxfs_mount); 22501da177e4SLinus Torvalds unregister_filesystem(&sel_fs_type); 22511da177e4SLinus Torvalds } 22521da177e4SLinus Torvalds #endif 2253