1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Updated: Karl MacMillan <kmacmillan@tresys.com>
4 * Added conditional policy language extensions
6 * Updated: Hewlett-Packard <paul@paul-moore.com>
8 * Added support for the policy capability bitmap
10 * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
11 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
12 * Copyright (C) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
15 #include <linux/kernel.h>
16 #include <linux/pagemap.h>
17 #include <linux/slab.h>
18 #include <linux/vmalloc.h>
20 #include <linux/mount.h>
21 #include <linux/mutex.h>
22 #include <linux/init.h>
23 #include <linux/string.h>
24 #include <linux/security.h>
25 #include <linux/major.h>
26 #include <linux/seq_file.h>
27 #include <linux/percpu.h>
28 #include <linux/audit.h>
29 #include <linux/uaccess.h>
30 #include <linux/kobject.h>
31 #include <linux/ctype.h>
33 /* selinuxfs pseudo filesystem for exporting the security policy API.
34 Based on the proc code and the fs/nfsd/nfsctl.c code. */
41 #include "conditional.h"
45 SEL_LOAD, /* load policy */
46 SEL_ENFORCE, /* get or set enforcing status */
47 SEL_CONTEXT, /* validate context */
48 SEL_ACCESS, /* compute access decision */
49 SEL_CREATE, /* compute create labeling decision */
50 SEL_RELABEL, /* compute relabeling decision */
51 SEL_USER, /* compute reachable user contexts */
52 SEL_POLICYVERS, /* return policy version for this kernel */
53 SEL_COMMIT_BOOLS, /* commit new boolean values */
54 SEL_MLS, /* return if MLS policy is enabled */
55 SEL_DISABLE, /* disable SELinux until next reboot */
56 SEL_MEMBER, /* compute polyinstantiation membership decision */
57 SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
58 SEL_COMPAT_NET, /* whether to use old compat network packet controls */
59 SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */
60 SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */
61 SEL_STATUS, /* export current status using mmap() */
62 SEL_POLICY, /* allow userspace to read the in kernel policy */
63 SEL_VALIDATE_TRANS, /* compute validatetrans decision */
64 SEL_INO_NEXT, /* The next inode number to use */
67 struct selinux_fs_info {
68 struct dentry *bool_dir;
69 unsigned int bool_num;
70 char **bool_pending_names;
71 unsigned int *bool_pending_values;
72 struct dentry *class_dir;
73 unsigned long last_class_ino;
75 struct dentry *policycap_dir;
77 unsigned long last_ino;
78 struct selinux_state *state;
79 struct super_block *sb;
82 static int selinux_fs_info_create(struct super_block *sb)
84 struct selinux_fs_info *fsi;
86 fsi = kzalloc(sizeof(*fsi), GFP_KERNEL);
90 mutex_init(&fsi->mutex);
91 fsi->last_ino = SEL_INO_NEXT - 1;
92 fsi->state = &selinux_state;
98 static void selinux_fs_info_free(struct super_block *sb)
100 struct selinux_fs_info *fsi = sb->s_fs_info;
104 for (i = 0; i < fsi->bool_num; i++)
105 kfree(fsi->bool_pending_names[i]);
106 kfree(fsi->bool_pending_names);
107 kfree(fsi->bool_pending_values);
109 kfree(sb->s_fs_info);
110 sb->s_fs_info = NULL;
113 #define SEL_INITCON_INO_OFFSET 0x01000000
114 #define SEL_BOOL_INO_OFFSET 0x02000000
115 #define SEL_CLASS_INO_OFFSET 0x04000000
116 #define SEL_POLICYCAP_INO_OFFSET 0x08000000
117 #define SEL_INO_MASK 0x00ffffff
120 static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
121 size_t count, loff_t *ppos)
123 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
124 char tmpbuf[TMPBUFLEN];
127 length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
128 enforcing_enabled(fsi->state));
129 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
132 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
133 static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
134 size_t count, loff_t *ppos)
137 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
138 struct selinux_state *state = fsi->state;
141 int old_value, new_value;
143 if (count >= PAGE_SIZE)
146 /* No partial writes. */
150 page = memdup_user_nul(buf, count);
152 return PTR_ERR(page);
155 if (sscanf(page, "%d", &new_value) != 1)
158 new_value = !!new_value;
160 old_value = enforcing_enabled(state);
161 if (new_value != old_value) {
162 length = avc_has_perm(&selinux_state,
163 current_sid(), SECINITSID_SECURITY,
164 SECCLASS_SECURITY, SECURITY__SETENFORCE,
168 audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS,
169 "enforcing=%d old_enforcing=%d auid=%u ses=%u"
170 " enabled=%d old-enabled=%d lsm=selinux res=1",
171 new_value, old_value,
172 from_kuid(&init_user_ns, audit_get_loginuid(current)),
173 audit_get_sessionid(current),
174 selinux_enabled, selinux_enabled);
175 enforcing_set(state, new_value);
177 avc_ss_reset(state->avc, 0);
178 selnl_notify_setenforce(new_value);
179 selinux_status_update_setenforce(state, new_value);
181 call_blocking_lsm_notifier(LSM_POLICY_CHANGE, NULL);
189 #define sel_write_enforce NULL
192 static const struct file_operations sel_enforce_ops = {
193 .read = sel_read_enforce,
194 .write = sel_write_enforce,
195 .llseek = generic_file_llseek,
198 static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf,
199 size_t count, loff_t *ppos)
201 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
202 struct selinux_state *state = fsi->state;
203 char tmpbuf[TMPBUFLEN];
205 ino_t ino = file_inode(filp)->i_ino;
206 int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
207 security_get_reject_unknown(state) :
208 !security_get_allow_unknown(state);
210 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
211 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
214 static const struct file_operations sel_handle_unknown_ops = {
215 .read = sel_read_handle_unknown,
216 .llseek = generic_file_llseek,
219 static int sel_open_handle_status(struct inode *inode, struct file *filp)
221 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
222 struct page *status = selinux_kernel_status_page(fsi->state);
227 filp->private_data = status;
232 static ssize_t sel_read_handle_status(struct file *filp, char __user *buf,
233 size_t count, loff_t *ppos)
235 struct page *status = filp->private_data;
239 return simple_read_from_buffer(buf, count, ppos,
240 page_address(status),
241 sizeof(struct selinux_kernel_status));
244 static int sel_mmap_handle_status(struct file *filp,
245 struct vm_area_struct *vma)
247 struct page *status = filp->private_data;
248 unsigned long size = vma->vm_end - vma->vm_start;
252 /* only allows one page from the head */
253 if (vma->vm_pgoff > 0 || size != PAGE_SIZE)
255 /* disallow writable mapping */
256 if (vma->vm_flags & VM_WRITE)
258 /* disallow mprotect() turns it into writable */
259 vma->vm_flags &= ~VM_MAYWRITE;
261 return remap_pfn_range(vma, vma->vm_start,
263 size, vma->vm_page_prot);
266 static const struct file_operations sel_handle_status_ops = {
267 .open = sel_open_handle_status,
268 .read = sel_read_handle_status,
269 .mmap = sel_mmap_handle_status,
270 .llseek = generic_file_llseek,
273 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
274 static ssize_t sel_write_disable(struct file *file, const char __user *buf,
275 size_t count, loff_t *ppos)
278 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
284 if (count >= PAGE_SIZE)
287 /* No partial writes. */
291 page = memdup_user_nul(buf, count);
293 return PTR_ERR(page);
296 if (sscanf(page, "%d", &new_value) != 1)
300 enforcing = enforcing_enabled(fsi->state);
301 length = selinux_disable(fsi->state);
304 audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS,
305 "enforcing=%d old_enforcing=%d auid=%u ses=%u"
306 " enabled=%d old-enabled=%d lsm=selinux res=1",
307 enforcing, enforcing,
308 from_kuid(&init_user_ns, audit_get_loginuid(current)),
309 audit_get_sessionid(current), 0, 1);
318 #define sel_write_disable NULL
321 static const struct file_operations sel_disable_ops = {
322 .write = sel_write_disable,
323 .llseek = generic_file_llseek,
326 static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
327 size_t count, loff_t *ppos)
329 char tmpbuf[TMPBUFLEN];
332 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
333 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
336 static const struct file_operations sel_policyvers_ops = {
337 .read = sel_read_policyvers,
338 .llseek = generic_file_llseek,
341 /* declaration for sel_write_load */
342 static int sel_make_bools(struct selinux_fs_info *fsi);
343 static int sel_make_classes(struct selinux_fs_info *fsi);
344 static int sel_make_policycap(struct selinux_fs_info *fsi);
346 /* declaration for sel_make_class_dirs */
347 static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
350 static ssize_t sel_read_mls(struct file *filp, char __user *buf,
351 size_t count, loff_t *ppos)
353 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
354 char tmpbuf[TMPBUFLEN];
357 length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
358 security_mls_enabled(fsi->state));
359 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
362 static const struct file_operations sel_mls_ops = {
363 .read = sel_read_mls,
364 .llseek = generic_file_llseek,
367 struct policy_load_memory {
372 static int sel_open_policy(struct inode *inode, struct file *filp)
374 struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
375 struct selinux_state *state = fsi->state;
376 struct policy_load_memory *plm = NULL;
379 BUG_ON(filp->private_data);
381 mutex_lock(&fsi->mutex);
383 rc = avc_has_perm(&selinux_state,
384 current_sid(), SECINITSID_SECURITY,
385 SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
390 if (fsi->policy_opened)
394 plm = kzalloc(sizeof(*plm), GFP_KERNEL);
398 if (i_size_read(inode) != security_policydb_len(state)) {
400 i_size_write(inode, security_policydb_len(state));
404 rc = security_read_policy(state, &plm->data, &plm->len);
408 fsi->policy_opened = 1;
410 filp->private_data = plm;
412 mutex_unlock(&fsi->mutex);
416 mutex_unlock(&fsi->mutex);
424 static int sel_release_policy(struct inode *inode, struct file *filp)
426 struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
427 struct policy_load_memory *plm = filp->private_data;
431 fsi->policy_opened = 0;
439 static ssize_t sel_read_policy(struct file *filp, char __user *buf,
440 size_t count, loff_t *ppos)
442 struct policy_load_memory *plm = filp->private_data;
445 ret = avc_has_perm(&selinux_state,
446 current_sid(), SECINITSID_SECURITY,
447 SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
451 return simple_read_from_buffer(buf, count, ppos, plm->data, plm->len);
454 static vm_fault_t sel_mmap_policy_fault(struct vm_fault *vmf)
456 struct policy_load_memory *plm = vmf->vma->vm_file->private_data;
457 unsigned long offset;
460 if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE))
461 return VM_FAULT_SIGBUS;
463 offset = vmf->pgoff << PAGE_SHIFT;
464 if (offset >= roundup(plm->len, PAGE_SIZE))
465 return VM_FAULT_SIGBUS;
467 page = vmalloc_to_page(plm->data + offset);
475 static const struct vm_operations_struct sel_mmap_policy_ops = {
476 .fault = sel_mmap_policy_fault,
477 .page_mkwrite = sel_mmap_policy_fault,
480 static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma)
482 if (vma->vm_flags & VM_SHARED) {
483 /* do not allow mprotect to make mapping writable */
484 vma->vm_flags &= ~VM_MAYWRITE;
486 if (vma->vm_flags & VM_WRITE)
490 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
491 vma->vm_ops = &sel_mmap_policy_ops;
496 static const struct file_operations sel_policy_ops = {
497 .open = sel_open_policy,
498 .read = sel_read_policy,
499 .mmap = sel_mmap_policy,
500 .release = sel_release_policy,
501 .llseek = generic_file_llseek,
504 static int sel_make_policy_nodes(struct selinux_fs_info *fsi)
508 ret = sel_make_bools(fsi);
510 pr_err("SELinux: failed to load policy booleans\n");
514 ret = sel_make_classes(fsi);
516 pr_err("SELinux: failed to load policy classes\n");
520 ret = sel_make_policycap(fsi);
522 pr_err("SELinux: failed to load policy capabilities\n");
529 static ssize_t sel_write_load(struct file *file, const char __user *buf,
530 size_t count, loff_t *ppos)
533 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
537 mutex_lock(&fsi->mutex);
539 length = avc_has_perm(&selinux_state,
540 current_sid(), SECINITSID_SECURITY,
541 SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL);
545 /* No partial writes. */
551 if (count > 64 * 1024 * 1024)
555 data = vmalloc(count);
560 if (copy_from_user(data, buf, count) != 0)
563 length = security_load_policy(fsi->state, data, count);
565 pr_warn_ratelimited("SELinux: failed to load policy\n");
569 length = sel_make_policy_nodes(fsi);
576 audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
577 "auid=%u ses=%u lsm=selinux res=1",
578 from_kuid(&init_user_ns, audit_get_loginuid(current)),
579 audit_get_sessionid(current));
581 mutex_unlock(&fsi->mutex);
586 static const struct file_operations sel_load_ops = {
587 .write = sel_write_load,
588 .llseek = generic_file_llseek,
591 static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
593 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
594 struct selinux_state *state = fsi->state;
599 length = avc_has_perm(&selinux_state,
600 current_sid(), SECINITSID_SECURITY,
601 SECCLASS_SECURITY, SECURITY__CHECK_CONTEXT, NULL);
605 length = security_context_to_sid(state, buf, size, &sid, GFP_KERNEL);
609 length = security_sid_to_context(state, sid, &canon, &len);
614 if (len > SIMPLE_TRANSACTION_LIMIT) {
615 pr_err("SELinux: %s: context size (%u) exceeds "
616 "payload max\n", __func__, len);
620 memcpy(buf, canon, len);
627 static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
628 size_t count, loff_t *ppos)
630 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
631 char tmpbuf[TMPBUFLEN];
634 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", fsi->state->checkreqprot);
635 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
638 static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
639 size_t count, loff_t *ppos)
641 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
644 unsigned int new_value;
646 length = avc_has_perm(&selinux_state,
647 current_sid(), SECINITSID_SECURITY,
648 SECCLASS_SECURITY, SECURITY__SETCHECKREQPROT,
653 if (count >= PAGE_SIZE)
656 /* No partial writes. */
660 page = memdup_user_nul(buf, count);
662 return PTR_ERR(page);
665 if (sscanf(page, "%u", &new_value) != 1)
668 fsi->state->checkreqprot = new_value ? 1 : 0;
674 static const struct file_operations sel_checkreqprot_ops = {
675 .read = sel_read_checkreqprot,
676 .write = sel_write_checkreqprot,
677 .llseek = generic_file_llseek,
680 static ssize_t sel_write_validatetrans(struct file *file,
681 const char __user *buf,
682 size_t count, loff_t *ppos)
684 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
685 struct selinux_state *state = fsi->state;
686 char *oldcon = NULL, *newcon = NULL, *taskcon = NULL;
688 u32 osid, nsid, tsid;
692 rc = avc_has_perm(&selinux_state,
693 current_sid(), SECINITSID_SECURITY,
694 SECCLASS_SECURITY, SECURITY__VALIDATE_TRANS, NULL);
699 if (count >= PAGE_SIZE)
702 /* No partial writes. */
707 req = memdup_user_nul(buf, count);
715 oldcon = kzalloc(count + 1, GFP_KERNEL);
719 newcon = kzalloc(count + 1, GFP_KERNEL);
723 taskcon = kzalloc(count + 1, GFP_KERNEL);
728 if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4)
731 rc = security_context_str_to_sid(state, oldcon, &osid, GFP_KERNEL);
735 rc = security_context_str_to_sid(state, newcon, &nsid, GFP_KERNEL);
739 rc = security_context_str_to_sid(state, taskcon, &tsid, GFP_KERNEL);
743 rc = security_validate_transition_user(state, osid, nsid, tsid, tclass);
754 static const struct file_operations sel_transition_ops = {
755 .write = sel_write_validatetrans,
756 .llseek = generic_file_llseek,
760 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
762 static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
763 static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
764 static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
765 static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
766 static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
768 static ssize_t (*const write_op[])(struct file *, char *, size_t) = {
769 [SEL_ACCESS] = sel_write_access,
770 [SEL_CREATE] = sel_write_create,
771 [SEL_RELABEL] = sel_write_relabel,
772 [SEL_USER] = sel_write_user,
773 [SEL_MEMBER] = sel_write_member,
774 [SEL_CONTEXT] = sel_write_context,
777 static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
779 ino_t ino = file_inode(file)->i_ino;
783 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
786 data = simple_transaction_get(file, buf, size);
788 return PTR_ERR(data);
790 rv = write_op[ino](file, data, size);
792 simple_transaction_set(file, rv);
798 static const struct file_operations transaction_ops = {
799 .write = selinux_transaction_write,
800 .read = simple_transaction_read,
801 .release = simple_transaction_release,
802 .llseek = generic_file_llseek,
806 * payload - write methods
807 * If the method has a response, the response should be put in buf,
808 * and the length returned. Otherwise return 0 or and -error.
811 static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
813 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
814 struct selinux_state *state = fsi->state;
815 char *scon = NULL, *tcon = NULL;
818 struct av_decision avd;
821 length = avc_has_perm(&selinux_state,
822 current_sid(), SECINITSID_SECURITY,
823 SECCLASS_SECURITY, SECURITY__COMPUTE_AV, NULL);
828 scon = kzalloc(size + 1, GFP_KERNEL);
833 tcon = kzalloc(size + 1, GFP_KERNEL);
838 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
841 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
845 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
849 security_compute_av_user(state, ssid, tsid, tclass, &avd);
851 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
853 avd.allowed, 0xffffffff,
854 avd.auditallow, avd.auditdeny,
855 avd.seqno, avd.flags);
862 static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
864 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
865 struct selinux_state *state = fsi->state;
866 char *scon = NULL, *tcon = NULL;
867 char *namebuf = NULL, *objname = NULL;
868 u32 ssid, tsid, newsid;
875 length = avc_has_perm(&selinux_state,
876 current_sid(), SECINITSID_SECURITY,
877 SECCLASS_SECURITY, SECURITY__COMPUTE_CREATE,
883 scon = kzalloc(size + 1, GFP_KERNEL);
888 tcon = kzalloc(size + 1, GFP_KERNEL);
893 namebuf = kzalloc(size + 1, GFP_KERNEL);
898 nargs = sscanf(buf, "%s %s %hu %s", scon, tcon, &tclass, namebuf);
899 if (nargs < 3 || nargs > 4)
903 * If and when the name of new object to be queried contains
904 * either whitespace or multibyte characters, they shall be
905 * encoded based on the percentage-encoding rule.
906 * If not encoded, the sscanf logic picks up only left-half
907 * of the supplied name; splitted by a whitespace unexpectedly.
917 else if (c1 == '%') {
918 c1 = hex_to_bin(*r++);
921 c2 = hex_to_bin(*r++);
927 } while (c1 != '\0');
932 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
936 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
940 length = security_transition_sid_user(state, ssid, tsid, tclass,
945 length = security_sid_to_context(state, newsid, &newcon, &len);
950 if (len > SIMPLE_TRANSACTION_LIMIT) {
951 pr_err("SELinux: %s: context size (%u) exceeds "
952 "payload max\n", __func__, len);
956 memcpy(buf, newcon, len);
966 static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
968 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
969 struct selinux_state *state = fsi->state;
970 char *scon = NULL, *tcon = NULL;
971 u32 ssid, tsid, newsid;
977 length = avc_has_perm(&selinux_state,
978 current_sid(), SECINITSID_SECURITY,
979 SECCLASS_SECURITY, SECURITY__COMPUTE_RELABEL,
985 scon = kzalloc(size + 1, GFP_KERNEL);
990 tcon = kzalloc(size + 1, GFP_KERNEL);
995 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
998 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
1002 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
1006 length = security_change_sid(state, ssid, tsid, tclass, &newsid);
1010 length = security_sid_to_context(state, newsid, &newcon, &len);
1015 if (len > SIMPLE_TRANSACTION_LIMIT)
1018 memcpy(buf, newcon, len);
1027 static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
1029 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1030 struct selinux_state *state = fsi->state;
1031 char *con = NULL, *user = NULL, *ptr;
1032 u32 sid, *sids = NULL;
1038 length = avc_has_perm(&selinux_state,
1039 current_sid(), SECINITSID_SECURITY,
1040 SECCLASS_SECURITY, SECURITY__COMPUTE_USER,
1046 con = kzalloc(size + 1, GFP_KERNEL);
1051 user = kzalloc(size + 1, GFP_KERNEL);
1056 if (sscanf(buf, "%s %s", con, user) != 2)
1059 length = security_context_str_to_sid(state, con, &sid, GFP_KERNEL);
1063 length = security_get_user_sids(state, sid, user, &sids, &nsids);
1067 length = sprintf(buf, "%u", nsids) + 1;
1069 for (i = 0; i < nsids; i++) {
1070 rc = security_sid_to_context(state, sids[i], &newcon, &len);
1075 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
1080 memcpy(ptr, newcon, len);
1092 static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
1094 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1095 struct selinux_state *state = fsi->state;
1096 char *scon = NULL, *tcon = NULL;
1097 u32 ssid, tsid, newsid;
1100 char *newcon = NULL;
1103 length = avc_has_perm(&selinux_state,
1104 current_sid(), SECINITSID_SECURITY,
1105 SECCLASS_SECURITY, SECURITY__COMPUTE_MEMBER,
1111 scon = kzalloc(size + 1, GFP_KERNEL);
1116 tcon = kzalloc(size + 1, GFP_KERNEL);
1121 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
1124 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
1128 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
1132 length = security_member_sid(state, ssid, tsid, tclass, &newsid);
1136 length = security_sid_to_context(state, newsid, &newcon, &len);
1141 if (len > SIMPLE_TRANSACTION_LIMIT) {
1142 pr_err("SELinux: %s: context size (%u) exceeds "
1143 "payload max\n", __func__, len);
1147 memcpy(buf, newcon, len);
1156 static struct inode *sel_make_inode(struct super_block *sb, int mode)
1158 struct inode *ret = new_inode(sb);
1162 ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret);
1167 static ssize_t sel_read_bool(struct file *filep, char __user *buf,
1168 size_t count, loff_t *ppos)
1170 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
1175 unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
1176 const char *name = filep->f_path.dentry->d_name.name;
1178 mutex_lock(&fsi->mutex);
1181 if (index >= fsi->bool_num || strcmp(name,
1182 fsi->bool_pending_names[index]))
1186 page = (char *)get_zeroed_page(GFP_KERNEL);
1190 cur_enforcing = security_get_bool_value(fsi->state, index);
1191 if (cur_enforcing < 0) {
1192 ret = cur_enforcing;
1195 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
1196 fsi->bool_pending_values[index]);
1197 mutex_unlock(&fsi->mutex);
1198 ret = simple_read_from_buffer(buf, count, ppos, page, length);
1200 free_page((unsigned long)page);
1204 mutex_unlock(&fsi->mutex);
1208 static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
1209 size_t count, loff_t *ppos)
1211 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
1215 unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
1216 const char *name = filep->f_path.dentry->d_name.name;
1218 if (count >= PAGE_SIZE)
1221 /* No partial writes. */
1225 page = memdup_user_nul(buf, count);
1227 return PTR_ERR(page);
1229 mutex_lock(&fsi->mutex);
1231 length = avc_has_perm(&selinux_state,
1232 current_sid(), SECINITSID_SECURITY,
1233 SECCLASS_SECURITY, SECURITY__SETBOOL,
1239 if (index >= fsi->bool_num || strcmp(name,
1240 fsi->bool_pending_names[index]))
1244 if (sscanf(page, "%d", &new_value) != 1)
1250 fsi->bool_pending_values[index] = new_value;
1254 mutex_unlock(&fsi->mutex);
1259 static const struct file_operations sel_bool_ops = {
1260 .read = sel_read_bool,
1261 .write = sel_write_bool,
1262 .llseek = generic_file_llseek,
1265 static ssize_t sel_commit_bools_write(struct file *filep,
1266 const char __user *buf,
1267 size_t count, loff_t *ppos)
1269 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
1274 if (count >= PAGE_SIZE)
1277 /* No partial writes. */
1281 page = memdup_user_nul(buf, count);
1283 return PTR_ERR(page);
1285 mutex_lock(&fsi->mutex);
1287 length = avc_has_perm(&selinux_state,
1288 current_sid(), SECINITSID_SECURITY,
1289 SECCLASS_SECURITY, SECURITY__SETBOOL,
1295 if (sscanf(page, "%d", &new_value) != 1)
1299 if (new_value && fsi->bool_pending_values)
1300 length = security_set_bools(fsi->state, fsi->bool_num,
1301 fsi->bool_pending_values);
1307 mutex_unlock(&fsi->mutex);
1312 static const struct file_operations sel_commit_bools_ops = {
1313 .write = sel_commit_bools_write,
1314 .llseek = generic_file_llseek,
1317 static void sel_remove_entries(struct dentry *de)
1320 shrink_dcache_parent(de);
1323 #define BOOL_DIR_NAME "booleans"
1325 static int sel_make_bools(struct selinux_fs_info *fsi)
1329 struct dentry *dentry = NULL;
1330 struct dentry *dir = fsi->bool_dir;
1331 struct inode *inode = NULL;
1332 struct inode_security_struct *isec;
1333 char **names = NULL, *page;
1338 /* remove any existing files */
1339 for (i = 0; i < fsi->bool_num; i++)
1340 kfree(fsi->bool_pending_names[i]);
1341 kfree(fsi->bool_pending_names);
1342 kfree(fsi->bool_pending_values);
1344 fsi->bool_pending_names = NULL;
1345 fsi->bool_pending_values = NULL;
1347 sel_remove_entries(dir);
1350 page = (char *)get_zeroed_page(GFP_KERNEL);
1354 ret = security_get_bools(fsi->state, &num, &names, &values);
1358 for (i = 0; i < num; i++) {
1360 dentry = d_alloc_name(dir, names[i]);
1365 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1371 ret = -ENAMETOOLONG;
1372 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
1373 if (len >= PAGE_SIZE) {
1379 isec = selinux_inode(inode);
1380 ret = security_genfs_sid(fsi->state, "selinuxfs", page,
1381 SECCLASS_FILE, &sid);
1383 pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n",
1385 sid = SECINITSID_SECURITY;
1389 isec->initialized = LABEL_INITIALIZED;
1390 inode->i_fop = &sel_bool_ops;
1391 inode->i_ino = i|SEL_BOOL_INO_OFFSET;
1392 d_add(dentry, inode);
1394 fsi->bool_num = num;
1395 fsi->bool_pending_names = names;
1396 fsi->bool_pending_values = values;
1398 free_page((unsigned long)page);
1401 free_page((unsigned long)page);
1404 for (i = 0; i < num; i++)
1409 sel_remove_entries(dir);
1414 static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1415 size_t count, loff_t *ppos)
1417 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
1418 struct selinux_state *state = fsi->state;
1419 char tmpbuf[TMPBUFLEN];
1422 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1423 avc_get_cache_threshold(state->avc));
1424 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1427 static ssize_t sel_write_avc_cache_threshold(struct file *file,
1428 const char __user *buf,
1429 size_t count, loff_t *ppos)
1432 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1433 struct selinux_state *state = fsi->state;
1436 unsigned int new_value;
1438 ret = avc_has_perm(&selinux_state,
1439 current_sid(), SECINITSID_SECURITY,
1440 SECCLASS_SECURITY, SECURITY__SETSECPARAM,
1445 if (count >= PAGE_SIZE)
1448 /* No partial writes. */
1452 page = memdup_user_nul(buf, count);
1454 return PTR_ERR(page);
1457 if (sscanf(page, "%u", &new_value) != 1)
1460 avc_set_cache_threshold(state->avc, new_value);
1468 static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1469 size_t count, loff_t *ppos)
1471 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
1472 struct selinux_state *state = fsi->state;
1476 page = (char *)__get_free_page(GFP_KERNEL);
1480 length = avc_get_hash_stats(state->avc, page);
1482 length = simple_read_from_buffer(buf, count, ppos, page, length);
1483 free_page((unsigned long)page);
1488 static const struct file_operations sel_avc_cache_threshold_ops = {
1489 .read = sel_read_avc_cache_threshold,
1490 .write = sel_write_avc_cache_threshold,
1491 .llseek = generic_file_llseek,
1494 static const struct file_operations sel_avc_hash_stats_ops = {
1495 .read = sel_read_avc_hash_stats,
1496 .llseek = generic_file_llseek,
1499 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1500 static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1504 for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
1505 if (!cpu_possible(cpu))
1508 return &per_cpu(avc_cache_stats, cpu);
1513 static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1515 loff_t n = *pos - 1;
1518 return SEQ_START_TOKEN;
1520 return sel_avc_get_stat_idx(&n);
1523 static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1525 return sel_avc_get_stat_idx(pos);
1528 static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1530 struct avc_cache_stats *st = v;
1532 if (v == SEQ_START_TOKEN) {
1534 "lookups hits misses allocations reclaims frees\n");
1536 unsigned int lookups = st->lookups;
1537 unsigned int misses = st->misses;
1538 unsigned int hits = lookups - misses;
1539 seq_printf(seq, "%u %u %u %u %u %u\n", lookups,
1540 hits, misses, st->allocations,
1541 st->reclaims, st->frees);
1546 static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1549 static const struct seq_operations sel_avc_cache_stats_seq_ops = {
1550 .start = sel_avc_stats_seq_start,
1551 .next = sel_avc_stats_seq_next,
1552 .show = sel_avc_stats_seq_show,
1553 .stop = sel_avc_stats_seq_stop,
1556 static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1558 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1561 static const struct file_operations sel_avc_cache_stats_ops = {
1562 .open = sel_open_avc_cache_stats,
1564 .llseek = seq_lseek,
1565 .release = seq_release,
1569 static int sel_make_avc_files(struct dentry *dir)
1571 struct super_block *sb = dir->d_sb;
1572 struct selinux_fs_info *fsi = sb->s_fs_info;
1574 static const struct tree_descr files[] = {
1575 { "cache_threshold",
1576 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1577 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1578 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1579 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1583 for (i = 0; i < ARRAY_SIZE(files); i++) {
1584 struct inode *inode;
1585 struct dentry *dentry;
1587 dentry = d_alloc_name(dir, files[i].name);
1591 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1597 inode->i_fop = files[i].ops;
1598 inode->i_ino = ++fsi->last_ino;
1599 d_add(dentry, inode);
1605 static ssize_t sel_read_initcon(struct file *file, char __user *buf,
1606 size_t count, loff_t *ppos)
1608 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1613 sid = file_inode(file)->i_ino&SEL_INO_MASK;
1614 ret = security_sid_to_context(fsi->state, sid, &con, &len);
1618 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1623 static const struct file_operations sel_initcon_ops = {
1624 .read = sel_read_initcon,
1625 .llseek = generic_file_llseek,
1628 static int sel_make_initcon_files(struct dentry *dir)
1632 for (i = 1; i <= SECINITSID_NUM; i++) {
1633 struct inode *inode;
1634 struct dentry *dentry;
1635 dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
1639 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1645 inode->i_fop = &sel_initcon_ops;
1646 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1647 d_add(dentry, inode);
1653 static inline unsigned long sel_class_to_ino(u16 class)
1655 return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1658 static inline u16 sel_ino_to_class(unsigned long ino)
1660 return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1);
1663 static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1665 return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1668 static inline u32 sel_ino_to_perm(unsigned long ino)
1670 return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1673 static ssize_t sel_read_class(struct file *file, char __user *buf,
1674 size_t count, loff_t *ppos)
1676 unsigned long ino = file_inode(file)->i_ino;
1677 char res[TMPBUFLEN];
1678 ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_class(ino));
1679 return simple_read_from_buffer(buf, count, ppos, res, len);
1682 static const struct file_operations sel_class_ops = {
1683 .read = sel_read_class,
1684 .llseek = generic_file_llseek,
1687 static ssize_t sel_read_perm(struct file *file, char __user *buf,
1688 size_t count, loff_t *ppos)
1690 unsigned long ino = file_inode(file)->i_ino;
1691 char res[TMPBUFLEN];
1692 ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino));
1693 return simple_read_from_buffer(buf, count, ppos, res, len);
1696 static const struct file_operations sel_perm_ops = {
1697 .read = sel_read_perm,
1698 .llseek = generic_file_llseek,
1701 static ssize_t sel_read_policycap(struct file *file, char __user *buf,
1702 size_t count, loff_t *ppos)
1704 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1706 char tmpbuf[TMPBUFLEN];
1708 unsigned long i_ino = file_inode(file)->i_ino;
1710 value = security_policycap_supported(fsi->state, i_ino & SEL_INO_MASK);
1711 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
1713 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1716 static const struct file_operations sel_policycap_ops = {
1717 .read = sel_read_policycap,
1718 .llseek = generic_file_llseek,
1721 static int sel_make_perm_files(char *objclass, int classvalue,
1724 struct selinux_fs_info *fsi = dir->d_sb->s_fs_info;
1728 rc = security_get_permissions(fsi->state, objclass, &perms, &nperms);
1732 for (i = 0; i < nperms; i++) {
1733 struct inode *inode;
1734 struct dentry *dentry;
1737 dentry = d_alloc_name(dir, perms[i]);
1742 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1748 inode->i_fop = &sel_perm_ops;
1749 /* i+1 since perm values are 1-indexed */
1750 inode->i_ino = sel_perm_to_ino(classvalue, i + 1);
1751 d_add(dentry, inode);
1755 for (i = 0; i < nperms; i++)
1761 static int sel_make_class_dir_entries(char *classname, int index,
1764 struct super_block *sb = dir->d_sb;
1765 struct selinux_fs_info *fsi = sb->s_fs_info;
1766 struct dentry *dentry = NULL;
1767 struct inode *inode = NULL;
1770 dentry = d_alloc_name(dir, "index");
1774 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1780 inode->i_fop = &sel_class_ops;
1781 inode->i_ino = sel_class_to_ino(index);
1782 d_add(dentry, inode);
1784 dentry = sel_make_dir(dir, "perms", &fsi->last_class_ino);
1786 return PTR_ERR(dentry);
1788 rc = sel_make_perm_files(classname, index, dentry);
1793 static int sel_make_classes(struct selinux_fs_info *fsi)
1796 int rc, nclasses, i;
1799 /* delete any existing entries */
1800 sel_remove_entries(fsi->class_dir);
1802 rc = security_get_classes(fsi->state, &classes, &nclasses);
1806 /* +2 since classes are 1-indexed */
1807 fsi->last_class_ino = sel_class_to_ino(nclasses + 2);
1809 for (i = 0; i < nclasses; i++) {
1810 struct dentry *class_name_dir;
1812 class_name_dir = sel_make_dir(fsi->class_dir, classes[i],
1813 &fsi->last_class_ino);
1814 if (IS_ERR(class_name_dir)) {
1815 rc = PTR_ERR(class_name_dir);
1819 /* i+1 since class values are 1-indexed */
1820 rc = sel_make_class_dir_entries(classes[i], i + 1,
1827 for (i = 0; i < nclasses; i++)
1833 static int sel_make_policycap(struct selinux_fs_info *fsi)
1836 struct dentry *dentry = NULL;
1837 struct inode *inode = NULL;
1839 sel_remove_entries(fsi->policycap_dir);
1841 for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
1842 if (iter < ARRAY_SIZE(selinux_policycap_names))
1843 dentry = d_alloc_name(fsi->policycap_dir,
1844 selinux_policycap_names[iter]);
1846 dentry = d_alloc_name(fsi->policycap_dir, "unknown");
1851 inode = sel_make_inode(fsi->sb, S_IFREG | 0444);
1852 if (inode == NULL) {
1857 inode->i_fop = &sel_policycap_ops;
1858 inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
1859 d_add(dentry, inode);
1865 static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
1868 struct dentry *dentry = d_alloc_name(dir, name);
1869 struct inode *inode;
1872 return ERR_PTR(-ENOMEM);
1874 inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1877 return ERR_PTR(-ENOMEM);
1880 inode->i_op = &simple_dir_inode_operations;
1881 inode->i_fop = &simple_dir_operations;
1882 inode->i_ino = ++(*ino);
1883 /* directory inodes start off with i_nlink == 2 (for "." entry) */
1885 d_add(dentry, inode);
1886 /* bump link count on parent directory, too */
1887 inc_nlink(d_inode(dir));
1892 #define NULL_FILE_NAME "null"
1894 static int sel_fill_super(struct super_block *sb, void *data, int silent)
1896 struct selinux_fs_info *fsi;
1898 struct dentry *dentry;
1899 struct inode *inode;
1900 struct inode_security_struct *isec;
1902 static const struct tree_descr selinux_files[] = {
1903 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1904 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
1905 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
1906 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1907 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1908 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1909 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1910 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1911 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1912 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1913 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1914 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1915 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
1916 [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
1917 [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
1918 [SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO},
1919 [SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO},
1920 [SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops,
1925 ret = selinux_fs_info_create(sb);
1929 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1933 fsi = sb->s_fs_info;
1934 fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino);
1935 if (IS_ERR(fsi->bool_dir)) {
1936 ret = PTR_ERR(fsi->bool_dir);
1937 fsi->bool_dir = NULL;
1942 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
1947 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
1953 inode->i_ino = ++fsi->last_ino;
1954 isec = selinux_inode(inode);
1955 isec->sid = SECINITSID_DEVNULL;
1956 isec->sclass = SECCLASS_CHR_FILE;
1957 isec->initialized = LABEL_INITIALIZED;
1959 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1960 d_add(dentry, inode);
1962 dentry = sel_make_dir(sb->s_root, "avc", &fsi->last_ino);
1963 if (IS_ERR(dentry)) {
1964 ret = PTR_ERR(dentry);
1968 ret = sel_make_avc_files(dentry);
1972 dentry = sel_make_dir(sb->s_root, "initial_contexts", &fsi->last_ino);
1973 if (IS_ERR(dentry)) {
1974 ret = PTR_ERR(dentry);
1978 ret = sel_make_initcon_files(dentry);
1982 fsi->class_dir = sel_make_dir(sb->s_root, "class", &fsi->last_ino);
1983 if (IS_ERR(fsi->class_dir)) {
1984 ret = PTR_ERR(fsi->class_dir);
1985 fsi->class_dir = NULL;
1989 fsi->policycap_dir = sel_make_dir(sb->s_root, "policy_capabilities",
1991 if (IS_ERR(fsi->policycap_dir)) {
1992 ret = PTR_ERR(fsi->policycap_dir);
1993 fsi->policycap_dir = NULL;
1997 ret = sel_make_policy_nodes(fsi);
2002 pr_err("SELinux: %s: failed while creating inodes\n",
2005 selinux_fs_info_free(sb);
2010 static struct dentry *sel_mount(struct file_system_type *fs_type,
2011 int flags, const char *dev_name, void *data)
2013 return mount_single(fs_type, flags, data, sel_fill_super);
2016 static void sel_kill_sb(struct super_block *sb)
2018 selinux_fs_info_free(sb);
2019 kill_litter_super(sb);
2022 static struct file_system_type sel_fs_type = {
2023 .name = "selinuxfs",
2025 .kill_sb = sel_kill_sb,
2028 struct vfsmount *selinuxfs_mount;
2029 struct path selinux_null;
2031 static int __init init_sel_fs(void)
2033 struct qstr null_name = QSTR_INIT(NULL_FILE_NAME,
2034 sizeof(NULL_FILE_NAME)-1);
2037 if (!selinux_enabled)
2040 err = sysfs_create_mount_point(fs_kobj, "selinux");
2044 err = register_filesystem(&sel_fs_type);
2046 sysfs_remove_mount_point(fs_kobj, "selinux");
2050 selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type);
2051 if (IS_ERR(selinuxfs_mount)) {
2052 pr_err("selinuxfs: could not mount!\n");
2053 err = PTR_ERR(selinuxfs_mount);
2054 selinuxfs_mount = NULL;
2056 selinux_null.dentry = d_hash_and_lookup(selinux_null.mnt->mnt_root,
2058 if (IS_ERR(selinux_null.dentry)) {
2059 pr_err("selinuxfs: could not lookup null!\n");
2060 err = PTR_ERR(selinux_null.dentry);
2061 selinux_null.dentry = NULL;
2067 __initcall(init_sel_fs);
2069 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
2070 void exit_sel_fs(void)
2072 sysfs_remove_mount_point(fs_kobj, "selinux");
2073 dput(selinux_null.dentry);
2074 kern_unmount(selinuxfs_mount);
2075 unregister_filesystem(&sel_fs_type);