xref: /openbmc/linux/security/selinux/selinuxfs.c (revision 6396bb221514d2876fd6dc0aa2a1f240d99b37bb)
1 /* Updated: Karl MacMillan <kmacmillan@tresys.com>
2  *
3  *	Added conditional policy language extensions
4  *
5  *  Updated: Hewlett-Packard <paul@paul-moore.com>
6  *
7  *	Added support for the policy capability bitmap
8  *
9  * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
10  * Copyright (C) 2003 - 2004 Tresys Technology, LLC
11  * Copyright (C) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
12  *	This program is free software; you can redistribute it and/or modify
13  *	it under the terms of the GNU General Public License as published by
14  *	the Free Software Foundation, version 2.
15  */
16 
17 #include <linux/kernel.h>
18 #include <linux/pagemap.h>
19 #include <linux/slab.h>
20 #include <linux/vmalloc.h>
21 #include <linux/fs.h>
22 #include <linux/mount.h>
23 #include <linux/mutex.h>
24 #include <linux/init.h>
25 #include <linux/string.h>
26 #include <linux/security.h>
27 #include <linux/major.h>
28 #include <linux/seq_file.h>
29 #include <linux/percpu.h>
30 #include <linux/audit.h>
31 #include <linux/uaccess.h>
32 #include <linux/kobject.h>
33 #include <linux/ctype.h>
34 
35 /* selinuxfs pseudo filesystem for exporting the security policy API.
36    Based on the proc code and the fs/nfsd/nfsctl.c code. */
37 
38 #include "flask.h"
39 #include "avc.h"
40 #include "avc_ss.h"
41 #include "security.h"
42 #include "objsec.h"
43 #include "conditional.h"
44 
45 enum sel_inos {
46 	SEL_ROOT_INO = 2,
47 	SEL_LOAD,	/* load policy */
48 	SEL_ENFORCE,	/* get or set enforcing status */
49 	SEL_CONTEXT,	/* validate context */
50 	SEL_ACCESS,	/* compute access decision */
51 	SEL_CREATE,	/* compute create labeling decision */
52 	SEL_RELABEL,	/* compute relabeling decision */
53 	SEL_USER,	/* compute reachable user contexts */
54 	SEL_POLICYVERS,	/* return policy version for this kernel */
55 	SEL_COMMIT_BOOLS, /* commit new boolean values */
56 	SEL_MLS,	/* return if MLS policy is enabled */
57 	SEL_DISABLE,	/* disable SELinux until next reboot */
58 	SEL_MEMBER,	/* compute polyinstantiation membership decision */
59 	SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
60 	SEL_COMPAT_NET,	/* whether to use old compat network packet controls */
61 	SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */
62 	SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */
63 	SEL_STATUS,	/* export current status using mmap() */
64 	SEL_POLICY,	/* allow userspace to read the in kernel policy */
65 	SEL_VALIDATE_TRANS, /* compute validatetrans decision */
66 	SEL_INO_NEXT,	/* The next inode number to use */
67 };
68 
69 struct selinux_fs_info {
70 	struct dentry *bool_dir;
71 	unsigned int bool_num;
72 	char **bool_pending_names;
73 	unsigned int *bool_pending_values;
74 	struct dentry *class_dir;
75 	unsigned long last_class_ino;
76 	bool policy_opened;
77 	struct dentry *policycap_dir;
78 	struct mutex mutex;
79 	unsigned long last_ino;
80 	struct selinux_state *state;
81 	struct super_block *sb;
82 };
83 
84 static int selinux_fs_info_create(struct super_block *sb)
85 {
86 	struct selinux_fs_info *fsi;
87 
88 	fsi = kzalloc(sizeof(*fsi), GFP_KERNEL);
89 	if (!fsi)
90 		return -ENOMEM;
91 
92 	mutex_init(&fsi->mutex);
93 	fsi->last_ino = SEL_INO_NEXT - 1;
94 	fsi->state = &selinux_state;
95 	fsi->sb = sb;
96 	sb->s_fs_info = fsi;
97 	return 0;
98 }
99 
100 static void selinux_fs_info_free(struct super_block *sb)
101 {
102 	struct selinux_fs_info *fsi = sb->s_fs_info;
103 	int i;
104 
105 	if (fsi) {
106 		for (i = 0; i < fsi->bool_num; i++)
107 			kfree(fsi->bool_pending_names[i]);
108 		kfree(fsi->bool_pending_names);
109 		kfree(fsi->bool_pending_values);
110 	}
111 	kfree(sb->s_fs_info);
112 	sb->s_fs_info = NULL;
113 }
114 
115 #define SEL_INITCON_INO_OFFSET		0x01000000
116 #define SEL_BOOL_INO_OFFSET		0x02000000
117 #define SEL_CLASS_INO_OFFSET		0x04000000
118 #define SEL_POLICYCAP_INO_OFFSET	0x08000000
119 #define SEL_INO_MASK			0x00ffffff
120 
121 #define TMPBUFLEN	12
122 static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
123 				size_t count, loff_t *ppos)
124 {
125 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
126 	char tmpbuf[TMPBUFLEN];
127 	ssize_t length;
128 
129 	length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
130 			   enforcing_enabled(fsi->state));
131 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
132 }
133 
134 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
135 static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
136 				 size_t count, loff_t *ppos)
137 
138 {
139 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
140 	struct selinux_state *state = fsi->state;
141 	char *page = NULL;
142 	ssize_t length;
143 	int old_value, new_value;
144 
145 	if (count >= PAGE_SIZE)
146 		return -ENOMEM;
147 
148 	/* No partial writes. */
149 	if (*ppos != 0)
150 		return -EINVAL;
151 
152 	page = memdup_user_nul(buf, count);
153 	if (IS_ERR(page))
154 		return PTR_ERR(page);
155 
156 	length = -EINVAL;
157 	if (sscanf(page, "%d", &new_value) != 1)
158 		goto out;
159 
160 	new_value = !!new_value;
161 
162 	old_value = enforcing_enabled(state);
163 	if (new_value != old_value) {
164 		length = avc_has_perm(&selinux_state,
165 				      current_sid(), SECINITSID_SECURITY,
166 				      SECCLASS_SECURITY, SECURITY__SETENFORCE,
167 				      NULL);
168 		if (length)
169 			goto out;
170 		audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS,
171 			"enforcing=%d old_enforcing=%d auid=%u ses=%u"
172 			" enabled=%d old-enabled=%d lsm=selinux res=1",
173 			new_value, old_value,
174 			from_kuid(&init_user_ns, audit_get_loginuid(current)),
175 			audit_get_sessionid(current),
176 			selinux_enabled, selinux_enabled);
177 		enforcing_set(state, new_value);
178 		if (new_value)
179 			avc_ss_reset(state->avc, 0);
180 		selnl_notify_setenforce(new_value);
181 		selinux_status_update_setenforce(state, new_value);
182 		if (!new_value)
183 			call_lsm_notifier(LSM_POLICY_CHANGE, NULL);
184 	}
185 	length = count;
186 out:
187 	kfree(page);
188 	return length;
189 }
190 #else
191 #define sel_write_enforce NULL
192 #endif
193 
194 static const struct file_operations sel_enforce_ops = {
195 	.read		= sel_read_enforce,
196 	.write		= sel_write_enforce,
197 	.llseek		= generic_file_llseek,
198 };
199 
200 static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf,
201 					size_t count, loff_t *ppos)
202 {
203 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
204 	struct selinux_state *state = fsi->state;
205 	char tmpbuf[TMPBUFLEN];
206 	ssize_t length;
207 	ino_t ino = file_inode(filp)->i_ino;
208 	int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
209 		security_get_reject_unknown(state) :
210 		!security_get_allow_unknown(state);
211 
212 	length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
213 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
214 }
215 
216 static const struct file_operations sel_handle_unknown_ops = {
217 	.read		= sel_read_handle_unknown,
218 	.llseek		= generic_file_llseek,
219 };
220 
221 static int sel_open_handle_status(struct inode *inode, struct file *filp)
222 {
223 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
224 	struct page    *status = selinux_kernel_status_page(fsi->state);
225 
226 	if (!status)
227 		return -ENOMEM;
228 
229 	filp->private_data = status;
230 
231 	return 0;
232 }
233 
234 static ssize_t sel_read_handle_status(struct file *filp, char __user *buf,
235 				      size_t count, loff_t *ppos)
236 {
237 	struct page    *status = filp->private_data;
238 
239 	BUG_ON(!status);
240 
241 	return simple_read_from_buffer(buf, count, ppos,
242 				       page_address(status),
243 				       sizeof(struct selinux_kernel_status));
244 }
245 
246 static int sel_mmap_handle_status(struct file *filp,
247 				  struct vm_area_struct *vma)
248 {
249 	struct page    *status = filp->private_data;
250 	unsigned long	size = vma->vm_end - vma->vm_start;
251 
252 	BUG_ON(!status);
253 
254 	/* only allows one page from the head */
255 	if (vma->vm_pgoff > 0 || size != PAGE_SIZE)
256 		return -EIO;
257 	/* disallow writable mapping */
258 	if (vma->vm_flags & VM_WRITE)
259 		return -EPERM;
260 	/* disallow mprotect() turns it into writable */
261 	vma->vm_flags &= ~VM_MAYWRITE;
262 
263 	return remap_pfn_range(vma, vma->vm_start,
264 			       page_to_pfn(status),
265 			       size, vma->vm_page_prot);
266 }
267 
268 static const struct file_operations sel_handle_status_ops = {
269 	.open		= sel_open_handle_status,
270 	.read		= sel_read_handle_status,
271 	.mmap		= sel_mmap_handle_status,
272 	.llseek		= generic_file_llseek,
273 };
274 
275 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
276 static ssize_t sel_write_disable(struct file *file, const char __user *buf,
277 				 size_t count, loff_t *ppos)
278 
279 {
280 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
281 	char *page;
282 	ssize_t length;
283 	int new_value;
284 	int enforcing;
285 
286 	if (count >= PAGE_SIZE)
287 		return -ENOMEM;
288 
289 	/* No partial writes. */
290 	if (*ppos != 0)
291 		return -EINVAL;
292 
293 	page = memdup_user_nul(buf, count);
294 	if (IS_ERR(page))
295 		return PTR_ERR(page);
296 
297 	length = -EINVAL;
298 	if (sscanf(page, "%d", &new_value) != 1)
299 		goto out;
300 
301 	if (new_value) {
302 		enforcing = enforcing_enabled(fsi->state);
303 		length = selinux_disable(fsi->state);
304 		if (length)
305 			goto out;
306 		audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS,
307 			"enforcing=%d old_enforcing=%d auid=%u ses=%u"
308 			" enabled=%d old-enabled=%d lsm=selinux res=1",
309 			enforcing, enforcing,
310 			from_kuid(&init_user_ns, audit_get_loginuid(current)),
311 			audit_get_sessionid(current), 0, 1);
312 	}
313 
314 	length = count;
315 out:
316 	kfree(page);
317 	return length;
318 }
319 #else
320 #define sel_write_disable NULL
321 #endif
322 
323 static const struct file_operations sel_disable_ops = {
324 	.write		= sel_write_disable,
325 	.llseek		= generic_file_llseek,
326 };
327 
328 static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
329 				   size_t count, loff_t *ppos)
330 {
331 	char tmpbuf[TMPBUFLEN];
332 	ssize_t length;
333 
334 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
335 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
336 }
337 
338 static const struct file_operations sel_policyvers_ops = {
339 	.read		= sel_read_policyvers,
340 	.llseek		= generic_file_llseek,
341 };
342 
343 /* declaration for sel_write_load */
344 static int sel_make_bools(struct selinux_fs_info *fsi);
345 static int sel_make_classes(struct selinux_fs_info *fsi);
346 static int sel_make_policycap(struct selinux_fs_info *fsi);
347 
348 /* declaration for sel_make_class_dirs */
349 static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
350 			unsigned long *ino);
351 
352 static ssize_t sel_read_mls(struct file *filp, char __user *buf,
353 				size_t count, loff_t *ppos)
354 {
355 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
356 	char tmpbuf[TMPBUFLEN];
357 	ssize_t length;
358 
359 	length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
360 			   security_mls_enabled(fsi->state));
361 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
362 }
363 
364 static const struct file_operations sel_mls_ops = {
365 	.read		= sel_read_mls,
366 	.llseek		= generic_file_llseek,
367 };
368 
369 struct policy_load_memory {
370 	size_t len;
371 	void *data;
372 };
373 
374 static int sel_open_policy(struct inode *inode, struct file *filp)
375 {
376 	struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
377 	struct selinux_state *state = fsi->state;
378 	struct policy_load_memory *plm = NULL;
379 	int rc;
380 
381 	BUG_ON(filp->private_data);
382 
383 	mutex_lock(&fsi->mutex);
384 
385 	rc = avc_has_perm(&selinux_state,
386 			  current_sid(), SECINITSID_SECURITY,
387 			  SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
388 	if (rc)
389 		goto err;
390 
391 	rc = -EBUSY;
392 	if (fsi->policy_opened)
393 		goto err;
394 
395 	rc = -ENOMEM;
396 	plm = kzalloc(sizeof(*plm), GFP_KERNEL);
397 	if (!plm)
398 		goto err;
399 
400 	if (i_size_read(inode) != security_policydb_len(state)) {
401 		inode_lock(inode);
402 		i_size_write(inode, security_policydb_len(state));
403 		inode_unlock(inode);
404 	}
405 
406 	rc = security_read_policy(state, &plm->data, &plm->len);
407 	if (rc)
408 		goto err;
409 
410 	fsi->policy_opened = 1;
411 
412 	filp->private_data = plm;
413 
414 	mutex_unlock(&fsi->mutex);
415 
416 	return 0;
417 err:
418 	mutex_unlock(&fsi->mutex);
419 
420 	if (plm)
421 		vfree(plm->data);
422 	kfree(plm);
423 	return rc;
424 }
425 
426 static int sel_release_policy(struct inode *inode, struct file *filp)
427 {
428 	struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
429 	struct policy_load_memory *plm = filp->private_data;
430 
431 	BUG_ON(!plm);
432 
433 	fsi->policy_opened = 0;
434 
435 	vfree(plm->data);
436 	kfree(plm);
437 
438 	return 0;
439 }
440 
441 static ssize_t sel_read_policy(struct file *filp, char __user *buf,
442 			       size_t count, loff_t *ppos)
443 {
444 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
445 	struct policy_load_memory *plm = filp->private_data;
446 	int ret;
447 
448 	mutex_lock(&fsi->mutex);
449 
450 	ret = avc_has_perm(&selinux_state,
451 			   current_sid(), SECINITSID_SECURITY,
452 			  SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
453 	if (ret)
454 		goto out;
455 
456 	ret = simple_read_from_buffer(buf, count, ppos, plm->data, plm->len);
457 out:
458 	mutex_unlock(&fsi->mutex);
459 	return ret;
460 }
461 
462 static vm_fault_t sel_mmap_policy_fault(struct vm_fault *vmf)
463 {
464 	struct policy_load_memory *plm = vmf->vma->vm_file->private_data;
465 	unsigned long offset;
466 	struct page *page;
467 
468 	if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE))
469 		return VM_FAULT_SIGBUS;
470 
471 	offset = vmf->pgoff << PAGE_SHIFT;
472 	if (offset >= roundup(plm->len, PAGE_SIZE))
473 		return VM_FAULT_SIGBUS;
474 
475 	page = vmalloc_to_page(plm->data + offset);
476 	get_page(page);
477 
478 	vmf->page = page;
479 
480 	return 0;
481 }
482 
483 static const struct vm_operations_struct sel_mmap_policy_ops = {
484 	.fault = sel_mmap_policy_fault,
485 	.page_mkwrite = sel_mmap_policy_fault,
486 };
487 
488 static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma)
489 {
490 	if (vma->vm_flags & VM_SHARED) {
491 		/* do not allow mprotect to make mapping writable */
492 		vma->vm_flags &= ~VM_MAYWRITE;
493 
494 		if (vma->vm_flags & VM_WRITE)
495 			return -EACCES;
496 	}
497 
498 	vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
499 	vma->vm_ops = &sel_mmap_policy_ops;
500 
501 	return 0;
502 }
503 
504 static const struct file_operations sel_policy_ops = {
505 	.open		= sel_open_policy,
506 	.read		= sel_read_policy,
507 	.mmap		= sel_mmap_policy,
508 	.release	= sel_release_policy,
509 	.llseek		= generic_file_llseek,
510 };
511 
512 static int sel_make_policy_nodes(struct selinux_fs_info *fsi)
513 {
514 	int ret;
515 
516 	ret = sel_make_bools(fsi);
517 	if (ret) {
518 		pr_err("SELinux: failed to load policy booleans\n");
519 		return ret;
520 	}
521 
522 	ret = sel_make_classes(fsi);
523 	if (ret) {
524 		pr_err("SELinux: failed to load policy classes\n");
525 		return ret;
526 	}
527 
528 	ret = sel_make_policycap(fsi);
529 	if (ret) {
530 		pr_err("SELinux: failed to load policy capabilities\n");
531 		return ret;
532 	}
533 
534 	return 0;
535 }
536 
537 static ssize_t sel_write_load(struct file *file, const char __user *buf,
538 			      size_t count, loff_t *ppos)
539 
540 {
541 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
542 	ssize_t length;
543 	void *data = NULL;
544 
545 	mutex_lock(&fsi->mutex);
546 
547 	length = avc_has_perm(&selinux_state,
548 			      current_sid(), SECINITSID_SECURITY,
549 			      SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL);
550 	if (length)
551 		goto out;
552 
553 	/* No partial writes. */
554 	length = -EINVAL;
555 	if (*ppos != 0)
556 		goto out;
557 
558 	length = -EFBIG;
559 	if (count > 64 * 1024 * 1024)
560 		goto out;
561 
562 	length = -ENOMEM;
563 	data = vmalloc(count);
564 	if (!data)
565 		goto out;
566 
567 	length = -EFAULT;
568 	if (copy_from_user(data, buf, count) != 0)
569 		goto out;
570 
571 	length = security_load_policy(fsi->state, data, count);
572 	if (length) {
573 		pr_warn_ratelimited("SELinux: failed to load policy\n");
574 		goto out;
575 	}
576 
577 	length = sel_make_policy_nodes(fsi);
578 	if (length)
579 		goto out1;
580 
581 	length = count;
582 
583 out1:
584 	audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
585 		"auid=%u ses=%u lsm=selinux res=1",
586 		from_kuid(&init_user_ns, audit_get_loginuid(current)),
587 		audit_get_sessionid(current));
588 out:
589 	mutex_unlock(&fsi->mutex);
590 	vfree(data);
591 	return length;
592 }
593 
594 static const struct file_operations sel_load_ops = {
595 	.write		= sel_write_load,
596 	.llseek		= generic_file_llseek,
597 };
598 
599 static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
600 {
601 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
602 	struct selinux_state *state = fsi->state;
603 	char *canon = NULL;
604 	u32 sid, len;
605 	ssize_t length;
606 
607 	length = avc_has_perm(&selinux_state,
608 			      current_sid(), SECINITSID_SECURITY,
609 			      SECCLASS_SECURITY, SECURITY__CHECK_CONTEXT, NULL);
610 	if (length)
611 		goto out;
612 
613 	length = security_context_to_sid(state, buf, size, &sid, GFP_KERNEL);
614 	if (length)
615 		goto out;
616 
617 	length = security_sid_to_context(state, sid, &canon, &len);
618 	if (length)
619 		goto out;
620 
621 	length = -ERANGE;
622 	if (len > SIMPLE_TRANSACTION_LIMIT) {
623 		printk(KERN_ERR "SELinux: %s:  context size (%u) exceeds "
624 			"payload max\n", __func__, len);
625 		goto out;
626 	}
627 
628 	memcpy(buf, canon, len);
629 	length = len;
630 out:
631 	kfree(canon);
632 	return length;
633 }
634 
635 static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
636 				     size_t count, loff_t *ppos)
637 {
638 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
639 	char tmpbuf[TMPBUFLEN];
640 	ssize_t length;
641 
642 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u", fsi->state->checkreqprot);
643 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
644 }
645 
646 static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
647 				      size_t count, loff_t *ppos)
648 {
649 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
650 	char *page;
651 	ssize_t length;
652 	unsigned int new_value;
653 
654 	length = avc_has_perm(&selinux_state,
655 			      current_sid(), SECINITSID_SECURITY,
656 			      SECCLASS_SECURITY, SECURITY__SETCHECKREQPROT,
657 			      NULL);
658 	if (length)
659 		return length;
660 
661 	if (count >= PAGE_SIZE)
662 		return -ENOMEM;
663 
664 	/* No partial writes. */
665 	if (*ppos != 0)
666 		return -EINVAL;
667 
668 	page = memdup_user_nul(buf, count);
669 	if (IS_ERR(page))
670 		return PTR_ERR(page);
671 
672 	length = -EINVAL;
673 	if (sscanf(page, "%u", &new_value) != 1)
674 		goto out;
675 
676 	fsi->state->checkreqprot = new_value ? 1 : 0;
677 	length = count;
678 out:
679 	kfree(page);
680 	return length;
681 }
682 static const struct file_operations sel_checkreqprot_ops = {
683 	.read		= sel_read_checkreqprot,
684 	.write		= sel_write_checkreqprot,
685 	.llseek		= generic_file_llseek,
686 };
687 
688 static ssize_t sel_write_validatetrans(struct file *file,
689 					const char __user *buf,
690 					size_t count, loff_t *ppos)
691 {
692 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
693 	struct selinux_state *state = fsi->state;
694 	char *oldcon = NULL, *newcon = NULL, *taskcon = NULL;
695 	char *req = NULL;
696 	u32 osid, nsid, tsid;
697 	u16 tclass;
698 	int rc;
699 
700 	rc = avc_has_perm(&selinux_state,
701 			  current_sid(), SECINITSID_SECURITY,
702 			  SECCLASS_SECURITY, SECURITY__VALIDATE_TRANS, NULL);
703 	if (rc)
704 		goto out;
705 
706 	rc = -ENOMEM;
707 	if (count >= PAGE_SIZE)
708 		goto out;
709 
710 	/* No partial writes. */
711 	rc = -EINVAL;
712 	if (*ppos != 0)
713 		goto out;
714 
715 	req = memdup_user_nul(buf, count);
716 	if (IS_ERR(req)) {
717 		rc = PTR_ERR(req);
718 		req = NULL;
719 		goto out;
720 	}
721 
722 	rc = -ENOMEM;
723 	oldcon = kzalloc(count + 1, GFP_KERNEL);
724 	if (!oldcon)
725 		goto out;
726 
727 	newcon = kzalloc(count + 1, GFP_KERNEL);
728 	if (!newcon)
729 		goto out;
730 
731 	taskcon = kzalloc(count + 1, GFP_KERNEL);
732 	if (!taskcon)
733 		goto out;
734 
735 	rc = -EINVAL;
736 	if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4)
737 		goto out;
738 
739 	rc = security_context_str_to_sid(state, oldcon, &osid, GFP_KERNEL);
740 	if (rc)
741 		goto out;
742 
743 	rc = security_context_str_to_sid(state, newcon, &nsid, GFP_KERNEL);
744 	if (rc)
745 		goto out;
746 
747 	rc = security_context_str_to_sid(state, taskcon, &tsid, GFP_KERNEL);
748 	if (rc)
749 		goto out;
750 
751 	rc = security_validate_transition_user(state, osid, nsid, tsid, tclass);
752 	if (!rc)
753 		rc = count;
754 out:
755 	kfree(req);
756 	kfree(oldcon);
757 	kfree(newcon);
758 	kfree(taskcon);
759 	return rc;
760 }
761 
762 static const struct file_operations sel_transition_ops = {
763 	.write		= sel_write_validatetrans,
764 	.llseek		= generic_file_llseek,
765 };
766 
767 /*
768  * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
769  */
770 static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
771 static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
772 static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
773 static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
774 static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
775 
776 static ssize_t (*write_op[])(struct file *, char *, size_t) = {
777 	[SEL_ACCESS] = sel_write_access,
778 	[SEL_CREATE] = sel_write_create,
779 	[SEL_RELABEL] = sel_write_relabel,
780 	[SEL_USER] = sel_write_user,
781 	[SEL_MEMBER] = sel_write_member,
782 	[SEL_CONTEXT] = sel_write_context,
783 };
784 
785 static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
786 {
787 	ino_t ino = file_inode(file)->i_ino;
788 	char *data;
789 	ssize_t rv;
790 
791 	if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
792 		return -EINVAL;
793 
794 	data = simple_transaction_get(file, buf, size);
795 	if (IS_ERR(data))
796 		return PTR_ERR(data);
797 
798 	rv = write_op[ino](file, data, size);
799 	if (rv > 0) {
800 		simple_transaction_set(file, rv);
801 		rv = size;
802 	}
803 	return rv;
804 }
805 
806 static const struct file_operations transaction_ops = {
807 	.write		= selinux_transaction_write,
808 	.read		= simple_transaction_read,
809 	.release	= simple_transaction_release,
810 	.llseek		= generic_file_llseek,
811 };
812 
813 /*
814  * payload - write methods
815  * If the method has a response, the response should be put in buf,
816  * and the length returned.  Otherwise return 0 or and -error.
817  */
818 
819 static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
820 {
821 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
822 	struct selinux_state *state = fsi->state;
823 	char *scon = NULL, *tcon = NULL;
824 	u32 ssid, tsid;
825 	u16 tclass;
826 	struct av_decision avd;
827 	ssize_t length;
828 
829 	length = avc_has_perm(&selinux_state,
830 			      current_sid(), SECINITSID_SECURITY,
831 			      SECCLASS_SECURITY, SECURITY__COMPUTE_AV, NULL);
832 	if (length)
833 		goto out;
834 
835 	length = -ENOMEM;
836 	scon = kzalloc(size + 1, GFP_KERNEL);
837 	if (!scon)
838 		goto out;
839 
840 	length = -ENOMEM;
841 	tcon = kzalloc(size + 1, GFP_KERNEL);
842 	if (!tcon)
843 		goto out;
844 
845 	length = -EINVAL;
846 	if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
847 		goto out;
848 
849 	length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
850 	if (length)
851 		goto out;
852 
853 	length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
854 	if (length)
855 		goto out;
856 
857 	security_compute_av_user(state, ssid, tsid, tclass, &avd);
858 
859 	length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
860 			  "%x %x %x %x %u %x",
861 			  avd.allowed, 0xffffffff,
862 			  avd.auditallow, avd.auditdeny,
863 			  avd.seqno, avd.flags);
864 out:
865 	kfree(tcon);
866 	kfree(scon);
867 	return length;
868 }
869 
870 static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
871 {
872 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
873 	struct selinux_state *state = fsi->state;
874 	char *scon = NULL, *tcon = NULL;
875 	char *namebuf = NULL, *objname = NULL;
876 	u32 ssid, tsid, newsid;
877 	u16 tclass;
878 	ssize_t length;
879 	char *newcon = NULL;
880 	u32 len;
881 	int nargs;
882 
883 	length = avc_has_perm(&selinux_state,
884 			      current_sid(), SECINITSID_SECURITY,
885 			      SECCLASS_SECURITY, SECURITY__COMPUTE_CREATE,
886 			      NULL);
887 	if (length)
888 		goto out;
889 
890 	length = -ENOMEM;
891 	scon = kzalloc(size + 1, GFP_KERNEL);
892 	if (!scon)
893 		goto out;
894 
895 	length = -ENOMEM;
896 	tcon = kzalloc(size + 1, GFP_KERNEL);
897 	if (!tcon)
898 		goto out;
899 
900 	length = -ENOMEM;
901 	namebuf = kzalloc(size + 1, GFP_KERNEL);
902 	if (!namebuf)
903 		goto out;
904 
905 	length = -EINVAL;
906 	nargs = sscanf(buf, "%s %s %hu %s", scon, tcon, &tclass, namebuf);
907 	if (nargs < 3 || nargs > 4)
908 		goto out;
909 	if (nargs == 4) {
910 		/*
911 		 * If and when the name of new object to be queried contains
912 		 * either whitespace or multibyte characters, they shall be
913 		 * encoded based on the percentage-encoding rule.
914 		 * If not encoded, the sscanf logic picks up only left-half
915 		 * of the supplied name; splitted by a whitespace unexpectedly.
916 		 */
917 		char   *r, *w;
918 		int     c1, c2;
919 
920 		r = w = namebuf;
921 		do {
922 			c1 = *r++;
923 			if (c1 == '+')
924 				c1 = ' ';
925 			else if (c1 == '%') {
926 				c1 = hex_to_bin(*r++);
927 				if (c1 < 0)
928 					goto out;
929 				c2 = hex_to_bin(*r++);
930 				if (c2 < 0)
931 					goto out;
932 				c1 = (c1 << 4) | c2;
933 			}
934 			*w++ = c1;
935 		} while (c1 != '\0');
936 
937 		objname = namebuf;
938 	}
939 
940 	length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
941 	if (length)
942 		goto out;
943 
944 	length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
945 	if (length)
946 		goto out;
947 
948 	length = security_transition_sid_user(state, ssid, tsid, tclass,
949 					      objname, &newsid);
950 	if (length)
951 		goto out;
952 
953 	length = security_sid_to_context(state, newsid, &newcon, &len);
954 	if (length)
955 		goto out;
956 
957 	length = -ERANGE;
958 	if (len > SIMPLE_TRANSACTION_LIMIT) {
959 		printk(KERN_ERR "SELinux: %s:  context size (%u) exceeds "
960 			"payload max\n", __func__, len);
961 		goto out;
962 	}
963 
964 	memcpy(buf, newcon, len);
965 	length = len;
966 out:
967 	kfree(newcon);
968 	kfree(namebuf);
969 	kfree(tcon);
970 	kfree(scon);
971 	return length;
972 }
973 
974 static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
975 {
976 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
977 	struct selinux_state *state = fsi->state;
978 	char *scon = NULL, *tcon = NULL;
979 	u32 ssid, tsid, newsid;
980 	u16 tclass;
981 	ssize_t length;
982 	char *newcon = NULL;
983 	u32 len;
984 
985 	length = avc_has_perm(&selinux_state,
986 			      current_sid(), SECINITSID_SECURITY,
987 			      SECCLASS_SECURITY, SECURITY__COMPUTE_RELABEL,
988 			      NULL);
989 	if (length)
990 		goto out;
991 
992 	length = -ENOMEM;
993 	scon = kzalloc(size + 1, GFP_KERNEL);
994 	if (!scon)
995 		goto out;
996 
997 	length = -ENOMEM;
998 	tcon = kzalloc(size + 1, GFP_KERNEL);
999 	if (!tcon)
1000 		goto out;
1001 
1002 	length = -EINVAL;
1003 	if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
1004 		goto out;
1005 
1006 	length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
1007 	if (length)
1008 		goto out;
1009 
1010 	length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
1011 	if (length)
1012 		goto out;
1013 
1014 	length = security_change_sid(state, ssid, tsid, tclass, &newsid);
1015 	if (length)
1016 		goto out;
1017 
1018 	length = security_sid_to_context(state, newsid, &newcon, &len);
1019 	if (length)
1020 		goto out;
1021 
1022 	length = -ERANGE;
1023 	if (len > SIMPLE_TRANSACTION_LIMIT)
1024 		goto out;
1025 
1026 	memcpy(buf, newcon, len);
1027 	length = len;
1028 out:
1029 	kfree(newcon);
1030 	kfree(tcon);
1031 	kfree(scon);
1032 	return length;
1033 }
1034 
1035 static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
1036 {
1037 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1038 	struct selinux_state *state = fsi->state;
1039 	char *con = NULL, *user = NULL, *ptr;
1040 	u32 sid, *sids = NULL;
1041 	ssize_t length;
1042 	char *newcon;
1043 	int i, rc;
1044 	u32 len, nsids;
1045 
1046 	length = avc_has_perm(&selinux_state,
1047 			      current_sid(), SECINITSID_SECURITY,
1048 			      SECCLASS_SECURITY, SECURITY__COMPUTE_USER,
1049 			      NULL);
1050 	if (length)
1051 		goto out;
1052 
1053 	length = -ENOMEM;
1054 	con = kzalloc(size + 1, GFP_KERNEL);
1055 	if (!con)
1056 		goto out;
1057 
1058 	length = -ENOMEM;
1059 	user = kzalloc(size + 1, GFP_KERNEL);
1060 	if (!user)
1061 		goto out;
1062 
1063 	length = -EINVAL;
1064 	if (sscanf(buf, "%s %s", con, user) != 2)
1065 		goto out;
1066 
1067 	length = security_context_str_to_sid(state, con, &sid, GFP_KERNEL);
1068 	if (length)
1069 		goto out;
1070 
1071 	length = security_get_user_sids(state, sid, user, &sids, &nsids);
1072 	if (length)
1073 		goto out;
1074 
1075 	length = sprintf(buf, "%u", nsids) + 1;
1076 	ptr = buf + length;
1077 	for (i = 0; i < nsids; i++) {
1078 		rc = security_sid_to_context(state, sids[i], &newcon, &len);
1079 		if (rc) {
1080 			length = rc;
1081 			goto out;
1082 		}
1083 		if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
1084 			kfree(newcon);
1085 			length = -ERANGE;
1086 			goto out;
1087 		}
1088 		memcpy(ptr, newcon, len);
1089 		kfree(newcon);
1090 		ptr += len;
1091 		length += len;
1092 	}
1093 out:
1094 	kfree(sids);
1095 	kfree(user);
1096 	kfree(con);
1097 	return length;
1098 }
1099 
1100 static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
1101 {
1102 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1103 	struct selinux_state *state = fsi->state;
1104 	char *scon = NULL, *tcon = NULL;
1105 	u32 ssid, tsid, newsid;
1106 	u16 tclass;
1107 	ssize_t length;
1108 	char *newcon = NULL;
1109 	u32 len;
1110 
1111 	length = avc_has_perm(&selinux_state,
1112 			      current_sid(), SECINITSID_SECURITY,
1113 			      SECCLASS_SECURITY, SECURITY__COMPUTE_MEMBER,
1114 			      NULL);
1115 	if (length)
1116 		goto out;
1117 
1118 	length = -ENOMEM;
1119 	scon = kzalloc(size + 1, GFP_KERNEL);
1120 	if (!scon)
1121 		goto out;
1122 
1123 	length = -ENOMEM;
1124 	tcon = kzalloc(size + 1, GFP_KERNEL);
1125 	if (!tcon)
1126 		goto out;
1127 
1128 	length = -EINVAL;
1129 	if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
1130 		goto out;
1131 
1132 	length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
1133 	if (length)
1134 		goto out;
1135 
1136 	length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
1137 	if (length)
1138 		goto out;
1139 
1140 	length = security_member_sid(state, ssid, tsid, tclass, &newsid);
1141 	if (length)
1142 		goto out;
1143 
1144 	length = security_sid_to_context(state, newsid, &newcon, &len);
1145 	if (length)
1146 		goto out;
1147 
1148 	length = -ERANGE;
1149 	if (len > SIMPLE_TRANSACTION_LIMIT) {
1150 		printk(KERN_ERR "SELinux: %s:  context size (%u) exceeds "
1151 			"payload max\n", __func__, len);
1152 		goto out;
1153 	}
1154 
1155 	memcpy(buf, newcon, len);
1156 	length = len;
1157 out:
1158 	kfree(newcon);
1159 	kfree(tcon);
1160 	kfree(scon);
1161 	return length;
1162 }
1163 
1164 static struct inode *sel_make_inode(struct super_block *sb, int mode)
1165 {
1166 	struct inode *ret = new_inode(sb);
1167 
1168 	if (ret) {
1169 		ret->i_mode = mode;
1170 		ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret);
1171 	}
1172 	return ret;
1173 }
1174 
1175 static ssize_t sel_read_bool(struct file *filep, char __user *buf,
1176 			     size_t count, loff_t *ppos)
1177 {
1178 	struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
1179 	char *page = NULL;
1180 	ssize_t length;
1181 	ssize_t ret;
1182 	int cur_enforcing;
1183 	unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
1184 	const char *name = filep->f_path.dentry->d_name.name;
1185 
1186 	mutex_lock(&fsi->mutex);
1187 
1188 	ret = -EINVAL;
1189 	if (index >= fsi->bool_num || strcmp(name,
1190 					     fsi->bool_pending_names[index]))
1191 		goto out;
1192 
1193 	ret = -ENOMEM;
1194 	page = (char *)get_zeroed_page(GFP_KERNEL);
1195 	if (!page)
1196 		goto out;
1197 
1198 	cur_enforcing = security_get_bool_value(fsi->state, index);
1199 	if (cur_enforcing < 0) {
1200 		ret = cur_enforcing;
1201 		goto out;
1202 	}
1203 	length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
1204 			  fsi->bool_pending_values[index]);
1205 	ret = simple_read_from_buffer(buf, count, ppos, page, length);
1206 out:
1207 	mutex_unlock(&fsi->mutex);
1208 	free_page((unsigned long)page);
1209 	return ret;
1210 }
1211 
1212 static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
1213 			      size_t count, loff_t *ppos)
1214 {
1215 	struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
1216 	char *page = NULL;
1217 	ssize_t length;
1218 	int new_value;
1219 	unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
1220 	const char *name = filep->f_path.dentry->d_name.name;
1221 
1222 	mutex_lock(&fsi->mutex);
1223 
1224 	length = avc_has_perm(&selinux_state,
1225 			      current_sid(), SECINITSID_SECURITY,
1226 			      SECCLASS_SECURITY, SECURITY__SETBOOL,
1227 			      NULL);
1228 	if (length)
1229 		goto out;
1230 
1231 	length = -EINVAL;
1232 	if (index >= fsi->bool_num || strcmp(name,
1233 					     fsi->bool_pending_names[index]))
1234 		goto out;
1235 
1236 	length = -ENOMEM;
1237 	if (count >= PAGE_SIZE)
1238 		goto out;
1239 
1240 	/* No partial writes. */
1241 	length = -EINVAL;
1242 	if (*ppos != 0)
1243 		goto out;
1244 
1245 	page = memdup_user_nul(buf, count);
1246 	if (IS_ERR(page)) {
1247 		length = PTR_ERR(page);
1248 		page = NULL;
1249 		goto out;
1250 	}
1251 
1252 	length = -EINVAL;
1253 	if (sscanf(page, "%d", &new_value) != 1)
1254 		goto out;
1255 
1256 	if (new_value)
1257 		new_value = 1;
1258 
1259 	fsi->bool_pending_values[index] = new_value;
1260 	length = count;
1261 
1262 out:
1263 	mutex_unlock(&fsi->mutex);
1264 	kfree(page);
1265 	return length;
1266 }
1267 
1268 static const struct file_operations sel_bool_ops = {
1269 	.read		= sel_read_bool,
1270 	.write		= sel_write_bool,
1271 	.llseek		= generic_file_llseek,
1272 };
1273 
1274 static ssize_t sel_commit_bools_write(struct file *filep,
1275 				      const char __user *buf,
1276 				      size_t count, loff_t *ppos)
1277 {
1278 	struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
1279 	char *page = NULL;
1280 	ssize_t length;
1281 	int new_value;
1282 
1283 	mutex_lock(&fsi->mutex);
1284 
1285 	length = avc_has_perm(&selinux_state,
1286 			      current_sid(), SECINITSID_SECURITY,
1287 			      SECCLASS_SECURITY, SECURITY__SETBOOL,
1288 			      NULL);
1289 	if (length)
1290 		goto out;
1291 
1292 	length = -ENOMEM;
1293 	if (count >= PAGE_SIZE)
1294 		goto out;
1295 
1296 	/* No partial writes. */
1297 	length = -EINVAL;
1298 	if (*ppos != 0)
1299 		goto out;
1300 
1301 	page = memdup_user_nul(buf, count);
1302 	if (IS_ERR(page)) {
1303 		length = PTR_ERR(page);
1304 		page = NULL;
1305 		goto out;
1306 	}
1307 
1308 	length = -EINVAL;
1309 	if (sscanf(page, "%d", &new_value) != 1)
1310 		goto out;
1311 
1312 	length = 0;
1313 	if (new_value && fsi->bool_pending_values)
1314 		length = security_set_bools(fsi->state, fsi->bool_num,
1315 					    fsi->bool_pending_values);
1316 
1317 	if (!length)
1318 		length = count;
1319 
1320 out:
1321 	mutex_unlock(&fsi->mutex);
1322 	kfree(page);
1323 	return length;
1324 }
1325 
1326 static const struct file_operations sel_commit_bools_ops = {
1327 	.write		= sel_commit_bools_write,
1328 	.llseek		= generic_file_llseek,
1329 };
1330 
1331 static void sel_remove_entries(struct dentry *de)
1332 {
1333 	d_genocide(de);
1334 	shrink_dcache_parent(de);
1335 }
1336 
1337 #define BOOL_DIR_NAME "booleans"
1338 
1339 static int sel_make_bools(struct selinux_fs_info *fsi)
1340 {
1341 	int i, ret;
1342 	ssize_t len;
1343 	struct dentry *dentry = NULL;
1344 	struct dentry *dir = fsi->bool_dir;
1345 	struct inode *inode = NULL;
1346 	struct inode_security_struct *isec;
1347 	char **names = NULL, *page;
1348 	int num;
1349 	int *values = NULL;
1350 	u32 sid;
1351 
1352 	/* remove any existing files */
1353 	for (i = 0; i < fsi->bool_num; i++)
1354 		kfree(fsi->bool_pending_names[i]);
1355 	kfree(fsi->bool_pending_names);
1356 	kfree(fsi->bool_pending_values);
1357 	fsi->bool_num = 0;
1358 	fsi->bool_pending_names = NULL;
1359 	fsi->bool_pending_values = NULL;
1360 
1361 	sel_remove_entries(dir);
1362 
1363 	ret = -ENOMEM;
1364 	page = (char *)get_zeroed_page(GFP_KERNEL);
1365 	if (!page)
1366 		goto out;
1367 
1368 	ret = security_get_bools(fsi->state, &num, &names, &values);
1369 	if (ret)
1370 		goto out;
1371 
1372 	for (i = 0; i < num; i++) {
1373 		ret = -ENOMEM;
1374 		dentry = d_alloc_name(dir, names[i]);
1375 		if (!dentry)
1376 			goto out;
1377 
1378 		ret = -ENOMEM;
1379 		inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1380 		if (!inode)
1381 			goto out;
1382 
1383 		ret = -ENAMETOOLONG;
1384 		len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
1385 		if (len >= PAGE_SIZE)
1386 			goto out;
1387 
1388 		isec = (struct inode_security_struct *)inode->i_security;
1389 		ret = security_genfs_sid(fsi->state, "selinuxfs", page,
1390 					 SECCLASS_FILE, &sid);
1391 		if (ret) {
1392 			pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n",
1393 					   page);
1394 			sid = SECINITSID_SECURITY;
1395 		}
1396 
1397 		isec->sid = sid;
1398 		isec->initialized = LABEL_INITIALIZED;
1399 		inode->i_fop = &sel_bool_ops;
1400 		inode->i_ino = i|SEL_BOOL_INO_OFFSET;
1401 		d_add(dentry, inode);
1402 	}
1403 	fsi->bool_num = num;
1404 	fsi->bool_pending_names = names;
1405 	fsi->bool_pending_values = values;
1406 
1407 	free_page((unsigned long)page);
1408 	return 0;
1409 out:
1410 	free_page((unsigned long)page);
1411 
1412 	if (names) {
1413 		for (i = 0; i < num; i++)
1414 			kfree(names[i]);
1415 		kfree(names);
1416 	}
1417 	kfree(values);
1418 	sel_remove_entries(dir);
1419 
1420 	return ret;
1421 }
1422 
1423 static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1424 					    size_t count, loff_t *ppos)
1425 {
1426 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
1427 	struct selinux_state *state = fsi->state;
1428 	char tmpbuf[TMPBUFLEN];
1429 	ssize_t length;
1430 
1431 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1432 			   avc_get_cache_threshold(state->avc));
1433 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1434 }
1435 
1436 static ssize_t sel_write_avc_cache_threshold(struct file *file,
1437 					     const char __user *buf,
1438 					     size_t count, loff_t *ppos)
1439 
1440 {
1441 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1442 	struct selinux_state *state = fsi->state;
1443 	char *page;
1444 	ssize_t ret;
1445 	unsigned int new_value;
1446 
1447 	ret = avc_has_perm(&selinux_state,
1448 			   current_sid(), SECINITSID_SECURITY,
1449 			   SECCLASS_SECURITY, SECURITY__SETSECPARAM,
1450 			   NULL);
1451 	if (ret)
1452 		return ret;
1453 
1454 	if (count >= PAGE_SIZE)
1455 		return -ENOMEM;
1456 
1457 	/* No partial writes. */
1458 	if (*ppos != 0)
1459 		return -EINVAL;
1460 
1461 	page = memdup_user_nul(buf, count);
1462 	if (IS_ERR(page))
1463 		return PTR_ERR(page);
1464 
1465 	ret = -EINVAL;
1466 	if (sscanf(page, "%u", &new_value) != 1)
1467 		goto out;
1468 
1469 	avc_set_cache_threshold(state->avc, new_value);
1470 
1471 	ret = count;
1472 out:
1473 	kfree(page);
1474 	return ret;
1475 }
1476 
1477 static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1478 				       size_t count, loff_t *ppos)
1479 {
1480 	struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
1481 	struct selinux_state *state = fsi->state;
1482 	char *page;
1483 	ssize_t length;
1484 
1485 	page = (char *)__get_free_page(GFP_KERNEL);
1486 	if (!page)
1487 		return -ENOMEM;
1488 
1489 	length = avc_get_hash_stats(state->avc, page);
1490 	if (length >= 0)
1491 		length = simple_read_from_buffer(buf, count, ppos, page, length);
1492 	free_page((unsigned long)page);
1493 
1494 	return length;
1495 }
1496 
1497 static const struct file_operations sel_avc_cache_threshold_ops = {
1498 	.read		= sel_read_avc_cache_threshold,
1499 	.write		= sel_write_avc_cache_threshold,
1500 	.llseek		= generic_file_llseek,
1501 };
1502 
1503 static const struct file_operations sel_avc_hash_stats_ops = {
1504 	.read		= sel_read_avc_hash_stats,
1505 	.llseek		= generic_file_llseek,
1506 };
1507 
1508 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1509 static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1510 {
1511 	int cpu;
1512 
1513 	for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
1514 		if (!cpu_possible(cpu))
1515 			continue;
1516 		*idx = cpu + 1;
1517 		return &per_cpu(avc_cache_stats, cpu);
1518 	}
1519 	return NULL;
1520 }
1521 
1522 static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1523 {
1524 	loff_t n = *pos - 1;
1525 
1526 	if (*pos == 0)
1527 		return SEQ_START_TOKEN;
1528 
1529 	return sel_avc_get_stat_idx(&n);
1530 }
1531 
1532 static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1533 {
1534 	return sel_avc_get_stat_idx(pos);
1535 }
1536 
1537 static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1538 {
1539 	struct avc_cache_stats *st = v;
1540 
1541 	if (v == SEQ_START_TOKEN) {
1542 		seq_puts(seq,
1543 			 "lookups hits misses allocations reclaims frees\n");
1544 	} else {
1545 		unsigned int lookups = st->lookups;
1546 		unsigned int misses = st->misses;
1547 		unsigned int hits = lookups - misses;
1548 		seq_printf(seq, "%u %u %u %u %u %u\n", lookups,
1549 			   hits, misses, st->allocations,
1550 			   st->reclaims, st->frees);
1551 	}
1552 	return 0;
1553 }
1554 
1555 static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1556 { }
1557 
1558 static const struct seq_operations sel_avc_cache_stats_seq_ops = {
1559 	.start		= sel_avc_stats_seq_start,
1560 	.next		= sel_avc_stats_seq_next,
1561 	.show		= sel_avc_stats_seq_show,
1562 	.stop		= sel_avc_stats_seq_stop,
1563 };
1564 
1565 static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1566 {
1567 	return seq_open(file, &sel_avc_cache_stats_seq_ops);
1568 }
1569 
1570 static const struct file_operations sel_avc_cache_stats_ops = {
1571 	.open		= sel_open_avc_cache_stats,
1572 	.read		= seq_read,
1573 	.llseek		= seq_lseek,
1574 	.release	= seq_release,
1575 };
1576 #endif
1577 
1578 static int sel_make_avc_files(struct dentry *dir)
1579 {
1580 	struct super_block *sb = dir->d_sb;
1581 	struct selinux_fs_info *fsi = sb->s_fs_info;
1582 	int i;
1583 	static const struct tree_descr files[] = {
1584 		{ "cache_threshold",
1585 		  &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1586 		{ "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1587 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1588 		{ "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1589 #endif
1590 	};
1591 
1592 	for (i = 0; i < ARRAY_SIZE(files); i++) {
1593 		struct inode *inode;
1594 		struct dentry *dentry;
1595 
1596 		dentry = d_alloc_name(dir, files[i].name);
1597 		if (!dentry)
1598 			return -ENOMEM;
1599 
1600 		inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1601 		if (!inode)
1602 			return -ENOMEM;
1603 
1604 		inode->i_fop = files[i].ops;
1605 		inode->i_ino = ++fsi->last_ino;
1606 		d_add(dentry, inode);
1607 	}
1608 
1609 	return 0;
1610 }
1611 
1612 static ssize_t sel_read_initcon(struct file *file, char __user *buf,
1613 				size_t count, loff_t *ppos)
1614 {
1615 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1616 	char *con;
1617 	u32 sid, len;
1618 	ssize_t ret;
1619 
1620 	sid = file_inode(file)->i_ino&SEL_INO_MASK;
1621 	ret = security_sid_to_context(fsi->state, sid, &con, &len);
1622 	if (ret)
1623 		return ret;
1624 
1625 	ret = simple_read_from_buffer(buf, count, ppos, con, len);
1626 	kfree(con);
1627 	return ret;
1628 }
1629 
1630 static const struct file_operations sel_initcon_ops = {
1631 	.read		= sel_read_initcon,
1632 	.llseek		= generic_file_llseek,
1633 };
1634 
1635 static int sel_make_initcon_files(struct dentry *dir)
1636 {
1637 	int i;
1638 
1639 	for (i = 1; i <= SECINITSID_NUM; i++) {
1640 		struct inode *inode;
1641 		struct dentry *dentry;
1642 		dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
1643 		if (!dentry)
1644 			return -ENOMEM;
1645 
1646 		inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1647 		if (!inode)
1648 			return -ENOMEM;
1649 
1650 		inode->i_fop = &sel_initcon_ops;
1651 		inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1652 		d_add(dentry, inode);
1653 	}
1654 
1655 	return 0;
1656 }
1657 
1658 static inline unsigned long sel_class_to_ino(u16 class)
1659 {
1660 	return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1661 }
1662 
1663 static inline u16 sel_ino_to_class(unsigned long ino)
1664 {
1665 	return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1);
1666 }
1667 
1668 static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1669 {
1670 	return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1671 }
1672 
1673 static inline u32 sel_ino_to_perm(unsigned long ino)
1674 {
1675 	return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1676 }
1677 
1678 static ssize_t sel_read_class(struct file *file, char __user *buf,
1679 				size_t count, loff_t *ppos)
1680 {
1681 	unsigned long ino = file_inode(file)->i_ino;
1682 	char res[TMPBUFLEN];
1683 	ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_class(ino));
1684 	return simple_read_from_buffer(buf, count, ppos, res, len);
1685 }
1686 
1687 static const struct file_operations sel_class_ops = {
1688 	.read		= sel_read_class,
1689 	.llseek		= generic_file_llseek,
1690 };
1691 
1692 static ssize_t sel_read_perm(struct file *file, char __user *buf,
1693 				size_t count, loff_t *ppos)
1694 {
1695 	unsigned long ino = file_inode(file)->i_ino;
1696 	char res[TMPBUFLEN];
1697 	ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino));
1698 	return simple_read_from_buffer(buf, count, ppos, res, len);
1699 }
1700 
1701 static const struct file_operations sel_perm_ops = {
1702 	.read		= sel_read_perm,
1703 	.llseek		= generic_file_llseek,
1704 };
1705 
1706 static ssize_t sel_read_policycap(struct file *file, char __user *buf,
1707 				  size_t count, loff_t *ppos)
1708 {
1709 	struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1710 	int value;
1711 	char tmpbuf[TMPBUFLEN];
1712 	ssize_t length;
1713 	unsigned long i_ino = file_inode(file)->i_ino;
1714 
1715 	value = security_policycap_supported(fsi->state, i_ino & SEL_INO_MASK);
1716 	length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
1717 
1718 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1719 }
1720 
1721 static const struct file_operations sel_policycap_ops = {
1722 	.read		= sel_read_policycap,
1723 	.llseek		= generic_file_llseek,
1724 };
1725 
1726 static int sel_make_perm_files(char *objclass, int classvalue,
1727 				struct dentry *dir)
1728 {
1729 	struct selinux_fs_info *fsi = dir->d_sb->s_fs_info;
1730 	int i, rc, nperms;
1731 	char **perms;
1732 
1733 	rc = security_get_permissions(fsi->state, objclass, &perms, &nperms);
1734 	if (rc)
1735 		return rc;
1736 
1737 	for (i = 0; i < nperms; i++) {
1738 		struct inode *inode;
1739 		struct dentry *dentry;
1740 
1741 		rc = -ENOMEM;
1742 		dentry = d_alloc_name(dir, perms[i]);
1743 		if (!dentry)
1744 			goto out;
1745 
1746 		rc = -ENOMEM;
1747 		inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1748 		if (!inode)
1749 			goto out;
1750 
1751 		inode->i_fop = &sel_perm_ops;
1752 		/* i+1 since perm values are 1-indexed */
1753 		inode->i_ino = sel_perm_to_ino(classvalue, i + 1);
1754 		d_add(dentry, inode);
1755 	}
1756 	rc = 0;
1757 out:
1758 	for (i = 0; i < nperms; i++)
1759 		kfree(perms[i]);
1760 	kfree(perms);
1761 	return rc;
1762 }
1763 
1764 static int sel_make_class_dir_entries(char *classname, int index,
1765 					struct dentry *dir)
1766 {
1767 	struct super_block *sb = dir->d_sb;
1768 	struct selinux_fs_info *fsi = sb->s_fs_info;
1769 	struct dentry *dentry = NULL;
1770 	struct inode *inode = NULL;
1771 	int rc;
1772 
1773 	dentry = d_alloc_name(dir, "index");
1774 	if (!dentry)
1775 		return -ENOMEM;
1776 
1777 	inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1778 	if (!inode)
1779 		return -ENOMEM;
1780 
1781 	inode->i_fop = &sel_class_ops;
1782 	inode->i_ino = sel_class_to_ino(index);
1783 	d_add(dentry, inode);
1784 
1785 	dentry = sel_make_dir(dir, "perms", &fsi->last_class_ino);
1786 	if (IS_ERR(dentry))
1787 		return PTR_ERR(dentry);
1788 
1789 	rc = sel_make_perm_files(classname, index, dentry);
1790 
1791 	return rc;
1792 }
1793 
1794 static int sel_make_classes(struct selinux_fs_info *fsi)
1795 {
1796 
1797 	int rc, nclasses, i;
1798 	char **classes;
1799 
1800 	/* delete any existing entries */
1801 	sel_remove_entries(fsi->class_dir);
1802 
1803 	rc = security_get_classes(fsi->state, &classes, &nclasses);
1804 	if (rc)
1805 		return rc;
1806 
1807 	/* +2 since classes are 1-indexed */
1808 	fsi->last_class_ino = sel_class_to_ino(nclasses + 2);
1809 
1810 	for (i = 0; i < nclasses; i++) {
1811 		struct dentry *class_name_dir;
1812 
1813 		class_name_dir = sel_make_dir(fsi->class_dir, classes[i],
1814 					      &fsi->last_class_ino);
1815 		if (IS_ERR(class_name_dir)) {
1816 			rc = PTR_ERR(class_name_dir);
1817 			goto out;
1818 		}
1819 
1820 		/* i+1 since class values are 1-indexed */
1821 		rc = sel_make_class_dir_entries(classes[i], i + 1,
1822 				class_name_dir);
1823 		if (rc)
1824 			goto out;
1825 	}
1826 	rc = 0;
1827 out:
1828 	for (i = 0; i < nclasses; i++)
1829 		kfree(classes[i]);
1830 	kfree(classes);
1831 	return rc;
1832 }
1833 
1834 static int sel_make_policycap(struct selinux_fs_info *fsi)
1835 {
1836 	unsigned int iter;
1837 	struct dentry *dentry = NULL;
1838 	struct inode *inode = NULL;
1839 
1840 	sel_remove_entries(fsi->policycap_dir);
1841 
1842 	for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
1843 		if (iter < ARRAY_SIZE(selinux_policycap_names))
1844 			dentry = d_alloc_name(fsi->policycap_dir,
1845 					      selinux_policycap_names[iter]);
1846 		else
1847 			dentry = d_alloc_name(fsi->policycap_dir, "unknown");
1848 
1849 		if (dentry == NULL)
1850 			return -ENOMEM;
1851 
1852 		inode = sel_make_inode(fsi->sb, S_IFREG | 0444);
1853 		if (inode == NULL)
1854 			return -ENOMEM;
1855 
1856 		inode->i_fop = &sel_policycap_ops;
1857 		inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
1858 		d_add(dentry, inode);
1859 	}
1860 
1861 	return 0;
1862 }
1863 
1864 static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
1865 			unsigned long *ino)
1866 {
1867 	struct dentry *dentry = d_alloc_name(dir, name);
1868 	struct inode *inode;
1869 
1870 	if (!dentry)
1871 		return ERR_PTR(-ENOMEM);
1872 
1873 	inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1874 	if (!inode) {
1875 		dput(dentry);
1876 		return ERR_PTR(-ENOMEM);
1877 	}
1878 
1879 	inode->i_op = &simple_dir_inode_operations;
1880 	inode->i_fop = &simple_dir_operations;
1881 	inode->i_ino = ++(*ino);
1882 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
1883 	inc_nlink(inode);
1884 	d_add(dentry, inode);
1885 	/* bump link count on parent directory, too */
1886 	inc_nlink(d_inode(dir));
1887 
1888 	return dentry;
1889 }
1890 
1891 #define NULL_FILE_NAME "null"
1892 
1893 static int sel_fill_super(struct super_block *sb, void *data, int silent)
1894 {
1895 	struct selinux_fs_info *fsi;
1896 	int ret;
1897 	struct dentry *dentry;
1898 	struct inode *inode;
1899 	struct inode_security_struct *isec;
1900 
1901 	static const struct tree_descr selinux_files[] = {
1902 		[SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1903 		[SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
1904 		[SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
1905 		[SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1906 		[SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1907 		[SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1908 		[SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1909 		[SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1910 		[SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1911 		[SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1912 		[SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1913 		[SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1914 		[SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
1915 		[SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
1916 		[SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
1917 		[SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO},
1918 		[SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO},
1919 		[SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops,
1920 					S_IWUGO},
1921 		/* last one */ {""}
1922 	};
1923 
1924 	ret = selinux_fs_info_create(sb);
1925 	if (ret)
1926 		goto err;
1927 
1928 	ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1929 	if (ret)
1930 		goto err;
1931 
1932 	fsi = sb->s_fs_info;
1933 	fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino);
1934 	if (IS_ERR(fsi->bool_dir)) {
1935 		ret = PTR_ERR(fsi->bool_dir);
1936 		fsi->bool_dir = NULL;
1937 		goto err;
1938 	}
1939 
1940 	ret = -ENOMEM;
1941 	dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
1942 	if (!dentry)
1943 		goto err;
1944 
1945 	ret = -ENOMEM;
1946 	inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
1947 	if (!inode)
1948 		goto err;
1949 
1950 	inode->i_ino = ++fsi->last_ino;
1951 	isec = (struct inode_security_struct *)inode->i_security;
1952 	isec->sid = SECINITSID_DEVNULL;
1953 	isec->sclass = SECCLASS_CHR_FILE;
1954 	isec->initialized = LABEL_INITIALIZED;
1955 
1956 	init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1957 	d_add(dentry, inode);
1958 
1959 	dentry = sel_make_dir(sb->s_root, "avc", &fsi->last_ino);
1960 	if (IS_ERR(dentry)) {
1961 		ret = PTR_ERR(dentry);
1962 		goto err;
1963 	}
1964 
1965 	ret = sel_make_avc_files(dentry);
1966 	if (ret)
1967 		goto err;
1968 
1969 	dentry = sel_make_dir(sb->s_root, "initial_contexts", &fsi->last_ino);
1970 	if (IS_ERR(dentry)) {
1971 		ret = PTR_ERR(dentry);
1972 		goto err;
1973 	}
1974 
1975 	ret = sel_make_initcon_files(dentry);
1976 	if (ret)
1977 		goto err;
1978 
1979 	fsi->class_dir = sel_make_dir(sb->s_root, "class", &fsi->last_ino);
1980 	if (IS_ERR(fsi->class_dir)) {
1981 		ret = PTR_ERR(fsi->class_dir);
1982 		fsi->class_dir = NULL;
1983 		goto err;
1984 	}
1985 
1986 	fsi->policycap_dir = sel_make_dir(sb->s_root, "policy_capabilities",
1987 					  &fsi->last_ino);
1988 	if (IS_ERR(fsi->policycap_dir)) {
1989 		ret = PTR_ERR(fsi->policycap_dir);
1990 		fsi->policycap_dir = NULL;
1991 		goto err;
1992 	}
1993 
1994 	ret = sel_make_policy_nodes(fsi);
1995 	if (ret)
1996 		goto err;
1997 	return 0;
1998 err:
1999 	printk(KERN_ERR "SELinux: %s:  failed while creating inodes\n",
2000 		__func__);
2001 
2002 	selinux_fs_info_free(sb);
2003 
2004 	return ret;
2005 }
2006 
2007 static struct dentry *sel_mount(struct file_system_type *fs_type,
2008 		      int flags, const char *dev_name, void *data)
2009 {
2010 	return mount_single(fs_type, flags, data, sel_fill_super);
2011 }
2012 
2013 static void sel_kill_sb(struct super_block *sb)
2014 {
2015 	selinux_fs_info_free(sb);
2016 	kill_litter_super(sb);
2017 }
2018 
2019 static struct file_system_type sel_fs_type = {
2020 	.name		= "selinuxfs",
2021 	.mount		= sel_mount,
2022 	.kill_sb	= sel_kill_sb,
2023 };
2024 
2025 struct vfsmount *selinuxfs_mount;
2026 struct path selinux_null;
2027 
2028 static int __init init_sel_fs(void)
2029 {
2030 	struct qstr null_name = QSTR_INIT(NULL_FILE_NAME,
2031 					  sizeof(NULL_FILE_NAME)-1);
2032 	int err;
2033 
2034 	if (!selinux_enabled)
2035 		return 0;
2036 
2037 	err = sysfs_create_mount_point(fs_kobj, "selinux");
2038 	if (err)
2039 		return err;
2040 
2041 	err = register_filesystem(&sel_fs_type);
2042 	if (err) {
2043 		sysfs_remove_mount_point(fs_kobj, "selinux");
2044 		return err;
2045 	}
2046 
2047 	selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type);
2048 	if (IS_ERR(selinuxfs_mount)) {
2049 		printk(KERN_ERR "selinuxfs:  could not mount!\n");
2050 		err = PTR_ERR(selinuxfs_mount);
2051 		selinuxfs_mount = NULL;
2052 	}
2053 	selinux_null.dentry = d_hash_and_lookup(selinux_null.mnt->mnt_root,
2054 						&null_name);
2055 	if (IS_ERR(selinux_null.dentry)) {
2056 		pr_err("selinuxfs:  could not lookup null!\n");
2057 		err = PTR_ERR(selinux_null.dentry);
2058 		selinux_null.dentry = NULL;
2059 	}
2060 
2061 	return err;
2062 }
2063 
2064 __initcall(init_sel_fs);
2065 
2066 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
2067 void exit_sel_fs(void)
2068 {
2069 	sysfs_remove_mount_point(fs_kobj, "selinux");
2070 	dput(selinux_null.dentry);
2071 	kern_unmount(selinuxfs_mount);
2072 	unregister_filesystem(&sel_fs_type);
2073 }
2074 #endif
2075