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