1 /* Updated: Karl MacMillan <kmacmillan@tresys.com>
3 * Added conditional policy language extensions
5 * Updated: Hewlett-Packard <paul.moore@hp.com>
7 * Added support for the policy capability bitmap
9 * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
10 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
11 * Copyright (C) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, version 2.
17 #include <linux/kernel.h>
18 #include <linux/pagemap.h>
19 #include <linux/slab.h>
20 #include <linux/vmalloc.h>
22 #include <linux/mutex.h>
23 #include <linux/init.h>
24 #include <linux/string.h>
25 #include <linux/security.h>
26 #include <linux/major.h>
27 #include <linux/seq_file.h>
28 #include <linux/percpu.h>
29 #include <linux/audit.h>
30 #include <linux/uaccess.h>
32 /* selinuxfs pseudo filesystem for exporting the security policy API.
33 Based on the proc code and the fs/nfsd/nfsctl.c code. */
40 #include "conditional.h"
42 /* Policy capability filenames */
43 static char *policycap_names[] = {
44 "network_peer_controls",
48 unsigned int selinux_checkreqprot = CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
50 #ifdef CONFIG_SECURITY_SELINUX_ENABLE_SECMARK_DEFAULT
51 #define SELINUX_COMPAT_NET_VALUE 0
53 #define SELINUX_COMPAT_NET_VALUE 1
56 int selinux_compat_net = SELINUX_COMPAT_NET_VALUE;
58 static int __init checkreqprot_setup(char *str)
60 unsigned long checkreqprot;
61 if (!strict_strtoul(str, 0, &checkreqprot))
62 selinux_checkreqprot = checkreqprot ? 1 : 0;
65 __setup("checkreqprot=", checkreqprot_setup);
67 static int __init selinux_compat_net_setup(char *str)
69 unsigned long compat_net;
70 if (!strict_strtoul(str, 0, &compat_net))
71 selinux_compat_net = compat_net ? 1 : 0;
74 __setup("selinux_compat_net=", selinux_compat_net_setup);
77 static DEFINE_MUTEX(sel_mutex);
79 /* global data for booleans */
80 static struct dentry *bool_dir;
82 static char **bool_pending_names;
83 static int *bool_pending_values;
85 /* global data for classes */
86 static struct dentry *class_dir;
87 static unsigned long last_class_ino;
89 /* global data for policy capabilities */
90 static struct dentry *policycap_dir;
92 extern void selnl_notify_setenforce(int val);
94 /* Check whether a task is allowed to use a security operation. */
95 static int task_has_security(struct task_struct *tsk,
98 struct task_security_struct *tsec;
100 tsec = tsk->security;
104 return avc_has_perm(tsec->sid, SECINITSID_SECURITY,
105 SECCLASS_SECURITY, perms, NULL);
110 SEL_LOAD, /* load policy */
111 SEL_ENFORCE, /* get or set enforcing status */
112 SEL_CONTEXT, /* validate context */
113 SEL_ACCESS, /* compute access decision */
114 SEL_CREATE, /* compute create labeling decision */
115 SEL_RELABEL, /* compute relabeling decision */
116 SEL_USER, /* compute reachable user contexts */
117 SEL_POLICYVERS, /* return policy version for this kernel */
118 SEL_COMMIT_BOOLS, /* commit new boolean values */
119 SEL_MLS, /* return if MLS policy is enabled */
120 SEL_DISABLE, /* disable SELinux until next reboot */
121 SEL_MEMBER, /* compute polyinstantiation membership decision */
122 SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
123 SEL_COMPAT_NET, /* whether to use old compat network packet controls */
124 SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */
125 SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */
126 SEL_INO_NEXT, /* The next inode number to use */
129 static unsigned long sel_last_ino = SEL_INO_NEXT - 1;
131 #define SEL_INITCON_INO_OFFSET 0x01000000
132 #define SEL_BOOL_INO_OFFSET 0x02000000
133 #define SEL_CLASS_INO_OFFSET 0x04000000
134 #define SEL_POLICYCAP_INO_OFFSET 0x08000000
135 #define SEL_INO_MASK 0x00ffffff
138 static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
139 size_t count, loff_t *ppos)
141 char tmpbuf[TMPBUFLEN];
144 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing);
145 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
148 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
149 static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
150 size_t count, loff_t *ppos)
157 if (count >= PAGE_SIZE)
160 /* No partial writes. */
163 page = (char *)get_zeroed_page(GFP_KERNEL);
167 if (copy_from_user(page, buf, count))
171 if (sscanf(page, "%d", &new_value) != 1)
174 if (new_value != selinux_enforcing) {
175 length = task_has_security(current, SECURITY__SETENFORCE);
178 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
179 "enforcing=%d old_enforcing=%d auid=%u ses=%u",
180 new_value, selinux_enforcing,
181 audit_get_loginuid(current),
182 audit_get_sessionid(current));
183 selinux_enforcing = new_value;
184 if (selinux_enforcing)
186 selnl_notify_setenforce(selinux_enforcing);
190 free_page((unsigned long) page);
194 #define sel_write_enforce NULL
197 static const struct file_operations sel_enforce_ops = {
198 .read = sel_read_enforce,
199 .write = sel_write_enforce,
202 static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf,
203 size_t count, loff_t *ppos)
205 char tmpbuf[TMPBUFLEN];
207 ino_t ino = filp->f_path.dentry->d_inode->i_ino;
208 int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
209 security_get_reject_unknown() : !security_get_allow_unknown();
211 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
212 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
215 static const struct file_operations sel_handle_unknown_ops = {
216 .read = sel_read_handle_unknown,
219 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
220 static ssize_t sel_write_disable(struct file *file, const char __user *buf,
221 size_t count, loff_t *ppos)
227 extern int selinux_disable(void);
229 if (count >= PAGE_SIZE)
232 /* No partial writes. */
235 page = (char *)get_zeroed_page(GFP_KERNEL);
239 if (copy_from_user(page, buf, count))
243 if (sscanf(page, "%d", &new_value) != 1)
247 length = selinux_disable();
250 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
251 "selinux=0 auid=%u ses=%u",
252 audit_get_loginuid(current),
253 audit_get_sessionid(current));
258 free_page((unsigned long) page);
262 #define sel_write_disable NULL
265 static const struct file_operations sel_disable_ops = {
266 .write = sel_write_disable,
269 static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
270 size_t count, loff_t *ppos)
272 char tmpbuf[TMPBUFLEN];
275 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
276 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
279 static const struct file_operations sel_policyvers_ops = {
280 .read = sel_read_policyvers,
283 /* declaration for sel_write_load */
284 static int sel_make_bools(void);
285 static int sel_make_classes(void);
286 static int sel_make_policycap(void);
288 /* declaration for sel_make_class_dirs */
289 static int sel_make_dir(struct inode *dir, struct dentry *dentry,
292 static ssize_t sel_read_mls(struct file *filp, char __user *buf,
293 size_t count, loff_t *ppos)
295 char tmpbuf[TMPBUFLEN];
298 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_mls_enabled);
299 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
302 static const struct file_operations sel_mls_ops = {
303 .read = sel_read_mls,
306 static ssize_t sel_write_load(struct file *file, const char __user *buf,
307 size_t count, loff_t *ppos)
314 mutex_lock(&sel_mutex);
316 length = task_has_security(current, SECURITY__LOAD_POLICY);
321 /* No partial writes. */
326 if ((count > 64 * 1024 * 1024)
327 || (data = vmalloc(count)) == NULL) {
333 if (copy_from_user(data, buf, count) != 0)
336 length = security_load_policy(data, count);
340 ret = sel_make_bools();
346 ret = sel_make_classes();
352 ret = sel_make_policycap();
359 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
360 "policy loaded auid=%u ses=%u",
361 audit_get_loginuid(current),
362 audit_get_sessionid(current));
364 mutex_unlock(&sel_mutex);
369 static const struct file_operations sel_load_ops = {
370 .write = sel_write_load,
373 static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
379 length = task_has_security(current, SECURITY__CHECK_CONTEXT);
383 length = security_context_to_sid(buf, size, &sid);
387 length = security_sid_to_context(sid, &canon, &len);
391 if (len > SIMPLE_TRANSACTION_LIMIT) {
392 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
393 "payload max\n", __func__, len);
398 memcpy(buf, canon, len);
405 static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
406 size_t count, loff_t *ppos)
408 char tmpbuf[TMPBUFLEN];
411 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot);
412 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
415 static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
416 size_t count, loff_t *ppos)
420 unsigned int new_value;
422 length = task_has_security(current, SECURITY__SETCHECKREQPROT);
426 if (count >= PAGE_SIZE)
429 /* No partial writes. */
432 page = (char *)get_zeroed_page(GFP_KERNEL);
436 if (copy_from_user(page, buf, count))
440 if (sscanf(page, "%u", &new_value) != 1)
443 selinux_checkreqprot = new_value ? 1 : 0;
446 free_page((unsigned long) page);
449 static const struct file_operations sel_checkreqprot_ops = {
450 .read = sel_read_checkreqprot,
451 .write = sel_write_checkreqprot,
454 static ssize_t sel_read_compat_net(struct file *filp, char __user *buf,
455 size_t count, loff_t *ppos)
457 char tmpbuf[TMPBUFLEN];
460 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_compat_net);
461 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
464 static ssize_t sel_write_compat_net(struct file *file, const char __user *buf,
465 size_t count, loff_t *ppos)
471 length = task_has_security(current, SECURITY__LOAD_POLICY);
475 if (count >= PAGE_SIZE)
478 /* No partial writes. */
481 page = (char *)get_zeroed_page(GFP_KERNEL);
485 if (copy_from_user(page, buf, count))
489 if (sscanf(page, "%d", &new_value) != 1)
492 selinux_compat_net = new_value ? 1 : 0;
495 free_page((unsigned long) page);
498 static const struct file_operations sel_compat_net_ops = {
499 .read = sel_read_compat_net,
500 .write = sel_write_compat_net,
504 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
506 static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
507 static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
508 static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
509 static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
510 static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
512 static ssize_t (*write_op[])(struct file *, char *, size_t) = {
513 [SEL_ACCESS] = sel_write_access,
514 [SEL_CREATE] = sel_write_create,
515 [SEL_RELABEL] = sel_write_relabel,
516 [SEL_USER] = sel_write_user,
517 [SEL_MEMBER] = sel_write_member,
518 [SEL_CONTEXT] = sel_write_context,
521 static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
523 ino_t ino = file->f_path.dentry->d_inode->i_ino;
527 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
530 data = simple_transaction_get(file, buf, size);
532 return PTR_ERR(data);
534 rv = write_op[ino](file, data, size);
536 simple_transaction_set(file, rv);
542 static const struct file_operations transaction_ops = {
543 .write = selinux_transaction_write,
544 .read = simple_transaction_read,
545 .release = simple_transaction_release,
549 * payload - write methods
550 * If the method has a response, the response should be put in buf,
551 * and the length returned. Otherwise return 0 or and -error.
554 static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
560 struct av_decision avd;
563 length = task_has_security(current, SECURITY__COMPUTE_AV);
568 scon = kzalloc(size+1, GFP_KERNEL);
572 tcon = kzalloc(size+1, GFP_KERNEL);
577 if (sscanf(buf, "%s %s %hu %x", scon, tcon, &tclass, &req) != 4)
580 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
583 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
587 length = security_compute_av(ssid, tsid, tclass, req, &avd);
591 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
593 avd.allowed, avd.decided,
594 avd.auditallow, avd.auditdeny,
603 static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
606 u32 ssid, tsid, newsid;
612 length = task_has_security(current, SECURITY__COMPUTE_CREATE);
617 scon = kzalloc(size+1, GFP_KERNEL);
621 tcon = kzalloc(size+1, GFP_KERNEL);
626 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
629 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
632 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
636 length = security_transition_sid(ssid, tsid, tclass, &newsid);
640 length = security_sid_to_context(newsid, &newcon, &len);
644 if (len > SIMPLE_TRANSACTION_LIMIT) {
645 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
646 "payload max\n", __func__, len);
651 memcpy(buf, newcon, len);
662 static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
665 u32 ssid, tsid, newsid;
671 length = task_has_security(current, SECURITY__COMPUTE_RELABEL);
676 scon = kzalloc(size+1, GFP_KERNEL);
680 tcon = kzalloc(size+1, GFP_KERNEL);
685 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
688 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
691 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
695 length = security_change_sid(ssid, tsid, tclass, &newsid);
699 length = security_sid_to_context(newsid, &newcon, &len);
703 if (len > SIMPLE_TRANSACTION_LIMIT) {
708 memcpy(buf, newcon, len);
719 static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
721 char *con, *user, *ptr;
728 length = task_has_security(current, SECURITY__COMPUTE_USER);
733 con = kzalloc(size+1, GFP_KERNEL);
737 user = kzalloc(size+1, GFP_KERNEL);
742 if (sscanf(buf, "%s %s", con, user) != 2)
745 length = security_context_to_sid(con, strlen(con)+1, &sid);
749 length = security_get_user_sids(sid, user, &sids, &nsids);
753 length = sprintf(buf, "%u", nsids) + 1;
755 for (i = 0; i < nsids; i++) {
756 rc = security_sid_to_context(sids[i], &newcon, &len);
761 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
766 memcpy(ptr, newcon, len);
780 static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
783 u32 ssid, tsid, newsid;
789 length = task_has_security(current, SECURITY__COMPUTE_MEMBER);
794 scon = kzalloc(size+1, GFP_KERNEL);
798 tcon = kzalloc(size+1, GFP_KERNEL);
803 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
806 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
809 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
813 length = security_member_sid(ssid, tsid, tclass, &newsid);
817 length = security_sid_to_context(newsid, &newcon, &len);
821 if (len > SIMPLE_TRANSACTION_LIMIT) {
822 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
823 "payload max\n", __func__, len);
828 memcpy(buf, newcon, len);
839 static struct inode *sel_make_inode(struct super_block *sb, int mode)
841 struct inode *ret = new_inode(sb);
845 ret->i_uid = ret->i_gid = 0;
847 ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
852 static ssize_t sel_read_bool(struct file *filep, char __user *buf,
853 size_t count, loff_t *ppos)
859 struct inode *inode = filep->f_path.dentry->d_inode;
860 unsigned index = inode->i_ino & SEL_INO_MASK;
861 const char *name = filep->f_path.dentry->d_name.name;
863 mutex_lock(&sel_mutex);
865 if (index >= bool_num || strcmp(name, bool_pending_names[index])) {
870 if (count > PAGE_SIZE) {
874 page = (char *)get_zeroed_page(GFP_KERNEL);
880 cur_enforcing = security_get_bool_value(index);
881 if (cur_enforcing < 0) {
885 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
886 bool_pending_values[index]);
887 ret = simple_read_from_buffer(buf, count, ppos, page, length);
889 mutex_unlock(&sel_mutex);
891 free_page((unsigned long)page);
895 static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
896 size_t count, loff_t *ppos)
901 struct inode *inode = filep->f_path.dentry->d_inode;
902 unsigned index = inode->i_ino & SEL_INO_MASK;
903 const char *name = filep->f_path.dentry->d_name.name;
905 mutex_lock(&sel_mutex);
907 length = task_has_security(current, SECURITY__SETBOOL);
911 if (index >= bool_num || strcmp(name, bool_pending_names[index])) {
916 if (count >= PAGE_SIZE) {
922 /* No partial writes. */
926 page = (char *)get_zeroed_page(GFP_KERNEL);
933 if (copy_from_user(page, buf, count))
937 if (sscanf(page, "%d", &new_value) != 1)
943 bool_pending_values[index] = new_value;
947 mutex_unlock(&sel_mutex);
949 free_page((unsigned long) page);
953 static const struct file_operations sel_bool_ops = {
954 .read = sel_read_bool,
955 .write = sel_write_bool,
958 static ssize_t sel_commit_bools_write(struct file *filep,
959 const char __user *buf,
960 size_t count, loff_t *ppos)
966 mutex_lock(&sel_mutex);
968 length = task_has_security(current, SECURITY__SETBOOL);
972 if (count >= PAGE_SIZE) {
977 /* No partial writes. */
980 page = (char *)get_zeroed_page(GFP_KERNEL);
987 if (copy_from_user(page, buf, count))
991 if (sscanf(page, "%d", &new_value) != 1)
994 if (new_value && bool_pending_values)
995 security_set_bools(bool_num, bool_pending_values);
1000 mutex_unlock(&sel_mutex);
1002 free_page((unsigned long) page);
1006 static const struct file_operations sel_commit_bools_ops = {
1007 .write = sel_commit_bools_write,
1010 static void sel_remove_entries(struct dentry *de)
1012 struct list_head *node;
1014 spin_lock(&dcache_lock);
1015 node = de->d_subdirs.next;
1016 while (node != &de->d_subdirs) {
1017 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
1018 list_del_init(node);
1022 spin_unlock(&dcache_lock);
1024 simple_unlink(de->d_inode, d);
1026 spin_lock(&dcache_lock);
1028 node = de->d_subdirs.next;
1031 spin_unlock(&dcache_lock);
1034 #define BOOL_DIR_NAME "booleans"
1036 static int sel_make_bools(void)
1040 struct dentry *dentry = NULL;
1041 struct dentry *dir = bool_dir;
1042 struct inode *inode = NULL;
1043 struct inode_security_struct *isec;
1044 char **names = NULL, *page;
1049 /* remove any existing files */
1050 kfree(bool_pending_names);
1051 kfree(bool_pending_values);
1052 bool_pending_names = NULL;
1053 bool_pending_values = NULL;
1055 sel_remove_entries(dir);
1057 page = (char *)get_zeroed_page(GFP_KERNEL);
1061 ret = security_get_bools(&num, &names, &values);
1065 for (i = 0; i < num; i++) {
1066 dentry = d_alloc_name(dir, names[i]);
1071 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1077 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
1081 } else if (len >= PAGE_SIZE) {
1082 ret = -ENAMETOOLONG;
1085 isec = (struct inode_security_struct *)inode->i_security;
1086 ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid);
1090 isec->initialized = 1;
1091 inode->i_fop = &sel_bool_ops;
1092 inode->i_ino = i|SEL_BOOL_INO_OFFSET;
1093 d_add(dentry, inode);
1096 bool_pending_names = names;
1097 bool_pending_values = values;
1099 free_page((unsigned long)page);
1103 for (i = 0; i < num; i++)
1108 sel_remove_entries(dir);
1113 #define NULL_FILE_NAME "null"
1115 struct dentry *selinux_null;
1117 static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1118 size_t count, loff_t *ppos)
1120 char tmpbuf[TMPBUFLEN];
1123 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
1124 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1127 static ssize_t sel_write_avc_cache_threshold(struct file *file,
1128 const char __user *buf,
1129 size_t count, loff_t *ppos)
1136 if (count >= PAGE_SIZE) {
1142 /* No partial writes. */
1147 page = (char *)get_zeroed_page(GFP_KERNEL);
1153 if (copy_from_user(page, buf, count)) {
1158 if (sscanf(page, "%u", &new_value) != 1) {
1163 if (new_value != avc_cache_threshold) {
1164 ret = task_has_security(current, SECURITY__SETSECPARAM);
1167 avc_cache_threshold = new_value;
1171 free_page((unsigned long)page);
1176 static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1177 size_t count, loff_t *ppos)
1182 page = (char *)__get_free_page(GFP_KERNEL);
1187 ret = avc_get_hash_stats(page);
1189 ret = simple_read_from_buffer(buf, count, ppos, page, ret);
1190 free_page((unsigned long)page);
1195 static const struct file_operations sel_avc_cache_threshold_ops = {
1196 .read = sel_read_avc_cache_threshold,
1197 .write = sel_write_avc_cache_threshold,
1200 static const struct file_operations sel_avc_hash_stats_ops = {
1201 .read = sel_read_avc_hash_stats,
1204 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1205 static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1209 for (cpu = *idx; cpu < NR_CPUS; ++cpu) {
1210 if (!cpu_possible(cpu))
1213 return &per_cpu(avc_cache_stats, cpu);
1218 static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1220 loff_t n = *pos - 1;
1223 return SEQ_START_TOKEN;
1225 return sel_avc_get_stat_idx(&n);
1228 static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1230 return sel_avc_get_stat_idx(pos);
1233 static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1235 struct avc_cache_stats *st = v;
1237 if (v == SEQ_START_TOKEN)
1238 seq_printf(seq, "lookups hits misses allocations reclaims "
1241 seq_printf(seq, "%u %u %u %u %u %u\n", st->lookups,
1242 st->hits, st->misses, st->allocations,
1243 st->reclaims, st->frees);
1247 static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1250 static const struct seq_operations sel_avc_cache_stats_seq_ops = {
1251 .start = sel_avc_stats_seq_start,
1252 .next = sel_avc_stats_seq_next,
1253 .show = sel_avc_stats_seq_show,
1254 .stop = sel_avc_stats_seq_stop,
1257 static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1259 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1262 static const struct file_operations sel_avc_cache_stats_ops = {
1263 .open = sel_open_avc_cache_stats,
1265 .llseek = seq_lseek,
1266 .release = seq_release,
1270 static int sel_make_avc_files(struct dentry *dir)
1273 static struct tree_descr files[] = {
1274 { "cache_threshold",
1275 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1276 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1277 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1278 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1282 for (i = 0; i < ARRAY_SIZE(files); i++) {
1283 struct inode *inode;
1284 struct dentry *dentry;
1286 dentry = d_alloc_name(dir, files[i].name);
1292 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1297 inode->i_fop = files[i].ops;
1298 inode->i_ino = ++sel_last_ino;
1299 d_add(dentry, inode);
1305 static ssize_t sel_read_initcon(struct file *file, char __user *buf,
1306 size_t count, loff_t *ppos)
1308 struct inode *inode;
1313 inode = file->f_path.dentry->d_inode;
1314 sid = inode->i_ino&SEL_INO_MASK;
1315 ret = security_sid_to_context(sid, &con, &len);
1319 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1324 static const struct file_operations sel_initcon_ops = {
1325 .read = sel_read_initcon,
1328 static int sel_make_initcon_files(struct dentry *dir)
1332 for (i = 1; i <= SECINITSID_NUM; i++) {
1333 struct inode *inode;
1334 struct dentry *dentry;
1335 dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
1341 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1346 inode->i_fop = &sel_initcon_ops;
1347 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1348 d_add(dentry, inode);
1354 static inline unsigned int sel_div(unsigned long a, unsigned long b)
1356 return a / b - (a % b < 0);
1359 static inline unsigned long sel_class_to_ino(u16 class)
1361 return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1364 static inline u16 sel_ino_to_class(unsigned long ino)
1366 return sel_div(ino & SEL_INO_MASK, SEL_VEC_MAX + 1);
1369 static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1371 return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1374 static inline u32 sel_ino_to_perm(unsigned long ino)
1376 return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1379 static ssize_t sel_read_class(struct file *file, char __user *buf,
1380 size_t count, loff_t *ppos)
1384 unsigned long ino = file->f_path.dentry->d_inode->i_ino;
1386 page = (char *)__get_free_page(GFP_KERNEL);
1392 len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_class(ino));
1393 rc = simple_read_from_buffer(buf, count, ppos, page, len);
1394 free_page((unsigned long)page);
1399 static const struct file_operations sel_class_ops = {
1400 .read = sel_read_class,
1403 static ssize_t sel_read_perm(struct file *file, char __user *buf,
1404 size_t count, loff_t *ppos)
1408 unsigned long ino = file->f_path.dentry->d_inode->i_ino;
1410 page = (char *)__get_free_page(GFP_KERNEL);
1416 len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_perm(ino));
1417 rc = simple_read_from_buffer(buf, count, ppos, page, len);
1418 free_page((unsigned long)page);
1423 static const struct file_operations sel_perm_ops = {
1424 .read = sel_read_perm,
1427 static ssize_t sel_read_policycap(struct file *file, char __user *buf,
1428 size_t count, loff_t *ppos)
1431 char tmpbuf[TMPBUFLEN];
1433 unsigned long i_ino = file->f_path.dentry->d_inode->i_ino;
1435 value = security_policycap_supported(i_ino & SEL_INO_MASK);
1436 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
1438 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1441 static const struct file_operations sel_policycap_ops = {
1442 .read = sel_read_policycap,
1445 static int sel_make_perm_files(char *objclass, int classvalue,
1448 int i, rc = 0, nperms;
1451 rc = security_get_permissions(objclass, &perms, &nperms);
1455 for (i = 0; i < nperms; i++) {
1456 struct inode *inode;
1457 struct dentry *dentry;
1459 dentry = d_alloc_name(dir, perms[i]);
1465 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1470 inode->i_fop = &sel_perm_ops;
1471 /* i+1 since perm values are 1-indexed */
1472 inode->i_ino = sel_perm_to_ino(classvalue, i+1);
1473 d_add(dentry, inode);
1477 for (i = 0; i < nperms; i++)
1484 static int sel_make_class_dir_entries(char *classname, int index,
1487 struct dentry *dentry = NULL;
1488 struct inode *inode = NULL;
1491 dentry = d_alloc_name(dir, "index");
1497 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1503 inode->i_fop = &sel_class_ops;
1504 inode->i_ino = sel_class_to_ino(index);
1505 d_add(dentry, inode);
1507 dentry = d_alloc_name(dir, "perms");
1513 rc = sel_make_dir(dir->d_inode, dentry, &last_class_ino);
1517 rc = sel_make_perm_files(classname, index, dentry);
1523 static void sel_remove_classes(void)
1525 struct list_head *class_node;
1527 list_for_each(class_node, &class_dir->d_subdirs) {
1528 struct dentry *class_subdir = list_entry(class_node,
1529 struct dentry, d_u.d_child);
1530 struct list_head *class_subdir_node;
1532 list_for_each(class_subdir_node, &class_subdir->d_subdirs) {
1533 struct dentry *d = list_entry(class_subdir_node,
1534 struct dentry, d_u.d_child);
1537 if (d->d_inode->i_mode & S_IFDIR)
1538 sel_remove_entries(d);
1541 sel_remove_entries(class_subdir);
1544 sel_remove_entries(class_dir);
1547 static int sel_make_classes(void)
1549 int rc = 0, nclasses, i;
1552 /* delete any existing entries */
1553 sel_remove_classes();
1555 rc = security_get_classes(&classes, &nclasses);
1559 /* +2 since classes are 1-indexed */
1560 last_class_ino = sel_class_to_ino(nclasses+2);
1562 for (i = 0; i < nclasses; i++) {
1563 struct dentry *class_name_dir;
1565 class_name_dir = d_alloc_name(class_dir, classes[i]);
1566 if (!class_name_dir) {
1571 rc = sel_make_dir(class_dir->d_inode, class_name_dir,
1576 /* i+1 since class values are 1-indexed */
1577 rc = sel_make_class_dir_entries(classes[i], i+1,
1584 for (i = 0; i < nclasses; i++)
1591 static int sel_make_policycap(void)
1594 struct dentry *dentry = NULL;
1595 struct inode *inode = NULL;
1597 sel_remove_entries(policycap_dir);
1599 for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
1600 if (iter < ARRAY_SIZE(policycap_names))
1601 dentry = d_alloc_name(policycap_dir,
1602 policycap_names[iter]);
1604 dentry = d_alloc_name(policycap_dir, "unknown");
1609 inode = sel_make_inode(policycap_dir->d_sb, S_IFREG | S_IRUGO);
1613 inode->i_fop = &sel_policycap_ops;
1614 inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
1615 d_add(dentry, inode);
1621 static int sel_make_dir(struct inode *dir, struct dentry *dentry,
1625 struct inode *inode;
1627 inode = sel_make_inode(dir->i_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1632 inode->i_op = &simple_dir_inode_operations;
1633 inode->i_fop = &simple_dir_operations;
1634 inode->i_ino = ++(*ino);
1635 /* directory inodes start off with i_nlink == 2 (for "." entry) */
1637 d_add(dentry, inode);
1638 /* bump link count on parent directory, too */
1644 static int sel_fill_super(struct super_block *sb, void *data, int silent)
1647 struct dentry *dentry;
1648 struct inode *inode, *root_inode;
1649 struct inode_security_struct *isec;
1651 static struct tree_descr selinux_files[] = {
1652 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1653 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
1654 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
1655 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1656 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1657 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1658 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1659 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1660 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1661 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1662 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1663 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1664 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
1665 [SEL_COMPAT_NET] = {"compat_net", &sel_compat_net_ops, S_IRUGO|S_IWUSR},
1666 [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
1667 [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
1670 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1674 root_inode = sb->s_root->d_inode;
1676 dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME);
1682 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1688 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
1694 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
1699 inode->i_ino = ++sel_last_ino;
1700 isec = (struct inode_security_struct *)inode->i_security;
1701 isec->sid = SECINITSID_DEVNULL;
1702 isec->sclass = SECCLASS_CHR_FILE;
1703 isec->initialized = 1;
1705 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1706 d_add(dentry, inode);
1707 selinux_null = dentry;
1709 dentry = d_alloc_name(sb->s_root, "avc");
1715 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1719 ret = sel_make_avc_files(dentry);
1723 dentry = d_alloc_name(sb->s_root, "initial_contexts");
1729 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1733 ret = sel_make_initcon_files(dentry);
1737 dentry = d_alloc_name(sb->s_root, "class");
1743 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1749 dentry = d_alloc_name(sb->s_root, "policy_capabilities");
1755 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1759 policycap_dir = dentry;
1764 printk(KERN_ERR "SELinux: %s: failed while creating inodes\n",
1769 static int sel_get_sb(struct file_system_type *fs_type,
1770 int flags, const char *dev_name, void *data,
1771 struct vfsmount *mnt)
1773 return get_sb_single(fs_type, flags, data, sel_fill_super, mnt);
1776 static struct file_system_type sel_fs_type = {
1777 .name = "selinuxfs",
1778 .get_sb = sel_get_sb,
1779 .kill_sb = kill_litter_super,
1782 struct vfsmount *selinuxfs_mount;
1784 static int __init init_sel_fs(void)
1788 if (!selinux_enabled)
1790 err = register_filesystem(&sel_fs_type);
1792 selinuxfs_mount = kern_mount(&sel_fs_type);
1793 if (IS_ERR(selinuxfs_mount)) {
1794 printk(KERN_ERR "selinuxfs: could not mount!\n");
1795 err = PTR_ERR(selinuxfs_mount);
1796 selinuxfs_mount = NULL;
1802 __initcall(init_sel_fs);
1804 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
1805 void exit_sel_fs(void)
1807 unregister_filesystem(&sel_fs_type);