xref: /openbmc/linux/security/selinux/selinuxfs.c (revision d141136f)
11da177e4SLinus Torvalds /* Updated: Karl MacMillan <kmacmillan@tresys.com>
21da177e4SLinus Torvalds  *
31da177e4SLinus Torvalds  *	Added conditional policy language extensions
41da177e4SLinus Torvalds  *
582c21bfaSPaul Moore  *  Updated: Hewlett-Packard <paul@paul-moore.com>
63bb56b25SPaul Moore  *
73bb56b25SPaul Moore  *	Added support for the policy capability bitmap
83bb56b25SPaul Moore  *
93bb56b25SPaul Moore  * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
101da177e4SLinus Torvalds  * Copyright (C) 2003 - 2004 Tresys Technology, LLC
111da177e4SLinus Torvalds  * Copyright (C) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
121da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or modify
131da177e4SLinus Torvalds  *	it under the terms of the GNU General Public License as published by
141da177e4SLinus Torvalds  *	the Free Software Foundation, version 2.
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #include <linux/kernel.h>
181da177e4SLinus Torvalds #include <linux/pagemap.h>
191da177e4SLinus Torvalds #include <linux/slab.h>
201da177e4SLinus Torvalds #include <linux/vmalloc.h>
211da177e4SLinus Torvalds #include <linux/fs.h>
220619f0f5SStephen Smalley #include <linux/mount.h>
23bb003079SIngo Molnar #include <linux/mutex.h>
241da177e4SLinus Torvalds #include <linux/init.h>
251da177e4SLinus Torvalds #include <linux/string.h>
261da177e4SLinus Torvalds #include <linux/security.h>
271da177e4SLinus Torvalds #include <linux/major.h>
281da177e4SLinus Torvalds #include <linux/seq_file.h>
291da177e4SLinus Torvalds #include <linux/percpu.h>
30af601e46SSteve Grubb #include <linux/audit.h>
31f5269710SEric Paris #include <linux/uaccess.h>
327a627e3bSGreg Kroah-Hartman #include <linux/kobject.h>
330f7e4c33SKohei Kaigai #include <linux/ctype.h>
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds /* selinuxfs pseudo filesystem for exporting the security policy API.
361da177e4SLinus Torvalds    Based on the proc code and the fs/nfsd/nfsctl.c code. */
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds #include "flask.h"
391da177e4SLinus Torvalds #include "avc.h"
401da177e4SLinus Torvalds #include "avc_ss.h"
411da177e4SLinus Torvalds #include "security.h"
421da177e4SLinus Torvalds #include "objsec.h"
431da177e4SLinus Torvalds #include "conditional.h"
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds enum sel_inos {
461da177e4SLinus Torvalds 	SEL_ROOT_INO = 2,
471da177e4SLinus Torvalds 	SEL_LOAD,	/* load policy */
481da177e4SLinus Torvalds 	SEL_ENFORCE,	/* get or set enforcing status */
491da177e4SLinus Torvalds 	SEL_CONTEXT,	/* validate context */
501da177e4SLinus Torvalds 	SEL_ACCESS,	/* compute access decision */
511da177e4SLinus Torvalds 	SEL_CREATE,	/* compute create labeling decision */
521da177e4SLinus Torvalds 	SEL_RELABEL,	/* compute relabeling decision */
531da177e4SLinus Torvalds 	SEL_USER,	/* compute reachable user contexts */
541da177e4SLinus Torvalds 	SEL_POLICYVERS,	/* return policy version for this kernel */
551da177e4SLinus Torvalds 	SEL_COMMIT_BOOLS, /* commit new boolean values */
561da177e4SLinus Torvalds 	SEL_MLS,	/* return if MLS policy is enabled */
571da177e4SLinus Torvalds 	SEL_DISABLE,	/* disable SELinux until next reboot */
581da177e4SLinus Torvalds 	SEL_MEMBER,	/* compute polyinstantiation membership decision */
591da177e4SLinus Torvalds 	SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
604e5ab4cbSJames Morris 	SEL_COMPAT_NET,	/* whether to use old compat network packet controls */
613f12070eSEric Paris 	SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */
623f12070eSEric Paris 	SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */
6311904167SKaiGai Kohei 	SEL_STATUS,	/* export current status using mmap() */
64cee74f47SEric Paris 	SEL_POLICY,	/* allow userspace to read the in kernel policy */
65f9df6458SAndrew Perepechko 	SEL_VALIDATE_TRANS, /* compute validatetrans decision */
666174eafcSJames Carter 	SEL_INO_NEXT,	/* The next inode number to use */
671da177e4SLinus Torvalds };
681da177e4SLinus Torvalds 
690619f0f5SStephen Smalley struct selinux_fs_info {
700619f0f5SStephen Smalley 	struct dentry *bool_dir;
710619f0f5SStephen Smalley 	unsigned int bool_num;
720619f0f5SStephen Smalley 	char **bool_pending_names;
730619f0f5SStephen Smalley 	unsigned int *bool_pending_values;
740619f0f5SStephen Smalley 	struct dentry *class_dir;
750619f0f5SStephen Smalley 	unsigned long last_class_ino;
760619f0f5SStephen Smalley 	bool policy_opened;
770619f0f5SStephen Smalley 	struct dentry *policycap_dir;
780619f0f5SStephen Smalley 	struct mutex mutex;
790619f0f5SStephen Smalley 	unsigned long last_ino;
800619f0f5SStephen Smalley 	struct selinux_state *state;
810619f0f5SStephen Smalley 	struct super_block *sb;
820619f0f5SStephen Smalley };
830619f0f5SStephen Smalley 
840619f0f5SStephen Smalley static int selinux_fs_info_create(struct super_block *sb)
850619f0f5SStephen Smalley {
860619f0f5SStephen Smalley 	struct selinux_fs_info *fsi;
870619f0f5SStephen Smalley 
880619f0f5SStephen Smalley 	fsi = kzalloc(sizeof(*fsi), GFP_KERNEL);
890619f0f5SStephen Smalley 	if (!fsi)
900619f0f5SStephen Smalley 		return -ENOMEM;
910619f0f5SStephen Smalley 
920619f0f5SStephen Smalley 	mutex_init(&fsi->mutex);
930619f0f5SStephen Smalley 	fsi->last_ino = SEL_INO_NEXT - 1;
940619f0f5SStephen Smalley 	fsi->state = &selinux_state;
950619f0f5SStephen Smalley 	fsi->sb = sb;
960619f0f5SStephen Smalley 	sb->s_fs_info = fsi;
970619f0f5SStephen Smalley 	return 0;
980619f0f5SStephen Smalley }
990619f0f5SStephen Smalley 
1000619f0f5SStephen Smalley static void selinux_fs_info_free(struct super_block *sb)
1010619f0f5SStephen Smalley {
1020619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = sb->s_fs_info;
1030619f0f5SStephen Smalley 	int i;
1040619f0f5SStephen Smalley 
1050619f0f5SStephen Smalley 	if (fsi) {
1060619f0f5SStephen Smalley 		for (i = 0; i < fsi->bool_num; i++)
1070619f0f5SStephen Smalley 			kfree(fsi->bool_pending_names[i]);
1080619f0f5SStephen Smalley 		kfree(fsi->bool_pending_names);
1090619f0f5SStephen Smalley 		kfree(fsi->bool_pending_values);
1100619f0f5SStephen Smalley 	}
1110619f0f5SStephen Smalley 	kfree(sb->s_fs_info);
1120619f0f5SStephen Smalley 	sb->s_fs_info = NULL;
1130619f0f5SStephen Smalley }
1146174eafcSJames Carter 
115f0ee2e46SJames Carter #define SEL_INITCON_INO_OFFSET		0x01000000
116bce34bc0SJames Carter #define SEL_BOOL_INO_OFFSET		0x02000000
117e47c8fc5SChristopher J. PeBenito #define SEL_CLASS_INO_OFFSET		0x04000000
1183bb56b25SPaul Moore #define SEL_POLICYCAP_INO_OFFSET	0x08000000
119f0ee2e46SJames Carter #define SEL_INO_MASK			0x00ffffff
120f0ee2e46SJames Carter 
1211da177e4SLinus Torvalds #define TMPBUFLEN	12
1221da177e4SLinus Torvalds static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
1231da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
1241da177e4SLinus Torvalds {
1250619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
1261da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
1271da177e4SLinus Torvalds 	ssize_t length;
1281da177e4SLinus Torvalds 
129aa8e712cSStephen Smalley 	length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
1300619f0f5SStephen Smalley 			   enforcing_enabled(fsi->state));
1311da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1321da177e4SLinus Torvalds }
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
1351da177e4SLinus Torvalds static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
1361da177e4SLinus Torvalds 				 size_t count, loff_t *ppos)
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds {
1390619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1400619f0f5SStephen Smalley 	struct selinux_state *state = fsi->state;
141b77a493bSEric Paris 	char *page = NULL;
1421da177e4SLinus Torvalds 	ssize_t length;
143aa8e712cSStephen Smalley 	int old_value, new_value;
1441da177e4SLinus Torvalds 
145bfd51626SDavi Arnaut 	if (count >= PAGE_SIZE)
1468365a719SAl Viro 		return -ENOMEM;
147b77a493bSEric Paris 
1481da177e4SLinus Torvalds 	/* No partial writes. */
149b77a493bSEric Paris 	if (*ppos != 0)
1508365a719SAl Viro 		return -EINVAL;
151b77a493bSEric Paris 
1528365a719SAl Viro 	page = memdup_user_nul(buf, count);
1538365a719SAl Viro 	if (IS_ERR(page))
1548365a719SAl Viro 		return PTR_ERR(page);
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds 	length = -EINVAL;
1571da177e4SLinus Torvalds 	if (sscanf(page, "%d", &new_value) != 1)
1581da177e4SLinus Torvalds 		goto out;
1591da177e4SLinus Torvalds 
160ea49d10eSStephen Smalley 	new_value = !!new_value;
161ea49d10eSStephen Smalley 
1620619f0f5SStephen Smalley 	old_value = enforcing_enabled(state);
163aa8e712cSStephen Smalley 	if (new_value != old_value) {
1646b6bc620SStephen Smalley 		length = avc_has_perm(&selinux_state,
1656b6bc620SStephen Smalley 				      current_sid(), SECINITSID_SECURITY,
166be0554c9SStephen Smalley 				      SECCLASS_SECURITY, SECURITY__SETENFORCE,
167be0554c9SStephen Smalley 				      NULL);
1681da177e4SLinus Torvalds 		if (length)
1691da177e4SLinus Torvalds 			goto out;
170af601e46SSteve Grubb 		audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
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;
306af601e46SSteve Grubb 		audit_log(current->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:
584af601e46SSteve Grubb 	audit_log(current->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) {
623744ba35eSEric Paris 		printk(KERN_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 
7761da177e4SLinus Torvalds static ssize_t (*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) {
959744ba35eSEric Paris 		printk(KERN_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) {
1150744ba35eSEric Paris 		printk(KERN_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);
1380b77a493bSEric Paris 		if (!inode)
1381b77a493bSEric Paris 			goto out;
1382b77a493bSEric Paris 
13831da177e4SLinus Torvalds 		ret = -ENAMETOOLONG;
1384cc1dad71SAl Viro 		len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
1385b77a493bSEric Paris 		if (len >= PAGE_SIZE)
1386b77a493bSEric Paris 			goto out;
1387b77a493bSEric Paris 
13881da177e4SLinus Torvalds 		isec = (struct inode_security_struct *)inode->i_security;
13890619f0f5SStephen Smalley 		ret = security_genfs_sid(fsi->state, "selinuxfs", page,
1390aa8e712cSStephen Smalley 					 SECCLASS_FILE, &sid);
13914262fb51SGary Tierney 		if (ret) {
1392900fde06SGary Tierney 			pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n",
1393900fde06SGary Tierney 					   page);
1394900fde06SGary Tierney 			sid = SECINITSID_SECURITY;
13954262fb51SGary Tierney 		}
13964262fb51SGary Tierney 
13971da177e4SLinus Torvalds 		isec->sid = sid;
139842059112SAndreas Gruenbacher 		isec->initialized = LABEL_INITIALIZED;
13991da177e4SLinus Torvalds 		inode->i_fop = &sel_bool_ops;
1400bce34bc0SJames Carter 		inode->i_ino = i|SEL_BOOL_INO_OFFSET;
14011da177e4SLinus Torvalds 		d_add(dentry, inode);
14021da177e4SLinus Torvalds 	}
14030619f0f5SStephen Smalley 	fsi->bool_num = num;
14040619f0f5SStephen Smalley 	fsi->bool_pending_names = names;
14050619f0f5SStephen Smalley 	fsi->bool_pending_values = values;
1406b77a493bSEric Paris 
1407b77a493bSEric Paris 	free_page((unsigned long)page);
1408b77a493bSEric Paris 	return 0;
14091da177e4SLinus Torvalds out:
14101da177e4SLinus Torvalds 	free_page((unsigned long)page);
1411b77a493bSEric Paris 
14121da177e4SLinus Torvalds 	if (names) {
14139a5f04bfSJesper Juhl 		for (i = 0; i < num; i++)
14141da177e4SLinus Torvalds 			kfree(names[i]);
14151da177e4SLinus Torvalds 		kfree(names);
14161da177e4SLinus Torvalds 	}
141720c19e41SDavi Arnaut 	kfree(values);
14180c92d7c7SChristopher J. PeBenito 	sel_remove_entries(dir);
1419b77a493bSEric Paris 
1420b77a493bSEric Paris 	return ret;
14211da177e4SLinus Torvalds }
14221da177e4SLinus Torvalds 
14231da177e4SLinus Torvalds static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
14241da177e4SLinus Torvalds 					    size_t count, loff_t *ppos)
14251da177e4SLinus Torvalds {
14266b6bc620SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
14276b6bc620SStephen Smalley 	struct selinux_state *state = fsi->state;
14281da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
14291da177e4SLinus Torvalds 	ssize_t length;
14301da177e4SLinus Torvalds 
14316b6bc620SStephen Smalley 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
14326b6bc620SStephen Smalley 			   avc_get_cache_threshold(state->avc));
14331da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
14341da177e4SLinus Torvalds }
14351da177e4SLinus Torvalds 
14361da177e4SLinus Torvalds static ssize_t sel_write_avc_cache_threshold(struct file *file,
14371da177e4SLinus Torvalds 					     const char __user *buf,
14381da177e4SLinus Torvalds 					     size_t count, loff_t *ppos)
14391da177e4SLinus Torvalds 
14401da177e4SLinus Torvalds {
14416b6bc620SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
14426b6bc620SStephen Smalley 	struct selinux_state *state = fsi->state;
14438365a719SAl Viro 	char *page;
14441da177e4SLinus Torvalds 	ssize_t ret;
1445309c5fadSHeinrich Schuchardt 	unsigned int new_value;
14461da177e4SLinus Torvalds 
14476b6bc620SStephen Smalley 	ret = avc_has_perm(&selinux_state,
14486b6bc620SStephen Smalley 			   current_sid(), SECINITSID_SECURITY,
1449be0554c9SStephen Smalley 			   SECCLASS_SECURITY, SECURITY__SETSECPARAM,
1450be0554c9SStephen Smalley 			   NULL);
14511da177e4SLinus Torvalds 	if (ret)
14528365a719SAl Viro 		return ret;
1453b77a493bSEric Paris 
1454b77a493bSEric Paris 	if (count >= PAGE_SIZE)
14558365a719SAl Viro 		return -ENOMEM;
1456b77a493bSEric Paris 
1457b77a493bSEric Paris 	/* No partial writes. */
1458b77a493bSEric Paris 	if (*ppos != 0)
14598365a719SAl Viro 		return -EINVAL;
1460b77a493bSEric Paris 
14618365a719SAl Viro 	page = memdup_user_nul(buf, count);
14628365a719SAl Viro 	if (IS_ERR(page))
14638365a719SAl Viro 		return PTR_ERR(page);
1464b77a493bSEric Paris 
1465b77a493bSEric Paris 	ret = -EINVAL;
1466b77a493bSEric Paris 	if (sscanf(page, "%u", &new_value) != 1)
1467b77a493bSEric Paris 		goto out;
1468b77a493bSEric Paris 
14696b6bc620SStephen Smalley 	avc_set_cache_threshold(state->avc, new_value);
1470b77a493bSEric Paris 
14711da177e4SLinus Torvalds 	ret = count;
14721da177e4SLinus Torvalds out:
14738365a719SAl Viro 	kfree(page);
14741da177e4SLinus Torvalds 	return ret;
14751da177e4SLinus Torvalds }
14761da177e4SLinus Torvalds 
14771da177e4SLinus Torvalds static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
14781da177e4SLinus Torvalds 				       size_t count, loff_t *ppos)
14791da177e4SLinus Torvalds {
14806b6bc620SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
14816b6bc620SStephen Smalley 	struct selinux_state *state = fsi->state;
14821da177e4SLinus Torvalds 	char *page;
1483b77a493bSEric Paris 	ssize_t length;
14841da177e4SLinus Torvalds 
14851da177e4SLinus Torvalds 	page = (char *)__get_free_page(GFP_KERNEL);
1486b77a493bSEric Paris 	if (!page)
1487b77a493bSEric Paris 		return -ENOMEM;
1488b77a493bSEric Paris 
14896b6bc620SStephen Smalley 	length = avc_get_hash_stats(state->avc, page);
1490b77a493bSEric Paris 	if (length >= 0)
1491b77a493bSEric Paris 		length = simple_read_from_buffer(buf, count, ppos, page, length);
14921da177e4SLinus Torvalds 	free_page((unsigned long)page);
1493b77a493bSEric Paris 
1494b77a493bSEric Paris 	return length;
14951da177e4SLinus Torvalds }
14961da177e4SLinus Torvalds 
14979c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_threshold_ops = {
14981da177e4SLinus Torvalds 	.read		= sel_read_avc_cache_threshold,
14991da177e4SLinus Torvalds 	.write		= sel_write_avc_cache_threshold,
150057a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
15011da177e4SLinus Torvalds };
15021da177e4SLinus Torvalds 
15039c2e08c5SArjan van de Ven static const struct file_operations sel_avc_hash_stats_ops = {
15041da177e4SLinus Torvalds 	.read		= sel_read_avc_hash_stats,
150557a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
15061da177e4SLinus Torvalds };
15071da177e4SLinus Torvalds 
15081da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
15091da177e4SLinus Torvalds static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
15101da177e4SLinus Torvalds {
15111da177e4SLinus Torvalds 	int cpu;
15121da177e4SLinus Torvalds 
15134f4b6c1aSRusty Russell 	for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
15141da177e4SLinus Torvalds 		if (!cpu_possible(cpu))
15151da177e4SLinus Torvalds 			continue;
15161da177e4SLinus Torvalds 		*idx = cpu + 1;
15171da177e4SLinus Torvalds 		return &per_cpu(avc_cache_stats, cpu);
15181da177e4SLinus Torvalds 	}
15191da177e4SLinus Torvalds 	return NULL;
15201da177e4SLinus Torvalds }
15211da177e4SLinus Torvalds 
15221da177e4SLinus Torvalds static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
15231da177e4SLinus Torvalds {
15241da177e4SLinus Torvalds 	loff_t n = *pos - 1;
15251da177e4SLinus Torvalds 
15261da177e4SLinus Torvalds 	if (*pos == 0)
15271da177e4SLinus Torvalds 		return SEQ_START_TOKEN;
15281da177e4SLinus Torvalds 
15291da177e4SLinus Torvalds 	return sel_avc_get_stat_idx(&n);
15301da177e4SLinus Torvalds }
15311da177e4SLinus Torvalds 
15321da177e4SLinus Torvalds static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
15331da177e4SLinus Torvalds {
15341da177e4SLinus Torvalds 	return sel_avc_get_stat_idx(pos);
15351da177e4SLinus Torvalds }
15361da177e4SLinus Torvalds 
15371da177e4SLinus Torvalds static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
15381da177e4SLinus Torvalds {
15391da177e4SLinus Torvalds 	struct avc_cache_stats *st = v;
15401da177e4SLinus Torvalds 
1541710a0647SMarkus Elfring 	if (v == SEQ_START_TOKEN) {
1542710a0647SMarkus Elfring 		seq_puts(seq,
1543710a0647SMarkus Elfring 			 "lookups hits misses allocations reclaims frees\n");
1544710a0647SMarkus Elfring 	} else {
1545257313b2SLinus Torvalds 		unsigned int lookups = st->lookups;
1546257313b2SLinus Torvalds 		unsigned int misses = st->misses;
1547257313b2SLinus Torvalds 		unsigned int hits = lookups - misses;
1548257313b2SLinus Torvalds 		seq_printf(seq, "%u %u %u %u %u %u\n", lookups,
1549257313b2SLinus Torvalds 			   hits, misses, st->allocations,
15501da177e4SLinus Torvalds 			   st->reclaims, st->frees);
1551257313b2SLinus Torvalds 	}
15521da177e4SLinus Torvalds 	return 0;
15531da177e4SLinus Torvalds }
15541da177e4SLinus Torvalds 
15551da177e4SLinus Torvalds static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
15561da177e4SLinus Torvalds { }
15571da177e4SLinus Torvalds 
15581996a109SJan Engelhardt static const struct seq_operations sel_avc_cache_stats_seq_ops = {
15591da177e4SLinus Torvalds 	.start		= sel_avc_stats_seq_start,
15601da177e4SLinus Torvalds 	.next		= sel_avc_stats_seq_next,
15611da177e4SLinus Torvalds 	.show		= sel_avc_stats_seq_show,
15621da177e4SLinus Torvalds 	.stop		= sel_avc_stats_seq_stop,
15631da177e4SLinus Torvalds };
15641da177e4SLinus Torvalds 
15651da177e4SLinus Torvalds static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
15661da177e4SLinus Torvalds {
15671da177e4SLinus Torvalds 	return seq_open(file, &sel_avc_cache_stats_seq_ops);
15681da177e4SLinus Torvalds }
15691da177e4SLinus Torvalds 
15709c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_stats_ops = {
15711da177e4SLinus Torvalds 	.open		= sel_open_avc_cache_stats,
15721da177e4SLinus Torvalds 	.read		= seq_read,
15731da177e4SLinus Torvalds 	.llseek		= seq_lseek,
15741da177e4SLinus Torvalds 	.release	= seq_release,
15751da177e4SLinus Torvalds };
15761da177e4SLinus Torvalds #endif
15771da177e4SLinus Torvalds 
15781da177e4SLinus Torvalds static int sel_make_avc_files(struct dentry *dir)
15791da177e4SLinus Torvalds {
15800619f0f5SStephen Smalley 	struct super_block *sb = dir->d_sb;
15810619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = sb->s_fs_info;
1582b77a493bSEric Paris 	int i;
1583cda37124SEric Biggers 	static const struct tree_descr files[] = {
15841da177e4SLinus Torvalds 		{ "cache_threshold",
15851da177e4SLinus Torvalds 		  &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
15861da177e4SLinus Torvalds 		{ "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
15871da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
15881da177e4SLinus Torvalds 		{ "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
15891da177e4SLinus Torvalds #endif
15901da177e4SLinus Torvalds 	};
15911da177e4SLinus Torvalds 
15926e20a64aSNicolas Kaiser 	for (i = 0; i < ARRAY_SIZE(files); i++) {
15931da177e4SLinus Torvalds 		struct inode *inode;
15941da177e4SLinus Torvalds 		struct dentry *dentry;
15951da177e4SLinus Torvalds 
15961da177e4SLinus Torvalds 		dentry = d_alloc_name(dir, files[i].name);
1597b77a493bSEric Paris 		if (!dentry)
1598b77a493bSEric Paris 			return -ENOMEM;
15991da177e4SLinus Torvalds 
16001da177e4SLinus Torvalds 		inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1601b77a493bSEric Paris 		if (!inode)
1602b77a493bSEric Paris 			return -ENOMEM;
1603b77a493bSEric Paris 
16041da177e4SLinus Torvalds 		inode->i_fop = files[i].ops;
16050619f0f5SStephen Smalley 		inode->i_ino = ++fsi->last_ino;
16061da177e4SLinus Torvalds 		d_add(dentry, inode);
16071da177e4SLinus Torvalds 	}
1608b77a493bSEric Paris 
1609b77a493bSEric Paris 	return 0;
16101da177e4SLinus Torvalds }
16111da177e4SLinus Torvalds 
1612f0ee2e46SJames Carter static ssize_t sel_read_initcon(struct file *file, char __user *buf,
1613f0ee2e46SJames Carter 				size_t count, loff_t *ppos)
1614f0ee2e46SJames Carter {
16150619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1616f0ee2e46SJames Carter 	char *con;
1617f0ee2e46SJames Carter 	u32 sid, len;
1618f0ee2e46SJames Carter 	ssize_t ret;
1619f0ee2e46SJames Carter 
1620496ad9aaSAl Viro 	sid = file_inode(file)->i_ino&SEL_INO_MASK;
16210619f0f5SStephen Smalley 	ret = security_sid_to_context(fsi->state, sid, &con, &len);
1622b77a493bSEric Paris 	if (ret)
1623f0ee2e46SJames Carter 		return ret;
1624f0ee2e46SJames Carter 
1625f0ee2e46SJames Carter 	ret = simple_read_from_buffer(buf, count, ppos, con, len);
1626f0ee2e46SJames Carter 	kfree(con);
1627f0ee2e46SJames Carter 	return ret;
1628f0ee2e46SJames Carter }
1629f0ee2e46SJames Carter 
1630f0ee2e46SJames Carter static const struct file_operations sel_initcon_ops = {
1631f0ee2e46SJames Carter 	.read		= sel_read_initcon,
163257a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
1633f0ee2e46SJames Carter };
1634f0ee2e46SJames Carter 
1635f0ee2e46SJames Carter static int sel_make_initcon_files(struct dentry *dir)
1636f0ee2e46SJames Carter {
1637b77a493bSEric Paris 	int i;
1638f0ee2e46SJames Carter 
1639f0ee2e46SJames Carter 	for (i = 1; i <= SECINITSID_NUM; i++) {
1640f0ee2e46SJames Carter 		struct inode *inode;
1641f0ee2e46SJames Carter 		struct dentry *dentry;
1642f0ee2e46SJames Carter 		dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
1643b77a493bSEric Paris 		if (!dentry)
1644b77a493bSEric Paris 			return -ENOMEM;
1645f0ee2e46SJames Carter 
1646f0ee2e46SJames Carter 		inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1647b77a493bSEric Paris 		if (!inode)
1648b77a493bSEric Paris 			return -ENOMEM;
1649b77a493bSEric Paris 
1650f0ee2e46SJames Carter 		inode->i_fop = &sel_initcon_ops;
1651f0ee2e46SJames Carter 		inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1652f0ee2e46SJames Carter 		d_add(dentry, inode);
1653f0ee2e46SJames Carter 	}
1654b77a493bSEric Paris 
1655b77a493bSEric Paris 	return 0;
1656f0ee2e46SJames Carter }
1657f0ee2e46SJames Carter 
1658e47c8fc5SChristopher J. PeBenito static inline unsigned long sel_class_to_ino(u16 class)
1659e47c8fc5SChristopher J. PeBenito {
1660e47c8fc5SChristopher J. PeBenito 	return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1661e47c8fc5SChristopher J. PeBenito }
1662e47c8fc5SChristopher J. PeBenito 
1663e47c8fc5SChristopher J. PeBenito static inline u16 sel_ino_to_class(unsigned long ino)
1664e47c8fc5SChristopher J. PeBenito {
166592ae9e82SEric Paris 	return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1);
1666e47c8fc5SChristopher J. PeBenito }
1667e47c8fc5SChristopher J. PeBenito 
1668e47c8fc5SChristopher J. PeBenito static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1669e47c8fc5SChristopher J. PeBenito {
1670e47c8fc5SChristopher J. PeBenito 	return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1671e47c8fc5SChristopher J. PeBenito }
1672e47c8fc5SChristopher J. PeBenito 
1673e47c8fc5SChristopher J. PeBenito static inline u32 sel_ino_to_perm(unsigned long ino)
1674e47c8fc5SChristopher J. PeBenito {
1675e47c8fc5SChristopher J. PeBenito 	return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1676e47c8fc5SChristopher J. PeBenito }
1677e47c8fc5SChristopher J. PeBenito 
1678e47c8fc5SChristopher J. PeBenito static ssize_t sel_read_class(struct file *file, char __user *buf,
1679e47c8fc5SChristopher J. PeBenito 				size_t count, loff_t *ppos)
1680e47c8fc5SChristopher J. PeBenito {
1681496ad9aaSAl Viro 	unsigned long ino = file_inode(file)->i_ino;
1682cc1dad71SAl Viro 	char res[TMPBUFLEN];
1683cc1dad71SAl Viro 	ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_class(ino));
1684cc1dad71SAl Viro 	return simple_read_from_buffer(buf, count, ppos, res, len);
1685e47c8fc5SChristopher J. PeBenito }
1686e47c8fc5SChristopher J. PeBenito 
1687e47c8fc5SChristopher J. PeBenito static const struct file_operations sel_class_ops = {
1688e47c8fc5SChristopher J. PeBenito 	.read		= sel_read_class,
168957a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
1690e47c8fc5SChristopher J. PeBenito };
1691e47c8fc5SChristopher J. PeBenito 
1692e47c8fc5SChristopher J. PeBenito static ssize_t sel_read_perm(struct file *file, char __user *buf,
1693e47c8fc5SChristopher J. PeBenito 				size_t count, loff_t *ppos)
1694e47c8fc5SChristopher J. PeBenito {
1695496ad9aaSAl Viro 	unsigned long ino = file_inode(file)->i_ino;
1696cc1dad71SAl Viro 	char res[TMPBUFLEN];
1697cc1dad71SAl Viro 	ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino));
1698cc1dad71SAl Viro 	return simple_read_from_buffer(buf, count, ppos, res, len);
1699e47c8fc5SChristopher J. PeBenito }
1700e47c8fc5SChristopher J. PeBenito 
1701e47c8fc5SChristopher J. PeBenito static const struct file_operations sel_perm_ops = {
1702e47c8fc5SChristopher J. PeBenito 	.read		= sel_read_perm,
170357a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
1704e47c8fc5SChristopher J. PeBenito };
1705e47c8fc5SChristopher J. PeBenito 
17063bb56b25SPaul Moore static ssize_t sel_read_policycap(struct file *file, char __user *buf,
17073bb56b25SPaul Moore 				  size_t count, loff_t *ppos)
17083bb56b25SPaul Moore {
17090619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
17103bb56b25SPaul Moore 	int value;
17113bb56b25SPaul Moore 	char tmpbuf[TMPBUFLEN];
17123bb56b25SPaul Moore 	ssize_t length;
1713496ad9aaSAl Viro 	unsigned long i_ino = file_inode(file)->i_ino;
17143bb56b25SPaul Moore 
17150619f0f5SStephen Smalley 	value = security_policycap_supported(fsi->state, i_ino & SEL_INO_MASK);
17163bb56b25SPaul Moore 	length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
17173bb56b25SPaul Moore 
17183bb56b25SPaul Moore 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
17193bb56b25SPaul Moore }
17203bb56b25SPaul Moore 
17213bb56b25SPaul Moore static const struct file_operations sel_policycap_ops = {
17223bb56b25SPaul Moore 	.read		= sel_read_policycap,
172357a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
17243bb56b25SPaul Moore };
17253bb56b25SPaul Moore 
1726e47c8fc5SChristopher J. PeBenito static int sel_make_perm_files(char *objclass, int classvalue,
1727e47c8fc5SChristopher J. PeBenito 				struct dentry *dir)
1728e47c8fc5SChristopher J. PeBenito {
17290619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = dir->d_sb->s_fs_info;
1730b77a493bSEric Paris 	int i, rc, nperms;
1731e47c8fc5SChristopher J. PeBenito 	char **perms;
1732e47c8fc5SChristopher J. PeBenito 
17330619f0f5SStephen Smalley 	rc = security_get_permissions(fsi->state, objclass, &perms, &nperms);
1734e47c8fc5SChristopher J. PeBenito 	if (rc)
1735b77a493bSEric Paris 		return rc;
1736e47c8fc5SChristopher J. PeBenito 
1737e47c8fc5SChristopher J. PeBenito 	for (i = 0; i < nperms; i++) {
1738e47c8fc5SChristopher J. PeBenito 		struct inode *inode;
1739e47c8fc5SChristopher J. PeBenito 		struct dentry *dentry;
1740e47c8fc5SChristopher J. PeBenito 
1741b77a493bSEric Paris 		rc = -ENOMEM;
1742e47c8fc5SChristopher J. PeBenito 		dentry = d_alloc_name(dir, perms[i]);
1743b77a493bSEric Paris 		if (!dentry)
1744b77a493bSEric Paris 			goto out;
1745e47c8fc5SChristopher J. PeBenito 
1746e47c8fc5SChristopher J. PeBenito 		rc = -ENOMEM;
1747b77a493bSEric Paris 		inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1748b77a493bSEric Paris 		if (!inode)
1749b77a493bSEric Paris 			goto out;
1750b77a493bSEric Paris 
1751e47c8fc5SChristopher J. PeBenito 		inode->i_fop = &sel_perm_ops;
1752e47c8fc5SChristopher J. PeBenito 		/* i+1 since perm values are 1-indexed */
1753e47c8fc5SChristopher J. PeBenito 		inode->i_ino = sel_perm_to_ino(classvalue, i + 1);
1754e47c8fc5SChristopher J. PeBenito 		d_add(dentry, inode);
1755e47c8fc5SChristopher J. PeBenito 	}
1756b77a493bSEric Paris 	rc = 0;
1757b77a493bSEric Paris out:
1758e47c8fc5SChristopher J. PeBenito 	for (i = 0; i < nperms; i++)
1759e47c8fc5SChristopher J. PeBenito 		kfree(perms[i]);
1760e47c8fc5SChristopher J. PeBenito 	kfree(perms);
1761e47c8fc5SChristopher J. PeBenito 	return rc;
1762e47c8fc5SChristopher J. PeBenito }
1763e47c8fc5SChristopher J. PeBenito 
1764e47c8fc5SChristopher J. PeBenito static int sel_make_class_dir_entries(char *classname, int index,
1765e47c8fc5SChristopher J. PeBenito 					struct dentry *dir)
1766e47c8fc5SChristopher J. PeBenito {
17670619f0f5SStephen Smalley 	struct super_block *sb = dir->d_sb;
17680619f0f5SStephen Smalley 	struct selinux_fs_info *fsi = sb->s_fs_info;
1769e47c8fc5SChristopher J. PeBenito 	struct dentry *dentry = NULL;
1770e47c8fc5SChristopher J. PeBenito 	struct inode *inode = NULL;
1771e47c8fc5SChristopher J. PeBenito 	int rc;
1772e47c8fc5SChristopher J. PeBenito 
1773e47c8fc5SChristopher J. PeBenito 	dentry = d_alloc_name(dir, "index");
1774b77a493bSEric Paris 	if (!dentry)
1775b77a493bSEric Paris 		return -ENOMEM;
1776e47c8fc5SChristopher J. PeBenito 
1777e47c8fc5SChristopher J. PeBenito 	inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1778b77a493bSEric Paris 	if (!inode)
1779b77a493bSEric Paris 		return -ENOMEM;
1780e47c8fc5SChristopher J. PeBenito 
1781e47c8fc5SChristopher J. PeBenito 	inode->i_fop = &sel_class_ops;
1782e47c8fc5SChristopher J. PeBenito 	inode->i_ino = sel_class_to_ino(index);
1783e47c8fc5SChristopher J. PeBenito 	d_add(dentry, inode);
1784e47c8fc5SChristopher J. PeBenito 
17850619f0f5SStephen Smalley 	dentry = sel_make_dir(dir, "perms", &fsi->last_class_ino);
1786a1c2aa1eSAl Viro 	if (IS_ERR(dentry))
1787a1c2aa1eSAl Viro 		return PTR_ERR(dentry);
1788e47c8fc5SChristopher J. PeBenito 
1789e47c8fc5SChristopher J. PeBenito 	rc = sel_make_perm_files(classname, index, dentry);
1790e47c8fc5SChristopher J. PeBenito 
1791e47c8fc5SChristopher J. PeBenito 	return rc;
1792e47c8fc5SChristopher J. PeBenito }
1793e47c8fc5SChristopher J. PeBenito 
17940619f0f5SStephen Smalley static int sel_make_classes(struct selinux_fs_info *fsi)
1795e47c8fc5SChristopher J. PeBenito {
17960619f0f5SStephen Smalley 
1797b77a493bSEric Paris 	int rc, nclasses, i;
1798e47c8fc5SChristopher J. PeBenito 	char **classes;
1799e47c8fc5SChristopher J. PeBenito 
1800e47c8fc5SChristopher J. PeBenito 	/* delete any existing entries */
18010619f0f5SStephen Smalley 	sel_remove_entries(fsi->class_dir);
1802e47c8fc5SChristopher J. PeBenito 
18030619f0f5SStephen Smalley 	rc = security_get_classes(fsi->state, &classes, &nclasses);
1804b77a493bSEric Paris 	if (rc)
1805b77a493bSEric Paris 		return rc;
1806e47c8fc5SChristopher J. PeBenito 
1807e47c8fc5SChristopher J. PeBenito 	/* +2 since classes are 1-indexed */
18080619f0f5SStephen Smalley 	fsi->last_class_ino = sel_class_to_ino(nclasses + 2);
1809e47c8fc5SChristopher J. PeBenito 
1810e47c8fc5SChristopher J. PeBenito 	for (i = 0; i < nclasses; i++) {
1811e47c8fc5SChristopher J. PeBenito 		struct dentry *class_name_dir;
1812e47c8fc5SChristopher J. PeBenito 
18130619f0f5SStephen Smalley 		class_name_dir = sel_make_dir(fsi->class_dir, classes[i],
18140619f0f5SStephen Smalley 					      &fsi->last_class_ino);
1815a1c2aa1eSAl Viro 		if (IS_ERR(class_name_dir)) {
1816a1c2aa1eSAl Viro 			rc = PTR_ERR(class_name_dir);
1817b77a493bSEric Paris 			goto out;
1818a1c2aa1eSAl Viro 		}
1819e47c8fc5SChristopher J. PeBenito 
1820e47c8fc5SChristopher J. PeBenito 		/* i+1 since class values are 1-indexed */
1821e47c8fc5SChristopher J. PeBenito 		rc = sel_make_class_dir_entries(classes[i], i + 1,
1822e47c8fc5SChristopher J. PeBenito 				class_name_dir);
1823e47c8fc5SChristopher J. PeBenito 		if (rc)
1824b77a493bSEric Paris 			goto out;
1825e47c8fc5SChristopher J. PeBenito 	}
1826b77a493bSEric Paris 	rc = 0;
1827b77a493bSEric Paris out:
1828e47c8fc5SChristopher J. PeBenito 	for (i = 0; i < nclasses; i++)
1829e47c8fc5SChristopher J. PeBenito 		kfree(classes[i]);
1830e47c8fc5SChristopher J. PeBenito 	kfree(classes);
1831e47c8fc5SChristopher J. PeBenito 	return rc;
1832e47c8fc5SChristopher J. PeBenito }
1833e47c8fc5SChristopher J. PeBenito 
18340619f0f5SStephen Smalley static int sel_make_policycap(struct selinux_fs_info *fsi)
18353bb56b25SPaul Moore {
18363bb56b25SPaul Moore 	unsigned int iter;
18373bb56b25SPaul Moore 	struct dentry *dentry = NULL;
18383bb56b25SPaul Moore 	struct inode *inode = NULL;
18393bb56b25SPaul Moore 
18400619f0f5SStephen Smalley 	sel_remove_entries(fsi->policycap_dir);
18413bb56b25SPaul Moore 
18423bb56b25SPaul Moore 	for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
18434dc2fce3SStephen Smalley 		if (iter < ARRAY_SIZE(selinux_policycap_names))
18440619f0f5SStephen Smalley 			dentry = d_alloc_name(fsi->policycap_dir,
18454dc2fce3SStephen Smalley 					      selinux_policycap_names[iter]);
18463bb56b25SPaul Moore 		else
18470619f0f5SStephen Smalley 			dentry = d_alloc_name(fsi->policycap_dir, "unknown");
18483bb56b25SPaul Moore 
18493bb56b25SPaul Moore 		if (dentry == NULL)
18503bb56b25SPaul Moore 			return -ENOMEM;
18513bb56b25SPaul Moore 
18520619f0f5SStephen Smalley 		inode = sel_make_inode(fsi->sb, S_IFREG | 0444);
18533bb56b25SPaul Moore 		if (inode == NULL)
18543bb56b25SPaul Moore 			return -ENOMEM;
18553bb56b25SPaul Moore 
18563bb56b25SPaul Moore 		inode->i_fop = &sel_policycap_ops;
18573bb56b25SPaul Moore 		inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
18583bb56b25SPaul Moore 		d_add(dentry, inode);
18593bb56b25SPaul Moore 	}
18603bb56b25SPaul Moore 
18613bb56b25SPaul Moore 	return 0;
18623bb56b25SPaul Moore }
18633bb56b25SPaul Moore 
1864a1c2aa1eSAl Viro static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
18650dd4ae51SChristopher J. PeBenito 			unsigned long *ino)
18661da177e4SLinus Torvalds {
1867a1c2aa1eSAl Viro 	struct dentry *dentry = d_alloc_name(dir, name);
18681da177e4SLinus Torvalds 	struct inode *inode;
18691da177e4SLinus Torvalds 
1870a1c2aa1eSAl Viro 	if (!dentry)
1871a1c2aa1eSAl Viro 		return ERR_PTR(-ENOMEM);
1872a1c2aa1eSAl Viro 
1873a1c2aa1eSAl Viro 	inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1874a1c2aa1eSAl Viro 	if (!inode) {
1875a1c2aa1eSAl Viro 		dput(dentry);
1876a1c2aa1eSAl Viro 		return ERR_PTR(-ENOMEM);
1877a1c2aa1eSAl Viro 	}
1878b77a493bSEric Paris 
18791da177e4SLinus Torvalds 	inode->i_op = &simple_dir_inode_operations;
18801da177e4SLinus Torvalds 	inode->i_fop = &simple_dir_operations;
18810dd4ae51SChristopher J. PeBenito 	inode->i_ino = ++(*ino);
188240e906f8SJames Morris 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
1883d8c76e6fSDave Hansen 	inc_nlink(inode);
18841da177e4SLinus Torvalds 	d_add(dentry, inode);
1885edb20fb5SJames Morris 	/* bump link count on parent directory, too */
1886ce0b16ddSDavid Howells 	inc_nlink(d_inode(dir));
1887b77a493bSEric Paris 
1888a1c2aa1eSAl Viro 	return dentry;
18891da177e4SLinus Torvalds }
18901da177e4SLinus Torvalds 
18910619f0f5SStephen Smalley #define NULL_FILE_NAME "null"
18920619f0f5SStephen Smalley 
18931da177e4SLinus Torvalds static int sel_fill_super(struct super_block *sb, void *data, int silent)
18941da177e4SLinus Torvalds {
18950619f0f5SStephen Smalley 	struct selinux_fs_info *fsi;
18961da177e4SLinus Torvalds 	int ret;
18971da177e4SLinus Torvalds 	struct dentry *dentry;
1898a1c2aa1eSAl Viro 	struct inode *inode;
18991da177e4SLinus Torvalds 	struct inode_security_struct *isec;
19001da177e4SLinus Torvalds 
1901cda37124SEric Biggers 	static const struct tree_descr selinux_files[] = {
19021da177e4SLinus Torvalds 		[SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
19031da177e4SLinus Torvalds 		[SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
1904ce9982d0SStephen Smalley 		[SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
19051da177e4SLinus Torvalds 		[SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
19061da177e4SLinus Torvalds 		[SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
19071da177e4SLinus Torvalds 		[SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
19081da177e4SLinus Torvalds 		[SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
19091da177e4SLinus Torvalds 		[SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
19101da177e4SLinus Torvalds 		[SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
19111da177e4SLinus Torvalds 		[SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
19121da177e4SLinus Torvalds 		[SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
19131da177e4SLinus Torvalds 		[SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
19141da177e4SLinus Torvalds 		[SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
19153f12070eSEric Paris 		[SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
19163f12070eSEric Paris 		[SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
191711904167SKaiGai Kohei 		[SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO},
191872e8c859SEric Paris 		[SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO},
1919f9df6458SAndrew Perepechko 		[SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops,
1920f9df6458SAndrew Perepechko 					S_IWUGO},
19211da177e4SLinus Torvalds 		/* last one */ {""}
19221da177e4SLinus Torvalds 	};
19230619f0f5SStephen Smalley 
19240619f0f5SStephen Smalley 	ret = selinux_fs_info_create(sb);
19250619f0f5SStephen Smalley 	if (ret)
19260619f0f5SStephen Smalley 		goto err;
19270619f0f5SStephen Smalley 
19281da177e4SLinus Torvalds 	ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
19291da177e4SLinus Torvalds 	if (ret)
1930161ce45aSJames Morris 		goto err;
19311da177e4SLinus Torvalds 
19320619f0f5SStephen Smalley 	fsi = sb->s_fs_info;
19330619f0f5SStephen Smalley 	fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino);
19340619f0f5SStephen Smalley 	if (IS_ERR(fsi->bool_dir)) {
19350619f0f5SStephen Smalley 		ret = PTR_ERR(fsi->bool_dir);
19360619f0f5SStephen Smalley 		fsi->bool_dir = NULL;
1937161ce45aSJames Morris 		goto err;
1938a1c2aa1eSAl Viro 	}
19391da177e4SLinus Torvalds 
1940b77a493bSEric Paris 	ret = -ENOMEM;
19411da177e4SLinus Torvalds 	dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
1942b77a493bSEric Paris 	if (!dentry)
1943161ce45aSJames Morris 		goto err;
19441da177e4SLinus Torvalds 
1945161ce45aSJames Morris 	ret = -ENOMEM;
1946b77a493bSEric Paris 	inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
1947b77a493bSEric Paris 	if (!inode)
1948161ce45aSJames Morris 		goto err;
1949b77a493bSEric Paris 
19500619f0f5SStephen Smalley 	inode->i_ino = ++fsi->last_ino;
19511da177e4SLinus Torvalds 	isec = (struct inode_security_struct *)inode->i_security;
19521da177e4SLinus Torvalds 	isec->sid = SECINITSID_DEVNULL;
19531da177e4SLinus Torvalds 	isec->sclass = SECCLASS_CHR_FILE;
195442059112SAndreas Gruenbacher 	isec->initialized = LABEL_INITIALIZED;
19551da177e4SLinus Torvalds 
19561da177e4SLinus Torvalds 	init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
19571da177e4SLinus Torvalds 	d_add(dentry, inode);
19581da177e4SLinus Torvalds 
19590619f0f5SStephen Smalley 	dentry = sel_make_dir(sb->s_root, "avc", &fsi->last_ino);
1960a1c2aa1eSAl Viro 	if (IS_ERR(dentry)) {
1961a1c2aa1eSAl Viro 		ret = PTR_ERR(dentry);
1962161ce45aSJames Morris 		goto err;
1963a1c2aa1eSAl Viro 	}
19641da177e4SLinus Torvalds 
19651da177e4SLinus Torvalds 	ret = sel_make_avc_files(dentry);
19661da177e4SLinus Torvalds 	if (ret)
1967161ce45aSJames Morris 		goto err;
1968f0ee2e46SJames Carter 
19690619f0f5SStephen Smalley 	dentry = sel_make_dir(sb->s_root, "initial_contexts", &fsi->last_ino);
1970a1c2aa1eSAl Viro 	if (IS_ERR(dentry)) {
1971a1c2aa1eSAl Viro 		ret = PTR_ERR(dentry);
1972f0ee2e46SJames Carter 		goto err;
1973a1c2aa1eSAl Viro 	}
1974f0ee2e46SJames Carter 
1975f0ee2e46SJames Carter 	ret = sel_make_initcon_files(dentry);
1976f0ee2e46SJames Carter 	if (ret)
1977f0ee2e46SJames Carter 		goto err;
1978f0ee2e46SJames Carter 
19790619f0f5SStephen Smalley 	fsi->class_dir = sel_make_dir(sb->s_root, "class", &fsi->last_ino);
19800619f0f5SStephen Smalley 	if (IS_ERR(fsi->class_dir)) {
19810619f0f5SStephen Smalley 		ret = PTR_ERR(fsi->class_dir);
19820619f0f5SStephen Smalley 		fsi->class_dir = NULL;
1983e47c8fc5SChristopher J. PeBenito 		goto err;
1984a1c2aa1eSAl Viro 	}
1985e47c8fc5SChristopher J. PeBenito 
19860619f0f5SStephen Smalley 	fsi->policycap_dir = sel_make_dir(sb->s_root, "policy_capabilities",
19870619f0f5SStephen Smalley 					  &fsi->last_ino);
19880619f0f5SStephen Smalley 	if (IS_ERR(fsi->policycap_dir)) {
19890619f0f5SStephen Smalley 		ret = PTR_ERR(fsi->policycap_dir);
19900619f0f5SStephen Smalley 		fsi->policycap_dir = NULL;
1991e47c8fc5SChristopher J. PeBenito 		goto err;
1992a1c2aa1eSAl Viro 	}
19930619f0f5SStephen Smalley 
19940619f0f5SStephen Smalley 	ret = sel_make_policy_nodes(fsi);
19950619f0f5SStephen Smalley 	if (ret)
19960619f0f5SStephen Smalley 		goto err;
1997b77a493bSEric Paris 	return 0;
1998161ce45aSJames Morris err:
1999744ba35eSEric Paris 	printk(KERN_ERR "SELinux: %s:  failed while creating inodes\n",
2000744ba35eSEric Paris 		__func__);
20010619f0f5SStephen Smalley 
20020619f0f5SStephen Smalley 	selinux_fs_info_free(sb);
20030619f0f5SStephen Smalley 
2004b77a493bSEric Paris 	return ret;
20051da177e4SLinus Torvalds }
20061da177e4SLinus Torvalds 
2007fc14f2feSAl Viro static struct dentry *sel_mount(struct file_system_type *fs_type,
2008fc14f2feSAl Viro 		      int flags, const char *dev_name, void *data)
20091da177e4SLinus Torvalds {
2010fc14f2feSAl Viro 	return mount_single(fs_type, flags, data, sel_fill_super);
20111da177e4SLinus Torvalds }
20121da177e4SLinus Torvalds 
20130619f0f5SStephen Smalley static void sel_kill_sb(struct super_block *sb)
20140619f0f5SStephen Smalley {
20150619f0f5SStephen Smalley 	selinux_fs_info_free(sb);
20160619f0f5SStephen Smalley 	kill_litter_super(sb);
20170619f0f5SStephen Smalley }
20180619f0f5SStephen Smalley 
20191da177e4SLinus Torvalds static struct file_system_type sel_fs_type = {
20201da177e4SLinus Torvalds 	.name		= "selinuxfs",
2021fc14f2feSAl Viro 	.mount		= sel_mount,
20220619f0f5SStephen Smalley 	.kill_sb	= sel_kill_sb,
20231da177e4SLinus Torvalds };
20241da177e4SLinus Torvalds 
20251da177e4SLinus Torvalds struct vfsmount *selinuxfs_mount;
20260619f0f5SStephen Smalley struct path selinux_null;
20271da177e4SLinus Torvalds 
20281da177e4SLinus Torvalds static int __init init_sel_fs(void)
20291da177e4SLinus Torvalds {
20300619f0f5SStephen Smalley 	struct qstr null_name = QSTR_INIT(NULL_FILE_NAME,
20310619f0f5SStephen Smalley 					  sizeof(NULL_FILE_NAME)-1);
20321da177e4SLinus Torvalds 	int err;
20331da177e4SLinus Torvalds 
20341da177e4SLinus Torvalds 	if (!selinux_enabled)
20351da177e4SLinus Torvalds 		return 0;
20367a627e3bSGreg Kroah-Hartman 
2037f9bb4882SEric W. Biederman 	err = sysfs_create_mount_point(fs_kobj, "selinux");
2038f9bb4882SEric W. Biederman 	if (err)
2039f9bb4882SEric W. Biederman 		return err;
20407a627e3bSGreg Kroah-Hartman 
20411da177e4SLinus Torvalds 	err = register_filesystem(&sel_fs_type);
20427a627e3bSGreg Kroah-Hartman 	if (err) {
2043f9bb4882SEric W. Biederman 		sysfs_remove_mount_point(fs_kobj, "selinux");
2044b77a493bSEric Paris 		return err;
20457a627e3bSGreg Kroah-Hartman 	}
2046b77a493bSEric Paris 
2047765927b2SAl Viro 	selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type);
20481da177e4SLinus Torvalds 	if (IS_ERR(selinuxfs_mount)) {
20491da177e4SLinus Torvalds 		printk(KERN_ERR "selinuxfs:  could not mount!\n");
20501da177e4SLinus Torvalds 		err = PTR_ERR(selinuxfs_mount);
20511da177e4SLinus Torvalds 		selinuxfs_mount = NULL;
20521da177e4SLinus Torvalds 	}
20530619f0f5SStephen Smalley 	selinux_null.dentry = d_hash_and_lookup(selinux_null.mnt->mnt_root,
20540619f0f5SStephen Smalley 						&null_name);
20550619f0f5SStephen Smalley 	if (IS_ERR(selinux_null.dentry)) {
20560619f0f5SStephen Smalley 		pr_err("selinuxfs:  could not lookup null!\n");
20570619f0f5SStephen Smalley 		err = PTR_ERR(selinux_null.dentry);
20580619f0f5SStephen Smalley 		selinux_null.dentry = NULL;
20590619f0f5SStephen Smalley 	}
2060b77a493bSEric Paris 
20611da177e4SLinus Torvalds 	return err;
20621da177e4SLinus Torvalds }
20631da177e4SLinus Torvalds 
20641da177e4SLinus Torvalds __initcall(init_sel_fs);
20651da177e4SLinus Torvalds 
20661da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
20671da177e4SLinus Torvalds void exit_sel_fs(void)
20681da177e4SLinus Torvalds {
2069f9bb4882SEric W. Biederman 	sysfs_remove_mount_point(fs_kobj, "selinux");
2070fd40ffc7SStephen Smalley 	dput(selinux_null.dentry);
2071423e0ab0STim Chen 	kern_unmount(selinuxfs_mount);
20721da177e4SLinus Torvalds 	unregister_filesystem(&sel_fs_type);
20731da177e4SLinus Torvalds }
20741da177e4SLinus Torvalds #endif
2075