m68knommu: remove empty timer offset function for 68360
[wrt350n-kernel.git] / security / selinux / selinuxfs.c
bloba85740530afc676fdb777c88a8cb4136d90370cd
1 /* Updated: Karl MacMillan <kmacmillan@tresys.com>
3 * Added conditional policy language extensions
5 * Updated: Hewlett-Packard <paul.moore@hp.com>
7 * Added support for the policy capability bitmap
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.
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/mutex.h>
23 #include <linux/init.h>
24 #include <linux/string.h>
25 #include <linux/security.h>
26 #include <linux/major.h>
27 #include <linux/seq_file.h>
28 #include <linux/percpu.h>
29 #include <linux/audit.h>
30 #include <asm/uaccess.h>
31 #include <asm/semaphore.h>
33 /* selinuxfs pseudo filesystem for exporting the security policy API.
34 Based on the proc code and the fs/nfsd/nfsctl.c code. */
36 #include "flask.h"
37 #include "avc.h"
38 #include "avc_ss.h"
39 #include "security.h"
40 #include "objsec.h"
41 #include "conditional.h"
43 /* Policy capability filenames */
44 static char *policycap_names[] = {
45 "network_peer_controls"
48 unsigned int selinux_checkreqprot = CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
50 #ifdef CONFIG_SECURITY_SELINUX_ENABLE_SECMARK_DEFAULT
51 #define SELINUX_COMPAT_NET_VALUE 0
52 #else
53 #define SELINUX_COMPAT_NET_VALUE 1
54 #endif
56 int selinux_compat_net = SELINUX_COMPAT_NET_VALUE;
58 static int __init checkreqprot_setup(char *str)
60 selinux_checkreqprot = simple_strtoul(str,NULL,0) ? 1 : 0;
61 return 1;
63 __setup("checkreqprot=", checkreqprot_setup);
65 static int __init selinux_compat_net_setup(char *str)
67 selinux_compat_net = simple_strtoul(str,NULL,0) ? 1 : 0;
68 return 1;
70 __setup("selinux_compat_net=", selinux_compat_net_setup);
73 static DEFINE_MUTEX(sel_mutex);
75 /* global data for booleans */
76 static struct dentry *bool_dir = NULL;
77 static int bool_num = 0;
78 static char **bool_pending_names;
79 static int *bool_pending_values = NULL;
81 /* global data for classes */
82 static struct dentry *class_dir = NULL;
83 static unsigned long last_class_ino;
85 /* global data for policy capabilities */
86 static struct dentry *policycap_dir = NULL;
88 extern void selnl_notify_setenforce(int val);
90 /* Check whether a task is allowed to use a security operation. */
91 static int task_has_security(struct task_struct *tsk,
92 u32 perms)
94 struct task_security_struct *tsec;
96 tsec = tsk->security;
97 if (!tsec)
98 return -EACCES;
100 return avc_has_perm(tsec->sid, SECINITSID_SECURITY,
101 SECCLASS_SECURITY, perms, NULL);
104 enum sel_inos {
105 SEL_ROOT_INO = 2,
106 SEL_LOAD, /* load policy */
107 SEL_ENFORCE, /* get or set enforcing status */
108 SEL_CONTEXT, /* validate context */
109 SEL_ACCESS, /* compute access decision */
110 SEL_CREATE, /* compute create labeling decision */
111 SEL_RELABEL, /* compute relabeling decision */
112 SEL_USER, /* compute reachable user contexts */
113 SEL_POLICYVERS, /* return policy version for this kernel */
114 SEL_COMMIT_BOOLS, /* commit new boolean values */
115 SEL_MLS, /* return if MLS policy is enabled */
116 SEL_DISABLE, /* disable SELinux until next reboot */
117 SEL_MEMBER, /* compute polyinstantiation membership decision */
118 SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
119 SEL_COMPAT_NET, /* whether to use old compat network packet controls */
120 SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */
121 SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */
122 SEL_INO_NEXT, /* The next inode number to use */
125 static unsigned long sel_last_ino = SEL_INO_NEXT - 1;
127 #define SEL_INITCON_INO_OFFSET 0x01000000
128 #define SEL_BOOL_INO_OFFSET 0x02000000
129 #define SEL_CLASS_INO_OFFSET 0x04000000
130 #define SEL_POLICYCAP_INO_OFFSET 0x08000000
131 #define SEL_INO_MASK 0x00ffffff
133 #define TMPBUFLEN 12
134 static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
135 size_t count, loff_t *ppos)
137 char tmpbuf[TMPBUFLEN];
138 ssize_t length;
140 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing);
141 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
144 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
145 static ssize_t sel_write_enforce(struct file * file, const char __user * buf,
146 size_t count, loff_t *ppos)
149 char *page;
150 ssize_t length;
151 int new_value;
153 if (count >= PAGE_SIZE)
154 return -ENOMEM;
155 if (*ppos != 0) {
156 /* No partial writes. */
157 return -EINVAL;
159 page = (char*)get_zeroed_page(GFP_KERNEL);
160 if (!page)
161 return -ENOMEM;
162 length = -EFAULT;
163 if (copy_from_user(page, buf, count))
164 goto out;
166 length = -EINVAL;
167 if (sscanf(page, "%d", &new_value) != 1)
168 goto out;
170 if (new_value != selinux_enforcing) {
171 length = task_has_security(current, SECURITY__SETENFORCE);
172 if (length)
173 goto out;
174 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
175 "enforcing=%d old_enforcing=%d auid=%u", new_value,
176 selinux_enforcing,
177 audit_get_loginuid(current->audit_context));
178 selinux_enforcing = new_value;
179 if (selinux_enforcing)
180 avc_ss_reset(0);
181 selnl_notify_setenforce(selinux_enforcing);
183 length = count;
184 out:
185 free_page((unsigned long) page);
186 return length;
188 #else
189 #define sel_write_enforce NULL
190 #endif
192 static const struct file_operations sel_enforce_ops = {
193 .read = sel_read_enforce,
194 .write = sel_write_enforce,
197 static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf,
198 size_t count, loff_t *ppos)
200 char tmpbuf[TMPBUFLEN];
201 ssize_t length;
202 ino_t ino = filp->f_path.dentry->d_inode->i_ino;
203 int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
204 security_get_reject_unknown() : !security_get_allow_unknown();
206 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
207 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
210 static const struct file_operations sel_handle_unknown_ops = {
211 .read = sel_read_handle_unknown,
214 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
215 static ssize_t sel_write_disable(struct file * file, const char __user * buf,
216 size_t count, loff_t *ppos)
219 char *page;
220 ssize_t length;
221 int new_value;
222 extern int selinux_disable(void);
224 if (count >= PAGE_SIZE)
225 return -ENOMEM;
226 if (*ppos != 0) {
227 /* No partial writes. */
228 return -EINVAL;
230 page = (char*)get_zeroed_page(GFP_KERNEL);
231 if (!page)
232 return -ENOMEM;
233 length = -EFAULT;
234 if (copy_from_user(page, buf, count))
235 goto out;
237 length = -EINVAL;
238 if (sscanf(page, "%d", &new_value) != 1)
239 goto out;
241 if (new_value) {
242 length = selinux_disable();
243 if (length < 0)
244 goto out;
245 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
246 "selinux=0 auid=%u",
247 audit_get_loginuid(current->audit_context));
250 length = count;
251 out:
252 free_page((unsigned long) page);
253 return length;
255 #else
256 #define sel_write_disable NULL
257 #endif
259 static const struct file_operations sel_disable_ops = {
260 .write = sel_write_disable,
263 static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
264 size_t count, loff_t *ppos)
266 char tmpbuf[TMPBUFLEN];
267 ssize_t length;
269 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
270 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
273 static const struct file_operations sel_policyvers_ops = {
274 .read = sel_read_policyvers,
277 /* declaration for sel_write_load */
278 static int sel_make_bools(void);
279 static int sel_make_classes(void);
280 static int sel_make_policycap(void);
282 /* declaration for sel_make_class_dirs */
283 static int sel_make_dir(struct inode *dir, struct dentry *dentry,
284 unsigned long *ino);
286 static ssize_t sel_read_mls(struct file *filp, char __user *buf,
287 size_t count, loff_t *ppos)
289 char tmpbuf[TMPBUFLEN];
290 ssize_t length;
292 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_mls_enabled);
293 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
296 static const struct file_operations sel_mls_ops = {
297 .read = sel_read_mls,
300 static ssize_t sel_write_load(struct file * file, const char __user * buf,
301 size_t count, loff_t *ppos)
304 int ret;
305 ssize_t length;
306 void *data = NULL;
308 mutex_lock(&sel_mutex);
310 length = task_has_security(current, SECURITY__LOAD_POLICY);
311 if (length)
312 goto out;
314 if (*ppos != 0) {
315 /* No partial writes. */
316 length = -EINVAL;
317 goto out;
320 if ((count > 64 * 1024 * 1024)
321 || (data = vmalloc(count)) == NULL) {
322 length = -ENOMEM;
323 goto out;
326 length = -EFAULT;
327 if (copy_from_user(data, buf, count) != 0)
328 goto out;
330 length = security_load_policy(data, count);
331 if (length)
332 goto out;
334 ret = sel_make_bools();
335 if (ret) {
336 length = ret;
337 goto out1;
340 ret = sel_make_classes();
341 if (ret) {
342 length = ret;
343 goto out1;
346 ret = sel_make_policycap();
347 if (ret)
348 length = ret;
349 else
350 length = count;
352 out1:
354 printk(KERN_INFO "SELinux: policy loaded with handle_unknown=%s\n",
355 (security_get_reject_unknown() ? "reject" :
356 (security_get_allow_unknown() ? "allow" : "deny")));
358 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
359 "policy loaded auid=%u",
360 audit_get_loginuid(current->audit_context));
361 out:
362 mutex_unlock(&sel_mutex);
363 vfree(data);
364 return length;
367 static const struct file_operations sel_load_ops = {
368 .write = sel_write_load,
371 static ssize_t sel_write_context(struct file * file, char *buf, size_t size)
373 char *canon;
374 u32 sid, len;
375 ssize_t length;
377 length = task_has_security(current, SECURITY__CHECK_CONTEXT);
378 if (length)
379 return length;
381 length = security_context_to_sid(buf, size, &sid);
382 if (length < 0)
383 return length;
385 length = security_sid_to_context(sid, &canon, &len);
386 if (length < 0)
387 return length;
389 if (len > SIMPLE_TRANSACTION_LIMIT) {
390 printk(KERN_ERR "%s: context size (%u) exceeds payload "
391 "max\n", __FUNCTION__, len);
392 length = -ERANGE;
393 goto out;
396 memcpy(buf, canon, len);
397 length = len;
398 out:
399 kfree(canon);
400 return length;
403 static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
404 size_t count, loff_t *ppos)
406 char tmpbuf[TMPBUFLEN];
407 ssize_t length;
409 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot);
410 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
413 static ssize_t sel_write_checkreqprot(struct file * file, const char __user * buf,
414 size_t count, loff_t *ppos)
416 char *page;
417 ssize_t length;
418 unsigned int new_value;
420 length = task_has_security(current, SECURITY__SETCHECKREQPROT);
421 if (length)
422 return length;
424 if (count >= PAGE_SIZE)
425 return -ENOMEM;
426 if (*ppos != 0) {
427 /* No partial writes. */
428 return -EINVAL;
430 page = (char*)get_zeroed_page(GFP_KERNEL);
431 if (!page)
432 return -ENOMEM;
433 length = -EFAULT;
434 if (copy_from_user(page, buf, count))
435 goto out;
437 length = -EINVAL;
438 if (sscanf(page, "%u", &new_value) != 1)
439 goto out;
441 selinux_checkreqprot = new_value ? 1 : 0;
442 length = count;
443 out:
444 free_page((unsigned long) page);
445 return length;
447 static const struct file_operations sel_checkreqprot_ops = {
448 .read = sel_read_checkreqprot,
449 .write = sel_write_checkreqprot,
452 static ssize_t sel_read_compat_net(struct file *filp, char __user *buf,
453 size_t count, loff_t *ppos)
455 char tmpbuf[TMPBUFLEN];
456 ssize_t length;
458 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_compat_net);
459 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
462 static ssize_t sel_write_compat_net(struct file * file, const char __user * buf,
463 size_t count, loff_t *ppos)
465 char *page;
466 ssize_t length;
467 int new_value;
469 length = task_has_security(current, SECURITY__LOAD_POLICY);
470 if (length)
471 return length;
473 if (count >= PAGE_SIZE)
474 return -ENOMEM;
475 if (*ppos != 0) {
476 /* No partial writes. */
477 return -EINVAL;
479 page = (char*)get_zeroed_page(GFP_KERNEL);
480 if (!page)
481 return -ENOMEM;
482 length = -EFAULT;
483 if (copy_from_user(page, buf, count))
484 goto out;
486 length = -EINVAL;
487 if (sscanf(page, "%d", &new_value) != 1)
488 goto out;
490 selinux_compat_net = new_value ? 1 : 0;
491 length = count;
492 out:
493 free_page((unsigned long) page);
494 return length;
496 static const struct file_operations sel_compat_net_ops = {
497 .read = sel_read_compat_net,
498 .write = sel_write_compat_net,
502 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
504 static ssize_t sel_write_access(struct file * file, char *buf, size_t size);
505 static ssize_t sel_write_create(struct file * file, char *buf, size_t size);
506 static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size);
507 static ssize_t sel_write_user(struct file * file, char *buf, size_t size);
508 static ssize_t sel_write_member(struct file * file, char *buf, size_t size);
510 static ssize_t (*write_op[])(struct file *, char *, size_t) = {
511 [SEL_ACCESS] = sel_write_access,
512 [SEL_CREATE] = sel_write_create,
513 [SEL_RELABEL] = sel_write_relabel,
514 [SEL_USER] = sel_write_user,
515 [SEL_MEMBER] = sel_write_member,
516 [SEL_CONTEXT] = sel_write_context,
519 static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
521 ino_t ino = file->f_path.dentry->d_inode->i_ino;
522 char *data;
523 ssize_t rv;
525 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
526 return -EINVAL;
528 data = simple_transaction_get(file, buf, size);
529 if (IS_ERR(data))
530 return PTR_ERR(data);
532 rv = write_op[ino](file, data, size);
533 if (rv>0) {
534 simple_transaction_set(file, rv);
535 rv = size;
537 return rv;
540 static const struct file_operations transaction_ops = {
541 .write = selinux_transaction_write,
542 .read = simple_transaction_read,
543 .release = simple_transaction_release,
547 * payload - write methods
548 * If the method has a response, the response should be put in buf,
549 * and the length returned. Otherwise return 0 or and -error.
552 static ssize_t sel_write_access(struct file * file, char *buf, size_t size)
554 char *scon, *tcon;
555 u32 ssid, tsid;
556 u16 tclass;
557 u32 req;
558 struct av_decision avd;
559 ssize_t length;
561 length = task_has_security(current, SECURITY__COMPUTE_AV);
562 if (length)
563 return length;
565 length = -ENOMEM;
566 scon = kzalloc(size+1, GFP_KERNEL);
567 if (!scon)
568 return length;
570 tcon = kzalloc(size+1, GFP_KERNEL);
571 if (!tcon)
572 goto out;
574 length = -EINVAL;
575 if (sscanf(buf, "%s %s %hu %x", scon, tcon, &tclass, &req) != 4)
576 goto out2;
578 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
579 if (length < 0)
580 goto out2;
581 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
582 if (length < 0)
583 goto out2;
585 length = security_compute_av(ssid, tsid, tclass, req, &avd);
586 if (length < 0)
587 goto out2;
589 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
590 "%x %x %x %x %u",
591 avd.allowed, avd.decided,
592 avd.auditallow, avd.auditdeny,
593 avd.seqno);
594 out2:
595 kfree(tcon);
596 out:
597 kfree(scon);
598 return length;
601 static ssize_t sel_write_create(struct file * file, char *buf, size_t size)
603 char *scon, *tcon;
604 u32 ssid, tsid, newsid;
605 u16 tclass;
606 ssize_t length;
607 char *newcon;
608 u32 len;
610 length = task_has_security(current, SECURITY__COMPUTE_CREATE);
611 if (length)
612 return length;
614 length = -ENOMEM;
615 scon = kzalloc(size+1, GFP_KERNEL);
616 if (!scon)
617 return length;
619 tcon = kzalloc(size+1, GFP_KERNEL);
620 if (!tcon)
621 goto out;
623 length = -EINVAL;
624 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
625 goto out2;
627 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
628 if (length < 0)
629 goto out2;
630 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
631 if (length < 0)
632 goto out2;
634 length = security_transition_sid(ssid, tsid, tclass, &newsid);
635 if (length < 0)
636 goto out2;
638 length = security_sid_to_context(newsid, &newcon, &len);
639 if (length < 0)
640 goto out2;
642 if (len > SIMPLE_TRANSACTION_LIMIT) {
643 printk(KERN_ERR "%s: context size (%u) exceeds payload "
644 "max\n", __FUNCTION__, len);
645 length = -ERANGE;
646 goto out3;
649 memcpy(buf, newcon, len);
650 length = len;
651 out3:
652 kfree(newcon);
653 out2:
654 kfree(tcon);
655 out:
656 kfree(scon);
657 return length;
660 static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size)
662 char *scon, *tcon;
663 u32 ssid, tsid, newsid;
664 u16 tclass;
665 ssize_t length;
666 char *newcon;
667 u32 len;
669 length = task_has_security(current, SECURITY__COMPUTE_RELABEL);
670 if (length)
671 return length;
673 length = -ENOMEM;
674 scon = kzalloc(size+1, GFP_KERNEL);
675 if (!scon)
676 return length;
678 tcon = kzalloc(size+1, GFP_KERNEL);
679 if (!tcon)
680 goto out;
682 length = -EINVAL;
683 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
684 goto out2;
686 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
687 if (length < 0)
688 goto out2;
689 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
690 if (length < 0)
691 goto out2;
693 length = security_change_sid(ssid, tsid, tclass, &newsid);
694 if (length < 0)
695 goto out2;
697 length = security_sid_to_context(newsid, &newcon, &len);
698 if (length < 0)
699 goto out2;
701 if (len > SIMPLE_TRANSACTION_LIMIT) {
702 length = -ERANGE;
703 goto out3;
706 memcpy(buf, newcon, len);
707 length = len;
708 out3:
709 kfree(newcon);
710 out2:
711 kfree(tcon);
712 out:
713 kfree(scon);
714 return length;
717 static ssize_t sel_write_user(struct file * file, char *buf, size_t size)
719 char *con, *user, *ptr;
720 u32 sid, *sids;
721 ssize_t length;
722 char *newcon;
723 int i, rc;
724 u32 len, nsids;
726 length = task_has_security(current, SECURITY__COMPUTE_USER);
727 if (length)
728 return length;
730 length = -ENOMEM;
731 con = kzalloc(size+1, GFP_KERNEL);
732 if (!con)
733 return length;
735 user = kzalloc(size+1, GFP_KERNEL);
736 if (!user)
737 goto out;
739 length = -EINVAL;
740 if (sscanf(buf, "%s %s", con, user) != 2)
741 goto out2;
743 length = security_context_to_sid(con, strlen(con)+1, &sid);
744 if (length < 0)
745 goto out2;
747 length = security_get_user_sids(sid, user, &sids, &nsids);
748 if (length < 0)
749 goto out2;
751 length = sprintf(buf, "%u", nsids) + 1;
752 ptr = buf + length;
753 for (i = 0; i < nsids; i++) {
754 rc = security_sid_to_context(sids[i], &newcon, &len);
755 if (rc) {
756 length = rc;
757 goto out3;
759 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
760 kfree(newcon);
761 length = -ERANGE;
762 goto out3;
764 memcpy(ptr, newcon, len);
765 kfree(newcon);
766 ptr += len;
767 length += len;
769 out3:
770 kfree(sids);
771 out2:
772 kfree(user);
773 out:
774 kfree(con);
775 return length;
778 static ssize_t sel_write_member(struct file * file, char *buf, size_t size)
780 char *scon, *tcon;
781 u32 ssid, tsid, newsid;
782 u16 tclass;
783 ssize_t length;
784 char *newcon;
785 u32 len;
787 length = task_has_security(current, SECURITY__COMPUTE_MEMBER);
788 if (length)
789 return length;
791 length = -ENOMEM;
792 scon = kzalloc(size+1, GFP_KERNEL);
793 if (!scon)
794 return length;
796 tcon = kzalloc(size+1, GFP_KERNEL);
797 if (!tcon)
798 goto out;
800 length = -EINVAL;
801 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
802 goto out2;
804 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
805 if (length < 0)
806 goto out2;
807 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
808 if (length < 0)
809 goto out2;
811 length = security_member_sid(ssid, tsid, tclass, &newsid);
812 if (length < 0)
813 goto out2;
815 length = security_sid_to_context(newsid, &newcon, &len);
816 if (length < 0)
817 goto out2;
819 if (len > SIMPLE_TRANSACTION_LIMIT) {
820 printk(KERN_ERR "%s: context size (%u) exceeds payload "
821 "max\n", __FUNCTION__, len);
822 length = -ERANGE;
823 goto out3;
826 memcpy(buf, newcon, len);
827 length = len;
828 out3:
829 kfree(newcon);
830 out2:
831 kfree(tcon);
832 out:
833 kfree(scon);
834 return length;
837 static struct inode *sel_make_inode(struct super_block *sb, int mode)
839 struct inode *ret = new_inode(sb);
841 if (ret) {
842 ret->i_mode = mode;
843 ret->i_uid = ret->i_gid = 0;
844 ret->i_blocks = 0;
845 ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
847 return ret;
850 static ssize_t sel_read_bool(struct file *filep, char __user *buf,
851 size_t count, loff_t *ppos)
853 char *page = NULL;
854 ssize_t length;
855 ssize_t ret;
856 int cur_enforcing;
857 struct inode *inode = filep->f_path.dentry->d_inode;
858 unsigned index = inode->i_ino & SEL_INO_MASK;
859 const char *name = filep->f_path.dentry->d_name.name;
861 mutex_lock(&sel_mutex);
863 if (index >= bool_num || strcmp(name, bool_pending_names[index])) {
864 ret = -EINVAL;
865 goto out;
868 if (count > PAGE_SIZE) {
869 ret = -EINVAL;
870 goto out;
872 if (!(page = (char*)get_zeroed_page(GFP_KERNEL))) {
873 ret = -ENOMEM;
874 goto out;
877 cur_enforcing = security_get_bool_value(index);
878 if (cur_enforcing < 0) {
879 ret = cur_enforcing;
880 goto out;
882 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
883 bool_pending_values[index]);
884 ret = simple_read_from_buffer(buf, count, ppos, page, length);
885 out:
886 mutex_unlock(&sel_mutex);
887 if (page)
888 free_page((unsigned long)page);
889 return ret;
892 static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
893 size_t count, loff_t *ppos)
895 char *page = NULL;
896 ssize_t length;
897 int new_value;
898 struct inode *inode = filep->f_path.dentry->d_inode;
899 unsigned index = inode->i_ino & SEL_INO_MASK;
900 const char *name = filep->f_path.dentry->d_name.name;
902 mutex_lock(&sel_mutex);
904 length = task_has_security(current, SECURITY__SETBOOL);
905 if (length)
906 goto out;
908 if (index >= bool_num || strcmp(name, bool_pending_names[index])) {
909 length = -EINVAL;
910 goto out;
913 if (count >= PAGE_SIZE) {
914 length = -ENOMEM;
915 goto out;
918 if (*ppos != 0) {
919 /* No partial writes. */
920 length = -EINVAL;
921 goto out;
923 page = (char*)get_zeroed_page(GFP_KERNEL);
924 if (!page) {
925 length = -ENOMEM;
926 goto out;
929 length = -EFAULT;
930 if (copy_from_user(page, buf, count))
931 goto out;
933 length = -EINVAL;
934 if (sscanf(page, "%d", &new_value) != 1)
935 goto out;
937 if (new_value)
938 new_value = 1;
940 bool_pending_values[index] = new_value;
941 length = count;
943 out:
944 mutex_unlock(&sel_mutex);
945 if (page)
946 free_page((unsigned long) page);
947 return length;
950 static const struct file_operations sel_bool_ops = {
951 .read = sel_read_bool,
952 .write = sel_write_bool,
955 static ssize_t sel_commit_bools_write(struct file *filep,
956 const char __user *buf,
957 size_t count, loff_t *ppos)
959 char *page = NULL;
960 ssize_t length;
961 int new_value;
963 mutex_lock(&sel_mutex);
965 length = task_has_security(current, SECURITY__SETBOOL);
966 if (length)
967 goto out;
969 if (count >= PAGE_SIZE) {
970 length = -ENOMEM;
971 goto out;
973 if (*ppos != 0) {
974 /* No partial writes. */
975 goto out;
977 page = (char*)get_zeroed_page(GFP_KERNEL);
978 if (!page) {
979 length = -ENOMEM;
980 goto out;
983 length = -EFAULT;
984 if (copy_from_user(page, buf, count))
985 goto out;
987 length = -EINVAL;
988 if (sscanf(page, "%d", &new_value) != 1)
989 goto out;
991 if (new_value && bool_pending_values) {
992 security_set_bools(bool_num, bool_pending_values);
995 length = count;
997 out:
998 mutex_unlock(&sel_mutex);
999 if (page)
1000 free_page((unsigned long) page);
1001 return length;
1004 static const struct file_operations sel_commit_bools_ops = {
1005 .write = sel_commit_bools_write,
1008 static void sel_remove_entries(struct dentry *de)
1010 struct list_head *node;
1012 spin_lock(&dcache_lock);
1013 node = de->d_subdirs.next;
1014 while (node != &de->d_subdirs) {
1015 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
1016 list_del_init(node);
1018 if (d->d_inode) {
1019 d = dget_locked(d);
1020 spin_unlock(&dcache_lock);
1021 d_delete(d);
1022 simple_unlink(de->d_inode, d);
1023 dput(d);
1024 spin_lock(&dcache_lock);
1026 node = de->d_subdirs.next;
1029 spin_unlock(&dcache_lock);
1032 #define BOOL_DIR_NAME "booleans"
1034 static int sel_make_bools(void)
1036 int i, ret = 0;
1037 ssize_t len;
1038 struct dentry *dentry = NULL;
1039 struct dentry *dir = bool_dir;
1040 struct inode *inode = NULL;
1041 struct inode_security_struct *isec;
1042 char **names = NULL, *page;
1043 int num;
1044 int *values = NULL;
1045 u32 sid;
1047 /* remove any existing files */
1048 kfree(bool_pending_names);
1049 kfree(bool_pending_values);
1050 bool_pending_names = NULL;
1051 bool_pending_values = NULL;
1053 sel_remove_entries(dir);
1055 if (!(page = (char*)get_zeroed_page(GFP_KERNEL)))
1056 return -ENOMEM;
1058 ret = security_get_bools(&num, &names, &values);
1059 if (ret != 0)
1060 goto out;
1062 for (i = 0; i < num; i++) {
1063 dentry = d_alloc_name(dir, names[i]);
1064 if (!dentry) {
1065 ret = -ENOMEM;
1066 goto err;
1068 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1069 if (!inode) {
1070 ret = -ENOMEM;
1071 goto err;
1074 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
1075 if (len < 0) {
1076 ret = -EINVAL;
1077 goto err;
1078 } else if (len >= PAGE_SIZE) {
1079 ret = -ENAMETOOLONG;
1080 goto err;
1082 isec = (struct inode_security_struct*)inode->i_security;
1083 if ((ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid)))
1084 goto err;
1085 isec->sid = sid;
1086 isec->initialized = 1;
1087 inode->i_fop = &sel_bool_ops;
1088 inode->i_ino = i|SEL_BOOL_INO_OFFSET;
1089 d_add(dentry, inode);
1091 bool_num = num;
1092 bool_pending_names = names;
1093 bool_pending_values = values;
1094 out:
1095 free_page((unsigned long)page);
1096 return ret;
1097 err:
1098 if (names) {
1099 for (i = 0; i < num; i++)
1100 kfree(names[i]);
1101 kfree(names);
1103 kfree(values);
1104 sel_remove_entries(dir);
1105 ret = -ENOMEM;
1106 goto out;
1109 #define NULL_FILE_NAME "null"
1111 struct dentry *selinux_null = NULL;
1113 static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1114 size_t count, loff_t *ppos)
1116 char tmpbuf[TMPBUFLEN];
1117 ssize_t length;
1119 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
1120 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1123 static ssize_t sel_write_avc_cache_threshold(struct file * file,
1124 const char __user * buf,
1125 size_t count, loff_t *ppos)
1128 char *page;
1129 ssize_t ret;
1130 int new_value;
1132 if (count >= PAGE_SIZE) {
1133 ret = -ENOMEM;
1134 goto out;
1137 if (*ppos != 0) {
1138 /* No partial writes. */
1139 ret = -EINVAL;
1140 goto out;
1143 page = (char*)get_zeroed_page(GFP_KERNEL);
1144 if (!page) {
1145 ret = -ENOMEM;
1146 goto out;
1149 if (copy_from_user(page, buf, count)) {
1150 ret = -EFAULT;
1151 goto out_free;
1154 if (sscanf(page, "%u", &new_value) != 1) {
1155 ret = -EINVAL;
1156 goto out;
1159 if (new_value != avc_cache_threshold) {
1160 ret = task_has_security(current, SECURITY__SETSECPARAM);
1161 if (ret)
1162 goto out_free;
1163 avc_cache_threshold = new_value;
1165 ret = count;
1166 out_free:
1167 free_page((unsigned long)page);
1168 out:
1169 return ret;
1172 static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1173 size_t count, loff_t *ppos)
1175 char *page;
1176 ssize_t ret = 0;
1178 page = (char *)__get_free_page(GFP_KERNEL);
1179 if (!page) {
1180 ret = -ENOMEM;
1181 goto out;
1183 ret = avc_get_hash_stats(page);
1184 if (ret >= 0)
1185 ret = simple_read_from_buffer(buf, count, ppos, page, ret);
1186 free_page((unsigned long)page);
1187 out:
1188 return ret;
1191 static const struct file_operations sel_avc_cache_threshold_ops = {
1192 .read = sel_read_avc_cache_threshold,
1193 .write = sel_write_avc_cache_threshold,
1196 static const struct file_operations sel_avc_hash_stats_ops = {
1197 .read = sel_read_avc_hash_stats,
1200 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1201 static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1203 int cpu;
1205 for (cpu = *idx; cpu < NR_CPUS; ++cpu) {
1206 if (!cpu_possible(cpu))
1207 continue;
1208 *idx = cpu + 1;
1209 return &per_cpu(avc_cache_stats, cpu);
1211 return NULL;
1214 static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1216 loff_t n = *pos - 1;
1218 if (*pos == 0)
1219 return SEQ_START_TOKEN;
1221 return sel_avc_get_stat_idx(&n);
1224 static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1226 return sel_avc_get_stat_idx(pos);
1229 static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1231 struct avc_cache_stats *st = v;
1233 if (v == SEQ_START_TOKEN)
1234 seq_printf(seq, "lookups hits misses allocations reclaims "
1235 "frees\n");
1236 else
1237 seq_printf(seq, "%u %u %u %u %u %u\n", st->lookups,
1238 st->hits, st->misses, st->allocations,
1239 st->reclaims, st->frees);
1240 return 0;
1243 static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1246 static const struct seq_operations sel_avc_cache_stats_seq_ops = {
1247 .start = sel_avc_stats_seq_start,
1248 .next = sel_avc_stats_seq_next,
1249 .show = sel_avc_stats_seq_show,
1250 .stop = sel_avc_stats_seq_stop,
1253 static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1255 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1258 static const struct file_operations sel_avc_cache_stats_ops = {
1259 .open = sel_open_avc_cache_stats,
1260 .read = seq_read,
1261 .llseek = seq_lseek,
1262 .release = seq_release,
1264 #endif
1266 static int sel_make_avc_files(struct dentry *dir)
1268 int i, ret = 0;
1269 static struct tree_descr files[] = {
1270 { "cache_threshold",
1271 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1272 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1273 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1274 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1275 #endif
1278 for (i = 0; i < ARRAY_SIZE(files); i++) {
1279 struct inode *inode;
1280 struct dentry *dentry;
1282 dentry = d_alloc_name(dir, files[i].name);
1283 if (!dentry) {
1284 ret = -ENOMEM;
1285 goto out;
1288 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1289 if (!inode) {
1290 ret = -ENOMEM;
1291 goto out;
1293 inode->i_fop = files[i].ops;
1294 inode->i_ino = ++sel_last_ino;
1295 d_add(dentry, inode);
1297 out:
1298 return ret;
1301 static ssize_t sel_read_initcon(struct file * file, char __user *buf,
1302 size_t count, loff_t *ppos)
1304 struct inode *inode;
1305 char *con;
1306 u32 sid, len;
1307 ssize_t ret;
1309 inode = file->f_path.dentry->d_inode;
1310 sid = inode->i_ino&SEL_INO_MASK;
1311 ret = security_sid_to_context(sid, &con, &len);
1312 if (ret < 0)
1313 return ret;
1315 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1316 kfree(con);
1317 return ret;
1320 static const struct file_operations sel_initcon_ops = {
1321 .read = sel_read_initcon,
1324 static int sel_make_initcon_files(struct dentry *dir)
1326 int i, ret = 0;
1328 for (i = 1; i <= SECINITSID_NUM; i++) {
1329 struct inode *inode;
1330 struct dentry *dentry;
1331 dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
1332 if (!dentry) {
1333 ret = -ENOMEM;
1334 goto out;
1337 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1338 if (!inode) {
1339 ret = -ENOMEM;
1340 goto out;
1342 inode->i_fop = &sel_initcon_ops;
1343 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1344 d_add(dentry, inode);
1346 out:
1347 return ret;
1350 static inline unsigned int sel_div(unsigned long a, unsigned long b)
1352 return a / b - (a % b < 0);
1355 static inline unsigned long sel_class_to_ino(u16 class)
1357 return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1360 static inline u16 sel_ino_to_class(unsigned long ino)
1362 return sel_div(ino & SEL_INO_MASK, SEL_VEC_MAX + 1);
1365 static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1367 return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1370 static inline u32 sel_ino_to_perm(unsigned long ino)
1372 return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1375 static ssize_t sel_read_class(struct file * file, char __user *buf,
1376 size_t count, loff_t *ppos)
1378 ssize_t rc, len;
1379 char *page;
1380 unsigned long ino = file->f_path.dentry->d_inode->i_ino;
1382 page = (char *)__get_free_page(GFP_KERNEL);
1383 if (!page) {
1384 rc = -ENOMEM;
1385 goto out;
1388 len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_class(ino));
1389 rc = simple_read_from_buffer(buf, count, ppos, page, len);
1390 free_page((unsigned long)page);
1391 out:
1392 return rc;
1395 static const struct file_operations sel_class_ops = {
1396 .read = sel_read_class,
1399 static ssize_t sel_read_perm(struct file * file, char __user *buf,
1400 size_t count, loff_t *ppos)
1402 ssize_t rc, len;
1403 char *page;
1404 unsigned long ino = file->f_path.dentry->d_inode->i_ino;
1406 page = (char *)__get_free_page(GFP_KERNEL);
1407 if (!page) {
1408 rc = -ENOMEM;
1409 goto out;
1412 len = snprintf(page, PAGE_SIZE,"%d", sel_ino_to_perm(ino));
1413 rc = simple_read_from_buffer(buf, count, ppos, page, len);
1414 free_page((unsigned long)page);
1415 out:
1416 return rc;
1419 static const struct file_operations sel_perm_ops = {
1420 .read = sel_read_perm,
1423 static ssize_t sel_read_policycap(struct file *file, char __user *buf,
1424 size_t count, loff_t *ppos)
1426 int value;
1427 char tmpbuf[TMPBUFLEN];
1428 ssize_t length;
1429 unsigned long i_ino = file->f_path.dentry->d_inode->i_ino;
1431 value = security_policycap_supported(i_ino & SEL_INO_MASK);
1432 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
1434 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1437 static const struct file_operations sel_policycap_ops = {
1438 .read = sel_read_policycap,
1441 static int sel_make_perm_files(char *objclass, int classvalue,
1442 struct dentry *dir)
1444 int i, rc = 0, nperms;
1445 char **perms;
1447 rc = security_get_permissions(objclass, &perms, &nperms);
1448 if (rc)
1449 goto out;
1451 for (i = 0; i < nperms; i++) {
1452 struct inode *inode;
1453 struct dentry *dentry;
1455 dentry = d_alloc_name(dir, perms[i]);
1456 if (!dentry) {
1457 rc = -ENOMEM;
1458 goto out1;
1461 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1462 if (!inode) {
1463 rc = -ENOMEM;
1464 goto out1;
1466 inode->i_fop = &sel_perm_ops;
1467 /* i+1 since perm values are 1-indexed */
1468 inode->i_ino = sel_perm_to_ino(classvalue, i+1);
1469 d_add(dentry, inode);
1472 out1:
1473 for (i = 0; i < nperms; i++)
1474 kfree(perms[i]);
1475 kfree(perms);
1476 out:
1477 return rc;
1480 static int sel_make_class_dir_entries(char *classname, int index,
1481 struct dentry *dir)
1483 struct dentry *dentry = NULL;
1484 struct inode *inode = NULL;
1485 int rc;
1487 dentry = d_alloc_name(dir, "index");
1488 if (!dentry) {
1489 rc = -ENOMEM;
1490 goto out;
1493 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1494 if (!inode) {
1495 rc = -ENOMEM;
1496 goto out;
1499 inode->i_fop = &sel_class_ops;
1500 inode->i_ino = sel_class_to_ino(index);
1501 d_add(dentry, inode);
1503 dentry = d_alloc_name(dir, "perms");
1504 if (!dentry) {
1505 rc = -ENOMEM;
1506 goto out;
1509 rc = sel_make_dir(dir->d_inode, dentry, &last_class_ino);
1510 if (rc)
1511 goto out;
1513 rc = sel_make_perm_files(classname, index, dentry);
1515 out:
1516 return rc;
1519 static void sel_remove_classes(void)
1521 struct list_head *class_node;
1523 list_for_each(class_node, &class_dir->d_subdirs) {
1524 struct dentry *class_subdir = list_entry(class_node,
1525 struct dentry, d_u.d_child);
1526 struct list_head *class_subdir_node;
1528 list_for_each(class_subdir_node, &class_subdir->d_subdirs) {
1529 struct dentry *d = list_entry(class_subdir_node,
1530 struct dentry, d_u.d_child);
1532 if (d->d_inode)
1533 if (d->d_inode->i_mode & S_IFDIR)
1534 sel_remove_entries(d);
1537 sel_remove_entries(class_subdir);
1540 sel_remove_entries(class_dir);
1543 static int sel_make_classes(void)
1545 int rc = 0, nclasses, i;
1546 char **classes;
1548 /* delete any existing entries */
1549 sel_remove_classes();
1551 rc = security_get_classes(&classes, &nclasses);
1552 if (rc < 0)
1553 goto out;
1555 /* +2 since classes are 1-indexed */
1556 last_class_ino = sel_class_to_ino(nclasses+2);
1558 for (i = 0; i < nclasses; i++) {
1559 struct dentry *class_name_dir;
1561 class_name_dir = d_alloc_name(class_dir, classes[i]);
1562 if (!class_name_dir) {
1563 rc = -ENOMEM;
1564 goto out1;
1567 rc = sel_make_dir(class_dir->d_inode, class_name_dir,
1568 &last_class_ino);
1569 if (rc)
1570 goto out1;
1572 /* i+1 since class values are 1-indexed */
1573 rc = sel_make_class_dir_entries(classes[i], i+1,
1574 class_name_dir);
1575 if (rc)
1576 goto out1;
1579 out1:
1580 for (i = 0; i < nclasses; i++)
1581 kfree(classes[i]);
1582 kfree(classes);
1583 out:
1584 return rc;
1587 static int sel_make_policycap(void)
1589 unsigned int iter;
1590 struct dentry *dentry = NULL;
1591 struct inode *inode = NULL;
1593 sel_remove_entries(policycap_dir);
1595 for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
1596 if (iter < ARRAY_SIZE(policycap_names))
1597 dentry = d_alloc_name(policycap_dir,
1598 policycap_names[iter]);
1599 else
1600 dentry = d_alloc_name(policycap_dir, "unknown");
1602 if (dentry == NULL)
1603 return -ENOMEM;
1605 inode = sel_make_inode(policycap_dir->d_sb, S_IFREG | S_IRUGO);
1606 if (inode == NULL)
1607 return -ENOMEM;
1609 inode->i_fop = &sel_policycap_ops;
1610 inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
1611 d_add(dentry, inode);
1614 return 0;
1617 static int sel_make_dir(struct inode *dir, struct dentry *dentry,
1618 unsigned long *ino)
1620 int ret = 0;
1621 struct inode *inode;
1623 inode = sel_make_inode(dir->i_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1624 if (!inode) {
1625 ret = -ENOMEM;
1626 goto out;
1628 inode->i_op = &simple_dir_inode_operations;
1629 inode->i_fop = &simple_dir_operations;
1630 inode->i_ino = ++(*ino);
1631 /* directory inodes start off with i_nlink == 2 (for "." entry) */
1632 inc_nlink(inode);
1633 d_add(dentry, inode);
1634 /* bump link count on parent directory, too */
1635 inc_nlink(dir);
1636 out:
1637 return ret;
1640 static int sel_fill_super(struct super_block * sb, void * data, int silent)
1642 int ret;
1643 struct dentry *dentry;
1644 struct inode *inode, *root_inode;
1645 struct inode_security_struct *isec;
1647 static struct tree_descr selinux_files[] = {
1648 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1649 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
1650 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
1651 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1652 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1653 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1654 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1655 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1656 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1657 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1658 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1659 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1660 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
1661 [SEL_COMPAT_NET] = {"compat_net", &sel_compat_net_ops, S_IRUGO|S_IWUSR},
1662 [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
1663 [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
1664 /* last one */ {""}
1666 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1667 if (ret)
1668 goto err;
1670 root_inode = sb->s_root->d_inode;
1672 dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME);
1673 if (!dentry) {
1674 ret = -ENOMEM;
1675 goto err;
1678 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1679 if (ret)
1680 goto err;
1682 bool_dir = dentry;
1684 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
1685 if (!dentry) {
1686 ret = -ENOMEM;
1687 goto err;
1690 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
1691 if (!inode) {
1692 ret = -ENOMEM;
1693 goto err;
1695 inode->i_ino = ++sel_last_ino;
1696 isec = (struct inode_security_struct*)inode->i_security;
1697 isec->sid = SECINITSID_DEVNULL;
1698 isec->sclass = SECCLASS_CHR_FILE;
1699 isec->initialized = 1;
1701 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1702 d_add(dentry, inode);
1703 selinux_null = dentry;
1705 dentry = d_alloc_name(sb->s_root, "avc");
1706 if (!dentry) {
1707 ret = -ENOMEM;
1708 goto err;
1711 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1712 if (ret)
1713 goto err;
1715 ret = sel_make_avc_files(dentry);
1716 if (ret)
1717 goto err;
1719 dentry = d_alloc_name(sb->s_root, "initial_contexts");
1720 if (!dentry) {
1721 ret = -ENOMEM;
1722 goto err;
1725 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1726 if (ret)
1727 goto err;
1729 ret = sel_make_initcon_files(dentry);
1730 if (ret)
1731 goto err;
1733 dentry = d_alloc_name(sb->s_root, "class");
1734 if (!dentry) {
1735 ret = -ENOMEM;
1736 goto err;
1739 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1740 if (ret)
1741 goto err;
1743 class_dir = dentry;
1745 dentry = d_alloc_name(sb->s_root, "policy_capabilities");
1746 if (!dentry) {
1747 ret = -ENOMEM;
1748 goto err;
1751 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1752 if (ret)
1753 goto err;
1755 policycap_dir = dentry;
1757 out:
1758 return ret;
1759 err:
1760 printk(KERN_ERR "%s: failed while creating inodes\n", __FUNCTION__);
1761 goto out;
1764 static int sel_get_sb(struct file_system_type *fs_type,
1765 int flags, const char *dev_name, void *data,
1766 struct vfsmount *mnt)
1768 return get_sb_single(fs_type, flags, data, sel_fill_super, mnt);
1771 static struct file_system_type sel_fs_type = {
1772 .name = "selinuxfs",
1773 .get_sb = sel_get_sb,
1774 .kill_sb = kill_litter_super,
1777 struct vfsmount *selinuxfs_mount;
1779 static int __init init_sel_fs(void)
1781 int err;
1783 if (!selinux_enabled)
1784 return 0;
1785 err = register_filesystem(&sel_fs_type);
1786 if (!err) {
1787 selinuxfs_mount = kern_mount(&sel_fs_type);
1788 if (IS_ERR(selinuxfs_mount)) {
1789 printk(KERN_ERR "selinuxfs: could not mount!\n");
1790 err = PTR_ERR(selinuxfs_mount);
1791 selinuxfs_mount = NULL;
1794 return err;
1797 __initcall(init_sel_fs);
1799 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
1800 void exit_sel_fs(void)
1802 unregister_filesystem(&sel_fs_type);
1804 #endif