xref: /openbmc/linux/security/selinux/selinuxfs.c (revision e3e0b582)
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 */
3490619f0f5SStephen Smalley static int sel_make_bools(struct selinux_fs_info *fsi);
3500619f0f5SStephen Smalley static int sel_make_classes(struct selinux_fs_info *fsi);
3510619f0f5SStephen Smalley static int sel_make_policycap(struct selinux_fs_info *fsi);
352e47c8fc5SChristopher J. PeBenito 
353e47c8fc5SChristopher J. PeBenito /* declaration for sel_make_class_dirs */
354a1c2aa1eSAl Viro static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
355e47c8fc5SChristopher J. PeBenito 			unsigned long *ino);
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds static ssize_t sel_read_mls(struct file *filp, char __user *buf,
3581da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
3591da177e4SLinus Torvalds {
3600619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
3611da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
3621da177e4SLinus Torvalds 	ssize_t length;
3631da177e4SLinus Torvalds 
3640719aaf5SGuido Trentalancia 	length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
3650619f0f5SStephen Smalley 			   security_mls_enabled(fsi->state));
3661da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
3671da177e4SLinus Torvalds }
3681da177e4SLinus Torvalds 
3699c2e08c5SArjan van de Ven static const struct file_operations sel_mls_ops = {
3701da177e4SLinus Torvalds 	.read		= sel_read_mls,
37157a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
3721da177e4SLinus Torvalds };
3731da177e4SLinus Torvalds 
374cee74f47SEric Paris struct policy_load_memory {
375cee74f47SEric Paris 	size_t len;
376cee74f47SEric Paris 	void *data;
377cee74f47SEric Paris };
378cee74f47SEric Paris 
379cee74f47SEric Paris static int sel_open_policy(struct inode *inode, struct file *filp)
380cee74f47SEric Paris {
3810619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
3820619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
383cee74f47SEric Paris 	struct policy_load_memory *plm = NULL;
384cee74f47SEric Paris 	int rc;
385cee74f47SEric Paris 
386cee74f47SEric Paris 	BUG_ON(filp->private_data);
387cee74f47SEric Paris 
3880619f0f5SStephen Smalley 	mutex_lock(&fsi->mutex);
389cee74f47SEric Paris 
3906b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
3916b6bc620SStephen Smalley 			  current_sid(), SECINITSID_SECURITY,
392be0554c9SStephen Smalley 			  SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
393cee74f47SEric Paris 	if (rc)
394cee74f47SEric Paris 		goto err;
395cee74f47SEric Paris 
396cee74f47SEric Paris 	rc = -EBUSY;
3970619f0f5SStephen Smalley 	if (fsi->policy_opened)
398cee74f47SEric Paris 		goto err;
399cee74f47SEric Paris 
400cee74f47SEric Paris 	rc = -ENOMEM;
401cee74f47SEric Paris 	plm = kzalloc(sizeof(*plm), GFP_KERNEL);
402cee74f47SEric Paris 	if (!plm)
403cee74f47SEric Paris 		goto err;
404cee74f47SEric Paris 
4050619f0f5SStephen Smalley 	if (i_size_read(inode) != security_policydb_len(state)) {
4065955102cSAl Viro 		inode_lock(inode);
4070619f0f5SStephen Smalley 		i_size_write(inode, security_policydb_len(state));
4085955102cSAl Viro 		inode_unlock(inode);
409cee74f47SEric Paris 	}
410cee74f47SEric Paris 
4110619f0f5SStephen Smalley 	rc = security_read_policy(state, &plm->data, &plm->len);
412cee74f47SEric Paris 	if (rc)
413cee74f47SEric Paris 		goto err;
414cee74f47SEric Paris 
4150619f0f5SStephen Smalley 	fsi->policy_opened = 1;
416cee74f47SEric Paris 
417cee74f47SEric Paris 	filp->private_data = plm;
418cee74f47SEric Paris 
4190619f0f5SStephen Smalley 	mutex_unlock(&fsi->mutex);
420cee74f47SEric Paris 
421cee74f47SEric Paris 	return 0;
422cee74f47SEric Paris err:
4230619f0f5SStephen Smalley 	mutex_unlock(&fsi->mutex);
424cee74f47SEric Paris 
425cee74f47SEric Paris 	if (plm)
426cee74f47SEric Paris 		vfree(plm->data);
427cee74f47SEric Paris 	kfree(plm);
428cee74f47SEric Paris 	return rc;
429cee74f47SEric Paris }
430cee74f47SEric Paris 
431cee74f47SEric Paris static int sel_release_policy(struct inode *inode, struct file *filp)
432cee74f47SEric Paris {
4330619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
434cee74f47SEric Paris 	struct policy_load_memory *plm = filp->private_data;
435cee74f47SEric Paris 
436cee74f47SEric Paris 	BUG_ON(!plm);
437cee74f47SEric Paris 
4380619f0f5SStephen Smalley 	fsi->policy_opened = 0;
439cee74f47SEric Paris 
440cee74f47SEric Paris 	vfree(plm->data);
441cee74f47SEric Paris 	kfree(plm);
442cee74f47SEric Paris 
443cee74f47SEric Paris 	return 0;
444cee74f47SEric Paris }
445cee74f47SEric Paris 
446cee74f47SEric Paris static ssize_t sel_read_policy(struct file *filp, char __user *buf,
447cee74f47SEric Paris 			       size_t count, loff_t *ppos)
448cee74f47SEric Paris {
449cee74f47SEric Paris 	struct policy_load_memory *plm = filp->private_data;
450cee74f47SEric Paris 	int ret;
451cee74f47SEric Paris 
4526b6bc620SStephen Smalley 	ret = avc_has_perm(&selinux_state,
4536b6bc620SStephen Smalley 			   current_sid(), SECINITSID_SECURITY,
454be0554c9SStephen Smalley 			  SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
455cee74f47SEric Paris 	if (ret)
456cee74f47SEric Paris 		return ret;
4570da74120SJann Horn 
4580da74120SJann Horn 	return simple_read_from_buffer(buf, count, ppos, plm->data, plm->len);
459cee74f47SEric Paris }
460cee74f47SEric Paris 
461ac9a1f6dSSouptick Joarder static vm_fault_t sel_mmap_policy_fault(struct vm_fault *vmf)
462845ca30fSEric Paris {
46311bac800SDave Jiang 	struct policy_load_memory *plm = vmf->vma->vm_file->private_data;
464845ca30fSEric Paris 	unsigned long offset;
465845ca30fSEric Paris 	struct page *page;
466845ca30fSEric Paris 
467845ca30fSEric Paris 	if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE))
468845ca30fSEric Paris 		return VM_FAULT_SIGBUS;
469845ca30fSEric Paris 
470845ca30fSEric Paris 	offset = vmf->pgoff << PAGE_SHIFT;
471845ca30fSEric Paris 	if (offset >= roundup(plm->len, PAGE_SIZE))
472845ca30fSEric Paris 		return VM_FAULT_SIGBUS;
473845ca30fSEric Paris 
474845ca30fSEric Paris 	page = vmalloc_to_page(plm->data + offset);
475845ca30fSEric Paris 	get_page(page);
476845ca30fSEric Paris 
477845ca30fSEric Paris 	vmf->page = page;
478845ca30fSEric Paris 
479845ca30fSEric Paris 	return 0;
480845ca30fSEric Paris }
481845ca30fSEric Paris 
4827cbea8dcSKirill A. Shutemov static const struct vm_operations_struct sel_mmap_policy_ops = {
483845ca30fSEric Paris 	.fault = sel_mmap_policy_fault,
484845ca30fSEric Paris 	.page_mkwrite = sel_mmap_policy_fault,
485845ca30fSEric Paris };
486845ca30fSEric Paris 
487ad3fa08cSJames Morris static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma)
488845ca30fSEric Paris {
489845ca30fSEric Paris 	if (vma->vm_flags & VM_SHARED) {
490845ca30fSEric Paris 		/* do not allow mprotect to make mapping writable */
491845ca30fSEric Paris 		vma->vm_flags &= ~VM_MAYWRITE;
492845ca30fSEric Paris 
493845ca30fSEric Paris 		if (vma->vm_flags & VM_WRITE)
494845ca30fSEric Paris 			return -EACCES;
495845ca30fSEric Paris 	}
496845ca30fSEric Paris 
497314e51b9SKonstantin Khlebnikov 	vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
498845ca30fSEric Paris 	vma->vm_ops = &sel_mmap_policy_ops;
499845ca30fSEric Paris 
500845ca30fSEric Paris 	return 0;
501845ca30fSEric Paris }
502845ca30fSEric Paris 
503cee74f47SEric Paris static const struct file_operations sel_policy_ops = {
504cee74f47SEric Paris 	.open		= sel_open_policy,
505cee74f47SEric Paris 	.read		= sel_read_policy,
506845ca30fSEric Paris 	.mmap		= sel_mmap_policy,
507cee74f47SEric Paris 	.release	= sel_release_policy,
50847a93a5bSEric Paris 	.llseek		= generic_file_llseek,
509cee74f47SEric Paris };
510cee74f47SEric Paris 
5110619f0f5SStephen Smalley static int sel_make_policy_nodes(struct selinux_fs_info *fsi)
5120619f0f5SStephen Smalley {
5130619f0f5SStephen Smalley 	int ret;
5140619f0f5SStephen Smalley 
5150619f0f5SStephen Smalley 	ret = sel_make_bools(fsi);
5160619f0f5SStephen Smalley 	if (ret) {
5170619f0f5SStephen Smalley 		pr_err("SELinux: failed to load policy booleans\n");
5180619f0f5SStephen Smalley 		return ret;
5190619f0f5SStephen Smalley 	}
5200619f0f5SStephen Smalley 
5210619f0f5SStephen Smalley 	ret = sel_make_classes(fsi);
5220619f0f5SStephen Smalley 	if (ret) {
5230619f0f5SStephen Smalley 		pr_err("SELinux: failed to load policy classes\n");
5240619f0f5SStephen Smalley 		return ret;
5250619f0f5SStephen Smalley 	}
5260619f0f5SStephen Smalley 
5270619f0f5SStephen Smalley 	ret = sel_make_policycap(fsi);
5280619f0f5SStephen Smalley 	if (ret) {
5290619f0f5SStephen Smalley 		pr_err("SELinux: failed to load policy capabilities\n");
5300619f0f5SStephen Smalley 		return ret;
5310619f0f5SStephen Smalley 	}
5320619f0f5SStephen Smalley 
5330619f0f5SStephen Smalley 	return 0;
5340619f0f5SStephen Smalley }
5350619f0f5SStephen Smalley 
5361da177e4SLinus Torvalds static ssize_t sel_write_load(struct file *file, const char __user *buf,
5371da177e4SLinus Torvalds 			      size_t count, loff_t *ppos)
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds {
5400619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
5411da177e4SLinus Torvalds 	ssize_t length;
5421da177e4SLinus Torvalds 	void *data = NULL;
5431da177e4SLinus Torvalds 
5440619f0f5SStephen Smalley 	mutex_lock(&fsi->mutex);
5451da177e4SLinus Torvalds 
5466b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
5476b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
548be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL);
5491da177e4SLinus Torvalds 	if (length)
5501da177e4SLinus Torvalds 		goto out;
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds 	/* No partial writes. */
5531da177e4SLinus Torvalds 	length = -EINVAL;
554b77a493bSEric Paris 	if (*ppos != 0)
5551da177e4SLinus Torvalds 		goto out;
5561da177e4SLinus Torvalds 
557b77a493bSEric Paris 	length = -ENOMEM;
558b77a493bSEric Paris 	data = vmalloc(count);
559b77a493bSEric Paris 	if (!data)
560b77a493bSEric Paris 		goto out;
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds 	length = -EFAULT;
5631da177e4SLinus Torvalds 	if (copy_from_user(data, buf, count) != 0)
5641da177e4SLinus Torvalds 		goto out;
5651da177e4SLinus Torvalds 
5660619f0f5SStephen Smalley 	length = security_load_policy(fsi->state, data, count);
5674262fb51SGary Tierney 	if (length) {
5684262fb51SGary Tierney 		pr_warn_ratelimited("SELinux: failed to load policy\n");
5691da177e4SLinus Torvalds 		goto out;
5704262fb51SGary Tierney 	}
5711da177e4SLinus Torvalds 
5720619f0f5SStephen Smalley 	length = sel_make_policy_nodes(fsi);
5730619f0f5SStephen Smalley 	if (length)
574e47c8fc5SChristopher J. PeBenito 		goto out1;
575b77a493bSEric Paris 
5761da177e4SLinus Torvalds 	length = count;
577e47c8fc5SChristopher J. PeBenito 
578e47c8fc5SChristopher J. PeBenito out1:
579cdfb6b34SRichard Guy Briggs 	audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
580d141136fSRichard Guy Briggs 		"auid=%u ses=%u lsm=selinux res=1",
581581abc09SEric W. Biederman 		from_kuid(&init_user_ns, audit_get_loginuid(current)),
5824746ec5bSEric Paris 		audit_get_sessionid(current));
5831da177e4SLinus Torvalds out:
5840619f0f5SStephen Smalley 	mutex_unlock(&fsi->mutex);
5851da177e4SLinus Torvalds 	vfree(data);
5861da177e4SLinus Torvalds 	return length;
5871da177e4SLinus Torvalds }
5881da177e4SLinus Torvalds 
5899c2e08c5SArjan van de Ven static const struct file_operations sel_load_ops = {
5901da177e4SLinus Torvalds 	.write		= sel_write_load,
59157a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
5921da177e4SLinus Torvalds };
5931da177e4SLinus Torvalds 
594ce9982d0SStephen Smalley static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
5951da177e4SLinus Torvalds {
5960619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
5970619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
598b77a493bSEric Paris 	char *canon = NULL;
599ce9982d0SStephen Smalley 	u32 sid, len;
6001da177e4SLinus Torvalds 	ssize_t length;
6011da177e4SLinus Torvalds 
6026b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
6036b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
604be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__CHECK_CONTEXT, NULL);
6051da177e4SLinus Torvalds 	if (length)
606b77a493bSEric Paris 		goto out;
6071da177e4SLinus Torvalds 
6080619f0f5SStephen Smalley 	length = security_context_to_sid(state, buf, size, &sid, GFP_KERNEL);
609b77a493bSEric Paris 	if (length)
610b77a493bSEric Paris 		goto out;
6111da177e4SLinus Torvalds 
6120619f0f5SStephen Smalley 	length = security_sid_to_context(state, sid, &canon, &len);
613b77a493bSEric Paris 	if (length)
614b77a493bSEric Paris 		goto out;
615ce9982d0SStephen Smalley 
616b77a493bSEric Paris 	length = -ERANGE;
617ce9982d0SStephen Smalley 	if (len > SIMPLE_TRANSACTION_LIMIT) {
618f8b69a5fSpeter enderborg 		pr_err("SELinux: %s:  context size (%u) exceeds "
619744ba35eSEric Paris 			"payload max\n", __func__, len);
620ce9982d0SStephen Smalley 		goto out;
621ce9982d0SStephen Smalley 	}
622ce9982d0SStephen Smalley 
623ce9982d0SStephen Smalley 	memcpy(buf, canon, len);
624ce9982d0SStephen Smalley 	length = len;
6251da177e4SLinus Torvalds out:
626ce9982d0SStephen Smalley 	kfree(canon);
6271da177e4SLinus Torvalds 	return length;
6281da177e4SLinus Torvalds }
6291da177e4SLinus Torvalds 
6301da177e4SLinus Torvalds static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
6311da177e4SLinus Torvalds 				     size_t count, loff_t *ppos)
6321da177e4SLinus Torvalds {
6330619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
6341da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
6351da177e4SLinus Torvalds 	ssize_t length;
6361da177e4SLinus Torvalds 
6370619f0f5SStephen Smalley 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u", fsi->state->checkreqprot);
6381da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
6391da177e4SLinus Torvalds }
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
6421da177e4SLinus Torvalds 				      size_t count, loff_t *ppos)
6431da177e4SLinus Torvalds {
6440619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
6458365a719SAl Viro 	char *page;
6461da177e4SLinus Torvalds 	ssize_t length;
6471da177e4SLinus Torvalds 	unsigned int new_value;
6481da177e4SLinus Torvalds 
6496b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
6506b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
651be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__SETCHECKREQPROT,
652be0554c9SStephen Smalley 			      NULL);
6531da177e4SLinus Torvalds 	if (length)
6548365a719SAl Viro 		return length;
6551da177e4SLinus Torvalds 
656bfd51626SDavi Arnaut 	if (count >= PAGE_SIZE)
6578365a719SAl Viro 		return -ENOMEM;
658b77a493bSEric Paris 
6591da177e4SLinus Torvalds 	/* No partial writes. */
660b77a493bSEric Paris 	if (*ppos != 0)
6618365a719SAl Viro 		return -EINVAL;
662b77a493bSEric Paris 
6638365a719SAl Viro 	page = memdup_user_nul(buf, count);
6648365a719SAl Viro 	if (IS_ERR(page))
6658365a719SAl Viro 		return PTR_ERR(page);
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds 	length = -EINVAL;
6681da177e4SLinus Torvalds 	if (sscanf(page, "%u", &new_value) != 1)
6691da177e4SLinus Torvalds 		goto out;
6701da177e4SLinus Torvalds 
671e9c38f9fSStephen Smalley 	if (new_value) {
672e9c38f9fSStephen Smalley 		char comm[sizeof(current->comm)];
673e9c38f9fSStephen Smalley 
674e9c38f9fSStephen Smalley 		memcpy(comm, current->comm, sizeof(comm));
675e9c38f9fSStephen Smalley 		pr_warn_once("SELinux: %s (%d) set checkreqprot to 1. This is deprecated and will be rejected in a future kernel release.\n",
676e9c38f9fSStephen Smalley 			     comm, current->pid);
677e9c38f9fSStephen Smalley 	}
678e9c38f9fSStephen Smalley 
6790619f0f5SStephen Smalley 	fsi->state->checkreqprot = new_value ? 1 : 0;
6801da177e4SLinus Torvalds 	length = count;
6811da177e4SLinus Torvalds out:
6828365a719SAl Viro 	kfree(page);
6831da177e4SLinus Torvalds 	return length;
6841da177e4SLinus Torvalds }
6859c2e08c5SArjan van de Ven static const struct file_operations sel_checkreqprot_ops = {
6861da177e4SLinus Torvalds 	.read		= sel_read_checkreqprot,
6871da177e4SLinus Torvalds 	.write		= sel_write_checkreqprot,
68857a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
6891da177e4SLinus Torvalds };
6901da177e4SLinus Torvalds 
691f9df6458SAndrew Perepechko static ssize_t sel_write_validatetrans(struct file *file,
692f9df6458SAndrew Perepechko 					const char __user *buf,
693f9df6458SAndrew Perepechko 					size_t count, loff_t *ppos)
694f9df6458SAndrew Perepechko {
6950619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
6960619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
697f9df6458SAndrew Perepechko 	char *oldcon = NULL, *newcon = NULL, *taskcon = NULL;
698f9df6458SAndrew Perepechko 	char *req = NULL;
699f9df6458SAndrew Perepechko 	u32 osid, nsid, tsid;
700f9df6458SAndrew Perepechko 	u16 tclass;
701f9df6458SAndrew Perepechko 	int rc;
702f9df6458SAndrew Perepechko 
7036b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
7046b6bc620SStephen Smalley 			  current_sid(), SECINITSID_SECURITY,
705be0554c9SStephen Smalley 			  SECCLASS_SECURITY, SECURITY__VALIDATE_TRANS, NULL);
706f9df6458SAndrew Perepechko 	if (rc)
707f9df6458SAndrew Perepechko 		goto out;
708f9df6458SAndrew Perepechko 
709f9df6458SAndrew Perepechko 	rc = -ENOMEM;
710f9df6458SAndrew Perepechko 	if (count >= PAGE_SIZE)
711f9df6458SAndrew Perepechko 		goto out;
712f9df6458SAndrew Perepechko 
713f9df6458SAndrew Perepechko 	/* No partial writes. */
714f9df6458SAndrew Perepechko 	rc = -EINVAL;
715f9df6458SAndrew Perepechko 	if (*ppos != 0)
716f9df6458SAndrew Perepechko 		goto out;
717f9df6458SAndrew Perepechko 
7180b884d25SAl Viro 	req = memdup_user_nul(buf, count);
7190b884d25SAl Viro 	if (IS_ERR(req)) {
7200b884d25SAl Viro 		rc = PTR_ERR(req);
7210b884d25SAl Viro 		req = NULL;
722f9df6458SAndrew Perepechko 		goto out;
7230b884d25SAl Viro 	}
724f9df6458SAndrew Perepechko 
725f9df6458SAndrew Perepechko 	rc = -ENOMEM;
726f9df6458SAndrew Perepechko 	oldcon = kzalloc(count + 1, GFP_KERNEL);
727f9df6458SAndrew Perepechko 	if (!oldcon)
728f9df6458SAndrew Perepechko 		goto out;
729f9df6458SAndrew Perepechko 
730f9df6458SAndrew Perepechko 	newcon = kzalloc(count + 1, GFP_KERNEL);
731f9df6458SAndrew Perepechko 	if (!newcon)
732f9df6458SAndrew Perepechko 		goto out;
733f9df6458SAndrew Perepechko 
734f9df6458SAndrew Perepechko 	taskcon = kzalloc(count + 1, GFP_KERNEL);
735f9df6458SAndrew Perepechko 	if (!taskcon)
736f9df6458SAndrew Perepechko 		goto out;
737f9df6458SAndrew Perepechko 
738f9df6458SAndrew Perepechko 	rc = -EINVAL;
739f9df6458SAndrew Perepechko 	if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4)
740f9df6458SAndrew Perepechko 		goto out;
741f9df6458SAndrew Perepechko 
7420619f0f5SStephen Smalley 	rc = security_context_str_to_sid(state, oldcon, &osid, GFP_KERNEL);
743f9df6458SAndrew Perepechko 	if (rc)
744f9df6458SAndrew Perepechko 		goto out;
745f9df6458SAndrew Perepechko 
7460619f0f5SStephen Smalley 	rc = security_context_str_to_sid(state, newcon, &nsid, GFP_KERNEL);
747f9df6458SAndrew Perepechko 	if (rc)
748f9df6458SAndrew Perepechko 		goto out;
749f9df6458SAndrew Perepechko 
7500619f0f5SStephen Smalley 	rc = security_context_str_to_sid(state, taskcon, &tsid, GFP_KERNEL);
751f9df6458SAndrew Perepechko 	if (rc)
752f9df6458SAndrew Perepechko 		goto out;
753f9df6458SAndrew Perepechko 
7540619f0f5SStephen Smalley 	rc = security_validate_transition_user(state, osid, nsid, tsid, tclass);
755f9df6458SAndrew Perepechko 	if (!rc)
756f9df6458SAndrew Perepechko 		rc = count;
757f9df6458SAndrew Perepechko out:
758f9df6458SAndrew Perepechko 	kfree(req);
759f9df6458SAndrew Perepechko 	kfree(oldcon);
760f9df6458SAndrew Perepechko 	kfree(newcon);
761f9df6458SAndrew Perepechko 	kfree(taskcon);
762f9df6458SAndrew Perepechko 	return rc;
763f9df6458SAndrew Perepechko }
764f9df6458SAndrew Perepechko 
765f9df6458SAndrew Perepechko static const struct file_operations sel_transition_ops = {
766f9df6458SAndrew Perepechko 	.write		= sel_write_validatetrans,
767f9df6458SAndrew Perepechko 	.llseek		= generic_file_llseek,
768f9df6458SAndrew Perepechko };
769f9df6458SAndrew Perepechko 
7701da177e4SLinus Torvalds /*
7711da177e4SLinus Torvalds  * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
7721da177e4SLinus Torvalds  */
7731da177e4SLinus Torvalds static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
7741da177e4SLinus Torvalds static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
7751da177e4SLinus Torvalds static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
7761da177e4SLinus Torvalds static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
7771da177e4SLinus Torvalds static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
7781da177e4SLinus Torvalds 
779631d2b49SEric Biggers static ssize_t (*const write_op[])(struct file *, char *, size_t) = {
7801da177e4SLinus Torvalds 	[SEL_ACCESS] = sel_write_access,
7811da177e4SLinus Torvalds 	[SEL_CREATE] = sel_write_create,
7821da177e4SLinus Torvalds 	[SEL_RELABEL] = sel_write_relabel,
7831da177e4SLinus Torvalds 	[SEL_USER] = sel_write_user,
7841da177e4SLinus Torvalds 	[SEL_MEMBER] = sel_write_member,
785ce9982d0SStephen Smalley 	[SEL_CONTEXT] = sel_write_context,
7861da177e4SLinus Torvalds };
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
7891da177e4SLinus Torvalds {
790496ad9aaSAl Viro 	ino_t ino = file_inode(file)->i_ino;
7911da177e4SLinus Torvalds 	char *data;
7921da177e4SLinus Torvalds 	ssize_t rv;
7931da177e4SLinus Torvalds 
7946e20a64aSNicolas Kaiser 	if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
7951da177e4SLinus Torvalds 		return -EINVAL;
7961da177e4SLinus Torvalds 
7971da177e4SLinus Torvalds 	data = simple_transaction_get(file, buf, size);
7981da177e4SLinus Torvalds 	if (IS_ERR(data))
7991da177e4SLinus Torvalds 		return PTR_ERR(data);
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds 	rv = write_op[ino](file, data, size);
8021da177e4SLinus Torvalds 	if (rv > 0) {
8031da177e4SLinus Torvalds 		simple_transaction_set(file, rv);
8041da177e4SLinus Torvalds 		rv = size;
8051da177e4SLinus Torvalds 	}
8061da177e4SLinus Torvalds 	return rv;
8071da177e4SLinus Torvalds }
8081da177e4SLinus Torvalds 
8099c2e08c5SArjan van de Ven static const struct file_operations transaction_ops = {
8101da177e4SLinus Torvalds 	.write		= selinux_transaction_write,
8111da177e4SLinus Torvalds 	.read		= simple_transaction_read,
8121da177e4SLinus Torvalds 	.release	= simple_transaction_release,
81357a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
8141da177e4SLinus Torvalds };
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds /*
8171da177e4SLinus Torvalds  * payload - write methods
8181da177e4SLinus Torvalds  * If the method has a response, the response should be put in buf,
8191da177e4SLinus Torvalds  * and the length returned.  Otherwise return 0 or and -error.
8201da177e4SLinus Torvalds  */
8211da177e4SLinus Torvalds 
8221da177e4SLinus Torvalds static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
8231da177e4SLinus Torvalds {
8240619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
8250619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
826b77a493bSEric Paris 	char *scon = NULL, *tcon = NULL;
8271da177e4SLinus Torvalds 	u32 ssid, tsid;
8281da177e4SLinus Torvalds 	u16 tclass;
8291da177e4SLinus Torvalds 	struct av_decision avd;
8301da177e4SLinus Torvalds 	ssize_t length;
8311da177e4SLinus Torvalds 
8326b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
8336b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
834be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__COMPUTE_AV, NULL);
8351da177e4SLinus Torvalds 	if (length)
836b77a493bSEric Paris 		goto out;
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds 	length = -ENOMEM;
83989d155efSJames Morris 	scon = kzalloc(size + 1, GFP_KERNEL);
8401da177e4SLinus Torvalds 	if (!scon)
841b77a493bSEric Paris 		goto out;
8421da177e4SLinus Torvalds 
843b77a493bSEric Paris 	length = -ENOMEM;
84489d155efSJames Morris 	tcon = kzalloc(size + 1, GFP_KERNEL);
8451da177e4SLinus Torvalds 	if (!tcon)
8461da177e4SLinus Torvalds 		goto out;
8471da177e4SLinus Torvalds 
8481da177e4SLinus Torvalds 	length = -EINVAL;
84919439d05SStephen Smalley 	if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
850b77a493bSEric Paris 		goto out;
8511da177e4SLinus Torvalds 
8520619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
853b77a493bSEric Paris 	if (length)
854b77a493bSEric Paris 		goto out;
855b77a493bSEric Paris 
8560619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
857b77a493bSEric Paris 	if (length)
858b77a493bSEric Paris 		goto out;
8591da177e4SLinus Torvalds 
8600619f0f5SStephen Smalley 	security_compute_av_user(state, ssid, tsid, tclass, &avd);
8611da177e4SLinus Torvalds 
8621da177e4SLinus Torvalds 	length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
8638a6f83afSKaiGai Kohei 			  "%x %x %x %x %u %x",
864f1c6381aSEric Paris 			  avd.allowed, 0xffffffff,
8651da177e4SLinus Torvalds 			  avd.auditallow, avd.auditdeny,
8668a6f83afSKaiGai Kohei 			  avd.seqno, avd.flags);
8671da177e4SLinus Torvalds out:
868b77a493bSEric Paris 	kfree(tcon);
8691da177e4SLinus Torvalds 	kfree(scon);
8701da177e4SLinus Torvalds 	return length;
8711da177e4SLinus Torvalds }
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
8741da177e4SLinus Torvalds {
8750619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
8760619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
877b77a493bSEric Paris 	char *scon = NULL, *tcon = NULL;
878f50a3ec9SKohei Kaigai 	char *namebuf = NULL, *objname = NULL;
8791da177e4SLinus Torvalds 	u32 ssid, tsid, newsid;
8801da177e4SLinus Torvalds 	u16 tclass;
8811da177e4SLinus Torvalds 	ssize_t length;
882b77a493bSEric Paris 	char *newcon = NULL;
8831da177e4SLinus Torvalds 	u32 len;
884f50a3ec9SKohei Kaigai 	int nargs;
8851da177e4SLinus Torvalds 
8866b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
8876b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
888be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__COMPUTE_CREATE,
889be0554c9SStephen Smalley 			      NULL);
8901da177e4SLinus Torvalds 	if (length)
891b77a493bSEric Paris 		goto out;
8921da177e4SLinus Torvalds 
8931da177e4SLinus Torvalds 	length = -ENOMEM;
89489d155efSJames Morris 	scon = kzalloc(size + 1, GFP_KERNEL);
8951da177e4SLinus Torvalds 	if (!scon)
896b77a493bSEric Paris 		goto out;
8971da177e4SLinus Torvalds 
898b77a493bSEric Paris 	length = -ENOMEM;
89989d155efSJames Morris 	tcon = kzalloc(size + 1, GFP_KERNEL);
9001da177e4SLinus Torvalds 	if (!tcon)
9011da177e4SLinus Torvalds 		goto out;
9021da177e4SLinus Torvalds 
903f50a3ec9SKohei Kaigai 	length = -ENOMEM;
904f50a3ec9SKohei Kaigai 	namebuf = kzalloc(size + 1, GFP_KERNEL);
905f50a3ec9SKohei Kaigai 	if (!namebuf)
906b77a493bSEric Paris 		goto out;
9071da177e4SLinus Torvalds 
908f50a3ec9SKohei Kaigai 	length = -EINVAL;
909f50a3ec9SKohei Kaigai 	nargs = sscanf(buf, "%s %s %hu %s", scon, tcon, &tclass, namebuf);
910f50a3ec9SKohei Kaigai 	if (nargs < 3 || nargs > 4)
911f50a3ec9SKohei Kaigai 		goto out;
9120f7e4c33SKohei Kaigai 	if (nargs == 4) {
9130f7e4c33SKohei Kaigai 		/*
9140f7e4c33SKohei Kaigai 		 * If and when the name of new object to be queried contains
9150f7e4c33SKohei Kaigai 		 * either whitespace or multibyte characters, they shall be
9160f7e4c33SKohei Kaigai 		 * encoded based on the percentage-encoding rule.
9170f7e4c33SKohei Kaigai 		 * If not encoded, the sscanf logic picks up only left-half
9180f7e4c33SKohei Kaigai 		 * of the supplied name; splitted by a whitespace unexpectedly.
9190f7e4c33SKohei Kaigai 		 */
9200f7e4c33SKohei Kaigai 		char   *r, *w;
9210f7e4c33SKohei Kaigai 		int     c1, c2;
9220f7e4c33SKohei Kaigai 
9230f7e4c33SKohei Kaigai 		r = w = namebuf;
9240f7e4c33SKohei Kaigai 		do {
9250f7e4c33SKohei Kaigai 			c1 = *r++;
9260f7e4c33SKohei Kaigai 			if (c1 == '+')
9270f7e4c33SKohei Kaigai 				c1 = ' ';
9280f7e4c33SKohei Kaigai 			else if (c1 == '%') {
929af7ff2c2SAndy Shevchenko 				c1 = hex_to_bin(*r++);
930af7ff2c2SAndy Shevchenko 				if (c1 < 0)
9310f7e4c33SKohei Kaigai 					goto out;
932af7ff2c2SAndy Shevchenko 				c2 = hex_to_bin(*r++);
933af7ff2c2SAndy Shevchenko 				if (c2 < 0)
9340f7e4c33SKohei Kaigai 					goto out;
9350f7e4c33SKohei Kaigai 				c1 = (c1 << 4) | c2;
9360f7e4c33SKohei Kaigai 			}
9370f7e4c33SKohei Kaigai 			*w++ = c1;
9380f7e4c33SKohei Kaigai 		} while (c1 != '\0');
9390f7e4c33SKohei Kaigai 
940f50a3ec9SKohei Kaigai 		objname = namebuf;
9410f7e4c33SKohei Kaigai 	}
942f50a3ec9SKohei Kaigai 
9430619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
944b77a493bSEric Paris 	if (length)
945b77a493bSEric Paris 		goto out;
946b77a493bSEric Paris 
9470619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
948b77a493bSEric Paris 	if (length)
949b77a493bSEric Paris 		goto out;
9501da177e4SLinus Torvalds 
9510619f0f5SStephen Smalley 	length = security_transition_sid_user(state, ssid, tsid, tclass,
9520619f0f5SStephen Smalley 					      objname, &newsid);
953b77a493bSEric Paris 	if (length)
954b77a493bSEric Paris 		goto out;
9551da177e4SLinus Torvalds 
9560619f0f5SStephen Smalley 	length = security_sid_to_context(state, newsid, &newcon, &len);
957b77a493bSEric Paris 	if (length)
958b77a493bSEric Paris 		goto out;
9591da177e4SLinus Torvalds 
960b77a493bSEric Paris 	length = -ERANGE;
9611da177e4SLinus Torvalds 	if (len > SIMPLE_TRANSACTION_LIMIT) {
962f8b69a5fSpeter enderborg 		pr_err("SELinux: %s:  context size (%u) exceeds "
963744ba35eSEric Paris 			"payload max\n", __func__, len);
964b77a493bSEric Paris 		goto out;
9651da177e4SLinus Torvalds 	}
9661da177e4SLinus Torvalds 
9671da177e4SLinus Torvalds 	memcpy(buf, newcon, len);
9681da177e4SLinus Torvalds 	length = len;
9691da177e4SLinus Torvalds out:
970b77a493bSEric Paris 	kfree(newcon);
971f50a3ec9SKohei Kaigai 	kfree(namebuf);
972b77a493bSEric Paris 	kfree(tcon);
9731da177e4SLinus Torvalds 	kfree(scon);
9741da177e4SLinus Torvalds 	return length;
9751da177e4SLinus Torvalds }
9761da177e4SLinus Torvalds 
9771da177e4SLinus Torvalds static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
9781da177e4SLinus Torvalds {
9790619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
9800619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
981b77a493bSEric Paris 	char *scon = NULL, *tcon = NULL;
9821da177e4SLinus Torvalds 	u32 ssid, tsid, newsid;
9831da177e4SLinus Torvalds 	u16 tclass;
9841da177e4SLinus Torvalds 	ssize_t length;
985b77a493bSEric Paris 	char *newcon = NULL;
9861da177e4SLinus Torvalds 	u32 len;
9871da177e4SLinus Torvalds 
9886b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
9896b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
990be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__COMPUTE_RELABEL,
991be0554c9SStephen Smalley 			      NULL);
9921da177e4SLinus Torvalds 	if (length)
993b77a493bSEric Paris 		goto out;
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds 	length = -ENOMEM;
99689d155efSJames Morris 	scon = kzalloc(size + 1, GFP_KERNEL);
9971da177e4SLinus Torvalds 	if (!scon)
998b77a493bSEric Paris 		goto out;
9991da177e4SLinus Torvalds 
1000b77a493bSEric Paris 	length = -ENOMEM;
100189d155efSJames Morris 	tcon = kzalloc(size + 1, GFP_KERNEL);
10021da177e4SLinus Torvalds 	if (!tcon)
10031da177e4SLinus Torvalds 		goto out;
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds 	length = -EINVAL;
10061da177e4SLinus Torvalds 	if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
1007b77a493bSEric Paris 		goto out;
10081da177e4SLinus Torvalds 
10090619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
1010b77a493bSEric Paris 	if (length)
1011b77a493bSEric Paris 		goto out;
1012b77a493bSEric Paris 
10130619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
1014b77a493bSEric Paris 	if (length)
1015b77a493bSEric Paris 		goto out;
10161da177e4SLinus Torvalds 
10170619f0f5SStephen Smalley 	length = security_change_sid(state, ssid, tsid, tclass, &newsid);
1018b77a493bSEric Paris 	if (length)
1019b77a493bSEric Paris 		goto out;
10201da177e4SLinus Torvalds 
10210619f0f5SStephen Smalley 	length = security_sid_to_context(state, newsid, &newcon, &len);
1022b77a493bSEric Paris 	if (length)
1023b77a493bSEric Paris 		goto out;
10241da177e4SLinus Torvalds 
10251da177e4SLinus Torvalds 	length = -ERANGE;
1026b77a493bSEric Paris 	if (len > SIMPLE_TRANSACTION_LIMIT)
1027b77a493bSEric Paris 		goto out;
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds 	memcpy(buf, newcon, len);
10301da177e4SLinus Torvalds 	length = len;
10311da177e4SLinus Torvalds out:
1032b77a493bSEric Paris 	kfree(newcon);
1033b77a493bSEric Paris 	kfree(tcon);
10341da177e4SLinus Torvalds 	kfree(scon);
10351da177e4SLinus Torvalds 	return length;
10361da177e4SLinus Torvalds }
10371da177e4SLinus Torvalds 
10381da177e4SLinus Torvalds static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
10391da177e4SLinus Torvalds {
10400619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
10410619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
1042b77a493bSEric Paris 	char *con = NULL, *user = NULL, *ptr;
1043b77a493bSEric Paris 	u32 sid, *sids = NULL;
10441da177e4SLinus Torvalds 	ssize_t length;
10451da177e4SLinus Torvalds 	char *newcon;
10461da177e4SLinus Torvalds 	int i, rc;
10471da177e4SLinus Torvalds 	u32 len, nsids;
10481da177e4SLinus Torvalds 
10496b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
10506b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
1051be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__COMPUTE_USER,
1052be0554c9SStephen Smalley 			      NULL);
10531da177e4SLinus Torvalds 	if (length)
10546eab04a8SJustin P. Mattock 		goto out;
10551da177e4SLinus Torvalds 
10561da177e4SLinus Torvalds 	length = -ENOMEM;
105789d155efSJames Morris 	con = kzalloc(size + 1, GFP_KERNEL);
10581da177e4SLinus Torvalds 	if (!con)
10596eab04a8SJustin P. Mattock 		goto out;
10601da177e4SLinus Torvalds 
1061b77a493bSEric Paris 	length = -ENOMEM;
106289d155efSJames Morris 	user = kzalloc(size + 1, GFP_KERNEL);
10631da177e4SLinus Torvalds 	if (!user)
10641da177e4SLinus Torvalds 		goto out;
10651da177e4SLinus Torvalds 
10661da177e4SLinus Torvalds 	length = -EINVAL;
10671da177e4SLinus Torvalds 	if (sscanf(buf, "%s %s", con, user) != 2)
1068b77a493bSEric Paris 		goto out;
10691da177e4SLinus Torvalds 
10700619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, con, &sid, GFP_KERNEL);
1071b77a493bSEric Paris 	if (length)
1072b77a493bSEric Paris 		goto out;
10731da177e4SLinus Torvalds 
10740619f0f5SStephen Smalley 	length = security_get_user_sids(state, sid, user, &sids, &nsids);
1075b77a493bSEric Paris 	if (length)
1076b77a493bSEric Paris 		goto out;
10771da177e4SLinus Torvalds 
10781da177e4SLinus Torvalds 	length = sprintf(buf, "%u", nsids) + 1;
10791da177e4SLinus Torvalds 	ptr = buf + length;
10801da177e4SLinus Torvalds 	for (i = 0; i < nsids; i++) {
10810619f0f5SStephen Smalley 		rc = security_sid_to_context(state, sids[i], &newcon, &len);
10821da177e4SLinus Torvalds 		if (rc) {
10831da177e4SLinus Torvalds 			length = rc;
1084b77a493bSEric Paris 			goto out;
10851da177e4SLinus Torvalds 		}
10861da177e4SLinus Torvalds 		if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
10871da177e4SLinus Torvalds 			kfree(newcon);
10881da177e4SLinus Torvalds 			length = -ERANGE;
1089b77a493bSEric Paris 			goto out;
10901da177e4SLinus Torvalds 		}
10911da177e4SLinus Torvalds 		memcpy(ptr, newcon, len);
10921da177e4SLinus Torvalds 		kfree(newcon);
10931da177e4SLinus Torvalds 		ptr += len;
10941da177e4SLinus Torvalds 		length += len;
10951da177e4SLinus Torvalds 	}
10961da177e4SLinus Torvalds out:
1097b77a493bSEric Paris 	kfree(sids);
1098b77a493bSEric Paris 	kfree(user);
10991da177e4SLinus Torvalds 	kfree(con);
11001da177e4SLinus Torvalds 	return length;
11011da177e4SLinus Torvalds }
11021da177e4SLinus Torvalds 
11031da177e4SLinus Torvalds static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
11041da177e4SLinus Torvalds {
11050619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
11060619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
1107b77a493bSEric Paris 	char *scon = NULL, *tcon = NULL;
11081da177e4SLinus Torvalds 	u32 ssid, tsid, newsid;
11091da177e4SLinus Torvalds 	u16 tclass;
11101da177e4SLinus Torvalds 	ssize_t length;
1111b77a493bSEric Paris 	char *newcon = NULL;
11121da177e4SLinus Torvalds 	u32 len;
11131da177e4SLinus Torvalds 
11146b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
11156b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
1116be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__COMPUTE_MEMBER,
1117be0554c9SStephen Smalley 			      NULL);
11181da177e4SLinus Torvalds 	if (length)
1119b77a493bSEric Paris 		goto out;
11201da177e4SLinus Torvalds 
11211da177e4SLinus Torvalds 	length = -ENOMEM;
112289d155efSJames Morris 	scon = kzalloc(size + 1, GFP_KERNEL);
11231da177e4SLinus Torvalds 	if (!scon)
11246eab04a8SJustin P. Mattock 		goto out;
11251da177e4SLinus Torvalds 
1126b77a493bSEric Paris 	length = -ENOMEM;
112789d155efSJames Morris 	tcon = kzalloc(size + 1, GFP_KERNEL);
11281da177e4SLinus Torvalds 	if (!tcon)
11291da177e4SLinus Torvalds 		goto out;
11301da177e4SLinus Torvalds 
11311da177e4SLinus Torvalds 	length = -EINVAL;
11321da177e4SLinus Torvalds 	if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
1133b77a493bSEric Paris 		goto out;
11341da177e4SLinus Torvalds 
11350619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
1136b77a493bSEric Paris 	if (length)
1137b77a493bSEric Paris 		goto out;
1138b77a493bSEric Paris 
11390619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
1140b77a493bSEric Paris 	if (length)
1141b77a493bSEric Paris 		goto out;
11421da177e4SLinus Torvalds 
11430619f0f5SStephen Smalley 	length = security_member_sid(state, ssid, tsid, tclass, &newsid);
1144b77a493bSEric Paris 	if (length)
1145b77a493bSEric Paris 		goto out;
11461da177e4SLinus Torvalds 
11470619f0f5SStephen Smalley 	length = security_sid_to_context(state, newsid, &newcon, &len);
1148b77a493bSEric Paris 	if (length)
1149b77a493bSEric Paris 		goto out;
11501da177e4SLinus Torvalds 
1151b77a493bSEric Paris 	length = -ERANGE;
11521da177e4SLinus Torvalds 	if (len > SIMPLE_TRANSACTION_LIMIT) {
1153f8b69a5fSpeter enderborg 		pr_err("SELinux: %s:  context size (%u) exceeds "
1154744ba35eSEric Paris 			"payload max\n", __func__, len);
1155b77a493bSEric Paris 		goto out;
11561da177e4SLinus Torvalds 	}
11571da177e4SLinus Torvalds 
11581da177e4SLinus Torvalds 	memcpy(buf, newcon, len);
11591da177e4SLinus Torvalds 	length = len;
11601da177e4SLinus Torvalds out:
1161b77a493bSEric Paris 	kfree(newcon);
1162b77a493bSEric Paris 	kfree(tcon);
11631da177e4SLinus Torvalds 	kfree(scon);
11641da177e4SLinus Torvalds 	return length;
11651da177e4SLinus Torvalds }
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds static struct inode *sel_make_inode(struct super_block *sb, int mode)
11681da177e4SLinus Torvalds {
11691da177e4SLinus Torvalds 	struct inode *ret = new_inode(sb);
11701da177e4SLinus Torvalds 
11711da177e4SLinus Torvalds 	if (ret) {
11721da177e4SLinus Torvalds 		ret->i_mode = mode;
1173078cd827SDeepa Dinamani 		ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret);
11741da177e4SLinus Torvalds 	}
11751da177e4SLinus Torvalds 	return ret;
11761da177e4SLinus Torvalds }
11771da177e4SLinus Torvalds 
11781da177e4SLinus Torvalds static ssize_t sel_read_bool(struct file *filep, char __user *buf,
11791da177e4SLinus Torvalds 			     size_t count, loff_t *ppos)
11801da177e4SLinus Torvalds {
11810619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
11821da177e4SLinus Torvalds 	char *page = NULL;
11831da177e4SLinus Torvalds 	ssize_t length;
11841da177e4SLinus Torvalds 	ssize_t ret;
11851da177e4SLinus Torvalds 	int cur_enforcing;
1186496ad9aaSAl Viro 	unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
1187d313f948SStephen Smalley 	const char *name = filep->f_path.dentry->d_name.name;
11881da177e4SLinus Torvalds 
11890619f0f5SStephen Smalley 	mutex_lock(&fsi->mutex);
11901da177e4SLinus Torvalds 
1191d313f948SStephen Smalley 	ret = -EINVAL;
11920619f0f5SStephen Smalley 	if (index >= fsi->bool_num || strcmp(name,
11930619f0f5SStephen Smalley 					     fsi->bool_pending_names[index]))
11940da74120SJann Horn 		goto out_unlock;
11951da177e4SLinus Torvalds 
11961da177e4SLinus Torvalds 	ret = -ENOMEM;
1197b77a493bSEric Paris 	page = (char *)get_zeroed_page(GFP_KERNEL);
1198b77a493bSEric Paris 	if (!page)
11990da74120SJann Horn 		goto out_unlock;
12001da177e4SLinus Torvalds 
12010619f0f5SStephen Smalley 	cur_enforcing = security_get_bool_value(fsi->state, index);
12021da177e4SLinus Torvalds 	if (cur_enforcing < 0) {
12031da177e4SLinus Torvalds 		ret = cur_enforcing;
12040da74120SJann Horn 		goto out_unlock;
12051da177e4SLinus Torvalds 	}
12061da177e4SLinus Torvalds 	length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
12070619f0f5SStephen Smalley 			  fsi->bool_pending_values[index]);
12080619f0f5SStephen Smalley 	mutex_unlock(&fsi->mutex);
12090da74120SJann Horn 	ret = simple_read_from_buffer(buf, count, ppos, page, length);
12100da74120SJann Horn out_free:
12111da177e4SLinus Torvalds 	free_page((unsigned long)page);
12121da177e4SLinus Torvalds 	return ret;
12130da74120SJann Horn 
12140da74120SJann Horn out_unlock:
12150da74120SJann Horn 	mutex_unlock(&fsi->mutex);
12160da74120SJann Horn 	goto out_free;
12171da177e4SLinus Torvalds }
12181da177e4SLinus Torvalds 
12191da177e4SLinus Torvalds static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
12201da177e4SLinus Torvalds 			      size_t count, loff_t *ppos)
12211da177e4SLinus Torvalds {
12220619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
12231da177e4SLinus Torvalds 	char *page = NULL;
1224d313f948SStephen Smalley 	ssize_t length;
12251da177e4SLinus Torvalds 	int new_value;
1226496ad9aaSAl Viro 	unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
1227d313f948SStephen Smalley 	const char *name = filep->f_path.dentry->d_name.name;
12281da177e4SLinus Torvalds 
12290da74120SJann Horn 	if (count >= PAGE_SIZE)
12300da74120SJann Horn 		return -ENOMEM;
12310da74120SJann Horn 
12320da74120SJann Horn 	/* No partial writes. */
12330da74120SJann Horn 	if (*ppos != 0)
12340da74120SJann Horn 		return -EINVAL;
12350da74120SJann Horn 
12360da74120SJann Horn 	page = memdup_user_nul(buf, count);
12370da74120SJann Horn 	if (IS_ERR(page))
12380da74120SJann Horn 		return PTR_ERR(page);
12390da74120SJann Horn 
12400619f0f5SStephen Smalley 	mutex_lock(&fsi->mutex);
12411da177e4SLinus Torvalds 
12426b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
12436b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
1244be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__SETBOOL,
1245be0554c9SStephen Smalley 			      NULL);
12461da177e4SLinus Torvalds 	if (length)
12471da177e4SLinus Torvalds 		goto out;
12481da177e4SLinus Torvalds 
1249d313f948SStephen Smalley 	length = -EINVAL;
12500619f0f5SStephen Smalley 	if (index >= fsi->bool_num || strcmp(name,
12510619f0f5SStephen Smalley 					     fsi->bool_pending_names[index]))
1252d313f948SStephen Smalley 		goto out;
1253d313f948SStephen Smalley 
12541da177e4SLinus Torvalds 	length = -EINVAL;
12551da177e4SLinus Torvalds 	if (sscanf(page, "%d", &new_value) != 1)
12561da177e4SLinus Torvalds 		goto out;
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds 	if (new_value)
12591da177e4SLinus Torvalds 		new_value = 1;
12601da177e4SLinus Torvalds 
12610619f0f5SStephen Smalley 	fsi->bool_pending_values[index] = new_value;
12621da177e4SLinus Torvalds 	length = count;
12631da177e4SLinus Torvalds 
12641da177e4SLinus Torvalds out:
12650619f0f5SStephen Smalley 	mutex_unlock(&fsi->mutex);
12668365a719SAl Viro 	kfree(page);
12671da177e4SLinus Torvalds 	return length;
12681da177e4SLinus Torvalds }
12691da177e4SLinus Torvalds 
12709c2e08c5SArjan van de Ven static const struct file_operations sel_bool_ops = {
12711da177e4SLinus Torvalds 	.read		= sel_read_bool,
12721da177e4SLinus Torvalds 	.write		= sel_write_bool,
127357a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
12741da177e4SLinus Torvalds };
12751da177e4SLinus Torvalds 
12761da177e4SLinus Torvalds static ssize_t sel_commit_bools_write(struct file *filep,
12771da177e4SLinus Torvalds 				      const char __user *buf,
12781da177e4SLinus Torvalds 				      size_t count, loff_t *ppos)
12791da177e4SLinus Torvalds {
12800619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
12811da177e4SLinus Torvalds 	char *page = NULL;
1282d313f948SStephen Smalley 	ssize_t length;
12831da177e4SLinus Torvalds 	int new_value;
12841da177e4SLinus Torvalds 
12850da74120SJann Horn 	if (count >= PAGE_SIZE)
12860da74120SJann Horn 		return -ENOMEM;
12870da74120SJann Horn 
12880da74120SJann Horn 	/* No partial writes. */
12890da74120SJann Horn 	if (*ppos != 0)
12900da74120SJann Horn 		return -EINVAL;
12910da74120SJann Horn 
12920da74120SJann Horn 	page = memdup_user_nul(buf, count);
12930da74120SJann Horn 	if (IS_ERR(page))
12940da74120SJann Horn 		return PTR_ERR(page);
12950da74120SJann Horn 
12960619f0f5SStephen Smalley 	mutex_lock(&fsi->mutex);
12971da177e4SLinus Torvalds 
12986b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
12996b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
1300be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__SETBOOL,
1301be0554c9SStephen Smalley 			      NULL);
13021da177e4SLinus Torvalds 	if (length)
13031da177e4SLinus Torvalds 		goto out;
13041da177e4SLinus Torvalds 
13051da177e4SLinus Torvalds 	length = -EINVAL;
13061da177e4SLinus Torvalds 	if (sscanf(page, "%d", &new_value) != 1)
13071da177e4SLinus Torvalds 		goto out;
13081da177e4SLinus Torvalds 
1309b77a493bSEric Paris 	length = 0;
13100619f0f5SStephen Smalley 	if (new_value && fsi->bool_pending_values)
13110619f0f5SStephen Smalley 		length = security_set_bools(fsi->state, fsi->bool_num,
13120619f0f5SStephen Smalley 					    fsi->bool_pending_values);
13131da177e4SLinus Torvalds 
1314b77a493bSEric Paris 	if (!length)
13151da177e4SLinus Torvalds 		length = count;
13161da177e4SLinus Torvalds 
13171da177e4SLinus Torvalds out:
13180619f0f5SStephen Smalley 	mutex_unlock(&fsi->mutex);
13198365a719SAl Viro 	kfree(page);
13201da177e4SLinus Torvalds 	return length;
13211da177e4SLinus Torvalds }
13221da177e4SLinus Torvalds 
13239c2e08c5SArjan van de Ven static const struct file_operations sel_commit_bools_ops = {
13241da177e4SLinus Torvalds 	.write		= sel_commit_bools_write,
132557a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
13261da177e4SLinus Torvalds };
13271da177e4SLinus Torvalds 
13280c92d7c7SChristopher J. PeBenito static void sel_remove_entries(struct dentry *de)
13291da177e4SLinus Torvalds {
1330ad52184bSAl Viro 	d_genocide(de);
1331ad52184bSAl Viro 	shrink_dcache_parent(de);
13321da177e4SLinus Torvalds }
13331da177e4SLinus Torvalds 
13341da177e4SLinus Torvalds #define BOOL_DIR_NAME "booleans"
13351da177e4SLinus Torvalds 
13360619f0f5SStephen Smalley static int sel_make_bools(struct selinux_fs_info *fsi)
13371da177e4SLinus Torvalds {
133860abd318SOndrej Mosnacek 	int ret;
13391da177e4SLinus Torvalds 	ssize_t len;
13401da177e4SLinus Torvalds 	struct dentry *dentry = NULL;
13410619f0f5SStephen Smalley 	struct dentry *dir = fsi->bool_dir;
13421da177e4SLinus Torvalds 	struct inode *inode = NULL;
13431da177e4SLinus Torvalds 	struct inode_security_struct *isec;
13441da177e4SLinus Torvalds 	char **names = NULL, *page;
134560abd318SOndrej Mosnacek 	u32 i, num;
13461da177e4SLinus Torvalds 	int *values = NULL;
13471da177e4SLinus Torvalds 	u32 sid;
13481da177e4SLinus Torvalds 
13491da177e4SLinus Torvalds 	/* remove any existing files */
13500619f0f5SStephen Smalley 	for (i = 0; i < fsi->bool_num; i++)
13510619f0f5SStephen Smalley 		kfree(fsi->bool_pending_names[i]);
13520619f0f5SStephen Smalley 	kfree(fsi->bool_pending_names);
13530619f0f5SStephen Smalley 	kfree(fsi->bool_pending_values);
13540619f0f5SStephen Smalley 	fsi->bool_num = 0;
13550619f0f5SStephen Smalley 	fsi->bool_pending_names = NULL;
13560619f0f5SStephen Smalley 	fsi->bool_pending_values = NULL;
13571da177e4SLinus Torvalds 
13580c92d7c7SChristopher J. PeBenito 	sel_remove_entries(dir);
13591da177e4SLinus Torvalds 
1360b77a493bSEric Paris 	ret = -ENOMEM;
13611872981bSEric Paris 	page = (char *)get_zeroed_page(GFP_KERNEL);
13621872981bSEric Paris 	if (!page)
1363b77a493bSEric Paris 		goto out;
13641da177e4SLinus Torvalds 
13650619f0f5SStephen Smalley 	ret = security_get_bools(fsi->state, &num, &names, &values);
1366b77a493bSEric Paris 	if (ret)
13671da177e4SLinus Torvalds 		goto out;
13681da177e4SLinus Torvalds 
13691da177e4SLinus Torvalds 	for (i = 0; i < num; i++) {
1370b77a493bSEric Paris 		ret = -ENOMEM;
13711da177e4SLinus Torvalds 		dentry = d_alloc_name(dir, names[i]);
1372b77a493bSEric Paris 		if (!dentry)
1373b77a493bSEric Paris 			goto out;
13741da177e4SLinus Torvalds 
1375b77a493bSEric Paris 		ret = -ENOMEM;
1376b77a493bSEric Paris 		inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
13777e4237faSnixiaoming 		if (!inode) {
13787e4237faSnixiaoming 			dput(dentry);
1379b77a493bSEric Paris 			goto out;
13807e4237faSnixiaoming 		}
1381b77a493bSEric Paris 
13821da177e4SLinus Torvalds 		ret = -ENAMETOOLONG;
1383cc1dad71SAl Viro 		len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
13847e4237faSnixiaoming 		if (len >= PAGE_SIZE) {
13857e4237faSnixiaoming 			dput(dentry);
13867e4237faSnixiaoming 			iput(inode);
1387b77a493bSEric Paris 			goto out;
13887e4237faSnixiaoming 		}
1389b77a493bSEric Paris 
139080788c22SCasey Schaufler 		isec = selinux_inode(inode);
13910619f0f5SStephen Smalley 		ret = security_genfs_sid(fsi->state, "selinuxfs", page,
1392aa8e712cSStephen Smalley 					 SECCLASS_FILE, &sid);
13934262fb51SGary Tierney 		if (ret) {
1394900fde06SGary Tierney 			pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n",
1395900fde06SGary Tierney 					   page);
1396900fde06SGary Tierney 			sid = SECINITSID_SECURITY;
13974262fb51SGary Tierney 		}
13984262fb51SGary Tierney 
13991da177e4SLinus Torvalds 		isec->sid = sid;
140042059112SAndreas Gruenbacher 		isec->initialized = LABEL_INITIALIZED;
14011da177e4SLinus Torvalds 		inode->i_fop = &sel_bool_ops;
1402bce34bc0SJames Carter 		inode->i_ino = i|SEL_BOOL_INO_OFFSET;
14031da177e4SLinus Torvalds 		d_add(dentry, inode);
14041da177e4SLinus Torvalds 	}
14050619f0f5SStephen Smalley 	fsi->bool_num = num;
14060619f0f5SStephen Smalley 	fsi->bool_pending_names = names;
14070619f0f5SStephen Smalley 	fsi->bool_pending_values = values;
1408b77a493bSEric Paris 
1409b77a493bSEric Paris 	free_page((unsigned long)page);
1410b77a493bSEric Paris 	return 0;
14111da177e4SLinus Torvalds out:
14121da177e4SLinus Torvalds 	free_page((unsigned long)page);
1413b77a493bSEric Paris 
14141da177e4SLinus Torvalds 	if (names) {
14159a5f04bfSJesper Juhl 		for (i = 0; i < num; i++)
14161da177e4SLinus Torvalds 			kfree(names[i]);
14171da177e4SLinus Torvalds 		kfree(names);
14181da177e4SLinus Torvalds 	}
141920c19e41SDavi Arnaut 	kfree(values);
14200c92d7c7SChristopher J. PeBenito 	sel_remove_entries(dir);
1421b77a493bSEric Paris 
1422b77a493bSEric Paris 	return ret;
14231da177e4SLinus Torvalds }
14241da177e4SLinus Torvalds 
14251da177e4SLinus Torvalds static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
14261da177e4SLinus Torvalds 					    size_t count, loff_t *ppos)
14271da177e4SLinus Torvalds {
14286b6bc620SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
14296b6bc620SStephen Smalley 	struct selinux_state *state = fsi->state;
14301da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
14311da177e4SLinus Torvalds 	ssize_t length;
14321da177e4SLinus Torvalds 
14336b6bc620SStephen Smalley 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
14346b6bc620SStephen Smalley 			   avc_get_cache_threshold(state->avc));
14351da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
14361da177e4SLinus Torvalds }
14371da177e4SLinus Torvalds 
14381da177e4SLinus Torvalds static ssize_t sel_write_avc_cache_threshold(struct file *file,
14391da177e4SLinus Torvalds 					     const char __user *buf,
14401da177e4SLinus Torvalds 					     size_t count, loff_t *ppos)
14411da177e4SLinus Torvalds 
14421da177e4SLinus Torvalds {
14436b6bc620SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
14446b6bc620SStephen Smalley 	struct selinux_state *state = fsi->state;
14458365a719SAl Viro 	char *page;
14461da177e4SLinus Torvalds 	ssize_t ret;
1447309c5fadSHeinrich Schuchardt 	unsigned int new_value;
14481da177e4SLinus Torvalds 
14496b6bc620SStephen Smalley 	ret = avc_has_perm(&selinux_state,
14506b6bc620SStephen Smalley 			   current_sid(), SECINITSID_SECURITY,
1451be0554c9SStephen Smalley 			   SECCLASS_SECURITY, SECURITY__SETSECPARAM,
1452be0554c9SStephen Smalley 			   NULL);
14531da177e4SLinus Torvalds 	if (ret)
14548365a719SAl Viro 		return ret;
1455b77a493bSEric Paris 
1456b77a493bSEric Paris 	if (count >= PAGE_SIZE)
14578365a719SAl Viro 		return -ENOMEM;
1458b77a493bSEric Paris 
1459b77a493bSEric Paris 	/* No partial writes. */
1460b77a493bSEric Paris 	if (*ppos != 0)
14618365a719SAl Viro 		return -EINVAL;
1462b77a493bSEric Paris 
14638365a719SAl Viro 	page = memdup_user_nul(buf, count);
14648365a719SAl Viro 	if (IS_ERR(page))
14658365a719SAl Viro 		return PTR_ERR(page);
1466b77a493bSEric Paris 
1467b77a493bSEric Paris 	ret = -EINVAL;
1468b77a493bSEric Paris 	if (sscanf(page, "%u", &new_value) != 1)
1469b77a493bSEric Paris 		goto out;
1470b77a493bSEric Paris 
14716b6bc620SStephen Smalley 	avc_set_cache_threshold(state->avc, new_value);
1472b77a493bSEric Paris 
14731da177e4SLinus Torvalds 	ret = count;
14741da177e4SLinus Torvalds out:
14758365a719SAl Viro 	kfree(page);
14761da177e4SLinus Torvalds 	return ret;
14771da177e4SLinus Torvalds }
14781da177e4SLinus Torvalds 
14791da177e4SLinus Torvalds static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
14801da177e4SLinus Torvalds 				       size_t count, loff_t *ppos)
14811da177e4SLinus Torvalds {
14826b6bc620SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
14836b6bc620SStephen Smalley 	struct selinux_state *state = fsi->state;
14841da177e4SLinus Torvalds 	char *page;
1485b77a493bSEric Paris 	ssize_t length;
14861da177e4SLinus Torvalds 
14871da177e4SLinus Torvalds 	page = (char *)__get_free_page(GFP_KERNEL);
1488b77a493bSEric Paris 	if (!page)
1489b77a493bSEric Paris 		return -ENOMEM;
1490b77a493bSEric Paris 
14916b6bc620SStephen Smalley 	length = avc_get_hash_stats(state->avc, page);
1492b77a493bSEric Paris 	if (length >= 0)
1493b77a493bSEric Paris 		length = simple_read_from_buffer(buf, count, ppos, page, length);
14941da177e4SLinus Torvalds 	free_page((unsigned long)page);
1495b77a493bSEric Paris 
1496b77a493bSEric Paris 	return length;
14971da177e4SLinus Torvalds }
14981da177e4SLinus Torvalds 
149966f8e2f0SJeff Vander Stoep static ssize_t sel_read_sidtab_hash_stats(struct file *filp, char __user *buf,
150066f8e2f0SJeff Vander Stoep 					size_t count, loff_t *ppos)
150166f8e2f0SJeff Vander Stoep {
150266f8e2f0SJeff Vander Stoep 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
150366f8e2f0SJeff Vander Stoep 	struct selinux_state *state = fsi->state;
150466f8e2f0SJeff Vander Stoep 	char *page;
150566f8e2f0SJeff Vander Stoep 	ssize_t length;
150666f8e2f0SJeff Vander Stoep 
150766f8e2f0SJeff Vander Stoep 	page = (char *)__get_free_page(GFP_KERNEL);
150866f8e2f0SJeff Vander Stoep 	if (!page)
150966f8e2f0SJeff Vander Stoep 		return -ENOMEM;
151066f8e2f0SJeff Vander Stoep 
151166f8e2f0SJeff Vander Stoep 	length = security_sidtab_hash_stats(state, page);
151266f8e2f0SJeff Vander Stoep 	if (length >= 0)
151366f8e2f0SJeff Vander Stoep 		length = simple_read_from_buffer(buf, count, ppos, page,
151466f8e2f0SJeff Vander Stoep 						length);
151566f8e2f0SJeff Vander Stoep 	free_page((unsigned long)page);
151666f8e2f0SJeff Vander Stoep 
151766f8e2f0SJeff Vander Stoep 	return length;
151866f8e2f0SJeff Vander Stoep }
151966f8e2f0SJeff Vander Stoep 
152066f8e2f0SJeff Vander Stoep static const struct file_operations sel_sidtab_hash_stats_ops = {
152166f8e2f0SJeff Vander Stoep 	.read		= sel_read_sidtab_hash_stats,
152266f8e2f0SJeff Vander Stoep 	.llseek		= generic_file_llseek,
152366f8e2f0SJeff Vander Stoep };
152466f8e2f0SJeff Vander Stoep 
15259c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_threshold_ops = {
15261da177e4SLinus Torvalds 	.read		= sel_read_avc_cache_threshold,
15271da177e4SLinus Torvalds 	.write		= sel_write_avc_cache_threshold,
152857a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
15291da177e4SLinus Torvalds };
15301da177e4SLinus Torvalds 
15319c2e08c5SArjan van de Ven static const struct file_operations sel_avc_hash_stats_ops = {
15321da177e4SLinus Torvalds 	.read		= sel_read_avc_hash_stats,
153357a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
15341da177e4SLinus Torvalds };
15351da177e4SLinus Torvalds 
15361da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
15371da177e4SLinus Torvalds static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
15381da177e4SLinus Torvalds {
15391da177e4SLinus Torvalds 	int cpu;
15401da177e4SLinus Torvalds 
15414f4b6c1aSRusty Russell 	for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
15421da177e4SLinus Torvalds 		if (!cpu_possible(cpu))
15431da177e4SLinus Torvalds 			continue;
15441da177e4SLinus Torvalds 		*idx = cpu + 1;
15451da177e4SLinus Torvalds 		return &per_cpu(avc_cache_stats, cpu);
15461da177e4SLinus Torvalds 	}
15478d269a8eSVasily Averin 	(*idx)++;
15481da177e4SLinus Torvalds 	return NULL;
15491da177e4SLinus Torvalds }
15501da177e4SLinus Torvalds 
15511da177e4SLinus Torvalds static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
15521da177e4SLinus Torvalds {
15531da177e4SLinus Torvalds 	loff_t n = *pos - 1;
15541da177e4SLinus Torvalds 
15551da177e4SLinus Torvalds 	if (*pos == 0)
15561da177e4SLinus Torvalds 		return SEQ_START_TOKEN;
15571da177e4SLinus Torvalds 
15581da177e4SLinus Torvalds 	return sel_avc_get_stat_idx(&n);
15591da177e4SLinus Torvalds }
15601da177e4SLinus Torvalds 
15611da177e4SLinus Torvalds static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
15621da177e4SLinus Torvalds {
15631da177e4SLinus Torvalds 	return sel_avc_get_stat_idx(pos);
15641da177e4SLinus Torvalds }
15651da177e4SLinus Torvalds 
15661da177e4SLinus Torvalds static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
15671da177e4SLinus Torvalds {
15681da177e4SLinus Torvalds 	struct avc_cache_stats *st = v;
15691da177e4SLinus Torvalds 
1570710a0647SMarkus Elfring 	if (v == SEQ_START_TOKEN) {
1571710a0647SMarkus Elfring 		seq_puts(seq,
1572710a0647SMarkus Elfring 			 "lookups hits misses allocations reclaims frees\n");
1573710a0647SMarkus Elfring 	} else {
1574257313b2SLinus Torvalds 		unsigned int lookups = st->lookups;
1575257313b2SLinus Torvalds 		unsigned int misses = st->misses;
1576257313b2SLinus Torvalds 		unsigned int hits = lookups - misses;
1577257313b2SLinus Torvalds 		seq_printf(seq, "%u %u %u %u %u %u\n", lookups,
1578257313b2SLinus Torvalds 			   hits, misses, st->allocations,
15791da177e4SLinus Torvalds 			   st->reclaims, st->frees);
1580257313b2SLinus Torvalds 	}
15811da177e4SLinus Torvalds 	return 0;
15821da177e4SLinus Torvalds }
15831da177e4SLinus Torvalds 
15841da177e4SLinus Torvalds static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
15851da177e4SLinus Torvalds { }
15861da177e4SLinus Torvalds 
15871996a109SJan Engelhardt static const struct seq_operations sel_avc_cache_stats_seq_ops = {
15881da177e4SLinus Torvalds 	.start		= sel_avc_stats_seq_start,
15891da177e4SLinus Torvalds 	.next		= sel_avc_stats_seq_next,
15901da177e4SLinus Torvalds 	.show		= sel_avc_stats_seq_show,
15911da177e4SLinus Torvalds 	.stop		= sel_avc_stats_seq_stop,
15921da177e4SLinus Torvalds };
15931da177e4SLinus Torvalds 
15941da177e4SLinus Torvalds static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
15951da177e4SLinus Torvalds {
15961da177e4SLinus Torvalds 	return seq_open(file, &sel_avc_cache_stats_seq_ops);
15971da177e4SLinus Torvalds }
15981da177e4SLinus Torvalds 
15999c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_stats_ops = {
16001da177e4SLinus Torvalds 	.open		= sel_open_avc_cache_stats,
16011da177e4SLinus Torvalds 	.read		= seq_read,
16021da177e4SLinus Torvalds 	.llseek		= seq_lseek,
16031da177e4SLinus Torvalds 	.release	= seq_release,
16041da177e4SLinus Torvalds };
16051da177e4SLinus Torvalds #endif
16061da177e4SLinus Torvalds 
16071da177e4SLinus Torvalds static int sel_make_avc_files(struct dentry *dir)
16081da177e4SLinus Torvalds {
16090619f0f5SStephen Smalley 	struct super_block *sb = dir->d_sb;
16100619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = sb->s_fs_info;
1611b77a493bSEric Paris 	int i;
1612cda37124SEric Biggers 	static const struct tree_descr files[] = {
16131da177e4SLinus Torvalds 		{ "cache_threshold",
16141da177e4SLinus Torvalds 		  &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
16151da177e4SLinus Torvalds 		{ "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
16161da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
16171da177e4SLinus Torvalds 		{ "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
16181da177e4SLinus Torvalds #endif
16191da177e4SLinus Torvalds 	};
16201da177e4SLinus Torvalds 
16216e20a64aSNicolas Kaiser 	for (i = 0; i < ARRAY_SIZE(files); i++) {
16221da177e4SLinus Torvalds 		struct inode *inode;
16231da177e4SLinus Torvalds 		struct dentry *dentry;
16241da177e4SLinus Torvalds 
16251da177e4SLinus Torvalds 		dentry = d_alloc_name(dir, files[i].name);
1626b77a493bSEric Paris 		if (!dentry)
1627b77a493bSEric Paris 			return -ENOMEM;
16281da177e4SLinus Torvalds 
16291da177e4SLinus Torvalds 		inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
16307e4237faSnixiaoming 		if (!inode) {
16317e4237faSnixiaoming 			dput(dentry);
1632b77a493bSEric Paris 			return -ENOMEM;
16337e4237faSnixiaoming 		}
1634b77a493bSEric Paris 
16351da177e4SLinus Torvalds 		inode->i_fop = files[i].ops;
16360619f0f5SStephen Smalley 		inode->i_ino = ++fsi->last_ino;
16371da177e4SLinus Torvalds 		d_add(dentry, inode);
16381da177e4SLinus Torvalds 	}
1639b77a493bSEric Paris 
1640b77a493bSEric Paris 	return 0;
16411da177e4SLinus Torvalds }
16421da177e4SLinus Torvalds 
164366f8e2f0SJeff Vander Stoep static int sel_make_ss_files(struct dentry *dir)
164466f8e2f0SJeff Vander Stoep {
164566f8e2f0SJeff Vander Stoep 	struct super_block *sb = dir->d_sb;
164666f8e2f0SJeff Vander Stoep 	struct selinux_fs_info *fsi = sb->s_fs_info;
164766f8e2f0SJeff Vander Stoep 	int i;
164866f8e2f0SJeff Vander Stoep 	static struct tree_descr files[] = {
164966f8e2f0SJeff Vander Stoep 		{ "sidtab_hash_stats", &sel_sidtab_hash_stats_ops, S_IRUGO },
165066f8e2f0SJeff Vander Stoep 	};
165166f8e2f0SJeff Vander Stoep 
165266f8e2f0SJeff Vander Stoep 	for (i = 0; i < ARRAY_SIZE(files); i++) {
165366f8e2f0SJeff Vander Stoep 		struct inode *inode;
165466f8e2f0SJeff Vander Stoep 		struct dentry *dentry;
165566f8e2f0SJeff Vander Stoep 
165666f8e2f0SJeff Vander Stoep 		dentry = d_alloc_name(dir, files[i].name);
165766f8e2f0SJeff Vander Stoep 		if (!dentry)
165866f8e2f0SJeff Vander Stoep 			return -ENOMEM;
165966f8e2f0SJeff Vander Stoep 
166066f8e2f0SJeff Vander Stoep 		inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
166166f8e2f0SJeff Vander Stoep 		if (!inode) {
166266f8e2f0SJeff Vander Stoep 			dput(dentry);
166366f8e2f0SJeff Vander Stoep 			return -ENOMEM;
166466f8e2f0SJeff Vander Stoep 		}
166566f8e2f0SJeff Vander Stoep 
166666f8e2f0SJeff Vander Stoep 		inode->i_fop = files[i].ops;
166766f8e2f0SJeff Vander Stoep 		inode->i_ino = ++fsi->last_ino;
166866f8e2f0SJeff Vander Stoep 		d_add(dentry, inode);
166966f8e2f0SJeff Vander Stoep 	}
167066f8e2f0SJeff Vander Stoep 
167166f8e2f0SJeff Vander Stoep 	return 0;
167266f8e2f0SJeff Vander Stoep }
167366f8e2f0SJeff Vander Stoep 
1674f0ee2e46SJames Carter static ssize_t sel_read_initcon(struct file *file, char __user *buf,
1675f0ee2e46SJames Carter 				size_t count, loff_t *ppos)
1676f0ee2e46SJames Carter {
16770619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1678f0ee2e46SJames Carter 	char *con;
1679f0ee2e46SJames Carter 	u32 sid, len;
1680f0ee2e46SJames Carter 	ssize_t ret;
1681f0ee2e46SJames Carter 
1682496ad9aaSAl Viro 	sid = file_inode(file)->i_ino&SEL_INO_MASK;
16830619f0f5SStephen Smalley 	ret = security_sid_to_context(fsi->state, sid, &con, &len);
1684b77a493bSEric Paris 	if (ret)
1685f0ee2e46SJames Carter 		return ret;
1686f0ee2e46SJames Carter 
1687f0ee2e46SJames Carter 	ret = simple_read_from_buffer(buf, count, ppos, con, len);
1688f0ee2e46SJames Carter 	kfree(con);
1689f0ee2e46SJames Carter 	return ret;
1690f0ee2e46SJames Carter }
1691f0ee2e46SJames Carter 
1692f0ee2e46SJames Carter static const struct file_operations sel_initcon_ops = {
1693f0ee2e46SJames Carter 	.read		= sel_read_initcon,
169457a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
1695f0ee2e46SJames Carter };
1696f0ee2e46SJames Carter 
1697f0ee2e46SJames Carter static int sel_make_initcon_files(struct dentry *dir)
1698f0ee2e46SJames Carter {
1699b77a493bSEric Paris 	int i;
1700f0ee2e46SJames Carter 
1701f0ee2e46SJames Carter 	for (i = 1; i <= SECINITSID_NUM; i++) {
1702f0ee2e46SJames Carter 		struct inode *inode;
1703f0ee2e46SJames Carter 		struct dentry *dentry;
1704e3e0b582SStephen Smalley 		const char *s = security_get_initial_sid_context(i);
1705e3e0b582SStephen Smalley 
1706e3e0b582SStephen Smalley 		if (!s)
1707e3e0b582SStephen Smalley 			continue;
1708e3e0b582SStephen Smalley 		dentry = d_alloc_name(dir, s);
1709b77a493bSEric Paris 		if (!dentry)
1710b77a493bSEric Paris 			return -ENOMEM;
1711f0ee2e46SJames Carter 
1712f0ee2e46SJames Carter 		inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
17137e4237faSnixiaoming 		if (!inode) {
17147e4237faSnixiaoming 			dput(dentry);
1715b77a493bSEric Paris 			return -ENOMEM;
17167e4237faSnixiaoming 		}
1717b77a493bSEric Paris 
1718f0ee2e46SJames Carter 		inode->i_fop = &sel_initcon_ops;
1719f0ee2e46SJames Carter 		inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1720f0ee2e46SJames Carter 		d_add(dentry, inode);
1721f0ee2e46SJames Carter 	}
1722b77a493bSEric Paris 
1723b77a493bSEric Paris 	return 0;
1724f0ee2e46SJames Carter }
1725f0ee2e46SJames Carter 
1726e47c8fc5SChristopher J. PeBenito static inline unsigned long sel_class_to_ino(u16 class)
1727e47c8fc5SChristopher J. PeBenito {
1728e47c8fc5SChristopher J. PeBenito 	return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1729e47c8fc5SChristopher J. PeBenito }
1730e47c8fc5SChristopher J. PeBenito 
1731e47c8fc5SChristopher J. PeBenito static inline u16 sel_ino_to_class(unsigned long ino)
1732e47c8fc5SChristopher J. PeBenito {
173392ae9e82SEric Paris 	return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1);
1734e47c8fc5SChristopher J. PeBenito }
1735e47c8fc5SChristopher J. PeBenito 
1736e47c8fc5SChristopher J. PeBenito static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1737e47c8fc5SChristopher J. PeBenito {
1738e47c8fc5SChristopher J. PeBenito 	return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1739e47c8fc5SChristopher J. PeBenito }
1740e47c8fc5SChristopher J. PeBenito 
1741e47c8fc5SChristopher J. PeBenito static inline u32 sel_ino_to_perm(unsigned long ino)
1742e47c8fc5SChristopher J. PeBenito {
1743e47c8fc5SChristopher J. PeBenito 	return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1744e47c8fc5SChristopher J. PeBenito }
1745e47c8fc5SChristopher J. PeBenito 
1746e47c8fc5SChristopher J. PeBenito static ssize_t sel_read_class(struct file *file, char __user *buf,
1747e47c8fc5SChristopher J. PeBenito 				size_t count, loff_t *ppos)
1748e47c8fc5SChristopher J. PeBenito {
1749496ad9aaSAl Viro 	unsigned long ino = file_inode(file)->i_ino;
1750cc1dad71SAl Viro 	char res[TMPBUFLEN];
17517e78c875Sliuyang34 	ssize_t len = scnprintf(res, sizeof(res), "%d", sel_ino_to_class(ino));
1752cc1dad71SAl Viro 	return simple_read_from_buffer(buf, count, ppos, res, len);
1753e47c8fc5SChristopher J. PeBenito }
1754e47c8fc5SChristopher J. PeBenito 
1755e47c8fc5SChristopher J. PeBenito static const struct file_operations sel_class_ops = {
1756e47c8fc5SChristopher J. PeBenito 	.read		= sel_read_class,
175757a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
1758e47c8fc5SChristopher J. PeBenito };
1759e47c8fc5SChristopher J. PeBenito 
1760e47c8fc5SChristopher J. PeBenito static ssize_t sel_read_perm(struct file *file, char __user *buf,
1761e47c8fc5SChristopher J. PeBenito 				size_t count, loff_t *ppos)
1762e47c8fc5SChristopher J. PeBenito {
1763496ad9aaSAl Viro 	unsigned long ino = file_inode(file)->i_ino;
1764cc1dad71SAl Viro 	char res[TMPBUFLEN];
17657e78c875Sliuyang34 	ssize_t len = scnprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino));
1766cc1dad71SAl Viro 	return simple_read_from_buffer(buf, count, ppos, res, len);
1767e47c8fc5SChristopher J. PeBenito }
1768e47c8fc5SChristopher J. PeBenito 
1769e47c8fc5SChristopher J. PeBenito static const struct file_operations sel_perm_ops = {
1770e47c8fc5SChristopher J. PeBenito 	.read		= sel_read_perm,
177157a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
1772e47c8fc5SChristopher J. PeBenito };
1773e47c8fc5SChristopher J. PeBenito 
17743bb56b25SPaul Moore static ssize_t sel_read_policycap(struct file *file, char __user *buf,
17753bb56b25SPaul Moore 				  size_t count, loff_t *ppos)
17763bb56b25SPaul Moore {
17770619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
17783bb56b25SPaul Moore 	int value;
17793bb56b25SPaul Moore 	char tmpbuf[TMPBUFLEN];
17803bb56b25SPaul Moore 	ssize_t length;
1781496ad9aaSAl Viro 	unsigned long i_ino = file_inode(file)->i_ino;
17823bb56b25SPaul Moore 
17830619f0f5SStephen Smalley 	value = security_policycap_supported(fsi->state, i_ino & SEL_INO_MASK);
17843bb56b25SPaul Moore 	length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
17853bb56b25SPaul Moore 
17863bb56b25SPaul Moore 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
17873bb56b25SPaul Moore }
17883bb56b25SPaul Moore 
17893bb56b25SPaul Moore static const struct file_operations sel_policycap_ops = {
17903bb56b25SPaul Moore 	.read		= sel_read_policycap,
179157a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
17923bb56b25SPaul Moore };
17933bb56b25SPaul Moore 
1794e47c8fc5SChristopher J. PeBenito static int sel_make_perm_files(char *objclass, int classvalue,
1795e47c8fc5SChristopher J. PeBenito 				struct dentry *dir)
1796e47c8fc5SChristopher J. PeBenito {
17970619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = dir->d_sb->s_fs_info;
1798b77a493bSEric Paris 	int i, rc, nperms;
1799e47c8fc5SChristopher J. PeBenito 	char **perms;
1800e47c8fc5SChristopher J. PeBenito 
18010619f0f5SStephen Smalley 	rc = security_get_permissions(fsi->state, objclass, &perms, &nperms);
1802e47c8fc5SChristopher J. PeBenito 	if (rc)
1803b77a493bSEric Paris 		return rc;
1804e47c8fc5SChristopher J. PeBenito 
1805e47c8fc5SChristopher J. PeBenito 	for (i = 0; i < nperms; i++) {
1806e47c8fc5SChristopher J. PeBenito 		struct inode *inode;
1807e47c8fc5SChristopher J. PeBenito 		struct dentry *dentry;
1808e47c8fc5SChristopher J. PeBenito 
1809b77a493bSEric Paris 		rc = -ENOMEM;
1810e47c8fc5SChristopher J. PeBenito 		dentry = d_alloc_name(dir, perms[i]);
1811b77a493bSEric Paris 		if (!dentry)
1812b77a493bSEric Paris 			goto out;
1813e47c8fc5SChristopher J. PeBenito 
1814e47c8fc5SChristopher J. PeBenito 		rc = -ENOMEM;
1815b77a493bSEric Paris 		inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
18167e4237faSnixiaoming 		if (!inode) {
18177e4237faSnixiaoming 			dput(dentry);
1818b77a493bSEric Paris 			goto out;
18197e4237faSnixiaoming 		}
1820b77a493bSEric Paris 
1821e47c8fc5SChristopher J. PeBenito 		inode->i_fop = &sel_perm_ops;
1822e47c8fc5SChristopher J. PeBenito 		/* i+1 since perm values are 1-indexed */
1823e47c8fc5SChristopher J. PeBenito 		inode->i_ino = sel_perm_to_ino(classvalue, i + 1);
1824e47c8fc5SChristopher J. PeBenito 		d_add(dentry, inode);
1825e47c8fc5SChristopher J. PeBenito 	}
1826b77a493bSEric Paris 	rc = 0;
1827b77a493bSEric Paris out:
1828e47c8fc5SChristopher J. PeBenito 	for (i = 0; i < nperms; i++)
1829e47c8fc5SChristopher J. PeBenito 		kfree(perms[i]);
1830e47c8fc5SChristopher J. PeBenito 	kfree(perms);
1831e47c8fc5SChristopher J. PeBenito 	return rc;
1832e47c8fc5SChristopher J. PeBenito }
1833e47c8fc5SChristopher J. PeBenito 
1834e47c8fc5SChristopher J. PeBenito static int sel_make_class_dir_entries(char *classname, int index,
1835e47c8fc5SChristopher J. PeBenito 					struct dentry *dir)
1836e47c8fc5SChristopher J. PeBenito {
18370619f0f5SStephen Smalley 	struct super_block *sb = dir->d_sb;
18380619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = sb->s_fs_info;
1839e47c8fc5SChristopher J. PeBenito 	struct dentry *dentry = NULL;
1840e47c8fc5SChristopher J. PeBenito 	struct inode *inode = NULL;
1841e47c8fc5SChristopher J. PeBenito 	int rc;
1842e47c8fc5SChristopher J. PeBenito 
1843e47c8fc5SChristopher J. PeBenito 	dentry = d_alloc_name(dir, "index");
1844b77a493bSEric Paris 	if (!dentry)
1845b77a493bSEric Paris 		return -ENOMEM;
1846e47c8fc5SChristopher J. PeBenito 
1847e47c8fc5SChristopher J. PeBenito 	inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
18487e4237faSnixiaoming 	if (!inode) {
18497e4237faSnixiaoming 		dput(dentry);
1850b77a493bSEric Paris 		return -ENOMEM;
18517e4237faSnixiaoming 	}
1852e47c8fc5SChristopher J. PeBenito 
1853e47c8fc5SChristopher J. PeBenito 	inode->i_fop = &sel_class_ops;
1854e47c8fc5SChristopher J. PeBenito 	inode->i_ino = sel_class_to_ino(index);
1855e47c8fc5SChristopher J. PeBenito 	d_add(dentry, inode);
1856e47c8fc5SChristopher J. PeBenito 
18570619f0f5SStephen Smalley 	dentry = sel_make_dir(dir, "perms", &fsi->last_class_ino);
1858a1c2aa1eSAl Viro 	if (IS_ERR(dentry))
1859a1c2aa1eSAl Viro 		return PTR_ERR(dentry);
1860e47c8fc5SChristopher J. PeBenito 
1861e47c8fc5SChristopher J. PeBenito 	rc = sel_make_perm_files(classname, index, dentry);
1862e47c8fc5SChristopher J. PeBenito 
1863e47c8fc5SChristopher J. PeBenito 	return rc;
1864e47c8fc5SChristopher J. PeBenito }
1865e47c8fc5SChristopher J. PeBenito 
18660619f0f5SStephen Smalley static int sel_make_classes(struct selinux_fs_info *fsi)
1867e47c8fc5SChristopher J. PeBenito {
18680619f0f5SStephen Smalley 
1869b77a493bSEric Paris 	int rc, nclasses, i;
1870e47c8fc5SChristopher J. PeBenito 	char **classes;
1871e47c8fc5SChristopher J. PeBenito 
1872e47c8fc5SChristopher J. PeBenito 	/* delete any existing entries */
18730619f0f5SStephen Smalley 	sel_remove_entries(fsi->class_dir);
1874e47c8fc5SChristopher J. PeBenito 
18750619f0f5SStephen Smalley 	rc = security_get_classes(fsi->state, &classes, &nclasses);
1876b77a493bSEric Paris 	if (rc)
1877b77a493bSEric Paris 		return rc;
1878e47c8fc5SChristopher J. PeBenito 
1879e47c8fc5SChristopher J. PeBenito 	/* +2 since classes are 1-indexed */
18800619f0f5SStephen Smalley 	fsi->last_class_ino = sel_class_to_ino(nclasses + 2);
1881e47c8fc5SChristopher J. PeBenito 
1882e47c8fc5SChristopher J. PeBenito 	for (i = 0; i < nclasses; i++) {
1883e47c8fc5SChristopher J. PeBenito 		struct dentry *class_name_dir;
1884e47c8fc5SChristopher J. PeBenito 
18850619f0f5SStephen Smalley 		class_name_dir = sel_make_dir(fsi->class_dir, classes[i],
18860619f0f5SStephen Smalley 					      &fsi->last_class_ino);
1887a1c2aa1eSAl Viro 		if (IS_ERR(class_name_dir)) {
1888a1c2aa1eSAl Viro 			rc = PTR_ERR(class_name_dir);
1889b77a493bSEric Paris 			goto out;
1890a1c2aa1eSAl Viro 		}
1891e47c8fc5SChristopher J. PeBenito 
1892e47c8fc5SChristopher J. PeBenito 		/* i+1 since class values are 1-indexed */
1893e47c8fc5SChristopher J. PeBenito 		rc = sel_make_class_dir_entries(classes[i], i + 1,
1894e47c8fc5SChristopher J. PeBenito 				class_name_dir);
1895e47c8fc5SChristopher J. PeBenito 		if (rc)
1896b77a493bSEric Paris 			goto out;
1897e47c8fc5SChristopher J. PeBenito 	}
1898b77a493bSEric Paris 	rc = 0;
1899b77a493bSEric Paris out:
1900e47c8fc5SChristopher J. PeBenito 	for (i = 0; i < nclasses; i++)
1901e47c8fc5SChristopher J. PeBenito 		kfree(classes[i]);
1902e47c8fc5SChristopher J. PeBenito 	kfree(classes);
1903e47c8fc5SChristopher J. PeBenito 	return rc;
1904e47c8fc5SChristopher J. PeBenito }
1905e47c8fc5SChristopher J. PeBenito 
19060619f0f5SStephen Smalley static int sel_make_policycap(struct selinux_fs_info *fsi)
19073bb56b25SPaul Moore {
19083bb56b25SPaul Moore 	unsigned int iter;
19093bb56b25SPaul Moore 	struct dentry *dentry = NULL;
19103bb56b25SPaul Moore 	struct inode *inode = NULL;
19113bb56b25SPaul Moore 
19120619f0f5SStephen Smalley 	sel_remove_entries(fsi->policycap_dir);
19133bb56b25SPaul Moore 
19143bb56b25SPaul Moore 	for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
19154dc2fce3SStephen Smalley 		if (iter < ARRAY_SIZE(selinux_policycap_names))
19160619f0f5SStephen Smalley 			dentry = d_alloc_name(fsi->policycap_dir,
19174dc2fce3SStephen Smalley 					      selinux_policycap_names[iter]);
19183bb56b25SPaul Moore 		else
19190619f0f5SStephen Smalley 			dentry = d_alloc_name(fsi->policycap_dir, "unknown");
19203bb56b25SPaul Moore 
19213bb56b25SPaul Moore 		if (dentry == NULL)
19223bb56b25SPaul Moore 			return -ENOMEM;
19233bb56b25SPaul Moore 
19240619f0f5SStephen Smalley 		inode = sel_make_inode(fsi->sb, S_IFREG | 0444);
19257e4237faSnixiaoming 		if (inode == NULL) {
19267e4237faSnixiaoming 			dput(dentry);
19273bb56b25SPaul Moore 			return -ENOMEM;
19287e4237faSnixiaoming 		}
19293bb56b25SPaul Moore 
19303bb56b25SPaul Moore 		inode->i_fop = &sel_policycap_ops;
19313bb56b25SPaul Moore 		inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
19323bb56b25SPaul Moore 		d_add(dentry, inode);
19333bb56b25SPaul Moore 	}
19343bb56b25SPaul Moore 
19353bb56b25SPaul Moore 	return 0;
19363bb56b25SPaul Moore }
19373bb56b25SPaul Moore 
1938a1c2aa1eSAl Viro static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
19390dd4ae51SChristopher J. PeBenito 			unsigned long *ino)
19401da177e4SLinus Torvalds {
1941a1c2aa1eSAl Viro 	struct dentry *dentry = d_alloc_name(dir, name);
19421da177e4SLinus Torvalds 	struct inode *inode;
19431da177e4SLinus Torvalds 
1944a1c2aa1eSAl Viro 	if (!dentry)
1945a1c2aa1eSAl Viro 		return ERR_PTR(-ENOMEM);
1946a1c2aa1eSAl Viro 
1947a1c2aa1eSAl Viro 	inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1948a1c2aa1eSAl Viro 	if (!inode) {
1949a1c2aa1eSAl Viro 		dput(dentry);
1950a1c2aa1eSAl Viro 		return ERR_PTR(-ENOMEM);
1951a1c2aa1eSAl Viro 	}
1952b77a493bSEric Paris 
19531da177e4SLinus Torvalds 	inode->i_op = &simple_dir_inode_operations;
19541da177e4SLinus Torvalds 	inode->i_fop = &simple_dir_operations;
19550dd4ae51SChristopher J. PeBenito 	inode->i_ino = ++(*ino);
195640e906f8SJames Morris 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
1957d8c76e6fSDave Hansen 	inc_nlink(inode);
19581da177e4SLinus Torvalds 	d_add(dentry, inode);
1959edb20fb5SJames Morris 	/* bump link count on parent directory, too */
1960ce0b16ddSDavid Howells 	inc_nlink(d_inode(dir));
1961b77a493bSEric Paris 
1962a1c2aa1eSAl Viro 	return dentry;
19631da177e4SLinus Torvalds }
19641da177e4SLinus Torvalds 
19650619f0f5SStephen Smalley #define NULL_FILE_NAME "null"
19660619f0f5SStephen Smalley 
1967920f50b2SDavid Howells static int sel_fill_super(struct super_block *sb, struct fs_context *fc)
19681da177e4SLinus Torvalds {
19690619f0f5SStephen Smalley 	struct selinux_fs_info *fsi;
19701da177e4SLinus Torvalds 	int ret;
19711da177e4SLinus Torvalds 	struct dentry *dentry;
1972a1c2aa1eSAl Viro 	struct inode *inode;
19731da177e4SLinus Torvalds 	struct inode_security_struct *isec;
19741da177e4SLinus Torvalds 
1975cda37124SEric Biggers 	static const struct tree_descr selinux_files[] = {
19761da177e4SLinus Torvalds 		[SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
19771da177e4SLinus Torvalds 		[SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
1978ce9982d0SStephen Smalley 		[SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
19791da177e4SLinus Torvalds 		[SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
19801da177e4SLinus Torvalds 		[SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
19811da177e4SLinus Torvalds 		[SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
19821da177e4SLinus Torvalds 		[SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
19831da177e4SLinus Torvalds 		[SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
19841da177e4SLinus Torvalds 		[SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
19851da177e4SLinus Torvalds 		[SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
19861da177e4SLinus Torvalds 		[SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
19871da177e4SLinus Torvalds 		[SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
19881da177e4SLinus Torvalds 		[SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
19893f12070eSEric Paris 		[SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
19903f12070eSEric Paris 		[SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
199111904167SKaiGai Kohei 		[SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO},
199272e8c859SEric Paris 		[SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO},
1993f9df6458SAndrew Perepechko 		[SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops,
1994f9df6458SAndrew Perepechko 					S_IWUGO},
19951da177e4SLinus Torvalds 		/* last one */ {""}
19961da177e4SLinus Torvalds 	};
19970619f0f5SStephen Smalley 
19980619f0f5SStephen Smalley 	ret = selinux_fs_info_create(sb);
19990619f0f5SStephen Smalley 	if (ret)
20000619f0f5SStephen Smalley 		goto err;
20010619f0f5SStephen Smalley 
20021da177e4SLinus Torvalds 	ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
20031da177e4SLinus Torvalds 	if (ret)
2004161ce45aSJames Morris 		goto err;
20051da177e4SLinus Torvalds 
20060619f0f5SStephen Smalley 	fsi = sb->s_fs_info;
20070619f0f5SStephen Smalley 	fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino);
20080619f0f5SStephen Smalley 	if (IS_ERR(fsi->bool_dir)) {
20090619f0f5SStephen Smalley 		ret = PTR_ERR(fsi->bool_dir);
20100619f0f5SStephen Smalley 		fsi->bool_dir = NULL;
2011161ce45aSJames Morris 		goto err;
2012a1c2aa1eSAl Viro 	}
20131da177e4SLinus Torvalds 
2014b77a493bSEric Paris 	ret = -ENOMEM;
20151da177e4SLinus Torvalds 	dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
2016b77a493bSEric Paris 	if (!dentry)
2017161ce45aSJames Morris 		goto err;
20181da177e4SLinus Torvalds 
2019161ce45aSJames Morris 	ret = -ENOMEM;
2020b77a493bSEric Paris 	inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
20217e4237faSnixiaoming 	if (!inode) {
20227e4237faSnixiaoming 		dput(dentry);
2023161ce45aSJames Morris 		goto err;
20247e4237faSnixiaoming 	}
2025b77a493bSEric Paris 
20260619f0f5SStephen Smalley 	inode->i_ino = ++fsi->last_ino;
202780788c22SCasey Schaufler 	isec = selinux_inode(inode);
20281da177e4SLinus Torvalds 	isec->sid = SECINITSID_DEVNULL;
20291da177e4SLinus Torvalds 	isec->sclass = SECCLASS_CHR_FILE;
203042059112SAndreas Gruenbacher 	isec->initialized = LABEL_INITIALIZED;
20311da177e4SLinus Torvalds 
20321da177e4SLinus Torvalds 	init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
20331da177e4SLinus Torvalds 	d_add(dentry, inode);
20341da177e4SLinus Torvalds 
20350619f0f5SStephen Smalley 	dentry = sel_make_dir(sb->s_root, "avc", &fsi->last_ino);
2036a1c2aa1eSAl Viro 	if (IS_ERR(dentry)) {
2037a1c2aa1eSAl Viro 		ret = PTR_ERR(dentry);
2038161ce45aSJames Morris 		goto err;
2039a1c2aa1eSAl Viro 	}
20401da177e4SLinus Torvalds 
20411da177e4SLinus Torvalds 	ret = sel_make_avc_files(dentry);
204266f8e2f0SJeff Vander Stoep 
204366f8e2f0SJeff Vander Stoep 	dentry = sel_make_dir(sb->s_root, "ss", &fsi->last_ino);
204466f8e2f0SJeff Vander Stoep 	if (IS_ERR(dentry)) {
204566f8e2f0SJeff Vander Stoep 		ret = PTR_ERR(dentry);
204666f8e2f0SJeff Vander Stoep 		goto err;
204766f8e2f0SJeff Vander Stoep 	}
204866f8e2f0SJeff Vander Stoep 
204966f8e2f0SJeff Vander Stoep 	ret = sel_make_ss_files(dentry);
20501da177e4SLinus Torvalds 	if (ret)
2051161ce45aSJames Morris 		goto err;
2052f0ee2e46SJames Carter 
20530619f0f5SStephen Smalley 	dentry = sel_make_dir(sb->s_root, "initial_contexts", &fsi->last_ino);
2054a1c2aa1eSAl Viro 	if (IS_ERR(dentry)) {
2055a1c2aa1eSAl Viro 		ret = PTR_ERR(dentry);
2056f0ee2e46SJames Carter 		goto err;
2057a1c2aa1eSAl Viro 	}
2058f0ee2e46SJames Carter 
2059f0ee2e46SJames Carter 	ret = sel_make_initcon_files(dentry);
2060f0ee2e46SJames Carter 	if (ret)
2061f0ee2e46SJames Carter 		goto err;
2062f0ee2e46SJames Carter 
20630619f0f5SStephen Smalley 	fsi->class_dir = sel_make_dir(sb->s_root, "class", &fsi->last_ino);
20640619f0f5SStephen Smalley 	if (IS_ERR(fsi->class_dir)) {
20650619f0f5SStephen Smalley 		ret = PTR_ERR(fsi->class_dir);
20660619f0f5SStephen Smalley 		fsi->class_dir = NULL;
2067e47c8fc5SChristopher J. PeBenito 		goto err;
2068a1c2aa1eSAl Viro 	}
2069e47c8fc5SChristopher J. PeBenito 
20700619f0f5SStephen Smalley 	fsi->policycap_dir = sel_make_dir(sb->s_root, "policy_capabilities",
20710619f0f5SStephen Smalley 					  &fsi->last_ino);
20720619f0f5SStephen Smalley 	if (IS_ERR(fsi->policycap_dir)) {
20730619f0f5SStephen Smalley 		ret = PTR_ERR(fsi->policycap_dir);
20740619f0f5SStephen Smalley 		fsi->policycap_dir = NULL;
2075e47c8fc5SChristopher J. PeBenito 		goto err;
2076a1c2aa1eSAl Viro 	}
20770619f0f5SStephen Smalley 
20780619f0f5SStephen Smalley 	ret = sel_make_policy_nodes(fsi);
20790619f0f5SStephen Smalley 	if (ret)
20800619f0f5SStephen Smalley 		goto err;
2081b77a493bSEric Paris 	return 0;
2082161ce45aSJames Morris err:
2083f8b69a5fSpeter enderborg 	pr_err("SELinux: %s:  failed while creating inodes\n",
2084744ba35eSEric Paris 		__func__);
20850619f0f5SStephen Smalley 
20860619f0f5SStephen Smalley 	selinux_fs_info_free(sb);
20870619f0f5SStephen Smalley 
2088b77a493bSEric Paris 	return ret;
20891da177e4SLinus Torvalds }
20901da177e4SLinus Torvalds 
2091920f50b2SDavid Howells static int sel_get_tree(struct fs_context *fc)
20921da177e4SLinus Torvalds {
2093920f50b2SDavid Howells 	return get_tree_single(fc, sel_fill_super);
2094920f50b2SDavid Howells }
2095920f50b2SDavid Howells 
2096920f50b2SDavid Howells static const struct fs_context_operations sel_context_ops = {
2097920f50b2SDavid Howells 	.get_tree	= sel_get_tree,
2098920f50b2SDavid Howells };
2099920f50b2SDavid Howells 
2100920f50b2SDavid Howells static int sel_init_fs_context(struct fs_context *fc)
2101920f50b2SDavid Howells {
2102920f50b2SDavid Howells 	fc->ops = &sel_context_ops;
2103920f50b2SDavid Howells 	return 0;
21041da177e4SLinus Torvalds }
21051da177e4SLinus Torvalds 
21060619f0f5SStephen Smalley static void sel_kill_sb(struct super_block *sb)
21070619f0f5SStephen Smalley {
21080619f0f5SStephen Smalley 	selinux_fs_info_free(sb);
21090619f0f5SStephen Smalley 	kill_litter_super(sb);
21100619f0f5SStephen Smalley }
21110619f0f5SStephen Smalley 
21121da177e4SLinus Torvalds static struct file_system_type sel_fs_type = {
21131da177e4SLinus Torvalds 	.name		= "selinuxfs",
2114920f50b2SDavid Howells 	.init_fs_context = sel_init_fs_context,
21150619f0f5SStephen Smalley 	.kill_sb	= sel_kill_sb,
21161da177e4SLinus Torvalds };
21171da177e4SLinus Torvalds 
21181da177e4SLinus Torvalds struct vfsmount *selinuxfs_mount;
21190619f0f5SStephen Smalley struct path selinux_null;
21201da177e4SLinus Torvalds 
21211da177e4SLinus Torvalds static int __init init_sel_fs(void)
21221da177e4SLinus Torvalds {
21230619f0f5SStephen Smalley 	struct qstr null_name = QSTR_INIT(NULL_FILE_NAME,
21240619f0f5SStephen Smalley 					  sizeof(NULL_FILE_NAME)-1);
21251da177e4SLinus Torvalds 	int err;
21261da177e4SLinus Torvalds 
21276c5a682eSStephen Smalley 	if (!selinux_enabled_boot)
21281da177e4SLinus Torvalds 		return 0;
21297a627e3bSGreg Kroah-Hartman 
2130f9bb4882SEric W. Biederman 	err = sysfs_create_mount_point(fs_kobj, "selinux");
2131f9bb4882SEric W. Biederman 	if (err)
2132f9bb4882SEric W. Biederman 		return err;
21337a627e3bSGreg Kroah-Hartman 
21341da177e4SLinus Torvalds 	err = register_filesystem(&sel_fs_type);
21357a627e3bSGreg Kroah-Hartman 	if (err) {
2136f9bb4882SEric W. Biederman 		sysfs_remove_mount_point(fs_kobj, "selinux");
2137b77a493bSEric Paris 		return err;
21387a627e3bSGreg Kroah-Hartman 	}
2139b77a493bSEric Paris 
2140765927b2SAl Viro 	selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type);
21411da177e4SLinus Torvalds 	if (IS_ERR(selinuxfs_mount)) {
2142f8b69a5fSpeter enderborg 		pr_err("selinuxfs:  could not mount!\n");
21431da177e4SLinus Torvalds 		err = PTR_ERR(selinuxfs_mount);
21441da177e4SLinus Torvalds 		selinuxfs_mount = NULL;
21451da177e4SLinus Torvalds 	}
21460619f0f5SStephen Smalley 	selinux_null.dentry = d_hash_and_lookup(selinux_null.mnt->mnt_root,
21470619f0f5SStephen Smalley 						&null_name);
21480619f0f5SStephen Smalley 	if (IS_ERR(selinux_null.dentry)) {
21490619f0f5SStephen Smalley 		pr_err("selinuxfs:  could not lookup null!\n");
21500619f0f5SStephen Smalley 		err = PTR_ERR(selinux_null.dentry);
21510619f0f5SStephen Smalley 		selinux_null.dentry = NULL;
21520619f0f5SStephen Smalley 	}
2153b77a493bSEric Paris 
21541da177e4SLinus Torvalds 	return err;
21551da177e4SLinus Torvalds }
21561da177e4SLinus Torvalds 
21571da177e4SLinus Torvalds __initcall(init_sel_fs);
21581da177e4SLinus Torvalds 
21591da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
21601da177e4SLinus Torvalds void exit_sel_fs(void)
21611da177e4SLinus Torvalds {
2162f9bb4882SEric W. Biederman 	sysfs_remove_mount_point(fs_kobj, "selinux");
2163fd40ffc7SStephen Smalley 	dput(selinux_null.dentry);
2164423e0ab0STim Chen 	kern_unmount(selinuxfs_mount);
21651da177e4SLinus Torvalds 	unregister_filesystem(&sel_fs_type);
21661da177e4SLinus Torvalds }
21671da177e4SLinus Torvalds #endif
2168