xref: /linux/security/selinux/selinuxfs.c (revision 9c2e08c5)
11da177e4SLinus Torvalds /* Updated: Karl MacMillan <kmacmillan@tresys.com>
21da177e4SLinus Torvalds  *
31da177e4SLinus Torvalds  * 	Added conditional policy language extensions
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright (C) 2003 - 2004 Tresys Technology, LLC
61da177e4SLinus Torvalds  * Copyright (C) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
71da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or modify
81da177e4SLinus Torvalds  *  	it under the terms of the GNU General Public License as published by
91da177e4SLinus Torvalds  *	the Free Software Foundation, version 2.
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds #include <linux/kernel.h>
131da177e4SLinus Torvalds #include <linux/pagemap.h>
141da177e4SLinus Torvalds #include <linux/slab.h>
151da177e4SLinus Torvalds #include <linux/vmalloc.h>
161da177e4SLinus Torvalds #include <linux/fs.h>
17bb003079SIngo Molnar #include <linux/mutex.h>
181da177e4SLinus Torvalds #include <linux/init.h>
191da177e4SLinus Torvalds #include <linux/string.h>
201da177e4SLinus Torvalds #include <linux/security.h>
211da177e4SLinus Torvalds #include <linux/major.h>
221da177e4SLinus Torvalds #include <linux/seq_file.h>
231da177e4SLinus Torvalds #include <linux/percpu.h>
24af601e46SSteve Grubb #include <linux/audit.h>
251da177e4SLinus Torvalds #include <asm/uaccess.h>
261da177e4SLinus Torvalds #include <asm/semaphore.h>
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds /* selinuxfs pseudo filesystem for exporting the security policy API.
291da177e4SLinus Torvalds    Based on the proc code and the fs/nfsd/nfsctl.c code. */
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds #include "flask.h"
321da177e4SLinus Torvalds #include "avc.h"
331da177e4SLinus Torvalds #include "avc_ss.h"
341da177e4SLinus Torvalds #include "security.h"
351da177e4SLinus Torvalds #include "objsec.h"
361da177e4SLinus Torvalds #include "conditional.h"
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds unsigned int selinux_checkreqprot = CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
391da177e4SLinus Torvalds 
404e5ab4cbSJames Morris #ifdef CONFIG_SECURITY_SELINUX_ENABLE_SECMARK_DEFAULT
414e5ab4cbSJames Morris #define SELINUX_COMPAT_NET_VALUE 0
424e5ab4cbSJames Morris #else
434e5ab4cbSJames Morris #define SELINUX_COMPAT_NET_VALUE 1
444e5ab4cbSJames Morris #endif
454e5ab4cbSJames Morris 
464e5ab4cbSJames Morris int selinux_compat_net = SELINUX_COMPAT_NET_VALUE;
474e5ab4cbSJames Morris 
481da177e4SLinus Torvalds static int __init checkreqprot_setup(char *str)
491da177e4SLinus Torvalds {
501da177e4SLinus Torvalds 	selinux_checkreqprot = simple_strtoul(str,NULL,0) ? 1 : 0;
511da177e4SLinus Torvalds 	return 1;
521da177e4SLinus Torvalds }
531da177e4SLinus Torvalds __setup("checkreqprot=", checkreqprot_setup);
541da177e4SLinus Torvalds 
554e5ab4cbSJames Morris static int __init selinux_compat_net_setup(char *str)
564e5ab4cbSJames Morris {
574e5ab4cbSJames Morris 	selinux_compat_net = simple_strtoul(str,NULL,0) ? 1 : 0;
584e5ab4cbSJames Morris 	return 1;
594e5ab4cbSJames Morris }
604e5ab4cbSJames Morris __setup("selinux_compat_net=", selinux_compat_net_setup);
614e5ab4cbSJames Morris 
621da177e4SLinus Torvalds 
63bb003079SIngo Molnar static DEFINE_MUTEX(sel_mutex);
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds /* global data for booleans */
661da177e4SLinus Torvalds static struct dentry *bool_dir = NULL;
671da177e4SLinus Torvalds static int bool_num = 0;
681da177e4SLinus Torvalds static int *bool_pending_values = NULL;
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds extern void selnl_notify_setenforce(int val);
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds /* Check whether a task is allowed to use a security operation. */
731da177e4SLinus Torvalds static int task_has_security(struct task_struct *tsk,
741da177e4SLinus Torvalds 			     u32 perms)
751da177e4SLinus Torvalds {
761da177e4SLinus Torvalds 	struct task_security_struct *tsec;
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds 	tsec = tsk->security;
791da177e4SLinus Torvalds 	if (!tsec)
801da177e4SLinus Torvalds 		return -EACCES;
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds 	return avc_has_perm(tsec->sid, SECINITSID_SECURITY,
831da177e4SLinus Torvalds 			    SECCLASS_SECURITY, perms, NULL);
841da177e4SLinus Torvalds }
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds enum sel_inos {
871da177e4SLinus Torvalds 	SEL_ROOT_INO = 2,
881da177e4SLinus Torvalds 	SEL_LOAD,	/* load policy */
891da177e4SLinus Torvalds 	SEL_ENFORCE,	/* get or set enforcing status */
901da177e4SLinus Torvalds 	SEL_CONTEXT,	/* validate context */
911da177e4SLinus Torvalds 	SEL_ACCESS,	/* compute access decision */
921da177e4SLinus Torvalds 	SEL_CREATE,	/* compute create labeling decision */
931da177e4SLinus Torvalds 	SEL_RELABEL,	/* compute relabeling decision */
941da177e4SLinus Torvalds 	SEL_USER,	/* compute reachable user contexts */
951da177e4SLinus Torvalds 	SEL_POLICYVERS,	/* return policy version for this kernel */
961da177e4SLinus Torvalds 	SEL_COMMIT_BOOLS, /* commit new boolean values */
971da177e4SLinus Torvalds 	SEL_MLS,	/* return if MLS policy is enabled */
981da177e4SLinus Torvalds 	SEL_DISABLE,	/* disable SELinux until next reboot */
991da177e4SLinus Torvalds 	SEL_AVC,	/* AVC management directory */
1001da177e4SLinus Torvalds 	SEL_MEMBER,	/* compute polyinstantiation membership decision */
1011da177e4SLinus Torvalds 	SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
1024e5ab4cbSJames Morris 	SEL_COMPAT_NET,	/* whether to use old compat network packet controls */
1031da177e4SLinus Torvalds };
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds #define TMPBUFLEN	12
1061da177e4SLinus Torvalds static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
1071da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
1081da177e4SLinus Torvalds {
1091da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
1101da177e4SLinus Torvalds 	ssize_t length;
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds 	length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing);
1131da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1141da177e4SLinus Torvalds }
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
1171da177e4SLinus Torvalds static ssize_t sel_write_enforce(struct file * file, const char __user * buf,
1181da177e4SLinus Torvalds 				 size_t count, loff_t *ppos)
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds {
1211da177e4SLinus Torvalds 	char *page;
1221da177e4SLinus Torvalds 	ssize_t length;
1231da177e4SLinus Torvalds 	int new_value;
1241da177e4SLinus Torvalds 
125bfd51626SDavi Arnaut 	if (count >= PAGE_SIZE)
1261da177e4SLinus Torvalds 		return -ENOMEM;
1271da177e4SLinus Torvalds 	if (*ppos != 0) {
1281da177e4SLinus Torvalds 		/* No partial writes. */
1291da177e4SLinus Torvalds 		return -EINVAL;
1301da177e4SLinus Torvalds 	}
1311da177e4SLinus Torvalds 	page = (char*)get_zeroed_page(GFP_KERNEL);
1321da177e4SLinus Torvalds 	if (!page)
1331da177e4SLinus Torvalds 		return -ENOMEM;
1341da177e4SLinus Torvalds 	length = -EFAULT;
1351da177e4SLinus Torvalds 	if (copy_from_user(page, buf, count))
1361da177e4SLinus Torvalds 		goto out;
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds 	length = -EINVAL;
1391da177e4SLinus Torvalds 	if (sscanf(page, "%d", &new_value) != 1)
1401da177e4SLinus Torvalds 		goto out;
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds 	if (new_value != selinux_enforcing) {
1431da177e4SLinus Torvalds 		length = task_has_security(current, SECURITY__SETENFORCE);
1441da177e4SLinus Torvalds 		if (length)
1451da177e4SLinus Torvalds 			goto out;
146af601e46SSteve Grubb 		audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
147af601e46SSteve Grubb 			"enforcing=%d old_enforcing=%d auid=%u", new_value,
148af601e46SSteve Grubb 			selinux_enforcing,
149af601e46SSteve Grubb 			audit_get_loginuid(current->audit_context));
1501da177e4SLinus Torvalds 		selinux_enforcing = new_value;
1511da177e4SLinus Torvalds 		if (selinux_enforcing)
1521da177e4SLinus Torvalds 			avc_ss_reset(0);
1531da177e4SLinus Torvalds 		selnl_notify_setenforce(selinux_enforcing);
1541da177e4SLinus Torvalds 	}
1551da177e4SLinus Torvalds 	length = count;
1561da177e4SLinus Torvalds out:
1571da177e4SLinus Torvalds 	free_page((unsigned long) page);
1581da177e4SLinus Torvalds 	return length;
1591da177e4SLinus Torvalds }
1601da177e4SLinus Torvalds #else
1611da177e4SLinus Torvalds #define sel_write_enforce NULL
1621da177e4SLinus Torvalds #endif
1631da177e4SLinus Torvalds 
164*9c2e08c5SArjan van de Ven static const struct file_operations sel_enforce_ops = {
1651da177e4SLinus Torvalds 	.read		= sel_read_enforce,
1661da177e4SLinus Torvalds 	.write		= sel_write_enforce,
1671da177e4SLinus Torvalds };
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
1701da177e4SLinus Torvalds static ssize_t sel_write_disable(struct file * file, const char __user * buf,
1711da177e4SLinus Torvalds 				 size_t count, loff_t *ppos)
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds {
1741da177e4SLinus Torvalds 	char *page;
1751da177e4SLinus Torvalds 	ssize_t length;
1761da177e4SLinus Torvalds 	int new_value;
1771da177e4SLinus Torvalds 	extern int selinux_disable(void);
1781da177e4SLinus Torvalds 
179bfd51626SDavi Arnaut 	if (count >= PAGE_SIZE)
1801da177e4SLinus Torvalds 		return -ENOMEM;
1811da177e4SLinus Torvalds 	if (*ppos != 0) {
1821da177e4SLinus Torvalds 		/* No partial writes. */
1831da177e4SLinus Torvalds 		return -EINVAL;
1841da177e4SLinus Torvalds 	}
1851da177e4SLinus Torvalds 	page = (char*)get_zeroed_page(GFP_KERNEL);
1861da177e4SLinus Torvalds 	if (!page)
1871da177e4SLinus Torvalds 		return -ENOMEM;
1881da177e4SLinus Torvalds 	length = -EFAULT;
1891da177e4SLinus Torvalds 	if (copy_from_user(page, buf, count))
1901da177e4SLinus Torvalds 		goto out;
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	length = -EINVAL;
1931da177e4SLinus Torvalds 	if (sscanf(page, "%d", &new_value) != 1)
1941da177e4SLinus Torvalds 		goto out;
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds 	if (new_value) {
1971da177e4SLinus Torvalds 		length = selinux_disable();
1981da177e4SLinus Torvalds 		if (length < 0)
1991da177e4SLinus Torvalds 			goto out;
200af601e46SSteve Grubb 		audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
201af601e46SSteve Grubb 			"selinux=0 auid=%u",
202af601e46SSteve Grubb 			audit_get_loginuid(current->audit_context));
2031da177e4SLinus Torvalds 	}
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds 	length = count;
2061da177e4SLinus Torvalds out:
2071da177e4SLinus Torvalds 	free_page((unsigned long) page);
2081da177e4SLinus Torvalds 	return length;
2091da177e4SLinus Torvalds }
2101da177e4SLinus Torvalds #else
2111da177e4SLinus Torvalds #define sel_write_disable NULL
2121da177e4SLinus Torvalds #endif
2131da177e4SLinus Torvalds 
214*9c2e08c5SArjan van de Ven static const struct file_operations sel_disable_ops = {
2151da177e4SLinus Torvalds 	.write		= sel_write_disable,
2161da177e4SLinus Torvalds };
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
2191da177e4SLinus Torvalds                                    size_t count, loff_t *ppos)
2201da177e4SLinus Torvalds {
2211da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
2221da177e4SLinus Torvalds 	ssize_t length;
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
2251da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
2261da177e4SLinus Torvalds }
2271da177e4SLinus Torvalds 
228*9c2e08c5SArjan van de Ven static const struct file_operations sel_policyvers_ops = {
2291da177e4SLinus Torvalds 	.read		= sel_read_policyvers,
2301da177e4SLinus Torvalds };
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds /* declaration for sel_write_load */
2331da177e4SLinus Torvalds static int sel_make_bools(void);
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds static ssize_t sel_read_mls(struct file *filp, char __user *buf,
2361da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
2371da177e4SLinus Torvalds {
2381da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
2391da177e4SLinus Torvalds 	ssize_t length;
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_mls_enabled);
2421da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
2431da177e4SLinus Torvalds }
2441da177e4SLinus Torvalds 
245*9c2e08c5SArjan van de Ven static const struct file_operations sel_mls_ops = {
2461da177e4SLinus Torvalds 	.read		= sel_read_mls,
2471da177e4SLinus Torvalds };
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds static ssize_t sel_write_load(struct file * file, const char __user * buf,
2501da177e4SLinus Torvalds 			      size_t count, loff_t *ppos)
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds {
2531da177e4SLinus Torvalds 	int ret;
2541da177e4SLinus Torvalds 	ssize_t length;
2551da177e4SLinus Torvalds 	void *data = NULL;
2561da177e4SLinus Torvalds 
257bb003079SIngo Molnar 	mutex_lock(&sel_mutex);
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 	length = task_has_security(current, SECURITY__LOAD_POLICY);
2601da177e4SLinus Torvalds 	if (length)
2611da177e4SLinus Torvalds 		goto out;
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 	if (*ppos != 0) {
2641da177e4SLinus Torvalds 		/* No partial writes. */
2651da177e4SLinus Torvalds 		length = -EINVAL;
2661da177e4SLinus Torvalds 		goto out;
2671da177e4SLinus Torvalds 	}
2681da177e4SLinus Torvalds 
269bfd51626SDavi Arnaut 	if ((count > 64 * 1024 * 1024)
2701da177e4SLinus Torvalds 	    || (data = vmalloc(count)) == NULL) {
2711da177e4SLinus Torvalds 		length = -ENOMEM;
2721da177e4SLinus Torvalds 		goto out;
2731da177e4SLinus Torvalds 	}
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 	length = -EFAULT;
2761da177e4SLinus Torvalds 	if (copy_from_user(data, buf, count) != 0)
2771da177e4SLinus Torvalds 		goto out;
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 	length = security_load_policy(data, count);
2801da177e4SLinus Torvalds 	if (length)
2811da177e4SLinus Torvalds 		goto out;
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	ret = sel_make_bools();
2841da177e4SLinus Torvalds 	if (ret)
2851da177e4SLinus Torvalds 		length = ret;
2861da177e4SLinus Torvalds 	else
2871da177e4SLinus Torvalds 		length = count;
288af601e46SSteve Grubb 	audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
289af601e46SSteve Grubb 		"policy loaded auid=%u",
290af601e46SSteve Grubb 		audit_get_loginuid(current->audit_context));
2911da177e4SLinus Torvalds out:
292bb003079SIngo Molnar 	mutex_unlock(&sel_mutex);
2931da177e4SLinus Torvalds 	vfree(data);
2941da177e4SLinus Torvalds 	return length;
2951da177e4SLinus Torvalds }
2961da177e4SLinus Torvalds 
297*9c2e08c5SArjan van de Ven static const struct file_operations sel_load_ops = {
2981da177e4SLinus Torvalds 	.write		= sel_write_load,
2991da177e4SLinus Torvalds };
3001da177e4SLinus Torvalds 
301ce9982d0SStephen Smalley static ssize_t sel_write_context(struct file * file, char *buf, size_t size)
3021da177e4SLinus Torvalds {
303ce9982d0SStephen Smalley 	char *canon;
304ce9982d0SStephen Smalley 	u32 sid, len;
3051da177e4SLinus Torvalds 	ssize_t length;
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds 	length = task_has_security(current, SECURITY__CHECK_CONTEXT);
3081da177e4SLinus Torvalds 	if (length)
3091da177e4SLinus Torvalds 		return length;
3101da177e4SLinus Torvalds 
311ce9982d0SStephen Smalley 	length = security_context_to_sid(buf, size, &sid);
3121da177e4SLinus Torvalds 	if (length < 0)
313ce9982d0SStephen Smalley 		return length;
3141da177e4SLinus Torvalds 
315ce9982d0SStephen Smalley 	length = security_sid_to_context(sid, &canon, &len);
316ce9982d0SStephen Smalley 	if (length < 0)
317ce9982d0SStephen Smalley 		return length;
318ce9982d0SStephen Smalley 
319ce9982d0SStephen Smalley 	if (len > SIMPLE_TRANSACTION_LIMIT) {
320ce9982d0SStephen Smalley 		printk(KERN_ERR "%s:  context size (%u) exceeds payload "
321ce9982d0SStephen Smalley 		       "max\n", __FUNCTION__, len);
322ce9982d0SStephen Smalley 		length = -ERANGE;
323ce9982d0SStephen Smalley 		goto out;
324ce9982d0SStephen Smalley 	}
325ce9982d0SStephen Smalley 
326ce9982d0SStephen Smalley 	memcpy(buf, canon, len);
327ce9982d0SStephen Smalley 	length = len;
3281da177e4SLinus Torvalds out:
329ce9982d0SStephen Smalley 	kfree(canon);
3301da177e4SLinus Torvalds 	return length;
3311da177e4SLinus Torvalds }
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
3341da177e4SLinus Torvalds 				     size_t count, loff_t *ppos)
3351da177e4SLinus Torvalds {
3361da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
3371da177e4SLinus Torvalds 	ssize_t length;
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot);
3401da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
3411da177e4SLinus Torvalds }
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds static ssize_t sel_write_checkreqprot(struct file * file, const char __user * buf,
3441da177e4SLinus Torvalds 				      size_t count, loff_t *ppos)
3451da177e4SLinus Torvalds {
3461da177e4SLinus Torvalds 	char *page;
3471da177e4SLinus Torvalds 	ssize_t length;
3481da177e4SLinus Torvalds 	unsigned int new_value;
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds 	length = task_has_security(current, SECURITY__SETCHECKREQPROT);
3511da177e4SLinus Torvalds 	if (length)
3521da177e4SLinus Torvalds 		return length;
3531da177e4SLinus Torvalds 
354bfd51626SDavi Arnaut 	if (count >= PAGE_SIZE)
3551da177e4SLinus Torvalds 		return -ENOMEM;
3561da177e4SLinus Torvalds 	if (*ppos != 0) {
3571da177e4SLinus Torvalds 		/* No partial writes. */
3581da177e4SLinus Torvalds 		return -EINVAL;
3591da177e4SLinus Torvalds 	}
3601da177e4SLinus Torvalds 	page = (char*)get_zeroed_page(GFP_KERNEL);
3611da177e4SLinus Torvalds 	if (!page)
3621da177e4SLinus Torvalds 		return -ENOMEM;
3631da177e4SLinus Torvalds 	length = -EFAULT;
3641da177e4SLinus Torvalds 	if (copy_from_user(page, buf, count))
3651da177e4SLinus Torvalds 		goto out;
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 	length = -EINVAL;
3681da177e4SLinus Torvalds 	if (sscanf(page, "%u", &new_value) != 1)
3691da177e4SLinus Torvalds 		goto out;
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds 	selinux_checkreqprot = new_value ? 1 : 0;
3721da177e4SLinus Torvalds 	length = count;
3731da177e4SLinus Torvalds out:
3741da177e4SLinus Torvalds 	free_page((unsigned long) page);
3751da177e4SLinus Torvalds 	return length;
3761da177e4SLinus Torvalds }
377*9c2e08c5SArjan van de Ven static const struct file_operations sel_checkreqprot_ops = {
3781da177e4SLinus Torvalds 	.read		= sel_read_checkreqprot,
3791da177e4SLinus Torvalds 	.write		= sel_write_checkreqprot,
3801da177e4SLinus Torvalds };
3811da177e4SLinus Torvalds 
3824e5ab4cbSJames Morris static ssize_t sel_read_compat_net(struct file *filp, char __user *buf,
3834e5ab4cbSJames Morris 				   size_t count, loff_t *ppos)
3844e5ab4cbSJames Morris {
3854e5ab4cbSJames Morris 	char tmpbuf[TMPBUFLEN];
3864e5ab4cbSJames Morris 	ssize_t length;
3874e5ab4cbSJames Morris 
3884e5ab4cbSJames Morris 	length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_compat_net);
3894e5ab4cbSJames Morris 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
3904e5ab4cbSJames Morris }
3914e5ab4cbSJames Morris 
3924e5ab4cbSJames Morris static ssize_t sel_write_compat_net(struct file * file, const char __user * buf,
3934e5ab4cbSJames Morris 				    size_t count, loff_t *ppos)
3944e5ab4cbSJames Morris {
3954e5ab4cbSJames Morris 	char *page;
3964e5ab4cbSJames Morris 	ssize_t length;
3974e5ab4cbSJames Morris 	int new_value;
3984e5ab4cbSJames Morris 
3994e5ab4cbSJames Morris 	length = task_has_security(current, SECURITY__LOAD_POLICY);
4004e5ab4cbSJames Morris 	if (length)
4014e5ab4cbSJames Morris 		return length;
4024e5ab4cbSJames Morris 
4034e5ab4cbSJames Morris 	if (count >= PAGE_SIZE)
4044e5ab4cbSJames Morris 		return -ENOMEM;
4054e5ab4cbSJames Morris 	if (*ppos != 0) {
4064e5ab4cbSJames Morris 		/* No partial writes. */
4074e5ab4cbSJames Morris 		return -EINVAL;
4084e5ab4cbSJames Morris 	}
4094e5ab4cbSJames Morris 	page = (char*)get_zeroed_page(GFP_KERNEL);
4104e5ab4cbSJames Morris 	if (!page)
4114e5ab4cbSJames Morris 		return -ENOMEM;
4124e5ab4cbSJames Morris 	length = -EFAULT;
4134e5ab4cbSJames Morris 	if (copy_from_user(page, buf, count))
4144e5ab4cbSJames Morris 		goto out;
4154e5ab4cbSJames Morris 
4164e5ab4cbSJames Morris 	length = -EINVAL;
4174e5ab4cbSJames Morris 	if (sscanf(page, "%d", &new_value) != 1)
4184e5ab4cbSJames Morris 		goto out;
4194e5ab4cbSJames Morris 
4204e5ab4cbSJames Morris 	selinux_compat_net = new_value ? 1 : 0;
4214e5ab4cbSJames Morris 	length = count;
4224e5ab4cbSJames Morris out:
4234e5ab4cbSJames Morris 	free_page((unsigned long) page);
4244e5ab4cbSJames Morris 	return length;
4254e5ab4cbSJames Morris }
426*9c2e08c5SArjan van de Ven static const struct file_operations sel_compat_net_ops = {
4274e5ab4cbSJames Morris 	.read		= sel_read_compat_net,
4284e5ab4cbSJames Morris 	.write		= sel_write_compat_net,
4294e5ab4cbSJames Morris };
4304e5ab4cbSJames Morris 
4311da177e4SLinus Torvalds /*
4321da177e4SLinus Torvalds  * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
4331da177e4SLinus Torvalds  */
4341da177e4SLinus Torvalds static ssize_t sel_write_access(struct file * file, char *buf, size_t size);
4351da177e4SLinus Torvalds static ssize_t sel_write_create(struct file * file, char *buf, size_t size);
4361da177e4SLinus Torvalds static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size);
4371da177e4SLinus Torvalds static ssize_t sel_write_user(struct file * file, char *buf, size_t size);
4381da177e4SLinus Torvalds static ssize_t sel_write_member(struct file * file, char *buf, size_t size);
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds static ssize_t (*write_op[])(struct file *, char *, size_t) = {
4411da177e4SLinus Torvalds 	[SEL_ACCESS] = sel_write_access,
4421da177e4SLinus Torvalds 	[SEL_CREATE] = sel_write_create,
4431da177e4SLinus Torvalds 	[SEL_RELABEL] = sel_write_relabel,
4441da177e4SLinus Torvalds 	[SEL_USER] = sel_write_user,
4451da177e4SLinus Torvalds 	[SEL_MEMBER] = sel_write_member,
446ce9982d0SStephen Smalley 	[SEL_CONTEXT] = sel_write_context,
4471da177e4SLinus Torvalds };
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
4501da177e4SLinus Torvalds {
4513d5ff529SJosef Sipek 	ino_t ino =  file->f_path.dentry->d_inode->i_ino;
4521da177e4SLinus Torvalds 	char *data;
4531da177e4SLinus Torvalds 	ssize_t rv;
4541da177e4SLinus Torvalds 
4556e20a64aSNicolas Kaiser 	if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
4561da177e4SLinus Torvalds 		return -EINVAL;
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds 	data = simple_transaction_get(file, buf, size);
4591da177e4SLinus Torvalds 	if (IS_ERR(data))
4601da177e4SLinus Torvalds 		return PTR_ERR(data);
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds 	rv =  write_op[ino](file, data, size);
4631da177e4SLinus Torvalds 	if (rv>0) {
4641da177e4SLinus Torvalds 		simple_transaction_set(file, rv);
4651da177e4SLinus Torvalds 		rv = size;
4661da177e4SLinus Torvalds 	}
4671da177e4SLinus Torvalds 	return rv;
4681da177e4SLinus Torvalds }
4691da177e4SLinus Torvalds 
470*9c2e08c5SArjan van de Ven static const struct file_operations transaction_ops = {
4711da177e4SLinus Torvalds 	.write		= selinux_transaction_write,
4721da177e4SLinus Torvalds 	.read		= simple_transaction_read,
4731da177e4SLinus Torvalds 	.release	= simple_transaction_release,
4741da177e4SLinus Torvalds };
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds /*
4771da177e4SLinus Torvalds  * payload - write methods
4781da177e4SLinus Torvalds  * If the method has a response, the response should be put in buf,
4791da177e4SLinus Torvalds  * and the length returned.  Otherwise return 0 or and -error.
4801da177e4SLinus Torvalds  */
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds static ssize_t sel_write_access(struct file * file, char *buf, size_t size)
4831da177e4SLinus Torvalds {
4841da177e4SLinus Torvalds 	char *scon, *tcon;
4851da177e4SLinus Torvalds 	u32 ssid, tsid;
4861da177e4SLinus Torvalds 	u16 tclass;
4871da177e4SLinus Torvalds 	u32 req;
4881da177e4SLinus Torvalds 	struct av_decision avd;
4891da177e4SLinus Torvalds 	ssize_t length;
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds 	length = task_has_security(current, SECURITY__COMPUTE_AV);
4921da177e4SLinus Torvalds 	if (length)
4931da177e4SLinus Torvalds 		return length;
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds 	length = -ENOMEM;
49689d155efSJames Morris 	scon = kzalloc(size+1, GFP_KERNEL);
4971da177e4SLinus Torvalds 	if (!scon)
4981da177e4SLinus Torvalds 		return length;
4991da177e4SLinus Torvalds 
50089d155efSJames Morris 	tcon = kzalloc(size+1, GFP_KERNEL);
5011da177e4SLinus Torvalds 	if (!tcon)
5021da177e4SLinus Torvalds 		goto out;
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds 	length = -EINVAL;
5051da177e4SLinus Torvalds 	if (sscanf(buf, "%s %s %hu %x", scon, tcon, &tclass, &req) != 4)
5061da177e4SLinus Torvalds 		goto out2;
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds 	length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
5091da177e4SLinus Torvalds 	if (length < 0)
5101da177e4SLinus Torvalds 		goto out2;
5111da177e4SLinus Torvalds 	length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
5121da177e4SLinus Torvalds 	if (length < 0)
5131da177e4SLinus Torvalds 		goto out2;
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 	length = security_compute_av(ssid, tsid, tclass, req, &avd);
5161da177e4SLinus Torvalds 	if (length < 0)
5171da177e4SLinus Torvalds 		goto out2;
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds 	length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
5201da177e4SLinus Torvalds 			  "%x %x %x %x %u",
5211da177e4SLinus Torvalds 			  avd.allowed, avd.decided,
5221da177e4SLinus Torvalds 			  avd.auditallow, avd.auditdeny,
5231da177e4SLinus Torvalds 			  avd.seqno);
5241da177e4SLinus Torvalds out2:
5251da177e4SLinus Torvalds 	kfree(tcon);
5261da177e4SLinus Torvalds out:
5271da177e4SLinus Torvalds 	kfree(scon);
5281da177e4SLinus Torvalds 	return length;
5291da177e4SLinus Torvalds }
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds static ssize_t sel_write_create(struct file * file, char *buf, size_t size)
5321da177e4SLinus Torvalds {
5331da177e4SLinus Torvalds 	char *scon, *tcon;
5341da177e4SLinus Torvalds 	u32 ssid, tsid, newsid;
5351da177e4SLinus Torvalds 	u16 tclass;
5361da177e4SLinus Torvalds 	ssize_t length;
5371da177e4SLinus Torvalds 	char *newcon;
5381da177e4SLinus Torvalds 	u32 len;
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds 	length = task_has_security(current, SECURITY__COMPUTE_CREATE);
5411da177e4SLinus Torvalds 	if (length)
5421da177e4SLinus Torvalds 		return length;
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds 	length = -ENOMEM;
54589d155efSJames Morris 	scon = kzalloc(size+1, GFP_KERNEL);
5461da177e4SLinus Torvalds 	if (!scon)
5471da177e4SLinus Torvalds 		return length;
5481da177e4SLinus Torvalds 
54989d155efSJames Morris 	tcon = kzalloc(size+1, GFP_KERNEL);
5501da177e4SLinus Torvalds 	if (!tcon)
5511da177e4SLinus Torvalds 		goto out;
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds 	length = -EINVAL;
5541da177e4SLinus Torvalds 	if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
5551da177e4SLinus Torvalds 		goto out2;
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds 	length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
5581da177e4SLinus Torvalds 	if (length < 0)
5591da177e4SLinus Torvalds 		goto out2;
5601da177e4SLinus Torvalds 	length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
5611da177e4SLinus Torvalds 	if (length < 0)
5621da177e4SLinus Torvalds 		goto out2;
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds 	length = security_transition_sid(ssid, tsid, tclass, &newsid);
5651da177e4SLinus Torvalds 	if (length < 0)
5661da177e4SLinus Torvalds 		goto out2;
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds 	length = security_sid_to_context(newsid, &newcon, &len);
5691da177e4SLinus Torvalds 	if (length < 0)
5701da177e4SLinus Torvalds 		goto out2;
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 	if (len > SIMPLE_TRANSACTION_LIMIT) {
5731da177e4SLinus Torvalds 		printk(KERN_ERR "%s:  context size (%u) exceeds payload "
5741da177e4SLinus Torvalds 		       "max\n", __FUNCTION__, len);
5751da177e4SLinus Torvalds 		length = -ERANGE;
5761da177e4SLinus Torvalds 		goto out3;
5771da177e4SLinus Torvalds 	}
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds 	memcpy(buf, newcon, len);
5801da177e4SLinus Torvalds 	length = len;
5811da177e4SLinus Torvalds out3:
5821da177e4SLinus Torvalds 	kfree(newcon);
5831da177e4SLinus Torvalds out2:
5841da177e4SLinus Torvalds 	kfree(tcon);
5851da177e4SLinus Torvalds out:
5861da177e4SLinus Torvalds 	kfree(scon);
5871da177e4SLinus Torvalds 	return length;
5881da177e4SLinus Torvalds }
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size)
5911da177e4SLinus Torvalds {
5921da177e4SLinus Torvalds 	char *scon, *tcon;
5931da177e4SLinus Torvalds 	u32 ssid, tsid, newsid;
5941da177e4SLinus Torvalds 	u16 tclass;
5951da177e4SLinus Torvalds 	ssize_t length;
5961da177e4SLinus Torvalds 	char *newcon;
5971da177e4SLinus Torvalds 	u32 len;
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds 	length = task_has_security(current, SECURITY__COMPUTE_RELABEL);
6001da177e4SLinus Torvalds 	if (length)
6011da177e4SLinus Torvalds 		return length;
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 	length = -ENOMEM;
60489d155efSJames Morris 	scon = kzalloc(size+1, GFP_KERNEL);
6051da177e4SLinus Torvalds 	if (!scon)
6061da177e4SLinus Torvalds 		return length;
6071da177e4SLinus Torvalds 
60889d155efSJames Morris 	tcon = kzalloc(size+1, GFP_KERNEL);
6091da177e4SLinus Torvalds 	if (!tcon)
6101da177e4SLinus Torvalds 		goto out;
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds 	length = -EINVAL;
6131da177e4SLinus Torvalds 	if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
6141da177e4SLinus Torvalds 		goto out2;
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds 	length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
6171da177e4SLinus Torvalds 	if (length < 0)
6181da177e4SLinus Torvalds 		goto out2;
6191da177e4SLinus Torvalds 	length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
6201da177e4SLinus Torvalds 	if (length < 0)
6211da177e4SLinus Torvalds 		goto out2;
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds 	length = security_change_sid(ssid, tsid, tclass, &newsid);
6241da177e4SLinus Torvalds 	if (length < 0)
6251da177e4SLinus Torvalds 		goto out2;
6261da177e4SLinus Torvalds 
6271da177e4SLinus Torvalds 	length = security_sid_to_context(newsid, &newcon, &len);
6281da177e4SLinus Torvalds 	if (length < 0)
6291da177e4SLinus Torvalds 		goto out2;
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds 	if (len > SIMPLE_TRANSACTION_LIMIT) {
6321da177e4SLinus Torvalds 		length = -ERANGE;
6331da177e4SLinus Torvalds 		goto out3;
6341da177e4SLinus Torvalds 	}
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds 	memcpy(buf, newcon, len);
6371da177e4SLinus Torvalds 	length = len;
6381da177e4SLinus Torvalds out3:
6391da177e4SLinus Torvalds 	kfree(newcon);
6401da177e4SLinus Torvalds out2:
6411da177e4SLinus Torvalds 	kfree(tcon);
6421da177e4SLinus Torvalds out:
6431da177e4SLinus Torvalds 	kfree(scon);
6441da177e4SLinus Torvalds 	return length;
6451da177e4SLinus Torvalds }
6461da177e4SLinus Torvalds 
6471da177e4SLinus Torvalds static ssize_t sel_write_user(struct file * file, char *buf, size_t size)
6481da177e4SLinus Torvalds {
6491da177e4SLinus Torvalds 	char *con, *user, *ptr;
6501da177e4SLinus Torvalds 	u32 sid, *sids;
6511da177e4SLinus Torvalds 	ssize_t length;
6521da177e4SLinus Torvalds 	char *newcon;
6531da177e4SLinus Torvalds 	int i, rc;
6541da177e4SLinus Torvalds 	u32 len, nsids;
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds 	length = task_has_security(current, SECURITY__COMPUTE_USER);
6571da177e4SLinus Torvalds 	if (length)
6581da177e4SLinus Torvalds 		return length;
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds 	length = -ENOMEM;
66189d155efSJames Morris 	con = kzalloc(size+1, GFP_KERNEL);
6621da177e4SLinus Torvalds 	if (!con)
6631da177e4SLinus Torvalds 		return length;
6641da177e4SLinus Torvalds 
66589d155efSJames Morris 	user = kzalloc(size+1, GFP_KERNEL);
6661da177e4SLinus Torvalds 	if (!user)
6671da177e4SLinus Torvalds 		goto out;
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds 	length = -EINVAL;
6701da177e4SLinus Torvalds 	if (sscanf(buf, "%s %s", con, user) != 2)
6711da177e4SLinus Torvalds 		goto out2;
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds 	length = security_context_to_sid(con, strlen(con)+1, &sid);
6741da177e4SLinus Torvalds 	if (length < 0)
6751da177e4SLinus Torvalds 		goto out2;
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds 	length = security_get_user_sids(sid, user, &sids, &nsids);
6781da177e4SLinus Torvalds 	if (length < 0)
6791da177e4SLinus Torvalds 		goto out2;
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds 	length = sprintf(buf, "%u", nsids) + 1;
6821da177e4SLinus Torvalds 	ptr = buf + length;
6831da177e4SLinus Torvalds 	for (i = 0; i < nsids; i++) {
6841da177e4SLinus Torvalds 		rc = security_sid_to_context(sids[i], &newcon, &len);
6851da177e4SLinus Torvalds 		if (rc) {
6861da177e4SLinus Torvalds 			length = rc;
6871da177e4SLinus Torvalds 			goto out3;
6881da177e4SLinus Torvalds 		}
6891da177e4SLinus Torvalds 		if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
6901da177e4SLinus Torvalds 			kfree(newcon);
6911da177e4SLinus Torvalds 			length = -ERANGE;
6921da177e4SLinus Torvalds 			goto out3;
6931da177e4SLinus Torvalds 		}
6941da177e4SLinus Torvalds 		memcpy(ptr, newcon, len);
6951da177e4SLinus Torvalds 		kfree(newcon);
6961da177e4SLinus Torvalds 		ptr += len;
6971da177e4SLinus Torvalds 		length += len;
6981da177e4SLinus Torvalds 	}
6991da177e4SLinus Torvalds out3:
7001da177e4SLinus Torvalds 	kfree(sids);
7011da177e4SLinus Torvalds out2:
7021da177e4SLinus Torvalds 	kfree(user);
7031da177e4SLinus Torvalds out:
7041da177e4SLinus Torvalds 	kfree(con);
7051da177e4SLinus Torvalds 	return length;
7061da177e4SLinus Torvalds }
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds static ssize_t sel_write_member(struct file * file, char *buf, size_t size)
7091da177e4SLinus Torvalds {
7101da177e4SLinus Torvalds 	char *scon, *tcon;
7111da177e4SLinus Torvalds 	u32 ssid, tsid, newsid;
7121da177e4SLinus Torvalds 	u16 tclass;
7131da177e4SLinus Torvalds 	ssize_t length;
7141da177e4SLinus Torvalds 	char *newcon;
7151da177e4SLinus Torvalds 	u32 len;
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds 	length = task_has_security(current, SECURITY__COMPUTE_MEMBER);
7181da177e4SLinus Torvalds 	if (length)
7191da177e4SLinus Torvalds 		return length;
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds 	length = -ENOMEM;
72289d155efSJames Morris 	scon = kzalloc(size+1, GFP_KERNEL);
7231da177e4SLinus Torvalds 	if (!scon)
7241da177e4SLinus Torvalds 		return length;
7251da177e4SLinus Torvalds 
72689d155efSJames Morris 	tcon = kzalloc(size+1, GFP_KERNEL);
7271da177e4SLinus Torvalds 	if (!tcon)
7281da177e4SLinus Torvalds 		goto out;
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds 	length = -EINVAL;
7311da177e4SLinus Torvalds 	if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
7321da177e4SLinus Torvalds 		goto out2;
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds 	length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
7351da177e4SLinus Torvalds 	if (length < 0)
7361da177e4SLinus Torvalds 		goto out2;
7371da177e4SLinus Torvalds 	length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
7381da177e4SLinus Torvalds 	if (length < 0)
7391da177e4SLinus Torvalds 		goto out2;
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds 	length = security_member_sid(ssid, tsid, tclass, &newsid);
7421da177e4SLinus Torvalds 	if (length < 0)
7431da177e4SLinus Torvalds 		goto out2;
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds 	length = security_sid_to_context(newsid, &newcon, &len);
7461da177e4SLinus Torvalds 	if (length < 0)
7471da177e4SLinus Torvalds 		goto out2;
7481da177e4SLinus Torvalds 
7491da177e4SLinus Torvalds 	if (len > SIMPLE_TRANSACTION_LIMIT) {
7501da177e4SLinus Torvalds 		printk(KERN_ERR "%s:  context size (%u) exceeds payload "
7511da177e4SLinus Torvalds 		       "max\n", __FUNCTION__, len);
7521da177e4SLinus Torvalds 		length = -ERANGE;
7531da177e4SLinus Torvalds 		goto out3;
7541da177e4SLinus Torvalds 	}
7551da177e4SLinus Torvalds 
7561da177e4SLinus Torvalds 	memcpy(buf, newcon, len);
7571da177e4SLinus Torvalds 	length = len;
7581da177e4SLinus Torvalds out3:
7591da177e4SLinus Torvalds 	kfree(newcon);
7601da177e4SLinus Torvalds out2:
7611da177e4SLinus Torvalds 	kfree(tcon);
7621da177e4SLinus Torvalds out:
7631da177e4SLinus Torvalds 	kfree(scon);
7641da177e4SLinus Torvalds 	return length;
7651da177e4SLinus Torvalds }
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds static struct inode *sel_make_inode(struct super_block *sb, int mode)
7681da177e4SLinus Torvalds {
7691da177e4SLinus Torvalds 	struct inode *ret = new_inode(sb);
7701da177e4SLinus Torvalds 
7711da177e4SLinus Torvalds 	if (ret) {
7721da177e4SLinus Torvalds 		ret->i_mode = mode;
7731da177e4SLinus Torvalds 		ret->i_uid = ret->i_gid = 0;
7741da177e4SLinus Torvalds 		ret->i_blocks = 0;
7751da177e4SLinus Torvalds 		ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
7761da177e4SLinus Torvalds 	}
7771da177e4SLinus Torvalds 	return ret;
7781da177e4SLinus Torvalds }
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds #define BOOL_INO_OFFSET 30
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds static ssize_t sel_read_bool(struct file *filep, char __user *buf,
7831da177e4SLinus Torvalds 			     size_t count, loff_t *ppos)
7841da177e4SLinus Torvalds {
7851da177e4SLinus Torvalds 	char *page = NULL;
7861da177e4SLinus Torvalds 	ssize_t length;
7871da177e4SLinus Torvalds 	ssize_t ret;
7881da177e4SLinus Torvalds 	int cur_enforcing;
7891da177e4SLinus Torvalds 	struct inode *inode;
7901da177e4SLinus Torvalds 
791bb003079SIngo Molnar 	mutex_lock(&sel_mutex);
7921da177e4SLinus Torvalds 
7931da177e4SLinus Torvalds 	ret = -EFAULT;
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds 	/* check to see if this file has been deleted */
7961da177e4SLinus Torvalds 	if (!filep->f_op)
7971da177e4SLinus Torvalds 		goto out;
7981da177e4SLinus Torvalds 
799bfd51626SDavi Arnaut 	if (count > PAGE_SIZE) {
8001da177e4SLinus Torvalds 		ret = -EINVAL;
8011da177e4SLinus Torvalds 		goto out;
8021da177e4SLinus Torvalds 	}
8031da177e4SLinus Torvalds 	if (!(page = (char*)get_zeroed_page(GFP_KERNEL))) {
8041da177e4SLinus Torvalds 		ret = -ENOMEM;
8051da177e4SLinus Torvalds 		goto out;
8061da177e4SLinus Torvalds 	}
8071da177e4SLinus Torvalds 
8083d5ff529SJosef Sipek 	inode = filep->f_path.dentry->d_inode;
8091da177e4SLinus Torvalds 	cur_enforcing = security_get_bool_value(inode->i_ino - BOOL_INO_OFFSET);
8101da177e4SLinus Torvalds 	if (cur_enforcing < 0) {
8111da177e4SLinus Torvalds 		ret = cur_enforcing;
8121da177e4SLinus Torvalds 		goto out;
8131da177e4SLinus Torvalds 	}
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds 	length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
8161da177e4SLinus Torvalds 			  bool_pending_values[inode->i_ino - BOOL_INO_OFFSET]);
81768bdcf28SStephen Smalley 	ret = simple_read_from_buffer(buf, count, ppos, page, length);
8181da177e4SLinus Torvalds out:
819bb003079SIngo Molnar 	mutex_unlock(&sel_mutex);
8201da177e4SLinus Torvalds 	if (page)
8211da177e4SLinus Torvalds 		free_page((unsigned long)page);
8221da177e4SLinus Torvalds 	return ret;
8231da177e4SLinus Torvalds }
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
8261da177e4SLinus Torvalds 			      size_t count, loff_t *ppos)
8271da177e4SLinus Torvalds {
8281da177e4SLinus Torvalds 	char *page = NULL;
8291da177e4SLinus Torvalds 	ssize_t length = -EFAULT;
8301da177e4SLinus Torvalds 	int new_value;
8311da177e4SLinus Torvalds 	struct inode *inode;
8321da177e4SLinus Torvalds 
833bb003079SIngo Molnar 	mutex_lock(&sel_mutex);
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds 	length = task_has_security(current, SECURITY__SETBOOL);
8361da177e4SLinus Torvalds 	if (length)
8371da177e4SLinus Torvalds 		goto out;
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds 	/* check to see if this file has been deleted */
8401da177e4SLinus Torvalds 	if (!filep->f_op)
8411da177e4SLinus Torvalds 		goto out;
8421da177e4SLinus Torvalds 
843bfd51626SDavi Arnaut 	if (count >= PAGE_SIZE) {
8441da177e4SLinus Torvalds 		length = -ENOMEM;
8451da177e4SLinus Torvalds 		goto out;
8461da177e4SLinus Torvalds 	}
8471da177e4SLinus Torvalds 	if (*ppos != 0) {
8481da177e4SLinus Torvalds 		/* No partial writes. */
8491da177e4SLinus Torvalds 		goto out;
8501da177e4SLinus Torvalds 	}
8511da177e4SLinus Torvalds 	page = (char*)get_zeroed_page(GFP_KERNEL);
8521da177e4SLinus Torvalds 	if (!page) {
8531da177e4SLinus Torvalds 		length = -ENOMEM;
8541da177e4SLinus Torvalds 		goto out;
8551da177e4SLinus Torvalds 	}
8561da177e4SLinus Torvalds 
8571da177e4SLinus Torvalds 	if (copy_from_user(page, buf, count))
8581da177e4SLinus Torvalds 		goto out;
8591da177e4SLinus Torvalds 
8601da177e4SLinus Torvalds 	length = -EINVAL;
8611da177e4SLinus Torvalds 	if (sscanf(page, "%d", &new_value) != 1)
8621da177e4SLinus Torvalds 		goto out;
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds 	if (new_value)
8651da177e4SLinus Torvalds 		new_value = 1;
8661da177e4SLinus Torvalds 
8673d5ff529SJosef Sipek 	inode = filep->f_path.dentry->d_inode;
8681da177e4SLinus Torvalds 	bool_pending_values[inode->i_ino - BOOL_INO_OFFSET] = new_value;
8691da177e4SLinus Torvalds 	length = count;
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds out:
872bb003079SIngo Molnar 	mutex_unlock(&sel_mutex);
8731da177e4SLinus Torvalds 	if (page)
8741da177e4SLinus Torvalds 		free_page((unsigned long) page);
8751da177e4SLinus Torvalds 	return length;
8761da177e4SLinus Torvalds }
8771da177e4SLinus Torvalds 
878*9c2e08c5SArjan van de Ven static const struct file_operations sel_bool_ops = {
8791da177e4SLinus Torvalds 	.read           = sel_read_bool,
8801da177e4SLinus Torvalds 	.write          = sel_write_bool,
8811da177e4SLinus Torvalds };
8821da177e4SLinus Torvalds 
8831da177e4SLinus Torvalds static ssize_t sel_commit_bools_write(struct file *filep,
8841da177e4SLinus Torvalds 				      const char __user *buf,
8851da177e4SLinus Torvalds 				      size_t count, loff_t *ppos)
8861da177e4SLinus Torvalds {
8871da177e4SLinus Torvalds 	char *page = NULL;
8881da177e4SLinus Torvalds 	ssize_t length = -EFAULT;
8891da177e4SLinus Torvalds 	int new_value;
8901da177e4SLinus Torvalds 
891bb003079SIngo Molnar 	mutex_lock(&sel_mutex);
8921da177e4SLinus Torvalds 
8931da177e4SLinus Torvalds 	length = task_has_security(current, SECURITY__SETBOOL);
8941da177e4SLinus Torvalds 	if (length)
8951da177e4SLinus Torvalds 		goto out;
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds 	/* check to see if this file has been deleted */
8981da177e4SLinus Torvalds 	if (!filep->f_op)
8991da177e4SLinus Torvalds 		goto out;
9001da177e4SLinus Torvalds 
901bfd51626SDavi Arnaut 	if (count >= PAGE_SIZE) {
9021da177e4SLinus Torvalds 		length = -ENOMEM;
9031da177e4SLinus Torvalds 		goto out;
9041da177e4SLinus Torvalds 	}
9051da177e4SLinus Torvalds 	if (*ppos != 0) {
9061da177e4SLinus Torvalds 		/* No partial writes. */
9071da177e4SLinus Torvalds 		goto out;
9081da177e4SLinus Torvalds 	}
9091da177e4SLinus Torvalds 	page = (char*)get_zeroed_page(GFP_KERNEL);
9101da177e4SLinus Torvalds 	if (!page) {
9111da177e4SLinus Torvalds 		length = -ENOMEM;
9121da177e4SLinus Torvalds 		goto out;
9131da177e4SLinus Torvalds 	}
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds 	if (copy_from_user(page, buf, count))
9161da177e4SLinus Torvalds 		goto out;
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds 	length = -EINVAL;
9191da177e4SLinus Torvalds 	if (sscanf(page, "%d", &new_value) != 1)
9201da177e4SLinus Torvalds 		goto out;
9211da177e4SLinus Torvalds 
92220c19e41SDavi Arnaut 	if (new_value && bool_pending_values) {
9231da177e4SLinus Torvalds 		security_set_bools(bool_num, bool_pending_values);
9241da177e4SLinus Torvalds 	}
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds 	length = count;
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds out:
929bb003079SIngo Molnar 	mutex_unlock(&sel_mutex);
9301da177e4SLinus Torvalds 	if (page)
9311da177e4SLinus Torvalds 		free_page((unsigned long) page);
9321da177e4SLinus Torvalds 	return length;
9331da177e4SLinus Torvalds }
9341da177e4SLinus Torvalds 
935*9c2e08c5SArjan van de Ven static const struct file_operations sel_commit_bools_ops = {
9361da177e4SLinus Torvalds 	.write          = sel_commit_bools_write,
9371da177e4SLinus Torvalds };
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds /* delete booleans - partial revoke() from
9401da177e4SLinus Torvalds  * fs/proc/generic.c proc_kill_inodes */
9411da177e4SLinus Torvalds static void sel_remove_bools(struct dentry *de)
9421da177e4SLinus Torvalds {
9431da177e4SLinus Torvalds 	struct list_head *p, *node;
9441da177e4SLinus Torvalds 	struct super_block *sb = de->d_sb;
9451da177e4SLinus Torvalds 
9461da177e4SLinus Torvalds 	spin_lock(&dcache_lock);
9471da177e4SLinus Torvalds 	node = de->d_subdirs.next;
9481da177e4SLinus Torvalds 	while (node != &de->d_subdirs) {
9495160ee6fSEric Dumazet 		struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
9501da177e4SLinus Torvalds 		list_del_init(node);
9511da177e4SLinus Torvalds 
9521da177e4SLinus Torvalds 		if (d->d_inode) {
9531da177e4SLinus Torvalds 			d = dget_locked(d);
9541da177e4SLinus Torvalds 			spin_unlock(&dcache_lock);
9551da177e4SLinus Torvalds 			d_delete(d);
9561da177e4SLinus Torvalds 			simple_unlink(de->d_inode, d);
9571da177e4SLinus Torvalds 			dput(d);
9581da177e4SLinus Torvalds 			spin_lock(&dcache_lock);
9591da177e4SLinus Torvalds 		}
9601da177e4SLinus Torvalds 		node = de->d_subdirs.next;
9611da177e4SLinus Torvalds 	}
9621da177e4SLinus Torvalds 
9631da177e4SLinus Torvalds 	spin_unlock(&dcache_lock);
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds 	file_list_lock();
9661da177e4SLinus Torvalds 	list_for_each(p, &sb->s_files) {
9672f512016SEric Dumazet 		struct file * filp = list_entry(p, struct file, f_u.fu_list);
9683d5ff529SJosef Sipek 		struct dentry * dentry = filp->f_path.dentry;
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds 		if (dentry->d_parent != de) {
9711da177e4SLinus Torvalds 			continue;
9721da177e4SLinus Torvalds 		}
9731da177e4SLinus Torvalds 		filp->f_op = NULL;
9741da177e4SLinus Torvalds 	}
9751da177e4SLinus Torvalds 	file_list_unlock();
9761da177e4SLinus Torvalds }
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds #define BOOL_DIR_NAME "booleans"
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds static int sel_make_bools(void)
9811da177e4SLinus Torvalds {
9821da177e4SLinus Torvalds 	int i, ret = 0;
9831da177e4SLinus Torvalds 	ssize_t len;
9841da177e4SLinus Torvalds 	struct dentry *dentry = NULL;
9851da177e4SLinus Torvalds 	struct dentry *dir = bool_dir;
9861da177e4SLinus Torvalds 	struct inode *inode = NULL;
9871da177e4SLinus Torvalds 	struct inode_security_struct *isec;
9881da177e4SLinus Torvalds 	char **names = NULL, *page;
9891da177e4SLinus Torvalds 	int num;
9901da177e4SLinus Torvalds 	int *values = NULL;
9911da177e4SLinus Torvalds 	u32 sid;
9921da177e4SLinus Torvalds 
9931da177e4SLinus Torvalds 	/* remove any existing files */
9941da177e4SLinus Torvalds 	kfree(bool_pending_values);
99520c19e41SDavi Arnaut 	bool_pending_values = NULL;
9961da177e4SLinus Torvalds 
9971da177e4SLinus Torvalds 	sel_remove_bools(dir);
9981da177e4SLinus Torvalds 
9991da177e4SLinus Torvalds 	if (!(page = (char*)get_zeroed_page(GFP_KERNEL)))
10001da177e4SLinus Torvalds 		return -ENOMEM;
10011da177e4SLinus Torvalds 
10021da177e4SLinus Torvalds 	ret = security_get_bools(&num, &names, &values);
10031da177e4SLinus Torvalds 	if (ret != 0)
10041da177e4SLinus Torvalds 		goto out;
10051da177e4SLinus Torvalds 
10061da177e4SLinus Torvalds 	for (i = 0; i < num; i++) {
10071da177e4SLinus Torvalds 		dentry = d_alloc_name(dir, names[i]);
10081da177e4SLinus Torvalds 		if (!dentry) {
10091da177e4SLinus Torvalds 			ret = -ENOMEM;
10101da177e4SLinus Torvalds 			goto err;
10111da177e4SLinus Torvalds 		}
10121da177e4SLinus Torvalds 		inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
10131da177e4SLinus Torvalds 		if (!inode) {
10141da177e4SLinus Torvalds 			ret = -ENOMEM;
10151da177e4SLinus Torvalds 			goto err;
10161da177e4SLinus Torvalds 		}
10171da177e4SLinus Torvalds 
10181da177e4SLinus Torvalds 		len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
10191da177e4SLinus Torvalds 		if (len < 0) {
10201da177e4SLinus Torvalds 			ret = -EINVAL;
10211da177e4SLinus Torvalds 			goto err;
10221da177e4SLinus Torvalds 		} else if (len >= PAGE_SIZE) {
10231da177e4SLinus Torvalds 			ret = -ENAMETOOLONG;
10241da177e4SLinus Torvalds 			goto err;
10251da177e4SLinus Torvalds 		}
10261da177e4SLinus Torvalds 		isec = (struct inode_security_struct*)inode->i_security;
10271da177e4SLinus Torvalds 		if ((ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid)))
10281da177e4SLinus Torvalds 			goto err;
10291da177e4SLinus Torvalds 		isec->sid = sid;
10301da177e4SLinus Torvalds 		isec->initialized = 1;
10311da177e4SLinus Torvalds 		inode->i_fop = &sel_bool_ops;
10321da177e4SLinus Torvalds 		inode->i_ino = i + BOOL_INO_OFFSET;
10331da177e4SLinus Torvalds 		d_add(dentry, inode);
10341da177e4SLinus Torvalds 	}
10351da177e4SLinus Torvalds 	bool_num = num;
10361da177e4SLinus Torvalds 	bool_pending_values = values;
10371da177e4SLinus Torvalds out:
10381da177e4SLinus Torvalds 	free_page((unsigned long)page);
10391da177e4SLinus Torvalds 	if (names) {
10409a5f04bfSJesper Juhl 		for (i = 0; i < num; i++)
10411da177e4SLinus Torvalds 			kfree(names[i]);
10421da177e4SLinus Torvalds 		kfree(names);
10431da177e4SLinus Torvalds 	}
10441da177e4SLinus Torvalds 	return ret;
10451da177e4SLinus Torvalds err:
104620c19e41SDavi Arnaut 	kfree(values);
1047253a8b1dSJames Morris 	sel_remove_bools(dir);
10481da177e4SLinus Torvalds 	ret = -ENOMEM;
10491da177e4SLinus Torvalds 	goto out;
10501da177e4SLinus Torvalds }
10511da177e4SLinus Torvalds 
10521da177e4SLinus Torvalds #define NULL_FILE_NAME "null"
10531da177e4SLinus Torvalds 
10541da177e4SLinus Torvalds struct dentry *selinux_null = NULL;
10551da177e4SLinus Torvalds 
10561da177e4SLinus Torvalds static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
10571da177e4SLinus Torvalds 					    size_t count, loff_t *ppos)
10581da177e4SLinus Torvalds {
10591da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
10601da177e4SLinus Torvalds 	ssize_t length;
10611da177e4SLinus Torvalds 
10621da177e4SLinus Torvalds 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
10631da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
10641da177e4SLinus Torvalds }
10651da177e4SLinus Torvalds 
10661da177e4SLinus Torvalds static ssize_t sel_write_avc_cache_threshold(struct file * file,
10671da177e4SLinus Torvalds 					     const char __user * buf,
10681da177e4SLinus Torvalds 					     size_t count, loff_t *ppos)
10691da177e4SLinus Torvalds 
10701da177e4SLinus Torvalds {
10711da177e4SLinus Torvalds 	char *page;
10721da177e4SLinus Torvalds 	ssize_t ret;
10731da177e4SLinus Torvalds 	int new_value;
10741da177e4SLinus Torvalds 
1075bfd51626SDavi Arnaut 	if (count >= PAGE_SIZE) {
10761da177e4SLinus Torvalds 		ret = -ENOMEM;
10771da177e4SLinus Torvalds 		goto out;
10781da177e4SLinus Torvalds 	}
10791da177e4SLinus Torvalds 
10801da177e4SLinus Torvalds 	if (*ppos != 0) {
10811da177e4SLinus Torvalds 		/* No partial writes. */
10821da177e4SLinus Torvalds 		ret = -EINVAL;
10831da177e4SLinus Torvalds 		goto out;
10841da177e4SLinus Torvalds 	}
10851da177e4SLinus Torvalds 
10861da177e4SLinus Torvalds 	page = (char*)get_zeroed_page(GFP_KERNEL);
10871da177e4SLinus Torvalds 	if (!page) {
10881da177e4SLinus Torvalds 		ret = -ENOMEM;
10891da177e4SLinus Torvalds 		goto out;
10901da177e4SLinus Torvalds 	}
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds 	if (copy_from_user(page, buf, count)) {
10931da177e4SLinus Torvalds 		ret = -EFAULT;
10941da177e4SLinus Torvalds 		goto out_free;
10951da177e4SLinus Torvalds 	}
10961da177e4SLinus Torvalds 
10971da177e4SLinus Torvalds 	if (sscanf(page, "%u", &new_value) != 1) {
10981da177e4SLinus Torvalds 		ret = -EINVAL;
10991da177e4SLinus Torvalds 		goto out;
11001da177e4SLinus Torvalds 	}
11011da177e4SLinus Torvalds 
11021da177e4SLinus Torvalds 	if (new_value != avc_cache_threshold) {
11031da177e4SLinus Torvalds 		ret = task_has_security(current, SECURITY__SETSECPARAM);
11041da177e4SLinus Torvalds 		if (ret)
11051da177e4SLinus Torvalds 			goto out_free;
11061da177e4SLinus Torvalds 		avc_cache_threshold = new_value;
11071da177e4SLinus Torvalds 	}
11081da177e4SLinus Torvalds 	ret = count;
11091da177e4SLinus Torvalds out_free:
11101da177e4SLinus Torvalds 	free_page((unsigned long)page);
11111da177e4SLinus Torvalds out:
11121da177e4SLinus Torvalds 	return ret;
11131da177e4SLinus Torvalds }
11141da177e4SLinus Torvalds 
11151da177e4SLinus Torvalds static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
11161da177e4SLinus Torvalds 				       size_t count, loff_t *ppos)
11171da177e4SLinus Torvalds {
11181da177e4SLinus Torvalds 	char *page;
11191da177e4SLinus Torvalds 	ssize_t ret = 0;
11201da177e4SLinus Torvalds 
11211da177e4SLinus Torvalds 	page = (char *)__get_free_page(GFP_KERNEL);
11221da177e4SLinus Torvalds 	if (!page) {
11231da177e4SLinus Torvalds 		ret = -ENOMEM;
11241da177e4SLinus Torvalds 		goto out;
11251da177e4SLinus Torvalds 	}
11261da177e4SLinus Torvalds 	ret = avc_get_hash_stats(page);
11271da177e4SLinus Torvalds 	if (ret >= 0)
11281da177e4SLinus Torvalds 		ret = simple_read_from_buffer(buf, count, ppos, page, ret);
11291da177e4SLinus Torvalds 	free_page((unsigned long)page);
11301da177e4SLinus Torvalds out:
11311da177e4SLinus Torvalds 	return ret;
11321da177e4SLinus Torvalds }
11331da177e4SLinus Torvalds 
1134*9c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_threshold_ops = {
11351da177e4SLinus Torvalds 	.read		= sel_read_avc_cache_threshold,
11361da177e4SLinus Torvalds 	.write		= sel_write_avc_cache_threshold,
11371da177e4SLinus Torvalds };
11381da177e4SLinus Torvalds 
1139*9c2e08c5SArjan van de Ven static const struct file_operations sel_avc_hash_stats_ops = {
11401da177e4SLinus Torvalds 	.read		= sel_read_avc_hash_stats,
11411da177e4SLinus Torvalds };
11421da177e4SLinus Torvalds 
11431da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
11441da177e4SLinus Torvalds static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
11451da177e4SLinus Torvalds {
11461da177e4SLinus Torvalds 	int cpu;
11471da177e4SLinus Torvalds 
11481da177e4SLinus Torvalds 	for (cpu = *idx; cpu < NR_CPUS; ++cpu) {
11491da177e4SLinus Torvalds 		if (!cpu_possible(cpu))
11501da177e4SLinus Torvalds 			continue;
11511da177e4SLinus Torvalds 		*idx = cpu + 1;
11521da177e4SLinus Torvalds 		return &per_cpu(avc_cache_stats, cpu);
11531da177e4SLinus Torvalds 	}
11541da177e4SLinus Torvalds 	return NULL;
11551da177e4SLinus Torvalds }
11561da177e4SLinus Torvalds 
11571da177e4SLinus Torvalds static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
11581da177e4SLinus Torvalds {
11591da177e4SLinus Torvalds 	loff_t n = *pos - 1;
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds 	if (*pos == 0)
11621da177e4SLinus Torvalds 		return SEQ_START_TOKEN;
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds 	return sel_avc_get_stat_idx(&n);
11651da177e4SLinus Torvalds }
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
11681da177e4SLinus Torvalds {
11691da177e4SLinus Torvalds 	return sel_avc_get_stat_idx(pos);
11701da177e4SLinus Torvalds }
11711da177e4SLinus Torvalds 
11721da177e4SLinus Torvalds static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
11731da177e4SLinus Torvalds {
11741da177e4SLinus Torvalds 	struct avc_cache_stats *st = v;
11751da177e4SLinus Torvalds 
11761da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
11771da177e4SLinus Torvalds 		seq_printf(seq, "lookups hits misses allocations reclaims "
11781da177e4SLinus Torvalds 			   "frees\n");
11791da177e4SLinus Torvalds 	else
11801da177e4SLinus Torvalds 		seq_printf(seq, "%u %u %u %u %u %u\n", st->lookups,
11811da177e4SLinus Torvalds 			   st->hits, st->misses, st->allocations,
11821da177e4SLinus Torvalds 			   st->reclaims, st->frees);
11831da177e4SLinus Torvalds 	return 0;
11841da177e4SLinus Torvalds }
11851da177e4SLinus Torvalds 
11861da177e4SLinus Torvalds static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
11871da177e4SLinus Torvalds { }
11881da177e4SLinus Torvalds 
11891da177e4SLinus Torvalds static struct seq_operations sel_avc_cache_stats_seq_ops = {
11901da177e4SLinus Torvalds 	.start		= sel_avc_stats_seq_start,
11911da177e4SLinus Torvalds 	.next		= sel_avc_stats_seq_next,
11921da177e4SLinus Torvalds 	.show		= sel_avc_stats_seq_show,
11931da177e4SLinus Torvalds 	.stop		= sel_avc_stats_seq_stop,
11941da177e4SLinus Torvalds };
11951da177e4SLinus Torvalds 
11961da177e4SLinus Torvalds static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
11971da177e4SLinus Torvalds {
11981da177e4SLinus Torvalds 	return seq_open(file, &sel_avc_cache_stats_seq_ops);
11991da177e4SLinus Torvalds }
12001da177e4SLinus Torvalds 
1201*9c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_stats_ops = {
12021da177e4SLinus Torvalds 	.open		= sel_open_avc_cache_stats,
12031da177e4SLinus Torvalds 	.read		= seq_read,
12041da177e4SLinus Torvalds 	.llseek		= seq_lseek,
12051da177e4SLinus Torvalds 	.release	= seq_release,
12061da177e4SLinus Torvalds };
12071da177e4SLinus Torvalds #endif
12081da177e4SLinus Torvalds 
12091da177e4SLinus Torvalds static int sel_make_avc_files(struct dentry *dir)
12101da177e4SLinus Torvalds {
12111da177e4SLinus Torvalds 	int i, ret = 0;
12121da177e4SLinus Torvalds 	static struct tree_descr files[] = {
12131da177e4SLinus Torvalds 		{ "cache_threshold",
12141da177e4SLinus Torvalds 		  &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
12151da177e4SLinus Torvalds 		{ "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
12161da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
12171da177e4SLinus Torvalds 		{ "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
12181da177e4SLinus Torvalds #endif
12191da177e4SLinus Torvalds 	};
12201da177e4SLinus Torvalds 
12216e20a64aSNicolas Kaiser 	for (i = 0; i < ARRAY_SIZE(files); i++) {
12221da177e4SLinus Torvalds 		struct inode *inode;
12231da177e4SLinus Torvalds 		struct dentry *dentry;
12241da177e4SLinus Torvalds 
12251da177e4SLinus Torvalds 		dentry = d_alloc_name(dir, files[i].name);
12261da177e4SLinus Torvalds 		if (!dentry) {
12271da177e4SLinus Torvalds 			ret = -ENOMEM;
1228d6aafa65SJames Morris 			goto out;
12291da177e4SLinus Torvalds 		}
12301da177e4SLinus Torvalds 
12311da177e4SLinus Torvalds 		inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
12321da177e4SLinus Torvalds 		if (!inode) {
12331da177e4SLinus Torvalds 			ret = -ENOMEM;
1234d6aafa65SJames Morris 			goto out;
12351da177e4SLinus Torvalds 		}
12361da177e4SLinus Torvalds 		inode->i_fop = files[i].ops;
12371da177e4SLinus Torvalds 		d_add(dentry, inode);
12381da177e4SLinus Torvalds 	}
12391da177e4SLinus Torvalds out:
12401da177e4SLinus Torvalds 	return ret;
12411da177e4SLinus Torvalds }
12421da177e4SLinus Torvalds 
1243edb20fb5SJames Morris static int sel_make_dir(struct inode *dir, struct dentry *dentry)
12441da177e4SLinus Torvalds {
12451da177e4SLinus Torvalds 	int ret = 0;
12461da177e4SLinus Torvalds 	struct inode *inode;
12471da177e4SLinus Torvalds 
1248edb20fb5SJames Morris 	inode = sel_make_inode(dir->i_sb, S_IFDIR | S_IRUGO | S_IXUGO);
12491da177e4SLinus Torvalds 	if (!inode) {
12501da177e4SLinus Torvalds 		ret = -ENOMEM;
12511da177e4SLinus Torvalds 		goto out;
12521da177e4SLinus Torvalds 	}
12531da177e4SLinus Torvalds 	inode->i_op = &simple_dir_inode_operations;
12541da177e4SLinus Torvalds 	inode->i_fop = &simple_dir_operations;
125540e906f8SJames Morris 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
1256d8c76e6fSDave Hansen 	inc_nlink(inode);
12571da177e4SLinus Torvalds 	d_add(dentry, inode);
1258edb20fb5SJames Morris 	/* bump link count on parent directory, too */
1259d8c76e6fSDave Hansen 	inc_nlink(dir);
12601da177e4SLinus Torvalds out:
12611da177e4SLinus Torvalds 	return ret;
12621da177e4SLinus Torvalds }
12631da177e4SLinus Torvalds 
12641da177e4SLinus Torvalds static int sel_fill_super(struct super_block * sb, void * data, int silent)
12651da177e4SLinus Torvalds {
12661da177e4SLinus Torvalds 	int ret;
12671da177e4SLinus Torvalds 	struct dentry *dentry;
1268edb20fb5SJames Morris 	struct inode *inode, *root_inode;
12691da177e4SLinus Torvalds 	struct inode_security_struct *isec;
12701da177e4SLinus Torvalds 
12711da177e4SLinus Torvalds 	static struct tree_descr selinux_files[] = {
12721da177e4SLinus Torvalds 		[SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
12731da177e4SLinus Torvalds 		[SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
1274ce9982d0SStephen Smalley 		[SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
12751da177e4SLinus Torvalds 		[SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
12761da177e4SLinus Torvalds 		[SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
12771da177e4SLinus Torvalds 		[SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
12781da177e4SLinus Torvalds 		[SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
12791da177e4SLinus Torvalds 		[SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
12801da177e4SLinus Torvalds 		[SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
12811da177e4SLinus Torvalds 		[SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
12821da177e4SLinus Torvalds 		[SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
12831da177e4SLinus Torvalds 		[SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
12841da177e4SLinus Torvalds 		[SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
12854e5ab4cbSJames Morris 		[SEL_COMPAT_NET] = {"compat_net", &sel_compat_net_ops, S_IRUGO|S_IWUSR},
12861da177e4SLinus Torvalds 		/* last one */ {""}
12871da177e4SLinus Torvalds 	};
12881da177e4SLinus Torvalds 	ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
12891da177e4SLinus Torvalds 	if (ret)
1290161ce45aSJames Morris 		goto err;
12911da177e4SLinus Torvalds 
1292edb20fb5SJames Morris 	root_inode = sb->s_root->d_inode;
1293edb20fb5SJames Morris 
12941da177e4SLinus Torvalds 	dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME);
1295161ce45aSJames Morris 	if (!dentry) {
1296161ce45aSJames Morris 		ret = -ENOMEM;
1297161ce45aSJames Morris 		goto err;
1298161ce45aSJames Morris 	}
12991da177e4SLinus Torvalds 
1300edb20fb5SJames Morris 	ret = sel_make_dir(root_inode, dentry);
1301cde174a8SJames Morris 	if (ret)
1302161ce45aSJames Morris 		goto err;
1303cde174a8SJames Morris 
13041da177e4SLinus Torvalds 	bool_dir = dentry;
13051da177e4SLinus Torvalds 
13061da177e4SLinus Torvalds 	dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
1307161ce45aSJames Morris 	if (!dentry) {
1308161ce45aSJames Morris 		ret = -ENOMEM;
1309161ce45aSJames Morris 		goto err;
1310161ce45aSJames Morris 	}
13111da177e4SLinus Torvalds 
13121da177e4SLinus Torvalds 	inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
1313161ce45aSJames Morris 	if (!inode) {
1314161ce45aSJames Morris 		ret = -ENOMEM;
1315161ce45aSJames Morris 		goto err;
1316161ce45aSJames Morris 	}
13171da177e4SLinus Torvalds 	isec = (struct inode_security_struct*)inode->i_security;
13181da177e4SLinus Torvalds 	isec->sid = SECINITSID_DEVNULL;
13191da177e4SLinus Torvalds 	isec->sclass = SECCLASS_CHR_FILE;
13201da177e4SLinus Torvalds 	isec->initialized = 1;
13211da177e4SLinus Torvalds 
13221da177e4SLinus Torvalds 	init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
13231da177e4SLinus Torvalds 	d_add(dentry, inode);
13241da177e4SLinus Torvalds 	selinux_null = dentry;
13251da177e4SLinus Torvalds 
13261da177e4SLinus Torvalds 	dentry = d_alloc_name(sb->s_root, "avc");
1327161ce45aSJames Morris 	if (!dentry) {
1328161ce45aSJames Morris 		ret = -ENOMEM;
1329161ce45aSJames Morris 		goto err;
1330161ce45aSJames Morris 	}
13311da177e4SLinus Torvalds 
1332edb20fb5SJames Morris 	ret = sel_make_dir(root_inode, dentry);
13331da177e4SLinus Torvalds 	if (ret)
1334161ce45aSJames Morris 		goto err;
13351da177e4SLinus Torvalds 
13361da177e4SLinus Torvalds 	ret = sel_make_avc_files(dentry);
13371da177e4SLinus Torvalds 	if (ret)
1338161ce45aSJames Morris 		goto err;
13391da177e4SLinus Torvalds out:
1340161ce45aSJames Morris 	return ret;
1341161ce45aSJames Morris err:
13421da177e4SLinus Torvalds 	printk(KERN_ERR "%s:  failed while creating inodes\n", __FUNCTION__);
1343161ce45aSJames Morris 	goto out;
13441da177e4SLinus Torvalds }
13451da177e4SLinus Torvalds 
1346454e2398SDavid Howells static int sel_get_sb(struct file_system_type *fs_type,
1347454e2398SDavid Howells 		      int flags, const char *dev_name, void *data,
1348454e2398SDavid Howells 		      struct vfsmount *mnt)
13491da177e4SLinus Torvalds {
1350454e2398SDavid Howells 	return get_sb_single(fs_type, flags, data, sel_fill_super, mnt);
13511da177e4SLinus Torvalds }
13521da177e4SLinus Torvalds 
13531da177e4SLinus Torvalds static struct file_system_type sel_fs_type = {
13541da177e4SLinus Torvalds 	.name		= "selinuxfs",
13551da177e4SLinus Torvalds 	.get_sb		= sel_get_sb,
13561da177e4SLinus Torvalds 	.kill_sb	= kill_litter_super,
13571da177e4SLinus Torvalds };
13581da177e4SLinus Torvalds 
13591da177e4SLinus Torvalds struct vfsmount *selinuxfs_mount;
13601da177e4SLinus Torvalds 
13611da177e4SLinus Torvalds static int __init init_sel_fs(void)
13621da177e4SLinus Torvalds {
13631da177e4SLinus Torvalds 	int err;
13641da177e4SLinus Torvalds 
13651da177e4SLinus Torvalds 	if (!selinux_enabled)
13661da177e4SLinus Torvalds 		return 0;
13671da177e4SLinus Torvalds 	err = register_filesystem(&sel_fs_type);
13681da177e4SLinus Torvalds 	if (!err) {
13691da177e4SLinus Torvalds 		selinuxfs_mount = kern_mount(&sel_fs_type);
13701da177e4SLinus Torvalds 		if (IS_ERR(selinuxfs_mount)) {
13711da177e4SLinus Torvalds 			printk(KERN_ERR "selinuxfs:  could not mount!\n");
13721da177e4SLinus Torvalds 			err = PTR_ERR(selinuxfs_mount);
13731da177e4SLinus Torvalds 			selinuxfs_mount = NULL;
13741da177e4SLinus Torvalds 		}
13751da177e4SLinus Torvalds 	}
13761da177e4SLinus Torvalds 	return err;
13771da177e4SLinus Torvalds }
13781da177e4SLinus Torvalds 
13791da177e4SLinus Torvalds __initcall(init_sel_fs);
13801da177e4SLinus Torvalds 
13811da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
13821da177e4SLinus Torvalds void exit_sel_fs(void)
13831da177e4SLinus Torvalds {
13841da177e4SLinus Torvalds 	unregister_filesystem(&sel_fs_type);
13851da177e4SLinus Torvalds }
13861da177e4SLinus Torvalds #endif
1387