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