blob: 10bff155947b230ef093285327aac6e7c1f41371 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0-only
2/* Updated: Karl MacMillan <kmacmillan@tresys.com>
3 *
4 * Added conditional policy language extensions
5 *
6 * Updated: Hewlett-Packard <paul@paul-moore.com>
7 *
8 * Added support for the policy capability bitmap
9 *
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>
13 */
14
15#include <linux/kernel.h>
16#include <linux/pagemap.h>
17#include <linux/slab.h>
18#include <linux/vmalloc.h>
19#include <linux/fs.h>
20#include <linux/fs_context.h>
21#include <linux/mount.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>
33
34/* selinuxfs pseudo filesystem for exporting the security policy API.
35 Based on the proc code and the fs/nfsd/nfsctl.c code. */
36
37#include "flask.h"
38#include "avc.h"
39#include "avc_ss.h"
40#include "security.h"
41#include "objsec.h"
42#include "conditional.h"
43
44enum sel_inos {
45 SEL_ROOT_INO = 2,
46 SEL_LOAD, /* load policy */
47 SEL_ENFORCE, /* get or set enforcing status */
48 SEL_CONTEXT, /* validate context */
49 SEL_ACCESS, /* compute access decision */
50 SEL_CREATE, /* compute create labeling decision */
51 SEL_RELABEL, /* compute relabeling decision */
52 SEL_USER, /* compute reachable user contexts */
53 SEL_POLICYVERS, /* return policy version for this kernel */
54 SEL_COMMIT_BOOLS, /* commit new boolean values */
55 SEL_MLS, /* return if MLS policy is enabled */
56 SEL_DISABLE, /* disable SELinux until next reboot */
57 SEL_MEMBER, /* compute polyinstantiation membership decision */
58 SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
59 SEL_COMPAT_NET, /* whether to use old compat network packet controls */
60 SEL_REJECT_UNKNOWN, /* export unknown reject handling to userspace */
61 SEL_DENY_UNKNOWN, /* export unknown deny handling to userspace */
62 SEL_STATUS, /* export current status using mmap() */
63 SEL_POLICY, /* allow userspace to read the in kernel policy */
64 SEL_VALIDATE_TRANS, /* compute validatetrans decision */
65 SEL_INO_NEXT, /* The next inode number to use */
66};
67
68struct selinux_fs_info {
69 struct dentry *bool_dir;
70 unsigned int bool_num;
71 char **bool_pending_names;
72 unsigned int *bool_pending_values;
73 struct dentry *class_dir;
74 unsigned long last_class_ino;
75 bool policy_opened;
76 struct dentry *policycap_dir;
77 struct mutex mutex;
78 unsigned long last_ino;
79 struct selinux_state *state;
80 struct super_block *sb;
81};
82
83static int selinux_fs_info_create(struct super_block *sb)
84{
85 struct selinux_fs_info *fsi;
86
87 fsi = kzalloc(sizeof(*fsi), GFP_KERNEL);
88 if (!fsi)
89 return -ENOMEM;
90
91 mutex_init(&fsi->mutex);
92 fsi->last_ino = SEL_INO_NEXT - 1;
93 fsi->state = &selinux_state;
94 fsi->sb = sb;
95 sb->s_fs_info = fsi;
96 return 0;
97}
98
99static void selinux_fs_info_free(struct super_block *sb)
100{
101 struct selinux_fs_info *fsi = sb->s_fs_info;
102 int i;
103
104 if (fsi) {
105 for (i = 0; i < fsi->bool_num; i++)
106 kfree(fsi->bool_pending_names[i]);
107 kfree(fsi->bool_pending_names);
108 kfree(fsi->bool_pending_values);
109 }
110 kfree(sb->s_fs_info);
111 sb->s_fs_info = NULL;
112}
113
114#define SEL_INITCON_INO_OFFSET 0x01000000
115#define SEL_BOOL_INO_OFFSET 0x02000000
116#define SEL_CLASS_INO_OFFSET 0x04000000
117#define SEL_POLICYCAP_INO_OFFSET 0x08000000
118#define SEL_INO_MASK 0x00ffffff
119
120#define TMPBUFLEN 12
121static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
122 size_t count, loff_t *ppos)
123{
124 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
125 char tmpbuf[TMPBUFLEN];
126 ssize_t length;
127
128 length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
129 enforcing_enabled(fsi->state));
130 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
131}
132
133#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
134static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
135 size_t count, loff_t *ppos)
136
137{
138 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
139 struct selinux_state *state = fsi->state;
140 char *page = NULL;
141 ssize_t length;
142 int old_value, new_value;
143
144 if (count >= PAGE_SIZE)
145 return -ENOMEM;
146
147 /* No partial writes. */
148 if (*ppos != 0)
149 return -EINVAL;
150
151 page = memdup_user_nul(buf, count);
152 if (IS_ERR(page))
153 return PTR_ERR(page);
154
155 length = -EINVAL;
156 if (sscanf(page, "%d", &new_value) != 1)
157 goto out;
158
159 new_value = !!new_value;
160
161 old_value = enforcing_enabled(state);
162 if (new_value != old_value) {
163 length = avc_has_perm(&selinux_state,
164 current_sid(), SECINITSID_SECURITY,
165 SECCLASS_SECURITY, SECURITY__SETENFORCE,
166 NULL);
167 if (length)
168 goto out;
169 audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS,
170 "enforcing=%d old_enforcing=%d auid=%u ses=%u"
171 " enabled=%d old-enabled=%d lsm=selinux res=1",
172 new_value, old_value,
173 from_kuid(&init_user_ns, audit_get_loginuid(current)),
174 audit_get_sessionid(current),
175 selinux_enabled, selinux_enabled);
176 enforcing_set(state, new_value);
177 if (new_value)
178 avc_ss_reset(state->avc, 0);
179 selnl_notify_setenforce(new_value);
180 selinux_status_update_setenforce(state, new_value);
181 if (!new_value)
182 call_blocking_lsm_notifier(LSM_POLICY_CHANGE, NULL);
183 }
184 length = count;
185out:
186 kfree(page);
187 return length;
188}
189#else
190#define sel_write_enforce NULL
191#endif
192
193static const struct file_operations sel_enforce_ops = {
194 .read = sel_read_enforce,
195 .write = sel_write_enforce,
196 .llseek = generic_file_llseek,
197};
198
199static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf,
200 size_t count, loff_t *ppos)
201{
202 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
203 struct selinux_state *state = fsi->state;
204 char tmpbuf[TMPBUFLEN];
205 ssize_t length;
206 ino_t ino = file_inode(filp)->i_ino;
207 int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
208 security_get_reject_unknown(state) :
209 !security_get_allow_unknown(state);
210
211 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
212 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
213}
214
215static const struct file_operations sel_handle_unknown_ops = {
216 .read = sel_read_handle_unknown,
217 .llseek = generic_file_llseek,
218};
219
220static int sel_open_handle_status(struct inode *inode, struct file *filp)
221{
222 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
223 struct page *status = selinux_kernel_status_page(fsi->state);
224
225 if (!status)
226 return -ENOMEM;
227
228 filp->private_data = status;
229
230 return 0;
231}
232
233static ssize_t sel_read_handle_status(struct file *filp, char __user *buf,
234 size_t count, loff_t *ppos)
235{
236 struct page *status = filp->private_data;
237
238 BUG_ON(!status);
239
240 return simple_read_from_buffer(buf, count, ppos,
241 page_address(status),
242 sizeof(struct selinux_kernel_status));
243}
244
245static int sel_mmap_handle_status(struct file *filp,
246 struct vm_area_struct *vma)
247{
248 struct page *status = filp->private_data;
249 unsigned long size = vma->vm_end - vma->vm_start;
250
251 BUG_ON(!status);
252
253 /* only allows one page from the head */
254 if (vma->vm_pgoff > 0 || size != PAGE_SIZE)
255 return -EIO;
256 /* disallow writable mapping */
257 if (vma->vm_flags & VM_WRITE)
258 return -EPERM;
259 /* disallow mprotect() turns it into writable */
260 vma->vm_flags &= ~VM_MAYWRITE;
261
262 return remap_pfn_range(vma, vma->vm_start,
263 page_to_pfn(status),
264 size, vma->vm_page_prot);
265}
266
267static 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,
272};
273
274#ifdef CONFIG_SECURITY_SELINUX_DISABLE
275static ssize_t sel_write_disable(struct file *file, const char __user *buf,
276 size_t count, loff_t *ppos)
277
278{
279 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
280 char *page;
281 ssize_t length;
282 int new_value;
283 int enforcing;
284
285 if (count >= PAGE_SIZE)
286 return -ENOMEM;
287
288 /* No partial writes. */
289 if (*ppos != 0)
290 return -EINVAL;
291
292 page = memdup_user_nul(buf, count);
293 if (IS_ERR(page))
294 return PTR_ERR(page);
295
296 length = -EINVAL;
297 if (sscanf(page, "%d", &new_value) != 1)
298 goto out;
299
300 if (new_value) {
301 enforcing = enforcing_enabled(fsi->state);
302 length = selinux_disable(fsi->state);
303 if (length)
304 goto out;
305 audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_STATUS,
306 "enforcing=%d old_enforcing=%d auid=%u ses=%u"
307 " enabled=%d old-enabled=%d lsm=selinux res=1",
308 enforcing, enforcing,
309 from_kuid(&init_user_ns, audit_get_loginuid(current)),
310 audit_get_sessionid(current), 0, 1);
311 }
312
313 length = count;
314out:
315 kfree(page);
316 return length;
317}
318#else
319#define sel_write_disable NULL
320#endif
321
322static const struct file_operations sel_disable_ops = {
323 .write = sel_write_disable,
324 .llseek = generic_file_llseek,
325};
326
327static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
328 size_t count, loff_t *ppos)
329{
330 char tmpbuf[TMPBUFLEN];
331 ssize_t length;
332
333 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
334 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
335}
336
337static const struct file_operations sel_policyvers_ops = {
338 .read = sel_read_policyvers,
339 .llseek = generic_file_llseek,
340};
341
342/* declaration for sel_write_load */
343static int sel_make_bools(struct selinux_fs_info *fsi);
344static int sel_make_classes(struct selinux_fs_info *fsi);
345static int sel_make_policycap(struct selinux_fs_info *fsi);
346
347/* declaration for sel_make_class_dirs */
348static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
349 unsigned long *ino);
350
351static ssize_t sel_read_mls(struct file *filp, char __user *buf,
352 size_t count, loff_t *ppos)
353{
354 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
355 char tmpbuf[TMPBUFLEN];
356 ssize_t length;
357
358 length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
359 security_mls_enabled(fsi->state));
360 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
361}
362
363static const struct file_operations sel_mls_ops = {
364 .read = sel_read_mls,
365 .llseek = generic_file_llseek,
366};
367
368struct policy_load_memory {
369 size_t len;
370 void *data;
371};
372
373static int sel_open_policy(struct inode *inode, struct file *filp)
374{
375 struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
376 struct selinux_state *state = fsi->state;
377 struct policy_load_memory *plm = NULL;
378 int rc;
379
380 BUG_ON(filp->private_data);
381
382 mutex_lock(&fsi->mutex);
383
384 rc = avc_has_perm(&selinux_state,
385 current_sid(), SECINITSID_SECURITY,
386 SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
387 if (rc)
388 goto err;
389
390 rc = -EBUSY;
391 if (fsi->policy_opened)
392 goto err;
393
394 rc = -ENOMEM;
395 plm = kzalloc(sizeof(*plm), GFP_KERNEL);
396 if (!plm)
397 goto err;
398
399 if (i_size_read(inode) != security_policydb_len(state)) {
400 inode_lock(inode);
401 i_size_write(inode, security_policydb_len(state));
402 inode_unlock(inode);
403 }
404
405 rc = security_read_policy(state, &plm->data, &plm->len);
406 if (rc)
407 goto err;
408
409 fsi->policy_opened = 1;
410
411 filp->private_data = plm;
412
413 mutex_unlock(&fsi->mutex);
414
415 return 0;
416err:
417 mutex_unlock(&fsi->mutex);
418
419 if (plm)
420 vfree(plm->data);
421 kfree(plm);
422 return rc;
423}
424
425static int sel_release_policy(struct inode *inode, struct file *filp)
426{
427 struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
428 struct policy_load_memory *plm = filp->private_data;
429
430 BUG_ON(!plm);
431
432 fsi->policy_opened = 0;
433
434 vfree(plm->data);
435 kfree(plm);
436
437 return 0;
438}
439
440static ssize_t sel_read_policy(struct file *filp, char __user *buf,
441 size_t count, loff_t *ppos)
442{
443 struct policy_load_memory *plm = filp->private_data;
444 int ret;
445
446 ret = avc_has_perm(&selinux_state,
447 current_sid(), SECINITSID_SECURITY,
448 SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
449 if (ret)
450 return ret;
451
452 return simple_read_from_buffer(buf, count, ppos, plm->data, plm->len);
453}
454
455static vm_fault_t sel_mmap_policy_fault(struct vm_fault *vmf)
456{
457 struct policy_load_memory *plm = vmf->vma->vm_file->private_data;
458 unsigned long offset;
459 struct page *page;
460
461 if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE))
462 return VM_FAULT_SIGBUS;
463
464 offset = vmf->pgoff << PAGE_SHIFT;
465 if (offset >= roundup(plm->len, PAGE_SIZE))
466 return VM_FAULT_SIGBUS;
467
468 page = vmalloc_to_page(plm->data + offset);
469 get_page(page);
470
471 vmf->page = page;
472
473 return 0;
474}
475
476static const struct vm_operations_struct sel_mmap_policy_ops = {
477 .fault = sel_mmap_policy_fault,
478 .page_mkwrite = sel_mmap_policy_fault,
479};
480
481static int sel_mmap_policy(struct file *filp, struct vm_area_struct *vma)
482{
483 if (vma->vm_flags & VM_SHARED) {
484 /* do not allow mprotect to make mapping writable */
485 vma->vm_flags &= ~VM_MAYWRITE;
486
487 if (vma->vm_flags & VM_WRITE)
488 return -EACCES;
489 }
490
491 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
492 vma->vm_ops = &sel_mmap_policy_ops;
493
494 return 0;
495}
496
497static 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,
503};
504
505static int sel_make_policy_nodes(struct selinux_fs_info *fsi)
506{
507 int ret;
508
509 ret = sel_make_bools(fsi);
510 if (ret) {
511 pr_err("SELinux: failed to load policy booleans\n");
512 return ret;
513 }
514
515 ret = sel_make_classes(fsi);
516 if (ret) {
517 pr_err("SELinux: failed to load policy classes\n");
518 return ret;
519 }
520
521 ret = sel_make_policycap(fsi);
522 if (ret) {
523 pr_err("SELinux: failed to load policy capabilities\n");
524 return ret;
525 }
526
527 return 0;
528}
529
530static ssize_t sel_write_load(struct file *file, const char __user *buf,
531 size_t count, loff_t *ppos)
532
533{
534 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
535 ssize_t length;
536 void *data = NULL;
537
538 /* no partial writes */
539 if (*ppos)
540 return -EINVAL;
541 /* no empty policies */
542 if (!count)
543 return -EINVAL;
544
545 if (count > 64 * 1024 * 1024)
546 return -EFBIG;
547
548 mutex_lock(&fsi->mutex);
549
550 length = avc_has_perm(&selinux_state,
551 current_sid(), SECINITSID_SECURITY,
552 SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL);
553 if (length)
554 goto out;
555
556 data = vmalloc(count);
557 if (!data) {
558 length = -ENOMEM;
559 goto out;
560 }
561 if (copy_from_user(data, buf, count) != 0) {
562 length = -EFAULT;
563 goto out;
564 }
565
566 length = security_load_policy(fsi->state, data, count);
567 if (length) {
568 pr_warn_ratelimited("SELinux: failed to load policy\n");
569 goto out;
570 }
571
572 length = sel_make_policy_nodes(fsi);
573 if (length)
574 goto out1;
575
576 length = count;
577
578out1:
579 audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
580 "auid=%u ses=%u lsm=selinux res=1",
581 from_kuid(&init_user_ns, audit_get_loginuid(current)),
582 audit_get_sessionid(current));
583
584out:
585 mutex_unlock(&fsi->mutex);
586 vfree(data);
587 return length;
588}
589
590static const struct file_operations sel_load_ops = {
591 .write = sel_write_load,
592 .llseek = generic_file_llseek,
593};
594
595static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
596{
597 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
598 struct selinux_state *state = fsi->state;
599 char *canon = NULL;
600 u32 sid, len;
601 ssize_t length;
602
603 length = avc_has_perm(&selinux_state,
604 current_sid(), SECINITSID_SECURITY,
605 SECCLASS_SECURITY, SECURITY__CHECK_CONTEXT, NULL);
606 if (length)
607 goto out;
608
609 length = security_context_to_sid(state, buf, size, &sid, GFP_KERNEL);
610 if (length)
611 goto out;
612
613 length = security_sid_to_context(state, sid, &canon, &len);
614 if (length)
615 goto out;
616
617 length = -ERANGE;
618 if (len > SIMPLE_TRANSACTION_LIMIT) {
619 pr_err("SELinux: %s: context size (%u) exceeds "
620 "payload max\n", __func__, len);
621 goto out;
622 }
623
624 memcpy(buf, canon, len);
625 length = len;
626out:
627 kfree(canon);
628 return length;
629}
630
631static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
632 size_t count, loff_t *ppos)
633{
634 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
635 char tmpbuf[TMPBUFLEN];
636 ssize_t length;
637
638 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", fsi->state->checkreqprot);
639 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
640}
641
642static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
643 size_t count, loff_t *ppos)
644{
645 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
646 char *page;
647 ssize_t length;
648 unsigned int new_value;
649
650 length = avc_has_perm(&selinux_state,
651 current_sid(), SECINITSID_SECURITY,
652 SECCLASS_SECURITY, SECURITY__SETCHECKREQPROT,
653 NULL);
654 if (length)
655 return length;
656
657 if (count >= PAGE_SIZE)
658 return -ENOMEM;
659
660 /* No partial writes. */
661 if (*ppos != 0)
662 return -EINVAL;
663
664 page = memdup_user_nul(buf, count);
665 if (IS_ERR(page))
666 return PTR_ERR(page);
667
668 length = -EINVAL;
669 if (sscanf(page, "%u", &new_value) != 1)
670 goto out;
671
672 fsi->state->checkreqprot = new_value ? 1 : 0;
673 length = count;
674out:
675 kfree(page);
676 return length;
677}
678static const struct file_operations sel_checkreqprot_ops = {
679 .read = sel_read_checkreqprot,
680 .write = sel_write_checkreqprot,
681 .llseek = generic_file_llseek,
682};
683
684static ssize_t sel_write_validatetrans(struct file *file,
685 const char __user *buf,
686 size_t count, loff_t *ppos)
687{
688 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
689 struct selinux_state *state = fsi->state;
690 char *oldcon = NULL, *newcon = NULL, *taskcon = NULL;
691 char *req = NULL;
692 u32 osid, nsid, tsid;
693 u16 tclass;
694 int rc;
695
696 rc = avc_has_perm(&selinux_state,
697 current_sid(), SECINITSID_SECURITY,
698 SECCLASS_SECURITY, SECURITY__VALIDATE_TRANS, NULL);
699 if (rc)
700 goto out;
701
702 rc = -ENOMEM;
703 if (count >= PAGE_SIZE)
704 goto out;
705
706 /* No partial writes. */
707 rc = -EINVAL;
708 if (*ppos != 0)
709 goto out;
710
711 req = memdup_user_nul(buf, count);
712 if (IS_ERR(req)) {
713 rc = PTR_ERR(req);
714 req = NULL;
715 goto out;
716 }
717
718 rc = -ENOMEM;
719 oldcon = kzalloc(count + 1, GFP_KERNEL);
720 if (!oldcon)
721 goto out;
722
723 newcon = kzalloc(count + 1, GFP_KERNEL);
724 if (!newcon)
725 goto out;
726
727 taskcon = kzalloc(count + 1, GFP_KERNEL);
728 if (!taskcon)
729 goto out;
730
731 rc = -EINVAL;
732 if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4)
733 goto out;
734
735 rc = security_context_str_to_sid(state, oldcon, &osid, GFP_KERNEL);
736 if (rc)
737 goto out;
738
739 rc = security_context_str_to_sid(state, newcon, &nsid, GFP_KERNEL);
740 if (rc)
741 goto out;
742
743 rc = security_context_str_to_sid(state, taskcon, &tsid, GFP_KERNEL);
744 if (rc)
745 goto out;
746
747 rc = security_validate_transition_user(state, osid, nsid, tsid, tclass);
748 if (!rc)
749 rc = count;
750out:
751 kfree(req);
752 kfree(oldcon);
753 kfree(newcon);
754 kfree(taskcon);
755 return rc;
756}
757
758static const struct file_operations sel_transition_ops = {
759 .write = sel_write_validatetrans,
760 .llseek = generic_file_llseek,
761};
762
763/*
764 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
765 */
766static ssize_t sel_write_access(struct file *file, char *buf, size_t size);
767static ssize_t sel_write_create(struct file *file, char *buf, size_t size);
768static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size);
769static ssize_t sel_write_user(struct file *file, char *buf, size_t size);
770static ssize_t sel_write_member(struct file *file, char *buf, size_t size);
771
772static ssize_t (*const write_op[])(struct file *, char *, size_t) = {
773 [SEL_ACCESS] = sel_write_access,
774 [SEL_CREATE] = sel_write_create,
775 [SEL_RELABEL] = sel_write_relabel,
776 [SEL_USER] = sel_write_user,
777 [SEL_MEMBER] = sel_write_member,
778 [SEL_CONTEXT] = sel_write_context,
779};
780
781static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
782{
783 ino_t ino = file_inode(file)->i_ino;
784 char *data;
785 ssize_t rv;
786
787 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
788 return -EINVAL;
789
790 data = simple_transaction_get(file, buf, size);
791 if (IS_ERR(data))
792 return PTR_ERR(data);
793
794 rv = write_op[ino](file, data, size);
795 if (rv > 0) {
796 simple_transaction_set(file, rv);
797 rv = size;
798 }
799 return rv;
800}
801
802static const struct file_operations transaction_ops = {
803 .write = selinux_transaction_write,
804 .read = simple_transaction_read,
805 .release = simple_transaction_release,
806 .llseek = generic_file_llseek,
807};
808
809/*
810 * payload - write methods
811 * If the method has a response, the response should be put in buf,
812 * and the length returned. Otherwise return 0 or and -error.
813 */
814
815static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
816{
817 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
818 struct selinux_state *state = fsi->state;
819 char *scon = NULL, *tcon = NULL;
820 u32 ssid, tsid;
821 u16 tclass;
822 struct av_decision avd;
823 ssize_t length;
824
825 length = avc_has_perm(&selinux_state,
826 current_sid(), SECINITSID_SECURITY,
827 SECCLASS_SECURITY, SECURITY__COMPUTE_AV, NULL);
828 if (length)
829 goto out;
830
831 length = -ENOMEM;
832 scon = kzalloc(size + 1, GFP_KERNEL);
833 if (!scon)
834 goto out;
835
836 length = -ENOMEM;
837 tcon = kzalloc(size + 1, GFP_KERNEL);
838 if (!tcon)
839 goto out;
840
841 length = -EINVAL;
842 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
843 goto out;
844
845 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
846 if (length)
847 goto out;
848
849 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
850 if (length)
851 goto out;
852
853 security_compute_av_user(state, ssid, tsid, tclass, &avd);
854
855 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
856 "%x %x %x %x %u %x",
857 avd.allowed, 0xffffffff,
858 avd.auditallow, avd.auditdeny,
859 avd.seqno, avd.flags);
860out:
861 kfree(tcon);
862 kfree(scon);
863 return length;
864}
865
866static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
867{
868 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
869 struct selinux_state *state = fsi->state;
870 char *scon = NULL, *tcon = NULL;
871 char *namebuf = NULL, *objname = NULL;
872 u32 ssid, tsid, newsid;
873 u16 tclass;
874 ssize_t length;
875 char *newcon = NULL;
876 u32 len;
877 int nargs;
878
879 length = avc_has_perm(&selinux_state,
880 current_sid(), SECINITSID_SECURITY,
881 SECCLASS_SECURITY, SECURITY__COMPUTE_CREATE,
882 NULL);
883 if (length)
884 goto out;
885
886 length = -ENOMEM;
887 scon = kzalloc(size + 1, GFP_KERNEL);
888 if (!scon)
889 goto out;
890
891 length = -ENOMEM;
892 tcon = kzalloc(size + 1, GFP_KERNEL);
893 if (!tcon)
894 goto out;
895
896 length = -ENOMEM;
897 namebuf = kzalloc(size + 1, GFP_KERNEL);
898 if (!namebuf)
899 goto out;
900
901 length = -EINVAL;
902 nargs = sscanf(buf, "%s %s %hu %s", scon, tcon, &tclass, namebuf);
903 if (nargs < 3 || nargs > 4)
904 goto out;
905 if (nargs == 4) {
906 /*
907 * If and when the name of new object to be queried contains
908 * either whitespace or multibyte characters, they shall be
909 * encoded based on the percentage-encoding rule.
910 * If not encoded, the sscanf logic picks up only left-half
911 * of the supplied name; splitted by a whitespace unexpectedly.
912 */
913 char *r, *w;
914 int c1, c2;
915
916 r = w = namebuf;
917 do {
918 c1 = *r++;
919 if (c1 == '+')
920 c1 = ' ';
921 else if (c1 == '%') {
922 c1 = hex_to_bin(*r++);
923 if (c1 < 0)
924 goto out;
925 c2 = hex_to_bin(*r++);
926 if (c2 < 0)
927 goto out;
928 c1 = (c1 << 4) | c2;
929 }
930 *w++ = c1;
931 } while (c1 != '\0');
932
933 objname = namebuf;
934 }
935
936 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
937 if (length)
938 goto out;
939
940 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
941 if (length)
942 goto out;
943
944 length = security_transition_sid_user(state, ssid, tsid, tclass,
945 objname, &newsid);
946 if (length)
947 goto out;
948
949 length = security_sid_to_context(state, newsid, &newcon, &len);
950 if (length)
951 goto out;
952
953 length = -ERANGE;
954 if (len > SIMPLE_TRANSACTION_LIMIT) {
955 pr_err("SELinux: %s: context size (%u) exceeds "
956 "payload max\n", __func__, len);
957 goto out;
958 }
959
960 memcpy(buf, newcon, len);
961 length = len;
962out:
963 kfree(newcon);
964 kfree(namebuf);
965 kfree(tcon);
966 kfree(scon);
967 return length;
968}
969
970static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
971{
972 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
973 struct selinux_state *state = fsi->state;
974 char *scon = NULL, *tcon = NULL;
975 u32 ssid, tsid, newsid;
976 u16 tclass;
977 ssize_t length;
978 char *newcon = NULL;
979 u32 len;
980
981 length = avc_has_perm(&selinux_state,
982 current_sid(), SECINITSID_SECURITY,
983 SECCLASS_SECURITY, SECURITY__COMPUTE_RELABEL,
984 NULL);
985 if (length)
986 goto out;
987
988 length = -ENOMEM;
989 scon = kzalloc(size + 1, GFP_KERNEL);
990 if (!scon)
991 goto out;
992
993 length = -ENOMEM;
994 tcon = kzalloc(size + 1, GFP_KERNEL);
995 if (!tcon)
996 goto out;
997
998 length = -EINVAL;
999 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
1000 goto out;
1001
1002 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
1003 if (length)
1004 goto out;
1005
1006 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
1007 if (length)
1008 goto out;
1009
1010 length = security_change_sid(state, ssid, tsid, tclass, &newsid);
1011 if (length)
1012 goto out;
1013
1014 length = security_sid_to_context(state, newsid, &newcon, &len);
1015 if (length)
1016 goto out;
1017
1018 length = -ERANGE;
1019 if (len > SIMPLE_TRANSACTION_LIMIT)
1020 goto out;
1021
1022 memcpy(buf, newcon, len);
1023 length = len;
1024out:
1025 kfree(newcon);
1026 kfree(tcon);
1027 kfree(scon);
1028 return length;
1029}
1030
1031static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
1032{
1033 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1034 struct selinux_state *state = fsi->state;
1035 char *con = NULL, *user = NULL, *ptr;
1036 u32 sid, *sids = NULL;
1037 ssize_t length;
1038 char *newcon;
1039 int i, rc;
1040 u32 len, nsids;
1041
1042 length = avc_has_perm(&selinux_state,
1043 current_sid(), SECINITSID_SECURITY,
1044 SECCLASS_SECURITY, SECURITY__COMPUTE_USER,
1045 NULL);
1046 if (length)
1047 goto out;
1048
1049 length = -ENOMEM;
1050 con = kzalloc(size + 1, GFP_KERNEL);
1051 if (!con)
1052 goto out;
1053
1054 length = -ENOMEM;
1055 user = kzalloc(size + 1, GFP_KERNEL);
1056 if (!user)
1057 goto out;
1058
1059 length = -EINVAL;
1060 if (sscanf(buf, "%s %s", con, user) != 2)
1061 goto out;
1062
1063 length = security_context_str_to_sid(state, con, &sid, GFP_KERNEL);
1064 if (length)
1065 goto out;
1066
1067 length = security_get_user_sids(state, sid, user, &sids, &nsids);
1068 if (length)
1069 goto out;
1070
1071 length = sprintf(buf, "%u", nsids) + 1;
1072 ptr = buf + length;
1073 for (i = 0; i < nsids; i++) {
1074 rc = security_sid_to_context(state, sids[i], &newcon, &len);
1075 if (rc) {
1076 length = rc;
1077 goto out;
1078 }
1079 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
1080 kfree(newcon);
1081 length = -ERANGE;
1082 goto out;
1083 }
1084 memcpy(ptr, newcon, len);
1085 kfree(newcon);
1086 ptr += len;
1087 length += len;
1088 }
1089out:
1090 kfree(sids);
1091 kfree(user);
1092 kfree(con);
1093 return length;
1094}
1095
1096static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
1097{
1098 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1099 struct selinux_state *state = fsi->state;
1100 char *scon = NULL, *tcon = NULL;
1101 u32 ssid, tsid, newsid;
1102 u16 tclass;
1103 ssize_t length;
1104 char *newcon = NULL;
1105 u32 len;
1106
1107 length = avc_has_perm(&selinux_state,
1108 current_sid(), SECINITSID_SECURITY,
1109 SECCLASS_SECURITY, SECURITY__COMPUTE_MEMBER,
1110 NULL);
1111 if (length)
1112 goto out;
1113
1114 length = -ENOMEM;
1115 scon = kzalloc(size + 1, GFP_KERNEL);
1116 if (!scon)
1117 goto out;
1118
1119 length = -ENOMEM;
1120 tcon = kzalloc(size + 1, GFP_KERNEL);
1121 if (!tcon)
1122 goto out;
1123
1124 length = -EINVAL;
1125 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
1126 goto out;
1127
1128 length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
1129 if (length)
1130 goto out;
1131
1132 length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
1133 if (length)
1134 goto out;
1135
1136 length = security_member_sid(state, ssid, tsid, tclass, &newsid);
1137 if (length)
1138 goto out;
1139
1140 length = security_sid_to_context(state, newsid, &newcon, &len);
1141 if (length)
1142 goto out;
1143
1144 length = -ERANGE;
1145 if (len > SIMPLE_TRANSACTION_LIMIT) {
1146 pr_err("SELinux: %s: context size (%u) exceeds "
1147 "payload max\n", __func__, len);
1148 goto out;
1149 }
1150
1151 memcpy(buf, newcon, len);
1152 length = len;
1153out:
1154 kfree(newcon);
1155 kfree(tcon);
1156 kfree(scon);
1157 return length;
1158}
1159
1160static struct inode *sel_make_inode(struct super_block *sb, int mode)
1161{
1162 struct inode *ret = new_inode(sb);
1163
1164 if (ret) {
1165 ret->i_mode = mode;
1166 ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret);
1167 }
1168 return ret;
1169}
1170
1171static ssize_t sel_read_bool(struct file *filep, char __user *buf,
1172 size_t count, loff_t *ppos)
1173{
1174 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
1175 char *page = NULL;
1176 ssize_t length;
1177 ssize_t ret;
1178 int cur_enforcing;
1179 unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
1180 const char *name = filep->f_path.dentry->d_name.name;
1181
1182 mutex_lock(&fsi->mutex);
1183
1184 ret = -EINVAL;
1185 if (index >= fsi->bool_num || strcmp(name,
1186 fsi->bool_pending_names[index]))
1187 goto out_unlock;
1188
1189 ret = -ENOMEM;
1190 page = (char *)get_zeroed_page(GFP_KERNEL);
1191 if (!page)
1192 goto out_unlock;
1193
1194 cur_enforcing = security_get_bool_value(fsi->state, index);
1195 if (cur_enforcing < 0) {
1196 ret = cur_enforcing;
1197 goto out_unlock;
1198 }
1199 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
1200 fsi->bool_pending_values[index]);
1201 mutex_unlock(&fsi->mutex);
1202 ret = simple_read_from_buffer(buf, count, ppos, page, length);
1203out_free:
1204 free_page((unsigned long)page);
1205 return ret;
1206
1207out_unlock:
1208 mutex_unlock(&fsi->mutex);
1209 goto out_free;
1210}
1211
1212static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
1213 size_t count, loff_t *ppos)
1214{
1215 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
1216 char *page = NULL;
1217 ssize_t length;
1218 int new_value;
1219 unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
1220 const char *name = filep->f_path.dentry->d_name.name;
1221
1222 if (count >= PAGE_SIZE)
1223 return -ENOMEM;
1224
1225 /* No partial writes. */
1226 if (*ppos != 0)
1227 return -EINVAL;
1228
1229 page = memdup_user_nul(buf, count);
1230 if (IS_ERR(page))
1231 return PTR_ERR(page);
1232
1233 mutex_lock(&fsi->mutex);
1234
1235 length = avc_has_perm(&selinux_state,
1236 current_sid(), SECINITSID_SECURITY,
1237 SECCLASS_SECURITY, SECURITY__SETBOOL,
1238 NULL);
1239 if (length)
1240 goto out;
1241
1242 length = -EINVAL;
1243 if (index >= fsi->bool_num || strcmp(name,
1244 fsi->bool_pending_names[index]))
1245 goto out;
1246
1247 length = -EINVAL;
1248 if (sscanf(page, "%d", &new_value) != 1)
1249 goto out;
1250
1251 if (new_value)
1252 new_value = 1;
1253
1254 fsi->bool_pending_values[index] = new_value;
1255 length = count;
1256
1257out:
1258 mutex_unlock(&fsi->mutex);
1259 kfree(page);
1260 return length;
1261}
1262
1263static const struct file_operations sel_bool_ops = {
1264 .read = sel_read_bool,
1265 .write = sel_write_bool,
1266 .llseek = generic_file_llseek,
1267};
1268
1269static ssize_t sel_commit_bools_write(struct file *filep,
1270 const char __user *buf,
1271 size_t count, loff_t *ppos)
1272{
1273 struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
1274 char *page = NULL;
1275 ssize_t length;
1276 int new_value;
1277
1278 if (count >= PAGE_SIZE)
1279 return -ENOMEM;
1280
1281 /* No partial writes. */
1282 if (*ppos != 0)
1283 return -EINVAL;
1284
1285 page = memdup_user_nul(buf, count);
1286 if (IS_ERR(page))
1287 return PTR_ERR(page);
1288
1289 mutex_lock(&fsi->mutex);
1290
1291 length = avc_has_perm(&selinux_state,
1292 current_sid(), SECINITSID_SECURITY,
1293 SECCLASS_SECURITY, SECURITY__SETBOOL,
1294 NULL);
1295 if (length)
1296 goto out;
1297
1298 length = -EINVAL;
1299 if (sscanf(page, "%d", &new_value) != 1)
1300 goto out;
1301
1302 length = 0;
1303 if (new_value && fsi->bool_pending_values)
1304 length = security_set_bools(fsi->state, fsi->bool_num,
1305 fsi->bool_pending_values);
1306
1307 if (!length)
1308 length = count;
1309
1310out:
1311 mutex_unlock(&fsi->mutex);
1312 kfree(page);
1313 return length;
1314}
1315
1316static const struct file_operations sel_commit_bools_ops = {
1317 .write = sel_commit_bools_write,
1318 .llseek = generic_file_llseek,
1319};
1320
1321static void sel_remove_entries(struct dentry *de)
1322{
1323 d_genocide(de);
1324 shrink_dcache_parent(de);
1325}
1326
1327#define BOOL_DIR_NAME "booleans"
1328
1329static int sel_make_bools(struct selinux_fs_info *fsi)
1330{
1331 int i, ret;
1332 ssize_t len;
1333 struct dentry *dentry = NULL;
1334 struct dentry *dir = fsi->bool_dir;
1335 struct inode *inode = NULL;
1336 struct inode_security_struct *isec;
1337 char **names = NULL, *page;
1338 int num;
1339 int *values = NULL;
1340 u32 sid;
1341
1342 /* remove any existing files */
1343 for (i = 0; i < fsi->bool_num; i++)
1344 kfree(fsi->bool_pending_names[i]);
1345 kfree(fsi->bool_pending_names);
1346 kfree(fsi->bool_pending_values);
1347 fsi->bool_num = 0;
1348 fsi->bool_pending_names = NULL;
1349 fsi->bool_pending_values = NULL;
1350
1351 sel_remove_entries(dir);
1352
1353 ret = -ENOMEM;
1354 page = (char *)get_zeroed_page(GFP_KERNEL);
1355 if (!page)
1356 goto out;
1357
1358 ret = security_get_bools(fsi->state, &num, &names, &values);
1359 if (ret)
1360 goto out;
1361
1362 for (i = 0; i < num; i++) {
1363 ret = -ENOMEM;
1364 dentry = d_alloc_name(dir, names[i]);
1365 if (!dentry)
1366 goto out;
1367
1368 ret = -ENOMEM;
1369 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1370 if (!inode) {
1371 dput(dentry);
1372 goto out;
1373 }
1374
1375 ret = -ENAMETOOLONG;
1376 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
1377 if (len >= PAGE_SIZE) {
1378 dput(dentry);
1379 iput(inode);
1380 goto out;
1381 }
1382
1383 isec = selinux_inode(inode);
1384 ret = security_genfs_sid(fsi->state, "selinuxfs", page,
1385 SECCLASS_FILE, &sid);
1386 if (ret) {
1387 pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n",
1388 page);
1389 sid = SECINITSID_SECURITY;
1390 }
1391
1392 isec->sid = sid;
1393 isec->initialized = LABEL_INITIALIZED;
1394 inode->i_fop = &sel_bool_ops;
1395 inode->i_ino = i|SEL_BOOL_INO_OFFSET;
1396 d_add(dentry, inode);
1397 }
1398 fsi->bool_num = num;
1399 fsi->bool_pending_names = names;
1400 fsi->bool_pending_values = values;
1401
1402 free_page((unsigned long)page);
1403 return 0;
1404out:
1405 free_page((unsigned long)page);
1406
1407 if (names) {
1408 for (i = 0; i < num; i++)
1409 kfree(names[i]);
1410 kfree(names);
1411 }
1412 kfree(values);
1413 sel_remove_entries(dir);
1414
1415 return ret;
1416}
1417
1418static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1419 size_t count, loff_t *ppos)
1420{
1421 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
1422 struct selinux_state *state = fsi->state;
1423 char tmpbuf[TMPBUFLEN];
1424 ssize_t length;
1425
1426 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1427 avc_get_cache_threshold(state->avc));
1428 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1429}
1430
1431static ssize_t sel_write_avc_cache_threshold(struct file *file,
1432 const char __user *buf,
1433 size_t count, loff_t *ppos)
1434
1435{
1436 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1437 struct selinux_state *state = fsi->state;
1438 char *page;
1439 ssize_t ret;
1440 unsigned int new_value;
1441
1442 ret = avc_has_perm(&selinux_state,
1443 current_sid(), SECINITSID_SECURITY,
1444 SECCLASS_SECURITY, SECURITY__SETSECPARAM,
1445 NULL);
1446 if (ret)
1447 return ret;
1448
1449 if (count >= PAGE_SIZE)
1450 return -ENOMEM;
1451
1452 /* No partial writes. */
1453 if (*ppos != 0)
1454 return -EINVAL;
1455
1456 page = memdup_user_nul(buf, count);
1457 if (IS_ERR(page))
1458 return PTR_ERR(page);
1459
1460 ret = -EINVAL;
1461 if (sscanf(page, "%u", &new_value) != 1)
1462 goto out;
1463
1464 avc_set_cache_threshold(state->avc, new_value);
1465
1466 ret = count;
1467out:
1468 kfree(page);
1469 return ret;
1470}
1471
1472static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1473 size_t count, loff_t *ppos)
1474{
1475 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
1476 struct selinux_state *state = fsi->state;
1477 char *page;
1478 ssize_t length;
1479
1480 page = (char *)__get_free_page(GFP_KERNEL);
1481 if (!page)
1482 return -ENOMEM;
1483
1484 length = avc_get_hash_stats(state->avc, page);
1485 if (length >= 0)
1486 length = simple_read_from_buffer(buf, count, ppos, page, length);
1487 free_page((unsigned long)page);
1488
1489 return length;
1490}
1491
1492static ssize_t sel_read_sidtab_hash_stats(struct file *filp, char __user *buf,
1493 size_t count, loff_t *ppos)
1494{
1495 struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
1496 struct selinux_state *state = fsi->state;
1497 char *page;
1498 ssize_t length;
1499
1500 page = (char *)__get_free_page(GFP_KERNEL);
1501 if (!page)
1502 return -ENOMEM;
1503
1504 length = security_sidtab_hash_stats(state, page);
1505 if (length >= 0)
1506 length = simple_read_from_buffer(buf, count, ppos, page,
1507 length);
1508 free_page((unsigned long)page);
1509
1510 return length;
1511}
1512
1513static const struct file_operations sel_sidtab_hash_stats_ops = {
1514 .read = sel_read_sidtab_hash_stats,
1515 .llseek = generic_file_llseek,
1516};
1517
1518static const struct file_operations sel_avc_cache_threshold_ops = {
1519 .read = sel_read_avc_cache_threshold,
1520 .write = sel_write_avc_cache_threshold,
1521 .llseek = generic_file_llseek,
1522};
1523
1524static const struct file_operations sel_avc_hash_stats_ops = {
1525 .read = sel_read_avc_hash_stats,
1526 .llseek = generic_file_llseek,
1527};
1528
1529#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1530static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1531{
1532 int cpu;
1533
1534 for (cpu = *idx; cpu < nr_cpu_ids; ++cpu) {
1535 if (!cpu_possible(cpu))
1536 continue;
1537 *idx = cpu + 1;
1538 return &per_cpu(avc_cache_stats, cpu);
1539 }
1540 (*idx)++;
1541 return NULL;
1542}
1543
1544static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1545{
1546 loff_t n = *pos - 1;
1547
1548 if (*pos == 0)
1549 return SEQ_START_TOKEN;
1550
1551 return sel_avc_get_stat_idx(&n);
1552}
1553
1554static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1555{
1556 return sel_avc_get_stat_idx(pos);
1557}
1558
1559static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1560{
1561 struct avc_cache_stats *st = v;
1562
1563 if (v == SEQ_START_TOKEN) {
1564 seq_puts(seq,
1565 "lookups hits misses allocations reclaims frees\n");
1566 } else {
1567 unsigned int lookups = st->lookups;
1568 unsigned int misses = st->misses;
1569 unsigned int hits = lookups - misses;
1570 seq_printf(seq, "%u %u %u %u %u %u\n", lookups,
1571 hits, misses, st->allocations,
1572 st->reclaims, st->frees);
1573 }
1574 return 0;
1575}
1576
1577static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1578{ }
1579
1580static const struct seq_operations sel_avc_cache_stats_seq_ops = {
1581 .start = sel_avc_stats_seq_start,
1582 .next = sel_avc_stats_seq_next,
1583 .show = sel_avc_stats_seq_show,
1584 .stop = sel_avc_stats_seq_stop,
1585};
1586
1587static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1588{
1589 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1590}
1591
1592static const struct file_operations sel_avc_cache_stats_ops = {
1593 .open = sel_open_avc_cache_stats,
1594 .read = seq_read,
1595 .llseek = seq_lseek,
1596 .release = seq_release,
1597};
1598#endif
1599
1600static int sel_make_avc_files(struct dentry *dir)
1601{
1602 struct super_block *sb = dir->d_sb;
1603 struct selinux_fs_info *fsi = sb->s_fs_info;
1604 int i;
1605 static const struct tree_descr files[] = {
1606 { "cache_threshold",
1607 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1608 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1609#ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1610 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1611#endif
1612 };
1613
1614 for (i = 0; i < ARRAY_SIZE(files); i++) {
1615 struct inode *inode;
1616 struct dentry *dentry;
1617
1618 dentry = d_alloc_name(dir, files[i].name);
1619 if (!dentry)
1620 return -ENOMEM;
1621
1622 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1623 if (!inode) {
1624 dput(dentry);
1625 return -ENOMEM;
1626 }
1627
1628 inode->i_fop = files[i].ops;
1629 inode->i_ino = ++fsi->last_ino;
1630 d_add(dentry, inode);
1631 }
1632
1633 return 0;
1634}
1635
1636static int sel_make_ss_files(struct dentry *dir)
1637{
1638 struct super_block *sb = dir->d_sb;
1639 struct selinux_fs_info *fsi = sb->s_fs_info;
1640 int i;
1641 static struct tree_descr files[] = {
1642 { "sidtab_hash_stats", &sel_sidtab_hash_stats_ops, S_IRUGO },
1643 };
1644
1645 for (i = 0; i < ARRAY_SIZE(files); i++) {
1646 struct inode *inode;
1647 struct dentry *dentry;
1648
1649 dentry = d_alloc_name(dir, files[i].name);
1650 if (!dentry)
1651 return -ENOMEM;
1652
1653 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1654 if (!inode) {
1655 dput(dentry);
1656 return -ENOMEM;
1657 }
1658
1659 inode->i_fop = files[i].ops;
1660 inode->i_ino = ++fsi->last_ino;
1661 d_add(dentry, inode);
1662 }
1663
1664 return 0;
1665}
1666
1667static ssize_t sel_read_initcon(struct file *file, char __user *buf,
1668 size_t count, loff_t *ppos)
1669{
1670 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1671 char *con;
1672 u32 sid, len;
1673 ssize_t ret;
1674
1675 sid = file_inode(file)->i_ino&SEL_INO_MASK;
1676 ret = security_sid_to_context(fsi->state, sid, &con, &len);
1677 if (ret)
1678 return ret;
1679
1680 ret = simple_read_from_buffer(buf, count, ppos, con, len);
1681 kfree(con);
1682 return ret;
1683}
1684
1685static const struct file_operations sel_initcon_ops = {
1686 .read = sel_read_initcon,
1687 .llseek = generic_file_llseek,
1688};
1689
1690static int sel_make_initcon_files(struct dentry *dir)
1691{
1692 int i;
1693
1694 for (i = 1; i <= SECINITSID_NUM; i++) {
1695 struct inode *inode;
1696 struct dentry *dentry;
1697 dentry = d_alloc_name(dir, security_get_initial_sid_context(i));
1698 if (!dentry)
1699 return -ENOMEM;
1700
1701 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1702 if (!inode) {
1703 dput(dentry);
1704 return -ENOMEM;
1705 }
1706
1707 inode->i_fop = &sel_initcon_ops;
1708 inode->i_ino = i|SEL_INITCON_INO_OFFSET;
1709 d_add(dentry, inode);
1710 }
1711
1712 return 0;
1713}
1714
1715static inline unsigned long sel_class_to_ino(u16 class)
1716{
1717 return (class * (SEL_VEC_MAX + 1)) | SEL_CLASS_INO_OFFSET;
1718}
1719
1720static inline u16 sel_ino_to_class(unsigned long ino)
1721{
1722 return (ino & SEL_INO_MASK) / (SEL_VEC_MAX + 1);
1723}
1724
1725static inline unsigned long sel_perm_to_ino(u16 class, u32 perm)
1726{
1727 return (class * (SEL_VEC_MAX + 1) + perm) | SEL_CLASS_INO_OFFSET;
1728}
1729
1730static inline u32 sel_ino_to_perm(unsigned long ino)
1731{
1732 return (ino & SEL_INO_MASK) % (SEL_VEC_MAX + 1);
1733}
1734
1735static ssize_t sel_read_class(struct file *file, char __user *buf,
1736 size_t count, loff_t *ppos)
1737{
1738 unsigned long ino = file_inode(file)->i_ino;
1739 char res[TMPBUFLEN];
1740 ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_class(ino));
1741 return simple_read_from_buffer(buf, count, ppos, res, len);
1742}
1743
1744static const struct file_operations sel_class_ops = {
1745 .read = sel_read_class,
1746 .llseek = generic_file_llseek,
1747};
1748
1749static ssize_t sel_read_perm(struct file *file, char __user *buf,
1750 size_t count, loff_t *ppos)
1751{
1752 unsigned long ino = file_inode(file)->i_ino;
1753 char res[TMPBUFLEN];
1754 ssize_t len = snprintf(res, sizeof(res), "%d", sel_ino_to_perm(ino));
1755 return simple_read_from_buffer(buf, count, ppos, res, len);
1756}
1757
1758static const struct file_operations sel_perm_ops = {
1759 .read = sel_read_perm,
1760 .llseek = generic_file_llseek,
1761};
1762
1763static ssize_t sel_read_policycap(struct file *file, char __user *buf,
1764 size_t count, loff_t *ppos)
1765{
1766 struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
1767 int value;
1768 char tmpbuf[TMPBUFLEN];
1769 ssize_t length;
1770 unsigned long i_ino = file_inode(file)->i_ino;
1771
1772 value = security_policycap_supported(fsi->state, i_ino & SEL_INO_MASK);
1773 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
1774
1775 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1776}
1777
1778static const struct file_operations sel_policycap_ops = {
1779 .read = sel_read_policycap,
1780 .llseek = generic_file_llseek,
1781};
1782
1783static int sel_make_perm_files(char *objclass, int classvalue,
1784 struct dentry *dir)
1785{
1786 struct selinux_fs_info *fsi = dir->d_sb->s_fs_info;
1787 int i, rc, nperms;
1788 char **perms;
1789
1790 rc = security_get_permissions(fsi->state, objclass, &perms, &nperms);
1791 if (rc)
1792 return rc;
1793
1794 for (i = 0; i < nperms; i++) {
1795 struct inode *inode;
1796 struct dentry *dentry;
1797
1798 rc = -ENOMEM;
1799 dentry = d_alloc_name(dir, perms[i]);
1800 if (!dentry)
1801 goto out;
1802
1803 rc = -ENOMEM;
1804 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1805 if (!inode) {
1806 dput(dentry);
1807 goto out;
1808 }
1809
1810 inode->i_fop = &sel_perm_ops;
1811 /* i+1 since perm values are 1-indexed */
1812 inode->i_ino = sel_perm_to_ino(classvalue, i + 1);
1813 d_add(dentry, inode);
1814 }
1815 rc = 0;
1816out:
1817 for (i = 0; i < nperms; i++)
1818 kfree(perms[i]);
1819 kfree(perms);
1820 return rc;
1821}
1822
1823static int sel_make_class_dir_entries(char *classname, int index,
1824 struct dentry *dir)
1825{
1826 struct super_block *sb = dir->d_sb;
1827 struct selinux_fs_info *fsi = sb->s_fs_info;
1828 struct dentry *dentry = NULL;
1829 struct inode *inode = NULL;
1830 int rc;
1831
1832 dentry = d_alloc_name(dir, "index");
1833 if (!dentry)
1834 return -ENOMEM;
1835
1836 inode = sel_make_inode(dir->d_sb, S_IFREG|S_IRUGO);
1837 if (!inode) {
1838 dput(dentry);
1839 return -ENOMEM;
1840 }
1841
1842 inode->i_fop = &sel_class_ops;
1843 inode->i_ino = sel_class_to_ino(index);
1844 d_add(dentry, inode);
1845
1846 dentry = sel_make_dir(dir, "perms", &fsi->last_class_ino);
1847 if (IS_ERR(dentry))
1848 return PTR_ERR(dentry);
1849
1850 rc = sel_make_perm_files(classname, index, dentry);
1851
1852 return rc;
1853}
1854
1855static int sel_make_classes(struct selinux_fs_info *fsi)
1856{
1857
1858 int rc, nclasses, i;
1859 char **classes;
1860
1861 /* delete any existing entries */
1862 sel_remove_entries(fsi->class_dir);
1863
1864 rc = security_get_classes(fsi->state, &classes, &nclasses);
1865 if (rc)
1866 return rc;
1867
1868 /* +2 since classes are 1-indexed */
1869 fsi->last_class_ino = sel_class_to_ino(nclasses + 2);
1870
1871 for (i = 0; i < nclasses; i++) {
1872 struct dentry *class_name_dir;
1873
1874 class_name_dir = sel_make_dir(fsi->class_dir, classes[i],
1875 &fsi->last_class_ino);
1876 if (IS_ERR(class_name_dir)) {
1877 rc = PTR_ERR(class_name_dir);
1878 goto out;
1879 }
1880
1881 /* i+1 since class values are 1-indexed */
1882 rc = sel_make_class_dir_entries(classes[i], i + 1,
1883 class_name_dir);
1884 if (rc)
1885 goto out;
1886 }
1887 rc = 0;
1888out:
1889 for (i = 0; i < nclasses; i++)
1890 kfree(classes[i]);
1891 kfree(classes);
1892 return rc;
1893}
1894
1895static int sel_make_policycap(struct selinux_fs_info *fsi)
1896{
1897 unsigned int iter;
1898 struct dentry *dentry = NULL;
1899 struct inode *inode = NULL;
1900
1901 sel_remove_entries(fsi->policycap_dir);
1902
1903 for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
1904 if (iter < ARRAY_SIZE(selinux_policycap_names))
1905 dentry = d_alloc_name(fsi->policycap_dir,
1906 selinux_policycap_names[iter]);
1907 else
1908 dentry = d_alloc_name(fsi->policycap_dir, "unknown");
1909
1910 if (dentry == NULL)
1911 return -ENOMEM;
1912
1913 inode = sel_make_inode(fsi->sb, S_IFREG | 0444);
1914 if (inode == NULL) {
1915 dput(dentry);
1916 return -ENOMEM;
1917 }
1918
1919 inode->i_fop = &sel_policycap_ops;
1920 inode->i_ino = iter | SEL_POLICYCAP_INO_OFFSET;
1921 d_add(dentry, inode);
1922 }
1923
1924 return 0;
1925}
1926
1927static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
1928 unsigned long *ino)
1929{
1930 struct dentry *dentry = d_alloc_name(dir, name);
1931 struct inode *inode;
1932
1933 if (!dentry)
1934 return ERR_PTR(-ENOMEM);
1935
1936 inode = sel_make_inode(dir->d_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1937 if (!inode) {
1938 dput(dentry);
1939 return ERR_PTR(-ENOMEM);
1940 }
1941
1942 inode->i_op = &simple_dir_inode_operations;
1943 inode->i_fop = &simple_dir_operations;
1944 inode->i_ino = ++(*ino);
1945 /* directory inodes start off with i_nlink == 2 (for "." entry) */
1946 inc_nlink(inode);
1947 d_add(dentry, inode);
1948 /* bump link count on parent directory, too */
1949 inc_nlink(d_inode(dir));
1950
1951 return dentry;
1952}
1953
1954#define NULL_FILE_NAME "null"
1955
1956static int sel_fill_super(struct super_block *sb, struct fs_context *fc)
1957{
1958 struct selinux_fs_info *fsi;
1959 int ret;
1960 struct dentry *dentry;
1961 struct inode *inode;
1962 struct inode_security_struct *isec;
1963
1964 static const struct tree_descr selinux_files[] = {
1965 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1966 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
1967 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
1968 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1969 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1970 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1971 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1972 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1973 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1974 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1975 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1976 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1977 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
1978 [SEL_REJECT_UNKNOWN] = {"reject_unknown", &sel_handle_unknown_ops, S_IRUGO},
1979 [SEL_DENY_UNKNOWN] = {"deny_unknown", &sel_handle_unknown_ops, S_IRUGO},
1980 [SEL_STATUS] = {"status", &sel_handle_status_ops, S_IRUGO},
1981 [SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO},
1982 [SEL_VALIDATE_TRANS] = {"validatetrans", &sel_transition_ops,
1983 S_IWUGO},
1984 /* last one */ {""}
1985 };
1986
1987 ret = selinux_fs_info_create(sb);
1988 if (ret)
1989 goto err;
1990
1991 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1992 if (ret)
1993 goto err;
1994
1995 fsi = sb->s_fs_info;
1996 fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino);
1997 if (IS_ERR(fsi->bool_dir)) {
1998 ret = PTR_ERR(fsi->bool_dir);
1999 fsi->bool_dir = NULL;
2000 goto err;
2001 }
2002
2003 ret = -ENOMEM;
2004 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
2005 if (!dentry)
2006 goto err;
2007
2008 ret = -ENOMEM;
2009 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
2010 if (!inode) {
2011 dput(dentry);
2012 goto err;
2013 }
2014
2015 inode->i_ino = ++fsi->last_ino;
2016 isec = selinux_inode(inode);
2017 isec->sid = SECINITSID_DEVNULL;
2018 isec->sclass = SECCLASS_CHR_FILE;
2019 isec->initialized = LABEL_INITIALIZED;
2020
2021 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
2022 d_add(dentry, inode);
2023
2024 dentry = sel_make_dir(sb->s_root, "avc", &fsi->last_ino);
2025 if (IS_ERR(dentry)) {
2026 ret = PTR_ERR(dentry);
2027 goto err;
2028 }
2029
2030 ret = sel_make_avc_files(dentry);
2031
2032 dentry = sel_make_dir(sb->s_root, "ss", &fsi->last_ino);
2033 if (IS_ERR(dentry)) {
2034 ret = PTR_ERR(dentry);
2035 goto err;
2036 }
2037
2038 ret = sel_make_ss_files(dentry);
2039 if (ret)
2040 goto err;
2041
2042 dentry = sel_make_dir(sb->s_root, "initial_contexts", &fsi->last_ino);
2043 if (IS_ERR(dentry)) {
2044 ret = PTR_ERR(dentry);
2045 goto err;
2046 }
2047
2048 ret = sel_make_initcon_files(dentry);
2049 if (ret)
2050 goto err;
2051
2052 fsi->class_dir = sel_make_dir(sb->s_root, "class", &fsi->last_ino);
2053 if (IS_ERR(fsi->class_dir)) {
2054 ret = PTR_ERR(fsi->class_dir);
2055 fsi->class_dir = NULL;
2056 goto err;
2057 }
2058
2059 fsi->policycap_dir = sel_make_dir(sb->s_root, "policy_capabilities",
2060 &fsi->last_ino);
2061 if (IS_ERR(fsi->policycap_dir)) {
2062 ret = PTR_ERR(fsi->policycap_dir);
2063 fsi->policycap_dir = NULL;
2064 goto err;
2065 }
2066
2067 ret = sel_make_policy_nodes(fsi);
2068 if (ret)
2069 goto err;
2070 return 0;
2071err:
2072 pr_err("SELinux: %s: failed while creating inodes\n",
2073 __func__);
2074
2075 selinux_fs_info_free(sb);
2076
2077 return ret;
2078}
2079
2080static int sel_get_tree(struct fs_context *fc)
2081{
2082 return get_tree_single(fc, sel_fill_super);
2083}
2084
2085static const struct fs_context_operations sel_context_ops = {
2086 .get_tree = sel_get_tree,
2087};
2088
2089static int sel_init_fs_context(struct fs_context *fc)
2090{
2091 fc->ops = &sel_context_ops;
2092 return 0;
2093}
2094
2095static void sel_kill_sb(struct super_block *sb)
2096{
2097 selinux_fs_info_free(sb);
2098 kill_litter_super(sb);
2099}
2100
2101static struct file_system_type sel_fs_type = {
2102 .name = "selinuxfs",
2103 .init_fs_context = sel_init_fs_context,
2104 .kill_sb = sel_kill_sb,
2105};
2106
2107struct vfsmount *selinuxfs_mount;
2108struct path selinux_null;
2109
2110static int __init init_sel_fs(void)
2111{
2112 struct qstr null_name = QSTR_INIT(NULL_FILE_NAME,
2113 sizeof(NULL_FILE_NAME)-1);
2114 int err;
2115
2116 if (!selinux_enabled)
2117 return 0;
2118
2119 err = sysfs_create_mount_point(fs_kobj, "selinux");
2120 if (err)
2121 return err;
2122
2123 err = register_filesystem(&sel_fs_type);
2124 if (err) {
2125 sysfs_remove_mount_point(fs_kobj, "selinux");
2126 return err;
2127 }
2128
2129 selinux_null.mnt = selinuxfs_mount = kern_mount(&sel_fs_type);
2130 if (IS_ERR(selinuxfs_mount)) {
2131 pr_err("selinuxfs: could not mount!\n");
2132 err = PTR_ERR(selinuxfs_mount);
2133 selinuxfs_mount = NULL;
2134 }
2135 selinux_null.dentry = d_hash_and_lookup(selinux_null.mnt->mnt_root,
2136 &null_name);
2137 if (IS_ERR(selinux_null.dentry)) {
2138 pr_err("selinuxfs: could not lookup null!\n");
2139 err = PTR_ERR(selinux_null.dentry);
2140 selinux_null.dentry = NULL;
2141 }
2142
2143 return err;
2144}
2145
2146__initcall(init_sel_fs);
2147
2148#ifdef CONFIG_SECURITY_SELINUX_DISABLE
2149void exit_sel_fs(void)
2150{
2151 sysfs_remove_mount_point(fs_kobj, "selinux");
2152 dput(selinux_null.dentry);
2153 kern_unmount(selinuxfs_mount);
2154 unregister_filesystem(&sel_fs_type);
2155}
2156#endif