xref: /linux/security/selinux/selinuxfs.c (revision f3d9478b2ce468c3115b02ecae7e975990697f15)
1 /* Updated: Karl MacMillan <kmacmillan@tresys.com>
2  *
3  * 	Added conditional policy language extensions
4  *
5  * Copyright (C) 2003 - 2004 Tresys Technology, LLC
6  * Copyright (C) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
7  *	This program is free software; you can redistribute it and/or modify
8  *  	it under the terms of the GNU General Public License as published by
9  *	the Free Software Foundation, version 2.
10  */
11 
12 #include <linux/config.h>
13 #include <linux/kernel.h>
14 #include <linux/pagemap.h>
15 #include <linux/slab.h>
16 #include <linux/vmalloc.h>
17 #include <linux/fs.h>
18 #include <linux/mutex.h>
19 #include <linux/init.h>
20 #include <linux/string.h>
21 #include <linux/security.h>
22 #include <linux/major.h>
23 #include <linux/seq_file.h>
24 #include <linux/percpu.h>
25 #include <linux/audit.h>
26 #include <asm/uaccess.h>
27 #include <asm/semaphore.h>
28 
29 /* selinuxfs pseudo filesystem for exporting the security policy API.
30    Based on the proc code and the fs/nfsd/nfsctl.c code. */
31 
32 #include "flask.h"
33 #include "avc.h"
34 #include "avc_ss.h"
35 #include "security.h"
36 #include "objsec.h"
37 #include "conditional.h"
38 
39 unsigned int selinux_checkreqprot = CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
40 
41 #ifdef CONFIG_SECURITY_SELINUX_ENABLE_SECMARK_DEFAULT
42 #define SELINUX_COMPAT_NET_VALUE 0
43 #else
44 #define SELINUX_COMPAT_NET_VALUE 1
45 #endif
46 
47 int selinux_compat_net = SELINUX_COMPAT_NET_VALUE;
48 
49 static int __init checkreqprot_setup(char *str)
50 {
51 	selinux_checkreqprot = simple_strtoul(str,NULL,0) ? 1 : 0;
52 	return 1;
53 }
54 __setup("checkreqprot=", checkreqprot_setup);
55 
56 static int __init selinux_compat_net_setup(char *str)
57 {
58 	selinux_compat_net = simple_strtoul(str,NULL,0) ? 1 : 0;
59 	return 1;
60 }
61 __setup("selinux_compat_net=", selinux_compat_net_setup);
62 
63 
64 static DEFINE_MUTEX(sel_mutex);
65 
66 /* global data for booleans */
67 static struct dentry *bool_dir = NULL;
68 static int bool_num = 0;
69 static int *bool_pending_values = NULL;
70 
71 extern void selnl_notify_setenforce(int val);
72 
73 /* Check whether a task is allowed to use a security operation. */
74 static int task_has_security(struct task_struct *tsk,
75 			     u32 perms)
76 {
77 	struct task_security_struct *tsec;
78 
79 	tsec = tsk->security;
80 	if (!tsec)
81 		return -EACCES;
82 
83 	return avc_has_perm(tsec->sid, SECINITSID_SECURITY,
84 			    SECCLASS_SECURITY, perms, NULL);
85 }
86 
87 enum sel_inos {
88 	SEL_ROOT_INO = 2,
89 	SEL_LOAD,	/* load policy */
90 	SEL_ENFORCE,	/* get or set enforcing status */
91 	SEL_CONTEXT,	/* validate context */
92 	SEL_ACCESS,	/* compute access decision */
93 	SEL_CREATE,	/* compute create labeling decision */
94 	SEL_RELABEL,	/* compute relabeling decision */
95 	SEL_USER,	/* compute reachable user contexts */
96 	SEL_POLICYVERS,	/* return policy version for this kernel */
97 	SEL_COMMIT_BOOLS, /* commit new boolean values */
98 	SEL_MLS,	/* return if MLS policy is enabled */
99 	SEL_DISABLE,	/* disable SELinux until next reboot */
100 	SEL_AVC,	/* AVC management directory */
101 	SEL_MEMBER,	/* compute polyinstantiation membership decision */
102 	SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
103 	SEL_COMPAT_NET,	/* whether to use old compat network packet controls */
104 };
105 
106 #define TMPBUFLEN	12
107 static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
108 				size_t count, loff_t *ppos)
109 {
110 	char tmpbuf[TMPBUFLEN];
111 	ssize_t length;
112 
113 	length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing);
114 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
115 }
116 
117 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
118 static ssize_t sel_write_enforce(struct file * file, const char __user * buf,
119 				 size_t count, loff_t *ppos)
120 
121 {
122 	char *page;
123 	ssize_t length;
124 	int new_value;
125 
126 	if (count >= PAGE_SIZE)
127 		return -ENOMEM;
128 	if (*ppos != 0) {
129 		/* No partial writes. */
130 		return -EINVAL;
131 	}
132 	page = (char*)get_zeroed_page(GFP_KERNEL);
133 	if (!page)
134 		return -ENOMEM;
135 	length = -EFAULT;
136 	if (copy_from_user(page, buf, count))
137 		goto out;
138 
139 	length = -EINVAL;
140 	if (sscanf(page, "%d", &new_value) != 1)
141 		goto out;
142 
143 	if (new_value != selinux_enforcing) {
144 		length = task_has_security(current, SECURITY__SETENFORCE);
145 		if (length)
146 			goto out;
147 		audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
148 			"enforcing=%d old_enforcing=%d auid=%u", new_value,
149 			selinux_enforcing,
150 			audit_get_loginuid(current->audit_context));
151 		selinux_enforcing = new_value;
152 		if (selinux_enforcing)
153 			avc_ss_reset(0);
154 		selnl_notify_setenforce(selinux_enforcing);
155 	}
156 	length = count;
157 out:
158 	free_page((unsigned long) page);
159 	return length;
160 }
161 #else
162 #define sel_write_enforce NULL
163 #endif
164 
165 static struct file_operations sel_enforce_ops = {
166 	.read		= sel_read_enforce,
167 	.write		= sel_write_enforce,
168 };
169 
170 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
171 static ssize_t sel_write_disable(struct file * file, const char __user * buf,
172 				 size_t count, loff_t *ppos)
173 
174 {
175 	char *page;
176 	ssize_t length;
177 	int new_value;
178 	extern int selinux_disable(void);
179 
180 	if (count >= PAGE_SIZE)
181 		return -ENOMEM;
182 	if (*ppos != 0) {
183 		/* No partial writes. */
184 		return -EINVAL;
185 	}
186 	page = (char*)get_zeroed_page(GFP_KERNEL);
187 	if (!page)
188 		return -ENOMEM;
189 	length = -EFAULT;
190 	if (copy_from_user(page, buf, count))
191 		goto out;
192 
193 	length = -EINVAL;
194 	if (sscanf(page, "%d", &new_value) != 1)
195 		goto out;
196 
197 	if (new_value) {
198 		length = selinux_disable();
199 		if (length < 0)
200 			goto out;
201 		audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
202 			"selinux=0 auid=%u",
203 			audit_get_loginuid(current->audit_context));
204 	}
205 
206 	length = count;
207 out:
208 	free_page((unsigned long) page);
209 	return length;
210 }
211 #else
212 #define sel_write_disable NULL
213 #endif
214 
215 static struct file_operations sel_disable_ops = {
216 	.write		= sel_write_disable,
217 };
218 
219 static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
220                                    size_t count, loff_t *ppos)
221 {
222 	char tmpbuf[TMPBUFLEN];
223 	ssize_t length;
224 
225 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
226 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
227 }
228 
229 static struct file_operations sel_policyvers_ops = {
230 	.read		= sel_read_policyvers,
231 };
232 
233 /* declaration for sel_write_load */
234 static int sel_make_bools(void);
235 
236 static ssize_t sel_read_mls(struct file *filp, char __user *buf,
237 				size_t count, loff_t *ppos)
238 {
239 	char tmpbuf[TMPBUFLEN];
240 	ssize_t length;
241 
242 	length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_mls_enabled);
243 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
244 }
245 
246 static struct file_operations sel_mls_ops = {
247 	.read		= sel_read_mls,
248 };
249 
250 static ssize_t sel_write_load(struct file * file, const char __user * buf,
251 			      size_t count, loff_t *ppos)
252 
253 {
254 	int ret;
255 	ssize_t length;
256 	void *data = NULL;
257 
258 	mutex_lock(&sel_mutex);
259 
260 	length = task_has_security(current, SECURITY__LOAD_POLICY);
261 	if (length)
262 		goto out;
263 
264 	if (*ppos != 0) {
265 		/* No partial writes. */
266 		length = -EINVAL;
267 		goto out;
268 	}
269 
270 	if ((count > 64 * 1024 * 1024)
271 	    || (data = vmalloc(count)) == NULL) {
272 		length = -ENOMEM;
273 		goto out;
274 	}
275 
276 	length = -EFAULT;
277 	if (copy_from_user(data, buf, count) != 0)
278 		goto out;
279 
280 	length = security_load_policy(data, count);
281 	if (length)
282 		goto out;
283 
284 	ret = sel_make_bools();
285 	if (ret)
286 		length = ret;
287 	else
288 		length = count;
289 	audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
290 		"policy loaded auid=%u",
291 		audit_get_loginuid(current->audit_context));
292 out:
293 	mutex_unlock(&sel_mutex);
294 	vfree(data);
295 	return length;
296 }
297 
298 static struct file_operations sel_load_ops = {
299 	.write		= sel_write_load,
300 };
301 
302 static ssize_t sel_write_context(struct file * file, char *buf, size_t size)
303 {
304 	char *canon;
305 	u32 sid, len;
306 	ssize_t length;
307 
308 	length = task_has_security(current, SECURITY__CHECK_CONTEXT);
309 	if (length)
310 		return length;
311 
312 	length = security_context_to_sid(buf, size, &sid);
313 	if (length < 0)
314 		return length;
315 
316 	length = security_sid_to_context(sid, &canon, &len);
317 	if (length < 0)
318 		return length;
319 
320 	if (len > SIMPLE_TRANSACTION_LIMIT) {
321 		printk(KERN_ERR "%s:  context size (%u) exceeds payload "
322 		       "max\n", __FUNCTION__, len);
323 		length = -ERANGE;
324 		goto out;
325 	}
326 
327 	memcpy(buf, canon, len);
328 	length = len;
329 out:
330 	kfree(canon);
331 	return length;
332 }
333 
334 static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
335 				     size_t count, loff_t *ppos)
336 {
337 	char tmpbuf[TMPBUFLEN];
338 	ssize_t length;
339 
340 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot);
341 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
342 }
343 
344 static ssize_t sel_write_checkreqprot(struct file * file, const char __user * buf,
345 				      size_t count, loff_t *ppos)
346 {
347 	char *page;
348 	ssize_t length;
349 	unsigned int new_value;
350 
351 	length = task_has_security(current, SECURITY__SETCHECKREQPROT);
352 	if (length)
353 		return length;
354 
355 	if (count >= PAGE_SIZE)
356 		return -ENOMEM;
357 	if (*ppos != 0) {
358 		/* No partial writes. */
359 		return -EINVAL;
360 	}
361 	page = (char*)get_zeroed_page(GFP_KERNEL);
362 	if (!page)
363 		return -ENOMEM;
364 	length = -EFAULT;
365 	if (copy_from_user(page, buf, count))
366 		goto out;
367 
368 	length = -EINVAL;
369 	if (sscanf(page, "%u", &new_value) != 1)
370 		goto out;
371 
372 	selinux_checkreqprot = new_value ? 1 : 0;
373 	length = count;
374 out:
375 	free_page((unsigned long) page);
376 	return length;
377 }
378 static struct file_operations sel_checkreqprot_ops = {
379 	.read		= sel_read_checkreqprot,
380 	.write		= sel_write_checkreqprot,
381 };
382 
383 static ssize_t sel_read_compat_net(struct file *filp, char __user *buf,
384 				   size_t count, loff_t *ppos)
385 {
386 	char tmpbuf[TMPBUFLEN];
387 	ssize_t length;
388 
389 	length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_compat_net);
390 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
391 }
392 
393 static ssize_t sel_write_compat_net(struct file * file, const char __user * buf,
394 				    size_t count, loff_t *ppos)
395 {
396 	char *page;
397 	ssize_t length;
398 	int new_value;
399 
400 	length = task_has_security(current, SECURITY__LOAD_POLICY);
401 	if (length)
402 		return length;
403 
404 	if (count >= PAGE_SIZE)
405 		return -ENOMEM;
406 	if (*ppos != 0) {
407 		/* No partial writes. */
408 		return -EINVAL;
409 	}
410 	page = (char*)get_zeroed_page(GFP_KERNEL);
411 	if (!page)
412 		return -ENOMEM;
413 	length = -EFAULT;
414 	if (copy_from_user(page, buf, count))
415 		goto out;
416 
417 	length = -EINVAL;
418 	if (sscanf(page, "%d", &new_value) != 1)
419 		goto out;
420 
421 	selinux_compat_net = new_value ? 1 : 0;
422 	length = count;
423 out:
424 	free_page((unsigned long) page);
425 	return length;
426 }
427 static struct file_operations sel_compat_net_ops = {
428 	.read		= sel_read_compat_net,
429 	.write		= sel_write_compat_net,
430 };
431 
432 /*
433  * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
434  */
435 static ssize_t sel_write_access(struct file * file, char *buf, size_t size);
436 static ssize_t sel_write_create(struct file * file, char *buf, size_t size);
437 static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size);
438 static ssize_t sel_write_user(struct file * file, char *buf, size_t size);
439 static ssize_t sel_write_member(struct file * file, char *buf, size_t size);
440 
441 static ssize_t (*write_op[])(struct file *, char *, size_t) = {
442 	[SEL_ACCESS] = sel_write_access,
443 	[SEL_CREATE] = sel_write_create,
444 	[SEL_RELABEL] = sel_write_relabel,
445 	[SEL_USER] = sel_write_user,
446 	[SEL_MEMBER] = sel_write_member,
447 	[SEL_CONTEXT] = sel_write_context,
448 };
449 
450 static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
451 {
452 	ino_t ino =  file->f_dentry->d_inode->i_ino;
453 	char *data;
454 	ssize_t rv;
455 
456 	if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
457 		return -EINVAL;
458 
459 	data = simple_transaction_get(file, buf, size);
460 	if (IS_ERR(data))
461 		return PTR_ERR(data);
462 
463 	rv =  write_op[ino](file, data, size);
464 	if (rv>0) {
465 		simple_transaction_set(file, rv);
466 		rv = size;
467 	}
468 	return rv;
469 }
470 
471 static struct file_operations transaction_ops = {
472 	.write		= selinux_transaction_write,
473 	.read		= simple_transaction_read,
474 	.release	= simple_transaction_release,
475 };
476 
477 /*
478  * payload - write methods
479  * If the method has a response, the response should be put in buf,
480  * and the length returned.  Otherwise return 0 or and -error.
481  */
482 
483 static ssize_t sel_write_access(struct file * file, char *buf, size_t size)
484 {
485 	char *scon, *tcon;
486 	u32 ssid, tsid;
487 	u16 tclass;
488 	u32 req;
489 	struct av_decision avd;
490 	ssize_t length;
491 
492 	length = task_has_security(current, SECURITY__COMPUTE_AV);
493 	if (length)
494 		return length;
495 
496 	length = -ENOMEM;
497 	scon = kzalloc(size+1, GFP_KERNEL);
498 	if (!scon)
499 		return length;
500 
501 	tcon = kzalloc(size+1, GFP_KERNEL);
502 	if (!tcon)
503 		goto out;
504 
505 	length = -EINVAL;
506 	if (sscanf(buf, "%s %s %hu %x", scon, tcon, &tclass, &req) != 4)
507 		goto out2;
508 
509 	length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
510 	if (length < 0)
511 		goto out2;
512 	length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
513 	if (length < 0)
514 		goto out2;
515 
516 	length = security_compute_av(ssid, tsid, tclass, req, &avd);
517 	if (length < 0)
518 		goto out2;
519 
520 	length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
521 			  "%x %x %x %x %u",
522 			  avd.allowed, avd.decided,
523 			  avd.auditallow, avd.auditdeny,
524 			  avd.seqno);
525 out2:
526 	kfree(tcon);
527 out:
528 	kfree(scon);
529 	return length;
530 }
531 
532 static ssize_t sel_write_create(struct file * file, char *buf, size_t size)
533 {
534 	char *scon, *tcon;
535 	u32 ssid, tsid, newsid;
536 	u16 tclass;
537 	ssize_t length;
538 	char *newcon;
539 	u32 len;
540 
541 	length = task_has_security(current, SECURITY__COMPUTE_CREATE);
542 	if (length)
543 		return length;
544 
545 	length = -ENOMEM;
546 	scon = kzalloc(size+1, GFP_KERNEL);
547 	if (!scon)
548 		return length;
549 
550 	tcon = kzalloc(size+1, GFP_KERNEL);
551 	if (!tcon)
552 		goto out;
553 
554 	length = -EINVAL;
555 	if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
556 		goto out2;
557 
558 	length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
559 	if (length < 0)
560 		goto out2;
561 	length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
562 	if (length < 0)
563 		goto out2;
564 
565 	length = security_transition_sid(ssid, tsid, tclass, &newsid);
566 	if (length < 0)
567 		goto out2;
568 
569 	length = security_sid_to_context(newsid, &newcon, &len);
570 	if (length < 0)
571 		goto out2;
572 
573 	if (len > SIMPLE_TRANSACTION_LIMIT) {
574 		printk(KERN_ERR "%s:  context size (%u) exceeds payload "
575 		       "max\n", __FUNCTION__, len);
576 		length = -ERANGE;
577 		goto out3;
578 	}
579 
580 	memcpy(buf, newcon, len);
581 	length = len;
582 out3:
583 	kfree(newcon);
584 out2:
585 	kfree(tcon);
586 out:
587 	kfree(scon);
588 	return length;
589 }
590 
591 static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size)
592 {
593 	char *scon, *tcon;
594 	u32 ssid, tsid, newsid;
595 	u16 tclass;
596 	ssize_t length;
597 	char *newcon;
598 	u32 len;
599 
600 	length = task_has_security(current, SECURITY__COMPUTE_RELABEL);
601 	if (length)
602 		return length;
603 
604 	length = -ENOMEM;
605 	scon = kzalloc(size+1, GFP_KERNEL);
606 	if (!scon)
607 		return length;
608 
609 	tcon = kzalloc(size+1, GFP_KERNEL);
610 	if (!tcon)
611 		goto out;
612 
613 	length = -EINVAL;
614 	if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
615 		goto out2;
616 
617 	length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
618 	if (length < 0)
619 		goto out2;
620 	length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
621 	if (length < 0)
622 		goto out2;
623 
624 	length = security_change_sid(ssid, tsid, tclass, &newsid);
625 	if (length < 0)
626 		goto out2;
627 
628 	length = security_sid_to_context(newsid, &newcon, &len);
629 	if (length < 0)
630 		goto out2;
631 
632 	if (len > SIMPLE_TRANSACTION_LIMIT) {
633 		length = -ERANGE;
634 		goto out3;
635 	}
636 
637 	memcpy(buf, newcon, len);
638 	length = len;
639 out3:
640 	kfree(newcon);
641 out2:
642 	kfree(tcon);
643 out:
644 	kfree(scon);
645 	return length;
646 }
647 
648 static ssize_t sel_write_user(struct file * file, char *buf, size_t size)
649 {
650 	char *con, *user, *ptr;
651 	u32 sid, *sids;
652 	ssize_t length;
653 	char *newcon;
654 	int i, rc;
655 	u32 len, nsids;
656 
657 	length = task_has_security(current, SECURITY__COMPUTE_USER);
658 	if (length)
659 		return length;
660 
661 	length = -ENOMEM;
662 	con = kzalloc(size+1, GFP_KERNEL);
663 	if (!con)
664 		return length;
665 
666 	user = kzalloc(size+1, GFP_KERNEL);
667 	if (!user)
668 		goto out;
669 
670 	length = -EINVAL;
671 	if (sscanf(buf, "%s %s", con, user) != 2)
672 		goto out2;
673 
674 	length = security_context_to_sid(con, strlen(con)+1, &sid);
675 	if (length < 0)
676 		goto out2;
677 
678 	length = security_get_user_sids(sid, user, &sids, &nsids);
679 	if (length < 0)
680 		goto out2;
681 
682 	length = sprintf(buf, "%u", nsids) + 1;
683 	ptr = buf + length;
684 	for (i = 0; i < nsids; i++) {
685 		rc = security_sid_to_context(sids[i], &newcon, &len);
686 		if (rc) {
687 			length = rc;
688 			goto out3;
689 		}
690 		if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
691 			kfree(newcon);
692 			length = -ERANGE;
693 			goto out3;
694 		}
695 		memcpy(ptr, newcon, len);
696 		kfree(newcon);
697 		ptr += len;
698 		length += len;
699 	}
700 out3:
701 	kfree(sids);
702 out2:
703 	kfree(user);
704 out:
705 	kfree(con);
706 	return length;
707 }
708 
709 static ssize_t sel_write_member(struct file * file, char *buf, size_t size)
710 {
711 	char *scon, *tcon;
712 	u32 ssid, tsid, newsid;
713 	u16 tclass;
714 	ssize_t length;
715 	char *newcon;
716 	u32 len;
717 
718 	length = task_has_security(current, SECURITY__COMPUTE_MEMBER);
719 	if (length)
720 		return length;
721 
722 	length = -ENOMEM;
723 	scon = kzalloc(size+1, GFP_KERNEL);
724 	if (!scon)
725 		return length;
726 
727 	tcon = kzalloc(size+1, GFP_KERNEL);
728 	if (!tcon)
729 		goto out;
730 
731 	length = -EINVAL;
732 	if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
733 		goto out2;
734 
735 	length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
736 	if (length < 0)
737 		goto out2;
738 	length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
739 	if (length < 0)
740 		goto out2;
741 
742 	length = security_member_sid(ssid, tsid, tclass, &newsid);
743 	if (length < 0)
744 		goto out2;
745 
746 	length = security_sid_to_context(newsid, &newcon, &len);
747 	if (length < 0)
748 		goto out2;
749 
750 	if (len > SIMPLE_TRANSACTION_LIMIT) {
751 		printk(KERN_ERR "%s:  context size (%u) exceeds payload "
752 		       "max\n", __FUNCTION__, len);
753 		length = -ERANGE;
754 		goto out3;
755 	}
756 
757 	memcpy(buf, newcon, len);
758 	length = len;
759 out3:
760 	kfree(newcon);
761 out2:
762 	kfree(tcon);
763 out:
764 	kfree(scon);
765 	return length;
766 }
767 
768 static struct inode *sel_make_inode(struct super_block *sb, int mode)
769 {
770 	struct inode *ret = new_inode(sb);
771 
772 	if (ret) {
773 		ret->i_mode = mode;
774 		ret->i_uid = ret->i_gid = 0;
775 		ret->i_blksize = PAGE_CACHE_SIZE;
776 		ret->i_blocks = 0;
777 		ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
778 	}
779 	return ret;
780 }
781 
782 #define BOOL_INO_OFFSET 30
783 
784 static ssize_t sel_read_bool(struct file *filep, char __user *buf,
785 			     size_t count, loff_t *ppos)
786 {
787 	char *page = NULL;
788 	ssize_t length;
789 	ssize_t ret;
790 	int cur_enforcing;
791 	struct inode *inode;
792 
793 	mutex_lock(&sel_mutex);
794 
795 	ret = -EFAULT;
796 
797 	/* check to see if this file has been deleted */
798 	if (!filep->f_op)
799 		goto out;
800 
801 	if (count > PAGE_SIZE) {
802 		ret = -EINVAL;
803 		goto out;
804 	}
805 	if (!(page = (char*)get_zeroed_page(GFP_KERNEL))) {
806 		ret = -ENOMEM;
807 		goto out;
808 	}
809 
810 	inode = filep->f_dentry->d_inode;
811 	cur_enforcing = security_get_bool_value(inode->i_ino - BOOL_INO_OFFSET);
812 	if (cur_enforcing < 0) {
813 		ret = cur_enforcing;
814 		goto out;
815 	}
816 
817 	length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
818 			  bool_pending_values[inode->i_ino - BOOL_INO_OFFSET]);
819 	ret = simple_read_from_buffer(buf, count, ppos, page, length);
820 out:
821 	mutex_unlock(&sel_mutex);
822 	if (page)
823 		free_page((unsigned long)page);
824 	return ret;
825 }
826 
827 static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
828 			      size_t count, loff_t *ppos)
829 {
830 	char *page = NULL;
831 	ssize_t length = -EFAULT;
832 	int new_value;
833 	struct inode *inode;
834 
835 	mutex_lock(&sel_mutex);
836 
837 	length = task_has_security(current, SECURITY__SETBOOL);
838 	if (length)
839 		goto out;
840 
841 	/* check to see if this file has been deleted */
842 	if (!filep->f_op)
843 		goto out;
844 
845 	if (count >= PAGE_SIZE) {
846 		length = -ENOMEM;
847 		goto out;
848 	}
849 	if (*ppos != 0) {
850 		/* No partial writes. */
851 		goto out;
852 	}
853 	page = (char*)get_zeroed_page(GFP_KERNEL);
854 	if (!page) {
855 		length = -ENOMEM;
856 		goto out;
857 	}
858 
859 	if (copy_from_user(page, buf, count))
860 		goto out;
861 
862 	length = -EINVAL;
863 	if (sscanf(page, "%d", &new_value) != 1)
864 		goto out;
865 
866 	if (new_value)
867 		new_value = 1;
868 
869 	inode = filep->f_dentry->d_inode;
870 	bool_pending_values[inode->i_ino - BOOL_INO_OFFSET] = new_value;
871 	length = count;
872 
873 out:
874 	mutex_unlock(&sel_mutex);
875 	if (page)
876 		free_page((unsigned long) page);
877 	return length;
878 }
879 
880 static struct file_operations sel_bool_ops = {
881 	.read           = sel_read_bool,
882 	.write          = sel_write_bool,
883 };
884 
885 static ssize_t sel_commit_bools_write(struct file *filep,
886 				      const char __user *buf,
887 				      size_t count, loff_t *ppos)
888 {
889 	char *page = NULL;
890 	ssize_t length = -EFAULT;
891 	int new_value;
892 
893 	mutex_lock(&sel_mutex);
894 
895 	length = task_has_security(current, SECURITY__SETBOOL);
896 	if (length)
897 		goto out;
898 
899 	/* check to see if this file has been deleted */
900 	if (!filep->f_op)
901 		goto out;
902 
903 	if (count >= PAGE_SIZE) {
904 		length = -ENOMEM;
905 		goto out;
906 	}
907 	if (*ppos != 0) {
908 		/* No partial writes. */
909 		goto out;
910 	}
911 	page = (char*)get_zeroed_page(GFP_KERNEL);
912 	if (!page) {
913 		length = -ENOMEM;
914 		goto out;
915 	}
916 
917 	if (copy_from_user(page, buf, count))
918 		goto out;
919 
920 	length = -EINVAL;
921 	if (sscanf(page, "%d", &new_value) != 1)
922 		goto out;
923 
924 	if (new_value && bool_pending_values) {
925 		security_set_bools(bool_num, bool_pending_values);
926 	}
927 
928 	length = count;
929 
930 out:
931 	mutex_unlock(&sel_mutex);
932 	if (page)
933 		free_page((unsigned long) page);
934 	return length;
935 }
936 
937 static struct file_operations sel_commit_bools_ops = {
938 	.write          = sel_commit_bools_write,
939 };
940 
941 /* delete booleans - partial revoke() from
942  * fs/proc/generic.c proc_kill_inodes */
943 static void sel_remove_bools(struct dentry *de)
944 {
945 	struct list_head *p, *node;
946 	struct super_block *sb = de->d_sb;
947 
948 	spin_lock(&dcache_lock);
949 	node = de->d_subdirs.next;
950 	while (node != &de->d_subdirs) {
951 		struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
952 		list_del_init(node);
953 
954 		if (d->d_inode) {
955 			d = dget_locked(d);
956 			spin_unlock(&dcache_lock);
957 			d_delete(d);
958 			simple_unlink(de->d_inode, d);
959 			dput(d);
960 			spin_lock(&dcache_lock);
961 		}
962 		node = de->d_subdirs.next;
963 	}
964 
965 	spin_unlock(&dcache_lock);
966 
967 	file_list_lock();
968 	list_for_each(p, &sb->s_files) {
969 		struct file * filp = list_entry(p, struct file, f_u.fu_list);
970 		struct dentry * dentry = filp->f_dentry;
971 
972 		if (dentry->d_parent != de) {
973 			continue;
974 		}
975 		filp->f_op = NULL;
976 	}
977 	file_list_unlock();
978 }
979 
980 #define BOOL_DIR_NAME "booleans"
981 
982 static int sel_make_bools(void)
983 {
984 	int i, ret = 0;
985 	ssize_t len;
986 	struct dentry *dentry = NULL;
987 	struct dentry *dir = bool_dir;
988 	struct inode *inode = NULL;
989 	struct inode_security_struct *isec;
990 	char **names = NULL, *page;
991 	int num;
992 	int *values = NULL;
993 	u32 sid;
994 
995 	/* remove any existing files */
996 	kfree(bool_pending_values);
997 	bool_pending_values = NULL;
998 
999 	sel_remove_bools(dir);
1000 
1001 	if (!(page = (char*)get_zeroed_page(GFP_KERNEL)))
1002 		return -ENOMEM;
1003 
1004 	ret = security_get_bools(&num, &names, &values);
1005 	if (ret != 0)
1006 		goto out;
1007 
1008 	for (i = 0; i < num; i++) {
1009 		dentry = d_alloc_name(dir, names[i]);
1010 		if (!dentry) {
1011 			ret = -ENOMEM;
1012 			goto err;
1013 		}
1014 		inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1015 		if (!inode) {
1016 			ret = -ENOMEM;
1017 			goto err;
1018 		}
1019 
1020 		len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
1021 		if (len < 0) {
1022 			ret = -EINVAL;
1023 			goto err;
1024 		} else if (len >= PAGE_SIZE) {
1025 			ret = -ENAMETOOLONG;
1026 			goto err;
1027 		}
1028 		isec = (struct inode_security_struct*)inode->i_security;
1029 		if ((ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid)))
1030 			goto err;
1031 		isec->sid = sid;
1032 		isec->initialized = 1;
1033 		inode->i_fop = &sel_bool_ops;
1034 		inode->i_ino = i + BOOL_INO_OFFSET;
1035 		d_add(dentry, inode);
1036 	}
1037 	bool_num = num;
1038 	bool_pending_values = values;
1039 out:
1040 	free_page((unsigned long)page);
1041 	if (names) {
1042 		for (i = 0; i < num; i++)
1043 			kfree(names[i]);
1044 		kfree(names);
1045 	}
1046 	return ret;
1047 err:
1048 	kfree(values);
1049 	sel_remove_bools(dir);
1050 	ret = -ENOMEM;
1051 	goto out;
1052 }
1053 
1054 #define NULL_FILE_NAME "null"
1055 
1056 struct dentry *selinux_null = NULL;
1057 
1058 static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1059 					    size_t count, loff_t *ppos)
1060 {
1061 	char tmpbuf[TMPBUFLEN];
1062 	ssize_t length;
1063 
1064 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
1065 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1066 }
1067 
1068 static ssize_t sel_write_avc_cache_threshold(struct file * file,
1069 					     const char __user * buf,
1070 					     size_t count, loff_t *ppos)
1071 
1072 {
1073 	char *page;
1074 	ssize_t ret;
1075 	int new_value;
1076 
1077 	if (count >= PAGE_SIZE) {
1078 		ret = -ENOMEM;
1079 		goto out;
1080 	}
1081 
1082 	if (*ppos != 0) {
1083 		/* No partial writes. */
1084 		ret = -EINVAL;
1085 		goto out;
1086 	}
1087 
1088 	page = (char*)get_zeroed_page(GFP_KERNEL);
1089 	if (!page) {
1090 		ret = -ENOMEM;
1091 		goto out;
1092 	}
1093 
1094 	if (copy_from_user(page, buf, count)) {
1095 		ret = -EFAULT;
1096 		goto out_free;
1097 	}
1098 
1099 	if (sscanf(page, "%u", &new_value) != 1) {
1100 		ret = -EINVAL;
1101 		goto out;
1102 	}
1103 
1104 	if (new_value != avc_cache_threshold) {
1105 		ret = task_has_security(current, SECURITY__SETSECPARAM);
1106 		if (ret)
1107 			goto out_free;
1108 		avc_cache_threshold = new_value;
1109 	}
1110 	ret = count;
1111 out_free:
1112 	free_page((unsigned long)page);
1113 out:
1114 	return ret;
1115 }
1116 
1117 static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1118 				       size_t count, loff_t *ppos)
1119 {
1120 	char *page;
1121 	ssize_t ret = 0;
1122 
1123 	page = (char *)__get_free_page(GFP_KERNEL);
1124 	if (!page) {
1125 		ret = -ENOMEM;
1126 		goto out;
1127 	}
1128 	ret = avc_get_hash_stats(page);
1129 	if (ret >= 0)
1130 		ret = simple_read_from_buffer(buf, count, ppos, page, ret);
1131 	free_page((unsigned long)page);
1132 out:
1133 	return ret;
1134 }
1135 
1136 static struct file_operations sel_avc_cache_threshold_ops = {
1137 	.read		= sel_read_avc_cache_threshold,
1138 	.write		= sel_write_avc_cache_threshold,
1139 };
1140 
1141 static struct file_operations sel_avc_hash_stats_ops = {
1142 	.read		= sel_read_avc_hash_stats,
1143 };
1144 
1145 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1146 static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1147 {
1148 	int cpu;
1149 
1150 	for (cpu = *idx; cpu < NR_CPUS; ++cpu) {
1151 		if (!cpu_possible(cpu))
1152 			continue;
1153 		*idx = cpu + 1;
1154 		return &per_cpu(avc_cache_stats, cpu);
1155 	}
1156 	return NULL;
1157 }
1158 
1159 static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1160 {
1161 	loff_t n = *pos - 1;
1162 
1163 	if (*pos == 0)
1164 		return SEQ_START_TOKEN;
1165 
1166 	return sel_avc_get_stat_idx(&n);
1167 }
1168 
1169 static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1170 {
1171 	return sel_avc_get_stat_idx(pos);
1172 }
1173 
1174 static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1175 {
1176 	struct avc_cache_stats *st = v;
1177 
1178 	if (v == SEQ_START_TOKEN)
1179 		seq_printf(seq, "lookups hits misses allocations reclaims "
1180 			   "frees\n");
1181 	else
1182 		seq_printf(seq, "%u %u %u %u %u %u\n", st->lookups,
1183 			   st->hits, st->misses, st->allocations,
1184 			   st->reclaims, st->frees);
1185 	return 0;
1186 }
1187 
1188 static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1189 { }
1190 
1191 static struct seq_operations sel_avc_cache_stats_seq_ops = {
1192 	.start		= sel_avc_stats_seq_start,
1193 	.next		= sel_avc_stats_seq_next,
1194 	.show		= sel_avc_stats_seq_show,
1195 	.stop		= sel_avc_stats_seq_stop,
1196 };
1197 
1198 static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1199 {
1200 	return seq_open(file, &sel_avc_cache_stats_seq_ops);
1201 }
1202 
1203 static struct file_operations sel_avc_cache_stats_ops = {
1204 	.open		= sel_open_avc_cache_stats,
1205 	.read		= seq_read,
1206 	.llseek		= seq_lseek,
1207 	.release	= seq_release,
1208 };
1209 #endif
1210 
1211 static int sel_make_avc_files(struct dentry *dir)
1212 {
1213 	int i, ret = 0;
1214 	static struct tree_descr files[] = {
1215 		{ "cache_threshold",
1216 		  &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1217 		{ "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1218 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1219 		{ "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1220 #endif
1221 	};
1222 
1223 	for (i = 0; i < ARRAY_SIZE(files); i++) {
1224 		struct inode *inode;
1225 		struct dentry *dentry;
1226 
1227 		dentry = d_alloc_name(dir, files[i].name);
1228 		if (!dentry) {
1229 			ret = -ENOMEM;
1230 			goto out;
1231 		}
1232 
1233 		inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1234 		if (!inode) {
1235 			ret = -ENOMEM;
1236 			goto out;
1237 		}
1238 		inode->i_fop = files[i].ops;
1239 		d_add(dentry, inode);
1240 	}
1241 out:
1242 	return ret;
1243 }
1244 
1245 static int sel_make_dir(struct inode *dir, struct dentry *dentry)
1246 {
1247 	int ret = 0;
1248 	struct inode *inode;
1249 
1250 	inode = sel_make_inode(dir->i_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1251 	if (!inode) {
1252 		ret = -ENOMEM;
1253 		goto out;
1254 	}
1255 	inode->i_op = &simple_dir_inode_operations;
1256 	inode->i_fop = &simple_dir_operations;
1257 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
1258 	inode->i_nlink++;
1259 	d_add(dentry, inode);
1260 	/* bump link count on parent directory, too */
1261 	dir->i_nlink++;
1262 out:
1263 	return ret;
1264 }
1265 
1266 static int sel_fill_super(struct super_block * sb, void * data, int silent)
1267 {
1268 	int ret;
1269 	struct dentry *dentry;
1270 	struct inode *inode, *root_inode;
1271 	struct inode_security_struct *isec;
1272 
1273 	static struct tree_descr selinux_files[] = {
1274 		[SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1275 		[SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
1276 		[SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
1277 		[SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1278 		[SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1279 		[SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1280 		[SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1281 		[SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1282 		[SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1283 		[SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1284 		[SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1285 		[SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1286 		[SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
1287 		[SEL_COMPAT_NET] = {"compat_net", &sel_compat_net_ops, S_IRUGO|S_IWUSR},
1288 		/* last one */ {""}
1289 	};
1290 	ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1291 	if (ret)
1292 		goto err;
1293 
1294 	root_inode = sb->s_root->d_inode;
1295 
1296 	dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME);
1297 	if (!dentry) {
1298 		ret = -ENOMEM;
1299 		goto err;
1300 	}
1301 
1302 	ret = sel_make_dir(root_inode, dentry);
1303 	if (ret)
1304 		goto err;
1305 
1306 	bool_dir = dentry;
1307 
1308 	dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
1309 	if (!dentry) {
1310 		ret = -ENOMEM;
1311 		goto err;
1312 	}
1313 
1314 	inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
1315 	if (!inode) {
1316 		ret = -ENOMEM;
1317 		goto err;
1318 	}
1319 	isec = (struct inode_security_struct*)inode->i_security;
1320 	isec->sid = SECINITSID_DEVNULL;
1321 	isec->sclass = SECCLASS_CHR_FILE;
1322 	isec->initialized = 1;
1323 
1324 	init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1325 	d_add(dentry, inode);
1326 	selinux_null = dentry;
1327 
1328 	dentry = d_alloc_name(sb->s_root, "avc");
1329 	if (!dentry) {
1330 		ret = -ENOMEM;
1331 		goto err;
1332 	}
1333 
1334 	ret = sel_make_dir(root_inode, dentry);
1335 	if (ret)
1336 		goto err;
1337 
1338 	ret = sel_make_avc_files(dentry);
1339 	if (ret)
1340 		goto err;
1341 out:
1342 	return ret;
1343 err:
1344 	printk(KERN_ERR "%s:  failed while creating inodes\n", __FUNCTION__);
1345 	goto out;
1346 }
1347 
1348 static struct super_block *sel_get_sb(struct file_system_type *fs_type,
1349 				      int flags, const char *dev_name, void *data)
1350 {
1351 	return get_sb_single(fs_type, flags, data, sel_fill_super);
1352 }
1353 
1354 static struct file_system_type sel_fs_type = {
1355 	.name		= "selinuxfs",
1356 	.get_sb		= sel_get_sb,
1357 	.kill_sb	= kill_litter_super,
1358 };
1359 
1360 struct vfsmount *selinuxfs_mount;
1361 
1362 static int __init init_sel_fs(void)
1363 {
1364 	int err;
1365 
1366 	if (!selinux_enabled)
1367 		return 0;
1368 	err = register_filesystem(&sel_fs_type);
1369 	if (!err) {
1370 		selinuxfs_mount = kern_mount(&sel_fs_type);
1371 		if (IS_ERR(selinuxfs_mount)) {
1372 			printk(KERN_ERR "selinuxfs:  could not mount!\n");
1373 			err = PTR_ERR(selinuxfs_mount);
1374 			selinuxfs_mount = NULL;
1375 		}
1376 	}
1377 	return err;
1378 }
1379 
1380 __initcall(init_sel_fs);
1381 
1382 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
1383 void exit_sel_fs(void)
1384 {
1385 	unregister_filesystem(&sel_fs_type);
1386 }
1387 #endif
1388