initial commit with v2.6.9
[linux-2.6.9-moxart.git] / security / selinux / selinuxfs.c
blob57c2c8eaf060f720870ba234a37df637aa4d332e
1 /* Updated: Karl MacMillan <kmacmillan@tresys.com>
3 * Added conditional policy language extensions
5 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, version 2.
9 */
11 #include <linux/config.h>
12 #include <linux/kernel.h>
13 #include <linux/pagemap.h>
14 #include <linux/slab.h>
15 #include <linux/vmalloc.h>
16 #include <linux/fs.h>
17 #include <linux/init.h>
18 #include <linux/string.h>
19 #include <linux/security.h>
20 #include <linux/major.h>
21 #include <asm/uaccess.h>
22 #include <asm/semaphore.h>
24 /* selinuxfs pseudo filesystem for exporting the security policy API.
25 Based on the proc code and the fs/nfsd/nfsctl.c code. */
27 #include "flask.h"
28 #include "avc.h"
29 #include "avc_ss.h"
30 #include "security.h"
31 #include "objsec.h"
32 #include "conditional.h"
34 static DECLARE_MUTEX(sel_sem);
36 /* global data for booleans */
37 static struct dentry *bool_dir = NULL;
38 static int bool_num = 0;
39 static int *bool_pending_values = NULL;
41 extern void selnl_notify_setenforce(int val);
43 /* Check whether a task is allowed to use a security operation. */
44 int task_has_security(struct task_struct *tsk,
45 u32 perms)
47 struct task_security_struct *tsec;
49 tsec = tsk->security;
50 if (!tsec)
51 return -EACCES;
53 return avc_has_perm(tsec->sid, SECINITSID_SECURITY,
54 SECCLASS_SECURITY, perms, NULL, NULL);
57 enum sel_inos {
58 SEL_ROOT_INO = 2,
59 SEL_LOAD, /* load policy */
60 SEL_ENFORCE, /* get or set enforcing status */
61 SEL_CONTEXT, /* validate context */
62 SEL_ACCESS, /* compute access decision */
63 SEL_CREATE, /* compute create labeling decision */
64 SEL_RELABEL, /* compute relabeling decision */
65 SEL_USER, /* compute reachable user contexts */
66 SEL_POLICYVERS, /* return policy version for this kernel */
67 SEL_COMMIT_BOOLS, /* commit new boolean values */
68 SEL_MLS, /* return if MLS policy is enabled */
69 SEL_DISABLE /* disable SELinux until next reboot */
72 #define TMPBUFLEN 12
73 static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
74 size_t count, loff_t *ppos)
76 char tmpbuf[TMPBUFLEN];
77 ssize_t length;
79 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing);
80 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
83 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
84 static ssize_t sel_write_enforce(struct file * file, const char __user * buf,
85 size_t count, loff_t *ppos)
88 char *page;
89 ssize_t length;
90 int new_value;
92 if (count < 0 || count >= PAGE_SIZE)
93 return -ENOMEM;
94 if (*ppos != 0) {
95 /* No partial writes. */
96 return -EINVAL;
98 page = (char*)get_zeroed_page(GFP_KERNEL);
99 if (!page)
100 return -ENOMEM;
101 length = -EFAULT;
102 if (copy_from_user(page, buf, count))
103 goto out;
105 length = -EINVAL;
106 if (sscanf(page, "%d", &new_value) != 1)
107 goto out;
109 if (new_value != selinux_enforcing) {
110 length = task_has_security(current, SECURITY__SETENFORCE);
111 if (length)
112 goto out;
113 selinux_enforcing = new_value;
114 if (selinux_enforcing)
115 avc_ss_reset(0);
116 selnl_notify_setenforce(selinux_enforcing);
118 length = count;
119 out:
120 free_page((unsigned long) page);
121 return length;
123 #else
124 #define sel_write_enforce NULL
125 #endif
127 static struct file_operations sel_enforce_ops = {
128 .read = sel_read_enforce,
129 .write = sel_write_enforce,
132 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
133 static ssize_t sel_write_disable(struct file * file, const char __user * buf,
134 size_t count, loff_t *ppos)
137 char *page;
138 ssize_t length;
139 int new_value;
140 extern int selinux_disable(void);
142 if (count < 0 || count >= PAGE_SIZE)
143 return -ENOMEM;
144 if (*ppos != 0) {
145 /* No partial writes. */
146 return -EINVAL;
148 page = (char*)get_zeroed_page(GFP_KERNEL);
149 if (!page)
150 return -ENOMEM;
151 length = -EFAULT;
152 if (copy_from_user(page, buf, count))
153 goto out;
155 length = -EINVAL;
156 if (sscanf(page, "%d", &new_value) != 1)
157 goto out;
159 if (new_value) {
160 length = selinux_disable();
161 if (length < 0)
162 goto out;
165 length = count;
166 out:
167 free_page((unsigned long) page);
168 return length;
170 #else
171 #define sel_write_disable NULL
172 #endif
174 static struct file_operations sel_disable_ops = {
175 .write = sel_write_disable,
178 static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
179 size_t count, loff_t *ppos)
181 char tmpbuf[TMPBUFLEN];
182 ssize_t length;
184 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
185 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
188 static struct file_operations sel_policyvers_ops = {
189 .read = sel_read_policyvers,
192 /* declaration for sel_write_load */
193 static int sel_make_bools(void);
195 static ssize_t sel_read_mls(struct file *filp, char __user *buf,
196 size_t count, loff_t *ppos)
198 char tmpbuf[TMPBUFLEN];
199 ssize_t length;
201 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_mls_enabled);
202 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
205 static struct file_operations sel_mls_ops = {
206 .read = sel_read_mls,
209 static ssize_t sel_write_load(struct file * file, const char __user * buf,
210 size_t count, loff_t *ppos)
213 int ret;
214 ssize_t length;
215 void *data = NULL;
217 down(&sel_sem);
219 length = task_has_security(current, SECURITY__LOAD_POLICY);
220 if (length)
221 goto out;
223 if (*ppos != 0) {
224 /* No partial writes. */
225 length = -EINVAL;
226 goto out;
229 if ((count < 0) || (count > 64 * 1024 * 1024)
230 || (data = vmalloc(count)) == NULL) {
231 length = -ENOMEM;
232 goto out;
235 length = -EFAULT;
236 if (copy_from_user(data, buf, count) != 0)
237 goto out;
239 length = security_load_policy(data, count);
240 if (length)
241 goto out;
243 ret = sel_make_bools();
244 if (ret)
245 length = ret;
246 else
247 length = count;
248 out:
249 up(&sel_sem);
250 vfree(data);
251 return length;
254 static struct file_operations sel_load_ops = {
255 .write = sel_write_load,
259 static ssize_t sel_write_context(struct file * file, const char __user * buf,
260 size_t count, loff_t *ppos)
263 char *page;
264 u32 sid;
265 ssize_t length;
267 length = task_has_security(current, SECURITY__CHECK_CONTEXT);
268 if (length)
269 return length;
271 if (count < 0 || count >= PAGE_SIZE)
272 return -ENOMEM;
273 if (*ppos != 0) {
274 /* No partial writes. */
275 return -EINVAL;
277 page = (char*)get_zeroed_page(GFP_KERNEL);
278 if (!page)
279 return -ENOMEM;
280 length = -EFAULT;
281 if (copy_from_user(page, buf, count))
282 goto out;
284 length = security_context_to_sid(page, count, &sid);
285 if (length < 0)
286 goto out;
288 length = count;
289 out:
290 free_page((unsigned long) page);
291 return length;
294 static struct file_operations sel_context_ops = {
295 .write = sel_write_context,
300 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
302 static ssize_t sel_write_access(struct file * file, char *buf, size_t size);
303 static ssize_t sel_write_create(struct file * file, char *buf, size_t size);
304 static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size);
305 static ssize_t sel_write_user(struct file * file, char *buf, size_t size);
307 static ssize_t (*write_op[])(struct file *, char *, size_t) = {
308 [SEL_ACCESS] = sel_write_access,
309 [SEL_CREATE] = sel_write_create,
310 [SEL_RELABEL] = sel_write_relabel,
311 [SEL_USER] = sel_write_user,
314 static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
316 ino_t ino = file->f_dentry->d_inode->i_ino;
317 char *data;
318 ssize_t rv;
320 if (ino >= sizeof(write_op)/sizeof(write_op[0]) || !write_op[ino])
321 return -EINVAL;
323 data = simple_transaction_get(file, buf, size);
324 if (IS_ERR(data))
325 return PTR_ERR(data);
327 rv = write_op[ino](file, data, size);
328 if (rv>0) {
329 simple_transaction_set(file, rv);
330 rv = size;
332 return rv;
335 static struct file_operations transaction_ops = {
336 .write = selinux_transaction_write,
337 .read = simple_transaction_read,
338 .release = simple_transaction_release,
342 * payload - write methods
343 * If the method has a response, the response should be put in buf,
344 * and the length returned. Otherwise return 0 or and -error.
347 static ssize_t sel_write_access(struct file * file, char *buf, size_t size)
349 char *scon, *tcon;
350 u32 ssid, tsid;
351 u16 tclass;
352 u32 req;
353 struct av_decision avd;
354 ssize_t length;
356 length = task_has_security(current, SECURITY__COMPUTE_AV);
357 if (length)
358 return length;
360 length = -ENOMEM;
361 scon = kmalloc(size+1, GFP_KERNEL);
362 if (!scon)
363 return length;
364 memset(scon, 0, size+1);
366 tcon = kmalloc(size+1, GFP_KERNEL);
367 if (!tcon)
368 goto out;
369 memset(tcon, 0, size+1);
371 length = -EINVAL;
372 if (sscanf(buf, "%s %s %hu %x", scon, tcon, &tclass, &req) != 4)
373 goto out2;
375 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
376 if (length < 0)
377 goto out2;
378 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
379 if (length < 0)
380 goto out2;
382 length = security_compute_av(ssid, tsid, tclass, req, &avd);
383 if (length < 0)
384 goto out2;
386 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
387 "%x %x %x %x %u",
388 avd.allowed, avd.decided,
389 avd.auditallow, avd.auditdeny,
390 avd.seqno);
391 out2:
392 kfree(tcon);
393 out:
394 kfree(scon);
395 return length;
398 static ssize_t sel_write_create(struct file * file, char *buf, size_t size)
400 char *scon, *tcon;
401 u32 ssid, tsid, newsid;
402 u16 tclass;
403 ssize_t length;
404 char *newcon;
405 u32 len;
407 length = task_has_security(current, SECURITY__COMPUTE_CREATE);
408 if (length)
409 return length;
411 length = -ENOMEM;
412 scon = kmalloc(size+1, GFP_KERNEL);
413 if (!scon)
414 return length;
415 memset(scon, 0, size+1);
417 tcon = kmalloc(size+1, GFP_KERNEL);
418 if (!tcon)
419 goto out;
420 memset(tcon, 0, size+1);
422 length = -EINVAL;
423 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
424 goto out2;
426 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
427 if (length < 0)
428 goto out2;
429 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
430 if (length < 0)
431 goto out2;
433 length = security_transition_sid(ssid, tsid, tclass, &newsid);
434 if (length < 0)
435 goto out2;
437 length = security_sid_to_context(newsid, &newcon, &len);
438 if (length < 0)
439 goto out2;
441 if (len > SIMPLE_TRANSACTION_LIMIT) {
442 printk(KERN_ERR "%s: context size (%u) exceeds payload "
443 "max\n", __FUNCTION__, len);
444 length = -ERANGE;
445 goto out3;
448 memcpy(buf, newcon, len);
449 length = len;
450 out3:
451 kfree(newcon);
452 out2:
453 kfree(tcon);
454 out:
455 kfree(scon);
456 return length;
459 static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size)
461 char *scon, *tcon;
462 u32 ssid, tsid, newsid;
463 u16 tclass;
464 ssize_t length;
465 char *newcon;
466 u32 len;
468 length = task_has_security(current, SECURITY__COMPUTE_RELABEL);
469 if (length)
470 return length;
472 length = -ENOMEM;
473 scon = kmalloc(size+1, GFP_KERNEL);
474 if (!scon)
475 return length;
476 memset(scon, 0, size+1);
478 tcon = kmalloc(size+1, GFP_KERNEL);
479 if (!tcon)
480 goto out;
481 memset(tcon, 0, size+1);
483 length = -EINVAL;
484 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
485 goto out2;
487 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
488 if (length < 0)
489 goto out2;
490 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
491 if (length < 0)
492 goto out2;
494 length = security_change_sid(ssid, tsid, tclass, &newsid);
495 if (length < 0)
496 goto out2;
498 length = security_sid_to_context(newsid, &newcon, &len);
499 if (length < 0)
500 goto out2;
502 if (len > SIMPLE_TRANSACTION_LIMIT) {
503 length = -ERANGE;
504 goto out3;
507 memcpy(buf, newcon, len);
508 length = len;
509 out3:
510 kfree(newcon);
511 out2:
512 kfree(tcon);
513 out:
514 kfree(scon);
515 return length;
518 static ssize_t sel_write_user(struct file * file, char *buf, size_t size)
520 char *con, *user, *ptr;
521 u32 sid, *sids;
522 ssize_t length;
523 char *newcon;
524 int i, rc;
525 u32 len, nsids;
527 length = task_has_security(current, SECURITY__COMPUTE_USER);
528 if (length)
529 return length;
531 length = -ENOMEM;
532 con = kmalloc(size+1, GFP_KERNEL);
533 if (!con)
534 return length;
535 memset(con, 0, size+1);
537 user = kmalloc(size+1, GFP_KERNEL);
538 if (!user)
539 goto out;
540 memset(user, 0, size+1);
542 length = -EINVAL;
543 if (sscanf(buf, "%s %s", con, user) != 2)
544 goto out2;
546 length = security_context_to_sid(con, strlen(con)+1, &sid);
547 if (length < 0)
548 goto out2;
550 length = security_get_user_sids(sid, user, &sids, &nsids);
551 if (length < 0)
552 goto out2;
554 length = sprintf(buf, "%u", nsids) + 1;
555 ptr = buf + length;
556 for (i = 0; i < nsids; i++) {
557 rc = security_sid_to_context(sids[i], &newcon, &len);
558 if (rc) {
559 length = rc;
560 goto out3;
562 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
563 kfree(newcon);
564 length = -ERANGE;
565 goto out3;
567 memcpy(ptr, newcon, len);
568 kfree(newcon);
569 ptr += len;
570 length += len;
572 out3:
573 kfree(sids);
574 out2:
575 kfree(user);
576 out:
577 kfree(con);
578 return length;
581 static struct inode *sel_make_inode(struct super_block *sb, int mode)
583 struct inode *ret = new_inode(sb);
585 if (ret) {
586 ret->i_mode = mode;
587 ret->i_uid = ret->i_gid = 0;
588 ret->i_blksize = PAGE_CACHE_SIZE;
589 ret->i_blocks = 0;
590 ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
592 return ret;
595 #define BOOL_INO_OFFSET 30
597 static ssize_t sel_read_bool(struct file *filep, char __user *buf,
598 size_t count, loff_t *ppos)
600 char *page = NULL;
601 ssize_t length;
602 ssize_t end;
603 ssize_t ret;
604 int cur_enforcing;
605 struct inode *inode;
607 down(&sel_sem);
609 ret = -EFAULT;
611 /* check to see if this file has been deleted */
612 if (!filep->f_op)
613 goto out;
615 if (count < 0 || count > PAGE_SIZE) {
616 ret = -EINVAL;
617 goto out;
619 if (!(page = (char*)get_zeroed_page(GFP_KERNEL))) {
620 ret = -ENOMEM;
621 goto out;
624 inode = filep->f_dentry->d_inode;
625 cur_enforcing = security_get_bool_value(inode->i_ino - BOOL_INO_OFFSET);
626 if (cur_enforcing < 0) {
627 ret = cur_enforcing;
628 goto out;
631 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
632 bool_pending_values[inode->i_ino - BOOL_INO_OFFSET]);
633 if (length < 0) {
634 ret = length;
635 goto out;
638 if (*ppos >= length) {
639 ret = 0;
640 goto out;
642 if (count + *ppos > length)
643 count = length - *ppos;
644 end = count + *ppos;
645 if (copy_to_user(buf, (char *) page + *ppos, count)) {
646 ret = -EFAULT;
647 goto out;
649 *ppos = end;
650 ret = count;
651 out:
652 up(&sel_sem);
653 if (page)
654 free_page((unsigned long)page);
655 return ret;
658 static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
659 size_t count, loff_t *ppos)
661 char *page = NULL;
662 ssize_t length = -EFAULT;
663 int new_value;
664 struct inode *inode;
666 down(&sel_sem);
668 length = task_has_security(current, SECURITY__SETBOOL);
669 if (length)
670 goto out;
672 /* check to see if this file has been deleted */
673 if (!filep->f_op)
674 goto out;
676 if (count < 0 || count >= PAGE_SIZE) {
677 length = -ENOMEM;
678 goto out;
680 if (*ppos != 0) {
681 /* No partial writes. */
682 goto out;
684 page = (char*)get_zeroed_page(GFP_KERNEL);
685 if (!page) {
686 length = -ENOMEM;
687 goto out;
690 if (copy_from_user(page, buf, count))
691 goto out;
693 length = -EINVAL;
694 if (sscanf(page, "%d", &new_value) != 1)
695 goto out;
697 if (new_value)
698 new_value = 1;
700 inode = filep->f_dentry->d_inode;
701 bool_pending_values[inode->i_ino - BOOL_INO_OFFSET] = new_value;
702 length = count;
704 out:
705 up(&sel_sem);
706 if (page)
707 free_page((unsigned long) page);
708 return length;
711 static struct file_operations sel_bool_ops = {
712 .read = sel_read_bool,
713 .write = sel_write_bool,
716 static ssize_t sel_commit_bools_write(struct file *filep,
717 const char __user *buf,
718 size_t count, loff_t *ppos)
720 char *page = NULL;
721 ssize_t length = -EFAULT;
722 int new_value;
724 down(&sel_sem);
726 length = task_has_security(current, SECURITY__SETBOOL);
727 if (length)
728 goto out;
730 /* check to see if this file has been deleted */
731 if (!filep->f_op)
732 goto out;
734 if (count < 0 || count >= PAGE_SIZE) {
735 length = -ENOMEM;
736 goto out;
738 if (*ppos != 0) {
739 /* No partial writes. */
740 goto out;
742 page = (char*)get_zeroed_page(GFP_KERNEL);
743 if (!page) {
744 length = -ENOMEM;
745 goto out;
748 if (copy_from_user(page, buf, count))
749 goto out;
751 length = -EINVAL;
752 if (sscanf(page, "%d", &new_value) != 1)
753 goto out;
755 if (new_value) {
756 security_set_bools(bool_num, bool_pending_values);
759 length = count;
761 out:
762 up(&sel_sem);
763 if (page)
764 free_page((unsigned long) page);
765 return length;
768 static struct file_operations sel_commit_bools_ops = {
769 .write = sel_commit_bools_write,
772 /* delete booleans - partial revoke() from
773 * fs/proc/generic.c proc_kill_inodes */
774 static void sel_remove_bools(struct dentry *de)
776 struct list_head *p, *node;
777 struct super_block *sb = de->d_sb;
779 spin_lock(&dcache_lock);
780 node = de->d_subdirs.next;
781 while (node != &de->d_subdirs) {
782 struct dentry *d = list_entry(node, struct dentry, d_child);
783 list_del_init(node);
785 if (d->d_inode) {
786 d = dget_locked(d);
787 spin_unlock(&dcache_lock);
788 d_delete(d);
789 simple_unlink(de->d_inode, d);
790 dput(d);
791 spin_lock(&dcache_lock);
793 node = de->d_subdirs.next;
796 spin_unlock(&dcache_lock);
798 file_list_lock();
799 list_for_each(p, &sb->s_files) {
800 struct file * filp = list_entry(p, struct file, f_list);
801 struct dentry * dentry = filp->f_dentry;
803 if (dentry->d_parent != de) {
804 continue;
806 filp->f_op = NULL;
808 file_list_unlock();
811 #define BOOL_DIR_NAME "booleans"
813 static int sel_make_bools(void)
815 int i, ret = 0;
816 ssize_t len;
817 struct dentry *dentry = NULL;
818 struct dentry *dir = bool_dir;
819 struct inode *inode = NULL;
820 struct inode_security_struct *isec;
821 struct qstr qname;
822 char **names = NULL, *page;
823 int num;
824 int *values = NULL;
825 u32 sid;
827 /* remove any existing files */
828 if (bool_pending_values)
829 kfree(bool_pending_values);
831 sel_remove_bools(dir);
833 if (!(page = (char*)get_zeroed_page(GFP_KERNEL)))
834 return -ENOMEM;
836 ret = security_get_bools(&num, &names, &values);
837 if (ret != 0)
838 goto out;
840 for (i = 0; i < num; i++) {
841 qname.name = names[i];
842 qname.len = strlen(qname.name);
843 qname.hash = full_name_hash(qname.name, qname.len);
844 dentry = d_alloc(dir, &qname);
845 if (!dentry) {
846 ret = -ENOMEM;
847 goto err;
849 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
850 if (!inode) {
851 ret = -ENOMEM;
852 goto err;
855 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
856 if (len < 0) {
857 ret = -EINVAL;
858 goto err;
859 } else if (len >= PAGE_SIZE) {
860 ret = -ENAMETOOLONG;
861 goto err;
863 isec = (struct inode_security_struct*)inode->i_security;
864 if ((ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid)))
865 goto err;
866 isec->sid = sid;
867 isec->initialized = 1;
868 inode->i_fop = &sel_bool_ops;
869 inode->i_ino = i + BOOL_INO_OFFSET;
870 d_add(dentry, inode);
872 bool_num = num;
873 bool_pending_values = values;
874 out:
875 free_page((unsigned long)page);
876 if (names) {
877 for (i = 0; i < num; i++) {
878 if (names[i])
879 kfree(names[i]);
881 kfree(names);
883 return ret;
884 err:
885 d_genocide(dir);
886 ret = -ENOMEM;
887 goto out;
890 #define NULL_FILE_NAME "null"
892 struct dentry *selinux_null = NULL;
894 static int sel_fill_super(struct super_block * sb, void * data, int silent)
896 int ret;
897 struct dentry *dentry;
898 struct inode *inode;
899 struct qstr qname;
900 struct inode_security_struct *isec;
902 static struct tree_descr selinux_files[] = {
903 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
904 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
905 [SEL_CONTEXT] = {"context", &sel_context_ops, S_IRUGO|S_IWUGO},
906 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
907 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
908 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
909 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
910 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
911 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
912 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
913 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
914 /* last one */ {""}
916 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
917 if (ret)
918 return ret;
920 qname.name = BOOL_DIR_NAME;
921 qname.len = strlen(qname.name);
922 qname.hash = full_name_hash(qname.name, qname.len);
923 dentry = d_alloc(sb->s_root, &qname);
924 if (!dentry)
925 return -ENOMEM;
927 inode = sel_make_inode(sb, S_IFDIR | S_IRUGO | S_IXUGO);
928 if (!inode)
929 goto out;
930 inode->i_op = &simple_dir_inode_operations;
931 inode->i_fop = &simple_dir_operations;
932 d_add(dentry, inode);
933 bool_dir = dentry;
934 ret = sel_make_bools();
935 if (ret)
936 goto out;
938 qname.name = NULL_FILE_NAME;
939 qname.len = strlen(qname.name);
940 qname.hash = full_name_hash(qname.name, qname.len);
941 dentry = d_alloc(sb->s_root, &qname);
942 if (!dentry)
943 return -ENOMEM;
945 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
946 if (!inode)
947 goto out;
948 isec = (struct inode_security_struct*)inode->i_security;
949 isec->sid = SECINITSID_DEVNULL;
950 isec->sclass = SECCLASS_CHR_FILE;
951 isec->initialized = 1;
953 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
954 d_add(dentry, inode);
955 selinux_null = dentry;
957 return 0;
958 out:
959 dput(dentry);
960 printk(KERN_ERR "%s: failed while creating inodes\n", __FUNCTION__);
961 return -ENOMEM;
964 static struct super_block *sel_get_sb(struct file_system_type *fs_type,
965 int flags, const char *dev_name, void *data)
967 return get_sb_single(fs_type, flags, data, sel_fill_super);
970 static struct file_system_type sel_fs_type = {
971 .name = "selinuxfs",
972 .get_sb = sel_get_sb,
973 .kill_sb = kill_litter_super,
976 struct vfsmount *selinuxfs_mount;
978 static int __init init_sel_fs(void)
980 int err;
982 if (!selinux_enabled)
983 return 0;
984 err = register_filesystem(&sel_fs_type);
985 if (!err) {
986 selinuxfs_mount = kern_mount(&sel_fs_type);
987 if (IS_ERR(selinuxfs_mount)) {
988 printk(KERN_ERR "selinuxfs: could not mount!\n");
989 err = PTR_ERR(selinuxfs_mount);
990 selinuxfs_mount = NULL;
993 return err;
996 __initcall(init_sel_fs);
998 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
999 void exit_sel_fs(void)
1001 unregister_filesystem(&sel_fs_type);
1003 #endif