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