3 * Added conditional policy language extensions
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
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>
31 #include <linux/kobject.h>
32 #include <linux/ctype.h>
34 /* selinuxfs pseudo filesystem for exporting the security policy API.
35 Based on the proc code and the fs/nfsd/nfsctl.c code. */
42 #include "conditional.h"
44 unsigned int selinux_checkreqprot = CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
46 static int __init checkreqprot_setup(char *str)
48 unsigned long checkreqprot;
49 if (!kstrtoul(str, 0, &checkreqprot))
50 selinux_checkreqprot = checkreqprot ? 1 : 0;
53 __setup("checkreqprot=", checkreqprot_setup);
55 static DEFINE_MUTEX(sel_mutex);
57 /* global data for booleans */
58 static struct dentry *bool_dir;
60 static char **bool_pending_names;
61 static int *bool_pending_values;
63 /* global data for classes */
64 static struct dentry *class_dir;
65 static unsigned long last_class_ino;
67 static char policy_opened;
69 /* global data for policy capabilities */
70 static struct dentry *policycap_dir;
74 SEL_LOAD, /* load policy */
75 SEL_ENFORCE, /* get or set enforcing status */
76 SEL_CONTEXT, /* validate context */
77 SEL_ACCESS, /* compute access decision */
78 SEL_CREATE, /* compute create labeling decision */
79 SEL_RELABEL, /* compute relabeling decision */
80 SEL_USER, /* compute reachable user contexts */
81 SEL_POLICYVERS, /* return policy version for this kernel */
82 SEL_COMMIT_BOOLS, /* commit new boolean values */
83 SEL_MLS, /* return if MLS policy is enabled */
84 SEL_DISABLE, /* disable SELinux until next reboot */
85 SEL_MEMBER, /* compute polyinstantiation membership decision */
86 SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
87 SEL_COMPAT_NET, /* whether to use old compat network packet controls */
88 SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */
89 SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */
90 SEL_STATUS, /* export current status using mmap() */
91 SEL_POLICY, /* allow userspace to read the in kernel policy */
92 SEL_VALIDATE_TRANS, /* compute validatetrans decision */
93 SEL_INO_NEXT, /* The next inode number to use */
96 static unsigned long sel_last_ino = SEL_INO_NEXT - 1;
98 #define SEL_INITCON_INO_OFFSET 0x01000000
99 #define SEL_BOOL_INO_OFFSET 0x02000000
100 #define SEL_CLASS_INO_OFFSET 0x04000000
101 #define SEL_POLICYCAP_INO_OFFSET 0x08000000
102 #define SEL_INO_MASK 0x00ffffff
105 static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
106 size_t count, loff_t *ppos)
108 char tmpbuf[TMPBUFLEN];
111 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing);
112 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
115 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
116 static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
117 size_t count, loff_t *ppos)
124 if (count >= PAGE_SIZE)
127 /* No partial writes. */
131 page = memdup_user_nul(buf, count);
133 return PTR_ERR(page);
136 if (sscanf(page, "%d", &new_value) != 1)
139 new_value = !!new_value;
141 if (new_value != selinux_enforcing) {
142 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
143 SECCLASS_SECURITY, SECURITY__SETENFORCE,
147 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
148 "enforcing=%d old_enforcing=%d auid=%u ses=%u",
149 new_value, selinux_enforcing,
150 from_kuid(&init_user_ns, audit_get_loginuid(current)),
151 audit_get_sessionid(current));
152 selinux_enforcing = new_value;
153 if (selinux_enforcing)
155 selnl_notify_setenforce(selinux_enforcing);
156 selinux_status_update_setenforce(selinux_enforcing);
157 if (!selinux_enforcing)
158 call_lsm_notifier(LSM_POLICY_CHANGE, NULL);
166 #define sel_write_enforce NULL
169 static const struct file_operations sel_enforce_ops = {
170 .read = sel_read_enforce,
171 .write = sel_write_enforce,
172 .llseek = generic_file_llseek,
175 static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf,
176 size_t count, loff_t *ppos)
178 char tmpbuf[TMPBUFLEN];
180 ino_t ino = file_inode(filp)->i_ino;
181 int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
182 security_get_reject_unknown() : !security_get_allow_unknown();
184 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
185 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
188 static const struct file_operations sel_handle_unknown_ops = {
189 .read = sel_read_handle_unknown,
190 .llseek = generic_file_llseek,
193 static int sel_open_handle_status(struct inode *inode, struct file *filp)
195 struct page *status = selinux_kernel_status_page();
200 filp->private_data = status;
205 static ssize_t sel_read_handle_status(struct file *filp, char __user *buf,
206 size_t count, loff_t *ppos)
208 struct page *status = filp->private_data;
212 return simple_read_from_buffer(buf, count, ppos,
213 page_address(status),
214 sizeof(struct selinux_kernel_status));
217 static int sel_mmap_handle_status(struct file *filp,
218 struct vm_area_struct *vma)
220 struct page *status = filp->private_data;
221 unsigned long size = vma->vm_end - vma->vm_start;
225 /* only allows one page from the head */
226 if (vma->vm_pgoff > 0 || size != PAGE_SIZE)
228 /* disallow writable mapping */
229 if (vma->vm_flags & VM_WRITE)
231 /* disallow mprotect() turns it into writable */
232 vma->vm_flags &= ~VM_MAYWRITE;
234 return remap_pfn_range(vma, vma->vm_start,
236 size, vma->vm_page_prot);
239 static const struct file_operations sel_handle_status_ops = {
240 .open = sel_open_handle_status,
241 .read = sel_read_handle_status,
242 .mmap = sel_mmap_handle_status,
243 .llseek = generic_file_llseek,
246 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
247 static ssize_t sel_write_disable(struct file *file, const char __user *buf,
248 size_t count, loff_t *ppos)
255 if (count >= PAGE_SIZE)
258 /* No partial writes. */
262 page = memdup_user_nul(buf, count);
264 return PTR_ERR(page);
267 if (sscanf(page, "%d", &new_value) != 1)
271 length = selinux_disable();
274 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
275 "selinux=0 auid=%u ses=%u",
276 from_kuid(&init_user_ns, audit_get_loginuid(current)),
277 audit_get_sessionid(current));
286 #define sel_write_disable NULL
289 static const struct file_operations sel_disable_ops = {
290 .write = sel_write_disable,
291 .llseek = generic_file_llseek,
294 static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
295 size_t count, loff_t *ppos)
297 char tmpbuf[TMPBUFLEN];
300 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
301 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
304 static const struct file_operations sel_policyvers_ops = {
305 .read = sel_read_policyvers,
306 .llseek = generic_file_llseek,
309 /* declaration for sel_write_load */
310 static int sel_make_bools(void);
311 static int sel_make_classes(void);
312 static int sel_make_policycap(void);
314 /* declaration for sel_make_class_dirs */
315 static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
318 static ssize_t sel_read_mls(struct file *filp, char __user *buf,
319 size_t count, loff_t *ppos)
321 char tmpbuf[TMPBUFLEN];
324 length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
325 security_mls_enabled());
326 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
329 static const struct file_operations sel_mls_ops = {
330 .read = sel_read_mls,
331 .llseek = generic_file_llseek,
334 struct policy_load_memory {
339 static int sel_open_policy(struct inode *inode, struct file *filp)
341 struct policy_load_memory *plm = NULL;
344 BUG_ON(filp->private_data);
346 mutex_lock(&sel_mutex);
348 rc = avc_has_perm(current_sid(), SECINITSID_SECURITY,
349 SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
358 plm = kzalloc(sizeof(*plm), GFP_KERNEL);
362 if (i_size_read(inode) != security_policydb_len()) {
364 i_size_write(inode, security_policydb_len());
368 rc = security_read_policy(&plm->data, &plm->len);
374 filp->private_data = plm;
376 mutex_unlock(&sel_mutex);
380 mutex_unlock(&sel_mutex);
388 static int sel_release_policy(struct inode *inode, struct file *filp)
390 struct policy_load_memory *plm = filp->private_data;
402 static ssize_t sel_read_policy(struct file *filp, char __user *buf,
403 size_t count, loff_t *ppos)
405 struct policy_load_memory *plm = filp->private_data;
408 mutex_lock(&sel_mutex);
410 ret = avc_has_perm(current_sid(), SECINITSID_SECURITY,
411 SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
415 ret = simple_read_from_buffer(buf, count, ppos, plm->data, plm->len);
417 mutex_unlock(&sel_mutex);
421 static int sel_mmap_policy_fault(struct vm_fault *vmf)
423 struct policy_load_memory *plm = vmf->vma->vm_file->private_data;
424 unsigned long offset;
427 if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE))
428 return VM_FAULT_SIGBUS;
430 offset = vmf->pgoff << PAGE_SHIFT;
431 if (offset >= roundup(plm->len, PAGE_SIZE))
432 return VM_FAULT_SIGBUS;
434 page = vmalloc_to_page(plm->data + offset);
442 static const struct vm_operations_struct sel_mmap_policy_ops = {
443 .fault = sel_mmap_policy_fault,
444 .page_mkwrite = sel_mmap_policy_fault,
447 static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma)
449 if (vma->vm_flags & VM_SHARED) {
450 /* do not allow mprotect to make mapping writable */
451 vma->vm_flags &= ~VM_MAYWRITE;
453 if (vma->vm_flags & VM_WRITE)
457 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
458 vma->vm_ops = &sel_mmap_policy_ops;
463 static const struct file_operations sel_policy_ops = {
464 .open = sel_open_policy,
465 .read = sel_read_policy,
466 .mmap = sel_mmap_policy,
467 .release = sel_release_policy,
468 .llseek = generic_file_llseek,
471 static ssize_t sel_write_load(struct file *file, const char __user *buf,
472 size_t count, loff_t *ppos)
478 mutex_lock(&sel_mutex);
480 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
481 SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL);
485 /* No partial writes. */
491 if (count > 64 * 1024 * 1024)
495 data = vmalloc(count);
500 if (copy_from_user(data, buf, count) != 0)
503 length = security_load_policy(data, count);
505 pr_warn_ratelimited("SELinux: failed to load policy\n");
509 length = sel_make_bools();
511 pr_err("SELinux: failed to load policy booleans\n");
515 length = sel_make_classes();
517 pr_err("SELinux: failed to load policy classes\n");
521 length = sel_make_policycap();
523 pr_err("SELinux: failed to load policy capabilities\n");
530 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
531 "policy loaded auid=%u ses=%u",
532 from_kuid(&init_user_ns, audit_get_loginuid(current)),
533 audit_get_sessionid(current));
535 mutex_unlock(&sel_mutex);
540 static const struct file_operations sel_load_ops = {
541 .write = sel_write_load,
542 .llseek = generic_file_llseek,
545 static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
551 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
552 SECCLASS_SECURITY, SECURITY__CHECK_CONTEXT, NULL);
556 length = security_context_to_sid(buf, size, &sid, GFP_KERNEL);
560 length = security_sid_to_context(sid, &canon, &len);
565 if (len > SIMPLE_TRANSACTION_LIMIT) {
566 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
567 "payload max\n", __func__, len);
571 memcpy(buf, canon, len);
578 static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
579 size_t count, loff_t *ppos)
581 char tmpbuf[TMPBUFLEN];
584 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot);
585 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
588 static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
589 size_t count, loff_t *ppos)
593 unsigned int new_value;
595 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
596 SECCLASS_SECURITY, SECURITY__SETCHECKREQPROT,
601 if (count >= PAGE_SIZE)
604 /* No partial writes. */
608 page = memdup_user_nul(buf, count);
610 return PTR_ERR(page);
613 if (sscanf(page, "%u", &new_value) != 1)
616 selinux_checkreqprot = new_value ? 1 : 0;
622 static const struct file_operations sel_checkreqprot_ops = {
623 .read = sel_read_checkreqprot,
624 .write = sel_write_checkreqprot,
625 .llseek = generic_file_llseek,
628 static ssize_t sel_write_validatetrans(struct file *file,
629 const char __user *buf,
630 size_t count, loff_t *ppos)
632 char *oldcon = NULL, *newcon = NULL, *taskcon = NULL;
634 u32 osid, nsid, tsid;
638 rc = avc_has_perm(current_sid(), SECINITSID_SECURITY,
639 SECCLASS_SECURITY, SECURITY__VALIDATE_TRANS, NULL);
644 if (count >= PAGE_SIZE)
647 /* No partial writes. */
652 req = memdup_user_nul(buf, count);
660 oldcon = kzalloc(count + 1, GFP_KERNEL);
664 newcon = kzalloc(count + 1, GFP_KERNEL);
668 taskcon = kzalloc(count + 1, GFP_KERNEL);
673 if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4)
676 rc = security_context_str_to_sid(oldcon, &osid, GFP_KERNEL);
680 rc = security_context_str_to_sid(newcon, &nsid, GFP_KERNEL);
684 rc = security_context_str_to_sid(taskcon, &tsid, GFP_KERNEL);
688 rc = security_validate_transition_user(osid, nsid, tsid, tclass);
699 static const struct file_operations sel_transition_ops = {
700 .write = sel_write_validatetrans,
701 .llseek = generic_file_llseek,
705 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
707 static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
708 static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
709 static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
710 static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
711 static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
713 static ssize_t (*write_op[])(struct file *, char *, size_t) = {
714 [SEL_ACCESS] = sel_write_access,
715 [SEL_CREATE] = sel_write_create,
716 [SEL_RELABEL] = sel_write_relabel,
717 [SEL_USER] = sel_write_user,
718 [SEL_MEMBER] = sel_write_member,
719 [SEL_CONTEXT] = sel_write_context,
722 static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
724 ino_t ino = file_inode(file)->i_ino;
728 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
731 data = simple_transaction_get(file, buf, size);
733 return PTR_ERR(data);
735 rv = write_op[ino](file, data, size);
737 simple_transaction_set(file, rv);
743 static const struct file_operations transaction_ops = {
744 .write = selinux_transaction_write,
745 .read = simple_transaction_read,
746 .release = simple_transaction_release,
747 .llseek = generic_file_llseek,
751 * payload - write methods
752 * If the method has a response, the response should be put in buf,
753 * and the length returned. Otherwise return 0 or and -error.
756 static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
758 char *scon = NULL, *tcon = NULL;
761 struct av_decision avd;
764 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
765 SECCLASS_SECURITY, SECURITY__COMPUTE_AV, NULL);
770 scon = kzalloc(size + 1, GFP_KERNEL);
775 tcon = kzalloc(size + 1, GFP_KERNEL);
780 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
783 length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL);
787 length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL);
791 security_compute_av_user(ssid, tsid, tclass, &avd);
793 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
795 avd.allowed, 0xffffffff,
796 avd.auditallow, avd.auditdeny,
797 avd.seqno, avd.flags);
804 static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
806 char *scon = NULL, *tcon = NULL;
807 char *namebuf = NULL, *objname = NULL;
808 u32 ssid, tsid, newsid;
815 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
816 SECCLASS_SECURITY, SECURITY__COMPUTE_CREATE,
822 scon = kzalloc(size + 1, GFP_KERNEL);
827 tcon = kzalloc(size + 1, GFP_KERNEL);
832 namebuf = kzalloc(size + 1, GFP_KERNEL);
837 nargs = sscanf(buf, "%s %s %hu %s", scon, tcon, &tclass, namebuf);
838 if (nargs < 3 || nargs > 4)
842 * If and when the name of new object to be queried contains
843 * either whitespace or multibyte characters, they shall be
844 * encoded based on the percentage-encoding rule.
845 * If not encoded, the sscanf logic picks up only left-half
846 * of the supplied name; splitted by a whitespace unexpectedly.
856 else if (c1 == '%') {
857 c1 = hex_to_bin(*r++);
860 c2 = hex_to_bin(*r++);
866 } while (c1 != '\0');
871 length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL);
875 length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL);
879 length = security_transition_sid_user(ssid, tsid, tclass,
884 length = security_sid_to_context(newsid, &newcon, &len);
889 if (len > SIMPLE_TRANSACTION_LIMIT) {
890 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
891 "payload max\n", __func__, len);
895 memcpy(buf, newcon, len);
905 static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
907 char *scon = NULL, *tcon = NULL;
908 u32 ssid, tsid, newsid;
914 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
915 SECCLASS_SECURITY, SECURITY__COMPUTE_RELABEL,
921 scon = kzalloc(size + 1, GFP_KERNEL);
926 tcon = kzalloc(size + 1, GFP_KERNEL);
931 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
934 length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL);
938 length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL);
942 length = security_change_sid(ssid, tsid, tclass, &newsid);
946 length = security_sid_to_context(newsid, &newcon, &len);
951 if (len > SIMPLE_TRANSACTION_LIMIT)
954 memcpy(buf, newcon, len);
963 static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
965 char *con = NULL, *user = NULL, *ptr;
966 u32 sid, *sids = NULL;
972 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
973 SECCLASS_SECURITY, SECURITY__COMPUTE_USER,
979 con = kzalloc(size + 1, GFP_KERNEL);
984 user = kzalloc(size + 1, GFP_KERNEL);
989 if (sscanf(buf, "%s %s", con, user) != 2)
992 length = security_context_str_to_sid(con, &sid, GFP_KERNEL);
996 length = security_get_user_sids(sid, user, &sids, &nsids);
1000 length = sprintf(buf, "%u", nsids) + 1;
1002 for (i = 0; i < nsids; i++) {
1003 rc = security_sid_to_context(sids[i], &newcon, &len);
1008 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
1013 memcpy(ptr, newcon, len);
1025 static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
1027 char *scon = NULL, *tcon = NULL;
1028 u32 ssid, tsid, newsid;
1031 char *newcon = NULL;
1034 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
1035 SECCLASS_SECURITY, SECURITY__COMPUTE_MEMBER,
1041 scon = kzalloc(size + 1, GFP_KERNEL);
1046 tcon = kzalloc(size + 1, GFP_KERNEL);
1051 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
1054 length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL);
1058 length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL);
1062 length = security_member_sid(ssid, tsid, tclass, &newsid);
1066 length = security_sid_to_context(newsid, &newcon, &len);
1071 if (len > SIMPLE_TRANSACTION_LIMIT) {
1072 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
1073 "payload max\n", __func__, len);
1077 memcpy(buf, newcon, len);
1086 static struct inode *sel_make_inode(struct super_block *sb, int mode)
1088 struct inode *ret = new_inode(sb);
1092 ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret);
1097 static ssize_t sel_read_bool(struct file *filep, char __user *buf,
1098 size_t count, loff_t *ppos)
1104 unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
1105 const char *name = filep->f_path.dentry->d_name.name;
1107 mutex_lock(&sel_mutex);
1110 if (index >= bool_num || strcmp(name, bool_pending_names[index]))
1114 page = (char *)get_zeroed_page(GFP_KERNEL);
1118 cur_enforcing = security_get_bool_value(index);
1119 if (cur_enforcing < 0) {
1120 ret = cur_enforcing;
1123 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
1124 bool_pending_values[index]);
1125 ret = simple_read_from_buffer(buf, count, ppos, page, length);
1127 mutex_unlock(&sel_mutex);
1128 free_page((unsigned long)page);
1132 static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
1133 size_t count, loff_t *ppos)
1138 unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
1139 const char *name = filep->f_path.dentry->d_name.name;
1141 mutex_lock(&sel_mutex);
1143 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
1144 SECCLASS_SECURITY, SECURITY__SETBOOL,
1150 if (index >= bool_num || strcmp(name, bool_pending_names[index]))
1154 if (count >= PAGE_SIZE)
1157 /* No partial writes. */
1162 page = memdup_user_nul(buf, count);
1164 length = PTR_ERR(page);
1170 if (sscanf(page, "%d", &new_value) != 1)
1176 bool_pending_values[index] = new_value;
1180 mutex_unlock(&sel_mutex);
1185 static const struct file_operations sel_bool_ops = {
1186 .read = sel_read_bool,
1187 .write = sel_write_bool,
1188 .llseek = generic_file_llseek,
1191 static ssize_t sel_commit_bools_write(struct file *filep,
1192 const char __user *buf,
1193 size_t count, loff_t *ppos)
1199 mutex_lock(&sel_mutex);
1201 length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
1202 SECCLASS_SECURITY, SECURITY__SETBOOL,
1208 if (count >= PAGE_SIZE)
1211 /* No partial writes. */
1216 page = memdup_user_nul(buf, count);
1218 length = PTR_ERR(page);
1224 if (sscanf(page, "%d", &new_value) != 1)
1228 if (new_value && bool_pending_values)
1229 length = security_set_bools(bool_num, bool_pending_values);
1235 mutex_unlock(&sel_mutex);
1240 static const struct file_operations sel_commit_bools_ops = {
1241 .write = sel_commit_bools_write,
1242 .llseek = generic_file_llseek,
1245 static void sel_remove_entries(struct dentry *de)
1248 shrink_dcache_parent(de);
1251 #define BOOL_DIR_NAME "booleans"
1253 static int sel_make_bools(void)
1257 struct dentry *dentry = NULL;
1258 struct dentry *dir = bool_dir;
1259 struct inode *inode = NULL;
1260 struct inode_security_struct *isec;
1261 char **names = NULL, *page;
1266 /* remove any existing files */
1267 for (i = 0; i < bool_num; i++)
1268 kfree(bool_pending_names[i]);
1269 kfree(bool_pending_names);
1270 kfree(bool_pending_values);
1272 bool_pending_names = NULL;
1273 bool_pending_values = NULL;
1275 sel_remove_entries(dir);
1278 page = (char *)get_zeroed_page(GFP_KERNEL);
1282 ret = security_get_bools(&num, &names, &values);
1286 for (i = 0; i < num; i++) {
1288 dentry = d_alloc_name(dir, names[i]);
1293 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1297 ret = -ENAMETOOLONG;
1298 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
1299 if (len >= PAGE_SIZE)
1302 isec = (struct inode_security_struct *)inode->i_security;
1303 ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid);
1305 pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n",
1307 sid = SECINITSID_SECURITY;
1311 isec->initialized = LABEL_INITIALIZED;
1312 inode->i_fop = &sel_bool_ops;
1313 inode->i_ino = i|SEL_BOOL_INO_OFFSET;
1314 d_add(dentry, inode);
1317 bool_pending_names = names;
1318 bool_pending_values = values;
1320 free_page((unsigned long)page);
1323 free_page((unsigned long)page);
1326 for (i = 0; i < num; i++)
1331 sel_remove_entries(dir);
1336 #define NULL_FILE_NAME "null"
1338 struct path selinux_null;
1340 static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1341 size_t count, loff_t *ppos)
1343 char tmpbuf[TMPBUFLEN];
1346 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
1347 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1350 static ssize_t sel_write_avc_cache_threshold(struct file *file,
1351 const char __user *buf,
1352 size_t count, loff_t *ppos)
1357 unsigned int new_value;
1359 ret = avc_has_perm(current_sid(), SECINITSID_SECURITY,
1360 SECCLASS_SECURITY, SECURITY__SETSECPARAM,
1365 if (count >= PAGE_SIZE)
1368 /* No partial writes. */
1372 page = memdup_user_nul(buf, count);
1374 return PTR_ERR(page);
1377 if (sscanf(page, "%u", &new_value) != 1)
1380 avc_cache_threshold = new_value;
1388 static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1389 size_t count, loff_t *ppos)
1394 page = (char *)__get_free_page(GFP_KERNEL);
1398 length = avc_get_hash_stats(page);
1400 length = simple_read_from_buffer(buf, count, ppos, page, length);
1401 free_page((unsigned long)page);
1406 static const struct file_operations sel_avc_cache_threshold_ops = {
1407 .read = sel_read_avc_cache_threshold,
1408 .write = sel_write_avc_cache_threshold,
1409 .llseek = generic_file_llseek,
1412 static const struct file_operations sel_avc_hash_stats_ops = {
1413 .read = sel_read_avc_hash_stats,
1414 .llseek = generic_file_llseek,
1417 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1418 static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1422 for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
1423 if (!cpu_possible(cpu))
1426 return &per_cpu(avc_cache_stats, cpu);
1431 static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1433 loff_t n = *pos - 1;
1436 return SEQ_START_TOKEN;
1438 return sel_avc_get_stat_idx(&n);
1441 static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1443 return sel_avc_get_stat_idx(pos);
1446 static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1448 struct avc_cache_stats *st = v;
1450 if (v == SEQ_START_TOKEN) {
1452 "lookups hits misses allocations reclaims frees\n");
1454 unsigned int lookups = st->lookups;
1455 unsigned int misses = st->misses;
1456 unsigned int hits = lookups - misses;
1457 seq_printf(seq, "%u %u %u %u %u %u\n", lookups,
1458 hits, misses, st->allocations,
1459 st->reclaims, st->frees);
1464 static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1467 static const struct seq_operations sel_avc_cache_stats_seq_ops = {
1468 .start = sel_avc_stats_seq_start,
1469 .next = sel_avc_stats_seq_next,
1470 .show = sel_avc_stats_seq_show,
1471 .stop = sel_avc_stats_seq_stop,
1474 static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1476 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1479 static const struct file_operations sel_avc_cache_stats_ops = {
1480 .open = sel_open_avc_cache_stats,
1482 .llseek = seq_lseek,
1483 .release = seq_release,
1487 static int sel_make_avc_files(struct dentry *dir)
1490 static const struct tree_descr files[] = {
1491 { "cache_threshold",
1492 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1493 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1494 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1495 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1499 for (i = 0; i < ARRAY_SIZE(files); i++) {
1500 struct inode *inode;
1501 struct dentry *dentry;
1503 dentry = d_alloc_name(dir, files[i].name);
1507 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1511 inode->i_fop = files[i].ops;
1512 inode->i_ino = ++sel_last_ino;
1513 d_add(dentry, inode);
1519 static ssize_t sel_read_initcon(struct file *file, char __user *buf,
1520 size_t count, loff_t *ppos)
1526 sid = file_inode(file)->i_ino&SEL_INO_MASK;
1527 ret = security_sid_to_context(sid, &con, &len);
1531 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1536 static const struct file_operations sel_initcon_ops = {
1537 .read = sel_read_initcon,
1538 .llseek = generic_file_llseek,
1541 static int sel_make_initcon_files(struct dentry *dir)
1545 for (i = 1; i <= SECINITSID_NUM; i++) {
1546 struct inode *inode;
1547 struct dentry *dentry;
1548 dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
1552 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1556 inode->i_fop = &sel_initcon_ops;
1557 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1558 d_add(dentry, inode);
1564 static inline unsigned long sel_class_to_ino(u16 class)
1566 return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1569 static inline u16 sel_ino_to_class(unsigned long ino)
1571 return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1);
1574 static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1576 return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1579 static inline u32 sel_ino_to_perm(unsigned long ino)
1581 return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1584 static ssize_t sel_read_class(struct file *file, char __user *buf,
1585 size_t count, loff_t *ppos)
1587 unsigned long ino = file_inode(file)->i_ino;
1588 char res[TMPBUFLEN];
1589 ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_class(ino));
1590 return simple_read_from_buffer(buf, count, ppos, res, len);
1593 static const struct file_operations sel_class_ops = {
1594 .read = sel_read_class,
1595 .llseek = generic_file_llseek,
1598 static ssize_t sel_read_perm(struct file *file, char __user *buf,
1599 size_t count, loff_t *ppos)
1601 unsigned long ino = file_inode(file)->i_ino;
1602 char res[TMPBUFLEN];
1603 ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino));
1604 return simple_read_from_buffer(buf, count, ppos, res, len);
1607 static const struct file_operations sel_perm_ops = {
1608 .read = sel_read_perm,
1609 .llseek = generic_file_llseek,
1612 static ssize_t sel_read_policycap(struct file *file, char __user *buf,
1613 size_t count, loff_t *ppos)
1616 char tmpbuf[TMPBUFLEN];
1618 unsigned long i_ino = file_inode(file)->i_ino;
1620 value = security_policycap_supported(i_ino & SEL_INO_MASK);
1621 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
1623 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1626 static const struct file_operations sel_policycap_ops = {
1627 .read = sel_read_policycap,
1628 .llseek = generic_file_llseek,
1631 static int sel_make_perm_files(char *objclass, int classvalue,
1637 rc = security_get_permissions(objclass, &perms, &nperms);
1641 for (i = 0; i < nperms; i++) {
1642 struct inode *inode;
1643 struct dentry *dentry;
1646 dentry = d_alloc_name(dir, perms[i]);
1651 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1655 inode->i_fop = &sel_perm_ops;
1656 /* i+1 since perm values are 1-indexed */
1657 inode->i_ino = sel_perm_to_ino(classvalue, i + 1);
1658 d_add(dentry, inode);
1662 for (i = 0; i < nperms; i++)
1668 static int sel_make_class_dir_entries(char *classname, int index,
1671 struct dentry *dentry = NULL;
1672 struct inode *inode = NULL;
1675 dentry = d_alloc_name(dir, "index");
1679 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1683 inode->i_fop = &sel_class_ops;
1684 inode->i_ino = sel_class_to_ino(index);
1685 d_add(dentry, inode);
1687 dentry = sel_make_dir(dir, "perms", &last_class_ino);
1689 return PTR_ERR(dentry);
1691 rc = sel_make_perm_files(classname, index, dentry);
1696 static int sel_make_classes(void)
1698 int rc, nclasses, i;
1701 /* delete any existing entries */
1702 sel_remove_entries(class_dir);
1704 rc = security_get_classes(&classes, &nclasses);
1708 /* +2 since classes are 1-indexed */
1709 last_class_ino = sel_class_to_ino(nclasses + 2);
1711 for (i = 0; i < nclasses; i++) {
1712 struct dentry *class_name_dir;
1714 class_name_dir = sel_make_dir(class_dir, classes[i],
1716 if (IS_ERR(class_name_dir)) {
1717 rc = PTR_ERR(class_name_dir);
1721 /* i+1 since class values are 1-indexed */
1722 rc = sel_make_class_dir_entries(classes[i], i + 1,
1729 for (i = 0; i < nclasses; i++)
1735 static int sel_make_policycap(void)
1738 struct dentry *dentry = NULL;
1739 struct inode *inode = NULL;
1741 sel_remove_entries(policycap_dir);
1743 for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
1744 if (iter < ARRAY_SIZE(selinux_policycap_names))
1745 dentry = d_alloc_name(policycap_dir,
1746 selinux_policycap_names[iter]);
1748 dentry = d_alloc_name(policycap_dir, "unknown");
1753 inode = sel_make_inode(policycap_dir->d_sb, S_IFREG | S_IRUGO);
1757 inode->i_fop = &sel_policycap_ops;
1758 inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
1759 d_add(dentry, inode);
1765 static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
1768 struct dentry *dentry = d_alloc_name(dir, name);
1769 struct inode *inode;
1772 return ERR_PTR(-ENOMEM);
1774 inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1777 return ERR_PTR(-ENOMEM);
1780 inode->i_op = &simple_dir_inode_operations;
1781 inode->i_fop = &simple_dir_operations;
1782 inode->i_ino = ++(*ino);
1783 /* directory inodes start off with i_nlink == 2 (for "." entry) */
1785 d_add(dentry, inode);
1786 /* bump link count on parent directory, too */
1787 inc_nlink(d_inode(dir));
1792 static int sel_fill_super(struct super_block *sb, void *data, int silent)
1795 struct dentry *dentry;
1796 struct inode *inode;
1797 struct inode_security_struct *isec;
1799 static const struct tree_descr selinux_files[] = {
1800 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1801 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
1802 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
1803 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1804 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1805 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1806 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1807 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1808 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1809 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1810 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1811 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1812 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
1813 [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
1814 [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
1815 [SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO},
1816 [SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO},
1817 [SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops,
1821 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1825 bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &sel_last_ino);
1826 if (IS_ERR(bool_dir)) {
1827 ret = PTR_ERR(bool_dir);
1833 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
1838 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
1842 inode->i_ino = ++sel_last_ino;
1843 isec = (struct inode_security_struct *)inode->i_security;
1844 isec->sid = SECINITSID_DEVNULL;
1845 isec->sclass = SECCLASS_CHR_FILE;
1846 isec->initialized = LABEL_INITIALIZED;
1848 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1849 d_add(dentry, inode);
1850 selinux_null.dentry = dentry;
1852 dentry = sel_make_dir(sb->s_root, "avc", &sel_last_ino);
1853 if (IS_ERR(dentry)) {
1854 ret = PTR_ERR(dentry);
1858 ret = sel_make_avc_files(dentry);
1862 dentry = sel_make_dir(sb->s_root, "initial_contexts", &sel_last_ino);
1863 if (IS_ERR(dentry)) {
1864 ret = PTR_ERR(dentry);
1868 ret = sel_make_initcon_files(dentry);
1872 class_dir = sel_make_dir(sb->s_root, "class", &sel_last_ino);
1873 if (IS_ERR(class_dir)) {
1874 ret = PTR_ERR(class_dir);
1879 policycap_dir = sel_make_dir(sb->s_root, "policy_capabilities", &sel_last_ino);
1880 if (IS_ERR(policycap_dir)) {
1881 ret = PTR_ERR(policycap_dir);
1882 policycap_dir = NULL;
1887 printk(KERN_ERR "SELinux: %s: failed while creating inodes\n",
1892 static struct dentry *sel_mount(struct file_system_type *fs_type,
1893 int flags, const char *dev_name, void *data)
1895 return mount_single(fs_type, flags, data, sel_fill_super);
1898 static struct file_system_type sel_fs_type = {
1899 .name = "selinuxfs",
1901 .kill_sb = kill_litter_super,
1904 struct vfsmount *selinuxfs_mount;
1906 static int __init init_sel_fs(void)
1910 if (!selinux_enabled)
1913 err = sysfs_create_mount_point(fs_kobj, "selinux");
1917 err = register_filesystem(&sel_fs_type);
1919 sysfs_remove_mount_point(fs_kobj, "selinux");
1923 selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type);
1924 if (IS_ERR(selinuxfs_mount)) {
1925 printk(KERN_ERR "selinuxfs: could not mount!\n");
1926 err = PTR_ERR(selinuxfs_mount);
1927 selinuxfs_mount = NULL;
1933 __initcall(init_sel_fs);
1935 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
1936 void exit_sel_fs(void)
1938 sysfs_remove_mount_point(fs_kobj, "selinux");
1939 kern_unmount(selinuxfs_mount);
1940 unregister_filesystem(&sel_fs_type);