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_AVC, /* AVC management directory */
100 SEL_MEMBER, /* compute polyinstantiation membership decision */
101 SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
102 SEL_COMPAT_NET, /* whether to use old compat network packet controls */
105 #define SEL_INITCON_INO_OFFSET 0x01000000
106 #define SEL_INO_MASK 0x00ffffff
109 static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
110 size_t count, loff_t *ppos)
112 char tmpbuf[TMPBUFLEN];
115 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing);
116 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
119 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
120 static ssize_t sel_write_enforce(struct file * file, const char __user * buf,
121 size_t count, loff_t *ppos)
128 if (count >= PAGE_SIZE)
131 /* No partial writes. */
134 page = (char*)get_zeroed_page(GFP_KERNEL);
138 if (copy_from_user(page, buf, count))
142 if (sscanf(page, "%d", &new_value) != 1)
145 if (new_value != selinux_enforcing) {
146 length = task_has_security(current, SECURITY__SETENFORCE);
149 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
150 "enforcing=%d old_enforcing=%d auid=%u", new_value,
152 audit_get_loginuid(current->audit_context));
153 selinux_enforcing = new_value;
154 if (selinux_enforcing)
156 selnl_notify_setenforce(selinux_enforcing);
160 free_page((unsigned long) page);
164 #define sel_write_enforce NULL
167 static const struct file_operations sel_enforce_ops = {
168 .read = sel_read_enforce,
169 .write = sel_write_enforce,
172 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
173 static ssize_t sel_write_disable(struct file * file, const char __user * buf,
174 size_t count, loff_t *ppos)
180 extern int selinux_disable(void);
182 if (count >= PAGE_SIZE)
185 /* No partial writes. */
188 page = (char*)get_zeroed_page(GFP_KERNEL);
192 if (copy_from_user(page, buf, count))
196 if (sscanf(page, "%d", &new_value) != 1)
200 length = selinux_disable();
203 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
205 audit_get_loginuid(current->audit_context));
210 free_page((unsigned long) page);
214 #define sel_write_disable NULL
217 static const struct file_operations sel_disable_ops = {
218 .write = sel_write_disable,
221 static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
222 size_t count, loff_t *ppos)
224 char tmpbuf[TMPBUFLEN];
227 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
228 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
231 static const struct file_operations sel_policyvers_ops = {
232 .read = sel_read_policyvers,
235 /* declaration for sel_write_load */
236 static int sel_make_bools(void);
238 static ssize_t sel_read_mls(struct file *filp, char __user *buf,
239 size_t count, loff_t *ppos)
241 char tmpbuf[TMPBUFLEN];
244 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_mls_enabled);
245 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
248 static const struct file_operations sel_mls_ops = {
249 .read = sel_read_mls,
252 static ssize_t sel_write_load(struct file * file, const char __user * buf,
253 size_t count, loff_t *ppos)
260 mutex_lock(&sel_mutex);
262 length = task_has_security(current, SECURITY__LOAD_POLICY);
267 /* No partial writes. */
272 if ((count > 64 * 1024 * 1024)
273 || (data = vmalloc(count)) == NULL) {
279 if (copy_from_user(data, buf, count) != 0)
282 length = security_load_policy(data, count);
286 ret = sel_make_bools();
291 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
292 "policy loaded auid=%u",
293 audit_get_loginuid(current->audit_context));
295 mutex_unlock(&sel_mutex);
300 static const struct file_operations sel_load_ops = {
301 .write = sel_write_load,
304 static ssize_t sel_write_context(struct file * file, char *buf, size_t size)
310 length = task_has_security(current, SECURITY__CHECK_CONTEXT);
314 length = security_context_to_sid(buf, size, &sid);
318 length = security_sid_to_context(sid, &canon, &len);
322 if (len > SIMPLE_TRANSACTION_LIMIT) {
323 printk(KERN_ERR "%s: context size (%u) exceeds payload "
324 "max\n", __FUNCTION__, len);
329 memcpy(buf, canon, len);
336 static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
337 size_t count, loff_t *ppos)
339 char tmpbuf[TMPBUFLEN];
342 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot);
343 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
346 static ssize_t sel_write_checkreqprot(struct file * file, const char __user * buf,
347 size_t count, loff_t *ppos)
351 unsigned int new_value;
353 length = task_has_security(current, SECURITY__SETCHECKREQPROT);
357 if (count >= PAGE_SIZE)
360 /* No partial writes. */
363 page = (char*)get_zeroed_page(GFP_KERNEL);
367 if (copy_from_user(page, buf, count))
371 if (sscanf(page, "%u", &new_value) != 1)
374 selinux_checkreqprot = new_value ? 1 : 0;
377 free_page((unsigned long) page);
380 static const struct file_operations sel_checkreqprot_ops = {
381 .read = sel_read_checkreqprot,
382 .write = sel_write_checkreqprot,
385 static ssize_t sel_read_compat_net(struct file *filp, char __user *buf,
386 size_t count, loff_t *ppos)
388 char tmpbuf[TMPBUFLEN];
391 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_compat_net);
392 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
395 static ssize_t sel_write_compat_net(struct file * file, const char __user * buf,
396 size_t count, loff_t *ppos)
402 length = task_has_security(current, SECURITY__LOAD_POLICY);
406 if (count >= PAGE_SIZE)
409 /* No partial writes. */
412 page = (char*)get_zeroed_page(GFP_KERNEL);
416 if (copy_from_user(page, buf, count))
420 if (sscanf(page, "%d", &new_value) != 1)
423 selinux_compat_net = new_value ? 1 : 0;
426 free_page((unsigned long) page);
429 static const struct file_operations sel_compat_net_ops = {
430 .read = sel_read_compat_net,
431 .write = sel_write_compat_net,
435 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
437 static ssize_t sel_write_access(struct file * file, char *buf, size_t size);
438 static ssize_t sel_write_create(struct file * file, char *buf, size_t size);
439 static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size);
440 static ssize_t sel_write_user(struct file * file, char *buf, size_t size);
441 static ssize_t sel_write_member(struct file * file, char *buf, size_t size);
443 static ssize_t (*write_op[])(struct file *, char *, size_t) = {
444 [SEL_ACCESS] = sel_write_access,
445 [SEL_CREATE] = sel_write_create,
446 [SEL_RELABEL] = sel_write_relabel,
447 [SEL_USER] = sel_write_user,
448 [SEL_MEMBER] = sel_write_member,
449 [SEL_CONTEXT] = sel_write_context,
452 static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
454 ino_t ino = file->f_path.dentry->d_inode->i_ino;
458 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
461 data = simple_transaction_get(file, buf, size);
463 return PTR_ERR(data);
465 rv = write_op[ino](file, data, size);
467 simple_transaction_set(file, rv);
473 static const struct file_operations transaction_ops = {
474 .write = selinux_transaction_write,
475 .read = simple_transaction_read,
476 .release = simple_transaction_release,
480 * payload - write methods
481 * If the method has a response, the response should be put in buf,
482 * and the length returned. Otherwise return 0 or and -error.
485 static ssize_t sel_write_access(struct file * file, char *buf, size_t size)
491 struct av_decision avd;
494 length = task_has_security(current, SECURITY__COMPUTE_AV);
499 scon = kzalloc(size+1, GFP_KERNEL);
503 tcon = kzalloc(size+1, GFP_KERNEL);
508 if (sscanf(buf, "%s %s %hu %x", scon, tcon, &tclass, &req) != 4)
511 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
514 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
518 length = security_compute_av(ssid, tsid, tclass, req, &avd);
522 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
524 avd.allowed, avd.decided,
525 avd.auditallow, avd.auditdeny,
534 static ssize_t sel_write_create(struct file * file, char *buf, size_t size)
537 u32 ssid, tsid, newsid;
543 length = task_has_security(current, SECURITY__COMPUTE_CREATE);
548 scon = kzalloc(size+1, GFP_KERNEL);
552 tcon = kzalloc(size+1, GFP_KERNEL);
557 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
560 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
563 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
567 length = security_transition_sid(ssid, tsid, tclass, &newsid);
571 length = security_sid_to_context(newsid, &newcon, &len);
575 if (len > SIMPLE_TRANSACTION_LIMIT) {
576 printk(KERN_ERR "%s: context size (%u) exceeds payload "
577 "max\n", __FUNCTION__, len);
582 memcpy(buf, newcon, len);
593 static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size)
596 u32 ssid, tsid, newsid;
602 length = task_has_security(current, SECURITY__COMPUTE_RELABEL);
607 scon = kzalloc(size+1, GFP_KERNEL);
611 tcon = kzalloc(size+1, GFP_KERNEL);
616 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
619 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
622 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
626 length = security_change_sid(ssid, tsid, tclass, &newsid);
630 length = security_sid_to_context(newsid, &newcon, &len);
634 if (len > SIMPLE_TRANSACTION_LIMIT) {
639 memcpy(buf, newcon, len);
650 static ssize_t sel_write_user(struct file * file, char *buf, size_t size)
652 char *con, *user, *ptr;
659 length = task_has_security(current, SECURITY__COMPUTE_USER);
664 con = kzalloc(size+1, GFP_KERNEL);
668 user = kzalloc(size+1, GFP_KERNEL);
673 if (sscanf(buf, "%s %s", con, user) != 2)
676 length = security_context_to_sid(con, strlen(con)+1, &sid);
680 length = security_get_user_sids(sid, user, &sids, &nsids);
684 length = sprintf(buf, "%u", nsids) + 1;
686 for (i = 0; i < nsids; i++) {
687 rc = security_sid_to_context(sids[i], &newcon, &len);
692 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
697 memcpy(ptr, newcon, len);
711 static ssize_t sel_write_member(struct file * file, char *buf, size_t size)
714 u32 ssid, tsid, newsid;
720 length = task_has_security(current, SECURITY__COMPUTE_MEMBER);
725 scon = kzalloc(size+1, GFP_KERNEL);
729 tcon = kzalloc(size+1, GFP_KERNEL);
734 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
737 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
740 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
744 length = security_member_sid(ssid, tsid, tclass, &newsid);
748 length = security_sid_to_context(newsid, &newcon, &len);
752 if (len > SIMPLE_TRANSACTION_LIMIT) {
753 printk(KERN_ERR "%s: context size (%u) exceeds payload "
754 "max\n", __FUNCTION__, len);
759 memcpy(buf, newcon, len);
770 static struct inode *sel_make_inode(struct super_block *sb, int mode)
772 struct inode *ret = new_inode(sb);
776 ret->i_uid = ret->i_gid = 0;
778 ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
783 #define BOOL_INO_OFFSET 30
785 static ssize_t sel_read_bool(struct file *filep, char __user *buf,
786 size_t count, loff_t *ppos)
794 mutex_lock(&sel_mutex);
798 /* check to see if this file has been deleted */
802 if (count > PAGE_SIZE) {
806 if (!(page = (char*)get_zeroed_page(GFP_KERNEL))) {
811 inode = filep->f_path.dentry->d_inode;
812 cur_enforcing = security_get_bool_value(inode->i_ino - BOOL_INO_OFFSET);
813 if (cur_enforcing < 0) {
818 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
819 bool_pending_values[inode->i_ino - BOOL_INO_OFFSET]);
820 ret = simple_read_from_buffer(buf, count, ppos, page, length);
822 mutex_unlock(&sel_mutex);
824 free_page((unsigned long)page);
828 static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
829 size_t count, loff_t *ppos)
832 ssize_t length = -EFAULT;
836 mutex_lock(&sel_mutex);
838 length = task_has_security(current, SECURITY__SETBOOL);
842 /* check to see if this file has been deleted */
846 if (count >= PAGE_SIZE) {
851 /* No partial writes. */
854 page = (char*)get_zeroed_page(GFP_KERNEL);
860 if (copy_from_user(page, buf, count))
864 if (sscanf(page, "%d", &new_value) != 1)
870 inode = filep->f_path.dentry->d_inode;
871 bool_pending_values[inode->i_ino - BOOL_INO_OFFSET] = new_value;
875 mutex_unlock(&sel_mutex);
877 free_page((unsigned long) page);
881 static const struct file_operations sel_bool_ops = {
882 .read = sel_read_bool,
883 .write = sel_write_bool,
886 static ssize_t sel_commit_bools_write(struct file *filep,
887 const char __user *buf,
888 size_t count, loff_t *ppos)
891 ssize_t length = -EFAULT;
894 mutex_lock(&sel_mutex);
896 length = task_has_security(current, SECURITY__SETBOOL);
900 /* check to see if this file has been deleted */
904 if (count >= PAGE_SIZE) {
909 /* No partial writes. */
912 page = (char*)get_zeroed_page(GFP_KERNEL);
918 if (copy_from_user(page, buf, count))
922 if (sscanf(page, "%d", &new_value) != 1)
925 if (new_value && bool_pending_values) {
926 security_set_bools(bool_num, bool_pending_values);
932 mutex_unlock(&sel_mutex);
934 free_page((unsigned long) page);
938 static const struct file_operations sel_commit_bools_ops = {
939 .write = sel_commit_bools_write,
942 /* delete booleans - partial revoke() from
943 * fs/proc/generic.c proc_kill_inodes */
944 static void sel_remove_bools(struct dentry *de)
946 struct list_head *p, *node;
947 struct super_block *sb = de->d_sb;
949 spin_lock(&dcache_lock);
950 node = de->d_subdirs.next;
951 while (node != &de->d_subdirs) {
952 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
957 spin_unlock(&dcache_lock);
959 simple_unlink(de->d_inode, d);
961 spin_lock(&dcache_lock);
963 node = de->d_subdirs.next;
966 spin_unlock(&dcache_lock);
969 list_for_each(p, &sb->s_files) {
970 struct file * filp = list_entry(p, struct file, f_u.fu_list);
971 struct dentry * dentry = filp->f_path.dentry;
973 if (dentry->d_parent != de) {
981 #define BOOL_DIR_NAME "booleans"
983 static int sel_make_bools(void)
987 struct dentry *dentry = NULL;
988 struct dentry *dir = bool_dir;
989 struct inode *inode = NULL;
990 struct inode_security_struct *isec;
991 char **names = NULL, *page;
996 /* remove any existing files */
997 kfree(bool_pending_values);
998 bool_pending_values = NULL;
1000 sel_remove_bools(dir);
1002 if (!(page = (char*)get_zeroed_page(GFP_KERNEL)))
1005 ret = security_get_bools(&num, &names, &values);
1009 for (i = 0; i < num; i++) {
1010 dentry = d_alloc_name(dir, names[i]);
1015 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1021 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
1025 } else if (len >= PAGE_SIZE) {
1026 ret = -ENAMETOOLONG;
1029 isec = (struct inode_security_struct*)inode->i_security;
1030 if ((ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid)))
1033 isec->initialized = 1;
1034 inode->i_fop = &sel_bool_ops;
1035 inode->i_ino = i + BOOL_INO_OFFSET;
1036 d_add(dentry, inode);
1039 bool_pending_values = values;
1041 free_page((unsigned long)page);
1043 for (i = 0; i < num; i++)
1050 sel_remove_bools(dir);
1055 #define NULL_FILE_NAME "null"
1057 struct dentry *selinux_null = NULL;
1059 static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1060 size_t count, loff_t *ppos)
1062 char tmpbuf[TMPBUFLEN];
1065 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
1066 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1069 static ssize_t sel_write_avc_cache_threshold(struct file * file,
1070 const char __user * buf,
1071 size_t count, loff_t *ppos)
1078 if (count >= PAGE_SIZE) {
1084 /* No partial writes. */
1089 page = (char*)get_zeroed_page(GFP_KERNEL);
1095 if (copy_from_user(page, buf, count)) {
1100 if (sscanf(page, "%u", &new_value) != 1) {
1105 if (new_value != avc_cache_threshold) {
1106 ret = task_has_security(current, SECURITY__SETSECPARAM);
1109 avc_cache_threshold = new_value;
1113 free_page((unsigned long)page);
1118 static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1119 size_t count, loff_t *ppos)
1124 page = (char *)__get_free_page(GFP_KERNEL);
1129 ret = avc_get_hash_stats(page);
1131 ret = simple_read_from_buffer(buf, count, ppos, page, ret);
1132 free_page((unsigned long)page);
1137 static const struct file_operations sel_avc_cache_threshold_ops = {
1138 .read = sel_read_avc_cache_threshold,
1139 .write = sel_write_avc_cache_threshold,
1142 static const struct file_operations sel_avc_hash_stats_ops = {
1143 .read = sel_read_avc_hash_stats,
1146 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1147 static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1151 for (cpu = *idx; cpu < NR_CPUS; ++cpu) {
1152 if (!cpu_possible(cpu))
1155 return &per_cpu(avc_cache_stats, cpu);
1160 static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1162 loff_t n = *pos - 1;
1165 return SEQ_START_TOKEN;
1167 return sel_avc_get_stat_idx(&n);
1170 static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1172 return sel_avc_get_stat_idx(pos);
1175 static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1177 struct avc_cache_stats *st = v;
1179 if (v == SEQ_START_TOKEN)
1180 seq_printf(seq, "lookups hits misses allocations reclaims "
1183 seq_printf(seq, "%u %u %u %u %u %u\n", st->lookups,
1184 st->hits, st->misses, st->allocations,
1185 st->reclaims, st->frees);
1189 static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1192 static struct seq_operations sel_avc_cache_stats_seq_ops = {
1193 .start = sel_avc_stats_seq_start,
1194 .next = sel_avc_stats_seq_next,
1195 .show = sel_avc_stats_seq_show,
1196 .stop = sel_avc_stats_seq_stop,
1199 static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1201 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1204 static const struct file_operations sel_avc_cache_stats_ops = {
1205 .open = sel_open_avc_cache_stats,
1207 .llseek = seq_lseek,
1208 .release = seq_release,
1212 static int sel_make_avc_files(struct dentry *dir)
1215 static struct tree_descr files[] = {
1216 { "cache_threshold",
1217 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1218 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1219 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1220 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1224 for (i = 0; i < ARRAY_SIZE(files); i++) {
1225 struct inode *inode;
1226 struct dentry *dentry;
1228 dentry = d_alloc_name(dir, files[i].name);
1234 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1239 inode->i_fop = files[i].ops;
1240 d_add(dentry, inode);
1246 static ssize_t sel_read_initcon(struct file * file, char __user *buf,
1247 size_t count, loff_t *ppos)
1249 struct inode *inode;
1254 inode = file->f_path.dentry->d_inode;
1255 sid = inode->i_ino&SEL_INO_MASK;
1256 ret = security_sid_to_context(sid, &con, &len);
1260 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1265 static const struct file_operations sel_initcon_ops = {
1266 .read = sel_read_initcon,
1269 static int sel_make_initcon_files(struct dentry *dir)
1273 for (i = 1; i <= SECINITSID_NUM; i++) {
1274 struct inode *inode;
1275 struct dentry *dentry;
1276 dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
1282 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1287 inode->i_fop = &sel_initcon_ops;
1288 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1289 d_add(dentry, inode);
1295 static int sel_make_dir(struct inode *dir, struct dentry *dentry)
1298 struct inode *inode;
1300 inode = sel_make_inode(dir->i_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1305 inode->i_op = &simple_dir_inode_operations;
1306 inode->i_fop = &simple_dir_operations;
1307 /* directory inodes start off with i_nlink == 2 (for "." entry) */
1309 d_add(dentry, inode);
1310 /* bump link count on parent directory, too */
1316 static int sel_fill_super(struct super_block * sb, void * data, int silent)
1319 struct dentry *dentry;
1320 struct inode *inode, *root_inode;
1321 struct inode_security_struct *isec;
1323 static struct tree_descr selinux_files[] = {
1324 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1325 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
1326 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
1327 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1328 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1329 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1330 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1331 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1332 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1333 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1334 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1335 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1336 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
1337 [SEL_COMPAT_NET] = {"compat_net", &sel_compat_net_ops, S_IRUGO|S_IWUSR},
1340 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1344 root_inode = sb->s_root->d_inode;
1346 dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME);
1352 ret = sel_make_dir(root_inode, dentry);
1358 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
1364 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
1369 isec = (struct inode_security_struct*)inode->i_security;
1370 isec->sid = SECINITSID_DEVNULL;
1371 isec->sclass = SECCLASS_CHR_FILE;
1372 isec->initialized = 1;
1374 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1375 d_add(dentry, inode);
1376 selinux_null = dentry;
1378 dentry = d_alloc_name(sb->s_root, "avc");
1384 ret = sel_make_dir(root_inode, dentry);
1388 ret = sel_make_avc_files(dentry);
1392 dentry = d_alloc_name(sb->s_root, "initial_contexts");
1398 ret = sel_make_dir(root_inode, dentry);
1402 ret = sel_make_initcon_files(dentry);
1409 printk(KERN_ERR "%s: failed while creating inodes\n", __FUNCTION__);
1413 static int sel_get_sb(struct file_system_type *fs_type,
1414 int flags, const char *dev_name, void *data,
1415 struct vfsmount *mnt)
1417 return get_sb_single(fs_type, flags, data, sel_fill_super, mnt);
1420 static struct file_system_type sel_fs_type = {
1421 .name = "selinuxfs",
1422 .get_sb = sel_get_sb,
1423 .kill_sb = kill_litter_super,
1426 struct vfsmount *selinuxfs_mount;
1428 static int __init init_sel_fs(void)
1432 if (!selinux_enabled)
1434 err = register_filesystem(&sel_fs_type);
1436 selinuxfs_mount = kern_mount(&sel_fs_type);
1437 if (IS_ERR(selinuxfs_mount)) {
1438 printk(KERN_ERR "selinuxfs: could not mount!\n");
1439 err = PTR_ERR(selinuxfs_mount);
1440 selinuxfs_mount = NULL;
1446 __initcall(init_sel_fs);
1448 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
1449 void exit_sel_fs(void)
1451 unregister_filesystem(&sel_fs_type);