1 /* Updated: Karl MacMillan <kmacmillan@tresys.com>
3 * Added conditional policy language extensions
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.
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>
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>
29 /* selinuxfs pseudo filesystem for exporting the security policy API.
30 Based on the proc code and the fs/nfsd/nfsctl.c code. */
37 #include "conditional.h"
39 unsigned int selinux_checkreqprot = CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
41 #ifdef CONFIG_SECURITY_SELINUX_ENABLE_SECMARK_DEFAULT
42 #define SELINUX_COMPAT_NET_VALUE 0
44 #define SELINUX_COMPAT_NET_VALUE 1
47 int selinux_compat_net = SELINUX_COMPAT_NET_VALUE;
49 static int __init checkreqprot_setup(char *str)
51 selinux_checkreqprot = simple_strtoul(str,NULL,0) ? 1 : 0;
54 __setup("checkreqprot=", checkreqprot_setup);
56 static int __init selinux_compat_net_setup(char *str)
58 selinux_compat_net = simple_strtoul(str,NULL,0) ? 1 : 0;
61 __setup("selinux_compat_net=", selinux_compat_net_setup);
64 static DEFINE_MUTEX(sel_mutex);
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;
71 extern void selnl_notify_setenforce(int val);
73 /* Check whether a task is allowed to use a security operation. */
74 static int task_has_security(struct task_struct *tsk,
77 struct task_security_struct *tsec;
83 return avc_has_perm(tsec->sid, SECINITSID_SECURITY,
84 SECCLASS_SECURITY, perms, NULL);
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 */
107 static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
108 size_t count, loff_t *ppos)
110 char tmpbuf[TMPBUFLEN];
113 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing);
114 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
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)
126 if (count >= PAGE_SIZE)
129 /* No partial writes. */
132 page = (char*)get_zeroed_page(GFP_KERNEL);
136 if (copy_from_user(page, buf, count))
140 if (sscanf(page, "%d", &new_value) != 1)
143 if (new_value != selinux_enforcing) {
144 length = task_has_security(current, SECURITY__SETENFORCE);
147 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
148 "enforcing=%d old_enforcing=%d auid=%u", new_value,
150 audit_get_loginuid(current->audit_context));
151 selinux_enforcing = new_value;
152 if (selinux_enforcing)
154 selnl_notify_setenforce(selinux_enforcing);
158 free_page((unsigned long) page);
162 #define sel_write_enforce NULL
165 static struct file_operations sel_enforce_ops = {
166 .read = sel_read_enforce,
167 .write = sel_write_enforce,
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)
178 extern int selinux_disable(void);
180 if (count >= PAGE_SIZE)
183 /* No partial writes. */
186 page = (char*)get_zeroed_page(GFP_KERNEL);
190 if (copy_from_user(page, buf, count))
194 if (sscanf(page, "%d", &new_value) != 1)
198 length = selinux_disable();
201 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
203 audit_get_loginuid(current->audit_context));
208 free_page((unsigned long) page);
212 #define sel_write_disable NULL
215 static struct file_operations sel_disable_ops = {
216 .write = sel_write_disable,
219 static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
220 size_t count, loff_t *ppos)
222 char tmpbuf[TMPBUFLEN];
225 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
226 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
229 static struct file_operations sel_policyvers_ops = {
230 .read = sel_read_policyvers,
233 /* declaration for sel_write_load */
234 static int sel_make_bools(void);
236 static ssize_t sel_read_mls(struct file *filp, char __user *buf,
237 size_t count, loff_t *ppos)
239 char tmpbuf[TMPBUFLEN];
242 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_mls_enabled);
243 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
246 static struct file_operations sel_mls_ops = {
247 .read = sel_read_mls,
250 static ssize_t sel_write_load(struct file * file, const char __user * buf,
251 size_t count, loff_t *ppos)
258 mutex_lock(&sel_mutex);
260 length = task_has_security(current, SECURITY__LOAD_POLICY);
265 /* No partial writes. */
270 if ((count > 64 * 1024 * 1024)
271 || (data = vmalloc(count)) == NULL) {
277 if (copy_from_user(data, buf, count) != 0)
280 length = security_load_policy(data, count);
284 ret = sel_make_bools();
289 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
290 "policy loaded auid=%u",
291 audit_get_loginuid(current->audit_context));
293 mutex_unlock(&sel_mutex);
298 static struct file_operations sel_load_ops = {
299 .write = sel_write_load,
302 static ssize_t sel_write_context(struct file * file, char *buf, size_t size)
308 length = task_has_security(current, SECURITY__CHECK_CONTEXT);
312 length = security_context_to_sid(buf, size, &sid);
316 length = security_sid_to_context(sid, &canon, &len);
320 if (len > SIMPLE_TRANSACTION_LIMIT) {
321 printk(KERN_ERR "%s: context size (%u) exceeds payload "
322 "max\n", __FUNCTION__, len);
327 memcpy(buf, canon, len);
334 static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
335 size_t count, loff_t *ppos)
337 char tmpbuf[TMPBUFLEN];
340 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot);
341 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
344 static ssize_t sel_write_checkreqprot(struct file * file, const char __user * buf,
345 size_t count, loff_t *ppos)
349 unsigned int new_value;
351 length = task_has_security(current, SECURITY__SETCHECKREQPROT);
355 if (count >= PAGE_SIZE)
358 /* No partial writes. */
361 page = (char*)get_zeroed_page(GFP_KERNEL);
365 if (copy_from_user(page, buf, count))
369 if (sscanf(page, "%u", &new_value) != 1)
372 selinux_checkreqprot = new_value ? 1 : 0;
375 free_page((unsigned long) page);
378 static struct file_operations sel_checkreqprot_ops = {
379 .read = sel_read_checkreqprot,
380 .write = sel_write_checkreqprot,
383 static ssize_t sel_read_compat_net(struct file *filp, char __user *buf,
384 size_t count, loff_t *ppos)
386 char tmpbuf[TMPBUFLEN];
389 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_compat_net);
390 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
393 static ssize_t sel_write_compat_net(struct file * file, const char __user * buf,
394 size_t count, loff_t *ppos)
400 length = task_has_security(current, SECURITY__LOAD_POLICY);
404 if (count >= PAGE_SIZE)
407 /* No partial writes. */
410 page = (char*)get_zeroed_page(GFP_KERNEL);
414 if (copy_from_user(page, buf, count))
418 if (sscanf(page, "%d", &new_value) != 1)
421 selinux_compat_net = new_value ? 1 : 0;
424 free_page((unsigned long) page);
427 static struct file_operations sel_compat_net_ops = {
428 .read = sel_read_compat_net,
429 .write = sel_write_compat_net,
433 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
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);
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,
450 static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
452 ino_t ino = file->f_dentry->d_inode->i_ino;
456 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
459 data = simple_transaction_get(file, buf, size);
461 return PTR_ERR(data);
463 rv = write_op[ino](file, data, size);
465 simple_transaction_set(file, rv);
471 static struct file_operations transaction_ops = {
472 .write = selinux_transaction_write,
473 .read = simple_transaction_read,
474 .release = simple_transaction_release,
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.
483 static ssize_t sel_write_access(struct file * file, char *buf, size_t size)
489 struct av_decision avd;
492 length = task_has_security(current, SECURITY__COMPUTE_AV);
497 scon = kzalloc(size+1, GFP_KERNEL);
501 tcon = kzalloc(size+1, GFP_KERNEL);
506 if (sscanf(buf, "%s %s %hu %x", scon, tcon, &tclass, &req) != 4)
509 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
512 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
516 length = security_compute_av(ssid, tsid, tclass, req, &avd);
520 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
522 avd.allowed, avd.decided,
523 avd.auditallow, avd.auditdeny,
532 static ssize_t sel_write_create(struct file * file, char *buf, size_t size)
535 u32 ssid, tsid, newsid;
541 length = task_has_security(current, SECURITY__COMPUTE_CREATE);
546 scon = kzalloc(size+1, GFP_KERNEL);
550 tcon = kzalloc(size+1, GFP_KERNEL);
555 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
558 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
561 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
565 length = security_transition_sid(ssid, tsid, tclass, &newsid);
569 length = security_sid_to_context(newsid, &newcon, &len);
573 if (len > SIMPLE_TRANSACTION_LIMIT) {
574 printk(KERN_ERR "%s: context size (%u) exceeds payload "
575 "max\n", __FUNCTION__, len);
580 memcpy(buf, newcon, len);
591 static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size)
594 u32 ssid, tsid, newsid;
600 length = task_has_security(current, SECURITY__COMPUTE_RELABEL);
605 scon = kzalloc(size+1, GFP_KERNEL);
609 tcon = kzalloc(size+1, GFP_KERNEL);
614 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
617 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
620 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
624 length = security_change_sid(ssid, tsid, tclass, &newsid);
628 length = security_sid_to_context(newsid, &newcon, &len);
632 if (len > SIMPLE_TRANSACTION_LIMIT) {
637 memcpy(buf, newcon, len);
648 static ssize_t sel_write_user(struct file * file, char *buf, size_t size)
650 char *con, *user, *ptr;
657 length = task_has_security(current, SECURITY__COMPUTE_USER);
662 con = kzalloc(size+1, GFP_KERNEL);
666 user = kzalloc(size+1, GFP_KERNEL);
671 if (sscanf(buf, "%s %s", con, user) != 2)
674 length = security_context_to_sid(con, strlen(con)+1, &sid);
678 length = security_get_user_sids(sid, user, &sids, &nsids);
682 length = sprintf(buf, "%u", nsids) + 1;
684 for (i = 0; i < nsids; i++) {
685 rc = security_sid_to_context(sids[i], &newcon, &len);
690 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
695 memcpy(ptr, newcon, len);
709 static ssize_t sel_write_member(struct file * file, char *buf, size_t size)
712 u32 ssid, tsid, newsid;
718 length = task_has_security(current, SECURITY__COMPUTE_MEMBER);
723 scon = kzalloc(size+1, GFP_KERNEL);
727 tcon = kzalloc(size+1, GFP_KERNEL);
732 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
735 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
738 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
742 length = security_member_sid(ssid, tsid, tclass, &newsid);
746 length = security_sid_to_context(newsid, &newcon, &len);
750 if (len > SIMPLE_TRANSACTION_LIMIT) {
751 printk(KERN_ERR "%s: context size (%u) exceeds payload "
752 "max\n", __FUNCTION__, len);
757 memcpy(buf, newcon, len);
768 static struct inode *sel_make_inode(struct super_block *sb, int mode)
770 struct inode *ret = new_inode(sb);
774 ret->i_uid = ret->i_gid = 0;
775 ret->i_blksize = PAGE_CACHE_SIZE;
777 ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
782 #define BOOL_INO_OFFSET 30
784 static ssize_t sel_read_bool(struct file *filep, char __user *buf,
785 size_t count, loff_t *ppos)
793 mutex_lock(&sel_mutex);
797 /* check to see if this file has been deleted */
801 if (count > PAGE_SIZE) {
805 if (!(page = (char*)get_zeroed_page(GFP_KERNEL))) {
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) {
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);
821 mutex_unlock(&sel_mutex);
823 free_page((unsigned long)page);
827 static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
828 size_t count, loff_t *ppos)
831 ssize_t length = -EFAULT;
835 mutex_lock(&sel_mutex);
837 length = task_has_security(current, SECURITY__SETBOOL);
841 /* check to see if this file has been deleted */
845 if (count >= PAGE_SIZE) {
850 /* No partial writes. */
853 page = (char*)get_zeroed_page(GFP_KERNEL);
859 if (copy_from_user(page, buf, count))
863 if (sscanf(page, "%d", &new_value) != 1)
869 inode = filep->f_dentry->d_inode;
870 bool_pending_values[inode->i_ino - BOOL_INO_OFFSET] = new_value;
874 mutex_unlock(&sel_mutex);
876 free_page((unsigned long) page);
880 static struct file_operations sel_bool_ops = {
881 .read = sel_read_bool,
882 .write = sel_write_bool,
885 static ssize_t sel_commit_bools_write(struct file *filep,
886 const char __user *buf,
887 size_t count, loff_t *ppos)
890 ssize_t length = -EFAULT;
893 mutex_lock(&sel_mutex);
895 length = task_has_security(current, SECURITY__SETBOOL);
899 /* check to see if this file has been deleted */
903 if (count >= PAGE_SIZE) {
908 /* No partial writes. */
911 page = (char*)get_zeroed_page(GFP_KERNEL);
917 if (copy_from_user(page, buf, count))
921 if (sscanf(page, "%d", &new_value) != 1)
924 if (new_value && bool_pending_values) {
925 security_set_bools(bool_num, bool_pending_values);
931 mutex_unlock(&sel_mutex);
933 free_page((unsigned long) page);
937 static struct file_operations sel_commit_bools_ops = {
938 .write = sel_commit_bools_write,
941 /* delete booleans - partial revoke() from
942 * fs/proc/generic.c proc_kill_inodes */
943 static void sel_remove_bools(struct dentry *de)
945 struct list_head *p, *node;
946 struct super_block *sb = de->d_sb;
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);
956 spin_unlock(&dcache_lock);
958 simple_unlink(de->d_inode, d);
960 spin_lock(&dcache_lock);
962 node = de->d_subdirs.next;
965 spin_unlock(&dcache_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;
972 if (dentry->d_parent != de) {
980 #define BOOL_DIR_NAME "booleans"
982 static int sel_make_bools(void)
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;
995 /* remove any existing files */
996 kfree(bool_pending_values);
997 bool_pending_values = NULL;
999 sel_remove_bools(dir);
1001 if (!(page = (char*)get_zeroed_page(GFP_KERNEL)))
1004 ret = security_get_bools(&num, &names, &values);
1008 for (i = 0; i < num; i++) {
1009 dentry = d_alloc_name(dir, names[i]);
1014 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1020 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
1024 } else if (len >= PAGE_SIZE) {
1025 ret = -ENAMETOOLONG;
1028 isec = (struct inode_security_struct*)inode->i_security;
1029 if ((ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &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);
1038 bool_pending_values = values;
1040 free_page((unsigned long)page);
1042 for (i = 0; i < num; i++)
1049 sel_remove_bools(dir);
1054 #define NULL_FILE_NAME "null"
1056 struct dentry *selinux_null = NULL;
1058 static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1059 size_t count, loff_t *ppos)
1061 char tmpbuf[TMPBUFLEN];
1064 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
1065 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1068 static ssize_t sel_write_avc_cache_threshold(struct file * file,
1069 const char __user * buf,
1070 size_t count, loff_t *ppos)
1077 if (count >= PAGE_SIZE) {
1083 /* No partial writes. */
1088 page = (char*)get_zeroed_page(GFP_KERNEL);
1094 if (copy_from_user(page, buf, count)) {
1099 if (sscanf(page, "%u", &new_value) != 1) {
1104 if (new_value != avc_cache_threshold) {
1105 ret = task_has_security(current, SECURITY__SETSECPARAM);
1108 avc_cache_threshold = new_value;
1112 free_page((unsigned long)page);
1117 static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1118 size_t count, loff_t *ppos)
1123 page = (char *)__get_free_page(GFP_KERNEL);
1128 ret = avc_get_hash_stats(page);
1130 ret = simple_read_from_buffer(buf, count, ppos, page, ret);
1131 free_page((unsigned long)page);
1136 static struct file_operations sel_avc_cache_threshold_ops = {
1137 .read = sel_read_avc_cache_threshold,
1138 .write = sel_write_avc_cache_threshold,
1141 static struct file_operations sel_avc_hash_stats_ops = {
1142 .read = sel_read_avc_hash_stats,
1145 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1146 static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1150 for (cpu = *idx; cpu < NR_CPUS; ++cpu) {
1151 if (!cpu_possible(cpu))
1154 return &per_cpu(avc_cache_stats, cpu);
1159 static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1161 loff_t n = *pos - 1;
1164 return SEQ_START_TOKEN;
1166 return sel_avc_get_stat_idx(&n);
1169 static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1171 return sel_avc_get_stat_idx(pos);
1174 static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1176 struct avc_cache_stats *st = v;
1178 if (v == SEQ_START_TOKEN)
1179 seq_printf(seq, "lookups hits misses allocations reclaims "
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);
1188 static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
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,
1198 static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1200 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1203 static struct file_operations sel_avc_cache_stats_ops = {
1204 .open = sel_open_avc_cache_stats,
1206 .llseek = seq_lseek,
1207 .release = seq_release,
1211 static int sel_make_avc_files(struct dentry *dir)
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 },
1223 for (i = 0; i < ARRAY_SIZE(files); i++) {
1224 struct inode *inode;
1225 struct dentry *dentry;
1227 dentry = d_alloc_name(dir, files[i].name);
1233 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1238 inode->i_fop = files[i].ops;
1239 d_add(dentry, inode);
1245 static int sel_make_dir(struct inode *dir, struct dentry *dentry)
1248 struct inode *inode;
1250 inode = sel_make_inode(dir->i_sb, S_IFDIR | S_IRUGO | S_IXUGO);
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) */
1259 d_add(dentry, inode);
1260 /* bump link count on parent directory, too */
1266 static int sel_fill_super(struct super_block * sb, void * data, int silent)
1269 struct dentry *dentry;
1270 struct inode *inode, *root_inode;
1271 struct inode_security_struct *isec;
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},
1290 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1294 root_inode = sb->s_root->d_inode;
1296 dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME);
1302 ret = sel_make_dir(root_inode, dentry);
1308 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
1314 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
1319 isec = (struct inode_security_struct*)inode->i_security;
1320 isec->sid = SECINITSID_DEVNULL;
1321 isec->sclass = SECCLASS_CHR_FILE;
1322 isec->initialized = 1;
1324 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1325 d_add(dentry, inode);
1326 selinux_null = dentry;
1328 dentry = d_alloc_name(sb->s_root, "avc");
1334 ret = sel_make_dir(root_inode, dentry);
1338 ret = sel_make_avc_files(dentry);
1344 printk(KERN_ERR "%s: failed while creating inodes\n", __FUNCTION__);
1348 static int sel_get_sb(struct file_system_type *fs_type,
1349 int flags, const char *dev_name, void *data,
1350 struct vfsmount *mnt)
1352 return get_sb_single(fs_type, flags, data, sel_fill_super, mnt);
1355 static struct file_system_type sel_fs_type = {
1356 .name = "selinuxfs",
1357 .get_sb = sel_get_sb,
1358 .kill_sb = kill_litter_super,
1361 struct vfsmount *selinuxfs_mount;
1363 static int __init init_sel_fs(void)
1367 if (!selinux_enabled)
1369 err = register_filesystem(&sel_fs_type);
1371 selinuxfs_mount = kern_mount(&sel_fs_type);
1372 if (IS_ERR(selinuxfs_mount)) {
1373 printk(KERN_ERR "selinuxfs: could not mount!\n");
1374 err = PTR_ERR(selinuxfs_mount);
1375 selinuxfs_mount = NULL;
1381 __initcall(init_sel_fs);
1383 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
1384 void exit_sel_fs(void)
1386 unregister_filesystem(&sel_fs_type);