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", selinux_mls_enabled);
286 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
289 static const struct file_operations sel_mls_ops = {
290 .read = sel_read_mls,
293 static ssize_t sel_write_load(struct file *file, const char __user *buf,
294 size_t count, loff_t *ppos)
301 mutex_lock(&sel_mutex);
303 length = task_has_security(current, SECURITY__LOAD_POLICY);
308 /* No partial writes. */
313 if ((count > 64 * 1024 * 1024)
314 || (data = vmalloc(count)) == NULL) {
320 if (copy_from_user(data, buf, count) != 0)
323 length = security_load_policy(data, count);
327 ret = sel_make_bools();
333 ret = sel_make_classes();
339 ret = sel_make_policycap();
346 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
347 "policy loaded auid=%u ses=%u",
348 audit_get_loginuid(current),
349 audit_get_sessionid(current));
351 mutex_unlock(&sel_mutex);
356 static const struct file_operations sel_load_ops = {
357 .write = sel_write_load,
360 static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
366 length = task_has_security(current, SECURITY__CHECK_CONTEXT);
370 length = security_context_to_sid(buf, size, &sid);
374 length = security_sid_to_context(sid, &canon, &len);
378 if (len > SIMPLE_TRANSACTION_LIMIT) {
379 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
380 "payload max\n", __func__, len);
385 memcpy(buf, canon, len);
392 static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
393 size_t count, loff_t *ppos)
395 char tmpbuf[TMPBUFLEN];
398 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot);
399 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
402 static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
403 size_t count, loff_t *ppos)
407 unsigned int new_value;
409 length = task_has_security(current, SECURITY__SETCHECKREQPROT);
413 if (count >= PAGE_SIZE)
416 /* No partial writes. */
419 page = (char *)get_zeroed_page(GFP_KERNEL);
423 if (copy_from_user(page, buf, count))
427 if (sscanf(page, "%u", &new_value) != 1)
430 selinux_checkreqprot = new_value ? 1 : 0;
433 free_page((unsigned long) page);
436 static const struct file_operations sel_checkreqprot_ops = {
437 .read = sel_read_checkreqprot,
438 .write = sel_write_checkreqprot,
442 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
444 static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
445 static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
446 static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
447 static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
448 static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
450 static ssize_t (*write_op[])(struct file *, char *, size_t) = {
451 [SEL_ACCESS] = sel_write_access,
452 [SEL_CREATE] = sel_write_create,
453 [SEL_RELABEL] = sel_write_relabel,
454 [SEL_USER] = sel_write_user,
455 [SEL_MEMBER] = sel_write_member,
456 [SEL_CONTEXT] = sel_write_context,
459 static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
461 ino_t ino = file->f_path.dentry->d_inode->i_ino;
465 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
468 data = simple_transaction_get(file, buf, size);
470 return PTR_ERR(data);
472 rv = write_op[ino](file, data, size);
474 simple_transaction_set(file, rv);
480 static const struct file_operations transaction_ops = {
481 .write = selinux_transaction_write,
482 .read = simple_transaction_read,
483 .release = simple_transaction_release,
487 * payload - write methods
488 * If the method has a response, the response should be put in buf,
489 * and the length returned. Otherwise return 0 or and -error.
492 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 %x", scon, tcon, &tclass, &req) != 4)
518 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
521 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
525 length = security_compute_av(ssid, tsid, tclass, req, &avd);
529 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
531 avd.allowed, 0xffffffff,
532 avd.auditallow, avd.auditdeny,
541 static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
544 u32 ssid, tsid, newsid;
550 length = task_has_security(current, SECURITY__COMPUTE_CREATE);
555 scon = kzalloc(size+1, GFP_KERNEL);
559 tcon = kzalloc(size+1, GFP_KERNEL);
564 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
567 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
570 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
574 length = security_transition_sid(ssid, tsid, tclass, &newsid);
578 length = security_sid_to_context(newsid, &newcon, &len);
582 if (len > SIMPLE_TRANSACTION_LIMIT) {
583 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
584 "payload max\n", __func__, len);
589 memcpy(buf, newcon, len);
600 static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
603 u32 ssid, tsid, newsid;
609 length = task_has_security(current, SECURITY__COMPUTE_RELABEL);
614 scon = kzalloc(size+1, GFP_KERNEL);
618 tcon = kzalloc(size+1, GFP_KERNEL);
623 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
626 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
629 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
633 length = security_change_sid(ssid, tsid, tclass, &newsid);
637 length = security_sid_to_context(newsid, &newcon, &len);
641 if (len > SIMPLE_TRANSACTION_LIMIT) {
646 memcpy(buf, newcon, len);
657 static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
659 char *con, *user, *ptr;
666 length = task_has_security(current, SECURITY__COMPUTE_USER);
671 con = kzalloc(size+1, GFP_KERNEL);
675 user = kzalloc(size+1, GFP_KERNEL);
680 if (sscanf(buf, "%s %s", con, user) != 2)
683 length = security_context_to_sid(con, strlen(con)+1, &sid);
687 length = security_get_user_sids(sid, user, &sids, &nsids);
691 length = sprintf(buf, "%u", nsids) + 1;
693 for (i = 0; i < nsids; i++) {
694 rc = security_sid_to_context(sids[i], &newcon, &len);
699 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
704 memcpy(ptr, newcon, len);
718 static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
721 u32 ssid, tsid, newsid;
727 length = task_has_security(current, SECURITY__COMPUTE_MEMBER);
732 scon = kzalloc(size+1, GFP_KERNEL);
736 tcon = kzalloc(size+1, GFP_KERNEL);
741 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
744 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
747 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
751 length = security_member_sid(ssid, tsid, tclass, &newsid);
755 length = security_sid_to_context(newsid, &newcon, &len);
759 if (len > SIMPLE_TRANSACTION_LIMIT) {
760 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
761 "payload max\n", __func__, len);
766 memcpy(buf, newcon, len);
777 static struct inode *sel_make_inode(struct super_block *sb, int mode)
779 struct inode *ret = new_inode(sb);
783 ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
788 static ssize_t sel_read_bool(struct file *filep, char __user *buf,
789 size_t count, loff_t *ppos)
795 struct inode *inode = filep->f_path.dentry->d_inode;
796 unsigned index = inode->i_ino & SEL_INO_MASK;
797 const char *name = filep->f_path.dentry->d_name.name;
799 mutex_lock(&sel_mutex);
801 if (index >= bool_num || strcmp(name, bool_pending_names[index])) {
806 if (count > PAGE_SIZE) {
810 page = (char *)get_zeroed_page(GFP_KERNEL);
816 cur_enforcing = security_get_bool_value(index);
817 if (cur_enforcing < 0) {
821 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
822 bool_pending_values[index]);
823 ret = simple_read_from_buffer(buf, count, ppos, page, length);
825 mutex_unlock(&sel_mutex);
827 free_page((unsigned long)page);
831 static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
832 size_t count, loff_t *ppos)
837 struct inode *inode = filep->f_path.dentry->d_inode;
838 unsigned index = inode->i_ino & SEL_INO_MASK;
839 const char *name = filep->f_path.dentry->d_name.name;
841 mutex_lock(&sel_mutex);
843 length = task_has_security(current, SECURITY__SETBOOL);
847 if (index >= bool_num || strcmp(name, bool_pending_names[index])) {
852 if (count >= PAGE_SIZE) {
858 /* No partial writes. */
862 page = (char *)get_zeroed_page(GFP_KERNEL);
869 if (copy_from_user(page, buf, count))
873 if (sscanf(page, "%d", &new_value) != 1)
879 bool_pending_values[index] = new_value;
883 mutex_unlock(&sel_mutex);
885 free_page((unsigned long) page);
889 static const struct file_operations sel_bool_ops = {
890 .read = sel_read_bool,
891 .write = sel_write_bool,
894 static ssize_t sel_commit_bools_write(struct file *filep,
895 const char __user *buf,
896 size_t count, loff_t *ppos)
902 mutex_lock(&sel_mutex);
904 length = task_has_security(current, SECURITY__SETBOOL);
908 if (count >= PAGE_SIZE) {
913 /* No partial writes. */
916 page = (char *)get_zeroed_page(GFP_KERNEL);
923 if (copy_from_user(page, buf, count))
927 if (sscanf(page, "%d", &new_value) != 1)
930 if (new_value && bool_pending_values)
931 security_set_bools(bool_num, bool_pending_values);
936 mutex_unlock(&sel_mutex);
938 free_page((unsigned long) page);
942 static const struct file_operations sel_commit_bools_ops = {
943 .write = sel_commit_bools_write,
946 static void sel_remove_entries(struct dentry *de)
948 struct list_head *node;
950 spin_lock(&dcache_lock);
951 node = de->d_subdirs.next;
952 while (node != &de->d_subdirs) {
953 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
958 spin_unlock(&dcache_lock);
960 simple_unlink(de->d_inode, d);
962 spin_lock(&dcache_lock);
964 node = de->d_subdirs.next;
967 spin_unlock(&dcache_lock);
970 #define BOOL_DIR_NAME "booleans"
972 static int sel_make_bools(void)
976 struct dentry *dentry = NULL;
977 struct dentry *dir = bool_dir;
978 struct inode *inode = NULL;
979 struct inode_security_struct *isec;
980 char **names = NULL, *page;
985 /* remove any existing files */
986 kfree(bool_pending_names);
987 kfree(bool_pending_values);
988 bool_pending_names = NULL;
989 bool_pending_values = NULL;
991 sel_remove_entries(dir);
993 page = (char *)get_zeroed_page(GFP_KERNEL);
997 ret = security_get_bools(&num, &names, &values);
1001 for (i = 0; i < num; i++) {
1002 dentry = d_alloc_name(dir, names[i]);
1007 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1013 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
1017 } else if (len >= PAGE_SIZE) {
1018 ret = -ENAMETOOLONG;
1021 isec = (struct inode_security_struct *)inode->i_security;
1022 ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid);
1026 isec->initialized = 1;
1027 inode->i_fop = &sel_bool_ops;
1028 inode->i_ino = i|SEL_BOOL_INO_OFFSET;
1029 d_add(dentry, inode);
1032 bool_pending_names = names;
1033 bool_pending_values = values;
1035 free_page((unsigned long)page);
1039 for (i = 0; i < num; i++)
1044 sel_remove_entries(dir);
1049 #define NULL_FILE_NAME "null"
1051 struct dentry *selinux_null;
1053 static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1054 size_t count, loff_t *ppos)
1056 char tmpbuf[TMPBUFLEN];
1059 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
1060 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1063 static ssize_t sel_write_avc_cache_threshold(struct file *file,
1064 const char __user *buf,
1065 size_t count, loff_t *ppos)
1072 if (count >= PAGE_SIZE) {
1078 /* No partial writes. */
1083 page = (char *)get_zeroed_page(GFP_KERNEL);
1089 if (copy_from_user(page, buf, count)) {
1094 if (sscanf(page, "%u", &new_value) != 1) {
1099 if (new_value != avc_cache_threshold) {
1100 ret = task_has_security(current, SECURITY__SETSECPARAM);
1103 avc_cache_threshold = new_value;
1107 free_page((unsigned long)page);
1112 static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1113 size_t count, loff_t *ppos)
1118 page = (char *)__get_free_page(GFP_KERNEL);
1123 ret = avc_get_hash_stats(page);
1125 ret = simple_read_from_buffer(buf, count, ppos, page, ret);
1126 free_page((unsigned long)page);
1131 static const struct file_operations sel_avc_cache_threshold_ops = {
1132 .read = sel_read_avc_cache_threshold,
1133 .write = sel_write_avc_cache_threshold,
1136 static const struct file_operations sel_avc_hash_stats_ops = {
1137 .read = sel_read_avc_hash_stats,
1140 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1141 static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1145 for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
1146 if (!cpu_possible(cpu))
1149 return &per_cpu(avc_cache_stats, cpu);
1154 static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1156 loff_t n = *pos - 1;
1159 return SEQ_START_TOKEN;
1161 return sel_avc_get_stat_idx(&n);
1164 static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1166 return sel_avc_get_stat_idx(pos);
1169 static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1171 struct avc_cache_stats *st = v;
1173 if (v == SEQ_START_TOKEN)
1174 seq_printf(seq, "lookups hits misses allocations reclaims "
1177 seq_printf(seq, "%u %u %u %u %u %u\n", st->lookups,
1178 st->hits, st->misses, st->allocations,
1179 st->reclaims, st->frees);
1183 static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1186 static const struct seq_operations sel_avc_cache_stats_seq_ops = {
1187 .start = sel_avc_stats_seq_start,
1188 .next = sel_avc_stats_seq_next,
1189 .show = sel_avc_stats_seq_show,
1190 .stop = sel_avc_stats_seq_stop,
1193 static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1195 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1198 static const struct file_operations sel_avc_cache_stats_ops = {
1199 .open = sel_open_avc_cache_stats,
1201 .llseek = seq_lseek,
1202 .release = seq_release,
1206 static int sel_make_avc_files(struct dentry *dir)
1209 static struct tree_descr files[] = {
1210 { "cache_threshold",
1211 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1212 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1213 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1214 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1218 for (i = 0; i < ARRAY_SIZE(files); i++) {
1219 struct inode *inode;
1220 struct dentry *dentry;
1222 dentry = d_alloc_name(dir, files[i].name);
1228 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1233 inode->i_fop = files[i].ops;
1234 inode->i_ino = ++sel_last_ino;
1235 d_add(dentry, inode);
1241 static ssize_t sel_read_initcon(struct file *file, char __user *buf,
1242 size_t count, loff_t *ppos)
1244 struct inode *inode;
1249 inode = file->f_path.dentry->d_inode;
1250 sid = inode->i_ino&SEL_INO_MASK;
1251 ret = security_sid_to_context(sid, &con, &len);
1255 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1260 static const struct file_operations sel_initcon_ops = {
1261 .read = sel_read_initcon,
1264 static int sel_make_initcon_files(struct dentry *dir)
1268 for (i = 1; i <= SECINITSID_NUM; i++) {
1269 struct inode *inode;
1270 struct dentry *dentry;
1271 dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
1277 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1282 inode->i_fop = &sel_initcon_ops;
1283 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1284 d_add(dentry, inode);
1290 static inline unsigned int sel_div(unsigned long a, unsigned long b)
1292 return a / b - (a % b < 0);
1295 static inline unsigned long sel_class_to_ino(u16 class)
1297 return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1300 static inline u16 sel_ino_to_class(unsigned long ino)
1302 return sel_div(ino & SEL_INO_MASK, SEL_VEC_MAX + 1);
1305 static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1307 return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1310 static inline u32 sel_ino_to_perm(unsigned long ino)
1312 return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1315 static ssize_t sel_read_class(struct file *file, char __user *buf,
1316 size_t count, loff_t *ppos)
1320 unsigned long ino = file->f_path.dentry->d_inode->i_ino;
1322 page = (char *)__get_free_page(GFP_KERNEL);
1328 len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_class(ino));
1329 rc = simple_read_from_buffer(buf, count, ppos, page, len);
1330 free_page((unsigned long)page);
1335 static const struct file_operations sel_class_ops = {
1336 .read = sel_read_class,
1339 static ssize_t sel_read_perm(struct file *file, char __user *buf,
1340 size_t count, loff_t *ppos)
1344 unsigned long ino = file->f_path.dentry->d_inode->i_ino;
1346 page = (char *)__get_free_page(GFP_KERNEL);
1352 len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_perm(ino));
1353 rc = simple_read_from_buffer(buf, count, ppos, page, len);
1354 free_page((unsigned long)page);
1359 static const struct file_operations sel_perm_ops = {
1360 .read = sel_read_perm,
1363 static ssize_t sel_read_policycap(struct file *file, char __user *buf,
1364 size_t count, loff_t *ppos)
1367 char tmpbuf[TMPBUFLEN];
1369 unsigned long i_ino = file->f_path.dentry->d_inode->i_ino;
1371 value = security_policycap_supported(i_ino & SEL_INO_MASK);
1372 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
1374 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1377 static const struct file_operations sel_policycap_ops = {
1378 .read = sel_read_policycap,
1381 static int sel_make_perm_files(char *objclass, int classvalue,
1384 int i, rc = 0, nperms;
1387 rc = security_get_permissions(objclass, &perms, &nperms);
1391 for (i = 0; i < nperms; i++) {
1392 struct inode *inode;
1393 struct dentry *dentry;
1395 dentry = d_alloc_name(dir, perms[i]);
1401 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1406 inode->i_fop = &sel_perm_ops;
1407 /* i+1 since perm values are 1-indexed */
1408 inode->i_ino = sel_perm_to_ino(classvalue, i+1);
1409 d_add(dentry, inode);
1413 for (i = 0; i < nperms; i++)
1420 static int sel_make_class_dir_entries(char *classname, int index,
1423 struct dentry *dentry = NULL;
1424 struct inode *inode = NULL;
1427 dentry = d_alloc_name(dir, "index");
1433 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1439 inode->i_fop = &sel_class_ops;
1440 inode->i_ino = sel_class_to_ino(index);
1441 d_add(dentry, inode);
1443 dentry = d_alloc_name(dir, "perms");
1449 rc = sel_make_dir(dir->d_inode, dentry, &last_class_ino);
1453 rc = sel_make_perm_files(classname, index, dentry);
1459 static void sel_remove_classes(void)
1461 struct list_head *class_node;
1463 list_for_each(class_node, &class_dir->d_subdirs) {
1464 struct dentry *class_subdir = list_entry(class_node,
1465 struct dentry, d_u.d_child);
1466 struct list_head *class_subdir_node;
1468 list_for_each(class_subdir_node, &class_subdir->d_subdirs) {
1469 struct dentry *d = list_entry(class_subdir_node,
1470 struct dentry, d_u.d_child);
1473 if (d->d_inode->i_mode & S_IFDIR)
1474 sel_remove_entries(d);
1477 sel_remove_entries(class_subdir);
1480 sel_remove_entries(class_dir);
1483 static int sel_make_classes(void)
1485 int rc = 0, nclasses, i;
1488 /* delete any existing entries */
1489 sel_remove_classes();
1491 rc = security_get_classes(&classes, &nclasses);
1495 /* +2 since classes are 1-indexed */
1496 last_class_ino = sel_class_to_ino(nclasses+2);
1498 for (i = 0; i < nclasses; i++) {
1499 struct dentry *class_name_dir;
1501 class_name_dir = d_alloc_name(class_dir, classes[i]);
1502 if (!class_name_dir) {
1507 rc = sel_make_dir(class_dir->d_inode, class_name_dir,
1512 /* i+1 since class values are 1-indexed */
1513 rc = sel_make_class_dir_entries(classes[i], i+1,
1520 for (i = 0; i < nclasses; i++)
1527 static int sel_make_policycap(void)
1530 struct dentry *dentry = NULL;
1531 struct inode *inode = NULL;
1533 sel_remove_entries(policycap_dir);
1535 for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
1536 if (iter < ARRAY_SIZE(policycap_names))
1537 dentry = d_alloc_name(policycap_dir,
1538 policycap_names[iter]);
1540 dentry = d_alloc_name(policycap_dir, "unknown");
1545 inode = sel_make_inode(policycap_dir->d_sb, S_IFREG | S_IRUGO);
1549 inode->i_fop = &sel_policycap_ops;
1550 inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
1551 d_add(dentry, inode);
1557 static int sel_make_dir(struct inode *dir, struct dentry *dentry,
1561 struct inode *inode;
1563 inode = sel_make_inode(dir->i_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1568 inode->i_op = &simple_dir_inode_operations;
1569 inode->i_fop = &simple_dir_operations;
1570 inode->i_ino = ++(*ino);
1571 /* directory inodes start off with i_nlink == 2 (for "." entry) */
1573 d_add(dentry, inode);
1574 /* bump link count on parent directory, too */
1580 static int sel_fill_super(struct super_block *sb, void *data, int silent)
1583 struct dentry *dentry;
1584 struct inode *inode, *root_inode;
1585 struct inode_security_struct *isec;
1587 static struct tree_descr selinux_files[] = {
1588 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1589 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
1590 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
1591 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1592 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1593 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1594 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1595 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1596 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1597 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1598 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1599 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1600 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
1601 [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
1602 [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
1605 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1609 root_inode = sb->s_root->d_inode;
1611 dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME);
1617 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1623 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
1629 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
1634 inode->i_ino = ++sel_last_ino;
1635 isec = (struct inode_security_struct *)inode->i_security;
1636 isec->sid = SECINITSID_DEVNULL;
1637 isec->sclass = SECCLASS_CHR_FILE;
1638 isec->initialized = 1;
1640 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1641 d_add(dentry, inode);
1642 selinux_null = dentry;
1644 dentry = d_alloc_name(sb->s_root, "avc");
1650 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1654 ret = sel_make_avc_files(dentry);
1658 dentry = d_alloc_name(sb->s_root, "initial_contexts");
1664 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1668 ret = sel_make_initcon_files(dentry);
1672 dentry = d_alloc_name(sb->s_root, "class");
1678 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1684 dentry = d_alloc_name(sb->s_root, "policy_capabilities");
1690 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1694 policycap_dir = dentry;
1699 printk(KERN_ERR "SELinux: %s: failed while creating inodes\n",
1704 static int sel_get_sb(struct file_system_type *fs_type,
1705 int flags, const char *dev_name, void *data,
1706 struct vfsmount *mnt)
1708 return get_sb_single(fs_type, flags, data, sel_fill_super, mnt);
1711 static struct file_system_type sel_fs_type = {
1712 .name = "selinuxfs",
1713 .get_sb = sel_get_sb,
1714 .kill_sb = kill_litter_super,
1717 struct vfsmount *selinuxfs_mount;
1719 static int __init init_sel_fs(void)
1723 if (!selinux_enabled)
1725 err = register_filesystem(&sel_fs_type);
1727 selinuxfs_mount = kern_mount(&sel_fs_type);
1728 if (IS_ERR(selinuxfs_mount)) {
1729 printk(KERN_ERR "selinuxfs: could not mount!\n");
1730 err = PTR_ERR(selinuxfs_mount);
1731 selinuxfs_mount = NULL;
1737 __initcall(init_sel_fs);
1739 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
1740 void exit_sel_fs(void)
1742 unregister_filesystem(&sel_fs_type);