ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 *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 * 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 * 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 *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 *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 * 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 * 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 *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 *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 *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 *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                 return -ENOMEM;
838         memset(page, 0, PAGE_SIZE);
839
840         if (copy_from_user(page, buf, count))
841                 goto out;
842
843         length = -EINVAL;
844         if (sscanf(page, "%d", &new_value) != 1)
845                 goto out;
846
847         if (new_value)
848                 new_value = 1;
849
850         inode = filep->f_dentry->d_inode;
851         bool_pending_values[inode->i_ino - BOOL_INO_OFFSET] = new_value;
852         length = count;
853
854 out:
855         up(&sel_sem);
856         if (page)
857                 free_page((unsigned long) page);
858         return length;
859 }
860
861 static struct file_operations sel_bool_ops = {
862         .read           = sel_read_bool,
863         .write          = sel_write_bool,
864 };
865
866 static ssize_t sel_commit_bools_write(struct file *filep, const char *buf,
867                                       size_t count, loff_t *ppos)
868 {
869         char *page = NULL;
870         ssize_t length = -EFAULT;
871         int new_value;
872
873         down(&sel_sem);
874
875         length = task_has_security(current, SECURITY__SETBOOL);
876         if (length)
877                 goto out;
878
879         /* check to see if this file has been deleted */
880         if (!filep->f_op)
881                 goto out;
882
883         if (count < 0 || count >= PAGE_SIZE) {
884                 length = -ENOMEM;
885                 goto out;
886         }
887         if (*ppos != 0) {
888                 /* No partial writes. */
889                 goto out;
890         }
891         page = (char*)__get_free_page(GFP_KERNEL);
892         if (!page)
893                 return -ENOMEM;
894
895         memset(page, 0, PAGE_SIZE);
896
897         if (copy_from_user(page, buf, count))
898                 goto out;
899
900         length = -EINVAL;
901         if (sscanf(page, "%d", &new_value) != 1)
902                 goto out;
903
904         if (new_value) {
905                 security_set_bools(bool_num, bool_pending_values);
906         }
907
908         length = count;
909
910 out:
911         up(&sel_sem);
912         if (page)
913                 free_page((unsigned long) page);
914         return length;
915 }
916
917 static struct file_operations sel_commit_bools_ops = {
918         .write          = sel_commit_bools_write,
919 };
920
921 /* delete booleans - partial revoke() from
922  * fs/proc/generic.c proc_kill_inodes */
923 static void sel_remove_bools(struct dentry *de)
924 {
925         struct list_head *p, *node;
926         struct super_block *sb = de->d_sb;
927
928         spin_lock(&dcache_lock);
929         node = de->d_subdirs.next;
930         while (node != &de->d_subdirs) {
931                 struct dentry *d = list_entry(node, struct dentry, d_child);
932                 list_del_init(node);
933
934                 if (d->d_inode) {
935                         d = dget_locked(d);
936                         spin_unlock(&dcache_lock);
937                         d_delete(d);
938                         simple_unlink(de->d_inode, d);
939                         dput(d);
940                         spin_lock(&dcache_lock);
941                 }
942                 node = de->d_subdirs.next;
943         }
944
945         spin_unlock(&dcache_lock);
946
947         file_list_lock();
948         list_for_each(p, &sb->s_files) {
949                 struct file * filp = list_entry(p, struct file, f_list);
950                 struct dentry * dentry = filp->f_dentry;
951
952                 if (dentry->d_parent != de) {
953                         continue;
954                 }
955                 filp->f_op = NULL;
956         }
957         file_list_unlock();
958 }
959
960 #define BOOL_DIR_NAME "booleans"
961
962 static int sel_make_bools(void)
963 {
964         int i, ret = 0;
965         ssize_t len;
966         struct dentry *dentry = NULL;
967         struct dentry *dir = bool_dir;
968         struct inode *inode = NULL;
969         struct inode_security_struct *isec;
970         struct qstr qname;
971         char **names = NULL, *page;
972         int num;
973         int *values = NULL;
974         u32 sid;
975
976         /* remove any existing files */
977         if (bool_pending_values)
978                 kfree(bool_pending_values);
979
980         sel_remove_bools(dir);
981
982         if (!(page = (char*)__get_free_page(GFP_KERNEL)))
983                 return -ENOMEM;
984         memset(page, 0, PAGE_SIZE);
985
986         ret = security_get_bools(&num, &names, &values);
987         if (ret != 0)
988                 goto out;
989
990         for (i = 0; i < num; i++) {
991                 qname.name = names[i];
992                 qname.len = strlen(qname.name);
993                 qname.hash = full_name_hash(qname.name, qname.len);
994                 dentry = d_alloc(dir, &qname);
995                 if (!dentry) {
996                         ret = -ENOMEM;
997                         goto err;
998                 }
999                 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1000                 if (!inode) {
1001                         ret = -ENOMEM;
1002                         goto err;
1003                 }
1004
1005                 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
1006                 if (len < 0) {
1007                         ret = -EINVAL;
1008                         goto err;
1009                 } else if (len >= PAGE_SIZE) {
1010                         ret = -ENAMETOOLONG;
1011                         goto err;
1012                 }
1013                 isec = (struct inode_security_struct*)inode->i_security;
1014                 if ((ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid)))
1015                         goto err;
1016                 isec->sid = sid;
1017                 isec->initialized = 1;
1018                 inode->i_fop = &sel_bool_ops;
1019                 inode->i_ino = i + BOOL_INO_OFFSET;
1020                 d_add(dentry, inode);
1021         }
1022         bool_num = num;
1023         bool_pending_values = values;
1024 out:
1025         free_page((unsigned long)page);
1026         if (names) {
1027                 for (i = 0; i < num; i++) {
1028                         if (names[i])
1029                                 kfree(names[i]);
1030                 }
1031                 kfree(names);
1032         }
1033         return ret;
1034 err:
1035         d_genocide(dir);
1036         ret = -ENOMEM;
1037         goto out;
1038 }
1039
1040 static int sel_fill_super(struct super_block * sb, void * data, int silent)
1041 {
1042         int ret;
1043         struct dentry *dentry;
1044         struct inode *inode;
1045         struct qstr qname;
1046
1047         static struct tree_descr selinux_files[] = {
1048                 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1049                 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
1050                 [SEL_CONTEXT] = {"context", &sel_context_ops, S_IRUGO|S_IWUGO},
1051                 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1052                 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1053                 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1054                 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1055                 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1056                 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1057                 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1058                 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1059                 /* last one */ {""}
1060         };
1061         ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1062         if (ret)
1063                 return ret;
1064
1065         qname.name = BOOL_DIR_NAME;
1066         qname.len = strlen(qname.name);
1067         qname.hash = full_name_hash(qname.name, qname.len);
1068         dentry = d_alloc(sb->s_root, &qname);
1069         if (!dentry)
1070                 return -ENOMEM;
1071
1072         inode = sel_make_inode(sb, S_IFDIR | S_IRUGO | S_IXUGO);
1073         if (!inode)
1074                 goto out;
1075         inode->i_op = &simple_dir_inode_operations;
1076         inode->i_fop = &simple_dir_operations;
1077         d_add(dentry, inode);
1078         bool_dir = dentry;
1079         ret = sel_make_bools();
1080         if (ret)
1081                 goto out;
1082
1083         return 0;
1084 out:
1085         dput(dentry);
1086         printk(KERN_ERR "security:      error creating conditional out_dput\n");
1087         return -ENOMEM;
1088 }
1089
1090 static struct super_block *sel_get_sb(struct file_system_type *fs_type,
1091                                       int flags, const char *dev_name, void *data)
1092 {
1093         return get_sb_single(fs_type, flags, data, sel_fill_super);
1094 }
1095
1096 static struct file_system_type sel_fs_type = {
1097         .name           = "selinuxfs",
1098         .get_sb         = sel_get_sb,
1099         .kill_sb        = kill_litter_super,
1100 };
1101
1102 static int __init init_sel_fs(void)
1103 {
1104         return selinux_enabled ? register_filesystem(&sel_fs_type) : 0;
1105 }
1106
1107 __initcall(init_sel_fs);
1108
1109 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
1110 void exit_sel_fs(void)
1111 {
1112         unregister_filesystem(&sel_fs_type);
1113 }
1114 #endif