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