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