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