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