xref: /openbmc/linux/security/selinux/selinuxfs.c (revision 6c5a682e)
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 
284bfd51626SDavi Arnaut 	if (count >= PAGE_SIZE)
2858365a719SAl Viro 		return -ENOMEM;
286b77a493bSEric Paris 
2871da177e4SLinus Torvalds 	/* No partial writes. */
288b77a493bSEric Paris 	if (*ppos != 0)
2898365a719SAl Viro 		return -EINVAL;
290b77a493bSEric Paris 
2918365a719SAl Viro 	page = memdup_user_nul(buf, count);
2928365a719SAl Viro 	if (IS_ERR(page))
2938365a719SAl Viro 		return PTR_ERR(page);
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 	length = -EINVAL;
2961da177e4SLinus Torvalds 	if (sscanf(page, "%d", &new_value) != 1)
2971da177e4SLinus Torvalds 		goto out;
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	if (new_value) {
3004195ed42SRichard Guy Briggs 		enforcing = enforcing_enabled(fsi->state);
3010619f0f5SStephen Smalley 		length = selinux_disable(fsi->state);
302b77a493bSEric Paris 		if (length)
3031da177e4SLinus Torvalds 			goto out;
304cdfb6b34SRichard Guy Briggs 		audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS,
3054195ed42SRichard Guy Briggs 			"enforcing=%d old_enforcing=%d auid=%u ses=%u"
3066c5a682eSStephen Smalley 			" enabled=0 old-enabled=1 lsm=selinux res=1",
3074195ed42SRichard Guy Briggs 			enforcing, enforcing,
308581abc09SEric W. Biederman 			from_kuid(&init_user_ns, audit_get_loginuid(current)),
3096c5a682eSStephen Smalley 			audit_get_sessionid(current));
3101da177e4SLinus Torvalds 	}
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 	length = count;
3131da177e4SLinus Torvalds out:
3148365a719SAl Viro 	kfree(page);
3151da177e4SLinus Torvalds 	return length;
3161da177e4SLinus Torvalds }
3171da177e4SLinus Torvalds #else
3181da177e4SLinus Torvalds #define sel_write_disable NULL
3191da177e4SLinus Torvalds #endif
3201da177e4SLinus Torvalds 
3219c2e08c5SArjan van de Ven static const struct file_operations sel_disable_ops = {
3221da177e4SLinus Torvalds 	.write		= sel_write_disable,
32357a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
3241da177e4SLinus Torvalds };
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
3271da177e4SLinus Torvalds 				   size_t count, loff_t *ppos)
3281da177e4SLinus Torvalds {
3291da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
3301da177e4SLinus Torvalds 	ssize_t length;
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
3331da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
3341da177e4SLinus Torvalds }
3351da177e4SLinus Torvalds 
3369c2e08c5SArjan van de Ven static const struct file_operations sel_policyvers_ops = {
3371da177e4SLinus Torvalds 	.read		= sel_read_policyvers,
33857a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
3391da177e4SLinus Torvalds };
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds /* declaration for sel_write_load */
3420619f0f5SStephen Smalley static int sel_make_bools(struct selinux_fs_info *fsi);
3430619f0f5SStephen Smalley static int sel_make_classes(struct selinux_fs_info *fsi);
3440619f0f5SStephen Smalley static int sel_make_policycap(struct selinux_fs_info *fsi);
345e47c8fc5SChristopher J. PeBenito 
346e47c8fc5SChristopher J. PeBenito /* declaration for sel_make_class_dirs */
347a1c2aa1eSAl Viro static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
348e47c8fc5SChristopher J. PeBenito 			unsigned long *ino);
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds static ssize_t sel_read_mls(struct file *filp, char __user *buf,
3511da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
3521da177e4SLinus Torvalds {
3530619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
3541da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
3551da177e4SLinus Torvalds 	ssize_t length;
3561da177e4SLinus Torvalds 
3570719aaf5SGuido Trentalancia 	length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
3580619f0f5SStephen Smalley 			   security_mls_enabled(fsi->state));
3591da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
3601da177e4SLinus Torvalds }
3611da177e4SLinus Torvalds 
3629c2e08c5SArjan van de Ven static const struct file_operations sel_mls_ops = {
3631da177e4SLinus Torvalds 	.read		= sel_read_mls,
36457a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
3651da177e4SLinus Torvalds };
3661da177e4SLinus Torvalds 
367cee74f47SEric Paris struct policy_load_memory {
368cee74f47SEric Paris 	size_t len;
369cee74f47SEric Paris 	void *data;
370cee74f47SEric Paris };
371cee74f47SEric Paris 
372cee74f47SEric Paris static int sel_open_policy(struct inode *inode, struct file *filp)
373cee74f47SEric Paris {
3740619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
3750619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
376cee74f47SEric Paris 	struct policy_load_memory *plm = NULL;
377cee74f47SEric Paris 	int rc;
378cee74f47SEric Paris 
379cee74f47SEric Paris 	BUG_ON(filp->private_data);
380cee74f47SEric Paris 
3810619f0f5SStephen Smalley 	mutex_lock(&fsi->mutex);
382cee74f47SEric Paris 
3836b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
3846b6bc620SStephen Smalley 			  current_sid(), SECINITSID_SECURITY,
385be0554c9SStephen Smalley 			  SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
386cee74f47SEric Paris 	if (rc)
387cee74f47SEric Paris 		goto err;
388cee74f47SEric Paris 
389cee74f47SEric Paris 	rc = -EBUSY;
3900619f0f5SStephen Smalley 	if (fsi->policy_opened)
391cee74f47SEric Paris 		goto err;
392cee74f47SEric Paris 
393cee74f47SEric Paris 	rc = -ENOMEM;
394cee74f47SEric Paris 	plm = kzalloc(sizeof(*plm), GFP_KERNEL);
395cee74f47SEric Paris 	if (!plm)
396cee74f47SEric Paris 		goto err;
397cee74f47SEric Paris 
3980619f0f5SStephen Smalley 	if (i_size_read(inode) != security_policydb_len(state)) {
3995955102cSAl Viro 		inode_lock(inode);
4000619f0f5SStephen Smalley 		i_size_write(inode, security_policydb_len(state));
4015955102cSAl Viro 		inode_unlock(inode);
402cee74f47SEric Paris 	}
403cee74f47SEric Paris 
4040619f0f5SStephen Smalley 	rc = security_read_policy(state, &plm->data, &plm->len);
405cee74f47SEric Paris 	if (rc)
406cee74f47SEric Paris 		goto err;
407cee74f47SEric Paris 
4080619f0f5SStephen Smalley 	fsi->policy_opened = 1;
409cee74f47SEric Paris 
410cee74f47SEric Paris 	filp->private_data = plm;
411cee74f47SEric Paris 
4120619f0f5SStephen Smalley 	mutex_unlock(&fsi->mutex);
413cee74f47SEric Paris 
414cee74f47SEric Paris 	return 0;
415cee74f47SEric Paris err:
4160619f0f5SStephen Smalley 	mutex_unlock(&fsi->mutex);
417cee74f47SEric Paris 
418cee74f47SEric Paris 	if (plm)
419cee74f47SEric Paris 		vfree(plm->data);
420cee74f47SEric Paris 	kfree(plm);
421cee74f47SEric Paris 	return rc;
422cee74f47SEric Paris }
423cee74f47SEric Paris 
424cee74f47SEric Paris static int sel_release_policy(struct inode *inode, struct file *filp)
425cee74f47SEric Paris {
4260619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
427cee74f47SEric Paris 	struct policy_load_memory *plm = filp->private_data;
428cee74f47SEric Paris 
429cee74f47SEric Paris 	BUG_ON(!plm);
430cee74f47SEric Paris 
4310619f0f5SStephen Smalley 	fsi->policy_opened = 0;
432cee74f47SEric Paris 
433cee74f47SEric Paris 	vfree(plm->data);
434cee74f47SEric Paris 	kfree(plm);
435cee74f47SEric Paris 
436cee74f47SEric Paris 	return 0;
437cee74f47SEric Paris }
438cee74f47SEric Paris 
439cee74f47SEric Paris static ssize_t sel_read_policy(struct file *filp, char __user *buf,
440cee74f47SEric Paris 			       size_t count, loff_t *ppos)
441cee74f47SEric Paris {
442cee74f47SEric Paris 	struct policy_load_memory *plm = filp->private_data;
443cee74f47SEric Paris 	int ret;
444cee74f47SEric Paris 
4456b6bc620SStephen Smalley 	ret = avc_has_perm(&selinux_state,
4466b6bc620SStephen Smalley 			   current_sid(), SECINITSID_SECURITY,
447be0554c9SStephen Smalley 			  SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
448cee74f47SEric Paris 	if (ret)
449cee74f47SEric Paris 		return ret;
4500da74120SJann Horn 
4510da74120SJann Horn 	return simple_read_from_buffer(buf, count, ppos, plm->data, plm->len);
452cee74f47SEric Paris }
453cee74f47SEric Paris 
454ac9a1f6dSSouptick Joarder static vm_fault_t sel_mmap_policy_fault(struct vm_fault *vmf)
455845ca30fSEric Paris {
45611bac800SDave Jiang 	struct policy_load_memory *plm = vmf->vma->vm_file->private_data;
457845ca30fSEric Paris 	unsigned long offset;
458845ca30fSEric Paris 	struct page *page;
459845ca30fSEric Paris 
460845ca30fSEric Paris 	if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE))
461845ca30fSEric Paris 		return VM_FAULT_SIGBUS;
462845ca30fSEric Paris 
463845ca30fSEric Paris 	offset = vmf->pgoff << PAGE_SHIFT;
464845ca30fSEric Paris 	if (offset >= roundup(plm->len, PAGE_SIZE))
465845ca30fSEric Paris 		return VM_FAULT_SIGBUS;
466845ca30fSEric Paris 
467845ca30fSEric Paris 	page = vmalloc_to_page(plm->data + offset);
468845ca30fSEric Paris 	get_page(page);
469845ca30fSEric Paris 
470845ca30fSEric Paris 	vmf->page = page;
471845ca30fSEric Paris 
472845ca30fSEric Paris 	return 0;
473845ca30fSEric Paris }
474845ca30fSEric Paris 
4757cbea8dcSKirill A. Shutemov static const struct vm_operations_struct sel_mmap_policy_ops = {
476845ca30fSEric Paris 	.fault = sel_mmap_policy_fault,
477845ca30fSEric Paris 	.page_mkwrite = sel_mmap_policy_fault,
478845ca30fSEric Paris };
479845ca30fSEric Paris 
480ad3fa08cSJames Morris static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma)
481845ca30fSEric Paris {
482845ca30fSEric Paris 	if (vma->vm_flags & VM_SHARED) {
483845ca30fSEric Paris 		/* do not allow mprotect to make mapping writable */
484845ca30fSEric Paris 		vma->vm_flags &= ~VM_MAYWRITE;
485845ca30fSEric Paris 
486845ca30fSEric Paris 		if (vma->vm_flags & VM_WRITE)
487845ca30fSEric Paris 			return -EACCES;
488845ca30fSEric Paris 	}
489845ca30fSEric Paris 
490314e51b9SKonstantin Khlebnikov 	vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
491845ca30fSEric Paris 	vma->vm_ops = &sel_mmap_policy_ops;
492845ca30fSEric Paris 
493845ca30fSEric Paris 	return 0;
494845ca30fSEric Paris }
495845ca30fSEric Paris 
496cee74f47SEric Paris static const struct file_operations sel_policy_ops = {
497cee74f47SEric Paris 	.open		= sel_open_policy,
498cee74f47SEric Paris 	.read		= sel_read_policy,
499845ca30fSEric Paris 	.mmap		= sel_mmap_policy,
500cee74f47SEric Paris 	.release	= sel_release_policy,
50147a93a5bSEric Paris 	.llseek		= generic_file_llseek,
502cee74f47SEric Paris };
503cee74f47SEric Paris 
5040619f0f5SStephen Smalley static int sel_make_policy_nodes(struct selinux_fs_info *fsi)
5050619f0f5SStephen Smalley {
5060619f0f5SStephen Smalley 	int ret;
5070619f0f5SStephen Smalley 
5080619f0f5SStephen Smalley 	ret = sel_make_bools(fsi);
5090619f0f5SStephen Smalley 	if (ret) {
5100619f0f5SStephen Smalley 		pr_err("SELinux: failed to load policy booleans\n");
5110619f0f5SStephen Smalley 		return ret;
5120619f0f5SStephen Smalley 	}
5130619f0f5SStephen Smalley 
5140619f0f5SStephen Smalley 	ret = sel_make_classes(fsi);
5150619f0f5SStephen Smalley 	if (ret) {
5160619f0f5SStephen Smalley 		pr_err("SELinux: failed to load policy classes\n");
5170619f0f5SStephen Smalley 		return ret;
5180619f0f5SStephen Smalley 	}
5190619f0f5SStephen Smalley 
5200619f0f5SStephen Smalley 	ret = sel_make_policycap(fsi);
5210619f0f5SStephen Smalley 	if (ret) {
5220619f0f5SStephen Smalley 		pr_err("SELinux: failed to load policy capabilities\n");
5230619f0f5SStephen Smalley 		return ret;
5240619f0f5SStephen Smalley 	}
5250619f0f5SStephen Smalley 
5260619f0f5SStephen Smalley 	return 0;
5270619f0f5SStephen Smalley }
5280619f0f5SStephen Smalley 
5291da177e4SLinus Torvalds static ssize_t sel_write_load(struct file *file, const char __user *buf,
5301da177e4SLinus Torvalds 			      size_t count, loff_t *ppos)
5311da177e4SLinus Torvalds 
5321da177e4SLinus Torvalds {
5330619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
5341da177e4SLinus Torvalds 	ssize_t length;
5351da177e4SLinus Torvalds 	void *data = NULL;
5361da177e4SLinus Torvalds 
5370619f0f5SStephen Smalley 	mutex_lock(&fsi->mutex);
5381da177e4SLinus Torvalds 
5396b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
5406b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
541be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL);
5421da177e4SLinus Torvalds 	if (length)
5431da177e4SLinus Torvalds 		goto out;
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds 	/* No partial writes. */
5461da177e4SLinus Torvalds 	length = -EINVAL;
547b77a493bSEric Paris 	if (*ppos != 0)
5481da177e4SLinus Torvalds 		goto out;
5491da177e4SLinus Torvalds 
550b77a493bSEric Paris 	length = -ENOMEM;
551b77a493bSEric Paris 	data = vmalloc(count);
552b77a493bSEric Paris 	if (!data)
553b77a493bSEric Paris 		goto out;
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds 	length = -EFAULT;
5561da177e4SLinus Torvalds 	if (copy_from_user(data, buf, count) != 0)
5571da177e4SLinus Torvalds 		goto out;
5581da177e4SLinus Torvalds 
5590619f0f5SStephen Smalley 	length = security_load_policy(fsi->state, data, count);
5604262fb51SGary Tierney 	if (length) {
5614262fb51SGary Tierney 		pr_warn_ratelimited("SELinux: failed to load policy\n");
5621da177e4SLinus Torvalds 		goto out;
5634262fb51SGary Tierney 	}
5641da177e4SLinus Torvalds 
5650619f0f5SStephen Smalley 	length = sel_make_policy_nodes(fsi);
5660619f0f5SStephen Smalley 	if (length)
567e47c8fc5SChristopher J. PeBenito 		goto out1;
568b77a493bSEric Paris 
5691da177e4SLinus Torvalds 	length = count;
570e47c8fc5SChristopher J. PeBenito 
571e47c8fc5SChristopher J. PeBenito out1:
572cdfb6b34SRichard Guy Briggs 	audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
573d141136fSRichard Guy Briggs 		"auid=%u ses=%u lsm=selinux res=1",
574581abc09SEric W. Biederman 		from_kuid(&init_user_ns, audit_get_loginuid(current)),
5754746ec5bSEric Paris 		audit_get_sessionid(current));
5761da177e4SLinus Torvalds out:
5770619f0f5SStephen Smalley 	mutex_unlock(&fsi->mutex);
5781da177e4SLinus Torvalds 	vfree(data);
5791da177e4SLinus Torvalds 	return length;
5801da177e4SLinus Torvalds }
5811da177e4SLinus Torvalds 
5829c2e08c5SArjan van de Ven static const struct file_operations sel_load_ops = {
5831da177e4SLinus Torvalds 	.write		= sel_write_load,
58457a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
5851da177e4SLinus Torvalds };
5861da177e4SLinus Torvalds 
587ce9982d0SStephen Smalley static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
5881da177e4SLinus Torvalds {
5890619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
5900619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
591b77a493bSEric Paris 	char *canon = NULL;
592ce9982d0SStephen Smalley 	u32 sid, len;
5931da177e4SLinus Torvalds 	ssize_t length;
5941da177e4SLinus Torvalds 
5956b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
5966b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
597be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__CHECK_CONTEXT, NULL);
5981da177e4SLinus Torvalds 	if (length)
599b77a493bSEric Paris 		goto out;
6001da177e4SLinus Torvalds 
6010619f0f5SStephen Smalley 	length = security_context_to_sid(state, buf, size, &sid, GFP_KERNEL);
602b77a493bSEric Paris 	if (length)
603b77a493bSEric Paris 		goto out;
6041da177e4SLinus Torvalds 
6050619f0f5SStephen Smalley 	length = security_sid_to_context(state, sid, &canon, &len);
606b77a493bSEric Paris 	if (length)
607b77a493bSEric Paris 		goto out;
608ce9982d0SStephen Smalley 
609b77a493bSEric Paris 	length = -ERANGE;
610ce9982d0SStephen Smalley 	if (len > SIMPLE_TRANSACTION_LIMIT) {
611f8b69a5fSpeter enderborg 		pr_err("SELinux: %s:  context size (%u) exceeds "
612744ba35eSEric Paris 			"payload max\n", __func__, len);
613ce9982d0SStephen Smalley 		goto out;
614ce9982d0SStephen Smalley 	}
615ce9982d0SStephen Smalley 
616ce9982d0SStephen Smalley 	memcpy(buf, canon, len);
617ce9982d0SStephen Smalley 	length = len;
6181da177e4SLinus Torvalds out:
619ce9982d0SStephen Smalley 	kfree(canon);
6201da177e4SLinus Torvalds 	return length;
6211da177e4SLinus Torvalds }
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
6241da177e4SLinus Torvalds 				     size_t count, loff_t *ppos)
6251da177e4SLinus Torvalds {
6260619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
6271da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
6281da177e4SLinus Torvalds 	ssize_t length;
6291da177e4SLinus Torvalds 
6300619f0f5SStephen Smalley 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u", fsi->state->checkreqprot);
6311da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
6321da177e4SLinus Torvalds }
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
6351da177e4SLinus Torvalds 				      size_t count, loff_t *ppos)
6361da177e4SLinus Torvalds {
6370619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
6388365a719SAl Viro 	char *page;
6391da177e4SLinus Torvalds 	ssize_t length;
6401da177e4SLinus Torvalds 	unsigned int new_value;
6411da177e4SLinus Torvalds 
6426b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
6436b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
644be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__SETCHECKREQPROT,
645be0554c9SStephen Smalley 			      NULL);
6461da177e4SLinus Torvalds 	if (length)
6478365a719SAl Viro 		return length;
6481da177e4SLinus Torvalds 
649bfd51626SDavi Arnaut 	if (count >= PAGE_SIZE)
6508365a719SAl Viro 		return -ENOMEM;
651b77a493bSEric Paris 
6521da177e4SLinus Torvalds 	/* No partial writes. */
653b77a493bSEric Paris 	if (*ppos != 0)
6548365a719SAl Viro 		return -EINVAL;
655b77a493bSEric Paris 
6568365a719SAl Viro 	page = memdup_user_nul(buf, count);
6578365a719SAl Viro 	if (IS_ERR(page))
6588365a719SAl Viro 		return PTR_ERR(page);
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds 	length = -EINVAL;
6611da177e4SLinus Torvalds 	if (sscanf(page, "%u", &new_value) != 1)
6621da177e4SLinus Torvalds 		goto out;
6631da177e4SLinus Torvalds 
6640619f0f5SStephen Smalley 	fsi->state->checkreqprot = new_value ? 1 : 0;
6651da177e4SLinus Torvalds 	length = count;
6661da177e4SLinus Torvalds out:
6678365a719SAl Viro 	kfree(page);
6681da177e4SLinus Torvalds 	return length;
6691da177e4SLinus Torvalds }
6709c2e08c5SArjan van de Ven static const struct file_operations sel_checkreqprot_ops = {
6711da177e4SLinus Torvalds 	.read		= sel_read_checkreqprot,
6721da177e4SLinus Torvalds 	.write		= sel_write_checkreqprot,
67357a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
6741da177e4SLinus Torvalds };
6751da177e4SLinus Torvalds 
676f9df6458SAndrew Perepechko static ssize_t sel_write_validatetrans(struct file *file,
677f9df6458SAndrew Perepechko 					const char __user *buf,
678f9df6458SAndrew Perepechko 					size_t count, loff_t *ppos)
679f9df6458SAndrew Perepechko {
6800619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
6810619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
682f9df6458SAndrew Perepechko 	char *oldcon = NULL, *newcon = NULL, *taskcon = NULL;
683f9df6458SAndrew Perepechko 	char *req = NULL;
684f9df6458SAndrew Perepechko 	u32 osid, nsid, tsid;
685f9df6458SAndrew Perepechko 	u16 tclass;
686f9df6458SAndrew Perepechko 	int rc;
687f9df6458SAndrew Perepechko 
6886b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
6896b6bc620SStephen Smalley 			  current_sid(), SECINITSID_SECURITY,
690be0554c9SStephen Smalley 			  SECCLASS_SECURITY, SECURITY__VALIDATE_TRANS, NULL);
691f9df6458SAndrew Perepechko 	if (rc)
692f9df6458SAndrew Perepechko 		goto out;
693f9df6458SAndrew Perepechko 
694f9df6458SAndrew Perepechko 	rc = -ENOMEM;
695f9df6458SAndrew Perepechko 	if (count >= PAGE_SIZE)
696f9df6458SAndrew Perepechko 		goto out;
697f9df6458SAndrew Perepechko 
698f9df6458SAndrew Perepechko 	/* No partial writes. */
699f9df6458SAndrew Perepechko 	rc = -EINVAL;
700f9df6458SAndrew Perepechko 	if (*ppos != 0)
701f9df6458SAndrew Perepechko 		goto out;
702f9df6458SAndrew Perepechko 
7030b884d25SAl Viro 	req = memdup_user_nul(buf, count);
7040b884d25SAl Viro 	if (IS_ERR(req)) {
7050b884d25SAl Viro 		rc = PTR_ERR(req);
7060b884d25SAl Viro 		req = NULL;
707f9df6458SAndrew Perepechko 		goto out;
7080b884d25SAl Viro 	}
709f9df6458SAndrew Perepechko 
710f9df6458SAndrew Perepechko 	rc = -ENOMEM;
711f9df6458SAndrew Perepechko 	oldcon = kzalloc(count + 1, GFP_KERNEL);
712f9df6458SAndrew Perepechko 	if (!oldcon)
713f9df6458SAndrew Perepechko 		goto out;
714f9df6458SAndrew Perepechko 
715f9df6458SAndrew Perepechko 	newcon = kzalloc(count + 1, GFP_KERNEL);
716f9df6458SAndrew Perepechko 	if (!newcon)
717f9df6458SAndrew Perepechko 		goto out;
718f9df6458SAndrew Perepechko 
719f9df6458SAndrew Perepechko 	taskcon = kzalloc(count + 1, GFP_KERNEL);
720f9df6458SAndrew Perepechko 	if (!taskcon)
721f9df6458SAndrew Perepechko 		goto out;
722f9df6458SAndrew Perepechko 
723f9df6458SAndrew Perepechko 	rc = -EINVAL;
724f9df6458SAndrew Perepechko 	if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4)
725f9df6458SAndrew Perepechko 		goto out;
726f9df6458SAndrew Perepechko 
7270619f0f5SStephen Smalley 	rc = security_context_str_to_sid(state, oldcon, &osid, GFP_KERNEL);
728f9df6458SAndrew Perepechko 	if (rc)
729f9df6458SAndrew Perepechko 		goto out;
730f9df6458SAndrew Perepechko 
7310619f0f5SStephen Smalley 	rc = security_context_str_to_sid(state, newcon, &nsid, GFP_KERNEL);
732f9df6458SAndrew Perepechko 	if (rc)
733f9df6458SAndrew Perepechko 		goto out;
734f9df6458SAndrew Perepechko 
7350619f0f5SStephen Smalley 	rc = security_context_str_to_sid(state, taskcon, &tsid, GFP_KERNEL);
736f9df6458SAndrew Perepechko 	if (rc)
737f9df6458SAndrew Perepechko 		goto out;
738f9df6458SAndrew Perepechko 
7390619f0f5SStephen Smalley 	rc = security_validate_transition_user(state, osid, nsid, tsid, tclass);
740f9df6458SAndrew Perepechko 	if (!rc)
741f9df6458SAndrew Perepechko 		rc = count;
742f9df6458SAndrew Perepechko out:
743f9df6458SAndrew Perepechko 	kfree(req);
744f9df6458SAndrew Perepechko 	kfree(oldcon);
745f9df6458SAndrew Perepechko 	kfree(newcon);
746f9df6458SAndrew Perepechko 	kfree(taskcon);
747f9df6458SAndrew Perepechko 	return rc;
748f9df6458SAndrew Perepechko }
749f9df6458SAndrew Perepechko 
750f9df6458SAndrew Perepechko static const struct file_operations sel_transition_ops = {
751f9df6458SAndrew Perepechko 	.write		= sel_write_validatetrans,
752f9df6458SAndrew Perepechko 	.llseek		= generic_file_llseek,
753f9df6458SAndrew Perepechko };
754f9df6458SAndrew Perepechko 
7551da177e4SLinus Torvalds /*
7561da177e4SLinus Torvalds  * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
7571da177e4SLinus Torvalds  */
7581da177e4SLinus Torvalds static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
7591da177e4SLinus Torvalds static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
7601da177e4SLinus Torvalds static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
7611da177e4SLinus Torvalds static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
7621da177e4SLinus Torvalds static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
7631da177e4SLinus Torvalds 
764631d2b49SEric Biggers static ssize_t (*const write_op[])(struct file *, char *, size_t) = {
7651da177e4SLinus Torvalds 	[SEL_ACCESS] = sel_write_access,
7661da177e4SLinus Torvalds 	[SEL_CREATE] = sel_write_create,
7671da177e4SLinus Torvalds 	[SEL_RELABEL] = sel_write_relabel,
7681da177e4SLinus Torvalds 	[SEL_USER] = sel_write_user,
7691da177e4SLinus Torvalds 	[SEL_MEMBER] = sel_write_member,
770ce9982d0SStephen Smalley 	[SEL_CONTEXT] = sel_write_context,
7711da177e4SLinus Torvalds };
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
7741da177e4SLinus Torvalds {
775496ad9aaSAl Viro 	ino_t ino = file_inode(file)->i_ino;
7761da177e4SLinus Torvalds 	char *data;
7771da177e4SLinus Torvalds 	ssize_t rv;
7781da177e4SLinus Torvalds 
7796e20a64aSNicolas Kaiser 	if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
7801da177e4SLinus Torvalds 		return -EINVAL;
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds 	data = simple_transaction_get(file, buf, size);
7831da177e4SLinus Torvalds 	if (IS_ERR(data))
7841da177e4SLinus Torvalds 		return PTR_ERR(data);
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds 	rv = write_op[ino](file, data, size);
7871da177e4SLinus Torvalds 	if (rv > 0) {
7881da177e4SLinus Torvalds 		simple_transaction_set(file, rv);
7891da177e4SLinus Torvalds 		rv = size;
7901da177e4SLinus Torvalds 	}
7911da177e4SLinus Torvalds 	return rv;
7921da177e4SLinus Torvalds }
7931da177e4SLinus Torvalds 
7949c2e08c5SArjan van de Ven static const struct file_operations transaction_ops = {
7951da177e4SLinus Torvalds 	.write		= selinux_transaction_write,
7961da177e4SLinus Torvalds 	.read		= simple_transaction_read,
7971da177e4SLinus Torvalds 	.release	= simple_transaction_release,
79857a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
7991da177e4SLinus Torvalds };
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds /*
8021da177e4SLinus Torvalds  * payload - write methods
8031da177e4SLinus Torvalds  * If the method has a response, the response should be put in buf,
8041da177e4SLinus Torvalds  * and the length returned.  Otherwise return 0 or and -error.
8051da177e4SLinus Torvalds  */
8061da177e4SLinus Torvalds 
8071da177e4SLinus Torvalds static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
8081da177e4SLinus Torvalds {
8090619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
8100619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
811b77a493bSEric Paris 	char *scon = NULL, *tcon = NULL;
8121da177e4SLinus Torvalds 	u32 ssid, tsid;
8131da177e4SLinus Torvalds 	u16 tclass;
8141da177e4SLinus Torvalds 	struct av_decision avd;
8151da177e4SLinus Torvalds 	ssize_t length;
8161da177e4SLinus Torvalds 
8176b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
8186b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
819be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__COMPUTE_AV, NULL);
8201da177e4SLinus Torvalds 	if (length)
821b77a493bSEric Paris 		goto out;
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds 	length = -ENOMEM;
82489d155efSJames Morris 	scon = kzalloc(size + 1, GFP_KERNEL);
8251da177e4SLinus Torvalds 	if (!scon)
826b77a493bSEric Paris 		goto out;
8271da177e4SLinus Torvalds 
828b77a493bSEric Paris 	length = -ENOMEM;
82989d155efSJames Morris 	tcon = kzalloc(size + 1, GFP_KERNEL);
8301da177e4SLinus Torvalds 	if (!tcon)
8311da177e4SLinus Torvalds 		goto out;
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds 	length = -EINVAL;
83419439d05SStephen Smalley 	if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
835b77a493bSEric Paris 		goto out;
8361da177e4SLinus Torvalds 
8370619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
838b77a493bSEric Paris 	if (length)
839b77a493bSEric Paris 		goto out;
840b77a493bSEric Paris 
8410619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
842b77a493bSEric Paris 	if (length)
843b77a493bSEric Paris 		goto out;
8441da177e4SLinus Torvalds 
8450619f0f5SStephen Smalley 	security_compute_av_user(state, ssid, tsid, tclass, &avd);
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 	length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
8488a6f83afSKaiGai Kohei 			  "%x %x %x %x %u %x",
849f1c6381aSEric Paris 			  avd.allowed, 0xffffffff,
8501da177e4SLinus Torvalds 			  avd.auditallow, avd.auditdeny,
8518a6f83afSKaiGai Kohei 			  avd.seqno, avd.flags);
8521da177e4SLinus Torvalds out:
853b77a493bSEric Paris 	kfree(tcon);
8541da177e4SLinus Torvalds 	kfree(scon);
8551da177e4SLinus Torvalds 	return length;
8561da177e4SLinus Torvalds }
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
8591da177e4SLinus Torvalds {
8600619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
8610619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
862b77a493bSEric Paris 	char *scon = NULL, *tcon = NULL;
863f50a3ec9SKohei Kaigai 	char *namebuf = NULL, *objname = NULL;
8641da177e4SLinus Torvalds 	u32 ssid, tsid, newsid;
8651da177e4SLinus Torvalds 	u16 tclass;
8661da177e4SLinus Torvalds 	ssize_t length;
867b77a493bSEric Paris 	char *newcon = NULL;
8681da177e4SLinus Torvalds 	u32 len;
869f50a3ec9SKohei Kaigai 	int nargs;
8701da177e4SLinus Torvalds 
8716b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
8726b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
873be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__COMPUTE_CREATE,
874be0554c9SStephen Smalley 			      NULL);
8751da177e4SLinus Torvalds 	if (length)
876b77a493bSEric Paris 		goto out;
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds 	length = -ENOMEM;
87989d155efSJames Morris 	scon = kzalloc(size + 1, GFP_KERNEL);
8801da177e4SLinus Torvalds 	if (!scon)
881b77a493bSEric Paris 		goto out;
8821da177e4SLinus Torvalds 
883b77a493bSEric Paris 	length = -ENOMEM;
88489d155efSJames Morris 	tcon = kzalloc(size + 1, GFP_KERNEL);
8851da177e4SLinus Torvalds 	if (!tcon)
8861da177e4SLinus Torvalds 		goto out;
8871da177e4SLinus Torvalds 
888f50a3ec9SKohei Kaigai 	length = -ENOMEM;
889f50a3ec9SKohei Kaigai 	namebuf = kzalloc(size + 1, GFP_KERNEL);
890f50a3ec9SKohei Kaigai 	if (!namebuf)
891b77a493bSEric Paris 		goto out;
8921da177e4SLinus Torvalds 
893f50a3ec9SKohei Kaigai 	length = -EINVAL;
894f50a3ec9SKohei Kaigai 	nargs = sscanf(buf, "%s %s %hu %s", scon, tcon, &tclass, namebuf);
895f50a3ec9SKohei Kaigai 	if (nargs < 3 || nargs > 4)
896f50a3ec9SKohei Kaigai 		goto out;
8970f7e4c33SKohei Kaigai 	if (nargs == 4) {
8980f7e4c33SKohei Kaigai 		/*
8990f7e4c33SKohei Kaigai 		 * If and when the name of new object to be queried contains
9000f7e4c33SKohei Kaigai 		 * either whitespace or multibyte characters, they shall be
9010f7e4c33SKohei Kaigai 		 * encoded based on the percentage-encoding rule.
9020f7e4c33SKohei Kaigai 		 * If not encoded, the sscanf logic picks up only left-half
9030f7e4c33SKohei Kaigai 		 * of the supplied name; splitted by a whitespace unexpectedly.
9040f7e4c33SKohei Kaigai 		 */
9050f7e4c33SKohei Kaigai 		char   *r, *w;
9060f7e4c33SKohei Kaigai 		int     c1, c2;
9070f7e4c33SKohei Kaigai 
9080f7e4c33SKohei Kaigai 		r = w = namebuf;
9090f7e4c33SKohei Kaigai 		do {
9100f7e4c33SKohei Kaigai 			c1 = *r++;
9110f7e4c33SKohei Kaigai 			if (c1 == '+')
9120f7e4c33SKohei Kaigai 				c1 = ' ';
9130f7e4c33SKohei Kaigai 			else if (c1 == '%') {
914af7ff2c2SAndy Shevchenko 				c1 = hex_to_bin(*r++);
915af7ff2c2SAndy Shevchenko 				if (c1 < 0)
9160f7e4c33SKohei Kaigai 					goto out;
917af7ff2c2SAndy Shevchenko 				c2 = hex_to_bin(*r++);
918af7ff2c2SAndy Shevchenko 				if (c2 < 0)
9190f7e4c33SKohei Kaigai 					goto out;
9200f7e4c33SKohei Kaigai 				c1 = (c1 << 4) | c2;
9210f7e4c33SKohei Kaigai 			}
9220f7e4c33SKohei Kaigai 			*w++ = c1;
9230f7e4c33SKohei Kaigai 		} while (c1 != '\0');
9240f7e4c33SKohei Kaigai 
925f50a3ec9SKohei Kaigai 		objname = namebuf;
9260f7e4c33SKohei Kaigai 	}
927f50a3ec9SKohei Kaigai 
9280619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
929b77a493bSEric Paris 	if (length)
930b77a493bSEric Paris 		goto out;
931b77a493bSEric Paris 
9320619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
933b77a493bSEric Paris 	if (length)
934b77a493bSEric Paris 		goto out;
9351da177e4SLinus Torvalds 
9360619f0f5SStephen Smalley 	length = security_transition_sid_user(state, ssid, tsid, tclass,
9370619f0f5SStephen Smalley 					      objname, &newsid);
938b77a493bSEric Paris 	if (length)
939b77a493bSEric Paris 		goto out;
9401da177e4SLinus Torvalds 
9410619f0f5SStephen Smalley 	length = security_sid_to_context(state, newsid, &newcon, &len);
942b77a493bSEric Paris 	if (length)
943b77a493bSEric Paris 		goto out;
9441da177e4SLinus Torvalds 
945b77a493bSEric Paris 	length = -ERANGE;
9461da177e4SLinus Torvalds 	if (len > SIMPLE_TRANSACTION_LIMIT) {
947f8b69a5fSpeter enderborg 		pr_err("SELinux: %s:  context size (%u) exceeds "
948744ba35eSEric Paris 			"payload max\n", __func__, len);
949b77a493bSEric Paris 		goto out;
9501da177e4SLinus Torvalds 	}
9511da177e4SLinus Torvalds 
9521da177e4SLinus Torvalds 	memcpy(buf, newcon, len);
9531da177e4SLinus Torvalds 	length = len;
9541da177e4SLinus Torvalds out:
955b77a493bSEric Paris 	kfree(newcon);
956f50a3ec9SKohei Kaigai 	kfree(namebuf);
957b77a493bSEric Paris 	kfree(tcon);
9581da177e4SLinus Torvalds 	kfree(scon);
9591da177e4SLinus Torvalds 	return length;
9601da177e4SLinus Torvalds }
9611da177e4SLinus Torvalds 
9621da177e4SLinus Torvalds static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
9631da177e4SLinus Torvalds {
9640619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
9650619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
966b77a493bSEric Paris 	char *scon = NULL, *tcon = NULL;
9671da177e4SLinus Torvalds 	u32 ssid, tsid, newsid;
9681da177e4SLinus Torvalds 	u16 tclass;
9691da177e4SLinus Torvalds 	ssize_t length;
970b77a493bSEric Paris 	char *newcon = NULL;
9711da177e4SLinus Torvalds 	u32 len;
9721da177e4SLinus Torvalds 
9736b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
9746b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
975be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__COMPUTE_RELABEL,
976be0554c9SStephen Smalley 			      NULL);
9771da177e4SLinus Torvalds 	if (length)
978b77a493bSEric Paris 		goto out;
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds 	length = -ENOMEM;
98189d155efSJames Morris 	scon = kzalloc(size + 1, GFP_KERNEL);
9821da177e4SLinus Torvalds 	if (!scon)
983b77a493bSEric Paris 		goto out;
9841da177e4SLinus Torvalds 
985b77a493bSEric Paris 	length = -ENOMEM;
98689d155efSJames Morris 	tcon = kzalloc(size + 1, GFP_KERNEL);
9871da177e4SLinus Torvalds 	if (!tcon)
9881da177e4SLinus Torvalds 		goto out;
9891da177e4SLinus Torvalds 
9901da177e4SLinus Torvalds 	length = -EINVAL;
9911da177e4SLinus Torvalds 	if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
992b77a493bSEric Paris 		goto out;
9931da177e4SLinus Torvalds 
9940619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
995b77a493bSEric Paris 	if (length)
996b77a493bSEric Paris 		goto out;
997b77a493bSEric Paris 
9980619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
999b77a493bSEric Paris 	if (length)
1000b77a493bSEric Paris 		goto out;
10011da177e4SLinus Torvalds 
10020619f0f5SStephen Smalley 	length = security_change_sid(state, ssid, tsid, tclass, &newsid);
1003b77a493bSEric Paris 	if (length)
1004b77a493bSEric Paris 		goto out;
10051da177e4SLinus Torvalds 
10060619f0f5SStephen Smalley 	length = security_sid_to_context(state, newsid, &newcon, &len);
1007b77a493bSEric Paris 	if (length)
1008b77a493bSEric Paris 		goto out;
10091da177e4SLinus Torvalds 
10101da177e4SLinus Torvalds 	length = -ERANGE;
1011b77a493bSEric Paris 	if (len > SIMPLE_TRANSACTION_LIMIT)
1012b77a493bSEric Paris 		goto out;
10131da177e4SLinus Torvalds 
10141da177e4SLinus Torvalds 	memcpy(buf, newcon, len);
10151da177e4SLinus Torvalds 	length = len;
10161da177e4SLinus Torvalds out:
1017b77a493bSEric Paris 	kfree(newcon);
1018b77a493bSEric Paris 	kfree(tcon);
10191da177e4SLinus Torvalds 	kfree(scon);
10201da177e4SLinus Torvalds 	return length;
10211da177e4SLinus Torvalds }
10221da177e4SLinus Torvalds 
10231da177e4SLinus Torvalds static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
10241da177e4SLinus Torvalds {
10250619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
10260619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
1027b77a493bSEric Paris 	char *con = NULL, *user = NULL, *ptr;
1028b77a493bSEric Paris 	u32 sid, *sids = NULL;
10291da177e4SLinus Torvalds 	ssize_t length;
10301da177e4SLinus Torvalds 	char *newcon;
10311da177e4SLinus Torvalds 	int i, rc;
10321da177e4SLinus Torvalds 	u32 len, nsids;
10331da177e4SLinus Torvalds 
10346b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
10356b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
1036be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__COMPUTE_USER,
1037be0554c9SStephen Smalley 			      NULL);
10381da177e4SLinus Torvalds 	if (length)
10396eab04a8SJustin P. Mattock 		goto out;
10401da177e4SLinus Torvalds 
10411da177e4SLinus Torvalds 	length = -ENOMEM;
104289d155efSJames Morris 	con = kzalloc(size + 1, GFP_KERNEL);
10431da177e4SLinus Torvalds 	if (!con)
10446eab04a8SJustin P. Mattock 		goto out;
10451da177e4SLinus Torvalds 
1046b77a493bSEric Paris 	length = -ENOMEM;
104789d155efSJames Morris 	user = kzalloc(size + 1, GFP_KERNEL);
10481da177e4SLinus Torvalds 	if (!user)
10491da177e4SLinus Torvalds 		goto out;
10501da177e4SLinus Torvalds 
10511da177e4SLinus Torvalds 	length = -EINVAL;
10521da177e4SLinus Torvalds 	if (sscanf(buf, "%s %s", con, user) != 2)
1053b77a493bSEric Paris 		goto out;
10541da177e4SLinus Torvalds 
10550619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, con, &sid, GFP_KERNEL);
1056b77a493bSEric Paris 	if (length)
1057b77a493bSEric Paris 		goto out;
10581da177e4SLinus Torvalds 
10590619f0f5SStephen Smalley 	length = security_get_user_sids(state, sid, user, &sids, &nsids);
1060b77a493bSEric Paris 	if (length)
1061b77a493bSEric Paris 		goto out;
10621da177e4SLinus Torvalds 
10631da177e4SLinus Torvalds 	length = sprintf(buf, "%u", nsids) + 1;
10641da177e4SLinus Torvalds 	ptr = buf + length;
10651da177e4SLinus Torvalds 	for (i = 0; i < nsids; i++) {
10660619f0f5SStephen Smalley 		rc = security_sid_to_context(state, sids[i], &newcon, &len);
10671da177e4SLinus Torvalds 		if (rc) {
10681da177e4SLinus Torvalds 			length = rc;
1069b77a493bSEric Paris 			goto out;
10701da177e4SLinus Torvalds 		}
10711da177e4SLinus Torvalds 		if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
10721da177e4SLinus Torvalds 			kfree(newcon);
10731da177e4SLinus Torvalds 			length = -ERANGE;
1074b77a493bSEric Paris 			goto out;
10751da177e4SLinus Torvalds 		}
10761da177e4SLinus Torvalds 		memcpy(ptr, newcon, len);
10771da177e4SLinus Torvalds 		kfree(newcon);
10781da177e4SLinus Torvalds 		ptr += len;
10791da177e4SLinus Torvalds 		length += len;
10801da177e4SLinus Torvalds 	}
10811da177e4SLinus Torvalds out:
1082b77a493bSEric Paris 	kfree(sids);
1083b77a493bSEric Paris 	kfree(user);
10841da177e4SLinus Torvalds 	kfree(con);
10851da177e4SLinus Torvalds 	return length;
10861da177e4SLinus Torvalds }
10871da177e4SLinus Torvalds 
10881da177e4SLinus Torvalds static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
10891da177e4SLinus Torvalds {
10900619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
10910619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
1092b77a493bSEric Paris 	char *scon = NULL, *tcon = NULL;
10931da177e4SLinus Torvalds 	u32 ssid, tsid, newsid;
10941da177e4SLinus Torvalds 	u16 tclass;
10951da177e4SLinus Torvalds 	ssize_t length;
1096b77a493bSEric Paris 	char *newcon = NULL;
10971da177e4SLinus Torvalds 	u32 len;
10981da177e4SLinus Torvalds 
10996b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
11006b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
1101be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__COMPUTE_MEMBER,
1102be0554c9SStephen Smalley 			      NULL);
11031da177e4SLinus Torvalds 	if (length)
1104b77a493bSEric Paris 		goto out;
11051da177e4SLinus Torvalds 
11061da177e4SLinus Torvalds 	length = -ENOMEM;
110789d155efSJames Morris 	scon = kzalloc(size + 1, GFP_KERNEL);
11081da177e4SLinus Torvalds 	if (!scon)
11096eab04a8SJustin P. Mattock 		goto out;
11101da177e4SLinus Torvalds 
1111b77a493bSEric Paris 	length = -ENOMEM;
111289d155efSJames Morris 	tcon = kzalloc(size + 1, GFP_KERNEL);
11131da177e4SLinus Torvalds 	if (!tcon)
11141da177e4SLinus Torvalds 		goto out;
11151da177e4SLinus Torvalds 
11161da177e4SLinus Torvalds 	length = -EINVAL;
11171da177e4SLinus Torvalds 	if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
1118b77a493bSEric Paris 		goto out;
11191da177e4SLinus Torvalds 
11200619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
1121b77a493bSEric Paris 	if (length)
1122b77a493bSEric Paris 		goto out;
1123b77a493bSEric Paris 
11240619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
1125b77a493bSEric Paris 	if (length)
1126b77a493bSEric Paris 		goto out;
11271da177e4SLinus Torvalds 
11280619f0f5SStephen Smalley 	length = security_member_sid(state, ssid, tsid, tclass, &newsid);
1129b77a493bSEric Paris 	if (length)
1130b77a493bSEric Paris 		goto out;
11311da177e4SLinus Torvalds 
11320619f0f5SStephen Smalley 	length = security_sid_to_context(state, newsid, &newcon, &len);
1133b77a493bSEric Paris 	if (length)
1134b77a493bSEric Paris 		goto out;
11351da177e4SLinus Torvalds 
1136b77a493bSEric Paris 	length = -ERANGE;
11371da177e4SLinus Torvalds 	if (len > SIMPLE_TRANSACTION_LIMIT) {
1138f8b69a5fSpeter enderborg 		pr_err("SELinux: %s:  context size (%u) exceeds "
1139744ba35eSEric Paris 			"payload max\n", __func__, len);
1140b77a493bSEric Paris 		goto out;
11411da177e4SLinus Torvalds 	}
11421da177e4SLinus Torvalds 
11431da177e4SLinus Torvalds 	memcpy(buf, newcon, len);
11441da177e4SLinus Torvalds 	length = len;
11451da177e4SLinus Torvalds out:
1146b77a493bSEric Paris 	kfree(newcon);
1147b77a493bSEric Paris 	kfree(tcon);
11481da177e4SLinus Torvalds 	kfree(scon);
11491da177e4SLinus Torvalds 	return length;
11501da177e4SLinus Torvalds }
11511da177e4SLinus Torvalds 
11521da177e4SLinus Torvalds static struct inode *sel_make_inode(struct super_block *sb, int mode)
11531da177e4SLinus Torvalds {
11541da177e4SLinus Torvalds 	struct inode *ret = new_inode(sb);
11551da177e4SLinus Torvalds 
11561da177e4SLinus Torvalds 	if (ret) {
11571da177e4SLinus Torvalds 		ret->i_mode = mode;
1158078cd827SDeepa Dinamani 		ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret);
11591da177e4SLinus Torvalds 	}
11601da177e4SLinus Torvalds 	return ret;
11611da177e4SLinus Torvalds }
11621da177e4SLinus Torvalds 
11631da177e4SLinus Torvalds static ssize_t sel_read_bool(struct file *filep, char __user *buf,
11641da177e4SLinus Torvalds 			     size_t count, loff_t *ppos)
11651da177e4SLinus Torvalds {
11660619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
11671da177e4SLinus Torvalds 	char *page = NULL;
11681da177e4SLinus Torvalds 	ssize_t length;
11691da177e4SLinus Torvalds 	ssize_t ret;
11701da177e4SLinus Torvalds 	int cur_enforcing;
1171496ad9aaSAl Viro 	unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
1172d313f948SStephen Smalley 	const char *name = filep->f_path.dentry->d_name.name;
11731da177e4SLinus Torvalds 
11740619f0f5SStephen Smalley 	mutex_lock(&fsi->mutex);
11751da177e4SLinus Torvalds 
1176d313f948SStephen Smalley 	ret = -EINVAL;
11770619f0f5SStephen Smalley 	if (index >= fsi->bool_num || strcmp(name,
11780619f0f5SStephen Smalley 					     fsi->bool_pending_names[index]))
11790da74120SJann Horn 		goto out_unlock;
11801da177e4SLinus Torvalds 
11811da177e4SLinus Torvalds 	ret = -ENOMEM;
1182b77a493bSEric Paris 	page = (char *)get_zeroed_page(GFP_KERNEL);
1183b77a493bSEric Paris 	if (!page)
11840da74120SJann Horn 		goto out_unlock;
11851da177e4SLinus Torvalds 
11860619f0f5SStephen Smalley 	cur_enforcing = security_get_bool_value(fsi->state, index);
11871da177e4SLinus Torvalds 	if (cur_enforcing < 0) {
11881da177e4SLinus Torvalds 		ret = cur_enforcing;
11890da74120SJann Horn 		goto out_unlock;
11901da177e4SLinus Torvalds 	}
11911da177e4SLinus Torvalds 	length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
11920619f0f5SStephen Smalley 			  fsi->bool_pending_values[index]);
11930619f0f5SStephen Smalley 	mutex_unlock(&fsi->mutex);
11940da74120SJann Horn 	ret = simple_read_from_buffer(buf, count, ppos, page, length);
11950da74120SJann Horn out_free:
11961da177e4SLinus Torvalds 	free_page((unsigned long)page);
11971da177e4SLinus Torvalds 	return ret;
11980da74120SJann Horn 
11990da74120SJann Horn out_unlock:
12000da74120SJann Horn 	mutex_unlock(&fsi->mutex);
12010da74120SJann Horn 	goto out_free;
12021da177e4SLinus Torvalds }
12031da177e4SLinus Torvalds 
12041da177e4SLinus Torvalds static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
12051da177e4SLinus Torvalds 			      size_t count, loff_t *ppos)
12061da177e4SLinus Torvalds {
12070619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
12081da177e4SLinus Torvalds 	char *page = NULL;
1209d313f948SStephen Smalley 	ssize_t length;
12101da177e4SLinus Torvalds 	int new_value;
1211496ad9aaSAl Viro 	unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
1212d313f948SStephen Smalley 	const char *name = filep->f_path.dentry->d_name.name;
12131da177e4SLinus Torvalds 
12140da74120SJann Horn 	if (count >= PAGE_SIZE)
12150da74120SJann Horn 		return -ENOMEM;
12160da74120SJann Horn 
12170da74120SJann Horn 	/* No partial writes. */
12180da74120SJann Horn 	if (*ppos != 0)
12190da74120SJann Horn 		return -EINVAL;
12200da74120SJann Horn 
12210da74120SJann Horn 	page = memdup_user_nul(buf, count);
12220da74120SJann Horn 	if (IS_ERR(page))
12230da74120SJann Horn 		return PTR_ERR(page);
12240da74120SJann Horn 
12250619f0f5SStephen Smalley 	mutex_lock(&fsi->mutex);
12261da177e4SLinus Torvalds 
12276b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
12286b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
1229be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__SETBOOL,
1230be0554c9SStephen Smalley 			      NULL);
12311da177e4SLinus Torvalds 	if (length)
12321da177e4SLinus Torvalds 		goto out;
12331da177e4SLinus Torvalds 
1234d313f948SStephen Smalley 	length = -EINVAL;
12350619f0f5SStephen Smalley 	if (index >= fsi->bool_num || strcmp(name,
12360619f0f5SStephen Smalley 					     fsi->bool_pending_names[index]))
1237d313f948SStephen Smalley 		goto out;
1238d313f948SStephen Smalley 
12391da177e4SLinus Torvalds 	length = -EINVAL;
12401da177e4SLinus Torvalds 	if (sscanf(page, "%d", &new_value) != 1)
12411da177e4SLinus Torvalds 		goto out;
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds 	if (new_value)
12441da177e4SLinus Torvalds 		new_value = 1;
12451da177e4SLinus Torvalds 
12460619f0f5SStephen Smalley 	fsi->bool_pending_values[index] = new_value;
12471da177e4SLinus Torvalds 	length = count;
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds out:
12500619f0f5SStephen Smalley 	mutex_unlock(&fsi->mutex);
12518365a719SAl Viro 	kfree(page);
12521da177e4SLinus Torvalds 	return length;
12531da177e4SLinus Torvalds }
12541da177e4SLinus Torvalds 
12559c2e08c5SArjan van de Ven static const struct file_operations sel_bool_ops = {
12561da177e4SLinus Torvalds 	.read		= sel_read_bool,
12571da177e4SLinus Torvalds 	.write		= sel_write_bool,
125857a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
12591da177e4SLinus Torvalds };
12601da177e4SLinus Torvalds 
12611da177e4SLinus Torvalds static ssize_t sel_commit_bools_write(struct file *filep,
12621da177e4SLinus Torvalds 				      const char __user *buf,
12631da177e4SLinus Torvalds 				      size_t count, loff_t *ppos)
12641da177e4SLinus Torvalds {
12650619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
12661da177e4SLinus Torvalds 	char *page = NULL;
1267d313f948SStephen Smalley 	ssize_t length;
12681da177e4SLinus Torvalds 	int new_value;
12691da177e4SLinus Torvalds 
12700da74120SJann Horn 	if (count >= PAGE_SIZE)
12710da74120SJann Horn 		return -ENOMEM;
12720da74120SJann Horn 
12730da74120SJann Horn 	/* No partial writes. */
12740da74120SJann Horn 	if (*ppos != 0)
12750da74120SJann Horn 		return -EINVAL;
12760da74120SJann Horn 
12770da74120SJann Horn 	page = memdup_user_nul(buf, count);
12780da74120SJann Horn 	if (IS_ERR(page))
12790da74120SJann Horn 		return PTR_ERR(page);
12800da74120SJann Horn 
12810619f0f5SStephen Smalley 	mutex_lock(&fsi->mutex);
12821da177e4SLinus Torvalds 
12836b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
12846b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
1285be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__SETBOOL,
1286be0554c9SStephen Smalley 			      NULL);
12871da177e4SLinus Torvalds 	if (length)
12881da177e4SLinus Torvalds 		goto out;
12891da177e4SLinus Torvalds 
12901da177e4SLinus Torvalds 	length = -EINVAL;
12911da177e4SLinus Torvalds 	if (sscanf(page, "%d", &new_value) != 1)
12921da177e4SLinus Torvalds 		goto out;
12931da177e4SLinus Torvalds 
1294b77a493bSEric Paris 	length = 0;
12950619f0f5SStephen Smalley 	if (new_value && fsi->bool_pending_values)
12960619f0f5SStephen Smalley 		length = security_set_bools(fsi->state, fsi->bool_num,
12970619f0f5SStephen Smalley 					    fsi->bool_pending_values);
12981da177e4SLinus Torvalds 
1299b77a493bSEric Paris 	if (!length)
13001da177e4SLinus Torvalds 		length = count;
13011da177e4SLinus Torvalds 
13021da177e4SLinus Torvalds out:
13030619f0f5SStephen Smalley 	mutex_unlock(&fsi->mutex);
13048365a719SAl Viro 	kfree(page);
13051da177e4SLinus Torvalds 	return length;
13061da177e4SLinus Torvalds }
13071da177e4SLinus Torvalds 
13089c2e08c5SArjan van de Ven static const struct file_operations sel_commit_bools_ops = {
13091da177e4SLinus Torvalds 	.write		= sel_commit_bools_write,
131057a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
13111da177e4SLinus Torvalds };
13121da177e4SLinus Torvalds 
13130c92d7c7SChristopher J. PeBenito static void sel_remove_entries(struct dentry *de)
13141da177e4SLinus Torvalds {
1315ad52184bSAl Viro 	d_genocide(de);
1316ad52184bSAl Viro 	shrink_dcache_parent(de);
13171da177e4SLinus Torvalds }
13181da177e4SLinus Torvalds 
13191da177e4SLinus Torvalds #define BOOL_DIR_NAME "booleans"
13201da177e4SLinus Torvalds 
13210619f0f5SStephen Smalley static int sel_make_bools(struct selinux_fs_info *fsi)
13221da177e4SLinus Torvalds {
1323b77a493bSEric Paris 	int i, ret;
13241da177e4SLinus Torvalds 	ssize_t len;
13251da177e4SLinus Torvalds 	struct dentry *dentry = NULL;
13260619f0f5SStephen Smalley 	struct dentry *dir = fsi->bool_dir;
13271da177e4SLinus Torvalds 	struct inode *inode = NULL;
13281da177e4SLinus Torvalds 	struct inode_security_struct *isec;
13291da177e4SLinus Torvalds 	char **names = NULL, *page;
13301da177e4SLinus Torvalds 	int num;
13311da177e4SLinus Torvalds 	int *values = NULL;
13321da177e4SLinus Torvalds 	u32 sid;
13331da177e4SLinus Torvalds 
13341da177e4SLinus Torvalds 	/* remove any existing files */
13350619f0f5SStephen Smalley 	for (i = 0; i < fsi->bool_num; i++)
13360619f0f5SStephen Smalley 		kfree(fsi->bool_pending_names[i]);
13370619f0f5SStephen Smalley 	kfree(fsi->bool_pending_names);
13380619f0f5SStephen Smalley 	kfree(fsi->bool_pending_values);
13390619f0f5SStephen Smalley 	fsi->bool_num = 0;
13400619f0f5SStephen Smalley 	fsi->bool_pending_names = NULL;
13410619f0f5SStephen Smalley 	fsi->bool_pending_values = NULL;
13421da177e4SLinus Torvalds 
13430c92d7c7SChristopher J. PeBenito 	sel_remove_entries(dir);
13441da177e4SLinus Torvalds 
1345b77a493bSEric Paris 	ret = -ENOMEM;
13461872981bSEric Paris 	page = (char *)get_zeroed_page(GFP_KERNEL);
13471872981bSEric Paris 	if (!page)
1348b77a493bSEric Paris 		goto out;
13491da177e4SLinus Torvalds 
13500619f0f5SStephen Smalley 	ret = security_get_bools(fsi->state, &num, &names, &values);
1351b77a493bSEric Paris 	if (ret)
13521da177e4SLinus Torvalds 		goto out;
13531da177e4SLinus Torvalds 
13541da177e4SLinus Torvalds 	for (i = 0; i < num; i++) {
1355b77a493bSEric Paris 		ret = -ENOMEM;
13561da177e4SLinus Torvalds 		dentry = d_alloc_name(dir, names[i]);
1357b77a493bSEric Paris 		if (!dentry)
1358b77a493bSEric Paris 			goto out;
13591da177e4SLinus Torvalds 
1360b77a493bSEric Paris 		ret = -ENOMEM;
1361b77a493bSEric Paris 		inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
13627e4237faSnixiaoming 		if (!inode) {
13637e4237faSnixiaoming 			dput(dentry);
1364b77a493bSEric Paris 			goto out;
13657e4237faSnixiaoming 		}
1366b77a493bSEric Paris 
13671da177e4SLinus Torvalds 		ret = -ENAMETOOLONG;
1368cc1dad71SAl Viro 		len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
13697e4237faSnixiaoming 		if (len >= PAGE_SIZE) {
13707e4237faSnixiaoming 			dput(dentry);
13717e4237faSnixiaoming 			iput(inode);
1372b77a493bSEric Paris 			goto out;
13737e4237faSnixiaoming 		}
1374b77a493bSEric Paris 
137580788c22SCasey Schaufler 		isec = selinux_inode(inode);
13760619f0f5SStephen Smalley 		ret = security_genfs_sid(fsi->state, "selinuxfs", page,
1377aa8e712cSStephen Smalley 					 SECCLASS_FILE, &sid);
13784262fb51SGary Tierney 		if (ret) {
1379900fde06SGary Tierney 			pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n",
1380900fde06SGary Tierney 					   page);
1381900fde06SGary Tierney 			sid = SECINITSID_SECURITY;
13824262fb51SGary Tierney 		}
13834262fb51SGary Tierney 
13841da177e4SLinus Torvalds 		isec->sid = sid;
138542059112SAndreas Gruenbacher 		isec->initialized = LABEL_INITIALIZED;
13861da177e4SLinus Torvalds 		inode->i_fop = &sel_bool_ops;
1387bce34bc0SJames Carter 		inode->i_ino = i|SEL_BOOL_INO_OFFSET;
13881da177e4SLinus Torvalds 		d_add(dentry, inode);
13891da177e4SLinus Torvalds 	}
13900619f0f5SStephen Smalley 	fsi->bool_num = num;
13910619f0f5SStephen Smalley 	fsi->bool_pending_names = names;
13920619f0f5SStephen Smalley 	fsi->bool_pending_values = values;
1393b77a493bSEric Paris 
1394b77a493bSEric Paris 	free_page((unsigned long)page);
1395b77a493bSEric Paris 	return 0;
13961da177e4SLinus Torvalds out:
13971da177e4SLinus Torvalds 	free_page((unsigned long)page);
1398b77a493bSEric Paris 
13991da177e4SLinus Torvalds 	if (names) {
14009a5f04bfSJesper Juhl 		for (i = 0; i < num; i++)
14011da177e4SLinus Torvalds 			kfree(names[i]);
14021da177e4SLinus Torvalds 		kfree(names);
14031da177e4SLinus Torvalds 	}
140420c19e41SDavi Arnaut 	kfree(values);
14050c92d7c7SChristopher J. PeBenito 	sel_remove_entries(dir);
1406b77a493bSEric Paris 
1407b77a493bSEric Paris 	return ret;
14081da177e4SLinus Torvalds }
14091da177e4SLinus Torvalds 
14101da177e4SLinus Torvalds static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
14111da177e4SLinus Torvalds 					    size_t count, loff_t *ppos)
14121da177e4SLinus Torvalds {
14136b6bc620SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
14146b6bc620SStephen Smalley 	struct selinux_state *state = fsi->state;
14151da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
14161da177e4SLinus Torvalds 	ssize_t length;
14171da177e4SLinus Torvalds 
14186b6bc620SStephen Smalley 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
14196b6bc620SStephen Smalley 			   avc_get_cache_threshold(state->avc));
14201da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
14211da177e4SLinus Torvalds }
14221da177e4SLinus Torvalds 
14231da177e4SLinus Torvalds static ssize_t sel_write_avc_cache_threshold(struct file *file,
14241da177e4SLinus Torvalds 					     const char __user *buf,
14251da177e4SLinus Torvalds 					     size_t count, loff_t *ppos)
14261da177e4SLinus Torvalds 
14271da177e4SLinus Torvalds {
14286b6bc620SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
14296b6bc620SStephen Smalley 	struct selinux_state *state = fsi->state;
14308365a719SAl Viro 	char *page;
14311da177e4SLinus Torvalds 	ssize_t ret;
1432309c5fadSHeinrich Schuchardt 	unsigned int new_value;
14331da177e4SLinus Torvalds 
14346b6bc620SStephen Smalley 	ret = avc_has_perm(&selinux_state,
14356b6bc620SStephen Smalley 			   current_sid(), SECINITSID_SECURITY,
1436be0554c9SStephen Smalley 			   SECCLASS_SECURITY, SECURITY__SETSECPARAM,
1437be0554c9SStephen Smalley 			   NULL);
14381da177e4SLinus Torvalds 	if (ret)
14398365a719SAl Viro 		return ret;
1440b77a493bSEric Paris 
1441b77a493bSEric Paris 	if (count >= PAGE_SIZE)
14428365a719SAl Viro 		return -ENOMEM;
1443b77a493bSEric Paris 
1444b77a493bSEric Paris 	/* No partial writes. */
1445b77a493bSEric Paris 	if (*ppos != 0)
14468365a719SAl Viro 		return -EINVAL;
1447b77a493bSEric Paris 
14488365a719SAl Viro 	page = memdup_user_nul(buf, count);
14498365a719SAl Viro 	if (IS_ERR(page))
14508365a719SAl Viro 		return PTR_ERR(page);
1451b77a493bSEric Paris 
1452b77a493bSEric Paris 	ret = -EINVAL;
1453b77a493bSEric Paris 	if (sscanf(page, "%u", &new_value) != 1)
1454b77a493bSEric Paris 		goto out;
1455b77a493bSEric Paris 
14566b6bc620SStephen Smalley 	avc_set_cache_threshold(state->avc, new_value);
1457b77a493bSEric Paris 
14581da177e4SLinus Torvalds 	ret = count;
14591da177e4SLinus Torvalds out:
14608365a719SAl Viro 	kfree(page);
14611da177e4SLinus Torvalds 	return ret;
14621da177e4SLinus Torvalds }
14631da177e4SLinus Torvalds 
14641da177e4SLinus Torvalds static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
14651da177e4SLinus Torvalds 				       size_t count, loff_t *ppos)
14661da177e4SLinus Torvalds {
14676b6bc620SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
14686b6bc620SStephen Smalley 	struct selinux_state *state = fsi->state;
14691da177e4SLinus Torvalds 	char *page;
1470b77a493bSEric Paris 	ssize_t length;
14711da177e4SLinus Torvalds 
14721da177e4SLinus Torvalds 	page = (char *)__get_free_page(GFP_KERNEL);
1473b77a493bSEric Paris 	if (!page)
1474b77a493bSEric Paris 		return -ENOMEM;
1475b77a493bSEric Paris 
14766b6bc620SStephen Smalley 	length = avc_get_hash_stats(state->avc, page);
1477b77a493bSEric Paris 	if (length >= 0)
1478b77a493bSEric Paris 		length = simple_read_from_buffer(buf, count, ppos, page, length);
14791da177e4SLinus Torvalds 	free_page((unsigned long)page);
1480b77a493bSEric Paris 
1481b77a493bSEric Paris 	return length;
14821da177e4SLinus Torvalds }
14831da177e4SLinus Torvalds 
148466f8e2f0SJeff Vander Stoep static ssize_t sel_read_sidtab_hash_stats(struct file *filp, char __user *buf,
148566f8e2f0SJeff Vander Stoep 					size_t count, loff_t *ppos)
148666f8e2f0SJeff Vander Stoep {
148766f8e2f0SJeff Vander Stoep 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
148866f8e2f0SJeff Vander Stoep 	struct selinux_state *state = fsi->state;
148966f8e2f0SJeff Vander Stoep 	char *page;
149066f8e2f0SJeff Vander Stoep 	ssize_t length;
149166f8e2f0SJeff Vander Stoep 
149266f8e2f0SJeff Vander Stoep 	page = (char *)__get_free_page(GFP_KERNEL);
149366f8e2f0SJeff Vander Stoep 	if (!page)
149466f8e2f0SJeff Vander Stoep 		return -ENOMEM;
149566f8e2f0SJeff Vander Stoep 
149666f8e2f0SJeff Vander Stoep 	length = security_sidtab_hash_stats(state, page);
149766f8e2f0SJeff Vander Stoep 	if (length >= 0)
149866f8e2f0SJeff Vander Stoep 		length = simple_read_from_buffer(buf, count, ppos, page,
149966f8e2f0SJeff Vander Stoep 						length);
150066f8e2f0SJeff Vander Stoep 	free_page((unsigned long)page);
150166f8e2f0SJeff Vander Stoep 
150266f8e2f0SJeff Vander Stoep 	return length;
150366f8e2f0SJeff Vander Stoep }
150466f8e2f0SJeff Vander Stoep 
150566f8e2f0SJeff Vander Stoep static const struct file_operations sel_sidtab_hash_stats_ops = {
150666f8e2f0SJeff Vander Stoep 	.read		= sel_read_sidtab_hash_stats,
150766f8e2f0SJeff Vander Stoep 	.llseek		= generic_file_llseek,
150866f8e2f0SJeff Vander Stoep };
150966f8e2f0SJeff Vander Stoep 
15109c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_threshold_ops = {
15111da177e4SLinus Torvalds 	.read		= sel_read_avc_cache_threshold,
15121da177e4SLinus Torvalds 	.write		= sel_write_avc_cache_threshold,
151357a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
15141da177e4SLinus Torvalds };
15151da177e4SLinus Torvalds 
15169c2e08c5SArjan van de Ven static const struct file_operations sel_avc_hash_stats_ops = {
15171da177e4SLinus Torvalds 	.read		= sel_read_avc_hash_stats,
151857a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
15191da177e4SLinus Torvalds };
15201da177e4SLinus Torvalds 
15211da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
15221da177e4SLinus Torvalds static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
15231da177e4SLinus Torvalds {
15241da177e4SLinus Torvalds 	int cpu;
15251da177e4SLinus Torvalds 
15264f4b6c1aSRusty Russell 	for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
15271da177e4SLinus Torvalds 		if (!cpu_possible(cpu))
15281da177e4SLinus Torvalds 			continue;
15291da177e4SLinus Torvalds 		*idx = cpu + 1;
15301da177e4SLinus Torvalds 		return &per_cpu(avc_cache_stats, cpu);
15311da177e4SLinus Torvalds 	}
15321da177e4SLinus Torvalds 	return NULL;
15331da177e4SLinus Torvalds }
15341da177e4SLinus Torvalds 
15351da177e4SLinus Torvalds static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
15361da177e4SLinus Torvalds {
15371da177e4SLinus Torvalds 	loff_t n = *pos - 1;
15381da177e4SLinus Torvalds 
15391da177e4SLinus Torvalds 	if (*pos == 0)
15401da177e4SLinus Torvalds 		return SEQ_START_TOKEN;
15411da177e4SLinus Torvalds 
15421da177e4SLinus Torvalds 	return sel_avc_get_stat_idx(&n);
15431da177e4SLinus Torvalds }
15441da177e4SLinus Torvalds 
15451da177e4SLinus Torvalds static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
15461da177e4SLinus Torvalds {
15471da177e4SLinus Torvalds 	return sel_avc_get_stat_idx(pos);
15481da177e4SLinus Torvalds }
15491da177e4SLinus Torvalds 
15501da177e4SLinus Torvalds static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
15511da177e4SLinus Torvalds {
15521da177e4SLinus Torvalds 	struct avc_cache_stats *st = v;
15531da177e4SLinus Torvalds 
1554710a0647SMarkus Elfring 	if (v == SEQ_START_TOKEN) {
1555710a0647SMarkus Elfring 		seq_puts(seq,
1556710a0647SMarkus Elfring 			 "lookups hits misses allocations reclaims frees\n");
1557710a0647SMarkus Elfring 	} else {
1558257313b2SLinus Torvalds 		unsigned int lookups = st->lookups;
1559257313b2SLinus Torvalds 		unsigned int misses = st->misses;
1560257313b2SLinus Torvalds 		unsigned int hits = lookups - misses;
1561257313b2SLinus Torvalds 		seq_printf(seq, "%u %u %u %u %u %u\n", lookups,
1562257313b2SLinus Torvalds 			   hits, misses, st->allocations,
15631da177e4SLinus Torvalds 			   st->reclaims, st->frees);
1564257313b2SLinus Torvalds 	}
15651da177e4SLinus Torvalds 	return 0;
15661da177e4SLinus Torvalds }
15671da177e4SLinus Torvalds 
15681da177e4SLinus Torvalds static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
15691da177e4SLinus Torvalds { }
15701da177e4SLinus Torvalds 
15711996a109SJan Engelhardt static const struct seq_operations sel_avc_cache_stats_seq_ops = {
15721da177e4SLinus Torvalds 	.start		= sel_avc_stats_seq_start,
15731da177e4SLinus Torvalds 	.next		= sel_avc_stats_seq_next,
15741da177e4SLinus Torvalds 	.show		= sel_avc_stats_seq_show,
15751da177e4SLinus Torvalds 	.stop		= sel_avc_stats_seq_stop,
15761da177e4SLinus Torvalds };
15771da177e4SLinus Torvalds 
15781da177e4SLinus Torvalds static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
15791da177e4SLinus Torvalds {
15801da177e4SLinus Torvalds 	return seq_open(file, &sel_avc_cache_stats_seq_ops);
15811da177e4SLinus Torvalds }
15821da177e4SLinus Torvalds 
15839c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_stats_ops = {
15841da177e4SLinus Torvalds 	.open		= sel_open_avc_cache_stats,
15851da177e4SLinus Torvalds 	.read		= seq_read,
15861da177e4SLinus Torvalds 	.llseek		= seq_lseek,
15871da177e4SLinus Torvalds 	.release	= seq_release,
15881da177e4SLinus Torvalds };
15891da177e4SLinus Torvalds #endif
15901da177e4SLinus Torvalds 
15911da177e4SLinus Torvalds static int sel_make_avc_files(struct dentry *dir)
15921da177e4SLinus Torvalds {
15930619f0f5SStephen Smalley 	struct super_block *sb = dir->d_sb;
15940619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = sb->s_fs_info;
1595b77a493bSEric Paris 	int i;
1596cda37124SEric Biggers 	static const struct tree_descr files[] = {
15971da177e4SLinus Torvalds 		{ "cache_threshold",
15981da177e4SLinus Torvalds 		  &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
15991da177e4SLinus Torvalds 		{ "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
16001da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
16011da177e4SLinus Torvalds 		{ "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
16021da177e4SLinus Torvalds #endif
16031da177e4SLinus Torvalds 	};
16041da177e4SLinus Torvalds 
16056e20a64aSNicolas Kaiser 	for (i = 0; i < ARRAY_SIZE(files); i++) {
16061da177e4SLinus Torvalds 		struct inode *inode;
16071da177e4SLinus Torvalds 		struct dentry *dentry;
16081da177e4SLinus Torvalds 
16091da177e4SLinus Torvalds 		dentry = d_alloc_name(dir, files[i].name);
1610b77a493bSEric Paris 		if (!dentry)
1611b77a493bSEric Paris 			return -ENOMEM;
16121da177e4SLinus Torvalds 
16131da177e4SLinus Torvalds 		inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
16147e4237faSnixiaoming 		if (!inode) {
16157e4237faSnixiaoming 			dput(dentry);
1616b77a493bSEric Paris 			return -ENOMEM;
16177e4237faSnixiaoming 		}
1618b77a493bSEric Paris 
16191da177e4SLinus Torvalds 		inode->i_fop = files[i].ops;
16200619f0f5SStephen Smalley 		inode->i_ino = ++fsi->last_ino;
16211da177e4SLinus Torvalds 		d_add(dentry, inode);
16221da177e4SLinus Torvalds 	}
1623b77a493bSEric Paris 
1624b77a493bSEric Paris 	return 0;
16251da177e4SLinus Torvalds }
16261da177e4SLinus Torvalds 
162766f8e2f0SJeff Vander Stoep static int sel_make_ss_files(struct dentry *dir)
162866f8e2f0SJeff Vander Stoep {
162966f8e2f0SJeff Vander Stoep 	struct super_block *sb = dir->d_sb;
163066f8e2f0SJeff Vander Stoep 	struct selinux_fs_info *fsi = sb->s_fs_info;
163166f8e2f0SJeff Vander Stoep 	int i;
163266f8e2f0SJeff Vander Stoep 	static struct tree_descr files[] = {
163366f8e2f0SJeff Vander Stoep 		{ "sidtab_hash_stats", &sel_sidtab_hash_stats_ops, S_IRUGO },
163466f8e2f0SJeff Vander Stoep 	};
163566f8e2f0SJeff Vander Stoep 
163666f8e2f0SJeff Vander Stoep 	for (i = 0; i < ARRAY_SIZE(files); i++) {
163766f8e2f0SJeff Vander Stoep 		struct inode *inode;
163866f8e2f0SJeff Vander Stoep 		struct dentry *dentry;
163966f8e2f0SJeff Vander Stoep 
164066f8e2f0SJeff Vander Stoep 		dentry = d_alloc_name(dir, files[i].name);
164166f8e2f0SJeff Vander Stoep 		if (!dentry)
164266f8e2f0SJeff Vander Stoep 			return -ENOMEM;
164366f8e2f0SJeff Vander Stoep 
164466f8e2f0SJeff Vander Stoep 		inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
164566f8e2f0SJeff Vander Stoep 		if (!inode) {
164666f8e2f0SJeff Vander Stoep 			dput(dentry);
164766f8e2f0SJeff Vander Stoep 			return -ENOMEM;
164866f8e2f0SJeff Vander Stoep 		}
164966f8e2f0SJeff Vander Stoep 
165066f8e2f0SJeff Vander Stoep 		inode->i_fop = files[i].ops;
165166f8e2f0SJeff Vander Stoep 		inode->i_ino = ++fsi->last_ino;
165266f8e2f0SJeff Vander Stoep 		d_add(dentry, inode);
165366f8e2f0SJeff Vander Stoep 	}
165466f8e2f0SJeff Vander Stoep 
165566f8e2f0SJeff Vander Stoep 	return 0;
165666f8e2f0SJeff Vander Stoep }
165766f8e2f0SJeff Vander Stoep 
1658f0ee2e46SJames Carter static ssize_t sel_read_initcon(struct file *file, char __user *buf,
1659f0ee2e46SJames Carter 				size_t count, loff_t *ppos)
1660f0ee2e46SJames Carter {
16610619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1662f0ee2e46SJames Carter 	char *con;
1663f0ee2e46SJames Carter 	u32 sid, len;
1664f0ee2e46SJames Carter 	ssize_t ret;
1665f0ee2e46SJames Carter 
1666496ad9aaSAl Viro 	sid = file_inode(file)->i_ino&SEL_INO_MASK;
16670619f0f5SStephen Smalley 	ret = security_sid_to_context(fsi->state, sid, &con, &len);
1668b77a493bSEric Paris 	if (ret)
1669f0ee2e46SJames Carter 		return ret;
1670f0ee2e46SJames Carter 
1671f0ee2e46SJames Carter 	ret = simple_read_from_buffer(buf, count, ppos, con, len);
1672f0ee2e46SJames Carter 	kfree(con);
1673f0ee2e46SJames Carter 	return ret;
1674f0ee2e46SJames Carter }
1675f0ee2e46SJames Carter 
1676f0ee2e46SJames Carter static const struct file_operations sel_initcon_ops = {
1677f0ee2e46SJames Carter 	.read		= sel_read_initcon,
167857a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
1679f0ee2e46SJames Carter };
1680f0ee2e46SJames Carter 
1681f0ee2e46SJames Carter static int sel_make_initcon_files(struct dentry *dir)
1682f0ee2e46SJames Carter {
1683b77a493bSEric Paris 	int i;
1684f0ee2e46SJames Carter 
1685f0ee2e46SJames Carter 	for (i = 1; i <= SECINITSID_NUM; i++) {
1686f0ee2e46SJames Carter 		struct inode *inode;
1687f0ee2e46SJames Carter 		struct dentry *dentry;
1688f0ee2e46SJames Carter 		dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
1689b77a493bSEric Paris 		if (!dentry)
1690b77a493bSEric Paris 			return -ENOMEM;
1691f0ee2e46SJames Carter 
1692f0ee2e46SJames Carter 		inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
16937e4237faSnixiaoming 		if (!inode) {
16947e4237faSnixiaoming 			dput(dentry);
1695b77a493bSEric Paris 			return -ENOMEM;
16967e4237faSnixiaoming 		}
1697b77a493bSEric Paris 
1698f0ee2e46SJames Carter 		inode->i_fop = &sel_initcon_ops;
1699f0ee2e46SJames Carter 		inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1700f0ee2e46SJames Carter 		d_add(dentry, inode);
1701f0ee2e46SJames Carter 	}
1702b77a493bSEric Paris 
1703b77a493bSEric Paris 	return 0;
1704f0ee2e46SJames Carter }
1705f0ee2e46SJames Carter 
1706e47c8fc5SChristopher J. PeBenito static inline unsigned long sel_class_to_ino(u16 class)
1707e47c8fc5SChristopher J. PeBenito {
1708e47c8fc5SChristopher J. PeBenito 	return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1709e47c8fc5SChristopher J. PeBenito }
1710e47c8fc5SChristopher J. PeBenito 
1711e47c8fc5SChristopher J. PeBenito static inline u16 sel_ino_to_class(unsigned long ino)
1712e47c8fc5SChristopher J. PeBenito {
171392ae9e82SEric Paris 	return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1);
1714e47c8fc5SChristopher J. PeBenito }
1715e47c8fc5SChristopher J. PeBenito 
1716e47c8fc5SChristopher J. PeBenito static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1717e47c8fc5SChristopher J. PeBenito {
1718e47c8fc5SChristopher J. PeBenito 	return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1719e47c8fc5SChristopher J. PeBenito }
1720e47c8fc5SChristopher J. PeBenito 
1721e47c8fc5SChristopher J. PeBenito static inline u32 sel_ino_to_perm(unsigned long ino)
1722e47c8fc5SChristopher J. PeBenito {
1723e47c8fc5SChristopher J. PeBenito 	return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1724e47c8fc5SChristopher J. PeBenito }
1725e47c8fc5SChristopher J. PeBenito 
1726e47c8fc5SChristopher J. PeBenito static ssize_t sel_read_class(struct file *file, char __user *buf,
1727e47c8fc5SChristopher J. PeBenito 				size_t count, loff_t *ppos)
1728e47c8fc5SChristopher J. PeBenito {
1729496ad9aaSAl Viro 	unsigned long ino = file_inode(file)->i_ino;
1730cc1dad71SAl Viro 	char res[TMPBUFLEN];
1731cc1dad71SAl Viro 	ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_class(ino));
1732cc1dad71SAl Viro 	return simple_read_from_buffer(buf, count, ppos, res, len);
1733e47c8fc5SChristopher J. PeBenito }
1734e47c8fc5SChristopher J. PeBenito 
1735e47c8fc5SChristopher J. PeBenito static const struct file_operations sel_class_ops = {
1736e47c8fc5SChristopher J. PeBenito 	.read		= sel_read_class,
173757a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
1738e47c8fc5SChristopher J. PeBenito };
1739e47c8fc5SChristopher J. PeBenito 
1740e47c8fc5SChristopher J. PeBenito static ssize_t sel_read_perm(struct file *file, char __user *buf,
1741e47c8fc5SChristopher J. PeBenito 				size_t count, loff_t *ppos)
1742e47c8fc5SChristopher J. PeBenito {
1743496ad9aaSAl Viro 	unsigned long ino = file_inode(file)->i_ino;
1744cc1dad71SAl Viro 	char res[TMPBUFLEN];
1745cc1dad71SAl Viro 	ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino));
1746cc1dad71SAl Viro 	return simple_read_from_buffer(buf, count, ppos, res, len);
1747e47c8fc5SChristopher J. PeBenito }
1748e47c8fc5SChristopher J. PeBenito 
1749e47c8fc5SChristopher J. PeBenito static const struct file_operations sel_perm_ops = {
1750e47c8fc5SChristopher J. PeBenito 	.read		= sel_read_perm,
175157a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
1752e47c8fc5SChristopher J. PeBenito };
1753e47c8fc5SChristopher J. PeBenito 
17543bb56b25SPaul Moore static ssize_t sel_read_policycap(struct file *file, char __user *buf,
17553bb56b25SPaul Moore 				  size_t count, loff_t *ppos)
17563bb56b25SPaul Moore {
17570619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
17583bb56b25SPaul Moore 	int value;
17593bb56b25SPaul Moore 	char tmpbuf[TMPBUFLEN];
17603bb56b25SPaul Moore 	ssize_t length;
1761496ad9aaSAl Viro 	unsigned long i_ino = file_inode(file)->i_ino;
17623bb56b25SPaul Moore 
17630619f0f5SStephen Smalley 	value = security_policycap_supported(fsi->state, i_ino & SEL_INO_MASK);
17643bb56b25SPaul Moore 	length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
17653bb56b25SPaul Moore 
17663bb56b25SPaul Moore 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
17673bb56b25SPaul Moore }
17683bb56b25SPaul Moore 
17693bb56b25SPaul Moore static const struct file_operations sel_policycap_ops = {
17703bb56b25SPaul Moore 	.read		= sel_read_policycap,
177157a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
17723bb56b25SPaul Moore };
17733bb56b25SPaul Moore 
1774e47c8fc5SChristopher J. PeBenito static int sel_make_perm_files(char *objclass, int classvalue,
1775e47c8fc5SChristopher J. PeBenito 				struct dentry *dir)
1776e47c8fc5SChristopher J. PeBenito {
17770619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = dir->d_sb->s_fs_info;
1778b77a493bSEric Paris 	int i, rc, nperms;
1779e47c8fc5SChristopher J. PeBenito 	char **perms;
1780e47c8fc5SChristopher J. PeBenito 
17810619f0f5SStephen Smalley 	rc = security_get_permissions(fsi->state, objclass, &perms, &nperms);
1782e47c8fc5SChristopher J. PeBenito 	if (rc)
1783b77a493bSEric Paris 		return rc;
1784e47c8fc5SChristopher J. PeBenito 
1785e47c8fc5SChristopher J. PeBenito 	for (i = 0; i < nperms; i++) {
1786e47c8fc5SChristopher J. PeBenito 		struct inode *inode;
1787e47c8fc5SChristopher J. PeBenito 		struct dentry *dentry;
1788e47c8fc5SChristopher J. PeBenito 
1789b77a493bSEric Paris 		rc = -ENOMEM;
1790e47c8fc5SChristopher J. PeBenito 		dentry = d_alloc_name(dir, perms[i]);
1791b77a493bSEric Paris 		if (!dentry)
1792b77a493bSEric Paris 			goto out;
1793e47c8fc5SChristopher J. PeBenito 
1794e47c8fc5SChristopher J. PeBenito 		rc = -ENOMEM;
1795b77a493bSEric Paris 		inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
17967e4237faSnixiaoming 		if (!inode) {
17977e4237faSnixiaoming 			dput(dentry);
1798b77a493bSEric Paris 			goto out;
17997e4237faSnixiaoming 		}
1800b77a493bSEric Paris 
1801e47c8fc5SChristopher J. PeBenito 		inode->i_fop = &sel_perm_ops;
1802e47c8fc5SChristopher J. PeBenito 		/* i+1 since perm values are 1-indexed */
1803e47c8fc5SChristopher J. PeBenito 		inode->i_ino = sel_perm_to_ino(classvalue, i + 1);
1804e47c8fc5SChristopher J. PeBenito 		d_add(dentry, inode);
1805e47c8fc5SChristopher J. PeBenito 	}
1806b77a493bSEric Paris 	rc = 0;
1807b77a493bSEric Paris out:
1808e47c8fc5SChristopher J. PeBenito 	for (i = 0; i < nperms; i++)
1809e47c8fc5SChristopher J. PeBenito 		kfree(perms[i]);
1810e47c8fc5SChristopher J. PeBenito 	kfree(perms);
1811e47c8fc5SChristopher J. PeBenito 	return rc;
1812e47c8fc5SChristopher J. PeBenito }
1813e47c8fc5SChristopher J. PeBenito 
1814e47c8fc5SChristopher J. PeBenito static int sel_make_class_dir_entries(char *classname, int index,
1815e47c8fc5SChristopher J. PeBenito 					struct dentry *dir)
1816e47c8fc5SChristopher J. PeBenito {
18170619f0f5SStephen Smalley 	struct super_block *sb = dir->d_sb;
18180619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = sb->s_fs_info;
1819e47c8fc5SChristopher J. PeBenito 	struct dentry *dentry = NULL;
1820e47c8fc5SChristopher J. PeBenito 	struct inode *inode = NULL;
1821e47c8fc5SChristopher J. PeBenito 	int rc;
1822e47c8fc5SChristopher J. PeBenito 
1823e47c8fc5SChristopher J. PeBenito 	dentry = d_alloc_name(dir, "index");
1824b77a493bSEric Paris 	if (!dentry)
1825b77a493bSEric Paris 		return -ENOMEM;
1826e47c8fc5SChristopher J. PeBenito 
1827e47c8fc5SChristopher J. PeBenito 	inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
18287e4237faSnixiaoming 	if (!inode) {
18297e4237faSnixiaoming 		dput(dentry);
1830b77a493bSEric Paris 		return -ENOMEM;
18317e4237faSnixiaoming 	}
1832e47c8fc5SChristopher J. PeBenito 
1833e47c8fc5SChristopher J. PeBenito 	inode->i_fop = &sel_class_ops;
1834e47c8fc5SChristopher J. PeBenito 	inode->i_ino = sel_class_to_ino(index);
1835e47c8fc5SChristopher J. PeBenito 	d_add(dentry, inode);
1836e47c8fc5SChristopher J. PeBenito 
18370619f0f5SStephen Smalley 	dentry = sel_make_dir(dir, "perms", &fsi->last_class_ino);
1838a1c2aa1eSAl Viro 	if (IS_ERR(dentry))
1839a1c2aa1eSAl Viro 		return PTR_ERR(dentry);
1840e47c8fc5SChristopher J. PeBenito 
1841e47c8fc5SChristopher J. PeBenito 	rc = sel_make_perm_files(classname, index, dentry);
1842e47c8fc5SChristopher J. PeBenito 
1843e47c8fc5SChristopher J. PeBenito 	return rc;
1844e47c8fc5SChristopher J. PeBenito }
1845e47c8fc5SChristopher J. PeBenito 
18460619f0f5SStephen Smalley static int sel_make_classes(struct selinux_fs_info *fsi)
1847e47c8fc5SChristopher J. PeBenito {
18480619f0f5SStephen Smalley 
1849b77a493bSEric Paris 	int rc, nclasses, i;
1850e47c8fc5SChristopher J. PeBenito 	char **classes;
1851e47c8fc5SChristopher J. PeBenito 
1852e47c8fc5SChristopher J. PeBenito 	/* delete any existing entries */
18530619f0f5SStephen Smalley 	sel_remove_entries(fsi->class_dir);
1854e47c8fc5SChristopher J. PeBenito 
18550619f0f5SStephen Smalley 	rc = security_get_classes(fsi->state, &classes, &nclasses);
1856b77a493bSEric Paris 	if (rc)
1857b77a493bSEric Paris 		return rc;
1858e47c8fc5SChristopher J. PeBenito 
1859e47c8fc5SChristopher J. PeBenito 	/* +2 since classes are 1-indexed */
18600619f0f5SStephen Smalley 	fsi->last_class_ino = sel_class_to_ino(nclasses + 2);
1861e47c8fc5SChristopher J. PeBenito 
1862e47c8fc5SChristopher J. PeBenito 	for (i = 0; i < nclasses; i++) {
1863e47c8fc5SChristopher J. PeBenito 		struct dentry *class_name_dir;
1864e47c8fc5SChristopher J. PeBenito 
18650619f0f5SStephen Smalley 		class_name_dir = sel_make_dir(fsi->class_dir, classes[i],
18660619f0f5SStephen Smalley 					      &fsi->last_class_ino);
1867a1c2aa1eSAl Viro 		if (IS_ERR(class_name_dir)) {
1868a1c2aa1eSAl Viro 			rc = PTR_ERR(class_name_dir);
1869b77a493bSEric Paris 			goto out;
1870a1c2aa1eSAl Viro 		}
1871e47c8fc5SChristopher J. PeBenito 
1872e47c8fc5SChristopher J. PeBenito 		/* i+1 since class values are 1-indexed */
1873e47c8fc5SChristopher J. PeBenito 		rc = sel_make_class_dir_entries(classes[i], i + 1,
1874e47c8fc5SChristopher J. PeBenito 				class_name_dir);
1875e47c8fc5SChristopher J. PeBenito 		if (rc)
1876b77a493bSEric Paris 			goto out;
1877e47c8fc5SChristopher J. PeBenito 	}
1878b77a493bSEric Paris 	rc = 0;
1879b77a493bSEric Paris out:
1880e47c8fc5SChristopher J. PeBenito 	for (i = 0; i < nclasses; i++)
1881e47c8fc5SChristopher J. PeBenito 		kfree(classes[i]);
1882e47c8fc5SChristopher J. PeBenito 	kfree(classes);
1883e47c8fc5SChristopher J. PeBenito 	return rc;
1884e47c8fc5SChristopher J. PeBenito }
1885e47c8fc5SChristopher J. PeBenito 
18860619f0f5SStephen Smalley static int sel_make_policycap(struct selinux_fs_info *fsi)
18873bb56b25SPaul Moore {
18883bb56b25SPaul Moore 	unsigned int iter;
18893bb56b25SPaul Moore 	struct dentry *dentry = NULL;
18903bb56b25SPaul Moore 	struct inode *inode = NULL;
18913bb56b25SPaul Moore 
18920619f0f5SStephen Smalley 	sel_remove_entries(fsi->policycap_dir);
18933bb56b25SPaul Moore 
18943bb56b25SPaul Moore 	for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
18954dc2fce3SStephen Smalley 		if (iter < ARRAY_SIZE(selinux_policycap_names))
18960619f0f5SStephen Smalley 			dentry = d_alloc_name(fsi->policycap_dir,
18974dc2fce3SStephen Smalley 					      selinux_policycap_names[iter]);
18983bb56b25SPaul Moore 		else
18990619f0f5SStephen Smalley 			dentry = d_alloc_name(fsi->policycap_dir, "unknown");
19003bb56b25SPaul Moore 
19013bb56b25SPaul Moore 		if (dentry == NULL)
19023bb56b25SPaul Moore 			return -ENOMEM;
19033bb56b25SPaul Moore 
19040619f0f5SStephen Smalley 		inode = sel_make_inode(fsi->sb, S_IFREG | 0444);
19057e4237faSnixiaoming 		if (inode == NULL) {
19067e4237faSnixiaoming 			dput(dentry);
19073bb56b25SPaul Moore 			return -ENOMEM;
19087e4237faSnixiaoming 		}
19093bb56b25SPaul Moore 
19103bb56b25SPaul Moore 		inode->i_fop = &sel_policycap_ops;
19113bb56b25SPaul Moore 		inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
19123bb56b25SPaul Moore 		d_add(dentry, inode);
19133bb56b25SPaul Moore 	}
19143bb56b25SPaul Moore 
19153bb56b25SPaul Moore 	return 0;
19163bb56b25SPaul Moore }
19173bb56b25SPaul Moore 
1918a1c2aa1eSAl Viro static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
19190dd4ae51SChristopher J. PeBenito 			unsigned long *ino)
19201da177e4SLinus Torvalds {
1921a1c2aa1eSAl Viro 	struct dentry *dentry = d_alloc_name(dir, name);
19221da177e4SLinus Torvalds 	struct inode *inode;
19231da177e4SLinus Torvalds 
1924a1c2aa1eSAl Viro 	if (!dentry)
1925a1c2aa1eSAl Viro 		return ERR_PTR(-ENOMEM);
1926a1c2aa1eSAl Viro 
1927a1c2aa1eSAl Viro 	inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1928a1c2aa1eSAl Viro 	if (!inode) {
1929a1c2aa1eSAl Viro 		dput(dentry);
1930a1c2aa1eSAl Viro 		return ERR_PTR(-ENOMEM);
1931a1c2aa1eSAl Viro 	}
1932b77a493bSEric Paris 
19331da177e4SLinus Torvalds 	inode->i_op = &simple_dir_inode_operations;
19341da177e4SLinus Torvalds 	inode->i_fop = &simple_dir_operations;
19350dd4ae51SChristopher J. PeBenito 	inode->i_ino = ++(*ino);
193640e906f8SJames Morris 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
1937d8c76e6fSDave Hansen 	inc_nlink(inode);
19381da177e4SLinus Torvalds 	d_add(dentry, inode);
1939edb20fb5SJames Morris 	/* bump link count on parent directory, too */
1940ce0b16ddSDavid Howells 	inc_nlink(d_inode(dir));
1941b77a493bSEric Paris 
1942a1c2aa1eSAl Viro 	return dentry;
19431da177e4SLinus Torvalds }
19441da177e4SLinus Torvalds 
19450619f0f5SStephen Smalley #define NULL_FILE_NAME "null"
19460619f0f5SStephen Smalley 
1947920f50b2SDavid Howells static int sel_fill_super(struct super_block *sb, struct fs_context *fc)
19481da177e4SLinus Torvalds {
19490619f0f5SStephen Smalley 	struct selinux_fs_info *fsi;
19501da177e4SLinus Torvalds 	int ret;
19511da177e4SLinus Torvalds 	struct dentry *dentry;
1952a1c2aa1eSAl Viro 	struct inode *inode;
19531da177e4SLinus Torvalds 	struct inode_security_struct *isec;
19541da177e4SLinus Torvalds 
1955cda37124SEric Biggers 	static const struct tree_descr selinux_files[] = {
19561da177e4SLinus Torvalds 		[SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
19571da177e4SLinus Torvalds 		[SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
1958ce9982d0SStephen Smalley 		[SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
19591da177e4SLinus Torvalds 		[SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
19601da177e4SLinus Torvalds 		[SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
19611da177e4SLinus Torvalds 		[SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
19621da177e4SLinus Torvalds 		[SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
19631da177e4SLinus Torvalds 		[SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
19641da177e4SLinus Torvalds 		[SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
19651da177e4SLinus Torvalds 		[SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
19661da177e4SLinus Torvalds 		[SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
19671da177e4SLinus Torvalds 		[SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
19681da177e4SLinus Torvalds 		[SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
19693f12070eSEric Paris 		[SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
19703f12070eSEric Paris 		[SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
197111904167SKaiGai Kohei 		[SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO},
197272e8c859SEric Paris 		[SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO},
1973f9df6458SAndrew Perepechko 		[SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops,
1974f9df6458SAndrew Perepechko 					S_IWUGO},
19751da177e4SLinus Torvalds 		/* last one */ {""}
19761da177e4SLinus Torvalds 	};
19770619f0f5SStephen Smalley 
19780619f0f5SStephen Smalley 	ret = selinux_fs_info_create(sb);
19790619f0f5SStephen Smalley 	if (ret)
19800619f0f5SStephen Smalley 		goto err;
19810619f0f5SStephen Smalley 
19821da177e4SLinus Torvalds 	ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
19831da177e4SLinus Torvalds 	if (ret)
1984161ce45aSJames Morris 		goto err;
19851da177e4SLinus Torvalds 
19860619f0f5SStephen Smalley 	fsi = sb->s_fs_info;
19870619f0f5SStephen Smalley 	fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino);
19880619f0f5SStephen Smalley 	if (IS_ERR(fsi->bool_dir)) {
19890619f0f5SStephen Smalley 		ret = PTR_ERR(fsi->bool_dir);
19900619f0f5SStephen Smalley 		fsi->bool_dir = NULL;
1991161ce45aSJames Morris 		goto err;
1992a1c2aa1eSAl Viro 	}
19931da177e4SLinus Torvalds 
1994b77a493bSEric Paris 	ret = -ENOMEM;
19951da177e4SLinus Torvalds 	dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
1996b77a493bSEric Paris 	if (!dentry)
1997161ce45aSJames Morris 		goto err;
19981da177e4SLinus Torvalds 
1999161ce45aSJames Morris 	ret = -ENOMEM;
2000b77a493bSEric Paris 	inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
20017e4237faSnixiaoming 	if (!inode) {
20027e4237faSnixiaoming 		dput(dentry);
2003161ce45aSJames Morris 		goto err;
20047e4237faSnixiaoming 	}
2005b77a493bSEric Paris 
20060619f0f5SStephen Smalley 	inode->i_ino = ++fsi->last_ino;
200780788c22SCasey Schaufler 	isec = selinux_inode(inode);
20081da177e4SLinus Torvalds 	isec->sid = SECINITSID_DEVNULL;
20091da177e4SLinus Torvalds 	isec->sclass = SECCLASS_CHR_FILE;
201042059112SAndreas Gruenbacher 	isec->initialized = LABEL_INITIALIZED;
20111da177e4SLinus Torvalds 
20121da177e4SLinus Torvalds 	init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
20131da177e4SLinus Torvalds 	d_add(dentry, inode);
20141da177e4SLinus Torvalds 
20150619f0f5SStephen Smalley 	dentry = sel_make_dir(sb->s_root, "avc", &fsi->last_ino);
2016a1c2aa1eSAl Viro 	if (IS_ERR(dentry)) {
2017a1c2aa1eSAl Viro 		ret = PTR_ERR(dentry);
2018161ce45aSJames Morris 		goto err;
2019a1c2aa1eSAl Viro 	}
20201da177e4SLinus Torvalds 
20211da177e4SLinus Torvalds 	ret = sel_make_avc_files(dentry);
202266f8e2f0SJeff Vander Stoep 
202366f8e2f0SJeff Vander Stoep 	dentry = sel_make_dir(sb->s_root, "ss", &fsi->last_ino);
202466f8e2f0SJeff Vander Stoep 	if (IS_ERR(dentry)) {
202566f8e2f0SJeff Vander Stoep 		ret = PTR_ERR(dentry);
202666f8e2f0SJeff Vander Stoep 		goto err;
202766f8e2f0SJeff Vander Stoep 	}
202866f8e2f0SJeff Vander Stoep 
202966f8e2f0SJeff Vander Stoep 	ret = sel_make_ss_files(dentry);
20301da177e4SLinus Torvalds 	if (ret)
2031161ce45aSJames Morris 		goto err;
2032f0ee2e46SJames Carter 
20330619f0f5SStephen Smalley 	dentry = sel_make_dir(sb->s_root, "initial_contexts", &fsi->last_ino);
2034a1c2aa1eSAl Viro 	if (IS_ERR(dentry)) {
2035a1c2aa1eSAl Viro 		ret = PTR_ERR(dentry);
2036f0ee2e46SJames Carter 		goto err;
2037a1c2aa1eSAl Viro 	}
2038f0ee2e46SJames Carter 
2039f0ee2e46SJames Carter 	ret = sel_make_initcon_files(dentry);
2040f0ee2e46SJames Carter 	if (ret)
2041f0ee2e46SJames Carter 		goto err;
2042f0ee2e46SJames Carter 
20430619f0f5SStephen Smalley 	fsi->class_dir = sel_make_dir(sb->s_root, "class", &fsi->last_ino);
20440619f0f5SStephen Smalley 	if (IS_ERR(fsi->class_dir)) {
20450619f0f5SStephen Smalley 		ret = PTR_ERR(fsi->class_dir);
20460619f0f5SStephen Smalley 		fsi->class_dir = NULL;
2047e47c8fc5SChristopher J. PeBenito 		goto err;
2048a1c2aa1eSAl Viro 	}
2049e47c8fc5SChristopher J. PeBenito 
20500619f0f5SStephen Smalley 	fsi->policycap_dir = sel_make_dir(sb->s_root, "policy_capabilities",
20510619f0f5SStephen Smalley 					  &fsi->last_ino);
20520619f0f5SStephen Smalley 	if (IS_ERR(fsi->policycap_dir)) {
20530619f0f5SStephen Smalley 		ret = PTR_ERR(fsi->policycap_dir);
20540619f0f5SStephen Smalley 		fsi->policycap_dir = NULL;
2055e47c8fc5SChristopher J. PeBenito 		goto err;
2056a1c2aa1eSAl Viro 	}
20570619f0f5SStephen Smalley 
20580619f0f5SStephen Smalley 	ret = sel_make_policy_nodes(fsi);
20590619f0f5SStephen Smalley 	if (ret)
20600619f0f5SStephen Smalley 		goto err;
2061b77a493bSEric Paris 	return 0;
2062161ce45aSJames Morris err:
2063f8b69a5fSpeter enderborg 	pr_err("SELinux: %s:  failed while creating inodes\n",
2064744ba35eSEric Paris 		__func__);
20650619f0f5SStephen Smalley 
20660619f0f5SStephen Smalley 	selinux_fs_info_free(sb);
20670619f0f5SStephen Smalley 
2068b77a493bSEric Paris 	return ret;
20691da177e4SLinus Torvalds }
20701da177e4SLinus Torvalds 
2071920f50b2SDavid Howells static int sel_get_tree(struct fs_context *fc)
20721da177e4SLinus Torvalds {
2073920f50b2SDavid Howells 	return get_tree_single(fc, sel_fill_super);
2074920f50b2SDavid Howells }
2075920f50b2SDavid Howells 
2076920f50b2SDavid Howells static const struct fs_context_operations sel_context_ops = {
2077920f50b2SDavid Howells 	.get_tree	= sel_get_tree,
2078920f50b2SDavid Howells };
2079920f50b2SDavid Howells 
2080920f50b2SDavid Howells static int sel_init_fs_context(struct fs_context *fc)
2081920f50b2SDavid Howells {
2082920f50b2SDavid Howells 	fc->ops = &sel_context_ops;
2083920f50b2SDavid Howells 	return 0;
20841da177e4SLinus Torvalds }
20851da177e4SLinus Torvalds 
20860619f0f5SStephen Smalley static void sel_kill_sb(struct super_block *sb)
20870619f0f5SStephen Smalley {
20880619f0f5SStephen Smalley 	selinux_fs_info_free(sb);
20890619f0f5SStephen Smalley 	kill_litter_super(sb);
20900619f0f5SStephen Smalley }
20910619f0f5SStephen Smalley 
20921da177e4SLinus Torvalds static struct file_system_type sel_fs_type = {
20931da177e4SLinus Torvalds 	.name		= "selinuxfs",
2094920f50b2SDavid Howells 	.init_fs_context = sel_init_fs_context,
20950619f0f5SStephen Smalley 	.kill_sb	= sel_kill_sb,
20961da177e4SLinus Torvalds };
20971da177e4SLinus Torvalds 
20981da177e4SLinus Torvalds struct vfsmount *selinuxfs_mount;
20990619f0f5SStephen Smalley struct path selinux_null;
21001da177e4SLinus Torvalds 
21011da177e4SLinus Torvalds static int __init init_sel_fs(void)
21021da177e4SLinus Torvalds {
21030619f0f5SStephen Smalley 	struct qstr null_name = QSTR_INIT(NULL_FILE_NAME,
21040619f0f5SStephen Smalley 					  sizeof(NULL_FILE_NAME)-1);
21051da177e4SLinus Torvalds 	int err;
21061da177e4SLinus Torvalds 
21076c5a682eSStephen Smalley 	if (!selinux_enabled_boot)
21081da177e4SLinus Torvalds 		return 0;
21097a627e3bSGreg Kroah-Hartman 
2110f9bb4882SEric W. Biederman 	err = sysfs_create_mount_point(fs_kobj, "selinux");
2111f9bb4882SEric W. Biederman 	if (err)
2112f9bb4882SEric W. Biederman 		return err;
21137a627e3bSGreg Kroah-Hartman 
21141da177e4SLinus Torvalds 	err = register_filesystem(&sel_fs_type);
21157a627e3bSGreg Kroah-Hartman 	if (err) {
2116f9bb4882SEric W. Biederman 		sysfs_remove_mount_point(fs_kobj, "selinux");
2117b77a493bSEric Paris 		return err;
21187a627e3bSGreg Kroah-Hartman 	}
2119b77a493bSEric Paris 
2120765927b2SAl Viro 	selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type);
21211da177e4SLinus Torvalds 	if (IS_ERR(selinuxfs_mount)) {
2122f8b69a5fSpeter enderborg 		pr_err("selinuxfs:  could not mount!\n");
21231da177e4SLinus Torvalds 		err = PTR_ERR(selinuxfs_mount);
21241da177e4SLinus Torvalds 		selinuxfs_mount = NULL;
21251da177e4SLinus Torvalds 	}
21260619f0f5SStephen Smalley 	selinux_null.dentry = d_hash_and_lookup(selinux_null.mnt->mnt_root,
21270619f0f5SStephen Smalley 						&null_name);
21280619f0f5SStephen Smalley 	if (IS_ERR(selinux_null.dentry)) {
21290619f0f5SStephen Smalley 		pr_err("selinuxfs:  could not lookup null!\n");
21300619f0f5SStephen Smalley 		err = PTR_ERR(selinux_null.dentry);
21310619f0f5SStephen Smalley 		selinux_null.dentry = NULL;
21320619f0f5SStephen Smalley 	}
2133b77a493bSEric Paris 
21341da177e4SLinus Torvalds 	return err;
21351da177e4SLinus Torvalds }
21361da177e4SLinus Torvalds 
21371da177e4SLinus Torvalds __initcall(init_sel_fs);
21381da177e4SLinus Torvalds 
21391da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
21401da177e4SLinus Torvalds void exit_sel_fs(void)
21411da177e4SLinus Torvalds {
2142f9bb4882SEric W. Biederman 	sysfs_remove_mount_point(fs_kobj, "selinux");
2143fd40ffc7SStephen Smalley 	dput(selinux_null.dentry);
2144423e0ab0STim Chen 	kern_unmount(selinuxfs_mount);
21451da177e4SLinus Torvalds 	unregister_filesystem(&sel_fs_type);
21461da177e4SLinus Torvalds }
21471da177e4SLinus Torvalds #endif
2148