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