1 /* Updated: Karl MacMillan <kmacmillan@tresys.com>
3 * Added conditional policy language extensions
5 * Updated: Hewlett-Packard <paul.moore@hp.com>
7 * Added support for the policy capability bitmap
9 * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
10 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
11 * Copyright (C) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, version 2.
17 #include <linux/kernel.h>
18 #include <linux/pagemap.h>
19 #include <linux/slab.h>
20 #include <linux/vmalloc.h>
22 #include <linux/mutex.h>
23 #include <linux/init.h>
24 #include <linux/string.h>
25 #include <linux/security.h>
26 #include <linux/major.h>
27 #include <linux/seq_file.h>
28 #include <linux/percpu.h>
29 #include <linux/audit.h>
30 #include <asm/uaccess.h>
31 #include <asm/semaphore.h>
33 /* selinuxfs pseudo filesystem for exporting the security policy API.
34 Based on the proc code and the fs/nfsd/nfsctl.c code. */
41 #include "conditional.h"
43 /* Policy capability filenames */
44 static char *policycap_names[] = {
45 "network_peer_controls"
48 unsigned int selinux_checkreqprot = CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
50 #ifdef CONFIG_SECURITY_SELINUX_ENABLE_SECMARK_DEFAULT
51 #define SELINUX_COMPAT_NET_VALUE 0
53 #define SELINUX_COMPAT_NET_VALUE 1
56 int selinux_compat_net = SELINUX_COMPAT_NET_VALUE;
58 static int __init checkreqprot_setup(char *str)
60 selinux_checkreqprot = simple_strtoul(str,NULL,0) ? 1 : 0;
63 __setup("checkreqprot=", checkreqprot_setup);
65 static int __init selinux_compat_net_setup(char *str)
67 selinux_compat_net = simple_strtoul(str,NULL,0) ? 1 : 0;
70 __setup("selinux_compat_net=", selinux_compat_net_setup);
73 static DEFINE_MUTEX(sel_mutex);
75 /* global data for booleans */
76 static struct dentry *bool_dir = NULL;
77 static int bool_num = 0;
78 static char **bool_pending_names;
79 static int *bool_pending_values = NULL;
81 /* global data for classes */
82 static struct dentry *class_dir = NULL;
83 static unsigned long last_class_ino;
85 /* global data for policy capabilities */
86 static struct dentry *policycap_dir = NULL;
88 extern void selnl_notify_setenforce(int val);
90 /* Check whether a task is allowed to use a security operation. */
91 static int task_has_security(struct task_struct *tsk,
94 struct task_security_struct *tsec;
100 return avc_has_perm(tsec->sid, SECINITSID_SECURITY,
101 SECCLASS_SECURITY, perms, NULL);
106 SEL_LOAD, /* load policy */
107 SEL_ENFORCE, /* get or set enforcing status */
108 SEL_CONTEXT, /* validate context */
109 SEL_ACCESS, /* compute access decision */
110 SEL_CREATE, /* compute create labeling decision */
111 SEL_RELABEL, /* compute relabeling decision */
112 SEL_USER, /* compute reachable user contexts */
113 SEL_POLICYVERS, /* return policy version for this kernel */
114 SEL_COMMIT_BOOLS, /* commit new boolean values */
115 SEL_MLS, /* return if MLS policy is enabled */
116 SEL_DISABLE, /* disable SELinux until next reboot */
117 SEL_MEMBER, /* compute polyinstantiation membership decision */
118 SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
119 SEL_COMPAT_NET, /* whether to use old compat network packet controls */
120 SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */
121 SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */
122 SEL_INO_NEXT, /* The next inode number to use */
125 static unsigned long sel_last_ino = SEL_INO_NEXT - 1;
127 #define SEL_INITCON_INO_OFFSET 0x01000000
128 #define SEL_BOOL_INO_OFFSET 0x02000000
129 #define SEL_CLASS_INO_OFFSET 0x04000000
130 #define SEL_POLICYCAP_INO_OFFSET 0x08000000
131 #define SEL_INO_MASK 0x00ffffff
134 static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
135 size_t count, loff_t *ppos)
137 char tmpbuf[TMPBUFLEN];
140 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing);
141 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
144 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
145 static ssize_t sel_write_enforce(struct file * file, const char __user * buf,
146 size_t count, loff_t *ppos)
153 if (count >= PAGE_SIZE)
156 /* No partial writes. */
159 page = (char*)get_zeroed_page(GFP_KERNEL);
163 if (copy_from_user(page, buf, count))
167 if (sscanf(page, "%d", &new_value) != 1)
170 if (new_value != selinux_enforcing) {
171 length = task_has_security(current, SECURITY__SETENFORCE);
174 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
175 "enforcing=%d old_enforcing=%d auid=%u", new_value,
177 audit_get_loginuid(current->audit_context));
178 selinux_enforcing = new_value;
179 if (selinux_enforcing)
181 selnl_notify_setenforce(selinux_enforcing);
185 free_page((unsigned long) page);
189 #define sel_write_enforce NULL
192 static const struct file_operations sel_enforce_ops = {
193 .read = sel_read_enforce,
194 .write = sel_write_enforce,
197 static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf,
198 size_t count, loff_t *ppos)
200 char tmpbuf[TMPBUFLEN];
202 ino_t ino = filp->f_path.dentry->d_inode->i_ino;
203 int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
204 security_get_reject_unknown() : !security_get_allow_unknown();
206 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
207 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
210 static const struct file_operations sel_handle_unknown_ops = {
211 .read = sel_read_handle_unknown,
214 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
215 static ssize_t sel_write_disable(struct file * file, const char __user * buf,
216 size_t count, loff_t *ppos)
222 extern int selinux_disable(void);
224 if (count >= PAGE_SIZE)
227 /* No partial writes. */
230 page = (char*)get_zeroed_page(GFP_KERNEL);
234 if (copy_from_user(page, buf, count))
238 if (sscanf(page, "%d", &new_value) != 1)
242 length = selinux_disable();
245 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
247 audit_get_loginuid(current->audit_context));
252 free_page((unsigned long) page);
256 #define sel_write_disable NULL
259 static const struct file_operations sel_disable_ops = {
260 .write = sel_write_disable,
263 static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
264 size_t count, loff_t *ppos)
266 char tmpbuf[TMPBUFLEN];
269 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
270 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
273 static const struct file_operations sel_policyvers_ops = {
274 .read = sel_read_policyvers,
277 /* declaration for sel_write_load */
278 static int sel_make_bools(void);
279 static int sel_make_classes(void);
280 static int sel_make_policycap(void);
282 /* declaration for sel_make_class_dirs */
283 static int sel_make_dir(struct inode *dir, struct dentry *dentry,
286 static ssize_t sel_read_mls(struct file *filp, char __user *buf,
287 size_t count, loff_t *ppos)
289 char tmpbuf[TMPBUFLEN];
292 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_mls_enabled);
293 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
296 static const struct file_operations sel_mls_ops = {
297 .read = sel_read_mls,
300 static ssize_t sel_write_load(struct file * file, const char __user * buf,
301 size_t count, loff_t *ppos)
308 mutex_lock(&sel_mutex);
310 length = task_has_security(current, SECURITY__LOAD_POLICY);
315 /* No partial writes. */
320 if ((count > 64 * 1024 * 1024)
321 || (data = vmalloc(count)) == NULL) {
327 if (copy_from_user(data, buf, count) != 0)
330 length = security_load_policy(data, count);
334 ret = sel_make_bools();
340 ret = sel_make_classes();
346 ret = sel_make_policycap();
354 printk(KERN_INFO "SELinux: policy loaded with handle_unknown=%s\n",
355 (security_get_reject_unknown() ? "reject" :
356 (security_get_allow_unknown() ? "allow" : "deny")));
358 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
359 "policy loaded auid=%u",
360 audit_get_loginuid(current->audit_context));
362 mutex_unlock(&sel_mutex);
367 static const struct file_operations sel_load_ops = {
368 .write = sel_write_load,
371 static ssize_t sel_write_context(struct file * file, char *buf, size_t size)
377 length = task_has_security(current, SECURITY__CHECK_CONTEXT);
381 length = security_context_to_sid(buf, size, &sid);
385 length = security_sid_to_context(sid, &canon, &len);
389 if (len > SIMPLE_TRANSACTION_LIMIT) {
390 printk(KERN_ERR "%s: context size (%u) exceeds payload "
391 "max\n", __FUNCTION__, len);
396 memcpy(buf, canon, len);
403 static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
404 size_t count, loff_t *ppos)
406 char tmpbuf[TMPBUFLEN];
409 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot);
410 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
413 static ssize_t sel_write_checkreqprot(struct file * file, const char __user * buf,
414 size_t count, loff_t *ppos)
418 unsigned int new_value;
420 length = task_has_security(current, SECURITY__SETCHECKREQPROT);
424 if (count >= PAGE_SIZE)
427 /* No partial writes. */
430 page = (char*)get_zeroed_page(GFP_KERNEL);
434 if (copy_from_user(page, buf, count))
438 if (sscanf(page, "%u", &new_value) != 1)
441 selinux_checkreqprot = new_value ? 1 : 0;
444 free_page((unsigned long) page);
447 static const struct file_operations sel_checkreqprot_ops = {
448 .read = sel_read_checkreqprot,
449 .write = sel_write_checkreqprot,
452 static ssize_t sel_read_compat_net(struct file *filp, char __user *buf,
453 size_t count, loff_t *ppos)
455 char tmpbuf[TMPBUFLEN];
458 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_compat_net);
459 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
462 static ssize_t sel_write_compat_net(struct file * file, const char __user * buf,
463 size_t count, loff_t *ppos)
469 length = task_has_security(current, SECURITY__LOAD_POLICY);
473 if (count >= PAGE_SIZE)
476 /* No partial writes. */
479 page = (char*)get_zeroed_page(GFP_KERNEL);
483 if (copy_from_user(page, buf, count))
487 if (sscanf(page, "%d", &new_value) != 1)
490 selinux_compat_net = new_value ? 1 : 0;
493 free_page((unsigned long) page);
496 static const struct file_operations sel_compat_net_ops = {
497 .read = sel_read_compat_net,
498 .write = sel_write_compat_net,
502 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
504 static ssize_t sel_write_access(struct file * file, char *buf, size_t size);
505 static ssize_t sel_write_create(struct file * file, char *buf, size_t size);
506 static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size);
507 static ssize_t sel_write_user(struct file * file, char *buf, size_t size);
508 static ssize_t sel_write_member(struct file * file, char *buf, size_t size);
510 static ssize_t (*write_op[])(struct file *, char *, size_t) = {
511 [SEL_ACCESS] = sel_write_access,
512 [SEL_CREATE] = sel_write_create,
513 [SEL_RELABEL] = sel_write_relabel,
514 [SEL_USER] = sel_write_user,
515 [SEL_MEMBER] = sel_write_member,
516 [SEL_CONTEXT] = sel_write_context,
519 static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
521 ino_t ino = file->f_path.dentry->d_inode->i_ino;
525 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
528 data = simple_transaction_get(file, buf, size);
530 return PTR_ERR(data);
532 rv = write_op[ino](file, data, size);
534 simple_transaction_set(file, rv);
540 static const struct file_operations transaction_ops = {
541 .write = selinux_transaction_write,
542 .read = simple_transaction_read,
543 .release = simple_transaction_release,
547 * payload - write methods
548 * If the method has a response, the response should be put in buf,
549 * and the length returned. Otherwise return 0 or and -error.
552 static ssize_t sel_write_access(struct file * file, char *buf, size_t size)
558 struct av_decision avd;
561 length = task_has_security(current, SECURITY__COMPUTE_AV);
566 scon = kzalloc(size+1, GFP_KERNEL);
570 tcon = kzalloc(size+1, GFP_KERNEL);
575 if (sscanf(buf, "%s %s %hu %x", scon, tcon, &tclass, &req) != 4)
578 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
581 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
585 length = security_compute_av(ssid, tsid, tclass, req, &avd);
589 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
591 avd.allowed, avd.decided,
592 avd.auditallow, avd.auditdeny,
601 static ssize_t sel_write_create(struct file * file, char *buf, size_t size)
604 u32 ssid, tsid, newsid;
610 length = task_has_security(current, SECURITY__COMPUTE_CREATE);
615 scon = kzalloc(size+1, GFP_KERNEL);
619 tcon = kzalloc(size+1, GFP_KERNEL);
624 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
627 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
630 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
634 length = security_transition_sid(ssid, tsid, tclass, &newsid);
638 length = security_sid_to_context(newsid, &newcon, &len);
642 if (len > SIMPLE_TRANSACTION_LIMIT) {
643 printk(KERN_ERR "%s: context size (%u) exceeds payload "
644 "max\n", __FUNCTION__, len);
649 memcpy(buf, newcon, len);
660 static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size)
663 u32 ssid, tsid, newsid;
669 length = task_has_security(current, SECURITY__COMPUTE_RELABEL);
674 scon = kzalloc(size+1, GFP_KERNEL);
678 tcon = kzalloc(size+1, GFP_KERNEL);
683 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
686 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
689 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
693 length = security_change_sid(ssid, tsid, tclass, &newsid);
697 length = security_sid_to_context(newsid, &newcon, &len);
701 if (len > SIMPLE_TRANSACTION_LIMIT) {
706 memcpy(buf, newcon, len);
717 static ssize_t sel_write_user(struct file * file, char *buf, size_t size)
719 char *con, *user, *ptr;
726 length = task_has_security(current, SECURITY__COMPUTE_USER);
731 con = kzalloc(size+1, GFP_KERNEL);
735 user = kzalloc(size+1, GFP_KERNEL);
740 if (sscanf(buf, "%s %s", con, user) != 2)
743 length = security_context_to_sid(con, strlen(con)+1, &sid);
747 length = security_get_user_sids(sid, user, &sids, &nsids);
751 length = sprintf(buf, "%u", nsids) + 1;
753 for (i = 0; i < nsids; i++) {
754 rc = security_sid_to_context(sids[i], &newcon, &len);
759 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
764 memcpy(ptr, newcon, len);
778 static ssize_t sel_write_member(struct file * file, char *buf, size_t size)
781 u32 ssid, tsid, newsid;
787 length = task_has_security(current, SECURITY__COMPUTE_MEMBER);
792 scon = kzalloc(size+1, GFP_KERNEL);
796 tcon = kzalloc(size+1, GFP_KERNEL);
801 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
804 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
807 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
811 length = security_member_sid(ssid, tsid, tclass, &newsid);
815 length = security_sid_to_context(newsid, &newcon, &len);
819 if (len > SIMPLE_TRANSACTION_LIMIT) {
820 printk(KERN_ERR "%s: context size (%u) exceeds payload "
821 "max\n", __FUNCTION__, len);
826 memcpy(buf, newcon, len);
837 static struct inode *sel_make_inode(struct super_block *sb, int mode)
839 struct inode *ret = new_inode(sb);
843 ret->i_uid = ret->i_gid = 0;
845 ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
850 static ssize_t sel_read_bool(struct file *filep, char __user *buf,
851 size_t count, loff_t *ppos)
857 struct inode *inode = filep->f_path.dentry->d_inode;
858 unsigned index = inode->i_ino & SEL_INO_MASK;
859 const char *name = filep->f_path.dentry->d_name.name;
861 mutex_lock(&sel_mutex);
863 if (index >= bool_num || strcmp(name, bool_pending_names[index])) {
868 if (count > PAGE_SIZE) {
872 if (!(page = (char*)get_zeroed_page(GFP_KERNEL))) {
877 cur_enforcing = security_get_bool_value(index);
878 if (cur_enforcing < 0) {
882 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
883 bool_pending_values[index]);
884 ret = simple_read_from_buffer(buf, count, ppos, page, length);
886 mutex_unlock(&sel_mutex);
888 free_page((unsigned long)page);
892 static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
893 size_t count, loff_t *ppos)
898 struct inode *inode = filep->f_path.dentry->d_inode;
899 unsigned index = inode->i_ino & SEL_INO_MASK;
900 const char *name = filep->f_path.dentry->d_name.name;
902 mutex_lock(&sel_mutex);
904 length = task_has_security(current, SECURITY__SETBOOL);
908 if (index >= bool_num || strcmp(name, bool_pending_names[index])) {
913 if (count >= PAGE_SIZE) {
919 /* No partial writes. */
923 page = (char*)get_zeroed_page(GFP_KERNEL);
930 if (copy_from_user(page, buf, count))
934 if (sscanf(page, "%d", &new_value) != 1)
940 bool_pending_values[index] = new_value;
944 mutex_unlock(&sel_mutex);
946 free_page((unsigned long) page);
950 static const struct file_operations sel_bool_ops = {
951 .read = sel_read_bool,
952 .write = sel_write_bool,
955 static ssize_t sel_commit_bools_write(struct file *filep,
956 const char __user *buf,
957 size_t count, loff_t *ppos)
963 mutex_lock(&sel_mutex);
965 length = task_has_security(current, SECURITY__SETBOOL);
969 if (count >= PAGE_SIZE) {
974 /* No partial writes. */
977 page = (char*)get_zeroed_page(GFP_KERNEL);
984 if (copy_from_user(page, buf, count))
988 if (sscanf(page, "%d", &new_value) != 1)
991 if (new_value && bool_pending_values) {
992 security_set_bools(bool_num, bool_pending_values);
998 mutex_unlock(&sel_mutex);
1000 free_page((unsigned long) page);
1004 static const struct file_operations sel_commit_bools_ops = {
1005 .write = sel_commit_bools_write,
1008 static void sel_remove_entries(struct dentry *de)
1010 struct list_head *node;
1012 spin_lock(&dcache_lock);
1013 node = de->d_subdirs.next;
1014 while (node != &de->d_subdirs) {
1015 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
1016 list_del_init(node);
1020 spin_unlock(&dcache_lock);
1022 simple_unlink(de->d_inode, d);
1024 spin_lock(&dcache_lock);
1026 node = de->d_subdirs.next;
1029 spin_unlock(&dcache_lock);
1032 #define BOOL_DIR_NAME "booleans"
1034 static int sel_make_bools(void)
1038 struct dentry *dentry = NULL;
1039 struct dentry *dir = bool_dir;
1040 struct inode *inode = NULL;
1041 struct inode_security_struct *isec;
1042 char **names = NULL, *page;
1047 /* remove any existing files */
1048 kfree(bool_pending_names);
1049 kfree(bool_pending_values);
1050 bool_pending_names = NULL;
1051 bool_pending_values = NULL;
1053 sel_remove_entries(dir);
1055 if (!(page = (char*)get_zeroed_page(GFP_KERNEL)))
1058 ret = security_get_bools(&num, &names, &values);
1062 for (i = 0; i < num; i++) {
1063 dentry = d_alloc_name(dir, names[i]);
1068 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1074 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
1078 } else if (len >= PAGE_SIZE) {
1079 ret = -ENAMETOOLONG;
1082 isec = (struct inode_security_struct*)inode->i_security;
1083 if ((ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid)))
1086 isec->initialized = 1;
1087 inode->i_fop = &sel_bool_ops;
1088 inode->i_ino = i|SEL_BOOL_INO_OFFSET;
1089 d_add(dentry, inode);
1092 bool_pending_names = names;
1093 bool_pending_values = values;
1095 free_page((unsigned long)page);
1099 for (i = 0; i < num; i++)
1104 sel_remove_entries(dir);
1109 #define NULL_FILE_NAME "null"
1111 struct dentry *selinux_null = NULL;
1113 static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1114 size_t count, loff_t *ppos)
1116 char tmpbuf[TMPBUFLEN];
1119 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
1120 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1123 static ssize_t sel_write_avc_cache_threshold(struct file * file,
1124 const char __user * buf,
1125 size_t count, loff_t *ppos)
1132 if (count >= PAGE_SIZE) {
1138 /* No partial writes. */
1143 page = (char*)get_zeroed_page(GFP_KERNEL);
1149 if (copy_from_user(page, buf, count)) {
1154 if (sscanf(page, "%u", &new_value) != 1) {
1159 if (new_value != avc_cache_threshold) {
1160 ret = task_has_security(current, SECURITY__SETSECPARAM);
1163 avc_cache_threshold = new_value;
1167 free_page((unsigned long)page);
1172 static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1173 size_t count, loff_t *ppos)
1178 page = (char *)__get_free_page(GFP_KERNEL);
1183 ret = avc_get_hash_stats(page);
1185 ret = simple_read_from_buffer(buf, count, ppos, page, ret);
1186 free_page((unsigned long)page);
1191 static const struct file_operations sel_avc_cache_threshold_ops = {
1192 .read = sel_read_avc_cache_threshold,
1193 .write = sel_write_avc_cache_threshold,
1196 static const struct file_operations sel_avc_hash_stats_ops = {
1197 .read = sel_read_avc_hash_stats,
1200 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1201 static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1205 for (cpu = *idx; cpu < NR_CPUS; ++cpu) {
1206 if (!cpu_possible(cpu))
1209 return &per_cpu(avc_cache_stats, cpu);
1214 static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1216 loff_t n = *pos - 1;
1219 return SEQ_START_TOKEN;
1221 return sel_avc_get_stat_idx(&n);
1224 static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1226 return sel_avc_get_stat_idx(pos);
1229 static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1231 struct avc_cache_stats *st = v;
1233 if (v == SEQ_START_TOKEN)
1234 seq_printf(seq, "lookups hits misses allocations reclaims "
1237 seq_printf(seq, "%u %u %u %u %u %u\n", st->lookups,
1238 st->hits, st->misses, st->allocations,
1239 st->reclaims, st->frees);
1243 static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1246 static const struct seq_operations sel_avc_cache_stats_seq_ops = {
1247 .start = sel_avc_stats_seq_start,
1248 .next = sel_avc_stats_seq_next,
1249 .show = sel_avc_stats_seq_show,
1250 .stop = sel_avc_stats_seq_stop,
1253 static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1255 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1258 static const struct file_operations sel_avc_cache_stats_ops = {
1259 .open = sel_open_avc_cache_stats,
1261 .llseek = seq_lseek,
1262 .release = seq_release,
1266 static int sel_make_avc_files(struct dentry *dir)
1269 static struct tree_descr files[] = {
1270 { "cache_threshold",
1271 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1272 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1273 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1274 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1278 for (i = 0; i < ARRAY_SIZE(files); i++) {
1279 struct inode *inode;
1280 struct dentry *dentry;
1282 dentry = d_alloc_name(dir, files[i].name);
1288 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1293 inode->i_fop = files[i].ops;
1294 inode->i_ino = ++sel_last_ino;
1295 d_add(dentry, inode);
1301 static ssize_t sel_read_initcon(struct file * file, char __user *buf,
1302 size_t count, loff_t *ppos)
1304 struct inode *inode;
1309 inode = file->f_path.dentry->d_inode;
1310 sid = inode->i_ino&SEL_INO_MASK;
1311 ret = security_sid_to_context(sid, &con, &len);
1315 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1320 static const struct file_operations sel_initcon_ops = {
1321 .read = sel_read_initcon,
1324 static int sel_make_initcon_files(struct dentry *dir)
1328 for (i = 1; i <= SECINITSID_NUM; i++) {
1329 struct inode *inode;
1330 struct dentry *dentry;
1331 dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
1337 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1342 inode->i_fop = &sel_initcon_ops;
1343 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1344 d_add(dentry, inode);
1350 static inline unsigned int sel_div(unsigned long a, unsigned long b)
1352 return a / b - (a % b < 0);
1355 static inline unsigned long sel_class_to_ino(u16 class)
1357 return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1360 static inline u16 sel_ino_to_class(unsigned long ino)
1362 return sel_div(ino & SEL_INO_MASK, SEL_VEC_MAX + 1);
1365 static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1367 return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1370 static inline u32 sel_ino_to_perm(unsigned long ino)
1372 return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1375 static ssize_t sel_read_class(struct file * file, char __user *buf,
1376 size_t count, loff_t *ppos)
1380 unsigned long ino = file->f_path.dentry->d_inode->i_ino;
1382 page = (char *)__get_free_page(GFP_KERNEL);
1388 len = snprintf(page, PAGE_SIZE, "%d", sel_ino_to_class(ino));
1389 rc = simple_read_from_buffer(buf, count, ppos, page, len);
1390 free_page((unsigned long)page);
1395 static const struct file_operations sel_class_ops = {
1396 .read = sel_read_class,
1399 static ssize_t sel_read_perm(struct file * file, char __user *buf,
1400 size_t count, loff_t *ppos)
1404 unsigned long ino = file->f_path.dentry->d_inode->i_ino;
1406 page = (char *)__get_free_page(GFP_KERNEL);
1412 len = snprintf(page, PAGE_SIZE,"%d", sel_ino_to_perm(ino));
1413 rc = simple_read_from_buffer(buf, count, ppos, page, len);
1414 free_page((unsigned long)page);
1419 static const struct file_operations sel_perm_ops = {
1420 .read = sel_read_perm,
1423 static ssize_t sel_read_policycap(struct file *file, char __user *buf,
1424 size_t count, loff_t *ppos)
1427 char tmpbuf[TMPBUFLEN];
1429 unsigned long i_ino = file->f_path.dentry->d_inode->i_ino;
1431 value = security_policycap_supported(i_ino & SEL_INO_MASK);
1432 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
1434 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1437 static const struct file_operations sel_policycap_ops = {
1438 .read = sel_read_policycap,
1441 static int sel_make_perm_files(char *objclass, int classvalue,
1444 int i, rc = 0, nperms;
1447 rc = security_get_permissions(objclass, &perms, &nperms);
1451 for (i = 0; i < nperms; i++) {
1452 struct inode *inode;
1453 struct dentry *dentry;
1455 dentry = d_alloc_name(dir, perms[i]);
1461 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1466 inode->i_fop = &sel_perm_ops;
1467 /* i+1 since perm values are 1-indexed */
1468 inode->i_ino = sel_perm_to_ino(classvalue, i+1);
1469 d_add(dentry, inode);
1473 for (i = 0; i < nperms; i++)
1480 static int sel_make_class_dir_entries(char *classname, int index,
1483 struct dentry *dentry = NULL;
1484 struct inode *inode = NULL;
1487 dentry = d_alloc_name(dir, "index");
1493 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1499 inode->i_fop = &sel_class_ops;
1500 inode->i_ino = sel_class_to_ino(index);
1501 d_add(dentry, inode);
1503 dentry = d_alloc_name(dir, "perms");
1509 rc = sel_make_dir(dir->d_inode, dentry, &last_class_ino);
1513 rc = sel_make_perm_files(classname, index, dentry);
1519 static void sel_remove_classes(void)
1521 struct list_head *class_node;
1523 list_for_each(class_node, &class_dir->d_subdirs) {
1524 struct dentry *class_subdir = list_entry(class_node,
1525 struct dentry, d_u.d_child);
1526 struct list_head *class_subdir_node;
1528 list_for_each(class_subdir_node, &class_subdir->d_subdirs) {
1529 struct dentry *d = list_entry(class_subdir_node,
1530 struct dentry, d_u.d_child);
1533 if (d->d_inode->i_mode & S_IFDIR)
1534 sel_remove_entries(d);
1537 sel_remove_entries(class_subdir);
1540 sel_remove_entries(class_dir);
1543 static int sel_make_classes(void)
1545 int rc = 0, nclasses, i;
1548 /* delete any existing entries */
1549 sel_remove_classes();
1551 rc = security_get_classes(&classes, &nclasses);
1555 /* +2 since classes are 1-indexed */
1556 last_class_ino = sel_class_to_ino(nclasses+2);
1558 for (i = 0; i < nclasses; i++) {
1559 struct dentry *class_name_dir;
1561 class_name_dir = d_alloc_name(class_dir, classes[i]);
1562 if (!class_name_dir) {
1567 rc = sel_make_dir(class_dir->d_inode, class_name_dir,
1572 /* i+1 since class values are 1-indexed */
1573 rc = sel_make_class_dir_entries(classes[i], i+1,
1580 for (i = 0; i < nclasses; i++)
1587 static int sel_make_policycap(void)
1590 struct dentry *dentry = NULL;
1591 struct inode *inode = NULL;
1593 sel_remove_entries(policycap_dir);
1595 for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
1596 if (iter < ARRAY_SIZE(policycap_names))
1597 dentry = d_alloc_name(policycap_dir,
1598 policycap_names[iter]);
1600 dentry = d_alloc_name(policycap_dir, "unknown");
1605 inode = sel_make_inode(policycap_dir->d_sb, S_IFREG | S_IRUGO);
1609 inode->i_fop = &sel_policycap_ops;
1610 inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
1611 d_add(dentry, inode);
1617 static int sel_make_dir(struct inode *dir, struct dentry *dentry,
1621 struct inode *inode;
1623 inode = sel_make_inode(dir->i_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1628 inode->i_op = &simple_dir_inode_operations;
1629 inode->i_fop = &simple_dir_operations;
1630 inode->i_ino = ++(*ino);
1631 /* directory inodes start off with i_nlink == 2 (for "." entry) */
1633 d_add(dentry, inode);
1634 /* bump link count on parent directory, too */
1640 static int sel_fill_super(struct super_block * sb, void * data, int silent)
1643 struct dentry *dentry;
1644 struct inode *inode, *root_inode;
1645 struct inode_security_struct *isec;
1647 static struct tree_descr selinux_files[] = {
1648 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1649 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
1650 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
1651 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1652 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1653 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1654 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1655 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1656 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1657 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1658 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1659 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1660 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
1661 [SEL_COMPAT_NET] = {"compat_net", &sel_compat_net_ops, S_IRUGO|S_IWUSR},
1662 [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
1663 [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
1666 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1670 root_inode = sb->s_root->d_inode;
1672 dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME);
1678 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1684 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
1690 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
1695 inode->i_ino = ++sel_last_ino;
1696 isec = (struct inode_security_struct*)inode->i_security;
1697 isec->sid = SECINITSID_DEVNULL;
1698 isec->sclass = SECCLASS_CHR_FILE;
1699 isec->initialized = 1;
1701 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1702 d_add(dentry, inode);
1703 selinux_null = dentry;
1705 dentry = d_alloc_name(sb->s_root, "avc");
1711 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1715 ret = sel_make_avc_files(dentry);
1719 dentry = d_alloc_name(sb->s_root, "initial_contexts");
1725 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1729 ret = sel_make_initcon_files(dentry);
1733 dentry = d_alloc_name(sb->s_root, "class");
1739 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1745 dentry = d_alloc_name(sb->s_root, "policy_capabilities");
1751 ret = sel_make_dir(root_inode, dentry, &sel_last_ino);
1755 policycap_dir = dentry;
1760 printk(KERN_ERR "%s: failed while creating inodes\n", __FUNCTION__);
1764 static int sel_get_sb(struct file_system_type *fs_type,
1765 int flags, const char *dev_name, void *data,
1766 struct vfsmount *mnt)
1768 return get_sb_single(fs_type, flags, data, sel_fill_super, mnt);
1771 static struct file_system_type sel_fs_type = {
1772 .name = "selinuxfs",
1773 .get_sb = sel_get_sb,
1774 .kill_sb = kill_litter_super,
1777 struct vfsmount *selinuxfs_mount;
1779 static int __init init_sel_fs(void)
1783 if (!selinux_enabled)
1785 err = register_filesystem(&sel_fs_type);
1787 selinuxfs_mount = kern_mount(&sel_fs_type);
1788 if (IS_ERR(selinuxfs_mount)) {
1789 printk(KERN_ERR "selinuxfs: could not mount!\n");
1790 err = PTR_ERR(selinuxfs_mount);
1791 selinuxfs_mount = NULL;
1797 __initcall(init_sel_fs);
1799 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
1800 void exit_sel_fs(void)
1802 unregister_filesystem(&sel_fs_type);