xref: /openbmc/linux/security/selinux/selinuxfs.c (revision e67b7985)
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 
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 
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;
1000619f0f5SStephen Smalley 	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
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",
130*e67b7985SStephen 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
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;
141aa8e712cSStephen Smalley 	int old_value, new_value;
1421da177e4SLinus Torvalds 
143bfd51626SDavi Arnaut 	if (count >= PAGE_SIZE)
1448365a719SAl Viro 		return -ENOMEM;
145b77a493bSEric Paris 
1461da177e4SLinus Torvalds 	/* No partial writes. */
147b77a493bSEric Paris 	if (*ppos != 0)
1488365a719SAl Viro 		return -EINVAL;
149b77a493bSEric Paris 
1508365a719SAl Viro 	page = memdup_user_nul(buf, count);
1518365a719SAl Viro 	if (IS_ERR(page))
1528365a719SAl Viro 		return PTR_ERR(page);
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 	length = -EINVAL;
1551da177e4SLinus Torvalds 	if (sscanf(page, "%d", &new_value) != 1)
1561da177e4SLinus Torvalds 		goto out;
1571da177e4SLinus Torvalds 
158ea49d10eSStephen Smalley 	new_value = !!new_value;
159ea49d10eSStephen Smalley 
160*e67b7985SStephen Smalley 	old_value = enforcing_enabled();
161aa8e712cSStephen Smalley 	if (new_value != old_value) {
162*e67b7985SStephen Smalley 		length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
163be0554c9SStephen Smalley 				      SECCLASS_SECURITY, SECURITY__SETENFORCE,
164be0554c9SStephen Smalley 				      NULL);
1651da177e4SLinus Torvalds 		if (length)
1661da177e4SLinus Torvalds 			goto out;
167cdfb6b34SRichard Guy Briggs 		audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS,
1684195ed42SRichard Guy Briggs 			"enforcing=%d old_enforcing=%d auid=%u ses=%u"
1696c5a682eSStephen Smalley 			" enabled=1 old-enabled=1 lsm=selinux res=1",
170aa8e712cSStephen Smalley 			new_value, old_value,
171581abc09SEric W. Biederman 			from_kuid(&init_user_ns, audit_get_loginuid(current)),
1726c5a682eSStephen Smalley 			audit_get_sessionid(current));
173*e67b7985SStephen Smalley 		enforcing_set(new_value);
174aa8e712cSStephen Smalley 		if (new_value)
175*e67b7985SStephen Smalley 			avc_ss_reset(0);
176aa8e712cSStephen Smalley 		selnl_notify_setenforce(new_value);
177*e67b7985SStephen Smalley 		selinux_status_update_setenforce(new_value);
178aa8e712cSStephen Smalley 		if (!new_value)
17942df744cSJanne Karhunen 			call_blocking_lsm_notifier(LSM_POLICY_CHANGE, NULL);
1802554a48fSLakshmi Ramasubramanian 
181*e67b7985SStephen Smalley 		selinux_ima_measure_state();
1821da177e4SLinus Torvalds 	}
1831da177e4SLinus Torvalds 	length = count;
1841da177e4SLinus Torvalds out:
1858365a719SAl Viro 	kfree(page);
1861da177e4SLinus Torvalds 	return length;
1871da177e4SLinus Torvalds }
1881da177e4SLinus Torvalds #else
1891da177e4SLinus Torvalds #define sel_write_enforce NULL
1901da177e4SLinus Torvalds #endif
1911da177e4SLinus Torvalds 
1929c2e08c5SArjan van de Ven static const struct file_operations sel_enforce_ops = {
1931da177e4SLinus Torvalds 	.read		= sel_read_enforce,
1941da177e4SLinus Torvalds 	.write		= sel_write_enforce,
19557a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
1961da177e4SLinus Torvalds };
1971da177e4SLinus Torvalds 
1983f12070eSEric Paris static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf,
1993f12070eSEric Paris 					size_t count, loff_t *ppos)
2003f12070eSEric Paris {
2013f12070eSEric Paris 	char tmpbuf[TMPBUFLEN];
2023f12070eSEric Paris 	ssize_t length;
203496ad9aaSAl Viro 	ino_t ino = file_inode(filp)->i_ino;
2043f12070eSEric Paris 	int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
205*e67b7985SStephen Smalley 		security_get_reject_unknown() :
206*e67b7985SStephen Smalley 		!security_get_allow_unknown();
2073f12070eSEric Paris 
2083f12070eSEric Paris 	length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
2093f12070eSEric Paris 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
2103f12070eSEric Paris }
2113f12070eSEric Paris 
2123f12070eSEric Paris static const struct file_operations sel_handle_unknown_ops = {
2133f12070eSEric Paris 	.read		= sel_read_handle_unknown,
21457a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
2153f12070eSEric Paris };
2163f12070eSEric Paris 
21711904167SKaiGai Kohei static int sel_open_handle_status(struct inode *inode, struct file *filp)
21811904167SKaiGai Kohei {
219*e67b7985SStephen Smalley 	struct page    *status = selinux_kernel_status_page();
22011904167SKaiGai Kohei 
22111904167SKaiGai Kohei 	if (!status)
22211904167SKaiGai Kohei 		return -ENOMEM;
22311904167SKaiGai Kohei 
22411904167SKaiGai Kohei 	filp->private_data = status;
22511904167SKaiGai Kohei 
22611904167SKaiGai Kohei 	return 0;
22711904167SKaiGai Kohei }
22811904167SKaiGai Kohei 
22911904167SKaiGai Kohei static ssize_t sel_read_handle_status(struct file *filp, char __user *buf,
23011904167SKaiGai Kohei 				      size_t count, loff_t *ppos)
23111904167SKaiGai Kohei {
23211904167SKaiGai Kohei 	struct page    *status = filp->private_data;
23311904167SKaiGai Kohei 
23411904167SKaiGai Kohei 	BUG_ON(!status);
23511904167SKaiGai Kohei 
23611904167SKaiGai Kohei 	return simple_read_from_buffer(buf, count, ppos,
23711904167SKaiGai Kohei 				       page_address(status),
23811904167SKaiGai Kohei 				       sizeof(struct selinux_kernel_status));
23911904167SKaiGai Kohei }
24011904167SKaiGai Kohei 
24111904167SKaiGai Kohei static int sel_mmap_handle_status(struct file *filp,
24211904167SKaiGai Kohei 				  struct vm_area_struct *vma)
24311904167SKaiGai Kohei {
24411904167SKaiGai Kohei 	struct page    *status = filp->private_data;
24511904167SKaiGai Kohei 	unsigned long	size = vma->vm_end - vma->vm_start;
24611904167SKaiGai Kohei 
24711904167SKaiGai Kohei 	BUG_ON(!status);
24811904167SKaiGai Kohei 
24911904167SKaiGai Kohei 	/* only allows one page from the head */
25011904167SKaiGai Kohei 	if (vma->vm_pgoff > 0 || size != PAGE_SIZE)
25111904167SKaiGai Kohei 		return -EIO;
25211904167SKaiGai Kohei 	/* disallow writable mapping */
25311904167SKaiGai Kohei 	if (vma->vm_flags & VM_WRITE)
25411904167SKaiGai Kohei 		return -EPERM;
25511904167SKaiGai Kohei 	/* disallow mprotect() turns it into writable */
2561c71222eSSuren Baghdasaryan 	vm_flags_clear(vma, VM_MAYWRITE);
25711904167SKaiGai Kohei 
25811904167SKaiGai Kohei 	return remap_pfn_range(vma, vma->vm_start,
25911904167SKaiGai Kohei 			       page_to_pfn(status),
26011904167SKaiGai Kohei 			       size, vma->vm_page_prot);
26111904167SKaiGai Kohei }
26211904167SKaiGai Kohei 
26311904167SKaiGai Kohei static const struct file_operations sel_handle_status_ops = {
26411904167SKaiGai Kohei 	.open		= sel_open_handle_status,
26511904167SKaiGai Kohei 	.read		= sel_read_handle_status,
26611904167SKaiGai Kohei 	.mmap		= sel_mmap_handle_status,
26711904167SKaiGai Kohei 	.llseek		= generic_file_llseek,
26811904167SKaiGai Kohei };
26911904167SKaiGai Kohei 
2701da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
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;
2784195ed42SRichard Guy Briggs 	int enforcing;
2791da177e4SLinus Torvalds 
28089b223bfSPaul Moore 	/* NOTE: we are now officially considering runtime disable as
28189b223bfSPaul Moore 	 *       deprecated, and using it will become increasingly painful
28289b223bfSPaul Moore 	 *       (e.g. sleeping/blocking) as we progress through future
28389b223bfSPaul Moore 	 *       kernel releases until eventually it is removed
28489b223bfSPaul Moore 	 */
28589b223bfSPaul Moore 	pr_err("SELinux:  Runtime disable is deprecated, use selinux=0 on the kernel cmdline.\n");
28643b66662SPaul Moore 	pr_err("SELinux:  https://github.com/SELinuxProject/selinux-kernel/wiki/DEPRECATE-runtime-disable\n");
287e0d82593SPaul Moore 	ssleep(15);
28889b223bfSPaul Moore 
289bfd51626SDavi Arnaut 	if (count >= PAGE_SIZE)
2908365a719SAl Viro 		return -ENOMEM;
291b77a493bSEric Paris 
2921da177e4SLinus Torvalds 	/* No partial writes. */
293b77a493bSEric Paris 	if (*ppos != 0)
2948365a719SAl Viro 		return -EINVAL;
295b77a493bSEric Paris 
2968365a719SAl Viro 	page = memdup_user_nul(buf, count);
2978365a719SAl Viro 	if (IS_ERR(page))
2988365a719SAl Viro 		return PTR_ERR(page);
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 	length = -EINVAL;
3011da177e4SLinus Torvalds 	if (sscanf(page, "%d", &new_value) != 1)
3021da177e4SLinus Torvalds 		goto out;
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 	if (new_value) {
305*e67b7985SStephen Smalley 		enforcing = enforcing_enabled();
306*e67b7985SStephen Smalley 		length = selinux_disable();
307b77a493bSEric Paris 		if (length)
3081da177e4SLinus Torvalds 			goto out;
309cdfb6b34SRichard Guy Briggs 		audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS,
3104195ed42SRichard Guy Briggs 			"enforcing=%d old_enforcing=%d auid=%u ses=%u"
3116c5a682eSStephen Smalley 			" enabled=0 old-enabled=1 lsm=selinux res=1",
3124195ed42SRichard Guy Briggs 			enforcing, enforcing,
313581abc09SEric W. Biederman 			from_kuid(&init_user_ns, audit_get_loginuid(current)),
3146c5a682eSStephen Smalley 			audit_get_sessionid(current));
3151da177e4SLinus Torvalds 	}
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds 	length = count;
3181da177e4SLinus Torvalds out:
3198365a719SAl Viro 	kfree(page);
3201da177e4SLinus Torvalds 	return length;
3211da177e4SLinus Torvalds }
3221da177e4SLinus Torvalds #else
3231da177e4SLinus Torvalds #define sel_write_disable NULL
3241da177e4SLinus Torvalds #endif
3251da177e4SLinus Torvalds 
3269c2e08c5SArjan van de Ven static const struct file_operations sel_disable_ops = {
3271da177e4SLinus Torvalds 	.write		= sel_write_disable,
32857a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
3291da177e4SLinus Torvalds };
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
3321da177e4SLinus Torvalds 				   size_t count, loff_t *ppos)
3331da177e4SLinus Torvalds {
3341da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
3351da177e4SLinus Torvalds 	ssize_t length;
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
3381da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
3391da177e4SLinus Torvalds }
3401da177e4SLinus Torvalds 
3419c2e08c5SArjan van de Ven static const struct file_operations sel_policyvers_ops = {
3421da177e4SLinus Torvalds 	.read		= sel_read_policyvers,
34357a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
3441da177e4SLinus Torvalds };
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds /* declaration for sel_write_load */
34766ec384aSDaniel Burgener static int sel_make_bools(struct selinux_policy *newpolicy, struct dentry *bool_dir,
34866ec384aSDaniel Burgener 			  unsigned int *bool_num, char ***bool_pending_names,
349c3fae2b2SChristian Göttsche 			  int **bool_pending_values);
35066ec384aSDaniel Burgener static int sel_make_classes(struct selinux_policy *newpolicy,
35166ec384aSDaniel Burgener 			    struct dentry *class_dir,
35266ec384aSDaniel Burgener 			    unsigned long *last_class_ino);
353e47c8fc5SChristopher J. PeBenito 
354e47c8fc5SChristopher J. PeBenito /* declaration for sel_make_class_dirs */
355a1c2aa1eSAl Viro static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
356e47c8fc5SChristopher J. PeBenito 			unsigned long *ino);
3571da177e4SLinus Torvalds 
3580eea6091SDaniel Burgener /* declaration for sel_make_policy_nodes */
3590eea6091SDaniel Burgener static struct dentry *sel_make_disconnected_dir(struct super_block *sb,
3600eea6091SDaniel Burgener 						unsigned long *ino);
3610eea6091SDaniel Burgener 
3620eea6091SDaniel Burgener /* declaration for sel_make_policy_nodes */
363aeecf4a3SDaniel Burgener static void sel_remove_entries(struct dentry *de);
364aeecf4a3SDaniel Burgener 
3651da177e4SLinus Torvalds static ssize_t sel_read_mls(struct file *filp, char __user *buf,
3661da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
3671da177e4SLinus Torvalds {
3681da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
3691da177e4SLinus Torvalds 	ssize_t length;
3701da177e4SLinus Torvalds 
3710719aaf5SGuido Trentalancia 	length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
372*e67b7985SStephen Smalley 			   security_mls_enabled());
3731da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
3741da177e4SLinus Torvalds }
3751da177e4SLinus Torvalds 
3769c2e08c5SArjan van de Ven static const struct file_operations sel_mls_ops = {
3771da177e4SLinus Torvalds 	.read		= sel_read_mls,
37857a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
3791da177e4SLinus Torvalds };
3801da177e4SLinus Torvalds 
381cee74f47SEric Paris struct policy_load_memory {
382cee74f47SEric Paris 	size_t len;
383cee74f47SEric Paris 	void *data;
384cee74f47SEric Paris };
385cee74f47SEric Paris 
386cee74f47SEric Paris static int sel_open_policy(struct inode *inode, struct file *filp)
387cee74f47SEric Paris {
3880619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
389cee74f47SEric Paris 	struct policy_load_memory *plm = NULL;
390cee74f47SEric Paris 	int rc;
391cee74f47SEric Paris 
392cee74f47SEric Paris 	BUG_ON(filp->private_data);
393cee74f47SEric Paris 
394*e67b7985SStephen Smalley 	mutex_lock(&selinux_state.policy_mutex);
395cee74f47SEric Paris 
396*e67b7985SStephen Smalley 	rc = avc_has_perm(current_sid(), SECINITSID_SECURITY,
397be0554c9SStephen Smalley 			  SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
398cee74f47SEric Paris 	if (rc)
399cee74f47SEric Paris 		goto err;
400cee74f47SEric Paris 
401cee74f47SEric Paris 	rc = -EBUSY;
4020619f0f5SStephen Smalley 	if (fsi->policy_opened)
403cee74f47SEric Paris 		goto err;
404cee74f47SEric Paris 
405cee74f47SEric Paris 	rc = -ENOMEM;
406cee74f47SEric Paris 	plm = kzalloc(sizeof(*plm), GFP_KERNEL);
407cee74f47SEric Paris 	if (!plm)
408cee74f47SEric Paris 		goto err;
409cee74f47SEric Paris 
410*e67b7985SStephen Smalley 	rc = security_read_policy(&plm->data, &plm->len);
411cee74f47SEric Paris 	if (rc)
412cee74f47SEric Paris 		goto err;
413cee74f47SEric Paris 
41466ccd256SOndrej Mosnacek 	if ((size_t)i_size_read(inode) != plm->len) {
41566ccd256SOndrej Mosnacek 		inode_lock(inode);
41666ccd256SOndrej Mosnacek 		i_size_write(inode, plm->len);
41766ccd256SOndrej Mosnacek 		inode_unlock(inode);
41866ccd256SOndrej Mosnacek 	}
41966ccd256SOndrej Mosnacek 
4200619f0f5SStephen Smalley 	fsi->policy_opened = 1;
421cee74f47SEric Paris 
422cee74f47SEric Paris 	filp->private_data = plm;
423cee74f47SEric Paris 
424*e67b7985SStephen Smalley 	mutex_unlock(&selinux_state.policy_mutex);
425cee74f47SEric Paris 
426cee74f47SEric Paris 	return 0;
427cee74f47SEric Paris err:
428*e67b7985SStephen Smalley 	mutex_unlock(&selinux_state.policy_mutex);
429cee74f47SEric Paris 
430cee74f47SEric Paris 	if (plm)
431cee74f47SEric Paris 		vfree(plm->data);
432cee74f47SEric Paris 	kfree(plm);
433cee74f47SEric Paris 	return rc;
434cee74f47SEric Paris }
435cee74f47SEric Paris 
436cee74f47SEric Paris static int sel_release_policy(struct inode *inode, struct file *filp)
437cee74f47SEric Paris {
4380619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
439cee74f47SEric Paris 	struct policy_load_memory *plm = filp->private_data;
440cee74f47SEric Paris 
441cee74f47SEric Paris 	BUG_ON(!plm);
442cee74f47SEric Paris 
4430619f0f5SStephen Smalley 	fsi->policy_opened = 0;
444cee74f47SEric Paris 
445cee74f47SEric Paris 	vfree(plm->data);
446cee74f47SEric Paris 	kfree(plm);
447cee74f47SEric Paris 
448cee74f47SEric Paris 	return 0;
449cee74f47SEric Paris }
450cee74f47SEric Paris 
451cee74f47SEric Paris static ssize_t sel_read_policy(struct file *filp, char __user *buf,
452cee74f47SEric Paris 			       size_t count, loff_t *ppos)
453cee74f47SEric Paris {
454cee74f47SEric Paris 	struct policy_load_memory *plm = filp->private_data;
455cee74f47SEric Paris 	int ret;
456cee74f47SEric Paris 
457*e67b7985SStephen Smalley 	ret = avc_has_perm(current_sid(), SECINITSID_SECURITY,
458be0554c9SStephen Smalley 			  SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
459cee74f47SEric Paris 	if (ret)
460cee74f47SEric Paris 		return ret;
4610da74120SJann Horn 
4620da74120SJann Horn 	return simple_read_from_buffer(buf, count, ppos, plm->data, plm->len);
463cee74f47SEric Paris }
464cee74f47SEric Paris 
465ac9a1f6dSSouptick Joarder static vm_fault_t sel_mmap_policy_fault(struct vm_fault *vmf)
466845ca30fSEric Paris {
46711bac800SDave Jiang 	struct policy_load_memory *plm = vmf->vma->vm_file->private_data;
468845ca30fSEric Paris 	unsigned long offset;
469845ca30fSEric Paris 	struct page *page;
470845ca30fSEric Paris 
471845ca30fSEric Paris 	if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE))
472845ca30fSEric Paris 		return VM_FAULT_SIGBUS;
473845ca30fSEric Paris 
474845ca30fSEric Paris 	offset = vmf->pgoff << PAGE_SHIFT;
475845ca30fSEric Paris 	if (offset >= roundup(plm->len, PAGE_SIZE))
476845ca30fSEric Paris 		return VM_FAULT_SIGBUS;
477845ca30fSEric Paris 
478845ca30fSEric Paris 	page = vmalloc_to_page(plm->data + offset);
479845ca30fSEric Paris 	get_page(page);
480845ca30fSEric Paris 
481845ca30fSEric Paris 	vmf->page = page;
482845ca30fSEric Paris 
483845ca30fSEric Paris 	return 0;
484845ca30fSEric Paris }
485845ca30fSEric Paris 
4867cbea8dcSKirill A. Shutemov static const struct vm_operations_struct sel_mmap_policy_ops = {
487845ca30fSEric Paris 	.fault = sel_mmap_policy_fault,
488845ca30fSEric Paris 	.page_mkwrite = sel_mmap_policy_fault,
489845ca30fSEric Paris };
490845ca30fSEric Paris 
491ad3fa08cSJames Morris static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma)
492845ca30fSEric Paris {
493845ca30fSEric Paris 	if (vma->vm_flags & VM_SHARED) {
494845ca30fSEric Paris 		/* do not allow mprotect to make mapping writable */
4951c71222eSSuren Baghdasaryan 		vm_flags_clear(vma, VM_MAYWRITE);
496845ca30fSEric Paris 
497845ca30fSEric Paris 		if (vma->vm_flags & VM_WRITE)
498845ca30fSEric Paris 			return -EACCES;
499845ca30fSEric Paris 	}
500845ca30fSEric Paris 
5011c71222eSSuren Baghdasaryan 	vm_flags_set(vma, VM_DONTEXPAND | VM_DONTDUMP);
502845ca30fSEric Paris 	vma->vm_ops = &sel_mmap_policy_ops;
503845ca30fSEric Paris 
504845ca30fSEric Paris 	return 0;
505845ca30fSEric Paris }
506845ca30fSEric Paris 
507cee74f47SEric Paris static const struct file_operations sel_policy_ops = {
508cee74f47SEric Paris 	.open		= sel_open_policy,
509cee74f47SEric Paris 	.read		= sel_read_policy,
510845ca30fSEric Paris 	.mmap		= sel_mmap_policy,
511cee74f47SEric Paris 	.release	= sel_release_policy,
51247a93a5bSEric Paris 	.llseek		= generic_file_llseek,
513cee74f47SEric Paris };
514cee74f47SEric Paris 
5150eea6091SDaniel Burgener static void sel_remove_old_bool_data(unsigned int bool_num, char **bool_names,
516c3fae2b2SChristian Göttsche 				     int *bool_values)
517aeecf4a3SDaniel Burgener {
518aeecf4a3SDaniel Burgener 	u32 i;
519aeecf4a3SDaniel Burgener 
520aeecf4a3SDaniel Burgener 	/* bool_dir cleanup */
5210eea6091SDaniel Burgener 	for (i = 0; i < bool_num; i++)
5220eea6091SDaniel Burgener 		kfree(bool_names[i]);
5230eea6091SDaniel Burgener 	kfree(bool_names);
5240eea6091SDaniel Burgener 	kfree(bool_values);
525aeecf4a3SDaniel Burgener }
526aeecf4a3SDaniel Burgener 
52702a52c5cSStephen Smalley static int sel_make_policy_nodes(struct selinux_fs_info *fsi,
52802a52c5cSStephen Smalley 				struct selinux_policy *newpolicy)
5290619f0f5SStephen Smalley {
5300eea6091SDaniel Burgener 	int ret = 0;
5310eea6091SDaniel Burgener 	struct dentry *tmp_parent, *tmp_bool_dir, *tmp_class_dir, *old_dentry;
5320eea6091SDaniel Burgener 	unsigned int tmp_bool_num, old_bool_num;
5330eea6091SDaniel Burgener 	char **tmp_bool_names, **old_bool_names;
534c3fae2b2SChristian Göttsche 	int *tmp_bool_values, *old_bool_values;
5350eea6091SDaniel Burgener 	unsigned long tmp_ino = fsi->last_ino; /* Don't increment last_ino in this function */
5360619f0f5SStephen Smalley 
5370eea6091SDaniel Burgener 	tmp_parent = sel_make_disconnected_dir(fsi->sb, &tmp_ino);
5380eea6091SDaniel Burgener 	if (IS_ERR(tmp_parent))
5390eea6091SDaniel Burgener 		return PTR_ERR(tmp_parent);
540aeecf4a3SDaniel Burgener 
5410eea6091SDaniel Burgener 	tmp_ino = fsi->bool_dir->d_inode->i_ino - 1; /* sel_make_dir will increment and set */
5420eea6091SDaniel Burgener 	tmp_bool_dir = sel_make_dir(tmp_parent, BOOL_DIR_NAME, &tmp_ino);
5430eea6091SDaniel Burgener 	if (IS_ERR(tmp_bool_dir)) {
5440eea6091SDaniel Burgener 		ret = PTR_ERR(tmp_bool_dir);
5450eea6091SDaniel Burgener 		goto out;
5460619f0f5SStephen Smalley 	}
5470619f0f5SStephen Smalley 
5480eea6091SDaniel Burgener 	tmp_ino = fsi->class_dir->d_inode->i_ino - 1; /* sel_make_dir will increment and set */
5490eea6091SDaniel Burgener 	tmp_class_dir = sel_make_dir(tmp_parent, CLASS_DIR_NAME, &tmp_ino);
5500eea6091SDaniel Burgener 	if (IS_ERR(tmp_class_dir)) {
5510eea6091SDaniel Burgener 		ret = PTR_ERR(tmp_class_dir);
5520eea6091SDaniel Burgener 		goto out;
5530eea6091SDaniel Burgener 	}
5540eea6091SDaniel Burgener 
5550eea6091SDaniel Burgener 	ret = sel_make_bools(newpolicy, tmp_bool_dir, &tmp_bool_num,
5560eea6091SDaniel Burgener 			     &tmp_bool_names, &tmp_bool_values);
557ee5de60aSOndrej Mosnacek 	if (ret)
5580eea6091SDaniel Burgener 		goto out;
5590eea6091SDaniel Burgener 
5600eea6091SDaniel Burgener 	ret = sel_make_classes(newpolicy, tmp_class_dir,
56166ec384aSDaniel Burgener 			       &fsi->last_class_ino);
562ee5de60aSOndrej Mosnacek 	if (ret)
5630eea6091SDaniel Burgener 		goto out;
5640619f0f5SStephen Smalley 
5650eea6091SDaniel Burgener 	/* booleans */
5660eea6091SDaniel Burgener 	old_dentry = fsi->bool_dir;
5670eea6091SDaniel Burgener 	lock_rename(tmp_bool_dir, old_dentry);
5680eea6091SDaniel Burgener 	d_exchange(tmp_bool_dir, fsi->bool_dir);
5690eea6091SDaniel Burgener 
5700eea6091SDaniel Burgener 	old_bool_num = fsi->bool_num;
5710eea6091SDaniel Burgener 	old_bool_names = fsi->bool_pending_names;
5720eea6091SDaniel Burgener 	old_bool_values = fsi->bool_pending_values;
5730eea6091SDaniel Burgener 
5740eea6091SDaniel Burgener 	fsi->bool_num = tmp_bool_num;
5750eea6091SDaniel Burgener 	fsi->bool_pending_names = tmp_bool_names;
5760eea6091SDaniel Burgener 	fsi->bool_pending_values = tmp_bool_values;
5770eea6091SDaniel Burgener 
5780eea6091SDaniel Burgener 	sel_remove_old_bool_data(old_bool_num, old_bool_names, old_bool_values);
5790eea6091SDaniel Burgener 
5800eea6091SDaniel Burgener 	fsi->bool_dir = tmp_bool_dir;
5810eea6091SDaniel Burgener 	unlock_rename(tmp_bool_dir, old_dentry);
5820eea6091SDaniel Burgener 
5830eea6091SDaniel Burgener 	/* classes */
5840eea6091SDaniel Burgener 	old_dentry = fsi->class_dir;
5850eea6091SDaniel Burgener 	lock_rename(tmp_class_dir, old_dentry);
5860eea6091SDaniel Burgener 	d_exchange(tmp_class_dir, fsi->class_dir);
5870eea6091SDaniel Burgener 	fsi->class_dir = tmp_class_dir;
5880eea6091SDaniel Burgener 	unlock_rename(tmp_class_dir, old_dentry);
5890eea6091SDaniel Burgener 
5900eea6091SDaniel Burgener out:
5910eea6091SDaniel Burgener 	/* Since the other temporary dirs are children of tmp_parent
5920eea6091SDaniel Burgener 	 * this will handle all the cleanup in the case of a failure before
5930eea6091SDaniel Burgener 	 * the swapover
5940eea6091SDaniel Burgener 	 */
5950eea6091SDaniel Burgener 	sel_remove_entries(tmp_parent);
5960eea6091SDaniel Burgener 	dput(tmp_parent); /* d_genocide() only handles the children */
5970eea6091SDaniel Burgener 
5980eea6091SDaniel Burgener 	return ret;
5990619f0f5SStephen Smalley }
6000619f0f5SStephen Smalley 
6011da177e4SLinus Torvalds static ssize_t sel_write_load(struct file *file, const char __user *buf,
6021da177e4SLinus Torvalds 			      size_t count, loff_t *ppos)
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds {
6050619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
6066406887aSOndrej Mosnacek 	struct selinux_load_state load_state;
6071da177e4SLinus Torvalds 	ssize_t length;
6081da177e4SLinus Torvalds 	void *data = NULL;
6091da177e4SLinus Torvalds 
610*e67b7985SStephen Smalley 	mutex_lock(&selinux_state.policy_mutex);
6111da177e4SLinus Torvalds 
612*e67b7985SStephen Smalley 	length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
613be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL);
6141da177e4SLinus Torvalds 	if (length)
6151da177e4SLinus Torvalds 		goto out;
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds 	/* No partial writes. */
6181da177e4SLinus Torvalds 	length = -EINVAL;
619b77a493bSEric Paris 	if (*ppos != 0)
6201da177e4SLinus Torvalds 		goto out;
6211da177e4SLinus Torvalds 
622b77a493bSEric Paris 	length = -ENOMEM;
623b77a493bSEric Paris 	data = vmalloc(count);
624b77a493bSEric Paris 	if (!data)
625b77a493bSEric Paris 		goto out;
6261da177e4SLinus Torvalds 
6271da177e4SLinus Torvalds 	length = -EFAULT;
6281da177e4SLinus Torvalds 	if (copy_from_user(data, buf, count) != 0)
6291da177e4SLinus Torvalds 		goto out;
6301da177e4SLinus Torvalds 
631*e67b7985SStephen Smalley 	length = security_load_policy(data, count, &load_state);
6324262fb51SGary Tierney 	if (length) {
6334262fb51SGary Tierney 		pr_warn_ratelimited("SELinux: failed to load policy\n");
6341da177e4SLinus Torvalds 		goto out;
6354262fb51SGary Tierney 	}
6361da177e4SLinus Torvalds 
6376406887aSOndrej Mosnacek 	length = sel_make_policy_nodes(fsi, load_state.policy);
63802a52c5cSStephen Smalley 	if (length) {
639ee5de60aSOndrej Mosnacek 		pr_warn_ratelimited("SELinux: failed to initialize selinuxfs\n");
640*e67b7985SStephen Smalley 		selinux_policy_cancel(&load_state);
641519dad3bSOndrej Mosnacek 		goto out;
64202a52c5cSStephen Smalley 	}
64302a52c5cSStephen Smalley 
644*e67b7985SStephen Smalley 	selinux_policy_commit(&load_state);
645b77a493bSEric Paris 
6461da177e4SLinus Torvalds 	length = count;
647e47c8fc5SChristopher J. PeBenito 
648cdfb6b34SRichard Guy Briggs 	audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
649d141136fSRichard Guy Briggs 		"auid=%u ses=%u lsm=selinux res=1",
650581abc09SEric W. Biederman 		from_kuid(&init_user_ns, audit_get_loginuid(current)),
6514746ec5bSEric Paris 		audit_get_sessionid(current));
6521da177e4SLinus Torvalds out:
653*e67b7985SStephen Smalley 	mutex_unlock(&selinux_state.policy_mutex);
6541da177e4SLinus Torvalds 	vfree(data);
6551da177e4SLinus Torvalds 	return length;
6561da177e4SLinus Torvalds }
6571da177e4SLinus Torvalds 
6589c2e08c5SArjan van de Ven static const struct file_operations sel_load_ops = {
6591da177e4SLinus Torvalds 	.write		= sel_write_load,
66057a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
6611da177e4SLinus Torvalds };
6621da177e4SLinus Torvalds 
663ce9982d0SStephen Smalley static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
6641da177e4SLinus Torvalds {
665b77a493bSEric Paris 	char *canon = NULL;
666ce9982d0SStephen Smalley 	u32 sid, len;
6671da177e4SLinus Torvalds 	ssize_t length;
6681da177e4SLinus Torvalds 
669*e67b7985SStephen Smalley 	length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
670be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__CHECK_CONTEXT, NULL);
6711da177e4SLinus Torvalds 	if (length)
672b77a493bSEric Paris 		goto out;
6731da177e4SLinus Torvalds 
674*e67b7985SStephen Smalley 	length = security_context_to_sid(buf, size, &sid, GFP_KERNEL);
675b77a493bSEric Paris 	if (length)
676b77a493bSEric Paris 		goto out;
6771da177e4SLinus Torvalds 
678*e67b7985SStephen Smalley 	length = security_sid_to_context(sid, &canon, &len);
679b77a493bSEric Paris 	if (length)
680b77a493bSEric Paris 		goto out;
681ce9982d0SStephen Smalley 
682b77a493bSEric Paris 	length = -ERANGE;
683ce9982d0SStephen Smalley 	if (len > SIMPLE_TRANSACTION_LIMIT) {
684f8b69a5fSpeter enderborg 		pr_err("SELinux: %s:  context size (%u) exceeds "
685744ba35eSEric Paris 			"payload max\n", __func__, len);
686ce9982d0SStephen Smalley 		goto out;
687ce9982d0SStephen Smalley 	}
688ce9982d0SStephen Smalley 
689ce9982d0SStephen Smalley 	memcpy(buf, canon, len);
690ce9982d0SStephen Smalley 	length = len;
6911da177e4SLinus Torvalds out:
692ce9982d0SStephen Smalley 	kfree(canon);
6931da177e4SLinus Torvalds 	return length;
6941da177e4SLinus Torvalds }
6951da177e4SLinus Torvalds 
6961da177e4SLinus Torvalds static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
6971da177e4SLinus Torvalds 				     size_t count, loff_t *ppos)
6981da177e4SLinus Torvalds {
6991da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
7001da177e4SLinus Torvalds 	ssize_t length;
7011da177e4SLinus Torvalds 
7028861d0afSLakshmi Ramasubramanian 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
703*e67b7985SStephen Smalley 			   checkreqprot_get());
7041da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
7051da177e4SLinus Torvalds }
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
7081da177e4SLinus Torvalds 				      size_t count, loff_t *ppos)
7091da177e4SLinus Torvalds {
7108365a719SAl Viro 	char *page;
7111da177e4SLinus Torvalds 	ssize_t length;
7121da177e4SLinus Torvalds 	unsigned int new_value;
7131da177e4SLinus Torvalds 
714*e67b7985SStephen Smalley 	length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
715be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__SETCHECKREQPROT,
716be0554c9SStephen Smalley 			      NULL);
7171da177e4SLinus Torvalds 	if (length)
7188365a719SAl Viro 		return length;
7191da177e4SLinus Torvalds 
720bfd51626SDavi Arnaut 	if (count >= PAGE_SIZE)
7218365a719SAl Viro 		return -ENOMEM;
722b77a493bSEric Paris 
7231da177e4SLinus Torvalds 	/* No partial writes. */
724b77a493bSEric Paris 	if (*ppos != 0)
7258365a719SAl Viro 		return -EINVAL;
726b77a493bSEric Paris 
7278365a719SAl Viro 	page = memdup_user_nul(buf, count);
7288365a719SAl Viro 	if (IS_ERR(page))
7298365a719SAl Viro 		return PTR_ERR(page);
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds 	length = -EINVAL;
7321da177e4SLinus Torvalds 	if (sscanf(page, "%u", &new_value) != 1)
7331da177e4SLinus Torvalds 		goto out;
7341da177e4SLinus Torvalds 
735e9c38f9fSStephen Smalley 	if (new_value) {
736e9c38f9fSStephen Smalley 		char comm[sizeof(current->comm)];
737e9c38f9fSStephen Smalley 
738e9c38f9fSStephen Smalley 		memcpy(comm, current->comm, sizeof(comm));
73981200b02SPaul Moore 		pr_err("SELinux: %s (%d) set checkreqprot to 1. This is deprecated and will be rejected in a future kernel release.\n",
740e9c38f9fSStephen Smalley 		       comm, current->pid);
741e9c38f9fSStephen Smalley 	}
742e9c38f9fSStephen Smalley 
743*e67b7985SStephen Smalley 	checkreqprot_set((new_value ? 1 : 0));
7446a9e261cSPaul Moore 	if (new_value)
745e0d82593SPaul Moore 		ssleep(15);
7461da177e4SLinus Torvalds 	length = count;
7472554a48fSLakshmi Ramasubramanian 
748*e67b7985SStephen Smalley 	selinux_ima_measure_state();
7492554a48fSLakshmi Ramasubramanian 
7501da177e4SLinus Torvalds out:
7518365a719SAl Viro 	kfree(page);
7521da177e4SLinus Torvalds 	return length;
7531da177e4SLinus Torvalds }
7549c2e08c5SArjan van de Ven static const struct file_operations sel_checkreqprot_ops = {
7551da177e4SLinus Torvalds 	.read		= sel_read_checkreqprot,
7561da177e4SLinus Torvalds 	.write		= sel_write_checkreqprot,
75757a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
7581da177e4SLinus Torvalds };
7591da177e4SLinus Torvalds 
760f9df6458SAndrew Perepechko static ssize_t sel_write_validatetrans(struct file *file,
761f9df6458SAndrew Perepechko 					const char __user *buf,
762f9df6458SAndrew Perepechko 					size_t count, loff_t *ppos)
763f9df6458SAndrew Perepechko {
764f9df6458SAndrew Perepechko 	char *oldcon = NULL, *newcon = NULL, *taskcon = NULL;
765f9df6458SAndrew Perepechko 	char *req = NULL;
766f9df6458SAndrew Perepechko 	u32 osid, nsid, tsid;
767f9df6458SAndrew Perepechko 	u16 tclass;
768f9df6458SAndrew Perepechko 	int rc;
769f9df6458SAndrew Perepechko 
770*e67b7985SStephen Smalley 	rc = avc_has_perm(current_sid(), SECINITSID_SECURITY,
771be0554c9SStephen Smalley 			  SECCLASS_SECURITY, SECURITY__VALIDATE_TRANS, NULL);
772f9df6458SAndrew Perepechko 	if (rc)
773f9df6458SAndrew Perepechko 		goto out;
774f9df6458SAndrew Perepechko 
775f9df6458SAndrew Perepechko 	rc = -ENOMEM;
776f9df6458SAndrew Perepechko 	if (count >= PAGE_SIZE)
777f9df6458SAndrew Perepechko 		goto out;
778f9df6458SAndrew Perepechko 
779f9df6458SAndrew Perepechko 	/* No partial writes. */
780f9df6458SAndrew Perepechko 	rc = -EINVAL;
781f9df6458SAndrew Perepechko 	if (*ppos != 0)
782f9df6458SAndrew Perepechko 		goto out;
783f9df6458SAndrew Perepechko 
7840b884d25SAl Viro 	req = memdup_user_nul(buf, count);
7850b884d25SAl Viro 	if (IS_ERR(req)) {
7860b884d25SAl Viro 		rc = PTR_ERR(req);
7870b884d25SAl Viro 		req = NULL;
788f9df6458SAndrew Perepechko 		goto out;
7890b884d25SAl Viro 	}
790f9df6458SAndrew Perepechko 
791f9df6458SAndrew Perepechko 	rc = -ENOMEM;
792f9df6458SAndrew Perepechko 	oldcon = kzalloc(count + 1, GFP_KERNEL);
793f9df6458SAndrew Perepechko 	if (!oldcon)
794f9df6458SAndrew Perepechko 		goto out;
795f9df6458SAndrew Perepechko 
796f9df6458SAndrew Perepechko 	newcon = kzalloc(count + 1, GFP_KERNEL);
797f9df6458SAndrew Perepechko 	if (!newcon)
798f9df6458SAndrew Perepechko 		goto out;
799f9df6458SAndrew Perepechko 
800f9df6458SAndrew Perepechko 	taskcon = kzalloc(count + 1, GFP_KERNEL);
801f9df6458SAndrew Perepechko 	if (!taskcon)
802f9df6458SAndrew Perepechko 		goto out;
803f9df6458SAndrew Perepechko 
804f9df6458SAndrew Perepechko 	rc = -EINVAL;
805f9df6458SAndrew Perepechko 	if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4)
806f9df6458SAndrew Perepechko 		goto out;
807f9df6458SAndrew Perepechko 
808*e67b7985SStephen Smalley 	rc = security_context_str_to_sid(oldcon, &osid, GFP_KERNEL);
809f9df6458SAndrew Perepechko 	if (rc)
810f9df6458SAndrew Perepechko 		goto out;
811f9df6458SAndrew Perepechko 
812*e67b7985SStephen Smalley 	rc = security_context_str_to_sid(newcon, &nsid, GFP_KERNEL);
813f9df6458SAndrew Perepechko 	if (rc)
814f9df6458SAndrew Perepechko 		goto out;
815f9df6458SAndrew Perepechko 
816*e67b7985SStephen Smalley 	rc = security_context_str_to_sid(taskcon, &tsid, GFP_KERNEL);
817f9df6458SAndrew Perepechko 	if (rc)
818f9df6458SAndrew Perepechko 		goto out;
819f9df6458SAndrew Perepechko 
820*e67b7985SStephen Smalley 	rc = security_validate_transition_user(osid, nsid, tsid, tclass);
821f9df6458SAndrew Perepechko 	if (!rc)
822f9df6458SAndrew Perepechko 		rc = count;
823f9df6458SAndrew Perepechko out:
824f9df6458SAndrew Perepechko 	kfree(req);
825f9df6458SAndrew Perepechko 	kfree(oldcon);
826f9df6458SAndrew Perepechko 	kfree(newcon);
827f9df6458SAndrew Perepechko 	kfree(taskcon);
828f9df6458SAndrew Perepechko 	return rc;
829f9df6458SAndrew Perepechko }
830f9df6458SAndrew Perepechko 
831f9df6458SAndrew Perepechko static const struct file_operations sel_transition_ops = {
832f9df6458SAndrew Perepechko 	.write		= sel_write_validatetrans,
833f9df6458SAndrew Perepechko 	.llseek		= generic_file_llseek,
834f9df6458SAndrew Perepechko };
835f9df6458SAndrew Perepechko 
8361da177e4SLinus Torvalds /*
8371da177e4SLinus Torvalds  * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
8381da177e4SLinus Torvalds  */
8391da177e4SLinus Torvalds static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
8401da177e4SLinus Torvalds static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
8411da177e4SLinus Torvalds static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
8421da177e4SLinus Torvalds static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
8431da177e4SLinus Torvalds static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
8441da177e4SLinus Torvalds 
845631d2b49SEric Biggers static ssize_t (*const write_op[])(struct file *, char *, size_t) = {
8461da177e4SLinus Torvalds 	[SEL_ACCESS] = sel_write_access,
8471da177e4SLinus Torvalds 	[SEL_CREATE] = sel_write_create,
8481da177e4SLinus Torvalds 	[SEL_RELABEL] = sel_write_relabel,
8491da177e4SLinus Torvalds 	[SEL_USER] = sel_write_user,
8501da177e4SLinus Torvalds 	[SEL_MEMBER] = sel_write_member,
851ce9982d0SStephen Smalley 	[SEL_CONTEXT] = sel_write_context,
8521da177e4SLinus Torvalds };
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
8551da177e4SLinus Torvalds {
856496ad9aaSAl Viro 	ino_t ino = file_inode(file)->i_ino;
8571da177e4SLinus Torvalds 	char *data;
8581da177e4SLinus Torvalds 	ssize_t rv;
8591da177e4SLinus Torvalds 
8606e20a64aSNicolas Kaiser 	if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
8611da177e4SLinus Torvalds 		return -EINVAL;
8621da177e4SLinus Torvalds 
8631da177e4SLinus Torvalds 	data = simple_transaction_get(file, buf, size);
8641da177e4SLinus Torvalds 	if (IS_ERR(data))
8651da177e4SLinus Torvalds 		return PTR_ERR(data);
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds 	rv = write_op[ino](file, data, size);
8681da177e4SLinus Torvalds 	if (rv > 0) {
8691da177e4SLinus Torvalds 		simple_transaction_set(file, rv);
8701da177e4SLinus Torvalds 		rv = size;
8711da177e4SLinus Torvalds 	}
8721da177e4SLinus Torvalds 	return rv;
8731da177e4SLinus Torvalds }
8741da177e4SLinus Torvalds 
8759c2e08c5SArjan van de Ven static const struct file_operations transaction_ops = {
8761da177e4SLinus Torvalds 	.write		= selinux_transaction_write,
8771da177e4SLinus Torvalds 	.read		= simple_transaction_read,
8781da177e4SLinus Torvalds 	.release	= simple_transaction_release,
87957a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
8801da177e4SLinus Torvalds };
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds /*
8831da177e4SLinus Torvalds  * payload - write methods
8841da177e4SLinus Torvalds  * If the method has a response, the response should be put in buf,
8851da177e4SLinus Torvalds  * and the length returned.  Otherwise return 0 or and -error.
8861da177e4SLinus Torvalds  */
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
8891da177e4SLinus Torvalds {
890b77a493bSEric Paris 	char *scon = NULL, *tcon = NULL;
8911da177e4SLinus Torvalds 	u32 ssid, tsid;
8921da177e4SLinus Torvalds 	u16 tclass;
8931da177e4SLinus Torvalds 	struct av_decision avd;
8941da177e4SLinus Torvalds 	ssize_t length;
8951da177e4SLinus Torvalds 
896*e67b7985SStephen Smalley 	length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
897be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__COMPUTE_AV, NULL);
8981da177e4SLinus Torvalds 	if (length)
899b77a493bSEric Paris 		goto out;
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds 	length = -ENOMEM;
90289d155efSJames Morris 	scon = kzalloc(size + 1, GFP_KERNEL);
9031da177e4SLinus Torvalds 	if (!scon)
904b77a493bSEric Paris 		goto out;
9051da177e4SLinus Torvalds 
906b77a493bSEric Paris 	length = -ENOMEM;
90789d155efSJames Morris 	tcon = kzalloc(size + 1, GFP_KERNEL);
9081da177e4SLinus Torvalds 	if (!tcon)
9091da177e4SLinus Torvalds 		goto out;
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds 	length = -EINVAL;
91219439d05SStephen Smalley 	if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
913b77a493bSEric Paris 		goto out;
9141da177e4SLinus Torvalds 
915*e67b7985SStephen Smalley 	length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL);
916b77a493bSEric Paris 	if (length)
917b77a493bSEric Paris 		goto out;
918b77a493bSEric Paris 
919*e67b7985SStephen Smalley 	length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL);
920b77a493bSEric Paris 	if (length)
921b77a493bSEric Paris 		goto out;
9221da177e4SLinus Torvalds 
923*e67b7985SStephen Smalley 	security_compute_av_user(ssid, tsid, tclass, &avd);
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds 	length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
9268a6f83afSKaiGai Kohei 			  "%x %x %x %x %u %x",
927f1c6381aSEric Paris 			  avd.allowed, 0xffffffff,
9281da177e4SLinus Torvalds 			  avd.auditallow, avd.auditdeny,
9298a6f83afSKaiGai Kohei 			  avd.seqno, avd.flags);
9301da177e4SLinus Torvalds out:
931b77a493bSEric Paris 	kfree(tcon);
9321da177e4SLinus Torvalds 	kfree(scon);
9331da177e4SLinus Torvalds 	return length;
9341da177e4SLinus Torvalds }
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
9371da177e4SLinus Torvalds {
938b77a493bSEric Paris 	char *scon = NULL, *tcon = NULL;
939f50a3ec9SKohei Kaigai 	char *namebuf = NULL, *objname = NULL;
9401da177e4SLinus Torvalds 	u32 ssid, tsid, newsid;
9411da177e4SLinus Torvalds 	u16 tclass;
9421da177e4SLinus Torvalds 	ssize_t length;
943b77a493bSEric Paris 	char *newcon = NULL;
9441da177e4SLinus Torvalds 	u32 len;
945f50a3ec9SKohei Kaigai 	int nargs;
9461da177e4SLinus Torvalds 
947*e67b7985SStephen Smalley 	length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
948be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__COMPUTE_CREATE,
949be0554c9SStephen Smalley 			      NULL);
9501da177e4SLinus Torvalds 	if (length)
951b77a493bSEric Paris 		goto out;
9521da177e4SLinus Torvalds 
9531da177e4SLinus Torvalds 	length = -ENOMEM;
95489d155efSJames Morris 	scon = kzalloc(size + 1, GFP_KERNEL);
9551da177e4SLinus Torvalds 	if (!scon)
956b77a493bSEric Paris 		goto out;
9571da177e4SLinus Torvalds 
958b77a493bSEric Paris 	length = -ENOMEM;
95989d155efSJames Morris 	tcon = kzalloc(size + 1, GFP_KERNEL);
9601da177e4SLinus Torvalds 	if (!tcon)
9611da177e4SLinus Torvalds 		goto out;
9621da177e4SLinus Torvalds 
963f50a3ec9SKohei Kaigai 	length = -ENOMEM;
964f50a3ec9SKohei Kaigai 	namebuf = kzalloc(size + 1, GFP_KERNEL);
965f50a3ec9SKohei Kaigai 	if (!namebuf)
966b77a493bSEric Paris 		goto out;
9671da177e4SLinus Torvalds 
968f50a3ec9SKohei Kaigai 	length = -EINVAL;
969f50a3ec9SKohei Kaigai 	nargs = sscanf(buf, "%s %s %hu %s", scon, tcon, &tclass, namebuf);
970f50a3ec9SKohei Kaigai 	if (nargs < 3 || nargs > 4)
971f50a3ec9SKohei Kaigai 		goto out;
9720f7e4c33SKohei Kaigai 	if (nargs == 4) {
9730f7e4c33SKohei Kaigai 		/*
9740f7e4c33SKohei Kaigai 		 * If and when the name of new object to be queried contains
9750f7e4c33SKohei Kaigai 		 * either whitespace or multibyte characters, they shall be
9760f7e4c33SKohei Kaigai 		 * encoded based on the percentage-encoding rule.
9770f7e4c33SKohei Kaigai 		 * If not encoded, the sscanf logic picks up only left-half
9780f7e4c33SKohei Kaigai 		 * of the supplied name; splitted by a whitespace unexpectedly.
9790f7e4c33SKohei Kaigai 		 */
9800f7e4c33SKohei Kaigai 		char   *r, *w;
9810f7e4c33SKohei Kaigai 		int     c1, c2;
9820f7e4c33SKohei Kaigai 
9830f7e4c33SKohei Kaigai 		r = w = namebuf;
9840f7e4c33SKohei Kaigai 		do {
9850f7e4c33SKohei Kaigai 			c1 = *r++;
9860f7e4c33SKohei Kaigai 			if (c1 == '+')
9870f7e4c33SKohei Kaigai 				c1 = ' ';
9880f7e4c33SKohei Kaigai 			else if (c1 == '%') {
989af7ff2c2SAndy Shevchenko 				c1 = hex_to_bin(*r++);
990af7ff2c2SAndy Shevchenko 				if (c1 < 0)
9910f7e4c33SKohei Kaigai 					goto out;
992af7ff2c2SAndy Shevchenko 				c2 = hex_to_bin(*r++);
993af7ff2c2SAndy Shevchenko 				if (c2 < 0)
9940f7e4c33SKohei Kaigai 					goto out;
9950f7e4c33SKohei Kaigai 				c1 = (c1 << 4) | c2;
9960f7e4c33SKohei Kaigai 			}
9970f7e4c33SKohei Kaigai 			*w++ = c1;
9980f7e4c33SKohei Kaigai 		} while (c1 != '\0');
9990f7e4c33SKohei Kaigai 
1000f50a3ec9SKohei Kaigai 		objname = namebuf;
10010f7e4c33SKohei Kaigai 	}
1002f50a3ec9SKohei Kaigai 
1003*e67b7985SStephen Smalley 	length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL);
1004b77a493bSEric Paris 	if (length)
1005b77a493bSEric Paris 		goto out;
1006b77a493bSEric Paris 
1007*e67b7985SStephen Smalley 	length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL);
1008b77a493bSEric Paris 	if (length)
1009b77a493bSEric Paris 		goto out;
10101da177e4SLinus Torvalds 
1011*e67b7985SStephen Smalley 	length = security_transition_sid_user(ssid, tsid, tclass,
10120619f0f5SStephen Smalley 					      objname, &newsid);
1013b77a493bSEric Paris 	if (length)
1014b77a493bSEric Paris 		goto out;
10151da177e4SLinus Torvalds 
1016*e67b7985SStephen Smalley 	length = security_sid_to_context(newsid, &newcon, &len);
1017b77a493bSEric Paris 	if (length)
1018b77a493bSEric Paris 		goto out;
10191da177e4SLinus Torvalds 
1020b77a493bSEric Paris 	length = -ERANGE;
10211da177e4SLinus Torvalds 	if (len > SIMPLE_TRANSACTION_LIMIT) {
1022f8b69a5fSpeter enderborg 		pr_err("SELinux: %s:  context size (%u) exceeds "
1023744ba35eSEric Paris 			"payload max\n", __func__, len);
1024b77a493bSEric Paris 		goto out;
10251da177e4SLinus Torvalds 	}
10261da177e4SLinus Torvalds 
10271da177e4SLinus Torvalds 	memcpy(buf, newcon, len);
10281da177e4SLinus Torvalds 	length = len;
10291da177e4SLinus Torvalds out:
1030b77a493bSEric Paris 	kfree(newcon);
1031f50a3ec9SKohei Kaigai 	kfree(namebuf);
1032b77a493bSEric Paris 	kfree(tcon);
10331da177e4SLinus Torvalds 	kfree(scon);
10341da177e4SLinus Torvalds 	return length;
10351da177e4SLinus Torvalds }
10361da177e4SLinus Torvalds 
10371da177e4SLinus Torvalds static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
10381da177e4SLinus Torvalds {
1039b77a493bSEric Paris 	char *scon = NULL, *tcon = NULL;
10401da177e4SLinus Torvalds 	u32 ssid, tsid, newsid;
10411da177e4SLinus Torvalds 	u16 tclass;
10421da177e4SLinus Torvalds 	ssize_t length;
1043b77a493bSEric Paris 	char *newcon = NULL;
10441da177e4SLinus Torvalds 	u32 len;
10451da177e4SLinus Torvalds 
1046*e67b7985SStephen Smalley 	length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
1047be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__COMPUTE_RELABEL,
1048be0554c9SStephen Smalley 			      NULL);
10491da177e4SLinus Torvalds 	if (length)
1050b77a493bSEric Paris 		goto out;
10511da177e4SLinus Torvalds 
10521da177e4SLinus Torvalds 	length = -ENOMEM;
105389d155efSJames Morris 	scon = kzalloc(size + 1, GFP_KERNEL);
10541da177e4SLinus Torvalds 	if (!scon)
1055b77a493bSEric Paris 		goto out;
10561da177e4SLinus Torvalds 
1057b77a493bSEric Paris 	length = -ENOMEM;
105889d155efSJames Morris 	tcon = kzalloc(size + 1, GFP_KERNEL);
10591da177e4SLinus Torvalds 	if (!tcon)
10601da177e4SLinus Torvalds 		goto out;
10611da177e4SLinus Torvalds 
10621da177e4SLinus Torvalds 	length = -EINVAL;
10631da177e4SLinus Torvalds 	if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
1064b77a493bSEric Paris 		goto out;
10651da177e4SLinus Torvalds 
1066*e67b7985SStephen Smalley 	length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL);
1067b77a493bSEric Paris 	if (length)
1068b77a493bSEric Paris 		goto out;
1069b77a493bSEric Paris 
1070*e67b7985SStephen Smalley 	length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL);
1071b77a493bSEric Paris 	if (length)
1072b77a493bSEric Paris 		goto out;
10731da177e4SLinus Torvalds 
1074*e67b7985SStephen Smalley 	length = security_change_sid(ssid, tsid, tclass, &newsid);
1075b77a493bSEric Paris 	if (length)
1076b77a493bSEric Paris 		goto out;
10771da177e4SLinus Torvalds 
1078*e67b7985SStephen Smalley 	length = security_sid_to_context(newsid, &newcon, &len);
1079b77a493bSEric Paris 	if (length)
1080b77a493bSEric Paris 		goto out;
10811da177e4SLinus Torvalds 
10821da177e4SLinus Torvalds 	length = -ERANGE;
1083b77a493bSEric Paris 	if (len > SIMPLE_TRANSACTION_LIMIT)
1084b77a493bSEric Paris 		goto out;
10851da177e4SLinus Torvalds 
10861da177e4SLinus Torvalds 	memcpy(buf, newcon, len);
10871da177e4SLinus Torvalds 	length = len;
10881da177e4SLinus Torvalds out:
1089b77a493bSEric Paris 	kfree(newcon);
1090b77a493bSEric Paris 	kfree(tcon);
10911da177e4SLinus Torvalds 	kfree(scon);
10921da177e4SLinus Torvalds 	return length;
10931da177e4SLinus Torvalds }
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
10961da177e4SLinus Torvalds {
1097b77a493bSEric Paris 	char *con = NULL, *user = NULL, *ptr;
1098b77a493bSEric Paris 	u32 sid, *sids = NULL;
10991da177e4SLinus Torvalds 	ssize_t length;
11001da177e4SLinus Torvalds 	char *newcon;
11011da177e4SLinus Torvalds 	int i, rc;
11021da177e4SLinus Torvalds 	u32 len, nsids;
11031da177e4SLinus Torvalds 
1104*e67b7985SStephen Smalley 	length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
1105be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__COMPUTE_USER,
1106be0554c9SStephen Smalley 			      NULL);
11071da177e4SLinus Torvalds 	if (length)
11086eab04a8SJustin P. Mattock 		goto out;
11091da177e4SLinus Torvalds 
11101da177e4SLinus Torvalds 	length = -ENOMEM;
111189d155efSJames Morris 	con = kzalloc(size + 1, GFP_KERNEL);
11121da177e4SLinus Torvalds 	if (!con)
11136eab04a8SJustin P. Mattock 		goto out;
11141da177e4SLinus Torvalds 
1115b77a493bSEric Paris 	length = -ENOMEM;
111689d155efSJames Morris 	user = kzalloc(size + 1, GFP_KERNEL);
11171da177e4SLinus Torvalds 	if (!user)
11181da177e4SLinus Torvalds 		goto out;
11191da177e4SLinus Torvalds 
11201da177e4SLinus Torvalds 	length = -EINVAL;
11211da177e4SLinus Torvalds 	if (sscanf(buf, "%s %s", con, user) != 2)
1122b77a493bSEric Paris 		goto out;
11231da177e4SLinus Torvalds 
1124*e67b7985SStephen Smalley 	length = security_context_str_to_sid(con, &sid, GFP_KERNEL);
1125b77a493bSEric Paris 	if (length)
1126b77a493bSEric Paris 		goto out;
11271da177e4SLinus Torvalds 
1128*e67b7985SStephen Smalley 	length = security_get_user_sids(sid, user, &sids, &nsids);
1129b77a493bSEric Paris 	if (length)
1130b77a493bSEric Paris 		goto out;
11311da177e4SLinus Torvalds 
11321da177e4SLinus Torvalds 	length = sprintf(buf, "%u", nsids) + 1;
11331da177e4SLinus Torvalds 	ptr = buf + length;
11341da177e4SLinus Torvalds 	for (i = 0; i < nsids; i++) {
1135*e67b7985SStephen Smalley 		rc = security_sid_to_context(sids[i], &newcon, &len);
11361da177e4SLinus Torvalds 		if (rc) {
11371da177e4SLinus Torvalds 			length = rc;
1138b77a493bSEric Paris 			goto out;
11391da177e4SLinus Torvalds 		}
11401da177e4SLinus Torvalds 		if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
11411da177e4SLinus Torvalds 			kfree(newcon);
11421da177e4SLinus Torvalds 			length = -ERANGE;
1143b77a493bSEric Paris 			goto out;
11441da177e4SLinus Torvalds 		}
11451da177e4SLinus Torvalds 		memcpy(ptr, newcon, len);
11461da177e4SLinus Torvalds 		kfree(newcon);
11471da177e4SLinus Torvalds 		ptr += len;
11481da177e4SLinus Torvalds 		length += len;
11491da177e4SLinus Torvalds 	}
11501da177e4SLinus Torvalds out:
1151b77a493bSEric Paris 	kfree(sids);
1152b77a493bSEric Paris 	kfree(user);
11531da177e4SLinus Torvalds 	kfree(con);
11541da177e4SLinus Torvalds 	return length;
11551da177e4SLinus Torvalds }
11561da177e4SLinus Torvalds 
11571da177e4SLinus Torvalds static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
11581da177e4SLinus Torvalds {
1159b77a493bSEric Paris 	char *scon = NULL, *tcon = NULL;
11601da177e4SLinus Torvalds 	u32 ssid, tsid, newsid;
11611da177e4SLinus Torvalds 	u16 tclass;
11621da177e4SLinus Torvalds 	ssize_t length;
1163b77a493bSEric Paris 	char *newcon = NULL;
11641da177e4SLinus Torvalds 	u32 len;
11651da177e4SLinus Torvalds 
1166*e67b7985SStephen Smalley 	length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
1167be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__COMPUTE_MEMBER,
1168be0554c9SStephen Smalley 			      NULL);
11691da177e4SLinus Torvalds 	if (length)
1170b77a493bSEric Paris 		goto out;
11711da177e4SLinus Torvalds 
11721da177e4SLinus Torvalds 	length = -ENOMEM;
117389d155efSJames Morris 	scon = kzalloc(size + 1, GFP_KERNEL);
11741da177e4SLinus Torvalds 	if (!scon)
11756eab04a8SJustin P. Mattock 		goto out;
11761da177e4SLinus Torvalds 
1177b77a493bSEric Paris 	length = -ENOMEM;
117889d155efSJames Morris 	tcon = kzalloc(size + 1, GFP_KERNEL);
11791da177e4SLinus Torvalds 	if (!tcon)
11801da177e4SLinus Torvalds 		goto out;
11811da177e4SLinus Torvalds 
11821da177e4SLinus Torvalds 	length = -EINVAL;
11831da177e4SLinus Torvalds 	if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
1184b77a493bSEric Paris 		goto out;
11851da177e4SLinus Torvalds 
1186*e67b7985SStephen Smalley 	length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL);
1187b77a493bSEric Paris 	if (length)
1188b77a493bSEric Paris 		goto out;
1189b77a493bSEric Paris 
1190*e67b7985SStephen Smalley 	length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL);
1191b77a493bSEric Paris 	if (length)
1192b77a493bSEric Paris 		goto out;
11931da177e4SLinus Torvalds 
1194*e67b7985SStephen Smalley 	length = security_member_sid(ssid, tsid, tclass, &newsid);
1195b77a493bSEric Paris 	if (length)
1196b77a493bSEric Paris 		goto out;
11971da177e4SLinus Torvalds 
1198*e67b7985SStephen Smalley 	length = security_sid_to_context(newsid, &newcon, &len);
1199b77a493bSEric Paris 	if (length)
1200b77a493bSEric Paris 		goto out;
12011da177e4SLinus Torvalds 
1202b77a493bSEric Paris 	length = -ERANGE;
12031da177e4SLinus Torvalds 	if (len > SIMPLE_TRANSACTION_LIMIT) {
1204f8b69a5fSpeter enderborg 		pr_err("SELinux: %s:  context size (%u) exceeds "
1205744ba35eSEric Paris 			"payload max\n", __func__, len);
1206b77a493bSEric Paris 		goto out;
12071da177e4SLinus Torvalds 	}
12081da177e4SLinus Torvalds 
12091da177e4SLinus Torvalds 	memcpy(buf, newcon, len);
12101da177e4SLinus Torvalds 	length = len;
12111da177e4SLinus Torvalds out:
1212b77a493bSEric Paris 	kfree(newcon);
1213b77a493bSEric Paris 	kfree(tcon);
12141da177e4SLinus Torvalds 	kfree(scon);
12151da177e4SLinus Torvalds 	return length;
12161da177e4SLinus Torvalds }
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds static struct inode *sel_make_inode(struct super_block *sb, int mode)
12191da177e4SLinus Torvalds {
12201da177e4SLinus Torvalds 	struct inode *ret = new_inode(sb);
12211da177e4SLinus Torvalds 
12221da177e4SLinus Torvalds 	if (ret) {
12231da177e4SLinus Torvalds 		ret->i_mode = mode;
1224078cd827SDeepa Dinamani 		ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret);
12251da177e4SLinus Torvalds 	}
12261da177e4SLinus Torvalds 	return ret;
12271da177e4SLinus Torvalds }
12281da177e4SLinus Torvalds 
12291da177e4SLinus Torvalds static ssize_t sel_read_bool(struct file *filep, char __user *buf,
12301da177e4SLinus Torvalds 			     size_t count, loff_t *ppos)
12311da177e4SLinus Torvalds {
12320619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
12331da177e4SLinus Torvalds 	char *page = NULL;
12341da177e4SLinus Torvalds 	ssize_t length;
12351da177e4SLinus Torvalds 	ssize_t ret;
12361da177e4SLinus Torvalds 	int cur_enforcing;
1237496ad9aaSAl Viro 	unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
1238d313f948SStephen Smalley 	const char *name = filep->f_path.dentry->d_name.name;
12391da177e4SLinus Torvalds 
1240*e67b7985SStephen Smalley 	mutex_lock(&selinux_state.policy_mutex);
12411da177e4SLinus Torvalds 
1242d313f948SStephen Smalley 	ret = -EINVAL;
12430619f0f5SStephen Smalley 	if (index >= fsi->bool_num || strcmp(name,
12440619f0f5SStephen Smalley 					     fsi->bool_pending_names[index]))
12450da74120SJann Horn 		goto out_unlock;
12461da177e4SLinus Torvalds 
12471da177e4SLinus Torvalds 	ret = -ENOMEM;
1248b77a493bSEric Paris 	page = (char *)get_zeroed_page(GFP_KERNEL);
1249b77a493bSEric Paris 	if (!page)
12500da74120SJann Horn 		goto out_unlock;
12511da177e4SLinus Torvalds 
1252*e67b7985SStephen Smalley 	cur_enforcing = security_get_bool_value(index);
12531da177e4SLinus Torvalds 	if (cur_enforcing < 0) {
12541da177e4SLinus Torvalds 		ret = cur_enforcing;
12550da74120SJann Horn 		goto out_unlock;
12561da177e4SLinus Torvalds 	}
12571da177e4SLinus Torvalds 	length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
12580619f0f5SStephen Smalley 			  fsi->bool_pending_values[index]);
1259*e67b7985SStephen Smalley 	mutex_unlock(&selinux_state.policy_mutex);
12600da74120SJann Horn 	ret = simple_read_from_buffer(buf, count, ppos, page, length);
12610da74120SJann Horn out_free:
12621da177e4SLinus Torvalds 	free_page((unsigned long)page);
12631da177e4SLinus Torvalds 	return ret;
12640da74120SJann Horn 
12650da74120SJann Horn out_unlock:
1266*e67b7985SStephen Smalley 	mutex_unlock(&selinux_state.policy_mutex);
12670da74120SJann Horn 	goto out_free;
12681da177e4SLinus Torvalds }
12691da177e4SLinus Torvalds 
12701da177e4SLinus Torvalds static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
12711da177e4SLinus Torvalds 			      size_t count, loff_t *ppos)
12721da177e4SLinus Torvalds {
12730619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
12741da177e4SLinus Torvalds 	char *page = NULL;
1275d313f948SStephen Smalley 	ssize_t length;
12761da177e4SLinus Torvalds 	int new_value;
1277496ad9aaSAl Viro 	unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
1278d313f948SStephen Smalley 	const char *name = filep->f_path.dentry->d_name.name;
12791da177e4SLinus Torvalds 
12800da74120SJann Horn 	if (count >= PAGE_SIZE)
12810da74120SJann Horn 		return -ENOMEM;
12820da74120SJann Horn 
12830da74120SJann Horn 	/* No partial writes. */
12840da74120SJann Horn 	if (*ppos != 0)
12850da74120SJann Horn 		return -EINVAL;
12860da74120SJann Horn 
12870da74120SJann Horn 	page = memdup_user_nul(buf, count);
12880da74120SJann Horn 	if (IS_ERR(page))
12890da74120SJann Horn 		return PTR_ERR(page);
12900da74120SJann Horn 
1291*e67b7985SStephen Smalley 	mutex_lock(&selinux_state.policy_mutex);
12921da177e4SLinus Torvalds 
1293*e67b7985SStephen Smalley 	length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
1294be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__SETBOOL,
1295be0554c9SStephen Smalley 			      NULL);
12961da177e4SLinus Torvalds 	if (length)
12971da177e4SLinus Torvalds 		goto out;
12981da177e4SLinus Torvalds 
1299d313f948SStephen Smalley 	length = -EINVAL;
13000619f0f5SStephen Smalley 	if (index >= fsi->bool_num || strcmp(name,
13010619f0f5SStephen Smalley 					     fsi->bool_pending_names[index]))
1302d313f948SStephen Smalley 		goto out;
1303d313f948SStephen Smalley 
13041da177e4SLinus Torvalds 	length = -EINVAL;
13051da177e4SLinus Torvalds 	if (sscanf(page, "%d", &new_value) != 1)
13061da177e4SLinus Torvalds 		goto out;
13071da177e4SLinus Torvalds 
13081da177e4SLinus Torvalds 	if (new_value)
13091da177e4SLinus Torvalds 		new_value = 1;
13101da177e4SLinus Torvalds 
13110619f0f5SStephen Smalley 	fsi->bool_pending_values[index] = new_value;
13121da177e4SLinus Torvalds 	length = count;
13131da177e4SLinus Torvalds 
13141da177e4SLinus Torvalds out:
1315*e67b7985SStephen Smalley 	mutex_unlock(&selinux_state.policy_mutex);
13168365a719SAl Viro 	kfree(page);
13171da177e4SLinus Torvalds 	return length;
13181da177e4SLinus Torvalds }
13191da177e4SLinus Torvalds 
13209c2e08c5SArjan van de Ven static const struct file_operations sel_bool_ops = {
13211da177e4SLinus Torvalds 	.read		= sel_read_bool,
13221da177e4SLinus Torvalds 	.write		= sel_write_bool,
132357a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
13241da177e4SLinus Torvalds };
13251da177e4SLinus Torvalds 
13261da177e4SLinus Torvalds static ssize_t sel_commit_bools_write(struct file *filep,
13271da177e4SLinus Torvalds 				      const char __user *buf,
13281da177e4SLinus Torvalds 				      size_t count, loff_t *ppos)
13291da177e4SLinus Torvalds {
13300619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
13311da177e4SLinus Torvalds 	char *page = NULL;
1332d313f948SStephen Smalley 	ssize_t length;
13331da177e4SLinus Torvalds 	int new_value;
13341da177e4SLinus Torvalds 
13350da74120SJann Horn 	if (count >= PAGE_SIZE)
13360da74120SJann Horn 		return -ENOMEM;
13370da74120SJann Horn 
13380da74120SJann Horn 	/* No partial writes. */
13390da74120SJann Horn 	if (*ppos != 0)
13400da74120SJann Horn 		return -EINVAL;
13410da74120SJann Horn 
13420da74120SJann Horn 	page = memdup_user_nul(buf, count);
13430da74120SJann Horn 	if (IS_ERR(page))
13440da74120SJann Horn 		return PTR_ERR(page);
13450da74120SJann Horn 
1346*e67b7985SStephen Smalley 	mutex_lock(&selinux_state.policy_mutex);
13471da177e4SLinus Torvalds 
1348*e67b7985SStephen Smalley 	length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
1349be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__SETBOOL,
1350be0554c9SStephen Smalley 			      NULL);
13511da177e4SLinus Torvalds 	if (length)
13521da177e4SLinus Torvalds 		goto out;
13531da177e4SLinus Torvalds 
13541da177e4SLinus Torvalds 	length = -EINVAL;
13551da177e4SLinus Torvalds 	if (sscanf(page, "%d", &new_value) != 1)
13561da177e4SLinus Torvalds 		goto out;
13571da177e4SLinus Torvalds 
1358b77a493bSEric Paris 	length = 0;
13590619f0f5SStephen Smalley 	if (new_value && fsi->bool_pending_values)
1360*e67b7985SStephen Smalley 		length = security_set_bools(fsi->bool_num,
13610619f0f5SStephen Smalley 					    fsi->bool_pending_values);
13621da177e4SLinus Torvalds 
1363b77a493bSEric Paris 	if (!length)
13641da177e4SLinus Torvalds 		length = count;
13651da177e4SLinus Torvalds 
13661da177e4SLinus Torvalds out:
1367*e67b7985SStephen Smalley 	mutex_unlock(&selinux_state.policy_mutex);
13688365a719SAl Viro 	kfree(page);
13691da177e4SLinus Torvalds 	return length;
13701da177e4SLinus Torvalds }
13711da177e4SLinus Torvalds 
13729c2e08c5SArjan van de Ven static const struct file_operations sel_commit_bools_ops = {
13731da177e4SLinus Torvalds 	.write		= sel_commit_bools_write,
137457a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
13751da177e4SLinus Torvalds };
13761da177e4SLinus Torvalds 
13770c92d7c7SChristopher J. PeBenito static void sel_remove_entries(struct dentry *de)
13781da177e4SLinus Torvalds {
1379ad52184bSAl Viro 	d_genocide(de);
1380ad52184bSAl Viro 	shrink_dcache_parent(de);
13811da177e4SLinus Torvalds }
13821da177e4SLinus Torvalds 
138366ec384aSDaniel Burgener static int sel_make_bools(struct selinux_policy *newpolicy, struct dentry *bool_dir,
138466ec384aSDaniel Burgener 			  unsigned int *bool_num, char ***bool_pending_names,
1385c3fae2b2SChristian Göttsche 			  int **bool_pending_values)
13861da177e4SLinus Torvalds {
138760abd318SOndrej Mosnacek 	int ret;
13881da177e4SLinus Torvalds 	ssize_t len;
13891da177e4SLinus Torvalds 	struct dentry *dentry = NULL;
13901da177e4SLinus Torvalds 	struct inode *inode = NULL;
13911da177e4SLinus Torvalds 	struct inode_security_struct *isec;
13921da177e4SLinus Torvalds 	char **names = NULL, *page;
139360abd318SOndrej Mosnacek 	u32 i, num;
13941da177e4SLinus Torvalds 	int *values = NULL;
13951da177e4SLinus Torvalds 	u32 sid;
13961da177e4SLinus Torvalds 
1397b77a493bSEric Paris 	ret = -ENOMEM;
13981872981bSEric Paris 	page = (char *)get_zeroed_page(GFP_KERNEL);
13991872981bSEric Paris 	if (!page)
1400b77a493bSEric Paris 		goto out;
14011da177e4SLinus Torvalds 
140202a52c5cSStephen Smalley 	ret = security_get_bools(newpolicy, &num, &names, &values);
1403b77a493bSEric Paris 	if (ret)
14041da177e4SLinus Torvalds 		goto out;
14051da177e4SLinus Torvalds 
14061da177e4SLinus Torvalds 	for (i = 0; i < num; i++) {
1407b77a493bSEric Paris 		ret = -ENOMEM;
140866ec384aSDaniel Burgener 		dentry = d_alloc_name(bool_dir, names[i]);
1409b77a493bSEric Paris 		if (!dentry)
1410b77a493bSEric Paris 			goto out;
14111da177e4SLinus Torvalds 
1412b77a493bSEric Paris 		ret = -ENOMEM;
141366ec384aSDaniel Burgener 		inode = sel_make_inode(bool_dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
14147e4237faSnixiaoming 		if (!inode) {
14157e4237faSnixiaoming 			dput(dentry);
1416b77a493bSEric Paris 			goto out;
14177e4237faSnixiaoming 		}
1418b77a493bSEric Paris 
14191da177e4SLinus Torvalds 		ret = -ENAMETOOLONG;
1420cc1dad71SAl Viro 		len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
14217e4237faSnixiaoming 		if (len >= PAGE_SIZE) {
14227e4237faSnixiaoming 			dput(dentry);
14237e4237faSnixiaoming 			iput(inode);
1424b77a493bSEric Paris 			goto out;
14257e4237faSnixiaoming 		}
1426b77a493bSEric Paris 
142780788c22SCasey Schaufler 		isec = selinux_inode(inode);
142802a52c5cSStephen Smalley 		ret = selinux_policy_genfs_sid(newpolicy, "selinuxfs", page,
1429aa8e712cSStephen Smalley 					 SECCLASS_FILE, &sid);
14304262fb51SGary Tierney 		if (ret) {
1431900fde06SGary Tierney 			pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n",
1432900fde06SGary Tierney 					   page);
1433900fde06SGary Tierney 			sid = SECINITSID_SECURITY;
14344262fb51SGary Tierney 		}
14354262fb51SGary Tierney 
14361da177e4SLinus Torvalds 		isec->sid = sid;
143742059112SAndreas Gruenbacher 		isec->initialized = LABEL_INITIALIZED;
14381da177e4SLinus Torvalds 		inode->i_fop = &sel_bool_ops;
1439bce34bc0SJames Carter 		inode->i_ino = i|SEL_BOOL_INO_OFFSET;
14401da177e4SLinus Torvalds 		d_add(dentry, inode);
14411da177e4SLinus Torvalds 	}
144266ec384aSDaniel Burgener 	*bool_num = num;
144366ec384aSDaniel Burgener 	*bool_pending_names = names;
144466ec384aSDaniel Burgener 	*bool_pending_values = values;
1445b77a493bSEric Paris 
1446b77a493bSEric Paris 	free_page((unsigned long)page);
1447b77a493bSEric Paris 	return 0;
14481da177e4SLinus Torvalds out:
14491da177e4SLinus Torvalds 	free_page((unsigned long)page);
1450b77a493bSEric Paris 
14511da177e4SLinus Torvalds 	if (names) {
14529a5f04bfSJesper Juhl 		for (i = 0; i < num; i++)
14531da177e4SLinus Torvalds 			kfree(names[i]);
14541da177e4SLinus Torvalds 		kfree(names);
14551da177e4SLinus Torvalds 	}
145620c19e41SDavi Arnaut 	kfree(values);
145766ec384aSDaniel Burgener 	sel_remove_entries(bool_dir);
1458b77a493bSEric Paris 
1459b77a493bSEric Paris 	return ret;
14601da177e4SLinus Torvalds }
14611da177e4SLinus Torvalds 
14621da177e4SLinus Torvalds static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
14631da177e4SLinus Torvalds 					    size_t count, loff_t *ppos)
14641da177e4SLinus Torvalds {
14651da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
14661da177e4SLinus Torvalds 	ssize_t length;
14671da177e4SLinus Torvalds 
14686b6bc620SStephen Smalley 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1469*e67b7985SStephen Smalley 			   avc_get_cache_threshold());
14701da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
14711da177e4SLinus Torvalds }
14721da177e4SLinus Torvalds 
14731da177e4SLinus Torvalds static ssize_t sel_write_avc_cache_threshold(struct file *file,
14741da177e4SLinus Torvalds 					     const char __user *buf,
14751da177e4SLinus Torvalds 					     size_t count, loff_t *ppos)
14761da177e4SLinus Torvalds 
14771da177e4SLinus Torvalds {
14788365a719SAl Viro 	char *page;
14791da177e4SLinus Torvalds 	ssize_t ret;
1480309c5fadSHeinrich Schuchardt 	unsigned int new_value;
14811da177e4SLinus Torvalds 
1482*e67b7985SStephen Smalley 	ret = avc_has_perm(current_sid(), SECINITSID_SECURITY,
1483be0554c9SStephen Smalley 			   SECCLASS_SECURITY, SECURITY__SETSECPARAM,
1484be0554c9SStephen Smalley 			   NULL);
14851da177e4SLinus Torvalds 	if (ret)
14868365a719SAl Viro 		return ret;
1487b77a493bSEric Paris 
1488b77a493bSEric Paris 	if (count >= PAGE_SIZE)
14898365a719SAl Viro 		return -ENOMEM;
1490b77a493bSEric Paris 
1491b77a493bSEric Paris 	/* No partial writes. */
1492b77a493bSEric Paris 	if (*ppos != 0)
14938365a719SAl Viro 		return -EINVAL;
1494b77a493bSEric Paris 
14958365a719SAl Viro 	page = memdup_user_nul(buf, count);
14968365a719SAl Viro 	if (IS_ERR(page))
14978365a719SAl Viro 		return PTR_ERR(page);
1498b77a493bSEric Paris 
1499b77a493bSEric Paris 	ret = -EINVAL;
1500b77a493bSEric Paris 	if (sscanf(page, "%u", &new_value) != 1)
1501b77a493bSEric Paris 		goto out;
1502b77a493bSEric Paris 
1503*e67b7985SStephen Smalley 	avc_set_cache_threshold(new_value);
1504b77a493bSEric Paris 
15051da177e4SLinus Torvalds 	ret = count;
15061da177e4SLinus Torvalds out:
15078365a719SAl Viro 	kfree(page);
15081da177e4SLinus Torvalds 	return ret;
15091da177e4SLinus Torvalds }
15101da177e4SLinus Torvalds 
15111da177e4SLinus Torvalds static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
15121da177e4SLinus Torvalds 				       size_t count, loff_t *ppos)
15131da177e4SLinus Torvalds {
15141da177e4SLinus Torvalds 	char *page;
1515b77a493bSEric Paris 	ssize_t length;
15161da177e4SLinus Torvalds 
15171da177e4SLinus Torvalds 	page = (char *)__get_free_page(GFP_KERNEL);
1518b77a493bSEric Paris 	if (!page)
1519b77a493bSEric Paris 		return -ENOMEM;
1520b77a493bSEric Paris 
1521*e67b7985SStephen Smalley 	length = avc_get_hash_stats(page);
1522b77a493bSEric Paris 	if (length >= 0)
1523b77a493bSEric Paris 		length = simple_read_from_buffer(buf, count, ppos, page, length);
15241da177e4SLinus Torvalds 	free_page((unsigned long)page);
1525b77a493bSEric Paris 
1526b77a493bSEric Paris 	return length;
15271da177e4SLinus Torvalds }
15281da177e4SLinus Torvalds 
152966f8e2f0SJeff Vander Stoep static ssize_t sel_read_sidtab_hash_stats(struct file *filp, char __user *buf,
153066f8e2f0SJeff Vander Stoep 					size_t count, loff_t *ppos)
153166f8e2f0SJeff Vander Stoep {
153266f8e2f0SJeff Vander Stoep 	char *page;
153366f8e2f0SJeff Vander Stoep 	ssize_t length;
153466f8e2f0SJeff Vander Stoep 
153566f8e2f0SJeff Vander Stoep 	page = (char *)__get_free_page(GFP_KERNEL);
153666f8e2f0SJeff Vander Stoep 	if (!page)
153766f8e2f0SJeff Vander Stoep 		return -ENOMEM;
153866f8e2f0SJeff Vander Stoep 
1539*e67b7985SStephen Smalley 	length = security_sidtab_hash_stats(page);
154066f8e2f0SJeff Vander Stoep 	if (length >= 0)
154166f8e2f0SJeff Vander Stoep 		length = simple_read_from_buffer(buf, count, ppos, page,
154266f8e2f0SJeff Vander Stoep 						length);
154366f8e2f0SJeff Vander Stoep 	free_page((unsigned long)page);
154466f8e2f0SJeff Vander Stoep 
154566f8e2f0SJeff Vander Stoep 	return length;
154666f8e2f0SJeff Vander Stoep }
154766f8e2f0SJeff Vander Stoep 
154866f8e2f0SJeff Vander Stoep static const struct file_operations sel_sidtab_hash_stats_ops = {
154966f8e2f0SJeff Vander Stoep 	.read		= sel_read_sidtab_hash_stats,
155066f8e2f0SJeff Vander Stoep 	.llseek		= generic_file_llseek,
155166f8e2f0SJeff Vander Stoep };
155266f8e2f0SJeff Vander Stoep 
15539c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_threshold_ops = {
15541da177e4SLinus Torvalds 	.read		= sel_read_avc_cache_threshold,
15551da177e4SLinus Torvalds 	.write		= sel_write_avc_cache_threshold,
155657a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
15571da177e4SLinus Torvalds };
15581da177e4SLinus Torvalds 
15599c2e08c5SArjan van de Ven static const struct file_operations sel_avc_hash_stats_ops = {
15601da177e4SLinus Torvalds 	.read		= sel_read_avc_hash_stats,
156157a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
15621da177e4SLinus Torvalds };
15631da177e4SLinus Torvalds 
15641da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
15651da177e4SLinus Torvalds static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
15661da177e4SLinus Torvalds {
15671da177e4SLinus Torvalds 	int cpu;
15681da177e4SLinus Torvalds 
15694f4b6c1aSRusty Russell 	for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
15701da177e4SLinus Torvalds 		if (!cpu_possible(cpu))
15711da177e4SLinus Torvalds 			continue;
15721da177e4SLinus Torvalds 		*idx = cpu + 1;
15731da177e4SLinus Torvalds 		return &per_cpu(avc_cache_stats, cpu);
15741da177e4SLinus Torvalds 	}
15758d269a8eSVasily Averin 	(*idx)++;
15761da177e4SLinus Torvalds 	return NULL;
15771da177e4SLinus Torvalds }
15781da177e4SLinus Torvalds 
15791da177e4SLinus Torvalds static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
15801da177e4SLinus Torvalds {
15811da177e4SLinus Torvalds 	loff_t n = *pos - 1;
15821da177e4SLinus Torvalds 
15831da177e4SLinus Torvalds 	if (*pos == 0)
15841da177e4SLinus Torvalds 		return SEQ_START_TOKEN;
15851da177e4SLinus Torvalds 
15861da177e4SLinus Torvalds 	return sel_avc_get_stat_idx(&n);
15871da177e4SLinus Torvalds }
15881da177e4SLinus Torvalds 
15891da177e4SLinus Torvalds static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
15901da177e4SLinus Torvalds {
15911da177e4SLinus Torvalds 	return sel_avc_get_stat_idx(pos);
15921da177e4SLinus Torvalds }
15931da177e4SLinus Torvalds 
15941da177e4SLinus Torvalds static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
15951da177e4SLinus Torvalds {
15961da177e4SLinus Torvalds 	struct avc_cache_stats *st = v;
15971da177e4SLinus Torvalds 
1598710a0647SMarkus Elfring 	if (v == SEQ_START_TOKEN) {
1599710a0647SMarkus Elfring 		seq_puts(seq,
1600710a0647SMarkus Elfring 			 "lookups hits misses allocations reclaims frees\n");
1601710a0647SMarkus Elfring 	} else {
1602257313b2SLinus Torvalds 		unsigned int lookups = st->lookups;
1603257313b2SLinus Torvalds 		unsigned int misses = st->misses;
1604257313b2SLinus Torvalds 		unsigned int hits = lookups - misses;
1605257313b2SLinus Torvalds 		seq_printf(seq, "%u %u %u %u %u %u\n", lookups,
1606257313b2SLinus Torvalds 			   hits, misses, st->allocations,
16071da177e4SLinus Torvalds 			   st->reclaims, st->frees);
1608257313b2SLinus Torvalds 	}
16091da177e4SLinus Torvalds 	return 0;
16101da177e4SLinus Torvalds }
16111da177e4SLinus Torvalds 
16121da177e4SLinus Torvalds static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
16131da177e4SLinus Torvalds { }
16141da177e4SLinus Torvalds 
16151996a109SJan Engelhardt static const struct seq_operations sel_avc_cache_stats_seq_ops = {
16161da177e4SLinus Torvalds 	.start		= sel_avc_stats_seq_start,
16171da177e4SLinus Torvalds 	.next		= sel_avc_stats_seq_next,
16181da177e4SLinus Torvalds 	.show		= sel_avc_stats_seq_show,
16191da177e4SLinus Torvalds 	.stop		= sel_avc_stats_seq_stop,
16201da177e4SLinus Torvalds };
16211da177e4SLinus Torvalds 
16221da177e4SLinus Torvalds static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
16231da177e4SLinus Torvalds {
16241da177e4SLinus Torvalds 	return seq_open(file, &sel_avc_cache_stats_seq_ops);
16251da177e4SLinus Torvalds }
16261da177e4SLinus Torvalds 
16279c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_stats_ops = {
16281da177e4SLinus Torvalds 	.open		= sel_open_avc_cache_stats,
16291da177e4SLinus Torvalds 	.read		= seq_read,
16301da177e4SLinus Torvalds 	.llseek		= seq_lseek,
16311da177e4SLinus Torvalds 	.release	= seq_release,
16321da177e4SLinus Torvalds };
16331da177e4SLinus Torvalds #endif
16341da177e4SLinus Torvalds 
16351da177e4SLinus Torvalds static int sel_make_avc_files(struct dentry *dir)
16361da177e4SLinus Torvalds {
16370619f0f5SStephen Smalley 	struct super_block *sb = dir->d_sb;
16380619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = sb->s_fs_info;
1639b77a493bSEric Paris 	int i;
1640cda37124SEric Biggers 	static const struct tree_descr files[] = {
16411da177e4SLinus Torvalds 		{ "cache_threshold",
16421da177e4SLinus Torvalds 		  &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
16431da177e4SLinus Torvalds 		{ "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
16441da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
16451da177e4SLinus Torvalds 		{ "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
16461da177e4SLinus Torvalds #endif
16471da177e4SLinus Torvalds 	};
16481da177e4SLinus Torvalds 
16496e20a64aSNicolas Kaiser 	for (i = 0; i < ARRAY_SIZE(files); i++) {
16501da177e4SLinus Torvalds 		struct inode *inode;
16511da177e4SLinus Torvalds 		struct dentry *dentry;
16521da177e4SLinus Torvalds 
16531da177e4SLinus Torvalds 		dentry = d_alloc_name(dir, files[i].name);
1654b77a493bSEric Paris 		if (!dentry)
1655b77a493bSEric Paris 			return -ENOMEM;
16561da177e4SLinus Torvalds 
16571da177e4SLinus Torvalds 		inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
16587e4237faSnixiaoming 		if (!inode) {
16597e4237faSnixiaoming 			dput(dentry);
1660b77a493bSEric Paris 			return -ENOMEM;
16617e4237faSnixiaoming 		}
1662b77a493bSEric Paris 
16631da177e4SLinus Torvalds 		inode->i_fop = files[i].ops;
16640619f0f5SStephen Smalley 		inode->i_ino = ++fsi->last_ino;
16651da177e4SLinus Torvalds 		d_add(dentry, inode);
16661da177e4SLinus Torvalds 	}
1667b77a493bSEric Paris 
1668b77a493bSEric Paris 	return 0;
16691da177e4SLinus Torvalds }
16701da177e4SLinus Torvalds 
167166f8e2f0SJeff Vander Stoep static int sel_make_ss_files(struct dentry *dir)
167266f8e2f0SJeff Vander Stoep {
167366f8e2f0SJeff Vander Stoep 	struct super_block *sb = dir->d_sb;
167466f8e2f0SJeff Vander Stoep 	struct selinux_fs_info *fsi = sb->s_fs_info;
167566f8e2f0SJeff Vander Stoep 	int i;
167666f8e2f0SJeff Vander Stoep 	static struct tree_descr files[] = {
167766f8e2f0SJeff Vander Stoep 		{ "sidtab_hash_stats", &sel_sidtab_hash_stats_ops, S_IRUGO },
167866f8e2f0SJeff Vander Stoep 	};
167966f8e2f0SJeff Vander Stoep 
168066f8e2f0SJeff Vander Stoep 	for (i = 0; i < ARRAY_SIZE(files); i++) {
168166f8e2f0SJeff Vander Stoep 		struct inode *inode;
168266f8e2f0SJeff Vander Stoep 		struct dentry *dentry;
168366f8e2f0SJeff Vander Stoep 
168466f8e2f0SJeff Vander Stoep 		dentry = d_alloc_name(dir, files[i].name);
168566f8e2f0SJeff Vander Stoep 		if (!dentry)
168666f8e2f0SJeff Vander Stoep 			return -ENOMEM;
168766f8e2f0SJeff Vander Stoep 
168866f8e2f0SJeff Vander Stoep 		inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
168966f8e2f0SJeff Vander Stoep 		if (!inode) {
169066f8e2f0SJeff Vander Stoep 			dput(dentry);
169166f8e2f0SJeff Vander Stoep 			return -ENOMEM;
169266f8e2f0SJeff Vander Stoep 		}
169366f8e2f0SJeff Vander Stoep 
169466f8e2f0SJeff Vander Stoep 		inode->i_fop = files[i].ops;
169566f8e2f0SJeff Vander Stoep 		inode->i_ino = ++fsi->last_ino;
169666f8e2f0SJeff Vander Stoep 		d_add(dentry, inode);
169766f8e2f0SJeff Vander Stoep 	}
169866f8e2f0SJeff Vander Stoep 
169966f8e2f0SJeff Vander Stoep 	return 0;
170066f8e2f0SJeff Vander Stoep }
170166f8e2f0SJeff Vander Stoep 
1702f0ee2e46SJames Carter static ssize_t sel_read_initcon(struct file *file, char __user *buf,
1703f0ee2e46SJames Carter 				size_t count, loff_t *ppos)
1704f0ee2e46SJames Carter {
1705f0ee2e46SJames Carter 	char *con;
1706f0ee2e46SJames Carter 	u32 sid, len;
1707f0ee2e46SJames Carter 	ssize_t ret;
1708f0ee2e46SJames Carter 
1709496ad9aaSAl Viro 	sid = file_inode(file)->i_ino&SEL_INO_MASK;
1710*e67b7985SStephen Smalley 	ret = security_sid_to_context(sid, &con, &len);
1711b77a493bSEric Paris 	if (ret)
1712f0ee2e46SJames Carter 		return ret;
1713f0ee2e46SJames Carter 
1714f0ee2e46SJames Carter 	ret = simple_read_from_buffer(buf, count, ppos, con, len);
1715f0ee2e46SJames Carter 	kfree(con);
1716f0ee2e46SJames Carter 	return ret;
1717f0ee2e46SJames Carter }
1718f0ee2e46SJames Carter 
1719f0ee2e46SJames Carter static const struct file_operations sel_initcon_ops = {
1720f0ee2e46SJames Carter 	.read		= sel_read_initcon,
172157a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
1722f0ee2e46SJames Carter };
1723f0ee2e46SJames Carter 
1724f0ee2e46SJames Carter static int sel_make_initcon_files(struct dentry *dir)
1725f0ee2e46SJames Carter {
1726b77a493bSEric Paris 	int i;
1727f0ee2e46SJames Carter 
1728f0ee2e46SJames Carter 	for (i = 1; i <= SECINITSID_NUM; i++) {
1729f0ee2e46SJames Carter 		struct inode *inode;
1730f0ee2e46SJames Carter 		struct dentry *dentry;
1731e3e0b582SStephen Smalley 		const char *s = security_get_initial_sid_context(i);
1732e3e0b582SStephen Smalley 
1733e3e0b582SStephen Smalley 		if (!s)
1734e3e0b582SStephen Smalley 			continue;
1735e3e0b582SStephen Smalley 		dentry = d_alloc_name(dir, s);
1736b77a493bSEric Paris 		if (!dentry)
1737b77a493bSEric Paris 			return -ENOMEM;
1738f0ee2e46SJames Carter 
1739f0ee2e46SJames Carter 		inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
17407e4237faSnixiaoming 		if (!inode) {
17417e4237faSnixiaoming 			dput(dentry);
1742b77a493bSEric Paris 			return -ENOMEM;
17437e4237faSnixiaoming 		}
1744b77a493bSEric Paris 
1745f0ee2e46SJames Carter 		inode->i_fop = &sel_initcon_ops;
1746f0ee2e46SJames Carter 		inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1747f0ee2e46SJames Carter 		d_add(dentry, inode);
1748f0ee2e46SJames Carter 	}
1749b77a493bSEric Paris 
1750b77a493bSEric Paris 	return 0;
1751f0ee2e46SJames Carter }
1752f0ee2e46SJames Carter 
1753e47c8fc5SChristopher J. PeBenito static inline unsigned long sel_class_to_ino(u16 class)
1754e47c8fc5SChristopher J. PeBenito {
1755e47c8fc5SChristopher J. PeBenito 	return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1756e47c8fc5SChristopher J. PeBenito }
1757e47c8fc5SChristopher J. PeBenito 
1758e47c8fc5SChristopher J. PeBenito static inline u16 sel_ino_to_class(unsigned long ino)
1759e47c8fc5SChristopher J. PeBenito {
176092ae9e82SEric Paris 	return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1);
1761e47c8fc5SChristopher J. PeBenito }
1762e47c8fc5SChristopher J. PeBenito 
1763e47c8fc5SChristopher J. PeBenito static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1764e47c8fc5SChristopher J. PeBenito {
1765e47c8fc5SChristopher J. PeBenito 	return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1766e47c8fc5SChristopher J. PeBenito }
1767e47c8fc5SChristopher J. PeBenito 
1768e47c8fc5SChristopher J. PeBenito static inline u32 sel_ino_to_perm(unsigned long ino)
1769e47c8fc5SChristopher J. PeBenito {
1770e47c8fc5SChristopher J. PeBenito 	return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1771e47c8fc5SChristopher J. PeBenito }
1772e47c8fc5SChristopher J. PeBenito 
1773e47c8fc5SChristopher J. PeBenito static ssize_t sel_read_class(struct file *file, char __user *buf,
1774e47c8fc5SChristopher J. PeBenito 				size_t count, loff_t *ppos)
1775e47c8fc5SChristopher J. PeBenito {
1776496ad9aaSAl Viro 	unsigned long ino = file_inode(file)->i_ino;
1777cc1dad71SAl Viro 	char res[TMPBUFLEN];
17787e78c875Sliuyang34 	ssize_t len = scnprintf(res, sizeof(res), "%d", sel_ino_to_class(ino));
1779cc1dad71SAl Viro 	return simple_read_from_buffer(buf, count, ppos, res, len);
1780e47c8fc5SChristopher J. PeBenito }
1781e47c8fc5SChristopher J. PeBenito 
1782e47c8fc5SChristopher J. PeBenito static const struct file_operations sel_class_ops = {
1783e47c8fc5SChristopher J. PeBenito 	.read		= sel_read_class,
178457a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
1785e47c8fc5SChristopher J. PeBenito };
1786e47c8fc5SChristopher J. PeBenito 
1787e47c8fc5SChristopher J. PeBenito static ssize_t sel_read_perm(struct file *file, char __user *buf,
1788e47c8fc5SChristopher J. PeBenito 				size_t count, loff_t *ppos)
1789e47c8fc5SChristopher J. PeBenito {
1790496ad9aaSAl Viro 	unsigned long ino = file_inode(file)->i_ino;
1791cc1dad71SAl Viro 	char res[TMPBUFLEN];
17927e78c875Sliuyang34 	ssize_t len = scnprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino));
1793cc1dad71SAl Viro 	return simple_read_from_buffer(buf, count, ppos, res, len);
1794e47c8fc5SChristopher J. PeBenito }
1795e47c8fc5SChristopher J. PeBenito 
1796e47c8fc5SChristopher J. PeBenito static const struct file_operations sel_perm_ops = {
1797e47c8fc5SChristopher J. PeBenito 	.read		= sel_read_perm,
179857a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
1799e47c8fc5SChristopher J. PeBenito };
1800e47c8fc5SChristopher J. PeBenito 
18013bb56b25SPaul Moore static ssize_t sel_read_policycap(struct file *file, char __user *buf,
18023bb56b25SPaul Moore 				  size_t count, loff_t *ppos)
18033bb56b25SPaul Moore {
18043bb56b25SPaul Moore 	int value;
18053bb56b25SPaul Moore 	char tmpbuf[TMPBUFLEN];
18063bb56b25SPaul Moore 	ssize_t length;
1807496ad9aaSAl Viro 	unsigned long i_ino = file_inode(file)->i_ino;
18083bb56b25SPaul Moore 
1809*e67b7985SStephen Smalley 	value = security_policycap_supported(i_ino & SEL_INO_MASK);
18103bb56b25SPaul Moore 	length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
18113bb56b25SPaul Moore 
18123bb56b25SPaul Moore 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
18133bb56b25SPaul Moore }
18143bb56b25SPaul Moore 
18153bb56b25SPaul Moore static const struct file_operations sel_policycap_ops = {
18163bb56b25SPaul Moore 	.read		= sel_read_policycap,
181757a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
18183bb56b25SPaul Moore };
18193bb56b25SPaul Moore 
182002a52c5cSStephen Smalley static int sel_make_perm_files(struct selinux_policy *newpolicy,
182102a52c5cSStephen Smalley 			char *objclass, int classvalue,
1822e47c8fc5SChristopher J. PeBenito 			struct dentry *dir)
1823e47c8fc5SChristopher J. PeBenito {
1824b77a493bSEric Paris 	int i, rc, nperms;
1825e47c8fc5SChristopher J. PeBenito 	char **perms;
1826e47c8fc5SChristopher J. PeBenito 
182702a52c5cSStephen Smalley 	rc = security_get_permissions(newpolicy, objclass, &perms, &nperms);
1828e47c8fc5SChristopher J. PeBenito 	if (rc)
1829b77a493bSEric Paris 		return rc;
1830e47c8fc5SChristopher J. PeBenito 
1831e47c8fc5SChristopher J. PeBenito 	for (i = 0; i < nperms; i++) {
1832e47c8fc5SChristopher J. PeBenito 		struct inode *inode;
1833e47c8fc5SChristopher J. PeBenito 		struct dentry *dentry;
1834e47c8fc5SChristopher J. PeBenito 
1835b77a493bSEric Paris 		rc = -ENOMEM;
1836e47c8fc5SChristopher J. PeBenito 		dentry = d_alloc_name(dir, perms[i]);
1837b77a493bSEric Paris 		if (!dentry)
1838b77a493bSEric Paris 			goto out;
1839e47c8fc5SChristopher J. PeBenito 
1840e47c8fc5SChristopher J. PeBenito 		rc = -ENOMEM;
1841b77a493bSEric Paris 		inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
18427e4237faSnixiaoming 		if (!inode) {
18437e4237faSnixiaoming 			dput(dentry);
1844b77a493bSEric Paris 			goto out;
18457e4237faSnixiaoming 		}
1846b77a493bSEric Paris 
1847e47c8fc5SChristopher J. PeBenito 		inode->i_fop = &sel_perm_ops;
1848e47c8fc5SChristopher J. PeBenito 		/* i+1 since perm values are 1-indexed */
1849e47c8fc5SChristopher J. PeBenito 		inode->i_ino = sel_perm_to_ino(classvalue, i + 1);
1850e47c8fc5SChristopher J. PeBenito 		d_add(dentry, inode);
1851e47c8fc5SChristopher J. PeBenito 	}
1852b77a493bSEric Paris 	rc = 0;
1853b77a493bSEric Paris out:
1854e47c8fc5SChristopher J. PeBenito 	for (i = 0; i < nperms; i++)
1855e47c8fc5SChristopher J. PeBenito 		kfree(perms[i]);
1856e47c8fc5SChristopher J. PeBenito 	kfree(perms);
1857e47c8fc5SChristopher J. PeBenito 	return rc;
1858e47c8fc5SChristopher J. PeBenito }
1859e47c8fc5SChristopher J. PeBenito 
186002a52c5cSStephen Smalley static int sel_make_class_dir_entries(struct selinux_policy *newpolicy,
186102a52c5cSStephen Smalley 				char *classname, int index,
1862e47c8fc5SChristopher J. PeBenito 				struct dentry *dir)
1863e47c8fc5SChristopher J. PeBenito {
18640619f0f5SStephen Smalley 	struct super_block *sb = dir->d_sb;
18650619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = sb->s_fs_info;
1866e47c8fc5SChristopher J. PeBenito 	struct dentry *dentry = NULL;
1867e47c8fc5SChristopher J. PeBenito 	struct inode *inode = NULL;
1868e47c8fc5SChristopher J. PeBenito 
1869e47c8fc5SChristopher J. PeBenito 	dentry = d_alloc_name(dir, "index");
1870b77a493bSEric Paris 	if (!dentry)
1871b77a493bSEric Paris 		return -ENOMEM;
1872e47c8fc5SChristopher J. PeBenito 
1873e47c8fc5SChristopher J. PeBenito 	inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
18747e4237faSnixiaoming 	if (!inode) {
18757e4237faSnixiaoming 		dput(dentry);
1876b77a493bSEric Paris 		return -ENOMEM;
18777e4237faSnixiaoming 	}
1878e47c8fc5SChristopher J. PeBenito 
1879e47c8fc5SChristopher J. PeBenito 	inode->i_fop = &sel_class_ops;
1880e47c8fc5SChristopher J. PeBenito 	inode->i_ino = sel_class_to_ino(index);
1881e47c8fc5SChristopher J. PeBenito 	d_add(dentry, inode);
1882e47c8fc5SChristopher J. PeBenito 
18830619f0f5SStephen Smalley 	dentry = sel_make_dir(dir, "perms", &fsi->last_class_ino);
1884a1c2aa1eSAl Viro 	if (IS_ERR(dentry))
1885a1c2aa1eSAl Viro 		return PTR_ERR(dentry);
1886e47c8fc5SChristopher J. PeBenito 
18875698f081Sye xingchen 	return sel_make_perm_files(newpolicy, classname, index, dentry);
1888e47c8fc5SChristopher J. PeBenito }
1889e47c8fc5SChristopher J. PeBenito 
189066ec384aSDaniel Burgener static int sel_make_classes(struct selinux_policy *newpolicy,
189166ec384aSDaniel Burgener 			    struct dentry *class_dir,
189266ec384aSDaniel Burgener 			    unsigned long *last_class_ino)
1893e47c8fc5SChristopher J. PeBenito {
18940619f0f5SStephen Smalley 
1895b77a493bSEric Paris 	int rc, nclasses, i;
1896e47c8fc5SChristopher J. PeBenito 	char **classes;
1897e47c8fc5SChristopher J. PeBenito 
189802a52c5cSStephen Smalley 	rc = security_get_classes(newpolicy, &classes, &nclasses);
1899b77a493bSEric Paris 	if (rc)
1900b77a493bSEric Paris 		return rc;
1901e47c8fc5SChristopher J. PeBenito 
1902e47c8fc5SChristopher J. PeBenito 	/* +2 since classes are 1-indexed */
190366ec384aSDaniel Burgener 	*last_class_ino = sel_class_to_ino(nclasses + 2);
1904e47c8fc5SChristopher J. PeBenito 
1905e47c8fc5SChristopher J. PeBenito 	for (i = 0; i < nclasses; i++) {
1906e47c8fc5SChristopher J. PeBenito 		struct dentry *class_name_dir;
1907e47c8fc5SChristopher J. PeBenito 
190866ec384aSDaniel Burgener 		class_name_dir = sel_make_dir(class_dir, classes[i],
190966ec384aSDaniel Burgener 					      last_class_ino);
1910a1c2aa1eSAl Viro 		if (IS_ERR(class_name_dir)) {
1911a1c2aa1eSAl Viro 			rc = PTR_ERR(class_name_dir);
1912b77a493bSEric Paris 			goto out;
1913a1c2aa1eSAl Viro 		}
1914e47c8fc5SChristopher J. PeBenito 
1915e47c8fc5SChristopher J. PeBenito 		/* i+1 since class values are 1-indexed */
191602a52c5cSStephen Smalley 		rc = sel_make_class_dir_entries(newpolicy, classes[i], i + 1,
1917e47c8fc5SChristopher J. PeBenito 				class_name_dir);
1918e47c8fc5SChristopher J. PeBenito 		if (rc)
1919b77a493bSEric Paris 			goto out;
1920e47c8fc5SChristopher J. PeBenito 	}
1921b77a493bSEric Paris 	rc = 0;
1922b77a493bSEric Paris out:
1923e47c8fc5SChristopher J. PeBenito 	for (i = 0; i < nclasses; i++)
1924e47c8fc5SChristopher J. PeBenito 		kfree(classes[i]);
1925e47c8fc5SChristopher J. PeBenito 	kfree(classes);
1926e47c8fc5SChristopher J. PeBenito 	return rc;
1927e47c8fc5SChristopher J. PeBenito }
1928e47c8fc5SChristopher J. PeBenito 
19290619f0f5SStephen Smalley static int sel_make_policycap(struct selinux_fs_info *fsi)
19303bb56b25SPaul Moore {
19313bb56b25SPaul Moore 	unsigned int iter;
19323bb56b25SPaul Moore 	struct dentry *dentry = NULL;
19333bb56b25SPaul Moore 	struct inode *inode = NULL;
19343bb56b25SPaul Moore 
1935cdbec3edSPaul Moore 	for (iter = 0; iter <= POLICYDB_CAP_MAX; iter++) {
19364dc2fce3SStephen Smalley 		if (iter < ARRAY_SIZE(selinux_policycap_names))
19370619f0f5SStephen Smalley 			dentry = d_alloc_name(fsi->policycap_dir,
19384dc2fce3SStephen Smalley 					      selinux_policycap_names[iter]);
19393bb56b25SPaul Moore 		else
19400619f0f5SStephen Smalley 			dentry = d_alloc_name(fsi->policycap_dir, "unknown");
19413bb56b25SPaul Moore 
19423bb56b25SPaul Moore 		if (dentry == NULL)
19433bb56b25SPaul Moore 			return -ENOMEM;
19443bb56b25SPaul Moore 
19450619f0f5SStephen Smalley 		inode = sel_make_inode(fsi->sb, S_IFREG | 0444);
19467e4237faSnixiaoming 		if (inode == NULL) {
19477e4237faSnixiaoming 			dput(dentry);
19483bb56b25SPaul Moore 			return -ENOMEM;
19497e4237faSnixiaoming 		}
19503bb56b25SPaul Moore 
19513bb56b25SPaul Moore 		inode->i_fop = &sel_policycap_ops;
19523bb56b25SPaul Moore 		inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
19533bb56b25SPaul Moore 		d_add(dentry, inode);
19543bb56b25SPaul Moore 	}
19553bb56b25SPaul Moore 
19563bb56b25SPaul Moore 	return 0;
19573bb56b25SPaul Moore }
19583bb56b25SPaul Moore 
1959a1c2aa1eSAl Viro static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
19600dd4ae51SChristopher J. PeBenito 			unsigned long *ino)
19611da177e4SLinus Torvalds {
1962a1c2aa1eSAl Viro 	struct dentry *dentry = d_alloc_name(dir, name);
19631da177e4SLinus Torvalds 	struct inode *inode;
19641da177e4SLinus Torvalds 
1965a1c2aa1eSAl Viro 	if (!dentry)
1966a1c2aa1eSAl Viro 		return ERR_PTR(-ENOMEM);
1967a1c2aa1eSAl Viro 
1968a1c2aa1eSAl Viro 	inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1969a1c2aa1eSAl Viro 	if (!inode) {
1970a1c2aa1eSAl Viro 		dput(dentry);
1971a1c2aa1eSAl Viro 		return ERR_PTR(-ENOMEM);
1972a1c2aa1eSAl Viro 	}
1973b77a493bSEric Paris 
19741da177e4SLinus Torvalds 	inode->i_op = &simple_dir_inode_operations;
19751da177e4SLinus Torvalds 	inode->i_fop = &simple_dir_operations;
19760dd4ae51SChristopher J. PeBenito 	inode->i_ino = ++(*ino);
197740e906f8SJames Morris 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
1978d8c76e6fSDave Hansen 	inc_nlink(inode);
19791da177e4SLinus Torvalds 	d_add(dentry, inode);
1980edb20fb5SJames Morris 	/* bump link count on parent directory, too */
1981ce0b16ddSDavid Howells 	inc_nlink(d_inode(dir));
1982b77a493bSEric Paris 
1983a1c2aa1eSAl Viro 	return dentry;
19841da177e4SLinus Torvalds }
19851da177e4SLinus Torvalds 
19860eea6091SDaniel Burgener static struct dentry *sel_make_disconnected_dir(struct super_block *sb,
19870eea6091SDaniel Burgener 						unsigned long *ino)
19880eea6091SDaniel Burgener {
19890eea6091SDaniel Burgener 	struct inode *inode = sel_make_inode(sb, S_IFDIR | S_IRUGO | S_IXUGO);
19900eea6091SDaniel Burgener 
19910eea6091SDaniel Burgener 	if (!inode)
19920eea6091SDaniel Burgener 		return ERR_PTR(-ENOMEM);
19930eea6091SDaniel Burgener 
19940eea6091SDaniel Burgener 	inode->i_op = &simple_dir_inode_operations;
19950eea6091SDaniel Burgener 	inode->i_fop = &simple_dir_operations;
19960eea6091SDaniel Burgener 	inode->i_ino = ++(*ino);
19970eea6091SDaniel Burgener 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
19980eea6091SDaniel Burgener 	inc_nlink(inode);
19990eea6091SDaniel Burgener 	return d_obtain_alias(inode);
20000eea6091SDaniel Burgener }
20010eea6091SDaniel Burgener 
20020619f0f5SStephen Smalley #define NULL_FILE_NAME "null"
20030619f0f5SStephen Smalley 
2004920f50b2SDavid Howells static int sel_fill_super(struct super_block *sb, struct fs_context *fc)
20051da177e4SLinus Torvalds {
20060619f0f5SStephen Smalley 	struct selinux_fs_info *fsi;
20071da177e4SLinus Torvalds 	int ret;
20081da177e4SLinus Torvalds 	struct dentry *dentry;
2009a1c2aa1eSAl Viro 	struct inode *inode;
20101da177e4SLinus Torvalds 	struct inode_security_struct *isec;
20111da177e4SLinus Torvalds 
2012cda37124SEric Biggers 	static const struct tree_descr selinux_files[] = {
20131da177e4SLinus Torvalds 		[SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
20141da177e4SLinus Torvalds 		[SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
2015ce9982d0SStephen Smalley 		[SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
20161da177e4SLinus Torvalds 		[SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
20171da177e4SLinus Torvalds 		[SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
20181da177e4SLinus Torvalds 		[SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
20191da177e4SLinus Torvalds 		[SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
20201da177e4SLinus Torvalds 		[SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
20211da177e4SLinus Torvalds 		[SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
20221da177e4SLinus Torvalds 		[SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
20231da177e4SLinus Torvalds 		[SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
20241da177e4SLinus Torvalds 		[SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
20251da177e4SLinus Torvalds 		[SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
20263f12070eSEric Paris 		[SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
20273f12070eSEric Paris 		[SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
202811904167SKaiGai Kohei 		[SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO},
202972e8c859SEric Paris 		[SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO},
2030f9df6458SAndrew Perepechko 		[SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops,
2031f9df6458SAndrew Perepechko 					S_IWUGO},
20321da177e4SLinus Torvalds 		/* last one */ {""}
20331da177e4SLinus Torvalds 	};
20340619f0f5SStephen Smalley 
20350619f0f5SStephen Smalley 	ret = selinux_fs_info_create(sb);
20360619f0f5SStephen Smalley 	if (ret)
20370619f0f5SStephen Smalley 		goto err;
20380619f0f5SStephen Smalley 
20391da177e4SLinus Torvalds 	ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
20401da177e4SLinus Torvalds 	if (ret)
2041161ce45aSJames Morris 		goto err;
20421da177e4SLinus Torvalds 
20430619f0f5SStephen Smalley 	fsi = sb->s_fs_info;
20440619f0f5SStephen Smalley 	fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino);
20450619f0f5SStephen Smalley 	if (IS_ERR(fsi->bool_dir)) {
20460619f0f5SStephen Smalley 		ret = PTR_ERR(fsi->bool_dir);
20470619f0f5SStephen Smalley 		fsi->bool_dir = NULL;
2048161ce45aSJames Morris 		goto err;
2049a1c2aa1eSAl Viro 	}
20501da177e4SLinus Torvalds 
2051b77a493bSEric Paris 	ret = -ENOMEM;
20521da177e4SLinus Torvalds 	dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
2053b77a493bSEric Paris 	if (!dentry)
2054161ce45aSJames Morris 		goto err;
20551da177e4SLinus Torvalds 
2056161ce45aSJames Morris 	ret = -ENOMEM;
2057b77a493bSEric Paris 	inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
20587e4237faSnixiaoming 	if (!inode) {
20597e4237faSnixiaoming 		dput(dentry);
2060161ce45aSJames Morris 		goto err;
20617e4237faSnixiaoming 	}
2062b77a493bSEric Paris 
20630619f0f5SStephen Smalley 	inode->i_ino = ++fsi->last_ino;
206480788c22SCasey Schaufler 	isec = selinux_inode(inode);
20651da177e4SLinus Torvalds 	isec->sid = SECINITSID_DEVNULL;
20661da177e4SLinus Torvalds 	isec->sclass = SECCLASS_CHR_FILE;
206742059112SAndreas Gruenbacher 	isec->initialized = LABEL_INITIALIZED;
20681da177e4SLinus Torvalds 
20691da177e4SLinus Torvalds 	init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
20701da177e4SLinus Torvalds 	d_add(dentry, inode);
20711da177e4SLinus Torvalds 
20720619f0f5SStephen Smalley 	dentry = sel_make_dir(sb->s_root, "avc", &fsi->last_ino);
2073a1c2aa1eSAl Viro 	if (IS_ERR(dentry)) {
2074a1c2aa1eSAl Viro 		ret = PTR_ERR(dentry);
2075161ce45aSJames Morris 		goto err;
2076a1c2aa1eSAl Viro 	}
20771da177e4SLinus Torvalds 
20781da177e4SLinus Torvalds 	ret = sel_make_avc_files(dentry);
2079bcb62828SChristian Göttsche 	if (ret)
2080bcb62828SChristian Göttsche 		goto err;
208166f8e2f0SJeff Vander Stoep 
208266f8e2f0SJeff Vander Stoep 	dentry = sel_make_dir(sb->s_root, "ss", &fsi->last_ino);
208366f8e2f0SJeff Vander Stoep 	if (IS_ERR(dentry)) {
208466f8e2f0SJeff Vander Stoep 		ret = PTR_ERR(dentry);
208566f8e2f0SJeff Vander Stoep 		goto err;
208666f8e2f0SJeff Vander Stoep 	}
208766f8e2f0SJeff Vander Stoep 
208866f8e2f0SJeff Vander Stoep 	ret = sel_make_ss_files(dentry);
20891da177e4SLinus Torvalds 	if (ret)
2090161ce45aSJames Morris 		goto err;
2091f0ee2e46SJames Carter 
20920619f0f5SStephen Smalley 	dentry = sel_make_dir(sb->s_root, "initial_contexts", &fsi->last_ino);
2093a1c2aa1eSAl Viro 	if (IS_ERR(dentry)) {
2094a1c2aa1eSAl Viro 		ret = PTR_ERR(dentry);
2095f0ee2e46SJames Carter 		goto err;
2096a1c2aa1eSAl Viro 	}
2097f0ee2e46SJames Carter 
2098f0ee2e46SJames Carter 	ret = sel_make_initcon_files(dentry);
2099f0ee2e46SJames Carter 	if (ret)
2100f0ee2e46SJames Carter 		goto err;
2101f0ee2e46SJames Carter 
2102613ba187SDaniel Burgener 	fsi->class_dir = sel_make_dir(sb->s_root, CLASS_DIR_NAME, &fsi->last_ino);
21030619f0f5SStephen Smalley 	if (IS_ERR(fsi->class_dir)) {
21040619f0f5SStephen Smalley 		ret = PTR_ERR(fsi->class_dir);
21050619f0f5SStephen Smalley 		fsi->class_dir = NULL;
2106e47c8fc5SChristopher J. PeBenito 		goto err;
2107a1c2aa1eSAl Viro 	}
2108e47c8fc5SChristopher J. PeBenito 
2109613ba187SDaniel Burgener 	fsi->policycap_dir = sel_make_dir(sb->s_root, POLICYCAP_DIR_NAME,
21100619f0f5SStephen Smalley 					  &fsi->last_ino);
21110619f0f5SStephen Smalley 	if (IS_ERR(fsi->policycap_dir)) {
21120619f0f5SStephen Smalley 		ret = PTR_ERR(fsi->policycap_dir);
21130619f0f5SStephen Smalley 		fsi->policycap_dir = NULL;
2114e47c8fc5SChristopher J. PeBenito 		goto err;
2115a1c2aa1eSAl Viro 	}
21160619f0f5SStephen Smalley 
211702a52c5cSStephen Smalley 	ret = sel_make_policycap(fsi);
211802a52c5cSStephen Smalley 	if (ret) {
211902a52c5cSStephen Smalley 		pr_err("SELinux: failed to load policy capabilities\n");
21200619f0f5SStephen Smalley 		goto err;
212102a52c5cSStephen Smalley 	}
212202a52c5cSStephen Smalley 
2123b77a493bSEric Paris 	return 0;
2124161ce45aSJames Morris err:
2125f8b69a5fSpeter enderborg 	pr_err("SELinux: %s:  failed while creating inodes\n",
2126744ba35eSEric Paris 		__func__);
21270619f0f5SStephen Smalley 
21280619f0f5SStephen Smalley 	selinux_fs_info_free(sb);
21290619f0f5SStephen Smalley 
2130b77a493bSEric Paris 	return ret;
21311da177e4SLinus Torvalds }
21321da177e4SLinus Torvalds 
2133920f50b2SDavid Howells static int sel_get_tree(struct fs_context *fc)
21341da177e4SLinus Torvalds {
2135920f50b2SDavid Howells 	return get_tree_single(fc, sel_fill_super);
2136920f50b2SDavid Howells }
2137920f50b2SDavid Howells 
2138920f50b2SDavid Howells static const struct fs_context_operations sel_context_ops = {
2139920f50b2SDavid Howells 	.get_tree	= sel_get_tree,
2140920f50b2SDavid Howells };
2141920f50b2SDavid Howells 
2142920f50b2SDavid Howells static int sel_init_fs_context(struct fs_context *fc)
2143920f50b2SDavid Howells {
2144920f50b2SDavid Howells 	fc->ops = &sel_context_ops;
2145920f50b2SDavid Howells 	return 0;
21461da177e4SLinus Torvalds }
21471da177e4SLinus Torvalds 
21480619f0f5SStephen Smalley static void sel_kill_sb(struct super_block *sb)
21490619f0f5SStephen Smalley {
21500619f0f5SStephen Smalley 	selinux_fs_info_free(sb);
21510619f0f5SStephen Smalley 	kill_litter_super(sb);
21520619f0f5SStephen Smalley }
21530619f0f5SStephen Smalley 
21541da177e4SLinus Torvalds static struct file_system_type sel_fs_type = {
21551da177e4SLinus Torvalds 	.name		= "selinuxfs",
2156920f50b2SDavid Howells 	.init_fs_context = sel_init_fs_context,
21570619f0f5SStephen Smalley 	.kill_sb	= sel_kill_sb,
21581da177e4SLinus Torvalds };
21591da177e4SLinus Torvalds 
2160cd2bb4cbSOndrej Mosnacek static struct vfsmount *selinuxfs_mount __ro_after_init;
2161cd2bb4cbSOndrej Mosnacek struct path selinux_null __ro_after_init;
21621da177e4SLinus Torvalds 
21631da177e4SLinus Torvalds static int __init init_sel_fs(void)
21641da177e4SLinus Torvalds {
21650619f0f5SStephen Smalley 	struct qstr null_name = QSTR_INIT(NULL_FILE_NAME,
21660619f0f5SStephen Smalley 					  sizeof(NULL_FILE_NAME)-1);
21671da177e4SLinus Torvalds 	int err;
21681da177e4SLinus Torvalds 
21696c5a682eSStephen Smalley 	if (!selinux_enabled_boot)
21701da177e4SLinus Torvalds 		return 0;
21717a627e3bSGreg Kroah-Hartman 
2172f9bb4882SEric W. Biederman 	err = sysfs_create_mount_point(fs_kobj, "selinux");
2173f9bb4882SEric W. Biederman 	if (err)
2174f9bb4882SEric W. Biederman 		return err;
21757a627e3bSGreg Kroah-Hartman 
21761da177e4SLinus Torvalds 	err = register_filesystem(&sel_fs_type);
21777a627e3bSGreg Kroah-Hartman 	if (err) {
2178f9bb4882SEric W. Biederman 		sysfs_remove_mount_point(fs_kobj, "selinux");
2179b77a493bSEric Paris 		return err;
21807a627e3bSGreg Kroah-Hartman 	}
2181b77a493bSEric Paris 
2182765927b2SAl Viro 	selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type);
21831da177e4SLinus Torvalds 	if (IS_ERR(selinuxfs_mount)) {
2184f8b69a5fSpeter enderborg 		pr_err("selinuxfs:  could not mount!\n");
21851da177e4SLinus Torvalds 		err = PTR_ERR(selinuxfs_mount);
21861da177e4SLinus Torvalds 		selinuxfs_mount = NULL;
21871da177e4SLinus Torvalds 	}
21880619f0f5SStephen Smalley 	selinux_null.dentry = d_hash_and_lookup(selinux_null.mnt->mnt_root,
21890619f0f5SStephen Smalley 						&null_name);
21900619f0f5SStephen Smalley 	if (IS_ERR(selinux_null.dentry)) {
21910619f0f5SStephen Smalley 		pr_err("selinuxfs:  could not lookup null!\n");
21920619f0f5SStephen Smalley 		err = PTR_ERR(selinux_null.dentry);
21930619f0f5SStephen Smalley 		selinux_null.dentry = NULL;
21940619f0f5SStephen Smalley 	}
2195b77a493bSEric Paris 
21961da177e4SLinus Torvalds 	return err;
21971da177e4SLinus Torvalds }
21981da177e4SLinus Torvalds 
21991da177e4SLinus Torvalds __initcall(init_sel_fs);
22001da177e4SLinus Torvalds 
22011da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
22021da177e4SLinus Torvalds void exit_sel_fs(void)
22031da177e4SLinus Torvalds {
2204f9bb4882SEric W. Biederman 	sysfs_remove_mount_point(fs_kobj, "selinux");
2205fd40ffc7SStephen Smalley 	dput(selinux_null.dentry);
2206423e0ab0STim Chen 	kern_unmount(selinuxfs_mount);
22071da177e4SLinus Torvalds 	unregister_filesystem(&sel_fs_type);
22081da177e4SLinus Torvalds }
22091da177e4SLinus Torvalds #endif
2210