1a10e763bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /* Updated: Karl MacMillan <kmacmillan@tresys.com>
31da177e4SLinus Torvalds *
41da177e4SLinus Torvalds * Added conditional policy language extensions
51da177e4SLinus Torvalds *
682c21bfaSPaul Moore * Updated: Hewlett-Packard <paul@paul-moore.com>
73bb56b25SPaul Moore *
83bb56b25SPaul Moore * Added support for the policy capability bitmap
93bb56b25SPaul Moore *
103bb56b25SPaul Moore * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
111da177e4SLinus Torvalds * Copyright (C) 2003 - 2004 Tresys Technology, LLC
121da177e4SLinus Torvalds * Copyright (C) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
131da177e4SLinus Torvalds */
141da177e4SLinus Torvalds
151da177e4SLinus Torvalds #include <linux/kernel.h>
161da177e4SLinus Torvalds #include <linux/pagemap.h>
171da177e4SLinus Torvalds #include <linux/slab.h>
181da177e4SLinus Torvalds #include <linux/vmalloc.h>
191da177e4SLinus Torvalds #include <linux/fs.h>
20920f50b2SDavid Howells #include <linux/fs_context.h>
210619f0f5SStephen Smalley #include <linux/mount.h>
22bb003079SIngo Molnar #include <linux/mutex.h>
230eea6091SDaniel Burgener #include <linux/namei.h>
241da177e4SLinus Torvalds #include <linux/init.h>
251da177e4SLinus Torvalds #include <linux/string.h>
261da177e4SLinus Torvalds #include <linux/security.h>
271da177e4SLinus Torvalds #include <linux/major.h>
281da177e4SLinus Torvalds #include <linux/seq_file.h>
291da177e4SLinus Torvalds #include <linux/percpu.h>
30af601e46SSteve Grubb #include <linux/audit.h>
31f5269710SEric Paris #include <linux/uaccess.h>
327a627e3bSGreg Kroah-Hartman #include <linux/kobject.h>
330f7e4c33SKohei Kaigai #include <linux/ctype.h>
341da177e4SLinus Torvalds
351da177e4SLinus Torvalds /* selinuxfs pseudo filesystem for exporting the security policy API.
361da177e4SLinus Torvalds Based on the proc code and the fs/nfsd/nfsctl.c code. */
371da177e4SLinus Torvalds
381da177e4SLinus Torvalds #include "flask.h"
391da177e4SLinus Torvalds #include "avc.h"
401da177e4SLinus Torvalds #include "avc_ss.h"
411da177e4SLinus Torvalds #include "security.h"
421da177e4SLinus Torvalds #include "objsec.h"
431da177e4SLinus Torvalds #include "conditional.h"
442554a48fSLakshmi Ramasubramanian #include "ima.h"
451da177e4SLinus Torvalds
461da177e4SLinus Torvalds enum sel_inos {
471da177e4SLinus Torvalds SEL_ROOT_INO = 2,
481da177e4SLinus Torvalds SEL_LOAD, /* load policy */
491da177e4SLinus Torvalds SEL_ENFORCE, /* get or set enforcing status */
501da177e4SLinus Torvalds SEL_CONTEXT, /* validate context */
511da177e4SLinus Torvalds SEL_ACCESS, /* compute access decision */
521da177e4SLinus Torvalds SEL_CREATE, /* compute create labeling decision */
531da177e4SLinus Torvalds SEL_RELABEL, /* compute relabeling decision */
541da177e4SLinus Torvalds SEL_USER, /* compute reachable user contexts */
551da177e4SLinus Torvalds SEL_POLICYVERS, /* return policy version for this kernel */
561da177e4SLinus Torvalds SEL_COMMIT_BOOLS, /* commit new boolean values */
571da177e4SLinus Torvalds SEL_MLS, /* return if MLS policy is enabled */
581da177e4SLinus Torvalds SEL_DISABLE, /* disable SELinux until next reboot */
591da177e4SLinus Torvalds SEL_MEMBER, /* compute polyinstantiation membership decision */
601da177e4SLinus Torvalds SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
614e5ab4cbSJames Morris SEL_COMPAT_NET, /* whether to use old compat network packet controls */
623f12070eSEric Paris SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */
633f12070eSEric Paris SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */
6411904167SKaiGai Kohei SEL_STATUS, /* export current status using mmap() */
65cee74f47SEric Paris SEL_POLICY, /* allow userspace to read the in kernel policy */
66f9df6458SAndrew Perepechko SEL_VALIDATE_TRANS, /* compute validatetrans decision */
676174eafcSJames Carter SEL_INO_NEXT, /* The next inode number to use */
681da177e4SLinus Torvalds };
691da177e4SLinus Torvalds
700619f0f5SStephen Smalley struct selinux_fs_info {
710619f0f5SStephen Smalley struct dentry *bool_dir;
720619f0f5SStephen Smalley unsigned int bool_num;
730619f0f5SStephen Smalley char **bool_pending_names;
74c3fae2b2SChristian Göttsche int *bool_pending_values;
750619f0f5SStephen Smalley struct dentry *class_dir;
760619f0f5SStephen Smalley unsigned long last_class_ino;
770619f0f5SStephen Smalley bool policy_opened;
780619f0f5SStephen Smalley struct dentry *policycap_dir;
790619f0f5SStephen Smalley unsigned long last_ino;
800619f0f5SStephen Smalley struct super_block *sb;
810619f0f5SStephen Smalley };
820619f0f5SStephen Smalley
selinux_fs_info_create(struct super_block * sb)830619f0f5SStephen Smalley static int selinux_fs_info_create(struct super_block *sb)
840619f0f5SStephen Smalley {
850619f0f5SStephen Smalley struct selinux_fs_info *fsi;
860619f0f5SStephen Smalley
870619f0f5SStephen Smalley fsi = kzalloc(sizeof(*fsi), GFP_KERNEL);
880619f0f5SStephen Smalley if (!fsi)
890619f0f5SStephen Smalley return -ENOMEM;
900619f0f5SStephen Smalley
910619f0f5SStephen Smalley fsi->last_ino = SEL_INO_NEXT - 1;
920619f0f5SStephen Smalley fsi->sb = sb;
930619f0f5SStephen Smalley sb->s_fs_info = fsi;
940619f0f5SStephen Smalley return 0;
950619f0f5SStephen Smalley }
960619f0f5SStephen Smalley
selinux_fs_info_free(struct super_block * sb)970619f0f5SStephen Smalley static void selinux_fs_info_free(struct super_block *sb)
980619f0f5SStephen Smalley {
990619f0f5SStephen Smalley struct selinux_fs_info *fsi = sb->s_fs_info;
10097842c56SChristian Göttsche unsigned int i;
1010619f0f5SStephen Smalley
1020619f0f5SStephen Smalley if (fsi) {
1030619f0f5SStephen Smalley for (i = 0; i < fsi->bool_num; i++)
1040619f0f5SStephen Smalley kfree(fsi->bool_pending_names[i]);
1050619f0f5SStephen Smalley kfree(fsi->bool_pending_names);
1060619f0f5SStephen Smalley kfree(fsi->bool_pending_values);
1070619f0f5SStephen Smalley }
1080619f0f5SStephen Smalley kfree(sb->s_fs_info);
1090619f0f5SStephen Smalley sb->s_fs_info = NULL;
1100619f0f5SStephen Smalley }
1116174eafcSJames Carter
112f0ee2e46SJames Carter #define SEL_INITCON_INO_OFFSET 0x01000000
113bce34bc0SJames Carter #define SEL_BOOL_INO_OFFSET 0x02000000
114e47c8fc5SChristopher J. PeBenito #define SEL_CLASS_INO_OFFSET 0x04000000
1153bb56b25SPaul Moore #define SEL_POLICYCAP_INO_OFFSET 0x08000000
116f0ee2e46SJames Carter #define SEL_INO_MASK 0x00ffffff
117f0ee2e46SJames Carter
118613ba187SDaniel Burgener #define BOOL_DIR_NAME "booleans"
119613ba187SDaniel Burgener #define CLASS_DIR_NAME "class"
120613ba187SDaniel Burgener #define POLICYCAP_DIR_NAME "policy_capabilities"
121613ba187SDaniel Burgener
1221da177e4SLinus Torvalds #define TMPBUFLEN 12
sel_read_enforce(struct file * filp,char __user * buf,size_t count,loff_t * ppos)1231da177e4SLinus Torvalds static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
1241da177e4SLinus Torvalds size_t count, loff_t *ppos)
1251da177e4SLinus Torvalds {
1261da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN];
1271da177e4SLinus Torvalds ssize_t length;
1281da177e4SLinus Torvalds
129aa8e712cSStephen Smalley length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
130e67b7985SStephen Smalley enforcing_enabled());
1311da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1321da177e4SLinus Torvalds }
1331da177e4SLinus Torvalds
1341da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
sel_write_enforce(struct file * file,const char __user * buf,size_t count,loff_t * ppos)1351da177e4SLinus Torvalds static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
1361da177e4SLinus Torvalds size_t count, loff_t *ppos)
1371da177e4SLinus Torvalds
1381da177e4SLinus Torvalds {
139b77a493bSEric Paris char *page = NULL;
1401da177e4SLinus Torvalds ssize_t length;
141c867248cSChristian Göttsche int scan_value;
142c867248cSChristian Göttsche bool old_value, new_value;
1431da177e4SLinus Torvalds
144bfd51626SDavi Arnaut if (count >= PAGE_SIZE)
1458365a719SAl Viro return -ENOMEM;
146b77a493bSEric Paris
1471da177e4SLinus Torvalds /* No partial writes. */
148b77a493bSEric Paris if (*ppos != 0)
1498365a719SAl Viro return -EINVAL;
150b77a493bSEric Paris
1518365a719SAl Viro page = memdup_user_nul(buf, count);
1528365a719SAl Viro if (IS_ERR(page))
1538365a719SAl Viro return PTR_ERR(page);
1541da177e4SLinus Torvalds
1551da177e4SLinus Torvalds length = -EINVAL;
156c867248cSChristian Göttsche if (sscanf(page, "%d", &scan_value) != 1)
1571da177e4SLinus Torvalds goto out;
1581da177e4SLinus Torvalds
159c867248cSChristian Göttsche new_value = !!scan_value;
160ea49d10eSStephen Smalley
161e67b7985SStephen Smalley old_value = enforcing_enabled();
162aa8e712cSStephen Smalley if (new_value != old_value) {
163e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
164be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETENFORCE,
165be0554c9SStephen Smalley NULL);
1661da177e4SLinus Torvalds if (length)
1671da177e4SLinus Torvalds goto out;
168cdfb6b34SRichard Guy Briggs audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS,
1694195ed42SRichard Guy Briggs "enforcing=%d old_enforcing=%d auid=%u ses=%u"
1706c5a682eSStephen Smalley " enabled=1 old-enabled=1 lsm=selinux res=1",
171aa8e712cSStephen Smalley new_value, old_value,
172581abc09SEric W. Biederman from_kuid(&init_user_ns, audit_get_loginuid(current)),
1736c5a682eSStephen Smalley audit_get_sessionid(current));
174e67b7985SStephen Smalley enforcing_set(new_value);
175aa8e712cSStephen Smalley if (new_value)
176e67b7985SStephen Smalley avc_ss_reset(0);
177aa8e712cSStephen Smalley selnl_notify_setenforce(new_value);
178e67b7985SStephen Smalley selinux_status_update_setenforce(new_value);
179aa8e712cSStephen Smalley if (!new_value)
18042df744cSJanne Karhunen call_blocking_lsm_notifier(LSM_POLICY_CHANGE, NULL);
1812554a48fSLakshmi Ramasubramanian
182e67b7985SStephen Smalley selinux_ima_measure_state();
1831da177e4SLinus Torvalds }
1841da177e4SLinus Torvalds length = count;
1851da177e4SLinus Torvalds out:
1868365a719SAl Viro kfree(page);
1871da177e4SLinus Torvalds return length;
1881da177e4SLinus Torvalds }
1891da177e4SLinus Torvalds #else
1901da177e4SLinus Torvalds #define sel_write_enforce NULL
1911da177e4SLinus Torvalds #endif
1921da177e4SLinus Torvalds
1939c2e08c5SArjan van de Ven static const struct file_operations sel_enforce_ops = {
1941da177e4SLinus Torvalds .read = sel_read_enforce,
1951da177e4SLinus Torvalds .write = sel_write_enforce,
19657a62c23SArnd Bergmann .llseek = generic_file_llseek,
1971da177e4SLinus Torvalds };
1981da177e4SLinus Torvalds
sel_read_handle_unknown(struct file * filp,char __user * buf,size_t count,loff_t * ppos)1993f12070eSEric Paris static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf,
2003f12070eSEric Paris size_t count, loff_t *ppos)
2013f12070eSEric Paris {
2023f12070eSEric Paris char tmpbuf[TMPBUFLEN];
2033f12070eSEric Paris ssize_t length;
204496ad9aaSAl Viro ino_t ino = file_inode(filp)->i_ino;
2053f12070eSEric Paris int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
206e67b7985SStephen Smalley security_get_reject_unknown() :
207e67b7985SStephen Smalley !security_get_allow_unknown();
2083f12070eSEric Paris
2093f12070eSEric Paris length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
2103f12070eSEric Paris return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
2113f12070eSEric Paris }
2123f12070eSEric Paris
2133f12070eSEric Paris static const struct file_operations sel_handle_unknown_ops = {
2143f12070eSEric Paris .read = sel_read_handle_unknown,
21557a62c23SArnd Bergmann .llseek = generic_file_llseek,
2163f12070eSEric Paris };
2173f12070eSEric Paris
sel_open_handle_status(struct inode * inode,struct file * filp)21811904167SKaiGai Kohei static int sel_open_handle_status(struct inode *inode, struct file *filp)
21911904167SKaiGai Kohei {
220e67b7985SStephen Smalley struct page *status = selinux_kernel_status_page();
22111904167SKaiGai Kohei
22211904167SKaiGai Kohei if (!status)
22311904167SKaiGai Kohei return -ENOMEM;
22411904167SKaiGai Kohei
22511904167SKaiGai Kohei filp->private_data = status;
22611904167SKaiGai Kohei
22711904167SKaiGai Kohei return 0;
22811904167SKaiGai Kohei }
22911904167SKaiGai Kohei
sel_read_handle_status(struct file * filp,char __user * buf,size_t count,loff_t * ppos)23011904167SKaiGai Kohei static ssize_t sel_read_handle_status(struct file *filp, char __user *buf,
23111904167SKaiGai Kohei size_t count, loff_t *ppos)
23211904167SKaiGai Kohei {
23311904167SKaiGai Kohei struct page *status = filp->private_data;
23411904167SKaiGai Kohei
23511904167SKaiGai Kohei BUG_ON(!status);
23611904167SKaiGai Kohei
23711904167SKaiGai Kohei return simple_read_from_buffer(buf, count, ppos,
23811904167SKaiGai Kohei page_address(status),
23911904167SKaiGai Kohei sizeof(struct selinux_kernel_status));
24011904167SKaiGai Kohei }
24111904167SKaiGai Kohei
sel_mmap_handle_status(struct file * filp,struct vm_area_struct * vma)24211904167SKaiGai Kohei static int sel_mmap_handle_status(struct file *filp,
24311904167SKaiGai Kohei struct vm_area_struct *vma)
24411904167SKaiGai Kohei {
24511904167SKaiGai Kohei struct page *status = filp->private_data;
24611904167SKaiGai Kohei unsigned long size = vma->vm_end - vma->vm_start;
24711904167SKaiGai Kohei
24811904167SKaiGai Kohei BUG_ON(!status);
24911904167SKaiGai Kohei
25011904167SKaiGai Kohei /* only allows one page from the head */
25111904167SKaiGai Kohei if (vma->vm_pgoff > 0 || size != PAGE_SIZE)
25211904167SKaiGai Kohei return -EIO;
25311904167SKaiGai Kohei /* disallow writable mapping */
25411904167SKaiGai Kohei if (vma->vm_flags & VM_WRITE)
25511904167SKaiGai Kohei return -EPERM;
25611904167SKaiGai Kohei /* disallow mprotect() turns it into writable */
2571c71222eSSuren Baghdasaryan vm_flags_clear(vma, VM_MAYWRITE);
25811904167SKaiGai Kohei
25911904167SKaiGai Kohei return remap_pfn_range(vma, vma->vm_start,
26011904167SKaiGai Kohei page_to_pfn(status),
26111904167SKaiGai Kohei size, vma->vm_page_prot);
26211904167SKaiGai Kohei }
26311904167SKaiGai Kohei
26411904167SKaiGai Kohei static const struct file_operations sel_handle_status_ops = {
26511904167SKaiGai Kohei .open = sel_open_handle_status,
26611904167SKaiGai Kohei .read = sel_read_handle_status,
26711904167SKaiGai Kohei .mmap = sel_mmap_handle_status,
26811904167SKaiGai Kohei .llseek = generic_file_llseek,
26911904167SKaiGai Kohei };
27011904167SKaiGai Kohei
sel_write_disable(struct file * file,const char __user * buf,size_t count,loff_t * ppos)2711da177e4SLinus Torvalds static ssize_t sel_write_disable(struct file *file, const char __user *buf,
2721da177e4SLinus Torvalds size_t count, loff_t *ppos)
2731da177e4SLinus Torvalds
2741da177e4SLinus Torvalds {
2758365a719SAl Viro char *page;
2761da177e4SLinus Torvalds ssize_t length;
2771da177e4SLinus Torvalds int new_value;
27889b223bfSPaul Moore
279bfd51626SDavi Arnaut if (count >= PAGE_SIZE)
2808365a719SAl Viro return -ENOMEM;
281b77a493bSEric Paris
2821da177e4SLinus Torvalds /* No partial writes. */
283b77a493bSEric Paris if (*ppos != 0)
2848365a719SAl Viro return -EINVAL;
285b77a493bSEric Paris
2868365a719SAl Viro page = memdup_user_nul(buf, count);
2878365a719SAl Viro if (IS_ERR(page))
2888365a719SAl Viro return PTR_ERR(page);
2891da177e4SLinus Torvalds
290f22f9aafSPaul Moore if (sscanf(page, "%d", &new_value) != 1) {
2911da177e4SLinus Torvalds length = -EINVAL;
2921da177e4SLinus Torvalds goto out;
293f22f9aafSPaul Moore }
294f22f9aafSPaul Moore length = count;
2951da177e4SLinus Torvalds
2961da177e4SLinus Torvalds if (new_value) {
297f22f9aafSPaul Moore pr_err("SELinux: https://github.com/SELinuxProject/selinux-kernel/wiki/DEPRECATE-runtime-disable\n");
298f22f9aafSPaul Moore pr_err("SELinux: Runtime disable is not supported, use selinux=0 on the kernel cmdline.\n");
2991da177e4SLinus Torvalds }
3001da177e4SLinus Torvalds
3011da177e4SLinus Torvalds out:
3028365a719SAl Viro kfree(page);
3031da177e4SLinus Torvalds return length;
3041da177e4SLinus Torvalds }
3051da177e4SLinus Torvalds
3069c2e08c5SArjan van de Ven static const struct file_operations sel_disable_ops = {
3071da177e4SLinus Torvalds .write = sel_write_disable,
30857a62c23SArnd Bergmann .llseek = generic_file_llseek,
3091da177e4SLinus Torvalds };
3101da177e4SLinus Torvalds
sel_read_policyvers(struct file * filp,char __user * buf,size_t count,loff_t * ppos)3111da177e4SLinus Torvalds static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
3121da177e4SLinus Torvalds size_t count, loff_t *ppos)
3131da177e4SLinus Torvalds {
3141da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN];
3151da177e4SLinus Torvalds ssize_t length;
3161da177e4SLinus Torvalds
3171da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
3181da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
3191da177e4SLinus Torvalds }
3201da177e4SLinus Torvalds
3219c2e08c5SArjan van de Ven static const struct file_operations sel_policyvers_ops = {
3221da177e4SLinus Torvalds .read = sel_read_policyvers,
32357a62c23SArnd Bergmann .llseek = generic_file_llseek,
3241da177e4SLinus Torvalds };
3251da177e4SLinus Torvalds
3261da177e4SLinus Torvalds /* declaration for sel_write_load */
32766ec384aSDaniel Burgener static int sel_make_bools(struct selinux_policy *newpolicy, struct dentry *bool_dir,
32866ec384aSDaniel Burgener unsigned int *bool_num, char ***bool_pending_names,
329c3fae2b2SChristian Göttsche int **bool_pending_values);
33066ec384aSDaniel Burgener static int sel_make_classes(struct selinux_policy *newpolicy,
33166ec384aSDaniel Burgener struct dentry *class_dir,
33266ec384aSDaniel Burgener unsigned long *last_class_ino);
333e47c8fc5SChristopher J. PeBenito
334e47c8fc5SChristopher J. PeBenito /* declaration for sel_make_class_dirs */
335a1c2aa1eSAl Viro static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
336e47c8fc5SChristopher J. PeBenito unsigned long *ino);
3371da177e4SLinus Torvalds
3380eea6091SDaniel Burgener /* declaration for sel_make_policy_nodes */
3394a0b33f7SAl Viro static struct dentry *sel_make_swapover_dir(struct super_block *sb,
3400eea6091SDaniel Burgener unsigned long *ino);
3410eea6091SDaniel Burgener
sel_read_mls(struct file * filp,char __user * buf,size_t count,loff_t * ppos)3421da177e4SLinus Torvalds static ssize_t sel_read_mls(struct file *filp, char __user *buf,
3431da177e4SLinus Torvalds size_t count, loff_t *ppos)
3441da177e4SLinus Torvalds {
3451da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN];
3461da177e4SLinus Torvalds ssize_t length;
3471da177e4SLinus Torvalds
3480719aaf5SGuido Trentalancia length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
349e67b7985SStephen Smalley security_mls_enabled());
3501da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
3511da177e4SLinus Torvalds }
3521da177e4SLinus Torvalds
3539c2e08c5SArjan van de Ven static const struct file_operations sel_mls_ops = {
3541da177e4SLinus Torvalds .read = sel_read_mls,
35557a62c23SArnd Bergmann .llseek = generic_file_llseek,
3561da177e4SLinus Torvalds };
3571da177e4SLinus Torvalds
358cee74f47SEric Paris struct policy_load_memory {
359cee74f47SEric Paris size_t len;
360cee74f47SEric Paris void *data;
361cee74f47SEric Paris };
362cee74f47SEric Paris
sel_open_policy(struct inode * inode,struct file * filp)363cee74f47SEric Paris static int sel_open_policy(struct inode *inode, struct file *filp)
364cee74f47SEric Paris {
3650619f0f5SStephen Smalley struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
366cee74f47SEric Paris struct policy_load_memory *plm = NULL;
367cee74f47SEric Paris int rc;
368cee74f47SEric Paris
369cee74f47SEric Paris BUG_ON(filp->private_data);
370cee74f47SEric Paris
371e67b7985SStephen Smalley mutex_lock(&selinux_state.policy_mutex);
372cee74f47SEric Paris
373e67b7985SStephen Smalley rc = avc_has_perm(current_sid(), SECINITSID_SECURITY,
374be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
375cee74f47SEric Paris if (rc)
376cee74f47SEric Paris goto err;
377cee74f47SEric Paris
378cee74f47SEric Paris rc = -EBUSY;
3790619f0f5SStephen Smalley if (fsi->policy_opened)
380cee74f47SEric Paris goto err;
381cee74f47SEric Paris
382cee74f47SEric Paris rc = -ENOMEM;
383cee74f47SEric Paris plm = kzalloc(sizeof(*plm), GFP_KERNEL);
384cee74f47SEric Paris if (!plm)
385cee74f47SEric Paris goto err;
386cee74f47SEric Paris
387e67b7985SStephen Smalley rc = security_read_policy(&plm->data, &plm->len);
388cee74f47SEric Paris if (rc)
389cee74f47SEric Paris goto err;
390cee74f47SEric Paris
39166ccd256SOndrej Mosnacek if ((size_t)i_size_read(inode) != plm->len) {
39266ccd256SOndrej Mosnacek inode_lock(inode);
39366ccd256SOndrej Mosnacek i_size_write(inode, plm->len);
39466ccd256SOndrej Mosnacek inode_unlock(inode);
39566ccd256SOndrej Mosnacek }
39666ccd256SOndrej Mosnacek
3970619f0f5SStephen Smalley fsi->policy_opened = 1;
398cee74f47SEric Paris
399cee74f47SEric Paris filp->private_data = plm;
400cee74f47SEric Paris
401e67b7985SStephen Smalley mutex_unlock(&selinux_state.policy_mutex);
402cee74f47SEric Paris
403cee74f47SEric Paris return 0;
404cee74f47SEric Paris err:
405e67b7985SStephen Smalley mutex_unlock(&selinux_state.policy_mutex);
406cee74f47SEric Paris
407cee74f47SEric Paris if (plm)
408cee74f47SEric Paris vfree(plm->data);
409cee74f47SEric Paris kfree(plm);
410cee74f47SEric Paris return rc;
411cee74f47SEric Paris }
412cee74f47SEric Paris
sel_release_policy(struct inode * inode,struct file * filp)413cee74f47SEric Paris static int sel_release_policy(struct inode *inode, struct file *filp)
414cee74f47SEric Paris {
4150619f0f5SStephen Smalley struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
416cee74f47SEric Paris struct policy_load_memory *plm = filp->private_data;
417cee74f47SEric Paris
418cee74f47SEric Paris BUG_ON(!plm);
419cee74f47SEric Paris
4200619f0f5SStephen Smalley fsi->policy_opened = 0;
421cee74f47SEric Paris
422cee74f47SEric Paris vfree(plm->data);
423cee74f47SEric Paris kfree(plm);
424cee74f47SEric Paris
425cee74f47SEric Paris return 0;
426cee74f47SEric Paris }
427cee74f47SEric Paris
sel_read_policy(struct file * filp,char __user * buf,size_t count,loff_t * ppos)428cee74f47SEric Paris static ssize_t sel_read_policy(struct file *filp, char __user *buf,
429cee74f47SEric Paris size_t count, loff_t *ppos)
430cee74f47SEric Paris {
431cee74f47SEric Paris struct policy_load_memory *plm = filp->private_data;
432cee74f47SEric Paris int ret;
433cee74f47SEric Paris
434e67b7985SStephen Smalley ret = avc_has_perm(current_sid(), SECINITSID_SECURITY,
435be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
436cee74f47SEric Paris if (ret)
437cee74f47SEric Paris return ret;
4380da74120SJann Horn
4390da74120SJann Horn return simple_read_from_buffer(buf, count, ppos, plm->data, plm->len);
440cee74f47SEric Paris }
441cee74f47SEric Paris
sel_mmap_policy_fault(struct vm_fault * vmf)442ac9a1f6dSSouptick Joarder static vm_fault_t sel_mmap_policy_fault(struct vm_fault *vmf)
443845ca30fSEric Paris {
44411bac800SDave Jiang struct policy_load_memory *plm = vmf->vma->vm_file->private_data;
445845ca30fSEric Paris unsigned long offset;
446845ca30fSEric Paris struct page *page;
447845ca30fSEric Paris
448845ca30fSEric Paris if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE))
449845ca30fSEric Paris return VM_FAULT_SIGBUS;
450845ca30fSEric Paris
451845ca30fSEric Paris offset = vmf->pgoff << PAGE_SHIFT;
452845ca30fSEric Paris if (offset >= roundup(plm->len, PAGE_SIZE))
453845ca30fSEric Paris return VM_FAULT_SIGBUS;
454845ca30fSEric Paris
455845ca30fSEric Paris page = vmalloc_to_page(plm->data + offset);
456845ca30fSEric Paris get_page(page);
457845ca30fSEric Paris
458845ca30fSEric Paris vmf->page = page;
459845ca30fSEric Paris
460845ca30fSEric Paris return 0;
461845ca30fSEric Paris }
462845ca30fSEric Paris
4637cbea8dcSKirill A. Shutemov static const struct vm_operations_struct sel_mmap_policy_ops = {
464845ca30fSEric Paris .fault = sel_mmap_policy_fault,
465845ca30fSEric Paris .page_mkwrite = sel_mmap_policy_fault,
466845ca30fSEric Paris };
467845ca30fSEric Paris
sel_mmap_policy(struct file * filp,struct vm_area_struct * vma)468ad3fa08cSJames Morris static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma)
469845ca30fSEric Paris {
470845ca30fSEric Paris if (vma->vm_flags & VM_SHARED) {
471845ca30fSEric Paris /* do not allow mprotect to make mapping writable */
4721c71222eSSuren Baghdasaryan vm_flags_clear(vma, VM_MAYWRITE);
473845ca30fSEric Paris
474845ca30fSEric Paris if (vma->vm_flags & VM_WRITE)
475845ca30fSEric Paris return -EACCES;
476845ca30fSEric Paris }
477845ca30fSEric Paris
4781c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_DONTEXPAND | VM_DONTDUMP);
479845ca30fSEric Paris vma->vm_ops = &sel_mmap_policy_ops;
480845ca30fSEric Paris
481845ca30fSEric Paris return 0;
482845ca30fSEric Paris }
483845ca30fSEric Paris
484cee74f47SEric Paris static const struct file_operations sel_policy_ops = {
485cee74f47SEric Paris .open = sel_open_policy,
486cee74f47SEric Paris .read = sel_read_policy,
487845ca30fSEric Paris .mmap = sel_mmap_policy,
488cee74f47SEric Paris .release = sel_release_policy,
48947a93a5bSEric Paris .llseek = generic_file_llseek,
490cee74f47SEric Paris };
491cee74f47SEric Paris
sel_remove_old_bool_data(unsigned int bool_num,char ** bool_names,int * bool_values)4920eea6091SDaniel Burgener static void sel_remove_old_bool_data(unsigned int bool_num, char **bool_names,
493c3fae2b2SChristian Göttsche int *bool_values)
494aeecf4a3SDaniel Burgener {
495aeecf4a3SDaniel Burgener u32 i;
496aeecf4a3SDaniel Burgener
497aeecf4a3SDaniel Burgener /* bool_dir cleanup */
4980eea6091SDaniel Burgener for (i = 0; i < bool_num; i++)
4990eea6091SDaniel Burgener kfree(bool_names[i]);
5000eea6091SDaniel Burgener kfree(bool_names);
5010eea6091SDaniel Burgener kfree(bool_values);
502aeecf4a3SDaniel Burgener }
503aeecf4a3SDaniel Burgener
sel_make_policy_nodes(struct selinux_fs_info * fsi,struct selinux_policy * newpolicy)50402a52c5cSStephen Smalley static int sel_make_policy_nodes(struct selinux_fs_info *fsi,
50502a52c5cSStephen Smalley struct selinux_policy *newpolicy)
5060619f0f5SStephen Smalley {
5070eea6091SDaniel Burgener int ret = 0;
5084a0b33f7SAl Viro struct dentry *tmp_parent, *tmp_bool_dir, *tmp_class_dir;
5094a0b33f7SAl Viro unsigned int bool_num = 0;
5104a0b33f7SAl Viro char **bool_names = NULL;
5114a0b33f7SAl Viro int *bool_values = NULL;
5120eea6091SDaniel Burgener unsigned long tmp_ino = fsi->last_ino; /* Don't increment last_ino in this function */
5130619f0f5SStephen Smalley
5144a0b33f7SAl Viro tmp_parent = sel_make_swapover_dir(fsi->sb, &tmp_ino);
5150eea6091SDaniel Burgener if (IS_ERR(tmp_parent))
5160eea6091SDaniel Burgener return PTR_ERR(tmp_parent);
517aeecf4a3SDaniel Burgener
5180eea6091SDaniel Burgener tmp_ino = fsi->bool_dir->d_inode->i_ino - 1; /* sel_make_dir will increment and set */
5190eea6091SDaniel Burgener tmp_bool_dir = sel_make_dir(tmp_parent, BOOL_DIR_NAME, &tmp_ino);
5200eea6091SDaniel Burgener if (IS_ERR(tmp_bool_dir)) {
5210eea6091SDaniel Burgener ret = PTR_ERR(tmp_bool_dir);
5220eea6091SDaniel Burgener goto out;
5230619f0f5SStephen Smalley }
5240619f0f5SStephen Smalley
5250eea6091SDaniel Burgener tmp_ino = fsi->class_dir->d_inode->i_ino - 1; /* sel_make_dir will increment and set */
5260eea6091SDaniel Burgener tmp_class_dir = sel_make_dir(tmp_parent, CLASS_DIR_NAME, &tmp_ino);
5270eea6091SDaniel Burgener if (IS_ERR(tmp_class_dir)) {
5280eea6091SDaniel Burgener ret = PTR_ERR(tmp_class_dir);
5290eea6091SDaniel Burgener goto out;
5300eea6091SDaniel Burgener }
5310eea6091SDaniel Burgener
5324a0b33f7SAl Viro ret = sel_make_bools(newpolicy, tmp_bool_dir, &bool_num,
5334a0b33f7SAl Viro &bool_names, &bool_values);
534ee5de60aSOndrej Mosnacek if (ret)
5350eea6091SDaniel Burgener goto out;
5360eea6091SDaniel Burgener
5370eea6091SDaniel Burgener ret = sel_make_classes(newpolicy, tmp_class_dir,
53866ec384aSDaniel Burgener &fsi->last_class_ino);
539ee5de60aSOndrej Mosnacek if (ret)
5400eea6091SDaniel Burgener goto out;
5410619f0f5SStephen Smalley
5424a0b33f7SAl Viro lock_rename(tmp_parent, fsi->sb->s_root);
5434a0b33f7SAl Viro
5440eea6091SDaniel Burgener /* booleans */
5450eea6091SDaniel Burgener d_exchange(tmp_bool_dir, fsi->bool_dir);
5460eea6091SDaniel Burgener
5474a0b33f7SAl Viro swap(fsi->bool_num, bool_num);
5484a0b33f7SAl Viro swap(fsi->bool_pending_names, bool_names);
5494a0b33f7SAl Viro swap(fsi->bool_pending_values, bool_values);
5500eea6091SDaniel Burgener
5510eea6091SDaniel Burgener fsi->bool_dir = tmp_bool_dir;
5520eea6091SDaniel Burgener
5530eea6091SDaniel Burgener /* classes */
5540eea6091SDaniel Burgener d_exchange(tmp_class_dir, fsi->class_dir);
5550eea6091SDaniel Burgener fsi->class_dir = tmp_class_dir;
5564a0b33f7SAl Viro
5574a0b33f7SAl Viro unlock_rename(tmp_parent, fsi->sb->s_root);
5580eea6091SDaniel Burgener
5590eea6091SDaniel Burgener out:
5604a0b33f7SAl Viro sel_remove_old_bool_data(bool_num, bool_names, bool_values);
5610eea6091SDaniel Burgener /* Since the other temporary dirs are children of tmp_parent
5620eea6091SDaniel Burgener * this will handle all the cleanup in the case of a failure before
5630eea6091SDaniel Burgener * the swapover
5640eea6091SDaniel Burgener */
5654a0b33f7SAl Viro simple_recursive_removal(tmp_parent, NULL);
5660eea6091SDaniel Burgener
5670eea6091SDaniel Burgener return ret;
5680619f0f5SStephen Smalley }
5690619f0f5SStephen Smalley
sel_write_load(struct file * file,const char __user * buf,size_t count,loff_t * ppos)5701da177e4SLinus Torvalds static ssize_t sel_write_load(struct file *file, const char __user *buf,
5711da177e4SLinus Torvalds size_t count, loff_t *ppos)
5721da177e4SLinus Torvalds
5731da177e4SLinus Torvalds {
57442c77323SPaul Moore struct selinux_fs_info *fsi;
5756406887aSOndrej Mosnacek struct selinux_load_state load_state;
5761da177e4SLinus Torvalds ssize_t length;
5771da177e4SLinus Torvalds void *data = NULL;
5781da177e4SLinus Torvalds
57942c77323SPaul Moore /* no partial writes */
58042c77323SPaul Moore if (*ppos)
58142c77323SPaul Moore return -EINVAL;
58242c77323SPaul Moore /* no empty policies */
58342c77323SPaul Moore if (!count)
58442c77323SPaul Moore return -EINVAL;
58542c77323SPaul Moore
586e67b7985SStephen Smalley mutex_lock(&selinux_state.policy_mutex);
5871da177e4SLinus Torvalds
588e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
589be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL);
5901da177e4SLinus Torvalds if (length)
5911da177e4SLinus Torvalds goto out;
5921da177e4SLinus Torvalds
593b77a493bSEric Paris data = vmalloc(count);
59442c77323SPaul Moore if (!data) {
59542c77323SPaul Moore length = -ENOMEM;
596b77a493bSEric Paris goto out;
59742c77323SPaul Moore }
59842c77323SPaul Moore if (copy_from_user(data, buf, count) != 0) {
5991da177e4SLinus Torvalds length = -EFAULT;
6001da177e4SLinus Torvalds goto out;
60142c77323SPaul Moore }
6021da177e4SLinus Torvalds
603e67b7985SStephen Smalley length = security_load_policy(data, count, &load_state);
6044262fb51SGary Tierney if (length) {
6054262fb51SGary Tierney pr_warn_ratelimited("SELinux: failed to load policy\n");
6061da177e4SLinus Torvalds goto out;
6074262fb51SGary Tierney }
60842c77323SPaul Moore fsi = file_inode(file)->i_sb->s_fs_info;
6096406887aSOndrej Mosnacek length = sel_make_policy_nodes(fsi, load_state.policy);
61002a52c5cSStephen Smalley if (length) {
611ee5de60aSOndrej Mosnacek pr_warn_ratelimited("SELinux: failed to initialize selinuxfs\n");
612e67b7985SStephen Smalley selinux_policy_cancel(&load_state);
613519dad3bSOndrej Mosnacek goto out;
61402a52c5cSStephen Smalley }
61502a52c5cSStephen Smalley
616e67b7985SStephen Smalley selinux_policy_commit(&load_state);
6171da177e4SLinus Torvalds length = count;
618cdfb6b34SRichard Guy Briggs audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
619d141136fSRichard Guy Briggs "auid=%u ses=%u lsm=selinux res=1",
620581abc09SEric W. Biederman from_kuid(&init_user_ns, audit_get_loginuid(current)),
6214746ec5bSEric Paris audit_get_sessionid(current));
62242c77323SPaul Moore
6231da177e4SLinus Torvalds out:
624e67b7985SStephen Smalley mutex_unlock(&selinux_state.policy_mutex);
6251da177e4SLinus Torvalds vfree(data);
6261da177e4SLinus Torvalds return length;
6271da177e4SLinus Torvalds }
6281da177e4SLinus Torvalds
6299c2e08c5SArjan van de Ven static const struct file_operations sel_load_ops = {
6301da177e4SLinus Torvalds .write = sel_write_load,
63157a62c23SArnd Bergmann .llseek = generic_file_llseek,
6321da177e4SLinus Torvalds };
6331da177e4SLinus Torvalds
sel_write_context(struct file * file,char * buf,size_t size)634ce9982d0SStephen Smalley static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
6351da177e4SLinus Torvalds {
636b77a493bSEric Paris char *canon = NULL;
637ce9982d0SStephen Smalley u32 sid, len;
6381da177e4SLinus Torvalds ssize_t length;
6391da177e4SLinus Torvalds
640e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
641be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__CHECK_CONTEXT, NULL);
6421da177e4SLinus Torvalds if (length)
643b77a493bSEric Paris goto out;
6441da177e4SLinus Torvalds
645e67b7985SStephen Smalley length = security_context_to_sid(buf, size, &sid, GFP_KERNEL);
646b77a493bSEric Paris if (length)
647b77a493bSEric Paris goto out;
6481da177e4SLinus Torvalds
649e67b7985SStephen Smalley length = security_sid_to_context(sid, &canon, &len);
650b77a493bSEric Paris if (length)
651b77a493bSEric Paris goto out;
652ce9982d0SStephen Smalley
653b77a493bSEric Paris length = -ERANGE;
654ce9982d0SStephen Smalley if (len > SIMPLE_TRANSACTION_LIMIT) {
655f8b69a5fSpeter enderborg pr_err("SELinux: %s: context size (%u) exceeds "
656744ba35eSEric Paris "payload max\n", __func__, len);
657ce9982d0SStephen Smalley goto out;
658ce9982d0SStephen Smalley }
659ce9982d0SStephen Smalley
660ce9982d0SStephen Smalley memcpy(buf, canon, len);
661ce9982d0SStephen Smalley length = len;
6621da177e4SLinus Torvalds out:
663ce9982d0SStephen Smalley kfree(canon);
6641da177e4SLinus Torvalds return length;
6651da177e4SLinus Torvalds }
6661da177e4SLinus Torvalds
sel_read_checkreqprot(struct file * filp,char __user * buf,size_t count,loff_t * ppos)6671da177e4SLinus Torvalds static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
6681da177e4SLinus Torvalds size_t count, loff_t *ppos)
6691da177e4SLinus Torvalds {
6701da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN];
6711da177e4SLinus Torvalds ssize_t length;
6721da177e4SLinus Torvalds
6738861d0afSLakshmi Ramasubramanian length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
674e67b7985SStephen Smalley checkreqprot_get());
6751da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
6761da177e4SLinus Torvalds }
6771da177e4SLinus Torvalds
sel_write_checkreqprot(struct file * file,const char __user * buf,size_t count,loff_t * ppos)6781da177e4SLinus Torvalds static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
6791da177e4SLinus Torvalds size_t count, loff_t *ppos)
6801da177e4SLinus Torvalds {
6818365a719SAl Viro char *page;
6821da177e4SLinus Torvalds ssize_t length;
6831da177e4SLinus Torvalds unsigned int new_value;
6841da177e4SLinus Torvalds
685e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
686be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETCHECKREQPROT,
687be0554c9SStephen Smalley NULL);
6881da177e4SLinus Torvalds if (length)
6898365a719SAl Viro return length;
6901da177e4SLinus Torvalds
691bfd51626SDavi Arnaut if (count >= PAGE_SIZE)
6928365a719SAl Viro return -ENOMEM;
693b77a493bSEric Paris
6941da177e4SLinus Torvalds /* No partial writes. */
695b77a493bSEric Paris if (*ppos != 0)
6968365a719SAl Viro return -EINVAL;
697b77a493bSEric Paris
6988365a719SAl Viro page = memdup_user_nul(buf, count);
6998365a719SAl Viro if (IS_ERR(page))
7008365a719SAl Viro return PTR_ERR(page);
7011da177e4SLinus Torvalds
702a7e4676eSPaul Moore if (sscanf(page, "%u", &new_value) != 1) {
7031da177e4SLinus Torvalds length = -EINVAL;
7041da177e4SLinus Torvalds goto out;
705a7e4676eSPaul Moore }
706a7e4676eSPaul Moore length = count;
7071da177e4SLinus Torvalds
708e9c38f9fSStephen Smalley if (new_value) {
709e9c38f9fSStephen Smalley char comm[sizeof(current->comm)];
710e9c38f9fSStephen Smalley
711e9c38f9fSStephen Smalley memcpy(comm, current->comm, sizeof(comm));
712a7e4676eSPaul Moore pr_err("SELinux: %s (%d) set checkreqprot to 1. This is no longer supported.\n",
713e9c38f9fSStephen Smalley comm, current->pid);
714e9c38f9fSStephen Smalley }
715e9c38f9fSStephen Smalley
716e67b7985SStephen Smalley selinux_ima_measure_state();
7172554a48fSLakshmi Ramasubramanian
7181da177e4SLinus Torvalds out:
7198365a719SAl Viro kfree(page);
7201da177e4SLinus Torvalds return length;
7211da177e4SLinus Torvalds }
7229c2e08c5SArjan van de Ven static const struct file_operations sel_checkreqprot_ops = {
7231da177e4SLinus Torvalds .read = sel_read_checkreqprot,
7241da177e4SLinus Torvalds .write = sel_write_checkreqprot,
72557a62c23SArnd Bergmann .llseek = generic_file_llseek,
7261da177e4SLinus Torvalds };
7271da177e4SLinus Torvalds
sel_write_validatetrans(struct file * file,const char __user * buf,size_t count,loff_t * ppos)728f9df6458SAndrew Perepechko static ssize_t sel_write_validatetrans(struct file *file,
729f9df6458SAndrew Perepechko const char __user *buf,
730f9df6458SAndrew Perepechko size_t count, loff_t *ppos)
731f9df6458SAndrew Perepechko {
732f9df6458SAndrew Perepechko char *oldcon = NULL, *newcon = NULL, *taskcon = NULL;
733f9df6458SAndrew Perepechko char *req = NULL;
734f9df6458SAndrew Perepechko u32 osid, nsid, tsid;
735f9df6458SAndrew Perepechko u16 tclass;
736f9df6458SAndrew Perepechko int rc;
737f9df6458SAndrew Perepechko
738e67b7985SStephen Smalley rc = avc_has_perm(current_sid(), SECINITSID_SECURITY,
739be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__VALIDATE_TRANS, NULL);
740f9df6458SAndrew Perepechko if (rc)
741f9df6458SAndrew Perepechko goto out;
742f9df6458SAndrew Perepechko
743f9df6458SAndrew Perepechko rc = -ENOMEM;
744f9df6458SAndrew Perepechko if (count >= PAGE_SIZE)
745f9df6458SAndrew Perepechko goto out;
746f9df6458SAndrew Perepechko
747f9df6458SAndrew Perepechko /* No partial writes. */
748f9df6458SAndrew Perepechko rc = -EINVAL;
749f9df6458SAndrew Perepechko if (*ppos != 0)
750f9df6458SAndrew Perepechko goto out;
751f9df6458SAndrew Perepechko
7520b884d25SAl Viro req = memdup_user_nul(buf, count);
7530b884d25SAl Viro if (IS_ERR(req)) {
7540b884d25SAl Viro rc = PTR_ERR(req);
7550b884d25SAl Viro req = NULL;
756f9df6458SAndrew Perepechko goto out;
7570b884d25SAl Viro }
758f9df6458SAndrew Perepechko
759f9df6458SAndrew Perepechko rc = -ENOMEM;
760f9df6458SAndrew Perepechko oldcon = kzalloc(count + 1, GFP_KERNEL);
761f9df6458SAndrew Perepechko if (!oldcon)
762f9df6458SAndrew Perepechko goto out;
763f9df6458SAndrew Perepechko
764f9df6458SAndrew Perepechko newcon = kzalloc(count + 1, GFP_KERNEL);
765f9df6458SAndrew Perepechko if (!newcon)
766f9df6458SAndrew Perepechko goto out;
767f9df6458SAndrew Perepechko
768f9df6458SAndrew Perepechko taskcon = kzalloc(count + 1, GFP_KERNEL);
769f9df6458SAndrew Perepechko if (!taskcon)
770f9df6458SAndrew Perepechko goto out;
771f9df6458SAndrew Perepechko
772f9df6458SAndrew Perepechko rc = -EINVAL;
773f9df6458SAndrew Perepechko if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4)
774f9df6458SAndrew Perepechko goto out;
775f9df6458SAndrew Perepechko
776e67b7985SStephen Smalley rc = security_context_str_to_sid(oldcon, &osid, GFP_KERNEL);
777f9df6458SAndrew Perepechko if (rc)
778f9df6458SAndrew Perepechko goto out;
779f9df6458SAndrew Perepechko
780e67b7985SStephen Smalley rc = security_context_str_to_sid(newcon, &nsid, GFP_KERNEL);
781f9df6458SAndrew Perepechko if (rc)
782f9df6458SAndrew Perepechko goto out;
783f9df6458SAndrew Perepechko
784e67b7985SStephen Smalley rc = security_context_str_to_sid(taskcon, &tsid, GFP_KERNEL);
785f9df6458SAndrew Perepechko if (rc)
786f9df6458SAndrew Perepechko goto out;
787f9df6458SAndrew Perepechko
788e67b7985SStephen Smalley rc = security_validate_transition_user(osid, nsid, tsid, tclass);
789f9df6458SAndrew Perepechko if (!rc)
790f9df6458SAndrew Perepechko rc = count;
791f9df6458SAndrew Perepechko out:
792f9df6458SAndrew Perepechko kfree(req);
793f9df6458SAndrew Perepechko kfree(oldcon);
794f9df6458SAndrew Perepechko kfree(newcon);
795f9df6458SAndrew Perepechko kfree(taskcon);
796f9df6458SAndrew Perepechko return rc;
797f9df6458SAndrew Perepechko }
798f9df6458SAndrew Perepechko
799f9df6458SAndrew Perepechko static const struct file_operations sel_transition_ops = {
800f9df6458SAndrew Perepechko .write = sel_write_validatetrans,
801f9df6458SAndrew Perepechko .llseek = generic_file_llseek,
802f9df6458SAndrew Perepechko };
803f9df6458SAndrew Perepechko
8041da177e4SLinus Torvalds /*
8051da177e4SLinus Torvalds * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
8061da177e4SLinus Torvalds */
8071da177e4SLinus Torvalds static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
8081da177e4SLinus Torvalds static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
8091da177e4SLinus Torvalds static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
8101da177e4SLinus Torvalds static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
8111da177e4SLinus Torvalds static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
8121da177e4SLinus Torvalds
813631d2b49SEric Biggers static ssize_t (*const write_op[])(struct file *, char *, size_t) = {
8141da177e4SLinus Torvalds [SEL_ACCESS] = sel_write_access,
8151da177e4SLinus Torvalds [SEL_CREATE] = sel_write_create,
8161da177e4SLinus Torvalds [SEL_RELABEL] = sel_write_relabel,
8171da177e4SLinus Torvalds [SEL_USER] = sel_write_user,
8181da177e4SLinus Torvalds [SEL_MEMBER] = sel_write_member,
819ce9982d0SStephen Smalley [SEL_CONTEXT] = sel_write_context,
8201da177e4SLinus Torvalds };
8211da177e4SLinus Torvalds
selinux_transaction_write(struct file * file,const char __user * buf,size_t size,loff_t * pos)8221da177e4SLinus Torvalds static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
8231da177e4SLinus Torvalds {
824496ad9aaSAl Viro ino_t ino = file_inode(file)->i_ino;
8251da177e4SLinus Torvalds char *data;
8261da177e4SLinus Torvalds ssize_t rv;
8271da177e4SLinus Torvalds
8286e20a64aSNicolas Kaiser if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
8291da177e4SLinus Torvalds return -EINVAL;
8301da177e4SLinus Torvalds
8311da177e4SLinus Torvalds data = simple_transaction_get(file, buf, size);
8321da177e4SLinus Torvalds if (IS_ERR(data))
8331da177e4SLinus Torvalds return PTR_ERR(data);
8341da177e4SLinus Torvalds
8351da177e4SLinus Torvalds rv = write_op[ino](file, data, size);
8361da177e4SLinus Torvalds if (rv > 0) {
8371da177e4SLinus Torvalds simple_transaction_set(file, rv);
8381da177e4SLinus Torvalds rv = size;
8391da177e4SLinus Torvalds }
8401da177e4SLinus Torvalds return rv;
8411da177e4SLinus Torvalds }
8421da177e4SLinus Torvalds
8439c2e08c5SArjan van de Ven static const struct file_operations transaction_ops = {
8441da177e4SLinus Torvalds .write = selinux_transaction_write,
8451da177e4SLinus Torvalds .read = simple_transaction_read,
8461da177e4SLinus Torvalds .release = simple_transaction_release,
84757a62c23SArnd Bergmann .llseek = generic_file_llseek,
8481da177e4SLinus Torvalds };
8491da177e4SLinus Torvalds
8501da177e4SLinus Torvalds /*
8511da177e4SLinus Torvalds * payload - write methods
8521da177e4SLinus Torvalds * If the method has a response, the response should be put in buf,
8531da177e4SLinus Torvalds * and the length returned. Otherwise return 0 or and -error.
8541da177e4SLinus Torvalds */
8551da177e4SLinus Torvalds
sel_write_access(struct file * file,char * buf,size_t size)8561da177e4SLinus Torvalds static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
8571da177e4SLinus Torvalds {
858b77a493bSEric Paris char *scon = NULL, *tcon = NULL;
8591da177e4SLinus Torvalds u32 ssid, tsid;
8601da177e4SLinus Torvalds u16 tclass;
8611da177e4SLinus Torvalds struct av_decision avd;
8621da177e4SLinus Torvalds ssize_t length;
8631da177e4SLinus Torvalds
864e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
865be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_AV, NULL);
8661da177e4SLinus Torvalds if (length)
867b77a493bSEric Paris goto out;
8681da177e4SLinus Torvalds
8691da177e4SLinus Torvalds length = -ENOMEM;
87089d155efSJames Morris scon = kzalloc(size + 1, GFP_KERNEL);
8711da177e4SLinus Torvalds if (!scon)
872b77a493bSEric Paris goto out;
8731da177e4SLinus Torvalds
874b77a493bSEric Paris length = -ENOMEM;
87589d155efSJames Morris tcon = kzalloc(size + 1, GFP_KERNEL);
8761da177e4SLinus Torvalds if (!tcon)
8771da177e4SLinus Torvalds goto out;
8781da177e4SLinus Torvalds
8791da177e4SLinus Torvalds length = -EINVAL;
88019439d05SStephen Smalley if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
881b77a493bSEric Paris goto out;
8821da177e4SLinus Torvalds
883e67b7985SStephen Smalley length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL);
884b77a493bSEric Paris if (length)
885b77a493bSEric Paris goto out;
886b77a493bSEric Paris
887e67b7985SStephen Smalley length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL);
888b77a493bSEric Paris if (length)
889b77a493bSEric Paris goto out;
8901da177e4SLinus Torvalds
891e67b7985SStephen Smalley security_compute_av_user(ssid, tsid, tclass, &avd);
8921da177e4SLinus Torvalds
8931da177e4SLinus Torvalds length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
8948a6f83afSKaiGai Kohei "%x %x %x %x %u %x",
895f1c6381aSEric Paris avd.allowed, 0xffffffff,
8961da177e4SLinus Torvalds avd.auditallow, avd.auditdeny,
8978a6f83afSKaiGai Kohei avd.seqno, avd.flags);
8981da177e4SLinus Torvalds out:
899b77a493bSEric Paris kfree(tcon);
9001da177e4SLinus Torvalds kfree(scon);
9011da177e4SLinus Torvalds return length;
9021da177e4SLinus Torvalds }
9031da177e4SLinus Torvalds
sel_write_create(struct file * file,char * buf,size_t size)9041da177e4SLinus Torvalds static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
9051da177e4SLinus Torvalds {
906b77a493bSEric Paris char *scon = NULL, *tcon = NULL;
907f50a3ec9SKohei Kaigai char *namebuf = NULL, *objname = NULL;
9081da177e4SLinus Torvalds u32 ssid, tsid, newsid;
9091da177e4SLinus Torvalds u16 tclass;
9101da177e4SLinus Torvalds ssize_t length;
911b77a493bSEric Paris char *newcon = NULL;
9121da177e4SLinus Torvalds u32 len;
913f50a3ec9SKohei Kaigai int nargs;
9141da177e4SLinus Torvalds
915e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
916be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_CREATE,
917be0554c9SStephen Smalley NULL);
9181da177e4SLinus Torvalds if (length)
919b77a493bSEric Paris goto out;
9201da177e4SLinus Torvalds
9211da177e4SLinus Torvalds length = -ENOMEM;
92289d155efSJames Morris scon = kzalloc(size + 1, GFP_KERNEL);
9231da177e4SLinus Torvalds if (!scon)
924b77a493bSEric Paris goto out;
9251da177e4SLinus Torvalds
926b77a493bSEric Paris length = -ENOMEM;
92789d155efSJames Morris tcon = kzalloc(size + 1, GFP_KERNEL);
9281da177e4SLinus Torvalds if (!tcon)
9291da177e4SLinus Torvalds goto out;
9301da177e4SLinus Torvalds
931f50a3ec9SKohei Kaigai length = -ENOMEM;
932f50a3ec9SKohei Kaigai namebuf = kzalloc(size + 1, GFP_KERNEL);
933f50a3ec9SKohei Kaigai if (!namebuf)
934b77a493bSEric Paris goto out;
9351da177e4SLinus Torvalds
936f50a3ec9SKohei Kaigai length = -EINVAL;
937f50a3ec9SKohei Kaigai nargs = sscanf(buf, "%s %s %hu %s", scon, tcon, &tclass, namebuf);
938f50a3ec9SKohei Kaigai if (nargs < 3 || nargs > 4)
939f50a3ec9SKohei Kaigai goto out;
9400f7e4c33SKohei Kaigai if (nargs == 4) {
9410f7e4c33SKohei Kaigai /*
9420f7e4c33SKohei Kaigai * If and when the name of new object to be queried contains
9430f7e4c33SKohei Kaigai * either whitespace or multibyte characters, they shall be
9440f7e4c33SKohei Kaigai * encoded based on the percentage-encoding rule.
9450f7e4c33SKohei Kaigai * If not encoded, the sscanf logic picks up only left-half
9463d9047a0SChristian Göttsche * of the supplied name; split by a whitespace unexpectedly.
9470f7e4c33SKohei Kaigai */
9480f7e4c33SKohei Kaigai char *r, *w;
9490f7e4c33SKohei Kaigai int c1, c2;
9500f7e4c33SKohei Kaigai
9510f7e4c33SKohei Kaigai r = w = namebuf;
9520f7e4c33SKohei Kaigai do {
9530f7e4c33SKohei Kaigai c1 = *r++;
9540f7e4c33SKohei Kaigai if (c1 == '+')
9550f7e4c33SKohei Kaigai c1 = ' ';
9560f7e4c33SKohei Kaigai else if (c1 == '%') {
957af7ff2c2SAndy Shevchenko c1 = hex_to_bin(*r++);
958af7ff2c2SAndy Shevchenko if (c1 < 0)
9590f7e4c33SKohei Kaigai goto out;
960af7ff2c2SAndy Shevchenko c2 = hex_to_bin(*r++);
961af7ff2c2SAndy Shevchenko if (c2 < 0)
9620f7e4c33SKohei Kaigai goto out;
9630f7e4c33SKohei Kaigai c1 = (c1 << 4) | c2;
9640f7e4c33SKohei Kaigai }
9650f7e4c33SKohei Kaigai *w++ = c1;
9660f7e4c33SKohei Kaigai } while (c1 != '\0');
9670f7e4c33SKohei Kaigai
968f50a3ec9SKohei Kaigai objname = namebuf;
9690f7e4c33SKohei Kaigai }
970f50a3ec9SKohei Kaigai
971e67b7985SStephen Smalley length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL);
972b77a493bSEric Paris if (length)
973b77a493bSEric Paris goto out;
974b77a493bSEric Paris
975e67b7985SStephen Smalley length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL);
976b77a493bSEric Paris if (length)
977b77a493bSEric Paris goto out;
9781da177e4SLinus Torvalds
979e67b7985SStephen Smalley length = security_transition_sid_user(ssid, tsid, tclass,
9800619f0f5SStephen Smalley objname, &newsid);
981b77a493bSEric Paris if (length)
982b77a493bSEric Paris goto out;
9831da177e4SLinus Torvalds
984e67b7985SStephen Smalley length = security_sid_to_context(newsid, &newcon, &len);
985b77a493bSEric Paris if (length)
986b77a493bSEric Paris goto out;
9871da177e4SLinus Torvalds
988b77a493bSEric Paris length = -ERANGE;
9891da177e4SLinus Torvalds if (len > SIMPLE_TRANSACTION_LIMIT) {
990f8b69a5fSpeter enderborg pr_err("SELinux: %s: context size (%u) exceeds "
991744ba35eSEric Paris "payload max\n", __func__, len);
992b77a493bSEric Paris goto out;
9931da177e4SLinus Torvalds }
9941da177e4SLinus Torvalds
9951da177e4SLinus Torvalds memcpy(buf, newcon, len);
9961da177e4SLinus Torvalds length = len;
9971da177e4SLinus Torvalds out:
998b77a493bSEric Paris kfree(newcon);
999f50a3ec9SKohei Kaigai kfree(namebuf);
1000b77a493bSEric Paris kfree(tcon);
10011da177e4SLinus Torvalds kfree(scon);
10021da177e4SLinus Torvalds return length;
10031da177e4SLinus Torvalds }
10041da177e4SLinus Torvalds
sel_write_relabel(struct file * file,char * buf,size_t size)10051da177e4SLinus Torvalds static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
10061da177e4SLinus Torvalds {
1007b77a493bSEric Paris char *scon = NULL, *tcon = NULL;
10081da177e4SLinus Torvalds u32 ssid, tsid, newsid;
10091da177e4SLinus Torvalds u16 tclass;
10101da177e4SLinus Torvalds ssize_t length;
1011b77a493bSEric Paris char *newcon = NULL;
10121da177e4SLinus Torvalds u32 len;
10131da177e4SLinus Torvalds
1014e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
1015be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_RELABEL,
1016be0554c9SStephen Smalley NULL);
10171da177e4SLinus Torvalds if (length)
1018b77a493bSEric Paris goto out;
10191da177e4SLinus Torvalds
10201da177e4SLinus Torvalds length = -ENOMEM;
102189d155efSJames Morris scon = kzalloc(size + 1, GFP_KERNEL);
10221da177e4SLinus Torvalds if (!scon)
1023b77a493bSEric Paris goto out;
10241da177e4SLinus Torvalds
1025b77a493bSEric Paris length = -ENOMEM;
102689d155efSJames Morris tcon = kzalloc(size + 1, GFP_KERNEL);
10271da177e4SLinus Torvalds if (!tcon)
10281da177e4SLinus Torvalds goto out;
10291da177e4SLinus Torvalds
10301da177e4SLinus Torvalds length = -EINVAL;
10311da177e4SLinus Torvalds if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
1032b77a493bSEric Paris goto out;
10331da177e4SLinus Torvalds
1034e67b7985SStephen Smalley length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL);
1035b77a493bSEric Paris if (length)
1036b77a493bSEric Paris goto out;
1037b77a493bSEric Paris
1038e67b7985SStephen Smalley length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL);
1039b77a493bSEric Paris if (length)
1040b77a493bSEric Paris goto out;
10411da177e4SLinus Torvalds
1042e67b7985SStephen Smalley length = security_change_sid(ssid, tsid, tclass, &newsid);
1043b77a493bSEric Paris if (length)
1044b77a493bSEric Paris goto out;
10451da177e4SLinus Torvalds
1046e67b7985SStephen Smalley length = security_sid_to_context(newsid, &newcon, &len);
1047b77a493bSEric Paris if (length)
1048b77a493bSEric Paris goto out;
10491da177e4SLinus Torvalds
10501da177e4SLinus Torvalds length = -ERANGE;
1051b77a493bSEric Paris if (len > SIMPLE_TRANSACTION_LIMIT)
1052b77a493bSEric Paris goto out;
10531da177e4SLinus Torvalds
10541da177e4SLinus Torvalds memcpy(buf, newcon, len);
10551da177e4SLinus Torvalds length = len;
10561da177e4SLinus Torvalds out:
1057b77a493bSEric Paris kfree(newcon);
1058b77a493bSEric Paris kfree(tcon);
10591da177e4SLinus Torvalds kfree(scon);
10601da177e4SLinus Torvalds return length;
10611da177e4SLinus Torvalds }
10621da177e4SLinus Torvalds
sel_write_user(struct file * file,char * buf,size_t size)10631da177e4SLinus Torvalds static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
10641da177e4SLinus Torvalds {
1065b77a493bSEric Paris char *con = NULL, *user = NULL, *ptr;
1066b77a493bSEric Paris u32 sid, *sids = NULL;
10671da177e4SLinus Torvalds ssize_t length;
10681da177e4SLinus Torvalds char *newcon;
106997842c56SChristian Göttsche int rc;
107097842c56SChristian Göttsche u32 i, len, nsids;
10711da177e4SLinus Torvalds
1072e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
1073be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_USER,
1074be0554c9SStephen Smalley NULL);
10751da177e4SLinus Torvalds if (length)
10766eab04a8SJustin P. Mattock goto out;
10771da177e4SLinus Torvalds
10781da177e4SLinus Torvalds length = -ENOMEM;
107989d155efSJames Morris con = kzalloc(size + 1, GFP_KERNEL);
10801da177e4SLinus Torvalds if (!con)
10816eab04a8SJustin P. Mattock goto out;
10821da177e4SLinus Torvalds
1083b77a493bSEric Paris length = -ENOMEM;
108489d155efSJames Morris user = kzalloc(size + 1, GFP_KERNEL);
10851da177e4SLinus Torvalds if (!user)
10861da177e4SLinus Torvalds goto out;
10871da177e4SLinus Torvalds
10881da177e4SLinus Torvalds length = -EINVAL;
10891da177e4SLinus Torvalds if (sscanf(buf, "%s %s", con, user) != 2)
1090b77a493bSEric Paris goto out;
10911da177e4SLinus Torvalds
1092e67b7985SStephen Smalley length = security_context_str_to_sid(con, &sid, GFP_KERNEL);
1093b77a493bSEric Paris if (length)
1094b77a493bSEric Paris goto out;
10951da177e4SLinus Torvalds
1096e67b7985SStephen Smalley length = security_get_user_sids(sid, user, &sids, &nsids);
1097b77a493bSEric Paris if (length)
1098b77a493bSEric Paris goto out;
10991da177e4SLinus Torvalds
11001da177e4SLinus Torvalds length = sprintf(buf, "%u", nsids) + 1;
11011da177e4SLinus Torvalds ptr = buf + length;
11021da177e4SLinus Torvalds for (i = 0; i < nsids; i++) {
1103e67b7985SStephen Smalley rc = security_sid_to_context(sids[i], &newcon, &len);
11041da177e4SLinus Torvalds if (rc) {
11051da177e4SLinus Torvalds length = rc;
1106b77a493bSEric Paris goto out;
11071da177e4SLinus Torvalds }
11081da177e4SLinus Torvalds if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
11091da177e4SLinus Torvalds kfree(newcon);
11101da177e4SLinus Torvalds length = -ERANGE;
1111b77a493bSEric Paris goto out;
11121da177e4SLinus Torvalds }
11131da177e4SLinus Torvalds memcpy(ptr, newcon, len);
11141da177e4SLinus Torvalds kfree(newcon);
11151da177e4SLinus Torvalds ptr += len;
11161da177e4SLinus Torvalds length += len;
11171da177e4SLinus Torvalds }
11181da177e4SLinus Torvalds out:
1119b77a493bSEric Paris kfree(sids);
1120b77a493bSEric Paris kfree(user);
11211da177e4SLinus Torvalds kfree(con);
11221da177e4SLinus Torvalds return length;
11231da177e4SLinus Torvalds }
11241da177e4SLinus Torvalds
sel_write_member(struct file * file,char * buf,size_t size)11251da177e4SLinus Torvalds static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
11261da177e4SLinus Torvalds {
1127b77a493bSEric Paris char *scon = NULL, *tcon = NULL;
11281da177e4SLinus Torvalds u32 ssid, tsid, newsid;
11291da177e4SLinus Torvalds u16 tclass;
11301da177e4SLinus Torvalds ssize_t length;
1131b77a493bSEric Paris char *newcon = NULL;
11321da177e4SLinus Torvalds u32 len;
11331da177e4SLinus Torvalds
1134e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
1135be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__COMPUTE_MEMBER,
1136be0554c9SStephen Smalley NULL);
11371da177e4SLinus Torvalds if (length)
1138b77a493bSEric Paris goto out;
11391da177e4SLinus Torvalds
11401da177e4SLinus Torvalds length = -ENOMEM;
114189d155efSJames Morris scon = kzalloc(size + 1, GFP_KERNEL);
11421da177e4SLinus Torvalds if (!scon)
11436eab04a8SJustin P. Mattock goto out;
11441da177e4SLinus Torvalds
1145b77a493bSEric Paris length = -ENOMEM;
114689d155efSJames Morris tcon = kzalloc(size + 1, GFP_KERNEL);
11471da177e4SLinus Torvalds if (!tcon)
11481da177e4SLinus Torvalds goto out;
11491da177e4SLinus Torvalds
11501da177e4SLinus Torvalds length = -EINVAL;
11511da177e4SLinus Torvalds if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
1152b77a493bSEric Paris goto out;
11531da177e4SLinus Torvalds
1154e67b7985SStephen Smalley length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL);
1155b77a493bSEric Paris if (length)
1156b77a493bSEric Paris goto out;
1157b77a493bSEric Paris
1158e67b7985SStephen Smalley length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL);
1159b77a493bSEric Paris if (length)
1160b77a493bSEric Paris goto out;
11611da177e4SLinus Torvalds
1162e67b7985SStephen Smalley length = security_member_sid(ssid, tsid, tclass, &newsid);
1163b77a493bSEric Paris if (length)
1164b77a493bSEric Paris goto out;
11651da177e4SLinus Torvalds
1166e67b7985SStephen Smalley length = security_sid_to_context(newsid, &newcon, &len);
1167b77a493bSEric Paris if (length)
1168b77a493bSEric Paris goto out;
11691da177e4SLinus Torvalds
1170b77a493bSEric Paris length = -ERANGE;
11711da177e4SLinus Torvalds if (len > SIMPLE_TRANSACTION_LIMIT) {
1172f8b69a5fSpeter enderborg pr_err("SELinux: %s: context size (%u) exceeds "
1173744ba35eSEric Paris "payload max\n", __func__, len);
1174b77a493bSEric Paris goto out;
11751da177e4SLinus Torvalds }
11761da177e4SLinus Torvalds
11771da177e4SLinus Torvalds memcpy(buf, newcon, len);
11781da177e4SLinus Torvalds length = len;
11791da177e4SLinus Torvalds out:
1180b77a493bSEric Paris kfree(newcon);
1181b77a493bSEric Paris kfree(tcon);
11821da177e4SLinus Torvalds kfree(scon);
11831da177e4SLinus Torvalds return length;
11841da177e4SLinus Torvalds }
11851da177e4SLinus Torvalds
sel_make_inode(struct super_block * sb,umode_t mode)118697842c56SChristian Göttsche static struct inode *sel_make_inode(struct super_block *sb, umode_t mode)
11871da177e4SLinus Torvalds {
11881da177e4SLinus Torvalds struct inode *ret = new_inode(sb);
11891da177e4SLinus Torvalds
11901da177e4SLinus Torvalds if (ret) {
11911da177e4SLinus Torvalds ret->i_mode = mode;
119226d12831SJeff Layton simple_inode_init_ts(ret);
11931da177e4SLinus Torvalds }
11941da177e4SLinus Torvalds return ret;
11951da177e4SLinus Torvalds }
11961da177e4SLinus Torvalds
sel_read_bool(struct file * filep,char __user * buf,size_t count,loff_t * ppos)11971da177e4SLinus Torvalds static ssize_t sel_read_bool(struct file *filep, char __user *buf,
11981da177e4SLinus Torvalds size_t count, loff_t *ppos)
11991da177e4SLinus Torvalds {
12000619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
12011da177e4SLinus Torvalds char *page = NULL;
12021da177e4SLinus Torvalds ssize_t length;
12031da177e4SLinus Torvalds ssize_t ret;
12041da177e4SLinus Torvalds int cur_enforcing;
1205496ad9aaSAl Viro unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
1206d313f948SStephen Smalley const char *name = filep->f_path.dentry->d_name.name;
12071da177e4SLinus Torvalds
1208e67b7985SStephen Smalley mutex_lock(&selinux_state.policy_mutex);
12091da177e4SLinus Torvalds
1210d313f948SStephen Smalley ret = -EINVAL;
12110619f0f5SStephen Smalley if (index >= fsi->bool_num || strcmp(name,
12120619f0f5SStephen Smalley fsi->bool_pending_names[index]))
12130da74120SJann Horn goto out_unlock;
12141da177e4SLinus Torvalds
12151da177e4SLinus Torvalds ret = -ENOMEM;
1216b77a493bSEric Paris page = (char *)get_zeroed_page(GFP_KERNEL);
1217b77a493bSEric Paris if (!page)
12180da74120SJann Horn goto out_unlock;
12191da177e4SLinus Torvalds
1220e67b7985SStephen Smalley cur_enforcing = security_get_bool_value(index);
12211da177e4SLinus Torvalds if (cur_enforcing < 0) {
12221da177e4SLinus Torvalds ret = cur_enforcing;
12230da74120SJann Horn goto out_unlock;
12241da177e4SLinus Torvalds }
12251da177e4SLinus Torvalds length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
12260619f0f5SStephen Smalley fsi->bool_pending_values[index]);
1227e67b7985SStephen Smalley mutex_unlock(&selinux_state.policy_mutex);
12280da74120SJann Horn ret = simple_read_from_buffer(buf, count, ppos, page, length);
12290da74120SJann Horn out_free:
12301da177e4SLinus Torvalds free_page((unsigned long)page);
12311da177e4SLinus Torvalds return ret;
12320da74120SJann Horn
12330da74120SJann Horn out_unlock:
1234e67b7985SStephen Smalley mutex_unlock(&selinux_state.policy_mutex);
12350da74120SJann Horn goto out_free;
12361da177e4SLinus Torvalds }
12371da177e4SLinus Torvalds
sel_write_bool(struct file * filep,const char __user * buf,size_t count,loff_t * ppos)12381da177e4SLinus Torvalds static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
12391da177e4SLinus Torvalds size_t count, loff_t *ppos)
12401da177e4SLinus Torvalds {
12410619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
12421da177e4SLinus Torvalds char *page = NULL;
1243d313f948SStephen Smalley ssize_t length;
12441da177e4SLinus Torvalds int new_value;
1245496ad9aaSAl Viro unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
1246d313f948SStephen Smalley const char *name = filep->f_path.dentry->d_name.name;
12471da177e4SLinus Torvalds
12480da74120SJann Horn if (count >= PAGE_SIZE)
12490da74120SJann Horn return -ENOMEM;
12500da74120SJann Horn
12510da74120SJann Horn /* No partial writes. */
12520da74120SJann Horn if (*ppos != 0)
12530da74120SJann Horn return -EINVAL;
12540da74120SJann Horn
12550da74120SJann Horn page = memdup_user_nul(buf, count);
12560da74120SJann Horn if (IS_ERR(page))
12570da74120SJann Horn return PTR_ERR(page);
12580da74120SJann Horn
1259e67b7985SStephen Smalley mutex_lock(&selinux_state.policy_mutex);
12601da177e4SLinus Torvalds
1261e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
1262be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETBOOL,
1263be0554c9SStephen Smalley NULL);
12641da177e4SLinus Torvalds if (length)
12651da177e4SLinus Torvalds goto out;
12661da177e4SLinus Torvalds
1267d313f948SStephen Smalley length = -EINVAL;
12680619f0f5SStephen Smalley if (index >= fsi->bool_num || strcmp(name,
12690619f0f5SStephen Smalley fsi->bool_pending_names[index]))
1270d313f948SStephen Smalley goto out;
1271d313f948SStephen Smalley
12721da177e4SLinus Torvalds length = -EINVAL;
12731da177e4SLinus Torvalds if (sscanf(page, "%d", &new_value) != 1)
12741da177e4SLinus Torvalds goto out;
12751da177e4SLinus Torvalds
12761da177e4SLinus Torvalds if (new_value)
12771da177e4SLinus Torvalds new_value = 1;
12781da177e4SLinus Torvalds
12790619f0f5SStephen Smalley fsi->bool_pending_values[index] = new_value;
12801da177e4SLinus Torvalds length = count;
12811da177e4SLinus Torvalds
12821da177e4SLinus Torvalds out:
1283e67b7985SStephen Smalley mutex_unlock(&selinux_state.policy_mutex);
12848365a719SAl Viro kfree(page);
12851da177e4SLinus Torvalds return length;
12861da177e4SLinus Torvalds }
12871da177e4SLinus Torvalds
12889c2e08c5SArjan van de Ven static const struct file_operations sel_bool_ops = {
12891da177e4SLinus Torvalds .read = sel_read_bool,
12901da177e4SLinus Torvalds .write = sel_write_bool,
129157a62c23SArnd Bergmann .llseek = generic_file_llseek,
12921da177e4SLinus Torvalds };
12931da177e4SLinus Torvalds
sel_commit_bools_write(struct file * filep,const char __user * buf,size_t count,loff_t * ppos)12941da177e4SLinus Torvalds static ssize_t sel_commit_bools_write(struct file *filep,
12951da177e4SLinus Torvalds const char __user *buf,
12961da177e4SLinus Torvalds size_t count, loff_t *ppos)
12971da177e4SLinus Torvalds {
12980619f0f5SStephen Smalley struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
12991da177e4SLinus Torvalds char *page = NULL;
1300d313f948SStephen Smalley ssize_t length;
13011da177e4SLinus Torvalds int new_value;
13021da177e4SLinus Torvalds
13030da74120SJann Horn if (count >= PAGE_SIZE)
13040da74120SJann Horn return -ENOMEM;
13050da74120SJann Horn
13060da74120SJann Horn /* No partial writes. */
13070da74120SJann Horn if (*ppos != 0)
13080da74120SJann Horn return -EINVAL;
13090da74120SJann Horn
13100da74120SJann Horn page = memdup_user_nul(buf, count);
13110da74120SJann Horn if (IS_ERR(page))
13120da74120SJann Horn return PTR_ERR(page);
13130da74120SJann Horn
1314e67b7985SStephen Smalley mutex_lock(&selinux_state.policy_mutex);
13151da177e4SLinus Torvalds
1316e67b7985SStephen Smalley length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
1317be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETBOOL,
1318be0554c9SStephen Smalley NULL);
13191da177e4SLinus Torvalds if (length)
13201da177e4SLinus Torvalds goto out;
13211da177e4SLinus Torvalds
13221da177e4SLinus Torvalds length = -EINVAL;
13231da177e4SLinus Torvalds if (sscanf(page, "%d", &new_value) != 1)
13241da177e4SLinus Torvalds goto out;
13251da177e4SLinus Torvalds
1326b77a493bSEric Paris length = 0;
13270619f0f5SStephen Smalley if (new_value && fsi->bool_pending_values)
1328e67b7985SStephen Smalley length = security_set_bools(fsi->bool_num,
13290619f0f5SStephen Smalley fsi->bool_pending_values);
13301da177e4SLinus Torvalds
1331b77a493bSEric Paris if (!length)
13321da177e4SLinus Torvalds length = count;
13331da177e4SLinus Torvalds
13341da177e4SLinus Torvalds out:
1335e67b7985SStephen Smalley mutex_unlock(&selinux_state.policy_mutex);
13368365a719SAl Viro kfree(page);
13371da177e4SLinus Torvalds return length;
13381da177e4SLinus Torvalds }
13391da177e4SLinus Torvalds
13409c2e08c5SArjan van de Ven static const struct file_operations sel_commit_bools_ops = {
13411da177e4SLinus Torvalds .write = sel_commit_bools_write,
134257a62c23SArnd Bergmann .llseek = generic_file_llseek,
13431da177e4SLinus Torvalds };
13441da177e4SLinus Torvalds
sel_make_bools(struct selinux_policy * newpolicy,struct dentry * bool_dir,unsigned int * bool_num,char *** bool_pending_names,int ** bool_pending_values)134566ec384aSDaniel Burgener static int sel_make_bools(struct selinux_policy *newpolicy, struct dentry *bool_dir,
134666ec384aSDaniel Burgener unsigned int *bool_num, char ***bool_pending_names,
1347c3fae2b2SChristian Göttsche int **bool_pending_values)
13481da177e4SLinus Torvalds {
134960abd318SOndrej Mosnacek int ret;
13504a0b33f7SAl Viro char **names, *page;
135160abd318SOndrej Mosnacek u32 i, num;
13521da177e4SLinus Torvalds
13531872981bSEric Paris page = (char *)get_zeroed_page(GFP_KERNEL);
13541872981bSEric Paris if (!page)
13554a0b33f7SAl Viro return -ENOMEM;
13561da177e4SLinus Torvalds
13574a0b33f7SAl Viro ret = security_get_bools(newpolicy, &num, &names, bool_pending_values);
1358b77a493bSEric Paris if (ret)
13591da177e4SLinus Torvalds goto out;
13601da177e4SLinus Torvalds
13614a0b33f7SAl Viro *bool_num = num;
13624a0b33f7SAl Viro *bool_pending_names = names;
13631da177e4SLinus Torvalds
13644a0b33f7SAl Viro for (i = 0; i < num; i++) {
13654a0b33f7SAl Viro struct dentry *dentry;
13664a0b33f7SAl Viro struct inode *inode;
13674a0b33f7SAl Viro struct inode_security_struct *isec;
13684a0b33f7SAl Viro ssize_t len;
13694a0b33f7SAl Viro u32 sid;
13704a0b33f7SAl Viro
13714a0b33f7SAl Viro len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
13724a0b33f7SAl Viro if (len >= PAGE_SIZE) {
13734a0b33f7SAl Viro ret = -ENAMETOOLONG;
13744a0b33f7SAl Viro break;
13754a0b33f7SAl Viro }
13764a0b33f7SAl Viro dentry = d_alloc_name(bool_dir, names[i]);
13774a0b33f7SAl Viro if (!dentry) {
1378b77a493bSEric Paris ret = -ENOMEM;
13794a0b33f7SAl Viro break;
13804a0b33f7SAl Viro }
13814a0b33f7SAl Viro
138266ec384aSDaniel Burgener inode = sel_make_inode(bool_dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
13837e4237faSnixiaoming if (!inode) {
13847e4237faSnixiaoming dput(dentry);
13854a0b33f7SAl Viro ret = -ENOMEM;
13864a0b33f7SAl Viro break;
13877e4237faSnixiaoming }
1388b77a493bSEric Paris
138980788c22SCasey Schaufler isec = selinux_inode(inode);
139002a52c5cSStephen Smalley ret = selinux_policy_genfs_sid(newpolicy, "selinuxfs", page,
1391aa8e712cSStephen Smalley SECCLASS_FILE, &sid);
13924262fb51SGary Tierney if (ret) {
1393900fde06SGary Tierney pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n",
1394900fde06SGary Tierney page);
1395900fde06SGary Tierney sid = SECINITSID_SECURITY;
13964262fb51SGary Tierney }
13974262fb51SGary Tierney
13981da177e4SLinus Torvalds isec->sid = sid;
139942059112SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED;
14001da177e4SLinus Torvalds inode->i_fop = &sel_bool_ops;
1401bce34bc0SJames Carter inode->i_ino = i|SEL_BOOL_INO_OFFSET;
14021da177e4SLinus Torvalds d_add(dentry, inode);
14031da177e4SLinus Torvalds }
14041da177e4SLinus Torvalds out:
14051da177e4SLinus Torvalds free_page((unsigned long)page);
1406b77a493bSEric Paris return ret;
14071da177e4SLinus Torvalds }
14081da177e4SLinus Torvalds
sel_read_avc_cache_threshold(struct file * filp,char __user * buf,size_t count,loff_t * ppos)14091da177e4SLinus Torvalds static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
14101da177e4SLinus Torvalds size_t count, loff_t *ppos)
14111da177e4SLinus Torvalds {
14121da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN];
14131da177e4SLinus Torvalds ssize_t length;
14141da177e4SLinus Torvalds
14156b6bc620SStephen Smalley length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1416e67b7985SStephen Smalley avc_get_cache_threshold());
14171da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
14181da177e4SLinus Torvalds }
14191da177e4SLinus Torvalds
sel_write_avc_cache_threshold(struct file * file,const char __user * buf,size_t count,loff_t * ppos)14201da177e4SLinus Torvalds static ssize_t sel_write_avc_cache_threshold(struct file *file,
14211da177e4SLinus Torvalds const char __user *buf,
14221da177e4SLinus Torvalds size_t count, loff_t *ppos)
14231da177e4SLinus Torvalds
14241da177e4SLinus Torvalds {
14258365a719SAl Viro char *page;
14261da177e4SLinus Torvalds ssize_t ret;
1427309c5fadSHeinrich Schuchardt unsigned int new_value;
14281da177e4SLinus Torvalds
1429e67b7985SStephen Smalley ret = avc_has_perm(current_sid(), SECINITSID_SECURITY,
1430be0554c9SStephen Smalley SECCLASS_SECURITY, SECURITY__SETSECPARAM,
1431be0554c9SStephen Smalley NULL);
14321da177e4SLinus Torvalds if (ret)
14338365a719SAl Viro return ret;
1434b77a493bSEric Paris
1435b77a493bSEric Paris if (count >= PAGE_SIZE)
14368365a719SAl Viro return -ENOMEM;
1437b77a493bSEric Paris
1438b77a493bSEric Paris /* No partial writes. */
1439b77a493bSEric Paris if (*ppos != 0)
14408365a719SAl Viro return -EINVAL;
1441b77a493bSEric Paris
14428365a719SAl Viro page = memdup_user_nul(buf, count);
14438365a719SAl Viro if (IS_ERR(page))
14448365a719SAl Viro return PTR_ERR(page);
1445b77a493bSEric Paris
1446b77a493bSEric Paris ret = -EINVAL;
1447b77a493bSEric Paris if (sscanf(page, "%u", &new_value) != 1)
1448b77a493bSEric Paris goto out;
1449b77a493bSEric Paris
1450e67b7985SStephen Smalley avc_set_cache_threshold(new_value);
1451b77a493bSEric Paris
14521da177e4SLinus Torvalds ret = count;
14531da177e4SLinus Torvalds out:
14548365a719SAl Viro kfree(page);
14551da177e4SLinus Torvalds return ret;
14561da177e4SLinus Torvalds }
14571da177e4SLinus Torvalds
sel_read_avc_hash_stats(struct file * filp,char __user * buf,size_t count,loff_t * ppos)14581da177e4SLinus Torvalds static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
14591da177e4SLinus Torvalds size_t count, loff_t *ppos)
14601da177e4SLinus Torvalds {
14611da177e4SLinus Torvalds char *page;
1462b77a493bSEric Paris ssize_t length;
14631da177e4SLinus Torvalds
14641da177e4SLinus Torvalds page = (char *)__get_free_page(GFP_KERNEL);
1465b77a493bSEric Paris if (!page)
1466b77a493bSEric Paris return -ENOMEM;
1467b77a493bSEric Paris
1468e67b7985SStephen Smalley length = avc_get_hash_stats(page);
1469b77a493bSEric Paris if (length >= 0)
1470b77a493bSEric Paris length = simple_read_from_buffer(buf, count, ppos, page, length);
14711da177e4SLinus Torvalds free_page((unsigned long)page);
1472b77a493bSEric Paris
1473b77a493bSEric Paris return length;
14741da177e4SLinus Torvalds }
14751da177e4SLinus Torvalds
sel_read_sidtab_hash_stats(struct file * filp,char __user * buf,size_t count,loff_t * ppos)147666f8e2f0SJeff Vander Stoep static ssize_t sel_read_sidtab_hash_stats(struct file *filp, char __user *buf,
147766f8e2f0SJeff Vander Stoep size_t count, loff_t *ppos)
147866f8e2f0SJeff Vander Stoep {
147966f8e2f0SJeff Vander Stoep char *page;
148066f8e2f0SJeff Vander Stoep ssize_t length;
148166f8e2f0SJeff Vander Stoep
148266f8e2f0SJeff Vander Stoep page = (char *)__get_free_page(GFP_KERNEL);
148366f8e2f0SJeff Vander Stoep if (!page)
148466f8e2f0SJeff Vander Stoep return -ENOMEM;
148566f8e2f0SJeff Vander Stoep
1486e67b7985SStephen Smalley length = security_sidtab_hash_stats(page);
148766f8e2f0SJeff Vander Stoep if (length >= 0)
148866f8e2f0SJeff Vander Stoep length = simple_read_from_buffer(buf, count, ppos, page,
148966f8e2f0SJeff Vander Stoep length);
149066f8e2f0SJeff Vander Stoep free_page((unsigned long)page);
149166f8e2f0SJeff Vander Stoep
149266f8e2f0SJeff Vander Stoep return length;
149366f8e2f0SJeff Vander Stoep }
149466f8e2f0SJeff Vander Stoep
149566f8e2f0SJeff Vander Stoep static const struct file_operations sel_sidtab_hash_stats_ops = {
149666f8e2f0SJeff Vander Stoep .read = sel_read_sidtab_hash_stats,
149766f8e2f0SJeff Vander Stoep .llseek = generic_file_llseek,
149866f8e2f0SJeff Vander Stoep };
149966f8e2f0SJeff Vander Stoep
15009c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_threshold_ops = {
15011da177e4SLinus Torvalds .read = sel_read_avc_cache_threshold,
15021da177e4SLinus Torvalds .write = sel_write_avc_cache_threshold,
150357a62c23SArnd Bergmann .llseek = generic_file_llseek,
15041da177e4SLinus Torvalds };
15051da177e4SLinus Torvalds
15069c2e08c5SArjan van de Ven static const struct file_operations sel_avc_hash_stats_ops = {
15071da177e4SLinus Torvalds .read = sel_read_avc_hash_stats,
150857a62c23SArnd Bergmann .llseek = generic_file_llseek,
15091da177e4SLinus Torvalds };
15101da177e4SLinus Torvalds
15111da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
sel_avc_get_stat_idx(loff_t * idx)15121da177e4SLinus Torvalds static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
15131da177e4SLinus Torvalds {
15141da177e4SLinus Torvalds int cpu;
15151da177e4SLinus Torvalds
15164f4b6c1aSRusty Russell for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
15171da177e4SLinus Torvalds if (!cpu_possible(cpu))
15181da177e4SLinus Torvalds continue;
15191da177e4SLinus Torvalds *idx = cpu + 1;
15201da177e4SLinus Torvalds return &per_cpu(avc_cache_stats, cpu);
15211da177e4SLinus Torvalds }
15228d269a8eSVasily Averin (*idx)++;
15231da177e4SLinus Torvalds return NULL;
15241da177e4SLinus Torvalds }
15251da177e4SLinus Torvalds
sel_avc_stats_seq_start(struct seq_file * seq,loff_t * pos)15261da177e4SLinus Torvalds static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
15271da177e4SLinus Torvalds {
15281da177e4SLinus Torvalds loff_t n = *pos - 1;
15291da177e4SLinus Torvalds
15301da177e4SLinus Torvalds if (*pos == 0)
15311da177e4SLinus Torvalds return SEQ_START_TOKEN;
15321da177e4SLinus Torvalds
15331da177e4SLinus Torvalds return sel_avc_get_stat_idx(&n);
15341da177e4SLinus Torvalds }
15351da177e4SLinus Torvalds
sel_avc_stats_seq_next(struct seq_file * seq,void * v,loff_t * pos)15361da177e4SLinus Torvalds static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
15371da177e4SLinus Torvalds {
15381da177e4SLinus Torvalds return sel_avc_get_stat_idx(pos);
15391da177e4SLinus Torvalds }
15401da177e4SLinus Torvalds
sel_avc_stats_seq_show(struct seq_file * seq,void * v)15411da177e4SLinus Torvalds static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
15421da177e4SLinus Torvalds {
15431da177e4SLinus Torvalds struct avc_cache_stats *st = v;
15441da177e4SLinus Torvalds
1545710a0647SMarkus Elfring if (v == SEQ_START_TOKEN) {
1546710a0647SMarkus Elfring seq_puts(seq,
1547710a0647SMarkus Elfring "lookups hits misses allocations reclaims frees\n");
1548710a0647SMarkus Elfring } else {
1549257313b2SLinus Torvalds unsigned int lookups = st->lookups;
1550257313b2SLinus Torvalds unsigned int misses = st->misses;
1551257313b2SLinus Torvalds unsigned int hits = lookups - misses;
1552257313b2SLinus Torvalds seq_printf(seq, "%u %u %u %u %u %u\n", lookups,
1553257313b2SLinus Torvalds hits, misses, st->allocations,
15541da177e4SLinus Torvalds st->reclaims, st->frees);
1555257313b2SLinus Torvalds }
15561da177e4SLinus Torvalds return 0;
15571da177e4SLinus Torvalds }
15581da177e4SLinus Torvalds
sel_avc_stats_seq_stop(struct seq_file * seq,void * v)15591da177e4SLinus Torvalds static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
15601da177e4SLinus Torvalds { }
15611da177e4SLinus Torvalds
15621996a109SJan Engelhardt static const struct seq_operations sel_avc_cache_stats_seq_ops = {
15631da177e4SLinus Torvalds .start = sel_avc_stats_seq_start,
15641da177e4SLinus Torvalds .next = sel_avc_stats_seq_next,
15651da177e4SLinus Torvalds .show = sel_avc_stats_seq_show,
15661da177e4SLinus Torvalds .stop = sel_avc_stats_seq_stop,
15671da177e4SLinus Torvalds };
15681da177e4SLinus Torvalds
sel_open_avc_cache_stats(struct inode * inode,struct file * file)15691da177e4SLinus Torvalds static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
15701da177e4SLinus Torvalds {
15711da177e4SLinus Torvalds return seq_open(file, &sel_avc_cache_stats_seq_ops);
15721da177e4SLinus Torvalds }
15731da177e4SLinus Torvalds
15749c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_stats_ops = {
15751da177e4SLinus Torvalds .open = sel_open_avc_cache_stats,
15761da177e4SLinus Torvalds .read = seq_read,
15771da177e4SLinus Torvalds .llseek = seq_lseek,
15781da177e4SLinus Torvalds .release = seq_release,
15791da177e4SLinus Torvalds };
15801da177e4SLinus Torvalds #endif
15811da177e4SLinus Torvalds
sel_make_avc_files(struct dentry * dir)15821da177e4SLinus Torvalds static int sel_make_avc_files(struct dentry *dir)
15831da177e4SLinus Torvalds {
15840619f0f5SStephen Smalley struct super_block *sb = dir->d_sb;
15850619f0f5SStephen Smalley struct selinux_fs_info *fsi = sb->s_fs_info;
158697842c56SChristian Göttsche unsigned int i;
1587cda37124SEric Biggers static const struct tree_descr files[] = {
15881da177e4SLinus Torvalds { "cache_threshold",
15891da177e4SLinus Torvalds &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
15901da177e4SLinus Torvalds { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
15911da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
15921da177e4SLinus Torvalds { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
15931da177e4SLinus Torvalds #endif
15941da177e4SLinus Torvalds };
15951da177e4SLinus Torvalds
15966e20a64aSNicolas Kaiser for (i = 0; i < ARRAY_SIZE(files); i++) {
15971da177e4SLinus Torvalds struct inode *inode;
15981da177e4SLinus Torvalds struct dentry *dentry;
15991da177e4SLinus Torvalds
16001da177e4SLinus Torvalds dentry = d_alloc_name(dir, files[i].name);
1601b77a493bSEric Paris if (!dentry)
1602b77a493bSEric Paris return -ENOMEM;
16031da177e4SLinus Torvalds
16041da177e4SLinus Torvalds inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
16057e4237faSnixiaoming if (!inode) {
16067e4237faSnixiaoming dput(dentry);
1607b77a493bSEric Paris return -ENOMEM;
16087e4237faSnixiaoming }
1609b77a493bSEric Paris
16101da177e4SLinus Torvalds inode->i_fop = files[i].ops;
16110619f0f5SStephen Smalley inode->i_ino = ++fsi->last_ino;
16121da177e4SLinus Torvalds d_add(dentry, inode);
16131da177e4SLinus Torvalds }
1614b77a493bSEric Paris
1615b77a493bSEric Paris return 0;
16161da177e4SLinus Torvalds }
16171da177e4SLinus Torvalds
sel_make_ss_files(struct dentry * dir)161866f8e2f0SJeff Vander Stoep static int sel_make_ss_files(struct dentry *dir)
161966f8e2f0SJeff Vander Stoep {
162066f8e2f0SJeff Vander Stoep struct super_block *sb = dir->d_sb;
162166f8e2f0SJeff Vander Stoep struct selinux_fs_info *fsi = sb->s_fs_info;
162297842c56SChristian Göttsche unsigned int i;
16234158cb60SChristian Göttsche static const struct tree_descr files[] = {
162466f8e2f0SJeff Vander Stoep { "sidtab_hash_stats", &sel_sidtab_hash_stats_ops, S_IRUGO },
162566f8e2f0SJeff Vander Stoep };
162666f8e2f0SJeff Vander Stoep
162766f8e2f0SJeff Vander Stoep for (i = 0; i < ARRAY_SIZE(files); i++) {
162866f8e2f0SJeff Vander Stoep struct inode *inode;
162966f8e2f0SJeff Vander Stoep struct dentry *dentry;
163066f8e2f0SJeff Vander Stoep
163166f8e2f0SJeff Vander Stoep dentry = d_alloc_name(dir, files[i].name);
163266f8e2f0SJeff Vander Stoep if (!dentry)
163366f8e2f0SJeff Vander Stoep return -ENOMEM;
163466f8e2f0SJeff Vander Stoep
163566f8e2f0SJeff Vander Stoep inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
163666f8e2f0SJeff Vander Stoep if (!inode) {
163766f8e2f0SJeff Vander Stoep dput(dentry);
163866f8e2f0SJeff Vander Stoep return -ENOMEM;
163966f8e2f0SJeff Vander Stoep }
164066f8e2f0SJeff Vander Stoep
164166f8e2f0SJeff Vander Stoep inode->i_fop = files[i].ops;
164266f8e2f0SJeff Vander Stoep inode->i_ino = ++fsi->last_ino;
164366f8e2f0SJeff Vander Stoep d_add(dentry, inode);
164466f8e2f0SJeff Vander Stoep }
164566f8e2f0SJeff Vander Stoep
164666f8e2f0SJeff Vander Stoep return 0;
164766f8e2f0SJeff Vander Stoep }
164866f8e2f0SJeff Vander Stoep
sel_read_initcon(struct file * file,char __user * buf,size_t count,loff_t * ppos)1649f0ee2e46SJames Carter static ssize_t sel_read_initcon(struct file *file, char __user *buf,
1650f0ee2e46SJames Carter size_t count, loff_t *ppos)
1651f0ee2e46SJames Carter {
1652f0ee2e46SJames Carter char *con;
1653f0ee2e46SJames Carter u32 sid, len;
1654f0ee2e46SJames Carter ssize_t ret;
1655f0ee2e46SJames Carter
1656496ad9aaSAl Viro sid = file_inode(file)->i_ino&SEL_INO_MASK;
1657e67b7985SStephen Smalley ret = security_sid_to_context(sid, &con, &len);
1658b77a493bSEric Paris if (ret)
1659f0ee2e46SJames Carter return ret;
1660f0ee2e46SJames Carter
1661f0ee2e46SJames Carter ret = simple_read_from_buffer(buf, count, ppos, con, len);
1662f0ee2e46SJames Carter kfree(con);
1663f0ee2e46SJames Carter return ret;
1664f0ee2e46SJames Carter }
1665f0ee2e46SJames Carter
1666f0ee2e46SJames Carter static const struct file_operations sel_initcon_ops = {
1667f0ee2e46SJames Carter .read = sel_read_initcon,
166857a62c23SArnd Bergmann .llseek = generic_file_llseek,
1669f0ee2e46SJames Carter };
1670f0ee2e46SJames Carter
sel_make_initcon_files(struct dentry * dir)1671f0ee2e46SJames Carter static int sel_make_initcon_files(struct dentry *dir)
1672f0ee2e46SJames Carter {
167397842c56SChristian Göttsche unsigned int i;
1674f0ee2e46SJames Carter
1675f0ee2e46SJames Carter for (i = 1; i <= SECINITSID_NUM; i++) {
1676f0ee2e46SJames Carter struct inode *inode;
1677f0ee2e46SJames Carter struct dentry *dentry;
1678e3e0b582SStephen Smalley const char *s = security_get_initial_sid_context(i);
1679e3e0b582SStephen Smalley
1680e3e0b582SStephen Smalley if (!s)
1681e3e0b582SStephen Smalley continue;
1682e3e0b582SStephen Smalley dentry = d_alloc_name(dir, s);
1683b77a493bSEric Paris if (!dentry)
1684b77a493bSEric Paris return -ENOMEM;
1685f0ee2e46SJames Carter
1686f0ee2e46SJames Carter inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
16877e4237faSnixiaoming if (!inode) {
16887e4237faSnixiaoming dput(dentry);
1689b77a493bSEric Paris return -ENOMEM;
16907e4237faSnixiaoming }
1691b77a493bSEric Paris
1692f0ee2e46SJames Carter inode->i_fop = &sel_initcon_ops;
1693f0ee2e46SJames Carter inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1694f0ee2e46SJames Carter d_add(dentry, inode);
1695f0ee2e46SJames Carter }
1696b77a493bSEric Paris
1697b77a493bSEric Paris return 0;
1698f0ee2e46SJames Carter }
1699f0ee2e46SJames Carter
sel_class_to_ino(u16 class)1700e47c8fc5SChristopher J. PeBenito static inline unsigned long sel_class_to_ino(u16 class)
1701e47c8fc5SChristopher J. PeBenito {
1702e47c8fc5SChristopher J. PeBenito return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1703e47c8fc5SChristopher J. PeBenito }
1704e47c8fc5SChristopher J. PeBenito
sel_ino_to_class(unsigned long ino)1705e47c8fc5SChristopher J. PeBenito static inline u16 sel_ino_to_class(unsigned long ino)
1706e47c8fc5SChristopher J. PeBenito {
170792ae9e82SEric Paris return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1);
1708e47c8fc5SChristopher J. PeBenito }
1709e47c8fc5SChristopher J. PeBenito
sel_perm_to_ino(u16 class,u32 perm)1710e47c8fc5SChristopher J. PeBenito static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1711e47c8fc5SChristopher J. PeBenito {
1712e47c8fc5SChristopher J. PeBenito return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1713e47c8fc5SChristopher J. PeBenito }
1714e47c8fc5SChristopher J. PeBenito
sel_ino_to_perm(unsigned long ino)1715e47c8fc5SChristopher J. PeBenito static inline u32 sel_ino_to_perm(unsigned long ino)
1716e47c8fc5SChristopher J. PeBenito {
1717e47c8fc5SChristopher J. PeBenito return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1718e47c8fc5SChristopher J. PeBenito }
1719e47c8fc5SChristopher J. PeBenito
sel_read_class(struct file * file,char __user * buf,size_t count,loff_t * ppos)1720e47c8fc5SChristopher J. PeBenito static ssize_t sel_read_class(struct file *file, char __user *buf,
1721e47c8fc5SChristopher J. PeBenito size_t count, loff_t *ppos)
1722e47c8fc5SChristopher J. PeBenito {
1723496ad9aaSAl Viro unsigned long ino = file_inode(file)->i_ino;
1724cc1dad71SAl Viro char res[TMPBUFLEN];
17257e78c875Sliuyang34 ssize_t len = scnprintf(res, sizeof(res), "%d", sel_ino_to_class(ino));
1726cc1dad71SAl Viro return simple_read_from_buffer(buf, count, ppos, res, len);
1727e47c8fc5SChristopher J. PeBenito }
1728e47c8fc5SChristopher J. PeBenito
1729e47c8fc5SChristopher J. PeBenito static const struct file_operations sel_class_ops = {
1730e47c8fc5SChristopher J. PeBenito .read = sel_read_class,
173157a62c23SArnd Bergmann .llseek = generic_file_llseek,
1732e47c8fc5SChristopher J. PeBenito };
1733e47c8fc5SChristopher J. PeBenito
sel_read_perm(struct file * file,char __user * buf,size_t count,loff_t * ppos)1734e47c8fc5SChristopher J. PeBenito static ssize_t sel_read_perm(struct file *file, char __user *buf,
1735e47c8fc5SChristopher J. PeBenito size_t count, loff_t *ppos)
1736e47c8fc5SChristopher J. PeBenito {
1737496ad9aaSAl Viro unsigned long ino = file_inode(file)->i_ino;
1738cc1dad71SAl Viro char res[TMPBUFLEN];
17397e78c875Sliuyang34 ssize_t len = scnprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino));
1740cc1dad71SAl Viro return simple_read_from_buffer(buf, count, ppos, res, len);
1741e47c8fc5SChristopher J. PeBenito }
1742e47c8fc5SChristopher J. PeBenito
1743e47c8fc5SChristopher J. PeBenito static const struct file_operations sel_perm_ops = {
1744e47c8fc5SChristopher J. PeBenito .read = sel_read_perm,
174557a62c23SArnd Bergmann .llseek = generic_file_llseek,
1746e47c8fc5SChristopher J. PeBenito };
1747e47c8fc5SChristopher J. PeBenito
sel_read_policycap(struct file * file,char __user * buf,size_t count,loff_t * ppos)17483bb56b25SPaul Moore static ssize_t sel_read_policycap(struct file *file, char __user *buf,
17493bb56b25SPaul Moore size_t count, loff_t *ppos)
17503bb56b25SPaul Moore {
17513bb56b25SPaul Moore int value;
17523bb56b25SPaul Moore char tmpbuf[TMPBUFLEN];
17533bb56b25SPaul Moore ssize_t length;
1754496ad9aaSAl Viro unsigned long i_ino = file_inode(file)->i_ino;
17553bb56b25SPaul Moore
1756e67b7985SStephen Smalley value = security_policycap_supported(i_ino & SEL_INO_MASK);
17573bb56b25SPaul Moore length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
17583bb56b25SPaul Moore
17593bb56b25SPaul Moore return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
17603bb56b25SPaul Moore }
17613bb56b25SPaul Moore
17623bb56b25SPaul Moore static const struct file_operations sel_policycap_ops = {
17633bb56b25SPaul Moore .read = sel_read_policycap,
176457a62c23SArnd Bergmann .llseek = generic_file_llseek,
17653bb56b25SPaul Moore };
17663bb56b25SPaul Moore
sel_make_perm_files(struct selinux_policy * newpolicy,char * objclass,int classvalue,struct dentry * dir)176702a52c5cSStephen Smalley static int sel_make_perm_files(struct selinux_policy *newpolicy,
176802a52c5cSStephen Smalley char *objclass, int classvalue,
1769e47c8fc5SChristopher J. PeBenito struct dentry *dir)
1770e47c8fc5SChristopher J. PeBenito {
1771c50e125dSChristian Göttsche u32 i, nperms;
1772c50e125dSChristian Göttsche int rc;
1773e47c8fc5SChristopher J. PeBenito char **perms;
1774e47c8fc5SChristopher J. PeBenito
177502a52c5cSStephen Smalley rc = security_get_permissions(newpolicy, objclass, &perms, &nperms);
1776e47c8fc5SChristopher J. PeBenito if (rc)
1777b77a493bSEric Paris return rc;
1778e47c8fc5SChristopher J. PeBenito
1779e47c8fc5SChristopher J. PeBenito for (i = 0; i < nperms; i++) {
1780e47c8fc5SChristopher J. PeBenito struct inode *inode;
1781e47c8fc5SChristopher J. PeBenito struct dentry *dentry;
1782e47c8fc5SChristopher J. PeBenito
1783b77a493bSEric Paris rc = -ENOMEM;
1784e47c8fc5SChristopher J. PeBenito dentry = d_alloc_name(dir, perms[i]);
1785b77a493bSEric Paris if (!dentry)
1786b77a493bSEric Paris goto out;
1787e47c8fc5SChristopher J. PeBenito
1788e47c8fc5SChristopher J. PeBenito rc = -ENOMEM;
1789b77a493bSEric Paris inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
17907e4237faSnixiaoming if (!inode) {
17917e4237faSnixiaoming dput(dentry);
1792b77a493bSEric Paris goto out;
17937e4237faSnixiaoming }
1794b77a493bSEric Paris
1795e47c8fc5SChristopher J. PeBenito inode->i_fop = &sel_perm_ops;
1796e47c8fc5SChristopher J. PeBenito /* i+1 since perm values are 1-indexed */
1797e47c8fc5SChristopher J. PeBenito inode->i_ino = sel_perm_to_ino(classvalue, i + 1);
1798e47c8fc5SChristopher J. PeBenito d_add(dentry, inode);
1799e47c8fc5SChristopher J. PeBenito }
1800b77a493bSEric Paris rc = 0;
1801b77a493bSEric Paris out:
1802e47c8fc5SChristopher J. PeBenito for (i = 0; i < nperms; i++)
1803e47c8fc5SChristopher J. PeBenito kfree(perms[i]);
1804e47c8fc5SChristopher J. PeBenito kfree(perms);
1805e47c8fc5SChristopher J. PeBenito return rc;
1806e47c8fc5SChristopher J. PeBenito }
1807e47c8fc5SChristopher J. PeBenito
sel_make_class_dir_entries(struct selinux_policy * newpolicy,char * classname,int index,struct dentry * dir)180802a52c5cSStephen Smalley static int sel_make_class_dir_entries(struct selinux_policy *newpolicy,
180902a52c5cSStephen Smalley char *classname, int index,
1810e47c8fc5SChristopher J. PeBenito struct dentry *dir)
1811e47c8fc5SChristopher J. PeBenito {
18120619f0f5SStephen Smalley struct super_block *sb = dir->d_sb;
18130619f0f5SStephen Smalley struct selinux_fs_info *fsi = sb->s_fs_info;
1814e47c8fc5SChristopher J. PeBenito struct dentry *dentry = NULL;
1815e47c8fc5SChristopher J. PeBenito struct inode *inode = NULL;
1816e47c8fc5SChristopher J. PeBenito
1817e47c8fc5SChristopher J. PeBenito dentry = d_alloc_name(dir, "index");
1818b77a493bSEric Paris if (!dentry)
1819b77a493bSEric Paris return -ENOMEM;
1820e47c8fc5SChristopher J. PeBenito
1821e47c8fc5SChristopher J. PeBenito inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
18227e4237faSnixiaoming if (!inode) {
18237e4237faSnixiaoming dput(dentry);
1824b77a493bSEric Paris return -ENOMEM;
18257e4237faSnixiaoming }
1826e47c8fc5SChristopher J. PeBenito
1827e47c8fc5SChristopher J. PeBenito inode->i_fop = &sel_class_ops;
1828e47c8fc5SChristopher J. PeBenito inode->i_ino = sel_class_to_ino(index);
1829e47c8fc5SChristopher J. PeBenito d_add(dentry, inode);
1830e47c8fc5SChristopher J. PeBenito
18310619f0f5SStephen Smalley dentry = sel_make_dir(dir, "perms", &fsi->last_class_ino);
1832a1c2aa1eSAl Viro if (IS_ERR(dentry))
1833a1c2aa1eSAl Viro return PTR_ERR(dentry);
1834e47c8fc5SChristopher J. PeBenito
18355698f081Sye xingchen return sel_make_perm_files(newpolicy, classname, index, dentry);
1836e47c8fc5SChristopher J. PeBenito }
1837e47c8fc5SChristopher J. PeBenito
sel_make_classes(struct selinux_policy * newpolicy,struct dentry * class_dir,unsigned long * last_class_ino)183866ec384aSDaniel Burgener static int sel_make_classes(struct selinux_policy *newpolicy,
183966ec384aSDaniel Burgener struct dentry *class_dir,
184066ec384aSDaniel Burgener unsigned long *last_class_ino)
1841e47c8fc5SChristopher J. PeBenito {
1842c50e125dSChristian Göttsche u32 i, nclasses;
1843c50e125dSChristian Göttsche int rc;
1844e47c8fc5SChristopher J. PeBenito char **classes;
1845e47c8fc5SChristopher J. PeBenito
184602a52c5cSStephen Smalley rc = security_get_classes(newpolicy, &classes, &nclasses);
1847b77a493bSEric Paris if (rc)
1848b77a493bSEric Paris return rc;
1849e47c8fc5SChristopher J. PeBenito
1850e47c8fc5SChristopher J. PeBenito /* +2 since classes are 1-indexed */
185166ec384aSDaniel Burgener *last_class_ino = sel_class_to_ino(nclasses + 2);
1852e47c8fc5SChristopher J. PeBenito
1853e47c8fc5SChristopher J. PeBenito for (i = 0; i < nclasses; i++) {
1854e47c8fc5SChristopher J. PeBenito struct dentry *class_name_dir;
1855e47c8fc5SChristopher J. PeBenito
185666ec384aSDaniel Burgener class_name_dir = sel_make_dir(class_dir, classes[i],
185766ec384aSDaniel Burgener last_class_ino);
1858a1c2aa1eSAl Viro if (IS_ERR(class_name_dir)) {
1859a1c2aa1eSAl Viro rc = PTR_ERR(class_name_dir);
1860b77a493bSEric Paris goto out;
1861a1c2aa1eSAl Viro }
1862e47c8fc5SChristopher J. PeBenito
1863e47c8fc5SChristopher J. PeBenito /* i+1 since class values are 1-indexed */
186402a52c5cSStephen Smalley rc = sel_make_class_dir_entries(newpolicy, classes[i], i + 1,
1865e47c8fc5SChristopher J. PeBenito class_name_dir);
1866e47c8fc5SChristopher J. PeBenito if (rc)
1867b77a493bSEric Paris goto out;
1868e47c8fc5SChristopher J. PeBenito }
1869b77a493bSEric Paris rc = 0;
1870b77a493bSEric Paris out:
1871e47c8fc5SChristopher J. PeBenito for (i = 0; i < nclasses; i++)
1872e47c8fc5SChristopher J. PeBenito kfree(classes[i]);
1873e47c8fc5SChristopher J. PeBenito kfree(classes);
1874e47c8fc5SChristopher J. PeBenito return rc;
1875e47c8fc5SChristopher J. PeBenito }
1876e47c8fc5SChristopher J. PeBenito
sel_make_policycap(struct selinux_fs_info * fsi)18770619f0f5SStephen Smalley static int sel_make_policycap(struct selinux_fs_info *fsi)
18783bb56b25SPaul Moore {
18793bb56b25SPaul Moore unsigned int iter;
18803bb56b25SPaul Moore struct dentry *dentry = NULL;
18813bb56b25SPaul Moore struct inode *inode = NULL;
18823bb56b25SPaul Moore
1883cdbec3edSPaul Moore for (iter = 0; iter <= POLICYDB_CAP_MAX; iter++) {
18844dc2fce3SStephen Smalley if (iter < ARRAY_SIZE(selinux_policycap_names))
18850619f0f5SStephen Smalley dentry = d_alloc_name(fsi->policycap_dir,
18864dc2fce3SStephen Smalley selinux_policycap_names[iter]);
18873bb56b25SPaul Moore else
18880619f0f5SStephen Smalley dentry = d_alloc_name(fsi->policycap_dir, "unknown");
18893bb56b25SPaul Moore
18903bb56b25SPaul Moore if (dentry == NULL)
18913bb56b25SPaul Moore return -ENOMEM;
18923bb56b25SPaul Moore
18930619f0f5SStephen Smalley inode = sel_make_inode(fsi->sb, S_IFREG | 0444);
18947e4237faSnixiaoming if (inode == NULL) {
18957e4237faSnixiaoming dput(dentry);
18963bb56b25SPaul Moore return -ENOMEM;
18977e4237faSnixiaoming }
18983bb56b25SPaul Moore
18993bb56b25SPaul Moore inode->i_fop = &sel_policycap_ops;
19003bb56b25SPaul Moore inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
19013bb56b25SPaul Moore d_add(dentry, inode);
19023bb56b25SPaul Moore }
19033bb56b25SPaul Moore
19043bb56b25SPaul Moore return 0;
19053bb56b25SPaul Moore }
19063bb56b25SPaul Moore
sel_make_dir(struct dentry * dir,const char * name,unsigned long * ino)1907a1c2aa1eSAl Viro static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
19080dd4ae51SChristopher J. PeBenito unsigned long *ino)
19091da177e4SLinus Torvalds {
1910a1c2aa1eSAl Viro struct dentry *dentry = d_alloc_name(dir, name);
19111da177e4SLinus Torvalds struct inode *inode;
19121da177e4SLinus Torvalds
1913a1c2aa1eSAl Viro if (!dentry)
1914a1c2aa1eSAl Viro return ERR_PTR(-ENOMEM);
1915a1c2aa1eSAl Viro
1916a1c2aa1eSAl Viro inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1917a1c2aa1eSAl Viro if (!inode) {
1918a1c2aa1eSAl Viro dput(dentry);
1919a1c2aa1eSAl Viro return ERR_PTR(-ENOMEM);
1920a1c2aa1eSAl Viro }
1921b77a493bSEric Paris
19221da177e4SLinus Torvalds inode->i_op = &simple_dir_inode_operations;
19231da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations;
19240dd4ae51SChristopher J. PeBenito inode->i_ino = ++(*ino);
192540e906f8SJames Morris /* directory inodes start off with i_nlink == 2 (for "." entry) */
1926d8c76e6fSDave Hansen inc_nlink(inode);
19271da177e4SLinus Torvalds d_add(dentry, inode);
1928edb20fb5SJames Morris /* bump link count on parent directory, too */
1929ce0b16ddSDavid Howells inc_nlink(d_inode(dir));
1930b77a493bSEric Paris
1931a1c2aa1eSAl Viro return dentry;
19321da177e4SLinus Torvalds }
19331da177e4SLinus Torvalds
reject_all(struct mnt_idmap * idmap,struct inode * inode,int mask)19344a0b33f7SAl Viro static int reject_all(struct mnt_idmap *idmap, struct inode *inode, int mask)
19354a0b33f7SAl Viro {
19364a0b33f7SAl Viro return -EPERM; // no access for anyone, root or no root.
19374a0b33f7SAl Viro }
19384a0b33f7SAl Viro
19394a0b33f7SAl Viro static const struct inode_operations swapover_dir_inode_operations = {
19404a0b33f7SAl Viro .lookup = simple_lookup,
19414a0b33f7SAl Viro .permission = reject_all,
19424a0b33f7SAl Viro };
19434a0b33f7SAl Viro
sel_make_swapover_dir(struct super_block * sb,unsigned long * ino)19444a0b33f7SAl Viro static struct dentry *sel_make_swapover_dir(struct super_block *sb,
19450eea6091SDaniel Burgener unsigned long *ino)
19460eea6091SDaniel Burgener {
19474a0b33f7SAl Viro struct dentry *dentry = d_alloc_name(sb->s_root, ".swapover");
19484a0b33f7SAl Viro struct inode *inode;
19490eea6091SDaniel Burgener
19504a0b33f7SAl Viro if (!dentry)
19510eea6091SDaniel Burgener return ERR_PTR(-ENOMEM);
19520eea6091SDaniel Burgener
19534a0b33f7SAl Viro inode = sel_make_inode(sb, S_IFDIR);
19544a0b33f7SAl Viro if (!inode) {
19554a0b33f7SAl Viro dput(dentry);
19564a0b33f7SAl Viro return ERR_PTR(-ENOMEM);
19574a0b33f7SAl Viro }
19584a0b33f7SAl Viro
19594a0b33f7SAl Viro inode->i_op = &swapover_dir_inode_operations;
19600eea6091SDaniel Burgener inode->i_ino = ++(*ino);
19610eea6091SDaniel Burgener /* directory inodes start off with i_nlink == 2 (for "." entry) */
19620eea6091SDaniel Burgener inc_nlink(inode);
19634a0b33f7SAl Viro inode_lock(sb->s_root->d_inode);
19644a0b33f7SAl Viro d_add(dentry, inode);
19654a0b33f7SAl Viro inc_nlink(sb->s_root->d_inode);
19664a0b33f7SAl Viro inode_unlock(sb->s_root->d_inode);
19674a0b33f7SAl Viro return dentry;
19680eea6091SDaniel Burgener }
19690eea6091SDaniel Burgener
19700619f0f5SStephen Smalley #define NULL_FILE_NAME "null"
19710619f0f5SStephen Smalley
sel_fill_super(struct super_block * sb,struct fs_context * fc)1972920f50b2SDavid Howells static int sel_fill_super(struct super_block *sb, struct fs_context *fc)
19731da177e4SLinus Torvalds {
19740619f0f5SStephen Smalley struct selinux_fs_info *fsi;
19751da177e4SLinus Torvalds int ret;
19761da177e4SLinus Torvalds struct dentry *dentry;
1977a1c2aa1eSAl Viro struct inode *inode;
19781da177e4SLinus Torvalds struct inode_security_struct *isec;
19791da177e4SLinus Torvalds
1980cda37124SEric Biggers static const struct tree_descr selinux_files[] = {
19811da177e4SLinus Torvalds [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
19821da177e4SLinus Torvalds [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
1983ce9982d0SStephen Smalley [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
19841da177e4SLinus Torvalds [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
19851da177e4SLinus Torvalds [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
19861da177e4SLinus Torvalds [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
19871da177e4SLinus Torvalds [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
19881da177e4SLinus Torvalds [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
19891da177e4SLinus Torvalds [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
19901da177e4SLinus Torvalds [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
19911da177e4SLinus Torvalds [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
19921da177e4SLinus Torvalds [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
19931da177e4SLinus Torvalds [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
19943f12070eSEric Paris [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
19953f12070eSEric Paris [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
199611904167SKaiGai Kohei [SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO},
199772e8c859SEric Paris [SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO},
1998f9df6458SAndrew Perepechko [SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops,
1999f9df6458SAndrew Perepechko S_IWUGO},
20001da177e4SLinus Torvalds /* last one */ {""}
20011da177e4SLinus Torvalds };
20020619f0f5SStephen Smalley
20030619f0f5SStephen Smalley ret = selinux_fs_info_create(sb);
20040619f0f5SStephen Smalley if (ret)
20050619f0f5SStephen Smalley goto err;
20060619f0f5SStephen Smalley
20071da177e4SLinus Torvalds ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
20081da177e4SLinus Torvalds if (ret)
2009161ce45aSJames Morris goto err;
20101da177e4SLinus Torvalds
20110619f0f5SStephen Smalley fsi = sb->s_fs_info;
20120619f0f5SStephen Smalley fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino);
20130619f0f5SStephen Smalley if (IS_ERR(fsi->bool_dir)) {
20140619f0f5SStephen Smalley ret = PTR_ERR(fsi->bool_dir);
20150619f0f5SStephen Smalley fsi->bool_dir = NULL;
2016161ce45aSJames Morris goto err;
2017a1c2aa1eSAl Viro }
20181da177e4SLinus Torvalds
2019b77a493bSEric Paris ret = -ENOMEM;
20201da177e4SLinus Torvalds dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
2021b77a493bSEric Paris if (!dentry)
2022161ce45aSJames Morris goto err;
20231da177e4SLinus Torvalds
2024161ce45aSJames Morris ret = -ENOMEM;
2025b77a493bSEric Paris inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
20267e4237faSnixiaoming if (!inode) {
20277e4237faSnixiaoming dput(dentry);
2028161ce45aSJames Morris goto err;
20297e4237faSnixiaoming }
2030b77a493bSEric Paris
20310619f0f5SStephen Smalley inode->i_ino = ++fsi->last_ino;
203280788c22SCasey Schaufler isec = selinux_inode(inode);
20331da177e4SLinus Torvalds isec->sid = SECINITSID_DEVNULL;
20341da177e4SLinus Torvalds isec->sclass = SECCLASS_CHR_FILE;
203542059112SAndreas Gruenbacher isec->initialized = LABEL_INITIALIZED;
20361da177e4SLinus Torvalds
20371da177e4SLinus Torvalds init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
20381da177e4SLinus Torvalds d_add(dentry, inode);
20391da177e4SLinus Torvalds
20400619f0f5SStephen Smalley dentry = sel_make_dir(sb->s_root, "avc", &fsi->last_ino);
2041a1c2aa1eSAl Viro if (IS_ERR(dentry)) {
2042a1c2aa1eSAl Viro ret = PTR_ERR(dentry);
2043161ce45aSJames Morris goto err;
2044a1c2aa1eSAl Viro }
20451da177e4SLinus Torvalds
20461da177e4SLinus Torvalds ret = sel_make_avc_files(dentry);
2047bcb62828SChristian Göttsche if (ret)
2048bcb62828SChristian Göttsche goto err;
204966f8e2f0SJeff Vander Stoep
205066f8e2f0SJeff Vander Stoep dentry = sel_make_dir(sb->s_root, "ss", &fsi->last_ino);
205166f8e2f0SJeff Vander Stoep if (IS_ERR(dentry)) {
205266f8e2f0SJeff Vander Stoep ret = PTR_ERR(dentry);
205366f8e2f0SJeff Vander Stoep goto err;
205466f8e2f0SJeff Vander Stoep }
205566f8e2f0SJeff Vander Stoep
205666f8e2f0SJeff Vander Stoep ret = sel_make_ss_files(dentry);
20571da177e4SLinus Torvalds if (ret)
2058161ce45aSJames Morris goto err;
2059f0ee2e46SJames Carter
20600619f0f5SStephen Smalley dentry = sel_make_dir(sb->s_root, "initial_contexts", &fsi->last_ino);
2061a1c2aa1eSAl Viro if (IS_ERR(dentry)) {
2062a1c2aa1eSAl Viro ret = PTR_ERR(dentry);
2063f0ee2e46SJames Carter goto err;
2064a1c2aa1eSAl Viro }
2065f0ee2e46SJames Carter
2066f0ee2e46SJames Carter ret = sel_make_initcon_files(dentry);
2067f0ee2e46SJames Carter if (ret)
2068f0ee2e46SJames Carter goto err;
2069f0ee2e46SJames Carter
2070613ba187SDaniel Burgener fsi->class_dir = sel_make_dir(sb->s_root, CLASS_DIR_NAME, &fsi->last_ino);
20710619f0f5SStephen Smalley if (IS_ERR(fsi->class_dir)) {
20720619f0f5SStephen Smalley ret = PTR_ERR(fsi->class_dir);
20730619f0f5SStephen Smalley fsi->class_dir = NULL;
2074e47c8fc5SChristopher J. PeBenito goto err;
2075a1c2aa1eSAl Viro }
2076e47c8fc5SChristopher J. PeBenito
2077613ba187SDaniel Burgener fsi->policycap_dir = sel_make_dir(sb->s_root, POLICYCAP_DIR_NAME,
20780619f0f5SStephen Smalley &fsi->last_ino);
20790619f0f5SStephen Smalley if (IS_ERR(fsi->policycap_dir)) {
20800619f0f5SStephen Smalley ret = PTR_ERR(fsi->policycap_dir);
20810619f0f5SStephen Smalley fsi->policycap_dir = NULL;
2082e47c8fc5SChristopher J. PeBenito goto err;
2083a1c2aa1eSAl Viro }
20840619f0f5SStephen Smalley
208502a52c5cSStephen Smalley ret = sel_make_policycap(fsi);
208602a52c5cSStephen Smalley if (ret) {
208702a52c5cSStephen Smalley pr_err("SELinux: failed to load policy capabilities\n");
20880619f0f5SStephen Smalley goto err;
208902a52c5cSStephen Smalley }
209002a52c5cSStephen Smalley
2091b77a493bSEric Paris return 0;
2092161ce45aSJames Morris err:
2093f8b69a5fSpeter enderborg pr_err("SELinux: %s: failed while creating inodes\n",
2094744ba35eSEric Paris __func__);
20950619f0f5SStephen Smalley
20960619f0f5SStephen Smalley selinux_fs_info_free(sb);
20970619f0f5SStephen Smalley
2098b77a493bSEric Paris return ret;
20991da177e4SLinus Torvalds }
21001da177e4SLinus Torvalds
sel_get_tree(struct fs_context * fc)2101920f50b2SDavid Howells static int sel_get_tree(struct fs_context *fc)
21021da177e4SLinus Torvalds {
2103920f50b2SDavid Howells return get_tree_single(fc, sel_fill_super);
2104920f50b2SDavid Howells }
2105920f50b2SDavid Howells
2106920f50b2SDavid Howells static const struct fs_context_operations sel_context_ops = {
2107920f50b2SDavid Howells .get_tree = sel_get_tree,
2108920f50b2SDavid Howells };
2109920f50b2SDavid Howells
sel_init_fs_context(struct fs_context * fc)2110920f50b2SDavid Howells static int sel_init_fs_context(struct fs_context *fc)
2111920f50b2SDavid Howells {
2112920f50b2SDavid Howells fc->ops = &sel_context_ops;
2113920f50b2SDavid Howells return 0;
21141da177e4SLinus Torvalds }
21151da177e4SLinus Torvalds
sel_kill_sb(struct super_block * sb)21160619f0f5SStephen Smalley static void sel_kill_sb(struct super_block *sb)
21170619f0f5SStephen Smalley {
21180619f0f5SStephen Smalley selinux_fs_info_free(sb);
21190619f0f5SStephen Smalley kill_litter_super(sb);
21200619f0f5SStephen Smalley }
21210619f0f5SStephen Smalley
21221da177e4SLinus Torvalds static struct file_system_type sel_fs_type = {
21231da177e4SLinus Torvalds .name = "selinuxfs",
2124920f50b2SDavid Howells .init_fs_context = sel_init_fs_context,
21250619f0f5SStephen Smalley .kill_sb = sel_kill_sb,
21261da177e4SLinus Torvalds };
21271da177e4SLinus Torvalds
2128cd2bb4cbSOndrej Mosnacek struct path selinux_null __ro_after_init;
2129cd2bb4cbSOndrej Mosnacek
init_sel_fs(void)21301da177e4SLinus Torvalds static int __init init_sel_fs(void)
21311da177e4SLinus Torvalds {
21321da177e4SLinus Torvalds struct qstr null_name = QSTR_INIT(NULL_FILE_NAME,
21330619f0f5SStephen Smalley sizeof(NULL_FILE_NAME)-1);
21340619f0f5SStephen Smalley int err;
21351da177e4SLinus Torvalds
21361da177e4SLinus Torvalds if (!selinux_enabled_boot)
21376c5a682eSStephen Smalley return 0;
21381da177e4SLinus Torvalds
21397a627e3bSGreg Kroah-Hartman err = sysfs_create_mount_point(fs_kobj, "selinux");
2140f9bb4882SEric W. Biederman if (err)
2141f9bb4882SEric W. Biederman return err;
2142f9bb4882SEric W. Biederman
21437a627e3bSGreg Kroah-Hartman err = register_filesystem(&sel_fs_type);
21441da177e4SLinus Torvalds if (err) {
21457a627e3bSGreg Kroah-Hartman sysfs_remove_mount_point(fs_kobj, "selinux");
2146f9bb4882SEric W. Biederman return err;
2147b77a493bSEric Paris }
21487a627e3bSGreg Kroah-Hartman
2149b77a493bSEric Paris selinux_null.mnt = kern_mount(&sel_fs_type);
2150765927b2SAl Viro if (IS_ERR(selinux_null.mnt)) {
21511da177e4SLinus Torvalds pr_err("selinuxfs: could not mount!\n");
2152f8b69a5fSpeter enderborg err = PTR_ERR(selinux_null.mnt);
21531da177e4SLinus Torvalds selinux_null.mnt = NULL;
21541da177e4SLinus Torvalds return err;
21551da177e4SLinus Torvalds }
21560619f0f5SStephen Smalley
21570619f0f5SStephen Smalley selinux_null.dentry = d_hash_and_lookup(selinux_null.mnt->mnt_root,
21580619f0f5SStephen Smalley &null_name);
21590619f0f5SStephen Smalley if (IS_ERR(selinux_null.dentry)) {
21600619f0f5SStephen Smalley pr_err("selinuxfs: could not lookup null!\n");
21610619f0f5SStephen Smalley err = PTR_ERR(selinux_null.dentry);
21620619f0f5SStephen Smalley selinux_null.dentry = NULL;
2163b77a493bSEric Paris return err;
2164*fc983171SChristian Göttsche }
2165*fc983171SChristian Göttsche
2166*fc983171SChristian Göttsche /*
2167*fc983171SChristian Göttsche * Try to pre-allocate the status page, so the sequence number of the
2168*fc983171SChristian Göttsche * initial policy load can be stored.
2169*fc983171SChristian Göttsche */
21701da177e4SLinus Torvalds (void) selinux_kernel_status_page();
21711da177e4SLinus Torvalds
21721da177e4SLinus Torvalds return err;
21731da177e4SLinus Torvalds }
2174
2175 __initcall(init_sel_fs);
2176