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 /* global data for classes */
71 static struct dentry *class_dir = NULL;
72 static unsigned long last_class_ino;
74 extern void selnl_notify_setenforce(int val);
76 /* Check whether a task is allowed to use a security operation. */
77 static int task_has_security(struct task_struct *tsk,
80 struct task_security_struct *tsec;
86 return avc_has_perm(tsec->sid, SECINITSID_SECURITY,
87 SECCLASS_SECURITY, perms, NULL);
92 SEL_LOAD, /* load policy */
93 SEL_ENFORCE, /* get or set enforcing status */
94 SEL_CONTEXT, /* validate context */
95 SEL_ACCESS, /* compute access decision */
96 SEL_CREATE, /* compute create labeling decision */
97 SEL_RELABEL, /* compute relabeling decision */
98 SEL_USER, /* compute reachable user contexts */
99 SEL_POLICYVERS, /* return policy version for this kernel */
100 SEL_COMMIT_BOOLS, /* commit new boolean values */
101 SEL_MLS, /* return if MLS policy is enabled */
102 SEL_DISABLE, /* disable SELinux until next reboot */
103 SEL_MEMBER, /* compute polyinstantiation membership decision */
104 SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
105 SEL_COMPAT_NET, /* whether to use old compat network packet controls */
106 SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */
107 SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */
108 SEL_INO_NEXT, /* The next inode number to use */
111 static unsigned long sel_last_ino = SEL_INO_NEXT - 1;
113 #define SEL_INITCON_INO_OFFSET 0x01000000
114 #define SEL_BOOL_INO_OFFSET 0x02000000
115 #define SEL_CLASS_INO_OFFSET 0x04000000
116 #define SEL_INO_MASK 0x00ffffff
119 static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
120 size_t count, loff_t *ppos)
122 char tmpbuf[TMPBUFLEN];
125 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing);
126 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
129 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
130 static ssize_t sel_write_enforce(struct file * file, const char __user * buf,
131 size_t count, loff_t *ppos)
138 if (count >= PAGE_SIZE)
141 /* No partial writes. */
144 page = (char*)get_zeroed_page(GFP_KERNEL);
148 if (copy_from_user(page, buf, count))
152 if (sscanf(page, "%d", &new_value) != 1)
155 if (new_value != selinux_enforcing) {
156 length = task_has_security(current, SECURITY__SETENFORCE);
159 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
160 "enforcing=%d old_enforcing=%d auid=%u", new_value,
162 audit_get_loginuid(current->audit_context));
163 selinux_enforcing = new_value;
164 if (selinux_enforcing)
166 selnl_notify_setenforce(selinux_enforcing);
170 free_page((unsigned long) page);
174 #define sel_write_enforce NULL
177 static const struct file_operations sel_enforce_ops = {
178 .read = sel_read_enforce,
179 .write = sel_write_enforce,
182 static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf,
183 size_t count, loff_t *ppos)
185 char tmpbuf[TMPBUFLEN];
187 ino_t ino = filp->f_path.dentry->d_inode->i_ino;
188 int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
189 security_get_reject_unknown() : !security_get_allow_unknown();
191 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
192 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
195 static const struct file_operations sel_handle_unknown_ops = {
196 .read = sel_read_handle_unknown,
199 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
200 static ssize_t sel_write_disable(struct file * file, const char __user * buf,
201 size_t count, loff_t *ppos)
207 extern int selinux_disable(void);
209 if (count >= PAGE_SIZE)
212 /* No partial writes. */
215 page = (char*)get_zeroed_page(GFP_KERNEL);
219 if (copy_from_user(page, buf, count))
223 if (sscanf(page, "%d", &new_value) != 1)
227 length = selinux_disable();
230 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
232 audit_get_loginuid(current->audit_context));
237 free_page((unsigned long) page);
241 #define sel_write_disable NULL
244 static const struct file_operations sel_disable_ops = {
245 .write = sel_write_disable,
248 static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
249 size_t count, loff_t *ppos)
251 char tmpbuf[TMPBUFLEN];
254 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
255 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
258 static const struct file_operations sel_policyvers_ops = {
259 .read = sel_read_policyvers,
262 /* declaration for sel_write_load */
263 static int sel_make_bools(void);
264 static int sel_make_classes(void);
266 /* declaration for sel_make_class_dirs */
267 static int sel_make_dir(struct inode *dir, struct dentry *dentry,
270 static ssize_t sel_read_mls(struct file *filp, char __user *buf,
271 size_t count, loff_t *ppos)
273 char tmpbuf[TMPBUFLEN];
276 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_mls_enabled);
277 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
280 static const struct file_operations sel_mls_ops = {
281 .read = sel_read_mls,
284 static ssize_t sel_write_load(struct file * file, const char __user * buf,
285 size_t count, loff_t *ppos)
292 mutex_lock(&sel_mutex);
294 length = task_has_security(current, SECURITY__LOAD_POLICY);
299 /* No partial writes. */
304 if ((count > 64 * 1024 * 1024)
305 || (data = vmalloc(count)) == NULL) {
311 if (copy_from_user(data, buf, count) != 0)
314 length = security_load_policy(data, count);
318 ret = sel_make_bools();
324 ret = sel_make_classes();
332 printk(KERN_INFO "SELinux: policy loaded with handle_unknown=%s\n",
333 (security_get_reject_unknown() ? "reject" :
334 (security_get_allow_unknown() ? "allow" : "deny")));
336 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
337 "policy loaded auid=%u",
338 audit_get_loginuid(current->audit_context));
340 mutex_unlock(&sel_mutex);
345 static const struct file_operations sel_load_ops = {
346 .write = sel_write_load,
349 static ssize_t sel_write_context(struct file * file, char *buf, size_t size)
355 length = task_has_security(current, SECURITY__CHECK_CONTEXT);
359 length = security_context_to_sid(buf, size, &sid);
363 length = security_sid_to_context(sid, &canon, &len);
367 if (len > SIMPLE_TRANSACTION_LIMIT) {
368 printk(KERN_ERR "%s: context size (%u) exceeds payload "
369 "max\n", __FUNCTION__, len);
374 memcpy(buf, canon, len);
381 static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
382 size_t count, loff_t *ppos)
384 char tmpbuf[TMPBUFLEN];
387 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot);
388 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
391 static ssize_t sel_write_checkreqprot(struct file * file, const char __user * buf,
392 size_t count, loff_t *ppos)
396 unsigned int new_value;
398 length = task_has_security(current, SECURITY__SETCHECKREQPROT);
402 if (count >= PAGE_SIZE)
405 /* No partial writes. */
408 page = (char*)get_zeroed_page(GFP_KERNEL);
412 if (copy_from_user(page, buf, count))
416 if (sscanf(page, "%u", &new_value) != 1)
419 selinux_checkreqprot = new_value ? 1 : 0;
422 free_page((unsigned long) page);
425 static const struct file_operations sel_checkreqprot_ops = {
426 .read = sel_read_checkreqprot,
427 .write = sel_write_checkreqprot,
430 static ssize_t sel_read_compat_net(struct file *filp, char __user *buf,
431 size_t count, loff_t *ppos)
433 char tmpbuf[TMPBUFLEN];
436 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_compat_net);
437 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
440 static ssize_t sel_write_compat_net(struct file * file, const char __user * buf,
441 size_t count, loff_t *ppos)
447 length = task_has_security(current, SECURITY__LOAD_POLICY);
451 if (count >= PAGE_SIZE)
454 /* No partial writes. */
457 page = (char*)get_zeroed_page(GFP_KERNEL);
461 if (copy_from_user(page, buf, count))
465 if (sscanf(page, "%d", &new_value) != 1)
468 selinux_compat_net = new_value ? 1 : 0;
471 free_page((unsigned long) page);
474 static const struct file_operations sel_compat_net_ops = {
475 .read = sel_read_compat_net,
476 .write = sel_write_compat_net,
480 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
482 static ssize_t sel_write_access(struct file * file, char *buf, size_t size);
483 static ssize_t sel_write_create(struct file * file, char *buf, size_t size);
484 static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size);
485 static ssize_t sel_write_user(struct file * file, char *buf, size_t size);
486 static ssize_t sel_write_member(struct file * file, char *buf, size_t size);
488 static ssize_t (*write_op[])(struct file *, char *, size_t) = {
489 [SEL_ACCESS] = sel_write_access,
490 [SEL_CREATE] = sel_write_create,
491 [SEL_RELABEL] = sel_write_relabel,
492 [SEL_USER] = sel_write_user,
493 [SEL_MEMBER] = sel_write_member,
494 [SEL_CONTEXT] = sel_write_context,
497 static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
499 ino_t ino = file->f_path.dentry->d_inode->i_ino;
503 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
506 data = simple_transaction_get(file, buf, size);
508 return PTR_ERR(data);
510 rv = write_op[ino](file, data, size);
512 simple_transaction_set(file, rv);
518 static const struct file_operations transaction_ops = {
519 .write = selinux_transaction_write,
520 .read = simple_transaction_read,
521 .release = simple_transaction_release,
525 * payload - write methods
526 * If the method has a response, the response should be put in buf,
527 * and the length returned. Otherwise return 0 or and -error.
530 static ssize_t sel_write_access(struct file * file, char *buf, size_t size)
536 struct av_decision avd;
539 length = task_has_security(current, SECURITY__COMPUTE_AV);
544 scon = kzalloc(size+1, GFP_KERNEL);
548 tcon = kzalloc(size+1, GFP_KERNEL);
553 if (sscanf(buf, "%s %s %hu %x", scon, tcon, &tclass, &req) != 4)
556 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
559 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
563 length = security_compute_av(ssid, tsid, tclass, req, &avd);
567 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
569 avd.allowed, avd.decided,
570 avd.auditallow, avd.auditdeny,
579 static ssize_t sel_write_create(struct file * file, char *buf, size_t size)
582 u32 ssid, tsid, newsid;
588 length = task_has_security(current, SECURITY__COMPUTE_CREATE);
593 scon = kzalloc(size+1, GFP_KERNEL);
597 tcon = kzalloc(size+1, GFP_KERNEL);
602 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
605 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
608 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
612 length = security_transition_sid(ssid, tsid, tclass, &newsid);
616 length = security_sid_to_context(newsid, &newcon, &len);
620 if (len > SIMPLE_TRANSACTION_LIMIT) {
621 printk(KERN_ERR "%s: context size (%u) exceeds payload "
622 "max\n", __FUNCTION__, len);
627 memcpy(buf, newcon, len);
638 static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size)
641 u32 ssid, tsid, newsid;
647 length = task_has_security(current, SECURITY__COMPUTE_RELABEL);
652 scon = kzalloc(size+1, GFP_KERNEL);
656 tcon = kzalloc(size+1, GFP_KERNEL);
661 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
664 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
667 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
671 length = security_change_sid(ssid, tsid, tclass, &newsid);
675 length = security_sid_to_context(newsid, &newcon, &len);
679 if (len > SIMPLE_TRANSACTION_LIMIT) {
684 memcpy(buf, newcon, len);
695 static ssize_t sel_write_user(struct file * file, char *buf, size_t size)
697 char *con, *user, *ptr;
704 length = task_has_security(current, SECURITY__COMPUTE_USER);
709 con = kzalloc(size+1, GFP_KERNEL);
713 user = kzalloc(size+1, GFP_KERNEL);
718 if (sscanf(buf, "%s %s", con, user) != 2)
721 length = security_context_to_sid(con, strlen(con)+1, &sid);
725 length = security_get_user_sids(sid, user, &sids, &nsids);
729 length = sprintf(buf, "%u", nsids) + 1;
731 for (i = 0; i < nsids; i++) {
732 rc = security_sid_to_context(sids[i], &newcon, &len);
737 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
742 memcpy(ptr, newcon, len);
756 static ssize_t sel_write_member(struct file * file, char *buf, size_t size)
759 u32 ssid, tsid, newsid;
765 length = task_has_security(current, SECURITY__COMPUTE_MEMBER);
770 scon = kzalloc(size+1, GFP_KERNEL);
774 tcon = kzalloc(size+1, GFP_KERNEL);
779 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
782 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
785 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
789 length = security_member_sid(ssid, tsid, tclass, &newsid);
793 length = security_sid_to_context(newsid, &newcon, &len);
797 if (len > SIMPLE_TRANSACTION_LIMIT) {
798 printk(KERN_ERR "%s: context size (%u) exceeds payload "
799 "max\n", __FUNCTION__, len);
804 memcpy(buf, newcon, len);
815 static struct inode *sel_make_inode(struct super_block *sb, int mode)
817 struct inode *ret = new_inode(sb);
821 ret->i_uid = ret->i_gid = 0;
823 ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
828 static ssize_t sel_read_bool(struct file *filep, char __user *buf,
829 size_t count, loff_t *ppos)
837 mutex_lock(&sel_mutex);
841 if (count > PAGE_SIZE) {
845 if (!(page = (char*)get_zeroed_page(GFP_KERNEL))) {
850 inode = filep->f_path.dentry->d_inode;
851 cur_enforcing = security_get_bool_value(inode->i_ino&SEL_INO_MASK);
852 if (cur_enforcing < 0) {
857 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
858 bool_pending_values[inode->i_ino&SEL_INO_MASK]);
859 ret = simple_read_from_buffer(buf, count, ppos, page, length);
861 mutex_unlock(&sel_mutex);
863 free_page((unsigned long)page);
867 static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
868 size_t count, loff_t *ppos)
871 ssize_t length = -EFAULT;
875 mutex_lock(&sel_mutex);
877 length = task_has_security(current, SECURITY__SETBOOL);
881 if (count >= PAGE_SIZE) {
886 /* No partial writes. */
889 page = (char*)get_zeroed_page(GFP_KERNEL);
895 if (copy_from_user(page, buf, count))
899 if (sscanf(page, "%d", &new_value) != 1)
905 inode = filep->f_path.dentry->d_inode;
906 bool_pending_values[inode->i_ino&SEL_INO_MASK] = new_value;
910 mutex_unlock(&sel_mutex);
912 free_page((unsigned long) page);
916 static const struct file_operations sel_bool_ops = {
917 .read = sel_read_bool,
918 .write = sel_write_bool,
921 static ssize_t sel_commit_bools_write(struct file *filep,
922 const char __user *buf,
923 size_t count, loff_t *ppos)
926 ssize_t length = -EFAULT;
929 mutex_lock(&sel_mutex);
931 length = task_has_security(current, SECURITY__SETBOOL);
935 if (count >= PAGE_SIZE) {
940 /* No partial writes. */
943 page = (char*)get_zeroed_page(GFP_KERNEL);
949 if (copy_from_user(page, buf, count))
953 if (sscanf(page, "%d", &new_value) != 1)
956 if (new_value && bool_pending_values) {
957 security_set_bools(bool_num, bool_pending_values);
963 mutex_unlock(&sel_mutex);
965 free_page((unsigned long) page);
969 static const struct file_operations sel_commit_bools_ops = {
970 .write = sel_commit_bools_write,
973 static void sel_remove_entries(struct dentry *de)
975 struct list_head *node;
977 spin_lock(&dcache_lock);
978 node = de->d_subdirs.next;
979 while (node != &de->d_subdirs) {
980 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
985 spin_unlock(&dcache_lock);
987 simple_unlink(de->d_inode, d);
989 spin_lock(&dcache_lock);
991 node = de->d_subdirs.next;
994 spin_unlock(&dcache_lock);
997 #define BOOL_DIR_NAME "booleans"
999 static int sel_make_bools(void)
1003 struct dentry *dentry = NULL;
1004 struct dentry *dir = bool_dir;
1005 struct inode *inode = NULL;
1006 struct inode_security_struct *isec;
1007 char **names = NULL, *page;
1012 /* remove any existing files */
1013 kfree(bool_pending_values);
1014 bool_pending_values = NULL;
1016 sel_remove_entries(dir);
1018 if (!(page = (char*)get_zeroed_page(GFP_KERNEL)))
1021 ret = security_get_bools(&num, &names, &values);
1025 for (i = 0; i < num; i++) {
1026 dentry = d_alloc_name(dir, names[i]);
1031 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1037 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
1041 } else if (len >= PAGE_SIZE) {
1042 ret = -ENAMETOOLONG;
1045 isec = (struct inode_security_struct*)inode->i_security;
1046 if ((ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid)))
1049 isec->initialized = 1;
1050 inode->i_fop = &sel_bool_ops;
1051 inode->i_ino = i|SEL_BOOL_INO_OFFSET;
1052 d_add(dentry, inode);
1055 bool_pending_values = values;
1057 free_page((unsigned long)page);
1059 for (i = 0; i < num; i++)
1066 sel_remove_entries(dir);
1071 #define NULL_FILE_NAME "null"
1073 struct dentry *selinux_null = NULL;
1075 static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1076 size_t count, loff_t *ppos)
1078 char tmpbuf[TMPBUFLEN];
1081 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
1082 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1085 static ssize_t sel_write_avc_cache_threshold(struct file * file,
1086 const char __user * buf,
1087 size_t count, loff_t *ppos)
1094 if (count >= PAGE_SIZE) {
1100 /* No partial writes. */
1105 page = (char*)get_zeroed_page(GFP_KERNEL);
1111 if (copy_from_user(page, buf, count)) {
1116 if (sscanf(page, "%u", &new_value) != 1) {
1121 if (new_value != avc_cache_threshold) {
1122 ret = task_has_security(current, SECURITY__SETSECPARAM);
1125 avc_cache_threshold = new_value;
1129 free_page((unsigned long)page);
1134 static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1135 size_t count, loff_t *ppos)
1140 page = (char *)__get_free_page(GFP_KERNEL);
1145 ret = avc_get_hash_stats(page);
1147 ret = simple_read_from_buffer(buf, count, ppos, page, ret);
1148 free_page((unsigned long)page);
1153 static const struct file_operations sel_avc_cache_threshold_ops = {
1154 .read = sel_read_avc_cache_threshold,
1155 .write = sel_write_avc_cache_threshold,
1158 static const struct file_operations sel_avc_hash_stats_ops = {
1159 .read = sel_read_avc_hash_stats,
1162 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1163 static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1167 for (cpu = *idx; cpu < NR_CPUS; ++cpu) {
1168 if (!cpu_possible(cpu))
1171 return &per_cpu(avc_cache_stats, cpu);
1176 static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1178 loff_t n = *pos - 1;
1181 return SEQ_START_TOKEN;
1183 return sel_avc_get_stat_idx(&n);
1186 static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1188 return sel_avc_get_stat_idx(pos);
1191 static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1193 struct avc_cache_stats *st = v;
1195 if (v == SEQ_START_TOKEN)
1196 seq_printf(seq, "lookups hits misses allocations reclaims "
1199 seq_printf(seq, "%u %u %u %u %u %u\n", st->lookups,
1200 st->hits, st->misses, st->allocations,
1201 st->reclaims, st->frees);
1205 static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1208 static struct seq_operations sel_avc_cache_stats_seq_ops = {
1209 .start = sel_avc_stats_seq_start,
1210 .next = sel_avc_stats_seq_next,
1211 .show = sel_avc_stats_seq_show,
1212 .stop = sel_avc_stats_seq_stop,
1215 static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1217 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1220 static const struct file_operations sel_avc_cache_stats_ops = {
1221 .open = sel_open_avc_cache_stats,
1223 .llseek = seq_lseek,
1224 .release = seq_release,
1228 static int sel_make_avc_files(struct dentry *dir)
1231 static struct tree_descr files[] = {
1232 { "cache_threshold",
1233 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1234 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1235 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1236 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1240 for (i = 0; i < ARRAY_SIZE(files); i++) {
1241 struct inode *inode;
1242 struct dentry *dentry;
1244 dentry = d_alloc_name(dir, files[i].name);
1250 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1255 inode->i_fop = files[i].ops;
1256 inode->i_ino = ++sel_last_ino;
1257 d_add(dentry, inode);
1263 static ssize_t sel_read_initcon(struct file * file, char __user *buf,
1264 size_t count, loff_t *ppos)
1266 struct inode *inode;
1271 inode = file->f_path.dentry->d_inode;
1272 sid = inode->i_ino&SEL_INO_MASK;
1273 ret = security_sid_to_context(sid, &con, &len);
1277 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1282 static const struct file_operations sel_initcon_ops = {
1283 .read = sel_read_initcon,
1286 static int sel_make_initcon_files(struct dentry *dir)
1290 for (i = 1; i <= SECINITSID_NUM; i++) {
1291 struct inode *inode;
1292 struct dentry *dentry;
1293 dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
1299 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1304 inode->i_fop = &sel_initcon_ops;
1305 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1306 d_add(dentry, inode);
1312 static inline unsigned int sel_div(unsigned long a, unsigned long b)
1314 return a / b - (a % b < 0);
1317 static inline unsigned long sel_class_to_ino(u16 class)
1319 return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1322 static inline u16 sel_ino_to_class(unsigned long ino)
1324 return sel_div(ino & SEL_INO_MASK, SEL_VEC_MAX + 1);
1327 static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1329 return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1332 static inline u32 sel_ino_to_perm(unsigned long ino)
1334 return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1337 static ssize_t sel_read_class(struct file * file, char __user *buf,
1338 size_t count, loff_t *ppos)
1342 unsigned long ino = file->f_path.dentry->d_inode->i_ino;
1344 page = (char *)__get_free_page(GFP_KERNEL);
1350 len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_class(ino));
1351 rc = simple_read_from_buffer(buf, count, ppos, page, len);
1352 free_page((unsigned long)page);
1357 static const struct file_operations sel_class_ops = {
1358 .read = sel_read_class,
1361 static ssize_t sel_read_perm(struct file * file, char __user *buf,
1362 size_t count, loff_t *ppos)
1366 unsigned long ino = file->f_path.dentry->d_inode->i_ino;
1368 page = (char *)__get_free_page(GFP_KERNEL);
1374 len = snprintf(page, PAGE_SIZE,"%d", sel_ino_to_perm(ino));
1375 rc = simple_read_from_buffer(buf, count, ppos, page, len);
1376 free_page((unsigned long)page);
1381 static const struct file_operations sel_perm_ops = {
1382 .read = sel_read_perm,
1385 static int sel_make_perm_files(char *objclass, int classvalue,
1388 int i, rc = 0, nperms;
1391 rc = security_get_permissions(objclass, &perms, &nperms);
1395 for (i = 0; i < nperms; i++) {
1396 struct inode *inode;
1397 struct dentry *dentry;
1399 dentry = d_alloc_name(dir, perms[i]);
1405 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1410 inode->i_fop = &sel_perm_ops;
1411 /* i+1 since perm values are 1-indexed */
1412 inode->i_ino = sel_perm_to_ino(classvalue, i+1);
1413 d_add(dentry, inode);
1417 for (i = 0; i < nperms; i++)
1424 static int sel_make_class_dir_entries(char *classname, int index,
1427 struct dentry *dentry = NULL;
1428 struct inode *inode = NULL;
1431 dentry = d_alloc_name(dir, "index");
1437 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1443 inode->i_fop = &sel_class_ops;
1444 inode->i_ino = sel_class_to_ino(index);
1445 d_add(dentry, inode);
1447 dentry = d_alloc_name(dir, "perms");
1453 rc = sel_make_dir(dir->d_inode, dentry, &last_class_ino);
1457 rc = sel_make_perm_files(classname, index, dentry);
1463 static void sel_remove_classes(void)
1465 struct list_head *class_node;
1467 list_for_each(class_node, &class_dir->d_subdirs) {
1468 struct dentry *class_subdir = list_entry(class_node,
1469 struct dentry, d_u.d_child);
1470 struct list_head *class_subdir_node;
1472 list_for_each(class_subdir_node, &class_subdir->d_subdirs) {
1473 struct dentry *d = list_entry(class_subdir_node,
1474 struct dentry, d_u.d_child);
1477 if (d->d_inode->i_mode & S_IFDIR)
1478 sel_remove_entries(d);
1481 sel_remove_entries(class_subdir);
1484 sel_remove_entries(class_dir);
1487 static int sel_make_classes(void)
1489 int rc = 0, nclasses, i;
1492 /* delete any existing entries */
1493 sel_remove_classes();
1495 rc = security_get_classes(&classes, &nclasses);
1499 /* +2 since classes are 1-indexed */
1500 last_class_ino = sel_class_to_ino(nclasses+2);
1502 for (i = 0; i < nclasses; i++) {
1503 struct dentry *class_name_dir;
1505 class_name_dir = d_alloc_name(class_dir, classes[i]);
1506 if (!class_name_dir) {
1511 rc = sel_make_dir(class_dir->d_inode, class_name_dir,
1516 /* i+1 since class values are 1-indexed */
1517 rc = sel_make_class_dir_entries(classes[i], i+1,
1524 for (i = 0; i < nclasses; i++)
1531 static int sel_make_dir(struct inode *dir, struct dentry *dentry,
1535 struct inode *inode;
1537 inode = sel_make_inode(dir->i_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1542 inode->i_op = &simple_dir_inode_operations;
1543 inode->i_fop = &simple_dir_operations;
1544 inode->i_ino = ++(*ino);
1545 /* directory inodes start off with i_nlink == 2 (for "." entry) */
1547 d_add(dentry, inode);
1548 /* bump link count on parent directory, too */
1554 static int sel_fill_super(struct super_block * sb, void * data, int silent)
1557 struct dentry *dentry;
1558 struct inode *inode, *root_inode;
1559 struct inode_security_struct *isec;
1561 static struct tree_descr selinux_files[] = {
1562 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1563 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
1564 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
1565 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1566 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1567 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1568 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1569 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1570 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1571 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1572 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1573 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1574 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
1575 [SEL_COMPAT_NET] = {"compat_net", &sel_compat_net_ops, S_IRUGO|S_IWUSR},
1576 [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
1577 [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
1580 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1584 root_inode = sb->s_root->d_inode;
1586 dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME);
1592 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1598 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
1604 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
1609 inode->i_ino = ++sel_last_ino;
1610 isec = (struct inode_security_struct*)inode->i_security;
1611 isec->sid = SECINITSID_DEVNULL;
1612 isec->sclass = SECCLASS_CHR_FILE;
1613 isec->initialized = 1;
1615 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1616 d_add(dentry, inode);
1617 selinux_null = dentry;
1619 dentry = d_alloc_name(sb->s_root, "avc");
1625 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1629 ret = sel_make_avc_files(dentry);
1633 dentry = d_alloc_name(sb->s_root, "initial_contexts");
1639 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1643 ret = sel_make_initcon_files(dentry);
1647 dentry = d_alloc_name(sb->s_root, "class");
1653 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1662 printk(KERN_ERR "%s: failed while creating inodes\n", __FUNCTION__);
1666 static int sel_get_sb(struct file_system_type *fs_type,
1667 int flags, const char *dev_name, void *data,
1668 struct vfsmount *mnt)
1670 return get_sb_single(fs_type, flags, data, sel_fill_super, mnt);
1673 static struct file_system_type sel_fs_type = {
1674 .name = "selinuxfs",
1675 .get_sb = sel_get_sb,
1676 .kill_sb = kill_litter_super,
1679 struct vfsmount *selinuxfs_mount;
1681 static int __init init_sel_fs(void)
1685 if (!selinux_enabled)
1687 err = register_filesystem(&sel_fs_type);
1689 selinuxfs_mount = kern_mount(&sel_fs_type);
1690 if (IS_ERR(selinuxfs_mount)) {
1691 printk(KERN_ERR "selinuxfs: could not mount!\n");
1692 err = PTR_ERR(selinuxfs_mount);
1693 selinuxfs_mount = NULL;
1699 __initcall(init_sel_fs);
1701 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
1702 void exit_sel_fs(void)
1704 unregister_filesystem(&sel_fs_type);