patch-2_6_7-vs1_9_1_12
[linux-2.6.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  *      This program is free software; you can redistribute it and/or modify
7  *      it under the terms of the GNU General Public License as published by
8  *      the Free Software Foundation, version 2.
9  */
10
11 #include <linux/config.h>
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/init.h>
18 #include <linux/string.h>
19 #include <linux/security.h>
20 #include <asm/uaccess.h>
21 #include <asm/semaphore.h>
22
23 /* selinuxfs pseudo filesystem for exporting the security policy API.
24    Based on the proc code and the fs/nfsd/nfsctl.c code. */
25
26 #include "flask.h"
27 #include "avc.h"
28 #include "avc_ss.h"
29 #include "security.h"
30 #include "objsec.h"
31 #include "conditional.h"
32
33 static DECLARE_MUTEX(sel_sem);
34
35 /* global data for booleans */
36 static struct dentry *bool_dir = NULL;
37 static int bool_num = 0;
38 static int *bool_pending_values = NULL;
39
40 extern void selnl_notify_setenforce(int val);
41
42 /* Check whether a task is allowed to use a security operation. */
43 int task_has_security(struct task_struct *tsk,
44                       u32 perms)
45 {
46         struct task_security_struct *tsec;
47
48         tsec = tsk->security;
49         if (!tsec)
50                 return -EACCES;
51
52         return avc_has_perm(tsec->sid, SECINITSID_SECURITY,
53                             SECCLASS_SECURITY, perms, NULL, NULL);
54 }
55
56 enum sel_inos {
57         SEL_ROOT_INO = 2,
58         SEL_LOAD,       /* load policy */
59         SEL_ENFORCE,    /* get or set enforcing status */
60         SEL_CONTEXT,    /* validate context */
61         SEL_ACCESS,     /* compute access decision */
62         SEL_CREATE,     /* compute create labeling decision */
63         SEL_RELABEL,    /* compute relabeling decision */
64         SEL_USER,       /* compute reachable user contexts */
65         SEL_POLICYVERS, /* return policy version for this kernel */
66         SEL_COMMIT_BOOLS, /* commit new boolean values */
67         SEL_MLS,        /* return if MLS policy is enabled */
68         SEL_DISABLE     /* disable SELinux until next reboot */
69 };
70
71 static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
72                                 size_t count, loff_t *ppos)
73 {
74         char *page;
75         ssize_t length;
76         ssize_t end;
77
78         if (count < 0 || count > PAGE_SIZE)
79                 return -EINVAL;
80         if (!(page = (char*)__get_free_page(GFP_KERNEL)))
81                 return -ENOMEM;
82         memset(page, 0, PAGE_SIZE);
83
84         length = scnprintf(page, PAGE_SIZE, "%d", selinux_enforcing);
85         if (length < 0) {
86                 free_page((unsigned long)page);
87                 return length;
88         }
89
90         if (*ppos >= length) {
91                 free_page((unsigned long)page);
92                 return 0;
93         }
94         if (count + *ppos > length)
95                 count = length - *ppos;
96         end = count + *ppos;
97         if (copy_to_user(buf, (char *) page + *ppos, count)) {
98                 count = -EFAULT;
99                 goto out;
100         }
101         *ppos = end;
102 out:
103         free_page((unsigned long)page);
104         return count;
105 }
106
107 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
108 static ssize_t sel_write_enforce(struct file * file, const char __user * buf,
109                                  size_t count, loff_t *ppos)
110
111 {
112         char *page;
113         ssize_t length;
114         int new_value;
115
116         if (count < 0 || count >= PAGE_SIZE)
117                 return -ENOMEM;
118         if (*ppos != 0) {
119                 /* No partial writes. */
120                 return -EINVAL;
121         }
122         page = (char*)__get_free_page(GFP_KERNEL);
123         if (!page)
124                 return -ENOMEM;
125         memset(page, 0, PAGE_SIZE);
126         length = -EFAULT;
127         if (copy_from_user(page, buf, count))
128                 goto out;
129
130         length = -EINVAL;
131         if (sscanf(page, "%d", &new_value) != 1)
132                 goto out;
133
134         if (new_value != selinux_enforcing) {
135                 length = task_has_security(current, SECURITY__SETENFORCE);
136                 if (length)
137                         goto out;
138                 selinux_enforcing = new_value;
139                 if (selinux_enforcing)
140                         avc_ss_reset(0);
141                 selnl_notify_setenforce(selinux_enforcing);
142         }
143         length = count;
144 out:
145         free_page((unsigned long) page);
146         return length;
147 }
148 #else
149 #define sel_write_enforce NULL
150 #endif
151
152 static struct file_operations sel_enforce_ops = {
153         .read           = sel_read_enforce,
154         .write          = sel_write_enforce,
155 };
156
157 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
158 static ssize_t sel_write_disable(struct file * file, const char __user * buf,
159                                  size_t count, loff_t *ppos)
160
161 {
162         char *page;
163         ssize_t length;
164         int new_value;
165         extern int selinux_disable(void);
166
167         if (count < 0 || count >= PAGE_SIZE)
168                 return -ENOMEM;
169         if (*ppos != 0) {
170                 /* No partial writes. */
171                 return -EINVAL;
172         }
173         page = (char*)__get_free_page(GFP_KERNEL);
174         if (!page)
175                 return -ENOMEM;
176         memset(page, 0, PAGE_SIZE);
177         length = -EFAULT;
178         if (copy_from_user(page, buf, count))
179                 goto out;
180
181         length = -EINVAL;
182         if (sscanf(page, "%d", &new_value) != 1)
183                 goto out;
184
185         if (new_value) {
186                 length = selinux_disable();
187                 if (length < 0)
188                         goto out;
189         }
190
191         length = count;
192 out:
193         free_page((unsigned long) page);
194         return length;
195 }
196 #else
197 #define sel_write_disable NULL
198 #endif
199
200 static struct file_operations sel_disable_ops = {
201         .write          = sel_write_disable,
202 };
203
204 static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
205                                    size_t count, loff_t *ppos)
206 {
207         char *page;
208         ssize_t length;
209         ssize_t end;
210
211         if (count < 0 || count > PAGE_SIZE)
212                 return -EINVAL;
213         if (!(page = (char*)__get_free_page(GFP_KERNEL)))
214                 return -ENOMEM;
215         memset(page, 0, PAGE_SIZE);
216
217         length = scnprintf(page, PAGE_SIZE, "%u", POLICYDB_VERSION_MAX);
218         if (length < 0) {
219                 free_page((unsigned long)page);
220                 return length;
221         }
222
223         if (*ppos >= length) {
224                 free_page((unsigned long)page);
225                 return 0;
226         }
227         if (count + *ppos > length)
228                 count = length - *ppos;
229         end = count + *ppos;
230         if (copy_to_user(buf, (char *) page + *ppos, count)) {
231                 count = -EFAULT;
232                 goto out;
233         }
234         *ppos = end;
235 out:
236         free_page((unsigned long)page);
237         return count;
238 }
239
240 static struct file_operations sel_policyvers_ops = {
241         .read           = sel_read_policyvers,
242 };
243
244 /* declaration for sel_write_load */
245 static int sel_make_bools(void);
246
247 static ssize_t sel_read_mls(struct file *filp, char __user *buf,
248                                 size_t count, loff_t *ppos)
249 {
250         char *page;
251         ssize_t length;
252         ssize_t end;
253
254         if (count < 0 || count > PAGE_SIZE)
255                 return -EINVAL;
256         if (!(page = (char*)__get_free_page(GFP_KERNEL)))
257                 return -ENOMEM;
258         memset(page, 0, PAGE_SIZE);
259
260         length = scnprintf(page, PAGE_SIZE, "%d", selinux_mls_enabled);
261         if (length < 0) {
262                 free_page((unsigned long)page);
263                 return length;
264         }
265
266         if (*ppos >= length) {
267                 free_page((unsigned long)page);
268                 return 0;
269         }
270         if (count + *ppos > length)
271                 count = length - *ppos;
272         end = count + *ppos;
273         if (copy_to_user(buf, (char *) page + *ppos, count)) {
274                 count = -EFAULT;
275                 goto out;
276         }
277         *ppos = end;
278 out:
279         free_page((unsigned long)page);
280         return count;
281 }
282
283 static struct file_operations sel_mls_ops = {
284         .read           = sel_read_mls,
285 };
286
287 static ssize_t sel_write_load(struct file * file, const char __user * buf,
288                               size_t count, loff_t *ppos)
289
290 {
291         int ret;
292         ssize_t length;
293         void *data = NULL;
294
295         down(&sel_sem);
296
297         length = task_has_security(current, SECURITY__LOAD_POLICY);
298         if (length)
299                 goto out;
300
301         if (*ppos != 0) {
302                 /* No partial writes. */
303                 length = -EINVAL;
304                 goto out;
305         }
306
307         if ((count < 0) || (count > 64 * 1024 * 1024)
308             || (data = vmalloc(count)) == NULL) {
309                 length = -ENOMEM;
310                 goto out;
311         }
312
313         length = -EFAULT;
314         if (copy_from_user(data, buf, count) != 0)
315                 goto out;
316
317         length = security_load_policy(data, count);
318         if (length)
319                 goto out;
320
321         ret = sel_make_bools();
322         if (ret)
323                 length = ret;
324         else
325                 length = count;
326 out:
327         up(&sel_sem);
328         vfree(data);
329         return length;
330 }
331
332 static struct file_operations sel_load_ops = {
333         .write          = sel_write_load,
334 };
335
336
337 static ssize_t sel_write_context(struct file * file, const char __user * buf,
338                                  size_t count, loff_t *ppos)
339
340 {
341         char *page;
342         u32 sid;
343         ssize_t length;
344
345         length = task_has_security(current, SECURITY__CHECK_CONTEXT);
346         if (length)
347                 return length;
348
349         if (count < 0 || count >= PAGE_SIZE)
350                 return -ENOMEM;
351         if (*ppos != 0) {
352                 /* No partial writes. */
353                 return -EINVAL;
354         }
355         page = (char*)__get_free_page(GFP_KERNEL);
356         if (!page)
357                 return -ENOMEM;
358         memset(page, 0, PAGE_SIZE);
359         length = -EFAULT;
360         if (copy_from_user(page, buf, count))
361                 goto out;
362
363         length = security_context_to_sid(page, count, &sid);
364         if (length < 0)
365                 goto out;
366
367         length = count;
368 out:
369         free_page((unsigned long) page);
370         return length;
371 }
372
373 static struct file_operations sel_context_ops = {
374         .write          = sel_write_context,
375 };
376
377
378 /*
379  * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
380  */
381 static ssize_t sel_write_access(struct file * file, char *buf, size_t size);
382 static ssize_t sel_write_create(struct file * file, char *buf, size_t size);
383 static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size);
384 static ssize_t sel_write_user(struct file * file, char *buf, size_t size);
385
386 static ssize_t (*write_op[])(struct file *, char *, size_t) = {
387         [SEL_ACCESS] = sel_write_access,
388         [SEL_CREATE] = sel_write_create,
389         [SEL_RELABEL] = sel_write_relabel,
390         [SEL_USER] = sel_write_user,
391 };
392
393 /* an argresp is stored in an allocated page and holds the
394  * size of the argument or response, along with its content
395  */
396 struct argresp {
397         ssize_t size;
398         char data[0];
399 };
400
401 #define PAYLOAD_SIZE (PAGE_SIZE - sizeof(struct argresp))
402
403 /*
404  * transaction based IO methods.
405  * The file expects a single write which triggers the transaction, and then
406  * possibly a read which collects the result - which is stored in a
407  * file-local buffer.
408  */
409 static ssize_t TA_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
410 {
411         ino_t ino =  file->f_dentry->d_inode->i_ino;
412         struct argresp *ar;
413         ssize_t rv = 0;
414
415         if (ino >= sizeof(write_op)/sizeof(write_op[0]) || !write_op[ino])
416                 return -EINVAL;
417         if (file->private_data)
418                 return -EINVAL; /* only one write allowed per open */
419         if (size > PAYLOAD_SIZE - 1) /* allow one byte for null terminator */
420                 return -EFBIG;
421
422         ar = kmalloc(PAGE_SIZE, GFP_KERNEL);
423         if (!ar)
424                 return -ENOMEM;
425         memset(ar, 0, PAGE_SIZE); /* clear buffer, particularly last byte */
426         ar->size = 0;
427         down(&file->f_dentry->d_inode->i_sem);
428         if (file->private_data)
429                 rv = -EINVAL;
430         else
431                 file->private_data = ar;
432         up(&file->f_dentry->d_inode->i_sem);
433         if (rv) {
434                 kfree(ar);
435                 return rv;
436         }
437         if (copy_from_user(ar->data, buf, size))
438                 return -EFAULT;
439
440         rv =  write_op[ino](file, ar->data, size);
441         if (rv>0) {
442                 ar->size = rv;
443                 rv = size;
444         }
445         return rv;
446 }
447
448 static ssize_t TA_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
449 {
450         struct argresp *ar;
451         ssize_t rv = 0;
452
453         if (file->private_data == NULL)
454                 rv = TA_write(file, buf, 0, pos);
455         if (rv < 0)
456                 return rv;
457
458         ar = file->private_data;
459         if (!ar)
460                 return 0;
461         if (*pos >= ar->size)
462                 return 0;
463         if (*pos + size > ar->size)
464                 size = ar->size - *pos;
465         if (copy_to_user(buf, ar->data + *pos, size))
466                 return -EFAULT;
467         *pos += size;
468         return size;
469 }
470
471 static int TA_open(struct inode *inode, struct file *file)
472 {
473         file->private_data = NULL;
474         return 0;
475 }
476
477 static int TA_release(struct inode *inode, struct file *file)
478 {
479         void *p = file->private_data;
480         file->private_data = NULL;
481         kfree(p);
482         return 0;
483 }
484
485 static struct file_operations transaction_ops = {
486         .write          = TA_write,
487         .read           = TA_read,
488         .open           = TA_open,
489         .release        = TA_release,
490 };
491
492 /*
493  * payload - write methods
494  * If the method has a response, the response should be put in buf,
495  * and the length returned.  Otherwise return 0 or and -error.
496  */
497
498 static ssize_t sel_write_access(struct file * file, char *buf, size_t size)
499 {
500         char *scon, *tcon;
501         u32 ssid, tsid;
502         u16 tclass;
503         u32 req;
504         struct av_decision avd;
505         ssize_t length;
506
507         length = task_has_security(current, SECURITY__COMPUTE_AV);
508         if (length)
509                 return length;
510
511         length = -ENOMEM;
512         scon = kmalloc(size+1, GFP_KERNEL);
513         if (!scon)
514                 return length;
515         memset(scon, 0, size+1);
516
517         tcon = kmalloc(size+1, GFP_KERNEL);
518         if (!tcon)
519                 goto out;
520         memset(tcon, 0, size+1);
521
522         length = -EINVAL;
523         if (sscanf(buf, "%s %s %hu %x", scon, tcon, &tclass, &req) != 4)
524                 goto out2;
525
526         length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
527         if (length < 0)
528                 goto out2;
529         length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
530         if (length < 0)
531                 goto out2;
532
533         length = security_compute_av(ssid, tsid, tclass, req, &avd);
534         if (length < 0)
535                 goto out2;
536
537         length = scnprintf(buf, PAYLOAD_SIZE, "%x %x %x %x %u",
538                           avd.allowed, avd.decided,
539                           avd.auditallow, avd.auditdeny,
540                           avd.seqno);
541 out2:
542         kfree(tcon);
543 out:
544         kfree(scon);
545         return length;
546 }
547
548 static ssize_t sel_write_create(struct file * file, char *buf, size_t size)
549 {
550         char *scon, *tcon;
551         u32 ssid, tsid, newsid;
552         u16 tclass;
553         ssize_t length;
554         char *newcon;
555         u32 len;
556
557         length = task_has_security(current, SECURITY__COMPUTE_CREATE);
558         if (length)
559                 return length;
560
561         length = -ENOMEM;
562         scon = kmalloc(size+1, GFP_KERNEL);
563         if (!scon)
564                 return length;
565         memset(scon, 0, size+1);
566
567         tcon = kmalloc(size+1, GFP_KERNEL);
568         if (!tcon)
569                 goto out;
570         memset(tcon, 0, size+1);
571
572         length = -EINVAL;
573         if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
574                 goto out2;
575
576         length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
577         if (length < 0)
578                 goto out2;
579         length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
580         if (length < 0)
581                 goto out2;
582
583         length = security_transition_sid(ssid, tsid, tclass, &newsid);
584         if (length < 0)
585                 goto out2;
586
587         length = security_sid_to_context(newsid, &newcon, &len);
588         if (length < 0)
589                 goto out2;
590
591         if (len > PAYLOAD_SIZE) {
592                 printk(KERN_ERR "%s:  context size (%u) exceeds payload "
593                        "max\n", __FUNCTION__, len);
594                 length = -ERANGE;
595                 goto out3;
596         }
597
598         memcpy(buf, newcon, len);
599         length = len;
600 out3:
601         kfree(newcon);
602 out2:
603         kfree(tcon);
604 out:
605         kfree(scon);
606         return length;
607 }
608
609 static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size)
610 {
611         char *scon, *tcon;
612         u32 ssid, tsid, newsid;
613         u16 tclass;
614         ssize_t length;
615         char *newcon;
616         u32 len;
617
618         length = task_has_security(current, SECURITY__COMPUTE_RELABEL);
619         if (length)
620                 return length;
621
622         length = -ENOMEM;
623         scon = kmalloc(size+1, GFP_KERNEL);
624         if (!scon)
625                 return length;
626         memset(scon, 0, size+1);
627
628         tcon = kmalloc(size+1, GFP_KERNEL);
629         if (!tcon)
630                 goto out;
631         memset(tcon, 0, size+1);
632
633         length = -EINVAL;
634         if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
635                 goto out2;
636
637         length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
638         if (length < 0)
639                 goto out2;
640         length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
641         if (length < 0)
642                 goto out2;
643
644         length = security_change_sid(ssid, tsid, tclass, &newsid);
645         if (length < 0)
646                 goto out2;
647
648         length = security_sid_to_context(newsid, &newcon, &len);
649         if (length < 0)
650                 goto out2;
651
652         if (len > PAYLOAD_SIZE) {
653                 length = -ERANGE;
654                 goto out3;
655         }
656
657         memcpy(buf, newcon, len);
658         length = len;
659 out3:
660         kfree(newcon);
661 out2:
662         kfree(tcon);
663 out:
664         kfree(scon);
665         return length;
666 }
667
668 static ssize_t sel_write_user(struct file * file, char *buf, size_t size)
669 {
670         char *con, *user, *ptr;
671         u32 sid, *sids;
672         ssize_t length;
673         char *newcon;
674         int i, rc;
675         u32 len, nsids;
676
677         length = task_has_security(current, SECURITY__COMPUTE_USER);
678         if (length)
679                 return length;
680
681         length = -ENOMEM;
682         con = kmalloc(size+1, GFP_KERNEL);
683         if (!con)
684                 return length;
685         memset(con, 0, size+1);
686
687         user = kmalloc(size+1, GFP_KERNEL);
688         if (!user)
689                 goto out;
690         memset(user, 0, size+1);
691
692         length = -EINVAL;
693         if (sscanf(buf, "%s %s", con, user) != 2)
694                 goto out2;
695
696         length = security_context_to_sid(con, strlen(con)+1, &sid);
697         if (length < 0)
698                 goto out2;
699
700         length = security_get_user_sids(sid, user, &sids, &nsids);
701         if (length < 0)
702                 goto out2;
703
704         length = sprintf(buf, "%u", nsids) + 1;
705         ptr = buf + length;
706         for (i = 0; i < nsids; i++) {
707                 rc = security_sid_to_context(sids[i], &newcon, &len);
708                 if (rc) {
709                         length = rc;
710                         goto out3;
711                 }
712                 if ((length + len) >= PAYLOAD_SIZE) {
713                         kfree(newcon);
714                         length = -ERANGE;
715                         goto out3;
716                 }
717                 memcpy(ptr, newcon, len);
718                 kfree(newcon);
719                 ptr += len;
720                 length += len;
721         }
722 out3:
723         kfree(sids);
724 out2:
725         kfree(user);
726 out:
727         kfree(con);
728         return length;
729 }
730
731 static struct inode *sel_make_inode(struct super_block *sb, int mode)
732 {
733         struct inode *ret = new_inode(sb);
734
735         if (ret) {
736                 ret->i_mode = mode;
737                 ret->i_uid = ret->i_gid = 0;
738                 ret->i_blksize = PAGE_CACHE_SIZE;
739                 ret->i_blocks = 0;
740                 ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
741         }
742         return ret;
743 }
744
745 #define BOOL_INO_OFFSET 30
746
747 static ssize_t sel_read_bool(struct file *filep, char __user *buf,
748                              size_t count, loff_t *ppos)
749 {
750         char *page = NULL;
751         ssize_t length;
752         ssize_t end;
753         ssize_t ret;
754         int cur_enforcing;
755         struct inode *inode;
756
757         down(&sel_sem);
758
759         ret = -EFAULT;
760
761         /* check to see if this file has been deleted */
762         if (!filep->f_op)
763                 goto out;
764
765         if (count < 0 || count > PAGE_SIZE) {
766                 ret = -EINVAL;
767                 goto out;
768         }
769         if (!(page = (char*)__get_free_page(GFP_KERNEL))) {
770                 ret = -ENOMEM;
771                 goto out;
772         }
773         memset(page, 0, PAGE_SIZE);
774
775         inode = filep->f_dentry->d_inode;
776         cur_enforcing = security_get_bool_value(inode->i_ino - BOOL_INO_OFFSET);
777         if (cur_enforcing < 0) {
778                 ret = cur_enforcing;
779                 goto out;
780         }
781
782         length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
783                           bool_pending_values[inode->i_ino - BOOL_INO_OFFSET]);
784         if (length < 0) {
785                 ret = length;
786                 goto out;
787         }
788
789         if (*ppos >= length) {
790                 ret = 0;
791                 goto out;
792         }
793         if (count + *ppos > length)
794                 count = length - *ppos;
795         end = count + *ppos;
796         if (copy_to_user(buf, (char *) page + *ppos, count)) {
797                 ret = -EFAULT;
798                 goto out;
799         }
800         *ppos = end;
801         ret = count;
802 out:
803         up(&sel_sem);
804         if (page)
805                 free_page((unsigned long)page);
806         return ret;
807 }
808
809 static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
810                               size_t count, loff_t *ppos)
811 {
812         char *page = NULL;
813         ssize_t length = -EFAULT;
814         int new_value;
815         struct inode *inode;
816
817         down(&sel_sem);
818
819         length = task_has_security(current, SECURITY__SETBOOL);
820         if (length)
821                 goto out;
822
823         /* check to see if this file has been deleted */
824         if (!filep->f_op)
825                 goto out;
826
827         if (count < 0 || count >= PAGE_SIZE) {
828                 length = -ENOMEM;
829                 goto out;
830         }
831         if (*ppos != 0) {
832                 /* No partial writes. */
833                 goto out;
834         }
835         page = (char*)__get_free_page(GFP_KERNEL);
836         if (!page) {
837                 length = -ENOMEM;
838                 goto out;
839         }
840         memset(page, 0, PAGE_SIZE);
841
842         if (copy_from_user(page, buf, count))
843                 goto out;
844
845         length = -EINVAL;
846         if (sscanf(page, "%d", &new_value) != 1)
847                 goto out;
848
849         if (new_value)
850                 new_value = 1;
851
852         inode = filep->f_dentry->d_inode;
853         bool_pending_values[inode->i_ino - BOOL_INO_OFFSET] = new_value;
854         length = count;
855
856 out:
857         up(&sel_sem);
858         if (page)
859                 free_page((unsigned long) page);
860         return length;
861 }
862
863 static struct file_operations sel_bool_ops = {
864         .read           = sel_read_bool,
865         .write          = sel_write_bool,
866 };
867
868 static ssize_t sel_commit_bools_write(struct file *filep,
869                                       const char __user *buf,
870                                       size_t count, loff_t *ppos)
871 {
872         char *page = NULL;
873         ssize_t length = -EFAULT;
874         int new_value;
875
876         down(&sel_sem);
877
878         length = task_has_security(current, SECURITY__SETBOOL);
879         if (length)
880                 goto out;
881
882         /* check to see if this file has been deleted */
883         if (!filep->f_op)
884                 goto out;
885
886         if (count < 0 || count >= PAGE_SIZE) {
887                 length = -ENOMEM;
888                 goto out;
889         }
890         if (*ppos != 0) {
891                 /* No partial writes. */
892                 goto out;
893         }
894         page = (char*)__get_free_page(GFP_KERNEL);
895         if (!page) {
896                 length = -ENOMEM;
897                 goto out;
898         }
899
900         memset(page, 0, PAGE_SIZE);
901
902         if (copy_from_user(page, buf, count))
903                 goto out;
904
905         length = -EINVAL;
906         if (sscanf(page, "%d", &new_value) != 1)
907                 goto out;
908
909         if (new_value) {
910                 security_set_bools(bool_num, bool_pending_values);
911         }
912
913         length = count;
914
915 out:
916         up(&sel_sem);
917         if (page)
918                 free_page((unsigned long) page);
919         return length;
920 }
921
922 static struct file_operations sel_commit_bools_ops = {
923         .write          = sel_commit_bools_write,
924 };
925
926 /* delete booleans - partial revoke() from
927  * fs/proc/generic.c proc_kill_inodes */
928 static void sel_remove_bools(struct dentry *de)
929 {
930         struct list_head *p, *node;
931         struct super_block *sb = de->d_sb;
932
933         spin_lock(&dcache_lock);
934         node = de->d_subdirs.next;
935         while (node != &de->d_subdirs) {
936                 struct dentry *d = list_entry(node, struct dentry, d_child);
937                 list_del_init(node);
938
939                 if (d->d_inode) {
940                         d = dget_locked(d);
941                         spin_unlock(&dcache_lock);
942                         d_delete(d);
943                         simple_unlink(de->d_inode, d);
944                         dput(d);
945                         spin_lock(&dcache_lock);
946                 }
947                 node = de->d_subdirs.next;
948         }
949
950         spin_unlock(&dcache_lock);
951
952         file_list_lock();
953         list_for_each(p, &sb->s_files) {
954                 struct file * filp = list_entry(p, struct file, f_list);
955                 struct dentry * dentry = filp->f_dentry;
956
957                 if (dentry->d_parent != de) {
958                         continue;
959                 }
960                 filp->f_op = NULL;
961         }
962         file_list_unlock();
963 }
964
965 #define BOOL_DIR_NAME "booleans"
966
967 static int sel_make_bools(void)
968 {
969         int i, ret = 0;
970         ssize_t len;
971         struct dentry *dentry = NULL;
972         struct dentry *dir = bool_dir;
973         struct inode *inode = NULL;
974         struct inode_security_struct *isec;
975         struct qstr qname;
976         char **names = NULL, *page;
977         int num;
978         int *values = NULL;
979         u32 sid;
980
981         /* remove any existing files */
982         if (bool_pending_values)
983                 kfree(bool_pending_values);
984
985         sel_remove_bools(dir);
986
987         if (!(page = (char*)__get_free_page(GFP_KERNEL)))
988                 return -ENOMEM;
989         memset(page, 0, PAGE_SIZE);
990
991         ret = security_get_bools(&num, &names, &values);
992         if (ret != 0)
993                 goto out;
994
995         for (i = 0; i < num; i++) {
996                 qname.name = names[i];
997                 qname.len = strlen(qname.name);
998                 qname.hash = full_name_hash(qname.name, qname.len);
999                 dentry = d_alloc(dir, &qname);
1000                 if (!dentry) {
1001                         ret = -ENOMEM;
1002                         goto err;
1003                 }
1004                 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1005                 if (!inode) {
1006                         ret = -ENOMEM;
1007                         goto err;
1008                 }
1009
1010                 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
1011                 if (len < 0) {
1012                         ret = -EINVAL;
1013                         goto err;
1014                 } else if (len >= PAGE_SIZE) {
1015                         ret = -ENAMETOOLONG;
1016                         goto err;
1017                 }
1018                 isec = (struct inode_security_struct*)inode->i_security;
1019                 if ((ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid)))
1020                         goto err;
1021                 isec->sid = sid;
1022                 isec->initialized = 1;
1023                 inode->i_fop = &sel_bool_ops;
1024                 inode->i_ino = i + BOOL_INO_OFFSET;
1025                 d_add(dentry, inode);
1026         }
1027         bool_num = num;
1028         bool_pending_values = values;
1029 out:
1030         free_page((unsigned long)page);
1031         if (names) {
1032                 for (i = 0; i < num; i++) {
1033                         if (names[i])
1034                                 kfree(names[i]);
1035                 }
1036                 kfree(names);
1037         }
1038         return ret;
1039 err:
1040         d_genocide(dir);
1041         ret = -ENOMEM;
1042         goto out;
1043 }
1044
1045 static int sel_fill_super(struct super_block * sb, void * data, int silent)
1046 {
1047         int ret;
1048         struct dentry *dentry;
1049         struct inode *inode;
1050         struct qstr qname;
1051
1052         static struct tree_descr selinux_files[] = {
1053                 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1054                 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
1055                 [SEL_CONTEXT] = {"context", &sel_context_ops, S_IRUGO|S_IWUGO},
1056                 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1057                 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1058                 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1059                 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1060                 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1061                 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1062                 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1063                 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1064                 /* last one */ {""}
1065         };
1066         ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1067         if (ret)
1068                 return ret;
1069
1070         qname.name = BOOL_DIR_NAME;
1071         qname.len = strlen(qname.name);
1072         qname.hash = full_name_hash(qname.name, qname.len);
1073         dentry = d_alloc(sb->s_root, &qname);
1074         if (!dentry)
1075                 return -ENOMEM;
1076
1077         inode = sel_make_inode(sb, S_IFDIR | S_IRUGO | S_IXUGO);
1078         if (!inode)
1079                 goto out;
1080         inode->i_op = &simple_dir_inode_operations;
1081         inode->i_fop = &simple_dir_operations;
1082         d_add(dentry, inode);
1083         bool_dir = dentry;
1084         ret = sel_make_bools();
1085         if (ret)
1086                 goto out;
1087
1088         return 0;
1089 out:
1090         dput(dentry);
1091         printk(KERN_ERR "security:      error creating conditional out_dput\n");
1092         return -ENOMEM;
1093 }
1094
1095 static struct super_block *sel_get_sb(struct file_system_type *fs_type,
1096                                       int flags, const char *dev_name, void *data)
1097 {
1098         return get_sb_single(fs_type, flags, data, sel_fill_super);
1099 }
1100
1101 static struct file_system_type sel_fs_type = {
1102         .name           = "selinuxfs",
1103         .get_sb         = sel_get_sb,
1104         .kill_sb        = kill_litter_super,
1105 };
1106
1107 static int __init init_sel_fs(void)
1108 {
1109         return selinux_enabled ? register_filesystem(&sel_fs_type) : 0;
1110 }
1111
1112 __initcall(init_sel_fs);
1113
1114 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
1115 void exit_sel_fs(void)
1116 {
1117         unregister_filesystem(&sel_fs_type);
1118 }
1119 #endif