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