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,
189 static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf,
190 size_t count, loff_t *ppos)
192 char tmpbuf[TMPBUFLEN];
194 ino_t ino = filp->f_path.dentry->d_inode->i_ino;
195 int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
196 security_get_reject_unknown() : !security_get_allow_unknown();
198 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
199 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
202 static const struct file_operations sel_handle_unknown_ops = {
203 .read = sel_read_handle_unknown,
206 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
207 static ssize_t sel_write_disable(struct file *file, const char __user *buf,
208 size_t count, loff_t *ppos)
214 extern int selinux_disable(void);
216 if (count >= PAGE_SIZE)
219 /* No partial writes. */
222 page = (char *)get_zeroed_page(GFP_KERNEL);
226 if (copy_from_user(page, buf, count))
230 if (sscanf(page, "%d", &new_value) != 1)
234 length = selinux_disable();
237 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
238 "selinux=0 auid=%u ses=%u",
239 audit_get_loginuid(current),
240 audit_get_sessionid(current));
245 free_page((unsigned long) page);
249 #define sel_write_disable NULL
252 static const struct file_operations sel_disable_ops = {
253 .write = sel_write_disable,
256 static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
257 size_t count, loff_t *ppos)
259 char tmpbuf[TMPBUFLEN];
262 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
263 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
266 static const struct file_operations sel_policyvers_ops = {
267 .read = sel_read_policyvers,
270 /* declaration for sel_write_load */
271 static int sel_make_bools(void);
272 static int sel_make_classes(void);
273 static int sel_make_policycap(void);
275 /* declaration for sel_make_class_dirs */
276 static int sel_make_dir(struct inode *dir, struct dentry *dentry,
279 static ssize_t sel_read_mls(struct file *filp, char __user *buf,
280 size_t count, loff_t *ppos)
282 char tmpbuf[TMPBUFLEN];
285 length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
286 security_mls_enabled());
287 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
290 static const struct file_operations sel_mls_ops = {
291 .read = sel_read_mls,
294 static ssize_t sel_write_load(struct file *file, const char __user *buf,
295 size_t count, loff_t *ppos)
302 mutex_lock(&sel_mutex);
304 length = task_has_security(current, SECURITY__LOAD_POLICY);
309 /* No partial writes. */
314 if ((count > 64 * 1024 * 1024)
315 || (data = vmalloc(count)) == NULL) {
321 if (copy_from_user(data, buf, count) != 0)
324 length = security_load_policy(data, count);
328 ret = sel_make_bools();
334 ret = sel_make_classes();
340 ret = sel_make_policycap();
347 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
348 "policy loaded auid=%u ses=%u",
349 audit_get_loginuid(current),
350 audit_get_sessionid(current));
352 mutex_unlock(&sel_mutex);
357 static const struct file_operations sel_load_ops = {
358 .write = sel_write_load,
361 static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
367 length = task_has_security(current, SECURITY__CHECK_CONTEXT);
371 length = security_context_to_sid(buf, size, &sid);
375 length = security_sid_to_context(sid, &canon, &len);
379 if (len > SIMPLE_TRANSACTION_LIMIT) {
380 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
381 "payload max\n", __func__, len);
386 memcpy(buf, canon, len);
393 static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
394 size_t count, loff_t *ppos)
396 char tmpbuf[TMPBUFLEN];
399 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot);
400 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
403 static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
404 size_t count, loff_t *ppos)
408 unsigned int new_value;
410 length = task_has_security(current, SECURITY__SETCHECKREQPROT);
414 if (count >= PAGE_SIZE)
417 /* No partial writes. */
420 page = (char *)get_zeroed_page(GFP_KERNEL);
424 if (copy_from_user(page, buf, count))
428 if (sscanf(page, "%u", &new_value) != 1)
431 selinux_checkreqprot = new_value ? 1 : 0;
434 free_page((unsigned long) page);
437 static const struct file_operations sel_checkreqprot_ops = {
438 .read = sel_read_checkreqprot,
439 .write = sel_write_checkreqprot,
443 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
445 static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
446 static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
447 static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
448 static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
449 static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
451 static ssize_t (*write_op[])(struct file *, char *, size_t) = {
452 [SEL_ACCESS] = sel_write_access,
453 [SEL_CREATE] = sel_write_create,
454 [SEL_RELABEL] = sel_write_relabel,
455 [SEL_USER] = sel_write_user,
456 [SEL_MEMBER] = sel_write_member,
457 [SEL_CONTEXT] = sel_write_context,
460 static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
462 ino_t ino = file->f_path.dentry->d_inode->i_ino;
466 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
469 data = simple_transaction_get(file, buf, size);
471 return PTR_ERR(data);
473 rv = write_op[ino](file, data, size);
475 simple_transaction_set(file, rv);
481 static const struct file_operations transaction_ops = {
482 .write = selinux_transaction_write,
483 .read = simple_transaction_read,
484 .release = simple_transaction_release,
488 * payload - write methods
489 * If the method has a response, the response should be put in buf,
490 * and the length returned. Otherwise return 0 or and -error.
493 static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
498 struct av_decision avd;
501 length = task_has_security(current, SECURITY__COMPUTE_AV);
506 scon = kzalloc(size+1, GFP_KERNEL);
510 tcon = kzalloc(size+1, GFP_KERNEL);
515 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
518 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
521 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
525 security_compute_av_user(ssid, tsid, tclass, &avd);
527 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
529 avd.allowed, 0xffffffff,
530 avd.auditallow, avd.auditdeny,
531 avd.seqno, avd.flags);
539 static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
542 u32 ssid, tsid, newsid;
548 length = task_has_security(current, SECURITY__COMPUTE_CREATE);
553 scon = kzalloc(size+1, GFP_KERNEL);
557 tcon = kzalloc(size+1, GFP_KERNEL);
562 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
565 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
568 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
572 length = security_transition_sid_user(ssid, tsid, tclass, &newsid);
576 length = security_sid_to_context(newsid, &newcon, &len);
580 if (len > SIMPLE_TRANSACTION_LIMIT) {
581 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
582 "payload max\n", __func__, len);
587 memcpy(buf, newcon, len);
598 static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
601 u32 ssid, tsid, newsid;
607 length = task_has_security(current, SECURITY__COMPUTE_RELABEL);
612 scon = kzalloc(size+1, GFP_KERNEL);
616 tcon = kzalloc(size+1, GFP_KERNEL);
621 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
624 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
627 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
631 length = security_change_sid(ssid, tsid, tclass, &newsid);
635 length = security_sid_to_context(newsid, &newcon, &len);
639 if (len > SIMPLE_TRANSACTION_LIMIT) {
644 memcpy(buf, newcon, len);
655 static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
657 char *con, *user, *ptr;
664 length = task_has_security(current, SECURITY__COMPUTE_USER);
669 con = kzalloc(size+1, GFP_KERNEL);
673 user = kzalloc(size+1, GFP_KERNEL);
678 if (sscanf(buf, "%s %s", con, user) != 2)
681 length = security_context_to_sid(con, strlen(con)+1, &sid);
685 length = security_get_user_sids(sid, user, &sids, &nsids);
689 length = sprintf(buf, "%u", nsids) + 1;
691 for (i = 0; i < nsids; i++) {
692 rc = security_sid_to_context(sids[i], &newcon, &len);
697 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
702 memcpy(ptr, newcon, len);
716 static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
719 u32 ssid, tsid, newsid;
725 length = task_has_security(current, SECURITY__COMPUTE_MEMBER);
730 scon = kzalloc(size+1, GFP_KERNEL);
734 tcon = kzalloc(size+1, GFP_KERNEL);
739 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
742 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
745 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
749 length = security_member_sid(ssid, tsid, tclass, &newsid);
753 length = security_sid_to_context(newsid, &newcon, &len);
757 if (len > SIMPLE_TRANSACTION_LIMIT) {
758 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
759 "payload max\n", __func__, len);
764 memcpy(buf, newcon, len);
775 static struct inode *sel_make_inode(struct super_block *sb, int mode)
777 struct inode *ret = new_inode(sb);
781 ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
786 static ssize_t sel_read_bool(struct file *filep, char __user *buf,
787 size_t count, loff_t *ppos)
793 struct inode *inode = filep->f_path.dentry->d_inode;
794 unsigned index = inode->i_ino & SEL_INO_MASK;
795 const char *name = filep->f_path.dentry->d_name.name;
797 mutex_lock(&sel_mutex);
799 if (index >= bool_num || strcmp(name, bool_pending_names[index])) {
804 page = (char *)get_zeroed_page(GFP_KERNEL);
810 cur_enforcing = security_get_bool_value(index);
811 if (cur_enforcing < 0) {
815 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
816 bool_pending_values[index]);
817 ret = simple_read_from_buffer(buf, count, ppos, page, length);
819 mutex_unlock(&sel_mutex);
821 free_page((unsigned long)page);
825 static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
826 size_t count, loff_t *ppos)
831 struct inode *inode = filep->f_path.dentry->d_inode;
832 unsigned index = inode->i_ino & SEL_INO_MASK;
833 const char *name = filep->f_path.dentry->d_name.name;
835 mutex_lock(&sel_mutex);
837 length = task_has_security(current, SECURITY__SETBOOL);
841 if (index >= bool_num || strcmp(name, bool_pending_names[index])) {
846 if (count >= PAGE_SIZE) {
852 /* No partial writes. */
856 page = (char *)get_zeroed_page(GFP_KERNEL);
863 if (copy_from_user(page, buf, count))
867 if (sscanf(page, "%d", &new_value) != 1)
873 bool_pending_values[index] = new_value;
877 mutex_unlock(&sel_mutex);
879 free_page((unsigned long) page);
883 static const struct file_operations sel_bool_ops = {
884 .read = sel_read_bool,
885 .write = sel_write_bool,
888 static ssize_t sel_commit_bools_write(struct file *filep,
889 const char __user *buf,
890 size_t count, loff_t *ppos)
896 mutex_lock(&sel_mutex);
898 length = task_has_security(current, SECURITY__SETBOOL);
902 if (count >= PAGE_SIZE) {
907 /* No partial writes. */
910 page = (char *)get_zeroed_page(GFP_KERNEL);
917 if (copy_from_user(page, buf, count))
921 if (sscanf(page, "%d", &new_value) != 1)
924 if (new_value && bool_pending_values)
925 security_set_bools(bool_num, bool_pending_values);
930 mutex_unlock(&sel_mutex);
932 free_page((unsigned long) page);
936 static const struct file_operations sel_commit_bools_ops = {
937 .write = sel_commit_bools_write,
940 static void sel_remove_entries(struct dentry *de)
942 struct list_head *node;
944 spin_lock(&dcache_lock);
945 node = de->d_subdirs.next;
946 while (node != &de->d_subdirs) {
947 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
952 spin_unlock(&dcache_lock);
954 simple_unlink(de->d_inode, d);
956 spin_lock(&dcache_lock);
958 node = de->d_subdirs.next;
961 spin_unlock(&dcache_lock);
964 #define BOOL_DIR_NAME "booleans"
966 static int sel_make_bools(void)
970 struct dentry *dentry = NULL;
971 struct dentry *dir = bool_dir;
972 struct inode *inode = NULL;
973 struct inode_security_struct *isec;
974 char **names = NULL, *page;
979 /* remove any existing files */
980 kfree(bool_pending_names);
981 kfree(bool_pending_values);
982 bool_pending_names = NULL;
983 bool_pending_values = NULL;
985 sel_remove_entries(dir);
987 page = (char *)get_zeroed_page(GFP_KERNEL);
991 ret = security_get_bools(&num, &names, &values);
995 for (i = 0; i < num; i++) {
996 dentry = d_alloc_name(dir, names[i]);
1001 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1007 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
1011 } else if (len >= PAGE_SIZE) {
1012 ret = -ENAMETOOLONG;
1015 isec = (struct inode_security_struct *)inode->i_security;
1016 ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid);
1020 isec->initialized = 1;
1021 inode->i_fop = &sel_bool_ops;
1022 inode->i_ino = i|SEL_BOOL_INO_OFFSET;
1023 d_add(dentry, inode);
1026 bool_pending_names = names;
1027 bool_pending_values = values;
1029 free_page((unsigned long)page);
1033 for (i = 0; i < num; i++)
1038 sel_remove_entries(dir);
1043 #define NULL_FILE_NAME "null"
1045 struct dentry *selinux_null;
1047 static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1048 size_t count, loff_t *ppos)
1050 char tmpbuf[TMPBUFLEN];
1053 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
1054 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1057 static ssize_t sel_write_avc_cache_threshold(struct file *file,
1058 const char __user *buf,
1059 size_t count, loff_t *ppos)
1066 if (count >= PAGE_SIZE) {
1072 /* No partial writes. */
1077 page = (char *)get_zeroed_page(GFP_KERNEL);
1083 if (copy_from_user(page, buf, count)) {
1088 if (sscanf(page, "%u", &new_value) != 1) {
1093 if (new_value != avc_cache_threshold) {
1094 ret = task_has_security(current, SECURITY__SETSECPARAM);
1097 avc_cache_threshold = new_value;
1101 free_page((unsigned long)page);
1106 static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1107 size_t count, loff_t *ppos)
1112 page = (char *)__get_free_page(GFP_KERNEL);
1117 ret = avc_get_hash_stats(page);
1119 ret = simple_read_from_buffer(buf, count, ppos, page, ret);
1120 free_page((unsigned long)page);
1125 static const struct file_operations sel_avc_cache_threshold_ops = {
1126 .read = sel_read_avc_cache_threshold,
1127 .write = sel_write_avc_cache_threshold,
1130 static const struct file_operations sel_avc_hash_stats_ops = {
1131 .read = sel_read_avc_hash_stats,
1134 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1135 static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1139 for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
1140 if (!cpu_possible(cpu))
1143 return &per_cpu(avc_cache_stats, cpu);
1148 static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1150 loff_t n = *pos - 1;
1153 return SEQ_START_TOKEN;
1155 return sel_avc_get_stat_idx(&n);
1158 static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1160 return sel_avc_get_stat_idx(pos);
1163 static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1165 struct avc_cache_stats *st = v;
1167 if (v == SEQ_START_TOKEN)
1168 seq_printf(seq, "lookups hits misses allocations reclaims "
1171 seq_printf(seq, "%u %u %u %u %u %u\n", st->lookups,
1172 st->hits, st->misses, st->allocations,
1173 st->reclaims, st->frees);
1177 static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1180 static const struct seq_operations sel_avc_cache_stats_seq_ops = {
1181 .start = sel_avc_stats_seq_start,
1182 .next = sel_avc_stats_seq_next,
1183 .show = sel_avc_stats_seq_show,
1184 .stop = sel_avc_stats_seq_stop,
1187 static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1189 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1192 static const struct file_operations sel_avc_cache_stats_ops = {
1193 .open = sel_open_avc_cache_stats,
1195 .llseek = seq_lseek,
1196 .release = seq_release,
1200 static int sel_make_avc_files(struct dentry *dir)
1203 static struct tree_descr files[] = {
1204 { "cache_threshold",
1205 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1206 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1207 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1208 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1212 for (i = 0; i < ARRAY_SIZE(files); i++) {
1213 struct inode *inode;
1214 struct dentry *dentry;
1216 dentry = d_alloc_name(dir, files[i].name);
1222 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1227 inode->i_fop = files[i].ops;
1228 inode->i_ino = ++sel_last_ino;
1229 d_add(dentry, inode);
1235 static ssize_t sel_read_initcon(struct file *file, char __user *buf,
1236 size_t count, loff_t *ppos)
1238 struct inode *inode;
1243 inode = file->f_path.dentry->d_inode;
1244 sid = inode->i_ino&SEL_INO_MASK;
1245 ret = security_sid_to_context(sid, &con, &len);
1249 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1254 static const struct file_operations sel_initcon_ops = {
1255 .read = sel_read_initcon,
1258 static int sel_make_initcon_files(struct dentry *dir)
1262 for (i = 1; i <= SECINITSID_NUM; i++) {
1263 struct inode *inode;
1264 struct dentry *dentry;
1265 dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
1271 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1276 inode->i_fop = &sel_initcon_ops;
1277 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1278 d_add(dentry, inode);
1284 static inline unsigned int sel_div(unsigned long a, unsigned long b)
1286 return a / b - (a % b < 0);
1289 static inline unsigned long sel_class_to_ino(u16 class)
1291 return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1294 static inline u16 sel_ino_to_class(unsigned long ino)
1296 return sel_div(ino & SEL_INO_MASK, SEL_VEC_MAX + 1);
1299 static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1301 return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1304 static inline u32 sel_ino_to_perm(unsigned long ino)
1306 return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1309 static ssize_t sel_read_class(struct file *file, char __user *buf,
1310 size_t count, loff_t *ppos)
1314 unsigned long ino = file->f_path.dentry->d_inode->i_ino;
1316 page = (char *)__get_free_page(GFP_KERNEL);
1322 len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_class(ino));
1323 rc = simple_read_from_buffer(buf, count, ppos, page, len);
1324 free_page((unsigned long)page);
1329 static const struct file_operations sel_class_ops = {
1330 .read = sel_read_class,
1333 static ssize_t sel_read_perm(struct file *file, char __user *buf,
1334 size_t count, loff_t *ppos)
1338 unsigned long ino = file->f_path.dentry->d_inode->i_ino;
1340 page = (char *)__get_free_page(GFP_KERNEL);
1346 len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_perm(ino));
1347 rc = simple_read_from_buffer(buf, count, ppos, page, len);
1348 free_page((unsigned long)page);
1353 static const struct file_operations sel_perm_ops = {
1354 .read = sel_read_perm,
1357 static ssize_t sel_read_policycap(struct file *file, char __user *buf,
1358 size_t count, loff_t *ppos)
1361 char tmpbuf[TMPBUFLEN];
1363 unsigned long i_ino = file->f_path.dentry->d_inode->i_ino;
1365 value = security_policycap_supported(i_ino & SEL_INO_MASK);
1366 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
1368 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1371 static const struct file_operations sel_policycap_ops = {
1372 .read = sel_read_policycap,
1375 static int sel_make_perm_files(char *objclass, int classvalue,
1378 int i, rc = 0, nperms;
1381 rc = security_get_permissions(objclass, &perms, &nperms);
1385 for (i = 0; i < nperms; i++) {
1386 struct inode *inode;
1387 struct dentry *dentry;
1389 dentry = d_alloc_name(dir, perms[i]);
1395 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1400 inode->i_fop = &sel_perm_ops;
1401 /* i+1 since perm values are 1-indexed */
1402 inode->i_ino = sel_perm_to_ino(classvalue, i+1);
1403 d_add(dentry, inode);
1407 for (i = 0; i < nperms; i++)
1414 static int sel_make_class_dir_entries(char *classname, int index,
1417 struct dentry *dentry = NULL;
1418 struct inode *inode = NULL;
1421 dentry = d_alloc_name(dir, "index");
1427 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1433 inode->i_fop = &sel_class_ops;
1434 inode->i_ino = sel_class_to_ino(index);
1435 d_add(dentry, inode);
1437 dentry = d_alloc_name(dir, "perms");
1443 rc = sel_make_dir(dir->d_inode, dentry, &last_class_ino);
1447 rc = sel_make_perm_files(classname, index, dentry);
1453 static void sel_remove_classes(void)
1455 struct list_head *class_node;
1457 list_for_each(class_node, &class_dir->d_subdirs) {
1458 struct dentry *class_subdir = list_entry(class_node,
1459 struct dentry, d_u.d_child);
1460 struct list_head *class_subdir_node;
1462 list_for_each(class_subdir_node, &class_subdir->d_subdirs) {
1463 struct dentry *d = list_entry(class_subdir_node,
1464 struct dentry, d_u.d_child);
1467 if (d->d_inode->i_mode & S_IFDIR)
1468 sel_remove_entries(d);
1471 sel_remove_entries(class_subdir);
1474 sel_remove_entries(class_dir);
1477 static int sel_make_classes(void)
1479 int rc = 0, nclasses, i;
1482 /* delete any existing entries */
1483 sel_remove_classes();
1485 rc = security_get_classes(&classes, &nclasses);
1489 /* +2 since classes are 1-indexed */
1490 last_class_ino = sel_class_to_ino(nclasses+2);
1492 for (i = 0; i < nclasses; i++) {
1493 struct dentry *class_name_dir;
1495 class_name_dir = d_alloc_name(class_dir, classes[i]);
1496 if (!class_name_dir) {
1501 rc = sel_make_dir(class_dir->d_inode, class_name_dir,
1506 /* i+1 since class values are 1-indexed */
1507 rc = sel_make_class_dir_entries(classes[i], i+1,
1514 for (i = 0; i < nclasses; i++)
1521 static int sel_make_policycap(void)
1524 struct dentry *dentry = NULL;
1525 struct inode *inode = NULL;
1527 sel_remove_entries(policycap_dir);
1529 for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
1530 if (iter < ARRAY_SIZE(policycap_names))
1531 dentry = d_alloc_name(policycap_dir,
1532 policycap_names[iter]);
1534 dentry = d_alloc_name(policycap_dir, "unknown");
1539 inode = sel_make_inode(policycap_dir->d_sb, S_IFREG | S_IRUGO);
1543 inode->i_fop = &sel_policycap_ops;
1544 inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
1545 d_add(dentry, inode);
1551 static int sel_make_dir(struct inode *dir, struct dentry *dentry,
1555 struct inode *inode;
1557 inode = sel_make_inode(dir->i_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1562 inode->i_op = &simple_dir_inode_operations;
1563 inode->i_fop = &simple_dir_operations;
1564 inode->i_ino = ++(*ino);
1565 /* directory inodes start off with i_nlink == 2 (for "." entry) */
1567 d_add(dentry, inode);
1568 /* bump link count on parent directory, too */
1574 static int sel_fill_super(struct super_block *sb, void *data, int silent)
1577 struct dentry *dentry;
1578 struct inode *inode, *root_inode;
1579 struct inode_security_struct *isec;
1581 static struct tree_descr selinux_files[] = {
1582 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1583 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
1584 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
1585 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1586 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1587 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1588 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1589 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1590 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1591 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1592 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1593 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1594 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
1595 [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
1596 [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
1599 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1603 root_inode = sb->s_root->d_inode;
1605 dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME);
1611 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1617 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
1623 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
1628 inode->i_ino = ++sel_last_ino;
1629 isec = (struct inode_security_struct *)inode->i_security;
1630 isec->sid = SECINITSID_DEVNULL;
1631 isec->sclass = SECCLASS_CHR_FILE;
1632 isec->initialized = 1;
1634 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1635 d_add(dentry, inode);
1636 selinux_null = dentry;
1638 dentry = d_alloc_name(sb->s_root, "avc");
1644 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1648 ret = sel_make_avc_files(dentry);
1652 dentry = d_alloc_name(sb->s_root, "initial_contexts");
1658 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1662 ret = sel_make_initcon_files(dentry);
1666 dentry = d_alloc_name(sb->s_root, "class");
1672 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1678 dentry = d_alloc_name(sb->s_root, "policy_capabilities");
1684 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1688 policycap_dir = dentry;
1693 printk(KERN_ERR "SELinux: %s: failed while creating inodes\n",
1698 static int sel_get_sb(struct file_system_type *fs_type,
1699 int flags, const char *dev_name, void *data,
1700 struct vfsmount *mnt)
1702 return get_sb_single(fs_type, flags, data, sel_fill_super, mnt);
1705 static struct file_system_type sel_fs_type = {
1706 .name = "selinuxfs",
1707 .get_sb = sel_get_sb,
1708 .kill_sb = kill_litter_super,
1711 struct vfsmount *selinuxfs_mount;
1713 static int __init init_sel_fs(void)
1717 if (!selinux_enabled)
1719 err = register_filesystem(&sel_fs_type);
1721 selinuxfs_mount = kern_mount(&sel_fs_type);
1722 if (IS_ERR(selinuxfs_mount)) {
1723 printk(KERN_ERR "selinuxfs: could not mount!\n");
1724 err = PTR_ERR(selinuxfs_mount);
1725 selinuxfs_mount = NULL;
1731 __initcall(init_sel_fs);
1733 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
1734 void exit_sel_fs(void)
1736 unregister_filesystem(&sel_fs_type);