1 /* Updated: Karl MacMillan <kmacmillan@tresys.com>
3 * Added conditional policy language extensions
5 * Updated: Hewlett-Packard <paul@paul-moore.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>
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 /* Policy capability filenames */
45 static char *policycap_names[] = {
46 "network_peer_controls",
49 "always_check_network"
52 unsigned int selinux_checkreqprot = CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
54 static int __init checkreqprot_setup(char *str)
56 unsigned long checkreqprot;
57 if (!kstrtoul(str, 0, &checkreqprot))
58 selinux_checkreqprot = checkreqprot ? 1 : 0;
61 __setup("checkreqprot=", checkreqprot_setup);
63 static DEFINE_MUTEX(sel_mutex);
65 /* global data for booleans */
66 static struct dentry *bool_dir;
68 static char **bool_pending_names;
69 static int *bool_pending_values;
71 /* global data for classes */
72 static struct dentry *class_dir;
73 static unsigned long last_class_ino;
75 static char policy_opened;
77 /* global data for policy capabilities */
78 static struct dentry *policycap_dir;
80 /* Check whether a task is allowed to use a security operation. */
81 static int task_has_security(struct task_struct *tsk,
84 const struct task_security_struct *tsec;
88 tsec = __task_cred(tsk)->security;
95 return avc_has_perm(sid, SECINITSID_SECURITY,
96 SECCLASS_SECURITY, perms, NULL);
101 SEL_LOAD, /* load policy */
102 SEL_ENFORCE, /* get or set enforcing status */
103 SEL_CONTEXT, /* validate context */
104 SEL_ACCESS, /* compute access decision */
105 SEL_CREATE, /* compute create labeling decision */
106 SEL_RELABEL, /* compute relabeling decision */
107 SEL_USER, /* compute reachable user contexts */
108 SEL_POLICYVERS, /* return policy version for this kernel */
109 SEL_COMMIT_BOOLS, /* commit new boolean values */
110 SEL_MLS, /* return if MLS policy is enabled */
111 SEL_DISABLE, /* disable SELinux until next reboot */
112 SEL_MEMBER, /* compute polyinstantiation membership decision */
113 SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
114 SEL_COMPAT_NET, /* whether to use old compat network packet controls */
115 SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */
116 SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */
117 SEL_STATUS, /* export current status using mmap() */
118 SEL_POLICY, /* allow userspace to read the in kernel policy */
119 SEL_VALIDATE_TRANS, /* compute validatetrans decision */
120 SEL_INO_NEXT, /* The next inode number to use */
123 static unsigned long sel_last_ino = SEL_INO_NEXT - 1;
125 #define SEL_INITCON_INO_OFFSET 0x01000000
126 #define SEL_BOOL_INO_OFFSET 0x02000000
127 #define SEL_CLASS_INO_OFFSET 0x04000000
128 #define SEL_POLICYCAP_INO_OFFSET 0x08000000
129 #define SEL_INO_MASK 0x00ffffff
132 static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
133 size_t count, loff_t *ppos)
135 char tmpbuf[TMPBUFLEN];
138 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing);
139 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
142 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
143 static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
144 size_t count, loff_t *ppos)
152 if (count >= PAGE_SIZE)
155 /* No partial writes. */
161 page = (char *)get_zeroed_page(GFP_KERNEL);
166 if (copy_from_user(page, buf, count))
170 if (sscanf(page, "%d", &new_value) != 1)
173 if (new_value != selinux_enforcing) {
174 length = task_has_security(current, SECURITY__SETENFORCE);
177 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
178 "enforcing=%d old_enforcing=%d auid=%u ses=%u",
179 new_value, selinux_enforcing,
180 from_kuid(&init_user_ns, audit_get_loginuid(current)),
181 audit_get_sessionid(current));
182 selinux_enforcing = new_value;
183 if (selinux_enforcing)
185 selnl_notify_setenforce(selinux_enforcing);
186 selinux_status_update_setenforce(selinux_enforcing);
190 free_page((unsigned long) page);
194 #define sel_write_enforce NULL
197 static const struct file_operations sel_enforce_ops = {
198 .read = sel_read_enforce,
199 .write = sel_write_enforce,
200 .llseek = generic_file_llseek,
203 static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf,
204 size_t count, loff_t *ppos)
206 char tmpbuf[TMPBUFLEN];
208 ino_t ino = file_inode(filp)->i_ino;
209 int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
210 security_get_reject_unknown() : !security_get_allow_unknown();
212 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
213 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
216 static const struct file_operations sel_handle_unknown_ops = {
217 .read = sel_read_handle_unknown,
218 .llseek = generic_file_llseek,
221 static int sel_open_handle_status(struct inode *inode, struct file *filp)
223 struct page *status = selinux_kernel_status_page();
228 filp->private_data = status;
233 static ssize_t sel_read_handle_status(struct file *filp, char __user *buf,
234 size_t count, loff_t *ppos)
236 struct page *status = filp->private_data;
240 return simple_read_from_buffer(buf, count, ppos,
241 page_address(status),
242 sizeof(struct selinux_kernel_status));
245 static int sel_mmap_handle_status(struct file *filp,
246 struct vm_area_struct *vma)
248 struct page *status = filp->private_data;
249 unsigned long size = vma->vm_end - vma->vm_start;
253 /* only allows one page from the head */
254 if (vma->vm_pgoff > 0 || size != PAGE_SIZE)
256 /* disallow writable mapping */
257 if (vma->vm_flags & VM_WRITE)
259 /* disallow mprotect() turns it into writable */
260 vma->vm_flags &= ~VM_MAYWRITE;
262 return remap_pfn_range(vma, vma->vm_start,
264 size, vma->vm_page_prot);
267 static const struct file_operations sel_handle_status_ops = {
268 .open = sel_open_handle_status,
269 .read = sel_read_handle_status,
270 .mmap = sel_mmap_handle_status,
271 .llseek = generic_file_llseek,
274 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
275 static ssize_t sel_write_disable(struct file *file, const char __user *buf,
276 size_t count, loff_t *ppos)
284 if (count >= PAGE_SIZE)
287 /* No partial writes. */
293 page = (char *)get_zeroed_page(GFP_KERNEL);
298 if (copy_from_user(page, buf, count))
302 if (sscanf(page, "%d", &new_value) != 1)
306 length = selinux_disable();
309 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
310 "selinux=0 auid=%u ses=%u",
311 from_kuid(&init_user_ns, audit_get_loginuid(current)),
312 audit_get_sessionid(current));
317 free_page((unsigned long) page);
321 #define sel_write_disable NULL
324 static const struct file_operations sel_disable_ops = {
325 .write = sel_write_disable,
326 .llseek = generic_file_llseek,
329 static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
330 size_t count, loff_t *ppos)
332 char tmpbuf[TMPBUFLEN];
335 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
336 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
339 static const struct file_operations sel_policyvers_ops = {
340 .read = sel_read_policyvers,
341 .llseek = generic_file_llseek,
344 /* declaration for sel_write_load */
345 static int sel_make_bools(void);
346 static int sel_make_classes(void);
347 static int sel_make_policycap(void);
349 /* declaration for sel_make_class_dirs */
350 static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
353 static ssize_t sel_read_mls(struct file *filp, char __user *buf,
354 size_t count, loff_t *ppos)
356 char tmpbuf[TMPBUFLEN];
359 length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
360 security_mls_enabled());
361 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
364 static const struct file_operations sel_mls_ops = {
365 .read = sel_read_mls,
366 .llseek = generic_file_llseek,
369 struct policy_load_memory {
374 static int sel_open_policy(struct inode *inode, struct file *filp)
376 struct policy_load_memory *plm = NULL;
379 BUG_ON(filp->private_data);
381 mutex_lock(&sel_mutex);
383 rc = task_has_security(current, SECURITY__READ_POLICY);
392 plm = kzalloc(sizeof(*plm), GFP_KERNEL);
396 if (i_size_read(inode) != security_policydb_len()) {
397 mutex_lock(&inode->i_mutex);
398 i_size_write(inode, security_policydb_len());
399 mutex_unlock(&inode->i_mutex);
402 rc = security_read_policy(&plm->data, &plm->len);
408 filp->private_data = plm;
410 mutex_unlock(&sel_mutex);
414 mutex_unlock(&sel_mutex);
422 static int sel_release_policy(struct inode *inode, struct file *filp)
424 struct policy_load_memory *plm = filp->private_data;
436 static ssize_t sel_read_policy(struct file *filp, char __user *buf,
437 size_t count, loff_t *ppos)
439 struct policy_load_memory *plm = filp->private_data;
442 mutex_lock(&sel_mutex);
444 ret = task_has_security(current, SECURITY__READ_POLICY);
448 ret = simple_read_from_buffer(buf, count, ppos, plm->data, plm->len);
450 mutex_unlock(&sel_mutex);
454 static int sel_mmap_policy_fault(struct vm_area_struct *vma,
455 struct vm_fault *vmf)
457 struct policy_load_memory *plm = vma->vm_file->private_data;
458 unsigned long offset;
461 if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE))
462 return VM_FAULT_SIGBUS;
464 offset = vmf->pgoff << PAGE_SHIFT;
465 if (offset >= roundup(plm->len, PAGE_SIZE))
466 return VM_FAULT_SIGBUS;
468 page = vmalloc_to_page(plm->data + offset);
476 static const struct vm_operations_struct sel_mmap_policy_ops = {
477 .fault = sel_mmap_policy_fault,
478 .page_mkwrite = sel_mmap_policy_fault,
481 static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma)
483 if (vma->vm_flags & VM_SHARED) {
484 /* do not allow mprotect to make mapping writable */
485 vma->vm_flags &= ~VM_MAYWRITE;
487 if (vma->vm_flags & VM_WRITE)
491 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
492 vma->vm_ops = &sel_mmap_policy_ops;
497 static const struct file_operations sel_policy_ops = {
498 .open = sel_open_policy,
499 .read = sel_read_policy,
500 .mmap = sel_mmap_policy,
501 .release = sel_release_policy,
502 .llseek = generic_file_llseek,
505 static ssize_t sel_write_load(struct file *file, const char __user *buf,
506 size_t count, loff_t *ppos)
512 mutex_lock(&sel_mutex);
514 length = task_has_security(current, SECURITY__LOAD_POLICY);
518 /* No partial writes. */
524 if (count > 64 * 1024 * 1024)
528 data = vmalloc(count);
533 if (copy_from_user(data, buf, count) != 0)
536 length = security_load_policy(data, count);
540 length = sel_make_bools();
544 length = sel_make_classes();
548 length = sel_make_policycap();
555 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
556 "policy loaded auid=%u ses=%u",
557 from_kuid(&init_user_ns, audit_get_loginuid(current)),
558 audit_get_sessionid(current));
560 mutex_unlock(&sel_mutex);
565 static const struct file_operations sel_load_ops = {
566 .write = sel_write_load,
567 .llseek = generic_file_llseek,
570 static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
576 length = task_has_security(current, SECURITY__CHECK_CONTEXT);
580 length = security_context_to_sid(buf, size, &sid, GFP_KERNEL);
584 length = security_sid_to_context(sid, &canon, &len);
589 if (len > SIMPLE_TRANSACTION_LIMIT) {
590 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
591 "payload max\n", __func__, len);
595 memcpy(buf, canon, len);
602 static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
603 size_t count, loff_t *ppos)
605 char tmpbuf[TMPBUFLEN];
608 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot);
609 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
612 static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
613 size_t count, loff_t *ppos)
617 unsigned int new_value;
619 length = task_has_security(current, SECURITY__SETCHECKREQPROT);
624 if (count >= PAGE_SIZE)
627 /* No partial writes. */
633 page = (char *)get_zeroed_page(GFP_KERNEL);
638 if (copy_from_user(page, buf, count))
642 if (sscanf(page, "%u", &new_value) != 1)
645 selinux_checkreqprot = new_value ? 1 : 0;
648 free_page((unsigned long) page);
651 static const struct file_operations sel_checkreqprot_ops = {
652 .read = sel_read_checkreqprot,
653 .write = sel_write_checkreqprot,
654 .llseek = generic_file_llseek,
657 static ssize_t sel_write_validatetrans(struct file *file,
658 const char __user *buf,
659 size_t count, loff_t *ppos)
661 char *oldcon = NULL, *newcon = NULL, *taskcon = NULL;
663 u32 osid, nsid, tsid;
667 rc = task_has_security(current, SECURITY__VALIDATE_TRANS);
672 if (count >= PAGE_SIZE)
675 /* No partial writes. */
681 req = kzalloc(count + 1, GFP_KERNEL);
686 if (copy_from_user(req, buf, count))
690 oldcon = kzalloc(count + 1, GFP_KERNEL);
694 newcon = kzalloc(count + 1, GFP_KERNEL);
698 taskcon = kzalloc(count + 1, GFP_KERNEL);
703 if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4)
706 rc = security_context_str_to_sid(oldcon, &osid, GFP_KERNEL);
710 rc = security_context_str_to_sid(newcon, &nsid, GFP_KERNEL);
714 rc = security_context_str_to_sid(taskcon, &tsid, GFP_KERNEL);
718 rc = security_validate_transition_user(osid, nsid, tsid, tclass);
729 static const struct file_operations sel_transition_ops = {
730 .write = sel_write_validatetrans,
731 .llseek = generic_file_llseek,
735 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
737 static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
738 static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
739 static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
740 static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
741 static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
743 static ssize_t (*write_op[])(struct file *, char *, size_t) = {
744 [SEL_ACCESS] = sel_write_access,
745 [SEL_CREATE] = sel_write_create,
746 [SEL_RELABEL] = sel_write_relabel,
747 [SEL_USER] = sel_write_user,
748 [SEL_MEMBER] = sel_write_member,
749 [SEL_CONTEXT] = sel_write_context,
752 static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
754 ino_t ino = file_inode(file)->i_ino;
758 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
761 data = simple_transaction_get(file, buf, size);
763 return PTR_ERR(data);
765 rv = write_op[ino](file, data, size);
767 simple_transaction_set(file, rv);
773 static const struct file_operations transaction_ops = {
774 .write = selinux_transaction_write,
775 .read = simple_transaction_read,
776 .release = simple_transaction_release,
777 .llseek = generic_file_llseek,
781 * payload - write methods
782 * If the method has a response, the response should be put in buf,
783 * and the length returned. Otherwise return 0 or and -error.
786 static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
788 char *scon = NULL, *tcon = NULL;
791 struct av_decision avd;
794 length = task_has_security(current, SECURITY__COMPUTE_AV);
799 scon = kzalloc(size + 1, GFP_KERNEL);
804 tcon = kzalloc(size + 1, GFP_KERNEL);
809 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
812 length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL);
816 length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL);
820 security_compute_av_user(ssid, tsid, tclass, &avd);
822 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
824 avd.allowed, 0xffffffff,
825 avd.auditallow, avd.auditdeny,
826 avd.seqno, avd.flags);
833 static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
835 char *scon = NULL, *tcon = NULL;
836 char *namebuf = NULL, *objname = NULL;
837 u32 ssid, tsid, newsid;
844 length = task_has_security(current, SECURITY__COMPUTE_CREATE);
849 scon = kzalloc(size + 1, GFP_KERNEL);
854 tcon = kzalloc(size + 1, GFP_KERNEL);
859 namebuf = kzalloc(size + 1, GFP_KERNEL);
864 nargs = sscanf(buf, "%s %s %hu %s", scon, tcon, &tclass, namebuf);
865 if (nargs < 3 || nargs > 4)
869 * If and when the name of new object to be queried contains
870 * either whitespace or multibyte characters, they shall be
871 * encoded based on the percentage-encoding rule.
872 * If not encoded, the sscanf logic picks up only left-half
873 * of the supplied name; splitted by a whitespace unexpectedly.
883 else if (c1 == '%') {
884 c1 = hex_to_bin(*r++);
887 c2 = hex_to_bin(*r++);
893 } while (c1 != '\0');
898 length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL);
902 length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL);
906 length = security_transition_sid_user(ssid, tsid, tclass,
911 length = security_sid_to_context(newsid, &newcon, &len);
916 if (len > SIMPLE_TRANSACTION_LIMIT) {
917 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
918 "payload max\n", __func__, len);
922 memcpy(buf, newcon, len);
932 static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
934 char *scon = NULL, *tcon = NULL;
935 u32 ssid, tsid, newsid;
941 length = task_has_security(current, SECURITY__COMPUTE_RELABEL);
946 scon = kzalloc(size + 1, GFP_KERNEL);
951 tcon = kzalloc(size + 1, GFP_KERNEL);
956 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
959 length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL);
963 length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL);
967 length = security_change_sid(ssid, tsid, tclass, &newsid);
971 length = security_sid_to_context(newsid, &newcon, &len);
976 if (len > SIMPLE_TRANSACTION_LIMIT)
979 memcpy(buf, newcon, len);
988 static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
990 char *con = NULL, *user = NULL, *ptr;
991 u32 sid, *sids = NULL;
997 length = task_has_security(current, SECURITY__COMPUTE_USER);
1002 con = kzalloc(size + 1, GFP_KERNEL);
1007 user = kzalloc(size + 1, GFP_KERNEL);
1012 if (sscanf(buf, "%s %s", con, user) != 2)
1015 length = security_context_str_to_sid(con, &sid, GFP_KERNEL);
1019 length = security_get_user_sids(sid, user, &sids, &nsids);
1023 length = sprintf(buf, "%u", nsids) + 1;
1025 for (i = 0; i < nsids; i++) {
1026 rc = security_sid_to_context(sids[i], &newcon, &len);
1031 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
1036 memcpy(ptr, newcon, len);
1048 static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
1050 char *scon = NULL, *tcon = NULL;
1051 u32 ssid, tsid, newsid;
1054 char *newcon = NULL;
1057 length = task_has_security(current, SECURITY__COMPUTE_MEMBER);
1062 scon = kzalloc(size + 1, GFP_KERNEL);
1067 tcon = kzalloc(size + 1, GFP_KERNEL);
1072 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
1075 length = security_context_str_to_sid(scon, &ssid, GFP_KERNEL);
1079 length = security_context_str_to_sid(tcon, &tsid, GFP_KERNEL);
1083 length = security_member_sid(ssid, tsid, tclass, &newsid);
1087 length = security_sid_to_context(newsid, &newcon, &len);
1092 if (len > SIMPLE_TRANSACTION_LIMIT) {
1093 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
1094 "payload max\n", __func__, len);
1098 memcpy(buf, newcon, len);
1107 static struct inode *sel_make_inode(struct super_block *sb, int mode)
1109 struct inode *ret = new_inode(sb);
1113 ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
1118 static ssize_t sel_read_bool(struct file *filep, char __user *buf,
1119 size_t count, loff_t *ppos)
1125 unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
1126 const char *name = filep->f_path.dentry->d_name.name;
1128 mutex_lock(&sel_mutex);
1131 if (index >= bool_num || strcmp(name, bool_pending_names[index]))
1135 page = (char *)get_zeroed_page(GFP_KERNEL);
1139 cur_enforcing = security_get_bool_value(index);
1140 if (cur_enforcing < 0) {
1141 ret = cur_enforcing;
1144 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
1145 bool_pending_values[index]);
1146 ret = simple_read_from_buffer(buf, count, ppos, page, length);
1148 mutex_unlock(&sel_mutex);
1149 free_page((unsigned long)page);
1153 static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
1154 size_t count, loff_t *ppos)
1159 unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
1160 const char *name = filep->f_path.dentry->d_name.name;
1162 mutex_lock(&sel_mutex);
1164 length = task_has_security(current, SECURITY__SETBOOL);
1169 if (index >= bool_num || strcmp(name, bool_pending_names[index]))
1173 if (count >= PAGE_SIZE)
1176 /* No partial writes. */
1182 page = (char *)get_zeroed_page(GFP_KERNEL);
1187 if (copy_from_user(page, buf, count))
1191 if (sscanf(page, "%d", &new_value) != 1)
1197 bool_pending_values[index] = new_value;
1201 mutex_unlock(&sel_mutex);
1202 free_page((unsigned long) page);
1206 static const struct file_operations sel_bool_ops = {
1207 .read = sel_read_bool,
1208 .write = sel_write_bool,
1209 .llseek = generic_file_llseek,
1212 static ssize_t sel_commit_bools_write(struct file *filep,
1213 const char __user *buf,
1214 size_t count, loff_t *ppos)
1220 mutex_lock(&sel_mutex);
1222 length = task_has_security(current, SECURITY__SETBOOL);
1227 if (count >= PAGE_SIZE)
1230 /* No partial writes. */
1236 page = (char *)get_zeroed_page(GFP_KERNEL);
1241 if (copy_from_user(page, buf, count))
1245 if (sscanf(page, "%d", &new_value) != 1)
1249 if (new_value && bool_pending_values)
1250 length = security_set_bools(bool_num, bool_pending_values);
1256 mutex_unlock(&sel_mutex);
1257 free_page((unsigned long) page);
1261 static const struct file_operations sel_commit_bools_ops = {
1262 .write = sel_commit_bools_write,
1263 .llseek = generic_file_llseek,
1266 static void sel_remove_entries(struct dentry *de)
1269 shrink_dcache_parent(de);
1272 #define BOOL_DIR_NAME "booleans"
1274 static int sel_make_bools(void)
1278 struct dentry *dentry = NULL;
1279 struct dentry *dir = bool_dir;
1280 struct inode *inode = NULL;
1281 struct inode_security_struct *isec;
1282 char **names = NULL, *page;
1287 /* remove any existing files */
1288 for (i = 0; i < bool_num; i++)
1289 kfree(bool_pending_names[i]);
1290 kfree(bool_pending_names);
1291 kfree(bool_pending_values);
1293 bool_pending_names = NULL;
1294 bool_pending_values = NULL;
1296 sel_remove_entries(dir);
1299 page = (char *)get_zeroed_page(GFP_KERNEL);
1303 ret = security_get_bools(&num, &names, &values);
1307 for (i = 0; i < num; i++) {
1309 dentry = d_alloc_name(dir, names[i]);
1314 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1318 ret = -ENAMETOOLONG;
1319 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
1320 if (len >= PAGE_SIZE)
1323 isec = (struct inode_security_struct *)inode->i_security;
1324 ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid);
1329 isec->initialized = 1;
1330 inode->i_fop = &sel_bool_ops;
1331 inode->i_ino = i|SEL_BOOL_INO_OFFSET;
1332 d_add(dentry, inode);
1335 bool_pending_names = names;
1336 bool_pending_values = values;
1338 free_page((unsigned long)page);
1341 free_page((unsigned long)page);
1344 for (i = 0; i < num; i++)
1349 sel_remove_entries(dir);
1354 #define NULL_FILE_NAME "null"
1356 struct path selinux_null;
1358 static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1359 size_t count, loff_t *ppos)
1361 char tmpbuf[TMPBUFLEN];
1364 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
1365 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1368 static ssize_t sel_write_avc_cache_threshold(struct file *file,
1369 const char __user *buf,
1370 size_t count, loff_t *ppos)
1377 ret = task_has_security(current, SECURITY__SETSECPARAM);
1382 if (count >= PAGE_SIZE)
1385 /* No partial writes. */
1391 page = (char *)get_zeroed_page(GFP_KERNEL);
1396 if (copy_from_user(page, buf, count))
1400 if (sscanf(page, "%u", &new_value) != 1)
1403 avc_cache_threshold = new_value;
1407 free_page((unsigned long)page);
1411 static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1412 size_t count, loff_t *ppos)
1417 page = (char *)__get_free_page(GFP_KERNEL);
1421 length = avc_get_hash_stats(page);
1423 length = simple_read_from_buffer(buf, count, ppos, page, length);
1424 free_page((unsigned long)page);
1429 static const struct file_operations sel_avc_cache_threshold_ops = {
1430 .read = sel_read_avc_cache_threshold,
1431 .write = sel_write_avc_cache_threshold,
1432 .llseek = generic_file_llseek,
1435 static const struct file_operations sel_avc_hash_stats_ops = {
1436 .read = sel_read_avc_hash_stats,
1437 .llseek = generic_file_llseek,
1440 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1441 static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1445 for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
1446 if (!cpu_possible(cpu))
1449 return &per_cpu(avc_cache_stats, cpu);
1454 static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1456 loff_t n = *pos - 1;
1459 return SEQ_START_TOKEN;
1461 return sel_avc_get_stat_idx(&n);
1464 static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1466 return sel_avc_get_stat_idx(pos);
1469 static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1471 struct avc_cache_stats *st = v;
1473 if (v == SEQ_START_TOKEN)
1474 seq_printf(seq, "lookups hits misses allocations reclaims "
1477 unsigned int lookups = st->lookups;
1478 unsigned int misses = st->misses;
1479 unsigned int hits = lookups - misses;
1480 seq_printf(seq, "%u %u %u %u %u %u\n", lookups,
1481 hits, misses, st->allocations,
1482 st->reclaims, st->frees);
1487 static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1490 static const struct seq_operations sel_avc_cache_stats_seq_ops = {
1491 .start = sel_avc_stats_seq_start,
1492 .next = sel_avc_stats_seq_next,
1493 .show = sel_avc_stats_seq_show,
1494 .stop = sel_avc_stats_seq_stop,
1497 static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1499 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1502 static const struct file_operations sel_avc_cache_stats_ops = {
1503 .open = sel_open_avc_cache_stats,
1505 .llseek = seq_lseek,
1506 .release = seq_release,
1510 static int sel_make_avc_files(struct dentry *dir)
1513 static struct tree_descr files[] = {
1514 { "cache_threshold",
1515 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1516 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1517 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1518 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1522 for (i = 0; i < ARRAY_SIZE(files); i++) {
1523 struct inode *inode;
1524 struct dentry *dentry;
1526 dentry = d_alloc_name(dir, files[i].name);
1530 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1534 inode->i_fop = files[i].ops;
1535 inode->i_ino = ++sel_last_ino;
1536 d_add(dentry, inode);
1542 static ssize_t sel_read_initcon(struct file *file, char __user *buf,
1543 size_t count, loff_t *ppos)
1549 sid = file_inode(file)->i_ino&SEL_INO_MASK;
1550 ret = security_sid_to_context(sid, &con, &len);
1554 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1559 static const struct file_operations sel_initcon_ops = {
1560 .read = sel_read_initcon,
1561 .llseek = generic_file_llseek,
1564 static int sel_make_initcon_files(struct dentry *dir)
1568 for (i = 1; i <= SECINITSID_NUM; i++) {
1569 struct inode *inode;
1570 struct dentry *dentry;
1571 dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
1575 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1579 inode->i_fop = &sel_initcon_ops;
1580 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1581 d_add(dentry, inode);
1587 static inline unsigned long sel_class_to_ino(u16 class)
1589 return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1592 static inline u16 sel_ino_to_class(unsigned long ino)
1594 return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1);
1597 static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1599 return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1602 static inline u32 sel_ino_to_perm(unsigned long ino)
1604 return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1607 static ssize_t sel_read_class(struct file *file, char __user *buf,
1608 size_t count, loff_t *ppos)
1610 unsigned long ino = file_inode(file)->i_ino;
1611 char res[TMPBUFLEN];
1612 ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_class(ino));
1613 return simple_read_from_buffer(buf, count, ppos, res, len);
1616 static const struct file_operations sel_class_ops = {
1617 .read = sel_read_class,
1618 .llseek = generic_file_llseek,
1621 static ssize_t sel_read_perm(struct file *file, char __user *buf,
1622 size_t count, loff_t *ppos)
1624 unsigned long ino = file_inode(file)->i_ino;
1625 char res[TMPBUFLEN];
1626 ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino));
1627 return simple_read_from_buffer(buf, count, ppos, res, len);
1630 static const struct file_operations sel_perm_ops = {
1631 .read = sel_read_perm,
1632 .llseek = generic_file_llseek,
1635 static ssize_t sel_read_policycap(struct file *file, char __user *buf,
1636 size_t count, loff_t *ppos)
1639 char tmpbuf[TMPBUFLEN];
1641 unsigned long i_ino = file_inode(file)->i_ino;
1643 value = security_policycap_supported(i_ino & SEL_INO_MASK);
1644 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
1646 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1649 static const struct file_operations sel_policycap_ops = {
1650 .read = sel_read_policycap,
1651 .llseek = generic_file_llseek,
1654 static int sel_make_perm_files(char *objclass, int classvalue,
1660 rc = security_get_permissions(objclass, &perms, &nperms);
1664 for (i = 0; i < nperms; i++) {
1665 struct inode *inode;
1666 struct dentry *dentry;
1669 dentry = d_alloc_name(dir, perms[i]);
1674 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1678 inode->i_fop = &sel_perm_ops;
1679 /* i+1 since perm values are 1-indexed */
1680 inode->i_ino = sel_perm_to_ino(classvalue, i + 1);
1681 d_add(dentry, inode);
1685 for (i = 0; i < nperms; i++)
1691 static int sel_make_class_dir_entries(char *classname, int index,
1694 struct dentry *dentry = NULL;
1695 struct inode *inode = NULL;
1698 dentry = d_alloc_name(dir, "index");
1702 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1706 inode->i_fop = &sel_class_ops;
1707 inode->i_ino = sel_class_to_ino(index);
1708 d_add(dentry, inode);
1710 dentry = sel_make_dir(dir, "perms", &last_class_ino);
1712 return PTR_ERR(dentry);
1714 rc = sel_make_perm_files(classname, index, dentry);
1719 static int sel_make_classes(void)
1721 int rc, nclasses, i;
1724 /* delete any existing entries */
1725 sel_remove_entries(class_dir);
1727 rc = security_get_classes(&classes, &nclasses);
1731 /* +2 since classes are 1-indexed */
1732 last_class_ino = sel_class_to_ino(nclasses + 2);
1734 for (i = 0; i < nclasses; i++) {
1735 struct dentry *class_name_dir;
1737 class_name_dir = sel_make_dir(class_dir, classes[i],
1739 if (IS_ERR(class_name_dir)) {
1740 rc = PTR_ERR(class_name_dir);
1744 /* i+1 since class values are 1-indexed */
1745 rc = sel_make_class_dir_entries(classes[i], i + 1,
1752 for (i = 0; i < nclasses; i++)
1758 static int sel_make_policycap(void)
1761 struct dentry *dentry = NULL;
1762 struct inode *inode = NULL;
1764 sel_remove_entries(policycap_dir);
1766 for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
1767 if (iter < ARRAY_SIZE(policycap_names))
1768 dentry = d_alloc_name(policycap_dir,
1769 policycap_names[iter]);
1771 dentry = d_alloc_name(policycap_dir, "unknown");
1776 inode = sel_make_inode(policycap_dir->d_sb, S_IFREG | S_IRUGO);
1780 inode->i_fop = &sel_policycap_ops;
1781 inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
1782 d_add(dentry, inode);
1788 static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
1791 struct dentry *dentry = d_alloc_name(dir, name);
1792 struct inode *inode;
1795 return ERR_PTR(-ENOMEM);
1797 inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1800 return ERR_PTR(-ENOMEM);
1803 inode->i_op = &simple_dir_inode_operations;
1804 inode->i_fop = &simple_dir_operations;
1805 inode->i_ino = ++(*ino);
1806 /* directory inodes start off with i_nlink == 2 (for "." entry) */
1808 d_add(dentry, inode);
1809 /* bump link count on parent directory, too */
1810 inc_nlink(d_inode(dir));
1815 static int sel_fill_super(struct super_block *sb, void *data, int silent)
1818 struct dentry *dentry;
1819 struct inode *inode;
1820 struct inode_security_struct *isec;
1822 static struct tree_descr selinux_files[] = {
1823 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1824 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
1825 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
1826 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1827 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1828 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1829 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1830 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1831 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1832 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1833 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1834 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1835 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
1836 [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
1837 [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
1838 [SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO},
1839 [SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO},
1840 [SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops,
1844 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1848 bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &sel_last_ino);
1849 if (IS_ERR(bool_dir)) {
1850 ret = PTR_ERR(bool_dir);
1856 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
1861 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
1865 inode->i_ino = ++sel_last_ino;
1866 isec = (struct inode_security_struct *)inode->i_security;
1867 isec->sid = SECINITSID_DEVNULL;
1868 isec->sclass = SECCLASS_CHR_FILE;
1869 isec->initialized = 1;
1871 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1872 d_add(dentry, inode);
1873 selinux_null.dentry = dentry;
1875 dentry = sel_make_dir(sb->s_root, "avc", &sel_last_ino);
1876 if (IS_ERR(dentry)) {
1877 ret = PTR_ERR(dentry);
1881 ret = sel_make_avc_files(dentry);
1885 dentry = sel_make_dir(sb->s_root, "initial_contexts", &sel_last_ino);
1886 if (IS_ERR(dentry)) {
1887 ret = PTR_ERR(dentry);
1891 ret = sel_make_initcon_files(dentry);
1895 class_dir = sel_make_dir(sb->s_root, "class", &sel_last_ino);
1896 if (IS_ERR(class_dir)) {
1897 ret = PTR_ERR(class_dir);
1902 policycap_dir = sel_make_dir(sb->s_root, "policy_capabilities", &sel_last_ino);
1903 if (IS_ERR(policycap_dir)) {
1904 ret = PTR_ERR(policycap_dir);
1905 policycap_dir = NULL;
1910 printk(KERN_ERR "SELinux: %s: failed while creating inodes\n",
1915 static struct dentry *sel_mount(struct file_system_type *fs_type,
1916 int flags, const char *dev_name, void *data)
1918 return mount_single(fs_type, flags, data, sel_fill_super);
1921 static struct file_system_type sel_fs_type = {
1922 .name = "selinuxfs",
1924 .kill_sb = kill_litter_super,
1927 struct vfsmount *selinuxfs_mount;
1929 static int __init init_sel_fs(void)
1933 if (!selinux_enabled)
1936 err = sysfs_create_mount_point(fs_kobj, "selinux");
1940 err = register_filesystem(&sel_fs_type);
1942 sysfs_remove_mount_point(fs_kobj, "selinux");
1946 selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type);
1947 if (IS_ERR(selinuxfs_mount)) {
1948 printk(KERN_ERR "selinuxfs: could not mount!\n");
1949 err = PTR_ERR(selinuxfs_mount);
1950 selinuxfs_mount = NULL;
1956 __initcall(init_sel_fs);
1958 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
1959 void exit_sel_fs(void)
1961 sysfs_remove_mount_point(fs_kobj, "selinux");
1962 kern_unmount(selinuxfs_mount);
1963 unregister_filesystem(&sel_fs_type);