xref: /openbmc/linux/security/selinux/selinuxfs.c (revision 02a52c5c)
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>
231da177e4SLinus Torvalds #include <linux/init.h>
241da177e4SLinus Torvalds #include <linux/string.h>
251da177e4SLinus Torvalds #include <linux/security.h>
261da177e4SLinus Torvalds #include <linux/major.h>
271da177e4SLinus Torvalds #include <linux/seq_file.h>
281da177e4SLinus Torvalds #include <linux/percpu.h>
29af601e46SSteve Grubb #include <linux/audit.h>
30f5269710SEric Paris #include <linux/uaccess.h>
317a627e3bSGreg Kroah-Hartman #include <linux/kobject.h>
320f7e4c33SKohei Kaigai #include <linux/ctype.h>
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds /* selinuxfs pseudo filesystem for exporting the security policy API.
351da177e4SLinus Torvalds    Based on the proc code and the fs/nfsd/nfsctl.c code. */
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds #include "flask.h"
381da177e4SLinus Torvalds #include "avc.h"
391da177e4SLinus Torvalds #include "avc_ss.h"
401da177e4SLinus Torvalds #include "security.h"
411da177e4SLinus Torvalds #include "objsec.h"
421da177e4SLinus Torvalds #include "conditional.h"
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds enum sel_inos {
451da177e4SLinus Torvalds 	SEL_ROOT_INO = 2,
461da177e4SLinus Torvalds 	SEL_LOAD,	/* load policy */
471da177e4SLinus Torvalds 	SEL_ENFORCE,	/* get or set enforcing status */
481da177e4SLinus Torvalds 	SEL_CONTEXT,	/* validate context */
491da177e4SLinus Torvalds 	SEL_ACCESS,	/* compute access decision */
501da177e4SLinus Torvalds 	SEL_CREATE,	/* compute create labeling decision */
511da177e4SLinus Torvalds 	SEL_RELABEL,	/* compute relabeling decision */
521da177e4SLinus Torvalds 	SEL_USER,	/* compute reachable user contexts */
531da177e4SLinus Torvalds 	SEL_POLICYVERS,	/* return policy version for this kernel */
541da177e4SLinus Torvalds 	SEL_COMMIT_BOOLS, /* commit new boolean values */
551da177e4SLinus Torvalds 	SEL_MLS,	/* return if MLS policy is enabled */
561da177e4SLinus Torvalds 	SEL_DISABLE,	/* disable SELinux until next reboot */
571da177e4SLinus Torvalds 	SEL_MEMBER,	/* compute polyinstantiation membership decision */
581da177e4SLinus Torvalds 	SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
594e5ab4cbSJames Morris 	SEL_COMPAT_NET,	/* whether to use old compat network packet controls */
603f12070eSEric Paris 	SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */
613f12070eSEric Paris 	SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */
6211904167SKaiGai Kohei 	SEL_STATUS,	/* export current status using mmap() */
63cee74f47SEric Paris 	SEL_POLICY,	/* allow userspace to read the in kernel policy */
64f9df6458SAndrew Perepechko 	SEL_VALIDATE_TRANS, /* compute validatetrans decision */
656174eafcSJames Carter 	SEL_INO_NEXT,	/* The next inode number to use */
661da177e4SLinus Torvalds };
671da177e4SLinus Torvalds 
680619f0f5SStephen Smalley struct selinux_fs_info {
690619f0f5SStephen Smalley 	struct dentry *bool_dir;
700619f0f5SStephen Smalley 	unsigned int bool_num;
710619f0f5SStephen Smalley 	char **bool_pending_names;
720619f0f5SStephen Smalley 	unsigned int *bool_pending_values;
730619f0f5SStephen Smalley 	struct dentry *class_dir;
740619f0f5SStephen Smalley 	unsigned long last_class_ino;
750619f0f5SStephen Smalley 	bool policy_opened;
760619f0f5SStephen Smalley 	struct dentry *policycap_dir;
770619f0f5SStephen Smalley 	struct mutex mutex;
780619f0f5SStephen Smalley 	unsigned long last_ino;
790619f0f5SStephen Smalley 	struct selinux_state *state;
800619f0f5SStephen Smalley 	struct super_block *sb;
810619f0f5SStephen Smalley };
820619f0f5SStephen Smalley 
830619f0f5SStephen Smalley static int selinux_fs_info_create(struct super_block *sb)
840619f0f5SStephen Smalley {
850619f0f5SStephen Smalley 	struct selinux_fs_info *fsi;
860619f0f5SStephen Smalley 
870619f0f5SStephen Smalley 	fsi = kzalloc(sizeof(*fsi), GFP_KERNEL);
880619f0f5SStephen Smalley 	if (!fsi)
890619f0f5SStephen Smalley 		return -ENOMEM;
900619f0f5SStephen Smalley 
910619f0f5SStephen Smalley 	mutex_init(&fsi->mutex);
920619f0f5SStephen Smalley 	fsi->last_ino = SEL_INO_NEXT - 1;
930619f0f5SStephen Smalley 	fsi->state = &selinux_state;
940619f0f5SStephen Smalley 	fsi->sb = sb;
950619f0f5SStephen Smalley 	sb->s_fs_info = fsi;
960619f0f5SStephen Smalley 	return 0;
970619f0f5SStephen Smalley }
980619f0f5SStephen Smalley 
990619f0f5SStephen Smalley static void selinux_fs_info_free(struct super_block *sb)
1000619f0f5SStephen Smalley {
1010619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = sb->s_fs_info;
1020619f0f5SStephen Smalley 	int i;
1030619f0f5SStephen Smalley 
1040619f0f5SStephen Smalley 	if (fsi) {
1050619f0f5SStephen Smalley 		for (i = 0; i < fsi->bool_num; i++)
1060619f0f5SStephen Smalley 			kfree(fsi->bool_pending_names[i]);
1070619f0f5SStephen Smalley 		kfree(fsi->bool_pending_names);
1080619f0f5SStephen Smalley 		kfree(fsi->bool_pending_values);
1090619f0f5SStephen Smalley 	}
1100619f0f5SStephen Smalley 	kfree(sb->s_fs_info);
1110619f0f5SStephen Smalley 	sb->s_fs_info = NULL;
1120619f0f5SStephen Smalley }
1136174eafcSJames Carter 
114f0ee2e46SJames Carter #define SEL_INITCON_INO_OFFSET		0x01000000
115bce34bc0SJames Carter #define SEL_BOOL_INO_OFFSET		0x02000000
116e47c8fc5SChristopher J. PeBenito #define SEL_CLASS_INO_OFFSET		0x04000000
1173bb56b25SPaul Moore #define SEL_POLICYCAP_INO_OFFSET	0x08000000
118f0ee2e46SJames Carter #define SEL_INO_MASK			0x00ffffff
119f0ee2e46SJames Carter 
1201da177e4SLinus Torvalds #define TMPBUFLEN	12
1211da177e4SLinus Torvalds static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
1221da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
1231da177e4SLinus Torvalds {
1240619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
1251da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
1261da177e4SLinus Torvalds 	ssize_t length;
1271da177e4SLinus Torvalds 
128aa8e712cSStephen Smalley 	length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
1290619f0f5SStephen Smalley 			   enforcing_enabled(fsi->state));
1301da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1311da177e4SLinus Torvalds }
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
1341da177e4SLinus Torvalds static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
1351da177e4SLinus Torvalds 				 size_t count, loff_t *ppos)
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds {
1380619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1390619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
140b77a493bSEric Paris 	char *page = NULL;
1411da177e4SLinus Torvalds 	ssize_t length;
142aa8e712cSStephen Smalley 	int old_value, new_value;
1431da177e4SLinus Torvalds 
144bfd51626SDavi Arnaut 	if (count >= PAGE_SIZE)
1458365a719SAl Viro 		return -ENOMEM;
146b77a493bSEric Paris 
1471da177e4SLinus Torvalds 	/* No partial writes. */
148b77a493bSEric Paris 	if (*ppos != 0)
1498365a719SAl Viro 		return -EINVAL;
150b77a493bSEric Paris 
1518365a719SAl Viro 	page = memdup_user_nul(buf, count);
1528365a719SAl Viro 	if (IS_ERR(page))
1538365a719SAl Viro 		return PTR_ERR(page);
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds 	length = -EINVAL;
1561da177e4SLinus Torvalds 	if (sscanf(page, "%d", &new_value) != 1)
1571da177e4SLinus Torvalds 		goto out;
1581da177e4SLinus Torvalds 
159ea49d10eSStephen Smalley 	new_value = !!new_value;
160ea49d10eSStephen Smalley 
1610619f0f5SStephen Smalley 	old_value = enforcing_enabled(state);
162aa8e712cSStephen Smalley 	if (new_value != old_value) {
1636b6bc620SStephen Smalley 		length = avc_has_perm(&selinux_state,
1646b6bc620SStephen Smalley 				      current_sid(), SECINITSID_SECURITY,
165be0554c9SStephen Smalley 				      SECCLASS_SECURITY, SECURITY__SETENFORCE,
166be0554c9SStephen Smalley 				      NULL);
1671da177e4SLinus Torvalds 		if (length)
1681da177e4SLinus Torvalds 			goto out;
169cdfb6b34SRichard Guy Briggs 		audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS,
1704195ed42SRichard Guy Briggs 			"enforcing=%d old_enforcing=%d auid=%u ses=%u"
1716c5a682eSStephen Smalley 			" enabled=1 old-enabled=1 lsm=selinux res=1",
172aa8e712cSStephen Smalley 			new_value, old_value,
173581abc09SEric W. Biederman 			from_kuid(&init_user_ns, audit_get_loginuid(current)),
1746c5a682eSStephen Smalley 			audit_get_sessionid(current));
1750619f0f5SStephen Smalley 		enforcing_set(state, new_value);
176aa8e712cSStephen Smalley 		if (new_value)
1776b6bc620SStephen Smalley 			avc_ss_reset(state->avc, 0);
178aa8e712cSStephen Smalley 		selnl_notify_setenforce(new_value);
1790619f0f5SStephen Smalley 		selinux_status_update_setenforce(state, new_value);
180aa8e712cSStephen Smalley 		if (!new_value)
18142df744cSJanne Karhunen 			call_blocking_lsm_notifier(LSM_POLICY_CHANGE, NULL);
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 {
2010619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
2020619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
2033f12070eSEric Paris 	char tmpbuf[TMPBUFLEN];
2043f12070eSEric Paris 	ssize_t length;
205496ad9aaSAl Viro 	ino_t ino = file_inode(filp)->i_ino;
2063f12070eSEric Paris 	int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
2070619f0f5SStephen Smalley 		security_get_reject_unknown(state) :
2080619f0f5SStephen Smalley 		!security_get_allow_unknown(state);
2093f12070eSEric Paris 
2103f12070eSEric Paris 	length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
2113f12070eSEric Paris 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
2123f12070eSEric Paris }
2133f12070eSEric Paris 
2143f12070eSEric Paris static const struct file_operations sel_handle_unknown_ops = {
2153f12070eSEric Paris 	.read		= sel_read_handle_unknown,
21657a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
2173f12070eSEric Paris };
2183f12070eSEric Paris 
21911904167SKaiGai Kohei static int sel_open_handle_status(struct inode *inode, struct file *filp)
22011904167SKaiGai Kohei {
2210619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
2220619f0f5SStephen Smalley 	struct page    *status = selinux_kernel_status_page(fsi->state);
22311904167SKaiGai Kohei 
22411904167SKaiGai Kohei 	if (!status)
22511904167SKaiGai Kohei 		return -ENOMEM;
22611904167SKaiGai Kohei 
22711904167SKaiGai Kohei 	filp->private_data = status;
22811904167SKaiGai Kohei 
22911904167SKaiGai Kohei 	return 0;
23011904167SKaiGai Kohei }
23111904167SKaiGai Kohei 
23211904167SKaiGai Kohei static ssize_t sel_read_handle_status(struct file *filp, char __user *buf,
23311904167SKaiGai Kohei 				      size_t count, loff_t *ppos)
23411904167SKaiGai Kohei {
23511904167SKaiGai Kohei 	struct page    *status = filp->private_data;
23611904167SKaiGai Kohei 
23711904167SKaiGai Kohei 	BUG_ON(!status);
23811904167SKaiGai Kohei 
23911904167SKaiGai Kohei 	return simple_read_from_buffer(buf, count, ppos,
24011904167SKaiGai Kohei 				       page_address(status),
24111904167SKaiGai Kohei 				       sizeof(struct selinux_kernel_status));
24211904167SKaiGai Kohei }
24311904167SKaiGai Kohei 
24411904167SKaiGai Kohei static int sel_mmap_handle_status(struct file *filp,
24511904167SKaiGai Kohei 				  struct vm_area_struct *vma)
24611904167SKaiGai Kohei {
24711904167SKaiGai Kohei 	struct page    *status = filp->private_data;
24811904167SKaiGai Kohei 	unsigned long	size = vma->vm_end - vma->vm_start;
24911904167SKaiGai Kohei 
25011904167SKaiGai Kohei 	BUG_ON(!status);
25111904167SKaiGai Kohei 
25211904167SKaiGai Kohei 	/* only allows one page from the head */
25311904167SKaiGai Kohei 	if (vma->vm_pgoff > 0 || size != PAGE_SIZE)
25411904167SKaiGai Kohei 		return -EIO;
25511904167SKaiGai Kohei 	/* disallow writable mapping */
25611904167SKaiGai Kohei 	if (vma->vm_flags & VM_WRITE)
25711904167SKaiGai Kohei 		return -EPERM;
25811904167SKaiGai Kohei 	/* disallow mprotect() turns it into writable */
25911904167SKaiGai Kohei 	vma->vm_flags &= ~VM_MAYWRITE;
26011904167SKaiGai Kohei 
26111904167SKaiGai Kohei 	return remap_pfn_range(vma, vma->vm_start,
26211904167SKaiGai Kohei 			       page_to_pfn(status),
26311904167SKaiGai Kohei 			       size, vma->vm_page_prot);
26411904167SKaiGai Kohei }
26511904167SKaiGai Kohei 
26611904167SKaiGai Kohei static const struct file_operations sel_handle_status_ops = {
26711904167SKaiGai Kohei 	.open		= sel_open_handle_status,
26811904167SKaiGai Kohei 	.read		= sel_read_handle_status,
26911904167SKaiGai Kohei 	.mmap		= sel_mmap_handle_status,
27011904167SKaiGai Kohei 	.llseek		= generic_file_llseek,
27111904167SKaiGai Kohei };
27211904167SKaiGai Kohei 
2731da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
2741da177e4SLinus Torvalds static ssize_t sel_write_disable(struct file *file, const char __user *buf,
2751da177e4SLinus Torvalds 				 size_t count, loff_t *ppos)
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds {
2780619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
2798365a719SAl Viro 	char *page;
2801da177e4SLinus Torvalds 	ssize_t length;
2811da177e4SLinus Torvalds 	int new_value;
2824195ed42SRichard Guy Briggs 	int enforcing;
2831da177e4SLinus Torvalds 
28489b223bfSPaul Moore 	/* NOTE: we are now officially considering runtime disable as
28589b223bfSPaul Moore 	 *       deprecated, and using it will become increasingly painful
28689b223bfSPaul Moore 	 *       (e.g. sleeping/blocking) as we progress through future
28789b223bfSPaul Moore 	 *       kernel releases until eventually it is removed
28889b223bfSPaul Moore 	 */
28989b223bfSPaul Moore 	pr_err("SELinux:  Runtime disable is deprecated, use selinux=0 on the kernel cmdline.\n");
29089b223bfSPaul Moore 
291bfd51626SDavi Arnaut 	if (count >= PAGE_SIZE)
2928365a719SAl Viro 		return -ENOMEM;
293b77a493bSEric Paris 
2941da177e4SLinus Torvalds 	/* No partial writes. */
295b77a493bSEric Paris 	if (*ppos != 0)
2968365a719SAl Viro 		return -EINVAL;
297b77a493bSEric Paris 
2988365a719SAl Viro 	page = memdup_user_nul(buf, count);
2998365a719SAl Viro 	if (IS_ERR(page))
3008365a719SAl Viro 		return PTR_ERR(page);
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 	length = -EINVAL;
3031da177e4SLinus Torvalds 	if (sscanf(page, "%d", &new_value) != 1)
3041da177e4SLinus Torvalds 		goto out;
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 	if (new_value) {
3074195ed42SRichard Guy Briggs 		enforcing = enforcing_enabled(fsi->state);
3080619f0f5SStephen Smalley 		length = selinux_disable(fsi->state);
309b77a493bSEric Paris 		if (length)
3101da177e4SLinus Torvalds 			goto out;
311cdfb6b34SRichard Guy Briggs 		audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS,
3124195ed42SRichard Guy Briggs 			"enforcing=%d old_enforcing=%d auid=%u ses=%u"
3136c5a682eSStephen Smalley 			" enabled=0 old-enabled=1 lsm=selinux res=1",
3144195ed42SRichard Guy Briggs 			enforcing, enforcing,
315581abc09SEric W. Biederman 			from_kuid(&init_user_ns, audit_get_loginuid(current)),
3166c5a682eSStephen Smalley 			audit_get_sessionid(current));
3171da177e4SLinus Torvalds 	}
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds 	length = count;
3201da177e4SLinus Torvalds out:
3218365a719SAl Viro 	kfree(page);
3221da177e4SLinus Torvalds 	return length;
3231da177e4SLinus Torvalds }
3241da177e4SLinus Torvalds #else
3251da177e4SLinus Torvalds #define sel_write_disable NULL
3261da177e4SLinus Torvalds #endif
3271da177e4SLinus Torvalds 
3289c2e08c5SArjan van de Ven static const struct file_operations sel_disable_ops = {
3291da177e4SLinus Torvalds 	.write		= sel_write_disable,
33057a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
3311da177e4SLinus Torvalds };
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
3341da177e4SLinus Torvalds 				   size_t count, loff_t *ppos)
3351da177e4SLinus Torvalds {
3361da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
3371da177e4SLinus Torvalds 	ssize_t length;
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
3401da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
3411da177e4SLinus Torvalds }
3421da177e4SLinus Torvalds 
3439c2e08c5SArjan van de Ven static const struct file_operations sel_policyvers_ops = {
3441da177e4SLinus Torvalds 	.read		= sel_read_policyvers,
34557a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
3461da177e4SLinus Torvalds };
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds /* declaration for sel_write_load */
34902a52c5cSStephen Smalley static int sel_make_bools(struct selinux_fs_info *fsi,
35002a52c5cSStephen Smalley 			struct selinux_policy *newpolicy);
35102a52c5cSStephen Smalley static int sel_make_classes(struct selinux_fs_info *fsi,
35202a52c5cSStephen Smalley 			struct selinux_policy *newpolicy);
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 
3581da177e4SLinus Torvalds static ssize_t sel_read_mls(struct file *filp, char __user *buf,
3591da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
3601da177e4SLinus Torvalds {
3610619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
3621da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
3631da177e4SLinus Torvalds 	ssize_t length;
3641da177e4SLinus Torvalds 
3650719aaf5SGuido Trentalancia 	length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
3660619f0f5SStephen Smalley 			   security_mls_enabled(fsi->state));
3671da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
3681da177e4SLinus Torvalds }
3691da177e4SLinus Torvalds 
3709c2e08c5SArjan van de Ven static const struct file_operations sel_mls_ops = {
3711da177e4SLinus Torvalds 	.read		= sel_read_mls,
37257a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
3731da177e4SLinus Torvalds };
3741da177e4SLinus Torvalds 
375cee74f47SEric Paris struct policy_load_memory {
376cee74f47SEric Paris 	size_t len;
377cee74f47SEric Paris 	void *data;
378cee74f47SEric Paris };
379cee74f47SEric Paris 
380cee74f47SEric Paris static int sel_open_policy(struct inode *inode, struct file *filp)
381cee74f47SEric Paris {
3820619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
3830619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
384cee74f47SEric Paris 	struct policy_load_memory *plm = NULL;
385cee74f47SEric Paris 	int rc;
386cee74f47SEric Paris 
387cee74f47SEric Paris 	BUG_ON(filp->private_data);
388cee74f47SEric Paris 
3890619f0f5SStephen Smalley 	mutex_lock(&fsi->mutex);
390cee74f47SEric Paris 
3916b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
3926b6bc620SStephen Smalley 			  current_sid(), SECINITSID_SECURITY,
393be0554c9SStephen Smalley 			  SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
394cee74f47SEric Paris 	if (rc)
395cee74f47SEric Paris 		goto err;
396cee74f47SEric Paris 
397cee74f47SEric Paris 	rc = -EBUSY;
3980619f0f5SStephen Smalley 	if (fsi->policy_opened)
399cee74f47SEric Paris 		goto err;
400cee74f47SEric Paris 
401cee74f47SEric Paris 	rc = -ENOMEM;
402cee74f47SEric Paris 	plm = kzalloc(sizeof(*plm), GFP_KERNEL);
403cee74f47SEric Paris 	if (!plm)
404cee74f47SEric Paris 		goto err;
405cee74f47SEric Paris 
4060619f0f5SStephen Smalley 	if (i_size_read(inode) != security_policydb_len(state)) {
4075955102cSAl Viro 		inode_lock(inode);
4080619f0f5SStephen Smalley 		i_size_write(inode, security_policydb_len(state));
4095955102cSAl Viro 		inode_unlock(inode);
410cee74f47SEric Paris 	}
411cee74f47SEric Paris 
4120619f0f5SStephen Smalley 	rc = security_read_policy(state, &plm->data, &plm->len);
413cee74f47SEric Paris 	if (rc)
414cee74f47SEric Paris 		goto err;
415cee74f47SEric Paris 
4160619f0f5SStephen Smalley 	fsi->policy_opened = 1;
417cee74f47SEric Paris 
418cee74f47SEric Paris 	filp->private_data = plm;
419cee74f47SEric Paris 
4200619f0f5SStephen Smalley 	mutex_unlock(&fsi->mutex);
421cee74f47SEric Paris 
422cee74f47SEric Paris 	return 0;
423cee74f47SEric Paris err:
4240619f0f5SStephen Smalley 	mutex_unlock(&fsi->mutex);
425cee74f47SEric Paris 
426cee74f47SEric Paris 	if (plm)
427cee74f47SEric Paris 		vfree(plm->data);
428cee74f47SEric Paris 	kfree(plm);
429cee74f47SEric Paris 	return rc;
430cee74f47SEric Paris }
431cee74f47SEric Paris 
432cee74f47SEric Paris static int sel_release_policy(struct inode *inode, struct file *filp)
433cee74f47SEric Paris {
4340619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
435cee74f47SEric Paris 	struct policy_load_memory *plm = filp->private_data;
436cee74f47SEric Paris 
437cee74f47SEric Paris 	BUG_ON(!plm);
438cee74f47SEric Paris 
4390619f0f5SStephen Smalley 	fsi->policy_opened = 0;
440cee74f47SEric Paris 
441cee74f47SEric Paris 	vfree(plm->data);
442cee74f47SEric Paris 	kfree(plm);
443cee74f47SEric Paris 
444cee74f47SEric Paris 	return 0;
445cee74f47SEric Paris }
446cee74f47SEric Paris 
447cee74f47SEric Paris static ssize_t sel_read_policy(struct file *filp, char __user *buf,
448cee74f47SEric Paris 			       size_t count, loff_t *ppos)
449cee74f47SEric Paris {
450cee74f47SEric Paris 	struct policy_load_memory *plm = filp->private_data;
451cee74f47SEric Paris 	int ret;
452cee74f47SEric Paris 
4536b6bc620SStephen Smalley 	ret = avc_has_perm(&selinux_state,
4546b6bc620SStephen Smalley 			   current_sid(), SECINITSID_SECURITY,
455be0554c9SStephen Smalley 			  SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
456cee74f47SEric Paris 	if (ret)
457cee74f47SEric Paris 		return ret;
4580da74120SJann Horn 
4590da74120SJann Horn 	return simple_read_from_buffer(buf, count, ppos, plm->data, plm->len);
460cee74f47SEric Paris }
461cee74f47SEric Paris 
462ac9a1f6dSSouptick Joarder static vm_fault_t sel_mmap_policy_fault(struct vm_fault *vmf)
463845ca30fSEric Paris {
46411bac800SDave Jiang 	struct policy_load_memory *plm = vmf->vma->vm_file->private_data;
465845ca30fSEric Paris 	unsigned long offset;
466845ca30fSEric Paris 	struct page *page;
467845ca30fSEric Paris 
468845ca30fSEric Paris 	if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE))
469845ca30fSEric Paris 		return VM_FAULT_SIGBUS;
470845ca30fSEric Paris 
471845ca30fSEric Paris 	offset = vmf->pgoff << PAGE_SHIFT;
472845ca30fSEric Paris 	if (offset >= roundup(plm->len, PAGE_SIZE))
473845ca30fSEric Paris 		return VM_FAULT_SIGBUS;
474845ca30fSEric Paris 
475845ca30fSEric Paris 	page = vmalloc_to_page(plm->data + offset);
476845ca30fSEric Paris 	get_page(page);
477845ca30fSEric Paris 
478845ca30fSEric Paris 	vmf->page = page;
479845ca30fSEric Paris 
480845ca30fSEric Paris 	return 0;
481845ca30fSEric Paris }
482845ca30fSEric Paris 
4837cbea8dcSKirill A. Shutemov static const struct vm_operations_struct sel_mmap_policy_ops = {
484845ca30fSEric Paris 	.fault = sel_mmap_policy_fault,
485845ca30fSEric Paris 	.page_mkwrite = sel_mmap_policy_fault,
486845ca30fSEric Paris };
487845ca30fSEric Paris 
488ad3fa08cSJames Morris static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma)
489845ca30fSEric Paris {
490845ca30fSEric Paris 	if (vma->vm_flags & VM_SHARED) {
491845ca30fSEric Paris 		/* do not allow mprotect to make mapping writable */
492845ca30fSEric Paris 		vma->vm_flags &= ~VM_MAYWRITE;
493845ca30fSEric Paris 
494845ca30fSEric Paris 		if (vma->vm_flags & VM_WRITE)
495845ca30fSEric Paris 			return -EACCES;
496845ca30fSEric Paris 	}
497845ca30fSEric Paris 
498314e51b9SKonstantin Khlebnikov 	vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
499845ca30fSEric Paris 	vma->vm_ops = &sel_mmap_policy_ops;
500845ca30fSEric Paris 
501845ca30fSEric Paris 	return 0;
502845ca30fSEric Paris }
503845ca30fSEric Paris 
504cee74f47SEric Paris static const struct file_operations sel_policy_ops = {
505cee74f47SEric Paris 	.open		= sel_open_policy,
506cee74f47SEric Paris 	.read		= sel_read_policy,
507845ca30fSEric Paris 	.mmap		= sel_mmap_policy,
508cee74f47SEric Paris 	.release	= sel_release_policy,
50947a93a5bSEric Paris 	.llseek		= generic_file_llseek,
510cee74f47SEric Paris };
511cee74f47SEric Paris 
51202a52c5cSStephen Smalley static int sel_make_policy_nodes(struct selinux_fs_info *fsi,
51302a52c5cSStephen Smalley 				struct selinux_policy *newpolicy)
5140619f0f5SStephen Smalley {
5150619f0f5SStephen Smalley 	int ret;
5160619f0f5SStephen Smalley 
51702a52c5cSStephen Smalley 	ret = sel_make_bools(fsi, newpolicy);
5180619f0f5SStephen Smalley 	if (ret) {
5190619f0f5SStephen Smalley 		pr_err("SELinux: failed to load policy booleans\n");
5200619f0f5SStephen Smalley 		return ret;
5210619f0f5SStephen Smalley 	}
5220619f0f5SStephen Smalley 
52302a52c5cSStephen Smalley 	ret = sel_make_classes(fsi, newpolicy);
5240619f0f5SStephen Smalley 	if (ret) {
5250619f0f5SStephen Smalley 		pr_err("SELinux: failed to load policy classes\n");
5260619f0f5SStephen Smalley 		return ret;
5270619f0f5SStephen Smalley 	}
5280619f0f5SStephen Smalley 
5290619f0f5SStephen Smalley 	return 0;
5300619f0f5SStephen Smalley }
5310619f0f5SStephen Smalley 
5321da177e4SLinus Torvalds static ssize_t sel_write_load(struct file *file, const char __user *buf,
5331da177e4SLinus Torvalds 			      size_t count, loff_t *ppos)
5341da177e4SLinus Torvalds 
5351da177e4SLinus Torvalds {
5360619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
53702a52c5cSStephen Smalley 	struct selinux_policy *newpolicy;
5381da177e4SLinus Torvalds 	ssize_t length;
5391da177e4SLinus Torvalds 	void *data = NULL;
5401da177e4SLinus Torvalds 
5410619f0f5SStephen Smalley 	mutex_lock(&fsi->mutex);
5421da177e4SLinus Torvalds 
5436b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
5446b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
545be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL);
5461da177e4SLinus Torvalds 	if (length)
5471da177e4SLinus Torvalds 		goto out;
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds 	/* No partial writes. */
5501da177e4SLinus Torvalds 	length = -EINVAL;
551b77a493bSEric Paris 	if (*ppos != 0)
5521da177e4SLinus Torvalds 		goto out;
5531da177e4SLinus Torvalds 
554b77a493bSEric Paris 	length = -ENOMEM;
555b77a493bSEric Paris 	data = vmalloc(count);
556b77a493bSEric Paris 	if (!data)
557b77a493bSEric Paris 		goto out;
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds 	length = -EFAULT;
5601da177e4SLinus Torvalds 	if (copy_from_user(data, buf, count) != 0)
5611da177e4SLinus Torvalds 		goto out;
5621da177e4SLinus Torvalds 
56302a52c5cSStephen Smalley 	length = security_load_policy(fsi->state, data, count, &newpolicy);
5644262fb51SGary Tierney 	if (length) {
5654262fb51SGary Tierney 		pr_warn_ratelimited("SELinux: failed to load policy\n");
5661da177e4SLinus Torvalds 		goto out;
5674262fb51SGary Tierney 	}
5681da177e4SLinus Torvalds 
56902a52c5cSStephen Smalley 	length = sel_make_policy_nodes(fsi, newpolicy);
57002a52c5cSStephen Smalley 	if (length) {
57102a52c5cSStephen Smalley 		selinux_policy_cancel(fsi->state, newpolicy);
572e47c8fc5SChristopher J. PeBenito 		goto out1;
57302a52c5cSStephen Smalley 	}
57402a52c5cSStephen Smalley 
57502a52c5cSStephen Smalley 	selinux_policy_commit(fsi->state, newpolicy);
576b77a493bSEric Paris 
5771da177e4SLinus Torvalds 	length = count;
578e47c8fc5SChristopher J. PeBenito 
579e47c8fc5SChristopher J. PeBenito out1:
580cdfb6b34SRichard Guy Briggs 	audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
581d141136fSRichard Guy Briggs 		"auid=%u ses=%u lsm=selinux res=1",
582581abc09SEric W. Biederman 		from_kuid(&init_user_ns, audit_get_loginuid(current)),
5834746ec5bSEric Paris 		audit_get_sessionid(current));
5841da177e4SLinus Torvalds out:
5850619f0f5SStephen Smalley 	mutex_unlock(&fsi->mutex);
5861da177e4SLinus Torvalds 	vfree(data);
5871da177e4SLinus Torvalds 	return length;
5881da177e4SLinus Torvalds }
5891da177e4SLinus Torvalds 
5909c2e08c5SArjan van de Ven static const struct file_operations sel_load_ops = {
5911da177e4SLinus Torvalds 	.write		= sel_write_load,
59257a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
5931da177e4SLinus Torvalds };
5941da177e4SLinus Torvalds 
595ce9982d0SStephen Smalley static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
5961da177e4SLinus Torvalds {
5970619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
5980619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
599b77a493bSEric Paris 	char *canon = NULL;
600ce9982d0SStephen Smalley 	u32 sid, len;
6011da177e4SLinus Torvalds 	ssize_t length;
6021da177e4SLinus Torvalds 
6036b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
6046b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
605be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__CHECK_CONTEXT, NULL);
6061da177e4SLinus Torvalds 	if (length)
607b77a493bSEric Paris 		goto out;
6081da177e4SLinus Torvalds 
6090619f0f5SStephen Smalley 	length = security_context_to_sid(state, buf, size, &sid, GFP_KERNEL);
610b77a493bSEric Paris 	if (length)
611b77a493bSEric Paris 		goto out;
6121da177e4SLinus Torvalds 
6130619f0f5SStephen Smalley 	length = security_sid_to_context(state, sid, &canon, &len);
614b77a493bSEric Paris 	if (length)
615b77a493bSEric Paris 		goto out;
616ce9982d0SStephen Smalley 
617b77a493bSEric Paris 	length = -ERANGE;
618ce9982d0SStephen Smalley 	if (len > SIMPLE_TRANSACTION_LIMIT) {
619f8b69a5fSpeter enderborg 		pr_err("SELinux: %s:  context size (%u) exceeds "
620744ba35eSEric Paris 			"payload max\n", __func__, len);
621ce9982d0SStephen Smalley 		goto out;
622ce9982d0SStephen Smalley 	}
623ce9982d0SStephen Smalley 
624ce9982d0SStephen Smalley 	memcpy(buf, canon, len);
625ce9982d0SStephen Smalley 	length = len;
6261da177e4SLinus Torvalds out:
627ce9982d0SStephen Smalley 	kfree(canon);
6281da177e4SLinus Torvalds 	return length;
6291da177e4SLinus Torvalds }
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
6321da177e4SLinus Torvalds 				     size_t count, loff_t *ppos)
6331da177e4SLinus Torvalds {
6340619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
6351da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
6361da177e4SLinus Torvalds 	ssize_t length;
6371da177e4SLinus Torvalds 
6380619f0f5SStephen Smalley 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u", fsi->state->checkreqprot);
6391da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
6401da177e4SLinus Torvalds }
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
6431da177e4SLinus Torvalds 				      size_t count, loff_t *ppos)
6441da177e4SLinus Torvalds {
6450619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
6468365a719SAl Viro 	char *page;
6471da177e4SLinus Torvalds 	ssize_t length;
6481da177e4SLinus Torvalds 	unsigned int new_value;
6491da177e4SLinus Torvalds 
6506b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
6516b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
652be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__SETCHECKREQPROT,
653be0554c9SStephen Smalley 			      NULL);
6541da177e4SLinus Torvalds 	if (length)
6558365a719SAl Viro 		return length;
6561da177e4SLinus Torvalds 
657bfd51626SDavi Arnaut 	if (count >= PAGE_SIZE)
6588365a719SAl Viro 		return -ENOMEM;
659b77a493bSEric Paris 
6601da177e4SLinus Torvalds 	/* No partial writes. */
661b77a493bSEric Paris 	if (*ppos != 0)
6628365a719SAl Viro 		return -EINVAL;
663b77a493bSEric Paris 
6648365a719SAl Viro 	page = memdup_user_nul(buf, count);
6658365a719SAl Viro 	if (IS_ERR(page))
6668365a719SAl Viro 		return PTR_ERR(page);
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds 	length = -EINVAL;
6691da177e4SLinus Torvalds 	if (sscanf(page, "%u", &new_value) != 1)
6701da177e4SLinus Torvalds 		goto out;
6711da177e4SLinus Torvalds 
672e9c38f9fSStephen Smalley 	if (new_value) {
673e9c38f9fSStephen Smalley 		char comm[sizeof(current->comm)];
674e9c38f9fSStephen Smalley 
675e9c38f9fSStephen Smalley 		memcpy(comm, current->comm, sizeof(comm));
676e9c38f9fSStephen Smalley 		pr_warn_once("SELinux: %s (%d) set checkreqprot to 1. This is deprecated and will be rejected in a future kernel release.\n",
677e9c38f9fSStephen Smalley 			     comm, current->pid);
678e9c38f9fSStephen Smalley 	}
679e9c38f9fSStephen Smalley 
6800619f0f5SStephen Smalley 	fsi->state->checkreqprot = new_value ? 1 : 0;
6811da177e4SLinus Torvalds 	length = count;
6821da177e4SLinus Torvalds out:
6838365a719SAl Viro 	kfree(page);
6841da177e4SLinus Torvalds 	return length;
6851da177e4SLinus Torvalds }
6869c2e08c5SArjan van de Ven static const struct file_operations sel_checkreqprot_ops = {
6871da177e4SLinus Torvalds 	.read		= sel_read_checkreqprot,
6881da177e4SLinus Torvalds 	.write		= sel_write_checkreqprot,
68957a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
6901da177e4SLinus Torvalds };
6911da177e4SLinus Torvalds 
692f9df6458SAndrew Perepechko static ssize_t sel_write_validatetrans(struct file *file,
693f9df6458SAndrew Perepechko 					const char __user *buf,
694f9df6458SAndrew Perepechko 					size_t count, loff_t *ppos)
695f9df6458SAndrew Perepechko {
6960619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
6970619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
698f9df6458SAndrew Perepechko 	char *oldcon = NULL, *newcon = NULL, *taskcon = NULL;
699f9df6458SAndrew Perepechko 	char *req = NULL;
700f9df6458SAndrew Perepechko 	u32 osid, nsid, tsid;
701f9df6458SAndrew Perepechko 	u16 tclass;
702f9df6458SAndrew Perepechko 	int rc;
703f9df6458SAndrew Perepechko 
7046b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
7056b6bc620SStephen Smalley 			  current_sid(), SECINITSID_SECURITY,
706be0554c9SStephen Smalley 			  SECCLASS_SECURITY, SECURITY__VALIDATE_TRANS, NULL);
707f9df6458SAndrew Perepechko 	if (rc)
708f9df6458SAndrew Perepechko 		goto out;
709f9df6458SAndrew Perepechko 
710f9df6458SAndrew Perepechko 	rc = -ENOMEM;
711f9df6458SAndrew Perepechko 	if (count >= PAGE_SIZE)
712f9df6458SAndrew Perepechko 		goto out;
713f9df6458SAndrew Perepechko 
714f9df6458SAndrew Perepechko 	/* No partial writes. */
715f9df6458SAndrew Perepechko 	rc = -EINVAL;
716f9df6458SAndrew Perepechko 	if (*ppos != 0)
717f9df6458SAndrew Perepechko 		goto out;
718f9df6458SAndrew Perepechko 
7190b884d25SAl Viro 	req = memdup_user_nul(buf, count);
7200b884d25SAl Viro 	if (IS_ERR(req)) {
7210b884d25SAl Viro 		rc = PTR_ERR(req);
7220b884d25SAl Viro 		req = NULL;
723f9df6458SAndrew Perepechko 		goto out;
7240b884d25SAl Viro 	}
725f9df6458SAndrew Perepechko 
726f9df6458SAndrew Perepechko 	rc = -ENOMEM;
727f9df6458SAndrew Perepechko 	oldcon = kzalloc(count + 1, GFP_KERNEL);
728f9df6458SAndrew Perepechko 	if (!oldcon)
729f9df6458SAndrew Perepechko 		goto out;
730f9df6458SAndrew Perepechko 
731f9df6458SAndrew Perepechko 	newcon = kzalloc(count + 1, GFP_KERNEL);
732f9df6458SAndrew Perepechko 	if (!newcon)
733f9df6458SAndrew Perepechko 		goto out;
734f9df6458SAndrew Perepechko 
735f9df6458SAndrew Perepechko 	taskcon = kzalloc(count + 1, GFP_KERNEL);
736f9df6458SAndrew Perepechko 	if (!taskcon)
737f9df6458SAndrew Perepechko 		goto out;
738f9df6458SAndrew Perepechko 
739f9df6458SAndrew Perepechko 	rc = -EINVAL;
740f9df6458SAndrew Perepechko 	if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4)
741f9df6458SAndrew Perepechko 		goto out;
742f9df6458SAndrew Perepechko 
7430619f0f5SStephen Smalley 	rc = security_context_str_to_sid(state, oldcon, &osid, GFP_KERNEL);
744f9df6458SAndrew Perepechko 	if (rc)
745f9df6458SAndrew Perepechko 		goto out;
746f9df6458SAndrew Perepechko 
7470619f0f5SStephen Smalley 	rc = security_context_str_to_sid(state, newcon, &nsid, GFP_KERNEL);
748f9df6458SAndrew Perepechko 	if (rc)
749f9df6458SAndrew Perepechko 		goto out;
750f9df6458SAndrew Perepechko 
7510619f0f5SStephen Smalley 	rc = security_context_str_to_sid(state, taskcon, &tsid, GFP_KERNEL);
752f9df6458SAndrew Perepechko 	if (rc)
753f9df6458SAndrew Perepechko 		goto out;
754f9df6458SAndrew Perepechko 
7550619f0f5SStephen Smalley 	rc = security_validate_transition_user(state, osid, nsid, tsid, tclass);
756f9df6458SAndrew Perepechko 	if (!rc)
757f9df6458SAndrew Perepechko 		rc = count;
758f9df6458SAndrew Perepechko out:
759f9df6458SAndrew Perepechko 	kfree(req);
760f9df6458SAndrew Perepechko 	kfree(oldcon);
761f9df6458SAndrew Perepechko 	kfree(newcon);
762f9df6458SAndrew Perepechko 	kfree(taskcon);
763f9df6458SAndrew Perepechko 	return rc;
764f9df6458SAndrew Perepechko }
765f9df6458SAndrew Perepechko 
766f9df6458SAndrew Perepechko static const struct file_operations sel_transition_ops = {
767f9df6458SAndrew Perepechko 	.write		= sel_write_validatetrans,
768f9df6458SAndrew Perepechko 	.llseek		= generic_file_llseek,
769f9df6458SAndrew Perepechko };
770f9df6458SAndrew Perepechko 
7711da177e4SLinus Torvalds /*
7721da177e4SLinus Torvalds  * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
7731da177e4SLinus Torvalds  */
7741da177e4SLinus Torvalds static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
7751da177e4SLinus Torvalds static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
7761da177e4SLinus Torvalds static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
7771da177e4SLinus Torvalds static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
7781da177e4SLinus Torvalds static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
7791da177e4SLinus Torvalds 
780631d2b49SEric Biggers static ssize_t (*const write_op[])(struct file *, char *, size_t) = {
7811da177e4SLinus Torvalds 	[SEL_ACCESS] = sel_write_access,
7821da177e4SLinus Torvalds 	[SEL_CREATE] = sel_write_create,
7831da177e4SLinus Torvalds 	[SEL_RELABEL] = sel_write_relabel,
7841da177e4SLinus Torvalds 	[SEL_USER] = sel_write_user,
7851da177e4SLinus Torvalds 	[SEL_MEMBER] = sel_write_member,
786ce9982d0SStephen Smalley 	[SEL_CONTEXT] = sel_write_context,
7871da177e4SLinus Torvalds };
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
7901da177e4SLinus Torvalds {
791496ad9aaSAl Viro 	ino_t ino = file_inode(file)->i_ino;
7921da177e4SLinus Torvalds 	char *data;
7931da177e4SLinus Torvalds 	ssize_t rv;
7941da177e4SLinus Torvalds 
7956e20a64aSNicolas Kaiser 	if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
7961da177e4SLinus Torvalds 		return -EINVAL;
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds 	data = simple_transaction_get(file, buf, size);
7991da177e4SLinus Torvalds 	if (IS_ERR(data))
8001da177e4SLinus Torvalds 		return PTR_ERR(data);
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 	rv = write_op[ino](file, data, size);
8031da177e4SLinus Torvalds 	if (rv > 0) {
8041da177e4SLinus Torvalds 		simple_transaction_set(file, rv);
8051da177e4SLinus Torvalds 		rv = size;
8061da177e4SLinus Torvalds 	}
8071da177e4SLinus Torvalds 	return rv;
8081da177e4SLinus Torvalds }
8091da177e4SLinus Torvalds 
8109c2e08c5SArjan van de Ven static const struct file_operations transaction_ops = {
8111da177e4SLinus Torvalds 	.write		= selinux_transaction_write,
8121da177e4SLinus Torvalds 	.read		= simple_transaction_read,
8131da177e4SLinus Torvalds 	.release	= simple_transaction_release,
81457a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
8151da177e4SLinus Torvalds };
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds /*
8181da177e4SLinus Torvalds  * payload - write methods
8191da177e4SLinus Torvalds  * If the method has a response, the response should be put in buf,
8201da177e4SLinus Torvalds  * and the length returned.  Otherwise return 0 or and -error.
8211da177e4SLinus Torvalds  */
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
8241da177e4SLinus Torvalds {
8250619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
8260619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
827b77a493bSEric Paris 	char *scon = NULL, *tcon = NULL;
8281da177e4SLinus Torvalds 	u32 ssid, tsid;
8291da177e4SLinus Torvalds 	u16 tclass;
8301da177e4SLinus Torvalds 	struct av_decision avd;
8311da177e4SLinus Torvalds 	ssize_t length;
8321da177e4SLinus Torvalds 
8336b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
8346b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
835be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__COMPUTE_AV, NULL);
8361da177e4SLinus Torvalds 	if (length)
837b77a493bSEric Paris 		goto out;
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds 	length = -ENOMEM;
84089d155efSJames Morris 	scon = kzalloc(size + 1, GFP_KERNEL);
8411da177e4SLinus Torvalds 	if (!scon)
842b77a493bSEric Paris 		goto out;
8431da177e4SLinus Torvalds 
844b77a493bSEric Paris 	length = -ENOMEM;
84589d155efSJames Morris 	tcon = kzalloc(size + 1, GFP_KERNEL);
8461da177e4SLinus Torvalds 	if (!tcon)
8471da177e4SLinus Torvalds 		goto out;
8481da177e4SLinus Torvalds 
8491da177e4SLinus Torvalds 	length = -EINVAL;
85019439d05SStephen Smalley 	if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
851b77a493bSEric Paris 		goto out;
8521da177e4SLinus Torvalds 
8530619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
854b77a493bSEric Paris 	if (length)
855b77a493bSEric Paris 		goto out;
856b77a493bSEric Paris 
8570619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
858b77a493bSEric Paris 	if (length)
859b77a493bSEric Paris 		goto out;
8601da177e4SLinus Torvalds 
8610619f0f5SStephen Smalley 	security_compute_av_user(state, ssid, tsid, tclass, &avd);
8621da177e4SLinus Torvalds 
8631da177e4SLinus Torvalds 	length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
8648a6f83afSKaiGai Kohei 			  "%x %x %x %x %u %x",
865f1c6381aSEric Paris 			  avd.allowed, 0xffffffff,
8661da177e4SLinus Torvalds 			  avd.auditallow, avd.auditdeny,
8678a6f83afSKaiGai Kohei 			  avd.seqno, avd.flags);
8681da177e4SLinus Torvalds out:
869b77a493bSEric Paris 	kfree(tcon);
8701da177e4SLinus Torvalds 	kfree(scon);
8711da177e4SLinus Torvalds 	return length;
8721da177e4SLinus Torvalds }
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
8751da177e4SLinus Torvalds {
8760619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
8770619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
878b77a493bSEric Paris 	char *scon = NULL, *tcon = NULL;
879f50a3ec9SKohei Kaigai 	char *namebuf = NULL, *objname = NULL;
8801da177e4SLinus Torvalds 	u32 ssid, tsid, newsid;
8811da177e4SLinus Torvalds 	u16 tclass;
8821da177e4SLinus Torvalds 	ssize_t length;
883b77a493bSEric Paris 	char *newcon = NULL;
8841da177e4SLinus Torvalds 	u32 len;
885f50a3ec9SKohei Kaigai 	int nargs;
8861da177e4SLinus Torvalds 
8876b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
8886b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
889be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__COMPUTE_CREATE,
890be0554c9SStephen Smalley 			      NULL);
8911da177e4SLinus Torvalds 	if (length)
892b77a493bSEric Paris 		goto out;
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 	length = -ENOMEM;
89589d155efSJames Morris 	scon = kzalloc(size + 1, GFP_KERNEL);
8961da177e4SLinus Torvalds 	if (!scon)
897b77a493bSEric Paris 		goto out;
8981da177e4SLinus Torvalds 
899b77a493bSEric Paris 	length = -ENOMEM;
90089d155efSJames Morris 	tcon = kzalloc(size + 1, GFP_KERNEL);
9011da177e4SLinus Torvalds 	if (!tcon)
9021da177e4SLinus Torvalds 		goto out;
9031da177e4SLinus Torvalds 
904f50a3ec9SKohei Kaigai 	length = -ENOMEM;
905f50a3ec9SKohei Kaigai 	namebuf = kzalloc(size + 1, GFP_KERNEL);
906f50a3ec9SKohei Kaigai 	if (!namebuf)
907b77a493bSEric Paris 		goto out;
9081da177e4SLinus Torvalds 
909f50a3ec9SKohei Kaigai 	length = -EINVAL;
910f50a3ec9SKohei Kaigai 	nargs = sscanf(buf, "%s %s %hu %s", scon, tcon, &tclass, namebuf);
911f50a3ec9SKohei Kaigai 	if (nargs < 3 || nargs > 4)
912f50a3ec9SKohei Kaigai 		goto out;
9130f7e4c33SKohei Kaigai 	if (nargs == 4) {
9140f7e4c33SKohei Kaigai 		/*
9150f7e4c33SKohei Kaigai 		 * If and when the name of new object to be queried contains
9160f7e4c33SKohei Kaigai 		 * either whitespace or multibyte characters, they shall be
9170f7e4c33SKohei Kaigai 		 * encoded based on the percentage-encoding rule.
9180f7e4c33SKohei Kaigai 		 * If not encoded, the sscanf logic picks up only left-half
9190f7e4c33SKohei Kaigai 		 * of the supplied name; splitted by a whitespace unexpectedly.
9200f7e4c33SKohei Kaigai 		 */
9210f7e4c33SKohei Kaigai 		char   *r, *w;
9220f7e4c33SKohei Kaigai 		int     c1, c2;
9230f7e4c33SKohei Kaigai 
9240f7e4c33SKohei Kaigai 		r = w = namebuf;
9250f7e4c33SKohei Kaigai 		do {
9260f7e4c33SKohei Kaigai 			c1 = *r++;
9270f7e4c33SKohei Kaigai 			if (c1 == '+')
9280f7e4c33SKohei Kaigai 				c1 = ' ';
9290f7e4c33SKohei Kaigai 			else if (c1 == '%') {
930af7ff2c2SAndy Shevchenko 				c1 = hex_to_bin(*r++);
931af7ff2c2SAndy Shevchenko 				if (c1 < 0)
9320f7e4c33SKohei Kaigai 					goto out;
933af7ff2c2SAndy Shevchenko 				c2 = hex_to_bin(*r++);
934af7ff2c2SAndy Shevchenko 				if (c2 < 0)
9350f7e4c33SKohei Kaigai 					goto out;
9360f7e4c33SKohei Kaigai 				c1 = (c1 << 4) | c2;
9370f7e4c33SKohei Kaigai 			}
9380f7e4c33SKohei Kaigai 			*w++ = c1;
9390f7e4c33SKohei Kaigai 		} while (c1 != '\0');
9400f7e4c33SKohei Kaigai 
941f50a3ec9SKohei Kaigai 		objname = namebuf;
9420f7e4c33SKohei Kaigai 	}
943f50a3ec9SKohei Kaigai 
9440619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
945b77a493bSEric Paris 	if (length)
946b77a493bSEric Paris 		goto out;
947b77a493bSEric Paris 
9480619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
949b77a493bSEric Paris 	if (length)
950b77a493bSEric Paris 		goto out;
9511da177e4SLinus Torvalds 
9520619f0f5SStephen Smalley 	length = security_transition_sid_user(state, ssid, tsid, tclass,
9530619f0f5SStephen Smalley 					      objname, &newsid);
954b77a493bSEric Paris 	if (length)
955b77a493bSEric Paris 		goto out;
9561da177e4SLinus Torvalds 
9570619f0f5SStephen Smalley 	length = security_sid_to_context(state, newsid, &newcon, &len);
958b77a493bSEric Paris 	if (length)
959b77a493bSEric Paris 		goto out;
9601da177e4SLinus Torvalds 
961b77a493bSEric Paris 	length = -ERANGE;
9621da177e4SLinus Torvalds 	if (len > SIMPLE_TRANSACTION_LIMIT) {
963f8b69a5fSpeter enderborg 		pr_err("SELinux: %s:  context size (%u) exceeds "
964744ba35eSEric Paris 			"payload max\n", __func__, len);
965b77a493bSEric Paris 		goto out;
9661da177e4SLinus Torvalds 	}
9671da177e4SLinus Torvalds 
9681da177e4SLinus Torvalds 	memcpy(buf, newcon, len);
9691da177e4SLinus Torvalds 	length = len;
9701da177e4SLinus Torvalds out:
971b77a493bSEric Paris 	kfree(newcon);
972f50a3ec9SKohei Kaigai 	kfree(namebuf);
973b77a493bSEric Paris 	kfree(tcon);
9741da177e4SLinus Torvalds 	kfree(scon);
9751da177e4SLinus Torvalds 	return length;
9761da177e4SLinus Torvalds }
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
9791da177e4SLinus Torvalds {
9800619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
9810619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
982b77a493bSEric Paris 	char *scon = NULL, *tcon = NULL;
9831da177e4SLinus Torvalds 	u32 ssid, tsid, newsid;
9841da177e4SLinus Torvalds 	u16 tclass;
9851da177e4SLinus Torvalds 	ssize_t length;
986b77a493bSEric Paris 	char *newcon = NULL;
9871da177e4SLinus Torvalds 	u32 len;
9881da177e4SLinus Torvalds 
9896b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
9906b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
991be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__COMPUTE_RELABEL,
992be0554c9SStephen Smalley 			      NULL);
9931da177e4SLinus Torvalds 	if (length)
994b77a493bSEric Paris 		goto out;
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds 	length = -ENOMEM;
99789d155efSJames Morris 	scon = kzalloc(size + 1, GFP_KERNEL);
9981da177e4SLinus Torvalds 	if (!scon)
999b77a493bSEric Paris 		goto out;
10001da177e4SLinus Torvalds 
1001b77a493bSEric Paris 	length = -ENOMEM;
100289d155efSJames Morris 	tcon = kzalloc(size + 1, GFP_KERNEL);
10031da177e4SLinus Torvalds 	if (!tcon)
10041da177e4SLinus Torvalds 		goto out;
10051da177e4SLinus Torvalds 
10061da177e4SLinus Torvalds 	length = -EINVAL;
10071da177e4SLinus Torvalds 	if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
1008b77a493bSEric Paris 		goto out;
10091da177e4SLinus Torvalds 
10100619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
1011b77a493bSEric Paris 	if (length)
1012b77a493bSEric Paris 		goto out;
1013b77a493bSEric Paris 
10140619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
1015b77a493bSEric Paris 	if (length)
1016b77a493bSEric Paris 		goto out;
10171da177e4SLinus Torvalds 
10180619f0f5SStephen Smalley 	length = security_change_sid(state, ssid, tsid, tclass, &newsid);
1019b77a493bSEric Paris 	if (length)
1020b77a493bSEric Paris 		goto out;
10211da177e4SLinus Torvalds 
10220619f0f5SStephen Smalley 	length = security_sid_to_context(state, newsid, &newcon, &len);
1023b77a493bSEric Paris 	if (length)
1024b77a493bSEric Paris 		goto out;
10251da177e4SLinus Torvalds 
10261da177e4SLinus Torvalds 	length = -ERANGE;
1027b77a493bSEric Paris 	if (len > SIMPLE_TRANSACTION_LIMIT)
1028b77a493bSEric Paris 		goto out;
10291da177e4SLinus Torvalds 
10301da177e4SLinus Torvalds 	memcpy(buf, newcon, len);
10311da177e4SLinus Torvalds 	length = len;
10321da177e4SLinus Torvalds out:
1033b77a493bSEric Paris 	kfree(newcon);
1034b77a493bSEric Paris 	kfree(tcon);
10351da177e4SLinus Torvalds 	kfree(scon);
10361da177e4SLinus Torvalds 	return length;
10371da177e4SLinus Torvalds }
10381da177e4SLinus Torvalds 
10391da177e4SLinus Torvalds static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
10401da177e4SLinus Torvalds {
10410619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
10420619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
1043b77a493bSEric Paris 	char *con = NULL, *user = NULL, *ptr;
1044b77a493bSEric Paris 	u32 sid, *sids = NULL;
10451da177e4SLinus Torvalds 	ssize_t length;
10461da177e4SLinus Torvalds 	char *newcon;
10471da177e4SLinus Torvalds 	int i, rc;
10481da177e4SLinus Torvalds 	u32 len, nsids;
10491da177e4SLinus Torvalds 
10506b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
10516b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
1052be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__COMPUTE_USER,
1053be0554c9SStephen Smalley 			      NULL);
10541da177e4SLinus Torvalds 	if (length)
10556eab04a8SJustin P. Mattock 		goto out;
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds 	length = -ENOMEM;
105889d155efSJames Morris 	con = kzalloc(size + 1, GFP_KERNEL);
10591da177e4SLinus Torvalds 	if (!con)
10606eab04a8SJustin P. Mattock 		goto out;
10611da177e4SLinus Torvalds 
1062b77a493bSEric Paris 	length = -ENOMEM;
106389d155efSJames Morris 	user = kzalloc(size + 1, GFP_KERNEL);
10641da177e4SLinus Torvalds 	if (!user)
10651da177e4SLinus Torvalds 		goto out;
10661da177e4SLinus Torvalds 
10671da177e4SLinus Torvalds 	length = -EINVAL;
10681da177e4SLinus Torvalds 	if (sscanf(buf, "%s %s", con, user) != 2)
1069b77a493bSEric Paris 		goto out;
10701da177e4SLinus Torvalds 
10710619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, con, &sid, GFP_KERNEL);
1072b77a493bSEric Paris 	if (length)
1073b77a493bSEric Paris 		goto out;
10741da177e4SLinus Torvalds 
10750619f0f5SStephen Smalley 	length = security_get_user_sids(state, sid, user, &sids, &nsids);
1076b77a493bSEric Paris 	if (length)
1077b77a493bSEric Paris 		goto out;
10781da177e4SLinus Torvalds 
10791da177e4SLinus Torvalds 	length = sprintf(buf, "%u", nsids) + 1;
10801da177e4SLinus Torvalds 	ptr = buf + length;
10811da177e4SLinus Torvalds 	for (i = 0; i < nsids; i++) {
10820619f0f5SStephen Smalley 		rc = security_sid_to_context(state, sids[i], &newcon, &len);
10831da177e4SLinus Torvalds 		if (rc) {
10841da177e4SLinus Torvalds 			length = rc;
1085b77a493bSEric Paris 			goto out;
10861da177e4SLinus Torvalds 		}
10871da177e4SLinus Torvalds 		if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
10881da177e4SLinus Torvalds 			kfree(newcon);
10891da177e4SLinus Torvalds 			length = -ERANGE;
1090b77a493bSEric Paris 			goto out;
10911da177e4SLinus Torvalds 		}
10921da177e4SLinus Torvalds 		memcpy(ptr, newcon, len);
10931da177e4SLinus Torvalds 		kfree(newcon);
10941da177e4SLinus Torvalds 		ptr += len;
10951da177e4SLinus Torvalds 		length += len;
10961da177e4SLinus Torvalds 	}
10971da177e4SLinus Torvalds out:
1098b77a493bSEric Paris 	kfree(sids);
1099b77a493bSEric Paris 	kfree(user);
11001da177e4SLinus Torvalds 	kfree(con);
11011da177e4SLinus Torvalds 	return length;
11021da177e4SLinus Torvalds }
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
11051da177e4SLinus Torvalds {
11060619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
11070619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
1108b77a493bSEric Paris 	char *scon = NULL, *tcon = NULL;
11091da177e4SLinus Torvalds 	u32 ssid, tsid, newsid;
11101da177e4SLinus Torvalds 	u16 tclass;
11111da177e4SLinus Torvalds 	ssize_t length;
1112b77a493bSEric Paris 	char *newcon = NULL;
11131da177e4SLinus Torvalds 	u32 len;
11141da177e4SLinus Torvalds 
11156b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
11166b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
1117be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__COMPUTE_MEMBER,
1118be0554c9SStephen Smalley 			      NULL);
11191da177e4SLinus Torvalds 	if (length)
1120b77a493bSEric Paris 		goto out;
11211da177e4SLinus Torvalds 
11221da177e4SLinus Torvalds 	length = -ENOMEM;
112389d155efSJames Morris 	scon = kzalloc(size + 1, GFP_KERNEL);
11241da177e4SLinus Torvalds 	if (!scon)
11256eab04a8SJustin P. Mattock 		goto out;
11261da177e4SLinus Torvalds 
1127b77a493bSEric Paris 	length = -ENOMEM;
112889d155efSJames Morris 	tcon = kzalloc(size + 1, GFP_KERNEL);
11291da177e4SLinus Torvalds 	if (!tcon)
11301da177e4SLinus Torvalds 		goto out;
11311da177e4SLinus Torvalds 
11321da177e4SLinus Torvalds 	length = -EINVAL;
11331da177e4SLinus Torvalds 	if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
1134b77a493bSEric Paris 		goto out;
11351da177e4SLinus Torvalds 
11360619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
1137b77a493bSEric Paris 	if (length)
1138b77a493bSEric Paris 		goto out;
1139b77a493bSEric Paris 
11400619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
1141b77a493bSEric Paris 	if (length)
1142b77a493bSEric Paris 		goto out;
11431da177e4SLinus Torvalds 
11440619f0f5SStephen Smalley 	length = security_member_sid(state, ssid, tsid, tclass, &newsid);
1145b77a493bSEric Paris 	if (length)
1146b77a493bSEric Paris 		goto out;
11471da177e4SLinus Torvalds 
11480619f0f5SStephen Smalley 	length = security_sid_to_context(state, newsid, &newcon, &len);
1149b77a493bSEric Paris 	if (length)
1150b77a493bSEric Paris 		goto out;
11511da177e4SLinus Torvalds 
1152b77a493bSEric Paris 	length = -ERANGE;
11531da177e4SLinus Torvalds 	if (len > SIMPLE_TRANSACTION_LIMIT) {
1154f8b69a5fSpeter enderborg 		pr_err("SELinux: %s:  context size (%u) exceeds "
1155744ba35eSEric Paris 			"payload max\n", __func__, len);
1156b77a493bSEric Paris 		goto out;
11571da177e4SLinus Torvalds 	}
11581da177e4SLinus Torvalds 
11591da177e4SLinus Torvalds 	memcpy(buf, newcon, len);
11601da177e4SLinus Torvalds 	length = len;
11611da177e4SLinus Torvalds out:
1162b77a493bSEric Paris 	kfree(newcon);
1163b77a493bSEric Paris 	kfree(tcon);
11641da177e4SLinus Torvalds 	kfree(scon);
11651da177e4SLinus Torvalds 	return length;
11661da177e4SLinus Torvalds }
11671da177e4SLinus Torvalds 
11681da177e4SLinus Torvalds static struct inode *sel_make_inode(struct super_block *sb, int mode)
11691da177e4SLinus Torvalds {
11701da177e4SLinus Torvalds 	struct inode *ret = new_inode(sb);
11711da177e4SLinus Torvalds 
11721da177e4SLinus Torvalds 	if (ret) {
11731da177e4SLinus Torvalds 		ret->i_mode = mode;
1174078cd827SDeepa Dinamani 		ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret);
11751da177e4SLinus Torvalds 	}
11761da177e4SLinus Torvalds 	return ret;
11771da177e4SLinus Torvalds }
11781da177e4SLinus Torvalds 
11791da177e4SLinus Torvalds static ssize_t sel_read_bool(struct file *filep, char __user *buf,
11801da177e4SLinus Torvalds 			     size_t count, loff_t *ppos)
11811da177e4SLinus Torvalds {
11820619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
11831da177e4SLinus Torvalds 	char *page = NULL;
11841da177e4SLinus Torvalds 	ssize_t length;
11851da177e4SLinus Torvalds 	ssize_t ret;
11861da177e4SLinus Torvalds 	int cur_enforcing;
1187496ad9aaSAl Viro 	unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
1188d313f948SStephen Smalley 	const char *name = filep->f_path.dentry->d_name.name;
11891da177e4SLinus Torvalds 
11900619f0f5SStephen Smalley 	mutex_lock(&fsi->mutex);
11911da177e4SLinus Torvalds 
1192d313f948SStephen Smalley 	ret = -EINVAL;
11930619f0f5SStephen Smalley 	if (index >= fsi->bool_num || strcmp(name,
11940619f0f5SStephen Smalley 					     fsi->bool_pending_names[index]))
11950da74120SJann Horn 		goto out_unlock;
11961da177e4SLinus Torvalds 
11971da177e4SLinus Torvalds 	ret = -ENOMEM;
1198b77a493bSEric Paris 	page = (char *)get_zeroed_page(GFP_KERNEL);
1199b77a493bSEric Paris 	if (!page)
12000da74120SJann Horn 		goto out_unlock;
12011da177e4SLinus Torvalds 
12020619f0f5SStephen Smalley 	cur_enforcing = security_get_bool_value(fsi->state, index);
12031da177e4SLinus Torvalds 	if (cur_enforcing < 0) {
12041da177e4SLinus Torvalds 		ret = cur_enforcing;
12050da74120SJann Horn 		goto out_unlock;
12061da177e4SLinus Torvalds 	}
12071da177e4SLinus Torvalds 	length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
12080619f0f5SStephen Smalley 			  fsi->bool_pending_values[index]);
12090619f0f5SStephen Smalley 	mutex_unlock(&fsi->mutex);
12100da74120SJann Horn 	ret = simple_read_from_buffer(buf, count, ppos, page, length);
12110da74120SJann Horn out_free:
12121da177e4SLinus Torvalds 	free_page((unsigned long)page);
12131da177e4SLinus Torvalds 	return ret;
12140da74120SJann Horn 
12150da74120SJann Horn out_unlock:
12160da74120SJann Horn 	mutex_unlock(&fsi->mutex);
12170da74120SJann Horn 	goto out_free;
12181da177e4SLinus Torvalds }
12191da177e4SLinus Torvalds 
12201da177e4SLinus Torvalds static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
12211da177e4SLinus Torvalds 			      size_t count, loff_t *ppos)
12221da177e4SLinus Torvalds {
12230619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
12241da177e4SLinus Torvalds 	char *page = NULL;
1225d313f948SStephen Smalley 	ssize_t length;
12261da177e4SLinus Torvalds 	int new_value;
1227496ad9aaSAl Viro 	unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
1228d313f948SStephen Smalley 	const char *name = filep->f_path.dentry->d_name.name;
12291da177e4SLinus Torvalds 
12300da74120SJann Horn 	if (count >= PAGE_SIZE)
12310da74120SJann Horn 		return -ENOMEM;
12320da74120SJann Horn 
12330da74120SJann Horn 	/* No partial writes. */
12340da74120SJann Horn 	if (*ppos != 0)
12350da74120SJann Horn 		return -EINVAL;
12360da74120SJann Horn 
12370da74120SJann Horn 	page = memdup_user_nul(buf, count);
12380da74120SJann Horn 	if (IS_ERR(page))
12390da74120SJann Horn 		return PTR_ERR(page);
12400da74120SJann Horn 
12410619f0f5SStephen Smalley 	mutex_lock(&fsi->mutex);
12421da177e4SLinus Torvalds 
12436b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
12446b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
1245be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__SETBOOL,
1246be0554c9SStephen Smalley 			      NULL);
12471da177e4SLinus Torvalds 	if (length)
12481da177e4SLinus Torvalds 		goto out;
12491da177e4SLinus Torvalds 
1250d313f948SStephen Smalley 	length = -EINVAL;
12510619f0f5SStephen Smalley 	if (index >= fsi->bool_num || strcmp(name,
12520619f0f5SStephen Smalley 					     fsi->bool_pending_names[index]))
1253d313f948SStephen Smalley 		goto out;
1254d313f948SStephen Smalley 
12551da177e4SLinus Torvalds 	length = -EINVAL;
12561da177e4SLinus Torvalds 	if (sscanf(page, "%d", &new_value) != 1)
12571da177e4SLinus Torvalds 		goto out;
12581da177e4SLinus Torvalds 
12591da177e4SLinus Torvalds 	if (new_value)
12601da177e4SLinus Torvalds 		new_value = 1;
12611da177e4SLinus Torvalds 
12620619f0f5SStephen Smalley 	fsi->bool_pending_values[index] = new_value;
12631da177e4SLinus Torvalds 	length = count;
12641da177e4SLinus Torvalds 
12651da177e4SLinus Torvalds out:
12660619f0f5SStephen Smalley 	mutex_unlock(&fsi->mutex);
12678365a719SAl Viro 	kfree(page);
12681da177e4SLinus Torvalds 	return length;
12691da177e4SLinus Torvalds }
12701da177e4SLinus Torvalds 
12719c2e08c5SArjan van de Ven static const struct file_operations sel_bool_ops = {
12721da177e4SLinus Torvalds 	.read		= sel_read_bool,
12731da177e4SLinus Torvalds 	.write		= sel_write_bool,
127457a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
12751da177e4SLinus Torvalds };
12761da177e4SLinus Torvalds 
12771da177e4SLinus Torvalds static ssize_t sel_commit_bools_write(struct file *filep,
12781da177e4SLinus Torvalds 				      const char __user *buf,
12791da177e4SLinus Torvalds 				      size_t count, loff_t *ppos)
12801da177e4SLinus Torvalds {
12810619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
12821da177e4SLinus Torvalds 	char *page = NULL;
1283d313f948SStephen Smalley 	ssize_t length;
12841da177e4SLinus Torvalds 	int new_value;
12851da177e4SLinus Torvalds 
12860da74120SJann Horn 	if (count >= PAGE_SIZE)
12870da74120SJann Horn 		return -ENOMEM;
12880da74120SJann Horn 
12890da74120SJann Horn 	/* No partial writes. */
12900da74120SJann Horn 	if (*ppos != 0)
12910da74120SJann Horn 		return -EINVAL;
12920da74120SJann Horn 
12930da74120SJann Horn 	page = memdup_user_nul(buf, count);
12940da74120SJann Horn 	if (IS_ERR(page))
12950da74120SJann Horn 		return PTR_ERR(page);
12960da74120SJann Horn 
12970619f0f5SStephen Smalley 	mutex_lock(&fsi->mutex);
12981da177e4SLinus Torvalds 
12996b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
13006b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
1301be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__SETBOOL,
1302be0554c9SStephen Smalley 			      NULL);
13031da177e4SLinus Torvalds 	if (length)
13041da177e4SLinus Torvalds 		goto out;
13051da177e4SLinus Torvalds 
13061da177e4SLinus Torvalds 	length = -EINVAL;
13071da177e4SLinus Torvalds 	if (sscanf(page, "%d", &new_value) != 1)
13081da177e4SLinus Torvalds 		goto out;
13091da177e4SLinus Torvalds 
1310b77a493bSEric Paris 	length = 0;
13110619f0f5SStephen Smalley 	if (new_value && fsi->bool_pending_values)
13120619f0f5SStephen Smalley 		length = security_set_bools(fsi->state, fsi->bool_num,
13130619f0f5SStephen Smalley 					    fsi->bool_pending_values);
13141da177e4SLinus Torvalds 
1315b77a493bSEric Paris 	if (!length)
13161da177e4SLinus Torvalds 		length = count;
13171da177e4SLinus Torvalds 
13181da177e4SLinus Torvalds out:
13190619f0f5SStephen Smalley 	mutex_unlock(&fsi->mutex);
13208365a719SAl Viro 	kfree(page);
13211da177e4SLinus Torvalds 	return length;
13221da177e4SLinus Torvalds }
13231da177e4SLinus Torvalds 
13249c2e08c5SArjan van de Ven static const struct file_operations sel_commit_bools_ops = {
13251da177e4SLinus Torvalds 	.write		= sel_commit_bools_write,
132657a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
13271da177e4SLinus Torvalds };
13281da177e4SLinus Torvalds 
13290c92d7c7SChristopher J. PeBenito static void sel_remove_entries(struct dentry *de)
13301da177e4SLinus Torvalds {
1331ad52184bSAl Viro 	d_genocide(de);
1332ad52184bSAl Viro 	shrink_dcache_parent(de);
13331da177e4SLinus Torvalds }
13341da177e4SLinus Torvalds 
13351da177e4SLinus Torvalds #define BOOL_DIR_NAME "booleans"
13361da177e4SLinus Torvalds 
133702a52c5cSStephen Smalley static int sel_make_bools(struct selinux_fs_info *fsi,
133802a52c5cSStephen Smalley 			struct selinux_policy *newpolicy)
13391da177e4SLinus Torvalds {
134060abd318SOndrej Mosnacek 	int ret;
13411da177e4SLinus Torvalds 	ssize_t len;
13421da177e4SLinus Torvalds 	struct dentry *dentry = NULL;
13430619f0f5SStephen Smalley 	struct dentry *dir = fsi->bool_dir;
13441da177e4SLinus Torvalds 	struct inode *inode = NULL;
13451da177e4SLinus Torvalds 	struct inode_security_struct *isec;
13461da177e4SLinus Torvalds 	char **names = NULL, *page;
134760abd318SOndrej Mosnacek 	u32 i, num;
13481da177e4SLinus Torvalds 	int *values = NULL;
13491da177e4SLinus Torvalds 	u32 sid;
13501da177e4SLinus Torvalds 
13511da177e4SLinus Torvalds 	/* remove any existing files */
13520619f0f5SStephen Smalley 	for (i = 0; i < fsi->bool_num; i++)
13530619f0f5SStephen Smalley 		kfree(fsi->bool_pending_names[i]);
13540619f0f5SStephen Smalley 	kfree(fsi->bool_pending_names);
13550619f0f5SStephen Smalley 	kfree(fsi->bool_pending_values);
13560619f0f5SStephen Smalley 	fsi->bool_num = 0;
13570619f0f5SStephen Smalley 	fsi->bool_pending_names = NULL;
13580619f0f5SStephen Smalley 	fsi->bool_pending_values = NULL;
13591da177e4SLinus Torvalds 
13600c92d7c7SChristopher J. PeBenito 	sel_remove_entries(dir);
13611da177e4SLinus Torvalds 
1362b77a493bSEric Paris 	ret = -ENOMEM;
13631872981bSEric Paris 	page = (char *)get_zeroed_page(GFP_KERNEL);
13641872981bSEric Paris 	if (!page)
1365b77a493bSEric Paris 		goto out;
13661da177e4SLinus Torvalds 
136702a52c5cSStephen Smalley 	ret = security_get_bools(newpolicy, &num, &names, &values);
1368b77a493bSEric Paris 	if (ret)
13691da177e4SLinus Torvalds 		goto out;
13701da177e4SLinus Torvalds 
13711da177e4SLinus Torvalds 	for (i = 0; i < num; i++) {
1372b77a493bSEric Paris 		ret = -ENOMEM;
13731da177e4SLinus Torvalds 		dentry = d_alloc_name(dir, names[i]);
1374b77a493bSEric Paris 		if (!dentry)
1375b77a493bSEric Paris 			goto out;
13761da177e4SLinus Torvalds 
1377b77a493bSEric Paris 		ret = -ENOMEM;
1378b77a493bSEric Paris 		inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
13797e4237faSnixiaoming 		if (!inode) {
13807e4237faSnixiaoming 			dput(dentry);
1381b77a493bSEric Paris 			goto out;
13827e4237faSnixiaoming 		}
1383b77a493bSEric Paris 
13841da177e4SLinus Torvalds 		ret = -ENAMETOOLONG;
1385cc1dad71SAl Viro 		len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
13867e4237faSnixiaoming 		if (len >= PAGE_SIZE) {
13877e4237faSnixiaoming 			dput(dentry);
13887e4237faSnixiaoming 			iput(inode);
1389b77a493bSEric Paris 			goto out;
13907e4237faSnixiaoming 		}
1391b77a493bSEric Paris 
139280788c22SCasey Schaufler 		isec = selinux_inode(inode);
139302a52c5cSStephen Smalley 		ret = selinux_policy_genfs_sid(newpolicy, "selinuxfs", page,
1394aa8e712cSStephen Smalley 					 SECCLASS_FILE, &sid);
13954262fb51SGary Tierney 		if (ret) {
1396900fde06SGary Tierney 			pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n",
1397900fde06SGary Tierney 					   page);
1398900fde06SGary Tierney 			sid = SECINITSID_SECURITY;
13994262fb51SGary Tierney 		}
14004262fb51SGary Tierney 
14011da177e4SLinus Torvalds 		isec->sid = sid;
140242059112SAndreas Gruenbacher 		isec->initialized = LABEL_INITIALIZED;
14031da177e4SLinus Torvalds 		inode->i_fop = &sel_bool_ops;
1404bce34bc0SJames Carter 		inode->i_ino = i|SEL_BOOL_INO_OFFSET;
14051da177e4SLinus Torvalds 		d_add(dentry, inode);
14061da177e4SLinus Torvalds 	}
14070619f0f5SStephen Smalley 	fsi->bool_num = num;
14080619f0f5SStephen Smalley 	fsi->bool_pending_names = names;
14090619f0f5SStephen Smalley 	fsi->bool_pending_values = values;
1410b77a493bSEric Paris 
1411b77a493bSEric Paris 	free_page((unsigned long)page);
1412b77a493bSEric Paris 	return 0;
14131da177e4SLinus Torvalds out:
14141da177e4SLinus Torvalds 	free_page((unsigned long)page);
1415b77a493bSEric Paris 
14161da177e4SLinus Torvalds 	if (names) {
14179a5f04bfSJesper Juhl 		for (i = 0; i < num; i++)
14181da177e4SLinus Torvalds 			kfree(names[i]);
14191da177e4SLinus Torvalds 		kfree(names);
14201da177e4SLinus Torvalds 	}
142120c19e41SDavi Arnaut 	kfree(values);
14220c92d7c7SChristopher J. PeBenito 	sel_remove_entries(dir);
1423b77a493bSEric Paris 
1424b77a493bSEric Paris 	return ret;
14251da177e4SLinus Torvalds }
14261da177e4SLinus Torvalds 
14271da177e4SLinus Torvalds static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
14281da177e4SLinus Torvalds 					    size_t count, loff_t *ppos)
14291da177e4SLinus Torvalds {
14306b6bc620SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
14316b6bc620SStephen Smalley 	struct selinux_state *state = fsi->state;
14321da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
14331da177e4SLinus Torvalds 	ssize_t length;
14341da177e4SLinus Torvalds 
14356b6bc620SStephen Smalley 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
14366b6bc620SStephen Smalley 			   avc_get_cache_threshold(state->avc));
14371da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
14381da177e4SLinus Torvalds }
14391da177e4SLinus Torvalds 
14401da177e4SLinus Torvalds static ssize_t sel_write_avc_cache_threshold(struct file *file,
14411da177e4SLinus Torvalds 					     const char __user *buf,
14421da177e4SLinus Torvalds 					     size_t count, loff_t *ppos)
14431da177e4SLinus Torvalds 
14441da177e4SLinus Torvalds {
14456b6bc620SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
14466b6bc620SStephen Smalley 	struct selinux_state *state = fsi->state;
14478365a719SAl Viro 	char *page;
14481da177e4SLinus Torvalds 	ssize_t ret;
1449309c5fadSHeinrich Schuchardt 	unsigned int new_value;
14501da177e4SLinus Torvalds 
14516b6bc620SStephen Smalley 	ret = avc_has_perm(&selinux_state,
14526b6bc620SStephen Smalley 			   current_sid(), SECINITSID_SECURITY,
1453be0554c9SStephen Smalley 			   SECCLASS_SECURITY, SECURITY__SETSECPARAM,
1454be0554c9SStephen Smalley 			   NULL);
14551da177e4SLinus Torvalds 	if (ret)
14568365a719SAl Viro 		return ret;
1457b77a493bSEric Paris 
1458b77a493bSEric Paris 	if (count >= PAGE_SIZE)
14598365a719SAl Viro 		return -ENOMEM;
1460b77a493bSEric Paris 
1461b77a493bSEric Paris 	/* No partial writes. */
1462b77a493bSEric Paris 	if (*ppos != 0)
14638365a719SAl Viro 		return -EINVAL;
1464b77a493bSEric Paris 
14658365a719SAl Viro 	page = memdup_user_nul(buf, count);
14668365a719SAl Viro 	if (IS_ERR(page))
14678365a719SAl Viro 		return PTR_ERR(page);
1468b77a493bSEric Paris 
1469b77a493bSEric Paris 	ret = -EINVAL;
1470b77a493bSEric Paris 	if (sscanf(page, "%u", &new_value) != 1)
1471b77a493bSEric Paris 		goto out;
1472b77a493bSEric Paris 
14736b6bc620SStephen Smalley 	avc_set_cache_threshold(state->avc, new_value);
1474b77a493bSEric Paris 
14751da177e4SLinus Torvalds 	ret = count;
14761da177e4SLinus Torvalds out:
14778365a719SAl Viro 	kfree(page);
14781da177e4SLinus Torvalds 	return ret;
14791da177e4SLinus Torvalds }
14801da177e4SLinus Torvalds 
14811da177e4SLinus Torvalds static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
14821da177e4SLinus Torvalds 				       size_t count, loff_t *ppos)
14831da177e4SLinus Torvalds {
14846b6bc620SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
14856b6bc620SStephen Smalley 	struct selinux_state *state = fsi->state;
14861da177e4SLinus Torvalds 	char *page;
1487b77a493bSEric Paris 	ssize_t length;
14881da177e4SLinus Torvalds 
14891da177e4SLinus Torvalds 	page = (char *)__get_free_page(GFP_KERNEL);
1490b77a493bSEric Paris 	if (!page)
1491b77a493bSEric Paris 		return -ENOMEM;
1492b77a493bSEric Paris 
14936b6bc620SStephen Smalley 	length = avc_get_hash_stats(state->avc, page);
1494b77a493bSEric Paris 	if (length >= 0)
1495b77a493bSEric Paris 		length = simple_read_from_buffer(buf, count, ppos, page, length);
14961da177e4SLinus Torvalds 	free_page((unsigned long)page);
1497b77a493bSEric Paris 
1498b77a493bSEric Paris 	return length;
14991da177e4SLinus Torvalds }
15001da177e4SLinus Torvalds 
150166f8e2f0SJeff Vander Stoep static ssize_t sel_read_sidtab_hash_stats(struct file *filp, char __user *buf,
150266f8e2f0SJeff Vander Stoep 					size_t count, loff_t *ppos)
150366f8e2f0SJeff Vander Stoep {
150466f8e2f0SJeff Vander Stoep 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
150566f8e2f0SJeff Vander Stoep 	struct selinux_state *state = fsi->state;
150666f8e2f0SJeff Vander Stoep 	char *page;
150766f8e2f0SJeff Vander Stoep 	ssize_t length;
150866f8e2f0SJeff Vander Stoep 
150966f8e2f0SJeff Vander Stoep 	page = (char *)__get_free_page(GFP_KERNEL);
151066f8e2f0SJeff Vander Stoep 	if (!page)
151166f8e2f0SJeff Vander Stoep 		return -ENOMEM;
151266f8e2f0SJeff Vander Stoep 
151366f8e2f0SJeff Vander Stoep 	length = security_sidtab_hash_stats(state, page);
151466f8e2f0SJeff Vander Stoep 	if (length >= 0)
151566f8e2f0SJeff Vander Stoep 		length = simple_read_from_buffer(buf, count, ppos, page,
151666f8e2f0SJeff Vander Stoep 						length);
151766f8e2f0SJeff Vander Stoep 	free_page((unsigned long)page);
151866f8e2f0SJeff Vander Stoep 
151966f8e2f0SJeff Vander Stoep 	return length;
152066f8e2f0SJeff Vander Stoep }
152166f8e2f0SJeff Vander Stoep 
152266f8e2f0SJeff Vander Stoep static const struct file_operations sel_sidtab_hash_stats_ops = {
152366f8e2f0SJeff Vander Stoep 	.read		= sel_read_sidtab_hash_stats,
152466f8e2f0SJeff Vander Stoep 	.llseek		= generic_file_llseek,
152566f8e2f0SJeff Vander Stoep };
152666f8e2f0SJeff Vander Stoep 
15279c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_threshold_ops = {
15281da177e4SLinus Torvalds 	.read		= sel_read_avc_cache_threshold,
15291da177e4SLinus Torvalds 	.write		= sel_write_avc_cache_threshold,
153057a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
15311da177e4SLinus Torvalds };
15321da177e4SLinus Torvalds 
15339c2e08c5SArjan van de Ven static const struct file_operations sel_avc_hash_stats_ops = {
15341da177e4SLinus Torvalds 	.read		= sel_read_avc_hash_stats,
153557a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
15361da177e4SLinus Torvalds };
15371da177e4SLinus Torvalds 
15381da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
15391da177e4SLinus Torvalds static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
15401da177e4SLinus Torvalds {
15411da177e4SLinus Torvalds 	int cpu;
15421da177e4SLinus Torvalds 
15434f4b6c1aSRusty Russell 	for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
15441da177e4SLinus Torvalds 		if (!cpu_possible(cpu))
15451da177e4SLinus Torvalds 			continue;
15461da177e4SLinus Torvalds 		*idx = cpu + 1;
15471da177e4SLinus Torvalds 		return &per_cpu(avc_cache_stats, cpu);
15481da177e4SLinus Torvalds 	}
15498d269a8eSVasily Averin 	(*idx)++;
15501da177e4SLinus Torvalds 	return NULL;
15511da177e4SLinus Torvalds }
15521da177e4SLinus Torvalds 
15531da177e4SLinus Torvalds static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
15541da177e4SLinus Torvalds {
15551da177e4SLinus Torvalds 	loff_t n = *pos - 1;
15561da177e4SLinus Torvalds 
15571da177e4SLinus Torvalds 	if (*pos == 0)
15581da177e4SLinus Torvalds 		return SEQ_START_TOKEN;
15591da177e4SLinus Torvalds 
15601da177e4SLinus Torvalds 	return sel_avc_get_stat_idx(&n);
15611da177e4SLinus Torvalds }
15621da177e4SLinus Torvalds 
15631da177e4SLinus Torvalds static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
15641da177e4SLinus Torvalds {
15651da177e4SLinus Torvalds 	return sel_avc_get_stat_idx(pos);
15661da177e4SLinus Torvalds }
15671da177e4SLinus Torvalds 
15681da177e4SLinus Torvalds static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
15691da177e4SLinus Torvalds {
15701da177e4SLinus Torvalds 	struct avc_cache_stats *st = v;
15711da177e4SLinus Torvalds 
1572710a0647SMarkus Elfring 	if (v == SEQ_START_TOKEN) {
1573710a0647SMarkus Elfring 		seq_puts(seq,
1574710a0647SMarkus Elfring 			 "lookups hits misses allocations reclaims frees\n");
1575710a0647SMarkus Elfring 	} else {
1576257313b2SLinus Torvalds 		unsigned int lookups = st->lookups;
1577257313b2SLinus Torvalds 		unsigned int misses = st->misses;
1578257313b2SLinus Torvalds 		unsigned int hits = lookups - misses;
1579257313b2SLinus Torvalds 		seq_printf(seq, "%u %u %u %u %u %u\n", lookups,
1580257313b2SLinus Torvalds 			   hits, misses, st->allocations,
15811da177e4SLinus Torvalds 			   st->reclaims, st->frees);
1582257313b2SLinus Torvalds 	}
15831da177e4SLinus Torvalds 	return 0;
15841da177e4SLinus Torvalds }
15851da177e4SLinus Torvalds 
15861da177e4SLinus Torvalds static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
15871da177e4SLinus Torvalds { }
15881da177e4SLinus Torvalds 
15891996a109SJan Engelhardt static const struct seq_operations sel_avc_cache_stats_seq_ops = {
15901da177e4SLinus Torvalds 	.start		= sel_avc_stats_seq_start,
15911da177e4SLinus Torvalds 	.next		= sel_avc_stats_seq_next,
15921da177e4SLinus Torvalds 	.show		= sel_avc_stats_seq_show,
15931da177e4SLinus Torvalds 	.stop		= sel_avc_stats_seq_stop,
15941da177e4SLinus Torvalds };
15951da177e4SLinus Torvalds 
15961da177e4SLinus Torvalds static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
15971da177e4SLinus Torvalds {
15981da177e4SLinus Torvalds 	return seq_open(file, &sel_avc_cache_stats_seq_ops);
15991da177e4SLinus Torvalds }
16001da177e4SLinus Torvalds 
16019c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_stats_ops = {
16021da177e4SLinus Torvalds 	.open		= sel_open_avc_cache_stats,
16031da177e4SLinus Torvalds 	.read		= seq_read,
16041da177e4SLinus Torvalds 	.llseek		= seq_lseek,
16051da177e4SLinus Torvalds 	.release	= seq_release,
16061da177e4SLinus Torvalds };
16071da177e4SLinus Torvalds #endif
16081da177e4SLinus Torvalds 
16091da177e4SLinus Torvalds static int sel_make_avc_files(struct dentry *dir)
16101da177e4SLinus Torvalds {
16110619f0f5SStephen Smalley 	struct super_block *sb = dir->d_sb;
16120619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = sb->s_fs_info;
1613b77a493bSEric Paris 	int i;
1614cda37124SEric Biggers 	static const struct tree_descr files[] = {
16151da177e4SLinus Torvalds 		{ "cache_threshold",
16161da177e4SLinus Torvalds 		  &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
16171da177e4SLinus Torvalds 		{ "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
16181da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
16191da177e4SLinus Torvalds 		{ "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
16201da177e4SLinus Torvalds #endif
16211da177e4SLinus Torvalds 	};
16221da177e4SLinus Torvalds 
16236e20a64aSNicolas Kaiser 	for (i = 0; i < ARRAY_SIZE(files); i++) {
16241da177e4SLinus Torvalds 		struct inode *inode;
16251da177e4SLinus Torvalds 		struct dentry *dentry;
16261da177e4SLinus Torvalds 
16271da177e4SLinus Torvalds 		dentry = d_alloc_name(dir, files[i].name);
1628b77a493bSEric Paris 		if (!dentry)
1629b77a493bSEric Paris 			return -ENOMEM;
16301da177e4SLinus Torvalds 
16311da177e4SLinus Torvalds 		inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
16327e4237faSnixiaoming 		if (!inode) {
16337e4237faSnixiaoming 			dput(dentry);
1634b77a493bSEric Paris 			return -ENOMEM;
16357e4237faSnixiaoming 		}
1636b77a493bSEric Paris 
16371da177e4SLinus Torvalds 		inode->i_fop = files[i].ops;
16380619f0f5SStephen Smalley 		inode->i_ino = ++fsi->last_ino;
16391da177e4SLinus Torvalds 		d_add(dentry, inode);
16401da177e4SLinus Torvalds 	}
1641b77a493bSEric Paris 
1642b77a493bSEric Paris 	return 0;
16431da177e4SLinus Torvalds }
16441da177e4SLinus Torvalds 
164566f8e2f0SJeff Vander Stoep static int sel_make_ss_files(struct dentry *dir)
164666f8e2f0SJeff Vander Stoep {
164766f8e2f0SJeff Vander Stoep 	struct super_block *sb = dir->d_sb;
164866f8e2f0SJeff Vander Stoep 	struct selinux_fs_info *fsi = sb->s_fs_info;
164966f8e2f0SJeff Vander Stoep 	int i;
165066f8e2f0SJeff Vander Stoep 	static struct tree_descr files[] = {
165166f8e2f0SJeff Vander Stoep 		{ "sidtab_hash_stats", &sel_sidtab_hash_stats_ops, S_IRUGO },
165266f8e2f0SJeff Vander Stoep 	};
165366f8e2f0SJeff Vander Stoep 
165466f8e2f0SJeff Vander Stoep 	for (i = 0; i < ARRAY_SIZE(files); i++) {
165566f8e2f0SJeff Vander Stoep 		struct inode *inode;
165666f8e2f0SJeff Vander Stoep 		struct dentry *dentry;
165766f8e2f0SJeff Vander Stoep 
165866f8e2f0SJeff Vander Stoep 		dentry = d_alloc_name(dir, files[i].name);
165966f8e2f0SJeff Vander Stoep 		if (!dentry)
166066f8e2f0SJeff Vander Stoep 			return -ENOMEM;
166166f8e2f0SJeff Vander Stoep 
166266f8e2f0SJeff Vander Stoep 		inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
166366f8e2f0SJeff Vander Stoep 		if (!inode) {
166466f8e2f0SJeff Vander Stoep 			dput(dentry);
166566f8e2f0SJeff Vander Stoep 			return -ENOMEM;
166666f8e2f0SJeff Vander Stoep 		}
166766f8e2f0SJeff Vander Stoep 
166866f8e2f0SJeff Vander Stoep 		inode->i_fop = files[i].ops;
166966f8e2f0SJeff Vander Stoep 		inode->i_ino = ++fsi->last_ino;
167066f8e2f0SJeff Vander Stoep 		d_add(dentry, inode);
167166f8e2f0SJeff Vander Stoep 	}
167266f8e2f0SJeff Vander Stoep 
167366f8e2f0SJeff Vander Stoep 	return 0;
167466f8e2f0SJeff Vander Stoep }
167566f8e2f0SJeff Vander Stoep 
1676f0ee2e46SJames Carter static ssize_t sel_read_initcon(struct file *file, char __user *buf,
1677f0ee2e46SJames Carter 				size_t count, loff_t *ppos)
1678f0ee2e46SJames Carter {
16790619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1680f0ee2e46SJames Carter 	char *con;
1681f0ee2e46SJames Carter 	u32 sid, len;
1682f0ee2e46SJames Carter 	ssize_t ret;
1683f0ee2e46SJames Carter 
1684496ad9aaSAl Viro 	sid = file_inode(file)->i_ino&SEL_INO_MASK;
16850619f0f5SStephen Smalley 	ret = security_sid_to_context(fsi->state, sid, &con, &len);
1686b77a493bSEric Paris 	if (ret)
1687f0ee2e46SJames Carter 		return ret;
1688f0ee2e46SJames Carter 
1689f0ee2e46SJames Carter 	ret = simple_read_from_buffer(buf, count, ppos, con, len);
1690f0ee2e46SJames Carter 	kfree(con);
1691f0ee2e46SJames Carter 	return ret;
1692f0ee2e46SJames Carter }
1693f0ee2e46SJames Carter 
1694f0ee2e46SJames Carter static const struct file_operations sel_initcon_ops = {
1695f0ee2e46SJames Carter 	.read		= sel_read_initcon,
169657a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
1697f0ee2e46SJames Carter };
1698f0ee2e46SJames Carter 
1699f0ee2e46SJames Carter static int sel_make_initcon_files(struct dentry *dir)
1700f0ee2e46SJames Carter {
1701b77a493bSEric Paris 	int i;
1702f0ee2e46SJames Carter 
1703f0ee2e46SJames Carter 	for (i = 1; i <= SECINITSID_NUM; i++) {
1704f0ee2e46SJames Carter 		struct inode *inode;
1705f0ee2e46SJames Carter 		struct dentry *dentry;
1706e3e0b582SStephen Smalley 		const char *s = security_get_initial_sid_context(i);
1707e3e0b582SStephen Smalley 
1708e3e0b582SStephen Smalley 		if (!s)
1709e3e0b582SStephen Smalley 			continue;
1710e3e0b582SStephen Smalley 		dentry = d_alloc_name(dir, s);
1711b77a493bSEric Paris 		if (!dentry)
1712b77a493bSEric Paris 			return -ENOMEM;
1713f0ee2e46SJames Carter 
1714f0ee2e46SJames Carter 		inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
17157e4237faSnixiaoming 		if (!inode) {
17167e4237faSnixiaoming 			dput(dentry);
1717b77a493bSEric Paris 			return -ENOMEM;
17187e4237faSnixiaoming 		}
1719b77a493bSEric Paris 
1720f0ee2e46SJames Carter 		inode->i_fop = &sel_initcon_ops;
1721f0ee2e46SJames Carter 		inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1722f0ee2e46SJames Carter 		d_add(dentry, inode);
1723f0ee2e46SJames Carter 	}
1724b77a493bSEric Paris 
1725b77a493bSEric Paris 	return 0;
1726f0ee2e46SJames Carter }
1727f0ee2e46SJames Carter 
1728e47c8fc5SChristopher J. PeBenito static inline unsigned long sel_class_to_ino(u16 class)
1729e47c8fc5SChristopher J. PeBenito {
1730e47c8fc5SChristopher J. PeBenito 	return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1731e47c8fc5SChristopher J. PeBenito }
1732e47c8fc5SChristopher J. PeBenito 
1733e47c8fc5SChristopher J. PeBenito static inline u16 sel_ino_to_class(unsigned long ino)
1734e47c8fc5SChristopher J. PeBenito {
173592ae9e82SEric Paris 	return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1);
1736e47c8fc5SChristopher J. PeBenito }
1737e47c8fc5SChristopher J. PeBenito 
1738e47c8fc5SChristopher J. PeBenito static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1739e47c8fc5SChristopher J. PeBenito {
1740e47c8fc5SChristopher J. PeBenito 	return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1741e47c8fc5SChristopher J. PeBenito }
1742e47c8fc5SChristopher J. PeBenito 
1743e47c8fc5SChristopher J. PeBenito static inline u32 sel_ino_to_perm(unsigned long ino)
1744e47c8fc5SChristopher J. PeBenito {
1745e47c8fc5SChristopher J. PeBenito 	return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1746e47c8fc5SChristopher J. PeBenito }
1747e47c8fc5SChristopher J. PeBenito 
1748e47c8fc5SChristopher J. PeBenito static ssize_t sel_read_class(struct file *file, char __user *buf,
1749e47c8fc5SChristopher J. PeBenito 				size_t count, loff_t *ppos)
1750e47c8fc5SChristopher J. PeBenito {
1751496ad9aaSAl Viro 	unsigned long ino = file_inode(file)->i_ino;
1752cc1dad71SAl Viro 	char res[TMPBUFLEN];
17537e78c875Sliuyang34 	ssize_t len = scnprintf(res, sizeof(res), "%d", sel_ino_to_class(ino));
1754cc1dad71SAl Viro 	return simple_read_from_buffer(buf, count, ppos, res, len);
1755e47c8fc5SChristopher J. PeBenito }
1756e47c8fc5SChristopher J. PeBenito 
1757e47c8fc5SChristopher J. PeBenito static const struct file_operations sel_class_ops = {
1758e47c8fc5SChristopher J. PeBenito 	.read		= sel_read_class,
175957a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
1760e47c8fc5SChristopher J. PeBenito };
1761e47c8fc5SChristopher J. PeBenito 
1762e47c8fc5SChristopher J. PeBenito static ssize_t sel_read_perm(struct file *file, char __user *buf,
1763e47c8fc5SChristopher J. PeBenito 				size_t count, loff_t *ppos)
1764e47c8fc5SChristopher J. PeBenito {
1765496ad9aaSAl Viro 	unsigned long ino = file_inode(file)->i_ino;
1766cc1dad71SAl Viro 	char res[TMPBUFLEN];
17677e78c875Sliuyang34 	ssize_t len = scnprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino));
1768cc1dad71SAl Viro 	return simple_read_from_buffer(buf, count, ppos, res, len);
1769e47c8fc5SChristopher J. PeBenito }
1770e47c8fc5SChristopher J. PeBenito 
1771e47c8fc5SChristopher J. PeBenito static const struct file_operations sel_perm_ops = {
1772e47c8fc5SChristopher J. PeBenito 	.read		= sel_read_perm,
177357a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
1774e47c8fc5SChristopher J. PeBenito };
1775e47c8fc5SChristopher J. PeBenito 
17763bb56b25SPaul Moore static ssize_t sel_read_policycap(struct file *file, char __user *buf,
17773bb56b25SPaul Moore 				  size_t count, loff_t *ppos)
17783bb56b25SPaul Moore {
17790619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
17803bb56b25SPaul Moore 	int value;
17813bb56b25SPaul Moore 	char tmpbuf[TMPBUFLEN];
17823bb56b25SPaul Moore 	ssize_t length;
1783496ad9aaSAl Viro 	unsigned long i_ino = file_inode(file)->i_ino;
17843bb56b25SPaul Moore 
17850619f0f5SStephen Smalley 	value = security_policycap_supported(fsi->state, i_ino & SEL_INO_MASK);
17863bb56b25SPaul Moore 	length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
17873bb56b25SPaul Moore 
17883bb56b25SPaul Moore 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
17893bb56b25SPaul Moore }
17903bb56b25SPaul Moore 
17913bb56b25SPaul Moore static const struct file_operations sel_policycap_ops = {
17923bb56b25SPaul Moore 	.read		= sel_read_policycap,
179357a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
17943bb56b25SPaul Moore };
17953bb56b25SPaul Moore 
179602a52c5cSStephen Smalley static int sel_make_perm_files(struct selinux_policy *newpolicy,
179702a52c5cSStephen Smalley 			char *objclass, int classvalue,
1798e47c8fc5SChristopher J. PeBenito 			struct dentry *dir)
1799e47c8fc5SChristopher J. PeBenito {
1800b77a493bSEric Paris 	int i, rc, nperms;
1801e47c8fc5SChristopher J. PeBenito 	char **perms;
1802e47c8fc5SChristopher J. PeBenito 
180302a52c5cSStephen Smalley 	rc = security_get_permissions(newpolicy, objclass, &perms, &nperms);
1804e47c8fc5SChristopher J. PeBenito 	if (rc)
1805b77a493bSEric Paris 		return rc;
1806e47c8fc5SChristopher J. PeBenito 
1807e47c8fc5SChristopher J. PeBenito 	for (i = 0; i < nperms; i++) {
1808e47c8fc5SChristopher J. PeBenito 		struct inode *inode;
1809e47c8fc5SChristopher J. PeBenito 		struct dentry *dentry;
1810e47c8fc5SChristopher J. PeBenito 
1811b77a493bSEric Paris 		rc = -ENOMEM;
1812e47c8fc5SChristopher J. PeBenito 		dentry = d_alloc_name(dir, perms[i]);
1813b77a493bSEric Paris 		if (!dentry)
1814b77a493bSEric Paris 			goto out;
1815e47c8fc5SChristopher J. PeBenito 
1816e47c8fc5SChristopher J. PeBenito 		rc = -ENOMEM;
1817b77a493bSEric Paris 		inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
18187e4237faSnixiaoming 		if (!inode) {
18197e4237faSnixiaoming 			dput(dentry);
1820b77a493bSEric Paris 			goto out;
18217e4237faSnixiaoming 		}
1822b77a493bSEric Paris 
1823e47c8fc5SChristopher J. PeBenito 		inode->i_fop = &sel_perm_ops;
1824e47c8fc5SChristopher J. PeBenito 		/* i+1 since perm values are 1-indexed */
1825e47c8fc5SChristopher J. PeBenito 		inode->i_ino = sel_perm_to_ino(classvalue, i + 1);
1826e47c8fc5SChristopher J. PeBenito 		d_add(dentry, inode);
1827e47c8fc5SChristopher J. PeBenito 	}
1828b77a493bSEric Paris 	rc = 0;
1829b77a493bSEric Paris out:
1830e47c8fc5SChristopher J. PeBenito 	for (i = 0; i < nperms; i++)
1831e47c8fc5SChristopher J. PeBenito 		kfree(perms[i]);
1832e47c8fc5SChristopher J. PeBenito 	kfree(perms);
1833e47c8fc5SChristopher J. PeBenito 	return rc;
1834e47c8fc5SChristopher J. PeBenito }
1835e47c8fc5SChristopher J. PeBenito 
183602a52c5cSStephen Smalley static int sel_make_class_dir_entries(struct selinux_policy *newpolicy,
183702a52c5cSStephen Smalley 				char *classname, int index,
1838e47c8fc5SChristopher J. PeBenito 				struct dentry *dir)
1839e47c8fc5SChristopher J. PeBenito {
18400619f0f5SStephen Smalley 	struct super_block *sb = dir->d_sb;
18410619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = sb->s_fs_info;
1842e47c8fc5SChristopher J. PeBenito 	struct dentry *dentry = NULL;
1843e47c8fc5SChristopher J. PeBenito 	struct inode *inode = NULL;
1844e47c8fc5SChristopher J. PeBenito 	int rc;
1845e47c8fc5SChristopher J. PeBenito 
1846e47c8fc5SChristopher J. PeBenito 	dentry = d_alloc_name(dir, "index");
1847b77a493bSEric Paris 	if (!dentry)
1848b77a493bSEric Paris 		return -ENOMEM;
1849e47c8fc5SChristopher J. PeBenito 
1850e47c8fc5SChristopher J. PeBenito 	inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
18517e4237faSnixiaoming 	if (!inode) {
18527e4237faSnixiaoming 		dput(dentry);
1853b77a493bSEric Paris 		return -ENOMEM;
18547e4237faSnixiaoming 	}
1855e47c8fc5SChristopher J. PeBenito 
1856e47c8fc5SChristopher J. PeBenito 	inode->i_fop = &sel_class_ops;
1857e47c8fc5SChristopher J. PeBenito 	inode->i_ino = sel_class_to_ino(index);
1858e47c8fc5SChristopher J. PeBenito 	d_add(dentry, inode);
1859e47c8fc5SChristopher J. PeBenito 
18600619f0f5SStephen Smalley 	dentry = sel_make_dir(dir, "perms", &fsi->last_class_ino);
1861a1c2aa1eSAl Viro 	if (IS_ERR(dentry))
1862a1c2aa1eSAl Viro 		return PTR_ERR(dentry);
1863e47c8fc5SChristopher J. PeBenito 
186402a52c5cSStephen Smalley 	rc = sel_make_perm_files(newpolicy, classname, index, dentry);
1865e47c8fc5SChristopher J. PeBenito 
1866e47c8fc5SChristopher J. PeBenito 	return rc;
1867e47c8fc5SChristopher J. PeBenito }
1868e47c8fc5SChristopher J. PeBenito 
186902a52c5cSStephen Smalley static int sel_make_classes(struct selinux_fs_info *fsi,
187002a52c5cSStephen Smalley 			struct selinux_policy *newpolicy)
1871e47c8fc5SChristopher J. PeBenito {
18720619f0f5SStephen Smalley 
1873b77a493bSEric Paris 	int rc, nclasses, i;
1874e47c8fc5SChristopher J. PeBenito 	char **classes;
1875e47c8fc5SChristopher J. PeBenito 
1876e47c8fc5SChristopher J. PeBenito 	/* delete any existing entries */
18770619f0f5SStephen Smalley 	sel_remove_entries(fsi->class_dir);
1878e47c8fc5SChristopher J. PeBenito 
187902a52c5cSStephen Smalley 	rc = security_get_classes(newpolicy, &classes, &nclasses);
1880b77a493bSEric Paris 	if (rc)
1881b77a493bSEric Paris 		return rc;
1882e47c8fc5SChristopher J. PeBenito 
1883e47c8fc5SChristopher J. PeBenito 	/* +2 since classes are 1-indexed */
18840619f0f5SStephen Smalley 	fsi->last_class_ino = sel_class_to_ino(nclasses + 2);
1885e47c8fc5SChristopher J. PeBenito 
1886e47c8fc5SChristopher J. PeBenito 	for (i = 0; i < nclasses; i++) {
1887e47c8fc5SChristopher J. PeBenito 		struct dentry *class_name_dir;
1888e47c8fc5SChristopher J. PeBenito 
18890619f0f5SStephen Smalley 		class_name_dir = sel_make_dir(fsi->class_dir, classes[i],
18900619f0f5SStephen Smalley 					      &fsi->last_class_ino);
1891a1c2aa1eSAl Viro 		if (IS_ERR(class_name_dir)) {
1892a1c2aa1eSAl Viro 			rc = PTR_ERR(class_name_dir);
1893b77a493bSEric Paris 			goto out;
1894a1c2aa1eSAl Viro 		}
1895e47c8fc5SChristopher J. PeBenito 
1896e47c8fc5SChristopher J. PeBenito 		/* i+1 since class values are 1-indexed */
189702a52c5cSStephen Smalley 		rc = sel_make_class_dir_entries(newpolicy, classes[i], i + 1,
1898e47c8fc5SChristopher J. PeBenito 				class_name_dir);
1899e47c8fc5SChristopher J. PeBenito 		if (rc)
1900b77a493bSEric Paris 			goto out;
1901e47c8fc5SChristopher J. PeBenito 	}
1902b77a493bSEric Paris 	rc = 0;
1903b77a493bSEric Paris out:
1904e47c8fc5SChristopher J. PeBenito 	for (i = 0; i < nclasses; i++)
1905e47c8fc5SChristopher J. PeBenito 		kfree(classes[i]);
1906e47c8fc5SChristopher J. PeBenito 	kfree(classes);
1907e47c8fc5SChristopher J. PeBenito 	return rc;
1908e47c8fc5SChristopher J. PeBenito }
1909e47c8fc5SChristopher J. PeBenito 
19100619f0f5SStephen Smalley static int sel_make_policycap(struct selinux_fs_info *fsi)
19113bb56b25SPaul Moore {
19123bb56b25SPaul Moore 	unsigned int iter;
19133bb56b25SPaul Moore 	struct dentry *dentry = NULL;
19143bb56b25SPaul Moore 	struct inode *inode = NULL;
19153bb56b25SPaul Moore 
19163bb56b25SPaul Moore 	for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
19174dc2fce3SStephen Smalley 		if (iter < ARRAY_SIZE(selinux_policycap_names))
19180619f0f5SStephen Smalley 			dentry = d_alloc_name(fsi->policycap_dir,
19194dc2fce3SStephen Smalley 					      selinux_policycap_names[iter]);
19203bb56b25SPaul Moore 		else
19210619f0f5SStephen Smalley 			dentry = d_alloc_name(fsi->policycap_dir, "unknown");
19223bb56b25SPaul Moore 
19233bb56b25SPaul Moore 		if (dentry == NULL)
19243bb56b25SPaul Moore 			return -ENOMEM;
19253bb56b25SPaul Moore 
19260619f0f5SStephen Smalley 		inode = sel_make_inode(fsi->sb, S_IFREG | 0444);
19277e4237faSnixiaoming 		if (inode == NULL) {
19287e4237faSnixiaoming 			dput(dentry);
19293bb56b25SPaul Moore 			return -ENOMEM;
19307e4237faSnixiaoming 		}
19313bb56b25SPaul Moore 
19323bb56b25SPaul Moore 		inode->i_fop = &sel_policycap_ops;
19333bb56b25SPaul Moore 		inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
19343bb56b25SPaul Moore 		d_add(dentry, inode);
19353bb56b25SPaul Moore 	}
19363bb56b25SPaul Moore 
19373bb56b25SPaul Moore 	return 0;
19383bb56b25SPaul Moore }
19393bb56b25SPaul Moore 
1940a1c2aa1eSAl Viro static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
19410dd4ae51SChristopher J. PeBenito 			unsigned long *ino)
19421da177e4SLinus Torvalds {
1943a1c2aa1eSAl Viro 	struct dentry *dentry = d_alloc_name(dir, name);
19441da177e4SLinus Torvalds 	struct inode *inode;
19451da177e4SLinus Torvalds 
1946a1c2aa1eSAl Viro 	if (!dentry)
1947a1c2aa1eSAl Viro 		return ERR_PTR(-ENOMEM);
1948a1c2aa1eSAl Viro 
1949a1c2aa1eSAl Viro 	inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1950a1c2aa1eSAl Viro 	if (!inode) {
1951a1c2aa1eSAl Viro 		dput(dentry);
1952a1c2aa1eSAl Viro 		return ERR_PTR(-ENOMEM);
1953a1c2aa1eSAl Viro 	}
1954b77a493bSEric Paris 
19551da177e4SLinus Torvalds 	inode->i_op = &simple_dir_inode_operations;
19561da177e4SLinus Torvalds 	inode->i_fop = &simple_dir_operations;
19570dd4ae51SChristopher J. PeBenito 	inode->i_ino = ++(*ino);
195840e906f8SJames Morris 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
1959d8c76e6fSDave Hansen 	inc_nlink(inode);
19601da177e4SLinus Torvalds 	d_add(dentry, inode);
1961edb20fb5SJames Morris 	/* bump link count on parent directory, too */
1962ce0b16ddSDavid Howells 	inc_nlink(d_inode(dir));
1963b77a493bSEric Paris 
1964a1c2aa1eSAl Viro 	return dentry;
19651da177e4SLinus Torvalds }
19661da177e4SLinus Torvalds 
19670619f0f5SStephen Smalley #define NULL_FILE_NAME "null"
19680619f0f5SStephen Smalley 
1969920f50b2SDavid Howells static int sel_fill_super(struct super_block *sb, struct fs_context *fc)
19701da177e4SLinus Torvalds {
19710619f0f5SStephen Smalley 	struct selinux_fs_info *fsi;
19721da177e4SLinus Torvalds 	int ret;
19731da177e4SLinus Torvalds 	struct dentry *dentry;
1974a1c2aa1eSAl Viro 	struct inode *inode;
19751da177e4SLinus Torvalds 	struct inode_security_struct *isec;
19761da177e4SLinus Torvalds 
1977cda37124SEric Biggers 	static const struct tree_descr selinux_files[] = {
19781da177e4SLinus Torvalds 		[SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
19791da177e4SLinus Torvalds 		[SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
1980ce9982d0SStephen Smalley 		[SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
19811da177e4SLinus Torvalds 		[SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
19821da177e4SLinus Torvalds 		[SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
19831da177e4SLinus Torvalds 		[SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
19841da177e4SLinus Torvalds 		[SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
19851da177e4SLinus Torvalds 		[SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
19861da177e4SLinus Torvalds 		[SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
19871da177e4SLinus Torvalds 		[SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
19881da177e4SLinus Torvalds 		[SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
19891da177e4SLinus Torvalds 		[SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
19901da177e4SLinus Torvalds 		[SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
19913f12070eSEric Paris 		[SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
19923f12070eSEric Paris 		[SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
199311904167SKaiGai Kohei 		[SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO},
199472e8c859SEric Paris 		[SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO},
1995f9df6458SAndrew Perepechko 		[SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops,
1996f9df6458SAndrew Perepechko 					S_IWUGO},
19971da177e4SLinus Torvalds 		/* last one */ {""}
19981da177e4SLinus Torvalds 	};
19990619f0f5SStephen Smalley 
20000619f0f5SStephen Smalley 	ret = selinux_fs_info_create(sb);
20010619f0f5SStephen Smalley 	if (ret)
20020619f0f5SStephen Smalley 		goto err;
20030619f0f5SStephen Smalley 
20041da177e4SLinus Torvalds 	ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
20051da177e4SLinus Torvalds 	if (ret)
2006161ce45aSJames Morris 		goto err;
20071da177e4SLinus Torvalds 
20080619f0f5SStephen Smalley 	fsi = sb->s_fs_info;
20090619f0f5SStephen Smalley 	fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino);
20100619f0f5SStephen Smalley 	if (IS_ERR(fsi->bool_dir)) {
20110619f0f5SStephen Smalley 		ret = PTR_ERR(fsi->bool_dir);
20120619f0f5SStephen Smalley 		fsi->bool_dir = NULL;
2013161ce45aSJames Morris 		goto err;
2014a1c2aa1eSAl Viro 	}
20151da177e4SLinus Torvalds 
2016b77a493bSEric Paris 	ret = -ENOMEM;
20171da177e4SLinus Torvalds 	dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
2018b77a493bSEric Paris 	if (!dentry)
2019161ce45aSJames Morris 		goto err;
20201da177e4SLinus Torvalds 
2021161ce45aSJames Morris 	ret = -ENOMEM;
2022b77a493bSEric Paris 	inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
20237e4237faSnixiaoming 	if (!inode) {
20247e4237faSnixiaoming 		dput(dentry);
2025161ce45aSJames Morris 		goto err;
20267e4237faSnixiaoming 	}
2027b77a493bSEric Paris 
20280619f0f5SStephen Smalley 	inode->i_ino = ++fsi->last_ino;
202980788c22SCasey Schaufler 	isec = selinux_inode(inode);
20301da177e4SLinus Torvalds 	isec->sid = SECINITSID_DEVNULL;
20311da177e4SLinus Torvalds 	isec->sclass = SECCLASS_CHR_FILE;
203242059112SAndreas Gruenbacher 	isec->initialized = LABEL_INITIALIZED;
20331da177e4SLinus Torvalds 
20341da177e4SLinus Torvalds 	init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
20351da177e4SLinus Torvalds 	d_add(dentry, inode);
20361da177e4SLinus Torvalds 
20370619f0f5SStephen Smalley 	dentry = sel_make_dir(sb->s_root, "avc", &fsi->last_ino);
2038a1c2aa1eSAl Viro 	if (IS_ERR(dentry)) {
2039a1c2aa1eSAl Viro 		ret = PTR_ERR(dentry);
2040161ce45aSJames Morris 		goto err;
2041a1c2aa1eSAl Viro 	}
20421da177e4SLinus Torvalds 
20431da177e4SLinus Torvalds 	ret = sel_make_avc_files(dentry);
204466f8e2f0SJeff Vander Stoep 
204566f8e2f0SJeff Vander Stoep 	dentry = sel_make_dir(sb->s_root, "ss", &fsi->last_ino);
204666f8e2f0SJeff Vander Stoep 	if (IS_ERR(dentry)) {
204766f8e2f0SJeff Vander Stoep 		ret = PTR_ERR(dentry);
204866f8e2f0SJeff Vander Stoep 		goto err;
204966f8e2f0SJeff Vander Stoep 	}
205066f8e2f0SJeff Vander Stoep 
205166f8e2f0SJeff Vander Stoep 	ret = sel_make_ss_files(dentry);
20521da177e4SLinus Torvalds 	if (ret)
2053161ce45aSJames Morris 		goto err;
2054f0ee2e46SJames Carter 
20550619f0f5SStephen Smalley 	dentry = sel_make_dir(sb->s_root, "initial_contexts", &fsi->last_ino);
2056a1c2aa1eSAl Viro 	if (IS_ERR(dentry)) {
2057a1c2aa1eSAl Viro 		ret = PTR_ERR(dentry);
2058f0ee2e46SJames Carter 		goto err;
2059a1c2aa1eSAl Viro 	}
2060f0ee2e46SJames Carter 
2061f0ee2e46SJames Carter 	ret = sel_make_initcon_files(dentry);
2062f0ee2e46SJames Carter 	if (ret)
2063f0ee2e46SJames Carter 		goto err;
2064f0ee2e46SJames Carter 
20650619f0f5SStephen Smalley 	fsi->class_dir = sel_make_dir(sb->s_root, "class", &fsi->last_ino);
20660619f0f5SStephen Smalley 	if (IS_ERR(fsi->class_dir)) {
20670619f0f5SStephen Smalley 		ret = PTR_ERR(fsi->class_dir);
20680619f0f5SStephen Smalley 		fsi->class_dir = NULL;
2069e47c8fc5SChristopher J. PeBenito 		goto err;
2070a1c2aa1eSAl Viro 	}
2071e47c8fc5SChristopher J. PeBenito 
20720619f0f5SStephen Smalley 	fsi->policycap_dir = sel_make_dir(sb->s_root, "policy_capabilities",
20730619f0f5SStephen Smalley 					  &fsi->last_ino);
20740619f0f5SStephen Smalley 	if (IS_ERR(fsi->policycap_dir)) {
20750619f0f5SStephen Smalley 		ret = PTR_ERR(fsi->policycap_dir);
20760619f0f5SStephen Smalley 		fsi->policycap_dir = NULL;
2077e47c8fc5SChristopher J. PeBenito 		goto err;
2078a1c2aa1eSAl Viro 	}
20790619f0f5SStephen Smalley 
208002a52c5cSStephen Smalley 	ret = sel_make_policycap(fsi);
208102a52c5cSStephen Smalley 	if (ret) {
208202a52c5cSStephen Smalley 		pr_err("SELinux: failed to load policy capabilities\n");
20830619f0f5SStephen Smalley 		goto err;
208402a52c5cSStephen Smalley 	}
208502a52c5cSStephen Smalley 
2086b77a493bSEric Paris 	return 0;
2087161ce45aSJames Morris err:
2088f8b69a5fSpeter enderborg 	pr_err("SELinux: %s:  failed while creating inodes\n",
2089744ba35eSEric Paris 		__func__);
20900619f0f5SStephen Smalley 
20910619f0f5SStephen Smalley 	selinux_fs_info_free(sb);
20920619f0f5SStephen Smalley 
2093b77a493bSEric Paris 	return ret;
20941da177e4SLinus Torvalds }
20951da177e4SLinus Torvalds 
2096920f50b2SDavid Howells static int sel_get_tree(struct fs_context *fc)
20971da177e4SLinus Torvalds {
2098920f50b2SDavid Howells 	return get_tree_single(fc, sel_fill_super);
2099920f50b2SDavid Howells }
2100920f50b2SDavid Howells 
2101920f50b2SDavid Howells static const struct fs_context_operations sel_context_ops = {
2102920f50b2SDavid Howells 	.get_tree	= sel_get_tree,
2103920f50b2SDavid Howells };
2104920f50b2SDavid Howells 
2105920f50b2SDavid Howells static int sel_init_fs_context(struct fs_context *fc)
2106920f50b2SDavid Howells {
2107920f50b2SDavid Howells 	fc->ops = &sel_context_ops;
2108920f50b2SDavid Howells 	return 0;
21091da177e4SLinus Torvalds }
21101da177e4SLinus Torvalds 
21110619f0f5SStephen Smalley static void sel_kill_sb(struct super_block *sb)
21120619f0f5SStephen Smalley {
21130619f0f5SStephen Smalley 	selinux_fs_info_free(sb);
21140619f0f5SStephen Smalley 	kill_litter_super(sb);
21150619f0f5SStephen Smalley }
21160619f0f5SStephen Smalley 
21171da177e4SLinus Torvalds static struct file_system_type sel_fs_type = {
21181da177e4SLinus Torvalds 	.name		= "selinuxfs",
2119920f50b2SDavid Howells 	.init_fs_context = sel_init_fs_context,
21200619f0f5SStephen Smalley 	.kill_sb	= sel_kill_sb,
21211da177e4SLinus Torvalds };
21221da177e4SLinus Torvalds 
21231da177e4SLinus Torvalds struct vfsmount *selinuxfs_mount;
21240619f0f5SStephen Smalley struct path selinux_null;
21251da177e4SLinus Torvalds 
21261da177e4SLinus Torvalds static int __init init_sel_fs(void)
21271da177e4SLinus Torvalds {
21280619f0f5SStephen Smalley 	struct qstr null_name = QSTR_INIT(NULL_FILE_NAME,
21290619f0f5SStephen Smalley 					  sizeof(NULL_FILE_NAME)-1);
21301da177e4SLinus Torvalds 	int err;
21311da177e4SLinus Torvalds 
21326c5a682eSStephen Smalley 	if (!selinux_enabled_boot)
21331da177e4SLinus Torvalds 		return 0;
21347a627e3bSGreg Kroah-Hartman 
2135f9bb4882SEric W. Biederman 	err = sysfs_create_mount_point(fs_kobj, "selinux");
2136f9bb4882SEric W. Biederman 	if (err)
2137f9bb4882SEric W. Biederman 		return err;
21387a627e3bSGreg Kroah-Hartman 
21391da177e4SLinus Torvalds 	err = register_filesystem(&sel_fs_type);
21407a627e3bSGreg Kroah-Hartman 	if (err) {
2141f9bb4882SEric W. Biederman 		sysfs_remove_mount_point(fs_kobj, "selinux");
2142b77a493bSEric Paris 		return err;
21437a627e3bSGreg Kroah-Hartman 	}
2144b77a493bSEric Paris 
2145765927b2SAl Viro 	selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type);
21461da177e4SLinus Torvalds 	if (IS_ERR(selinuxfs_mount)) {
2147f8b69a5fSpeter enderborg 		pr_err("selinuxfs:  could not mount!\n");
21481da177e4SLinus Torvalds 		err = PTR_ERR(selinuxfs_mount);
21491da177e4SLinus Torvalds 		selinuxfs_mount = NULL;
21501da177e4SLinus Torvalds 	}
21510619f0f5SStephen Smalley 	selinux_null.dentry = d_hash_and_lookup(selinux_null.mnt->mnt_root,
21520619f0f5SStephen Smalley 						&null_name);
21530619f0f5SStephen Smalley 	if (IS_ERR(selinux_null.dentry)) {
21540619f0f5SStephen Smalley 		pr_err("selinuxfs:  could not lookup null!\n");
21550619f0f5SStephen Smalley 		err = PTR_ERR(selinux_null.dentry);
21560619f0f5SStephen Smalley 		selinux_null.dentry = NULL;
21570619f0f5SStephen Smalley 	}
2158b77a493bSEric Paris 
21591da177e4SLinus Torvalds 	return err;
21601da177e4SLinus Torvalds }
21611da177e4SLinus Torvalds 
21621da177e4SLinus Torvalds __initcall(init_sel_fs);
21631da177e4SLinus Torvalds 
21641da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
21651da177e4SLinus Torvalds void exit_sel_fs(void)
21661da177e4SLinus Torvalds {
2167f9bb4882SEric W. Biederman 	sysfs_remove_mount_point(fs_kobj, "selinux");
2168fd40ffc7SStephen Smalley 	dput(selinux_null.dentry);
2169423e0ab0STim Chen 	kern_unmount(selinuxfs_mount);
21701da177e4SLinus Torvalds 	unregister_filesystem(&sel_fs_type);
21711da177e4SLinus Torvalds }
21721da177e4SLinus Torvalds #endif
2173