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 static int __init checkreqprot_setup(char *str)
52 unsigned long checkreqprot;
53 if (!strict_strtoul(str, 0, &checkreqprot))
54 selinux_checkreqprot = checkreqprot ? 1 : 0;
57 __setup("checkreqprot=", checkreqprot_setup);
59 static DEFINE_MUTEX(sel_mutex);
61 /* global data for booleans */
62 static struct dentry *bool_dir;
64 static char **bool_pending_names;
65 static int *bool_pending_values;
67 /* global data for classes */
68 static struct dentry *class_dir;
69 static unsigned long last_class_ino;
71 /* global data for policy capabilities */
72 static struct dentry *policycap_dir;
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 const struct task_security_struct *tsec;
84 tsec = __task_cred(tsk)->security;
91 return avc_has_perm(sid, SECINITSID_SECURITY,
92 SECCLASS_SECURITY, perms, NULL);
97 SEL_LOAD, /* load policy */
98 SEL_ENFORCE, /* get or set enforcing status */
99 SEL_CONTEXT, /* validate context */
100 SEL_ACCESS, /* compute access decision */
101 SEL_CREATE, /* compute create labeling decision */
102 SEL_RELABEL, /* compute relabeling decision */
103 SEL_USER, /* compute reachable user contexts */
104 SEL_POLICYVERS, /* return policy version for this kernel */
105 SEL_COMMIT_BOOLS, /* commit new boolean values */
106 SEL_MLS, /* return if MLS policy is enabled */
107 SEL_DISABLE, /* disable SELinux until next reboot */
108 SEL_MEMBER, /* compute polyinstantiation membership decision */
109 SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
110 SEL_COMPAT_NET, /* whether to use old compat network packet controls */
111 SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */
112 SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */
113 SEL_INO_NEXT, /* The next inode number to use */
116 static unsigned long sel_last_ino = SEL_INO_NEXT - 1;
118 #define SEL_INITCON_INO_OFFSET 0x01000000
119 #define SEL_BOOL_INO_OFFSET 0x02000000
120 #define SEL_CLASS_INO_OFFSET 0x04000000
121 #define SEL_POLICYCAP_INO_OFFSET 0x08000000
122 #define SEL_INO_MASK 0x00ffffff
125 static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
126 size_t count, loff_t *ppos)
128 char tmpbuf[TMPBUFLEN];
131 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing);
132 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
135 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
136 static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
137 size_t count, loff_t *ppos)
144 if (count >= PAGE_SIZE)
147 /* No partial writes. */
150 page = (char *)get_zeroed_page(GFP_KERNEL);
154 if (copy_from_user(page, buf, count))
158 if (sscanf(page, "%d", &new_value) != 1)
161 if (new_value != selinux_enforcing) {
162 length = task_has_security(current, SECURITY__SETENFORCE);
165 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
166 "enforcing=%d old_enforcing=%d auid=%u ses=%u",
167 new_value, selinux_enforcing,
168 audit_get_loginuid(current),
169 audit_get_sessionid(current));
170 selinux_enforcing = new_value;
171 if (selinux_enforcing)
173 selnl_notify_setenforce(selinux_enforcing);
177 free_page((unsigned long) page);
181 #define sel_write_enforce NULL
184 static const struct file_operations sel_enforce_ops = {
185 .read = sel_read_enforce,
186 .write = sel_write_enforce,
187 .llseek = generic_file_llseek,
190 static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf,
191 size_t count, loff_t *ppos)
193 char tmpbuf[TMPBUFLEN];
195 ino_t ino = filp->f_path.dentry->d_inode->i_ino;
196 int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
197 security_get_reject_unknown() : !security_get_allow_unknown();
199 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
200 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
203 static const struct file_operations sel_handle_unknown_ops = {
204 .read = sel_read_handle_unknown,
205 .llseek = generic_file_llseek,
208 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
209 static ssize_t sel_write_disable(struct file *file, const char __user *buf,
210 size_t count, loff_t *ppos)
216 extern int selinux_disable(void);
218 if (count >= PAGE_SIZE)
221 /* No partial writes. */
224 page = (char *)get_zeroed_page(GFP_KERNEL);
228 if (copy_from_user(page, buf, count))
232 if (sscanf(page, "%d", &new_value) != 1)
236 length = selinux_disable();
239 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
240 "selinux=0 auid=%u ses=%u",
241 audit_get_loginuid(current),
242 audit_get_sessionid(current));
247 free_page((unsigned long) page);
251 #define sel_write_disable NULL
254 static const struct file_operations sel_disable_ops = {
255 .write = sel_write_disable,
256 .llseek = generic_file_llseek,
259 static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
260 size_t count, loff_t *ppos)
262 char tmpbuf[TMPBUFLEN];
265 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
266 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
269 static const struct file_operations sel_policyvers_ops = {
270 .read = sel_read_policyvers,
271 .llseek = generic_file_llseek,
274 /* declaration for sel_write_load */
275 static int sel_make_bools(void);
276 static int sel_make_classes(void);
277 static int sel_make_policycap(void);
279 /* declaration for sel_make_class_dirs */
280 static int sel_make_dir(struct inode *dir, struct dentry *dentry,
283 static ssize_t sel_read_mls(struct file *filp, char __user *buf,
284 size_t count, loff_t *ppos)
286 char tmpbuf[TMPBUFLEN];
289 length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
290 security_mls_enabled());
291 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
294 static const struct file_operations sel_mls_ops = {
295 .read = sel_read_mls,
296 .llseek = generic_file_llseek,
299 static ssize_t sel_write_load(struct file *file, const char __user *buf,
300 size_t count, loff_t *ppos)
307 mutex_lock(&sel_mutex);
309 length = task_has_security(current, SECURITY__LOAD_POLICY);
314 /* No partial writes. */
319 if ((count > 64 * 1024 * 1024)
320 || (data = vmalloc(count)) == NULL) {
326 if (copy_from_user(data, buf, count) != 0)
329 length = security_load_policy(data, count);
333 ret = sel_make_bools();
339 ret = sel_make_classes();
345 ret = sel_make_policycap();
352 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
353 "policy loaded auid=%u ses=%u",
354 audit_get_loginuid(current),
355 audit_get_sessionid(current));
357 mutex_unlock(&sel_mutex);
362 static const struct file_operations sel_load_ops = {
363 .write = sel_write_load,
364 .llseek = generic_file_llseek,
367 static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
373 length = task_has_security(current, SECURITY__CHECK_CONTEXT);
377 length = security_context_to_sid(buf, size, &sid);
381 length = security_sid_to_context(sid, &canon, &len);
385 if (len > SIMPLE_TRANSACTION_LIMIT) {
386 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
387 "payload max\n", __func__, len);
392 memcpy(buf, canon, len);
399 static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
400 size_t count, loff_t *ppos)
402 char tmpbuf[TMPBUFLEN];
405 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot);
406 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
409 static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
410 size_t count, loff_t *ppos)
414 unsigned int new_value;
416 length = task_has_security(current, SECURITY__SETCHECKREQPROT);
420 if (count >= PAGE_SIZE)
423 /* No partial writes. */
426 page = (char *)get_zeroed_page(GFP_KERNEL);
430 if (copy_from_user(page, buf, count))
434 if (sscanf(page, "%u", &new_value) != 1)
437 selinux_checkreqprot = new_value ? 1 : 0;
440 free_page((unsigned long) page);
443 static const struct file_operations sel_checkreqprot_ops = {
444 .read = sel_read_checkreqprot,
445 .write = sel_write_checkreqprot,
446 .llseek = generic_file_llseek,
450 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
452 static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
453 static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
454 static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
455 static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
456 static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
458 static ssize_t (*write_op[])(struct file *, char *, size_t) = {
459 [SEL_ACCESS] = sel_write_access,
460 [SEL_CREATE] = sel_write_create,
461 [SEL_RELABEL] = sel_write_relabel,
462 [SEL_USER] = sel_write_user,
463 [SEL_MEMBER] = sel_write_member,
464 [SEL_CONTEXT] = sel_write_context,
467 static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
469 ino_t ino = file->f_path.dentry->d_inode->i_ino;
473 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
476 data = simple_transaction_get(file, buf, size);
478 return PTR_ERR(data);
480 rv = write_op[ino](file, data, size);
482 simple_transaction_set(file, rv);
488 static const struct file_operations transaction_ops = {
489 .write = selinux_transaction_write,
490 .read = simple_transaction_read,
491 .release = simple_transaction_release,
492 .llseek = generic_file_llseek,
496 * payload - write methods
497 * If the method has a response, the response should be put in buf,
498 * and the length returned. Otherwise return 0 or and -error.
501 static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
506 struct av_decision avd;
509 length = task_has_security(current, SECURITY__COMPUTE_AV);
514 scon = kzalloc(size + 1, GFP_KERNEL);
518 tcon = kzalloc(size + 1, GFP_KERNEL);
523 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
526 length = security_context_to_sid(scon, strlen(scon) + 1, &ssid);
529 length = security_context_to_sid(tcon, strlen(tcon) + 1, &tsid);
533 security_compute_av_user(ssid, tsid, tclass, &avd);
535 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
537 avd.allowed, 0xffffffff,
538 avd.auditallow, avd.auditdeny,
539 avd.seqno, avd.flags);
547 static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
550 u32 ssid, tsid, newsid;
556 length = task_has_security(current, SECURITY__COMPUTE_CREATE);
561 scon = kzalloc(size + 1, GFP_KERNEL);
565 tcon = kzalloc(size + 1, GFP_KERNEL);
570 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
573 length = security_context_to_sid(scon, strlen(scon) + 1, &ssid);
576 length = security_context_to_sid(tcon, strlen(tcon) + 1, &tsid);
580 length = security_transition_sid_user(ssid, tsid, tclass, &newsid);
584 length = security_sid_to_context(newsid, &newcon, &len);
588 if (len > SIMPLE_TRANSACTION_LIMIT) {
589 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
590 "payload max\n", __func__, len);
595 memcpy(buf, newcon, len);
606 static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
609 u32 ssid, tsid, newsid;
615 length = task_has_security(current, SECURITY__COMPUTE_RELABEL);
620 scon = kzalloc(size + 1, GFP_KERNEL);
624 tcon = kzalloc(size + 1, GFP_KERNEL);
629 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
632 length = security_context_to_sid(scon, strlen(scon) + 1, &ssid);
635 length = security_context_to_sid(tcon, strlen(tcon) + 1, &tsid);
639 length = security_change_sid(ssid, tsid, tclass, &newsid);
643 length = security_sid_to_context(newsid, &newcon, &len);
647 if (len > SIMPLE_TRANSACTION_LIMIT) {
652 memcpy(buf, newcon, len);
663 static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
665 char *con, *user, *ptr;
672 length = task_has_security(current, SECURITY__COMPUTE_USER);
677 con = kzalloc(size + 1, GFP_KERNEL);
681 user = kzalloc(size + 1, GFP_KERNEL);
686 if (sscanf(buf, "%s %s", con, user) != 2)
689 length = security_context_to_sid(con, strlen(con) + 1, &sid);
693 length = security_get_user_sids(sid, user, &sids, &nsids);
697 length = sprintf(buf, "%u", nsids) + 1;
699 for (i = 0; i < nsids; i++) {
700 rc = security_sid_to_context(sids[i], &newcon, &len);
705 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
710 memcpy(ptr, newcon, len);
724 static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
727 u32 ssid, tsid, newsid;
733 length = task_has_security(current, SECURITY__COMPUTE_MEMBER);
738 scon = kzalloc(size + 1, GFP_KERNEL);
742 tcon = kzalloc(size + 1, GFP_KERNEL);
747 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
750 length = security_context_to_sid(scon, strlen(scon) + 1, &ssid);
753 length = security_context_to_sid(tcon, strlen(tcon) + 1, &tsid);
757 length = security_member_sid(ssid, tsid, tclass, &newsid);
761 length = security_sid_to_context(newsid, &newcon, &len);
765 if (len > SIMPLE_TRANSACTION_LIMIT) {
766 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
767 "payload max\n", __func__, len);
772 memcpy(buf, newcon, len);
783 static struct inode *sel_make_inode(struct super_block *sb, int mode)
785 struct inode *ret = new_inode(sb);
789 ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
794 static ssize_t sel_read_bool(struct file *filep, char __user *buf,
795 size_t count, loff_t *ppos)
801 struct inode *inode = filep->f_path.dentry->d_inode;
802 unsigned index = inode->i_ino & SEL_INO_MASK;
803 const char *name = filep->f_path.dentry->d_name.name;
805 mutex_lock(&sel_mutex);
807 if (index >= bool_num || strcmp(name, bool_pending_names[index])) {
812 page = (char *)get_zeroed_page(GFP_KERNEL);
818 cur_enforcing = security_get_bool_value(index);
819 if (cur_enforcing < 0) {
823 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
824 bool_pending_values[index]);
825 ret = simple_read_from_buffer(buf, count, ppos, page, length);
827 mutex_unlock(&sel_mutex);
829 free_page((unsigned long)page);
833 static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
834 size_t count, loff_t *ppos)
839 struct inode *inode = filep->f_path.dentry->d_inode;
840 unsigned index = inode->i_ino & SEL_INO_MASK;
841 const char *name = filep->f_path.dentry->d_name.name;
843 mutex_lock(&sel_mutex);
845 length = task_has_security(current, SECURITY__SETBOOL);
849 if (index >= bool_num || strcmp(name, bool_pending_names[index])) {
854 if (count >= PAGE_SIZE) {
860 /* No partial writes. */
864 page = (char *)get_zeroed_page(GFP_KERNEL);
871 if (copy_from_user(page, buf, count))
875 if (sscanf(page, "%d", &new_value) != 1)
881 bool_pending_values[index] = new_value;
885 mutex_unlock(&sel_mutex);
887 free_page((unsigned long) page);
891 static const struct file_operations sel_bool_ops = {
892 .read = sel_read_bool,
893 .write = sel_write_bool,
894 .llseek = generic_file_llseek,
897 static ssize_t sel_commit_bools_write(struct file *filep,
898 const char __user *buf,
899 size_t count, loff_t *ppos)
905 mutex_lock(&sel_mutex);
907 length = task_has_security(current, SECURITY__SETBOOL);
911 if (count >= PAGE_SIZE) {
916 /* No partial writes. */
919 page = (char *)get_zeroed_page(GFP_KERNEL);
926 if (copy_from_user(page, buf, count))
930 if (sscanf(page, "%d", &new_value) != 1)
933 if (new_value && bool_pending_values)
934 security_set_bools(bool_num, bool_pending_values);
939 mutex_unlock(&sel_mutex);
941 free_page((unsigned long) page);
945 static const struct file_operations sel_commit_bools_ops = {
946 .write = sel_commit_bools_write,
947 .llseek = generic_file_llseek,
950 static void sel_remove_entries(struct dentry *de)
952 struct list_head *node;
954 spin_lock(&dcache_lock);
955 node = de->d_subdirs.next;
956 while (node != &de->d_subdirs) {
957 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
962 spin_unlock(&dcache_lock);
964 simple_unlink(de->d_inode, d);
966 spin_lock(&dcache_lock);
968 node = de->d_subdirs.next;
971 spin_unlock(&dcache_lock);
974 #define BOOL_DIR_NAME "booleans"
976 static int sel_make_bools(void)
980 struct dentry *dentry = NULL;
981 struct dentry *dir = bool_dir;
982 struct inode *inode = NULL;
983 struct inode_security_struct *isec;
984 char **names = NULL, *page;
989 /* remove any existing files */
990 for (i = 0; i < bool_num; i++)
991 kfree(bool_pending_names[i]);
992 kfree(bool_pending_names);
993 kfree(bool_pending_values);
994 bool_pending_names = NULL;
995 bool_pending_values = NULL;
997 sel_remove_entries(dir);
999 page = (char *)get_zeroed_page(GFP_KERNEL);
1003 ret = security_get_bools(&num, &names, &values);
1007 for (i = 0; i < num; i++) {
1008 dentry = d_alloc_name(dir, names[i]);
1013 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1019 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
1023 } else if (len >= PAGE_SIZE) {
1024 ret = -ENAMETOOLONG;
1027 isec = (struct inode_security_struct *)inode->i_security;
1028 ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid);
1032 isec->initialized = 1;
1033 inode->i_fop = &sel_bool_ops;
1034 inode->i_ino = i|SEL_BOOL_INO_OFFSET;
1035 d_add(dentry, inode);
1038 bool_pending_names = names;
1039 bool_pending_values = values;
1041 free_page((unsigned long)page);
1045 for (i = 0; i < num; i++)
1050 sel_remove_entries(dir);
1055 #define NULL_FILE_NAME "null"
1057 struct dentry *selinux_null;
1059 static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1060 size_t count, loff_t *ppos)
1062 char tmpbuf[TMPBUFLEN];
1065 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
1066 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1069 static ssize_t sel_write_avc_cache_threshold(struct file *file,
1070 const char __user *buf,
1071 size_t count, loff_t *ppos)
1078 if (count >= PAGE_SIZE) {
1084 /* No partial writes. */
1089 page = (char *)get_zeroed_page(GFP_KERNEL);
1095 if (copy_from_user(page, buf, count)) {
1100 if (sscanf(page, "%u", &new_value) != 1) {
1105 if (new_value != avc_cache_threshold) {
1106 ret = task_has_security(current, SECURITY__SETSECPARAM);
1109 avc_cache_threshold = new_value;
1113 free_page((unsigned long)page);
1118 static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1119 size_t count, loff_t *ppos)
1124 page = (char *)__get_free_page(GFP_KERNEL);
1129 ret = avc_get_hash_stats(page);
1131 ret = simple_read_from_buffer(buf, count, ppos, page, ret);
1132 free_page((unsigned long)page);
1137 static const struct file_operations sel_avc_cache_threshold_ops = {
1138 .read = sel_read_avc_cache_threshold,
1139 .write = sel_write_avc_cache_threshold,
1140 .llseek = generic_file_llseek,
1143 static const struct file_operations sel_avc_hash_stats_ops = {
1144 .read = sel_read_avc_hash_stats,
1145 .llseek = generic_file_llseek,
1148 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1149 static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1153 for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
1154 if (!cpu_possible(cpu))
1157 return &per_cpu(avc_cache_stats, cpu);
1162 static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1164 loff_t n = *pos - 1;
1167 return SEQ_START_TOKEN;
1169 return sel_avc_get_stat_idx(&n);
1172 static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1174 return sel_avc_get_stat_idx(pos);
1177 static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1179 struct avc_cache_stats *st = v;
1181 if (v == SEQ_START_TOKEN)
1182 seq_printf(seq, "lookups hits misses allocations reclaims "
1185 seq_printf(seq, "%u %u %u %u %u %u\n", st->lookups,
1186 st->hits, st->misses, st->allocations,
1187 st->reclaims, st->frees);
1191 static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1194 static const struct seq_operations sel_avc_cache_stats_seq_ops = {
1195 .start = sel_avc_stats_seq_start,
1196 .next = sel_avc_stats_seq_next,
1197 .show = sel_avc_stats_seq_show,
1198 .stop = sel_avc_stats_seq_stop,
1201 static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1203 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1206 static const struct file_operations sel_avc_cache_stats_ops = {
1207 .open = sel_open_avc_cache_stats,
1209 .llseek = seq_lseek,
1210 .release = seq_release,
1214 static int sel_make_avc_files(struct dentry *dir)
1217 static struct tree_descr files[] = {
1218 { "cache_threshold",
1219 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1220 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1221 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1222 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1226 for (i = 0; i < ARRAY_SIZE(files); i++) {
1227 struct inode *inode;
1228 struct dentry *dentry;
1230 dentry = d_alloc_name(dir, files[i].name);
1236 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1241 inode->i_fop = files[i].ops;
1242 inode->i_ino = ++sel_last_ino;
1243 d_add(dentry, inode);
1249 static ssize_t sel_read_initcon(struct file *file, char __user *buf,
1250 size_t count, loff_t *ppos)
1252 struct inode *inode;
1257 inode = file->f_path.dentry->d_inode;
1258 sid = inode->i_ino&SEL_INO_MASK;
1259 ret = security_sid_to_context(sid, &con, &len);
1263 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1268 static const struct file_operations sel_initcon_ops = {
1269 .read = sel_read_initcon,
1270 .llseek = generic_file_llseek,
1273 static int sel_make_initcon_files(struct dentry *dir)
1277 for (i = 1; i <= SECINITSID_NUM; i++) {
1278 struct inode *inode;
1279 struct dentry *dentry;
1280 dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
1286 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1291 inode->i_fop = &sel_initcon_ops;
1292 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1293 d_add(dentry, inode);
1299 static inline unsigned int sel_div(unsigned long a, unsigned long b)
1301 return a / b - (a % b < 0);
1304 static inline unsigned long sel_class_to_ino(u16 class)
1306 return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1309 static inline u16 sel_ino_to_class(unsigned long ino)
1311 return sel_div(ino & SEL_INO_MASK, SEL_VEC_MAX + 1);
1314 static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1316 return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1319 static inline u32 sel_ino_to_perm(unsigned long ino)
1321 return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1324 static ssize_t sel_read_class(struct file *file, char __user *buf,
1325 size_t count, loff_t *ppos)
1329 unsigned long ino = file->f_path.dentry->d_inode->i_ino;
1331 page = (char *)__get_free_page(GFP_KERNEL);
1337 len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_class(ino));
1338 rc = simple_read_from_buffer(buf, count, ppos, page, len);
1339 free_page((unsigned long)page);
1344 static const struct file_operations sel_class_ops = {
1345 .read = sel_read_class,
1346 .llseek = generic_file_llseek,
1349 static ssize_t sel_read_perm(struct file *file, char __user *buf,
1350 size_t count, loff_t *ppos)
1354 unsigned long ino = file->f_path.dentry->d_inode->i_ino;
1356 page = (char *)__get_free_page(GFP_KERNEL);
1362 len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_perm(ino));
1363 rc = simple_read_from_buffer(buf, count, ppos, page, len);
1364 free_page((unsigned long)page);
1369 static const struct file_operations sel_perm_ops = {
1370 .read = sel_read_perm,
1371 .llseek = generic_file_llseek,
1374 static ssize_t sel_read_policycap(struct file *file, char __user *buf,
1375 size_t count, loff_t *ppos)
1378 char tmpbuf[TMPBUFLEN];
1380 unsigned long i_ino = file->f_path.dentry->d_inode->i_ino;
1382 value = security_policycap_supported(i_ino & SEL_INO_MASK);
1383 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
1385 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1388 static const struct file_operations sel_policycap_ops = {
1389 .read = sel_read_policycap,
1390 .llseek = generic_file_llseek,
1393 static int sel_make_perm_files(char *objclass, int classvalue,
1396 int i, rc = 0, nperms;
1399 rc = security_get_permissions(objclass, &perms, &nperms);
1403 for (i = 0; i < nperms; i++) {
1404 struct inode *inode;
1405 struct dentry *dentry;
1407 dentry = d_alloc_name(dir, perms[i]);
1413 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1418 inode->i_fop = &sel_perm_ops;
1419 /* i+1 since perm values are 1-indexed */
1420 inode->i_ino = sel_perm_to_ino(classvalue, i + 1);
1421 d_add(dentry, inode);
1425 for (i = 0; i < nperms; i++)
1432 static int sel_make_class_dir_entries(char *classname, int index,
1435 struct dentry *dentry = NULL;
1436 struct inode *inode = NULL;
1439 dentry = d_alloc_name(dir, "index");
1445 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1451 inode->i_fop = &sel_class_ops;
1452 inode->i_ino = sel_class_to_ino(index);
1453 d_add(dentry, inode);
1455 dentry = d_alloc_name(dir, "perms");
1461 rc = sel_make_dir(dir->d_inode, dentry, &last_class_ino);
1465 rc = sel_make_perm_files(classname, index, dentry);
1471 static void sel_remove_classes(void)
1473 struct list_head *class_node;
1475 list_for_each(class_node, &class_dir->d_subdirs) {
1476 struct dentry *class_subdir = list_entry(class_node,
1477 struct dentry, d_u.d_child);
1478 struct list_head *class_subdir_node;
1480 list_for_each(class_subdir_node, &class_subdir->d_subdirs) {
1481 struct dentry *d = list_entry(class_subdir_node,
1482 struct dentry, d_u.d_child);
1485 if (d->d_inode->i_mode & S_IFDIR)
1486 sel_remove_entries(d);
1489 sel_remove_entries(class_subdir);
1492 sel_remove_entries(class_dir);
1495 static int sel_make_classes(void)
1497 int rc = 0, nclasses, i;
1500 /* delete any existing entries */
1501 sel_remove_classes();
1503 rc = security_get_classes(&classes, &nclasses);
1507 /* +2 since classes are 1-indexed */
1508 last_class_ino = sel_class_to_ino(nclasses + 2);
1510 for (i = 0; i < nclasses; i++) {
1511 struct dentry *class_name_dir;
1513 class_name_dir = d_alloc_name(class_dir, classes[i]);
1514 if (!class_name_dir) {
1519 rc = sel_make_dir(class_dir->d_inode, class_name_dir,
1524 /* i+1 since class values are 1-indexed */
1525 rc = sel_make_class_dir_entries(classes[i], i + 1,
1532 for (i = 0; i < nclasses; i++)
1539 static int sel_make_policycap(void)
1542 struct dentry *dentry = NULL;
1543 struct inode *inode = NULL;
1545 sel_remove_entries(policycap_dir);
1547 for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
1548 if (iter < ARRAY_SIZE(policycap_names))
1549 dentry = d_alloc_name(policycap_dir,
1550 policycap_names[iter]);
1552 dentry = d_alloc_name(policycap_dir, "unknown");
1557 inode = sel_make_inode(policycap_dir->d_sb, S_IFREG | S_IRUGO);
1561 inode->i_fop = &sel_policycap_ops;
1562 inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
1563 d_add(dentry, inode);
1569 static int sel_make_dir(struct inode *dir, struct dentry *dentry,
1573 struct inode *inode;
1575 inode = sel_make_inode(dir->i_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1580 inode->i_op = &simple_dir_inode_operations;
1581 inode->i_fop = &simple_dir_operations;
1582 inode->i_ino = ++(*ino);
1583 /* directory inodes start off with i_nlink == 2 (for "." entry) */
1585 d_add(dentry, inode);
1586 /* bump link count on parent directory, too */
1592 static int sel_fill_super(struct super_block *sb, void *data, int silent)
1595 struct dentry *dentry;
1596 struct inode *inode, *root_inode;
1597 struct inode_security_struct *isec;
1599 static struct tree_descr selinux_files[] = {
1600 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1601 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
1602 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
1603 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1604 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1605 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1606 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1607 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1608 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1609 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1610 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1611 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1612 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
1613 [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
1614 [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
1617 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1621 root_inode = sb->s_root->d_inode;
1623 dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME);
1629 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1635 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
1641 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
1646 inode->i_ino = ++sel_last_ino;
1647 isec = (struct inode_security_struct *)inode->i_security;
1648 isec->sid = SECINITSID_DEVNULL;
1649 isec->sclass = SECCLASS_CHR_FILE;
1650 isec->initialized = 1;
1652 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1653 d_add(dentry, inode);
1654 selinux_null = dentry;
1656 dentry = d_alloc_name(sb->s_root, "avc");
1662 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1666 ret = sel_make_avc_files(dentry);
1670 dentry = d_alloc_name(sb->s_root, "initial_contexts");
1676 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1680 ret = sel_make_initcon_files(dentry);
1684 dentry = d_alloc_name(sb->s_root, "class");
1690 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1696 dentry = d_alloc_name(sb->s_root, "policy_capabilities");
1702 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1706 policycap_dir = dentry;
1711 printk(KERN_ERR "SELinux: %s: failed while creating inodes\n",
1716 static int sel_get_sb(struct file_system_type *fs_type,
1717 int flags, const char *dev_name, void *data,
1718 struct vfsmount *mnt)
1720 return get_sb_single(fs_type, flags, data, sel_fill_super, mnt);
1723 static struct file_system_type sel_fs_type = {
1724 .name = "selinuxfs",
1725 .get_sb = sel_get_sb,
1726 .kill_sb = kill_litter_super,
1729 struct vfsmount *selinuxfs_mount;
1731 static int __init init_sel_fs(void)
1735 if (!selinux_enabled)
1737 err = register_filesystem(&sel_fs_type);
1739 selinuxfs_mount = kern_mount(&sel_fs_type);
1740 if (IS_ERR(selinuxfs_mount)) {
1741 printk(KERN_ERR "selinuxfs: could not mount!\n");
1742 err = PTR_ERR(selinuxfs_mount);
1743 selinuxfs_mount = NULL;
1749 __initcall(init_sel_fs);
1751 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
1752 void exit_sel_fs(void)
1754 unregister_filesystem(&sel_fs_type);