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