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