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/kernel.h>
13 #include <linux/pagemap.h>
14 #include <linux/slab.h>
15 #include <linux/vmalloc.h>
17 #include <linux/mutex.h>
18 #include <linux/init.h>
19 #include <linux/string.h>
20 #include <linux/security.h>
21 #include <linux/major.h>
22 #include <linux/seq_file.h>
23 #include <linux/percpu.h>
24 #include <linux/audit.h>
25 #include <asm/uaccess.h>
26 #include <asm/semaphore.h>
28 /* selinuxfs pseudo filesystem for exporting the security policy API.
29 Based on the proc code and the fs/nfsd/nfsctl.c code. */
36 #include "conditional.h"
38 unsigned int selinux_checkreqprot = CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
40 #ifdef CONFIG_SECURITY_SELINUX_ENABLE_SECMARK_DEFAULT
41 #define SELINUX_COMPAT_NET_VALUE 0
43 #define SELINUX_COMPAT_NET_VALUE 1
46 int selinux_compat_net = SELINUX_COMPAT_NET_VALUE;
48 static int __init checkreqprot_setup(char *str)
50 selinux_checkreqprot = simple_strtoul(str,NULL,0) ? 1 : 0;
53 __setup("checkreqprot=", checkreqprot_setup);
55 static int __init selinux_compat_net_setup(char *str)
57 selinux_compat_net = simple_strtoul(str,NULL,0) ? 1 : 0;
60 __setup("selinux_compat_net=", selinux_compat_net_setup);
63 static DEFINE_MUTEX(sel_mutex);
65 /* global data for booleans */
66 static struct dentry *bool_dir = NULL;
67 static int bool_num = 0;
68 static int *bool_pending_values = NULL;
70 extern void selnl_notify_setenforce(int val);
72 /* Check whether a task is allowed to use a security operation. */
73 static int task_has_security(struct task_struct *tsk,
76 struct task_security_struct *tsec;
82 return avc_has_perm(tsec->sid, SECINITSID_SECURITY,
83 SECCLASS_SECURITY, perms, NULL);
88 SEL_LOAD, /* load policy */
89 SEL_ENFORCE, /* get or set enforcing status */
90 SEL_CONTEXT, /* validate context */
91 SEL_ACCESS, /* compute access decision */
92 SEL_CREATE, /* compute create labeling decision */
93 SEL_RELABEL, /* compute relabeling decision */
94 SEL_USER, /* compute reachable user contexts */
95 SEL_POLICYVERS, /* return policy version for this kernel */
96 SEL_COMMIT_BOOLS, /* commit new boolean values */
97 SEL_MLS, /* return if MLS policy is enabled */
98 SEL_DISABLE, /* disable SELinux until next reboot */
99 SEL_MEMBER, /* compute polyinstantiation membership decision */
100 SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
101 SEL_COMPAT_NET, /* whether to use old compat network packet controls */
102 SEL_INO_NEXT, /* The next inode number to use */
105 static unsigned long sel_last_ino = SEL_INO_NEXT - 1;
107 #define SEL_INITCON_INO_OFFSET 0x01000000
108 #define SEL_INO_MASK 0x00ffffff
111 static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
112 size_t count, loff_t *ppos)
114 char tmpbuf[TMPBUFLEN];
117 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing);
118 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
121 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
122 static ssize_t sel_write_enforce(struct file * file, const char __user * buf,
123 size_t count, loff_t *ppos)
130 if (count >= PAGE_SIZE)
133 /* No partial writes. */
136 page = (char*)get_zeroed_page(GFP_KERNEL);
140 if (copy_from_user(page, buf, count))
144 if (sscanf(page, "%d", &new_value) != 1)
147 if (new_value != selinux_enforcing) {
148 length = task_has_security(current, SECURITY__SETENFORCE);
151 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
152 "enforcing=%d old_enforcing=%d auid=%u", new_value,
154 audit_get_loginuid(current->audit_context));
155 selinux_enforcing = new_value;
156 if (selinux_enforcing)
158 selnl_notify_setenforce(selinux_enforcing);
162 free_page((unsigned long) page);
166 #define sel_write_enforce NULL
169 static const struct file_operations sel_enforce_ops = {
170 .read = sel_read_enforce,
171 .write = sel_write_enforce,
174 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
175 static ssize_t sel_write_disable(struct file * file, const char __user * buf,
176 size_t count, loff_t *ppos)
182 extern int selinux_disable(void);
184 if (count >= PAGE_SIZE)
187 /* No partial writes. */
190 page = (char*)get_zeroed_page(GFP_KERNEL);
194 if (copy_from_user(page, buf, count))
198 if (sscanf(page, "%d", &new_value) != 1)
202 length = selinux_disable();
205 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
207 audit_get_loginuid(current->audit_context));
212 free_page((unsigned long) page);
216 #define sel_write_disable NULL
219 static const struct file_operations sel_disable_ops = {
220 .write = sel_write_disable,
223 static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
224 size_t count, loff_t *ppos)
226 char tmpbuf[TMPBUFLEN];
229 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
230 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
233 static const struct file_operations sel_policyvers_ops = {
234 .read = sel_read_policyvers,
237 /* declaration for sel_write_load */
238 static int sel_make_bools(void);
240 static ssize_t sel_read_mls(struct file *filp, char __user *buf,
241 size_t count, loff_t *ppos)
243 char tmpbuf[TMPBUFLEN];
246 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_mls_enabled);
247 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
250 static const struct file_operations sel_mls_ops = {
251 .read = sel_read_mls,
254 static ssize_t sel_write_load(struct file * file, const char __user * buf,
255 size_t count, loff_t *ppos)
262 mutex_lock(&sel_mutex);
264 length = task_has_security(current, SECURITY__LOAD_POLICY);
269 /* No partial writes. */
274 if ((count > 64 * 1024 * 1024)
275 || (data = vmalloc(count)) == NULL) {
281 if (copy_from_user(data, buf, count) != 0)
284 length = security_load_policy(data, count);
288 ret = sel_make_bools();
293 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
294 "policy loaded auid=%u",
295 audit_get_loginuid(current->audit_context));
297 mutex_unlock(&sel_mutex);
302 static const struct file_operations sel_load_ops = {
303 .write = sel_write_load,
306 static ssize_t sel_write_context(struct file * file, char *buf, size_t size)
312 length = task_has_security(current, SECURITY__CHECK_CONTEXT);
316 length = security_context_to_sid(buf, size, &sid);
320 length = security_sid_to_context(sid, &canon, &len);
324 if (len > SIMPLE_TRANSACTION_LIMIT) {
325 printk(KERN_ERR "%s: context size (%u) exceeds payload "
326 "max\n", __FUNCTION__, len);
331 memcpy(buf, canon, len);
338 static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
339 size_t count, loff_t *ppos)
341 char tmpbuf[TMPBUFLEN];
344 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot);
345 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
348 static ssize_t sel_write_checkreqprot(struct file * file, const char __user * buf,
349 size_t count, loff_t *ppos)
353 unsigned int new_value;
355 length = task_has_security(current, SECURITY__SETCHECKREQPROT);
359 if (count >= PAGE_SIZE)
362 /* No partial writes. */
365 page = (char*)get_zeroed_page(GFP_KERNEL);
369 if (copy_from_user(page, buf, count))
373 if (sscanf(page, "%u", &new_value) != 1)
376 selinux_checkreqprot = new_value ? 1 : 0;
379 free_page((unsigned long) page);
382 static const struct file_operations sel_checkreqprot_ops = {
383 .read = sel_read_checkreqprot,
384 .write = sel_write_checkreqprot,
387 static ssize_t sel_read_compat_net(struct file *filp, char __user *buf,
388 size_t count, loff_t *ppos)
390 char tmpbuf[TMPBUFLEN];
393 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_compat_net);
394 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
397 static ssize_t sel_write_compat_net(struct file * file, const char __user * buf,
398 size_t count, loff_t *ppos)
404 length = task_has_security(current, SECURITY__LOAD_POLICY);
408 if (count >= PAGE_SIZE)
411 /* No partial writes. */
414 page = (char*)get_zeroed_page(GFP_KERNEL);
418 if (copy_from_user(page, buf, count))
422 if (sscanf(page, "%d", &new_value) != 1)
425 selinux_compat_net = new_value ? 1 : 0;
428 free_page((unsigned long) page);
431 static const struct file_operations sel_compat_net_ops = {
432 .read = sel_read_compat_net,
433 .write = sel_write_compat_net,
437 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
439 static ssize_t sel_write_access(struct file * file, char *buf, size_t size);
440 static ssize_t sel_write_create(struct file * file, char *buf, size_t size);
441 static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size);
442 static ssize_t sel_write_user(struct file * file, char *buf, size_t size);
443 static ssize_t sel_write_member(struct file * file, char *buf, size_t size);
445 static ssize_t (*write_op[])(struct file *, char *, size_t) = {
446 [SEL_ACCESS] = sel_write_access,
447 [SEL_CREATE] = sel_write_create,
448 [SEL_RELABEL] = sel_write_relabel,
449 [SEL_USER] = sel_write_user,
450 [SEL_MEMBER] = sel_write_member,
451 [SEL_CONTEXT] = sel_write_context,
454 static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
456 ino_t ino = file->f_path.dentry->d_inode->i_ino;
460 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
463 data = simple_transaction_get(file, buf, size);
465 return PTR_ERR(data);
467 rv = write_op[ino](file, data, size);
469 simple_transaction_set(file, rv);
475 static const struct file_operations transaction_ops = {
476 .write = selinux_transaction_write,
477 .read = simple_transaction_read,
478 .release = simple_transaction_release,
482 * payload - write methods
483 * If the method has a response, the response should be put in buf,
484 * and the length returned. Otherwise return 0 or and -error.
487 static ssize_t sel_write_access(struct file * file, char *buf, size_t size)
493 struct av_decision avd;
496 length = task_has_security(current, SECURITY__COMPUTE_AV);
501 scon = kzalloc(size+1, GFP_KERNEL);
505 tcon = kzalloc(size+1, GFP_KERNEL);
510 if (sscanf(buf, "%s %s %hu %x", scon, tcon, &tclass, &req) != 4)
513 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
516 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
520 length = security_compute_av(ssid, tsid, tclass, req, &avd);
524 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
526 avd.allowed, avd.decided,
527 avd.auditallow, avd.auditdeny,
536 static ssize_t sel_write_create(struct file * file, char *buf, size_t size)
539 u32 ssid, tsid, newsid;
545 length = task_has_security(current, SECURITY__COMPUTE_CREATE);
550 scon = kzalloc(size+1, GFP_KERNEL);
554 tcon = kzalloc(size+1, GFP_KERNEL);
559 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
562 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
565 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
569 length = security_transition_sid(ssid, tsid, tclass, &newsid);
573 length = security_sid_to_context(newsid, &newcon, &len);
577 if (len > SIMPLE_TRANSACTION_LIMIT) {
578 printk(KERN_ERR "%s: context size (%u) exceeds payload "
579 "max\n", __FUNCTION__, len);
584 memcpy(buf, newcon, len);
595 static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size)
598 u32 ssid, tsid, newsid;
604 length = task_has_security(current, SECURITY__COMPUTE_RELABEL);
609 scon = kzalloc(size+1, GFP_KERNEL);
613 tcon = kzalloc(size+1, GFP_KERNEL);
618 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
621 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
624 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
628 length = security_change_sid(ssid, tsid, tclass, &newsid);
632 length = security_sid_to_context(newsid, &newcon, &len);
636 if (len > SIMPLE_TRANSACTION_LIMIT) {
641 memcpy(buf, newcon, len);
652 static ssize_t sel_write_user(struct file * file, char *buf, size_t size)
654 char *con, *user, *ptr;
661 length = task_has_security(current, SECURITY__COMPUTE_USER);
666 con = kzalloc(size+1, GFP_KERNEL);
670 user = kzalloc(size+1, GFP_KERNEL);
675 if (sscanf(buf, "%s %s", con, user) != 2)
678 length = security_context_to_sid(con, strlen(con)+1, &sid);
682 length = security_get_user_sids(sid, user, &sids, &nsids);
686 length = sprintf(buf, "%u", nsids) + 1;
688 for (i = 0; i < nsids; i++) {
689 rc = security_sid_to_context(sids[i], &newcon, &len);
694 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
699 memcpy(ptr, newcon, len);
713 static ssize_t sel_write_member(struct file * file, char *buf, size_t size)
716 u32 ssid, tsid, newsid;
722 length = task_has_security(current, SECURITY__COMPUTE_MEMBER);
727 scon = kzalloc(size+1, GFP_KERNEL);
731 tcon = kzalloc(size+1, GFP_KERNEL);
736 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
739 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
742 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
746 length = security_member_sid(ssid, tsid, tclass, &newsid);
750 length = security_sid_to_context(newsid, &newcon, &len);
754 if (len > SIMPLE_TRANSACTION_LIMIT) {
755 printk(KERN_ERR "%s: context size (%u) exceeds payload "
756 "max\n", __FUNCTION__, len);
761 memcpy(buf, newcon, len);
772 static struct inode *sel_make_inode(struct super_block *sb, int mode)
774 struct inode *ret = new_inode(sb);
778 ret->i_uid = ret->i_gid = 0;
780 ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
785 #define BOOL_INO_OFFSET 30
787 static ssize_t sel_read_bool(struct file *filep, char __user *buf,
788 size_t count, loff_t *ppos)
796 mutex_lock(&sel_mutex);
800 /* check to see if this file has been deleted */
804 if (count > PAGE_SIZE) {
808 if (!(page = (char*)get_zeroed_page(GFP_KERNEL))) {
813 inode = filep->f_path.dentry->d_inode;
814 cur_enforcing = security_get_bool_value(inode->i_ino - BOOL_INO_OFFSET);
815 if (cur_enforcing < 0) {
820 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
821 bool_pending_values[inode->i_ino - BOOL_INO_OFFSET]);
822 ret = simple_read_from_buffer(buf, count, ppos, page, length);
824 mutex_unlock(&sel_mutex);
826 free_page((unsigned long)page);
830 static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
831 size_t count, loff_t *ppos)
834 ssize_t length = -EFAULT;
838 mutex_lock(&sel_mutex);
840 length = task_has_security(current, SECURITY__SETBOOL);
844 /* check to see if this file has been deleted */
848 if (count >= PAGE_SIZE) {
853 /* No partial writes. */
856 page = (char*)get_zeroed_page(GFP_KERNEL);
862 if (copy_from_user(page, buf, count))
866 if (sscanf(page, "%d", &new_value) != 1)
872 inode = filep->f_path.dentry->d_inode;
873 bool_pending_values[inode->i_ino - BOOL_INO_OFFSET] = new_value;
877 mutex_unlock(&sel_mutex);
879 free_page((unsigned long) page);
883 static const struct file_operations sel_bool_ops = {
884 .read = sel_read_bool,
885 .write = sel_write_bool,
888 static ssize_t sel_commit_bools_write(struct file *filep,
889 const char __user *buf,
890 size_t count, loff_t *ppos)
893 ssize_t length = -EFAULT;
896 mutex_lock(&sel_mutex);
898 length = task_has_security(current, SECURITY__SETBOOL);
902 /* check to see if this file has been deleted */
906 if (count >= PAGE_SIZE) {
911 /* No partial writes. */
914 page = (char*)get_zeroed_page(GFP_KERNEL);
920 if (copy_from_user(page, buf, count))
924 if (sscanf(page, "%d", &new_value) != 1)
927 if (new_value && bool_pending_values) {
928 security_set_bools(bool_num, bool_pending_values);
934 mutex_unlock(&sel_mutex);
936 free_page((unsigned long) page);
940 static const struct file_operations sel_commit_bools_ops = {
941 .write = sel_commit_bools_write,
944 /* delete booleans - partial revoke() from
945 * fs/proc/generic.c proc_kill_inodes */
946 static void sel_remove_bools(struct dentry *de)
948 struct list_head *p, *node;
949 struct super_block *sb = de->d_sb;
951 spin_lock(&dcache_lock);
952 node = de->d_subdirs.next;
953 while (node != &de->d_subdirs) {
954 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
959 spin_unlock(&dcache_lock);
961 simple_unlink(de->d_inode, d);
963 spin_lock(&dcache_lock);
965 node = de->d_subdirs.next;
968 spin_unlock(&dcache_lock);
971 list_for_each(p, &sb->s_files) {
972 struct file * filp = list_entry(p, struct file, f_u.fu_list);
973 struct dentry * dentry = filp->f_path.dentry;
975 if (dentry->d_parent != de) {
983 #define BOOL_DIR_NAME "booleans"
985 static int sel_make_bools(void)
989 struct dentry *dentry = NULL;
990 struct dentry *dir = bool_dir;
991 struct inode *inode = NULL;
992 struct inode_security_struct *isec;
993 char **names = NULL, *page;
998 /* remove any existing files */
999 kfree(bool_pending_values);
1000 bool_pending_values = NULL;
1002 sel_remove_bools(dir);
1004 if (!(page = (char*)get_zeroed_page(GFP_KERNEL)))
1007 ret = security_get_bools(&num, &names, &values);
1011 for (i = 0; i < num; i++) {
1012 dentry = d_alloc_name(dir, names[i]);
1017 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1023 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
1027 } else if (len >= PAGE_SIZE) {
1028 ret = -ENAMETOOLONG;
1031 isec = (struct inode_security_struct*)inode->i_security;
1032 if ((ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid)))
1035 isec->initialized = 1;
1036 inode->i_fop = &sel_bool_ops;
1037 inode->i_ino = i + BOOL_INO_OFFSET;
1038 d_add(dentry, inode);
1041 bool_pending_values = values;
1043 free_page((unsigned long)page);
1045 for (i = 0; i < num; i++)
1052 sel_remove_bools(dir);
1057 #define NULL_FILE_NAME "null"
1059 struct dentry *selinux_null = NULL;
1061 static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1062 size_t count, loff_t *ppos)
1064 char tmpbuf[TMPBUFLEN];
1067 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
1068 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1071 static ssize_t sel_write_avc_cache_threshold(struct file * file,
1072 const char __user * buf,
1073 size_t count, loff_t *ppos)
1080 if (count >= PAGE_SIZE) {
1086 /* No partial writes. */
1091 page = (char*)get_zeroed_page(GFP_KERNEL);
1097 if (copy_from_user(page, buf, count)) {
1102 if (sscanf(page, "%u", &new_value) != 1) {
1107 if (new_value != avc_cache_threshold) {
1108 ret = task_has_security(current, SECURITY__SETSECPARAM);
1111 avc_cache_threshold = new_value;
1115 free_page((unsigned long)page);
1120 static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1121 size_t count, loff_t *ppos)
1126 page = (char *)__get_free_page(GFP_KERNEL);
1131 ret = avc_get_hash_stats(page);
1133 ret = simple_read_from_buffer(buf, count, ppos, page, ret);
1134 free_page((unsigned long)page);
1139 static const struct file_operations sel_avc_cache_threshold_ops = {
1140 .read = sel_read_avc_cache_threshold,
1141 .write = sel_write_avc_cache_threshold,
1144 static const struct file_operations sel_avc_hash_stats_ops = {
1145 .read = sel_read_avc_hash_stats,
1148 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1149 static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1153 for (cpu = *idx; cpu < NR_CPUS; ++cpu) {
1154 if (!cpu_possible(cpu))
1157 return &per_cpu(avc_cache_stats, cpu);
1162 static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1164 loff_t n = *pos - 1;
1167 return SEQ_START_TOKEN;
1169 return sel_avc_get_stat_idx(&n);
1172 static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1174 return sel_avc_get_stat_idx(pos);
1177 static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1179 struct avc_cache_stats *st = v;
1181 if (v == SEQ_START_TOKEN)
1182 seq_printf(seq, "lookups hits misses allocations reclaims "
1185 seq_printf(seq, "%u %u %u %u %u %u\n", st->lookups,
1186 st->hits, st->misses, st->allocations,
1187 st->reclaims, st->frees);
1191 static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1194 static struct seq_operations sel_avc_cache_stats_seq_ops = {
1195 .start = sel_avc_stats_seq_start,
1196 .next = sel_avc_stats_seq_next,
1197 .show = sel_avc_stats_seq_show,
1198 .stop = sel_avc_stats_seq_stop,
1201 static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1203 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1206 static const struct file_operations sel_avc_cache_stats_ops = {
1207 .open = sel_open_avc_cache_stats,
1209 .llseek = seq_lseek,
1210 .release = seq_release,
1214 static int sel_make_avc_files(struct dentry *dir)
1217 static struct tree_descr files[] = {
1218 { "cache_threshold",
1219 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1220 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1221 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1222 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1226 for (i = 0; i < ARRAY_SIZE(files); i++) {
1227 struct inode *inode;
1228 struct dentry *dentry;
1230 dentry = d_alloc_name(dir, files[i].name);
1236 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1241 inode->i_fop = files[i].ops;
1242 inode->i_ino = ++sel_last_ino;
1243 d_add(dentry, inode);
1249 static ssize_t sel_read_initcon(struct file * file, char __user *buf,
1250 size_t count, loff_t *ppos)
1252 struct inode *inode;
1257 inode = file->f_path.dentry->d_inode;
1258 sid = inode->i_ino&SEL_INO_MASK;
1259 ret = security_sid_to_context(sid, &con, &len);
1263 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1268 static const struct file_operations sel_initcon_ops = {
1269 .read = sel_read_initcon,
1272 static int sel_make_initcon_files(struct dentry *dir)
1276 for (i = 1; i <= SECINITSID_NUM; i++) {
1277 struct inode *inode;
1278 struct dentry *dentry;
1279 dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
1285 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1290 inode->i_fop = &sel_initcon_ops;
1291 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1292 d_add(dentry, inode);
1298 static int sel_make_dir(struct inode *dir, struct dentry *dentry)
1301 struct inode *inode;
1303 inode = sel_make_inode(dir->i_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1308 inode->i_op = &simple_dir_inode_operations;
1309 inode->i_fop = &simple_dir_operations;
1310 inode->i_ino = ++sel_last_ino;
1311 /* directory inodes start off with i_nlink == 2 (for "." entry) */
1313 d_add(dentry, inode);
1314 /* bump link count on parent directory, too */
1320 static int sel_fill_super(struct super_block * sb, void * data, int silent)
1323 struct dentry *dentry;
1324 struct inode *inode, *root_inode;
1325 struct inode_security_struct *isec;
1327 static struct tree_descr selinux_files[] = {
1328 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1329 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
1330 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
1331 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1332 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1333 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1334 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1335 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1336 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1337 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1338 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1339 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1340 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
1341 [SEL_COMPAT_NET] = {"compat_net", &sel_compat_net_ops, S_IRUGO|S_IWUSR},
1344 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1348 root_inode = sb->s_root->d_inode;
1350 dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME);
1356 ret = sel_make_dir(root_inode, dentry);
1362 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
1368 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
1373 inode->i_ino = ++sel_last_ino;
1374 isec = (struct inode_security_struct*)inode->i_security;
1375 isec->sid = SECINITSID_DEVNULL;
1376 isec->sclass = SECCLASS_CHR_FILE;
1377 isec->initialized = 1;
1379 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1380 d_add(dentry, inode);
1381 selinux_null = dentry;
1383 dentry = d_alloc_name(sb->s_root, "avc");
1389 ret = sel_make_dir(root_inode, dentry);
1393 ret = sel_make_avc_files(dentry);
1397 dentry = d_alloc_name(sb->s_root, "initial_contexts");
1403 ret = sel_make_dir(root_inode, dentry);
1407 ret = sel_make_initcon_files(dentry);
1414 printk(KERN_ERR "%s: failed while creating inodes\n", __FUNCTION__);
1418 static int sel_get_sb(struct file_system_type *fs_type,
1419 int flags, const char *dev_name, void *data,
1420 struct vfsmount *mnt)
1422 return get_sb_single(fs_type, flags, data, sel_fill_super, mnt);
1425 static struct file_system_type sel_fs_type = {
1426 .name = "selinuxfs",
1427 .get_sb = sel_get_sb,
1428 .kill_sb = kill_litter_super,
1431 struct vfsmount *selinuxfs_mount;
1433 static int __init init_sel_fs(void)
1437 if (!selinux_enabled)
1439 err = register_filesystem(&sel_fs_type);
1441 selinuxfs_mount = kern_mount(&sel_fs_type);
1442 if (IS_ERR(selinuxfs_mount)) {
1443 printk(KERN_ERR "selinuxfs: could not mount!\n");
1444 err = PTR_ERR(selinuxfs_mount);
1445 selinuxfs_mount = NULL;
1451 __initcall(init_sel_fs);
1453 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
1454 void exit_sel_fs(void)
1456 unregister_filesystem(&sel_fs_type);