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