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/mount.h>
23 #include <linux/mutex.h>
24 #include <linux/init.h>
25 #include <linux/string.h>
26 #include <linux/security.h>
27 #include <linux/major.h>
28 #include <linux/seq_file.h>
29 #include <linux/percpu.h>
30 #include <linux/audit.h>
31 #include <linux/uaccess.h>
32 #include <linux/kobject.h>
33 #include <linux/ctype.h>
35 /* selinuxfs pseudo filesystem for exporting the security policy API.
36 Based on the proc code and the fs/nfsd/nfsctl.c code. */
43 #include "conditional.h"
47 SEL_LOAD, /* load policy */
48 SEL_ENFORCE, /* get or set enforcing status */
49 SEL_CONTEXT, /* validate context */
50 SEL_ACCESS, /* compute access decision */
51 SEL_CREATE, /* compute create labeling decision */
52 SEL_RELABEL, /* compute relabeling decision */
53 SEL_USER, /* compute reachable user contexts */
54 SEL_POLICYVERS, /* return policy version for this kernel */
55 SEL_COMMIT_BOOLS, /* commit new boolean values */
56 SEL_MLS, /* return if MLS policy is enabled */
57 SEL_DISABLE, /* disable SELinux until next reboot */
58 SEL_MEMBER, /* compute polyinstantiation membership decision */
59 SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
60 SEL_COMPAT_NET, /* whether to use old compat network packet controls */
61 SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */
62 SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */
63 SEL_STATUS, /* export current status using mmap() */
64 SEL_POLICY, /* allow userspace to read the in kernel policy */
65 SEL_VALIDATE_TRANS, /* compute validatetrans decision */
66 SEL_INO_NEXT, /* The next inode number to use */
69 struct selinux_fs_info {
70 struct dentry *bool_dir;
71 unsigned int bool_num;
72 char **bool_pending_names;
73 unsigned int *bool_pending_values;
74 struct dentry *class_dir;
75 unsigned long last_class_ino;
77 struct dentry *policycap_dir;
79 unsigned long last_ino;
80 struct selinux_state *state;
81 struct super_block *sb;
84 static int selinux_fs_info_create(struct super_block *sb)
86 struct selinux_fs_info *fsi;
88 fsi = kzalloc(sizeof(*fsi), GFP_KERNEL);
92 mutex_init(&fsi->mutex);
93 fsi->last_ino = SEL_INO_NEXT - 1;
94 fsi->state = &selinux_state;
100 static void selinux_fs_info_free(struct super_block *sb)
102 struct selinux_fs_info *fsi = sb->s_fs_info;
106 for (i = 0; i < fsi->bool_num; i++)
107 kfree(fsi->bool_pending_names[i]);
108 kfree(fsi->bool_pending_names);
109 kfree(fsi->bool_pending_values);
111 kfree(sb->s_fs_info);
112 sb->s_fs_info = NULL;
115 #define SEL_INITCON_INO_OFFSET 0x01000000
116 #define SEL_BOOL_INO_OFFSET 0x02000000
117 #define SEL_CLASS_INO_OFFSET 0x04000000
118 #define SEL_POLICYCAP_INO_OFFSET 0x08000000
119 #define SEL_INO_MASK 0x00ffffff
122 static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
123 size_t count, loff_t *ppos)
125 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
126 char tmpbuf[TMPBUFLEN];
129 length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
130 enforcing_enabled(fsi->state));
131 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
134 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
135 static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
136 size_t count, loff_t *ppos)
139 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
140 struct selinux_state *state = fsi->state;
143 int old_value, new_value;
145 if (count >= PAGE_SIZE)
148 /* No partial writes. */
152 page = memdup_user_nul(buf, count);
154 return PTR_ERR(page);
157 if (sscanf(page, "%d", &new_value) != 1)
160 new_value = !!new_value;
162 old_value = enforcing_enabled(state);
163 if (new_value != old_value) {
164 length = avc_has_perm(&selinux_state,
165 current_sid(), SECINITSID_SECURITY,
166 SECCLASS_SECURITY, SECURITY__SETENFORCE,
170 audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS,
171 "enforcing=%d old_enforcing=%d auid=%u ses=%u"
172 " enabled=%d old-enabled=%d lsm=selinux res=1",
173 new_value, old_value,
174 from_kuid(&init_user_ns, audit_get_loginuid(current)),
175 audit_get_sessionid(current),
176 selinux_enabled, selinux_enabled);
177 enforcing_set(state, new_value);
179 avc_ss_reset(state->avc, 0);
180 selnl_notify_setenforce(new_value);
181 selinux_status_update_setenforce(state, new_value);
183 call_lsm_notifier(LSM_POLICY_CHANGE, NULL);
191 #define sel_write_enforce NULL
194 static const struct file_operations sel_enforce_ops = {
195 .read = sel_read_enforce,
196 .write = sel_write_enforce,
197 .llseek = generic_file_llseek,
200 static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf,
201 size_t count, loff_t *ppos)
203 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
204 struct selinux_state *state = fsi->state;
205 char tmpbuf[TMPBUFLEN];
207 ino_t ino = file_inode(filp)->i_ino;
208 int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
209 security_get_reject_unknown(state) :
210 !security_get_allow_unknown(state);
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 selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
224 struct page *status = selinux_kernel_status_page(fsi->state);
229 filp->private_data = status;
234 static ssize_t sel_read_handle_status(struct file *filp, char __user *buf,
235 size_t count, loff_t *ppos)
237 struct page *status = filp->private_data;
241 return simple_read_from_buffer(buf, count, ppos,
242 page_address(status),
243 sizeof(struct selinux_kernel_status));
246 static int sel_mmap_handle_status(struct file *filp,
247 struct vm_area_struct *vma)
249 struct page *status = filp->private_data;
250 unsigned long size = vma->vm_end - vma->vm_start;
254 /* only allows one page from the head */
255 if (vma->vm_pgoff > 0 || size != PAGE_SIZE)
257 /* disallow writable mapping */
258 if (vma->vm_flags & VM_WRITE)
260 /* disallow mprotect() turns it into writable */
261 vma->vm_flags &= ~VM_MAYWRITE;
263 return remap_pfn_range(vma, vma->vm_start,
265 size, vma->vm_page_prot);
268 static const struct file_operations sel_handle_status_ops = {
269 .open = sel_open_handle_status,
270 .read = sel_read_handle_status,
271 .mmap = sel_mmap_handle_status,
272 .llseek = generic_file_llseek,
275 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
276 static ssize_t sel_write_disable(struct file *file, const char __user *buf,
277 size_t count, loff_t *ppos)
280 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
286 if (count >= PAGE_SIZE)
289 /* No partial writes. */
293 page = memdup_user_nul(buf, count);
295 return PTR_ERR(page);
298 if (sscanf(page, "%d", &new_value) != 1)
302 enforcing = enforcing_enabled(fsi->state);
303 length = selinux_disable(fsi->state);
306 audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS,
307 "enforcing=%d old_enforcing=%d auid=%u ses=%u"
308 " enabled=%d old-enabled=%d lsm=selinux res=1",
309 enforcing, enforcing,
310 from_kuid(&init_user_ns, audit_get_loginuid(current)),
311 audit_get_sessionid(current), 0, 1);
320 #define sel_write_disable NULL
323 static const struct file_operations sel_disable_ops = {
324 .write = sel_write_disable,
325 .llseek = generic_file_llseek,
328 static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
329 size_t count, loff_t *ppos)
331 char tmpbuf[TMPBUFLEN];
334 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
335 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
338 static const struct file_operations sel_policyvers_ops = {
339 .read = sel_read_policyvers,
340 .llseek = generic_file_llseek,
343 /* declaration for sel_write_load */
344 static int sel_make_bools(struct selinux_fs_info *fsi);
345 static int sel_make_classes(struct selinux_fs_info *fsi);
346 static int sel_make_policycap(struct selinux_fs_info *fsi);
348 /* declaration for sel_make_class_dirs */
349 static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
352 static ssize_t sel_read_mls(struct file *filp, char __user *buf,
353 size_t count, loff_t *ppos)
355 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
356 char tmpbuf[TMPBUFLEN];
359 length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
360 security_mls_enabled(fsi->state));
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 selinux_fs_info *fsi = inode->i_sb->s_fs_info;
377 struct selinux_state *state = fsi->state;
378 struct policy_load_memory *plm = NULL;
381 BUG_ON(filp->private_data);
383 mutex_lock(&fsi->mutex);
385 rc = avc_has_perm(&selinux_state,
386 current_sid(), SECINITSID_SECURITY,
387 SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
392 if (fsi->policy_opened)
396 plm = kzalloc(sizeof(*plm), GFP_KERNEL);
400 if (i_size_read(inode) != security_policydb_len(state)) {
402 i_size_write(inode, security_policydb_len(state));
406 rc = security_read_policy(state, &plm->data, &plm->len);
410 fsi->policy_opened = 1;
412 filp->private_data = plm;
414 mutex_unlock(&fsi->mutex);
418 mutex_unlock(&fsi->mutex);
426 static int sel_release_policy(struct inode *inode, struct file *filp)
428 struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
429 struct policy_load_memory *plm = filp->private_data;
433 fsi->policy_opened = 0;
441 static ssize_t sel_read_policy(struct file *filp, char __user *buf,
442 size_t count, loff_t *ppos)
444 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
445 struct policy_load_memory *plm = filp->private_data;
448 mutex_lock(&fsi->mutex);
450 ret = avc_has_perm(&selinux_state,
451 current_sid(), SECINITSID_SECURITY,
452 SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
456 ret = simple_read_from_buffer(buf, count, ppos, plm->data, plm->len);
458 mutex_unlock(&fsi->mutex);
462 static vm_fault_t sel_mmap_policy_fault(struct vm_fault *vmf)
464 struct policy_load_memory *plm = vmf->vma->vm_file->private_data;
465 unsigned long offset;
468 if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE))
469 return VM_FAULT_SIGBUS;
471 offset = vmf->pgoff << PAGE_SHIFT;
472 if (offset >= roundup(plm->len, PAGE_SIZE))
473 return VM_FAULT_SIGBUS;
475 page = vmalloc_to_page(plm->data + offset);
483 static const struct vm_operations_struct sel_mmap_policy_ops = {
484 .fault = sel_mmap_policy_fault,
485 .page_mkwrite = sel_mmap_policy_fault,
488 static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma)
490 if (vma->vm_flags & VM_SHARED) {
491 /* do not allow mprotect to make mapping writable */
492 vma->vm_flags &= ~VM_MAYWRITE;
494 if (vma->vm_flags & VM_WRITE)
498 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
499 vma->vm_ops = &sel_mmap_policy_ops;
504 static const struct file_operations sel_policy_ops = {
505 .open = sel_open_policy,
506 .read = sel_read_policy,
507 .mmap = sel_mmap_policy,
508 .release = sel_release_policy,
509 .llseek = generic_file_llseek,
512 static int sel_make_policy_nodes(struct selinux_fs_info *fsi)
516 ret = sel_make_bools(fsi);
518 pr_err("SELinux: failed to load policy booleans\n");
522 ret = sel_make_classes(fsi);
524 pr_err("SELinux: failed to load policy classes\n");
528 ret = sel_make_policycap(fsi);
530 pr_err("SELinux: failed to load policy capabilities\n");
537 static ssize_t sel_write_load(struct file *file, const char __user *buf,
538 size_t count, loff_t *ppos)
541 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
545 mutex_lock(&fsi->mutex);
547 length = avc_has_perm(&selinux_state,
548 current_sid(), SECINITSID_SECURITY,
549 SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL);
553 /* No partial writes. */
559 if (count > 64 * 1024 * 1024)
563 data = vmalloc(count);
568 if (copy_from_user(data, buf, count) != 0)
571 length = security_load_policy(fsi->state, data, count);
573 pr_warn_ratelimited("SELinux: failed to load policy\n");
577 length = sel_make_policy_nodes(fsi);
584 audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
585 "auid=%u ses=%u lsm=selinux res=1",
586 from_kuid(&init_user_ns, audit_get_loginuid(current)),
587 audit_get_sessionid(current));
589 mutex_unlock(&fsi->mutex);
594 static const struct file_operations sel_load_ops = {
595 .write = sel_write_load,
596 .llseek = generic_file_llseek,
599 static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
601 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
602 struct selinux_state *state = fsi->state;
607 length = avc_has_perm(&selinux_state,
608 current_sid(), SECINITSID_SECURITY,
609 SECCLASS_SECURITY, SECURITY__CHECK_CONTEXT, NULL);
613 length = security_context_to_sid(state, buf, size, &sid, GFP_KERNEL);
617 length = security_sid_to_context(state, sid, &canon, &len);
622 if (len > SIMPLE_TRANSACTION_LIMIT) {
623 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
624 "payload max\n", __func__, len);
628 memcpy(buf, canon, len);
635 static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
636 size_t count, loff_t *ppos)
638 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
639 char tmpbuf[TMPBUFLEN];
642 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", fsi->state->checkreqprot);
643 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
646 static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
647 size_t count, loff_t *ppos)
649 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
652 unsigned int new_value;
654 length = avc_has_perm(&selinux_state,
655 current_sid(), SECINITSID_SECURITY,
656 SECCLASS_SECURITY, SECURITY__SETCHECKREQPROT,
661 if (count >= PAGE_SIZE)
664 /* No partial writes. */
668 page = memdup_user_nul(buf, count);
670 return PTR_ERR(page);
673 if (sscanf(page, "%u", &new_value) != 1)
676 fsi->state->checkreqprot = new_value ? 1 : 0;
682 static const struct file_operations sel_checkreqprot_ops = {
683 .read = sel_read_checkreqprot,
684 .write = sel_write_checkreqprot,
685 .llseek = generic_file_llseek,
688 static ssize_t sel_write_validatetrans(struct file *file,
689 const char __user *buf,
690 size_t count, loff_t *ppos)
692 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
693 struct selinux_state *state = fsi->state;
694 char *oldcon = NULL, *newcon = NULL, *taskcon = NULL;
696 u32 osid, nsid, tsid;
700 rc = avc_has_perm(&selinux_state,
701 current_sid(), SECINITSID_SECURITY,
702 SECCLASS_SECURITY, SECURITY__VALIDATE_TRANS, NULL);
707 if (count >= PAGE_SIZE)
710 /* No partial writes. */
715 req = memdup_user_nul(buf, count);
723 oldcon = kzalloc(count + 1, GFP_KERNEL);
727 newcon = kzalloc(count + 1, GFP_KERNEL);
731 taskcon = kzalloc(count + 1, GFP_KERNEL);
736 if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4)
739 rc = security_context_str_to_sid(state, oldcon, &osid, GFP_KERNEL);
743 rc = security_context_str_to_sid(state, newcon, &nsid, GFP_KERNEL);
747 rc = security_context_str_to_sid(state, taskcon, &tsid, GFP_KERNEL);
751 rc = security_validate_transition_user(state, osid, nsid, tsid, tclass);
762 static const struct file_operations sel_transition_ops = {
763 .write = sel_write_validatetrans,
764 .llseek = generic_file_llseek,
768 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
770 static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
771 static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
772 static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
773 static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
774 static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
776 static ssize_t (*write_op[])(struct file *, char *, size_t) = {
777 [SEL_ACCESS] = sel_write_access,
778 [SEL_CREATE] = sel_write_create,
779 [SEL_RELABEL] = sel_write_relabel,
780 [SEL_USER] = sel_write_user,
781 [SEL_MEMBER] = sel_write_member,
782 [SEL_CONTEXT] = sel_write_context,
785 static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
787 ino_t ino = file_inode(file)->i_ino;
791 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
794 data = simple_transaction_get(file, buf, size);
796 return PTR_ERR(data);
798 rv = write_op[ino](file, data, size);
800 simple_transaction_set(file, rv);
806 static const struct file_operations transaction_ops = {
807 .write = selinux_transaction_write,
808 .read = simple_transaction_read,
809 .release = simple_transaction_release,
810 .llseek = generic_file_llseek,
814 * payload - write methods
815 * If the method has a response, the response should be put in buf,
816 * and the length returned. Otherwise return 0 or and -error.
819 static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
821 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
822 struct selinux_state *state = fsi->state;
823 char *scon = NULL, *tcon = NULL;
826 struct av_decision avd;
829 length = avc_has_perm(&selinux_state,
830 current_sid(), SECINITSID_SECURITY,
831 SECCLASS_SECURITY, SECURITY__COMPUTE_AV, NULL);
836 scon = kzalloc(size + 1, GFP_KERNEL);
841 tcon = kzalloc(size + 1, GFP_KERNEL);
846 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
849 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
853 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
857 security_compute_av_user(state, ssid, tsid, tclass, &avd);
859 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
861 avd.allowed, 0xffffffff,
862 avd.auditallow, avd.auditdeny,
863 avd.seqno, avd.flags);
870 static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
872 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
873 struct selinux_state *state = fsi->state;
874 char *scon = NULL, *tcon = NULL;
875 char *namebuf = NULL, *objname = NULL;
876 u32 ssid, tsid, newsid;
883 length = avc_has_perm(&selinux_state,
884 current_sid(), SECINITSID_SECURITY,
885 SECCLASS_SECURITY, SECURITY__COMPUTE_CREATE,
891 scon = kzalloc(size + 1, GFP_KERNEL);
896 tcon = kzalloc(size + 1, GFP_KERNEL);
901 namebuf = kzalloc(size + 1, GFP_KERNEL);
906 nargs = sscanf(buf, "%s %s %hu %s", scon, tcon, &tclass, namebuf);
907 if (nargs < 3 || nargs > 4)
911 * If and when the name of new object to be queried contains
912 * either whitespace or multibyte characters, they shall be
913 * encoded based on the percentage-encoding rule.
914 * If not encoded, the sscanf logic picks up only left-half
915 * of the supplied name; splitted by a whitespace unexpectedly.
925 else if (c1 == '%') {
926 c1 = hex_to_bin(*r++);
929 c2 = hex_to_bin(*r++);
935 } while (c1 != '\0');
940 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
944 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
948 length = security_transition_sid_user(state, ssid, tsid, tclass,
953 length = security_sid_to_context(state, newsid, &newcon, &len);
958 if (len > SIMPLE_TRANSACTION_LIMIT) {
959 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
960 "payload max\n", __func__, len);
964 memcpy(buf, newcon, len);
974 static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
976 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
977 struct selinux_state *state = fsi->state;
978 char *scon = NULL, *tcon = NULL;
979 u32 ssid, tsid, newsid;
985 length = avc_has_perm(&selinux_state,
986 current_sid(), SECINITSID_SECURITY,
987 SECCLASS_SECURITY, SECURITY__COMPUTE_RELABEL,
993 scon = kzalloc(size + 1, GFP_KERNEL);
998 tcon = kzalloc(size + 1, GFP_KERNEL);
1003 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
1006 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
1010 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
1014 length = security_change_sid(state, ssid, tsid, tclass, &newsid);
1018 length = security_sid_to_context(state, newsid, &newcon, &len);
1023 if (len > SIMPLE_TRANSACTION_LIMIT)
1026 memcpy(buf, newcon, len);
1035 static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
1037 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1038 struct selinux_state *state = fsi->state;
1039 char *con = NULL, *user = NULL, *ptr;
1040 u32 sid, *sids = NULL;
1046 length = avc_has_perm(&selinux_state,
1047 current_sid(), SECINITSID_SECURITY,
1048 SECCLASS_SECURITY, SECURITY__COMPUTE_USER,
1054 con = kzalloc(size + 1, GFP_KERNEL);
1059 user = kzalloc(size + 1, GFP_KERNEL);
1064 if (sscanf(buf, "%s %s", con, user) != 2)
1067 length = security_context_str_to_sid(state, con, &sid, GFP_KERNEL);
1071 length = security_get_user_sids(state, sid, user, &sids, &nsids);
1075 length = sprintf(buf, "%u", nsids) + 1;
1077 for (i = 0; i < nsids; i++) {
1078 rc = security_sid_to_context(state, sids[i], &newcon, &len);
1083 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
1088 memcpy(ptr, newcon, len);
1100 static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
1102 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1103 struct selinux_state *state = fsi->state;
1104 char *scon = NULL, *tcon = NULL;
1105 u32 ssid, tsid, newsid;
1108 char *newcon = NULL;
1111 length = avc_has_perm(&selinux_state,
1112 current_sid(), SECINITSID_SECURITY,
1113 SECCLASS_SECURITY, SECURITY__COMPUTE_MEMBER,
1119 scon = kzalloc(size + 1, GFP_KERNEL);
1124 tcon = kzalloc(size + 1, GFP_KERNEL);
1129 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
1132 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
1136 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
1140 length = security_member_sid(state, ssid, tsid, tclass, &newsid);
1144 length = security_sid_to_context(state, newsid, &newcon, &len);
1149 if (len > SIMPLE_TRANSACTION_LIMIT) {
1150 printk(KERN_ERR "SELinux: %s: context size (%u) exceeds "
1151 "payload max\n", __func__, len);
1155 memcpy(buf, newcon, len);
1164 static struct inode *sel_make_inode(struct super_block *sb, int mode)
1166 struct inode *ret = new_inode(sb);
1170 ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret);
1175 static ssize_t sel_read_bool(struct file *filep, char __user *buf,
1176 size_t count, loff_t *ppos)
1178 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
1183 unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
1184 const char *name = filep->f_path.dentry->d_name.name;
1186 mutex_lock(&fsi->mutex);
1189 if (index >= fsi->bool_num || strcmp(name,
1190 fsi->bool_pending_names[index]))
1194 page = (char *)get_zeroed_page(GFP_KERNEL);
1198 cur_enforcing = security_get_bool_value(fsi->state, index);
1199 if (cur_enforcing < 0) {
1200 ret = cur_enforcing;
1203 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
1204 fsi->bool_pending_values[index]);
1205 ret = simple_read_from_buffer(buf, count, ppos, page, length);
1207 mutex_unlock(&fsi->mutex);
1208 free_page((unsigned long)page);
1212 static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
1213 size_t count, loff_t *ppos)
1215 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
1219 unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
1220 const char *name = filep->f_path.dentry->d_name.name;
1222 mutex_lock(&fsi->mutex);
1224 length = avc_has_perm(&selinux_state,
1225 current_sid(), SECINITSID_SECURITY,
1226 SECCLASS_SECURITY, SECURITY__SETBOOL,
1232 if (index >= fsi->bool_num || strcmp(name,
1233 fsi->bool_pending_names[index]))
1237 if (count >= PAGE_SIZE)
1240 /* No partial writes. */
1245 page = memdup_user_nul(buf, count);
1247 length = PTR_ERR(page);
1253 if (sscanf(page, "%d", &new_value) != 1)
1259 fsi->bool_pending_values[index] = new_value;
1263 mutex_unlock(&fsi->mutex);
1268 static const struct file_operations sel_bool_ops = {
1269 .read = sel_read_bool,
1270 .write = sel_write_bool,
1271 .llseek = generic_file_llseek,
1274 static ssize_t sel_commit_bools_write(struct file *filep,
1275 const char __user *buf,
1276 size_t count, loff_t *ppos)
1278 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
1283 mutex_lock(&fsi->mutex);
1285 length = avc_has_perm(&selinux_state,
1286 current_sid(), SECINITSID_SECURITY,
1287 SECCLASS_SECURITY, SECURITY__SETBOOL,
1293 if (count >= PAGE_SIZE)
1296 /* No partial writes. */
1301 page = memdup_user_nul(buf, count);
1303 length = PTR_ERR(page);
1309 if (sscanf(page, "%d", &new_value) != 1)
1313 if (new_value && fsi->bool_pending_values)
1314 length = security_set_bools(fsi->state, fsi->bool_num,
1315 fsi->bool_pending_values);
1321 mutex_unlock(&fsi->mutex);
1326 static const struct file_operations sel_commit_bools_ops = {
1327 .write = sel_commit_bools_write,
1328 .llseek = generic_file_llseek,
1331 static void sel_remove_entries(struct dentry *de)
1334 shrink_dcache_parent(de);
1337 #define BOOL_DIR_NAME "booleans"
1339 static int sel_make_bools(struct selinux_fs_info *fsi)
1343 struct dentry *dentry = NULL;
1344 struct dentry *dir = fsi->bool_dir;
1345 struct inode *inode = NULL;
1346 struct inode_security_struct *isec;
1347 char **names = NULL, *page;
1352 /* remove any existing files */
1353 for (i = 0; i < fsi->bool_num; i++)
1354 kfree(fsi->bool_pending_names[i]);
1355 kfree(fsi->bool_pending_names);
1356 kfree(fsi->bool_pending_values);
1358 fsi->bool_pending_names = NULL;
1359 fsi->bool_pending_values = NULL;
1361 sel_remove_entries(dir);
1364 page = (char *)get_zeroed_page(GFP_KERNEL);
1368 ret = security_get_bools(fsi->state, &num, &names, &values);
1372 for (i = 0; i < num; i++) {
1374 dentry = d_alloc_name(dir, names[i]);
1379 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1383 ret = -ENAMETOOLONG;
1384 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
1385 if (len >= PAGE_SIZE)
1388 isec = (struct inode_security_struct *)inode->i_security;
1389 ret = security_genfs_sid(fsi->state, "selinuxfs", page,
1390 SECCLASS_FILE, &sid);
1392 pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n",
1394 sid = SECINITSID_SECURITY;
1398 isec->initialized = LABEL_INITIALIZED;
1399 inode->i_fop = &sel_bool_ops;
1400 inode->i_ino = i|SEL_BOOL_INO_OFFSET;
1401 d_add(dentry, inode);
1403 fsi->bool_num = num;
1404 fsi->bool_pending_names = names;
1405 fsi->bool_pending_values = values;
1407 free_page((unsigned long)page);
1410 free_page((unsigned long)page);
1413 for (i = 0; i < num; i++)
1418 sel_remove_entries(dir);
1423 static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1424 size_t count, loff_t *ppos)
1426 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
1427 struct selinux_state *state = fsi->state;
1428 char tmpbuf[TMPBUFLEN];
1431 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1432 avc_get_cache_threshold(state->avc));
1433 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1436 static ssize_t sel_write_avc_cache_threshold(struct file *file,
1437 const char __user *buf,
1438 size_t count, loff_t *ppos)
1441 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1442 struct selinux_state *state = fsi->state;
1445 unsigned int new_value;
1447 ret = avc_has_perm(&selinux_state,
1448 current_sid(), SECINITSID_SECURITY,
1449 SECCLASS_SECURITY, SECURITY__SETSECPARAM,
1454 if (count >= PAGE_SIZE)
1457 /* No partial writes. */
1461 page = memdup_user_nul(buf, count);
1463 return PTR_ERR(page);
1466 if (sscanf(page, "%u", &new_value) != 1)
1469 avc_set_cache_threshold(state->avc, new_value);
1477 static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1478 size_t count, loff_t *ppos)
1480 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
1481 struct selinux_state *state = fsi->state;
1485 page = (char *)__get_free_page(GFP_KERNEL);
1489 length = avc_get_hash_stats(state->avc, page);
1491 length = simple_read_from_buffer(buf, count, ppos, page, length);
1492 free_page((unsigned long)page);
1497 static const struct file_operations sel_avc_cache_threshold_ops = {
1498 .read = sel_read_avc_cache_threshold,
1499 .write = sel_write_avc_cache_threshold,
1500 .llseek = generic_file_llseek,
1503 static const struct file_operations sel_avc_hash_stats_ops = {
1504 .read = sel_read_avc_hash_stats,
1505 .llseek = generic_file_llseek,
1508 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1509 static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1513 for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
1514 if (!cpu_possible(cpu))
1517 return &per_cpu(avc_cache_stats, cpu);
1522 static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1524 loff_t n = *pos - 1;
1527 return SEQ_START_TOKEN;
1529 return sel_avc_get_stat_idx(&n);
1532 static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1534 return sel_avc_get_stat_idx(pos);
1537 static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1539 struct avc_cache_stats *st = v;
1541 if (v == SEQ_START_TOKEN) {
1543 "lookups hits misses allocations reclaims frees\n");
1545 unsigned int lookups = st->lookups;
1546 unsigned int misses = st->misses;
1547 unsigned int hits = lookups - misses;
1548 seq_printf(seq, "%u %u %u %u %u %u\n", lookups,
1549 hits, misses, st->allocations,
1550 st->reclaims, st->frees);
1555 static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1558 static const struct seq_operations sel_avc_cache_stats_seq_ops = {
1559 .start = sel_avc_stats_seq_start,
1560 .next = sel_avc_stats_seq_next,
1561 .show = sel_avc_stats_seq_show,
1562 .stop = sel_avc_stats_seq_stop,
1565 static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1567 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1570 static const struct file_operations sel_avc_cache_stats_ops = {
1571 .open = sel_open_avc_cache_stats,
1573 .llseek = seq_lseek,
1574 .release = seq_release,
1578 static int sel_make_avc_files(struct dentry *dir)
1580 struct super_block *sb = dir->d_sb;
1581 struct selinux_fs_info *fsi = sb->s_fs_info;
1583 static const struct tree_descr files[] = {
1584 { "cache_threshold",
1585 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1586 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1587 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1588 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1592 for (i = 0; i < ARRAY_SIZE(files); i++) {
1593 struct inode *inode;
1594 struct dentry *dentry;
1596 dentry = d_alloc_name(dir, files[i].name);
1600 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1604 inode->i_fop = files[i].ops;
1605 inode->i_ino = ++fsi->last_ino;
1606 d_add(dentry, inode);
1612 static ssize_t sel_read_initcon(struct file *file, char __user *buf,
1613 size_t count, loff_t *ppos)
1615 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1620 sid = file_inode(file)->i_ino&SEL_INO_MASK;
1621 ret = security_sid_to_context(fsi->state, sid, &con, &len);
1625 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1630 static const struct file_operations sel_initcon_ops = {
1631 .read = sel_read_initcon,
1632 .llseek = generic_file_llseek,
1635 static int sel_make_initcon_files(struct dentry *dir)
1639 for (i = 1; i <= SECINITSID_NUM; i++) {
1640 struct inode *inode;
1641 struct dentry *dentry;
1642 dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
1646 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1650 inode->i_fop = &sel_initcon_ops;
1651 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1652 d_add(dentry, inode);
1658 static inline unsigned long sel_class_to_ino(u16 class)
1660 return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1663 static inline u16 sel_ino_to_class(unsigned long ino)
1665 return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1);
1668 static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1670 return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1673 static inline u32 sel_ino_to_perm(unsigned long ino)
1675 return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1678 static ssize_t sel_read_class(struct file *file, char __user *buf,
1679 size_t count, loff_t *ppos)
1681 unsigned long ino = file_inode(file)->i_ino;
1682 char res[TMPBUFLEN];
1683 ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_class(ino));
1684 return simple_read_from_buffer(buf, count, ppos, res, len);
1687 static const struct file_operations sel_class_ops = {
1688 .read = sel_read_class,
1689 .llseek = generic_file_llseek,
1692 static ssize_t sel_read_perm(struct file *file, char __user *buf,
1693 size_t count, loff_t *ppos)
1695 unsigned long ino = file_inode(file)->i_ino;
1696 char res[TMPBUFLEN];
1697 ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino));
1698 return simple_read_from_buffer(buf, count, ppos, res, len);
1701 static const struct file_operations sel_perm_ops = {
1702 .read = sel_read_perm,
1703 .llseek = generic_file_llseek,
1706 static ssize_t sel_read_policycap(struct file *file, char __user *buf,
1707 size_t count, loff_t *ppos)
1709 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1711 char tmpbuf[TMPBUFLEN];
1713 unsigned long i_ino = file_inode(file)->i_ino;
1715 value = security_policycap_supported(fsi->state, i_ino & SEL_INO_MASK);
1716 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
1718 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1721 static const struct file_operations sel_policycap_ops = {
1722 .read = sel_read_policycap,
1723 .llseek = generic_file_llseek,
1726 static int sel_make_perm_files(char *objclass, int classvalue,
1729 struct selinux_fs_info *fsi = dir->d_sb->s_fs_info;
1733 rc = security_get_permissions(fsi->state, objclass, &perms, &nperms);
1737 for (i = 0; i < nperms; i++) {
1738 struct inode *inode;
1739 struct dentry *dentry;
1742 dentry = d_alloc_name(dir, perms[i]);
1747 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1751 inode->i_fop = &sel_perm_ops;
1752 /* i+1 since perm values are 1-indexed */
1753 inode->i_ino = sel_perm_to_ino(classvalue, i + 1);
1754 d_add(dentry, inode);
1758 for (i = 0; i < nperms; i++)
1764 static int sel_make_class_dir_entries(char *classname, int index,
1767 struct super_block *sb = dir->d_sb;
1768 struct selinux_fs_info *fsi = sb->s_fs_info;
1769 struct dentry *dentry = NULL;
1770 struct inode *inode = NULL;
1773 dentry = d_alloc_name(dir, "index");
1777 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1781 inode->i_fop = &sel_class_ops;
1782 inode->i_ino = sel_class_to_ino(index);
1783 d_add(dentry, inode);
1785 dentry = sel_make_dir(dir, "perms", &fsi->last_class_ino);
1787 return PTR_ERR(dentry);
1789 rc = sel_make_perm_files(classname, index, dentry);
1794 static int sel_make_classes(struct selinux_fs_info *fsi)
1797 int rc, nclasses, i;
1800 /* delete any existing entries */
1801 sel_remove_entries(fsi->class_dir);
1803 rc = security_get_classes(fsi->state, &classes, &nclasses);
1807 /* +2 since classes are 1-indexed */
1808 fsi->last_class_ino = sel_class_to_ino(nclasses + 2);
1810 for (i = 0; i < nclasses; i++) {
1811 struct dentry *class_name_dir;
1813 class_name_dir = sel_make_dir(fsi->class_dir, classes[i],
1814 &fsi->last_class_ino);
1815 if (IS_ERR(class_name_dir)) {
1816 rc = PTR_ERR(class_name_dir);
1820 /* i+1 since class values are 1-indexed */
1821 rc = sel_make_class_dir_entries(classes[i], i + 1,
1828 for (i = 0; i < nclasses; i++)
1834 static int sel_make_policycap(struct selinux_fs_info *fsi)
1837 struct dentry *dentry = NULL;
1838 struct inode *inode = NULL;
1840 sel_remove_entries(fsi->policycap_dir);
1842 for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
1843 if (iter < ARRAY_SIZE(selinux_policycap_names))
1844 dentry = d_alloc_name(fsi->policycap_dir,
1845 selinux_policycap_names[iter]);
1847 dentry = d_alloc_name(fsi->policycap_dir, "unknown");
1852 inode = sel_make_inode(fsi->sb, S_IFREG | 0444);
1856 inode->i_fop = &sel_policycap_ops;
1857 inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
1858 d_add(dentry, inode);
1864 static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
1867 struct dentry *dentry = d_alloc_name(dir, name);
1868 struct inode *inode;
1871 return ERR_PTR(-ENOMEM);
1873 inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1876 return ERR_PTR(-ENOMEM);
1879 inode->i_op = &simple_dir_inode_operations;
1880 inode->i_fop = &simple_dir_operations;
1881 inode->i_ino = ++(*ino);
1882 /* directory inodes start off with i_nlink == 2 (for "." entry) */
1884 d_add(dentry, inode);
1885 /* bump link count on parent directory, too */
1886 inc_nlink(d_inode(dir));
1891 #define NULL_FILE_NAME "null"
1893 static int sel_fill_super(struct super_block *sb, void *data, int silent)
1895 struct selinux_fs_info *fsi;
1897 struct dentry *dentry;
1898 struct inode *inode;
1899 struct inode_security_struct *isec;
1901 static const struct tree_descr selinux_files[] = {
1902 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1903 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
1904 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
1905 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1906 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1907 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1908 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1909 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1910 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1911 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1912 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1913 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1914 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
1915 [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
1916 [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
1917 [SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO},
1918 [SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO},
1919 [SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops,
1924 ret = selinux_fs_info_create(sb);
1928 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1932 fsi = sb->s_fs_info;
1933 fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino);
1934 if (IS_ERR(fsi->bool_dir)) {
1935 ret = PTR_ERR(fsi->bool_dir);
1936 fsi->bool_dir = NULL;
1941 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
1946 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
1950 inode->i_ino = ++fsi->last_ino;
1951 isec = (struct inode_security_struct *)inode->i_security;
1952 isec->sid = SECINITSID_DEVNULL;
1953 isec->sclass = SECCLASS_CHR_FILE;
1954 isec->initialized = LABEL_INITIALIZED;
1956 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1957 d_add(dentry, inode);
1959 dentry = sel_make_dir(sb->s_root, "avc", &fsi->last_ino);
1960 if (IS_ERR(dentry)) {
1961 ret = PTR_ERR(dentry);
1965 ret = sel_make_avc_files(dentry);
1969 dentry = sel_make_dir(sb->s_root, "initial_contexts", &fsi->last_ino);
1970 if (IS_ERR(dentry)) {
1971 ret = PTR_ERR(dentry);
1975 ret = sel_make_initcon_files(dentry);
1979 fsi->class_dir = sel_make_dir(sb->s_root, "class", &fsi->last_ino);
1980 if (IS_ERR(fsi->class_dir)) {
1981 ret = PTR_ERR(fsi->class_dir);
1982 fsi->class_dir = NULL;
1986 fsi->policycap_dir = sel_make_dir(sb->s_root, "policy_capabilities",
1988 if (IS_ERR(fsi->policycap_dir)) {
1989 ret = PTR_ERR(fsi->policycap_dir);
1990 fsi->policycap_dir = NULL;
1994 ret = sel_make_policy_nodes(fsi);
1999 printk(KERN_ERR "SELinux: %s: failed while creating inodes\n",
2002 selinux_fs_info_free(sb);
2007 static struct dentry *sel_mount(struct file_system_type *fs_type,
2008 int flags, const char *dev_name, void *data)
2010 return mount_single(fs_type, flags, data, sel_fill_super);
2013 static void sel_kill_sb(struct super_block *sb)
2015 selinux_fs_info_free(sb);
2016 kill_litter_super(sb);
2019 static struct file_system_type sel_fs_type = {
2020 .name = "selinuxfs",
2022 .kill_sb = sel_kill_sb,
2025 struct vfsmount *selinuxfs_mount;
2026 struct path selinux_null;
2028 static int __init init_sel_fs(void)
2030 struct qstr null_name = QSTR_INIT(NULL_FILE_NAME,
2031 sizeof(NULL_FILE_NAME)-1);
2034 if (!selinux_enabled)
2037 err = sysfs_create_mount_point(fs_kobj, "selinux");
2041 err = register_filesystem(&sel_fs_type);
2043 sysfs_remove_mount_point(fs_kobj, "selinux");
2047 selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type);
2048 if (IS_ERR(selinuxfs_mount)) {
2049 printk(KERN_ERR "selinuxfs: could not mount!\n");
2050 err = PTR_ERR(selinuxfs_mount);
2051 selinuxfs_mount = NULL;
2053 selinux_null.dentry = d_hash_and_lookup(selinux_null.mnt->mnt_root,
2055 if (IS_ERR(selinux_null.dentry)) {
2056 pr_err("selinuxfs: could not lookup null!\n");
2057 err = PTR_ERR(selinux_null.dentry);
2058 selinux_null.dentry = NULL;
2064 __initcall(init_sel_fs);
2066 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
2067 void exit_sel_fs(void)
2069 sysfs_remove_mount_point(fs_kobj, "selinux");
2070 dput(selinux_null.dentry);
2071 kern_unmount(selinuxfs_mount);
2072 unregister_filesystem(&sel_fs_type);