xref: /linux/security/selinux/selinuxfs.c (revision 496ad9aa)
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>
22bb003079SIngo Molnar #include <linux/mutex.h>
231da177e4SLinus Torvalds #include <linux/init.h>
241da177e4SLinus Torvalds #include <linux/string.h>
251da177e4SLinus Torvalds #include <linux/security.h>
261da177e4SLinus Torvalds #include <linux/major.h>
271da177e4SLinus Torvalds #include <linux/seq_file.h>
281da177e4SLinus Torvalds #include <linux/percpu.h>
29af601e46SSteve Grubb #include <linux/audit.h>
30f5269710SEric Paris #include <linux/uaccess.h>
317a627e3bSGreg Kroah-Hartman #include <linux/kobject.h>
320f7e4c33SKohei Kaigai #include <linux/ctype.h>
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds /* selinuxfs pseudo filesystem for exporting the security policy API.
351da177e4SLinus Torvalds    Based on the proc code and the fs/nfsd/nfsctl.c code. */
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds #include "flask.h"
381da177e4SLinus Torvalds #include "avc.h"
391da177e4SLinus Torvalds #include "avc_ss.h"
401da177e4SLinus Torvalds #include "security.h"
411da177e4SLinus Torvalds #include "objsec.h"
421da177e4SLinus Torvalds #include "conditional.h"
431da177e4SLinus Torvalds 
443bb56b25SPaul Moore /* Policy capability filenames */
453bb56b25SPaul Moore static char *policycap_names[] = {
46b0c636b9SEric Paris 	"network_peer_controls",
47b0c636b9SEric Paris 	"open_perms"
483bb56b25SPaul Moore };
493bb56b25SPaul Moore 
501da177e4SLinus Torvalds unsigned int selinux_checkreqprot = CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds static int __init checkreqprot_setup(char *str)
531da177e4SLinus Torvalds {
54f5269710SEric Paris 	unsigned long checkreqprot;
55f5269710SEric Paris 	if (!strict_strtoul(str, 0, &checkreqprot))
56f5269710SEric Paris 		selinux_checkreqprot = checkreqprot ? 1 : 0;
571da177e4SLinus Torvalds 	return 1;
581da177e4SLinus Torvalds }
591da177e4SLinus Torvalds __setup("checkreqprot=", checkreqprot_setup);
601da177e4SLinus Torvalds 
61bb003079SIngo Molnar static DEFINE_MUTEX(sel_mutex);
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds /* global data for booleans */
641872981bSEric Paris static struct dentry *bool_dir;
651872981bSEric Paris static int bool_num;
66d313f948SStephen Smalley static char **bool_pending_names;
671872981bSEric Paris static int *bool_pending_values;
681da177e4SLinus Torvalds 
69e47c8fc5SChristopher J. PeBenito /* global data for classes */
701872981bSEric Paris static struct dentry *class_dir;
71e47c8fc5SChristopher J. PeBenito static unsigned long last_class_ino;
72e47c8fc5SChristopher J. PeBenito 
73cee74f47SEric Paris static char policy_opened;
74cee74f47SEric Paris 
753bb56b25SPaul Moore /* global data for policy capabilities */
761872981bSEric Paris static struct dentry *policycap_dir;
773bb56b25SPaul Moore 
781da177e4SLinus Torvalds /* Check whether a task is allowed to use a security operation. */
791da177e4SLinus Torvalds static int task_has_security(struct task_struct *tsk,
801da177e4SLinus Torvalds 			     u32 perms)
811da177e4SLinus Torvalds {
82c69e8d9cSDavid Howells 	const struct task_security_struct *tsec;
83c69e8d9cSDavid Howells 	u32 sid = 0;
841da177e4SLinus Torvalds 
85c69e8d9cSDavid Howells 	rcu_read_lock();
86c69e8d9cSDavid Howells 	tsec = __task_cred(tsk)->security;
87c69e8d9cSDavid Howells 	if (tsec)
88c69e8d9cSDavid Howells 		sid = tsec->sid;
89c69e8d9cSDavid Howells 	rcu_read_unlock();
901da177e4SLinus Torvalds 	if (!tsec)
911da177e4SLinus Torvalds 		return -EACCES;
921da177e4SLinus Torvalds 
93c69e8d9cSDavid Howells 	return avc_has_perm(sid, SECINITSID_SECURITY,
941da177e4SLinus Torvalds 			    SECCLASS_SECURITY, perms, NULL);
951da177e4SLinus Torvalds }
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds enum sel_inos {
981da177e4SLinus Torvalds 	SEL_ROOT_INO = 2,
991da177e4SLinus Torvalds 	SEL_LOAD,	/* load policy */
1001da177e4SLinus Torvalds 	SEL_ENFORCE,	/* get or set enforcing status */
1011da177e4SLinus Torvalds 	SEL_CONTEXT,	/* validate context */
1021da177e4SLinus Torvalds 	SEL_ACCESS,	/* compute access decision */
1031da177e4SLinus Torvalds 	SEL_CREATE,	/* compute create labeling decision */
1041da177e4SLinus Torvalds 	SEL_RELABEL,	/* compute relabeling decision */
1051da177e4SLinus Torvalds 	SEL_USER,	/* compute reachable user contexts */
1061da177e4SLinus Torvalds 	SEL_POLICYVERS,	/* return policy version for this kernel */
1071da177e4SLinus Torvalds 	SEL_COMMIT_BOOLS, /* commit new boolean values */
1081da177e4SLinus Torvalds 	SEL_MLS,	/* return if MLS policy is enabled */
1091da177e4SLinus Torvalds 	SEL_DISABLE,	/* disable SELinux until next reboot */
1101da177e4SLinus Torvalds 	SEL_MEMBER,	/* compute polyinstantiation membership decision */
1111da177e4SLinus Torvalds 	SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
1124e5ab4cbSJames Morris 	SEL_COMPAT_NET,	/* whether to use old compat network packet controls */
1133f12070eSEric Paris 	SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */
1143f12070eSEric Paris 	SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */
11511904167SKaiGai Kohei 	SEL_STATUS,	/* export current status using mmap() */
116cee74f47SEric Paris 	SEL_POLICY,	/* allow userspace to read the in kernel policy */
1176174eafcSJames Carter 	SEL_INO_NEXT,	/* The next inode number to use */
1181da177e4SLinus Torvalds };
1191da177e4SLinus Torvalds 
1206174eafcSJames Carter static unsigned long sel_last_ino = SEL_INO_NEXT - 1;
1216174eafcSJames Carter 
122f0ee2e46SJames Carter #define SEL_INITCON_INO_OFFSET		0x01000000
123bce34bc0SJames Carter #define SEL_BOOL_INO_OFFSET		0x02000000
124e47c8fc5SChristopher J. PeBenito #define SEL_CLASS_INO_OFFSET		0x04000000
1253bb56b25SPaul Moore #define SEL_POLICYCAP_INO_OFFSET	0x08000000
126f0ee2e46SJames Carter #define SEL_INO_MASK			0x00ffffff
127f0ee2e46SJames Carter 
1281da177e4SLinus Torvalds #define TMPBUFLEN	12
1291da177e4SLinus Torvalds static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
1301da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
1311da177e4SLinus Torvalds {
1321da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
1331da177e4SLinus Torvalds 	ssize_t length;
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 	length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing);
1361da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1371da177e4SLinus Torvalds }
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
1401da177e4SLinus Torvalds static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
1411da177e4SLinus Torvalds 				 size_t count, loff_t *ppos)
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds {
144b77a493bSEric Paris 	char *page = NULL;
1451da177e4SLinus Torvalds 	ssize_t length;
1461da177e4SLinus Torvalds 	int new_value;
1471da177e4SLinus Torvalds 
148b77a493bSEric Paris 	length = -ENOMEM;
149bfd51626SDavi Arnaut 	if (count >= PAGE_SIZE)
150b77a493bSEric Paris 		goto out;
151b77a493bSEric Paris 
1521da177e4SLinus Torvalds 	/* No partial writes. */
153b77a493bSEric Paris 	length = EINVAL;
154b77a493bSEric Paris 	if (*ppos != 0)
155b77a493bSEric Paris 		goto out;
156b77a493bSEric Paris 
157b77a493bSEric Paris 	length = -ENOMEM;
1581da177e4SLinus Torvalds 	page = (char *)get_zeroed_page(GFP_KERNEL);
1591da177e4SLinus Torvalds 	if (!page)
160b77a493bSEric Paris 		goto out;
161b77a493bSEric Paris 
1621da177e4SLinus Torvalds 	length = -EFAULT;
1631da177e4SLinus Torvalds 	if (copy_from_user(page, buf, count))
1641da177e4SLinus Torvalds 		goto out;
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	length = -EINVAL;
1671da177e4SLinus Torvalds 	if (sscanf(page, "%d", &new_value) != 1)
1681da177e4SLinus Torvalds 		goto out;
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds 	if (new_value != selinux_enforcing) {
1711da177e4SLinus Torvalds 		length = task_has_security(current, SECURITY__SETENFORCE);
1721da177e4SLinus Torvalds 		if (length)
1731da177e4SLinus Torvalds 			goto out;
174af601e46SSteve Grubb 		audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
1754746ec5bSEric Paris 			"enforcing=%d old_enforcing=%d auid=%u ses=%u",
1764746ec5bSEric Paris 			new_value, selinux_enforcing,
177581abc09SEric W. Biederman 			from_kuid(&init_user_ns, audit_get_loginuid(current)),
1784746ec5bSEric Paris 			audit_get_sessionid(current));
1791da177e4SLinus Torvalds 		selinux_enforcing = new_value;
1801da177e4SLinus Torvalds 		if (selinux_enforcing)
1811da177e4SLinus Torvalds 			avc_ss_reset(0);
1821da177e4SLinus Torvalds 		selnl_notify_setenforce(selinux_enforcing);
18311904167SKaiGai Kohei 		selinux_status_update_setenforce(selinux_enforcing);
1841da177e4SLinus Torvalds 	}
1851da177e4SLinus Torvalds 	length = count;
1861da177e4SLinus Torvalds out:
1871da177e4SLinus Torvalds 	free_page((unsigned long) 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 {
2033f12070eSEric Paris 	char tmpbuf[TMPBUFLEN];
2043f12070eSEric Paris 	ssize_t length;
205*496ad9aaSAl Viro 	ino_t ino = file_inode(filp)->i_ino;
2063f12070eSEric Paris 	int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
2073f12070eSEric Paris 		security_get_reject_unknown() : !security_get_allow_unknown();
2083f12070eSEric Paris 
2093f12070eSEric Paris 	length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
2103f12070eSEric Paris 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
2113f12070eSEric Paris }
2123f12070eSEric Paris 
2133f12070eSEric Paris static const struct file_operations sel_handle_unknown_ops = {
2143f12070eSEric Paris 	.read		= sel_read_handle_unknown,
21557a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
2163f12070eSEric Paris };
2173f12070eSEric Paris 
21811904167SKaiGai Kohei static int sel_open_handle_status(struct inode *inode, struct file *filp)
21911904167SKaiGai Kohei {
22011904167SKaiGai Kohei 	struct page    *status = selinux_kernel_status_page();
22111904167SKaiGai Kohei 
22211904167SKaiGai Kohei 	if (!status)
22311904167SKaiGai Kohei 		return -ENOMEM;
22411904167SKaiGai Kohei 
22511904167SKaiGai Kohei 	filp->private_data = status;
22611904167SKaiGai Kohei 
22711904167SKaiGai Kohei 	return 0;
22811904167SKaiGai Kohei }
22911904167SKaiGai Kohei 
23011904167SKaiGai Kohei static ssize_t sel_read_handle_status(struct file *filp, char __user *buf,
23111904167SKaiGai Kohei 				      size_t count, loff_t *ppos)
23211904167SKaiGai Kohei {
23311904167SKaiGai Kohei 	struct page    *status = filp->private_data;
23411904167SKaiGai Kohei 
23511904167SKaiGai Kohei 	BUG_ON(!status);
23611904167SKaiGai Kohei 
23711904167SKaiGai Kohei 	return simple_read_from_buffer(buf, count, ppos,
23811904167SKaiGai Kohei 				       page_address(status),
23911904167SKaiGai Kohei 				       sizeof(struct selinux_kernel_status));
24011904167SKaiGai Kohei }
24111904167SKaiGai Kohei 
24211904167SKaiGai Kohei static int sel_mmap_handle_status(struct file *filp,
24311904167SKaiGai Kohei 				  struct vm_area_struct *vma)
24411904167SKaiGai Kohei {
24511904167SKaiGai Kohei 	struct page    *status = filp->private_data;
24611904167SKaiGai Kohei 	unsigned long	size = vma->vm_end - vma->vm_start;
24711904167SKaiGai Kohei 
24811904167SKaiGai Kohei 	BUG_ON(!status);
24911904167SKaiGai Kohei 
25011904167SKaiGai Kohei 	/* only allows one page from the head */
25111904167SKaiGai Kohei 	if (vma->vm_pgoff > 0 || size != PAGE_SIZE)
25211904167SKaiGai Kohei 		return -EIO;
25311904167SKaiGai Kohei 	/* disallow writable mapping */
25411904167SKaiGai Kohei 	if (vma->vm_flags & VM_WRITE)
25511904167SKaiGai Kohei 		return -EPERM;
25611904167SKaiGai Kohei 	/* disallow mprotect() turns it into writable */
25711904167SKaiGai Kohei 	vma->vm_flags &= ~VM_MAYWRITE;
25811904167SKaiGai Kohei 
25911904167SKaiGai Kohei 	return remap_pfn_range(vma, vma->vm_start,
26011904167SKaiGai Kohei 			       page_to_pfn(status),
26111904167SKaiGai Kohei 			       size, vma->vm_page_prot);
26211904167SKaiGai Kohei }
26311904167SKaiGai Kohei 
26411904167SKaiGai Kohei static const struct file_operations sel_handle_status_ops = {
26511904167SKaiGai Kohei 	.open		= sel_open_handle_status,
26611904167SKaiGai Kohei 	.read		= sel_read_handle_status,
26711904167SKaiGai Kohei 	.mmap		= sel_mmap_handle_status,
26811904167SKaiGai Kohei 	.llseek		= generic_file_llseek,
26911904167SKaiGai Kohei };
27011904167SKaiGai Kohei 
2711da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
2721da177e4SLinus Torvalds static ssize_t sel_write_disable(struct file *file, const char __user *buf,
2731da177e4SLinus Torvalds 				 size_t count, loff_t *ppos)
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds {
276b77a493bSEric Paris 	char *page = NULL;
2771da177e4SLinus Torvalds 	ssize_t length;
2781da177e4SLinus Torvalds 	int new_value;
2791da177e4SLinus Torvalds 
280b77a493bSEric Paris 	length = -ENOMEM;
281bfd51626SDavi Arnaut 	if (count >= PAGE_SIZE)
2826eab04a8SJustin P. Mattock 		goto out;
283b77a493bSEric Paris 
2841da177e4SLinus Torvalds 	/* No partial writes. */
285b77a493bSEric Paris 	length = -EINVAL;
286b77a493bSEric Paris 	if (*ppos != 0)
287b77a493bSEric Paris 		goto out;
288b77a493bSEric Paris 
289b77a493bSEric Paris 	length = -ENOMEM;
2901da177e4SLinus Torvalds 	page = (char *)get_zeroed_page(GFP_KERNEL);
2911da177e4SLinus Torvalds 	if (!page)
292b77a493bSEric Paris 		goto out;
293b77a493bSEric Paris 
2941da177e4SLinus Torvalds 	length = -EFAULT;
2951da177e4SLinus Torvalds 	if (copy_from_user(page, buf, count))
2961da177e4SLinus Torvalds 		goto out;
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 	length = -EINVAL;
2991da177e4SLinus Torvalds 	if (sscanf(page, "%d", &new_value) != 1)
3001da177e4SLinus Torvalds 		goto out;
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 	if (new_value) {
3031da177e4SLinus Torvalds 		length = selinux_disable();
304b77a493bSEric Paris 		if (length)
3051da177e4SLinus Torvalds 			goto out;
306af601e46SSteve Grubb 		audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
3074746ec5bSEric Paris 			"selinux=0 auid=%u ses=%u",
308581abc09SEric W. Biederman 			from_kuid(&init_user_ns, audit_get_loginuid(current)),
3094746ec5bSEric Paris 			audit_get_sessionid(current));
3101da177e4SLinus Torvalds 	}
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 	length = count;
3131da177e4SLinus Torvalds out:
3141da177e4SLinus Torvalds 	free_page((unsigned long) page);
3151da177e4SLinus Torvalds 	return length;
3161da177e4SLinus Torvalds }
3171da177e4SLinus Torvalds #else
3181da177e4SLinus Torvalds #define sel_write_disable NULL
3191da177e4SLinus Torvalds #endif
3201da177e4SLinus Torvalds 
3219c2e08c5SArjan van de Ven static const struct file_operations sel_disable_ops = {
3221da177e4SLinus Torvalds 	.write		= sel_write_disable,
32357a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
3241da177e4SLinus Torvalds };
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
3271da177e4SLinus Torvalds 				   size_t count, loff_t *ppos)
3281da177e4SLinus Torvalds {
3291da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
3301da177e4SLinus Torvalds 	ssize_t length;
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
3331da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
3341da177e4SLinus Torvalds }
3351da177e4SLinus Torvalds 
3369c2e08c5SArjan van de Ven static const struct file_operations sel_policyvers_ops = {
3371da177e4SLinus Torvalds 	.read		= sel_read_policyvers,
33857a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
3391da177e4SLinus Torvalds };
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds /* declaration for sel_write_load */
3421da177e4SLinus Torvalds static int sel_make_bools(void);
343e47c8fc5SChristopher J. PeBenito static int sel_make_classes(void);
3443bb56b25SPaul Moore static int sel_make_policycap(void);
345e47c8fc5SChristopher J. PeBenito 
346e47c8fc5SChristopher J. PeBenito /* declaration for sel_make_class_dirs */
347a1c2aa1eSAl Viro static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
348e47c8fc5SChristopher J. PeBenito 			unsigned long *ino);
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds static ssize_t sel_read_mls(struct file *filp, char __user *buf,
3511da177e4SLinus Torvalds 				size_t count, loff_t *ppos)
3521da177e4SLinus Torvalds {
3531da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
3541da177e4SLinus Torvalds 	ssize_t length;
3551da177e4SLinus Torvalds 
3560719aaf5SGuido Trentalancia 	length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
3570719aaf5SGuido Trentalancia 			   security_mls_enabled());
3581da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
3591da177e4SLinus Torvalds }
3601da177e4SLinus Torvalds 
3619c2e08c5SArjan van de Ven static const struct file_operations sel_mls_ops = {
3621da177e4SLinus Torvalds 	.read		= sel_read_mls,
36357a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
3641da177e4SLinus Torvalds };
3651da177e4SLinus Torvalds 
366cee74f47SEric Paris struct policy_load_memory {
367cee74f47SEric Paris 	size_t len;
368cee74f47SEric Paris 	void *data;
369cee74f47SEric Paris };
370cee74f47SEric Paris 
371cee74f47SEric Paris static int sel_open_policy(struct inode *inode, struct file *filp)
372cee74f47SEric Paris {
373cee74f47SEric Paris 	struct policy_load_memory *plm = NULL;
374cee74f47SEric Paris 	int rc;
375cee74f47SEric Paris 
376cee74f47SEric Paris 	BUG_ON(filp->private_data);
377cee74f47SEric Paris 
378cee74f47SEric Paris 	mutex_lock(&sel_mutex);
379cee74f47SEric Paris 
380cee74f47SEric Paris 	rc = task_has_security(current, SECURITY__READ_POLICY);
381cee74f47SEric Paris 	if (rc)
382cee74f47SEric Paris 		goto err;
383cee74f47SEric Paris 
384cee74f47SEric Paris 	rc = -EBUSY;
385cee74f47SEric Paris 	if (policy_opened)
386cee74f47SEric Paris 		goto err;
387cee74f47SEric Paris 
388cee74f47SEric Paris 	rc = -ENOMEM;
389cee74f47SEric Paris 	plm = kzalloc(sizeof(*plm), GFP_KERNEL);
390cee74f47SEric Paris 	if (!plm)
391cee74f47SEric Paris 		goto err;
392cee74f47SEric Paris 
393cee74f47SEric Paris 	if (i_size_read(inode) != security_policydb_len()) {
394cee74f47SEric Paris 		mutex_lock(&inode->i_mutex);
395cee74f47SEric Paris 		i_size_write(inode, security_policydb_len());
396cee74f47SEric Paris 		mutex_unlock(&inode->i_mutex);
397cee74f47SEric Paris 	}
398cee74f47SEric Paris 
399cee74f47SEric Paris 	rc = security_read_policy(&plm->data, &plm->len);
400cee74f47SEric Paris 	if (rc)
401cee74f47SEric Paris 		goto err;
402cee74f47SEric Paris 
403cee74f47SEric Paris 	policy_opened = 1;
404cee74f47SEric Paris 
405cee74f47SEric Paris 	filp->private_data = plm;
406cee74f47SEric Paris 
407cee74f47SEric Paris 	mutex_unlock(&sel_mutex);
408cee74f47SEric Paris 
409cee74f47SEric Paris 	return 0;
410cee74f47SEric Paris err:
411cee74f47SEric Paris 	mutex_unlock(&sel_mutex);
412cee74f47SEric Paris 
413cee74f47SEric Paris 	if (plm)
414cee74f47SEric Paris 		vfree(plm->data);
415cee74f47SEric Paris 	kfree(plm);
416cee74f47SEric Paris 	return rc;
417cee74f47SEric Paris }
418cee74f47SEric Paris 
419cee74f47SEric Paris static int sel_release_policy(struct inode *inode, struct file *filp)
420cee74f47SEric Paris {
421cee74f47SEric Paris 	struct policy_load_memory *plm = filp->private_data;
422cee74f47SEric Paris 
423cee74f47SEric Paris 	BUG_ON(!plm);
424cee74f47SEric Paris 
425cee74f47SEric Paris 	policy_opened = 0;
426cee74f47SEric Paris 
427cee74f47SEric Paris 	vfree(plm->data);
428cee74f47SEric Paris 	kfree(plm);
429cee74f47SEric Paris 
430cee74f47SEric Paris 	return 0;
431cee74f47SEric Paris }
432cee74f47SEric Paris 
433cee74f47SEric Paris static ssize_t sel_read_policy(struct file *filp, char __user *buf,
434cee74f47SEric Paris 			       size_t count, loff_t *ppos)
435cee74f47SEric Paris {
436cee74f47SEric Paris 	struct policy_load_memory *plm = filp->private_data;
437cee74f47SEric Paris 	int ret;
438cee74f47SEric Paris 
439cee74f47SEric Paris 	mutex_lock(&sel_mutex);
440cee74f47SEric Paris 
441cee74f47SEric Paris 	ret = task_has_security(current, SECURITY__READ_POLICY);
442cee74f47SEric Paris 	if (ret)
443cee74f47SEric Paris 		goto out;
444cee74f47SEric Paris 
445cee74f47SEric Paris 	ret = simple_read_from_buffer(buf, count, ppos, plm->data, plm->len);
446cee74f47SEric Paris out:
447cee74f47SEric Paris 	mutex_unlock(&sel_mutex);
448cee74f47SEric Paris 	return ret;
449cee74f47SEric Paris }
450cee74f47SEric Paris 
451845ca30fSEric Paris static int sel_mmap_policy_fault(struct vm_area_struct *vma,
452845ca30fSEric Paris 				 struct vm_fault *vmf)
453845ca30fSEric Paris {
454845ca30fSEric Paris 	struct policy_load_memory *plm = vma->vm_file->private_data;
455845ca30fSEric Paris 	unsigned long offset;
456845ca30fSEric Paris 	struct page *page;
457845ca30fSEric Paris 
458845ca30fSEric Paris 	if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE))
459845ca30fSEric Paris 		return VM_FAULT_SIGBUS;
460845ca30fSEric Paris 
461845ca30fSEric Paris 	offset = vmf->pgoff << PAGE_SHIFT;
462845ca30fSEric Paris 	if (offset >= roundup(plm->len, PAGE_SIZE))
463845ca30fSEric Paris 		return VM_FAULT_SIGBUS;
464845ca30fSEric Paris 
465845ca30fSEric Paris 	page = vmalloc_to_page(plm->data + offset);
466845ca30fSEric Paris 	get_page(page);
467845ca30fSEric Paris 
468845ca30fSEric Paris 	vmf->page = page;
469845ca30fSEric Paris 
470845ca30fSEric Paris 	return 0;
471845ca30fSEric Paris }
472845ca30fSEric Paris 
473845ca30fSEric Paris static struct vm_operations_struct sel_mmap_policy_ops = {
474845ca30fSEric Paris 	.fault = sel_mmap_policy_fault,
475845ca30fSEric Paris 	.page_mkwrite = sel_mmap_policy_fault,
476845ca30fSEric Paris };
477845ca30fSEric Paris 
478ad3fa08cSJames Morris static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma)
479845ca30fSEric Paris {
480845ca30fSEric Paris 	if (vma->vm_flags & VM_SHARED) {
481845ca30fSEric Paris 		/* do not allow mprotect to make mapping writable */
482845ca30fSEric Paris 		vma->vm_flags &= ~VM_MAYWRITE;
483845ca30fSEric Paris 
484845ca30fSEric Paris 		if (vma->vm_flags & VM_WRITE)
485845ca30fSEric Paris 			return -EACCES;
486845ca30fSEric Paris 	}
487845ca30fSEric Paris 
488314e51b9SKonstantin Khlebnikov 	vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
489845ca30fSEric Paris 	vma->vm_ops = &sel_mmap_policy_ops;
490845ca30fSEric Paris 
491845ca30fSEric Paris 	return 0;
492845ca30fSEric Paris }
493845ca30fSEric Paris 
494cee74f47SEric Paris static const struct file_operations sel_policy_ops = {
495cee74f47SEric Paris 	.open		= sel_open_policy,
496cee74f47SEric Paris 	.read		= sel_read_policy,
497845ca30fSEric Paris 	.mmap		= sel_mmap_policy,
498cee74f47SEric Paris 	.release	= sel_release_policy,
49947a93a5bSEric Paris 	.llseek		= generic_file_llseek,
500cee74f47SEric Paris };
501cee74f47SEric Paris 
5021da177e4SLinus Torvalds static ssize_t sel_write_load(struct file *file, const char __user *buf,
5031da177e4SLinus Torvalds 			      size_t count, loff_t *ppos)
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds {
5061da177e4SLinus Torvalds 	ssize_t length;
5071da177e4SLinus Torvalds 	void *data = NULL;
5081da177e4SLinus Torvalds 
509bb003079SIngo Molnar 	mutex_lock(&sel_mutex);
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds 	length = task_has_security(current, SECURITY__LOAD_POLICY);
5121da177e4SLinus Torvalds 	if (length)
5131da177e4SLinus Torvalds 		goto out;
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 	/* No partial writes. */
5161da177e4SLinus Torvalds 	length = -EINVAL;
517b77a493bSEric Paris 	if (*ppos != 0)
5181da177e4SLinus Torvalds 		goto out;
5191da177e4SLinus Torvalds 
520b77a493bSEric Paris 	length = -EFBIG;
521b77a493bSEric Paris 	if (count > 64 * 1024 * 1024)
5221da177e4SLinus Torvalds 		goto out;
523b77a493bSEric Paris 
524b77a493bSEric Paris 	length = -ENOMEM;
525b77a493bSEric Paris 	data = vmalloc(count);
526b77a493bSEric Paris 	if (!data)
527b77a493bSEric Paris 		goto out;
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds 	length = -EFAULT;
5301da177e4SLinus Torvalds 	if (copy_from_user(data, buf, count) != 0)
5311da177e4SLinus Torvalds 		goto out;
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds 	length = security_load_policy(data, count);
5341da177e4SLinus Torvalds 	if (length)
5351da177e4SLinus Torvalds 		goto out;
5361da177e4SLinus Torvalds 
537b77a493bSEric Paris 	length = sel_make_bools();
538b77a493bSEric Paris 	if (length)
539e47c8fc5SChristopher J. PeBenito 		goto out1;
540e47c8fc5SChristopher J. PeBenito 
541b77a493bSEric Paris 	length = sel_make_classes();
542b77a493bSEric Paris 	if (length)
5433bb56b25SPaul Moore 		goto out1;
5443bb56b25SPaul Moore 
545b77a493bSEric Paris 	length = sel_make_policycap();
546b77a493bSEric Paris 	if (length)
547b77a493bSEric Paris 		goto out1;
548b77a493bSEric Paris 
5491da177e4SLinus Torvalds 	length = count;
550e47c8fc5SChristopher J. PeBenito 
551e47c8fc5SChristopher J. PeBenito out1:
552af601e46SSteve Grubb 	audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
5534746ec5bSEric Paris 		"policy loaded auid=%u ses=%u",
554581abc09SEric W. Biederman 		from_kuid(&init_user_ns, audit_get_loginuid(current)),
5554746ec5bSEric Paris 		audit_get_sessionid(current));
5561da177e4SLinus Torvalds out:
557bb003079SIngo Molnar 	mutex_unlock(&sel_mutex);
5581da177e4SLinus Torvalds 	vfree(data);
5591da177e4SLinus Torvalds 	return length;
5601da177e4SLinus Torvalds }
5611da177e4SLinus Torvalds 
5629c2e08c5SArjan van de Ven static const struct file_operations sel_load_ops = {
5631da177e4SLinus Torvalds 	.write		= sel_write_load,
56457a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
5651da177e4SLinus Torvalds };
5661da177e4SLinus Torvalds 
567ce9982d0SStephen Smalley static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
5681da177e4SLinus Torvalds {
569b77a493bSEric Paris 	char *canon = NULL;
570ce9982d0SStephen Smalley 	u32 sid, len;
5711da177e4SLinus Torvalds 	ssize_t length;
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds 	length = task_has_security(current, SECURITY__CHECK_CONTEXT);
5741da177e4SLinus Torvalds 	if (length)
575b77a493bSEric Paris 		goto out;
5761da177e4SLinus Torvalds 
577ce9982d0SStephen Smalley 	length = security_context_to_sid(buf, size, &sid);
578b77a493bSEric Paris 	if (length)
579b77a493bSEric Paris 		goto out;
5801da177e4SLinus Torvalds 
581ce9982d0SStephen Smalley 	length = security_sid_to_context(sid, &canon, &len);
582b77a493bSEric Paris 	if (length)
583b77a493bSEric Paris 		goto out;
584ce9982d0SStephen Smalley 
585b77a493bSEric Paris 	length = -ERANGE;
586ce9982d0SStephen Smalley 	if (len > SIMPLE_TRANSACTION_LIMIT) {
587744ba35eSEric Paris 		printk(KERN_ERR "SELinux: %s:  context size (%u) exceeds "
588744ba35eSEric Paris 			"payload max\n", __func__, len);
589ce9982d0SStephen Smalley 		goto out;
590ce9982d0SStephen Smalley 	}
591ce9982d0SStephen Smalley 
592ce9982d0SStephen Smalley 	memcpy(buf, canon, len);
593ce9982d0SStephen Smalley 	length = len;
5941da177e4SLinus Torvalds out:
595ce9982d0SStephen Smalley 	kfree(canon);
5961da177e4SLinus Torvalds 	return length;
5971da177e4SLinus Torvalds }
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
6001da177e4SLinus Torvalds 				     size_t count, loff_t *ppos)
6011da177e4SLinus Torvalds {
6021da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
6031da177e4SLinus Torvalds 	ssize_t length;
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot);
6061da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
6071da177e4SLinus Torvalds }
6081da177e4SLinus Torvalds 
6091da177e4SLinus Torvalds static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
6101da177e4SLinus Torvalds 				      size_t count, loff_t *ppos)
6111da177e4SLinus Torvalds {
612b77a493bSEric Paris 	char *page = NULL;
6131da177e4SLinus Torvalds 	ssize_t length;
6141da177e4SLinus Torvalds 	unsigned int new_value;
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds 	length = task_has_security(current, SECURITY__SETCHECKREQPROT);
6171da177e4SLinus Torvalds 	if (length)
618b77a493bSEric Paris 		goto out;
6191da177e4SLinus Torvalds 
620b77a493bSEric Paris 	length = -ENOMEM;
621bfd51626SDavi Arnaut 	if (count >= PAGE_SIZE)
622b77a493bSEric Paris 		goto out;
623b77a493bSEric Paris 
6241da177e4SLinus Torvalds 	/* No partial writes. */
625b77a493bSEric Paris 	length = -EINVAL;
626b77a493bSEric Paris 	if (*ppos != 0)
627b77a493bSEric Paris 		goto out;
628b77a493bSEric Paris 
629b77a493bSEric Paris 	length = -ENOMEM;
6301da177e4SLinus Torvalds 	page = (char *)get_zeroed_page(GFP_KERNEL);
6311da177e4SLinus Torvalds 	if (!page)
632b77a493bSEric Paris 		goto out;
633b77a493bSEric Paris 
6341da177e4SLinus Torvalds 	length = -EFAULT;
6351da177e4SLinus Torvalds 	if (copy_from_user(page, buf, count))
6361da177e4SLinus Torvalds 		goto out;
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds 	length = -EINVAL;
6391da177e4SLinus Torvalds 	if (sscanf(page, "%u", &new_value) != 1)
6401da177e4SLinus Torvalds 		goto out;
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 	selinux_checkreqprot = new_value ? 1 : 0;
6431da177e4SLinus Torvalds 	length = count;
6441da177e4SLinus Torvalds out:
6451da177e4SLinus Torvalds 	free_page((unsigned long) page);
6461da177e4SLinus Torvalds 	return length;
6471da177e4SLinus Torvalds }
6489c2e08c5SArjan van de Ven static const struct file_operations sel_checkreqprot_ops = {
6491da177e4SLinus Torvalds 	.read		= sel_read_checkreqprot,
6501da177e4SLinus Torvalds 	.write		= sel_write_checkreqprot,
65157a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
6521da177e4SLinus Torvalds };
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds /*
6551da177e4SLinus Torvalds  * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
6561da177e4SLinus Torvalds  */
6571da177e4SLinus Torvalds static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
6581da177e4SLinus Torvalds static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
6591da177e4SLinus Torvalds static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
6601da177e4SLinus Torvalds static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
6611da177e4SLinus Torvalds static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
6621da177e4SLinus Torvalds 
6631da177e4SLinus Torvalds static ssize_t (*write_op[])(struct file *, char *, size_t) = {
6641da177e4SLinus Torvalds 	[SEL_ACCESS] = sel_write_access,
6651da177e4SLinus Torvalds 	[SEL_CREATE] = sel_write_create,
6661da177e4SLinus Torvalds 	[SEL_RELABEL] = sel_write_relabel,
6671da177e4SLinus Torvalds 	[SEL_USER] = sel_write_user,
6681da177e4SLinus Torvalds 	[SEL_MEMBER] = sel_write_member,
669ce9982d0SStephen Smalley 	[SEL_CONTEXT] = sel_write_context,
6701da177e4SLinus Torvalds };
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
6731da177e4SLinus Torvalds {
674*496ad9aaSAl Viro 	ino_t ino = file_inode(file)->i_ino;
6751da177e4SLinus Torvalds 	char *data;
6761da177e4SLinus Torvalds 	ssize_t rv;
6771da177e4SLinus Torvalds 
6786e20a64aSNicolas Kaiser 	if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
6791da177e4SLinus Torvalds 		return -EINVAL;
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds 	data = simple_transaction_get(file, buf, size);
6821da177e4SLinus Torvalds 	if (IS_ERR(data))
6831da177e4SLinus Torvalds 		return PTR_ERR(data);
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds 	rv = write_op[ino](file, data, size);
6861da177e4SLinus Torvalds 	if (rv > 0) {
6871da177e4SLinus Torvalds 		simple_transaction_set(file, rv);
6881da177e4SLinus Torvalds 		rv = size;
6891da177e4SLinus Torvalds 	}
6901da177e4SLinus Torvalds 	return rv;
6911da177e4SLinus Torvalds }
6921da177e4SLinus Torvalds 
6939c2e08c5SArjan van de Ven static const struct file_operations transaction_ops = {
6941da177e4SLinus Torvalds 	.write		= selinux_transaction_write,
6951da177e4SLinus Torvalds 	.read		= simple_transaction_read,
6961da177e4SLinus Torvalds 	.release	= simple_transaction_release,
69757a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
6981da177e4SLinus Torvalds };
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds /*
7011da177e4SLinus Torvalds  * payload - write methods
7021da177e4SLinus Torvalds  * If the method has a response, the response should be put in buf,
7031da177e4SLinus Torvalds  * and the length returned.  Otherwise return 0 or and -error.
7041da177e4SLinus Torvalds  */
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
7071da177e4SLinus Torvalds {
708b77a493bSEric Paris 	char *scon = NULL, *tcon = NULL;
7091da177e4SLinus Torvalds 	u32 ssid, tsid;
7101da177e4SLinus Torvalds 	u16 tclass;
7111da177e4SLinus Torvalds 	struct av_decision avd;
7121da177e4SLinus Torvalds 	ssize_t length;
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds 	length = task_has_security(current, SECURITY__COMPUTE_AV);
7151da177e4SLinus Torvalds 	if (length)
716b77a493bSEric Paris 		goto out;
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds 	length = -ENOMEM;
71989d155efSJames Morris 	scon = kzalloc(size + 1, GFP_KERNEL);
7201da177e4SLinus Torvalds 	if (!scon)
721b77a493bSEric Paris 		goto out;
7221da177e4SLinus Torvalds 
723b77a493bSEric Paris 	length = -ENOMEM;
72489d155efSJames Morris 	tcon = kzalloc(size + 1, GFP_KERNEL);
7251da177e4SLinus Torvalds 	if (!tcon)
7261da177e4SLinus Torvalds 		goto out;
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds 	length = -EINVAL;
72919439d05SStephen Smalley 	if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
730b77a493bSEric Paris 		goto out;
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds 	length = security_context_to_sid(scon, strlen(scon) + 1, &ssid);
733b77a493bSEric Paris 	if (length)
734b77a493bSEric Paris 		goto out;
735b77a493bSEric Paris 
7361da177e4SLinus Torvalds 	length = security_context_to_sid(tcon, strlen(tcon) + 1, &tsid);
737b77a493bSEric Paris 	if (length)
738b77a493bSEric Paris 		goto out;
7391da177e4SLinus Torvalds 
74019439d05SStephen Smalley 	security_compute_av_user(ssid, tsid, tclass, &avd);
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds 	length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
7438a6f83afSKaiGai Kohei 			  "%x %x %x %x %u %x",
744f1c6381aSEric Paris 			  avd.allowed, 0xffffffff,
7451da177e4SLinus Torvalds 			  avd.auditallow, avd.auditdeny,
7468a6f83afSKaiGai Kohei 			  avd.seqno, avd.flags);
7471da177e4SLinus Torvalds out:
748b77a493bSEric Paris 	kfree(tcon);
7491da177e4SLinus Torvalds 	kfree(scon);
7501da177e4SLinus Torvalds 	return length;
7511da177e4SLinus Torvalds }
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
7541da177e4SLinus Torvalds {
755b77a493bSEric Paris 	char *scon = NULL, *tcon = NULL;
756f50a3ec9SKohei Kaigai 	char *namebuf = NULL, *objname = NULL;
7571da177e4SLinus Torvalds 	u32 ssid, tsid, newsid;
7581da177e4SLinus Torvalds 	u16 tclass;
7591da177e4SLinus Torvalds 	ssize_t length;
760b77a493bSEric Paris 	char *newcon = NULL;
7611da177e4SLinus Torvalds 	u32 len;
762f50a3ec9SKohei Kaigai 	int nargs;
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds 	length = task_has_security(current, SECURITY__COMPUTE_CREATE);
7651da177e4SLinus Torvalds 	if (length)
766b77a493bSEric Paris 		goto out;
7671da177e4SLinus Torvalds 
7681da177e4SLinus Torvalds 	length = -ENOMEM;
76989d155efSJames Morris 	scon = kzalloc(size + 1, GFP_KERNEL);
7701da177e4SLinus Torvalds 	if (!scon)
771b77a493bSEric Paris 		goto out;
7721da177e4SLinus Torvalds 
773b77a493bSEric Paris 	length = -ENOMEM;
77489d155efSJames Morris 	tcon = kzalloc(size + 1, GFP_KERNEL);
7751da177e4SLinus Torvalds 	if (!tcon)
7761da177e4SLinus Torvalds 		goto out;
7771da177e4SLinus Torvalds 
778f50a3ec9SKohei Kaigai 	length = -ENOMEM;
779f50a3ec9SKohei Kaigai 	namebuf = kzalloc(size + 1, GFP_KERNEL);
780f50a3ec9SKohei Kaigai 	if (!namebuf)
781b77a493bSEric Paris 		goto out;
7821da177e4SLinus Torvalds 
783f50a3ec9SKohei Kaigai 	length = -EINVAL;
784f50a3ec9SKohei Kaigai 	nargs = sscanf(buf, "%s %s %hu %s", scon, tcon, &tclass, namebuf);
785f50a3ec9SKohei Kaigai 	if (nargs < 3 || nargs > 4)
786f50a3ec9SKohei Kaigai 		goto out;
7870f7e4c33SKohei Kaigai 	if (nargs == 4) {
7880f7e4c33SKohei Kaigai 		/*
7890f7e4c33SKohei Kaigai 		 * If and when the name of new object to be queried contains
7900f7e4c33SKohei Kaigai 		 * either whitespace or multibyte characters, they shall be
7910f7e4c33SKohei Kaigai 		 * encoded based on the percentage-encoding rule.
7920f7e4c33SKohei Kaigai 		 * If not encoded, the sscanf logic picks up only left-half
7930f7e4c33SKohei Kaigai 		 * of the supplied name; splitted by a whitespace unexpectedly.
7940f7e4c33SKohei Kaigai 		 */
7950f7e4c33SKohei Kaigai 		char   *r, *w;
7960f7e4c33SKohei Kaigai 		int     c1, c2;
7970f7e4c33SKohei Kaigai 
7980f7e4c33SKohei Kaigai 		r = w = namebuf;
7990f7e4c33SKohei Kaigai 		do {
8000f7e4c33SKohei Kaigai 			c1 = *r++;
8010f7e4c33SKohei Kaigai 			if (c1 == '+')
8020f7e4c33SKohei Kaigai 				c1 = ' ';
8030f7e4c33SKohei Kaigai 			else if (c1 == '%') {
804af7ff2c2SAndy Shevchenko 				c1 = hex_to_bin(*r++);
805af7ff2c2SAndy Shevchenko 				if (c1 < 0)
8060f7e4c33SKohei Kaigai 					goto out;
807af7ff2c2SAndy Shevchenko 				c2 = hex_to_bin(*r++);
808af7ff2c2SAndy Shevchenko 				if (c2 < 0)
8090f7e4c33SKohei Kaigai 					goto out;
8100f7e4c33SKohei Kaigai 				c1 = (c1 << 4) | c2;
8110f7e4c33SKohei Kaigai 			}
8120f7e4c33SKohei Kaigai 			*w++ = c1;
8130f7e4c33SKohei Kaigai 		} while (c1 != '\0');
8140f7e4c33SKohei Kaigai 
815f50a3ec9SKohei Kaigai 		objname = namebuf;
8160f7e4c33SKohei Kaigai 	}
817f50a3ec9SKohei Kaigai 
8181da177e4SLinus Torvalds 	length = security_context_to_sid(scon, strlen(scon) + 1, &ssid);
819b77a493bSEric Paris 	if (length)
820b77a493bSEric Paris 		goto out;
821b77a493bSEric Paris 
8221da177e4SLinus Torvalds 	length = security_context_to_sid(tcon, strlen(tcon) + 1, &tsid);
823b77a493bSEric Paris 	if (length)
824b77a493bSEric Paris 		goto out;
8251da177e4SLinus Torvalds 
826f50a3ec9SKohei Kaigai 	length = security_transition_sid_user(ssid, tsid, tclass,
827f50a3ec9SKohei Kaigai 					      objname, &newsid);
828b77a493bSEric Paris 	if (length)
829b77a493bSEric Paris 		goto out;
8301da177e4SLinus Torvalds 
8311da177e4SLinus Torvalds 	length = security_sid_to_context(newsid, &newcon, &len);
832b77a493bSEric Paris 	if (length)
833b77a493bSEric Paris 		goto out;
8341da177e4SLinus Torvalds 
835b77a493bSEric Paris 	length = -ERANGE;
8361da177e4SLinus Torvalds 	if (len > SIMPLE_TRANSACTION_LIMIT) {
837744ba35eSEric Paris 		printk(KERN_ERR "SELinux: %s:  context size (%u) exceeds "
838744ba35eSEric Paris 			"payload max\n", __func__, len);
839b77a493bSEric Paris 		goto out;
8401da177e4SLinus Torvalds 	}
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds 	memcpy(buf, newcon, len);
8431da177e4SLinus Torvalds 	length = len;
8441da177e4SLinus Torvalds out:
845b77a493bSEric Paris 	kfree(newcon);
846f50a3ec9SKohei Kaigai 	kfree(namebuf);
847b77a493bSEric Paris 	kfree(tcon);
8481da177e4SLinus Torvalds 	kfree(scon);
8491da177e4SLinus Torvalds 	return length;
8501da177e4SLinus Torvalds }
8511da177e4SLinus Torvalds 
8521da177e4SLinus Torvalds static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
8531da177e4SLinus Torvalds {
854b77a493bSEric Paris 	char *scon = NULL, *tcon = NULL;
8551da177e4SLinus Torvalds 	u32 ssid, tsid, newsid;
8561da177e4SLinus Torvalds 	u16 tclass;
8571da177e4SLinus Torvalds 	ssize_t length;
858b77a493bSEric Paris 	char *newcon = NULL;
8591da177e4SLinus Torvalds 	u32 len;
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds 	length = task_has_security(current, SECURITY__COMPUTE_RELABEL);
8621da177e4SLinus Torvalds 	if (length)
863b77a493bSEric Paris 		goto out;
8641da177e4SLinus Torvalds 
8651da177e4SLinus Torvalds 	length = -ENOMEM;
86689d155efSJames Morris 	scon = kzalloc(size + 1, GFP_KERNEL);
8671da177e4SLinus Torvalds 	if (!scon)
868b77a493bSEric Paris 		goto out;
8691da177e4SLinus Torvalds 
870b77a493bSEric Paris 	length = -ENOMEM;
87189d155efSJames Morris 	tcon = kzalloc(size + 1, GFP_KERNEL);
8721da177e4SLinus Torvalds 	if (!tcon)
8731da177e4SLinus Torvalds 		goto out;
8741da177e4SLinus Torvalds 
8751da177e4SLinus Torvalds 	length = -EINVAL;
8761da177e4SLinus Torvalds 	if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
877b77a493bSEric Paris 		goto out;
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds 	length = security_context_to_sid(scon, strlen(scon) + 1, &ssid);
880b77a493bSEric Paris 	if (length)
881b77a493bSEric Paris 		goto out;
882b77a493bSEric Paris 
8831da177e4SLinus Torvalds 	length = security_context_to_sid(tcon, strlen(tcon) + 1, &tsid);
884b77a493bSEric Paris 	if (length)
885b77a493bSEric Paris 		goto out;
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds 	length = security_change_sid(ssid, tsid, tclass, &newsid);
888b77a493bSEric Paris 	if (length)
889b77a493bSEric Paris 		goto out;
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds 	length = security_sid_to_context(newsid, &newcon, &len);
892b77a493bSEric Paris 	if (length)
893b77a493bSEric Paris 		goto out;
8941da177e4SLinus Torvalds 
8951da177e4SLinus Torvalds 	length = -ERANGE;
896b77a493bSEric Paris 	if (len > SIMPLE_TRANSACTION_LIMIT)
897b77a493bSEric Paris 		goto out;
8981da177e4SLinus Torvalds 
8991da177e4SLinus Torvalds 	memcpy(buf, newcon, len);
9001da177e4SLinus Torvalds 	length = len;
9011da177e4SLinus Torvalds out:
902b77a493bSEric Paris 	kfree(newcon);
903b77a493bSEric Paris 	kfree(tcon);
9041da177e4SLinus Torvalds 	kfree(scon);
9051da177e4SLinus Torvalds 	return length;
9061da177e4SLinus Torvalds }
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
9091da177e4SLinus Torvalds {
910b77a493bSEric Paris 	char *con = NULL, *user = NULL, *ptr;
911b77a493bSEric Paris 	u32 sid, *sids = NULL;
9121da177e4SLinus Torvalds 	ssize_t length;
9131da177e4SLinus Torvalds 	char *newcon;
9141da177e4SLinus Torvalds 	int i, rc;
9151da177e4SLinus Torvalds 	u32 len, nsids;
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds 	length = task_has_security(current, SECURITY__COMPUTE_USER);
9181da177e4SLinus Torvalds 	if (length)
9196eab04a8SJustin P. Mattock 		goto out;
9201da177e4SLinus Torvalds 
9211da177e4SLinus Torvalds 	length = -ENOMEM;
92289d155efSJames Morris 	con = kzalloc(size + 1, GFP_KERNEL);
9231da177e4SLinus Torvalds 	if (!con)
9246eab04a8SJustin P. Mattock 		goto out;
9251da177e4SLinus Torvalds 
926b77a493bSEric Paris 	length = -ENOMEM;
92789d155efSJames Morris 	user = kzalloc(size + 1, GFP_KERNEL);
9281da177e4SLinus Torvalds 	if (!user)
9291da177e4SLinus Torvalds 		goto out;
9301da177e4SLinus Torvalds 
9311da177e4SLinus Torvalds 	length = -EINVAL;
9321da177e4SLinus Torvalds 	if (sscanf(buf, "%s %s", con, user) != 2)
933b77a493bSEric Paris 		goto out;
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds 	length = security_context_to_sid(con, strlen(con) + 1, &sid);
936b77a493bSEric Paris 	if (length)
937b77a493bSEric Paris 		goto out;
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds 	length = security_get_user_sids(sid, user, &sids, &nsids);
940b77a493bSEric Paris 	if (length)
941b77a493bSEric Paris 		goto out;
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds 	length = sprintf(buf, "%u", nsids) + 1;
9441da177e4SLinus Torvalds 	ptr = buf + length;
9451da177e4SLinus Torvalds 	for (i = 0; i < nsids; i++) {
9461da177e4SLinus Torvalds 		rc = security_sid_to_context(sids[i], &newcon, &len);
9471da177e4SLinus Torvalds 		if (rc) {
9481da177e4SLinus Torvalds 			length = rc;
949b77a493bSEric Paris 			goto out;
9501da177e4SLinus Torvalds 		}
9511da177e4SLinus Torvalds 		if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
9521da177e4SLinus Torvalds 			kfree(newcon);
9531da177e4SLinus Torvalds 			length = -ERANGE;
954b77a493bSEric Paris 			goto out;
9551da177e4SLinus Torvalds 		}
9561da177e4SLinus Torvalds 		memcpy(ptr, newcon, len);
9571da177e4SLinus Torvalds 		kfree(newcon);
9581da177e4SLinus Torvalds 		ptr += len;
9591da177e4SLinus Torvalds 		length += len;
9601da177e4SLinus Torvalds 	}
9611da177e4SLinus Torvalds out:
962b77a493bSEric Paris 	kfree(sids);
963b77a493bSEric Paris 	kfree(user);
9641da177e4SLinus Torvalds 	kfree(con);
9651da177e4SLinus Torvalds 	return length;
9661da177e4SLinus Torvalds }
9671da177e4SLinus Torvalds 
9681da177e4SLinus Torvalds static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
9691da177e4SLinus Torvalds {
970b77a493bSEric Paris 	char *scon = NULL, *tcon = NULL;
9711da177e4SLinus Torvalds 	u32 ssid, tsid, newsid;
9721da177e4SLinus Torvalds 	u16 tclass;
9731da177e4SLinus Torvalds 	ssize_t length;
974b77a493bSEric Paris 	char *newcon = NULL;
9751da177e4SLinus Torvalds 	u32 len;
9761da177e4SLinus Torvalds 
9771da177e4SLinus Torvalds 	length = task_has_security(current, SECURITY__COMPUTE_MEMBER);
9781da177e4SLinus Torvalds 	if (length)
979b77a493bSEric Paris 		goto out;
9801da177e4SLinus Torvalds 
9811da177e4SLinus Torvalds 	length = -ENOMEM;
98289d155efSJames Morris 	scon = kzalloc(size + 1, GFP_KERNEL);
9831da177e4SLinus Torvalds 	if (!scon)
9846eab04a8SJustin P. Mattock 		goto out;
9851da177e4SLinus Torvalds 
986b77a493bSEric Paris 	length = -ENOMEM;
98789d155efSJames Morris 	tcon = kzalloc(size + 1, GFP_KERNEL);
9881da177e4SLinus Torvalds 	if (!tcon)
9891da177e4SLinus Torvalds 		goto out;
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds 	length = -EINVAL;
9921da177e4SLinus Torvalds 	if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
993b77a493bSEric Paris 		goto out;
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds 	length = security_context_to_sid(scon, strlen(scon) + 1, &ssid);
996b77a493bSEric Paris 	if (length)
997b77a493bSEric Paris 		goto out;
998b77a493bSEric Paris 
9991da177e4SLinus Torvalds 	length = security_context_to_sid(tcon, strlen(tcon) + 1, &tsid);
1000b77a493bSEric Paris 	if (length)
1001b77a493bSEric Paris 		goto out;
10021da177e4SLinus Torvalds 
10031da177e4SLinus Torvalds 	length = security_member_sid(ssid, tsid, tclass, &newsid);
1004b77a493bSEric Paris 	if (length)
1005b77a493bSEric Paris 		goto out;
10061da177e4SLinus Torvalds 
10071da177e4SLinus Torvalds 	length = security_sid_to_context(newsid, &newcon, &len);
1008b77a493bSEric Paris 	if (length)
1009b77a493bSEric Paris 		goto out;
10101da177e4SLinus Torvalds 
1011b77a493bSEric Paris 	length = -ERANGE;
10121da177e4SLinus Torvalds 	if (len > SIMPLE_TRANSACTION_LIMIT) {
1013744ba35eSEric Paris 		printk(KERN_ERR "SELinux: %s:  context size (%u) exceeds "
1014744ba35eSEric Paris 			"payload max\n", __func__, len);
1015b77a493bSEric Paris 		goto out;
10161da177e4SLinus Torvalds 	}
10171da177e4SLinus Torvalds 
10181da177e4SLinus Torvalds 	memcpy(buf, newcon, len);
10191da177e4SLinus Torvalds 	length = len;
10201da177e4SLinus Torvalds out:
1021b77a493bSEric Paris 	kfree(newcon);
1022b77a493bSEric Paris 	kfree(tcon);
10231da177e4SLinus Torvalds 	kfree(scon);
10241da177e4SLinus Torvalds 	return length;
10251da177e4SLinus Torvalds }
10261da177e4SLinus Torvalds 
10271da177e4SLinus Torvalds static struct inode *sel_make_inode(struct super_block *sb, int mode)
10281da177e4SLinus Torvalds {
10291da177e4SLinus Torvalds 	struct inode *ret = new_inode(sb);
10301da177e4SLinus Torvalds 
10311da177e4SLinus Torvalds 	if (ret) {
10321da177e4SLinus Torvalds 		ret->i_mode = mode;
10331da177e4SLinus Torvalds 		ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
10341da177e4SLinus Torvalds 	}
10351da177e4SLinus Torvalds 	return ret;
10361da177e4SLinus Torvalds }
10371da177e4SLinus Torvalds 
10381da177e4SLinus Torvalds static ssize_t sel_read_bool(struct file *filep, char __user *buf,
10391da177e4SLinus Torvalds 			     size_t count, loff_t *ppos)
10401da177e4SLinus Torvalds {
10411da177e4SLinus Torvalds 	char *page = NULL;
10421da177e4SLinus Torvalds 	ssize_t length;
10431da177e4SLinus Torvalds 	ssize_t ret;
10441da177e4SLinus Torvalds 	int cur_enforcing;
1045*496ad9aaSAl Viro 	unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
1046d313f948SStephen Smalley 	const char *name = filep->f_path.dentry->d_name.name;
10471da177e4SLinus Torvalds 
1048bb003079SIngo Molnar 	mutex_lock(&sel_mutex);
10491da177e4SLinus Torvalds 
1050d313f948SStephen Smalley 	ret = -EINVAL;
1051b77a493bSEric Paris 	if (index >= bool_num || strcmp(name, bool_pending_names[index]))
1052d313f948SStephen Smalley 		goto out;
10531da177e4SLinus Torvalds 
10541da177e4SLinus Torvalds 	ret = -ENOMEM;
1055b77a493bSEric Paris 	page = (char *)get_zeroed_page(GFP_KERNEL);
1056b77a493bSEric Paris 	if (!page)
10571da177e4SLinus Torvalds 		goto out;
10581da177e4SLinus Torvalds 
1059d313f948SStephen Smalley 	cur_enforcing = security_get_bool_value(index);
10601da177e4SLinus Torvalds 	if (cur_enforcing < 0) {
10611da177e4SLinus Torvalds 		ret = cur_enforcing;
10621da177e4SLinus Torvalds 		goto out;
10631da177e4SLinus Torvalds 	}
10641da177e4SLinus Torvalds 	length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
1065d313f948SStephen Smalley 			  bool_pending_values[index]);
106668bdcf28SStephen Smalley 	ret = simple_read_from_buffer(buf, count, ppos, page, length);
10671da177e4SLinus Torvalds out:
1068bb003079SIngo Molnar 	mutex_unlock(&sel_mutex);
10691da177e4SLinus Torvalds 	free_page((unsigned long)page);
10701da177e4SLinus Torvalds 	return ret;
10711da177e4SLinus Torvalds }
10721da177e4SLinus Torvalds 
10731da177e4SLinus Torvalds static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
10741da177e4SLinus Torvalds 			      size_t count, loff_t *ppos)
10751da177e4SLinus Torvalds {
10761da177e4SLinus Torvalds 	char *page = NULL;
1077d313f948SStephen Smalley 	ssize_t length;
10781da177e4SLinus Torvalds 	int new_value;
1079*496ad9aaSAl Viro 	unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
1080d313f948SStephen Smalley 	const char *name = filep->f_path.dentry->d_name.name;
10811da177e4SLinus Torvalds 
1082bb003079SIngo Molnar 	mutex_lock(&sel_mutex);
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds 	length = task_has_security(current, SECURITY__SETBOOL);
10851da177e4SLinus Torvalds 	if (length)
10861da177e4SLinus Torvalds 		goto out;
10871da177e4SLinus Torvalds 
1088d313f948SStephen Smalley 	length = -EINVAL;
1089b77a493bSEric Paris 	if (index >= bool_num || strcmp(name, bool_pending_names[index]))
1090d313f948SStephen Smalley 		goto out;
1091d313f948SStephen Smalley 
10921da177e4SLinus Torvalds 	length = -ENOMEM;
1093b77a493bSEric Paris 	if (count >= PAGE_SIZE)
10941da177e4SLinus Torvalds 		goto out;
1095d313f948SStephen Smalley 
10961da177e4SLinus Torvalds 	/* No partial writes. */
1097d313f948SStephen Smalley 	length = -EINVAL;
1098b77a493bSEric Paris 	if (*ppos != 0)
10991da177e4SLinus Torvalds 		goto out;
1100b77a493bSEric Paris 
11011da177e4SLinus Torvalds 	length = -ENOMEM;
1102b77a493bSEric Paris 	page = (char *)get_zeroed_page(GFP_KERNEL);
1103b77a493bSEric Paris 	if (!page)
11041da177e4SLinus Torvalds 		goto out;
11051da177e4SLinus Torvalds 
1106d313f948SStephen Smalley 	length = -EFAULT;
11071da177e4SLinus Torvalds 	if (copy_from_user(page, buf, count))
11081da177e4SLinus Torvalds 		goto out;
11091da177e4SLinus Torvalds 
11101da177e4SLinus Torvalds 	length = -EINVAL;
11111da177e4SLinus Torvalds 	if (sscanf(page, "%d", &new_value) != 1)
11121da177e4SLinus Torvalds 		goto out;
11131da177e4SLinus Torvalds 
11141da177e4SLinus Torvalds 	if (new_value)
11151da177e4SLinus Torvalds 		new_value = 1;
11161da177e4SLinus Torvalds 
1117d313f948SStephen Smalley 	bool_pending_values[index] = new_value;
11181da177e4SLinus Torvalds 	length = count;
11191da177e4SLinus Torvalds 
11201da177e4SLinus Torvalds out:
1121bb003079SIngo Molnar 	mutex_unlock(&sel_mutex);
11221da177e4SLinus Torvalds 	free_page((unsigned long) page);
11231da177e4SLinus Torvalds 	return length;
11241da177e4SLinus Torvalds }
11251da177e4SLinus Torvalds 
11269c2e08c5SArjan van de Ven static const struct file_operations sel_bool_ops = {
11271da177e4SLinus Torvalds 	.read		= sel_read_bool,
11281da177e4SLinus Torvalds 	.write		= sel_write_bool,
112957a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
11301da177e4SLinus Torvalds };
11311da177e4SLinus Torvalds 
11321da177e4SLinus Torvalds static ssize_t sel_commit_bools_write(struct file *filep,
11331da177e4SLinus Torvalds 				      const char __user *buf,
11341da177e4SLinus Torvalds 				      size_t count, loff_t *ppos)
11351da177e4SLinus Torvalds {
11361da177e4SLinus Torvalds 	char *page = NULL;
1137d313f948SStephen Smalley 	ssize_t length;
11381da177e4SLinus Torvalds 	int new_value;
11391da177e4SLinus Torvalds 
1140bb003079SIngo Molnar 	mutex_lock(&sel_mutex);
11411da177e4SLinus Torvalds 
11421da177e4SLinus Torvalds 	length = task_has_security(current, SECURITY__SETBOOL);
11431da177e4SLinus Torvalds 	if (length)
11441da177e4SLinus Torvalds 		goto out;
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds 	length = -ENOMEM;
1147b77a493bSEric Paris 	if (count >= PAGE_SIZE)
11481da177e4SLinus Torvalds 		goto out;
1149b77a493bSEric Paris 
11501da177e4SLinus Torvalds 	/* No partial writes. */
1151b77a493bSEric Paris 	length = -EINVAL;
1152b77a493bSEric Paris 	if (*ppos != 0)
11531da177e4SLinus Torvalds 		goto out;
1154b77a493bSEric Paris 
11551da177e4SLinus Torvalds 	length = -ENOMEM;
1156b77a493bSEric Paris 	page = (char *)get_zeroed_page(GFP_KERNEL);
1157b77a493bSEric Paris 	if (!page)
11581da177e4SLinus Torvalds 		goto out;
11591da177e4SLinus Torvalds 
1160d313f948SStephen Smalley 	length = -EFAULT;
11611da177e4SLinus Torvalds 	if (copy_from_user(page, buf, count))
11621da177e4SLinus Torvalds 		goto out;
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds 	length = -EINVAL;
11651da177e4SLinus Torvalds 	if (sscanf(page, "%d", &new_value) != 1)
11661da177e4SLinus Torvalds 		goto out;
11671da177e4SLinus Torvalds 
1168b77a493bSEric Paris 	length = 0;
11691872981bSEric Paris 	if (new_value && bool_pending_values)
1170b77a493bSEric Paris 		length = security_set_bools(bool_num, bool_pending_values);
11711da177e4SLinus Torvalds 
1172b77a493bSEric Paris 	if (!length)
11731da177e4SLinus Torvalds 		length = count;
11741da177e4SLinus Torvalds 
11751da177e4SLinus Torvalds out:
1176bb003079SIngo Molnar 	mutex_unlock(&sel_mutex);
11771da177e4SLinus Torvalds 	free_page((unsigned long) page);
11781da177e4SLinus Torvalds 	return length;
11791da177e4SLinus Torvalds }
11801da177e4SLinus Torvalds 
11819c2e08c5SArjan van de Ven static const struct file_operations sel_commit_bools_ops = {
11821da177e4SLinus Torvalds 	.write		= sel_commit_bools_write,
118357a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
11841da177e4SLinus Torvalds };
11851da177e4SLinus Torvalds 
11860c92d7c7SChristopher J. PeBenito static void sel_remove_entries(struct dentry *de)
11871da177e4SLinus Torvalds {
11880955dc03SStephen Smalley 	struct list_head *node;
11891da177e4SLinus Torvalds 
11902fd6b7f5SNick Piggin 	spin_lock(&de->d_lock);
11911da177e4SLinus Torvalds 	node = de->d_subdirs.next;
11921da177e4SLinus Torvalds 	while (node != &de->d_subdirs) {
11935160ee6fSEric Dumazet 		struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
11942fd6b7f5SNick Piggin 
11952fd6b7f5SNick Piggin 		spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
11961da177e4SLinus Torvalds 		list_del_init(node);
11971da177e4SLinus Torvalds 
11981da177e4SLinus Torvalds 		if (d->d_inode) {
1199dc0474beSNick Piggin 			dget_dlock(d);
12002fd6b7f5SNick Piggin 			spin_unlock(&de->d_lock);
12012fd6b7f5SNick Piggin 			spin_unlock(&d->d_lock);
12021da177e4SLinus Torvalds 			d_delete(d);
12031da177e4SLinus Torvalds 			simple_unlink(de->d_inode, d);
12041da177e4SLinus Torvalds 			dput(d);
12052fd6b7f5SNick Piggin 			spin_lock(&de->d_lock);
12062fd6b7f5SNick Piggin 		} else
12072fd6b7f5SNick Piggin 			spin_unlock(&d->d_lock);
12081da177e4SLinus Torvalds 		node = de->d_subdirs.next;
12091da177e4SLinus Torvalds 	}
12101da177e4SLinus Torvalds 
12112fd6b7f5SNick Piggin 	spin_unlock(&de->d_lock);
12121da177e4SLinus Torvalds }
12131da177e4SLinus Torvalds 
12141da177e4SLinus Torvalds #define BOOL_DIR_NAME "booleans"
12151da177e4SLinus Torvalds 
12161da177e4SLinus Torvalds static int sel_make_bools(void)
12171da177e4SLinus Torvalds {
1218b77a493bSEric Paris 	int i, ret;
12191da177e4SLinus Torvalds 	ssize_t len;
12201da177e4SLinus Torvalds 	struct dentry *dentry = NULL;
12211da177e4SLinus Torvalds 	struct dentry *dir = bool_dir;
12221da177e4SLinus Torvalds 	struct inode *inode = NULL;
12231da177e4SLinus Torvalds 	struct inode_security_struct *isec;
12241da177e4SLinus Torvalds 	char **names = NULL, *page;
12251da177e4SLinus Torvalds 	int num;
12261da177e4SLinus Torvalds 	int *values = NULL;
12271da177e4SLinus Torvalds 	u32 sid;
12281da177e4SLinus Torvalds 
12291da177e4SLinus Torvalds 	/* remove any existing files */
12308007f102SXiaotian Feng 	for (i = 0; i < bool_num; i++)
12318007f102SXiaotian Feng 		kfree(bool_pending_names[i]);
1232d313f948SStephen Smalley 	kfree(bool_pending_names);
12331da177e4SLinus Torvalds 	kfree(bool_pending_values);
1234154c50caSEric Paris 	bool_num = 0;
1235d313f948SStephen Smalley 	bool_pending_names = NULL;
123620c19e41SDavi Arnaut 	bool_pending_values = NULL;
12371da177e4SLinus Torvalds 
12380c92d7c7SChristopher J. PeBenito 	sel_remove_entries(dir);
12391da177e4SLinus Torvalds 
1240b77a493bSEric Paris 	ret = -ENOMEM;
12411872981bSEric Paris 	page = (char *)get_zeroed_page(GFP_KERNEL);
12421872981bSEric Paris 	if (!page)
1243b77a493bSEric Paris 		goto out;
12441da177e4SLinus Torvalds 
12451da177e4SLinus Torvalds 	ret = security_get_bools(&num, &names, &values);
1246b77a493bSEric Paris 	if (ret)
12471da177e4SLinus Torvalds 		goto out;
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds 	for (i = 0; i < num; i++) {
1250b77a493bSEric Paris 		ret = -ENOMEM;
12511da177e4SLinus Torvalds 		dentry = d_alloc_name(dir, names[i]);
1252b77a493bSEric Paris 		if (!dentry)
1253b77a493bSEric Paris 			goto out;
12541da177e4SLinus Torvalds 
1255b77a493bSEric Paris 		ret = -ENOMEM;
1256b77a493bSEric Paris 		inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1257b77a493bSEric Paris 		if (!inode)
1258b77a493bSEric Paris 			goto out;
1259b77a493bSEric Paris 
12601da177e4SLinus Torvalds 		ret = -ENAMETOOLONG;
1261cc1dad71SAl Viro 		len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
1262b77a493bSEric Paris 		if (len >= PAGE_SIZE)
1263b77a493bSEric Paris 			goto out;
1264b77a493bSEric Paris 
12651da177e4SLinus Torvalds 		isec = (struct inode_security_struct *)inode->i_security;
12661872981bSEric Paris 		ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid);
12671872981bSEric Paris 		if (ret)
1268b77a493bSEric Paris 			goto out;
1269b77a493bSEric Paris 
12701da177e4SLinus Torvalds 		isec->sid = sid;
12711da177e4SLinus Torvalds 		isec->initialized = 1;
12721da177e4SLinus Torvalds 		inode->i_fop = &sel_bool_ops;
1273bce34bc0SJames Carter 		inode->i_ino = i|SEL_BOOL_INO_OFFSET;
12741da177e4SLinus Torvalds 		d_add(dentry, inode);
12751da177e4SLinus Torvalds 	}
12761da177e4SLinus Torvalds 	bool_num = num;
1277d313f948SStephen Smalley 	bool_pending_names = names;
12781da177e4SLinus Torvalds 	bool_pending_values = values;
1279b77a493bSEric Paris 
1280b77a493bSEric Paris 	free_page((unsigned long)page);
1281b77a493bSEric Paris 	return 0;
12821da177e4SLinus Torvalds out:
12831da177e4SLinus Torvalds 	free_page((unsigned long)page);
1284b77a493bSEric Paris 
12851da177e4SLinus Torvalds 	if (names) {
12869a5f04bfSJesper Juhl 		for (i = 0; i < num; i++)
12871da177e4SLinus Torvalds 			kfree(names[i]);
12881da177e4SLinus Torvalds 		kfree(names);
12891da177e4SLinus Torvalds 	}
129020c19e41SDavi Arnaut 	kfree(values);
12910c92d7c7SChristopher J. PeBenito 	sel_remove_entries(dir);
1292b77a493bSEric Paris 
1293b77a493bSEric Paris 	return ret;
12941da177e4SLinus Torvalds }
12951da177e4SLinus Torvalds 
12961da177e4SLinus Torvalds #define NULL_FILE_NAME "null"
12971da177e4SLinus Torvalds 
1298765927b2SAl Viro struct path selinux_null;
12991da177e4SLinus Torvalds 
13001da177e4SLinus Torvalds static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
13011da177e4SLinus Torvalds 					    size_t count, loff_t *ppos)
13021da177e4SLinus Torvalds {
13031da177e4SLinus Torvalds 	char tmpbuf[TMPBUFLEN];
13041da177e4SLinus Torvalds 	ssize_t length;
13051da177e4SLinus Torvalds 
13061da177e4SLinus Torvalds 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
13071da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
13081da177e4SLinus Torvalds }
13091da177e4SLinus Torvalds 
13101da177e4SLinus Torvalds static ssize_t sel_write_avc_cache_threshold(struct file *file,
13111da177e4SLinus Torvalds 					     const char __user *buf,
13121da177e4SLinus Torvalds 					     size_t count, loff_t *ppos)
13131da177e4SLinus Torvalds 
13141da177e4SLinus Torvalds {
1315b77a493bSEric Paris 	char *page = NULL;
13161da177e4SLinus Torvalds 	ssize_t ret;
13171da177e4SLinus Torvalds 	int new_value;
13181da177e4SLinus Torvalds 
13191da177e4SLinus Torvalds 	ret = task_has_security(current, SECURITY__SETSECPARAM);
13201da177e4SLinus Torvalds 	if (ret)
1321b77a493bSEric Paris 		goto out;
1322b77a493bSEric Paris 
1323b77a493bSEric Paris 	ret = -ENOMEM;
1324b77a493bSEric Paris 	if (count >= PAGE_SIZE)
1325b77a493bSEric Paris 		goto out;
1326b77a493bSEric Paris 
1327b77a493bSEric Paris 	/* No partial writes. */
1328b77a493bSEric Paris 	ret = -EINVAL;
1329b77a493bSEric Paris 	if (*ppos != 0)
1330b77a493bSEric Paris 		goto out;
1331b77a493bSEric Paris 
1332b77a493bSEric Paris 	ret = -ENOMEM;
1333b77a493bSEric Paris 	page = (char *)get_zeroed_page(GFP_KERNEL);
1334b77a493bSEric Paris 	if (!page)
1335b77a493bSEric Paris 		goto out;
1336b77a493bSEric Paris 
1337b77a493bSEric Paris 	ret = -EFAULT;
1338b77a493bSEric Paris 	if (copy_from_user(page, buf, count))
1339b77a493bSEric Paris 		goto out;
1340b77a493bSEric Paris 
1341b77a493bSEric Paris 	ret = -EINVAL;
1342b77a493bSEric Paris 	if (sscanf(page, "%u", &new_value) != 1)
1343b77a493bSEric Paris 		goto out;
1344b77a493bSEric Paris 
13451da177e4SLinus Torvalds 	avc_cache_threshold = new_value;
1346b77a493bSEric Paris 
13471da177e4SLinus Torvalds 	ret = count;
13481da177e4SLinus Torvalds out:
1349b77a493bSEric Paris 	free_page((unsigned long)page);
13501da177e4SLinus Torvalds 	return ret;
13511da177e4SLinus Torvalds }
13521da177e4SLinus Torvalds 
13531da177e4SLinus Torvalds static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
13541da177e4SLinus Torvalds 				       size_t count, loff_t *ppos)
13551da177e4SLinus Torvalds {
13561da177e4SLinus Torvalds 	char *page;
1357b77a493bSEric Paris 	ssize_t length;
13581da177e4SLinus Torvalds 
13591da177e4SLinus Torvalds 	page = (char *)__get_free_page(GFP_KERNEL);
1360b77a493bSEric Paris 	if (!page)
1361b77a493bSEric Paris 		return -ENOMEM;
1362b77a493bSEric Paris 
1363b77a493bSEric Paris 	length = avc_get_hash_stats(page);
1364b77a493bSEric Paris 	if (length >= 0)
1365b77a493bSEric Paris 		length = simple_read_from_buffer(buf, count, ppos, page, length);
13661da177e4SLinus Torvalds 	free_page((unsigned long)page);
1367b77a493bSEric Paris 
1368b77a493bSEric Paris 	return length;
13691da177e4SLinus Torvalds }
13701da177e4SLinus Torvalds 
13719c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_threshold_ops = {
13721da177e4SLinus Torvalds 	.read		= sel_read_avc_cache_threshold,
13731da177e4SLinus Torvalds 	.write		= sel_write_avc_cache_threshold,
137457a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
13751da177e4SLinus Torvalds };
13761da177e4SLinus Torvalds 
13779c2e08c5SArjan van de Ven static const struct file_operations sel_avc_hash_stats_ops = {
13781da177e4SLinus Torvalds 	.read		= sel_read_avc_hash_stats,
137957a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
13801da177e4SLinus Torvalds };
13811da177e4SLinus Torvalds 
13821da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
13831da177e4SLinus Torvalds static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
13841da177e4SLinus Torvalds {
13851da177e4SLinus Torvalds 	int cpu;
13861da177e4SLinus Torvalds 
13874f4b6c1aSRusty Russell 	for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
13881da177e4SLinus Torvalds 		if (!cpu_possible(cpu))
13891da177e4SLinus Torvalds 			continue;
13901da177e4SLinus Torvalds 		*idx = cpu + 1;
13911da177e4SLinus Torvalds 		return &per_cpu(avc_cache_stats, cpu);
13921da177e4SLinus Torvalds 	}
13931da177e4SLinus Torvalds 	return NULL;
13941da177e4SLinus Torvalds }
13951da177e4SLinus Torvalds 
13961da177e4SLinus Torvalds static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
13971da177e4SLinus Torvalds {
13981da177e4SLinus Torvalds 	loff_t n = *pos - 1;
13991da177e4SLinus Torvalds 
14001da177e4SLinus Torvalds 	if (*pos == 0)
14011da177e4SLinus Torvalds 		return SEQ_START_TOKEN;
14021da177e4SLinus Torvalds 
14031da177e4SLinus Torvalds 	return sel_avc_get_stat_idx(&n);
14041da177e4SLinus Torvalds }
14051da177e4SLinus Torvalds 
14061da177e4SLinus Torvalds static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
14071da177e4SLinus Torvalds {
14081da177e4SLinus Torvalds 	return sel_avc_get_stat_idx(pos);
14091da177e4SLinus Torvalds }
14101da177e4SLinus Torvalds 
14111da177e4SLinus Torvalds static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
14121da177e4SLinus Torvalds {
14131da177e4SLinus Torvalds 	struct avc_cache_stats *st = v;
14141da177e4SLinus Torvalds 
14151da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
14161da177e4SLinus Torvalds 		seq_printf(seq, "lookups hits misses allocations reclaims "
14171da177e4SLinus Torvalds 			   "frees\n");
1418257313b2SLinus Torvalds 	else {
1419257313b2SLinus Torvalds 		unsigned int lookups = st->lookups;
1420257313b2SLinus Torvalds 		unsigned int misses = st->misses;
1421257313b2SLinus Torvalds 		unsigned int hits = lookups - misses;
1422257313b2SLinus Torvalds 		seq_printf(seq, "%u %u %u %u %u %u\n", lookups,
1423257313b2SLinus Torvalds 			   hits, misses, st->allocations,
14241da177e4SLinus Torvalds 			   st->reclaims, st->frees);
1425257313b2SLinus Torvalds 	}
14261da177e4SLinus Torvalds 	return 0;
14271da177e4SLinus Torvalds }
14281da177e4SLinus Torvalds 
14291da177e4SLinus Torvalds static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
14301da177e4SLinus Torvalds { }
14311da177e4SLinus Torvalds 
14321996a109SJan Engelhardt static const struct seq_operations sel_avc_cache_stats_seq_ops = {
14331da177e4SLinus Torvalds 	.start		= sel_avc_stats_seq_start,
14341da177e4SLinus Torvalds 	.next		= sel_avc_stats_seq_next,
14351da177e4SLinus Torvalds 	.show		= sel_avc_stats_seq_show,
14361da177e4SLinus Torvalds 	.stop		= sel_avc_stats_seq_stop,
14371da177e4SLinus Torvalds };
14381da177e4SLinus Torvalds 
14391da177e4SLinus Torvalds static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
14401da177e4SLinus Torvalds {
14411da177e4SLinus Torvalds 	return seq_open(file, &sel_avc_cache_stats_seq_ops);
14421da177e4SLinus Torvalds }
14431da177e4SLinus Torvalds 
14449c2e08c5SArjan van de Ven static const struct file_operations sel_avc_cache_stats_ops = {
14451da177e4SLinus Torvalds 	.open		= sel_open_avc_cache_stats,
14461da177e4SLinus Torvalds 	.read		= seq_read,
14471da177e4SLinus Torvalds 	.llseek		= seq_lseek,
14481da177e4SLinus Torvalds 	.release	= seq_release,
14491da177e4SLinus Torvalds };
14501da177e4SLinus Torvalds #endif
14511da177e4SLinus Torvalds 
14521da177e4SLinus Torvalds static int sel_make_avc_files(struct dentry *dir)
14531da177e4SLinus Torvalds {
1454b77a493bSEric Paris 	int i;
14551da177e4SLinus Torvalds 	static struct tree_descr files[] = {
14561da177e4SLinus Torvalds 		{ "cache_threshold",
14571da177e4SLinus Torvalds 		  &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
14581da177e4SLinus Torvalds 		{ "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
14591da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
14601da177e4SLinus Torvalds 		{ "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
14611da177e4SLinus Torvalds #endif
14621da177e4SLinus Torvalds 	};
14631da177e4SLinus Torvalds 
14646e20a64aSNicolas Kaiser 	for (i = 0; i < ARRAY_SIZE(files); i++) {
14651da177e4SLinus Torvalds 		struct inode *inode;
14661da177e4SLinus Torvalds 		struct dentry *dentry;
14671da177e4SLinus Torvalds 
14681da177e4SLinus Torvalds 		dentry = d_alloc_name(dir, files[i].name);
1469b77a493bSEric Paris 		if (!dentry)
1470b77a493bSEric Paris 			return -ENOMEM;
14711da177e4SLinus Torvalds 
14721da177e4SLinus Torvalds 		inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1473b77a493bSEric Paris 		if (!inode)
1474b77a493bSEric Paris 			return -ENOMEM;
1475b77a493bSEric Paris 
14761da177e4SLinus Torvalds 		inode->i_fop = files[i].ops;
14776174eafcSJames Carter 		inode->i_ino = ++sel_last_ino;
14781da177e4SLinus Torvalds 		d_add(dentry, inode);
14791da177e4SLinus Torvalds 	}
1480b77a493bSEric Paris 
1481b77a493bSEric Paris 	return 0;
14821da177e4SLinus Torvalds }
14831da177e4SLinus Torvalds 
1484f0ee2e46SJames Carter static ssize_t sel_read_initcon(struct file *file, char __user *buf,
1485f0ee2e46SJames Carter 				size_t count, loff_t *ppos)
1486f0ee2e46SJames Carter {
1487f0ee2e46SJames Carter 	char *con;
1488f0ee2e46SJames Carter 	u32 sid, len;
1489f0ee2e46SJames Carter 	ssize_t ret;
1490f0ee2e46SJames Carter 
1491*496ad9aaSAl Viro 	sid = file_inode(file)->i_ino&SEL_INO_MASK;
1492f0ee2e46SJames Carter 	ret = security_sid_to_context(sid, &con, &len);
1493b77a493bSEric Paris 	if (ret)
1494f0ee2e46SJames Carter 		return ret;
1495f0ee2e46SJames Carter 
1496f0ee2e46SJames Carter 	ret = simple_read_from_buffer(buf, count, ppos, con, len);
1497f0ee2e46SJames Carter 	kfree(con);
1498f0ee2e46SJames Carter 	return ret;
1499f0ee2e46SJames Carter }
1500f0ee2e46SJames Carter 
1501f0ee2e46SJames Carter static const struct file_operations sel_initcon_ops = {
1502f0ee2e46SJames Carter 	.read		= sel_read_initcon,
150357a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
1504f0ee2e46SJames Carter };
1505f0ee2e46SJames Carter 
1506f0ee2e46SJames Carter static int sel_make_initcon_files(struct dentry *dir)
1507f0ee2e46SJames Carter {
1508b77a493bSEric Paris 	int i;
1509f0ee2e46SJames Carter 
1510f0ee2e46SJames Carter 	for (i = 1; i <= SECINITSID_NUM; i++) {
1511f0ee2e46SJames Carter 		struct inode *inode;
1512f0ee2e46SJames Carter 		struct dentry *dentry;
1513f0ee2e46SJames Carter 		dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
1514b77a493bSEric Paris 		if (!dentry)
1515b77a493bSEric Paris 			return -ENOMEM;
1516f0ee2e46SJames Carter 
1517f0ee2e46SJames Carter 		inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1518b77a493bSEric Paris 		if (!inode)
1519b77a493bSEric Paris 			return -ENOMEM;
1520b77a493bSEric Paris 
1521f0ee2e46SJames Carter 		inode->i_fop = &sel_initcon_ops;
1522f0ee2e46SJames Carter 		inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1523f0ee2e46SJames Carter 		d_add(dentry, inode);
1524f0ee2e46SJames Carter 	}
1525b77a493bSEric Paris 
1526b77a493bSEric Paris 	return 0;
1527f0ee2e46SJames Carter }
1528f0ee2e46SJames Carter 
1529e47c8fc5SChristopher J. PeBenito static inline unsigned long sel_class_to_ino(u16 class)
1530e47c8fc5SChristopher J. PeBenito {
1531e47c8fc5SChristopher J. PeBenito 	return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1532e47c8fc5SChristopher J. PeBenito }
1533e47c8fc5SChristopher J. PeBenito 
1534e47c8fc5SChristopher J. PeBenito static inline u16 sel_ino_to_class(unsigned long ino)
1535e47c8fc5SChristopher J. PeBenito {
153692ae9e82SEric Paris 	return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1);
1537e47c8fc5SChristopher J. PeBenito }
1538e47c8fc5SChristopher J. PeBenito 
1539e47c8fc5SChristopher J. PeBenito static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1540e47c8fc5SChristopher J. PeBenito {
1541e47c8fc5SChristopher J. PeBenito 	return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1542e47c8fc5SChristopher J. PeBenito }
1543e47c8fc5SChristopher J. PeBenito 
1544e47c8fc5SChristopher J. PeBenito static inline u32 sel_ino_to_perm(unsigned long ino)
1545e47c8fc5SChristopher J. PeBenito {
1546e47c8fc5SChristopher J. PeBenito 	return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1547e47c8fc5SChristopher J. PeBenito }
1548e47c8fc5SChristopher J. PeBenito 
1549e47c8fc5SChristopher J. PeBenito static ssize_t sel_read_class(struct file *file, char __user *buf,
1550e47c8fc5SChristopher J. PeBenito 				size_t count, loff_t *ppos)
1551e47c8fc5SChristopher J. PeBenito {
1552*496ad9aaSAl Viro 	unsigned long ino = file_inode(file)->i_ino;
1553cc1dad71SAl Viro 	char res[TMPBUFLEN];
1554cc1dad71SAl Viro 	ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_class(ino));
1555cc1dad71SAl Viro 	return simple_read_from_buffer(buf, count, ppos, res, len);
1556e47c8fc5SChristopher J. PeBenito }
1557e47c8fc5SChristopher J. PeBenito 
1558e47c8fc5SChristopher J. PeBenito static const struct file_operations sel_class_ops = {
1559e47c8fc5SChristopher J. PeBenito 	.read		= sel_read_class,
156057a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
1561e47c8fc5SChristopher J. PeBenito };
1562e47c8fc5SChristopher J. PeBenito 
1563e47c8fc5SChristopher J. PeBenito static ssize_t sel_read_perm(struct file *file, char __user *buf,
1564e47c8fc5SChristopher J. PeBenito 				size_t count, loff_t *ppos)
1565e47c8fc5SChristopher J. PeBenito {
1566*496ad9aaSAl Viro 	unsigned long ino = file_inode(file)->i_ino;
1567cc1dad71SAl Viro 	char res[TMPBUFLEN];
1568cc1dad71SAl Viro 	ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino));
1569cc1dad71SAl Viro 	return simple_read_from_buffer(buf, count, ppos, res, len);
1570e47c8fc5SChristopher J. PeBenito }
1571e47c8fc5SChristopher J. PeBenito 
1572e47c8fc5SChristopher J. PeBenito static const struct file_operations sel_perm_ops = {
1573e47c8fc5SChristopher J. PeBenito 	.read		= sel_read_perm,
157457a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
1575e47c8fc5SChristopher J. PeBenito };
1576e47c8fc5SChristopher J. PeBenito 
15773bb56b25SPaul Moore static ssize_t sel_read_policycap(struct file *file, char __user *buf,
15783bb56b25SPaul Moore 				  size_t count, loff_t *ppos)
15793bb56b25SPaul Moore {
15803bb56b25SPaul Moore 	int value;
15813bb56b25SPaul Moore 	char tmpbuf[TMPBUFLEN];
15823bb56b25SPaul Moore 	ssize_t length;
1583*496ad9aaSAl Viro 	unsigned long i_ino = file_inode(file)->i_ino;
15843bb56b25SPaul Moore 
15853bb56b25SPaul Moore 	value = security_policycap_supported(i_ino & SEL_INO_MASK);
15863bb56b25SPaul Moore 	length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
15873bb56b25SPaul Moore 
15883bb56b25SPaul Moore 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
15893bb56b25SPaul Moore }
15903bb56b25SPaul Moore 
15913bb56b25SPaul Moore static const struct file_operations sel_policycap_ops = {
15923bb56b25SPaul Moore 	.read		= sel_read_policycap,
159357a62c23SArnd Bergmann 	.llseek		= generic_file_llseek,
15943bb56b25SPaul Moore };
15953bb56b25SPaul Moore 
1596e47c8fc5SChristopher J. PeBenito static int sel_make_perm_files(char *objclass, int classvalue,
1597e47c8fc5SChristopher J. PeBenito 				struct dentry *dir)
1598e47c8fc5SChristopher J. PeBenito {
1599b77a493bSEric Paris 	int i, rc, nperms;
1600e47c8fc5SChristopher J. PeBenito 	char **perms;
1601e47c8fc5SChristopher J. PeBenito 
1602e47c8fc5SChristopher J. PeBenito 	rc = security_get_permissions(objclass, &perms, &nperms);
1603e47c8fc5SChristopher J. PeBenito 	if (rc)
1604b77a493bSEric Paris 		return rc;
1605e47c8fc5SChristopher J. PeBenito 
1606e47c8fc5SChristopher J. PeBenito 	for (i = 0; i < nperms; i++) {
1607e47c8fc5SChristopher J. PeBenito 		struct inode *inode;
1608e47c8fc5SChristopher J. PeBenito 		struct dentry *dentry;
1609e47c8fc5SChristopher J. PeBenito 
1610b77a493bSEric Paris 		rc = -ENOMEM;
1611e47c8fc5SChristopher J. PeBenito 		dentry = d_alloc_name(dir, perms[i]);
1612b77a493bSEric Paris 		if (!dentry)
1613b77a493bSEric Paris 			goto out;
1614e47c8fc5SChristopher J. PeBenito 
1615e47c8fc5SChristopher J. PeBenito 		rc = -ENOMEM;
1616b77a493bSEric Paris 		inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1617b77a493bSEric Paris 		if (!inode)
1618b77a493bSEric Paris 			goto out;
1619b77a493bSEric Paris 
1620e47c8fc5SChristopher J. PeBenito 		inode->i_fop = &sel_perm_ops;
1621e47c8fc5SChristopher J. PeBenito 		/* i+1 since perm values are 1-indexed */
1622e47c8fc5SChristopher J. PeBenito 		inode->i_ino = sel_perm_to_ino(classvalue, i + 1);
1623e47c8fc5SChristopher J. PeBenito 		d_add(dentry, inode);
1624e47c8fc5SChristopher J. PeBenito 	}
1625b77a493bSEric Paris 	rc = 0;
1626b77a493bSEric Paris out:
1627e47c8fc5SChristopher J. PeBenito 	for (i = 0; i < nperms; i++)
1628e47c8fc5SChristopher J. PeBenito 		kfree(perms[i]);
1629e47c8fc5SChristopher J. PeBenito 	kfree(perms);
1630e47c8fc5SChristopher J. PeBenito 	return rc;
1631e47c8fc5SChristopher J. PeBenito }
1632e47c8fc5SChristopher J. PeBenito 
1633e47c8fc5SChristopher J. PeBenito static int sel_make_class_dir_entries(char *classname, int index,
1634e47c8fc5SChristopher J. PeBenito 					struct dentry *dir)
1635e47c8fc5SChristopher J. PeBenito {
1636e47c8fc5SChristopher J. PeBenito 	struct dentry *dentry = NULL;
1637e47c8fc5SChristopher J. PeBenito 	struct inode *inode = NULL;
1638e47c8fc5SChristopher J. PeBenito 	int rc;
1639e47c8fc5SChristopher J. PeBenito 
1640e47c8fc5SChristopher J. PeBenito 	dentry = d_alloc_name(dir, "index");
1641b77a493bSEric Paris 	if (!dentry)
1642b77a493bSEric Paris 		return -ENOMEM;
1643e47c8fc5SChristopher J. PeBenito 
1644e47c8fc5SChristopher J. PeBenito 	inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1645b77a493bSEric Paris 	if (!inode)
1646b77a493bSEric Paris 		return -ENOMEM;
1647e47c8fc5SChristopher J. PeBenito 
1648e47c8fc5SChristopher J. PeBenito 	inode->i_fop = &sel_class_ops;
1649e47c8fc5SChristopher J. PeBenito 	inode->i_ino = sel_class_to_ino(index);
1650e47c8fc5SChristopher J. PeBenito 	d_add(dentry, inode);
1651e47c8fc5SChristopher J. PeBenito 
1652a1c2aa1eSAl Viro 	dentry = sel_make_dir(dir, "perms", &last_class_ino);
1653a1c2aa1eSAl Viro 	if (IS_ERR(dentry))
1654a1c2aa1eSAl Viro 		return PTR_ERR(dentry);
1655e47c8fc5SChristopher J. PeBenito 
1656e47c8fc5SChristopher J. PeBenito 	rc = sel_make_perm_files(classname, index, dentry);
1657e47c8fc5SChristopher J. PeBenito 
1658e47c8fc5SChristopher J. PeBenito 	return rc;
1659e47c8fc5SChristopher J. PeBenito }
1660e47c8fc5SChristopher J. PeBenito 
1661e47c8fc5SChristopher J. PeBenito static void sel_remove_classes(void)
1662e47c8fc5SChristopher J. PeBenito {
1663e47c8fc5SChristopher J. PeBenito 	struct list_head *class_node;
1664e47c8fc5SChristopher J. PeBenito 
1665e47c8fc5SChristopher J. PeBenito 	list_for_each(class_node, &class_dir->d_subdirs) {
1666e47c8fc5SChristopher J. PeBenito 		struct dentry *class_subdir = list_entry(class_node,
1667e47c8fc5SChristopher J. PeBenito 					struct dentry, d_u.d_child);
1668e47c8fc5SChristopher J. PeBenito 		struct list_head *class_subdir_node;
1669e47c8fc5SChristopher J. PeBenito 
1670e47c8fc5SChristopher J. PeBenito 		list_for_each(class_subdir_node, &class_subdir->d_subdirs) {
1671e47c8fc5SChristopher J. PeBenito 			struct dentry *d = list_entry(class_subdir_node,
1672e47c8fc5SChristopher J. PeBenito 						struct dentry, d_u.d_child);
1673e47c8fc5SChristopher J. PeBenito 
1674e47c8fc5SChristopher J. PeBenito 			if (d->d_inode)
1675e47c8fc5SChristopher J. PeBenito 				if (d->d_inode->i_mode & S_IFDIR)
1676e47c8fc5SChristopher J. PeBenito 					sel_remove_entries(d);
1677e47c8fc5SChristopher J. PeBenito 		}
1678e47c8fc5SChristopher J. PeBenito 
1679e47c8fc5SChristopher J. PeBenito 		sel_remove_entries(class_subdir);
1680e47c8fc5SChristopher J. PeBenito 	}
1681e47c8fc5SChristopher J. PeBenito 
1682e47c8fc5SChristopher J. PeBenito 	sel_remove_entries(class_dir);
1683e47c8fc5SChristopher J. PeBenito }
1684e47c8fc5SChristopher J. PeBenito 
1685e47c8fc5SChristopher J. PeBenito static int sel_make_classes(void)
1686e47c8fc5SChristopher J. PeBenito {
1687b77a493bSEric Paris 	int rc, nclasses, i;
1688e47c8fc5SChristopher J. PeBenito 	char **classes;
1689e47c8fc5SChristopher J. PeBenito 
1690e47c8fc5SChristopher J. PeBenito 	/* delete any existing entries */
1691e47c8fc5SChristopher J. PeBenito 	sel_remove_classes();
1692e47c8fc5SChristopher J. PeBenito 
1693e47c8fc5SChristopher J. PeBenito 	rc = security_get_classes(&classes, &nclasses);
1694b77a493bSEric Paris 	if (rc)
1695b77a493bSEric Paris 		return rc;
1696e47c8fc5SChristopher J. PeBenito 
1697e47c8fc5SChristopher J. PeBenito 	/* +2 since classes are 1-indexed */
1698e47c8fc5SChristopher J. PeBenito 	last_class_ino = sel_class_to_ino(nclasses + 2);
1699e47c8fc5SChristopher J. PeBenito 
1700e47c8fc5SChristopher J. PeBenito 	for (i = 0; i < nclasses; i++) {
1701e47c8fc5SChristopher J. PeBenito 		struct dentry *class_name_dir;
1702e47c8fc5SChristopher J. PeBenito 
1703a1c2aa1eSAl Viro 		class_name_dir = sel_make_dir(class_dir, classes[i],
1704e47c8fc5SChristopher J. PeBenito 				&last_class_ino);
1705a1c2aa1eSAl Viro 		if (IS_ERR(class_name_dir)) {
1706a1c2aa1eSAl Viro 			rc = PTR_ERR(class_name_dir);
1707b77a493bSEric Paris 			goto out;
1708a1c2aa1eSAl Viro 		}
1709e47c8fc5SChristopher J. PeBenito 
1710e47c8fc5SChristopher J. PeBenito 		/* i+1 since class values are 1-indexed */
1711e47c8fc5SChristopher J. PeBenito 		rc = sel_make_class_dir_entries(classes[i], i + 1,
1712e47c8fc5SChristopher J. PeBenito 				class_name_dir);
1713e47c8fc5SChristopher J. PeBenito 		if (rc)
1714b77a493bSEric Paris 			goto out;
1715e47c8fc5SChristopher J. PeBenito 	}
1716b77a493bSEric Paris 	rc = 0;
1717b77a493bSEric Paris out:
1718e47c8fc5SChristopher J. PeBenito 	for (i = 0; i < nclasses; i++)
1719e47c8fc5SChristopher J. PeBenito 		kfree(classes[i]);
1720e47c8fc5SChristopher J. PeBenito 	kfree(classes);
1721e47c8fc5SChristopher J. PeBenito 	return rc;
1722e47c8fc5SChristopher J. PeBenito }
1723e47c8fc5SChristopher J. PeBenito 
17243bb56b25SPaul Moore static int sel_make_policycap(void)
17253bb56b25SPaul Moore {
17263bb56b25SPaul Moore 	unsigned int iter;
17273bb56b25SPaul Moore 	struct dentry *dentry = NULL;
17283bb56b25SPaul Moore 	struct inode *inode = NULL;
17293bb56b25SPaul Moore 
17303bb56b25SPaul Moore 	sel_remove_entries(policycap_dir);
17313bb56b25SPaul Moore 
17323bb56b25SPaul Moore 	for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
17333bb56b25SPaul Moore 		if (iter < ARRAY_SIZE(policycap_names))
17343bb56b25SPaul Moore 			dentry = d_alloc_name(policycap_dir,
17353bb56b25SPaul Moore 					      policycap_names[iter]);
17363bb56b25SPaul Moore 		else
17373bb56b25SPaul Moore 			dentry = d_alloc_name(policycap_dir, "unknown");
17383bb56b25SPaul Moore 
17393bb56b25SPaul Moore 		if (dentry == NULL)
17403bb56b25SPaul Moore 			return -ENOMEM;
17413bb56b25SPaul Moore 
17423bb56b25SPaul Moore 		inode = sel_make_inode(policycap_dir->d_sb, S_IFREG | S_IRUGO);
17433bb56b25SPaul Moore 		if (inode == NULL)
17443bb56b25SPaul Moore 			return -ENOMEM;
17453bb56b25SPaul Moore 
17463bb56b25SPaul Moore 		inode->i_fop = &sel_policycap_ops;
17473bb56b25SPaul Moore 		inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
17483bb56b25SPaul Moore 		d_add(dentry, inode);
17493bb56b25SPaul Moore 	}
17503bb56b25SPaul Moore 
17513bb56b25SPaul Moore 	return 0;
17523bb56b25SPaul Moore }
17533bb56b25SPaul Moore 
1754a1c2aa1eSAl Viro static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
17550dd4ae51SChristopher J. PeBenito 			unsigned long *ino)
17561da177e4SLinus Torvalds {
1757a1c2aa1eSAl Viro 	struct dentry *dentry = d_alloc_name(dir, name);
17581da177e4SLinus Torvalds 	struct inode *inode;
17591da177e4SLinus Torvalds 
1760a1c2aa1eSAl Viro 	if (!dentry)
1761a1c2aa1eSAl Viro 		return ERR_PTR(-ENOMEM);
1762a1c2aa1eSAl Viro 
1763a1c2aa1eSAl Viro 	inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1764a1c2aa1eSAl Viro 	if (!inode) {
1765a1c2aa1eSAl Viro 		dput(dentry);
1766a1c2aa1eSAl Viro 		return ERR_PTR(-ENOMEM);
1767a1c2aa1eSAl Viro 	}
1768b77a493bSEric Paris 
17691da177e4SLinus Torvalds 	inode->i_op = &simple_dir_inode_operations;
17701da177e4SLinus Torvalds 	inode->i_fop = &simple_dir_operations;
17710dd4ae51SChristopher J. PeBenito 	inode->i_ino = ++(*ino);
177240e906f8SJames Morris 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
1773d8c76e6fSDave Hansen 	inc_nlink(inode);
17741da177e4SLinus Torvalds 	d_add(dentry, inode);
1775edb20fb5SJames Morris 	/* bump link count on parent directory, too */
1776a1c2aa1eSAl Viro 	inc_nlink(dir->d_inode);
1777b77a493bSEric Paris 
1778a1c2aa1eSAl Viro 	return dentry;
17791da177e4SLinus Torvalds }
17801da177e4SLinus Torvalds 
17811da177e4SLinus Torvalds static int sel_fill_super(struct super_block *sb, void *data, int silent)
17821da177e4SLinus Torvalds {
17831da177e4SLinus Torvalds 	int ret;
17841da177e4SLinus Torvalds 	struct dentry *dentry;
1785a1c2aa1eSAl Viro 	struct inode *inode;
17861da177e4SLinus Torvalds 	struct inode_security_struct *isec;
17871da177e4SLinus Torvalds 
17881da177e4SLinus Torvalds 	static struct tree_descr selinux_files[] = {
17891da177e4SLinus Torvalds 		[SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
17901da177e4SLinus Torvalds 		[SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
1791ce9982d0SStephen Smalley 		[SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
17921da177e4SLinus Torvalds 		[SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
17931da177e4SLinus Torvalds 		[SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
17941da177e4SLinus Torvalds 		[SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
17951da177e4SLinus Torvalds 		[SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
17961da177e4SLinus Torvalds 		[SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
17971da177e4SLinus Torvalds 		[SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
17981da177e4SLinus Torvalds 		[SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
17991da177e4SLinus Torvalds 		[SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
18001da177e4SLinus Torvalds 		[SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
18011da177e4SLinus Torvalds 		[SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
18023f12070eSEric Paris 		[SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
18033f12070eSEric Paris 		[SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
180411904167SKaiGai Kohei 		[SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO},
180572e8c859SEric Paris 		[SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO},
18061da177e4SLinus Torvalds 		/* last one */ {""}
18071da177e4SLinus Torvalds 	};
18081da177e4SLinus Torvalds 	ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
18091da177e4SLinus Torvalds 	if (ret)
1810161ce45aSJames Morris 		goto err;
18111da177e4SLinus Torvalds 
1812a1c2aa1eSAl Viro 	bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &sel_last_ino);
1813a1c2aa1eSAl Viro 	if (IS_ERR(bool_dir)) {
1814a1c2aa1eSAl Viro 		ret = PTR_ERR(bool_dir);
1815a1c2aa1eSAl Viro 		bool_dir = NULL;
1816161ce45aSJames Morris 		goto err;
1817a1c2aa1eSAl Viro 	}
18181da177e4SLinus Torvalds 
1819b77a493bSEric Paris 	ret = -ENOMEM;
18201da177e4SLinus Torvalds 	dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
1821b77a493bSEric Paris 	if (!dentry)
1822161ce45aSJames Morris 		goto err;
18231da177e4SLinus Torvalds 
1824161ce45aSJames Morris 	ret = -ENOMEM;
1825b77a493bSEric Paris 	inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
1826b77a493bSEric Paris 	if (!inode)
1827161ce45aSJames Morris 		goto err;
1828b77a493bSEric Paris 
18296174eafcSJames Carter 	inode->i_ino = ++sel_last_ino;
18301da177e4SLinus Torvalds 	isec = (struct inode_security_struct *)inode->i_security;
18311da177e4SLinus Torvalds 	isec->sid = SECINITSID_DEVNULL;
18321da177e4SLinus Torvalds 	isec->sclass = SECCLASS_CHR_FILE;
18331da177e4SLinus Torvalds 	isec->initialized = 1;
18341da177e4SLinus Torvalds 
18351da177e4SLinus Torvalds 	init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
18361da177e4SLinus Torvalds 	d_add(dentry, inode);
1837765927b2SAl Viro 	selinux_null.dentry = dentry;
18381da177e4SLinus Torvalds 
1839a1c2aa1eSAl Viro 	dentry = sel_make_dir(sb->s_root, "avc", &sel_last_ino);
1840a1c2aa1eSAl Viro 	if (IS_ERR(dentry)) {
1841a1c2aa1eSAl Viro 		ret = PTR_ERR(dentry);
1842161ce45aSJames Morris 		goto err;
1843a1c2aa1eSAl Viro 	}
18441da177e4SLinus Torvalds 
18451da177e4SLinus Torvalds 	ret = sel_make_avc_files(dentry);
18461da177e4SLinus Torvalds 	if (ret)
1847161ce45aSJames Morris 		goto err;
1848f0ee2e46SJames Carter 
1849a1c2aa1eSAl Viro 	dentry = sel_make_dir(sb->s_root, "initial_contexts", &sel_last_ino);
1850a1c2aa1eSAl Viro 	if (IS_ERR(dentry)) {
1851a1c2aa1eSAl Viro 		ret = PTR_ERR(dentry);
1852f0ee2e46SJames Carter 		goto err;
1853a1c2aa1eSAl Viro 	}
1854f0ee2e46SJames Carter 
1855f0ee2e46SJames Carter 	ret = sel_make_initcon_files(dentry);
1856f0ee2e46SJames Carter 	if (ret)
1857f0ee2e46SJames Carter 		goto err;
1858f0ee2e46SJames Carter 
1859a1c2aa1eSAl Viro 	class_dir = sel_make_dir(sb->s_root, "class", &sel_last_ino);
1860a1c2aa1eSAl Viro 	if (IS_ERR(class_dir)) {
1861a1c2aa1eSAl Viro 		ret = PTR_ERR(class_dir);
1862a1c2aa1eSAl Viro 		class_dir = NULL;
1863e47c8fc5SChristopher J. PeBenito 		goto err;
1864a1c2aa1eSAl Viro 	}
1865e47c8fc5SChristopher J. PeBenito 
1866a1c2aa1eSAl Viro 	policycap_dir = sel_make_dir(sb->s_root, "policy_capabilities", &sel_last_ino);
1867a1c2aa1eSAl Viro 	if (IS_ERR(policycap_dir)) {
1868a1c2aa1eSAl Viro 		ret = PTR_ERR(policycap_dir);
1869a1c2aa1eSAl Viro 		policycap_dir = NULL;
1870e47c8fc5SChristopher J. PeBenito 		goto err;
1871a1c2aa1eSAl Viro 	}
1872b77a493bSEric Paris 	return 0;
1873161ce45aSJames Morris err:
1874744ba35eSEric Paris 	printk(KERN_ERR "SELinux: %s:  failed while creating inodes\n",
1875744ba35eSEric Paris 		__func__);
1876b77a493bSEric Paris 	return ret;
18771da177e4SLinus Torvalds }
18781da177e4SLinus Torvalds 
1879fc14f2feSAl Viro static struct dentry *sel_mount(struct file_system_type *fs_type,
1880fc14f2feSAl Viro 		      int flags, const char *dev_name, void *data)
18811da177e4SLinus Torvalds {
1882fc14f2feSAl Viro 	return mount_single(fs_type, flags, data, sel_fill_super);
18831da177e4SLinus Torvalds }
18841da177e4SLinus Torvalds 
18851da177e4SLinus Torvalds static struct file_system_type sel_fs_type = {
18861da177e4SLinus Torvalds 	.name		= "selinuxfs",
1887fc14f2feSAl Viro 	.mount		= sel_mount,
18881da177e4SLinus Torvalds 	.kill_sb	= kill_litter_super,
18891da177e4SLinus Torvalds };
18901da177e4SLinus Torvalds 
18911da177e4SLinus Torvalds struct vfsmount *selinuxfs_mount;
18927a627e3bSGreg Kroah-Hartman static struct kobject *selinuxfs_kobj;
18931da177e4SLinus Torvalds 
18941da177e4SLinus Torvalds static int __init init_sel_fs(void)
18951da177e4SLinus Torvalds {
18961da177e4SLinus Torvalds 	int err;
18971da177e4SLinus Torvalds 
18981da177e4SLinus Torvalds 	if (!selinux_enabled)
18991da177e4SLinus Torvalds 		return 0;
19007a627e3bSGreg Kroah-Hartman 
19017a627e3bSGreg Kroah-Hartman 	selinuxfs_kobj = kobject_create_and_add("selinux", fs_kobj);
19027a627e3bSGreg Kroah-Hartman 	if (!selinuxfs_kobj)
19037a627e3bSGreg Kroah-Hartman 		return -ENOMEM;
19047a627e3bSGreg Kroah-Hartman 
19051da177e4SLinus Torvalds 	err = register_filesystem(&sel_fs_type);
19067a627e3bSGreg Kroah-Hartman 	if (err) {
19077a627e3bSGreg Kroah-Hartman 		kobject_put(selinuxfs_kobj);
1908b77a493bSEric Paris 		return err;
19097a627e3bSGreg Kroah-Hartman 	}
1910b77a493bSEric Paris 
1911765927b2SAl Viro 	selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type);
19121da177e4SLinus Torvalds 	if (IS_ERR(selinuxfs_mount)) {
19131da177e4SLinus Torvalds 		printk(KERN_ERR "selinuxfs:  could not mount!\n");
19141da177e4SLinus Torvalds 		err = PTR_ERR(selinuxfs_mount);
19151da177e4SLinus Torvalds 		selinuxfs_mount = NULL;
19161da177e4SLinus Torvalds 	}
1917b77a493bSEric Paris 
19181da177e4SLinus Torvalds 	return err;
19191da177e4SLinus Torvalds }
19201da177e4SLinus Torvalds 
19211da177e4SLinus Torvalds __initcall(init_sel_fs);
19221da177e4SLinus Torvalds 
19231da177e4SLinus Torvalds #ifdef CONFIG_SECURITY_SELINUX_DISABLE
19241da177e4SLinus Torvalds void exit_sel_fs(void)
19251da177e4SLinus Torvalds {
19267a627e3bSGreg Kroah-Hartman 	kobject_put(selinuxfs_kobj);
1927423e0ab0STim Chen 	kern_unmount(selinuxfs_mount);
19281da177e4SLinus Torvalds 	unregister_filesystem(&sel_fs_type);
19291da177e4SLinus Torvalds }
19301da177e4SLinus Torvalds #endif
1931