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