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