xref: /openbmc/linux/security/selinux/selinuxfs.c (revision fd40ffc7)
11da177e4SLinus Torvalds /* Updated: Karl MacMillan <kmacmillan@tresys.com>
21da177e4SLinus Torvalds  *
31da177e4SLinus Torvalds  *	Added conditional policy language extensions
41da177e4SLinus Torvalds  *
582c21bfaSPaul Moore  *  Updated: Hewlett-Packard <paul@paul-moore.com>
63bb56b25SPaul Moore  *
73bb56b25SPaul Moore  *	Added support for the policy capability bitmap
83bb56b25SPaul Moore  *
93bb56b25SPaul Moore  * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
101da177e4SLinus Torvalds  * Copyright (C) 2003 - 2004 Tresys Technology, LLC
111da177e4SLinus Torvalds  * Copyright (C) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
121da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or modify
131da177e4SLinus Torvalds  *	it under the terms of the GNU General Public License as published by
141da177e4SLinus Torvalds  *	the Free Software Foundation, version 2.
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #include <linux/kernel.h>
181da177e4SLinus Torvalds #include <linux/pagemap.h>
191da177e4SLinus Torvalds #include <linux/slab.h>
201da177e4SLinus Torvalds #include <linux/vmalloc.h>
211da177e4SLinus Torvalds #include <linux/fs.h>
220619f0f5SStephen Smalley #include <linux/mount.h>
23bb003079SIngo Molnar #include <linux/mutex.h>
241da177e4SLinus Torvalds #include <linux/init.h>
251da177e4SLinus Torvalds #include <linux/string.h>
261da177e4SLinus Torvalds #include <linux/security.h>
271da177e4SLinus Torvalds #include <linux/major.h>
281da177e4SLinus Torvalds #include <linux/seq_file.h>
291da177e4SLinus Torvalds #include <linux/percpu.h>
30af601e46SSteve Grubb #include <linux/audit.h>
31f5269710SEric Paris #include <linux/uaccess.h>
327a627e3bSGreg Kroah-Hartman #include <linux/kobject.h>
330f7e4c33SKohei Kaigai #include <linux/ctype.h>
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds /* selinuxfs pseudo filesystem for exporting the security policy API.
361da177e4SLinus Torvalds    Based on the proc code and the fs/nfsd/nfsctl.c code. */
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds #include "flask.h"
391da177e4SLinus Torvalds #include "avc.h"
401da177e4SLinus Torvalds #include "avc_ss.h"
411da177e4SLinus Torvalds #include "security.h"
421da177e4SLinus Torvalds #include "objsec.h"
431da177e4SLinus Torvalds #include "conditional.h"
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds enum sel_inos {
461da177e4SLinus Torvalds 	SEL_ROOT_INO = 2,
471da177e4SLinus Torvalds 	SEL_LOAD,	/* load policy */
481da177e4SLinus Torvalds 	SEL_ENFORCE,	/* get or set enforcing status */
491da177e4SLinus Torvalds 	SEL_CONTEXT,	/* validate context */
501da177e4SLinus Torvalds 	SEL_ACCESS,	/* compute access decision */
511da177e4SLinus Torvalds 	SEL_CREATE,	/* compute create labeling decision */
521da177e4SLinus Torvalds 	SEL_RELABEL,	/* compute relabeling decision */
531da177e4SLinus Torvalds 	SEL_USER,	/* compute reachable user contexts */
541da177e4SLinus Torvalds 	SEL_POLICYVERS,	/* return policy version for this kernel */
551da177e4SLinus Torvalds 	SEL_COMMIT_BOOLS, /* commit new boolean values */
561da177e4SLinus Torvalds 	SEL_MLS,	/* return if MLS policy is enabled */
571da177e4SLinus Torvalds 	SEL_DISABLE,	/* disable SELinux until next reboot */
581da177e4SLinus Torvalds 	SEL_MEMBER,	/* compute polyinstantiation membership decision */
591da177e4SLinus Torvalds 	SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
604e5ab4cbSJames Morris 	SEL_COMPAT_NET,	/* whether to use old compat network packet controls */
613f12070eSEric Paris 	SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */
623f12070eSEric Paris 	SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */
6311904167SKaiGai Kohei 	SEL_STATUS,	/* export current status using mmap() */
64cee74f47SEric Paris 	SEL_POLICY,	/* allow userspace to read the in kernel policy */
65f9df6458SAndrew Perepechko 	SEL_VALIDATE_TRANS, /* compute validatetrans decision */
666174eafcSJames Carter 	SEL_INO_NEXT,	/* The next inode number to use */
671da177e4SLinus Torvalds };
681da177e4SLinus Torvalds 
690619f0f5SStephen Smalley struct selinux_fs_info {
700619f0f5SStephen Smalley 	struct dentry *bool_dir;
710619f0f5SStephen Smalley 	unsigned int bool_num;
720619f0f5SStephen Smalley 	char **bool_pending_names;
730619f0f5SStephen Smalley 	unsigned int *bool_pending_values;
740619f0f5SStephen Smalley 	struct dentry *class_dir;
750619f0f5SStephen Smalley 	unsigned long last_class_ino;
760619f0f5SStephen Smalley 	bool policy_opened;
770619f0f5SStephen Smalley 	struct dentry *policycap_dir;
780619f0f5SStephen Smalley 	struct mutex mutex;
790619f0f5SStephen Smalley 	unsigned long last_ino;
800619f0f5SStephen Smalley 	struct selinux_state *state;
810619f0f5SStephen Smalley 	struct super_block *sb;
820619f0f5SStephen Smalley };
830619f0f5SStephen Smalley 
840619f0f5SStephen Smalley static int selinux_fs_info_create(struct super_block *sb)
850619f0f5SStephen Smalley {
860619f0f5SStephen Smalley 	struct selinux_fs_info *fsi;
870619f0f5SStephen Smalley 
880619f0f5SStephen Smalley 	fsi = kzalloc(sizeof(*fsi), GFP_KERNEL);
890619f0f5SStephen Smalley 	if (!fsi)
900619f0f5SStephen Smalley 		return -ENOMEM;
910619f0f5SStephen Smalley 
920619f0f5SStephen Smalley 	mutex_init(&fsi->mutex);
930619f0f5SStephen Smalley 	fsi->last_ino = SEL_INO_NEXT - 1;
940619f0f5SStephen Smalley 	fsi->state = &selinux_state;
950619f0f5SStephen Smalley 	fsi->sb = sb;
960619f0f5SStephen Smalley 	sb->s_fs_info = fsi;
970619f0f5SStephen Smalley 	return 0;
980619f0f5SStephen Smalley }
990619f0f5SStephen Smalley 
1000619f0f5SStephen Smalley static void selinux_fs_info_free(struct super_block *sb)
1010619f0f5SStephen Smalley {
1020619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = sb->s_fs_info;
1030619f0f5SStephen Smalley 	int i;
1040619f0f5SStephen Smalley 
1050619f0f5SStephen Smalley 	if (fsi) {
1060619f0f5SStephen Smalley 		for (i = 0; i < fsi->bool_num; i++)
1070619f0f5SStephen Smalley 			kfree(fsi->bool_pending_names[i]);
1080619f0f5SStephen Smalley 		kfree(fsi->bool_pending_names);
1090619f0f5SStephen Smalley 		kfree(fsi->bool_pending_values);
1100619f0f5SStephen Smalley 	}
1110619f0f5SStephen Smalley 	kfree(sb->s_fs_info);
1120619f0f5SStephen Smalley 	sb->s_fs_info = NULL;
1130619f0f5SStephen Smalley }
1146174eafcSJames Carter 
115f0ee2e46SJames Carter #define SEL_INITCON_INO_OFFSET		0x01000000
116bce34bc0SJames Carter #define SEL_BOOL_INO_OFFSET		0x02000000
117e47c8fc5SChristopher J. PeBenito #define SEL_CLASS_INO_OFFSET		0x04000000
1183bb56b25SPaul Moore #define SEL_POLICYCAP_INO_OFFSET	0x08000000
119f0ee2e46SJames Carter #define SEL_INO_MASK			0x00ffffff
120f0ee2e46SJames Carter 
1211da177e4SLinus Torvalds #define TMPBUFLEN	12
1221da177e4SLinus Torvalds static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
1231da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
1241da177e4SLinus Torvalds {
1250619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
1261da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
1271da177e4SLinus Torvalds 	ssize_t length;
1281da177e4SLinus Torvalds 
129aa8e712cSStephen Smalley 	length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
1300619f0f5SStephen Smalley 			   enforcing_enabled(fsi->state));
1311da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1321da177e4SLinus Torvalds }
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
1351da177e4SLinus Torvalds static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
1361da177e4SLinus Torvalds 				 size_t count, loff_t *ppos)
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds {
1390619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1400619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
141b77a493bSEric Paris 	char *page = NULL;
1421da177e4SLinus Torvalds 	ssize_t length;
143aa8e712cSStephen Smalley 	int old_value, new_value;
1441da177e4SLinus Torvalds 
145bfd51626SDavi Arnaut 	if (count >= PAGE_SIZE)
1468365a719SAl Viro 		return -ENOMEM;
147b77a493bSEric Paris 
1481da177e4SLinus Torvalds 	/* No partial writes. */
149b77a493bSEric Paris 	if (*ppos != 0)
1508365a719SAl Viro 		return -EINVAL;
151b77a493bSEric Paris 
1528365a719SAl Viro 	page = memdup_user_nul(buf, count);
1538365a719SAl Viro 	if (IS_ERR(page))
1548365a719SAl Viro 		return PTR_ERR(page);
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds 	length = -EINVAL;
1571da177e4SLinus Torvalds 	if (sscanf(page, "%d", &new_value) != 1)
1581da177e4SLinus Torvalds 		goto out;
1591da177e4SLinus Torvalds 
160ea49d10eSStephen Smalley 	new_value = !!new_value;
161ea49d10eSStephen Smalley 
1620619f0f5SStephen Smalley 	old_value = enforcing_enabled(state);
163aa8e712cSStephen Smalley 	if (new_value != old_value) {
1646b6bc620SStephen Smalley 		length = avc_has_perm(&selinux_state,
1656b6bc620SStephen Smalley 				      current_sid(), SECINITSID_SECURITY,
166be0554c9SStephen Smalley 				      SECCLASS_SECURITY, SECURITY__SETENFORCE,
167be0554c9SStephen Smalley 				      NULL);
1681da177e4SLinus Torvalds 		if (length)
1691da177e4SLinus Torvalds 			goto out;
170af601e46SSteve Grubb 		audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
1714746ec5bSEric Paris 			"enforcing=%d old_enforcing=%d auid=%u ses=%u",
172aa8e712cSStephen Smalley 			new_value, old_value,
173581abc09SEric W. Biederman 			from_kuid(&init_user_ns, audit_get_loginuid(current)),
1744746ec5bSEric Paris 			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)
1818f408ab6SDaniel Jurgens 			call_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;
2821da177e4SLinus Torvalds 
283bfd51626SDavi Arnaut 	if (count >= PAGE_SIZE)
2848365a719SAl Viro 		return -ENOMEM;
285b77a493bSEric Paris 
2861da177e4SLinus Torvalds 	/* No partial writes. */
287b77a493bSEric Paris 	if (*ppos != 0)
2888365a719SAl Viro 		return -EINVAL;
289b77a493bSEric Paris 
2908365a719SAl Viro 	page = memdup_user_nul(buf, count);
2918365a719SAl Viro 	if (IS_ERR(page))
2928365a719SAl Viro 		return PTR_ERR(page);
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	length = -EINVAL;
2951da177e4SLinus Torvalds 	if (sscanf(page, "%d", &new_value) != 1)
2961da177e4SLinus Torvalds 		goto out;
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 	if (new_value) {
2990619f0f5SStephen Smalley 		length = selinux_disable(fsi->state);
300b77a493bSEric Paris 		if (length)
3011da177e4SLinus Torvalds 			goto out;
302af601e46SSteve Grubb 		audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
3034746ec5bSEric Paris 			"selinux=0 auid=%u ses=%u",
304581abc09SEric W. Biederman 			from_kuid(&init_user_ns, audit_get_loginuid(current)),
3054746ec5bSEric Paris 			audit_get_sessionid(current));
3061da177e4SLinus Torvalds 	}
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	length = count;
3091da177e4SLinus Torvalds out:
3108365a719SAl Viro 	kfree(page);
3111da177e4SLinus Torvalds 	return length;
3121da177e4SLinus Torvalds }
3131da177e4SLinus Torvalds #else
3141da177e4SLinus Torvalds #define sel_write_disable NULL
3151da177e4SLinus Torvalds #endif
3161da177e4SLinus Torvalds 
3179c2e08c5SArjan van de Ven static const struct file_operations sel_disable_ops = {
3181da177e4SLinus Torvalds 	.write		= sel_write_disable,
31957a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
3201da177e4SLinus Torvalds };
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
3231da177e4SLinus Torvalds 				   size_t count, loff_t *ppos)
3241da177e4SLinus Torvalds {
3251da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
3261da177e4SLinus Torvalds 	ssize_t length;
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
3291da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
3301da177e4SLinus Torvalds }
3311da177e4SLinus Torvalds 
3329c2e08c5SArjan van de Ven static const struct file_operations sel_policyvers_ops = {
3331da177e4SLinus Torvalds 	.read		= sel_read_policyvers,
33457a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
3351da177e4SLinus Torvalds };
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds /* declaration for sel_write_load */
3380619f0f5SStephen Smalley static int sel_make_bools(struct selinux_fs_info *fsi);
3390619f0f5SStephen Smalley static int sel_make_classes(struct selinux_fs_info *fsi);
3400619f0f5SStephen Smalley static int sel_make_policycap(struct selinux_fs_info *fsi);
341e47c8fc5SChristopher J. PeBenito 
342e47c8fc5SChristopher J. PeBenito /* declaration for sel_make_class_dirs */
343a1c2aa1eSAl Viro static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
344e47c8fc5SChristopher J. PeBenito 			unsigned long *ino);
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds static ssize_t sel_read_mls(struct file *filp, char __user *buf,
3471da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
3481da177e4SLinus Torvalds {
3490619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
3501da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
3511da177e4SLinus Torvalds 	ssize_t length;
3521da177e4SLinus Torvalds 
3530719aaf5SGuido Trentalancia 	length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
3540619f0f5SStephen Smalley 			   security_mls_enabled(fsi->state));
3551da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
3561da177e4SLinus Torvalds }
3571da177e4SLinus Torvalds 
3589c2e08c5SArjan van de Ven static const struct file_operations sel_mls_ops = {
3591da177e4SLinus Torvalds 	.read		= sel_read_mls,
36057a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
3611da177e4SLinus Torvalds };
3621da177e4SLinus Torvalds 
363cee74f47SEric Paris struct policy_load_memory {
364cee74f47SEric Paris 	size_t len;
365cee74f47SEric Paris 	void *data;
366cee74f47SEric Paris };
367cee74f47SEric Paris 
368cee74f47SEric Paris static int sel_open_policy(struct inode *inode, struct file *filp)
369cee74f47SEric Paris {
3700619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
3710619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
372cee74f47SEric Paris 	struct policy_load_memory *plm = NULL;
373cee74f47SEric Paris 	int rc;
374cee74f47SEric Paris 
375cee74f47SEric Paris 	BUG_ON(filp->private_data);
376cee74f47SEric Paris 
3770619f0f5SStephen Smalley 	mutex_lock(&fsi->mutex);
378cee74f47SEric Paris 
3796b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
3806b6bc620SStephen Smalley 			  current_sid(), SECINITSID_SECURITY,
381be0554c9SStephen Smalley 			  SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
382cee74f47SEric Paris 	if (rc)
383cee74f47SEric Paris 		goto err;
384cee74f47SEric Paris 
385cee74f47SEric Paris 	rc = -EBUSY;
3860619f0f5SStephen Smalley 	if (fsi->policy_opened)
387cee74f47SEric Paris 		goto err;
388cee74f47SEric Paris 
389cee74f47SEric Paris 	rc = -ENOMEM;
390cee74f47SEric Paris 	plm = kzalloc(sizeof(*plm), GFP_KERNEL);
391cee74f47SEric Paris 	if (!plm)
392cee74f47SEric Paris 		goto err;
393cee74f47SEric Paris 
3940619f0f5SStephen Smalley 	if (i_size_read(inode) != security_policydb_len(state)) {
3955955102cSAl Viro 		inode_lock(inode);
3960619f0f5SStephen Smalley 		i_size_write(inode, security_policydb_len(state));
3975955102cSAl Viro 		inode_unlock(inode);
398cee74f47SEric Paris 	}
399cee74f47SEric Paris 
4000619f0f5SStephen Smalley 	rc = security_read_policy(state, &plm->data, &plm->len);
401cee74f47SEric Paris 	if (rc)
402cee74f47SEric Paris 		goto err;
403cee74f47SEric Paris 
4040619f0f5SStephen Smalley 	fsi->policy_opened = 1;
405cee74f47SEric Paris 
406cee74f47SEric Paris 	filp->private_data = plm;
407cee74f47SEric Paris 
4080619f0f5SStephen Smalley 	mutex_unlock(&fsi->mutex);
409cee74f47SEric Paris 
410cee74f47SEric Paris 	return 0;
411cee74f47SEric Paris err:
4120619f0f5SStephen Smalley 	mutex_unlock(&fsi->mutex);
413cee74f47SEric Paris 
414cee74f47SEric Paris 	if (plm)
415cee74f47SEric Paris 		vfree(plm->data);
416cee74f47SEric Paris 	kfree(plm);
417cee74f47SEric Paris 	return rc;
418cee74f47SEric Paris }
419cee74f47SEric Paris 
420cee74f47SEric Paris static int sel_release_policy(struct inode *inode, struct file *filp)
421cee74f47SEric Paris {
4220619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
423cee74f47SEric Paris 	struct policy_load_memory *plm = filp->private_data;
424cee74f47SEric Paris 
425cee74f47SEric Paris 	BUG_ON(!plm);
426cee74f47SEric Paris 
4270619f0f5SStephen Smalley 	fsi->policy_opened = 0;
428cee74f47SEric Paris 
429cee74f47SEric Paris 	vfree(plm->data);
430cee74f47SEric Paris 	kfree(plm);
431cee74f47SEric Paris 
432cee74f47SEric Paris 	return 0;
433cee74f47SEric Paris }
434cee74f47SEric Paris 
435cee74f47SEric Paris static ssize_t sel_read_policy(struct file *filp, char __user *buf,
436cee74f47SEric Paris 			       size_t count, loff_t *ppos)
437cee74f47SEric Paris {
4380619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
439cee74f47SEric Paris 	struct policy_load_memory *plm = filp->private_data;
440cee74f47SEric Paris 	int ret;
441cee74f47SEric Paris 
4420619f0f5SStephen Smalley 	mutex_lock(&fsi->mutex);
443cee74f47SEric Paris 
4446b6bc620SStephen Smalley 	ret = avc_has_perm(&selinux_state,
4456b6bc620SStephen Smalley 			   current_sid(), SECINITSID_SECURITY,
446be0554c9SStephen Smalley 			  SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
447cee74f47SEric Paris 	if (ret)
448cee74f47SEric Paris 		goto out;
449cee74f47SEric Paris 
450cee74f47SEric Paris 	ret = simple_read_from_buffer(buf, count, ppos, plm->data, plm->len);
451cee74f47SEric Paris out:
4520619f0f5SStephen Smalley 	mutex_unlock(&fsi->mutex);
453cee74f47SEric Paris 	return ret;
454cee74f47SEric Paris }
455cee74f47SEric Paris 
45611bac800SDave Jiang static int sel_mmap_policy_fault(struct vm_fault *vmf)
457845ca30fSEric Paris {
45811bac800SDave Jiang 	struct policy_load_memory *plm = vmf->vma->vm_file->private_data;
459845ca30fSEric Paris 	unsigned long offset;
460845ca30fSEric Paris 	struct page *page;
461845ca30fSEric Paris 
462845ca30fSEric Paris 	if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE))
463845ca30fSEric Paris 		return VM_FAULT_SIGBUS;
464845ca30fSEric Paris 
465845ca30fSEric Paris 	offset = vmf->pgoff << PAGE_SHIFT;
466845ca30fSEric Paris 	if (offset >= roundup(plm->len, PAGE_SIZE))
467845ca30fSEric Paris 		return VM_FAULT_SIGBUS;
468845ca30fSEric Paris 
469845ca30fSEric Paris 	page = vmalloc_to_page(plm->data + offset);
470845ca30fSEric Paris 	get_page(page);
471845ca30fSEric Paris 
472845ca30fSEric Paris 	vmf->page = page;
473845ca30fSEric Paris 
474845ca30fSEric Paris 	return 0;
475845ca30fSEric Paris }
476845ca30fSEric Paris 
4777cbea8dcSKirill A. Shutemov static const struct vm_operations_struct sel_mmap_policy_ops = {
478845ca30fSEric Paris 	.fault = sel_mmap_policy_fault,
479845ca30fSEric Paris 	.page_mkwrite = sel_mmap_policy_fault,
480845ca30fSEric Paris };
481845ca30fSEric Paris 
482ad3fa08cSJames Morris static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma)
483845ca30fSEric Paris {
484845ca30fSEric Paris 	if (vma->vm_flags & VM_SHARED) {
485845ca30fSEric Paris 		/* do not allow mprotect to make mapping writable */
486845ca30fSEric Paris 		vma->vm_flags &= ~VM_MAYWRITE;
487845ca30fSEric Paris 
488845ca30fSEric Paris 		if (vma->vm_flags & VM_WRITE)
489845ca30fSEric Paris 			return -EACCES;
490845ca30fSEric Paris 	}
491845ca30fSEric Paris 
492314e51b9SKonstantin Khlebnikov 	vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
493845ca30fSEric Paris 	vma->vm_ops = &sel_mmap_policy_ops;
494845ca30fSEric Paris 
495845ca30fSEric Paris 	return 0;
496845ca30fSEric Paris }
497845ca30fSEric Paris 
498cee74f47SEric Paris static const struct file_operations sel_policy_ops = {
499cee74f47SEric Paris 	.open		= sel_open_policy,
500cee74f47SEric Paris 	.read		= sel_read_policy,
501845ca30fSEric Paris 	.mmap		= sel_mmap_policy,
502cee74f47SEric Paris 	.release	= sel_release_policy,
50347a93a5bSEric Paris 	.llseek		= generic_file_llseek,
504cee74f47SEric Paris };
505cee74f47SEric Paris 
5060619f0f5SStephen Smalley static int sel_make_policy_nodes(struct selinux_fs_info *fsi)
5070619f0f5SStephen Smalley {
5080619f0f5SStephen Smalley 	int ret;
5090619f0f5SStephen Smalley 
5100619f0f5SStephen Smalley 	ret = sel_make_bools(fsi);
5110619f0f5SStephen Smalley 	if (ret) {
5120619f0f5SStephen Smalley 		pr_err("SELinux: failed to load policy booleans\n");
5130619f0f5SStephen Smalley 		return ret;
5140619f0f5SStephen Smalley 	}
5150619f0f5SStephen Smalley 
5160619f0f5SStephen Smalley 	ret = sel_make_classes(fsi);
5170619f0f5SStephen Smalley 	if (ret) {
5180619f0f5SStephen Smalley 		pr_err("SELinux: failed to load policy classes\n");
5190619f0f5SStephen Smalley 		return ret;
5200619f0f5SStephen Smalley 	}
5210619f0f5SStephen Smalley 
5220619f0f5SStephen Smalley 	ret = sel_make_policycap(fsi);
5230619f0f5SStephen Smalley 	if (ret) {
5240619f0f5SStephen Smalley 		pr_err("SELinux: failed to load policy capabilities\n");
5250619f0f5SStephen Smalley 		return ret;
5260619f0f5SStephen Smalley 	}
5270619f0f5SStephen Smalley 
5280619f0f5SStephen Smalley 	return 0;
5290619f0f5SStephen Smalley }
5300619f0f5SStephen Smalley 
5311da177e4SLinus Torvalds static ssize_t sel_write_load(struct file *file, const char __user *buf,
5321da177e4SLinus Torvalds 			      size_t count, loff_t *ppos)
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds {
5350619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
5361da177e4SLinus Torvalds 	ssize_t length;
5371da177e4SLinus Torvalds 	void *data = NULL;
5381da177e4SLinus Torvalds 
5390619f0f5SStephen Smalley 	mutex_lock(&fsi->mutex);
5401da177e4SLinus Torvalds 
5416b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
5426b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
543be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL);
5441da177e4SLinus Torvalds 	if (length)
5451da177e4SLinus Torvalds 		goto out;
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds 	/* No partial writes. */
5481da177e4SLinus Torvalds 	length = -EINVAL;
549b77a493bSEric Paris 	if (*ppos != 0)
5501da177e4SLinus Torvalds 		goto out;
5511da177e4SLinus Torvalds 
552b77a493bSEric Paris 	length = -EFBIG;
553b77a493bSEric Paris 	if (count > 64 * 1024 * 1024)
5541da177e4SLinus Torvalds 		goto out;
555b77a493bSEric Paris 
556b77a493bSEric Paris 	length = -ENOMEM;
557b77a493bSEric Paris 	data = vmalloc(count);
558b77a493bSEric Paris 	if (!data)
559b77a493bSEric Paris 		goto out;
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds 	length = -EFAULT;
5621da177e4SLinus Torvalds 	if (copy_from_user(data, buf, count) != 0)
5631da177e4SLinus Torvalds 		goto out;
5641da177e4SLinus Torvalds 
5650619f0f5SStephen Smalley 	length = security_load_policy(fsi->state, data, count);
5664262fb51SGary Tierney 	if (length) {
5674262fb51SGary Tierney 		pr_warn_ratelimited("SELinux: failed to load policy\n");
5681da177e4SLinus Torvalds 		goto out;
5694262fb51SGary Tierney 	}
5701da177e4SLinus Torvalds 
5710619f0f5SStephen Smalley 	length = sel_make_policy_nodes(fsi);
5720619f0f5SStephen Smalley 	if (length)
573e47c8fc5SChristopher J. PeBenito 		goto out1;
574b77a493bSEric Paris 
5751da177e4SLinus Torvalds 	length = count;
576e47c8fc5SChristopher J. PeBenito 
577e47c8fc5SChristopher J. PeBenito out1:
578af601e46SSteve Grubb 	audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
5794746ec5bSEric Paris 		"policy loaded auid=%u ses=%u",
580581abc09SEric W. Biederman 		from_kuid(&init_user_ns, audit_get_loginuid(current)),
5814746ec5bSEric Paris 		audit_get_sessionid(current));
5821da177e4SLinus Torvalds out:
5830619f0f5SStephen Smalley 	mutex_unlock(&fsi->mutex);
5841da177e4SLinus Torvalds 	vfree(data);
5851da177e4SLinus Torvalds 	return length;
5861da177e4SLinus Torvalds }
5871da177e4SLinus Torvalds 
5889c2e08c5SArjan van de Ven static const struct file_operations sel_load_ops = {
5891da177e4SLinus Torvalds 	.write		= sel_write_load,
59057a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
5911da177e4SLinus Torvalds };
5921da177e4SLinus Torvalds 
593ce9982d0SStephen Smalley static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
5941da177e4SLinus Torvalds {
5950619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
5960619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
597b77a493bSEric Paris 	char *canon = NULL;
598ce9982d0SStephen Smalley 	u32 sid, len;
5991da177e4SLinus Torvalds 	ssize_t length;
6001da177e4SLinus Torvalds 
6016b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
6026b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
603be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__CHECK_CONTEXT, NULL);
6041da177e4SLinus Torvalds 	if (length)
605b77a493bSEric Paris 		goto out;
6061da177e4SLinus Torvalds 
6070619f0f5SStephen Smalley 	length = security_context_to_sid(state, buf, size, &sid, GFP_KERNEL);
608b77a493bSEric Paris 	if (length)
609b77a493bSEric Paris 		goto out;
6101da177e4SLinus Torvalds 
6110619f0f5SStephen Smalley 	length = security_sid_to_context(state, sid, &canon, &len);
612b77a493bSEric Paris 	if (length)
613b77a493bSEric Paris 		goto out;
614ce9982d0SStephen Smalley 
615b77a493bSEric Paris 	length = -ERANGE;
616ce9982d0SStephen Smalley 	if (len > SIMPLE_TRANSACTION_LIMIT) {
617744ba35eSEric Paris 		printk(KERN_ERR "SELinux: %s:  context size (%u) exceeds "
618744ba35eSEric Paris 			"payload max\n", __func__, len);
619ce9982d0SStephen Smalley 		goto out;
620ce9982d0SStephen Smalley 	}
621ce9982d0SStephen Smalley 
622ce9982d0SStephen Smalley 	memcpy(buf, canon, len);
623ce9982d0SStephen Smalley 	length = len;
6241da177e4SLinus Torvalds out:
625ce9982d0SStephen Smalley 	kfree(canon);
6261da177e4SLinus Torvalds 	return length;
6271da177e4SLinus Torvalds }
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
6301da177e4SLinus Torvalds 				     size_t count, loff_t *ppos)
6311da177e4SLinus Torvalds {
6320619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
6331da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
6341da177e4SLinus Torvalds 	ssize_t length;
6351da177e4SLinus Torvalds 
6360619f0f5SStephen Smalley 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u", fsi->state->checkreqprot);
6371da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
6381da177e4SLinus Torvalds }
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
6411da177e4SLinus Torvalds 				      size_t count, loff_t *ppos)
6421da177e4SLinus Torvalds {
6430619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
6448365a719SAl Viro 	char *page;
6451da177e4SLinus Torvalds 	ssize_t length;
6461da177e4SLinus Torvalds 	unsigned int new_value;
6471da177e4SLinus Torvalds 
6486b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
6496b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
650be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__SETCHECKREQPROT,
651be0554c9SStephen Smalley 			      NULL);
6521da177e4SLinus Torvalds 	if (length)
6538365a719SAl Viro 		return length;
6541da177e4SLinus Torvalds 
655bfd51626SDavi Arnaut 	if (count >= PAGE_SIZE)
6568365a719SAl Viro 		return -ENOMEM;
657b77a493bSEric Paris 
6581da177e4SLinus Torvalds 	/* No partial writes. */
659b77a493bSEric Paris 	if (*ppos != 0)
6608365a719SAl Viro 		return -EINVAL;
661b77a493bSEric Paris 
6628365a719SAl Viro 	page = memdup_user_nul(buf, count);
6638365a719SAl Viro 	if (IS_ERR(page))
6648365a719SAl Viro 		return PTR_ERR(page);
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds 	length = -EINVAL;
6671da177e4SLinus Torvalds 	if (sscanf(page, "%u", &new_value) != 1)
6681da177e4SLinus Torvalds 		goto out;
6691da177e4SLinus Torvalds 
6700619f0f5SStephen Smalley 	fsi->state->checkreqprot = new_value ? 1 : 0;
6711da177e4SLinus Torvalds 	length = count;
6721da177e4SLinus Torvalds out:
6738365a719SAl Viro 	kfree(page);
6741da177e4SLinus Torvalds 	return length;
6751da177e4SLinus Torvalds }
6769c2e08c5SArjan van de Ven static const struct file_operations sel_checkreqprot_ops = {
6771da177e4SLinus Torvalds 	.read		= sel_read_checkreqprot,
6781da177e4SLinus Torvalds 	.write		= sel_write_checkreqprot,
67957a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
6801da177e4SLinus Torvalds };
6811da177e4SLinus Torvalds 
682f9df6458SAndrew Perepechko static ssize_t sel_write_validatetrans(struct file *file,
683f9df6458SAndrew Perepechko 					const char __user *buf,
684f9df6458SAndrew Perepechko 					size_t count, loff_t *ppos)
685f9df6458SAndrew Perepechko {
6860619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
6870619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
688f9df6458SAndrew Perepechko 	char *oldcon = NULL, *newcon = NULL, *taskcon = NULL;
689f9df6458SAndrew Perepechko 	char *req = NULL;
690f9df6458SAndrew Perepechko 	u32 osid, nsid, tsid;
691f9df6458SAndrew Perepechko 	u16 tclass;
692f9df6458SAndrew Perepechko 	int rc;
693f9df6458SAndrew Perepechko 
6946b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
6956b6bc620SStephen Smalley 			  current_sid(), SECINITSID_SECURITY,
696be0554c9SStephen Smalley 			  SECCLASS_SECURITY, SECURITY__VALIDATE_TRANS, NULL);
697f9df6458SAndrew Perepechko 	if (rc)
698f9df6458SAndrew Perepechko 		goto out;
699f9df6458SAndrew Perepechko 
700f9df6458SAndrew Perepechko 	rc = -ENOMEM;
701f9df6458SAndrew Perepechko 	if (count >= PAGE_SIZE)
702f9df6458SAndrew Perepechko 		goto out;
703f9df6458SAndrew Perepechko 
704f9df6458SAndrew Perepechko 	/* No partial writes. */
705f9df6458SAndrew Perepechko 	rc = -EINVAL;
706f9df6458SAndrew Perepechko 	if (*ppos != 0)
707f9df6458SAndrew Perepechko 		goto out;
708f9df6458SAndrew Perepechko 
7090b884d25SAl Viro 	req = memdup_user_nul(buf, count);
7100b884d25SAl Viro 	if (IS_ERR(req)) {
7110b884d25SAl Viro 		rc = PTR_ERR(req);
7120b884d25SAl Viro 		req = NULL;
713f9df6458SAndrew Perepechko 		goto out;
7140b884d25SAl Viro 	}
715f9df6458SAndrew Perepechko 
716f9df6458SAndrew Perepechko 	rc = -ENOMEM;
717f9df6458SAndrew Perepechko 	oldcon = kzalloc(count + 1, GFP_KERNEL);
718f9df6458SAndrew Perepechko 	if (!oldcon)
719f9df6458SAndrew Perepechko 		goto out;
720f9df6458SAndrew Perepechko 
721f9df6458SAndrew Perepechko 	newcon = kzalloc(count + 1, GFP_KERNEL);
722f9df6458SAndrew Perepechko 	if (!newcon)
723f9df6458SAndrew Perepechko 		goto out;
724f9df6458SAndrew Perepechko 
725f9df6458SAndrew Perepechko 	taskcon = kzalloc(count + 1, GFP_KERNEL);
726f9df6458SAndrew Perepechko 	if (!taskcon)
727f9df6458SAndrew Perepechko 		goto out;
728f9df6458SAndrew Perepechko 
729f9df6458SAndrew Perepechko 	rc = -EINVAL;
730f9df6458SAndrew Perepechko 	if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4)
731f9df6458SAndrew Perepechko 		goto out;
732f9df6458SAndrew Perepechko 
7330619f0f5SStephen Smalley 	rc = security_context_str_to_sid(state, oldcon, &osid, GFP_KERNEL);
734f9df6458SAndrew Perepechko 	if (rc)
735f9df6458SAndrew Perepechko 		goto out;
736f9df6458SAndrew Perepechko 
7370619f0f5SStephen Smalley 	rc = security_context_str_to_sid(state, newcon, &nsid, GFP_KERNEL);
738f9df6458SAndrew Perepechko 	if (rc)
739f9df6458SAndrew Perepechko 		goto out;
740f9df6458SAndrew Perepechko 
7410619f0f5SStephen Smalley 	rc = security_context_str_to_sid(state, taskcon, &tsid, GFP_KERNEL);
742f9df6458SAndrew Perepechko 	if (rc)
743f9df6458SAndrew Perepechko 		goto out;
744f9df6458SAndrew Perepechko 
7450619f0f5SStephen Smalley 	rc = security_validate_transition_user(state, osid, nsid, tsid, tclass);
746f9df6458SAndrew Perepechko 	if (!rc)
747f9df6458SAndrew Perepechko 		rc = count;
748f9df6458SAndrew Perepechko out:
749f9df6458SAndrew Perepechko 	kfree(req);
750f9df6458SAndrew Perepechko 	kfree(oldcon);
751f9df6458SAndrew Perepechko 	kfree(newcon);
752f9df6458SAndrew Perepechko 	kfree(taskcon);
753f9df6458SAndrew Perepechko 	return rc;
754f9df6458SAndrew Perepechko }
755f9df6458SAndrew Perepechko 
756f9df6458SAndrew Perepechko static const struct file_operations sel_transition_ops = {
757f9df6458SAndrew Perepechko 	.write		= sel_write_validatetrans,
758f9df6458SAndrew Perepechko 	.llseek		= generic_file_llseek,
759f9df6458SAndrew Perepechko };
760f9df6458SAndrew Perepechko 
7611da177e4SLinus Torvalds /*
7621da177e4SLinus Torvalds  * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
7631da177e4SLinus Torvalds  */
7641da177e4SLinus Torvalds static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
7651da177e4SLinus Torvalds static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
7661da177e4SLinus Torvalds static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
7671da177e4SLinus Torvalds static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
7681da177e4SLinus Torvalds static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds static ssize_t (*write_op[])(struct file *, char *, size_t) = {
7711da177e4SLinus Torvalds 	[SEL_ACCESS] = sel_write_access,
7721da177e4SLinus Torvalds 	[SEL_CREATE] = sel_write_create,
7731da177e4SLinus Torvalds 	[SEL_RELABEL] = sel_write_relabel,
7741da177e4SLinus Torvalds 	[SEL_USER] = sel_write_user,
7751da177e4SLinus Torvalds 	[SEL_MEMBER] = sel_write_member,
776ce9982d0SStephen Smalley 	[SEL_CONTEXT] = sel_write_context,
7771da177e4SLinus Torvalds };
7781da177e4SLinus Torvalds 
7791da177e4SLinus Torvalds static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
7801da177e4SLinus Torvalds {
781496ad9aaSAl Viro 	ino_t ino = file_inode(file)->i_ino;
7821da177e4SLinus Torvalds 	char *data;
7831da177e4SLinus Torvalds 	ssize_t rv;
7841da177e4SLinus Torvalds 
7856e20a64aSNicolas Kaiser 	if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
7861da177e4SLinus Torvalds 		return -EINVAL;
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds 	data = simple_transaction_get(file, buf, size);
7891da177e4SLinus Torvalds 	if (IS_ERR(data))
7901da177e4SLinus Torvalds 		return PTR_ERR(data);
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 	rv = write_op[ino](file, data, size);
7931da177e4SLinus Torvalds 	if (rv > 0) {
7941da177e4SLinus Torvalds 		simple_transaction_set(file, rv);
7951da177e4SLinus Torvalds 		rv = size;
7961da177e4SLinus Torvalds 	}
7971da177e4SLinus Torvalds 	return rv;
7981da177e4SLinus Torvalds }
7991da177e4SLinus Torvalds 
8009c2e08c5SArjan van de Ven static const struct file_operations transaction_ops = {
8011da177e4SLinus Torvalds 	.write		= selinux_transaction_write,
8021da177e4SLinus Torvalds 	.read		= simple_transaction_read,
8031da177e4SLinus Torvalds 	.release	= simple_transaction_release,
80457a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
8051da177e4SLinus Torvalds };
8061da177e4SLinus Torvalds 
8071da177e4SLinus Torvalds /*
8081da177e4SLinus Torvalds  * payload - write methods
8091da177e4SLinus Torvalds  * If the method has a response, the response should be put in buf,
8101da177e4SLinus Torvalds  * and the length returned.  Otherwise return 0 or and -error.
8111da177e4SLinus Torvalds  */
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
8141da177e4SLinus Torvalds {
8150619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
8160619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
817b77a493bSEric Paris 	char *scon = NULL, *tcon = NULL;
8181da177e4SLinus Torvalds 	u32 ssid, tsid;
8191da177e4SLinus Torvalds 	u16 tclass;
8201da177e4SLinus Torvalds 	struct av_decision avd;
8211da177e4SLinus Torvalds 	ssize_t length;
8221da177e4SLinus Torvalds 
8236b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
8246b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
825be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__COMPUTE_AV, NULL);
8261da177e4SLinus Torvalds 	if (length)
827b77a493bSEric Paris 		goto out;
8281da177e4SLinus Torvalds 
8291da177e4SLinus Torvalds 	length = -ENOMEM;
83089d155efSJames Morris 	scon = kzalloc(size + 1, GFP_KERNEL);
8311da177e4SLinus Torvalds 	if (!scon)
832b77a493bSEric Paris 		goto out;
8331da177e4SLinus Torvalds 
834b77a493bSEric Paris 	length = -ENOMEM;
83589d155efSJames Morris 	tcon = kzalloc(size + 1, GFP_KERNEL);
8361da177e4SLinus Torvalds 	if (!tcon)
8371da177e4SLinus Torvalds 		goto out;
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds 	length = -EINVAL;
84019439d05SStephen Smalley 	if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
841b77a493bSEric Paris 		goto out;
8421da177e4SLinus Torvalds 
8430619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
844b77a493bSEric Paris 	if (length)
845b77a493bSEric Paris 		goto out;
846b77a493bSEric Paris 
8470619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
848b77a493bSEric Paris 	if (length)
849b77a493bSEric Paris 		goto out;
8501da177e4SLinus Torvalds 
8510619f0f5SStephen Smalley 	security_compute_av_user(state, ssid, tsid, tclass, &avd);
8521da177e4SLinus Torvalds 
8531da177e4SLinus Torvalds 	length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
8548a6f83afSKaiGai Kohei 			  "%x %x %x %x %u %x",
855f1c6381aSEric Paris 			  avd.allowed, 0xffffffff,
8561da177e4SLinus Torvalds 			  avd.auditallow, avd.auditdeny,
8578a6f83afSKaiGai Kohei 			  avd.seqno, avd.flags);
8581da177e4SLinus Torvalds out:
859b77a493bSEric Paris 	kfree(tcon);
8601da177e4SLinus Torvalds 	kfree(scon);
8611da177e4SLinus Torvalds 	return length;
8621da177e4SLinus Torvalds }
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
8651da177e4SLinus Torvalds {
8660619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
8670619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
868b77a493bSEric Paris 	char *scon = NULL, *tcon = NULL;
869f50a3ec9SKohei Kaigai 	char *namebuf = NULL, *objname = NULL;
8701da177e4SLinus Torvalds 	u32 ssid, tsid, newsid;
8711da177e4SLinus Torvalds 	u16 tclass;
8721da177e4SLinus Torvalds 	ssize_t length;
873b77a493bSEric Paris 	char *newcon = NULL;
8741da177e4SLinus Torvalds 	u32 len;
875f50a3ec9SKohei Kaigai 	int nargs;
8761da177e4SLinus Torvalds 
8776b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
8786b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
879be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__COMPUTE_CREATE,
880be0554c9SStephen Smalley 			      NULL);
8811da177e4SLinus Torvalds 	if (length)
882b77a493bSEric Paris 		goto out;
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 	length = -ENOMEM;
88589d155efSJames Morris 	scon = kzalloc(size + 1, GFP_KERNEL);
8861da177e4SLinus Torvalds 	if (!scon)
887b77a493bSEric Paris 		goto out;
8881da177e4SLinus Torvalds 
889b77a493bSEric Paris 	length = -ENOMEM;
89089d155efSJames Morris 	tcon = kzalloc(size + 1, GFP_KERNEL);
8911da177e4SLinus Torvalds 	if (!tcon)
8921da177e4SLinus Torvalds 		goto out;
8931da177e4SLinus Torvalds 
894f50a3ec9SKohei Kaigai 	length = -ENOMEM;
895f50a3ec9SKohei Kaigai 	namebuf = kzalloc(size + 1, GFP_KERNEL);
896f50a3ec9SKohei Kaigai 	if (!namebuf)
897b77a493bSEric Paris 		goto out;
8981da177e4SLinus Torvalds 
899f50a3ec9SKohei Kaigai 	length = -EINVAL;
900f50a3ec9SKohei Kaigai 	nargs = sscanf(buf, "%s %s %hu %s", scon, tcon, &tclass, namebuf);
901f50a3ec9SKohei Kaigai 	if (nargs < 3 || nargs > 4)
902f50a3ec9SKohei Kaigai 		goto out;
9030f7e4c33SKohei Kaigai 	if (nargs == 4) {
9040f7e4c33SKohei Kaigai 		/*
9050f7e4c33SKohei Kaigai 		 * If and when the name of new object to be queried contains
9060f7e4c33SKohei Kaigai 		 * either whitespace or multibyte characters, they shall be
9070f7e4c33SKohei Kaigai 		 * encoded based on the percentage-encoding rule.
9080f7e4c33SKohei Kaigai 		 * If not encoded, the sscanf logic picks up only left-half
9090f7e4c33SKohei Kaigai 		 * of the supplied name; splitted by a whitespace unexpectedly.
9100f7e4c33SKohei Kaigai 		 */
9110f7e4c33SKohei Kaigai 		char   *r, *w;
9120f7e4c33SKohei Kaigai 		int     c1, c2;
9130f7e4c33SKohei Kaigai 
9140f7e4c33SKohei Kaigai 		r = w = namebuf;
9150f7e4c33SKohei Kaigai 		do {
9160f7e4c33SKohei Kaigai 			c1 = *r++;
9170f7e4c33SKohei Kaigai 			if (c1 == '+')
9180f7e4c33SKohei Kaigai 				c1 = ' ';
9190f7e4c33SKohei Kaigai 			else if (c1 == '%') {
920af7ff2c2SAndy Shevchenko 				c1 = hex_to_bin(*r++);
921af7ff2c2SAndy Shevchenko 				if (c1 < 0)
9220f7e4c33SKohei Kaigai 					goto out;
923af7ff2c2SAndy Shevchenko 				c2 = hex_to_bin(*r++);
924af7ff2c2SAndy Shevchenko 				if (c2 < 0)
9250f7e4c33SKohei Kaigai 					goto out;
9260f7e4c33SKohei Kaigai 				c1 = (c1 << 4) | c2;
9270f7e4c33SKohei Kaigai 			}
9280f7e4c33SKohei Kaigai 			*w++ = c1;
9290f7e4c33SKohei Kaigai 		} while (c1 != '\0');
9300f7e4c33SKohei Kaigai 
931f50a3ec9SKohei Kaigai 		objname = namebuf;
9320f7e4c33SKohei Kaigai 	}
933f50a3ec9SKohei Kaigai 
9340619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
935b77a493bSEric Paris 	if (length)
936b77a493bSEric Paris 		goto out;
937b77a493bSEric Paris 
9380619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
939b77a493bSEric Paris 	if (length)
940b77a493bSEric Paris 		goto out;
9411da177e4SLinus Torvalds 
9420619f0f5SStephen Smalley 	length = security_transition_sid_user(state, ssid, tsid, tclass,
9430619f0f5SStephen Smalley 					      objname, &newsid);
944b77a493bSEric Paris 	if (length)
945b77a493bSEric Paris 		goto out;
9461da177e4SLinus Torvalds 
9470619f0f5SStephen Smalley 	length = security_sid_to_context(state, newsid, &newcon, &len);
948b77a493bSEric Paris 	if (length)
949b77a493bSEric Paris 		goto out;
9501da177e4SLinus Torvalds 
951b77a493bSEric Paris 	length = -ERANGE;
9521da177e4SLinus Torvalds 	if (len > SIMPLE_TRANSACTION_LIMIT) {
953744ba35eSEric Paris 		printk(KERN_ERR "SELinux: %s:  context size (%u) exceeds "
954744ba35eSEric Paris 			"payload max\n", __func__, len);
955b77a493bSEric Paris 		goto out;
9561da177e4SLinus Torvalds 	}
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds 	memcpy(buf, newcon, len);
9591da177e4SLinus Torvalds 	length = len;
9601da177e4SLinus Torvalds out:
961b77a493bSEric Paris 	kfree(newcon);
962f50a3ec9SKohei Kaigai 	kfree(namebuf);
963b77a493bSEric Paris 	kfree(tcon);
9641da177e4SLinus Torvalds 	kfree(scon);
9651da177e4SLinus Torvalds 	return length;
9661da177e4SLinus Torvalds }
9671da177e4SLinus Torvalds 
9681da177e4SLinus Torvalds static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
9691da177e4SLinus Torvalds {
9700619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
9710619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
972b77a493bSEric Paris 	char *scon = NULL, *tcon = NULL;
9731da177e4SLinus Torvalds 	u32 ssid, tsid, newsid;
9741da177e4SLinus Torvalds 	u16 tclass;
9751da177e4SLinus Torvalds 	ssize_t length;
976b77a493bSEric Paris 	char *newcon = NULL;
9771da177e4SLinus Torvalds 	u32 len;
9781da177e4SLinus Torvalds 
9796b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
9806b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
981be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__COMPUTE_RELABEL,
982be0554c9SStephen Smalley 			      NULL);
9831da177e4SLinus Torvalds 	if (length)
984b77a493bSEric Paris 		goto out;
9851da177e4SLinus Torvalds 
9861da177e4SLinus Torvalds 	length = -ENOMEM;
98789d155efSJames Morris 	scon = kzalloc(size + 1, GFP_KERNEL);
9881da177e4SLinus Torvalds 	if (!scon)
989b77a493bSEric Paris 		goto out;
9901da177e4SLinus Torvalds 
991b77a493bSEric Paris 	length = -ENOMEM;
99289d155efSJames Morris 	tcon = kzalloc(size + 1, GFP_KERNEL);
9931da177e4SLinus Torvalds 	if (!tcon)
9941da177e4SLinus Torvalds 		goto out;
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds 	length = -EINVAL;
9971da177e4SLinus Torvalds 	if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
998b77a493bSEric Paris 		goto out;
9991da177e4SLinus Torvalds 
10000619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
1001b77a493bSEric Paris 	if (length)
1002b77a493bSEric Paris 		goto out;
1003b77a493bSEric Paris 
10040619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
1005b77a493bSEric Paris 	if (length)
1006b77a493bSEric Paris 		goto out;
10071da177e4SLinus Torvalds 
10080619f0f5SStephen Smalley 	length = security_change_sid(state, ssid, tsid, tclass, &newsid);
1009b77a493bSEric Paris 	if (length)
1010b77a493bSEric Paris 		goto out;
10111da177e4SLinus Torvalds 
10120619f0f5SStephen Smalley 	length = security_sid_to_context(state, newsid, &newcon, &len);
1013b77a493bSEric Paris 	if (length)
1014b77a493bSEric Paris 		goto out;
10151da177e4SLinus Torvalds 
10161da177e4SLinus Torvalds 	length = -ERANGE;
1017b77a493bSEric Paris 	if (len > SIMPLE_TRANSACTION_LIMIT)
1018b77a493bSEric Paris 		goto out;
10191da177e4SLinus Torvalds 
10201da177e4SLinus Torvalds 	memcpy(buf, newcon, len);
10211da177e4SLinus Torvalds 	length = len;
10221da177e4SLinus Torvalds out:
1023b77a493bSEric Paris 	kfree(newcon);
1024b77a493bSEric Paris 	kfree(tcon);
10251da177e4SLinus Torvalds 	kfree(scon);
10261da177e4SLinus Torvalds 	return length;
10271da177e4SLinus Torvalds }
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
10301da177e4SLinus Torvalds {
10310619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
10320619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
1033b77a493bSEric Paris 	char *con = NULL, *user = NULL, *ptr;
1034b77a493bSEric Paris 	u32 sid, *sids = NULL;
10351da177e4SLinus Torvalds 	ssize_t length;
10361da177e4SLinus Torvalds 	char *newcon;
10371da177e4SLinus Torvalds 	int i, rc;
10381da177e4SLinus Torvalds 	u32 len, nsids;
10391da177e4SLinus Torvalds 
10406b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
10416b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
1042be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__COMPUTE_USER,
1043be0554c9SStephen Smalley 			      NULL);
10441da177e4SLinus Torvalds 	if (length)
10456eab04a8SJustin P. Mattock 		goto out;
10461da177e4SLinus Torvalds 
10471da177e4SLinus Torvalds 	length = -ENOMEM;
104889d155efSJames Morris 	con = kzalloc(size + 1, GFP_KERNEL);
10491da177e4SLinus Torvalds 	if (!con)
10506eab04a8SJustin P. Mattock 		goto out;
10511da177e4SLinus Torvalds 
1052b77a493bSEric Paris 	length = -ENOMEM;
105389d155efSJames Morris 	user = kzalloc(size + 1, GFP_KERNEL);
10541da177e4SLinus Torvalds 	if (!user)
10551da177e4SLinus Torvalds 		goto out;
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds 	length = -EINVAL;
10581da177e4SLinus Torvalds 	if (sscanf(buf, "%s %s", con, user) != 2)
1059b77a493bSEric Paris 		goto out;
10601da177e4SLinus Torvalds 
10610619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, con, &sid, GFP_KERNEL);
1062b77a493bSEric Paris 	if (length)
1063b77a493bSEric Paris 		goto out;
10641da177e4SLinus Torvalds 
10650619f0f5SStephen Smalley 	length = security_get_user_sids(state, sid, user, &sids, &nsids);
1066b77a493bSEric Paris 	if (length)
1067b77a493bSEric Paris 		goto out;
10681da177e4SLinus Torvalds 
10691da177e4SLinus Torvalds 	length = sprintf(buf, "%u", nsids) + 1;
10701da177e4SLinus Torvalds 	ptr = buf + length;
10711da177e4SLinus Torvalds 	for (i = 0; i < nsids; i++) {
10720619f0f5SStephen Smalley 		rc = security_sid_to_context(state, sids[i], &newcon, &len);
10731da177e4SLinus Torvalds 		if (rc) {
10741da177e4SLinus Torvalds 			length = rc;
1075b77a493bSEric Paris 			goto out;
10761da177e4SLinus Torvalds 		}
10771da177e4SLinus Torvalds 		if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
10781da177e4SLinus Torvalds 			kfree(newcon);
10791da177e4SLinus Torvalds 			length = -ERANGE;
1080b77a493bSEric Paris 			goto out;
10811da177e4SLinus Torvalds 		}
10821da177e4SLinus Torvalds 		memcpy(ptr, newcon, len);
10831da177e4SLinus Torvalds 		kfree(newcon);
10841da177e4SLinus Torvalds 		ptr += len;
10851da177e4SLinus Torvalds 		length += len;
10861da177e4SLinus Torvalds 	}
10871da177e4SLinus Torvalds out:
1088b77a493bSEric Paris 	kfree(sids);
1089b77a493bSEric Paris 	kfree(user);
10901da177e4SLinus Torvalds 	kfree(con);
10911da177e4SLinus Torvalds 	return length;
10921da177e4SLinus Torvalds }
10931da177e4SLinus Torvalds 
10941da177e4SLinus Torvalds static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
10951da177e4SLinus Torvalds {
10960619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
10970619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
1098b77a493bSEric Paris 	char *scon = NULL, *tcon = NULL;
10991da177e4SLinus Torvalds 	u32 ssid, tsid, newsid;
11001da177e4SLinus Torvalds 	u16 tclass;
11011da177e4SLinus Torvalds 	ssize_t length;
1102b77a493bSEric Paris 	char *newcon = NULL;
11031da177e4SLinus Torvalds 	u32 len;
11041da177e4SLinus Torvalds 
11056b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
11066b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
1107be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__COMPUTE_MEMBER,
1108be0554c9SStephen Smalley 			      NULL);
11091da177e4SLinus Torvalds 	if (length)
1110b77a493bSEric Paris 		goto out;
11111da177e4SLinus Torvalds 
11121da177e4SLinus Torvalds 	length = -ENOMEM;
111389d155efSJames Morris 	scon = kzalloc(size + 1, GFP_KERNEL);
11141da177e4SLinus Torvalds 	if (!scon)
11156eab04a8SJustin P. Mattock 		goto out;
11161da177e4SLinus Torvalds 
1117b77a493bSEric Paris 	length = -ENOMEM;
111889d155efSJames Morris 	tcon = kzalloc(size + 1, GFP_KERNEL);
11191da177e4SLinus Torvalds 	if (!tcon)
11201da177e4SLinus Torvalds 		goto out;
11211da177e4SLinus Torvalds 
11221da177e4SLinus Torvalds 	length = -EINVAL;
11231da177e4SLinus Torvalds 	if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
1124b77a493bSEric Paris 		goto out;
11251da177e4SLinus Torvalds 
11260619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
1127b77a493bSEric Paris 	if (length)
1128b77a493bSEric Paris 		goto out;
1129b77a493bSEric Paris 
11300619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
1131b77a493bSEric Paris 	if (length)
1132b77a493bSEric Paris 		goto out;
11331da177e4SLinus Torvalds 
11340619f0f5SStephen Smalley 	length = security_member_sid(state, ssid, tsid, tclass, &newsid);
1135b77a493bSEric Paris 	if (length)
1136b77a493bSEric Paris 		goto out;
11371da177e4SLinus Torvalds 
11380619f0f5SStephen Smalley 	length = security_sid_to_context(state, newsid, &newcon, &len);
1139b77a493bSEric Paris 	if (length)
1140b77a493bSEric Paris 		goto out;
11411da177e4SLinus Torvalds 
1142b77a493bSEric Paris 	length = -ERANGE;
11431da177e4SLinus Torvalds 	if (len > SIMPLE_TRANSACTION_LIMIT) {
1144744ba35eSEric Paris 		printk(KERN_ERR "SELinux: %s:  context size (%u) exceeds "
1145744ba35eSEric Paris 			"payload max\n", __func__, len);
1146b77a493bSEric Paris 		goto out;
11471da177e4SLinus Torvalds 	}
11481da177e4SLinus Torvalds 
11491da177e4SLinus Torvalds 	memcpy(buf, newcon, len);
11501da177e4SLinus Torvalds 	length = len;
11511da177e4SLinus Torvalds out:
1152b77a493bSEric Paris 	kfree(newcon);
1153b77a493bSEric Paris 	kfree(tcon);
11541da177e4SLinus Torvalds 	kfree(scon);
11551da177e4SLinus Torvalds 	return length;
11561da177e4SLinus Torvalds }
11571da177e4SLinus Torvalds 
11581da177e4SLinus Torvalds static struct inode *sel_make_inode(struct super_block *sb, int mode)
11591da177e4SLinus Torvalds {
11601da177e4SLinus Torvalds 	struct inode *ret = new_inode(sb);
11611da177e4SLinus Torvalds 
11621da177e4SLinus Torvalds 	if (ret) {
11631da177e4SLinus Torvalds 		ret->i_mode = mode;
1164078cd827SDeepa Dinamani 		ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret);
11651da177e4SLinus Torvalds 	}
11661da177e4SLinus Torvalds 	return ret;
11671da177e4SLinus Torvalds }
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds static ssize_t sel_read_bool(struct file *filep, char __user *buf,
11701da177e4SLinus Torvalds 			     size_t count, loff_t *ppos)
11711da177e4SLinus Torvalds {
11720619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
11731da177e4SLinus Torvalds 	char *page = NULL;
11741da177e4SLinus Torvalds 	ssize_t length;
11751da177e4SLinus Torvalds 	ssize_t ret;
11761da177e4SLinus Torvalds 	int cur_enforcing;
1177496ad9aaSAl Viro 	unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
1178d313f948SStephen Smalley 	const char *name = filep->f_path.dentry->d_name.name;
11791da177e4SLinus Torvalds 
11800619f0f5SStephen Smalley 	mutex_lock(&fsi->mutex);
11811da177e4SLinus Torvalds 
1182d313f948SStephen Smalley 	ret = -EINVAL;
11830619f0f5SStephen Smalley 	if (index >= fsi->bool_num || strcmp(name,
11840619f0f5SStephen Smalley 					     fsi->bool_pending_names[index]))
1185d313f948SStephen Smalley 		goto out;
11861da177e4SLinus Torvalds 
11871da177e4SLinus Torvalds 	ret = -ENOMEM;
1188b77a493bSEric Paris 	page = (char *)get_zeroed_page(GFP_KERNEL);
1189b77a493bSEric Paris 	if (!page)
11901da177e4SLinus Torvalds 		goto out;
11911da177e4SLinus Torvalds 
11920619f0f5SStephen Smalley 	cur_enforcing = security_get_bool_value(fsi->state, index);
11931da177e4SLinus Torvalds 	if (cur_enforcing < 0) {
11941da177e4SLinus Torvalds 		ret = cur_enforcing;
11951da177e4SLinus Torvalds 		goto out;
11961da177e4SLinus Torvalds 	}
11971da177e4SLinus Torvalds 	length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
11980619f0f5SStephen Smalley 			  fsi->bool_pending_values[index]);
119968bdcf28SStephen Smalley 	ret = simple_read_from_buffer(buf, count, ppos, page, length);
12001da177e4SLinus Torvalds out:
12010619f0f5SStephen Smalley 	mutex_unlock(&fsi->mutex);
12021da177e4SLinus Torvalds 	free_page((unsigned long)page);
12031da177e4SLinus Torvalds 	return ret;
12041da177e4SLinus Torvalds }
12051da177e4SLinus Torvalds 
12061da177e4SLinus Torvalds static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
12071da177e4SLinus Torvalds 			      size_t count, loff_t *ppos)
12081da177e4SLinus Torvalds {
12090619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
12101da177e4SLinus Torvalds 	char *page = NULL;
1211d313f948SStephen Smalley 	ssize_t length;
12121da177e4SLinus Torvalds 	int new_value;
1213496ad9aaSAl Viro 	unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
1214d313f948SStephen Smalley 	const char *name = filep->f_path.dentry->d_name.name;
12151da177e4SLinus Torvalds 
12160619f0f5SStephen Smalley 	mutex_lock(&fsi->mutex);
12171da177e4SLinus Torvalds 
12186b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
12196b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
1220be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__SETBOOL,
1221be0554c9SStephen Smalley 			      NULL);
12221da177e4SLinus Torvalds 	if (length)
12231da177e4SLinus Torvalds 		goto out;
12241da177e4SLinus Torvalds 
1225d313f948SStephen Smalley 	length = -EINVAL;
12260619f0f5SStephen Smalley 	if (index >= fsi->bool_num || strcmp(name,
12270619f0f5SStephen Smalley 					     fsi->bool_pending_names[index]))
1228d313f948SStephen Smalley 		goto out;
1229d313f948SStephen Smalley 
12301da177e4SLinus Torvalds 	length = -ENOMEM;
1231b77a493bSEric Paris 	if (count >= PAGE_SIZE)
12321da177e4SLinus Torvalds 		goto out;
1233d313f948SStephen Smalley 
12341da177e4SLinus Torvalds 	/* No partial writes. */
1235d313f948SStephen Smalley 	length = -EINVAL;
1236b77a493bSEric Paris 	if (*ppos != 0)
12371da177e4SLinus Torvalds 		goto out;
1238b77a493bSEric Paris 
12398365a719SAl Viro 	page = memdup_user_nul(buf, count);
12408365a719SAl Viro 	if (IS_ERR(page)) {
12418365a719SAl Viro 		length = PTR_ERR(page);
12428365a719SAl Viro 		page = NULL;
12431da177e4SLinus Torvalds 		goto out;
12448365a719SAl Viro 	}
12451da177e4SLinus Torvalds 
12461da177e4SLinus Torvalds 	length = -EINVAL;
12471da177e4SLinus Torvalds 	if (sscanf(page, "%d", &new_value) != 1)
12481da177e4SLinus Torvalds 		goto out;
12491da177e4SLinus Torvalds 
12501da177e4SLinus Torvalds 	if (new_value)
12511da177e4SLinus Torvalds 		new_value = 1;
12521da177e4SLinus Torvalds 
12530619f0f5SStephen Smalley 	fsi->bool_pending_values[index] = new_value;
12541da177e4SLinus Torvalds 	length = count;
12551da177e4SLinus Torvalds 
12561da177e4SLinus Torvalds out:
12570619f0f5SStephen Smalley 	mutex_unlock(&fsi->mutex);
12588365a719SAl Viro 	kfree(page);
12591da177e4SLinus Torvalds 	return length;
12601da177e4SLinus Torvalds }
12611da177e4SLinus Torvalds 
12629c2e08c5SArjan van de Ven static const struct file_operations sel_bool_ops = {
12631da177e4SLinus Torvalds 	.read		= sel_read_bool,
12641da177e4SLinus Torvalds 	.write		= sel_write_bool,
126557a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
12661da177e4SLinus Torvalds };
12671da177e4SLinus Torvalds 
12681da177e4SLinus Torvalds static ssize_t sel_commit_bools_write(struct file *filep,
12691da177e4SLinus Torvalds 				      const char __user *buf,
12701da177e4SLinus Torvalds 				      size_t count, loff_t *ppos)
12711da177e4SLinus Torvalds {
12720619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
12731da177e4SLinus Torvalds 	char *page = NULL;
1274d313f948SStephen Smalley 	ssize_t length;
12751da177e4SLinus Torvalds 	int new_value;
12761da177e4SLinus Torvalds 
12770619f0f5SStephen Smalley 	mutex_lock(&fsi->mutex);
12781da177e4SLinus Torvalds 
12796b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
12806b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
1281be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__SETBOOL,
1282be0554c9SStephen Smalley 			      NULL);
12831da177e4SLinus Torvalds 	if (length)
12841da177e4SLinus Torvalds 		goto out;
12851da177e4SLinus Torvalds 
12861da177e4SLinus Torvalds 	length = -ENOMEM;
1287b77a493bSEric Paris 	if (count >= PAGE_SIZE)
12881da177e4SLinus Torvalds 		goto out;
1289b77a493bSEric Paris 
12901da177e4SLinus Torvalds 	/* No partial writes. */
1291b77a493bSEric Paris 	length = -EINVAL;
1292b77a493bSEric Paris 	if (*ppos != 0)
12931da177e4SLinus Torvalds 		goto out;
1294b77a493bSEric Paris 
12958365a719SAl Viro 	page = memdup_user_nul(buf, count);
12968365a719SAl Viro 	if (IS_ERR(page)) {
12978365a719SAl Viro 		length = PTR_ERR(page);
12988365a719SAl Viro 		page = NULL;
12991da177e4SLinus Torvalds 		goto out;
13008365a719SAl Viro 	}
13011da177e4SLinus Torvalds 
13021da177e4SLinus Torvalds 	length = -EINVAL;
13031da177e4SLinus Torvalds 	if (sscanf(page, "%d", &new_value) != 1)
13041da177e4SLinus Torvalds 		goto out;
13051da177e4SLinus Torvalds 
1306b77a493bSEric Paris 	length = 0;
13070619f0f5SStephen Smalley 	if (new_value && fsi->bool_pending_values)
13080619f0f5SStephen Smalley 		length = security_set_bools(fsi->state, fsi->bool_num,
13090619f0f5SStephen Smalley 					    fsi->bool_pending_values);
13101da177e4SLinus Torvalds 
1311b77a493bSEric Paris 	if (!length)
13121da177e4SLinus Torvalds 		length = count;
13131da177e4SLinus Torvalds 
13141da177e4SLinus Torvalds out:
13150619f0f5SStephen Smalley 	mutex_unlock(&fsi->mutex);
13168365a719SAl Viro 	kfree(page);
13171da177e4SLinus Torvalds 	return length;
13181da177e4SLinus Torvalds }
13191da177e4SLinus Torvalds 
13209c2e08c5SArjan van de Ven static const struct file_operations sel_commit_bools_ops = {
13211da177e4SLinus Torvalds 	.write		= sel_commit_bools_write,
132257a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
13231da177e4SLinus Torvalds };
13241da177e4SLinus Torvalds 
13250c92d7c7SChristopher J. PeBenito static void sel_remove_entries(struct dentry *de)
13261da177e4SLinus Torvalds {
1327ad52184bSAl Viro 	d_genocide(de);
1328ad52184bSAl Viro 	shrink_dcache_parent(de);
13291da177e4SLinus Torvalds }
13301da177e4SLinus Torvalds 
13311da177e4SLinus Torvalds #define BOOL_DIR_NAME "booleans"
13321da177e4SLinus Torvalds 
13330619f0f5SStephen Smalley static int sel_make_bools(struct selinux_fs_info *fsi)
13341da177e4SLinus Torvalds {
1335b77a493bSEric Paris 	int i, ret;
13361da177e4SLinus Torvalds 	ssize_t len;
13371da177e4SLinus Torvalds 	struct dentry *dentry = NULL;
13380619f0f5SStephen Smalley 	struct dentry *dir = fsi->bool_dir;
13391da177e4SLinus Torvalds 	struct inode *inode = NULL;
13401da177e4SLinus Torvalds 	struct inode_security_struct *isec;
13411da177e4SLinus Torvalds 	char **names = NULL, *page;
13421da177e4SLinus Torvalds 	int num;
13431da177e4SLinus Torvalds 	int *values = NULL;
13441da177e4SLinus Torvalds 	u32 sid;
13451da177e4SLinus Torvalds 
13461da177e4SLinus Torvalds 	/* remove any existing files */
13470619f0f5SStephen Smalley 	for (i = 0; i < fsi->bool_num; i++)
13480619f0f5SStephen Smalley 		kfree(fsi->bool_pending_names[i]);
13490619f0f5SStephen Smalley 	kfree(fsi->bool_pending_names);
13500619f0f5SStephen Smalley 	kfree(fsi->bool_pending_values);
13510619f0f5SStephen Smalley 	fsi->bool_num = 0;
13520619f0f5SStephen Smalley 	fsi->bool_pending_names = NULL;
13530619f0f5SStephen Smalley 	fsi->bool_pending_values = NULL;
13541da177e4SLinus Torvalds 
13550c92d7c7SChristopher J. PeBenito 	sel_remove_entries(dir);
13561da177e4SLinus Torvalds 
1357b77a493bSEric Paris 	ret = -ENOMEM;
13581872981bSEric Paris 	page = (char *)get_zeroed_page(GFP_KERNEL);
13591872981bSEric Paris 	if (!page)
1360b77a493bSEric Paris 		goto out;
13611da177e4SLinus Torvalds 
13620619f0f5SStephen Smalley 	ret = security_get_bools(fsi->state, &num, &names, &values);
1363b77a493bSEric Paris 	if (ret)
13641da177e4SLinus Torvalds 		goto out;
13651da177e4SLinus Torvalds 
13661da177e4SLinus Torvalds 	for (i = 0; i < num; i++) {
1367b77a493bSEric Paris 		ret = -ENOMEM;
13681da177e4SLinus Torvalds 		dentry = d_alloc_name(dir, names[i]);
1369b77a493bSEric Paris 		if (!dentry)
1370b77a493bSEric Paris 			goto out;
13711da177e4SLinus Torvalds 
1372b77a493bSEric Paris 		ret = -ENOMEM;
1373b77a493bSEric Paris 		inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1374b77a493bSEric Paris 		if (!inode)
1375b77a493bSEric Paris 			goto out;
1376b77a493bSEric Paris 
13771da177e4SLinus Torvalds 		ret = -ENAMETOOLONG;
1378cc1dad71SAl Viro 		len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
1379b77a493bSEric Paris 		if (len >= PAGE_SIZE)
1380b77a493bSEric Paris 			goto out;
1381b77a493bSEric Paris 
13821da177e4SLinus Torvalds 		isec = (struct inode_security_struct *)inode->i_security;
13830619f0f5SStephen Smalley 		ret = security_genfs_sid(fsi->state, "selinuxfs", page,
1384aa8e712cSStephen Smalley 					 SECCLASS_FILE, &sid);
13854262fb51SGary Tierney 		if (ret) {
1386900fde06SGary Tierney 			pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n",
1387900fde06SGary Tierney 					   page);
1388900fde06SGary Tierney 			sid = SECINITSID_SECURITY;
13894262fb51SGary Tierney 		}
13904262fb51SGary Tierney 
13911da177e4SLinus Torvalds 		isec->sid = sid;
139242059112SAndreas Gruenbacher 		isec->initialized = LABEL_INITIALIZED;
13931da177e4SLinus Torvalds 		inode->i_fop = &sel_bool_ops;
1394bce34bc0SJames Carter 		inode->i_ino = i|SEL_BOOL_INO_OFFSET;
13951da177e4SLinus Torvalds 		d_add(dentry, inode);
13961da177e4SLinus Torvalds 	}
13970619f0f5SStephen Smalley 	fsi->bool_num = num;
13980619f0f5SStephen Smalley 	fsi->bool_pending_names = names;
13990619f0f5SStephen Smalley 	fsi->bool_pending_values = values;
1400b77a493bSEric Paris 
1401b77a493bSEric Paris 	free_page((unsigned long)page);
1402b77a493bSEric Paris 	return 0;
14031da177e4SLinus Torvalds out:
14041da177e4SLinus Torvalds 	free_page((unsigned long)page);
1405b77a493bSEric Paris 
14061da177e4SLinus Torvalds 	if (names) {
14079a5f04bfSJesper Juhl 		for (i = 0; i < num; i++)
14081da177e4SLinus Torvalds 			kfree(names[i]);
14091da177e4SLinus Torvalds 		kfree(names);
14101da177e4SLinus Torvalds 	}
141120c19e41SDavi Arnaut 	kfree(values);
14120c92d7c7SChristopher J. PeBenito 	sel_remove_entries(dir);
1413b77a493bSEric Paris 
1414b77a493bSEric Paris 	return ret;
14151da177e4SLinus Torvalds }
14161da177e4SLinus Torvalds 
14171da177e4SLinus Torvalds static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
14181da177e4SLinus Torvalds 					    size_t count, loff_t *ppos)
14191da177e4SLinus Torvalds {
14206b6bc620SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
14216b6bc620SStephen Smalley 	struct selinux_state *state = fsi->state;
14221da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
14231da177e4SLinus Torvalds 	ssize_t length;
14241da177e4SLinus Torvalds 
14256b6bc620SStephen Smalley 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
14266b6bc620SStephen Smalley 			   avc_get_cache_threshold(state->avc));
14271da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
14281da177e4SLinus Torvalds }
14291da177e4SLinus Torvalds 
14301da177e4SLinus Torvalds static ssize_t sel_write_avc_cache_threshold(struct file *file,
14311da177e4SLinus Torvalds 					     const char __user *buf,
14321da177e4SLinus Torvalds 					     size_t count, loff_t *ppos)
14331da177e4SLinus Torvalds 
14341da177e4SLinus Torvalds {
14356b6bc620SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
14366b6bc620SStephen Smalley 	struct selinux_state *state = fsi->state;
14378365a719SAl Viro 	char *page;
14381da177e4SLinus Torvalds 	ssize_t ret;
1439309c5fadSHeinrich Schuchardt 	unsigned int new_value;
14401da177e4SLinus Torvalds 
14416b6bc620SStephen Smalley 	ret = avc_has_perm(&selinux_state,
14426b6bc620SStephen Smalley 			   current_sid(), SECINITSID_SECURITY,
1443be0554c9SStephen Smalley 			   SECCLASS_SECURITY, SECURITY__SETSECPARAM,
1444be0554c9SStephen Smalley 			   NULL);
14451da177e4SLinus Torvalds 	if (ret)
14468365a719SAl Viro 		return ret;
1447b77a493bSEric Paris 
1448b77a493bSEric Paris 	if (count >= PAGE_SIZE)
14498365a719SAl Viro 		return -ENOMEM;
1450b77a493bSEric Paris 
1451b77a493bSEric Paris 	/* No partial writes. */
1452b77a493bSEric Paris 	if (*ppos != 0)
14538365a719SAl Viro 		return -EINVAL;
1454b77a493bSEric Paris 
14558365a719SAl Viro 	page = memdup_user_nul(buf, count);
14568365a719SAl Viro 	if (IS_ERR(page))
14578365a719SAl Viro 		return PTR_ERR(page);
1458b77a493bSEric Paris 
1459b77a493bSEric Paris 	ret = -EINVAL;
1460b77a493bSEric Paris 	if (sscanf(page, "%u", &new_value) != 1)
1461b77a493bSEric Paris 		goto out;
1462b77a493bSEric Paris 
14636b6bc620SStephen Smalley 	avc_set_cache_threshold(state->avc, new_value);
1464b77a493bSEric Paris 
14651da177e4SLinus Torvalds 	ret = count;
14661da177e4SLinus Torvalds out:
14678365a719SAl Viro 	kfree(page);
14681da177e4SLinus Torvalds 	return ret;
14691da177e4SLinus Torvalds }
14701da177e4SLinus Torvalds 
14711da177e4SLinus Torvalds static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
14721da177e4SLinus Torvalds 				       size_t count, loff_t *ppos)
14731da177e4SLinus Torvalds {
14746b6bc620SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
14756b6bc620SStephen Smalley 	struct selinux_state *state = fsi->state;
14761da177e4SLinus Torvalds 	char *page;
1477b77a493bSEric Paris 	ssize_t length;
14781da177e4SLinus Torvalds 
14791da177e4SLinus Torvalds 	page = (char *)__get_free_page(GFP_KERNEL);
1480b77a493bSEric Paris 	if (!page)
1481b77a493bSEric Paris 		return -ENOMEM;
1482b77a493bSEric Paris 
14836b6bc620SStephen Smalley 	length = avc_get_hash_stats(state->avc, page);
1484b77a493bSEric Paris 	if (length >= 0)
1485b77a493bSEric Paris 		length = simple_read_from_buffer(buf, count, ppos, page, length);
14861da177e4SLinus Torvalds 	free_page((unsigned long)page);
1487b77a493bSEric Paris 
1488b77a493bSEric Paris 	return length;
14891da177e4SLinus Torvalds }
14901da177e4SLinus Torvalds 
14919c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_threshold_ops = {
14921da177e4SLinus Torvalds 	.read		= sel_read_avc_cache_threshold,
14931da177e4SLinus Torvalds 	.write		= sel_write_avc_cache_threshold,
149457a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
14951da177e4SLinus Torvalds };
14961da177e4SLinus Torvalds 
14979c2e08c5SArjan van de Ven static const struct file_operations sel_avc_hash_stats_ops = {
14981da177e4SLinus Torvalds 	.read		= sel_read_avc_hash_stats,
149957a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
15001da177e4SLinus Torvalds };
15011da177e4SLinus Torvalds 
15021da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
15031da177e4SLinus Torvalds static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
15041da177e4SLinus Torvalds {
15051da177e4SLinus Torvalds 	int cpu;
15061da177e4SLinus Torvalds 
15074f4b6c1aSRusty Russell 	for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
15081da177e4SLinus Torvalds 		if (!cpu_possible(cpu))
15091da177e4SLinus Torvalds 			continue;
15101da177e4SLinus Torvalds 		*idx = cpu + 1;
15111da177e4SLinus Torvalds 		return &per_cpu(avc_cache_stats, cpu);
15121da177e4SLinus Torvalds 	}
15131da177e4SLinus Torvalds 	return NULL;
15141da177e4SLinus Torvalds }
15151da177e4SLinus Torvalds 
15161da177e4SLinus Torvalds static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
15171da177e4SLinus Torvalds {
15181da177e4SLinus Torvalds 	loff_t n = *pos - 1;
15191da177e4SLinus Torvalds 
15201da177e4SLinus Torvalds 	if (*pos == 0)
15211da177e4SLinus Torvalds 		return SEQ_START_TOKEN;
15221da177e4SLinus Torvalds 
15231da177e4SLinus Torvalds 	return sel_avc_get_stat_idx(&n);
15241da177e4SLinus Torvalds }
15251da177e4SLinus Torvalds 
15261da177e4SLinus Torvalds static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
15271da177e4SLinus Torvalds {
15281da177e4SLinus Torvalds 	return sel_avc_get_stat_idx(pos);
15291da177e4SLinus Torvalds }
15301da177e4SLinus Torvalds 
15311da177e4SLinus Torvalds static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
15321da177e4SLinus Torvalds {
15331da177e4SLinus Torvalds 	struct avc_cache_stats *st = v;
15341da177e4SLinus Torvalds 
1535710a0647SMarkus Elfring 	if (v == SEQ_START_TOKEN) {
1536710a0647SMarkus Elfring 		seq_puts(seq,
1537710a0647SMarkus Elfring 			 "lookups hits misses allocations reclaims frees\n");
1538710a0647SMarkus Elfring 	} else {
1539257313b2SLinus Torvalds 		unsigned int lookups = st->lookups;
1540257313b2SLinus Torvalds 		unsigned int misses = st->misses;
1541257313b2SLinus Torvalds 		unsigned int hits = lookups - misses;
1542257313b2SLinus Torvalds 		seq_printf(seq, "%u %u %u %u %u %u\n", lookups,
1543257313b2SLinus Torvalds 			   hits, misses, st->allocations,
15441da177e4SLinus Torvalds 			   st->reclaims, st->frees);
1545257313b2SLinus Torvalds 	}
15461da177e4SLinus Torvalds 	return 0;
15471da177e4SLinus Torvalds }
15481da177e4SLinus Torvalds 
15491da177e4SLinus Torvalds static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
15501da177e4SLinus Torvalds { }
15511da177e4SLinus Torvalds 
15521996a109SJan Engelhardt static const struct seq_operations sel_avc_cache_stats_seq_ops = {
15531da177e4SLinus Torvalds 	.start		= sel_avc_stats_seq_start,
15541da177e4SLinus Torvalds 	.next		= sel_avc_stats_seq_next,
15551da177e4SLinus Torvalds 	.show		= sel_avc_stats_seq_show,
15561da177e4SLinus Torvalds 	.stop		= sel_avc_stats_seq_stop,
15571da177e4SLinus Torvalds };
15581da177e4SLinus Torvalds 
15591da177e4SLinus Torvalds static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
15601da177e4SLinus Torvalds {
15611da177e4SLinus Torvalds 	return seq_open(file, &sel_avc_cache_stats_seq_ops);
15621da177e4SLinus Torvalds }
15631da177e4SLinus Torvalds 
15649c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_stats_ops = {
15651da177e4SLinus Torvalds 	.open		= sel_open_avc_cache_stats,
15661da177e4SLinus Torvalds 	.read		= seq_read,
15671da177e4SLinus Torvalds 	.llseek		= seq_lseek,
15681da177e4SLinus Torvalds 	.release	= seq_release,
15691da177e4SLinus Torvalds };
15701da177e4SLinus Torvalds #endif
15711da177e4SLinus Torvalds 
15721da177e4SLinus Torvalds static int sel_make_avc_files(struct dentry *dir)
15731da177e4SLinus Torvalds {
15740619f0f5SStephen Smalley 	struct super_block *sb = dir->d_sb;
15750619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = sb->s_fs_info;
1576b77a493bSEric Paris 	int i;
1577cda37124SEric Biggers 	static const struct tree_descr files[] = {
15781da177e4SLinus Torvalds 		{ "cache_threshold",
15791da177e4SLinus Torvalds 		  &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
15801da177e4SLinus Torvalds 		{ "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
15811da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
15821da177e4SLinus Torvalds 		{ "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
15831da177e4SLinus Torvalds #endif
15841da177e4SLinus Torvalds 	};
15851da177e4SLinus Torvalds 
15866e20a64aSNicolas Kaiser 	for (i = 0; i < ARRAY_SIZE(files); i++) {
15871da177e4SLinus Torvalds 		struct inode *inode;
15881da177e4SLinus Torvalds 		struct dentry *dentry;
15891da177e4SLinus Torvalds 
15901da177e4SLinus Torvalds 		dentry = d_alloc_name(dir, files[i].name);
1591b77a493bSEric Paris 		if (!dentry)
1592b77a493bSEric Paris 			return -ENOMEM;
15931da177e4SLinus Torvalds 
15941da177e4SLinus Torvalds 		inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1595b77a493bSEric Paris 		if (!inode)
1596b77a493bSEric Paris 			return -ENOMEM;
1597b77a493bSEric Paris 
15981da177e4SLinus Torvalds 		inode->i_fop = files[i].ops;
15990619f0f5SStephen Smalley 		inode->i_ino = ++fsi->last_ino;
16001da177e4SLinus Torvalds 		d_add(dentry, inode);
16011da177e4SLinus Torvalds 	}
1602b77a493bSEric Paris 
1603b77a493bSEric Paris 	return 0;
16041da177e4SLinus Torvalds }
16051da177e4SLinus Torvalds 
1606f0ee2e46SJames Carter static ssize_t sel_read_initcon(struct file *file, char __user *buf,
1607f0ee2e46SJames Carter 				size_t count, loff_t *ppos)
1608f0ee2e46SJames Carter {
16090619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1610f0ee2e46SJames Carter 	char *con;
1611f0ee2e46SJames Carter 	u32 sid, len;
1612f0ee2e46SJames Carter 	ssize_t ret;
1613f0ee2e46SJames Carter 
1614496ad9aaSAl Viro 	sid = file_inode(file)->i_ino&SEL_INO_MASK;
16150619f0f5SStephen Smalley 	ret = security_sid_to_context(fsi->state, sid, &con, &len);
1616b77a493bSEric Paris 	if (ret)
1617f0ee2e46SJames Carter 		return ret;
1618f0ee2e46SJames Carter 
1619f0ee2e46SJames Carter 	ret = simple_read_from_buffer(buf, count, ppos, con, len);
1620f0ee2e46SJames Carter 	kfree(con);
1621f0ee2e46SJames Carter 	return ret;
1622f0ee2e46SJames Carter }
1623f0ee2e46SJames Carter 
1624f0ee2e46SJames Carter static const struct file_operations sel_initcon_ops = {
1625f0ee2e46SJames Carter 	.read		= sel_read_initcon,
162657a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
1627f0ee2e46SJames Carter };
1628f0ee2e46SJames Carter 
1629f0ee2e46SJames Carter static int sel_make_initcon_files(struct dentry *dir)
1630f0ee2e46SJames Carter {
1631b77a493bSEric Paris 	int i;
1632f0ee2e46SJames Carter 
1633f0ee2e46SJames Carter 	for (i = 1; i <= SECINITSID_NUM; i++) {
1634f0ee2e46SJames Carter 		struct inode *inode;
1635f0ee2e46SJames Carter 		struct dentry *dentry;
1636f0ee2e46SJames Carter 		dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
1637b77a493bSEric Paris 		if (!dentry)
1638b77a493bSEric Paris 			return -ENOMEM;
1639f0ee2e46SJames Carter 
1640f0ee2e46SJames Carter 		inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1641b77a493bSEric Paris 		if (!inode)
1642b77a493bSEric Paris 			return -ENOMEM;
1643b77a493bSEric Paris 
1644f0ee2e46SJames Carter 		inode->i_fop = &sel_initcon_ops;
1645f0ee2e46SJames Carter 		inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1646f0ee2e46SJames Carter 		d_add(dentry, inode);
1647f0ee2e46SJames Carter 	}
1648b77a493bSEric Paris 
1649b77a493bSEric Paris 	return 0;
1650f0ee2e46SJames Carter }
1651f0ee2e46SJames Carter 
1652e47c8fc5SChristopher J. PeBenito static inline unsigned long sel_class_to_ino(u16 class)
1653e47c8fc5SChristopher J. PeBenito {
1654e47c8fc5SChristopher J. PeBenito 	return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1655e47c8fc5SChristopher J. PeBenito }
1656e47c8fc5SChristopher J. PeBenito 
1657e47c8fc5SChristopher J. PeBenito static inline u16 sel_ino_to_class(unsigned long ino)
1658e47c8fc5SChristopher J. PeBenito {
165992ae9e82SEric Paris 	return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1);
1660e47c8fc5SChristopher J. PeBenito }
1661e47c8fc5SChristopher J. PeBenito 
1662e47c8fc5SChristopher J. PeBenito static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1663e47c8fc5SChristopher J. PeBenito {
1664e47c8fc5SChristopher J. PeBenito 	return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1665e47c8fc5SChristopher J. PeBenito }
1666e47c8fc5SChristopher J. PeBenito 
1667e47c8fc5SChristopher J. PeBenito static inline u32 sel_ino_to_perm(unsigned long ino)
1668e47c8fc5SChristopher J. PeBenito {
1669e47c8fc5SChristopher J. PeBenito 	return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1670e47c8fc5SChristopher J. PeBenito }
1671e47c8fc5SChristopher J. PeBenito 
1672e47c8fc5SChristopher J. PeBenito static ssize_t sel_read_class(struct file *file, char __user *buf,
1673e47c8fc5SChristopher J. PeBenito 				size_t count, loff_t *ppos)
1674e47c8fc5SChristopher J. PeBenito {
1675496ad9aaSAl Viro 	unsigned long ino = file_inode(file)->i_ino;
1676cc1dad71SAl Viro 	char res[TMPBUFLEN];
1677cc1dad71SAl Viro 	ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_class(ino));
1678cc1dad71SAl Viro 	return simple_read_from_buffer(buf, count, ppos, res, len);
1679e47c8fc5SChristopher J. PeBenito }
1680e47c8fc5SChristopher J. PeBenito 
1681e47c8fc5SChristopher J. PeBenito static const struct file_operations sel_class_ops = {
1682e47c8fc5SChristopher J. PeBenito 	.read		= sel_read_class,
168357a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
1684e47c8fc5SChristopher J. PeBenito };
1685e47c8fc5SChristopher J. PeBenito 
1686e47c8fc5SChristopher J. PeBenito static ssize_t sel_read_perm(struct file *file, char __user *buf,
1687e47c8fc5SChristopher J. PeBenito 				size_t count, loff_t *ppos)
1688e47c8fc5SChristopher J. PeBenito {
1689496ad9aaSAl Viro 	unsigned long ino = file_inode(file)->i_ino;
1690cc1dad71SAl Viro 	char res[TMPBUFLEN];
1691cc1dad71SAl Viro 	ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino));
1692cc1dad71SAl Viro 	return simple_read_from_buffer(buf, count, ppos, res, len);
1693e47c8fc5SChristopher J. PeBenito }
1694e47c8fc5SChristopher J. PeBenito 
1695e47c8fc5SChristopher J. PeBenito static const struct file_operations sel_perm_ops = {
1696e47c8fc5SChristopher J. PeBenito 	.read		= sel_read_perm,
169757a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
1698e47c8fc5SChristopher J. PeBenito };
1699e47c8fc5SChristopher J. PeBenito 
17003bb56b25SPaul Moore static ssize_t sel_read_policycap(struct file *file, char __user *buf,
17013bb56b25SPaul Moore 				  size_t count, loff_t *ppos)
17023bb56b25SPaul Moore {
17030619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
17043bb56b25SPaul Moore 	int value;
17053bb56b25SPaul Moore 	char tmpbuf[TMPBUFLEN];
17063bb56b25SPaul Moore 	ssize_t length;
1707496ad9aaSAl Viro 	unsigned long i_ino = file_inode(file)->i_ino;
17083bb56b25SPaul Moore 
17090619f0f5SStephen Smalley 	value = security_policycap_supported(fsi->state, i_ino & SEL_INO_MASK);
17103bb56b25SPaul Moore 	length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
17113bb56b25SPaul Moore 
17123bb56b25SPaul Moore 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
17133bb56b25SPaul Moore }
17143bb56b25SPaul Moore 
17153bb56b25SPaul Moore static const struct file_operations sel_policycap_ops = {
17163bb56b25SPaul Moore 	.read		= sel_read_policycap,
171757a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
17183bb56b25SPaul Moore };
17193bb56b25SPaul Moore 
1720e47c8fc5SChristopher J. PeBenito static int sel_make_perm_files(char *objclass, int classvalue,
1721e47c8fc5SChristopher J. PeBenito 				struct dentry *dir)
1722e47c8fc5SChristopher J. PeBenito {
17230619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = dir->d_sb->s_fs_info;
1724b77a493bSEric Paris 	int i, rc, nperms;
1725e47c8fc5SChristopher J. PeBenito 	char **perms;
1726e47c8fc5SChristopher J. PeBenito 
17270619f0f5SStephen Smalley 	rc = security_get_permissions(fsi->state, objclass, &perms, &nperms);
1728e47c8fc5SChristopher J. PeBenito 	if (rc)
1729b77a493bSEric Paris 		return rc;
1730e47c8fc5SChristopher J. PeBenito 
1731e47c8fc5SChristopher J. PeBenito 	for (i = 0; i < nperms; i++) {
1732e47c8fc5SChristopher J. PeBenito 		struct inode *inode;
1733e47c8fc5SChristopher J. PeBenito 		struct dentry *dentry;
1734e47c8fc5SChristopher J. PeBenito 
1735b77a493bSEric Paris 		rc = -ENOMEM;
1736e47c8fc5SChristopher J. PeBenito 		dentry = d_alloc_name(dir, perms[i]);
1737b77a493bSEric Paris 		if (!dentry)
1738b77a493bSEric Paris 			goto out;
1739e47c8fc5SChristopher J. PeBenito 
1740e47c8fc5SChristopher J. PeBenito 		rc = -ENOMEM;
1741b77a493bSEric Paris 		inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1742b77a493bSEric Paris 		if (!inode)
1743b77a493bSEric Paris 			goto out;
1744b77a493bSEric Paris 
1745e47c8fc5SChristopher J. PeBenito 		inode->i_fop = &sel_perm_ops;
1746e47c8fc5SChristopher J. PeBenito 		/* i+1 since perm values are 1-indexed */
1747e47c8fc5SChristopher J. PeBenito 		inode->i_ino = sel_perm_to_ino(classvalue, i + 1);
1748e47c8fc5SChristopher J. PeBenito 		d_add(dentry, inode);
1749e47c8fc5SChristopher J. PeBenito 	}
1750b77a493bSEric Paris 	rc = 0;
1751b77a493bSEric Paris out:
1752e47c8fc5SChristopher J. PeBenito 	for (i = 0; i < nperms; i++)
1753e47c8fc5SChristopher J. PeBenito 		kfree(perms[i]);
1754e47c8fc5SChristopher J. PeBenito 	kfree(perms);
1755e47c8fc5SChristopher J. PeBenito 	return rc;
1756e47c8fc5SChristopher J. PeBenito }
1757e47c8fc5SChristopher J. PeBenito 
1758e47c8fc5SChristopher J. PeBenito static int sel_make_class_dir_entries(char *classname, int index,
1759e47c8fc5SChristopher J. PeBenito 					struct dentry *dir)
1760e47c8fc5SChristopher J. PeBenito {
17610619f0f5SStephen Smalley 	struct super_block *sb = dir->d_sb;
17620619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = sb->s_fs_info;
1763e47c8fc5SChristopher J. PeBenito 	struct dentry *dentry = NULL;
1764e47c8fc5SChristopher J. PeBenito 	struct inode *inode = NULL;
1765e47c8fc5SChristopher J. PeBenito 	int rc;
1766e47c8fc5SChristopher J. PeBenito 
1767e47c8fc5SChristopher J. PeBenito 	dentry = d_alloc_name(dir, "index");
1768b77a493bSEric Paris 	if (!dentry)
1769b77a493bSEric Paris 		return -ENOMEM;
1770e47c8fc5SChristopher J. PeBenito 
1771e47c8fc5SChristopher J. PeBenito 	inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1772b77a493bSEric Paris 	if (!inode)
1773b77a493bSEric Paris 		return -ENOMEM;
1774e47c8fc5SChristopher J. PeBenito 
1775e47c8fc5SChristopher J. PeBenito 	inode->i_fop = &sel_class_ops;
1776e47c8fc5SChristopher J. PeBenito 	inode->i_ino = sel_class_to_ino(index);
1777e47c8fc5SChristopher J. PeBenito 	d_add(dentry, inode);
1778e47c8fc5SChristopher J. PeBenito 
17790619f0f5SStephen Smalley 	dentry = sel_make_dir(dir, "perms", &fsi->last_class_ino);
1780a1c2aa1eSAl Viro 	if (IS_ERR(dentry))
1781a1c2aa1eSAl Viro 		return PTR_ERR(dentry);
1782e47c8fc5SChristopher J. PeBenito 
1783e47c8fc5SChristopher J. PeBenito 	rc = sel_make_perm_files(classname, index, dentry);
1784e47c8fc5SChristopher J. PeBenito 
1785e47c8fc5SChristopher J. PeBenito 	return rc;
1786e47c8fc5SChristopher J. PeBenito }
1787e47c8fc5SChristopher J. PeBenito 
17880619f0f5SStephen Smalley static int sel_make_classes(struct selinux_fs_info *fsi)
1789e47c8fc5SChristopher J. PeBenito {
17900619f0f5SStephen Smalley 
1791b77a493bSEric Paris 	int rc, nclasses, i;
1792e47c8fc5SChristopher J. PeBenito 	char **classes;
1793e47c8fc5SChristopher J. PeBenito 
1794e47c8fc5SChristopher J. PeBenito 	/* delete any existing entries */
17950619f0f5SStephen Smalley 	sel_remove_entries(fsi->class_dir);
1796e47c8fc5SChristopher J. PeBenito 
17970619f0f5SStephen Smalley 	rc = security_get_classes(fsi->state, &classes, &nclasses);
1798b77a493bSEric Paris 	if (rc)
1799b77a493bSEric Paris 		return rc;
1800e47c8fc5SChristopher J. PeBenito 
1801e47c8fc5SChristopher J. PeBenito 	/* +2 since classes are 1-indexed */
18020619f0f5SStephen Smalley 	fsi->last_class_ino = sel_class_to_ino(nclasses + 2);
1803e47c8fc5SChristopher J. PeBenito 
1804e47c8fc5SChristopher J. PeBenito 	for (i = 0; i < nclasses; i++) {
1805e47c8fc5SChristopher J. PeBenito 		struct dentry *class_name_dir;
1806e47c8fc5SChristopher J. PeBenito 
18070619f0f5SStephen Smalley 		class_name_dir = sel_make_dir(fsi->class_dir, classes[i],
18080619f0f5SStephen Smalley 					      &fsi->last_class_ino);
1809a1c2aa1eSAl Viro 		if (IS_ERR(class_name_dir)) {
1810a1c2aa1eSAl Viro 			rc = PTR_ERR(class_name_dir);
1811b77a493bSEric Paris 			goto out;
1812a1c2aa1eSAl Viro 		}
1813e47c8fc5SChristopher J. PeBenito 
1814e47c8fc5SChristopher J. PeBenito 		/* i+1 since class values are 1-indexed */
1815e47c8fc5SChristopher J. PeBenito 		rc = sel_make_class_dir_entries(classes[i], i + 1,
1816e47c8fc5SChristopher J. PeBenito 				class_name_dir);
1817e47c8fc5SChristopher J. PeBenito 		if (rc)
1818b77a493bSEric Paris 			goto out;
1819e47c8fc5SChristopher J. PeBenito 	}
1820b77a493bSEric Paris 	rc = 0;
1821b77a493bSEric Paris out:
1822e47c8fc5SChristopher J. PeBenito 	for (i = 0; i < nclasses; i++)
1823e47c8fc5SChristopher J. PeBenito 		kfree(classes[i]);
1824e47c8fc5SChristopher J. PeBenito 	kfree(classes);
1825e47c8fc5SChristopher J. PeBenito 	return rc;
1826e47c8fc5SChristopher J. PeBenito }
1827e47c8fc5SChristopher J. PeBenito 
18280619f0f5SStephen Smalley static int sel_make_policycap(struct selinux_fs_info *fsi)
18293bb56b25SPaul Moore {
18303bb56b25SPaul Moore 	unsigned int iter;
18313bb56b25SPaul Moore 	struct dentry *dentry = NULL;
18323bb56b25SPaul Moore 	struct inode *inode = NULL;
18333bb56b25SPaul Moore 
18340619f0f5SStephen Smalley 	sel_remove_entries(fsi->policycap_dir);
18353bb56b25SPaul Moore 
18363bb56b25SPaul Moore 	for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
18374dc2fce3SStephen Smalley 		if (iter < ARRAY_SIZE(selinux_policycap_names))
18380619f0f5SStephen Smalley 			dentry = d_alloc_name(fsi->policycap_dir,
18394dc2fce3SStephen Smalley 					      selinux_policycap_names[iter]);
18403bb56b25SPaul Moore 		else
18410619f0f5SStephen Smalley 			dentry = d_alloc_name(fsi->policycap_dir, "unknown");
18423bb56b25SPaul Moore 
18433bb56b25SPaul Moore 		if (dentry == NULL)
18443bb56b25SPaul Moore 			return -ENOMEM;
18453bb56b25SPaul Moore 
18460619f0f5SStephen Smalley 		inode = sel_make_inode(fsi->sb, S_IFREG | 0444);
18473bb56b25SPaul Moore 		if (inode == NULL)
18483bb56b25SPaul Moore 			return -ENOMEM;
18493bb56b25SPaul Moore 
18503bb56b25SPaul Moore 		inode->i_fop = &sel_policycap_ops;
18513bb56b25SPaul Moore 		inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
18523bb56b25SPaul Moore 		d_add(dentry, inode);
18533bb56b25SPaul Moore 	}
18543bb56b25SPaul Moore 
18553bb56b25SPaul Moore 	return 0;
18563bb56b25SPaul Moore }
18573bb56b25SPaul Moore 
1858a1c2aa1eSAl Viro static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
18590dd4ae51SChristopher J. PeBenito 			unsigned long *ino)
18601da177e4SLinus Torvalds {
1861a1c2aa1eSAl Viro 	struct dentry *dentry = d_alloc_name(dir, name);
18621da177e4SLinus Torvalds 	struct inode *inode;
18631da177e4SLinus Torvalds 
1864a1c2aa1eSAl Viro 	if (!dentry)
1865a1c2aa1eSAl Viro 		return ERR_PTR(-ENOMEM);
1866a1c2aa1eSAl Viro 
1867a1c2aa1eSAl Viro 	inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1868a1c2aa1eSAl Viro 	if (!inode) {
1869a1c2aa1eSAl Viro 		dput(dentry);
1870a1c2aa1eSAl Viro 		return ERR_PTR(-ENOMEM);
1871a1c2aa1eSAl Viro 	}
1872b77a493bSEric Paris 
18731da177e4SLinus Torvalds 	inode->i_op = &simple_dir_inode_operations;
18741da177e4SLinus Torvalds 	inode->i_fop = &simple_dir_operations;
18750dd4ae51SChristopher J. PeBenito 	inode->i_ino = ++(*ino);
187640e906f8SJames Morris 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
1877d8c76e6fSDave Hansen 	inc_nlink(inode);
18781da177e4SLinus Torvalds 	d_add(dentry, inode);
1879edb20fb5SJames Morris 	/* bump link count on parent directory, too */
1880ce0b16ddSDavid Howells 	inc_nlink(d_inode(dir));
1881b77a493bSEric Paris 
1882a1c2aa1eSAl Viro 	return dentry;
18831da177e4SLinus Torvalds }
18841da177e4SLinus Torvalds 
18850619f0f5SStephen Smalley #define NULL_FILE_NAME "null"
18860619f0f5SStephen Smalley 
18871da177e4SLinus Torvalds static int sel_fill_super(struct super_block *sb, void *data, int silent)
18881da177e4SLinus Torvalds {
18890619f0f5SStephen Smalley 	struct selinux_fs_info *fsi;
18901da177e4SLinus Torvalds 	int ret;
18911da177e4SLinus Torvalds 	struct dentry *dentry;
1892a1c2aa1eSAl Viro 	struct inode *inode;
18931da177e4SLinus Torvalds 	struct inode_security_struct *isec;
18941da177e4SLinus Torvalds 
1895cda37124SEric Biggers 	static const struct tree_descr selinux_files[] = {
18961da177e4SLinus Torvalds 		[SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
18971da177e4SLinus Torvalds 		[SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
1898ce9982d0SStephen Smalley 		[SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
18991da177e4SLinus Torvalds 		[SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
19001da177e4SLinus Torvalds 		[SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
19011da177e4SLinus Torvalds 		[SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
19021da177e4SLinus Torvalds 		[SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
19031da177e4SLinus Torvalds 		[SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
19041da177e4SLinus Torvalds 		[SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
19051da177e4SLinus Torvalds 		[SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
19061da177e4SLinus Torvalds 		[SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
19071da177e4SLinus Torvalds 		[SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
19081da177e4SLinus Torvalds 		[SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
19093f12070eSEric Paris 		[SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
19103f12070eSEric Paris 		[SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
191111904167SKaiGai Kohei 		[SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO},
191272e8c859SEric Paris 		[SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO},
1913f9df6458SAndrew Perepechko 		[SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops,
1914f9df6458SAndrew Perepechko 					S_IWUGO},
19151da177e4SLinus Torvalds 		/* last one */ {""}
19161da177e4SLinus Torvalds 	};
19170619f0f5SStephen Smalley 
19180619f0f5SStephen Smalley 	ret = selinux_fs_info_create(sb);
19190619f0f5SStephen Smalley 	if (ret)
19200619f0f5SStephen Smalley 		goto err;
19210619f0f5SStephen Smalley 
19221da177e4SLinus Torvalds 	ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
19231da177e4SLinus Torvalds 	if (ret)
1924161ce45aSJames Morris 		goto err;
19251da177e4SLinus Torvalds 
19260619f0f5SStephen Smalley 	fsi = sb->s_fs_info;
19270619f0f5SStephen Smalley 	fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino);
19280619f0f5SStephen Smalley 	if (IS_ERR(fsi->bool_dir)) {
19290619f0f5SStephen Smalley 		ret = PTR_ERR(fsi->bool_dir);
19300619f0f5SStephen Smalley 		fsi->bool_dir = NULL;
1931161ce45aSJames Morris 		goto err;
1932a1c2aa1eSAl Viro 	}
19331da177e4SLinus Torvalds 
1934b77a493bSEric Paris 	ret = -ENOMEM;
19351da177e4SLinus Torvalds 	dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
1936b77a493bSEric Paris 	if (!dentry)
1937161ce45aSJames Morris 		goto err;
19381da177e4SLinus Torvalds 
1939161ce45aSJames Morris 	ret = -ENOMEM;
1940b77a493bSEric Paris 	inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
1941b77a493bSEric Paris 	if (!inode)
1942161ce45aSJames Morris 		goto err;
1943b77a493bSEric Paris 
19440619f0f5SStephen Smalley 	inode->i_ino = ++fsi->last_ino;
19451da177e4SLinus Torvalds 	isec = (struct inode_security_struct *)inode->i_security;
19461da177e4SLinus Torvalds 	isec->sid = SECINITSID_DEVNULL;
19471da177e4SLinus Torvalds 	isec->sclass = SECCLASS_CHR_FILE;
194842059112SAndreas Gruenbacher 	isec->initialized = LABEL_INITIALIZED;
19491da177e4SLinus Torvalds 
19501da177e4SLinus Torvalds 	init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
19511da177e4SLinus Torvalds 	d_add(dentry, inode);
19521da177e4SLinus Torvalds 
19530619f0f5SStephen Smalley 	dentry = sel_make_dir(sb->s_root, "avc", &fsi->last_ino);
1954a1c2aa1eSAl Viro 	if (IS_ERR(dentry)) {
1955a1c2aa1eSAl Viro 		ret = PTR_ERR(dentry);
1956161ce45aSJames Morris 		goto err;
1957a1c2aa1eSAl Viro 	}
19581da177e4SLinus Torvalds 
19591da177e4SLinus Torvalds 	ret = sel_make_avc_files(dentry);
19601da177e4SLinus Torvalds 	if (ret)
1961161ce45aSJames Morris 		goto err;
1962f0ee2e46SJames Carter 
19630619f0f5SStephen Smalley 	dentry = sel_make_dir(sb->s_root, "initial_contexts", &fsi->last_ino);
1964a1c2aa1eSAl Viro 	if (IS_ERR(dentry)) {
1965a1c2aa1eSAl Viro 		ret = PTR_ERR(dentry);
1966f0ee2e46SJames Carter 		goto err;
1967a1c2aa1eSAl Viro 	}
1968f0ee2e46SJames Carter 
1969f0ee2e46SJames Carter 	ret = sel_make_initcon_files(dentry);
1970f0ee2e46SJames Carter 	if (ret)
1971f0ee2e46SJames Carter 		goto err;
1972f0ee2e46SJames Carter 
19730619f0f5SStephen Smalley 	fsi->class_dir = sel_make_dir(sb->s_root, "class", &fsi->last_ino);
19740619f0f5SStephen Smalley 	if (IS_ERR(fsi->class_dir)) {
19750619f0f5SStephen Smalley 		ret = PTR_ERR(fsi->class_dir);
19760619f0f5SStephen Smalley 		fsi->class_dir = NULL;
1977e47c8fc5SChristopher J. PeBenito 		goto err;
1978a1c2aa1eSAl Viro 	}
1979e47c8fc5SChristopher J. PeBenito 
19800619f0f5SStephen Smalley 	fsi->policycap_dir = sel_make_dir(sb->s_root, "policy_capabilities",
19810619f0f5SStephen Smalley 					  &fsi->last_ino);
19820619f0f5SStephen Smalley 	if (IS_ERR(fsi->policycap_dir)) {
19830619f0f5SStephen Smalley 		ret = PTR_ERR(fsi->policycap_dir);
19840619f0f5SStephen Smalley 		fsi->policycap_dir = NULL;
1985e47c8fc5SChristopher J. PeBenito 		goto err;
1986a1c2aa1eSAl Viro 	}
19870619f0f5SStephen Smalley 
19880619f0f5SStephen Smalley 	ret = sel_make_policy_nodes(fsi);
19890619f0f5SStephen Smalley 	if (ret)
19900619f0f5SStephen Smalley 		goto err;
1991b77a493bSEric Paris 	return 0;
1992161ce45aSJames Morris err:
1993744ba35eSEric Paris 	printk(KERN_ERR "SELinux: %s:  failed while creating inodes\n",
1994744ba35eSEric Paris 		__func__);
19950619f0f5SStephen Smalley 
19960619f0f5SStephen Smalley 	selinux_fs_info_free(sb);
19970619f0f5SStephen Smalley 
1998b77a493bSEric Paris 	return ret;
19991da177e4SLinus Torvalds }
20001da177e4SLinus Torvalds 
2001fc14f2feSAl Viro static struct dentry *sel_mount(struct file_system_type *fs_type,
2002fc14f2feSAl Viro 		      int flags, const char *dev_name, void *data)
20031da177e4SLinus Torvalds {
2004fc14f2feSAl Viro 	return mount_single(fs_type, flags, data, sel_fill_super);
20051da177e4SLinus Torvalds }
20061da177e4SLinus Torvalds 
20070619f0f5SStephen Smalley static void sel_kill_sb(struct super_block *sb)
20080619f0f5SStephen Smalley {
20090619f0f5SStephen Smalley 	selinux_fs_info_free(sb);
20100619f0f5SStephen Smalley 	kill_litter_super(sb);
20110619f0f5SStephen Smalley }
20120619f0f5SStephen Smalley 
20131da177e4SLinus Torvalds static struct file_system_type sel_fs_type = {
20141da177e4SLinus Torvalds 	.name		= "selinuxfs",
2015fc14f2feSAl Viro 	.mount		= sel_mount,
20160619f0f5SStephen Smalley 	.kill_sb	= sel_kill_sb,
20171da177e4SLinus Torvalds };
20181da177e4SLinus Torvalds 
20191da177e4SLinus Torvalds struct vfsmount *selinuxfs_mount;
20200619f0f5SStephen Smalley struct path selinux_null;
20211da177e4SLinus Torvalds 
20221da177e4SLinus Torvalds static int __init init_sel_fs(void)
20231da177e4SLinus Torvalds {
20240619f0f5SStephen Smalley 	struct qstr null_name = QSTR_INIT(NULL_FILE_NAME,
20250619f0f5SStephen Smalley 					  sizeof(NULL_FILE_NAME)-1);
20261da177e4SLinus Torvalds 	int err;
20271da177e4SLinus Torvalds 
20281da177e4SLinus Torvalds 	if (!selinux_enabled)
20291da177e4SLinus Torvalds 		return 0;
20307a627e3bSGreg Kroah-Hartman 
2031f9bb4882SEric W. Biederman 	err = sysfs_create_mount_point(fs_kobj, "selinux");
2032f9bb4882SEric W. Biederman 	if (err)
2033f9bb4882SEric W. Biederman 		return err;
20347a627e3bSGreg Kroah-Hartman 
20351da177e4SLinus Torvalds 	err = register_filesystem(&sel_fs_type);
20367a627e3bSGreg Kroah-Hartman 	if (err) {
2037f9bb4882SEric W. Biederman 		sysfs_remove_mount_point(fs_kobj, "selinux");
2038b77a493bSEric Paris 		return err;
20397a627e3bSGreg Kroah-Hartman 	}
2040b77a493bSEric Paris 
2041765927b2SAl Viro 	selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type);
20421da177e4SLinus Torvalds 	if (IS_ERR(selinuxfs_mount)) {
20431da177e4SLinus Torvalds 		printk(KERN_ERR "selinuxfs:  could not mount!\n");
20441da177e4SLinus Torvalds 		err = PTR_ERR(selinuxfs_mount);
20451da177e4SLinus Torvalds 		selinuxfs_mount = NULL;
20461da177e4SLinus Torvalds 	}
20470619f0f5SStephen Smalley 	selinux_null.dentry = d_hash_and_lookup(selinux_null.mnt->mnt_root,
20480619f0f5SStephen Smalley 						&null_name);
20490619f0f5SStephen Smalley 	if (IS_ERR(selinux_null.dentry)) {
20500619f0f5SStephen Smalley 		pr_err("selinuxfs:  could not lookup null!\n");
20510619f0f5SStephen Smalley 		err = PTR_ERR(selinux_null.dentry);
20520619f0f5SStephen Smalley 		selinux_null.dentry = NULL;
20530619f0f5SStephen Smalley 	}
2054b77a493bSEric Paris 
20551da177e4SLinus Torvalds 	return err;
20561da177e4SLinus Torvalds }
20571da177e4SLinus Torvalds 
20581da177e4SLinus Torvalds __initcall(init_sel_fs);
20591da177e4SLinus Torvalds 
20601da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
20611da177e4SLinus Torvalds void exit_sel_fs(void)
20621da177e4SLinus Torvalds {
2063f9bb4882SEric W. Biederman 	sysfs_remove_mount_point(fs_kobj, "selinux");
2064fd40ffc7SStephen Smalley 	dput(selinux_null.dentry);
2065423e0ab0STim Chen 	kern_unmount(selinuxfs_mount);
20661da177e4SLinus Torvalds 	unregister_filesystem(&sel_fs_type);
20671da177e4SLinus Torvalds }
20681da177e4SLinus Torvalds #endif
2069