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