xref: /openbmc/linux/security/selinux/selinuxfs.c (revision 7e4237fa)
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;
170cdfb6b34SRichard Guy Briggs 		audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS,
1714195ed42SRichard Guy Briggs 			"enforcing=%d old_enforcing=%d auid=%u ses=%u"
1724195ed42SRichard Guy Briggs 			" enabled=%d old-enabled=%d lsm=selinux res=1",
173aa8e712cSStephen Smalley 			new_value, old_value,
174581abc09SEric W. Biederman 			from_kuid(&init_user_ns, audit_get_loginuid(current)),
1754195ed42SRichard Guy Briggs 			audit_get_sessionid(current),
1764195ed42SRichard Guy Briggs 			selinux_enabled, selinux_enabled);
1770619f0f5SStephen Smalley 		enforcing_set(state, new_value);
178aa8e712cSStephen Smalley 		if (new_value)
1796b6bc620SStephen Smalley 			avc_ss_reset(state->avc, 0);
180aa8e712cSStephen Smalley 		selnl_notify_setenforce(new_value);
1810619f0f5SStephen Smalley 		selinux_status_update_setenforce(state, new_value);
182aa8e712cSStephen Smalley 		if (!new_value)
1838f408ab6SDaniel Jurgens 			call_lsm_notifier(LSM_POLICY_CHANGE, NULL);
1841da177e4SLinus Torvalds 	}
1851da177e4SLinus Torvalds 	length = count;
1861da177e4SLinus Torvalds out:
1878365a719SAl Viro 	kfree(page);
1881da177e4SLinus Torvalds 	return length;
1891da177e4SLinus Torvalds }
1901da177e4SLinus Torvalds #else
1911da177e4SLinus Torvalds #define sel_write_enforce NULL
1921da177e4SLinus Torvalds #endif
1931da177e4SLinus Torvalds 
1949c2e08c5SArjan van de Ven static const struct file_operations sel_enforce_ops = {
1951da177e4SLinus Torvalds 	.read		= sel_read_enforce,
1961da177e4SLinus Torvalds 	.write		= sel_write_enforce,
19757a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
1981da177e4SLinus Torvalds };
1991da177e4SLinus Torvalds 
2003f12070eSEric Paris static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf,
2013f12070eSEric Paris 					size_t count, loff_t *ppos)
2023f12070eSEric Paris {
2030619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
2040619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
2053f12070eSEric Paris 	char tmpbuf[TMPBUFLEN];
2063f12070eSEric Paris 	ssize_t length;
207496ad9aaSAl Viro 	ino_t ino = file_inode(filp)->i_ino;
2083f12070eSEric Paris 	int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
2090619f0f5SStephen Smalley 		security_get_reject_unknown(state) :
2100619f0f5SStephen Smalley 		!security_get_allow_unknown(state);
2113f12070eSEric Paris 
2123f12070eSEric Paris 	length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
2133f12070eSEric Paris 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
2143f12070eSEric Paris }
2153f12070eSEric Paris 
2163f12070eSEric Paris static const struct file_operations sel_handle_unknown_ops = {
2173f12070eSEric Paris 	.read		= sel_read_handle_unknown,
21857a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
2193f12070eSEric Paris };
2203f12070eSEric Paris 
22111904167SKaiGai Kohei static int sel_open_handle_status(struct inode *inode, struct file *filp)
22211904167SKaiGai Kohei {
2230619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
2240619f0f5SStephen Smalley 	struct page    *status = selinux_kernel_status_page(fsi->state);
22511904167SKaiGai Kohei 
22611904167SKaiGai Kohei 	if (!status)
22711904167SKaiGai Kohei 		return -ENOMEM;
22811904167SKaiGai Kohei 
22911904167SKaiGai Kohei 	filp->private_data = status;
23011904167SKaiGai Kohei 
23111904167SKaiGai Kohei 	return 0;
23211904167SKaiGai Kohei }
23311904167SKaiGai Kohei 
23411904167SKaiGai Kohei static ssize_t sel_read_handle_status(struct file *filp, char __user *buf,
23511904167SKaiGai Kohei 				      size_t count, loff_t *ppos)
23611904167SKaiGai Kohei {
23711904167SKaiGai Kohei 	struct page    *status = filp->private_data;
23811904167SKaiGai Kohei 
23911904167SKaiGai Kohei 	BUG_ON(!status);
24011904167SKaiGai Kohei 
24111904167SKaiGai Kohei 	return simple_read_from_buffer(buf, count, ppos,
24211904167SKaiGai Kohei 				       page_address(status),
24311904167SKaiGai Kohei 				       sizeof(struct selinux_kernel_status));
24411904167SKaiGai Kohei }
24511904167SKaiGai Kohei 
24611904167SKaiGai Kohei static int sel_mmap_handle_status(struct file *filp,
24711904167SKaiGai Kohei 				  struct vm_area_struct *vma)
24811904167SKaiGai Kohei {
24911904167SKaiGai Kohei 	struct page    *status = filp->private_data;
25011904167SKaiGai Kohei 	unsigned long	size = vma->vm_end - vma->vm_start;
25111904167SKaiGai Kohei 
25211904167SKaiGai Kohei 	BUG_ON(!status);
25311904167SKaiGai Kohei 
25411904167SKaiGai Kohei 	/* only allows one page from the head */
25511904167SKaiGai Kohei 	if (vma->vm_pgoff > 0 || size != PAGE_SIZE)
25611904167SKaiGai Kohei 		return -EIO;
25711904167SKaiGai Kohei 	/* disallow writable mapping */
25811904167SKaiGai Kohei 	if (vma->vm_flags & VM_WRITE)
25911904167SKaiGai Kohei 		return -EPERM;
26011904167SKaiGai Kohei 	/* disallow mprotect() turns it into writable */
26111904167SKaiGai Kohei 	vma->vm_flags &= ~VM_MAYWRITE;
26211904167SKaiGai Kohei 
26311904167SKaiGai Kohei 	return remap_pfn_range(vma, vma->vm_start,
26411904167SKaiGai Kohei 			       page_to_pfn(status),
26511904167SKaiGai Kohei 			       size, vma->vm_page_prot);
26611904167SKaiGai Kohei }
26711904167SKaiGai Kohei 
26811904167SKaiGai Kohei static const struct file_operations sel_handle_status_ops = {
26911904167SKaiGai Kohei 	.open		= sel_open_handle_status,
27011904167SKaiGai Kohei 	.read		= sel_read_handle_status,
27111904167SKaiGai Kohei 	.mmap		= sel_mmap_handle_status,
27211904167SKaiGai Kohei 	.llseek		= generic_file_llseek,
27311904167SKaiGai Kohei };
27411904167SKaiGai Kohei 
2751da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
2761da177e4SLinus Torvalds static ssize_t sel_write_disable(struct file *file, const char __user *buf,
2771da177e4SLinus Torvalds 				 size_t count, loff_t *ppos)
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds {
2800619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
2818365a719SAl Viro 	char *page;
2821da177e4SLinus Torvalds 	ssize_t length;
2831da177e4SLinus Torvalds 	int new_value;
2844195ed42SRichard Guy Briggs 	int enforcing;
2851da177e4SLinus Torvalds 
286bfd51626SDavi Arnaut 	if (count >= PAGE_SIZE)
2878365a719SAl Viro 		return -ENOMEM;
288b77a493bSEric Paris 
2891da177e4SLinus Torvalds 	/* No partial writes. */
290b77a493bSEric Paris 	if (*ppos != 0)
2918365a719SAl Viro 		return -EINVAL;
292b77a493bSEric Paris 
2938365a719SAl Viro 	page = memdup_user_nul(buf, count);
2948365a719SAl Viro 	if (IS_ERR(page))
2958365a719SAl Viro 		return PTR_ERR(page);
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds 	length = -EINVAL;
2981da177e4SLinus Torvalds 	if (sscanf(page, "%d", &new_value) != 1)
2991da177e4SLinus Torvalds 		goto out;
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 	if (new_value) {
3024195ed42SRichard Guy Briggs 		enforcing = enforcing_enabled(fsi->state);
3030619f0f5SStephen Smalley 		length = selinux_disable(fsi->state);
304b77a493bSEric Paris 		if (length)
3051da177e4SLinus Torvalds 			goto out;
306cdfb6b34SRichard Guy Briggs 		audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS,
3074195ed42SRichard Guy Briggs 			"enforcing=%d old_enforcing=%d auid=%u ses=%u"
3084195ed42SRichard Guy Briggs 			" enabled=%d old-enabled=%d lsm=selinux res=1",
3094195ed42SRichard Guy Briggs 			enforcing, enforcing,
310581abc09SEric W. Biederman 			from_kuid(&init_user_ns, audit_get_loginuid(current)),
3114195ed42SRichard Guy Briggs 			audit_get_sessionid(current), 0, 1);
3121da177e4SLinus Torvalds 	}
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds 	length = count;
3151da177e4SLinus Torvalds out:
3168365a719SAl Viro 	kfree(page);
3171da177e4SLinus Torvalds 	return length;
3181da177e4SLinus Torvalds }
3191da177e4SLinus Torvalds #else
3201da177e4SLinus Torvalds #define sel_write_disable NULL
3211da177e4SLinus Torvalds #endif
3221da177e4SLinus Torvalds 
3239c2e08c5SArjan van de Ven static const struct file_operations sel_disable_ops = {
3241da177e4SLinus Torvalds 	.write		= sel_write_disable,
32557a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
3261da177e4SLinus Torvalds };
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
3291da177e4SLinus Torvalds 				   size_t count, loff_t *ppos)
3301da177e4SLinus Torvalds {
3311da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
3321da177e4SLinus Torvalds 	ssize_t length;
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
3351da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
3361da177e4SLinus Torvalds }
3371da177e4SLinus Torvalds 
3389c2e08c5SArjan van de Ven static const struct file_operations sel_policyvers_ops = {
3391da177e4SLinus Torvalds 	.read		= sel_read_policyvers,
34057a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
3411da177e4SLinus Torvalds };
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds /* declaration for sel_write_load */
3440619f0f5SStephen Smalley static int sel_make_bools(struct selinux_fs_info *fsi);
3450619f0f5SStephen Smalley static int sel_make_classes(struct selinux_fs_info *fsi);
3460619f0f5SStephen Smalley static int sel_make_policycap(struct selinux_fs_info *fsi);
347e47c8fc5SChristopher J. PeBenito 
348e47c8fc5SChristopher J. PeBenito /* declaration for sel_make_class_dirs */
349a1c2aa1eSAl Viro static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
350e47c8fc5SChristopher J. PeBenito 			unsigned long *ino);
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds static ssize_t sel_read_mls(struct file *filp, char __user *buf,
3531da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
3541da177e4SLinus Torvalds {
3550619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
3561da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
3571da177e4SLinus Torvalds 	ssize_t length;
3581da177e4SLinus Torvalds 
3590719aaf5SGuido Trentalancia 	length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
3600619f0f5SStephen Smalley 			   security_mls_enabled(fsi->state));
3611da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
3621da177e4SLinus Torvalds }
3631da177e4SLinus Torvalds 
3649c2e08c5SArjan van de Ven static const struct file_operations sel_mls_ops = {
3651da177e4SLinus Torvalds 	.read		= sel_read_mls,
36657a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
3671da177e4SLinus Torvalds };
3681da177e4SLinus Torvalds 
369cee74f47SEric Paris struct policy_load_memory {
370cee74f47SEric Paris 	size_t len;
371cee74f47SEric Paris 	void *data;
372cee74f47SEric Paris };
373cee74f47SEric Paris 
374cee74f47SEric Paris static int sel_open_policy(struct inode *inode, struct file *filp)
375cee74f47SEric Paris {
3760619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
3770619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
378cee74f47SEric Paris 	struct policy_load_memory *plm = NULL;
379cee74f47SEric Paris 	int rc;
380cee74f47SEric Paris 
381cee74f47SEric Paris 	BUG_ON(filp->private_data);
382cee74f47SEric Paris 
3830619f0f5SStephen Smalley 	mutex_lock(&fsi->mutex);
384cee74f47SEric Paris 
3856b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
3866b6bc620SStephen Smalley 			  current_sid(), SECINITSID_SECURITY,
387be0554c9SStephen Smalley 			  SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
388cee74f47SEric Paris 	if (rc)
389cee74f47SEric Paris 		goto err;
390cee74f47SEric Paris 
391cee74f47SEric Paris 	rc = -EBUSY;
3920619f0f5SStephen Smalley 	if (fsi->policy_opened)
393cee74f47SEric Paris 		goto err;
394cee74f47SEric Paris 
395cee74f47SEric Paris 	rc = -ENOMEM;
396cee74f47SEric Paris 	plm = kzalloc(sizeof(*plm), GFP_KERNEL);
397cee74f47SEric Paris 	if (!plm)
398cee74f47SEric Paris 		goto err;
399cee74f47SEric Paris 
4000619f0f5SStephen Smalley 	if (i_size_read(inode) != security_policydb_len(state)) {
4015955102cSAl Viro 		inode_lock(inode);
4020619f0f5SStephen Smalley 		i_size_write(inode, security_policydb_len(state));
4035955102cSAl Viro 		inode_unlock(inode);
404cee74f47SEric Paris 	}
405cee74f47SEric Paris 
4060619f0f5SStephen Smalley 	rc = security_read_policy(state, &plm->data, &plm->len);
407cee74f47SEric Paris 	if (rc)
408cee74f47SEric Paris 		goto err;
409cee74f47SEric Paris 
4100619f0f5SStephen Smalley 	fsi->policy_opened = 1;
411cee74f47SEric Paris 
412cee74f47SEric Paris 	filp->private_data = plm;
413cee74f47SEric Paris 
4140619f0f5SStephen Smalley 	mutex_unlock(&fsi->mutex);
415cee74f47SEric Paris 
416cee74f47SEric Paris 	return 0;
417cee74f47SEric Paris err:
4180619f0f5SStephen Smalley 	mutex_unlock(&fsi->mutex);
419cee74f47SEric Paris 
420cee74f47SEric Paris 	if (plm)
421cee74f47SEric Paris 		vfree(plm->data);
422cee74f47SEric Paris 	kfree(plm);
423cee74f47SEric Paris 	return rc;
424cee74f47SEric Paris }
425cee74f47SEric Paris 
426cee74f47SEric Paris static int sel_release_policy(struct inode *inode, struct file *filp)
427cee74f47SEric Paris {
4280619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
429cee74f47SEric Paris 	struct policy_load_memory *plm = filp->private_data;
430cee74f47SEric Paris 
431cee74f47SEric Paris 	BUG_ON(!plm);
432cee74f47SEric Paris 
4330619f0f5SStephen Smalley 	fsi->policy_opened = 0;
434cee74f47SEric Paris 
435cee74f47SEric Paris 	vfree(plm->data);
436cee74f47SEric Paris 	kfree(plm);
437cee74f47SEric Paris 
438cee74f47SEric Paris 	return 0;
439cee74f47SEric Paris }
440cee74f47SEric Paris 
441cee74f47SEric Paris static ssize_t sel_read_policy(struct file *filp, char __user *buf,
442cee74f47SEric Paris 			       size_t count, loff_t *ppos)
443cee74f47SEric Paris {
4440619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
445cee74f47SEric Paris 	struct policy_load_memory *plm = filp->private_data;
446cee74f47SEric Paris 	int ret;
447cee74f47SEric Paris 
4480619f0f5SStephen Smalley 	mutex_lock(&fsi->mutex);
449cee74f47SEric Paris 
4506b6bc620SStephen Smalley 	ret = avc_has_perm(&selinux_state,
4516b6bc620SStephen Smalley 			   current_sid(), SECINITSID_SECURITY,
452be0554c9SStephen Smalley 			  SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
453cee74f47SEric Paris 	if (ret)
454cee74f47SEric Paris 		goto out;
455cee74f47SEric Paris 
456cee74f47SEric Paris 	ret = simple_read_from_buffer(buf, count, ppos, plm->data, plm->len);
457cee74f47SEric Paris out:
4580619f0f5SStephen Smalley 	mutex_unlock(&fsi->mutex);
459cee74f47SEric Paris 	return ret;
460cee74f47SEric Paris }
461cee74f47SEric Paris 
462ac9a1f6dSSouptick Joarder static vm_fault_t sel_mmap_policy_fault(struct vm_fault *vmf)
463845ca30fSEric Paris {
46411bac800SDave Jiang 	struct policy_load_memory *plm = vmf->vma->vm_file->private_data;
465845ca30fSEric Paris 	unsigned long offset;
466845ca30fSEric Paris 	struct page *page;
467845ca30fSEric Paris 
468845ca30fSEric Paris 	if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE))
469845ca30fSEric Paris 		return VM_FAULT_SIGBUS;
470845ca30fSEric Paris 
471845ca30fSEric Paris 	offset = vmf->pgoff << PAGE_SHIFT;
472845ca30fSEric Paris 	if (offset >= roundup(plm->len, PAGE_SIZE))
473845ca30fSEric Paris 		return VM_FAULT_SIGBUS;
474845ca30fSEric Paris 
475845ca30fSEric Paris 	page = vmalloc_to_page(plm->data + offset);
476845ca30fSEric Paris 	get_page(page);
477845ca30fSEric Paris 
478845ca30fSEric Paris 	vmf->page = page;
479845ca30fSEric Paris 
480845ca30fSEric Paris 	return 0;
481845ca30fSEric Paris }
482845ca30fSEric Paris 
4837cbea8dcSKirill A. Shutemov static const struct vm_operations_struct sel_mmap_policy_ops = {
484845ca30fSEric Paris 	.fault = sel_mmap_policy_fault,
485845ca30fSEric Paris 	.page_mkwrite = sel_mmap_policy_fault,
486845ca30fSEric Paris };
487845ca30fSEric Paris 
488ad3fa08cSJames Morris static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma)
489845ca30fSEric Paris {
490845ca30fSEric Paris 	if (vma->vm_flags & VM_SHARED) {
491845ca30fSEric Paris 		/* do not allow mprotect to make mapping writable */
492845ca30fSEric Paris 		vma->vm_flags &= ~VM_MAYWRITE;
493845ca30fSEric Paris 
494845ca30fSEric Paris 		if (vma->vm_flags & VM_WRITE)
495845ca30fSEric Paris 			return -EACCES;
496845ca30fSEric Paris 	}
497845ca30fSEric Paris 
498314e51b9SKonstantin Khlebnikov 	vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
499845ca30fSEric Paris 	vma->vm_ops = &sel_mmap_policy_ops;
500845ca30fSEric Paris 
501845ca30fSEric Paris 	return 0;
502845ca30fSEric Paris }
503845ca30fSEric Paris 
504cee74f47SEric Paris static const struct file_operations sel_policy_ops = {
505cee74f47SEric Paris 	.open		= sel_open_policy,
506cee74f47SEric Paris 	.read		= sel_read_policy,
507845ca30fSEric Paris 	.mmap		= sel_mmap_policy,
508cee74f47SEric Paris 	.release	= sel_release_policy,
50947a93a5bSEric Paris 	.llseek		= generic_file_llseek,
510cee74f47SEric Paris };
511cee74f47SEric Paris 
5120619f0f5SStephen Smalley static int sel_make_policy_nodes(struct selinux_fs_info *fsi)
5130619f0f5SStephen Smalley {
5140619f0f5SStephen Smalley 	int ret;
5150619f0f5SStephen Smalley 
5160619f0f5SStephen Smalley 	ret = sel_make_bools(fsi);
5170619f0f5SStephen Smalley 	if (ret) {
5180619f0f5SStephen Smalley 		pr_err("SELinux: failed to load policy booleans\n");
5190619f0f5SStephen Smalley 		return ret;
5200619f0f5SStephen Smalley 	}
5210619f0f5SStephen Smalley 
5220619f0f5SStephen Smalley 	ret = sel_make_classes(fsi);
5230619f0f5SStephen Smalley 	if (ret) {
5240619f0f5SStephen Smalley 		pr_err("SELinux: failed to load policy classes\n");
5250619f0f5SStephen Smalley 		return ret;
5260619f0f5SStephen Smalley 	}
5270619f0f5SStephen Smalley 
5280619f0f5SStephen Smalley 	ret = sel_make_policycap(fsi);
5290619f0f5SStephen Smalley 	if (ret) {
5300619f0f5SStephen Smalley 		pr_err("SELinux: failed to load policy capabilities\n");
5310619f0f5SStephen Smalley 		return ret;
5320619f0f5SStephen Smalley 	}
5330619f0f5SStephen Smalley 
5340619f0f5SStephen Smalley 	return 0;
5350619f0f5SStephen Smalley }
5360619f0f5SStephen Smalley 
5371da177e4SLinus Torvalds static ssize_t sel_write_load(struct file *file, const char __user *buf,
5381da177e4SLinus Torvalds 			      size_t count, loff_t *ppos)
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds {
5410619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
5421da177e4SLinus Torvalds 	ssize_t length;
5431da177e4SLinus Torvalds 	void *data = NULL;
5441da177e4SLinus Torvalds 
5450619f0f5SStephen Smalley 	mutex_lock(&fsi->mutex);
5461da177e4SLinus Torvalds 
5476b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
5486b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
549be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL);
5501da177e4SLinus Torvalds 	if (length)
5511da177e4SLinus Torvalds 		goto out;
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds 	/* No partial writes. */
5541da177e4SLinus Torvalds 	length = -EINVAL;
555b77a493bSEric Paris 	if (*ppos != 0)
5561da177e4SLinus Torvalds 		goto out;
5571da177e4SLinus Torvalds 
558b77a493bSEric Paris 	length = -EFBIG;
559b77a493bSEric Paris 	if (count > 64 * 1024 * 1024)
5601da177e4SLinus Torvalds 		goto out;
561b77a493bSEric Paris 
562b77a493bSEric Paris 	length = -ENOMEM;
563b77a493bSEric Paris 	data = vmalloc(count);
564b77a493bSEric Paris 	if (!data)
565b77a493bSEric Paris 		goto out;
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds 	length = -EFAULT;
5681da177e4SLinus Torvalds 	if (copy_from_user(data, buf, count) != 0)
5691da177e4SLinus Torvalds 		goto out;
5701da177e4SLinus Torvalds 
5710619f0f5SStephen Smalley 	length = security_load_policy(fsi->state, data, count);
5724262fb51SGary Tierney 	if (length) {
5734262fb51SGary Tierney 		pr_warn_ratelimited("SELinux: failed to load policy\n");
5741da177e4SLinus Torvalds 		goto out;
5754262fb51SGary Tierney 	}
5761da177e4SLinus Torvalds 
5770619f0f5SStephen Smalley 	length = sel_make_policy_nodes(fsi);
5780619f0f5SStephen Smalley 	if (length)
579e47c8fc5SChristopher J. PeBenito 		goto out1;
580b77a493bSEric Paris 
5811da177e4SLinus Torvalds 	length = count;
582e47c8fc5SChristopher J. PeBenito 
583e47c8fc5SChristopher J. PeBenito out1:
584cdfb6b34SRichard Guy Briggs 	audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
585d141136fSRichard Guy Briggs 		"auid=%u ses=%u lsm=selinux res=1",
586581abc09SEric W. Biederman 		from_kuid(&init_user_ns, audit_get_loginuid(current)),
5874746ec5bSEric Paris 		audit_get_sessionid(current));
5881da177e4SLinus Torvalds out:
5890619f0f5SStephen Smalley 	mutex_unlock(&fsi->mutex);
5901da177e4SLinus Torvalds 	vfree(data);
5911da177e4SLinus Torvalds 	return length;
5921da177e4SLinus Torvalds }
5931da177e4SLinus Torvalds 
5949c2e08c5SArjan van de Ven static const struct file_operations sel_load_ops = {
5951da177e4SLinus Torvalds 	.write		= sel_write_load,
59657a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
5971da177e4SLinus Torvalds };
5981da177e4SLinus Torvalds 
599ce9982d0SStephen Smalley static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
6001da177e4SLinus Torvalds {
6010619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
6020619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
603b77a493bSEric Paris 	char *canon = NULL;
604ce9982d0SStephen Smalley 	u32 sid, len;
6051da177e4SLinus Torvalds 	ssize_t length;
6061da177e4SLinus Torvalds 
6076b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
6086b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
609be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__CHECK_CONTEXT, NULL);
6101da177e4SLinus Torvalds 	if (length)
611b77a493bSEric Paris 		goto out;
6121da177e4SLinus Torvalds 
6130619f0f5SStephen Smalley 	length = security_context_to_sid(state, buf, size, &sid, GFP_KERNEL);
614b77a493bSEric Paris 	if (length)
615b77a493bSEric Paris 		goto out;
6161da177e4SLinus Torvalds 
6170619f0f5SStephen Smalley 	length = security_sid_to_context(state, sid, &canon, &len);
618b77a493bSEric Paris 	if (length)
619b77a493bSEric Paris 		goto out;
620ce9982d0SStephen Smalley 
621b77a493bSEric Paris 	length = -ERANGE;
622ce9982d0SStephen Smalley 	if (len > SIMPLE_TRANSACTION_LIMIT) {
623f8b69a5fSpeter enderborg 		pr_err("SELinux: %s:  context size (%u) exceeds "
624744ba35eSEric Paris 			"payload max\n", __func__, len);
625ce9982d0SStephen Smalley 		goto out;
626ce9982d0SStephen Smalley 	}
627ce9982d0SStephen Smalley 
628ce9982d0SStephen Smalley 	memcpy(buf, canon, len);
629ce9982d0SStephen Smalley 	length = len;
6301da177e4SLinus Torvalds out:
631ce9982d0SStephen Smalley 	kfree(canon);
6321da177e4SLinus Torvalds 	return length;
6331da177e4SLinus Torvalds }
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
6361da177e4SLinus Torvalds 				     size_t count, loff_t *ppos)
6371da177e4SLinus Torvalds {
6380619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
6391da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
6401da177e4SLinus Torvalds 	ssize_t length;
6411da177e4SLinus Torvalds 
6420619f0f5SStephen Smalley 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u", fsi->state->checkreqprot);
6431da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
6441da177e4SLinus Torvalds }
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
6471da177e4SLinus Torvalds 				      size_t count, loff_t *ppos)
6481da177e4SLinus Torvalds {
6490619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
6508365a719SAl Viro 	char *page;
6511da177e4SLinus Torvalds 	ssize_t length;
6521da177e4SLinus Torvalds 	unsigned int new_value;
6531da177e4SLinus Torvalds 
6546b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
6556b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
656be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__SETCHECKREQPROT,
657be0554c9SStephen Smalley 			      NULL);
6581da177e4SLinus Torvalds 	if (length)
6598365a719SAl Viro 		return length;
6601da177e4SLinus Torvalds 
661bfd51626SDavi Arnaut 	if (count >= PAGE_SIZE)
6628365a719SAl Viro 		return -ENOMEM;
663b77a493bSEric Paris 
6641da177e4SLinus Torvalds 	/* No partial writes. */
665b77a493bSEric Paris 	if (*ppos != 0)
6668365a719SAl Viro 		return -EINVAL;
667b77a493bSEric Paris 
6688365a719SAl Viro 	page = memdup_user_nul(buf, count);
6698365a719SAl Viro 	if (IS_ERR(page))
6708365a719SAl Viro 		return PTR_ERR(page);
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds 	length = -EINVAL;
6731da177e4SLinus Torvalds 	if (sscanf(page, "%u", &new_value) != 1)
6741da177e4SLinus Torvalds 		goto out;
6751da177e4SLinus Torvalds 
6760619f0f5SStephen Smalley 	fsi->state->checkreqprot = new_value ? 1 : 0;
6771da177e4SLinus Torvalds 	length = count;
6781da177e4SLinus Torvalds out:
6798365a719SAl Viro 	kfree(page);
6801da177e4SLinus Torvalds 	return length;
6811da177e4SLinus Torvalds }
6829c2e08c5SArjan van de Ven static const struct file_operations sel_checkreqprot_ops = {
6831da177e4SLinus Torvalds 	.read		= sel_read_checkreqprot,
6841da177e4SLinus Torvalds 	.write		= sel_write_checkreqprot,
68557a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
6861da177e4SLinus Torvalds };
6871da177e4SLinus Torvalds 
688f9df6458SAndrew Perepechko static ssize_t sel_write_validatetrans(struct file *file,
689f9df6458SAndrew Perepechko 					const char __user *buf,
690f9df6458SAndrew Perepechko 					size_t count, loff_t *ppos)
691f9df6458SAndrew Perepechko {
6920619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
6930619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
694f9df6458SAndrew Perepechko 	char *oldcon = NULL, *newcon = NULL, *taskcon = NULL;
695f9df6458SAndrew Perepechko 	char *req = NULL;
696f9df6458SAndrew Perepechko 	u32 osid, nsid, tsid;
697f9df6458SAndrew Perepechko 	u16 tclass;
698f9df6458SAndrew Perepechko 	int rc;
699f9df6458SAndrew Perepechko 
7006b6bc620SStephen Smalley 	rc = avc_has_perm(&selinux_state,
7016b6bc620SStephen Smalley 			  current_sid(), SECINITSID_SECURITY,
702be0554c9SStephen Smalley 			  SECCLASS_SECURITY, SECURITY__VALIDATE_TRANS, NULL);
703f9df6458SAndrew Perepechko 	if (rc)
704f9df6458SAndrew Perepechko 		goto out;
705f9df6458SAndrew Perepechko 
706f9df6458SAndrew Perepechko 	rc = -ENOMEM;
707f9df6458SAndrew Perepechko 	if (count >= PAGE_SIZE)
708f9df6458SAndrew Perepechko 		goto out;
709f9df6458SAndrew Perepechko 
710f9df6458SAndrew Perepechko 	/* No partial writes. */
711f9df6458SAndrew Perepechko 	rc = -EINVAL;
712f9df6458SAndrew Perepechko 	if (*ppos != 0)
713f9df6458SAndrew Perepechko 		goto out;
714f9df6458SAndrew Perepechko 
7150b884d25SAl Viro 	req = memdup_user_nul(buf, count);
7160b884d25SAl Viro 	if (IS_ERR(req)) {
7170b884d25SAl Viro 		rc = PTR_ERR(req);
7180b884d25SAl Viro 		req = NULL;
719f9df6458SAndrew Perepechko 		goto out;
7200b884d25SAl Viro 	}
721f9df6458SAndrew Perepechko 
722f9df6458SAndrew Perepechko 	rc = -ENOMEM;
723f9df6458SAndrew Perepechko 	oldcon = kzalloc(count + 1, GFP_KERNEL);
724f9df6458SAndrew Perepechko 	if (!oldcon)
725f9df6458SAndrew Perepechko 		goto out;
726f9df6458SAndrew Perepechko 
727f9df6458SAndrew Perepechko 	newcon = kzalloc(count + 1, GFP_KERNEL);
728f9df6458SAndrew Perepechko 	if (!newcon)
729f9df6458SAndrew Perepechko 		goto out;
730f9df6458SAndrew Perepechko 
731f9df6458SAndrew Perepechko 	taskcon = kzalloc(count + 1, GFP_KERNEL);
732f9df6458SAndrew Perepechko 	if (!taskcon)
733f9df6458SAndrew Perepechko 		goto out;
734f9df6458SAndrew Perepechko 
735f9df6458SAndrew Perepechko 	rc = -EINVAL;
736f9df6458SAndrew Perepechko 	if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4)
737f9df6458SAndrew Perepechko 		goto out;
738f9df6458SAndrew Perepechko 
7390619f0f5SStephen Smalley 	rc = security_context_str_to_sid(state, oldcon, &osid, GFP_KERNEL);
740f9df6458SAndrew Perepechko 	if (rc)
741f9df6458SAndrew Perepechko 		goto out;
742f9df6458SAndrew Perepechko 
7430619f0f5SStephen Smalley 	rc = security_context_str_to_sid(state, newcon, &nsid, GFP_KERNEL);
744f9df6458SAndrew Perepechko 	if (rc)
745f9df6458SAndrew Perepechko 		goto out;
746f9df6458SAndrew Perepechko 
7470619f0f5SStephen Smalley 	rc = security_context_str_to_sid(state, taskcon, &tsid, GFP_KERNEL);
748f9df6458SAndrew Perepechko 	if (rc)
749f9df6458SAndrew Perepechko 		goto out;
750f9df6458SAndrew Perepechko 
7510619f0f5SStephen Smalley 	rc = security_validate_transition_user(state, osid, nsid, tsid, tclass);
752f9df6458SAndrew Perepechko 	if (!rc)
753f9df6458SAndrew Perepechko 		rc = count;
754f9df6458SAndrew Perepechko out:
755f9df6458SAndrew Perepechko 	kfree(req);
756f9df6458SAndrew Perepechko 	kfree(oldcon);
757f9df6458SAndrew Perepechko 	kfree(newcon);
758f9df6458SAndrew Perepechko 	kfree(taskcon);
759f9df6458SAndrew Perepechko 	return rc;
760f9df6458SAndrew Perepechko }
761f9df6458SAndrew Perepechko 
762f9df6458SAndrew Perepechko static const struct file_operations sel_transition_ops = {
763f9df6458SAndrew Perepechko 	.write		= sel_write_validatetrans,
764f9df6458SAndrew Perepechko 	.llseek		= generic_file_llseek,
765f9df6458SAndrew Perepechko };
766f9df6458SAndrew Perepechko 
7671da177e4SLinus Torvalds /*
7681da177e4SLinus Torvalds  * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
7691da177e4SLinus Torvalds  */
7701da177e4SLinus Torvalds static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
7711da177e4SLinus Torvalds static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
7721da177e4SLinus Torvalds static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
7731da177e4SLinus Torvalds static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
7741da177e4SLinus Torvalds static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
7751da177e4SLinus Torvalds 
776631d2b49SEric Biggers static ssize_t (*const write_op[])(struct file *, char *, size_t) = {
7771da177e4SLinus Torvalds 	[SEL_ACCESS] = sel_write_access,
7781da177e4SLinus Torvalds 	[SEL_CREATE] = sel_write_create,
7791da177e4SLinus Torvalds 	[SEL_RELABEL] = sel_write_relabel,
7801da177e4SLinus Torvalds 	[SEL_USER] = sel_write_user,
7811da177e4SLinus Torvalds 	[SEL_MEMBER] = sel_write_member,
782ce9982d0SStephen Smalley 	[SEL_CONTEXT] = sel_write_context,
7831da177e4SLinus Torvalds };
7841da177e4SLinus Torvalds 
7851da177e4SLinus Torvalds static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
7861da177e4SLinus Torvalds {
787496ad9aaSAl Viro 	ino_t ino = file_inode(file)->i_ino;
7881da177e4SLinus Torvalds 	char *data;
7891da177e4SLinus Torvalds 	ssize_t rv;
7901da177e4SLinus Torvalds 
7916e20a64aSNicolas Kaiser 	if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
7921da177e4SLinus Torvalds 		return -EINVAL;
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds 	data = simple_transaction_get(file, buf, size);
7951da177e4SLinus Torvalds 	if (IS_ERR(data))
7961da177e4SLinus Torvalds 		return PTR_ERR(data);
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds 	rv = write_op[ino](file, data, size);
7991da177e4SLinus Torvalds 	if (rv > 0) {
8001da177e4SLinus Torvalds 		simple_transaction_set(file, rv);
8011da177e4SLinus Torvalds 		rv = size;
8021da177e4SLinus Torvalds 	}
8031da177e4SLinus Torvalds 	return rv;
8041da177e4SLinus Torvalds }
8051da177e4SLinus Torvalds 
8069c2e08c5SArjan van de Ven static const struct file_operations transaction_ops = {
8071da177e4SLinus Torvalds 	.write		= selinux_transaction_write,
8081da177e4SLinus Torvalds 	.read		= simple_transaction_read,
8091da177e4SLinus Torvalds 	.release	= simple_transaction_release,
81057a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
8111da177e4SLinus Torvalds };
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds /*
8141da177e4SLinus Torvalds  * payload - write methods
8151da177e4SLinus Torvalds  * If the method has a response, the response should be put in buf,
8161da177e4SLinus Torvalds  * and the length returned.  Otherwise return 0 or and -error.
8171da177e4SLinus Torvalds  */
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
8201da177e4SLinus Torvalds {
8210619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
8220619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
823b77a493bSEric Paris 	char *scon = NULL, *tcon = NULL;
8241da177e4SLinus Torvalds 	u32 ssid, tsid;
8251da177e4SLinus Torvalds 	u16 tclass;
8261da177e4SLinus Torvalds 	struct av_decision avd;
8271da177e4SLinus Torvalds 	ssize_t length;
8281da177e4SLinus Torvalds 
8296b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
8306b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
831be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__COMPUTE_AV, NULL);
8321da177e4SLinus Torvalds 	if (length)
833b77a493bSEric Paris 		goto out;
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds 	length = -ENOMEM;
83689d155efSJames Morris 	scon = kzalloc(size + 1, GFP_KERNEL);
8371da177e4SLinus Torvalds 	if (!scon)
838b77a493bSEric Paris 		goto out;
8391da177e4SLinus Torvalds 
840b77a493bSEric Paris 	length = -ENOMEM;
84189d155efSJames Morris 	tcon = kzalloc(size + 1, GFP_KERNEL);
8421da177e4SLinus Torvalds 	if (!tcon)
8431da177e4SLinus Torvalds 		goto out;
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds 	length = -EINVAL;
84619439d05SStephen Smalley 	if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
847b77a493bSEric Paris 		goto out;
8481da177e4SLinus Torvalds 
8490619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
850b77a493bSEric Paris 	if (length)
851b77a493bSEric Paris 		goto out;
852b77a493bSEric Paris 
8530619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
854b77a493bSEric Paris 	if (length)
855b77a493bSEric Paris 		goto out;
8561da177e4SLinus Torvalds 
8570619f0f5SStephen Smalley 	security_compute_av_user(state, ssid, tsid, tclass, &avd);
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds 	length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
8608a6f83afSKaiGai Kohei 			  "%x %x %x %x %u %x",
861f1c6381aSEric Paris 			  avd.allowed, 0xffffffff,
8621da177e4SLinus Torvalds 			  avd.auditallow, avd.auditdeny,
8638a6f83afSKaiGai Kohei 			  avd.seqno, avd.flags);
8641da177e4SLinus Torvalds out:
865b77a493bSEric Paris 	kfree(tcon);
8661da177e4SLinus Torvalds 	kfree(scon);
8671da177e4SLinus Torvalds 	return length;
8681da177e4SLinus Torvalds }
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
8711da177e4SLinus Torvalds {
8720619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
8730619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
874b77a493bSEric Paris 	char *scon = NULL, *tcon = NULL;
875f50a3ec9SKohei Kaigai 	char *namebuf = NULL, *objname = NULL;
8761da177e4SLinus Torvalds 	u32 ssid, tsid, newsid;
8771da177e4SLinus Torvalds 	u16 tclass;
8781da177e4SLinus Torvalds 	ssize_t length;
879b77a493bSEric Paris 	char *newcon = NULL;
8801da177e4SLinus Torvalds 	u32 len;
881f50a3ec9SKohei Kaigai 	int nargs;
8821da177e4SLinus Torvalds 
8836b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
8846b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
885be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__COMPUTE_CREATE,
886be0554c9SStephen Smalley 			      NULL);
8871da177e4SLinus Torvalds 	if (length)
888b77a493bSEric Paris 		goto out;
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds 	length = -ENOMEM;
89189d155efSJames Morris 	scon = kzalloc(size + 1, GFP_KERNEL);
8921da177e4SLinus Torvalds 	if (!scon)
893b77a493bSEric Paris 		goto out;
8941da177e4SLinus Torvalds 
895b77a493bSEric Paris 	length = -ENOMEM;
89689d155efSJames Morris 	tcon = kzalloc(size + 1, GFP_KERNEL);
8971da177e4SLinus Torvalds 	if (!tcon)
8981da177e4SLinus Torvalds 		goto out;
8991da177e4SLinus Torvalds 
900f50a3ec9SKohei Kaigai 	length = -ENOMEM;
901f50a3ec9SKohei Kaigai 	namebuf = kzalloc(size + 1, GFP_KERNEL);
902f50a3ec9SKohei Kaigai 	if (!namebuf)
903b77a493bSEric Paris 		goto out;
9041da177e4SLinus Torvalds 
905f50a3ec9SKohei Kaigai 	length = -EINVAL;
906f50a3ec9SKohei Kaigai 	nargs = sscanf(buf, "%s %s %hu %s", scon, tcon, &tclass, namebuf);
907f50a3ec9SKohei Kaigai 	if (nargs < 3 || nargs > 4)
908f50a3ec9SKohei Kaigai 		goto out;
9090f7e4c33SKohei Kaigai 	if (nargs == 4) {
9100f7e4c33SKohei Kaigai 		/*
9110f7e4c33SKohei Kaigai 		 * If and when the name of new object to be queried contains
9120f7e4c33SKohei Kaigai 		 * either whitespace or multibyte characters, they shall be
9130f7e4c33SKohei Kaigai 		 * encoded based on the percentage-encoding rule.
9140f7e4c33SKohei Kaigai 		 * If not encoded, the sscanf logic picks up only left-half
9150f7e4c33SKohei Kaigai 		 * of the supplied name; splitted by a whitespace unexpectedly.
9160f7e4c33SKohei Kaigai 		 */
9170f7e4c33SKohei Kaigai 		char   *r, *w;
9180f7e4c33SKohei Kaigai 		int     c1, c2;
9190f7e4c33SKohei Kaigai 
9200f7e4c33SKohei Kaigai 		r = w = namebuf;
9210f7e4c33SKohei Kaigai 		do {
9220f7e4c33SKohei Kaigai 			c1 = *r++;
9230f7e4c33SKohei Kaigai 			if (c1 == '+')
9240f7e4c33SKohei Kaigai 				c1 = ' ';
9250f7e4c33SKohei Kaigai 			else if (c1 == '%') {
926af7ff2c2SAndy Shevchenko 				c1 = hex_to_bin(*r++);
927af7ff2c2SAndy Shevchenko 				if (c1 < 0)
9280f7e4c33SKohei Kaigai 					goto out;
929af7ff2c2SAndy Shevchenko 				c2 = hex_to_bin(*r++);
930af7ff2c2SAndy Shevchenko 				if (c2 < 0)
9310f7e4c33SKohei Kaigai 					goto out;
9320f7e4c33SKohei Kaigai 				c1 = (c1 << 4) | c2;
9330f7e4c33SKohei Kaigai 			}
9340f7e4c33SKohei Kaigai 			*w++ = c1;
9350f7e4c33SKohei Kaigai 		} while (c1 != '\0');
9360f7e4c33SKohei Kaigai 
937f50a3ec9SKohei Kaigai 		objname = namebuf;
9380f7e4c33SKohei Kaigai 	}
939f50a3ec9SKohei Kaigai 
9400619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
941b77a493bSEric Paris 	if (length)
942b77a493bSEric Paris 		goto out;
943b77a493bSEric Paris 
9440619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
945b77a493bSEric Paris 	if (length)
946b77a493bSEric Paris 		goto out;
9471da177e4SLinus Torvalds 
9480619f0f5SStephen Smalley 	length = security_transition_sid_user(state, ssid, tsid, tclass,
9490619f0f5SStephen Smalley 					      objname, &newsid);
950b77a493bSEric Paris 	if (length)
951b77a493bSEric Paris 		goto out;
9521da177e4SLinus Torvalds 
9530619f0f5SStephen Smalley 	length = security_sid_to_context(state, newsid, &newcon, &len);
954b77a493bSEric Paris 	if (length)
955b77a493bSEric Paris 		goto out;
9561da177e4SLinus Torvalds 
957b77a493bSEric Paris 	length = -ERANGE;
9581da177e4SLinus Torvalds 	if (len > SIMPLE_TRANSACTION_LIMIT) {
959f8b69a5fSpeter enderborg 		pr_err("SELinux: %s:  context size (%u) exceeds "
960744ba35eSEric Paris 			"payload max\n", __func__, len);
961b77a493bSEric Paris 		goto out;
9621da177e4SLinus Torvalds 	}
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds 	memcpy(buf, newcon, len);
9651da177e4SLinus Torvalds 	length = len;
9661da177e4SLinus Torvalds out:
967b77a493bSEric Paris 	kfree(newcon);
968f50a3ec9SKohei Kaigai 	kfree(namebuf);
969b77a493bSEric Paris 	kfree(tcon);
9701da177e4SLinus Torvalds 	kfree(scon);
9711da177e4SLinus Torvalds 	return length;
9721da177e4SLinus Torvalds }
9731da177e4SLinus Torvalds 
9741da177e4SLinus Torvalds static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
9751da177e4SLinus Torvalds {
9760619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
9770619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
978b77a493bSEric Paris 	char *scon = NULL, *tcon = NULL;
9791da177e4SLinus Torvalds 	u32 ssid, tsid, newsid;
9801da177e4SLinus Torvalds 	u16 tclass;
9811da177e4SLinus Torvalds 	ssize_t length;
982b77a493bSEric Paris 	char *newcon = NULL;
9831da177e4SLinus Torvalds 	u32 len;
9841da177e4SLinus Torvalds 
9856b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
9866b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
987be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__COMPUTE_RELABEL,
988be0554c9SStephen Smalley 			      NULL);
9891da177e4SLinus Torvalds 	if (length)
990b77a493bSEric Paris 		goto out;
9911da177e4SLinus Torvalds 
9921da177e4SLinus Torvalds 	length = -ENOMEM;
99389d155efSJames Morris 	scon = kzalloc(size + 1, GFP_KERNEL);
9941da177e4SLinus Torvalds 	if (!scon)
995b77a493bSEric Paris 		goto out;
9961da177e4SLinus Torvalds 
997b77a493bSEric Paris 	length = -ENOMEM;
99889d155efSJames Morris 	tcon = kzalloc(size + 1, GFP_KERNEL);
9991da177e4SLinus Torvalds 	if (!tcon)
10001da177e4SLinus Torvalds 		goto out;
10011da177e4SLinus Torvalds 
10021da177e4SLinus Torvalds 	length = -EINVAL;
10031da177e4SLinus Torvalds 	if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
1004b77a493bSEric Paris 		goto out;
10051da177e4SLinus Torvalds 
10060619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
1007b77a493bSEric Paris 	if (length)
1008b77a493bSEric Paris 		goto out;
1009b77a493bSEric Paris 
10100619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
1011b77a493bSEric Paris 	if (length)
1012b77a493bSEric Paris 		goto out;
10131da177e4SLinus Torvalds 
10140619f0f5SStephen Smalley 	length = security_change_sid(state, ssid, tsid, tclass, &newsid);
1015b77a493bSEric Paris 	if (length)
1016b77a493bSEric Paris 		goto out;
10171da177e4SLinus Torvalds 
10180619f0f5SStephen Smalley 	length = security_sid_to_context(state, newsid, &newcon, &len);
1019b77a493bSEric Paris 	if (length)
1020b77a493bSEric Paris 		goto out;
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds 	length = -ERANGE;
1023b77a493bSEric Paris 	if (len > SIMPLE_TRANSACTION_LIMIT)
1024b77a493bSEric Paris 		goto out;
10251da177e4SLinus Torvalds 
10261da177e4SLinus Torvalds 	memcpy(buf, newcon, len);
10271da177e4SLinus Torvalds 	length = len;
10281da177e4SLinus Torvalds out:
1029b77a493bSEric Paris 	kfree(newcon);
1030b77a493bSEric Paris 	kfree(tcon);
10311da177e4SLinus Torvalds 	kfree(scon);
10321da177e4SLinus Torvalds 	return length;
10331da177e4SLinus Torvalds }
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
10361da177e4SLinus Torvalds {
10370619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
10380619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
1039b77a493bSEric Paris 	char *con = NULL, *user = NULL, *ptr;
1040b77a493bSEric Paris 	u32 sid, *sids = NULL;
10411da177e4SLinus Torvalds 	ssize_t length;
10421da177e4SLinus Torvalds 	char *newcon;
10431da177e4SLinus Torvalds 	int i, rc;
10441da177e4SLinus Torvalds 	u32 len, nsids;
10451da177e4SLinus Torvalds 
10466b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
10476b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
1048be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__COMPUTE_USER,
1049be0554c9SStephen Smalley 			      NULL);
10501da177e4SLinus Torvalds 	if (length)
10516eab04a8SJustin P. Mattock 		goto out;
10521da177e4SLinus Torvalds 
10531da177e4SLinus Torvalds 	length = -ENOMEM;
105489d155efSJames Morris 	con = kzalloc(size + 1, GFP_KERNEL);
10551da177e4SLinus Torvalds 	if (!con)
10566eab04a8SJustin P. Mattock 		goto out;
10571da177e4SLinus Torvalds 
1058b77a493bSEric Paris 	length = -ENOMEM;
105989d155efSJames Morris 	user = kzalloc(size + 1, GFP_KERNEL);
10601da177e4SLinus Torvalds 	if (!user)
10611da177e4SLinus Torvalds 		goto out;
10621da177e4SLinus Torvalds 
10631da177e4SLinus Torvalds 	length = -EINVAL;
10641da177e4SLinus Torvalds 	if (sscanf(buf, "%s %s", con, user) != 2)
1065b77a493bSEric Paris 		goto out;
10661da177e4SLinus Torvalds 
10670619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, con, &sid, GFP_KERNEL);
1068b77a493bSEric Paris 	if (length)
1069b77a493bSEric Paris 		goto out;
10701da177e4SLinus Torvalds 
10710619f0f5SStephen Smalley 	length = security_get_user_sids(state, sid, user, &sids, &nsids);
1072b77a493bSEric Paris 	if (length)
1073b77a493bSEric Paris 		goto out;
10741da177e4SLinus Torvalds 
10751da177e4SLinus Torvalds 	length = sprintf(buf, "%u", nsids) + 1;
10761da177e4SLinus Torvalds 	ptr = buf + length;
10771da177e4SLinus Torvalds 	for (i = 0; i < nsids; i++) {
10780619f0f5SStephen Smalley 		rc = security_sid_to_context(state, sids[i], &newcon, &len);
10791da177e4SLinus Torvalds 		if (rc) {
10801da177e4SLinus Torvalds 			length = rc;
1081b77a493bSEric Paris 			goto out;
10821da177e4SLinus Torvalds 		}
10831da177e4SLinus Torvalds 		if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
10841da177e4SLinus Torvalds 			kfree(newcon);
10851da177e4SLinus Torvalds 			length = -ERANGE;
1086b77a493bSEric Paris 			goto out;
10871da177e4SLinus Torvalds 		}
10881da177e4SLinus Torvalds 		memcpy(ptr, newcon, len);
10891da177e4SLinus Torvalds 		kfree(newcon);
10901da177e4SLinus Torvalds 		ptr += len;
10911da177e4SLinus Torvalds 		length += len;
10921da177e4SLinus Torvalds 	}
10931da177e4SLinus Torvalds out:
1094b77a493bSEric Paris 	kfree(sids);
1095b77a493bSEric Paris 	kfree(user);
10961da177e4SLinus Torvalds 	kfree(con);
10971da177e4SLinus Torvalds 	return length;
10981da177e4SLinus Torvalds }
10991da177e4SLinus Torvalds 
11001da177e4SLinus Torvalds static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
11011da177e4SLinus Torvalds {
11020619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
11030619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
1104b77a493bSEric Paris 	char *scon = NULL, *tcon = NULL;
11051da177e4SLinus Torvalds 	u32 ssid, tsid, newsid;
11061da177e4SLinus Torvalds 	u16 tclass;
11071da177e4SLinus Torvalds 	ssize_t length;
1108b77a493bSEric Paris 	char *newcon = NULL;
11091da177e4SLinus Torvalds 	u32 len;
11101da177e4SLinus Torvalds 
11116b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
11126b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
1113be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__COMPUTE_MEMBER,
1114be0554c9SStephen Smalley 			      NULL);
11151da177e4SLinus Torvalds 	if (length)
1116b77a493bSEric Paris 		goto out;
11171da177e4SLinus Torvalds 
11181da177e4SLinus Torvalds 	length = -ENOMEM;
111989d155efSJames Morris 	scon = kzalloc(size + 1, GFP_KERNEL);
11201da177e4SLinus Torvalds 	if (!scon)
11216eab04a8SJustin P. Mattock 		goto out;
11221da177e4SLinus Torvalds 
1123b77a493bSEric Paris 	length = -ENOMEM;
112489d155efSJames Morris 	tcon = kzalloc(size + 1, GFP_KERNEL);
11251da177e4SLinus Torvalds 	if (!tcon)
11261da177e4SLinus Torvalds 		goto out;
11271da177e4SLinus Torvalds 
11281da177e4SLinus Torvalds 	length = -EINVAL;
11291da177e4SLinus Torvalds 	if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
1130b77a493bSEric Paris 		goto out;
11311da177e4SLinus Torvalds 
11320619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
1133b77a493bSEric Paris 	if (length)
1134b77a493bSEric Paris 		goto out;
1135b77a493bSEric Paris 
11360619f0f5SStephen Smalley 	length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
1137b77a493bSEric Paris 	if (length)
1138b77a493bSEric Paris 		goto out;
11391da177e4SLinus Torvalds 
11400619f0f5SStephen Smalley 	length = security_member_sid(state, ssid, tsid, tclass, &newsid);
1141b77a493bSEric Paris 	if (length)
1142b77a493bSEric Paris 		goto out;
11431da177e4SLinus Torvalds 
11440619f0f5SStephen Smalley 	length = security_sid_to_context(state, newsid, &newcon, &len);
1145b77a493bSEric Paris 	if (length)
1146b77a493bSEric Paris 		goto out;
11471da177e4SLinus Torvalds 
1148b77a493bSEric Paris 	length = -ERANGE;
11491da177e4SLinus Torvalds 	if (len > SIMPLE_TRANSACTION_LIMIT) {
1150f8b69a5fSpeter enderborg 		pr_err("SELinux: %s:  context size (%u) exceeds "
1151744ba35eSEric Paris 			"payload max\n", __func__, len);
1152b77a493bSEric Paris 		goto out;
11531da177e4SLinus Torvalds 	}
11541da177e4SLinus Torvalds 
11551da177e4SLinus Torvalds 	memcpy(buf, newcon, len);
11561da177e4SLinus Torvalds 	length = len;
11571da177e4SLinus Torvalds out:
1158b77a493bSEric Paris 	kfree(newcon);
1159b77a493bSEric Paris 	kfree(tcon);
11601da177e4SLinus Torvalds 	kfree(scon);
11611da177e4SLinus Torvalds 	return length;
11621da177e4SLinus Torvalds }
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds static struct inode *sel_make_inode(struct super_block *sb, int mode)
11651da177e4SLinus Torvalds {
11661da177e4SLinus Torvalds 	struct inode *ret = new_inode(sb);
11671da177e4SLinus Torvalds 
11681da177e4SLinus Torvalds 	if (ret) {
11691da177e4SLinus Torvalds 		ret->i_mode = mode;
1170078cd827SDeepa Dinamani 		ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret);
11711da177e4SLinus Torvalds 	}
11721da177e4SLinus Torvalds 	return ret;
11731da177e4SLinus Torvalds }
11741da177e4SLinus Torvalds 
11751da177e4SLinus Torvalds static ssize_t sel_read_bool(struct file *filep, char __user *buf,
11761da177e4SLinus Torvalds 			     size_t count, loff_t *ppos)
11771da177e4SLinus Torvalds {
11780619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
11791da177e4SLinus Torvalds 	char *page = NULL;
11801da177e4SLinus Torvalds 	ssize_t length;
11811da177e4SLinus Torvalds 	ssize_t ret;
11821da177e4SLinus Torvalds 	int cur_enforcing;
1183496ad9aaSAl Viro 	unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
1184d313f948SStephen Smalley 	const char *name = filep->f_path.dentry->d_name.name;
11851da177e4SLinus Torvalds 
11860619f0f5SStephen Smalley 	mutex_lock(&fsi->mutex);
11871da177e4SLinus Torvalds 
1188d313f948SStephen Smalley 	ret = -EINVAL;
11890619f0f5SStephen Smalley 	if (index >= fsi->bool_num || strcmp(name,
11900619f0f5SStephen Smalley 					     fsi->bool_pending_names[index]))
1191d313f948SStephen Smalley 		goto out;
11921da177e4SLinus Torvalds 
11931da177e4SLinus Torvalds 	ret = -ENOMEM;
1194b77a493bSEric Paris 	page = (char *)get_zeroed_page(GFP_KERNEL);
1195b77a493bSEric Paris 	if (!page)
11961da177e4SLinus Torvalds 		goto out;
11971da177e4SLinus Torvalds 
11980619f0f5SStephen Smalley 	cur_enforcing = security_get_bool_value(fsi->state, index);
11991da177e4SLinus Torvalds 	if (cur_enforcing < 0) {
12001da177e4SLinus Torvalds 		ret = cur_enforcing;
12011da177e4SLinus Torvalds 		goto out;
12021da177e4SLinus Torvalds 	}
12031da177e4SLinus Torvalds 	length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
12040619f0f5SStephen Smalley 			  fsi->bool_pending_values[index]);
120568bdcf28SStephen Smalley 	ret = simple_read_from_buffer(buf, count, ppos, page, length);
12061da177e4SLinus Torvalds out:
12070619f0f5SStephen Smalley 	mutex_unlock(&fsi->mutex);
12081da177e4SLinus Torvalds 	free_page((unsigned long)page);
12091da177e4SLinus Torvalds 	return ret;
12101da177e4SLinus Torvalds }
12111da177e4SLinus Torvalds 
12121da177e4SLinus Torvalds static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
12131da177e4SLinus Torvalds 			      size_t count, loff_t *ppos)
12141da177e4SLinus Torvalds {
12150619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
12161da177e4SLinus Torvalds 	char *page = NULL;
1217d313f948SStephen Smalley 	ssize_t length;
12181da177e4SLinus Torvalds 	int new_value;
1219496ad9aaSAl Viro 	unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
1220d313f948SStephen Smalley 	const char *name = filep->f_path.dentry->d_name.name;
12211da177e4SLinus Torvalds 
12220619f0f5SStephen Smalley 	mutex_lock(&fsi->mutex);
12231da177e4SLinus Torvalds 
12246b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
12256b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
1226be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__SETBOOL,
1227be0554c9SStephen Smalley 			      NULL);
12281da177e4SLinus Torvalds 	if (length)
12291da177e4SLinus Torvalds 		goto out;
12301da177e4SLinus Torvalds 
1231d313f948SStephen Smalley 	length = -EINVAL;
12320619f0f5SStephen Smalley 	if (index >= fsi->bool_num || strcmp(name,
12330619f0f5SStephen Smalley 					     fsi->bool_pending_names[index]))
1234d313f948SStephen Smalley 		goto out;
1235d313f948SStephen Smalley 
12361da177e4SLinus Torvalds 	length = -ENOMEM;
1237b77a493bSEric Paris 	if (count >= PAGE_SIZE)
12381da177e4SLinus Torvalds 		goto out;
1239d313f948SStephen Smalley 
12401da177e4SLinus Torvalds 	/* No partial writes. */
1241d313f948SStephen Smalley 	length = -EINVAL;
1242b77a493bSEric Paris 	if (*ppos != 0)
12431da177e4SLinus Torvalds 		goto out;
1244b77a493bSEric Paris 
12458365a719SAl Viro 	page = memdup_user_nul(buf, count);
12468365a719SAl Viro 	if (IS_ERR(page)) {
12478365a719SAl Viro 		length = PTR_ERR(page);
12488365a719SAl Viro 		page = NULL;
12491da177e4SLinus Torvalds 		goto out;
12508365a719SAl Viro 	}
12511da177e4SLinus Torvalds 
12521da177e4SLinus Torvalds 	length = -EINVAL;
12531da177e4SLinus Torvalds 	if (sscanf(page, "%d", &new_value) != 1)
12541da177e4SLinus Torvalds 		goto out;
12551da177e4SLinus Torvalds 
12561da177e4SLinus Torvalds 	if (new_value)
12571da177e4SLinus Torvalds 		new_value = 1;
12581da177e4SLinus Torvalds 
12590619f0f5SStephen Smalley 	fsi->bool_pending_values[index] = new_value;
12601da177e4SLinus Torvalds 	length = count;
12611da177e4SLinus Torvalds 
12621da177e4SLinus Torvalds out:
12630619f0f5SStephen Smalley 	mutex_unlock(&fsi->mutex);
12648365a719SAl Viro 	kfree(page);
12651da177e4SLinus Torvalds 	return length;
12661da177e4SLinus Torvalds }
12671da177e4SLinus Torvalds 
12689c2e08c5SArjan van de Ven static const struct file_operations sel_bool_ops = {
12691da177e4SLinus Torvalds 	.read		= sel_read_bool,
12701da177e4SLinus Torvalds 	.write		= sel_write_bool,
127157a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
12721da177e4SLinus Torvalds };
12731da177e4SLinus Torvalds 
12741da177e4SLinus Torvalds static ssize_t sel_commit_bools_write(struct file *filep,
12751da177e4SLinus Torvalds 				      const char __user *buf,
12761da177e4SLinus Torvalds 				      size_t count, loff_t *ppos)
12771da177e4SLinus Torvalds {
12780619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
12791da177e4SLinus Torvalds 	char *page = NULL;
1280d313f948SStephen Smalley 	ssize_t length;
12811da177e4SLinus Torvalds 	int new_value;
12821da177e4SLinus Torvalds 
12830619f0f5SStephen Smalley 	mutex_lock(&fsi->mutex);
12841da177e4SLinus Torvalds 
12856b6bc620SStephen Smalley 	length = avc_has_perm(&selinux_state,
12866b6bc620SStephen Smalley 			      current_sid(), SECINITSID_SECURITY,
1287be0554c9SStephen Smalley 			      SECCLASS_SECURITY, SECURITY__SETBOOL,
1288be0554c9SStephen Smalley 			      NULL);
12891da177e4SLinus Torvalds 	if (length)
12901da177e4SLinus Torvalds 		goto out;
12911da177e4SLinus Torvalds 
12921da177e4SLinus Torvalds 	length = -ENOMEM;
1293b77a493bSEric Paris 	if (count >= PAGE_SIZE)
12941da177e4SLinus Torvalds 		goto out;
1295b77a493bSEric Paris 
12961da177e4SLinus Torvalds 	/* No partial writes. */
1297b77a493bSEric Paris 	length = -EINVAL;
1298b77a493bSEric Paris 	if (*ppos != 0)
12991da177e4SLinus Torvalds 		goto out;
1300b77a493bSEric Paris 
13018365a719SAl Viro 	page = memdup_user_nul(buf, count);
13028365a719SAl Viro 	if (IS_ERR(page)) {
13038365a719SAl Viro 		length = PTR_ERR(page);
13048365a719SAl Viro 		page = NULL;
13051da177e4SLinus Torvalds 		goto out;
13068365a719SAl Viro 	}
13071da177e4SLinus Torvalds 
13081da177e4SLinus Torvalds 	length = -EINVAL;
13091da177e4SLinus Torvalds 	if (sscanf(page, "%d", &new_value) != 1)
13101da177e4SLinus Torvalds 		goto out;
13111da177e4SLinus Torvalds 
1312b77a493bSEric Paris 	length = 0;
13130619f0f5SStephen Smalley 	if (new_value && fsi->bool_pending_values)
13140619f0f5SStephen Smalley 		length = security_set_bools(fsi->state, fsi->bool_num,
13150619f0f5SStephen Smalley 					    fsi->bool_pending_values);
13161da177e4SLinus Torvalds 
1317b77a493bSEric Paris 	if (!length)
13181da177e4SLinus Torvalds 		length = count;
13191da177e4SLinus Torvalds 
13201da177e4SLinus Torvalds out:
13210619f0f5SStephen Smalley 	mutex_unlock(&fsi->mutex);
13228365a719SAl Viro 	kfree(page);
13231da177e4SLinus Torvalds 	return length;
13241da177e4SLinus Torvalds }
13251da177e4SLinus Torvalds 
13269c2e08c5SArjan van de Ven static const struct file_operations sel_commit_bools_ops = {
13271da177e4SLinus Torvalds 	.write		= sel_commit_bools_write,
132857a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
13291da177e4SLinus Torvalds };
13301da177e4SLinus Torvalds 
13310c92d7c7SChristopher J. PeBenito static void sel_remove_entries(struct dentry *de)
13321da177e4SLinus Torvalds {
1333ad52184bSAl Viro 	d_genocide(de);
1334ad52184bSAl Viro 	shrink_dcache_parent(de);
13351da177e4SLinus Torvalds }
13361da177e4SLinus Torvalds 
13371da177e4SLinus Torvalds #define BOOL_DIR_NAME "booleans"
13381da177e4SLinus Torvalds 
13390619f0f5SStephen Smalley static int sel_make_bools(struct selinux_fs_info *fsi)
13401da177e4SLinus Torvalds {
1341b77a493bSEric Paris 	int i, ret;
13421da177e4SLinus Torvalds 	ssize_t len;
13431da177e4SLinus Torvalds 	struct dentry *dentry = NULL;
13440619f0f5SStephen Smalley 	struct dentry *dir = fsi->bool_dir;
13451da177e4SLinus Torvalds 	struct inode *inode = NULL;
13461da177e4SLinus Torvalds 	struct inode_security_struct *isec;
13471da177e4SLinus Torvalds 	char **names = NULL, *page;
13481da177e4SLinus Torvalds 	int num;
13491da177e4SLinus Torvalds 	int *values = NULL;
13501da177e4SLinus Torvalds 	u32 sid;
13511da177e4SLinus Torvalds 
13521da177e4SLinus Torvalds 	/* remove any existing files */
13530619f0f5SStephen Smalley 	for (i = 0; i < fsi->bool_num; i++)
13540619f0f5SStephen Smalley 		kfree(fsi->bool_pending_names[i]);
13550619f0f5SStephen Smalley 	kfree(fsi->bool_pending_names);
13560619f0f5SStephen Smalley 	kfree(fsi->bool_pending_values);
13570619f0f5SStephen Smalley 	fsi->bool_num = 0;
13580619f0f5SStephen Smalley 	fsi->bool_pending_names = NULL;
13590619f0f5SStephen Smalley 	fsi->bool_pending_values = NULL;
13601da177e4SLinus Torvalds 
13610c92d7c7SChristopher J. PeBenito 	sel_remove_entries(dir);
13621da177e4SLinus Torvalds 
1363b77a493bSEric Paris 	ret = -ENOMEM;
13641872981bSEric Paris 	page = (char *)get_zeroed_page(GFP_KERNEL);
13651872981bSEric Paris 	if (!page)
1366b77a493bSEric Paris 		goto out;
13671da177e4SLinus Torvalds 
13680619f0f5SStephen Smalley 	ret = security_get_bools(fsi->state, &num, &names, &values);
1369b77a493bSEric Paris 	if (ret)
13701da177e4SLinus Torvalds 		goto out;
13711da177e4SLinus Torvalds 
13721da177e4SLinus Torvalds 	for (i = 0; i < num; i++) {
1373b77a493bSEric Paris 		ret = -ENOMEM;
13741da177e4SLinus Torvalds 		dentry = d_alloc_name(dir, names[i]);
1375b77a493bSEric Paris 		if (!dentry)
1376b77a493bSEric Paris 			goto out;
13771da177e4SLinus Torvalds 
1378b77a493bSEric Paris 		ret = -ENOMEM;
1379b77a493bSEric Paris 		inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
13807e4237faSnixiaoming 		if (!inode) {
13817e4237faSnixiaoming 			dput(dentry);
1382b77a493bSEric Paris 			goto out;
13837e4237faSnixiaoming 		}
1384b77a493bSEric Paris 
13851da177e4SLinus Torvalds 		ret = -ENAMETOOLONG;
1386cc1dad71SAl Viro 		len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
13877e4237faSnixiaoming 		if (len >= PAGE_SIZE) {
13887e4237faSnixiaoming 			dput(dentry);
13897e4237faSnixiaoming 			iput(inode);
1390b77a493bSEric Paris 			goto out;
13917e4237faSnixiaoming 		}
1392b77a493bSEric Paris 
13931da177e4SLinus Torvalds 		isec = (struct inode_security_struct *)inode->i_security;
13940619f0f5SStephen Smalley 		ret = security_genfs_sid(fsi->state, "selinuxfs", page,
1395aa8e712cSStephen Smalley 					 SECCLASS_FILE, &sid);
13964262fb51SGary Tierney 		if (ret) {
1397900fde06SGary Tierney 			pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n",
1398900fde06SGary Tierney 					   page);
1399900fde06SGary Tierney 			sid = SECINITSID_SECURITY;
14004262fb51SGary Tierney 		}
14014262fb51SGary Tierney 
14021da177e4SLinus Torvalds 		isec->sid = sid;
140342059112SAndreas Gruenbacher 		isec->initialized = LABEL_INITIALIZED;
14041da177e4SLinus Torvalds 		inode->i_fop = &sel_bool_ops;
1405bce34bc0SJames Carter 		inode->i_ino = i|SEL_BOOL_INO_OFFSET;
14061da177e4SLinus Torvalds 		d_add(dentry, inode);
14071da177e4SLinus Torvalds 	}
14080619f0f5SStephen Smalley 	fsi->bool_num = num;
14090619f0f5SStephen Smalley 	fsi->bool_pending_names = names;
14100619f0f5SStephen Smalley 	fsi->bool_pending_values = values;
1411b77a493bSEric Paris 
1412b77a493bSEric Paris 	free_page((unsigned long)page);
1413b77a493bSEric Paris 	return 0;
14141da177e4SLinus Torvalds out:
14151da177e4SLinus Torvalds 	free_page((unsigned long)page);
1416b77a493bSEric Paris 
14171da177e4SLinus Torvalds 	if (names) {
14189a5f04bfSJesper Juhl 		for (i = 0; i < num; i++)
14191da177e4SLinus Torvalds 			kfree(names[i]);
14201da177e4SLinus Torvalds 		kfree(names);
14211da177e4SLinus Torvalds 	}
142220c19e41SDavi Arnaut 	kfree(values);
14230c92d7c7SChristopher J. PeBenito 	sel_remove_entries(dir);
1424b77a493bSEric Paris 
1425b77a493bSEric Paris 	return ret;
14261da177e4SLinus Torvalds }
14271da177e4SLinus Torvalds 
14281da177e4SLinus Torvalds static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
14291da177e4SLinus Torvalds 					    size_t count, loff_t *ppos)
14301da177e4SLinus Torvalds {
14316b6bc620SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
14326b6bc620SStephen Smalley 	struct selinux_state *state = fsi->state;
14331da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
14341da177e4SLinus Torvalds 	ssize_t length;
14351da177e4SLinus Torvalds 
14366b6bc620SStephen Smalley 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
14376b6bc620SStephen Smalley 			   avc_get_cache_threshold(state->avc));
14381da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
14391da177e4SLinus Torvalds }
14401da177e4SLinus Torvalds 
14411da177e4SLinus Torvalds static ssize_t sel_write_avc_cache_threshold(struct file *file,
14421da177e4SLinus Torvalds 					     const char __user *buf,
14431da177e4SLinus Torvalds 					     size_t count, loff_t *ppos)
14441da177e4SLinus Torvalds 
14451da177e4SLinus Torvalds {
14466b6bc620SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
14476b6bc620SStephen Smalley 	struct selinux_state *state = fsi->state;
14488365a719SAl Viro 	char *page;
14491da177e4SLinus Torvalds 	ssize_t ret;
1450309c5fadSHeinrich Schuchardt 	unsigned int new_value;
14511da177e4SLinus Torvalds 
14526b6bc620SStephen Smalley 	ret = avc_has_perm(&selinux_state,
14536b6bc620SStephen Smalley 			   current_sid(), SECINITSID_SECURITY,
1454be0554c9SStephen Smalley 			   SECCLASS_SECURITY, SECURITY__SETSECPARAM,
1455be0554c9SStephen Smalley 			   NULL);
14561da177e4SLinus Torvalds 	if (ret)
14578365a719SAl Viro 		return ret;
1458b77a493bSEric Paris 
1459b77a493bSEric Paris 	if (count >= PAGE_SIZE)
14608365a719SAl Viro 		return -ENOMEM;
1461b77a493bSEric Paris 
1462b77a493bSEric Paris 	/* No partial writes. */
1463b77a493bSEric Paris 	if (*ppos != 0)
14648365a719SAl Viro 		return -EINVAL;
1465b77a493bSEric Paris 
14668365a719SAl Viro 	page = memdup_user_nul(buf, count);
14678365a719SAl Viro 	if (IS_ERR(page))
14688365a719SAl Viro 		return PTR_ERR(page);
1469b77a493bSEric Paris 
1470b77a493bSEric Paris 	ret = -EINVAL;
1471b77a493bSEric Paris 	if (sscanf(page, "%u", &new_value) != 1)
1472b77a493bSEric Paris 		goto out;
1473b77a493bSEric Paris 
14746b6bc620SStephen Smalley 	avc_set_cache_threshold(state->avc, new_value);
1475b77a493bSEric Paris 
14761da177e4SLinus Torvalds 	ret = count;
14771da177e4SLinus Torvalds out:
14788365a719SAl Viro 	kfree(page);
14791da177e4SLinus Torvalds 	return ret;
14801da177e4SLinus Torvalds }
14811da177e4SLinus Torvalds 
14821da177e4SLinus Torvalds static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
14831da177e4SLinus Torvalds 				       size_t count, loff_t *ppos)
14841da177e4SLinus Torvalds {
14856b6bc620SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
14866b6bc620SStephen Smalley 	struct selinux_state *state = fsi->state;
14871da177e4SLinus Torvalds 	char *page;
1488b77a493bSEric Paris 	ssize_t length;
14891da177e4SLinus Torvalds 
14901da177e4SLinus Torvalds 	page = (char *)__get_free_page(GFP_KERNEL);
1491b77a493bSEric Paris 	if (!page)
1492b77a493bSEric Paris 		return -ENOMEM;
1493b77a493bSEric Paris 
14946b6bc620SStephen Smalley 	length = avc_get_hash_stats(state->avc, page);
1495b77a493bSEric Paris 	if (length >= 0)
1496b77a493bSEric Paris 		length = simple_read_from_buffer(buf, count, ppos, page, length);
14971da177e4SLinus Torvalds 	free_page((unsigned long)page);
1498b77a493bSEric Paris 
1499b77a493bSEric Paris 	return length;
15001da177e4SLinus Torvalds }
15011da177e4SLinus Torvalds 
15029c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_threshold_ops = {
15031da177e4SLinus Torvalds 	.read		= sel_read_avc_cache_threshold,
15041da177e4SLinus Torvalds 	.write		= sel_write_avc_cache_threshold,
150557a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
15061da177e4SLinus Torvalds };
15071da177e4SLinus Torvalds 
15089c2e08c5SArjan van de Ven static const struct file_operations sel_avc_hash_stats_ops = {
15091da177e4SLinus Torvalds 	.read		= sel_read_avc_hash_stats,
151057a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
15111da177e4SLinus Torvalds };
15121da177e4SLinus Torvalds 
15131da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
15141da177e4SLinus Torvalds static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
15151da177e4SLinus Torvalds {
15161da177e4SLinus Torvalds 	int cpu;
15171da177e4SLinus Torvalds 
15184f4b6c1aSRusty Russell 	for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
15191da177e4SLinus Torvalds 		if (!cpu_possible(cpu))
15201da177e4SLinus Torvalds 			continue;
15211da177e4SLinus Torvalds 		*idx = cpu + 1;
15221da177e4SLinus Torvalds 		return &per_cpu(avc_cache_stats, cpu);
15231da177e4SLinus Torvalds 	}
15241da177e4SLinus Torvalds 	return NULL;
15251da177e4SLinus Torvalds }
15261da177e4SLinus Torvalds 
15271da177e4SLinus Torvalds static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
15281da177e4SLinus Torvalds {
15291da177e4SLinus Torvalds 	loff_t n = *pos - 1;
15301da177e4SLinus Torvalds 
15311da177e4SLinus Torvalds 	if (*pos == 0)
15321da177e4SLinus Torvalds 		return SEQ_START_TOKEN;
15331da177e4SLinus Torvalds 
15341da177e4SLinus Torvalds 	return sel_avc_get_stat_idx(&n);
15351da177e4SLinus Torvalds }
15361da177e4SLinus Torvalds 
15371da177e4SLinus Torvalds static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
15381da177e4SLinus Torvalds {
15391da177e4SLinus Torvalds 	return sel_avc_get_stat_idx(pos);
15401da177e4SLinus Torvalds }
15411da177e4SLinus Torvalds 
15421da177e4SLinus Torvalds static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
15431da177e4SLinus Torvalds {
15441da177e4SLinus Torvalds 	struct avc_cache_stats *st = v;
15451da177e4SLinus Torvalds 
1546710a0647SMarkus Elfring 	if (v == SEQ_START_TOKEN) {
1547710a0647SMarkus Elfring 		seq_puts(seq,
1548710a0647SMarkus Elfring 			 "lookups hits misses allocations reclaims frees\n");
1549710a0647SMarkus Elfring 	} else {
1550257313b2SLinus Torvalds 		unsigned int lookups = st->lookups;
1551257313b2SLinus Torvalds 		unsigned int misses = st->misses;
1552257313b2SLinus Torvalds 		unsigned int hits = lookups - misses;
1553257313b2SLinus Torvalds 		seq_printf(seq, "%u %u %u %u %u %u\n", lookups,
1554257313b2SLinus Torvalds 			   hits, misses, st->allocations,
15551da177e4SLinus Torvalds 			   st->reclaims, st->frees);
1556257313b2SLinus Torvalds 	}
15571da177e4SLinus Torvalds 	return 0;
15581da177e4SLinus Torvalds }
15591da177e4SLinus Torvalds 
15601da177e4SLinus Torvalds static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
15611da177e4SLinus Torvalds { }
15621da177e4SLinus Torvalds 
15631996a109SJan Engelhardt static const struct seq_operations sel_avc_cache_stats_seq_ops = {
15641da177e4SLinus Torvalds 	.start		= sel_avc_stats_seq_start,
15651da177e4SLinus Torvalds 	.next		= sel_avc_stats_seq_next,
15661da177e4SLinus Torvalds 	.show		= sel_avc_stats_seq_show,
15671da177e4SLinus Torvalds 	.stop		= sel_avc_stats_seq_stop,
15681da177e4SLinus Torvalds };
15691da177e4SLinus Torvalds 
15701da177e4SLinus Torvalds static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
15711da177e4SLinus Torvalds {
15721da177e4SLinus Torvalds 	return seq_open(file, &sel_avc_cache_stats_seq_ops);
15731da177e4SLinus Torvalds }
15741da177e4SLinus Torvalds 
15759c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_stats_ops = {
15761da177e4SLinus Torvalds 	.open		= sel_open_avc_cache_stats,
15771da177e4SLinus Torvalds 	.read		= seq_read,
15781da177e4SLinus Torvalds 	.llseek		= seq_lseek,
15791da177e4SLinus Torvalds 	.release	= seq_release,
15801da177e4SLinus Torvalds };
15811da177e4SLinus Torvalds #endif
15821da177e4SLinus Torvalds 
15831da177e4SLinus Torvalds static int sel_make_avc_files(struct dentry *dir)
15841da177e4SLinus Torvalds {
15850619f0f5SStephen Smalley 	struct super_block *sb = dir->d_sb;
15860619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = sb->s_fs_info;
1587b77a493bSEric Paris 	int i;
1588cda37124SEric Biggers 	static const struct tree_descr files[] = {
15891da177e4SLinus Torvalds 		{ "cache_threshold",
15901da177e4SLinus Torvalds 		  &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
15911da177e4SLinus Torvalds 		{ "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
15921da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
15931da177e4SLinus Torvalds 		{ "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
15941da177e4SLinus Torvalds #endif
15951da177e4SLinus Torvalds 	};
15961da177e4SLinus Torvalds 
15976e20a64aSNicolas Kaiser 	for (i = 0; i < ARRAY_SIZE(files); i++) {
15981da177e4SLinus Torvalds 		struct inode *inode;
15991da177e4SLinus Torvalds 		struct dentry *dentry;
16001da177e4SLinus Torvalds 
16011da177e4SLinus Torvalds 		dentry = d_alloc_name(dir, files[i].name);
1602b77a493bSEric Paris 		if (!dentry)
1603b77a493bSEric Paris 			return -ENOMEM;
16041da177e4SLinus Torvalds 
16051da177e4SLinus Torvalds 		inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
16067e4237faSnixiaoming 		if (!inode) {
16077e4237faSnixiaoming 			dput(dentry);
1608b77a493bSEric Paris 			return -ENOMEM;
16097e4237faSnixiaoming 		}
1610b77a493bSEric Paris 
16111da177e4SLinus Torvalds 		inode->i_fop = files[i].ops;
16120619f0f5SStephen Smalley 		inode->i_ino = ++fsi->last_ino;
16131da177e4SLinus Torvalds 		d_add(dentry, inode);
16141da177e4SLinus Torvalds 	}
1615b77a493bSEric Paris 
1616b77a493bSEric Paris 	return 0;
16171da177e4SLinus Torvalds }
16181da177e4SLinus Torvalds 
1619f0ee2e46SJames Carter static ssize_t sel_read_initcon(struct file *file, char __user *buf,
1620f0ee2e46SJames Carter 				size_t count, loff_t *ppos)
1621f0ee2e46SJames Carter {
16220619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1623f0ee2e46SJames Carter 	char *con;
1624f0ee2e46SJames Carter 	u32 sid, len;
1625f0ee2e46SJames Carter 	ssize_t ret;
1626f0ee2e46SJames Carter 
1627496ad9aaSAl Viro 	sid = file_inode(file)->i_ino&SEL_INO_MASK;
16280619f0f5SStephen Smalley 	ret = security_sid_to_context(fsi->state, sid, &con, &len);
1629b77a493bSEric Paris 	if (ret)
1630f0ee2e46SJames Carter 		return ret;
1631f0ee2e46SJames Carter 
1632f0ee2e46SJames Carter 	ret = simple_read_from_buffer(buf, count, ppos, con, len);
1633f0ee2e46SJames Carter 	kfree(con);
1634f0ee2e46SJames Carter 	return ret;
1635f0ee2e46SJames Carter }
1636f0ee2e46SJames Carter 
1637f0ee2e46SJames Carter static const struct file_operations sel_initcon_ops = {
1638f0ee2e46SJames Carter 	.read		= sel_read_initcon,
163957a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
1640f0ee2e46SJames Carter };
1641f0ee2e46SJames Carter 
1642f0ee2e46SJames Carter static int sel_make_initcon_files(struct dentry *dir)
1643f0ee2e46SJames Carter {
1644b77a493bSEric Paris 	int i;
1645f0ee2e46SJames Carter 
1646f0ee2e46SJames Carter 	for (i = 1; i <= SECINITSID_NUM; i++) {
1647f0ee2e46SJames Carter 		struct inode *inode;
1648f0ee2e46SJames Carter 		struct dentry *dentry;
1649f0ee2e46SJames Carter 		dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
1650b77a493bSEric Paris 		if (!dentry)
1651b77a493bSEric Paris 			return -ENOMEM;
1652f0ee2e46SJames Carter 
1653f0ee2e46SJames Carter 		inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
16547e4237faSnixiaoming 		if (!inode) {
16557e4237faSnixiaoming 			dput(dentry);
1656b77a493bSEric Paris 			return -ENOMEM;
16577e4237faSnixiaoming 		}
1658b77a493bSEric Paris 
1659f0ee2e46SJames Carter 		inode->i_fop = &sel_initcon_ops;
1660f0ee2e46SJames Carter 		inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1661f0ee2e46SJames Carter 		d_add(dentry, inode);
1662f0ee2e46SJames Carter 	}
1663b77a493bSEric Paris 
1664b77a493bSEric Paris 	return 0;
1665f0ee2e46SJames Carter }
1666f0ee2e46SJames Carter 
1667e47c8fc5SChristopher J. PeBenito static inline unsigned long sel_class_to_ino(u16 class)
1668e47c8fc5SChristopher J. PeBenito {
1669e47c8fc5SChristopher J. PeBenito 	return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1670e47c8fc5SChristopher J. PeBenito }
1671e47c8fc5SChristopher J. PeBenito 
1672e47c8fc5SChristopher J. PeBenito static inline u16 sel_ino_to_class(unsigned long ino)
1673e47c8fc5SChristopher J. PeBenito {
167492ae9e82SEric Paris 	return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1);
1675e47c8fc5SChristopher J. PeBenito }
1676e47c8fc5SChristopher J. PeBenito 
1677e47c8fc5SChristopher J. PeBenito static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1678e47c8fc5SChristopher J. PeBenito {
1679e47c8fc5SChristopher J. PeBenito 	return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1680e47c8fc5SChristopher J. PeBenito }
1681e47c8fc5SChristopher J. PeBenito 
1682e47c8fc5SChristopher J. PeBenito static inline u32 sel_ino_to_perm(unsigned long ino)
1683e47c8fc5SChristopher J. PeBenito {
1684e47c8fc5SChristopher J. PeBenito 	return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1685e47c8fc5SChristopher J. PeBenito }
1686e47c8fc5SChristopher J. PeBenito 
1687e47c8fc5SChristopher J. PeBenito static ssize_t sel_read_class(struct file *file, char __user *buf,
1688e47c8fc5SChristopher J. PeBenito 				size_t count, loff_t *ppos)
1689e47c8fc5SChristopher J. PeBenito {
1690496ad9aaSAl Viro 	unsigned long ino = file_inode(file)->i_ino;
1691cc1dad71SAl Viro 	char res[TMPBUFLEN];
1692cc1dad71SAl Viro 	ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_class(ino));
1693cc1dad71SAl Viro 	return simple_read_from_buffer(buf, count, ppos, res, len);
1694e47c8fc5SChristopher J. PeBenito }
1695e47c8fc5SChristopher J. PeBenito 
1696e47c8fc5SChristopher J. PeBenito static const struct file_operations sel_class_ops = {
1697e47c8fc5SChristopher J. PeBenito 	.read		= sel_read_class,
169857a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
1699e47c8fc5SChristopher J. PeBenito };
1700e47c8fc5SChristopher J. PeBenito 
1701e47c8fc5SChristopher J. PeBenito static ssize_t sel_read_perm(struct file *file, char __user *buf,
1702e47c8fc5SChristopher J. PeBenito 				size_t count, loff_t *ppos)
1703e47c8fc5SChristopher J. PeBenito {
1704496ad9aaSAl Viro 	unsigned long ino = file_inode(file)->i_ino;
1705cc1dad71SAl Viro 	char res[TMPBUFLEN];
1706cc1dad71SAl Viro 	ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino));
1707cc1dad71SAl Viro 	return simple_read_from_buffer(buf, count, ppos, res, len);
1708e47c8fc5SChristopher J. PeBenito }
1709e47c8fc5SChristopher J. PeBenito 
1710e47c8fc5SChristopher J. PeBenito static const struct file_operations sel_perm_ops = {
1711e47c8fc5SChristopher J. PeBenito 	.read		= sel_read_perm,
171257a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
1713e47c8fc5SChristopher J. PeBenito };
1714e47c8fc5SChristopher J. PeBenito 
17153bb56b25SPaul Moore static ssize_t sel_read_policycap(struct file *file, char __user *buf,
17163bb56b25SPaul Moore 				  size_t count, loff_t *ppos)
17173bb56b25SPaul Moore {
17180619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
17193bb56b25SPaul Moore 	int value;
17203bb56b25SPaul Moore 	char tmpbuf[TMPBUFLEN];
17213bb56b25SPaul Moore 	ssize_t length;
1722496ad9aaSAl Viro 	unsigned long i_ino = file_inode(file)->i_ino;
17233bb56b25SPaul Moore 
17240619f0f5SStephen Smalley 	value = security_policycap_supported(fsi->state, i_ino & SEL_INO_MASK);
17253bb56b25SPaul Moore 	length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
17263bb56b25SPaul Moore 
17273bb56b25SPaul Moore 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
17283bb56b25SPaul Moore }
17293bb56b25SPaul Moore 
17303bb56b25SPaul Moore static const struct file_operations sel_policycap_ops = {
17313bb56b25SPaul Moore 	.read		= sel_read_policycap,
173257a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
17333bb56b25SPaul Moore };
17343bb56b25SPaul Moore 
1735e47c8fc5SChristopher J. PeBenito static int sel_make_perm_files(char *objclass, int classvalue,
1736e47c8fc5SChristopher J. PeBenito 				struct dentry *dir)
1737e47c8fc5SChristopher J. PeBenito {
17380619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = dir->d_sb->s_fs_info;
1739b77a493bSEric Paris 	int i, rc, nperms;
1740e47c8fc5SChristopher J. PeBenito 	char **perms;
1741e47c8fc5SChristopher J. PeBenito 
17420619f0f5SStephen Smalley 	rc = security_get_permissions(fsi->state, objclass, &perms, &nperms);
1743e47c8fc5SChristopher J. PeBenito 	if (rc)
1744b77a493bSEric Paris 		return rc;
1745e47c8fc5SChristopher J. PeBenito 
1746e47c8fc5SChristopher J. PeBenito 	for (i = 0; i < nperms; i++) {
1747e47c8fc5SChristopher J. PeBenito 		struct inode *inode;
1748e47c8fc5SChristopher J. PeBenito 		struct dentry *dentry;
1749e47c8fc5SChristopher J. PeBenito 
1750b77a493bSEric Paris 		rc = -ENOMEM;
1751e47c8fc5SChristopher J. PeBenito 		dentry = d_alloc_name(dir, perms[i]);
1752b77a493bSEric Paris 		if (!dentry)
1753b77a493bSEric Paris 			goto out;
1754e47c8fc5SChristopher J. PeBenito 
1755e47c8fc5SChristopher J. PeBenito 		rc = -ENOMEM;
1756b77a493bSEric Paris 		inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
17577e4237faSnixiaoming 		if (!inode) {
17587e4237faSnixiaoming 			dput(dentry);
1759b77a493bSEric Paris 			goto out;
17607e4237faSnixiaoming 		}
1761b77a493bSEric Paris 
1762e47c8fc5SChristopher J. PeBenito 		inode->i_fop = &sel_perm_ops;
1763e47c8fc5SChristopher J. PeBenito 		/* i+1 since perm values are 1-indexed */
1764e47c8fc5SChristopher J. PeBenito 		inode->i_ino = sel_perm_to_ino(classvalue, i + 1);
1765e47c8fc5SChristopher J. PeBenito 		d_add(dentry, inode);
1766e47c8fc5SChristopher J. PeBenito 	}
1767b77a493bSEric Paris 	rc = 0;
1768b77a493bSEric Paris out:
1769e47c8fc5SChristopher J. PeBenito 	for (i = 0; i < nperms; i++)
1770e47c8fc5SChristopher J. PeBenito 		kfree(perms[i]);
1771e47c8fc5SChristopher J. PeBenito 	kfree(perms);
1772e47c8fc5SChristopher J. PeBenito 	return rc;
1773e47c8fc5SChristopher J. PeBenito }
1774e47c8fc5SChristopher J. PeBenito 
1775e47c8fc5SChristopher J. PeBenito static int sel_make_class_dir_entries(char *classname, int index,
1776e47c8fc5SChristopher J. PeBenito 					struct dentry *dir)
1777e47c8fc5SChristopher J. PeBenito {
17780619f0f5SStephen Smalley 	struct super_block *sb = dir->d_sb;
17790619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = sb->s_fs_info;
1780e47c8fc5SChristopher J. PeBenito 	struct dentry *dentry = NULL;
1781e47c8fc5SChristopher J. PeBenito 	struct inode *inode = NULL;
1782e47c8fc5SChristopher J. PeBenito 	int rc;
1783e47c8fc5SChristopher J. PeBenito 
1784e47c8fc5SChristopher J. PeBenito 	dentry = d_alloc_name(dir, "index");
1785b77a493bSEric Paris 	if (!dentry)
1786b77a493bSEric Paris 		return -ENOMEM;
1787e47c8fc5SChristopher J. PeBenito 
1788e47c8fc5SChristopher J. PeBenito 	inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
17897e4237faSnixiaoming 	if (!inode) {
17907e4237faSnixiaoming 		dput(dentry);
1791b77a493bSEric Paris 		return -ENOMEM;
17927e4237faSnixiaoming 	}
1793e47c8fc5SChristopher J. PeBenito 
1794e47c8fc5SChristopher J. PeBenito 	inode->i_fop = &sel_class_ops;
1795e47c8fc5SChristopher J. PeBenito 	inode->i_ino = sel_class_to_ino(index);
1796e47c8fc5SChristopher J. PeBenito 	d_add(dentry, inode);
1797e47c8fc5SChristopher J. PeBenito 
17980619f0f5SStephen Smalley 	dentry = sel_make_dir(dir, "perms", &fsi->last_class_ino);
1799a1c2aa1eSAl Viro 	if (IS_ERR(dentry))
1800a1c2aa1eSAl Viro 		return PTR_ERR(dentry);
1801e47c8fc5SChristopher J. PeBenito 
1802e47c8fc5SChristopher J. PeBenito 	rc = sel_make_perm_files(classname, index, dentry);
1803e47c8fc5SChristopher J. PeBenito 
1804e47c8fc5SChristopher J. PeBenito 	return rc;
1805e47c8fc5SChristopher J. PeBenito }
1806e47c8fc5SChristopher J. PeBenito 
18070619f0f5SStephen Smalley static int sel_make_classes(struct selinux_fs_info *fsi)
1808e47c8fc5SChristopher J. PeBenito {
18090619f0f5SStephen Smalley 
1810b77a493bSEric Paris 	int rc, nclasses, i;
1811e47c8fc5SChristopher J. PeBenito 	char **classes;
1812e47c8fc5SChristopher J. PeBenito 
1813e47c8fc5SChristopher J. PeBenito 	/* delete any existing entries */
18140619f0f5SStephen Smalley 	sel_remove_entries(fsi->class_dir);
1815e47c8fc5SChristopher J. PeBenito 
18160619f0f5SStephen Smalley 	rc = security_get_classes(fsi->state, &classes, &nclasses);
1817b77a493bSEric Paris 	if (rc)
1818b77a493bSEric Paris 		return rc;
1819e47c8fc5SChristopher J. PeBenito 
1820e47c8fc5SChristopher J. PeBenito 	/* +2 since classes are 1-indexed */
18210619f0f5SStephen Smalley 	fsi->last_class_ino = sel_class_to_ino(nclasses + 2);
1822e47c8fc5SChristopher J. PeBenito 
1823e47c8fc5SChristopher J. PeBenito 	for (i = 0; i < nclasses; i++) {
1824e47c8fc5SChristopher J. PeBenito 		struct dentry *class_name_dir;
1825e47c8fc5SChristopher J. PeBenito 
18260619f0f5SStephen Smalley 		class_name_dir = sel_make_dir(fsi->class_dir, classes[i],
18270619f0f5SStephen Smalley 					      &fsi->last_class_ino);
1828a1c2aa1eSAl Viro 		if (IS_ERR(class_name_dir)) {
1829a1c2aa1eSAl Viro 			rc = PTR_ERR(class_name_dir);
1830b77a493bSEric Paris 			goto out;
1831a1c2aa1eSAl Viro 		}
1832e47c8fc5SChristopher J. PeBenito 
1833e47c8fc5SChristopher J. PeBenito 		/* i+1 since class values are 1-indexed */
1834e47c8fc5SChristopher J. PeBenito 		rc = sel_make_class_dir_entries(classes[i], i + 1,
1835e47c8fc5SChristopher J. PeBenito 				class_name_dir);
1836e47c8fc5SChristopher J. PeBenito 		if (rc)
1837b77a493bSEric Paris 			goto out;
1838e47c8fc5SChristopher J. PeBenito 	}
1839b77a493bSEric Paris 	rc = 0;
1840b77a493bSEric Paris out:
1841e47c8fc5SChristopher J. PeBenito 	for (i = 0; i < nclasses; i++)
1842e47c8fc5SChristopher J. PeBenito 		kfree(classes[i]);
1843e47c8fc5SChristopher J. PeBenito 	kfree(classes);
1844e47c8fc5SChristopher J. PeBenito 	return rc;
1845e47c8fc5SChristopher J. PeBenito }
1846e47c8fc5SChristopher J. PeBenito 
18470619f0f5SStephen Smalley static int sel_make_policycap(struct selinux_fs_info *fsi)
18483bb56b25SPaul Moore {
18493bb56b25SPaul Moore 	unsigned int iter;
18503bb56b25SPaul Moore 	struct dentry *dentry = NULL;
18513bb56b25SPaul Moore 	struct inode *inode = NULL;
18523bb56b25SPaul Moore 
18530619f0f5SStephen Smalley 	sel_remove_entries(fsi->policycap_dir);
18543bb56b25SPaul Moore 
18553bb56b25SPaul Moore 	for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
18564dc2fce3SStephen Smalley 		if (iter < ARRAY_SIZE(selinux_policycap_names))
18570619f0f5SStephen Smalley 			dentry = d_alloc_name(fsi->policycap_dir,
18584dc2fce3SStephen Smalley 					      selinux_policycap_names[iter]);
18593bb56b25SPaul Moore 		else
18600619f0f5SStephen Smalley 			dentry = d_alloc_name(fsi->policycap_dir, "unknown");
18613bb56b25SPaul Moore 
18623bb56b25SPaul Moore 		if (dentry == NULL)
18633bb56b25SPaul Moore 			return -ENOMEM;
18643bb56b25SPaul Moore 
18650619f0f5SStephen Smalley 		inode = sel_make_inode(fsi->sb, S_IFREG | 0444);
18667e4237faSnixiaoming 		if (inode == NULL) {
18677e4237faSnixiaoming 			dput(dentry);
18683bb56b25SPaul Moore 			return -ENOMEM;
18697e4237faSnixiaoming 		}
18703bb56b25SPaul Moore 
18713bb56b25SPaul Moore 		inode->i_fop = &sel_policycap_ops;
18723bb56b25SPaul Moore 		inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
18733bb56b25SPaul Moore 		d_add(dentry, inode);
18743bb56b25SPaul Moore 	}
18753bb56b25SPaul Moore 
18763bb56b25SPaul Moore 	return 0;
18773bb56b25SPaul Moore }
18783bb56b25SPaul Moore 
1879a1c2aa1eSAl Viro static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
18800dd4ae51SChristopher J. PeBenito 			unsigned long *ino)
18811da177e4SLinus Torvalds {
1882a1c2aa1eSAl Viro 	struct dentry *dentry = d_alloc_name(dir, name);
18831da177e4SLinus Torvalds 	struct inode *inode;
18841da177e4SLinus Torvalds 
1885a1c2aa1eSAl Viro 	if (!dentry)
1886a1c2aa1eSAl Viro 		return ERR_PTR(-ENOMEM);
1887a1c2aa1eSAl Viro 
1888a1c2aa1eSAl Viro 	inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1889a1c2aa1eSAl Viro 	if (!inode) {
1890a1c2aa1eSAl Viro 		dput(dentry);
1891a1c2aa1eSAl Viro 		return ERR_PTR(-ENOMEM);
1892a1c2aa1eSAl Viro 	}
1893b77a493bSEric Paris 
18941da177e4SLinus Torvalds 	inode->i_op = &simple_dir_inode_operations;
18951da177e4SLinus Torvalds 	inode->i_fop = &simple_dir_operations;
18960dd4ae51SChristopher J. PeBenito 	inode->i_ino = ++(*ino);
189740e906f8SJames Morris 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
1898d8c76e6fSDave Hansen 	inc_nlink(inode);
18991da177e4SLinus Torvalds 	d_add(dentry, inode);
1900edb20fb5SJames Morris 	/* bump link count on parent directory, too */
1901ce0b16ddSDavid Howells 	inc_nlink(d_inode(dir));
1902b77a493bSEric Paris 
1903a1c2aa1eSAl Viro 	return dentry;
19041da177e4SLinus Torvalds }
19051da177e4SLinus Torvalds 
19060619f0f5SStephen Smalley #define NULL_FILE_NAME "null"
19070619f0f5SStephen Smalley 
19081da177e4SLinus Torvalds static int sel_fill_super(struct super_block *sb, void *data, int silent)
19091da177e4SLinus Torvalds {
19100619f0f5SStephen Smalley 	struct selinux_fs_info *fsi;
19111da177e4SLinus Torvalds 	int ret;
19121da177e4SLinus Torvalds 	struct dentry *dentry;
1913a1c2aa1eSAl Viro 	struct inode *inode;
19141da177e4SLinus Torvalds 	struct inode_security_struct *isec;
19151da177e4SLinus Torvalds 
1916cda37124SEric Biggers 	static const struct tree_descr selinux_files[] = {
19171da177e4SLinus Torvalds 		[SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
19181da177e4SLinus Torvalds 		[SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
1919ce9982d0SStephen Smalley 		[SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
19201da177e4SLinus Torvalds 		[SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
19211da177e4SLinus Torvalds 		[SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
19221da177e4SLinus Torvalds 		[SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
19231da177e4SLinus Torvalds 		[SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
19241da177e4SLinus Torvalds 		[SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
19251da177e4SLinus Torvalds 		[SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
19261da177e4SLinus Torvalds 		[SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
19271da177e4SLinus Torvalds 		[SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
19281da177e4SLinus Torvalds 		[SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
19291da177e4SLinus Torvalds 		[SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
19303f12070eSEric Paris 		[SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
19313f12070eSEric Paris 		[SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
193211904167SKaiGai Kohei 		[SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO},
193372e8c859SEric Paris 		[SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO},
1934f9df6458SAndrew Perepechko 		[SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops,
1935f9df6458SAndrew Perepechko 					S_IWUGO},
19361da177e4SLinus Torvalds 		/* last one */ {""}
19371da177e4SLinus Torvalds 	};
19380619f0f5SStephen Smalley 
19390619f0f5SStephen Smalley 	ret = selinux_fs_info_create(sb);
19400619f0f5SStephen Smalley 	if (ret)
19410619f0f5SStephen Smalley 		goto err;
19420619f0f5SStephen Smalley 
19431da177e4SLinus Torvalds 	ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
19441da177e4SLinus Torvalds 	if (ret)
1945161ce45aSJames Morris 		goto err;
19461da177e4SLinus Torvalds 
19470619f0f5SStephen Smalley 	fsi = sb->s_fs_info;
19480619f0f5SStephen Smalley 	fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino);
19490619f0f5SStephen Smalley 	if (IS_ERR(fsi->bool_dir)) {
19500619f0f5SStephen Smalley 		ret = PTR_ERR(fsi->bool_dir);
19510619f0f5SStephen Smalley 		fsi->bool_dir = NULL;
1952161ce45aSJames Morris 		goto err;
1953a1c2aa1eSAl Viro 	}
19541da177e4SLinus Torvalds 
1955b77a493bSEric Paris 	ret = -ENOMEM;
19561da177e4SLinus Torvalds 	dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
1957b77a493bSEric Paris 	if (!dentry)
1958161ce45aSJames Morris 		goto err;
19591da177e4SLinus Torvalds 
1960161ce45aSJames Morris 	ret = -ENOMEM;
1961b77a493bSEric Paris 	inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
19627e4237faSnixiaoming 	if (!inode) {
19637e4237faSnixiaoming 		dput(dentry);
1964161ce45aSJames Morris 		goto err;
19657e4237faSnixiaoming 	}
1966b77a493bSEric Paris 
19670619f0f5SStephen Smalley 	inode->i_ino = ++fsi->last_ino;
19681da177e4SLinus Torvalds 	isec = (struct inode_security_struct *)inode->i_security;
19691da177e4SLinus Torvalds 	isec->sid = SECINITSID_DEVNULL;
19701da177e4SLinus Torvalds 	isec->sclass = SECCLASS_CHR_FILE;
197142059112SAndreas Gruenbacher 	isec->initialized = LABEL_INITIALIZED;
19721da177e4SLinus Torvalds 
19731da177e4SLinus Torvalds 	init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
19741da177e4SLinus Torvalds 	d_add(dentry, inode);
19751da177e4SLinus Torvalds 
19760619f0f5SStephen Smalley 	dentry = sel_make_dir(sb->s_root, "avc", &fsi->last_ino);
1977a1c2aa1eSAl Viro 	if (IS_ERR(dentry)) {
1978a1c2aa1eSAl Viro 		ret = PTR_ERR(dentry);
1979161ce45aSJames Morris 		goto err;
1980a1c2aa1eSAl Viro 	}
19811da177e4SLinus Torvalds 
19821da177e4SLinus Torvalds 	ret = sel_make_avc_files(dentry);
19831da177e4SLinus Torvalds 	if (ret)
1984161ce45aSJames Morris 		goto err;
1985f0ee2e46SJames Carter 
19860619f0f5SStephen Smalley 	dentry = sel_make_dir(sb->s_root, "initial_contexts", &fsi->last_ino);
1987a1c2aa1eSAl Viro 	if (IS_ERR(dentry)) {
1988a1c2aa1eSAl Viro 		ret = PTR_ERR(dentry);
1989f0ee2e46SJames Carter 		goto err;
1990a1c2aa1eSAl Viro 	}
1991f0ee2e46SJames Carter 
1992f0ee2e46SJames Carter 	ret = sel_make_initcon_files(dentry);
1993f0ee2e46SJames Carter 	if (ret)
1994f0ee2e46SJames Carter 		goto err;
1995f0ee2e46SJames Carter 
19960619f0f5SStephen Smalley 	fsi->class_dir = sel_make_dir(sb->s_root, "class", &fsi->last_ino);
19970619f0f5SStephen Smalley 	if (IS_ERR(fsi->class_dir)) {
19980619f0f5SStephen Smalley 		ret = PTR_ERR(fsi->class_dir);
19990619f0f5SStephen Smalley 		fsi->class_dir = NULL;
2000e47c8fc5SChristopher J. PeBenito 		goto err;
2001a1c2aa1eSAl Viro 	}
2002e47c8fc5SChristopher J. PeBenito 
20030619f0f5SStephen Smalley 	fsi->policycap_dir = sel_make_dir(sb->s_root, "policy_capabilities",
20040619f0f5SStephen Smalley 					  &fsi->last_ino);
20050619f0f5SStephen Smalley 	if (IS_ERR(fsi->policycap_dir)) {
20060619f0f5SStephen Smalley 		ret = PTR_ERR(fsi->policycap_dir);
20070619f0f5SStephen Smalley 		fsi->policycap_dir = NULL;
2008e47c8fc5SChristopher J. PeBenito 		goto err;
2009a1c2aa1eSAl Viro 	}
20100619f0f5SStephen Smalley 
20110619f0f5SStephen Smalley 	ret = sel_make_policy_nodes(fsi);
20120619f0f5SStephen Smalley 	if (ret)
20130619f0f5SStephen Smalley 		goto err;
2014b77a493bSEric Paris 	return 0;
2015161ce45aSJames Morris err:
2016f8b69a5fSpeter enderborg 	pr_err("SELinux: %s:  failed while creating inodes\n",
2017744ba35eSEric Paris 		__func__);
20180619f0f5SStephen Smalley 
20190619f0f5SStephen Smalley 	selinux_fs_info_free(sb);
20200619f0f5SStephen Smalley 
2021b77a493bSEric Paris 	return ret;
20221da177e4SLinus Torvalds }
20231da177e4SLinus Torvalds 
2024fc14f2feSAl Viro static struct dentry *sel_mount(struct file_system_type *fs_type,
2025fc14f2feSAl Viro 		      int flags, const char *dev_name, void *data)
20261da177e4SLinus Torvalds {
2027fc14f2feSAl Viro 	return mount_single(fs_type, flags, data, sel_fill_super);
20281da177e4SLinus Torvalds }
20291da177e4SLinus Torvalds 
20300619f0f5SStephen Smalley static void sel_kill_sb(struct super_block *sb)
20310619f0f5SStephen Smalley {
20320619f0f5SStephen Smalley 	selinux_fs_info_free(sb);
20330619f0f5SStephen Smalley 	kill_litter_super(sb);
20340619f0f5SStephen Smalley }
20350619f0f5SStephen Smalley 
20361da177e4SLinus Torvalds static struct file_system_type sel_fs_type = {
20371da177e4SLinus Torvalds 	.name		= "selinuxfs",
2038fc14f2feSAl Viro 	.mount		= sel_mount,
20390619f0f5SStephen Smalley 	.kill_sb	= sel_kill_sb,
20401da177e4SLinus Torvalds };
20411da177e4SLinus Torvalds 
20421da177e4SLinus Torvalds struct vfsmount *selinuxfs_mount;
20430619f0f5SStephen Smalley struct path selinux_null;
20441da177e4SLinus Torvalds 
20451da177e4SLinus Torvalds static int __init init_sel_fs(void)
20461da177e4SLinus Torvalds {
20470619f0f5SStephen Smalley 	struct qstr null_name = QSTR_INIT(NULL_FILE_NAME,
20480619f0f5SStephen Smalley 					  sizeof(NULL_FILE_NAME)-1);
20491da177e4SLinus Torvalds 	int err;
20501da177e4SLinus Torvalds 
20511da177e4SLinus Torvalds 	if (!selinux_enabled)
20521da177e4SLinus Torvalds 		return 0;
20537a627e3bSGreg Kroah-Hartman 
2054f9bb4882SEric W. Biederman 	err = sysfs_create_mount_point(fs_kobj, "selinux");
2055f9bb4882SEric W. Biederman 	if (err)
2056f9bb4882SEric W. Biederman 		return err;
20577a627e3bSGreg Kroah-Hartman 
20581da177e4SLinus Torvalds 	err = register_filesystem(&sel_fs_type);
20597a627e3bSGreg Kroah-Hartman 	if (err) {
2060f9bb4882SEric W. Biederman 		sysfs_remove_mount_point(fs_kobj, "selinux");
2061b77a493bSEric Paris 		return err;
20627a627e3bSGreg Kroah-Hartman 	}
2063b77a493bSEric Paris 
2064765927b2SAl Viro 	selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type);
20651da177e4SLinus Torvalds 	if (IS_ERR(selinuxfs_mount)) {
2066f8b69a5fSpeter enderborg 		pr_err("selinuxfs:  could not mount!\n");
20671da177e4SLinus Torvalds 		err = PTR_ERR(selinuxfs_mount);
20681da177e4SLinus Torvalds 		selinuxfs_mount = NULL;
20691da177e4SLinus Torvalds 	}
20700619f0f5SStephen Smalley 	selinux_null.dentry = d_hash_and_lookup(selinux_null.mnt->mnt_root,
20710619f0f5SStephen Smalley 						&null_name);
20720619f0f5SStephen Smalley 	if (IS_ERR(selinux_null.dentry)) {
20730619f0f5SStephen Smalley 		pr_err("selinuxfs:  could not lookup null!\n");
20740619f0f5SStephen Smalley 		err = PTR_ERR(selinux_null.dentry);
20750619f0f5SStephen Smalley 		selinux_null.dentry = NULL;
20760619f0f5SStephen Smalley 	}
2077b77a493bSEric Paris 
20781da177e4SLinus Torvalds 	return err;
20791da177e4SLinus Torvalds }
20801da177e4SLinus Torvalds 
20811da177e4SLinus Torvalds __initcall(init_sel_fs);
20821da177e4SLinus Torvalds 
20831da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
20841da177e4SLinus Torvalds void exit_sel_fs(void)
20851da177e4SLinus Torvalds {
2086f9bb4882SEric W. Biederman 	sysfs_remove_mount_point(fs_kobj, "selinux");
2087fd40ffc7SStephen Smalley 	dput(selinux_null.dentry);
2088423e0ab0STim Chen 	kern_unmount(selinuxfs_mount);
20891da177e4SLinus Torvalds 	unregister_filesystem(&sel_fs_type);
20901da177e4SLinus Torvalds }
20911da177e4SLinus Torvalds #endif
2092