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