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 <asm/uaccess.h>
31 #include <asm/semaphore.h>
33 /* selinuxfs pseudo filesystem for exporting the security policy API.
34 Based on the proc code and the fs/nfsd/nfsctl.c code. */
41 #include "conditional.h"
43 /* Policy capability filenames */
44 static char *policycap_names[] = {
45 "network_peer_controls",
49 unsigned int selinux_checkreqprot = CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
51 #ifdef CONFIG_SECURITY_SELINUX_ENABLE_SECMARK_DEFAULT
52 #define SELINUX_COMPAT_NET_VALUE 0
54 #define SELINUX_COMPAT_NET_VALUE 1
57 int selinux_compat_net = SELINUX_COMPAT_NET_VALUE;
59 static int __init checkreqprot_setup(char *str)
61 selinux_checkreqprot = simple_strtoul(str, NULL, 0) ? 1 : 0;
64 __setup("checkreqprot=", checkreqprot_setup);
66 static int __init selinux_compat_net_setup(char *str)
68 selinux_compat_net = simple_strtoul(str, NULL, 0) ? 1 : 0;
71 __setup("selinux_compat_net=", selinux_compat_net_setup);
74 static DEFINE_MUTEX(sel_mutex);
76 /* global data for booleans */
77 static struct dentry *bool_dir;
79 static char **bool_pending_names;
80 static int *bool_pending_values;
82 /* global data for classes */
83 static struct dentry *class_dir;
84 static unsigned long last_class_ino;
86 /* global data for policy capabilities */
87 static struct dentry *policycap_dir;
89 extern void selnl_notify_setenforce(int val);
91 /* Check whether a task is allowed to use a security operation. */
92 static int task_has_security(struct task_struct *tsk,
95 struct task_security_struct *tsec;
101 return avc_has_perm(tsec->sid, SECINITSID_SECURITY,
102 SECCLASS_SECURITY, perms, NULL);
107 SEL_LOAD, /* load policy */
108 SEL_ENFORCE, /* get or set enforcing status */
109 SEL_CONTEXT, /* validate context */
110 SEL_ACCESS, /* compute access decision */
111 SEL_CREATE, /* compute create labeling decision */
112 SEL_RELABEL, /* compute relabeling decision */
113 SEL_USER, /* compute reachable user contexts */
114 SEL_POLICYVERS, /* return policy version for this kernel */
115 SEL_COMMIT_BOOLS, /* commit new boolean values */
116 SEL_MLS, /* return if MLS policy is enabled */
117 SEL_DISABLE, /* disable SELinux until next reboot */
118 SEL_MEMBER, /* compute polyinstantiation membership decision */
119 SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
120 SEL_COMPAT_NET, /* whether to use old compat network packet controls */
121 SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */
122 SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */
123 SEL_INO_NEXT, /* The next inode number to use */
126 static unsigned long sel_last_ino = SEL_INO_NEXT - 1;
128 #define SEL_INITCON_INO_OFFSET 0x01000000
129 #define SEL_BOOL_INO_OFFSET 0x02000000
130 #define SEL_CLASS_INO_OFFSET 0x04000000
131 #define SEL_POLICYCAP_INO_OFFSET 0x08000000
132 #define SEL_INO_MASK 0x00ffffff
135 static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
136 size_t count, loff_t *ppos)
138 char tmpbuf[TMPBUFLEN];
141 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing);
142 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
145 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
146 static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
147 size_t count, loff_t *ppos)
154 if (count >= PAGE_SIZE)
157 /* No partial writes. */
160 page = (char *)get_zeroed_page(GFP_KERNEL);
164 if (copy_from_user(page, buf, count))
168 if (sscanf(page, "%d", &new_value) != 1)
171 if (new_value != selinux_enforcing) {
172 length = task_has_security(current, SECURITY__SETENFORCE);
175 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
176 "enforcing=%d old_enforcing=%d auid=%u ses=%u",
177 new_value, selinux_enforcing,
178 audit_get_loginuid(current),
179 audit_get_sessionid(current));
180 selinux_enforcing = new_value;
181 if (selinux_enforcing)
183 selnl_notify_setenforce(selinux_enforcing);
187 free_page((unsigned long) page);
191 #define sel_write_enforce NULL
194 static const struct file_operations sel_enforce_ops = {
195 .read = sel_read_enforce,
196 .write = sel_write_enforce,
199 static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf,
200 size_t count, loff_t *ppos)
202 char tmpbuf[TMPBUFLEN];
204 ino_t ino = filp->f_path.dentry->d_inode->i_ino;
205 int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
206 security_get_reject_unknown() : !security_get_allow_unknown();
208 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
209 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
212 static const struct file_operations sel_handle_unknown_ops = {
213 .read = sel_read_handle_unknown,
216 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
217 static ssize_t sel_write_disable(struct file *file, const char __user *buf,
218 size_t count, loff_t *ppos)
224 extern int selinux_disable(void);
226 if (count >= PAGE_SIZE)
229 /* No partial writes. */
232 page = (char *)get_zeroed_page(GFP_KERNEL);
236 if (copy_from_user(page, buf, count))
240 if (sscanf(page, "%d", &new_value) != 1)
244 length = selinux_disable();
247 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
248 "selinux=0 auid=%u ses=%u",
249 audit_get_loginuid(current),
250 audit_get_sessionid(current));
255 free_page((unsigned long) page);
259 #define sel_write_disable NULL
262 static const struct file_operations sel_disable_ops = {
263 .write = sel_write_disable,
266 static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
267 size_t count, loff_t *ppos)
269 char tmpbuf[TMPBUFLEN];
272 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
273 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
276 static const struct file_operations sel_policyvers_ops = {
277 .read = sel_read_policyvers,
280 /* declaration for sel_write_load */
281 static int sel_make_bools(void);
282 static int sel_make_classes(void);
283 static int sel_make_policycap(void);
285 /* declaration for sel_make_class_dirs */
286 static int sel_make_dir(struct inode *dir, struct dentry *dentry,
289 static ssize_t sel_read_mls(struct file *filp, char __user *buf,
290 size_t count, loff_t *ppos)
292 char tmpbuf[TMPBUFLEN];
295 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_mls_enabled);
296 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
299 static const struct file_operations sel_mls_ops = {
300 .read = sel_read_mls,
303 static ssize_t sel_write_load(struct file *file, const char __user *buf,
304 size_t count, loff_t *ppos)
311 mutex_lock(&sel_mutex);
313 length = task_has_security(current, SECURITY__LOAD_POLICY);
318 /* No partial writes. */
323 if ((count > 64 * 1024 * 1024)
324 || (data = vmalloc(count)) == NULL) {
330 if (copy_from_user(data, buf, count) != 0)
333 length = security_load_policy(data, count);
337 ret = sel_make_bools();
343 ret = sel_make_classes();
349 ret = sel_make_policycap();
357 printk(KERN_INFO "SELinux: policy loaded with handle_unknown=%s\n",
358 (security_get_reject_unknown() ? "reject" :
359 (security_get_allow_unknown() ? "allow" : "deny")));
361 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
362 "policy loaded auid=%u ses=%u",
363 audit_get_loginuid(current),
364 audit_get_sessionid(current));
366 mutex_unlock(&sel_mutex);
371 static const struct file_operations sel_load_ops = {
372 .write = sel_write_load,
375 static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
381 length = task_has_security(current, SECURITY__CHECK_CONTEXT);
385 length = security_context_to_sid(buf, size, &sid);
389 length = security_sid_to_context(sid, &canon, &len);
393 if (len > SIMPLE_TRANSACTION_LIMIT) {
394 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
395 "payload max\n", __func__, len);
400 memcpy(buf, canon, len);
407 static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
408 size_t count, loff_t *ppos)
410 char tmpbuf[TMPBUFLEN];
413 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot);
414 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
417 static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
418 size_t count, loff_t *ppos)
422 unsigned int new_value;
424 length = task_has_security(current, SECURITY__SETCHECKREQPROT);
428 if (count >= PAGE_SIZE)
431 /* No partial writes. */
434 page = (char *)get_zeroed_page(GFP_KERNEL);
438 if (copy_from_user(page, buf, count))
442 if (sscanf(page, "%u", &new_value) != 1)
445 selinux_checkreqprot = new_value ? 1 : 0;
448 free_page((unsigned long) page);
451 static const struct file_operations sel_checkreqprot_ops = {
452 .read = sel_read_checkreqprot,
453 .write = sel_write_checkreqprot,
456 static ssize_t sel_read_compat_net(struct file *filp, char __user *buf,
457 size_t count, loff_t *ppos)
459 char tmpbuf[TMPBUFLEN];
462 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_compat_net);
463 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
466 static ssize_t sel_write_compat_net(struct file *file, const char __user *buf,
467 size_t count, loff_t *ppos)
473 length = task_has_security(current, SECURITY__LOAD_POLICY);
477 if (count >= PAGE_SIZE)
480 /* No partial writes. */
483 page = (char *)get_zeroed_page(GFP_KERNEL);
487 if (copy_from_user(page, buf, count))
491 if (sscanf(page, "%d", &new_value) != 1)
494 selinux_compat_net = new_value ? 1 : 0;
497 free_page((unsigned long) page);
500 static const struct file_operations sel_compat_net_ops = {
501 .read = sel_read_compat_net,
502 .write = sel_write_compat_net,
506 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
508 static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
509 static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
510 static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
511 static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
512 static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
514 static ssize_t (*write_op[])(struct file *, char *, size_t) = {
515 [SEL_ACCESS] = sel_write_access,
516 [SEL_CREATE] = sel_write_create,
517 [SEL_RELABEL] = sel_write_relabel,
518 [SEL_USER] = sel_write_user,
519 [SEL_MEMBER] = sel_write_member,
520 [SEL_CONTEXT] = sel_write_context,
523 static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
525 ino_t ino = file->f_path.dentry->d_inode->i_ino;
529 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
532 data = simple_transaction_get(file, buf, size);
534 return PTR_ERR(data);
536 rv = write_op[ino](file, data, size);
538 simple_transaction_set(file, rv);
544 static const struct file_operations transaction_ops = {
545 .write = selinux_transaction_write,
546 .read = simple_transaction_read,
547 .release = simple_transaction_release,
551 * payload - write methods
552 * If the method has a response, the response should be put in buf,
553 * and the length returned. Otherwise return 0 or and -error.
556 static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
562 struct av_decision avd;
565 length = task_has_security(current, SECURITY__COMPUTE_AV);
570 scon = kzalloc(size+1, GFP_KERNEL);
574 tcon = kzalloc(size+1, GFP_KERNEL);
579 if (sscanf(buf, "%s %s %hu %x", scon, tcon, &tclass, &req) != 4)
582 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
585 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
589 length = security_compute_av(ssid, tsid, tclass, req, &avd);
593 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
595 avd.allowed, avd.decided,
596 avd.auditallow, avd.auditdeny,
605 static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
608 u32 ssid, tsid, newsid;
614 length = task_has_security(current, SECURITY__COMPUTE_CREATE);
619 scon = kzalloc(size+1, GFP_KERNEL);
623 tcon = kzalloc(size+1, GFP_KERNEL);
628 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
631 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
634 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
638 length = security_transition_sid(ssid, tsid, tclass, &newsid);
642 length = security_sid_to_context(newsid, &newcon, &len);
646 if (len > SIMPLE_TRANSACTION_LIMIT) {
647 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
648 "payload max\n", __func__, len);
653 memcpy(buf, newcon, len);
664 static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
667 u32 ssid, tsid, newsid;
673 length = task_has_security(current, SECURITY__COMPUTE_RELABEL);
678 scon = kzalloc(size+1, GFP_KERNEL);
682 tcon = kzalloc(size+1, GFP_KERNEL);
687 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
690 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
693 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
697 length = security_change_sid(ssid, tsid, tclass, &newsid);
701 length = security_sid_to_context(newsid, &newcon, &len);
705 if (len > SIMPLE_TRANSACTION_LIMIT) {
710 memcpy(buf, newcon, len);
721 static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
723 char *con, *user, *ptr;
730 length = task_has_security(current, SECURITY__COMPUTE_USER);
735 con = kzalloc(size+1, GFP_KERNEL);
739 user = kzalloc(size+1, GFP_KERNEL);
744 if (sscanf(buf, "%s %s", con, user) != 2)
747 length = security_context_to_sid(con, strlen(con)+1, &sid);
751 length = security_get_user_sids(sid, user, &sids, &nsids);
755 length = sprintf(buf, "%u", nsids) + 1;
757 for (i = 0; i < nsids; i++) {
758 rc = security_sid_to_context(sids[i], &newcon, &len);
763 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
768 memcpy(ptr, newcon, len);
782 static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
785 u32 ssid, tsid, newsid;
791 length = task_has_security(current, SECURITY__COMPUTE_MEMBER);
796 scon = kzalloc(size+1, GFP_KERNEL);
800 tcon = kzalloc(size+1, GFP_KERNEL);
805 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
808 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
811 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
815 length = security_member_sid(ssid, tsid, tclass, &newsid);
819 length = security_sid_to_context(newsid, &newcon, &len);
823 if (len > SIMPLE_TRANSACTION_LIMIT) {
824 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
825 "payload max\n", __func__, len);
830 memcpy(buf, newcon, len);
841 static struct inode *sel_make_inode(struct super_block *sb, int mode)
843 struct inode *ret = new_inode(sb);
847 ret->i_uid = ret->i_gid = 0;
849 ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
854 static ssize_t sel_read_bool(struct file *filep, char __user *buf,
855 size_t count, loff_t *ppos)
861 struct inode *inode = filep->f_path.dentry->d_inode;
862 unsigned index = inode->i_ino & SEL_INO_MASK;
863 const char *name = filep->f_path.dentry->d_name.name;
865 mutex_lock(&sel_mutex);
867 if (index >= bool_num || strcmp(name, bool_pending_names[index])) {
872 if (count > PAGE_SIZE) {
876 page = (char *)get_zeroed_page(GFP_KERNEL);
882 cur_enforcing = security_get_bool_value(index);
883 if (cur_enforcing < 0) {
887 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
888 bool_pending_values[index]);
889 ret = simple_read_from_buffer(buf, count, ppos, page, length);
891 mutex_unlock(&sel_mutex);
893 free_page((unsigned long)page);
897 static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
898 size_t count, loff_t *ppos)
903 struct inode *inode = filep->f_path.dentry->d_inode;
904 unsigned index = inode->i_ino & SEL_INO_MASK;
905 const char *name = filep->f_path.dentry->d_name.name;
907 mutex_lock(&sel_mutex);
909 length = task_has_security(current, SECURITY__SETBOOL);
913 if (index >= bool_num || strcmp(name, bool_pending_names[index])) {
918 if (count >= PAGE_SIZE) {
924 /* No partial writes. */
928 page = (char *)get_zeroed_page(GFP_KERNEL);
935 if (copy_from_user(page, buf, count))
939 if (sscanf(page, "%d", &new_value) != 1)
945 bool_pending_values[index] = new_value;
949 mutex_unlock(&sel_mutex);
951 free_page((unsigned long) page);
955 static const struct file_operations sel_bool_ops = {
956 .read = sel_read_bool,
957 .write = sel_write_bool,
960 static ssize_t sel_commit_bools_write(struct file *filep,
961 const char __user *buf,
962 size_t count, loff_t *ppos)
968 mutex_lock(&sel_mutex);
970 length = task_has_security(current, SECURITY__SETBOOL);
974 if (count >= PAGE_SIZE) {
979 /* No partial writes. */
982 page = (char *)get_zeroed_page(GFP_KERNEL);
989 if (copy_from_user(page, buf, count))
993 if (sscanf(page, "%d", &new_value) != 1)
996 if (new_value && bool_pending_values)
997 security_set_bools(bool_num, bool_pending_values);
1002 mutex_unlock(&sel_mutex);
1004 free_page((unsigned long) page);
1008 static const struct file_operations sel_commit_bools_ops = {
1009 .write = sel_commit_bools_write,
1012 static void sel_remove_entries(struct dentry *de)
1014 struct list_head *node;
1016 spin_lock(&dcache_lock);
1017 node = de->d_subdirs.next;
1018 while (node != &de->d_subdirs) {
1019 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
1020 list_del_init(node);
1024 spin_unlock(&dcache_lock);
1026 simple_unlink(de->d_inode, d);
1028 spin_lock(&dcache_lock);
1030 node = de->d_subdirs.next;
1033 spin_unlock(&dcache_lock);
1036 #define BOOL_DIR_NAME "booleans"
1038 static int sel_make_bools(void)
1042 struct dentry *dentry = NULL;
1043 struct dentry *dir = bool_dir;
1044 struct inode *inode = NULL;
1045 struct inode_security_struct *isec;
1046 char **names = NULL, *page;
1051 /* remove any existing files */
1052 kfree(bool_pending_names);
1053 kfree(bool_pending_values);
1054 bool_pending_names = NULL;
1055 bool_pending_values = NULL;
1057 sel_remove_entries(dir);
1059 page = (char *)get_zeroed_page(GFP_KERNEL);
1063 ret = security_get_bools(&num, &names, &values);
1067 for (i = 0; i < num; i++) {
1068 dentry = d_alloc_name(dir, names[i]);
1073 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1079 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
1083 } else if (len >= PAGE_SIZE) {
1084 ret = -ENAMETOOLONG;
1087 isec = (struct inode_security_struct *)inode->i_security;
1088 ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid);
1092 isec->initialized = 1;
1093 inode->i_fop = &sel_bool_ops;
1094 inode->i_ino = i|SEL_BOOL_INO_OFFSET;
1095 d_add(dentry, inode);
1098 bool_pending_names = names;
1099 bool_pending_values = values;
1101 free_page((unsigned long)page);
1105 for (i = 0; i < num; i++)
1110 sel_remove_entries(dir);
1115 #define NULL_FILE_NAME "null"
1117 struct dentry *selinux_null;
1119 static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1120 size_t count, loff_t *ppos)
1122 char tmpbuf[TMPBUFLEN];
1125 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
1126 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1129 static ssize_t sel_write_avc_cache_threshold(struct file *file,
1130 const char __user *buf,
1131 size_t count, loff_t *ppos)
1138 if (count >= PAGE_SIZE) {
1144 /* No partial writes. */
1149 page = (char *)get_zeroed_page(GFP_KERNEL);
1155 if (copy_from_user(page, buf, count)) {
1160 if (sscanf(page, "%u", &new_value) != 1) {
1165 if (new_value != avc_cache_threshold) {
1166 ret = task_has_security(current, SECURITY__SETSECPARAM);
1169 avc_cache_threshold = new_value;
1173 free_page((unsigned long)page);
1178 static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1179 size_t count, loff_t *ppos)
1184 page = (char *)__get_free_page(GFP_KERNEL);
1189 ret = avc_get_hash_stats(page);
1191 ret = simple_read_from_buffer(buf, count, ppos, page, ret);
1192 free_page((unsigned long)page);
1197 static const struct file_operations sel_avc_cache_threshold_ops = {
1198 .read = sel_read_avc_cache_threshold,
1199 .write = sel_write_avc_cache_threshold,
1202 static const struct file_operations sel_avc_hash_stats_ops = {
1203 .read = sel_read_avc_hash_stats,
1206 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1207 static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1211 for (cpu = *idx; cpu < NR_CPUS; ++cpu) {
1212 if (!cpu_possible(cpu))
1215 return &per_cpu(avc_cache_stats, cpu);
1220 static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1222 loff_t n = *pos - 1;
1225 return SEQ_START_TOKEN;
1227 return sel_avc_get_stat_idx(&n);
1230 static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1232 return sel_avc_get_stat_idx(pos);
1235 static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1237 struct avc_cache_stats *st = v;
1239 if (v == SEQ_START_TOKEN)
1240 seq_printf(seq, "lookups hits misses allocations reclaims "
1243 seq_printf(seq, "%u %u %u %u %u %u\n", st->lookups,
1244 st->hits, st->misses, st->allocations,
1245 st->reclaims, st->frees);
1249 static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1252 static const struct seq_operations sel_avc_cache_stats_seq_ops = {
1253 .start = sel_avc_stats_seq_start,
1254 .next = sel_avc_stats_seq_next,
1255 .show = sel_avc_stats_seq_show,
1256 .stop = sel_avc_stats_seq_stop,
1259 static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1261 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1264 static const struct file_operations sel_avc_cache_stats_ops = {
1265 .open = sel_open_avc_cache_stats,
1267 .llseek = seq_lseek,
1268 .release = seq_release,
1272 static int sel_make_avc_files(struct dentry *dir)
1275 static struct tree_descr files[] = {
1276 { "cache_threshold",
1277 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1278 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1279 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1280 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1284 for (i = 0; i < ARRAY_SIZE(files); i++) {
1285 struct inode *inode;
1286 struct dentry *dentry;
1288 dentry = d_alloc_name(dir, files[i].name);
1294 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1299 inode->i_fop = files[i].ops;
1300 inode->i_ino = ++sel_last_ino;
1301 d_add(dentry, inode);
1307 static ssize_t sel_read_initcon(struct file *file, char __user *buf,
1308 size_t count, loff_t *ppos)
1310 struct inode *inode;
1315 inode = file->f_path.dentry->d_inode;
1316 sid = inode->i_ino&SEL_INO_MASK;
1317 ret = security_sid_to_context(sid, &con, &len);
1321 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1326 static const struct file_operations sel_initcon_ops = {
1327 .read = sel_read_initcon,
1330 static int sel_make_initcon_files(struct dentry *dir)
1334 for (i = 1; i <= SECINITSID_NUM; i++) {
1335 struct inode *inode;
1336 struct dentry *dentry;
1337 dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
1343 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1348 inode->i_fop = &sel_initcon_ops;
1349 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1350 d_add(dentry, inode);
1356 static inline unsigned int sel_div(unsigned long a, unsigned long b)
1358 return a / b - (a % b < 0);
1361 static inline unsigned long sel_class_to_ino(u16 class)
1363 return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1366 static inline u16 sel_ino_to_class(unsigned long ino)
1368 return sel_div(ino & SEL_INO_MASK, SEL_VEC_MAX + 1);
1371 static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1373 return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1376 static inline u32 sel_ino_to_perm(unsigned long ino)
1378 return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1381 static ssize_t sel_read_class(struct file *file, char __user *buf,
1382 size_t count, loff_t *ppos)
1386 unsigned long ino = file->f_path.dentry->d_inode->i_ino;
1388 page = (char *)__get_free_page(GFP_KERNEL);
1394 len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_class(ino));
1395 rc = simple_read_from_buffer(buf, count, ppos, page, len);
1396 free_page((unsigned long)page);
1401 static const struct file_operations sel_class_ops = {
1402 .read = sel_read_class,
1405 static ssize_t sel_read_perm(struct file *file, char __user *buf,
1406 size_t count, loff_t *ppos)
1410 unsigned long ino = file->f_path.dentry->d_inode->i_ino;
1412 page = (char *)__get_free_page(GFP_KERNEL);
1418 len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_perm(ino));
1419 rc = simple_read_from_buffer(buf, count, ppos, page, len);
1420 free_page((unsigned long)page);
1425 static const struct file_operations sel_perm_ops = {
1426 .read = sel_read_perm,
1429 static ssize_t sel_read_policycap(struct file *file, char __user *buf,
1430 size_t count, loff_t *ppos)
1433 char tmpbuf[TMPBUFLEN];
1435 unsigned long i_ino = file->f_path.dentry->d_inode->i_ino;
1437 value = security_policycap_supported(i_ino & SEL_INO_MASK);
1438 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
1440 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1443 static const struct file_operations sel_policycap_ops = {
1444 .read = sel_read_policycap,
1447 static int sel_make_perm_files(char *objclass, int classvalue,
1450 int i, rc = 0, nperms;
1453 rc = security_get_permissions(objclass, &perms, &nperms);
1457 for (i = 0; i < nperms; i++) {
1458 struct inode *inode;
1459 struct dentry *dentry;
1461 dentry = d_alloc_name(dir, perms[i]);
1467 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1472 inode->i_fop = &sel_perm_ops;
1473 /* i+1 since perm values are 1-indexed */
1474 inode->i_ino = sel_perm_to_ino(classvalue, i+1);
1475 d_add(dentry, inode);
1479 for (i = 0; i < nperms; i++)
1486 static int sel_make_class_dir_entries(char *classname, int index,
1489 struct dentry *dentry = NULL;
1490 struct inode *inode = NULL;
1493 dentry = d_alloc_name(dir, "index");
1499 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1505 inode->i_fop = &sel_class_ops;
1506 inode->i_ino = sel_class_to_ino(index);
1507 d_add(dentry, inode);
1509 dentry = d_alloc_name(dir, "perms");
1515 rc = sel_make_dir(dir->d_inode, dentry, &last_class_ino);
1519 rc = sel_make_perm_files(classname, index, dentry);
1525 static void sel_remove_classes(void)
1527 struct list_head *class_node;
1529 list_for_each(class_node, &class_dir->d_subdirs) {
1530 struct dentry *class_subdir = list_entry(class_node,
1531 struct dentry, d_u.d_child);
1532 struct list_head *class_subdir_node;
1534 list_for_each(class_subdir_node, &class_subdir->d_subdirs) {
1535 struct dentry *d = list_entry(class_subdir_node,
1536 struct dentry, d_u.d_child);
1539 if (d->d_inode->i_mode & S_IFDIR)
1540 sel_remove_entries(d);
1543 sel_remove_entries(class_subdir);
1546 sel_remove_entries(class_dir);
1549 static int sel_make_classes(void)
1551 int rc = 0, nclasses, i;
1554 /* delete any existing entries */
1555 sel_remove_classes();
1557 rc = security_get_classes(&classes, &nclasses);
1561 /* +2 since classes are 1-indexed */
1562 last_class_ino = sel_class_to_ino(nclasses+2);
1564 for (i = 0; i < nclasses; i++) {
1565 struct dentry *class_name_dir;
1567 class_name_dir = d_alloc_name(class_dir, classes[i]);
1568 if (!class_name_dir) {
1573 rc = sel_make_dir(class_dir->d_inode, class_name_dir,
1578 /* i+1 since class values are 1-indexed */
1579 rc = sel_make_class_dir_entries(classes[i], i+1,
1586 for (i = 0; i < nclasses; i++)
1593 static int sel_make_policycap(void)
1596 struct dentry *dentry = NULL;
1597 struct inode *inode = NULL;
1599 sel_remove_entries(policycap_dir);
1601 for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
1602 if (iter < ARRAY_SIZE(policycap_names))
1603 dentry = d_alloc_name(policycap_dir,
1604 policycap_names[iter]);
1606 dentry = d_alloc_name(policycap_dir, "unknown");
1611 inode = sel_make_inode(policycap_dir->d_sb, S_IFREG | S_IRUGO);
1615 inode->i_fop = &sel_policycap_ops;
1616 inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
1617 d_add(dentry, inode);
1623 static int sel_make_dir(struct inode *dir, struct dentry *dentry,
1627 struct inode *inode;
1629 inode = sel_make_inode(dir->i_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1634 inode->i_op = &simple_dir_inode_operations;
1635 inode->i_fop = &simple_dir_operations;
1636 inode->i_ino = ++(*ino);
1637 /* directory inodes start off with i_nlink == 2 (for "." entry) */
1639 d_add(dentry, inode);
1640 /* bump link count on parent directory, too */
1646 static int sel_fill_super(struct super_block *sb, void *data, int silent)
1649 struct dentry *dentry;
1650 struct inode *inode, *root_inode;
1651 struct inode_security_struct *isec;
1653 static struct tree_descr selinux_files[] = {
1654 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1655 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
1656 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
1657 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1658 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1659 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1660 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1661 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1662 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1663 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1664 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1665 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1666 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
1667 [SEL_COMPAT_NET] = {"compat_net", &sel_compat_net_ops, S_IRUGO|S_IWUSR},
1668 [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
1669 [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
1672 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1676 root_inode = sb->s_root->d_inode;
1678 dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME);
1684 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1690 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
1696 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
1701 inode->i_ino = ++sel_last_ino;
1702 isec = (struct inode_security_struct *)inode->i_security;
1703 isec->sid = SECINITSID_DEVNULL;
1704 isec->sclass = SECCLASS_CHR_FILE;
1705 isec->initialized = 1;
1707 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1708 d_add(dentry, inode);
1709 selinux_null = dentry;
1711 dentry = d_alloc_name(sb->s_root, "avc");
1717 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1721 ret = sel_make_avc_files(dentry);
1725 dentry = d_alloc_name(sb->s_root, "initial_contexts");
1731 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1735 ret = sel_make_initcon_files(dentry);
1739 dentry = d_alloc_name(sb->s_root, "class");
1745 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1751 dentry = d_alloc_name(sb->s_root, "policy_capabilities");
1757 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1761 policycap_dir = dentry;
1766 printk(KERN_ERR "SELinux: %s: failed while creating inodes\n",
1771 static int sel_get_sb(struct file_system_type *fs_type,
1772 int flags, const char *dev_name, void *data,
1773 struct vfsmount *mnt)
1775 return get_sb_single(fs_type, flags, data, sel_fill_super, mnt);
1778 static struct file_system_type sel_fs_type = {
1779 .name = "selinuxfs",
1780 .get_sb = sel_get_sb,
1781 .kill_sb = kill_litter_super,
1784 struct vfsmount *selinuxfs_mount;
1786 static int __init init_sel_fs(void)
1790 if (!selinux_enabled)
1792 err = register_filesystem(&sel_fs_type);
1794 selinuxfs_mount = kern_mount(&sel_fs_type);
1795 if (IS_ERR(selinuxfs_mount)) {
1796 printk(KERN_ERR "selinuxfs: could not mount!\n");
1797 err = PTR_ERR(selinuxfs_mount);
1798 selinuxfs_mount = NULL;
1804 __initcall(init_sel_fs);
1806 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
1807 void exit_sel_fs(void)
1809 unregister_filesystem(&sel_fs_type);