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